aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorIsaac Freund <ifreund@ifreund.xyz>2021-03-28 19:08:42 +0200
committerIsaac Freund <ifreund@ifreund.xyz>2021-03-28 19:10:10 +0200
commit402f87a213b9f3d5e19d5a1d412d8963957d8849 (patch)
treefc82b22ed9387a937457594fd5c99a4589e71eb6 /src
parent68f4eb0f67b6e5f7c20332e79c8e8decb07748ee (diff)
downloadzig-402f87a213b9f3d5e19d5a1d412d8963957d8849.tar.gz
zig-402f87a213b9f3d5e19d5a1d412d8963957d8849.zip
stage2: rename WipZirCode => AstGen, astgen.zig => AstGen.zig
Diffstat (limited to 'src')
-rw-r--r--src/AstGen.zig (renamed from src/astgen.zig)262
-rw-r--r--src/Module.zig370
-rw-r--r--src/translate_c.zig2
-rw-r--r--src/zir.zig4
4 files changed, 323 insertions, 315 deletions
diff --git a/src/astgen.zig b/src/AstGen.zig
index 36d1abad41..cc80d5d752 100644
--- a/src/astgen.zig
+++ b/src/AstGen.zig
@@ -1,19 +1,117 @@
+//! A Work-In-Progress `zir.Code`. This is a shared parent of all
+//! `GenZir` scopes. Once the `zir.Code` is produced, this struct
+//! is deinitialized.
+//! The `GenZir.finish` function converts this to a `zir.Code`.
+
+const AstGen = @This();
+
const std = @import("std");
+const ast = std.zig.ast;
const mem = std.mem;
const Allocator = std.mem.Allocator;
const assert = std.debug.assert;
+const ArrayListUnmanaged = std.ArrayListUnmanaged;
const Value = @import("value.zig").Value;
const Type = @import("type.zig").Type;
const TypedValue = @import("TypedValue.zig");
const zir = @import("zir.zig");
const Module = @import("Module.zig");
-const ast = std.zig.ast;
const trace = @import("tracy.zig").trace;
const Scope = Module.Scope;
const InnerError = Module.InnerError;
+const Decl = Module.Decl;
const BuiltinFn = @import("BuiltinFn.zig");
+instructions: std.MultiArrayList(zir.Inst) = .{},
+string_bytes: ArrayListUnmanaged(u8) = .{},
+extra: ArrayListUnmanaged(u32) = .{},
+decl_map: std.StringArrayHashMapUnmanaged(void) = .{},
+decls: ArrayListUnmanaged(*Decl) = .{},
+/// The end of special indexes. `zir.Inst.Ref` subtracts against this number to convert
+/// to `zir.Inst.Index`. The default here is correct if there are 0 parameters.
+ref_start_index: u32 = zir.Inst.Ref.typed_value_map.len,
+mod: *Module,
+decl: *Decl,
+arena: *Allocator,
+
+/// Call `deinit` on the result.
+pub fn init(mod: *Module, decl: *Decl, arena: *Allocator) !AstGen {
+ var astgen: AstGen = .{
+ .mod = mod,
+ .decl = decl,
+ .arena = arena,
+ };
+ // Must be a block instruction at index 0 with the root body.
+ try astgen.instructions.append(mod.gpa, .{
+ .tag = .block,
+ .data = .{ .pl_node = .{
+ .src_node = 0,
+ .payload_index = undefined,
+ } },
+ });
+ return astgen;
+}
+
+pub fn addExtra(astgen: *AstGen, extra: anytype) Allocator.Error!u32 {
+ const fields = std.meta.fields(@TypeOf(extra));
+ try astgen.extra.ensureCapacity(astgen.mod.gpa, astgen.extra.items.len + fields.len);
+ return addExtraAssumeCapacity(astgen, extra);
+}
+
+pub fn addExtraAssumeCapacity(astgen: *AstGen, extra: anytype) u32 {
+ const fields = std.meta.fields(@TypeOf(extra));
+ const result = @intCast(u32, astgen.extra.items.len);
+ inline for (fields) |field| {
+ astgen.extra.appendAssumeCapacity(switch (field.field_type) {
+ u32 => @field(extra, field.name),
+ zir.Inst.Ref => @enumToInt(@field(extra, field.name)),
+ else => @compileError("bad field type"),
+ });
+ }
+ return result;
+}
+
+pub fn appendRefs(astgen: *AstGen, refs: []const zir.Inst.Ref) !void {
+ const coerced = @bitCast([]const u32, refs);
+ return astgen.extra.appendSlice(astgen.mod.gpa, coerced);
+}
+
+pub fn appendRefsAssumeCapacity(astgen: *AstGen, refs: []const zir.Inst.Ref) void {
+ const coerced = @bitCast([]const u32, refs);
+ astgen.extra.appendSliceAssumeCapacity(coerced);
+}
+
+pub fn refIsNoReturn(astgen: AstGen, inst_ref: zir.Inst.Ref) bool {
+ if (inst_ref == .unreachable_value) return true;
+ if (astgen.refToIndex(inst_ref)) |inst_index| {
+ return astgen.instructions.items(.tag)[inst_index].isNoReturn();
+ }
+ return false;
+}
+
+pub fn indexToRef(astgen: AstGen, inst: zir.Inst.Index) zir.Inst.Ref {
+ return @intToEnum(zir.Inst.Ref, astgen.ref_start_index + inst);
+}
+
+pub fn refToIndex(astgen: AstGen, inst: zir.Inst.Ref) ?zir.Inst.Index {
+ const ref_int = @enumToInt(inst);
+ if (ref_int >= astgen.ref_start_index) {
+ return ref_int - astgen.ref_start_index;
+ } else {
+ return null;
+ }
+}
+
+pub fn deinit(astgen: *AstGen) void {
+ const gpa = astgen.mod.gpa;
+ astgen.instructions.deinit(gpa);
+ astgen.extra.deinit(gpa);
+ astgen.string_bytes.deinit(gpa);
+ astgen.decl_map.deinit(gpa);
+ astgen.decls.deinit(gpa);
+}
+
pub const ResultLoc = union(enum) {
/// The expression is the right-hand side of assignment to `_`. Only the side-effects of the
/// expression should be generated. The result instruction from the expression must
@@ -419,7 +517,7 @@ pub fn expr(mod: *Module, scope: *Scope, rl: ResultLoc, node: ast.Node.Index) In
.tag = .@"unreachable",
.data = .{ .@"unreachable" = .{
.safety = true,
- .src_node = gz.zir_code.decl.nodeIndexToRelative(node),
+ .src_node = gz.astgen.decl.nodeIndexToRelative(node),
} },
});
return zir.Inst.Ref.unreachable_value;
@@ -717,8 +815,8 @@ fn breakExpr(mod: *Module, parent_scope: *Scope, node: ast.Node.Index) InnerErro
try block_gz.labeled_breaks.append(mod.gpa, br);
if (have_store_to_block) {
- const zir_tags = parent_gz.zir_code.instructions.items(.tag);
- const zir_datas = parent_gz.zir_code.instructions.items(.data);
+ const zir_tags = parent_gz.astgen.instructions.items(.tag);
+ const zir_datas = parent_gz.astgen.instructions.items(.data);
const store_inst = @intCast(u32, zir_tags.len - 2);
assert(zir_tags[store_inst] == .store_to_block_ptr);
assert(zir_datas[store_inst].bin.lhs == block_gz.rl_ptr);
@@ -882,7 +980,7 @@ fn labeledBlockExpr(
var block_scope: Scope.GenZir = .{
.parent = parent_scope,
- .zir_code = gz.zir_code,
+ .astgen = gz.astgen,
.force_comptime = gz.force_comptime,
.instructions = .{},
// TODO @as here is working around a stage1 miscompilation bug :(
@@ -902,8 +1000,8 @@ fn labeledBlockExpr(
return mod.failTok(parent_scope, label_token, "unused block label", .{});
}
- const zir_tags = gz.zir_code.instructions.items(.tag);
- const zir_datas = gz.zir_code.instructions.items(.data);
+ const zir_tags = gz.astgen.instructions.items(.tag);
+ const zir_datas = gz.astgen.instructions.items(.data);
const strat = rlStrategy(rl, &block_scope);
switch (strat.tag) {
@@ -915,7 +1013,7 @@ fn labeledBlockExpr(
}
try block_scope.setBlockBody(block_inst);
- return gz.zir_code.indexToRef(block_inst);
+ return gz.astgen.indexToRef(block_inst);
},
.break_operand => {
// All break operands are values that did not use the result location pointer.
@@ -928,7 +1026,7 @@ fn labeledBlockExpr(
// would be better still to elide the ones that are in this list.
}
try block_scope.setBlockBody(block_inst);
- const block_ref = gz.zir_code.indexToRef(block_inst);
+ const block_ref = gz.astgen.indexToRef(block_inst);
switch (rl) {
.ref => return block_ref,
else => return rvalue(mod, parent_scope, rl, block_ref, block_node),
@@ -982,13 +1080,13 @@ fn blockExprStmts(
// We need to emit an error if the result is not `noreturn` or `void`, but
// we want to avoid adding the ZIR instruction if possible for performance.
const maybe_unused_result = try expr(mod, scope, .none, statement);
- const elide_check = if (gz.zir_code.refToIndex(maybe_unused_result)) |inst| b: {
+ const elide_check = if (gz.astgen.refToIndex(maybe_unused_result)) |inst| b: {
// Note that this array becomes invalid after appending more items to it
// in the above while loop.
- const zir_tags = gz.zir_code.instructions.items(.tag);
+ const zir_tags = gz.astgen.instructions.items(.tag);
switch (zir_tags[inst]) {
.@"const" => {
- const tv = gz.zir_code.instructions.items(.data)[inst].@"const";
+ const tv = gz.astgen.instructions.items(.data)[inst].@"const";
break :b switch (tv.ty.zigTypeTag()) {
.NoReturn, .Void => true,
else => false,
@@ -1173,7 +1271,7 @@ fn varDecl(
return mod.failNode(scope, var_decl.ast.align_node, "TODO implement alignment on locals", .{});
}
const gz = scope.getGenZir();
- const wzc = gz.zir_code;
+ const astgen = gz.astgen;
const tree = scope.tree();
const token_tags = tree.tokens.items(.tag);
@@ -1255,7 +1353,7 @@ fn varDecl(
var init_scope: Scope.GenZir = .{
.parent = scope,
.force_comptime = gz.force_comptime,
- .zir_code = wzc,
+ .astgen = astgen,
};
defer init_scope.instructions.deinit(mod.gpa);
@@ -1272,8 +1370,8 @@ fn varDecl(
}
const init_result_loc: ResultLoc = .{ .block_ptr = &init_scope };
const init_inst = try expr(mod, &init_scope.base, init_result_loc, var_decl.ast.init_node);
- const zir_tags = wzc.instructions.items(.tag);
- const zir_datas = wzc.instructions.items(.data);
+ const zir_tags = astgen.instructions.items(.tag);
+ const zir_datas = astgen.instructions.items(.data);
const parent_zir = &gz.instructions;
if (init_scope.rvalue_rl_count == 1) {
@@ -1284,7 +1382,7 @@ fn varDecl(
const expected_len = parent_zir.items.len + init_scope.instructions.items.len - 2;
try parent_zir.ensureCapacity(mod.gpa, expected_len);
for (init_scope.instructions.items) |src_inst| {
- if (wzc.indexToRef(src_inst) == init_scope.rl_ptr) continue;
+ if (astgen.indexToRef(src_inst) == init_scope.rl_ptr) continue;
if (zir_tags[src_inst] == .store_to_block_ptr) {
if (zir_datas[src_inst].bin.lhs == init_scope.rl_ptr) continue;
}
@@ -1498,27 +1596,27 @@ fn ptrType(
trailing_count += 2;
}
- const gpa = gz.zir_code.gpa;
+ const gpa = gz.astgen.mod.gpa;
try gz.instructions.ensureCapacity(gpa, gz.instructions.items.len + 1);
- try gz.zir_code.instructions.ensureCapacity(gpa, gz.zir_code.instructions.len + 1);
- try gz.zir_code.extra.ensureCapacity(gpa, gz.zir_code.extra.items.len +
+ try gz.astgen.instructions.ensureCapacity(gpa, gz.astgen.instructions.len + 1);
+ try gz.astgen.extra.ensureCapacity(gpa, gz.astgen.extra.items.len +
@typeInfo(zir.Inst.PtrType).Struct.fields.len + trailing_count);
- const payload_index = gz.zir_code.addExtraAssumeCapacity(zir.Inst.PtrType{ .elem_type = elem_type });
+ const payload_index = gz.astgen.addExtraAssumeCapacity(zir.Inst.PtrType{ .elem_type = elem_type });
if (sentinel_ref != .none) {
- gz.zir_code.extra.appendAssumeCapacity(@enumToInt(sentinel_ref));
+ gz.astgen.extra.appendAssumeCapacity(@enumToInt(sentinel_ref));
}
if (align_ref != .none) {
- gz.zir_code.extra.appendAssumeCapacity(@enumToInt(align_ref));
+ gz.astgen.extra.appendAssumeCapacity(@enumToInt(align_ref));
}
if (bit_start_ref != .none) {
- gz.zir_code.extra.appendAssumeCapacity(@enumToInt(bit_start_ref));
- gz.zir_code.extra.appendAssumeCapacity(@enumToInt(bit_end_ref));
+ gz.astgen.extra.appendAssumeCapacity(@enumToInt(bit_start_ref));
+ gz.astgen.extra.appendAssumeCapacity(@enumToInt(bit_end_ref));
}
- const new_index = @intCast(zir.Inst.Index, gz.zir_code.instructions.len);
- const result = gz.zir_code.indexToRef(new_index);
- gz.zir_code.instructions.appendAssumeCapacity(.{ .tag = .ptr_type, .data = .{
+ const new_index = @intCast(zir.Inst.Index, gz.astgen.instructions.len);
+ const result = gz.astgen.indexToRef(new_index);
+ gz.astgen.instructions.appendAssumeCapacity(.{ .tag = .ptr_type, .data = .{
.ptr_type = .{
.flags = .{
.is_allowzero = ptr_info.allowzero_token != null,
@@ -1638,7 +1736,7 @@ fn orelseCatchExpr(
var block_scope: Scope.GenZir = .{
.parent = scope,
- .zir_code = parent_gz.zir_code,
+ .astgen = parent_gz.astgen,
.force_comptime = parent_gz.force_comptime,
.instructions = .{},
};
@@ -1674,7 +1772,7 @@ fn orelseCatchExpr(
var then_scope: Scope.GenZir = .{
.parent = scope,
- .zir_code = parent_gz.zir_code,
+ .astgen = parent_gz.astgen,
.force_comptime = block_scope.force_comptime,
.instructions = .{},
};
@@ -1705,7 +1803,7 @@ fn orelseCatchExpr(
var else_scope: Scope.GenZir = .{
.parent = scope,
- .zir_code = parent_gz.zir_code,
+ .astgen = parent_gz.astgen,
.force_comptime = block_scope.force_comptime,
.instructions = .{},
};
@@ -1759,26 +1857,26 @@ fn finishThenElseBlock(
// We now have enough information to decide whether the result instruction should
// be communicated via result location pointer or break instructions.
const strat = rlStrategy(rl, block_scope);
- const wzc = block_scope.zir_code;
+ const astgen = block_scope.astgen;
switch (strat.tag) {
.break_void => {
- if (!wzc.refIsNoReturn(then_result)) {
+ if (!astgen.refIsNoReturn(then_result)) {
_ = try then_scope.addBreak(break_tag, then_break_block, .void_value);
}
- const elide_else = if (else_result != .none) wzc.refIsNoReturn(else_result) else false;
+ const elide_else = if (else_result != .none) astgen.refIsNoReturn(else_result) else false;
if (!elide_else) {
_ = try else_scope.addBreak(break_tag, main_block, .void_value);
}
assert(!strat.elide_store_to_block_ptr_instructions);
try setCondBrPayload(condbr, cond, then_scope, else_scope);
- return wzc.indexToRef(main_block);
+ return astgen.indexToRef(main_block);
},
.break_operand => {
- if (!wzc.refIsNoReturn(then_result)) {
+ if (!astgen.refIsNoReturn(then_result)) {
_ = try then_scope.addBreak(break_tag, then_break_block, then_result);
}
if (else_result != .none) {
- if (!wzc.refIsNoReturn(else_result)) {
+ if (!astgen.refIsNoReturn(else_result)) {
_ = try else_scope.addBreak(break_tag, main_block, else_result);
}
} else {
@@ -1789,7 +1887,7 @@ fn finishThenElseBlock(
} else {
try setCondBrPayload(condbr, cond, then_scope, else_scope);
}
- const block_ref = wzc.indexToRef(main_block);
+ const block_ref = astgen.indexToRef(main_block);
switch (rl) {
.ref => return block_ref,
else => return rvalue(mod, parent_scope, rl, block_ref, node),
@@ -1821,7 +1919,7 @@ pub fn fieldAccess(
const object_node = node_datas[node].lhs;
const dot_token = main_tokens[node];
const field_ident = dot_token + 1;
- const string_bytes = &gz.zir_code.string_bytes;
+ const string_bytes = &gz.astgen.string_bytes;
const str_index = @intCast(u32, string_bytes.items.len);
try mod.appendIdentStr(scope, field_ident, string_bytes);
try string_bytes.append(mod.gpa, 0);
@@ -1888,7 +1986,7 @@ fn simpleStrTok(
op_inst_tag: zir.Inst.Tag,
) InnerError!zir.Inst.Ref {
const gz = scope.getGenZir();
- const string_bytes = &gz.zir_code.string_bytes;
+ const string_bytes = &gz.astgen.string_bytes;
const str_index = @intCast(u32, string_bytes.items.len);
try mod.appendIdentStr(scope, ident_token, string_bytes);
try string_bytes.append(mod.gpa, 0);
@@ -1911,7 +2009,7 @@ fn boolBinOp(
var rhs_scope: Scope.GenZir = .{
.parent = scope,
- .zir_code = gz.zir_code,
+ .astgen = gz.astgen,
.force_comptime = gz.force_comptime,
};
defer rhs_scope.instructions.deinit(mod.gpa);
@@ -1919,7 +2017,7 @@ fn boolBinOp(
_ = try rhs_scope.addBreak(.break_inline, bool_br, rhs);
try rhs_scope.setBoolBrBody(bool_br);
- const block_ref = gz.zir_code.indexToRef(bool_br);
+ const block_ref = gz.astgen.indexToRef(bool_br);
return rvalue(mod, scope, rl, block_ref, node);
}
@@ -1933,7 +2031,7 @@ fn ifExpr(
const parent_gz = scope.getGenZir();
var block_scope: Scope.GenZir = .{
.parent = scope,
- .zir_code = parent_gz.zir_code,
+ .astgen = parent_gz.astgen,
.force_comptime = parent_gz.force_comptime,
.instructions = .{},
};
@@ -1959,7 +2057,7 @@ fn ifExpr(
var then_scope: Scope.GenZir = .{
.parent = scope,
- .zir_code = parent_gz.zir_code,
+ .astgen = parent_gz.astgen,
.force_comptime = block_scope.force_comptime,
.instructions = .{},
};
@@ -1976,7 +2074,7 @@ fn ifExpr(
var else_scope: Scope.GenZir = .{
.parent = scope,
- .zir_code = parent_gz.zir_code,
+ .astgen = parent_gz.astgen,
.force_comptime = block_scope.force_comptime,
.instructions = .{},
};
@@ -2024,20 +2122,20 @@ fn setCondBrPayload(
then_scope: *Scope.GenZir,
else_scope: *Scope.GenZir,
) !void {
- const wzc = then_scope.zir_code;
+ const astgen = then_scope.astgen;
- try wzc.extra.ensureCapacity(wzc.gpa, wzc.extra.items.len +
+ try astgen.extra.ensureCapacity(astgen.mod.gpa, astgen.extra.items.len +
@typeInfo(zir.Inst.CondBr).Struct.fields.len +
then_scope.instructions.items.len + else_scope.instructions.items.len);
- const zir_datas = wzc.instructions.items(.data);
- zir_datas[condbr].pl_node.payload_index = wzc.addExtraAssumeCapacity(zir.Inst.CondBr{
+ const zir_datas = astgen.instructions.items(.data);
+ zir_datas[condbr].pl_node.payload_index = astgen.addExtraAssumeCapacity(zir.Inst.CondBr{
.condition = cond,
.then_body_len = @intCast(u32, then_scope.instructions.items.len),
.else_body_len = @intCast(u32, else_scope.instructions.items.len),
});
- wzc.extra.appendSliceAssumeCapacity(then_scope.instructions.items);
- wzc.extra.appendSliceAssumeCapacity(else_scope.instructions.items);
+ astgen.extra.appendSliceAssumeCapacity(then_scope.instructions.items);
+ astgen.extra.appendSliceAssumeCapacity(else_scope.instructions.items);
}
/// If `elide_block_store_ptr` is set, expects to find exactly 1 .store_to_block_ptr instruction.
@@ -2047,24 +2145,24 @@ fn setCondBrPayloadElideBlockStorePtr(
then_scope: *Scope.GenZir,
else_scope: *Scope.GenZir,
) !void {
- const wzc = then_scope.zir_code;
+ const astgen = then_scope.astgen;
- try wzc.extra.ensureCapacity(wzc.gpa, wzc.extra.items.len +
+ try astgen.extra.ensureCapacity(astgen.mod.gpa, astgen.extra.items.len +
@typeInfo(zir.Inst.CondBr).Struct.fields.len +
then_scope.instructions.items.len + else_scope.instructions.items.len - 2);
- const zir_datas = wzc.instructions.items(.data);
- zir_datas[condbr].pl_node.payload_index = wzc.addExtraAssumeCapacity(zir.Inst.CondBr{
+ const zir_datas = astgen.instructions.items(.data);
+ zir_datas[condbr].pl_node.payload_index = astgen.addExtraAssumeCapacity(zir.Inst.CondBr{
.condition = cond,
.then_body_len = @intCast(u32, then_scope.instructions.items.len - 1),
.else_body_len = @intCast(u32, else_scope.instructions.items.len - 1),
});
- const zir_tags = wzc.instructions.items(.tag);
+ const zir_tags = astgen.instructions.items(.tag);
for ([_]*Scope.GenZir{ then_scope, else_scope }) |scope| {
for (scope.instructions.items) |src_inst| {
if (zir_tags[src_inst] != .store_to_block_ptr) {
- wzc.extra.appendAssumeCapacity(src_inst);
+ astgen.extra.appendAssumeCapacity(src_inst);
}
}
}
@@ -2088,7 +2186,7 @@ fn whileExpr(
var loop_scope: Scope.GenZir = .{
.parent = scope,
- .zir_code = parent_gz.zir_code,
+ .astgen = parent_gz.astgen,
.force_comptime = parent_gz.force_comptime,
.instructions = .{},
};
@@ -2097,7 +2195,7 @@ fn whileExpr(
var continue_scope: Scope.GenZir = .{
.parent = &loop_scope.base,
- .zir_code = parent_gz.zir_code,
+ .astgen = parent_gz.astgen,
.force_comptime = loop_scope.force_comptime,
.instructions = .{},
};
@@ -2143,7 +2241,7 @@ fn whileExpr(
var then_scope: Scope.GenZir = .{
.parent = &continue_scope.base,
- .zir_code = parent_gz.zir_code,
+ .astgen = parent_gz.astgen,
.force_comptime = continue_scope.force_comptime,
.instructions = .{},
};
@@ -2156,7 +2254,7 @@ fn whileExpr(
var else_scope: Scope.GenZir = .{
.parent = &continue_scope.base,
- .zir_code = parent_gz.zir_code,
+ .astgen = parent_gz.astgen,
.force_comptime = continue_scope.force_comptime,
.instructions = .{},
};
@@ -2236,7 +2334,7 @@ fn forExpr(
var loop_scope: Scope.GenZir = .{
.parent = scope,
- .zir_code = parent_gz.zir_code,
+ .astgen = parent_gz.astgen,
.force_comptime = parent_gz.force_comptime,
.instructions = .{},
};
@@ -2245,7 +2343,7 @@ fn forExpr(
var cond_scope: Scope.GenZir = .{
.parent = &loop_scope.base,
- .zir_code = parent_gz.zir_code,
+ .astgen = parent_gz.astgen,
.force_comptime = loop_scope.force_comptime,
.instructions = .{},
};
@@ -2287,7 +2385,7 @@ fn forExpr(
var then_scope: Scope.GenZir = .{
.parent = &cond_scope.base,
- .zir_code = parent_gz.zir_code,
+ .astgen = parent_gz.astgen,
.force_comptime = cond_scope.force_comptime,
.instructions = .{},
};
@@ -2331,7 +2429,7 @@ fn forExpr(
var else_scope: Scope.GenZir = .{
.parent = &cond_scope.base,
- .zir_code = parent_gz.zir_code,
+ .astgen = parent_gz.astgen,
.force_comptime = cond_scope.force_comptime,
.instructions = .{},
};
@@ -2787,7 +2885,7 @@ fn identifier(
const result = try gz.add(.{
.tag = .int_type,
.data = .{ .int_type = .{
- .src_node = gz.zir_code.decl.nodeIndexToRelative(ident),
+ .src_node = gz.astgen.decl.nodeIndexToRelative(ident),
.signedness = signedness,
.bit_count = bit_count,
} },
@@ -2821,11 +2919,11 @@ fn identifier(
};
}
- const gop = try gz.zir_code.decl_map.getOrPut(mod.gpa, ident_name);
+ const gop = try gz.astgen.decl_map.getOrPut(mod.gpa, ident_name);
if (!gop.found_existing) {
const decl = mod.lookupDeclName(scope, ident_name) orelse
return mod.failNode(scope, ident, "use of undeclared identifier '{s}'", .{ident_name});
- try gz.zir_code.decls.append(mod.gpa, decl);
+ try gz.astgen.decls.append(mod.gpa, decl);
}
const decl_index = @intCast(u32, gop.index);
switch (rl) {
@@ -2843,7 +2941,7 @@ fn stringLiteral(
const tree = scope.tree();
const main_tokens = tree.nodes.items(.main_token);
const gz = scope.getGenZir();
- const string_bytes = &gz.zir_code.string_bytes;
+ const string_bytes = &gz.astgen.string_bytes;
const str_index = string_bytes.items.len;
const str_lit_token = main_tokens[node];
const token_bytes = tree.tokenSlice(str_lit_token);
@@ -2872,7 +2970,7 @@ fn multilineStringLiteral(
const start = node_datas[node].lhs;
const end = node_datas[node].rhs;
- const string_bytes = &gz.zir_code.string_bytes;
+ const string_bytes = &gz.astgen.string_bytes;
const str_index = string_bytes.items.len;
// First line: do not append a newline.
@@ -3000,7 +3098,7 @@ fn asmExpr(
for (full.inputs) |input, i| {
const constraint_token = main_tokens[input] + 2;
- const string_bytes = &gz.zir_code.string_bytes;
+ const string_bytes = &gz.astgen.string_bytes;
constraints[i] = @intCast(u32, string_bytes.items.len);
const token_bytes = tree.tokenSlice(constraint_token);
try mod.parseStrLit(scope, constraint_token, string_bytes, token_bytes, 0);
@@ -3018,10 +3116,10 @@ fn asmExpr(
.clobbers_len = 0, // TODO implement asm clobbers
});
- try gz.zir_code.extra.ensureCapacity(mod.gpa, gz.zir_code.extra.items.len +
+ try gz.astgen.extra.ensureCapacity(mod.gpa, gz.astgen.extra.items.len +
args.len + constraints.len);
- gz.zir_code.appendRefsAssumeCapacity(args);
- gz.zir_code.extra.appendSliceAssumeCapacity(constraints);
+ gz.astgen.appendRefsAssumeCapacity(args);
+ gz.astgen.extra.appendSliceAssumeCapacity(constraints);
return rvalue(mod, scope, rl, result, node);
}
@@ -3072,11 +3170,11 @@ fn asRlPtr(
// result location. If it does, elide the coerce_result_ptr instruction
// as well as the store instruction, instead passing the result as an rvalue.
const parent_gz = scope.getGenZir();
- const wzc = parent_gz.zir_code;
+ const astgen = parent_gz.astgen;
var as_scope: Scope.GenZir = .{
.parent = scope,
- .zir_code = wzc,
+ .astgen = astgen,
.force_comptime = parent_gz.force_comptime,
.instructions = .{},
};
@@ -3087,12 +3185,12 @@ fn asRlPtr(
const parent_zir = &parent_gz.instructions;
if (as_scope.rvalue_rl_count == 1) {
// Busted! This expression didn't actually need a pointer.
- const zir_tags = wzc.instructions.items(.tag);
- const zir_datas = wzc.instructions.items(.data);
+ const zir_tags = astgen.instructions.items(.tag);
+ const zir_datas = astgen.instructions.items(.data);
const expected_len = parent_zir.items.len + as_scope.instructions.items.len - 2;
try parent_zir.ensureCapacity(mod.gpa, expected_len);
for (as_scope.instructions.items) |src_inst| {
- if (wzc.indexToRef(src_inst) == as_scope.rl_ptr) continue;
+ if (astgen.indexToRef(src_inst) == as_scope.rl_ptr) continue;
if (zir_tags[src_inst] == .store_to_block_ptr) {
if (zir_datas[src_inst].bin.lhs == as_scope.rl_ptr) continue;
}
@@ -3186,7 +3284,7 @@ fn typeOf(
const result = try gz.addPlNode(.typeof_peer, node, zir.Inst.MultiOp{
.operands_len = @intCast(u32, params.len),
});
- try gz.zir_code.appendRefs(items);
+ try gz.astgen.appendRefs(items);
return rvalue(mod, scope, rl, result, node);
}
@@ -3252,7 +3350,7 @@ fn builtinCall(
.breakpoint => {
const result = try gz.add(.{
.tag = .breakpoint,
- .data = .{ .node = gz.zir_code.decl.nodeIndexToRelative(node) },
+ .data = .{ .node = gz.astgen.decl.nodeIndexToRelative(node) },
});
return rvalue(mod, scope, rl, result, node);
},
@@ -3280,7 +3378,7 @@ fn builtinCall(
const result = try gz.addPlNode(.compile_log, node, zir.Inst.MultiOp{
.operands_len = @intCast(u32, params.len),
});
- try gz.zir_code.appendRefs(arg_refs);
+ try gz.astgen.appendRefs(arg_refs);
return rvalue(mod, scope, rl, result, node);
},
.field => {
diff --git a/src/Module.zig b/src/Module.zig
index c8c1f06538..de26043050 100644
--- a/src/Module.zig
+++ b/src/Module.zig
@@ -23,7 +23,7 @@ const link = @import("link.zig");
const ir = @import("ir.zig");
const zir = @import("zir.zig");
const trace = @import("tracy.zig").trace;
-const astgen = @import("astgen.zig");
+const AstGen = @import("AstGen.zig");
const Sema = @import("Sema.zig");
const target_util = @import("target.zig");
@@ -407,9 +407,9 @@ pub const Scope = struct {
pub fn arena(scope: *Scope) *Allocator {
switch (scope.tag) {
.block => return scope.cast(Block).?.sema.arena,
- .gen_zir => return scope.cast(GenZir).?.zir_code.arena,
- .local_val => return scope.cast(LocalVal).?.gen_zir.zir_code.arena,
- .local_ptr => return scope.cast(LocalPtr).?.gen_zir.zir_code.arena,
+ .gen_zir => return scope.cast(GenZir).?.astgen.arena,
+ .local_val => return scope.cast(LocalVal).?.gen_zir.astgen.arena,
+ .local_ptr => return scope.cast(LocalPtr).?.gen_zir.astgen.arena,
.file => unreachable,
.container => unreachable,
.decl_ref => unreachable,
@@ -419,9 +419,9 @@ pub const Scope = struct {
pub fn ownerDecl(scope: *Scope) ?*Decl {
return switch (scope.tag) {
.block => scope.cast(Block).?.sema.owner_decl,
- .gen_zir => scope.cast(GenZir).?.zir_code.decl,
- .local_val => scope.cast(LocalVal).?.gen_zir.zir_code.decl,
- .local_ptr => scope.cast(LocalPtr).?.gen_zir.zir_code.decl,
+ .gen_zir => scope.cast(GenZir).?.astgen.decl,
+ .local_val => scope.cast(LocalVal).?.gen_zir.astgen.decl,
+ .local_ptr => scope.cast(LocalPtr).?.gen_zir.astgen.decl,
.file => null,
.container => null,
.decl_ref => scope.cast(DeclRef).?.decl,
@@ -431,9 +431,9 @@ pub const Scope = struct {
pub fn srcDecl(scope: *Scope) ?*Decl {
return switch (scope.tag) {
.block => scope.cast(Block).?.src_decl,
- .gen_zir => scope.cast(GenZir).?.zir_code.decl,
- .local_val => scope.cast(LocalVal).?.gen_zir.zir_code.decl,
- .local_ptr => scope.cast(LocalPtr).?.gen_zir.zir_code.decl,
+ .gen_zir => scope.cast(GenZir).?.astgen.decl,
+ .local_val => scope.cast(LocalVal).?.gen_zir.astgen.decl,
+ .local_ptr => scope.cast(LocalPtr).?.gen_zir.astgen.decl,
.file => null,
.container => null,
.decl_ref => scope.cast(DeclRef).?.decl,
@@ -444,9 +444,9 @@ pub const Scope = struct {
pub fn namespace(scope: *Scope) *Container {
switch (scope.tag) {
.block => return scope.cast(Block).?.sema.owner_decl.container,
- .gen_zir => return scope.cast(GenZir).?.zir_code.decl.container,
- .local_val => return scope.cast(LocalVal).?.gen_zir.zir_code.decl.container,
- .local_ptr => return scope.cast(LocalPtr).?.gen_zir.zir_code.decl.container,
+ .gen_zir => return scope.cast(GenZir).?.astgen.decl.container,
+ .local_val => return scope.cast(LocalVal).?.gen_zir.astgen.decl.container,
+ .local_ptr => return scope.cast(LocalPtr).?.gen_zir.astgen.decl.container,
.file => return &scope.cast(File).?.root_container,
.container => return scope.cast(Container).?,
.decl_ref => return scope.cast(DeclRef).?.decl.container,
@@ -474,8 +474,8 @@ pub const Scope = struct {
.file => return &scope.cast(File).?.tree,
.block => return &scope.cast(Block).?.src_decl.container.file_scope.tree,
.gen_zir => return scope.cast(GenZir).?.tree(),
- .local_val => return &scope.cast(LocalVal).?.gen_zir.zir_code.decl.container.file_scope.tree,
- .local_ptr => return &scope.cast(LocalPtr).?.gen_zir.zir_code.decl.container.file_scope.tree,
+ .local_val => return &scope.cast(LocalVal).?.gen_zir.astgen.decl.container.file_scope.tree,
+ .local_ptr => return &scope.cast(LocalPtr).?.gen_zir.astgen.decl.container.file_scope.tree,
.container => return &scope.cast(Container).?.file_scope.tree,
.decl_ref => return &scope.cast(DeclRef).?.decl.container.file_scope.tree,
}
@@ -913,15 +913,15 @@ pub const Scope = struct {
/// Parents can be: `GenZir`, `File`
parent: *Scope,
/// All `GenZir` scopes for the same ZIR share this.
- zir_code: *WipZirCode,
+ astgen: *AstGen,
/// Keeps track of the list of instructions in this scope only. Indexes
- /// to instructions in `zir_code`.
+ /// to instructions in `astgen`.
instructions: ArrayListUnmanaged(zir.Inst.Index) = .{},
label: ?Label = null,
break_block: zir.Inst.Index = 0,
continue_block: zir.Inst.Index = 0,
/// Only valid when setBlockResultLoc is called.
- break_result_loc: astgen.ResultLoc = undefined,
+ break_result_loc: AstGen.ResultLoc = undefined,
/// When a block has a pointer result location, here it is.
rl_ptr: zir.Inst.Ref = .none,
/// Keeps track of how many branches of a block did not actually
@@ -948,49 +948,51 @@ pub const Scope = struct {
};
/// Only valid to call on the top of the `GenZir` stack. Completes the
- /// `WipZirCode` into a `zir.Code`. Leaves the `WipZirCode` in an
+ /// `AstGen` into a `zir.Code`. Leaves the `AstGen` in an
/// initialized, but empty, state.
pub fn finish(gz: *GenZir) !zir.Code {
- const gpa = gz.zir_code.gpa;
+ const gpa = gz.astgen.mod.gpa;
try gz.setBlockBody(0);
return zir.Code{
- .instructions = gz.zir_code.instructions.toOwnedSlice(),
- .string_bytes = gz.zir_code.string_bytes.toOwnedSlice(gpa),
- .extra = gz.zir_code.extra.toOwnedSlice(gpa),
- .decls = gz.zir_code.decls.toOwnedSlice(gpa),
+ .instructions = gz.astgen.instructions.toOwnedSlice(),
+ .string_bytes = gz.astgen.string_bytes.toOwnedSlice(gpa),
+ .extra = gz.astgen.extra.toOwnedSlice(gpa),
+ .decls = gz.astgen.decls.toOwnedSlice(gpa),
};
}
pub fn tokSrcLoc(gz: GenZir, token_index: ast.TokenIndex) LazySrcLoc {
- return gz.zir_code.decl.tokSrcLoc(token_index);
+ return gz.astgen.decl.tokSrcLoc(token_index);
}
pub fn nodeSrcLoc(gz: GenZir, node_index: ast.Node.Index) LazySrcLoc {
- return gz.zir_code.decl.nodeSrcLoc(node_index);
+ return gz.astgen.decl.nodeSrcLoc(node_index);
}
pub fn tree(gz: *const GenZir) *const ast.Tree {
- return &gz.zir_code.decl.container.file_scope.tree;
+ return &gz.astgen.decl.container.file_scope.tree;
}
pub fn setBoolBrBody(gz: GenZir, inst: zir.Inst.Index) !void {
- try gz.zir_code.extra.ensureCapacity(gz.zir_code.gpa, gz.zir_code.extra.items.len +
+ const gpa = gz.astgen.mod.gpa;
+ try gz.astgen.extra.ensureCapacity(gpa, gz.astgen.extra.items.len +
@typeInfo(zir.Inst.Block).Struct.fields.len + gz.instructions.items.len);
- const zir_datas = gz.zir_code.instructions.items(.data);
- zir_datas[inst].bool_br.payload_index = gz.zir_code.addExtraAssumeCapacity(
+ const zir_datas = gz.astgen.instructions.items(.data);
+ zir_datas[inst].bool_br.payload_index = gz.astgen.addExtraAssumeCapacity(
zir.Inst.Block{ .body_len = @intCast(u32, gz.instructions.items.len) },
);
- gz.zir_code.extra.appendSliceAssumeCapacity(gz.instructions.items);
+ gz.astgen.extra.appendSliceAssumeCapacity(gz.instructions.items);
}
pub fn setBlockBody(gz: GenZir, inst: zir.Inst.Index) !void {
- try gz.zir_code.extra.ensureCapacity(gz.zir_code.gpa, gz.zir_code.extra.items.len +
+ const gpa = gz.astgen.mod.gpa;
+ try gz.astgen.extra.ensureCapacity(gpa, gz.astgen.extra.items.len +
@typeInfo(zir.Inst.Block).Struct.fields.len + gz.instructions.items.len);
- const zir_datas = gz.zir_code.instructions.items(.data);
- zir_datas[inst].pl_node.payload_index = gz.zir_code.addExtraAssumeCapacity(
+ const zir_datas = gz.astgen.instructions.items(.data);
+ zir_datas[inst].pl_node.payload_index = gz.astgen.addExtraAssumeCapacity(
zir.Inst.Block{ .body_len = @intCast(u32, gz.instructions.items.len) },
);
- gz.zir_code.extra.appendSliceAssumeCapacity(gz.instructions.items);
+ gz.astgen.extra.appendSliceAssumeCapacity(gz.instructions.items);
}
pub fn addFnTypeCc(gz: *GenZir, tag: zir.Inst.Tag, args: struct {
@@ -1000,20 +1002,20 @@ pub const Scope = struct {
}) !zir.Inst.Ref {
assert(args.ret_ty != .none);
assert(args.cc != .none);
- const gpa = gz.zir_code.gpa;
+ const gpa = gz.astgen.mod.gpa;
try gz.instructions.ensureCapacity(gpa, gz.instructions.items.len + 1);
- try gz.zir_code.instructions.ensureCapacity(gpa, gz.zir_code.instructions.len + 1);
- try gz.zir_code.extra.ensureCapacity(gpa, gz.zir_code.extra.items.len +
+ try gz.astgen.instructions.ensureCapacity(gpa, gz.astgen.instructions.len + 1);
+ try gz.astgen.extra.ensureCapacity(gpa, gz.astgen.extra.items.len +
@typeInfo(zir.Inst.FnTypeCc).Struct.fields.len + args.param_types.len);
- const payload_index = gz.zir_code.addExtraAssumeCapacity(zir.Inst.FnTypeCc{
+ const payload_index = gz.astgen.addExtraAssumeCapacity(zir.Inst.FnTypeCc{
.cc = args.cc,
.param_types_len = @intCast(u32, args.param_types.len),
});
- gz.zir_code.appendRefsAssumeCapacity(args.param_types);
+ gz.astgen.appendRefsAssumeCapacity(args.param_types);
- const new_index = @intCast(zir.Inst.Index, gz.zir_code.instructions.len);
- gz.zir_code.instructions.appendAssumeCapacity(.{
+ const new_index = @intCast(zir.Inst.Index, gz.astgen.instructions.len);
+ gz.astgen.instructions.appendAssumeCapacity(.{
.tag = tag,
.data = .{ .fn_type = .{
.return_type = args.ret_ty,
@@ -1021,7 +1023,7 @@ pub const Scope = struct {
} },
});
gz.instructions.appendAssumeCapacity(new_index);
- return gz.zir_code.indexToRef(new_index);
+ return gz.astgen.indexToRef(new_index);
}
pub fn addFnType(
@@ -1031,19 +1033,19 @@ pub const Scope = struct {
param_types: []const zir.Inst.Ref,
) !zir.Inst.Ref {
assert(ret_ty != .none);
- const gpa = gz.zir_code.gpa;
+ const gpa = gz.astgen.mod.gpa;
try gz.instructions.ensureCapacity(gpa, gz.instructions.items.len + 1);
- try gz.zir_code.instructions.ensureCapacity(gpa, gz.zir_code.instructions.len + 1);
- try gz.zir_code.extra.ensureCapacity(gpa, gz.zir_code.extra.items.len +
+ try gz.astgen.instructions.ensureCapacity(gpa, gz.astgen.instructions.len + 1);
+ try gz.astgen.extra.ensureCapacity(gpa, gz.astgen.extra.items.len +
@typeInfo(zir.Inst.FnType).Struct.fields.len + param_types.len);
- const payload_index = gz.zir_code.addExtraAssumeCapacity(zir.Inst.FnType{
+ const payload_index = gz.astgen.addExtraAssumeCapacity(zir.Inst.FnType{
.param_types_len = @intCast(u32, param_types.len),
});
- gz.zir_code.appendRefsAssumeCapacity(param_types);
+ gz.astgen.appendRefsAssumeCapacity(param_types);
- const new_index = @intCast(zir.Inst.Index, gz.zir_code.instructions.len);
- gz.zir_code.instructions.appendAssumeCapacity(.{
+ const new_index = @intCast(zir.Inst.Index, gz.astgen.instructions.len);
+ gz.astgen.instructions.appendAssumeCapacity(.{
.tag = tag,
.data = .{ .fn_type = .{
.return_type = ret_ty,
@@ -1051,7 +1053,7 @@ pub const Scope = struct {
} },
});
gz.instructions.appendAssumeCapacity(new_index);
- return gz.zir_code.indexToRef(new_index);
+ return gz.astgen.indexToRef(new_index);
}
pub fn addCall(
@@ -1064,28 +1066,28 @@ pub const Scope = struct {
) !zir.Inst.Ref {
assert(callee != .none);
assert(src_node != 0);
- const gpa = gz.zir_code.gpa;
+ const gpa = gz.astgen.mod.gpa;
try gz.instructions.ensureCapacity(gpa, gz.instructions.items.len + 1);
- try gz.zir_code.instructions.ensureCapacity(gpa, gz.zir_code.instructions.len + 1);
- try gz.zir_code.extra.ensureCapacity(gpa, gz.zir_code.extra.items.len +
+ try gz.astgen.instructions.ensureCapacity(gpa, gz.astgen.instructions.len + 1);
+ try gz.astgen.extra.ensureCapacity(gpa, gz.astgen.extra.items.len +
@typeInfo(zir.Inst.Call).Struct.fields.len + args.len);
- const payload_index = gz.zir_code.addExtraAssumeCapacity(zir.Inst.Call{
+ const payload_index = gz.astgen.addExtraAssumeCapacity(zir.Inst.Call{
.callee = callee,
.args_len = @intCast(u32, args.len),
});
- gz.zir_code.appendRefsAssumeCapacity(args);
+ gz.astgen.appendRefsAssumeCapacity(args);
- const new_index = @intCast(zir.Inst.Index, gz.zir_code.instructions.len);
- gz.zir_code.instructions.appendAssumeCapacity(.{
+ const new_index = @intCast(zir.Inst.Index, gz.astgen.instructions.len);
+ gz.astgen.instructions.appendAssumeCapacity(.{
.tag = tag,
.data = .{ .pl_node = .{
- .src_node = gz.zir_code.decl.nodeIndexToRelative(src_node),
+ .src_node = gz.astgen.decl.nodeIndexToRelative(src_node),
.payload_index = payload_index,
} },
});
gz.instructions.appendAssumeCapacity(new_index);
- return gz.zir_code.indexToRef(new_index);
+ return gz.astgen.indexToRef(new_index);
}
/// Note that this returns a `zir.Inst.Index` not a ref.
@@ -1096,12 +1098,12 @@ pub const Scope = struct {
lhs: zir.Inst.Ref,
) !zir.Inst.Index {
assert(lhs != .none);
- const gpa = gz.zir_code.gpa;
+ const gpa = gz.astgen.mod.gpa;
try gz.instructions.ensureCapacity(gpa, gz.instructions.items.len + 1);
- try gz.zir_code.instructions.ensureCapacity(gpa, gz.zir_code.instructions.len + 1);
+ try gz.astgen.instructions.ensureCapacity(gpa, gz.astgen.instructions.len + 1);
- const new_index = @intCast(zir.Inst.Index, gz.zir_code.instructions.len);
- gz.zir_code.instructions.appendAssumeCapacity(.{
+ const new_index = @intCast(zir.Inst.Index, gz.astgen.instructions.len);
+ gz.astgen.instructions.appendAssumeCapacity(.{
.tag = tag,
.data = .{ .bool_br = .{
.lhs = lhs,
@@ -1131,7 +1133,7 @@ pub const Scope = struct {
.tag = tag,
.data = .{ .un_node = .{
.operand = operand,
- .src_node = gz.zir_code.decl.nodeIndexToRelative(src_node),
+ .src_node = gz.astgen.decl.nodeIndexToRelative(src_node),
} },
});
}
@@ -1143,21 +1145,21 @@ pub const Scope = struct {
src_node: ast.Node.Index,
extra: anytype,
) !zir.Inst.Ref {
- const gpa = gz.zir_code.gpa;
+ const gpa = gz.astgen.mod.gpa;
try gz.instructions.ensureCapacity(gpa, gz.instructions.items.len + 1);
- try gz.zir_code.instructions.ensureCapacity(gpa, gz.zir_code.instructions.len + 1);
+ try gz.astgen.instructions.ensureCapacity(gpa, gz.astgen.instructions.len + 1);
- const payload_index = try gz.zir_code.addExtra(extra);
- const new_index = @intCast(zir.Inst.Index, gz.zir_code.instructions.len);
- gz.zir_code.instructions.appendAssumeCapacity(.{
+ const payload_index = try gz.astgen.addExtra(extra);
+ const new_index = @intCast(zir.Inst.Index, gz.astgen.instructions.len);
+ gz.astgen.instructions.appendAssumeCapacity(.{
.tag = tag,
.data = .{ .pl_node = .{
- .src_node = gz.zir_code.decl.nodeIndexToRelative(src_node),
+ .src_node = gz.astgen.decl.nodeIndexToRelative(src_node),
.payload_index = payload_index,
} },
});
gz.instructions.appendAssumeCapacity(new_index);
- return gz.zir_code.indexToRef(new_index);
+ return gz.astgen.indexToRef(new_index);
}
pub fn addArrayTypeSentinel(
@@ -1166,16 +1168,16 @@ pub const Scope = struct {
sentinel: zir.Inst.Ref,
elem_type: zir.Inst.Ref,
) !zir.Inst.Ref {
- const gpa = gz.zir_code.gpa;
+ const gpa = gz.astgen.mod.gpa;
try gz.instructions.ensureCapacity(gpa, gz.instructions.items.len + 1);
- try gz.zir_code.instructions.ensureCapacity(gpa, gz.zir_code.instructions.len + 1);
+ try gz.astgen.instructions.ensureCapacity(gpa, gz.astgen.instructions.len + 1);
- const payload_index = try gz.zir_code.addExtra(zir.Inst.ArrayTypeSentinel{
+ const payload_index = try gz.astgen.addExtra(zir.Inst.ArrayTypeSentinel{
.sentinel = sentinel,
.elem_type = elem_type,
});
- const new_index = @intCast(zir.Inst.Index, gz.zir_code.instructions.len);
- gz.zir_code.instructions.appendAssumeCapacity(.{
+ const new_index = @intCast(zir.Inst.Index, gz.astgen.instructions.len);
+ gz.astgen.instructions.appendAssumeCapacity(.{
.tag = .array_type_sentinel,
.data = .{ .array_type_sentinel = .{
.len = len,
@@ -1183,7 +1185,7 @@ pub const Scope = struct {
} },
});
gz.instructions.appendAssumeCapacity(new_index);
- return gz.zir_code.indexToRef(new_index);
+ return gz.astgen.indexToRef(new_index);
}
pub fn addUnTok(
@@ -1198,7 +1200,7 @@ pub const Scope = struct {
.tag = tag,
.data = .{ .un_tok = .{
.operand = operand,
- .src_tok = abs_tok_index - gz.zir_code.decl.srcToken(),
+ .src_tok = abs_tok_index - gz.astgen.decl.srcToken(),
} },
});
}
@@ -1214,7 +1216,7 @@ pub const Scope = struct {
.tag = tag,
.data = .{ .str_tok = .{
.start = str_index,
- .src_tok = abs_tok_index - gz.zir_code.decl.srcToken(),
+ .src_tok = abs_tok_index - gz.astgen.decl.srcToken(),
} },
});
}
@@ -1260,7 +1262,7 @@ pub const Scope = struct {
return gz.add(.{
.tag = tag,
.data = .{ .pl_node = .{
- .src_node = gz.zir_code.decl.nodeIndexToRelative(src_node),
+ .src_node = gz.astgen.decl.nodeIndexToRelative(src_node),
.payload_index = decl_index,
} },
});
@@ -1274,7 +1276,7 @@ pub const Scope = struct {
) !zir.Inst.Ref {
return gz.add(.{
.tag = tag,
- .data = .{ .node = gz.zir_code.decl.nodeIndexToRelative(src_node) },
+ .data = .{ .node = gz.astgen.decl.nodeIndexToRelative(src_node) },
});
}
@@ -1298,11 +1300,12 @@ pub const Scope = struct {
/// Does *not* append the block instruction to the scope.
/// Leaves the `payload_index` field undefined.
pub fn addBlock(gz: *GenZir, tag: zir.Inst.Tag, node: ast.Node.Index) !zir.Inst.Index {
- const new_index = @intCast(zir.Inst.Index, gz.zir_code.instructions.len);
- try gz.zir_code.instructions.append(gz.zir_code.gpa, .{
+ const new_index = @intCast(zir.Inst.Index, gz.astgen.instructions.len);
+ const gpa = gz.astgen.mod.gpa;
+ try gz.astgen.instructions.append(gpa, .{
.tag = tag,
.data = .{ .pl_node = .{
- .src_node = gz.zir_code.decl.nodeIndexToRelative(node),
+ .src_node = gz.astgen.decl.nodeIndexToRelative(node),
.payload_index = undefined,
} },
});
@@ -1312,12 +1315,13 @@ pub const Scope = struct {
/// Note that this returns a `zir.Inst.Index` not a ref.
/// Leaves the `payload_index` field undefined.
pub fn addCondBr(gz: *GenZir, tag: zir.Inst.Tag, node: ast.Node.Index) !zir.Inst.Index {
- try gz.instructions.ensureCapacity(gz.zir_code.gpa, gz.instructions.items.len + 1);
- const new_index = @intCast(zir.Inst.Index, gz.zir_code.instructions.len);
- try gz.zir_code.instructions.append(gz.zir_code.gpa, .{
+ const gpa = gz.astgen.mod.gpa;
+ try gz.instructions.ensureCapacity(gpa, gz.instructions.items.len + 1);
+ const new_index = @intCast(zir.Inst.Index, gz.astgen.instructions.len);
+ try gz.astgen.instructions.append(gpa, .{
.tag = tag,
.data = .{ .pl_node = .{
- .src_node = gz.zir_code.decl.nodeIndexToRelative(node),
+ .src_node = gz.astgen.decl.nodeIndexToRelative(node),
.payload_index = undefined,
} },
});
@@ -1326,16 +1330,16 @@ pub const Scope = struct {
}
pub fn add(gz: *GenZir, inst: zir.Inst) !zir.Inst.Ref {
- return gz.zir_code.indexToRef(try gz.addAsIndex(inst));
+ return gz.astgen.indexToRef(try gz.addAsIndex(inst));
}
pub fn addAsIndex(gz: *GenZir, inst: zir.Inst) !zir.Inst.Index {
- const gpa = gz.zir_code.gpa;
+ const gpa = gz.astgen.mod.gpa;
try gz.instructions.ensureCapacity(gpa, gz.instructions.items.len + 1);
- try gz.zir_code.instructions.ensureCapacity(gpa, gz.zir_code.instructions.len + 1);
+ try gz.astgen.instructions.ensureCapacity(gpa, gz.astgen.instructions.len + 1);
- const new_index = @intCast(zir.Inst.Index, gz.zir_code.instructions.len);
- gz.zir_code.instructions.appendAssumeCapacity(inst);
+ const new_index = @intCast(zir.Inst.Index, gz.astgen.instructions.len);
+ gz.astgen.instructions.appendAssumeCapacity(inst);
gz.instructions.appendAssumeCapacity(new_index);
return new_index;
}
@@ -1378,100 +1382,6 @@ pub const Scope = struct {
};
};
-/// A Work-In-Progress `zir.Code`. This is a shared parent of all
-/// `GenZir` scopes. Once the `zir.Code` is produced, this struct
-/// is deinitialized.
-/// The `GenZir.finish` function converts this to a `zir.Code`.
-pub const WipZirCode = struct {
- instructions: std.MultiArrayList(zir.Inst) = .{},
- string_bytes: ArrayListUnmanaged(u8) = .{},
- extra: ArrayListUnmanaged(u32) = .{},
- decl_map: std.StringArrayHashMapUnmanaged(void) = .{},
- decls: ArrayListUnmanaged(*Decl) = .{},
- /// The end of special indexes. `zir.Inst.Ref` subtracts against this number to convert
- /// to `zir.Inst.Index`. The default here is correct if there are 0 parameters.
- ref_start_index: u32 = zir.Inst.Ref.typed_value_map.len,
- decl: *Decl,
- gpa: *Allocator,
- arena: *Allocator,
-
- pub fn addExtra(wzc: *WipZirCode, extra: anytype) Allocator.Error!u32 {
- const fields = std.meta.fields(@TypeOf(extra));
- try wzc.extra.ensureCapacity(wzc.gpa, wzc.extra.items.len + fields.len);
- return addExtraAssumeCapacity(wzc, extra);
- }
-
- pub fn addExtraAssumeCapacity(wzc: *WipZirCode, extra: anytype) u32 {
- const fields = std.meta.fields(@TypeOf(extra));
- const result = @intCast(u32, wzc.extra.items.len);
- inline for (fields) |field| {
- wzc.extra.appendAssumeCapacity(switch (field.field_type) {
- u32 => @field(extra, field.name),
- zir.Inst.Ref => @enumToInt(@field(extra, field.name)),
- else => @compileError("bad field type"),
- });
- }
- return result;
- }
-
- pub fn appendRefs(wzc: *WipZirCode, refs: []const zir.Inst.Ref) !void {
- const coerced = @bitCast([]const u32, refs);
- return wzc.extra.appendSlice(wzc.gpa, coerced);
- }
-
- pub fn appendRefsAssumeCapacity(wzc: *WipZirCode, refs: []const zir.Inst.Ref) void {
- const coerced = @bitCast([]const u32, refs);
- wzc.extra.appendSliceAssumeCapacity(coerced);
- }
-
- pub fn refIsNoReturn(wzc: WipZirCode, inst_ref: zir.Inst.Ref) bool {
- if (inst_ref == .unreachable_value) return true;
- if (wzc.refToIndex(inst_ref)) |inst_index| {
- return wzc.instructions.items(.tag)[inst_index].isNoReturn();
- }
- return false;
- }
-
- pub fn indexToRef(wzc: WipZirCode, inst: zir.Inst.Index) zir.Inst.Ref {
- return @intToEnum(zir.Inst.Ref, wzc.ref_start_index + inst);
- }
-
- pub fn refToIndex(wzc: WipZirCode, inst: zir.Inst.Ref) ?zir.Inst.Index {
- const ref_int = @enumToInt(inst);
- if (ref_int >= wzc.ref_start_index) {
- return ref_int - wzc.ref_start_index;
- } else {
- return null;
- }
- }
-
- pub fn deinit(wzc: *WipZirCode) void {
- wzc.instructions.deinit(wzc.gpa);
- wzc.extra.deinit(wzc.gpa);
- wzc.string_bytes.deinit(wzc.gpa);
- wzc.decl_map.deinit(wzc.gpa);
- wzc.decls.deinit(wzc.gpa);
- }
-};
-
-/// Call `deinit` on the result.
-fn initAstGen(mod: *Module, decl: *Decl, arena: *Allocator) !WipZirCode {
- var wzc: WipZirCode = .{
- .decl = decl,
- .arena = arena,
- .gpa = mod.gpa,
- };
- // Must be a block instruction at index 0 with the root body.
- try wzc.instructions.append(mod.gpa, .{
- .tag = .block,
- .data = .{ .pl_node = .{
- .src_node = 0,
- .payload_index = undefined,
- } },
- });
- return wzc;
-}
-
/// This struct holds data necessary to construct API-facing `AllErrors.Message`.
/// Its memory is managed with the general purpose allocator so that they
/// can be created and destroyed in response to incremental updates.
@@ -2102,18 +2012,18 @@ fn astgenAndSemaDecl(mod: *Module, decl: *Decl) !bool {
defer analysis_arena.deinit();
var code: zir.Code = blk: {
- var wip_zir_code = try mod.initAstGen(decl, &analysis_arena.allocator);
- defer wip_zir_code.deinit();
+ var astgen = try AstGen.init(mod, decl, &analysis_arena.allocator);
+ defer astgen.deinit();
var gen_scope: Scope.GenZir = .{
.force_comptime = true,
.parent = &decl.container.base,
- .zir_code = &wip_zir_code,
+ .astgen = &astgen,
};
defer gen_scope.instructions.deinit(mod.gpa);
const block_expr = node_datas[decl_node].lhs;
- _ = try astgen.comptimeExpr(mod, &gen_scope.base, .none, block_expr);
+ _ = try AstGen.comptimeExpr(mod, &gen_scope.base, .none, block_expr);
const code = try gen_scope.finish();
if (std.builtin.mode == .Debug and mod.comp.verbose_ir) {
@@ -2175,13 +2085,13 @@ fn astgenAndSemaFn(
var fn_type_scope_arena = std.heap.ArenaAllocator.init(mod.gpa);
defer fn_type_scope_arena.deinit();
- var fn_type_wip_zir_code = try mod.initAstGen(decl, &fn_type_scope_arena.allocator);
- defer fn_type_wip_zir_code.deinit();
+ var fn_type_astgen = try AstGen.init(mod, decl, &fn_type_scope_arena.allocator);
+ defer fn_type_astgen.deinit();
var fn_type_scope: Scope.GenZir = .{
.force_comptime = true,
.parent = &decl.container.base,
- .zir_code = &fn_type_wip_zir_code,
+ .astgen = &fn_type_astgen,
};
defer fn_type_scope.instructions.deinit(mod.gpa);
@@ -2223,7 +2133,7 @@ fn astgenAndSemaFn(
const param_type_node = param.type_expr;
assert(param_type_node != 0);
param_types[param_type_i] =
- try astgen.expr(mod, &fn_type_scope.base, .{ .ty = .type_type }, param_type_node);
+ try AstGen.expr(mod, &fn_type_scope.base, .{ .ty = .type_type }, param_type_node);
}
assert(param_type_i == param_count);
}
@@ -2292,7 +2202,7 @@ fn astgenAndSemaFn(
if (token_tags[maybe_bang] == .bang) {
return mod.failTok(&fn_type_scope.base, maybe_bang, "TODO implement inferred error sets", .{});
}
- const return_type_inst = try astgen.expr(
+ const return_type_inst = try AstGen.expr(
mod,
&fn_type_scope.base,
.{ .ty = .type_type },
@@ -2308,7 +2218,7 @@ fn astgenAndSemaFn(
// TODO instead of enum literal type, this needs to be the
// std.builtin.CallingConvention enum. We need to implement importing other files
// and enums in order to fix this.
- try astgen.comptimeExpr(
+ try AstGen.comptimeExpr(
mod,
&fn_type_scope.base,
.{ .ty = .enum_literal_type },
@@ -2408,21 +2318,21 @@ fn astgenAndSemaFn(
const fn_zir: zir.Code = blk: {
// We put the ZIR inside the Decl arena.
- var wip_zir_code = try mod.initAstGen(decl, &decl_arena.allocator);
- wip_zir_code.ref_start_index = @intCast(u32, zir.Inst.Ref.typed_value_map.len + param_count);
- defer wip_zir_code.deinit();
+ var astgen = try AstGen.init(mod, decl, &decl_arena.allocator);
+ astgen.ref_start_index = @intCast(u32, zir.Inst.Ref.typed_value_map.len + param_count);
+ defer astgen.deinit();
var gen_scope: Scope.GenZir = .{
.force_comptime = false,
.parent = &decl.container.base,
- .zir_code = &wip_zir_code,
+ .astgen = &astgen,
};
defer gen_scope.instructions.deinit(mod.gpa);
// Iterate over the parameters. We put the param names as the first N
// items inside `extra` so that debug info later can refer to the parameter names
// even while the respective source code is unloaded.
- try wip_zir_code.extra.ensureCapacity(mod.gpa, param_count);
+ try astgen.extra.ensureCapacity(mod.gpa, param_count);
var params_scope = &gen_scope.base;
var i: usize = 0;
@@ -2443,18 +2353,18 @@ fn astgenAndSemaFn(
// Additionally put the param name into `string_bytes` and reference it with
// `extra` so that we have access to the data in codegen, for debug info.
- const str_index = @intCast(u32, wip_zir_code.string_bytes.items.len);
- wip_zir_code.extra.appendAssumeCapacity(str_index);
- const used_bytes = wip_zir_code.string_bytes.items.len;
- try wip_zir_code.string_bytes.ensureCapacity(mod.gpa, used_bytes + param_name.len + 1);
- wip_zir_code.string_bytes.appendSliceAssumeCapacity(param_name);
- wip_zir_code.string_bytes.appendAssumeCapacity(0);
+ const str_index = @intCast(u32, astgen.string_bytes.items.len);
+ astgen.extra.appendAssumeCapacity(str_index);
+ const used_bytes = astgen.string_bytes.items.len;
+ try astgen.string_bytes.ensureCapacity(mod.gpa, used_bytes + param_name.len + 1);
+ astgen.string_bytes.appendSliceAssumeCapacity(param_name);
+ astgen.string_bytes.appendAssumeCapacity(0);
}
- _ = try astgen.expr(mod, params_scope, .none, body_node);
+ _ = try AstGen.expr(mod, params_scope, .none, body_node);
if (gen_scope.instructions.items.len == 0 or
- !wip_zir_code.instructions.items(.tag)[gen_scope.instructions.items.len - 1]
+ !astgen.instructions.items(.tag)[gen_scope.instructions.items.len - 1]
.isNoReturn())
{
// astgen uses result location semantics to coerce return operands.
@@ -2615,21 +2525,21 @@ fn astgenAndSemaVarDecl(
var gen_scope_arena = std.heap.ArenaAllocator.init(mod.gpa);
defer gen_scope_arena.deinit();
- var wip_zir_code = try mod.initAstGen(decl, &gen_scope_arena.allocator);
- defer wip_zir_code.deinit();
+ var astgen = try AstGen.init(mod, decl, &gen_scope_arena.allocator);
+ defer astgen.deinit();
var gen_scope: Scope.GenZir = .{
.force_comptime = true,
.parent = &decl.container.base,
- .zir_code = &wip_zir_code,
+ .astgen = &astgen,
};
defer gen_scope.instructions.deinit(mod.gpa);
- const init_result_loc: astgen.ResultLoc = if (var_decl.ast.type_node != 0) .{
- .ty = try astgen.expr(mod, &gen_scope.base, .{ .ty = .type_type }, var_decl.ast.type_node),
+ const init_result_loc: AstGen.ResultLoc = if (var_decl.ast.type_node != 0) .{
+ .ty = try AstGen.expr(mod, &gen_scope.base, .{ .ty = .type_type }, var_decl.ast.type_node),
} else .none;
- const init_inst = try astgen.comptimeExpr(
+ const init_inst = try AstGen.comptimeExpr(
mod,
&gen_scope.base,
init_result_loc,
@@ -2684,17 +2594,17 @@ fn astgenAndSemaVarDecl(
var type_scope_arena = std.heap.ArenaAllocator.init(mod.gpa);
defer type_scope_arena.deinit();
- var wip_zir_code = try mod.initAstGen(decl, &type_scope_arena.allocator);
- defer wip_zir_code.deinit();
+ var astgen = try AstGen.init(mod, decl, &type_scope_arena.allocator);
+ defer astgen.deinit();
var type_scope: Scope.GenZir = .{
.force_comptime = true,
.parent = &decl.container.base,
- .zir_code = &wip_zir_code,
+ .astgen = &astgen,
};
defer type_scope.instructions.deinit(mod.gpa);
- const var_type = try astgen.typeExpr(mod, &type_scope.base, var_decl.ast.type_node);
+ const var_type = try AstGen.typeExpr(mod, &type_scope.base, var_decl.ast.type_node);
_ = try type_scope.addBreak(.break_inline, 0, var_type);
var code = try type_scope.finish();
@@ -3796,21 +3706,21 @@ pub fn failWithOwnedErrorMsg(mod: *Module, scope: *Scope, err_msg: *ErrorMsg) In
},
.gen_zir => {
const gen_zir = scope.cast(Scope.GenZir).?;
- gen_zir.zir_code.decl.analysis = .sema_failure;
- gen_zir.zir_code.decl.generation = mod.generation;
- mod.failed_decls.putAssumeCapacityNoClobber(gen_zir.zir_code.decl, err_msg);
+ gen_zir.astgen.decl.analysis = .sema_failure;
+ gen_zir.astgen.decl.generation = mod.generation;
+ mod.failed_decls.putAssumeCapacityNoClobber(gen_zir.astgen.decl, err_msg);
},
.local_val => {
const gen_zir = scope.cast(Scope.LocalVal).?.gen_zir;
- gen_zir.zir_code.decl.analysis = .sema_failure;
- gen_zir.zir_code.decl.generation = mod.generation;
- mod.failed_decls.putAssumeCapacityNoClobber(gen_zir.zir_code.decl, err_msg);
+ gen_zir.astgen.decl.analysis = .sema_failure;
+ gen_zir.astgen.decl.generation = mod.generation;
+ mod.failed_decls.putAssumeCapacityNoClobber(gen_zir.astgen.decl, err_msg);
},
.local_ptr => {
const gen_zir = scope.cast(Scope.LocalPtr).?.gen_zir;
- gen_zir.zir_code.decl.analysis = .sema_failure;
- gen_zir.zir_code.decl.generation = mod.generation;
- mod.failed_decls.putAssumeCapacityNoClobber(gen_zir.zir_code.decl, err_msg);
+ gen_zir.astgen.decl.analysis = .sema_failure;
+ gen_zir.astgen.decl.generation = mod.generation;
+ mod.failed_decls.putAssumeCapacityNoClobber(gen_zir.astgen.decl, err_msg);
},
.file => unreachable,
.container => unreachable,
diff --git a/src/translate_c.zig b/src/translate_c.zig
index 8aabc30754..a446d9de41 100644
--- a/src/translate_c.zig
+++ b/src/translate_c.zig
@@ -4266,7 +4266,7 @@ fn isZigPrimitiveType(name: []const u8) bool {
}
return true;
}
- return @import("astgen.zig").simple_types.has(name);
+ return @import("Astgen.zig").simple_types.has(name);
}
const MacroCtx = struct {
diff --git a/src/zir.zig b/src/zir.zig
index 71af10e995..b0a52d6beb 100644
--- a/src/zir.zig
+++ b/src/zir.zig
@@ -1,4 +1,4 @@
-//! Zig Intermediate Representation. astgen.zig converts AST nodes to these
+//! Zig Intermediate Representation. Astgen.zig converts AST nodes to these
//! untyped IR instructions. Next, Sema.zig processes these into TZIR.
const std = @import("std");
@@ -491,7 +491,7 @@ pub const Inst = struct {
store_to_block_ptr,
/// Same as `store` but the type of the value being stored will be used to infer
/// the pointer type.
- /// Uses the `bin` union field - astgen.zig depends on the ability to change
+ /// Uses the `bin` union field - Astgen.zig depends on the ability to change
/// the tag of an instruction from `store_to_block_ptr` to `store_to_inferred_ptr`
/// without changing the data.
store_to_inferred_ptr,