From 7ef1eb1c27754cb0349fdc10db1f02ff2dddd99b Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Thu, 17 Aug 2023 14:25:18 -0700 Subject: InternPool: safer enum API The key changes in this commit are: ```diff - names: []const NullTerminatedString, + names: NullTerminatedString.Slice, - values: []const Index, + values: Index.Slice, ``` Which eliminates the slices from `InternPool.Key.EnumType` and replaces them with structs that contain `start` and `len` indexes. This makes the lifetime of `EnumType` change from expiring with updates to InternPool, to expiring when the InternPool is garbage-collected, which is currently never. This is gearing up for a larger change I started working on locally which moves union types into InternPool. As a bonus, I fixed some unnecessary instances of `@as`. --- src/link/Dwarf.zig | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) (limited to 'src/link') diff --git a/src/link/Dwarf.zig b/src/link/Dwarf.zig index 6b7744644e..b2ca85467c 100644 --- a/src/link/Dwarf.zig +++ b/src/link/Dwarf.zig @@ -388,9 +388,10 @@ pub const DeclState = struct { try ty.print(dbg_info_buffer.writer(), mod); try dbg_info_buffer.append(0); - const enum_type = mod.intern_pool.indexToKey(ty.ip_index).enum_type; - for (enum_type.names, 0..) |field_name_index, field_i| { - const field_name = mod.intern_pool.stringToSlice(field_name_index); + const ip = &mod.intern_pool; + const enum_type = ip.indexToKey(ty.ip_index).enum_type; + for (enum_type.names.get(ip), 0..) |field_name_index, field_i| { + const field_name = ip.stringToSlice(field_name_index); // DW.AT.enumerator try dbg_info_buffer.ensureUnusedCapacity(field_name.len + 2 + @sizeOf(u64)); dbg_info_buffer.appendAssumeCapacity(@intFromEnum(AbbrevKind.enum_variant)); @@ -400,7 +401,7 @@ pub const DeclState = struct { // DW.AT.const_value, DW.FORM.data8 const value: u64 = value: { if (enum_type.values.len == 0) break :value field_i; // auto-numbered - const value = enum_type.values[field_i]; + const value = enum_type.values.get(ip)[field_i]; // TODO do not assume a 64bit enum value - could be bigger. // See https://github.com/ziglang/zig/issues/645 const field_int_val = try value.toValue().intFromEnum(ty, mod); -- cgit v1.2.3