| ... | ... | @@ -17,6 +17,16 @@ tree: *const ast.Tree, |
| 17 | 17 | instructions: std.MultiArrayList(Zir.Inst) = .{}, |
| 18 | 18 | extra: ArrayListUnmanaged(u32) = .{}, |
| 19 | 19 | string_bytes: ArrayListUnmanaged(u8) = .{}, |
| 20 | /// Tracks the current byte offset within the source file. |
| 21 | /// Used to populate line deltas in the ZIR. AstGen maintains |
| 22 | /// this "cursor" throughout the entire AST lowering process in order |
| 23 | /// to avoid starting over the line/column scan for every declaration, which |
| 24 | /// would be O(N^2). |
| 25 | source_offset: u32 = 0, |
| 26 | /// Tracks the current line of `source_offset`. |
| 27 | source_line: u32 = 0, |
| 28 | /// Tracks the current column of `source_offset`. |
| 29 | source_column: u32 = 0, |
| 20 | 30 | /// Used for temporary allocations; freed after AstGen is complete. |
| 21 | 31 | /// The resulting ZIR code has no references to anything in this arena. |
| 22 | 32 | arena: *Allocator, |
| ... | ... | @@ -2472,15 +2482,18 @@ fn emitDbgNode(gz: *GenZir, node: ast.Node.Index) !void { |
| 2472 | 2482 | |
| 2473 | 2483 | const astgen = gz.astgen; |
| 2474 | 2484 | const tree = astgen.tree; |
| 2485 | const source = tree.source; |
| 2475 | 2486 | const token_starts = tree.tokens.items(.start); |
| 2476 | | const decl_start = token_starts[tree.firstToken(gz.decl_node_index)]; |
| 2477 | 2487 | const node_start = token_starts[tree.firstToken(node)]; |
| 2478 | | const source = tree.source[decl_start..node_start]; |
| 2479 | | const loc = std.zig.findLineColumn(source, source.len); |
| 2488 | |
| 2489 | astgen.advanceSourceCursor(source, node_start); |
| 2490 | const line = @intCast(u32, astgen.source_line); |
| 2491 | const column = @intCast(u32, astgen.source_column); |
| 2492 | |
| 2480 | 2493 | _ = try gz.add(.{ .tag = .dbg_stmt, .data = .{ |
| 2481 | 2494 | .dbg_stmt = .{ |
| 2482 | | .line = @intCast(u32, loc.line), |
| 2483 | | .column = @intCast(u32, loc.column), |
| 2495 | .line = line, |
| 2496 | .column = column, |
| 2484 | 2497 | }, |
| 2485 | 2498 | } }); |
| 2486 | 2499 | } |
| ... | ... | @@ -8564,12 +8577,13 @@ const GenZir = struct { |
| 8564 | 8577 | fn calcLine(gz: GenZir, node: ast.Node.Index) u32 { |
| 8565 | 8578 | const astgen = gz.astgen; |
| 8566 | 8579 | const tree = astgen.tree; |
| 8580 | const source = tree.source; |
| 8567 | 8581 | const token_starts = tree.tokens.items(.start); |
| 8568 | | const decl_start = token_starts[tree.firstToken(gz.decl_node_index)]; |
| 8569 | 8582 | const node_start = token_starts[tree.firstToken(node)]; |
| 8570 | | const source = tree.source[decl_start..node_start]; |
| 8571 | | const loc = std.zig.findLineColumn(source, source.len); |
| 8572 | | return @intCast(u32, gz.decl_line + loc.line); |
| 8583 | |
| 8584 | astgen.advanceSourceCursor(source, node_start); |
| 8585 | |
| 8586 | return @intCast(u32, gz.decl_line + astgen.source_line); |
| 8573 | 8587 | } |
| 8574 | 8588 | |
| 8575 | 8589 | fn tokSrcLoc(gz: GenZir, token_index: ast.TokenIndex) LazySrcLoc { |
| ... | ... | @@ -8713,20 +8727,21 @@ const GenZir = struct { |
| 8713 | 8727 | const node_tags = tree.nodes.items(.tag); |
| 8714 | 8728 | const node_datas = tree.nodes.items(.data); |
| 8715 | 8729 | const token_starts = tree.tokens.items(.start); |
| 8716 | | const decl_start = token_starts[tree.firstToken(gz.decl_node_index)]; |
| 8717 | 8730 | const fn_decl = args.src_node; |
| 8718 | 8731 | assert(node_tags[fn_decl] == .fn_decl or node_tags[fn_decl] == .test_decl); |
| 8719 | 8732 | const block = node_datas[fn_decl].rhs; |
| 8720 | 8733 | const lbrace_start = token_starts[tree.firstToken(block)]; |
| 8721 | 8734 | const rbrace_start = token_starts[tree.lastToken(block)]; |
| 8722 | | const lbrace_source = tree.source[decl_start..lbrace_start]; |
| 8723 | | const lbrace_loc = std.zig.findLineColumn(lbrace_source, lbrace_source.len); |
| 8724 | | const rbrace_source = tree.source[lbrace_start..rbrace_start]; |
| 8725 | | const rbrace_loc = std.zig.findLineColumn(rbrace_source, rbrace_source.len); |
| 8726 | | const lbrace_line = @intCast(u32, lbrace_loc.line); |
| 8727 | | const rbrace_line = lbrace_line + @intCast(u32, rbrace_loc.line); |
| 8728 | | const columns = @intCast(u32, lbrace_loc.column) | |
| 8729 | | (@intCast(u32, rbrace_loc.column) << 16); |
| 8735 | |
| 8736 | astgen.advanceSourceCursor(tree.source, lbrace_start); |
| 8737 | const lbrace_line = @intCast(u32, astgen.source_line); |
| 8738 | const lbrace_column = @intCast(u32, astgen.source_column); |
| 8739 | |
| 8740 | astgen.advanceSourceCursor(tree.source, rbrace_start); |
| 8741 | const rbrace_line = @intCast(u32, astgen.source_line); |
| 8742 | const rbrace_column = @intCast(u32, astgen.source_column); |
| 8743 | |
| 8744 | const columns = lbrace_column | (rbrace_column << 16); |
| 8730 | 8745 | src_locs_buffer[0] = lbrace_line; |
| 8731 | 8746 | src_locs_buffer[1] = rbrace_line; |
| 8732 | 8747 | src_locs_buffer[2] = columns; |
| ... | ... | @@ -9542,3 +9557,20 @@ fn declareNewName( |
| 9542 | 9557 | } |
| 9543 | 9558 | } |
| 9544 | 9559 | } |
| 9560 | |
| 9561 | fn advanceSourceCursor(astgen: *AstGen, source: []const u8, end: usize) void { |
| 9562 | var i = astgen.source_offset; |
| 9563 | var line = astgen.source_line; |
| 9564 | var column = astgen.source_column; |
| 9565 | while (i < end) : (i += 1) { |
| 9566 | if (source[i] == '\n') { |
| 9567 | line += 1; |
| 9568 | column = 0; |
| 9569 | } else { |
| 9570 | column += 1; |
| 9571 | } |
| 9572 | } |
| 9573 | astgen.source_offset = i; |
| 9574 | astgen.source_line = line; |
| 9575 | astgen.source_column = column; |
| 9576 | } |