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();
1818pub const Type = extern union {
1919 /// If the tag value is less than Tag.no_payload_count, then no pointer
2020 /// dereference is needed.
21 tag_if_small_enough: usize,
21 tag_if_small_enough: Tag,
2222 ptr_otherwise: *Payload,
2323
2424 pub fn zigTypeTag(ty: Type) std.builtin.TypeId {
......@@ -178,7 +178,7 @@ pub const Type = extern union {
178178
179179 pub fn initTag(comptime small_tag: Tag) Type {
180180 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 };
182182 }
183183
184184 pub fn initPayload(payload: *Payload) Type {
......@@ -187,8 +187,8 @@ pub const Type = extern union {
187187 }
188188
189189 pub fn tag(self: Type) Tag {
190 if (self.tag_if_small_enough < Tag.no_payload_count) {
191 return @intToEnum(Tag, @intCast(std.meta.Tag(Tag), self.tag_if_small_enough));
190 if (@enumToInt(self.tag_if_small_enough) < Tag.no_payload_count) {
191 return self.tag_if_small_enough;
192192 } else {
193193 return self.ptr_otherwise.tag;
194194 }
......@@ -199,7 +199,7 @@ pub const Type = extern union {
199199 if (@hasField(T, "base_tag")) {
200200 return self.castTag(T.base_tag);
201201 }
202 if (self.tag_if_small_enough < Tag.no_payload_count) {
202 if (@enumToInt(self.tag_if_small_enough) < Tag.no_payload_count) {
203203 return null;
204204 }
205205 inline for (@typeInfo(Tag).Enum.fields) |field| {
......@@ -217,7 +217,7 @@ pub const Type = extern union {
217217 }
218218
219219 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)
221221 return null;
222222
223223 if (self.ptr_otherwise.tag == t)
......@@ -690,7 +690,7 @@ pub const Type = extern union {
690690 };
691691
692692 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) {
694694 return Type{ .tag_if_small_enough = self.tag_if_small_enough };
695695 } else switch (self.ptr_otherwise.tag) {
696696 .u1,
......@@ -1459,7 +1459,7 @@ pub const Type = extern union {
14591459
14601460 pub fn isNoReturn(self: Type) bool {
14611461 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;
14631463 assert(fast_result == definitely_correct_result);
14641464 return fast_result;
14651465 }
......@@ -3165,7 +3165,7 @@ pub const Type = extern union {
31653165 /// but with different alignment values, in this data structure they are represented
31663166 /// with different enum tags, because the the former requires more payload data than the latter.
31673167 /// See `zigTypeTag` for the function that corresponds to `std.builtin.TypeId`.
3168 pub const Tag = enum {
3168 pub const Tag = enum(usize) {
31693169 // The first section of this enum are tags that require no payload.
31703170 u1,
31713171 u8,
......@@ -3382,7 +3382,7 @@ pub const Type = extern union {
33823382
33833383 pub fn init(comptime t: Tag) file_struct.Type {
33843384 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 };
33863386 }
33873387
33883388 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");
1717pub const Value = extern union {
1818 /// If the tag value is less than Tag.no_payload_count, then no pointer
1919 /// dereference is needed.
20 tag_if_small_enough: usize,
20 tag_if_small_enough: Tag,
2121 ptr_otherwise: *Payload,
2222
23 pub const Tag = enum {
23 pub const Tag = enum(usize) {
2424 // The first section of this enum are tags that require no payload.
2525 u1_type,
2626 u8_type,
......@@ -296,7 +296,7 @@ pub const Value = extern union {
296296
297297 pub fn initTag(small_tag: Tag) Value {
298298 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 };
300300 }
301301
302302 pub fn initPayload(payload: *Payload) Value {
......@@ -305,8 +305,8 @@ pub const Value = extern union {
305305 }
306306
307307 pub fn tag(self: Value) Tag {
308 if (self.tag_if_small_enough < Tag.no_payload_count) {
309 return @intToEnum(Tag, @intCast(std.meta.Tag(Tag), self.tag_if_small_enough));
308 if (@enumToInt(self.tag_if_small_enough) < Tag.no_payload_count) {
309 return self.tag_if_small_enough;
310310 } else {
311311 return self.ptr_otherwise.tag;
312312 }
......@@ -317,7 +317,7 @@ pub const Value = extern union {
317317 if (@hasField(T, "base_tag")) {
318318 return self.castTag(T.base_tag);
319319 }
320 if (self.tag_if_small_enough < Tag.no_payload_count) {
320 if (@enumToInt(self.tag_if_small_enough) < Tag.no_payload_count) {
321321 return null;
322322 }
323323 inline for (@typeInfo(Tag).Enum.fields) |field| {
......@@ -335,7 +335,7 @@ pub const Value = extern union {
335335 }
336336
337337 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)
339339 return null;
340340
341341 if (self.ptr_otherwise.tag == t)
......@@ -347,7 +347,7 @@ pub const Value = extern union {
347347 /// It's intentional that this function is not passed a corresponding Type, so that
348348 /// a Value can be copied from a Sema to a Decl prior to resolving struct/union field types.
349349 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) {
351351 return Value{ .tag_if_small_enough = self.tag_if_small_enough };
352352 } else switch (self.ptr_otherwise.tag) {
353353 .u1_type,