authorgravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2024-06-10 01:22:54+01:00
committergravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2024-06-15 00:57:52+01:00
loge39cc0dff7fff510c3d72f61947b977589ea5583
tree5d7c405520d5c898c46a517820bd331526b7852d
parent82a934bb912241809ac8029e1fa5843092a7fdf6
signaturelock-open Commit is signed but in an unrecognized format.

Zir: use absolute nodes for declarations and type declarations

The justification for using relative source nodes in ZIR is that it allows source locations -- which may be serialized across incremental updates -- to be relative to the source location of their containing declaration. However, having those "baseline" instructions themselves be relative to their own parent is counterproductive, since the source location updating problem is only being moved to `Decl`. Storing the absolute node here instead makes more sense, since it allows for this source location update logic to be elided entirely in the future by storing a `TrackedInst.Index` to resolve a source location relative to rather than a `Decl.Index`.

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

lib/std/zig/AstGen.zig+25-12
......@@ -4011,7 +4011,7 @@ fn fnDecl(
40114011
40124012 // We insert this at the beginning so that its instruction index marks the
40134013 // start of the top level declaration.
4014 const decl_inst = try gz.makeBlockInst(.declaration, fn_proto.ast.proto_node);
4014 const decl_inst = try gz.makeDeclaration(fn_proto.ast.proto_node);
40154015 astgen.advanceSourceCursorToNode(decl_node);
40164016
40174017 var decl_gz: GenZir = .{
......@@ -4393,7 +4393,7 @@ fn globalVarDecl(
43934393 const is_mutable = token_tags[var_decl.ast.mut_token] == .keyword_var;
43944394 // We do this at the beginning so that the instruction index marks the range start
43954395 // of the top level declaration.
4396 const decl_inst = try gz.makeBlockInst(.declaration, node);
4396 const decl_inst = try gz.makeDeclaration(node);
43974397
43984398 const name_token = var_decl.ast.mut_token + 1;
43994399 astgen.advanceSourceCursorToNode(node);
......@@ -4555,7 +4555,7 @@ fn comptimeDecl(
45554555
45564556 // Up top so the ZIR instruction index marks the start range of this
45574557 // top-level declaration.
4558 const decl_inst = try gz.makeBlockInst(.declaration, node);
4558 const decl_inst = try gz.makeDeclaration(node);
45594559 wip_members.nextDecl(decl_inst);
45604560 astgen.advanceSourceCursorToNode(node);
45614561
......@@ -4607,7 +4607,7 @@ fn usingnamespaceDecl(
46074607 };
46084608 // Up top so the ZIR instruction index marks the start range of this
46094609 // top-level declaration.
4610 const decl_inst = try gz.makeBlockInst(.declaration, node);
4610 const decl_inst = try gz.makeDeclaration(node);
46114611 wip_members.nextDecl(decl_inst);
46124612 astgen.advanceSourceCursorToNode(node);
46134613
......@@ -4651,7 +4651,7 @@ fn testDecl(
46514651
46524652 // Up top so the ZIR instruction index marks the start range of this
46534653 // top-level declaration.
4654 const decl_inst = try gz.makeBlockInst(.declaration, node);
4654 const decl_inst = try gz.makeDeclaration(node);
46554655
46564656 wip_members.nextDecl(decl_inst);
46574657 astgen.advanceSourceCursorToNode(node);
......@@ -13071,6 +13071,21 @@ const GenZir = struct {
1307113071 return new_index;
1307213072 }
1307313073
13074 /// Note that this returns a `Zir.Inst.Index` not a ref.
13075 /// Does *not* append the block instruction to the scope.
13076 /// Leaves the `payload_index` field undefined. Use `setDeclaration` to finalize.
13077 fn makeDeclaration(gz: *GenZir, node: Ast.Node.Index) !Zir.Inst.Index {
13078 const new_index: Zir.Inst.Index = @enumFromInt(gz.astgen.instructions.len);
13079 try gz.astgen.instructions.append(gz.astgen.gpa, .{
13080 .tag = .declaration,
13081 .data = .{ .declaration = .{
13082 .src_node = node,
13083 .payload_index = undefined,
13084 } },
13085 });
13086 return new_index;
13087 }
13088
1307413089 /// Note that this returns a `Zir.Inst.Index` not a ref.
1307513090 /// Leaves the `payload_index` field undefined.
1307613091 fn addCondBr(gz: *GenZir, tag: Zir.Inst.Tag, node: Ast.Node.Index) !Zir.Inst.Index {
......@@ -13117,7 +13132,7 @@ const GenZir = struct {
1311713132 .fields_hash_1 = fields_hash_arr[1],
1311813133 .fields_hash_2 = fields_hash_arr[2],
1311913134 .fields_hash_3 = fields_hash_arr[3],
13120 .src_node = gz.nodeIndexToRelative(args.src_node),
13135 .src_node = args.src_node,
1312113136 });
1312213137
1312313138 if (args.captures_len != 0) {
......@@ -13177,7 +13192,7 @@ const GenZir = struct {
1317713192 .fields_hash_1 = fields_hash_arr[1],
1317813193 .fields_hash_2 = fields_hash_arr[2],
1317913194 .fields_hash_3 = fields_hash_arr[3],
13180 .src_node = gz.nodeIndexToRelative(args.src_node),
13195 .src_node = args.src_node,
1318113196 });
1318213197
1318313198 if (args.tag_type != .none) {
......@@ -13238,7 +13253,7 @@ const GenZir = struct {
1323813253 .fields_hash_1 = fields_hash_arr[1],
1323913254 .fields_hash_2 = fields_hash_arr[2],
1324013255 .fields_hash_3 = fields_hash_arr[3],
13241 .src_node = gz.nodeIndexToRelative(args.src_node),
13256 .src_node = args.src_node,
1324213257 });
1324313258
1324413259 if (args.tag_type != .none) {
......@@ -13285,9 +13300,7 @@ const GenZir = struct {
1328513300 assert(args.src_node != 0);
1328613301
1328713302 try astgen.extra.ensureUnusedCapacity(gpa, @typeInfo(Zir.Inst.OpaqueDecl).Struct.fields.len + 2);
13288 const payload_index = astgen.addExtraAssumeCapacity(Zir.Inst.OpaqueDecl{
13289 .src_node = gz.nodeIndexToRelative(args.src_node),
13290 });
13303 const payload_index = astgen.addExtraAssumeCapacity(Zir.Inst.OpaqueDecl{ .src_node = args.src_node });
1329113304
1329213305 if (args.captures_len != 0) {
1329313306 astgen.extra.appendAssumeCapacity(args.captures_len);
......@@ -13897,7 +13910,7 @@ fn setDeclaration(
1389713910 .has_align_linksection_addrspace = align_len != 0 or linksection_len != 0 or addrspace_len != 0,
1389813911 },
1389913912 };
13900 astgen.instructions.items(.data)[@intFromEnum(decl_inst)].pl_node.payload_index = try astgen.addExtra(extra);
13913 astgen.instructions.items(.data)[@intFromEnum(decl_inst)].declaration.payload_index = try astgen.addExtra(extra);
1390113914 if (extra.flags.has_doc_comment) {
1390213915 try astgen.extra.append(gpa, @intFromEnum(true_doc_comment));
1390313916 }
lib/std/zig/Zir.zig+26-11
......@@ -287,7 +287,7 @@ pub const Inst = struct {
287287 /// namespace type, e.g. within a `struct_decl` instruction. It represents a
288288 /// single source declaration (`const`/`var`/`fn`), containing the name,
289289 /// attributes, type, and value of the declaration.
290 /// Uses the `pl_node` union field. Payload is `Declaration`.
290 /// Uses the `declaration` union field. Payload is `Declaration`.
291291 declaration,
292292 /// Implements `suspend {...}`.
293293 /// Uses the `pl_node` union field. Payload is `Block`.
......@@ -1596,7 +1596,7 @@ pub const Inst = struct {
15961596 .block = .pl_node,
15971597 .block_comptime = .pl_node,
15981598 .block_inline = .pl_node,
1599 .declaration = .pl_node,
1599 .declaration = .declaration,
16001600 .suspend_block = .pl_node,
16011601 .bool_not = .un_node,
16021602 .bool_br_and = .pl_node,
......@@ -2370,6 +2370,16 @@ pub const Inst = struct {
23702370 /// The index being accessed.
23712371 idx: u32,
23722372 },
2373 declaration: struct {
2374 /// This node provides a new absolute baseline node for all instructions within this struct.
2375 src_node: Ast.Node.Index,
2376 /// index into extra to a `Declaration` payload.
2377 payload_index: u32,
2378
2379 pub fn src(self: @This()) LazySrcLoc {
2380 return .{ .node_abs = self.src_node };
2381 }
2382 },
23732383
23742384 // Make sure we don't accidentally add a field to make this union
23752385 // bigger than expected. Note that in Debug builds, Zig is allowed
......@@ -2408,6 +2418,7 @@ pub const Inst = struct {
24082418 defer_err_code,
24092419 save_err_ret_index,
24102420 elem_val_imm,
2421 declaration,
24112422 };
24122423 };
24132424
......@@ -3018,10 +3029,11 @@ pub const Inst = struct {
30183029 fields_hash_1: u32,
30193030 fields_hash_2: u32,
30203031 fields_hash_3: u32,
3021 src_node: i32,
3032 /// This node provides a new absolute baseline node for all instructions within this struct.
3033 src_node: Ast.Node.Index,
30223034
30233035 pub fn src(self: StructDecl) LazySrcLoc {
3024 return LazySrcLoc.nodeOffset(self.src_node);
3036 return .{ .node_abs = self.src_node };
30253037 }
30263038
30273039 pub const Small = packed struct {
......@@ -3150,10 +3162,11 @@ pub const Inst = struct {
31503162 fields_hash_1: u32,
31513163 fields_hash_2: u32,
31523164 fields_hash_3: u32,
3153 src_node: i32,
3165 /// This node provides a new absolute baseline node for all instructions within this struct.
3166 src_node: Ast.Node.Index,
31543167
31553168 pub fn src(self: EnumDecl) LazySrcLoc {
3156 return LazySrcLoc.nodeOffset(self.src_node);
3169 return .{ .node_abs = self.src_node };
31573170 }
31583171
31593172 pub const Small = packed struct {
......@@ -3198,10 +3211,11 @@ pub const Inst = struct {
31983211 fields_hash_1: u32,
31993212 fields_hash_2: u32,
32003213 fields_hash_3: u32,
3201 src_node: i32,
3214 /// This node provides a new absolute baseline node for all instructions within this struct.
3215 src_node: Ast.Node.Index,
32023216
32033217 pub fn src(self: UnionDecl) LazySrcLoc {
3204 return LazySrcLoc.nodeOffset(self.src_node);
3218 return .{ .node_abs = self.src_node };
32053219 }
32063220
32073221 pub const Small = packed struct {
......@@ -3230,10 +3244,11 @@ pub const Inst = struct {
32303244 /// 2. capture: Capture, // for every captures_len
32313245 /// 3. decl: Index, // for every decls_len; points to a `declaration` instruction
32323246 pub const OpaqueDecl = struct {
3233 src_node: i32,
3247 /// This node provides a new absolute baseline node for all instructions within this struct.
3248 src_node: Ast.Node.Index,
32343249
32353250 pub fn src(self: OpaqueDecl) LazySrcLoc {
3236 return LazySrcLoc.nodeOffset(self.src_node);
3251 return .{ .node_abs = self.src_node };
32373252 }
32383253
32393254 pub const Small = packed struct {
......@@ -4046,7 +4061,7 @@ pub fn getFnInfo(zir: Zir, fn_inst: Inst.Index) FnInfo {
40464061
40474062pub fn getDeclaration(zir: Zir, inst: Zir.Inst.Index) struct { Inst.Declaration, u32 } {
40484063 assert(zir.instructions.items(.tag)[@intFromEnum(inst)] == .declaration);
4049 const pl_node = zir.instructions.items(.data)[@intFromEnum(inst)].pl_node;
4064 const pl_node = zir.instructions.items(.data)[@intFromEnum(inst)].declaration;
40504065 const extra = zir.extraData(Inst.Declaration, pl_node.payload_index);
40514066 return .{
40524067 extra.data,
src/Module.zig+6-7
......@@ -413,8 +413,8 @@ pub const Decl = struct {
413413 pub fn zirBodies(decl: Decl, zcu: *Zcu) Zir.Inst.Declaration.Bodies {
414414 const zir = decl.getFileScope(zcu).zir;
415415 const zir_index = decl.zir_decl_index.unwrap().?.resolve(&zcu.intern_pool);
416 const pl_node = zir.instructions.items(.data)[@intFromEnum(zir_index)].pl_node;
417 const extra = zir.extraData(Zir.Inst.Declaration, pl_node.payload_index);
416 const declaration = zir.instructions.items(.data)[@intFromEnum(zir_index)].declaration;
417 const extra = zir.extraData(Zir.Inst.Declaration, declaration.payload_index);
418418 return extra.data.getBodies(@intCast(extra.end), zir);
419419 }
420420
......@@ -4255,12 +4255,11 @@ fn scanDecl(iter: *ScanDeclIter, decl_inst: Zir.Inst.Index) Allocator.Error!void
42554255 const zir = namespace.file_scope.zir;
42564256 const ip = &zcu.intern_pool;
42574257
4258 const pl_node = zir.instructions.items(.data)[@intFromEnum(decl_inst)].pl_node;
4259 const extra = zir.extraData(Zir.Inst.Declaration, pl_node.payload_index);
4258 const inst_data = zir.instructions.items(.data)[@intFromEnum(decl_inst)].declaration;
4259 const extra = zir.extraData(Zir.Inst.Declaration, inst_data.payload_index);
42604260 const declaration = extra.data;
42614261
42624262 const line = iter.parent_decl.src_line + declaration.line_offset;
4263 const decl_node = iter.parent_decl.relativeToNodeIndex(pl_node.src_node);
42644263
42654264 // Every Decl needs a name.
42664265 const decl_name: InternPool.NullTerminatedString, const kind: Decl.Kind, const is_named_test: bool = switch (declaration.name) {
......@@ -4348,14 +4347,14 @@ fn scanDecl(iter: *ScanDeclIter, decl_inst: Zir.Inst.Index) Allocator.Error!void
43484347 const was_exported = decl.is_exported;
43494348 assert(decl.kind == kind); // ZIR tracking should preserve this
43504349 decl.name = decl_name;
4351 decl.src_node = decl_node;
4350 decl.src_node = inst_data.src_node;
43524351 decl.src_line = line;
43534352 decl.is_pub = declaration.flags.is_pub;
43544353 decl.is_exported = declaration.flags.is_export;
43554354 break :decl_index .{ was_exported, decl_index };
43564355 } else decl_index: {
43574356 // Create and set up a new Decl.
4358 const new_decl_index = try zcu.allocateNewDecl(namespace_index, decl_node);
4357 const new_decl_index = try zcu.allocateNewDecl(namespace_index, inst_data.src_node);
43594358 const new_decl = zcu.declPtr(new_decl_index);
43604359 new_decl.kind = kind;
43614360 new_decl.name = decl_name;
src/Sema.zig+33-30
......@@ -2835,7 +2835,7 @@ fn zirStructDecl(
28352835
28362836 const new_decl_index = try sema.createAnonymousDeclTypeNamed(
28372837 block,
2838 src,
2838 extra.data.src_node,
28392839 Value.fromInterned(wip_ty.index),
28402840 small.name_strategy,
28412841 "struct",
......@@ -2872,7 +2872,7 @@ fn zirStructDecl(
28722872fn createAnonymousDeclTypeNamed(
28732873 sema: *Sema,
28742874 block: *Block,
2875 src: LazySrcLoc,
2875 src_node: std.zig.Ast.Node.Index,
28762876 val: Value,
28772877 name_strategy: Zir.Inst.NameStrategy,
28782878 anon_prefix: []const u8,
......@@ -2883,31 +2883,17 @@ fn createAnonymousDeclTypeNamed(
28832883 const gpa = sema.gpa;
28842884 const namespace = block.namespace;
28852885 const src_decl = zcu.declPtr(block.src_decl);
2886 const src_node = src_decl.relativeToNodeIndex(src.node_offset.x);
28872886 const new_decl_index = try zcu.allocateNewDecl(namespace, src_node);
28882887 errdefer zcu.destroyDecl(new_decl_index);
28892888
28902889 switch (name_strategy) {
2891 .anon => {
2892 // It would be neat to have "struct:line:column" but this name has
2893 // to survive incremental updates, where it may have been shifted down
2894 // or up to a different line, but unchanged, and thus not unnecessarily
2895 // semantically analyzed.
2896 // This name is also used as the key in the parent namespace so it cannot be
2897 // renamed.
2898
2899 const name = ip.getOrPutStringFmt(gpa, "{}__{s}_{d}", .{
2900 src_decl.name.fmt(ip), anon_prefix, @intFromEnum(new_decl_index),
2901 }, .no_embedded_nulls) catch unreachable;
2902 try zcu.initNewAnonDecl(new_decl_index, src_decl.src_line, val, name);
2903 return new_decl_index;
2904 },
2890 .anon => {}, // handled after switch
29052891 .parent => {
29062892 const name = zcu.declPtr(block.src_decl).name;
29072893 try zcu.initNewAnonDecl(new_decl_index, src_decl.src_line, val, name);
29082894 return new_decl_index;
29092895 },
2910 .func => {
2896 .func => func_strat: {
29112897 const fn_info = sema.code.getFnInfo(ip.funcZirBodyInst(sema.func_index).resolve(ip));
29122898 const zir_tags = sema.code.instructions.items(.tag);
29132899
......@@ -2927,7 +2913,7 @@ fn createAnonymousDeclTypeNamed(
29272913 // function and the name doesn't matter since it will later
29282914 // result in a compile error.
29292915 const arg_val = sema.resolveConstValue(block, .unneeded, arg, undefined) catch
2930 return sema.createAnonymousDeclTypeNamed(block, src, val, .anon, anon_prefix, null);
2916 break :func_strat; // fall through to anon strat
29312917
29322918 if (arg_i != 0) try writer.writeByte(',');
29332919
......@@ -2969,9 +2955,24 @@ fn createAnonymousDeclTypeNamed(
29692955 },
29702956 else => {},
29712957 };
2972 return sema.createAnonymousDeclTypeNamed(block, src, val, .anon, anon_prefix, null);
2958 // fall through to anon strat
29732959 },
29742960 }
2961
2962 // anon strat handling.
2963
2964 // It would be neat to have "struct:line:column" but this name has
2965 // to survive incremental updates, where it may have been shifted down
2966 // or up to a different line, but unchanged, and thus not unnecessarily
2967 // semantically analyzed.
2968 // This name is also used as the key in the parent namespace so it cannot be
2969 // renamed.
2970
2971 const name = ip.getOrPutStringFmt(gpa, "{}__{s}_{d}", .{
2972 src_decl.name.fmt(ip), anon_prefix, @intFromEnum(new_decl_index),
2973 }, .no_embedded_nulls) catch unreachable;
2974 try zcu.initNewAnonDecl(new_decl_index, src_decl.src_line, val, name);
2975 return new_decl_index;
29752976}
29762977
29772978fn zirEnumDecl(
......@@ -2991,7 +2992,6 @@ fn zirEnumDecl(
29912992 var extra_index: usize = extra.end;
29922993
29932994 const src = extra.data.src();
2994 const tag_ty_src: LazySrcLoc = .{ .node_offset_container_tag = src.node_offset.x };
29952995
29962996 const tag_type_ref = if (small.has_tag_type) blk: {
29972997 const tag_type_ref: Zir.Inst.Ref = @enumFromInt(sema.code.extra[extra_index]);
......@@ -3071,7 +3071,7 @@ fn zirEnumDecl(
30713071
30723072 const new_decl_index = try sema.createAnonymousDeclTypeNamed(
30733073 block,
3074 src,
3074 extra.data.src_node,
30753075 Value.fromInterned(wip_ty.index),
30763076 small.name_strategy,
30773077 "enum",
......@@ -3140,14 +3140,17 @@ fn zirEnumDecl(
31403140 };
31413141 defer enum_block.instructions.deinit(sema.gpa);
31423142
3143 // This source location applies in the context of `enum_block`.
3144 const tag_ty_src: LazySrcLoc = .{ .node_offset_container_tag = 0 };
3145
31433146 if (body.len != 0) {
31443147 _ = try sema.analyzeInlineBody(&enum_block, body, inst);
31453148 }
31463149
31473150 if (tag_type_ref != .none) {
3148 const ty = try sema.resolveType(block, tag_ty_src, tag_type_ref);
3151 const ty = try sema.resolveType(&enum_block, tag_ty_src, tag_type_ref);
31493152 if (ty.zigTypeTag(mod) != .Int and ty.zigTypeTag(mod) != .ComptimeInt) {
3150 return sema.fail(block, tag_ty_src, "expected integer tag type, found '{}'", .{ty.fmt(sema.mod)});
3153 return sema.fail(&enum_block, tag_ty_src, "expected integer tag type, found '{}'", .{ty.fmt(sema.mod)});
31513154 }
31523155 break :ty ty;
31533156 } else if (fields_len == 0) {
......@@ -3342,7 +3345,7 @@ fn zirUnionDecl(
33423345
33433346 const new_decl_index = try sema.createAnonymousDeclTypeNamed(
33443347 block,
3345 src,
3348 extra.data.src_node,
33463349 Value.fromInterned(wip_ty.index),
33473350 small.name_strategy,
33483351 "union",
......@@ -3430,7 +3433,7 @@ fn zirOpaqueDecl(
34303433
34313434 const new_decl_index = try sema.createAnonymousDeclTypeNamed(
34323435 block,
3433 src,
3436 extra.data.src_node,
34343437 Value.fromInterned(wip_ty.index),
34353438 small.name_strategy,
34363439 "opaque",
......@@ -21658,7 +21661,7 @@ fn zirReify(
2165821661
2165921662 const new_decl_index = try sema.createAnonymousDeclTypeNamed(
2166021663 block,
21661 src,
21664 mod.declPtr(block.src_decl).relativeToNodeIndex(src.node_offset.x),
2166221665 Value.fromInterned(wip_ty.index),
2166321666 name_strategy,
2166421667 "opaque",
......@@ -21858,7 +21861,7 @@ fn reifyEnum(
2185821861
2185921862 const new_decl_index = try sema.createAnonymousDeclTypeNamed(
2186021863 block,
21861 src,
21864 mod.declPtr(block.src_decl).relativeToNodeIndex(src.node_offset.x),
2186221865 Value.fromInterned(wip_ty.index),
2186321866 name_strategy,
2186421867 "enum",
......@@ -22005,7 +22008,7 @@ fn reifyUnion(
2200522008
2200622009 const new_decl_index = try sema.createAnonymousDeclTypeNamed(
2200722010 block,
22008 src,
22011 mod.declPtr(block.src_decl).relativeToNodeIndex(src.node_offset.x),
2200922012 Value.fromInterned(wip_ty.index),
2201022013 name_strategy,
2201122014 "union",
......@@ -22264,7 +22267,7 @@ fn reifyStruct(
2226422267
2226522268 const new_decl_index = try sema.createAnonymousDeclTypeNamed(
2226622269 block,
22267 src,
22270 mod.declPtr(block.src_decl).relativeToNodeIndex(src.node_offset.x),
2226822271 Value.fromInterned(wip_ty.index),
2226922272 name_strategy,
2227022273 "struct",
src/print_zir.zig+33-38
......@@ -1390,9 +1390,14 @@ const Writer = struct {
13901390 }
13911391
13921392 fn writeStructDecl(self: *Writer, stream: anytype, extended: Zir.Inst.Extended.InstData) !void {
1393 const small = @as(Zir.Inst.StructDecl.Small, @bitCast(extended.small));
1393 const small: Zir.Inst.StructDecl.Small = @bitCast(extended.small);
13941394
13951395 const extra = self.code.extraData(Zir.Inst.StructDecl, extended.operand);
1396
1397 const prev_parent_decl_node = self.parent_decl_node;
1398 self.parent_decl_node = extra.data.src_node;
1399 defer self.parent_decl_node = prev_parent_decl_node;
1400
13961401 const fields_hash: std.zig.SrcHash = @bitCast([4]u32{
13971402 extra.data.fields_hash_0,
13981403 extra.data.fields_hash_1,
......@@ -1465,10 +1470,6 @@ const Writer = struct {
14651470 if (decls_len == 0) {
14661471 try stream.writeAll("{}, ");
14671472 } else {
1468 const prev_parent_decl_node = self.parent_decl_node;
1469 self.parent_decl_node = self.relativeToNodeIndex(extra.data.src_node);
1470 defer self.parent_decl_node = prev_parent_decl_node;
1471
14721473 try stream.writeAll("{\n");
14731474 self.indent += 2;
14741475 try self.writeBody(stream, self.code.bodySlice(extra_index, decls_len));
......@@ -1546,8 +1547,6 @@ const Writer = struct {
15461547 }
15471548 }
15481549
1549 const prev_parent_decl_node = self.parent_decl_node;
1550 self.parent_decl_node = self.relativeToNodeIndex(extra.data.src_node);
15511550 try stream.writeAll("{\n");
15521551 self.indent += 2;
15531552
......@@ -1595,18 +1594,22 @@ const Writer = struct {
15951594 try stream.writeAll(",\n");
15961595 }
15971596
1598 self.parent_decl_node = prev_parent_decl_node;
15991597 self.indent -= 2;
16001598 try stream.writeByteNTimes(' ', self.indent);
16011599 try stream.writeAll("})");
16021600 }
1603 try self.writeSrcNode(stream, extra.data.src_node);
1601 try self.writeSrcNode(stream, 0);
16041602 }
16051603
16061604 fn writeUnionDecl(self: *Writer, stream: anytype, extended: Zir.Inst.Extended.InstData) !void {
16071605 const small = @as(Zir.Inst.UnionDecl.Small, @bitCast(extended.small));
16081606
16091607 const extra = self.code.extraData(Zir.Inst.UnionDecl, extended.operand);
1608
1609 const prev_parent_decl_node = self.parent_decl_node;
1610 self.parent_decl_node = extra.data.src_node;
1611 defer self.parent_decl_node = prev_parent_decl_node;
1612
16101613 const fields_hash: std.zig.SrcHash = @bitCast([4]u32{
16111614 extra.data.fields_hash_0,
16121615 extra.data.fields_hash_1,
......@@ -1670,10 +1673,6 @@ const Writer = struct {
16701673 if (decls_len == 0) {
16711674 try stream.writeAll("{}");
16721675 } else {
1673 const prev_parent_decl_node = self.parent_decl_node;
1674 self.parent_decl_node = self.relativeToNodeIndex(extra.data.src_node);
1675 defer self.parent_decl_node = prev_parent_decl_node;
1676
16771676 try stream.writeAll("{\n");
16781677 self.indent += 2;
16791678 try self.writeBody(stream, self.code.bodySlice(extra_index, decls_len));
......@@ -1690,7 +1689,7 @@ const Writer = struct {
16901689
16911690 if (fields_len == 0) {
16921691 try stream.writeAll("})");
1693 try self.writeSrcNode(stream, extra.data.src_node);
1692 try self.writeSrcNode(stream, 0);
16941693 return;
16951694 }
16961695 try stream.writeAll(", ");
......@@ -1698,8 +1697,6 @@ const Writer = struct {
16981697 const body = self.code.bodySlice(extra_index, body_len);
16991698 extra_index += body.len;
17001699
1701 const prev_parent_decl_node = self.parent_decl_node;
1702 self.parent_decl_node = self.relativeToNodeIndex(extra.data.src_node);
17031700 try self.writeBracedDecl(stream, body);
17041701 try stream.writeAll(", {\n");
17051702
......@@ -1763,17 +1760,21 @@ const Writer = struct {
17631760 try stream.writeAll(",\n");
17641761 }
17651762
1766 self.parent_decl_node = prev_parent_decl_node;
17671763 self.indent -= 2;
17681764 try stream.writeByteNTimes(' ', self.indent);
17691765 try stream.writeAll("})");
1770 try self.writeSrcNode(stream, extra.data.src_node);
1766 try self.writeSrcNode(stream, 0);
17711767 }
17721768
17731769 fn writeEnumDecl(self: *Writer, stream: anytype, extended: Zir.Inst.Extended.InstData) !void {
17741770 const small = @as(Zir.Inst.EnumDecl.Small, @bitCast(extended.small));
17751771
17761772 const extra = self.code.extraData(Zir.Inst.EnumDecl, extended.operand);
1773
1774 const prev_parent_decl_node = self.parent_decl_node;
1775 self.parent_decl_node = extra.data.src_node;
1776 defer self.parent_decl_node = prev_parent_decl_node;
1777
17771778 const fields_hash: std.zig.SrcHash = @bitCast([4]u32{
17781779 extra.data.fields_hash_0,
17791780 extra.data.fields_hash_1,
......@@ -1835,10 +1836,6 @@ const Writer = struct {
18351836 if (decls_len == 0) {
18361837 try stream.writeAll("{}, ");
18371838 } else {
1838 const prev_parent_decl_node = self.parent_decl_node;
1839 self.parent_decl_node = self.relativeToNodeIndex(extra.data.src_node);
1840 defer self.parent_decl_node = prev_parent_decl_node;
1841
18421839 try stream.writeAll("{\n");
18431840 self.indent += 2;
18441841 try self.writeBody(stream, self.code.bodySlice(extra_index, decls_len));
......@@ -1856,12 +1853,9 @@ const Writer = struct {
18561853 const body = self.code.bodySlice(extra_index, body_len);
18571854 extra_index += body.len;
18581855
1859 const prev_parent_decl_node = self.parent_decl_node;
1860 self.parent_decl_node = self.relativeToNodeIndex(extra.data.src_node);
18611856 try self.writeBracedDecl(stream, body);
18621857 if (fields_len == 0) {
18631858 try stream.writeAll(", {})");
1864 self.parent_decl_node = prev_parent_decl_node;
18651859 } else {
18661860 try stream.writeAll(", {\n");
18671861
......@@ -1900,12 +1894,11 @@ const Writer = struct {
19001894 }
19011895 try stream.writeAll(",\n");
19021896 }
1903 self.parent_decl_node = prev_parent_decl_node;
19041897 self.indent -= 2;
19051898 try stream.writeByteNTimes(' ', self.indent);
19061899 try stream.writeAll("})");
19071900 }
1908 try self.writeSrcNode(stream, extra.data.src_node);
1901 try self.writeSrcNode(stream, 0);
19091902 }
19101903
19111904 fn writeOpaqueDecl(
......@@ -1915,6 +1908,11 @@ const Writer = struct {
19151908 ) !void {
19161909 const small = @as(Zir.Inst.OpaqueDecl.Small, @bitCast(extended.small));
19171910 const extra = self.code.extraData(Zir.Inst.OpaqueDecl, extended.operand);
1911
1912 const prev_parent_decl_node = self.parent_decl_node;
1913 self.parent_decl_node = extra.data.src_node;
1914 defer self.parent_decl_node = prev_parent_decl_node;
1915
19181916 var extra_index: usize = extra.end;
19191917
19201918 const captures_len = if (small.has_captures_len) blk: {
......@@ -1948,10 +1946,6 @@ const Writer = struct {
19481946 if (decls_len == 0) {
19491947 try stream.writeAll("{})");
19501948 } else {
1951 const prev_parent_decl_node = self.parent_decl_node;
1952 self.parent_decl_node = self.relativeToNodeIndex(extra.data.src_node);
1953 defer self.parent_decl_node = prev_parent_decl_node;
1954
19551949 try stream.writeAll("{\n");
19561950 self.indent += 2;
19571951 try self.writeBody(stream, self.code.bodySlice(extra_index, decls_len));
......@@ -1959,7 +1953,7 @@ const Writer = struct {
19591953 try stream.writeByteNTimes(' ', self.indent);
19601954 try stream.writeAll("})");
19611955 }
1962 try self.writeSrcNode(stream, extra.data.src_node);
1956 try self.writeSrcNode(stream, 0);
19631957 }
19641958
19651959 fn writeErrorSetDecl(
......@@ -2729,11 +2723,16 @@ const Writer = struct {
27292723 }
27302724
27312725 fn writeDeclaration(self: *Writer, stream: anytype, inst: Zir.Inst.Index) !void {
2732 const inst_data = self.code.instructions.items(.data)[@intFromEnum(inst)].pl_node;
2726 const inst_data = self.code.instructions.items(.data)[@intFromEnum(inst)].declaration;
27332727 const extra = self.code.extraData(Zir.Inst.Declaration, inst_data.payload_index);
27342728 const doc_comment: ?Zir.NullTerminatedString = if (extra.data.flags.has_doc_comment) dc: {
27352729 break :dc @enumFromInt(self.code.extra[extra.end]);
27362730 } else null;
2731
2732 const prev_parent_decl_node = self.parent_decl_node;
2733 defer self.parent_decl_node = prev_parent_decl_node;
2734 self.parent_decl_node = inst_data.src_node;
2735
27372736 if (extra.data.flags.is_pub) try stream.writeAll("pub ");
27382737 if (extra.data.flags.is_export) try stream.writeAll("export ");
27392738 switch (extra.data.name) {
......@@ -2757,10 +2756,6 @@ const Writer = struct {
27572756 try stream.print(" line(+{d}) hash({})", .{ extra.data.line_offset, std.fmt.fmtSliceHexLower(&src_hash_bytes) });
27582757
27592758 {
2760 const prev_parent_decl_node = self.parent_decl_node;
2761 defer self.parent_decl_node = prev_parent_decl_node;
2762 self.parent_decl_node = self.relativeToNodeIndex(inst_data.src_node);
2763
27642759 const bodies = extra.data.getBodies(@intCast(extra.end), self.code);
27652760
27662761 try stream.writeAll(" value=");
......@@ -2783,7 +2778,7 @@ const Writer = struct {
27832778 }
27842779
27852780 try stream.writeAll(") ");
2786 try self.writeSrc(stream, inst_data.src());
2781 try self.writeSrcNode(stream, 0);
27872782 }
27882783
27892784 fn writeClosureGet(self: *Writer, stream: anytype, extended: Zir.Inst.Extended.InstData) !void {