| ... | @@ -1758,11 +1758,17 @@ static IrInstruction *ir_build_optional_unwrap_ptr(IrBuilder *irb, Scope *scope, | ... | @@ -1758,11 +1758,17 @@ static IrInstruction *ir_build_optional_unwrap_ptr(IrBuilder *irb, Scope *scope, |
| 1758 | return &instruction->base; | 1758 | return &instruction->base; |
| 1759 | } | 1759 | } |
| 1760 | | 1760 | |
| 1761 | static IrInstruction *ir_build_maybe_wrap(IrBuilder *irb, Scope *scope, AstNode *source_node, IrInstruction *value) { | 1761 | static IrInstruction *ir_build_optional_wrap(IrAnalyze *ira, IrInstruction *source_instruction, ZigType *result_ty, |
| 1762 | IrInstructionOptionalWrap *instruction = ir_build_instruction<IrInstructionOptionalWrap>(irb, scope, source_node); | 1762 | IrInstruction *operand, IrInstruction *result_loc) |
| 1763 | instruction->value = value; | 1763 | { |
| | 1764 | IrInstructionOptionalWrap *instruction = ir_build_instruction<IrInstructionOptionalWrap>( |
| | 1765 | &ira->new_irb, source_instruction->scope, source_instruction->source_node); |
| | 1766 | instruction->base.value.type = result_ty; |
| | 1767 | instruction->operand = operand; |
| | 1768 | instruction->result_loc = result_loc; |
| 1764 | | 1769 | |
| 1765 | ir_ref_instruction(value, irb->current_basic_block); | 1770 | ir_ref_instruction(operand, ira->new_irb.current_basic_block); |
| | 1771 | ir_ref_instruction(result_loc, ira->new_irb.current_basic_block); |
| 1766 | | 1772 | |
| 1767 | return &instruction->base; | 1773 | return &instruction->base; |
| 1768 | } | 1774 | } |
| ... | @@ -11035,7 +11041,7 @@ static ZigFn *ir_resolve_fn(IrAnalyze *ira, IrInstruction *fn_value) { | ... | @@ -11035,7 +11041,7 @@ static ZigFn *ir_resolve_fn(IrAnalyze *ira, IrInstruction *fn_value) { |
| 11035 | } | 11041 | } |
| 11036 | | 11042 | |
| 11037 | static IrInstruction *ir_analyze_optional_wrap(IrAnalyze *ira, IrInstruction *source_instr, IrInstruction *value, | 11043 | static IrInstruction *ir_analyze_optional_wrap(IrAnalyze *ira, IrInstruction *source_instr, IrInstruction *value, |
| 11038 | ZigType *wanted_type) | 11044 | ZigType *wanted_type, ResultLoc *result_loc) |
| 11039 | { | 11045 | { |
| 11040 | assert(wanted_type->id == ZigTypeIdOptional); | 11046 | assert(wanted_type->id == ZigTypeIdOptional); |
| 11041 | | 11047 | |
| ... | @@ -11061,10 +11067,13 @@ static IrInstruction *ir_analyze_optional_wrap(IrAnalyze *ira, IrInstruction *so | ... | @@ -11061,10 +11067,13 @@ static IrInstruction *ir_analyze_optional_wrap(IrAnalyze *ira, IrInstruction *so |
| 11061 | return &const_instruction->base; | 11067 | return &const_instruction->base; |
| 11062 | } | 11068 | } |
| 11063 | | 11069 | |
| 11064 | IrInstruction *result = ir_build_maybe_wrap(&ira->new_irb, source_instr->scope, source_instr->source_node, value); | 11070 | if (result_loc == nullptr) result_loc = no_result_loc(); |
| 11065 | result->value.type = wanted_type; | 11071 | IrInstruction *result_loc_inst = ir_resolve_result(ira, source_instr, result_loc, wanted_type, nullptr); |
| | 11072 | if (type_is_invalid(result_loc_inst->value.type) || instr_is_unreachable(result_loc_inst)) { |
| | 11073 | return result_loc_inst; |
| | 11074 | } |
| | 11075 | IrInstruction *result = ir_build_optional_wrap(ira, source_instr, wanted_type, value, result_loc_inst); |
| 11066 | result->value.data.rh_maybe = RuntimeHintOptionalNonNull; | 11076 | result->value.data.rh_maybe = RuntimeHintOptionalNonNull; |
| 11067 | ir_add_alloca(ira, result, wanted_type); | | |
| 11068 | return result; | 11077 | return result; |
| 11069 | } | 11078 | } |
| 11070 | | 11079 | |
| ... | @@ -12009,12 +12018,12 @@ static IrInstruction *ir_analyze_cast(IrAnalyze *ira, IrInstruction *source_inst | ... | @@ -12009,12 +12018,12 @@ static IrInstruction *ir_analyze_cast(IrAnalyze *ira, IrInstruction *source_inst |
| 12009 | if (types_match_const_cast_only(ira, wanted_child_type, actual_type, source_node, | 12018 | if (types_match_const_cast_only(ira, wanted_child_type, actual_type, source_node, |
| 12010 | false).id == ConstCastResultIdOk) | 12019 | false).id == ConstCastResultIdOk) |
| 12011 | { | 12020 | { |
| 12012 | return ir_analyze_optional_wrap(ira, source_instr, value, wanted_type); | 12021 | return ir_analyze_optional_wrap(ira, source_instr, value, wanted_type, result_loc); |
| 12013 | } else if (actual_type->id == ZigTypeIdComptimeInt || | 12022 | } else if (actual_type->id == ZigTypeIdComptimeInt || |
| 12014 | actual_type->id == ZigTypeIdComptimeFloat) | 12023 | actual_type->id == ZigTypeIdComptimeFloat) |
| 12015 | { | 12024 | { |
| 12016 | if (ir_num_lit_fits_in_other_type(ira, value, wanted_child_type, true)) { | 12025 | if (ir_num_lit_fits_in_other_type(ira, value, wanted_child_type, true)) { |
| 12017 | return ir_analyze_optional_wrap(ira, source_instr, value, wanted_type); | 12026 | return ir_analyze_optional_wrap(ira, source_instr, value, wanted_type, result_loc); |
| 12018 | } else { | 12027 | } else { |
| 12019 | return ira->codegen->invalid_instruction; | 12028 | return ira->codegen->invalid_instruction; |
| 12020 | } | 12029 | } |
| ... | @@ -12038,7 +12047,7 @@ static IrInstruction *ir_analyze_cast(IrAnalyze *ira, IrInstruction *source_inst | ... | @@ -12038,7 +12047,7 @@ static IrInstruction *ir_analyze_cast(IrAnalyze *ira, IrInstruction *source_inst |
| 12038 | wanted_child_type); | 12047 | wanted_child_type); |
| 12039 | if (type_is_invalid(cast1->value.type)) | 12048 | if (type_is_invalid(cast1->value.type)) |
| 12040 | return ira->codegen->invalid_instruction; | 12049 | return ira->codegen->invalid_instruction; |
| 12041 | return ir_analyze_optional_wrap(ira, source_instr, cast1, wanted_type); | 12050 | return ir_analyze_optional_wrap(ira, source_instr, cast1, wanted_type, result_loc); |
| 12042 | } | 12051 | } |
| 12043 | } | 12052 | } |
| 12044 | } | 12053 | } |
| ... | @@ -24387,6 +24396,7 @@ bool ir_has_side_effects(IrInstruction *instruction) { | ... | @@ -24387,6 +24396,7 @@ bool ir_has_side_effects(IrInstruction *instruction) { |
| 24387 | case IrInstructionIdEndExpr: | 24396 | case IrInstructionIdEndExpr: |
| 24388 | case IrInstructionIdPtrOfArrayToSlice: | 24397 | case IrInstructionIdPtrOfArrayToSlice: |
| 24389 | case IrInstructionIdSliceGen: | 24398 | case IrInstructionIdSliceGen: |
| | 24399 | case IrInstructionIdOptionalWrap: |
| 24390 | return true; | 24400 | return true; |
| 24391 | | 24401 | |
| 24392 | case IrInstructionIdPhi: | 24402 | case IrInstructionIdPhi: |
| ... | @@ -24436,7 +24446,6 @@ bool ir_has_side_effects(IrInstruction *instruction) { | ... | @@ -24436,7 +24446,6 @@ bool ir_has_side_effects(IrInstruction *instruction) { |
| 24436 | case IrInstructionIdHandle: | 24446 | case IrInstructionIdHandle: |
| 24437 | case IrInstructionIdTestErr: | 24447 | case IrInstructionIdTestErr: |
| 24438 | case IrInstructionIdUnwrapErrCode: | 24448 | case IrInstructionIdUnwrapErrCode: |
| 24439 | case IrInstructionIdOptionalWrap: | | |
| 24440 | case IrInstructionIdErrWrapCode: | 24449 | case IrInstructionIdErrWrapCode: |
| 24441 | case IrInstructionIdErrWrapPayload: | 24450 | case IrInstructionIdErrWrapPayload: |
| 24442 | case IrInstructionIdFnProto: | 24451 | case IrInstructionIdFnProto: |