| ... | ... | @@ -14444,8 +14444,7 @@ static IrInstruction *ir_analyze_container_field_ptr(IrAnalyze *ira, Buf *field_ |
| 14444 | 14444 | ConstExprValue *payload_val = union_val->data.x_union.payload; |
| 14445 | 14445 | |
| 14446 | 14446 | TypeTableEntry *field_type = field->type_entry; |
| 14447 | | if (field_type->id == TypeTableEntryIdVoid) |
| 14448 | | { |
| 14447 | if (field_type->id == TypeTableEntryIdVoid) { |
| 14449 | 14448 | assert(payload_val == nullptr); |
| 14450 | 14449 | payload_val = create_const_vals(1); |
| 14451 | 14450 | payload_val->special = ConstValSpecialStatic; |
| ... | ... | @@ -16797,12 +16796,11 @@ static TypeTableEntry *ir_type_info_get_type(IrAnalyze *ira, const char *type_na |
| 16797 | 16796 | return var->value->data.x_type; |
| 16798 | 16797 | } |
| 16799 | 16798 | |
| 16800 | | static bool ir_make_type_info_defs(IrAnalyze *ira, ConstExprValue *out_val, ScopeDecls *decls_scope) |
| 16801 | | { |
| 16799 | static Error ir_make_type_info_defs(IrAnalyze *ira, ConstExprValue *out_val, ScopeDecls *decls_scope) { |
| 16802 | 16800 | Error err; |
| 16803 | 16801 | TypeTableEntry *type_info_definition_type = ir_type_info_get_type(ira, "Definition", nullptr); |
| 16804 | 16802 | if ((err = ensure_complete_type(ira->codegen, type_info_definition_type))) |
| 16805 | | return false; |
| 16803 | return err; |
| 16806 | 16804 | |
| 16807 | 16805 | ensure_field_index(type_info_definition_type, "name", 0); |
| 16808 | 16806 | ensure_field_index(type_info_definition_type, "is_pub", 1); |
| ... | ... | @@ -16810,38 +16808,33 @@ static bool ir_make_type_info_defs(IrAnalyze *ira, ConstExprValue *out_val, Scop |
| 16810 | 16808 | |
| 16811 | 16809 | TypeTableEntry *type_info_definition_data_type = ir_type_info_get_type(ira, "Data", type_info_definition_type); |
| 16812 | 16810 | if ((err = ensure_complete_type(ira->codegen, type_info_definition_data_type))) |
| 16813 | | return false; |
| 16811 | return err; |
| 16814 | 16812 | |
| 16815 | 16813 | TypeTableEntry *type_info_fn_def_type = ir_type_info_get_type(ira, "FnDef", type_info_definition_data_type); |
| 16816 | 16814 | if ((err = ensure_complete_type(ira->codegen, type_info_fn_def_type))) |
| 16817 | | return false; |
| 16815 | return err; |
| 16818 | 16816 | |
| 16819 | 16817 | TypeTableEntry *type_info_fn_def_inline_type = ir_type_info_get_type(ira, "Inline", type_info_fn_def_type); |
| 16820 | 16818 | if ((err = ensure_complete_type(ira->codegen, type_info_fn_def_inline_type))) |
| 16821 | | return false; |
| 16819 | return err; |
| 16822 | 16820 | |
| 16823 | 16821 | // Loop through our definitions once to figure out how many definitions we will generate info for. |
| 16824 | 16822 | auto decl_it = decls_scope->decl_table.entry_iterator(); |
| 16825 | 16823 | decltype(decls_scope->decl_table)::Entry *curr_entry = nullptr; |
| 16826 | 16824 | int definition_count = 0; |
| 16827 | 16825 | |
| 16828 | | while ((curr_entry = decl_it.next()) != nullptr) |
| 16829 | | { |
| 16826 | while ((curr_entry = decl_it.next()) != nullptr) { |
| 16830 | 16827 | // If the definition is unresolved, force it to be resolved again. |
| 16831 | | if (curr_entry->value->resolution == TldResolutionUnresolved) |
| 16832 | | { |
| 16828 | if (curr_entry->value->resolution == TldResolutionUnresolved) { |
| 16833 | 16829 | resolve_top_level_decl(ira->codegen, curr_entry->value, false, curr_entry->value->source_node); |
| 16834 | | if (curr_entry->value->resolution != TldResolutionOk) |
| 16835 | | { |
| 16836 | | return false; |
| 16830 | if (curr_entry->value->resolution != TldResolutionOk) { |
| 16831 | return ErrorSemanticAnalyzeFail; |
| 16837 | 16832 | } |
| 16838 | 16833 | } |
| 16839 | 16834 | |
| 16840 | 16835 | // Skip comptime blocks and test functions. |
| 16841 | | if (curr_entry->value->id != TldIdCompTime) |
| 16842 | | { |
| 16843 | | if (curr_entry->value->id == TldIdFn) |
| 16844 | | { |
| 16836 | if (curr_entry->value->id != TldIdCompTime) { |
| 16837 | if (curr_entry->value->id == TldIdFn) { |
| 16845 | 16838 | FnTableEntry *fn_entry = ((TldFn *)curr_entry->value)->fn_entry; |
| 16846 | 16839 | if (fn_entry->is_test) |
| 16847 | 16840 | continue; |
| ... | ... | @@ -16863,13 +16856,11 @@ static bool ir_make_type_info_defs(IrAnalyze *ira, ConstExprValue *out_val, Scop |
| 16863 | 16856 | decl_it = decls_scope->decl_table.entry_iterator(); |
| 16864 | 16857 | curr_entry = nullptr; |
| 16865 | 16858 | int definition_index = 0; |
| 16866 | | while ((curr_entry = decl_it.next()) != nullptr) |
| 16867 | | { |
| 16859 | while ((curr_entry = decl_it.next()) != nullptr) { |
| 16868 | 16860 | // Skip comptime blocks and test functions. |
| 16869 | | if (curr_entry->value->id == TldIdCompTime) |
| 16861 | if (curr_entry->value->id == TldIdCompTime) { |
| 16870 | 16862 | continue; |
| 16871 | | else if (curr_entry->value->id == TldIdFn) |
| 16872 | | { |
| 16863 | } else if (curr_entry->value->id == TldIdFn) { |
| 16873 | 16864 | FnTableEntry *fn_entry = ((TldFn *)curr_entry->value)->fn_entry; |
| 16874 | 16865 | if (fn_entry->is_test) |
| 16875 | 16866 | continue; |
| ... | ... | @@ -16892,13 +16883,12 @@ static bool ir_make_type_info_defs(IrAnalyze *ira, ConstExprValue *out_val, Scop |
| 16892 | 16883 | inner_fields[2].data.x_union.parent.data.p_struct.struct_val = definition_val; |
| 16893 | 16884 | inner_fields[2].data.x_union.parent.data.p_struct.field_index = 1; |
| 16894 | 16885 | |
| 16895 | | switch (curr_entry->value->id) |
| 16896 | | { |
| 16886 | switch (curr_entry->value->id) { |
| 16897 | 16887 | case TldIdVar: |
| 16898 | 16888 | { |
| 16899 | 16889 | VariableTableEntry *var = ((TldVar *)curr_entry->value)->var; |
| 16900 | 16890 | if ((err = ensure_complete_type(ira->codegen, var->value->type))) |
| 16901 | | return false; |
| 16891 | return ErrorSemanticAnalyzeFail; |
| 16902 | 16892 | |
| 16903 | 16893 | if (var->value->type->id == TypeTableEntryIdMetaType) |
| 16904 | 16894 | { |
| ... | ... | @@ -17029,7 +17019,7 @@ static bool ir_make_type_info_defs(IrAnalyze *ira, ConstExprValue *out_val, Scop |
| 17029 | 17019 | { |
| 17030 | 17020 | TypeTableEntry *type_entry = ((TldContainer *)curr_entry->value)->type_entry; |
| 17031 | 17021 | if ((err = ensure_complete_type(ira->codegen, type_entry))) |
| 17032 | | return false; |
| 17022 | return ErrorSemanticAnalyzeFail; |
| 17033 | 17023 | |
| 17034 | 17024 | // This is a type. |
| 17035 | 17025 | bigint_init_unsigned(&inner_fields[2].data.x_union.tag, 0); |
| ... | ... | @@ -17051,7 +17041,7 @@ static bool ir_make_type_info_defs(IrAnalyze *ira, ConstExprValue *out_val, Scop |
| 17051 | 17041 | } |
| 17052 | 17042 | |
| 17053 | 17043 | assert(definition_index == definition_count); |
| 17054 | | return true; |
| 17044 | return ErrorNone; |
| 17055 | 17045 | } |
| 17056 | 17046 | |
| 17057 | 17047 | static ConstExprValue *create_ptr_like_type_info(IrAnalyze *ira, TypeTableEntry *ptr_type_entry) { |
| ... | ... | @@ -17109,30 +17099,31 @@ static ConstExprValue *create_ptr_like_type_info(IrAnalyze *ira, TypeTableEntry |
| 17109 | 17099 | return result; |
| 17110 | 17100 | }; |
| 17111 | 17101 | |
| 17112 | | static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, TypeTableEntry *type_entry) { |
| 17113 | | Error err; |
| 17114 | | assert(type_entry != nullptr); |
| 17115 | | assert(!type_is_invalid(type_entry)); |
| 17102 | static void make_enum_field_val(IrAnalyze *ira, ConstExprValue *enum_field_val, TypeEnumField *enum_field, |
| 17103 | TypeTableEntry *type_info_enum_field_type) |
| 17104 | { |
| 17105 | enum_field_val->special = ConstValSpecialStatic; |
| 17106 | enum_field_val->type = type_info_enum_field_type; |
| 17116 | 17107 | |
| 17117 | | if ((err = ensure_complete_type(ira->codegen, type_entry))) |
| 17118 | | return nullptr; |
| 17108 | ConstExprValue *inner_fields = create_const_vals(2); |
| 17109 | inner_fields[1].special = ConstValSpecialStatic; |
| 17110 | inner_fields[1].type = ira->codegen->builtin_types.entry_usize; |
| 17119 | 17111 | |
| 17120 | | const auto make_enum_field_val = [ira](ConstExprValue *enum_field_val, TypeEnumField *enum_field, |
| 17121 | | TypeTableEntry *type_info_enum_field_type) { |
| 17122 | | enum_field_val->special = ConstValSpecialStatic; |
| 17123 | | enum_field_val->type = type_info_enum_field_type; |
| 17112 | ConstExprValue *name = create_const_str_lit(ira->codegen, enum_field->name); |
| 17113 | init_const_slice(ira->codegen, &inner_fields[0], name, 0, buf_len(enum_field->name), true); |
| 17124 | 17114 | |
| 17125 | | ConstExprValue *inner_fields = create_const_vals(2); |
| 17126 | | inner_fields[1].special = ConstValSpecialStatic; |
| 17127 | | inner_fields[1].type = ira->codegen->builtin_types.entry_usize; |
| 17115 | bigint_init_bigint(&inner_fields[1].data.x_bigint, &enum_field->value); |
| 17128 | 17116 | |
| 17129 | | ConstExprValue *name = create_const_str_lit(ira->codegen, enum_field->name); |
| 17130 | | init_const_slice(ira->codegen, &inner_fields[0], name, 0, buf_len(enum_field->name), true); |
| 17117 | enum_field_val->data.x_struct.fields = inner_fields; |
| 17118 | } |
| 17131 | 17119 | |
| 17132 | | bigint_init_bigint(&inner_fields[1].data.x_bigint, &enum_field->value); |
| 17120 | static Error ir_make_type_info_value(IrAnalyze *ira, TypeTableEntry *type_entry, ConstExprValue **out) { |
| 17121 | Error err; |
| 17122 | assert(type_entry != nullptr); |
| 17123 | assert(!type_is_invalid(type_entry)); |
| 17133 | 17124 | |
| 17134 | | enum_field_val->data.x_struct.fields = inner_fields; |
| 17135 | | }; |
| 17125 | if ((err = ensure_complete_type(ira->codegen, type_entry))) |
| 17126 | return err; |
| 17136 | 17127 | |
| 17137 | 17128 | if (type_entry == ira->codegen->builtin_types.entry_global_error_set) { |
| 17138 | 17129 | zig_panic("TODO implement @typeInfo for global error set"); |
| ... | ... | @@ -17155,13 +17146,16 @@ static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, TypeTableEntry *t |
| 17155 | 17146 | case TypeTableEntryIdBlock: |
| 17156 | 17147 | case TypeTableEntryIdArgTuple: |
| 17157 | 17148 | case TypeTableEntryIdOpaque: |
| 17158 | | return nullptr; |
| 17149 | *out = nullptr; |
| 17150 | return ErrorNone; |
| 17159 | 17151 | default: |
| 17160 | 17152 | { |
| 17161 | 17153 | // Lookup an available value in our cache. |
| 17162 | 17154 | auto entry = ira->codegen->type_info_cache.maybe_get(type_entry); |
| 17163 | | if (entry != nullptr) |
| 17164 | | return entry->value; |
| 17155 | if (entry != nullptr) { |
| 17156 | *out = entry->value; |
| 17157 | return ErrorNone; |
| 17158 | } |
| 17165 | 17159 | |
| 17166 | 17160 | // Fallthrough if we don't find one. |
| 17167 | 17161 | } |
| ... | ... | @@ -17312,15 +17306,15 @@ static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, TypeTableEntry *t |
| 17312 | 17306 | { |
| 17313 | 17307 | TypeEnumField *enum_field = &type_entry->data.enumeration.fields[enum_field_index]; |
| 17314 | 17308 | ConstExprValue *enum_field_val = &enum_field_array->data.x_array.s_none.elements[enum_field_index]; |
| 17315 | | make_enum_field_val(enum_field_val, enum_field, type_info_enum_field_type); |
| 17309 | make_enum_field_val(ira, enum_field_val, enum_field, type_info_enum_field_type); |
| 17316 | 17310 | enum_field_val->data.x_struct.parent.id = ConstParentIdArray; |
| 17317 | 17311 | enum_field_val->data.x_struct.parent.data.p_array.array_val = enum_field_array; |
| 17318 | 17312 | enum_field_val->data.x_struct.parent.data.p_array.elem_index = enum_field_index; |
| 17319 | 17313 | } |
| 17320 | 17314 | // defs: []TypeInfo.Definition |
| 17321 | 17315 | ensure_field_index(result->type, "defs", 3); |
| 17322 | | if (!ir_make_type_info_defs(ira, &fields[3], type_entry->data.enumeration.decls_scope)) |
| 17323 | | return nullptr; |
| 17316 | if ((err = ir_make_type_info_defs(ira, &fields[3], type_entry->data.enumeration.decls_scope))) |
| 17317 | return err; |
| 17324 | 17318 | |
| 17325 | 17319 | break; |
| 17326 | 17320 | } |
| ... | ... | @@ -17346,8 +17340,7 @@ static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, TypeTableEntry *t |
| 17346 | 17340 | error_array->data.x_array.s_none.elements = create_const_vals(error_count); |
| 17347 | 17341 | |
| 17348 | 17342 | init_const_slice(ira->codegen, &fields[0], error_array, 0, error_count, false); |
| 17349 | | for (uint32_t error_index = 0; error_index < error_count; error_index++) |
| 17350 | | { |
| 17343 | for (uint32_t error_index = 0; error_index < error_count; error_index++) { |
| 17351 | 17344 | ErrorTableEntry *error = type_entry->data.error_set.errors[error_index]; |
| 17352 | 17345 | ConstExprValue *error_val = &error_array->data.x_array.s_none.elements[error_index]; |
| 17353 | 17346 | |
| ... | ... | @@ -17425,9 +17418,9 @@ static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, TypeTableEntry *t |
| 17425 | 17418 | tag_type->type = ira->codegen->builtin_types.entry_type; |
| 17426 | 17419 | tag_type->data.x_type = type_entry->data.unionation.tag_type; |
| 17427 | 17420 | fields[1].data.x_optional = tag_type; |
| 17428 | | } |
| 17429 | | else |
| 17421 | } else { |
| 17430 | 17422 | fields[1].data.x_optional = nullptr; |
| 17423 | } |
| 17431 | 17424 | // fields: []TypeInfo.UnionField |
| 17432 | 17425 | ensure_field_index(result->type, "fields", 2); |
| 17433 | 17426 | |
| ... | ... | @@ -17460,7 +17453,7 @@ static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, TypeTableEntry *t |
| 17460 | 17453 | inner_fields[1].data.x_optional = nullptr; |
| 17461 | 17454 | } else { |
| 17462 | 17455 | inner_fields[1].data.x_optional = create_const_vals(1); |
| 17463 | | make_enum_field_val(inner_fields[1].data.x_optional, union_field->enum_field, type_info_enum_field_type); |
| 17456 | make_enum_field_val(ira, inner_fields[1].data.x_optional, union_field->enum_field, type_info_enum_field_type); |
| 17464 | 17457 | } |
| 17465 | 17458 | |
| 17466 | 17459 | inner_fields[2].special = ConstValSpecialStatic; |
| ... | ... | @@ -17477,8 +17470,8 @@ static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, TypeTableEntry *t |
| 17477 | 17470 | } |
| 17478 | 17471 | // defs: []TypeInfo.Definition |
| 17479 | 17472 | ensure_field_index(result->type, "defs", 3); |
| 17480 | | if (!ir_make_type_info_defs(ira, &fields[3], type_entry->data.unionation.decls_scope)) |
| 17481 | | return nullptr; |
| 17473 | if ((err = ir_make_type_info_defs(ira, &fields[3], type_entry->data.unionation.decls_scope))) |
| 17474 | return err; |
| 17482 | 17475 | |
| 17483 | 17476 | break; |
| 17484 | 17477 | } |
| ... | ... | @@ -17551,8 +17544,8 @@ static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, TypeTableEntry *t |
| 17551 | 17544 | } |
| 17552 | 17545 | // defs: []TypeInfo.Definition |
| 17553 | 17546 | ensure_field_index(result->type, "defs", 2); |
| 17554 | | if (!ir_make_type_info_defs(ira, &fields[2], type_entry->data.structure.decls_scope)) |
| 17555 | | return nullptr; |
| 17547 | if ((err = ir_make_type_info_defs(ira, &fields[2], type_entry->data.structure.decls_scope))) |
| 17548 | return err; |
| 17556 | 17549 | |
| 17557 | 17550 | break; |
| 17558 | 17551 | } |
| ... | ... | @@ -17665,7 +17658,8 @@ static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, TypeTableEntry *t |
| 17665 | 17658 | { |
| 17666 | 17659 | TypeTableEntry *fn_type = type_entry->data.bound_fn.fn_type; |
| 17667 | 17660 | assert(fn_type->id == TypeTableEntryIdFn); |
| 17668 | | result = ir_make_type_info_value(ira, fn_type); |
| 17661 | if ((err = ir_make_type_info_value(ira, fn_type, &result))) |
| 17662 | return err; |
| 17669 | 17663 | |
| 17670 | 17664 | break; |
| 17671 | 17665 | } |
| ... | ... | @@ -17673,12 +17667,14 @@ static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, TypeTableEntry *t |
| 17673 | 17667 | |
| 17674 | 17668 | assert(result != nullptr); |
| 17675 | 17669 | ira->codegen->type_info_cache.put(type_entry, result); |
| 17676 | | return result; |
| 17670 | *out = result; |
| 17671 | return ErrorNone; |
| 17677 | 17672 | } |
| 17678 | 17673 | |
| 17679 | 17674 | static TypeTableEntry *ir_analyze_instruction_type_info(IrAnalyze *ira, |
| 17680 | 17675 | IrInstructionTypeInfo *instruction) |
| 17681 | 17676 | { |
| 17677 | Error err; |
| 17682 | 17678 | IrInstruction *type_value = instruction->type_value->other; |
| 17683 | 17679 | TypeTableEntry *type_entry = ir_resolve_type(ira, type_value); |
| 17684 | 17680 | if (type_is_invalid(type_entry)) |
| ... | ... | @@ -17686,15 +17682,16 @@ static TypeTableEntry *ir_analyze_instruction_type_info(IrAnalyze *ira, |
| 17686 | 17682 | |
| 17687 | 17683 | TypeTableEntry *result_type = ir_type_info_get_type(ira, nullptr, nullptr); |
| 17688 | 17684 | |
| 17685 | ConstExprValue *payload; |
| 17686 | if ((err = ir_make_type_info_value(ira, type_entry, &payload))) |
| 17687 | return ira->codegen->builtin_types.entry_invalid; |
| 17688 | |
| 17689 | 17689 | ConstExprValue *out_val = ir_build_const_from(ira, &instruction->base); |
| 17690 | 17690 | out_val->type = result_type; |
| 17691 | 17691 | bigint_init_unsigned(&out_val->data.x_union.tag, type_id_index(type_entry)); |
| 17692 | | |
| 17693 | | ConstExprValue *payload = ir_make_type_info_value(ira, type_entry); |
| 17694 | 17692 | out_val->data.x_union.payload = payload; |
| 17695 | 17693 | |
| 17696 | | if (payload != nullptr) |
| 17697 | | { |
| 17694 | if (payload != nullptr) { |
| 17698 | 17695 | assert(payload->type->id == TypeTableEntryIdStruct); |
| 17699 | 17696 | payload->data.x_struct.parent.id = ConstParentIdUnion; |
| 17700 | 17697 | payload->data.x_struct.parent.data.p_union.union_val = out_val; |