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) {
63496349 }
63506350 {
63516351 // @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.
63526358 buf_appendf(contents,
63536359 "pub const TypeInfo = union(TypeId) {\n"
63546360 " Type: void,\n"
......@@ -6453,7 +6459,7 @@ static void define_builtin_compile_vars(CodeGen *g) {
64536459 "\n"
64546460 " pub const UnionField = struct {\n"
64556461 " name: []const u8,\n"
6456 " enum_field: EnumField,\n"
6462 " enum_field: ?EnumField,\n"
64576463 " field_type: type,\n"
64586464 " };\n"
64596465 "\n"
src/ir.cpp+23-33
......@@ -15787,8 +15787,8 @@ static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, TypeTableEntry *t
1578715787 assert(type_entry != nullptr);
1578815788 assert(!type_is_invalid(type_entry));
1578915789
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");
15790 const auto make_enum_field_val = [ira](ConstExprValue *enum_field_val, TypeEnumField *enum_field,
15791 TypeTableEntry *type_info_enum_field_type) {
1579215792 // @TODO Those cause a find_struct_type_field assertion to fail (type_entry->data.structure.complete)
1579315793 // ensure_field_index(type_info_enum_field_type, "name", 0);
1579415794 // 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
1599015990 ensure_field_index(result->type, "fields", 2);
1599115991
1599215992 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
1599715993 uint32_t enum_field_count = type_entry->data.enumeration.src_field_count;
1599815994
1599915995 ConstExprValue *enum_field_array = create_const_vals(1);
......@@ -16009,14 +16005,13 @@ static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, TypeTableEntry *t
1600916005 {
1601016006 TypeEnumField *enum_field = &type_entry->data.enumeration.fields[enum_field_index];
1601116007 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);
1601316009 enum_field_val->data.x_struct.parent.id = ConstParentIdArray;
1601416010 enum_field_val->data.x_struct.parent.data.p_array.array_val = enum_field_array;
1601516011 enum_field_val->data.x_struct.parent.data.p_array.elem_index = enum_field_index;
1601616012 }
1601716013
16018 // @TODO
16019 // methods: []TypeInfo.Method
16014 // @TODO Definitions
1602016015 break;
1602116016 }
1602216017 case TypeTableEntryIdErrorSet:
......@@ -16032,10 +16027,6 @@ static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, TypeTableEntry *t
1603216027 ensure_field_index(result->type, "errors", 0);
1603316028
1603416029 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
1603916030 uint32_t error_count = type_entry->data.error_set.err_count;
1604016031 ConstExprValue *error_array = create_const_vals(1);
1604116032 error_array->special = ConstValSpecialStatic;
......@@ -16115,21 +16106,18 @@ static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, TypeTableEntry *t
1611516106 fields[1].special = ConstValSpecialStatic;
1611616107 fields[1].type = ira->codegen->builtin_types.entry_type;
1611716108 // @TODO ?type instead of using @typeOf(undefined) when we have no type.
16118 if (type_entry->data.unionation.tag_type == nullptr)
16119 fields[1].data.x_type = ira->codegen->builtin_types.entry_undef;
16120 else
16109 AstNode *union_decl_node = type_entry->data.unionation.decl_node;
16110 if (union_decl_node->data.container_decl.auto_enum ||
16111 union_decl_node->data.container_decl.init_arg_expr != nullptr)
16112 {
1612116113 fields[1].data.x_type = type_entry->data.unionation.tag_type;
16122
16123 fields[1].data.x_type = type_entry->data.unionation.tag_type;
16114 }
16115 else
16116 fields[1].data.x_type = ira->codegen->builtin_types.entry_undef;
1612416117 // fields: []TypeInfo.UnionField
1612516118 ensure_field_index(result->type, "fields", 2);
1612616119
1612716120 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
1613316121 uint32_t union_field_count = type_entry->data.unionation.src_field_count;
1613416122
1613516123 ConstExprValue *union_field_array = create_const_vals(1);
......@@ -16141,6 +16129,8 @@ static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, TypeTableEntry *t
1614116129
1614216130 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
1614416134 for (uint32_t union_field_index = 0; union_field_index < union_field_count; union_field_index++)
1614516135 {
1614616136 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
1615116141
1615216142 ConstExprValue *inner_fields = create_const_vals(3);
1615316143 inner_fields[1].special = ConstValSpecialStatic;
16154 make_enum_field_val(&inner_fields[1], union_field->enum_field);
16155 inner_fields[1].data.x_struct.parent.id = ConstParentIdStruct;
16156 inner_fields[1].data.x_struct.parent.data.p_struct.struct_val = union_field_val;
16157 inner_fields[1].data.x_struct.parent.data.p_struct.field_index = 1;
16144 inner_fields[1].type = get_maybe_type(ira->codegen, type_info_enum_field_type);
16145
16146 if (fields[1].data.x_type == ira->codegen->builtin_types.entry_undef)
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
1615916154 inner_fields[2].special = ConstValSpecialStatic;
1616016155 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
1616316158 ConstExprValue *name = create_const_str_lit(ira->codegen, union_field->name);
1616416159 init_const_slice(ira->codegen, &inner_fields[0], name, 0, buf_len(union_field->name), true);
1616516160
16166
1616716161 union_field_val->data.x_struct.fields = inner_fields;
1616816162 union_field_val->data.x_struct.parent.id = ConstParentIdArray;
1616916163 union_field_val->data.x_struct.parent.data.p_array.array_val = union_field_array;
1617016164 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.
1617416165 }
1617516166
16176 // @TODO
16177 // methods: []TypeInfo.Method
16167 // @TODO Definitions
1617816168 break;
1617916169 }
1618016170 }