| ... | ... | @@ -27,9 +27,11 @@ string_bytes: ArrayListUnmanaged(u8) = .{}, |
| 27 | 27 | /// to avoid starting over the line/column scan for every declaration, which |
| 28 | 28 | /// would be O(N^2). |
| 29 | 29 | source_offset: u32 = 0, |
| 30 | | /// Tracks the current line of `source_offset`. |
| 30 | /// Tracks the corresponding line of `source_offset`. |
| 31 | /// This value is absolute. |
| 31 | 32 | source_line: u32 = 0, |
| 32 | | /// Tracks the current column of `source_offset`. |
| 33 | /// Tracks the corresponding column of `source_offset`. |
| 34 | /// This value is absolute. |
| 33 | 35 | source_column: u32 = 0, |
| 34 | 36 | /// Used for temporary allocations; freed after AstGen is complete. |
| 35 | 37 | /// The resulting ZIR code has no references to anything in this arena. |
| ... | ... | @@ -2511,7 +2513,7 @@ fn makeDeferScope( |
| 2511 | 2513 | const token_starts = tree.tokens.items(.start); |
| 2512 | 2514 | const node_start = token_starts[tree.firstToken(expr_node)]; |
| 2513 | 2515 | const defer_scope = try block_arena.create(Scope.Defer); |
| 2514 | | astgen.advanceSourceCursor(tree.source, node_start); |
| 2516 | astgen.advanceSourceCursor(node_start); |
| 2515 | 2517 | |
| 2516 | 2518 | defer_scope.* = .{ |
| 2517 | 2519 | .base = .{ .tag = scope_tag }, |
| ... | ... | @@ -2775,14 +2777,9 @@ fn emitDbgNode(gz: *GenZir, node: Ast.Node.Index) !void { |
| 2775 | 2777 | if (gz.force_comptime) return; |
| 2776 | 2778 | |
| 2777 | 2779 | const astgen = gz.astgen; |
| 2778 | | const tree = astgen.tree; |
| 2779 | | const source = tree.source; |
| 2780 | | const token_starts = tree.tokens.items(.start); |
| 2781 | | const node_start = token_starts[tree.firstToken(node)]; |
| 2782 | | |
| 2783 | | astgen.advanceSourceCursor(source, node_start); |
| 2784 | | const line = @intCast(u32, astgen.source_line); |
| 2785 | | const column = @intCast(u32, astgen.source_column); |
| 2780 | astgen.advanceSourceCursorToNode(node); |
| 2781 | const line = astgen.source_line - gz.decl_line; |
| 2782 | const column = astgen.source_column; |
| 2786 | 2783 | |
| 2787 | 2784 | _ = try gz.add(.{ .tag = .dbg_stmt, .data = .{ |
| 2788 | 2785 | .dbg_stmt = .{ |
| ... | ... | @@ -3188,12 +3185,13 @@ fn fnDecl( |
| 3188 | 3185 | // We insert this at the beginning so that its instruction index marks the |
| 3189 | 3186 | // start of the top level declaration. |
| 3190 | 3187 | const block_inst = try gz.makeBlockInst(.block_inline, fn_proto.ast.proto_node); |
| 3188 | astgen.advanceSourceCursorToNode(decl_node); |
| 3191 | 3189 | |
| 3192 | 3190 | var decl_gz: GenZir = .{ |
| 3193 | 3191 | .force_comptime = true, |
| 3194 | 3192 | .in_defer = false, |
| 3195 | 3193 | .decl_node_index = fn_proto.ast.proto_node, |
| 3196 | | .decl_line = gz.calcLine(decl_node), |
| 3194 | .decl_line = astgen.source_line, |
| 3197 | 3195 | .parent = scope, |
| 3198 | 3196 | .astgen = astgen, |
| 3199 | 3197 | .instructions = gz.instructions, |
| ... | ... | @@ -3391,11 +3389,9 @@ fn fnDecl( |
| 3391 | 3389 | astgen.fn_block = &fn_gz; |
| 3392 | 3390 | defer astgen.fn_block = prev_fn_block; |
| 3393 | 3391 | |
| 3394 | | const token_starts = tree.tokens.items(.start); |
| 3395 | | const lbrace_start = token_starts[tree.firstToken(body_node)]; |
| 3396 | | astgen.advanceSourceCursor(tree.source, lbrace_start); |
| 3397 | | const lbrace_line = @intCast(u32, astgen.source_line); |
| 3398 | | const lbrace_column = @intCast(u32, astgen.source_column); |
| 3392 | astgen.advanceSourceCursorToNode(body_node); |
| 3393 | const lbrace_line = astgen.source_line - decl_gz.decl_line; |
| 3394 | const lbrace_column = astgen.source_column; |
| 3399 | 3395 | |
| 3400 | 3396 | _ = try expr(&fn_gz, params_scope, .none, body_node); |
| 3401 | 3397 | try checkUsed(gz, &fn_gz.base, params_scope); |
| ... | ... | @@ -3465,11 +3461,12 @@ fn globalVarDecl( |
| 3465 | 3461 | |
| 3466 | 3462 | const name_token = var_decl.ast.mut_token + 1; |
| 3467 | 3463 | const name_str_index = try astgen.identAsString(name_token); |
| 3464 | astgen.advanceSourceCursorToNode(node); |
| 3468 | 3465 | |
| 3469 | 3466 | var block_scope: GenZir = .{ |
| 3470 | 3467 | .parent = scope, |
| 3471 | 3468 | .decl_node_index = node, |
| 3472 | | .decl_line = gz.calcLine(node), |
| 3469 | .decl_line = astgen.source_line, |
| 3473 | 3470 | .astgen = astgen, |
| 3474 | 3471 | .force_comptime = true, |
| 3475 | 3472 | .in_defer = false, |
| ... | ... | @@ -3614,12 +3611,13 @@ fn comptimeDecl( |
| 3614 | 3611 | // top-level declaration. |
| 3615 | 3612 | const block_inst = try gz.makeBlockInst(.block_inline, node); |
| 3616 | 3613 | wip_members.nextDecl(false, false, false, false); |
| 3614 | astgen.advanceSourceCursorToNode(node); |
| 3617 | 3615 | |
| 3618 | 3616 | var decl_block: GenZir = .{ |
| 3619 | 3617 | .force_comptime = true, |
| 3620 | 3618 | .in_defer = false, |
| 3621 | 3619 | .decl_node_index = node, |
| 3622 | | .decl_line = gz.calcLine(node), |
| 3620 | .decl_line = astgen.source_line, |
| 3623 | 3621 | .parent = scope, |
| 3624 | 3622 | .astgen = astgen, |
| 3625 | 3623 | .instructions = gz.instructions, |
| ... | ... | @@ -3668,12 +3666,13 @@ fn usingnamespaceDecl( |
| 3668 | 3666 | // top-level declaration. |
| 3669 | 3667 | const block_inst = try gz.makeBlockInst(.block_inline, node); |
| 3670 | 3668 | wip_members.nextDecl(is_pub, true, false, false); |
| 3669 | astgen.advanceSourceCursorToNode(node); |
| 3671 | 3670 | |
| 3672 | 3671 | var decl_block: GenZir = .{ |
| 3673 | 3672 | .force_comptime = true, |
| 3674 | 3673 | .in_defer = false, |
| 3675 | 3674 | .decl_node_index = node, |
| 3676 | | .decl_line = gz.calcLine(node), |
| 3675 | .decl_line = astgen.source_line, |
| 3677 | 3676 | .parent = scope, |
| 3678 | 3677 | .astgen = astgen, |
| 3679 | 3678 | .instructions = gz.instructions, |
| ... | ... | @@ -3715,12 +3714,13 @@ fn testDecl( |
| 3715 | 3714 | const block_inst = try gz.makeBlockInst(.block_inline, node); |
| 3716 | 3715 | |
| 3717 | 3716 | wip_members.nextDecl(false, false, false, false); |
| 3717 | astgen.advanceSourceCursorToNode(node); |
| 3718 | 3718 | |
| 3719 | 3719 | var decl_block: GenZir = .{ |
| 3720 | 3720 | .force_comptime = true, |
| 3721 | 3721 | .in_defer = false, |
| 3722 | 3722 | .decl_node_index = node, |
| 3723 | | .decl_line = gz.calcLine(node), |
| 3723 | .decl_line = astgen.source_line, |
| 3724 | 3724 | .parent = scope, |
| 3725 | 3725 | .astgen = astgen, |
| 3726 | 3726 | .instructions = gz.instructions, |
| ... | ... | @@ -3756,11 +3756,9 @@ fn testDecl( |
| 3756 | 3756 | astgen.fn_block = &fn_block; |
| 3757 | 3757 | defer astgen.fn_block = prev_fn_block; |
| 3758 | 3758 | |
| 3759 | | const token_starts = tree.tokens.items(.start); |
| 3760 | | const lbrace_start = token_starts[tree.firstToken(body_node)]; |
| 3761 | | astgen.advanceSourceCursor(tree.source, lbrace_start); |
| 3762 | | const lbrace_line = @intCast(u32, astgen.source_line); |
| 3763 | | const lbrace_column = @intCast(u32, astgen.source_column); |
| 3759 | astgen.advanceSourceCursorToNode(body_node); |
| 3760 | const lbrace_line = astgen.source_line - decl_block.decl_line; |
| 3761 | const lbrace_column = astgen.source_column; |
| 3764 | 3762 | |
| 3765 | 3763 | const block_result = try expr(&fn_block, &fn_block.base, .none, body_node); |
| 3766 | 3764 | if (fn_block.isEmpty() or !fn_block.refIsNoReturn(block_result)) { |
| ... | ... | @@ -3841,10 +3839,11 @@ fn structDeclInner( |
| 3841 | 3839 | // The struct_decl instruction introduces a scope in which the decls of the struct |
| 3842 | 3840 | // are in scope, so that field types, alignments, and default value expressions |
| 3843 | 3841 | // can refer to decls within the struct itself. |
| 3842 | astgen.advanceSourceCursorToNode(node); |
| 3844 | 3843 | var block_scope: GenZir = .{ |
| 3845 | 3844 | .parent = &namespace.base, |
| 3846 | 3845 | .decl_node_index = node, |
| 3847 | | .decl_line = gz.calcLine(node), |
| 3846 | .decl_line = astgen.source_line, |
| 3848 | 3847 | .astgen = astgen, |
| 3849 | 3848 | .force_comptime = true, |
| 3850 | 3849 | .in_defer = false, |
| ... | ... | @@ -3966,10 +3965,11 @@ fn unionDeclInner( |
| 3966 | 3965 | // The union_decl instruction introduces a scope in which the decls of the union |
| 3967 | 3966 | // are in scope, so that field types, alignments, and default value expressions |
| 3968 | 3967 | // can refer to decls within the union itself. |
| 3968 | astgen.advanceSourceCursorToNode(node); |
| 3969 | 3969 | var block_scope: GenZir = .{ |
| 3970 | 3970 | .parent = &namespace.base, |
| 3971 | 3971 | .decl_node_index = node, |
| 3972 | | .decl_line = gz.calcLine(node), |
| 3972 | .decl_line = astgen.source_line, |
| 3973 | 3973 | .astgen = astgen, |
| 3974 | 3974 | .force_comptime = true, |
| 3975 | 3975 | .in_defer = false, |
| ... | ... | @@ -4249,10 +4249,11 @@ fn containerDecl( |
| 4249 | 4249 | |
| 4250 | 4250 | // The enum_decl instruction introduces a scope in which the decls of the enum |
| 4251 | 4251 | // are in scope, so that tag values can refer to decls within the enum itself. |
| 4252 | astgen.advanceSourceCursorToNode(node); |
| 4252 | 4253 | var block_scope: GenZir = .{ |
| 4253 | 4254 | .parent = &namespace.base, |
| 4254 | 4255 | .decl_node_index = node, |
| 4255 | | .decl_line = gz.calcLine(node), |
| 4256 | .decl_line = astgen.source_line, |
| 4256 | 4257 | .astgen = astgen, |
| 4257 | 4258 | .force_comptime = true, |
| 4258 | 4259 | .in_defer = false, |
| ... | ... | @@ -6980,7 +6981,7 @@ fn builtinCall( |
| 6980 | 6981 | const token_starts = tree.tokens.items(.start); |
| 6981 | 6982 | const node_start = token_starts[tree.firstToken(node)]; |
| 6982 | 6983 | |
| 6983 | | astgen.advanceSourceCursor(tree.source, node_start); |
| 6984 | astgen.advanceSourceCursor(node_start); |
| 6984 | 6985 | |
| 6985 | 6986 | const result = try gz.addExtendedPayload(.builtin_src, Zir.Inst.LineColumn{ |
| 6986 | 6987 | .line = @intCast(u32, astgen.source_line), |
| ... | ... | @@ -9560,18 +9561,6 @@ const GenZir = struct { |
| 9560 | 9561 | return false; |
| 9561 | 9562 | } |
| 9562 | 9563 | |
| 9563 | | fn calcLine(gz: GenZir, node: Ast.Node.Index) u32 { |
| 9564 | | const astgen = gz.astgen; |
| 9565 | | const tree = astgen.tree; |
| 9566 | | const source = tree.source; |
| 9567 | | const token_starts = tree.tokens.items(.start); |
| 9568 | | const node_start = token_starts[tree.firstToken(node)]; |
| 9569 | | |
| 9570 | | astgen.advanceSourceCursor(source, node_start); |
| 9571 | | |
| 9572 | | return @intCast(u32, gz.decl_line + astgen.source_line); |
| 9573 | | } |
| 9574 | | |
| 9575 | 9564 | fn nodeIndexToRelative(gz: GenZir, node_index: Ast.Node.Index) i32 { |
| 9576 | 9565 | return @bitCast(i32, node_index) - @bitCast(i32, gz.decl_node_index); |
| 9577 | 9566 | } |
| ... | ... | @@ -9704,8 +9693,8 @@ const GenZir = struct { |
| 9704 | 9693 | assert(node_tags[fn_decl] == .fn_decl or node_tags[fn_decl] == .test_decl); |
| 9705 | 9694 | const block = node_datas[fn_decl].rhs; |
| 9706 | 9695 | const rbrace_start = token_starts[tree.lastToken(block)]; |
| 9707 | | astgen.advanceSourceCursor(tree.source, rbrace_start); |
| 9708 | | const rbrace_line = @intCast(u32, astgen.source_line); |
| 9696 | astgen.advanceSourceCursor(rbrace_start); |
| 9697 | const rbrace_line = @intCast(u32, astgen.source_line - gz.decl_line); |
| 9709 | 9698 | const rbrace_column = @intCast(u32, astgen.source_column); |
| 9710 | 9699 | |
| 9711 | 9700 | const columns = args.lbrace_column | (rbrace_column << 16); |
| ... | ... | @@ -10736,7 +10725,17 @@ fn detectLocalShadowing( |
| 10736 | 10725 | }; |
| 10737 | 10726 | } |
| 10738 | 10727 | |
| 10739 | | fn advanceSourceCursor(astgen: *AstGen, source: []const u8, end: usize) void { |
| 10728 | /// Advances the source cursor to the beginning of `node`. |
| 10729 | fn advanceSourceCursorToNode(astgen: *AstGen, node: Ast.Node.Index) void { |
| 10730 | const tree = astgen.tree; |
| 10731 | const token_starts = tree.tokens.items(.start); |
| 10732 | const node_start = token_starts[tree.firstToken(node)]; |
| 10733 | astgen.advanceSourceCursor(node_start); |
| 10734 | } |
| 10735 | |
| 10736 | /// Advances the source cursor to an absolute byte offset `end` in the file. |
| 10737 | fn advanceSourceCursor(astgen: *AstGen, end: usize) void { |
| 10738 | const source = astgen.tree.source; |
| 10740 | 10739 | var i = astgen.source_offset; |
| 10741 | 10740 | var line = astgen.source_line; |
| 10742 | 10741 | var column = astgen.source_column; |