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) {...@@ -6412,7 +6412,7 @@ static void define_builtin_compile_vars(CodeGen *g) {
6412 " pub const StructField = struct {\n"6412 " pub const StructField = struct {\n"
6413 " name: []const u8,\n"6413 " name: []const u8,\n"
6414 " offset: usize,\n"6414 " offset: usize,\n"
6415 " type_info: TypeInfo,\n"6415 " type_info: &TypeInfo,\n"
6416 " };\n"6416 " };\n"
6417 "\n"6417 "\n"
6418 " pub const Struct = struct {\n"6418 " pub const Struct = struct {\n"
...@@ -6446,7 +6446,7 @@ static void define_builtin_compile_vars(CodeGen *g) {...@@ -6446,7 +6446,7 @@ static void define_builtin_compile_vars(CodeGen *g) {
6446 "\n"6446 "\n"
6447 " pub const Enum = struct {\n"6447 " pub const Enum = struct {\n"
6448 " layout: ContainerLayout,\n"6448 " layout: ContainerLayout,\n"
6449 " tag_type: Int,\n"6449 " tag_type: &Int,\n"
6450 " fields: []EnumField,\n"6450 " fields: []EnumField,\n"
6451 " methods: []Method,\n"6451 " methods: []Method,\n"
6452 " };\n"6452 " };\n"
...@@ -6454,7 +6454,7 @@ static void define_builtin_compile_vars(CodeGen *g) {...@@ -6454,7 +6454,7 @@ static void define_builtin_compile_vars(CodeGen *g) {
6454 " pub const UnionField = struct {\n"6454 " pub const UnionField = struct {\n"
6455 " name: []const u8,\n"6455 " name: []const u8,\n"
6456 " enum_field: EnumField,\n"6456 " enum_field: EnumField,\n"
6457 " type_info: TypeInfo,\n"6457 " type_info: &TypeInfo,\n"
6458 " };\n"6458 " };\n"
6459 "\n"6459 "\n"
6460 " pub const Union = struct {\n"6460 " pub const Union = struct {\n"
...@@ -6476,7 +6476,7 @@ static void define_builtin_compile_vars(CodeGen *g) {...@@ -6476,7 +6476,7 @@ static void define_builtin_compile_vars(CodeGen *g) {
6476 " pub const FnArg = struct {\n"6476 " pub const FnArg = struct {\n"
6477 " is_comptime: bool,\n"6477 " is_comptime: bool,\n"
6478 " name: []const u8,\n"6478 " name: []const u8,\n"
6479 " type_info: TypeInfo,\n"6479 " type_info: &TypeInfo,\n"
6480 " };\n"6480 " };\n"
6481 "\n"6481 "\n"
6482 " pub const Fn = struct {\n"6482 " 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...@@ -15864,7 +15864,11 @@ static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, ConstExprValue *p
15864 return result;15864 return result;
15865 }15865 }
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
15869 switch (type_entry->id)15873 switch (type_entry->id)
15870 {15874 {
...@@ -15885,7 +15889,7 @@ static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, ConstExprValue *p...@@ -15885,7 +15889,7 @@ static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, ConstExprValue *p
15885 return nullptr;15889 return nullptr;
15886 case TypeTableEntryIdInt:15890 case TypeTableEntryIdInt:
15887 {15891 {
15888 result = create_const_vals(1);15892 ConstExprValue *result = create_const_vals(1);
15889 result->special = ConstValSpecialStatic;15893 result->special = ConstValSpecialStatic;
15890 result->type = ir_type_info_get_type(ira, "Int");15894 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...@@ -15893,6 +15897,7 @@ static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, ConstExprValue *p
15893 result->data.x_struct.fields = fields;15897 result->data.x_struct.fields = fields;
1589415898
15895 ir_type_info_struct_set_parent(result, parent, parent_field_index);15899 ir_type_info_struct_set_parent(result, parent, parent_field_index);
15900 ira->codegen->type_info_cache.put(type_entry, result);
1589615901
15897 // is_signed: bool15902 // is_signed: bool
15898 ensure_field_index(result->type, "is_signed", 0);15903 ensure_field_index(result->type, "is_signed", 0);
...@@ -15904,11 +15909,12 @@ static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, ConstExprValue *p...@@ -15904,11 +15909,12 @@ static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, ConstExprValue *p
15904 fields[1].special = ConstValSpecialStatic;15909 fields[1].special = ConstValSpecialStatic;
15905 fields[1].type = ira->codegen->builtin_types.entry_u8;15910 fields[1].type = ira->codegen->builtin_types.entry_u8;
15906 bigint_init_unsigned(&fields[1].data.x_bigint, type_entry->data.integral.bit_count);15911 bigint_init_unsigned(&fields[1].data.x_bigint, type_entry->data.integral.bit_count);
15907 break;15912
15913 return result;
15908 }15914 }
15909 case TypeTableEntryIdFloat:15915 case TypeTableEntryIdFloat:
15910 {15916 {
15911 result = create_const_vals(1);15917 ConstExprValue *result = create_const_vals(1);
15912 result->special = ConstValSpecialStatic;15918 result->special = ConstValSpecialStatic;
15913 result->type = ir_type_info_get_type(ira, "Float");15919 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...@@ -15916,17 +15922,19 @@ static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, ConstExprValue *p
15916 result->data.x_struct.fields = fields;15922 result->data.x_struct.fields = fields;
1591715923
15918 ir_type_info_struct_set_parent(result, parent, parent_field_index);15924 ir_type_info_struct_set_parent(result, parent, parent_field_index);
15925 ira->codegen->type_info_cache.put(type_entry, result);
1591915926
15920 // bits: u815927 // bits: u8
15921 ensure_field_index(result->type, "bits", 0);15928 ensure_field_index(result->type, "bits", 0);
15922 fields[0].special = ConstValSpecialStatic;15929 fields[0].special = ConstValSpecialStatic;
15923 fields[0].type = ira->codegen->builtin_types.entry_u8;15930 fields[0].type = ira->codegen->builtin_types.entry_u8;
15924 bigint_init_unsigned(&fields->data.x_bigint, type_entry->data.floating.bit_count);15931 bigint_init_unsigned(&fields->data.x_bigint, type_entry->data.floating.bit_count);
15925 break;15932
15933 return result;
15926 }15934 }
15927 case TypeTableEntryIdPointer:15935 case TypeTableEntryIdPointer:
15928 {15936 {
15929 result = create_const_vals(1);15937 ConstExprValue *result = create_const_vals(1);
15930 result->special = ConstValSpecialStatic;15938 result->special = ConstValSpecialStatic;
15931 result->type = ir_type_info_get_type(ira, "Pointer");15939 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...@@ -15934,6 +15942,7 @@ static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, ConstExprValue *p
15934 result->data.x_struct.fields = fields;15942 result->data.x_struct.fields = fields;
1593515943
15936 ir_type_info_struct_set_parent(result, parent, parent_field_index);15944 ir_type_info_struct_set_parent(result, parent, parent_field_index);
15945 ira->codegen->type_info_cache.put(type_entry, result);
1593715946
15938 // is_const: bool15947 // is_const: bool
15939 ensure_field_index(result->type, "is_const", 0);15948 ensure_field_index(result->type, "is_const", 0);
...@@ -15968,11 +15977,12 @@ static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, ConstExprValue *p...@@ -15968,11 +15977,12 @@ static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, ConstExprValue *p
15968 type_entry->data.pointer.child_type);15977 type_entry->data.pointer.child_type);
1596915978
15970 fields[3].data.x_ptr.data.ref.pointee = union_val;15979 fields[3].data.x_ptr.data.ref.pointee = union_val;
15971 break;15980
15981 return result;
15972 }15982 }
15973 case TypeTableEntryIdArray:15983 case TypeTableEntryIdArray:
15974 {15984 {
15975 result = create_const_vals(1);15985 ConstExprValue *result = create_const_vals(1);
15976 result->special = ConstValSpecialStatic;15986 result->special = ConstValSpecialStatic;
15977 result->type = ir_type_info_get_type(ira, "Array");15987 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...@@ -15980,6 +15990,7 @@ static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, ConstExprValue *p
15980 result->data.x_struct.fields = fields;15990 result->data.x_struct.fields = fields;
1598115991
15982 ir_type_info_struct_set_parent(result, parent, parent_field_index);15992 ir_type_info_struct_set_parent(result, parent, parent_field_index);
15993 ira->codegen->type_info_cache.put(type_entry, result);
1598315994
15984 // len: usize15995 // len: usize
15985 ensure_field_index(result->type, "len", 0);15996 ensure_field_index(result->type, "len", 0);
...@@ -15988,7 +15999,6 @@ static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, ConstExprValue *p...@@ -15988,7 +15999,6 @@ static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, ConstExprValue *p
15988 bigint_init_unsigned(&fields[0].data.x_bigint, type_entry->data.array.len);15999 bigint_init_unsigned(&fields[0].data.x_bigint, type_entry->data.array.len);
15989 // child: &TypeInfo16000 // child: &TypeInfo
15990 ensure_field_index(result->type, "child", 1);16001 ensure_field_index(result->type, "child", 1);
15991
15992 TypeTableEntry *type_info_type = ir_type_info_get_type(ira, nullptr);16002 TypeTableEntry *type_info_type = ir_type_info_get_type(ira, nullptr);
1599316003
15994 fields[1].special = ConstValSpecialStatic;16004 fields[1].special = ConstValSpecialStatic;
...@@ -16004,11 +16014,12 @@ static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, ConstExprValue *p...@@ -16004,11 +16014,12 @@ static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, ConstExprValue *p
16004 type_entry->data.array.child_type);16014 type_entry->data.array.child_type);
1600516015
16006 fields[1].data.x_ptr.data.ref.pointee = union_val;16016 fields[1].data.x_ptr.data.ref.pointee = union_val;
16007 break;16017
16018 return result;
16008 }16019 }
16009 case TypeTableEntryIdMaybe:16020 case TypeTableEntryIdMaybe:
16010 {16021 {
16011 result = create_const_vals(1);16022 ConstExprValue *result = create_const_vals(1);
16012 result->special = ConstValSpecialStatic;16023 result->special = ConstValSpecialStatic;
16013 result->type = ir_type_info_get_type(ira, "Nullable");16024 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...@@ -16016,6 +16027,7 @@ static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, ConstExprValue *p
16016 result->data.x_struct.fields = fields;16027 result->data.x_struct.fields = fields;
1601716028
16018 ir_type_info_struct_set_parent(result, parent, parent_field_index);16029 ir_type_info_struct_set_parent(result, parent, parent_field_index);
16030 ira->codegen->type_info_cache.put(type_entry, result);
1601916031
16020 // child: &TypeInfo16032 // child: &TypeInfo
16021 ensure_field_index(result->type, "child", 0);16033 ensure_field_index(result->type, "child", 0);
...@@ -16035,11 +16047,12 @@ static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, ConstExprValue *p...@@ -16035,11 +16047,12 @@ static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, ConstExprValue *p
16035 type_entry->data.maybe.child_type);16047 type_entry->data.maybe.child_type);
1603616048
16037 fields[0].data.x_ptr.data.ref.pointee = union_val;16049 fields[0].data.x_ptr.data.ref.pointee = union_val;
16038 break;16050
16051 return result;
16039 }16052 }
16040 case TypeTableEntryIdPromise:16053 case TypeTableEntryIdPromise:
16041 {16054 {
16042 result = create_const_vals(1);16055 ConstExprValue *result = create_const_vals(1);
16043 result->special = ConstValSpecialStatic;16056 result->special = ConstValSpecialStatic;
16044 result->type = ir_type_info_get_type(ira, "Promise");16057 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...@@ -16047,6 +16060,7 @@ static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, ConstExprValue *p
16047 result->data.x_struct.fields = fields;16060 result->data.x_struct.fields = fields;
1604816061
16049 ir_type_info_struct_set_parent(result, parent, parent_field_index);16062 ir_type_info_struct_set_parent(result, parent, parent_field_index);
16063 ira->codegen->type_info_cache.put(type_entry, result);
1605016064
16051 // child: ?&TypeInfo16065 // child: ?&TypeInfo
16052 ensure_field_index(result->type, "child", 0);16066 ensure_field_index(result->type, "child", 0);
...@@ -16080,16 +16094,91 @@ static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, ConstExprValue *p...@@ -16080,16 +16094,91 @@ static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, ConstExprValue *p
16080 maybe_value->data.x_ptr.data.ref.pointee = union_val;16094 maybe_value->data.x_ptr.data.ref.pointee = union_val;
16081 fields[0].data.x_maybe = maybe_value;16095 fields[0].data.x_maybe = maybe_value;
16082 }16096 }
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;
16084 }16176 }
16085 default:16177 default:
16086 zig_unreachable();16178 zig_unreachable();
16087 }16179 }
1608816180
16089 // Cache the returned value.16181 zig_unreachable();
16090 assert(result != nullptr);
16091 ira->codegen->type_info_cache.put(type_entry, result);
16092 return result;
16093}16182}
1609416183
16095static TypeTableEntry *ir_analyze_instruction_type_info(IrAnalyze *ira,16184static TypeTableEntry *ir_analyze_instruction_type_info(IrAnalyze *ira,