add struct field default value to typeinfo

This commit is contained in:
Vexu 2020-01-16 13:55:39 +02:00 committed by Andrew Kelley
parent df03fcf5f0
commit bac27731e3
3 changed files with 12 additions and 2 deletions

View File

@ -205,6 +205,7 @@ pub const TypeInfo = union(enum) {
name: []const u8,
offset: ?comptime_int,
field_type: type,
default_value: var,
};
/// This data structure is used by the Zig language code generation and

View File

@ -23338,7 +23338,7 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInstruction *source_instr
struct_field_val->special = ConstValSpecialStatic;
struct_field_val->type = type_info_struct_field_type;
ZigValue **inner_fields = alloc_const_vals_ptrs(3);
ZigValue **inner_fields = alloc_const_vals_ptrs(4);
inner_fields[1]->special = ConstValSpecialStatic;
inner_fields[1]->type = get_optional_type(ira->codegen, ira->codegen->builtin_types.entry_num_lit_int);
@ -23361,6 +23361,12 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInstruction *source_instr
inner_fields[2]->type = ira->codegen->builtin_types.entry_type;
inner_fields[2]->data.x_type = struct_field->type_entry;
// default_value: var
inner_fields[3]->special = ConstValSpecialStatic;
inner_fields[3]->type = get_optional_type(ira->codegen, struct_field->type_entry);
memoize_field_init_val(ira->codegen, type_entry, struct_field);
set_optional_payload(inner_fields[3], struct_field->init_val);
ZigValue *name = create_const_str_lit(ira->codegen, struct_field->name)->data.x_ptr.data.ref.pointee;
init_const_slice(ira->codegen, inner_fields[0], name, 0, buf_len(struct_field->name), true);

View File

@ -237,9 +237,11 @@ fn testStruct() void {
const struct_info = @typeInfo(TestStruct);
expect(@as(TypeId, struct_info) == TypeId.Struct);
expect(struct_info.Struct.layout == TypeInfo.ContainerLayout.Packed);
expect(struct_info.Struct.fields.len == 3);
expect(struct_info.Struct.fields.len == 4);
expect(struct_info.Struct.fields[1].offset == null);
expect(struct_info.Struct.fields[2].field_type == *TestStruct);
expect(struct_info.Struct.fields[2].default_value == null);
expect(struct_info.Struct.fields[3].default_value.? == 4);
expect(struct_info.Struct.decls.len == 2);
expect(struct_info.Struct.decls[0].is_pub);
expect(!struct_info.Struct.decls[0].data.Fn.is_extern);
@ -254,6 +256,7 @@ const TestStruct = packed struct {
fieldA: usize,
fieldB: void,
fieldC: *Self,
fieldD: u32 = 4,
pub fn foo(self: *const Self) void {}
};