╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Value.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭ find file ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╮ ╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │ │ │http│ ssh │ │ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╯ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1773 }, 1774 .@"union" => switch (aggregate_ty.containerLayout(zcu)) { 1775 .auto => {}, 1776 .@"packed", .@"extern" => return pt.getCoerced(parent_ptr, field_ptr_ty), 1777 }, 1778 else => unreachable, 1779 } 1780 1781 // If we get here, we need to use the `.field` comptime pointer representation, because the 1782 // aggregate does not have a well-defined layout. 1783 1784 if (parent_ptr.isUndef(zcu)) return pt.undefValue(field_ptr_ty); 1785 1786 const base_ptr = try parent_ptr.canonicalizeBasePtr(.one, aggregate_ty, pt); 1787 return .fromInterned(try pt.intern(.{ .ptr = .{ 1788 .ty = field_ptr_ty.toIntern(), 1789 .base_addr = .{ .field = .{ 1790 .base = base_ptr.toIntern(), 1791 .index = field_idx, 1792 } }, 1793 .byte_offset = 0, 1794 } })); 1795 } 1796 1797 /// `orig_parent_ptr` must be either a single-pointer to an array, a slice, a many-item pointer, or a C pointer. 1798 /// Returns a pointer to the element at the specified index. 1799 /// Asserts that the layout of the pointer element type is resolved. 1800 pub fn ptrElem(orig_parent_ptr: Value, field_idx: u64, pt: Zcu.PerThread) !Value { 1801 const zcu = pt.zcu; 1802 const parent_ptr = switch (orig_parent_ptr.typeOf(zcu).ptrSize(zcu)) { 1803 .one, .many, .c => orig_parent_ptr, 1804 .slice => orig_parent_ptr.slicePtr(zcu), 1805 }; 1806 1807 const parent_ptr_ty = parent_ptr.typeOf(zcu); 1808 const result_ty = try parent_ptr_ty.elemPtrType(field_idx, pt); 1809 const elem_ty = result_ty.childType(zcu); 1810 elem_ty.assertHasLayout(zcu); 1811 1812 if (parent_ptr.isUndef(zcu)) return pt.undefValue(result_ty); 1813 1814 if (!elem_ty.comptimeOnly(zcu)) { 1815 const byte_offset = field_idx * elem_ty.abiSize(zcu); 1816 return parent_ptr.getOffsetPtr(byte_offset, result_ty, pt); 1817 } 1818 1819 // Comptime-only element type. 1820 1821 if (field_idx == 0) { 1822 return pt.getCoerced(parent_ptr, result_ty); 1823 } 1824 1825 const arr_base_ty, const arr_base_len = elem_ty.arrayBase(zcu); 1826 const base_idx = arr_base_len * field_idx; 1827 const parent_info = zcu.intern_pool.indexToKey(parent_ptr.toIntern()).ptr; 1828 switch (parent_info.base_addr) { 1829 .arr_elem => |arr_elem| { 1830 if (Value.fromInterned(arr_elem.base).typeOf(zcu).childType(zcu).toIntern() == arr_base_ty.toIntern()) { 1831 // We already have a pointer to an element of an array of this type. 1832 // Just modify the index. 1833 return .fromInterned(try pt.intern(.{ .ptr = ptr: { 1834 var new = parent_info; 1835 new.base_addr.arr_elem.index += base_idx; 1836 new.ty = result_ty.toIntern(); 1837 break :ptr new; 1838 } })); 1839 } 1840 }, 1841 else => {}, 1842 } 1843 const base_ptr = try parent_ptr.canonicalizeBasePtr(.many, arr_base_ty, pt); 1844 return .fromInterned(try pt.intern(.{ .ptr = .{ 1845 .ty = result_ty.toIntern(), 1846 .base_addr = .{ .arr_elem = .{ 1847 .base = base_ptr.toIntern(), 1848 .index = base_idx, 1849 } }, 1850 .byte_offset = 0, 1851 } })); 1852 } 1853 1854 fn canonicalizeBasePtr(base_ptr: Value, want_size: std.lang.Type.Pointer.Size, want_child: Type, pt: Zcu.PerThread) !Value { 1855 const ptr_ty = base_ptr.typeOf(pt.zcu); 1856 const ptr_info = ptr_ty.ptrInfo(pt.zcu); 1857 1858 if (ptr_info.flags.size == want_size and 1859 ptr_info.child == want_child.toIntern() and 1860 !ptr_info.flags.is_const and 1861 !ptr_info.flags.is_volatile and 1862 !ptr_info.flags.is_allowzero and 1863 ptr_info.sentinel == .none and 1864 ptr_info.flags.alignment == .none) 1865 { 1866 // Already canonical! 1867 return base_ptr; 1868 } 1869 1870 const new_ty = try pt.ptrType(.{ 1871 .child = want_child.toIntern(), 1872 .sentinel = .none, 1873 .flags = .{ 1874 .size = want_size, 1875 .alignment = .none, 1876 .is_const = false, 1877 .is_volatile = false, 1878 .is_allowzero = false, 1879 .address_space = ptr_info.flags.address_space, 1880 }, 1881 }); 1882 return pt.getCoerced(base_ptr, new_ty); 1883 } 1884 1885 pub fn getOffsetPtr(ptr_val: Value, byte_off: u64, new_ty: Type, pt: Zcu.PerThread) !Value { 1886 if (ptr_val.isUndef(pt.zcu)) return ptr_val; 1887 var ptr = pt.zcu.intern_pool.indexToKey(ptr_val.toIntern()).ptr; 1888 ptr.ty = new_ty.toIntern(); 1889 ptr.byte_offset += byte_off; 1890 return Value.fromInterned(try pt.intern(.{ .ptr = ptr })); 1891 } 1892 1893 pub const PointerDeriveStep = union(enum) { 1894 int: struct { 1895 addr: u64, 1896 ptr_ty: Type, 1897 }, 1898 nav_ptr: InternPool.Nav.Index, 1899 uav_ptr: InternPool.Key.Ptr.BaseAddr.Uav, 1900 comptime_alloc_ptr: struct { 1901 idx: InternPool.ComptimeAllocIndex, 1902 val: Value, 1903 ptr_ty: Type, 1904 }, 1905 comptime_field_ptr: Value, 1906 eu_payload_ptr: struct { 1907 parent: *PointerDeriveStep, 1908 /// This type will never be cast: it is provided for convenience. 1909 result_ptr_ty: Type, 1910 }, 1911 opt_payload_ptr: struct { 1912 parent: *PointerDeriveStep, 1913 /// This type will never be cast: it is provided for convenience. 1914 result_ptr_ty: Type, 1915 }, 1916 field_ptr: struct { 1917 parent: *PointerDeriveStep, 1918 field_idx: u32, 1919 /// This type will never be cast: it is provided for convenience. 1920 result_ptr_ty: Type, 1921 }, 1922 elem_ptr: struct { 1923 parent: *PointerDeriveStep, 1924 elem_idx: u64, 1925 /// This type will never be cast: it is provided for convenience. 1926 result_ptr_ty: Type, 1927 }, 1928 offset_and_cast: struct { 1929 parent: *PointerDeriveStep, 1930 byte_offset: u64, 1931 new_ptr_ty: Type, 1932 }, 1933 1934 pub fn ptrType(step: PointerDeriveStep, pt: Zcu.PerThread) !Type { 1935 return switch (step) { 1936 .int => |int| int.ptr_ty, 1937 .nav_ptr => |nav| try pt.navPtrType(nav), 1938 .uav_ptr => |uav| Type.fromInterned(uav.orig_ty), 1939 .comptime_alloc_ptr => |info| info.ptr_ty, 1940 .comptime_field_ptr => |val| try pt.singleConstPtrType(val.typeOf(pt.zcu)), 1941 .offset_and_cast => |oac| oac.new_ptr_ty, 1942 inline .eu_payload_ptr, .opt_payload_ptr, .field_ptr, .elem_ptr => |x| x.result_ptr_ty, 1943 }; 1944 } 1945 }; 1946 1947 /// Given a pointer value, get the sequence of steps to derive it, ideally by taking 1948 /// only field and element pointers with no casts. This can be used by codegen backends 1949 /// which prefer field/elem accesses when lowering constant pointer values. 1950 /// It is also used by the Value printing logic for pointers. 1951 pub fn pointerDerivation(ptr_val: Value, arena: Allocator, pt: Zcu.PerThread, opt_sema: ?*Sema) Allocator.Error!PointerDeriveStep { 1952 const zcu = pt.zcu; 1953 const ptr = zcu.intern_pool.indexToKey(ptr_val.toIntern()).ptr; 1954 const base_derive: PointerDeriveStep = switch (ptr.base_addr) { 1955 .int => return .{ .int = .{ 1956 .addr = ptr.byte_offset, 1957 .ptr_ty = Type.fromInterned(ptr.ty), 1958 } }, 1959 .nav => |nav| .{ .nav_ptr = nav }, 1960 .uav => |uav| base: { 1961 // A slight tweak: `orig_ty` here is sometimes not `const`, but it ought to be. 1962 // TODO: fix this in the sites interning anon decls! 1963 const const_ty = try pt.ptrType(info: { 1964 var info = Type.fromInterned(uav.orig_ty).ptrInfo(zcu); 1965 info.flags.is_const = true; 1966 break :info info; 1967 }); 1968 break :base .{ .uav_ptr = .{ 1969 .val = uav.val, 1970 .orig_ty = const_ty.toIntern(), 1971 } }; 1972 }, 1973 .comptime_alloc => |idx| base: { 1974 const sema = opt_sema.?; 1975 const alloc = sema.getComptimeAlloc(idx); 1976 const val = try alloc.val.intern(pt, arena); 1977 const ty = val.typeOf(zcu); 1978 break :base .{ .comptime_alloc_ptr = .{ 1979 .idx = idx, 1980 .val = val, 1981 .ptr_ty = try pt.ptrType(.{ 1982 .child = ty.toIntern(), 1983 .flags = .{ 1984 .alignment = alloc.alignment, 1985 }, 1986 }), 1987 } }; 1988 }, 1989 .comptime_field => |val| .{ .comptime_field_ptr = Value.fromInterned(val) }, 1990 .eu_payload => |eu_ptr| base: { 1991 const base_ptr = Value.fromInterned(eu_ptr); 1992 const base_ptr_ty = base_ptr.typeOf(zcu); 1993 const parent_step = try arena.create(PointerDeriveStep); 1994 parent_step.* = try pointerDerivation(.fromInterned(eu_ptr), arena, pt, opt_sema); 1995 break :base .{ .eu_payload_ptr = .{ 1996 .parent = parent_step, 1997 .result_ptr_ty = try pt.adjustPtrTypeChild(base_ptr_ty, base_ptr_ty.childType(zcu).errorUnionPayload(zcu)), 1998 } }; 1999 }, 2000 .opt_payload => |opt_ptr| base: { 2001 const base_ptr = Value.fromInterned(opt_ptr); 2002 const base_ptr_ty = base_ptr.typeOf(zcu); 2003 const parent_step = try arena.create(PointerDeriveStep); 2004 parent_step.* = try pointerDerivation(.fromInterned(opt_ptr), arena, pt, opt_sema); 2005 break :base .{ .opt_payload_ptr = .{ 2006 .parent = parent_step, 2007 .result_ptr_ty = try pt.adjustPtrTypeChild(base_ptr_ty, base_ptr_ty.childType(zcu).optionalChild(zcu)), 2008 } }; 2009 }, 2010 .field => |field| base: { 2011 const base_ptr = Value.fromInterned(field.base); 2012 const base_ptr_ty = try pt.ptrType(info: { 2013 var info = base_ptr.typeOf(zcu).ptrInfo(zcu); 2014 info.flags.size = .one; 2015 break :info info; 2016 }); 2017 const parent_step = try arena.create(PointerDeriveStep); 2018 parent_step.* = try pointerDerivation(base_ptr, arena, pt, opt_sema); 2019 break :base .{ .field_ptr = .{ 2020 .parent = parent_step, 2021 .field_idx = @intCast(field.index), 2022 .result_ptr_ty = try base_ptr_ty.fieldPtrType(@intCast(field.index), pt), 2023 } }; 2024 }, 2025 .arr_elem => |arr_elem| base: { 2026 const parent_step = try arena.create(PointerDeriveStep); 2027 parent_step.* = try pointerDerivation(.fromInterned(arr_elem.base), arena, pt, opt_sema); 2028 const parent_ptr_info = (try parent_step.ptrType(pt)).ptrInfo(zcu); 2029 const result_ptr_ty = try pt.ptrType(.{ 2030 .child = parent_ptr_info.child, 2031 .flags = flags: { 2032 var flags = parent_ptr_info.flags; 2033 flags.size = .one; 2034 if (flags.alignment != .none) flags.alignment = .minStrict( 2035 flags.alignment, 2036 Type.fromInterned(parent_ptr_info.child).abiAlignment(zcu), 2037 ); 2038 break :flags flags; 2039 }, 2040 }); 2041 break :base .{ .elem_ptr = .{ 2042 .parent = parent_step, 2043 .elem_idx = arr_elem.index, 2044 .result_ptr_ty = result_ptr_ty, 2045 } }; 2046 }, 2047 }; 2048 2049 if (ptr.byte_offset == 0 and ptr.ty == (try base_derive.ptrType(pt)).toIntern()) { 2050 return base_derive; 2051 } 2052 2053 const ptr_ty_info = Type.fromInterned(ptr.ty).ptrInfo(zcu); 2054 const need_child: Type = .fromInterned(ptr_ty_info.child); 2055 if (need_child.comptimeOnly(zcu) or 2056 need_child.zigTypeTag(zcu) == .@"opaque" or 2057 need_child.isSpirvRuntimeArray(zcu)) 2058 { 2059 // No refinement can happen - this pointer is presumably invalid. 2060 // Just offset it. 2061 const parent = try arena.create(PointerDeriveStep); 2062 parent.* = base_derive; 2063 return .{ .offset_and_cast = .{ 2064 .parent = parent, 2065 .byte_offset = ptr.byte_offset, 2066 .new_ptr_ty = Type.fromInterned(ptr.ty), 2067 } }; 2068 } 2069 const need_bytes = need_child.abiSize(zcu); 2070 2071 var cur_derive = base_derive; 2072 var cur_offset = ptr.byte_offset; 2073 2074 // Refine through fields and array elements as much as possible. 2075 2076 if (need_bytes > 0) while (true) { 2077 const cur_ty = (try cur_derive.ptrType(pt)).childType(zcu); 2078 if (cur_ty.toIntern() == need_child.toIntern() and cur_offset == 0) { 2079 break; 2080 } 2081 switch (cur_ty.zigTypeTag(zcu)) { 2082 .noreturn, 2083 .type, 2084 .comptime_int, 2085 .comptime_float, 2086 .null, 2087 .undefined, 2088 .enum_literal, 2089 .@"opaque", 2090 .spirv, 2091 .@"fn", 2092 .error_union, 2093 .int, 2094 .float, 2095 .bool, 2096 .void, 2097 .pointer, 2098 .error_set, 2099 .@"anyframe", 2100 .frame, 2101 .@"enum", 2102 .vector, 2103 .@"union", 2104 => break, 2105 2106 .optional => { 2107 ptr_opt: { 2108 if (!cur_ty.isPtrLikeOptional(zcu)) break :ptr_opt; 2109 if (need_child.zigTypeTag(zcu) != .pointer) break :ptr_opt; 2110 switch (need_child.ptrSize(zcu)) { 2111 .one, .many => {}, 2112 .slice, .c => break :ptr_opt, 2113 } 2114 const parent = try arena.create(PointerDeriveStep); 2115 parent.* = cur_derive; 2116 cur_derive = .{ .opt_payload_ptr = .{ 2117 .parent = parent, 2118 .result_ptr_ty = try pt.adjustPtrTypeChild(try parent.ptrType(pt), cur_ty.optionalChild(zcu)), 2119 } }; 2120 continue; 2121 } 2122 break; 2123 }, 2124 2125 .array => { 2126 const elem_ty = cur_ty.childType(zcu); 2127 const elem_size = elem_ty.abiSize(zcu); 2128 const start_idx = cur_offset / elem_size; 2129 const end_idx = (cur_offset + need_bytes + elem_size - 1) / elem_size; 2130 if (end_idx == start_idx + 1 and ptr_ty_info.flags.size == .one) { 2131 const parent = try arena.create(PointerDeriveStep); 2132 parent.* = cur_derive; 2133 cur_derive = .{ .elem_ptr = .{ 2134 .parent = parent, 2135 .elem_idx = start_idx, 2136 .result_ptr_ty = try pt.adjustPtrTypeChild(try parent.ptrType(pt), elem_ty), 2137 } }; 2138 cur_offset -= start_idx * elem_size; 2139 } else { 2140 // Go into the first element if needed, but don't go any deeper. 2141 if (start_idx > 0) { 2142 const parent = try arena.create(PointerDeriveStep); 2143 parent.* = cur_derive; 2144 cur_derive = .{ .elem_ptr = .{ 2145 .parent = parent, 2146 .elem_idx = start_idx, 2147 .result_ptr_ty = try pt.adjustPtrTypeChild(try parent.ptrType(pt), elem_ty), 2148 } }; 2149 cur_offset -= start_idx * elem_size; 2150 } 2151 break; 2152 } 2153 }, 2154 .@"struct" => switch (cur_ty.containerLayout(zcu)) { 2155 .auto, .@"packed" => break, 2156 .@"extern" => for (0..cur_ty.structFieldCount(zcu)) |field_idx| { 2157 const field_ty = cur_ty.fieldType(field_idx, zcu); 2158 const start_off = cur_ty.structFieldOffset(field_idx, zcu); 2159 const end_off = start_off + field_ty.abiSize(zcu); 2160 if (cur_offset >= start_off and cur_offset + need_bytes <= end_off) { 2161 const base_ptr_ty = try pt.ptrType(info: { 2162 var info = (try cur_derive.ptrType(pt)).ptrInfo(zcu); 2163 info.flags.size = .one; 2164 break :info info; 2165 }); 2166 const parent = try arena.create(PointerDeriveStep); 2167 parent.* = cur_derive; 2168 cur_derive = .{ .field_ptr = .{ 2169 .parent = parent, 2170 .field_idx = @intCast(field_idx), 2171 .result_ptr_ty = try base_ptr_ty.fieldPtrType(@intCast(field_idx), pt), 2172 } }; 2173 cur_offset -= start_off; 2174 break; 2175 } 2176 } else break, // pointer spans multiple fields 2177 }, 2178 } 2179 }; 2180 2181 if (cur_offset == 0) compatible: { 2182 const src_ptr_ty_info = (try cur_derive.ptrType(pt)).ptrInfo(zcu); 2183 // We allow silently doing some "coercible" pointer things. 2184 // In particular, we only give up if cv qualifiers are *removed*. 2185 if (src_ptr_ty_info.flags.is_const and !ptr_ty_info.flags.is_const) break :compatible; 2186 if (src_ptr_ty_info.flags.is_volatile and !ptr_ty_info.flags.is_volatile) break :compatible; 2187 if (src_ptr_ty_info.flags.is_allowzero and !ptr_ty_info.flags.is_allowzero) break :compatible; 2188 // Everything else has to match exactly. 2189 if (src_ptr_ty_info.child != ptr_ty_info.child) break :compatible; 2190 if (src_ptr_ty_info.sentinel != ptr_ty_info.sentinel) break :compatible; 2191 if (src_ptr_ty_info.packed_offset != ptr_ty_info.packed_offset) break :compatible; 2192 if (src_ptr_ty_info.flags.size != ptr_ty_info.flags.size) break :compatible; 2193 if (src_ptr_ty_info.flags.alignment != ptr_ty_info.flags.alignment) break :compatible; 2194 if (src_ptr_ty_info.flags.address_space != ptr_ty_info.flags.address_space) break :compatible; 2195 if (src_ptr_ty_info.flags.vector_index != ptr_ty_info.flags.vector_index) break :compatible; 2196 2197 return cur_derive; 2198 } 2199 2200 const parent = try arena.create(PointerDeriveStep); 2201 parent.* = cur_derive; 2202 return .{ .offset_and_cast = .{ 2203 .parent = parent, 2204 .byte_offset = cur_offset, 2205 .new_ptr_ty = Type.fromInterned(ptr.ty), 2206 } }; 2207 } 2208 2209 const InterpretMode = enum { 2210 /// In this mode, types are assumed to match what the compiler was built with in terms of field 2211 /// order, field types, etc. This improves compiler performance. However, it means that certain 2212 /// modifications to `std.lang` will result in compiler crashes. 2213 direct, 2214 /// In this mode, various details of the type are allowed to differ from what the compiler was built 2215 /// with. Fields are matched by name rather than index; added struct fields are ignored, and removed 2216 /// struct fields use their default value if one exists. This is slower than `.direct`, but permits 2217 /// making certain changes to `std.lang` (in particular reordering/adding/removing fields), so it is 2218 /// useful when applying breaking changes. 2219 by_name, 2220 }; 2221 const interpret_mode: InterpretMode = @field(InterpretMode, @tagName(build_options.value_interpret_mode)); 2222 2223 /// Given a `Value` representing a comptime-known value of type `T`, unwrap it into an actual `T` known to the compiler. 2224 /// This is useful for accessing `std.lang` structures received from comptime logic. 2225 pub fn interpret(val: Value, comptime T: type, pt: Zcu.PerThread) error{ OutOfMemory, UndefinedValue, TypeMismatch }!T { 2226 const zcu = pt.zcu; 2227 const io = zcu.comp.io; 2228 const ip = &zcu.intern_pool; 2229 const ty = val.typeOf(zcu); 2230 if (ty.zigTypeTag(zcu) != @typeInfo(T)) return error.TypeMismatch; 2231 if (val.isUndef(zcu)) return error.UndefinedValue; 2232 2233 return switch (@typeInfo(T)) { 2234 .type, 2235 .noreturn, 2236 .comptime_float, 2237 .comptime_int, 2238 .undefined, 2239 .null, 2240 .@"fn", 2241 .@"opaque", 2242 .spirv, 2243 .enum_literal, 2244 => comptime unreachable, // comptime-only or otherwise impossible 2245 2246 .pointer, 2247 .array, 2248 .error_union, 2249 .error_set, 2250 .frame, 2251 .@"anyframe", 2252 .vector, 2253 => comptime unreachable, // unsupported 2254 2255 .void => {}, 2256 2257 .bool => switch (val.toIntern()) { 2258 .bool_false => false, 2259 .bool_true => true, 2260 else => unreachable, 2261 }, 2262 2263 .int => switch (ip.indexToKey(val.toIntern()).int.storage) { 2264 inline .u64, .i64 => |x| std.math.cast(T, x) orelse return error.TypeMismatch, 2265 .big_int => |big| big.toInt(T) catch return error.TypeMismatch, 2266 }, 2267 2268 .float => val.toFloat(T, zcu), 2269 2270 .optional => |opt| if (val.optionalValue(zcu)) |unwrapped| 2271 try unwrapped.interpret(opt.child, pt) 2272 else 2273 null, 2274 2275 .@"enum" => switch (interpret_mode) { 2276 .direct => { 2277 const int = val.getUnsignedInt(zcu) orelse return error.TypeMismatch; 2278 return std.enums.fromInt(T, int) orelse error.TypeMismatch; 2279 }, 2280 .by_name => { 2281 const field_index = ty.enumTagFieldIndex(val, zcu) orelse return error.TypeMismatch; 2282 const field_name = ty.enumFieldName(field_index, zcu); 2283 return std.meta.stringToEnum(T, field_name.toSlice(ip)) orelse error.TypeMismatch; 2284 }, 2285 }, 2286 2287 .@"union" => |@"union"| { 2288 // No need to handle `interpret_mode`, because the `.@"enum"` handling already deals with it. 2289 const tag_val = val.unionTag(zcu) orelse return error.TypeMismatch; 2290 const tag = try tag_val.interpret(@"union".tag_type.?, pt); 2291 return switch (tag) { 2292 inline else => |tag_comptime| @unionInit( 2293 T, 2294 @tagName(tag_comptime), 2295 try val.unionPayload(zcu).interpret(@FieldType(T, @tagName(tag_comptime)), pt), 2296 ), 2297 }; 2298 }, 2299 2300 .@"struct" => |@"struct"| switch (interpret_mode) { 2301 .direct => { 2302 if (ty.structFieldCount(zcu) != @"struct".field_names.len) return error.TypeMismatch; 2303 var result: T = undefined; 2304 inline for (@"struct".field_names, @"struct".field_types, 0..) |field_name, field_type, field_idx| { 2305 const field_val = try val.fieldValue(pt, field_idx); 2306 @field(result, field_name) = try field_val.interpret(field_type, pt); 2307 } 2308 return result; 2309 }, 2310 .by_name => { 2311 const struct_obj = zcu.typeToStruct(ty) orelse return error.TypeMismatch; 2312 var result: T = undefined; 2313 inline for (@"struct".field_names, @"struct".field_types, @"struct".field_attrs) |field_name, field_type, field_attr| { 2314 const field_name_ip = try ip.getOrPutString(zcu.gpa, io, pt.tid, field_name, .no_embedded_nulls); 2315 @field(result, field_name) = if (struct_obj.nameIndex(ip, field_name_ip)) |field_idx| f: { 2316 const field_val = try val.fieldValue(pt, field_idx); 2317 break :f try field_val.interpret(field_type, pt); 2318 } else (field_attr.defaultValue(field_type) orelse return error.TypeMismatch); 2319 } 2320 return result; 2321 }, 2322 }, 2323 }; 2324 } 2325 2326 /// Given any `val` and a `Type` corresponding `@TypeOf(val)`, construct a `Value` representing it which can be used 2327 /// within the compilation. This is useful for passing `std.lang` structures in the compiler back to the compilation. 2328 /// This is the inverse of `interpret`. 2329 pub fn uninterpret(val: anytype, ty: Type, pt: Zcu.PerThread) error{ OutOfMemory, TypeMismatch }!Value { 2330 const T = @TypeOf(val); 2331 2332 const zcu = pt.zcu; 2333 const io = zcu.comp.io; 2334 const ip = &zcu.intern_pool; 2335 if (ty.zigTypeTag(zcu) != @typeInfo(T)) return error.TypeMismatch; 2336 2337 return switch (@typeInfo(T)) { 2338 .type, 2339 .noreturn, 2340 .comptime_float, 2341 .comptime_int, 2342 .undefined, 2343 .null, 2344 .@"fn", 2345 .@"opaque", 2346 .spirv, 2347 .enum_literal, 2348 => comptime unreachable, // comptime-only or otherwise impossible 2349 2350 .pointer, 2351 .array, 2352 .error_union, 2353 .error_set, 2354 .frame, 2355 .@"anyframe", 2356 .vector, 2357 => comptime unreachable, // unsupported 2358 2359 .void => .void, 2360 2361 .bool => if (val) .true else .false, 2362 2363 .int => try pt.intValue(ty, val), 2364 2365 .float => try pt.floatValue(ty, val), 2366 2367 .optional => if (val) |some| 2368 .fromInterned(try pt.intern(.{ .opt = .{ 2369 .ty = ty.toIntern(), 2370 .val = (try uninterpret(some, ty.optionalChild(zcu), pt)).toIntern(), 2371 } })) 2372 else 2373 try pt.nullValue(ty), 2374 2375 .@"enum" => switch (interpret_mode) { 2376 .direct => try pt.enumValue(ty, try uninterpret(@backingInt(val), ty.backingIntType(zcu), pt)), 2377 .by_name => { 2378 const field_name_ip = try ip.getOrPutString(zcu.gpa, io, pt.tid, @tagName(val), .no_embedded_nulls); 2379 const field_idx = ty.enumFieldIndex(field_name_ip, zcu) orelse return error.TypeMismatch; 2380 return pt.enumValueFieldIndex(ty, field_idx); 2381 }, 2382 }, 2383 2384 .@"union" => |@"union"| { 2385 // No need to handle `interpret_mode`, because the `.@"enum"` handling already deals with it. 2386 const tag: @"union".tag_type.? = val; 2387 const tag_val = try uninterpret(tag, ty.unionTagType(zcu).?, pt); 2388 const field_ty = ty.unionFieldType(tag_val, zcu) orelse return error.TypeMismatch; 2389 return switch (val) { 2390 inline else => |payload| try pt.unionValue( 2391 ty, 2392 tag_val, 2393 try uninterpret(payload, field_ty, pt), 2394 ), 2395 }; 2396 }, 2397 2398 .@"struct" => |@"struct"| switch (interpret_mode) { 2399 .direct => { 2400 if (ty.structFieldCount(zcu) != @"struct".field_names.len) return error.TypeMismatch; 2401 var field_vals: [@"struct".field_names.len]InternPool.Index = undefined; 2402 inline for (&field_vals, @"struct".field_names, 0..) |*field_val, field_name, field_idx| { 2403 const field_ty = ty.fieldType(field_idx, zcu); 2404 field_val.* = (try uninterpret(@field(val, field_name), field_ty, pt)).toIntern(); 2405 } 2406 return pt.aggregateValue(ty, &field_vals); 2407 }, 2408 .by_name => { 2409 const struct_obj = zcu.typeToStruct(ty) orelse return error.TypeMismatch; 2410 const want_fields_len = struct_obj.field_types.len; 2411 const field_vals = try zcu.gpa.alloc(InternPool.Index, want_fields_len); 2412 defer zcu.gpa.free(field_vals); 2413 @memset(field_vals, .none); 2414 inline for (@"struct".field_names) |field_name| { 2415 const field_name_ip = try ip.getOrPutString(zcu.gpa, io, pt.tid, field_name, .no_embedded_nulls); 2416 if (struct_obj.nameIndex(ip, field_name_ip)) |field_idx| { 2417 const field_ty = ty.fieldType(field_idx, zcu); 2418 field_vals[field_idx] = (try uninterpret(@field(val, field_name), field_ty, pt)).toIntern(); 2419 } 2420 } 2421 for (field_vals, 0..) |*field_val, field_idx| { 2422 if (field_val.* == .none) { 2423 const default_init = struct_obj.field_defaults.get(ip)[field_idx]; 2424 if (default_init == .none) return error.TypeMismatch; 2425 field_val.* = default_init; 2426 } 2427 } 2428 return pt.aggregateValue(ty, field_vals); 2429 }, 2430 }, 2431 }; 2432 } 2433 2434 /// Returns whether `ptr_val_a[0..elem_count]` and `ptr_val_b[0..elem_count]` overlap. 2435 /// `ptr_val_a` and `ptr_val_b` are indexable pointers (not slices) whose element types are in-memory coercible. 2436 pub fn doPointersOverlap(ptr_val_a: Value, ptr_val_b: Value, elem_count: u64, zcu: *const Zcu) bool { 2437 const ip = &zcu.intern_pool; 2438 2439 const a_elem_ty = ptr_val_a.typeOf(zcu).indexableElem(zcu); 2440 const b_elem_ty = ptr_val_b.typeOf(zcu).indexableElem(zcu); 2441 2442 const a_ptr = ip.indexToKey(ptr_val_a.toIntern()).ptr; 2443 const b_ptr = ip.indexToKey(ptr_val_b.toIntern()).ptr; 2444 2445 // If `a_elem_ty` is not comptime-only, then overlapping pointers have identical 2446 // `base_addr`, and we just need to look at the byte offset. If it *is* comptime-only, 2447 // then `base_addr` may be an `arr_elem`, and we'll have to consider the element index. 2448 if (a_elem_ty.comptimeOnly(zcu)) { 2449 assert(a_elem_ty.toIntern() == b_elem_ty.toIntern()); // IMC comptime-only types are equivalent 2450 2451 const a_base_addr: InternPool.Key.Ptr.BaseAddr, const a_idx: u64 = switch (a_ptr.base_addr) { 2452 else => .{ a_ptr.base_addr, 0 }, 2453 .arr_elem => |arr_elem| a: { 2454 const base_ptr = Value.fromInterned(arr_elem.base); 2455 const base_child_ty = base_ptr.typeOf(zcu).childType(zcu); 2456 if (base_child_ty.toIntern() == a_elem_ty.toIntern()) { 2457 // This `arr_elem` is indexing into the element type we want. 2458 const base_ptr_info = ip.indexToKey(base_ptr.toIntern()).ptr; 2459 if (base_ptr_info.byte_offset != 0) { 2460 return false; // this pointer is invalid, just let the access fail 2461 } 2462 break :a .{ base_ptr_info.base_addr, arr_elem.index }; 2463 } 2464 break :a .{ a_ptr.base_addr, 0 }; 2465 }, 2466 }; 2467 const b_base_addr: InternPool.Key.Ptr.BaseAddr, const b_idx: u64 = switch (a_ptr.base_addr) { 2468 else => .{ b_ptr.base_addr, 0 }, 2469 .arr_elem => |arr_elem| b: { 2470 const base_ptr = Value.fromInterned(arr_elem.base); 2471 const base_child_ty = base_ptr.typeOf(zcu).childType(zcu); 2472 if (base_child_ty.toIntern() == b_elem_ty.toIntern()) { 2473 // This `arr_elem` is indexing into the element type we want. 2474 const base_ptr_info = ip.indexToKey(base_ptr.toIntern()).ptr; 2475 if (base_ptr_info.byte_offset != 0) { 2476 return false; // this pointer is invalid, just let the access fail 2477 } 2478 break :b .{ base_ptr_info.base_addr, arr_elem.index }; 2479 } 2480 break :b .{ b_ptr.base_addr, 0 }; 2481 }, 2482 }; 2483 if (!std.meta.eql(a_base_addr, b_base_addr)) return false; 2484 const diff = if (a_idx >= b_idx) a_idx - b_idx else b_idx - a_idx; 2485 return diff < elem_count; 2486 } else { 2487 assert(a_elem_ty.abiSize(zcu) == b_elem_ty.abiSize(zcu)); 2488 2489 if (!std.meta.eql(a_ptr.base_addr, b_ptr.base_addr)) return false; 2490 2491 const bytes_diff = if (a_ptr.byte_offset >= b_ptr.byte_offset) 2492 a_ptr.byte_offset - b_ptr.byte_offset 2493 else 2494 b_ptr.byte_offset - a_ptr.byte_offset; 2495 2496 const need_bytes_diff = elem_count * a_elem_ty.abiSize(zcu); 2497 return bytes_diff < need_bytes_diff; 2498 } 2499 } 2500 2501 /// `lhs` and `rhs` are both scalar numeric values (int or float). 2502 /// Supports comparisons between heterogeneous types. 2503 /// If `lhs` or `rhs` is undef, returns `false`. 2504 pub fn eqlScalarNum(lhs: Value, rhs: Value, zcu: *Zcu) bool { 2505 if (lhs.isUndef(zcu)) return false; 2506 if (rhs.isUndef(zcu)) return false; 2507 2508 if (lhs.isFloat(zcu) or rhs.isFloat(zcu)) { 2509 const lhs_f128 = lhs.toFloat(f128, zcu); 2510 const rhs_f128 = rhs.toFloat(f128, zcu); 2511 return lhs_f128 == rhs_f128; 2512 } 2513 2514 if (lhs.getUnsignedInt(zcu)) |lhs_u64| { 2515 if (rhs.getUnsignedInt(zcu)) |rhs_u64| { 2516 return lhs_u64 == rhs_u64; 2517 } 2518 } 2519 2520 var lhs_bigint_space: BigIntSpace = undefined; 2521 var rhs_bigint_space: BigIntSpace = undefined; 2522 const lhs_bigint = lhs.toBigInt(&lhs_bigint_space, zcu); 2523 const rhs_bigint = rhs.toBigInt(&rhs_bigint_space, zcu); 2524 return lhs_bigint.eql(rhs_bigint); 2525 } 2526 2527 /// Asserts the value is an integer, and the destination type is ComptimeInt or Int. 2528 /// Vectors are also accepted. Vector results are reduced with AND. 2529 /// 2530 /// If provided, `vector_index` reports the first element that failed the range check. 2531 pub fn intFitsInType( 2532 val: Value, 2533 ty: Type, 2534 vector_index: ?*usize, 2535 zcu: *const Zcu, 2536 ) bool { 2537 if (ty.toIntern() == .comptime_int_type) return true; 2538 const info = ty.intInfo(zcu); 2539 switch (val.toIntern()) { 2540 .zero_usize, .zero_u8 => return true, 2541 else => switch (zcu.intern_pool.indexToKey(val.toIntern())) { 2542 .undef => return true, 2543 .@"extern", .func, .ptr => { 2544 const target = zcu.getTarget(); 2545 const ptr_bits = target.ptrBitWidth(); 2546 return switch (info.signedness) { 2547 .signed => info.bits > ptr_bits, 2548 .unsigned => info.bits >= ptr_bits, 2549 }; 2550 }, 2551 .int => |int| { 2552 var buffer: InternPool.Key.Int.Storage.BigIntSpace = undefined; 2553 const big_int = int.storage.toBigInt(&buffer); 2554 return big_int.fitsInTwosComp(info.signedness, info.bits); 2555 }, 2556 .aggregate => |aggregate| { 2557 assert(ty.zigTypeTag(zcu) == .vector); 2558 return switch (aggregate.storage) { 2559 .bytes => |bytes| for (bytes.toSlice(ty.vectorLen(zcu), &zcu.intern_pool), 0..) |byte, i| { 2560 if (byte == 0) continue; 2561 const actual_needed_bits = std.math.log2(byte) + 1 + @intFromBool(info.signedness == .signed); 2562 if (info.bits >= actual_needed_bits) continue; 2563 if (vector_index) |vi| vi.* = i; 2564 break false; 2565 } else true, 2566 .elems, .repeated_elem => for (switch (aggregate.storage) { 2567 .bytes => unreachable, 2568 .elems => |elems| elems, 2569 .repeated_elem => |elem| @as(*const [1]InternPool.Index, &elem), 2570 }, 0..) |elem, i| { 2571 if (Value.fromInterned(elem).intFitsInType(ty.scalarType(zcu), null, zcu)) continue; 2572 if (vector_index) |vi| vi.* = i; 2573 break false; 2574 } else true, 2575 }; 2576 }, 2577 else => unreachable, 2578 }, 2579 } 2580 } 2581