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 {...@@ -362,7 +362,7 @@ pub const Decl = struct {
362 src_line: u32,362 src_line: u32,
363 /// Index of the ZIR `declaration` instruction from which this `Decl` was created.363 /// Index of the ZIR `declaration` instruction from which this `Decl` was created.
364 /// For the root `Decl` of a `File` and legacy anonymous decls, this is `.none`.364 /// 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
367 /// Represents the "shallow" analysis status. For example, for decls that are functions,367 /// Represents the "shallow" analysis status. For example, for decls that are functions,
368 /// the function type is analyzed with this set to `in_progress`, however, the semantic368 /// the function type is analyzed with this set to `in_progress`, however, the semantic
...@@ -428,16 +428,9 @@ pub const Decl = struct {...@@ -428,16 +428,9 @@ pub const Decl = struct {
428 const Index = InternPool.DeclIndex;428 const Index = InternPool.DeclIndex;
429 const OptionalIndex = InternPool.OptionalDeclIndex;429 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
438 pub fn zirBodies(decl: Decl, zcu: *Zcu) Zir.Inst.Declaration.Bodies {431 pub fn zirBodies(decl: Decl, zcu: *Zcu) Zir.Inst.Declaration.Bodies {
439 const zir = decl.getFileScope(zcu).zir;432 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);
441 const pl_node = zir.instructions.items(.data)[@intFromEnum(zir_index)].pl_node;434 const pl_node = zir.instructions.items(.data)[@intFromEnum(zir_index)].pl_node;
442 const extra = zir.extraData(Zir.Inst.Declaration, pl_node.payload_index);435 const extra = zir.extraData(Zir.Inst.Declaration, pl_node.payload_index);
443 return extra.data.getBodies(@intCast(extra.end), zir);436 return extra.data.getBodies(@intCast(extra.end), zir);
...@@ -3471,7 +3464,7 @@ fn semaDecl(mod: *Module, decl_index: Decl.Index) !SemaDeclResult {...@@ -3471,7 +3464,7 @@ fn semaDecl(mod: *Module, decl_index: Decl.Index) !SemaDeclResult {
3471 @panic("TODO: update owner Decl");3464 @panic("TODO: update owner Decl");
3472 }3465 }
34733466
3474 const decl_inst = decl.zir_decl_index.unwrap().?;3467 const decl_inst = decl.zir_decl_index.unwrap().?.resolve(ip);
34753468
3476 const gpa = mod.gpa;3469 const gpa = mod.gpa;
3477 const zir = decl.getFileScope(mod).zir;3470 const zir = decl.getFileScope(mod).zir;
...@@ -4231,6 +4224,8 @@ fn scanDecl(iter: *ScanDeclIter, decl_inst: Zir.Inst.Index) Allocator.Error!void...@@ -4231,6 +4224,8 @@ fn scanDecl(iter: *ScanDeclIter, decl_inst: Zir.Inst.Index) Allocator.Error!void
42314224
4232 if (kind == .@"usingnamespace") try namespace.usingnamespace_set.ensureUnusedCapacity(gpa, 1);4225 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
4234 // We create a Decl for it regardless of analysis status.4229 // We create a Decl for it regardless of analysis status.
4235 const gop = try namespace.decls.getOrPutContextAdapted(4230 const gop = try namespace.decls.getOrPutContextAdapted(
4236 gpa,4231 gpa,
...@@ -4277,7 +4272,7 @@ fn scanDecl(iter: *ScanDeclIter, decl_inst: Zir.Inst.Index) Allocator.Error!void...@@ -4277,7 +4272,7 @@ fn scanDecl(iter: *ScanDeclIter, decl_inst: Zir.Inst.Index) Allocator.Error!void
4277 }4272 }
4278 new_decl.is_pub = declaration.flags.is_pub;4273 new_decl.is_pub = declaration.flags.is_pub;
4279 new_decl.is_exported = declaration.flags.is_export;4274 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();
4281 new_decl.alive = true; // This Decl corresponds to an AST node and therefore always alive.4276 new_decl.alive = true; // This Decl corresponds to an AST node and therefore always alive.
4282 return;4277 return;
4283 }4278 }
...@@ -4291,7 +4286,7 @@ fn scanDecl(iter: *ScanDeclIter, decl_inst: Zir.Inst.Index) Allocator.Error!void...@@ -4291,7 +4286,7 @@ fn scanDecl(iter: *ScanDeclIter, decl_inst: Zir.Inst.Index) Allocator.Error!void
4291 decl.is_pub = declaration.flags.is_pub;4286 decl.is_pub = declaration.flags.is_pub;
4292 decl.is_exported = declaration.flags.is_export;4287 decl.is_exported = declaration.flags.is_export;
4293 decl.kind = kind;4288 decl.kind = kind;
4294 decl.zir_decl_index = decl_inst.toOptional();4289 decl.zir_decl_index = tracked_inst.toOptional();
4295 if (decl.getOwnedFunction(zcu) != null) {4290 if (decl.getOwnedFunction(zcu) != null) {
4296 // TODO Look into detecting when this would be unnecessary by storing enough state4291 // TODO Look into detecting when this would be unnecessary by storing enough state
4297 // in `Decl` to notice that the line number did not change.4292 // in `Decl` to notice that the line number did not change.