AstGen: implement anytype struct fields

This commit is contained in:
Andrew Kelley 2021-04-29 17:13:18 -07:00
parent 55e86b724a
commit 9e49a65e1b
4 changed files with 25 additions and 13 deletions

View File

@ -483,6 +483,8 @@ pub fn expr(gz: *GenZir, scope: *Scope, rl: ResultLoc, node: ast.Node.Index) Inn
.asm_output => unreachable, // Handled in `asmExpr`.
.asm_input => unreachable, // Handled in `asmExpr`.
.@"anytype" => unreachable, // Handled in `containerDecl`.
.assign => {
try assign(gz, scope, node);
return rvalue(gz, scope, rl, .void_value, node);
@ -869,8 +871,6 @@ pub fn expr(gz: *GenZir, scope: *Scope, rl: ResultLoc, node: ast.Node.Index) Inn
.struct_init_comma,
=> return structInitExpr(gz, scope, rl, node, tree.structInit(node)),
.@"anytype" => return astgen.failNode(node, "TODO implement astgen.expr for .anytype", .{}),
.fn_proto_simple => {
var params: [1]ast.Node.Index = undefined;
return fnProtoExpr(gz, scope, rl, tree.fnProtoSimple(&params, node));
@ -3318,7 +3318,10 @@ fn structDeclInner(
const field_name = try gz.identAsString(member.ast.name_token);
fields_data.appendAssumeCapacity(field_name);
const field_type = try typeExpr(&block_scope, &block_scope.base, member.ast.type_expr);
const field_type: Zir.Inst.Ref = if (node_tags[member.ast.type_expr] == .@"anytype")
.none
else
try typeExpr(&block_scope, &block_scope.base, member.ast.type_expr);
fields_data.appendAssumeCapacity(@enumToInt(field_type));
const have_align = member.ast.align_expr != 0;
@ -3336,7 +3339,9 @@ fn structDeclInner(
fields_data.appendAssumeCapacity(@enumToInt(align_inst));
}
if (have_value) {
const default_inst = try expr(&block_scope, &block_scope.base, .{ .ty = field_type }, member.ast.value_expr);
const rl: ResultLoc = if (field_type == .none) .none else .{ .ty = field_type };
const default_inst = try expr(&block_scope, &block_scope.base, rl, member.ast.value_expr);
fields_data.appendAssumeCapacity(@enumToInt(default_inst));
} else if (member.comptime_token) |comptime_token| {
return astgen.failTok(comptime_token, "comptime field without default initialization value", .{});

View File

@ -467,6 +467,7 @@ pub const Struct = struct {
node_offset: i32,
pub const Field = struct {
/// Uses `noreturn` to indicate `anytype`.
ty: Type,
abi_align: Value,
/// Uses `unreachable_value` to indicate no default.

View File

@ -750,10 +750,16 @@ pub fn zirStructDecl(
// This string needs to outlive the ZIR code.
const field_name = try new_decl_arena.allocator.dupe(u8, field_name_zir);
// TODO: if we need to report an error here, use a source location
// that points to this type expression rather than the struct.
// But only resolve the source location if we need to emit a compile error.
const field_ty = try sema.resolveType(block, src, field_type_ref);
if (field_type_ref == .none) {
return sema.mod.fail(&block.base, src, "TODO: implement anytype struct field", .{});
}
const field_ty: Type = if (field_type_ref == .none)
Type.initTag(.noreturn)
else
// TODO: if we need to report an error here, use a source location
// that points to this type expression rather than the struct.
// But only resolve the source location if we need to emit a compile error.
try sema.resolveType(block, src, field_type_ref);
const gop = struct_obj.fields.getOrPutAssumeCapacity(field_name);
assert(!gop.found_existing);

View File

@ -3367,15 +3367,15 @@ const Writer = struct {
var extra_index: usize = undefined;
if (decls_len == 0) {
try stream.writeAll("}) ");
try stream.writeAll("{}, ");
extra_index = extra.end;
} else {
try stream.writeAll("\n");
try stream.writeAll("{\n");
self.indent += 2;
extra_index = try self.writeDecls(stream, decls_len, extra.end);
self.indent -= 2;
try stream.writeByteNTimes(' ', self.indent);
try stream.writeAll("}) ");
try stream.writeAll("}, ");
}
const body = self.code.extra[extra_index..][0..extra.data.body_len];
@ -3383,7 +3383,7 @@ const Writer = struct {
if (fields_len == 0) {
assert(body.len == 0);
try stream.writeAll("{}, {}, {");
try stream.writeAll("{}, {}) ");
extra_index = extra.end;
} else {
self.indent += 2;
@ -3451,7 +3451,7 @@ const Writer = struct {
self.indent -= 2;
try stream.writeByteNTimes(' ', self.indent);
try stream.writeAll("}, {");
try stream.writeAll("}) ");
}
try self.writeSrc(stream, inst_data.src());
}