1//! All interned objects have both a value and a type.
2//! This data structure is self-contained.
3const InternPool = @This();
4
5const builtin = @import("builtin");
6const build_options = @import("build_options");
7
8const std = @import("std");
9const Io = std.Io;
10const Allocator = std.mem.Allocator;
11const assert = std.debug.assert;
12const BigIntConst = std.math.big.int.Const;
13const BigIntMutable = std.math.big.int.Mutable;
14const Cache = std.Build.Cache;
15const Limb = std.math.big.Limb;
16const Hash = std.hash.Wyhash;
17const Zir = std.zig.Zir;
18
19const Zcu = @import("Zcu.zig");
20const TypeClass = @import("Type.zig").Class;
21
22/// One item per thread, indexed by `tid`, which is dense and unique per thread.
23locals: []Local,
24/// Length must be a power of two and represents the number of simultaneous
25/// writers that can mutate any single sharded data structure.
26shards: []Shard,
27/// Key is the error name, index is the error tag value. Index 0 has a length-0 string.
28global_error_set: GlobalErrorSet,
29/// Cached number of active bits in a `tid`.
30tid_width: if (single_threaded) u0 else std.math.Log2Int(u32),
31/// Cached shift amount to put a `tid` in the top bits of a 30-bit value.
32tid_shift_30: if (single_threaded) u0 else std.math.Log2Int(u32),
33/// Cached shift amount to put a `tid` in the top bits of a 31-bit value.
34tid_shift_31: if (single_threaded) u0 else std.math.Log2Int(u32),
35/// Cached shift amount to put a `tid` in the top bits of a 32-bit value.
36tid_shift_32: if (single_threaded) u0 else std.math.Log2Int(u32),
37
38/// Dependencies on the source code hash associated with a ZIR instruction.
39/// * For a `declaration`, this is the entire declaration body.
40/// * For a `struct_decl`, `union_decl`, etc, this is the source of the fields (but not declarations).
41/// * For a `func`, this is the source of the full function signature.
42/// These are also invalidated if tracking fails for this instruction.
43/// Value is index into `dep_entries` of the first dependency on this hash.
44src_hash_deps: std.array_hash_map.Auto(TrackedInst.Index, DepEntry.Index),
45/// Dependencies on the value of a Nav.
46/// Value is index into `dep_entries` of the first dependency on this Nav value.
47nav_val_deps: std.array_hash_map.Auto(Nav.Index, DepEntry.Index),
48/// Dependencies on the type of a Nav.
49/// Value is index into `dep_entries` of the first dependency on this Nav value.
50nav_ty_deps: std.array_hash_map.Auto(Nav.Index, DepEntry.Index),
51/// Dependencies on a function's inferred error set. Key is the function body, not the IES.
52/// Value is index into `dep_entries` of the first dependency on this function's IES.
53func_ies_deps: std.array_hash_map.Auto(Index, DepEntry.Index),
54/// Dependencies on the resolved layout of a `struct`, `union`, or `enum` type.
55/// Value is index into `dep_entries` of the first dependency on this type's layout.
56type_layout_deps: std.array_hash_map.Auto(Index, DepEntry.Index),
57/// Dependencies on the resolved default field values of a `struct` type.
58/// Value is index into `dep_entries` of the first dependency on this type's inits.
59struct_defaults_deps: std.array_hash_map.Auto(Index, DepEntry.Index),
60/// Dependencies on a Zig or ZON source file. Triggered by `@import`.
61/// * For ZON source files, the dependency is invalidated if the file changes at all. The `@import`
62/// must be re-analyzed to return the new data structure.
63/// * For Zig source files, the dependency is invalidated if the file's root struct type changes
64/// (which can only happen because the `.main_struct_inst` got lost). The `@import` must be
65/// re-analyzed to return the new type.
66/// Value is index into `dep_entries` of the first dependency on this Zig/ZON file.
67source_file_deps: std.array_hash_map.Auto(FileIndex, DepEntry.Index),
68/// Dependencies on an embedded file.
69/// Introduced by `@embedFile`; invalidated when the file changes.
70/// Value is index into `dep_entries` of the first dependency on this `Zcu.EmbedFile`.
71embed_file_deps: std.array_hash_map.Auto(Zcu.EmbedFile.Index, DepEntry.Index),
72/// Dependencies on the full set of names in a ZIR namespace.
73/// Key refers to a `struct_decl`, `union_decl`, etc.
74/// Value is index into `dep_entries` of the first dependency on this namespace.
75namespace_deps: std.array_hash_map.Auto(TrackedInst.Index, DepEntry.Index),
76/// Dependencies on the (non-)existence of some name in a namespace.
77/// Value is index into `dep_entries` of the first dependency on this name.
78namespace_name_deps: std.array_hash_map.Auto(NamespaceNameKey, DepEntry.Index),
79// Dependencies on the value of fields memoized on `Zcu` (`panic_messages` etc).
80// If set, these are indices into `dep_entries` of the first dependency on this state.
81memoized_state_main_deps: DepEntry.Index.Optional,
82memoized_state_panic_deps: DepEntry.Index.Optional,
83memoized_state_va_list_deps: DepEntry.Index.Optional,
84memoized_state_assembly_deps: DepEntry.Index.Optional,
85
86/// Given a `Depender`, points to an entry in `dep_entries` whose `depender`
87/// matches. The `next_dependee` field can be used to iterate all such entries
88/// and remove them from the corresponding lists.
89first_dependency: std.array_hash_map.Auto(AnalUnit, DepEntry.Index),
90
91/// Stores dependency information. The hashmaps declared above are used to look
92/// up entries in this list as required. This is not stored in `extra` so that
93/// we can use `free_dep_entries` to track free indices, since dependencies are
94/// removed frequently.
95dep_entries: std.ArrayList(DepEntry),
96/// Stores unused indices in `dep_entries` which can be reused without a full
97/// garbage collection pass.
98free_dep_entries: std.ArrayList(DepEntry.Index),
99
100/// Whether a single-threaded intern pool impl is in use.
101pub const single_threaded = switch (build_options.io_mode) {
102 .threaded => builtin.single_threaded,
103 .evented => false, // even without threads, evented can be access from multiple tasks at a time
104};
105
106pub const empty: InternPool = .{
107 .locals = &.{},
108 .shards = &.{},
109 .global_error_set = .empty,
110 .tid_width = 0,
111 .tid_shift_30 = 0,
112 .tid_shift_31 = 0,
113 .tid_shift_32 = 0,
114 .src_hash_deps = .empty,
115 .nav_val_deps = .empty,
116 .nav_ty_deps = .empty,
117 .func_ies_deps = .empty,
118 .type_layout_deps = .empty,
119 .struct_defaults_deps = .empty,
120 .source_file_deps = .empty,
121 .embed_file_deps = .empty,
122 .namespace_deps = .empty,
123 .namespace_name_deps = .empty,
124 .memoized_state_main_deps = .none,
125 .memoized_state_panic_deps = .none,
126 .memoized_state_va_list_deps = .none,
127 .memoized_state_assembly_deps = .none,
128 .first_dependency = .empty,
129 .dep_entries = .empty,
130 .free_dep_entries = .empty,
131};
132
133/// A `TrackedInst.Index` provides a single, unchanging reference to a ZIR instruction across a whole
134/// compilation. From this index, you can acquire a `TrackedInst`, which containss a reference to both
135/// the file which the instruction lives in, and the instruction index itself, which is updated on
136/// incremental updates by `Zcu.updateZirRefs`.
137pub const TrackedInst = extern struct {
138 file: FileIndex,
139 inst: Zir.Inst.Index,
140
141 /// It is possible on an incremental update that we "lose" a ZIR instruction: some tracked `%x` in
142 /// the old ZIR failed to map to any `%y` in the new ZIR. For this reason, we actually store values
143 /// of type `MaybeLost`, which uses `ZirIndex.lost` to represent this case. `Index.resolve` etc
144 /// return `null` when the `TrackedInst` being resolved has been lost.
145 pub const MaybeLost = extern struct {
146 file: FileIndex,
147 inst: ZirIndex,
148 pub const ZirIndex = enum(u32) {
149 /// Tracking failed for this ZIR instruction. Uses of it should fail.
150 lost = std.math.maxInt(u32),
151 _,
152 pub fn unwrap(inst: ZirIndex) ?Zir.Inst.Index {
153 return switch (inst) {
154 .lost => null,
155 _ => @fromBackingInt(@intCast(@backingInt(inst))),
156 };
157 }
158 pub fn wrap(inst: Zir.Inst.Index) ZirIndex {
159 return @fromBackingInt(@intCast(@backingInt(inst)));
160 }
161 };
162 comptime {
163 // The fields should be tightly packed. See also serialiation logic in `Compilation.saveState`.
164 assert(@sizeOf(@This()) == @sizeOf(FileIndex) + @sizeOf(ZirIndex));
165 }
166 };
167
168 pub const Index = enum(u32) {
169 _,
170 pub fn resolveFull(tracked_inst_index: TrackedInst.Index, ip: *const InternPool) ?TrackedInst {
171 const tracked_inst_unwrapped = tracked_inst_index.unwrap(ip);
172 const tracked_insts = ip.getLocalShared(tracked_inst_unwrapped.tid).tracked_insts.acquire();
173 const maybe_lost = tracked_insts.view().items(.@"0")[tracked_inst_unwrapped.index];
174 return .{
175 .file = maybe_lost.file,
176 .inst = maybe_lost.inst.unwrap() orelse return null,
177 };
178 }
179 pub fn resolveFile(tracked_inst_index: TrackedInst.Index, ip: *const InternPool) FileIndex {
180 const tracked_inst_unwrapped = tracked_inst_index.unwrap(ip);
181 const tracked_insts = ip.getLocalShared(tracked_inst_unwrapped.tid).tracked_insts.acquire();
182 const maybe_lost = tracked_insts.view().items(.@"0")[tracked_inst_unwrapped.index];
183 return maybe_lost.file;
184 }
185 pub fn resolve(i: TrackedInst.Index, ip: *const InternPool) ?Zir.Inst.Index {
186 return (i.resolveFull(ip) orelse return null).inst;
187 }
188
189 pub fn toOptional(i: TrackedInst.Index) Optional {
190 return @fromBackingInt(@intCast(@backingInt(i)));
191 }
192 pub const Optional = enum(u32) {
193 none = std.math.maxInt(u32),
194 _,
195 pub fn unwrap(opt: Optional) ?TrackedInst.Index {
196 return switch (opt) {
197 .none => null,
198 _ => @fromBackingInt(@intCast(@backingInt(opt))),
199 };
200 }
201
202 const debug_state = InternPool.debug_state;
203 };
204
205 pub const Unwrapped = struct {
206 tid: Zcu.PerThread.Id,
207 index: u32,
208
209 pub fn wrap(unwrapped: Unwrapped, ip: *const InternPool) TrackedInst.Index {
210 assert(@backingInt(unwrapped.tid) <= ip.getTidMask());
211 assert(unwrapped.index <= ip.getIndexMask(u32));
212 return @fromBackingInt(@intCast(@shlExact(@as(u32, @backingInt(unwrapped.tid)), ip.tid_shift_32) |
213 unwrapped.index));
214 }
215 };
216 pub fn unwrap(tracked_inst_index: TrackedInst.Index, ip: *const InternPool) Unwrapped {
217 return .{
218 .tid = @fromBackingInt(@intCast(@backingInt(tracked_inst_index) >> ip.tid_shift_32 & ip.getTidMask())),
219 .index = @backingInt(tracked_inst_index) & ip.getIndexMask(u32),
220 };
221 }
222
223 const debug_state = InternPool.debug_state;
224 };
225};
226
227pub fn trackZir(
228 ip: *InternPool,
229 gpa: Allocator,
230 io: Io,
231 tid: Zcu.PerThread.Id,
232 key: TrackedInst,
233) Allocator.Error!TrackedInst.Index {
234 const maybe_lost_key: TrackedInst.MaybeLost = .{
235 .file = key.file,
236 .inst = TrackedInst.MaybeLost.ZirIndex.wrap(key.inst),
237 };
238 const full_hash = Hash.hash(0, std.mem.asBytes(&maybe_lost_key));
239 const hash: u32 = @truncate(full_hash >> 32);
240 const shard = &ip.shards[@intCast(full_hash & (ip.shards.len - 1))];
241 var map = shard.shared.tracked_inst_map.acquire();
242 const Map = @TypeOf(map);
243 var map_mask = map.header().mask();
244 var map_index = hash;
245 while (true) : (map_index += 1) {
246 map_index &= map_mask;
247 const entry = &map.entries[map_index];
248 const index = entry.acquire().unwrap() orelse break;
249 if (entry.hash != hash) continue;
250 if (std.meta.eql(index.resolveFull(ip) orelse continue, key)) return index;
251 }
252 shard.mutate.tracked_inst_map.mutex.lock(io, tid);
253 defer shard.mutate.tracked_inst_map.mutex.unlock(io);
254 if (map.entries != shard.shared.tracked_inst_map.entries) {
255 map = shard.shared.tracked_inst_map;
256 map_mask = map.header().mask();
257 map_index = hash;
258 }
259 while (true) : (map_index += 1) {
260 map_index &= map_mask;
261 const entry = &map.entries[map_index];
262 const index = entry.acquire().unwrap() orelse break;
263 if (entry.hash != hash) continue;
264 if (std.meta.eql(index.resolveFull(ip) orelse continue, key)) return index;
265 }
266 defer shard.mutate.tracked_inst_map.len += 1;
267 const local = ip.getLocal(tid);
268 const list = local.getMutableTrackedInsts(gpa, io);
269 try list.ensureUnusedCapacity(1);
270 const map_header = map.header().*;
271 if (shard.mutate.tracked_inst_map.len < map_header.capacity * 3 / 5) {
272 const entry = &map.entries[map_index];
273 entry.hash = hash;
274 const index = (TrackedInst.Index.Unwrapped{
275 .tid = tid,
276 .index = list.mutate.len,
277 }).wrap(ip);
278 list.appendAssumeCapacity(.{maybe_lost_key});
279 entry.release(index.toOptional());
280 return index;
281 }
282 const arena_state = &local.mutate.arena;
283 var arena = arena_state.promote(gpa);
284 defer arena_state.* = arena.state;
285 const new_map_capacity = map_header.capacity * 2;
286 const new_map_buf = try arena.allocator().alignedAlloc(
287 u8,
288 .fromByteUnits(Map.alignment),
289 Map.entries_offset + new_map_capacity * @sizeOf(Map.Entry),
290 );
291 const new_map: Map = .{ .entries = @ptrCast(new_map_buf[Map.entries_offset..].ptr) };
292 new_map.header().* = .{ .capacity = new_map_capacity };
293 @memset(new_map.entries[0..new_map_capacity], .{ .value = .none, .hash = undefined });
294 const new_map_mask = new_map.header().mask();
295 map_index = 0;
296 while (map_index < map_header.capacity) : (map_index += 1) {
297 const entry = &map.entries[map_index];
298 const index = entry.value.unwrap() orelse continue;
299 const item_hash = entry.hash;
300 var new_map_index = item_hash;
301 while (true) : (new_map_index += 1) {
302 new_map_index &= new_map_mask;
303 const new_entry = &new_map.entries[new_map_index];
304 if (new_entry.value != .none) continue;
305 new_entry.* = .{
306 .value = index.toOptional(),
307 .hash = item_hash,
308 };
309 break;
310 }
311 }
312 map = new_map;
313 map_index = hash;
314 while (true) : (map_index += 1) {
315 map_index &= new_map_mask;
316 if (map.entries[map_index].value == .none) break;
317 }
318 const index = (TrackedInst.Index.Unwrapped{
319 .tid = tid,
320 .index = list.mutate.len,
321 }).wrap(ip);
322 list.appendAssumeCapacity(.{maybe_lost_key});
323 map.entries[map_index] = .{ .value = index.toOptional(), .hash = hash };
324 shard.shared.tracked_inst_map.release(new_map);
325 return index;
326}
327
328/// At the start of an incremental update, we update every entry in `tracked_insts` to include
329/// the new ZIR index. Once this is done, we must update the hashmap metadata so that lookups
330/// return correct entries where they already exist.
331pub fn rehashTrackedInsts(
332 ip: *InternPool,
333 gpa: Allocator,
334 io: Io,
335 tid: Zcu.PerThread.Id,
336) Allocator.Error!void {
337 assert(tid == .main); // we shouldn't have any other threads active right now
338
339 // TODO: this function doesn't handle OOM well. What should it do?
340
341 // We don't lock anything, as this function assumes that no other thread is
342 // accessing `tracked_insts`. This is necessary because we're going to be
343 // iterating the `TrackedInst`s in each `Local`, so we have to know that
344 // none will be added as we work.
345
346 // Figure out how big each shard need to be and store it in its mutate `len`.
347 for (ip.shards) |*shard| shard.mutate.tracked_inst_map.len = 0;
348 for (ip.locals) |*local| {
349 // `getMutableTrackedInsts` is okay only because no other thread is currently active.
350 // We need the `mutate` for the len.
351 for (local.getMutableTrackedInsts(gpa, io).viewAllowEmpty().items(.@"0")) |tracked_inst| {
352 if (tracked_inst.inst == .lost) continue; // we can ignore this one!
353 const full_hash = Hash.hash(0, std.mem.asBytes(&tracked_inst));
354 const shard = &ip.shards[@intCast(full_hash & (ip.shards.len - 1))];
355 shard.mutate.tracked_inst_map.len += 1;
356 }
357 }
358
359 const Map = Shard.Map(TrackedInst.Index.Optional);
360
361 const arena_state = &ip.getLocal(tid).mutate.arena;
362
363 // We know how big each shard must be, so ensure we have the capacity we need.
364 for (ip.shards) |*shard| {
365 const want_capacity = if (shard.mutate.tracked_inst_map.len == 0) 0 else cap: {
366 // We need to return a capacity of at least 2 to make sure we don't have the `Map(...).empty` value.
367 // For this reason, note the `+ 1` in the below expression. This matches the behavior of `trackZir`.
368 break :cap std.math.ceilPowerOfTwo(u32, shard.mutate.tracked_inst_map.len * 5 / 3 + 1) catch unreachable;
369 };
370 const have_capacity = shard.shared.tracked_inst_map.header().capacity; // no acquire because we hold the mutex
371 if (have_capacity >= want_capacity) {
372 if (have_capacity == 1) {
373 // The map is `.empty` -- we can't memset the entries, or we'll segfault, because
374 // the buffer is secretly constant.
375 } else {
376 @memset(shard.shared.tracked_inst_map.entries[0..have_capacity], .{ .value = .none, .hash = undefined });
377 }
378 continue;
379 }
380 var arena = arena_state.promote(gpa);
381 defer arena_state.* = arena.state;
382 const new_map_buf = try arena.allocator().alignedAlloc(
383 u8,
384 .fromByteUnits(Map.alignment),
385 Map.entries_offset + want_capacity * @sizeOf(Map.Entry),
386 );
387 const new_map: Map = .{ .entries = @ptrCast(new_map_buf[Map.entries_offset..].ptr) };
388 new_map.header().* = .{ .capacity = want_capacity };
389 @memset(new_map.entries[0..want_capacity], .{ .value = .none, .hash = undefined });
390 shard.shared.tracked_inst_map.release(new_map);
391 }
392
393 // Now, actually insert the items.
394 for (ip.locals, 0..) |*local, local_tid| {
395 // `getMutableTrackedInsts` is okay only because no other thread is currently active.
396 // We need the `mutate` for the len.
397 for (local.getMutableTrackedInsts(gpa, io).viewAllowEmpty().items(.@"0"), 0..) |tracked_inst, local_inst_index| {
398 if (tracked_inst.inst == .lost) continue; // we can ignore this one!
399 const full_hash = Hash.hash(0, std.mem.asBytes(&tracked_inst));
400 const hash: u32 = @truncate(full_hash >> 32);
401 const shard = &ip.shards[@intCast(full_hash & (ip.shards.len - 1))];
402 const map = shard.shared.tracked_inst_map; // no acquire because we hold the mutex
403 const map_mask = map.header().mask();
404 var map_index = hash;
405 const entry = while (true) : (map_index += 1) {
406 map_index &= map_mask;
407 const entry = &map.entries[map_index];
408 if (entry.acquire() == .none) break entry;
409 };
410 const index = TrackedInst.Index.Unwrapped.wrap(.{
411 .tid = @fromBackingInt(@intCast(local_tid)),
412 .index = @intCast(local_inst_index),
413 }, ip);
414 entry.hash = hash;
415 entry.release(index.toOptional());
416 }
417 }
418}
419
420/// Analysis Unit. Represents a single entity which undergoes semantic analysis.
421/// This is the "source" of an incremental dependency edge.
422pub const AnalUnit = packed struct(u64) {
423 kind: Kind,
424 id: u32,
425
426 pub const Kind = enum(u32) {
427 @"comptime",
428 nav_val,
429 nav_ty,
430 type_layout,
431 struct_defaults,
432 func,
433 memoized_state,
434 };
435
436 pub const Unwrapped = union(Kind) {
437 /// This `AnalUnit` analyzes the body of the given `comptime` declaration.
438 @"comptime": ComptimeUnit.Id,
439 /// This `AnalUnit` resolves the value of the given `Nav`.
440 nav_val: Nav.Index,
441 /// This `AnalUnit` resolves the type of the given `Nav`.
442 nav_ty: Nav.Index,
443 /// This `AnalUnit` resolves the layout of the given `struct`, `union`, or `enum` type.
444 type_layout: InternPool.Index,
445 /// This `AnalUnit` resolves the default field values of the given `struct` type.
446 struct_defaults: InternPool.Index,
447 /// This `AnalUnit` analyzes the body of the given runtime function.
448 func: InternPool.Index,
449 /// This `AnalUnit` resolves all state which is memoized in fields on `Zcu`.
450 memoized_state: MemoizedStateStage,
451 };
452
453 pub fn unwrap(au: AnalUnit) Unwrapped {
454 return switch (au.kind) {
455 inline else => |tag| @unionInit(
456 Unwrapped,
457 @tagName(tag),
458 @fromBackingInt(@intCast(au.id)),
459 ),
460 };
461 }
462 pub fn wrap(raw: Unwrapped) AnalUnit {
463 return switch (raw) {
464 inline else => |id, tag| .{
465 .kind = tag,
466 .id = @backingInt(id),
467 },
468 };
469 }
470
471 pub fn toOptional(as: AnalUnit) Optional {
472 return @fromBackingInt(@intCast(@as(u64, @bitCast(as))));
473 }
474 pub const Optional = enum(u64) {
475 none = std.math.maxInt(u64),
476 _,
477 pub fn unwrap(opt: Optional) ?AnalUnit {
478 return switch (opt) {
479 .none => null,
480 _ => @bitCast(@backingInt(opt)),
481 };
482 }
483 };
484};
485
486pub const MemoizedStateStage = enum(u32) {
487 /// Everything other than panics and `VaList`.
488 main,
489 /// Everything within `std.lang.Panic`.
490 /// Since the panic handler is user-provided, this must be able to reference the other memoized state.
491 panic,
492 /// Specifically `std.lang.VaList`. See `Zcu.StdLangDecl.stage`.
493 va_list,
494 /// Everything within `std.lang.assembly`. See `Zcu.StdLangDecl.stage`.
495 assembly,
496};
497
498pub const ComptimeUnit = extern struct {
499 zir_index: TrackedInst.Index,
500 namespace: NamespaceIndex,
501
502 comptime {
503 assert(std.meta.hasUniqueRepresentation(ComptimeUnit));
504 }
505
506 pub const Id = enum(u32) {
507 _,
508 const Unwrapped = struct {
509 tid: Zcu.PerThread.Id,
510 index: u32,
511 fn wrap(unwrapped: Unwrapped, ip: *const InternPool) ComptimeUnit.Id {
512 assert(@backingInt(unwrapped.tid) <= ip.getTidMask());
513 assert(unwrapped.index <= ip.getIndexMask(u32));
514 return @fromBackingInt(@intCast(@shlExact(@as(u32, @backingInt(unwrapped.tid)), ip.tid_shift_32) |
515 unwrapped.index));
516 }
517 };
518 fn unwrap(id: Id, ip: *const InternPool) Unwrapped {
519 return .{
520 .tid = @fromBackingInt(@intCast(@backingInt(id) >> ip.tid_shift_32 & ip.getTidMask())),
521 .index = @backingInt(id) & ip.getIndexMask(u31),
522 };
523 }
524
525 const debug_state = InternPool.debug_state;
526 };
527};
528
529/// Named Addressable Value. Represents a global value with a name and address. This name may be
530/// generated, and the type (and hence address) may be comptime-only. A `Nav` whose type has runtime
531/// bits is sent to the linker to be emitted to the binary.
532///
533/// * Every ZIR `declaration` which is not a `comptime` declaration has a `Nav` (post-instantiation)
534/// which stores the declaration's resolved value.
535/// * Generic instances have a `Nav` corresponding to the instantiated function.
536/// * `@extern` calls create a `Nav` whose value is a `.@"extern"`.
537///
538/// This data structure is optimized for the `analysis_info != null` case, because this is much more
539/// common in practice; the other case is used only for externs and for generic instances. At the time
540/// of writing, in the compiler itself, around 74% of all `Nav`s have `analysis_info != null`.
541/// (Specifically, 104225 / 140923)
542///
543/// `Nav.Repr` is the in-memory representation.
544pub const Nav = struct {
545 /// The unqualified name of this `Nav`. Namespace lookups use this name, and error messages may use it.
546 /// Additionally, extern `Nav`s (i.e. those whose value is an `extern`) use this name.
547 name: NullTerminatedString,
548 /// The fully-qualified name of this `Nav`.
549 fqn: NullTerminatedString,
550 /// This field is populated iff this `Nav` is resolved by semantic analysis.
551 /// If this is `null`, then `resolved` is *not* `null`.
552 analysis: ?struct {
553 namespace: NamespaceIndex,
554 zir_index: TrackedInst.Index,
555 /// Initially `false`. Set to `true` by `setWantNavAnalysis`.
556 wanted: bool,
557 },
558 /// If this is `null`, then `analysis` is *not* `null`, and semantic analysis is required to
559 /// resolve the type and value of this `Nav`. Otherwise, the type is resolved---therefore,
560 /// `Nav.resolved.?.type` is never `.none`. However, the *value* may not be resolved yet even
561 /// if this field is not `null`---see `Resolved.value` for details.
562 resolved: ?Resolved,
563
564 pub const Resolved = struct {
565 /// This is never `.none`
566 type: InternPool.Index,
567 @"align": Alignment,
568 @"linksection": OptionalNullTerminatedString,
569 @"addrspace": std.lang.AddressSpace,
570 @"const": bool,
571 @"threadlocal": bool,
572 /// This field is whether this `Nav` is a literal `extern` definition.
573 /// It does *not* tell you whether this might alias an extern fn (see #21027).
574 is_extern_decl: bool,
575 /// If the type is resolved but not the value, this is `.none`. In that case, the value will
576 /// be resolved by semantic analysis, so `Nav.analysis` is definitely not `null`.
577 ///
578 /// If this is an extern, the special key `Key.@"extern"` is used.
579 ///
580 /// If this is a variable (`Resolved.@"const" == false`) and not an extern, then this value
581 /// is the global variable's initializer; the value loaded from the variable at runtime may
582 /// of course be different.
583 value: InternPool.Index,
584 };
585
586 /// If the value of this `Nav` is resolved and is an extern, returns the `Key.Extern`. If the
587 /// value is *not* an extern, *or* if the value is not yet resolved (only the type is), returns
588 /// `null`.
589 ///
590 /// This logic works because the frontend ensures that if a `Nav` *might* be extern, its value
591 /// is resolved more eagerly (see logic in `Sema.analyzeNavRefInner`). Therefore, if we see that
592 /// the value is not yet resolved, we know the frontend determined that the `Nav` is definitely
593 /// *not* extern.
594 ///
595 /// This function is only intended be used by the compiler backend (codegen/link). The guarantee
596 /// mentioned above does not necessarily hold in the compiler frontend (if we haven't reached
597 /// `Sema.analyzeNavRefInner` yet).
598 ///
599 /// Asserts that `nav.resolved != null`.
600 pub fn getExtern(nav: Nav, ip: *const InternPool) ?Key.Extern {
601 const r = nav.resolved.?;
602 if (r.value == .none) return null;
603 return switch (ip.indexToKey(r.value)) {
604 .@"extern" => |e| e,
605 else => null,
606 };
607 }
608
609 /// Get the ZIR instruction corresponding to this `Nav`, used to resolve source locations.
610 /// This is a `declaration`.
611 pub fn srcInst(nav: Nav, ip: *const InternPool) TrackedInst.Index {
612 if (nav.analysis) |a| {
613 return a.zir_index;
614 }
615 // A `Nav` which does not undergo analysis always has a resolved value.
616 return switch (ip.indexToKey(nav.resolved.?.value)) {
617 .func => |func| {
618 // Since `analysis` was not populated, this must be an instantiation.
619 // Go up to the generic owner and consult *its* `analysis` field.
620 const go_nav = ip.getNav(ip.indexToKey(func.generic_owner).func.owner_nav);
621 return go_nav.analysis.?.zir_index;
622 },
623 .@"extern" => |@"extern"| @"extern".zir_index, // extern / @extern
624 else => unreachable,
625 };
626 }
627
628 pub const Index = enum(u32) {
629 _,
630 pub const Optional = enum(u32) {
631 none = std.math.maxInt(u32),
632 _,
633 pub fn unwrap(opt: Optional) ?Nav.Index {
634 return switch (opt) {
635 .none => null,
636 _ => @fromBackingInt(@intCast(@backingInt(opt))),
637 };
638 }
639
640 const debug_state = InternPool.debug_state;
641 };
642 pub fn toOptional(i: Nav.Index) Optional {
643 return @fromBackingInt(@intCast(@backingInt(i)));
644 }
645 const Unwrapped = struct {
646 tid: Zcu.PerThread.Id,
647 index: u32,
648
649 fn wrap(unwrapped: Unwrapped, ip: *const InternPool) Nav.Index {
650 assert(@backingInt(unwrapped.tid) <= ip.getTidMask());
651 assert(unwrapped.index <= ip.getIndexMask(u30));
652 return @fromBackingInt(@intCast(@shlExact(@as(u32, @backingInt(unwrapped.tid)), ip.tid_shift_30) |
653 unwrapped.index));
654 }
655 };
656 fn unwrap(nav_index: Nav.Index, ip: *const InternPool) Unwrapped {
657 return .{
658 .tid = @fromBackingInt(@intCast(@backingInt(nav_index) >> ip.tid_shift_30 & ip.getTidMask())),
659 .index = @backingInt(nav_index) & ip.getIndexMask(u30),
660 };
661 }
662
663 const debug_state = InternPool.debug_state;
664 };
665
666 /// The compact in-memory representation of a `Nav`.
667 /// 30 bytes.
668 const Repr = struct {
669 name: NullTerminatedString,
670 fqn: NullTerminatedString,
671 // The following 2 fields are either both populated, or both `.none`.
672 analysis_namespace: OptionalNamespaceIndex,
673 analysis_zir_index: TrackedInst.Index.Optional,
674 type: InternPool.Index,
675 value: InternPool.Index,
676 @"linksection": OptionalNullTerminatedString,
677 bits: Bits,
678
679 const Bits = packed struct(u16) {
680 @"align": Alignment,
681 @"addrspace": std.lang.AddressSpace,
682 @"const": bool,
683 @"threadlocal": bool,
684 is_extern_decl: bool,
685 want_analysis: bool,
686 _: u1 = 0,
687 };
688
689 fn unpack(repr: Repr) Nav {
690 return .{
691 .name = repr.name,
692 .fqn = repr.fqn,
693 .analysis = if (repr.analysis_namespace.unwrap()) |namespace| .{
694 .namespace = namespace,
695 .zir_index = repr.analysis_zir_index.unwrap().?,
696 .wanted = repr.bits.want_analysis,
697 } else a: {
698 assert(repr.analysis_zir_index == .none);
699 break :a null;
700 },
701 .resolved = if (repr.type == .none) null else .{
702 .type = repr.type,
703 .@"align" = repr.bits.@"align",
704 .@"linksection" = repr.@"linksection",
705 .@"addrspace" = repr.bits.@"addrspace",
706 .@"const" = repr.bits.@"const",
707 .@"threadlocal" = repr.bits.@"threadlocal",
708 .is_extern_decl = repr.bits.is_extern_decl,
709 .value = repr.value,
710 },
711 };
712 }
713 };
714
715 fn pack(nav: Nav) Repr {
716 // Note that even if `nav.resolved == null`, we do not set any fields to `undefined`, even
717 // though they should not be used. This is to avoid writing undefined bytes to disk when
718 // serializing buffers.
719 return .{
720 .name = nav.name,
721 .fqn = nav.fqn,
722 .analysis_namespace = if (nav.analysis) |a| a.namespace.toOptional() else .none,
723 .analysis_zir_index = if (nav.analysis) |a| a.zir_index.toOptional() else .none,
724 .type = if (nav.resolved) |r| r.type else .none,
725 .value = if (nav.resolved) |r| r.value else .none,
726 .@"linksection" = if (nav.resolved) |r| r.@"linksection" else .none,
727 .bits = if (nav.resolved) |r| .{
728 .@"align" = r.@"align",
729 .@"addrspace" = r.@"addrspace",
730 .@"const" = r.@"const",
731 .@"threadlocal" = r.@"threadlocal",
732 .is_extern_decl = r.is_extern_decl,
733 .want_analysis = if (nav.analysis) |a| a.wanted else false,
734 } else .{
735 .@"align" = .none,
736 .@"addrspace" = .generic,
737 .@"const" = false,
738 .@"threadlocal" = false,
739 .is_extern_decl = false,
740 .want_analysis = if (nav.analysis) |a| a.wanted else false,
741 },
742 };
743 }
744};
745
746pub const Dependee = union(enum) {
747 src_hash: TrackedInst.Index,
748 nav_val: Nav.Index,
749 nav_ty: Nav.Index,
750 /// Index is the function, not its IES.
751 func_ies: Index,
752 type_layout: Index,
753 struct_defaults: Index,
754 source_file: FileIndex,
755 embed_file: Zcu.EmbedFile.Index,
756 namespace: TrackedInst.Index,
757 namespace_name: NamespaceNameKey,
758 memoized_state: MemoizedStateStage,
759};
760
761pub fn removeDependenciesForDepender(ip: *InternPool, gpa: Allocator, depender: AnalUnit) void {
762 var opt_idx = (ip.first_dependency.fetchSwapRemove(depender) orelse return).value.toOptional();
763
764 while (opt_idx.unwrap()) |idx| {
765 const dep = ip.dep_entries.items[@backingInt(idx)];
766 opt_idx = dep.next_dependee;
767
768 const prev_idx = dep.prev.unwrap() orelse {
769 // This entry is the start of a list in some `*_deps`.
770 // We cannot easily remove this mapping, so this must remain as a dummy entry.
771 ip.dep_entries.items[@backingInt(idx)].depender = .none;
772 continue;
773 };
774
775 ip.dep_entries.items[@backingInt(prev_idx)].next = dep.next;
776 if (dep.next.unwrap()) |next_idx| {
777 ip.dep_entries.items[@backingInt(next_idx)].prev = dep.prev;
778 }
779
780 ip.free_dep_entries.append(gpa, idx) catch {
781 // This memory will be reclaimed on the next garbage collection.
782 // Thus, we do not need to propagate this error.
783 };
784 }
785}
786
787pub const DependencyIterator = struct {
788 ip: *const InternPool,
789 next_entry: DepEntry.Index.Optional,
790 pub fn next(it: *DependencyIterator) ?AnalUnit {
791 while (true) {
792 const idx = it.next_entry.unwrap() orelse return null;
793 const entry = it.ip.dep_entries.items[@backingInt(idx)];
794 it.next_entry = entry.next;
795 if (entry.depender.unwrap()) |depender| return depender;
796 }
797 }
798};
799
800pub fn dependencyIterator(ip: *const InternPool, dependee: Dependee) DependencyIterator {
801 const first_entry = switch (dependee) {
802 .src_hash => |x| ip.src_hash_deps.get(x),
803 .nav_val => |x| ip.nav_val_deps.get(x),
804 .nav_ty => |x| ip.nav_ty_deps.get(x),
805 .func_ies => |x| ip.func_ies_deps.get(x),
806 .type_layout => |x| ip.type_layout_deps.get(x),
807 .struct_defaults => |x| ip.struct_defaults_deps.get(x),
808 .source_file => |x| ip.source_file_deps.get(x),
809 .embed_file => |x| ip.embed_file_deps.get(x),
810 .namespace => |x| ip.namespace_deps.get(x),
811 .namespace_name => |x| ip.namespace_name_deps.get(x),
812 .memoized_state => |stage| switch (stage) {
813 .main => ip.memoized_state_main_deps.unwrap(),
814 .panic => ip.memoized_state_panic_deps.unwrap(),
815 .va_list => ip.memoized_state_va_list_deps.unwrap(),
816 .assembly => ip.memoized_state_assembly_deps.unwrap(),
817 },
818 } orelse return .{
819 .ip = ip,
820 .next_entry = .none,
821 };
822 return .{
823 .ip = ip,
824 .next_entry = first_entry.toOptional(),
825 };
826}
827
828pub fn addDependency(ip: *InternPool, gpa: Allocator, depender: AnalUnit, dependee: Dependee) Allocator.Error!void {
829 const first_depender_dep: DepEntry.Index.Optional = if (ip.first_dependency.get(depender)) |idx| dep: {
830 // The entry already exists, so there is capacity to overwrite it later.
831 break :dep idx.toOptional();
832 } else none: {
833 // Ensure there is capacity available to add this dependency later.
834 try ip.first_dependency.ensureUnusedCapacity(gpa, 1);
835 break :none .none;
836 };
837
838 // We're very likely to need space for a new entry - reserve it now to avoid
839 // the need for error cleanup logic.
840 if (ip.free_dep_entries.items.len == 0) {
841 try ip.dep_entries.ensureUnusedCapacity(gpa, 1);
842 }
843
844 // This block should allocate an entry and prepend it to the relevant `*_deps` list.
845 // The `next` field should be correctly initialized; all other fields may be undefined.
846 const new_index: DepEntry.Index = switch (dependee) {
847 .memoized_state => |stage| new_index: {
848 const deps = switch (stage) {
849 .main => &ip.memoized_state_main_deps,
850 .panic => &ip.memoized_state_panic_deps,
851 .va_list => &ip.memoized_state_va_list_deps,
852 .assembly => &ip.memoized_state_assembly_deps,
853 };
854
855 if (deps.unwrap()) |first| {
856 if (ip.dep_entries.items[@backingInt(first)].depender == .none) {
857 // Dummy entry, so we can reuse it rather than allocating a new one!
858 break :new_index first;
859 }
860 }
861
862 // Prepend a new dependency.
863 const new_index: DepEntry.Index, const ptr = if (ip.free_dep_entries.pop()) |new_index| new: {
864 break :new .{ new_index, &ip.dep_entries.items[@backingInt(new_index)] };
865 } else .{ @fromBackingInt(@intCast(ip.dep_entries.items.len)), ip.dep_entries.addOneAssumeCapacity() };
866 if (deps.unwrap()) |old_first| {
867 ptr.next = old_first.toOptional();
868 ip.dep_entries.items[@backingInt(old_first)].prev = new_index.toOptional();
869 } else {
870 ptr.next = .none;
871 }
872 deps.* = new_index.toOptional();
873 break :new_index new_index;
874 },
875 inline else => |dependee_payload, tag| new_index: {
876 const gop = try switch (tag) {
877 .src_hash => ip.src_hash_deps,
878 .nav_val => ip.nav_val_deps,
879 .nav_ty => ip.nav_ty_deps,
880 .func_ies => ip.func_ies_deps,
881 .type_layout => ip.type_layout_deps,
882 .struct_defaults => ip.struct_defaults_deps,
883 .source_file => ip.source_file_deps,
884 .embed_file => ip.embed_file_deps,
885 .namespace => ip.namespace_deps,
886 .namespace_name => ip.namespace_name_deps,
887 .memoized_state => comptime unreachable,
888 }.getOrPut(gpa, dependee_payload);
889
890 if (gop.found_existing and ip.dep_entries.items[@backingInt(gop.value_ptr.*)].depender == .none) {
891 // Dummy entry, so we can reuse it rather than allocating a new one!
892 break :new_index gop.value_ptr.*;
893 }
894
895 // Prepend a new dependency.
896 const new_index: DepEntry.Index, const ptr = if (ip.free_dep_entries.pop()) |new_index| new: {
897 break :new .{ new_index, &ip.dep_entries.items[@backingInt(new_index)] };
898 } else .{ @fromBackingInt(@intCast(ip.dep_entries.items.len)), ip.dep_entries.addOneAssumeCapacity() };
899 if (gop.found_existing) {
900 ptr.next = gop.value_ptr.*.toOptional();
901 ip.dep_entries.items[@backingInt(gop.value_ptr.*)].prev = new_index.toOptional();
902 } else {
903 ptr.next = .none;
904 }
905 gop.value_ptr.* = new_index;
906 break :new_index new_index;
907 },
908 };
909
910 ip.dep_entries.items[@backingInt(new_index)].depender = depender.toOptional();
911 ip.dep_entries.items[@backingInt(new_index)].prev = .none;
912 ip.dep_entries.items[@backingInt(new_index)].next_dependee = first_depender_dep;
913 ip.first_dependency.putAssumeCapacity(depender, new_index);
914}
915
916/// String is the name whose existence the dependency is on.
917/// DepEntry.Index refers to the first such dependency.
918pub const NamespaceNameKey = struct {
919 /// The instruction (`struct_decl` etc) which owns the namespace in question.
920 namespace: TrackedInst.Index,
921 /// The name whose existence the dependency is on.
922 name: NullTerminatedString,
923};
924
925pub const DepEntry = extern struct {
926 /// If null, this is a dummy entry. `next_dependee` is undefined. This is the first
927 /// entry in one of `*_deps`, and does not appear in any list by `first_dependency`,
928 /// but is not in `free_dep_entries` since `*_deps` stores a reference to it.
929 depender: AnalUnit.Optional,
930 /// Index into `dep_entries` forming a doubly linked list of all dependencies on this dependee.
931 /// Used to iterate all dependers for a given dependee during an update.
932 /// null if this is the end of the list.
933 next: DepEntry.Index.Optional,
934 /// The other link for `next`.
935 /// null if this is the start of the list.
936 prev: DepEntry.Index.Optional,
937 /// Index into `dep_entries` forming a singly linked list of dependencies *of* `depender`.
938 /// Used to efficiently remove all `DepEntry`s for a single `depender` when it is re-analyzed.
939 /// null if this is the end of the list.
940 next_dependee: DepEntry.Index.Optional,
941
942 pub const Index = enum(u32) {
943 _,
944 pub fn toOptional(dep: DepEntry.Index) Optional {
945 return @fromBackingInt(@intCast(@backingInt(dep)));
946 }
947 pub const Optional = enum(u32) {
948 none = std.math.maxInt(u32),
949 _,
950 pub fn unwrap(opt: Optional) ?DepEntry.Index {
951 return switch (opt) {
952 .none => null,
953 _ => @fromBackingInt(@intCast(@backingInt(opt))),
954 };
955 }
956 };
957 };
958};
959
960const Local = struct {
961 /// These fields can be accessed from any thread by calling `acquire`.
962 /// They are only modified by the owning thread.
963 shared: Shared align(std.atomic.cache_line),
964 /// This state is fully local to the owning thread and does not require any
965 /// atomic access.
966 mutate: struct {
967 /// When we need to allocate any long-lived buffer for mutating the `InternPool`, it is
968 /// allocated into this `arena` (for the `Id` of the thread performing the mutation). An
969 /// arena is used to avoid contention on the GPA, and to ensure that any code which retains
970 /// references to old state remains valid. For instance, when reallocing hashmap metadata,
971 /// a racing lookup on another thread may still retain a handle to the old metadata pointer,
972 /// so it must remain valid.
973 /// This arena's lifetime is tied to that of `Compilation`, although it can be cleared on
974 /// garbage collection (currently vaporware).
975 arena: std.heap.ArenaAllocator.State,
976
977 items: ListMutate,
978 extra: ListMutate,
979 limbs: ListMutate,
980 strings: ListMutate,
981 string_bytes: ListMutate,
982 tracked_insts: ListMutate,
983 files: ListMutate,
984 maps: ListMutate,
985 navs: ListMutate,
986 comptime_units: ListMutate,
987
988 namespaces: BucketListMutate,
989 } align(std.atomic.cache_line),
990
991 const Shared = struct {
992 items: List(Item),
993 extra: Extra,
994 limbs: Limbs,
995 strings: Strings,
996 string_bytes: StringBytes,
997 tracked_insts: TrackedInsts,
998 files: List(File),
999 maps: Maps,
1000 navs: Navs,
1001 comptime_units: ComptimeUnits,
1002
1003 namespaces: Namespaces,
1004
1005 pub fn getLimbs(shared: *const Local.Shared) Limbs {
1006 return switch (@sizeOf(Limb)) {
1007 @sizeOf(u32) => shared.extra,
1008 @sizeOf(u64) => shared.limbs,
1009 else => @compileError("unsupported host"),
1010 }.acquire();
1011 }
1012 };
1013
1014 const Extra = List(struct { u32 });
1015 const Limbs = switch (@sizeOf(Limb)) {
1016 @sizeOf(u32) => Extra,
1017 @sizeOf(u64) => List(struct { u64 }),
1018 else => @compileError("unsupported host"),
1019 };
1020 const Strings = List(struct { u32 });
1021 const StringBytes = List(struct { u8 });
1022 const TrackedInsts = List(struct { TrackedInst.MaybeLost });
1023 const Maps = List(struct { FieldMap });
1024 const Navs = List(Nav.Repr);
1025 const ComptimeUnits = List(struct { ComptimeUnit });
1026
1027 const namespaces_bucket_width = 8;
1028 const namespaces_bucket_mask = (1 << namespaces_bucket_width) - 1;
1029 const namespace_next_free_field = "owner_type";
1030 const Namespaces = List(struct { *[1 << namespaces_bucket_width]Zcu.Namespace });
1031
1032 const ListMutate = struct {
1033 mutex: Io.Mutex,
1034 len: u32,
1035
1036 const empty: ListMutate = .{
1037 .mutex = .init,
1038 .len = 0,
1039 };
1040 };
1041
1042 const BucketListMutate = struct {
1043 last_bucket_len: u32,
1044 buckets_list: ListMutate,
1045 free_list: u32,
1046
1047 const free_list_sentinel = std.math.maxInt(u32);
1048
1049 const empty: BucketListMutate = .{
1050 .last_bucket_len = 0,
1051 .buckets_list = ListMutate.empty,
1052 .free_list = free_list_sentinel,
1053 };
1054 };
1055
1056 fn List(comptime Elem: type) type {
1057 assert(@typeInfo(Elem) == .@"struct");
1058 return struct {
1059 bytes: [*]align(@alignOf(Elem)) u8,
1060
1061 const ListSelf = @This();
1062 const Mutable = struct {
1063 gpa: Allocator,
1064 io: Io,
1065 arena: *std.heap.ArenaAllocator.State,
1066 mutate: *ListMutate,
1067 list: *ListSelf,
1068
1069 const fields = std.enums.values(std.meta.FieldEnum(Elem));
1070
1071 fn PtrArrayElem(comptime len: usize) type {
1072 const elem_info = @typeInfo(Elem).@"struct";
1073
1074 var new_types: [elem_info.field_types.len]type = undefined;
1075 for (&new_types, elem_info.field_types) |*NewType, elem_field_type| {
1076 NewType.* = *[len]elem_field_type;
1077 }
1078 if (elem_info.is_tuple) {
1079 return @Tuple(&new_types);
1080 } else {
1081 return @Struct(.auto, null, elem_info.field_names, &new_types, &@splat(.{}));
1082 }
1083 }
1084 fn PtrElem(comptime opts: struct {
1085 size: std.lang.Type.Pointer.Size,
1086 is_const: bool = false,
1087 }) type {
1088 const elem_info = @typeInfo(Elem).@"struct";
1089 var new_types: [elem_info.field_types.len]type = undefined;
1090 for (&new_types, elem_info.field_types) |*NewType, elem_field_type| {
1091 NewType.* = @Pointer(opts.size, .{ .@"const" = opts.is_const }, elem_field_type, null);
1092 }
1093 if (elem_info.is_tuple) {
1094 return @Tuple(&new_types);
1095 } else {
1096 return @Struct(.auto, null, elem_info.field_names, &new_types, &@splat(.{}));
1097 }
1098 }
1099
1100 pub fn addOne(mutable: Mutable) Allocator.Error!PtrElem(.{ .size = .one }) {
1101 try mutable.ensureUnusedCapacity(1);
1102 return mutable.addOneAssumeCapacity();
1103 }
1104
1105 pub fn addOneAssumeCapacity(mutable: Mutable) PtrElem(.{ .size = .one }) {
1106 const index = mutable.mutate.len;
1107 assert(index < mutable.list.header().capacity);
1108 mutable.mutate.len = index + 1;
1109 const mutable_view = mutable.view().slice();
1110 var ptr: PtrElem(.{ .size = .one }) = undefined;
1111 inline for (fields) |field| {
1112 @field(ptr, @tagName(field)) = &mutable_view.items(field)[index];
1113 }
1114 return ptr;
1115 }
1116
1117 pub fn append(mutable: Mutable, elem: Elem) Allocator.Error!void {
1118 try mutable.ensureUnusedCapacity(1);
1119 mutable.appendAssumeCapacity(elem);
1120 }
1121
1122 pub fn appendAssumeCapacity(mutable: Mutable, elem: Elem) void {
1123 var mutable_view = mutable.view();
1124 defer mutable.mutate.len = @intCast(mutable_view.len);
1125 mutable_view.appendAssumeCapacity(elem);
1126 }
1127
1128 pub fn appendSliceAssumeCapacity(
1129 mutable: Mutable,
1130 slice: PtrElem(.{ .size = .slice, .is_const = true }),
1131 ) void {
1132 if (fields.len == 0) return;
1133 const start = mutable.mutate.len;
1134 const slice_len = @field(slice, @tagName(fields[0])).len;
1135 assert(slice_len <= mutable.list.header().capacity - start);
1136 mutable.mutate.len = @intCast(start + slice_len);
1137 const mutable_view = mutable.view().slice();
1138 inline for (fields) |field| {
1139 const field_slice = @field(slice, @tagName(field));
1140 assert(field_slice.len == slice_len);
1141 @memcpy(mutable_view.items(field)[start..][0..slice_len], field_slice);
1142 }
1143 }
1144
1145 pub fn appendNTimes(mutable: Mutable, elem: Elem, len: usize) Allocator.Error!void {
1146 try mutable.ensureUnusedCapacity(len);
1147 mutable.appendNTimesAssumeCapacity(elem, len);
1148 }
1149
1150 pub fn appendNTimesAssumeCapacity(mutable: Mutable, elem: Elem, len: usize) void {
1151 const start = mutable.mutate.len;
1152 assert(len <= mutable.list.header().capacity - start);
1153 mutable.mutate.len = @intCast(start + len);
1154 const mutable_view = mutable.view().slice();
1155 inline for (fields) |field| {
1156 @memset(mutable_view.items(field)[start..][0..len], @field(elem, @tagName(field)));
1157 }
1158 }
1159
1160 pub fn addManyAsArray(mutable: Mutable, comptime len: usize) Allocator.Error!PtrArrayElem(len) {
1161 try mutable.ensureUnusedCapacity(len);
1162 return mutable.addManyAsArrayAssumeCapacity(len);
1163 }
1164
1165 pub fn addManyAsArrayAssumeCapacity(mutable: Mutable, comptime len: usize) PtrArrayElem(len) {
1166 const start = mutable.mutate.len;
1167 assert(len <= mutable.list.header().capacity - start);
1168 mutable.mutate.len = @intCast(start + len);
1169 const mutable_view = mutable.view().slice();
1170 var ptr_array: PtrArrayElem(len) = undefined;
1171 inline for (fields) |field| {
1172 @field(ptr_array, @tagName(field)) = mutable_view.items(field)[start..][0..len];
1173 }
1174 return ptr_array;
1175 }
1176
1177 pub fn addManyAsSlice(mutable: Mutable, len: usize) Allocator.Error!PtrElem(.{ .size = .slice }) {
1178 try mutable.ensureUnusedCapacity(len);
1179 return mutable.addManyAsSliceAssumeCapacity(len);
1180 }
1181
1182 pub fn addManyAsSliceAssumeCapacity(mutable: Mutable, len: usize) PtrElem(.{ .size = .slice }) {
1183 const start = mutable.mutate.len;
1184 assert(len <= mutable.list.header().capacity - start);
1185 mutable.mutate.len = @intCast(start + len);
1186 const mutable_view = mutable.view().slice();
1187 var slice: PtrElem(.{ .size = .slice }) = undefined;
1188 inline for (fields) |field| {
1189 @field(slice, @tagName(field)) = mutable_view.items(field)[start..][0..len];
1190 }
1191 return slice;
1192 }
1193
1194 pub fn shrinkRetainingCapacity(mutable: Mutable, len: usize) void {
1195 assert(len <= mutable.mutate.len);
1196 mutable.mutate.len = @intCast(len);
1197 }
1198
1199 pub fn ensureUnusedCapacity(mutable: Mutable, unused_capacity: usize) Allocator.Error!void {
1200 try mutable.ensureTotalCapacity(@intCast(mutable.mutate.len + unused_capacity));
1201 }
1202
1203 pub fn ensureTotalCapacity(mutable: Mutable, total_capacity: usize) Allocator.Error!void {
1204 const old_capacity = mutable.list.header().capacity;
1205 if (old_capacity >= total_capacity) return;
1206 var new_capacity = old_capacity;
1207 while (new_capacity < total_capacity) new_capacity = (new_capacity + 10) * 2;
1208 try mutable.setCapacity(new_capacity);
1209 }
1210
1211 fn setCapacity(mutable: Mutable, capacity: u32) Allocator.Error!void {
1212 const io = mutable.io;
1213 var arena = mutable.arena.promote(mutable.gpa);
1214 defer mutable.arena.* = arena.state;
1215 const buf = try arena.allocator().alignedAlloc(
1216 u8,
1217 .fromByteUnits(alignment),
1218 bytes_offset + View.capacityInBytes(capacity),
1219 );
1220 var new_list: ListSelf = .{ .bytes = @ptrCast(buf[bytes_offset..].ptr) };
1221 new_list.header().* = .{ .capacity = capacity };
1222 const len = mutable.mutate.len;
1223 // this cold, quickly predictable, condition enables
1224 // the `MultiArrayList` optimization in `view`
1225 if (len > 0) {
1226 const old_slice = mutable.list.view().slice();
1227 const new_slice = new_list.view().slice();
1228 inline for (fields) |field| @memcpy(new_slice.items(field)[0..len], old_slice.items(field)[0..len]);
1229 }
1230 mutable.mutate.mutex.lockUncancelable(io);
1231 defer mutable.mutate.mutex.unlock(io);
1232 mutable.list.release(new_list);
1233 }
1234
1235 pub fn viewAllowEmpty(mutable: Mutable) View {
1236 const capacity = mutable.list.header().capacity;
1237 return .{
1238 .bytes = mutable.list.bytes,
1239 .len = mutable.mutate.len,
1240 .capacity = capacity,
1241 };
1242 }
1243 pub fn view(mutable: Mutable) View {
1244 const capacity = mutable.list.header().capacity;
1245 assert(capacity > 0); // optimizes `MultiArrayList.Slice.items`
1246 return .{
1247 .bytes = mutable.list.bytes,
1248 .len = mutable.mutate.len,
1249 .capacity = capacity,
1250 };
1251 }
1252 };
1253
1254 const empty: ListSelf = .{ .bytes = @constCast(&(extern struct {
1255 header: Header,
1256 bytes: [0]u8 align(@alignOf(Elem)),
1257 }{
1258 .header = .{ .capacity = 0 },
1259 .bytes = .{},
1260 }).bytes) };
1261
1262 const alignment = @max(@alignOf(Header), @alignOf(Elem));
1263 const bytes_offset = std.mem.alignForward(usize, @sizeOf(Header), @alignOf(Elem));
1264 const View = std.MultiArrayList(Elem);
1265
1266 /// Must be called when accessing from another thread.
1267 pub fn acquire(list: *const ListSelf) ListSelf {
1268 return .{ .bytes = @atomicLoad([*]align(@alignOf(Elem)) u8, &list.bytes, .acquire) };
1269 }
1270 fn release(list: *ListSelf, new_list: ListSelf) void {
1271 @atomicStore([*]align(@alignOf(Elem)) u8, &list.bytes, new_list.bytes, .release);
1272 }
1273
1274 const Header = extern struct {
1275 capacity: u32,
1276 };
1277 fn header(list: ListSelf) *Header {
1278 return @ptrCast(@alignCast(list.bytes - bytes_offset));
1279 }
1280 pub fn view(list: ListSelf) View {
1281 const capacity = list.header().capacity;
1282 assert(capacity > 0); // optimizes `MultiArrayList.Slice.items`
1283 return .{
1284 .bytes = list.bytes,
1285 .len = capacity,
1286 .capacity = capacity,
1287 };
1288 }
1289 };
1290 }
1291
1292 pub fn getMutableItems(local: *Local, gpa: Allocator, io: Io) List(Item).Mutable {
1293 return .{
1294 .gpa = gpa,
1295 .io = io,
1296 .arena = &local.mutate.arena,
1297 .mutate = &local.mutate.items,
1298 .list = &local.shared.items,
1299 };
1300 }
1301
1302 pub fn getMutableExtra(local: *Local, gpa: Allocator, io: Io) Extra.Mutable {
1303 return .{
1304 .gpa = gpa,
1305 .io = io,
1306 .arena = &local.mutate.arena,
1307 .mutate = &local.mutate.extra,
1308 .list = &local.shared.extra,
1309 };
1310 }
1311
1312 /// On 32-bit systems, this array is ignored and extra is used for everything.
1313 /// On 64-bit systems, this array is used for big integers and associated metadata.
1314 /// Use the helper methods instead of accessing this directly in order to not
1315 /// violate the above mechanism.
1316 pub fn getMutableLimbs(local: *Local, gpa: Allocator, io: Io) Limbs.Mutable {
1317 return switch (@sizeOf(Limb)) {
1318 @sizeOf(u32) => local.getMutableExtra(gpa, io),
1319 @sizeOf(u64) => .{
1320 .gpa = gpa,
1321 .io = io,
1322 .arena = &local.mutate.arena,
1323 .mutate = &local.mutate.limbs,
1324 .list = &local.shared.limbs,
1325 },
1326 else => @compileError("unsupported host"),
1327 };
1328 }
1329
1330 /// A list of offsets into `string_bytes` for each string.
1331 pub fn getMutableStrings(local: *Local, gpa: Allocator, io: Io) Strings.Mutable {
1332 return .{
1333 .gpa = gpa,
1334 .io = io,
1335 .arena = &local.mutate.arena,
1336 .mutate = &local.mutate.strings,
1337 .list = &local.shared.strings,
1338 };
1339 }
1340
1341 /// In order to store references to strings in fewer bytes, we copy all
1342 /// string bytes into here. String bytes can be null. It is up to whomever
1343 /// is referencing the data here whether they want to store both index and length,
1344 /// thus allowing null bytes, or store only index, and use null-termination. The
1345 /// `strings_bytes` array is agnostic to either usage.
1346 pub fn getMutableStringBytes(local: *Local, gpa: Allocator, io: Io) StringBytes.Mutable {
1347 return .{
1348 .gpa = gpa,
1349 .io = io,
1350 .arena = &local.mutate.arena,
1351 .mutate = &local.mutate.string_bytes,
1352 .list = &local.shared.string_bytes,
1353 };
1354 }
1355
1356 /// An index into `tracked_insts` gives a reference to a single ZIR instruction which
1357 /// persists across incremental updates.
1358 pub fn getMutableTrackedInsts(local: *Local, gpa: Allocator, io: Io) TrackedInsts.Mutable {
1359 return .{
1360 .gpa = gpa,
1361 .io = io,
1362 .arena = &local.mutate.arena,
1363 .mutate = &local.mutate.tracked_insts,
1364 .list = &local.shared.tracked_insts,
1365 };
1366 }
1367
1368 /// Elements are ordered identically to the `import_table` field of `Zcu`.
1369 ///
1370 /// Unlike `import_table`, this data is serialized as part of incremental
1371 /// compilation state.
1372 ///
1373 /// Key is the hash of the path to this file, used to store
1374 /// `InternPool.TrackedInst`.
1375 pub fn getMutableFiles(local: *Local, gpa: Allocator, io: Io) List(File).Mutable {
1376 return .{
1377 .gpa = gpa,
1378 .io = io,
1379 .arena = &local.mutate.arena,
1380 .mutate = &local.mutate.files,
1381 .list = &local.shared.files,
1382 };
1383 }
1384
1385 /// Some types such as enums, structs, and unions need to store mappings from field names
1386 /// to field index, or value to field index. In such cases, they will store the underlying
1387 /// field names and values directly, relying on one of these maps, stored separately,
1388 /// to provide lookup.
1389 /// These are not serialized; it is computed upon deserialization.
1390 pub fn getMutableMaps(local: *Local, gpa: Allocator, io: Io) Maps.Mutable {
1391 return .{
1392 .gpa = gpa,
1393 .io = io,
1394 .arena = &local.mutate.arena,
1395 .mutate = &local.mutate.maps,
1396 .list = &local.shared.maps,
1397 };
1398 }
1399
1400 pub fn getMutableNavs(local: *Local, gpa: Allocator, io: Io) Navs.Mutable {
1401 return .{
1402 .gpa = gpa,
1403 .io = io,
1404 .arena = &local.mutate.arena,
1405 .mutate = &local.mutate.navs,
1406 .list = &local.shared.navs,
1407 };
1408 }
1409
1410 pub fn getMutableComptimeUnits(local: *Local, gpa: Allocator, io: Io) ComptimeUnits.Mutable {
1411 return .{
1412 .gpa = gpa,
1413 .io = io,
1414 .arena = &local.mutate.arena,
1415 .mutate = &local.mutate.comptime_units,
1416 .list = &local.shared.comptime_units,
1417 };
1418 }
1419
1420 /// Rather than allocating Namespace objects with an Allocator, we instead allocate
1421 /// them with this BucketList. This provides four advantages:
1422 /// * Stable memory so that one thread can access a Namespace object while another
1423 /// thread allocates additional Namespace objects from this list.
1424 /// * It allows us to use u32 indexes to reference Namespace objects rather than
1425 /// pointers, saving memory in types.
1426 /// * Using integers to reference Namespace objects rather than pointers makes
1427 /// serialization trivial.
1428 /// * It provides a unique integer to be used for anonymous symbol names, avoiding
1429 /// multi-threaded contention on an atomic counter.
1430 pub fn getMutableNamespaces(local: *Local, gpa: Allocator, io: Io) Namespaces.Mutable {
1431 return .{
1432 .gpa = gpa,
1433 .io = io,
1434 .arena = &local.mutate.arena,
1435 .mutate = &local.mutate.namespaces.buckets_list,
1436 .list = &local.shared.namespaces,
1437 };
1438 }
1439};
1440
1441pub fn getLocal(ip: *InternPool, tid: Zcu.PerThread.Id) *Local {
1442 return &ip.locals[@backingInt(tid)];
1443}
1444
1445pub fn getLocalShared(ip: *const InternPool, tid: Zcu.PerThread.Id) *const Local.Shared {
1446 return &ip.locals[@backingInt(tid)].shared;
1447}
1448
1449const Shard = struct {
1450 shared: struct {
1451 map: Map(Index),
1452 string_map: Map(OptionalNullTerminatedString),
1453 tracked_inst_map: Map(TrackedInst.Index.Optional),
1454 } align(std.atomic.cache_line),
1455 mutate: struct {
1456 // TODO: measure cost of sharing unrelated mutate state
1457 map: Mutate align(std.atomic.cache_line),
1458 string_map: Mutate align(std.atomic.cache_line),
1459 tracked_inst_map: Mutate align(std.atomic.cache_line),
1460 },
1461
1462 const Mutate = struct {
1463 /// This mutex needs to be recursive because `getFuncDeclIes` interns multiple things at
1464 /// once (the function, its IES, the corresponding error union, and the resulting function
1465 /// type), so calls `getOrPutKeyEnsuringAdditionalCapacity` multiple times. Each of these
1466 /// calls acquires a lock which will only be released when the whole operation is finalized,
1467 /// and these different items could be in the same shard, in which case that shard's lock
1468 /// will be acquired multiple times.
1469 mutex: RecursiveMutex,
1470 len: u32,
1471
1472 const RecursiveMutex = struct {
1473 const OptionalTid = if (single_threaded) enum(u8) {
1474 null,
1475 main,
1476 fn unwrap(ot: OptionalTid) ?Zcu.PerThread.Id {
1477 return switch (ot) {
1478 .null => null,
1479 .main => .main,
1480 };
1481 }
1482 fn wrap(tid: Zcu.PerThread.Id) OptionalTid {
1483 comptime assert(tid == .main);
1484 return .main;
1485 }
1486 } else packed struct(u8) {
1487 non_null: bool,
1488 value: Zcu.PerThread.Id,
1489 const @"null": OptionalTid = .{ .non_null = false, .value = .main };
1490 fn unwrap(ot: OptionalTid) ?Zcu.PerThread.Id {
1491 return if (ot.non_null) ot.value else null;
1492 }
1493 fn wrap(tid: Zcu.PerThread.Id) OptionalTid {
1494 return .{ .non_null = true, .value = tid };
1495 }
1496 };
1497 mutex: Io.Mutex,
1498 tid: std.atomic.Value(OptionalTid),
1499 lock_count: u32,
1500 const init: RecursiveMutex = .{ .mutex = .init, .tid = .init(.null), .lock_count = 0 };
1501 fn lock(r: *RecursiveMutex, io: Io, tid: Zcu.PerThread.Id) void {
1502 if (r.tid.load(.monotonic) != OptionalTid.wrap(tid)) {
1503 r.mutex.lockUncancelable(io);
1504 assert(r.lock_count == 0);
1505 r.tid.store(.wrap(tid), .monotonic);
1506 }
1507 r.lock_count += 1;
1508 }
1509 fn unlock(r: *RecursiveMutex, io: Io) void {
1510 r.lock_count -= 1;
1511 if (r.lock_count == 0) {
1512 r.tid.store(.null, .monotonic);
1513 r.mutex.unlock(io);
1514 }
1515 }
1516 };
1517
1518 const empty: Mutate = .{
1519 .mutex = .init,
1520 .len = 0,
1521 };
1522 };
1523
1524 fn Map(comptime Value: type) type {
1525 comptime assert(@typeInfo(Value).@"enum".tag_type == u32);
1526 _ = @as(Value, .none); // expected .none key
1527 return struct {
1528 /// header: Header,
1529 /// entries: [header.capacity]Entry,
1530 entries: [*]Entry,
1531
1532 const empty: @This() = .{ .entries = @constCast(&(extern struct {
1533 header: Header,
1534 entries: [1]Entry,
1535 }{
1536 .header = .{ .capacity = 1 },
1537 .entries = .{.{ .value = .none, .hash = undefined }},
1538 }).entries) };
1539
1540 const alignment = @max(@alignOf(Header), @alignOf(Entry));
1541 const entries_offset = std.mem.alignForward(usize, @sizeOf(Header), @alignOf(Entry));
1542
1543 /// Must be called unless the mutate mutex is locked.
1544 fn acquire(map: *const @This()) @This() {
1545 return .{ .entries = @atomicLoad([*]Entry, &map.entries, .acquire) };
1546 }
1547 fn release(map: *@This(), new_map: @This()) void {
1548 @atomicStore([*]Entry, &map.entries, new_map.entries, .release);
1549 }
1550
1551 const Header = extern struct {
1552 capacity: u32,
1553
1554 fn mask(head: *const Header) u32 {
1555 assert(std.math.isPowerOfTwo(head.capacity));
1556 return head.capacity - 1;
1557 }
1558 };
1559 fn header(map: @This()) *Header {
1560 return @ptrCast(@alignCast(@as([*]u8, @ptrCast(map.entries)) - entries_offset));
1561 }
1562
1563 const Entry = extern struct {
1564 value: Value,
1565 hash: u32,
1566
1567 fn acquire(entry: *const Entry) Value {
1568 return @atomicLoad(Value, &entry.value, .acquire);
1569 }
1570 fn release(entry: *Entry, value: Value) void {
1571 assert(value != .none);
1572 @atomicStore(Value, &entry.value, value, .release);
1573 }
1574 fn resetUnordered(entry: *Entry) void {
1575 @atomicStore(Value, &entry.value, .none, .unordered);
1576 }
1577 };
1578 };
1579 }
1580};
1581
1582fn getTidMask(ip: *const InternPool) u32 {
1583 return @shlExact(@as(u32, 1), ip.tid_width) - 1;
1584}
1585
1586fn getIndexMask(ip: *const InternPool, comptime BackingInt: type) u32 {
1587 return @as(u32, std.math.maxInt(BackingInt)) >> ip.tid_width;
1588}
1589
1590const FieldMap = std.array_hash_map.Custom(void, void, std.array_hash_map.AutoContext(void), false);
1591
1592/// An index into `maps` which might be `none`.
1593pub const OptionalMapIndex = enum(u32) {
1594 none = std.math.maxInt(u32),
1595 _,
1596
1597 pub fn unwrap(oi: OptionalMapIndex) ?MapIndex {
1598 if (oi == .none) return null;
1599 return @fromBackingInt(@intCast(@backingInt(oi)));
1600 }
1601};
1602
1603/// An index into `maps`.
1604pub const MapIndex = enum(u32) {
1605 _,
1606
1607 pub fn get(map_index: MapIndex, ip: *const InternPool) *FieldMap {
1608 const unwrapped_map_index = map_index.unwrap(ip);
1609 const maps = ip.getLocalShared(unwrapped_map_index.tid).maps.acquire();
1610 return &maps.view().items(.@"0")[unwrapped_map_index.index];
1611 }
1612
1613 pub fn toOptional(i: MapIndex) OptionalMapIndex {
1614 return @fromBackingInt(@intCast(@backingInt(i)));
1615 }
1616
1617 const Unwrapped = struct {
1618 tid: Zcu.PerThread.Id,
1619 index: u32,
1620
1621 fn wrap(unwrapped: Unwrapped, ip: *const InternPool) MapIndex {
1622 assert(@backingInt(unwrapped.tid) <= ip.getTidMask());
1623 assert(unwrapped.index <= ip.getIndexMask(u32));
1624 return @fromBackingInt(@intCast(@shlExact(@as(u32, @backingInt(unwrapped.tid)), ip.tid_shift_32) |
1625 unwrapped.index));
1626 }
1627 };
1628 fn unwrap(map_index: MapIndex, ip: *const InternPool) Unwrapped {
1629 return .{
1630 .tid = @fromBackingInt(@intCast(@backingInt(map_index) >> ip.tid_shift_32 & ip.getTidMask())),
1631 .index = @backingInt(map_index) & ip.getIndexMask(u32),
1632 };
1633 }
1634};
1635
1636pub const ComptimeAllocIndex = enum(u32) { _ };
1637
1638pub const NamespaceIndex = enum(u32) {
1639 _,
1640
1641 const Unwrapped = struct {
1642 tid: Zcu.PerThread.Id,
1643 bucket_index: u32,
1644 index: u32,
1645
1646 fn wrap(unwrapped: Unwrapped, ip: *const InternPool) NamespaceIndex {
1647 assert(@backingInt(unwrapped.tid) <= ip.getTidMask());
1648 assert(unwrapped.bucket_index <= ip.getIndexMask(u32) >> Local.namespaces_bucket_width);
1649 assert(unwrapped.index <= Local.namespaces_bucket_mask);
1650 return @fromBackingInt(@intCast(@shlExact(@as(u32, @backingInt(unwrapped.tid)), ip.tid_shift_32) |
1651 unwrapped.bucket_index << Local.namespaces_bucket_width |
1652 unwrapped.index));
1653 }
1654 };
1655 fn unwrap(namespace_index: NamespaceIndex, ip: *const InternPool) Unwrapped {
1656 const index = @backingInt(namespace_index) & ip.getIndexMask(u32);
1657 return .{
1658 .tid = @fromBackingInt(@intCast(@backingInt(namespace_index) >> ip.tid_shift_32 & ip.getTidMask())),
1659 .bucket_index = index >> Local.namespaces_bucket_width,
1660 .index = index & Local.namespaces_bucket_mask,
1661 };
1662 }
1663
1664 pub fn toOptional(i: NamespaceIndex) OptionalNamespaceIndex {
1665 return @fromBackingInt(@intCast(@backingInt(i)));
1666 }
1667};
1668
1669pub const OptionalNamespaceIndex = enum(u32) {
1670 none = std.math.maxInt(u32),
1671 _,
1672
1673 pub fn init(oi: ?NamespaceIndex) OptionalNamespaceIndex {
1674 return @fromBackingInt(@intCast(@backingInt(oi orelse return .none)));
1675 }
1676
1677 pub fn unwrap(oi: OptionalNamespaceIndex) ?NamespaceIndex {
1678 if (oi == .none) return null;
1679 return @fromBackingInt(@intCast(@backingInt(oi)));
1680 }
1681};
1682
1683pub const FileIndex = enum(u32) {
1684 _,
1685
1686 const Unwrapped = struct {
1687 tid: Zcu.PerThread.Id,
1688 index: u32,
1689
1690 fn wrap(unwrapped: Unwrapped, ip: *const InternPool) FileIndex {
1691 assert(@backingInt(unwrapped.tid) <= ip.getTidMask());
1692 assert(unwrapped.index <= ip.getIndexMask(u32));
1693 return @fromBackingInt(@intCast(@shlExact(@as(u32, @backingInt(unwrapped.tid)), ip.tid_shift_32) |
1694 unwrapped.index));
1695 }
1696 };
1697 pub fn unwrap(file_index: FileIndex, ip: *const InternPool) Unwrapped {
1698 return .{
1699 .tid = @fromBackingInt(@intCast(@backingInt(file_index) >> ip.tid_shift_32 & ip.getTidMask())),
1700 .index = @backingInt(file_index) & ip.getIndexMask(u32),
1701 };
1702 }
1703 pub fn toOptional(i: FileIndex) Optional {
1704 return @fromBackingInt(@intCast(@backingInt(i)));
1705 }
1706 pub const Optional = enum(u32) {
1707 none = std.math.maxInt(u32),
1708 _,
1709 pub fn unwrap(opt: Optional) ?FileIndex {
1710 return switch (opt) {
1711 .none => null,
1712 _ => @fromBackingInt(@intCast(@backingInt(opt))),
1713 };
1714 }
1715 };
1716};
1717
1718const File = struct {
1719 bin_digest: Cache.BinDigest,
1720 file: *Zcu.File,
1721 /// `.none` means no type has been created yet.
1722 root_type: InternPool.Index,
1723};
1724
1725/// An index into `strings`.
1726pub const String = enum(u32) {
1727 /// An empty string.
1728 empty = 0,
1729 _,
1730
1731 pub fn toSlice(string: String, len: u64, ip: *const InternPool) []const u8 {
1732 return string.toOverlongSlice(ip)[0..@intCast(len)];
1733 }
1734
1735 pub fn at(string: String, index: u64, ip: *const InternPool) u8 {
1736 return string.toOverlongSlice(ip)[@intCast(index)];
1737 }
1738
1739 pub fn toNullTerminatedString(string: String, len: u64, ip: *const InternPool) NullTerminatedString {
1740 assert(std.mem.findScalar(u8, string.toSlice(len, ip), 0) == null);
1741 assert(string.at(len, ip) == 0);
1742 return @fromBackingInt(@intCast(@backingInt(string)));
1743 }
1744
1745 const Unwrapped = struct {
1746 tid: Zcu.PerThread.Id,
1747 index: u32,
1748
1749 fn wrap(unwrapped: Unwrapped, ip: *const InternPool) String {
1750 assert(@backingInt(unwrapped.tid) <= ip.getTidMask());
1751 assert(unwrapped.index <= ip.getIndexMask(u32));
1752 return @fromBackingInt(@intCast(@shlExact(@as(u32, @backingInt(unwrapped.tid)), ip.tid_shift_32) |
1753 unwrapped.index));
1754 }
1755 };
1756 fn unwrap(string: String, ip: *const InternPool) Unwrapped {
1757 return .{
1758 .tid = @fromBackingInt(@intCast(@backingInt(string) >> ip.tid_shift_32 & ip.getTidMask())),
1759 .index = @backingInt(string) & ip.getIndexMask(u32),
1760 };
1761 }
1762
1763 fn toOverlongSlice(string: String, ip: *const InternPool) []const u8 {
1764 const unwrapped = string.unwrap(ip);
1765 const local_shared = ip.getLocalShared(unwrapped.tid);
1766 const strings = local_shared.strings.acquire().view().items(.@"0");
1767 const string_bytes = local_shared.string_bytes.acquire().view().items(.@"0");
1768 return string_bytes[strings[unwrapped.index]..];
1769 }
1770
1771 const debug_state = InternPool.debug_state;
1772};
1773
1774/// An index into `strings` which might be `none`.
1775pub const OptionalString = enum(u32) {
1776 /// This is distinct from `none` - it is a valid index that represents empty string.
1777 empty = 0,
1778 none = std.math.maxInt(u32),
1779 _,
1780
1781 pub fn unwrap(string: OptionalString) ?String {
1782 return if (string != .none) @fromBackingInt(@intCast(@backingInt(string))) else null;
1783 }
1784
1785 pub fn toSlice(string: OptionalString, len: u64, ip: *const InternPool) ?[]const u8 {
1786 return (string.unwrap() orelse return null).toSlice(len, ip);
1787 }
1788
1789 const debug_state = InternPool.debug_state;
1790};
1791
1792/// An index into `strings`.
1793pub const NullTerminatedString = enum(u32) {
1794 /// An empty string.
1795 empty = 0,
1796 _,
1797
1798 /// An array of `NullTerminatedString` existing within the `extra` array.
1799 /// This type exists to provide a struct with lifetime that is
1800 /// not invalidated when items are added to the `InternPool`.
1801 pub const Slice = struct {
1802 tid: Zcu.PerThread.Id,
1803 start: u32,
1804 len: u32,
1805
1806 pub const empty: Slice = .{ .tid = .main, .start = 0, .len = 0 };
1807
1808 pub fn get(slice: Slice, ip: *const InternPool) []NullTerminatedString {
1809 const extra = ip.getLocalShared(slice.tid).extra.acquire();
1810 return @ptrCast(extra.view().items(.@"0")[slice.start..][0..slice.len]);
1811 }
1812 };
1813
1814 pub fn toString(self: NullTerminatedString) String {
1815 return @fromBackingInt(@intCast(@backingInt(self)));
1816 }
1817
1818 pub fn toOptional(self: NullTerminatedString) OptionalNullTerminatedString {
1819 return @fromBackingInt(@intCast(@backingInt(self)));
1820 }
1821
1822 pub fn toSlice(string: NullTerminatedString, ip: *const InternPool) [:0]const u8 {
1823 const unwrapped = string.toString().unwrap(ip);
1824 const local_shared = ip.getLocalShared(unwrapped.tid);
1825 const strings = local_shared.strings.acquire().view().items(.@"0");
1826 const string_bytes = local_shared.string_bytes.acquire().view().items(.@"0");
1827 return string_bytes[strings[unwrapped.index] .. strings[unwrapped.index + 1] - 1 :0];
1828 }
1829
1830 pub fn length(string: NullTerminatedString, ip: *const InternPool) u32 {
1831 const unwrapped = string.toString().unwrap(ip);
1832 const local_shared = ip.getLocalShared(unwrapped.tid);
1833 const strings = local_shared.strings.acquire().view().items(.@"0");
1834 return strings[unwrapped.index + 1] - 1 - strings[unwrapped.index];
1835 }
1836
1837 pub fn eqlSlice(string: NullTerminatedString, slice: []const u8, ip: *const InternPool) bool {
1838 const overlong_slice = string.toString().toOverlongSlice(ip);
1839 return overlong_slice.len > slice.len and
1840 std.mem.eql(u8, overlong_slice[0..slice.len], slice) and
1841 overlong_slice[slice.len] == 0;
1842 }
1843
1844 const Adapter = struct {
1845 strings: []const NullTerminatedString,
1846
1847 pub fn eql(ctx: @This(), a: NullTerminatedString, b_void: void, b_map_index: usize) bool {
1848 _ = b_void;
1849 return a == ctx.strings[b_map_index];
1850 }
1851
1852 pub fn hash(ctx: @This(), a: NullTerminatedString) u32 {
1853 _ = ctx;
1854 return std.hash.int(@backingInt(a));
1855 }
1856 };
1857
1858 /// Compare based on integer value alone, ignoring the string contents.
1859 pub fn indexLessThan(ctx: void, a: NullTerminatedString, b: NullTerminatedString) bool {
1860 _ = ctx;
1861 return @backingInt(a) < @backingInt(b);
1862 }
1863
1864 pub fn toUnsigned(string: NullTerminatedString, ip: *const InternPool) ?u32 {
1865 const slice = string.toSlice(ip);
1866 if (slice.len > 1 and slice[0] == '0') return null;
1867 if (std.mem.findScalar(u8, slice, '_')) |_| return null;
1868 return std.fmt.parseUnsigned(u32, slice, 10) catch null;
1869 }
1870
1871 const FormatData = struct {
1872 string: NullTerminatedString,
1873 ip: *const InternPool,
1874 id: bool,
1875 };
1876 fn format(data: FormatData, writer: *Io.Writer) Io.Writer.Error!void {
1877 const slice = data.string.toSlice(data.ip);
1878 if (!data.id) {
1879 try writer.writeAll(slice);
1880 } else {
1881 try writer.print("{f}", .{std.zig.fmtIdP(slice)});
1882 }
1883 }
1884
1885 pub fn fmt(string: NullTerminatedString, ip: *const InternPool) std.fmt.Alt(FormatData, format) {
1886 return .{ .data = .{ .string = string, .ip = ip, .id = false } };
1887 }
1888
1889 pub fn fmtId(string: NullTerminatedString, ip: *const InternPool) std.fmt.Alt(FormatData, format) {
1890 return .{ .data = .{ .string = string, .ip = ip, .id = true } };
1891 }
1892
1893 const debug_state = InternPool.debug_state;
1894};
1895
1896/// An index into `strings` which might be `none`.
1897pub const OptionalNullTerminatedString = enum(u32) {
1898 /// This is distinct from `none` - it is a valid index that represents empty string.
1899 empty = 0,
1900 none = std.math.maxInt(u32),
1901 _,
1902
1903 pub fn unwrap(string: OptionalNullTerminatedString) ?NullTerminatedString {
1904 return if (string != .none) @fromBackingInt(@intCast(@backingInt(string))) else null;
1905 }
1906
1907 pub fn toSlice(string: OptionalNullTerminatedString, ip: *const InternPool) ?[:0]const u8 {
1908 return (string.unwrap() orelse return null).toSlice(ip);
1909 }
1910
1911 const debug_state = InternPool.debug_state;
1912};
1913
1914/// A single value captured in the closure of a namespace type. This is not a plain
1915/// `Index` because we must differentiate between the following cases:
1916/// * runtime-known value (where we store the type)
1917/// * comptime-known value (where we store the value)
1918/// * `Nav` val (so that we can analyze the value lazily)
1919/// * `Nav` ref (so that we can analyze the reference lazily)
1920pub const CaptureValue = packed struct(u32) {
1921 tag: enum(u2) { @"comptime", runtime, nav_val, nav_ref },
1922 idx: u30,
1923
1924 pub fn wrap(val: Unwrapped) CaptureValue {
1925 return switch (val) {
1926 .@"comptime" => |i| .{ .tag = .@"comptime", .idx = @intCast(@backingInt(i)) },
1927 .runtime => |i| .{ .tag = .runtime, .idx = @intCast(@backingInt(i)) },
1928 .nav_val => |i| .{ .tag = .nav_val, .idx = @intCast(@backingInt(i)) },
1929 .nav_ref => |i| .{ .tag = .nav_ref, .idx = @intCast(@backingInt(i)) },
1930 };
1931 }
1932 pub fn unwrap(val: CaptureValue) Unwrapped {
1933 return switch (val.tag) {
1934 .@"comptime" => .{ .@"comptime" = @fromBackingInt(@intCast(val.idx)) },
1935 .runtime => .{ .runtime = @fromBackingInt(@intCast(val.idx)) },
1936 .nav_val => .{ .nav_val = @fromBackingInt(@intCast(val.idx)) },
1937 .nav_ref => .{ .nav_ref = @fromBackingInt(@intCast(val.idx)) },
1938 };
1939 }
1940
1941 pub const Unwrapped = union(enum) {
1942 /// Index refers to the value.
1943 @"comptime": Index,
1944 /// Index refers to the type.
1945 runtime: Index,
1946 nav_val: Nav.Index,
1947 nav_ref: Nav.Index,
1948 };
1949
1950 pub const Slice = struct {
1951 tid: Zcu.PerThread.Id,
1952 start: u32,
1953 len: u32,
1954
1955 pub const empty: Slice = .{ .tid = .main, .start = 0, .len = 0 };
1956
1957 pub fn get(slice: Slice, ip: *const InternPool) []CaptureValue {
1958 const extra = ip.getLocalShared(slice.tid).extra.acquire();
1959 return @ptrCast(extra.view().items(.@"0")[slice.start..][0..slice.len]);
1960 }
1961 };
1962};
1963
1964pub const Key = union(enum) {
1965 int_type: IntType,
1966 ptr_type: PtrType,
1967 array_type: ArrayType,
1968 vector_type: VectorType,
1969 opt_type: Index,
1970 /// `anyframe->T`. The payload is the child type, which may be `none` to indicate
1971 /// `anyframe`.
1972 anyframe_type: Index,
1973 error_union_type: ErrorUnionType,
1974 simple_type: SimpleType,
1975 /// This represents a struct that has been explicitly declared in source code,
1976 /// or was created with `@Struct`. It is unique and based on a declaration.
1977 struct_type: ContainerType,
1978 /// This is a tuple type. Tuples are logically similar to structs, but have some
1979 /// important differences in semantics; they do not undergo staged type resolution,
1980 /// so cannot be self-referential, and they are not considered container/namespace
1981 /// types, so cannot have declarations and have structural equality properties.
1982 tuple_type: TupleType,
1983 union_type: ContainerType,
1984 opaque_type: ContainerType,
1985 enum_type: ContainerType,
1986 spirv_type: SpirvType,
1987 func_type: FuncType,
1988 error_set_type: ErrorSetType,
1989 /// The payload is the function body, either a `func_decl` or `func_instance`.
1990 inferred_error_set_type: Index,
1991
1992 /// Typed `undefined`. This will never be `none`; untyped `undefined` is represented
1993 /// via `simple_value` and has a named `Index` tag for it.
1994 undef: Index,
1995 simple_value: SimpleValue,
1996 @"extern": Extern,
1997 func: Func,
1998 int: Key.Int,
1999 err: Error,
2000 error_union: ErrorUnion,
2001 enum_literal: NullTerminatedString,
2002 /// A specific enum tag, indicated by the integer tag value.
2003 enum_tag: EnumTag,
2004 float: Float,
2005 ptr: Ptr,
2006 slice: Slice,
2007 opt: Opt,
2008 /// An instance of a struct, array, or vector.
2009 /// Each element/field stored as an `Index`.
2010 /// In the case of sentinel-terminated arrays, the sentinel value *is* stored,
2011 /// so the slice length will be one more than the type's array length.
2012 /// There must be at least one element which is not `undefined`. If all elements are
2013 /// undefined, instead create an undefined value of the aggregate type.
2014 aggregate: Aggregate,
2015 /// An instance of a union.
2016 un: Union,
2017 /// An instance of a `packed struct` or `packed union`.
2018 bitpack: Bitpack,
2019
2020 /// A comptime function call with a memoized result.
2021 memoized_call: Key.MemoizedCall,
2022
2023 pub const TypeValue = extern struct {
2024 ty: Index,
2025 val: Index,
2026 };
2027
2028 pub const IntType = std.lang.Type.Int;
2029
2030 /// Extern for hashing via memory reinterpretation.
2031 pub const ErrorUnionType = extern struct {
2032 error_set_type: Index,
2033 payload_type: Index,
2034 };
2035
2036 pub const ErrorSetType = struct {
2037 /// Set of error names, sorted by null terminated string index.
2038 names: NullTerminatedString.Slice,
2039 /// This is ignored by `get` but will always be provided by `indexToKey`.
2040 names_map: OptionalMapIndex = .none,
2041
2042 /// Look up field index based on field name.
2043 pub fn nameIndex(self: ErrorSetType, ip: *const InternPool, name: NullTerminatedString) ?u32 {
2044 const map = self.names_map.unwrap().?.get(ip);
2045 const adapter: NullTerminatedString.Adapter = .{ .strings = self.names.get(ip) };
2046 const field_index = map.getIndexAdapted(name, adapter) orelse return null;
2047 return @intCast(field_index);
2048 }
2049 };
2050
2051 /// Extern layout so it can be hashed with `std.mem.asBytes`.
2052 pub const PtrType = extern struct {
2053 child: Index,
2054 sentinel: Index = .none,
2055 flags: Flags = .{},
2056 packed_offset: PackedOffset = .{ .bit_offset = 0, .host_size = 0 },
2057
2058 pub const VectorIndex = enum(u16) {
2059 none = std.math.maxInt(u16),
2060 _,
2061 };
2062
2063 pub const Flags = packed struct(u32) {
2064 size: Size = .one,
2065 /// `none` indicates the ABI alignment of the pointee_type. In this
2066 /// case, this field *must* be set to `none`, otherwise the
2067 /// `InternPool` equality and hashing functions will return incorrect
2068 /// results.
2069 alignment: Alignment = .none,
2070 is_const: bool = false,
2071 is_volatile: bool = false,
2072 is_allowzero: bool = false,
2073 /// See src/target.zig defaultAddressSpace function for how to obtain
2074 /// an appropriate value for this field.
2075 address_space: AddressSpace = .generic,
2076 vector_index: VectorIndex = .none,
2077 };
2078
2079 pub const PackedOffset = packed struct(u32) {
2080 /// If this is non-zero it means the pointer points to a sub-byte
2081 /// range of data, which is backed by a "host integer" with this
2082 /// number of bytes.
2083 /// When host_size=pointee_abi_size and bit_offset=0, this must be
2084 /// represented with host_size=0 instead.
2085 host_size: u16,
2086 bit_offset: u16,
2087 };
2088
2089 pub const Size = std.lang.Type.Pointer.Size;
2090 pub const AddressSpace = std.lang.AddressSpace;
2091 };
2092
2093 /// Extern so that hashing can be done via memory reinterpreting.
2094 pub const ArrayType = extern struct {
2095 len: u64,
2096 child: Index,
2097 sentinel: Index = .none,
2098
2099 pub fn lenIncludingSentinel(array_type: ArrayType) u64 {
2100 return array_type.len + @intFromBool(array_type.sentinel != .none);
2101 }
2102 };
2103
2104 /// Extern so that hashing can be done via memory reinterpreting.
2105 pub const VectorType = extern struct {
2106 len: u32,
2107 child: Index,
2108 };
2109
2110 pub const TupleType = struct {
2111 types: Index.Slice,
2112 /// These elements may be `none`, indicating runtime-known.
2113 values: Index.Slice,
2114 };
2115
2116 /// This is the hashmap key. To fetch other data associated with the type, see:
2117 /// * `loadStructType`
2118 /// * `loadUnionType`
2119 /// * `loadEnumType`
2120 /// * `loadOpaqueType`
2121 pub const ContainerType = union(enum) {
2122 /// This type corresponds to an actual source declaration, e.g. `struct { ... }`.
2123 /// It is hashed based on its ZIR instruction index and set of captures.
2124 declared: Declared,
2125 /// This type originates from a reification via `@Enum`, `@Struct`, `@Union` or from an anonymous initialization.
2126 /// It is hashed based on its ZIR instruction index and fields, attributes, etc.
2127 /// To avoid making this key overly complex, the type-specific data is hashed by Sema.
2128 reified: struct {
2129 /// A `reify`, `struct_init`, `struct_init_ref`, or `struct_init_anon` instruction.
2130 /// Alternatively, this is `main_struct_inst` of a ZON file.
2131 zir_index: TrackedInst.Index,
2132 /// A hash of this type's attributes, fields, etc, generated by Sema.
2133 type_hash: u64,
2134 },
2135 /// This type is an automatically-generated enum tag type for this union type.
2136 /// It is hashed based on the index of the union type it corresponds to.
2137 generated_union_tag: Index,
2138
2139 pub const Declared = struct {
2140 /// A `struct_decl`, `union_decl`, `enum_decl`, or `opaque_decl` instruction.
2141 zir_index: TrackedInst.Index,
2142 /// The captured values of this type. These values must be fully resolved per the language spec.
2143 captures: union(enum) {
2144 owned: CaptureValue.Slice,
2145 external: []const CaptureValue,
2146 },
2147 };
2148 };
2149
2150 pub const SpirvType = extern struct {
2151 /// If tag is `.image`, this is the sampled type or `.none` if `usage` is `.storage`.
2152 /// If tag is `.sampled_image`, this is the image type.
2153 /// If tag is `.runtime_array`, this is the element type.
2154 /// Otherwise this is `.none`.
2155 ty: Index,
2156 flags: Flags,
2157
2158 pub const Flags = packed struct(u32) {
2159 tag: @typeInfo(std.lang.Type.Spirv).@"union".tag_type.?,
2160 // Image type flags
2161 usage: @typeInfo(std.lang.Type.Spirv.Image.Usage).@"union".tag_type.?,
2162 format: std.lang.Type.Spirv.Image.Format,
2163 dim: std.lang.Type.Spirv.Image.Dimensionality,
2164 depth: std.lang.Type.Spirv.Image.Depth,
2165 access: std.lang.Type.Spirv.Image.Access,
2166 is_arrayed: bool,
2167 is_multisampled: bool,
2168
2169 _: u16 = 0,
2170 };
2171 };
2172
2173 pub const FuncType = struct {
2174 param_types: Index.Slice,
2175 return_type: Index,
2176 /// Tells whether a parameter is comptime. See `paramIsComptime` helper
2177 /// method for accessing this.
2178 comptime_bits: u32,
2179 /// Tells whether a parameter is noalias. See `paramIsNoalias` helper
2180 /// method for accessing this.
2181 noalias_bits: u32,
2182 cc: std.lang.CallingConvention,
2183 is_var_args: bool,
2184 is_noinline: bool,
2185
2186 pub fn paramIsComptime(self: @This(), i: u5) bool {
2187 assert(i < self.param_types.len);
2188 return @as(u1, @truncate(self.comptime_bits >> i)) != 0;
2189 }
2190
2191 pub fn paramIsNoalias(self: @This(), i: u5) bool {
2192 assert(i < self.param_types.len);
2193 return @as(u1, @truncate(self.noalias_bits >> i)) != 0;
2194 }
2195
2196 pub fn eql(a: FuncType, b: FuncType, ip: *const InternPool) bool {
2197 return std.mem.eql(Index, a.param_types.get(ip), b.param_types.get(ip)) and
2198 a.return_type == b.return_type and
2199 a.comptime_bits == b.comptime_bits and
2200 a.noalias_bits == b.noalias_bits and
2201 a.is_var_args == b.is_var_args and
2202 a.is_noinline == b.is_noinline and
2203 std.meta.eql(a.cc, b.cc);
2204 }
2205
2206 pub fn hash(self: FuncType, hasher: *Hash, ip: *const InternPool) void {
2207 for (self.param_types.get(ip)) |param_type| {
2208 std.hash.autoHash(hasher, param_type);
2209 }
2210 std.hash.autoHash(hasher, self.return_type);
2211 std.hash.autoHash(hasher, self.comptime_bits);
2212 std.hash.autoHash(hasher, self.noalias_bits);
2213 std.hash.autoHash(hasher, self.cc);
2214 std.hash.autoHash(hasher, self.is_var_args);
2215 std.hash.autoHash(hasher, self.is_noinline);
2216 }
2217 };
2218
2219 pub const Extern = struct {
2220 /// The name of the extern symbol.
2221 name: NullTerminatedString,
2222 /// The type of the extern symbol itself.
2223 /// This may be `.anyopaque_type`, in which case the value may not be loaded.
2224 ty: Index,
2225 /// Library name if specified.
2226 /// For example `extern "c" fn write(...) usize` would have 'c' as library name.
2227 /// Index into the string table bytes.
2228 lib_name: OptionalNullTerminatedString,
2229 linkage: std.lang.GlobalLinkage,
2230 visibility: std.lang.SymbolVisibility,
2231 is_threadlocal: bool,
2232 is_dll_import: bool,
2233 relocation: std.lang.ExternOptions.Relocation,
2234 decoration: ?std.lang.ExternOptions.Decoration,
2235 is_const: bool,
2236 alignment: Alignment,
2237 @"addrspace": std.lang.AddressSpace,
2238 /// The ZIR instruction which created this extern; used only for source locations.
2239 /// This is a `declaration`.
2240 zir_index: TrackedInst.Index,
2241 /// The `Nav` corresponding to this extern symbol.
2242 /// This is ignored by hashing and equality.
2243 owner_nav: Nav.Index,
2244 source: Tag.Extern.Flags.Source,
2245 };
2246
2247 pub const Func = struct {
2248 tid: Zcu.PerThread.Id,
2249 /// In the case of a generic function, this type will potentially have fewer parameters
2250 /// than the generic owner's type, because the comptime parameters will be deleted.
2251 ty: Index,
2252 /// If this is a function body that has been coerced to a different type, for example
2253 /// ```
2254 /// fn f2() !void {}
2255 /// const f: fn()anyerror!void = f2;
2256 /// ```
2257 /// then it contains the original type of the function body.
2258 uncoerced_ty: Index,
2259 /// Index into extra array of the `FuncAnalysis` corresponding to this function.
2260 /// Used for mutating that data.
2261 analysis_extra_index: u32,
2262 /// Index into extra array of the `zir_body_inst` corresponding to this function.
2263 /// Used for mutating that data.
2264 zir_body_inst_extra_index: u32,
2265 /// Index into extra array of the resolved inferred error set for this function.
2266 /// Used for mutating that data.
2267 /// 0 when the function does not have an inferred error set.
2268 resolved_error_set_extra_index: u32,
2269 /// When a generic function is instantiated, branch_quota is inherited from the
2270 /// active Sema context. Importantly, this value is also updated when an existing
2271 /// generic function instantiation is found and called.
2272 /// This field contains the index into the extra array of this value,
2273 /// so that it can be mutated.
2274 /// This will be 0 when the function is not a generic function instantiation.
2275 branch_quota_extra_index: u32,
2276 owner_nav: Nav.Index,
2277 /// The ZIR instruction that is a function instruction. Use this to find
2278 /// the body. We store this rather than the body directly so that when ZIR
2279 /// is regenerated on update(), we can map this to the new corresponding
2280 /// ZIR instruction.
2281 zir_body_inst: TrackedInst.Index,
2282 /// Relative to owner Decl.
2283 lbrace_line: u32,
2284 /// Relative to owner Decl.
2285 rbrace_line: u32,
2286 lbrace_column: u32,
2287 rbrace_column: u32,
2288
2289 /// The `func_decl` which is the generic function from whence this instance was spawned.
2290 /// If this is `none` it means the function is not a generic instantiation.
2291 generic_owner: Index,
2292 /// If this is a generic function instantiation, this will be non-empty.
2293 /// Corresponds to the parameters of the `generic_owner` type, which
2294 /// may have more parameters than `ty`.
2295 /// Each element is the comptime-known value the generic function was instantiated with,
2296 /// or `none` if the element is runtime-known.
2297 /// TODO: as a follow-up optimization, don't store `none` values here since that data
2298 /// is redundant with `comptime_bits` stored elsewhere.
2299 comptime_args: Index.Slice,
2300
2301 /// Returns a pointer that becomes invalid after any additions to the `InternPool`.
2302 fn analysisPtr(func: Func, ip: *const InternPool) *FuncAnalysis {
2303 const extra = ip.getLocalShared(func.tid).extra.acquire();
2304 return @ptrCast(&extra.view().items(.@"0")[func.analysis_extra_index]);
2305 }
2306
2307 pub fn analysisUnordered(func: Func, ip: *const InternPool) FuncAnalysis {
2308 return @atomicLoad(FuncAnalysis, func.analysisPtr(ip), .unordered);
2309 }
2310
2311 pub fn setBranchHint(func: Func, ip: *InternPool, io: Io, hint: std.lang.BranchHint) void {
2312 const extra_mutex = &ip.getLocal(func.tid).mutate.extra.mutex;
2313 extra_mutex.lockUncancelable(io);
2314 defer extra_mutex.unlock(io);
2315
2316 const analysis_ptr = func.analysisPtr(ip);
2317 var analysis = analysis_ptr.*;
2318 analysis.branch_hint = hint;
2319 @atomicStore(FuncAnalysis, analysis_ptr, analysis, .release);
2320 }
2321
2322 /// Returns a pointer that becomes invalid after any additions to the `InternPool`.
2323 fn zirBodyInstPtr(func: Func, ip: *const InternPool) *TrackedInst.Index {
2324 const extra = ip.getLocalShared(func.tid).extra.acquire();
2325 return @ptrCast(&extra.view().items(.@"0")[func.zir_body_inst_extra_index]);
2326 }
2327
2328 pub fn zirBodyInstUnordered(func: Func, ip: *const InternPool) TrackedInst.Index {
2329 return @atomicLoad(TrackedInst.Index, func.zirBodyInstPtr(ip), .unordered);
2330 }
2331
2332 /// Returns a pointer that becomes invalid after any additions to the `InternPool`.
2333 fn branchQuotaPtr(func: Func, ip: *const InternPool) *u32 {
2334 const extra = ip.getLocalShared(func.tid).extra.acquire();
2335 return &extra.view().items(.@"0")[func.branch_quota_extra_index];
2336 }
2337
2338 pub fn branchQuotaUnordered(func: Func, ip: *const InternPool) u32 {
2339 return @atomicLoad(u32, func.branchQuotaPtr(ip), .unordered);
2340 }
2341
2342 pub fn maxBranchQuota(func: Func, ip: *InternPool, io: Io, new_branch_quota: u32) void {
2343 const extra_mutex = &ip.getLocal(func.tid).mutate.extra.mutex;
2344 extra_mutex.lockUncancelable(io);
2345 defer extra_mutex.unlock(io);
2346
2347 const branch_quota_ptr = func.branchQuotaPtr(ip);
2348 @atomicStore(u32, branch_quota_ptr, @max(branch_quota_ptr.*, new_branch_quota), .release);
2349 }
2350
2351 /// Returns a pointer that becomes invalid after any additions to the `InternPool`.
2352 fn resolvedErrorSetPtr(func: Func, ip: *const InternPool) *Index {
2353 const extra = ip.getLocalShared(func.tid).extra.acquire();
2354 assert(func.analysisUnordered(ip).inferred_error_set);
2355 return @ptrCast(&extra.view().items(.@"0")[func.resolved_error_set_extra_index]);
2356 }
2357
2358 pub fn resolvedErrorSetUnordered(func: Func, ip: *const InternPool) Index {
2359 return @atomicLoad(Index, func.resolvedErrorSetPtr(ip), .unordered);
2360 }
2361
2362 pub fn setResolvedErrorSet(func: Func, ip: *InternPool, io: Io, ies: Index) void {
2363 const extra_mutex = &ip.getLocal(func.tid).mutate.extra.mutex;
2364 extra_mutex.lockUncancelable(io);
2365 defer extra_mutex.unlock(io);
2366
2367 @atomicStore(Index, func.resolvedErrorSetPtr(ip), ies, .release);
2368 }
2369 };
2370
2371 pub const Int = struct {
2372 ty: Index,
2373 storage: Storage,
2374
2375 pub const Storage = union(enum) {
2376 u64: u64,
2377 i64: i64,
2378 big_int: BigIntConst,
2379
2380 /// Big enough to fit any non-BigInt value
2381 pub const BigIntSpace = struct {
2382 /// The +1 is headroom so that operations such as incrementing once
2383 /// or decrementing once are possible without using an allocator.
2384 limbs: [(@sizeOf(u64) / @sizeOf(std.math.big.Limb)) + 1]std.math.big.Limb,
2385 };
2386
2387 pub fn toBigInt(storage: Storage, space: *BigIntSpace) BigIntConst {
2388 return switch (storage) {
2389 .big_int => |x| x,
2390 inline .u64, .i64 => |x| BigIntMutable.init(&space.limbs, x).toConst(),
2391 };
2392 }
2393 };
2394 };
2395
2396 pub const Error = extern struct {
2397 ty: Index,
2398 name: NullTerminatedString,
2399 };
2400
2401 pub const ErrorUnion = struct {
2402 ty: Index,
2403 val: Value,
2404
2405 pub const Value = union(enum) {
2406 err_name: NullTerminatedString,
2407 payload: Index,
2408 };
2409 };
2410
2411 pub const EnumTag = extern struct {
2412 /// The enum type.
2413 ty: Index,
2414 /// The integer tag value which has the integer tag type of the enum.
2415 int: Index,
2416 };
2417
2418 pub const Float = struct {
2419 ty: Index,
2420 /// The storage used must match the size of the float type being represented.
2421 storage: Storage,
2422
2423 pub const Storage = union(enum) {
2424 f16: f16,
2425 f32: f32,
2426 f64: f64,
2427 f80: f80,
2428 f128: f128,
2429 };
2430 };
2431
2432 pub const Ptr = struct {
2433 /// This is the pointer type, not the element type.
2434 ty: Index,
2435 /// The base address which this pointer is offset from.
2436 base_addr: BaseAddr,
2437 /// The offset of this pointer from `base_addr` in bytes.
2438 byte_offset: u64,
2439
2440 pub const BaseAddr = union(enum) {
2441 const Tag = @typeInfo(BaseAddr).@"union".tag_type.?;
2442
2443 /// Points to the value of a single `Nav`.
2444 nav: Nav.Index,
2445
2446 /// Points to the value of a single comptime alloc stored in `Sema`.
2447 comptime_alloc: ComptimeAllocIndex,
2448
2449 /// Points to a single unnamed constant value.
2450 uav: Uav,
2451
2452 /// Points to a comptime field of a struct. Index is the field's value.
2453 ///
2454 /// TODO: this exists because these fields are semantically mutable. We
2455 /// should probably change the language so that this isn't the case.
2456 comptime_field: Index,
2457
2458 /// A pointer with a fixed integer address, usually from `@ptrFromInt`.
2459 ///
2460 /// The address is stored entirely by `byte_offset`, which will be positive
2461 /// and in-range of a `usize`. The base address is, for all intents and purposes, 0.
2462 int,
2463
2464 /// A pointer to the payload of an error union. Index is the error union pointer.
2465 /// To ensure a canonical representation, the type of the base pointer must:
2466 /// * be a one-pointer
2467 /// * be `const`, `volatile` and `allowzero`
2468 /// * have alignment 1
2469 /// * have the same address space as this pointer
2470 /// * have a host size, bit offset, and vector index of 0
2471 /// See `Value.canonicalizeBasePtr` which enforces these properties.
2472 eu_payload: Index,
2473
2474 /// A pointer to the payload of a non-pointer-like optional. Index is the
2475 /// optional pointer. To ensure a canonical representation, the base
2476 /// pointer is subject to the same restrictions as in `eu_payload`.
2477 opt_payload: Index,
2478
2479 /// A pointer to a field of a slice, or of an auto-layout struct or union. Slice fields
2480 /// are referenced according to `Value.slice_ptr_index` and `Value.slice_len_index`.
2481 /// Base is the aggregate pointer, which is subject to the same restrictions as
2482 /// in `eu_payload`.
2483 field: BaseIndex,
2484
2485 /// A pointer to an element of a comptime-only array. Base is the
2486 /// many-pointer we are indexing into. It is subject to the same restrictions
2487 /// as in `eu_payload`, except it must be a many-pointer rather than a one-pointer.
2488 ///
2489 /// The element type of the base pointer must NOT be an array. Additionally, the
2490 /// base pointer is guaranteed to not be an `arr_elem` into a pointer with the
2491 /// same child type. Thus, since there are no two comptime-only types which are
2492 /// IMC to one another, the only case where the base pointer may also be an
2493 /// `arr_elem` is when this pointer is semantically invalid (e.g. it reinterprets
2494 /// a `type` as a `comptime_int`). These restrictions are in place to ensure
2495 /// a canonical representation.
2496 ///
2497 /// This kind of base address differs from others in that it may refer to any
2498 /// sequence of values; for instance, an `arr_elem` at index 2 may refer to
2499 /// any number of elements starting from index 2.
2500 ///
2501 /// Index must not be 0. To refer to the element at index 0, simply reinterpret
2502 /// the aggregate pointer.
2503 arr_elem: BaseIndex,
2504
2505 pub const BaseIndex = struct {
2506 base: Index,
2507 index: u64,
2508 };
2509 pub const Uav = extern struct {
2510 val: Index,
2511 /// Contains the canonical pointer type of the anonymous
2512 /// declaration. This may equal `ty` of the `Ptr` or it may be
2513 /// different. Importantly, when lowering the anonymous decl,
2514 /// the original pointer type alignment must be used.
2515 orig_ty: Index,
2516 };
2517
2518 pub fn eql(a: BaseAddr, b: BaseAddr) bool {
2519 if (@as(Key.Ptr.BaseAddr.Tag, a) != @as(Key.Ptr.BaseAddr.Tag, b)) return false;
2520
2521 return switch (a) {
2522 .nav => |a_nav| a_nav == b.nav,
2523 .comptime_alloc => |a_alloc| a_alloc == b.comptime_alloc,
2524 .uav => |ad| ad.val == b.uav.val and
2525 ad.orig_ty == b.uav.orig_ty,
2526 .int => true,
2527 .eu_payload => |a_eu_payload| a_eu_payload == b.eu_payload,
2528 .opt_payload => |a_opt_payload| a_opt_payload == b.opt_payload,
2529 .comptime_field => |a_comptime_field| a_comptime_field == b.comptime_field,
2530 .arr_elem => |a_elem| std.meta.eql(a_elem, b.arr_elem),
2531 .field => |a_field| std.meta.eql(a_field, b.field),
2532 };
2533 }
2534 };
2535 };
2536
2537 pub const Slice = struct {
2538 /// This is the slice type, not the element type.
2539 ty: Index,
2540 /// The slice's `ptr` field. Must be a many-ptr with the same properties as `ty`.
2541 ptr: Index,
2542 /// The slice's `len` field. Must be a `usize`.
2543 len: Index,
2544 };
2545
2546 /// `null` is represented by the `val` field being `none`.
2547 pub const Opt = extern struct {
2548 /// This is the optional type; not the payload type.
2549 ty: Index,
2550 /// This could be `none`, indicating the optional is `null`.
2551 val: Index,
2552 };
2553
2554 pub const Union = extern struct {
2555 /// This is the union type; not the field type.
2556 ty: Index,
2557 /// Indicates the active field. This could be `none`, which indicates the tag is not known. `none` is only a valid value for extern and packed unions.
2558 /// In those cases, the type of `val` is:
2559 /// extern: a u8 array of the same byte length as the union
2560 /// packed: an unsigned integer with the same bit size as the union
2561 tag: Index,
2562 /// The value of the active field.
2563 val: Index,
2564 };
2565
2566 pub const Aggregate = struct {
2567 ty: Index,
2568 storage: Storage,
2569
2570 pub const Storage = union(enum) {
2571 bytes: String,
2572 elems: []const Index,
2573 repeated_elem: Index,
2574
2575 pub fn values(self: *const Storage) []const Index {
2576 return switch (self.*) {
2577 .bytes => &.{},
2578 .elems => |elems| elems,
2579 .repeated_elem => |*elem| @as(*const [1]Index, elem),
2580 };
2581 }
2582 };
2583 };
2584
2585 /// As well as a key, this type doubles as the payload in `extra` for `Tag.bitpack`.
2586 pub const Bitpack = struct {
2587 /// The `packed struct` or `packed union` type.
2588 ty: Index,
2589 /// The contents of the bitpack, represented as the backing integer value. The type of this
2590 /// value is the same as the backing integer type of `ty`.
2591 backing_int_val: Index,
2592 };
2593
2594 pub const MemoizedCall = struct {
2595 func: Index,
2596 arg_values: []const Index,
2597 result: Index,
2598 branch_count: u32,
2599 branch_quota: u32,
2600 };
2601
2602 pub fn hash32(key: Key, ip: *const InternPool) u32 {
2603 return @truncate(key.hash64(ip));
2604 }
2605
2606 pub fn hash64(key: Key, ip: *const InternPool) u64 {
2607 const asBytes = std.mem.asBytes;
2608 const KeyTag = @typeInfo(Key).@"union".tag_type.?;
2609 const seed = @backingInt(@as(KeyTag, key));
2610 return switch (key) {
2611 inline .ptr_type,
2612 .array_type,
2613 .vector_type,
2614 .opt_type,
2615 .anyframe_type,
2616 .error_union_type,
2617 .spirv_type,
2618 .simple_type,
2619 .simple_value,
2620 .opt,
2621 .undef,
2622 .err,
2623 .enum_literal,
2624 .enum_tag,
2625 .inferred_error_set_type,
2626 .un,
2627 => |x| {
2628 _ = extern struct { is_extern: @TypeOf(x) };
2629 comptime assert(std.meta.hasUniqueRepresentation(@TypeOf(x)));
2630 return Hash.hash(seed, asBytes(&x));
2631 },
2632
2633 .int_type => |x| Hash.hash(seed + @backingInt(x.signedness), asBytes(&x.bits)),
2634
2635 .error_union => |x| switch (x.val) {
2636 .err_name => |y| Hash.hash(seed + 0, asBytes(&x.ty) ++ asBytes(&y)),
2637 .payload => |y| Hash.hash(seed + 1, asBytes(&x.ty) ++ asBytes(&y)),
2638 },
2639
2640 .opaque_type,
2641 .enum_type,
2642 .union_type,
2643 .struct_type,
2644 => |namespace_type| {
2645 var hasher = Hash.init(seed);
2646 std.hash.autoHash(&hasher, std.meta.activeTag(namespace_type));
2647 switch (namespace_type) {
2648 .declared => |declared| {
2649 std.hash.autoHash(&hasher, declared.zir_index);
2650 const captures = switch (declared.captures) {
2651 .owned => |cvs| cvs.get(ip),
2652 .external => |cvs| cvs,
2653 };
2654 for (captures) |cv| {
2655 std.hash.autoHash(&hasher, cv);
2656 }
2657 },
2658 .reified => |reified| {
2659 std.hash.autoHash(&hasher, reified.zir_index);
2660 std.hash.autoHash(&hasher, reified.type_hash);
2661 },
2662 .generated_union_tag => |union_type| {
2663 std.hash.autoHash(&hasher, union_type);
2664 },
2665 }
2666 return hasher.final();
2667 },
2668
2669 .int => |int| {
2670 var hasher = Hash.init(seed);
2671 // Canonicalize all integers by converting them to BigIntConst.
2672 var buffer: Key.Int.Storage.BigIntSpace = undefined;
2673 const big_int = int.storage.toBigInt(&buffer);
2674
2675 std.hash.autoHash(&hasher, int.ty);
2676 std.hash.autoHash(&hasher, big_int.positive or big_int.eqlZero());
2677 for (big_int.limbs) |limb| std.hash.autoHash(&hasher, limb);
2678 return hasher.final();
2679 },
2680
2681 .float => |float| {
2682 var hasher = Hash.init(seed);
2683 std.hash.autoHash(&hasher, float.ty);
2684 switch (float.storage) {
2685 inline else => |val| std.hash.autoHash(
2686 &hasher,
2687 @as(@Int(.unsigned, @bitSizeOf(@TypeOf(val))), @bitCast(val)),
2688 ),
2689 }
2690 return hasher.final();
2691 },
2692
2693 .slice => |slice| Hash.hash(seed, asBytes(&slice.ty) ++ asBytes(&slice.ptr) ++ asBytes(&slice.len)),
2694
2695 .ptr => |ptr| {
2696 // Int-to-ptr pointers are hashed separately than decl-referencing pointers.
2697 // This is sound due to pointer provenance rules.
2698 const addr_tag: Key.Ptr.BaseAddr.Tag = ptr.base_addr;
2699 const seed2 = seed + @backingInt(addr_tag);
2700 const big_offset: i128 = ptr.byte_offset;
2701 const common = asBytes(&ptr.ty) ++ asBytes(&big_offset);
2702 return switch (ptr.base_addr) {
2703 inline .nav,
2704 .comptime_alloc,
2705 .uav,
2706 .int,
2707 .eu_payload,
2708 .opt_payload,
2709 .comptime_field,
2710 => |x| Hash.hash(seed2, common ++ asBytes(&x)),
2711
2712 .arr_elem, .field => |x| Hash.hash(
2713 seed2,
2714 common ++ asBytes(&x.base) ++ asBytes(&x.index),
2715 ),
2716 };
2717 },
2718
2719 .aggregate => |aggregate| {
2720 var hasher = Hash.init(seed);
2721 std.hash.autoHash(&hasher, aggregate.ty);
2722 const len = ip.aggregateTypeLen(aggregate.ty);
2723 const child = switch (ip.indexToKey(aggregate.ty)) {
2724 .array_type => |array_type| array_type.child,
2725 .vector_type => |vector_type| vector_type.child,
2726 .tuple_type, .struct_type => .none,
2727 else => unreachable,
2728 };
2729
2730 if (child == .u8_type) {
2731 switch (aggregate.storage) {
2732 .bytes => |bytes| for (bytes.toSlice(len, ip)) |byte| {
2733 std.hash.autoHash(&hasher, KeyTag.int);
2734 std.hash.autoHash(&hasher, byte);
2735 },
2736 .elems => |elems| for (elems[0..@intCast(len)]) |elem| {
2737 const elem_key = ip.indexToKey(elem);
2738 std.hash.autoHash(&hasher, @as(KeyTag, elem_key));
2739 switch (elem_key) {
2740 .undef => {},
2741 .int => |int| std.hash.autoHash(
2742 &hasher,
2743 @as(u8, @intCast(int.storage.u64)),
2744 ),
2745 else => unreachable,
2746 }
2747 },
2748 .repeated_elem => |elem| {
2749 const elem_key = ip.indexToKey(elem);
2750 var remaining = len;
2751 while (remaining > 0) : (remaining -= 1) {
2752 std.hash.autoHash(&hasher, @as(KeyTag, elem_key));
2753 switch (elem_key) {
2754 .undef => {},
2755 .int => |int| std.hash.autoHash(
2756 &hasher,
2757 @as(u8, @intCast(int.storage.u64)),
2758 ),
2759 else => unreachable,
2760 }
2761 }
2762 },
2763 }
2764 return hasher.final();
2765 }
2766
2767 switch (aggregate.storage) {
2768 .bytes => unreachable,
2769 .elems => |elems| for (elems[0..@intCast(len)]) |elem|
2770 std.hash.autoHash(&hasher, elem),
2771 .repeated_elem => |elem| {
2772 var remaining = len;
2773 while (remaining > 0) : (remaining -= 1) std.hash.autoHash(&hasher, elem);
2774 },
2775 }
2776 return hasher.final();
2777 },
2778
2779 .error_set_type => |x| Hash.hash(seed, std.mem.sliceAsBytes(x.names.get(ip))),
2780
2781 .tuple_type => |tuple_type| {
2782 var hasher = Hash.init(seed);
2783 for (tuple_type.types.get(ip)) |elem| std.hash.autoHash(&hasher, elem);
2784 for (tuple_type.values.get(ip)) |elem| std.hash.autoHash(&hasher, elem);
2785 return hasher.final();
2786 },
2787
2788 .func_type => |func_type| {
2789 var hasher = Hash.init(seed);
2790 func_type.hash(&hasher, ip);
2791 return hasher.final();
2792 },
2793
2794 .memoized_call => |memoized_call| {
2795 var hasher = Hash.init(seed);
2796 std.hash.autoHash(&hasher, memoized_call.func);
2797 for (memoized_call.arg_values) |arg| std.hash.autoHash(&hasher, arg);
2798 return hasher.final();
2799 },
2800
2801 .func => |func| {
2802 // In the case of a function with an inferred error set, we
2803 // must not include the inferred error set type in the hash,
2804 // otherwise we would get false negatives for interning generic
2805 // function instances which have inferred error sets.
2806
2807 if (func.generic_owner == .none and func.resolved_error_set_extra_index == 0) {
2808 const bytes = asBytes(&func.owner_nav) ++ asBytes(&func.ty) ++
2809 [1]u8{@intFromBool(func.uncoerced_ty == func.ty)};
2810 return Hash.hash(seed, bytes);
2811 }
2812
2813 var hasher = Hash.init(seed);
2814 std.hash.autoHash(&hasher, func.generic_owner);
2815 std.hash.autoHash(&hasher, func.uncoerced_ty == func.ty);
2816 for (func.comptime_args.get(ip)) |arg| std.hash.autoHash(&hasher, arg);
2817 if (func.resolved_error_set_extra_index == 0) {
2818 std.hash.autoHash(&hasher, func.ty);
2819 } else {
2820 var ty_info = ip.indexToFuncType(func.ty).?;
2821 ty_info.return_type = ip.errorUnionPayload(ty_info.return_type);
2822 ty_info.hash(&hasher, ip);
2823 }
2824 return hasher.final();
2825 },
2826
2827 .@"extern" => |e| Hash.hash(seed, asBytes(&e.name) ++
2828 asBytes(&e.ty) ++ asBytes(&e.lib_name) ++
2829 asBytes(&e.linkage) ++ asBytes(&e.visibility) ++
2830 asBytes(&e.is_threadlocal) ++ asBytes(&e.is_dll_import) ++
2831 asBytes(&e.relocation) ++
2832 asBytes(&e.is_const) ++ asBytes(&e.alignment) ++ asBytes(&e.@"addrspace") ++
2833 asBytes(&e.zir_index) ++ &[1]u8{@backingInt(e.source)}),
2834
2835 .bitpack => |bitpack| Hash.hash(seed, asBytes(&bitpack.ty) ++ asBytes(&bitpack.backing_int_val)),
2836 };
2837 }
2838
2839 pub fn eql(a: Key, b: Key, ip: *const InternPool) bool {
2840 const KeyTag = @typeInfo(Key).@"union".tag_type.?;
2841 const a_tag: KeyTag = a;
2842 const b_tag: KeyTag = b;
2843 if (a_tag != b_tag) return false;
2844 switch (a) {
2845 .int_type => |a_info| {
2846 const b_info = b.int_type;
2847 return std.meta.eql(a_info, b_info);
2848 },
2849 .ptr_type => |a_info| {
2850 const b_info = b.ptr_type;
2851 return std.meta.eql(a_info, b_info);
2852 },
2853 .array_type => |a_info| {
2854 const b_info = b.array_type;
2855 return std.meta.eql(a_info, b_info);
2856 },
2857 .vector_type => |a_info| {
2858 const b_info = b.vector_type;
2859 return std.meta.eql(a_info, b_info);
2860 },
2861 .opt_type => |a_info| {
2862 const b_info = b.opt_type;
2863 return a_info == b_info;
2864 },
2865 .anyframe_type => |a_info| {
2866 const b_info = b.anyframe_type;
2867 return a_info == b_info;
2868 },
2869 .error_union_type => |a_info| {
2870 const b_info = b.error_union_type;
2871 return std.meta.eql(a_info, b_info);
2872 },
2873 .spirv_type => |a_info| {
2874 const b_info = b.spirv_type;
2875 return std.meta.eql(a_info, b_info);
2876 },
2877 .simple_type => |a_info| {
2878 const b_info = b.simple_type;
2879 return a_info == b_info;
2880 },
2881 .simple_value => |a_info| {
2882 const b_info = b.simple_value;
2883 return a_info == b_info;
2884 },
2885 .undef => |a_info| {
2886 const b_info = b.undef;
2887 return a_info == b_info;
2888 },
2889 .opt => |a_info| {
2890 const b_info = b.opt;
2891 return std.meta.eql(a_info, b_info);
2892 },
2893 .un => |a_info| {
2894 const b_info = b.un;
2895 return std.meta.eql(a_info, b_info);
2896 },
2897 .err => |a_info| {
2898 const b_info = b.err;
2899 return std.meta.eql(a_info, b_info);
2900 },
2901 .error_union => |a_info| {
2902 const b_info = b.error_union;
2903 return std.meta.eql(a_info, b_info);
2904 },
2905 .enum_literal => |a_info| {
2906 const b_info = b.enum_literal;
2907 return a_info == b_info;
2908 },
2909 .enum_tag => |a_info| {
2910 const b_info = b.enum_tag;
2911 return std.meta.eql(a_info, b_info);
2912 },
2913 .bitpack => |a_info| {
2914 const b_info = b.bitpack;
2915 return a_info.ty == b_info.ty and a_info.backing_int_val == b_info.backing_int_val;
2916 },
2917
2918 .@"extern" => |a_info| {
2919 const b_info = b.@"extern";
2920 return a_info.name == b_info.name and
2921 a_info.ty == b_info.ty and
2922 a_info.lib_name == b_info.lib_name and
2923 a_info.linkage == b_info.linkage and
2924 a_info.visibility == b_info.visibility and
2925 a_info.is_threadlocal == b_info.is_threadlocal and
2926 a_info.is_dll_import == b_info.is_dll_import and
2927 a_info.relocation == b_info.relocation and
2928 a_info.is_const == b_info.is_const and
2929 a_info.alignment == b_info.alignment and
2930 a_info.@"addrspace" == b_info.@"addrspace" and
2931 a_info.zir_index == b_info.zir_index and
2932 a_info.source == b_info.source;
2933 },
2934 .func => |a_info| {
2935 const b_info = b.func;
2936
2937 if (a_info.generic_owner != b_info.generic_owner)
2938 return false;
2939
2940 if (a_info.generic_owner == .none) {
2941 if (a_info.owner_nav != b_info.owner_nav)
2942 return false;
2943 } else {
2944 if (!std.mem.eql(
2945 Index,
2946 a_info.comptime_args.get(ip),
2947 b_info.comptime_args.get(ip),
2948 )) return false;
2949 }
2950
2951 if ((a_info.ty == a_info.uncoerced_ty) !=
2952 (b_info.ty == b_info.uncoerced_ty))
2953 {
2954 return false;
2955 }
2956
2957 if (a_info.ty == b_info.ty)
2958 return true;
2959
2960 // There is one case where the types may be inequal but we
2961 // still want to find the same function body instance. In the
2962 // case of the functions having an inferred error set, the key
2963 // used to find an existing function body will necessarily have
2964 // a unique inferred error set type, because it refers to the
2965 // function body InternPool Index. To make this case work we
2966 // omit the inferred error set from the equality check.
2967 if (a_info.resolved_error_set_extra_index == 0 or
2968 b_info.resolved_error_set_extra_index == 0)
2969 {
2970 return false;
2971 }
2972 var a_ty_info = ip.indexToFuncType(a_info.ty).?;
2973 a_ty_info.return_type = ip.errorUnionPayload(a_ty_info.return_type);
2974 var b_ty_info = ip.indexToFuncType(b_info.ty).?;
2975 b_ty_info.return_type = ip.errorUnionPayload(b_ty_info.return_type);
2976 return a_ty_info.eql(b_ty_info, ip);
2977 },
2978
2979 .slice => |a_info| {
2980 const b_info = b.slice;
2981 if (a_info.ty != b_info.ty) return false;
2982 if (a_info.ptr != b_info.ptr) return false;
2983 if (a_info.len != b_info.len) return false;
2984 return true;
2985 },
2986
2987 .ptr => |a_info| {
2988 const b_info = b.ptr;
2989 if (a_info.ty != b_info.ty) return false;
2990 if (a_info.byte_offset != b_info.byte_offset) return false;
2991 if (!a_info.base_addr.eql(b_info.base_addr)) return false;
2992 return true;
2993 },
2994
2995 .int => |a_info| {
2996 const b_info = b.int;
2997
2998 if (a_info.ty != b_info.ty)
2999 return false;
3000
3001 return switch (a_info.storage) {
3002 .u64 => |aa| switch (b_info.storage) {
3003 .u64 => |bb| aa == bb,
3004 .i64 => |bb| aa == bb,
3005 .big_int => |bb| bb.orderAgainstScalar(aa) == .eq,
3006 },
3007 .i64 => |aa| switch (b_info.storage) {
3008 .u64 => |bb| aa == bb,
3009 .i64 => |bb| aa == bb,
3010 .big_int => |bb| bb.orderAgainstScalar(aa) == .eq,
3011 },
3012 .big_int => |aa| switch (b_info.storage) {
3013 .u64 => |bb| aa.orderAgainstScalar(bb) == .eq,
3014 .i64 => |bb| aa.orderAgainstScalar(bb) == .eq,
3015 .big_int => |bb| aa.eql(bb),
3016 },
3017 };
3018 },
3019
3020 .float => |a_info| {
3021 const b_info = b.float;
3022
3023 if (a_info.ty != b_info.ty)
3024 return false;
3025
3026 if (a_info.ty == .c_longdouble_type and a_info.storage != .f80) {
3027 // These are strange: we'll sometimes represent them as f128, even if the
3028 // underlying type is smaller. f80 is an exception: see float_c_longdouble_f80.
3029 const a_val: u128 = switch (a_info.storage) {
3030 inline else => |val| @bitCast(@as(f128, @floatCast(val))),
3031 };
3032 const b_val: u128 = switch (b_info.storage) {
3033 inline else => |val| @bitCast(@as(f128, @floatCast(val))),
3034 };
3035 return a_val == b_val;
3036 }
3037
3038 const StorageTag = @typeInfo(Key.Float.Storage).@"union".tag_type.?;
3039 assert(@as(StorageTag, a_info.storage) == @as(StorageTag, b_info.storage));
3040
3041 switch (a_info.storage) {
3042 inline else => |val, tag| {
3043 const Bits = @Int(.unsigned, @bitSizeOf(@TypeOf(val)));
3044 const a_bits: Bits = @bitCast(val);
3045 const b_bits: Bits = @bitCast(@field(b_info.storage, @tagName(tag)));
3046 return a_bits == b_bits;
3047 },
3048 }
3049 },
3050
3051 inline .opaque_type, .enum_type, .union_type, .struct_type => |a_info, a_tag_ct| {
3052 const b_info = @field(b, @tagName(a_tag_ct));
3053 if (std.meta.activeTag(a_info) != b_info) return false;
3054 switch (a_info) {
3055 .declared => |a_d| {
3056 const b_d = b_info.declared;
3057 if (a_d.zir_index != b_d.zir_index) return false;
3058 const a_captures = switch (a_d.captures) {
3059 .owned => |s| s.get(ip),
3060 .external => |cvs| cvs,
3061 };
3062 const b_captures = switch (b_d.captures) {
3063 .owned => |s| s.get(ip),
3064 .external => |cvs| cvs,
3065 };
3066 return std.mem.eql(u32, @ptrCast(a_captures), @ptrCast(b_captures));
3067 },
3068 .reified => |a_r| {
3069 const b_r = b_info.reified;
3070 return a_r.zir_index == b_r.zir_index and
3071 a_r.type_hash == b_r.type_hash;
3072 },
3073 .generated_union_tag => |a_union_ty| return a_union_ty == b_info.generated_union_tag,
3074 }
3075 },
3076 .aggregate => |a_info| {
3077 const b_info = b.aggregate;
3078 if (a_info.ty != b_info.ty) return false;
3079
3080 const len = ip.aggregateTypeLen(a_info.ty);
3081 const StorageTag = @typeInfo(Key.Aggregate.Storage).@"union".tag_type.?;
3082 if (@as(StorageTag, a_info.storage) != @as(StorageTag, b_info.storage)) {
3083 for (0..@intCast(len)) |elem_index| {
3084 const a_elem = switch (a_info.storage) {
3085 .bytes => |bytes| ip.getIfExists(.{ .int = .{
3086 .ty = .u8_type,
3087 .storage = .{ .u64 = bytes.at(elem_index, ip) },
3088 } }) orelse return false,
3089 .elems => |elems| elems[elem_index],
3090 .repeated_elem => |elem| elem,
3091 };
3092 const b_elem = switch (b_info.storage) {
3093 .bytes => |bytes| ip.getIfExists(.{ .int = .{
3094 .ty = .u8_type,
3095 .storage = .{ .u64 = bytes.at(elem_index, ip) },
3096 } }) orelse return false,
3097 .elems => |elems| elems[elem_index],
3098 .repeated_elem => |elem| elem,
3099 };
3100 if (a_elem != b_elem) return false;
3101 }
3102 return true;
3103 }
3104
3105 switch (a_info.storage) {
3106 .bytes => |a_bytes| {
3107 const b_bytes = b_info.storage.bytes;
3108 return a_bytes == b_bytes or
3109 std.mem.eql(u8, a_bytes.toSlice(len, ip), b_bytes.toSlice(len, ip));
3110 },
3111 .elems => |a_elems| {
3112 const b_elems = b_info.storage.elems;
3113 return std.mem.eql(
3114 Index,
3115 a_elems[0..@intCast(len)],
3116 b_elems[0..@intCast(len)],
3117 );
3118 },
3119 .repeated_elem => |a_elem| {
3120 const b_elem = b_info.storage.repeated_elem;
3121 return a_elem == b_elem;
3122 },
3123 }
3124 },
3125 .tuple_type => |a_info| {
3126 const b_info = b.tuple_type;
3127 return std.mem.eql(Index, a_info.types.get(ip), b_info.types.get(ip)) and
3128 std.mem.eql(Index, a_info.values.get(ip), b_info.values.get(ip));
3129 },
3130 .error_set_type => |a_info| {
3131 const b_info = b.error_set_type;
3132 return std.mem.eql(NullTerminatedString, a_info.names.get(ip), b_info.names.get(ip));
3133 },
3134 .inferred_error_set_type => |a_info| {
3135 const b_info = b.inferred_error_set_type;
3136 return a_info == b_info;
3137 },
3138
3139 .func_type => |a_info| {
3140 const b_info = b.func_type;
3141 return Key.FuncType.eql(a_info, b_info, ip);
3142 },
3143
3144 .memoized_call => |a_info| {
3145 const b_info = b.memoized_call;
3146 return a_info.func == b_info.func and
3147 std.mem.eql(Index, a_info.arg_values, b_info.arg_values);
3148 },
3149 }
3150 }
3151
3152 pub fn typeOf(key: Key) Index {
3153 return switch (key) {
3154 .int_type,
3155 .ptr_type,
3156 .array_type,
3157 .vector_type,
3158 .opt_type,
3159 .anyframe_type,
3160 .error_union_type,
3161 .error_set_type,
3162 .inferred_error_set_type,
3163 .simple_type,
3164 .struct_type,
3165 .union_type,
3166 .spirv_type,
3167 .opaque_type,
3168 .enum_type,
3169 .tuple_type,
3170 .func_type,
3171 => .type_type,
3172
3173 inline .ptr,
3174 .slice,
3175 .int,
3176 .float,
3177 .opt,
3178 .@"extern",
3179 .func,
3180 .err,
3181 .error_union,
3182 .enum_tag,
3183 .aggregate,
3184 .un,
3185 .bitpack,
3186 => |x| x.ty,
3187
3188 .enum_literal => .enum_literal_type,
3189
3190 .undef => |x| x,
3191
3192 .simple_value => |s| switch (s) {
3193 .void => .void_type,
3194 .null => .null_type,
3195 .false, .true => .bool_type,
3196 .@"unreachable" => .noreturn_type,
3197 },
3198
3199 .memoized_call => unreachable,
3200 };
3201 }
3202};
3203
3204pub const LoadedStructType = struct {
3205 /// Index of the `struct_decl` or `reify` ZIR instruction.
3206 zir_index: TrackedInst.Index,
3207 captures: CaptureValue.Slice,
3208 is_reified: bool,
3209
3210 // TODO: the non-fqn will be needed by the new dwarf structure
3211 /// The name of this struct type.
3212 name: NullTerminatedString,
3213 /// If this is a declared type with the `.parent` name strategy, this is the `Nav` it was named after.
3214 /// Otherwise, or if this is a file's root struct type, this is `.none`.
3215 name_nav: Nav.Index.Optional,
3216 namespace: NamespaceIndex,
3217
3218 layout: std.lang.Type.ContainerLayout,
3219 /// May be `undefined` if `layout != .@"packed"`.
3220 packed_backing_mode: BackingTypeMode,
3221
3222 /// Initially `false`, and set to `true` once any dependency on or reference to the struct's
3223 /// layout is encountered, after which it is never reset to `false`, even across incremental
3224 /// updates.
3225 ///
3226 /// This field is purely an optimization to avoid resolving the layout of types whose layouts
3227 /// are never demanded. If this field is `true` but the layout is not actually needed, the
3228 /// compiler frontend resolves this by traversing the reference graph at the end of each update
3229 /// with `Zcu.resolveReferences` and hiding compile errors which arise from this analysis.
3230 want_layout: bool,
3231
3232 // The remaining fields are only valid once the struct's layout is resolved.
3233 field_name_map: MapIndex,
3234 field_names: NullTerminatedString.Slice,
3235 field_types: Index.Slice,
3236 field_defaults: Index.Slice,
3237 field_aligns: Alignment.Slice,
3238 field_is_comptime_bits: ComptimeBits,
3239 /// If `layout` is `.@"packed"`, this is `.empty`.
3240 field_runtime_order: RuntimeOrder.Slice,
3241 /// If `layout` is `.@"packed"`, this is `.empty`.
3242 field_offsets: Offsets,
3243 /// Only valid if `layout` is `.@"packed"`.
3244 packed_backing_int_type: Index,
3245 /// Only valid if `layout` is *not* `.@"packed"`.
3246 class: TypeClass,
3247 /// Only valid if `layout` is *not* `.@"packed"`.
3248 size: u32,
3249 /// Only valid if `layout` is *not* `.@"packed"`.
3250 alignment: Alignment,
3251
3252 pub const ComptimeBits = struct {
3253 tid: Zcu.PerThread.Id,
3254 start: u32,
3255 /// This is the number of u32 elements, not the number of struct fields.
3256 len: u32,
3257
3258 pub const empty: ComptimeBits = .{ .tid = .main, .start = 0, .len = 0 };
3259
3260 pub fn getAll(this: ComptimeBits, ip: *const InternPool) []u32 {
3261 const extra = ip.getLocalShared(this.tid).extra.acquire();
3262 return extra.view().items(.@"0")[this.start..][0..this.len];
3263 }
3264
3265 pub fn get(this: ComptimeBits, ip: *const InternPool, i: usize) bool {
3266 if (this.len == 0) return false;
3267 return @as(u1, @truncate(this.getAll(ip)[i / 32] >> @intCast(i % 32))) != 0;
3268 }
3269 };
3270
3271 pub const Offsets = struct {
3272 tid: Zcu.PerThread.Id,
3273 start: u32,
3274 len: u32,
3275
3276 pub const empty: Offsets = .{ .tid = .main, .start = 0, .len = 0 };
3277
3278 pub fn get(this: Offsets, ip: *const InternPool) []u32 {
3279 const extra = ip.getLocalShared(this.tid).extra.acquire();
3280 return @ptrCast(extra.view().items(.@"0")[this.start..][0..this.len]);
3281 }
3282 };
3283
3284 pub const RuntimeOrder = enum(u32) {
3285 /// Placeholder until layout is resolved.
3286 unresolved = std.math.maxInt(u32) - 0,
3287 /// Field not present at runtime
3288 omitted = std.math.maxInt(u32) - 1,
3289 _,
3290
3291 pub const Slice = struct {
3292 tid: Zcu.PerThread.Id,
3293 start: u32,
3294 len: u32,
3295
3296 pub const empty: Slice = .{ .tid = .main, .start = 0, .len = 0 };
3297
3298 pub fn get(slice: RuntimeOrder.Slice, ip: *const InternPool) []RuntimeOrder {
3299 const extra = ip.getLocalShared(slice.tid).extra.acquire();
3300 return @ptrCast(extra.view().items(.@"0")[slice.start..][0..slice.len]);
3301 }
3302 };
3303
3304 pub fn toInt(i: RuntimeOrder) ?u32 {
3305 return switch (i) {
3306 .omitted => null,
3307 .unresolved => unreachable,
3308 else => @backingInt(i),
3309 };
3310 }
3311 };
3312
3313 /// Look up field index based on field name.
3314 pub fn nameIndex(s: LoadedStructType, ip: *const InternPool, name: NullTerminatedString) ?u32 {
3315 const map = s.field_name_map.get(ip);
3316 const adapter: NullTerminatedString.Adapter = .{ .strings = s.field_names.get(ip) };
3317 const field_index = map.getIndexAdapted(name, adapter) orelse return null;
3318 return @intCast(field_index);
3319 }
3320
3321 /// Iterates over non-comptime fields in the order they are laid out in memory at runtime.
3322 /// May or may not include zero-bit fields.
3323 /// Asserts the struct is not packed.
3324 pub fn iterateRuntimeOrder(s: *const LoadedStructType, ip: *const InternPool) RuntimeOrderIterator {
3325 switch (s.layout) {
3326 .auto => {
3327 const ro = std.mem.sliceTo(s.field_runtime_order.get(ip), .omitted);
3328 return .{
3329 .runtime_order = ro,
3330 .fields_len = @intCast(ro.len),
3331 .next_index = 0,
3332 };
3333 },
3334 .@"extern" => return .{
3335 .runtime_order = null,
3336 .fields_len = s.field_names.len,
3337 .next_index = 0,
3338 },
3339 .@"packed" => unreachable,
3340 }
3341 }
3342 pub const RuntimeOrderIterator = struct {
3343 runtime_order: ?[]const RuntimeOrder,
3344 fields_len: u32,
3345 next_index: u32,
3346 pub fn next(it: *RuntimeOrderIterator) ?u32 {
3347 const i = it.next_index;
3348 if (i == it.fields_len) return null;
3349 it.next_index = i + 1;
3350 const ro = it.runtime_order orelse return i;
3351 return ro[i].toInt().?;
3352 }
3353 };
3354
3355 pub fn iterateRuntimeOrderReverse(s: *const LoadedStructType, ip: *InternPool) ReverseRuntimeOrderIterator {
3356 switch (s.layout) {
3357 .auto => {
3358 const ro = std.mem.sliceTo(s.field_runtime_order.get(ip), .omitted);
3359 return .{
3360 .runtime_order = ro,
3361 .last_index = @intCast(ro.len),
3362 };
3363 },
3364 .@"extern" => return .{
3365 .runtime_order = null,
3366 .last_index = s.field_names.len,
3367 },
3368 .@"packed" => unreachable,
3369 }
3370 }
3371 pub const ReverseRuntimeOrderIterator = struct {
3372 runtime_order: ?[]const RuntimeOrder,
3373 last_index: u32,
3374 pub fn next(it: *ReverseRuntimeOrderIterator) ?u32 {
3375 if (it.last_index == 0) return null;
3376 const i = it.last_index - 1;
3377 it.last_index = i;
3378 const ro = it.runtime_order orelse return i;
3379 return ro[i].toInt().?;
3380 }
3381 };
3382};
3383
3384/// Unlike `Tag.TypeUnion` which is an encoding, and `Key.UnionType` which is a
3385/// minimal hashmap key, this type is a convenience type that contains info
3386/// needed by semantic analysis.
3387pub const LoadedUnionType = struct {
3388 /// Index of the `union_decl` or `reify` ZIR instruction.
3389 zir_index: TrackedInst.Index,
3390 captures: CaptureValue.Slice,
3391 is_reified: bool,
3392
3393 // TODO: the non-fqn will be needed by the new dwarf structure
3394 /// The name of this union type.
3395 name: NullTerminatedString,
3396 /// If this is a declared type with the `.parent` name strategy, this is the `Nav` it was named after.
3397 /// Otherwise, this is `.none`.
3398 name_nav: Nav.Index.Optional,
3399 namespace: NamespaceIndex,
3400
3401 layout: std.lang.Type.ContainerLayout,
3402 enum_tag_mode: BackingTypeMode,
3403 /// May be `undefined` if `layout != .@"packed"`.
3404 packed_backing_mode: BackingTypeMode,
3405
3406 /// Only reified unions store field names; typically they should be loaded from `enum_tag_type`
3407 /// instead. Reified unions store them because type resolution needs them in order to validate
3408 /// or populate `enum_tag_type`.
3409 reified_field_names: NullTerminatedString.Slice,
3410
3411 /// Initially `false`, and set to `true` once any dependency on or reference to the struct's
3412 /// layout is encountered, after which it is never reset to `false`, even across incremental
3413 /// updates.
3414 ///
3415 /// This field is purely an optimization to avoid resolving the layout of types whose layouts
3416 /// are never demanded. If this field is `true` but the layout is not actually needed, the
3417 /// compiler frontend resolves this by traversing the reference graph at the end of each update
3418 /// with `Zcu.resolveReferences` and hiding compile errors which arise from this analysis.
3419 want_layout: bool,
3420
3421 // The remaining fields are only valid once the union's layout is resolved.
3422 field_types: Index.Slice,
3423 field_aligns: Alignment.Slice,
3424 tag_usage: TagUsage,
3425 /// While `tag_usage` indicates whether the union should logically contain a tag, it may be
3426 /// omitted if the union layout is resolved as OPV or NPV. This field is `true` iff there is an
3427 /// actual runtime tag, with one or more runtime bits, in the union layout. It is always `false`
3428 /// if `layout` is not `.auto`.
3429 has_runtime_tag: bool,
3430 /// Even if `tag_usage == .none` and `has_runtime_tag == false`, this is still populated with
3431 /// the union's "hypothetical" tag type.
3432 enum_tag_type: Index,
3433 /// Only valid if `layout` is `.@"packed"`.
3434 packed_backing_int_type: Index,
3435 /// Not valid if `layout` is `.@"packed"`.
3436 class: TypeClass,
3437 /// Not valid if `layout` is `.@"packed"`.
3438 size: u32,
3439 /// Not valid if `layout` is `.@"packed"`.
3440 padding: u32,
3441 /// Not valid if `layout` is `.@"packed"`.
3442 alignment: Alignment,
3443
3444 pub const TagUsage = enum(u2) {
3445 none,
3446 safety,
3447 tagged,
3448 };
3449};
3450
3451pub const LoadedEnumType = struct {
3452 /// This is `none` iff this is a generated tag type.
3453 /// Otherwise, index of the `enum_decl` or `reify` ZIR instruction.
3454 zir_index: TrackedInst.Index.Optional,
3455 captures: CaptureValue.Slice,
3456 /// If `zir_index` is `.none`, this is the union type for which this enum is the tag type.
3457 owner_union: Index,
3458 is_reified: bool,
3459
3460 // TODO: the non-fqn will be needed by the new dwarf structure
3461 /// The name of this enum type.
3462 name: NullTerminatedString,
3463 /// If this is a declared type with the `.parent` name strategy, this is the `Nav` it was named after.
3464 /// Otherwise, this is `.none`.
3465 name_nav: Nav.Index.Optional,
3466 namespace: NamespaceIndex,
3467
3468 int_tag_mode: BackingTypeMode,
3469 nonexhaustive: bool,
3470
3471 /// Initially `false`, and set to `true` once any dependency on or reference to the struct's
3472 /// layout is encountered, after which it is never reset to `false`, even across incremental
3473 /// updates.
3474 ///
3475 /// This field is purely an optimization to avoid resolving the layout of types whose layouts
3476 /// are never demanded. If this field is `true` but the layout is not actually needed, the
3477 /// compiler frontend resolves this by traversing the reference graph at the end of each update
3478 /// with `Zcu.resolveReferences` and hiding compile errors which arise from this analysis.
3479 want_layout: bool,
3480
3481 // The remaining fields are only valid once the enum's layout is resolved.
3482 int_tag_type: Index,
3483 field_name_map: MapIndex,
3484 field_names: NullTerminatedString.Slice,
3485 field_value_map: OptionalMapIndex,
3486 field_values: Index.Slice,
3487
3488 /// Look up field index based on field name.
3489 pub fn nameIndex(e: LoadedEnumType, ip: *const InternPool, name: NullTerminatedString) ?u32 {
3490 const map = e.field_name_map.get(ip);
3491 const adapter: NullTerminatedString.Adapter = .{ .strings = e.field_names.get(ip) };
3492 const field_index = map.getIndexAdapted(name, adapter) orelse return null;
3493 return @intCast(field_index);
3494 }
3495
3496 /// Look up field index based on integer tag value.
3497 /// Asserts that the type of `tag_val` is `enum_obj.int_tag_type`.
3498 /// Asserts that `tag_val` is not `undefined`.
3499 pub fn tagValueIndex(e: LoadedEnumType, ip: *const InternPool, tag_val: Index) ?u32 {
3500 assert(ip.typeOf(tag_val) == e.int_tag_type);
3501 assert(ip.indexToKey(tag_val) == .int);
3502 if (e.field_value_map.unwrap()) |field_value_map| {
3503 const map = field_value_map.get(ip);
3504 const adapter: Index.Adapter = .{ .indexes = e.field_values.get(ip) };
3505 const field_index = map.getIndexAdapted(tag_val, adapter) orelse return null;
3506 return @intCast(field_index);
3507 }
3508 // Auto-numbered enum, so convert `tag_val` to field index
3509 const field_index = switch (ip.indexToKey(tag_val).int.storage) {
3510 inline .u64, .i64 => |x| std.math.cast(u32, x) orelse return null,
3511 .big_int => |x| x.toInt(u32) catch return null,
3512 };
3513 return if (field_index < e.field_names.len) field_index else null;
3514 }
3515};
3516
3517pub const LoadedOpaqueType = struct {
3518 /// Index of the `opaque_decl` instruction.
3519 zir_index: TrackedInst.Index,
3520 captures: CaptureValue.Slice,
3521
3522 // TODO: the non-fqn will be needed by the new dwarf structure
3523 /// The name of this opaque type.
3524 name: NullTerminatedString,
3525 /// If this is a declared type with the `.parent` name strategy, this is the `Nav` it was named after.
3526 /// Otherwise, this is `.none`.
3527 name_nav: Nav.Index.Optional,
3528 namespace: NamespaceIndex,
3529};
3530
3531pub fn loadStructType(ip: *const InternPool, index: Index) LoadedStructType {
3532 const unwrapped_index = index.unwrap(ip);
3533 const extra_list = unwrapped_index.getExtra(ip);
3534 const extra_items = extra_list.view().items(.@"0");
3535 const item = unwrapped_index.getItem(ip);
3536 // Exiting this `switch` means this is a `packed struct`.
3537 const backing_mode: BackingTypeMode, const any_defaults: bool = switch (item.tag) {
3538 .type_struct_packed_auto => .{ .auto, false },
3539 .type_struct_packed_explicit => .{ .explicit, false },
3540 .type_struct_packed_auto_defaults => .{ .auto, true },
3541 .type_struct_packed_explicit_defaults => .{ .explicit, true },
3542 .type_struct => {
3543 const extra = extraDataTrail(extra_list, Tag.TypeStruct, item.data);
3544 var extra_index = extra.end;
3545 const captures: CaptureValue.Slice = switch (extra.data.flags.any_captures) {
3546 .reified => captures: {
3547 extra_index += 2; // type_hash: PackedU64
3548 break :captures .empty;
3549 },
3550 .false => .empty,
3551 .true => captures: {
3552 const len = extra_items[extra_index];
3553 extra_index += 1;
3554 break :captures .{
3555 .tid = unwrapped_index.tid,
3556 .start = extra_index,
3557 .len = len,
3558 };
3559 },
3560 };
3561 extra_index += captures.len;
3562 const field_names: NullTerminatedString.Slice = .{
3563 .tid = unwrapped_index.tid,
3564 .start = extra_index,
3565 .len = extra.data.fields_len,
3566 };
3567 extra_index += field_names.len;
3568 const field_types: Index.Slice = .{
3569 .tid = unwrapped_index.tid,
3570 .start = extra_index,
3571 .len = extra.data.fields_len,
3572 };
3573 extra_index += field_types.len;
3574 const field_defaults: Index.Slice = if (extra.data.flags.any_field_defaults) .{
3575 .tid = unwrapped_index.tid,
3576 .start = extra_index,
3577 .len = extra.data.fields_len,
3578 } else .empty;
3579 extra_index += field_defaults.len;
3580 const field_aligns: Alignment.Slice = if (extra.data.flags.any_field_aligns) .{
3581 .tid = unwrapped_index.tid,
3582 .start = extra_index,
3583 .len = extra.data.fields_len,
3584 } else .empty;
3585 extra_index += @divCeil(field_aligns.len, 4);
3586 const field_is_comptime_bits: LoadedStructType.ComptimeBits = if (extra.data.flags.any_comptime_fields) .{
3587 .tid = unwrapped_index.tid,
3588 .start = extra_index,
3589 .len = @divCeil(extra.data.fields_len, 32),
3590 } else .empty;
3591 extra_index += field_is_comptime_bits.len;
3592 const field_runtime_order: LoadedStructType.RuntimeOrder.Slice = if (extra.data.flags.layout == .auto) .{
3593 .tid = unwrapped_index.tid,
3594 .start = extra_index,
3595 .len = extra.data.fields_len,
3596 } else .empty;
3597 extra_index += field_runtime_order.len;
3598 const field_offsets: LoadedStructType.Offsets = .{
3599 .tid = unwrapped_index.tid,
3600 .start = extra_index,
3601 .len = extra.data.fields_len,
3602 };
3603 extra_index += field_offsets.len;
3604
3605 return .{
3606 .zir_index = extra.data.zir_index,
3607 .captures = captures,
3608 .is_reified = extra.data.flags.any_captures == .reified,
3609 .name = extra.data.name,
3610 .name_nav = extra.data.name_nav,
3611 .namespace = extra.data.namespace,
3612 .layout = switch (extra.data.flags.layout) {
3613 .auto => .auto,
3614 .@"extern" => .@"extern",
3615 },
3616 .packed_backing_mode = undefined,
3617
3618 .want_layout = extra.data.flags.want_layout,
3619
3620 .field_name_map = extra.data.field_name_map,
3621 .field_names = field_names,
3622 .field_types = field_types,
3623 .field_defaults = field_defaults,
3624 .field_aligns = field_aligns,
3625 .field_is_comptime_bits = field_is_comptime_bits,
3626 .field_runtime_order = field_runtime_order,
3627 .field_offsets = field_offsets,
3628 .packed_backing_int_type = .none,
3629 .class = extra.data.flags.class,
3630 .size = extra.data.size,
3631 .alignment = extra.data.flags.alignment,
3632 };
3633 },
3634 else => unreachable,
3635 };
3636 const extra = extraDataTrail(extra_list, Tag.TypeStructPacked, item.data);
3637 var extra_index = extra.end;
3638 const captures: CaptureValue.Slice = switch (extra.data.bits.captures_len) {
3639 .reified => captures: {
3640 extra_index += 2; // type_hash: PackedU64
3641 break :captures .empty;
3642 },
3643 _ => |n| .{
3644 .tid = unwrapped_index.tid,
3645 .start = extra_index,
3646 .len = @backingInt(n),
3647 },
3648 };
3649 extra_index += captures.len;
3650 const field_names: NullTerminatedString.Slice = .{
3651 .tid = unwrapped_index.tid,
3652 .start = extra_index,
3653 .len = extra.data.fields_len,
3654 };
3655 extra_index += field_names.len;
3656 const field_types: Index.Slice = .{
3657 .tid = unwrapped_index.tid,
3658 .start = extra_index,
3659 .len = extra.data.fields_len,
3660 };
3661 extra_index += field_types.len;
3662 const field_defaults: Index.Slice = if (any_defaults) .{
3663 .tid = unwrapped_index.tid,
3664 .start = extra_index,
3665 .len = extra.data.fields_len,
3666 } else .empty;
3667 extra_index += field_defaults.len;
3668 return .{
3669 .zir_index = extra.data.zir_index,
3670 .captures = captures,
3671 .is_reified = extra.data.bits.captures_len == .reified,
3672 .name = extra.data.name,
3673 .name_nav = extra.data.name_nav,
3674 .namespace = extra.data.namespace,
3675 .layout = .@"packed",
3676 .packed_backing_mode = backing_mode,
3677
3678 .want_layout = extra.data.bits.want_layout,
3679
3680 .field_name_map = extra.data.field_name_map,
3681 .field_names = field_names,
3682 .field_types = field_types,
3683 .field_defaults = field_defaults,
3684 .field_aligns = .empty,
3685 .field_is_comptime_bits = .empty,
3686 .field_runtime_order = .empty,
3687 .field_offsets = .empty,
3688 .packed_backing_int_type = extra.data.backing_int_type,
3689 .class = undefined,
3690 .size = undefined,
3691 .alignment = undefined,
3692 };
3693}
3694
3695pub fn loadUnionType(ip: *const InternPool, index: Index) LoadedUnionType {
3696 const unwrapped_index = index.unwrap(ip);
3697 const extra_list = unwrapped_index.getExtra(ip);
3698 const extra_items = extra_list.view().items(.@"0");
3699 const item = unwrapped_index.getItem(ip);
3700 // Exiting this `switch` means this is a `packed union`.
3701 const backing_mode: BackingTypeMode = switch (item.tag) {
3702 .type_union_packed_auto => .auto,
3703 .type_union_packed_explicit => .explicit,
3704 .type_union => {
3705 const extra = extraDataTrail(extra_list, Tag.TypeUnion, item.data);
3706 var extra_index = extra.end;
3707 const captures: CaptureValue.Slice = switch (extra.data.flags.any_captures) {
3708 .reified => captures: {
3709 extra_index += 2; // type_hash: PackedU64
3710 break :captures .empty;
3711 },
3712 .false => .empty,
3713 .true => captures: {
3714 const len = extra_items[extra_index];
3715 extra_index += 1;
3716 break :captures .{
3717 .tid = unwrapped_index.tid,
3718 .start = extra_index,
3719 .len = len,
3720 };
3721 },
3722 };
3723 extra_index += captures.len;
3724 const reified_field_names: NullTerminatedString.Slice = if (extra.data.flags.any_captures == .reified) .{
3725 .tid = unwrapped_index.tid,
3726 .start = extra_index,
3727 .len = extra.data.fields_len,
3728 } else .empty;
3729 extra_index += reified_field_names.len;
3730 const field_types: Index.Slice = .{
3731 .tid = unwrapped_index.tid,
3732 .start = extra_index,
3733 .len = extra.data.fields_len,
3734 };
3735 extra_index += field_types.len;
3736 const field_aligns: Alignment.Slice = if (extra.data.flags.any_field_aligns) .{
3737 .tid = unwrapped_index.tid,
3738 .start = extra_index,
3739 .len = extra.data.fields_len,
3740 } else .empty;
3741 extra_index += @divCeil(field_aligns.len, 4);
3742
3743 return .{
3744 .zir_index = extra.data.zir_index,
3745 .captures = captures,
3746 .is_reified = extra.data.flags.any_captures == .reified,
3747 .name = extra.data.name,
3748 .name_nav = extra.data.name_nav,
3749 .namespace = extra.data.namespace,
3750 .layout = switch (extra.data.flags.layout) {
3751 .auto => .auto,
3752 .@"extern" => .@"extern",
3753 },
3754 .tag_usage = extra.data.flags.tag_usage,
3755 .enum_tag_mode = extra.data.flags.enum_tag_mode,
3756 .enum_tag_type = extra.data.enum_tag_type,
3757 .packed_backing_mode = undefined,
3758 .packed_backing_int_type = undefined,
3759 .reified_field_names = reified_field_names,
3760 .want_layout = extra.data.flags.want_layout,
3761 .field_types = field_types,
3762 .field_aligns = field_aligns,
3763 .has_runtime_tag = extra.data.flags.has_runtime_tag,
3764 .class = extra.data.flags.class,
3765 .size = extra.data.size,
3766 .padding = extra.data.padding,
3767 .alignment = extra.data.flags.alignment,
3768 };
3769 },
3770 else => unreachable,
3771 };
3772 const extra = extraDataTrail(extra_list, Tag.TypeUnionPacked, item.data);
3773 var extra_index = extra.end;
3774 const captures: CaptureValue.Slice = switch (extra.data.bits.captures_len) {
3775 .reified => captures: {
3776 extra_index += 2; // type_hash: PackedU64
3777 break :captures .empty;
3778 },
3779 _ => |n| .{
3780 .tid = unwrapped_index.tid,
3781 .start = extra_index,
3782 .len = @backingInt(n),
3783 },
3784 };
3785 extra_index += captures.len;
3786 const reified_field_names: NullTerminatedString.Slice = if (extra.data.bits.captures_len == .reified) .{
3787 .tid = unwrapped_index.tid,
3788 .start = extra_index,
3789 .len = extra.data.fields_len,
3790 } else .empty;
3791 extra_index += reified_field_names.len;
3792 const field_types: Index.Slice = .{
3793 .tid = unwrapped_index.tid,
3794 .start = extra_index,
3795 .len = extra.data.fields_len,
3796 };
3797 extra_index += field_types.len;
3798 return .{
3799 .zir_index = extra.data.zir_index,
3800 .captures = captures,
3801 .is_reified = extra.data.bits.captures_len == .reified,
3802 .name = extra.data.name,
3803 .name_nav = extra.data.name_nav,
3804 .namespace = extra.data.namespace,
3805 .layout = .@"packed",
3806 .tag_usage = .none,
3807 .enum_tag_mode = .auto,
3808 .enum_tag_type = extra.data.enum_tag_type,
3809 .packed_backing_mode = backing_mode,
3810 .packed_backing_int_type = extra.data.backing_int_type,
3811 .reified_field_names = reified_field_names,
3812 .want_layout = extra.data.bits.want_layout,
3813 .field_types = field_types,
3814 .field_aligns = .empty,
3815 .has_runtime_tag = false,
3816 .class = undefined,
3817 .size = undefined,
3818 .padding = undefined,
3819 .alignment = undefined,
3820 };
3821}
3822
3823pub fn loadEnumType(ip: *const InternPool, index: Index) LoadedEnumType {
3824 const unwrapped_index = index.unwrap(ip);
3825 const extra_list = unwrapped_index.getExtra(ip);
3826 const extra_items = extra_list.view().items(.@"0");
3827 const item = unwrapped_index.getItem(ip);
3828 const explicit_int_tag: bool, const nonexhaustive: bool = switch (item.tag) {
3829 .type_enum_auto => .{ false, false },
3830 .type_enum_explicit => .{ true, false },
3831 .type_enum_nonexhaustive => .{ true, true },
3832 else => unreachable,
3833 };
3834 const extra = extraDataTrail(extra_list, Tag.TypeEnum, item.data);
3835 var extra_index: u32 = @intCast(extra.end);
3836 const zir_index: TrackedInst.Index.Optional, const captures: CaptureValue.Slice, const owner_union: Index = switch (extra.data.bits.captures_len) {
3837 .reified => info: {
3838 const zir_index: TrackedInst.Index = @fromBackingInt(@intCast(extra_items[extra_index]));
3839 extra_index += 1;
3840 extra_index += 2; // type_hash: PackedU64
3841 break :info .{ zir_index.toOptional(), .empty, .none };
3842 },
3843 .generated_union_tag => info: {
3844 const owner_union: Index = @fromBackingInt(@intCast(extra_items[extra_index]));
3845 extra_index += 1;
3846 break :info .{ .none, .empty, owner_union };
3847 },
3848 _ => |n| info: {
3849 const zir_index: TrackedInst.Index = @fromBackingInt(@intCast(extra_items[extra_index]));
3850 extra_index += 1;
3851 const captures: CaptureValue.Slice = .{
3852 .tid = unwrapped_index.tid,
3853 .start = extra_index,
3854 .len = @backingInt(n),
3855 };
3856 extra_index += captures.len;
3857 break :info .{ zir_index.toOptional(), captures, .none };
3858 },
3859 };
3860 const field_value_map: OptionalMapIndex = if (explicit_int_tag) m: {
3861 const map: MapIndex = @fromBackingInt(@intCast(extra_items[extra_index]));
3862 extra_index += 1;
3863 break :m map.toOptional();
3864 } else .none;
3865 const field_names: NullTerminatedString.Slice = .{
3866 .tid = unwrapped_index.tid,
3867 .start = extra_index,
3868 .len = extra.data.fields_len,
3869 };
3870 extra_index += field_names.len;
3871 const field_values: Index.Slice = if (explicit_int_tag) .{
3872 .tid = unwrapped_index.tid,
3873 .start = extra_index,
3874 .len = extra.data.fields_len,
3875 } else .empty;
3876 extra_index += field_values.len;
3877 return .{
3878 .zir_index = zir_index,
3879 .captures = captures,
3880 .is_reified = extra.data.bits.captures_len == .reified,
3881 .owner_union = owner_union,
3882 .name = extra.data.name,
3883 .name_nav = extra.data.name_nav,
3884 .namespace = extra.data.namespace,
3885 .int_tag_type = extra.data.int_tag_type,
3886 .int_tag_mode = if (explicit_int_tag) .explicit else .auto,
3887 .nonexhaustive = nonexhaustive,
3888 .want_layout = extra.data.bits.want_layout,
3889 .field_name_map = extra.data.field_name_map,
3890 .field_value_map = field_value_map,
3891 .field_names = field_names,
3892 .field_values = field_values,
3893 };
3894}
3895
3896pub fn loadOpaqueType(ip: *const InternPool, index: Index) LoadedOpaqueType {
3897 const unwrapped_index = index.unwrap(ip);
3898 const item = unwrapped_index.getItem(ip);
3899 assert(item.tag == .type_opaque);
3900 const extra = extraDataTrail(unwrapped_index.getExtra(ip), Tag.TypeOpaque, item.data);
3901 return .{
3902 .zir_index = extra.data.zir_index,
3903 .captures = .{
3904 .tid = unwrapped_index.tid,
3905 .start = extra.end,
3906 .len = extra.data.captures_len,
3907 },
3908 .name = extra.data.name,
3909 .name_nav = extra.data.name_nav,
3910 .namespace = extra.data.namespace,
3911 };
3912}
3913
3914pub fn loadSpirvType(ip: *const InternPool, index: Index) Tag.TypeSpirv {
3915 const unwrapped_index = index.unwrap(ip);
3916 const item = unwrapped_index.getItem(ip);
3917 assert(item.tag == .type_spirv);
3918 const extra = extraData(unwrapped_index.getExtra(ip), Tag.TypeSpirv, item.data);
3919 return extra;
3920}
3921
3922pub const Item = struct {
3923 tag: Tag,
3924 /// The doc comments on the respective Tag explain how to interpret this.
3925 data: u32,
3926};
3927
3928/// Represents an index into `map`. It represents the canonical index
3929/// of a `Value` within this `InternPool`. The values are typed.
3930/// Two values which have the same type can be equality compared simply
3931/// by checking if their indexes are equal, provided they are both in
3932/// the same `InternPool`.
3933/// When adding a tag to this enum, consider adding a corresponding entry to
3934/// `primitives` in AstGen.zig.
3935pub const Index = enum(u32) {
3936 pub const first_type: Index = .u0_type;
3937 pub const last_type: Index = .empty_tuple_type;
3938 pub const first_value: Index = .undef;
3939 pub const last_value: Index = .empty_tuple;
3940
3941 u0_type,
3942 u1_type,
3943 u8_type,
3944 i8_type,
3945 u16_type,
3946 i16_type,
3947 u29_type,
3948 u32_type,
3949 i32_type,
3950 u64_type,
3951 i64_type,
3952 u80_type,
3953 u128_type,
3954 i128_type,
3955 u256_type,
3956 usize_type,
3957 isize_type,
3958 c_char_type,
3959 c_short_type,
3960 c_ushort_type,
3961 c_int_type,
3962 c_uint_type,
3963 c_long_type,
3964 c_ulong_type,
3965 c_longlong_type,
3966 c_ulonglong_type,
3967 c_longdouble_type,
3968 f16_type,
3969 f32_type,
3970 f64_type,
3971 f80_type,
3972 f128_type,
3973 anyopaque_type,
3974 bool_type,
3975 void_type,
3976 type_type,
3977 anyerror_type,
3978 comptime_int_type,
3979 comptime_float_type,
3980 noreturn_type,
3981 anyframe_type,
3982 null_type,
3983 undefined_type,
3984 enum_literal_type,
3985
3986 ptr_usize_type,
3987 ptr_const_comptime_int_type,
3988 manyptr_u8_type,
3989 manyptr_const_u8_type,
3990 manyptr_const_u8_sentinel_0_type,
3991 slice_const_u8_type,
3992 slice_const_u8_sentinel_0_type,
3993
3994 manyptr_const_slice_const_u8_type,
3995 slice_const_slice_const_u8_type,
3996
3997 optional_type_type,
3998 manyptr_const_type_type,
3999 slice_const_type_type,
4000
4001 vector_8_i8_type,
4002 vector_16_i8_type,
4003 vector_32_i8_type,
4004 vector_64_i8_type,
4005 vector_1_u8_type,
4006 vector_2_u8_type,
4007 vector_4_u8_type,
4008 vector_8_u8_type,
4009 vector_16_u8_type,
4010 vector_32_u8_type,
4011 vector_64_u8_type,
4012 vector_2_i16_type,
4013 vector_4_i16_type,
4014 vector_8_i16_type,
4015 vector_16_i16_type,
4016 vector_32_i16_type,
4017 vector_4_u16_type,
4018 vector_8_u16_type,
4019 vector_16_u16_type,
4020 vector_32_u16_type,
4021 vector_2_i32_type,
4022 vector_4_i32_type,
4023 vector_8_i32_type,
4024 vector_16_i32_type,
4025 vector_4_u32_type,
4026 vector_8_u32_type,
4027 vector_16_u32_type,
4028 vector_2_i64_type,
4029 vector_4_i64_type,
4030 vector_8_i64_type,
4031 vector_2_u64_type,
4032 vector_4_u64_type,
4033 vector_8_u64_type,
4034 vector_1_u128_type,
4035 vector_2_u128_type,
4036 vector_1_u256_type,
4037 vector_4_f16_type,
4038 vector_8_f16_type,
4039 vector_16_f16_type,
4040 vector_32_f16_type,
4041 vector_2_f32_type,
4042 vector_4_f32_type,
4043 vector_8_f32_type,
4044 vector_16_f32_type,
4045 vector_2_f64_type,
4046 vector_4_f64_type,
4047 vector_8_f64_type,
4048
4049 optional_noreturn_type,
4050 anyerror_void_error_union_type,
4051 /// Used for the inferred error set of inline/comptime function calls.
4052 adhoc_inferred_error_set_type,
4053 /// Represents a type which is unknown.
4054 /// This is used in functions to represent generic parameter/return types, and
4055 /// during semantic analysis to represent unknown result types (i.e. where AstGen
4056 /// thought we would have a result type, but we do not).
4057 generic_poison_type,
4058 /// `@TypeOf(.{})`; a tuple with zero elements.
4059 /// This is not the same as `struct {}`, since that is a struct rather than a tuple.
4060 empty_tuple_type,
4061
4062 /// `undefined` (untyped)
4063 undef,
4064 /// `@as(bool, undefined)`
4065 undef_bool,
4066 /// `@as(usize, undefined)`
4067 undef_usize,
4068 /// `@as(u1, undefined)`
4069 undef_u1,
4070 /// `0` (comptime_int)
4071 zero,
4072 /// `@as(usize, 0)`
4073 zero_usize,
4074 /// `@as(u1, 0)`
4075 zero_u1,
4076 /// `@as(u8, 0)`
4077 zero_u8,
4078 /// `1` (comptime_int)
4079 one,
4080 /// `@as(usize, 1)`
4081 one_usize,
4082 /// `@as(u1, 1)`
4083 one_u1,
4084 /// `@as(u8, 1)`
4085 one_u8,
4086 /// `@as(u8, 4)`
4087 four_u8,
4088 /// `-1` (comptime_int)
4089 negative_one,
4090 /// `{}`
4091 void_value,
4092 /// `unreachable` (noreturn type)
4093 unreachable_value,
4094 /// `null` (untyped)
4095 null_value,
4096 /// `true`
4097 bool_true,
4098 /// `false`
4099 bool_false,
4100 /// `.{}`
4101 empty_tuple,
4102
4103 /// Used by Air/Sema only.
4104 none = std.math.maxInt(u32),
4105
4106 _,
4107
4108 /// An array of `Index` existing within the `extra` array.
4109 /// This type exists to provide a struct with lifetime that is
4110 /// not invalidated when items are added to the `InternPool`.
4111 pub const Slice = struct {
4112 tid: Zcu.PerThread.Id,
4113 start: u32,
4114 len: u32,
4115
4116 pub const empty: Slice = .{ .tid = .main, .start = 0, .len = 0 };
4117
4118 pub fn get(slice: Slice, ip: *const InternPool) []Index {
4119 const extra = ip.getLocalShared(slice.tid).extra.acquire();
4120 return @ptrCast(extra.view().items(.@"0")[slice.start..][0..slice.len]);
4121 }
4122
4123 /// If `slice` is empty (`slice.len == 0`), returns `.none`.
4124 /// Otherwise, asserts that `index < slice.len`, and returns the value at `index`.
4125 pub fn getOrNone(slice: Slice, ip: *const InternPool, index: usize) Index {
4126 if (slice.len == 0) return .none;
4127 return slice.get(ip)[index];
4128 }
4129 };
4130
4131 /// Used for a map of `Index` values to the index within a list of `Index` values.
4132 const Adapter = struct {
4133 indexes: []const Index,
4134
4135 pub fn eql(ctx: @This(), a: Index, b_void: void, b_map_index: usize) bool {
4136 _ = b_void;
4137 return a == ctx.indexes[b_map_index];
4138 }
4139
4140 pub fn hash(ctx: @This(), a: Index) u32 {
4141 _ = ctx;
4142 return std.hash.int(@backingInt(a));
4143 }
4144 };
4145
4146 const Unwrapped = struct {
4147 tid: Zcu.PerThread.Id,
4148 index: u32,
4149
4150 fn wrap(unwrapped: Unwrapped, ip: *const InternPool) Index {
4151 assert(@backingInt(unwrapped.tid) <= ip.getTidMask());
4152 assert(unwrapped.index <= ip.getIndexMask(u30));
4153 return @fromBackingInt(@intCast(@shlExact(@as(u32, @backingInt(unwrapped.tid)), ip.tid_shift_30) |
4154 unwrapped.index));
4155 }
4156
4157 pub fn getExtra(unwrapped: Unwrapped, ip: *const InternPool) Local.Extra {
4158 return ip.getLocalShared(unwrapped.tid).extra.acquire();
4159 }
4160
4161 pub fn getItem(unwrapped: Unwrapped, ip: *const InternPool) Item {
4162 const item_ptr = unwrapped.itemPtr(ip);
4163 const tag = @atomicLoad(Tag, item_ptr.tag_ptr, .acquire);
4164 return .{ .tag = tag, .data = item_ptr.data_ptr.* };
4165 }
4166
4167 pub fn getTag(unwrapped: Unwrapped, ip: *const InternPool) Tag {
4168 const item_ptr = unwrapped.itemPtr(ip);
4169 return @atomicLoad(Tag, item_ptr.tag_ptr, .acquire);
4170 }
4171
4172 pub fn getData(unwrapped: Unwrapped, ip: *const InternPool) u32 {
4173 return unwrapped.getItem(ip).data;
4174 }
4175
4176 const ItemPtr = struct {
4177 tag_ptr: *Tag,
4178 data_ptr: *u32,
4179 };
4180 fn itemPtr(unwrapped: Unwrapped, ip: *const InternPool) ItemPtr {
4181 const slice = ip.getLocalShared(unwrapped.tid).items.acquire().view().slice();
4182 return .{
4183 .tag_ptr = &slice.items(.tag)[unwrapped.index],
4184 .data_ptr = &slice.items(.data)[unwrapped.index],
4185 };
4186 }
4187
4188 const debug_state = InternPool.debug_state;
4189 };
4190 pub fn unwrap(index: Index, ip: *const InternPool) Unwrapped {
4191 return .{
4192 .tid = @fromBackingInt(@intCast(@backingInt(index) >> ip.tid_shift_30 & ip.getTidMask())),
4193 .index = @backingInt(index) & ip.getIndexMask(u30),
4194 };
4195 }
4196
4197 /// This function is used in the debugger pretty formatters in lib/lldb/ to fetch the
4198 /// Tag to encoding mapping to facilitate fancy debug printing for this type.
4199 fn dbHelper(self: *Index, tag_to_encoding_map: *struct {
4200 const DataIsIndex = struct { data: Index };
4201
4202 removed: void,
4203 type_int_signed: struct { data: u32 },
4204 type_int_unsigned: struct { data: u32 },
4205 type_array_big: struct { data: *Array },
4206 type_array_small: struct { data: *Vector },
4207 type_vector: struct { data: *Vector },
4208 type_pointer: struct { data: *Tag.TypePointer },
4209 type_slice: DataIsIndex,
4210 type_optional: DataIsIndex,
4211 type_anyframe: DataIsIndex,
4212 type_error_union: struct { data: *Key.ErrorUnionType },
4213 type_anyerror_union: DataIsIndex,
4214 type_error_set: struct {
4215 const @"data.names_len" = opaque {};
4216 data: *Tag.ErrorSet,
4217 @"trailing.names.len": *@"data.names_len",
4218 trailing: struct { names: []NullTerminatedString },
4219 },
4220 type_inferred_error_set: DataIsIndex,
4221 simple_type: void,
4222 type_function: struct {
4223 const @"data.params_len" = opaque {};
4224 data: *Tag.TypeFunction,
4225 @"trailing.param_types.len": *@"data.params_len",
4226 trailing: struct { param_types: []Index },
4227 },
4228 type_tuple: struct {
4229 const @"data.fields_len" = opaque {};
4230 data: *TypeTuple,
4231 @"trailing.types.len": *@"data.fields_len",
4232 @"trailing.values.len": *@"data.fields_len",
4233 trailing: struct { types: []Index, values: []Index },
4234 },
4235
4236 type_struct: struct { data: *Tag.TypeStruct },
4237 type_struct_packed_auto: struct { data: *Tag.TypeStructPacked },
4238 type_struct_packed_explicit: struct { data: *Tag.TypeStructPacked },
4239 type_struct_packed_auto_defaults: struct { data: *Tag.TypeStructPacked },
4240 type_struct_packed_explicit_defaults: struct { data: *Tag.TypeStructPacked },
4241 type_union: struct { data: *Tag.TypeUnion },
4242 type_union_packed_auto: struct { data: *Tag.TypeUnionPacked },
4243 type_union_packed_explicit: struct { data: *Tag.TypeUnionPacked },
4244 type_enum_auto: struct { data: *Tag.TypeEnum },
4245 type_enum_explicit: struct { data: *Tag.TypeEnum },
4246 type_enum_nonexhaustive: struct { data: *Tag.TypeEnum },
4247 type_opaque: struct { data: *Tag.TypeOpaque },
4248
4249 type_spirv: struct { data: *Tag.TypeSpirv },
4250
4251 undef: DataIsIndex,
4252 simple_value: void,
4253 ptr_nav: struct { data: *PtrNav },
4254 ptr_comptime_alloc: struct { data: *PtrComptimeAlloc },
4255 ptr_uav: struct { data: *PtrUav },
4256 ptr_uav_aligned: struct { data: *PtrUavAligned },
4257 ptr_comptime_field: struct { data: *PtrComptimeField },
4258 ptr_int: struct { data: *PtrInt },
4259 ptr_eu_payload: struct { data: *PtrBase },
4260 ptr_opt_payload: struct { data: *PtrBase },
4261 ptr_elem: struct { data: *PtrBaseIndex },
4262 ptr_field: struct { data: *PtrBaseIndex },
4263 ptr_slice: struct { data: *PtrSlice },
4264 opt_payload: struct { data: *Tag.TypeValue },
4265 opt_null: DataIsIndex,
4266 int_u8: struct { data: u8 },
4267 int_u16: struct { data: u16 },
4268 int_u32: struct { data: u32 },
4269 int_i32: struct { data: i32 },
4270 int_usize: struct { data: u32 },
4271 int_comptime_int_u32: struct { data: u32 },
4272 int_comptime_int_i32: struct { data: i32 },
4273 int_small: struct { data: *IntSmall },
4274 int_positive: struct { data: u32 },
4275 int_negative: struct { data: u32 },
4276 error_set_error: struct { data: *Key.Error },
4277 error_union_error: struct { data: *Key.Error },
4278 error_union_payload: struct { data: *Tag.TypeValue },
4279 enum_literal: struct { data: NullTerminatedString },
4280 enum_tag: struct { data: *Tag.EnumTag },
4281 float_f16: struct { data: f16 },
4282 float_f32: struct { data: f32 },
4283 float_f64: struct { data: *Float64 },
4284 float_f80: struct { data: *Float80 },
4285 float_f128: struct { data: *Float128 },
4286 float_c_longdouble_f80: struct { data: *Float80 },
4287 float_c_longdouble_f128: struct { data: *Float128 },
4288 float_comptime_float: struct { data: *Float128 },
4289 @"extern": struct { data: *Tag.Extern },
4290 func_decl: struct {
4291 const @"data.analysis.inferred_error_set" = opaque {};
4292 data: *Tag.FuncDecl,
4293 @"trailing.resolved_error_set.len": *@"data.analysis.inferred_error_set",
4294 trailing: struct { resolved_error_set: []Index },
4295 },
4296 func_instance: struct {
4297 const @"data.analysis.inferred_error_set" = opaque {};
4298 const @"data.generic_owner.data.ty.data.params_len" = opaque {};
4299 data: *Tag.FuncInstance,
4300 @"trailing.resolved_error_set.len": *@"data.analysis.inferred_error_set",
4301 @"trailing.comptime_args.len": *@"data.generic_owner.data.ty.data.params_len",
4302 trailing: struct { resolved_error_set: []Index, comptime_args: []Index },
4303 },
4304 func_coerced: struct {
4305 data: *Tag.FuncCoerced,
4306 },
4307 only_possible_value: DataIsIndex,
4308 union_value: struct { data: *Key.Union },
4309 bytes: struct { data: *Bytes },
4310 aggregate: struct {
4311 const @"data.ty.data.len orelse data.ty.data.fields_len" = opaque {};
4312 data: *Tag.Aggregate,
4313 @"trailing.element_values.len": *@"data.ty.data.len orelse data.ty.data.fields_len",
4314 trailing: struct { element_values: []Index },
4315 },
4316 repeated: struct { data: *Repeated },
4317 bitpack: struct { data: *Key.Bitpack },
4318
4319 memoized_call: struct {
4320 const @"data.args_len" = opaque {};
4321 data: *MemoizedCall,
4322 @"trailing.arg_values.len": *@"data.args_len",
4323 trailing: struct { arg_values: []Index },
4324 },
4325 }) void {
4326 _ = self;
4327 const map_info = @typeInfo(@typeInfo(@TypeOf(tag_to_encoding_map)).pointer.child).@"struct";
4328 @setEvalBranchQuota(3_000);
4329 inline for (@typeInfo(Tag).@"enum".field_names, 0..) |tag_name, start| {
4330 inline for (0..map_info.field_names.len) |offset| {
4331 if (comptime std.mem.eql(u8, tag_name, map_info.field_names[(start + offset) % map_info.field_names.len])) break;
4332 } else {
4333 @compileError(@typeName(Tag) ++ "." ++ tag_name ++ " missing dbHelper tag_to_encoding_map entry");
4334 }
4335 }
4336 }
4337 comptime {
4338 if (!builtin.strip_debug_info) switch (builtin.zig_backend) {
4339 .stage2_llvm => _ = &dbHelper,
4340 .stage2_x86_64 => for (@typeInfo(Tag).@"enum".field_names) |tag_name| {
4341 if (!@hasField(@TypeOf(Tag.encodings), tag_name)) @compileLog("missing: " ++ @typeName(Tag) ++ ".encodings." ++ tag_name);
4342 const encoding = @field(Tag.encodings, tag_name);
4343 if (@hasField(@TypeOf(encoding), "trailing")) {
4344 const trailing_info = @typeInfo(encoding.trailing).@"struct";
4345 for (trailing_info.field_names, trailing_info.field_types) |trailing_field_name, trailing_field_type| {
4346 struct {
4347 fn checkConfig(name: []const u8) void {
4348 if (!@hasField(@TypeOf(encoding.config), name)) @compileError("missing field: " ++ @typeName(Tag) ++ ".encodings." ++ tag_name ++ ".config.@\"" ++ name ++ "\"");
4349 const FieldType = @TypeOf(@field(encoding.config, name));
4350 if (@typeInfo(FieldType) != .enum_literal) @compileError("expected enum literal: " ++ @typeName(Tag) ++ ".encodings." ++ tag_name ++ ".config.@\"" ++ name ++ "\": " ++ @typeName(FieldType));
4351 }
4352 fn checkField(name: []const u8, Type: type) void {
4353 switch (@typeInfo(Type)) {
4354 .int, .@"enum" => return,
4355 .@"struct" => |info| switch (info.layout) {
4356 .auto => unreachable,
4357 .@"extern" => {
4358 for (info.field_names, info.field_types) |field_name, field_type| checkField(name ++ "." ++ field_name, field_type);
4359 return;
4360 },
4361 .@"packed" => return,
4362 },
4363 .optional => |info| {
4364 checkConfig(name ++ ".?");
4365 checkField(name ++ ".?", info.child);
4366 return;
4367 },
4368 .pointer => |info| if (info.size == .slice) {
4369 checkConfig(name ++ ".len");
4370 checkField(name ++ "[0]", info.child);
4371 return;
4372 },
4373 else => {},
4374 }
4375 @compileError("unsupported type: " ++ @typeName(Tag) ++ ".encodings." ++ tag_name ++ "." ++ name ++ ": " ++ @typeName(Type));
4376 }
4377 }.checkField("trailing." ++ trailing_field_name, trailing_field_type);
4378 }
4379 }
4380 },
4381 else => {},
4382 };
4383 }
4384};
4385
4386pub const static_keys: [static_len]Key = .{
4387 .{ .int_type = .{
4388 .signedness = .unsigned,
4389 .bits = 0,
4390 } },
4391
4392 .{ .int_type = .{
4393 .signedness = .unsigned,
4394 .bits = 1,
4395 } },
4396
4397 .{ .int_type = .{
4398 .signedness = .unsigned,
4399 .bits = 8,
4400 } },
4401
4402 .{ .int_type = .{
4403 .signedness = .signed,
4404 .bits = 8,
4405 } },
4406
4407 .{ .int_type = .{
4408 .signedness = .unsigned,
4409 .bits = 16,
4410 } },
4411
4412 .{ .int_type = .{
4413 .signedness = .signed,
4414 .bits = 16,
4415 } },
4416
4417 .{ .int_type = .{
4418 .signedness = .unsigned,
4419 .bits = 29,
4420 } },
4421
4422 .{ .int_type = .{
4423 .signedness = .unsigned,
4424 .bits = 32,
4425 } },
4426
4427 .{ .int_type = .{
4428 .signedness = .signed,
4429 .bits = 32,
4430 } },
4431
4432 .{ .int_type = .{
4433 .signedness = .unsigned,
4434 .bits = 64,
4435 } },
4436
4437 .{ .int_type = .{
4438 .signedness = .signed,
4439 .bits = 64,
4440 } },
4441
4442 .{ .int_type = .{
4443 .signedness = .unsigned,
4444 .bits = 80,
4445 } },
4446
4447 .{ .int_type = .{
4448 .signedness = .unsigned,
4449 .bits = 128,
4450 } },
4451
4452 .{ .int_type = .{
4453 .signedness = .signed,
4454 .bits = 128,
4455 } },
4456
4457 .{ .int_type = .{
4458 .signedness = .unsigned,
4459 .bits = 256,
4460 } },
4461
4462 .{ .simple_type = .usize },
4463 .{ .simple_type = .isize },
4464 .{ .simple_type = .c_char },
4465 .{ .simple_type = .c_short },
4466 .{ .simple_type = .c_ushort },
4467 .{ .simple_type = .c_int },
4468 .{ .simple_type = .c_uint },
4469 .{ .simple_type = .c_long },
4470 .{ .simple_type = .c_ulong },
4471 .{ .simple_type = .c_longlong },
4472 .{ .simple_type = .c_ulonglong },
4473 .{ .simple_type = .c_longdouble },
4474 .{ .simple_type = .f16 },
4475 .{ .simple_type = .f32 },
4476 .{ .simple_type = .f64 },
4477 .{ .simple_type = .f80 },
4478 .{ .simple_type = .f128 },
4479 .{ .simple_type = .anyopaque },
4480 .{ .simple_type = .bool },
4481 .{ .simple_type = .void },
4482 .{ .simple_type = .type },
4483 .{ .simple_type = .anyerror },
4484 .{ .simple_type = .comptime_int },
4485 .{ .simple_type = .comptime_float },
4486 .{ .simple_type = .noreturn },
4487 .{ .anyframe_type = .none },
4488 .{ .simple_type = .null },
4489 .{ .simple_type = .undefined },
4490 .{ .simple_type = .enum_literal },
4491
4492 // *usize
4493 .{ .ptr_type = .{
4494 .child = .usize_type,
4495 .flags = .{},
4496 } },
4497
4498 // *const comptime_int
4499 .{ .ptr_type = .{
4500 .child = .comptime_int_type,
4501 .flags = .{
4502 .is_const = true,
4503 },
4504 } },
4505
4506 // [*]u8
4507 .{ .ptr_type = .{
4508 .child = .u8_type,
4509 .flags = .{
4510 .size = .many,
4511 },
4512 } },
4513
4514 // [*]const u8
4515 .{ .ptr_type = .{
4516 .child = .u8_type,
4517 .flags = .{
4518 .size = .many,
4519 .is_const = true,
4520 },
4521 } },
4522
4523 // [*:0]const u8
4524 .{ .ptr_type = .{
4525 .child = .u8_type,
4526 .sentinel = .zero_u8,
4527 .flags = .{
4528 .size = .many,
4529 .is_const = true,
4530 },
4531 } },
4532
4533 // []const u8
4534 .{ .ptr_type = .{
4535 .child = .u8_type,
4536 .flags = .{
4537 .size = .slice,
4538 .is_const = true,
4539 },
4540 } },
4541
4542 // [:0]const u8
4543 .{ .ptr_type = .{
4544 .child = .u8_type,
4545 .sentinel = .zero_u8,
4546 .flags = .{
4547 .size = .slice,
4548 .is_const = true,
4549 },
4550 } },
4551
4552 // [*]const []const u8
4553 .{ .ptr_type = .{
4554 .child = .slice_const_u8_type,
4555 .flags = .{
4556 .size = .many,
4557 .is_const = true,
4558 },
4559 } },
4560
4561 // []const []const u8
4562 .{ .ptr_type = .{
4563 .child = .slice_const_u8_type,
4564 .flags = .{
4565 .size = .slice,
4566 .is_const = true,
4567 },
4568 } },
4569
4570 // ?type
4571 .{ .opt_type = .type_type },
4572
4573 // [*]const type
4574 .{ .ptr_type = .{
4575 .child = .type_type,
4576 .flags = .{
4577 .size = .many,
4578 .is_const = true,
4579 },
4580 } },
4581
4582 // []const type
4583 .{ .ptr_type = .{
4584 .child = .type_type,
4585 .flags = .{
4586 .size = .slice,
4587 .is_const = true,
4588 },
4589 } },
4590
4591 // @Vector(8, i8)
4592 .{ .vector_type = .{ .len = 8, .child = .i8_type } },
4593 // @Vector(16, i8)
4594 .{ .vector_type = .{ .len = 16, .child = .i8_type } },
4595 // @Vector(32, i8)
4596 .{ .vector_type = .{ .len = 32, .child = .i8_type } },
4597 // @Vector(64, i8)
4598 .{ .vector_type = .{ .len = 64, .child = .i8_type } },
4599 // @Vector(1, u8)
4600 .{ .vector_type = .{ .len = 1, .child = .u8_type } },
4601 // @Vector(2, u8)
4602 .{ .vector_type = .{ .len = 2, .child = .u8_type } },
4603 // @Vector(4, u8)
4604 .{ .vector_type = .{ .len = 4, .child = .u8_type } },
4605 // @Vector(8, u8)
4606 .{ .vector_type = .{ .len = 8, .child = .u8_type } },
4607 // @Vector(16, u8)
4608 .{ .vector_type = .{ .len = 16, .child = .u8_type } },
4609 // @Vector(32, u8)
4610 .{ .vector_type = .{ .len = 32, .child = .u8_type } },
4611 // @Vector(64, u8)
4612 .{ .vector_type = .{ .len = 64, .child = .u8_type } },
4613 // @Vector(2, i16)
4614 .{ .vector_type = .{ .len = 2, .child = .i16_type } },
4615 // @Vector(4, i16)
4616 .{ .vector_type = .{ .len = 4, .child = .i16_type } },
4617 // @Vector(8, i16)
4618 .{ .vector_type = .{ .len = 8, .child = .i16_type } },
4619 // @Vector(16, i16)
4620 .{ .vector_type = .{ .len = 16, .child = .i16_type } },
4621 // @Vector(32, i16)
4622 .{ .vector_type = .{ .len = 32, .child = .i16_type } },
4623 // @Vector(4, u16)
4624 .{ .vector_type = .{ .len = 4, .child = .u16_type } },
4625 // @Vector(8, u16)
4626 .{ .vector_type = .{ .len = 8, .child = .u16_type } },
4627 // @Vector(16, u16)
4628 .{ .vector_type = .{ .len = 16, .child = .u16_type } },
4629 // @Vector(32, u16)
4630 .{ .vector_type = .{ .len = 32, .child = .u16_type } },
4631 // @Vector(2, i32)
4632 .{ .vector_type = .{ .len = 2, .child = .i32_type } },
4633 // @Vector(4, i32)
4634 .{ .vector_type = .{ .len = 4, .child = .i32_type } },
4635 // @Vector(8, i32)
4636 .{ .vector_type = .{ .len = 8, .child = .i32_type } },
4637 // @Vector(16, i32)
4638 .{ .vector_type = .{ .len = 16, .child = .i32_type } },
4639 // @Vector(4, u32)
4640 .{ .vector_type = .{ .len = 4, .child = .u32_type } },
4641 // @Vector(8, u32)
4642 .{ .vector_type = .{ .len = 8, .child = .u32_type } },
4643 // @Vector(16, u32)
4644 .{ .vector_type = .{ .len = 16, .child = .u32_type } },
4645 // @Vector(2, i64)
4646 .{ .vector_type = .{ .len = 2, .child = .i64_type } },
4647 // @Vector(4, i64)
4648 .{ .vector_type = .{ .len = 4, .child = .i64_type } },
4649 // @Vector(8, i64)
4650 .{ .vector_type = .{ .len = 8, .child = .i64_type } },
4651 // @Vector(2, u64)
4652 .{ .vector_type = .{ .len = 2, .child = .u64_type } },
4653 // @Vector(4, u64)
4654 .{ .vector_type = .{ .len = 4, .child = .u64_type } },
4655 // @Vector(8, u64)
4656 .{ .vector_type = .{ .len = 8, .child = .u64_type } },
4657 // @Vector(1, u128)
4658 .{ .vector_type = .{ .len = 1, .child = .u128_type } },
4659 // @Vector(2, u128)
4660 .{ .vector_type = .{ .len = 2, .child = .u128_type } },
4661 // @Vector(1, u256)
4662 .{ .vector_type = .{ .len = 1, .child = .u256_type } },
4663 // @Vector(4, f16)
4664 .{ .vector_type = .{ .len = 4, .child = .f16_type } },
4665 // @Vector(8, f16)
4666 .{ .vector_type = .{ .len = 8, .child = .f16_type } },
4667 // @Vector(16, f16)
4668 .{ .vector_type = .{ .len = 16, .child = .f16_type } },
4669 // @Vector(32, f16)
4670 .{ .vector_type = .{ .len = 32, .child = .f16_type } },
4671 // @Vector(2, f32)
4672 .{ .vector_type = .{ .len = 2, .child = .f32_type } },
4673 // @Vector(4, f32)
4674 .{ .vector_type = .{ .len = 4, .child = .f32_type } },
4675 // @Vector(8, f32)
4676 .{ .vector_type = .{ .len = 8, .child = .f32_type } },
4677 // @Vector(16, f32)
4678 .{ .vector_type = .{ .len = 16, .child = .f32_type } },
4679 // @Vector(2, f64)
4680 .{ .vector_type = .{ .len = 2, .child = .f64_type } },
4681 // @Vector(4, f64)
4682 .{ .vector_type = .{ .len = 4, .child = .f64_type } },
4683 // @Vector(8, f64)
4684 .{ .vector_type = .{ .len = 8, .child = .f64_type } },
4685
4686 // ?noreturn
4687 .{ .opt_type = .noreturn_type },
4688
4689 // anyerror!void
4690 .{ .error_union_type = .{
4691 .error_set_type = .anyerror_type,
4692 .payload_type = .void_type,
4693 } },
4694
4695 // adhoc_inferred_error_set_type
4696 .{ .simple_type = .adhoc_inferred_error_set },
4697 // generic_poison_type
4698 .{ .simple_type = .generic_poison },
4699
4700 // empty_tuple_type
4701 .{ .tuple_type = .{
4702 .types = .empty,
4703 .values = .empty,
4704 } },
4705
4706 .{ .undef = .undefined_type },
4707 .{ .undef = .bool_type },
4708 .{ .undef = .usize_type },
4709 .{ .undef = .u1_type },
4710
4711 .{ .int = .{
4712 .ty = .comptime_int_type,
4713 .storage = .{ .u64 = 0 },
4714 } },
4715
4716 .{ .int = .{
4717 .ty = .usize_type,
4718 .storage = .{ .u64 = 0 },
4719 } },
4720
4721 .{ .int = .{
4722 .ty = .u1_type,
4723 .storage = .{ .u64 = 0 },
4724 } },
4725
4726 .{ .int = .{
4727 .ty = .u8_type,
4728 .storage = .{ .u64 = 0 },
4729 } },
4730
4731 .{ .int = .{
4732 .ty = .comptime_int_type,
4733 .storage = .{ .u64 = 1 },
4734 } },
4735
4736 .{ .int = .{
4737 .ty = .usize_type,
4738 .storage = .{ .u64 = 1 },
4739 } },
4740
4741 .{ .int = .{
4742 .ty = .u1_type,
4743 .storage = .{ .u64 = 1 },
4744 } },
4745
4746 .{ .int = .{
4747 .ty = .u8_type,
4748 .storage = .{ .u64 = 1 },
4749 } },
4750
4751 .{ .int = .{
4752 .ty = .u8_type,
4753 .storage = .{ .u64 = 4 },
4754 } },
4755
4756 .{ .int = .{
4757 .ty = .comptime_int_type,
4758 .storage = .{ .i64 = -1 },
4759 } },
4760
4761 .{ .simple_value = .void },
4762 .{ .simple_value = .@"unreachable" },
4763 .{ .simple_value = .null },
4764 .{ .simple_value = .true },
4765 .{ .simple_value = .false },
4766
4767 .{ .aggregate = .{
4768 .ty = .empty_tuple_type,
4769 .storage = .{ .elems = &.{} },
4770 } },
4771};
4772
4773/// How many items in the InternPool are statically known.
4774/// This is specified with an integer literal and a corresponding comptime
4775/// assert below to break an unfortunate and arguably incorrect dependency loop
4776/// when compiling.
4777pub const static_len = Zir.Inst.Ref.static_len;
4778
4779pub const Tag = enum(u8) {
4780 /// This special tag represents a value which was removed from this pool via
4781 /// `InternPool.remove`. The item remains allocated to preserve indices, but
4782 /// lookups will consider it not equal to any other item, and all queries
4783 /// assert not this tag. `data` is unused.
4784 removed,
4785
4786 /// A type that can be represented with only an enum tag.
4787 simple_type,
4788 /// An integer type.
4789 /// data is number of bits
4790 type_int_signed,
4791 /// An integer type.
4792 /// data is number of bits
4793 type_int_unsigned,
4794 /// An array type whose length requires 64 bits or which has a sentinel.
4795 /// data is payload to Array.
4796 type_array_big,
4797 /// An array type that has no sentinel and whose length fits in 32 bits.
4798 /// data is payload to Vector.
4799 type_array_small,
4800 /// A vector type.
4801 /// data is payload to Vector.
4802 type_vector,
4803 /// A fully explicitly specified pointer type.
4804 type_pointer,
4805 /// A slice type.
4806 /// data is Index of underlying pointer type.
4807 type_slice,
4808 /// An optional type.
4809 /// data is the child type.
4810 type_optional,
4811 /// The type `anyframe->T`.
4812 /// data is the child type.
4813 /// If the child type is `none`, the type is `anyframe`.
4814 type_anyframe,
4815 /// An error union type.
4816 /// data is payload to `Key.ErrorUnionType`.
4817 type_error_union,
4818 /// An error union type of the form `anyerror!T`.
4819 /// data is `Index` of payload type.
4820 type_anyerror_union,
4821 /// An error set type.
4822 /// data is payload to `ErrorSet`.
4823 type_error_set,
4824 /// The inferred error set type of a function.
4825 /// data is `Index` of a `func_decl` or `func_instance`.
4826 type_inferred_error_set,
4827 /// A function body type.
4828 /// `data` is extra index to `TypeFunction`.
4829 type_function,
4830 /// A `TupleType`.
4831 /// data is extra index of `TypeTuple`.
4832 type_tuple,
4833
4834 /// A non-packed struct type.
4835 /// data is extra index of `TypeStruct`.
4836 type_struct,
4837 /// `packed struct { ... }` with no default field values.
4838 /// data is extra index of `TypeStructPacked`.
4839 type_struct_packed_auto,
4840 /// `packed struct(T) { ... }` with no default field values.
4841 /// data is extra index of `TypeStructPacked`.
4842 type_struct_packed_explicit,
4843 /// `packed struct { ... }` with one or more default field values.
4844 /// data is extra index of `TypeStructPacked`.
4845 type_struct_packed_auto_defaults,
4846 /// `packed struct(T) { ... }` with one or more default field values.
4847 /// data is extra index of `TypeStructPacked`.
4848 type_struct_packed_explicit_defaults,
4849
4850 /// A non-packed union type.
4851 /// data is extra index of `TypeUnion`.
4852 type_union,
4853 /// `packed union { ... }`.
4854 /// data is extra index of `TypeUnionPacked`.
4855 type_union_packed_auto,
4856 /// `packed union(T) { ... }`.
4857 /// data is extra index of `TypeUnionPacked`.
4858 type_union_packed_explicit,
4859
4860 /// An exhaustive enum type *without* an explicit integer tag type. The tag type is inferred.
4861 ///
4862 /// Because the tag type is inferred, there are no explicit field values.
4863 ///
4864 /// May be the generated tag type for a `union(enum)`.
4865 ///
4866 /// data is extra index of `TypeEnum`.
4867 type_enum_auto,
4868 /// An exhaustive enum type *with* an explicit integer tag type.
4869 ///
4870 /// May have explicit field values.
4871 ///
4872 /// May be the generated tag type for a `union(enum(T))`.
4873 ///
4874 /// data is extra index of `TypeEnum`.
4875 type_enum_explicit,
4876 /// An non-exhaustive enum type (with an explicit integer tag type, since it is required for
4877 /// non-exhaustive enums).
4878 ///
4879 /// May have explicit field values.
4880 ///
4881 /// This is *not* a union's generated tag type, because such types are always exhaustive.
4882 ///
4883 /// data is extra index of `TypeEnum`.
4884 type_enum_nonexhaustive,
4885
4886 /// An spirv type.
4887 /// data is index of `TypeSpirv` in extra.
4888 type_spirv,
4889
4890 /// An opaque type.
4891 /// data is extra index of `TypeOpaque`.
4892 type_opaque,
4893
4894 /// Typed `undefined`.
4895 /// `data` is `Index` of the type.
4896 /// Untyped `undefined` is stored instead via `simple_value`.
4897 undef,
4898 /// A value that can be represented with only an enum tag.
4899 simple_value,
4900 /// A pointer to a `Nav`.
4901 /// data is extra index of `PtrNav`, which contains the type and address.
4902 ptr_nav,
4903 /// A pointer to a decl that can be mutated at comptime.
4904 /// data is extra index of `PtrComptimeAlloc`, which contains the type and address.
4905 ptr_comptime_alloc,
4906 /// A pointer to an anonymous addressable value.
4907 /// data is extra index of `PtrUav`, which contains the pointer type and decl value.
4908 /// The alignment of the uav is communicated via the pointer type.
4909 ptr_uav,
4910 /// A pointer to an unnamed addressable value.
4911 /// data is extra index of `PtrUavAligned`, which contains the pointer
4912 /// type and decl value.
4913 /// The original pointer type is also provided, which will be different than `ty`.
4914 /// This encoding is only used when a pointer to a Uav is
4915 /// coerced to a different pointer type with a different alignment.
4916 ptr_uav_aligned,
4917 /// data is extra index of `PtrComptimeField`, which contains the pointer type and field value.
4918 ptr_comptime_field,
4919 /// A pointer with an integer value.
4920 /// data is extra index of `PtrInt`, which contains the type and address (byte offset from 0).
4921 /// Only pointer types are allowed to have this encoding. Optional types must use
4922 /// `opt_payload` or `opt_null`.
4923 ptr_int,
4924 /// A pointer to the payload of an error union.
4925 /// data is extra index of `PtrBase`, which contains the type and base pointer.
4926 ptr_eu_payload,
4927 /// A pointer to the payload of an optional.
4928 /// data is extra index of `PtrBase`, which contains the type and base pointer.
4929 ptr_opt_payload,
4930 /// A pointer to an array element.
4931 /// data is extra index of PtrBaseIndex, which contains the base array and element index.
4932 /// In order to use this encoding, one must ensure that the `InternPool`
4933 /// already contains the elem pointer type corresponding to this payload.
4934 ptr_elem,
4935 /// A pointer to a container field.
4936 /// data is extra index of PtrBaseIndex, which contains the base container and field index.
4937 ptr_field,
4938 /// A slice.
4939 /// data is extra index of PtrSlice, which contains the ptr and len values
4940 ptr_slice,
4941 /// An optional value that is non-null.
4942 /// data is extra index of `TypeValue`.
4943 /// The type is the optional type (not the payload type).
4944 opt_payload,
4945 /// An optional value that is null.
4946 /// data is Index of the optional type.
4947 opt_null,
4948 /// Type: u8
4949 /// data is integer value
4950 int_u8,
4951 /// Type: u16
4952 /// data is integer value
4953 int_u16,
4954 /// Type: u32
4955 /// data is integer value
4956 int_u32,
4957 /// Type: i32
4958 /// data is integer value bitcasted to u32.
4959 int_i32,
4960 /// A usize that fits in 32 bits.
4961 /// data is integer value.
4962 int_usize,
4963 /// A comptime_int that fits in a u32.
4964 /// data is integer value.
4965 int_comptime_int_u32,
4966 /// A comptime_int that fits in an i32.
4967 /// data is integer value bitcasted to u32.
4968 int_comptime_int_i32,
4969 /// An integer value that fits in 32 bits with an explicitly provided type.
4970 /// data is extra index of `IntSmall`.
4971 int_small,
4972 /// A positive integer value.
4973 /// data is a limbs index to `Int`.
4974 int_positive,
4975 /// A negative integer value.
4976 /// data is a limbs index to `Int`.
4977 int_negative,
4978 /// An error value.
4979 /// data is extra index of `Key.Error`.
4980 error_set_error,
4981 /// An error union error.
4982 /// data is extra index of `Key.Error`.
4983 error_union_error,
4984 /// An error union payload.
4985 /// data is extra index of `TypeValue`.
4986 error_union_payload,
4987 /// An enum literal value.
4988 /// data is `NullTerminatedString` of the error name.
4989 enum_literal,
4990 /// An enum tag value.
4991 /// data is extra index of `EnumTag`.
4992 enum_tag,
4993 /// An f16 value.
4994 /// data is float value bitcasted to u16 and zero-extended.
4995 float_f16,
4996 /// An f32 value.
4997 /// data is float value bitcasted to u32.
4998 float_f32,
4999 /// An f64 value.
5000 /// data is extra index to Float64.
5001 float_f64,
5002 /// An f80 value.
5003 /// data is extra index to Float80.
5004 float_f80,
5005 /// An f128 value.
5006 /// data is extra index to Float128.
5007 float_f128,
5008 /// A c_longdouble value of 80 bits.
5009 /// data is extra index to Float80.
5010 /// This is used when a c_longdouble value is provided as an f80, because f80 has unnormalized
5011 /// values which cannot be losslessly represented as f128. It should only be used when the type
5012 /// underlying c_longdouble for the target is 80 bits.
5013 float_c_longdouble_f80,
5014 /// A c_longdouble value of 128 bits.
5015 /// data is extra index to Float128.
5016 /// This is used when a c_longdouble value is provided as any type other than an f80, since all
5017 /// other float types can be losslessly converted to and from f128.
5018 float_c_longdouble_f128,
5019 /// A comptime_float value.
5020 /// data is extra index to Float128.
5021 float_comptime_float,
5022 /// An extern function or variable.
5023 /// data is extra index to Extern.
5024 /// Some parts of the key are stored in `owner_nav`.
5025 @"extern",
5026 /// A non-extern function corresponding directly to the AST node from whence it originated.
5027 /// data is extra index to `FuncDecl`.
5028 /// Only the owner Decl is used for hashing and equality because the other
5029 /// fields can get patched up during incremental compilation.
5030 func_decl,
5031 /// A generic function instantiation.
5032 /// data is extra index to `FuncInstance`.
5033 func_instance,
5034 /// A `func_decl` or a `func_instance` that has been coerced to a different type.
5035 /// data is extra index to `FuncCoerced`.
5036 func_coerced,
5037 /// This represents the only possible value for *some* types which have
5038 /// only one possible value. Not all only-possible-values are encoded this way;
5039 /// for example structs which have all comptime fields are not encoded this way.
5040 /// The set of values that are encoded this way is:
5041 /// * An array or vector which has length 0.
5042 /// * A struct which has all fields comptime-known.
5043 /// data is Index of the type, which is known to be zero bits at runtime.
5044 only_possible_value,
5045 /// data is extra index to Key.Union.
5046 union_value,
5047 /// An array of bytes.
5048 /// data is extra index to `Bytes`.
5049 bytes,
5050 /// An instance of a struct, array, or vector.
5051 /// data is extra index to `Aggregate`.
5052 aggregate,
5053 /// An instance of an array or vector with every element being the same value.
5054 /// data is extra index to `Repeated`.
5055 repeated,
5056 /// An instance of a `packed struct` or `packed union`.
5057 /// data is extra index to `Key.Bitpack`.
5058 bitpack,
5059
5060 /// A memoized comptime function call result.
5061 /// data is extra index to `MemoizedCall`
5062 memoized_call,
5063
5064 const ErrorUnionType = Key.ErrorUnionType;
5065 const TypeValue = Key.TypeValue;
5066 const Error = Key.Error;
5067 const EnumTag = Key.EnumTag;
5068 const Union = Key.Union;
5069 const TypePointer = Key.PtrType;
5070 const TypeSpirv = Key.SpirvType;
5071
5072 const struct_packed_encoding = .{
5073 .summary = .@"{.payload.name%summary#\"}",
5074 .payload = TypeStructPacked,
5075 .trailing = struct {
5076 type_hash: ?u64,
5077 captures: ?[]CaptureValue,
5078 field_names: []NullTerminatedString,
5079 field_types: []Index,
5080 },
5081 .config = .{
5082 .@"trailing.type_hash.?" = .@"payload.bits.captures_len == .reified",
5083 .@"trailing.captures.?" = .@"payload.bits.captures_len != .reified",
5084 .@"trailing.captures.?.len" = .@"@intFromEnum(payload.bits.captures_len)",
5085 .@"trailing.field_names.len" = .@"payload.fields_len",
5086 .@"trailing.field_types.len" = .@"payload.fields_len",
5087 },
5088 };
5089 const struct_packed_defaults_encoding = .{
5090 .summary = .@"{.payload.name%summary#\"}",
5091 .payload = TypeStructPacked,
5092 .trailing = struct {
5093 type_hash: ?u64,
5094 captures: ?[]CaptureValue,
5095 field_names: []NullTerminatedString,
5096 field_types: []Index,
5097 field_defaults: []Index,
5098 },
5099 .config = .{
5100 .@"trailing.type_hash.?" = .@"payload.bits.captures_len == .reified",
5101 .@"trailing.captures.?" = .@"payload.bits.captures_len != .reified",
5102 .@"trailing.captures.?.len" = .@"@intFromEnum(payload.bits.captures_len)",
5103 .@"trailing.field_names.len" = .@"payload.fields_len",
5104 .@"trailing.field_types.len" = .@"payload.fields_len",
5105 .@"trailing.field_defaults.len" = .@"payload.fields_len",
5106 },
5107 };
5108 const union_packed_encoding = .{
5109 .summary = .@"{.payload.name%summary#\"}",
5110 .payload = TypeUnionPacked,
5111 .trailing = struct {
5112 type_hash: ?u64,
5113 captures: ?[]CaptureValue,
5114 field_types: []Index,
5115 },
5116 .config = .{
5117 .@"trailing.type_hash.?" = .@"payload.bits.captures_len == .reified",
5118 .@"trailing.captures.?" = .@"payload.bits.captures_len != .reified",
5119 .@"trailing.captures.?.len" = .@"@intFromEnum(payload.bits.captures_len)",
5120 .@"trailing.field_types.len" = .@"payload.fields_len",
5121 },
5122 };
5123 const enum_explicit_encoding = .{
5124 .summary = .@"{.payload.name%summary#\"}",
5125 .payload = TypeEnum,
5126 .trailing = struct {
5127 owner_union: ?Index,
5128 zir_index: ?TrackedInst.Index,
5129 type_hash: ?u64,
5130 captures: ?[]CaptureValue,
5131 field_value_map: MapIndex,
5132 field_names: []NullTerminatedString,
5133 field_values: []Index,
5134 },
5135 .config = .{
5136 .@"trailing.owner_union.?" = .@"payload.bits.captures_len == .generated_union_tag",
5137 .@"trailing.zir_index.?" = .@"payload.bits.captures_len != .generated_union_tag",
5138 .@"trailing.type_hash.?" = .@"payload.bits.captures_len == .reified",
5139 .@"trailing.captures.?" = .@"payload.bits.captures_len != .reified and payload.bits.captures_len != .generated_union_tag",
5140 .@"trailing.captures.?.len" = .@"@intFromEnum(payload.bits.captures_len)",
5141 .@"trailing.field_names.len" = .@"payload.fields_len",
5142 .@"trailing.field_values.len" = .@"payload.fields_len",
5143 },
5144 };
5145 const encodings = .{
5146 .removed = .{},
5147
5148 .type_int_signed = .{ .summary = .@"i{.data%value}", .data = u32 },
5149 .type_int_unsigned = .{ .summary = .@"u{.data%value}", .data = u32 },
5150 .type_array_big = .{
5151 .summary = .@"[{.payload.len1%value} << 32 | {.payload.len0%value}:{.payload.sentinel%summary}]{.payload.child%summary}",
5152 .payload = Array,
5153 },
5154 .type_array_small = .{ .summary = .@"[{.payload.len%value}]{.payload.child%summary}", .payload = Vector },
5155 .type_vector = .{ .summary = .@"@Vector({.payload.len%value}, {.payload.child%summary})", .payload = Vector },
5156 .type_pointer = .{ .summary = .@"*... {.payload.child%summary}", .payload = TypePointer },
5157 .type_slice = .{ .summary = .@"[]... {.data.unwrapped.payload.child%summary}", .data = Index },
5158 .type_optional = .{ .summary = .@"?{.data%summary}", .data = Index },
5159 .type_anyframe = .{ .summary = .@"anyframe->{.data%summary}", .data = Index },
5160 .type_error_union = .{
5161 .summary = .@"{.payload.error_set_type%summary}!{.payload.payload_type%summary}",
5162 .payload = ErrorUnionType,
5163 },
5164 .type_anyerror_union = .{ .summary = .@"anyerror!{.data%summary}", .data = Index },
5165 .type_error_set = .{ .summary = .@"error{...}", .payload = ErrorSet },
5166 .type_inferred_error_set = .{
5167 .summary = .@"@typeInfo(@typeInfo(@TypeOf({.data%summary})).@\"fn\".return_type.?).error_union.error_set",
5168 .data = Index,
5169 },
5170 .simple_type = .{ .summary = .@"{.index%value#.}", .index = SimpleType },
5171 .type_tuple = .{
5172 .summary = .@"struct {...}",
5173 .payload = TypeTuple,
5174 .trailing = struct {
5175 field_types: []Index,
5176 field_values: []Index,
5177 },
5178 .config = .{
5179 .@"trailing.field_types.len" = .@"payload.fields_len",
5180 .@"trailing.field_values.len" = .@"payload.fields_len",
5181 },
5182 },
5183 .type_function = .{
5184 .summary = .@"fn (...) ... {.payload.return_type%summary}",
5185 .payload = TypeFunction,
5186 .trailing = struct {
5187 param_comptime_bits: ?[]u32,
5188 param_noalias_bits: ?[]u32,
5189 spirv_kernel_options: ?extern struct { x: u32, y: u32, z: u32 },
5190 spirv_mesh_options: ?extern struct { max_primitives: u32, max_vertices: u32, x: u32, y: u32, z: u32 },
5191 param_types: []Index,
5192 },
5193 .config = .{
5194 .@"trailing.param_comptime_bits.?" = .@"payload.flags.has_comptime_bits",
5195 .@"trailing.param_comptime_bits.?.len" = .@"(payload.params_len + 31) / 32",
5196 .@"trailing.param_noalias_bits.?" = .@"payload.flags.has_noalias_bits",
5197 .@"trailing.param_noalias_bits.?.len" = .@"(payload.params_len + 31) / 32",
5198 .@"trailing.spirv_kernel_options.?" = .@"payload.flags.cc.tag == .spirv_kernel or payload.flags.cc.tag == .spirv_task",
5199 .@"trailing.spirv_mesh_options.?" = .@"payload.flags.cc.tag == .spirv_mesh",
5200 .@"trailing.param_types.len" = .@"payload.params_len",
5201 },
5202 },
5203
5204 .type_struct = .{
5205 .summary = .@"{.payload.name%summary#\"}",
5206 .payload = TypeStruct,
5207 .trailing = struct {
5208 type_hash: ?u64,
5209 captures_len: ?u32,
5210 captures: ?[]CaptureValue,
5211 field_names: []NullTerminatedString,
5212 field_types: []Index,
5213 field_defaults: ?[]Index,
5214 field_aligns: ?[]Alignment,
5215 field_is_comptime_bits: ?[]u32,
5216 field_runtime_order: ?[]u32,
5217 field_offsets: []u32,
5218 },
5219 .config = .{
5220 .@"trailing.type_hash.?" = .@"payload.flags.any_captures == .reified",
5221 .@"trailing.captures_len.?" = .@"payload.flags.any_captures == .true",
5222 .@"trailing.captures.?" = .@"payload.flags.any_captures == .true",
5223 .@"trailing.captures.?.len" = .@"trailing.captures_len.?",
5224 .@"trailing.field_names.len" = .@"payload.fields_len",
5225 .@"trailing.field_types.len" = .@"payload.fields_len",
5226 .@"trailing.field_defaults.?" = .@"payload.flags.any_field_defaults",
5227 .@"trailing.field_defaults.?.len" = .@"payload.fields_len",
5228 .@"trailing.field_aligns.?" = .@"payload.flags.any_field_aligns",
5229 .@"trailing.field_aligns.?.len" = .@"(payload.fields_len + 3) / 4",
5230 .@"trailing.field_is_comptime_bits.?" = .@"payload.flags.any_comptime_fields",
5231 .@"trailing.field_is_comptime_bits.?.len" = .@"(payload.fields_len + 31) / 32",
5232 .@"trailing.field_runtime_order.?" = .@"payload.flags.layout == .auto",
5233 .@"trailing.field_runtime_order.?.len" = .@"payload.fields_len",
5234 .@"trailing.field_offsets.len" = .@"payload.fields_len",
5235 },
5236 },
5237 .type_struct_packed_auto = struct_packed_encoding,
5238 .type_struct_packed_explicit = struct_packed_encoding,
5239 .type_struct_packed_auto_defaults = struct_packed_defaults_encoding,
5240 .type_struct_packed_explicit_defaults = struct_packed_defaults_encoding,
5241 .type_union = .{
5242 .summary = .@"{.payload.name%summary#\"}",
5243 .payload = TypeUnion,
5244 .trailing = struct {
5245 type_hash: ?u64,
5246 captures_len: ?u32,
5247 captures: ?[]CaptureValue,
5248 field_types: []Index,
5249 field_aligns: ?[]Alignment,
5250 },
5251 .config = .{
5252 .@"trailing.type_hash.?" = .@"payload.flags.any_captures == .reified",
5253 .@"trailing.captures_len.?" = .@"payload.flags.any_captures == .true",
5254 .@"trailing.captures.?" = .@"payload.flags.any_captures == .true",
5255 .@"trailing.captures.?.len" = .@"trailing.captures_len.?",
5256 .@"trailing.field_types.len" = .@"payload.fields_len",
5257 .@"trailing.field_aligns.?" = .@"payloads.flags.any_field_aligns",
5258 .@"trailing.field_aligns.?.len" = .@"(payload.fields_len + 3) / 4",
5259 },
5260 },
5261 .type_union_packed_auto = union_packed_encoding,
5262 .type_union_packed_explicit = union_packed_encoding,
5263 .type_enum_auto = .{
5264 .summary = .@"{.payload.name%summary#\"}",
5265 .payload = TypeEnum,
5266 .trailing = struct {
5267 owner_union: ?Index,
5268 zir_index: ?TrackedInst.Index,
5269 type_hash: ?u64,
5270 captures: ?[]CaptureValue,
5271 field_names: []NullTerminatedString,
5272 },
5273 .config = .{
5274 .@"trailing.owner_union.?" = .@"payload.bits.captures_len == .generated_union_tag",
5275 .@"trailing.zir_index.?" = .@"payload.bits.captures_len != .generated_union_tag",
5276 .@"trailing.type_hash.?" = .@"payload.bits.captures_len == .reified",
5277 .@"trailing.captures.?" = .@"payload.bits.captures_len != .reified and payload.bits.captures_len != .generated_union_tag",
5278 .@"trailing.captures.?.len" = .@"@intFromEnum(payload.bits.captures_len)",
5279 .@"trailing.field_names.len" = .@"payload.fields_len",
5280 },
5281 },
5282 .type_enum_explicit = enum_explicit_encoding,
5283 .type_enum_nonexhaustive = enum_explicit_encoding,
5284 .type_spirv = .{ .payload = Tag.TypeSpirv },
5285 .type_opaque = .{
5286 .summary = .@"{.payload.name%summary#\"}",
5287 .payload = TypeOpaque,
5288 .trailing = struct { captures: []CaptureValue },
5289 .config = .{ .@"trailing.captures.len" = .@"payload.captures_len" },
5290 },
5291
5292 .undef = .{ .summary = .@"@as({.data%summary}, undefined)", .data = Index },
5293 .simple_value = .{ .summary = .@"{.index%value#.}", .index = SimpleValue },
5294 .ptr_nav = .{
5295 .summary = .@"@as({.payload.ty%summary}, @ptrFromInt(@intFromPtr(&{.payload.nav.fqn%summary#\"}) + ({.payload.byte_offset_a%value} << 32 | {.payload.byte_offset_b%value})))",
5296 .payload = PtrNav,
5297 },
5298 .ptr_comptime_alloc = .{
5299 .summary = .@"@as({.payload.ty%summary}, @ptrFromInt(@intFromPtr(&comptime_allocs[{.payload.index%summary}]) + ({.payload.byte_offset_a%value} << 32 | {.payload.byte_offset_b%value})))",
5300 .payload = PtrComptimeAlloc,
5301 },
5302 .ptr_uav = .{
5303 .summary = .@"@as({.payload.ty%summary}, @ptrFromInt(@intFromPtr(&{.payload.val%summary}) + ({.payload.byte_offset_a%value} << 32 | {.payload.byte_offset_b%value})))",
5304 .payload = PtrUav,
5305 },
5306 .ptr_uav_aligned = .{
5307 .summary = .@"@as({.payload.ty%summary}, @ptrFromInt(@intFromPtr(@as({.payload.orig_ty%summary}, &{.payload.val%summary})) + ({.payload.byte_offset_a%value} << 32 | {.payload.byte_offset_b%value})))",
5308 .payload = PtrUavAligned,
5309 },
5310 .ptr_comptime_field = .{
5311 .summary = .@"@as({.payload.ty%summary}, @ptrFromInt(@intFromPtr(&{.payload.field_val%summary}) + ({.payload.byte_offset_a%value} << 32 | {.payload.byte_offset_b%value})))",
5312 .payload = PtrComptimeField,
5313 },
5314 .ptr_int = .{
5315 .summary = .@"@as({.payload.ty%summary}, @ptrFromInt({.payload.byte_offset_a%value} << 32 | {.payload.byte_offset_b%value}))",
5316 .payload = PtrInt,
5317 },
5318 .ptr_eu_payload = .{
5319 .summary = .@"@as({.payload.ty%summary}, @ptrFromInt(@intFromPtr(&({.payload.base%summary} catch unreachable)) + ({.payload.byte_offset_a%value} << 32 | {.payload.byte_offset_b%value})))",
5320 .payload = PtrBase,
5321 },
5322 .ptr_opt_payload = .{
5323 .summary = .@"@as({.payload.ty%summary}, @ptrFromInt(@intFromPtr(&{.payload.base%summary}.?) + ({.payload.byte_offset_a%value} << 32 | {.payload.byte_offset_b%value})))",
5324 .payload = PtrBase,
5325 },
5326 .ptr_elem = .{
5327 .summary = .@"@as({.payload.ty%summary}, @ptrFromInt(@intFromPtr(&{.payload.base%summary}[{.payload.index%summary}]) + ({.payload.byte_offset_a%value} << 32 | {.payload.byte_offset_b%value})))",
5328 .payload = PtrBaseIndex,
5329 },
5330 .ptr_field = .{
5331 .summary = .@"@as({.payload.ty%summary}, @ptrFromInt(@intFromPtr(&{.payload.base%summary}[{.payload.index%summary}]) + ({.payload.byte_offset_a%value} << 32 | {.payload.byte_offset_b%value})))",
5332 .payload = PtrBaseIndex,
5333 },
5334 .ptr_slice = .{
5335 .summary = .@"{.payload.ptr%summary}[0..{.payload.len%summary}]",
5336 .payload = PtrSlice,
5337 },
5338 .opt_payload = .{ .summary = .@"@as({.payload.ty%summary}, {.payload.val%summary})", .payload = TypeValue },
5339 .opt_null = .{ .summary = .@"@as({.data%summary}, null)", .data = Index },
5340 .int_u8 = .{ .summary = .@"@as(u8, {.data%value})", .data = u8 },
5341 .int_u16 = .{ .summary = .@"@as(u16, {.data%value})", .data = u16 },
5342 .int_u32 = .{ .summary = .@"@as(u32, {.data%value})", .data = u32 },
5343 .int_i32 = .{ .summary = .@"@as(i32, {.data%value})", .data = i32 },
5344 .int_usize = .{ .summary = .@"@as(usize, {.data%value})", .data = u32 },
5345 .int_comptime_int_u32 = .{ .summary = .@"{.data%value}", .data = u32 },
5346 .int_comptime_int_i32 = .{ .summary = .@"{.data%value}", .data = i32 },
5347 .int_small = .{ .summary = .@"@as({.payload.ty%summary}, {.payload.value%value})", .payload = IntSmall },
5348 .int_positive = .{},
5349 .int_negative = .{},
5350 .error_set_error = .{ .summary = .@"@as({.payload.ty%summary}, error.@{.payload.name%summary})", .payload = Error },
5351 .error_union_error = .{ .summary = .@"@as({.payload.ty%summary}, error.@{.payload.name%summary})", .payload = Error },
5352 .error_union_payload = .{ .summary = .@"@as({.payload.ty%summary}, {.payload.val%summary})", .payload = TypeValue },
5353 .enum_literal = .{ .summary = .@".@{.data%summary}", .data = NullTerminatedString },
5354 .enum_tag = .{ .summary = .@"@as({.payload.ty%summary}, @enumFromInt({.payload.int%summary}))", .payload = EnumTag },
5355 .float_f16 = .{ .summary = .@"@as(f16, {.data%value})", .data = f16 },
5356 .float_f32 = .{ .summary = .@"@as(f32, {.data%value})", .data = f32 },
5357 .float_f64 = .{ .summary = .@"@as(f64, {.payload%value})", .payload = f64 },
5358 .float_f80 = .{ .summary = .@"@as(f80, {.payload%value})", .payload = f80 },
5359 .float_f128 = .{ .summary = .@"@as(f128, {.payload%value})", .payload = f128 },
5360 .float_c_longdouble_f80 = .{ .summary = .@"@as(c_longdouble, {.payload%value})", .payload = f80 },
5361 .float_c_longdouble_f128 = .{ .summary = .@"@as(c_longdouble, {.payload%value})", .payload = f128 },
5362 .float_comptime_float = .{ .summary = .@"{.payload%value}", .payload = f128 },
5363 .@"extern" = .{ .summary = .@"{.payload.owner_nav.fqn%summary#\"}", .payload = Extern },
5364 .func_decl = .{
5365 .summary = .@"{.payload.owner_nav.fqn%summary#\"}",
5366 .payload = FuncDecl,
5367 .trailing = struct { inferred_error_set: ?Index },
5368 .config = .{ .@"trailing.inferred_error_set.?" = .@"payload.analysis.inferred_error_set" },
5369 },
5370 .func_instance = .{
5371 .summary = .@"{.payload.owner_nav.fqn%summary#\"}",
5372 .payload = FuncInstance,
5373 .trailing = struct {
5374 inferred_error_set: ?Index,
5375 param_values: []Index,
5376 },
5377 .config = .{
5378 .@"trailing.inferred_error_set.?" = .@"payload.analysis.inferred_error_set",
5379 .@"trailing.param_values.len" = .@"payload.ty.payload.params_len",
5380 },
5381 },
5382 .func_coerced = .{
5383 .summary = .@"@as(*const {.payload.ty%summary}, @ptrCast(&{.payload.func%summary})).*",
5384 .payload = FuncCoerced,
5385 },
5386 .only_possible_value = .{ .summary = .@"@as({.data%summary}, undefined)", .data = Index },
5387 .union_value = .{ .summary = .@"@as({.payload.ty%summary}, {})", .payload = Union },
5388 .bytes = .{ .summary = .@"@as({.payload.ty%summary}, {.payload.bytes%summary}.*)", .payload = Bytes },
5389 .aggregate = .{
5390 .summary = .@"@as({.payload.ty%summary}, .{...})",
5391 .payload = Aggregate,
5392 .trailing = struct { elements: []Index },
5393 .config = .{ .@"trailing.elements.len" = .@"payload.ty.payload.fields_len" },
5394 },
5395 .repeated = .{ .summary = .@"@as({.payload.ty%summary}, @splat({.payload.elem_val%summary}))", .payload = Repeated },
5396 .bitpack = .{ .summary = .@"@as({.payload.ty%summary}, {})", .payload = Key.Bitpack },
5397
5398 .memoized_call = .{
5399 .summary = .@"@memoize({.payload.func%summary})",
5400 .payload = MemoizedCall,
5401 .trailing = struct { arg_values: []Index },
5402 .config = .{ .@"trailing.arg_values.len" = .@"payload.args_len" },
5403 },
5404 };
5405 fn Payload(comptime tag: Tag) type {
5406 return @field(encodings, @tagName(tag)).payload;
5407 }
5408
5409 pub const Extern = struct {
5410 // name, is_const, alignment, addrspace come from `owner_nav`.
5411 ty: Index,
5412 lib_name: OptionalNullTerminatedString,
5413 flags: Flags,
5414 owner_nav: Nav.Index,
5415 zir_index: TrackedInst.Index,
5416 location_or_descriptor_set: u32,
5417 descriptor_binding: u32,
5418
5419 pub const Flags = packed struct(u32) {
5420 linkage: std.lang.GlobalLinkage,
5421 visibility: std.lang.SymbolVisibility,
5422 is_dll_import: bool,
5423 relocation: std.lang.ExternOptions.Relocation,
5424 source: Source,
5425 decoration_type: DecorationType,
5426 _: u23 = 0,
5427
5428 pub const Source = enum(u1) { builtin, syntax };
5429 pub const DecorationType = enum(u2) { none, location, descriptor, flat };
5430 };
5431
5432 pub fn decoration(self: Extern) ?std.lang.ExternOptions.Decoration {
5433 return switch (self.flags.decoration_type) {
5434 .none => null,
5435 .location => std.lang.ExternOptions.Decoration{ .location = self.location_or_descriptor_set },
5436 .descriptor => std.lang.ExternOptions.Decoration{ .descriptor = .{ .set = self.location_or_descriptor_set, .binding = self.descriptor_binding } },
5437 .flat => std.lang.ExternOptions.Decoration{ .flat = self.location_or_descriptor_set },
5438 };
5439 }
5440 };
5441
5442 /// Trailing:
5443 /// 0. element: Index for each len
5444 /// len is determined by the aggregate type.
5445 pub const Aggregate = struct {
5446 /// The type of the aggregate.
5447 ty: Index,
5448 };
5449
5450 /// Trailing:
5451 /// 0. If `analysis.inferred_error_set` is `true`, `Index` of an `error_set` which
5452 /// is a regular error set corresponding to the finished inferred error set.
5453 /// A `none` value marks that the inferred error set is not resolved yet.
5454 pub const FuncDecl = struct {
5455 analysis: FuncAnalysis,
5456 owner_nav: Nav.Index,
5457 ty: Index,
5458 zir_body_inst: TrackedInst.Index,
5459 lbrace_line: u32,
5460 rbrace_line: u32,
5461 lbrace_column: u32,
5462 rbrace_column: u32,
5463 };
5464
5465 /// Trailing:
5466 /// 0. If `analysis.inferred_error_set` is `true`, `Index` of an `error_set` which
5467 /// is a regular error set corresponding to the finished inferred error set.
5468 /// A `none` value marks that the inferred error set is not resolved yet.
5469 /// 1. For each parameter of generic_owner: `Index` if comptime, otherwise `none`
5470 pub const FuncInstance = struct {
5471 analysis: FuncAnalysis,
5472 // Needed by the linker for codegen. Not part of hashing or equality.
5473 owner_nav: Nav.Index,
5474 ty: Index,
5475 branch_quota: u32,
5476 /// Points to a `FuncDecl`.
5477 generic_owner: Index,
5478 };
5479
5480 pub const FuncCoerced = struct {
5481 ty: Index,
5482 func: Index,
5483 };
5484
5485 /// Trailing:
5486 /// 0. name: NullTerminatedString for each names_len
5487 pub const ErrorSet = struct {
5488 names_len: u32,
5489 /// Maps error names to declaration index.
5490 names_map: MapIndex,
5491 };
5492
5493 /// Trailing:
5494 /// 0. comptime_bits: u32, // if has_comptime_bits
5495 /// 1. noalias_bits: u32, // if has_noalias_bits
5496 /// 2. param_type: Index for each params_len
5497 pub const TypeFunction = struct {
5498 params_len: u32,
5499 return_type: Index,
5500 flags: Flags,
5501
5502 pub const Flags = packed struct(u32) {
5503 cc: PackedCallingConvention,
5504 is_var_args: bool,
5505 has_comptime_bits: bool,
5506 has_noalias_bits: bool,
5507 is_noinline: bool,
5508 _: u10 = 0,
5509 };
5510 };
5511
5512 /// At first I thought of storing the denormalized data externally, such as...
5513 ///
5514 /// * runtime field order
5515 /// * calculated field offsets
5516 /// * size and alignment of the struct
5517 ///
5518 /// ...since these can be computed based on the other data here. However,
5519 /// this data does need to be memoized, and therefore stored in memory
5520 /// while the compiler is running, in order to avoid O(N^2) logic in many
5521 /// places. Since the data can be stored compactly in the InternPool
5522 /// representation, it is better for memory usage to store denormalized data
5523 /// here, and potentially also better for performance as well. It's also simpler
5524 /// than coming up with some other scheme for the data.
5525 ///
5526 /// Trailing:
5527 /// 0. type_hash: PackedU64 // if `any_captures == .reified`
5528 /// 1. captures_len: u32 // if `any_captures == .true`
5529 /// 2. capture: CaptureValue // for each `captures_len`
5530 /// 3. field_name: NullTerminatedString // for each `fields_len`
5531 /// 4. field_type: Index // for each `fields_len`
5532 /// 5. field_default: Index // if `any_field_defaults`; for each `fields_len`
5533 /// 6. field_align: Alignment // if `any_field_aligns`; for each `fields_len`
5534 /// 7. field_is_comptime_bits: u32 // if `any_comptime_fields`; minimum `u32` for `fields_len`; LSB is field 0
5535 /// 8. field_runtime_order: RuntimeOrder // if `layout == .auto`; for each `fields_len`
5536 /// 9. field_offset: u32 // for each `fields_len`
5537 pub const TypeStruct = struct {
5538 zir_index: TrackedInst.Index,
5539
5540 name: NullTerminatedString,
5541 name_nav: Nav.Index.Optional,
5542 namespace: NamespaceIndex,
5543
5544 fields_len: u32,
5545 field_name_map: MapIndex,
5546
5547 /// Size in bytes of the whole struct. Always 0 until layout resolved.
5548 size: u32,
5549
5550 flags: Flags,
5551
5552 pub const Flags = packed struct(u32) {
5553 any_captures: enum(u2) { true, false, reified },
5554
5555 /// `packed` layout is represented separately by `TypeStructPacked`.
5556 layout: enum(u1) { auto, @"extern" },
5557
5558 any_comptime_fields: bool,
5559 any_field_defaults: bool,
5560 any_field_aligns: bool,
5561
5562 class: TypeClass,
5563 /// Alignment of the whole struct. Always `.none` until layout resolved.
5564 alignment: Alignment,
5565
5566 want_layout: bool,
5567
5568 _: u16 = 0,
5569 };
5570 };
5571
5572 /// Trailing:
5573 /// 0. type_hash: PackedU64 // if `captures_len == .reified`
5574 /// 1. capture: CaptureValue // if `captures_len != .reified`; for each `captures_len`
5575 /// 2. field_name: NullTerminatedString // for each `fields_len`
5576 /// 3. field_type: Index // for each `fields_len`
5577 /// 4. field_default: Index // if item tag implies field defaults; for each `fields_len`
5578 pub const TypeStructPacked = struct {
5579 zir_index: TrackedInst.Index,
5580 bits: Bits,
5581
5582 name: NullTerminatedString,
5583 name_nav: Nav.Index.Optional,
5584 namespace: NamespaceIndex,
5585
5586 /// The corresponding `BackingTypeMode` depends on the item's `Tag`.
5587 backing_int_type: Index,
5588
5589 fields_len: u32,
5590 field_name_map: MapIndex,
5591
5592 const Bits = packed struct(u32) {
5593 captures_len: enum(u31) {
5594 reified = std.math.maxInt(u31),
5595 _,
5596 },
5597 want_layout: bool,
5598 };
5599 };
5600
5601 /// For declared unions, field names are intentionally omitted because they are available in
5602 /// `enum_tag_type`. However, reified unions do store field names, because they are needed by
5603 /// type resolution to create or validate the enum tag type (type resolution for declared unions
5604 /// instead fetches field names from ZIR).
5605 ///
5606 /// Trailing:
5607 /// 0. type_hash: PackedU64 // if `any_captures == .reified`
5608 /// 1. captures_len: u32 // if `any_captures == .true`
5609 /// 2. capture: CaptureValue // if `any_captures == .true`; for each `captures_len`
5610 /// 3. reified_field_name: NullTerminatedString // if `any_captures == .reified`; for each `fields_len`
5611 /// 4. field_type: Index // for each `fields_len`
5612 /// 5. field_align: Alignment // for each `fields_len` if `any_field_aligns`
5613 pub const TypeUnion = struct {
5614 zir_index: TrackedInst.Index,
5615
5616 name: NullTerminatedString,
5617 name_nav: Nav.Index.Optional,
5618 namespace: NamespaceIndex,
5619 /// The enum that provides the list of field names and values.
5620 enum_tag_type: Index,
5621
5622 /// This could be provided through the tag type, but it is more convenient
5623 /// to store it directly. This is also necessary for `dumpStatsFallible` to
5624 /// work on unresolved types.
5625 fields_len: u32,
5626
5627 /// Always 0 until layout resolved.
5628 size: u32,
5629 /// Always 0 until layout resolved.
5630 padding: u32,
5631
5632 flags: Flags,
5633
5634 pub const Flags = packed struct(u32) {
5635 any_captures: enum(u2) { true, false, reified },
5636
5637 /// Whether `enum_tag_type` was explicitly specified with `union(E)` syntax.
5638 ///
5639 /// For `union(enum(E))` syntax, this is `false`, but the generated enum tag type is
5640 /// considered to have an explicitly specified integer tag type.
5641 enum_tag_mode: BackingTypeMode,
5642
5643 /// `packed` layout is represented separately by `TypeStructPacked`.
5644 layout: enum(u1) { auto, @"extern" },
5645
5646 any_field_aligns: bool,
5647 tag_usage: LoadedUnionType.TagUsage,
5648
5649 class: TypeClass,
5650 has_runtime_tag: bool,
5651
5652 /// Alignment of the whole union. Always `.none` until layout resolved.
5653 alignment: Alignment,
5654
5655 want_layout: bool,
5656
5657 _: u14 = 0,
5658 };
5659 };
5660
5661 /// For declared unions, field names are intentionally omitted because they are available in
5662 /// `enum_tag_type`. However, reified unions do store field names, because they are needed by
5663 /// type resolution to create or validate the enum tag type (type resolution for declared unions
5664 /// instead fetches field names from ZIR).
5665 ///
5666 /// Trailing:
5667 /// 0. type_hash: PackedU64 // if `captures_len == .reified`
5668 /// 1. capture: CaptureValue // if `captures_len != .reified`; for each `captures_len`
5669 /// 2. reified_field_name: NullTerminatedString // if `captures_len == .reified`; for each `fields_len`
5670 /// 3. field_type: Index // for each `fields_len`
5671 pub const TypeUnionPacked = struct {
5672 zir_index: TrackedInst.Index,
5673 bits: Bits,
5674
5675 name: NullTerminatedString,
5676 name_nav: Nav.Index.Optional,
5677 namespace: NamespaceIndex,
5678
5679 /// The corresponding `BackingTypeMode` depends on the item's `Tag`.
5680 backing_int_type: Index,
5681 /// Although packed unions do not semantically have a tag type, the compiler still assigns
5682 /// them a "hypothetical" tag type.
5683 enum_tag_type: Index,
5684
5685 /// This could be provided through the tag type, but it is more convenient
5686 /// to store it directly. This is also necessary for `dumpStatsFallible` to
5687 /// work on unresolved types.
5688 fields_len: u32,
5689
5690 const Bits = packed struct(u32) {
5691 captures_len: enum(u31) {
5692 reified = std.math.maxInt(u31),
5693 _,
5694 },
5695 want_layout: bool,
5696 };
5697 };
5698
5699 /// Trailing:
5700 /// 0. owner_union: Index // if `captures_len == .generated_union_tag`
5701 /// 1. zir_index: TrackedInst.Index // if `captures_len != .generated_union_tag`
5702 /// 2. type_hash: PackedU64 // if `captures_len == .reified`
5703 /// 3. capture: CaptureValue // if `captures_len` is not a named tag; for each `captures_len`
5704 /// 4. field_value_map: MapIndex // if tag is not `.type_enum_auto`
5705 /// 5. field_name: NullTerminatedString // for each `fields_len`
5706 /// 6. field_value: Index // if tag is not `.type_enum_auto`; for each `fields_len`
5707 pub const TypeEnum = struct {
5708 bits: Bits,
5709
5710 name: NullTerminatedString,
5711 name_nav: Nav.Index.Optional,
5712 namespace: NamespaceIndex,
5713
5714 /// An integer type which is used for the numerical value of the enum. Whether this was
5715 /// user-provided or inferred by the compiler depends on the tag.
5716 int_tag_type: Index,
5717
5718 fields_len: u32,
5719 field_name_map: MapIndex,
5720
5721 const Bits = packed struct(u32) {
5722 captures_len: enum(u31) {
5723 reified = std.math.maxInt(u31),
5724 generated_union_tag = std.math.maxInt(u31) - 1,
5725 _,
5726 },
5727 want_layout: bool,
5728 };
5729 };
5730
5731 /// Trailing:
5732 /// 0. capture: CaptureValue // for each `captures_len`
5733 pub const TypeOpaque = struct {
5734 zir_index: TrackedInst.Index,
5735 captures_len: u32,
5736
5737 name: NullTerminatedString,
5738 name_nav: Nav.Index.Optional,
5739 namespace: NamespaceIndex,
5740 };
5741};
5742
5743/// Differentiates between user-provided and compiler-generated backing types for packed and tagged types.
5744pub const BackingTypeMode = enum(u1) {
5745 /// The backing type was explicitly provided by the user. For instance:
5746 /// union(T)
5747 /// enum(T)
5748 /// packed struct(T)
5749 /// packed union(T)
5750 /// Type layout resolution will evaluate the user-provided expression and validate that type.
5751 explicit,
5752 /// No backing type was explicitly provided by the user. Type layout resolution will populate
5753 /// an inferred/generated type.
5754 auto,
5755};
5756
5757/// State that is mutable during semantic analysis. This data is not used for
5758/// equality or hashing, except for `inferred_error_set` which is considered
5759/// to be part of the type of the function.
5760pub const FuncAnalysis = packed struct(u32) {
5761 want_runtime_analysis: bool,
5762 branch_hint: std.lang.BranchHint,
5763 is_noinline: bool,
5764 has_error_trace: bool,
5765 /// True if this function has an inferred error set.
5766 inferred_error_set: bool,
5767 disable_instrumentation: bool,
5768 disable_intrinsics: bool,
5769
5770 _: u23 = 0,
5771};
5772
5773pub const Bytes = struct {
5774 /// The type of the aggregate
5775 ty: Index,
5776 /// Index into strings, of len ip.aggregateTypeLen(ty)
5777 bytes: String,
5778};
5779
5780pub const Repeated = struct {
5781 /// The type of the aggregate.
5782 ty: Index,
5783 /// The value of every element.
5784 elem_val: Index,
5785};
5786
5787/// Trailing:
5788/// 0. type: Index for each fields_len
5789/// 1. value: Index for each fields_len
5790pub const TypeTuple = struct {
5791 fields_len: u32,
5792};
5793
5794/// Having `SimpleType` and `SimpleValue` in separate enums makes it easier to
5795/// implement logic that only wants to deal with types because the logic can
5796/// ignore all simple values. Note that technically, types are values.
5797pub const SimpleType = enum(u32) {
5798 f16 = @backingInt(Index.f16_type),
5799 f32 = @backingInt(Index.f32_type),
5800 f64 = @backingInt(Index.f64_type),
5801 f80 = @backingInt(Index.f80_type),
5802 f128 = @backingInt(Index.f128_type),
5803 usize = @backingInt(Index.usize_type),
5804 isize = @backingInt(Index.isize_type),
5805 c_char = @backingInt(Index.c_char_type),
5806 c_short = @backingInt(Index.c_short_type),
5807 c_ushort = @backingInt(Index.c_ushort_type),
5808 c_int = @backingInt(Index.c_int_type),
5809 c_uint = @backingInt(Index.c_uint_type),
5810 c_long = @backingInt(Index.c_long_type),
5811 c_ulong = @backingInt(Index.c_ulong_type),
5812 c_longlong = @backingInt(Index.c_longlong_type),
5813 c_ulonglong = @backingInt(Index.c_ulonglong_type),
5814 c_longdouble = @backingInt(Index.c_longdouble_type),
5815 anyopaque = @backingInt(Index.anyopaque_type),
5816 bool = @backingInt(Index.bool_type),
5817 void = @backingInt(Index.void_type),
5818 type = @backingInt(Index.type_type),
5819 anyerror = @backingInt(Index.anyerror_type),
5820 comptime_int = @backingInt(Index.comptime_int_type),
5821 comptime_float = @backingInt(Index.comptime_float_type),
5822 noreturn = @backingInt(Index.noreturn_type),
5823 null = @backingInt(Index.null_type),
5824 undefined = @backingInt(Index.undefined_type),
5825 enum_literal = @backingInt(Index.enum_literal_type),
5826
5827 adhoc_inferred_error_set = @backingInt(Index.adhoc_inferred_error_set_type),
5828 generic_poison = @backingInt(Index.generic_poison_type),
5829};
5830
5831pub const SimpleValue = enum(u32) {
5832 void = @backingInt(Index.void_value),
5833 /// This is untyped `null`.
5834 null = @backingInt(Index.null_value),
5835 true = @backingInt(Index.bool_true),
5836 false = @backingInt(Index.bool_false),
5837 @"unreachable" = @backingInt(Index.unreachable_value),
5838};
5839
5840/// Stored as a power-of-two, with one special value to indicate none.
5841pub const Alignment = enum(u6) {
5842 @"1" = 0,
5843 @"2" = 1,
5844 @"4" = 2,
5845 @"8" = 3,
5846 @"16" = 4,
5847 @"32" = 5,
5848 @"64" = 6,
5849 none = std.math.maxInt(u6),
5850 _,
5851
5852 pub fn toByteUnits(a: Alignment) ?u64 {
5853 return switch (a) {
5854 .none => null,
5855 else => @as(u64, 1) << @backingInt(a),
5856 };
5857 }
5858
5859 pub fn fromByteUnits(n: u64) Alignment {
5860 if (n == 0) return .none;
5861 assert(std.math.isPowerOfTwo(n));
5862 return @fromBackingInt(@intCast(@ctz(n)));
5863 }
5864
5865 pub fn fromNonzeroByteUnits(n: u64) Alignment {
5866 assert(n != 0);
5867 return fromByteUnits(n);
5868 }
5869
5870 pub fn toLog2Units(a: Alignment) u6 {
5871 assert(a != .none);
5872 return @backingInt(a);
5873 }
5874
5875 /// This is just a glorified `@enumFromInt` but using it can help
5876 /// document the intended conversion.
5877 /// The parameter uses a u32 for convenience at the callsite.
5878 pub fn fromLog2Units(a: u32) Alignment {
5879 assert(a != @backingInt(Alignment.none));
5880 return @fromBackingInt(@intCast(a));
5881 }
5882
5883 pub fn order(lhs: Alignment, rhs: Alignment) std.math.Order {
5884 assert(lhs != .none);
5885 assert(rhs != .none);
5886 return std.math.order(@backingInt(lhs), @backingInt(rhs));
5887 }
5888
5889 /// Relaxed comparison. We have this as default because a lot of callsites
5890 /// were upgraded from directly using comparison operators on byte units,
5891 /// with the `none` value represented by zero.
5892 /// Prefer `compareStrict` if possible.
5893 pub fn compare(lhs: Alignment, op: std.math.CompareOperator, rhs: Alignment) bool {
5894 return std.math.compare(lhs.toRelaxedCompareUnits(), op, rhs.toRelaxedCompareUnits());
5895 }
5896
5897 pub fn compareStrict(lhs: Alignment, op: std.math.CompareOperator, rhs: Alignment) bool {
5898 assert(lhs != .none);
5899 assert(rhs != .none);
5900 return std.math.compare(@backingInt(lhs), op, @backingInt(rhs));
5901 }
5902
5903 /// Treats `none` as zero.
5904 /// This matches previous behavior of using `@max` directly on byte units.
5905 /// Prefer `maxStrict` if possible.
5906 pub fn max(lhs: Alignment, rhs: Alignment) Alignment {
5907 if (lhs == .none) return rhs;
5908 if (rhs == .none) return lhs;
5909 return maxStrict(lhs, rhs);
5910 }
5911
5912 pub fn maxStrict(lhs: Alignment, rhs: Alignment) Alignment {
5913 assert(lhs != .none);
5914 assert(rhs != .none);
5915 return @fromBackingInt(@intCast(@max(@backingInt(lhs), @backingInt(rhs))));
5916 }
5917
5918 /// Treats `none` as zero.
5919 /// This matches previous behavior of using `@min` directly on byte units.
5920 /// Prefer `minStrict` if possible.
5921 pub fn min(lhs: Alignment, rhs: Alignment) Alignment {
5922 if (lhs == .none) return lhs;
5923 if (rhs == .none) return rhs;
5924 return minStrict(lhs, rhs);
5925 }
5926
5927 pub fn minStrict(lhs: Alignment, rhs: Alignment) Alignment {
5928 assert(lhs != .none);
5929 assert(rhs != .none);
5930 return @fromBackingInt(@intCast(@min(@backingInt(lhs), @backingInt(rhs))));
5931 }
5932
5933 /// Given a base address known to be aligned to `a`,
5934 /// computes the known alignment of base address plus `off`.
5935 pub fn offset(a: Alignment, off: u64) Alignment {
5936 return .fromLog2Units(@min(a.toLog2Units(), @ctz(off)));
5937 }
5938
5939 /// Align an address forwards to this alignment.
5940 pub fn forward(a: Alignment, addr: u64) u64 {
5941 assert(a != .none);
5942 const x = (@as(u64, 1) << @backingInt(a)) - 1;
5943 return (addr + x) & ~x;
5944 }
5945
5946 /// Align an address backwards to this alignment.
5947 pub fn backward(a: Alignment, addr: u64) u64 {
5948 assert(a != .none);
5949 const x = (@as(u64, 1) << @backingInt(a)) - 1;
5950 return addr & ~x;
5951 }
5952
5953 /// Check if an address is aligned to this amount.
5954 pub fn check(a: Alignment, addr: u64) bool {
5955 assert(a != .none);
5956 return @ctz(addr) >= @backingInt(a);
5957 }
5958
5959 /// An array of `Alignment` objects existing within the `extra` array.
5960 /// This type exists to provide a struct with lifetime that is
5961 /// not invalidated when items are added to the `InternPool`.
5962 pub const Slice = struct {
5963 tid: Zcu.PerThread.Id,
5964 start: u32,
5965 /// This is the number of alignment values, not the number of u32 elements.
5966 len: u32,
5967
5968 pub const empty: Slice = .{ .tid = .main, .start = 0, .len = 0 };
5969
5970 pub fn get(slice: Slice, ip: *const InternPool) []Alignment {
5971 const extra = ip.getLocalShared(slice.tid).extra.acquire();
5972 const bytes: []u8 = @ptrCast(extra.view().items(.@"0")[slice.start..]);
5973 return @ptrCast(bytes[0..slice.len]);
5974 }
5975
5976 /// If `slice` is empty (`slice.len == 0`), returns `.none`.
5977 /// Otherwise, asserts that `index < slice.len`, and returns the value at `index`.
5978 pub fn getOrNone(slice: Slice, ip: *const InternPool, index: usize) Alignment {
5979 if (slice.len == 0) return .none;
5980 return slice.get(ip)[index];
5981 }
5982 };
5983
5984 pub fn toRelaxedCompareUnits(a: Alignment) u8 {
5985 const n: u8 = @backingInt(a);
5986 assert(n <= @backingInt(Alignment.none));
5987 if (n == @backingInt(Alignment.none)) return 0;
5988 return n + 1;
5989 }
5990
5991 pub fn toLlvm(a: Alignment) std.zig.llvm.Builder.Alignment {
5992 return @fromBackingInt(@intCast(@backingInt(a)));
5993 }
5994};
5995
5996/// Used for non-sentineled arrays that have length fitting in u32, as well as
5997/// vectors.
5998pub const Vector = struct {
5999 len: u32,
6000 child: Index,
6001};
6002
6003pub const Array = struct {
6004 len0: u32,
6005 len1: u32,
6006 child: Index,
6007 sentinel: Index,
6008
6009 pub const Length = PackedU64;
6010
6011 pub fn getLength(a: Array) u64 {
6012 return (PackedU64{
6013 .a = a.len0,
6014 .b = a.len1,
6015 }).get();
6016 }
6017};
6018
6019pub const PackedU64 = packed struct(u64) {
6020 a: u32,
6021 b: u32,
6022
6023 pub fn get(x: PackedU64) u64 {
6024 return @bitCast(x);
6025 }
6026
6027 pub fn init(x: u64) PackedU64 {
6028 return @bitCast(x);
6029 }
6030};
6031
6032pub const PtrNav = struct {
6033 ty: Index,
6034 nav: Nav.Index,
6035 byte_offset_a: u32,
6036 byte_offset_b: u32,
6037 fn init(ty: Index, nav: Nav.Index, byte_offset: u64) @This() {
6038 return .{
6039 .ty = ty,
6040 .nav = nav,
6041 .byte_offset_a = @intCast(byte_offset >> 32),
6042 .byte_offset_b = @truncate(byte_offset),
6043 };
6044 }
6045 fn byteOffset(data: @This()) u64 {
6046 return @as(u64, data.byte_offset_a) << 32 | data.byte_offset_b;
6047 }
6048};
6049
6050pub const PtrUav = struct {
6051 ty: Index,
6052 val: Index,
6053 byte_offset_a: u32,
6054 byte_offset_b: u32,
6055 fn init(ty: Index, val: Index, byte_offset: u64) @This() {
6056 return .{
6057 .ty = ty,
6058 .val = val,
6059 .byte_offset_a = @intCast(byte_offset >> 32),
6060 .byte_offset_b = @truncate(byte_offset),
6061 };
6062 }
6063 fn byteOffset(data: @This()) u64 {
6064 return @as(u64, data.byte_offset_a) << 32 | data.byte_offset_b;
6065 }
6066};
6067
6068pub const PtrUavAligned = struct {
6069 ty: Index,
6070 val: Index,
6071 /// Must be nonequal to `ty`. Only the alignment from this value is important.
6072 orig_ty: Index,
6073 byte_offset_a: u32,
6074 byte_offset_b: u32,
6075 fn init(ty: Index, val: Index, orig_ty: Index, byte_offset: u64) @This() {
6076 return .{
6077 .ty = ty,
6078 .val = val,
6079 .orig_ty = orig_ty,
6080 .byte_offset_a = @intCast(byte_offset >> 32),
6081 .byte_offset_b = @truncate(byte_offset),
6082 };
6083 }
6084 fn byteOffset(data: @This()) u64 {
6085 return @as(u64, data.byte_offset_a) << 32 | data.byte_offset_b;
6086 }
6087};
6088
6089pub const PtrComptimeAlloc = struct {
6090 ty: Index,
6091 index: ComptimeAllocIndex,
6092 byte_offset_a: u32,
6093 byte_offset_b: u32,
6094 fn init(ty: Index, index: ComptimeAllocIndex, byte_offset: u64) @This() {
6095 return .{
6096 .ty = ty,
6097 .index = index,
6098 .byte_offset_a = @intCast(byte_offset >> 32),
6099 .byte_offset_b = @truncate(byte_offset),
6100 };
6101 }
6102 fn byteOffset(data: @This()) u64 {
6103 return @as(u64, data.byte_offset_a) << 32 | data.byte_offset_b;
6104 }
6105};
6106
6107pub const PtrComptimeField = struct {
6108 ty: Index,
6109 field_val: Index,
6110 byte_offset_a: u32,
6111 byte_offset_b: u32,
6112 fn init(ty: Index, field_val: Index, byte_offset: u64) @This() {
6113 return .{
6114 .ty = ty,
6115 .field_val = field_val,
6116 .byte_offset_a = @intCast(byte_offset >> 32),
6117 .byte_offset_b = @truncate(byte_offset),
6118 };
6119 }
6120 fn byteOffset(data: @This()) u64 {
6121 return @as(u64, data.byte_offset_a) << 32 | data.byte_offset_b;
6122 }
6123};
6124
6125pub const PtrBase = struct {
6126 ty: Index,
6127 base: Index,
6128 byte_offset_a: u32,
6129 byte_offset_b: u32,
6130 fn init(ty: Index, base: Index, byte_offset: u64) @This() {
6131 return .{
6132 .ty = ty,
6133 .base = base,
6134 .byte_offset_a = @intCast(byte_offset >> 32),
6135 .byte_offset_b = @truncate(byte_offset),
6136 };
6137 }
6138 fn byteOffset(data: @This()) u64 {
6139 return @as(u64, data.byte_offset_a) << 32 | data.byte_offset_b;
6140 }
6141};
6142
6143pub const PtrBaseIndex = struct {
6144 ty: Index,
6145 base: Index,
6146 index: Index,
6147 byte_offset_a: u32,
6148 byte_offset_b: u32,
6149 fn init(ty: Index, base: Index, index: Index, byte_offset: u64) @This() {
6150 return .{
6151 .ty = ty,
6152 .base = base,
6153 .index = index,
6154 .byte_offset_a = @intCast(byte_offset >> 32),
6155 .byte_offset_b = @truncate(byte_offset),
6156 };
6157 }
6158 fn byteOffset(data: @This()) u64 {
6159 return @as(u64, data.byte_offset_a) << 32 | data.byte_offset_b;
6160 }
6161};
6162
6163pub const PtrInt = struct {
6164 ty: Index,
6165 byte_offset_a: u32,
6166 byte_offset_b: u32,
6167 fn init(ty: Index, byte_offset: u64) @This() {
6168 return .{
6169 .ty = ty,
6170 .byte_offset_a = @intCast(byte_offset >> 32),
6171 .byte_offset_b = @truncate(byte_offset),
6172 };
6173 }
6174 fn byteOffset(data: @This()) u64 {
6175 return @as(u64, data.byte_offset_a) << 32 | data.byte_offset_b;
6176 }
6177};
6178
6179pub const PtrSlice = struct {
6180 /// The slice type.
6181 ty: Index,
6182 /// A many pointer value.
6183 ptr: Index,
6184 /// A usize value.
6185 len: Index,
6186};
6187
6188/// Trailing: Limb for every limbs_len
6189pub const Int = packed struct {
6190 ty: Index,
6191 limbs_len: u32,
6192
6193 const limbs_items_len = @divExact(@sizeOf(Int), @sizeOf(Limb));
6194};
6195
6196pub const IntSmall = struct {
6197 ty: Index,
6198 value: u32,
6199};
6200
6201/// A f64 value, broken up into 2 u32 parts.
6202pub const Float64 = struct {
6203 piece0: u32,
6204 piece1: u32,
6205
6206 pub fn get(self: Float64) f64 {
6207 const int_bits = @as(u64, self.piece0) | (@as(u64, self.piece1) << 32);
6208 return @bitCast(int_bits);
6209 }
6210
6211 fn pack(val: f64) Float64 {
6212 const bits: u64 = @bitCast(val);
6213 return .{
6214 .piece0 = @truncate(bits),
6215 .piece1 = @truncate(bits >> 32),
6216 };
6217 }
6218};
6219
6220/// A f80 value, broken up into 2 u32 parts and a u16 part zero-padded to a u32.
6221pub const Float80 = struct {
6222 piece0: u32,
6223 piece1: u32,
6224 piece2: u32, // u16 part, top bits
6225
6226 pub fn get(self: Float80) f80 {
6227 const int_bits = @as(u80, self.piece0) |
6228 (@as(u80, self.piece1) << 32) |
6229 (@as(u80, self.piece2) << 64);
6230 return @bitCast(int_bits);
6231 }
6232
6233 fn pack(val: f80) Float80 {
6234 const bits: u80 = @bitCast(val);
6235 return .{
6236 .piece0 = @truncate(bits),
6237 .piece1 = @truncate(bits >> 32),
6238 .piece2 = @truncate(bits >> 64),
6239 };
6240 }
6241};
6242
6243/// A f128 value, broken up into 4 u32 parts.
6244pub const Float128 = struct {
6245 piece0: u32,
6246 piece1: u32,
6247 piece2: u32,
6248 piece3: u32,
6249
6250 pub fn get(self: Float128) f128 {
6251 const int_bits = @as(u128, self.piece0) |
6252 (@as(u128, self.piece1) << 32) |
6253 (@as(u128, self.piece2) << 64) |
6254 (@as(u128, self.piece3) << 96);
6255 return @bitCast(int_bits);
6256 }
6257
6258 fn pack(val: f128) Float128 {
6259 const bits: u128 = @bitCast(val);
6260 return .{
6261 .piece0 = @truncate(bits),
6262 .piece1 = @truncate(bits >> 32),
6263 .piece2 = @truncate(bits >> 64),
6264 .piece3 = @truncate(bits >> 96),
6265 };
6266 }
6267};
6268
6269/// Trailing:
6270/// 0. arg value: Index for each args_len
6271pub const MemoizedCall = struct {
6272 func: Index,
6273 args_len: u32,
6274 result: Index,
6275 branch_count: u32,
6276 branch_quota: u32,
6277};
6278
6279pub fn init(ip: *InternPool, gpa: Allocator, io: Io, available_threads: usize) !void {
6280 errdefer ip.deinit(gpa, io);
6281 assert(ip.locals.len == 0 and ip.shards.len == 0);
6282 assert(available_threads > 0 and available_threads <= std.math.maxInt(u8));
6283
6284 const used_threads = if (single_threaded) 1 else @max(available_threads, 2);
6285 ip.locals = try gpa.alloc(Local, used_threads);
6286 @memset(ip.locals, .{
6287 .shared = .{
6288 .items = .empty,
6289 .extra = .empty,
6290 .limbs = .empty,
6291 .strings = .empty,
6292 .string_bytes = .empty,
6293 .tracked_insts = .empty,
6294 .files = .empty,
6295 .maps = .empty,
6296 .navs = .empty,
6297 .comptime_units = .empty,
6298
6299 .namespaces = .empty,
6300 },
6301 .mutate = .{
6302 .arena = .{},
6303
6304 .items = .empty,
6305 .extra = .empty,
6306 .limbs = .empty,
6307 .strings = .empty,
6308 .string_bytes = .empty,
6309 .tracked_insts = .empty,
6310 .files = .empty,
6311 .maps = .empty,
6312 .navs = .empty,
6313 .comptime_units = .empty,
6314
6315 .namespaces = .empty,
6316 },
6317 });
6318 for (ip.locals) |*local| try local.getMutableStrings(gpa, io).append(.{0});
6319
6320 ip.tid_width = @intCast(std.math.log2_int_ceil(usize, used_threads));
6321 ip.tid_shift_30 = if (single_threaded) 0 else 30 - ip.tid_width;
6322 ip.tid_shift_31 = if (single_threaded) 0 else 31 - ip.tid_width;
6323 ip.tid_shift_32 = if (single_threaded) 0 else ip.tid_shift_31 +| 1;
6324 ip.shards = try gpa.alloc(Shard, @as(usize, 1) << ip.tid_width);
6325 @memset(ip.shards, .{
6326 .shared = .{
6327 .map = .empty,
6328 .string_map = .empty,
6329 .tracked_inst_map = .empty,
6330 },
6331 .mutate = .{
6332 .map = .empty,
6333 .string_map = .empty,
6334 .tracked_inst_map = .empty,
6335 },
6336 });
6337
6338 // Reserve string index 0 for an empty string.
6339 assert((try ip.getOrPutString(gpa, io, .main, "", .no_embedded_nulls)) == .empty);
6340
6341 // This inserts all the statically-known values into the intern pool in the
6342 // order expected.
6343 for (&static_keys, 0..) |key, key_index| switch (@as(Index, @fromBackingInt(@intCast(key_index)))) {
6344 .empty_tuple_type => assert(try ip.getTupleType(gpa, io, .main, .{
6345 .types = &.{},
6346 .values = &.{},
6347 }) == .empty_tuple_type),
6348 else => |expected_index| assert(try ip.get(gpa, io, .main, key) == expected_index),
6349 };
6350
6351 if (std.debug.runtime_safety) {
6352 // Sanity check.
6353 assert(ip.indexToKey(.bool_true).simple_value == .true);
6354 assert(ip.indexToKey(.bool_false).simple_value == .false);
6355 }
6356}
6357
6358pub fn deinit(ip: *InternPool, gpa: Allocator, io: Io) void {
6359 std.debug.assert(debug_state.intern_pool == null);
6360
6361 ip.src_hash_deps.deinit(gpa);
6362 ip.nav_val_deps.deinit(gpa);
6363 ip.nav_ty_deps.deinit(gpa);
6364 ip.func_ies_deps.deinit(gpa);
6365 ip.type_layout_deps.deinit(gpa);
6366 ip.struct_defaults_deps.deinit(gpa);
6367 ip.source_file_deps.deinit(gpa);
6368 ip.embed_file_deps.deinit(gpa);
6369 ip.namespace_deps.deinit(gpa);
6370 ip.namespace_name_deps.deinit(gpa);
6371
6372 ip.first_dependency.deinit(gpa);
6373
6374 ip.dep_entries.deinit(gpa);
6375 ip.free_dep_entries.deinit(gpa);
6376
6377 gpa.free(ip.shards);
6378 for (ip.locals) |*local| {
6379 const buckets_len = local.mutate.namespaces.buckets_list.len;
6380 if (buckets_len > 0) for (
6381 local.shared.namespaces.view().items(.@"0")[0..buckets_len],
6382 0..,
6383 ) |namespace_bucket, buckets_index| {
6384 for (namespace_bucket[0..if (buckets_index < buckets_len - 1)
6385 namespace_bucket.len
6386 else
6387 local.mutate.namespaces.last_bucket_len]) |*namespace|
6388 {
6389 namespace.pub_decls.deinit(gpa);
6390 namespace.priv_decls.deinit(gpa);
6391 namespace.comptime_decls.deinit(gpa);
6392 namespace.test_decls.deinit(gpa);
6393 }
6394 };
6395 const maps = local.getMutableMaps(gpa, io);
6396 if (maps.mutate.len > 0) for (maps.view().items(.@"0")) |*map| map.deinit(gpa);
6397 local.mutate.arena.promote(gpa).deinit();
6398 }
6399 gpa.free(ip.locals);
6400
6401 ip.* = undefined;
6402}
6403
6404pub const Active = struct {
6405 prev_ip: if (debug_state.enable) ?*const InternPool else void,
6406 pub fn deactivate(active: Active) void {
6407 if (!debug_state.enable) return;
6408 debug_state.intern_pool = active.prev_ip;
6409 }
6410};
6411pub fn activate(ip: *const InternPool) Active {
6412 if (!debug_state.enable) return .{ .prev_ip = {} };
6413 _ = Index.Unwrapped.debug_state;
6414 _ = String.debug_state;
6415 _ = OptionalString.debug_state;
6416 _ = NullTerminatedString.debug_state;
6417 _ = OptionalNullTerminatedString.debug_state;
6418 _ = TrackedInst.Index.debug_state;
6419 _ = TrackedInst.Index.Optional.debug_state;
6420 _ = Nav.Index.debug_state;
6421 _ = Nav.Index.Optional.debug_state;
6422 defer debug_state.intern_pool = ip;
6423 return .{ .prev_ip = debug_state.intern_pool };
6424}
6425
6426/// For debugger access only.
6427const debug_state = struct {
6428 const enable = switch (builtin.zig_backend) {
6429 else => false,
6430 .stage2_x86_64 => !builtin.strip_debug_info and build_options.io_mode == .threaded,
6431 };
6432 threadlocal var intern_pool: ?*const InternPool = null;
6433};
6434
6435pub fn indexToKey(ip: *const InternPool, index: Index) Key {
6436 assert(index != .none);
6437 const unwrapped_index = index.unwrap(ip);
6438 const item = unwrapped_index.getItem(ip);
6439 const data = item.data;
6440 return switch (item.tag) {
6441 .removed => unreachable,
6442 .type_int_signed => .{
6443 .int_type = .{
6444 .signedness = .signed,
6445 .bits = @intCast(data),
6446 },
6447 },
6448 .type_int_unsigned => .{
6449 .int_type = .{
6450 .signedness = .unsigned,
6451 .bits = @intCast(data),
6452 },
6453 },
6454 .type_array_big => {
6455 const array_info = extraData(unwrapped_index.getExtra(ip), Array, data);
6456 return .{ .array_type = .{
6457 .len = array_info.getLength(),
6458 .child = array_info.child,
6459 .sentinel = array_info.sentinel,
6460 } };
6461 },
6462 .type_array_small => {
6463 const array_info = extraData(unwrapped_index.getExtra(ip), Vector, data);
6464 return .{ .array_type = .{
6465 .len = array_info.len,
6466 .child = array_info.child,
6467 .sentinel = .none,
6468 } };
6469 },
6470 .simple_type => .{ .simple_type = @fromBackingInt(@intCast(@backingInt(index))) },
6471 .simple_value => .{ .simple_value = @fromBackingInt(@intCast(@backingInt(index))) },
6472
6473 .type_vector => {
6474 const vector_info = extraData(unwrapped_index.getExtra(ip), Vector, data);
6475 return .{ .vector_type = .{
6476 .len = vector_info.len,
6477 .child = vector_info.child,
6478 } };
6479 },
6480
6481 .type_pointer => .{ .ptr_type = extraData(unwrapped_index.getExtra(ip), Tag.TypePointer, data) },
6482
6483 .type_slice => {
6484 const many_ptr_index: Index = @fromBackingInt(@intCast(data));
6485 const many_ptr_unwrapped = many_ptr_index.unwrap(ip);
6486 const many_ptr_item = many_ptr_unwrapped.getItem(ip);
6487 assert(many_ptr_item.tag == .type_pointer);
6488 var ptr_info = extraData(many_ptr_unwrapped.getExtra(ip), Tag.TypePointer, many_ptr_item.data);
6489 ptr_info.flags.size = .slice;
6490 return .{ .ptr_type = ptr_info };
6491 },
6492
6493 .type_optional => .{ .opt_type = @fromBackingInt(@intCast(data)) },
6494 .type_anyframe => .{ .anyframe_type = @fromBackingInt(@intCast(data)) },
6495
6496 .type_error_union => .{ .error_union_type = extraData(unwrapped_index.getExtra(ip), Key.ErrorUnionType, data) },
6497 .type_anyerror_union => .{ .error_union_type = .{
6498 .error_set_type = .anyerror_type,
6499 .payload_type = @fromBackingInt(@intCast(data)),
6500 } },
6501 .type_error_set => .{ .error_set_type = extraErrorSet(unwrapped_index.tid, unwrapped_index.getExtra(ip), data) },
6502 .type_inferred_error_set => .{
6503 .inferred_error_set_type = @fromBackingInt(@intCast(data)),
6504 },
6505 .type_function => .{ .func_type = extraFuncType(unwrapped_index.tid, unwrapped_index.getExtra(ip), data) },
6506 .type_tuple => .{ .tuple_type = extraTypeTuple(unwrapped_index.tid, unwrapped_index.getExtra(ip), data) },
6507
6508 .type_struct => .{ .struct_type = ns: {
6509 const extra_list = unwrapped_index.getExtra(ip);
6510 const extra = extraDataTrail(extra_list, Tag.TypeStruct, data);
6511 break :ns switch (extra.data.flags.any_captures) {
6512 .reified => .{ .reified = .{
6513 .zir_index = extra.data.zir_index,
6514 .type_hash = extraData(extra_list, PackedU64, extra.end).get(),
6515 } },
6516 .false => .{ .declared = .{
6517 .zir_index = extra.data.zir_index,
6518 .captures = .{ .owned = .empty },
6519 } },
6520 .true => .{ .declared = .{
6521 .zir_index = extra.data.zir_index,
6522 .captures = .{ .owned = .{
6523 .tid = unwrapped_index.tid,
6524 .start = extra.end + 1,
6525 .len = extra_list.view().items(.@"0")[extra.end],
6526 } },
6527 } },
6528 };
6529 } },
6530 .type_struct_packed_auto,
6531 .type_struct_packed_explicit,
6532 .type_struct_packed_auto_defaults,
6533 .type_struct_packed_explicit_defaults,
6534 => .{ .struct_type = ns: {
6535 const extra_list = unwrapped_index.getExtra(ip);
6536 const extra = extraDataTrail(extra_list, Tag.TypeStructPacked, data);
6537 break :ns switch (extra.data.bits.captures_len) {
6538 .reified => .{ .reified = .{
6539 .zir_index = extra.data.zir_index,
6540 .type_hash = extraData(extra_list, PackedU64, extra.end).get(),
6541 } },
6542 _ => |len| .{ .declared = .{
6543 .zir_index = extra.data.zir_index,
6544 .captures = .{ .owned = .{
6545 .tid = unwrapped_index.tid,
6546 .start = extra.end,
6547 .len = @backingInt(len),
6548 } },
6549 } },
6550 };
6551 } },
6552 .type_union => .{ .union_type = ns: {
6553 const extra_list = unwrapped_index.getExtra(ip);
6554 const extra = extraDataTrail(extra_list, Tag.TypeUnion, data);
6555 break :ns switch (extra.data.flags.any_captures) {
6556 .reified => .{ .reified = .{
6557 .zir_index = extra.data.zir_index,
6558 .type_hash = extraData(extra_list, PackedU64, extra.end).get(),
6559 } },
6560 .false => .{ .declared = .{
6561 .zir_index = extra.data.zir_index,
6562 .captures = .{ .owned = .empty },
6563 } },
6564 .true => .{ .declared = .{
6565 .zir_index = extra.data.zir_index,
6566 .captures = .{ .owned = .{
6567 .tid = unwrapped_index.tid,
6568 .start = extra.end + 1,
6569 .len = extra_list.view().items(.@"0")[extra.end],
6570 } },
6571 } },
6572 };
6573 } },
6574 .type_union_packed_auto, .type_union_packed_explicit => .{ .union_type = ns: {
6575 const extra_list = unwrapped_index.getExtra(ip);
6576 const extra = extraDataTrail(extra_list, Tag.TypeUnionPacked, data);
6577 break :ns switch (extra.data.bits.captures_len) {
6578 .reified => .{ .reified = .{
6579 .zir_index = extra.data.zir_index,
6580 .type_hash = extraData(extra_list, PackedU64, extra.end).get(),
6581 } },
6582 _ => |len| .{ .declared = .{
6583 .zir_index = extra.data.zir_index,
6584 .captures = .{ .owned = .{
6585 .tid = unwrapped_index.tid,
6586 .start = extra.end,
6587 .len = @backingInt(len),
6588 } },
6589 } },
6590 };
6591 } },
6592 .type_enum_auto, .type_enum_explicit, .type_enum_nonexhaustive => .{ .enum_type = ns: {
6593 const extra_list = unwrapped_index.getExtra(ip);
6594 const extra = extraDataTrail(extra_list, Tag.TypeEnum, data);
6595 break :ns switch (extra.data.bits.captures_len) {
6596 .reified => .{ .reified = .{
6597 .zir_index = @fromBackingInt(@intCast(extra_list.view().items(.@"0")[extra.end])),
6598 .type_hash = extraData(extra_list, PackedU64, extra.end + 1).get(),
6599 } },
6600 .generated_union_tag => .{ .generated_union_tag = owner_union: {
6601 break :owner_union @fromBackingInt(@intCast(extra_list.view().items(.@"0")[extra.end]));
6602 } },
6603 _ => |len| .{ .declared = .{
6604 .zir_index = @fromBackingInt(@intCast(extra_list.view().items(.@"0")[extra.end])),
6605 .captures = .{ .owned = .{
6606 .tid = unwrapped_index.tid,
6607 .start = extra.end + 1,
6608 .len = @backingInt(len),
6609 } },
6610 } },
6611 };
6612 } },
6613 .type_spirv => .{ .spirv_type = ns: {
6614 const extra = extraData(unwrapped_index.getExtra(ip), Tag.TypeSpirv, data);
6615 break :ns .{
6616 .ty = extra.ty,
6617 .flags = extra.flags,
6618 };
6619 } },
6620 .type_opaque => .{ .opaque_type = ns: {
6621 const extra = extraDataTrail(unwrapped_index.getExtra(ip), Tag.TypeOpaque, data);
6622 break :ns .{ .declared = .{
6623 .zir_index = extra.data.zir_index,
6624 .captures = .{ .owned = .{
6625 .tid = unwrapped_index.tid,
6626 .start = extra.end,
6627 .len = extra.data.captures_len,
6628 } },
6629 } };
6630 } },
6631
6632 .undef => .{ .undef = @fromBackingInt(@intCast(data)) },
6633 .opt_null => .{ .opt = .{
6634 .ty = @fromBackingInt(@intCast(data)),
6635 .val = .none,
6636 } },
6637 .opt_payload => {
6638 const extra = extraData(unwrapped_index.getExtra(ip), Tag.TypeValue, data);
6639 return .{ .opt = .{
6640 .ty = extra.ty,
6641 .val = extra.val,
6642 } };
6643 },
6644 .ptr_nav => {
6645 const info = extraData(unwrapped_index.getExtra(ip), PtrNav, data);
6646 return .{ .ptr = .{ .ty = info.ty, .base_addr = .{ .nav = info.nav }, .byte_offset = info.byteOffset() } };
6647 },
6648 .ptr_comptime_alloc => {
6649 const info = extraData(unwrapped_index.getExtra(ip), PtrComptimeAlloc, data);
6650 return .{ .ptr = .{ .ty = info.ty, .base_addr = .{ .comptime_alloc = info.index }, .byte_offset = info.byteOffset() } };
6651 },
6652 .ptr_uav => {
6653 const info = extraData(unwrapped_index.getExtra(ip), PtrUav, data);
6654 return .{ .ptr = .{ .ty = info.ty, .base_addr = .{ .uav = .{
6655 .val = info.val,
6656 .orig_ty = info.ty,
6657 } }, .byte_offset = info.byteOffset() } };
6658 },
6659 .ptr_uav_aligned => {
6660 const info = extraData(unwrapped_index.getExtra(ip), PtrUavAligned, data);
6661 return .{ .ptr = .{ .ty = info.ty, .base_addr = .{ .uav = .{
6662 .val = info.val,
6663 .orig_ty = info.orig_ty,
6664 } }, .byte_offset = info.byteOffset() } };
6665 },
6666 .ptr_comptime_field => {
6667 const info = extraData(unwrapped_index.getExtra(ip), PtrComptimeField, data);
6668 return .{ .ptr = .{ .ty = info.ty, .base_addr = .{ .comptime_field = info.field_val }, .byte_offset = info.byteOffset() } };
6669 },
6670 .ptr_int => {
6671 const info = extraData(unwrapped_index.getExtra(ip), PtrInt, data);
6672 return .{ .ptr = .{
6673 .ty = info.ty,
6674 .base_addr = .int,
6675 .byte_offset = info.byteOffset(),
6676 } };
6677 },
6678 .ptr_eu_payload => {
6679 const info = extraData(unwrapped_index.getExtra(ip), PtrBase, data);
6680 return .{ .ptr = .{ .ty = info.ty, .base_addr = .{ .eu_payload = info.base }, .byte_offset = info.byteOffset() } };
6681 },
6682 .ptr_opt_payload => {
6683 const info = extraData(unwrapped_index.getExtra(ip), PtrBase, data);
6684 return .{ .ptr = .{ .ty = info.ty, .base_addr = .{ .opt_payload = info.base }, .byte_offset = info.byteOffset() } };
6685 },
6686 .ptr_elem => {
6687 // Avoid `indexToKey` recursion by asserting the tag encoding.
6688 const info = extraData(unwrapped_index.getExtra(ip), PtrBaseIndex, data);
6689 const index_item = info.index.unwrap(ip).getItem(ip);
6690 return switch (index_item.tag) {
6691 .int_usize => .{ .ptr = .{ .ty = info.ty, .base_addr = .{ .arr_elem = .{
6692 .base = info.base,
6693 .index = index_item.data,
6694 } }, .byte_offset = info.byteOffset() } },
6695 .int_positive => @panic("TODO"), // implement along with behavior test coverage
6696 else => unreachable,
6697 };
6698 },
6699 .ptr_field => {
6700 // Avoid `indexToKey` recursion by asserting the tag encoding.
6701 const info = extraData(unwrapped_index.getExtra(ip), PtrBaseIndex, data);
6702 const index_item = info.index.unwrap(ip).getItem(ip);
6703 return switch (index_item.tag) {
6704 .int_usize => .{ .ptr = .{ .ty = info.ty, .base_addr = .{ .field = .{
6705 .base = info.base,
6706 .index = index_item.data,
6707 } }, .byte_offset = info.byteOffset() } },
6708 .int_positive => @panic("TODO"), // implement along with behavior test coverage
6709 else => unreachable,
6710 };
6711 },
6712 .ptr_slice => {
6713 const info = extraData(unwrapped_index.getExtra(ip), PtrSlice, data);
6714 return .{ .slice = .{
6715 .ty = info.ty,
6716 .ptr = info.ptr,
6717 .len = info.len,
6718 } };
6719 },
6720 .int_u8 => .{ .int = .{
6721 .ty = .u8_type,
6722 .storage = .{ .u64 = data },
6723 } },
6724 .int_u16 => .{ .int = .{
6725 .ty = .u16_type,
6726 .storage = .{ .u64 = data },
6727 } },
6728 .int_u32 => .{ .int = .{
6729 .ty = .u32_type,
6730 .storage = .{ .u64 = data },
6731 } },
6732 .int_i32 => .{ .int = .{
6733 .ty = .i32_type,
6734 .storage = .{ .i64 = @as(i32, @bitCast(data)) },
6735 } },
6736 .int_usize => .{ .int = .{
6737 .ty = .usize_type,
6738 .storage = .{ .u64 = data },
6739 } },
6740 .int_comptime_int_u32 => .{ .int = .{
6741 .ty = .comptime_int_type,
6742 .storage = .{ .u64 = data },
6743 } },
6744 .int_comptime_int_i32 => .{ .int = .{
6745 .ty = .comptime_int_type,
6746 .storage = .{ .i64 = @as(i32, @bitCast(data)) },
6747 } },
6748 .int_positive => ip.indexToKeyBigInt(unwrapped_index.tid, data, true),
6749 .int_negative => ip.indexToKeyBigInt(unwrapped_index.tid, data, false),
6750 .int_small => {
6751 const info = extraData(unwrapped_index.getExtra(ip), IntSmall, data);
6752 return .{ .int = .{
6753 .ty = info.ty,
6754 .storage = .{ .u64 = info.value },
6755 } };
6756 },
6757 .float_f16 => .{ .float = .{
6758 .ty = .f16_type,
6759 .storage = .{ .f16 = @bitCast(@as(u16, @intCast(data))) },
6760 } },
6761 .float_f32 => .{ .float = .{
6762 .ty = .f32_type,
6763 .storage = .{ .f32 = @bitCast(data) },
6764 } },
6765 .float_f64 => .{ .float = .{
6766 .ty = .f64_type,
6767 .storage = .{ .f64 = extraData(unwrapped_index.getExtra(ip), Float64, data).get() },
6768 } },
6769 .float_f80 => .{ .float = .{
6770 .ty = .f80_type,
6771 .storage = .{ .f80 = extraData(unwrapped_index.getExtra(ip), Float80, data).get() },
6772 } },
6773 .float_f128 => .{ .float = .{
6774 .ty = .f128_type,
6775 .storage = .{ .f128 = extraData(unwrapped_index.getExtra(ip), Float128, data).get() },
6776 } },
6777 .float_c_longdouble_f80 => .{ .float = .{
6778 .ty = .c_longdouble_type,
6779 .storage = .{ .f80 = extraData(unwrapped_index.getExtra(ip), Float80, data).get() },
6780 } },
6781 .float_c_longdouble_f128 => .{ .float = .{
6782 .ty = .c_longdouble_type,
6783 .storage = .{ .f128 = extraData(unwrapped_index.getExtra(ip), Float128, data).get() },
6784 } },
6785 .float_comptime_float => .{ .float = .{
6786 .ty = .comptime_float_type,
6787 .storage = .{ .f128 = extraData(unwrapped_index.getExtra(ip), Float128, data).get() },
6788 } },
6789 .@"extern" => {
6790 const extra = extraData(unwrapped_index.getExtra(ip), Tag.Extern, data);
6791 const nav = ip.getNav(extra.owner_nav);
6792 return .{ .@"extern" = .{
6793 .name = nav.name,
6794 .ty = extra.ty,
6795 .lib_name = extra.lib_name,
6796 .linkage = extra.flags.linkage,
6797 .visibility = extra.flags.visibility,
6798 .is_threadlocal = nav.resolved.?.@"threadlocal",
6799 .is_dll_import = extra.flags.is_dll_import,
6800 .relocation = extra.flags.relocation,
6801 .decoration = extra.decoration(),
6802 .is_const = nav.resolved.?.@"const",
6803 .alignment = nav.resolved.?.@"align",
6804 .@"addrspace" = nav.resolved.?.@"addrspace",
6805 .zir_index = extra.zir_index,
6806 .owner_nav = extra.owner_nav,
6807 .source = extra.flags.source,
6808 } };
6809 },
6810 .func_instance => .{ .func = ip.extraFuncInstance(unwrapped_index.tid, unwrapped_index.getExtra(ip), data) },
6811 .func_decl => .{ .func = extraFuncDecl(unwrapped_index.tid, unwrapped_index.getExtra(ip), data) },
6812 .func_coerced => .{ .func = ip.extraFuncCoerced(unwrapped_index.getExtra(ip), data) },
6813 .only_possible_value => {
6814 const ty: Index = @fromBackingInt(@intCast(data));
6815 const ty_unwrapped = ty.unwrap(ip);
6816 const ty_extra = ty_unwrapped.getExtra(ip);
6817 const ty_item = ty_unwrapped.getItem(ip);
6818 return switch (ty_item.tag) {
6819 .type_array_big => {
6820 const sentinel = @as(
6821 *const [1]Index,
6822 @ptrCast(&ty_extra.view().items(.@"0")[ty_item.data + std.meta.fieldIndex(Array, "sentinel").?]),
6823 );
6824 return .{ .aggregate = .{
6825 .ty = ty,
6826 .storage = .{ .elems = sentinel[0..@intFromBool(sentinel[0] != .none)] },
6827 } };
6828 },
6829 .type_array_small,
6830 .type_vector,
6831 .type_struct_packed_auto,
6832 .type_struct_packed_explicit,
6833 => .{ .aggregate = .{
6834 .ty = ty,
6835 .storage = .{ .elems = &.{} },
6836 } },
6837
6838 // There is only one possible value precisely due to the
6839 // fact that this values slice is fully populated!
6840 .type_struct,
6841 .type_struct_packed_auto_defaults,
6842 .type_struct_packed_explicit_defaults,
6843 => {
6844 const info = loadStructType(ip, ty);
6845 return .{ .aggregate = .{
6846 .ty = ty,
6847 .storage = .{ .elems = @ptrCast(info.field_defaults.get(ip)) },
6848 } };
6849 },
6850
6851 // There is only one possible value precisely due to the
6852 // fact that this values slice is fully populated!
6853 .type_tuple => {
6854 const type_tuple = extraDataTrail(ty_extra, TypeTuple, ty_item.data);
6855 const fields_len = type_tuple.data.fields_len;
6856 const values = ty_extra.view().items(.@"0")[type_tuple.end + fields_len ..][0..fields_len];
6857 return .{ .aggregate = .{
6858 .ty = ty,
6859 .storage = .{ .elems = @ptrCast(values) },
6860 } };
6861 },
6862
6863 else => unreachable,
6864 };
6865 },
6866 .bytes => {
6867 const extra = extraData(unwrapped_index.getExtra(ip), Bytes, data);
6868 return .{ .aggregate = .{
6869 .ty = extra.ty,
6870 .storage = .{ .bytes = extra.bytes },
6871 } };
6872 },
6873 .aggregate => {
6874 const extra_list = unwrapped_index.getExtra(ip);
6875 const extra = extraDataTrail(extra_list, Tag.Aggregate, data);
6876 const len: u32 = @intCast(ip.aggregateTypeLenIncludingSentinel(extra.data.ty));
6877 const fields: []const Index = @ptrCast(extra_list.view().items(.@"0")[extra.end..][0..len]);
6878 return .{ .aggregate = .{
6879 .ty = extra.data.ty,
6880 .storage = .{ .elems = fields },
6881 } };
6882 },
6883 .repeated => {
6884 const extra = extraData(unwrapped_index.getExtra(ip), Repeated, data);
6885 return .{ .aggregate = .{
6886 .ty = extra.ty,
6887 .storage = .{ .repeated_elem = extra.elem_val },
6888 } };
6889 },
6890 .union_value => .{ .un = extraData(unwrapped_index.getExtra(ip), Key.Union, data) },
6891 .error_set_error => .{ .err = extraData(unwrapped_index.getExtra(ip), Key.Error, data) },
6892 .error_union_error => {
6893 const extra = extraData(unwrapped_index.getExtra(ip), Key.Error, data);
6894 return .{ .error_union = .{
6895 .ty = extra.ty,
6896 .val = .{ .err_name = extra.name },
6897 } };
6898 },
6899 .error_union_payload => {
6900 const extra = extraData(unwrapped_index.getExtra(ip), Tag.TypeValue, data);
6901 return .{ .error_union = .{
6902 .ty = extra.ty,
6903 .val = .{ .payload = extra.val },
6904 } };
6905 },
6906 .enum_literal => .{ .enum_literal = @fromBackingInt(@intCast(data)) },
6907 .enum_tag => .{ .enum_tag = extraData(unwrapped_index.getExtra(ip), Tag.EnumTag, data) },
6908 .bitpack => .{ .bitpack = extraData(unwrapped_index.getExtra(ip), Key.Bitpack, data) },
6909
6910 .memoized_call => {
6911 const extra_list = unwrapped_index.getExtra(ip);
6912 const extra = extraDataTrail(extra_list, MemoizedCall, data);
6913 return .{ .memoized_call = .{
6914 .func = extra.data.func,
6915 .arg_values = @ptrCast(extra_list.view().items(.@"0")[extra.end..][0..extra.data.args_len]),
6916 .result = extra.data.result,
6917 .branch_count = extra.data.branch_count,
6918 .branch_quota = extra.data.branch_quota,
6919 } };
6920 },
6921 };
6922}
6923
6924fn extraErrorSet(tid: Zcu.PerThread.Id, extra: Local.Extra, extra_index: u32) Key.ErrorSetType {
6925 const error_set = extraDataTrail(extra, Tag.ErrorSet, extra_index);
6926 return .{
6927 .names = .{
6928 .tid = tid,
6929 .start = @intCast(error_set.end),
6930 .len = error_set.data.names_len,
6931 },
6932 .names_map = error_set.data.names_map.toOptional(),
6933 };
6934}
6935
6936fn extraTypeTuple(tid: Zcu.PerThread.Id, extra: Local.Extra, extra_index: u32) Key.TupleType {
6937 const type_tuple = extraDataTrail(extra, TypeTuple, extra_index);
6938 const fields_len = type_tuple.data.fields_len;
6939 return .{
6940 .types = .{
6941 .tid = tid,
6942 .start = type_tuple.end,
6943 .len = fields_len,
6944 },
6945 .values = .{
6946 .tid = tid,
6947 .start = type_tuple.end + fields_len,
6948 .len = fields_len,
6949 },
6950 };
6951}
6952
6953fn extraFuncType(tid: Zcu.PerThread.Id, extra: Local.Extra, extra_index: u32) Key.FuncType {
6954 const type_function = extraDataTrail(extra, Tag.TypeFunction, extra_index);
6955 var trail_index: usize = type_function.end;
6956 const comptime_bits: u32 = if (!type_function.data.flags.has_comptime_bits) 0 else b: {
6957 const x = extra.view().items(.@"0")[trail_index];
6958 trail_index += 1;
6959 break :b x;
6960 };
6961 const noalias_bits: u32 = if (!type_function.data.flags.has_noalias_bits) 0 else b: {
6962 const x = extra.view().items(.@"0")[trail_index];
6963 trail_index += 1;
6964 break :b x;
6965 };
6966 const cc_extra_len = type_function.data.flags.cc.extraLen();
6967 const cc = type_function.data.flags.cc.unpack(extra.view().items(.@"0")[trail_index..][0..cc_extra_len]);
6968 trail_index += cc_extra_len;
6969 return .{
6970 .param_types = .{
6971 .tid = tid,
6972 .start = @intCast(trail_index),
6973 .len = type_function.data.params_len,
6974 },
6975 .return_type = type_function.data.return_type,
6976 .comptime_bits = comptime_bits,
6977 .noalias_bits = noalias_bits,
6978 .cc = cc,
6979 .is_var_args = type_function.data.flags.is_var_args,
6980 .is_noinline = type_function.data.flags.is_noinline,
6981 };
6982}
6983
6984fn extraFuncDecl(tid: Zcu.PerThread.Id, extra: Local.Extra, extra_index: u32) Key.Func {
6985 const P = Tag.FuncDecl;
6986 const func_decl = extraDataTrail(extra, P, extra_index);
6987 return .{
6988 .tid = tid,
6989 .ty = func_decl.data.ty,
6990 .uncoerced_ty = func_decl.data.ty,
6991 .analysis_extra_index = extra_index + std.meta.fieldIndex(P, "analysis").?,
6992 .zir_body_inst_extra_index = extra_index + std.meta.fieldIndex(P, "zir_body_inst").?,
6993 .resolved_error_set_extra_index = if (func_decl.data.analysis.inferred_error_set) func_decl.end else 0,
6994 .branch_quota_extra_index = 0,
6995 .owner_nav = func_decl.data.owner_nav,
6996 .zir_body_inst = func_decl.data.zir_body_inst,
6997 .lbrace_line = func_decl.data.lbrace_line,
6998 .rbrace_line = func_decl.data.rbrace_line,
6999 .lbrace_column = func_decl.data.lbrace_column,
7000 .rbrace_column = func_decl.data.rbrace_column,
7001 .generic_owner = .none,
7002 .comptime_args = Index.Slice.empty,
7003 };
7004}
7005
7006fn extraFuncInstance(ip: *const InternPool, tid: Zcu.PerThread.Id, extra: Local.Extra, extra_index: u32) Key.Func {
7007 const extra_items = extra.view().items(.@"0");
7008 const analysis_extra_index = extra_index + std.meta.fieldIndex(Tag.FuncInstance, "analysis").?;
7009 const analysis: FuncAnalysis = @bitCast(@atomicLoad(u32, &extra_items[analysis_extra_index], .unordered));
7010 const owner_nav: Nav.Index = @fromBackingInt(@intCast(extra_items[extra_index + std.meta.fieldIndex(Tag.FuncInstance, "owner_nav").?]));
7011 const ty: Index = @fromBackingInt(@intCast(extra_items[extra_index + std.meta.fieldIndex(Tag.FuncInstance, "ty").?]));
7012 const generic_owner: Index = @fromBackingInt(@intCast(extra_items[extra_index + std.meta.fieldIndex(Tag.FuncInstance, "generic_owner").?]));
7013 const func_decl = ip.funcDeclInfo(generic_owner);
7014 const end_extra_index = extra_index + @as(u32, @typeInfo(Tag.FuncInstance).@"struct".field_names.len);
7015 return .{
7016 .tid = tid,
7017 .ty = ty,
7018 .uncoerced_ty = ty,
7019 .analysis_extra_index = analysis_extra_index,
7020 .zir_body_inst_extra_index = func_decl.zir_body_inst_extra_index,
7021 .resolved_error_set_extra_index = if (analysis.inferred_error_set) end_extra_index else 0,
7022 .branch_quota_extra_index = extra_index + std.meta.fieldIndex(Tag.FuncInstance, "branch_quota").?,
7023 .owner_nav = owner_nav,
7024 .zir_body_inst = func_decl.zir_body_inst,
7025 .lbrace_line = func_decl.lbrace_line,
7026 .rbrace_line = func_decl.rbrace_line,
7027 .lbrace_column = func_decl.lbrace_column,
7028 .rbrace_column = func_decl.rbrace_column,
7029 .generic_owner = generic_owner,
7030 .comptime_args = .{
7031 .tid = tid,
7032 .start = end_extra_index + @intFromBool(analysis.inferred_error_set),
7033 .len = ip.funcTypeParamsLen(func_decl.ty),
7034 },
7035 };
7036}
7037
7038fn extraFuncCoerced(ip: *const InternPool, extra: Local.Extra, extra_index: u32) Key.Func {
7039 const func_coerced = extraData(extra, Tag.FuncCoerced, extra_index);
7040 const func_unwrapped = func_coerced.func.unwrap(ip);
7041 const sub_item = func_unwrapped.getItem(ip);
7042 const func_extra = func_unwrapped.getExtra(ip);
7043 var func: Key.Func = switch (sub_item.tag) {
7044 .func_instance => ip.extraFuncInstance(func_unwrapped.tid, func_extra, sub_item.data),
7045 .func_decl => extraFuncDecl(func_unwrapped.tid, func_extra, sub_item.data),
7046 else => unreachable,
7047 };
7048 func.ty = func_coerced.ty;
7049 return func;
7050}
7051
7052fn indexToKeyBigInt(ip: *const InternPool, tid: Zcu.PerThread.Id, limb_index: u32, positive: bool) Key {
7053 const limbs_items = ip.getLocalShared(tid).getLimbs().view().items(.@"0");
7054 const int: Int = @bitCast(limbs_items[limb_index..][0..Int.limbs_items_len].*);
7055 const big_int: BigIntConst = .{
7056 .limbs = limbs_items[limb_index + Int.limbs_items_len ..][0..int.limbs_len],
7057 .positive = positive,
7058 };
7059 return .{ .int = .{
7060 .ty = int.ty,
7061 .storage = if (big_int.toInt(u64)) |x|
7062 .{ .u64 = x }
7063 else |_| if (big_int.toInt(i64)) |x|
7064 .{ .i64 = x }
7065 else |_|
7066 .{ .big_int = big_int },
7067 } };
7068}
7069
7070const GetOrPutKey = union(enum) {
7071 existing: Index,
7072 new: struct {
7073 ip: *InternPool,
7074 tid: Zcu.PerThread.Id,
7075 io: Io,
7076 shard: *Shard,
7077 map_index: u32,
7078 },
7079
7080 fn put(gop: *GetOrPutKey) Index {
7081 switch (gop.*) {
7082 .existing => unreachable,
7083 .new => |*info| {
7084 const index = Index.Unwrapped.wrap(.{
7085 .tid = info.tid,
7086 .index = info.ip.getLocal(info.tid).mutate.items.len - 1,
7087 }, info.ip);
7088 gop.putTentative(index);
7089 gop.putFinal(index);
7090 return index;
7091 },
7092 }
7093 }
7094
7095 fn putTentative(gop: *GetOrPutKey, index: Index) void {
7096 assert(index != .none);
7097 switch (gop.*) {
7098 .existing => unreachable,
7099 .new => |*info| gop.new.shard.shared.map.entries[info.map_index].release(index),
7100 }
7101 }
7102
7103 fn putFinal(gop: *GetOrPutKey, index: Index) void {
7104 assert(index != .none);
7105 switch (gop.*) {
7106 .existing => unreachable,
7107 .new => |info| {
7108 assert(info.shard.shared.map.entries[info.map_index].value == index);
7109 info.shard.mutate.map.len += 1;
7110 info.shard.mutate.map.mutex.unlock(info.io);
7111 gop.* = .{ .existing = index };
7112 },
7113 }
7114 }
7115
7116 fn cancel(gop: *GetOrPutKey) void {
7117 switch (gop.*) {
7118 .existing => {},
7119 .new => |info| info.shard.mutate.map.mutex.unlock(info.io),
7120 }
7121 gop.* = .{ .existing = undefined };
7122 }
7123
7124 fn deinit(gop: *GetOrPutKey) void {
7125 switch (gop.*) {
7126 .existing => {},
7127 .new => |info| info.shard.shared.map.entries[info.map_index].resetUnordered(),
7128 }
7129 gop.cancel();
7130 gop.* = undefined;
7131 }
7132};
7133fn getOrPutKey(
7134 ip: *InternPool,
7135 gpa: Allocator,
7136 io: Io,
7137 tid: Zcu.PerThread.Id,
7138 key: Key,
7139) Allocator.Error!GetOrPutKey {
7140 return ip.getOrPutKeyEnsuringAdditionalCapacity(gpa, io, tid, key, 0);
7141}
7142fn getOrPutKeyEnsuringAdditionalCapacity(
7143 ip: *InternPool,
7144 gpa: Allocator,
7145 io: Io,
7146 tid: Zcu.PerThread.Id,
7147 key: Key,
7148 additional_capacity: u32,
7149) Allocator.Error!GetOrPutKey {
7150 const full_hash = key.hash64(ip);
7151 const hash: u32 = @truncate(full_hash >> 32);
7152 const shard = &ip.shards[@intCast(full_hash & (ip.shards.len - 1))];
7153 var map = shard.shared.map.acquire();
7154 const Map = @TypeOf(map);
7155 var map_mask = map.header().mask();
7156 var map_index = hash;
7157 while (true) : (map_index += 1) {
7158 map_index &= map_mask;
7159 const entry = &map.entries[map_index];
7160 const index = entry.acquire();
7161 if (index == .none) break;
7162 if (entry.hash != hash) continue;
7163 if (index.unwrap(ip).getTag(ip) == .removed) continue;
7164 if (ip.indexToKey(index).eql(key, ip)) return .{ .existing = index };
7165 }
7166 shard.mutate.map.mutex.lock(io, tid);
7167 errdefer shard.mutate.map.mutex.unlock(io);
7168 if (map.entries != shard.shared.map.entries) {
7169 map = shard.shared.map;
7170 map_mask = map.header().mask();
7171 map_index = hash;
7172 }
7173 while (true) : (map_index += 1) {
7174 map_index &= map_mask;
7175 const entry = &map.entries[map_index];
7176 const index = entry.value;
7177 if (index == .none) break;
7178 if (entry.hash != hash) continue;
7179 if (ip.indexToKey(index).eql(key, ip)) {
7180 defer shard.mutate.map.mutex.unlock(io);
7181 return .{ .existing = index };
7182 }
7183 }
7184 const map_header = map.header().*;
7185 const required = shard.mutate.map.len + additional_capacity;
7186 if (required >= map_header.capacity * 3 / 5) {
7187 const arena_state = &ip.getLocal(tid).mutate.arena;
7188 var arena = arena_state.promote(gpa);
7189 defer arena_state.* = arena.state;
7190 var new_map_capacity = map_header.capacity;
7191 while (true) {
7192 new_map_capacity *= 2;
7193 if (required < new_map_capacity * 3 / 5) break;
7194 }
7195 const new_map_buf = try arena.allocator().alignedAlloc(
7196 u8,
7197 .fromByteUnits(Map.alignment),
7198 Map.entries_offset + new_map_capacity * @sizeOf(Map.Entry),
7199 );
7200 const new_map: Map = .{ .entries = @ptrCast(new_map_buf[Map.entries_offset..].ptr) };
7201 new_map.header().* = .{ .capacity = new_map_capacity };
7202 @memset(new_map.entries[0..new_map_capacity], .{ .value = .none, .hash = undefined });
7203 const new_map_mask = new_map.header().mask();
7204 map_index = 0;
7205 while (map_index < map_header.capacity) : (map_index += 1) {
7206 const entry = &map.entries[map_index];
7207 const index = entry.value;
7208 if (index == .none) continue;
7209 const item_hash = entry.hash;
7210 var new_map_index = item_hash;
7211 while (true) : (new_map_index += 1) {
7212 new_map_index &= new_map_mask;
7213 const new_entry = &new_map.entries[new_map_index];
7214 if (new_entry.value != .none) continue;
7215 new_entry.* = .{
7216 .value = index,
7217 .hash = item_hash,
7218 };
7219 break;
7220 }
7221 }
7222 map = new_map;
7223 map_index = hash;
7224 while (true) : (map_index += 1) {
7225 map_index &= new_map_mask;
7226 if (map.entries[map_index].value == .none) break;
7227 }
7228 shard.shared.map.release(new_map);
7229 }
7230 map.entries[map_index].hash = hash;
7231 return .{ .new = .{
7232 .ip = ip,
7233 .tid = tid,
7234 .io = io,
7235 .shard = shard,
7236 .map_index = map_index,
7237 } };
7238}
7239
7240pub fn get(ip: *InternPool, gpa: Allocator, io: Io, tid: Zcu.PerThread.Id, key: Key) Allocator.Error!Index {
7241 var gop = try ip.getOrPutKey(gpa, io, tid, key);
7242 defer gop.deinit();
7243 if (gop == .existing) return gop.existing;
7244 const local = ip.getLocal(tid);
7245 const items = local.getMutableItems(gpa, io);
7246 const extra = local.getMutableExtra(gpa, io);
7247 try items.ensureUnusedCapacity(1);
7248 switch (key) {
7249 .int_type => |int_type| {
7250 if (int_type.signedness == .signed) assert(int_type.bits > 0);
7251 const t: Tag = switch (int_type.signedness) {
7252 .signed => .type_int_signed,
7253 .unsigned => .type_int_unsigned,
7254 };
7255 items.appendAssumeCapacity(.{
7256 .tag = t,
7257 .data = int_type.bits,
7258 });
7259 },
7260 .ptr_type => |ptr_type| {
7261 assert(ptr_type.child != .none);
7262 assert(ptr_type.sentinel == .none or ip.typeOf(ptr_type.sentinel) == ptr_type.child);
7263
7264 if (ptr_type.flags.size == .slice) {
7265 gop.cancel();
7266 var new_key = key;
7267 new_key.ptr_type.flags.size = .many;
7268 const ptr_type_index = try ip.get(gpa, io, tid, new_key);
7269 gop = try ip.getOrPutKey(gpa, io, tid, key);
7270
7271 try items.ensureUnusedCapacity(1);
7272 items.appendAssumeCapacity(.{
7273 .tag = .type_slice,
7274 .data = @backingInt(ptr_type_index),
7275 });
7276 return gop.put();
7277 }
7278
7279 var ptr_type_adjusted = ptr_type;
7280 if (ptr_type.flags.size == .c) ptr_type_adjusted.flags.is_allowzero = true;
7281
7282 items.appendAssumeCapacity(.{
7283 .tag = .type_pointer,
7284 .data = try addExtra(extra, ptr_type_adjusted),
7285 });
7286 },
7287 .array_type => |array_type| {
7288 assert(array_type.child != .none);
7289 assert(array_type.sentinel == .none or ip.typeOf(array_type.sentinel) == array_type.child);
7290
7291 if (std.math.cast(u32, array_type.len)) |len| {
7292 if (array_type.sentinel == .none) {
7293 items.appendAssumeCapacity(.{
7294 .tag = .type_array_small,
7295 .data = try addExtra(extra, Vector{
7296 .len = len,
7297 .child = array_type.child,
7298 }),
7299 });
7300 return gop.put();
7301 }
7302 }
7303
7304 const length = Array.Length.init(array_type.len);
7305 items.appendAssumeCapacity(.{
7306 .tag = .type_array_big,
7307 .data = try addExtra(extra, Array{
7308 .len0 = length.a,
7309 .len1 = length.b,
7310 .child = array_type.child,
7311 .sentinel = array_type.sentinel,
7312 }),
7313 });
7314 },
7315 .vector_type => |vector_type| {
7316 items.appendAssumeCapacity(.{
7317 .tag = .type_vector,
7318 .data = try addExtra(extra, Vector{
7319 .len = vector_type.len,
7320 .child = vector_type.child,
7321 }),
7322 });
7323 },
7324 .opt_type => |payload_type| {
7325 assert(payload_type != .none);
7326 items.appendAssumeCapacity(.{
7327 .tag = .type_optional,
7328 .data = @backingInt(payload_type),
7329 });
7330 },
7331 .anyframe_type => |payload_type| {
7332 // payload_type might be none, indicating the type is `anyframe`.
7333 items.appendAssumeCapacity(.{
7334 .tag = .type_anyframe,
7335 .data = @backingInt(payload_type),
7336 });
7337 },
7338 .error_union_type => |error_union_type| {
7339 items.appendAssumeCapacity(if (error_union_type.error_set_type == .anyerror_type) .{
7340 .tag = .type_anyerror_union,
7341 .data = @backingInt(error_union_type.payload_type),
7342 } else .{
7343 .tag = .type_error_union,
7344 .data = try addExtra(extra, error_union_type),
7345 });
7346 },
7347 .error_set_type => |error_set_type| {
7348 assert(error_set_type.names_map == .none);
7349 assert(std.sort.isSorted(NullTerminatedString, error_set_type.names.get(ip), {}, NullTerminatedString.indexLessThan));
7350 const names = error_set_type.names.get(ip);
7351 const names_map = try ip.addMap(gpa, io, tid, names.len);
7352 ip.addStringsToMap(names_map, names);
7353 const names_len = error_set_type.names.len;
7354 try extra.ensureUnusedCapacity(@typeInfo(Tag.ErrorSet).@"struct".field_names.len + names_len);
7355 items.appendAssumeCapacity(.{
7356 .tag = .type_error_set,
7357 .data = addExtraAssumeCapacity(extra, Tag.ErrorSet{
7358 .names_len = names_len,
7359 .names_map = names_map,
7360 }),
7361 });
7362 extra.appendSliceAssumeCapacity(.{@ptrCast(error_set_type.names.get(ip))});
7363 },
7364 .inferred_error_set_type => |ies_index| {
7365 items.appendAssumeCapacity(.{
7366 .tag = .type_inferred_error_set,
7367 .data = @backingInt(ies_index),
7368 });
7369 },
7370 .simple_type => |simple_type| {
7371 assert(@backingInt(simple_type) == items.mutate.len);
7372 items.appendAssumeCapacity(.{
7373 .tag = .simple_type,
7374 .data = 0, // avoid writing `undefined` bits to a file
7375 });
7376 },
7377 .simple_value => |simple_value| {
7378 assert(@backingInt(simple_value) == items.mutate.len);
7379 items.appendAssumeCapacity(.{
7380 .tag = .simple_value,
7381 .data = 0, // avoid writing `undefined` bits to a file
7382 });
7383 },
7384 .undef => |ty| {
7385 assert(ty != .none);
7386 items.appendAssumeCapacity(.{
7387 .tag = .undef,
7388 .data = @backingInt(ty),
7389 });
7390 },
7391
7392 .struct_type => unreachable, // instead use: getDeclaredStructType, getReifiedStructType
7393 .union_type => unreachable, // instead use: getDeclaredUnionType, getReifiedUnionType
7394 .enum_type => unreachable, // instead use: getDeclaredEnumType, getReifiedEnumType, getGeneratedEnumTagType
7395 .opaque_type => unreachable, // instead use: getDeclaredOpaqueType
7396 .spirv_type => unreachable, // instead use: getSpirvType
7397
7398 .tuple_type => unreachable, // use getTupleType() instead
7399 .func_type => unreachable, // use getFuncType() instead
7400 .@"extern" => unreachable, // use getExtern() instead
7401 .func => unreachable, // use getFuncInstance() or getFuncDecl() instead
7402 .un => unreachable, // use getUnion instead
7403
7404 .slice => |slice| {
7405 assert(ip.indexToKey(slice.ty).ptr_type.flags.size == .slice);
7406 assert(ip.indexToKey(ip.typeOf(slice.ptr)).ptr_type.flags.size == .many);
7407 items.appendAssumeCapacity(.{
7408 .tag = .ptr_slice,
7409 .data = try addExtra(extra, PtrSlice{
7410 .ty = slice.ty,
7411 .ptr = slice.ptr,
7412 .len = slice.len,
7413 }),
7414 });
7415 },
7416
7417 .ptr => |ptr| {
7418 const ptr_type = ip.indexToKey(ptr.ty).ptr_type;
7419 assert(ptr_type.flags.size != .slice);
7420 items.appendAssumeCapacity(switch (ptr.base_addr) {
7421 .nav => |nav| .{
7422 .tag = .ptr_nav,
7423 .data = try addExtra(extra, PtrNav.init(ptr.ty, nav, ptr.byte_offset)),
7424 },
7425 .comptime_alloc => |alloc_index| .{
7426 .tag = .ptr_comptime_alloc,
7427 .data = try addExtra(extra, PtrComptimeAlloc.init(ptr.ty, alloc_index, ptr.byte_offset)),
7428 },
7429 .uav => |uav| if (ptrsHaveSameAlignment(ip, ptr.ty, ptr_type, uav.orig_ty)) item: {
7430 if (ptr.ty != uav.orig_ty) {
7431 gop.cancel();
7432 var new_key = key;
7433 new_key.ptr.base_addr.uav.orig_ty = ptr.ty;
7434 gop = try ip.getOrPutKey(gpa, io, tid, new_key);
7435 if (gop == .existing) return gop.existing;
7436 }
7437 break :item .{
7438 .tag = .ptr_uav,
7439 .data = try addExtra(extra, PtrUav.init(ptr.ty, uav.val, ptr.byte_offset)),
7440 };
7441 } else .{
7442 .tag = .ptr_uav_aligned,
7443 .data = try addExtra(extra, PtrUavAligned.init(ptr.ty, uav.val, uav.orig_ty, ptr.byte_offset)),
7444 },
7445 .comptime_field => |field_val| item: {
7446 assert(field_val != .none);
7447 break :item .{
7448 .tag = .ptr_comptime_field,
7449 .data = try addExtra(extra, PtrComptimeField.init(ptr.ty, field_val, ptr.byte_offset)),
7450 };
7451 },
7452 .eu_payload, .opt_payload => |base| item: {
7453 switch (ptr.base_addr) {
7454 .eu_payload => assert(ip.indexToKey(
7455 ip.indexToKey(ip.typeOf(base)).ptr_type.child,
7456 ) == .error_union_type),
7457 .opt_payload => assert(ip.indexToKey(
7458 ip.indexToKey(ip.typeOf(base)).ptr_type.child,
7459 ) == .opt_type),
7460 else => unreachable,
7461 }
7462 break :item .{
7463 .tag = switch (ptr.base_addr) {
7464 .eu_payload => .ptr_eu_payload,
7465 .opt_payload => .ptr_opt_payload,
7466 else => unreachable,
7467 },
7468 .data = try addExtra(extra, PtrBase.init(ptr.ty, base, ptr.byte_offset)),
7469 };
7470 },
7471 .int => .{
7472 .tag = .ptr_int,
7473 .data = try addExtra(extra, PtrInt.init(ptr.ty, ptr.byte_offset)),
7474 },
7475 .arr_elem, .field => |base_index| {
7476 const base_ptr_type = ip.indexToKey(ip.typeOf(base_index.base)).ptr_type;
7477 switch (ptr.base_addr) {
7478 .arr_elem => assert(base_ptr_type.flags.size == .many),
7479 .field => {
7480 assert(base_ptr_type.flags.size == .one);
7481 switch (ip.indexToKey(base_ptr_type.child)) {
7482 .tuple_type => |tuple_type| {
7483 assert(ptr.base_addr == .field);
7484 assert(base_index.index < tuple_type.types.len);
7485 },
7486 .struct_type => {
7487 assert(ptr.base_addr == .field);
7488 assert(base_index.index < ip.loadStructType(base_ptr_type.child).field_types.len);
7489 },
7490 .union_type => {
7491 const union_type = ip.loadUnionType(base_ptr_type.child);
7492 assert(ptr.base_addr == .field);
7493 assert(base_index.index < union_type.field_types.len);
7494 },
7495 .ptr_type => |slice_type| {
7496 assert(ptr.base_addr == .field);
7497 assert(slice_type.flags.size == .slice);
7498 assert(base_index.index < 2);
7499 },
7500 else => unreachable,
7501 }
7502 },
7503 else => unreachable,
7504 }
7505 gop.cancel();
7506 const index_index = try ip.get(gpa, io, tid, .{ .int = .{
7507 .ty = .usize_type,
7508 .storage = .{ .u64 = base_index.index },
7509 } });
7510 gop = try ip.getOrPutKey(gpa, io, tid, key);
7511 try items.ensureUnusedCapacity(1);
7512 items.appendAssumeCapacity(.{
7513 .tag = switch (ptr.base_addr) {
7514 .arr_elem => .ptr_elem,
7515 .field => .ptr_field,
7516 else => unreachable,
7517 },
7518 .data = try addExtra(extra, PtrBaseIndex.init(ptr.ty, base_index.base, index_index, ptr.byte_offset)),
7519 });
7520 return gop.put();
7521 },
7522 });
7523 },
7524
7525 .opt => |opt| {
7526 assert(ip.isOptionalType(opt.ty));
7527 assert(opt.val == .none or ip.indexToKey(opt.ty).opt_type == ip.typeOf(opt.val));
7528 items.appendAssumeCapacity(if (opt.val == .none) .{
7529 .tag = .opt_null,
7530 .data = @backingInt(opt.ty),
7531 } else .{
7532 .tag = .opt_payload,
7533 .data = try addExtra(extra, Tag.TypeValue{
7534 .ty = opt.ty,
7535 .val = opt.val,
7536 }),
7537 });
7538 },
7539
7540 .int => |int| b: {
7541 assert(ip.isIntegerType(int.ty));
7542 switch (int.ty) {
7543 .u8_type => switch (int.storage) {
7544 .big_int => |big_int| {
7545 items.appendAssumeCapacity(.{
7546 .tag = .int_u8,
7547 .data = big_int.toInt(u8) catch unreachable,
7548 });
7549 break :b;
7550 },
7551 inline .u64, .i64 => |x| {
7552 items.appendAssumeCapacity(.{
7553 .tag = .int_u8,
7554 .data = @as(u8, @intCast(x)),
7555 });
7556 break :b;
7557 },
7558 },
7559 .u16_type => switch (int.storage) {
7560 .big_int => |big_int| {
7561 items.appendAssumeCapacity(.{
7562 .tag = .int_u16,
7563 .data = big_int.toInt(u16) catch unreachable,
7564 });
7565 break :b;
7566 },
7567 inline .u64, .i64 => |x| {
7568 items.appendAssumeCapacity(.{
7569 .tag = .int_u16,
7570 .data = @as(u16, @intCast(x)),
7571 });
7572 break :b;
7573 },
7574 },
7575 .u32_type => switch (int.storage) {
7576 .big_int => |big_int| {
7577 items.appendAssumeCapacity(.{
7578 .tag = .int_u32,
7579 .data = big_int.toInt(u32) catch unreachable,
7580 });
7581 break :b;
7582 },
7583 inline .u64, .i64 => |x| {
7584 items.appendAssumeCapacity(.{
7585 .tag = .int_u32,
7586 .data = @as(u32, @intCast(x)),
7587 });
7588 break :b;
7589 },
7590 },
7591 .i32_type => switch (int.storage) {
7592 .big_int => |big_int| {
7593 const casted = big_int.toInt(i32) catch unreachable;
7594 items.appendAssumeCapacity(.{
7595 .tag = .int_i32,
7596 .data = @as(u32, @bitCast(casted)),
7597 });
7598 break :b;
7599 },
7600 inline .u64, .i64 => |x| {
7601 items.appendAssumeCapacity(.{
7602 .tag = .int_i32,
7603 .data = @as(u32, @bitCast(@as(i32, @intCast(x)))),
7604 });
7605 break :b;
7606 },
7607 },
7608 .usize_type => switch (int.storage) {
7609 .big_int => |big_int| {
7610 if (big_int.toInt(u32)) |casted| {
7611 items.appendAssumeCapacity(.{
7612 .tag = .int_usize,
7613 .data = casted,
7614 });
7615 break :b;
7616 } else |_| {}
7617 },
7618 inline .u64, .i64 => |x| {
7619 if (std.math.cast(u32, x)) |casted| {
7620 items.appendAssumeCapacity(.{
7621 .tag = .int_usize,
7622 .data = casted,
7623 });
7624 break :b;
7625 }
7626 },
7627 },
7628 .comptime_int_type => switch (int.storage) {
7629 .big_int => |big_int| {
7630 if (big_int.toInt(u32)) |casted| {
7631 items.appendAssumeCapacity(.{
7632 .tag = .int_comptime_int_u32,
7633 .data = casted,
7634 });
7635 break :b;
7636 } else |_| {}
7637 if (big_int.toInt(i32)) |casted| {
7638 items.appendAssumeCapacity(.{
7639 .tag = .int_comptime_int_i32,
7640 .data = @as(u32, @bitCast(casted)),
7641 });
7642 break :b;
7643 } else |_| {}
7644 },
7645 inline .u64, .i64 => |x| {
7646 if (std.math.cast(u32, x)) |casted| {
7647 items.appendAssumeCapacity(.{
7648 .tag = .int_comptime_int_u32,
7649 .data = casted,
7650 });
7651 break :b;
7652 }
7653 if (std.math.cast(i32, x)) |casted| {
7654 items.appendAssumeCapacity(.{
7655 .tag = .int_comptime_int_i32,
7656 .data = @as(u32, @bitCast(casted)),
7657 });
7658 break :b;
7659 }
7660 },
7661 },
7662 else => {},
7663 }
7664 switch (int.storage) {
7665 .big_int => |big_int| {
7666 if (big_int.toInt(u32)) |casted| {
7667 items.appendAssumeCapacity(.{
7668 .tag = .int_small,
7669 .data = try addExtra(extra, IntSmall{
7670 .ty = int.ty,
7671 .value = casted,
7672 }),
7673 });
7674 return gop.put();
7675 } else |_| {}
7676
7677 const tag: Tag = if (big_int.positive or big_int.eqlZero()) .int_positive else .int_negative;
7678 try addInt(ip, gpa, io, tid, int.ty, tag, big_int.limbs);
7679 },
7680 inline .u64, .i64 => |x| {
7681 if (std.math.cast(u32, x)) |casted| {
7682 items.appendAssumeCapacity(.{
7683 .tag = .int_small,
7684 .data = try addExtra(extra, IntSmall{
7685 .ty = int.ty,
7686 .value = casted,
7687 }),
7688 });
7689 return gop.put();
7690 }
7691
7692 var buf: [2]Limb = undefined;
7693 const big_int = BigIntMutable.init(&buf, x).toConst();
7694 const tag: Tag = if (big_int.positive or big_int.eqlZero()) .int_positive else .int_negative;
7695 try addInt(ip, gpa, io, tid, int.ty, tag, big_int.limbs);
7696 },
7697 }
7698 },
7699
7700 .err => |err| {
7701 assert(ip.isErrorSetType(err.ty));
7702 items.appendAssumeCapacity(.{
7703 .tag = .error_set_error,
7704 .data = try addExtra(extra, err),
7705 });
7706 },
7707
7708 .error_union => |error_union| {
7709 assert(ip.isErrorUnionType(error_union.ty));
7710 items.appendAssumeCapacity(switch (error_union.val) {
7711 .err_name => |err_name| .{
7712 .tag = .error_union_error,
7713 .data = try addExtra(extra, Key.Error{
7714 .ty = error_union.ty,
7715 .name = err_name,
7716 }),
7717 },
7718 .payload => |payload| .{
7719 .tag = .error_union_payload,
7720 .data = try addExtra(extra, Tag.TypeValue{
7721 .ty = error_union.ty,
7722 .val = payload,
7723 }),
7724 },
7725 });
7726 },
7727
7728 .enum_literal => |enum_literal| items.appendAssumeCapacity(.{
7729 .tag = .enum_literal,
7730 .data = @backingInt(enum_literal),
7731 }),
7732
7733 .enum_tag => |enum_tag| {
7734 const enum_obj = ip.loadEnumType(enum_tag.ty);
7735 assert(ip.typeOf(enum_tag.int) == enum_obj.int_tag_type);
7736 items.appendAssumeCapacity(.{
7737 .tag = .enum_tag,
7738 .data = try addExtra(extra, enum_tag),
7739 });
7740 },
7741
7742 .float => |float| {
7743 switch (float.ty) {
7744 .f16_type => items.appendAssumeCapacity(.{
7745 .tag = .float_f16,
7746 .data = @as(u16, @bitCast(float.storage.f16)),
7747 }),
7748 .f32_type => items.appendAssumeCapacity(.{
7749 .tag = .float_f32,
7750 .data = @as(u32, @bitCast(float.storage.f32)),
7751 }),
7752 .f64_type => items.appendAssumeCapacity(.{
7753 .tag = .float_f64,
7754 .data = try addExtra(extra, Float64.pack(float.storage.f64)),
7755 }),
7756 .f80_type => items.appendAssumeCapacity(.{
7757 .tag = .float_f80,
7758 .data = try addExtra(extra, Float80.pack(float.storage.f80)),
7759 }),
7760 .f128_type => items.appendAssumeCapacity(.{
7761 .tag = .float_f128,
7762 .data = try addExtra(extra, Float128.pack(float.storage.f128)),
7763 }),
7764 .c_longdouble_type => switch (float.storage) {
7765 .f80 => |x| items.appendAssumeCapacity(.{
7766 .tag = .float_c_longdouble_f80,
7767 .data = try addExtra(extra, Float80.pack(x)),
7768 }),
7769 inline .f16, .f32, .f64, .f128 => |x| items.appendAssumeCapacity(.{
7770 .tag = .float_c_longdouble_f128,
7771 .data = try addExtra(extra, Float128.pack(x)),
7772 }),
7773 },
7774 .comptime_float_type => items.appendAssumeCapacity(.{
7775 .tag = .float_comptime_float,
7776 .data = try addExtra(extra, Float128.pack(float.storage.f128)),
7777 }),
7778 else => unreachable,
7779 }
7780 },
7781
7782 .aggregate => |aggregate| {
7783 const ty_key = ip.indexToKey(aggregate.ty);
7784 const len = ip.aggregateTypeLen(aggregate.ty);
7785 const child: Index, const sentinel: Index = switch (ty_key) {
7786 .array_type => |array_type| .{ array_type.child, array_type.sentinel },
7787 .vector_type => |vector_type| .{ vector_type.child, .none },
7788 .tuple_type => .{ .none, .none },
7789 .struct_type => child: {
7790 assert(ip.loadStructType(aggregate.ty).layout != .@"packed");
7791 break :child .{ .none, .none };
7792 },
7793 else => unreachable,
7794 };
7795 const len_including_sentinel = len + @intFromBool(sentinel != .none);
7796 switch (aggregate.storage) {
7797 .bytes => |bytes| {
7798 assert(child == .u8_type);
7799 if (sentinel != .none) {
7800 assert(bytes.at(@intCast(len), ip) == ip.indexToKey(sentinel).int.storage.u64);
7801 }
7802 },
7803 .elems => |elems| {
7804 if (elems.len != len) {
7805 assert(elems.len == len_including_sentinel);
7806 assert(elems[@intCast(len)] == sentinel);
7807 }
7808 },
7809 .repeated_elem => |elem| {
7810 assert(sentinel == .none or elem == sentinel);
7811 },
7812 }
7813 if (aggregate.storage.values().len > 0) switch (ty_key) {
7814 .array_type, .vector_type => {
7815 var any_defined = false;
7816 for (aggregate.storage.values()) |elem| {
7817 if (!ip.isUndef(elem)) any_defined = true;
7818 assert(ip.typeOf(elem) == child);
7819 }
7820 assert(any_defined); // aggregate fields must not be all undefined
7821 },
7822 .struct_type => {
7823 var any_defined = false;
7824 for (aggregate.storage.values(), ip.loadStructType(aggregate.ty).field_types.get(ip)) |elem, field_ty| {
7825 if (!ip.isUndef(elem)) any_defined = true;
7826 assert(ip.typeOf(elem) == field_ty);
7827 }
7828 assert(any_defined); // aggregate fields must not be all undefined
7829 },
7830 .tuple_type => |tuple_type| {
7831 var any_defined = false;
7832 for (aggregate.storage.values(), tuple_type.types.get(ip)) |elem, ty| {
7833 if (!ip.isUndef(elem)) any_defined = true;
7834 assert(ip.typeOf(elem) == ty);
7835 }
7836 assert(any_defined); // aggregate fields must not be all undefined
7837 },
7838 else => unreachable,
7839 };
7840
7841 if (len == 0) {
7842 items.appendAssumeCapacity(.{
7843 .tag = .only_possible_value,
7844 .data = @backingInt(aggregate.ty),
7845 });
7846 return gop.put();
7847 }
7848
7849 switch (ty_key) {
7850 .tuple_type => |tuple_type| opv: {
7851 switch (aggregate.storage) {
7852 .bytes => |bytes| for (tuple_type.values.get(ip), bytes.at(0, ip)..) |value, byte| {
7853 if (value == .none) break :opv;
7854 switch (ip.indexToKey(value)) {
7855 .undef => break :opv,
7856 .int => |int| switch (int.storage) {
7857 .u64 => |x| if (x != byte) break :opv,
7858 else => break :opv,
7859 },
7860 else => unreachable,
7861 }
7862 },
7863 .elems => |elems| if (!std.mem.eql(
7864 Index,
7865 tuple_type.values.get(ip),
7866 elems,
7867 )) break :opv,
7868 .repeated_elem => |elem| for (tuple_type.values.get(ip)) |value| {
7869 if (value != elem) break :opv;
7870 },
7871 }
7872 // This encoding works thanks to the fact that, as we just verified,
7873 // the type itself contains a slice of values that can be provided
7874 // in the aggregate fields.
7875 items.appendAssumeCapacity(.{
7876 .tag = .only_possible_value,
7877 .data = @backingInt(aggregate.ty),
7878 });
7879 return gop.put();
7880 },
7881 else => {},
7882 }
7883
7884 repeated: {
7885 switch (aggregate.storage) {
7886 .bytes => |bytes| for (bytes.toSlice(len, ip)[1..]) |byte|
7887 if (byte != bytes.at(0, ip)) break :repeated,
7888 .elems => |elems| for (elems[1..@intCast(len)]) |elem|
7889 if (elem != elems[0]) break :repeated,
7890 .repeated_elem => {},
7891 }
7892 const elem = switch (aggregate.storage) {
7893 .bytes => |bytes| elem: {
7894 gop.cancel();
7895 const elem = try ip.get(gpa, io, tid, .{ .int = .{
7896 .ty = .u8_type,
7897 .storage = .{ .u64 = bytes.at(0, ip) },
7898 } });
7899 gop = try ip.getOrPutKey(gpa, io, tid, key);
7900 try items.ensureUnusedCapacity(1);
7901 break :elem elem;
7902 },
7903 .elems => |elems| elems[0],
7904 .repeated_elem => |elem| elem,
7905 };
7906
7907 try extra.ensureUnusedCapacity(@typeInfo(Repeated).@"struct".field_names.len);
7908 items.appendAssumeCapacity(.{
7909 .tag = .repeated,
7910 .data = addExtraAssumeCapacity(extra, Repeated{
7911 .ty = aggregate.ty,
7912 .elem_val = elem,
7913 }),
7914 });
7915 return gop.put();
7916 }
7917
7918 if (child == .u8_type) bytes: {
7919 const string_bytes = ip.getLocal(tid).getMutableStringBytes(gpa, io);
7920 const start = string_bytes.mutate.len;
7921 try string_bytes.ensureUnusedCapacity(@intCast(len_including_sentinel + 1));
7922 try extra.ensureUnusedCapacity(@typeInfo(Bytes).@"struct".field_names.len);
7923 switch (aggregate.storage) {
7924 .bytes => |bytes| string_bytes.appendSliceAssumeCapacity(.{bytes.toSlice(len, ip)}),
7925 .elems => |elems| for (elems[0..@intCast(len)]) |elem| switch (ip.indexToKey(elem)) {
7926 .undef => {
7927 string_bytes.shrinkRetainingCapacity(start);
7928 break :bytes;
7929 },
7930 .int => |int| string_bytes.appendAssumeCapacity(.{@intCast(int.storage.u64)}),
7931 else => unreachable,
7932 },
7933 .repeated_elem => |elem| switch (ip.indexToKey(elem)) {
7934 .undef => break :bytes,
7935 .int => |int| @memset(
7936 string_bytes.addManyAsSliceAssumeCapacity(@intCast(len))[0],
7937 @intCast(int.storage.u64),
7938 ),
7939 else => unreachable,
7940 },
7941 }
7942 if (sentinel != .none) string_bytes.appendAssumeCapacity(.{
7943 @intCast(ip.indexToKey(sentinel).int.storage.u64),
7944 });
7945 const string = try ip.getOrPutTrailingString(
7946 gpa,
7947 io,
7948 tid,
7949 @intCast(len_including_sentinel),
7950 .maybe_embedded_nulls,
7951 );
7952 items.appendAssumeCapacity(.{
7953 .tag = .bytes,
7954 .data = addExtraAssumeCapacity(extra, Bytes{
7955 .ty = aggregate.ty,
7956 .bytes = string,
7957 }),
7958 });
7959 return gop.put();
7960 }
7961
7962 try extra.ensureUnusedCapacity(
7963 @typeInfo(Tag.Aggregate).@"struct".field_names.len + @as(usize, @intCast(len_including_sentinel + 1)),
7964 );
7965 items.appendAssumeCapacity(.{
7966 .tag = .aggregate,
7967 .data = addExtraAssumeCapacity(extra, Tag.Aggregate{
7968 .ty = aggregate.ty,
7969 }),
7970 });
7971 extra.appendSliceAssumeCapacity(.{@ptrCast(aggregate.storage.elems)});
7972 if (sentinel != .none) extra.appendAssumeCapacity(.{@backingInt(sentinel)});
7973 },
7974 .bitpack => |bitpack| {
7975 switch (ip.zigTypeTag(bitpack.ty)) {
7976 .@"struct" => assert(ip.typeOf(bitpack.backing_int_val) == ip.loadStructType(bitpack.ty).packed_backing_int_type),
7977 .@"union" => assert(ip.typeOf(bitpack.backing_int_val) == ip.loadUnionType(bitpack.ty).packed_backing_int_type),
7978 else => unreachable,
7979 }
7980 assert(!ip.isUndef(bitpack.backing_int_val));
7981 items.appendAssumeCapacity(.{
7982 .tag = .bitpack,
7983 .data = try addExtra(extra, bitpack),
7984 });
7985 },
7986
7987 .memoized_call => |memoized_call| {
7988 for (memoized_call.arg_values) |arg| assert(arg != .none);
7989 try extra.ensureUnusedCapacity(@typeInfo(MemoizedCall).@"struct".field_names.len +
7990 memoized_call.arg_values.len);
7991 items.appendAssumeCapacity(.{
7992 .tag = .memoized_call,
7993 .data = addExtraAssumeCapacity(extra, MemoizedCall{
7994 .func = memoized_call.func,
7995 .args_len = @intCast(memoized_call.arg_values.len),
7996 .result = memoized_call.result,
7997 .branch_count = memoized_call.branch_count,
7998 .branch_quota = memoized_call.branch_quota,
7999 }),
8000 });
8001 extra.appendSliceAssumeCapacity(.{@ptrCast(memoized_call.arg_values)});
8002 },
8003 }
8004 return gop.put();
8005}
8006
8007pub fn getDeclaredStructType(
8008 ip: *InternPool,
8009 gpa: Allocator,
8010 io: Io,
8011 tid: Zcu.PerThread.Id,
8012 ini: struct {
8013 zir_index: TrackedInst.Index,
8014 captures: []const CaptureValue,
8015
8016 // If the value of any of the following fields would change on an incremental update, then logic
8017 // in `Zcu.mapOldZirToNew` must detect that (these properties are all trivially known from ZIR)
8018 // and refuse to map the type declaration. This causes `zir_index` to change so that a new type
8019 // will be interned at a fresh index.
8020 //
8021 // In the future, it would be good to remove all of those fields from `ini`, and in fact just
8022 // have a single function `getDeclaredContainer` which is suitable for all container types.
8023 // However, this requires some major changes to how container types are represented in the
8024 // InternPool, so that it is possible for their backing storage to be "reallocated" as needed
8025 // during type resolution.
8026 fields_len: u32,
8027 layout: std.lang.Type.ContainerLayout,
8028 any_comptime_fields: bool,
8029 any_field_defaults: bool,
8030 any_field_aligns: bool,
8031 packed_backing_mode: BackingTypeMode,
8032 },
8033) Allocator.Error!WipContainerType.Result {
8034 var gop = try ip.getOrPutKey(gpa, io, tid, .{ .struct_type = .{ .declared = .{
8035 .zir_index = ini.zir_index,
8036 .captures = .{ .external = ini.captures },
8037 } } });
8038 defer gop.deinit();
8039 if (gop == .existing) return .{ .existing = gop.existing };
8040
8041 const local = ip.getLocal(tid);
8042 const items = local.getMutableItems(gpa, io);
8043 const extra = local.getMutableExtra(gpa, io);
8044 try items.ensureUnusedCapacity(1);
8045
8046 const field_name_map = try ip.addMap(gpa, io, tid, ini.fields_len);
8047 errdefer local.mutate.maps.len -= 1;
8048
8049 const is_extern = switch (ini.layout) {
8050 .auto => false,
8051 .@"extern" => true,
8052 .@"packed" => {
8053 try extra.ensureUnusedCapacity(@typeInfo(Tag.TypeStructPacked).@"struct".field_names.len +
8054 ini.captures.len + // capture
8055 ini.fields_len + // field_name
8056 ini.fields_len + // field_type
8057 (if (ini.any_field_defaults) ini.fields_len else 0)); // field_default
8058
8059 const extra_index = addExtraAssumeCapacity(extra, Tag.TypeStructPacked{
8060 .zir_index = ini.zir_index,
8061 .bits = .{
8062 .captures_len = @fromBackingInt(@intCast(ini.captures.len)),
8063 .want_layout = false,
8064 },
8065 .name = undefined, // set by `finish`
8066 .name_nav = undefined, // set by `finish`
8067 .namespace = undefined, // set by `finish`
8068 .backing_int_type = .none,
8069 .fields_len = ini.fields_len,
8070 .field_name_map = field_name_map,
8071 });
8072 extra.appendSliceAssumeCapacity(.{@ptrCast(ini.captures)}); // capture
8073 extra.appendNTimesAssumeCapacity(.{@backingInt(NullTerminatedString.empty)}, ini.fields_len); // field_name
8074 extra.appendNTimesAssumeCapacity(.{@backingInt(Index.none)}, ini.fields_len); // field_type
8075 if (ini.any_field_defaults) {
8076 extra.appendNTimesAssumeCapacity(.{@backingInt(Index.none)}, ini.fields_len); // field_default
8077 }
8078 items.appendAssumeCapacity(.{
8079 .tag = switch (ini.packed_backing_mode) {
8080 .auto => if (ini.any_field_defaults) .type_struct_packed_auto_defaults else .type_struct_packed_auto,
8081 .explicit => if (ini.any_field_defaults) .type_struct_packed_explicit_defaults else .type_struct_packed_explicit,
8082 },
8083 .data = extra_index,
8084 });
8085 return .{ .wip = .{
8086 .index = gop.put(),
8087 .tid = tid,
8088 .type_name_index = extra_index + std.meta.fieldIndex(Tag.TypeStructPacked, "name").?,
8089 .name_nav_index = extra_index + std.meta.fieldIndex(Tag.TypeStructPacked, "name_nav").?,
8090 .namespace_index = extra_index + std.meta.fieldIndex(Tag.TypeStructPacked, "namespace").?,
8091 .field_names = undefined,
8092 .field_types = undefined,
8093 .field_values = undefined,
8094 .field_aligns = undefined,
8095 .field_is_comptime_bits = undefined,
8096 } };
8097 },
8098 };
8099
8100 try extra.ensureUnusedCapacity(@typeInfo(Tag.TypeStruct).@"struct".field_names.len +
8101 1 + // captures_len
8102 ini.captures.len + // capture
8103 ini.fields_len + // field_name
8104 ini.fields_len + // field_type
8105 (if (ini.any_field_defaults) ini.fields_len else 0) + // field_default
8106 (if (ini.any_field_aligns) (ini.fields_len + 3) / 4 else 0) + // field_align
8107 (if (ini.any_comptime_fields) (ini.fields_len + 31) / 32 else 0) + // field_is_comptime_bits
8108 (if (!is_extern) ini.fields_len else 0) + // field_runtime_order
8109 ini.fields_len); // field_offset
8110
8111 const extra_index = addExtraAssumeCapacity(extra, Tag.TypeStruct{
8112 .zir_index = ini.zir_index,
8113 .name = undefined, // set by `finish`
8114 .name_nav = undefined, // set by `finish`
8115 .namespace = undefined, // set by `finish`
8116 .fields_len = ini.fields_len,
8117 .field_name_map = field_name_map,
8118 .size = 0,
8119 .flags = .{
8120 .any_captures = if (ini.captures.len != 0) .true else .false,
8121 .layout = if (is_extern) .@"extern" else .auto,
8122 .any_comptime_fields = ini.any_comptime_fields,
8123 .any_field_defaults = ini.any_field_defaults,
8124 .any_field_aligns = ini.any_field_aligns,
8125 .class = .no_possible_value,
8126 .alignment = .none,
8127 .want_layout = false,
8128 },
8129 });
8130 if (ini.captures.len != 0) {
8131 extra.appendAssumeCapacity(.{@intCast(ini.captures.len)}); // captures_len
8132 extra.appendSliceAssumeCapacity(.{@ptrCast(ini.captures)}); // capture
8133 }
8134 extra.appendNTimesAssumeCapacity(.{@backingInt(NullTerminatedString.empty)}, ini.fields_len); // field_name
8135 extra.appendNTimesAssumeCapacity(.{@backingInt(Index.none)}, ini.fields_len); // field_type
8136 if (ini.any_field_defaults) {
8137 extra.appendNTimesAssumeCapacity(.{@backingInt(Index.none)}, ini.fields_len); // field_default
8138 }
8139 if (ini.any_field_aligns) {
8140 extra.appendNTimesAssumeCapacity(.{0}, (ini.fields_len + 3) / 4); // field_align
8141 }
8142 if (ini.any_comptime_fields) {
8143 extra.appendNTimesAssumeCapacity(.{0}, (ini.fields_len + 31) / 32); // field_is_comptime_bits
8144 }
8145 if (!is_extern) {
8146 extra.appendNTimesAssumeCapacity(.{@backingInt(LoadedStructType.RuntimeOrder.unresolved)}, ini.fields_len); // field_runtime_order
8147 }
8148 extra.appendNTimesAssumeCapacity(.{0}, ini.fields_len); // field_offset
8149 items.appendAssumeCapacity(.{
8150 .tag = .type_struct,
8151 .data = extra_index,
8152 });
8153 return .{ .wip = .{
8154 .index = gop.put(),
8155 .tid = tid,
8156 .type_name_index = extra_index + std.meta.fieldIndex(Tag.TypeStruct, "name").?,
8157 .name_nav_index = extra_index + std.meta.fieldIndex(Tag.TypeStruct, "name_nav").?,
8158 .namespace_index = extra_index + std.meta.fieldIndex(Tag.TypeStruct, "namespace").?,
8159 .field_names = undefined,
8160 .field_types = undefined,
8161 .field_values = undefined,
8162 .field_aligns = undefined,
8163 .field_is_comptime_bits = undefined,
8164 } };
8165}
8166
8167pub fn getReifiedStructType(ip: *InternPool, gpa: Allocator, io: Io, tid: Zcu.PerThread.Id, ini: struct {
8168 zir_index: TrackedInst.Index,
8169 type_hash: u64,
8170 fields_len: u32,
8171 layout: std.lang.Type.ContainerLayout,
8172 any_comptime_fields: bool,
8173 any_field_defaults: bool,
8174 any_field_aligns: bool,
8175 /// Explicitly specified backing int type. `.none` if not packed or if backing type is inferred.
8176 packed_backing_int_type: Index,
8177}) Allocator.Error!WipContainerType.Result {
8178 var gop = try ip.getOrPutKey(gpa, io, tid, .{ .struct_type = .{ .reified = .{
8179 .zir_index = ini.zir_index,
8180 .type_hash = ini.type_hash,
8181 } } });
8182 defer gop.deinit();
8183 if (gop == .existing) return .{ .existing = gop.existing };
8184
8185 const local = ip.getLocal(tid);
8186 const items = local.getMutableItems(gpa, io);
8187 const extra = local.getMutableExtra(gpa, io);
8188 try items.ensureUnusedCapacity(1);
8189
8190 const field_name_map = try ip.addMap(gpa, io, tid, ini.fields_len);
8191 errdefer local.mutate.maps.len -= 1;
8192
8193 const is_extern = switch (ini.layout) {
8194 .auto => false,
8195 .@"extern" => true,
8196 .@"packed" => {
8197 try extra.ensureUnusedCapacity(@typeInfo(Tag.TypeStructPacked).@"struct".field_names.len +
8198 2 + // type_hash
8199 ini.fields_len + // field_name
8200 ini.fields_len + // field_type
8201 (if (ini.any_field_defaults) ini.fields_len else 0)); // field_default
8202
8203 const extra_index = addExtraAssumeCapacity(extra, Tag.TypeStructPacked{
8204 .zir_index = ini.zir_index,
8205 .bits = .{
8206 .captures_len = .reified,
8207 .want_layout = false,
8208 },
8209 .name = undefined, // set by `finish`
8210 .name_nav = undefined, // set by `finish`
8211 .namespace = undefined, // set by `finish`
8212 .backing_int_type = ini.packed_backing_int_type,
8213 .fields_len = ini.fields_len,
8214 .field_name_map = field_name_map,
8215 });
8216 _ = addExtraAssumeCapacity(extra, PackedU64.init(ini.type_hash)); // type_hash
8217 const field_names_start = extra.mutate.len;
8218 extra.appendNTimesAssumeCapacity(.{@backingInt(NullTerminatedString.empty)}, ini.fields_len); // field_name
8219 const field_types_start = extra.mutate.len;
8220 extra.appendNTimesAssumeCapacity(.{@backingInt(Index.none)}, ini.fields_len); // field_type
8221 const field_defaults_start = extra.mutate.len;
8222 if (ini.any_field_defaults) {
8223 extra.appendNTimesAssumeCapacity(.{@backingInt(Index.none)}, ini.fields_len); // field_default
8224 }
8225 items.appendAssumeCapacity(.{
8226 .tag = switch (ini.packed_backing_int_type) {
8227 .none => if (ini.any_field_defaults) .type_struct_packed_auto_defaults else .type_struct_packed_auto,
8228 else => if (ini.any_field_defaults) .type_struct_packed_explicit_defaults else .type_struct_packed_explicit,
8229 },
8230 .data = extra_index,
8231 });
8232 return .{ .wip = .{
8233 .index = gop.put(),
8234 .tid = tid,
8235 .type_name_index = extra_index + std.meta.fieldIndex(Tag.TypeStructPacked, "name").?,
8236 .name_nav_index = extra_index + std.meta.fieldIndex(Tag.TypeStructPacked, "name_nav").?,
8237 .namespace_index = extra_index + std.meta.fieldIndex(Tag.TypeStructPacked, "namespace").?,
8238 .field_names = .{ .tid = tid, .start = field_names_start, .len = ini.fields_len },
8239 .field_types = .{ .tid = tid, .start = field_types_start, .len = ini.fields_len },
8240 .field_values = if (ini.any_field_defaults)
8241 .{ .tid = tid, .start = field_defaults_start, .len = ini.fields_len }
8242 else
8243 undefined,
8244 .field_aligns = undefined,
8245 .field_is_comptime_bits = undefined,
8246 } };
8247 },
8248 };
8249
8250 try extra.ensureUnusedCapacity(@typeInfo(Tag.TypeStruct).@"struct".field_names.len +
8251 2 + // type_hash
8252 ini.fields_len + // field_name
8253 ini.fields_len + // field_type
8254 (if (ini.any_field_defaults) ini.fields_len else 0) + // field_default
8255 (if (ini.any_field_aligns) (ini.fields_len + 3) / 4 else 0) + // field_align
8256 (if (ini.any_comptime_fields) (ini.fields_len + 31) / 32 else 0) + // field_is_comptime_bits
8257 (if (!is_extern) ini.fields_len else 0) + // field_runtime_order
8258 ini.fields_len); // field_offset
8259
8260 const extra_index = addExtraAssumeCapacity(extra, Tag.TypeStruct{
8261 .zir_index = ini.zir_index,
8262 .name = undefined, // set by `finish`
8263 .name_nav = undefined, // set by `finish`
8264 .namespace = undefined, // set by `finish`
8265 .fields_len = ini.fields_len,
8266 .field_name_map = field_name_map,
8267 .size = 0,
8268 .flags = .{
8269 .any_captures = .reified,
8270 .layout = if (is_extern) .@"extern" else .auto,
8271 .any_comptime_fields = ini.any_comptime_fields,
8272 .any_field_defaults = ini.any_field_defaults,
8273 .any_field_aligns = ini.any_field_aligns,
8274 .class = .no_possible_value,
8275 .alignment = .none,
8276 .want_layout = false,
8277 },
8278 });
8279 _ = addExtraAssumeCapacity(extra, PackedU64.init(ini.type_hash)); // type_hash
8280 const field_names_start = extra.mutate.len;
8281 extra.appendNTimesAssumeCapacity(.{@backingInt(NullTerminatedString.empty)}, ini.fields_len); // field_name
8282 const field_types_start = extra.mutate.len;
8283 extra.appendNTimesAssumeCapacity(.{@backingInt(Index.none)}, ini.fields_len); // field_type
8284 const field_defaults_start = extra.mutate.len;
8285 if (ini.any_field_defaults) {
8286 extra.appendNTimesAssumeCapacity(.{@backingInt(Index.none)}, ini.fields_len); // field_default
8287 }
8288 const field_aligns_start = extra.mutate.len;
8289 if (ini.any_field_aligns) {
8290 extra.appendNTimesAssumeCapacity(.{0}, (ini.fields_len + 3) / 4); // field_align
8291 }
8292 const field_is_comptime_bits_start = extra.mutate.len;
8293 if (ini.any_comptime_fields) {
8294 extra.appendNTimesAssumeCapacity(.{0}, (ini.fields_len + 31) / 32); // field_is_comptime_bits
8295 }
8296 if (!is_extern) {
8297 extra.appendNTimesAssumeCapacity(.{@backingInt(LoadedStructType.RuntimeOrder.unresolved)}, ini.fields_len); // field_runtime_order
8298 }
8299 extra.appendNTimesAssumeCapacity(.{0}, ini.fields_len); // field_offset
8300 items.appendAssumeCapacity(.{
8301 .tag = .type_struct,
8302 .data = extra_index,
8303 });
8304 return .{ .wip = .{
8305 .index = gop.put(),
8306 .tid = tid,
8307 .type_name_index = extra_index + std.meta.fieldIndex(Tag.TypeStruct, "name").?,
8308 .name_nav_index = extra_index + std.meta.fieldIndex(Tag.TypeStruct, "name_nav").?,
8309 .namespace_index = extra_index + std.meta.fieldIndex(Tag.TypeStruct, "namespace").?,
8310 .field_names = .{ .tid = tid, .start = field_names_start, .len = ini.fields_len },
8311 .field_types = .{ .tid = tid, .start = field_types_start, .len = ini.fields_len },
8312 .field_values = if (ini.any_field_defaults)
8313 .{ .tid = tid, .start = field_defaults_start, .len = ini.fields_len }
8314 else
8315 undefined,
8316 .field_aligns = if (ini.any_field_aligns)
8317 .{ .tid = tid, .start = field_aligns_start, .len = ini.fields_len }
8318 else
8319 undefined,
8320 .field_is_comptime_bits = if (ini.any_comptime_fields)
8321 .{ .tid = tid, .start = field_is_comptime_bits_start, .len = (ini.fields_len + 31) / 32 }
8322 else
8323 undefined,
8324 } };
8325}
8326
8327pub fn getDeclaredUnionType(
8328 ip: *InternPool,
8329 gpa: Allocator,
8330 io: Io,
8331 tid: Zcu.PerThread.Id,
8332 ini: struct {
8333 zir_index: TrackedInst.Index,
8334 captures: []const CaptureValue,
8335
8336 // If the value of any of the following fields would change on an incremental update, then logic
8337 // in `Zcu.mapOldZirToNew` must detect that (these properties are all trivially known from ZIR)
8338 // and refuse to map the type declaration. This causes `zir_index` to change so that a new type
8339 // will be interned at a fresh index.
8340 //
8341 // In the future, it would be good to remove all of those fields from `ini`, and in fact just
8342 // have a single function `getDeclaredContainer` which is suitable for all container types.
8343 // However, this requires some major changes to how container types are represented in the
8344 // InternPool, so that it is possible for their backing storage to be "reallocated" as needed
8345 // during type resolution.
8346 fields_len: u32,
8347 layout: std.lang.Type.ContainerLayout,
8348 any_field_aligns: bool,
8349 tag_usage: LoadedUnionType.TagUsage,
8350 enum_tag_mode: BackingTypeMode,
8351 packed_backing_mode: BackingTypeMode,
8352 },
8353) Allocator.Error!WipContainerType.Result {
8354 var gop = try ip.getOrPutKey(gpa, io, tid, .{ .union_type = .{ .declared = .{
8355 .zir_index = ini.zir_index,
8356 .captures = .{ .external = ini.captures },
8357 } } });
8358 defer gop.deinit();
8359 if (gop == .existing) return .{ .existing = gop.existing };
8360
8361 const local = ip.getLocal(tid);
8362 const items = local.getMutableItems(gpa, io);
8363 const extra = local.getMutableExtra(gpa, io);
8364 try items.ensureUnusedCapacity(1);
8365
8366 const is_extern = switch (ini.layout) {
8367 .auto => false,
8368 .@"extern" => true,
8369 .@"packed" => {
8370 try extra.ensureUnusedCapacity(@typeInfo(Tag.TypeUnionPacked).@"struct".field_names.len +
8371 ini.captures.len + // capture
8372 ini.fields_len); // field_type
8373
8374 const extra_index = addExtraAssumeCapacity(extra, Tag.TypeUnionPacked{
8375 .zir_index = ini.zir_index,
8376 .bits = .{
8377 .captures_len = @fromBackingInt(@intCast(ini.captures.len)),
8378 .want_layout = false,
8379 },
8380 .name = undefined, // set by `finish`
8381 .name_nav = undefined, // set by `finish`
8382 .namespace = undefined, // set by `finish`
8383 .backing_int_type = .none,
8384 .enum_tag_type = .none,
8385 .fields_len = ini.fields_len,
8386 });
8387 extra.appendSliceAssumeCapacity(.{@ptrCast(ini.captures)}); // capture
8388 extra.appendNTimesAssumeCapacity(.{@backingInt(Index.none)}, ini.fields_len); // field_type
8389 items.appendAssumeCapacity(.{
8390 .tag = switch (ini.packed_backing_mode) {
8391 .auto => .type_union_packed_auto,
8392 .explicit => .type_union_packed_explicit,
8393 },
8394 .data = extra_index,
8395 });
8396 return .{ .wip = .{
8397 .index = gop.put(),
8398 .tid = tid,
8399 .type_name_index = extra_index + std.meta.fieldIndex(Tag.TypeUnionPacked, "name").?,
8400 .name_nav_index = extra_index + std.meta.fieldIndex(Tag.TypeUnionPacked, "name_nav").?,
8401 .namespace_index = extra_index + std.meta.fieldIndex(Tag.TypeUnionPacked, "namespace").?,
8402 .field_names = undefined,
8403 .field_types = undefined,
8404 .field_values = undefined,
8405 .field_aligns = undefined,
8406 .field_is_comptime_bits = undefined,
8407 } };
8408 },
8409 };
8410
8411 try extra.ensureUnusedCapacity(@typeInfo(Tag.TypeUnion).@"struct".field_names.len +
8412 1 + // captures_len
8413 ini.captures.len + // capture
8414 ini.fields_len + // field_type
8415 (if (ini.any_field_aligns) (ini.fields_len + 3) / 4 else 0)); // field_align
8416
8417 const extra_index = addExtraAssumeCapacity(extra, Tag.TypeUnion{
8418 .zir_index = ini.zir_index,
8419 .name = undefined, // set by `finish`
8420 .name_nav = undefined, // set by `finish`
8421 .namespace = undefined, // set by `finish`
8422 .enum_tag_type = .none,
8423 .fields_len = ini.fields_len,
8424 .size = 0,
8425 .padding = 0,
8426 .flags = .{
8427 .any_captures = if (ini.captures.len != 0) .true else .false,
8428 .enum_tag_mode = ini.enum_tag_mode,
8429 .layout = if (is_extern) .@"extern" else .auto,
8430 .any_field_aligns = ini.any_field_aligns,
8431 .tag_usage = ini.tag_usage,
8432 .class = .no_possible_value,
8433 .has_runtime_tag = false,
8434 .alignment = .none,
8435 .want_layout = false,
8436 },
8437 });
8438 if (ini.captures.len > 0) {
8439 extra.appendAssumeCapacity(.{@intCast(ini.captures.len)}); // captures_len
8440 extra.appendSliceAssumeCapacity(.{@ptrCast(ini.captures)}); // capture
8441 }
8442 extra.appendNTimesAssumeCapacity(.{@backingInt(Index.none)}, ini.fields_len); // field_type
8443 if (ini.any_field_aligns) {
8444 extra.appendNTimesAssumeCapacity(.{0}, (ini.fields_len + 3) / 4); // field_align
8445 }
8446 items.appendAssumeCapacity(.{
8447 .tag = .type_union,
8448 .data = extra_index,
8449 });
8450 return .{ .wip = .{
8451 .index = gop.put(),
8452 .tid = tid,
8453 .type_name_index = extra_index + std.meta.fieldIndex(Tag.TypeUnion, "name").?,
8454 .name_nav_index = extra_index + std.meta.fieldIndex(Tag.TypeUnion, "name_nav").?,
8455 .namespace_index = extra_index + std.meta.fieldIndex(Tag.TypeUnion, "namespace").?,
8456 .field_names = undefined,
8457 .field_types = undefined,
8458 .field_values = undefined,
8459 .field_aligns = undefined,
8460 .field_is_comptime_bits = undefined,
8461 } };
8462}
8463
8464pub fn getReifiedUnionType(ip: *InternPool, gpa: Allocator, io: Io, tid: Zcu.PerThread.Id, ini: struct {
8465 zir_index: TrackedInst.Index,
8466 type_hash: u64,
8467 fields_len: u32,
8468 layout: std.lang.Type.ContainerLayout,
8469 any_field_aligns: bool,
8470 tag_usage: LoadedUnionType.TagUsage,
8471 /// Explicitly specified enum tag type. `.none` if `tag_usage != .tagged`.
8472 enum_tag_type: Index,
8473 /// Explicitly specified backing int type. `.none` if not packed or if backing type is inferred.
8474 packed_backing_int_type: Index,
8475}) Allocator.Error!WipContainerType.Result {
8476 var gop = try ip.getOrPutKey(gpa, io, tid, .{ .union_type = .{ .reified = .{
8477 .zir_index = ini.zir_index,
8478 .type_hash = ini.type_hash,
8479 } } });
8480 defer gop.deinit();
8481 if (gop == .existing) return .{ .existing = gop.existing };
8482
8483 const local = ip.getLocal(tid);
8484 const items = local.getMutableItems(gpa, io);
8485 const extra = local.getMutableExtra(gpa, io);
8486 try items.ensureUnusedCapacity(1);
8487
8488 const is_extern = switch (ini.layout) {
8489 .auto => false,
8490 .@"extern" => true,
8491 .@"packed" => {
8492 try extra.ensureUnusedCapacity(@typeInfo(Tag.TypeUnionPacked).@"struct".field_names.len +
8493 2 + // type_hash
8494 ini.fields_len + // reified_field_name
8495 ini.fields_len); // field_type
8496
8497 const extra_index = addExtraAssumeCapacity(extra, Tag.TypeUnionPacked{
8498 .zir_index = ini.zir_index,
8499 .bits = .{
8500 .captures_len = .reified,
8501 .want_layout = false,
8502 },
8503 .name = undefined, // set by `finish`
8504 .name_nav = undefined, // set by `finish`
8505 .namespace = undefined, // set by `finish`
8506 .backing_int_type = ini.packed_backing_int_type,
8507 .enum_tag_type = .none,
8508 .fields_len = ini.fields_len,
8509 });
8510 _ = addExtraAssumeCapacity(extra, PackedU64.init(ini.type_hash)); // type_hash
8511 const field_names_start = extra.mutate.len;
8512 extra.appendNTimesAssumeCapacity(.{@backingInt(NullTerminatedString.empty)}, ini.fields_len); // reified_field_name
8513 const field_types_start = extra.mutate.len;
8514 extra.appendNTimesAssumeCapacity(.{@backingInt(Index.none)}, ini.fields_len); // field_type
8515 items.appendAssumeCapacity(.{
8516 .tag = switch (ini.packed_backing_int_type) {
8517 .none => .type_union_packed_auto,
8518 else => .type_union_packed_explicit,
8519 },
8520 .data = extra_index,
8521 });
8522 return .{ .wip = .{
8523 .index = gop.put(),
8524 .tid = tid,
8525 .type_name_index = extra_index + std.meta.fieldIndex(Tag.TypeUnionPacked, "name").?,
8526 .name_nav_index = extra_index + std.meta.fieldIndex(Tag.TypeUnionPacked, "name_nav").?,
8527 .namespace_index = extra_index + std.meta.fieldIndex(Tag.TypeUnionPacked, "namespace").?,
8528 .field_names = .{ .tid = tid, .start = field_names_start, .len = ini.fields_len },
8529 .field_types = .{ .tid = tid, .start = field_types_start, .len = ini.fields_len },
8530 .field_values = undefined,
8531 .field_aligns = undefined,
8532 .field_is_comptime_bits = undefined,
8533 } };
8534 },
8535 };
8536
8537 try extra.ensureUnusedCapacity(@typeInfo(Tag.TypeUnion).@"struct".field_names.len +
8538 2 + // type_hash
8539 ini.fields_len + // reified_field_name
8540 ini.fields_len + // field_type
8541 (if (ini.any_field_aligns) (ini.fields_len + 3) / 4 else 0)); // field_align
8542
8543 const extra_index = addExtraAssumeCapacity(extra, Tag.TypeUnion{
8544 .zir_index = ini.zir_index,
8545 .name = undefined, // set by `finish`
8546 .name_nav = undefined, // set by `finish`
8547 .namespace = undefined, // set by `finish`
8548 .enum_tag_type = ini.enum_tag_type,
8549 .fields_len = ini.fields_len,
8550 .size = 0,
8551 .padding = 0,
8552 .flags = .{
8553 .any_captures = .reified,
8554 .enum_tag_mode = if (ini.enum_tag_type == .none) .auto else .explicit,
8555 .layout = if (is_extern) .@"extern" else .auto,
8556 .any_field_aligns = ini.any_field_aligns,
8557 .tag_usage = ini.tag_usage,
8558 .class = .no_possible_value,
8559 .has_runtime_tag = false,
8560 .alignment = .none,
8561 .want_layout = false,
8562 },
8563 });
8564 _ = addExtraAssumeCapacity(extra, PackedU64.init(ini.type_hash));
8565 const field_names_start = extra.mutate.len;
8566 extra.appendNTimesAssumeCapacity(.{@backingInt(NullTerminatedString.empty)}, ini.fields_len); // reified_field_name
8567 const field_types_start = extra.mutate.len;
8568 extra.appendNTimesAssumeCapacity(.{@backingInt(Index.none)}, ini.fields_len); // field_type
8569 const field_aligns_start = extra.mutate.len;
8570 if (ini.any_field_aligns) {
8571 extra.appendNTimesAssumeCapacity(.{0}, (ini.fields_len + 3) / 4); // field_align
8572 }
8573 items.appendAssumeCapacity(.{
8574 .tag = .type_union,
8575 .data = extra_index,
8576 });
8577 return .{ .wip = .{
8578 .index = gop.put(),
8579 .tid = tid,
8580 .type_name_index = extra_index + std.meta.fieldIndex(Tag.TypeUnion, "name").?,
8581 .name_nav_index = extra_index + std.meta.fieldIndex(Tag.TypeUnion, "name_nav").?,
8582 .namespace_index = extra_index + std.meta.fieldIndex(Tag.TypeUnion, "namespace").?,
8583 .field_names = .{ .tid = tid, .start = field_names_start, .len = ini.fields_len },
8584 .field_types = .{ .tid = tid, .start = field_types_start, .len = ini.fields_len },
8585 .field_values = undefined,
8586 .field_aligns = if (ini.any_field_aligns)
8587 .{ .tid = tid, .start = field_aligns_start, .len = ini.fields_len }
8588 else
8589 undefined,
8590 .field_is_comptime_bits = undefined,
8591 } };
8592}
8593
8594pub fn getDeclaredEnumType(
8595 ip: *InternPool,
8596 gpa: Allocator,
8597 io: Io,
8598 tid: Zcu.PerThread.Id,
8599 ini: struct {
8600 zir_index: TrackedInst.Index,
8601 captures: []const CaptureValue,
8602
8603 // If the value of any of the following fields would change on an incremental update, then logic
8604 // in `Zcu.mapOldZirToNew` must detect that (these properties are all trivially known from ZIR)
8605 // and refuse to map the type declaration. This causes `zir_index` to change so that a new type
8606 // will be interned at a fresh index.
8607 //
8608 // In the future, it would be good to remove all of those fields from `ini`, and in fact just
8609 // have a single function `getDeclaredContainer` which is suitable for all container types.
8610 // However, this requires some major changes to how container types are represented in the
8611 // InternPool, so that it is possible for their backing storage to be "reallocated" as needed
8612 // during type resolution.
8613 fields_len: u32,
8614 nonexhaustive: bool,
8615 /// For `enum(T)` this is `.explicit`. Otherwise this is `.none`.
8616 int_tag_mode: BackingTypeMode,
8617 },
8618) Allocator.Error!WipContainerType.Result {
8619 var gop = try ip.getOrPutKey(gpa, io, tid, .{ .enum_type = .{ .declared = .{
8620 .zir_index = ini.zir_index,
8621 .captures = .{ .external = ini.captures },
8622 } } });
8623 defer gop.deinit();
8624 if (gop == .existing) return .{ .existing = gop.existing };
8625
8626 const local = ip.getLocal(tid);
8627 const items = local.getMutableItems(gpa, io);
8628 const extra = local.getMutableExtra(gpa, io);
8629 try items.ensureUnusedCapacity(1);
8630
8631 const tag: Tag, const have_values: bool = if (ini.nonexhaustive)
8632 .{ .type_enum_nonexhaustive, true }
8633 else if (ini.int_tag_mode == .explicit)
8634 .{ .type_enum_explicit, true }
8635 else
8636 .{ .type_enum_auto, false };
8637
8638 const field_name_map = try ip.addMap(gpa, io, tid, ini.fields_len);
8639 errdefer local.mutate.maps.len -= 1;
8640
8641 const field_value_map = if (have_values) try ip.addMap(gpa, io, tid, ini.fields_len) else undefined;
8642 errdefer local.mutate.maps.len -= @intFromBool(have_values);
8643
8644 try extra.ensureUnusedCapacity(@typeInfo(Tag.TypeEnum).@"struct".field_names.len +
8645 1 + // zir_index
8646 ini.captures.len + // capture
8647 @intFromBool(have_values) + // field_value_map
8648 ini.fields_len + // field_name
8649 (if (have_values) ini.fields_len else 0)); // field_value
8650
8651 const extra_index = addExtraAssumeCapacity(extra, Tag.TypeEnum{
8652 .bits = .{
8653 .captures_len = @fromBackingInt(@intCast(ini.captures.len)),
8654 .want_layout = false,
8655 },
8656 .name = undefined, // set by `finish`
8657 .name_nav = undefined, // set by `finish`
8658 .namespace = undefined, // set by `finish`
8659 .int_tag_type = .none,
8660 .fields_len = ini.fields_len,
8661 .field_name_map = field_name_map,
8662 });
8663 extra.appendAssumeCapacity(.{@backingInt(ini.zir_index)}); // zir_index
8664 extra.appendSliceAssumeCapacity(.{@ptrCast(ini.captures)}); // capture
8665 if (have_values) extra.appendAssumeCapacity(.{@backingInt(field_value_map)}); // field_value_map
8666 extra.appendNTimesAssumeCapacity(.{@backingInt(NullTerminatedString.empty)}, ini.fields_len); // field_name
8667 if (have_values) extra.appendNTimesAssumeCapacity(.{@backingInt(Index.none)}, ini.fields_len); // field_value
8668 items.appendAssumeCapacity(.{
8669 .tag = tag,
8670 .data = extra_index,
8671 });
8672 return .{ .wip = .{
8673 .index = gop.put(),
8674 .tid = tid,
8675 .type_name_index = extra_index + std.meta.fieldIndex(Tag.TypeEnum, "name").?,
8676 .name_nav_index = extra_index + std.meta.fieldIndex(Tag.TypeEnum, "name_nav").?,
8677 .namespace_index = extra_index + std.meta.fieldIndex(Tag.TypeEnum, "namespace").?,
8678 .field_names = undefined,
8679 .field_types = undefined,
8680 .field_values = undefined,
8681 .field_aligns = undefined,
8682 .field_is_comptime_bits = undefined,
8683 } };
8684}
8685
8686pub fn getReifiedEnumType(ip: *InternPool, gpa: Allocator, io: Io, tid: Zcu.PerThread.Id, ini: struct {
8687 zir_index: TrackedInst.Index,
8688 type_hash: u64,
8689 fields_len: u32,
8690 nonexhaustive: bool,
8691 /// Explicitly specified int tag type, or `.none` if the int tag type is inferred.
8692 int_tag_type: Index,
8693}) Allocator.Error!WipContainerType.Result {
8694 var gop = try ip.getOrPutKey(gpa, io, tid, .{ .enum_type = .{ .reified = .{
8695 .zir_index = ini.zir_index,
8696 .type_hash = ini.type_hash,
8697 } } });
8698 defer gop.deinit();
8699 if (gop == .existing) return .{ .existing = gop.existing };
8700
8701 const local = ip.getLocal(tid);
8702 const items = local.getMutableItems(gpa, io);
8703 const extra = local.getMutableExtra(gpa, io);
8704 try items.ensureUnusedCapacity(1);
8705
8706 const tag: Tag, const have_values: bool = if (ini.nonexhaustive)
8707 .{ .type_enum_nonexhaustive, true }
8708 else if (ini.int_tag_type != .none)
8709 .{ .type_enum_explicit, true }
8710 else
8711 .{ .type_enum_auto, false };
8712
8713 const field_name_map = try ip.addMap(gpa, io, tid, ini.fields_len);
8714 errdefer local.mutate.maps.len -= 1;
8715
8716 const field_value_map = if (have_values) try ip.addMap(gpa, io, tid, ini.fields_len) else undefined;
8717 errdefer local.mutate.maps.len -= @intFromBool(have_values);
8718
8719 try extra.ensureUnusedCapacity(@typeInfo(Tag.TypeEnum).@"struct".field_names.len +
8720 1 + // zir_index
8721 2 + // type_hash
8722 @intFromBool(have_values) + // field_value_map
8723 ini.fields_len + // field_name
8724 (if (have_values) ini.fields_len else 0)); // field_value
8725
8726 const extra_index = addExtraAssumeCapacity(extra, Tag.TypeEnum{
8727 .bits = .{
8728 .captures_len = .reified,
8729 .want_layout = false,
8730 },
8731 .name = undefined, // set by `finish`
8732 .name_nav = undefined, // set by `finish`
8733 .namespace = undefined, // set by `finish`
8734 .int_tag_type = ini.int_tag_type,
8735 .fields_len = ini.fields_len,
8736 .field_name_map = field_name_map,
8737 });
8738 extra.appendAssumeCapacity(.{@backingInt(ini.zir_index)}); // zir_index
8739 _ = addExtraAssumeCapacity(extra, PackedU64.init(ini.type_hash)); // type_hash
8740 if (have_values) extra.appendAssumeCapacity(.{@backingInt(field_value_map)}); // field_value_map
8741 const field_names_start = extra.mutate.len;
8742 extra.appendNTimesAssumeCapacity(.{@backingInt(NullTerminatedString.empty)}, ini.fields_len); // field_name
8743 const field_values_start = extra.mutate.len;
8744 if (have_values) extra.appendNTimesAssumeCapacity(.{@backingInt(Index.none)}, ini.fields_len); // field_value
8745 items.appendAssumeCapacity(.{
8746 .tag = tag,
8747 .data = extra_index,
8748 });
8749 return .{ .wip = .{
8750 .index = gop.put(),
8751 .tid = tid,
8752 .type_name_index = extra_index + std.meta.fieldIndex(Tag.TypeEnum, "name").?,
8753 .name_nav_index = extra_index + std.meta.fieldIndex(Tag.TypeEnum, "name_nav").?,
8754 .namespace_index = extra_index + std.meta.fieldIndex(Tag.TypeEnum, "namespace").?,
8755 .field_names = .{ .tid = tid, .start = field_names_start, .len = ini.fields_len },
8756 .field_types = undefined,
8757 .field_values = if (have_values)
8758 .{ .tid = tid, .start = field_values_start, .len = ini.fields_len }
8759 else
8760 undefined,
8761 .field_aligns = undefined,
8762 .field_is_comptime_bits = undefined,
8763 } };
8764}
8765
8766pub fn getReifiedSpirvType(
8767 ip: *InternPool,
8768 gpa: Allocator,
8769 io: Io,
8770 tid: Zcu.PerThread.Id,
8771 type_spirv: Key.SpirvType,
8772) Allocator.Error!Index {
8773 var gop = try ip.getOrPutKey(gpa, io, tid, .{ .spirv_type = .{
8774 .ty = type_spirv.ty,
8775 .flags = type_spirv.flags,
8776 } });
8777 defer gop.deinit();
8778 if (gop == .existing) return gop.existing;
8779
8780 const local = ip.getLocal(tid);
8781 const items = local.getMutableItems(gpa, io);
8782 const extra = local.getMutableExtra(gpa, io);
8783 try items.ensureUnusedCapacity(1);
8784
8785 try extra.ensureUnusedCapacity(@typeInfo(Tag.TypeSpirv).@"struct".field_names.len);
8786 const extra_index = addExtraAssumeCapacity(extra, type_spirv);
8787
8788 items.appendAssumeCapacity(.{ .tag = .type_spirv, .data = extra_index });
8789 return gop.put();
8790}
8791
8792pub fn getGeneratedEnumTagType(ip: *InternPool, gpa: Allocator, io: Io, tid: Zcu.PerThread.Id, ini: struct {
8793 /// The union type for which this enum is a generated tag.
8794 union_type: Index,
8795 /// For `union(enum(T))` this is `.explicit`. Otherwise this is `.none`.
8796 int_tag_mode: BackingTypeMode,
8797 fields_len: u32,
8798}) Allocator.Error!WipContainerType.Result {
8799 var gop = try ip.getOrPutKey(gpa, io, tid, .{ .enum_type = .{ .generated_union_tag = ini.union_type } });
8800 defer gop.deinit();
8801 if (gop == .existing) return .{ .existing = gop.existing };
8802
8803 const local = ip.getLocal(tid);
8804 const items = local.getMutableItems(gpa, io);
8805 const extra = local.getMutableExtra(gpa, io);
8806 try items.ensureUnusedCapacity(1);
8807
8808 const field_name_map = try ip.addMap(gpa, io, tid, ini.fields_len);
8809 errdefer local.mutate.maps.len -= 1;
8810
8811 const have_values = switch (ini.int_tag_mode) {
8812 .explicit => true,
8813 .auto => false,
8814 };
8815
8816 const field_value_map = if (have_values) try ip.addMap(gpa, io, tid, ini.fields_len) else undefined;
8817 errdefer local.mutate.maps.len -= @intFromBool(have_values);
8818
8819 try extra.ensureUnusedCapacity(@typeInfo(Tag.TypeEnum).@"struct".field_names.len +
8820 1 + // owner_union
8821 @intFromBool(have_values) + // field_value_map
8822 ini.fields_len + // field_name
8823 (if (have_values) ini.fields_len else 0)); // field_value
8824
8825 const extra_index = addExtraAssumeCapacity(extra, Tag.TypeEnum{
8826 .bits = .{
8827 .captures_len = .generated_union_tag,
8828 .want_layout = false,
8829 },
8830 .name = undefined, // set by `finish`
8831 .name_nav = undefined, // set by `finish`
8832 .namespace = undefined, // set by `finish`
8833 .int_tag_type = .none,
8834 .fields_len = ini.fields_len,
8835 .field_name_map = field_name_map,
8836 });
8837 extra.appendAssumeCapacity(.{@backingInt(ini.union_type)}); // owner_union
8838 if (have_values) extra.appendAssumeCapacity(.{@backingInt(field_value_map)});
8839 extra.appendNTimesAssumeCapacity(.{@backingInt(NullTerminatedString.empty)}, ini.fields_len); // field_name
8840 if (have_values) extra.appendNTimesAssumeCapacity(.{@backingInt(Index.none)}, ini.fields_len); // field_value
8841 items.appendAssumeCapacity(.{
8842 .tag = switch (ini.int_tag_mode) {
8843 .auto => .type_enum_auto,
8844 .explicit => .type_enum_explicit,
8845 },
8846 .data = extra_index,
8847 });
8848 return .{ .wip = .{
8849 .index = gop.put(),
8850 .tid = tid,
8851 .type_name_index = extra_index + std.meta.fieldIndex(Tag.TypeEnum, "name").?,
8852 .name_nav_index = extra_index + std.meta.fieldIndex(Tag.TypeEnum, "name_nav").?,
8853 .namespace_index = extra_index + std.meta.fieldIndex(Tag.TypeEnum, "namespace").?,
8854 .field_names = undefined,
8855 .field_types = undefined,
8856 .field_values = undefined,
8857 .field_aligns = undefined,
8858 .field_is_comptime_bits = undefined,
8859 } };
8860}
8861
8862pub fn getDeclaredOpaqueType(ip: *InternPool, gpa: Allocator, io: Io, tid: Zcu.PerThread.Id, ini: struct {
8863 zir_index: TrackedInst.Index,
8864 captures: []const CaptureValue,
8865}) Allocator.Error!WipContainerType.Result {
8866 var gop = try ip.getOrPutKey(gpa, io, tid, .{ .opaque_type = .{ .declared = .{
8867 .zir_index = ini.zir_index,
8868 .captures = .{ .external = ini.captures },
8869 } } });
8870 defer gop.deinit();
8871 if (gop == .existing) return .{ .existing = gop.existing };
8872
8873 const local = ip.getLocal(tid);
8874 const items = local.getMutableItems(gpa, io);
8875 const extra = local.getMutableExtra(gpa, io);
8876 try items.ensureUnusedCapacity(1);
8877
8878 try extra.ensureUnusedCapacity(@typeInfo(Tag.TypeOpaque).@"struct".field_names.len + ini.captures.len);
8879 const extra_index = addExtraAssumeCapacity(extra, Tag.TypeOpaque{
8880 .zir_index = ini.zir_index,
8881 .captures_len = @intCast(ini.captures.len),
8882 .name = undefined, // set by `finish`
8883 .name_nav = undefined, // set by `finish`
8884 .namespace = undefined, // set by `finish`
8885 });
8886 extra.appendSliceAssumeCapacity(.{@ptrCast(ini.captures)});
8887 items.appendAssumeCapacity(.{
8888 .tag = .type_opaque,
8889 .data = extra_index,
8890 });
8891 return .{ .wip = .{
8892 .index = gop.put(),
8893 .tid = tid,
8894 .type_name_index = extra_index + std.meta.fieldIndex(Tag.TypeOpaque, "name").?,
8895 .name_nav_index = extra_index + std.meta.fieldIndex(Tag.TypeOpaque, "name_nav").?,
8896 .namespace_index = extra_index + std.meta.fieldIndex(Tag.TypeOpaque, "namespace").?,
8897 .field_names = undefined,
8898 .field_types = undefined,
8899 .field_values = undefined,
8900 .field_aligns = undefined,
8901 .field_is_comptime_bits = undefined,
8902 } };
8903}
8904
8905pub const WipContainerType = struct {
8906 index: Index,
8907 tid: Zcu.PerThread.Id,
8908 type_name_index: u32,
8909 name_nav_index: u32,
8910 namespace_index: u32,
8911
8912 // These fields are only populated when creating reified types, because reified types populate
8913 // field information immediately, with type resolution only handling validation. This is in
8914 // contrast to declared types, where field information is populated by the type resolution
8915 // process evaluating ZIR expressions.
8916 field_names: NullTerminatedString.Slice,
8917 field_types: Index.Slice,
8918 field_values: Index.Slice,
8919 field_aligns: Alignment.Slice,
8920 field_is_comptime_bits: LoadedStructType.ComptimeBits,
8921
8922 pub fn setName(
8923 wip: WipContainerType,
8924 ip: *InternPool,
8925 type_name: NullTerminatedString,
8926 /// This should be the `Nav` we are named after if we use the `.parent` name strategy; `.none` otherwise.
8927 /// This is also `.none` if we use `.parent` because we are the root struct type for a file.
8928 name_nav: Nav.Index.Optional,
8929 ) void {
8930 const extra = ip.getLocalShared(wip.tid).extra.acquire();
8931 const extra_items = extra.view().items(.@"0");
8932 extra_items[wip.type_name_index] = @backingInt(type_name);
8933 extra_items[wip.name_nav_index] = @backingInt(name_nav);
8934 }
8935
8936 pub fn finish(
8937 wip: WipContainerType,
8938 ip: *InternPool,
8939 namespace: NamespaceIndex,
8940 ) Index {
8941 const extra = ip.getLocalShared(wip.tid).extra.acquire();
8942 const extra_items = extra.view().items(.@"0");
8943
8944 extra_items[wip.namespace_index] = @backingInt(namespace);
8945
8946 return wip.index;
8947 }
8948
8949 pub fn cancel(wip: WipContainerType, ip: *InternPool, tid: Zcu.PerThread.Id) void {
8950 ip.remove(tid, wip.index);
8951 }
8952
8953 pub const Result = union(enum) {
8954 wip: WipContainerType,
8955 existing: Index,
8956 };
8957};
8958
8959pub fn getUnion(
8960 ip: *InternPool,
8961 gpa: Allocator,
8962 io: Io,
8963 tid: Zcu.PerThread.Id,
8964 un: Key.Union,
8965) Allocator.Error!Index {
8966 assert(un.ty != .none);
8967 assert(un.val != .none);
8968 assert(ip.loadUnionType(un.ty).layout != .@"packed");
8969
8970 var gop = try ip.getOrPutKey(gpa, io, tid, .{ .un = un });
8971 defer gop.deinit();
8972 if (gop == .existing) return gop.existing;
8973 const local = ip.getLocal(tid);
8974 const items = local.getMutableItems(gpa, io);
8975 const extra = local.getMutableExtra(gpa, io);
8976 try items.ensureUnusedCapacity(1);
8977
8978 items.appendAssumeCapacity(.{
8979 .tag = .union_value,
8980 .data = try addExtra(extra, un),
8981 });
8982
8983 return gop.put();
8984}
8985
8986pub const TupleTypeInit = struct {
8987 types: []const Index,
8988 /// These elements may be `none`, indicating runtime-known.
8989 values: []const Index,
8990};
8991
8992pub fn getTupleType(
8993 ip: *InternPool,
8994 gpa: Allocator,
8995 io: Io,
8996 tid: Zcu.PerThread.Id,
8997 ini: TupleTypeInit,
8998) Allocator.Error!Index {
8999 assert(ini.types.len == ini.values.len);
9000 for (ini.types) |elem| assert(elem != .none);
9001
9002 const local = ip.getLocal(tid);
9003 const items = local.getMutableItems(gpa, io);
9004 const extra = local.getMutableExtra(gpa, io);
9005
9006 const prev_extra_len = extra.mutate.len;
9007 const fields_len: u32 = @intCast(ini.types.len);
9008
9009 try items.ensureUnusedCapacity(1);
9010 try extra.ensureUnusedCapacity(
9011 @typeInfo(TypeTuple).@"struct".field_names.len + (fields_len * 3),
9012 );
9013
9014 const extra_index = addExtraAssumeCapacity(extra, TypeTuple{
9015 .fields_len = fields_len,
9016 });
9017 extra.appendSliceAssumeCapacity(.{@ptrCast(ini.types)});
9018 extra.appendSliceAssumeCapacity(.{@ptrCast(ini.values)});
9019 errdefer extra.mutate.len = prev_extra_len;
9020
9021 var gop = try ip.getOrPutKey(gpa, io, tid, .{ .tuple_type = extraTypeTuple(tid, extra.list.*, extra_index) });
9022 defer gop.deinit();
9023 if (gop == .existing) {
9024 extra.mutate.len = prev_extra_len;
9025 return gop.existing;
9026 }
9027
9028 items.appendAssumeCapacity(.{
9029 .tag = .type_tuple,
9030 .data = extra_index,
9031 });
9032 return gop.put();
9033}
9034
9035/// This is equivalent to `Key.FuncType` but adjusted to have a slice for `param_types`.
9036pub const GetFuncTypeKey = struct {
9037 param_types: []const Index,
9038 return_type: Index,
9039 comptime_bits: u32 = 0,
9040 noalias_bits: u32 = 0,
9041 /// `null` means generic.
9042 cc: ?std.lang.CallingConvention = .auto,
9043 is_var_args: bool = false,
9044 is_noinline: bool = false,
9045};
9046
9047pub fn getFuncType(
9048 ip: *InternPool,
9049 gpa: Allocator,
9050 io: Io,
9051 tid: Zcu.PerThread.Id,
9052 key: GetFuncTypeKey,
9053) Allocator.Error!Index {
9054 // Validate input parameters.
9055 assert(key.return_type != .none);
9056 for (key.param_types) |param_type| assert(param_type != .none);
9057
9058 const local = ip.getLocal(tid);
9059 const items = local.getMutableItems(gpa, io);
9060 try items.ensureUnusedCapacity(1);
9061 const extra = local.getMutableExtra(gpa, io);
9062
9063 // The strategy here is to add the function type unconditionally, then to
9064 // ask if it already exists, and if so, revert the lengths of the mutated
9065 // arrays. This is similar to what `getOrPutTrailingString` does.
9066 const prev_extra_len = extra.mutate.len;
9067 const packed_cc: PackedCallingConvention = .pack(key.cc orelse .auto);
9068 const cc_extra_len = packed_cc.extraLen();
9069 const params_len: u32 = @intCast(key.param_types.len);
9070
9071 try extra.ensureUnusedCapacity(@typeInfo(Tag.TypeFunction).@"struct".field_names.len +
9072 @intFromBool(key.comptime_bits != 0) +
9073 @intFromBool(key.noalias_bits != 0) +
9074 cc_extra_len +
9075 params_len);
9076
9077 const func_type_extra_index = addExtraAssumeCapacity(extra, Tag.TypeFunction{
9078 .params_len = params_len,
9079 .return_type = key.return_type,
9080 .flags = .{
9081 .cc = packed_cc,
9082 .is_var_args = key.is_var_args,
9083 .has_comptime_bits = key.comptime_bits != 0,
9084 .has_noalias_bits = key.noalias_bits != 0,
9085 .is_noinline = key.is_noinline,
9086 },
9087 });
9088
9089 if (key.comptime_bits != 0) extra.appendAssumeCapacity(.{key.comptime_bits});
9090 if (key.noalias_bits != 0) extra.appendAssumeCapacity(.{key.noalias_bits});
9091 if (key.cc) |cc| switch (cc) {
9092 .spirv_kernel, .spirv_task => |kernel| extra.appendSliceAssumeCapacity(.{&.{
9093 kernel.x,
9094 kernel.y,
9095 kernel.z,
9096 }}),
9097 .spirv_mesh => |mesh| extra.appendSliceAssumeCapacity(.{&.{
9098 mesh.max_primitives,
9099 mesh.max_vertices,
9100 mesh.x,
9101 mesh.y,
9102 mesh.z,
9103 }}),
9104 else => {},
9105 };
9106 extra.appendSliceAssumeCapacity(.{@ptrCast(key.param_types)});
9107 errdefer extra.mutate.len = prev_extra_len;
9108
9109 var gop = try ip.getOrPutKey(gpa, io, tid, .{
9110 .func_type = extraFuncType(tid, extra.list.*, func_type_extra_index),
9111 });
9112 defer gop.deinit();
9113 if (gop == .existing) {
9114 extra.mutate.len = prev_extra_len;
9115 return gop.existing;
9116 }
9117
9118 items.appendAssumeCapacity(.{
9119 .tag = .type_function,
9120 .data = func_type_extra_index,
9121 });
9122 return gop.put();
9123}
9124
9125/// Intern an `.@"extern"`, creating a corresponding owner `Nav` if necessary.
9126/// This will *not* queue the extern for codegen: see `Zcu.PerThread.getExtern` for a wrapper which does.
9127pub fn getExtern(
9128 ip: *InternPool,
9129 gpa: Allocator,
9130 io: Io,
9131 tid: Zcu.PerThread.Id,
9132 /// `key.owner_nav` is ignored.
9133 key: Key.Extern,
9134) Allocator.Error!struct {
9135 index: Index,
9136 /// Only set if the `Nav` was newly created.
9137 new_nav: Nav.Index.Optional,
9138} {
9139 var gop = try ip.getOrPutKey(gpa, io, tid, .{ .@"extern" = key });
9140 defer gop.deinit();
9141 if (gop == .existing) return .{
9142 .index = gop.existing,
9143 .new_nav = .none,
9144 };
9145
9146 const local = ip.getLocal(tid);
9147 const items = local.getMutableItems(gpa, io);
9148 const extra = local.getMutableExtra(gpa, io);
9149 try items.ensureUnusedCapacity(1);
9150 try extra.ensureUnusedCapacity(@typeInfo(Tag.Extern).@"struct".field_names.len);
9151 try local.getMutableNavs(gpa, io).ensureUnusedCapacity(1);
9152
9153 // Predict the index the `@"extern" will live at, so we can construct the owner `Nav` before releasing the shard's mutex.
9154 const extern_index = Index.Unwrapped.wrap(.{
9155 .tid = tid,
9156 .index = items.mutate.len,
9157 }, ip);
9158 const owner_nav = ip.createNav(gpa, io, tid, key.name, key.name, .{
9159 .type = key.ty,
9160 .@"align" = key.alignment,
9161 .@"linksection" = .none,
9162 .@"addrspace" = key.@"addrspace",
9163 .@"const" = key.is_const,
9164 .@"threadlocal" = key.is_threadlocal,
9165 .is_extern_decl = true,
9166 .value = extern_index,
9167 }) catch unreachable; // capacity asserted above
9168 const decoration_type, const location_or_descriptor_set, const descriptor_binding = if (key.decoration) |decoration| switch (decoration) {
9169 .location => |location| .{ Tag.Extern.Flags.DecorationType.location, location, undefined },
9170 .flat => |location| .{ Tag.Extern.Flags.DecorationType.flat, location, undefined },
9171 .descriptor => |descriptor| .{ Tag.Extern.Flags.DecorationType.descriptor, descriptor.set, descriptor.binding },
9172 } else .{ Tag.Extern.Flags.DecorationType.none, undefined, undefined };
9173 const extra_index = addExtraAssumeCapacity(extra, Tag.Extern{
9174 .ty = key.ty,
9175 .lib_name = key.lib_name,
9176 .location_or_descriptor_set = location_or_descriptor_set,
9177 .descriptor_binding = descriptor_binding,
9178 .flags = .{
9179 .linkage = key.linkage,
9180 .visibility = key.visibility,
9181 .is_dll_import = key.is_dll_import,
9182 .relocation = key.relocation,
9183 .decoration_type = decoration_type,
9184 .source = key.source,
9185 },
9186 .zir_index = key.zir_index,
9187 .owner_nav = owner_nav,
9188 });
9189 items.appendAssumeCapacity(.{
9190 .tag = .@"extern",
9191 .data = extra_index,
9192 });
9193 assert(gop.put() == extern_index);
9194
9195 return .{
9196 .index = extern_index,
9197 .new_nav = owner_nav.toOptional(),
9198 };
9199}
9200
9201pub const GetFuncDeclKey = struct {
9202 owner_nav: Nav.Index,
9203 ty: Index,
9204 zir_body_inst: TrackedInst.Index,
9205 lbrace_line: u32,
9206 rbrace_line: u32,
9207 lbrace_column: u32,
9208 rbrace_column: u32,
9209 cc: ?std.lang.CallingConvention,
9210 is_noinline: bool,
9211};
9212
9213pub fn getFuncDecl(
9214 ip: *InternPool,
9215 gpa: Allocator,
9216 io: Io,
9217 tid: Zcu.PerThread.Id,
9218 key: GetFuncDeclKey,
9219) Allocator.Error!Index {
9220 const local = ip.getLocal(tid);
9221 const items = local.getMutableItems(gpa, io);
9222 try items.ensureUnusedCapacity(1);
9223 const extra = local.getMutableExtra(gpa, io);
9224
9225 // The strategy here is to add the function type unconditionally, then to
9226 // ask if it already exists, and if so, revert the lengths of the mutated
9227 // arrays. This is similar to what `getOrPutTrailingString` does.
9228 const prev_extra_len = extra.mutate.len;
9229
9230 try extra.ensureUnusedCapacity(@typeInfo(Tag.FuncDecl).@"struct".field_names.len);
9231
9232 const func_decl_extra_index = addExtraAssumeCapacity(extra, Tag.FuncDecl{
9233 .analysis = .{
9234 .want_runtime_analysis = false,
9235 .branch_hint = .none,
9236 .is_noinline = key.is_noinline,
9237 .has_error_trace = false,
9238 .inferred_error_set = false,
9239 .disable_instrumentation = false,
9240 .disable_intrinsics = false,
9241 },
9242 .owner_nav = key.owner_nav,
9243 .ty = key.ty,
9244 .zir_body_inst = key.zir_body_inst,
9245 .lbrace_line = key.lbrace_line,
9246 .rbrace_line = key.rbrace_line,
9247 .lbrace_column = key.lbrace_column,
9248 .rbrace_column = key.rbrace_column,
9249 });
9250 errdefer extra.mutate.len = prev_extra_len;
9251
9252 var gop = try ip.getOrPutKey(gpa, io, tid, .{
9253 .func = extraFuncDecl(tid, extra.list.*, func_decl_extra_index),
9254 });
9255 defer gop.deinit();
9256 if (gop == .existing) {
9257 extra.mutate.len = prev_extra_len;
9258
9259 const zir_body_inst_ptr = ip.funcDeclInfo(gop.existing).zirBodyInstPtr(ip);
9260 if (zir_body_inst_ptr.* != key.zir_body_inst) {
9261 // Since this function's `owner_nav` matches `key`, this *is* the function we're talking
9262 // about. The only way it could have a different ZIR `func` instruction is if the old
9263 // instruction has been lost and replaced with a new `TrackedInst.Index`.
9264 assert(zir_body_inst_ptr.resolve(ip) == null);
9265 zir_body_inst_ptr.* = key.zir_body_inst;
9266 }
9267
9268 return gop.existing;
9269 }
9270
9271 items.appendAssumeCapacity(.{
9272 .tag = .func_decl,
9273 .data = func_decl_extra_index,
9274 });
9275 return gop.put();
9276}
9277
9278pub const GetFuncDeclIesKey = struct {
9279 owner_nav: Nav.Index,
9280 param_types: []Index,
9281 noalias_bits: u32,
9282 comptime_bits: u32,
9283 bare_return_type: Index,
9284 /// null means generic.
9285 cc: ?std.lang.CallingConvention,
9286 is_var_args: bool,
9287 is_noinline: bool,
9288 zir_body_inst: TrackedInst.Index,
9289 lbrace_line: u32,
9290 rbrace_line: u32,
9291 lbrace_column: u32,
9292 rbrace_column: u32,
9293};
9294
9295pub fn getFuncDeclIes(
9296 ip: *InternPool,
9297 gpa: Allocator,
9298 io: Io,
9299 tid: Zcu.PerThread.Id,
9300 key: GetFuncDeclIesKey,
9301) Allocator.Error!Index {
9302 // Validate input parameters.
9303 assert(key.bare_return_type != .none);
9304 for (key.param_types) |param_type| assert(param_type != .none);
9305
9306 const local = ip.getLocal(tid);
9307 const items = local.getMutableItems(gpa, io);
9308 try items.ensureUnusedCapacity(4);
9309 const extra = local.getMutableExtra(gpa, io);
9310
9311 // The strategy here is to add the function decl unconditionally, then to
9312 // ask if it already exists, and if so, revert the lengths of the mutated
9313 // arrays. This is similar to what `getOrPutTrailingString` does.
9314 const prev_extra_len = extra.mutate.len;
9315 const params_len: u32 = @intCast(key.param_types.len);
9316
9317 try extra.ensureUnusedCapacity(@typeInfo(Tag.FuncDecl).@"struct".field_names.len +
9318 1 + // inferred_error_set
9319 @typeInfo(Tag.ErrorUnionType).@"struct".field_names.len +
9320 @typeInfo(Tag.TypeFunction).@"struct".field_names.len +
9321 @intFromBool(key.comptime_bits != 0) +
9322 @intFromBool(key.noalias_bits != 0) +
9323 params_len);
9324
9325 const func_index = Index.Unwrapped.wrap(.{
9326 .tid = tid,
9327 .index = items.mutate.len + 0,
9328 }, ip);
9329 const error_union_type = Index.Unwrapped.wrap(.{
9330 .tid = tid,
9331 .index = items.mutate.len + 1,
9332 }, ip);
9333 const error_set_type = Index.Unwrapped.wrap(.{
9334 .tid = tid,
9335 .index = items.mutate.len + 2,
9336 }, ip);
9337 const func_ty = Index.Unwrapped.wrap(.{
9338 .tid = tid,
9339 .index = items.mutate.len + 3,
9340 }, ip);
9341
9342 const func_decl_extra_index = addExtraAssumeCapacity(extra, Tag.FuncDecl{
9343 .analysis = .{
9344 .want_runtime_analysis = false,
9345 .branch_hint = .none,
9346 .is_noinline = key.is_noinline,
9347 .has_error_trace = false,
9348 .inferred_error_set = true,
9349 .disable_instrumentation = false,
9350 .disable_intrinsics = false,
9351 },
9352 .owner_nav = key.owner_nav,
9353 .ty = func_ty,
9354 .zir_body_inst = key.zir_body_inst,
9355 .lbrace_line = key.lbrace_line,
9356 .rbrace_line = key.rbrace_line,
9357 .lbrace_column = key.lbrace_column,
9358 .rbrace_column = key.rbrace_column,
9359 });
9360 extra.appendAssumeCapacity(.{@backingInt(Index.none)});
9361
9362 const func_type_extra_index = addExtraAssumeCapacity(extra, Tag.TypeFunction{
9363 .params_len = params_len,
9364 .return_type = error_union_type,
9365 .flags = .{
9366 .cc = .pack(key.cc orelse .auto),
9367 .is_var_args = key.is_var_args,
9368 .has_comptime_bits = key.comptime_bits != 0,
9369 .has_noalias_bits = key.noalias_bits != 0,
9370 .is_noinline = key.is_noinline,
9371 },
9372 });
9373 if (key.comptime_bits != 0) extra.appendAssumeCapacity(.{key.comptime_bits});
9374 if (key.noalias_bits != 0) extra.appendAssumeCapacity(.{key.noalias_bits});
9375 extra.appendSliceAssumeCapacity(.{@ptrCast(key.param_types)});
9376
9377 items.appendSliceAssumeCapacity(.{
9378 .tag = &.{
9379 .func_decl,
9380 .type_error_union,
9381 .type_inferred_error_set,
9382 .type_function,
9383 },
9384 .data = &.{
9385 func_decl_extra_index,
9386 addExtraAssumeCapacity(extra, Tag.ErrorUnionType{
9387 .error_set_type = error_set_type,
9388 .payload_type = key.bare_return_type,
9389 }),
9390 @backingInt(func_index),
9391 func_type_extra_index,
9392 },
9393 });
9394 errdefer {
9395 items.mutate.len -= 4;
9396 extra.mutate.len = prev_extra_len;
9397 }
9398
9399 var func_gop = try ip.getOrPutKeyEnsuringAdditionalCapacity(gpa, io, tid, .{
9400 .func = extraFuncDecl(tid, extra.list.*, func_decl_extra_index),
9401 }, 3);
9402 defer func_gop.deinit();
9403 if (func_gop == .existing) {
9404 // An existing function type was found; undo the additions to our two arrays.
9405 items.mutate.len -= 4;
9406 extra.mutate.len = prev_extra_len;
9407
9408 const zir_body_inst_ptr = ip.funcDeclInfo(func_gop.existing).zirBodyInstPtr(ip);
9409 if (zir_body_inst_ptr.* != key.zir_body_inst) {
9410 // Since this function's `owner_nav` matches `key`, this *is* the function we're talking
9411 // about. The only way it could have a different ZIR `func` instruction is if the old
9412 // instruction has been lost and replaced with a new `TrackedInst.Index`.
9413 assert(zir_body_inst_ptr.resolve(ip) == null);
9414 zir_body_inst_ptr.* = key.zir_body_inst;
9415 }
9416
9417 return func_gop.existing;
9418 }
9419 func_gop.putTentative(func_index);
9420 var error_union_type_gop = try ip.getOrPutKeyEnsuringAdditionalCapacity(gpa, io, tid, .{ .error_union_type = .{
9421 .error_set_type = error_set_type,
9422 .payload_type = key.bare_return_type,
9423 } }, 2);
9424 defer error_union_type_gop.deinit();
9425 error_union_type_gop.putTentative(error_union_type);
9426 var error_set_type_gop = try ip.getOrPutKeyEnsuringAdditionalCapacity(gpa, io, tid, .{
9427 .inferred_error_set_type = func_index,
9428 }, 1);
9429 defer error_set_type_gop.deinit();
9430 error_set_type_gop.putTentative(error_set_type);
9431 var func_ty_gop = try ip.getOrPutKey(gpa, io, tid, .{
9432 .func_type = extraFuncType(tid, extra.list.*, func_type_extra_index),
9433 });
9434 defer func_ty_gop.deinit();
9435 func_ty_gop.putTentative(func_ty);
9436
9437 func_gop.putFinal(func_index);
9438 error_union_type_gop.putFinal(error_union_type);
9439 error_set_type_gop.putFinal(error_set_type);
9440 func_ty_gop.putFinal(func_ty);
9441 return func_index;
9442}
9443
9444pub fn getErrorSetType(
9445 ip: *InternPool,
9446 gpa: Allocator,
9447 io: Io,
9448 tid: Zcu.PerThread.Id,
9449 names: []const NullTerminatedString,
9450) Allocator.Error!Index {
9451 assert(std.sort.isSorted(NullTerminatedString, names, {}, NullTerminatedString.indexLessThan));
9452
9453 const local = ip.getLocal(tid);
9454 const items = local.getMutableItems(gpa, io);
9455 const extra = local.getMutableExtra(gpa, io);
9456 try extra.ensureUnusedCapacity(@typeInfo(Tag.ErrorSet).@"struct".field_names.len + names.len);
9457
9458 const names_map = try ip.addMap(gpa, io, tid, names.len);
9459 errdefer local.mutate.maps.len -= 1;
9460
9461 // The strategy here is to add the type unconditionally, then to ask if it
9462 // already exists, and if so, revert the lengths of the mutated arrays.
9463 // This is similar to what `getOrPutTrailingString` does.
9464 const prev_extra_len = extra.mutate.len;
9465 errdefer extra.mutate.len = prev_extra_len;
9466
9467 const error_set_extra_index = addExtraAssumeCapacity(extra, Tag.ErrorSet{
9468 .names_len = @intCast(names.len),
9469 .names_map = names_map,
9470 });
9471 extra.appendSliceAssumeCapacity(.{@ptrCast(names)});
9472 errdefer extra.mutate.len = prev_extra_len;
9473
9474 var gop = try ip.getOrPutKey(gpa, io, tid, .{
9475 .error_set_type = extraErrorSet(tid, extra.list.*, error_set_extra_index),
9476 });
9477 defer gop.deinit();
9478 if (gop == .existing) {
9479 extra.mutate.len = prev_extra_len;
9480 return gop.existing;
9481 }
9482
9483 try items.append(.{
9484 .tag = .type_error_set,
9485 .data = error_set_extra_index,
9486 });
9487 errdefer items.mutate.len -= 1;
9488
9489 ip.addStringsToMap(names_map, names);
9490
9491 return gop.put();
9492}
9493
9494pub const GetFuncInstanceKey = struct {
9495 /// Has the length of the instance function (may be lesser than
9496 /// comptime_args).
9497 param_types: []Index,
9498 /// Has the length of generic_owner's parameters (may be greater than
9499 /// param_types).
9500 comptime_args: []const Index,
9501 noalias_bits: u32,
9502 bare_return_type: Index,
9503 is_noinline: bool,
9504 generic_owner: Index,
9505 inferred_error_set: bool,
9506};
9507
9508pub fn getFuncInstance(
9509 ip: *InternPool,
9510 gpa: Allocator,
9511 io: Io,
9512 tid: Zcu.PerThread.Id,
9513 arg: GetFuncInstanceKey,
9514) Allocator.Error!Index {
9515 if (arg.inferred_error_set)
9516 return getFuncInstanceIes(ip, gpa, io, tid, arg);
9517
9518 const generic_owner = unwrapCoercedFunc(ip, arg.generic_owner);
9519 const generic_owner_ty = ip.indexToKey(ip.funcDeclInfo(generic_owner).ty).func_type;
9520
9521 const func_ty = try ip.getFuncType(gpa, io, tid, .{
9522 .param_types = arg.param_types,
9523 .return_type = arg.bare_return_type,
9524 .noalias_bits = arg.noalias_bits,
9525 .cc = generic_owner_ty.cc,
9526 .is_noinline = arg.is_noinline,
9527 });
9528
9529 const local = ip.getLocal(tid);
9530 const items = local.getMutableItems(gpa, io);
9531 const extra = local.getMutableExtra(gpa, io);
9532 try extra.ensureUnusedCapacity(@typeInfo(Tag.FuncInstance).@"struct".field_names.len +
9533 arg.comptime_args.len);
9534
9535 assert(arg.comptime_args.len == ip.funcTypeParamsLen(ip.typeOf(generic_owner)));
9536
9537 const prev_extra_len = extra.mutate.len;
9538 errdefer extra.mutate.len = prev_extra_len;
9539
9540 const func_extra_index = addExtraAssumeCapacity(extra, Tag.FuncInstance{
9541 .analysis = .{
9542 .want_runtime_analysis = false,
9543 .branch_hint = .none,
9544 .is_noinline = arg.is_noinline,
9545 .has_error_trace = false,
9546 .inferred_error_set = false,
9547 .disable_instrumentation = false,
9548 .disable_intrinsics = false,
9549 },
9550 // This is populated after we create the Nav below. It is not read
9551 // by equality or hashing functions.
9552 .owner_nav = undefined,
9553 .ty = func_ty,
9554 .branch_quota = 0,
9555 .generic_owner = generic_owner,
9556 });
9557 extra.appendSliceAssumeCapacity(.{@ptrCast(arg.comptime_args)});
9558
9559 var gop = try ip.getOrPutKey(gpa, io, tid, .{
9560 .func = ip.extraFuncInstance(tid, extra.list.*, func_extra_index),
9561 });
9562 defer gop.deinit();
9563 if (gop == .existing) {
9564 extra.mutate.len = prev_extra_len;
9565 return gop.existing;
9566 }
9567
9568 const func_index = Index.Unwrapped.wrap(.{ .tid = tid, .index = items.mutate.len }, ip);
9569 try items.append(.{
9570 .tag = .func_instance,
9571 .data = func_extra_index,
9572 });
9573 errdefer items.mutate.len -= 1;
9574 try finishFuncInstance(
9575 ip,
9576 gpa,
9577 io,
9578 tid,
9579 extra,
9580 generic_owner,
9581 func_index,
9582 func_extra_index,
9583 );
9584 return gop.put();
9585}
9586
9587/// This function exists separately than `getFuncInstance` because it needs to
9588/// create 4 new items in the InternPool atomically before it can look for an
9589/// existing item in the map.
9590fn getFuncInstanceIes(
9591 ip: *InternPool,
9592 gpa: Allocator,
9593 io: Io,
9594 tid: Zcu.PerThread.Id,
9595 arg: GetFuncInstanceKey,
9596) Allocator.Error!Index {
9597 // Validate input parameters.
9598 assert(arg.inferred_error_set);
9599 assert(arg.bare_return_type != .none);
9600 for (arg.param_types) |param_type| assert(param_type != .none);
9601
9602 const local = ip.getLocal(tid);
9603 const items = local.getMutableItems(gpa, io);
9604 const extra = local.getMutableExtra(gpa, io);
9605 try items.ensureUnusedCapacity(4);
9606
9607 const generic_owner = unwrapCoercedFunc(ip, arg.generic_owner);
9608 const generic_owner_ty = ip.indexToKey(ip.funcDeclInfo(generic_owner).ty).func_type;
9609
9610 // The strategy here is to add the function decl unconditionally, then to
9611 // ask if it already exists, and if so, revert the lengths of the mutated
9612 // arrays. This is similar to what `getOrPutTrailingString` does.
9613 const prev_extra_len = extra.mutate.len;
9614 const params_len: u32 = @intCast(arg.param_types.len);
9615
9616 try extra.ensureUnusedCapacity(@typeInfo(Tag.FuncInstance).@"struct".field_names.len +
9617 1 + // inferred_error_set
9618 arg.comptime_args.len +
9619 @typeInfo(Tag.ErrorUnionType).@"struct".field_names.len +
9620 @typeInfo(Tag.TypeFunction).@"struct".field_names.len +
9621 @intFromBool(arg.noalias_bits != 0) +
9622 params_len);
9623
9624 const func_index = Index.Unwrapped.wrap(.{
9625 .tid = tid,
9626 .index = items.mutate.len + 0,
9627 }, ip);
9628 const error_union_type = Index.Unwrapped.wrap(.{
9629 .tid = tid,
9630 .index = items.mutate.len + 1,
9631 }, ip);
9632 const error_set_type = Index.Unwrapped.wrap(.{
9633 .tid = tid,
9634 .index = items.mutate.len + 2,
9635 }, ip);
9636 const func_ty = Index.Unwrapped.wrap(.{
9637 .tid = tid,
9638 .index = items.mutate.len + 3,
9639 }, ip);
9640
9641 const func_extra_index = addExtraAssumeCapacity(extra, Tag.FuncInstance{
9642 .analysis = .{
9643 .want_runtime_analysis = false,
9644 .branch_hint = .none,
9645 .is_noinline = arg.is_noinline,
9646 .has_error_trace = false,
9647 .inferred_error_set = true,
9648 .disable_instrumentation = false,
9649 .disable_intrinsics = false,
9650 },
9651 // This is populated after we create the Nav below. It is not read
9652 // by equality or hashing functions.
9653 .owner_nav = undefined,
9654 .ty = func_ty,
9655 .branch_quota = 0,
9656 .generic_owner = generic_owner,
9657 });
9658 extra.appendAssumeCapacity(.{@backingInt(Index.none)}); // resolved error set
9659 extra.appendSliceAssumeCapacity(.{@ptrCast(arg.comptime_args)});
9660
9661 const func_type_extra_index = addExtraAssumeCapacity(extra, Tag.TypeFunction{
9662 .params_len = params_len,
9663 .return_type = error_union_type,
9664 .flags = .{
9665 .cc = .pack(generic_owner_ty.cc),
9666 .is_var_args = false,
9667 .has_comptime_bits = false,
9668 .has_noalias_bits = arg.noalias_bits != 0,
9669 .is_noinline = arg.is_noinline,
9670 },
9671 });
9672 // no comptime_bits because has_comptime_bits is false
9673 if (arg.noalias_bits != 0) extra.appendAssumeCapacity(.{arg.noalias_bits});
9674 extra.appendSliceAssumeCapacity(.{@ptrCast(arg.param_types)});
9675
9676 items.appendSliceAssumeCapacity(.{
9677 .tag = &.{
9678 .func_instance,
9679 .type_error_union,
9680 .type_inferred_error_set,
9681 .type_function,
9682 },
9683 .data = &.{
9684 func_extra_index,
9685 addExtraAssumeCapacity(extra, Tag.ErrorUnionType{
9686 .error_set_type = error_set_type,
9687 .payload_type = arg.bare_return_type,
9688 }),
9689 @backingInt(func_index),
9690 func_type_extra_index,
9691 },
9692 });
9693 errdefer {
9694 items.mutate.len -= 4;
9695 extra.mutate.len = prev_extra_len;
9696 }
9697
9698 var func_gop = try ip.getOrPutKeyEnsuringAdditionalCapacity(gpa, io, tid, .{
9699 .func = ip.extraFuncInstance(tid, extra.list.*, func_extra_index),
9700 }, 3);
9701 defer func_gop.deinit();
9702 if (func_gop == .existing) {
9703 // Hot path: undo the additions to our two arrays.
9704 items.mutate.len -= 4;
9705 extra.mutate.len = prev_extra_len;
9706 return func_gop.existing;
9707 }
9708 func_gop.putTentative(func_index);
9709 var error_union_type_gop = try ip.getOrPutKeyEnsuringAdditionalCapacity(gpa, io, tid, .{ .error_union_type = .{
9710 .error_set_type = error_set_type,
9711 .payload_type = arg.bare_return_type,
9712 } }, 2);
9713 defer error_union_type_gop.deinit();
9714 error_union_type_gop.putTentative(error_union_type);
9715 var error_set_type_gop = try ip.getOrPutKeyEnsuringAdditionalCapacity(gpa, io, tid, .{
9716 .inferred_error_set_type = func_index,
9717 }, 1);
9718 defer error_set_type_gop.deinit();
9719 error_set_type_gop.putTentative(error_set_type);
9720 var func_ty_gop = try ip.getOrPutKey(gpa, io, tid, .{
9721 .func_type = extraFuncType(tid, extra.list.*, func_type_extra_index),
9722 });
9723 defer func_ty_gop.deinit();
9724 func_ty_gop.putTentative(func_ty);
9725 try finishFuncInstance(
9726 ip,
9727 gpa,
9728 io,
9729 tid,
9730 extra,
9731 generic_owner,
9732 func_index,
9733 func_extra_index,
9734 );
9735
9736 func_gop.putFinal(func_index);
9737 error_union_type_gop.putFinal(error_union_type);
9738 error_set_type_gop.putFinal(error_set_type);
9739 func_ty_gop.putFinal(func_ty);
9740 return func_index;
9741}
9742
9743fn finishFuncInstance(
9744 ip: *InternPool,
9745 gpa: Allocator,
9746 io: Io,
9747 tid: Zcu.PerThread.Id,
9748 extra: Local.Extra.Mutable,
9749 generic_owner: Index,
9750 func_index: Index,
9751 func_extra_index: u32,
9752) Allocator.Error!void {
9753 const fn_owner_nav = ip.getNav(ip.funcDeclInfo(generic_owner).owner_nav);
9754 const fn_namespace = fn_owner_nav.analysis.?.namespace;
9755
9756 // TODO: improve this name
9757 const nav_name = try ip.getOrPutStringFmt(gpa, io, tid, "{f}__anon_{d}", .{
9758 fn_owner_nav.name.fmt(ip), @backingInt(func_index),
9759 }, .no_embedded_nulls);
9760 const nav_fqn = try ip.namespacePtr(fn_namespace).internFullyQualifiedName(ip, gpa, io, tid, nav_name);
9761 const nav_index = try ip.createNav(gpa, io, tid, nav_name, nav_fqn, .{
9762 .type = ip.typeOf(func_index),
9763 .@"align" = fn_owner_nav.resolved.?.@"align",
9764 .@"linksection" = fn_owner_nav.resolved.?.@"linksection",
9765 .@"addrspace" = fn_owner_nav.resolved.?.@"addrspace",
9766 .@"const" = true,
9767 .@"threadlocal" = false,
9768 .is_extern_decl = false,
9769 .value = func_index,
9770 });
9771
9772 // Populate the owner_nav field which was left undefined until now.
9773 extra.view().items(.@"0")[
9774 func_extra_index + std.meta.fieldIndex(Tag.FuncInstance, "owner_nav").?
9775 ] = @backingInt(nav_index);
9776}
9777
9778pub fn getIfExists(ip: *const InternPool, key: Key) ?Index {
9779 const full_hash = key.hash64(ip);
9780 const hash: u32 = @truncate(full_hash >> 32);
9781 const shard = &ip.shards[@intCast(full_hash & (ip.shards.len - 1))];
9782 const map = shard.shared.map.acquire();
9783 const map_mask = map.header().mask();
9784 var map_index = hash;
9785 while (true) : (map_index += 1) {
9786 map_index &= map_mask;
9787 const entry = &map.entries[map_index];
9788 const index = entry.acquire();
9789 if (index == .none) return null;
9790 if (entry.hash != hash) continue;
9791 if (ip.indexToKey(index).eql(key, ip)) return index;
9792 }
9793}
9794
9795fn addStringsToMap(
9796 ip: *InternPool,
9797 map_index: MapIndex,
9798 strings: []const NullTerminatedString,
9799) void {
9800 const map = map_index.get(ip);
9801 const adapter: NullTerminatedString.Adapter = .{ .strings = strings };
9802 for (strings) |string| {
9803 const gop = map.getOrPutAssumeCapacityAdapted(string, adapter);
9804 assert(!gop.found_existing);
9805 }
9806}
9807
9808fn addMap(ip: *InternPool, gpa: Allocator, io: Io, tid: Zcu.PerThread.Id, cap: usize) Allocator.Error!MapIndex {
9809 const maps = ip.getLocal(tid).getMutableMaps(gpa, io);
9810 const unwrapped: MapIndex.Unwrapped = .{ .tid = tid, .index = maps.mutate.len };
9811 const ptr = try maps.addOne();
9812 errdefer maps.mutate.len = unwrapped.index;
9813 ptr[0].* = .{};
9814 try ptr[0].ensureTotalCapacity(gpa, cap);
9815 return unwrapped.wrap(ip);
9816}
9817
9818/// This operation only happens under compile error conditions.
9819/// Leak the index until the next garbage collection.
9820/// Invalidates all references to this index.
9821pub fn remove(ip: *InternPool, tid: Zcu.PerThread.Id, index: Index) void {
9822 const unwrapped_index = index.unwrap(ip);
9823
9824 if (unwrapped_index.tid == tid) {
9825 const items_len = &ip.getLocal(unwrapped_index.tid).mutate.items.len;
9826 if (unwrapped_index.index == items_len.* - 1) {
9827 // Happy case - we can just drop the item without affecting any other indices.
9828 items_len.* -= 1;
9829 return;
9830 }
9831 }
9832
9833 // We must preserve the item so that indices following it remain valid.
9834 // Thus, we will rewrite the tag to `removed`, leaking the item until
9835 // next GC but causing `KeyAdapter` to ignore it.
9836 const items = ip.getLocalShared(unwrapped_index.tid).items.acquire().view();
9837 @atomicStore(Tag, &items.items(.tag)[unwrapped_index.index], .removed, .unordered);
9838}
9839
9840fn addInt(
9841 ip: *InternPool,
9842 gpa: Allocator,
9843 io: Io,
9844 tid: Zcu.PerThread.Id,
9845 ty: Index,
9846 tag: Tag,
9847 limbs: []const Limb,
9848) !void {
9849 const local = ip.getLocal(tid);
9850 const items_list = local.getMutableItems(gpa, io);
9851 const limbs_list = local.getMutableLimbs(gpa, io);
9852 const limbs_len: u32 = @intCast(limbs.len);
9853 try limbs_list.ensureUnusedCapacity(Int.limbs_items_len + limbs_len);
9854 items_list.appendAssumeCapacity(.{
9855 .tag = tag,
9856 .data = limbs_list.mutate.len,
9857 });
9858 limbs_list.addManyAsArrayAssumeCapacity(Int.limbs_items_len)[0].* = @bitCast(Int{
9859 .ty = ty,
9860 .limbs_len = limbs_len,
9861 });
9862 limbs_list.appendSliceAssumeCapacity(.{limbs});
9863}
9864
9865fn addExtra(extra: Local.Extra.Mutable, item: anytype) Allocator.Error!u32 {
9866 const field_count = @typeInfo(@TypeOf(item)).@"struct".field_names.len;
9867 try extra.ensureUnusedCapacity(field_count);
9868 return addExtraAssumeCapacity(extra, item);
9869}
9870
9871fn addExtraAssumeCapacity(extra: Local.Extra.Mutable, item: anytype) u32 {
9872 const result: u32 = extra.mutate.len;
9873 const info = @typeInfo(@TypeOf(item)).@"struct";
9874 inline for (info.field_types, info.field_names) |field_type, field_name| {
9875 extra.appendAssumeCapacity(.{switch (field_type) {
9876 Index,
9877 Nav.Index,
9878 Nav.Index.Optional,
9879 NamespaceIndex,
9880 OptionalNamespaceIndex,
9881 MapIndex,
9882 OptionalMapIndex,
9883 String,
9884 NullTerminatedString,
9885 OptionalNullTerminatedString,
9886 Tag.TypePointer.VectorIndex,
9887 TrackedInst.Index,
9888 TrackedInst.Index.Optional,
9889 ComptimeAllocIndex,
9890 => @backingInt(@field(item, field_name)),
9891
9892 u32,
9893 i32,
9894 FuncAnalysis,
9895 Tag.Extern.Flags,
9896 Tag.TypePointer.Flags,
9897 Tag.TypeFunction.Flags,
9898 Tag.TypePointer.PackedOffset,
9899 Tag.TypeUnion.Flags,
9900 Tag.TypeStruct.Flags,
9901 Tag.TypeStructPacked.Bits,
9902 Tag.TypeUnionPacked.Bits,
9903 Tag.TypeEnum.Bits,
9904 Tag.TypeSpirv.Flags,
9905 => @bitCast(@field(item, field_name)),
9906
9907 else => @compileError("bad field type: " ++ @typeName(field_type)),
9908 }});
9909 }
9910 return result;
9911}
9912
9913fn addLimbsExtraAssumeCapacity(ip: *InternPool, extra: anytype) u32 {
9914 switch (@sizeOf(Limb)) {
9915 @sizeOf(u32) => return addExtraAssumeCapacity(ip, extra),
9916 @sizeOf(u64) => {},
9917 else => @compileError("unsupported host"),
9918 }
9919 const result: u32 = @intCast(ip.limbs.items.len);
9920 const info = @typeInfo(@TypeOf(extra)).@"struct";
9921 inline for (info.field_names, info.field_types, 0..) |field_name, field_type, i| {
9922 const new: u32 = switch (field_type) {
9923 u32 => @field(extra, field_name),
9924 Index => @backingInt(@field(extra, field_name)),
9925 else => @compileError("bad field type: " ++ @typeName(field_type)),
9926 };
9927 if (i % 2 == 0) {
9928 ip.limbs.appendAssumeCapacity(new);
9929 } else {
9930 ip.limbs.items[ip.limbs.items.len - 1] |= @as(u64, new) << 32;
9931 }
9932 }
9933 return result;
9934}
9935
9936fn extraDataTrail(extra: Local.Extra, comptime T: type, index: u32) struct { data: T, end: u32 } {
9937 const extra_items = extra.view().items(.@"0");
9938 var result: T = undefined;
9939 const field_names = @typeInfo(T).@"struct".field_names;
9940 const field_types = @typeInfo(T).@"struct".field_types;
9941 inline for (field_names, field_types, index..) |field_name, field_type, extra_index| {
9942 const extra_item = extra_items[extra_index];
9943 @field(result, field_name) = switch (field_type) {
9944 Index,
9945 Nav.Index,
9946 Nav.Index.Optional,
9947 NamespaceIndex,
9948 OptionalNamespaceIndex,
9949 MapIndex,
9950 OptionalMapIndex,
9951 String,
9952 NullTerminatedString,
9953 OptionalNullTerminatedString,
9954 Tag.TypePointer.VectorIndex,
9955 TrackedInst.Index,
9956 TrackedInst.Index.Optional,
9957 ComptimeAllocIndex,
9958 => @fromBackingInt(@intCast(extra_item)),
9959
9960 u32,
9961 i32,
9962 Tag.Extern.Flags,
9963 Tag.TypePointer.Flags,
9964 Tag.TypeFunction.Flags,
9965 Tag.TypePointer.PackedOffset,
9966 Tag.TypeUnion.Flags,
9967 Tag.TypeStruct.Flags,
9968 FuncAnalysis,
9969 Tag.TypeStructPacked.Bits,
9970 Tag.TypeUnionPacked.Bits,
9971 Tag.TypeEnum.Bits,
9972 Tag.TypeSpirv.Flags,
9973 => @bitCast(extra_item),
9974
9975 else => @compileError("bad field type: " ++ @typeName(field_type)),
9976 };
9977 }
9978 return .{
9979 .data = result,
9980 .end = @intCast(index + field_names.len),
9981 };
9982}
9983
9984fn extraData(extra: Local.Extra, comptime T: type, index: u32) T {
9985 return extraDataTrail(extra, T, index).data;
9986}
9987
9988test "basic usage" {
9989 const gpa = std.testing.allocator;
9990 const io = std.testing.io;
9991
9992 var ip: InternPool = .empty;
9993 try ip.init(gpa, io, 1);
9994 defer ip.deinit(gpa, io);
9995
9996 const i32_type = try ip.get(gpa, io, .main, .{ .int_type = .{
9997 .signedness = .signed,
9998 .bits = 32,
9999 } });
10000 const array_i32 = try ip.get(gpa, io, .main, .{ .array_type = .{
10001 .len = 10,
10002 .child = i32_type,
10003 .sentinel = .none,
10004 } });
10005
10006 const another_i32_type = try ip.get(gpa, io, .main, .{ .int_type = .{
10007 .signedness = .signed,
10008 .bits = 32,
10009 } });
10010 try std.testing.expect(another_i32_type == i32_type);
10011
10012 const another_array_i32 = try ip.get(gpa, io, .main, .{ .array_type = .{
10013 .len = 10,
10014 .child = i32_type,
10015 .sentinel = .none,
10016 } });
10017 try std.testing.expect(another_array_i32 == array_i32);
10018}
10019
10020pub fn childType(ip: *const InternPool, i: Index) Index {
10021 return switch (ip.indexToKey(i)) {
10022 .ptr_type => |ptr_type| ptr_type.child,
10023 .vector_type => |vector_type| vector_type.child,
10024 .array_type => |array_type| array_type.child,
10025 .opt_type, .anyframe_type => |child| child,
10026 .spirv_type => blk: {
10027 const info = ip.loadSpirvType(i);
10028 assert(info.flags.tag == .runtime_array);
10029 break :blk info.ty;
10030 },
10031 else => unreachable,
10032 };
10033}
10034
10035/// Given a slice type, returns the type of the ptr field.
10036pub fn slicePtrType(ip: *const InternPool, index: Index) Index {
10037 switch (index) {
10038 .slice_const_u8_type => return .manyptr_const_u8_type,
10039 .slice_const_u8_sentinel_0_type => return .manyptr_const_u8_sentinel_0_type,
10040 .slice_const_slice_const_u8_type => return .manyptr_const_slice_const_u8_type,
10041 .slice_const_type_type => return .manyptr_const_type_type,
10042 else => {},
10043 }
10044 const item = index.unwrap(ip).getItem(ip);
10045 switch (item.tag) {
10046 .type_slice => return @fromBackingInt(@intCast(item.data)),
10047 else => unreachable, // not a slice type
10048 }
10049}
10050
10051/// Given a slice value, returns the value of the ptr field.
10052pub fn slicePtr(ip: *const InternPool, index: Index) Index {
10053 const unwrapped_index = index.unwrap(ip);
10054 const item = unwrapped_index.getItem(ip);
10055 switch (item.tag) {
10056 .ptr_slice => return extraData(unwrapped_index.getExtra(ip), PtrSlice, item.data).ptr,
10057 else => unreachable, // not a slice value
10058 }
10059}
10060
10061/// Given a slice value, returns the value of the len field.
10062pub fn sliceLen(ip: *const InternPool, index: Index) Index {
10063 const unwrapped_index = index.unwrap(ip);
10064 const item = unwrapped_index.getItem(ip);
10065 switch (item.tag) {
10066 .ptr_slice => return extraData(unwrapped_index.getExtra(ip), PtrSlice, item.data).len,
10067 else => unreachable, // not a slice value
10068 }
10069}
10070
10071/// Given an existing value, returns the same value but with the supplied type.
10072/// Only some combinations are allowed:
10073/// * identity coercion
10074/// * undef => any
10075/// * int <=> int
10076/// * int <=> enum
10077/// * enum_literal => enum
10078/// * float <=> float
10079/// * ptr <=> ptr
10080/// * opt ptr <=> ptr
10081/// * opt ptr <=> opt ptr
10082/// * int <=> ptr
10083/// * null_value => opt
10084/// * payload => opt
10085/// * error set <=> error set
10086/// * error union <=> error union
10087/// * error set => error union
10088/// * payload => error union
10089/// * fn <=> fn
10090/// * aggregate <=> aggregate (where children can also be coerced)
10091pub fn getCoerced(
10092 ip: *InternPool,
10093 gpa: Allocator,
10094 io: Io,
10095 tid: Zcu.PerThread.Id,
10096 val: Index,
10097 new_ty: Index,
10098) Allocator.Error!Index {
10099 const old_ty = ip.typeOf(val);
10100 if (old_ty == new_ty) return val;
10101
10102 switch (val) {
10103 .undef => return ip.get(gpa, io, tid, .{ .undef = new_ty }),
10104 .null_value => {
10105 if (ip.isOptionalType(new_ty)) return ip.get(gpa, io, tid, .{ .opt = .{
10106 .ty = new_ty,
10107 .val = .none,
10108 } });
10109
10110 if (ip.isPointerType(new_ty)) switch (ip.indexToKey(new_ty).ptr_type.flags.size) {
10111 .one, .many, .c => return ip.get(gpa, io, tid, .{ .ptr = .{
10112 .ty = new_ty,
10113 .base_addr = .int,
10114 .byte_offset = 0,
10115 } }),
10116 .slice => return ip.get(gpa, io, tid, .{ .slice = .{
10117 .ty = new_ty,
10118 .ptr = try ip.get(gpa, io, tid, .{ .ptr = .{
10119 .ty = ip.slicePtrType(new_ty),
10120 .base_addr = .int,
10121 .byte_offset = 0,
10122 } }),
10123 .len = .undef_usize,
10124 } }),
10125 };
10126 },
10127 else => {
10128 const unwrapped_val = val.unwrap(ip);
10129 const val_item = unwrapped_val.getItem(ip);
10130 switch (val_item.tag) {
10131 .func_decl => return getCoercedFuncDecl(ip, gpa, io, tid, val, new_ty),
10132 .func_instance => return getCoercedFuncInstance(ip, gpa, io, tid, val, new_ty),
10133 .func_coerced => {
10134 const func: Index = @fromBackingInt(@intCast(unwrapped_val.getExtra(ip).view().items(.@"0")[
10135 val_item.data + std.meta.fieldIndex(Tag.FuncCoerced, "func").?
10136 ]));
10137 switch (func.unwrap(ip).getTag(ip)) {
10138 .func_decl => return getCoercedFuncDecl(ip, gpa, io, tid, func, new_ty),
10139 .func_instance => return getCoercedFuncInstance(ip, gpa, io, tid, func, new_ty),
10140 else => unreachable,
10141 }
10142 },
10143 else => {},
10144 }
10145 },
10146 }
10147
10148 switch (ip.indexToKey(val)) {
10149 .undef => return ip.get(gpa, io, tid, .{ .undef = new_ty }),
10150 .func => unreachable,
10151
10152 .int => |int| switch (ip.indexToKey(new_ty)) {
10153 .enum_type => return ip.get(gpa, io, tid, .{ .enum_tag = .{
10154 .ty = new_ty,
10155 .int = try ip.getCoerced(gpa, io, tid, val, ip.loadEnumType(new_ty).int_tag_type),
10156 } }),
10157 .ptr_type => switch (int.storage) {
10158 inline .u64, .i64 => |int_val| return ip.get(gpa, io, tid, .{ .ptr = .{
10159 .ty = new_ty,
10160 .base_addr = .int,
10161 .byte_offset = @intCast(int_val),
10162 } }),
10163 .big_int => unreachable, // must be a usize
10164 },
10165 else => if (ip.isIntegerType(new_ty))
10166 return ip.getCoercedInts(gpa, io, tid, int, new_ty),
10167 },
10168 .float => |float| switch (ip.indexToKey(new_ty)) {
10169 .simple_type => |simple| switch (simple) {
10170 .f16,
10171 .f32,
10172 .f64,
10173 .f80,
10174 .f128,
10175 .c_longdouble,
10176 .comptime_float,
10177 => return ip.get(gpa, io, tid, .{ .float = .{
10178 .ty = new_ty,
10179 .storage = float.storage,
10180 } }),
10181 else => {},
10182 },
10183 else => {},
10184 },
10185 .enum_tag => |enum_tag| if (ip.isIntegerType(new_ty))
10186 return ip.getCoercedInts(gpa, io, tid, ip.indexToKey(enum_tag.int).int, new_ty),
10187 .enum_literal => |enum_literal| switch (ip.indexToKey(new_ty)) {
10188 .enum_type => {
10189 const enum_type = ip.loadEnumType(new_ty);
10190 const index = enum_type.nameIndex(ip, enum_literal).?;
10191 assert(enum_type.int_tag_type != .noreturn_type);
10192 return ip.get(gpa, io, tid, .{ .enum_tag = .{
10193 .ty = new_ty,
10194 .int = if (enum_type.field_values.len != 0)
10195 enum_type.field_values.get(ip)[index]
10196 else
10197 try ip.get(gpa, io, tid, .{ .int = .{
10198 .ty = enum_type.int_tag_type,
10199 .storage = .{ .u64 = index },
10200 } }),
10201 } });
10202 },
10203 else => {},
10204 },
10205 .slice => |slice| if (ip.isPointerType(new_ty) and ip.indexToKey(new_ty).ptr_type.flags.size == .slice)
10206 return ip.get(gpa, io, tid, .{ .slice = .{
10207 .ty = new_ty,
10208 .ptr = try ip.getCoerced(gpa, io, tid, slice.ptr, ip.slicePtrType(new_ty)),
10209 .len = slice.len,
10210 } })
10211 else if (ip.isIntegerType(new_ty))
10212 return ip.getCoerced(gpa, io, tid, slice.ptr, new_ty),
10213 .ptr => |ptr| if (ip.isPointerType(new_ty) and ip.indexToKey(new_ty).ptr_type.flags.size != .slice)
10214 return ip.get(gpa, io, tid, .{ .ptr = .{
10215 .ty = new_ty,
10216 .base_addr = ptr.base_addr,
10217 .byte_offset = ptr.byte_offset,
10218 } })
10219 else if (ip.isIntegerType(new_ty))
10220 switch (ptr.base_addr) {
10221 .int => return ip.get(gpa, io, tid, .{ .int = .{
10222 .ty = .usize_type,
10223 .storage = .{ .u64 = @intCast(ptr.byte_offset) },
10224 } }),
10225 else => {},
10226 },
10227 .opt => |opt| switch (ip.indexToKey(new_ty)) {
10228 .ptr_type => |ptr_type| return switch (opt.val) {
10229 .none => switch (ptr_type.flags.size) {
10230 .one, .many, .c => try ip.get(gpa, io, tid, .{ .ptr = .{
10231 .ty = new_ty,
10232 .base_addr = .int,
10233 .byte_offset = 0,
10234 } }),
10235 .slice => try ip.get(gpa, io, tid, .{ .slice = .{
10236 .ty = new_ty,
10237 .ptr = try ip.get(gpa, io, tid, .{ .ptr = .{
10238 .ty = ip.slicePtrType(new_ty),
10239 .base_addr = .int,
10240 .byte_offset = 0,
10241 } }),
10242 .len = .undef_usize,
10243 } }),
10244 },
10245 else => |payload| try ip.getCoerced(gpa, io, tid, payload, new_ty),
10246 },
10247 .opt_type => |child_type| return try ip.get(gpa, io, tid, .{ .opt = .{
10248 .ty = new_ty,
10249 .val = switch (opt.val) {
10250 .none => .none,
10251 else => try ip.getCoerced(gpa, io, tid, opt.val, child_type),
10252 },
10253 } }),
10254 else => {},
10255 },
10256 .err => |err| if (ip.isErrorSetType(new_ty))
10257 return ip.get(gpa, io, tid, .{ .err = .{
10258 .ty = new_ty,
10259 .name = err.name,
10260 } })
10261 else if (ip.isErrorUnionType(new_ty))
10262 return ip.get(gpa, io, tid, .{ .error_union = .{
10263 .ty = new_ty,
10264 .val = .{ .err_name = err.name },
10265 } }),
10266 .error_union => |error_union| if (ip.isErrorUnionType(new_ty))
10267 return ip.get(gpa, io, tid, .{ .error_union = .{
10268 .ty = new_ty,
10269 .val = error_union.val,
10270 } }),
10271 .aggregate => |aggregate| {
10272 const new_len: usize = @intCast(ip.aggregateTypeLen(new_ty));
10273 direct: {
10274 const old_ty_child = switch (ip.indexToKey(old_ty)) {
10275 inline .array_type, .vector_type => |seq_type| seq_type.child,
10276 .tuple_type, .struct_type => break :direct,
10277 else => unreachable,
10278 };
10279 const new_ty_child = switch (ip.indexToKey(new_ty)) {
10280 inline .array_type, .vector_type => |seq_type| seq_type.child,
10281 .tuple_type, .struct_type => break :direct,
10282 else => unreachable,
10283 };
10284 if (old_ty_child != new_ty_child) break :direct;
10285 switch (aggregate.storage) {
10286 .bytes => |bytes| return ip.get(gpa, io, tid, .{ .aggregate = .{
10287 .ty = new_ty,
10288 .storage = .{ .bytes = bytes },
10289 } }),
10290 .elems => |elems| {
10291 const elems_copy = try gpa.dupe(Index, elems[0..new_len]);
10292 defer gpa.free(elems_copy);
10293 return ip.get(gpa, io, tid, .{ .aggregate = .{
10294 .ty = new_ty,
10295 .storage = .{ .elems = elems_copy },
10296 } });
10297 },
10298 .repeated_elem => |elem| {
10299 return ip.get(gpa, io, tid, .{ .aggregate = .{
10300 .ty = new_ty,
10301 .storage = .{ .repeated_elem = elem },
10302 } });
10303 },
10304 }
10305 }
10306 // Direct approach failed - we must recursively coerce elems
10307 const agg_elems = try gpa.alloc(Index, new_len);
10308 defer gpa.free(agg_elems);
10309 // First, fill the vector with the uncoerced elements. We do this to avoid key
10310 // lifetime issues, since it'll allow us to avoid referencing `aggregate` after we
10311 // begin interning elems.
10312 switch (aggregate.storage) {
10313 .bytes => |bytes| {
10314 // We have to intern each value here, so unfortunately we can't easily avoid
10315 // the repeated indexToKey calls.
10316 for (agg_elems, 0..) |*elem, index| {
10317 elem.* = try ip.get(gpa, io, tid, .{ .int = .{
10318 .ty = .u8_type,
10319 .storage = .{ .u64 = bytes.at(index, ip) },
10320 } });
10321 }
10322 },
10323 .elems => |elems| @memcpy(agg_elems, elems[0..new_len]),
10324 .repeated_elem => |elem| @memset(agg_elems, elem),
10325 }
10326 // Now, coerce each element to its new type.
10327 for (agg_elems, 0..) |*elem, i| {
10328 const new_elem_ty = switch (ip.indexToKey(new_ty)) {
10329 inline .array_type, .vector_type => |seq_type| seq_type.child,
10330 .tuple_type => |tuple_type| tuple_type.types.get(ip)[i],
10331 .struct_type => ip.loadStructType(new_ty).field_types.get(ip)[i],
10332 else => unreachable,
10333 };
10334 elem.* = try ip.getCoerced(gpa, io, tid, elem.*, new_elem_ty);
10335 }
10336 return ip.get(gpa, io, tid, .{ .aggregate = .{ .ty = new_ty, .storage = .{ .elems = agg_elems } } });
10337 },
10338 else => {},
10339 }
10340
10341 switch (ip.indexToKey(new_ty)) {
10342 .opt_type => |child_type| switch (val) {
10343 .null_value => return ip.get(gpa, io, tid, .{ .opt = .{
10344 .ty = new_ty,
10345 .val = .none,
10346 } }),
10347 else => return ip.get(gpa, io, tid, .{ .opt = .{
10348 .ty = new_ty,
10349 .val = try ip.getCoerced(gpa, io, tid, val, child_type),
10350 } }),
10351 },
10352 .error_union_type => |error_union_type| return ip.get(gpa, io, tid, .{ .error_union = .{
10353 .ty = new_ty,
10354 .val = .{ .payload = try ip.getCoerced(gpa, io, tid, val, error_union_type.payload_type) },
10355 } }),
10356 else => {},
10357 }
10358 if (std.debug.runtime_safety) {
10359 std.debug.panic("InternPool.getCoerced of {s} not implemented from {s} to {s}", .{
10360 @tagName(ip.indexToKey(val)),
10361 @tagName(ip.indexToKey(old_ty)),
10362 @tagName(ip.indexToKey(new_ty)),
10363 });
10364 }
10365 unreachable;
10366}
10367
10368fn getCoercedFuncDecl(
10369 ip: *InternPool,
10370 gpa: Allocator,
10371 io: Io,
10372 tid: Zcu.PerThread.Id,
10373 val: Index,
10374 new_ty: Index,
10375) Allocator.Error!Index {
10376 const unwrapped_val = val.unwrap(ip);
10377 const prev_ty: Index = @fromBackingInt(@intCast(unwrapped_val.getExtra(ip).view().items(.@"0")[
10378 unwrapped_val.getData(ip) + std.meta.fieldIndex(Tag.FuncDecl, "ty").?
10379 ]));
10380 if (new_ty == prev_ty) return val;
10381 return getCoercedFunc(ip, gpa, io, tid, val, new_ty);
10382}
10383
10384fn getCoercedFuncInstance(
10385 ip: *InternPool,
10386 gpa: Allocator,
10387 io: Io,
10388 tid: Zcu.PerThread.Id,
10389 val: Index,
10390 new_ty: Index,
10391) Allocator.Error!Index {
10392 const unwrapped_val = val.unwrap(ip);
10393 const prev_ty: Index = @fromBackingInt(@intCast(unwrapped_val.getExtra(ip).view().items(.@"0")[
10394 unwrapped_val.getData(ip) + std.meta.fieldIndex(Tag.FuncInstance, "ty").?
10395 ]));
10396 if (new_ty == prev_ty) return val;
10397 return getCoercedFunc(ip, gpa, io, tid, val, new_ty);
10398}
10399
10400fn getCoercedFunc(
10401 ip: *InternPool,
10402 gpa: Allocator,
10403 io: Io,
10404 tid: Zcu.PerThread.Id,
10405 func: Index,
10406 ty: Index,
10407) Allocator.Error!Index {
10408 const local = ip.getLocal(tid);
10409 const items = local.getMutableItems(gpa, io);
10410 try items.ensureUnusedCapacity(1);
10411 const extra = local.getMutableExtra(gpa, io);
10412
10413 const prev_extra_len = extra.mutate.len;
10414 try extra.ensureUnusedCapacity(@typeInfo(Tag.FuncCoerced).@"struct".field_names.len);
10415
10416 const extra_index = addExtraAssumeCapacity(extra, Tag.FuncCoerced{
10417 .ty = ty,
10418 .func = func,
10419 });
10420 errdefer extra.mutate.len = prev_extra_len;
10421
10422 var gop = try ip.getOrPutKey(gpa, io, tid, .{
10423 .func = ip.extraFuncCoerced(extra.list.*, extra_index),
10424 });
10425 defer gop.deinit();
10426 if (gop == .existing) {
10427 extra.mutate.len = prev_extra_len;
10428 return gop.existing;
10429 }
10430
10431 items.appendAssumeCapacity(.{
10432 .tag = .func_coerced,
10433 .data = extra_index,
10434 });
10435 return gop.put();
10436}
10437
10438/// Asserts `val` has an integer type.
10439/// Assumes `new_ty` is an integer type.
10440pub fn getCoercedInts(
10441 ip: *InternPool,
10442 gpa: Allocator,
10443 io: Io,
10444 tid: Zcu.PerThread.Id,
10445 int: Key.Int,
10446 new_ty: Index,
10447) Allocator.Error!Index {
10448 return ip.get(gpa, io, tid, .{ .int = .{
10449 .ty = new_ty,
10450 .storage = int.storage,
10451 } });
10452}
10453
10454pub fn indexToFuncType(ip: *const InternPool, val: Index) ?Key.FuncType {
10455 const unwrapped_val = val.unwrap(ip);
10456 const item = unwrapped_val.getItem(ip);
10457 switch (item.tag) {
10458 .type_function => return extraFuncType(unwrapped_val.tid, unwrapped_val.getExtra(ip), item.data),
10459 else => return null,
10460 }
10461}
10462
10463/// includes .comptime_int_type
10464pub fn isIntegerType(ip: *const InternPool, ty: Index) bool {
10465 return switch (ty) {
10466 .usize_type,
10467 .isize_type,
10468 .c_char_type,
10469 .c_short_type,
10470 .c_ushort_type,
10471 .c_int_type,
10472 .c_uint_type,
10473 .c_long_type,
10474 .c_ulong_type,
10475 .c_longlong_type,
10476 .c_ulonglong_type,
10477 .comptime_int_type,
10478 => true,
10479 else => switch (ty.unwrap(ip).getTag(ip)) {
10480 .type_int_signed,
10481 .type_int_unsigned,
10482 => true,
10483 else => false,
10484 },
10485 };
10486}
10487
10488/// does not include .enum_literal_type
10489pub fn isEnumType(ip: *const InternPool, ty: Index) bool {
10490 return ip.indexToKey(ty) == .enum_type;
10491}
10492
10493pub fn isUnion(ip: *const InternPool, ty: Index) bool {
10494 return ip.indexToKey(ty) == .union_type;
10495}
10496
10497pub fn isFunctionType(ip: *const InternPool, ty: Index) bool {
10498 return ip.indexToKey(ty) == .func_type;
10499}
10500
10501pub fn isPointerType(ip: *const InternPool, ty: Index) bool {
10502 return ip.indexToKey(ty) == .ptr_type;
10503}
10504
10505pub fn isOptionalType(ip: *const InternPool, ty: Index) bool {
10506 return ip.indexToKey(ty) == .opt_type;
10507}
10508
10509/// includes .inferred_error_set_type
10510pub fn isErrorSetType(ip: *const InternPool, ty: Index) bool {
10511 return switch (ty) {
10512 .anyerror_type, .adhoc_inferred_error_set_type => true,
10513 else => switch (ip.indexToKey(ty)) {
10514 .error_set_type, .inferred_error_set_type => true,
10515 else => false,
10516 },
10517 };
10518}
10519
10520pub fn isInferredErrorSetType(ip: *const InternPool, ty: Index) bool {
10521 return ty == .adhoc_inferred_error_set_type or ip.indexToKey(ty) == .inferred_error_set_type;
10522}
10523
10524pub fn isErrorUnionType(ip: *const InternPool, ty: Index) bool {
10525 return ip.indexToKey(ty) == .error_union_type;
10526}
10527
10528pub fn isAggregateType(ip: *const InternPool, ty: Index) bool {
10529 return switch (ip.indexToKey(ty)) {
10530 .array_type, .vector_type, .tuple_type, .struct_type => true,
10531 else => false,
10532 };
10533}
10534
10535pub fn errorUnionSet(ip: *const InternPool, ty: Index) Index {
10536 return ip.indexToKey(ty).error_union_type.error_set_type;
10537}
10538
10539pub fn errorUnionPayload(ip: *const InternPool, ty: Index) Index {
10540 return ip.indexToKey(ty).error_union_type.payload_type;
10541}
10542
10543pub fn dump(ip: *const InternPool) void {
10544 var buffer: [4096]u8 = undefined;
10545 const stderr = std.debug.lockStderr(&buffer);
10546 defer std.debug.unlockStderr();
10547 const w = &stderr.file_writer.interface;
10548 dumpDependencyStatsFallible(ip, w) catch return;
10549 dumpStatsFallible(ip, w, std.heap.page_allocator) catch return;
10550 dumpAllFallible(ip, w) catch return;
10551}
10552
10553fn dumpDependencyStatsFallible(ip: *const InternPool, w: *Io.Writer) !void {
10554 const dep_entries_len = ip.dep_entries.items.len - ip.free_dep_entries.items.len;
10555 const src_hash_deps_len = ip.src_hash_deps.count();
10556 const nav_val_deps_len = ip.nav_val_deps.count();
10557 const nav_ty_deps_len = ip.nav_ty_deps.count();
10558 const func_ies_deps_len = ip.func_ies_deps.count();
10559 const type_layout_deps_len = ip.type_layout_deps.count();
10560 const struct_defaults_deps_len = ip.struct_defaults_deps.count();
10561 const source_file_deps_len = ip.source_file_deps.count();
10562 const embed_file_deps_len = ip.embed_file_deps.count();
10563 const namespace_deps_len = ip.namespace_deps.count();
10564 const namespace_name_deps_len = ip.namespace_name_deps.count();
10565 const dep_entries_size = dep_entries_len * @sizeOf(DepEntry);
10566 const src_hash_deps_size = src_hash_deps_len * 8;
10567 const nav_val_deps_size = nav_val_deps_len * 8;
10568 const nav_ty_deps_size = nav_ty_deps_len * 8;
10569 const func_ies_deps_size = func_ies_deps_len * 8;
10570 const type_layout_deps_size = type_layout_deps_len * 8;
10571 const struct_defaults_deps_size = struct_defaults_deps_len * 8;
10572 const source_file_deps_size = source_file_deps_len * 8;
10573 const embed_file_deps_size = embed_file_deps_len * 8;
10574 const namespace_deps_size = namespace_deps_len * 8;
10575 const namespace_name_deps_size = namespace_name_deps_len * (@sizeOf(NamespaceNameKey) + 4);
10576
10577 try w.print(
10578 \\InternPool dependencies: {d} bytes
10579 \\ {d} entries: {d} bytes
10580 \\ {d} src_hash: {d} bytes
10581 \\ {d} nav_val: {d} bytes
10582 \\ {d} nav_ty: {d} bytes
10583 \\ {d} func_ies: {d} bytes
10584 \\ {d} type_layout: {d} bytes
10585 \\ {d} struct_defaults: {d} bytes
10586 \\ {d} source_file: {d} bytes
10587 \\ {d} embed_file: {d} bytes
10588 \\ {d} namespace: {d} bytes
10589 \\ {d} namespace_name: {d} bytes
10590 \\
10591 , .{
10592 dep_entries_size + src_hash_deps_size + nav_val_deps_size + nav_ty_deps_size +
10593 func_ies_deps_size + type_layout_deps_size + struct_defaults_deps_size + source_file_deps_size +
10594 embed_file_deps_size + namespace_deps_size + namespace_name_deps_size,
10595 dep_entries_len,
10596 dep_entries_size,
10597 src_hash_deps_len,
10598 src_hash_deps_size,
10599 nav_val_deps_len,
10600 nav_val_deps_size,
10601 nav_ty_deps_len,
10602 nav_ty_deps_size,
10603 func_ies_deps_len,
10604 func_ies_deps_size,
10605 type_layout_deps_len,
10606 type_layout_deps_size,
10607 struct_defaults_deps_len,
10608 struct_defaults_deps_size,
10609 source_file_deps_len,
10610 source_file_deps_size,
10611 embed_file_deps_len,
10612 embed_file_deps_size,
10613 namespace_deps_len,
10614 namespace_deps_size,
10615 namespace_name_deps_len,
10616 namespace_name_deps_size,
10617 });
10618}
10619
10620fn dumpStatsFallible(ip: *const InternPool, w: *Io.Writer, arena: Allocator) !void {
10621 var items_len: usize = 0;
10622 var extra_len: usize = 0;
10623 var limbs_len: usize = 0;
10624 for (ip.locals) |*local| {
10625 items_len += local.mutate.items.len;
10626 extra_len += local.mutate.extra.len;
10627 limbs_len += local.mutate.limbs.len;
10628 }
10629 const items_size = (1 + 4) * items_len;
10630 const extra_size = 4 * extra_len;
10631 const limbs_size = 8 * limbs_len;
10632
10633 // TODO: map overhead size is not taken into account
10634 const total_size = items_size + extra_size + limbs_size;
10635
10636 try w.print(
10637 \\InternPool values: {d} bytes
10638 \\ {d} items: {d} bytes
10639 \\ {d} extra: {d} bytes
10640 \\ {d} limbs: {d} bytes
10641 \\
10642 , .{
10643 total_size,
10644 items_len,
10645 items_size,
10646 extra_len,
10647 extra_size,
10648 limbs_len,
10649 limbs_size,
10650 });
10651
10652 const TagStats = struct {
10653 count: usize = 0,
10654 bytes: usize = 0,
10655 };
10656 var counts: std.array_hash_map.Auto(Tag, TagStats) = .empty;
10657 for (ip.locals) |*local| {
10658 // Early check for length 0, because `view()` is invalid if capacity is 0
10659 if (local.mutate.items.len == 0) continue;
10660 const items = local.shared.items.view().slice();
10661 const extra_list = local.shared.extra;
10662 const extra_items = extra_list.view().items(.@"0");
10663 for (
10664 items.items(.tag)[0..local.mutate.items.len],
10665 items.items(.data)[0..local.mutate.items.len],
10666 ) |tag, data| {
10667 const gop = try counts.getOrPut(arena, tag);
10668 if (!gop.found_existing) gop.value_ptr.* = .{};
10669 gop.value_ptr.count += 1;
10670 gop.value_ptr.bytes += 1 + 4 + @as(usize, switch (tag) {
10671 // Note that in this case, we have technically leaked some extra data
10672 // bytes which we do not account for here.
10673 .removed => 0,
10674
10675 .type_int_signed => 0,
10676 .type_int_unsigned => 0,
10677 .type_array_small => @sizeOf(Vector),
10678 .type_array_big => @sizeOf(Array),
10679 .type_vector => @sizeOf(Vector),
10680 .type_pointer => @sizeOf(Tag.TypePointer),
10681 .type_slice => 0,
10682 .type_optional => 0,
10683 .type_anyframe => 0,
10684 .type_error_union => @sizeOf(Key.ErrorUnionType),
10685 .type_spirv => @sizeOf(Tag.TypeSpirv),
10686 .type_anyerror_union => 0,
10687 .type_error_set => b: {
10688 const info = extraData(extra_list, Tag.ErrorSet, data);
10689 break :b @sizeOf(Tag.ErrorSet) + (@sizeOf(u32) * info.names_len);
10690 },
10691 .type_inferred_error_set => 0,
10692 .type_tuple => b: {
10693 const info = extraData(extra_list, TypeTuple, data);
10694 break :b @sizeOf(TypeTuple) + (@sizeOf(u32) * 2 * info.fields_len);
10695 },
10696 .type_function => b: {
10697 const info = extraData(extra_list, Tag.TypeFunction, data);
10698 break :b @sizeOf(Tag.TypeFunction) +
10699 (@sizeOf(Index) * info.params_len) +
10700 (@as(u32, 4) * info.flags.cc.extraLen()) +
10701 (@as(u32, 4) * @intFromBool(info.flags.has_comptime_bits)) +
10702 (@as(u32, 4) * @intFromBool(info.flags.has_noalias_bits));
10703 },
10704
10705 .type_struct => b: {
10706 var n: usize = @typeInfo(Tag.TypeStruct).@"struct".field_names.len;
10707 const extra = extraDataTrail(extra_list, Tag.TypeStruct, data);
10708 switch (extra.data.flags.any_captures) {
10709 .reified => n += 2, // type_hash: PackedU64
10710 .true => {
10711 n += 1; // captures_len: u32
10712 n += extra_items[extra.end]; // capture: CaptureValue
10713 },
10714 .false => {},
10715 }
10716 n += extra.data.fields_len; // field_name: NullTerminatedString
10717 n += extra.data.fields_len; // field_type: Index
10718 if (extra.data.flags.any_field_defaults) {
10719 n += extra.data.fields_len; // field_default: Index
10720 }
10721 if (extra.data.flags.any_field_aligns) {
10722 n += (extra.data.fields_len + 3) / 4; // field_align: Alignment
10723 }
10724 if (extra.data.flags.any_comptime_fields) {
10725 n += (extra.data.fields_len + 31) / 32; // field_is_comptime_bits: u32
10726 }
10727 if (extra.data.flags.layout == .auto) {
10728 n += extra.data.fields_len; // field_runtime_order: RuntimeOrder
10729 }
10730 n += extra.data.fields_len; // field_offset: u32
10731 break :b n * @sizeOf(u32);
10732 },
10733 .type_struct_packed_auto, .type_struct_packed_explicit => b: {
10734 var n: usize = @typeInfo(Tag.TypeStructPacked).@"struct".field_names.len;
10735 const extra = extraDataTrail(extra_list, Tag.TypeStructPacked, data);
10736 switch (extra.data.bits.captures_len) {
10737 .reified => n += 2, // type_hash: PackedU64
10738 _ => |len| n += @backingInt(len), // capture: CaptureValue
10739 }
10740 n += extra.data.fields_len; // field_name: NullTerminatedString
10741 n += extra.data.fields_len; // field_type: Index
10742 break :b n * @sizeOf(u32);
10743 },
10744 .type_struct_packed_auto_defaults, .type_struct_packed_explicit_defaults => b: {
10745 var n: usize = @typeInfo(Tag.TypeStructPacked).@"struct".field_names.len;
10746 const extra = extraDataTrail(extra_list, Tag.TypeStructPacked, data);
10747 switch (extra.data.bits.captures_len) {
10748 .reified => n += 2, // type_hash: PackedU64
10749 _ => |len| n += @backingInt(len), // capture: CaptureValue
10750 }
10751 n += extra.data.fields_len; // field_name: NullTerminatedString
10752 n += extra.data.fields_len; // field_type: Index
10753 n += extra.data.fields_len; // field_default: Index
10754 break :b n * @sizeOf(u32);
10755 },
10756 .type_union => b: {
10757 var n: usize = @typeInfo(Tag.TypeUnion).@"struct".field_names.len;
10758 const extra = extraDataTrail(extra_list, Tag.TypeUnion, data);
10759 switch (extra.data.flags.any_captures) {
10760 .reified => n += 2, // type_hash: PackedU64
10761 .true => {
10762 n += 1; // captures_len: u32
10763 n += extra_items[extra.end]; // capture: CaptureValue
10764 },
10765 .false => {},
10766 }
10767 n += extra.data.fields_len; // field_type: Index
10768 if (extra.data.flags.any_field_aligns) {
10769 n += (extra.data.fields_len + 3) / 4; // field_align: Alignment
10770 }
10771 break :b n * @sizeOf(u32);
10772 },
10773 .type_union_packed_auto, .type_union_packed_explicit => b: {
10774 var n: usize = @typeInfo(Tag.TypeUnionPacked).@"struct".field_names.len;
10775 const extra = extraDataTrail(extra_list, Tag.TypeUnionPacked, data);
10776 switch (extra.data.bits.captures_len) {
10777 .reified => n += 2, // type_hash: PackedU64
10778 _ => |len| n += @backingInt(len), // capture: CaptureValue
10779 }
10780 n += extra.data.fields_len; // field_type: Index
10781 break :b n * @sizeOf(u32);
10782 },
10783 .type_enum_auto => b: {
10784 var n: usize = @typeInfo(Tag.TypeEnum).@"struct".field_names.len;
10785 const extra = extraData(extra_list, Tag.TypeEnum, data);
10786 switch (extra.bits.captures_len) {
10787 .generated_union_tag => n += 1, // owner_union: Index
10788 .reified => {
10789 n += 1; // zir_index: TrackedInst.Index,
10790 n += 2; // type_hash: PackedU64
10791 },
10792 _ => |len| {
10793 n += 1; // zir_index: TrackedInst.Index,
10794 n += @backingInt(len); // capture: CaptureValue
10795 },
10796 }
10797 n += extra.fields_len; // field_name: NullTerminatedString
10798 break :b n * @sizeOf(u32);
10799 },
10800 .type_enum_explicit, .type_enum_nonexhaustive => b: {
10801 var n: usize = @typeInfo(Tag.TypeEnum).@"struct".field_names.len;
10802 const extra = extraData(extra_list, Tag.TypeEnum, data);
10803 switch (extra.bits.captures_len) {
10804 .generated_union_tag => n += 1, // owner_union: Index
10805 .reified => {
10806 n += 1; // zir_index: TrackedInst.Index,
10807 n += 2; // type_hash: PackedU64
10808 },
10809 _ => |len| {
10810 n += 1; // zir_index: TrackedInst.Index,
10811 n += @backingInt(len); // capture: CaptureValue
10812 },
10813 }
10814 n += 1; // field_value_map: MapIndex
10815 n += extra.fields_len; // field_name: NullTerminatedString
10816 n += extra.fields_len; // field_value: Index
10817 break :b n * @sizeOf(u32);
10818 },
10819 .type_opaque => b: {
10820 var n: usize = @typeInfo(Tag.TypeEnum).@"struct".field_names.len;
10821 const extra = extraData(extra_list, Tag.TypeOpaque, data);
10822 n += extra.captures_len; // capture: CaptureValue
10823 break :b n * @sizeOf(u32);
10824 },
10825
10826 .undef => 0,
10827 .simple_type => 0,
10828 .simple_value => 0,
10829 .ptr_nav => @sizeOf(PtrNav),
10830 .ptr_comptime_alloc => @sizeOf(PtrComptimeAlloc),
10831 .ptr_uav => @sizeOf(PtrUav),
10832 .ptr_uav_aligned => @sizeOf(PtrUavAligned),
10833 .ptr_comptime_field => @sizeOf(PtrComptimeField),
10834 .ptr_int => @sizeOf(PtrInt),
10835 .ptr_eu_payload => @sizeOf(PtrBase),
10836 .ptr_opt_payload => @sizeOf(PtrBase),
10837 .ptr_elem => @sizeOf(PtrBaseIndex),
10838 .ptr_field => @sizeOf(PtrBaseIndex),
10839 .ptr_slice => @sizeOf(PtrSlice),
10840 .opt_null => 0,
10841 .opt_payload => @sizeOf(Tag.TypeValue),
10842 .int_u8 => 0,
10843 .int_u16 => 0,
10844 .int_u32 => 0,
10845 .int_i32 => 0,
10846 .int_usize => 0,
10847 .int_comptime_int_u32 => 0,
10848 .int_comptime_int_i32 => 0,
10849 .int_small => @sizeOf(IntSmall),
10850
10851 .int_positive,
10852 .int_negative,
10853 => b: {
10854 const limbs_list = local.shared.getLimbs();
10855 const int: Int = @bitCast(limbs_list.view().items(.@"0")[data..][0..Int.limbs_items_len].*);
10856 break :b @sizeOf(Int) + int.limbs_len * @sizeOf(Limb);
10857 },
10858
10859 .error_set_error, .error_union_error => @sizeOf(Key.Error),
10860 .error_union_payload => @sizeOf(Tag.TypeValue),
10861 .enum_literal => 0,
10862 .enum_tag => @sizeOf(Tag.EnumTag),
10863
10864 .bytes => b: {
10865 const info = extraData(extra_list, Bytes, data);
10866 const len: usize = @intCast(ip.aggregateTypeLenIncludingSentinel(info.ty));
10867 break :b @sizeOf(Bytes) + len + @intFromBool(info.bytes.at(len - 1, ip) != 0);
10868 },
10869 .aggregate => b: {
10870 const info = extraData(extra_list, Tag.Aggregate, data);
10871 const fields_len: u32 = @intCast(ip.aggregateTypeLenIncludingSentinel(info.ty));
10872 break :b @sizeOf(Tag.Aggregate) + (@sizeOf(Index) * fields_len);
10873 },
10874 .repeated => @sizeOf(Repeated),
10875
10876 .float_f16 => 0,
10877 .float_f32 => 0,
10878 .float_f64 => @sizeOf(Float64),
10879 .float_f80 => @sizeOf(Float80),
10880 .float_f128 => @sizeOf(Float128),
10881 .float_c_longdouble_f80 => @sizeOf(Float80),
10882 .float_c_longdouble_f128 => @sizeOf(Float128),
10883 .float_comptime_float => @sizeOf(Float128),
10884 .@"extern" => @sizeOf(Tag.Extern),
10885 .func_decl => @sizeOf(Tag.FuncDecl),
10886 .func_instance => b: {
10887 const info = extraData(extra_list, Tag.FuncInstance, data);
10888 const ty = ip.typeOf(info.generic_owner);
10889 const params_len = ip.indexToKey(ty).func_type.param_types.len;
10890 break :b @sizeOf(Tag.FuncInstance) + @sizeOf(Index) * params_len;
10891 },
10892 .func_coerced => @sizeOf(Tag.FuncCoerced),
10893 .only_possible_value => 0,
10894 .union_value => @sizeOf(Key.Union),
10895 .bitpack => 2 * @sizeOf(u32),
10896
10897 .memoized_call => b: {
10898 const info = extraData(extra_list, MemoizedCall, data);
10899 break :b @sizeOf(MemoizedCall) + (@sizeOf(Index) * info.args_len);
10900 },
10901 });
10902 }
10903 }
10904 const SortContext = struct {
10905 map: *std.array_hash_map.Auto(Tag, TagStats),
10906 pub fn lessThan(ctx: @This(), a_index: usize, b_index: usize) bool {
10907 const values = ctx.map.values();
10908 return values[a_index].bytes > values[b_index].bytes;
10909 //return values[a_index].count > values[b_index].count;
10910 }
10911 };
10912 counts.sort(SortContext{ .map = &counts });
10913 const len = @min(50, counts.count());
10914 try w.print(" top 50 tags:\n", .{});
10915 for (counts.keys()[0..len], counts.values()[0..len]) |tag, stats| {
10916 try w.print(" {t}: {d} occurrences, {d} total bytes\n", .{ tag, stats.count, stats.bytes });
10917 }
10918}
10919
10920fn dumpAllFallible(ip: *const InternPool, w: *Io.Writer) anyerror!void {
10921 for (ip.locals, 0..) |*local, tid| {
10922 // Early check for length 0, because `view()` is invalid if capacity is 0
10923 if (local.mutate.items.len == 0) continue;
10924 const items = local.shared.items.view();
10925 for (
10926 items.items(.tag)[0..local.mutate.items.len],
10927 items.items(.data)[0..local.mutate.items.len],
10928 0..,
10929 ) |tag, data, index| {
10930 const i = Index.Unwrapped.wrap(.{ .tid = @fromBackingInt(@intCast(tid)), .index = @intCast(index) }, ip);
10931 try w.print("${d} = {s}(", .{ i, @tagName(tag) });
10932 switch (tag) {
10933 .removed => {},
10934
10935 .simple_type => try w.print("{s}", .{@tagName(@as(SimpleType, @fromBackingInt(@intCast(@backingInt(i)))))}),
10936 .simple_value => try w.print("{s}", .{@tagName(@as(SimpleValue, @fromBackingInt(@intCast(@backingInt(i)))))}),
10937
10938 .type_int_signed,
10939 .type_int_unsigned,
10940 .type_array_small,
10941 .type_array_big,
10942 .type_vector,
10943 .type_pointer,
10944 .type_optional,
10945 .type_anyframe,
10946 .type_error_union,
10947 .type_anyerror_union,
10948 .type_error_set,
10949 .type_inferred_error_set,
10950 .type_tuple,
10951 .type_function,
10952 .type_struct,
10953 .type_struct_packed_auto,
10954 .type_struct_packed_explicit,
10955 .type_struct_packed_auto_defaults,
10956 .type_struct_packed_explicit_defaults,
10957 .type_union,
10958 .type_union_packed_auto,
10959 .type_union_packed_explicit,
10960 .type_enum_auto,
10961 .type_enum_explicit,
10962 .type_enum_nonexhaustive,
10963 .type_opaque,
10964 .type_spirv,
10965 .undef,
10966 .ptr_nav,
10967 .ptr_comptime_alloc,
10968 .ptr_uav,
10969 .ptr_uav_aligned,
10970 .ptr_comptime_field,
10971 .ptr_int,
10972 .ptr_eu_payload,
10973 .ptr_opt_payload,
10974 .ptr_elem,
10975 .ptr_field,
10976 .ptr_slice,
10977 .opt_payload,
10978 .int_u8,
10979 .int_u16,
10980 .int_u32,
10981 .int_i32,
10982 .int_usize,
10983 .int_comptime_int_u32,
10984 .int_comptime_int_i32,
10985 .int_small,
10986 .int_positive,
10987 .int_negative,
10988 .error_set_error,
10989 .error_union_error,
10990 .error_union_payload,
10991 .enum_literal,
10992 .enum_tag,
10993 .bytes,
10994 .aggregate,
10995 .repeated,
10996 .float_f16,
10997 .float_f32,
10998 .float_f64,
10999 .float_f80,
11000 .float_f128,
11001 .float_c_longdouble_f80,
11002 .float_c_longdouble_f128,
11003 .float_comptime_float,
11004 .@"extern",
11005 .func_decl,
11006 .func_instance,
11007 .func_coerced,
11008 .union_value,
11009 .bitpack,
11010 .memoized_call,
11011 => try w.print("{d}", .{data}),
11012
11013 .opt_null,
11014 .type_slice,
11015 .only_possible_value,
11016 => try w.print("${d}", .{data}),
11017 }
11018 try w.writeAll(")\n");
11019 }
11020 }
11021}
11022
11023pub fn dumpGenericInstances(ip: *const InternPool, allocator: Allocator) void {
11024 var buffer: [4096]u8 = undefined;
11025 const stderr = std.debug.lockStderr(&buffer);
11026 defer std.debug.unlockStderr();
11027 const w = &stderr.file_writer.interface;
11028 ip.dumpGenericInstancesFallible(allocator, w) catch return;
11029}
11030
11031pub fn dumpGenericInstancesFallible(ip: *const InternPool, allocator: Allocator, w: *Io.Writer) !void {
11032 var arena_allocator = std.heap.ArenaAllocator.init(allocator);
11033 defer arena_allocator.deinit();
11034 const arena = arena_allocator.allocator();
11035
11036 var instances: std.array_hash_map.Auto(Index, std.ArrayList(Index)) = .empty;
11037 for (ip.locals, 0..) |*local, tid| {
11038 const items = local.shared.items.view().slice();
11039 const extra_list = local.shared.extra;
11040 for (
11041 items.items(.tag)[0..local.mutate.items.len],
11042 items.items(.data)[0..local.mutate.items.len],
11043 0..,
11044 ) |tag, data, index| {
11045 if (tag != .func_instance) continue;
11046 const info = extraData(extra_list, Tag.FuncInstance, data);
11047
11048 const gop = try instances.getOrPut(arena, info.generic_owner);
11049 if (!gop.found_existing) gop.value_ptr.* = .empty;
11050
11051 try gop.value_ptr.append(
11052 arena,
11053 Index.Unwrapped.wrap(.{ .tid = @fromBackingInt(@intCast(tid)), .index = @intCast(index) }, ip),
11054 );
11055 }
11056 }
11057
11058 const SortContext = struct {
11059 values: []std.ArrayList(Index),
11060 pub fn lessThan(ctx: @This(), a_index: usize, b_index: usize) bool {
11061 return ctx.values[a_index].items.len > ctx.values[b_index].items.len;
11062 }
11063 };
11064
11065 instances.sort(SortContext{ .values = instances.values() });
11066 var it = instances.iterator();
11067 while (it.next()) |entry| {
11068 const generic_fn_owner_nav = ip.getNav(ip.funcDeclInfo(entry.key_ptr.*).owner_nav);
11069 try w.print("{f} ({d}): \n", .{ generic_fn_owner_nav.name.fmt(ip), entry.value_ptr.items.len });
11070 for (entry.value_ptr.items) |index| {
11071 const unwrapped_index = index.unwrap(ip);
11072 const func = ip.extraFuncInstance(unwrapped_index.tid, unwrapped_index.getExtra(ip), unwrapped_index.getData(ip));
11073 const owner_nav = ip.getNav(func.owner_nav);
11074 try w.print(" {f}: (", .{owner_nav.name.fmt(ip)});
11075 for (func.comptime_args.get(ip)) |arg| {
11076 if (arg != .none) {
11077 const key = ip.indexToKey(arg);
11078 try w.print(" {} ", .{key});
11079 }
11080 }
11081 try w.writeAll(")\n");
11082 }
11083 }
11084}
11085
11086pub fn getNav(ip: *const InternPool, index: Nav.Index) Nav {
11087 const unwrapped = index.unwrap(ip);
11088 const view = ip.getLocalShared(unwrapped.tid).navs.acquire().view();
11089 // We can't just call `view.get(unwrapped.index)`, because a concurrent call to `resolveNav`
11090 // could be writing to fields, making a non-atomic load illegal. Instead, atomically load
11091 // each field. We don't need any ordering guarantees because if we need to see (e.g.) the
11092 // resolved type of a `Nav`, that information should have already been released to our caller.
11093 const repr: Nav.Repr = .{
11094 // Load the first few fields non-atomically---they are never mutated after `Nav` creation.
11095 .name = view.items(.name)[unwrapped.index],
11096 .fqn = view.items(.fqn)[unwrapped.index],
11097 .analysis_namespace = view.items(.analysis_namespace)[unwrapped.index],
11098 .analysis_zir_index = view.items(.analysis_zir_index)[unwrapped.index],
11099 // The last few fields are populated by `resolveNav` so must be loaded atomically.
11100 .type = @atomicLoad(InternPool.Index, &view.items(.type)[unwrapped.index], .monotonic),
11101 .value = @atomicLoad(InternPool.Index, &view.items(.value)[unwrapped.index], .monotonic),
11102 .@"linksection" = @atomicLoad(OptionalNullTerminatedString, &view.items(.@"linksection")[unwrapped.index], .monotonic),
11103 .bits = @atomicLoad(Nav.Repr.Bits, &view.items(.bits)[unwrapped.index], .monotonic),
11104 };
11105 return repr.unpack();
11106}
11107
11108pub fn namespacePtr(ip: *InternPool, namespace_index: NamespaceIndex) *Zcu.Namespace {
11109 const unwrapped_namespace_index = namespace_index.unwrap(ip);
11110 const namespaces = ip.getLocalShared(unwrapped_namespace_index.tid).namespaces.acquire();
11111 const namespaces_bucket = namespaces.view().items(.@"0")[unwrapped_namespace_index.bucket_index];
11112 return &namespaces_bucket[unwrapped_namespace_index.index];
11113}
11114
11115/// Create a `ComptimeUnit`, forming an `AnalUnit` for a `comptime` declaration.
11116pub fn createComptimeUnit(
11117 ip: *InternPool,
11118 gpa: Allocator,
11119 io: Io,
11120 tid: Zcu.PerThread.Id,
11121 zir_index: TrackedInst.Index,
11122 namespace: NamespaceIndex,
11123) Allocator.Error!ComptimeUnit.Id {
11124 const comptime_units = ip.getLocal(tid).getMutableComptimeUnits(gpa, io);
11125 const id_unwrapped: ComptimeUnit.Id.Unwrapped = .{
11126 .tid = tid,
11127 .index = comptime_units.mutate.len,
11128 };
11129 try comptime_units.append(.{.{
11130 .zir_index = zir_index,
11131 .namespace = namespace,
11132 }});
11133 return id_unwrapped.wrap(ip);
11134}
11135
11136pub fn getComptimeUnit(ip: *const InternPool, id: ComptimeUnit.Id) ComptimeUnit {
11137 const unwrapped = id.unwrap(ip);
11138 const comptime_units = ip.getLocalShared(unwrapped.tid).comptime_units.acquire();
11139 return comptime_units.view().items(.@"0")[unwrapped.index];
11140}
11141
11142/// Create a `Nav` which does not undergo semantic analysis.
11143/// Since it is never analyzed, the `Nav`'s value must be known at creation time.
11144fn createNav(
11145 ip: *InternPool,
11146 gpa: Allocator,
11147 io: Io,
11148 tid: Zcu.PerThread.Id,
11149 name: NullTerminatedString,
11150 fqn: NullTerminatedString,
11151 resolved: @typeInfo(@FieldType(Nav, "resolved")).optional.child,
11152) Allocator.Error!Nav.Index {
11153 const navs = ip.getLocal(tid).getMutableNavs(gpa, io);
11154 const index_unwrapped: Nav.Index.Unwrapped = .{
11155 .tid = tid,
11156 .index = navs.mutate.len,
11157 };
11158 try navs.append(Nav.pack(.{
11159 .name = name,
11160 .fqn = fqn,
11161 .analysis = null,
11162 .resolved = resolved,
11163 }));
11164 return index_unwrapped.wrap(ip);
11165}
11166
11167/// Create a `Nav` which undergoes semantic analysis because it corresponds to a source declaration.
11168/// The value of the `Nav` is initially unresolved.
11169pub fn createDeclNav(
11170 ip: *InternPool,
11171 gpa: Allocator,
11172 io: Io,
11173 tid: Zcu.PerThread.Id,
11174 name: NullTerminatedString,
11175 fqn: NullTerminatedString,
11176 zir_index: TrackedInst.Index,
11177 namespace: NamespaceIndex,
11178) Allocator.Error!Nav.Index {
11179 const navs = ip.getLocal(tid).getMutableNavs(gpa, io);
11180
11181 try navs.ensureUnusedCapacity(1);
11182
11183 const nav = Nav.Index.Unwrapped.wrap(.{
11184 .tid = tid,
11185 .index = navs.mutate.len,
11186 }, ip);
11187
11188 navs.appendAssumeCapacity(Nav.pack(.{
11189 .name = name,
11190 .fqn = fqn,
11191 .analysis = .{
11192 .namespace = namespace,
11193 .zir_index = zir_index,
11194 .wanted = false,
11195 },
11196 .resolved = null,
11197 }));
11198
11199 return nav;
11200}
11201
11202/// Resolve the type (and possibly the value) of a `Nav` with an analysis owner.
11203/// If its status is already `resolved`, the old value is discarded.
11204pub fn resolveNav(
11205 ip: *InternPool,
11206 io: Io,
11207 nav: Nav.Index,
11208 resolved: @typeInfo(@FieldType(Nav, "resolved")).optional.child,
11209) void {
11210 const unwrapped = nav.unwrap(ip);
11211
11212 const local = ip.getLocal(unwrapped.tid);
11213 local.mutate.extra.mutex.lockUncancelable(io);
11214 defer local.mutate.extra.mutex.unlock(io);
11215
11216 const navs = local.shared.navs.view();
11217
11218 const nav_analysis_namespace = navs.items(.analysis_namespace);
11219 const nav_analysis_zir_index = navs.items(.analysis_zir_index);
11220 const nav_types = navs.items(.type);
11221 const nav_values = navs.items(.value);
11222 const nav_linksections = navs.items(.@"linksection");
11223 const nav_bits = navs.items(.bits);
11224
11225 assert(nav_analysis_namespace[unwrapped.index] != .none);
11226 assert(nav_analysis_zir_index[unwrapped.index] != .none);
11227
11228 @atomicStore(
11229 OptionalNullTerminatedString,
11230 &nav_linksections[unwrapped.index],
11231 resolved.@"linksection",
11232 .monotonic,
11233 );
11234
11235 const bits = &nav_bits[unwrapped.index];
11236 assert(@atomicLoad(Nav.Repr.Bits, bits, .monotonic).want_analysis); // otherwise we wouldn't be resolving `nav` at all
11237 @atomicStore(Nav.Repr.Bits, bits, .{
11238 .@"align" = resolved.@"align",
11239 .@"addrspace" = resolved.@"addrspace",
11240 .@"const" = resolved.@"const",
11241 .@"threadlocal" = resolved.@"threadlocal",
11242 .is_extern_decl = resolved.is_extern_decl,
11243 .want_analysis = true, // asserted above that this is already `true`
11244 }, .monotonic);
11245
11246 @atomicStore(
11247 InternPool.Index,
11248 &nav_types[unwrapped.index],
11249 resolved.type,
11250 .monotonic,
11251 );
11252
11253 @atomicStore(
11254 InternPool.Index,
11255 &nav_values[unwrapped.index],
11256 resolved.value,
11257 .monotonic,
11258 );
11259}
11260
11261pub fn createNamespace(
11262 ip: *InternPool,
11263 gpa: Allocator,
11264 io: Io,
11265 tid: Zcu.PerThread.Id,
11266 initialization: Zcu.Namespace,
11267) Allocator.Error!NamespaceIndex {
11268 const local = ip.getLocal(tid);
11269 const free_list_next = local.mutate.namespaces.free_list;
11270 if (free_list_next != Local.BucketListMutate.free_list_sentinel) {
11271 const reused_namespace_index: NamespaceIndex = @fromBackingInt(@intCast(free_list_next));
11272 const reused_namespace = ip.namespacePtr(reused_namespace_index);
11273 local.mutate.namespaces.free_list =
11274 @backingInt(@field(reused_namespace, Local.namespace_next_free_field));
11275 reused_namespace.* = initialization;
11276 return reused_namespace_index;
11277 }
11278 const namespaces = local.getMutableNamespaces(gpa, io);
11279 const last_bucket_len = local.mutate.namespaces.last_bucket_len & Local.namespaces_bucket_mask;
11280 if (last_bucket_len == 0) {
11281 try namespaces.ensureUnusedCapacity(1);
11282 var arena = namespaces.arena.promote(namespaces.gpa);
11283 defer namespaces.arena.* = arena.state;
11284 namespaces.appendAssumeCapacity(.{try arena.allocator().create(
11285 [1 << Local.namespaces_bucket_width]Zcu.Namespace,
11286 )});
11287 }
11288 const unwrapped_namespace_index: NamespaceIndex.Unwrapped = .{
11289 .tid = tid,
11290 .bucket_index = namespaces.mutate.len - 1,
11291 .index = last_bucket_len,
11292 };
11293 local.mutate.namespaces.last_bucket_len = last_bucket_len + 1;
11294 const namespace_index = unwrapped_namespace_index.wrap(ip);
11295 ip.namespacePtr(namespace_index).* = initialization;
11296 return namespace_index;
11297}
11298
11299pub fn destroyNamespace(
11300 ip: *InternPool,
11301 tid: Zcu.PerThread.Id,
11302 namespace_index: NamespaceIndex,
11303) void {
11304 const local = ip.getLocal(tid);
11305 const namespace = ip.namespacePtr(namespace_index);
11306 namespace.* = .{
11307 .parent = undefined,
11308 .file_scope = undefined,
11309 .owner_type = undefined,
11310 .generation = undefined,
11311 };
11312 @field(namespace, Local.namespace_next_free_field) =
11313 @fromBackingInt(@intCast(local.mutate.namespaces.free_list));
11314 local.mutate.namespaces.free_list = @backingInt(namespace_index);
11315}
11316
11317pub fn filePtr(ip: *const InternPool, file_index: FileIndex) *Zcu.File {
11318 const file_index_unwrapped = file_index.unwrap(ip);
11319 const files = ip.getLocalShared(file_index_unwrapped.tid).files.acquire();
11320 return files.view().items(.file)[file_index_unwrapped.index];
11321}
11322
11323pub fn createFile(
11324 ip: *InternPool,
11325 gpa: Allocator,
11326 io: Io,
11327 tid: Zcu.PerThread.Id,
11328 file: File,
11329) Allocator.Error!FileIndex {
11330 const files = ip.getLocal(tid).getMutableFiles(gpa, io);
11331 const file_index_unwrapped: FileIndex.Unwrapped = .{
11332 .tid = tid,
11333 .index = files.mutate.len,
11334 };
11335 try files.append(file);
11336 return file_index_unwrapped.wrap(ip);
11337}
11338
11339const EmbeddedNulls = enum {
11340 no_embedded_nulls,
11341 maybe_embedded_nulls,
11342
11343 fn StringType(comptime embedded_nulls: EmbeddedNulls) type {
11344 return switch (embedded_nulls) {
11345 .no_embedded_nulls => NullTerminatedString,
11346 .maybe_embedded_nulls => String,
11347 };
11348 }
11349
11350 fn OptionalStringType(comptime embedded_nulls: EmbeddedNulls) type {
11351 return switch (embedded_nulls) {
11352 .no_embedded_nulls => OptionalNullTerminatedString,
11353 .maybe_embedded_nulls => OptionalString,
11354 };
11355 }
11356};
11357
11358pub fn getOrPutString(
11359 ip: *InternPool,
11360 gpa: Allocator,
11361 io: Io,
11362 tid: Zcu.PerThread.Id,
11363 slice: []const u8,
11364 comptime embedded_nulls: EmbeddedNulls,
11365) Allocator.Error!embedded_nulls.StringType() {
11366 const string_bytes = ip.getLocal(tid).getMutableStringBytes(gpa, io);
11367 try string_bytes.ensureUnusedCapacity(slice.len + 1);
11368 string_bytes.appendSliceAssumeCapacity(.{slice});
11369 string_bytes.appendAssumeCapacity(.{0});
11370 return ip.getOrPutTrailingString(gpa, io, tid, @intCast(slice.len + 1), embedded_nulls);
11371}
11372
11373pub fn getOrPutStringFmt(
11374 ip: *InternPool,
11375 gpa: Allocator,
11376 io: Io,
11377 tid: Zcu.PerThread.Id,
11378 comptime format: []const u8,
11379 args: anytype,
11380 comptime embedded_nulls: EmbeddedNulls,
11381) Allocator.Error!embedded_nulls.StringType() {
11382 // ensure that references to strings in args do not get invalidated
11383 const format_z = format ++ .{0};
11384 const len: u32 = @intCast(std.fmt.count(format_z, args));
11385 const string_bytes = ip.getLocal(tid).getMutableStringBytes(gpa, io);
11386 const slice = try string_bytes.addManyAsSlice(len);
11387 assert((std.mem.print(slice[0], format_z, args) catch unreachable).len == len);
11388 return ip.getOrPutTrailingString(gpa, io, tid, len, embedded_nulls);
11389}
11390
11391pub fn getOrPutStringOpt(
11392 ip: *InternPool,
11393 gpa: Allocator,
11394 io: Io,
11395 tid: Zcu.PerThread.Id,
11396 slice: ?[]const u8,
11397 comptime embedded_nulls: EmbeddedNulls,
11398) Allocator.Error!embedded_nulls.OptionalStringType() {
11399 const string = try getOrPutString(ip, gpa, io, tid, slice orelse return .none, embedded_nulls);
11400 return string.toOptional();
11401}
11402
11403/// Uses the last len bytes of strings as the key.
11404pub fn getOrPutTrailingString(
11405 ip: *InternPool,
11406 gpa: Allocator,
11407 io: Io,
11408 tid: Zcu.PerThread.Id,
11409 len: u32,
11410 comptime embedded_nulls: EmbeddedNulls,
11411) Allocator.Error!embedded_nulls.StringType() {
11412 const local = ip.getLocal(tid);
11413 const strings = local.getMutableStrings(gpa, io);
11414 try strings.ensureUnusedCapacity(1);
11415 const string_bytes = local.getMutableStringBytes(gpa, io);
11416 const start: u32 = @intCast(string_bytes.mutate.len - len);
11417 if (len > 0 and string_bytes.view().items(.@"0")[string_bytes.mutate.len - 1] == 0) {
11418 string_bytes.mutate.len -= 1;
11419 } else {
11420 try string_bytes.ensureUnusedCapacity(1);
11421 }
11422 const key: []const u8 = string_bytes.view().items(.@"0")[start..];
11423 const value: embedded_nulls.StringType() = @fromBackingInt(@intCast(@backingInt((String.Unwrapped{
11424 .tid = tid,
11425 .index = strings.mutate.len - 1,
11426 }).wrap(ip))));
11427 const has_embedded_null = std.mem.findScalar(u8, key, 0) != null;
11428 switch (embedded_nulls) {
11429 .no_embedded_nulls => assert(!has_embedded_null),
11430 .maybe_embedded_nulls => if (has_embedded_null) {
11431 string_bytes.appendAssumeCapacity(.{0});
11432 strings.appendAssumeCapacity(.{string_bytes.mutate.len});
11433 return value;
11434 },
11435 }
11436
11437 const full_hash = Hash.hash(0, key);
11438 const hash: u32 = @truncate(full_hash >> 32);
11439 const shard = &ip.shards[@intCast(full_hash & (ip.shards.len - 1))];
11440 var map = shard.shared.string_map.acquire();
11441 const Map = @TypeOf(map);
11442 var map_mask = map.header().mask();
11443 var map_index = hash;
11444 while (true) : (map_index += 1) {
11445 map_index &= map_mask;
11446 const entry = &map.entries[map_index];
11447 const index = entry.acquire().unwrap() orelse break;
11448 if (entry.hash != hash) continue;
11449 if (!index.eqlSlice(key, ip)) continue;
11450 string_bytes.shrinkRetainingCapacity(start);
11451 return @fromBackingInt(@intCast(@backingInt(index)));
11452 }
11453 shard.mutate.string_map.mutex.lock(io, tid);
11454 defer shard.mutate.string_map.mutex.unlock(io);
11455 if (map.entries != shard.shared.string_map.entries) {
11456 map = shard.shared.string_map;
11457 map_mask = map.header().mask();
11458 map_index = hash;
11459 }
11460 while (true) : (map_index += 1) {
11461 map_index &= map_mask;
11462 const entry = &map.entries[map_index];
11463 const index = entry.acquire().unwrap() orelse break;
11464 if (entry.hash != hash) continue;
11465 if (!index.eqlSlice(key, ip)) continue;
11466 string_bytes.shrinkRetainingCapacity(start);
11467 return @fromBackingInt(@intCast(@backingInt(index)));
11468 }
11469 defer shard.mutate.string_map.len += 1;
11470 const map_header = map.header().*;
11471 if (shard.mutate.string_map.len < map_header.capacity * 3 / 5) {
11472 string_bytes.appendAssumeCapacity(.{0});
11473 strings.appendAssumeCapacity(.{string_bytes.mutate.len});
11474 const entry = &map.entries[map_index];
11475 entry.hash = hash;
11476 entry.release(@fromBackingInt(@intCast(@backingInt(value))));
11477 return value;
11478 }
11479 const arena_state = &local.mutate.arena;
11480 var arena = arena_state.promote(gpa);
11481 defer arena_state.* = arena.state;
11482 const new_map_capacity = map_header.capacity * 2;
11483 const new_map_buf = try arena.allocator().alignedAlloc(
11484 u8,
11485 .fromByteUnits(Map.alignment),
11486 Map.entries_offset + new_map_capacity * @sizeOf(Map.Entry),
11487 );
11488 const new_map: Map = .{ .entries = @ptrCast(new_map_buf[Map.entries_offset..].ptr) };
11489 new_map.header().* = .{ .capacity = new_map_capacity };
11490 @memset(new_map.entries[0..new_map_capacity], .{ .value = .none, .hash = undefined });
11491 const new_map_mask = new_map.header().mask();
11492 map_index = 0;
11493 while (map_index < map_header.capacity) : (map_index += 1) {
11494 const entry = &map.entries[map_index];
11495 const index = entry.value.unwrap() orelse continue;
11496 const item_hash = entry.hash;
11497 var new_map_index = item_hash;
11498 while (true) : (new_map_index += 1) {
11499 new_map_index &= new_map_mask;
11500 const new_entry = &new_map.entries[new_map_index];
11501 if (new_entry.value != .none) continue;
11502 new_entry.* = .{
11503 .value = index.toOptional(),
11504 .hash = item_hash,
11505 };
11506 break;
11507 }
11508 }
11509 map = new_map;
11510 map_index = hash;
11511 while (true) : (map_index += 1) {
11512 map_index &= new_map_mask;
11513 if (map.entries[map_index].value == .none) break;
11514 }
11515 string_bytes.appendAssumeCapacity(.{0});
11516 strings.appendAssumeCapacity(.{string_bytes.mutate.len});
11517 map.entries[map_index] = .{
11518 .value = @fromBackingInt(@intCast(@backingInt(value))),
11519 .hash = hash,
11520 };
11521 shard.shared.string_map.release(new_map);
11522 return value;
11523}
11524
11525pub fn getString(ip: *InternPool, key: []const u8) OptionalNullTerminatedString {
11526 const full_hash = Hash.hash(0, key);
11527 const hash: u32 = @truncate(full_hash >> 32);
11528 const shard = &ip.shards[@intCast(full_hash & (ip.shards.len - 1))];
11529 const map = shard.shared.string_map.acquire();
11530 const map_mask = map.header().mask();
11531 var map_index = hash;
11532 while (true) : (map_index += 1) {
11533 map_index &= map_mask;
11534 const entry = &map.entries[map_index];
11535 const index = entry.value.unwrap() orelse return .none;
11536 if (entry.hash != hash) continue;
11537 if (index.eqlSlice(key, ip)) return index.toOptional();
11538 }
11539}
11540
11541pub fn typeOf(ip: *const InternPool, index: Index) Index {
11542 // This optimization of static keys is required so that typeOf can be called
11543 // on static keys that haven't been added yet during static key initialization.
11544 // An alternative would be to topological sort the static keys, but this would
11545 // mean that the range of type indices would not be dense.
11546 return switch (index) {
11547 .u0_type,
11548 .u1_type,
11549 .u8_type,
11550 .i8_type,
11551 .u16_type,
11552 .i16_type,
11553 .u29_type,
11554 .u32_type,
11555 .i32_type,
11556 .u64_type,
11557 .i64_type,
11558 .u80_type,
11559 .u128_type,
11560 .i128_type,
11561 .u256_type,
11562 .usize_type,
11563 .isize_type,
11564 .c_char_type,
11565 .c_short_type,
11566 .c_ushort_type,
11567 .c_int_type,
11568 .c_uint_type,
11569 .c_long_type,
11570 .c_ulong_type,
11571 .c_longlong_type,
11572 .c_ulonglong_type,
11573 .c_longdouble_type,
11574 .f16_type,
11575 .f32_type,
11576 .f64_type,
11577 .f80_type,
11578 .f128_type,
11579 .anyopaque_type,
11580 .bool_type,
11581 .void_type,
11582 .type_type,
11583 .anyerror_type,
11584 .comptime_int_type,
11585 .comptime_float_type,
11586 .noreturn_type,
11587 .anyframe_type,
11588 .null_type,
11589 .undefined_type,
11590 .enum_literal_type,
11591 .ptr_usize_type,
11592 .ptr_const_comptime_int_type,
11593 .manyptr_u8_type,
11594 .manyptr_const_u8_type,
11595 .manyptr_const_u8_sentinel_0_type,
11596 .manyptr_const_slice_const_u8_type,
11597 .slice_const_u8_type,
11598 .slice_const_u8_sentinel_0_type,
11599 .slice_const_slice_const_u8_type,
11600 .optional_type_type,
11601 .manyptr_const_type_type,
11602 .slice_const_type_type,
11603 .vector_8_i8_type,
11604 .vector_16_i8_type,
11605 .vector_32_i8_type,
11606 .vector_64_i8_type,
11607 .vector_1_u8_type,
11608 .vector_2_u8_type,
11609 .vector_4_u8_type,
11610 .vector_8_u8_type,
11611 .vector_16_u8_type,
11612 .vector_32_u8_type,
11613 .vector_64_u8_type,
11614 .vector_2_i16_type,
11615 .vector_4_i16_type,
11616 .vector_8_i16_type,
11617 .vector_16_i16_type,
11618 .vector_32_i16_type,
11619 .vector_4_u16_type,
11620 .vector_8_u16_type,
11621 .vector_16_u16_type,
11622 .vector_32_u16_type,
11623 .vector_2_i32_type,
11624 .vector_4_i32_type,
11625 .vector_8_i32_type,
11626 .vector_16_i32_type,
11627 .vector_4_u32_type,
11628 .vector_8_u32_type,
11629 .vector_16_u32_type,
11630 .vector_2_i64_type,
11631 .vector_4_i64_type,
11632 .vector_8_i64_type,
11633 .vector_2_u64_type,
11634 .vector_4_u64_type,
11635 .vector_8_u64_type,
11636 .vector_1_u128_type,
11637 .vector_2_u128_type,
11638 .vector_1_u256_type,
11639 .vector_4_f16_type,
11640 .vector_8_f16_type,
11641 .vector_16_f16_type,
11642 .vector_32_f16_type,
11643 .vector_2_f32_type,
11644 .vector_4_f32_type,
11645 .vector_8_f32_type,
11646 .vector_16_f32_type,
11647 .vector_2_f64_type,
11648 .vector_4_f64_type,
11649 .vector_8_f64_type,
11650 .optional_noreturn_type,
11651 .anyerror_void_error_union_type,
11652 .adhoc_inferred_error_set_type,
11653 .generic_poison_type,
11654 .empty_tuple_type,
11655 => .type_type,
11656
11657 .undef => .undefined_type,
11658 .zero, .one, .negative_one => .comptime_int_type,
11659 .undef_usize, .zero_usize, .one_usize => .usize_type,
11660 .undef_u1, .zero_u1, .one_u1 => .u1_type,
11661 .zero_u8, .one_u8, .four_u8 => .u8_type,
11662 .void_value => .void_type,
11663 .unreachable_value => .noreturn_type,
11664 .null_value => .null_type,
11665 .undef_bool, .bool_true, .bool_false => .bool_type,
11666 .empty_tuple => .empty_tuple_type,
11667
11668 // This optimization on tags is needed so that indexToKey can call
11669 // typeOf without being recursive.
11670 _ => {
11671 const unwrapped_index = index.unwrap(ip);
11672 const item = unwrapped_index.getItem(ip);
11673 return switch (item.tag) {
11674 .removed => unreachable,
11675
11676 .type_int_signed,
11677 .type_int_unsigned,
11678 .type_array_big,
11679 .type_array_small,
11680 .type_vector,
11681 .type_pointer,
11682 .type_slice,
11683 .type_optional,
11684 .type_anyframe,
11685 .type_error_union,
11686 .type_anyerror_union,
11687 .type_error_set,
11688 .type_inferred_error_set,
11689 .type_tuple,
11690 .type_function,
11691 .type_struct,
11692 .type_struct_packed_auto,
11693 .type_struct_packed_explicit,
11694 .type_struct_packed_auto_defaults,
11695 .type_struct_packed_explicit_defaults,
11696 .type_union,
11697 .type_union_packed_auto,
11698 .type_union_packed_explicit,
11699 .type_enum_auto,
11700 .type_enum_explicit,
11701 .type_enum_nonexhaustive,
11702 .type_opaque,
11703 .type_spirv,
11704 => .type_type,
11705
11706 .undef,
11707 .opt_null,
11708 .only_possible_value,
11709 => @fromBackingInt(@intCast(item.data)),
11710
11711 .simple_type, .simple_value => unreachable, // handled via Index above
11712
11713 inline .ptr_nav,
11714 .ptr_comptime_alloc,
11715 .ptr_uav,
11716 .ptr_uav_aligned,
11717 .ptr_comptime_field,
11718 .ptr_int,
11719 .ptr_eu_payload,
11720 .ptr_opt_payload,
11721 .ptr_elem,
11722 .ptr_field,
11723 .ptr_slice,
11724 .opt_payload,
11725 .error_union_payload,
11726 .int_small,
11727 .error_set_error,
11728 .error_union_error,
11729 .enum_tag,
11730 .@"extern",
11731 .func_decl,
11732 .func_instance,
11733 .func_coerced,
11734 .union_value,
11735 .bytes,
11736 .aggregate,
11737 .repeated,
11738 .bitpack,
11739 => |t| {
11740 const extra_list = unwrapped_index.getExtra(ip);
11741 return @fromBackingInt(@intCast(extra_list.view().items(.@"0")[item.data + std.meta.fieldIndex(t.Payload(), "ty").?]));
11742 },
11743
11744 .int_u8 => .u8_type,
11745 .int_u16 => .u16_type,
11746 .int_u32 => .u32_type,
11747 .int_i32 => .i32_type,
11748 .int_usize => .usize_type,
11749
11750 .int_comptime_int_u32,
11751 .int_comptime_int_i32,
11752 => .comptime_int_type,
11753
11754 // Note these are stored in limbs data, not extra data.
11755 .int_positive,
11756 .int_negative,
11757 => {
11758 const limbs_list = ip.getLocalShared(unwrapped_index.tid).getLimbs();
11759 const int: Int = @bitCast(limbs_list.view().items(.@"0")[item.data..][0..Int.limbs_items_len].*);
11760 return int.ty;
11761 },
11762
11763 .enum_literal => .enum_literal_type,
11764 .float_f16 => .f16_type,
11765 .float_f32 => .f32_type,
11766 .float_f64 => .f64_type,
11767 .float_f80 => .f80_type,
11768 .float_f128 => .f128_type,
11769
11770 .float_c_longdouble_f80,
11771 .float_c_longdouble_f128,
11772 => .c_longdouble_type,
11773
11774 .float_comptime_float => .comptime_float_type,
11775
11776 .memoized_call => unreachable,
11777 };
11778 },
11779
11780 .none => unreachable,
11781 };
11782}
11783
11784/// Assumes that the enum's field indexes equal its value tags.
11785pub fn toEnum(ip: *const InternPool, comptime E: type, i: Index) E {
11786 const int = ip.indexToKey(i).enum_tag.int;
11787 return @fromBackingInt(@intCast(ip.indexToKey(int).int.storage.u64));
11788}
11789
11790pub fn toFunc(ip: *const InternPool, i: Index) Key.Func {
11791 return ip.indexToKey(i).func;
11792}
11793
11794pub fn aggregateTypeLen(ip: *const InternPool, ty: Index) u64 {
11795 return switch (ip.indexToKey(ty)) {
11796 .struct_type => ip.loadStructType(ty).field_types.len,
11797 .tuple_type => |tuple_type| tuple_type.types.len,
11798 .array_type => |array_type| array_type.len,
11799 .vector_type => |vector_type| vector_type.len,
11800 else => unreachable,
11801 };
11802}
11803
11804pub fn aggregateTypeLenIncludingSentinel(ip: *const InternPool, ty: Index) u64 {
11805 return switch (ip.indexToKey(ty)) {
11806 .struct_type => ip.loadStructType(ty).field_types.len,
11807 .tuple_type => |tuple_type| tuple_type.types.len,
11808 .array_type => |array_type| array_type.lenIncludingSentinel(),
11809 .vector_type => |vector_type| vector_type.len,
11810 else => unreachable,
11811 };
11812}
11813
11814pub fn funcTypeReturnType(ip: *const InternPool, ty: Index) Index {
11815 const unwrapped_ty = ty.unwrap(ip);
11816 const ty_extra = unwrapped_ty.getExtra(ip);
11817 const ty_item = unwrapped_ty.getItem(ip);
11818 const child_extra, const child_item = switch (ty_item.tag) {
11819 .type_pointer => child: {
11820 const child_index: Index = @fromBackingInt(@intCast(ty_extra.view().items(.@"0")[
11821 ty_item.data + std.meta.fieldIndex(Tag.TypePointer, "child").?
11822 ]));
11823 const unwrapped_child = child_index.unwrap(ip);
11824 break :child .{ unwrapped_child.getExtra(ip), unwrapped_child.getItem(ip) };
11825 },
11826 .type_function => .{ ty_extra, ty_item },
11827 else => unreachable,
11828 };
11829 assert(child_item.tag == .type_function);
11830 return @fromBackingInt(@intCast(child_extra.view().items(.@"0")[
11831 child_item.data + std.meta.fieldIndex(Tag.TypeFunction, "return_type").?
11832 ]));
11833}
11834
11835pub fn isUndef(ip: *const InternPool, val: Index) bool {
11836 return val.unwrap(ip).getTag(ip) == .undef;
11837}
11838
11839pub fn getBackingAddrTag(ip: *const InternPool, val: Index) ?Key.Ptr.BaseAddr.Tag {
11840 var base = val;
11841 while (true) {
11842 const unwrapped_base = base.unwrap(ip);
11843 const base_item = unwrapped_base.getItem(ip);
11844 switch (base_item.tag) {
11845 .ptr_nav => return .nav,
11846 .ptr_comptime_alloc => return .comptime_alloc,
11847 .ptr_uav,
11848 .ptr_uav_aligned,
11849 => return .uav,
11850 .ptr_comptime_field => return .comptime_field,
11851 .ptr_int => return .int,
11852 inline .ptr_eu_payload,
11853 .ptr_opt_payload,
11854 .ptr_elem,
11855 .ptr_field,
11856 => |tag| base = @fromBackingInt(@intCast(unwrapped_base.getExtra(ip).view().items(.@"0")[
11857 base_item.data + std.meta.fieldIndex(tag.Payload(), "base").?
11858 ])),
11859 inline .ptr_slice => |tag| base = @fromBackingInt(@intCast(unwrapped_base.getExtra(ip).view().items(.@"0")[
11860 base_item.data + std.meta.fieldIndex(tag.Payload(), "ptr").?
11861 ])),
11862 else => return null,
11863 }
11864 }
11865}
11866
11867/// This is a particularly hot function, so we operate directly on encodings
11868/// rather than the more straightforward implementation of calling `indexToKey`.
11869/// Asserts `index` is not `.generic_poison_type`.
11870pub fn zigTypeTag(ip: *const InternPool, index: Index) std.lang.TypeId {
11871 return switch (index) {
11872 .u0_type,
11873 .u1_type,
11874 .u8_type,
11875 .i8_type,
11876 .u16_type,
11877 .i16_type,
11878 .u29_type,
11879 .u32_type,
11880 .i32_type,
11881 .u64_type,
11882 .i64_type,
11883 .u80_type,
11884 .u128_type,
11885 .i128_type,
11886 .u256_type,
11887 .usize_type,
11888 .isize_type,
11889 .c_char_type,
11890 .c_short_type,
11891 .c_ushort_type,
11892 .c_int_type,
11893 .c_uint_type,
11894 .c_long_type,
11895 .c_ulong_type,
11896 .c_longlong_type,
11897 .c_ulonglong_type,
11898 => .int,
11899
11900 .c_longdouble_type,
11901 .f16_type,
11902 .f32_type,
11903 .f64_type,
11904 .f80_type,
11905 .f128_type,
11906 => .float,
11907
11908 .anyopaque_type => .@"opaque",
11909 .bool_type => .bool,
11910 .void_type => .void,
11911 .type_type => .type,
11912 .anyerror_type, .adhoc_inferred_error_set_type => .error_set,
11913 .comptime_int_type => .comptime_int,
11914 .comptime_float_type => .comptime_float,
11915 .noreturn_type => .noreturn,
11916 .anyframe_type => .@"anyframe",
11917 .null_type => .null,
11918 .undefined_type => .undefined,
11919 .enum_literal_type => .enum_literal,
11920
11921 .ptr_usize_type,
11922 .ptr_const_comptime_int_type,
11923 .manyptr_u8_type,
11924 .manyptr_const_u8_type,
11925 .manyptr_const_u8_sentinel_0_type,
11926 .manyptr_const_slice_const_u8_type,
11927 .slice_const_u8_type,
11928 .slice_const_u8_sentinel_0_type,
11929 .slice_const_slice_const_u8_type,
11930 .manyptr_const_type_type,
11931 .slice_const_type_type,
11932 => .pointer,
11933
11934 .vector_8_i8_type,
11935 .vector_16_i8_type,
11936 .vector_32_i8_type,
11937 .vector_64_i8_type,
11938 .vector_1_u8_type,
11939 .vector_2_u8_type,
11940 .vector_4_u8_type,
11941 .vector_8_u8_type,
11942 .vector_16_u8_type,
11943 .vector_32_u8_type,
11944 .vector_64_u8_type,
11945 .vector_2_i16_type,
11946 .vector_4_i16_type,
11947 .vector_8_i16_type,
11948 .vector_16_i16_type,
11949 .vector_32_i16_type,
11950 .vector_4_u16_type,
11951 .vector_8_u16_type,
11952 .vector_16_u16_type,
11953 .vector_32_u16_type,
11954 .vector_2_i32_type,
11955 .vector_4_i32_type,
11956 .vector_8_i32_type,
11957 .vector_16_i32_type,
11958 .vector_4_u32_type,
11959 .vector_8_u32_type,
11960 .vector_16_u32_type,
11961 .vector_2_i64_type,
11962 .vector_4_i64_type,
11963 .vector_8_i64_type,
11964 .vector_2_u64_type,
11965 .vector_4_u64_type,
11966 .vector_8_u64_type,
11967 .vector_1_u128_type,
11968 .vector_2_u128_type,
11969 .vector_1_u256_type,
11970 .vector_4_f16_type,
11971 .vector_8_f16_type,
11972 .vector_16_f16_type,
11973 .vector_32_f16_type,
11974 .vector_2_f32_type,
11975 .vector_4_f32_type,
11976 .vector_8_f32_type,
11977 .vector_16_f32_type,
11978 .vector_2_f64_type,
11979 .vector_4_f64_type,
11980 .vector_8_f64_type,
11981 => .vector,
11982
11983 .optional_type_type => .optional,
11984 .optional_noreturn_type => .optional,
11985 .anyerror_void_error_union_type => .error_union,
11986 .empty_tuple_type => .@"struct",
11987
11988 .generic_poison_type => unreachable,
11989
11990 // values, not types
11991 .undef => unreachable,
11992 .undef_bool => unreachable,
11993 .undef_usize => unreachable,
11994 .undef_u1 => unreachable,
11995 .zero => unreachable,
11996 .zero_usize => unreachable,
11997 .zero_u1 => unreachable,
11998 .zero_u8 => unreachable,
11999 .one => unreachable,
12000 .one_usize => unreachable,
12001 .one_u1 => unreachable,
12002 .one_u8 => unreachable,
12003 .four_u8 => unreachable,
12004 .negative_one => unreachable,
12005 .void_value => unreachable,
12006 .unreachable_value => unreachable,
12007 .null_value => unreachable,
12008 .bool_true => unreachable,
12009 .bool_false => unreachable,
12010 .empty_tuple => unreachable,
12011
12012 _ => switch (index.unwrap(ip).getTag(ip)) {
12013 .removed => unreachable,
12014
12015 .type_int_signed,
12016 .type_int_unsigned,
12017 => .int,
12018
12019 .type_array_big,
12020 .type_array_small,
12021 => .array,
12022
12023 .type_vector => .vector,
12024
12025 .type_pointer,
12026 .type_slice,
12027 => .pointer,
12028
12029 .type_optional => .optional,
12030 .type_anyframe => .@"anyframe",
12031
12032 .type_error_union,
12033 .type_anyerror_union,
12034 => .error_union,
12035
12036 .type_error_set,
12037 .type_inferred_error_set,
12038 => .error_set,
12039
12040 .simple_type => unreachable, // handled via Index tag above
12041
12042 .type_tuple => .@"struct",
12043
12044 .type_struct,
12045 .type_struct_packed_auto,
12046 .type_struct_packed_explicit,
12047 .type_struct_packed_auto_defaults,
12048 .type_struct_packed_explicit_defaults,
12049 => .@"struct",
12050 .type_union,
12051 .type_union_packed_auto,
12052 .type_union_packed_explicit,
12053 => .@"union",
12054 .type_enum_auto,
12055 .type_enum_explicit,
12056 .type_enum_nonexhaustive,
12057 => .@"enum",
12058 .type_opaque,
12059 => .@"opaque",
12060
12061 .type_spirv => .spirv,
12062
12063 .type_function => .@"fn",
12064
12065 // values, not types
12066 .undef,
12067 .simple_value,
12068 .ptr_nav,
12069 .ptr_comptime_alloc,
12070 .ptr_uav,
12071 .ptr_uav_aligned,
12072 .ptr_comptime_field,
12073 .ptr_int,
12074 .ptr_eu_payload,
12075 .ptr_opt_payload,
12076 .ptr_elem,
12077 .ptr_field,
12078 .ptr_slice,
12079 .opt_payload,
12080 .opt_null,
12081 .int_u8,
12082 .int_u16,
12083 .int_u32,
12084 .int_i32,
12085 .int_usize,
12086 .int_comptime_int_u32,
12087 .int_comptime_int_i32,
12088 .int_small,
12089 .int_positive,
12090 .int_negative,
12091 .error_set_error,
12092 .error_union_error,
12093 .error_union_payload,
12094 .enum_literal,
12095 .enum_tag,
12096 .float_f16,
12097 .float_f32,
12098 .float_f64,
12099 .float_f80,
12100 .float_f128,
12101 .float_c_longdouble_f80,
12102 .float_c_longdouble_f128,
12103 .float_comptime_float,
12104 .@"extern",
12105 .func_decl,
12106 .func_instance,
12107 .func_coerced,
12108 .only_possible_value,
12109 .union_value,
12110 .bytes,
12111 .aggregate,
12112 .repeated,
12113 .bitpack,
12114 // memoization, not types
12115 .memoized_call,
12116 => unreachable,
12117 },
12118 .none => unreachable, // special tag
12119 };
12120}
12121
12122pub fn isFuncBody(ip: *const InternPool, func: Index) bool {
12123 return switch (func.unwrap(ip).getTag(ip)) {
12124 .func_decl, .func_instance, .func_coerced => true,
12125 else => false,
12126 };
12127}
12128
12129fn funcAnalysisPtr(ip: *const InternPool, func: Index) *FuncAnalysis {
12130 const unwrapped_func = func.unwrap(ip);
12131 const extra = unwrapped_func.getExtra(ip);
12132 const item = unwrapped_func.getItem(ip);
12133 const extra_index = switch (item.tag) {
12134 .func_decl => item.data + std.meta.fieldIndex(Tag.FuncDecl, "analysis").?,
12135 .func_instance => item.data + std.meta.fieldIndex(Tag.FuncInstance, "analysis").?,
12136 .func_coerced => {
12137 const extra_index = item.data + std.meta.fieldIndex(Tag.FuncCoerced, "func").?;
12138 const coerced_func_index: Index = @fromBackingInt(@intCast(extra.view().items(.@"0")[extra_index]));
12139 const unwrapped_coerced_func = coerced_func_index.unwrap(ip);
12140 const coerced_func_item = unwrapped_coerced_func.getItem(ip);
12141 return @ptrCast(&unwrapped_coerced_func.getExtra(ip).view().items(.@"0")[
12142 switch (coerced_func_item.tag) {
12143 .func_decl => coerced_func_item.data + std.meta.fieldIndex(Tag.FuncDecl, "analysis").?,
12144 .func_instance => coerced_func_item.data + std.meta.fieldIndex(Tag.FuncInstance, "analysis").?,
12145 else => unreachable,
12146 }
12147 ]);
12148 },
12149 else => unreachable,
12150 };
12151 return @ptrCast(&extra.view().items(.@"0")[extra_index]);
12152}
12153
12154pub fn funcAnalysisUnordered(ip: *const InternPool, func: Index) FuncAnalysis {
12155 return @atomicLoad(FuncAnalysis, ip.funcAnalysisPtr(func), .unordered);
12156}
12157
12158pub fn funcSetHasErrorTrace(ip: *InternPool, io: Io, func: Index, has_error_trace: bool) void {
12159 const unwrapped_func = func.unwrap(ip);
12160 const extra_mutex = &ip.getLocal(unwrapped_func.tid).mutate.extra.mutex;
12161 extra_mutex.lockUncancelable(io);
12162 defer extra_mutex.unlock(io);
12163
12164 const analysis_ptr = ip.funcAnalysisPtr(func);
12165 var analysis = analysis_ptr.*;
12166 analysis.has_error_trace = has_error_trace;
12167 @atomicStore(FuncAnalysis, analysis_ptr, analysis, .release);
12168}
12169
12170pub fn funcSetDisableInstrumentation(ip: *InternPool, io: Io, func: Index) void {
12171 const unwrapped_func = func.unwrap(ip);
12172 const extra_mutex = &ip.getLocal(unwrapped_func.tid).mutate.extra.mutex;
12173 extra_mutex.lockUncancelable(io);
12174 defer extra_mutex.unlock(io);
12175
12176 const analysis_ptr = ip.funcAnalysisPtr(func);
12177 var analysis = analysis_ptr.*;
12178 analysis.disable_instrumentation = true;
12179 @atomicStore(FuncAnalysis, analysis_ptr, analysis, .release);
12180}
12181
12182pub fn funcSetDisableIntrinsics(ip: *InternPool, io: Io, func: Index) void {
12183 const unwrapped_func = func.unwrap(ip);
12184 const extra_mutex = &ip.getLocal(unwrapped_func.tid).mutate.extra.mutex;
12185 extra_mutex.lockUncancelable(io);
12186 defer extra_mutex.unlock(io);
12187
12188 const analysis_ptr = ip.funcAnalysisPtr(func);
12189 var analysis = analysis_ptr.*;
12190 analysis.disable_intrinsics = true;
12191 @atomicStore(FuncAnalysis, analysis_ptr, analysis, .release);
12192}
12193
12194pub fn funcZirBodyInst(ip: *const InternPool, func: Index) TrackedInst.Index {
12195 const unwrapped_func = func.unwrap(ip);
12196 const item = unwrapped_func.getItem(ip);
12197 const item_extra = unwrapped_func.getExtra(ip);
12198 const zir_body_inst_field_index = std.meta.fieldIndex(Tag.FuncDecl, "zir_body_inst").?;
12199 switch (item.tag) {
12200 .func_decl => return @fromBackingInt(@intCast(item_extra.view().items(.@"0")[item.data + zir_body_inst_field_index])),
12201 .func_instance => {
12202 const generic_owner_field_index = std.meta.fieldIndex(Tag.FuncInstance, "generic_owner").?;
12203 const func_decl_index: Index = @fromBackingInt(@intCast(item_extra.view().items(.@"0")[item.data + generic_owner_field_index]));
12204 const unwrapped_func_decl = func_decl_index.unwrap(ip);
12205 const func_decl_item = unwrapped_func_decl.getItem(ip);
12206 const func_decl_extra = unwrapped_func_decl.getExtra(ip);
12207 assert(func_decl_item.tag == .func_decl);
12208 return @fromBackingInt(@intCast(func_decl_extra.view().items(.@"0")[func_decl_item.data + zir_body_inst_field_index]));
12209 },
12210 .func_coerced => {
12211 const uncoerced_func_index: Index = @fromBackingInt(@intCast(item_extra.view().items(.@"0")[
12212 item.data + std.meta.fieldIndex(Tag.FuncCoerced, "func").?
12213 ]));
12214 return ip.funcZirBodyInst(uncoerced_func_index);
12215 },
12216 else => unreachable,
12217 }
12218}
12219
12220pub fn iesFuncIndex(ip: *const InternPool, ies_index: Index) Index {
12221 const item = ies_index.unwrap(ip).getItem(ip);
12222 assert(item.tag == .type_inferred_error_set);
12223 const func_index: Index = @fromBackingInt(@intCast(item.data));
12224 switch (func_index.unwrap(ip).getTag(ip)) {
12225 .func_decl, .func_instance => {},
12226 else => unreachable, // assertion failed
12227 }
12228 return func_index;
12229}
12230
12231/// Returns a mutable pointer to the resolved error set type of an inferred
12232/// error set function. The returned pointer is invalidated when anything is
12233/// added to `ip`.
12234fn funcIesResolvedPtr(ip: *const InternPool, func_index: Index) *Index {
12235 assert(ip.funcAnalysisUnordered(func_index).inferred_error_set);
12236 const unwrapped_func = func_index.unwrap(ip);
12237 const func_extra = unwrapped_func.getExtra(ip);
12238 const func_item = unwrapped_func.getItem(ip);
12239 const extra_index = switch (func_item.tag) {
12240 .func_decl => func_item.data + @typeInfo(Tag.FuncDecl).@"struct".field_names.len,
12241 .func_instance => func_item.data + @typeInfo(Tag.FuncInstance).@"struct".field_names.len,
12242 .func_coerced => {
12243 const uncoerced_func_index: Index = @fromBackingInt(@intCast(func_extra.view().items(.@"0")[
12244 func_item.data + std.meta.fieldIndex(Tag.FuncCoerced, "func").?
12245 ]));
12246 const unwrapped_uncoerced_func = uncoerced_func_index.unwrap(ip);
12247 const uncoerced_func_item = unwrapped_uncoerced_func.getItem(ip);
12248 return @ptrCast(&unwrapped_uncoerced_func.getExtra(ip).view().items(.@"0")[
12249 switch (uncoerced_func_item.tag) {
12250 .func_decl => uncoerced_func_item.data + @typeInfo(Tag.FuncDecl).@"struct".field_names.len,
12251 .func_instance => uncoerced_func_item.data + @typeInfo(Tag.FuncInstance).@"struct".field_names.len,
12252 else => unreachable,
12253 }
12254 ]);
12255 },
12256 else => unreachable,
12257 };
12258 return @ptrCast(&func_extra.view().items(.@"0")[extra_index]);
12259}
12260
12261pub fn funcIesResolvedUnordered(ip: *const InternPool, index: Index) Index {
12262 return @atomicLoad(Index, ip.funcIesResolvedPtr(index), .unordered);
12263}
12264
12265pub fn funcSetIesResolved(ip: *InternPool, io: Io, index: Index, ies: Index) void {
12266 const unwrapped_func = index.unwrap(ip);
12267 const extra_mutex = &ip.getLocal(unwrapped_func.tid).mutate.extra.mutex;
12268 extra_mutex.lockUncancelable(io);
12269 defer extra_mutex.unlock(io);
12270
12271 @atomicStore(Index, ip.funcIesResolvedPtr(index), ies, .release);
12272}
12273
12274pub fn funcDeclInfo(ip: *const InternPool, index: Index) Key.Func {
12275 const unwrapped_index = index.unwrap(ip);
12276 const item = unwrapped_index.getItem(ip);
12277 assert(item.tag == .func_decl);
12278 return extraFuncDecl(unwrapped_index.tid, unwrapped_index.getExtra(ip), item.data);
12279}
12280
12281pub fn funcTypeParamsLen(ip: *const InternPool, index: Index) u32 {
12282 const unwrapped_index = index.unwrap(ip);
12283 const extra_list = unwrapped_index.getExtra(ip);
12284 const item = unwrapped_index.getItem(ip);
12285 assert(item.tag == .type_function);
12286 return extra_list.view().items(.@"0")[item.data + std.meta.fieldIndex(Tag.TypeFunction, "params_len").?];
12287}
12288
12289pub fn unwrapCoercedFunc(ip: *const InternPool, index: Index) Index {
12290 const unwrapped_index = index.unwrap(ip);
12291 const item = unwrapped_index.getItem(ip);
12292 return switch (item.tag) {
12293 .func_coerced => @fromBackingInt(@intCast(unwrapped_index.getExtra(ip).view().items(.@"0")[
12294 item.data + std.meta.fieldIndex(Tag.FuncCoerced, "func").?
12295 ])),
12296 .func_instance, .func_decl => index,
12297 else => unreachable,
12298 };
12299}
12300
12301/// Puts `name` into `names_slice` at the next index (that being the current length of `map`).
12302/// Also inserts the name into `map`. If there is an existing field with this name, its index
12303/// is returned. Otherwise, `null` is returned.
12304pub fn addFieldName(
12305 ip: *InternPool,
12306 names: NullTerminatedString.Slice,
12307 map: MapIndex,
12308 name: NullTerminatedString,
12309) ?u32 {
12310 const m = map.get(ip);
12311 const field_idx = m.count();
12312 const names_slice = names.get(ip);
12313 names_slice[field_idx] = name;
12314 const adapter: NullTerminatedString.Adapter = .{ .strings = names_slice[0..field_idx] };
12315 const gop = m.getOrPutAssumeCapacityAdapted(name, adapter);
12316 if (gop.found_existing) return @intCast(gop.index);
12317 assert(gop.index == field_idx);
12318 return null;
12319}
12320
12321/// Like `addFieldName`, but instead of adding a field name to a struct, union, or enum, adds a
12322/// field tag value for an enum.
12323pub fn addFieldTagValue(
12324 ip: *InternPool,
12325 values: Index.Slice,
12326 map: MapIndex,
12327 value: Index,
12328) ?u32 {
12329 const m = map.get(ip);
12330 const field_idx = m.count();
12331 const values_slice = values.get(ip);
12332 values_slice[field_idx] = value;
12333 const adapter: Index.Adapter = .{ .indexes = values_slice[0..field_idx] };
12334 const gop = m.getOrPutAssumeCapacityAdapted(value, adapter);
12335 if (gop.found_existing) return @intCast(gop.index);
12336 assert(gop.index == field_idx);
12337 return null;
12338}
12339
12340/// Used only by `get` for pointer values, and mainly intended to use `Tag.ptr_uav`
12341/// encoding instead of `Tag.ptr_uav_aligned` when possible.
12342fn ptrsHaveSameAlignment(ip: *InternPool, a_ty: Index, a_info: Key.PtrType, b_ty: Index) bool {
12343 if (a_ty == b_ty) return true;
12344 const b_info = ip.indexToKey(b_ty).ptr_type;
12345 return a_info.flags.alignment == b_info.flags.alignment and
12346 (a_info.child == b_info.child or a_info.flags.alignment != .none);
12347}
12348
12349const GlobalErrorSet = struct {
12350 shared: struct {
12351 names: Names,
12352 map: Shard.Map(GlobalErrorSet.Index),
12353 } align(std.atomic.cache_line),
12354 mutate: struct {
12355 names: Local.ListMutate,
12356 map: struct { mutex: Io.Mutex },
12357 } align(std.atomic.cache_line),
12358
12359 const Names = Local.List(struct { NullTerminatedString });
12360
12361 const empty: GlobalErrorSet = .{
12362 .shared = .{
12363 .names = .empty,
12364 .map = .empty,
12365 },
12366 .mutate = .{
12367 .names = .empty,
12368 .map = .{ .mutex = .init },
12369 },
12370 };
12371
12372 const Index = enum(Zcu.ErrorInt) {
12373 none = 0,
12374 _,
12375 };
12376
12377 /// Not thread-safe, may only be called from the main thread.
12378 pub fn getNamesFromMainThread(ges: *const GlobalErrorSet) []const NullTerminatedString {
12379 const len = ges.mutate.names.len;
12380 return if (len > 0) ges.shared.names.view().items(.@"0")[0..len] else &.{};
12381 }
12382
12383 fn getErrorValue(
12384 ges: *GlobalErrorSet,
12385 gpa: Allocator,
12386 io: Io,
12387 arena_state: *std.heap.ArenaAllocator.State,
12388 name: NullTerminatedString,
12389 ) Allocator.Error!GlobalErrorSet.Index {
12390 if (name == .empty) return .none;
12391 const hash = std.hash.int(@backingInt(name));
12392 var map = ges.shared.map.acquire();
12393 const Map = @TypeOf(map);
12394 var map_mask = map.header().mask();
12395 const names = ges.shared.names.acquire();
12396 var map_index = hash;
12397 while (true) : (map_index += 1) {
12398 map_index &= map_mask;
12399 const entry = &map.entries[map_index];
12400 const index = entry.acquire();
12401 if (index == .none) break;
12402 if (entry.hash != hash) continue;
12403 if (names.view().items(.@"0")[@backingInt(index) - 1] == name) return index;
12404 }
12405 ges.mutate.map.mutex.lockUncancelable(io);
12406 defer ges.mutate.map.mutex.unlock(io);
12407 if (map.entries != ges.shared.map.entries) {
12408 map = ges.shared.map;
12409 map_mask = map.header().mask();
12410 map_index = hash;
12411 }
12412 while (true) : (map_index += 1) {
12413 map_index &= map_mask;
12414 const entry = &map.entries[map_index];
12415 const index = entry.value;
12416 if (index == .none) break;
12417 if (entry.hash != hash) continue;
12418 if (names.view().items(.@"0")[@backingInt(index) - 1] == name) return index;
12419 }
12420 const mutable_names: Names.Mutable = .{
12421 .gpa = gpa,
12422 .io = io,
12423 .arena = arena_state,
12424 .mutate = &ges.mutate.names,
12425 .list = &ges.shared.names,
12426 };
12427 try mutable_names.ensureUnusedCapacity(1);
12428 const map_header = map.header().*;
12429 if (ges.mutate.names.len < map_header.capacity * 3 / 5) {
12430 mutable_names.appendAssumeCapacity(.{name});
12431 const index: GlobalErrorSet.Index = @fromBackingInt(@intCast(mutable_names.mutate.len));
12432 const entry = &map.entries[map_index];
12433 entry.hash = hash;
12434 entry.release(index);
12435 return index;
12436 }
12437 var arena = arena_state.promote(gpa);
12438 defer arena_state.* = arena.state;
12439 const new_map_capacity = map_header.capacity * 2;
12440 const new_map_buf = try arena.allocator().alignedAlloc(
12441 u8,
12442 .fromByteUnits(Map.alignment),
12443 Map.entries_offset + new_map_capacity * @sizeOf(Map.Entry),
12444 );
12445 const new_map: Map = .{ .entries = @ptrCast(new_map_buf[Map.entries_offset..].ptr) };
12446 new_map.header().* = .{ .capacity = new_map_capacity };
12447 @memset(new_map.entries[0..new_map_capacity], .{ .value = .none, .hash = undefined });
12448 const new_map_mask = new_map.header().mask();
12449 map_index = 0;
12450 while (map_index < map_header.capacity) : (map_index += 1) {
12451 const entry = &map.entries[map_index];
12452 const index = entry.value;
12453 if (index == .none) continue;
12454 const item_hash = entry.hash;
12455 var new_map_index = item_hash;
12456 while (true) : (new_map_index += 1) {
12457 new_map_index &= new_map_mask;
12458 const new_entry = &new_map.entries[new_map_index];
12459 if (new_entry.value != .none) continue;
12460 new_entry.* = .{
12461 .value = index,
12462 .hash = item_hash,
12463 };
12464 break;
12465 }
12466 }
12467 map = new_map;
12468 map_index = hash;
12469 while (true) : (map_index += 1) {
12470 map_index &= new_map_mask;
12471 if (map.entries[map_index].value == .none) break;
12472 }
12473 mutable_names.appendAssumeCapacity(.{name});
12474 const index: GlobalErrorSet.Index = @fromBackingInt(@intCast(mutable_names.mutate.len));
12475 map.entries[map_index] = .{ .value = index, .hash = hash };
12476 ges.shared.map.release(new_map);
12477 return index;
12478 }
12479
12480 fn getErrorValueIfExists(
12481 ges: *const GlobalErrorSet,
12482 name: NullTerminatedString,
12483 ) ?GlobalErrorSet.Index {
12484 if (name == .empty) return .none;
12485 const hash = std.hash.int(@backingInt(name));
12486 const map = ges.shared.map.acquire();
12487 const map_mask = map.header().mask();
12488 const names_items = ges.shared.names.acquire().view().items(.@"0");
12489 var map_index = hash;
12490 while (true) : (map_index += 1) {
12491 map_index &= map_mask;
12492 const entry = &map.entries[map_index];
12493 const index = entry.acquire();
12494 if (index == .none) return null;
12495 if (entry.hash != hash) continue;
12496 if (names_items[@backingInt(index) - 1] == name) return index;
12497 }
12498 }
12499};
12500
12501pub fn getErrorValue(
12502 ip: *InternPool,
12503 gpa: Allocator,
12504 io: Io,
12505 tid: Zcu.PerThread.Id,
12506 name: NullTerminatedString,
12507) Allocator.Error!Zcu.ErrorInt {
12508 return @backingInt(try ip.global_error_set.getErrorValue(gpa, io, &ip.getLocal(tid).mutate.arena, name));
12509}
12510
12511pub fn getErrorValueIfExists(ip: *const InternPool, name: NullTerminatedString) ?Zcu.ErrorInt {
12512 return @backingInt(ip.global_error_set.getErrorValueIfExists(name) orelse return null);
12513}
12514
12515const PackedCallingConvention = packed struct(u18) {
12516 tag: std.lang.CallingConvention.Tag,
12517 /// May be ignored depending on `tag`.
12518 incoming_stack_alignment: Alignment,
12519 /// Interpretation depends on `tag`.
12520 extra: u4,
12521
12522 fn pack(cc: std.lang.CallingConvention) PackedCallingConvention {
12523 return switch (cc) {
12524 inline else => |pl, tag| switch (@TypeOf(pl)) {
12525 void => .{
12526 .tag = tag,
12527 .incoming_stack_alignment = .none, // unused
12528 .extra = 0, // unused
12529 },
12530 std.lang.CallingConvention.CommonOptions => .{
12531 .tag = tag,
12532 .incoming_stack_alignment = .fromByteUnits(pl.incoming_stack_alignment orelse 0),
12533 .extra = 0, // unused
12534 },
12535 std.lang.CallingConvention.X86RegparmOptions => .{
12536 .tag = tag,
12537 .incoming_stack_alignment = .fromByteUnits(pl.incoming_stack_alignment orelse 0),
12538 .extra = pl.register_params,
12539 },
12540 std.lang.CallingConvention.ArcInterruptOptions => .{
12541 .tag = tag,
12542 .incoming_stack_alignment = .fromByteUnits(pl.incoming_stack_alignment orelse 0),
12543 .extra = @backingInt(pl.type),
12544 },
12545 std.lang.CallingConvention.ArmInterruptOptions => .{
12546 .tag = tag,
12547 .incoming_stack_alignment = .fromByteUnits(pl.incoming_stack_alignment orelse 0),
12548 .extra = @backingInt(pl.type),
12549 },
12550 std.lang.CallingConvention.MicroblazeInterruptOptions => .{
12551 .tag = tag,
12552 .incoming_stack_alignment = .fromByteUnits(pl.incoming_stack_alignment orelse 0),
12553 .extra = @backingInt(pl.type),
12554 },
12555 std.lang.CallingConvention.MipsInterruptOptions => .{
12556 .tag = tag,
12557 .incoming_stack_alignment = .fromByteUnits(pl.incoming_stack_alignment orelse 0),
12558 .extra = @backingInt(pl.mode),
12559 },
12560 std.lang.CallingConvention.RiscvInterruptOptions => .{
12561 .tag = tag,
12562 .incoming_stack_alignment = .fromByteUnits(pl.incoming_stack_alignment orelse 0),
12563 .extra = @backingInt(pl.mode),
12564 },
12565 std.lang.CallingConvention.ShInterruptOptions => .{
12566 .tag = tag,
12567 .incoming_stack_alignment = .fromByteUnits(pl.incoming_stack_alignment orelse 0),
12568 .extra = @backingInt(pl.save),
12569 },
12570 std.lang.CallingConvention.SpirvKernelOptions => .{
12571 .tag = tag,
12572 .incoming_stack_alignment = .none,
12573 .extra = 0,
12574 },
12575 std.lang.CallingConvention.SpirvFragmentOptions => .{
12576 .tag = tag,
12577 .incoming_stack_alignment = .none,
12578 .extra = @as(u4, @backingInt(pl.depth_assumption)) << 1 | @intFromBool(pl.pixel_centered_integer),
12579 },
12580 std.lang.CallingConvention.SpirvMeshOptions => .{
12581 .tag = tag,
12582 .incoming_stack_alignment = .none,
12583 .extra = @backingInt(pl.stage_output),
12584 },
12585 else => comptime unreachable,
12586 },
12587 };
12588 }
12589
12590 fn extraLen(cc: PackedCallingConvention) u3 {
12591 return switch (cc.tag) {
12592 .spirv_kernel, .spirv_task => 3,
12593 .spirv_mesh => 5,
12594 else => 0,
12595 };
12596 }
12597
12598 fn unpack(cc: PackedCallingConvention, trailing: []const u32) std.lang.CallingConvention {
12599 return switch (cc.tag) {
12600 inline else => |tag| @unionInit(
12601 std.lang.CallingConvention,
12602 @tagName(tag),
12603 switch (@FieldType(std.lang.CallingConvention, @tagName(tag))) {
12604 void => {},
12605 std.lang.CallingConvention.CommonOptions => .{
12606 .incoming_stack_alignment = cc.incoming_stack_alignment.toByteUnits(),
12607 },
12608 std.lang.CallingConvention.X86RegparmOptions => .{
12609 .incoming_stack_alignment = cc.incoming_stack_alignment.toByteUnits(),
12610 .register_params = @intCast(cc.extra),
12611 },
12612 std.lang.CallingConvention.ArcInterruptOptions => .{
12613 .incoming_stack_alignment = cc.incoming_stack_alignment.toByteUnits(),
12614 .type = @fromBackingInt(@intCast(cc.extra)),
12615 },
12616 std.lang.CallingConvention.ArmInterruptOptions => .{
12617 .incoming_stack_alignment = cc.incoming_stack_alignment.toByteUnits(),
12618 .type = @fromBackingInt(@intCast(cc.extra)),
12619 },
12620 std.lang.CallingConvention.MicroblazeInterruptOptions => .{
12621 .incoming_stack_alignment = cc.incoming_stack_alignment.toByteUnits(),
12622 .type = @fromBackingInt(@intCast(cc.extra)),
12623 },
12624 std.lang.CallingConvention.MipsInterruptOptions => .{
12625 .incoming_stack_alignment = cc.incoming_stack_alignment.toByteUnits(),
12626 .mode = @fromBackingInt(@intCast(cc.extra)),
12627 },
12628 std.lang.CallingConvention.RiscvInterruptOptions => .{
12629 .incoming_stack_alignment = cc.incoming_stack_alignment.toByteUnits(),
12630 .mode = @fromBackingInt(@intCast(cc.extra)),
12631 },
12632 std.lang.CallingConvention.ShInterruptOptions => .{
12633 .incoming_stack_alignment = cc.incoming_stack_alignment.toByteUnits(),
12634 .save = @fromBackingInt(@intCast(cc.extra)),
12635 },
12636 std.lang.CallingConvention.SpirvKernelOptions => .{
12637 .x = trailing[0],
12638 .y = trailing[1],
12639 .z = trailing[2],
12640 },
12641 std.lang.CallingConvention.SpirvFragmentOptions => .{
12642 .pixel_centered_integer = @bitCast(@as(u1, @truncate(cc.extra))),
12643 .depth_assumption = @fromBackingInt(@intCast(@as(u2, @truncate(cc.extra >> 1)))),
12644 },
12645 std.lang.CallingConvention.SpirvMeshOptions => .{
12646 .stage_output = @fromBackingInt(@intCast(cc.extra)),
12647 .max_primitives = trailing[0],
12648 .max_vertices = trailing[1],
12649 .x = trailing[2],
12650 .y = trailing[3],
12651 .z = trailing[4],
12652 },
12653 else => comptime unreachable,
12654 },
12655 ),
12656 };
12657 }
12658};
12659
12660/// Asserts that `struct_type` is a non-packed struct type.
12661/// As well as calling this function, the caller must also populate these arrays:
12662/// * `field_types`
12663/// * `field_aligns`
12664/// * `field_runtime_order`
12665/// * `field_offsets`
12666pub fn resolveStructLayout(
12667 ip: *InternPool,
12668 io: Io,
12669 struct_type: Index,
12670 size: u32,
12671 alignment: Alignment,
12672 class: TypeClass,
12673) void {
12674 const unwrapped_index = struct_type.unwrap(ip);
12675
12676 const local = ip.getLocal(unwrapped_index.tid);
12677 local.mutate.extra.mutex.lockUncancelable(io);
12678 defer local.mutate.extra.mutex.unlock(io);
12679
12680 const extra_items = local.shared.extra.view().items(.@"0");
12681 const item = unwrapped_index.getItem(ip);
12682 assert(item.tag == .type_struct);
12683
12684 extra_items[item.data + std.meta.fieldIndex(Tag.TypeStruct, "size").?] = size;
12685 const flags: *Tag.TypeStruct.Flags = @ptrCast(&extra_items[item.data + std.meta.fieldIndex(Tag.TypeStruct, "flags").?]);
12686 flags.class = class;
12687 flags.alignment = alignment;
12688}
12689
12690/// Asserts that `union_type` is a non-packed union type.
12691/// As well as calling this function, the caller must also populate these arrays:
12692/// * `field_types`
12693/// * `field_aligns`
12694pub fn resolveUnionLayout(
12695 ip: *InternPool,
12696 io: Io,
12697 union_type: Index,
12698 enum_tag_type: Index,
12699 class: TypeClass,
12700 has_runtime_tag: bool,
12701 size: u32,
12702 padding: u32,
12703 alignment: Alignment,
12704) void {
12705 const unwrapped_index = union_type.unwrap(ip);
12706
12707 const local = ip.getLocal(unwrapped_index.tid);
12708 local.mutate.extra.mutex.lockUncancelable(io);
12709 defer local.mutate.extra.mutex.unlock(io);
12710
12711 const extra_items = local.shared.extra.view().items(.@"0");
12712 const item = unwrapped_index.getItem(ip);
12713 assert(item.tag == .type_union);
12714
12715 extra_items[item.data + std.meta.fieldIndex(Tag.TypeUnion, "enum_tag_type").?] = @backingInt(enum_tag_type);
12716 extra_items[item.data + std.meta.fieldIndex(Tag.TypeUnion, "size").?] = size;
12717 extra_items[item.data + std.meta.fieldIndex(Tag.TypeUnion, "padding").?] = padding;
12718 const flags: *Tag.TypeUnion.Flags = @ptrCast(&extra_items[item.data + std.meta.fieldIndex(Tag.TypeUnion, "flags").?]);
12719 flags.class = class;
12720 flags.has_runtime_tag = has_runtime_tag;
12721 flags.alignment = alignment;
12722}
12723
12724/// Asserts that `struct_type` is a packed struct type.
12725pub fn resolvePackedStructLayout(
12726 ip: *InternPool,
12727 io: Io,
12728 struct_type: Index,
12729 backing_int_type: Index,
12730) void {
12731 const unwrapped_index = struct_type.unwrap(ip);
12732
12733 const local = ip.getLocal(unwrapped_index.tid);
12734 local.mutate.extra.mutex.lockUncancelable(io);
12735 defer local.mutate.extra.mutex.unlock(io);
12736
12737 const extra_items = local.shared.extra.view().items(.@"0");
12738 const item = unwrapped_index.getItem(ip);
12739 switch (item.tag) {
12740 .type_struct_packed_auto,
12741 .type_struct_packed_explicit,
12742 .type_struct_packed_auto_defaults,
12743 .type_struct_packed_explicit_defaults,
12744 => {},
12745 else => unreachable,
12746 }
12747
12748 extra_items[item.data + std.meta.fieldIndex(Tag.TypeStructPacked, "backing_int_type").?] = @backingInt(backing_int_type);
12749}
12750
12751/// Asserts that `union_type` is a packed union type.
12752pub fn resolvePackedUnionLayout(
12753 ip: *InternPool,
12754 io: Io,
12755 union_type: Index,
12756 enum_tag_type: Index,
12757 backing_int_type: Index,
12758) void {
12759 const unwrapped_index = union_type.unwrap(ip);
12760
12761 const local = ip.getLocal(unwrapped_index.tid);
12762 local.mutate.extra.mutex.lockUncancelable(io);
12763 defer local.mutate.extra.mutex.unlock(io);
12764
12765 const extra_items = local.shared.extra.view().items(.@"0");
12766 const item = unwrapped_index.getItem(ip);
12767 switch (item.tag) {
12768 .type_union_packed_auto,
12769 .type_union_packed_explicit,
12770 => {},
12771 else => unreachable,
12772 }
12773
12774 extra_items[item.data + std.meta.fieldIndex(Tag.TypeUnionPacked, "enum_tag_type").?] = @backingInt(enum_tag_type);
12775 extra_items[item.data + std.meta.fieldIndex(Tag.TypeUnionPacked, "backing_int_type").?] = @backingInt(backing_int_type);
12776}
12777
12778/// Asserts that `enum_type` is an enum type.
12779pub fn resolveEnumLayout(
12780 ip: *InternPool,
12781 io: Io,
12782 enum_type: Index,
12783 int_tag_type: Index,
12784) void {
12785 const unwrapped_index = enum_type.unwrap(ip);
12786
12787 const local = ip.getLocal(unwrapped_index.tid);
12788 local.mutate.extra.mutex.lockUncancelable(io);
12789 defer local.mutate.extra.mutex.unlock(io);
12790
12791 const extra_items = local.shared.extra.view().items(.@"0");
12792 const item = unwrapped_index.getItem(ip);
12793 switch (item.tag) {
12794 .type_enum_auto,
12795 .type_enum_explicit,
12796 .type_enum_nonexhaustive,
12797 => {},
12798 else => unreachable,
12799 }
12800
12801 extra_items[item.data + std.meta.fieldIndex(Tag.TypeEnum, "int_tag_type").?] = @backingInt(int_tag_type);
12802}
12803
12804/// Sets the "want_layout" flag on the given struct, union, or enum type. Returns true if the flag
12805/// was *not* already set, meaning we have just discovered the first reference to this type's
12806/// layout. This flag is never reset to false, and exists purely as an optimization; for details,
12807/// see doc comments in `LoadedStructType`.
12808pub fn setWantTypeLayout(ip: *InternPool, io: Io, container_type: Index) bool {
12809 const unwrapped_index = container_type.unwrap(ip);
12810
12811 const local = ip.getLocal(unwrapped_index.tid);
12812 local.mutate.extra.mutex.lockUncancelable(io);
12813 defer local.mutate.extra.mutex.unlock(io);
12814
12815 const extra_items = local.shared.extra.view().items(.@"0");
12816 const item = unwrapped_index.getItem(ip);
12817 switch (item.tag) {
12818 .type_struct_packed_auto,
12819 .type_struct_packed_explicit,
12820 .type_struct_packed_auto_defaults,
12821 .type_struct_packed_explicit_defaults,
12822 => {
12823 const bits: *Tag.TypeStructPacked.Bits = @ptrCast(&extra_items[
12824 item.data + std.meta.fieldIndex(Tag.TypeStructPacked, "bits").?
12825 ]);
12826 if (bits.want_layout) {
12827 return false;
12828 } else {
12829 bits.want_layout = true;
12830 return true;
12831 }
12832 },
12833
12834 .type_struct => {
12835 const flags: *Tag.TypeStruct.Flags = @ptrCast(&extra_items[
12836 item.data + std.meta.fieldIndex(Tag.TypeStruct, "flags").?
12837 ]);
12838 if (flags.want_layout) {
12839 return false;
12840 } else {
12841 flags.want_layout = true;
12842 return true;
12843 }
12844 },
12845
12846 .type_union_packed_auto,
12847 .type_union_packed_explicit,
12848 => {
12849 const bits: *Tag.TypeUnionPacked.Bits = @ptrCast(&extra_items[
12850 item.data + std.meta.fieldIndex(Tag.TypeUnionPacked, "bits").?
12851 ]);
12852 if (bits.want_layout) {
12853 return false;
12854 } else {
12855 bits.want_layout = true;
12856 return true;
12857 }
12858 },
12859
12860 .type_union => {
12861 const flags: *Tag.TypeUnion.Flags = @ptrCast(&extra_items[
12862 item.data + std.meta.fieldIndex(Tag.TypeUnion, "flags").?
12863 ]);
12864 if (flags.want_layout) {
12865 return false;
12866 } else {
12867 flags.want_layout = true;
12868 return true;
12869 }
12870 },
12871
12872 .type_enum_auto,
12873 .type_enum_explicit,
12874 .type_enum_nonexhaustive,
12875 => {
12876 const bits: *Tag.TypeEnum.Bits = @ptrCast(&extra_items[
12877 item.data + std.meta.fieldIndex(Tag.TypeEnum, "bits").?
12878 ]);
12879 if (bits.want_layout) {
12880 return false;
12881 } else {
12882 bits.want_layout = true;
12883 return true;
12884 }
12885 },
12886
12887 else => unreachable,
12888 }
12889}
12890
12891/// Like `setWantTypeLayout`, but for runtime analysis of a function body, using the
12892/// `FuncAnalysis.want_runtime_analysis` flag.
12893pub fn setWantRuntimeFnAnalysis(ip: *InternPool, io: Io, func_index: Index) bool {
12894 const unwrapped_index = func_index.unwrap(ip);
12895
12896 const local = ip.getLocal(unwrapped_index.tid);
12897 local.mutate.extra.mutex.lockUncancelable(io);
12898 defer local.mutate.extra.mutex.unlock(io);
12899
12900 const a = funcAnalysisPtr(ip, func_index);
12901 if (a.want_runtime_analysis) {
12902 return false;
12903 } else {
12904 a.want_runtime_analysis = true;
12905 return true;
12906 }
12907}
12908
12909/// Like `setWantTypeLayout`, but for runtime analysis of a `Nav`, using the `Nav.analysis.wanted` flag.
12910pub fn setWantNavAnalysis(ip: *InternPool, io: Io, nav_index: Nav.Index) bool {
12911 const unwrapped = nav_index.unwrap(ip);
12912
12913 const local = ip.getLocal(unwrapped.tid);
12914 local.mutate.extra.mutex.lockUncancelable(io);
12915 defer local.mutate.extra.mutex.unlock(io);
12916
12917 const navs = local.shared.navs.view();
12918
12919 if (navs.items(.analysis_namespace)[unwrapped.index] == .none) {
12920 return false;
12921 }
12922
12923 // Mutate `bits` atomically so that we don't introduce an illegal data race with `getNav`.
12924 const old_bits = @atomicRmw(
12925 Nav.Repr.Bits,
12926 &navs.items(.bits)[unwrapped.index],
12927 .Or,
12928 mask: {
12929 var mask: Nav.Repr.Bits = @bitCast(@as(u16, 0));
12930 mask.want_analysis = true;
12931 break :mask mask;
12932 },
12933 .monotonic,
12934 );
12935 return !old_bits.want_analysis;
12936}