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(...@@ -6918,12 +6918,25 @@ fn builtinCall(
6918 return rvalue(gz, rl, result, node);6918 return rvalue(gz, rl, result, node);
6919 },6919 },
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
6921 .breakpoint => return simpleNoOpVoid(gz, rl, node, .breakpoint),6935 .breakpoint => return simpleNoOpVoid(gz, rl, node, .breakpoint),
69226936
6923 // zig fmt: off6937 // zig fmt: off
6924 .This => return rvalue(gz, rl, try gz.addNodeExtended(.this, node), node),6938 .This => return rvalue(gz, rl, try gz.addNodeExtended(.this, node), node),
6925 .return_address => return rvalue(gz, rl, try gz.addNodeExtended(.ret_addr, node), node),6939 .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),
6927 .error_return_trace => return rvalue(gz, rl, try gz.addNodeExtended(.error_return_trace, node), node),6940 .error_return_trace => return rvalue(gz, rl, try gz.addNodeExtended(.error_return_trace, node), node),
6928 .frame => return rvalue(gz, rl, try gz.addNodeExtended(.frame, node), node),6941 .frame => return rvalue(gz, rl, try gz.addNodeExtended(.frame, node), node),
6929 .frame_address => return rvalue(gz, rl, try gz.addNodeExtended(.frame_address, node), node),6942 .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 {...@@ -1663,6 +1663,11 @@ pub const File = struct {
1663 return file.pkg.root_src_directory.join(ally, &[_][]const u8{file.sub_file_path});1663 return file.pkg.root_src_directory.join(ally, &[_][]const u8{file.sub_file_path});
1664 }1664 }
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
1666 pub fn dumpSrc(file: *File, src: LazySrcLoc) void {1671 pub fn dumpSrc(file: *File, src: LazySrcLoc) void {
1667 const loc = std.zig.findLineColumn(file.source.bytes, src);1672 const loc = std.zig.findLineColumn(file.source.bytes, src);
1668 std.debug.print("{s}:{d}:{d}\n", .{ file.sub_file_path, loc.line + 1, loc.column + 1 });1673 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(...@@ -9182,8 +9182,50 @@ fn zirBuiltinSrc(
9182 block: *Block,9182 block: *Block,
9183 extended: Zir.Inst.Extended.InstData,9183 extended: Zir.Inst.Extended.InstData,
9184) CompileError!Air.Inst.Ref {9184) CompileError!Air.Inst.Ref {
9185 const tracy = trace(@src());
9186 defer tracy.end();
9187
9185 const src: LazySrcLoc = .{ .node_offset = @bitCast(i32, extended.operand) };9188 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 );
9187}9229}
91889230
9189fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {9231fn 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 {...@@ -1510,7 +1510,7 @@ pub const Inst = struct {
1510 /// `operand` is `src_node: i32`.1510 /// `operand` is `src_node: i32`.
1511 ret_addr,1511 ret_addr,
1512 /// Implements the `@src` builtin.1512 /// Implements the `@src` builtin.
1513 /// `operand` is `src_node: i32`.1513 /// `operand` is payload index to `ColumnLine`.
1514 builtin_src,1514 builtin_src,
1515 /// Implements the `@errorReturnTrace` builtin.1515 /// Implements the `@errorReturnTrace` builtin.
1516 /// `operand` is `src_node: i32`.1516 /// `operand` is `src_node: i32`.
...@@ -2160,10 +2160,7 @@ pub const Inst = struct {...@@ -2160,10 +2160,7 @@ pub const Inst = struct {
2160 switch_inst: Index,2160 switch_inst: Index,
2161 prong_index: u32,2161 prong_index: u32,
2162 },2162 },
2163 dbg_stmt: struct {2163 dbg_stmt: LineColumn,
2164 line: u32,
2165 column: u32,
2166 },
2167 /// Used for unary operators which reference an inst,2164 /// Used for unary operators which reference an inst,
2168 /// with an AST node source location.2165 /// with an AST node source location.
2169 inst_node: struct {2166 inst_node: struct {
...@@ -2964,6 +2961,11 @@ pub const Inst = struct {...@@ -2964,6 +2961,11 @@ pub const Inst = struct {
2964 token: Ast.TokenIndex,2961 token: Ast.TokenIndex,
2965 };2962 };
2966 };2963 };
2964
2965 pub const LineColumn = struct {
2966 line: u32,
2967 column: u32,
2968 };
2967};2969};
29682970
2969pub const SpecialProng = enum { none, @"else", under };2971pub const SpecialProng = enum { none, @"else", under };
test/behavior.zig+1-1
...@@ -65,6 +65,7 @@ test {...@@ -65,6 +65,7 @@ test {
65 _ = @import("behavior/translate_c_macros.zig");65 _ = @import("behavior/translate_c_macros.zig");
66 _ = @import("behavior/try.zig");66 _ = @import("behavior/try.zig");
67 _ = @import("behavior/undefined.zig");67 _ = @import("behavior/undefined.zig");
68 _ = @import("behavior/src.zig");
6869
69 if (builtin.zig_backend != .stage2_c) {70 if (builtin.zig_backend != .stage2_c) {
70 // Tests that pass for stage1 and the llvm backend.71 // Tests that pass for stage1 and the llvm backend.
...@@ -204,7 +205,6 @@ test {...@@ -204,7 +205,6 @@ test {
204 _ = @import("behavior/wasm.zig");205 _ = @import("behavior/wasm.zig");
205 }206 }
206 _ = @import("behavior/while_stage1.zig");207 _ = @import("behavior/while_stage1.zig");
207 _ = @import("behavior/src.zig");
208 _ = @import("behavior/translate_c_macros_stage1.zig");208 _ = @import("behavior/translate_c_macros_stage1.zig");
209 }209 }
210 }210 }