authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2020-01-16 13:55:39+02:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-01-16 13:22:30-05:00
logbac27731e30cbea76997fa3547791b3462ac8697
tree1e14fcb22dc0957dda325fbc058989609c48b5a0
parentdf03fcf5f07e0d5d16e5b837658265f6c468cbe4

add struct field default value to typeinfo


3 files changed, 12 insertions(+), 2 deletions(-)

lib/std/builtin.zig+1
......@@ -205,6 +205,7 @@ pub const TypeInfo = union(enum) {
205205 name: []const u8,
206206 offset: ?comptime_int,
207207 field_type: type,
208 default_value: var,
208209 };
209210
210211 /// This data structure is used by the Zig language code generation and
src/ir.cpp+7-1
......@@ -23338,7 +23338,7 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInstruction *source_instr
2333823338 struct_field_val->special = ConstValSpecialStatic;
2333923339 struct_field_val->type = type_info_struct_field_type;
2334023340
23341 ZigValue **inner_fields = alloc_const_vals_ptrs(3);
23341 ZigValue **inner_fields = alloc_const_vals_ptrs(4);
2334223342 inner_fields[1]->special = ConstValSpecialStatic;
2334323343 inner_fields[1]->type = get_optional_type(ira->codegen, ira->codegen->builtin_types.entry_num_lit_int);
2334423344
......@@ -23361,6 +23361,12 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInstruction *source_instr
2336123361 inner_fields[2]->type = ira->codegen->builtin_types.entry_type;
2336223362 inner_fields[2]->data.x_type = struct_field->type_entry;
2336323363
23364 // default_value: var
23365 inner_fields[3]->special = ConstValSpecialStatic;
23366 inner_fields[3]->type = get_optional_type(ira->codegen, struct_field->type_entry);
23367 memoize_field_init_val(ira->codegen, type_entry, struct_field);
23368 set_optional_payload(inner_fields[3], struct_field->init_val);
23369
2336423370 ZigValue *name = create_const_str_lit(ira->codegen, struct_field->name)->data.x_ptr.data.ref.pointee;
2336523371 init_const_slice(ira->codegen, inner_fields[0], name, 0, buf_len(struct_field->name), true);
2336623372
test/stage1/behavior/type_info.zig+4-1
......@@ -237,9 +237,11 @@ fn testStruct() void {
237237 const struct_info = @typeInfo(TestStruct);
238238 expect(@as(TypeId, struct_info) == TypeId.Struct);
239239 expect(struct_info.Struct.layout == TypeInfo.ContainerLayout.Packed);
240 expect(struct_info.Struct.fields.len == 3);
240 expect(struct_info.Struct.fields.len == 4);
241241 expect(struct_info.Struct.fields[1].offset == null);
242242 expect(struct_info.Struct.fields[2].field_type == *TestStruct);
243 expect(struct_info.Struct.fields[2].default_value == null);
244 expect(struct_info.Struct.fields[3].default_value.? == 4);
243245 expect(struct_info.Struct.decls.len == 2);
244246 expect(struct_info.Struct.decls[0].is_pub);
245247 expect(!struct_info.Struct.decls[0].data.Fn.is_extern);
......@@ -254,6 +256,7 @@ const TestStruct = packed struct {
254256 fieldA: usize,
255257 fieldB: void,
256258 fieldC: *Self,
259 fieldD: u32 = 4,
257260
258261 pub fn foo(self: *const Self) void {}
259262};