| ... | ... | @@ -11,115 +11,99 @@ const TypedValue = @import("TypedValue.zig"); |
| 11 | 11 | const Sema = @import("Sema.zig"); |
| 12 | 12 | const InternPool = @import("InternPool.zig"); |
| 13 | 13 | |
| 14 | | const file_struct = @This(); |
| 15 | | |
| 14 | /// Both types and values are canonically represented by a single 32-bit integer |
| 15 | /// which is an index into an `InternPool` data structure. |
| 16 | /// This struct abstracts around this storage by providing methods only |
| 17 | /// applicable to types rather than values in general. |
| 16 | 18 | pub const Type = struct { |
| 17 | | /// We are migrating towards using this for every Type object. However, many |
| 18 | | /// types are still represented the legacy way. This is indicated by using |
| 19 | | /// InternPool.Index.none. |
| 20 | 19 | ip_index: InternPool.Index, |
| 21 | 20 | |
| 22 | | /// This is the raw data, with no bookkeeping, no memory awareness, no de-duplication. |
| 23 | | /// This union takes advantage of the fact that the first page of memory |
| 24 | | /// is unmapped, giving us 4096 possible enum tags that have no payload. |
| 25 | | legacy: extern union { |
| 26 | | /// If the tag value is less than Tag.no_payload_count, then no pointer |
| 27 | | /// dereference is needed. |
| 28 | | tag_if_small_enough: Tag, |
| 29 | | ptr_otherwise: *Payload, |
| 30 | | }, |
| 31 | | |
| 32 | 21 | pub fn zigTypeTag(ty: Type, mod: *const Module) std.builtin.TypeId { |
| 33 | 22 | return ty.zigTypeTagOrPoison(mod) catch unreachable; |
| 34 | 23 | } |
| 35 | 24 | |
| 36 | 25 | pub fn zigTypeTagOrPoison(ty: Type, mod: *const Module) error{GenericPoison}!std.builtin.TypeId { |
| 37 | | switch (ty.ip_index) { |
| 38 | | .none => switch (ty.tag()) { |
| 39 | | .inferred_alloc_const, |
| 40 | | .inferred_alloc_mut, |
| 41 | | => return .Pointer, |
| 42 | | }, |
| 43 | | else => return switch (mod.intern_pool.indexToKey(ty.ip_index)) { |
| 44 | | .int_type => .Int, |
| 45 | | .ptr_type => .Pointer, |
| 46 | | .array_type => .Array, |
| 47 | | .vector_type => .Vector, |
| 48 | | .opt_type => .Optional, |
| 49 | | .error_union_type => .ErrorUnion, |
| 50 | | .error_set_type, .inferred_error_set_type => .ErrorSet, |
| 51 | | .struct_type, .anon_struct_type => .Struct, |
| 52 | | .union_type => .Union, |
| 53 | | .opaque_type => .Opaque, |
| 54 | | .enum_type => .Enum, |
| 55 | | .func_type => .Fn, |
| 56 | | .anyframe_type => .AnyFrame, |
| 57 | | .simple_type => |s| switch (s) { |
| 58 | | .f16, |
| 59 | | .f32, |
| 60 | | .f64, |
| 61 | | .f80, |
| 62 | | .f128, |
| 63 | | .c_longdouble, |
| 64 | | => .Float, |
| 26 | return switch (mod.intern_pool.indexToKey(ty.ip_index)) { |
| 27 | .int_type => .Int, |
| 28 | .ptr_type => .Pointer, |
| 29 | .array_type => .Array, |
| 30 | .vector_type => .Vector, |
| 31 | .opt_type => .Optional, |
| 32 | .error_union_type => .ErrorUnion, |
| 33 | .error_set_type, .inferred_error_set_type => .ErrorSet, |
| 34 | .struct_type, .anon_struct_type => .Struct, |
| 35 | .union_type => .Union, |
| 36 | .opaque_type => .Opaque, |
| 37 | .enum_type => .Enum, |
| 38 | .func_type => .Fn, |
| 39 | .anyframe_type => .AnyFrame, |
| 40 | .simple_type => |s| switch (s) { |
| 41 | .f16, |
| 42 | .f32, |
| 43 | .f64, |
| 44 | .f80, |
| 45 | .f128, |
| 46 | .c_longdouble, |
| 47 | => .Float, |
| 65 | 48 | |
| 66 | | .usize, |
| 67 | | .isize, |
| 68 | | .c_char, |
| 69 | | .c_short, |
| 70 | | .c_ushort, |
| 71 | | .c_int, |
| 72 | | .c_uint, |
| 73 | | .c_long, |
| 74 | | .c_ulong, |
| 75 | | .c_longlong, |
| 76 | | .c_ulonglong, |
| 77 | | => .Int, |
| 78 | | |
| 79 | | .anyopaque => .Opaque, |
| 80 | | .bool => .Bool, |
| 81 | | .void => .Void, |
| 82 | | .type => .Type, |
| 83 | | .anyerror => .ErrorSet, |
| 84 | | .comptime_int => .ComptimeInt, |
| 85 | | .comptime_float => .ComptimeFloat, |
| 86 | | .noreturn => .NoReturn, |
| 87 | | .null => .Null, |
| 88 | | .undefined => .Undefined, |
| 89 | | .enum_literal => .EnumLiteral, |
| 49 | .usize, |
| 50 | .isize, |
| 51 | .c_char, |
| 52 | .c_short, |
| 53 | .c_ushort, |
| 54 | .c_int, |
| 55 | .c_uint, |
| 56 | .c_long, |
| 57 | .c_ulong, |
| 58 | .c_longlong, |
| 59 | .c_ulonglong, |
| 60 | => .Int, |
| 61 | |
| 62 | .anyopaque => .Opaque, |
| 63 | .bool => .Bool, |
| 64 | .void => .Void, |
| 65 | .type => .Type, |
| 66 | .anyerror => .ErrorSet, |
| 67 | .comptime_int => .ComptimeInt, |
| 68 | .comptime_float => .ComptimeFloat, |
| 69 | .noreturn => .NoReturn, |
| 70 | .null => .Null, |
| 71 | .undefined => .Undefined, |
| 72 | .enum_literal => .EnumLiteral, |
| 90 | 73 | |
| 91 | | .atomic_order, |
| 92 | | .atomic_rmw_op, |
| 93 | | .calling_convention, |
| 94 | | .address_space, |
| 95 | | .float_mode, |
| 96 | | .reduce_op, |
| 97 | | .call_modifier, |
| 98 | | => .Enum, |
| 74 | .atomic_order, |
| 75 | .atomic_rmw_op, |
| 76 | .calling_convention, |
| 77 | .address_space, |
| 78 | .float_mode, |
| 79 | .reduce_op, |
| 80 | .call_modifier, |
| 81 | => .Enum, |
| 99 | 82 | |
| 100 | | .prefetch_options, |
| 101 | | .export_options, |
| 102 | | .extern_options, |
| 103 | | => .Struct, |
| 83 | .prefetch_options, |
| 84 | .export_options, |
| 85 | .extern_options, |
| 86 | => .Struct, |
| 104 | 87 | |
| 105 | | .type_info => .Union, |
| 88 | .type_info => .Union, |
| 106 | 89 | |
| 107 | | .generic_poison => return error.GenericPoison, |
| 108 | | }, |
| 90 | .generic_poison => return error.GenericPoison, |
| 109 | 91 | |
| 110 | | // values, not types |
| 111 | | .undef => unreachable, |
| 112 | | .un => unreachable, |
| 113 | | .extern_func => unreachable, |
| 114 | | .int => unreachable, |
| 115 | | .float => unreachable, |
| 116 | | .ptr => unreachable, |
| 117 | | .opt => unreachable, |
| 118 | | .enum_tag => unreachable, |
| 119 | | .simple_value => unreachable, |
| 120 | | .aggregate => unreachable, |
| 92 | .inferred_alloc_const, .inferred_alloc_mut => return .Pointer, |
| 121 | 93 | }, |
| 122 | | } |
| 94 | |
| 95 | // values, not types |
| 96 | .undef => unreachable, |
| 97 | .un => unreachable, |
| 98 | .extern_func => unreachable, |
| 99 | .int => unreachable, |
| 100 | .float => unreachable, |
| 101 | .ptr => unreachable, |
| 102 | .opt => unreachable, |
| 103 | .enum_tag => unreachable, |
| 104 | .simple_value => unreachable, |
| 105 | .aggregate => unreachable, |
| 106 | }; |
| 123 | 107 | } |
| 124 | 108 | |
| 125 | 109 | pub fn baseZigTypeTag(self: Type, mod: *Module) std.builtin.TypeId { |
| ... | ... | @@ -171,68 +155,6 @@ pub const Type = struct { |
| 171 | 155 | }; |
| 172 | 156 | } |
| 173 | 157 | |
| 174 | | pub fn initTag(comptime small_tag: Tag) Type { |
| 175 | | comptime assert(@enumToInt(small_tag) < Tag.no_payload_count); |
| 176 | | return Type{ |
| 177 | | .ip_index = .none, |
| 178 | | .legacy = .{ .tag_if_small_enough = small_tag }, |
| 179 | | }; |
| 180 | | } |
| 181 | | |
| 182 | | pub fn initPayload(payload: *Payload) Type { |
| 183 | | assert(@enumToInt(payload.tag) >= Tag.no_payload_count); |
| 184 | | return Type{ |
| 185 | | .ip_index = .none, |
| 186 | | .legacy = .{ .ptr_otherwise = payload }, |
| 187 | | }; |
| 188 | | } |
| 189 | | |
| 190 | | pub fn tag(ty: Type) Tag { |
| 191 | | assert(ty.ip_index == .none); |
| 192 | | if (@enumToInt(ty.legacy.tag_if_small_enough) < Tag.no_payload_count) { |
| 193 | | return ty.legacy.tag_if_small_enough; |
| 194 | | } else { |
| 195 | | return ty.legacy.ptr_otherwise.tag; |
| 196 | | } |
| 197 | | } |
| 198 | | |
| 199 | | /// Prefer `castTag` to this. |
| 200 | | pub fn cast(self: Type, comptime T: type) ?*T { |
| 201 | | if (self.ip_index != .none) { |
| 202 | | return null; |
| 203 | | } |
| 204 | | if (@hasField(T, "base_tag")) { |
| 205 | | return self.castTag(T.base_tag); |
| 206 | | } |
| 207 | | if (@enumToInt(self.legacy.tag_if_small_enough) < Tag.no_payload_count) { |
| 208 | | return null; |
| 209 | | } |
| 210 | | inline for (@typeInfo(Tag).Enum.fields) |field| { |
| 211 | | if (field.value < Tag.no_payload_count) |
| 212 | | continue; |
| 213 | | const t = @intToEnum(Tag, field.value); |
| 214 | | if (self.legacy.ptr_otherwise.tag == t) { |
| 215 | | if (T == t.Type()) { |
| 216 | | return @fieldParentPtr(T, "base", self.legacy.ptr_otherwise); |
| 217 | | } |
| 218 | | return null; |
| 219 | | } |
| 220 | | } |
| 221 | | unreachable; |
| 222 | | } |
| 223 | | |
| 224 | | pub fn castTag(self: Type, comptime t: Tag) ?*t.Type() { |
| 225 | | if (self.ip_index != .none) return null; |
| 226 | | |
| 227 | | if (@enumToInt(self.legacy.tag_if_small_enough) < Tag.no_payload_count) |
| 228 | | return null; |
| 229 | | |
| 230 | | if (self.legacy.ptr_otherwise.tag == t) |
| 231 | | return @fieldParentPtr(t.Type(), "base", self.legacy.ptr_otherwise); |
| 232 | | |
| 233 | | return null; |
| 234 | | } |
| 235 | | |
| 236 | 158 | /// If it is a function pointer, returns the function type. Otherwise returns null. |
| 237 | 159 | pub fn castPtrToFn(ty: Type, mod: *const Module) ?Type { |
| 238 | 160 | if (ty.zigTypeTag(mod) != .Pointer) return null; |
| ... | ... | @@ -260,8 +182,8 @@ pub const Type = struct { |
| 260 | 182 | }; |
| 261 | 183 | } |
| 262 | 184 | |
| 263 | | pub fn ptrInfoIp(ty: Type, ip: InternPool) InternPool.Key.PtrType { |
| 264 | | return switch (ip.indexToKey(ty.ip_index)) { |
| 185 | pub fn ptrInfoIp(ip: InternPool, ty: InternPool.Index) InternPool.Key.PtrType { |
| 186 | return switch (ip.indexToKey(ty)) { |
| 265 | 187 | .ptr_type => |p| p, |
| 266 | 188 | .opt_type => |child| switch (ip.indexToKey(child)) { |
| 267 | 189 | .ptr_type => |p| p, |
| ... | ... | @@ -272,135 +194,28 @@ pub const Type = struct { |
| 272 | 194 | } |
| 273 | 195 | |
| 274 | 196 | pub fn ptrInfo(ty: Type, mod: *const Module) Payload.Pointer.Data { |
| 275 | | return Payload.Pointer.Data.fromKey(ptrInfoIp(ty, mod.intern_pool)); |
| 276 | | } |
| 277 | | |
| 278 | | pub fn eql(a: Type, b: Type, mod: *Module) bool { |
| 279 | | if (a.ip_index != .none or b.ip_index != .none) { |
| 280 | | // The InternPool data structure hashes based on Key to make interned objects |
| 281 | | // unique. An Index can be treated simply as u32 value for the |
| 282 | | // purpose of Type/Value hashing and equality. |
| 283 | | return a.ip_index == b.ip_index; |
| 284 | | } |
| 285 | | // As a shortcut, if the small tags / addresses match, we're done. |
| 286 | | if (a.legacy.tag_if_small_enough == b.legacy.tag_if_small_enough) return true; |
| 287 | | |
| 288 | | switch (a.tag()) { |
| 289 | | .inferred_alloc_const, |
| 290 | | .inferred_alloc_mut, |
| 291 | | => { |
| 292 | | if (b.zigTypeTag(mod) != .Pointer) return false; |
| 293 | | |
| 294 | | const info_a = a.ptrInfo(mod); |
| 295 | | const info_b = b.ptrInfo(mod); |
| 296 | | if (!info_a.pointee_type.eql(info_b.pointee_type, mod)) |
| 297 | | return false; |
| 298 | | if (info_a.@"align" != info_b.@"align") |
| 299 | | return false; |
| 300 | | if (info_a.@"addrspace" != info_b.@"addrspace") |
| 301 | | return false; |
| 302 | | if (info_a.bit_offset != info_b.bit_offset) |
| 303 | | return false; |
| 304 | | if (info_a.host_size != info_b.host_size) |
| 305 | | return false; |
| 306 | | if (info_a.vector_index != info_b.vector_index) |
| 307 | | return false; |
| 308 | | if (info_a.@"allowzero" != info_b.@"allowzero") |
| 309 | | return false; |
| 310 | | if (info_a.mutable != info_b.mutable) |
| 311 | | return false; |
| 312 | | if (info_a.@"volatile" != info_b.@"volatile") |
| 313 | | return false; |
| 314 | | if (info_a.size != info_b.size) |
| 315 | | return false; |
| 316 | | |
| 317 | | const sentinel_a = info_a.sentinel; |
| 318 | | const sentinel_b = info_b.sentinel; |
| 319 | | if (sentinel_a) |sa| { |
| 320 | | if (sentinel_b) |sb| { |
| 321 | | if (!sa.eql(sb, info_a.pointee_type, mod)) |
| 322 | | return false; |
| 323 | | } else { |
| 324 | | return false; |
| 325 | | } |
| 326 | | } else { |
| 327 | | if (sentinel_b != null) |
| 328 | | return false; |
| 329 | | } |
| 330 | | |
| 331 | | return true; |
| 332 | | }, |
| 333 | | } |
| 197 | return Payload.Pointer.Data.fromKey(ptrInfoIp(mod.intern_pool, ty.ip_index)); |
| 334 | 198 | } |
| 335 | 199 | |
| 336 | | pub fn hash(self: Type, mod: *Module) u64 { |
| 337 | | var hasher = std.hash.Wyhash.init(0); |
| 338 | | self.hashWithHasher(&hasher, mod); |
| 339 | | return hasher.final(); |
| 200 | pub fn eql(a: Type, b: Type, mod: *const Module) bool { |
| 201 | _ = mod; // TODO: remove this parameter |
| 202 | assert(a.ip_index != .none); |
| 203 | assert(b.ip_index != .none); |
| 204 | // The InternPool data structure hashes based on Key to make interned objects |
| 205 | // unique. An Index can be treated simply as u32 value for the |
| 206 | // purpose of Type/Value hashing and equality. |
| 207 | return a.ip_index == b.ip_index; |
| 340 | 208 | } |
| 341 | 209 | |
| 342 | | pub fn hashWithHasher(ty: Type, hasher: *std.hash.Wyhash, mod: *Module) void { |
| 343 | | if (ty.ip_index != .none) { |
| 344 | | // The InternPool data structure hashes based on Key to make interned objects |
| 345 | | // unique. An Index can be treated simply as u32 value for the |
| 346 | | // purpose of Type/Value hashing and equality. |
| 347 | | std.hash.autoHash(hasher, ty.ip_index); |
| 348 | | return; |
| 349 | | } |
| 350 | | switch (ty.tag()) { |
| 351 | | .inferred_alloc_const, |
| 352 | | .inferred_alloc_mut, |
| 353 | | => { |
| 354 | | std.hash.autoHash(hasher, std.builtin.TypeId.Pointer); |
| 355 | | |
| 356 | | const info = ty.ptrInfo(mod); |
| 357 | | hashWithHasher(info.pointee_type, hasher, mod); |
| 358 | | hashSentinel(info.sentinel, info.pointee_type, hasher, mod); |
| 359 | | std.hash.autoHash(hasher, info.@"align"); |
| 360 | | std.hash.autoHash(hasher, info.@"addrspace"); |
| 361 | | std.hash.autoHash(hasher, info.bit_offset); |
| 362 | | std.hash.autoHash(hasher, info.host_size); |
| 363 | | std.hash.autoHash(hasher, info.vector_index); |
| 364 | | std.hash.autoHash(hasher, info.@"allowzero"); |
| 365 | | std.hash.autoHash(hasher, info.mutable); |
| 366 | | std.hash.autoHash(hasher, info.@"volatile"); |
| 367 | | std.hash.autoHash(hasher, info.size); |
| 368 | | }, |
| 369 | | } |
| 370 | | } |
| 371 | | |
| 372 | | fn hashSentinel(opt_val: ?Value, ty: Type, hasher: *std.hash.Wyhash, mod: *Module) void { |
| 373 | | if (opt_val) |s| { |
| 374 | | std.hash.autoHash(hasher, true); |
| 375 | | s.hash(ty, hasher, mod); |
| 376 | | } else { |
| 377 | | std.hash.autoHash(hasher, false); |
| 378 | | } |
| 210 | pub fn hash(ty: Type, mod: *const Module) u32 { |
| 211 | _ = mod; // TODO: remove this parameter |
| 212 | assert(ty.ip_index != .none); |
| 213 | // The InternPool data structure hashes based on Key to make interned objects |
| 214 | // unique. An Index can be treated simply as u32 value for the |
| 215 | // purpose of Type/Value hashing and equality. |
| 216 | return std.hash.uint32(@enumToInt(ty.ip_index)); |
| 379 | 217 | } |
| 380 | 218 | |
| 381 | | pub const HashContext64 = struct { |
| 382 | | mod: *Module, |
| 383 | | |
| 384 | | pub fn hash(self: @This(), t: Type) u64 { |
| 385 | | return t.hash(self.mod); |
| 386 | | } |
| 387 | | pub fn eql(self: @This(), a: Type, b: Type) bool { |
| 388 | | return a.eql(b, self.mod); |
| 389 | | } |
| 390 | | }; |
| 391 | | |
| 392 | | pub const HashContext32 = struct { |
| 393 | | mod: *Module, |
| 394 | | |
| 395 | | pub fn hash(self: @This(), t: Type) u32 { |
| 396 | | return @truncate(u32, t.hash(self.mod)); |
| 397 | | } |
| 398 | | pub fn eql(self: @This(), a: Type, b: Type, b_index: usize) bool { |
| 399 | | _ = b_index; |
| 400 | | return a.eql(b, self.mod); |
| 401 | | } |
| 402 | | }; |
| 403 | | |
| 404 | 219 | pub fn format(ty: Type, comptime unused_fmt_string: []const u8, options: std.fmt.FormatOptions, writer: anytype) !void { |
| 405 | 220 | _ = ty; |
| 406 | 221 | _ = unused_fmt_string; |
| ... | ... | @@ -460,214 +275,208 @@ pub const Type = struct { |
| 460 | 275 | |
| 461 | 276 | /// Prints a name suitable for `@typeName`. |
| 462 | 277 | pub fn print(ty: Type, writer: anytype, mod: *Module) @TypeOf(writer).Error!void { |
| 463 | | switch (ty.ip_index) { |
| 464 | | .none => switch (ty.tag()) { |
| 465 | | .inferred_alloc_const => unreachable, |
| 466 | | .inferred_alloc_mut => unreachable, |
| 278 | switch (mod.intern_pool.indexToKey(ty.ip_index)) { |
| 279 | .int_type => |int_type| { |
| 280 | const sign_char: u8 = switch (int_type.signedness) { |
| 281 | .signed => 'i', |
| 282 | .unsigned => 'u', |
| 283 | }; |
| 284 | return writer.print("{c}{d}", .{ sign_char, int_type.bits }); |
| 467 | 285 | }, |
| 468 | | else => switch (mod.intern_pool.indexToKey(ty.ip_index)) { |
| 469 | | .int_type => |int_type| { |
| 470 | | const sign_char: u8 = switch (int_type.signedness) { |
| 471 | | .signed => 'i', |
| 472 | | .unsigned => 'u', |
| 473 | | }; |
| 474 | | return writer.print("{c}{d}", .{ sign_char, int_type.bits }); |
| 475 | | }, |
| 476 | | .ptr_type => { |
| 477 | | const info = ty.ptrInfo(mod); |
| 478 | | |
| 479 | | if (info.sentinel) |s| switch (info.size) { |
| 480 | | .One, .C => unreachable, |
| 481 | | .Many => try writer.print("[*:{}]", .{s.fmtValue(info.pointee_type, mod)}), |
| 482 | | .Slice => try writer.print("[:{}]", .{s.fmtValue(info.pointee_type, mod)}), |
| 483 | | } else switch (info.size) { |
| 484 | | .One => try writer.writeAll("*"), |
| 485 | | .Many => try writer.writeAll("[*]"), |
| 486 | | .C => try writer.writeAll("[*c]"), |
| 487 | | .Slice => try writer.writeAll("[]"), |
| 488 | | } |
| 489 | | if (info.@"align" != 0 or info.host_size != 0 or info.vector_index != .none) { |
| 490 | | if (info.@"align" != 0) { |
| 491 | | try writer.print("align({d}", .{info.@"align"}); |
| 492 | | } else { |
| 493 | | const alignment = info.pointee_type.abiAlignment(mod); |
| 494 | | try writer.print("align({d}", .{alignment}); |
| 495 | | } |
| 286 | .ptr_type => { |
| 287 | const info = ty.ptrInfo(mod); |
| 496 | 288 | |
| 497 | | if (info.bit_offset != 0 or info.host_size != 0) { |
| 498 | | try writer.print(":{d}:{d}", .{ info.bit_offset, info.host_size }); |
| 499 | | } |
| 500 | | if (info.vector_index == .runtime) { |
| 501 | | try writer.writeAll(":?"); |
| 502 | | } else if (info.vector_index != .none) { |
| 503 | | try writer.print(":{d}", .{@enumToInt(info.vector_index)}); |
| 504 | | } |
| 505 | | try writer.writeAll(") "); |
| 506 | | } |
| 507 | | if (info.@"addrspace" != .generic) { |
| 508 | | try writer.print("addrspace(.{s}) ", .{@tagName(info.@"addrspace")}); |
| 289 | if (info.sentinel) |s| switch (info.size) { |
| 290 | .One, .C => unreachable, |
| 291 | .Many => try writer.print("[*:{}]", .{s.fmtValue(info.pointee_type, mod)}), |
| 292 | .Slice => try writer.print("[:{}]", .{s.fmtValue(info.pointee_type, mod)}), |
| 293 | } else switch (info.size) { |
| 294 | .One => try writer.writeAll("*"), |
| 295 | .Many => try writer.writeAll("[*]"), |
| 296 | .C => try writer.writeAll("[*c]"), |
| 297 | .Slice => try writer.writeAll("[]"), |
| 298 | } |
| 299 | if (info.@"align" != 0 or info.host_size != 0 or info.vector_index != .none) { |
| 300 | if (info.@"align" != 0) { |
| 301 | try writer.print("align({d}", .{info.@"align"}); |
| 302 | } else { |
| 303 | const alignment = info.pointee_type.abiAlignment(mod); |
| 304 | try writer.print("align({d}", .{alignment}); |
| 509 | 305 | } |
| 510 | | if (!info.mutable) try writer.writeAll("const "); |
| 511 | | if (info.@"volatile") try writer.writeAll("volatile "); |
| 512 | | if (info.@"allowzero" and info.size != .C) try writer.writeAll("allowzero "); |
| 513 | 306 | |
| 514 | | try print(info.pointee_type, writer, mod); |
| 515 | | return; |
| 516 | | }, |
| 517 | | .array_type => |array_type| { |
| 518 | | if (array_type.sentinel == .none) { |
| 519 | | try writer.print("[{d}]", .{array_type.len}); |
| 520 | | try print(array_type.child.toType(), writer, mod); |
| 521 | | } else { |
| 522 | | try writer.print("[{d}:{}]", .{ |
| 523 | | array_type.len, |
| 524 | | array_type.sentinel.toValue().fmtValue(array_type.child.toType(), mod), |
| 525 | | }); |
| 526 | | try print(array_type.child.toType(), writer, mod); |
| 307 | if (info.bit_offset != 0 or info.host_size != 0) { |
| 308 | try writer.print(":{d}:{d}", .{ info.bit_offset, info.host_size }); |
| 527 | 309 | } |
| 528 | | return; |
| 529 | | }, |
| 530 | | .vector_type => |vector_type| { |
| 531 | | try writer.print("@Vector({d}, ", .{vector_type.len}); |
| 532 | | try print(vector_type.child.toType(), writer, mod); |
| 533 | | try writer.writeAll(")"); |
| 534 | | return; |
| 535 | | }, |
| 536 | | .opt_type => |child| { |
| 537 | | try writer.writeByte('?'); |
| 538 | | return print(child.toType(), writer, mod); |
| 539 | | }, |
| 540 | | .error_union_type => |error_union_type| { |
| 541 | | try print(error_union_type.error_set_type.toType(), writer, mod); |
| 542 | | try writer.writeByte('!'); |
| 543 | | try print(error_union_type.payload_type.toType(), writer, mod); |
| 544 | | return; |
| 545 | | }, |
| 546 | | .inferred_error_set_type => |index| { |
| 547 | | const ies = mod.inferredErrorSetPtr(index); |
| 548 | | const func = ies.func; |
| 310 | if (info.vector_index == .runtime) { |
| 311 | try writer.writeAll(":?"); |
| 312 | } else if (info.vector_index != .none) { |
| 313 | try writer.print(":{d}", .{@enumToInt(info.vector_index)}); |
| 314 | } |
| 315 | try writer.writeAll(") "); |
| 316 | } |
| 317 | if (info.@"addrspace" != .generic) { |
| 318 | try writer.print("addrspace(.{s}) ", .{@tagName(info.@"addrspace")}); |
| 319 | } |
| 320 | if (!info.mutable) try writer.writeAll("const "); |
| 321 | if (info.@"volatile") try writer.writeAll("volatile "); |
| 322 | if (info.@"allowzero" and info.size != .C) try writer.writeAll("allowzero "); |
| 549 | 323 | |
| 550 | | try writer.writeAll("@typeInfo(@typeInfo(@TypeOf("); |
| 551 | | const owner_decl = mod.declPtr(func.owner_decl); |
| 552 | | try owner_decl.renderFullyQualifiedName(mod, writer); |
| 553 | | try writer.writeAll(")).Fn.return_type.?).ErrorUnion.error_set"); |
| 554 | | }, |
| 555 | | .error_set_type => |error_set_type| { |
| 556 | | const names = error_set_type.names; |
| 557 | | try writer.writeAll("error{"); |
| 558 | | for (names, 0..) |name, i| { |
| 559 | | if (i != 0) try writer.writeByte(','); |
| 560 | | try writer.writeAll(mod.intern_pool.stringToSlice(name)); |
| 324 | try print(info.pointee_type, writer, mod); |
| 325 | return; |
| 326 | }, |
| 327 | .array_type => |array_type| { |
| 328 | if (array_type.sentinel == .none) { |
| 329 | try writer.print("[{d}]", .{array_type.len}); |
| 330 | try print(array_type.child.toType(), writer, mod); |
| 331 | } else { |
| 332 | try writer.print("[{d}:{}]", .{ |
| 333 | array_type.len, |
| 334 | array_type.sentinel.toValue().fmtValue(array_type.child.toType(), mod), |
| 335 | }); |
| 336 | try print(array_type.child.toType(), writer, mod); |
| 337 | } |
| 338 | return; |
| 339 | }, |
| 340 | .vector_type => |vector_type| { |
| 341 | try writer.print("@Vector({d}, ", .{vector_type.len}); |
| 342 | try print(vector_type.child.toType(), writer, mod); |
| 343 | try writer.writeAll(")"); |
| 344 | return; |
| 345 | }, |
| 346 | .opt_type => |child| { |
| 347 | try writer.writeByte('?'); |
| 348 | return print(child.toType(), writer, mod); |
| 349 | }, |
| 350 | .error_union_type => |error_union_type| { |
| 351 | try print(error_union_type.error_set_type.toType(), writer, mod); |
| 352 | try writer.writeByte('!'); |
| 353 | try print(error_union_type.payload_type.toType(), writer, mod); |
| 354 | return; |
| 355 | }, |
| 356 | .inferred_error_set_type => |index| { |
| 357 | const ies = mod.inferredErrorSetPtr(index); |
| 358 | const func = ies.func; |
| 359 | |
| 360 | try writer.writeAll("@typeInfo(@typeInfo(@TypeOf("); |
| 361 | const owner_decl = mod.declPtr(func.owner_decl); |
| 362 | try owner_decl.renderFullyQualifiedName(mod, writer); |
| 363 | try writer.writeAll(")).Fn.return_type.?).ErrorUnion.error_set"); |
| 364 | }, |
| 365 | .error_set_type => |error_set_type| { |
| 366 | const names = error_set_type.names; |
| 367 | try writer.writeAll("error{"); |
| 368 | for (names, 0..) |name, i| { |
| 369 | if (i != 0) try writer.writeByte(','); |
| 370 | try writer.writeAll(mod.intern_pool.stringToSlice(name)); |
| 371 | } |
| 372 | try writer.writeAll("}"); |
| 373 | }, |
| 374 | .simple_type => |s| return writer.writeAll(@tagName(s)), |
| 375 | .struct_type => |struct_type| { |
| 376 | if (mod.structPtrUnwrap(struct_type.index)) |struct_obj| { |
| 377 | const decl = mod.declPtr(struct_obj.owner_decl); |
| 378 | try decl.renderFullyQualifiedName(mod, writer); |
| 379 | } else if (struct_type.namespace.unwrap()) |namespace_index| { |
| 380 | const namespace = mod.namespacePtr(namespace_index); |
| 381 | try namespace.renderFullyQualifiedName(mod, "", writer); |
| 382 | } else { |
| 383 | try writer.writeAll("@TypeOf(.{})"); |
| 384 | } |
| 385 | }, |
| 386 | .anon_struct_type => |anon_struct| { |
| 387 | try writer.writeAll("struct{"); |
| 388 | for (anon_struct.types, anon_struct.values, 0..) |field_ty, val, i| { |
| 389 | if (i != 0) try writer.writeAll(", "); |
| 390 | if (val != .none) { |
| 391 | try writer.writeAll("comptime "); |
| 561 | 392 | } |
| 562 | | try writer.writeAll("}"); |
| 563 | | }, |
| 564 | | .simple_type => |s| return writer.writeAll(@tagName(s)), |
| 565 | | .struct_type => |struct_type| { |
| 566 | | if (mod.structPtrUnwrap(struct_type.index)) |struct_obj| { |
| 567 | | const decl = mod.declPtr(struct_obj.owner_decl); |
| 568 | | try decl.renderFullyQualifiedName(mod, writer); |
| 569 | | } else if (struct_type.namespace.unwrap()) |namespace_index| { |
| 570 | | const namespace = mod.namespacePtr(namespace_index); |
| 571 | | try namespace.renderFullyQualifiedName(mod, "", writer); |
| 572 | | } else { |
| 573 | | try writer.writeAll("@TypeOf(.{})"); |
| 393 | if (anon_struct.names.len != 0) { |
| 394 | const name = mod.intern_pool.stringToSlice(anon_struct.names[i]); |
| 395 | try writer.writeAll(name); |
| 396 | try writer.writeAll(": "); |
| 574 | 397 | } |
| 575 | | }, |
| 576 | | .anon_struct_type => |anon_struct| { |
| 577 | | try writer.writeAll("struct{"); |
| 578 | | for (anon_struct.types, anon_struct.values, 0..) |field_ty, val, i| { |
| 579 | | if (i != 0) try writer.writeAll(", "); |
| 580 | | if (val != .none) { |
| 581 | | try writer.writeAll("comptime "); |
| 582 | | } |
| 583 | | if (anon_struct.names.len != 0) { |
| 584 | | const name = mod.intern_pool.stringToSlice(anon_struct.names[i]); |
| 585 | | try writer.writeAll(name); |
| 586 | | try writer.writeAll(": "); |
| 587 | | } |
| 588 | 398 | |
| 589 | | try print(field_ty.toType(), writer, mod); |
| 399 | try print(field_ty.toType(), writer, mod); |
| 590 | 400 | |
| 591 | | if (val != .none) { |
| 592 | | try writer.print(" = {}", .{val.toValue().fmtValue(field_ty.toType(), mod)}); |
| 593 | | } |
| 401 | if (val != .none) { |
| 402 | try writer.print(" = {}", .{val.toValue().fmtValue(field_ty.toType(), mod)}); |
| 594 | 403 | } |
| 595 | | try writer.writeAll("}"); |
| 596 | | }, |
| 404 | } |
| 405 | try writer.writeAll("}"); |
| 406 | }, |
| 597 | 407 | |
| 598 | | .union_type => |union_type| { |
| 599 | | const union_obj = mod.unionPtr(union_type.index); |
| 600 | | const decl = mod.declPtr(union_obj.owner_decl); |
| 601 | | try decl.renderFullyQualifiedName(mod, writer); |
| 602 | | }, |
| 603 | | .opaque_type => |opaque_type| { |
| 604 | | const decl = mod.declPtr(opaque_type.decl); |
| 605 | | try decl.renderFullyQualifiedName(mod, writer); |
| 606 | | }, |
| 607 | | .enum_type => |enum_type| { |
| 608 | | const decl = mod.declPtr(enum_type.decl); |
| 609 | | try decl.renderFullyQualifiedName(mod, writer); |
| 610 | | }, |
| 611 | | .func_type => |fn_info| { |
| 612 | | if (fn_info.is_noinline) { |
| 613 | | try writer.writeAll("noinline "); |
| 614 | | } |
| 615 | | try writer.writeAll("fn("); |
| 616 | | for (fn_info.param_types, 0..) |param_ty, i| { |
| 617 | | if (i != 0) try writer.writeAll(", "); |
| 618 | | if (std.math.cast(u5, i)) |index| { |
| 619 | | if (fn_info.paramIsComptime(index)) { |
| 620 | | try writer.writeAll("comptime "); |
| 621 | | } |
| 622 | | if (fn_info.paramIsNoalias(index)) { |
| 623 | | try writer.writeAll("noalias "); |
| 624 | | } |
| 625 | | } |
| 626 | | if (param_ty == .generic_poison_type) { |
| 627 | | try writer.writeAll("anytype"); |
| 628 | | } else { |
| 629 | | try print(param_ty.toType(), writer, mod); |
| 408 | .union_type => |union_type| { |
| 409 | const union_obj = mod.unionPtr(union_type.index); |
| 410 | const decl = mod.declPtr(union_obj.owner_decl); |
| 411 | try decl.renderFullyQualifiedName(mod, writer); |
| 412 | }, |
| 413 | .opaque_type => |opaque_type| { |
| 414 | const decl = mod.declPtr(opaque_type.decl); |
| 415 | try decl.renderFullyQualifiedName(mod, writer); |
| 416 | }, |
| 417 | .enum_type => |enum_type| { |
| 418 | const decl = mod.declPtr(enum_type.decl); |
| 419 | try decl.renderFullyQualifiedName(mod, writer); |
| 420 | }, |
| 421 | .func_type => |fn_info| { |
| 422 | if (fn_info.is_noinline) { |
| 423 | try writer.writeAll("noinline "); |
| 424 | } |
| 425 | try writer.writeAll("fn("); |
| 426 | for (fn_info.param_types, 0..) |param_ty, i| { |
| 427 | if (i != 0) try writer.writeAll(", "); |
| 428 | if (std.math.cast(u5, i)) |index| { |
| 429 | if (fn_info.paramIsComptime(index)) { |
| 430 | try writer.writeAll("comptime "); |
| 630 | 431 | } |
| 631 | | } |
| 632 | | if (fn_info.is_var_args) { |
| 633 | | if (fn_info.param_types.len != 0) { |
| 634 | | try writer.writeAll(", "); |
| 432 | if (fn_info.paramIsNoalias(index)) { |
| 433 | try writer.writeAll("noalias "); |
| 635 | 434 | } |
| 636 | | try writer.writeAll("..."); |
| 637 | | } |
| 638 | | try writer.writeAll(") "); |
| 639 | | if (fn_info.alignment.toByteUnitsOptional()) |a| { |
| 640 | | try writer.print("align({d}) ", .{a}); |
| 641 | | } |
| 642 | | if (fn_info.cc != .Unspecified) { |
| 643 | | try writer.writeAll("callconv(."); |
| 644 | | try writer.writeAll(@tagName(fn_info.cc)); |
| 645 | | try writer.writeAll(") "); |
| 646 | 435 | } |
| 647 | | if (fn_info.return_type == .generic_poison_type) { |
| 436 | if (param_ty == .generic_poison_type) { |
| 648 | 437 | try writer.writeAll("anytype"); |
| 649 | 438 | } else { |
| 650 | | try print(fn_info.return_type.toType(), writer, mod); |
| 439 | try print(param_ty.toType(), writer, mod); |
| 651 | 440 | } |
| 652 | | }, |
| 653 | | .anyframe_type => |child| { |
| 654 | | if (child == .none) return writer.writeAll("anyframe"); |
| 655 | | try writer.writeAll("anyframe->"); |
| 656 | | return print(child.toType(), writer, mod); |
| 657 | | }, |
| 658 | | |
| 659 | | // values, not types |
| 660 | | .undef => unreachable, |
| 661 | | .un => unreachable, |
| 662 | | .simple_value => unreachable, |
| 663 | | .extern_func => unreachable, |
| 664 | | .int => unreachable, |
| 665 | | .float => unreachable, |
| 666 | | .ptr => unreachable, |
| 667 | | .opt => unreachable, |
| 668 | | .enum_tag => unreachable, |
| 669 | | .aggregate => unreachable, |
| 441 | } |
| 442 | if (fn_info.is_var_args) { |
| 443 | if (fn_info.param_types.len != 0) { |
| 444 | try writer.writeAll(", "); |
| 445 | } |
| 446 | try writer.writeAll("..."); |
| 447 | } |
| 448 | try writer.writeAll(") "); |
| 449 | if (fn_info.alignment.toByteUnitsOptional()) |a| { |
| 450 | try writer.print("align({d}) ", .{a}); |
| 451 | } |
| 452 | if (fn_info.cc != .Unspecified) { |
| 453 | try writer.writeAll("callconv(."); |
| 454 | try writer.writeAll(@tagName(fn_info.cc)); |
| 455 | try writer.writeAll(") "); |
| 456 | } |
| 457 | if (fn_info.return_type == .generic_poison_type) { |
| 458 | try writer.writeAll("anytype"); |
| 459 | } else { |
| 460 | try print(fn_info.return_type.toType(), writer, mod); |
| 461 | } |
| 462 | }, |
| 463 | .anyframe_type => |child| { |
| 464 | if (child == .none) return writer.writeAll("anyframe"); |
| 465 | try writer.writeAll("anyframe->"); |
| 466 | return print(child.toType(), writer, mod); |
| 670 | 467 | }, |
| 468 | |
| 469 | // values, not types |
| 470 | .undef => unreachable, |
| 471 | .un => unreachable, |
| 472 | .simple_value => unreachable, |
| 473 | .extern_func => unreachable, |
| 474 | .int => unreachable, |
| 475 | .float => unreachable, |
| 476 | .ptr => unreachable, |
| 477 | .opt => unreachable, |
| 478 | .enum_tag => unreachable, |
| 479 | .aggregate => unreachable, |
| 671 | 480 | } |
| 672 | 481 | } |
| 673 | 482 | |
| ... | ... | @@ -699,15 +508,10 @@ pub const Type = struct { |
| 699 | 508 | ignore_comptime_only: bool, |
| 700 | 509 | strat: AbiAlignmentAdvancedStrat, |
| 701 | 510 | ) RuntimeBitsError!bool { |
| 702 | | switch (ty.ip_index) { |
| 511 | return switch (ty.ip_index) { |
| 703 | 512 | // False because it is a comptime-only type. |
| 704 | | .empty_struct_type => return false, |
| 705 | | |
| 706 | | .none => switch (ty.tag()) { |
| 707 | | .inferred_alloc_const => unreachable, |
| 708 | | .inferred_alloc_mut => unreachable, |
| 709 | | }, |
| 710 | | else => return switch (mod.intern_pool.indexToKey(ty.ip_index)) { |
| 513 | .empty_struct_type => false, |
| 514 | else => switch (mod.intern_pool.indexToKey(ty.ip_index)) { |
| 711 | 515 | .int_type => |int_type| int_type.bits != 0, |
| 712 | 516 | .ptr_type => |ptr_type| { |
| 713 | 517 | // Pointers to zero-bit types still have a runtime address; however, pointers |
| ... | ... | @@ -802,6 +606,8 @@ pub const Type = struct { |
| 802 | 606 | => false, |
| 803 | 607 | |
| 804 | 608 | .generic_poison => unreachable, |
| 609 | .inferred_alloc_const => unreachable, |
| 610 | .inferred_alloc_mut => unreachable, |
| 805 | 611 | }, |
| 806 | 612 | .struct_type => |struct_type| { |
| 807 | 613 | const struct_obj = mod.structPtrUnwrap(struct_type.index) orelse { |
| ... | ... | @@ -880,7 +686,7 @@ pub const Type = struct { |
| 880 | 686 | .enum_tag => unreachable, |
| 881 | 687 | .aggregate => unreachable, |
| 882 | 688 | }, |
| 883 | | } |
| 689 | }; |
| 884 | 690 | } |
| 885 | 691 | |
| 886 | 692 | /// true if and only if the type has a well-defined memory layout |
| ... | ... | @@ -950,6 +756,9 @@ pub const Type = struct { |
| 950 | 756 | .type_info, |
| 951 | 757 | .generic_poison, |
| 952 | 758 | => false, |
| 759 | |
| 760 | .inferred_alloc_const => unreachable, |
| 761 | .inferred_alloc_mut => unreachable, |
| 953 | 762 | }, |
| 954 | 763 | .struct_type => |struct_type| { |
| 955 | 764 | const struct_obj = mod.structPtrUnwrap(struct_type.index) orelse { |
| ... | ... | @@ -1167,10 +976,7 @@ pub const Type = struct { |
| 1167 | 976 | .f80 => switch (target.c_type_bit_size(.longdouble)) { |
| 1168 | 977 | 80 => return AbiAlignmentAdvanced{ .scalar = target.c_type_alignment(.longdouble) }, |
| 1169 | 978 | else => { |
| 1170 | | const u80_ty: Type = .{ |
| 1171 | | .ip_index = .u80_type, |
| 1172 | | .legacy = undefined, |
| 1173 | | }; |
| 979 | const u80_ty: Type = .{ .ip_index = .u80_type }; |
| 1174 | 980 | return AbiAlignmentAdvanced{ .scalar = abiAlignment(u80_ty, mod) }; |
| 1175 | 981 | }, |
| 1176 | 982 | }, |
| ... | ... | @@ -1194,6 +1000,8 @@ pub const Type = struct { |
| 1194 | 1000 | |
| 1195 | 1001 | .noreturn => unreachable, |
| 1196 | 1002 | .generic_poison => unreachable, |
| 1003 | .inferred_alloc_const => unreachable, |
| 1004 | .inferred_alloc_mut => unreachable, |
| 1197 | 1005 | }, |
| 1198 | 1006 | .struct_type => |struct_type| { |
| 1199 | 1007 | const struct_obj = mod.structPtrUnwrap(struct_type.index) orelse |
| ... | ... | @@ -1562,10 +1370,7 @@ pub const Type = struct { |
| 1562 | 1370 | .f80 => switch (target.c_type_bit_size(.longdouble)) { |
| 1563 | 1371 | 80 => return AbiSizeAdvanced{ .scalar = target.c_type_byte_size(.longdouble) }, |
| 1564 | 1372 | else => { |
| 1565 | | const u80_ty: Type = .{ |
| 1566 | | .ip_index = .u80_type, |
| 1567 | | .legacy = undefined, |
| 1568 | | }; |
| 1373 | const u80_ty: Type = .{ .ip_index = .u80_type }; |
| 1569 | 1374 | return AbiSizeAdvanced{ .scalar = abiSize(u80_ty, mod) }; |
| 1570 | 1375 | }, |
| 1571 | 1376 | }, |
| ... | ... | @@ -1605,6 +1410,8 @@ pub const Type = struct { |
| 1605 | 1410 | .type_info => unreachable, |
| 1606 | 1411 | .noreturn => unreachable, |
| 1607 | 1412 | .generic_poison => unreachable, |
| 1413 | .inferred_alloc_const => unreachable, |
| 1414 | .inferred_alloc_mut => unreachable, |
| 1608 | 1415 | }, |
| 1609 | 1416 | .struct_type => |struct_type| switch (ty.containerLayout(mod)) { |
| 1610 | 1417 | .Packed => { |
| ... | ... | @@ -1835,6 +1642,8 @@ pub const Type = struct { |
| 1835 | 1642 | .undefined => unreachable, |
| 1836 | 1643 | .enum_literal => unreachable, |
| 1837 | 1644 | .generic_poison => unreachable, |
| 1645 | .inferred_alloc_const => unreachable, |
| 1646 | .inferred_alloc_mut => unreachable, |
| 1838 | 1647 | |
| 1839 | 1648 | .atomic_order => unreachable, // missing call to resolveTypeFields |
| 1840 | 1649 | .atomic_rmw_op => unreachable, // missing call to resolveTypeFields |
| ... | ... | @@ -1927,17 +1736,13 @@ pub const Type = struct { |
| 1927 | 1736 | } |
| 1928 | 1737 | |
| 1929 | 1738 | pub fn isSinglePointer(ty: Type, mod: *const Module) bool { |
| 1930 | | switch (ty.ip_index) { |
| 1931 | | .none => return switch (ty.tag()) { |
| 1932 | | .inferred_alloc_const, |
| 1933 | | .inferred_alloc_mut, |
| 1934 | | => true, |
| 1935 | | }, |
| 1936 | | else => return switch (mod.intern_pool.indexToKey(ty.ip_index)) { |
| 1739 | return switch (ty.ip_index) { |
| 1740 | .inferred_alloc_const_type, .inferred_alloc_mut_type => true, |
| 1741 | else => switch (mod.intern_pool.indexToKey(ty.ip_index)) { |
| 1937 | 1742 | .ptr_type => |ptr_info| ptr_info.size == .One, |
| 1938 | 1743 | else => false, |
| 1939 | 1744 | }, |
| 1940 | | } |
| 1745 | }; |
| 1941 | 1746 | } |
| 1942 | 1747 | |
| 1943 | 1748 | /// Asserts `ty` is a pointer. |
| ... | ... | @@ -1948,11 +1753,7 @@ pub const Type = struct { |
| 1948 | 1753 | /// Returns `null` if `ty` is not a pointer. |
| 1949 | 1754 | pub fn ptrSizeOrNull(ty: Type, mod: *const Module) ?std.builtin.Type.Pointer.Size { |
| 1950 | 1755 | return switch (ty.ip_index) { |
| 1951 | | .none => switch (ty.tag()) { |
| 1952 | | .inferred_alloc_const, |
| 1953 | | .inferred_alloc_mut, |
| 1954 | | => .One, |
| 1955 | | }, |
| 1756 | .inferred_alloc_const_type, .inferred_alloc_mut_type => .One, |
| 1956 | 1757 | else => switch (mod.intern_pool.indexToKey(ty.ip_index)) { |
| 1957 | 1758 | .ptr_type => |ptr_info| ptr_info.size, |
| 1958 | 1759 | else => null, |
| ... | ... | @@ -2625,10 +2426,6 @@ pub const Type = struct { |
| 2625 | 2426 | while (true) switch (ty.ip_index) { |
| 2626 | 2427 | .empty_struct_type => return Value.empty_struct, |
| 2627 | 2428 | |
| 2628 | | .none => switch (ty.tag()) { |
| 2629 | | .inferred_alloc_const => unreachable, |
| 2630 | | .inferred_alloc_mut => unreachable, |
| 2631 | | }, |
| 2632 | 2429 | else => switch (mod.intern_pool.indexToKey(ty.ip_index)) { |
| 2633 | 2430 | .int_type => |int_type| { |
| 2634 | 2431 | if (int_type.bits == 0) { |
| ... | ... | @@ -2710,6 +2507,8 @@ pub const Type = struct { |
| 2710 | 2507 | .undefined => return Value.undef, |
| 2711 | 2508 | |
| 2712 | 2509 | .generic_poison => unreachable, |
| 2510 | .inferred_alloc_const => unreachable, |
| 2511 | .inferred_alloc_mut => unreachable, |
| 2713 | 2512 | }, |
| 2714 | 2513 | .struct_type => |struct_type| { |
| 2715 | 2514 | if (mod.structPtrUnwrap(struct_type.index)) |s| { |
| ... | ... | @@ -2888,6 +2687,9 @@ pub const Type = struct { |
| 2888 | 2687 | .enum_literal, |
| 2889 | 2688 | .type_info, |
| 2890 | 2689 | => true, |
| 2690 | |
| 2691 | .inferred_alloc_const => unreachable, |
| 2692 | .inferred_alloc_mut => unreachable, |
| 2891 | 2693 | }, |
| 2892 | 2694 | .struct_type => |struct_type| { |
| 2893 | 2695 | // A struct with no fields is not comptime-only. |
| ... | ... | @@ -3343,61 +3145,56 @@ pub const Type = struct { |
| 3343 | 3145 | |
| 3344 | 3146 | /// Supports structs and unions. |
| 3345 | 3147 | pub fn structFieldOffset(ty: Type, index: usize, mod: *Module) u64 { |
| 3346 | | switch (ty.ip_index) { |
| 3347 | | .none => switch (ty.tag()) { |
| 3348 | | else => unreachable, |
| 3349 | | }, |
| 3350 | | else => switch (mod.intern_pool.indexToKey(ty.ip_index)) { |
| 3351 | | .struct_type => |struct_type| { |
| 3352 | | const struct_obj = mod.structPtrUnwrap(struct_type.index).?; |
| 3353 | | assert(struct_obj.haveLayout()); |
| 3354 | | assert(struct_obj.layout != .Packed); |
| 3355 | | var it = ty.iterateStructOffsets(mod); |
| 3356 | | while (it.next()) |field_offset| { |
| 3357 | | if (index == field_offset.field) |
| 3358 | | return field_offset.offset; |
| 3359 | | } |
| 3360 | | |
| 3361 | | return std.mem.alignForwardGeneric(u64, it.offset, @max(it.big_align, 1)); |
| 3362 | | }, |
| 3148 | switch (mod.intern_pool.indexToKey(ty.ip_index)) { |
| 3149 | .struct_type => |struct_type| { |
| 3150 | const struct_obj = mod.structPtrUnwrap(struct_type.index).?; |
| 3151 | assert(struct_obj.haveLayout()); |
| 3152 | assert(struct_obj.layout != .Packed); |
| 3153 | var it = ty.iterateStructOffsets(mod); |
| 3154 | while (it.next()) |field_offset| { |
| 3155 | if (index == field_offset.field) |
| 3156 | return field_offset.offset; |
| 3157 | } |
| 3363 | 3158 | |
| 3364 | | .anon_struct_type => |tuple| { |
| 3365 | | var offset: u64 = 0; |
| 3366 | | var big_align: u32 = 0; |
| 3159 | return std.mem.alignForwardGeneric(u64, it.offset, @max(it.big_align, 1)); |
| 3160 | }, |
| 3367 | 3161 | |
| 3368 | | for (tuple.types, tuple.values, 0..) |field_ty, field_val, i| { |
| 3369 | | if (field_val != .none or !field_ty.toType().hasRuntimeBits(mod)) { |
| 3370 | | // comptime field |
| 3371 | | if (i == index) return offset; |
| 3372 | | continue; |
| 3373 | | } |
| 3162 | .anon_struct_type => |tuple| { |
| 3163 | var offset: u64 = 0; |
| 3164 | var big_align: u32 = 0; |
| 3374 | 3165 | |
| 3375 | | const field_align = field_ty.toType().abiAlignment(mod); |
| 3376 | | big_align = @max(big_align, field_align); |
| 3377 | | offset = std.mem.alignForwardGeneric(u64, offset, field_align); |
| 3166 | for (tuple.types, tuple.values, 0..) |field_ty, field_val, i| { |
| 3167 | if (field_val != .none or !field_ty.toType().hasRuntimeBits(mod)) { |
| 3168 | // comptime field |
| 3378 | 3169 | if (i == index) return offset; |
| 3379 | | offset += field_ty.toType().abiSize(mod); |
| 3170 | continue; |
| 3380 | 3171 | } |
| 3381 | | offset = std.mem.alignForwardGeneric(u64, offset, @max(big_align, 1)); |
| 3382 | | return offset; |
| 3383 | | }, |
| 3384 | 3172 | |
| 3385 | | .union_type => |union_type| { |
| 3386 | | if (!union_type.hasTag()) |
| 3387 | | return 0; |
| 3388 | | const union_obj = mod.unionPtr(union_type.index); |
| 3389 | | const layout = union_obj.getLayout(mod, true); |
| 3390 | | if (layout.tag_align >= layout.payload_align) { |
| 3391 | | // {Tag, Payload} |
| 3392 | | return std.mem.alignForwardGeneric(u64, layout.tag_size, layout.payload_align); |
| 3393 | | } else { |
| 3394 | | // {Payload, Tag} |
| 3395 | | return 0; |
| 3396 | | } |
| 3397 | | }, |
| 3173 | const field_align = field_ty.toType().abiAlignment(mod); |
| 3174 | big_align = @max(big_align, field_align); |
| 3175 | offset = std.mem.alignForwardGeneric(u64, offset, field_align); |
| 3176 | if (i == index) return offset; |
| 3177 | offset += field_ty.toType().abiSize(mod); |
| 3178 | } |
| 3179 | offset = std.mem.alignForwardGeneric(u64, offset, @max(big_align, 1)); |
| 3180 | return offset; |
| 3181 | }, |
| 3398 | 3182 | |
| 3399 | | else => unreachable, |
| 3183 | .union_type => |union_type| { |
| 3184 | if (!union_type.hasTag()) |
| 3185 | return 0; |
| 3186 | const union_obj = mod.unionPtr(union_type.index); |
| 3187 | const layout = union_obj.getLayout(mod, true); |
| 3188 | if (layout.tag_align >= layout.payload_align) { |
| 3189 | // {Tag, Payload} |
| 3190 | return std.mem.alignForwardGeneric(u64, layout.tag_size, layout.payload_align); |
| 3191 | } else { |
| 3192 | // {Payload, Tag} |
| 3193 | return 0; |
| 3194 | } |
| 3400 | 3195 | }, |
| 3196 | |
| 3197 | else => unreachable, |
| 3401 | 3198 | } |
| 3402 | 3199 | } |
| 3403 | 3200 | |
| ... | ... | @@ -3445,25 +3242,6 @@ pub const Type = struct { |
| 3445 | 3242 | return ty.ip_index == .generic_poison_type; |
| 3446 | 3243 | } |
| 3447 | 3244 | |
| 3448 | | /// This enum does not directly correspond to `std.builtin.TypeId` because |
| 3449 | | /// it has extra enum tags in it, as a way of using less memory. For example, |
| 3450 | | /// even though Zig recognizes `*align(10) i32` and `*i32` both as Pointer types |
| 3451 | | /// but with different alignment values, in this data structure they are represented |
| 3452 | | /// with different enum tags, because the the former requires more payload data than the latter. |
| 3453 | | /// See `zigTypeTag` for the function that corresponds to `std.builtin.TypeId`. |
| 3454 | | pub const Tag = enum(usize) { |
| 3455 | | /// This is a special value that tracks a set of types that have been stored |
| 3456 | | /// to an inferred allocation. It does not support most of the normal type queries. |
| 3457 | | /// However it does respond to `isConstPtr`, `ptrSize`, `zigTypeTag`, etc. |
| 3458 | | inferred_alloc_mut, |
| 3459 | | /// Same as `inferred_alloc_mut` but the local is `var` not `const`. |
| 3460 | | inferred_alloc_const, // See last_no_payload_tag below. |
| 3461 | | // After this, the tag requires a payload. |
| 3462 | | |
| 3463 | | pub const last_no_payload_tag = Tag.inferred_alloc_const; |
| 3464 | | pub const no_payload_count = @enumToInt(last_no_payload_tag) + 1; |
| 3465 | | }; |
| 3466 | | |
| 3467 | 3245 | pub fn isTuple(ty: Type, mod: *Module) bool { |
| 3468 | 3246 | return switch (ty.ip_index) { |
| 3469 | 3247 | .none => false, |
| ... | ... | @@ -3511,14 +3289,9 @@ pub const Type = struct { |
| 3511 | 3289 | }; |
| 3512 | 3290 | } |
| 3513 | 3291 | |
| 3514 | | /// The sub-types are named after what fields they contain. |
| 3515 | 3292 | pub const Payload = struct { |
| 3516 | | tag: Tag, |
| 3517 | | |
| 3518 | 3293 | /// TODO: remove this data structure since we have `InternPool.Key.PtrType`. |
| 3519 | 3294 | pub const Pointer = struct { |
| 3520 | | data: Data, |
| 3521 | | |
| 3522 | 3295 | pub const Data = struct { |
| 3523 | 3296 | pointee_type: Type, |
| 3524 | 3297 | sentinel: ?Value = null, |
| ... | ... | @@ -3568,64 +3341,60 @@ pub const Type = struct { |
| 3568 | 3341 | }; |
| 3569 | 3342 | }; |
| 3570 | 3343 | |
| 3571 | | pub const @"u1": Type = .{ .ip_index = .u1_type, .legacy = undefined }; |
| 3572 | | pub const @"u8": Type = .{ .ip_index = .u8_type, .legacy = undefined }; |
| 3573 | | pub const @"u16": Type = .{ .ip_index = .u16_type, .legacy = undefined }; |
| 3574 | | pub const @"u29": Type = .{ .ip_index = .u29_type, .legacy = undefined }; |
| 3575 | | pub const @"u32": Type = .{ .ip_index = .u32_type, .legacy = undefined }; |
| 3576 | | pub const @"u64": Type = .{ .ip_index = .u64_type, .legacy = undefined }; |
| 3577 | | pub const @"u128": Type = .{ .ip_index = .u128_type, .legacy = undefined }; |
| 3578 | | |
| 3579 | | pub const @"i8": Type = .{ .ip_index = .i8_type, .legacy = undefined }; |
| 3580 | | pub const @"i16": Type = .{ .ip_index = .i16_type, .legacy = undefined }; |
| 3581 | | pub const @"i32": Type = .{ .ip_index = .i32_type, .legacy = undefined }; |
| 3582 | | pub const @"i64": Type = .{ .ip_index = .i64_type, .legacy = undefined }; |
| 3583 | | pub const @"i128": Type = .{ .ip_index = .i128_type, .legacy = undefined }; |
| 3584 | | |
| 3585 | | pub const @"f16": Type = .{ .ip_index = .f16_type, .legacy = undefined }; |
| 3586 | | pub const @"f32": Type = .{ .ip_index = .f32_type, .legacy = undefined }; |
| 3587 | | pub const @"f64": Type = .{ .ip_index = .f64_type, .legacy = undefined }; |
| 3588 | | pub const @"f80": Type = .{ .ip_index = .f80_type, .legacy = undefined }; |
| 3589 | | pub const @"f128": Type = .{ .ip_index = .f128_type, .legacy = undefined }; |
| 3590 | | |
| 3591 | | pub const @"bool": Type = .{ .ip_index = .bool_type, .legacy = undefined }; |
| 3592 | | pub const @"usize": Type = .{ .ip_index = .usize_type, .legacy = undefined }; |
| 3593 | | pub const @"isize": Type = .{ .ip_index = .isize_type, .legacy = undefined }; |
| 3594 | | pub const @"comptime_int": Type = .{ .ip_index = .comptime_int_type, .legacy = undefined }; |
| 3595 | | pub const @"comptime_float": Type = .{ .ip_index = .comptime_float_type, .legacy = undefined }; |
| 3596 | | pub const @"void": Type = .{ .ip_index = .void_type, .legacy = undefined }; |
| 3597 | | pub const @"type": Type = .{ .ip_index = .type_type, .legacy = undefined }; |
| 3598 | | pub const @"anyerror": Type = .{ .ip_index = .anyerror_type, .legacy = undefined }; |
| 3599 | | pub const @"anyopaque": Type = .{ .ip_index = .anyopaque_type, .legacy = undefined }; |
| 3600 | | pub const @"anyframe": Type = .{ .ip_index = .anyframe_type, .legacy = undefined }; |
| 3601 | | pub const @"null": Type = .{ .ip_index = .null_type, .legacy = undefined }; |
| 3602 | | pub const @"undefined": Type = .{ .ip_index = .undefined_type, .legacy = undefined }; |
| 3603 | | pub const @"noreturn": Type = .{ .ip_index = .noreturn_type, .legacy = undefined }; |
| 3604 | | |
| 3605 | | pub const @"c_char": Type = .{ .ip_index = .c_char_type, .legacy = undefined }; |
| 3606 | | pub const @"c_short": Type = .{ .ip_index = .c_short_type, .legacy = undefined }; |
| 3607 | | pub const @"c_ushort": Type = .{ .ip_index = .c_ushort_type, .legacy = undefined }; |
| 3608 | | pub const @"c_int": Type = .{ .ip_index = .c_int_type, .legacy = undefined }; |
| 3609 | | pub const @"c_uint": Type = .{ .ip_index = .c_uint_type, .legacy = undefined }; |
| 3610 | | pub const @"c_long": Type = .{ .ip_index = .c_long_type, .legacy = undefined }; |
| 3611 | | pub const @"c_ulong": Type = .{ .ip_index = .c_ulong_type, .legacy = undefined }; |
| 3612 | | pub const @"c_longlong": Type = .{ .ip_index = .c_longlong_type, .legacy = undefined }; |
| 3613 | | pub const @"c_ulonglong": Type = .{ .ip_index = .c_ulonglong_type, .legacy = undefined }; |
| 3614 | | pub const @"c_longdouble": Type = .{ .ip_index = .c_longdouble_type, .legacy = undefined }; |
| 3615 | | |
| 3616 | | pub const const_slice_u8: Type = .{ .ip_index = .const_slice_u8_type, .legacy = undefined }; |
| 3617 | | pub const manyptr_u8: Type = .{ .ip_index = .manyptr_u8_type, .legacy = undefined }; |
| 3344 | pub const @"u1": Type = .{ .ip_index = .u1_type }; |
| 3345 | pub const @"u8": Type = .{ .ip_index = .u8_type }; |
| 3346 | pub const @"u16": Type = .{ .ip_index = .u16_type }; |
| 3347 | pub const @"u29": Type = .{ .ip_index = .u29_type }; |
| 3348 | pub const @"u32": Type = .{ .ip_index = .u32_type }; |
| 3349 | pub const @"u64": Type = .{ .ip_index = .u64_type }; |
| 3350 | pub const @"u128": Type = .{ .ip_index = .u128_type }; |
| 3351 | |
| 3352 | pub const @"i8": Type = .{ .ip_index = .i8_type }; |
| 3353 | pub const @"i16": Type = .{ .ip_index = .i16_type }; |
| 3354 | pub const @"i32": Type = .{ .ip_index = .i32_type }; |
| 3355 | pub const @"i64": Type = .{ .ip_index = .i64_type }; |
| 3356 | pub const @"i128": Type = .{ .ip_index = .i128_type }; |
| 3357 | |
| 3358 | pub const @"f16": Type = .{ .ip_index = .f16_type }; |
| 3359 | pub const @"f32": Type = .{ .ip_index = .f32_type }; |
| 3360 | pub const @"f64": Type = .{ .ip_index = .f64_type }; |
| 3361 | pub const @"f80": Type = .{ .ip_index = .f80_type }; |
| 3362 | pub const @"f128": Type = .{ .ip_index = .f128_type }; |
| 3363 | |
| 3364 | pub const @"bool": Type = .{ .ip_index = .bool_type }; |
| 3365 | pub const @"usize": Type = .{ .ip_index = .usize_type }; |
| 3366 | pub const @"isize": Type = .{ .ip_index = .isize_type }; |
| 3367 | pub const @"comptime_int": Type = .{ .ip_index = .comptime_int_type }; |
| 3368 | pub const @"comptime_float": Type = .{ .ip_index = .comptime_float_type }; |
| 3369 | pub const @"void": Type = .{ .ip_index = .void_type }; |
| 3370 | pub const @"type": Type = .{ .ip_index = .type_type }; |
| 3371 | pub const @"anyerror": Type = .{ .ip_index = .anyerror_type }; |
| 3372 | pub const @"anyopaque": Type = .{ .ip_index = .anyopaque_type }; |
| 3373 | pub const @"anyframe": Type = .{ .ip_index = .anyframe_type }; |
| 3374 | pub const @"null": Type = .{ .ip_index = .null_type }; |
| 3375 | pub const @"undefined": Type = .{ .ip_index = .undefined_type }; |
| 3376 | pub const @"noreturn": Type = .{ .ip_index = .noreturn_type }; |
| 3377 | |
| 3378 | pub const @"c_char": Type = .{ .ip_index = .c_char_type }; |
| 3379 | pub const @"c_short": Type = .{ .ip_index = .c_short_type }; |
| 3380 | pub const @"c_ushort": Type = .{ .ip_index = .c_ushort_type }; |
| 3381 | pub const @"c_int": Type = .{ .ip_index = .c_int_type }; |
| 3382 | pub const @"c_uint": Type = .{ .ip_index = .c_uint_type }; |
| 3383 | pub const @"c_long": Type = .{ .ip_index = .c_long_type }; |
| 3384 | pub const @"c_ulong": Type = .{ .ip_index = .c_ulong_type }; |
| 3385 | pub const @"c_longlong": Type = .{ .ip_index = .c_longlong_type }; |
| 3386 | pub const @"c_ulonglong": Type = .{ .ip_index = .c_ulonglong_type }; |
| 3387 | pub const @"c_longdouble": Type = .{ .ip_index = .c_longdouble_type }; |
| 3388 | |
| 3389 | pub const const_slice_u8: Type = .{ .ip_index = .const_slice_u8_type }; |
| 3390 | pub const manyptr_u8: Type = .{ .ip_index = .manyptr_u8_type }; |
| 3618 | 3391 | pub const single_const_pointer_to_comptime_int: Type = .{ |
| 3619 | 3392 | .ip_index = .single_const_pointer_to_comptime_int_type, |
| 3620 | | .legacy = undefined, |
| 3621 | | }; |
| 3622 | | pub const const_slice_u8_sentinel_0: Type = .{ |
| 3623 | | .ip_index = .const_slice_u8_sentinel_0_type, |
| 3624 | | .legacy = undefined, |
| 3625 | 3393 | }; |
| 3626 | | pub const empty_struct_literal: Type = .{ .ip_index = .empty_struct_type, .legacy = undefined }; |
| 3394 | pub const const_slice_u8_sentinel_0: Type = .{ .ip_index = .const_slice_u8_sentinel_0_type }; |
| 3395 | pub const empty_struct_literal: Type = .{ .ip_index = .empty_struct_type }; |
| 3627 | 3396 | |
| 3628 | | pub const generic_poison: Type = .{ .ip_index = .generic_poison_type, .legacy = undefined }; |
| 3397 | pub const generic_poison: Type = .{ .ip_index = .generic_poison_type }; |
| 3629 | 3398 | |
| 3630 | 3399 | pub const err_int = Type.u16; |
| 3631 | 3400 | |
| ... | ... | @@ -3709,33 +3478,4 @@ pub const Type = struct { |
| 3709 | 3478 | /// This is only used for comptime asserts. Bump this number when you make a change |
| 3710 | 3479 | /// to packed struct layout to find out all the places in the codebase you need to edit! |
| 3711 | 3480 | pub const packed_struct_layout_version = 2; |
| 3712 | | |
| 3713 | | /// This function is used in the debugger pretty formatters in tools/ to fetch the |
| 3714 | | /// Tag to Payload mapping to facilitate fancy debug printing for this type. |
| 3715 | | fn dbHelper(self: *Type, tag_to_payload_map: *map: { |
| 3716 | | const tags = @typeInfo(Tag).Enum.fields; |
| 3717 | | var fields: [tags.len]std.builtin.Type.StructField = undefined; |
| 3718 | | for (&fields, tags) |*field, t| field.* = .{ |
| 3719 | | .name = t.name, |
| 3720 | | .type = *if (t.value < Tag.no_payload_count) void else @field(Tag, t.name).Type(), |
| 3721 | | .default_value = null, |
| 3722 | | .is_comptime = false, |
| 3723 | | .alignment = 0, |
| 3724 | | }; |
| 3725 | | break :map @Type(.{ .Struct = .{ |
| 3726 | | .layout = .Extern, |
| 3727 | | .fields = &fields, |
| 3728 | | .decls = &.{}, |
| 3729 | | .is_tuple = false, |
| 3730 | | } }); |
| 3731 | | }) void { |
| 3732 | | _ = self; |
| 3733 | | _ = tag_to_payload_map; |
| 3734 | | } |
| 3735 | | |
| 3736 | | comptime { |
| 3737 | | if (builtin.mode == .Debug) { |
| 3738 | | _ = &dbHelper; |
| 3739 | | } |
| 3740 | | } |
| 3741 | 3481 | }; |