| ... | ... | @@ -1507,7 +1507,7 @@ static IrInstruction *ir_build_un_op(IrBuilder *irb, Scope *scope, AstNode *sour |
| 1507 | 1507 | } |
| 1508 | 1508 | |
| 1509 | 1509 | static IrInstruction *ir_build_container_init_list(IrBuilder *irb, Scope *scope, AstNode *source_node, |
| 1510 | | IrInstruction *container_type, size_t item_count, IrInstruction **items, ResultLoc *result_loc) |
| 1510 | IrInstruction *container_type, size_t item_count, IrInstruction **items, IrInstruction *result_loc) |
| 1511 | 1511 | { |
| 1512 | 1512 | IrInstructionContainerInitList *container_init_list_instruction = |
| 1513 | 1513 | ir_build_instruction<IrInstructionContainerInitList>(irb, scope, source_node); |
| ... | ... | @@ -1520,13 +1520,14 @@ static IrInstruction *ir_build_container_init_list(IrBuilder *irb, Scope *scope, |
| 1520 | 1520 | for (size_t i = 0; i < item_count; i += 1) { |
| 1521 | 1521 | ir_ref_instruction(items[i], irb->current_basic_block); |
| 1522 | 1522 | } |
| 1523 | if (result_loc != nullptr) ir_ref_instruction(result_loc, irb->current_basic_block); |
| 1523 | 1524 | |
| 1524 | 1525 | return &container_init_list_instruction->base; |
| 1525 | 1526 | } |
| 1526 | 1527 | |
| 1527 | 1528 | static IrInstruction *ir_build_container_init_fields(IrBuilder *irb, Scope *scope, AstNode *source_node, |
| 1528 | 1529 | IrInstruction *container_type, size_t field_count, IrInstructionContainerInitFieldsField *fields, |
| 1529 | | ResultLoc *result_loc) |
| 1530 | IrInstruction *result_loc) |
| 1530 | 1531 | { |
| 1531 | 1532 | IrInstructionContainerInitFields *container_init_fields_instruction = |
| 1532 | 1533 | ir_build_instruction<IrInstructionContainerInitFields>(irb, scope, source_node); |
| ... | ... | @@ -1539,6 +1540,7 @@ static IrInstruction *ir_build_container_init_fields(IrBuilder *irb, Scope *scop |
| 1539 | 1540 | for (size_t i = 0; i < field_count; i += 1) { |
| 1540 | 1541 | ir_ref_instruction(fields[i].value, irb->current_basic_block); |
| 1541 | 1542 | } |
| 1543 | if (result_loc != nullptr) ir_ref_instruction(result_loc, irb->current_basic_block); |
| 1542 | 1544 | |
| 1543 | 1545 | return &container_init_fields_instruction->base; |
| 1544 | 1546 | } |
| ... | ... | @@ -5667,14 +5669,10 @@ static IrInstruction *ir_gen_prefix_op_expr(IrBuilder *irb, Scope *scope, AstNod |
| 5667 | 5669 | } |
| 5668 | 5670 | |
| 5669 | 5671 | static IrInstruction *ir_gen_container_init_expr(IrBuilder *irb, Scope *scope, AstNode *node, LVal lval, |
| 5670 | | ResultLoc *result_loc) |
| 5672 | ResultLoc *parent_result_loc) |
| 5671 | 5673 | { |
| 5672 | 5674 | assert(node->type == NodeTypeContainerInitExpr); |
| 5673 | 5675 | |
| 5674 | | if (ir_should_inline(irb->exec, scope)) { |
| 5675 | | result_loc = nullptr; |
| 5676 | | } |
| 5677 | | |
| 5678 | 5676 | AstNodeContainerInitExpr *container_init_expr = &node->data.container_init_expr; |
| 5679 | 5677 | ContainerInitKind kind = container_init_expr->kind; |
| 5680 | 5678 | |
| ... | ... | @@ -5699,13 +5697,13 @@ static IrInstruction *ir_gen_container_init_expr(IrBuilder *irb, Scope *scope, A |
| 5699 | 5697 | } |
| 5700 | 5698 | |
| 5701 | 5699 | IrInstruction *container_ptr = nullptr; |
| 5702 | | if (result_loc != nullptr) { |
| 5703 | | src_assert(result_loc->scope_elide == nullptr, node); |
| 5704 | | result_loc->scope_elide = create_elide_scope(irb->codegen, node, scope); |
| 5700 | if (!ir_should_inline(irb->exec, scope)) { |
| 5701 | src_assert(parent_result_loc->scope_elide == nullptr, node); |
| 5702 | parent_result_loc->scope_elide = create_elide_scope(irb->codegen, node, scope); |
| 5705 | 5703 | |
| 5706 | | src_assert(result_loc != nullptr, node); |
| 5707 | | container_ptr = ir_build_resolve_result(irb, &result_loc->scope_elide->base, |
| 5708 | | node, result_loc, container_type); |
| 5704 | src_assert(parent_result_loc != nullptr, node); |
| 5705 | container_ptr = ir_build_resolve_result(irb, &parent_result_loc->scope_elide->base, |
| 5706 | node, parent_result_loc, container_type); |
| 5709 | 5707 | } |
| 5710 | 5708 | |
| 5711 | 5709 | size_t field_count = container_init_expr->entries.length; |
| ... | ... | @@ -5720,14 +5718,14 @@ static IrInstruction *ir_gen_container_init_expr(IrBuilder *irb, Scope *scope, A |
| 5720 | 5718 | Scope *val_scope = scope; |
| 5721 | 5719 | ResultLoc *child_result_loc = nullptr; |
| 5722 | 5720 | if (container_ptr != nullptr) { |
| 5723 | | IrInstruction *field_ptr = ir_build_field_ptr(irb, &result_loc->scope_elide->base, expr_node, |
| 5724 | | container_ptr, name, true); |
| 5721 | IrInstruction *field_ptr = ir_build_field_ptr(irb, &parent_result_loc->scope_elide->base, |
| 5722 | expr_node, container_ptr, name, true); |
| 5725 | 5723 | ResultLocInstruction *result_loc_inst = allocate<ResultLocInstruction>(1); |
| 5726 | 5724 | result_loc_inst->base.id = ResultLocIdInstruction; |
| 5727 | 5725 | result_loc_inst->base.source_instruction = field_ptr; |
| 5728 | 5726 | ir_ref_instruction(field_ptr, irb->current_basic_block); |
| 5729 | 5727 | child_result_loc = &result_loc_inst->base; |
| 5730 | | val_scope = &result_loc->scope_elide->base; |
| 5728 | val_scope = &parent_result_loc->scope_elide->base; |
| 5731 | 5729 | } |
| 5732 | 5730 | |
| 5733 | 5731 | IrInstruction *expr_value = ir_gen_node_extra(irb, expr_node, val_scope, LValNone, |
| ... | ... | @@ -5740,9 +5738,9 @@ static IrInstruction *ir_gen_container_init_expr(IrBuilder *irb, Scope *scope, A |
| 5740 | 5738 | fields[i].source_node = entry_node; |
| 5741 | 5739 | } |
| 5742 | 5740 | IrInstruction *init_fields = ir_build_container_init_fields(irb, scope, node, container_type, |
| 5743 | | field_count, fields, result_loc); |
| 5741 | field_count, fields, container_ptr); |
| 5744 | 5742 | |
| 5745 | | return ir_lval_wrap(irb, scope, init_fields, lval, result_loc); |
| 5743 | return ir_lval_wrap(irb, scope, init_fields, lval, parent_result_loc); |
| 5746 | 5744 | } |
| 5747 | 5745 | case ContainerInitKindArray: { |
| 5748 | 5746 | size_t item_count = container_init_expr->entries.length; |
| ... | ... | @@ -5753,12 +5751,12 @@ static IrInstruction *ir_gen_container_init_expr(IrBuilder *irb, Scope *scope, A |
| 5753 | 5751 | } |
| 5754 | 5752 | |
| 5755 | 5753 | IrInstruction *container_ptr = nullptr; |
| 5756 | | if (result_loc != nullptr) { |
| 5757 | | src_assert(result_loc->scope_elide == nullptr, node); |
| 5758 | | result_loc->scope_elide = create_elide_scope(irb->codegen, node, scope); |
| 5754 | if (!ir_should_inline(irb->exec, scope)) { |
| 5755 | src_assert(parent_result_loc->scope_elide == nullptr, node); |
| 5756 | parent_result_loc->scope_elide = create_elide_scope(irb->codegen, node, scope); |
| 5759 | 5757 | |
| 5760 | | container_ptr = ir_build_resolve_result(irb, &result_loc->scope_elide->base, |
| 5761 | | node, result_loc, container_type); |
| 5758 | container_ptr = ir_build_resolve_result(irb, &parent_result_loc->scope_elide->base, |
| 5759 | node, parent_result_loc, container_type); |
| 5762 | 5760 | } |
| 5763 | 5761 | |
| 5764 | 5762 | IrInstruction **values = allocate<IrInstruction *>(item_count); |
| ... | ... | @@ -5768,15 +5766,16 @@ static IrInstruction *ir_gen_container_init_expr(IrBuilder *irb, Scope *scope, A |
| 5768 | 5766 | ResultLoc *child_result_loc = nullptr; |
| 5769 | 5767 | Scope *val_scope = scope; |
| 5770 | 5768 | if (container_ptr != nullptr) { |
| 5771 | | IrInstruction *elem_index = ir_build_const_usize(irb, &result_loc->scope_elide->base, expr_node, i); |
| 5772 | | IrInstruction *elem_ptr = ir_build_elem_ptr(irb, &result_loc->scope_elide->base, expr_node, |
| 5773 | | container_ptr, elem_index, false, PtrLenSingle, true); |
| 5769 | IrInstruction *elem_index = ir_build_const_usize(irb, &parent_result_loc->scope_elide->base, |
| 5770 | expr_node, i); |
| 5771 | IrInstruction *elem_ptr = ir_build_elem_ptr(irb, &parent_result_loc->scope_elide->base, |
| 5772 | expr_node, container_ptr, elem_index, false, PtrLenSingle, true); |
| 5774 | 5773 | ResultLocInstruction *result_loc_inst = allocate<ResultLocInstruction>(1); |
| 5775 | 5774 | result_loc_inst->base.id = ResultLocIdInstruction; |
| 5776 | 5775 | result_loc_inst->base.source_instruction = elem_ptr; |
| 5777 | 5776 | ir_ref_instruction(elem_ptr, irb->current_basic_block); |
| 5778 | 5777 | child_result_loc = &result_loc_inst->base; |
| 5779 | | val_scope = &result_loc->scope_elide->base; |
| 5778 | val_scope = &parent_result_loc->scope_elide->base; |
| 5780 | 5779 | } |
| 5781 | 5780 | |
| 5782 | 5781 | IrInstruction *expr_value = ir_gen_node_extra(irb, expr_node, val_scope, LValNone, |
| ... | ... | @@ -5787,8 +5786,8 @@ static IrInstruction *ir_gen_container_init_expr(IrBuilder *irb, Scope *scope, A |
| 5787 | 5786 | values[i] = expr_value; |
| 5788 | 5787 | } |
| 5789 | 5788 | IrInstruction *init_list = ir_build_container_init_list(irb, scope, node, container_type, |
| 5790 | | item_count, values, result_loc); |
| 5791 | | return ir_lval_wrap(irb, scope, init_list, lval, result_loc); |
| 5789 | item_count, values, container_ptr); |
| 5790 | return ir_lval_wrap(irb, scope, init_list, lval, parent_result_loc); |
| 5792 | 5791 | } |
| 5793 | 5792 | } |
| 5794 | 5793 | zig_unreachable(); |
| ... | ... | @@ -16900,6 +16899,14 @@ static IrInstruction *ir_analyze_instruction_elem_ptr(IrAnalyze *ira, IrInstruct |
| 16900 | 16899 | } |
| 16901 | 16900 | } |
| 16902 | 16901 | |
| 16902 | if (is_slice(array_type) && elem_ptr_instruction->initializing) { |
| 16903 | // we need a pointer to an element inside a slice. but we're initializing an array. |
| 16904 | // this means that the slice isn't actually pointing at anything. |
| 16905 | ir_add_error(ira, &elem_ptr_instruction->base, |
| 16906 | buf_sprintf("runtime-initialized array cannot be casted to slice type '%s'", |
| 16907 | buf_ptr(&array_type->name))); |
| 16908 | return ira->codegen->invalid_instruction; |
| 16909 | } |
| 16903 | 16910 | } else { |
| 16904 | 16911 | // runtime known element index |
| 16905 | 16912 | switch (type_requires_comptime(ira->codegen, return_type)) { |
| ... | ... | @@ -18729,7 +18736,8 @@ static IrInstruction *ir_analyze_instruction_ref(IrAnalyze *ira, IrInstructionRe |
| 18729 | 18736 | } |
| 18730 | 18737 | |
| 18731 | 18738 | static IrInstruction *ir_analyze_container_init_fields_union(IrAnalyze *ira, IrInstruction *instruction, |
| 18732 | | ZigType *container_type, size_t instr_field_count, IrInstructionContainerInitFieldsField *fields) |
| 18739 | ZigType *container_type, size_t instr_field_count, IrInstructionContainerInitFieldsField *fields, |
| 18740 | IrInstruction *old_result_loc) |
| 18733 | 18741 | { |
| 18734 | 18742 | Error err; |
| 18735 | 18743 | assert(container_type->id == ZigTypeIdUnion); |
| ... | ... | @@ -18785,20 +18793,21 @@ static IrInstruction *ir_analyze_container_init_fields_union(IrAnalyze *ira, IrI |
| 18785 | 18793 | return result; |
| 18786 | 18794 | } |
| 18787 | 18795 | |
| 18788 | | // this instruction should not get to codegen |
| 18789 | | IrInstruction *new_instruction = ir_const(ira, instruction, container_type); |
| 18790 | | // this is how we signal to EndExpr the value is not comptime known |
| 18791 | | new_instruction->value.special = ConstValSpecialRuntime; |
| 18792 | | return new_instruction; |
| 18796 | ir_assert(old_result_loc != nullptr, instruction); |
| 18797 | IrInstruction *result_loc = old_result_loc->child; |
| 18798 | if (type_is_invalid(result_loc->value.type)) |
| 18799 | return result_loc; |
| 18800 | return ir_get_deref(ira, instruction, result_loc, nullptr); |
| 18793 | 18801 | } |
| 18794 | 18802 | |
| 18795 | 18803 | static IrInstruction *ir_analyze_container_init_fields(IrAnalyze *ira, IrInstruction *instruction, |
| 18796 | 18804 | ZigType *container_type, size_t instr_field_count, IrInstructionContainerInitFieldsField *fields, |
| 18797 | | ResultLoc *result_loc_pass1) |
| 18805 | IrInstruction *old_result_loc) |
| 18798 | 18806 | { |
| 18799 | 18807 | Error err; |
| 18800 | 18808 | if (container_type->id == ZigTypeIdUnion) { |
| 18801 | | return ir_analyze_container_init_fields_union(ira, instruction, container_type, instr_field_count, fields); |
| 18809 | return ir_analyze_container_init_fields_union(ira, instruction, container_type, instr_field_count, |
| 18810 | fields, old_result_loc); |
| 18802 | 18811 | } |
| 18803 | 18812 | if (container_type->id != ZigTypeIdStruct || is_slice(container_type)) { |
| 18804 | 18813 | ir_add_error(ira, instruction, |
| ... | ... | @@ -18929,9 +18938,10 @@ static IrInstruction *ir_analyze_container_init_fields(IrAnalyze *ira, IrInstruc |
| 18929 | 18938 | return ira->codegen->invalid_instruction; |
| 18930 | 18939 | } |
| 18931 | 18940 | |
| 18932 | | IrInstruction *result_loc = ir_resolve_result(ira, instruction, result_loc_pass1, |
| 18933 | | container_type, nullptr); |
| 18934 | | if (type_is_invalid(result_loc->value.type) || instr_is_unreachable(result_loc)) |
| 18941 | |
| 18942 | ir_assert(old_result_loc != nullptr, instruction); |
| 18943 | IrInstruction *result_loc = old_result_loc->child; |
| 18944 | if (type_is_invalid(result_loc->value.type)) |
| 18935 | 18945 | return result_loc; |
| 18936 | 18946 | return ir_get_deref(ira, instruction, result_loc, nullptr); |
| 18937 | 18947 | } |
| ... | ... | @@ -19039,9 +19049,9 @@ static IrInstruction *ir_analyze_instruction_container_init_list(IrAnalyze *ira, |
| 19039 | 19049 | return ira->codegen->invalid_instruction; |
| 19040 | 19050 | } |
| 19041 | 19051 | |
| 19042 | | IrInstruction *result_loc = ir_resolve_result(ira, &instruction->base, instruction->result_loc, |
| 19043 | | fixed_size_array_type, nullptr); |
| 19044 | | if (type_is_invalid(result_loc->value.type) || instr_is_unreachable(result_loc)) |
| 19052 | ir_assert(instruction->result_loc != nullptr, &instruction->base); |
| 19053 | IrInstruction *result_loc = instruction->result_loc->child; |
| 19054 | if (type_is_invalid(result_loc->value.type)) |
| 19045 | 19055 | return result_loc; |
| 19046 | 19056 | return ir_get_deref(ira, &instruction->base, result_loc, nullptr); |
| 19047 | 19057 | } else if (container_type->id == ZigTypeIdVoid) { |