| ... | ... | @@ -1430,9 +1430,6 @@ fn zirDbgStmtNode(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerE |
| 1430 | 1430 | } |
| 1431 | 1431 | |
| 1432 | 1432 | fn zirDeclRef(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerError!*Inst { |
| 1433 | | const tracy = trace(@src()); |
| 1434 | | defer tracy.end(); |
| 1435 | | |
| 1436 | 1433 | const inst_data = sema.code.instructions.items(.data)[inst].pl_node; |
| 1437 | 1434 | const src = inst_data.src(); |
| 1438 | 1435 | const decl = sema.owner_decl.dependencies.entries.items[inst_data.payload_index].key; |
| ... | ... | @@ -1440,9 +1437,6 @@ fn zirDeclRef(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerError |
| 1440 | 1437 | } |
| 1441 | 1438 | |
| 1442 | 1439 | fn zirDeclVal(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerError!*Inst { |
| 1443 | | const tracy = trace(@src()); |
| 1444 | | defer tracy.end(); |
| 1445 | | |
| 1446 | 1440 | const inst_data = sema.code.instructions.items(.data)[inst].pl_node; |
| 1447 | 1441 | const src = inst_data.src(); |
| 1448 | 1442 | const decl = sema.owner_decl.dependencies.entries.items[inst_data.payload_index].key; |
| ... | ... | @@ -2571,7 +2565,10 @@ fn zirElemVal(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerError |
| 2571 | 2565 | |
| 2572 | 2566 | const bin_inst = sema.code.instructions.items(.data)[inst].bin; |
| 2573 | 2567 | const array = try sema.resolveInst(bin_inst.lhs); |
| 2574 | | const array_ptr = try sema.analyzeRef(block, sema.src, array); |
| 2568 | const array_ptr = if (array.ty.zigTypeTag() == .Pointer) |
| 2569 | array |
| 2570 | else |
| 2571 | try sema.analyzeRef(block, sema.src, array); |
| 2575 | 2572 | const elem_index = try sema.resolveInst(bin_inst.rhs); |
| 2576 | 2573 | const result_ptr = try sema.elemPtr(block, sema.src, array_ptr, elem_index, sema.src); |
| 2577 | 2574 | return sema.analyzeLoad(block, sema.src, result_ptr, sema.src); |
| ... | ... | @@ -2586,7 +2583,10 @@ fn zirElemValNode(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerE |
| 2586 | 2583 | const elem_index_src: LazySrcLoc = .{ .node_offset_array_access_index = inst_data.src_node }; |
| 2587 | 2584 | const extra = sema.code.extraData(zir.Inst.Bin, inst_data.payload_index).data; |
| 2588 | 2585 | const array = try sema.resolveInst(extra.lhs); |
| 2589 | | const array_ptr = try sema.analyzeRef(block, src, array); |
| 2586 | const array_ptr = if (array.ty.zigTypeTag() == .Pointer) |
| 2587 | array |
| 2588 | else |
| 2589 | try sema.analyzeRef(block, src, array); |
| 2590 | 2590 | const elem_index = try sema.resolveInst(extra.rhs); |
| 2591 | 2591 | const result_ptr = try sema.elemPtr(block, src, array_ptr, elem_index, elem_index_src); |
| 2592 | 2592 | return sema.analyzeLoad(block, src, result_ptr, src); |
| ... | ... | @@ -4786,37 +4786,51 @@ fn elemPtr( |
| 4786 | 4786 | elem_index: *Inst, |
| 4787 | 4787 | elem_index_src: LazySrcLoc, |
| 4788 | 4788 | ) InnerError!*Inst { |
| 4789 | | const elem_ty = switch (array_ptr.ty.zigTypeTag()) { |
| 4789 | const array_ty = switch (array_ptr.ty.zigTypeTag()) { |
| 4790 | 4790 | .Pointer => array_ptr.ty.elemType(), |
| 4791 | 4791 | else => return sema.mod.fail(&block.base, array_ptr.src, "expected pointer, found '{}'", .{array_ptr.ty}), |
| 4792 | 4792 | }; |
| 4793 | | if (!elem_ty.isIndexable()) { |
| 4794 | | return sema.mod.fail(&block.base, src, "array access of non-array type '{}'", .{elem_ty}); |
| 4793 | if (!array_ty.isIndexable()) { |
| 4794 | return sema.mod.fail(&block.base, src, "array access of non-array type '{}'", .{array_ty}); |
| 4795 | 4795 | } |
| 4796 | | |
| 4797 | | if (elem_ty.isSinglePointer() and elem_ty.elemType().zigTypeTag() == .Array) { |
| 4796 | if (array_ty.isSinglePointer() and array_ty.elemType().zigTypeTag() == .Array) { |
| 4798 | 4797 | // we have to deref the ptr operand to get the actual array pointer |
| 4799 | 4798 | const array_ptr_deref = try sema.analyzeLoad(block, src, array_ptr, array_ptr.src); |
| 4800 | | if (array_ptr_deref.value()) |array_ptr_val| { |
| 4801 | | if (elem_index.value()) |index_val| { |
| 4802 | | // Both array pointer and index are compile-time known. |
| 4803 | | const index_u64 = index_val.toUnsignedInt(); |
| 4804 | | // @intCast here because it would have been impossible to construct a value that |
| 4805 | | // required a larger index. |
| 4806 | | const elem_ptr = try array_ptr_val.elemPtr(sema.arena, @intCast(usize, index_u64)); |
| 4807 | | const pointee_type = elem_ty.elemType().elemType(); |
| 4808 | | |
| 4809 | | return sema.mod.constInst(sema.arena, src, .{ |
| 4810 | | .ty = try Type.Tag.single_const_pointer.create(sema.arena, pointee_type), |
| 4811 | | .val = elem_ptr, |
| 4812 | | }); |
| 4813 | | } |
| 4814 | | } |
| 4799 | return sema.elemPtrArray(block, src, array_ptr_deref, elem_index, elem_index_src); |
| 4800 | } |
| 4801 | if (array_ty.zigTypeTag() == .Array) { |
| 4802 | return sema.elemPtrArray(block, src, array_ptr, elem_index, elem_index_src); |
| 4815 | 4803 | } |
| 4816 | 4804 | |
| 4817 | 4805 | return sema.mod.fail(&block.base, src, "TODO implement more analyze elemptr", .{}); |
| 4818 | 4806 | } |
| 4819 | 4807 | |
| 4808 | fn elemPtrArray( |
| 4809 | sema: *Sema, |
| 4810 | block: *Scope.Block, |
| 4811 | src: LazySrcLoc, |
| 4812 | array_ptr: *Inst, |
| 4813 | elem_index: *Inst, |
| 4814 | elem_index_src: LazySrcLoc, |
| 4815 | ) InnerError!*Inst { |
| 4816 | if (array_ptr.value()) |array_ptr_val| { |
| 4817 | if (elem_index.value()) |index_val| { |
| 4818 | // Both array pointer and index are compile-time known. |
| 4819 | const index_u64 = index_val.toUnsignedInt(); |
| 4820 | // @intCast here because it would have been impossible to construct a value that |
| 4821 | // required a larger index. |
| 4822 | const elem_ptr = try array_ptr_val.elemPtr(sema.arena, @intCast(usize, index_u64)); |
| 4823 | const pointee_type = array_ptr.ty.elemType().elemType(); |
| 4824 | |
| 4825 | return sema.mod.constInst(sema.arena, src, .{ |
| 4826 | .ty = try Type.Tag.single_const_pointer.create(sema.arena, pointee_type), |
| 4827 | .val = elem_ptr, |
| 4828 | }); |
| 4829 | } |
| 4830 | } |
| 4831 | return sema.mod.fail(&block.base, src, "TODO implement more analyze elemptr for arrays", .{}); |
| 4832 | } |
| 4833 | |
| 4820 | 4834 | fn coerce( |
| 4821 | 4835 | sema: *Sema, |
| 4822 | 4836 | block: *Scope.Block, |