authorgravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2024-03-08 11:05:35+00:00
committergravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2024-03-14 07:40:06+00:00
logd4ce1ec8dd2bb63bafda7e6fe23315d167492a1b
treef3365c9dbf569b12c61e1ad301621945db5d849c
parent7c4793f23c971f5305c63f1b343e7f8576f32781
signaturelock-open Commit is signed but in an unrecognized format.

Zcu: convert Decl.zir_inst_index to a TrackedInst.Index.Optional

This makes tracking easier across incremental updates: `scanDecl` can now tell whether an existing decl in a namespace was mapped to the one it's analyzing in the new ZIR.

1 files changed, 7 insertions(+), 12 deletions(-)

src/Module.zig+7-12
......@@ -362,7 +362,7 @@ pub const Decl = struct {
362362 src_line: u32,
363363 /// Index of the ZIR `declaration` instruction from which this `Decl` was created.
364364 /// For the root `Decl` of a `File` and legacy anonymous decls, this is `.none`.
365 zir_decl_index: Zir.Inst.OptionalIndex,
365 zir_decl_index: InternPool.TrackedInst.Index.Optional,
366366
367367 /// Represents the "shallow" analysis status. For example, for decls that are functions,
368368 /// the function type is analyzed with this set to `in_progress`, however, the semantic
......@@ -428,16 +428,9 @@ pub const Decl = struct {
428428 const Index = InternPool.DeclIndex;
429429 const OptionalIndex = InternPool.OptionalDeclIndex;
430430
431 /// Asserts that `zir_decl_index` is not `.none`.
432 fn getDeclaration(decl: Decl, zir: Zir) Zir.Inst.Declaration {
433 const zir_index = decl.zir_decl_index.unwrap().?;
434 const pl_node = zir.instructions.items(.data)[@intFromEnum(zir_index)].pl_node;
435 return zir.extraData(Zir.Inst.Declaration, pl_node.payload_index).data;
436 }
437
438431 pub fn zirBodies(decl: Decl, zcu: *Zcu) Zir.Inst.Declaration.Bodies {
439432 const zir = decl.getFileScope(zcu).zir;
440 const zir_index = decl.zir_decl_index.unwrap().?;
433 const zir_index = decl.zir_decl_index.unwrap().?.resolve(&zcu.intern_pool);
441434 const pl_node = zir.instructions.items(.data)[@intFromEnum(zir_index)].pl_node;
442435 const extra = zir.extraData(Zir.Inst.Declaration, pl_node.payload_index);
443436 return extra.data.getBodies(@intCast(extra.end), zir);
......@@ -3471,7 +3464,7 @@ fn semaDecl(mod: *Module, decl_index: Decl.Index) !SemaDeclResult {
34713464 @panic("TODO: update owner Decl");
34723465 }
34733466
3474 const decl_inst = decl.zir_decl_index.unwrap().?;
3467 const decl_inst = decl.zir_decl_index.unwrap().?.resolve(ip);
34753468
34763469 const gpa = mod.gpa;
34773470 const zir = decl.getFileScope(mod).zir;
......@@ -4231,6 +4224,8 @@ fn scanDecl(iter: *ScanDeclIter, decl_inst: Zir.Inst.Index) Allocator.Error!void
42314224
42324225 if (kind == .@"usingnamespace") try namespace.usingnamespace_set.ensureUnusedCapacity(gpa, 1);
42334226
4227 const tracked_inst = try ip.trackZir(gpa, iter.parent_decl.getFileScope(zcu), decl_inst);
4228
42344229 // We create a Decl for it regardless of analysis status.
42354230 const gop = try namespace.decls.getOrPutContextAdapted(
42364231 gpa,
......@@ -4277,7 +4272,7 @@ fn scanDecl(iter: *ScanDeclIter, decl_inst: Zir.Inst.Index) Allocator.Error!void
42774272 }
42784273 new_decl.is_pub = declaration.flags.is_pub;
42794274 new_decl.is_exported = declaration.flags.is_export;
4280 new_decl.zir_decl_index = decl_inst.toOptional();
4275 new_decl.zir_decl_index = tracked_inst.toOptional();
42814276 new_decl.alive = true; // This Decl corresponds to an AST node and therefore always alive.
42824277 return;
42834278 }
......@@ -4291,7 +4286,7 @@ fn scanDecl(iter: *ScanDeclIter, decl_inst: Zir.Inst.Index) Allocator.Error!void
42914286 decl.is_pub = declaration.flags.is_pub;
42924287 decl.is_exported = declaration.flags.is_export;
42934288 decl.kind = kind;
4294 decl.zir_decl_index = decl_inst.toOptional();
4289 decl.zir_decl_index = tracked_inst.toOptional();
42954290 if (decl.getOwnedFunction(zcu) != null) {
42964291 // TODO Look into detecting when this would be unnecessary by storing enough state
42974292 // in `Decl` to notice that the line number did not change.