| ... | ... | @@ -15728,27 +15728,13 @@ static TypeTableEntry *ir_analyze_instruction_offset_of(IrAnalyze *ira, |
| 15728 | 15728 | return ira->codegen->builtin_types.entry_num_lit_int; |
| 15729 | 15729 | } |
| 15730 | 15730 | |
| 15731 | | static TypeTableEntry *ir_analyze_instruction_type_info(IrAnalyze *ira, |
| 15732 | | IrInstructionTypeInfo *instruction) |
| 15731 | static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, ConstExprValue *parent, TypeTableEntry *type_entry) |
| 15733 | 15732 | { |
| 15734 | | IrInstruction *type_value = instruction->type_value->other; |
| 15735 | | TypeTableEntry *type_entry = ir_resolve_type(ira, type_value); |
| 15736 | | if (type_is_invalid(type_entry)) |
| 15737 | | return ira->codegen->builtin_types.entry_invalid; |
| 15733 | assert(type_entry != nullptr); |
| 15734 | assert(!type_is_invalid(type_entry)); |
| 15738 | 15735 | |
| 15739 | | ConstExprValue *var_value = get_builtin_value(ira->codegen, "TypeInfo"); |
| 15740 | | assert(var_value->type->id == TypeTableEntryIdMetaType); |
| 15741 | | TypeTableEntry *result_type = var_value->data.x_type; |
| 15742 | | |
| 15743 | | ConstExprValue *out_val = ir_build_const_from(ira, &instruction->base); |
| 15744 | | // TODO: Do I need those? probably set in ir_build_const_from |
| 15745 | | //out_val->special = ConstValSpecialStatic; |
| 15746 | | //out_val->type = result_type; |
| 15747 | | |
| 15748 | | bigint_init_unsigned(&out_val->data.x_union.tag, type_id_index(type_entry->id)); |
| 15749 | | //out_val->data.x_union.parent.id = ConstParentIdNone; |
| 15750 | | |
| 15751 | | switch (type_entry->id) { |
| 15736 | switch (type_entry->id) |
| 15737 | { |
| 15752 | 15738 | case TypeTableEntryIdInvalid: |
| 15753 | 15739 | zig_unreachable(); |
| 15754 | 15740 | case TypeTableEntryIdMetaType: |
| ... | ... | @@ -15763,14 +15749,11 @@ static TypeTableEntry *ir_analyze_instruction_type_info(IrAnalyze *ira, |
| 15763 | 15749 | case TypeTableEntryIdBlock: |
| 15764 | 15750 | case TypeTableEntryIdArgTuple: |
| 15765 | 15751 | case TypeTableEntryIdOpaque: |
| 15766 | | out_val->data.x_union.payload = nullptr; |
| 15767 | | break; |
| 15752 | // TODO: Construct a valid void payload. |
| 15753 | return nullptr; |
| 15768 | 15754 | case TypeTableEntryIdInt: |
| 15769 | 15755 | { |
| 15770 | | // Error from 'ir_resolve_const': "unable to evaluate constant expression" |
| 15771 | 15756 | ConstExprValue *payload = create_const_vals(1); |
| 15772 | | out_val->data.x_union.payload = payload; |
| 15773 | | |
| 15774 | 15757 | payload->special = ConstValSpecialStatic; |
| 15775 | 15758 | |
| 15776 | 15759 | ConstExprValue *int_info_type = get_builtin_value(ira->codegen, "IntInfo"); |
| ... | ... | @@ -15780,23 +15763,135 @@ static TypeTableEntry *ir_analyze_instruction_type_info(IrAnalyze *ira, |
| 15780 | 15763 | ConstExprValue *fields = create_const_vals(2); |
| 15781 | 15764 | payload->data.x_struct.fields = fields; |
| 15782 | 15765 | |
| 15783 | | payload->data.x_struct.parent.id = ConstParentIdUnion; |
| 15784 | | payload->data.x_struct.parent.data.p_union.union_val = out_val; |
| 15766 | if (parent->type->id == TypeTableEntryIdStruct) |
| 15767 | { |
| 15768 | payload->data.x_struct.parent.id = ConstParentIdStruct; |
| 15769 | payload->data.x_struct.parent.data.p_union.union_val = parent; |
| 15770 | } |
| 15771 | else if (parent->type->id == TypeTableEntryIdUnion) |
| 15772 | { |
| 15773 | payload->data.x_struct.parent.id = ConstParentIdUnion; |
| 15774 | payload->data.x_struct.parent.data.p_union.union_val = parent; |
| 15775 | } |
| 15776 | else |
| 15777 | { |
| 15778 | payload->data.x_struct.parent.id = ConstParentIdNone; |
| 15779 | } |
| 15785 | 15780 | |
| 15786 | | // TODO: See what happens if we don't set the field type (set it to nullptr) |
| 15781 | // is_signed: bool |
| 15787 | 15782 | fields[0].special = ConstValSpecialStatic; |
| 15788 | 15783 | fields[0].type = ira->codegen->builtin_types.entry_bool; |
| 15789 | 15784 | fields[0].data.x_bool = type_entry->data.integral.is_signed; |
| 15790 | | |
| 15785 | // bits: u8 |
| 15791 | 15786 | fields[1].special = ConstValSpecialStatic; |
| 15792 | 15787 | fields[1].type = ira->codegen->builtin_types.entry_u8; |
| 15793 | 15788 | bigint_init_unsigned(&fields[1].data.x_bigint, type_entry->data.integral.bit_count); |
| 15794 | 15789 | |
| 15795 | | break; |
| 15790 | return payload; |
| 15791 | } |
| 15792 | case TypeTableEntryIdFloat: |
| 15793 | { |
| 15794 | ConstExprValue *payload = create_const_vals(1); |
| 15795 | payload->special = ConstValSpecialStatic; |
| 15796 | |
| 15797 | ConstExprValue *float_info_type = get_builtin_value(ira->codegen, "FloatInfo"); |
| 15798 | assert(float_info_type->type->id == TypeTableEntryIdMetaType); |
| 15799 | payload->type = float_info_type->data.x_type; |
| 15800 | |
| 15801 | ConstExprValue *fields = create_const_vals(1); |
| 15802 | payload->data.x_struct.fields = fields; |
| 15803 | |
| 15804 | if (parent->type->id == TypeTableEntryIdStruct) |
| 15805 | { |
| 15806 | payload->data.x_struct.parent.id = ConstParentIdStruct; |
| 15807 | payload->data.x_struct.parent.data.p_union.union_val = parent; |
| 15808 | } |
| 15809 | else if (parent->type->id == TypeTableEntryIdUnion) |
| 15810 | { |
| 15811 | payload->data.x_struct.parent.id = ConstParentIdUnion; |
| 15812 | payload->data.x_struct.parent.data.p_union.union_val = parent; |
| 15813 | } |
| 15814 | else |
| 15815 | { |
| 15816 | payload->data.x_struct.parent.id = ConstParentIdNone; |
| 15817 | } |
| 15818 | // bits: u8 |
| 15819 | fields[0].special = ConstValSpecialStatic; |
| 15820 | fields[0].type = ira->codegen->builtin_types.entry_u8; |
| 15821 | bigint_init_unsigned(&fields->data.x_bigint, type_entry->data.floating.bit_count); |
| 15822 | |
| 15823 | return payload; |
| 15824 | } |
| 15825 | case TypeTableEntryIdPointer: |
| 15826 | { |
| 15827 | ConstExprValue *payload = create_const_vals(1); |
| 15828 | payload->special = ConstValSpecialStatic; |
| 15829 | |
| 15830 | ConstExprValue *pointer_info_type = get_builtin_value(ira->codegen, "PointerInfo"); |
| 15831 | assert(pointer_info_type->type->id == TypeTableEntryIdMetaType); |
| 15832 | payload->type = pointer_info_type->data.x_type; |
| 15833 | |
| 15834 | ConstExprValue *fields = create_const_vals(4); |
| 15835 | payload->data.x_struct.fields = fields; |
| 15836 | |
| 15837 | if (parent->type->id == TypeTableEntryIdStruct) |
| 15838 | { |
| 15839 | payload->data.x_struct.parent.id = ConstParentIdStruct; |
| 15840 | payload->data.x_struct.parent.data.p_union.union_val = parent; |
| 15841 | } |
| 15842 | else if (parent->type->id == TypeTableEntryIdUnion) |
| 15843 | { |
| 15844 | payload->data.x_struct.parent.id = ConstParentIdUnion; |
| 15845 | payload->data.x_struct.parent.data.p_union.union_val = parent; |
| 15846 | } |
| 15847 | else |
| 15848 | { |
| 15849 | payload->data.x_struct.parent.id = ConstParentIdNone; |
| 15850 | } |
| 15851 | // is_const: bool |
| 15852 | fields[0].special = ConstValSpecialStatic; |
| 15853 | fields[0].type = ira->codegen->builtin_types.entry_bool; |
| 15854 | fields[0].data.x_bool = type_entry->data.pointer.is_const; |
| 15855 | // is_volatile: bool |
| 15856 | fields[1].special = ConstValSpecialStatic; |
| 15857 | fields[1].type = ira->codegen->builtin_types.entry_bool; |
| 15858 | fields[1].data.x_bool = type_entry->data.pointer.is_volatile; |
| 15859 | // alignment: u32 |
| 15860 | fields[2].special = ConstValSpecialStatic; |
| 15861 | fields[2].type = ira->codegen->builtin_types.entry_u32; |
| 15862 | bigint_init_unsigned(&fields->data.x_bigint, type_entry->data.pointer.alignment); |
| 15863 | // child: &TypeInfo |
| 15864 | ConstExprValue *type_info_type = get_builtin_value(ira->codegen, "TypeInfo"); |
| 15865 | assert(type_info_type->type->id == TypeTableEntryIdMetaType); |
| 15866 | fields[3].special = ConstValSpecialStatic; |
| 15867 | fields[3].type = get_pointer_to_type(ira->codegen, type_info_type->data.x_type, false); |
| 15868 | fields[3].data.x_ptr.special = ConstPtrSpecialRef; |
| 15869 | fields[3].data.x_ptr.mut = ConstPtrMutComptimeVar; |
| 15870 | fields[3].data.x_ptr.data.ref.pointee = ir_make_type_info_value(ira, &fields[3], type_entry->data.pointer.child_type); |
| 15871 | return payload; |
| 15796 | 15872 | } |
| 15797 | 15873 | default: |
| 15798 | | zig_panic("@typeInfo unsupported for %s", buf_ptr(&type_entry->name)); |
| 15874 | zig_unreachable(); |
| 15799 | 15875 | } |
| 15876 | } |
| 15877 | |
| 15878 | static TypeTableEntry *ir_analyze_instruction_type_info(IrAnalyze *ira, |
| 15879 | IrInstructionTypeInfo *instruction) |
| 15880 | { |
| 15881 | IrInstruction *type_value = instruction->type_value->other; |
| 15882 | TypeTableEntry *type_entry = ir_resolve_type(ira, type_value); |
| 15883 | if (type_is_invalid(type_entry)) |
| 15884 | return ira->codegen->builtin_types.entry_invalid; |
| 15885 | |
| 15886 | ConstExprValue *var_value = get_builtin_value(ira->codegen, "TypeInfo"); |
| 15887 | assert(var_value->type->id == TypeTableEntryIdMetaType); |
| 15888 | TypeTableEntry *result_type = var_value->data.x_type; |
| 15889 | |
| 15890 | // TODO: We need to return a const pointer to the typeinfo, not the typeinfo by value. |
| 15891 | ConstExprValue *out_val = ir_build_const_from(ira, &instruction->base); |
| 15892 | out_val->type = result_type; |
| 15893 | bigint_init_unsigned(&out_val->data.x_union.tag, type_id_index(type_entry->id)); |
| 15894 | out_val->data.x_union.payload = ir_make_type_info_value(ira, out_val, type_entry); |
| 15800 | 15895 | |
| 15801 | 15896 | return result_type; |
| 15802 | 15897 | } |