authorgravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2024-12-17 22:52:30+00:00
committergravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2024-12-17 22:52:30+00:00
log98f63990d55c27969ebae3c0fe392c6716548781
tree99b60573b0d8ab3ed72a6faf541159b360d576e9
parent737154fcd80e31ba7d1035384b8178e39734379b
signaturelock-open Commit is signed but in an unrecognized format.

Zir: store declaration column number so Dwarf doesn't need to load the AST

Resolves: #21227

3 files changed, 60 insertions(+), 62 deletions(-)

lib/std/zig/AstGen.zig+18
......@@ -4151,6 +4151,8 @@ fn fnDecl(
41514151 };
41524152 defer fn_gz.unstack();
41534153
4154 const decl_column = astgen.source_column;
4155
41544156 // Set this now, since parameter types, return type, etc may be generic.
41554157 const prev_within_fn = astgen.within_fn;
41564158 defer astgen.within_fn = prev_within_fn;
......@@ -4523,6 +4525,7 @@ fn fnDecl(
45234525 hash,
45244526 .{ .named = fn_name_token },
45254527 decl_gz.decl_line,
4528 decl_column,
45264529 is_pub,
45274530 is_export,
45284531 &decl_gz,
......@@ -4568,6 +4571,8 @@ fn globalVarDecl(
45684571 };
45694572 defer block_scope.unstack();
45704573
4574 const decl_column = astgen.source_column;
4575
45714576 const is_pub = var_decl.visib_token != null;
45724577 const is_export = blk: {
45734578 const maybe_export_token = var_decl.extern_export_token orelse break :blk false;
......@@ -4693,6 +4698,7 @@ fn globalVarDecl(
46934698 hash,
46944699 .{ .named = name_token },
46954700 block_scope.decl_line,
4701 decl_column,
46964702 is_pub,
46974703 is_export,
46984704 &block_scope,
......@@ -4738,6 +4744,8 @@ fn comptimeDecl(
47384744 };
47394745 defer decl_block.unstack();
47404746
4747 const decl_column = astgen.source_column;
4748
47414749 const block_result = try fullBodyExpr(&decl_block, &decl_block.base, .{ .rl = .none }, body_node, .normal);
47424750 if (decl_block.isEmpty() or !decl_block.refIsNoReturn(block_result)) {
47434751 _ = try decl_block.addBreak(.break_inline, decl_inst, .void_value);
......@@ -4750,6 +4758,7 @@ fn comptimeDecl(
47504758 hash,
47514759 .@"comptime",
47524760 decl_block.decl_line,
4761 decl_column,
47534762 false,
47544763 false,
47554764 &decl_block,
......@@ -4797,6 +4806,8 @@ fn usingnamespaceDecl(
47974806 };
47984807 defer decl_block.unstack();
47994808
4809 const decl_column = astgen.source_column;
4810
48004811 const namespace_inst = try typeExpr(&decl_block, &decl_block.base, type_expr);
48014812 _ = try decl_block.addBreak(.break_inline, decl_inst, namespace_inst);
48024813
......@@ -4807,6 +4818,7 @@ fn usingnamespaceDecl(
48074818 hash,
48084819 .@"usingnamespace",
48094820 decl_block.decl_line,
4821 decl_column,
48104822 is_pub,
48114823 false,
48124824 &decl_block,
......@@ -4849,6 +4861,8 @@ fn testDecl(
48494861 };
48504862 defer decl_block.unstack();
48514863
4864 const decl_column = astgen.source_column;
4865
48524866 const main_tokens = tree.nodes.items(.main_token);
48534867 const token_tags = tree.tokens.items(.tag);
48544868 const test_token = main_tokens[node];
......@@ -5013,6 +5027,7 @@ fn testDecl(
50135027 hash,
50145028 test_name,
50155029 decl_block.decl_line,
5030 decl_column,
50165031 false,
50175032 false,
50185033 &decl_block,
......@@ -14013,6 +14028,7 @@ fn addFailedDeclaration(
1401314028 @splat(0), // use a fixed hash to represent an AstGen failure; we don't care about source changes if AstGen still failed!
1401414029 name,
1401514030 gz.astgen.source_line,
14031 gz.astgen.source_column,
1401614032 is_pub,
1401714033 false, // we don't care about exports since semantic analysis will fail
1401814034 &decl_gz,
......@@ -14027,6 +14043,7 @@ fn setDeclaration(
1402714043 src_hash: std.zig.SrcHash,
1402814044 name: DeclarationName,
1402914045 src_line: u32,
14046 src_column: u32,
1403014047 is_pub: bool,
1403114048 is_export: bool,
1403214049 value_gz: *GenZir,
......@@ -14079,6 +14096,7 @@ fn setDeclaration(
1407914096 .@"usingnamespace" => .@"usingnamespace",
1408014097 },
1408114098 .src_line = src_line,
14099 .src_column = src_column,
1408214100 .flags = .{
1408314101 .value_body_len = @intCast(value_len),
1408414102 .is_pub = is_pub,
lib/std/zig/Zir.zig+1
......@@ -2636,6 +2636,7 @@ pub const Inst = struct {
26362636 /// The name of this `Decl`. Also indicates whether it is a test, comptime block, etc.
26372637 name: Name,
26382638 src_line: u32,
2639 src_column: u32,
26392640 flags: Flags,
26402641
26412642 pub const Flags = packed struct(u32) {
src/link/Dwarf.zig+41-62
......@@ -2259,25 +2259,20 @@ pub fn initWipNav(dwarf: *Dwarf, pt: Zcu.PerThread, nav_index: InternPool.Nav.In
22592259 switch (ip.indexToKey(nav_val.toIntern())) {
22602260 else => {
22612261 assert(file.zir_loaded);
2262 const decl_inst = file.zir.instructions.get(@intFromEnum(inst_info.inst));
2263 assert(decl_inst.tag == .declaration);
2264 const tree = try file.getTree(dwarf.gpa);
2265 const loc = tree.tokenLocation(0, tree.nodes.items(.main_token)[decl_inst.data.declaration.src_node]);
2266 assert(loc.line == zcu.navSrcLine(nav_index));
2262 const decl = file.zir.getDeclaration(inst_info.inst)[0];
22672263
22682264 const parent_type, const accessibility: u8 = if (nav.analysis_owner.unwrap()) |cau| parent: {
2269 const decl_extra = file.zir.extraData(Zir.Inst.Declaration, decl_inst.data.declaration.payload_index).data;
22702265 const parent_namespace_ptr = ip.namespacePtr(ip.getCau(cau).namespace);
22712266 break :parent .{
22722267 parent_namespace_ptr.owner_type,
2273 switch (decl_extra.name) {
2268 switch (decl.name) {
22742269 .@"comptime",
22752270 .@"usingnamespace",
22762271 .unnamed_test,
22772272 => DW.ACCESS.private,
2278 _ => if (decl_extra.name.isNamedTest(file.zir))
2273 _ => if (decl.name.isNamedTest(file.zir))
22792274 DW.ACCESS.private
2280 else if (decl_extra.flags.is_pub)
2275 else if (decl.flags.is_pub)
22812276 DW.ACCESS.public
22822277 else
22832278 DW.ACCESS.private,
......@@ -2289,8 +2284,8 @@ pub fn initWipNav(dwarf: *Dwarf, pt: Zcu.PerThread, nav_index: InternPool.Nav.In
22892284 try wip_nav.abbrevCode(.decl_var);
22902285 try wip_nav.refType(.fromInterned(parent_type));
22912286 assert(wip_nav.debug_info.items.len == DebugInfo.declEntryLineOff(dwarf));
2292 try diw.writeInt(u32, @intCast(loc.line + 1), dwarf.endian);
2293 try uleb128(diw, loc.column + 1);
2287 try diw.writeInt(u32, @intCast(decl.src_line + 1), dwarf.endian);
2288 try uleb128(diw, decl.src_column + 1);
22942289 try diw.writeByte(accessibility);
22952290 try wip_nav.strp(nav.name.toSlice(ip));
22962291 try wip_nav.strp(nav.fqn.toSlice(ip));
......@@ -2306,25 +2301,20 @@ pub fn initWipNav(dwarf: *Dwarf, pt: Zcu.PerThread, nav_index: InternPool.Nav.In
23062301 },
23072302 .variable => |variable| {
23082303 assert(file.zir_loaded);
2309 const decl_inst = file.zir.instructions.get(@intFromEnum(inst_info.inst));
2310 assert(decl_inst.tag == .declaration);
2311 const tree = try file.getTree(dwarf.gpa);
2312 const loc = tree.tokenLocation(0, tree.nodes.items(.main_token)[decl_inst.data.declaration.src_node]);
2313 assert(loc.line == zcu.navSrcLine(nav_index));
2304 const decl = file.zir.getDeclaration(inst_info.inst)[0];
23142305
23152306 const parent_type, const accessibility: u8 = if (nav.analysis_owner.unwrap()) |cau| parent: {
2316 const decl_extra = file.zir.extraData(Zir.Inst.Declaration, decl_inst.data.declaration.payload_index).data;
23172307 const parent_namespace_ptr = ip.namespacePtr(ip.getCau(cau).namespace);
23182308 break :parent .{
23192309 parent_namespace_ptr.owner_type,
2320 switch (decl_extra.name) {
2310 switch (decl.name) {
23212311 .@"comptime",
23222312 .@"usingnamespace",
23232313 .unnamed_test,
23242314 => DW.ACCESS.private,
2325 _ => if (decl_extra.name.isNamedTest(file.zir))
2315 _ => if (decl.name.isNamedTest(file.zir))
23262316 DW.ACCESS.private
2327 else if (decl_extra.flags.is_pub)
2317 else if (decl.flags.is_pub)
23282318 DW.ACCESS.public
23292319 else
23302320 DW.ACCESS.private,
......@@ -2336,8 +2326,8 @@ pub fn initWipNav(dwarf: *Dwarf, pt: Zcu.PerThread, nav_index: InternPool.Nav.In
23362326 try wip_nav.abbrevCode(.decl_var);
23372327 try wip_nav.refType(.fromInterned(parent_type));
23382328 assert(wip_nav.debug_info.items.len == DebugInfo.declEntryLineOff(dwarf));
2339 try diw.writeInt(u32, @intCast(loc.line + 1), dwarf.endian);
2340 try uleb128(diw, loc.column + 1);
2329 try diw.writeInt(u32, @intCast(decl.src_line + 1), dwarf.endian);
2330 try uleb128(diw, decl.src_column + 1);
23412331 try diw.writeByte(accessibility);
23422332 try wip_nav.strp(nav.name.toSlice(ip));
23432333 try wip_nav.strp(nav.fqn.toSlice(ip));
......@@ -2351,25 +2341,20 @@ pub fn initWipNav(dwarf: *Dwarf, pt: Zcu.PerThread, nav_index: InternPool.Nav.In
23512341 },
23522342 .func => |func| {
23532343 assert(file.zir_loaded);
2354 const decl_inst = file.zir.instructions.get(@intFromEnum(inst_info.inst));
2355 assert(decl_inst.tag == .declaration);
2356 const tree = try file.getTree(dwarf.gpa);
2357 const loc = tree.tokenLocation(0, tree.nodes.items(.main_token)[decl_inst.data.declaration.src_node]);
2358 assert(loc.line == zcu.navSrcLine(nav_index));
2344 const decl = file.zir.getDeclaration(inst_info.inst)[0];
23592345
23602346 const parent_type, const accessibility: u8 = if (nav.analysis_owner.unwrap()) |cau| parent: {
2361 const decl_extra = file.zir.extraData(Zir.Inst.Declaration, decl_inst.data.declaration.payload_index).data;
23622347 const parent_namespace_ptr = ip.namespacePtr(ip.getCau(cau).namespace);
23632348 break :parent .{
23642349 parent_namespace_ptr.owner_type,
2365 switch (decl_extra.name) {
2350 switch (decl.name) {
23662351 .@"comptime",
23672352 .@"usingnamespace",
23682353 .unnamed_test,
23692354 => DW.ACCESS.private,
2370 _ => if (decl_extra.name.isNamedTest(file.zir))
2355 _ => if (decl.name.isNamedTest(file.zir))
23712356 DW.ACCESS.private
2372 else if (decl_extra.flags.is_pub)
2357 else if (decl.flags.is_pub)
23732358 DW.ACCESS.public
23742359 else
23752360 DW.ACCESS.private,
......@@ -2426,8 +2411,8 @@ pub fn initWipNav(dwarf: *Dwarf, pt: Zcu.PerThread, nav_index: InternPool.Nav.In
24262411 try wip_nav.abbrevCode(.decl_func);
24272412 try wip_nav.refType(.fromInterned(parent_type));
24282413 assert(wip_nav.debug_info.items.len == DebugInfo.declEntryLineOff(dwarf));
2429 try diw.writeInt(u32, @intCast(loc.line + 1), dwarf.endian);
2430 try uleb128(diw, loc.column + 1);
2414 try diw.writeInt(u32, @intCast(decl.src_line + 1), dwarf.endian);
2415 try uleb128(diw, decl.src_column + 1);
24312416 try diw.writeByte(accessibility);
24322417 try wip_nav.strp(nav.name.toSlice(ip));
24332418 try wip_nav.strp(nav.fqn.toSlice(ip));
......@@ -2476,7 +2461,7 @@ pub fn initWipNav(dwarf: *Dwarf, pt: Zcu.PerThread, nav_index: InternPool.Nav.In
24762461 try dlw.writeByte(DW.LNS.set_column);
24772462 try uleb128(dlw, func.lbrace_column + 1);
24782463
2479 try wip_nav.advancePCAndLine(@intCast(loc.line + func.lbrace_line), 0);
2464 try wip_nav.advancePCAndLine(@intCast(decl.src_line + func.lbrace_line), 0);
24802465 }
24812466 },
24822467 }
......@@ -2600,14 +2585,12 @@ pub fn updateComptimeNav(dwarf: *Dwarf, pt: Zcu.PerThread, nav_index: InternPool
26002585 const inst_info = nav.srcInst(ip).resolveFull(ip).?;
26012586 const file = zcu.fileByIndex(inst_info.file);
26022587 assert(file.zir_loaded);
2603 const decl_inst = file.zir.instructions.get(@intFromEnum(inst_info.inst));
2604 assert(decl_inst.tag == .declaration);
2605 const decl_extra = file.zir.extraData(Zir.Inst.Declaration, decl_inst.data.declaration.payload_index);
2588 const decl = file.zir.getDeclaration(inst_info.inst)[0];
26062589
2607 const is_test = switch (decl_extra.data.name) {
2590 const is_test = switch (decl.name) {
26082591 .unnamed_test => true,
26092592 .@"comptime", .@"usingnamespace" => false,
2610 _ => decl_extra.data.name.isNamedTest(file.zir),
2593 _ => decl.name.isNamedTest(file.zir),
26112594 };
26122595 if (is_test) {
26132596 // This isn't actually a comptime Nav! It's a test, so it'll definitely never be referenced at comptime.
......@@ -2618,14 +2601,10 @@ pub fn updateComptimeNav(dwarf: *Dwarf, pt: Zcu.PerThread, nav_index: InternPool
26182601 const parent_namespace_ptr = ip.namespacePtr(ip.getCau(cau).namespace);
26192602 break :parent .{
26202603 parent_namespace_ptr.owner_type,
2621 if (decl_extra.data.flags.is_pub) DW.ACCESS.public else DW.ACCESS.private,
2604 if (decl.flags.is_pub) DW.ACCESS.public else DW.ACCESS.private,
26222605 };
26232606 } else .{ zcu.fileRootType(inst_info.file), DW.ACCESS.private };
26242607
2625 const tree = try file.getTree(dwarf.gpa);
2626 const loc = tree.tokenLocation(0, tree.nodes.items(.main_token)[decl_inst.data.declaration.src_node]);
2627 assert(loc.line == zcu.navSrcLine(nav_index));
2628
26292608 var wip_nav: WipNav = .{
26302609 .dwarf = dwarf,
26312610 .pt = pt,
......@@ -2688,8 +2667,8 @@ pub fn updateComptimeNav(dwarf: *Dwarf, pt: Zcu.PerThread, nav_index: InternPool
26882667 try wip_nav.abbrevCode(if (loaded_struct.field_types.len == 0) .decl_namespace_struct else .decl_struct);
26892668 try wip_nav.refType(.fromInterned(parent_type));
26902669 assert(wip_nav.debug_info.items.len == DebugInfo.declEntryLineOff(dwarf));
2691 try diw.writeInt(u32, @intCast(loc.line + 1), dwarf.endian);
2692 try uleb128(diw, loc.column + 1);
2670 try diw.writeInt(u32, @intCast(decl.src_line + 1), dwarf.endian);
2671 try uleb128(diw, decl.src_column + 1);
26932672 try diw.writeByte(accessibility);
26942673 try wip_nav.strp(nav.name.toSlice(ip));
26952674 if (loaded_struct.field_types.len == 0) try diw.writeByte(@intFromBool(false)) else {
......@@ -2748,8 +2727,8 @@ pub fn updateComptimeNav(dwarf: *Dwarf, pt: Zcu.PerThread, nav_index: InternPool
27482727 try wip_nav.abbrevCode(.decl_packed_struct);
27492728 try wip_nav.refType(.fromInterned(parent_type));
27502729 assert(wip_nav.debug_info.items.len == DebugInfo.declEntryLineOff(dwarf));
2751 try diw.writeInt(u32, @intCast(loc.line + 1), dwarf.endian);
2752 try uleb128(diw, loc.column + 1);
2730 try diw.writeInt(u32, @intCast(decl.src_line + 1), dwarf.endian);
2731 try uleb128(diw, decl.src_column + 1);
27532732 try diw.writeByte(accessibility);
27542733 try wip_nav.strp(nav.name.toSlice(ip));
27552734 try wip_nav.refType(.fromInterned(loaded_struct.backingIntTypeUnordered(ip)));
......@@ -2790,8 +2769,8 @@ pub fn updateComptimeNav(dwarf: *Dwarf, pt: Zcu.PerThread, nav_index: InternPool
27902769 try wip_nav.abbrevCode(if (loaded_enum.names.len > 0) .decl_enum else .decl_empty_enum);
27912770 try wip_nav.refType(.fromInterned(parent_type));
27922771 assert(wip_nav.debug_info.items.len == DebugInfo.declEntryLineOff(dwarf));
2793 try diw.writeInt(u32, @intCast(loc.line + 1), dwarf.endian);
2794 try uleb128(diw, loc.column + 1);
2772 try diw.writeInt(u32, @intCast(decl.src_line + 1), dwarf.endian);
2773 try uleb128(diw, decl.src_column + 1);
27952774 try diw.writeByte(accessibility);
27962775 try wip_nav.strp(nav.name.toSlice(ip));
27972776 try wip_nav.refType(.fromInterned(loaded_enum.tag_ty));
......@@ -2828,8 +2807,8 @@ pub fn updateComptimeNav(dwarf: *Dwarf, pt: Zcu.PerThread, nav_index: InternPool
28282807 try wip_nav.abbrevCode(.decl_union);
28292808 try wip_nav.refType(.fromInterned(parent_type));
28302809 assert(wip_nav.debug_info.items.len == DebugInfo.declEntryLineOff(dwarf));
2831 try diw.writeInt(u32, @intCast(loc.line + 1), dwarf.endian);
2832 try uleb128(diw, loc.column + 1);
2810 try diw.writeInt(u32, @intCast(decl.src_line + 1), dwarf.endian);
2811 try uleb128(diw, decl.src_column + 1);
28332812 try diw.writeByte(accessibility);
28342813 try wip_nav.strp(nav.name.toSlice(ip));
28352814 const union_layout = Type.getUnionLayout(loaded_union, zcu);
......@@ -2902,8 +2881,8 @@ pub fn updateComptimeNav(dwarf: *Dwarf, pt: Zcu.PerThread, nav_index: InternPool
29022881 try wip_nav.abbrevCode(.decl_namespace_struct);
29032882 try wip_nav.refType(.fromInterned(parent_type));
29042883 assert(wip_nav.debug_info.items.len == DebugInfo.declEntryLineOff(dwarf));
2905 try diw.writeInt(u32, @intCast(loc.line + 1), dwarf.endian);
2906 try uleb128(diw, loc.column + 1);
2884 try diw.writeInt(u32, @intCast(decl.src_line + 1), dwarf.endian);
2885 try uleb128(diw, decl.src_column + 1);
29072886 try diw.writeByte(accessibility);
29082887 try wip_nav.strp(nav.name.toSlice(ip));
29092888 try diw.writeByte(@intFromBool(false));
......@@ -2958,8 +2937,8 @@ pub fn updateComptimeNav(dwarf: *Dwarf, pt: Zcu.PerThread, nav_index: InternPool
29582937 .decl_empty_func_generic);
29592938 try wip_nav.refType(.fromInterned(parent_type));
29602939 assert(wip_nav.debug_info.items.len == DebugInfo.declEntryLineOff(dwarf));
2961 try diw.writeInt(u32, @intCast(loc.line + 1), dwarf.endian);
2962 try uleb128(diw, loc.column + 1);
2940 try diw.writeInt(u32, @intCast(decl.src_line + 1), dwarf.endian);
2941 try uleb128(diw, decl.src_column + 1);
29632942 try diw.writeByte(accessibility);
29642943 try wip_nav.strp(nav.name.toSlice(ip));
29652944 try wip_nav.refType(.fromInterned(func_type.return_type));
......@@ -2990,8 +2969,8 @@ pub fn updateComptimeNav(dwarf: *Dwarf, pt: Zcu.PerThread, nav_index: InternPool
29902969 try wip_nav.abbrevCode(.decl_alias);
29912970 try wip_nav.refType(.fromInterned(parent_type));
29922971 assert(wip_nav.debug_info.items.len == DebugInfo.declEntryLineOff(dwarf));
2993 try diw.writeInt(u32, @intCast(loc.line + 1), dwarf.endian);
2994 try uleb128(diw, loc.column + 1);
2972 try diw.writeInt(u32, @intCast(decl.src_line + 1), dwarf.endian);
2973 try uleb128(diw, decl.src_column + 1);
29952974 try diw.writeByte(accessibility);
29962975 try wip_nav.strp(nav.name.toSlice(ip));
29972976 try wip_nav.refType(nav_val.toType());
......@@ -3001,8 +2980,8 @@ pub fn updateComptimeNav(dwarf: *Dwarf, pt: Zcu.PerThread, nav_index: InternPool
30012980 try wip_nav.abbrevCode(.decl_var);
30022981 try wip_nav.refType(.fromInterned(parent_type));
30032982 assert(wip_nav.debug_info.items.len == DebugInfo.declEntryLineOff(dwarf));
3004 try diw.writeInt(u32, @intCast(loc.line + 1), dwarf.endian);
3005 try uleb128(diw, loc.column + 1);
2983 try diw.writeInt(u32, @intCast(decl.src_line + 1), dwarf.endian);
2984 try uleb128(diw, decl.src_column + 1);
30062985 try diw.writeByte(accessibility);
30072986 try wip_nav.strp(nav.name.toSlice(ip));
30082987 try wip_nav.strp(nav.fqn.toSlice(ip));
......@@ -3028,8 +3007,8 @@ pub fn updateComptimeNav(dwarf: *Dwarf, pt: Zcu.PerThread, nav_index: InternPool
30283007 .decl_const);
30293008 try wip_nav.refType(.fromInterned(parent_type));
30303009 assert(wip_nav.debug_info.items.len == DebugInfo.declEntryLineOff(dwarf));
3031 try diw.writeInt(u32, @intCast(loc.line + 1), dwarf.endian);
3032 try uleb128(diw, loc.column + 1);
3010 try diw.writeInt(u32, @intCast(decl.src_line + 1), dwarf.endian);
3011 try uleb128(diw, decl.src_column + 1);
30333012 try diw.writeByte(accessibility);
30343013 try wip_nav.strp(nav.name.toSlice(ip));
30353014 try wip_nav.strp(nav.fqn.toSlice(ip));