An array initialization expression using an array_init or array_init_ref instruction.
fn arrayInitExprTyped(
gz: *GenZir,
scope: *Scope,
node: Ast.Node.Index,
elements: []const Ast.Node.Index,
ty_inst: Zir.Inst.Ref,
maybe_elem_ty_inst: Zir.Inst.Ref,
is_ref: bool,
) InnerError!Zir.Inst.Ref
fn arrayInitExprTyped(
gz: *GenZir,
scope: *Scope,
node: Ast.Node.Index,
elements: []const Ast.Node.Index,
ty_inst: Zir.Inst.Ref,
maybe_elem_ty_inst: Zir.Inst.Ref,
is_ref: bool,
) InnerError!Zir.Inst.Ref {
const astgen = gz.astgen;
const len = elements.len + 1; // +1 for type
const payload_index = try addExtra(astgen, Zir.Inst.MultiOp{
.operands_len = @intCast(len),
});
var extra_index = try reserveExtra(astgen, len);
astgen.extra.items[extra_index] = @backingInt(ty_inst);
extra_index += 1;
if (maybe_elem_ty_inst != .none) {
const elem_ri: ResultInfo = .{ .rl = .{ .coerced_ty = maybe_elem_ty_inst } };
for (elements) |elem_init| {
const elem_inst = try expr(gz, scope, elem_ri, elem_init);
astgen.extra.items[extra_index] = @backingInt(elem_inst);
extra_index += 1;
}
} else {
for (elements, 0..) |elem_init, i| {
const ri: ResultInfo = .{ .rl = .{ .coerced_ty = try gz.add(.{
.tag = .array_init_elem_type,
.data = .{ .bin = .{
.lhs = ty_inst,
.rhs = @fromBackingInt(@intCast(i)),
} },
}) } };
const elem_inst = try expr(gz, scope, ri, elem_init);
astgen.extra.items[extra_index] = @backingInt(elem_inst);
extra_index += 1;
}
}
const tag: Zir.Inst.Tag = if (is_ref) .array_init_ref else .array_init;
return try gz.addPlNodePayloadIndex(tag, node, payload_index);
}