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