Commit bac27731e3

Vexu <git@vexu.eu>
2020-01-16 12:55:39
add struct field default value to typeinfo
1 parent df03fcf
Changed files (3)
lib
src
test
stage1
behavior
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
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);
 
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 {}
 };