authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-09-13 21:14:40-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-09-13 21:14:40-07:00
loga9a21c59888905e060915dee818633110cc54cfa
tree8b8ddfd2fb56dc92abf01a6e4de60243b37c1f32
parent5529febab056c870f7b2a123b0645b5e3b1146c9

stage2: Type/Value use an enum rather than usize

Makes debugging nicer when you want to look at Type/Value

2 files changed, 18 insertions(+), 18 deletions(-)

src/type.zig+10-10
...@@ -18,7 +18,7 @@ const file_struct = @This();...@@ -18,7 +18,7 @@ const file_struct = @This();
18pub const Type = extern union {18pub const Type = extern union {
19 /// If the tag value is less than Tag.no_payload_count, then no pointer19 /// If the tag value is less than Tag.no_payload_count, then no pointer
20 /// dereference is needed.20 /// dereference is needed.
21 tag_if_small_enough: usize,21 tag_if_small_enough: Tag,
22 ptr_otherwise: *Payload,22 ptr_otherwise: *Payload,
2323
24 pub fn zigTypeTag(ty: Type) std.builtin.TypeId {24 pub fn zigTypeTag(ty: Type) std.builtin.TypeId {
...@@ -178,7 +178,7 @@ pub const Type = extern union {...@@ -178,7 +178,7 @@ pub const Type = extern union {
178178
179 pub fn initTag(comptime small_tag: Tag) Type {179 pub fn initTag(comptime small_tag: Tag) Type {
180 comptime assert(@enumToInt(small_tag) < Tag.no_payload_count);180 comptime assert(@enumToInt(small_tag) < Tag.no_payload_count);
181 return .{ .tag_if_small_enough = @enumToInt(small_tag) };181 return .{ .tag_if_small_enough = small_tag };
182 }182 }
183183
184 pub fn initPayload(payload: *Payload) Type {184 pub fn initPayload(payload: *Payload) Type {
...@@ -187,8 +187,8 @@ pub const Type = extern union {...@@ -187,8 +187,8 @@ pub const Type = extern union {
187 }187 }
188188
189 pub fn tag(self: Type) Tag {189 pub fn tag(self: Type) Tag {
190 if (self.tag_if_small_enough < Tag.no_payload_count) {190 if (@enumToInt(self.tag_if_small_enough) < Tag.no_payload_count) {
191 return @intToEnum(Tag, @intCast(std.meta.Tag(Tag), self.tag_if_small_enough));191 return self.tag_if_small_enough;
192 } else {192 } else {
193 return self.ptr_otherwise.tag;193 return self.ptr_otherwise.tag;
194 }194 }
...@@ -199,7 +199,7 @@ pub const Type = extern union {...@@ -199,7 +199,7 @@ pub const Type = extern union {
199 if (@hasField(T, "base_tag")) {199 if (@hasField(T, "base_tag")) {
200 return self.castTag(T.base_tag);200 return self.castTag(T.base_tag);
201 }201 }
202 if (self.tag_if_small_enough < Tag.no_payload_count) {202 if (@enumToInt(self.tag_if_small_enough) < Tag.no_payload_count) {
203 return null;203 return null;
204 }204 }
205 inline for (@typeInfo(Tag).Enum.fields) |field| {205 inline for (@typeInfo(Tag).Enum.fields) |field| {
...@@ -217,7 +217,7 @@ pub const Type = extern union {...@@ -217,7 +217,7 @@ pub const Type = extern union {
217 }217 }
218218
219 pub fn castTag(self: Type, comptime t: Tag) ?*t.Type() {219 pub fn castTag(self: Type, comptime t: Tag) ?*t.Type() {
220 if (self.tag_if_small_enough < Tag.no_payload_count)220 if (@enumToInt(self.tag_if_small_enough) < Tag.no_payload_count)
221 return null;221 return null;
222222
223 if (self.ptr_otherwise.tag == t)223 if (self.ptr_otherwise.tag == t)
...@@ -690,7 +690,7 @@ pub const Type = extern union {...@@ -690,7 +690,7 @@ pub const Type = extern union {
690 };690 };
691691
692 pub fn copy(self: Type, allocator: *Allocator) error{OutOfMemory}!Type {692 pub fn copy(self: Type, allocator: *Allocator) error{OutOfMemory}!Type {
693 if (self.tag_if_small_enough < Tag.no_payload_count) {693 if (@enumToInt(self.tag_if_small_enough) < Tag.no_payload_count) {
694 return Type{ .tag_if_small_enough = self.tag_if_small_enough };694 return Type{ .tag_if_small_enough = self.tag_if_small_enough };
695 } else switch (self.ptr_otherwise.tag) {695 } else switch (self.ptr_otherwise.tag) {
696 .u1,696 .u1,
...@@ -1459,7 +1459,7 @@ pub const Type = extern union {...@@ -1459,7 +1459,7 @@ pub const Type = extern union {
14591459
1460 pub fn isNoReturn(self: Type) bool {1460 pub fn isNoReturn(self: Type) bool {
1461 const definitely_correct_result = self.zigTypeTag() == .NoReturn;1461 const definitely_correct_result = self.zigTypeTag() == .NoReturn;
1462 const fast_result = self.tag_if_small_enough == @enumToInt(Tag.noreturn);1462 const fast_result = self.tag_if_small_enough == Tag.noreturn;
1463 assert(fast_result == definitely_correct_result);1463 assert(fast_result == definitely_correct_result);
1464 return fast_result;1464 return fast_result;
1465 }1465 }
...@@ -3165,7 +3165,7 @@ pub const Type = extern union {...@@ -3165,7 +3165,7 @@ pub const Type = extern union {
3165 /// but with different alignment values, in this data structure they are represented3165 /// but with different alignment values, in this data structure they are represented
3166 /// with different enum tags, because the the former requires more payload data than the latter.3166 /// with different enum tags, because the the former requires more payload data than the latter.
3167 /// See `zigTypeTag` for the function that corresponds to `std.builtin.TypeId`.3167 /// See `zigTypeTag` for the function that corresponds to `std.builtin.TypeId`.
3168 pub const Tag = enum {3168 pub const Tag = enum(usize) {
3169 // The first section of this enum are tags that require no payload.3169 // The first section of this enum are tags that require no payload.
3170 u1,3170 u1,
3171 u8,3171 u8,
...@@ -3382,7 +3382,7 @@ pub const Type = extern union {...@@ -3382,7 +3382,7 @@ pub const Type = extern union {
33823382
3383 pub fn init(comptime t: Tag) file_struct.Type {3383 pub fn init(comptime t: Tag) file_struct.Type {
3384 comptime std.debug.assert(@enumToInt(t) < Tag.no_payload_count);3384 comptime std.debug.assert(@enumToInt(t) < Tag.no_payload_count);
3385 return .{ .tag_if_small_enough = @enumToInt(t) };3385 return .{ .tag_if_small_enough = t };
3386 }3386 }
33873387
3388 pub fn create(comptime t: Tag, ally: *Allocator, data: Data(t)) error{OutOfMemory}!file_struct.Type {3388 pub fn create(comptime t: Tag, ally: *Allocator, data: Data(t)) error{OutOfMemory}!file_struct.Type {
src/value.zig+8-8
...@@ -17,10 +17,10 @@ const Air = @import("Air.zig");...@@ -17,10 +17,10 @@ const Air = @import("Air.zig");
17pub const Value = extern union {17pub const Value = extern union {
18 /// If the tag value is less than Tag.no_payload_count, then no pointer18 /// If the tag value is less than Tag.no_payload_count, then no pointer
19 /// dereference is needed.19 /// dereference is needed.
20 tag_if_small_enough: usize,20 tag_if_small_enough: Tag,
21 ptr_otherwise: *Payload,21 ptr_otherwise: *Payload,
2222
23 pub const Tag = enum {23 pub const Tag = enum(usize) {
24 // The first section of this enum are tags that require no payload.24 // The first section of this enum are tags that require no payload.
25 u1_type,25 u1_type,
26 u8_type,26 u8_type,
...@@ -296,7 +296,7 @@ pub const Value = extern union {...@@ -296,7 +296,7 @@ pub const Value = extern union {
296296
297 pub fn initTag(small_tag: Tag) Value {297 pub fn initTag(small_tag: Tag) Value {
298 assert(@enumToInt(small_tag) < Tag.no_payload_count);298 assert(@enumToInt(small_tag) < Tag.no_payload_count);
299 return .{ .tag_if_small_enough = @enumToInt(small_tag) };299 return .{ .tag_if_small_enough = small_tag };
300 }300 }
301301
302 pub fn initPayload(payload: *Payload) Value {302 pub fn initPayload(payload: *Payload) Value {
...@@ -305,8 +305,8 @@ pub const Value = extern union {...@@ -305,8 +305,8 @@ pub const Value = extern union {
305 }305 }
306306
307 pub fn tag(self: Value) Tag {307 pub fn tag(self: Value) Tag {
308 if (self.tag_if_small_enough < Tag.no_payload_count) {308 if (@enumToInt(self.tag_if_small_enough) < Tag.no_payload_count) {
309 return @intToEnum(Tag, @intCast(std.meta.Tag(Tag), self.tag_if_small_enough));309 return self.tag_if_small_enough;
310 } else {310 } else {
311 return self.ptr_otherwise.tag;311 return self.ptr_otherwise.tag;
312 }312 }
...@@ -317,7 +317,7 @@ pub const Value = extern union {...@@ -317,7 +317,7 @@ pub const Value = extern union {
317 if (@hasField(T, "base_tag")) {317 if (@hasField(T, "base_tag")) {
318 return self.castTag(T.base_tag);318 return self.castTag(T.base_tag);
319 }319 }
320 if (self.tag_if_small_enough < Tag.no_payload_count) {320 if (@enumToInt(self.tag_if_small_enough) < Tag.no_payload_count) {
321 return null;321 return null;
322 }322 }
323 inline for (@typeInfo(Tag).Enum.fields) |field| {323 inline for (@typeInfo(Tag).Enum.fields) |field| {
...@@ -335,7 +335,7 @@ pub const Value = extern union {...@@ -335,7 +335,7 @@ pub const Value = extern union {
335 }335 }
336336
337 pub fn castTag(self: Value, comptime t: Tag) ?*t.Type() {337 pub fn castTag(self: Value, comptime t: Tag) ?*t.Type() {
338 if (self.tag_if_small_enough < Tag.no_payload_count)338 if (@enumToInt(self.tag_if_small_enough) < Tag.no_payload_count)
339 return null;339 return null;
340340
341 if (self.ptr_otherwise.tag == t)341 if (self.ptr_otherwise.tag == t)
...@@ -347,7 +347,7 @@ pub const Value = extern union {...@@ -347,7 +347,7 @@ pub const Value = extern union {
347 /// It's intentional that this function is not passed a corresponding Type, so that347 /// It's intentional that this function is not passed a corresponding Type, so that
348 /// a Value can be copied from a Sema to a Decl prior to resolving struct/union field types.348 /// a Value can be copied from a Sema to a Decl prior to resolving struct/union field types.
349 pub fn copy(self: Value, arena: *Allocator) error{OutOfMemory}!Value {349 pub fn copy(self: Value, arena: *Allocator) error{OutOfMemory}!Value {
350 if (self.tag_if_small_enough < Tag.no_payload_count) {350 if (@enumToInt(self.tag_if_small_enough) < Tag.no_payload_count) {
351 return Value{ .tag_if_small_enough = self.tag_if_small_enough };351 return Value{ .tag_if_small_enough = self.tag_if_small_enough };
352 } else switch (self.ptr_otherwise.tag) {352 } else switch (self.ptr_otherwise.tag) {
353 .u1_type,353 .u1_type,