authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2020-08-18 14:28:33+03:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2020-08-18 14:28:33+03:00
loge0b01bd4a98e2605f197a04f84e0c281ccc90f81
tree0bac6d2e6d24d57e0d6312dcf637e2a7915288be
parent31d8efc6b32a6b60b7ab292ac50e0f6c02bc140e
signature Commit is signed but in an unrecognized format.

stage2: enum literals


5 files changed, 98 insertions(+), 4 deletions(-)

src-self-hosted/astgen.zig+9-1
......@@ -130,6 +130,7 @@ pub fn expr(mod: *Module, scope: *Scope, rl: ResultLoc, node: *ast.Node) InnerEr
130130 .GroupedExpression => return expr(mod, scope, rl, node.castTag(.GroupedExpression).?.expr),
131131 .ArrayType => return rlWrap(mod, scope, rl, try arrayType(mod, scope, node.castTag(.ArrayType).?)),
132132 .ArrayTypeSentinel => return rlWrap(mod, scope, rl, try arrayTypeSentinel(mod, scope, node.castTag(.ArrayTypeSentinel).?)),
133 .EnumLiteral => return rlWrap(mod, scope, rl, try enumLiteral(mod, scope, node.castTag(.EnumLiteral).?)),
133134
134135 .Defer => return mod.failNode(scope, node, "TODO implement astgen.expr for .Defer", .{}),
135136 .Catch => return mod.failNode(scope, node, "TODO implement astgen.expr for .Catch", .{}),
......@@ -158,7 +159,6 @@ pub fn expr(mod: *Module, scope: *Scope, rl: ResultLoc, node: *ast.Node) InnerEr
158159 .ErrorType => return mod.failNode(scope, node, "TODO implement astgen.expr for .ErrorType", .{}),
159160 .FnProto => return mod.failNode(scope, node, "TODO implement astgen.expr for .FnProto", .{}),
160161 .AnyFrameType => return mod.failNode(scope, node, "TODO implement astgen.expr for .AnyFrameType", .{}),
161 .EnumLiteral => return mod.failNode(scope, node, "TODO implement astgen.expr for .EnumLiteral", .{}),
162162 .MultilineStringLiteral => return mod.failNode(scope, node, "TODO implement astgen.expr for .MultilineStringLiteral", .{}),
163163 .CharLiteral => return mod.failNode(scope, node, "TODO implement astgen.expr for .CharLiteral", .{}),
164164 .ErrorSetDecl => return mod.failNode(scope, node, "TODO implement astgen.expr for .ErrorSetDecl", .{}),
......@@ -527,6 +527,14 @@ fn arrayTypeSentinel(mod: *Module, scope: *Scope, node: *ast.Node.ArrayTypeSenti
527527 }, .{});
528528}
529529
530fn enumLiteral(mod: *Module, scope: *Scope, node: *ast.Node.EnumLiteral) !*zir.Inst {
531 const tree = scope.tree();
532 const src = tree.token_locs[node.name].start;
533 const name = try identifierTokenString(mod, scope, node.name);
534
535 return addZIRInst(mod, scope, src, zir.Inst.EnumLiteral, .{ .name = name }, .{});
536}
537
530538fn unwrapOptional(mod: *Module, scope: *Scope, rl: ResultLoc, node: *ast.Node.SimpleSuffixOp) InnerError!*zir.Inst {
531539 const tree = scope.tree();
532540 const src = tree.token_locs[node.rtoken].start;
src-self-hosted/type.zig+29-1
......@@ -75,6 +75,7 @@ pub const Type = extern union {
7575 .optional_single_const_pointer,
7676 .optional_single_mut_pointer,
7777 => return .Optional,
78 .enum_literal => return .EnumLiteral,
7879 }
7980 }
8081
......@@ -127,6 +128,7 @@ pub const Type = extern union {
127128 if (zig_tag_a != zig_tag_b)
128129 return false;
129130 switch (zig_tag_a) {
131 .EnumLiteral => return true,
130132 .Type => return true,
131133 .Void => return true,
132134 .Bool => return true,
......@@ -211,7 +213,6 @@ pub const Type = extern union {
211213 .Frame,
212214 .AnyFrame,
213215 .Vector,
214 .EnumLiteral,
215216 => std.debug.panic("TODO implement Type equality comparison of {} and {}", .{ a, b }),
216217 }
217218 }
......@@ -327,6 +328,7 @@ pub const Type = extern union {
327328 .fn_ccc_void_no_args,
328329 .single_const_pointer_to_comptime_int,
329330 .const_slice_u8,
331 .enum_literal,
330332 => unreachable,
331333
332334 .array_u8_sentinel_0 => return self.copyPayloadShallow(allocator, Payload.Array_u8_Sentinel0),
......@@ -437,6 +439,7 @@ pub const Type = extern union {
437439 .noreturn,
438440 => return out_stream.writeAll(@tagName(t)),
439441
442 .enum_literal => return out_stream.writeAll("@TypeOf(.EnumLiteral)"),
440443 .@"null" => return out_stream.writeAll("@TypeOf(null)"),
441444 .@"undefined" => return out_stream.writeAll("@TypeOf(undefined)"),
442445
......@@ -561,6 +564,7 @@ pub const Type = extern union {
561564 .fn_ccc_void_no_args => return Value.initTag(.fn_ccc_void_no_args_type),
562565 .single_const_pointer_to_comptime_int => return Value.initTag(.single_const_pointer_to_comptime_int_type),
563566 .const_slice_u8 => return Value.initTag(.const_slice_u8_type),
567 .enum_literal => return Value.initTag(.enum_literal_type),
564568 else => {
565569 const ty_payload = try allocator.create(Value.Payload.Ty);
566570 ty_payload.* = .{ .ty = self };
......@@ -625,6 +629,7 @@ pub const Type = extern union {
625629 .noreturn,
626630 .@"null",
627631 .@"undefined",
632 .enum_literal,
628633 => false,
629634 };
630635 }
......@@ -716,6 +721,7 @@ pub const Type = extern union {
716721 .noreturn,
717722 .@"null",
718723 .@"undefined",
724 .enum_literal,
719725 => unreachable,
720726 };
721727 }
......@@ -736,6 +742,7 @@ pub const Type = extern union {
736742 .noreturn => unreachable,
737743 .@"null" => unreachable,
738744 .@"undefined" => unreachable,
745 .enum_literal => unreachable,
739746
740747 .u8,
741748 .i8,
......@@ -863,6 +870,7 @@ pub const Type = extern union {
863870 .optional,
864871 .optional_single_mut_pointer,
865872 .optional_single_const_pointer,
873 .enum_literal,
866874 => false,
867875
868876 .single_const_pointer,
......@@ -924,6 +932,7 @@ pub const Type = extern union {
924932 .optional,
925933 .optional_single_mut_pointer,
926934 .optional_single_const_pointer,
935 .enum_literal,
927936 => false,
928937
929938 .const_slice_u8 => true,
......@@ -980,6 +989,7 @@ pub const Type = extern union {
980989 .optional,
981990 .optional_single_mut_pointer,
982991 .optional_single_const_pointer,
992 .enum_literal,
983993 => false,
984994
985995 .single_const_pointer,
......@@ -1042,6 +1052,7 @@ pub const Type = extern union {
10421052 .optional,
10431053 .optional_single_mut_pointer,
10441054 .optional_single_const_pointer,
1055 .enum_literal,
10451056 => false,
10461057 };
10471058 }
......@@ -1147,6 +1158,7 @@ pub const Type = extern union {
11471158 .optional,
11481159 .optional_single_const_pointer,
11491160 .optional_single_mut_pointer,
1161 .enum_literal,
11501162 => unreachable,
11511163
11521164 .array => self.cast(Payload.Array).?.elem_type,
......@@ -1252,6 +1264,7 @@ pub const Type = extern union {
12521264 .optional,
12531265 .optional_single_mut_pointer,
12541266 .optional_single_const_pointer,
1267 .enum_literal,
12551268 => unreachable,
12561269
12571270 .array => self.cast(Payload.Array).?.len,
......@@ -1311,6 +1324,7 @@ pub const Type = extern union {
13111324 .optional,
13121325 .optional_single_mut_pointer,
13131326 .optional_single_const_pointer,
1327 .enum_literal,
13141328 => unreachable,
13151329
13161330 .array, .array_u8 => return null,
......@@ -1368,6 +1382,7 @@ pub const Type = extern union {
13681382 .optional,
13691383 .optional_single_mut_pointer,
13701384 .optional_single_const_pointer,
1385 .enum_literal,
13711386 => false,
13721387
13731388 .int_signed,
......@@ -1428,6 +1443,7 @@ pub const Type = extern union {
14281443 .optional,
14291444 .optional_single_mut_pointer,
14301445 .optional_single_const_pointer,
1446 .enum_literal,
14311447 => false,
14321448
14331449 .int_unsigned,
......@@ -1478,6 +1494,7 @@ pub const Type = extern union {
14781494 .optional,
14791495 .optional_single_mut_pointer,
14801496 .optional_single_const_pointer,
1497 .enum_literal,
14811498 => unreachable,
14821499
14831500 .int_unsigned => .{ .signed = false, .bits = self.cast(Payload.IntUnsigned).?.bits },
......@@ -1546,6 +1563,7 @@ pub const Type = extern union {
15461563 .optional,
15471564 .optional_single_mut_pointer,
15481565 .optional_single_const_pointer,
1566 .enum_literal,
15491567 => false,
15501568
15511569 .usize,
......@@ -1643,6 +1661,7 @@ pub const Type = extern union {
16431661 .optional,
16441662 .optional_single_mut_pointer,
16451663 .optional_single_const_pointer,
1664 .enum_literal,
16461665 => unreachable,
16471666 };
16481667 }
......@@ -1706,6 +1725,7 @@ pub const Type = extern union {
17061725 .optional,
17071726 .optional_single_mut_pointer,
17081727 .optional_single_const_pointer,
1728 .enum_literal,
17091729 => unreachable,
17101730 }
17111731 }
......@@ -1768,6 +1788,7 @@ pub const Type = extern union {
17681788 .optional,
17691789 .optional_single_mut_pointer,
17701790 .optional_single_const_pointer,
1791 .enum_literal,
17711792 => unreachable,
17721793 }
17731794 }
......@@ -1830,6 +1851,7 @@ pub const Type = extern union {
18301851 .optional,
18311852 .optional_single_mut_pointer,
18321853 .optional_single_const_pointer,
1854 .enum_literal,
18331855 => unreachable,
18341856 };
18351857 }
......@@ -1889,6 +1911,7 @@ pub const Type = extern union {
18891911 .optional,
18901912 .optional_single_mut_pointer,
18911913 .optional_single_const_pointer,
1914 .enum_literal,
18921915 => unreachable,
18931916 };
18941917 }
......@@ -1948,6 +1971,7 @@ pub const Type = extern union {
19481971 .optional,
19491972 .optional_single_mut_pointer,
19501973 .optional_single_const_pointer,
1974 .enum_literal,
19511975 => unreachable,
19521976 };
19531977 }
......@@ -2007,6 +2031,7 @@ pub const Type = extern union {
20072031 .optional,
20082032 .optional_single_mut_pointer,
20092033 .optional_single_const_pointer,
2034 .enum_literal,
20102035 => false,
20112036 };
20122037 }
......@@ -2055,6 +2080,7 @@ pub const Type = extern union {
20552080 .optional,
20562081 .optional_single_mut_pointer,
20572082 .optional_single_const_pointer,
2083 .enum_literal,
20582084 => return null,
20592085
20602086 .void => return Value.initTag(.void_value),
......@@ -2143,6 +2169,7 @@ pub const Type = extern union {
21432169 .optional,
21442170 .optional_single_mut_pointer,
21452171 .optional_single_const_pointer,
2172 .enum_literal,
21462173 => return false,
21472174 };
21482175 }
......@@ -2186,6 +2213,7 @@ pub const Type = extern union {
21862213 comptime_int,
21872214 comptime_float,
21882215 noreturn,
2216 enum_literal,
21892217 @"null",
21902218 @"undefined",
21912219 fn_noreturn_no_args,
src-self-hosted/value.zig+33-2
......@@ -60,6 +60,7 @@ pub const Value = extern union {
6060 fn_ccc_void_no_args_type,
6161 single_const_pointer_to_comptime_int_type,
6262 const_slice_u8_type,
63 enum_literal_type,
6364
6465 undef,
6566 zero,
......@@ -87,6 +88,7 @@ pub const Value = extern union {
8788 float_32,
8889 float_64,
8990 float_128,
91 enum_literal,
9092
9193 pub const last_no_payload_tag = Tag.bool_false;
9294 pub const no_payload_count = @enumToInt(last_no_payload_tag) + 1;
......@@ -164,6 +166,7 @@ pub const Value = extern union {
164166 .fn_ccc_void_no_args_type,
165167 .single_const_pointer_to_comptime_int_type,
166168 .const_slice_u8_type,
169 .enum_literal_type,
167170 .undef,
168171 .zero,
169172 .void_value,
......@@ -213,7 +216,7 @@ pub const Value = extern union {
213216 };
214217 return Value{ .ptr_otherwise = &new_payload.base };
215218 },
216 .bytes => return self.copyPayloadShallow(allocator, Payload.Bytes),
219 .enum_literal, .bytes => return self.copyPayloadShallow(allocator, Payload.Bytes),
217220 .repeated => {
218221 const payload = @fieldParentPtr(Payload.Repeated, "base", self.ptr_otherwise);
219222 const new_payload = try allocator.create(Payload.Repeated);
......@@ -285,6 +288,7 @@ pub const Value = extern union {
285288 .fn_ccc_void_no_args_type => return out_stream.writeAll("fn() callconv(.C) void"),
286289 .single_const_pointer_to_comptime_int_type => return out_stream.writeAll("*const comptime_int"),
287290 .const_slice_u8_type => return out_stream.writeAll("[]const u8"),
291 .enum_literal_type => return out_stream.writeAll("@TypeOf(.EnumLiteral)"),
288292
289293 .null_value => return out_stream.writeAll("null"),
290294 .undef => return out_stream.writeAll("undefined"),
......@@ -318,7 +322,7 @@ pub const Value = extern union {
318322 val = elem_ptr.array_ptr;
319323 },
320324 .empty_array => return out_stream.writeAll(".{}"),
321 .bytes => return std.zig.renderStringLiteral(self.cast(Payload.Bytes).?.data, out_stream),
325 .enum_literal, .bytes => return std.zig.renderStringLiteral(self.cast(Payload.Bytes).?.data, out_stream),
322326 .repeated => {
323327 try out_stream.writeAll("(repeated) ");
324328 val = val.cast(Payload.Repeated).?.val;
......@@ -391,6 +395,7 @@ pub const Value = extern union {
391395 .fn_ccc_void_no_args_type => Type.initTag(.fn_ccc_void_no_args),
392396 .single_const_pointer_to_comptime_int_type => Type.initTag(.single_const_pointer_to_comptime_int),
393397 .const_slice_u8_type => Type.initTag(.const_slice_u8),
398 .enum_literal_type => Type.initTag(.enum_literal),
394399
395400 .undef,
396401 .zero,
......@@ -414,6 +419,7 @@ pub const Value = extern union {
414419 .float_32,
415420 .float_64,
416421 .float_128,
422 .enum_literal,
417423 => unreachable,
418424 };
419425 }
......@@ -462,6 +468,7 @@ pub const Value = extern union {
462468 .fn_ccc_void_no_args_type,
463469 .single_const_pointer_to_comptime_int_type,
464470 .const_slice_u8_type,
471 .enum_literal_type,
465472 .null_value,
466473 .function,
467474 .ref_val,
......@@ -476,6 +483,7 @@ pub const Value = extern union {
476483 .void_value,
477484 .unreachable_value,
478485 .empty_array,
486 .enum_literal,
479487 => unreachable,
480488
481489 .undef => unreachable,
......@@ -537,6 +545,7 @@ pub const Value = extern union {
537545 .fn_ccc_void_no_args_type,
538546 .single_const_pointer_to_comptime_int_type,
539547 .const_slice_u8_type,
548 .enum_literal_type,
540549 .null_value,
541550 .function,
542551 .ref_val,
......@@ -551,6 +560,7 @@ pub const Value = extern union {
551560 .void_value,
552561 .unreachable_value,
553562 .empty_array,
563 .enum_literal,
554564 => unreachable,
555565
556566 .undef => unreachable,
......@@ -612,6 +622,7 @@ pub const Value = extern union {
612622 .fn_ccc_void_no_args_type,
613623 .single_const_pointer_to_comptime_int_type,
614624 .const_slice_u8_type,
625 .enum_literal_type,
615626 .null_value,
616627 .function,
617628 .ref_val,
......@@ -626,6 +637,7 @@ pub const Value = extern union {
626637 .void_value,
627638 .unreachable_value,
628639 .empty_array,
640 .enum_literal,
629641 => unreachable,
630642
631643 .undef => unreachable,
......@@ -713,6 +725,7 @@ pub const Value = extern union {
713725 .fn_ccc_void_no_args_type,
714726 .single_const_pointer_to_comptime_int_type,
715727 .const_slice_u8_type,
728 .enum_literal_type,
716729 .null_value,
717730 .function,
718731 .ref_val,
......@@ -728,6 +741,7 @@ pub const Value = extern union {
728741 .void_value,
729742 .unreachable_value,
730743 .empty_array,
744 .enum_literal,
731745 => unreachable,
732746
733747 .zero,
......@@ -793,6 +807,7 @@ pub const Value = extern union {
793807 .fn_ccc_void_no_args_type,
794808 .single_const_pointer_to_comptime_int_type,
795809 .const_slice_u8_type,
810 .enum_literal_type,
796811 .null_value,
797812 .function,
798813 .ref_val,
......@@ -807,6 +822,7 @@ pub const Value = extern union {
807822 .void_value,
808823 .unreachable_value,
809824 .empty_array,
825 .enum_literal,
810826 => unreachable,
811827
812828 .zero,
......@@ -953,6 +969,7 @@ pub const Value = extern union {
953969 .fn_ccc_void_no_args_type,
954970 .single_const_pointer_to_comptime_int_type,
955971 .const_slice_u8_type,
972 .enum_literal_type,
956973 .bool_true,
957974 .bool_false,
958975 .null_value,
......@@ -970,6 +987,7 @@ pub const Value = extern union {
970987 .empty_array,
971988 .void_value,
972989 .unreachable_value,
990 .enum_literal,
973991 => unreachable,
974992
975993 .zero => false,
......@@ -1025,6 +1043,7 @@ pub const Value = extern union {
10251043 .fn_ccc_void_no_args_type,
10261044 .single_const_pointer_to_comptime_int_type,
10271045 .const_slice_u8_type,
1046 .enum_literal_type,
10281047 .null_value,
10291048 .function,
10301049 .ref_val,
......@@ -1036,6 +1055,7 @@ pub const Value = extern union {
10361055 .void_value,
10371056 .unreachable_value,
10381057 .empty_array,
1058 .enum_literal,
10391059 => unreachable,
10401060
10411061 .zero,
......@@ -1102,6 +1122,11 @@ pub const Value = extern union {
11021122 }
11031123
11041124 pub fn eql(a: Value, b: Value) bool {
1125 if (a.tag() == b.tag() and a.tag() == .enum_literal) {
1126 const a_name = @fieldParentPtr(Payload.Bytes, "base", a.ptr_otherwise).data;
1127 const b_name = @fieldParentPtr(Payload.Bytes, "base", b.ptr_otherwise).data;
1128 return std.mem.eql(u8, a_name, b_name);
1129 }
11051130 // TODO non numerical comparisons
11061131 return compare(a, .eq, b);
11071132 }
......@@ -1151,6 +1176,7 @@ pub const Value = extern union {
11511176 .fn_ccc_void_no_args_type,
11521177 .single_const_pointer_to_comptime_int_type,
11531178 .const_slice_u8_type,
1179 .enum_literal_type,
11541180 .zero,
11551181 .bool_true,
11561182 .bool_false,
......@@ -1170,6 +1196,7 @@ pub const Value = extern union {
11701196 .void_value,
11711197 .unreachable_value,
11721198 .empty_array,
1199 .enum_literal,
11731200 => unreachable,
11741201
11751202 .ref_val => self.cast(Payload.RefVal).?.val,
......@@ -1227,6 +1254,7 @@ pub const Value = extern union {
12271254 .fn_ccc_void_no_args_type,
12281255 .single_const_pointer_to_comptime_int_type,
12291256 .const_slice_u8_type,
1257 .enum_literal_type,
12301258 .zero,
12311259 .bool_true,
12321260 .bool_false,
......@@ -1246,6 +1274,7 @@ pub const Value = extern union {
12461274 .float_128,
12471275 .void_value,
12481276 .unreachable_value,
1277 .enum_literal,
12491278 => unreachable,
12501279
12511280 .empty_array => unreachable, // out of bounds array index
......@@ -1320,6 +1349,7 @@ pub const Value = extern union {
13201349 .fn_ccc_void_no_args_type,
13211350 .single_const_pointer_to_comptime_int_type,
13221351 .const_slice_u8_type,
1352 .enum_literal_type,
13231353 .zero,
13241354 .empty_array,
13251355 .bool_true,
......@@ -1339,6 +1369,7 @@ pub const Value = extern union {
13391369 .float_64,
13401370 .float_128,
13411371 .void_value,
1372 .enum_literal,
13421373 => false,
13431374
13441375 .undef => unreachable,
src-self-hosted/zir.zig+14
......@@ -231,6 +231,8 @@ pub const Inst = struct {
231231 unwrap_err_unsafe,
232232 /// Takes a *E!T and raises a compiler error if T != void
233233 ensure_err_payload_void,
234 /// Enum literal
235 enum_literal,
234236
235237 pub fn Type(tag: Tag) type {
236238 return switch (tag) {
......@@ -326,6 +328,7 @@ pub const Inst = struct {
326328 .elemptr => ElemPtr,
327329 .condbr => CondBr,
328330 .ptr_type => PtrType,
331 .enum_literal => EnumLiteral,
329332 };
330333 }
331334
......@@ -410,6 +413,7 @@ pub const Inst = struct {
410413 .unwrap_err_unsafe,
411414 .ptr_type,
412415 .ensure_err_payload_void,
416 .enum_literal,
413417 => false,
414418
415419 .@"break",
......@@ -869,6 +873,16 @@ pub const Inst = struct {
869873 },
870874 kw_args: struct {},
871875 };
876
877 pub const EnumLiteral = struct {
878 pub const base_tag = Tag.enum_literal;
879 base: Inst,
880
881 positionals: struct {
882 name: []const u8,
883 },
884 kw_args: struct {},
885 };
872886};
873887
874888pub const ErrorMsg = struct {
src-self-hosted/zir_sema.zig+13
......@@ -115,6 +115,7 @@ pub fn analyzeInst(mod: *Module, scope: *Scope, old_inst: *zir.Inst) InnerError!
115115 .ensure_err_payload_void => return analyzeInstEnsureErrPayloadVoid(mod, scope, old_inst.castTag(.ensure_err_payload_void).?),
116116 .array_type => return analyzeInstArrayType(mod, scope, old_inst.castTag(.array_type).?),
117117 .array_type_sentinel => return analyzeInstArrayTypeSentinel(mod, scope, old_inst.castTag(.array_type_sentinel).?),
118 .enum_literal => return analyzeInstEnumLiteral(mod, scope, old_inst.castTag(.enum_literal).?),
118119 }
119120}
120121
......@@ -701,6 +702,18 @@ fn analyzeInstArrayTypeSentinel(mod: *Module, scope: *Scope, array: *zir.Inst.Ar
701702 return mod.constType(scope, array.base.src, try mod.arrayType(scope, len.val.toUnsignedInt(), sentinel.val, elem_type));
702703}
703704
705fn analyzeInstEnumLiteral(mod: *Module, scope: *Scope, inst: *zir.Inst.EnumLiteral) InnerError!*Inst {
706 const payload = try scope.arena().create(Value.Payload.Bytes);
707 payload.* = .{
708 .base = .{ .tag = .enum_literal },
709 .data = try scope.arena().dupe(u8, inst.positionals.name),
710 };
711 return mod.constInst(scope, inst.base.src, .{
712 .ty = Type.initTag(.enum_literal),
713 .val = Value.initPayload(&payload.base),
714 });
715}
716
704717fn analyzeInstUnwrapOptional(mod: *Module, scope: *Scope, unwrap: *zir.Inst.UnOp, safety_check: bool) InnerError!*Inst {
705718 const operand = try resolveInst(mod, scope, unwrap.positionals.operand);
706719 assert(operand.ty.zigTypeTag() == .Pointer);