authorgravatar for alex_naskos@hotmail.comAlexandros Naskos <alex_naskos@hotmail.com> 2018-04-27 02:05:24+03:00
committergravatar for alex_naskos@hotmail.comAlexandros Naskos <alex_naskos@hotmail.com> 2018-04-27 02:05:24+03:00
log9041d0d37e59e3f07d568f93bd680cc0065d1cd2
tree4b7c40c312a751f994dddd259c41e074c2e9c0b6
parent884e32d5c3aac470cdcd9b20e15554d6193f5501

Fixed enum tag type detection in TypeInfo generation.


2 files changed, 30 insertions(+), 34 deletions(-)

src/codegen.cpp+7-1
...@@ -6349,6 +6349,12 @@ static void define_builtin_compile_vars(CodeGen *g) {...@@ -6349,6 +6349,12 @@ static void define_builtin_compile_vars(CodeGen *g) {
6349 }6349 }
6350 {6350 {
6351 // @TODO Add Namespace info.6351 // @TODO Add Namespace info.
6352 // @TODO Methods -> definitions
6353 // @TODO Includes type definitions (name + type bound) + functions + const variable definitions (+ type of variable)
6354 // @TODO Type definitions are defined as variable definitions of type 'type'
6355 // @TODO This should give us everything available.
6356 // @TODO An alternative is exposing the value of every variable definition, check out if it's possible and wether we want that.
6357 // @TODO I don't think so, @field gives it to us for free.
6352 buf_appendf(contents,6358 buf_appendf(contents,
6353 "pub const TypeInfo = union(TypeId) {\n"6359 "pub const TypeInfo = union(TypeId) {\n"
6354 " Type: void,\n"6360 " Type: void,\n"
...@@ -6453,7 +6459,7 @@ static void define_builtin_compile_vars(CodeGen *g) {...@@ -6453,7 +6459,7 @@ static void define_builtin_compile_vars(CodeGen *g) {
6453 "\n"6459 "\n"
6454 " pub const UnionField = struct {\n"6460 " pub const UnionField = struct {\n"
6455 " name: []const u8,\n"6461 " name: []const u8,\n"
6456 " enum_field: EnumField,\n"6462 " enum_field: ?EnumField,\n"
6457 " field_type: type,\n"6463 " field_type: type,\n"
6458 " };\n"6464 " };\n"
6459 "\n"6465 "\n"
src/ir.cpp+23-33
...@@ -15787,8 +15787,8 @@ static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, TypeTableEntry *t...@@ -15787,8 +15787,8 @@ static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, TypeTableEntry *t
15787 assert(type_entry != nullptr);15787 assert(type_entry != nullptr);
15788 assert(!type_is_invalid(type_entry));15788 assert(!type_is_invalid(type_entry));
1578915789
15790 const auto make_enum_field_val = [ira](ConstExprValue *enum_field_val, TypeEnumField *enum_field) {15790 const auto make_enum_field_val = [ira](ConstExprValue *enum_field_val, TypeEnumField *enum_field,
15791 TypeTableEntry *type_info_enum_field_type = ir_type_info_get_type(ira, "EnumField");15791 TypeTableEntry *type_info_enum_field_type) {
15792 // @TODO Those cause a find_struct_type_field assertion to fail (type_entry->data.structure.complete)15792 // @TODO Those cause a find_struct_type_field assertion to fail (type_entry->data.structure.complete)
15793 // ensure_field_index(type_info_enum_field_type, "name", 0);15793 // ensure_field_index(type_info_enum_field_type, "name", 0);
15794 // ensure_field_index(type_info_enum_field_type, "value", 1);15794 // ensure_field_index(type_info_enum_field_type, "value", 1);
...@@ -15990,10 +15990,6 @@ static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, TypeTableEntry *t...@@ -15990,10 +15990,6 @@ static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, TypeTableEntry *t
15990 ensure_field_index(result->type, "fields", 2);15990 ensure_field_index(result->type, "fields", 2);
1599115991
15992 TypeTableEntry *type_info_enum_field_type = ir_type_info_get_type(ira, "EnumField");15992 TypeTableEntry *type_info_enum_field_type = ir_type_info_get_type(ira, "EnumField");
15993 // @TODO Those cause a find_struct_type_field assertion to fail (type_entry->data.structure.complete)
15994 // ensure_field_index(type_info_enum_field_type, "name", 0);
15995 // ensure_field_index(type_info_enum_field_type, "value", 1);
15996
15997 uint32_t enum_field_count = type_entry->data.enumeration.src_field_count;15993 uint32_t enum_field_count = type_entry->data.enumeration.src_field_count;
1599815994
15999 ConstExprValue *enum_field_array = create_const_vals(1);15995 ConstExprValue *enum_field_array = create_const_vals(1);
...@@ -16009,14 +16005,13 @@ static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, TypeTableEntry *t...@@ -16009,14 +16005,13 @@ static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, TypeTableEntry *t
16009 {16005 {
16010 TypeEnumField *enum_field = &type_entry->data.enumeration.fields[enum_field_index];16006 TypeEnumField *enum_field = &type_entry->data.enumeration.fields[enum_field_index];
16011 ConstExprValue *enum_field_val = &enum_field_array->data.x_array.s_none.elements[enum_field_index];16007 ConstExprValue *enum_field_val = &enum_field_array->data.x_array.s_none.elements[enum_field_index];
16012 make_enum_field_val(enum_field_val, enum_field);16008 make_enum_field_val(enum_field_val, enum_field, type_info_enum_field_type);
16013 enum_field_val->data.x_struct.parent.id = ConstParentIdArray;16009 enum_field_val->data.x_struct.parent.id = ConstParentIdArray;
16014 enum_field_val->data.x_struct.parent.data.p_array.array_val = enum_field_array;16010 enum_field_val->data.x_struct.parent.data.p_array.array_val = enum_field_array;
16015 enum_field_val->data.x_struct.parent.data.p_array.elem_index = enum_field_index;16011 enum_field_val->data.x_struct.parent.data.p_array.elem_index = enum_field_index;
16016 }16012 }
1601716013
16018 // @TODO16014 // @TODO Definitions
16019 // methods: []TypeInfo.Method
16020 break;16015 break;
16021 }16016 }
16022 case TypeTableEntryIdErrorSet:16017 case TypeTableEntryIdErrorSet:
...@@ -16032,10 +16027,6 @@ static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, TypeTableEntry *t...@@ -16032,10 +16027,6 @@ static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, TypeTableEntry *t
16032 ensure_field_index(result->type, "errors", 0);16027 ensure_field_index(result->type, "errors", 0);
1603316028
16034 TypeTableEntry *type_info_error_type = ir_type_info_get_type(ira, "Error");16029 TypeTableEntry *type_info_error_type = ir_type_info_get_type(ira, "Error");
16035 // @TODO Same as above in Enum TypeInfo generation.
16036 // ensure_field_index(type_info_error_type, "name", 0);
16037 // ensure_field_index(type_info_error_type, "value", 1);
16038
16039 uint32_t error_count = type_entry->data.error_set.err_count;16030 uint32_t error_count = type_entry->data.error_set.err_count;
16040 ConstExprValue *error_array = create_const_vals(1);16031 ConstExprValue *error_array = create_const_vals(1);
16041 error_array->special = ConstValSpecialStatic;16032 error_array->special = ConstValSpecialStatic;
...@@ -16115,21 +16106,18 @@ static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, TypeTableEntry *t...@@ -16115,21 +16106,18 @@ static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, TypeTableEntry *t
16115 fields[1].special = ConstValSpecialStatic;16106 fields[1].special = ConstValSpecialStatic;
16116 fields[1].type = ira->codegen->builtin_types.entry_type;16107 fields[1].type = ira->codegen->builtin_types.entry_type;
16117 // @TODO ?type instead of using @typeOf(undefined) when we have no type.16108 // @TODO ?type instead of using @typeOf(undefined) when we have no type.
16118 if (type_entry->data.unionation.tag_type == nullptr)16109 AstNode *union_decl_node = type_entry->data.unionation.decl_node;
16119 fields[1].data.x_type = ira->codegen->builtin_types.entry_undef;16110 if (union_decl_node->data.container_decl.auto_enum ||
16120 else16111 union_decl_node->data.container_decl.init_arg_expr != nullptr)
16112 {
16121 fields[1].data.x_type = type_entry->data.unionation.tag_type;16113 fields[1].data.x_type = type_entry->data.unionation.tag_type;
1612216114 }
16123 fields[1].data.x_type = type_entry->data.unionation.tag_type;16115 else
16116 fields[1].data.x_type = ira->codegen->builtin_types.entry_undef;
16124 // fields: []TypeInfo.UnionField16117 // fields: []TypeInfo.UnionField
16125 ensure_field_index(result->type, "fields", 2);16118 ensure_field_index(result->type, "fields", 2);
1612616119
16127 TypeTableEntry *type_info_union_field_type = ir_type_info_get_type(ira, "UnionField");16120 TypeTableEntry *type_info_union_field_type = ir_type_info_get_type(ira, "UnionField");
16128 // @TODO Those cause a find_struct_type_field assertion to fail (type_entry->data.structure.complete)
16129 // ensure_field_index(type_info_union_field_type, "name", 0);
16130 // ensure_field_index(type_info_union_field_type, "enum_field", 1);
16131 // ensure_field_index(type_info_union_field_type, "field_type", 2);
16132
16133 uint32_t union_field_count = type_entry->data.unionation.src_field_count;16121 uint32_t union_field_count = type_entry->data.unionation.src_field_count;
1613416122
16135 ConstExprValue *union_field_array = create_const_vals(1);16123 ConstExprValue *union_field_array = create_const_vals(1);
...@@ -16141,6 +16129,8 @@ static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, TypeTableEntry *t...@@ -16141,6 +16129,8 @@ static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, TypeTableEntry *t
1614116129
16142 init_const_slice(ira->codegen, &fields[2], union_field_array, 0, union_field_count, false);16130 init_const_slice(ira->codegen, &fields[2], union_field_array, 0, union_field_count, false);
1614316131
16132 TypeTableEntry *type_info_enum_field_type = ir_type_info_get_type(ira, "EnumField");
16133
16144 for (uint32_t union_field_index = 0; union_field_index < union_field_count; union_field_index++)16134 for (uint32_t union_field_index = 0; union_field_index < union_field_count; union_field_index++)
16145 {16135 {
16146 TypeUnionField *union_field = &type_entry->data.unionation.fields[union_field_index];16136 TypeUnionField *union_field = &type_entry->data.unionation.fields[union_field_index];
...@@ -16151,10 +16141,15 @@ static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, TypeTableEntry *t...@@ -16151,10 +16141,15 @@ static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, TypeTableEntry *t
1615116141
16152 ConstExprValue *inner_fields = create_const_vals(3);16142 ConstExprValue *inner_fields = create_const_vals(3);
16153 inner_fields[1].special = ConstValSpecialStatic;16143 inner_fields[1].special = ConstValSpecialStatic;
16154 make_enum_field_val(&inner_fields[1], union_field->enum_field);16144 inner_fields[1].type = get_maybe_type(ira->codegen, type_info_enum_field_type);
16155 inner_fields[1].data.x_struct.parent.id = ConstParentIdStruct;16145
16156 inner_fields[1].data.x_struct.parent.data.p_struct.struct_val = union_field_val;16146 if (fields[1].data.x_type == ira->codegen->builtin_types.entry_undef)
16157 inner_fields[1].data.x_struct.parent.data.p_struct.field_index = 1;16147 inner_fields[1].data.x_maybe = nullptr;
16148 else
16149 {
16150 inner_fields[1].data.x_maybe = create_const_vals(1);
16151 make_enum_field_val(inner_fields[1].data.x_maybe, union_field->enum_field, type_info_enum_field_type);
16152 }
1615816153
16159 inner_fields[2].special = ConstValSpecialStatic;16154 inner_fields[2].special = ConstValSpecialStatic;
16160 inner_fields[2].type = ira->codegen->builtin_types.entry_type;16155 inner_fields[2].type = ira->codegen->builtin_types.entry_type;
...@@ -16163,18 +16158,13 @@ static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, TypeTableEntry *t...@@ -16163,18 +16158,13 @@ static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, TypeTableEntry *t
16163 ConstExprValue *name = create_const_str_lit(ira->codegen, union_field->name);16158 ConstExprValue *name = create_const_str_lit(ira->codegen, union_field->name);
16164 init_const_slice(ira->codegen, &inner_fields[0], name, 0, buf_len(union_field->name), true);16159 init_const_slice(ira->codegen, &inner_fields[0], name, 0, buf_len(union_field->name), true);
1616516160
16166
16167 union_field_val->data.x_struct.fields = inner_fields;16161 union_field_val->data.x_struct.fields = inner_fields;
16168 union_field_val->data.x_struct.parent.id = ConstParentIdArray;16162 union_field_val->data.x_struct.parent.id = ConstParentIdArray;
16169 union_field_val->data.x_struct.parent.data.p_array.array_val = union_field_array;16163 union_field_val->data.x_struct.parent.data.p_array.array_val = union_field_array;
16170 union_field_val->data.x_struct.parent.data.p_array.elem_index = union_field_index;16164 union_field_val->data.x_struct.parent.data.p_array.elem_index = union_field_index;
16171
16172 // @TODO Check if TypeUnionField::enum_field == nullptr when tag_type == nullptr
16173 // If it is, make enum_field: ?EnumField, set it when available, done.
16174 }16165 }
1617516166
16176 // @TODO16167 // @TODO Definitions
16177 // methods: []TypeInfo.Method
16178 break;16168 break;
16179 }16169 }
16180 }16170 }