authorgravatar for alex_naskos@hotmail.comAlexandros Naskos <alex_naskos@hotmail.com> 2018-04-26 16:41:59+03:00
committergravatar for alex_naskos@hotmail.comAlexandros Naskos <alex_naskos@hotmail.com> 2018-04-26 16:41:59+03:00
logf5977f68ebe344ec9c96507322218b87c24804a1
treeeadcc1a910b40666126cb84cd43aae48e5973fad
parent7a91e4736a4151bd41b65b6e3b395cc51c443162

Added Enum TypeInfo except for methods


2 files changed, 111 insertions(+), 22 deletions(-)

src/codegen.cpp+4-4
......@@ -6412,7 +6412,7 @@ static void define_builtin_compile_vars(CodeGen *g) {
64126412 " pub const StructField = struct {\n"
64136413 " name: []const u8,\n"
64146414 " offset: usize,\n"
6415 " type_info: TypeInfo,\n"
6415 " type_info: &TypeInfo,\n"
64166416 " };\n"
64176417 "\n"
64186418 " pub const Struct = struct {\n"
......@@ -6446,7 +6446,7 @@ static void define_builtin_compile_vars(CodeGen *g) {
64466446 "\n"
64476447 " pub const Enum = struct {\n"
64486448 " layout: ContainerLayout,\n"
6449 " tag_type: Int,\n"
6449 " tag_type: &Int,\n"
64506450 " fields: []EnumField,\n"
64516451 " methods: []Method,\n"
64526452 " };\n"
......@@ -6454,7 +6454,7 @@ static void define_builtin_compile_vars(CodeGen *g) {
64546454 " pub const UnionField = struct {\n"
64556455 " name: []const u8,\n"
64566456 " enum_field: EnumField,\n"
6457 " type_info: TypeInfo,\n"
6457 " type_info: &TypeInfo,\n"
64586458 " };\n"
64596459 "\n"
64606460 " pub const Union = struct {\n"
......@@ -6476,7 +6476,7 @@ static void define_builtin_compile_vars(CodeGen *g) {
64766476 " pub const FnArg = struct {\n"
64776477 " is_comptime: bool,\n"
64786478 " name: []const u8,\n"
6479 " type_info: TypeInfo,\n"
6479 " type_info: &TypeInfo,\n"
64806480 " };\n"
64816481 "\n"
64826482 " pub const Fn = struct {\n"
src/ir.cpp+107-18
......@@ -15864,7 +15864,11 @@ static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, ConstExprValue *p
1586415864 return result;
1586515865 }
1586615866
15867 ConstExprValue *result = nullptr;
15867 // If the value is not present in the cache, we will build it, add it and return it.
15868 // We add values to the cache eagerly, as soon as we have filled out the root object's fields.
15869 // That way, if we need to fetch the value in a recursive call down the line, even if we need to
15870 // copy the value and reajust the parent, the value we get back still points to child values that
15871 // will be filled later.
1586815872
1586915873 switch (type_entry->id)
1587015874 {
......@@ -15885,7 +15889,7 @@ static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, ConstExprValue *p
1588515889 return nullptr;
1588615890 case TypeTableEntryIdInt:
1588715891 {
15888 result = create_const_vals(1);
15892 ConstExprValue *result = create_const_vals(1);
1588915893 result->special = ConstValSpecialStatic;
1589015894 result->type = ir_type_info_get_type(ira, "Int");
1589115895
......@@ -15893,6 +15897,7 @@ static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, ConstExprValue *p
1589315897 result->data.x_struct.fields = fields;
1589415898
1589515899 ir_type_info_struct_set_parent(result, parent, parent_field_index);
15900 ira->codegen->type_info_cache.put(type_entry, result);
1589615901
1589715902 // is_signed: bool
1589815903 ensure_field_index(result->type, "is_signed", 0);
......@@ -15904,11 +15909,12 @@ static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, ConstExprValue *p
1590415909 fields[1].special = ConstValSpecialStatic;
1590515910 fields[1].type = ira->codegen->builtin_types.entry_u8;
1590615911 bigint_init_unsigned(&fields[1].data.x_bigint, type_entry->data.integral.bit_count);
15907 break;
15912
15913 return result;
1590815914 }
1590915915 case TypeTableEntryIdFloat:
1591015916 {
15911 result = create_const_vals(1);
15917 ConstExprValue *result = create_const_vals(1);
1591215918 result->special = ConstValSpecialStatic;
1591315919 result->type = ir_type_info_get_type(ira, "Float");
1591415920
......@@ -15916,17 +15922,19 @@ static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, ConstExprValue *p
1591615922 result->data.x_struct.fields = fields;
1591715923
1591815924 ir_type_info_struct_set_parent(result, parent, parent_field_index);
15925 ira->codegen->type_info_cache.put(type_entry, result);
1591915926
1592015927 // bits: u8
1592115928 ensure_field_index(result->type, "bits", 0);
1592215929 fields[0].special = ConstValSpecialStatic;
1592315930 fields[0].type = ira->codegen->builtin_types.entry_u8;
1592415931 bigint_init_unsigned(&fields->data.x_bigint, type_entry->data.floating.bit_count);
15925 break;
15932
15933 return result;
1592615934 }
1592715935 case TypeTableEntryIdPointer:
1592815936 {
15929 result = create_const_vals(1);
15937 ConstExprValue *result = create_const_vals(1);
1593015938 result->special = ConstValSpecialStatic;
1593115939 result->type = ir_type_info_get_type(ira, "Pointer");
1593215940
......@@ -15934,6 +15942,7 @@ static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, ConstExprValue *p
1593415942 result->data.x_struct.fields = fields;
1593515943
1593615944 ir_type_info_struct_set_parent(result, parent, parent_field_index);
15945 ira->codegen->type_info_cache.put(type_entry, result);
1593715946
1593815947 // is_const: bool
1593915948 ensure_field_index(result->type, "is_const", 0);
......@@ -15968,11 +15977,12 @@ static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, ConstExprValue *p
1596815977 type_entry->data.pointer.child_type);
1596915978
1597015979 fields[3].data.x_ptr.data.ref.pointee = union_val;
15971 break;
15980
15981 return result;
1597215982 }
1597315983 case TypeTableEntryIdArray:
1597415984 {
15975 result = create_const_vals(1);
15985 ConstExprValue *result = create_const_vals(1);
1597615986 result->special = ConstValSpecialStatic;
1597715987 result->type = ir_type_info_get_type(ira, "Array");
1597815988
......@@ -15980,6 +15990,7 @@ static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, ConstExprValue *p
1598015990 result->data.x_struct.fields = fields;
1598115991
1598215992 ir_type_info_struct_set_parent(result, parent, parent_field_index);
15993 ira->codegen->type_info_cache.put(type_entry, result);
1598315994
1598415995 // len: usize
1598515996 ensure_field_index(result->type, "len", 0);
......@@ -15988,7 +15999,6 @@ static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, ConstExprValue *p
1598815999 bigint_init_unsigned(&fields[0].data.x_bigint, type_entry->data.array.len);
1598916000 // child: &TypeInfo
1599016001 ensure_field_index(result->type, "child", 1);
15991
1599216002 TypeTableEntry *type_info_type = ir_type_info_get_type(ira, nullptr);
1599316003
1599416004 fields[1].special = ConstValSpecialStatic;
......@@ -16004,11 +16014,12 @@ static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, ConstExprValue *p
1600416014 type_entry->data.array.child_type);
1600516015
1600616016 fields[1].data.x_ptr.data.ref.pointee = union_val;
16007 break;
16017
16018 return result;
1600816019 }
1600916020 case TypeTableEntryIdMaybe:
1601016021 {
16011 result = create_const_vals(1);
16022 ConstExprValue *result = create_const_vals(1);
1601216023 result->special = ConstValSpecialStatic;
1601316024 result->type = ir_type_info_get_type(ira, "Nullable");
1601416025
......@@ -16016,6 +16027,7 @@ static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, ConstExprValue *p
1601616027 result->data.x_struct.fields = fields;
1601716028
1601816029 ir_type_info_struct_set_parent(result, parent, parent_field_index);
16030 ira->codegen->type_info_cache.put(type_entry, result);
1601916031
1602016032 // child: &TypeInfo
1602116033 ensure_field_index(result->type, "child", 0);
......@@ -16035,11 +16047,12 @@ static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, ConstExprValue *p
1603516047 type_entry->data.maybe.child_type);
1603616048
1603716049 fields[0].data.x_ptr.data.ref.pointee = union_val;
16038 break;
16050
16051 return result;
1603916052 }
1604016053 case TypeTableEntryIdPromise:
1604116054 {
16042 result = create_const_vals(1);
16055 ConstExprValue *result = create_const_vals(1);
1604316056 result->special = ConstValSpecialStatic;
1604416057 result->type = ir_type_info_get_type(ira, "Promise");
1604516058
......@@ -16047,6 +16060,7 @@ static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, ConstExprValue *p
1604716060 result->data.x_struct.fields = fields;
1604816061
1604916062 ir_type_info_struct_set_parent(result, parent, parent_field_index);
16063 ira->codegen->type_info_cache.put(type_entry, result);
1605016064
1605116065 // child: ?&TypeInfo
1605216066 ensure_field_index(result->type, "child", 0);
......@@ -16080,16 +16094,91 @@ static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, ConstExprValue *p
1608016094 maybe_value->data.x_ptr.data.ref.pointee = union_val;
1608116095 fields[0].data.x_maybe = maybe_value;
1608216096 }
16083 break;
16097
16098 return result;
16099 }
16100 case TypeTableEntryIdEnum:
16101 {
16102 ConstExprValue *result = create_const_vals(1);
16103 result->special = ConstValSpecialStatic;
16104 result->type = ir_type_info_get_type(ira, "Enum");
16105
16106 ConstExprValue *fields = create_const_vals(4);
16107 result->data.x_struct.fields = fields;
16108
16109 ir_type_info_struct_set_parent(result, parent, parent_field_index);
16110 ira->codegen->type_info_cache.put(type_entry, result);
16111
16112 // layout: ContainerLayout
16113 ensure_field_index(result->type, "layout", 0);
16114 fields[0].special = ConstValSpecialStatic;
16115 fields[0].type = ir_type_info_get_type(ira, "ContainerLayout");
16116 bigint_init_unsigned(&fields[0].data.x_enum_tag, type_entry->data.enumeration.layout);
16117 // tag_type: &TypeInfo.Int
16118 ensure_field_index(result->type, "tag_type", 1);
16119
16120 TypeTableEntry *type_info_int_type = ir_type_info_get_type(ira, "Int");
16121
16122 fields[1].special = ConstValSpecialStatic;
16123 fields[1].type = get_pointer_to_type(ira->codegen, type_info_int_type, false);
16124 fields[1].data.x_ptr.special = ConstPtrSpecialRef;
16125 fields[1].data.x_ptr.mut = ConstPtrMutComptimeVar;
16126
16127 ConstExprValue *tag_type_info_struct = ir_make_type_info_value(ira, &fields[1], -1,
16128 type_entry->data.enumeration.tag_int_type);
16129 assert(tag_type_info_struct->type == type_info_int_type);
16130 fields[1].data.x_ptr.data.ref.pointee = tag_type_info_struct;
16131 // fields: []TypeInfo.EnumField
16132 ensure_field_index(result->type, "fields", 2);
16133
16134 TypeTableEntry *type_info_enum_field_type = ir_type_info_get_type(ira, "EnumField");
16135 // @TODO Those cause a find_struct_type_field assertion to fail (type_entry->data.structure.complete)
16136 // ensure_field_index(type_info_enum_field_type, "name", 0);
16137 // ensure_field_index(type_info_enum_field_type, "value", 1);
16138
16139 uint32_t enum_field_count = type_entry->data.enumeration.src_field_count;
16140
16141 ConstExprValue *enum_field_array = create_const_vals(1);
16142 enum_field_array->special = ConstValSpecialStatic;
16143 enum_field_array->type = get_array_type(ira->codegen, type_info_enum_field_type, enum_field_count);
16144 enum_field_array->data.x_array.special = ConstArraySpecialNone;
16145 enum_field_array->data.x_array.s_none.parent.id = ConstParentIdNone;
16146 enum_field_array->data.x_array.s_none.elements = create_const_vals(enum_field_count);
16147
16148 init_const_slice(ira->codegen, &fields[2], enum_field_array, 0, enum_field_count, false);
16149
16150 for (uint32_t enum_field_index = 0; enum_field_index < enum_field_count; enum_field_index++)
16151 {
16152 TypeEnumField *enum_field = &type_entry->data.enumeration.fields[enum_field_index];
16153 ConstExprValue *enum_field_val = &enum_field_array->data.x_array.s_none.elements[enum_field_index];
16154
16155 enum_field_val->special = ConstValSpecialStatic;
16156 enum_field_val->type = type_info_enum_field_type;
16157
16158 ConstExprValue *inner_fields = create_const_vals(2);
16159 inner_fields[1].special = ConstValSpecialStatic;
16160 inner_fields[1].type = ira->codegen->builtin_types.entry_usize;
16161
16162 ConstExprValue *name = create_const_str_lit(ira->codegen, enum_field->name);
16163 init_const_slice(ira->codegen, &inner_fields[0], name, 0, buf_len(enum_field->name), true);
16164
16165 bigint_init_bigint(&inner_fields[1].data.x_bigint, &enum_field->value);
16166
16167 enum_field_val->data.x_struct.fields = inner_fields;
16168 enum_field_val->data.x_struct.parent.id = ConstParentIdArray;
16169 enum_field_val->data.x_struct.parent.data.p_array.array_val = enum_field_array;
16170 enum_field_val->data.x_struct.parent.data.p_array.elem_index = enum_field_index;
16171 }
16172
16173 // @TODO
16174 // methods: []TypeInfo.Method
16175 return result;
1608416176 }
1608516177 default:
1608616178 zig_unreachable();
1608716179 }
1608816180
16089 // Cache the returned value.
16090 assert(result != nullptr);
16091 ira->codegen->type_info_cache.put(type_entry, result);
16092 return result;
16181 zig_unreachable();
1609316182}
1609416183
1609516184static TypeTableEntry *ir_analyze_instruction_type_info(IrAnalyze *ira,