Merge pull request #20299 from mlugg/the-great-decl-split

The Great Decl Split (preliminary work): refactor source locations and eliminate `Sema.Block.src_decl`.
This commit is contained in:
Matthew Lugg 2024-06-20 11:07:17 +01:00 committed by GitHub
commit f73be120f4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
34 changed files with 2989 additions and 3344 deletions

View File

@ -350,379 +350,6 @@ pub fn serializeCpuAlloc(ally: Allocator, cpu: std.Target.Cpu) Allocator.Error![
return buffer.toOwnedSlice();
}
pub const DeclIndex = enum(u32) {
_,
pub fn toOptional(i: DeclIndex) OptionalDeclIndex {
return @enumFromInt(@intFromEnum(i));
}
};
pub const OptionalDeclIndex = enum(u32) {
none = std.math.maxInt(u32),
_,
pub fn init(oi: ?DeclIndex) OptionalDeclIndex {
return @enumFromInt(@intFromEnum(oi orelse return .none));
}
pub fn unwrap(oi: OptionalDeclIndex) ?DeclIndex {
if (oi == .none) return null;
return @enumFromInt(@intFromEnum(oi));
}
};
/// Resolving a source location into a byte offset may require doing work
/// that we would rather not do unless the error actually occurs.
/// Therefore we need a data structure that contains the information necessary
/// to lazily produce a `SrcLoc` as required.
/// Most of the offsets in this data structure are relative to the containing Decl.
/// This makes the source location resolve properly even when a Decl gets
/// shifted up or down in the file, as long as the Decl's contents itself
/// do not change.
pub const LazySrcLoc = union(enum) {
/// When this tag is set, the code that constructed this `LazySrcLoc` is asserting
/// that all code paths which would need to resolve the source location are
/// unreachable. If you are debugging this tag incorrectly being this value,
/// look into using reverse-continue with a memory watchpoint to see where the
/// value is being set to this tag.
unneeded,
/// Means the source location points to an entire file; not any particular
/// location within the file. `file_scope` union field will be active.
entire_file,
/// The source location points to a byte offset within a source file,
/// offset from 0. The source file is determined contextually.
/// Inside a `SrcLoc`, the `file_scope` union field will be active.
byte_abs: u32,
/// The source location points to a token within a source file,
/// offset from 0. The source file is determined contextually.
/// Inside a `SrcLoc`, the `file_scope` union field will be active.
token_abs: u32,
/// The source location points to an AST node within a source file,
/// offset from 0. The source file is determined contextually.
/// Inside a `SrcLoc`, the `file_scope` union field will be active.
node_abs: u32,
/// The source location points to a byte offset within a source file,
/// offset from the byte offset of the Decl within the file.
/// The Decl is determined contextually.
byte_offset: u32,
/// This data is the offset into the token list from the Decl token.
/// The Decl is determined contextually.
token_offset: u32,
/// The source location points to an AST node, which is this value offset
/// from its containing Decl node AST index.
/// The Decl is determined contextually.
node_offset: TracedOffset,
/// The source location points to the main token of an AST node, found
/// by taking this AST node index offset from the containing Decl AST node.
/// The Decl is determined contextually.
node_offset_main_token: i32,
/// The source location points to the beginning of a struct initializer.
/// The Decl is determined contextually.
node_offset_initializer: i32,
/// The source location points to a variable declaration type expression,
/// found by taking this AST node index offset from the containing
/// Decl AST node, which points to a variable declaration AST node. Next, navigate
/// to the type expression.
/// The Decl is determined contextually.
node_offset_var_decl_ty: i32,
/// The source location points to the alignment expression of a var decl.
/// The Decl is determined contextually.
node_offset_var_decl_align: i32,
/// The source location points to the linksection expression of a var decl.
/// The Decl is determined contextually.
node_offset_var_decl_section: i32,
/// The source location points to the addrspace expression of a var decl.
/// The Decl is determined contextually.
node_offset_var_decl_addrspace: i32,
/// The source location points to the initializer of a var decl.
/// The Decl is determined contextually.
node_offset_var_decl_init: i32,
/// The source location points to the first parameter of a builtin
/// function call, found by taking this AST node index offset from the containing
/// Decl AST node, which points to a builtin call AST node. Next, navigate
/// to the first parameter.
/// The Decl is determined contextually.
node_offset_builtin_call_arg0: i32,
/// Same as `node_offset_builtin_call_arg0` except arg index 1.
node_offset_builtin_call_arg1: i32,
node_offset_builtin_call_arg2: i32,
node_offset_builtin_call_arg3: i32,
node_offset_builtin_call_arg4: i32,
node_offset_builtin_call_arg5: i32,
/// Like `node_offset_builtin_call_arg0` but recurses through arbitrarily many calls
/// to pointer cast builtins.
node_offset_ptrcast_operand: i32,
/// The source location points to the index expression of an array access
/// expression, found by taking this AST node index offset from the containing
/// Decl AST node, which points to an array access AST node. Next, navigate
/// to the index expression.
/// The Decl is determined contextually.
node_offset_array_access_index: i32,
/// The source location points to the LHS of a slice expression
/// expression, found by taking this AST node index offset from the containing
/// Decl AST node, which points to a slice AST node. Next, navigate
/// to the sentinel expression.
/// The Decl is determined contextually.
node_offset_slice_ptr: i32,
/// The source location points to start expression of a slice expression
/// expression, found by taking this AST node index offset from the containing
/// Decl AST node, which points to a slice AST node. Next, navigate
/// to the sentinel expression.
/// The Decl is determined contextually.
node_offset_slice_start: i32,
/// The source location points to the end expression of a slice
/// expression, found by taking this AST node index offset from the containing
/// Decl AST node, which points to a slice AST node. Next, navigate
/// to the sentinel expression.
/// The Decl is determined contextually.
node_offset_slice_end: i32,
/// The source location points to the sentinel expression of a slice
/// expression, found by taking this AST node index offset from the containing
/// Decl AST node, which points to a slice AST node. Next, navigate
/// to the sentinel expression.
/// The Decl is determined contextually.
node_offset_slice_sentinel: i32,
/// The source location points to the callee expression of a function
/// call expression, found by taking this AST node index offset from the containing
/// Decl AST node, which points to a function call AST node. Next, navigate
/// to the callee expression.
/// The Decl is determined contextually.
node_offset_call_func: i32,
/// The payload is offset from the containing Decl AST node.
/// The source location points to the field name of:
/// * a field access expression (`a.b`), or
/// * the callee of a method call (`a.b()`)
/// The Decl is determined contextually.
node_offset_field_name: i32,
/// The payload is offset from the containing Decl AST node.
/// The source location points to the field name of the operand ("b" node)
/// of a field initialization expression (`.a = b`)
/// The Decl is determined contextually.
node_offset_field_name_init: i32,
/// The source location points to the pointer of a pointer deref expression,
/// found by taking this AST node index offset from the containing
/// Decl AST node, which points to a pointer deref AST node. Next, navigate
/// to the pointer expression.
/// The Decl is determined contextually.
node_offset_deref_ptr: i32,
/// The source location points to the assembly source code of an inline assembly
/// expression, found by taking this AST node index offset from the containing
/// Decl AST node, which points to inline assembly AST node. Next, navigate
/// to the asm template source code.
/// The Decl is determined contextually.
node_offset_asm_source: i32,
/// The source location points to the return type of an inline assembly
/// expression, found by taking this AST node index offset from the containing
/// Decl AST node, which points to inline assembly AST node. Next, navigate
/// to the return type expression.
/// The Decl is determined contextually.
node_offset_asm_ret_ty: i32,
/// The source location points to the condition expression of an if
/// expression, found by taking this AST node index offset from the containing
/// Decl AST node, which points to an if expression AST node. Next, navigate
/// to the condition expression.
/// The Decl is determined contextually.
node_offset_if_cond: i32,
/// The source location points to a binary expression, such as `a + b`, found
/// by taking this AST node index offset from the containing Decl AST node.
/// The Decl is determined contextually.
node_offset_bin_op: i32,
/// The source location points to the LHS of a binary expression, found
/// by taking this AST node index offset from the containing Decl AST node,
/// which points to a binary expression AST node. Next, navigate to the LHS.
/// The Decl is determined contextually.
node_offset_bin_lhs: i32,
/// The source location points to the RHS of a binary expression, found
/// by taking this AST node index offset from the containing Decl AST node,
/// which points to a binary expression AST node. Next, navigate to the RHS.
/// The Decl is determined contextually.
node_offset_bin_rhs: i32,
/// The source location points to the operand of a switch expression, found
/// by taking this AST node index offset from the containing Decl AST node,
/// which points to a switch expression AST node. Next, navigate to the operand.
/// The Decl is determined contextually.
node_offset_switch_operand: i32,
/// The source location points to the else/`_` prong of a switch expression, found
/// by taking this AST node index offset from the containing Decl AST node,
/// which points to a switch expression AST node. Next, navigate to the else/`_` prong.
/// The Decl is determined contextually.
node_offset_switch_special_prong: i32,
/// The source location points to all the ranges of a switch expression, found
/// by taking this AST node index offset from the containing Decl AST node,
/// which points to a switch expression AST node. Next, navigate to any of the
/// range nodes. The error applies to all of them.
/// The Decl is determined contextually.
node_offset_switch_range: i32,
/// The source location points to the capture of a switch_prong.
/// The Decl is determined contextually.
node_offset_switch_prong_capture: i32,
/// The source location points to the tag capture of a switch_prong.
/// The Decl is determined contextually.
node_offset_switch_prong_tag_capture: i32,
/// The source location points to the align expr of a function type
/// expression, found by taking this AST node index offset from the containing
/// Decl AST node, which points to a function type AST node. Next, navigate to
/// the calling convention node.
/// The Decl is determined contextually.
node_offset_fn_type_align: i32,
/// The source location points to the addrspace expr of a function type
/// expression, found by taking this AST node index offset from the containing
/// Decl AST node, which points to a function type AST node. Next, navigate to
/// the calling convention node.
/// The Decl is determined contextually.
node_offset_fn_type_addrspace: i32,
/// The source location points to the linksection expr of a function type
/// expression, found by taking this AST node index offset from the containing
/// Decl AST node, which points to a function type AST node. Next, navigate to
/// the calling convention node.
/// The Decl is determined contextually.
node_offset_fn_type_section: i32,
/// The source location points to the calling convention of a function type
/// expression, found by taking this AST node index offset from the containing
/// Decl AST node, which points to a function type AST node. Next, navigate to
/// the calling convention node.
/// The Decl is determined contextually.
node_offset_fn_type_cc: i32,
/// The source location points to the return type of a function type
/// expression, found by taking this AST node index offset from the containing
/// Decl AST node, which points to a function type AST node. Next, navigate to
/// the return type node.
/// The Decl is determined contextually.
node_offset_fn_type_ret_ty: i32,
node_offset_param: i32,
token_offset_param: i32,
/// The source location points to the type expression of an `anyframe->T`
/// expression, found by taking this AST node index offset from the containing
/// Decl AST node, which points to a `anyframe->T` expression AST node. Next, navigate
/// to the type expression.
/// The Decl is determined contextually.
node_offset_anyframe_type: i32,
/// The source location points to the string literal of `extern "foo"`, found
/// by taking this AST node index offset from the containing
/// Decl AST node, which points to a function prototype or variable declaration
/// expression AST node. Next, navigate to the string literal of the `extern "foo"`.
/// The Decl is determined contextually.
node_offset_lib_name: i32,
/// The source location points to the len expression of an `[N:S]T`
/// expression, found by taking this AST node index offset from the containing
/// Decl AST node, which points to an `[N:S]T` expression AST node. Next, navigate
/// to the len expression.
/// The Decl is determined contextually.
node_offset_array_type_len: i32,
/// The source location points to the sentinel expression of an `[N:S]T`
/// expression, found by taking this AST node index offset from the containing
/// Decl AST node, which points to an `[N:S]T` expression AST node. Next, navigate
/// to the sentinel expression.
/// The Decl is determined contextually.
node_offset_array_type_sentinel: i32,
/// The source location points to the elem expression of an `[N:S]T`
/// expression, found by taking this AST node index offset from the containing
/// Decl AST node, which points to an `[N:S]T` expression AST node. Next, navigate
/// to the elem expression.
/// The Decl is determined contextually.
node_offset_array_type_elem: i32,
/// The source location points to the operand of an unary expression.
/// The Decl is determined contextually.
node_offset_un_op: i32,
/// The source location points to the elem type of a pointer.
/// The Decl is determined contextually.
node_offset_ptr_elem: i32,
/// The source location points to the sentinel of a pointer.
/// The Decl is determined contextually.
node_offset_ptr_sentinel: i32,
/// The source location points to the align expr of a pointer.
/// The Decl is determined contextually.
node_offset_ptr_align: i32,
/// The source location points to the addrspace expr of a pointer.
/// The Decl is determined contextually.
node_offset_ptr_addrspace: i32,
/// The source location points to the bit-offset of a pointer.
/// The Decl is determined contextually.
node_offset_ptr_bitoffset: i32,
/// The source location points to the host size of a pointer.
/// The Decl is determined contextually.
node_offset_ptr_hostsize: i32,
/// The source location points to the tag type of an union or an enum.
/// The Decl is determined contextually.
node_offset_container_tag: i32,
/// The source location points to the default value of a field.
/// The Decl is determined contextually.
node_offset_field_default: i32,
/// The source location points to the type of an array or struct initializer.
/// The Decl is determined contextually.
node_offset_init_ty: i32,
/// The source location points to the LHS of an assignment.
/// The Decl is determined contextually.
node_offset_store_ptr: i32,
/// The source location points to the RHS of an assignment.
/// The Decl is determined contextually.
node_offset_store_operand: i32,
/// The source location points to the operand of a `return` statement, or
/// the `return` itself if there is no explicit operand.
/// The Decl is determined contextually.
node_offset_return_operand: i32,
/// The source location points to a for loop input.
/// The Decl is determined contextually.
for_input: struct {
/// Points to the for loop AST node.
for_node_offset: i32,
/// Picks one of the inputs from the condition.
input_index: u32,
},
/// The source location points to one of the captures of a for loop, found
/// by taking this AST node index offset from the containing
/// Decl AST node, which points to one of the input nodes of a for loop.
/// Next, navigate to the corresponding capture.
/// The Decl is determined contextually.
for_capture_from_input: i32,
/// The source location points to the argument node of a function call.
call_arg: struct {
decl: DeclIndex,
/// Points to the function call AST node.
call_node_offset: i32,
/// The index of the argument the source location points to.
arg_index: u32,
},
fn_proto_param: struct {
decl: DeclIndex,
/// Points to the function prototype AST node.
fn_proto_node_offset: i32,
/// The index of the parameter the source location points to.
param_index: u32,
},
array_cat_lhs: ArrayCat,
array_cat_rhs: ArrayCat,
const ArrayCat = struct {
/// Points to the array concat AST node.
array_cat_offset: i32,
/// The index of the element the source location points to.
elem_index: u32,
};
pub const nodeOffset = if (TracedOffset.want_tracing) nodeOffsetDebug else nodeOffsetRelease;
noinline fn nodeOffsetDebug(node_offset: i32) LazySrcLoc {
var result: LazySrcLoc = .{ .node_offset = .{ .x = node_offset } };
result.node_offset.trace.addAddr(@returnAddress(), "init");
return result;
}
fn nodeOffsetRelease(node_offset: i32) LazySrcLoc {
return .{ .node_offset = .{ .x = node_offset } };
}
/// This wraps a simple integer in debug builds so that later on we can find out
/// where in semantic analysis the value got set.
pub const TracedOffset = struct {
x: i32,
trace: std.debug.Trace = std.debug.Trace.init,
const want_tracing = false;
};
};
const std = @import("std.zig");
const tokenizer = @import("zig/tokenizer.zig");
const assert = std.debug.assert;

View File

@ -4011,7 +4011,7 @@ fn fnDecl(
// We insert this at the beginning so that its instruction index marks the
// start of the top level declaration.
const decl_inst = try gz.makeBlockInst(.declaration, fn_proto.ast.proto_node);
const decl_inst = try gz.makeDeclaration(fn_proto.ast.proto_node);
astgen.advanceSourceCursorToNode(decl_node);
var decl_gz: GenZir = .{
@ -4393,7 +4393,7 @@ fn globalVarDecl(
const is_mutable = token_tags[var_decl.ast.mut_token] == .keyword_var;
// We do this at the beginning so that the instruction index marks the range start
// of the top level declaration.
const decl_inst = try gz.makeBlockInst(.declaration, node);
const decl_inst = try gz.makeDeclaration(node);
const name_token = var_decl.ast.mut_token + 1;
astgen.advanceSourceCursorToNode(node);
@ -4555,7 +4555,7 @@ fn comptimeDecl(
// Up top so the ZIR instruction index marks the start range of this
// top-level declaration.
const decl_inst = try gz.makeBlockInst(.declaration, node);
const decl_inst = try gz.makeDeclaration(node);
wip_members.nextDecl(decl_inst);
astgen.advanceSourceCursorToNode(node);
@ -4607,7 +4607,7 @@ fn usingnamespaceDecl(
};
// Up top so the ZIR instruction index marks the start range of this
// top-level declaration.
const decl_inst = try gz.makeBlockInst(.declaration, node);
const decl_inst = try gz.makeDeclaration(node);
wip_members.nextDecl(decl_inst);
astgen.advanceSourceCursorToNode(node);
@ -4651,7 +4651,7 @@ fn testDecl(
// Up top so the ZIR instruction index marks the start range of this
// top-level declaration.
const decl_inst = try gz.makeBlockInst(.declaration, node);
const decl_inst = try gz.makeDeclaration(node);
wip_members.nextDecl(decl_inst);
astgen.advanceSourceCursorToNode(node);
@ -9366,9 +9366,10 @@ fn builtinCall(
try gz.instructions.ensureUnusedCapacity(gpa, 1);
try gz.astgen.instructions.ensureUnusedCapacity(gpa, 1);
const payload_index = try gz.astgen.addExtra(Zir.Inst.UnNode{
.node = gz.nodeIndexToRelative(node),
const payload_index = try gz.astgen.addExtra(Zir.Inst.Reify{
.node = node, // Absolute node index -- see the definition of `Reify`.
.operand = operand,
.src_line = astgen.source_line,
});
const new_index: Zir.Inst.Index = @enumFromInt(gz.astgen.instructions.len);
gz.astgen.instructions.appendAssumeCapacity(.{
@ -13076,6 +13077,21 @@ const GenZir = struct {
return new_index;
}
/// Note that this returns a `Zir.Inst.Index` not a ref.
/// Does *not* append the block instruction to the scope.
/// Leaves the `payload_index` field undefined. Use `setDeclaration` to finalize.
fn makeDeclaration(gz: *GenZir, node: Ast.Node.Index) !Zir.Inst.Index {
const new_index: Zir.Inst.Index = @enumFromInt(gz.astgen.instructions.len);
try gz.astgen.instructions.append(gz.astgen.gpa, .{
.tag = .declaration,
.data = .{ .declaration = .{
.src_node = node,
.payload_index = undefined,
} },
});
return new_index;
}
/// Note that this returns a `Zir.Inst.Index` not a ref.
/// Leaves the `payload_index` field undefined.
fn addCondBr(gz: *GenZir, tag: Zir.Inst.Tag, node: Ast.Node.Index) !Zir.Inst.Index {
@ -13122,7 +13138,8 @@ const GenZir = struct {
.fields_hash_1 = fields_hash_arr[1],
.fields_hash_2 = fields_hash_arr[2],
.fields_hash_3 = fields_hash_arr[3],
.src_node = gz.nodeIndexToRelative(args.src_node),
.src_line = astgen.source_line,
.src_node = args.src_node,
});
if (args.captures_len != 0) {
@ -13182,7 +13199,8 @@ const GenZir = struct {
.fields_hash_1 = fields_hash_arr[1],
.fields_hash_2 = fields_hash_arr[2],
.fields_hash_3 = fields_hash_arr[3],
.src_node = gz.nodeIndexToRelative(args.src_node),
.src_line = astgen.source_line,
.src_node = args.src_node,
});
if (args.tag_type != .none) {
@ -13243,7 +13261,8 @@ const GenZir = struct {
.fields_hash_1 = fields_hash_arr[1],
.fields_hash_2 = fields_hash_arr[2],
.fields_hash_3 = fields_hash_arr[3],
.src_node = gz.nodeIndexToRelative(args.src_node),
.src_line = astgen.source_line,
.src_node = args.src_node,
});
if (args.tag_type != .none) {
@ -13291,7 +13310,8 @@ const GenZir = struct {
try astgen.extra.ensureUnusedCapacity(gpa, @typeInfo(Zir.Inst.OpaqueDecl).Struct.fields.len + 2);
const payload_index = astgen.addExtraAssumeCapacity(Zir.Inst.OpaqueDecl{
.src_node = gz.nodeIndexToRelative(args.src_node),
.src_line = astgen.source_line,
.src_node = args.src_node,
});
if (args.captures_len != 0) {
@ -13902,7 +13922,7 @@ fn setDeclaration(
.has_align_linksection_addrspace = align_len != 0 or linksection_len != 0 or addrspace_len != 0,
},
};
astgen.instructions.items(.data)[@intFromEnum(decl_inst)].pl_node.payload_index = try astgen.addExtra(extra);
astgen.instructions.items(.data)[@intFromEnum(decl_inst)].declaration.payload_index = try astgen.addExtra(extra);
if (extra.flags.has_doc_comment) {
try astgen.extra.append(gpa, @intFromEnum(true_doc_comment));
}

View File

@ -20,7 +20,6 @@ const BigIntMutable = std.math.big.int.Mutable;
const Ast = std.zig.Ast;
const Zir = @This();
const LazySrcLoc = std.zig.LazySrcLoc;
instructions: std.MultiArrayList(Inst).Slice,
/// In order to store references to strings in fewer bytes, we copy all
@ -287,7 +286,7 @@ pub const Inst = struct {
/// namespace type, e.g. within a `struct_decl` instruction. It represents a
/// single source declaration (`const`/`var`/`fn`), containing the name,
/// attributes, type, and value of the declaration.
/// Uses the `pl_node` union field. Payload is `Declaration`.
/// Uses the `declaration` union field. Payload is `Declaration`.
declaration,
/// Implements `suspend {...}`.
/// Uses the `pl_node` union field. Payload is `Block`.
@ -1596,7 +1595,7 @@ pub const Inst = struct {
.block = .pl_node,
.block_comptime = .pl_node,
.block_inline = .pl_node,
.declaration = .pl_node,
.declaration = .declaration,
.suspend_block = .pl_node,
.bool_not = .un_node,
.bool_br_and = .pl_node,
@ -1982,7 +1981,7 @@ pub const Inst = struct {
/// `operand` is payload index to `UnNode`.
error_from_int,
/// Implement builtin `@Type`.
/// `operand` is payload index to `UnNode`.
/// `operand` is payload index to `Reify`.
/// `small` contains `NameStrategy`.
reify,
/// Implements the `@asyncCall` builtin.
@ -2221,10 +2220,6 @@ pub const Inst = struct {
src_node: i32,
/// The meaning of this operand depends on the corresponding `Tag`.
operand: Ref,
pub fn src(self: @This()) LazySrcLoc {
return LazySrcLoc.nodeOffset(self.src_node);
}
},
/// Used for unary operators, with a token source location.
un_tok: struct {
@ -2232,10 +2227,6 @@ pub const Inst = struct {
src_tok: Ast.TokenIndex,
/// The meaning of this operand depends on the corresponding `Tag`.
operand: Ref,
pub fn src(self: @This()) LazySrcLoc {
return .{ .token_offset = self.src_tok };
}
},
pl_node: struct {
/// Offset from Decl AST node index.
@ -2244,10 +2235,6 @@ pub const Inst = struct {
/// index into extra.
/// `Tag` determines what lives there.
payload_index: u32,
pub fn src(self: @This()) LazySrcLoc {
return LazySrcLoc.nodeOffset(self.src_node);
}
},
pl_tok: struct {
/// Offset from Decl AST token index.
@ -2255,10 +2242,6 @@ pub const Inst = struct {
/// index into extra.
/// `Tag` determines what lives there.
payload_index: u32,
pub fn src(self: @This()) LazySrcLoc {
return .{ .token_offset = self.src_tok };
}
},
bin: Bin,
/// For strings which may contain null bytes.
@ -2281,10 +2264,6 @@ pub const Inst = struct {
pub fn get(self: @This(), code: Zir) [:0]const u8 {
return code.nullTerminatedString(self.start);
}
pub fn src(self: @This()) LazySrcLoc {
return .{ .token_offset = self.src_tok };
}
},
/// Offset from Decl AST token index.
tok: Ast.TokenIndex,
@ -2313,19 +2292,11 @@ pub const Inst = struct {
src_node: i32,
signedness: std.builtin.Signedness,
bit_count: u16,
pub fn src(self: @This()) LazySrcLoc {
return LazySrcLoc.nodeOffset(self.src_node);
}
},
@"unreachable": struct {
/// Offset from Decl AST node index.
/// `Tag` determines which kind of AST node this points to.
src_node: i32,
pub fn src(self: @This()) LazySrcLoc {
return LazySrcLoc.nodeOffset(self.src_node);
}
},
@"break": struct {
operand: Ref,
@ -2339,10 +2310,6 @@ pub const Inst = struct {
src_node: i32,
/// The meaning of this operand depends on the corresponding `Tag`.
inst: Index,
pub fn src(self: @This()) LazySrcLoc {
return LazySrcLoc.nodeOffset(self.src_node);
}
},
str_op: struct {
/// Offset into `string_bytes`. Null-terminated.
@ -2370,6 +2337,12 @@ pub const Inst = struct {
/// The index being accessed.
idx: u32,
},
declaration: struct {
/// This node provides a new absolute baseline node for all instructions within this struct.
src_node: Ast.Node.Index,
/// index into extra to a `Declaration` payload.
payload_index: u32,
},
// Make sure we don't accidentally add a field to make this union
// bigger than expected. Note that in Debug builds, Zig is allowed
@ -2408,6 +2381,7 @@ pub const Inst = struct {
defer_err_code,
save_err_ret_index,
elem_val_imm,
declaration,
};
};
@ -2860,6 +2834,14 @@ pub const Inst = struct {
index: u32,
};
pub const Reify = struct {
/// This node is absolute, because `reify` instructions are tracked across updates, and
/// this simplifies the logic for getting source locations for types.
node: Ast.Node.Index,
operand: Ref,
src_line: u32,
};
pub const SwitchBlockErrUnion = struct {
operand: Ref,
bits: Bits,
@ -3018,11 +3000,9 @@ pub const Inst = struct {
fields_hash_1: u32,
fields_hash_2: u32,
fields_hash_3: u32,
src_node: i32,
pub fn src(self: StructDecl) LazySrcLoc {
return LazySrcLoc.nodeOffset(self.src_node);
}
src_line: u32,
/// This node provides a new absolute baseline node for all instructions within this struct.
src_node: Ast.Node.Index,
pub const Small = packed struct {
has_captures_len: bool,
@ -3150,11 +3130,9 @@ pub const Inst = struct {
fields_hash_1: u32,
fields_hash_2: u32,
fields_hash_3: u32,
src_node: i32,
pub fn src(self: EnumDecl) LazySrcLoc {
return LazySrcLoc.nodeOffset(self.src_node);
}
src_line: u32,
/// This node provides a new absolute baseline node for all instructions within this struct.
src_node: Ast.Node.Index,
pub const Small = packed struct {
has_tag_type: bool,
@ -3198,11 +3176,9 @@ pub const Inst = struct {
fields_hash_1: u32,
fields_hash_2: u32,
fields_hash_3: u32,
src_node: i32,
pub fn src(self: UnionDecl) LazySrcLoc {
return LazySrcLoc.nodeOffset(self.src_node);
}
src_line: u32,
/// This node provides a new absolute baseline node for all instructions within this struct.
src_node: Ast.Node.Index,
pub const Small = packed struct {
has_tag_type: bool,
@ -3230,11 +3206,9 @@ pub const Inst = struct {
/// 2. capture: Capture, // for every captures_len
/// 3. decl: Index, // for every decls_len; points to a `declaration` instruction
pub const OpaqueDecl = struct {
src_node: i32,
pub fn src(self: OpaqueDecl) LazySrcLoc {
return LazySrcLoc.nodeOffset(self.src_node);
}
src_line: u32,
/// This node provides a new absolute baseline node for all instructions within this struct.
src_node: Ast.Node.Index,
pub const Small = packed struct {
has_captures_len: bool,
@ -3352,10 +3326,6 @@ pub const Inst = struct {
parent_ptr_type: Ref,
field_name: Ref,
field_ptr: Ref,
pub fn src(self: FieldParentPtr) LazySrcLoc {
return LazySrcLoc.nodeOffset(self.src_node);
}
};
pub const Shuffle = struct {
@ -3505,10 +3475,6 @@ pub const Inst = struct {
block: Ref,
/// If `.none`, restore unconditionally.
operand: Ref,
pub fn src(self: RestoreErrRetIndex) LazySrcLoc {
return LazySrcLoc.nodeOffset(self.src_node);
}
};
};
@ -3772,6 +3738,7 @@ fn findDeclsInner(
.union_decl,
.enum_decl,
.opaque_decl,
.reify,
=> return list.append(inst),
else => return,
@ -4046,7 +4013,7 @@ pub fn getFnInfo(zir: Zir, fn_inst: Inst.Index) FnInfo {
pub fn getDeclaration(zir: Zir, inst: Zir.Inst.Index) struct { Inst.Declaration, u32 } {
assert(zir.instructions.items(.tag)[@intFromEnum(inst)] == .declaration);
const pl_node = zir.instructions.items(.data)[@intFromEnum(inst)].pl_node;
const pl_node = zir.instructions.items(.data)[@intFromEnum(inst)].declaration;
const extra = zir.extraData(Inst.Declaration, pl_node.payload_index);
return .{
extra.data,

View File

@ -2639,7 +2639,7 @@ fn reportMultiModuleErrors(mod: *Module) !void {
.root => |pkg| blk: {
break :blk try Module.ErrorMsg.init(
mod.gpa,
.{ .file_scope = file, .parent_decl_node = 0, .lazy = .entire_file },
.{ .file_scope = file, .base_node = 0, .lazy = .entire_file },
"root of module {s}",
.{pkg.fully_qualified_name},
);
@ -2651,7 +2651,7 @@ fn reportMultiModuleErrors(mod: *Module) !void {
if (omitted > 0) {
notes[num_notes] = try Module.ErrorMsg.init(
mod.gpa,
.{ .file_scope = file, .parent_decl_node = 0, .lazy = .entire_file },
.{ .file_scope = file, .base_node = 0, .lazy = .entire_file },
"{} more references omitted",
.{omitted},
);
@ -2660,7 +2660,7 @@ fn reportMultiModuleErrors(mod: *Module) !void {
const err = try Module.ErrorMsg.create(
mod.gpa,
.{ .file_scope = file, .parent_decl_node = 0, .lazy = .entire_file },
.{ .file_scope = file, .base_node = 0, .lazy = .entire_file },
"file exists in multiple modules",
.{},
);
@ -3040,29 +3040,26 @@ pub fn getAllErrorsAlloc(comp: *Compilation) !ErrorBundle {
}
}
if (comp.module) |module| {
if (bundle.root_list.items.len == 0 and module.compile_log_decls.count() != 0) {
const keys = module.compile_log_decls.keys();
const values = module.compile_log_decls.values();
if (comp.module) |zcu| {
if (bundle.root_list.items.len == 0 and zcu.compile_log_decls.count() != 0) {
const values = zcu.compile_log_decls.values();
// First one will be the error; subsequent ones will be notes.
const err_decl = module.declPtr(keys[0]);
const src_loc = err_decl.nodeOffsetSrcLoc(values[0], module);
const err_msg = Module.ErrorMsg{
const src_loc = values[0].src().upgrade(zcu);
const err_msg: Module.ErrorMsg = .{
.src_loc = src_loc,
.msg = "found compile log statement",
.notes = try gpa.alloc(Module.ErrorMsg, module.compile_log_decls.count() - 1),
.notes = try gpa.alloc(Module.ErrorMsg, zcu.compile_log_decls.count() - 1),
};
defer gpa.free(err_msg.notes);
for (keys[1..], 0..) |key, i| {
const note_decl = module.declPtr(key);
err_msg.notes[i] = .{
.src_loc = note_decl.nodeOffsetSrcLoc(values[i + 1], module),
for (values[1..], err_msg.notes) |src_info, *note| {
note.* = .{
.src_loc = src_info.src().upgrade(zcu),
.msg = "also here",
};
}
try addModuleErrorMsg(module, &bundle, err_msg);
try addModuleErrorMsg(zcu, &bundle, err_msg);
}
}
@ -3492,7 +3489,7 @@ fn processOneJob(comp: *Compilation, job: Job, prog_node: std.Progress.Node) !vo
try module.failed_decls.ensureUnusedCapacity(gpa, 1);
module.failed_decls.putAssumeCapacityNoClobber(decl_index, try Module.ErrorMsg.create(
gpa,
decl.srcLoc(module),
decl.navSrcLoc(module).upgrade(module),
"unable to update line number: {s}",
.{@errorName(err)},
));
@ -3993,7 +3990,7 @@ fn workerAstGenFile(
if (!res.is_pkg) {
res.file.addReference(mod.*, .{ .import = .{
.file_scope = file,
.parent_decl_node = 0,
.base_node = 0,
.lazy = .{ .token_abs = item.data.token },
} }) catch continue;
}
@ -4370,7 +4367,7 @@ fn reportRetryableAstGenError(
const src_loc: Module.SrcLoc = switch (src) {
.root => .{
.file_scope = file,
.parent_decl_node = 0,
.base_node = 0,
.lazy = .entire_file,
},
.import => |info| blk: {
@ -4378,7 +4375,7 @@ fn reportRetryableAstGenError(
break :blk .{
.file_scope = importing_file,
.parent_decl_node = 0,
.base_node = 0,
.lazy = .{ .token_abs = info.import_tok },
};
},

View File

@ -101,8 +101,11 @@ pub const TrackedInst = extern struct {
}
pub const Index = enum(u32) {
_,
pub fn resolveFull(i: TrackedInst.Index, ip: *const InternPool) TrackedInst {
return ip.tracked_insts.keys()[@intFromEnum(i)];
}
pub fn resolve(i: TrackedInst.Index, ip: *const InternPool) Zir.Inst.Index {
return ip.tracked_insts.keys()[@intFromEnum(i)].inst;
return i.resolveFull(ip).inst;
}
pub fn toOptional(i: TrackedInst.Index) Optional {
return @enumFromInt(@intFromEnum(i));
@ -391,8 +394,27 @@ pub const RuntimeIndex = enum(u32) {
pub const ComptimeAllocIndex = enum(u32) { _ };
pub const DeclIndex = std.zig.DeclIndex;
pub const OptionalDeclIndex = std.zig.OptionalDeclIndex;
pub const DeclIndex = enum(u32) {
_,
pub fn toOptional(i: DeclIndex) OptionalDeclIndex {
return @enumFromInt(@intFromEnum(i));
}
};
pub const OptionalDeclIndex = enum(u32) {
none = std.math.maxInt(u32),
_,
pub fn init(oi: ?DeclIndex) OptionalDeclIndex {
return @enumFromInt(@intFromEnum(oi orelse return .none));
}
pub fn unwrap(oi: OptionalDeclIndex) ?DeclIndex {
if (oi == .none) return null;
return @enumFromInt(@intFromEnum(oi));
}
};
pub const NamespaceIndex = enum(u32) {
_,
@ -6935,7 +6957,6 @@ fn finishFuncInstance(
const decl_index = try ip.createDecl(gpa, .{
.name = undefined,
.src_namespace = fn_owner_decl.src_namespace,
.src_node = fn_owner_decl.src_node,
.src_line = fn_owner_decl.src_line,
.has_tv = true,
.owns_tv = true,

File diff suppressed because it is too large Load Diff

View File

@ -7,7 +7,7 @@ const Type = @import("type.zig").Type;
const Value = @import("Value.zig");
const Module = @import("Module.zig");
const RangeSet = @This();
const SwitchProngSrc = @import("Module.zig").SwitchProngSrc;
const LazySrcLoc = @import("Module.zig").LazySrcLoc;
ranges: std.ArrayList(Range),
module: *Module,
@ -15,7 +15,7 @@ module: *Module,
pub const Range = struct {
first: InternPool.Index,
last: InternPool.Index,
src: SwitchProngSrc,
src: LazySrcLoc,
};
pub fn init(allocator: std.mem.Allocator, module: *Module) RangeSet {
@ -33,8 +33,8 @@ pub fn add(
self: *RangeSet,
first: InternPool.Index,
last: InternPool.Index,
src: SwitchProngSrc,
) !?SwitchProngSrc {
src: LazySrcLoc,
) !?LazySrcLoc {
const mod = self.module;
const ip = &mod.intern_pool;

File diff suppressed because it is too large Load Diff

View File

@ -1025,18 +1025,18 @@ fn checkComptimeVarStore(
if (@intFromEnum(runtime_index) < @intFromEnum(block.runtime_index)) {
if (block.runtime_cond) |cond_src| {
const msg = msg: {
const msg = try sema.errMsg(block, src, "store to comptime variable depends on runtime condition", .{});
const msg = try sema.errMsg(src, "store to comptime variable depends on runtime condition", .{});
errdefer msg.destroy(sema.gpa);
try sema.mod.errNoteNonLazy(cond_src, msg, "runtime condition here", .{});
try sema.errNote(cond_src, msg, "runtime condition here", .{});
break :msg msg;
};
return sema.failWithOwnedErrorMsg(block, msg);
}
if (block.runtime_loop) |loop_src| {
const msg = msg: {
const msg = try sema.errMsg(block, src, "cannot store to comptime variable in non-inline loop", .{});
const msg = try sema.errMsg(src, "cannot store to comptime variable in non-inline loop", .{});
errdefer msg.destroy(sema.gpa);
try sema.mod.errNoteNonLazy(loop_src, msg, "non-inline loop here", .{});
try sema.errNote(loop_src, msg, "non-inline loop here", .{});
break :msg msg;
};
return sema.failWithOwnedErrorMsg(block, msg);
@ -1048,7 +1048,6 @@ fn checkComptimeVarStore(
const std = @import("std");
const assert = std.debug.assert;
const Allocator = std.mem.Allocator;
const LazySrcLoc = std.zig.LazySrcLoc;
const InternPool = @import("../InternPool.zig");
const ComptimeAllocIndex = InternPool.ComptimeAllocIndex;
@ -1057,3 +1056,5 @@ const Block = Sema.Block;
const MutableValue = @import("../mutable_value.zig").MutableValue;
const Type = @import("../type.zig").Type;
const Value = @import("../Value.zig");
const Zcu = @import("../Module.zig");
const LazySrcLoc = Zcu.LazySrcLoc;

View File

@ -4014,7 +4014,6 @@ pub fn pointerDerivation(ptr_val: Value, arena: Allocator, zcu: *Zcu) Allocator.
return ptr_val.pointerDerivationAdvanced(arena, zcu, null) catch |err| switch (err) {
error.OutOfMemory => |e| return e,
error.AnalysisFail,
error.NeededSourceLocation,
error.GenericPoison,
error.ComptimeReturn,
error.ComptimeBreak,

View File

@ -16,7 +16,6 @@ const Decl = Module.Decl;
const Type = @import("../../type.zig").Type;
const Value = @import("../../Value.zig");
const Compilation = @import("../../Compilation.zig");
const LazySrcLoc = std.zig.LazySrcLoc;
const link = @import("../../link.zig");
const Air = @import("../../Air.zig");
const Liveness = @import("../../Liveness.zig");
@ -766,7 +765,7 @@ pub fn deinit(func: *CodeGen) void {
/// Sets `err_msg` on `CodeGen` and returns `error.CodegenFail` which is caught in link/Wasm.zig
fn fail(func: *CodeGen, comptime fmt: []const u8, args: anytype) InnerError {
const mod = func.bin_file.base.comp.module.?;
const src_loc = func.decl.srcLoc(mod);
const src_loc = func.decl.navSrcLoc(mod).upgrade(mod);
func.err_msg = try Module.ErrorMsg.create(func.gpa, src_loc, fmt, args);
return error.CodegenFail;
}
@ -3123,7 +3122,7 @@ fn lowerAnonDeclRef(
}
const decl_align = mod.intern_pool.indexToKey(anon_decl.orig_ty).ptr_type.flags.alignment;
const res = try func.bin_file.lowerAnonDecl(decl_val, decl_align, func.decl.srcLoc(mod));
const res = try func.bin_file.lowerAnonDecl(decl_val, decl_align, func.decl.navSrcLoc(mod).upgrade(mod));
switch (res) {
.ok => {},
.fail => |em| {

View File

@ -257,7 +257,7 @@ fn fail(emit: *Emit, comptime format: []const u8, args: anytype) InnerError {
const comp = emit.bin_file.base.comp;
const zcu = comp.module.?;
const gpa = comp.gpa;
emit.error_msg = try Module.ErrorMsg.create(gpa, zcu.declPtr(emit.decl_index).srcLoc(zcu), format, args);
emit.error_msg = try Module.ErrorMsg.create(gpa, zcu.declPtr(emit.decl_index).navSrcLoc(zcu).upgrade(zcu), format, args);
return error.EmitFail;
}

View File

@ -13,7 +13,6 @@ const Type = @import("../type.zig").Type;
const C = link.File.C;
const Decl = Zcu.Decl;
const trace = @import("../tracy.zig").trace;
const LazySrcLoc = std.zig.LazySrcLoc;
const Air = @import("../Air.zig");
const Liveness = @import("../Liveness.zig");
const InternPool = @import("../InternPool.zig");
@ -638,7 +637,7 @@ pub const DeclGen = struct {
const zcu = dg.zcu;
const decl_index = dg.pass.decl;
const decl = zcu.declPtr(decl_index);
const src_loc = decl.srcLoc(zcu);
const src_loc = decl.navSrcLoc(zcu).upgrade(zcu);
dg.error_msg = try Zcu.ErrorMsg.create(dg.gpa, src_loc, format, args);
return error.AnalysisFail;
}

View File

@ -2581,8 +2581,8 @@ pub const AlignAs = packed struct {
const Alignment = @import("../../InternPool.zig").Alignment;
const assert = std.debug.assert;
const CType = @This();
const DeclIndex = std.zig.DeclIndex;
const Module = @import("../../Package/Module.zig");
const std = @import("std");
const Type = @import("../../type.zig").Type;
const Zcu = @import("../../Module.zig");
const DeclIndex = @import("../../InternPool.zig").DeclIndex;

View File

@ -22,7 +22,6 @@ const Air = @import("../Air.zig");
const Liveness = @import("../Liveness.zig");
const Value = @import("../Value.zig");
const Type = @import("../type.zig").Type;
const LazySrcLoc = std.zig.LazySrcLoc;
const x86_64_abi = @import("../arch/x86_64/abi.zig");
const wasm_c_abi = @import("../arch/wasm/abi.zig");
const aarch64_c_abi = @import("../arch/aarch64/abi.zig");
@ -2067,7 +2066,7 @@ pub const Object = struct {
try o.builder.metadataString(name),
file,
scope,
owner_decl.src_node + 1, // Line
owner_decl.src_line + 1, // Line
try o.lowerDebugType(int_ty),
ty.abiSize(mod) * 8,
(ty.abiAlignment(mod).toByteUnits() orelse 0) * 8,
@ -2237,7 +2236,7 @@ pub const Object = struct {
try o.builder.metadataString(name),
try o.getDebugFile(mod.namespacePtr(owner_decl.src_namespace).file_scope),
try o.namespaceToDebugScope(owner_decl.src_namespace),
owner_decl.src_node + 1, // Line
owner_decl.src_line + 1, // Line
.none, // Underlying type
0, // Size
0, // Align
@ -4729,7 +4728,7 @@ pub const DeclGen = struct {
const o = dg.object;
const gpa = o.gpa;
const mod = o.module;
const src_loc = dg.decl.srcLoc(mod);
const src_loc = dg.decl.navSrcLoc(mod).upgrade(mod);
dg.err_msg = try Module.ErrorMsg.create(gpa, src_loc, "TODO (LLVM): " ++ format, args);
return error.CodegenFail;
}

View File

@ -9,7 +9,6 @@ const Module = @import("../Module.zig");
const Decl = Module.Decl;
const Type = @import("../type.zig").Type;
const Value = @import("../Value.zig");
const LazySrcLoc = std.zig.LazySrcLoc;
const Air = @import("../Air.zig");
const Liveness = @import("../Liveness.zig");
const InternPool = @import("../InternPool.zig");
@ -414,7 +413,7 @@ const DeclGen = struct {
pub fn fail(self: *DeclGen, comptime format: []const u8, args: anytype) Error {
@setCold(true);
const mod = self.module;
const src_loc = self.module.declPtr(self.decl_index).srcLoc(mod);
const src_loc = self.module.declPtr(self.decl_index).navSrcLoc(mod).upgrade(mod);
assert(self.error_msg == null);
self.error_msg = try Module.ErrorMsg.create(self.module.gpa, src_loc, format, args);
return error.CodegenFail;
@ -6438,7 +6437,7 @@ const DeclGen = struct {
// TODO: Translate proper error locations.
assert(as.errors.items.len != 0);
assert(self.error_msg == null);
const src_loc = self.module.declPtr(self.decl_index).srcLoc(mod);
const src_loc = self.module.declPtr(self.decl_index).navSrcLoc(mod).upgrade(mod);
self.error_msg = try Module.ErrorMsg.create(self.module.gpa, src_loc, "failed to assemble SPIR-V inline assembly", .{});
const notes = try self.module.gpa.alloc(Module.ErrorMsg, as.errors.items.len);

View File

@ -10,6 +10,7 @@ const native_os = builtin.os.tag;
const Module = @import("Module.zig");
const Sema = @import("Sema.zig");
const InternPool = @import("InternPool.zig");
const Zir = std.zig.Zir;
const Decl = Module.Decl;
@ -76,18 +77,19 @@ fn dumpStatusReport() !void {
const stderr = io.getStdErr().writer();
const block: *Sema.Block = anal.block;
const mod = anal.sema.mod;
const block_src_decl = mod.declPtr(block.src_decl);
const file, const src_base_node = Module.LazySrcLoc.resolveBaseNode(block.src_base_inst, mod);
try stderr.writeAll("Analyzing ");
try writeFullyQualifiedDeclWithFile(mod, block_src_decl, stderr);
try writeFilePath(file, stderr);
try stderr.writeAll("\n");
print_zir.renderInstructionContext(
allocator,
anal.body,
anal.body_index,
mod.namespacePtr(block.namespace).file_scope,
block_src_decl.src_node,
file,
src_base_node,
6, // indent
stderr,
) catch |err| switch (err) {
@ -95,21 +97,21 @@ fn dumpStatusReport() !void {
else => |e| return e,
};
try stderr.writeAll(" For full context, use the command\n zig ast-check -t ");
try writeFilePath(mod.namespacePtr(block.namespace).file_scope, stderr);
try writeFilePath(file, stderr);
try stderr.writeAll("\n\n");
var parent = anal.parent;
while (parent) |curr| {
fba.reset();
try stderr.writeAll(" in ");
const curr_block_src_decl = mod.declPtr(curr.block.src_decl);
try writeFullyQualifiedDeclWithFile(mod, curr_block_src_decl, stderr);
const cur_block_file, const cur_block_src_base_node = Module.LazySrcLoc.resolveBaseNode(curr.block.src_base_inst, mod);
try writeFilePath(cur_block_file, stderr);
try stderr.writeAll("\n > ");
print_zir.renderSingleInstruction(
allocator,
curr.body[curr.body_index],
mod.namespacePtr(curr.block.namespace).file_scope,
curr_block_src_decl.src_node,
cur_block_file,
cur_block_src_base_node,
6, // indent
stderr,
) catch |err| switch (err) {
@ -138,12 +140,6 @@ fn writeFilePath(file: *Module.File, writer: anytype) !void {
try writer.writeAll(file.sub_file_path);
}
fn writeFullyQualifiedDeclWithFile(mod: *Module, decl: *Decl, writer: anytype) !void {
try writeFilePath(decl.getFileScope(mod), writer);
try writer.writeAll(": ");
try decl.renderFullyQualifiedDebugName(mod, writer);
}
pub fn compilerPanic(msg: []const u8, error_return_trace: ?*std.builtin.StackTrace, maybe_ret_addr: ?usize) noreturn {
PanicSwitch.preDispatch();
@setCold(true);

View File

@ -1144,7 +1144,7 @@ pub fn updateFunc(self: *Coff, mod: *Module, func_index: InternPool.Index, air:
const res = try codegen.generateFunction(
&self.base,
decl.srcLoc(mod),
decl.navSrcLoc(mod).upgrade(mod),
func_index,
air,
liveness,
@ -1181,7 +1181,7 @@ pub fn lowerUnnamedConst(self: *Coff, val: Value, decl_index: InternPool.DeclInd
const sym_name = try std.fmt.allocPrint(gpa, "__unnamed_{}_{d}", .{ decl_name.fmt(&mod.intern_pool), index });
defer gpa.free(sym_name);
const ty = val.typeOf(mod);
const atom_index = switch (try self.lowerConst(sym_name, val, ty.abiAlignment(mod), self.rdata_section_index.?, decl.srcLoc(mod))) {
const atom_index = switch (try self.lowerConst(sym_name, val, ty.abiAlignment(mod), self.rdata_section_index.?, decl.navSrcLoc(mod).upgrade(mod))) {
.ok => |atom_index| atom_index,
.fail => |em| {
decl.analysis = .codegen_failure;
@ -1272,7 +1272,7 @@ pub fn updateDecl(
defer code_buffer.deinit();
const decl_val = if (decl.val.getVariable(mod)) |variable| Value.fromInterned(variable.init) else decl.val;
const res = try codegen.generateSymbol(&self.base, decl.srcLoc(mod), decl_val, &code_buffer, .none, .{
const res = try codegen.generateSymbol(&self.base, decl.navSrcLoc(mod).upgrade(mod), decl_val, &code_buffer, .none, .{
.parent_atom_index = atom.getSymbolIndex().?,
});
const code = switch (res) {
@ -1313,12 +1313,12 @@ fn updateLazySymbolAtom(
const atom = self.getAtomPtr(atom_index);
const local_sym_index = atom.getSymbolIndex().?;
const src = if (sym.ty.getOwnerDeclOrNull(mod)) |owner_decl|
mod.declPtr(owner_decl).srcLoc(mod)
const src = if (sym.ty.srcLocOrNull(mod)) |src|
src.upgrade(mod)
else
Module.SrcLoc{
.file_scope = undefined,
.parent_decl_node = undefined,
.base_node = undefined,
.lazy = .unneeded,
};
const res = try codegen.generateLazySymbol(

View File

@ -1074,7 +1074,7 @@ pub fn updateFunc(
const res = if (decl_state) |*ds|
try codegen.generateFunction(
&elf_file.base,
decl.srcLoc(mod),
decl.navSrcLoc(mod).upgrade(mod),
func_index,
air,
liveness,
@ -1084,7 +1084,7 @@ pub fn updateFunc(
else
try codegen.generateFunction(
&elf_file.base,
decl.srcLoc(mod),
decl.navSrcLoc(mod).upgrade(mod),
func_index,
air,
liveness,
@ -1158,13 +1158,13 @@ pub fn updateDecl(
// TODO implement .debug_info for global variables
const decl_val = if (decl.val.getVariable(mod)) |variable| Value.fromInterned(variable.init) else decl.val;
const res = if (decl_state) |*ds|
try codegen.generateSymbol(&elf_file.base, decl.srcLoc(mod), decl_val, &code_buffer, .{
try codegen.generateSymbol(&elf_file.base, decl.navSrcLoc(mod).upgrade(mod), decl_val, &code_buffer, .{
.dwarf = ds,
}, .{
.parent_atom_index = sym_index,
})
else
try codegen.generateSymbol(&elf_file.base, decl.srcLoc(mod), decl_val, &code_buffer, .none, .{
try codegen.generateSymbol(&elf_file.base, decl.navSrcLoc(mod).upgrade(mod), decl_val, &code_buffer, .none, .{
.parent_atom_index = sym_index,
});
@ -1221,12 +1221,12 @@ fn updateLazySymbol(
break :blk try self.strtab.insert(gpa, name);
};
const src = if (sym.ty.getOwnerDeclOrNull(mod)) |owner_decl|
mod.declPtr(owner_decl).srcLoc(mod)
const src = if (sym.ty.srcLocOrNull(mod)) |src|
src.upgrade(mod)
else
Module.SrcLoc{
.file_scope = undefined,
.parent_decl_node = undefined,
.base_node = undefined,
.lazy = .unneeded,
};
const res = try codegen.generateLazySymbol(
@ -1306,7 +1306,7 @@ pub fn lowerUnnamedConst(
val,
ty.abiAlignment(mod),
elf_file.zig_data_rel_ro_section_index.?,
decl.srcLoc(mod),
decl.navSrcLoc(mod).upgrade(mod),
)) {
.ok => |sym_index| sym_index,
.fail => |em| {

View File

@ -682,7 +682,7 @@ pub fn updateFunc(
const dio: codegen.DebugInfoOutput = if (decl_state) |*ds| .{ .dwarf = ds } else .none;
const res = try codegen.generateFunction(
&macho_file.base,
decl.srcLoc(mod),
decl.navSrcLoc(mod).upgrade(mod),
func_index,
air,
liveness,
@ -756,7 +756,7 @@ pub fn updateDecl(
const decl_val = if (decl.val.getVariable(mod)) |variable| Value.fromInterned(variable.init) else decl.val;
const dio: codegen.DebugInfoOutput = if (decl_state) |*ds| .{ .dwarf = ds } else .none;
const res = try codegen.generateSymbol(&macho_file.base, decl.srcLoc(mod), decl_val, &code_buffer, dio, .{
const res = try codegen.generateSymbol(&macho_file.base, decl.navSrcLoc(mod).upgrade(mod), decl_val, &code_buffer, dio, .{
.parent_atom_index = sym_index,
});
@ -1104,7 +1104,7 @@ pub fn lowerUnnamedConst(
val,
val.typeOf(mod).abiAlignment(mod),
macho_file.zig_const_sect_index.?,
decl.srcLoc(mod),
decl.navSrcLoc(mod).upgrade(mod),
)) {
.ok => |sym_index| sym_index,
.fail => |em| {
@ -1294,12 +1294,12 @@ fn updateLazySymbol(
break :blk try self.strtab.insert(gpa, name);
};
const src = if (lazy_sym.ty.getOwnerDeclOrNull(mod)) |owner_decl|
mod.declPtr(owner_decl).srcLoc(mod)
const src = if (lazy_sym.ty.srcLocOrNull(mod)) |src|
src.upgrade(mod)
else
Module.SrcLoc{
.file_scope = undefined,
.parent_decl_node = undefined,
.base_node = undefined,
.lazy = .unneeded,
};
const res = try codegen.generateLazySymbol(

View File

@ -433,7 +433,7 @@ pub fn updateFunc(self: *Plan9, mod: *Module, func_index: InternPool.Index, air:
const res = try codegen.generateFunction(
&self.base,
decl.srcLoc(mod),
decl.navSrcLoc(mod).upgrade(mod),
func_index,
air,
liveness,
@ -499,7 +499,7 @@ pub fn lowerUnnamedConst(self: *Plan9, val: Value, decl_index: InternPool.DeclIn
};
self.syms.items[info.sym_index.?] = sym;
const res = try codegen.generateSymbol(&self.base, decl.srcLoc(mod), val, &code_buffer, .{
const res = try codegen.generateSymbol(&self.base, decl.navSrcLoc(mod).upgrade(mod), val, &code_buffer, .{
.none = {},
}, .{
.parent_atom_index = new_atom_idx,
@ -538,7 +538,7 @@ pub fn updateDecl(self: *Plan9, mod: *Module, decl_index: InternPool.DeclIndex)
defer code_buffer.deinit();
const decl_val = if (decl.val.getVariable(mod)) |variable| Value.fromInterned(variable.init) else decl.val;
// TODO we need the symbol index for symbol in the table of locals for the containing atom
const res = try codegen.generateSymbol(&self.base, decl.srcLoc(mod), decl_val, &code_buffer, .{ .none = {} }, .{
const res = try codegen.generateSymbol(&self.base, decl.navSrcLoc(mod).upgrade(mod), decl_val, &code_buffer, .{ .none = {} }, .{
.parent_atom_index = @as(Atom.Index, @intCast(atom_idx)),
});
const code = switch (res) {
@ -1020,7 +1020,7 @@ fn addDeclExports(
{
try mod.failed_exports.put(mod.gpa, exp, try Module.ErrorMsg.create(
gpa,
mod.declPtr(decl_index).srcLoc(mod),
mod.declPtr(decl_index).navSrcLoc(mod).upgrade(mod),
"plan9 does not support extra sections",
.{},
));
@ -1212,12 +1212,12 @@ fn updateLazySymbolAtom(self: *Plan9, sym: File.LazySymbol, atom_index: Atom.Ind
self.syms.items[self.getAtomPtr(atom_index).sym_index.?] = symbol;
// generate the code
const src = if (sym.ty.getOwnerDeclOrNull(mod)) |owner_decl|
mod.declPtr(owner_decl).srcLoc(mod)
const src = if (sym.ty.srcLocOrNull(mod)) |src|
src.upgrade(mod)
else
Module.SrcLoc{
.file_scope = undefined,
.parent_decl_node = undefined,
.base_node = undefined,
.lazy = .unneeded,
};
const res = try codegen.generateLazySymbol(

View File

@ -269,7 +269,7 @@ pub fn updateDecl(
const res = try codegen.generateSymbol(
&wasm_file.base,
decl.srcLoc(mod),
decl.navSrcLoc(mod).upgrade(mod),
val,
&code_writer,
.none,
@ -308,7 +308,7 @@ pub fn updateFunc(
defer code_writer.deinit();
const result = try codegen.generateFunction(
&wasm_file.base,
decl.srcLoc(mod),
decl.navSrcLoc(mod).upgrade(mod),
func_index,
air,
liveness,
@ -484,7 +484,17 @@ pub fn lowerUnnamedConst(zig_object: *ZigObject, wasm_file: *Wasm, val: Value, d
});
defer gpa.free(name);
switch (try zig_object.lowerConst(wasm_file, name, val, decl.srcLoc(mod))) {
// We want to lower the source location of `decl`. However, when generating
// lazy functions (for e.g. `@tagName`), `decl` may correspond to a type
// rather than a `Nav`!
// The future split of `Decl` into `Nav` and `Cau` may require rethinking this
// logic. For now, just get the source location conditionally as needed.
const decl_src = if (decl.typeOf(mod).toIntern() == .type_type)
decl.val.toType().srcLoc(mod)
else
decl.navSrcLoc(mod);
switch (try zig_object.lowerConst(wasm_file, name, val, decl_src.upgrade(mod))) {
.ok => |atom_index| {
try wasm_file.getAtomPtr(parent_atom_index).locals.append(gpa, atom_index);
return @intFromEnum(wasm_file.getAtom(atom_index).sym_index);
@ -867,7 +877,7 @@ pub fn updateExports(
if (exp.opts.section.toSlice(&mod.intern_pool)) |section| {
try mod.failed_exports.putNoClobber(gpa, exp, try Module.ErrorMsg.create(
gpa,
decl.srcLoc(mod),
decl.navSrcLoc(mod).upgrade(mod),
"Unimplemented: ExportOptions.section '{s}'",
.{section},
));
@ -900,7 +910,7 @@ pub fn updateExports(
.link_once => {
try mod.failed_exports.putNoClobber(gpa, exp, try Module.ErrorMsg.create(
gpa,
decl.srcLoc(mod),
decl.navSrcLoc(mod).upgrade(mod),
"Unimplemented: LinkOnce",
.{},
));

View File

@ -5966,6 +5966,7 @@ fn cmdAstCheck(
.zir = undefined,
.mod = undefined,
.root_decl = .none,
.path_digest = undefined,
};
if (zig_source_file) |file_name| {
var f = fs.cwd().openFile(file_name, .{}) catch |err| {
@ -6284,6 +6285,7 @@ fn cmdDumpZir(
.zir = try Module.loadZirCache(gpa, f),
.mod = undefined,
.root_decl = .none,
.path_digest = undefined,
};
defer file.zir.deinit(gpa);
@ -6354,6 +6356,7 @@ fn cmdChangelist(
.zir = undefined,
.mod = undefined,
.root_decl = .none,
.path_digest = undefined,
};
file.mod = try Package.Module.createLimited(arena, .{

View File

@ -32,7 +32,7 @@ pub fn format(
return print(ctx.val, writer, ctx.depth, ctx.mod, ctx.opt_sema) catch |err| switch (err) {
error.OutOfMemory => @panic("OOM"), // We're not allowed to return this from a format function
error.ComptimeBreak, error.ComptimeReturn => unreachable,
error.AnalysisFail, error.NeededSourceLocation => unreachable, // TODO: re-evaluate when we use `opt_sema` more fully
error.AnalysisFail => unreachable, // TODO: re-evaluate when we use `opt_sema` more fully
else => |e| return e,
};
}

View File

@ -6,8 +6,9 @@ const Ast = std.zig.Ast;
const InternPool = @import("InternPool.zig");
const Zir = std.zig.Zir;
const Module = @import("Module.zig");
const LazySrcLoc = std.zig.LazySrcLoc;
const Zcu = @import("Module.zig");
const Module = Zcu;
const LazySrcLoc = Zcu.LazySrcLoc;
/// Write human-readable, debug formatted ZIR code to a file.
pub fn renderAsTextToFile(
@ -47,12 +48,11 @@ pub fn renderAsTextToFile(
const item = scope_file.zir.extraData(Zir.Inst.Imports.Item, extra_index);
extra_index = item.end;
const src: LazySrcLoc = .{ .token_abs = item.data.token };
const import_path = scope_file.zir.nullTerminatedString(item.data.name);
try stream.print(" @import(\"{}\") ", .{
std.zig.fmtEscapes(import_path),
});
try writer.writeSrc(stream, src);
try writer.writeSrcTokAbs(stream, item.data.token);
try stream.writeAll("\n");
}
}
@ -187,7 +187,7 @@ const Writer = struct {
} = .{},
fn relativeToNodeIndex(self: *Writer, offset: i32) Ast.Node.Index {
return @as(Ast.Node.Index, @bitCast(offset + @as(i32, @bitCast(self.parent_decl_node))));
return @bitCast(offset + @as(i32, @bitCast(self.parent_decl_node)));
}
fn writeInstToStream(
@ -569,7 +569,6 @@ const Writer = struct {
.wasm_memory_size,
.int_from_error,
.error_from_int,
.reify,
.c_va_copy,
.c_va_end,
.work_item_id,
@ -577,10 +576,20 @@ const Writer = struct {
.work_group_id,
=> {
const inst_data = self.code.extraData(Zir.Inst.UnNode, extended.operand).data;
const src = LazySrcLoc.nodeOffset(inst_data.node);
try self.writeInstRef(stream, inst_data.operand);
try stream.writeAll(")) ");
try self.writeSrc(stream, src);
try self.writeSrcNode(stream, inst_data.node);
},
.reify => {
const inst_data = self.code.extraData(Zir.Inst.Reify, extended.operand).data;
try stream.print("{d}, ", .{inst_data.src_line});
try self.writeInstRef(stream, inst_data.operand);
try stream.writeAll(")) ");
const prev_parent_decl_node = self.parent_decl_node;
self.parent_decl_node = inst_data.node;
defer self.parent_decl_node = prev_parent_decl_node;
try self.writeSrcNode(stream, 0);
},
.builtin_extern,
@ -591,12 +600,11 @@ const Writer = struct {
.c_va_arg,
=> {
const inst_data = self.code.extraData(Zir.Inst.BinNode, extended.operand).data;
const src = LazySrcLoc.nodeOffset(inst_data.node);
try self.writeInstRef(stream, inst_data.lhs);
try stream.writeAll(", ");
try self.writeInstRef(stream, inst_data.rhs);
try stream.writeAll(")) ");
try self.writeSrc(stream, src);
try self.writeSrcNode(stream, inst_data.node);
},
.builtin_async_call => try self.writeBuiltinAsyncCall(stream, extended),
@ -611,9 +619,8 @@ const Writer = struct {
}
fn writeExtNode(self: *Writer, stream: anytype, extended: Zir.Inst.Extended.InstData) !void {
const src = LazySrcLoc.nodeOffset(@as(i32, @bitCast(extended.operand)));
try stream.writeAll(")) ");
try self.writeSrc(stream, src);
try self.writeSrcNode(stream, @bitCast(extended.operand));
}
fn writeArrayInitElemType(self: *Writer, stream: anytype, inst: Zir.Inst.Index) !void {
@ -630,7 +637,7 @@ const Writer = struct {
const inst_data = self.code.instructions.items(.data)[@intFromEnum(inst)].un_node;
try self.writeInstRef(stream, inst_data.operand);
try stream.writeAll(") ");
try self.writeSrc(stream, inst_data.src());
try self.writeSrcNode(stream, inst_data.src_node);
}
fn writeUnTok(
@ -641,7 +648,7 @@ const Writer = struct {
const inst_data = self.code.instructions.items(.data)[@intFromEnum(inst)].un_tok;
try self.writeInstRef(stream, inst_data.operand);
try stream.writeAll(") ");
try self.writeSrc(stream, inst_data.src());
try self.writeSrcTok(stream, inst_data.src_tok);
}
fn writeValidateDestructure(
@ -653,9 +660,9 @@ const Writer = struct {
const extra = self.code.extraData(Zir.Inst.ValidateDestructure, inst_data.payload_index).data;
try self.writeInstRef(stream, extra.operand);
try stream.print(", {d}) (destructure=", .{extra.expect_len});
try self.writeSrc(stream, LazySrcLoc.nodeOffset(extra.destructure_node));
try self.writeSrcNode(stream, extra.destructure_node);
try stream.writeAll(") ");
try self.writeSrc(stream, inst_data.src());
try self.writeSrcNode(stream, inst_data.src_node);
}
fn writeValidateArrayInitTy(
@ -667,7 +674,7 @@ const Writer = struct {
const extra = self.code.extraData(Zir.Inst.ArrayInit, inst_data.payload_index).data;
try self.writeInstRef(stream, extra.ty);
try stream.print(", {d}) ", .{extra.init_count});
try self.writeSrc(stream, inst_data.src());
try self.writeSrcNode(stream, inst_data.src_node);
}
fn writeArrayTypeSentinel(
@ -683,7 +690,7 @@ const Writer = struct {
try stream.writeAll(", ");
try self.writeInstRef(stream, extra.elem_type);
try stream.writeAll(") ");
try self.writeSrc(stream, inst_data.src());
try self.writeSrcNode(stream, inst_data.src_node);
}
fn writePtrType(
@ -728,7 +735,7 @@ const Writer = struct {
try stream.writeAll(")");
}
try stream.writeAll(") ");
try self.writeSrc(stream, LazySrcLoc.nodeOffset(extra.data.src_node));
try self.writeSrcNode(stream, extra.data.src_node);
}
fn writeInt(self: *Writer, stream: anytype, inst: Zir.Inst.Index) !void {
@ -763,11 +770,10 @@ const Writer = struct {
fn writeFloat128(self: *Writer, stream: anytype, inst: Zir.Inst.Index) !void {
const inst_data = self.code.instructions.items(.data)[@intFromEnum(inst)].pl_node;
const extra = self.code.extraData(Zir.Inst.Float128, inst_data.payload_index).data;
const src = inst_data.src();
const number = extra.get();
// TODO improve std.format to be able to print f128 values
try stream.print("{d}) ", .{@as(f64, @floatCast(number))});
try self.writeSrc(stream, src);
try self.writeSrcNode(stream, inst_data.src_node);
}
fn writeStr(
@ -787,7 +793,7 @@ const Writer = struct {
try stream.writeAll(", ");
try self.writeInstRef(stream, extra.start);
try stream.writeAll(") ");
try self.writeSrc(stream, inst_data.src());
try self.writeSrcNode(stream, inst_data.src_node);
}
fn writeSliceEnd(self: *Writer, stream: anytype, inst: Zir.Inst.Index) !void {
@ -799,7 +805,7 @@ const Writer = struct {
try stream.writeAll(", ");
try self.writeInstRef(stream, extra.end);
try stream.writeAll(") ");
try self.writeSrc(stream, inst_data.src());
try self.writeSrcNode(stream, inst_data.src_node);
}
fn writeSliceSentinel(self: *Writer, stream: anytype, inst: Zir.Inst.Index) !void {
@ -813,7 +819,7 @@ const Writer = struct {
try stream.writeAll(", ");
try self.writeInstRef(stream, extra.sentinel);
try stream.writeAll(") ");
try self.writeSrc(stream, inst_data.src());
try self.writeSrcNode(stream, inst_data.src_node);
}
fn writeSliceLength(self: *Writer, stream: anytype, inst: Zir.Inst.Index) !void {
@ -829,7 +835,7 @@ const Writer = struct {
try self.writeInstRef(stream, extra.sentinel);
}
try stream.writeAll(") ");
try self.writeSrc(stream, inst_data.src());
try self.writeSrcNode(stream, inst_data.src_node);
}
fn writeUnionInit(self: *Writer, stream: anytype, inst: Zir.Inst.Index) !void {
@ -841,7 +847,7 @@ const Writer = struct {
try stream.writeAll(", ");
try self.writeInstRef(stream, extra.init);
try stream.writeAll(") ");
try self.writeSrc(stream, inst_data.src());
try self.writeSrcNode(stream, inst_data.src_node);
}
fn writeShuffle(self: *Writer, stream: anytype, inst: Zir.Inst.Index) !void {
@ -855,7 +861,7 @@ const Writer = struct {
try stream.writeAll(", ");
try self.writeInstRef(stream, extra.mask);
try stream.writeAll(") ");
try self.writeSrc(stream, inst_data.src());
try self.writeSrcNode(stream, inst_data.src_node);
}
fn writeSelect(self: *Writer, stream: anytype, extended: Zir.Inst.Extended.InstData) !void {
@ -868,7 +874,7 @@ const Writer = struct {
try stream.writeAll(", ");
try self.writeInstRef(stream, extra.b);
try stream.writeAll(") ");
try self.writeSrc(stream, LazySrcLoc.nodeOffset(extra.node));
try self.writeSrcNode(stream, extra.node);
}
fn writeMulAdd(self: *Writer, stream: anytype, inst: Zir.Inst.Index) !void {
@ -880,7 +886,7 @@ const Writer = struct {
try stream.writeAll(", ");
try self.writeInstRef(stream, extra.addend);
try stream.writeAll(") ");
try self.writeSrc(stream, inst_data.src());
try self.writeSrcNode(stream, inst_data.src_node);
}
fn writeBuiltinCall(self: *Writer, stream: anytype, inst: Zir.Inst.Index) !void {
@ -896,7 +902,7 @@ const Writer = struct {
try stream.writeAll(", ");
try self.writeInstRef(stream, extra.args);
try stream.writeAll(") ");
try self.writeSrc(stream, inst_data.src());
try self.writeSrcNode(stream, inst_data.src_node);
}
fn writeFieldParentPtr(self: *Writer, stream: anytype, extended: Zir.Inst.Extended.InstData) !void {
@ -913,7 +919,7 @@ const Writer = struct {
try stream.writeAll(", ");
try self.writeInstRef(stream, extra.field_ptr);
try stream.writeAll(") ");
try self.writeSrc(stream, extra.src());
try self.writeSrcNode(stream, extra.src_node);
}
fn writeBuiltinAsyncCall(self: *Writer, stream: anytype, extended: Zir.Inst.Extended.InstData) !void {
@ -926,7 +932,7 @@ const Writer = struct {
try stream.writeAll(", ");
try self.writeInstRef(stream, extra.args);
try stream.writeAll(") ");
try self.writeSrc(stream, LazySrcLoc.nodeOffset(extra.node));
try self.writeSrcNode(stream, extra.node);
}
fn writeParam(self: *Writer, stream: anytype, inst: Zir.Inst.Index) !void {
@ -944,7 +950,7 @@ const Writer = struct {
}
try self.writeBracedBody(stream, body);
try stream.writeAll(") ");
try self.writeSrc(stream, inst_data.src());
try self.writeSrcTok(stream, inst_data.src_tok);
}
fn writePlNodeBin(self: *Writer, stream: anytype, inst: Zir.Inst.Index) !void {
@ -954,7 +960,7 @@ const Writer = struct {
try stream.writeAll(", ");
try self.writeInstRef(stream, extra.rhs);
try stream.writeAll(") ");
try self.writeSrc(stream, inst_data.src());
try self.writeSrcNode(stream, inst_data.src_node);
}
fn writePlNodeMultiOp(self: *Writer, stream: anytype, inst: Zir.Inst.Index) !void {
@ -967,7 +973,7 @@ const Writer = struct {
try self.writeInstRef(stream, arg);
}
try stream.writeAll("}) ");
try self.writeSrc(stream, inst_data.src());
try self.writeSrcNode(stream, inst_data.src_node);
}
fn writeArrayMul(self: *Writer, stream: anytype, inst: Zir.Inst.Index) !void {
@ -979,7 +985,7 @@ const Writer = struct {
try stream.writeAll(", ");
try self.writeInstRef(stream, extra.rhs);
try stream.writeAll(") ");
try self.writeSrc(stream, inst_data.src());
try self.writeSrcNode(stream, inst_data.src_node);
}
fn writeElemValImm(self: *Writer, stream: anytype, inst: Zir.Inst.Index) !void {
@ -994,7 +1000,7 @@ const Writer = struct {
try self.writeInstRef(stream, extra.ptr);
try stream.print(", {d}) ", .{extra.index});
try self.writeSrc(stream, inst_data.src());
try self.writeSrcNode(stream, inst_data.src_node);
}
fn writePlNodeExport(self: *Writer, stream: anytype, inst: Zir.Inst.Index) !void {
@ -1006,7 +1012,7 @@ const Writer = struct {
try stream.print(", {p}, ", .{std.zig.fmtId(decl_name)});
try self.writeInstRef(stream, extra.options);
try stream.writeAll(") ");
try self.writeSrc(stream, inst_data.src());
try self.writeSrcNode(stream, inst_data.src_node);
}
fn writePlNodeExportValue(self: *Writer, stream: anytype, inst: Zir.Inst.Index) !void {
@ -1017,7 +1023,7 @@ const Writer = struct {
try stream.writeAll(", ");
try self.writeInstRef(stream, extra.options);
try stream.writeAll(") ");
try self.writeSrc(stream, inst_data.src());
try self.writeSrcNode(stream, inst_data.src_node);
}
fn writeValidateArrayInitRefTy(self: *Writer, stream: anytype, inst: Zir.Inst.Index) !void {
@ -1027,7 +1033,7 @@ const Writer = struct {
try self.writeInstRef(stream, extra.ptr_ty);
try stream.writeAll(", ");
try stream.print(", {}) ", .{extra.elem_count});
try self.writeSrc(stream, inst_data.src());
try self.writeSrcNode(stream, inst_data.src_node);
}
fn writeStructInit(self: *Writer, stream: anytype, inst: Zir.Inst.Index) !void {
@ -1051,12 +1057,11 @@ const Writer = struct {
try stream.writeAll("]");
}
try stream.writeAll(") ");
try self.writeSrc(stream, inst_data.src());
try self.writeSrcNode(stream, inst_data.src_node);
}
fn writeCmpxchg(self: *Writer, stream: anytype, extended: Zir.Inst.Extended.InstData) !void {
const extra = self.code.extraData(Zir.Inst.Cmpxchg, extended.operand).data;
const src = LazySrcLoc.nodeOffset(extra.node);
try self.writeInstRef(stream, extra.ptr);
try stream.writeAll(", ");
@ -1068,14 +1073,13 @@ const Writer = struct {
try stream.writeAll(", ");
try self.writeInstRef(stream, extra.failure_order);
try stream.writeAll(") ");
try self.writeSrc(stream, src);
try self.writeSrcNode(stream, extra.node);
}
fn writePtrCastFull(self: *Writer, stream: anytype, extended: Zir.Inst.Extended.InstData) !void {
const FlagsInt = @typeInfo(Zir.Inst.FullPtrCastFlags).Struct.backing_integer.?;
const flags: Zir.Inst.FullPtrCastFlags = @bitCast(@as(FlagsInt, @truncate(extended.small)));
const extra = self.code.extraData(Zir.Inst.BinNode, extended.operand).data;
const src = LazySrcLoc.nodeOffset(extra.node);
if (flags.ptr_cast) try stream.writeAll("ptr_cast, ");
if (flags.align_cast) try stream.writeAll("align_cast, ");
if (flags.addrspace_cast) try stream.writeAll("addrspace_cast, ");
@ -1085,19 +1089,18 @@ const Writer = struct {
try stream.writeAll(", ");
try self.writeInstRef(stream, extra.rhs);
try stream.writeAll(")) ");
try self.writeSrc(stream, src);
try self.writeSrcNode(stream, extra.node);
}
fn writePtrCastNoDest(self: *Writer, stream: anytype, extended: Zir.Inst.Extended.InstData) !void {
const FlagsInt = @typeInfo(Zir.Inst.FullPtrCastFlags).Struct.backing_integer.?;
const flags: Zir.Inst.FullPtrCastFlags = @bitCast(@as(FlagsInt, @truncate(extended.small)));
const extra = self.code.extraData(Zir.Inst.UnNode, extended.operand).data;
const src = LazySrcLoc.nodeOffset(extra.node);
if (flags.const_cast) try stream.writeAll("const_cast, ");
if (flags.volatile_cast) try stream.writeAll("volatile_cast, ");
try self.writeInstRef(stream, extra.operand);
try stream.writeAll(")) ");
try self.writeSrc(stream, src);
try self.writeSrcNode(stream, extra.node);
}
fn writeAtomicLoad(self: *Writer, stream: anytype, inst: Zir.Inst.Index) !void {
@ -1110,7 +1113,7 @@ const Writer = struct {
try stream.writeAll(", ");
try self.writeInstRef(stream, extra.ordering);
try stream.writeAll(") ");
try self.writeSrc(stream, inst_data.src());
try self.writeSrcNode(stream, inst_data.src_node);
}
fn writeAtomicStore(self: *Writer, stream: anytype, inst: Zir.Inst.Index) !void {
@ -1123,7 +1126,7 @@ const Writer = struct {
try stream.writeAll(", ");
try self.writeInstRef(stream, extra.ordering);
try stream.writeAll(") ");
try self.writeSrc(stream, inst_data.src());
try self.writeSrcNode(stream, inst_data.src_node);
}
fn writeAtomicRmw(self: *Writer, stream: anytype, inst: Zir.Inst.Index) !void {
@ -1138,7 +1141,7 @@ const Writer = struct {
try stream.writeAll(", ");
try self.writeInstRef(stream, extra.ordering);
try stream.writeAll(") ");
try self.writeSrc(stream, inst_data.src());
try self.writeSrcNode(stream, inst_data.src_node);
}
fn writeStructInitAnon(self: *Writer, stream: anytype, inst: Zir.Inst.Index) !void {
@ -1159,7 +1162,7 @@ const Writer = struct {
try stream.writeAll("]");
}
try stream.writeAll(") ");
try self.writeSrc(stream, inst_data.src());
try self.writeSrcNode(stream, inst_data.src_node);
}
fn writeStructInitFieldType(self: *Writer, stream: anytype, inst: Zir.Inst.Index) !void {
@ -1168,7 +1171,7 @@ const Writer = struct {
try self.writeInstRef(stream, extra.container_type);
const field_name = self.code.nullTerminatedString(extra.name_start);
try stream.print(", {s}) ", .{field_name});
try self.writeSrc(stream, inst_data.src());
try self.writeSrcNode(stream, inst_data.src_node);
}
fn writeFieldTypeRef(self: *Writer, stream: anytype, inst: Zir.Inst.Index) !void {
@ -1178,12 +1181,11 @@ const Writer = struct {
try stream.writeAll(", ");
try self.writeInstRef(stream, extra.field_name);
try stream.writeAll(") ");
try self.writeSrc(stream, inst_data.src());
try self.writeSrcNode(stream, inst_data.src_node);
}
fn writeNodeMultiOp(self: *Writer, stream: anytype, extended: Zir.Inst.Extended.InstData) !void {
const extra = self.code.extraData(Zir.Inst.NodeMultiOp, extended.operand);
const src = LazySrcLoc.nodeOffset(extra.data.src_node);
const operands = self.code.refSlice(extra.end, extended.small);
for (operands, 0..) |operand, i| {
@ -1191,7 +1193,7 @@ const Writer = struct {
try self.writeInstRef(stream, operand);
}
try stream.writeAll(")) ");
try self.writeSrc(stream, src);
try self.writeSrcNode(stream, extra.data.src_node);
}
fn writeInstNode(
@ -1202,7 +1204,7 @@ const Writer = struct {
const inst_data = self.code.instructions.items(.data)[@intFromEnum(inst)].inst_node;
try self.writeInstIndex(stream, inst_data.inst);
try stream.writeAll(") ");
try self.writeSrc(stream, inst_data.src());
try self.writeSrcNode(stream, inst_data.src_node);
}
fn writeAsm(
@ -1212,7 +1214,6 @@ const Writer = struct {
tmpl_is_expr: bool,
) !void {
const extra = self.code.extraData(Zir.Inst.Asm, extended.operand);
const src = LazySrcLoc.nodeOffset(extra.data.src_node);
const outputs_len = @as(u5, @truncate(extended.small));
const inputs_len = @as(u5, @truncate(extended.small >> 5));
const clobbers_len = @as(u5, @truncate(extended.small >> 10));
@ -1283,18 +1284,17 @@ const Writer = struct {
}
}
try stream.writeAll(")) ");
try self.writeSrc(stream, src);
try self.writeSrcNode(stream, extra.data.src_node);
}
fn writeOverflowArithmetic(self: *Writer, stream: anytype, extended: Zir.Inst.Extended.InstData) !void {
const extra = self.code.extraData(Zir.Inst.BinNode, extended.operand).data;
const src = LazySrcLoc.nodeOffset(extra.node);
try self.writeInstRef(stream, extra.lhs);
try stream.writeAll(", ");
try self.writeInstRef(stream, extra.rhs);
try stream.writeAll(")) ");
try self.writeSrc(stream, src);
try self.writeSrcNode(stream, extra.node);
}
fn writeCall(
@ -1347,13 +1347,13 @@ const Writer = struct {
}
try stream.writeAll("]) ");
try self.writeSrc(stream, inst_data.src());
try self.writeSrcNode(stream, inst_data.src_node);
}
fn writeBlock(self: *Writer, stream: anytype, inst: Zir.Inst.Index) !void {
const inst_data = self.code.instructions.items(.data)[@intFromEnum(inst)].pl_node;
try self.writePlNodeBlockWithoutSrc(stream, inst);
try self.writeSrc(stream, inst_data.src());
try self.writeSrcNode(stream, inst_data.src_node);
}
fn writePlNodeBlockWithoutSrc(self: *Writer, stream: anytype, inst: Zir.Inst.Index) !void {
@ -1375,7 +1375,7 @@ const Writer = struct {
try stream.writeAll(", ");
try self.writeBracedBody(stream, else_body);
try stream.writeAll(") ");
try self.writeSrc(stream, inst_data.src());
try self.writeSrcNode(stream, inst_data.src_node);
}
fn writeTry(self: *Writer, stream: anytype, inst: Zir.Inst.Index) !void {
@ -1386,13 +1386,18 @@ const Writer = struct {
try stream.writeAll(", ");
try self.writeBracedBody(stream, body);
try stream.writeAll(") ");
try self.writeSrc(stream, inst_data.src());
try self.writeSrcNode(stream, inst_data.src_node);
}
fn writeStructDecl(self: *Writer, stream: anytype, extended: Zir.Inst.Extended.InstData) !void {
const small = @as(Zir.Inst.StructDecl.Small, @bitCast(extended.small));
const small: Zir.Inst.StructDecl.Small = @bitCast(extended.small);
const extra = self.code.extraData(Zir.Inst.StructDecl, extended.operand);
const prev_parent_decl_node = self.parent_decl_node;
self.parent_decl_node = extra.data.src_node;
defer self.parent_decl_node = prev_parent_decl_node;
const fields_hash: std.zig.SrcHash = @bitCast([4]u32{
extra.data.fields_hash_0,
extra.data.fields_hash_1,
@ -1465,10 +1470,6 @@ const Writer = struct {
if (decls_len == 0) {
try stream.writeAll("{}, ");
} else {
const prev_parent_decl_node = self.parent_decl_node;
self.parent_decl_node = self.relativeToNodeIndex(extra.data.src_node);
defer self.parent_decl_node = prev_parent_decl_node;
try stream.writeAll("{\n");
self.indent += 2;
try self.writeBody(stream, self.code.bodySlice(extra_index, decls_len));
@ -1479,7 +1480,7 @@ const Writer = struct {
}
if (fields_len == 0) {
try stream.writeAll("{}, {})");
try stream.writeAll("{}, {}) ");
} else {
const bits_per_field = 4;
const fields_per_u32 = 32 / bits_per_field;
@ -1546,8 +1547,6 @@ const Writer = struct {
}
}
const prev_parent_decl_node = self.parent_decl_node;
self.parent_decl_node = self.relativeToNodeIndex(extra.data.src_node);
try stream.writeAll("{\n");
self.indent += 2;
@ -1595,18 +1594,22 @@ const Writer = struct {
try stream.writeAll(",\n");
}
self.parent_decl_node = prev_parent_decl_node;
self.indent -= 2;
try stream.writeByteNTimes(' ', self.indent);
try stream.writeAll("})");
try stream.writeAll("}) ");
}
try self.writeSrcNode(stream, extra.data.src_node);
try self.writeSrcNode(stream, 0);
}
fn writeUnionDecl(self: *Writer, stream: anytype, extended: Zir.Inst.Extended.InstData) !void {
const small = @as(Zir.Inst.UnionDecl.Small, @bitCast(extended.small));
const extra = self.code.extraData(Zir.Inst.UnionDecl, extended.operand);
const prev_parent_decl_node = self.parent_decl_node;
self.parent_decl_node = extra.data.src_node;
defer self.parent_decl_node = prev_parent_decl_node;
const fields_hash: std.zig.SrcHash = @bitCast([4]u32{
extra.data.fields_hash_0,
extra.data.fields_hash_1,
@ -1670,10 +1673,6 @@ const Writer = struct {
if (decls_len == 0) {
try stream.writeAll("{}");
} else {
const prev_parent_decl_node = self.parent_decl_node;
self.parent_decl_node = self.relativeToNodeIndex(extra.data.src_node);
defer self.parent_decl_node = prev_parent_decl_node;
try stream.writeAll("{\n");
self.indent += 2;
try self.writeBody(stream, self.code.bodySlice(extra_index, decls_len));
@ -1689,8 +1688,8 @@ const Writer = struct {
}
if (fields_len == 0) {
try stream.writeAll("})");
try self.writeSrcNode(stream, extra.data.src_node);
try stream.writeAll("}) ");
try self.writeSrcNode(stream, 0);
return;
}
try stream.writeAll(", ");
@ -1698,8 +1697,6 @@ const Writer = struct {
const body = self.code.bodySlice(extra_index, body_len);
extra_index += body.len;
const prev_parent_decl_node = self.parent_decl_node;
self.parent_decl_node = self.relativeToNodeIndex(extra.data.src_node);
try self.writeBracedDecl(stream, body);
try stream.writeAll(", {\n");
@ -1763,17 +1760,21 @@ const Writer = struct {
try stream.writeAll(",\n");
}
self.parent_decl_node = prev_parent_decl_node;
self.indent -= 2;
try stream.writeByteNTimes(' ', self.indent);
try stream.writeAll("})");
try self.writeSrcNode(stream, extra.data.src_node);
try stream.writeAll("}) ");
try self.writeSrcNode(stream, 0);
}
fn writeEnumDecl(self: *Writer, stream: anytype, extended: Zir.Inst.Extended.InstData) !void {
const small = @as(Zir.Inst.EnumDecl.Small, @bitCast(extended.small));
const extra = self.code.extraData(Zir.Inst.EnumDecl, extended.operand);
const prev_parent_decl_node = self.parent_decl_node;
self.parent_decl_node = extra.data.src_node;
defer self.parent_decl_node = prev_parent_decl_node;
const fields_hash: std.zig.SrcHash = @bitCast([4]u32{
extra.data.fields_hash_0,
extra.data.fields_hash_1,
@ -1835,10 +1836,6 @@ const Writer = struct {
if (decls_len == 0) {
try stream.writeAll("{}, ");
} else {
const prev_parent_decl_node = self.parent_decl_node;
self.parent_decl_node = self.relativeToNodeIndex(extra.data.src_node);
defer self.parent_decl_node = prev_parent_decl_node;
try stream.writeAll("{\n");
self.indent += 2;
try self.writeBody(stream, self.code.bodySlice(extra_index, decls_len));
@ -1856,12 +1853,9 @@ const Writer = struct {
const body = self.code.bodySlice(extra_index, body_len);
extra_index += body.len;
const prev_parent_decl_node = self.parent_decl_node;
self.parent_decl_node = self.relativeToNodeIndex(extra.data.src_node);
try self.writeBracedDecl(stream, body);
if (fields_len == 0) {
try stream.writeAll(", {})");
self.parent_decl_node = prev_parent_decl_node;
try stream.writeAll(", {}) ");
} else {
try stream.writeAll(", {\n");
@ -1900,12 +1894,11 @@ const Writer = struct {
}
try stream.writeAll(",\n");
}
self.parent_decl_node = prev_parent_decl_node;
self.indent -= 2;
try stream.writeByteNTimes(' ', self.indent);
try stream.writeAll("})");
try stream.writeAll("}) ");
}
try self.writeSrcNode(stream, extra.data.src_node);
try self.writeSrcNode(stream, 0);
}
fn writeOpaqueDecl(
@ -1915,6 +1908,11 @@ const Writer = struct {
) !void {
const small = @as(Zir.Inst.OpaqueDecl.Small, @bitCast(extended.small));
const extra = self.code.extraData(Zir.Inst.OpaqueDecl, extended.operand);
const prev_parent_decl_node = self.parent_decl_node;
self.parent_decl_node = extra.data.src_node;
defer self.parent_decl_node = prev_parent_decl_node;
var extra_index: usize = extra.end;
const captures_len = if (small.has_captures_len) blk: {
@ -1946,20 +1944,16 @@ const Writer = struct {
}
if (decls_len == 0) {
try stream.writeAll("{})");
try stream.writeAll("{}) ");
} else {
const prev_parent_decl_node = self.parent_decl_node;
self.parent_decl_node = self.relativeToNodeIndex(extra.data.src_node);
defer self.parent_decl_node = prev_parent_decl_node;
try stream.writeAll("{\n");
self.indent += 2;
try self.writeBody(stream, self.code.bodySlice(extra_index, decls_len));
self.indent -= 2;
try stream.writeByteNTimes(' ', self.indent);
try stream.writeAll("})");
try stream.writeAll("}) ");
}
try self.writeSrcNode(stream, extra.data.src_node);
try self.writeSrcNode(stream, 0);
}
fn writeErrorSetDecl(
@ -1988,7 +1982,7 @@ const Writer = struct {
try stream.writeByteNTimes(' ', self.indent);
try stream.writeAll("}) ");
try self.writeSrc(stream, inst_data.src());
try self.writeSrcNode(stream, inst_data.src_node);
}
fn writeSwitchBlockErrUnion(self: *Writer, stream: anytype, inst: Zir.Inst.Index) !void {
@ -2125,7 +2119,7 @@ const Writer = struct {
self.indent -= 2;
try stream.writeAll(") ");
try self.writeSrc(stream, inst_data.src());
try self.writeSrcNode(stream, inst_data.src_node);
}
fn writeSwitchBlock(self: *Writer, stream: anytype, inst: Zir.Inst.Index) !void {
@ -2255,7 +2249,7 @@ const Writer = struct {
self.indent -= 2;
try stream.writeAll(") ");
try self.writeSrc(stream, inst_data.src());
try self.writeSrcNode(stream, inst_data.src_node);
}
fn writePlNodeField(self: *Writer, stream: anytype, inst: Zir.Inst.Index) !void {
@ -2264,7 +2258,7 @@ const Writer = struct {
const name = self.code.nullTerminatedString(extra.field_name_start);
try self.writeInstRef(stream, extra.lhs);
try stream.print(", \"{}\") ", .{std.zig.fmtEscapes(name)});
try self.writeSrc(stream, inst_data.src());
try self.writeSrcNode(stream, inst_data.src_node);
}
fn writePlNodeFieldNamed(self: *Writer, stream: anytype, inst: Zir.Inst.Index) !void {
@ -2274,7 +2268,7 @@ const Writer = struct {
try stream.writeAll(", ");
try self.writeInstRef(stream, extra.field_name);
try stream.writeAll(") ");
try self.writeSrc(stream, inst_data.src());
try self.writeSrcNode(stream, inst_data.src_node);
}
fn writeAs(self: *Writer, stream: anytype, inst: Zir.Inst.Index) !void {
@ -2284,7 +2278,7 @@ const Writer = struct {
try stream.writeAll(", ");
try self.writeInstRef(stream, extra.operand);
try stream.writeAll(") ");
try self.writeSrc(stream, inst_data.src());
try self.writeSrcNode(stream, inst_data.src_node);
}
fn writeNode(
@ -2293,9 +2287,8 @@ const Writer = struct {
inst: Zir.Inst.Index,
) (@TypeOf(stream).Error || error{OutOfMemory})!void {
const src_node = self.code.instructions.items(.data)[@intFromEnum(inst)].node;
const src = LazySrcLoc.nodeOffset(src_node);
try stream.writeAll(") ");
try self.writeSrc(stream, src);
try self.writeSrcNode(stream, src_node);
}
fn writeStrTok(
@ -2306,7 +2299,7 @@ const Writer = struct {
const inst_data = self.code.instructions.items(.data)[@intFromEnum(inst)].str_tok;
const str = inst_data.get(self.code);
try stream.print("\"{}\") ", .{std.zig.fmtEscapes(str)});
try self.writeSrc(stream, inst_data.src());
try self.writeSrcTok(stream, inst_data.src_tok);
}
fn writeStrOp(self: *Writer, stream: anytype, inst: Zir.Inst.Index) !void {
@ -2323,7 +2316,6 @@ const Writer = struct {
inferred_error_set: bool,
) !void {
const inst_data = self.code.instructions.items(.data)[@intFromEnum(inst)].pl_node;
const src = inst_data.src();
const extra = self.code.extraData(Zir.Inst.Func, inst_data.payload_index);
var extra_index = extra.end;
@ -2370,7 +2362,7 @@ const Writer = struct {
ret_ty_body,
body,
src,
inst_data.src_node,
src_locs,
0,
);
@ -2379,7 +2371,6 @@ const Writer = struct {
fn writeFuncFancy(self: *Writer, stream: anytype, inst: Zir.Inst.Index) !void {
const inst_data = self.code.instructions.items(.data)[@intFromEnum(inst)].pl_node;
const extra = self.code.extraData(Zir.Inst.FuncFancy, inst_data.payload_index);
const src = inst_data.src();
var extra_index: usize = extra.end;
var align_ref: Zir.Inst.Ref = .none;
@ -2476,7 +2467,7 @@ const Writer = struct {
ret_ty_ref,
ret_ty_body,
body,
src,
inst_data.src_node,
src_locs,
noalias_bits,
);
@ -2515,7 +2506,6 @@ const Writer = struct {
fn writeAllocExtended(self: *Writer, stream: anytype, extended: Zir.Inst.Extended.InstData) !void {
const extra = self.code.extraData(Zir.Inst.AllocExtended, extended.operand);
const small = @as(Zir.Inst.AllocExtended.Small, @bitCast(extended.small));
const src = LazySrcLoc.nodeOffset(extra.data.src_node);
var extra_index: usize = extra.end;
const type_inst: Zir.Inst.Ref = if (!small.has_type) .none else blk: {
@ -2533,7 +2523,7 @@ const Writer = struct {
try self.writeOptionalInstRef(stream, ",ty=", type_inst);
try self.writeOptionalInstRef(stream, ",align=", align_inst);
try stream.writeAll(")) ");
try self.writeSrc(stream, src);
try self.writeSrcNode(stream, extra.data.src_node);
}
fn writeTypeofPeer(self: *Writer, stream: anytype, extended: Zir.Inst.Extended.InstData) !void {
@ -2557,7 +2547,7 @@ const Writer = struct {
try stream.writeAll(", ");
try self.writeBracedBody(stream, body);
try stream.writeAll(") ");
try self.writeSrc(stream, inst_data.src());
try self.writeSrcNode(stream, inst_data.src_node);
}
fn writeIntType(self: *Writer, stream: anytype, inst: Zir.Inst.Index) !void {
@ -2567,7 +2557,7 @@ const Writer = struct {
.unsigned => 'u',
};
try stream.print("{c}{d}) ", .{ prefix, int_type.bit_count });
try self.writeSrc(stream, int_type.src());
try self.writeSrcNode(stream, int_type.src_node);
}
fn writeSaveErrRetIndex(self: *Writer, stream: anytype, inst: Zir.Inst.Index) !void {
@ -2585,7 +2575,7 @@ const Writer = struct {
try self.writeInstRef(stream, extra.operand);
try stream.writeAll(") ");
try self.writeSrc(stream, extra.src());
try self.writeSrcNode(stream, extra.src_node);
}
fn writeBreak(self: *Writer, stream: anytype, inst: Zir.Inst.Index) !void {
@ -2611,7 +2601,7 @@ const Writer = struct {
try self.writeInstRef(stream, arg);
}
try stream.writeAll("}) ");
try self.writeSrc(stream, inst_data.src());
try self.writeSrcNode(stream, inst_data.src_node);
}
fn writeArrayInitAnon(self: *Writer, stream: anytype, inst: Zir.Inst.Index) !void {
@ -2626,7 +2616,7 @@ const Writer = struct {
try self.writeInstRef(stream, arg);
}
try stream.writeAll("}) ");
try self.writeSrc(stream, inst_data.src());
try self.writeSrcNode(stream, inst_data.src_node);
}
fn writeArrayInitSent(self: *Writer, stream: anytype, inst: Zir.Inst.Index) !void {
@ -2646,13 +2636,13 @@ const Writer = struct {
try self.writeInstRef(stream, elem);
}
try stream.writeAll("}) ");
try self.writeSrc(stream, inst_data.src());
try self.writeSrcNode(stream, inst_data.src_node);
}
fn writeUnreachable(self: *Writer, stream: anytype, inst: Zir.Inst.Index) !void {
const inst_data = self.code.instructions.items(.data)[@intFromEnum(inst)].@"unreachable";
try stream.writeAll(") ");
try self.writeSrc(stream, inst_data.src());
try self.writeSrcNode(stream, inst_data.src_node);
}
fn writeFuncCommon(
@ -2673,7 +2663,7 @@ const Writer = struct {
ret_ty_ref: Zir.Inst.Ref,
ret_ty_body: []const Zir.Inst.Index,
body: []const Zir.Inst.Index,
src: LazySrcLoc,
src_node: i32,
src_locs: Zir.Inst.Func.SrcLocs,
noalias_bits: u32,
) !void {
@ -2700,7 +2690,7 @@ const Writer = struct {
src_locs.rbrace_line + 1, @as(u16, @truncate(src_locs.columns >> 16)) + 1,
});
}
try self.writeSrc(stream, src);
try self.writeSrcNode(stream, src_node);
}
fn writeDbgStmt(self: *Writer, stream: anytype, inst: Zir.Inst.Index) !void {
@ -2729,11 +2719,16 @@ const Writer = struct {
}
fn writeDeclaration(self: *Writer, stream: anytype, inst: Zir.Inst.Index) !void {
const inst_data = self.code.instructions.items(.data)[@intFromEnum(inst)].pl_node;
const inst_data = self.code.instructions.items(.data)[@intFromEnum(inst)].declaration;
const extra = self.code.extraData(Zir.Inst.Declaration, inst_data.payload_index);
const doc_comment: ?Zir.NullTerminatedString = if (extra.data.flags.has_doc_comment) dc: {
break :dc @enumFromInt(self.code.extra[extra.end]);
} else null;
const prev_parent_decl_node = self.parent_decl_node;
defer self.parent_decl_node = prev_parent_decl_node;
self.parent_decl_node = inst_data.src_node;
if (extra.data.flags.is_pub) try stream.writeAll("pub ");
if (extra.data.flags.is_export) try stream.writeAll("export ");
switch (extra.data.name) {
@ -2757,10 +2752,6 @@ const Writer = struct {
try stream.print(" line(+{d}) hash({})", .{ extra.data.line_offset, std.fmt.fmtSliceHexLower(&src_hash_bytes) });
{
const prev_parent_decl_node = self.parent_decl_node;
defer self.parent_decl_node = prev_parent_decl_node;
self.parent_decl_node = self.relativeToNodeIndex(inst_data.src_node);
const bodies = extra.data.getBodies(@intCast(extra.end), self.code);
try stream.writeAll(" value=");
@ -2783,13 +2774,12 @@ const Writer = struct {
}
try stream.writeAll(") ");
try self.writeSrc(stream, inst_data.src());
try self.writeSrcNode(stream, 0);
}
fn writeClosureGet(self: *Writer, stream: anytype, extended: Zir.Inst.Extended.InstData) !void {
const src = LazySrcLoc.nodeOffset(@bitCast(extended.operand));
try stream.print("{d})) ", .{extended.small});
try self.writeSrc(stream, src);
try self.writeSrcNode(stream, @bitCast(extended.operand));
}
fn writeInstRef(self: *Writer, stream: anytype, ref: Zir.Inst.Ref) !void {
@ -2865,29 +2855,44 @@ const Writer = struct {
try stream.writeAll(name);
}
fn writeSrc(self: *Writer, stream: anytype, src: LazySrcLoc) !void {
if (self.file.tree_loaded) {
const tree = self.file.tree;
const src_loc: Module.SrcLoc = .{
.file_scope = self.file,
.parent_decl_node = self.parent_decl_node,
.lazy = src,
};
const src_span = src_loc.span(self.gpa) catch unreachable;
const start = self.line_col_cursor.find(tree.source, src_span.start);
const end = self.line_col_cursor.find(tree.source, src_span.end);
try stream.print("{s}:{d}:{d} to :{d}:{d}", .{
@tagName(src), start.line + 1, start.column + 1,
end.line + 1, end.column + 1,
});
}
fn writeSrcNode(self: *Writer, stream: anytype, src_node: i32) !void {
if (!self.file.tree_loaded) return;
const tree = self.file.tree;
const abs_node = self.relativeToNodeIndex(src_node);
const src_span = tree.nodeToSpan(abs_node);
const start = self.line_col_cursor.find(tree.source, src_span.start);
const end = self.line_col_cursor.find(tree.source, src_span.end);
try stream.print("node_offset:{d}:{d} to :{d}:{d}", .{
start.line + 1, start.column + 1,
end.line + 1, end.column + 1,
});
}
fn writeSrcNode(self: *Writer, stream: anytype, src_node: ?i32) !void {
const node_offset = src_node orelse return;
const src = LazySrcLoc.nodeOffset(node_offset);
try stream.writeAll(" ");
return self.writeSrc(stream, src);
fn writeSrcTok(self: *Writer, stream: anytype, src_tok: u32) !void {
if (!self.file.tree_loaded) return;
const tree = self.file.tree;
const abs_tok = tree.firstToken(self.parent_decl_node) + src_tok;
const span_start = tree.tokens.items(.start)[abs_tok];
const span_end = span_start + @as(u32, @intCast(tree.tokenSlice(abs_tok).len));
const start = self.line_col_cursor.find(tree.source, span_start);
const end = self.line_col_cursor.find(tree.source, span_end);
try stream.print("token_offset:{d}:{d} to :{d}:{d}", .{
start.line + 1, start.column + 1,
end.line + 1, end.column + 1,
});
}
fn writeSrcTokAbs(self: *Writer, stream: anytype, src_tok: u32) !void {
if (!self.file.tree_loaded) return;
const tree = self.file.tree;
const span_start = tree.tokens.items(.start)[src_tok];
const span_end = span_start + @as(u32, @intCast(tree.tokenSlice(src_tok).len));
const start = self.line_col_cursor.find(tree.source, span_start);
const end = self.line_col_cursor.find(tree.source, span_end);
try stream.print("token_abs:{d}:{d} to :{d}:{d}", .{
start.line + 1, start.column + 1,
end.line + 1, end.column + 1,
});
}
fn writeBracedDecl(self: *Writer, stream: anytype, body: []const Zir.Inst.Index) !void {

View File

@ -3317,15 +3317,6 @@ pub const Type = struct {
}
}
pub fn declSrcLoc(ty: Type, mod: *Module) Module.SrcLoc {
return declSrcLocOrNull(ty, mod).?;
}
pub fn declSrcLocOrNull(ty: Type, mod: *Module) ?Module.SrcLoc {
const decl = ty.getOwnerDeclOrNull(mod) orelse return null;
return mod.declPtr(decl).srcLoc(mod);
}
pub fn getOwnerDecl(ty: Type, mod: *Module) InternPool.DeclIndex {
return ty.getOwnerDeclOrNull(mod) orelse unreachable;
}
@ -3341,6 +3332,26 @@ pub const Type = struct {
};
}
pub fn srcLocOrNull(ty: Type, zcu: *Zcu) ?Module.LazySrcLoc {
const ip = &zcu.intern_pool;
return .{
.base_node_inst = switch (ip.indexToKey(ty.toIntern())) {
.struct_type, .union_type, .opaque_type, .enum_type => |info| switch (info) {
.declared => |d| d.zir_index,
.reified => |r| r.zir_index,
.generated_tag => |gt| ip.loadUnionType(gt.union_type).zir_index, // must be declared since we can't generate tags when reifying
.empty_struct => return null,
},
else => return null,
},
.offset = Module.LazySrcLoc.Offset.nodeOffset(0),
};
}
pub fn srcLoc(ty: Type, zcu: *Zcu) Module.LazySrcLoc {
return ty.srcLocOrNull(zcu).?;
}
pub fn isGenericPoison(ty: Type) bool {
return ty.toIntern() == .generic_poison_type;
}

View File

@ -15,4 +15,4 @@ export fn entry() void {
// target=native
//
// :6:9: error: enum tag value 60 already taken
// :4:5: note: other occurrence here
// :4:9: note: other occurrence here

View File

@ -6,4 +6,4 @@ export fn foo(comptime x: anytype, y: i32) i32 {
// backend=stage2
// target=native
//
// :1:27: error: comptime parameters not allowed in function with calling convention 'C'
// :1:15: error: comptime parameters not allowed in function with calling convention 'C'

View File

@ -7,4 +7,4 @@ export fn foo(num: anytype) i32 {
// backend=stage2
// target=native
//
// :1:20: error: generic parameters not allowed in function with calling convention 'C'
// :1:15: error: generic parameters not allowed in function with calling convention 'C'

View File

@ -19,5 +19,5 @@ comptime {
// target=native
//
// :5:30: error: comptime parameters not allowed in function with calling convention 'C'
// :6:41: error: generic parameters not allowed in function with calling convention 'C'
// :6:30: error: generic parameters not allowed in function with calling convention 'C'
// :1:15: error: comptime parameters not allowed in function with calling convention 'C'

View File

@ -27,9 +27,9 @@ export fn h() void {
// target=native
//
// :9:16: error: missing struct field: x
// :1:11: note: struct 'tmp.A' declared here
// :1:11: note: struct declared here
// :18:16: error: missing tuple field with index 1
// :16:11: note: struct declared here
// :22:16: error: missing tuple field with index 0
// :22:16: note: missing tuple field with index 1
// :16:11: note: struct 'tmp.B' declared here
// :16:11: note: struct declared here

View File

@ -14,5 +14,5 @@ comptime {
// target=native
//
// :5:17: error: missing struct field: b
// :1:11: note: struct 'tmp.S' declared here
// :1:11: note: struct declared here
// :9:15: note: called from here

View File

@ -17,5 +17,5 @@ pub export fn entr2() void {
// backend=stage2
// target=native
//
// :4:9: error: range start value is greater than the end value
// :11:9: error: range start value is greater than the end value
// :4:10: error: range start value is greater than the end value
// :11:11: error: range start value is greater than the end value

View File

@ -14,5 +14,5 @@ export fn entry() void {
// backend=stage2
// target=native
//
// :6:5: error: enum tag value 60 already taken
// :4:5: note: other occurrence here
// :6:9: error: enum tag value 60 already taken
// :4:9: note: other occurrence here