From bac27731e30cbea76997fa3547791b3462ac8697 Mon Sep 17 00:00:00 2001 From: Vexu Date: Thu, 16 Jan 2020 13:55:39 +0200 Subject: [PATCH] add struct field default value to typeinfo --- lib/std/builtin.zig | 1 + src/ir.cpp | 8 +++++++- test/stage1/behavior/type_info.zig | 5 ++++- 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/lib/std/builtin.zig b/lib/std/builtin.zig index dbd19fbadf..65604a5b20 100644 --- a/lib/std/builtin.zig +++ b/lib/std/builtin.zig @@ -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 diff --git a/src/ir.cpp b/src/ir.cpp index c3ca366456..818644f501 100644 --- a/src/ir.cpp +++ b/src/ir.cpp @@ -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); diff --git a/test/stage1/behavior/type_info.zig b/test/stage1/behavior/type_info.zig index d35cc8831f..9d577ec0b4 100644 --- a/test/stage1/behavior/type_info.zig +++ b/test/stage1/behavior/type_info.zig @@ -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 {} };