authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-10-27 19:22:40-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-10-28 04:30:27-04:00
log256ab68a97cb6a84278c78d93917ab5e8ae53209
treeb5838383e70b6c4c0e12a053def4f2af022ec68c
parent9ff9ea38ea2a7dc776bb2954d31109ec1c81d191

frontend: make Decl.zir_decl_index typed

This field had the wrong type. It's not a `Zir.Inst.Index`, it's actually a `Zir.OptionalExtraIndex`. Also, the former is currently aliased to `u32` while the latter is a nonexhaustive enum that gives us more type checking. This commit is preparation for making this field non-optional. Now it can be changed to `Zir.ExtraIndex` and then the compiler will point out all the places that the non-optional assumption is being violated.

5 files changed, 56 insertions(+), 42 deletions(-)

src/Autodoc.zig+9-9
......@@ -3925,7 +3925,7 @@ fn analyzeAllDecls(
39253925 {
39263926 var it = original_it;
39273927 while (it.next()) |d| {
3928 const decl_name_index = file.zir.extra[d.sub_index + 5];
3928 const decl_name_index = file.zir.extra[@intFromEnum(d.sub_index) + 5];
39293929 switch (decl_name_index) {
39303930 0, 1, 2 => continue,
39313931 else => if (file.zir.string_bytes[decl_name_index] == 0) {
......@@ -3942,7 +3942,7 @@ fn analyzeAllDecls(
39423942 var it = original_it;
39433943 var decl_indexes_slot = first_decl_indexes_slot;
39443944 while (it.next()) |d| : (decl_indexes_slot += 1) {
3945 const decl_name_index = file.zir.extra[d.sub_index + 5];
3945 const decl_name_index = file.zir.extra[@intFromEnum(d.sub_index) + 5];
39463946 switch (decl_name_index) {
39473947 0 => {
39483948 const is_exported = @as(u1, @truncate(d.flags >> 1));
......@@ -3969,7 +3969,7 @@ fn analyzeAllDecls(
39693969 // Third loop to analyze all remaining decls
39703970 var it = original_it;
39713971 while (it.next()) |d| {
3972 const decl_name_index = file.zir.extra[d.sub_index + 5];
3972 const decl_name_index = file.zir.extra[@intFromEnum(d.sub_index) + 5];
39733973 switch (decl_name_index) {
39743974 0, 1 => continue, // skip over usingnamespace decls
39753975 2 => continue, // skip decltests
......@@ -3993,7 +3993,7 @@ fn analyzeAllDecls(
39933993 // Fourth loop to analyze decltests
39943994 it = original_it;
39953995 while (it.next()) |d| {
3996 const decl_name_index = file.zir.extra[d.sub_index + 5];
3996 const decl_name_index = file.zir.extra[@intFromEnum(d.sub_index) + 5];
39973997 switch (decl_name_index) {
39983998 0, 1 => continue, // skip over usingnamespace decls
39993999 2 => {},
......@@ -4028,7 +4028,7 @@ fn analyzeDecl(
40284028 const has_align = @as(u1, @truncate(d.flags >> 2)) != 0;
40294029 const has_section_or_addrspace = @as(u1, @truncate(d.flags >> 3)) != 0;
40304030
4031 var extra_index = d.sub_index;
4031 var extra_index = @intFromEnum(d.sub_index);
40324032 // const hash_u32s = file.zir.extra[extra_index..][0..4];
40334033
40344034 extra_index += 4;
......@@ -4158,8 +4158,8 @@ fn analyzeUsingnamespaceDecl(
41584158 const data = file.zir.instructions.items(.data);
41594159
41604160 const is_pub = @as(u1, @truncate(d.flags)) != 0;
4161 const value_index = file.zir.extra[d.sub_index + 6];
4162 const doc_comment_index = file.zir.extra[d.sub_index + 7];
4161 const value_index = file.zir.extra[@intFromEnum(d.sub_index) + 6];
4162 const doc_comment_index = file.zir.extra[@intFromEnum(d.sub_index) + 7];
41634163
41644164 // This is known to work because decl values are always block_inlines
41654165 const value_pl_node = data[value_index].pl_node;
......@@ -4218,8 +4218,8 @@ fn analyzeDecltest(
42184218) AutodocErrors!void {
42194219 const data = file.zir.instructions.items(.data);
42204220
4221 const value_index = file.zir.extra[d.sub_index + 6];
4222 const decl_name_index = file.zir.extra[d.sub_index + 7];
4221 const value_index = file.zir.extra[@intFromEnum(d.sub_index) + 6];
4222 const decl_name_index = file.zir.extra[@intFromEnum(d.sub_index) + 7];
42234223
42244224 const value_pl_node = data[value_index].pl_node;
42254225 const decl_src = try self.srcLocInfo(file, value_pl_node.src_node, parent_src);
src/Compilation.zig+1-1
......@@ -2267,7 +2267,7 @@ pub fn update(comp: *Compilation, main_progress_node: *std.Progress.Node) !void
22672267 const decl = module.declPtr(decl_index);
22682268 assert(decl.deletion_flag);
22692269 assert(decl.dependants.count() == 0);
2270 assert(decl.zir_decl_index != 0);
2270 assert(decl.zir_decl_index != .none);
22712271
22722272 try module.clearDecl(decl_index, null);
22732273 }
src/Module.zig+26-27
......@@ -390,7 +390,7 @@ pub const Decl = struct {
390390 /// (the part that says "for every decls_len"). The first item at this index is
391391 /// the contents hash, followed by line, name, etc.
392392 /// For anonymous decls and also the root Decl for a File, this is 0.
393 zir_decl_index: Zir.Inst.Index,
393 zir_decl_index: Zir.OptionalExtraIndex,
394394
395395 /// Represents the "shallow" analysis status. For example, for decls that are functions,
396396 /// the function type is analyzed with this set to `in_progress`, however, the semantic
......@@ -526,8 +526,8 @@ pub const Decl = struct {
526526 }
527527
528528 pub fn getNameZir(decl: Decl, zir: Zir) ?[:0]const u8 {
529 assert(decl.zir_decl_index != 0);
530 const name_index = zir.extra[decl.zir_decl_index + 5];
529 assert(decl.zir_decl_index != .none);
530 const name_index = zir.extra[@intFromEnum(decl.zir_decl_index) + 5];
531531 if (name_index <= 1) return null;
532532 return zir.nullTerminatedString(name_index);
533533 }
......@@ -538,39 +538,39 @@ pub const Decl = struct {
538538 }
539539
540540 pub fn contentsHashZir(decl: Decl, zir: Zir) std.zig.SrcHash {
541 assert(decl.zir_decl_index != 0);
542 const hash_u32s = zir.extra[decl.zir_decl_index..][0..4];
541 assert(decl.zir_decl_index != .none);
542 const hash_u32s = zir.extra[@intFromEnum(decl.zir_decl_index)..][0..4];
543543 const contents_hash = @as(std.zig.SrcHash, @bitCast(hash_u32s.*));
544544 return contents_hash;
545545 }
546546
547547 pub fn zirBlockIndex(decl: *const Decl, mod: *Module) Zir.Inst.Index {
548 assert(decl.zir_decl_index != 0);
548 assert(decl.zir_decl_index != .none);
549549 const zir = decl.getFileScope(mod).zir;
550 return zir.extra[decl.zir_decl_index + 6];
550 return zir.extra[@intFromEnum(decl.zir_decl_index) + 6];
551551 }
552552
553553 pub fn zirAlignRef(decl: Decl, mod: *Module) Zir.Inst.Ref {
554554 if (!decl.has_align) return .none;
555 assert(decl.zir_decl_index != 0);
555 assert(decl.zir_decl_index != .none);
556556 const zir = decl.getFileScope(mod).zir;
557 return @as(Zir.Inst.Ref, @enumFromInt(zir.extra[decl.zir_decl_index + 8]));
557 return @enumFromInt(zir.extra[@intFromEnum(decl.zir_decl_index) + 8]);
558558 }
559559
560560 pub fn zirLinksectionRef(decl: Decl, mod: *Module) Zir.Inst.Ref {
561561 if (!decl.has_linksection_or_addrspace) return .none;
562 assert(decl.zir_decl_index != 0);
562 assert(decl.zir_decl_index != .none);
563563 const zir = decl.getFileScope(mod).zir;
564 const extra_index = decl.zir_decl_index + 8 + @intFromBool(decl.has_align);
565 return @as(Zir.Inst.Ref, @enumFromInt(zir.extra[extra_index]));
564 const extra_index = @intFromEnum(decl.zir_decl_index) + 8 + @intFromBool(decl.has_align);
565 return @enumFromInt(zir.extra[extra_index]);
566566 }
567567
568568 pub fn zirAddrspaceRef(decl: Decl, mod: *Module) Zir.Inst.Ref {
569569 if (!decl.has_linksection_or_addrspace) return .none;
570 assert(decl.zir_decl_index != 0);
570 assert(decl.zir_decl_index != .none);
571571 const zir = decl.getFileScope(mod).zir;
572 const extra_index = decl.zir_decl_index + 8 + @intFromBool(decl.has_align) + 1;
573 return @as(Zir.Inst.Ref, @enumFromInt(zir.extra[extra_index]));
572 const extra_index = @intFromEnum(decl.zir_decl_index) + 8 + @intFromBool(decl.has_align) + 1;
573 return @enumFromInt(zir.extra[extra_index]);
574574 }
575575
576576 pub fn relativeToLine(decl: Decl, offset: u32) u32 {
......@@ -578,7 +578,7 @@ pub const Decl = struct {
578578 }
579579
580580 pub fn relativeToNodeIndex(decl: Decl, offset: i32) Ast.Node.Index {
581 return @as(Ast.Node.Index, @bitCast(offset + @as(i32, @bitCast(decl.src_node))));
581 return @bitCast(offset + @as(i32, @bitCast(decl.src_node)));
582582 }
583583
584584 pub fn nodeIndexToRelative(decl: Decl, node_index: Ast.Node.Index) i32 {
......@@ -3051,7 +3051,7 @@ fn updateZirRefs(mod: *Module, file: *File, old_zir: Zir) !void {
30513051 defer inst_map.deinit(gpa);
30523052 // Maps from old ZIR to new ZIR, the extra data index for the sub-decl item.
30533053 // e.g. the thing that Decl.zir_decl_index points to.
3054 var extra_map: std.AutoHashMapUnmanaged(u32, u32) = .{};
3054 var extra_map: std.AutoHashMapUnmanaged(Zir.ExtraIndex, Zir.ExtraIndex) = .{};
30553055 defer extra_map.deinit(gpa);
30563056
30573057 try mapOldZirToNew(gpa, old_zir, new_zir, &inst_map, &extra_map);
......@@ -3079,14 +3079,13 @@ fn updateZirRefs(mod: *Module, file: *File, old_zir: Zir) !void {
30793079 // to walk them but we do not need to modify this value.
30803080 // Anonymous decls should not be marked outdated. They will be re-generated
30813081 // if their owner decl is marked outdated.
3082 if (decl.zir_decl_index != 0) {
3083 const old_zir_decl_index = decl.zir_decl_index;
3082 if (decl.zir_decl_index.unwrap()) |old_zir_decl_index| {
30843083 const new_zir_decl_index = extra_map.get(old_zir_decl_index) orelse {
30853084 try file.deleted_decls.append(gpa, decl_index);
30863085 continue;
30873086 };
30883087 const old_hash = decl.contentsHashZir(old_zir);
3089 decl.zir_decl_index = new_zir_decl_index;
3088 decl.zir_decl_index = new_zir_decl_index.toOptional();
30903089 const new_hash = decl.contentsHashZir(new_zir);
30913090 if (!std.zig.srcHashEql(old_hash, new_hash)) {
30923091 try file.outdated_decls.append(gpa, decl_index);
......@@ -3195,7 +3194,7 @@ pub fn mapOldZirToNew(
31953194 old_zir: Zir,
31963195 new_zir: Zir,
31973196 inst_map: *std.AutoHashMapUnmanaged(Zir.Inst.Index, Zir.Inst.Index),
3198 extra_map: *std.AutoHashMapUnmanaged(u32, u32),
3197 extra_map: *std.AutoHashMapUnmanaged(Zir.ExtraIndex, Zir.ExtraIndex),
31993198) Allocator.Error!void {
32003199 // Contain ZIR indexes of declaration instructions.
32013200 const MatchedZirDecl = struct {
......@@ -3220,7 +3219,7 @@ pub fn mapOldZirToNew(
32203219 try inst_map.put(gpa, match_item.old_inst, match_item.new_inst);
32213220
32223221 // Maps name to extra index of decl sub item.
3223 var decl_map: std.StringHashMapUnmanaged(u32) = .{};
3222 var decl_map: std.StringHashMapUnmanaged(Zir.ExtraIndex) = .{};
32243223 defer decl_map.deinit(gpa);
32253224
32263225 {
......@@ -3301,7 +3300,7 @@ pub fn ensureDeclAnalyzed(mod: *Module, decl_index: Decl.Index) SemaError!void {
33013300 defer decl_prog_node.end();
33023301
33033302 const type_changed = blk: {
3304 if (decl.zir_decl_index == 0 and !mod.declIsRoot(decl_index)) {
3303 if (decl.zir_decl_index == .none and !mod.declIsRoot(decl_index)) {
33053304 // Anonymous decl. We don't semantically analyze these.
33063305 break :blk false; // tv unchanged
33073306 }
......@@ -4451,7 +4450,7 @@ fn scanDecl(iter: *ScanDeclIter, decl_sub_index: usize, flags: u4) Allocator.Err
44514450 new_decl.is_exported = is_exported;
44524451 new_decl.has_align = has_align;
44534452 new_decl.has_linksection_or_addrspace = has_linksection_or_addrspace;
4454 new_decl.zir_decl_index = @as(u32, @intCast(decl_sub_index));
4453 new_decl.zir_decl_index = @enumFromInt(decl_sub_index);
44554454 new_decl.alive = true; // This Decl corresponds to an AST node and therefore always alive.
44564455 return;
44574456 }
......@@ -4485,7 +4484,7 @@ fn scanDecl(iter: *ScanDeclIter, decl_sub_index: usize, flags: u4) Allocator.Err
44854484 decl.kind = kind;
44864485 decl.has_align = has_align;
44874486 decl.has_linksection_or_addrspace = has_linksection_or_addrspace;
4488 decl.zir_decl_index = @as(u32, @intCast(decl_sub_index));
4487 decl.zir_decl_index = @enumFromInt(decl_sub_index);
44894488 if (decl.getOwnedFunction(mod) != null) {
44904489 switch (comp.bin_file.tag) {
44914490 .coff, .elf, .macho, .plan9 => {
......@@ -4982,7 +4981,7 @@ pub fn allocateNewDecl(
49824981 .@"addrspace" = .generic,
49834982 .analysis = .unreferenced,
49844983 .deletion_flag = false,
4985 .zir_decl_index = 0,
4984 .zir_decl_index = .none,
49864985 .src_scope = src_scope,
49874986 .generation = 0,
49884987 .is_pub = false,
......@@ -5507,7 +5506,7 @@ pub fn processOutdatedAndDeletedDecls(mod: *Module) !void {
55075506 const decl = mod.declPtr(decl_index);
55085507
55095508 // Remove from the namespace it resides in, preserving declaration order.
5510 assert(decl.zir_decl_index != 0);
5509 assert(decl.zir_decl_index != .none);
55115510 _ = mod.namespacePtr(decl.src_namespace).decls.orderedRemoveAdapted(
55125511 decl.name,
55135512 DeclAdapter{ .mod = mod },
src/Zir.zig+19-4
......@@ -63,6 +63,21 @@ pub const ExtraIndex = enum(u32) {
6363 imports,
6464
6565 _,
66
67 pub fn toOptional(i: ExtraIndex) OptionalExtraIndex {
68 return @enumFromInt(@intFromEnum(i));
69 }
70};
71
72pub const OptionalExtraIndex = enum(u32) {
73 compile_errors,
74 imports,
75 none = std.math.maxInt(u32),
76 _,
77
78 pub fn unwrap(oi: OptionalExtraIndex) ?ExtraIndex {
79 return if (oi == .none) null else @enumFromInt(@intFromEnum(oi));
80 }
6681};
6782
6883fn ExtraData(comptime T: type) type {
......@@ -3325,7 +3340,7 @@ pub const DeclIterator = struct {
33253340
33263341 pub const Item = struct {
33273342 name: [:0]const u8,
3328 sub_index: u32,
3343 sub_index: ExtraIndex,
33293344 flags: u4,
33303345 };
33313346
......@@ -3341,7 +3356,7 @@ pub const DeclIterator = struct {
33413356 const flags: u4 = @truncate(it.cur_bit_bag);
33423357 it.cur_bit_bag >>= 4;
33433358
3344 const sub_index: u32 = @intCast(it.extra_index);
3359 const sub_index: ExtraIndex = @enumFromInt(it.extra_index);
33453360 it.extra_index += 5; // src_hash(4) + line(1)
33463361 const name = it.zir.nullTerminatedString(it.zir.extra[it.extra_index]);
33473362 it.extra_index += 3; // name(1) + value(1) + doc_comment(1)
......@@ -3453,8 +3468,8 @@ pub fn declIteratorInner(zir: Zir, extra_index: usize, decls_len: u32) DeclItera
34533468
34543469/// The iterator would have to allocate memory anyway to iterate. So here we populate
34553470/// an ArrayList as the result.
3456pub fn findDecls(zir: Zir, list: *std.ArrayList(Inst.Index), decl_sub_index: u32) !void {
3457 const block_inst = zir.extra[decl_sub_index + 6];
3471pub fn findDecls(zir: Zir, list: *std.ArrayList(Inst.Index), decl_sub_index: ExtraIndex) !void {
3472 const block_inst = zir.extra[@intFromEnum(decl_sub_index) + 6];
34583473 list.clearRetainingCapacity();
34593474
34603475 return zir.findDeclsInner(list, block_inst);
src/main.zig+1-1
......@@ -6632,7 +6632,7 @@ pub fn cmdChangelist(
66326632 var inst_map: std.AutoHashMapUnmanaged(Zir.Inst.Index, Zir.Inst.Index) = .{};
66336633 defer inst_map.deinit(gpa);
66346634
6635 var extra_map: std.AutoHashMapUnmanaged(u32, u32) = .{};
6635 var extra_map: std.AutoHashMapUnmanaged(Zir.ExtraIndex, Zir.ExtraIndex) = .{};
66366636 defer extra_map.deinit(gpa);
66376637
66386638 try Module.mapOldZirToNew(gpa, old_zir, file.zir, &inst_map, &extra_map);