authorgravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2024-02-14 22:01:18+00:00
committergravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2024-02-16 11:26:34+00:00
log260c84535546c81028cf42f1eb6ec9f17275db0f
tree566fd6156bbbbed22741b0d94b996542ee334ae6
parent10784c7fc8d419fff943994f1aa5a454d4e43391
signaturelock-open Commit is signed but in an unrecognized format.

Zir: make src_node of type declarations non-optional

Previously, the `src_node` field of `struct_decl`, `union_decl`, `enum_decl`, and `opaque_decl` was optional, included in trailing data only if a flag in `Small` was set. However, this was unnecessary logic: AstGen always provided the source node. We can simplify a few bits of logic by making this field non-optional, moving it into non-trailing data. There was one place where the field was actually omitted before: the root struct of a file was at source node 0, so the node was coincidentally elided. Therefore, this commit has a fixed cost of 4 bytes of ZIR per file.

5 files changed, 111 insertions(+), 178 deletions(-)

src/AstGen.zig+19-25
......@@ -12918,20 +12918,20 @@ const GenZir = struct {
1291812918 const astgen = gz.astgen;
1291912919 const gpa = astgen.gpa;
1292012920
12921 // Node 0 is valid for the root `struct_decl` of a file!
12922 assert(args.src_node != 0 or gz.parent.tag == .top);
12923
1292112924 const fields_hash_arr: [4]u32 = @bitCast(args.fields_hash);
1292212925
12923 try astgen.extra.ensureUnusedCapacity(gpa, @typeInfo(Zir.Inst.StructDecl).Struct.fields.len + 6);
12926 try astgen.extra.ensureUnusedCapacity(gpa, @typeInfo(Zir.Inst.StructDecl).Struct.fields.len + 4);
1292412927 const payload_index = astgen.addExtraAssumeCapacity(Zir.Inst.StructDecl{
1292512928 .fields_hash_0 = fields_hash_arr[0],
1292612929 .fields_hash_1 = fields_hash_arr[1],
1292712930 .fields_hash_2 = fields_hash_arr[2],
1292812931 .fields_hash_3 = fields_hash_arr[3],
12932 .src_node = gz.nodeIndexToRelative(args.src_node),
1292912933 });
1293012934
12931 if (args.src_node != 0) {
12932 const node_offset = gz.nodeIndexToRelative(args.src_node);
12933 astgen.extra.appendAssumeCapacity(@bitCast(node_offset));
12934 }
1293512935 if (args.fields_len != 0) {
1293612936 astgen.extra.appendAssumeCapacity(args.fields_len);
1293712937 }
......@@ -12949,7 +12949,6 @@ const GenZir = struct {
1294912949 .data = .{ .extended = .{
1295012950 .opcode = .struct_decl,
1295112951 .small = @bitCast(Zir.Inst.StructDecl.Small{
12952 .has_src_node = args.src_node != 0,
1295312952 .has_fields_len = args.fields_len != 0,
1295412953 .has_decls_len = args.decls_len != 0,
1295512954 .has_backing_int = args.backing_int_ref != .none,
......@@ -12981,20 +12980,19 @@ const GenZir = struct {
1298112980 const astgen = gz.astgen;
1298212981 const gpa = astgen.gpa;
1298312982
12983 assert(args.src_node != 0);
12984
1298412985 const fields_hash_arr: [4]u32 = @bitCast(args.fields_hash);
1298512986
12986 try astgen.extra.ensureUnusedCapacity(gpa, @typeInfo(Zir.Inst.UnionDecl).Struct.fields.len + 5);
12987 try astgen.extra.ensureUnusedCapacity(gpa, @typeInfo(Zir.Inst.UnionDecl).Struct.fields.len + 4);
1298712988 const payload_index = astgen.addExtraAssumeCapacity(Zir.Inst.UnionDecl{
1298812989 .fields_hash_0 = fields_hash_arr[0],
1298912990 .fields_hash_1 = fields_hash_arr[1],
1299012991 .fields_hash_2 = fields_hash_arr[2],
1299112992 .fields_hash_3 = fields_hash_arr[3],
12993 .src_node = gz.nodeIndexToRelative(args.src_node),
1299212994 });
1299312995
12994 if (args.src_node != 0) {
12995 const node_offset = gz.nodeIndexToRelative(args.src_node);
12996 astgen.extra.appendAssumeCapacity(@bitCast(node_offset));
12997 }
1299812996 if (args.tag_type != .none) {
1299912997 astgen.extra.appendAssumeCapacity(@intFromEnum(args.tag_type));
1300012998 }
......@@ -13012,7 +13010,6 @@ const GenZir = struct {
1301213010 .data = .{ .extended = .{
1301313011 .opcode = .union_decl,
1301413012 .small = @bitCast(Zir.Inst.UnionDecl.Small{
13015 .has_src_node = args.src_node != 0,
1301613013 .has_tag_type = args.tag_type != .none,
1301713014 .has_body_len = args.body_len != 0,
1301813015 .has_fields_len = args.fields_len != 0,
......@@ -13039,20 +13036,19 @@ const GenZir = struct {
1303913036 const astgen = gz.astgen;
1304013037 const gpa = astgen.gpa;
1304113038
13039 assert(args.src_node != 0);
13040
1304213041 const fields_hash_arr: [4]u32 = @bitCast(args.fields_hash);
1304313042
13044 try astgen.extra.ensureUnusedCapacity(gpa, @typeInfo(Zir.Inst.EnumDecl).Struct.fields.len + 5);
13043 try astgen.extra.ensureUnusedCapacity(gpa, @typeInfo(Zir.Inst.EnumDecl).Struct.fields.len + 4);
1304513044 const payload_index = astgen.addExtraAssumeCapacity(Zir.Inst.EnumDecl{
1304613045 .fields_hash_0 = fields_hash_arr[0],
1304713046 .fields_hash_1 = fields_hash_arr[1],
1304813047 .fields_hash_2 = fields_hash_arr[2],
1304913048 .fields_hash_3 = fields_hash_arr[3],
13049 .src_node = gz.nodeIndexToRelative(args.src_node),
1305013050 });
1305113051
13052 if (args.src_node != 0) {
13053 const node_offset = gz.nodeIndexToRelative(args.src_node);
13054 astgen.extra.appendAssumeCapacity(@bitCast(node_offset));
13055 }
1305613052 if (args.tag_type != .none) {
1305713053 astgen.extra.appendAssumeCapacity(@intFromEnum(args.tag_type));
1305813054 }
......@@ -13070,7 +13066,6 @@ const GenZir = struct {
1307013066 .data = .{ .extended = .{
1307113067 .opcode = .enum_decl,
1307213068 .small = @bitCast(Zir.Inst.EnumDecl.Small{
13073 .has_src_node = args.src_node != 0,
1307413069 .has_tag_type = args.tag_type != .none,
1307513070 .has_body_len = args.body_len != 0,
1307613071 .has_fields_len = args.fields_len != 0,
......@@ -13090,13 +13085,13 @@ const GenZir = struct {
1309013085 const astgen = gz.astgen;
1309113086 const gpa = astgen.gpa;
1309213087
13093 try astgen.extra.ensureUnusedCapacity(gpa, 2);
13094 const payload_index: u32 = @intCast(astgen.extra.items.len);
13088 assert(args.src_node != 0);
13089
13090 try astgen.extra.ensureUnusedCapacity(gpa, @typeInfo(Zir.Inst.OpaqueDecl).Struct.fields.len + 1);
13091 const payload_index = astgen.addExtraAssumeCapacity(Zir.Inst.OpaqueDecl{
13092 .src_node = gz.nodeIndexToRelative(args.src_node),
13093 });
1309513094
13096 if (args.src_node != 0) {
13097 const node_offset = gz.nodeIndexToRelative(args.src_node);
13098 astgen.extra.appendAssumeCapacity(@bitCast(node_offset));
13099 }
1310013095 if (args.decls_len != 0) {
1310113096 astgen.extra.appendAssumeCapacity(args.decls_len);
1310213097 }
......@@ -13105,7 +13100,6 @@ const GenZir = struct {
1310513100 .data = .{ .extended = .{
1310613101 .opcode = .opaque_decl,
1310713102 .small = @bitCast(Zir.Inst.OpaqueDecl.Small{
13108 .has_src_node = args.src_node != 0,
1310913103 .has_decls_len = args.decls_len != 0,
1311013104 .name_strategy = gz.anon_name_strategy,
1311113105 }),
src/Autodoc.zig+12-45
......@@ -3395,19 +3395,10 @@ fn walkInstruction(
33953395 .enclosing_type = type_slot_index,
33963396 };
33973397
3398 const small = @as(Zir.Inst.OpaqueDecl.Small, @bitCast(extended.small));
3399 var extra_index: usize = extended.operand;
3400
3401 const src_node: ?i32 = if (small.has_src_node) blk: {
3402 const src_node = @as(i32, @bitCast(file.zir.extra[extra_index]));
3403 extra_index += 1;
3404 break :blk src_node;
3405 } else null;
3398 const extra = file.zir.extraData(Zir.Inst.OpaqueDecl, extended.operand);
3399 var extra_index: usize = extra.end;
34063400
3407 const src_info = if (src_node) |sn|
3408 try self.srcLocInfo(file, sn, parent_src)
3409 else
3410 parent_src;
3401 const src_info = try self.srcLocInfo(file, extra.data.src_node, parent_src);
34113402
34123403 var decl_indexes: std.ArrayListUnmanaged(usize) = .{};
34133404 var priv_decl_indexes: std.ArrayListUnmanaged(usize) = .{};
......@@ -3498,18 +3489,10 @@ fn walkInstruction(
34983489 };
34993490
35003491 const small = @as(Zir.Inst.UnionDecl.Small, @bitCast(extended.small));
3501 var extra_index: usize = extended.operand + @typeInfo(Zir.Inst.UnionDecl).Struct.fields.len;
3502
3503 const src_node: ?i32 = if (small.has_src_node) blk: {
3504 const src_node = @as(i32, @bitCast(file.zir.extra[extra_index]));
3505 extra_index += 1;
3506 break :blk src_node;
3507 } else null;
3492 const extra = file.zir.extraData(Zir.Inst.UnionDecl, extended.operand);
3493 var extra_index: usize = extra.end;
35083494
3509 const src_info = if (src_node) |sn|
3510 try self.srcLocInfo(file, sn, parent_src)
3511 else
3512 parent_src;
3495 const src_info = try self.srcLocInfo(file, extra.data.src_node, parent_src);
35133496
35143497 // We delay analysis because union tags can refer to
35153498 // decls defined inside the union itself.
......@@ -3628,18 +3611,10 @@ fn walkInstruction(
36283611 };
36293612
36303613 const small = @as(Zir.Inst.EnumDecl.Small, @bitCast(extended.small));
3631 var extra_index: usize = extended.operand + @typeInfo(Zir.Inst.EnumDecl).Struct.fields.len;
3632
3633 const src_node: ?i32 = if (small.has_src_node) blk: {
3634 const src_node = @as(i32, @bitCast(file.zir.extra[extra_index]));
3635 extra_index += 1;
3636 break :blk src_node;
3637 } else null;
3614 const extra = file.zir.extraData(Zir.Inst.EnumDecl, extended.operand);
3615 var extra_index: usize = extra.end;
36383616
3639 const src_info = if (src_node) |sn|
3640 try self.srcLocInfo(file, sn, parent_src)
3641 else
3642 parent_src;
3617 const src_info = try self.srcLocInfo(file, extra.data.src_node, parent_src);
36433618
36443619 const tag_type: ?DocData.Expr = if (small.has_tag_type) blk: {
36453620 const tag_type = file.zir.extra[extra_index];
......@@ -3779,18 +3754,10 @@ fn walkInstruction(
37793754 };
37803755
37813756 const small = @as(Zir.Inst.StructDecl.Small, @bitCast(extended.small));
3782 var extra_index: usize = extended.operand + @typeInfo(Zir.Inst.StructDecl).Struct.fields.len;
3783
3784 const src_node: ?i32 = if (small.has_src_node) blk: {
3785 const src_node = @as(i32, @bitCast(file.zir.extra[extra_index]));
3786 extra_index += 1;
3787 break :blk src_node;
3788 } else null;
3757 const extra = file.zir.extraData(Zir.Inst.StructDecl, extended.operand);
3758 var extra_index: usize = extra.end;
37893759
3790 const src_info = if (src_node) |sn|
3791 try self.srcLocInfo(file, sn, parent_src)
3792 else
3793 parent_src;
3760 const src_info = try self.srcLocInfo(file, extra.data.src_node, parent_src);
37943761
37953762 const fields_len = if (small.has_fields_len) blk: {
37963763 const fields_len = file.zir.extra[extra_index];
src/Sema.zig+10-27
......@@ -2725,7 +2725,6 @@ pub fn getStructType(
27252725 const small: Zir.Inst.StructDecl.Small = @bitCast(extended.small);
27262726
27272727 var extra_index: usize = extended.operand + @typeInfo(Zir.Inst.StructDecl).Struct.fields.len;
2728 extra_index += @intFromBool(small.has_src_node);
27292728 const fields_len = if (small.has_fields_len) blk: {
27302729 const fields_len = sema.code.extra[extra_index];
27312730 extra_index += 1;
......@@ -2778,10 +2777,7 @@ fn zirStructDecl(
27782777 const mod = sema.mod;
27792778 const ip = &mod.intern_pool;
27802779 const small: Zir.Inst.StructDecl.Small = @bitCast(extended.small);
2781 const src: LazySrcLoc = if (small.has_src_node) blk: {
2782 const node_offset: i32 = @bitCast(sema.code.extra[extended.operand + @typeInfo(Zir.Inst.StructDecl).Struct.fields.len]);
2783 break :blk LazySrcLoc.nodeOffset(node_offset);
2784 } else unreachable; // MLUGG TODO
2780 const src = sema.code.extraData(Zir.Inst.StructDecl, extended.operand).data.src();
27852781
27862782 // Because these three things each reference each other, `undefined`
27872783 // placeholders are used before being set after the struct type gains an
......@@ -2941,13 +2937,10 @@ fn zirEnumDecl(
29412937 const mod = sema.mod;
29422938 const gpa = sema.gpa;
29432939 const small: Zir.Inst.EnumDecl.Small = @bitCast(extended.small);
2944 var extra_index: usize = extended.operand + @typeInfo(Zir.Inst.EnumDecl).Struct.fields.len;
2940 const extra = sema.code.extraData(Zir.Inst.EnumDecl, extended.operand);
2941 var extra_index: usize = extra.end;
29452942
2946 const src: LazySrcLoc = if (small.has_src_node) blk: {
2947 const node_offset: i32 = @bitCast(sema.code.extra[extra_index]);
2948 extra_index += 1;
2949 break :blk LazySrcLoc.nodeOffset(node_offset);
2950 } else unreachable; // MLUGG TODO
2943 const src = extra.data.src();
29512944 const tag_ty_src: LazySrcLoc = .{ .node_offset_container_tag = src.node_offset.x };
29522945
29532946 const tag_type_ref = if (small.has_tag_type) blk: {
......@@ -3214,13 +3207,10 @@ fn zirUnionDecl(
32143207 const mod = sema.mod;
32153208 const gpa = sema.gpa;
32163209 const small: Zir.Inst.UnionDecl.Small = @bitCast(extended.small);
3217 var extra_index: usize = extended.operand + @typeInfo(Zir.Inst.UnionDecl).Struct.fields.len;
3210 const extra = sema.code.extraData(Zir.Inst.UnionDecl, extended.operand);
3211 var extra_index: usize = extra.end;
32183212
3219 const src: LazySrcLoc = if (small.has_src_node) blk: {
3220 const node_offset: i32 = @bitCast(sema.code.extra[extra_index]);
3221 extra_index += 1;
3222 break :blk LazySrcLoc.nodeOffset(node_offset);
3223 } else unreachable; // MLUGG TODO
3213 const src = extra.data.src();
32243214
32253215 extra_index += @intFromBool(small.has_tag_type);
32263216 extra_index += @intFromBool(small.has_body_len);
......@@ -3323,13 +3313,10 @@ fn zirOpaqueDecl(
33233313
33243314 const mod = sema.mod;
33253315 const small: Zir.Inst.OpaqueDecl.Small = @bitCast(extended.small);
3326 var extra_index: usize = extended.operand;
3316 const extra = sema.code.extraData(Zir.Inst.OpaqueDecl, extended.operand);
3317 var extra_index: usize = extra.end;
33273318
3328 const src: LazySrcLoc = if (small.has_src_node) blk: {
3329 const node_offset: i32 = @bitCast(sema.code.extra[extra_index]);
3330 extra_index += 1;
3331 break :blk LazySrcLoc.nodeOffset(node_offset);
3332 } else unreachable; // MLUGG TODO
3319 const src = extra.data.src();
33333320
33343321 const decls_len = if (small.has_decls_len) blk: {
33353322 const decls_len = sema.code.extra[extra_index];
......@@ -35662,7 +35649,6 @@ fn semaBackingIntType(mod: *Module, struct_type: InternPool.Key.StructType) Comp
3566235649
3566335650 if (small.has_backing_int) {
3566435651 var extra_index: usize = extended.operand + @typeInfo(Zir.Inst.StructDecl).Struct.fields.len;
35665 extra_index += @intFromBool(small.has_src_node);
3566635652 extra_index += @intFromBool(small.has_fields_len);
3566735653 extra_index += @intFromBool(small.has_decls_len);
3566835654
......@@ -36374,8 +36360,6 @@ fn structZirInfo(zir: Zir, zir_index: Zir.Inst.Index) struct {
3637436360 const small: Zir.Inst.StructDecl.Small = @bitCast(extended.small);
3637536361 var extra_index: usize = extended.operand + @typeInfo(Zir.Inst.StructDecl).Struct.fields.len;
3637636362
36377 extra_index += @intFromBool(small.has_src_node);
36378
3637936363 const fields_len = if (small.has_fields_len) blk: {
3638036364 const fields_len = zir.extra[extra_index];
3638136365 extra_index += 1;
......@@ -36843,7 +36827,6 @@ fn semaUnionFields(mod: *Module, arena: Allocator, union_type: InternPool.Key.Un
3684336827 var extra_index: usize = extended.operand + @typeInfo(Zir.Inst.UnionDecl).Struct.fields.len;
3684436828
3684536829 const src = LazySrcLoc.nodeOffset(0);
36846 extra_index += @intFromBool(small.has_src_node);
3684736830
3684836831 const tag_type_ref: Zir.Inst.Ref = if (small.has_tag_type) blk: {
3684936832 const ty_ref: Zir.Inst.Ref = @enumFromInt(zir.extra[extra_index]);
src/Zir.zig+56-44
......@@ -3022,20 +3022,19 @@ pub const Inst = struct {
30223022 };
30233023
30243024 /// Trailing:
3025 /// 0. src_node: i32, // if has_src_node
3026 /// 1. fields_len: u32, // if has_fields_len
3027 /// 2. decls_len: u32, // if has_decls_len
3028 /// 3. backing_int_body_len: u32, // if has_backing_int
3029 /// 4. backing_int_ref: Ref, // if has_backing_int and backing_int_body_len is 0
3030 /// 5. backing_int_body_inst: Inst, // if has_backing_int and backing_int_body_len is > 0
3031 /// 6. decl: Index, // for every decls_len; points to a `declaration` instruction
3032 /// 7. flags: u32 // for every 8 fields
3025 /// 0. fields_len: u32, // if has_fields_len
3026 /// 1. decls_len: u32, // if has_decls_len
3027 /// 2. backing_int_body_len: u32, // if has_backing_int
3028 /// 3. backing_int_ref: Ref, // if has_backing_int and backing_int_body_len is 0
3029 /// 4. backing_int_body_inst: Inst, // if has_backing_int and backing_int_body_len is > 0
3030 /// 5. decl: Index, // for every decls_len; points to a `declaration` instruction
3031 /// 6. flags: u32 // for every 8 fields
30333032 /// - sets of 4 bits:
30343033 /// 0b000X: whether corresponding field has an align expression
30353034 /// 0b00X0: whether corresponding field has a default expression
30363035 /// 0b0X00: whether corresponding field is comptime
30373036 /// 0bX000: whether corresponding field has a type expression
3038 /// 8. fields: { // for every fields_len
3037 /// 7. fields: { // for every fields_len
30393038 /// field_name: u32, // if !is_tuple
30403039 /// doc_comment: NullTerminatedString, // .empty if no doc comment
30413040 /// field_type: Ref, // if corresponding bit is not set. none means anytype.
......@@ -3043,7 +3042,7 @@ pub const Inst = struct {
30433042 /// align_body_len: u32, // if corresponding bit is set
30443043 /// init_body_len: u32, // if corresponding bit is set
30453044 /// }
3046 /// 10. bodies: { // for every fields_len
3045 /// 8. bodies: { // for every fields_len
30473046 /// field_type_body_inst: Inst, // for each field_type_body_len
30483047 /// align_body_inst: Inst, // for each align_body_len
30493048 /// init_body_inst: Inst, // for each init_body_len
......@@ -3055,8 +3054,13 @@ pub const Inst = struct {
30553054 fields_hash_1: u32,
30563055 fields_hash_2: u32,
30573056 fields_hash_3: u32,
3057 src_node: i32,
3058
3059 pub fn src(self: StructDecl) LazySrcLoc {
3060 return LazySrcLoc.nodeOffset(self.src_node);
3061 }
3062
30583063 pub const Small = packed struct {
3059 has_src_node: bool,
30603064 has_fields_len: bool,
30613065 has_decls_len: bool,
30623066 has_backing_int: bool,
......@@ -3068,7 +3072,7 @@ pub const Inst = struct {
30683072 any_default_inits: bool,
30693073 any_comptime_fields: bool,
30703074 any_aligned_fields: bool,
3071 _: u2 = undefined,
3075 _: u3 = undefined,
30723076 };
30733077 };
30743078
......@@ -3102,16 +3106,15 @@ pub const Inst = struct {
31023106 };
31033107
31043108 /// Trailing:
3105 /// 0. src_node: i32, // if has_src_node
3106 /// 1. tag_type: Ref, // if has_tag_type
3107 /// 2. body_len: u32, // if has_body_len
3108 /// 3. fields_len: u32, // if has_fields_len
3109 /// 4. decls_len: u32, // if has_decls_len
3110 /// 5. decl: Index, // for every decls_len; points to a `declaration` instruction
3111 /// 6. inst: Index // for every body_len
3112 /// 7. has_bits: u32 // for every 32 fields
3109 /// 0. tag_type: Ref, // if has_tag_type
3110 /// 1. body_len: u32, // if has_body_len
3111 /// 2. fields_len: u32, // if has_fields_len
3112 /// 3. decls_len: u32, // if has_decls_len
3113 /// 4. decl: Index, // for every decls_len; points to a `declaration` instruction
3114 /// 5. inst: Index // for every body_len
3115 /// 6. has_bits: u32 // for every 32 fields
31133116 /// - the bit is whether corresponding field has an value expression
3114 /// 8. fields: { // for every fields_len
3117 /// 7. fields: { // for every fields_len
31153118 /// field_name: u32,
31163119 /// doc_comment: u32, // .empty if no doc_comment
31173120 /// value: Ref, // if corresponding bit is set
......@@ -3123,33 +3126,37 @@ pub const Inst = struct {
31233126 fields_hash_1: u32,
31243127 fields_hash_2: u32,
31253128 fields_hash_3: u32,
3129 src_node: i32,
3130
3131 pub fn src(self: EnumDecl) LazySrcLoc {
3132 return LazySrcLoc.nodeOffset(self.src_node);
3133 }
3134
31263135 pub const Small = packed struct {
3127 has_src_node: bool,
31283136 has_tag_type: bool,
31293137 has_body_len: bool,
31303138 has_fields_len: bool,
31313139 has_decls_len: bool,
31323140 name_strategy: NameStrategy,
31333141 nonexhaustive: bool,
3134 _: u8 = undefined,
3142 _: u9 = undefined,
31353143 };
31363144 };
31373145
31383146 /// Trailing:
3139 /// 0. src_node: i32, // if has_src_node
3140 /// 1. tag_type: Ref, // if has_tag_type
3141 /// 2. body_len: u32, // if has_body_len
3142 /// 3. fields_len: u32, // if has_fields_len
3143 /// 4. decls_len: u32, // if has_decls_len
3144 /// 5. decl: Index, // for every decls_len; points to a `declaration` instruction
3145 /// 6. inst: Index // for every body_len
3146 /// 7. has_bits: u32 // for every 8 fields
3147 /// 0. tag_type: Ref, // if has_tag_type
3148 /// 1. body_len: u32, // if has_body_len
3149 /// 2. fields_len: u32, // if has_fields_len
3150 /// 3. decls_len: u32, // if has_decls_len
3151 /// 4. decl: Index, // for every decls_len; points to a `declaration` instruction
3152 /// 5. inst: Index // for every body_len
3153 /// 6. has_bits: u32 // for every 8 fields
31473154 /// - sets of 4 bits:
31483155 /// 0b000X: whether corresponding field has a type expression
31493156 /// 0b00X0: whether corresponding field has a align expression
31503157 /// 0b0X00: whether corresponding field has a tag value expression
31513158 /// 0bX000: unused
3152 /// 8. fields: { // for every fields_len
3159 /// 7. fields: { // for every fields_len
31533160 /// field_name: NullTerminatedString, // null terminated string index
31543161 /// doc_comment: NullTerminatedString, // .empty if no doc comment
31553162 /// field_type: Ref, // if corresponding bit is set
......@@ -3164,8 +3171,13 @@ pub const Inst = struct {
31643171 fields_hash_1: u32,
31653172 fields_hash_2: u32,
31663173 fields_hash_3: u32,
3174 src_node: i32,
3175
3176 pub fn src(self: UnionDecl) LazySrcLoc {
3177 return LazySrcLoc.nodeOffset(self.src_node);
3178 }
3179
31673180 pub const Small = packed struct {
3168 has_src_node: bool,
31693181 has_tag_type: bool,
31703182 has_body_len: bool,
31713183 has_fields_len: bool,
......@@ -3180,20 +3192,24 @@ pub const Inst = struct {
31803192 /// true | false | union(T) { }
31813193 auto_enum_tag: bool,
31823194 any_aligned_fields: bool,
3183 _: u5 = undefined,
3195 _: u6 = undefined,
31843196 };
31853197 };
31863198
31873199 /// Trailing:
3188 /// 0. src_node: i32, // if has_src_node
3189 /// 1. decls_len: u32, // if has_decls_len
3190 /// 2. decl: Index, // for every decls_len; points to a `declaration` instruction
3200 /// 0. decls_len: u32, // if has_decls_len
3201 /// 1. decl: Index, // for every decls_len; points to a `declaration` instruction
31913202 pub const OpaqueDecl = struct {
3203 src_node: i32,
3204
3205 pub fn src(self: OpaqueDecl) LazySrcLoc {
3206 return LazySrcLoc.nodeOffset(self.src_node);
3207 }
3208
31923209 pub const Small = packed struct {
3193 has_src_node: bool,
31943210 has_decls_len: bool,
31953211 name_strategy: NameStrategy,
3196 _: u12 = undefined,
3212 _: u13 = undefined,
31973213 };
31983214 };
31993215
......@@ -3495,7 +3511,6 @@ pub fn declIterator(zir: Zir, decl_inst: Zir.Inst.Index) DeclIterator {
34953511 .struct_decl => {
34963512 const small: Inst.StructDecl.Small = @bitCast(extended.small);
34973513 var extra_index: u32 = @intCast(extended.operand + @typeInfo(Inst.StructDecl).Struct.fields.len);
3498 extra_index += @intFromBool(small.has_src_node);
34993514 extra_index += @intFromBool(small.has_fields_len);
35003515 const decls_len = if (small.has_decls_len) decls_len: {
35013516 const decls_len = zir.extra[extra_index];
......@@ -3522,7 +3537,6 @@ pub fn declIterator(zir: Zir, decl_inst: Zir.Inst.Index) DeclIterator {
35223537 .enum_decl => {
35233538 const small: Inst.EnumDecl.Small = @bitCast(extended.small);
35243539 var extra_index: u32 = @intCast(extended.operand + @typeInfo(Inst.EnumDecl).Struct.fields.len);
3525 extra_index += @intFromBool(small.has_src_node);
35263540 extra_index += @intFromBool(small.has_tag_type);
35273541 extra_index += @intFromBool(small.has_body_len);
35283542 extra_index += @intFromBool(small.has_fields_len);
......@@ -3541,7 +3555,6 @@ pub fn declIterator(zir: Zir, decl_inst: Zir.Inst.Index) DeclIterator {
35413555 .union_decl => {
35423556 const small: Inst.UnionDecl.Small = @bitCast(extended.small);
35433557 var extra_index: u32 = @intCast(extended.operand + @typeInfo(Inst.UnionDecl).Struct.fields.len);
3544 extra_index += @intFromBool(small.has_src_node);
35453558 extra_index += @intFromBool(small.has_tag_type);
35463559 extra_index += @intFromBool(small.has_body_len);
35473560 extra_index += @intFromBool(small.has_fields_len);
......@@ -3559,8 +3572,7 @@ pub fn declIterator(zir: Zir, decl_inst: Zir.Inst.Index) DeclIterator {
35593572 },
35603573 .opaque_decl => {
35613574 const small: Inst.OpaqueDecl.Small = @bitCast(extended.small);
3562 var extra_index: u32 = extended.operand;
3563 extra_index += @intFromBool(small.has_src_node);
3575 var extra_index: u32 = @intCast(extended.operand + @typeInfo(Inst.OpaqueDecl).Struct.fields.len);
35643576 const decls_len = if (small.has_decls_len) decls_len: {
35653577 const decls_len = zir.extra[extra_index];
35663578 extra_index += 1;
src/print_zir.zig+14-37
......@@ -1405,12 +1405,6 @@ const Writer = struct {
14051405
14061406 var extra_index: usize = extra.end;
14071407
1408 const src_node: ?i32 = if (small.has_src_node) blk: {
1409 const src_node = @as(i32, @bitCast(self.code.extra[extra_index]));
1410 extra_index += 1;
1411 break :blk src_node;
1412 } else null;
1413
14141408 const fields_len = if (small.has_fields_len) blk: {
14151409 const fields_len = self.code.extra[extra_index];
14161410 extra_index += 1;
......@@ -1453,7 +1447,7 @@ const Writer = struct {
14531447 try stream.writeAll("{}, ");
14541448 } else {
14551449 const prev_parent_decl_node = self.parent_decl_node;
1456 if (src_node) |off| self.parent_decl_node = self.relativeToNodeIndex(off);
1450 self.parent_decl_node = self.relativeToNodeIndex(extra.data.src_node);
14571451 defer self.parent_decl_node = prev_parent_decl_node;
14581452
14591453 try stream.writeAll("{\n");
......@@ -1534,7 +1528,7 @@ const Writer = struct {
15341528 }
15351529
15361530 const prev_parent_decl_node = self.parent_decl_node;
1537 if (src_node) |off| self.parent_decl_node = self.relativeToNodeIndex(off);
1531 self.parent_decl_node = self.relativeToNodeIndex(extra.data.src_node);
15381532 try stream.writeAll("{\n");
15391533 self.indent += 2;
15401534
......@@ -1587,7 +1581,7 @@ const Writer = struct {
15871581 try stream.writeByteNTimes(' ', self.indent);
15881582 try stream.writeAll("})");
15891583 }
1590 try self.writeSrcNode(stream, src_node);
1584 try self.writeSrcNode(stream, extra.data.src_node);
15911585 }
15921586
15931587 fn writeUnionDecl(self: *Writer, stream: anytype, extended: Zir.Inst.Extended.InstData) !void {
......@@ -1605,12 +1599,6 @@ const Writer = struct {
16051599
16061600 var extra_index: usize = extra.end;
16071601
1608 const src_node: ?i32 = if (small.has_src_node) blk: {
1609 const src_node = @as(i32, @bitCast(self.code.extra[extra_index]));
1610 extra_index += 1;
1611 break :blk src_node;
1612 } else null;
1613
16141602 const tag_type_ref = if (small.has_tag_type) blk: {
16151603 const tag_type_ref = @as(Zir.Inst.Ref, @enumFromInt(self.code.extra[extra_index]));
16161604 extra_index += 1;
......@@ -1644,7 +1632,7 @@ const Writer = struct {
16441632 try stream.writeAll("{}");
16451633 } else {
16461634 const prev_parent_decl_node = self.parent_decl_node;
1647 if (src_node) |off| self.parent_decl_node = self.relativeToNodeIndex(off);
1635 self.parent_decl_node = self.relativeToNodeIndex(extra.data.src_node);
16481636 defer self.parent_decl_node = prev_parent_decl_node;
16491637
16501638 try stream.writeAll("{\n");
......@@ -1663,7 +1651,7 @@ const Writer = struct {
16631651
16641652 if (fields_len == 0) {
16651653 try stream.writeAll("})");
1666 try self.writeSrcNode(stream, src_node);
1654 try self.writeSrcNode(stream, extra.data.src_node);
16671655 return;
16681656 }
16691657 try stream.writeAll(", ");
......@@ -1672,7 +1660,7 @@ const Writer = struct {
16721660 extra_index += body.len;
16731661
16741662 const prev_parent_decl_node = self.parent_decl_node;
1675 if (src_node) |off| self.parent_decl_node = self.relativeToNodeIndex(off);
1663 self.parent_decl_node = self.relativeToNodeIndex(extra.data.src_node);
16761664 try self.writeBracedDecl(stream, body);
16771665 try stream.writeAll(", {\n");
16781666
......@@ -1740,7 +1728,7 @@ const Writer = struct {
17401728 self.indent -= 2;
17411729 try stream.writeByteNTimes(' ', self.indent);
17421730 try stream.writeAll("})");
1743 try self.writeSrcNode(stream, src_node);
1731 try self.writeSrcNode(stream, extra.data.src_node);
17441732 }
17451733
17461734 fn writeEnumDecl(self: *Writer, stream: anytype, extended: Zir.Inst.Extended.InstData) !void {
......@@ -1758,12 +1746,6 @@ const Writer = struct {
17581746
17591747 var extra_index: usize = extra.end;
17601748
1761 const src_node: ?i32 = if (small.has_src_node) blk: {
1762 const src_node = @as(i32, @bitCast(self.code.extra[extra_index]));
1763 extra_index += 1;
1764 break :blk src_node;
1765 } else null;
1766
17671749 const tag_type_ref = if (small.has_tag_type) blk: {
17681750 const tag_type_ref = @as(Zir.Inst.Ref, @enumFromInt(self.code.extra[extra_index]));
17691751 extra_index += 1;
......@@ -1795,7 +1777,7 @@ const Writer = struct {
17951777 try stream.writeAll("{}, ");
17961778 } else {
17971779 const prev_parent_decl_node = self.parent_decl_node;
1798 if (src_node) |off| self.parent_decl_node = self.relativeToNodeIndex(off);
1780 self.parent_decl_node = self.relativeToNodeIndex(extra.data.src_node);
17991781 defer self.parent_decl_node = prev_parent_decl_node;
18001782
18011783 try stream.writeAll("{\n");
......@@ -1816,7 +1798,7 @@ const Writer = struct {
18161798 extra_index += body.len;
18171799
18181800 const prev_parent_decl_node = self.parent_decl_node;
1819 if (src_node) |off| self.parent_decl_node = self.relativeToNodeIndex(off);
1801 self.parent_decl_node = self.relativeToNodeIndex(extra.data.src_node);
18201802 try self.writeBracedDecl(stream, body);
18211803 if (fields_len == 0) {
18221804 try stream.writeAll(", {})");
......@@ -1864,7 +1846,7 @@ const Writer = struct {
18641846 try stream.writeByteNTimes(' ', self.indent);
18651847 try stream.writeAll("})");
18661848 }
1867 try self.writeSrcNode(stream, src_node);
1849 try self.writeSrcNode(stream, extra.data.src_node);
18681850 }
18691851
18701852 fn writeOpaqueDecl(
......@@ -1873,13 +1855,8 @@ const Writer = struct {
18731855 extended: Zir.Inst.Extended.InstData,
18741856 ) !void {
18751857 const small = @as(Zir.Inst.OpaqueDecl.Small, @bitCast(extended.small));
1876 var extra_index: usize = extended.operand;
1877
1878 const src_node: ?i32 = if (small.has_src_node) blk: {
1879 const src_node = @as(i32, @bitCast(self.code.extra[extra_index]));
1880 extra_index += 1;
1881 break :blk src_node;
1882 } else null;
1858 const extra = self.code.extraData(Zir.Inst.OpaqueDecl, extended.operand);
1859 var extra_index: usize = extra.end;
18831860
18841861 const decls_len = if (small.has_decls_len) blk: {
18851862 const decls_len = self.code.extra[extra_index];
......@@ -1893,7 +1870,7 @@ const Writer = struct {
18931870 try stream.writeAll("{})");
18941871 } else {
18951872 const prev_parent_decl_node = self.parent_decl_node;
1896 if (src_node) |off| self.parent_decl_node = self.relativeToNodeIndex(off);
1873 self.parent_decl_node = self.relativeToNodeIndex(extra.data.src_node);
18971874 defer self.parent_decl_node = prev_parent_decl_node;
18981875
18991876 try stream.writeAll("{\n");
......@@ -1903,7 +1880,7 @@ const Writer = struct {
19031880 try stream.writeByteNTimes(' ', self.indent);
19041881 try stream.writeAll("})");
19051882 }
1906 try self.writeSrcNode(stream, src_node);
1883 try self.writeSrcNode(stream, extra.data.src_node);
19071884 }
19081885
19091886 fn writeErrorSetDecl(