Skip to content

Commit d769fd0

Browse files
Vexuandrewrk
authored andcommitted
stage2: pass anon name strategy to reify
1 parent 3afc4df commit d769fd0

File tree

6 files changed

+78
-20
lines changed

6 files changed

+78
-20
lines changed

src/AstGen.zig

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2454,7 +2454,6 @@ fn addEnsureResult(gz: *GenZir, maybe_unused_result: Zir.Inst.Ref, statement: As
24542454
.trunc,
24552455
.round,
24562456
.tag_name,
2457-
.reify,
24582457
.type_name,
24592458
.frame_type,
24602459
.frame_size,
@@ -7553,7 +7552,6 @@ fn builtinCall(
75537552
.trunc => return simpleUnOp(gz, scope, rl, node, .none, params[0], .trunc),
75547553
.round => return simpleUnOp(gz, scope, rl, node, .none, params[0], .round),
75557554
.tag_name => return simpleUnOp(gz, scope, rl, node, .none, params[0], .tag_name),
7556-
.Type => return simpleUnOp(gz, scope, rl, node, .{ .coerced_ty = .type_info_type }, params[0], .reify),
75577555
.type_name => return simpleUnOp(gz, scope, rl, node, .none, params[0], .type_name),
75587556
.Frame => return simpleUnOp(gz, scope, rl, node, .none, params[0], .frame_type),
75597557
.frame_size => return simpleUnOp(gz, scope, rl, node, .none, params[0], .frame_size),
@@ -7568,6 +7566,30 @@ fn builtinCall(
75687566
.truncate => return typeCast(gz, scope, rl, node, params[0], params[1], .truncate),
75697567
// zig fmt: on
75707568

7569+
.Type => {
7570+
const operand = try expr(gz, scope, .{ .coerced_ty = .type_info_type }, params[0]);
7571+
7572+
const gpa = gz.astgen.gpa;
7573+
7574+
try gz.instructions.ensureUnusedCapacity(gpa, 1);
7575+
try gz.astgen.instructions.ensureUnusedCapacity(gpa, 1);
7576+
7577+
const payload_index = try gz.astgen.addExtra(Zir.Inst.UnNode{
7578+
.node = gz.nodeIndexToRelative(node),
7579+
.operand = operand,
7580+
});
7581+
const new_index = @intCast(Zir.Inst.Index, gz.astgen.instructions.len);
7582+
gz.astgen.instructions.appendAssumeCapacity(.{
7583+
.tag = .extended,
7584+
.data = .{ .extended = .{
7585+
.opcode = .reify,
7586+
.small = @enumToInt(gz.anon_name_strategy),
7587+
.operand = payload_index,
7588+
} },
7589+
});
7590+
gz.instructions.appendAssumeCapacity(new_index);
7591+
return indexToRef(new_index);
7592+
},
75717593
.panic => {
75727594
try emitDbgNode(gz, node);
75737595
return simpleUnOp(gz, scope, rl, node, .{ .ty = .const_slice_u8_type }, params[0], if (gz.force_comptime) .panic_comptime else .panic);

src/Autodoc.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1220,7 +1220,6 @@ fn walkInstruction(
12201220
.trunc,
12211221
.round,
12221222
.tag_name,
1223-
.reify,
12241223
.type_name,
12251224
.frame_type,
12261225
.frame_size,
@@ -2605,6 +2604,7 @@ fn walkInstruction(
26052604
},
26062605
.error_to_int,
26072606
.int_to_error,
2607+
.reify,
26082608
=> {
26092609
const extra = file.zir.extraData(Zir.Inst.UnNode, extended.operand).data;
26102610
const bin_index = self.exprs.items.len;

src/Sema.zig

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -816,7 +816,6 @@ fn analyzeBodyInner(
816816
.embed_file => try sema.zirEmbedFile(block, inst),
817817
.error_name => try sema.zirErrorName(block, inst),
818818
.tag_name => try sema.zirTagName(block, inst),
819-
.reify => try sema.zirReify(block, inst),
820819
.type_name => try sema.zirTypeName(block, inst),
821820
.frame_type => try sema.zirFrameType(block, inst),
822821
.frame_size => try sema.zirFrameSize(block, inst),
@@ -951,6 +950,7 @@ fn analyzeBodyInner(
951950
.select => try sema.zirSelect( block, extended),
952951
.error_to_int => try sema.zirErrorToInt( block, extended),
953952
.int_to_error => try sema.zirIntToError( block, extended),
953+
.reify => try sema.zirReify( block, extended, inst),
954954
// zig fmt: on
955955
.fence => {
956956
try sema.zirFence(block, extended);
@@ -16023,13 +16023,14 @@ fn zirTagName(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air
1602316023
return block.addUnOp(.tag_name, casted_operand);
1602416024
}
1602516025

16026-
fn zirReify(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
16026+
fn zirReify(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstData, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
1602716027
const mod = sema.mod;
16028-
const inst_data = sema.code.instructions.items(.data)[inst].un_node;
16029-
const src = inst_data.src();
16028+
const name_strategy = @intToEnum(Zir.Inst.NameStrategy, extended.small);
16029+
const extra = sema.code.extraData(Zir.Inst.UnNode, extended.operand).data;
16030+
const src = LazySrcLoc.nodeOffset(extra.node);
1603016031
const type_info_ty = try sema.resolveBuiltinTypeFields(block, src, "Type");
16031-
const uncasted_operand = try sema.resolveInst(inst_data.operand);
16032-
const operand_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = inst_data.src_node };
16032+
const uncasted_operand = try sema.resolveInst(extra.operand);
16033+
const operand_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = extra.node };
1603316034
const type_info = try sema.coerce(block, type_info_ty, uncasted_operand, operand_src);
1603416035
const val = try sema.resolveConstValue(block, operand_src, type_info, "operand to @Type must be comptime known");
1603516036
const union_val = val.cast(Value.Payload.Union).?.data;
@@ -16289,7 +16290,7 @@ fn zirReify(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.I
1628916290
return if (is_tuple_val.toBool())
1629016291
try sema.reifyTuple(block, src, fields_val)
1629116292
else
16292-
try sema.reifyStruct(block, inst, src, layout_val, fields_val);
16293+
try sema.reifyStruct(block, inst, src, layout_val, fields_val, name_strategy);
1629316294
},
1629416295
.Enum => {
1629516296
const struct_val = union_val.val.castTag(.aggregate).?.data;
@@ -16338,7 +16339,7 @@ fn zirReify(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.I
1633816339
const new_decl_index = try sema.createAnonymousDeclTypeNamed(block, .{
1633916340
.ty = Type.type,
1634016341
.val = enum_val,
16341-
}, .anon, "enum", null);
16342+
}, name_strategy, "enum", inst);
1634216343
const new_decl = mod.declPtr(new_decl_index);
1634316344
new_decl.owns_tv = true;
1634416345
errdefer mod.abortAnonDecl(new_decl_index);
@@ -16435,7 +16436,7 @@ fn zirReify(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.I
1643516436
const new_decl_index = try sema.createAnonymousDeclTypeNamed(block, .{
1643616437
.ty = Type.type,
1643716438
.val = opaque_val,
16438-
}, .anon, "opaque", null);
16439+
}, name_strategy, "opaque", inst);
1643916440
const new_decl = mod.declPtr(new_decl_index);
1644016441
new_decl.owns_tv = true;
1644116442
errdefer mod.abortAnonDecl(new_decl_index);
@@ -16494,7 +16495,7 @@ fn zirReify(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.I
1649416495
const new_decl_index = try sema.createAnonymousDeclTypeNamed(block, .{
1649516496
.ty = Type.type,
1649616497
.val = new_union_val,
16497-
}, .anon, "union", null);
16498+
}, name_strategy, "union", inst);
1649816499
const new_decl = mod.declPtr(new_decl_index);
1649916500
new_decl.owns_tv = true;
1650016501
errdefer mod.abortAnonDecl(new_decl_index);
@@ -16787,6 +16788,7 @@ fn reifyStruct(
1678716788
src: LazySrcLoc,
1678816789
layout_val: Value,
1678916790
fields_val: Value,
16791+
name_strategy: Zir.Inst.NameStrategy,
1679016792
) CompileError!Air.Inst.Ref {
1679116793
var new_decl_arena = std.heap.ArenaAllocator.init(sema.gpa);
1679216794
errdefer new_decl_arena.deinit();
@@ -16799,7 +16801,7 @@ fn reifyStruct(
1679916801
const new_decl_index = try sema.createAnonymousDeclTypeNamed(block, .{
1680016802
.ty = Type.type,
1680116803
.val = new_struct_val,
16802-
}, .anon, "struct", null);
16804+
}, name_strategy, "struct", inst);
1680316805
const new_decl = mod.declPtr(new_decl_index);
1680416806
new_decl.owns_tv = true;
1680516807
errdefer mod.abortAnonDecl(new_decl_index);

src/Zir.zig

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -839,8 +839,6 @@ pub const Inst = struct {
839839
round,
840840
/// Implement builtin `@tagName`. Uses `un_node`.
841841
tag_name,
842-
/// Implement builtin `@Type`. Uses `un_node`.
843-
reify,
844842
/// Implement builtin `@typeName`. Uses `un_node`.
845843
type_name,
846844
/// Implement builtin `@Frame`. Uses `un_node`.
@@ -1197,7 +1195,6 @@ pub const Inst = struct {
11971195
.trunc,
11981196
.round,
11991197
.tag_name,
1200-
.reify,
12011198
.type_name,
12021199
.frame_type,
12031200
.frame_size,
@@ -1484,7 +1481,6 @@ pub const Inst = struct {
14841481
.trunc,
14851482
.round,
14861483
.tag_name,
1487-
.reify,
14881484
.type_name,
14891485
.frame_type,
14901486
.frame_size,
@@ -1759,7 +1755,6 @@ pub const Inst = struct {
17591755
.trunc = .un_node,
17601756
.round = .un_node,
17611757
.tag_name = .un_node,
1762-
.reify = .un_node,
17631758
.type_name = .un_node,
17641759
.frame_type = .un_node,
17651760
.frame_size = .un_node,
@@ -1980,6 +1975,10 @@ pub const Inst = struct {
19801975
/// Implement builtin `@intToError`.
19811976
/// `operand` is payload index to `UnNode`.
19821977
int_to_error,
1978+
/// Implement builtin `@Type`.
1979+
/// `operand` is payload index to `UnNode`.
1980+
/// `small` contains `NameStrategy
1981+
reify,
19831982

19841983
pub const InstData = struct {
19851984
opcode: Extended,

src/print_zir.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,6 @@ const Writer = struct {
214214
.trunc,
215215
.round,
216216
.tag_name,
217-
.reify,
218217
.type_name,
219218
.frame_type,
220219
.frame_size,
@@ -500,6 +499,7 @@ const Writer = struct {
500499
.wasm_memory_size,
501500
.error_to_int,
502501
.int_to_error,
502+
.reify,
503503
=> {
504504
const inst_data = self.code.extraData(Zir.Inst.UnNode, extended.operand).data;
505505
const src = LazySrcLoc.nodeOffset(inst_data.node);
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
const Tag = @Type(.{
2+
.Enum = .{
3+
.layout = .Auto,
4+
.tag_type = u1,
5+
.fields = &.{
6+
.{ .name = "signed", .value = 0 },
7+
.{ .name = "unsigned", .value = 1 },
8+
},
9+
.decls = &.{},
10+
.is_exhaustive = true,
11+
},
12+
});
13+
const Tagged = @Type(.{
14+
.Union = .{
15+
.layout = .Auto,
16+
.tag_type = Tag,
17+
.fields = &.{
18+
.{ .name = "signed", .field_type = i32, .alignment = @alignOf(i32) },
19+
.{ .name = "unsigned", .field_type = u32, .alignment = @alignOf(u32) },
20+
.{ .name = "arst", .field_type = f32, .alignment = @alignOf(f32) },
21+
},
22+
.decls = &.{},
23+
},
24+
});
25+
export fn entry() void {
26+
var tagged = Tagged{ .signed = -1 };
27+
tagged = .{ .unsigned = 1 };
28+
}
29+
30+
// error
31+
// backend=stage2
32+
// target=native
33+
//
34+
// :13:16: error: no field named 'arst' in enum 'tmp.Tag'
35+
// :1:13: note: enum declared here

0 commit comments

Comments
 (0)