authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-02-25 18:21:22-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-02-25 18:21:22-07:00
logfd208d9d5913a0929e444deb97b91092c427bb14
tree2d3fa39c5f3b8eec18ead580a8dc1c7a0c091c51
parent4360f45d7fa242f95aae86a137d56c737798dccf

stage2: implement the error_value AST tag


4 files changed, 41 insertions(+), 4 deletions(-)

src/astgen.zig+7-1
...@@ -482,6 +482,13 @@ pub fn expr(mod: *Module, scope: *Scope, rl: ResultLoc, node: ast.Node.Index) In...@@ -482,6 +482,13 @@ pub fn expr(mod: *Module, scope: *Scope, rl: ResultLoc, node: ast.Node.Index) In
482 const result = try addZIRInst(mod, scope, src, zir.Inst.EnumLiteral, .{ .name = name }, .{});482 const result = try addZIRInst(mod, scope, src, zir.Inst.EnumLiteral, .{ .name = name }, .{});
483 return rvalue(mod, scope, rl, result);483 return rvalue(mod, scope, rl, result);
484 },484 },
485 .error_value => {
486 const ident_token = node_datas[node].rhs;
487 const name = try mod.identifierTokenString(scope, ident_token);
488 const src = token_starts[ident_token];
489 const result = try addZirInstTag(mod, scope, src, .error_value, .{ .name = name });
490 return rvalue(mod, scope, rl, result);
491 },
485 .error_union => {492 .error_union => {
486 const error_set = try typeExpr(mod, scope, node_datas[node].lhs);493 const error_set = try typeExpr(mod, scope, node_datas[node].lhs);
487 const payload = try typeExpr(mod, scope, node_datas[node].rhs);494 const payload = try typeExpr(mod, scope, node_datas[node].rhs);
...@@ -644,7 +651,6 @@ pub fn expr(mod: *Module, scope: *Scope, rl: ResultLoc, node: ast.Node.Index) In...@@ -644,7 +651,6 @@ pub fn expr(mod: *Module, scope: *Scope, rl: ResultLoc, node: ast.Node.Index) In
644 => return mod.failNode(scope, node, "TODO implement astgen.expr for function prototypes", .{}),651 => return mod.failNode(scope, node, "TODO implement astgen.expr for function prototypes", .{}),
645652
646 .@"nosuspend" => return mod.failNode(scope, node, "TODO implement astgen.expr for .nosuspend", .{}),653 .@"nosuspend" => return mod.failNode(scope, node, "TODO implement astgen.expr for .nosuspend", .{}),
647 .error_value => return mod.failNode(scope, node, "TODO implement astgen.expr for .error_value", .{}),
648 }654 }
649}655}
650656
src/value.zig+3-3
...@@ -2138,13 +2138,13 @@ pub const Value = extern union {...@@ -2138,13 +2138,13 @@ pub const Value = extern union {
2138 data: f128,2138 data: f128,
2139 };2139 };
21402140
2141 // TODO move to type.zig2141 /// TODO move to type.zig
2142 pub const ErrorSet = struct {2142 pub const ErrorSet = struct {
2143 pub const base_tag = Tag.error_set;2143 pub const base_tag = Tag.error_set;
21442144
2145 base: Payload = .{ .tag = base_tag },2145 base: Payload = .{ .tag = base_tag },
2146 data: struct {2146 data: struct {
2147 // TODO revisit this when we have the concept of the error tag type2147 /// TODO revisit this when we have the concept of the error tag type
2148 fields: std.StringHashMapUnmanaged(u16),2148 fields: std.StringHashMapUnmanaged(u16),
2149 decl: *Module.Decl,2149 decl: *Module.Decl,
2150 },2150 },
...@@ -2153,9 +2153,9 @@ pub const Value = extern union {...@@ -2153,9 +2153,9 @@ pub const Value = extern union {
2153 pub const Error = struct {2153 pub const Error = struct {
2154 base: Payload = .{ .tag = .@"error" },2154 base: Payload = .{ .tag = .@"error" },
2155 data: struct {2155 data: struct {
2156 // TODO revisit this when we have the concept of the error tag type
2157 /// `name` is owned by `Module` and will be valid for the entire2156 /// `name` is owned by `Module` and will be valid for the entire
2158 /// duration of the compilation.2157 /// duration of the compilation.
2158 /// TODO revisit this when we have the concept of the error tag type
2159 name: []const u8,2159 name: []const u8,
2160 value: u16,2160 value: u16,
2161 },2161 },
src/zir.zig+14
...@@ -154,6 +154,8 @@ pub const Inst = struct {...@@ -154,6 +154,8 @@ pub const Inst = struct {
154 error_union_type,154 error_union_type,
155 /// Create an error set.155 /// Create an error set.
156 error_set,156 error_set,
157 /// `error.Foo` syntax.
158 error_value,
157 /// Export the provided Decl as the provided name in the compilation's output object file.159 /// Export the provided Decl as the provided name in the compilation's output object file.
158 @"export",160 @"export",
159 /// Given a pointer to a struct or object that contains virtual fields, returns a pointer161 /// Given a pointer to a struct or object that contains virtual fields, returns a pointer
...@@ -487,6 +489,7 @@ pub const Inst = struct {...@@ -487,6 +489,7 @@ pub const Inst = struct {
487 .ptr_type => PtrType,489 .ptr_type => PtrType,
488 .enum_literal => EnumLiteral,490 .enum_literal => EnumLiteral,
489 .error_set => ErrorSet,491 .error_set => ErrorSet,
492 .error_value => ErrorValue,
490 .slice => Slice,493 .slice => Slice,
491 .typeof_peer => TypeOfPeer,494 .typeof_peer => TypeOfPeer,
492 .container_field_named => ContainerFieldNamed,495 .container_field_named => ContainerFieldNamed,
...@@ -612,6 +615,7 @@ pub const Inst = struct {...@@ -612,6 +615,7 @@ pub const Inst = struct {
612 .error_union_type,615 .error_union_type,
613 .bit_not,616 .bit_not,
614 .error_set,617 .error_set,
618 .error_value,
615 .slice,619 .slice,
616 .slice_start,620 .slice_start,
617 .import,621 .import,
...@@ -1099,6 +1103,16 @@ pub const Inst = struct {...@@ -1099,6 +1103,16 @@ pub const Inst = struct {
1099 kw_args: struct {},1103 kw_args: struct {},
1100 };1104 };
11011105
1106 pub const ErrorValue = struct {
1107 pub const base_tag = Tag.error_value;
1108 base: Inst,
1109
1110 positionals: struct {
1111 name: []const u8,
1112 },
1113 kw_args: struct {},
1114 };
1115
1102 pub const Slice = struct {1116 pub const Slice = struct {
1103 pub const base_tag = Tag.slice;1117 pub const base_tag = Tag.slice;
1104 base: Inst,1118 base: Inst,
src/zir_sema.zig+17
...@@ -149,6 +149,7 @@ pub fn analyzeInst(mod: *Module, scope: *Scope, old_inst: *zir.Inst) InnerError!...@@ -149,6 +149,7 @@ pub fn analyzeInst(mod: *Module, scope: *Scope, old_inst: *zir.Inst) InnerError!
149 .error_union_type => return zirErrorUnionType(mod, scope, old_inst.castTag(.error_union_type).?),149 .error_union_type => return zirErrorUnionType(mod, scope, old_inst.castTag(.error_union_type).?),
150 .anyframe_type => return zirAnyframeType(mod, scope, old_inst.castTag(.anyframe_type).?),150 .anyframe_type => return zirAnyframeType(mod, scope, old_inst.castTag(.anyframe_type).?),
151 .error_set => return zirErrorSet(mod, scope, old_inst.castTag(.error_set).?),151 .error_set => return zirErrorSet(mod, scope, old_inst.castTag(.error_set).?),
152 .error_value => return zirErrorValue(mod, scope, old_inst.castTag(.error_value).?),
152 .slice => return zirSlice(mod, scope, old_inst.castTag(.slice).?),153 .slice => return zirSlice(mod, scope, old_inst.castTag(.slice).?),
153 .slice_start => return zirSliceStart(mod, scope, old_inst.castTag(.slice_start).?),154 .slice_start => return zirSliceStart(mod, scope, old_inst.castTag(.slice_start).?),
154 .import => return zirImport(mod, scope, old_inst.castTag(.import).?),155 .import => return zirImport(mod, scope, old_inst.castTag(.import).?),
...@@ -1166,6 +1167,22 @@ fn zirErrorSet(mod: *Module, scope: *Scope, inst: *zir.Inst.ErrorSet) InnerError...@@ -1166,6 +1167,22 @@ fn zirErrorSet(mod: *Module, scope: *Scope, inst: *zir.Inst.ErrorSet) InnerError
1166 return mod.analyzeDeclVal(scope, inst.base.src, new_decl);1167 return mod.analyzeDeclVal(scope, inst.base.src, new_decl);
1167}1168}
11681169
1170fn zirErrorValue(mod: *Module, scope: *Scope, inst: *zir.Inst.ErrorValue) InnerError!*Inst {
1171 const tracy = trace(@src());
1172 defer tracy.end();
1173
1174 // Create an anonymous error set type with only this error value, and return the value.
1175 const entry = try mod.getErrorValue(inst.positionals.name);
1176 const result_type = try Type.Tag.error_set_single.create(scope.arena(), entry.key);
1177 return mod.constInst(scope, inst.base.src, .{
1178 .ty = result_type,
1179 .val = try Value.Tag.@"error".create(scope.arena(), .{
1180 .name = entry.key,
1181 .value = entry.value,
1182 }),
1183 });
1184}
1185
1169fn zirMergeErrorSets(mod: *Module, scope: *Scope, inst: *zir.Inst.BinOp) InnerError!*Inst {1186fn zirMergeErrorSets(mod: *Module, scope: *Scope, inst: *zir.Inst.BinOp) InnerError!*Inst {
1170 const tracy = trace(@src());1187 const tracy = trace(@src());
1171 defer tracy.end();1188 defer tracy.end();