authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2020-08-22 16:30:57+03:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-08-24 15:36:42-07:00
log16d7db59ed9b0b1be6790bdb80777fbe5f80c7ed
treededed38af34d9e77fc9a62639a15a03c5f6c78d5
parentd62c12e077e4e3993864317bf8af5f0fcc631515

stage2: anyframe and error union types


6 files changed, 287 insertions(+), 13 deletions(-)

src-self-hosted/Module.zig+22
......@@ -3309,6 +3309,28 @@ pub fn arrayType(self: *Module, scope: *Scope, len: u64, sentinel: ?Value, elem_
33093309 return Type.initPayload(&payload.base);
33103310}
33113311
3312pub fn errorUnionType(self: *Module, scope: *Scope, error_set: Type, payload: Type) Allocator.Error!Type {
3313 assert(error_set.zigTypeTag() == .ErrorSet);
3314 if (error_set.eql(Type.initTag(.anyerror)) and payload.eql(Type.initTag(.void))) {
3315 return Type.initTag(.anyerror_void_error_union);
3316 }
3317
3318 const result = try scope.arena().create(Type.Payload.ErrorUnion);
3319 result.* = .{
3320 .error_set = error_set,
3321 .payload = payload,
3322 };
3323 return Type.initPayload(&result.base);
3324}
3325
3326pub fn anyframeType(self: *Module, scope: *Scope, return_type: Type) Allocator.Error!Type {
3327 const result = try scope.arena().create(Type.Payload.AnyFrame);
3328 result.* = .{
3329 .return_type = return_type,
3330 };
3331 return Type.initPayload(&result.base);
3332}
3333
33123334pub fn dumpInst(self: *Module, scope: *Scope, inst: *Inst) void {
33133335 const zir_module = scope.namespace();
33143336 const source = zir_module.getSource(self) catch @panic("dumpInst failed to get source");
src-self-hosted/astgen.zig+35-6
......@@ -267,11 +267,12 @@ pub fn expr(mod: *Module, scope: *Scope, rl: ResultLoc, node: *ast.Node) InnerEr
267267 .MultilineStringLiteral => return rlWrap(mod, scope, rl, try multilineStrLiteral(mod, scope, node.castTag(.MultilineStringLiteral).?)),
268268 .CharLiteral => return rlWrap(mod, scope, rl, try charLiteral(mod, scope, node.castTag(.CharLiteral).?)),
269269 .SliceType => return rlWrap(mod, scope, rl, try sliceType(mod, scope, node.castTag(.SliceType).?)),
270 .ErrorUnion => return rlWrap(mod, scope, rl, try typeInixOp(mod, scope, node.castTag(.ErrorUnion).?, .error_union_type)),
271 .MergeErrorSets => return rlWrap(mod, scope, rl, try typeInixOp(mod, scope, node.castTag(.MergeErrorSets).?, .merge_error_sets)),
272 .AnyFrameType => return rlWrap(mod, scope, rl, try anyFrameType(mod, scope, node.castTag(.AnyFrameType).?)),
270273
271274 .Defer => return mod.failNode(scope, node, "TODO implement astgen.expr for .Defer", .{}),
272275 .Catch => return mod.failNode(scope, node, "TODO implement astgen.expr for .Catch", .{}),
273 .ErrorUnion => return mod.failNode(scope, node, "TODO implement astgen.expr for .ErrorUnion", .{}),
274 .MergeErrorSets => return mod.failNode(scope, node, "TODO implement astgen.expr for .MergeErrorSets", .{}),
275276 .Range => return mod.failNode(scope, node, "TODO implement astgen.expr for .Range", .{}),
276277 .OrElse => return mod.failNode(scope, node, "TODO implement astgen.expr for .OrElse", .{}),
277278 .Await => return mod.failNode(scope, node, "TODO implement astgen.expr for .Await", .{}),
......@@ -290,7 +291,6 @@ pub fn expr(mod: *Module, scope: *Scope, rl: ResultLoc, node: *ast.Node) InnerEr
290291 .AnyType => return mod.failNode(scope, node, "TODO implement astgen.expr for .AnyType", .{}),
291292 .ErrorType => return mod.failNode(scope, node, "TODO implement astgen.expr for .ErrorType", .{}),
292293 .FnProto => return mod.failNode(scope, node, "TODO implement astgen.expr for .FnProto", .{}),
293 .AnyFrameType => return mod.failNode(scope, node, "TODO implement astgen.expr for .AnyFrameType", .{}),
294294 .ErrorSetDecl => return mod.failNode(scope, node, "TODO implement astgen.expr for .ErrorSetDecl", .{}),
295295 .ContainerDecl => return mod.failNode(scope, node, "TODO implement astgen.expr for .ContainerDecl", .{}),
296296 .Comptime => return mod.failNode(scope, node, "TODO implement astgen.expr for .Comptime", .{}),
......@@ -566,14 +566,13 @@ fn negation(mod: *Module, scope: *Scope, node: *ast.Node.SimplePrefixOp, op_inst
566566 const tree = scope.tree();
567567 const src = tree.token_locs[node.op_token].start;
568568
569 const lhs = addZIRInstConst(mod, scope, src, .{
569 const lhs = try addZIRInstConst(mod, scope, src, .{
570570 .ty = Type.initTag(.comptime_int),
571571 .val = Value.initTag(.zero),
572572 });
573573 const rhs = try expr(mod, scope, .none, node.rhs);
574574
575 const result = try addZIRBinOp(mod, scope, src, op_inst_tag, lhs, rhs);
576 return rlWrap(mod, scope, rl, result);
575 return addZIRBinOp(mod, scope, src, op_inst_tag, lhs, rhs);
577576}
578577
579578fn addressOf(mod: *Module, scope: *Scope, node: *ast.Node.SimplePrefixOp) InnerError!*zir.Inst {
......@@ -703,6 +702,36 @@ fn arrayTypeSentinel(mod: *Module, scope: *Scope, node: *ast.Node.ArrayTypeSenti
703702 }, .{});
704703}
705704
705fn anyFrameType(mod: *Module, scope: *Scope, node: *ast.Node.AnyFrameType) InnerError!*zir.Inst {
706 const tree = scope.tree();
707 const src = tree.token_locs[node.anyframe_token].start;
708 if (node.result) |some| {
709 const meta_type = try addZIRInstConst(mod, scope, src, .{
710 .ty = Type.initTag(.type),
711 .val = Value.initTag(.type_type),
712 });
713 const return_type = try expr(mod, scope, .{ .ty = meta_type}, some.return_type);
714 return addZIRUnOp(mod, scope, src, .anyframe_type, return_type);
715 } else {
716 return addZIRInstConst(mod, scope, src, .{
717 .ty = Type.initTag(.type),
718 .val = Value.initTag(.anyframe_type),
719 });
720 }
721}
722
723fn typeInixOp(mod: *Module, scope: *Scope, node: *ast.Node.SimpleInfixOp, op_inst_tag: zir.Inst.Tag) InnerError!*zir.Inst {
724 const tree = scope.tree();
725 const src = tree.token_locs[node.op_token].start;
726 const meta_type = try addZIRInstConst(mod, scope, src, .{
727 .ty = Type.initTag(.type),
728 .val = Value.initTag(.type_type),
729 });
730 const error_set = try expr(mod, scope, .{ .ty = meta_type }, node.lhs);
731 const payload = try expr(mod, scope, .{ .ty = meta_type }, node.rhs);
732 return addZIRBinOp(mod, scope, src, op_inst_tag, error_set, payload);
733}
734
706735fn enumLiteral(mod: *Module, scope: *Scope, node: *ast.Node.EnumLiteral) !*zir.Inst {
707736 const tree = scope.tree();
708737 const src = tree.token_locs[node.name].start;
src-self-hosted/type.zig+173-6
......@@ -84,6 +84,10 @@ pub const Type = extern union {
8484 .optional_single_mut_pointer,
8585 => return .Optional,
8686 .enum_literal => return .EnumLiteral,
87
88 .anyerror_void_error_union, .error_union => return .ErrorUnion,
89
90 .anyframe_T, .@"anyframe" => return .AnyFrame,
8791 }
8892 }
8993
......@@ -151,6 +155,9 @@ pub const Type = extern union {
151155 .ComptimeInt => return true,
152156 .Undefined => return true,
153157 .Null => return true,
158 .AnyFrame => {
159 return a.elemType().eql(b.elemType());
160 },
154161 .Pointer => {
155162 // Hot path for common case:
156163 if (a.castPointer()) |a_payload| {
......@@ -225,7 +232,6 @@ pub const Type = extern union {
225232 .BoundFn,
226233 .Opaque,
227234 .Frame,
228 .AnyFrame,
229235 .Vector,
230236 => std.debug.panic("TODO implement Type equality comparison of {} and {}", .{ a, b }),
231237 }
......@@ -343,6 +349,8 @@ pub const Type = extern union {
343349 .single_const_pointer_to_comptime_int,
344350 .const_slice_u8,
345351 .enum_literal,
352 .anyerror_void_error_union,
353 .@"anyframe",
346354 => unreachable,
347355
348356 .array_u8_sentinel_0 => return self.copyPayloadShallow(allocator, Payload.Array_u8_Sentinel0),
......@@ -397,6 +405,7 @@ pub const Type = extern union {
397405 .optional_single_mut_pointer,
398406 .optional_single_const_pointer,
399407 => return self.copyPayloadSingleField(allocator, Payload.PointerSimple, "pointee_type"),
408 .anyframe_T => return self.copyPayloadSingleField(allocator, Payload.AnyFrame, "return_type"),
400409
401410 .pointer => {
402411 const payload = @fieldParentPtr(Payload.Pointer, "base", self.ptr_otherwise);
......@@ -416,6 +425,17 @@ pub const Type = extern union {
416425 };
417426 return Type{ .ptr_otherwise = &new_payload.base };
418427 },
428 .error_union => {
429 const payload = @fieldParentPtr(Payload.ErrorUnion, "base", self.ptr_otherwise);
430 const new_payload = try allocator.create(Payload.ErrorUnion);
431 new_payload.* = .{
432 .base = payload.base,
433
434 .error_set = try payload.error_set.copy(allocator),
435 .payload = try payload.payload.copy(allocator),
436 };
437 return Type{ .ptr_otherwise = &new_payload.base };
438 },
419439 }
420440 }
421441
......@@ -482,6 +502,8 @@ pub const Type = extern union {
482502 .@"null" => return out_stream.writeAll("@TypeOf(null)"),
483503 .@"undefined" => return out_stream.writeAll("@TypeOf(undefined)"),
484504
505 .@"anyframe" => return out_stream.writeAll("anyframe"),
506 .anyerror_void_error_union => return out_stream.writeAll("anyerror!void"),
485507 .const_slice_u8 => return out_stream.writeAll("[]const u8"),
486508 .fn_noreturn_no_args => return out_stream.writeAll("fn() noreturn"),
487509 .fn_void_no_args => return out_stream.writeAll("fn() void"),
......@@ -500,6 +522,12 @@ pub const Type = extern union {
500522 continue;
501523 },
502524
525 .anyframe_T => {
526 const payload = @fieldParentPtr(Payload.AnyFrame, "base", ty.ptr_otherwise);
527 try out_stream.print("anyframe->", .{});
528 ty = payload.return_type;
529 continue;
530 },
503531 .array_u8 => {
504532 const payload = @fieldParentPtr(Payload.Array_u8, "base", ty.ptr_otherwise);
505533 return out_stream.print("[{}]u8", .{payload.len});
......@@ -622,6 +650,13 @@ pub const Type = extern union {
622650 ty = payload.pointee_type;
623651 continue;
624652 },
653 .error_union => {
654 const payload = @fieldParentPtr(Payload.ErrorUnion, "base", ty.ptr_otherwise);
655 try payload.error_set.format("", .{}, out_stream);
656 try out_stream.writeAll("!");
657 ty = payload.payload;
658 continue;
659 },
625660 }
626661 unreachable;
627662 }
......@@ -715,6 +750,9 @@ pub const Type = extern union {
715750 .optional,
716751 .optional_single_mut_pointer,
717752 .optional_single_const_pointer,
753 .@"anyframe",
754 .anyframe_T,
755 .anyerror_void_error_union,
718756 => true,
719757 // TODO lazy types
720758 .array => self.elemType().hasCodeGenBits() and self.arrayLen() != 0,
......@@ -723,6 +761,11 @@ pub const Type = extern union {
723761 .int_signed => self.cast(Payload.IntSigned).?.bits == 0,
724762 .int_unsigned => self.cast(Payload.IntUnsigned).?.bits == 0,
725763
764 .error_union => {
765 const payload = self.cast(Payload.ErrorUnion).?;
766 return payload.error_set.hasCodeGenBits() or payload.payload.hasCodeGenBits();
767 },
768
726769 .c_void,
727770 .void,
728771 .type,
......@@ -779,6 +822,8 @@ pub const Type = extern union {
779822 .mut_slice,
780823 .optional_single_const_pointer,
781824 .optional_single_mut_pointer,
825 .@"anyframe",
826 .anyframe_T,
782827 => return @divExact(target.cpu.arch.ptrBitWidth(), 8),
783828
784829 .pointer => {
......@@ -803,7 +848,7 @@ pub const Type = extern union {
803848 .f128 => return 16,
804849 .c_longdouble => return 16,
805850
806 .anyerror => return 2, // TODO revisit this when we have the concept of the error tag type
851 .anyerror_void_error_union, .anyerror => return 2, // TODO revisit this when we have the concept of the error tag type
807852
808853 .array, .array_sentinel => return self.elemType().abiAlignment(target),
809854
......@@ -829,6 +874,16 @@ pub const Type = extern union {
829874 return child_type.abiAlignment(target);
830875 },
831876
877 .error_union => {
878 const payload = self.cast(Payload.ErrorUnion).?;
879 if (!payload.error_set.hasCodeGenBits()) {
880 return payload.payload.abiAlignment(target);
881 } else if (!payload.payload.hasCodeGenBits()) {
882 return payload.error_set.abiAlignment(target);
883 }
884 @panic("TODO abiAlignment error union");
885 },
886
832887 .c_void,
833888 .void,
834889 .type,
......@@ -882,12 +937,15 @@ pub const Type = extern union {
882937 .i32, .u32 => return 4,
883938 .i64, .u64 => return 8,
884939
885 .isize, .usize => return @divExact(target.cpu.arch.ptrBitWidth(), 8),
940 .@"anyframe", .anyframe_T, .isize, .usize => return @divExact(target.cpu.arch.ptrBitWidth(), 8),
886941
887942 .const_slice,
888943 .mut_slice,
889 .const_slice_u8,
890 => return @divExact(target.cpu.arch.ptrBitWidth(), 8) * 2,
944 => {
945 if (self.elemType().hasCodeGenBits()) return @divExact(target.cpu.arch.ptrBitWidth(), 8) * 2;
946 return @divExact(target.cpu.arch.ptrBitWidth(), 8);
947 },
948 .const_slice_u8 => return @divExact(target.cpu.arch.ptrBitWidth(), 8) * 2,
891949
892950 .optional_single_const_pointer,
893951 .optional_single_mut_pointer,
......@@ -923,7 +981,7 @@ pub const Type = extern union {
923981 .f128 => return 16,
924982 .c_longdouble => return 16,
925983
926 .anyerror => return 2, // TODO revisit this when we have the concept of the error tag type
984 .anyerror_void_error_union, .anyerror => return 2, // TODO revisit this when we have the concept of the error tag type
927985
928986 .int_signed, .int_unsigned => {
929987 const bits: u16 = if (self.cast(Payload.IntSigned)) |pl|
......@@ -950,6 +1008,18 @@ pub const Type = extern union {
9501008 // to the child type's ABI alignment.
9511009 return child_type.abiAlignment(target) + child_type.abiSize(target);
9521010 },
1011
1012 .error_union => {
1013 const payload = self.cast(Payload.ErrorUnion).?;
1014 if (!payload.error_set.hasCodeGenBits() and !payload.payload.hasCodeGenBits()) {
1015 return 0;
1016 } else if (!payload.error_set.hasCodeGenBits()) {
1017 return payload.payload.abiSize(target);
1018 } else if (!payload.payload.hasCodeGenBits()) {
1019 return payload.error_set.abiSize(target);
1020 }
1021 @panic("TODO abiSize error union");
1022 },
9531023 };
9541024 }
9551025
......@@ -1010,6 +1080,10 @@ pub const Type = extern union {
10101080 .c_mut_pointer,
10111081 .const_slice,
10121082 .mut_slice,
1083 .error_union,
1084 .@"anyframe",
1085 .anyframe_T,
1086 .anyerror_void_error_union,
10131087 => false,
10141088
10151089 .single_const_pointer,
......@@ -1078,6 +1152,10 @@ pub const Type = extern union {
10781152 .optional_single_mut_pointer,
10791153 .optional_single_const_pointer,
10801154 .enum_literal,
1155 .error_union,
1156 .@"anyframe",
1157 .anyframe_T,
1158 .anyerror_void_error_union,
10811159 => false,
10821160
10831161 .const_slice,
......@@ -1143,6 +1221,10 @@ pub const Type = extern union {
11431221 .optional_single_const_pointer,
11441222 .enum_literal,
11451223 .mut_slice,
1224 .error_union,
1225 .@"anyframe",
1226 .anyframe_T,
1227 .anyerror_void_error_union,
11461228 => false,
11471229
11481230 .single_const_pointer,
......@@ -1217,6 +1299,10 @@ pub const Type = extern union {
12171299 .optional_single_mut_pointer,
12181300 .optional_single_const_pointer,
12191301 .enum_literal,
1302 .error_union,
1303 .@"anyframe",
1304 .anyframe_T,
1305 .anyerror_void_error_union,
12201306 => false,
12211307
12221308 .pointer => {
......@@ -1328,6 +1414,10 @@ pub const Type = extern union {
13281414 .optional_single_const_pointer,
13291415 .optional_single_mut_pointer,
13301416 .enum_literal,
1417 .error_union,
1418 .@"anyframe",
1419 .anyframe_T,
1420 .anyerror_void_error_union,
13311421 => unreachable,
13321422
13331423 .array => self.cast(Payload.Array).?.elem_type,
......@@ -1449,6 +1539,10 @@ pub const Type = extern union {
14491539 .optional_single_mut_pointer,
14501540 .optional_single_const_pointer,
14511541 .enum_literal,
1542 .error_union,
1543 .@"anyframe",
1544 .anyframe_T,
1545 .anyerror_void_error_union,
14521546 => unreachable,
14531547
14541548 .array => self.cast(Payload.Array).?.len,
......@@ -1516,6 +1610,10 @@ pub const Type = extern union {
15161610 .optional_single_mut_pointer,
15171611 .optional_single_const_pointer,
15181612 .enum_literal,
1613 .error_union,
1614 .@"anyframe",
1615 .anyframe_T,
1616 .anyerror_void_error_union,
15191617 => unreachable,
15201618
15211619 .array, .array_u8 => return null,
......@@ -1581,6 +1679,10 @@ pub const Type = extern union {
15811679 .optional_single_mut_pointer,
15821680 .optional_single_const_pointer,
15831681 .enum_literal,
1682 .error_union,
1683 .@"anyframe",
1684 .anyframe_T,
1685 .anyerror_void_error_union,
15841686 => false,
15851687
15861688 .int_signed,
......@@ -1649,6 +1751,10 @@ pub const Type = extern union {
16491751 .optional_single_mut_pointer,
16501752 .optional_single_const_pointer,
16511753 .enum_literal,
1754 .error_union,
1755 .@"anyframe",
1756 .anyframe_T,
1757 .anyerror_void_error_union,
16521758 => false,
16531759
16541760 .int_unsigned,
......@@ -1707,6 +1813,10 @@ pub const Type = extern union {
17071813 .optional_single_mut_pointer,
17081814 .optional_single_const_pointer,
17091815 .enum_literal,
1816 .error_union,
1817 .@"anyframe",
1818 .anyframe_T,
1819 .anyerror_void_error_union,
17101820 => unreachable,
17111821
17121822 .int_unsigned => .{ .signed = false, .bits = self.cast(Payload.IntUnsigned).?.bits },
......@@ -1783,6 +1893,10 @@ pub const Type = extern union {
17831893 .optional_single_mut_pointer,
17841894 .optional_single_const_pointer,
17851895 .enum_literal,
1896 .error_union,
1897 .@"anyframe",
1898 .anyframe_T,
1899 .anyerror_void_error_union,
17861900 => false,
17871901
17881902 .usize,
......@@ -1888,6 +2002,10 @@ pub const Type = extern union {
18882002 .optional_single_mut_pointer,
18892003 .optional_single_const_pointer,
18902004 .enum_literal,
2005 .error_union,
2006 .@"anyframe",
2007 .anyframe_T,
2008 .anyerror_void_error_union,
18912009 => unreachable,
18922010 };
18932011 }
......@@ -1959,6 +2077,10 @@ pub const Type = extern union {
19592077 .optional_single_mut_pointer,
19602078 .optional_single_const_pointer,
19612079 .enum_literal,
2080 .error_union,
2081 .@"anyframe",
2082 .anyframe_T,
2083 .anyerror_void_error_union,
19622084 => unreachable,
19632085 }
19642086 }
......@@ -2029,6 +2151,10 @@ pub const Type = extern union {
20292151 .optional_single_mut_pointer,
20302152 .optional_single_const_pointer,
20312153 .enum_literal,
2154 .error_union,
2155 .@"anyframe",
2156 .anyframe_T,
2157 .anyerror_void_error_union,
20322158 => unreachable,
20332159 }
20342160 }
......@@ -2099,6 +2225,10 @@ pub const Type = extern union {
20992225 .optional_single_mut_pointer,
21002226 .optional_single_const_pointer,
21012227 .enum_literal,
2228 .error_union,
2229 .@"anyframe",
2230 .anyframe_T,
2231 .anyerror_void_error_union,
21022232 => unreachable,
21032233 };
21042234 }
......@@ -2166,6 +2296,10 @@ pub const Type = extern union {
21662296 .optional_single_mut_pointer,
21672297 .optional_single_const_pointer,
21682298 .enum_literal,
2299 .error_union,
2300 .@"anyframe",
2301 .anyframe_T,
2302 .anyerror_void_error_union,
21692303 => unreachable,
21702304 };
21712305 }
......@@ -2233,6 +2367,10 @@ pub const Type = extern union {
22332367 .optional_single_mut_pointer,
22342368 .optional_single_const_pointer,
22352369 .enum_literal,
2370 .error_union,
2371 .@"anyframe",
2372 .anyframe_T,
2373 .anyerror_void_error_union,
22362374 => unreachable,
22372375 };
22382376 }
......@@ -2300,6 +2438,10 @@ pub const Type = extern union {
23002438 .optional_single_mut_pointer,
23012439 .optional_single_const_pointer,
23022440 .enum_literal,
2441 .error_union,
2442 .@"anyframe",
2443 .anyframe_T,
2444 .anyerror_void_error_union,
23032445 => false,
23042446 };
23052447 }
......@@ -2351,6 +2493,10 @@ pub const Type = extern union {
23512493 .optional_single_mut_pointer,
23522494 .optional_single_const_pointer,
23532495 .enum_literal,
2496 .anyerror_void_error_union,
2497 .anyframe_T,
2498 .@"anyframe",
2499 .error_union,
23542500 => return null,
23552501
23562502 .void => return Value.initTag(.void_value),
......@@ -2454,6 +2600,10 @@ pub const Type = extern union {
24542600 .optional_single_mut_pointer,
24552601 .optional_single_const_pointer,
24562602 .enum_literal,
2603 .error_union,
2604 .@"anyframe",
2605 .anyframe_T,
2606 .anyerror_void_error_union,
24572607 => return false,
24582608
24592609 .c_const_pointer,
......@@ -2511,6 +2661,8 @@ pub const Type = extern union {
25112661 fn_naked_noreturn_no_args,
25122662 fn_ccc_void_no_args,
25132663 single_const_pointer_to_comptime_int,
2664 anyerror_void_error_union,
2665 @"anyframe",
25142666 const_slice_u8, // See last_no_payload_tag below.
25152667 // After this, the tag requires a payload.
25162668
......@@ -2533,6 +2685,8 @@ pub const Type = extern union {
25332685 optional,
25342686 optional_single_mut_pointer,
25352687 optional_single_const_pointer,
2688 error_union,
2689 anyframe_T,
25362690
25372691 pub const last_no_payload_tag = Tag.const_slice_u8;
25382692 pub const no_payload_count = @enumToInt(last_no_payload_tag) + 1;
......@@ -2614,6 +2768,19 @@ pub const Type = extern union {
26142768 @"volatile": bool,
26152769 size: std.builtin.TypeInfo.Pointer.Size,
26162770 };
2771
2772 pub const ErrorUnion = struct {
2773 base: Payload = .{ .tag = .error_union },
2774
2775 error_set: Type,
2776 payload: Type,
2777 };
2778
2779 pub const AnyFrame = struct {
2780 base: Payload = .{ .tag = .anyframe_T },
2781
2782 return_type: Type,
2783 };
26172784 };
26182785};
26192786
src-self-hosted/value.zig+14
......@@ -61,6 +61,7 @@ pub const Value = extern union {
6161 single_const_pointer_to_comptime_int_type,
6262 const_slice_u8_type,
6363 enum_literal_type,
64 anyframe_type,
6465
6566 undef,
6667 zero,
......@@ -168,6 +169,7 @@ pub const Value = extern union {
168169 .single_const_pointer_to_comptime_int_type,
169170 .const_slice_u8_type,
170171 .enum_literal_type,
172 .anyframe_type,
171173 .undef,
172174 .zero,
173175 .void_value,
......@@ -300,6 +302,7 @@ pub const Value = extern union {
300302 .single_const_pointer_to_comptime_int_type => return out_stream.writeAll("*const comptime_int"),
301303 .const_slice_u8_type => return out_stream.writeAll("[]const u8"),
302304 .enum_literal_type => return out_stream.writeAll("@TypeOf(.EnumLiteral)"),
305 .anyframe_type => return out_stream.writeAll("anyframe"),
303306
304307 .null_value => return out_stream.writeAll("null"),
305308 .undef => return out_stream.writeAll("undefined"),
......@@ -408,6 +411,7 @@ pub const Value = extern union {
408411 .single_const_pointer_to_comptime_int_type => Type.initTag(.single_const_pointer_to_comptime_int),
409412 .const_slice_u8_type => Type.initTag(.const_slice_u8),
410413 .enum_literal_type => Type.initTag(.enum_literal),
414 .anyframe_type => Type.initTag(.@"anyframe"),
411415
412416 .undef,
413417 .zero,
......@@ -482,6 +486,7 @@ pub const Value = extern union {
482486 .single_const_pointer_to_comptime_int_type,
483487 .const_slice_u8_type,
484488 .enum_literal_type,
489 .anyframe_type,
485490 .null_value,
486491 .function,
487492 .variable,
......@@ -560,6 +565,7 @@ pub const Value = extern union {
560565 .single_const_pointer_to_comptime_int_type,
561566 .const_slice_u8_type,
562567 .enum_literal_type,
568 .anyframe_type,
563569 .null_value,
564570 .function,
565571 .variable,
......@@ -638,6 +644,7 @@ pub const Value = extern union {
638644 .single_const_pointer_to_comptime_int_type,
639645 .const_slice_u8_type,
640646 .enum_literal_type,
647 .anyframe_type,
641648 .null_value,
642649 .function,
643650 .variable,
......@@ -742,6 +749,7 @@ pub const Value = extern union {
742749 .single_const_pointer_to_comptime_int_type,
743750 .const_slice_u8_type,
744751 .enum_literal_type,
752 .anyframe_type,
745753 .null_value,
746754 .function,
747755 .variable,
......@@ -825,6 +833,7 @@ pub const Value = extern union {
825833 .single_const_pointer_to_comptime_int_type,
826834 .const_slice_u8_type,
827835 .enum_literal_type,
836 .anyframe_type,
828837 .null_value,
829838 .function,
830839 .variable,
......@@ -988,6 +997,7 @@ pub const Value = extern union {
988997 .single_const_pointer_to_comptime_int_type,
989998 .const_slice_u8_type,
990999 .enum_literal_type,
1000 .anyframe_type,
9911001 .bool_true,
9921002 .bool_false,
9931003 .null_value,
......@@ -1063,6 +1073,7 @@ pub const Value = extern union {
10631073 .single_const_pointer_to_comptime_int_type,
10641074 .const_slice_u8_type,
10651075 .enum_literal_type,
1076 .anyframe_type,
10661077 .null_value,
10671078 .function,
10681079 .variable,
......@@ -1197,6 +1208,7 @@ pub const Value = extern union {
11971208 .single_const_pointer_to_comptime_int_type,
11981209 .const_slice_u8_type,
11991210 .enum_literal_type,
1211 .anyframe_type,
12001212 .zero,
12011213 .bool_true,
12021214 .bool_false,
......@@ -1276,6 +1288,7 @@ pub const Value = extern union {
12761288 .single_const_pointer_to_comptime_int_type,
12771289 .const_slice_u8_type,
12781290 .enum_literal_type,
1291 .anyframe_type,
12791292 .zero,
12801293 .bool_true,
12811294 .bool_false,
......@@ -1372,6 +1385,7 @@ pub const Value = extern union {
13721385 .single_const_pointer_to_comptime_int_type,
13731386 .const_slice_u8_type,
13741387 .enum_literal_type,
1388 .anyframe_type,
13751389 .zero,
13761390 .empty_array,
13771391 .bool_true,
src-self-hosted/zir.zig+14
......@@ -43,6 +43,8 @@ pub const Inst = struct {
4343 alloc,
4444 /// Same as `alloc` except the type is inferred.
4545 alloc_inferred,
46 /// Create an `anyframe->T`.
47 anyframe_type,
4648 /// Array concatenation. `a ++ b`
4749 array_cat,
4850 /// Array multiplication `a ** b`
......@@ -135,6 +137,8 @@ pub const Inst = struct {
135137 ensure_result_used,
136138 /// Emits a compile error if an error is ignored.
137139 ensure_result_non_error,
140 /// Create a `E!T` type.
141 error_union_type,
138142 /// Export the provided Decl as the provided name in the compilation's output object file.
139143 @"export",
140144 /// Given a pointer to a struct or object that contains virtual fields, returns a pointer
......@@ -162,6 +166,8 @@ pub const Inst = struct {
162166 /// A labeled block of code that loops forever. At the end of the body it is implied
163167 /// to repeat; no explicit "repeat" instruction terminates loop bodies.
164168 loop,
169 /// Merge two error sets into one, `E1 || E2`.
170 merge_error_sets,
165171 /// Ambiguously remainder division or modulus. If the computation would possibly have
166172 /// a different value depending on whether the operation is remainder division or modulus,
167173 /// a compile error is emitted. Otherwise the computation is performed.
......@@ -288,6 +294,8 @@ pub const Inst = struct {
288294 .unwrap_err_safe,
289295 .unwrap_err_unsafe,
290296 .ensure_err_payload_void,
297 .anyframe_type,
298 .bitnot,
291299 => UnOp,
292300
293301 .add,
......@@ -318,6 +326,8 @@ pub const Inst = struct {
318326 .bitcast,
319327 .coerce_result_ptr,
320328 .xor,
329 .error_union_type,
330 .merge_error_sets,
321331 => BinOp,
322332
323333 .arg => Arg,
......@@ -440,6 +450,10 @@ pub const Inst = struct {
440450 .ptr_type,
441451 .ensure_err_payload_void,
442452 .enum_literal,
453 .merge_error_sets,
454 .anyframe_type,
455 .error_union_type,
456 .bitnot,
443457 => false,
444458
445459 .@"break",
src-self-hosted/zir_sema.zig+29-1
......@@ -97,7 +97,7 @@ pub fn analyzeInst(mod: *Module, scope: *Scope, old_inst: *zir.Inst) InnerError!
9797 .array_cat => return analyzeInstArrayCat(mod, scope, old_inst.castTag(.array_cat).?),
9898 .array_mul => return analyzeInstArrayMul(mod, scope, old_inst.castTag(.array_mul).?),
9999 .bitand => return analyzeInstBitwise(mod, scope, old_inst.castTag(.bitand).?),
100 .bitnot => return analyzeInstBitwise(mod, scope, old_inst.castTag(.bitnot).?),
100 .bitnot => return analyzeInstBitNot(mod, scope, old_inst.castTag(.bitnot).?),
101101 .bitor => return analyzeInstBitwise(mod, scope, old_inst.castTag(.bitor).?),
102102 .xor => return analyzeInstBitwise(mod, scope, old_inst.castTag(.xor).?),
103103 .shl => return analyzeInstShl(mod, scope, old_inst.castTag(.shl).?),
......@@ -123,6 +123,9 @@ pub fn analyzeInst(mod: *Module, scope: *Scope, old_inst: *zir.Inst) InnerError!
123123 .array_type => return analyzeInstArrayType(mod, scope, old_inst.castTag(.array_type).?),
124124 .array_type_sentinel => return analyzeInstArrayTypeSentinel(mod, scope, old_inst.castTag(.array_type_sentinel).?),
125125 .enum_literal => return analyzeInstEnumLiteral(mod, scope, old_inst.castTag(.enum_literal).?),
126 .merge_error_sets => return analyzeInstMergeErrorSets(mod, scope, old_inst.castTag(.merge_error_sets).?),
127 .error_union_type => return analyzeInstErrorUnionType(mod, scope, old_inst.castTag(.error_union_type).?),
128 .anyframe_type => return analyzeInstAnyframeType(mod, scope, old_inst.castTag(.anyframe_type).?),
126129 }
127130}
128131
......@@ -717,6 +720,27 @@ fn analyzeInstArrayTypeSentinel(mod: *Module, scope: *Scope, array: *zir.Inst.Ar
717720 return mod.constType(scope, array.base.src, try mod.arrayType(scope, len.val.toUnsignedInt(), sentinel.val, elem_type));
718721}
719722
723fn analyzeInstErrorUnionType(mod: *Module, scope: *Scope, inst: *zir.Inst.BinOp) InnerError!*Inst {
724 const error_union = try resolveType(mod, scope, inst.positionals.lhs);
725 const payload = try resolveType(mod, scope, inst.positionals.rhs);
726
727 if (error_union.zigTypeTag() != .ErrorSet) {
728 return mod.fail(scope, inst.base.src, "expected error set type, found {}", .{error_union.elemType()});
729 }
730
731 return mod.constType(scope, inst.base.src, try mod.errorUnionType(scope, error_union, payload));
732}
733
734fn analyzeInstAnyframeType(mod: *Module, scope: *Scope, inst: *zir.Inst.UnOp) InnerError!*Inst {
735 const return_type = try resolveType(mod, scope, inst.positionals.operand);
736
737 return mod.constType(scope, inst.base.src, try mod.anyframeType(scope, return_type));
738}
739
740fn analyzeInstMergeErrorSets(mod: *Module, scope: *Scope, inst: *zir.Inst.BinOp) InnerError!*Inst {
741 return mod.fail(scope, inst.base.src, "TODO implement merge_error_sets", .{});
742}
743
720744fn analyzeInstEnumLiteral(mod: *Module, scope: *Scope, inst: *zir.Inst.EnumLiteral) InnerError!*Inst {
721745 const payload = try scope.arena().create(Value.Payload.Bytes);
722746 payload.* = .{
......@@ -984,6 +1008,10 @@ fn analyzeInstBitwise(mod: *Module, scope: *Scope, inst: *zir.Inst.BinOp) InnerE
9841008 return mod.fail(scope, inst.base.src, "TODO implement analyzeInstBitwise", .{});
9851009}
9861010
1011fn analyzeInstBitNot(mod: *Module, scope: *Scope, inst: *zir.Inst.UnOp) InnerError!*Inst {
1012 return mod.fail(scope, inst.base.src, "TODO implement analyzeInstBitNot", .{});
1013}
1014
9871015fn analyzeInstArrayCat(mod: *Module, scope: *Scope, inst: *zir.Inst.BinOp) InnerError!*Inst {
9881016 return mod.fail(scope, inst.base.src, "TODO implement analyzeInstArrayCat", .{});
9891017}