| ... | ... | @@ -1298,14 +1298,16 @@ static IrInstruction *ir_build_return_ptr(IrAnalyze *ira, IrInstruction *source_ |
| 1298 | 1298 | return &instruction->base; |
| 1299 | 1299 | } |
| 1300 | 1300 | |
| 1301 | | static IrInstruction *ir_build_elem_ptr(IrBuilder *irb, Scope *scope, AstNode *source_node, IrInstruction *array_ptr, |
| 1302 | | IrInstruction *elem_index, bool safety_check_on, PtrLen ptr_len) |
| 1301 | static IrInstruction *ir_build_elem_ptr(IrBuilder *irb, Scope *scope, AstNode *source_node, |
| 1302 | IrInstruction *array_ptr, IrInstruction *elem_index, bool safety_check_on, PtrLen ptr_len, |
| 1303 | bool initializing) |
| 1303 | 1304 | { |
| 1304 | 1305 | IrInstructionElemPtr *instruction = ir_build_instruction<IrInstructionElemPtr>(irb, scope, source_node); |
| 1305 | 1306 | instruction->array_ptr = array_ptr; |
| 1306 | 1307 | instruction->elem_index = elem_index; |
| 1307 | 1308 | instruction->safety_check_on = safety_check_on; |
| 1308 | 1309 | instruction->ptr_len = ptr_len; |
| 1310 | instruction->initializing = initializing; |
| 1309 | 1311 | |
| 1310 | 1312 | ir_ref_instruction(array_ptr, irb->current_basic_block); |
| 1311 | 1313 | ir_ref_instruction(elem_index, irb->current_basic_block); |
| ... | ... | @@ -1505,13 +1507,14 @@ static IrInstruction *ir_build_un_op(IrBuilder *irb, Scope *scope, AstNode *sour |
| 1505 | 1507 | } |
| 1506 | 1508 | |
| 1507 | 1509 | static IrInstruction *ir_build_container_init_list(IrBuilder *irb, Scope *scope, AstNode *source_node, |
| 1508 | | IrInstruction *container_type, size_t item_count, IrInstruction **items) |
| 1510 | IrInstruction *container_type, size_t item_count, IrInstruction **items, ResultLoc *result_loc) |
| 1509 | 1511 | { |
| 1510 | 1512 | IrInstructionContainerInitList *container_init_list_instruction = |
| 1511 | 1513 | ir_build_instruction<IrInstructionContainerInitList>(irb, scope, source_node); |
| 1512 | 1514 | container_init_list_instruction->container_type = container_type; |
| 1513 | 1515 | container_init_list_instruction->item_count = item_count; |
| 1514 | 1516 | container_init_list_instruction->items = items; |
| 1517 | container_init_list_instruction->result_loc = result_loc; |
| 1515 | 1518 | |
| 1516 | 1519 | ir_ref_instruction(container_type, irb->current_basic_block); |
| 1517 | 1520 | for (size_t i = 0; i < item_count; i += 1) { |
| ... | ... | @@ -1522,13 +1525,15 @@ static IrInstruction *ir_build_container_init_list(IrBuilder *irb, Scope *scope, |
| 1522 | 1525 | } |
| 1523 | 1526 | |
| 1524 | 1527 | static IrInstruction *ir_build_container_init_fields(IrBuilder *irb, Scope *scope, AstNode *source_node, |
| 1525 | | IrInstruction *container_type, size_t field_count, IrInstructionContainerInitFieldsField *fields) |
| 1528 | IrInstruction *container_type, size_t field_count, IrInstructionContainerInitFieldsField *fields, |
| 1529 | ResultLoc *result_loc) |
| 1526 | 1530 | { |
| 1527 | 1531 | IrInstructionContainerInitFields *container_init_fields_instruction = |
| 1528 | 1532 | ir_build_instruction<IrInstructionContainerInitFields>(irb, scope, source_node); |
| 1529 | 1533 | container_init_fields_instruction->container_type = container_type; |
| 1530 | 1534 | container_init_fields_instruction->field_count = field_count; |
| 1531 | 1535 | container_init_fields_instruction->fields = fields; |
| 1536 | container_init_fields_instruction->result_loc = result_loc; |
| 1532 | 1537 | |
| 1533 | 1538 | ir_ref_instruction(container_type, irb->current_basic_block); |
| 1534 | 1539 | for (size_t i = 0; i < field_count; i += 1) { |
| ... | ... | @@ -4202,7 +4207,7 @@ static IrInstruction *ir_gen_array_access(IrBuilder *irb, Scope *scope, AstNode |
| 4202 | 4207 | return subscript_instruction; |
| 4203 | 4208 | |
| 4204 | 4209 | IrInstruction *ptr_instruction = ir_build_elem_ptr(irb, scope, node, array_ref_instruction, |
| 4205 | | subscript_instruction, true, PtrLenSingle); |
| 4210 | subscript_instruction, true, PtrLenSingle, false); |
| 4206 | 4211 | if (lval == LValPtr) |
| 4207 | 4212 | return ptr_instruction; |
| 4208 | 4213 | |
| ... | ... | @@ -5734,7 +5739,8 @@ static IrInstruction *ir_gen_container_init_expr(IrBuilder *irb, Scope *scope, A |
| 5734 | 5739 | fields[i].value = expr_value; |
| 5735 | 5740 | fields[i].source_node = entry_node; |
| 5736 | 5741 | } |
| 5737 | | IrInstruction *init_fields = ir_build_container_init_fields(irb, scope, node, container_type, field_count, fields); |
| 5742 | IrInstruction *init_fields = ir_build_container_init_fields(irb, scope, node, container_type, |
| 5743 | field_count, fields, result_loc); |
| 5738 | 5744 | |
| 5739 | 5745 | return ir_lval_wrap(irb, scope, init_fields, lval, result_loc); |
| 5740 | 5746 | } |
| ... | ... | @@ -5764,7 +5770,7 @@ static IrInstruction *ir_gen_container_init_expr(IrBuilder *irb, Scope *scope, A |
| 5764 | 5770 | if (container_ptr != nullptr) { |
| 5765 | 5771 | IrInstruction *elem_index = ir_build_const_usize(irb, &result_loc->scope_elide->base, expr_node, i); |
| 5766 | 5772 | IrInstruction *elem_ptr = ir_build_elem_ptr(irb, &result_loc->scope_elide->base, expr_node, |
| 5767 | | container_ptr, elem_index, false, PtrLenSingle); |
| 5773 | container_ptr, elem_index, false, PtrLenSingle, true); |
| 5768 | 5774 | ResultLocInstruction *result_loc_inst = allocate<ResultLocInstruction>(1); |
| 5769 | 5775 | result_loc_inst->base.id = ResultLocIdInstruction; |
| 5770 | 5776 | result_loc_inst->base.source_instruction = elem_ptr; |
| ... | ... | @@ -5781,7 +5787,7 @@ static IrInstruction *ir_gen_container_init_expr(IrBuilder *irb, Scope *scope, A |
| 5781 | 5787 | values[i] = expr_value; |
| 5782 | 5788 | } |
| 5783 | 5789 | IrInstruction *init_list = ir_build_container_init_list(irb, scope, node, container_type, |
| 5784 | | item_count, values); |
| 5790 | item_count, values, result_loc); |
| 5785 | 5791 | return ir_lval_wrap(irb, scope, init_list, lval, result_loc); |
| 5786 | 5792 | } |
| 5787 | 5793 | } |
| ... | ... | @@ -6264,7 +6270,8 @@ static IrInstruction *ir_gen_for_expr(IrBuilder *irb, Scope *parent_scope, AstNo |
| 6264 | 6270 | is_comptime); |
| 6265 | 6271 | |
| 6266 | 6272 | ir_set_cursor_at_end_and_append_block(irb, body_block); |
| 6267 | | IrInstruction *elem_ptr = ir_build_elem_ptr(irb, parent_scope, node, array_val_ptr, index_val, false, PtrLenSingle); |
| 6273 | IrInstruction *elem_ptr = ir_build_elem_ptr(irb, parent_scope, node, array_val_ptr, index_val, false, |
| 6274 | PtrLenSingle, false); |
| 6268 | 6275 | // TODO make it an error to write to element variable or i variable. |
| 6269 | 6276 | Buf *elem_var_name = elem_node->data.symbol_expr.symbol; |
| 6270 | 6277 | ZigVar *elem_var = ir_create_var(irb, elem_node, parent_scope, elem_var_name, true, false, false, is_comptime); |
| ... | ... | @@ -16825,8 +16832,9 @@ static IrInstruction *ir_analyze_instruction_elem_ptr(IrAnalyze *ira, IrInstruct |
| 16825 | 16832 | } else if (is_slice(array_type)) { |
| 16826 | 16833 | ConstExprValue *ptr_field = &array_ptr_val->data.x_struct.fields[slice_ptr_index]; |
| 16827 | 16834 | if (ptr_field->data.x_ptr.special == ConstPtrSpecialHardCodedAddr) { |
| 16828 | | IrInstruction *result = ir_build_elem_ptr(&ira->new_irb, elem_ptr_instruction->base.scope, elem_ptr_instruction->base.source_node, |
| 16829 | | array_ptr, casted_elem_index, false, elem_ptr_instruction->ptr_len); |
| 16835 | IrInstruction *result = ir_build_elem_ptr(&ira->new_irb, elem_ptr_instruction->base.scope, |
| 16836 | elem_ptr_instruction->base.source_node, array_ptr, casted_elem_index, false, |
| 16837 | elem_ptr_instruction->ptr_len, true); |
| 16830 | 16838 | result->value.type = return_type; |
| 16831 | 16839 | return result; |
| 16832 | 16840 | } |
| ... | ... | @@ -16917,8 +16925,9 @@ static IrInstruction *ir_analyze_instruction_elem_ptr(IrAnalyze *ira, IrInstruct |
| 16917 | 16925 | } |
| 16918 | 16926 | } |
| 16919 | 16927 | |
| 16920 | | IrInstruction *result = ir_build_elem_ptr(&ira->new_irb, elem_ptr_instruction->base.scope, elem_ptr_instruction->base.source_node, |
| 16921 | | array_ptr, casted_elem_index, safety_check_on, elem_ptr_instruction->ptr_len); |
| 16928 | IrInstruction *result = ir_build_elem_ptr(&ira->new_irb, elem_ptr_instruction->base.scope, |
| 16929 | elem_ptr_instruction->base.source_node, array_ptr, casted_elem_index, safety_check_on, |
| 16930 | elem_ptr_instruction->ptr_len, elem_ptr_instruction->initializing); |
| 16922 | 16931 | result->value.type = return_type; |
| 16923 | 16932 | return result; |
| 16924 | 16933 | } |
| ... | ... | @@ -18784,7 +18793,8 @@ static IrInstruction *ir_analyze_container_init_fields_union(IrAnalyze *ira, IrI |
| 18784 | 18793 | } |
| 18785 | 18794 | |
| 18786 | 18795 | static IrInstruction *ir_analyze_container_init_fields(IrAnalyze *ira, IrInstruction *instruction, |
| 18787 | | ZigType *container_type, size_t instr_field_count, IrInstructionContainerInitFieldsField *fields) |
| 18796 | ZigType *container_type, size_t instr_field_count, IrInstructionContainerInitFieldsField *fields, |
| 18797 | ResultLoc *result_loc_pass1) |
| 18788 | 18798 | { |
| 18789 | 18799 | Error err; |
| 18790 | 18800 | if (container_type->id == ZigTypeIdUnion) { |
| ... | ... | @@ -18919,11 +18929,11 @@ static IrInstruction *ir_analyze_container_init_fields(IrAnalyze *ira, IrInstruc |
| 18919 | 18929 | return ira->codegen->invalid_instruction; |
| 18920 | 18930 | } |
| 18921 | 18931 | |
| 18922 | | // this instruction should not get to codegen |
| 18923 | | IrInstruction *result = ir_const(ira, instruction, container_type); |
| 18924 | | // this is how we signal to EndExpr the value is not comptime known |
| 18925 | | result->value.special = ConstValSpecialRuntime; |
| 18926 | | return result; |
| 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)) |
| 18935 | return result_loc; |
| 18936 | return ir_get_deref(ira, instruction, result_loc, nullptr); |
| 18927 | 18937 | } |
| 18928 | 18938 | |
| 18929 | 18939 | static IrInstruction *ir_analyze_instruction_container_init_list(IrAnalyze *ira, |
| ... | ... | @@ -18942,8 +18952,8 @@ static IrInstruction *ir_analyze_instruction_container_init_list(IrAnalyze *ira, |
| 18942 | 18952 | buf_sprintf("expected array type or [_], found slice")); |
| 18943 | 18953 | return ira->codegen->invalid_instruction; |
| 18944 | 18954 | } else if (container_type->id == ZigTypeIdStruct && !is_slice(container_type) && elem_count == 0) { |
| 18945 | | return ir_analyze_container_init_fields(ira, &instruction->base, container_type, |
| 18946 | | 0, nullptr); |
| 18955 | return ir_analyze_container_init_fields(ira, &instruction->base, container_type, 0, nullptr, |
| 18956 | instruction->result_loc); |
| 18947 | 18957 | } else if (container_type->id == ZigTypeIdArray) { |
| 18948 | 18958 | // array is same as slice init but we make a compile error if the length is wrong |
| 18949 | 18959 | ZigType *child_type; |
| ... | ... | @@ -19029,11 +19039,11 @@ static IrInstruction *ir_analyze_instruction_container_init_list(IrAnalyze *ira, |
| 19029 | 19039 | return ira->codegen->invalid_instruction; |
| 19030 | 19040 | } |
| 19031 | 19041 | |
| 19032 | | // this instruction should not get to codegen |
| 19033 | | IrInstruction *new_instruction = ir_const(ira, &instruction->base, fixed_size_array_type); |
| 19034 | | // this is how we signal to EndExpr the value is not comptime known |
| 19035 | | new_instruction->value.special = ConstValSpecialRuntime; |
| 19036 | | return new_instruction; |
| 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)) |
| 19045 | return result_loc; |
| 19046 | return ir_get_deref(ira, &instruction->base, result_loc, nullptr); |
| 19037 | 19047 | } else if (container_type->id == ZigTypeIdVoid) { |
| 19038 | 19048 | if (elem_count != 0) { |
| 19039 | 19049 | ir_add_error_node(ira, instruction->base.source_node, |
| ... | ... | @@ -19058,7 +19068,7 @@ static IrInstruction *ir_analyze_instruction_container_init_fields(IrAnalyze *ir |
| 19058 | 19068 | return ira->codegen->invalid_instruction; |
| 19059 | 19069 | |
| 19060 | 19070 | return ir_analyze_container_init_fields(ira, &instruction->base, container_type, |
| 19061 | | instruction->field_count, instruction->fields); |
| 19071 | instruction->field_count, instruction->fields, instruction->result_loc); |
| 19062 | 19072 | } |
| 19063 | 19073 | |
| 19064 | 19074 | static IrInstruction *ir_analyze_instruction_compile_err(IrAnalyze *ira, |