aboutsummaryrefslogtreecommitdiff
path: root/src/Sema.zig
diff options
context:
space:
mode:
authormlugg <mlugg@mlugg.co.uk>2024-02-14 22:01:18 +0000
committermlugg <mlugg@mlugg.co.uk>2024-02-16 11:26:34 +0000
commit260c84535546c81028cf42f1eb6ec9f17275db0f (patch)
tree566fd6156bbbbed22741b0d94b996542ee334ae6 /src/Sema.zig
parent10784c7fc8d419fff943994f1aa5a454d4e43391 (diff)
downloadzig-260c84535546c81028cf42f1eb6ec9f17275db0f.tar.gz
zig-260c84535546c81028cf42f1eb6ec9f17275db0f.zip
Zir: make src_node of type declarations non-optional
Previously, the `src_node` field of `struct_decl`, `union_decl`, `enum_decl`, and `opaque_decl` was optional, included in trailing data only if a flag in `Small` was set. However, this was unnecessary logic: AstGen always provided the source node. We can simplify a few bits of logic by making this field non-optional, moving it into non-trailing data. There was one place where the field was actually omitted before: the root struct of a file was at source node 0, so the node was coincidentally elided. Therefore, this commit has a fixed cost of 4 bytes of ZIR per file.
Diffstat (limited to 'src/Sema.zig')
-rw-r--r--src/Sema.zig37
1 files changed, 10 insertions, 27 deletions
diff --git a/src/Sema.zig b/src/Sema.zig
index 3465f923a8..45f0edcb56 100644
--- a/src/Sema.zig
+++ b/src/Sema.zig
@@ -2725,7 +2725,6 @@ pub fn getStructType(
const small: Zir.Inst.StructDecl.Small = @bitCast(extended.small);
var extra_index: usize = extended.operand + @typeInfo(Zir.Inst.StructDecl).Struct.fields.len;
- extra_index += @intFromBool(small.has_src_node);
const fields_len = if (small.has_fields_len) blk: {
const fields_len = sema.code.extra[extra_index];
extra_index += 1;
@@ -2778,10 +2777,7 @@ fn zirStructDecl(
const mod = sema.mod;
const ip = &mod.intern_pool;
const small: Zir.Inst.StructDecl.Small = @bitCast(extended.small);
- const src: LazySrcLoc = if (small.has_src_node) blk: {
- const node_offset: i32 = @bitCast(sema.code.extra[extended.operand + @typeInfo(Zir.Inst.StructDecl).Struct.fields.len]);
- break :blk LazySrcLoc.nodeOffset(node_offset);
- } else unreachable; // MLUGG TODO
+ const src = sema.code.extraData(Zir.Inst.StructDecl, extended.operand).data.src();
// Because these three things each reference each other, `undefined`
// placeholders are used before being set after the struct type gains an
@@ -2941,13 +2937,10 @@ fn zirEnumDecl(
const mod = sema.mod;
const gpa = sema.gpa;
const small: Zir.Inst.EnumDecl.Small = @bitCast(extended.small);
- var extra_index: usize = extended.operand + @typeInfo(Zir.Inst.EnumDecl).Struct.fields.len;
+ const extra = sema.code.extraData(Zir.Inst.EnumDecl, extended.operand);
+ var extra_index: usize = extra.end;
- const src: LazySrcLoc = if (small.has_src_node) blk: {
- const node_offset: i32 = @bitCast(sema.code.extra[extra_index]);
- extra_index += 1;
- break :blk LazySrcLoc.nodeOffset(node_offset);
- } else unreachable; // MLUGG TODO
+ const src = extra.data.src();
const tag_ty_src: LazySrcLoc = .{ .node_offset_container_tag = src.node_offset.x };
const tag_type_ref = if (small.has_tag_type) blk: {
@@ -3214,13 +3207,10 @@ fn zirUnionDecl(
const mod = sema.mod;
const gpa = sema.gpa;
const small: Zir.Inst.UnionDecl.Small = @bitCast(extended.small);
- var extra_index: usize = extended.operand + @typeInfo(Zir.Inst.UnionDecl).Struct.fields.len;
+ const extra = sema.code.extraData(Zir.Inst.UnionDecl, extended.operand);
+ var extra_index: usize = extra.end;
- const src: LazySrcLoc = if (small.has_src_node) blk: {
- const node_offset: i32 = @bitCast(sema.code.extra[extra_index]);
- extra_index += 1;
- break :blk LazySrcLoc.nodeOffset(node_offset);
- } else unreachable; // MLUGG TODO
+ const src = extra.data.src();
extra_index += @intFromBool(small.has_tag_type);
extra_index += @intFromBool(small.has_body_len);
@@ -3323,13 +3313,10 @@ fn zirOpaqueDecl(
const mod = sema.mod;
const small: Zir.Inst.OpaqueDecl.Small = @bitCast(extended.small);
- var extra_index: usize = extended.operand;
+ const extra = sema.code.extraData(Zir.Inst.OpaqueDecl, extended.operand);
+ var extra_index: usize = extra.end;
- const src: LazySrcLoc = if (small.has_src_node) blk: {
- const node_offset: i32 = @bitCast(sema.code.extra[extra_index]);
- extra_index += 1;
- break :blk LazySrcLoc.nodeOffset(node_offset);
- } else unreachable; // MLUGG TODO
+ const src = extra.data.src();
const decls_len = if (small.has_decls_len) blk: {
const decls_len = sema.code.extra[extra_index];
@@ -35662,7 +35649,6 @@ fn semaBackingIntType(mod: *Module, struct_type: InternPool.Key.StructType) Comp
if (small.has_backing_int) {
var extra_index: usize = extended.operand + @typeInfo(Zir.Inst.StructDecl).Struct.fields.len;
- extra_index += @intFromBool(small.has_src_node);
extra_index += @intFromBool(small.has_fields_len);
extra_index += @intFromBool(small.has_decls_len);
@@ -36374,8 +36360,6 @@ fn structZirInfo(zir: Zir, zir_index: Zir.Inst.Index) struct {
const small: Zir.Inst.StructDecl.Small = @bitCast(extended.small);
var extra_index: usize = extended.operand + @typeInfo(Zir.Inst.StructDecl).Struct.fields.len;
- extra_index += @intFromBool(small.has_src_node);
-
const fields_len = if (small.has_fields_len) blk: {
const fields_len = zir.extra[extra_index];
extra_index += 1;
@@ -36843,7 +36827,6 @@ fn semaUnionFields(mod: *Module, arena: Allocator, union_type: InternPool.Key.Un
var extra_index: usize = extended.operand + @typeInfo(Zir.Inst.UnionDecl).Struct.fields.len;
const src = LazySrcLoc.nodeOffset(0);
- extra_index += @intFromBool(small.has_src_node);
const tag_type_ref: Zir.Inst.Ref = if (small.has_tag_type) blk: {
const ty_ref: Zir.Inst.Ref = @enumFromInt(zir.extra[extra_index]);