From 09c7d5aebc4f7fe96a89f93c0cf99cc03977ea5f Mon Sep 17 00:00:00 2001 From: Robin Voetter Date: Thu, 21 Oct 2021 16:17:21 +0200 Subject: stage2: elemPtr for slices * Restructure elemPtr a bit * New AIR instruction: slice_elem_ptr, which returns a pointer to an element of a slice * Value: adapt elemPtr to work on slices --- src/type.zig | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'src/type.zig') diff --git a/src/type.zig b/src/type.zig index fceb3c4dfd..72347f5adc 100644 --- a/src/type.zig +++ b/src/type.zig @@ -2520,6 +2520,20 @@ pub const Type = extern union { }; } + /// Returns the type of a pointer to an element. + /// Asserts that the type is a pointer, and that the element type is indexable. + /// For *[N]T, return *T + /// For [*]T, returns *T + /// For []T, returns *T + /// Handles const-ness and address spaces in particular. + pub fn elemPtrType(ptr_ty: Type, arena: *Allocator) !Type { + return try Type.ptr(arena, .{ + .pointee_type = ptr_ty.elemType2(), + .mutable = ptr_ty.ptrIsMutable(), + .@"addrspace" = ptr_ty.ptrAddressSpace(), + }); + } + fn shallowElemType(child_ty: Type) Type { return switch (child_ty.zigTypeTag()) { .Array, .Vector => child_ty.childType(), -- cgit v1.2.3