| ... | @@ -4195,7 +4195,7 @@ static IrInstruction *ir_analyze_cast(IrAnalyze *ira, IrInstruction *source_inst | ... | @@ -4195,7 +4195,7 @@ static IrInstruction *ir_analyze_cast(IrAnalyze *ira, IrInstruction *source_inst |
| 4195 | return ira->codegen->invalid_instruction; | 4195 | return ira->codegen->invalid_instruction; |
| 4196 | } | 4196 | } |
| 4197 | | 4197 | |
| 4198 | static IrInstruction *ir_get_casted_value(IrAnalyze *ira, IrInstruction *value, TypeTableEntry *expected_type) { | 4198 | static IrInstruction *ir_implicit_cast(IrAnalyze *ira, IrInstruction *value, TypeTableEntry *expected_type) { |
| 4199 | assert(value); | 4199 | assert(value); |
| 4200 | assert(value != ira->codegen->invalid_instruction); | 4200 | assert(value != ira->codegen->invalid_instruction); |
| 4201 | assert(!expected_type || expected_type->id != TypeTableEntryIdInvalid); | 4201 | assert(!expected_type || expected_type->id != TypeTableEntryIdInvalid); |
| ... | @@ -4283,7 +4283,7 @@ static bool ir_resolve_usize(IrAnalyze *ira, IrInstruction *value, uint64_t *out | ... | @@ -4283,7 +4283,7 @@ static bool ir_resolve_usize(IrAnalyze *ira, IrInstruction *value, uint64_t *out |
| 4283 | if (value->type_entry->id == TypeTableEntryIdInvalid) | 4283 | if (value->type_entry->id == TypeTableEntryIdInvalid) |
| 4284 | return false; | 4284 | return false; |
| 4285 | | 4285 | |
| 4286 | IrInstruction *casted_value = ir_get_casted_value(ira, value, ira->codegen->builtin_types.entry_usize); | 4286 | IrInstruction *casted_value = ir_implicit_cast(ira, value, ira->codegen->builtin_types.entry_usize); |
| 4287 | if (casted_value->type_entry->id == TypeTableEntryIdInvalid) | 4287 | if (casted_value->type_entry->id == TypeTableEntryIdInvalid) |
| 4288 | return false; | 4288 | return false; |
| 4289 | | 4289 | |
| ... | @@ -4299,7 +4299,7 @@ static bool ir_resolve_bool(IrAnalyze *ira, IrInstruction *value, bool *out) { | ... | @@ -4299,7 +4299,7 @@ static bool ir_resolve_bool(IrAnalyze *ira, IrInstruction *value, bool *out) { |
| 4299 | if (value->type_entry->id == TypeTableEntryIdInvalid) | 4299 | if (value->type_entry->id == TypeTableEntryIdInvalid) |
| 4300 | return false; | 4300 | return false; |
| 4301 | | 4301 | |
| 4302 | IrInstruction *casted_value = ir_get_casted_value(ira, value, ira->codegen->builtin_types.entry_bool); | 4302 | IrInstruction *casted_value = ir_implicit_cast(ira, value, ira->codegen->builtin_types.entry_bool); |
| 4303 | if (casted_value->type_entry->id == TypeTableEntryIdInvalid) | 4303 | if (casted_value->type_entry->id == TypeTableEntryIdInvalid) |
| 4304 | return false; | 4304 | return false; |
| 4305 | | 4305 | |
| ... | @@ -4315,7 +4315,7 @@ static bool ir_resolve_atomic_order(IrAnalyze *ira, IrInstruction *value, Atomic | ... | @@ -4315,7 +4315,7 @@ static bool ir_resolve_atomic_order(IrAnalyze *ira, IrInstruction *value, Atomic |
| 4315 | if (value->type_entry->id == TypeTableEntryIdInvalid) | 4315 | if (value->type_entry->id == TypeTableEntryIdInvalid) |
| 4316 | return false; | 4316 | return false; |
| 4317 | | 4317 | |
| 4318 | IrInstruction *casted_value = ir_get_casted_value(ira, value, ira->codegen->builtin_types.entry_atomic_order_enum); | 4318 | IrInstruction *casted_value = ir_implicit_cast(ira, value, ira->codegen->builtin_types.entry_atomic_order_enum); |
| 4319 | if (casted_value->type_entry->id == TypeTableEntryIdInvalid) | 4319 | if (casted_value->type_entry->id == TypeTableEntryIdInvalid) |
| 4320 | return false; | 4320 | return false; |
| 4321 | | 4321 | |
| ... | @@ -4332,7 +4332,7 @@ static Buf *ir_resolve_str(IrAnalyze *ira, IrInstruction *value) { | ... | @@ -4332,7 +4332,7 @@ static Buf *ir_resolve_str(IrAnalyze *ira, IrInstruction *value) { |
| 4332 | return nullptr; | 4332 | return nullptr; |
| 4333 | | 4333 | |
| 4334 | TypeTableEntry *str_type = get_slice_type(ira->codegen, ira->codegen->builtin_types.entry_u8, true); | 4334 | TypeTableEntry *str_type = get_slice_type(ira->codegen, ira->codegen->builtin_types.entry_u8, true); |
| 4335 | IrInstruction *casted_value = ir_get_casted_value(ira, value, str_type); | 4335 | IrInstruction *casted_value = ir_implicit_cast(ira, value, str_type); |
| 4336 | if (casted_value->type_entry->id == TypeTableEntryIdInvalid) | 4336 | if (casted_value->type_entry->id == TypeTableEntryIdInvalid) |
| 4337 | return nullptr; | 4337 | return nullptr; |
| 4338 | | 4338 | |
| ... | @@ -4366,7 +4366,7 @@ static TypeTableEntry *ir_analyze_instruction_return(IrAnalyze *ira, | ... | @@ -4366,7 +4366,7 @@ static TypeTableEntry *ir_analyze_instruction_return(IrAnalyze *ira, |
| 4366 | return ir_unreach_error(ira); | 4366 | return ir_unreach_error(ira); |
| 4367 | ira->implicit_return_type_list.append(value); | 4367 | ira->implicit_return_type_list.append(value); |
| 4368 | | 4368 | |
| 4369 | IrInstruction *casted_value = ir_get_casted_value(ira, value, ira->explicit_return_type); | 4369 | IrInstruction *casted_value = ir_implicit_cast(ira, value, ira->explicit_return_type); |
| 4370 | if (casted_value == ira->codegen->invalid_instruction) | 4370 | if (casted_value == ira->codegen->invalid_instruction) |
| 4371 | return ir_unreach_error(ira); | 4371 | return ir_unreach_error(ira); |
| 4372 | | 4372 | |
| ... | @@ -4392,11 +4392,11 @@ static TypeTableEntry *ir_analyze_bin_op_bool(IrAnalyze *ira, IrInstructionBinOp | ... | @@ -4392,11 +4392,11 @@ static TypeTableEntry *ir_analyze_bin_op_bool(IrAnalyze *ira, IrInstructionBinOp |
| 4392 | | 4392 | |
| 4393 | TypeTableEntry *bool_type = ira->codegen->builtin_types.entry_bool; | 4393 | TypeTableEntry *bool_type = ira->codegen->builtin_types.entry_bool; |
| 4394 | | 4394 | |
| 4395 | IrInstruction *casted_op1 = ir_get_casted_value(ira, op1, bool_type); | 4395 | IrInstruction *casted_op1 = ir_implicit_cast(ira, op1, bool_type); |
| 4396 | if (casted_op1 == ira->codegen->invalid_instruction) | 4396 | if (casted_op1 == ira->codegen->invalid_instruction) |
| 4397 | return ira->codegen->builtin_types.entry_invalid; | 4397 | return ira->codegen->builtin_types.entry_invalid; |
| 4398 | | 4398 | |
| 4399 | IrInstruction *casted_op2 = ir_get_casted_value(ira, op2, bool_type); | 4399 | IrInstruction *casted_op2 = ir_implicit_cast(ira, op2, bool_type); |
| 4400 | if (casted_op2 == ira->codegen->invalid_instruction) | 4400 | if (casted_op2 == ira->codegen->invalid_instruction) |
| 4401 | return ira->codegen->builtin_types.entry_invalid; | 4401 | return ira->codegen->builtin_types.entry_invalid; |
| 4402 | | 4402 | |
| ... | @@ -4485,11 +4485,11 @@ static TypeTableEntry *ir_analyze_bin_op_cmp(IrAnalyze *ira, IrInstructionBinOp | ... | @@ -4485,11 +4485,11 @@ static TypeTableEntry *ir_analyze_bin_op_cmp(IrAnalyze *ira, IrInstructionBinOp |
| 4485 | zig_unreachable(); | 4485 | zig_unreachable(); |
| 4486 | } | 4486 | } |
| 4487 | | 4487 | |
| 4488 | IrInstruction *casted_op1 = ir_get_casted_value(ira, op1, resolved_type); | 4488 | IrInstruction *casted_op1 = ir_implicit_cast(ira, op1, resolved_type); |
| 4489 | if (casted_op1 == ira->codegen->invalid_instruction) | 4489 | if (casted_op1 == ira->codegen->invalid_instruction) |
| 4490 | return ira->codegen->builtin_types.entry_invalid; | 4490 | return ira->codegen->builtin_types.entry_invalid; |
| 4491 | | 4491 | |
| 4492 | IrInstruction *casted_op2 = ir_get_casted_value(ira, op2, resolved_type); | 4492 | IrInstruction *casted_op2 = ir_implicit_cast(ira, op2, resolved_type); |
| 4493 | if (casted_op2 == ira->codegen->invalid_instruction) | 4493 | if (casted_op2 == ira->codegen->invalid_instruction) |
| 4494 | return ira->codegen->builtin_types.entry_invalid; | 4494 | return ira->codegen->builtin_types.entry_invalid; |
| 4495 | | 4495 | |
| ... | @@ -4669,11 +4669,11 @@ static TypeTableEntry *ir_analyze_bin_op_math(IrAnalyze *ira, IrInstructionBinOp | ... | @@ -4669,11 +4669,11 @@ static TypeTableEntry *ir_analyze_bin_op_math(IrAnalyze *ira, IrInstructionBinOp |
| 4669 | return ira->codegen->builtin_types.entry_invalid; | 4669 | return ira->codegen->builtin_types.entry_invalid; |
| 4670 | } | 4670 | } |
| 4671 | | 4671 | |
| 4672 | IrInstruction *casted_op1 = ir_get_casted_value(ira, op1, resolved_type); | 4672 | IrInstruction *casted_op1 = ir_implicit_cast(ira, op1, resolved_type); |
| 4673 | if (casted_op1 == ira->codegen->invalid_instruction) | 4673 | if (casted_op1 == ira->codegen->invalid_instruction) |
| 4674 | return ira->codegen->builtin_types.entry_invalid; | 4674 | return ira->codegen->builtin_types.entry_invalid; |
| 4675 | | 4675 | |
| 4676 | IrInstruction *casted_op2 = ir_get_casted_value(ira, op2, resolved_type); | 4676 | IrInstruction *casted_op2 = ir_implicit_cast(ira, op2, resolved_type); |
| 4677 | if (casted_op2 == ira->codegen->invalid_instruction) | 4677 | if (casted_op2 == ira->codegen->invalid_instruction) |
| 4678 | return ira->codegen->builtin_types.entry_invalid; | 4678 | return ira->codegen->builtin_types.entry_invalid; |
| 4679 | | 4679 | |
| ... | @@ -4709,6 +4709,165 @@ static TypeTableEntry *ir_analyze_bin_op_math(IrAnalyze *ira, IrInstructionBinOp | ... | @@ -4709,6 +4709,165 @@ static TypeTableEntry *ir_analyze_bin_op_math(IrAnalyze *ira, IrInstructionBinOp |
| 4709 | return resolved_type; | 4709 | return resolved_type; |
| 4710 | } | 4710 | } |
| 4711 | | 4711 | |
| | 4712 | static TypeTableEntry *ir_analyze_array_cat(IrAnalyze *ira, IrInstructionBinOp *instruction) { |
| | 4713 | IrInstruction *op1 = instruction->op1->other; |
| | 4714 | if (op1->type_entry->id == TypeTableEntryIdInvalid) |
| | 4715 | return ira->codegen->builtin_types.entry_invalid; |
| | 4716 | |
| | 4717 | IrInstruction *op2 = instruction->op2->other; |
| | 4718 | if (op2->type_entry->id == TypeTableEntryIdInvalid) |
| | 4719 | return ira->codegen->builtin_types.entry_invalid; |
| | 4720 | |
| | 4721 | // AstNode **op1 = node->data.bin_op_expr.op1->parent_field; |
| | 4722 | // AstNode **op2 = node->data.bin_op_expr.op2->parent_field; |
| | 4723 | // |
| | 4724 | // TypeTableEntry *op1_type = analyze_expression(g, import, context, nullptr, *op1); |
| | 4725 | // TypeTableEntry *child_type; |
| | 4726 | // if (op1_type->id == TypeTableEntryIdInvalid) { |
| | 4727 | // return g->builtin_types.entry_invalid; |
| | 4728 | // } else if (op1_type->id == TypeTableEntryIdArray) { |
| | 4729 | // child_type = op1_type->data.array.child_type; |
| | 4730 | // } else if (op1_type->id == TypeTableEntryIdPointer && |
| | 4731 | // op1_type->data.pointer.child_type == g->builtin_types.entry_u8) { |
| | 4732 | // child_type = op1_type->data.pointer.child_type; |
| | 4733 | // } else { |
| | 4734 | // add_node_error(g, *op1, buf_sprintf("expected array or C string literal, found '%s'", |
| | 4735 | // buf_ptr(&op1_type->name))); |
| | 4736 | // return g->builtin_types.entry_invalid; |
| | 4737 | // } |
| | 4738 | // |
| | 4739 | // TypeTableEntry *op2_type = analyze_expression(g, import, context, nullptr, *op2); |
| | 4740 | // |
| | 4741 | // if (op2_type->id == TypeTableEntryIdInvalid) { |
| | 4742 | // return g->builtin_types.entry_invalid; |
| | 4743 | // } else if (op2_type->id == TypeTableEntryIdArray) { |
| | 4744 | // if (op2_type->data.array.child_type != child_type) { |
| | 4745 | // add_node_error(g, *op2, buf_sprintf("expected array of type '%s', found '%s'", |
| | 4746 | // buf_ptr(&child_type->name), |
| | 4747 | // buf_ptr(&op2_type->name))); |
| | 4748 | // return g->builtin_types.entry_invalid; |
| | 4749 | // } |
| | 4750 | // } else if (op2_type->id == TypeTableEntryIdPointer && |
| | 4751 | // op2_type->data.pointer.child_type == g->builtin_types.entry_u8) { |
| | 4752 | // } else { |
| | 4753 | // add_node_error(g, *op2, buf_sprintf("expected array or C string literal, found '%s'", |
| | 4754 | // buf_ptr(&op2_type->name))); |
| | 4755 | // return g->builtin_types.entry_invalid; |
| | 4756 | // } |
| | 4757 | // |
| | 4758 | // ConstExprValue *op1_val = &get_resolved_expr(*op1)->const_val; |
| | 4759 | // ConstExprValue *op2_val = &get_resolved_expr(*op2)->const_val; |
| | 4760 | // |
| | 4761 | // AstNode *bad_node; |
| | 4762 | // if (!op1_val->ok) { |
| | 4763 | // bad_node = *op1; |
| | 4764 | // } else if (!op2_val->ok) { |
| | 4765 | // bad_node = *op2; |
| | 4766 | // } else { |
| | 4767 | // bad_node = nullptr; |
| | 4768 | // } |
| | 4769 | // if (bad_node) { |
| | 4770 | // add_node_error(g, bad_node, buf_sprintf("array concatenation requires constant expression")); |
| | 4771 | // return g->builtin_types.entry_invalid; |
| | 4772 | // } |
| | 4773 | // |
| | 4774 | // ConstExprValue *const_val = &get_resolved_expr(node)->const_val; |
| | 4775 | // const_val->ok = true; |
| | 4776 | // const_val->depends_on_compile_var = op1_val->depends_on_compile_var || |
| | 4777 | // op2_val->depends_on_compile_var; |
| | 4778 | // |
| | 4779 | // if (op1_type->id == TypeTableEntryIdArray) { |
| | 4780 | // uint64_t new_len = op1_type->data.array.len + op2_type->data.array.len; |
| | 4781 | // const_val->data.x_array.fields = allocate<ConstExprValue*>(new_len); |
| | 4782 | // uint64_t next_index = 0; |
| | 4783 | // for (uint64_t i = 0; i < op1_type->data.array.len; i += 1, next_index += 1) { |
| | 4784 | // const_val->data.x_array.fields[next_index] = op1_val->data.x_array.fields[i]; |
| | 4785 | // } |
| | 4786 | // for (uint64_t i = 0; i < op2_type->data.array.len; i += 1, next_index += 1) { |
| | 4787 | // const_val->data.x_array.fields[next_index] = op2_val->data.x_array.fields[i]; |
| | 4788 | // } |
| | 4789 | // return get_array_type(g, child_type, new_len); |
| | 4790 | // } else if (op1_type->id == TypeTableEntryIdPointer) { |
| | 4791 | // if (!op1_val->data.x_ptr.is_c_str) { |
| | 4792 | // add_node_error(g, *op1, |
| | 4793 | // buf_sprintf("expected array or C string literal, found '%s'", |
| | 4794 | // buf_ptr(&op1_type->name))); |
| | 4795 | // return g->builtin_types.entry_invalid; |
| | 4796 | // } else if (!op2_val->data.x_ptr.is_c_str) { |
| | 4797 | // add_node_error(g, *op2, |
| | 4798 | // buf_sprintf("expected array or C string literal, found '%s'", |
| | 4799 | // buf_ptr(&op2_type->name))); |
| | 4800 | // return g->builtin_types.entry_invalid; |
| | 4801 | // } |
| | 4802 | // const_val->data.x_ptr.is_c_str = true; |
| | 4803 | // const_val->data.x_ptr.len = op1_val->data.x_ptr.len + op2_val->data.x_ptr.len - 1; |
| | 4804 | // const_val->data.x_ptr.ptr = allocate<ConstExprValue*>(const_val->data.x_ptr.len); |
| | 4805 | // uint64_t next_index = 0; |
| | 4806 | // for (uint64_t i = 0; i < op1_val->data.x_ptr.len - 1; i += 1, next_index += 1) { |
| | 4807 | // const_val->data.x_ptr.ptr[next_index] = op1_val->data.x_ptr.ptr[i]; |
| | 4808 | // } |
| | 4809 | // for (uint64_t i = 0; i < op2_val->data.x_ptr.len; i += 1, next_index += 1) { |
| | 4810 | // const_val->data.x_ptr.ptr[next_index] = op2_val->data.x_ptr.ptr[i]; |
| | 4811 | // } |
| | 4812 | // return op1_type; |
| | 4813 | // } else { |
| | 4814 | // zig_unreachable(); |
| | 4815 | // } |
| | 4816 | zig_panic("TODO"); |
| | 4817 | } |
| | 4818 | |
| | 4819 | static TypeTableEntry *ir_analyze_array_mult(IrAnalyze *ira, IrInstructionBinOp *instruction) { |
| | 4820 | IrInstruction *op1 = instruction->op1->other; |
| | 4821 | if (op1->type_entry->id == TypeTableEntryIdInvalid) |
| | 4822 | return ira->codegen->builtin_types.entry_invalid; |
| | 4823 | |
| | 4824 | IrInstruction *op2 = instruction->op2->other; |
| | 4825 | if (op2->type_entry->id == TypeTableEntryIdInvalid) |
| | 4826 | return ira->codegen->builtin_types.entry_invalid; |
| | 4827 | |
| | 4828 | ConstExprValue *array_val = ir_resolve_const(ira, op1); |
| | 4829 | if (!array_val) |
| | 4830 | return ira->codegen->builtin_types.entry_invalid; |
| | 4831 | |
| | 4832 | uint64_t mult_amt; |
| | 4833 | if (!ir_resolve_usize(ira, op2, &mult_amt)) |
| | 4834 | return ira->codegen->builtin_types.entry_invalid; |
| | 4835 | |
| | 4836 | TypeTableEntry *array_canon_type = get_underlying_type(op1->type_entry); |
| | 4837 | if (array_canon_type->id != TypeTableEntryIdArray) { |
| | 4838 | ir_add_error(ira, op1, buf_sprintf("expected array type, found '%s'", buf_ptr(&op1->type_entry->name))); |
| | 4839 | // TODO if meta_type is type decl, add note pointing to type decl declaration |
| | 4840 | return ira->codegen->builtin_types.entry_invalid; |
| | 4841 | } |
| | 4842 | |
| | 4843 | uint64_t old_array_len = array_canon_type->data.array.len; |
| | 4844 | |
| | 4845 | BigNum array_len; |
| | 4846 | bignum_init_unsigned(&array_len, old_array_len); |
| | 4847 | if (bignum_multiply_by_scalar(&array_len, mult_amt)) { |
| | 4848 | ir_add_error(ira, &instruction->base, buf_sprintf("operation results in overflow")); |
| | 4849 | return ira->codegen->builtin_types.entry_invalid; |
| | 4850 | } |
| | 4851 | |
| | 4852 | bool depends_on_compile_var = op1->static_value.depends_on_compile_var || op2->static_value.depends_on_compile_var; |
| | 4853 | ConstExprValue *out_val = ir_build_const_from(ira, &instruction->base, depends_on_compile_var); |
| | 4854 | |
| | 4855 | uint64_t new_array_len = array_len.data.x_uint; |
| | 4856 | out_val->data.x_array.size = new_array_len; |
| | 4857 | out_val->data.x_array.elements = allocate<ConstExprValue>(new_array_len); |
| | 4858 | |
| | 4859 | uint64_t i = 0; |
| | 4860 | for (uint64_t x = 0; x < mult_amt; x += 1) { |
| | 4861 | for (uint64_t y = 0; y < old_array_len; y += 1) { |
| | 4862 | out_val->data.x_array.elements[i] = array_val->data.x_array.elements[y]; |
| | 4863 | i += 1; |
| | 4864 | } |
| | 4865 | } |
| | 4866 | assert(i == new_array_len); |
| | 4867 | |
| | 4868 | TypeTableEntry *child_type = array_canon_type->data.array.child_type; |
| | 4869 | return get_array_type(ira->codegen, child_type, new_array_len); |
| | 4870 | } |
| 4712 | | 4871 | |
| 4713 | static TypeTableEntry *ir_analyze_instruction_bin_op(IrAnalyze *ira, IrInstructionBinOp *bin_op_instruction) { | 4872 | static TypeTableEntry *ir_analyze_instruction_bin_op(IrAnalyze *ira, IrInstructionBinOp *bin_op_instruction) { |
| 4714 | IrBinOp op_id = bin_op_instruction->op_id; | 4873 | IrBinOp op_id = bin_op_instruction->op_id; |
| ... | @@ -4741,8 +4900,9 @@ static TypeTableEntry *ir_analyze_instruction_bin_op(IrAnalyze *ira, IrInstructi | ... | @@ -4741,8 +4900,9 @@ static TypeTableEntry *ir_analyze_instruction_bin_op(IrAnalyze *ira, IrInstructi |
| 4741 | case IrBinOpMod: | 4900 | case IrBinOpMod: |
| 4742 | return ir_analyze_bin_op_math(ira, bin_op_instruction); | 4901 | return ir_analyze_bin_op_math(ira, bin_op_instruction); |
| 4743 | case IrBinOpArrayCat: | 4902 | case IrBinOpArrayCat: |
| | 4903 | return ir_analyze_array_cat(ira, bin_op_instruction); |
| 4744 | case IrBinOpArrayMult: | 4904 | case IrBinOpArrayMult: |
| 4745 | zig_panic("TODO analyze more binary operations"); | 4905 | return ir_analyze_array_mult(ira, bin_op_instruction); |
| 4746 | } | 4906 | } |
| 4747 | zig_unreachable(); | 4907 | zig_unreachable(); |
| 4748 | } | 4908 | } |
| ... | @@ -4774,7 +4934,7 @@ static TypeTableEntry *ir_analyze_instruction_decl_var(IrAnalyze *ira, IrInstruc | ... | @@ -4774,7 +4934,7 @@ static TypeTableEntry *ir_analyze_instruction_decl_var(IrAnalyze *ira, IrInstruc |
| 4774 | | 4934 | |
| 4775 | AstNode *source_node = decl_var_instruction->base.source_node; | 4935 | AstNode *source_node = decl_var_instruction->base.source_node; |
| 4776 | | 4936 | |
| 4777 | IrInstruction *casted_init_value = ir_get_casted_value(ira, init_value, explicit_type); | 4937 | IrInstruction *casted_init_value = ir_implicit_cast(ira, init_value, explicit_type); |
| 4778 | TypeTableEntry *result_type = get_underlying_type(casted_init_value->type_entry); | 4938 | TypeTableEntry *result_type = get_underlying_type(casted_init_value->type_entry); |
| 4779 | switch (result_type->id) { | 4939 | switch (result_type->id) { |
| 4780 | case TypeTableEntryIdTypeDecl: | 4940 | case TypeTableEntryIdTypeDecl: |
| ... | @@ -4859,7 +5019,7 @@ static bool ir_analyze_fn_call_inline_arg(IrAnalyze *ira, AstNode *fn_proto_node | ... | @@ -4859,7 +5019,7 @@ static bool ir_analyze_fn_call_inline_arg(IrAnalyze *ira, AstNode *fn_proto_node |
| 4859 | if (param_type->id == TypeTableEntryIdInvalid) | 5019 | if (param_type->id == TypeTableEntryIdInvalid) |
| 4860 | return false; | 5020 | return false; |
| 4861 | | 5021 | |
| 4862 | IrInstruction *casted_arg = ir_get_casted_value(ira, arg, param_type); | 5022 | IrInstruction *casted_arg = ir_implicit_cast(ira, arg, param_type); |
| 4863 | if (casted_arg->type_entry->id == TypeTableEntryIdInvalid) | 5023 | if (casted_arg->type_entry->id == TypeTableEntryIdInvalid) |
| 4864 | return false; | 5024 | return false; |
| 4865 | | 5025 | |
| ... | @@ -4893,7 +5053,7 @@ static bool ir_analyze_fn_call_generic_arg(IrAnalyze *ira, AstNode *fn_proto_nod | ... | @@ -4893,7 +5053,7 @@ static bool ir_analyze_fn_call_generic_arg(IrAnalyze *ira, AstNode *fn_proto_nod |
| 4893 | if (is_var_type) { | 5053 | if (is_var_type) { |
| 4894 | casted_arg = arg; | 5054 | casted_arg = arg; |
| 4895 | } else { | 5055 | } else { |
| 4896 | casted_arg = ir_get_casted_value(ira, arg, param_type); | 5056 | casted_arg = ir_implicit_cast(ira, arg, param_type); |
| 4897 | if (casted_arg->type_entry->id == TypeTableEntryIdInvalid) | 5057 | if (casted_arg->type_entry->id == TypeTableEntryIdInvalid) |
| 4898 | return false; | 5058 | return false; |
| 4899 | } | 5059 | } |
| ... | @@ -5098,7 +5258,7 @@ static TypeTableEntry *ir_analyze_fn_call(IrAnalyze *ira, IrInstructionCall *cal | ... | @@ -5098,7 +5258,7 @@ static TypeTableEntry *ir_analyze_fn_call(IrAnalyze *ira, IrInstructionCall *cal |
| 5098 | if (param_type->id == TypeTableEntryIdInvalid) | 5258 | if (param_type->id == TypeTableEntryIdInvalid) |
| 5099 | return ira->codegen->builtin_types.entry_invalid; | 5259 | return ira->codegen->builtin_types.entry_invalid; |
| 5100 | | 5260 | |
| 5101 | IrInstruction *casted_arg = ir_get_casted_value(ira, first_arg, param_type); | 5261 | IrInstruction *casted_arg = ir_implicit_cast(ira, first_arg, param_type); |
| 5102 | if (casted_arg->type_entry->id == TypeTableEntryIdInvalid) | 5262 | if (casted_arg->type_entry->id == TypeTableEntryIdInvalid) |
| 5103 | return ira->codegen->builtin_types.entry_invalid; | 5263 | return ira->codegen->builtin_types.entry_invalid; |
| 5104 | | 5264 | |
| ... | @@ -5114,7 +5274,7 @@ static TypeTableEntry *ir_analyze_fn_call(IrAnalyze *ira, IrInstructionCall *cal | ... | @@ -5114,7 +5274,7 @@ static TypeTableEntry *ir_analyze_fn_call(IrAnalyze *ira, IrInstructionCall *cal |
| 5114 | TypeTableEntry *param_type = fn_type_id->param_info[next_arg_index].type; | 5274 | TypeTableEntry *param_type = fn_type_id->param_info[next_arg_index].type; |
| 5115 | if (param_type->id == TypeTableEntryIdInvalid) | 5275 | if (param_type->id == TypeTableEntryIdInvalid) |
| 5116 | return ira->codegen->builtin_types.entry_invalid; | 5276 | return ira->codegen->builtin_types.entry_invalid; |
| 5117 | casted_arg = ir_get_casted_value(ira, old_arg, param_type); | 5277 | casted_arg = ir_implicit_cast(ira, old_arg, param_type); |
| 5118 | if (casted_arg->type_entry->id == TypeTableEntryIdInvalid) | 5278 | if (casted_arg->type_entry->id == TypeTableEntryIdInvalid) |
| 5119 | return ira->codegen->builtin_types.entry_invalid; | 5279 | return ira->codegen->builtin_types.entry_invalid; |
| 5120 | } else { | 5280 | } else { |
| ... | @@ -5541,7 +5701,7 @@ static TypeTableEntry *ir_analyze_instruction_cond_br(IrAnalyze *ira, IrInstruct | ... | @@ -5541,7 +5701,7 @@ static TypeTableEntry *ir_analyze_instruction_cond_br(IrAnalyze *ira, IrInstruct |
| 5541 | } | 5701 | } |
| 5542 | | 5702 | |
| 5543 | TypeTableEntry *bool_type = ira->codegen->builtin_types.entry_bool; | 5703 | TypeTableEntry *bool_type = ira->codegen->builtin_types.entry_bool; |
| 5544 | IrInstruction *casted_condition = ir_get_casted_value(ira, condition, bool_type); | 5704 | IrInstruction *casted_condition = ir_implicit_cast(ira, condition, bool_type); |
| 5545 | if (casted_condition == ira->codegen->invalid_instruction) | 5705 | if (casted_condition == ira->codegen->invalid_instruction) |
| 5546 | return ir_unreach_error(ira); | 5706 | return ir_unreach_error(ira); |
| 5547 | | 5707 | |
| ... | @@ -5624,7 +5784,7 @@ static TypeTableEntry *ir_analyze_instruction_phi(IrAnalyze *ira, IrInstructionP | ... | @@ -5624,7 +5784,7 @@ static TypeTableEntry *ir_analyze_instruction_phi(IrAnalyze *ira, IrInstructionP |
| 5624 | // cast all literal values to the resolved type | 5784 | // cast all literal values to the resolved type |
| 5625 | for (size_t i = 0; i < new_incoming_values.length; i += 1) { | 5785 | for (size_t i = 0; i < new_incoming_values.length; i += 1) { |
| 5626 | IrInstruction *new_value = new_incoming_values.at(i); | 5786 | IrInstruction *new_value = new_incoming_values.at(i); |
| 5627 | IrInstruction *casted_value = ir_get_casted_value(ira, new_value, resolved_type); | 5787 | IrInstruction *casted_value = ir_implicit_cast(ira, new_value, resolved_type); |
| 5628 | new_incoming_values.items[i] = casted_value; | 5788 | new_incoming_values.items[i] = casted_value; |
| 5629 | } | 5789 | } |
| 5630 | | 5790 | |
| ... | @@ -5700,7 +5860,7 @@ static TypeTableEntry *ir_analyze_instruction_elem_ptr(IrAnalyze *ira, IrInstruc | ... | @@ -5700,7 +5860,7 @@ static TypeTableEntry *ir_analyze_instruction_elem_ptr(IrAnalyze *ira, IrInstruc |
| 5700 | } | 5860 | } |
| 5701 | | 5861 | |
| 5702 | TypeTableEntry *usize = ira->codegen->builtin_types.entry_usize; | 5862 | TypeTableEntry *usize = ira->codegen->builtin_types.entry_usize; |
| 5703 | IrInstruction *casted_elem_index = ir_get_casted_value(ira, elem_index, usize); | 5863 | IrInstruction *casted_elem_index = ir_implicit_cast(ira, elem_index, usize); |
| 5704 | if (casted_elem_index == ira->codegen->invalid_instruction) | 5864 | if (casted_elem_index == ira->codegen->invalid_instruction) |
| 5705 | return ira->codegen->builtin_types.entry_invalid; | 5865 | return ira->codegen->builtin_types.entry_invalid; |
| 5706 | | 5866 | |
| ... | @@ -6076,7 +6236,7 @@ static TypeTableEntry *ir_analyze_instruction_store_ptr(IrAnalyze *ira, IrInstru | ... | @@ -6076,7 +6236,7 @@ static TypeTableEntry *ir_analyze_instruction_store_ptr(IrAnalyze *ira, IrInstru |
| 6076 | return value->type_entry; | 6236 | return value->type_entry; |
| 6077 | | 6237 | |
| 6078 | TypeTableEntry *child_type = ptr->type_entry->data.pointer.child_type; | 6238 | TypeTableEntry *child_type = ptr->type_entry->data.pointer.child_type; |
| 6079 | IrInstruction *casted_value = ir_get_casted_value(ira, value, child_type); | 6239 | IrInstruction *casted_value = ir_implicit_cast(ira, value, child_type); |
| 6080 | if (casted_value == ira->codegen->invalid_instruction) | 6240 | if (casted_value == ira->codegen->invalid_instruction) |
| 6081 | return ira->codegen->builtin_types.entry_invalid; | 6241 | return ira->codegen->builtin_types.entry_invalid; |
| 6082 | | 6242 | |
| ... | @@ -6717,7 +6877,7 @@ static TypeTableEntry *ir_analyze_instruction_switch_br(IrAnalyze *ira, | ... | @@ -6717,7 +6877,7 @@ static TypeTableEntry *ir_analyze_instruction_switch_br(IrAnalyze *ira, |
| 6717 | if (case_value->type_entry->id == TypeTableEntryIdInvalid) | 6877 | if (case_value->type_entry->id == TypeTableEntryIdInvalid) |
| 6718 | return ir_unreach_error(ira); | 6878 | return ir_unreach_error(ira); |
| 6719 | | 6879 | |
| 6720 | IrInstruction *casted_case_value = ir_get_casted_value(ira, case_value, target_value->type_entry); | 6880 | IrInstruction *casted_case_value = ir_implicit_cast(ira, case_value, target_value->type_entry); |
| 6721 | if (casted_case_value->type_entry->id == TypeTableEntryIdInvalid) | 6881 | if (casted_case_value->type_entry->id == TypeTableEntryIdInvalid) |
| 6722 | return ir_unreach_error(ira); | 6882 | return ir_unreach_error(ira); |
| 6723 | | 6883 | |
| ... | @@ -6751,7 +6911,7 @@ static TypeTableEntry *ir_analyze_instruction_switch_br(IrAnalyze *ira, | ... | @@ -6751,7 +6911,7 @@ static TypeTableEntry *ir_analyze_instruction_switch_br(IrAnalyze *ira, |
| 6751 | if (new_value->type_entry->id == TypeTableEntryIdInvalid) | 6911 | if (new_value->type_entry->id == TypeTableEntryIdInvalid) |
| 6752 | continue; | 6912 | continue; |
| 6753 | | 6913 | |
| 6754 | IrInstruction *casted_new_value = ir_get_casted_value(ira, new_value, target_value->type_entry); | 6914 | IrInstruction *casted_new_value = ir_implicit_cast(ira, new_value, target_value->type_entry); |
| 6755 | if (casted_new_value->type_entry->id == TypeTableEntryIdInvalid) | 6915 | if (casted_new_value->type_entry->id == TypeTableEntryIdInvalid) |
| 6756 | continue; | 6916 | continue; |
| 6757 | | 6917 | |
| ... | @@ -7265,7 +7425,7 @@ static TypeTableEntry *ir_analyze_instruction_err_name(IrAnalyze *ira, IrInstruc | ... | @@ -7265,7 +7425,7 @@ static TypeTableEntry *ir_analyze_instruction_err_name(IrAnalyze *ira, IrInstruc |
| 7265 | if (value->type_entry->id == TypeTableEntryIdInvalid) | 7425 | if (value->type_entry->id == TypeTableEntryIdInvalid) |
| 7266 | return ira->codegen->builtin_types.entry_invalid; | 7426 | return ira->codegen->builtin_types.entry_invalid; |
| 7267 | | 7427 | |
| 7268 | IrInstruction *casted_value = ir_get_casted_value(ira, value, value->type_entry); | 7428 | IrInstruction *casted_value = ir_implicit_cast(ira, value, value->type_entry); |
| 7269 | if (casted_value->type_entry->id == TypeTableEntryIdInvalid) | 7429 | if (casted_value->type_entry->id == TypeTableEntryIdInvalid) |
| 7270 | return ira->codegen->builtin_types.entry_invalid; | 7430 | return ira->codegen->builtin_types.entry_invalid; |
| 7271 | | 7431 | |
| ... | @@ -7493,11 +7653,11 @@ static TypeTableEntry *ir_analyze_instruction_cmpxchg(IrAnalyze *ira, IrInstruct | ... | @@ -7493,11 +7653,11 @@ static TypeTableEntry *ir_analyze_instruction_cmpxchg(IrAnalyze *ira, IrInstruct |
| 7493 | | 7653 | |
| 7494 | TypeTableEntry *child_type = ptr->type_entry->data.pointer.child_type; | 7654 | TypeTableEntry *child_type = ptr->type_entry->data.pointer.child_type; |
| 7495 | | 7655 | |
| 7496 | IrInstruction *casted_cmp_value = ir_get_casted_value(ira, cmp_value, child_type); | 7656 | IrInstruction *casted_cmp_value = ir_implicit_cast(ira, cmp_value, child_type); |
| 7497 | if (casted_cmp_value->type_entry->id == TypeTableEntryIdInvalid) | 7657 | if (casted_cmp_value->type_entry->id == TypeTableEntryIdInvalid) |
| 7498 | return ira->codegen->builtin_types.entry_invalid; | 7658 | return ira->codegen->builtin_types.entry_invalid; |
| 7499 | | 7659 | |
| 7500 | IrInstruction *casted_new_value = ir_get_casted_value(ira, new_value, child_type); | 7660 | IrInstruction *casted_new_value = ir_implicit_cast(ira, new_value, child_type); |
| 7501 | if (casted_new_value->type_entry->id == TypeTableEntryIdInvalid) | 7661 | if (casted_new_value->type_entry->id == TypeTableEntryIdInvalid) |
| 7502 | return ira->codegen->builtin_types.entry_invalid; | 7662 | return ira->codegen->builtin_types.entry_invalid; |
| 7503 | | 7663 | |
| ... | @@ -7567,11 +7727,11 @@ static TypeTableEntry *ir_analyze_instruction_div_exact(IrAnalyze *ira, IrInstru | ... | @@ -7567,11 +7727,11 @@ static TypeTableEntry *ir_analyze_instruction_div_exact(IrAnalyze *ira, IrInstru |
| 7567 | return ira->codegen->builtin_types.entry_invalid; | 7727 | return ira->codegen->builtin_types.entry_invalid; |
| 7568 | } | 7728 | } |
| 7569 | | 7729 | |
| 7570 | IrInstruction *casted_op1 = ir_get_casted_value(ira, op1, result_type); | 7730 | IrInstruction *casted_op1 = ir_implicit_cast(ira, op1, result_type); |
| 7571 | if (casted_op1->type_entry->id == TypeTableEntryIdInvalid) | 7731 | if (casted_op1->type_entry->id == TypeTableEntryIdInvalid) |
| 7572 | return ira->codegen->builtin_types.entry_invalid; | 7732 | return ira->codegen->builtin_types.entry_invalid; |
| 7573 | | 7733 | |
| 7574 | IrInstruction *casted_op2 = ir_get_casted_value(ira, op2, result_type); | 7734 | IrInstruction *casted_op2 = ir_implicit_cast(ira, op2, result_type); |
| 7575 | if (casted_op2->type_entry->id == TypeTableEntryIdInvalid) | 7735 | if (casted_op2->type_entry->id == TypeTableEntryIdInvalid) |
| 7576 | return ira->codegen->builtin_types.entry_invalid; | 7736 | return ira->codegen->builtin_types.entry_invalid; |
| 7577 | | 7737 | |
| ... | @@ -7690,7 +7850,7 @@ static TypeTableEntry *ir_analyze_instruction_bool_not(IrAnalyze *ira, IrInstruc | ... | @@ -7690,7 +7850,7 @@ static TypeTableEntry *ir_analyze_instruction_bool_not(IrAnalyze *ira, IrInstruc |
| 7690 | | 7850 | |
| 7691 | TypeTableEntry *bool_type = ira->codegen->builtin_types.entry_bool; | 7851 | TypeTableEntry *bool_type = ira->codegen->builtin_types.entry_bool; |
| 7692 | | 7852 | |
| 7693 | IrInstruction *casted_value = ir_get_casted_value(ira, value, bool_type); | 7853 | IrInstruction *casted_value = ir_implicit_cast(ira, value, bool_type); |
| 7694 | if (casted_value->type_entry->id == TypeTableEntryIdInvalid) | 7854 | if (casted_value->type_entry->id == TypeTableEntryIdInvalid) |
| 7695 | return ira->codegen->builtin_types.entry_invalid; | 7855 | return ira->codegen->builtin_types.entry_invalid; |
| 7696 | | 7856 | |
| ... | @@ -8265,89 +8425,6 @@ bool ir_has_side_effects(IrInstruction *instruction) { | ... | @@ -8265,89 +8425,6 @@ bool ir_has_side_effects(IrInstruction *instruction) { |
| 8265 | // return return_type; | 8425 | // return return_type; |
| 8266 | //} | 8426 | //} |
| 8267 | // | 8427 | // |
| 8268 | //static TypeTableEntry *analyze_array_mult(CodeGen *g, ImportTableEntry *import, BlockContext *context, | | |
| 8269 | // TypeTableEntry *expected_type, AstNode *node) | | |
| 8270 | //{ | | |
| 8271 | // assert(node->type == NodeTypeBinOpExpr); | | |
| 8272 | // assert(node->data.bin_op_expr.bin_op == BinOpTypeArrayMult); | | |
| 8273 | // | | |
| 8274 | // AstNode **op1 = node->data.bin_op_expr.op1->parent_field; | | |
| 8275 | // AstNode **op2 = node->data.bin_op_expr.op2->parent_field; | | |
| 8276 | // | | |
| 8277 | // TypeTableEntry *op1_type = analyze_expression(g, import, context, nullptr, *op1); | | |
| 8278 | // TypeTableEntry *op2_type = analyze_expression(g, import, context, nullptr, *op2); | | |
| 8279 | // | | |
| 8280 | // if (op1_type->id == TypeTableEntryIdInvalid || | | |
| 8281 | // op2_type->id == TypeTableEntryIdInvalid) | | |
| 8282 | // { | | |
| 8283 | // return g->builtin_types.entry_invalid; | | |
| 8284 | // } | | |
| 8285 | // | | |
| 8286 | // ConstExprValue *op1_val = &get_resolved_expr(*op1)->const_val; | | |
| 8287 | // ConstExprValue *op2_val = &get_resolved_expr(*op2)->const_val; | | |
| 8288 | // | | |
| 8289 | // AstNode *bad_node; | | |
| 8290 | // if (!op1_val->ok) { | | |
| 8291 | // bad_node = *op1; | | |
| 8292 | // } else if (!op2_val->ok) { | | |
| 8293 | // bad_node = *op2; | | |
| 8294 | // } else { | | |
| 8295 | // bad_node = nullptr; | | |
| 8296 | // } | | |
| 8297 | // if (bad_node) { | | |
| 8298 | // add_node_error(g, bad_node, buf_sprintf("array multiplication requires constant expression")); | | |
| 8299 | // return g->builtin_types.entry_invalid; | | |
| 8300 | // } | | |
| 8301 | // | | |
| 8302 | // if (op1_type->id != TypeTableEntryIdArray) { | | |
| 8303 | // add_node_error(g, *op1, | | |
| 8304 | // buf_sprintf("expected array type, found '%s'", buf_ptr(&op1_type->name))); | | |
| 8305 | // return g->builtin_types.entry_invalid; | | |
| 8306 | // } | | |
| 8307 | // | | |
| 8308 | // if (op2_type->id != TypeTableEntryIdNumLitInt && | | |
| 8309 | // op2_type->id != TypeTableEntryIdInt) | | |
| 8310 | // { | | |
| 8311 | // add_node_error(g, *op2, buf_sprintf("expected integer type, found '%s'", buf_ptr(&op2_type->name))); | | |
| 8312 | // return g->builtin_types.entry_invalid; | | |
| 8313 | // } | | |
| 8314 | // | | |
| 8315 | // if (op2_val->data.x_bignum.is_negative) { | | |
| 8316 | // add_node_error(g, *op2, buf_sprintf("expected positive number")); | | |
| 8317 | // return g->builtin_types.entry_invalid; | | |
| 8318 | // } | | |
| 8319 | // | | |
| 8320 | // ConstExprValue *const_val = &get_resolved_expr(node)->const_val; | | |
| 8321 | // const_val->ok = true; | | |
| 8322 | // const_val->depends_on_compile_var = op1_val->depends_on_compile_var || op2_val->depends_on_compile_var; | | |
| 8323 | // | | |
| 8324 | // TypeTableEntry *child_type = op1_type->data.array.child_type; | | |
| 8325 | // BigNum old_array_len; | | |
| 8326 | // bignum_init_unsigned(&old_array_len, op1_type->data.array.len); | | |
| 8327 | // | | |
| 8328 | // BigNum new_array_len; | | |
| 8329 | // if (bignum_mul(&new_array_len, &old_array_len, &op2_val->data.x_bignum)) { | | |
| 8330 | // add_node_error(g, node, buf_sprintf("operation results in overflow")); | | |
| 8331 | // return g->builtin_types.entry_invalid; | | |
| 8332 | // } | | |
| 8333 | // | | |
| 8334 | // uint64_t old_array_len_bare = op1_type->data.array.len; | | |
| 8335 | // uint64_t operand_amt = op2_val->data.x_bignum.data.x_uint; | | |
| 8336 | // | | |
| 8337 | // uint64_t new_array_len_bare = new_array_len.data.x_uint; | | |
| 8338 | // const_val->data.x_array.fields = allocate<ConstExprValue*>(new_array_len_bare); | | |
| 8339 | // | | |
| 8340 | // uint64_t i = 0; | | |
| 8341 | // for (uint64_t x = 0; x < operand_amt; x += 1) { | | |
| 8342 | // for (uint64_t y = 0; y < old_array_len_bare; y += 1) { | | |
| 8343 | // const_val->data.x_array.fields[i] = op1_val->data.x_array.fields[y]; | | |
| 8344 | // i += 1; | | |
| 8345 | // } | | |
| 8346 | // } | | |
| 8347 | // | | |
| 8348 | // return get_array_type(g, child_type, new_array_len_bare); | | |
| 8349 | //} | | |
| 8350 | // | | |
| 8351 | //static TypeTableEntry *analyze_unwrap_error_expr(CodeGen *g, ImportTableEntry *import, | 8428 | //static TypeTableEntry *analyze_unwrap_error_expr(CodeGen *g, ImportTableEntry *import, |
| 8352 | // BlockContext *parent_context, TypeTableEntry *expected_type, AstNode *node) | 8429 | // BlockContext *parent_context, TypeTableEntry *expected_type, AstNode *node) |
| 8353 | //{ | 8430 | //{ |