authorgravatar for robin@voetter.nlRobin Voetter <robin@voetter.nl> 2022-01-08 04:29:49+01:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-01-08 14:32:40-05:00
logcc5c25d48b5331396bfa5218dc7f29dff26e20f9
tree08767d5933f36d6bc57cc284ed62ac9ca0d625c4
parent4931b8dc93ee4a99a415dffab03d400e95d1a90a

stage2: implement @src


5 files changed, 70 insertions(+), 8 deletions(-)

src/AstGen.zig+14-1
......@@ -6918,12 +6918,25 @@ fn builtinCall(
69186918 return rvalue(gz, rl, result, node);
69196919 },
69206920
6921 .src => {
6922 const token_starts = tree.tokens.items(.start);
6923 const node_start = token_starts[tree.firstToken(node)];
6924
6925 astgen.advanceSourceCursor(tree.source, node_start);
6926
6927 const result = try gz.addExtendedPayload(.builtin_src, Zir.Inst.LineColumn{
6928 .line = @intCast(u32, astgen.source_line),
6929 .column = @intCast(u32, astgen.source_column),
6930 });
6931
6932 return rvalue(gz, rl, result, node);
6933 },
6934
69216935 .breakpoint => return simpleNoOpVoid(gz, rl, node, .breakpoint),
69226936
69236937 // zig fmt: off
69246938 .This => return rvalue(gz, rl, try gz.addNodeExtended(.this, node), node),
69256939 .return_address => return rvalue(gz, rl, try gz.addNodeExtended(.ret_addr, node), node),
6926 .src => return rvalue(gz, rl, try gz.addNodeExtended(.builtin_src, node), node),
69276940 .error_return_trace => return rvalue(gz, rl, try gz.addNodeExtended(.error_return_trace, node), node),
69286941 .frame => return rvalue(gz, rl, try gz.addNodeExtended(.frame, node), node),
69296942 .frame_address => return rvalue(gz, rl, try gz.addNodeExtended(.frame_address, node), node),
src/Module.zig+5
......@@ -1663,6 +1663,11 @@ pub const File = struct {
16631663 return file.pkg.root_src_directory.join(ally, &[_][]const u8{file.sub_file_path});
16641664 }
16651665
1666 /// Returns the full path to this file relative to its package.
1667 pub fn fullPathZ(file: File, ally: Allocator) ![:0]u8 {
1668 return file.pkg.root_src_directory.joinZ(ally, &[_][]const u8{file.sub_file_path});
1669 }
1670
16661671 pub fn dumpSrc(file: *File, src: LazySrcLoc) void {
16671672 const loc = std.zig.findLineColumn(file.source.bytes, src);
16681673 std.debug.print("{s}:{d}:{d}\n", .{ file.sub_file_path, loc.line + 1, loc.column + 1 });
src/Sema.zig+43-1
......@@ -9182,8 +9182,50 @@ fn zirBuiltinSrc(
91829182 block: *Block,
91839183 extended: Zir.Inst.Extended.InstData,
91849184) CompileError!Air.Inst.Ref {
9185 const tracy = trace(@src());
9186 defer tracy.end();
9187
91859188 const src: LazySrcLoc = .{ .node_offset = @bitCast(i32, extended.operand) };
9186 return sema.fail(block, src, "TODO: implement Sema.zirBuiltinSrc", .{});
9189 const extra = sema.code.extraData(Zir.Inst.LineColumn, extended.operand).data;
9190 const func = sema.func orelse return sema.fail(block, src, "@src outside function", .{});
9191
9192 const func_name_val = blk: {
9193 var anon_decl = try block.startAnonDecl();
9194 defer anon_decl.deinit();
9195 const name = std.mem.span(func.owner_decl.name);
9196 const bytes = try anon_decl.arena().dupe(u8, name[0 .. name.len + 1]);
9197 const new_decl = try anon_decl.finish(
9198 try Type.Tag.array_u8_sentinel_0.create(anon_decl.arena(), bytes.len - 1),
9199 try Value.Tag.bytes.create(anon_decl.arena(), bytes),
9200 );
9201 break :blk try Value.Tag.decl_ref.create(sema.arena, new_decl);
9202 };
9203
9204 const file_name_val = blk: {
9205 var anon_decl = try block.startAnonDecl();
9206 defer anon_decl.deinit();
9207 const name = try func.owner_decl.getFileScope().fullPathZ(anon_decl.arena());
9208 const new_decl = try anon_decl.finish(
9209 try Type.Tag.array_u8_sentinel_0.create(anon_decl.arena(), name.len),
9210 try Value.Tag.bytes.create(anon_decl.arena(), name[0 .. name.len + 1]),
9211 );
9212 break :blk try Value.Tag.decl_ref.create(sema.arena, new_decl);
9213 };
9214
9215 const field_values = try sema.arena.alloc(Value, 4);
9216 // file: [:0]const u8,
9217 field_values[0] = file_name_val;
9218 // fn_name: [:0]const u8,
9219 field_values[1] = func_name_val;
9220 // line: u32
9221 field_values[2] = try Value.Tag.int_u64.create(sema.arena, extra.line + 1);
9222 // column: u32,
9223 field_values[3] = try Value.Tag.int_u64.create(sema.arena, extra.column + 1);
9224
9225 return sema.addConstant(
9226 try sema.getBuiltinType(block, src, "SourceLocation"),
9227 try Value.Tag.@"struct".create(sema.arena, field_values),
9228 );
91879229}
91889230
91899231fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
src/Zir.zig+7-5
......@@ -1510,7 +1510,7 @@ pub const Inst = struct {
15101510 /// `operand` is `src_node: i32`.
15111511 ret_addr,
15121512 /// Implements the `@src` builtin.
1513 /// `operand` is `src_node: i32`.
1513 /// `operand` is payload index to `ColumnLine`.
15141514 builtin_src,
15151515 /// Implements the `@errorReturnTrace` builtin.
15161516 /// `operand` is `src_node: i32`.
......@@ -2160,10 +2160,7 @@ pub const Inst = struct {
21602160 switch_inst: Index,
21612161 prong_index: u32,
21622162 },
2163 dbg_stmt: struct {
2164 line: u32,
2165 column: u32,
2166 },
2163 dbg_stmt: LineColumn,
21672164 /// Used for unary operators which reference an inst,
21682165 /// with an AST node source location.
21692166 inst_node: struct {
......@@ -2964,6 +2961,11 @@ pub const Inst = struct {
29642961 token: Ast.TokenIndex,
29652962 };
29662963 };
2964
2965 pub const LineColumn = struct {
2966 line: u32,
2967 column: u32,
2968 };
29672969};
29682970
29692971pub const SpecialProng = enum { none, @"else", under };
test/behavior.zig+1-1
......@@ -65,6 +65,7 @@ test {
6565 _ = @import("behavior/translate_c_macros.zig");
6666 _ = @import("behavior/try.zig");
6767 _ = @import("behavior/undefined.zig");
68 _ = @import("behavior/src.zig");
6869
6970 if (builtin.zig_backend != .stage2_c) {
7071 // Tests that pass for stage1 and the llvm backend.
......@@ -204,7 +205,6 @@ test {
204205 _ = @import("behavior/wasm.zig");
205206 }
206207 _ = @import("behavior/while_stage1.zig");
207 _ = @import("behavior/src.zig");
208208 _ = @import("behavior/translate_c_macros_stage1.zig");
209209 }
210210 }