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) {...@@ -205,6 +205,7 @@ pub const TypeInfo = union(enum) {
205 name: []const u8,205 name: []const u8,
206 offset: ?comptime_int,206 offset: ?comptime_int,
207 field_type: type,207 field_type: type,
208 default_value: var,
208 };209 };
209210
210 /// This data structure is used by the Zig language code generation and211 /// 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...@@ -23338,7 +23338,7 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInstruction *source_instr
23338 struct_field_val->special = ConstValSpecialStatic;23338 struct_field_val->special = ConstValSpecialStatic;
23339 struct_field_val->type = type_info_struct_field_type;23339 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);
23342 inner_fields[1]->special = ConstValSpecialStatic;23342 inner_fields[1]->special = ConstValSpecialStatic;
23343 inner_fields[1]->type = get_optional_type(ira->codegen, ira->codegen->builtin_types.entry_num_lit_int);23343 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...@@ -23361,6 +23361,12 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInstruction *source_instr
23361 inner_fields[2]->type = ira->codegen->builtin_types.entry_type;23361 inner_fields[2]->type = ira->codegen->builtin_types.entry_type;
23362 inner_fields[2]->data.x_type = struct_field->type_entry;23362 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
23364 ZigValue *name = create_const_str_lit(ira->codegen, struct_field->name)->data.x_ptr.data.ref.pointee;23370 ZigValue *name = create_const_str_lit(ira->codegen, struct_field->name)->data.x_ptr.data.ref.pointee;
23365 init_const_slice(ira->codegen, inner_fields[0], name, 0, buf_len(struct_field->name), true);23371 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 {...@@ -237,9 +237,11 @@ fn testStruct() void {
237 const struct_info = @typeInfo(TestStruct);237 const struct_info = @typeInfo(TestStruct);
238 expect(@as(TypeId, struct_info) == TypeId.Struct);238 expect(@as(TypeId, struct_info) == TypeId.Struct);
239 expect(struct_info.Struct.layout == TypeInfo.ContainerLayout.Packed);239 expect(struct_info.Struct.layout == TypeInfo.ContainerLayout.Packed);
240 expect(struct_info.Struct.fields.len == 3);240 expect(struct_info.Struct.fields.len == 4);
241 expect(struct_info.Struct.fields[1].offset == null);241 expect(struct_info.Struct.fields[1].offset == null);
242 expect(struct_info.Struct.fields[2].field_type == *TestStruct);242 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);
243 expect(struct_info.Struct.decls.len == 2);245 expect(struct_info.Struct.decls.len == 2);
244 expect(struct_info.Struct.decls[0].is_pub);246 expect(struct_info.Struct.decls[0].is_pub);
245 expect(!struct_info.Struct.decls[0].data.Fn.is_extern);247 expect(!struct_info.Struct.decls[0].data.Fn.is_extern);
...@@ -254,6 +256,7 @@ const TestStruct = packed struct {...@@ -254,6 +256,7 @@ const TestStruct = packed struct {
254 fieldA: usize,256 fieldA: usize,
255 fieldB: void,257 fieldB: void,
256 fieldC: *Self,258 fieldC: *Self,
259 fieldD: u32 = 4,
257260
258 pub fn foo(self: *const Self) void {}261 pub fn foo(self: *const Self) void {}
259};262};