aboutsummaryrefslogtreecommitdiff
path: root/src/type.zig
diff options
context:
space:
mode:
authorAndrew Kelley <andrew@ziglang.org>2023-08-17 14:25:18 -0700
committerAndrew Kelley <andrew@ziglang.org>2023-08-17 18:16:03 -0700
commit7ef1eb1c27754cb0349fdc10db1f02ff2dddd99b (patch)
tree6a76839d8be347648741e17f50d6c70d8313adc3 /src/type.zig
parent8c1329b222ab620d7388d766e9e558baa502ce93 (diff)
downloadzig-7ef1eb1c27754cb0349fdc10db1f02ff2dddd99b.tar.gz
zig-7ef1eb1c27754cb0349fdc10db1f02ff2dddd99b.zip
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`.
Diffstat (limited to 'src/type.zig')
-rw-r--r--src/type.zig12
1 files changed, 7 insertions, 5 deletions
diff --git a/src/type.zig b/src/type.zig
index df39192a63..b82d3723d0 100644
--- a/src/type.zig
+++ b/src/type.zig
@@ -2434,11 +2434,11 @@ pub const Type = struct {
/// resolves field types rather than asserting they are already resolved.
pub fn onePossibleValue(starting_type: Type, mod: *Module) !?Value {
var ty = starting_type;
-
+ const ip = &mod.intern_pool;
while (true) switch (ty.toIntern()) {
.empty_struct_type => return Value.empty_struct,
- else => switch (mod.intern_pool.indexToKey(ty.toIntern())) {
+ else => switch (ip.indexToKey(ty.toIntern())) {
.int_type => |int_type| {
if (int_type.bits == 0) {
return try mod.intValue(ty, 0);
@@ -2619,7 +2619,7 @@ pub const Type = struct {
} });
return only.toValue();
} else {
- return enum_type.values[0].toValue();
+ return enum_type.values.get(ip)[0].toValue();
}
},
else => return null,
@@ -2967,7 +2967,8 @@ pub const Type = struct {
}
pub fn enumFields(ty: Type, mod: *Module) []const InternPool.NullTerminatedString {
- return mod.intern_pool.indexToKey(ty.toIntern()).enum_type.names;
+ const ip = &mod.intern_pool;
+ return ip.indexToKey(ty.toIntern()).enum_type.names.get(ip);
}
pub fn enumFieldCount(ty: Type, mod: *Module) usize {
@@ -2975,7 +2976,8 @@ pub const Type = struct {
}
pub fn enumFieldName(ty: Type, field_index: usize, mod: *Module) InternPool.NullTerminatedString {
- return mod.intern_pool.indexToKey(ty.toIntern()).enum_type.names[field_index];
+ const ip = &mod.intern_pool;
+ return ip.indexToKey(ty.toIntern()).enum_type.names.get(ip)[field_index];
}
pub fn enumFieldIndex(ty: Type, field_name: InternPool.NullTerminatedString, mod: *Module) ?u32 {