| ... | @@ -1729,13 +1729,12 @@ static IrInstruction *ir_build_memcpy_from(IrBuilder *irb, IrInstruction *old_in | ... | @@ -1729,13 +1729,12 @@ static IrInstruction *ir_build_memcpy_from(IrBuilder *irb, IrInstruction *old_in |
| 1729 | } | 1729 | } |
| 1730 | | 1730 | |
| 1731 | static IrInstruction *ir_build_slice(IrBuilder *irb, Scope *scope, AstNode *source_node, | 1731 | static IrInstruction *ir_build_slice(IrBuilder *irb, Scope *scope, AstNode *source_node, |
| 1732 | IrInstruction *ptr, IrInstruction *start, IrInstruction *end, bool is_const, bool safety_check_on) | 1732 | IrInstruction *ptr, IrInstruction *start, IrInstruction *end, bool safety_check_on) |
| 1733 | { | 1733 | { |
| 1734 | IrInstructionSlice *instruction = ir_build_instruction<IrInstructionSlice>(irb, scope, source_node); | 1734 | IrInstructionSlice *instruction = ir_build_instruction<IrInstructionSlice>(irb, scope, source_node); |
| 1735 | instruction->ptr = ptr; | 1735 | instruction->ptr = ptr; |
| 1736 | instruction->start = start; | 1736 | instruction->start = start; |
| 1737 | instruction->end = end; | 1737 | instruction->end = end; |
| 1738 | instruction->is_const = is_const; | | |
| 1739 | instruction->safety_check_on = safety_check_on; | 1738 | instruction->safety_check_on = safety_check_on; |
| 1740 | | 1739 | |
| 1741 | ir_ref_instruction(ptr, irb->current_basic_block); | 1740 | ir_ref_instruction(ptr, irb->current_basic_block); |
| ... | @@ -1746,10 +1745,10 @@ static IrInstruction *ir_build_slice(IrBuilder *irb, Scope *scope, AstNode *sour | ... | @@ -1746,10 +1745,10 @@ static IrInstruction *ir_build_slice(IrBuilder *irb, Scope *scope, AstNode *sour |
| 1746 | } | 1745 | } |
| 1747 | | 1746 | |
| 1748 | static IrInstruction *ir_build_slice_from(IrBuilder *irb, IrInstruction *old_instruction, | 1747 | static IrInstruction *ir_build_slice_from(IrBuilder *irb, IrInstruction *old_instruction, |
| 1749 | IrInstruction *ptr, IrInstruction *start, IrInstruction *end, bool is_const, bool safety_check_on) | 1748 | IrInstruction *ptr, IrInstruction *start, IrInstruction *end, bool safety_check_on) |
| 1750 | { | 1749 | { |
| 1751 | IrInstruction *new_instruction = ir_build_slice(irb, old_instruction->scope, | 1750 | IrInstruction *new_instruction = ir_build_slice(irb, old_instruction->scope, |
| 1752 | old_instruction->source_node, ptr, start, end, is_const, safety_check_on); | 1751 | old_instruction->source_node, ptr, start, end, safety_check_on); |
| 1753 | ir_link_new_instruction(new_instruction, old_instruction); | 1752 | ir_link_new_instruction(new_instruction, old_instruction); |
| 1754 | return new_instruction; | 1753 | return new_instruction; |
| 1755 | } | 1754 | } |
| ... | @@ -5439,7 +5438,7 @@ static IrInstruction *ir_gen_slice(IrBuilder *irb, Scope *scope, AstNode *node) | ... | @@ -5439,7 +5438,7 @@ static IrInstruction *ir_gen_slice(IrBuilder *irb, Scope *scope, AstNode *node) |
| 5439 | end_value = nullptr; | 5438 | end_value = nullptr; |
| 5440 | } | 5439 | } |
| 5441 | | 5440 | |
| 5442 | return ir_build_slice(irb, scope, node, ptr_value, start_value, end_value, slice_expr->is_const, true); | 5441 | return ir_build_slice(irb, scope, node, ptr_value, start_value, end_value, true); |
| 5443 | } | 5442 | } |
| 5444 | | 5443 | |
| 5445 | static IrInstruction *ir_gen_err_ok_or(IrBuilder *irb, Scope *parent_scope, AstNode *node) { | 5444 | static IrInstruction *ir_gen_err_ok_or(IrBuilder *irb, Scope *parent_scope, AstNode *node) { |
| ... | @@ -5903,6 +5902,11 @@ static bool is_slice(TypeTableEntry *type) { | ... | @@ -5903,6 +5902,11 @@ static bool is_slice(TypeTableEntry *type) { |
| 5903 | return type->id == TypeTableEntryIdStruct && type->data.structure.is_slice; | 5902 | return type->id == TypeTableEntryIdStruct && type->data.structure.is_slice; |
| 5904 | } | 5903 | } |
| 5905 | | 5904 | |
| | 5905 | static bool slice_is_const(TypeTableEntry *type) { |
| | 5906 | assert(is_slice(type)); |
| | 5907 | return type->data.structure.fields[slice_ptr_index].type_entry->data.pointer.is_const; |
| | 5908 | } |
| | 5909 | |
| 5906 | enum ImplicitCastMatchResult { | 5910 | enum ImplicitCastMatchResult { |
| 5907 | ImplicitCastMatchResultNo, | 5911 | ImplicitCastMatchResultNo, |
| 5908 | ImplicitCastMatchResultYes, | 5912 | ImplicitCastMatchResultYes, |
| ... | @@ -6778,7 +6782,7 @@ static IrInstruction *ir_analyze_array_to_slice(IrAnalyze *ira, IrInstruction *s | ... | @@ -6778,7 +6782,7 @@ static IrInstruction *ir_analyze_array_to_slice(IrAnalyze *ira, IrInstruction *s |
| 6778 | IrInstruction *array_ptr = ir_get_ref(ira, source_instr, array, true, false); | 6782 | IrInstruction *array_ptr = ir_get_ref(ira, source_instr, array, true, false); |
| 6779 | | 6783 | |
| 6780 | IrInstruction *result = ir_build_slice(&ira->new_irb, source_instr->scope, | 6784 | IrInstruction *result = ir_build_slice(&ira->new_irb, source_instr->scope, |
| 6781 | source_instr->source_node, array_ptr, start, end, false, false); | 6785 | source_instr->source_node, array_ptr, start, end, false); |
| 6782 | TypeTableEntry *child_type = array_type->data.array.child_type; | 6786 | TypeTableEntry *child_type = array_type->data.array.child_type; |
| 6783 | result->value.type = get_slice_type(ira->codegen, child_type, true); | 6787 | result->value.type = get_slice_type(ira->codegen, child_type, true); |
| 6784 | ir_add_alloca(ira, result, result->value.type); | 6788 | ir_add_alloca(ira, result, result->value.type); |
| ... | @@ -12076,17 +12080,17 @@ static TypeTableEntry *ir_analyze_instruction_slice(IrAnalyze *ira, IrInstructio | ... | @@ -12076,17 +12080,17 @@ static TypeTableEntry *ir_analyze_instruction_slice(IrAnalyze *ira, IrInstructio |
| 12076 | TypeTableEntry *return_type; | 12080 | TypeTableEntry *return_type; |
| 12077 | | 12081 | |
| 12078 | if (array_type->id == TypeTableEntryIdArray) { | 12082 | if (array_type->id == TypeTableEntryIdArray) { |
| 12079 | return_type = get_slice_type(ira->codegen, array_type->data.array.child_type, instruction->is_const); | 12083 | return_type = get_slice_type(ira->codegen, array_type->data.array.child_type, ptr_type->data.pointer.is_const); |
| 12080 | } else if (array_type->id == TypeTableEntryIdPointer) { | 12084 | } else if (array_type->id == TypeTableEntryIdPointer) { |
| 12081 | return_type = get_slice_type(ira->codegen, array_type->data.pointer.child_type, instruction->is_const); | 12085 | return_type = get_slice_type(ira->codegen, array_type->data.pointer.child_type, |
| | 12086 | array_type->data.pointer.is_const); |
| 12082 | if (!end) { | 12087 | if (!end) { |
| 12083 | ir_add_error(ira, &instruction->base, buf_sprintf("slice of pointer must include end value")); | 12088 | ir_add_error(ira, &instruction->base, buf_sprintf("slice of pointer must include end value")); |
| 12084 | return ira->codegen->builtin_types.entry_invalid; | 12089 | return ira->codegen->builtin_types.entry_invalid; |
| 12085 | } | 12090 | } |
| 12086 | } else if (is_slice(array_type)) { | 12091 | } else if (is_slice(array_type)) { |
| 12087 | return_type = get_slice_type(ira->codegen, | 12092 | TypeTableEntry *ptr_type = array_type->data.structure.fields[slice_ptr_index].type_entry; |
| 12088 | array_type->data.structure.fields[slice_ptr_index].type_entry->data.pointer.child_type, | 12093 | return_type = get_slice_type(ira->codegen, ptr_type->data.pointer.child_type, ptr_type->data.pointer.is_const); |
| 12089 | instruction->is_const); | | |
| 12090 | } else { | 12094 | } else { |
| 12091 | ir_add_error(ira, &instruction->base, | 12095 | ir_add_error(ira, &instruction->base, |
| 12092 | buf_sprintf("slice of non-array type '%s'", buf_ptr(&array_type->name))); | 12096 | buf_sprintf("slice of non-array type '%s'", buf_ptr(&array_type->name))); |
| ... | @@ -12186,7 +12190,8 @@ static TypeTableEntry *ir_analyze_instruction_slice(IrAnalyze *ira, IrInstructio | ... | @@ -12186,7 +12190,8 @@ static TypeTableEntry *ir_analyze_instruction_slice(IrAnalyze *ira, IrInstructio |
| 12186 | | 12190 | |
| 12187 | if (array_val) { | 12191 | if (array_val) { |
| 12188 | size_t index = abs_offset + start_scalar; | 12192 | size_t index = abs_offset + start_scalar; |
| 12189 | init_const_ptr_array(ira->codegen, ptr_val, array_val, index, instruction->is_const); | 12193 | bool is_const = slice_is_const(return_type); |
| | 12194 | init_const_ptr_array(ira->codegen, ptr_val, array_val, index, is_const); |
| 12190 | if (array_type->id == TypeTableEntryIdArray) { | 12195 | if (array_type->id == TypeTableEntryIdArray) { |
| 12191 | ptr_val->data.x_ptr.mut = ptr_ptr->value.data.x_ptr.mut; | 12196 | ptr_val->data.x_ptr.mut = ptr_ptr->value.data.x_ptr.mut; |
| 12192 | } | 12197 | } |
| ... | @@ -12197,7 +12202,7 @@ static TypeTableEntry *ir_analyze_instruction_slice(IrAnalyze *ira, IrInstructio | ... | @@ -12197,7 +12202,7 @@ static TypeTableEntry *ir_analyze_instruction_slice(IrAnalyze *ira, IrInstructio |
| 12197 | zig_unreachable(); | 12202 | zig_unreachable(); |
| 12198 | case ConstPtrSpecialRef: | 12203 | case ConstPtrSpecialRef: |
| 12199 | init_const_ptr_ref(ira->codegen, ptr_val, | 12204 | init_const_ptr_ref(ira->codegen, ptr_val, |
| 12200 | parent_ptr->data.x_ptr.data.ref.pointee, instruction->is_const); | 12205 | parent_ptr->data.x_ptr.data.ref.pointee, slice_is_const(return_type)); |
| 12201 | break; | 12206 | break; |
| 12202 | case ConstPtrSpecialBaseArray: | 12207 | case ConstPtrSpecialBaseArray: |
| 12203 | zig_unreachable(); | 12208 | zig_unreachable(); |
| ... | @@ -12216,7 +12221,7 @@ static TypeTableEntry *ir_analyze_instruction_slice(IrAnalyze *ira, IrInstructio | ... | @@ -12216,7 +12221,7 @@ static TypeTableEntry *ir_analyze_instruction_slice(IrAnalyze *ira, IrInstructio |
| 12216 | } | 12221 | } |
| 12217 | | 12222 | |
| 12218 | IrInstruction *new_instruction = ir_build_slice_from(&ira->new_irb, &instruction->base, ptr_ptr, | 12223 | IrInstruction *new_instruction = ir_build_slice_from(&ira->new_irb, &instruction->base, ptr_ptr, |
| 12219 | casted_start, end, instruction->is_const, instruction->safety_check_on); | 12224 | casted_start, end, instruction->safety_check_on); |
| 12220 | ir_add_alloca(ira, new_instruction, return_type); | 12225 | ir_add_alloca(ira, new_instruction, return_type); |
| 12221 | | 12226 | |
| 12222 | return return_type; | 12227 | return return_type; |