authorgravatar for alex_naskos@hotmail.comAlexandros Naskos <alex_naskos@hotmail.com> 2018-04-26 19:56:34+03:00
committergravatar for alex_naskos@hotmail.comAlexandros Naskos <alex_naskos@hotmail.com> 2018-04-26 19:56:34+03:00
log884e32d5c3aac470cdcd9b20e15554d6193f5501
treea57777ef3adc3b1480ede9e5a470f55bf5c3fb80
parentfbbbee6b72991b65c31a8eac8f1edfc3d1a88b59

Added ErrorUnion, Union TypeInfo generation


2 files changed, 127 insertions(+), 15 deletions(-)

src/codegen.cpp+1-1
...@@ -6426,7 +6426,7 @@ static void define_builtin_compile_vars(CodeGen *g) {...@@ -6426,7 +6426,7 @@ static void define_builtin_compile_vars(CodeGen *g) {
6426 " };\n"6426 " };\n"
6427 "\n"6427 "\n"
6428 " pub const ErrorUnion = struct {\n"6428 " pub const ErrorUnion = struct {\n"
6429 " error_set: &ErrorSet,\n"6429 " error_set: type,\n"
6430 " payload: type,\n"6430 " payload: type,\n"
6431 " };\n"6431 " };\n"
6432 "\n"6432 "\n"
src/ir.cpp+126-14
...@@ -15787,6 +15787,27 @@ static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, TypeTableEntry *t...@@ -15787,6 +15787,27 @@ 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) {
15791 TypeTableEntry *type_info_enum_field_type = ir_type_info_get_type(ira, "EnumField");
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);
15794 // ensure_field_index(type_info_enum_field_type, "value", 1);
15795
15796 enum_field_val->special = ConstValSpecialStatic;
15797 enum_field_val->type = type_info_enum_field_type;
15798
15799 ConstExprValue *inner_fields = create_const_vals(2);
15800 inner_fields[1].special = ConstValSpecialStatic;
15801 inner_fields[1].type = ira->codegen->builtin_types.entry_usize;
15802
15803 ConstExprValue *name = create_const_str_lit(ira->codegen, enum_field->name);
15804 init_const_slice(ira->codegen, &inner_fields[0], name, 0, buf_len(enum_field->name), true);
15805
15806 bigint_init_bigint(&inner_fields[1].data.x_bigint, &enum_field->value);
15807
15808 enum_field_val->data.x_struct.fields = inner_fields;
15809 };
15810
15790 ConstExprValue *result = nullptr;15811 ConstExprValue *result = nullptr;
15791 switch (type_entry->id)15812 switch (type_entry->id)
15792 {15813 {
...@@ -15988,20 +16009,7 @@ static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, TypeTableEntry *t...@@ -15988,20 +16009,7 @@ static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, TypeTableEntry *t
15988 {16009 {
15989 TypeEnumField *enum_field = &type_entry->data.enumeration.fields[enum_field_index];16010 TypeEnumField *enum_field = &type_entry->data.enumeration.fields[enum_field_index];
15990 ConstExprValue *enum_field_val = &enum_field_array->data.x_array.s_none.elements[enum_field_index];16011 ConstExprValue *enum_field_val = &enum_field_array->data.x_array.s_none.elements[enum_field_index];
1599116012 make_enum_field_val(enum_field_val, enum_field);
15992 enum_field_val->special = ConstValSpecialStatic;
15993 enum_field_val->type = type_info_enum_field_type;
15994
15995 ConstExprValue *inner_fields = create_const_vals(2);
15996 inner_fields[1].special = ConstValSpecialStatic;
15997 inner_fields[1].type = ira->codegen->builtin_types.entry_usize;
15998
15999 ConstExprValue *name = create_const_str_lit(ira->codegen, enum_field->name);
16000 init_const_slice(ira->codegen, &inner_fields[0], name, 0, buf_len(enum_field->name), true);
16001
16002 bigint_init_bigint(&inner_fields[1].data.x_bigint, &enum_field->value);
16003
16004 enum_field_val->data.x_struct.fields = inner_fields;
16005 enum_field_val->data.x_struct.parent.id = ConstParentIdArray;16013 enum_field_val->data.x_struct.parent.id = ConstParentIdArray;
16006 enum_field_val->data.x_struct.parent.data.p_array.array_val = enum_field_array;16014 enum_field_val->data.x_struct.parent.data.p_array.array_val = enum_field_array;
16007 enum_field_val->data.x_struct.parent.data.p_array.elem_index = enum_field_index;16015 enum_field_val->data.x_struct.parent.data.p_array.elem_index = enum_field_index;
...@@ -16063,6 +16071,110 @@ static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, TypeTableEntry *t...@@ -16063,6 +16071,110 @@ static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, TypeTableEntry *t
16063 error_val->data.x_struct.parent.data.p_array.elem_index = error_index;16071 error_val->data.x_struct.parent.data.p_array.elem_index = error_index;
16064 }16072 }
1606516073
16074 break;
16075 }
16076 case TypeTableEntryIdErrorUnion:
16077 {
16078 result = create_const_vals(1);
16079 result->special = ConstValSpecialStatic;
16080 result->type = ir_type_info_get_type(ira, "ErrorUnion");
16081
16082 ConstExprValue *fields = create_const_vals(2);
16083 result->data.x_struct.fields = fields;
16084
16085 // error_set: type
16086 ensure_field_index(result->type, "error_set", 0);
16087 fields[0].special = ConstValSpecialStatic;
16088 fields[0].type = ira->codegen->builtin_types.entry_type;
16089 fields[0].data.x_type = type_entry->data.error_union.err_set_type;
16090
16091 // payload: type
16092 ensure_field_index(result->type, "payload", 1);
16093 fields[1].special = ConstValSpecialStatic;
16094 fields[1].type = ira->codegen->builtin_types.entry_type;
16095 fields[1].data.x_type = type_entry->data.error_union.payload_type;
16096
16097 break;
16098 }
16099 case TypeTableEntryIdUnion:
16100 {
16101 result = create_const_vals(1);
16102 result->special = ConstValSpecialStatic;
16103 result->type = ir_type_info_get_type(ira, "Union");
16104
16105 ConstExprValue *fields = create_const_vals(4);
16106 result->data.x_struct.fields = fields;
16107
16108 // layout: ContainerLayout
16109 ensure_field_index(result->type, "layout", 0);
16110 fields[0].special = ConstValSpecialStatic;
16111 fields[0].type = ir_type_info_get_type(ira, "ContainerLayout");
16112 bigint_init_unsigned(&fields[0].data.x_enum_tag, type_entry->data.unionation.layout);
16113 // tag_type: type
16114 ensure_field_index(result->type, "tag_type", 1);
16115 fields[1].special = ConstValSpecialStatic;
16116 fields[1].type = ira->codegen->builtin_types.entry_type;
16117 // @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
16121 fields[1].data.x_type = type_entry->data.unionation.tag_type;
16122
16123 fields[1].data.x_type = type_entry->data.unionation.tag_type;
16124 // fields: []TypeInfo.UnionField
16125 ensure_field_index(result->type, "fields", 2);
16126
16127 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;
16134
16135 ConstExprValue *union_field_array = create_const_vals(1);
16136 union_field_array->special = ConstValSpecialStatic;
16137 union_field_array->type = get_array_type(ira->codegen, type_info_union_field_type, union_field_count);
16138 union_field_array->data.x_array.special = ConstArraySpecialNone;
16139 union_field_array->data.x_array.s_none.parent.id = ConstParentIdNone;
16140 union_field_array->data.x_array.s_none.elements = create_const_vals(union_field_count);
16141
16142 init_const_slice(ira->codegen, &fields[2], union_field_array, 0, union_field_count, false);
16143
16144 for (uint32_t union_field_index = 0; union_field_index < union_field_count; union_field_index++)
16145 {
16146 TypeUnionField *union_field = &type_entry->data.unionation.fields[union_field_index];
16147 ConstExprValue *union_field_val = &union_field_array->data.x_array.s_none.elements[union_field_index];
16148
16149 union_field_val->special = ConstValSpecialStatic;
16150 union_field_val->type = type_info_union_field_type;
16151
16152 ConstExprValue *inner_fields = create_const_vals(3);
16153 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;
16158
16159 inner_fields[2].special = ConstValSpecialStatic;
16160 inner_fields[2].type = ira->codegen->builtin_types.entry_type;
16161 inner_fields[2].data.x_type = union_field->type_entry;
16162
16163 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);
16165
16166
16167 union_field_val->data.x_struct.fields = inner_fields;
16168 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;
16170 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 }
16175
16176 // @TODO
16177 // methods: []TypeInfo.Method
16066 break;16178 break;
16067 }16179 }
16068 }16180 }