authorgravatar for jacobly@ziglang.orgJacob Young <jacobly@ziglang.org> 2023-05-26 03:41:35-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-06-10 20:47:55-07:00
logf2c716187cf486e519482ef014b34f7271cee3cf
treed72154cb4ccf73bc346d05ff4d9e50feecfc5d9b
parent66c43968546e38879a2d4c3f2264e10676deef73

InternPool: fix more crashes


8 files changed, 502 insertions(+), 448 deletions(-)

src/InternPool.zig+148-97
......@@ -650,8 +650,14 @@ pub const Key = union(enum) {
650650 .enum_type => |enum_type| std.hash.autoHash(hasher, enum_type.decl),
651651
652652 .variable => |variable| std.hash.autoHash(hasher, variable.decl),
653 .extern_func => |extern_func| std.hash.autoHash(hasher, extern_func.decl),
654 .func => |func| std.hash.autoHash(hasher, func.index),
653 .extern_func => |extern_func| {
654 std.hash.autoHash(hasher, extern_func.ty);
655 std.hash.autoHash(hasher, extern_func.decl);
656 },
657 .func => |func| {
658 std.hash.autoHash(hasher, func.ty);
659 std.hash.autoHash(hasher, func.index);
660 },
655661
656662 .int => |int| {
657663 // Canonicalize all integers by converting them to BigIntConst.
......@@ -854,11 +860,11 @@ pub const Key = union(enum) {
854860 },
855861 .extern_func => |a_info| {
856862 const b_info = b.extern_func;
857 return a_info.decl == b_info.decl;
863 return a_info.ty == b_info.ty and a_info.decl == b_info.decl;
858864 },
859865 .func => |a_info| {
860866 const b_info = b.func;
861 return a_info.index == b_info.index;
867 return a_info.ty == b_info.ty and a_info.index == b_info.index;
862868 },
863869
864870 .ptr => |a_info| {
......@@ -1340,8 +1346,8 @@ pub const Index = enum(u32) {
13401346 float_c_longdouble_f128: struct { data: *Float128 },
13411347 float_comptime_float: struct { data: *Float128 },
13421348 variable: struct { data: *Variable },
1343 extern_func: struct { data: void },
1344 func: struct { data: void },
1349 extern_func: struct { data: *Key.ExternFunc },
1350 func: struct { data: *Key.Func },
13451351 only_possible_value: DataIsIndex,
13461352 union_value: struct { data: *Key.Union },
13471353 bytes: struct { data: *Bytes },
......@@ -3216,6 +3222,7 @@ pub fn get(ip: *InternPool, gpa: Allocator, key: Key) Allocator.Error!Index {
32163222
32173223 .opt => |opt| {
32183224 assert(ip.isOptionalType(opt.ty));
3225 assert(opt.val == .none or ip.indexToKey(opt.ty).opt_type == ip.typeOf(opt.val));
32193226 ip.items.appendAssumeCapacity(if (opt.val == .none) .{
32203227 .tag = .opt_null,
32213228 .data = @enumToInt(opt.ty),
......@@ -3226,23 +3233,7 @@ pub fn get(ip: *InternPool, gpa: Allocator, key: Key) Allocator.Error!Index {
32263233 },
32273234
32283235 .int => |int| b: {
3229 switch (int.ty) {
3230 .usize_type,
3231 .isize_type,
3232 .c_char_type,
3233 .c_short_type,
3234 .c_ushort_type,
3235 .c_int_type,
3236 .c_uint_type,
3237 .c_long_type,
3238 .c_ulong_type,
3239 .c_longlong_type,
3240 .c_ulonglong_type,
3241 .c_longdouble_type,
3242 .comptime_int_type,
3243 => {},
3244 else => assert(ip.indexToKey(int.ty) == .int_type),
3245 }
3236 assert(ip.isIntegerType(int.ty));
32463237 switch (int.storage) {
32473238 .u64, .i64, .big_int => {},
32483239 .lazy_align, .lazy_size => |lazy_ty| {
......@@ -3425,13 +3416,16 @@ pub fn get(ip: *InternPool, gpa: Allocator, key: Key) Allocator.Error!Index {
34253416 }
34263417 },
34273418
3428 .err => |err| ip.items.appendAssumeCapacity(.{
3429 .tag = .error_set_error,
3430 .data = try ip.addExtra(gpa, err),
3431 }),
3419 .err => |err| {
3420 assert(ip.isErrorSetType(err.ty));
3421 ip.items.appendAssumeCapacity(.{
3422 .tag = .error_set_error,
3423 .data = try ip.addExtra(gpa, err),
3424 });
3425 },
34323426
34333427 .error_union => |error_union| {
3434 assert(ip.indexToKey(error_union.ty) == .error_union_type);
3428 assert(ip.isErrorUnionType(error_union.ty));
34353429 ip.items.appendAssumeCapacity(switch (error_union.val) {
34363430 .err_name => |err_name| .{
34373431 .tag = .error_union_error,
......@@ -3456,9 +3450,8 @@ pub fn get(ip: *InternPool, gpa: Allocator, key: Key) Allocator.Error!Index {
34563450 }),
34573451
34583452 .enum_tag => |enum_tag| {
3459 assert(enum_tag.ty != .none);
3460 assert(enum_tag.int != .none);
3461
3453 assert(ip.isEnumType(enum_tag.ty));
3454 assert(ip.indexToKey(enum_tag.int) == .int);
34623455 ip.items.appendAssumeCapacity(.{
34633456 .tag = .enum_tag,
34643457 .data = try ip.addExtra(gpa, enum_tag),
......@@ -4191,69 +4184,93 @@ pub fn sliceLen(ip: InternPool, i: Index) Index {
41914184/// * identity coercion
41924185/// * int <=> int
41934186/// * int <=> enum
4187/// * enum_literal => enum
41944188/// * ptr <=> ptr
41954189/// * null_value => opt
41964190/// * payload => opt
41974191/// * error set <=> error set
4192/// * error union <=> error union
4193/// * error set => error union
4194/// * payload => error union
4195/// * fn <=> fn
41984196pub fn getCoerced(ip: *InternPool, gpa: Allocator, val: Index, new_ty: Index) Allocator.Error!Index {
41994197 const old_ty = ip.typeOf(val);
42004198 if (old_ty == new_ty) return val;
42014199 switch (ip.indexToKey(val)) {
4202 .int => |int| switch (ip.indexToKey(new_ty)) {
4203 .simple_type => |simple_type| switch (simple_type) {
4204 .usize,
4205 .isize,
4206 .c_char,
4207 .c_short,
4208 .c_ushort,
4209 .c_int,
4210 .c_uint,
4211 .c_long,
4212 .c_ulong,
4213 .c_longlong,
4214 .c_ulonglong,
4215 .comptime_int,
4216 => return getCoercedInts(ip, gpa, int, new_ty),
4217 else => {},
4218 },
4219 .int_type => return getCoercedInts(ip, gpa, int, new_ty),
4220 .enum_type => return ip.get(gpa, .{ .enum_tag = .{
4200 .extern_func => |extern_func| if (ip.isFunctionType(new_ty))
4201 return ip.get(gpa, .{ .extern_func = .{
4202 .ty = new_ty,
4203 .decl = extern_func.decl,
4204 .lib_name = extern_func.lib_name,
4205 } }),
4206 .func => |func| if (ip.isFunctionType(new_ty))
4207 return ip.get(gpa, .{ .func = .{
4208 .ty = new_ty,
4209 .index = func.index,
4210 } }),
4211 .int => |int| if (ip.isIntegerType(new_ty))
4212 return getCoercedInts(ip, gpa, int, new_ty)
4213 else if (ip.isEnumType(new_ty))
4214 return ip.get(gpa, .{ .enum_tag = .{
42214215 .ty = new_ty,
42224216 .int = val,
42234217 } }),
4218 .enum_tag => |enum_tag| if (ip.isIntegerType(new_ty))
4219 return getCoercedInts(ip, gpa, ip.indexToKey(enum_tag.int).int, new_ty),
4220 .enum_literal => |enum_literal| switch (ip.indexToKey(new_ty)) {
4221 .enum_type => |enum_type| {
4222 const index = enum_type.nameIndex(ip, enum_literal).?;
4223 return ip.get(gpa, .{ .enum_tag = .{
4224 .ty = new_ty,
4225 .int = if (enum_type.values.len != 0)
4226 enum_type.values[index]
4227 else
4228 try ip.get(gpa, .{ .int = .{
4229 .ty = enum_type.tag_ty,
4230 .storage = .{ .u64 = index },
4231 } }),
4232 } });
4233 },
42244234 else => {},
42254235 },
4226 .enum_tag => |enum_tag| {
4227 // Assume new_ty is an integer type.
4228 return getCoercedInts(ip, gpa, ip.indexToKey(enum_tag.int).int, new_ty);
4229 },
4230 .ptr => |ptr| switch (ip.indexToKey(new_ty)) {
4231 .ptr_type => return ip.get(gpa, .{ .ptr = .{
4236 .ptr => |ptr| if (ip.isPointerType(new_ty))
4237 return ip.get(gpa, .{ .ptr = .{
42324238 .ty = new_ty,
42334239 .addr = ptr.addr,
4240 .len = ptr.len,
42344241 } }),
4235 else => {},
4236 },
4237 .err => |err| switch (ip.indexToKey(new_ty)) {
4238 .error_set_type, .inferred_error_set_type => return ip.get(gpa, .{ .err = .{
4242 .err => |err| if (ip.isErrorSetType(new_ty))
4243 return ip.get(gpa, .{ .err = .{
42394244 .ty = new_ty,
42404245 .name = err.name,
4246 } })
4247 else if (ip.isErrorUnionType(new_ty))
4248 return ip.get(gpa, .{ .error_union = .{
4249 .ty = new_ty,
4250 .val = .{ .err_name = err.name },
4251 } }),
4252 .error_union => |error_union| if (ip.isErrorUnionType(new_ty))
4253 return ip.get(gpa, .{ .error_union = .{
4254 .ty = new_ty,
4255 .val = error_union.val,
42414256 } }),
4242 else => {},
4243 },
42444257 else => {},
42454258 }
42464259 switch (ip.indexToKey(new_ty)) {
4247 .opt_type => |child_ty| switch (val) {
4260 .opt_type => |child_type| switch (val) {
42484261 .null_value => return ip.get(gpa, .{ .opt = .{
42494262 .ty = new_ty,
42504263 .val = .none,
42514264 } }),
42524265 else => return ip.get(gpa, .{ .opt = .{
42534266 .ty = new_ty,
4254 .val = try ip.getCoerced(gpa, val, child_ty),
4267 .val = try ip.getCoerced(gpa, val, child_type),
42554268 } }),
42564269 },
4270 .error_union_type => |error_union_type| return ip.get(gpa, .{ .error_union = .{
4271 .ty = new_ty,
4272 .val = .{ .payload = try ip.getCoerced(gpa, val, error_union_type.payload_type) },
4273 } }),
42574274 else => {},
42584275 }
42594276 if (std.debug.runtime_safety) {
......@@ -4271,33 +4288,24 @@ pub fn getCoercedInts(ip: *InternPool, gpa: Allocator, int: Key.Int, new_ty: Ind
42714288 // big_int storage, the limbs would be invalidated before they are read.
42724289 // Here we pre-reserve the limbs to ensure that the logic in `addInt` will
42734290 // not use an invalidated limbs pointer.
4274 switch (int.storage) {
4275 .u64 => |x| return ip.get(gpa, .{ .int = .{
4276 .ty = new_ty,
4277 .storage = .{ .u64 = x },
4278 } }),
4279 .i64 => |x| return ip.get(gpa, .{ .int = .{
4280 .ty = new_ty,
4281 .storage = .{ .i64 = x },
4282 } }),
4283
4284 .big_int => |big_int| {
4291 const new_storage: Key.Int.Storage = switch (int.storage) {
4292 .u64, .i64, .lazy_align, .lazy_size => int.storage,
4293 .big_int => |big_int| storage: {
42854294 const positive = big_int.positive;
42864295 const limbs = ip.limbsSliceToIndex(big_int.limbs);
42874296 // This line invalidates the limbs slice, but the indexes computed in the
42884297 // previous line are still correct.
42894298 try reserveLimbs(ip, gpa, @typeInfo(Int).Struct.fields.len + big_int.limbs.len);
4290 return ip.get(gpa, .{ .int = .{
4291 .ty = new_ty,
4292 .storage = .{ .big_int = .{
4293 .limbs = ip.limbsIndexToSlice(limbs),
4294 .positive = positive,
4295 } },
4296 } });
4299 break :storage .{ .big_int = .{
4300 .limbs = ip.limbsIndexToSlice(limbs),
4301 .positive = positive,
4302 } };
42974303 },
4298
4299 .lazy_align, .lazy_size => unreachable,
4300 }
4304 };
4305 return ip.get(gpa, .{ .int = .{
4306 .ty = new_ty,
4307 .storage = new_storage,
4308 } });
43014309}
43024310
43034311pub fn indexToStructType(ip: InternPool, val: Index) Module.Struct.OptionalIndex {
......@@ -4345,25 +4353,68 @@ pub fn indexToInferredErrorSetType(ip: InternPool, val: Index) Module.Fn.Inferre
43454353 return @intToEnum(Module.Fn.InferredErrorSet.Index, datas[@enumToInt(val)]).toOptional();
43464354}
43474355
4348pub fn isPointerType(ip: InternPool, ty: Index) bool {
4349 const tags = ip.items.items(.tag);
4350 if (ty == .none) return false;
4351 return switch (tags[@enumToInt(ty)]) {
4352 .type_pointer, .type_slice => true,
4353 else => false,
4356/// includes .comptime_int_type
4357pub fn isIntegerType(ip: InternPool, ty: Index) bool {
4358 return switch (ty) {
4359 .usize_type,
4360 .isize_type,
4361 .c_char_type,
4362 .c_short_type,
4363 .c_ushort_type,
4364 .c_int_type,
4365 .c_uint_type,
4366 .c_long_type,
4367 .c_ulong_type,
4368 .c_longlong_type,
4369 .c_ulonglong_type,
4370 .c_longdouble_type,
4371 .comptime_int_type,
4372 => true,
4373 else => ip.indexToKey(ty) == .int_type,
4374 };
4375}
4376
4377/// does not include .enum_literal_type
4378pub fn isEnumType(ip: InternPool, ty: Index) bool {
4379 return switch (ty) {
4380 .atomic_order_type,
4381 .atomic_rmw_op_type,
4382 .calling_convention_type,
4383 .address_space_type,
4384 .float_mode_type,
4385 .reduce_op_type,
4386 .call_modifier_type,
4387 => true,
4388 else => ip.indexToKey(ty) == .enum_type,
43544389 };
43554390}
43564391
4392pub fn isFunctionType(ip: InternPool, ty: Index) bool {
4393 return ip.indexToKey(ty) == .func_type;
4394}
4395
4396pub fn isPointerType(ip: InternPool, ty: Index) bool {
4397 return ip.indexToKey(ty) == .ptr_type;
4398}
4399
43574400pub fn isOptionalType(ip: InternPool, ty: Index) bool {
4358 const tags = ip.items.items(.tag);
4359 if (ty == .none) return false;
4360 return tags[@enumToInt(ty)] == .type_optional;
4401 return ip.indexToKey(ty) == .opt_type;
4402}
4403
4404/// includes .inferred_error_set_type
4405pub fn isErrorSetType(ip: InternPool, ty: Index) bool {
4406 return ty == .anyerror_type or switch (ip.indexToKey(ty)) {
4407 .error_set_type, .inferred_error_set_type => true,
4408 else => false,
4409 };
43614410}
43624411
43634412pub fn isInferredErrorSetType(ip: InternPool, ty: Index) bool {
4364 const tags = ip.items.items(.tag);
4365 assert(ty != .none);
4366 return tags[@enumToInt(ty)] == .type_inferred_error_set;
4413 return ip.indexToKey(ty) == .inferred_error_set_type;
4414}
4415
4416pub fn isErrorUnionType(ip: InternPool, ty: Index) bool {
4417 return ip.indexToKey(ty) == .error_union_type;
43674418}
43684419
43694420/// The is only legal because the initializer is not part of the hash.
src/Module.zig+5
......@@ -6699,6 +6699,11 @@ pub fn intern(mod: *Module, key: InternPool.Key) Allocator.Error!InternPool.Inde
66996699 return mod.intern_pool.get(mod.gpa, key);
67006700}
67016701
6702/// Shortcut for calling `intern_pool.getCoerced`.
6703pub fn getCoerced(mod: *Module, val: Value, new_ty: Type) Allocator.Error!Value {
6704 return (try mod.intern_pool.getCoerced(mod.gpa, val.toIntern(), new_ty.toIntern())).toValue();
6705}
6706
67026707pub fn intType(mod: *Module, signedness: std.builtin.Signedness, bits: u16) Allocator.Error!Type {
67036708 const i = try intern(mod, .{ .int_type = .{
67046709 .signedness = signedness,
src/Sema.zig+113-105
......@@ -7821,7 +7821,6 @@ fn resolveGenericInstantiationType(
78217821 const new_func_inst = try child_sema.resolveBody(&child_block, fn_info.param_body, fn_info.param_body_inst);
78227822 const new_func_val = child_sema.resolveConstValue(&child_block, .unneeded, new_func_inst, undefined) catch unreachable;
78237823 const new_func = new_func_val.getFunctionIndex(mod).unwrap().?;
7824 errdefer mod.destroyFunc(new_func);
78257824 assert(new_func == new_module_func);
78267825
78277826 arg_i = 0;
......@@ -10793,7 +10792,7 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError
1079310792 check_range: {
1079410793 if (operand_ty.zigTypeTag(mod) == .Int) {
1079510794 const min_int = try operand_ty.minInt(mod);
10796 const max_int = try operand_ty.maxIntScalar(mod, Type.comptime_int);
10795 const max_int = try operand_ty.maxInt(mod, operand_ty);
1079710796 if (try range_set.spans(min_int, max_int, operand_ty)) {
1079810797 if (special_prong == .@"else") {
1079910798 return sema.fail(
......@@ -11649,7 +11648,7 @@ const RangeSetUnhandledIterator = struct {
1164911648 fn init(sema: *Sema, ty: Type, range_set: RangeSet) !RangeSetUnhandledIterator {
1165011649 const mod = sema.mod;
1165111650 const min = try ty.minInt(mod);
11652 const max = try ty.maxIntScalar(mod, Type.comptime_int);
11651 const max = try ty.maxInt(mod, ty);
1165311652
1165411653 return RangeSetUnhandledIterator{
1165511654 .sema = sema,
......@@ -15964,25 +15963,24 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1596415963 }
1596515964
1596615965 const args_val = v: {
15967 const args_slice_ty = try mod.ptrType(.{
15968 .elem_type = param_info_ty.toIntern(),
15969 .size = .Slice,
15970 .is_const = true,
15966 const new_decl_ty = try mod.arrayType(.{
15967 .len = param_vals.len,
15968 .child = param_info_ty.toIntern(),
1597115969 });
1597215970 const new_decl = try params_anon_decl.finish(
15973 try mod.arrayType(.{
15974 .len = param_vals.len,
15975 .child = param_info_ty.toIntern(),
15976 .sentinel = .none,
15977 }),
15971 new_decl_ty,
1597815972 (try mod.intern(.{ .aggregate = .{
15979 .ty = args_slice_ty.toIntern(),
15973 .ty = new_decl_ty.toIntern(),
1598015974 .storage = .{ .elems = param_vals },
1598115975 } })).toValue(),
1598215976 0, // default alignment
1598315977 );
1598415978 break :v try mod.intern(.{ .ptr = .{
15985 .ty = args_slice_ty.toIntern(),
15979 .ty = (try mod.ptrType(.{
15980 .elem_type = param_info_ty.toIntern(),
15981 .size = .Slice,
15982 .is_const = true,
15983 })).toIntern(),
1598615984 .addr = .{ .decl = new_decl },
1598715985 .len = (try mod.intValue(Type.usize, param_vals.len)).toIntern(),
1598815986 } });
......@@ -16214,7 +16212,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1621416212 };
1621516213 return sema.addConstant(type_info_ty, (try mod.intern(.{ .un = .{
1621616214 .ty = type_info_ty.toIntern(),
16217 .tag = (try mod.enumValueFieldIndex(type_info_tag_ty, @enumToInt(std.builtin.TypeId.Vector))).toIntern(),
16215 .tag = (try mod.enumValueFieldIndex(type_info_tag_ty, @enumToInt(std.builtin.TypeId.Optional))).toIntern(),
1621816216 .val = try mod.intern(.{ .aggregate = .{
1621916217 .ty = optional_field_ty.toIntern(),
1622016218 .storage = .{ .elems = &field_values },
......@@ -16258,7 +16256,6 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1625816256 const new_decl_ty = try mod.arrayType(.{
1625916257 .len = name.len,
1626016258 .child = .u8_type,
16261 .sentinel = .zero_u8,
1626216259 });
1626316260 const new_decl = try anon_decl.finish(
1626416261 new_decl_ty,
......@@ -16269,8 +16266,9 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1626916266 0, // default alignment
1627016267 );
1627116268 break :v try mod.intern(.{ .ptr = .{
16272 .ty = .slice_const_u8_sentinel_0_type,
16269 .ty = .slice_const_u8_type,
1627316270 .addr = .{ .decl = new_decl },
16271 .len = (try mod.intValue(Type.usize, name.len)).toIntern(),
1627416272 } });
1627516273 };
1627616274
......@@ -16386,7 +16384,6 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1638616384 const new_decl_ty = try mod.arrayType(.{
1638716385 .len = name.len,
1638816386 .child = .u8_type,
16389 .sentinel = .zero_u8,
1639016387 });
1639116388 const new_decl = try anon_decl.finish(
1639216389 new_decl_ty,
......@@ -16397,8 +16394,9 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1639716394 0, // default alignment
1639816395 );
1639916396 break :v try mod.intern(.{ .ptr = .{
16400 .ty = .slice_const_u8_sentinel_0_type,
16397 .ty = .slice_const_u8_type,
1640116398 .addr = .{ .decl = new_decl },
16399 .len = (try mod.intValue(Type.usize, name.len)).toIntern(),
1640216400 } });
1640316401 };
1640416402
......@@ -16521,7 +16519,6 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1652116519 const new_decl_ty = try mod.arrayType(.{
1652216520 .len = name.len,
1652316521 .child = .u8_type,
16524 .sentinel = .zero_u8,
1652516522 });
1652616523 const new_decl = try anon_decl.finish(
1652716524 new_decl_ty,
......@@ -16532,8 +16529,9 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1653216529 0, // default alignment
1653316530 );
1653416531 break :v try mod.intern(.{ .ptr = .{
16535 .ty = .slice_const_u8_sentinel_0_type,
16532 .ty = .slice_const_u8_type,
1653616533 .addr = .{ .decl = new_decl },
16534 .len = (try mod.intValue(Type.usize, name.len)).toIntern(),
1653716535 } });
1653816536 };
1653916537
......@@ -16663,12 +16661,10 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1666316661 const struct_type = switch (mod.intern_pool.indexToKey(struct_ty.toIntern())) {
1666416662 .anon_struct_type => |tuple| {
1666516663 struct_field_vals = try gpa.alloc(InternPool.Index, tuple.types.len);
16666 for (
16667 tuple.types,
16668 tuple.values,
16669 struct_field_vals,
16670 0..,
16671 ) |field_ty, field_val, *struct_field_val, i| {
16664 for (struct_field_vals, 0..) |*struct_field_val, i| {
16665 const anon_struct_type = mod.intern_pool.indexToKey(struct_ty.toIntern()).anon_struct_type;
16666 const field_ty = anon_struct_type.types[i];
16667 const field_val = anon_struct_type.values[i];
1667216668 const name_val = v: {
1667316669 var anon_decl = try block.startAnonDecl();
1667416670 defer anon_decl.deinit();
......@@ -16735,7 +16731,6 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1673516731 const new_decl_ty = try mod.arrayType(.{
1673616732 .len = name.len,
1673716733 .child = .u8_type,
16738 .sentinel = .zero_u8,
1673916734 });
1674016735 const new_decl = try anon_decl.finish(
1674116736 new_decl_ty,
......@@ -16746,7 +16741,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1674616741 0, // default alignment
1674716742 );
1674816743 break :v try mod.intern(.{ .ptr = .{
16749 .ty = .slice_const_u8_sentinel_0_type,
16744 .ty = .slice_const_u8_type,
1675016745 .addr = .{ .decl = new_decl },
1675116746 .len = (try mod.intValue(Type.usize, name.len)).toIntern(),
1675216747 } });
......@@ -16975,7 +16970,6 @@ fn typeInfoNamespaceDecls(
1697516970 const new_decl_ty = try mod.arrayType(.{
1697616971 .len = name.len,
1697716972 .child = .u8_type,
16978 .sentinel = .zero_u8,
1697916973 });
1698016974 const new_decl = try anon_decl.finish(
1698116975 new_decl_ty,
......@@ -16986,7 +16980,7 @@ fn typeInfoNamespaceDecls(
1698616980 0, // default alignment
1698716981 );
1698816982 break :v try mod.intern(.{ .ptr = .{
16989 .ty = .slice_const_u8_sentinel_0_type,
16983 .ty = .slice_const_u8_type,
1699016984 .addr = .{ .decl = new_decl },
1699116985 .len = (try mod.intValue(Type.usize, name.len)).toIntern(),
1699216986 } });
......@@ -20404,7 +20398,7 @@ fn zirPtrCast(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air
2040420398 .val = operand_val.toIntern(),
2040520399 } })).toValue());
2040620400 }
20407 return sema.addConstant(aligned_dest_ty, operand_val);
20401 return sema.addConstant(aligned_dest_ty, try mod.getCoerced(operand_val, aligned_dest_ty));
2040820402 }
2040920403
2041020404 try sema.requireRuntimeBlock(block, src, null);
......@@ -22401,7 +22395,7 @@ fn analyzeMinMax(
2240122395 if (std.debug.runtime_safety) {
2240222396 assert(try sema.intFitsInType(val, refined_ty, null));
2240322397 }
22404 cur_minmax = try sema.addConstant(refined_ty, val);
22398 cur_minmax = try sema.addConstant(refined_ty, try mod.getCoerced(val, refined_ty));
2240522399 }
2240622400
2240722401 break :refined refined_ty;
......@@ -22459,8 +22453,8 @@ fn analyzeMinMax(
2245922453 else => unreachable,
2246022454 };
2246122455 const max_val = switch (air_tag) {
22462 .min => try comptime_elem_ty.maxInt(mod, Type.comptime_int), // @min(ct, rt) <= ct
22463 .max => try unrefined_elem_ty.maxInt(mod, Type.comptime_int),
22456 .min => try comptime_elem_ty.maxInt(mod, comptime_elem_ty), // @min(ct, rt) <= ct
22457 .max => try unrefined_elem_ty.maxInt(mod, unrefined_elem_ty),
2246422458 else => unreachable,
2246522459 };
2246622460
......@@ -23356,11 +23350,14 @@ fn zirBuiltinExtern(
2335623350 try mod.declareDeclDependency(sema.owner_decl_index, new_decl_index);
2335723351 try sema.ensureDeclAnalyzed(new_decl_index);
2335823352
23359 const ref = try mod.intern(.{ .ptr = .{
23360 .ty = (try mod.singleConstPtrType(ty)).toIntern(),
23353 return sema.addConstant(ty, try mod.getCoerced((try mod.intern(.{ .ptr = .{
23354 .ty = switch (mod.intern_pool.indexToKey(ty.toIntern())) {
23355 .ptr_type => ty.toIntern(),
23356 .opt_type => |child_type| child_type,
23357 else => unreachable,
23358 },
2336123359 .addr = .{ .decl = new_decl_index },
23362 } });
23363 return sema.addConstant(ty, ref.toValue());
23360 } })).toValue(), ty));
2336423361}
2336523362
2336623363fn zirWorkItem(
......@@ -25887,13 +25884,7 @@ fn coerceExtra(
2588725884 var in_memory_result = try sema.coerceInMemoryAllowed(block, dest_ty, inst_ty, false, target, dest_ty_src, inst_src);
2588825885 if (in_memory_result == .ok) {
2588925886 if (maybe_inst_val) |val| {
25890 if (val.ip_index == .none) {
25891 // Keep the comptime Value representation; take the new type.
25892 return sema.addConstant(dest_ty, val);
25893 } else {
25894 const new_val = try mod.intern_pool.getCoerced(sema.gpa, val.toIntern(), dest_ty.toIntern());
25895 return sema.addConstant(dest_ty, new_val.toValue());
25896 }
25887 return sema.addConstant(dest_ty, try mod.getCoerced(val, dest_ty));
2589725888 }
2589825889 try sema.requireRuntimeBlock(block, inst_src, null);
2589925890 return block.addBitCast(dest_ty, inst);
......@@ -26269,8 +26260,7 @@ fn coerceExtra(
2626926260 if (!opts.report_err) return error.NotCoercible;
2627026261 return sema.fail(block, inst_src, "type '{}' cannot represent integer value '{}'", .{ dest_ty.fmt(sema.mod), val.fmtValue(inst_ty, sema.mod) });
2627126262 }
26272 const new_val = try mod.intern_pool.getCoerced(sema.gpa, val.toIntern(), dest_ty.toIntern());
26273 return try sema.addConstant(dest_ty, new_val.toValue());
26263 return try sema.addConstant(dest_ty, try mod.getCoerced(val, dest_ty));
2627426264 }
2627526265 if (dest_ty.zigTypeTag(mod) == .ComptimeInt) {
2627626266 if (!opts.report_err) return error.NotCoercible;
......@@ -27222,68 +27212,84 @@ fn coerceInMemoryAllowedFns(
2722227212 src_src: LazySrcLoc,
2722327213) !InMemoryCoercionResult {
2722427214 const mod = sema.mod;
27225 const dest_info = mod.typeToFunc(dest_ty).?;
27226 const src_info = mod.typeToFunc(src_ty).?;
2722727215
27228 if (dest_info.is_var_args != src_info.is_var_args) {
27229 return InMemoryCoercionResult{ .fn_var_args = dest_info.is_var_args };
27230 }
27216 {
27217 const dest_info = mod.typeToFunc(dest_ty).?;
27218 const src_info = mod.typeToFunc(src_ty).?;
2723127219
27232 if (dest_info.is_generic != src_info.is_generic) {
27233 return InMemoryCoercionResult{ .fn_generic = dest_info.is_generic };
27234 }
27220 if (dest_info.is_var_args != src_info.is_var_args) {
27221 return InMemoryCoercionResult{ .fn_var_args = dest_info.is_var_args };
27222 }
2723527223
27236 if (dest_info.cc != src_info.cc) {
27237 return InMemoryCoercionResult{ .fn_cc = .{
27238 .actual = src_info.cc,
27239 .wanted = dest_info.cc,
27240 } };
27241 }
27224 if (dest_info.is_generic != src_info.is_generic) {
27225 return InMemoryCoercionResult{ .fn_generic = dest_info.is_generic };
27226 }
2724227227
27243 if (src_info.return_type != .noreturn_type) {
27244 const rt = try sema.coerceInMemoryAllowed(block, dest_info.return_type.toType(), src_info.return_type.toType(), false, target, dest_src, src_src);
27245 if (rt != .ok) {
27246 return InMemoryCoercionResult{ .fn_return_type = .{
27247 .child = try rt.dupe(sema.arena),
27248 .actual = src_info.return_type.toType(),
27249 .wanted = dest_info.return_type.toType(),
27228 if (dest_info.cc != src_info.cc) {
27229 return InMemoryCoercionResult{ .fn_cc = .{
27230 .actual = src_info.cc,
27231 .wanted = dest_info.cc,
2725027232 } };
2725127233 }
27252 }
2725327234
27254 if (dest_info.param_types.len != src_info.param_types.len) {
27255 return InMemoryCoercionResult{ .fn_param_count = .{
27256 .actual = src_info.param_types.len,
27257 .wanted = dest_info.param_types.len,
27258 } };
27235 if (src_info.return_type != .noreturn_type) {
27236 const dest_return_type = dest_info.return_type.toType();
27237 const src_return_type = src_info.return_type.toType();
27238 const rt = try sema.coerceInMemoryAllowed(block, dest_return_type, src_return_type, false, target, dest_src, src_src);
27239 if (rt != .ok) {
27240 return InMemoryCoercionResult{ .fn_return_type = .{
27241 .child = try rt.dupe(sema.arena),
27242 .actual = dest_return_type,
27243 .wanted = src_return_type,
27244 } };
27245 }
27246 }
2725927247 }
2726027248
27261 if (dest_info.noalias_bits != src_info.noalias_bits) {
27262 return InMemoryCoercionResult{ .fn_param_noalias = .{
27263 .actual = src_info.noalias_bits,
27264 .wanted = dest_info.noalias_bits,
27265 } };
27266 }
27249 const params_len = params_len: {
27250 const dest_info = mod.typeToFunc(dest_ty).?;
27251 const src_info = mod.typeToFunc(src_ty).?;
27252
27253 if (dest_info.param_types.len != src_info.param_types.len) {
27254 return InMemoryCoercionResult{ .fn_param_count = .{
27255 .actual = src_info.param_types.len,
27256 .wanted = dest_info.param_types.len,
27257 } };
27258 }
2726727259
27268 for (dest_info.param_types, 0..) |dest_param_ty, i| {
27269 const src_param_ty = src_info.param_types[i].toType();
27260 if (dest_info.noalias_bits != src_info.noalias_bits) {
27261 return InMemoryCoercionResult{ .fn_param_noalias = .{
27262 .actual = src_info.noalias_bits,
27263 .wanted = dest_info.noalias_bits,
27264 } };
27265 }
27266
27267 break :params_len dest_info.param_types.len;
27268 };
27269
27270 for (0..params_len) |param_i| {
27271 const dest_info = mod.typeToFunc(dest_ty).?;
27272 const src_info = mod.typeToFunc(src_ty).?;
2727027273
27271 const i_small = @intCast(u5, i);
27272 if (dest_info.paramIsComptime(i_small) != src_info.paramIsComptime(i_small)) {
27274 const dest_param_ty = dest_info.param_types[param_i].toType();
27275 const src_param_ty = src_info.param_types[param_i].toType();
27276
27277 const param_i_small = @intCast(u5, param_i);
27278 if (dest_info.paramIsComptime(param_i_small) != src_info.paramIsComptime(param_i_small)) {
2727327279 return InMemoryCoercionResult{ .fn_param_comptime = .{
27274 .index = i,
27275 .wanted = dest_info.paramIsComptime(i_small),
27280 .index = param_i,
27281 .wanted = dest_info.paramIsComptime(param_i_small),
2727627282 } };
2727727283 }
2727827284
2727927285 // Note: Cast direction is reversed here.
27280 const param = try sema.coerceInMemoryAllowed(block, src_param_ty, dest_param_ty.toType(), false, target, dest_src, src_src);
27286 const param = try sema.coerceInMemoryAllowed(block, src_param_ty, dest_param_ty, false, target, dest_src, src_src);
2728127287 if (param != .ok) {
2728227288 return InMemoryCoercionResult{ .fn_param = .{
2728327289 .child = try param.dupe(sema.arena),
2728427290 .actual = src_param_ty,
27285 .wanted = dest_param_ty.toType(),
27286 .index = i,
27291 .wanted = dest_param_ty,
27292 .index = param_i,
2728727293 } };
2728827294 }
2728927295 }
......@@ -28385,7 +28391,7 @@ fn beginComptimePtrLoad(
2838528391 };
2838628392 },
2838728393 .elem => |elem_ptr| blk: {
28388 const elem_ty = ptr.ty.toType().childType(mod);
28394 const elem_ty = ptr.ty.toType().elemType2(mod);
2838928395 var deref = try sema.beginComptimePtrLoad(block, src, elem_ptr.base.toValue(), null);
2839028396
2839128397 // This code assumes that elem_ptrs have been "flattened" in order for direct dereference
......@@ -28678,11 +28684,10 @@ fn coerceCompatiblePtrs(
2867828684 return sema.fail(block, inst_src, "null pointer casted to type '{}'", .{dest_ty.fmt(sema.mod)});
2867928685 }
2868028686 // The comptime Value representation is compatible with both types.
28681 return sema.addConstant(dest_ty, (try mod.intern_pool.getCoerced(
28682 sema.gpa,
28683 try val.intern(inst_ty, mod),
28684 dest_ty.toIntern(),
28685 )).toValue());
28687 return sema.addConstant(
28688 dest_ty,
28689 try mod.getCoerced((try val.intern(inst_ty, mod)).toValue(), dest_ty),
28690 );
2868628691 }
2868728692 try sema.requireRuntimeBlock(block, inst_src, null);
2868828693 const inst_allows_zero = inst_ty.zigTypeTag(mod) != .Pointer or inst_ty.ptrAllowsZero(mod);
......@@ -29390,9 +29395,13 @@ fn refValue(sema: *Sema, block: *Block, ty: Type, val: Value) !Value {
2939029395
2939129396fn optRefValue(sema: *Sema, block: *Block, ty: Type, opt_val: ?Value) !Value {
2939229397 const mod = sema.mod;
29398 const ptr_anyopaque_ty = try mod.singleConstPtrType(Type.anyopaque);
2939329399 return (try mod.intern(.{ .opt = .{
29394 .ty = (try mod.optionalType((try mod.singleConstPtrType(Type.anyopaque)).toIntern())).toIntern(),
29395 .val = if (opt_val) |val| (try sema.refValue(block, ty, val)).toIntern() else .none,
29400 .ty = (try mod.optionalType(ptr_anyopaque_ty.toIntern())).toIntern(),
29401 .val = if (opt_val) |val| (try mod.getCoerced(
29402 try sema.refValue(block, ty, val),
29403 ptr_anyopaque_ty,
29404 )).toIntern() else .none,
2939629405 } })).toValue();
2939729406}
2939829407
......@@ -30051,11 +30060,10 @@ fn analyzeSlice(
3005130060 };
3005230061
3005330062 if (!new_ptr_val.isUndef(mod)) {
30054 return sema.addConstant(return_ty, (try mod.intern_pool.getCoerced(
30055 sema.gpa,
30056 try new_ptr_val.intern(new_ptr_ty, mod),
30057 return_ty.toIntern(),
30058 )).toValue());
30063 return sema.addConstant(return_ty, try mod.getCoerced(
30064 (try new_ptr_val.intern(new_ptr_ty, mod)).toValue(),
30065 return_ty,
30066 ));
3005930067 }
3006030068
3006130069 // Special case: @as([]i32, undefined)[x..x]
......@@ -34237,9 +34245,9 @@ fn enumHasInt(sema: *Sema, ty: Type, int: Value) CompileError!bool {
3423734245 // The `tagValueIndex` function call below relies on the type being the integer tag type.
3423834246 // `getCoerced` assumes the value will fit the new type.
3423934247 if (!(try sema.intFitsInType(int, enum_type.tag_ty.toType(), null))) return false;
34240 const int_coerced = try mod.intern_pool.getCoerced(sema.gpa, int.toIntern(), enum_type.tag_ty);
34248 const int_coerced = try mod.getCoerced(int, enum_type.tag_ty.toType());
3424134249
34242 return enum_type.tagValueIndex(&mod.intern_pool, int_coerced) != null;
34250 return enum_type.tagValueIndex(&mod.intern_pool, int_coerced.toIntern()) != null;
3424334251}
3424434252
3424534253fn intAddWithOverflow(
src/codegen.zig+15-13
......@@ -185,7 +185,7 @@ pub fn generateSymbol(
185185
186186 const mod = bin_file.options.module.?;
187187 var typed_value = arg_tv;
188 switch (mod.intern_pool.indexToKey(typed_value.val.ip_index)) {
188 switch (mod.intern_pool.indexToKey(typed_value.val.toIntern())) {
189189 .runtime_value => |rt| typed_value.val = rt.val.toValue(),
190190 else => {},
191191 }
......@@ -204,7 +204,7 @@ pub fn generateSymbol(
204204 return .ok;
205205 }
206206
207 switch (mod.intern_pool.indexToKey(typed_value.val.ip_index)) {
207 switch (mod.intern_pool.indexToKey(typed_value.val.toIntern())) {
208208 .int_type,
209209 .ptr_type,
210210 .array_type,
......@@ -282,7 +282,7 @@ pub fn generateSymbol(
282282 switch (try generateSymbol(bin_file, src_loc, .{
283283 .ty = payload_ty,
284284 .val = switch (error_union.val) {
285 .err_name => try mod.intern(.{ .undef = payload_ty.ip_index }),
285 .err_name => try mod.intern(.{ .undef = payload_ty.toIntern() }),
286286 .payload => |payload| payload,
287287 }.toValue(),
288288 }, code, debug_output, reloc_info)) {
......@@ -315,7 +315,7 @@ pub fn generateSymbol(
315315 const int_tag_ty = try typed_value.ty.intTagType(mod);
316316 switch (try generateSymbol(bin_file, src_loc, .{
317317 .ty = int_tag_ty,
318 .val = (try mod.intern_pool.getCoerced(mod.gpa, enum_tag.int, int_tag_ty.ip_index)).toValue(),
318 .val = try mod.getCoerced(enum_tag.int.toValue(), int_tag_ty),
319319 }, code, debug_output, reloc_info)) {
320320 .ok => {},
321321 .fail => |em| return .{ .fail = em },
......@@ -337,7 +337,7 @@ pub fn generateSymbol(
337337 switch (try lowerParentPtr(bin_file, src_loc, switch (ptr.len) {
338338 .none => typed_value.val,
339339 else => typed_value.val.slicePtr(mod),
340 }.ip_index, code, debug_output, reloc_info)) {
340 }.toIntern(), code, debug_output, reloc_info)) {
341341 .ok => {},
342342 .fail => |em| return .{ .fail = em },
343343 }
......@@ -372,7 +372,7 @@ pub fn generateSymbol(
372372 } else {
373373 const padding = abi_size - (math.cast(usize, payload_type.abiSize(mod)) orelse return error.Overflow) - 1;
374374 if (payload_type.hasRuntimeBits(mod)) {
375 const value = payload_val orelse (try mod.intern(.{ .undef = payload_type.ip_index })).toValue();
375 const value = payload_val orelse (try mod.intern(.{ .undef = payload_type.toIntern() })).toValue();
376376 switch (try generateSymbol(bin_file, src_loc, .{
377377 .ty = payload_type,
378378 .val = value,
......@@ -385,7 +385,7 @@ pub fn generateSymbol(
385385 try code.writer().writeByteNTimes(0, padding);
386386 }
387387 },
388 .aggregate => |aggregate| switch (mod.intern_pool.indexToKey(typed_value.ty.ip_index)) {
388 .aggregate => |aggregate| switch (mod.intern_pool.indexToKey(typed_value.ty.toIntern())) {
389389 .array_type => |array_type| {
390390 var index: u64 = 0;
391391 while (index < array_type.len) : (index += 1) {
......@@ -850,7 +850,7 @@ pub fn genTypedValue(
850850) CodeGenError!GenResult {
851851 const mod = bin_file.options.module.?;
852852 var typed_value = arg_tv;
853 switch (mod.intern_pool.indexToKey(typed_value.val.ip_index)) {
853 switch (mod.intern_pool.indexToKey(typed_value.val.toIntern())) {
854854 .runtime_value => |rt| typed_value.val = rt.val.toValue(),
855855 else => {},
856856 }
......@@ -866,7 +866,7 @@ pub fn genTypedValue(
866866 const target = bin_file.options.target;
867867 const ptr_bits = target.ptrBitWidth();
868868
869 if (!typed_value.ty.isSlice(mod)) switch (mod.intern_pool.indexToKey(typed_value.val.ip_index)) {
869 if (!typed_value.ty.isSlice(mod)) switch (mod.intern_pool.indexToKey(typed_value.val.toIntern())) {
870870 .ptr => |ptr| switch (ptr.addr) {
871871 .decl => |decl| return genDeclRef(bin_file, src_loc, typed_value, decl),
872872 .mut_decl => |mut_decl| return genDeclRef(bin_file, src_loc, typed_value, mut_decl.decl),
......@@ -879,12 +879,12 @@ pub fn genTypedValue(
879879 .Void => return GenResult.mcv(.none),
880880 .Pointer => switch (typed_value.ty.ptrSize(mod)) {
881881 .Slice => {},
882 else => switch (typed_value.val.ip_index) {
882 else => switch (typed_value.val.toIntern()) {
883883 .null_value => {
884884 return GenResult.mcv(.{ .immediate = 0 });
885885 },
886886 .none => {},
887 else => switch (mod.intern_pool.indexToKey(typed_value.val.ip_index)) {
887 else => switch (mod.intern_pool.indexToKey(typed_value.val.toIntern())) {
888888 .int => {
889889 return GenResult.mcv(.{ .immediate = typed_value.val.toUnsignedInt(mod) });
890890 },
......@@ -916,7 +916,7 @@ pub fn genTypedValue(
916916 }
917917 },
918918 .Enum => {
919 const enum_tag = mod.intern_pool.indexToKey(typed_value.val.ip_index).enum_tag;
919 const enum_tag = mod.intern_pool.indexToKey(typed_value.val.toIntern()).enum_tag;
920920 const int_tag_ty = mod.intern_pool.typeOf(enum_tag.int);
921921 return genTypedValue(bin_file, src_loc, .{
922922 .ty = int_tag_ty.toType(),
......@@ -924,7 +924,9 @@ pub fn genTypedValue(
924924 }, owner_decl_index);
925925 },
926926 .ErrorSet => {
927 const err_name = mod.intern_pool.stringToSlice(mod.intern_pool.indexToKey(typed_value.val.ip_index).err.name);
927 const err_name = mod.intern_pool.stringToSlice(
928 mod.intern_pool.indexToKey(typed_value.val.toIntern()).err.name,
929 );
928930 const global_error_set = mod.global_error_set;
929931 const error_index = global_error_set.get(err_name).?;
930932 return GenResult.mcv(.{ .immediate = error_index });
src/codegen/llvm.zig+1-1
......@@ -2329,7 +2329,7 @@ pub const Object = struct {
23292329 try param_di_types.append(try o.lowerDebugType(ptr_ty, .full));
23302330 }
23312331
2332 for (fn_info.param_types) |param_ty| {
2332 for (mod.typeToFunc(ty).?.param_types) |param_ty| {
23332333 if (!param_ty.toType().hasRuntimeBitsIgnoreComptime(mod)) continue;
23342334
23352335 if (isByRef(param_ty.toType(), mod)) {
src/type.zig+177-215
......@@ -23,7 +23,7 @@ pub const Type = struct {
2323 }
2424
2525 pub fn zigTypeTagOrPoison(ty: Type, mod: *const Module) error{GenericPoison}!std.builtin.TypeId {
26 return switch (mod.intern_pool.indexToKey(ty.ip_index)) {
26 return switch (mod.intern_pool.indexToKey(ty.toIntern())) {
2727 .int_type => .Int,
2828 .ptr_type => .Pointer,
2929 .array_type => .Array,
......@@ -170,7 +170,7 @@ pub const Type = struct {
170170
171171 /// Asserts the type is a pointer.
172172 pub fn ptrIsMutable(ty: Type, mod: *const Module) bool {
173 return !mod.intern_pool.indexToKey(ty.ip_index).ptr_type.is_const;
173 return !mod.intern_pool.indexToKey(ty.toIntern()).ptr_type.is_const;
174174 }
175175
176176 pub const ArrayInfo = struct {
......@@ -199,26 +199,23 @@ pub const Type = struct {
199199 }
200200
201201 pub fn ptrInfo(ty: Type, mod: *const Module) Payload.Pointer.Data {
202 return Payload.Pointer.Data.fromKey(ptrInfoIp(mod.intern_pool, ty.ip_index));
202 return Payload.Pointer.Data.fromKey(ptrInfoIp(mod.intern_pool, ty.toIntern()));
203203 }
204204
205205 pub fn eql(a: Type, b: Type, mod: *const Module) bool {
206206 _ = mod; // TODO: remove this parameter
207 assert(a.ip_index != .none);
208 assert(b.ip_index != .none);
209207 // The InternPool data structure hashes based on Key to make interned objects
210208 // unique. An Index can be treated simply as u32 value for the
211209 // purpose of Type/Value hashing and equality.
212 return a.ip_index == b.ip_index;
210 return a.toIntern() == b.toIntern();
213211 }
214212
215213 pub fn hash(ty: Type, mod: *const Module) u32 {
216214 _ = mod; // TODO: remove this parameter
217 assert(ty.ip_index != .none);
218215 // The InternPool data structure hashes based on Key to make interned objects
219216 // unique. An Index can be treated simply as u32 value for the
220217 // purpose of Type/Value hashing and equality.
221 return std.hash.uint32(@enumToInt(ty.ip_index));
218 return std.hash.uint32(@enumToInt(ty.toIntern()));
222219 }
223220
224221 pub fn format(ty: Type, comptime unused_fmt_string: []const u8, options: std.fmt.FormatOptions, writer: anytype) !void {
......@@ -280,7 +277,7 @@ pub const Type = struct {
280277
281278 /// Prints a name suitable for `@typeName`.
282279 pub fn print(ty: Type, writer: anytype, mod: *Module) @TypeOf(writer).Error!void {
283 switch (mod.intern_pool.indexToKey(ty.ip_index)) {
280 switch (mod.intern_pool.indexToKey(ty.toIntern())) {
284281 .int_type => |int_type| {
285282 const sign_char: u8 = switch (int_type.signedness) {
286283 .signed => 'i',
......@@ -520,10 +517,10 @@ pub const Type = struct {
520517 ignore_comptime_only: bool,
521518 strat: AbiAlignmentAdvancedStrat,
522519 ) RuntimeBitsError!bool {
523 return switch (ty.ip_index) {
520 return switch (ty.toIntern()) {
524521 // False because it is a comptime-only type.
525522 .empty_struct_type => false,
526 else => switch (mod.intern_pool.indexToKey(ty.ip_index)) {
523 else => switch (mod.intern_pool.indexToKey(ty.toIntern())) {
527524 .int_type => |int_type| int_type.bits != 0,
528525 .ptr_type => |ptr_type| {
529526 // Pointers to zero-bit types still have a runtime address; however, pointers
......@@ -710,7 +707,7 @@ pub const Type = struct {
710707 /// readFrom/writeToMemory are supported only for types with a well-
711708 /// defined memory layout
712709 pub fn hasWellDefinedLayout(ty: Type, mod: *Module) bool {
713 return switch (mod.intern_pool.indexToKey(ty.ip_index)) {
710 return switch (mod.intern_pool.indexToKey(ty.toIntern())) {
714711 .int_type,
715712 .ptr_type,
716713 .vector_type,
......@@ -847,7 +844,7 @@ pub const Type = struct {
847844 }
848845
849846 pub fn isNoReturn(ty: Type, mod: *Module) bool {
850 return if (ty.ip_index != .none) mod.intern_pool.isNoReturn(ty.ip_index) else false;
847 return mod.intern_pool.isNoReturn(ty.toIntern());
851848 }
852849
853850 /// Returns 0 if the pointer is naturally aligned and the element type is 0-bit.
......@@ -856,7 +853,7 @@ pub const Type = struct {
856853 }
857854
858855 pub fn ptrAlignmentAdvanced(ty: Type, mod: *Module, opt_sema: ?*Sema) !u32 {
859 return switch (mod.intern_pool.indexToKey(ty.ip_index)) {
856 return switch (mod.intern_pool.indexToKey(ty.toIntern())) {
860857 .ptr_type => |ptr_type| {
861858 if (ptr_type.alignment.toByteUnitsOptional()) |a| {
862859 return @intCast(u32, a);
......@@ -873,7 +870,7 @@ pub const Type = struct {
873870 }
874871
875872 pub fn ptrAddressSpace(ty: Type, mod: *const Module) std.builtin.AddressSpace {
876 return switch (mod.intern_pool.indexToKey(ty.ip_index)) {
873 return switch (mod.intern_pool.indexToKey(ty.toIntern())) {
877874 .ptr_type => |ptr_type| ptr_type.address_space,
878875 .opt_type => |child| mod.intern_pool.indexToKey(child).ptr_type.address_space,
879876 else => unreachable,
......@@ -923,9 +920,9 @@ pub const Type = struct {
923920 else => null,
924921 };
925922
926 switch (ty.ip_index) {
923 switch (ty.toIntern()) {
927924 .empty_struct_type => return AbiAlignmentAdvanced{ .scalar = 0 },
928 else => switch (mod.intern_pool.indexToKey(ty.ip_index)) {
925 else => switch (mod.intern_pool.indexToKey(ty.toIntern())) {
929926 .int_type => |int_type| {
930927 if (int_type.bits == 0) return AbiAlignmentAdvanced{ .scalar = 0 };
931928 return AbiAlignmentAdvanced{ .scalar = intAbiAlignment(int_type.bits, target) };
......@@ -1040,7 +1037,7 @@ pub const Type = struct {
10401037 .sema => unreachable, // handled above
10411038 .lazy => return .{ .val = (try mod.intern(.{ .int = .{
10421039 .ty = .comptime_int_type,
1043 .storage = .{ .lazy_align = ty.ip_index },
1040 .storage = .{ .lazy_align = ty.toIntern() },
10441041 } })).toValue() },
10451042 };
10461043 if (struct_obj.layout == .Packed) {
......@@ -1048,7 +1045,7 @@ pub const Type = struct {
10481045 .sema => |sema| try sema.resolveTypeLayout(ty),
10491046 .lazy => if (!struct_obj.haveLayout()) return .{ .val = (try mod.intern(.{ .int = .{
10501047 .ty = .comptime_int_type,
1051 .storage = .{ .lazy_align = ty.ip_index },
1048 .storage = .{ .lazy_align = ty.toIntern() },
10521049 } })).toValue() },
10531050 .eager => {},
10541051 }
......@@ -1062,7 +1059,7 @@ pub const Type = struct {
10621059 if (!(field.ty.hasRuntimeBitsAdvanced(mod, false, strat) catch |err| switch (err) {
10631060 error.NeedLazy => return .{ .val = (try mod.intern(.{ .int = .{
10641061 .ty = .comptime_int_type,
1065 .storage = .{ .lazy_align = ty.ip_index },
1062 .storage = .{ .lazy_align = ty.toIntern() },
10661063 } })).toValue() },
10671064 else => |e| return e,
10681065 })) continue;
......@@ -1076,7 +1073,7 @@ pub const Type = struct {
10761073 .sema => unreachable, // handled above
10771074 .lazy => return .{ .val = (try mod.intern(.{ .int = .{
10781075 .ty = .comptime_int_type,
1079 .storage = .{ .lazy_align = ty.ip_index },
1076 .storage = .{ .lazy_align = ty.toIntern() },
10801077 } })).toValue() },
10811078 },
10821079 };
......@@ -1106,7 +1103,7 @@ pub const Type = struct {
11061103 .sema => unreachable, // passed to abiAlignmentAdvanced above
11071104 .lazy => return .{ .val = (try mod.intern(.{ .int = .{
11081105 .ty = .comptime_int_type,
1109 .storage = .{ .lazy_align = ty.ip_index },
1106 .storage = .{ .lazy_align = ty.toIntern() },
11101107 } })).toValue() },
11111108 },
11121109 }
......@@ -1157,7 +1154,7 @@ pub const Type = struct {
11571154 if (!(payload_ty.hasRuntimeBitsAdvanced(mod, false, strat) catch |err| switch (err) {
11581155 error.NeedLazy => return .{ .val = (try mod.intern(.{ .int = .{
11591156 .ty = .comptime_int_type,
1160 .storage = .{ .lazy_align = ty.ip_index },
1157 .storage = .{ .lazy_align = ty.toIntern() },
11611158 } })).toValue() },
11621159 else => |e| return e,
11631160 })) {
......@@ -1179,7 +1176,7 @@ pub const Type = struct {
11791176 }
11801177 return .{ .val = (try mod.intern(.{ .int = .{
11811178 .ty = .comptime_int_type,
1182 .storage = .{ .lazy_align = ty.ip_index },
1179 .storage = .{ .lazy_align = ty.toIntern() },
11831180 } })).toValue() };
11841181 },
11851182 }
......@@ -1205,7 +1202,7 @@ pub const Type = struct {
12051202 if (!(child_type.hasRuntimeBitsAdvanced(mod, false, strat) catch |err| switch (err) {
12061203 error.NeedLazy => return .{ .val = (try mod.intern(.{ .int = .{
12071204 .ty = .comptime_int_type,
1208 .storage = .{ .lazy_align = ty.ip_index },
1205 .storage = .{ .lazy_align = ty.toIntern() },
12091206 } })).toValue() },
12101207 else => |e| return e,
12111208 })) {
......@@ -1217,7 +1214,7 @@ pub const Type = struct {
12171214 .scalar => |x| return AbiAlignmentAdvanced{ .scalar = @max(x, 1) },
12181215 .val => return .{ .val = (try mod.intern(.{ .int = .{
12191216 .ty = .comptime_int_type,
1220 .storage = .{ .lazy_align = ty.ip_index },
1217 .storage = .{ .lazy_align = ty.toIntern() },
12211218 } })).toValue() },
12221219 },
12231220 }
......@@ -1249,7 +1246,7 @@ pub const Type = struct {
12491246 .sema => unreachable, // handled above
12501247 .lazy => return .{ .val = (try mod.intern(.{ .int = .{
12511248 .ty = .comptime_int_type,
1252 .storage = .{ .lazy_align = ty.ip_index },
1249 .storage = .{ .lazy_align = ty.toIntern() },
12531250 } })).toValue() },
12541251 };
12551252 if (union_obj.fields.count() == 0) {
......@@ -1266,7 +1263,7 @@ pub const Type = struct {
12661263 if (!(field.ty.hasRuntimeBitsAdvanced(mod, false, strat) catch |err| switch (err) {
12671264 error.NeedLazy => return .{ .val = (try mod.intern(.{ .int = .{
12681265 .ty = .comptime_int_type,
1269 .storage = .{ .lazy_align = ty.ip_index },
1266 .storage = .{ .lazy_align = ty.toIntern() },
12701267 } })).toValue() },
12711268 else => |e| return e,
12721269 })) continue;
......@@ -1280,7 +1277,7 @@ pub const Type = struct {
12801277 .sema => unreachable, // handled above
12811278 .lazy => return .{ .val = (try mod.intern(.{ .int = .{
12821279 .ty = .comptime_int_type,
1283 .storage = .{ .lazy_align = ty.ip_index },
1280 .storage = .{ .lazy_align = ty.toIntern() },
12841281 } })).toValue() },
12851282 },
12861283 };
......@@ -1321,10 +1318,10 @@ pub const Type = struct {
13211318 ) Module.CompileError!AbiSizeAdvanced {
13221319 const target = mod.getTarget();
13231320
1324 switch (ty.ip_index) {
1321 switch (ty.toIntern()) {
13251322 .empty_struct_type => return AbiSizeAdvanced{ .scalar = 0 },
13261323
1327 else => switch (mod.intern_pool.indexToKey(ty.ip_index)) {
1324 else => switch (mod.intern_pool.indexToKey(ty.toIntern())) {
13281325 .int_type => |int_type| {
13291326 if (int_type.bits == 0) return AbiSizeAdvanced{ .scalar = 0 };
13301327 return AbiSizeAdvanced{ .scalar = intAbiSize(int_type.bits, target) };
......@@ -1343,7 +1340,7 @@ pub const Type = struct {
13431340 .sema, .eager => unreachable,
13441341 .lazy => return .{ .val = (try mod.intern(.{ .int = .{
13451342 .ty = .comptime_int_type,
1346 .storage = .{ .lazy_size = ty.ip_index },
1343 .storage = .{ .lazy_size = ty.toIntern() },
13471344 } })).toValue() },
13481345 },
13491346 }
......@@ -1354,7 +1351,7 @@ pub const Type = struct {
13541351 .eager => null,
13551352 .lazy => return .{ .val = (try mod.intern(.{ .int = .{
13561353 .ty = .comptime_int_type,
1357 .storage = .{ .lazy_size = ty.ip_index },
1354 .storage = .{ .lazy_size = ty.toIntern() },
13581355 } })).toValue() },
13591356 };
13601357 const elem_bits_u64 = try vector_type.child.toType().bitSizeAdvanced(mod, opt_sema);
......@@ -1365,7 +1362,7 @@ pub const Type = struct {
13651362 .scalar => |x| x,
13661363 .val => return .{ .val = (try mod.intern(.{ .int = .{
13671364 .ty = .comptime_int_type,
1368 .storage = .{ .lazy_size = ty.ip_index },
1365 .storage = .{ .lazy_size = ty.toIntern() },
13691366 } })).toValue() },
13701367 };
13711368 const result = std.mem.alignForwardGeneric(u32, total_bytes, alignment);
......@@ -1385,7 +1382,7 @@ pub const Type = struct {
13851382 if (!(payload_ty.hasRuntimeBitsAdvanced(mod, false, strat) catch |err| switch (err) {
13861383 error.NeedLazy => return .{ .val = (try mod.intern(.{ .int = .{
13871384 .ty = .comptime_int_type,
1388 .storage = .{ .lazy_size = ty.ip_index },
1385 .storage = .{ .lazy_size = ty.toIntern() },
13891386 } })).toValue() },
13901387 else => |e| return e,
13911388 })) {
......@@ -1401,7 +1398,7 @@ pub const Type = struct {
14011398 .eager => unreachable,
14021399 .lazy => return .{ .val = (try mod.intern(.{ .int = .{
14031400 .ty = .comptime_int_type,
1404 .storage = .{ .lazy_size = ty.ip_index },
1401 .storage = .{ .lazy_size = ty.toIntern() },
14051402 } })).toValue() },
14061403 },
14071404 };
......@@ -1489,7 +1486,7 @@ pub const Type = struct {
14891486 .sema => |sema| try sema.resolveTypeLayout(ty),
14901487 .lazy => if (!struct_obj.haveLayout()) return .{ .val = (try mod.intern(.{ .int = .{
14911488 .ty = .comptime_int_type,
1492 .storage = .{ .lazy_size = ty.ip_index },
1489 .storage = .{ .lazy_size = ty.toIntern() },
14931490 } })).toValue() },
14941491 .eager => {},
14951492 }
......@@ -1504,7 +1501,7 @@ pub const Type = struct {
15041501 return AbiSizeAdvanced{ .scalar = 0 };
15051502 if (!struct_obj.haveLayout()) return .{ .val = (try mod.intern(.{ .int = .{
15061503 .ty = .comptime_int_type,
1507 .storage = .{ .lazy_size = ty.ip_index },
1504 .storage = .{ .lazy_size = ty.toIntern() },
15081505 } })).toValue() };
15091506 },
15101507 .eager => {},
......@@ -1568,7 +1565,7 @@ pub const Type = struct {
15681565 .sema => |sema| try sema.resolveTypeLayout(ty),
15691566 .lazy => if (!union_obj.haveLayout()) return .{ .val = (try mod.intern(.{ .int = .{
15701567 .ty = .comptime_int_type,
1571 .storage = .{ .lazy_size = ty.ip_index },
1568 .storage = .{ .lazy_size = ty.toIntern() },
15721569 } })).toValue() },
15731570 .eager => {},
15741571 }
......@@ -1589,7 +1586,7 @@ pub const Type = struct {
15891586 if (!(child_ty.hasRuntimeBitsAdvanced(mod, false, strat) catch |err| switch (err) {
15901587 error.NeedLazy => return .{ .val = (try mod.intern(.{ .int = .{
15911588 .ty = .comptime_int_type,
1592 .storage = .{ .lazy_size = ty.ip_index },
1589 .storage = .{ .lazy_size = ty.toIntern() },
15931590 } })).toValue() },
15941591 else => |e| return e,
15951592 })) return AbiSizeAdvanced{ .scalar = 1 };
......@@ -1605,7 +1602,7 @@ pub const Type = struct {
16051602 .eager => unreachable,
16061603 .lazy => return .{ .val = (try mod.intern(.{ .int = .{
16071604 .ty = .comptime_int_type,
1608 .storage = .{ .lazy_size = ty.ip_index },
1605 .storage = .{ .lazy_size = ty.toIntern() },
16091606 } })).toValue() },
16101607 },
16111608 };
......@@ -1647,7 +1644,7 @@ pub const Type = struct {
16471644
16481645 const strat: AbiAlignmentAdvancedStrat = if (opt_sema) |sema| .{ .sema = sema } else .eager;
16491646
1650 switch (mod.intern_pool.indexToKey(ty.ip_index)) {
1647 switch (mod.intern_pool.indexToKey(ty.toIntern())) {
16511648 .int_type => |int_type| return int_type.bits,
16521649 .ptr_type => |ptr_type| switch (ptr_type.size) {
16531650 .Slice => return target.ptrBitWidth() * 2,
......@@ -1820,7 +1817,7 @@ pub const Type = struct {
18201817 }
18211818
18221819 pub fn isSinglePointer(ty: Type, mod: *const Module) bool {
1823 return switch (mod.intern_pool.indexToKey(ty.ip_index)) {
1820 return switch (mod.intern_pool.indexToKey(ty.toIntern())) {
18241821 .ptr_type => |ptr_info| ptr_info.size == .One,
18251822 else => false,
18261823 };
......@@ -1833,33 +1830,27 @@ pub const Type = struct {
18331830
18341831 /// Returns `null` if `ty` is not a pointer.
18351832 pub fn ptrSizeOrNull(ty: Type, mod: *const Module) ?std.builtin.Type.Pointer.Size {
1836 return switch (mod.intern_pool.indexToKey(ty.ip_index)) {
1833 return switch (mod.intern_pool.indexToKey(ty.toIntern())) {
18371834 .ptr_type => |ptr_info| ptr_info.size,
18381835 else => null,
18391836 };
18401837 }
18411838
18421839 pub fn isSlice(ty: Type, mod: *const Module) bool {
1843 return switch (ty.ip_index) {
1844 .none => false,
1845 else => switch (mod.intern_pool.indexToKey(ty.ip_index)) {
1846 .ptr_type => |ptr_type| ptr_type.size == .Slice,
1847 else => false,
1848 },
1840 return switch (mod.intern_pool.indexToKey(ty.toIntern())) {
1841 .ptr_type => |ptr_type| ptr_type.size == .Slice,
1842 else => false,
18491843 };
18501844 }
18511845
18521846 pub fn slicePtrFieldType(ty: Type, mod: *const Module) Type {
1853 return mod.intern_pool.slicePtrType(ty.ip_index).toType();
1847 return mod.intern_pool.slicePtrType(ty.toIntern()).toType();
18541848 }
18551849
18561850 pub fn isConstPtr(ty: Type, mod: *const Module) bool {
1857 return switch (ty.ip_index) {
1858 .none => false,
1859 else => return switch (mod.intern_pool.indexToKey(ty.ip_index)) {
1860 .ptr_type => |ptr_type| ptr_type.is_const,
1861 else => false,
1862 },
1851 return switch (mod.intern_pool.indexToKey(ty.toIntern())) {
1852 .ptr_type => |ptr_type| ptr_type.is_const,
1853 else => false,
18631854 };
18641855 }
18651856
......@@ -1868,53 +1859,41 @@ pub const Type = struct {
18681859 }
18691860
18701861 pub fn isVolatilePtrIp(ty: Type, ip: InternPool) bool {
1871 return switch (ty.ip_index) {
1872 .none => false,
1873 else => switch (ip.indexToKey(ty.ip_index)) {
1874 .ptr_type => |ptr_type| ptr_type.is_volatile,
1875 else => false,
1876 },
1862 return switch (ip.indexToKey(ty.toIntern())) {
1863 .ptr_type => |ptr_type| ptr_type.is_volatile,
1864 else => false,
18771865 };
18781866 }
18791867
18801868 pub fn isAllowzeroPtr(ty: Type, mod: *const Module) bool {
1881 return switch (ty.ip_index) {
1882 .none => false,
1883 else => switch (mod.intern_pool.indexToKey(ty.ip_index)) {
1884 .ptr_type => |ptr_type| ptr_type.is_allowzero,
1885 .opt_type => true,
1886 else => false,
1887 },
1869 return switch (mod.intern_pool.indexToKey(ty.toIntern())) {
1870 .ptr_type => |ptr_type| ptr_type.is_allowzero,
1871 .opt_type => true,
1872 else => false,
18881873 };
18891874 }
18901875
18911876 pub fn isCPtr(ty: Type, mod: *const Module) bool {
1892 return switch (ty.ip_index) {
1893 .none => false,
1894 else => switch (mod.intern_pool.indexToKey(ty.ip_index)) {
1895 .ptr_type => |ptr_type| ptr_type.size == .C,
1896 else => false,
1897 },
1877 return switch (mod.intern_pool.indexToKey(ty.toIntern())) {
1878 .ptr_type => |ptr_type| ptr_type.size == .C,
1879 else => false,
18981880 };
18991881 }
19001882
19011883 pub fn isPtrAtRuntime(ty: Type, mod: *const Module) bool {
1902 return switch (ty.ip_index) {
1903 .none => false,
1904 else => switch (mod.intern_pool.indexToKey(ty.ip_index)) {
1905 .ptr_type => |ptr_type| switch (ptr_type.size) {
1906 .Slice => false,
1907 .One, .Many, .C => true,
1908 },
1909 .opt_type => |child| switch (mod.intern_pool.indexToKey(child)) {
1910 .ptr_type => |p| switch (p.size) {
1911 .Slice, .C => false,
1912 .Many, .One => !p.is_allowzero,
1913 },
1914 else => false,
1884 return switch (mod.intern_pool.indexToKey(ty.toIntern())) {
1885 .ptr_type => |ptr_type| switch (ptr_type.size) {
1886 .Slice => false,
1887 .One, .Many, .C => true,
1888 },
1889 .opt_type => |child| switch (mod.intern_pool.indexToKey(child)) {
1890 .ptr_type => |p| switch (p.size) {
1891 .Slice, .C => false,
1892 .Many, .One => !p.is_allowzero,
19151893 },
19161894 else => false,
19171895 },
1896 else => false,
19181897 };
19191898 }
19201899
......@@ -1929,22 +1908,19 @@ pub const Type = struct {
19291908
19301909 /// See also `isPtrLikeOptional`.
19311910 pub fn optionalReprIsPayload(ty: Type, mod: *const Module) bool {
1932 return switch (ty.ip_index) {
1933 .none => false,
1934 else => switch (mod.intern_pool.indexToKey(ty.ip_index)) {
1935 .opt_type => |child| switch (child.toType().zigTypeTag(mod)) {
1936 .Pointer => {
1937 const info = child.toType().ptrInfo(mod);
1938 return switch (info.size) {
1939 .C => false,
1940 else => !info.@"allowzero",
1941 };
1942 },
1943 .ErrorSet => true,
1944 else => false,
1911 return switch (mod.intern_pool.indexToKey(ty.toIntern())) {
1912 .opt_type => |child| switch (child.toType().zigTypeTag(mod)) {
1913 .Pointer => {
1914 const info = child.toType().ptrInfo(mod);
1915 return switch (info.size) {
1916 .C => false,
1917 else => !info.@"allowzero",
1918 };
19451919 },
1920 .ErrorSet => true,
19461921 else => false,
19471922 },
1923 else => false,
19481924 };
19491925 }
19501926
......@@ -1952,19 +1928,16 @@ pub const Type = struct {
19521928 /// address value, using 0 for null. Note that this returns true for C pointers.
19531929 /// This function must be kept in sync with `Sema.typePtrOrOptionalPtrTy`.
19541930 pub fn isPtrLikeOptional(ty: Type, mod: *const Module) bool {
1955 return switch (ty.ip_index) {
1956 .none => false,
1957 else => switch (mod.intern_pool.indexToKey(ty.ip_index)) {
1958 .ptr_type => |ptr_type| ptr_type.size == .C,
1959 .opt_type => |child| switch (mod.intern_pool.indexToKey(child)) {
1960 .ptr_type => |ptr_type| switch (ptr_type.size) {
1961 .Slice, .C => false,
1962 .Many, .One => !ptr_type.is_allowzero,
1963 },
1964 else => false,
1931 return switch (mod.intern_pool.indexToKey(ty.toIntern())) {
1932 .ptr_type => |ptr_type| ptr_type.size == .C,
1933 .opt_type => |child| switch (mod.intern_pool.indexToKey(child)) {
1934 .ptr_type => |ptr_type| switch (ptr_type.size) {
1935 .Slice, .C => false,
1936 .Many, .One => !ptr_type.is_allowzero,
19651937 },
19661938 else => false,
19671939 },
1940 else => false,
19681941 };
19691942 }
19701943
......@@ -1976,7 +1949,7 @@ pub const Type = struct {
19761949 }
19771950
19781951 pub fn childTypeIp(ty: Type, ip: InternPool) Type {
1979 return ip.childType(ty.ip_index).toType();
1952 return ip.childType(ty.toIntern()).toType();
19801953 }
19811954
19821955 /// For *[N]T, returns T.
......@@ -1989,7 +1962,7 @@ pub const Type = struct {
19891962 /// For []T, returns T.
19901963 /// For anyframe->T, returns T.
19911964 pub fn elemType2(ty: Type, mod: *const Module) Type {
1992 return switch (mod.intern_pool.indexToKey(ty.ip_index)) {
1965 return switch (mod.intern_pool.indexToKey(ty.toIntern())) {
19931966 .ptr_type => |ptr_type| switch (ptr_type.size) {
19941967 .One => ptr_type.elem_type.toType().shallowElemType(mod),
19951968 .Many, .C, .Slice => ptr_type.elem_type.toType(),
......@@ -2023,7 +1996,7 @@ pub const Type = struct {
20231996 /// Asserts that the type is an optional.
20241997 /// Note that for C pointers this returns the type unmodified.
20251998 pub fn optionalChild(ty: Type, mod: *const Module) Type {
2026 return switch (mod.intern_pool.indexToKey(ty.ip_index)) {
1999 return switch (mod.intern_pool.indexToKey(ty.toIntern())) {
20272000 .opt_type => |child| child.toType(),
20282001 .ptr_type => |ptr_type| b: {
20292002 assert(ptr_type.size == .C);
......@@ -2036,7 +2009,7 @@ pub const Type = struct {
20362009 /// Returns the tag type of a union, if the type is a union and it has a tag type.
20372010 /// Otherwise, returns `null`.
20382011 pub fn unionTagType(ty: Type, mod: *Module) ?Type {
2039 return switch (mod.intern_pool.indexToKey(ty.ip_index)) {
2012 return switch (mod.intern_pool.indexToKey(ty.toIntern())) {
20402013 .union_type => |union_type| switch (union_type.runtime_tag) {
20412014 .tagged => {
20422015 const union_obj = mod.unionPtr(union_type.index);
......@@ -2052,7 +2025,7 @@ pub const Type = struct {
20522025 /// Same as `unionTagType` but includes safety tag.
20532026 /// Codegen should use this version.
20542027 pub fn unionTagTypeSafety(ty: Type, mod: *Module) ?Type {
2055 return switch (mod.intern_pool.indexToKey(ty.ip_index)) {
2028 return switch (mod.intern_pool.indexToKey(ty.toIntern())) {
20562029 .union_type => |union_type| {
20572030 if (!union_type.hasTag()) return null;
20582031 const union_obj = mod.unionPtr(union_type.index);
......@@ -2097,13 +2070,13 @@ pub const Type = struct {
20972070 }
20982071
20992072 pub fn unionGetLayout(ty: Type, mod: *Module) Module.Union.Layout {
2100 const union_type = mod.intern_pool.indexToKey(ty.ip_index).union_type;
2073 const union_type = mod.intern_pool.indexToKey(ty.toIntern()).union_type;
21012074 const union_obj = mod.unionPtr(union_type.index);
21022075 return union_obj.getLayout(mod, union_type.hasTag());
21032076 }
21042077
21052078 pub fn containerLayout(ty: Type, mod: *Module) std.builtin.Type.ContainerLayout {
2106 return switch (mod.intern_pool.indexToKey(ty.ip_index)) {
2079 return switch (mod.intern_pool.indexToKey(ty.toIntern())) {
21072080 .struct_type => |struct_type| {
21082081 const struct_obj = mod.structPtrUnwrap(struct_type.index) orelse return .Auto;
21092082 return struct_obj.layout;
......@@ -2119,19 +2092,19 @@ pub const Type = struct {
21192092
21202093 /// Asserts that the type is an error union.
21212094 pub fn errorUnionPayload(ty: Type, mod: *Module) Type {
2122 return mod.intern_pool.indexToKey(ty.ip_index).error_union_type.payload_type.toType();
2095 return mod.intern_pool.indexToKey(ty.toIntern()).error_union_type.payload_type.toType();
21232096 }
21242097
21252098 /// Asserts that the type is an error union.
21262099 pub fn errorUnionSet(ty: Type, mod: *Module) Type {
2127 return mod.intern_pool.indexToKey(ty.ip_index).error_union_type.error_set_type.toType();
2100 return mod.intern_pool.indexToKey(ty.toIntern()).error_union_type.error_set_type.toType();
21282101 }
21292102
21302103 /// Returns false for unresolved inferred error sets.
21312104 pub fn errorSetIsEmpty(ty: Type, mod: *Module) bool {
2132 return switch (ty.ip_index) {
2105 return switch (ty.toIntern()) {
21332106 .anyerror_type => false,
2134 else => switch (mod.intern_pool.indexToKey(ty.ip_index)) {
2107 else => switch (mod.intern_pool.indexToKey(ty.toIntern())) {
21352108 .error_set_type => |error_set_type| error_set_type.names.len == 0,
21362109 .inferred_error_set_type => |index| {
21372110 const inferred_error_set = mod.inferredErrorSetPtr(index);
......@@ -2149,9 +2122,9 @@ pub const Type = struct {
21492122 /// Note that the result may be a false negative if the type did not get error set
21502123 /// resolution prior to this call.
21512124 pub fn isAnyError(ty: Type, mod: *Module) bool {
2152 return switch (ty.ip_index) {
2125 return switch (ty.toIntern()) {
21532126 .anyerror_type => true,
2154 else => switch (mod.intern_pool.indexToKey(ty.ip_index)) {
2127 else => switch (mod.intern_pool.indexToKey(ty.toIntern())) {
21552128 .inferred_error_set_type => |i| mod.inferredErrorSetPtr(i).is_anyerror,
21562129 else => false,
21572130 },
......@@ -2194,9 +2167,9 @@ pub const Type = struct {
21942167 /// resolved yet.
21952168 pub fn errorSetHasField(ty: Type, name: []const u8, mod: *Module) bool {
21962169 const ip = &mod.intern_pool;
2197 return switch (ty.ip_index) {
2170 return switch (ty.toIntern()) {
21982171 .anyerror_type => true,
2199 else => switch (ip.indexToKey(ty.ip_index)) {
2172 else => switch (ip.indexToKey(ty.toIntern())) {
22002173 .error_set_type => |error_set_type| {
22012174 // If the string is not interned, then the field certainly is not present.
22022175 const field_name_interned = ip.getString(name).unwrap() orelse return false;
......@@ -2220,7 +2193,7 @@ pub const Type = struct {
22202193 }
22212194
22222195 pub fn arrayLenIp(ty: Type, ip: InternPool) u64 {
2223 return switch (ip.indexToKey(ty.ip_index)) {
2196 return switch (ip.indexToKey(ty.toIntern())) {
22242197 .vector_type => |vector_type| vector_type.len,
22252198 .array_type => |array_type| array_type.len,
22262199 .struct_type => |struct_type| {
......@@ -2238,7 +2211,7 @@ pub const Type = struct {
22382211 }
22392212
22402213 pub fn vectorLen(ty: Type, mod: *const Module) u32 {
2241 return switch (mod.intern_pool.indexToKey(ty.ip_index)) {
2214 return switch (mod.intern_pool.indexToKey(ty.toIntern())) {
22422215 .vector_type => |vector_type| vector_type.len,
22432216 .anon_struct_type => |tuple| @intCast(u32, tuple.types.len),
22442217 else => unreachable,
......@@ -2247,7 +2220,7 @@ pub const Type = struct {
22472220
22482221 /// Asserts the type is an array, pointer or vector.
22492222 pub fn sentinel(ty: Type, mod: *const Module) ?Value {
2250 return switch (mod.intern_pool.indexToKey(ty.ip_index)) {
2223 return switch (mod.intern_pool.indexToKey(ty.toIntern())) {
22512224 .vector_type,
22522225 .struct_type,
22532226 .anon_struct_type,
......@@ -2267,10 +2240,9 @@ pub const Type = struct {
22672240
22682241 /// Returns true if and only if the type is a fixed-width, signed integer.
22692242 pub fn isSignedInt(ty: Type, mod: *const Module) bool {
2270 return switch (ty.ip_index) {
2243 return switch (ty.toIntern()) {
22712244 .c_char_type, .isize_type, .c_short_type, .c_int_type, .c_long_type, .c_longlong_type => true,
2272 .none => false,
2273 else => switch (mod.intern_pool.indexToKey(ty.ip_index)) {
2245 else => switch (mod.intern_pool.indexToKey(ty.toIntern())) {
22742246 .int_type => |int_type| int_type.signedness == .signed,
22752247 else => false,
22762248 },
......@@ -2279,10 +2251,9 @@ pub const Type = struct {
22792251
22802252 /// Returns true if and only if the type is a fixed-width, unsigned integer.
22812253 pub fn isUnsignedInt(ty: Type, mod: *const Module) bool {
2282 return switch (ty.ip_index) {
2254 return switch (ty.toIntern()) {
22832255 .usize_type, .c_ushort_type, .c_uint_type, .c_ulong_type, .c_ulonglong_type => true,
2284 .none => false,
2285 else => switch (mod.intern_pool.indexToKey(ty.ip_index)) {
2256 else => switch (mod.intern_pool.indexToKey(ty.toIntern())) {
22862257 .int_type => |int_type| int_type.signedness == .unsigned,
22872258 else => false,
22882259 },
......@@ -2304,7 +2275,7 @@ pub const Type = struct {
23042275 const target = mod.getTarget();
23052276 var ty = starting_ty;
23062277
2307 while (true) switch (ty.ip_index) {
2278 while (true) switch (ty.toIntern()) {
23082279 .anyerror_type => {
23092280 // TODO revisit this when error sets support custom int types
23102281 return .{ .signedness = .unsigned, .bits = 16 };
......@@ -2320,7 +2291,7 @@ pub const Type = struct {
23202291 .c_ulong_type => return .{ .signedness = .unsigned, .bits = target.c_type_bit_size(.ulong) },
23212292 .c_longlong_type => return .{ .signedness = .signed, .bits = target.c_type_bit_size(.longlong) },
23222293 .c_ulonglong_type => return .{ .signedness = .unsigned, .bits = target.c_type_bit_size(.ulonglong) },
2323 else => switch (mod.intern_pool.indexToKey(ty.ip_index)) {
2294 else => switch (mod.intern_pool.indexToKey(ty.toIntern())) {
23242295 .int_type => |int_type| return int_type,
23252296 .struct_type => |struct_type| {
23262297 const struct_obj = mod.structPtrUnwrap(struct_type.index).?;
......@@ -2370,7 +2341,7 @@ pub const Type = struct {
23702341 }
23712342
23722343 pub fn isNamedInt(ty: Type) bool {
2373 return switch (ty.ip_index) {
2344 return switch (ty.toIntern()) {
23742345 .usize_type,
23752346 .isize_type,
23762347 .c_char_type,
......@@ -2390,7 +2361,7 @@ pub const Type = struct {
23902361
23912362 /// Returns `false` for `comptime_float`.
23922363 pub fn isRuntimeFloat(ty: Type) bool {
2393 return switch (ty.ip_index) {
2364 return switch (ty.toIntern()) {
23942365 .f16_type,
23952366 .f32_type,
23962367 .f64_type,
......@@ -2405,7 +2376,7 @@ pub const Type = struct {
24052376
24062377 /// Returns `true` for `comptime_float`.
24072378 pub fn isAnyFloat(ty: Type) bool {
2408 return switch (ty.ip_index) {
2379 return switch (ty.toIntern()) {
24092380 .f16_type,
24102381 .f32_type,
24112382 .f64_type,
......@@ -2422,7 +2393,7 @@ pub const Type = struct {
24222393 /// Asserts the type is a fixed-size float or comptime_float.
24232394 /// Returns 128 for comptime_float types.
24242395 pub fn floatBits(ty: Type, target: Target) u16 {
2425 return switch (ty.ip_index) {
2396 return switch (ty.toIntern()) {
24262397 .f16_type => 16,
24272398 .f32_type => 32,
24282399 .f64_type => 64,
......@@ -2440,7 +2411,7 @@ pub const Type = struct {
24402411 }
24412412
24422413 pub fn fnReturnTypeIp(ty: Type, ip: InternPool) Type {
2443 return switch (ip.indexToKey(ty.ip_index)) {
2414 return switch (ip.indexToKey(ty.toIntern())) {
24442415 .ptr_type => |ptr_type| ip.indexToKey(ptr_type.elem_type).func_type.return_type,
24452416 .func_type => |func_type| func_type.return_type,
24462417 else => unreachable,
......@@ -2449,7 +2420,7 @@ pub const Type = struct {
24492420
24502421 /// Asserts the type is a function.
24512422 pub fn fnCallingConvention(ty: Type, mod: *Module) std.builtin.CallingConvention {
2452 return mod.intern_pool.indexToKey(ty.ip_index).func_type.cc;
2423 return mod.intern_pool.indexToKey(ty.toIntern()).func_type.cc;
24532424 }
24542425
24552426 pub fn isValidParamType(self: Type, mod: *const Module) bool {
......@@ -2468,11 +2439,11 @@ pub const Type = struct {
24682439
24692440 /// Asserts the type is a function.
24702441 pub fn fnIsVarArgs(ty: Type, mod: *Module) bool {
2471 return mod.intern_pool.indexToKey(ty.ip_index).func_type.is_var_args;
2442 return mod.intern_pool.indexToKey(ty.toIntern()).func_type.is_var_args;
24722443 }
24732444
24742445 pub fn isNumeric(ty: Type, mod: *const Module) bool {
2475 return switch (ty.ip_index) {
2446 return switch (ty.toIntern()) {
24762447 .f16_type,
24772448 .f32_type,
24782449 .f64_type,
......@@ -2494,9 +2465,7 @@ pub const Type = struct {
24942465 .c_ulonglong_type,
24952466 => true,
24962467
2497 .none => false,
2498
2499 else => switch (mod.intern_pool.indexToKey(ty.ip_index)) {
2468 else => switch (mod.intern_pool.indexToKey(ty.toIntern())) {
25002469 .int_type => true,
25012470 else => false,
25022471 },
......@@ -2508,10 +2477,10 @@ pub const Type = struct {
25082477 pub fn onePossibleValue(starting_type: Type, mod: *Module) !?Value {
25092478 var ty = starting_type;
25102479
2511 while (true) switch (ty.ip_index) {
2480 while (true) switch (ty.toIntern()) {
25122481 .empty_struct_type => return Value.empty_struct,
25132482
2514 else => switch (mod.intern_pool.indexToKey(ty.ip_index)) {
2483 else => switch (mod.intern_pool.indexToKey(ty.toIntern())) {
25152484 .int_type => |int_type| {
25162485 if (int_type.bits == 0) {
25172486 return try mod.intValue(ty, 0);
......@@ -2530,13 +2499,13 @@ pub const Type = struct {
25302499
25312500 inline .array_type, .vector_type => |seq_type| {
25322501 if (seq_type.len == 0) return (try mod.intern(.{ .aggregate = .{
2533 .ty = ty.ip_index,
2502 .ty = ty.toIntern(),
25342503 .storage = .{ .elems = &.{} },
25352504 } })).toValue();
25362505 if (try seq_type.child.toType().onePossibleValue(mod)) |opv| {
25372506 return (try mod.intern(.{ .aggregate = .{
2538 .ty = ty.ip_index,
2539 .storage = .{ .repeated_elem = opv.ip_index },
2507 .ty = ty.toIntern(),
2508 .storage = .{ .repeated_elem = opv.toIntern() },
25402509 } })).toValue();
25412510 }
25422511 return null;
......@@ -2612,7 +2581,7 @@ pub const Type = struct {
26122581 // This TODO is repeated in the redundant implementation of
26132582 // one-possible-value logic in Sema.zig.
26142583 const empty = try mod.intern(.{ .aggregate = .{
2615 .ty = ty.ip_index,
2584 .ty = ty.toIntern(),
26162585 .storage = .{ .elems = &.{} },
26172586 } });
26182587 return empty.toValue();
......@@ -2625,7 +2594,7 @@ pub const Type = struct {
26252594 // In this case the struct has all comptime-known fields and
26262595 // therefore has one possible value.
26272596 return (try mod.intern(.{ .aggregate = .{
2628 .ty = ty.ip_index,
2597 .ty = ty.toIntern(),
26292598 .storage = .{ .elems = tuple.values },
26302599 } })).toValue();
26312600 },
......@@ -2637,9 +2606,9 @@ pub const Type = struct {
26372606 const only_field = union_obj.fields.values()[0];
26382607 const val_val = (try only_field.ty.onePossibleValue(mod)) orelse return null;
26392608 const only = try mod.intern(.{ .un = .{
2640 .ty = ty.ip_index,
2641 .tag = tag_val.ip_index,
2642 .val = val_val.ip_index,
2609 .ty = ty.toIntern(),
2610 .tag = tag_val.toIntern(),
2611 .val = val_val.toIntern(),
26432612 } });
26442613 return only.toValue();
26452614 },
......@@ -2650,8 +2619,8 @@ pub const Type = struct {
26502619
26512620 if (try enum_type.tag_ty.toType().onePossibleValue(mod)) |int_opv| {
26522621 const only = try mod.intern(.{ .enum_tag = .{
2653 .ty = ty.ip_index,
2654 .int = int_opv.ip_index,
2622 .ty = ty.toIntern(),
2623 .int = int_opv.toIntern(),
26552624 } });
26562625 return only.toValue();
26572626 }
......@@ -2663,7 +2632,7 @@ pub const Type = struct {
26632632 1 => {
26642633 if (enum_type.values.len == 0) {
26652634 const only = try mod.intern(.{ .enum_tag = .{
2666 .ty = ty.ip_index,
2635 .ty = ty.toIntern(),
26672636 .int = try mod.intern(.{ .int = .{
26682637 .ty = enum_type.tag_ty,
26692638 .storage = .{ .u64 = 0 },
......@@ -2705,10 +2674,10 @@ pub const Type = struct {
27052674 /// TODO merge these implementations together with the "advanced" pattern seen
27062675 /// elsewhere in this file.
27072676 pub fn comptimeOnly(ty: Type, mod: *Module) bool {
2708 return switch (ty.ip_index) {
2677 return switch (ty.toIntern()) {
27092678 .empty_struct_type => false,
27102679
2711 else => switch (mod.intern_pool.indexToKey(ty.ip_index)) {
2680 else => switch (mod.intern_pool.indexToKey(ty.toIntern())) {
27122681 .int_type => false,
27132682 .ptr_type => |ptr_type| {
27142683 const child_ty = ptr_type.elem_type.toType();
......@@ -2880,8 +2849,7 @@ pub const Type = struct {
28802849
28812850 /// Returns null if the type has no namespace.
28822851 pub fn getNamespaceIndex(ty: Type, mod: *Module) Module.Namespace.OptionalIndex {
2883 if (ty.ip_index == .none) return .none;
2884 return switch (mod.intern_pool.indexToKey(ty.ip_index)) {
2852 return switch (mod.intern_pool.indexToKey(ty.toIntern())) {
28852853 .opaque_type => |opaque_type| opaque_type.namespace.toOptional(),
28862854 .struct_type => |struct_type| struct_type.namespace,
28872855 .union_type => |union_type| mod.unionPtr(union_type.index).namespace.toOptional(),
......@@ -2900,8 +2868,8 @@ pub const Type = struct {
29002868 pub fn minInt(ty: Type, mod: *Module) !Value {
29012869 const scalar = try minIntScalar(ty.scalarType(mod), mod);
29022870 return if (ty.zigTypeTag(mod) == .Vector) (try mod.intern(.{ .aggregate = .{
2903 .ty = ty.ip_index,
2904 .storage = .{ .repeated_elem = scalar.ip_index },
2871 .ty = ty.toIntern(),
2872 .storage = .{ .repeated_elem = scalar.toIntern() },
29052873 } })).toValue() else scalar;
29062874 }
29072875
......@@ -2929,8 +2897,8 @@ pub const Type = struct {
29292897 pub fn maxInt(ty: Type, mod: *Module, dest_ty: Type) !Value {
29302898 const scalar = try maxIntScalar(ty.scalarType(mod), mod, dest_ty);
29312899 return if (ty.zigTypeTag(mod) == .Vector) (try mod.intern(.{ .aggregate = .{
2932 .ty = ty.ip_index,
2933 .storage = .{ .repeated_elem = scalar.ip_index },
2900 .ty = ty.toIntern(),
2901 .storage = .{ .repeated_elem = scalar.toIntern() },
29342902 } })).toValue() else scalar;
29352903 }
29362904
......@@ -2971,7 +2939,7 @@ pub const Type = struct {
29712939
29722940 /// Asserts the type is an enum or a union.
29732941 pub fn intTagType(ty: Type, mod: *Module) !Type {
2974 return switch (mod.intern_pool.indexToKey(ty.ip_index)) {
2942 return switch (mod.intern_pool.indexToKey(ty.toIntern())) {
29752943 .union_type => |union_type| mod.unionPtr(union_type.index).tag_ty.intTagType(mod),
29762944 .enum_type => |enum_type| enum_type.tag_ty.toType(),
29772945 else => unreachable,
......@@ -2979,21 +2947,18 @@ pub const Type = struct {
29792947 }
29802948
29812949 pub fn isNonexhaustiveEnum(ty: Type, mod: *Module) bool {
2982 return switch (ty.ip_index) {
2983 .none => false,
2984 else => switch (mod.intern_pool.indexToKey(ty.ip_index)) {
2985 .enum_type => |enum_type| switch (enum_type.tag_mode) {
2986 .nonexhaustive => true,
2987 .auto, .explicit => false,
2988 },
2989 else => false,
2950 return switch (mod.intern_pool.indexToKey(ty.toIntern())) {
2951 .enum_type => |enum_type| switch (enum_type.tag_mode) {
2952 .nonexhaustive => true,
2953 .auto, .explicit => false,
29902954 },
2955 else => false,
29912956 };
29922957 }
29932958
29942959 // Asserts that `ty` is an error set and not `anyerror`.
29952960 pub fn errorSetNames(ty: Type, mod: *Module) []const InternPool.NullTerminatedString {
2996 return switch (mod.intern_pool.indexToKey(ty.ip_index)) {
2961 return switch (mod.intern_pool.indexToKey(ty.toIntern())) {
29972962 .error_set_type => |x| x.names,
29982963 .inferred_error_set_type => |index| {
29992964 const inferred_error_set = mod.inferredErrorSetPtr(index);
......@@ -3006,22 +2971,22 @@ pub const Type = struct {
30062971 }
30072972
30082973 pub fn enumFields(ty: Type, mod: *Module) []const InternPool.NullTerminatedString {
3009 return mod.intern_pool.indexToKey(ty.ip_index).enum_type.names;
2974 return mod.intern_pool.indexToKey(ty.toIntern()).enum_type.names;
30102975 }
30112976
30122977 pub fn enumFieldCount(ty: Type, mod: *Module) usize {
3013 return mod.intern_pool.indexToKey(ty.ip_index).enum_type.names.len;
2978 return mod.intern_pool.indexToKey(ty.toIntern()).enum_type.names.len;
30142979 }
30152980
30162981 pub fn enumFieldName(ty: Type, field_index: usize, mod: *Module) [:0]const u8 {
30172982 const ip = &mod.intern_pool;
3018 const field_name = ip.indexToKey(ty.ip_index).enum_type.names[field_index];
2983 const field_name = ip.indexToKey(ty.toIntern()).enum_type.names[field_index];
30192984 return ip.stringToSlice(field_name);
30202985 }
30212986
30222987 pub fn enumFieldIndex(ty: Type, field_name: []const u8, mod: *Module) ?u32 {
30232988 const ip = &mod.intern_pool;
3024 const enum_type = ip.indexToKey(ty.ip_index).enum_type;
2989 const enum_type = ip.indexToKey(ty.toIntern()).enum_type;
30252990 // If the string is not interned, then the field certainly is not present.
30262991 const field_name_interned = ip.getString(field_name).unwrap() orelse return null;
30272992 return enum_type.nameIndex(ip, field_name_interned);
......@@ -3032,9 +2997,9 @@ pub const Type = struct {
30322997 /// declaration order, or `null` if `enum_tag` does not match any field.
30332998 pub fn enumTagFieldIndex(ty: Type, enum_tag: Value, mod: *Module) ?u32 {
30342999 const ip = &mod.intern_pool;
3035 const enum_type = ip.indexToKey(ty.ip_index).enum_type;
3036 const int_tag = switch (ip.indexToKey(enum_tag.ip_index)) {
3037 .int => enum_tag.ip_index,
3000 const enum_type = ip.indexToKey(ty.toIntern()).enum_type;
3001 const int_tag = switch (ip.indexToKey(enum_tag.toIntern())) {
3002 .int => enum_tag.toIntern(),
30383003 .enum_tag => |info| info.int,
30393004 else => unreachable,
30403005 };
......@@ -3043,7 +3008,7 @@ pub const Type = struct {
30433008 }
30443009
30453010 pub fn structFields(ty: Type, mod: *Module) Module.Struct.Fields {
3046 switch (mod.intern_pool.indexToKey(ty.ip_index)) {
3011 switch (mod.intern_pool.indexToKey(ty.toIntern())) {
30473012 .struct_type => |struct_type| {
30483013 const struct_obj = mod.structPtrUnwrap(struct_type.index) orelse return .{};
30493014 assert(struct_obj.haveFieldTypes());
......@@ -3054,7 +3019,7 @@ pub const Type = struct {
30543019 }
30553020
30563021 pub fn structFieldName(ty: Type, field_index: usize, mod: *Module) []const u8 {
3057 switch (mod.intern_pool.indexToKey(ty.ip_index)) {
3022 switch (mod.intern_pool.indexToKey(ty.toIntern())) {
30583023 .struct_type => |struct_type| {
30593024 const struct_obj = mod.structPtrUnwrap(struct_type.index).?;
30603025 assert(struct_obj.haveFieldTypes());
......@@ -3069,7 +3034,7 @@ pub const Type = struct {
30693034 }
30703035
30713036 pub fn structFieldCount(ty: Type, mod: *Module) usize {
3072 return switch (mod.intern_pool.indexToKey(ty.ip_index)) {
3037 return switch (mod.intern_pool.indexToKey(ty.toIntern())) {
30733038 .struct_type => |struct_type| {
30743039 const struct_obj = mod.structPtrUnwrap(struct_type.index) orelse return 0;
30753040 assert(struct_obj.haveFieldTypes());
......@@ -3082,7 +3047,7 @@ pub const Type = struct {
30823047
30833048 /// Supports structs and unions.
30843049 pub fn structFieldType(ty: Type, index: usize, mod: *Module) Type {
3085 return switch (mod.intern_pool.indexToKey(ty.ip_index)) {
3050 return switch (mod.intern_pool.indexToKey(ty.toIntern())) {
30863051 .struct_type => |struct_type| {
30873052 const struct_obj = mod.structPtrUnwrap(struct_type.index).?;
30883053 return struct_obj.fields.values()[index].ty;
......@@ -3097,7 +3062,7 @@ pub const Type = struct {
30973062 }
30983063
30993064 pub fn structFieldAlign(ty: Type, index: usize, mod: *Module) u32 {
3100 switch (mod.intern_pool.indexToKey(ty.ip_index)) {
3065 switch (mod.intern_pool.indexToKey(ty.toIntern())) {
31013066 .struct_type => |struct_type| {
31023067 const struct_obj = mod.structPtrUnwrap(struct_type.index).?;
31033068 assert(struct_obj.layout != .Packed);
......@@ -3115,7 +3080,7 @@ pub const Type = struct {
31153080 }
31163081
31173082 pub fn structFieldDefaultValue(ty: Type, index: usize, mod: *Module) Value {
3118 switch (mod.intern_pool.indexToKey(ty.ip_index)) {
3083 switch (mod.intern_pool.indexToKey(ty.toIntern())) {
31193084 .struct_type => |struct_type| {
31203085 const struct_obj = mod.structPtrUnwrap(struct_type.index).?;
31213086 return struct_obj.fields.values()[index].default_val;
......@@ -3131,7 +3096,7 @@ pub const Type = struct {
31313096 }
31323097
31333098 pub fn structFieldValueComptime(ty: Type, mod: *Module, index: usize) !?Value {
3134 switch (mod.intern_pool.indexToKey(ty.ip_index)) {
3099 switch (mod.intern_pool.indexToKey(ty.toIntern())) {
31353100 .struct_type => |struct_type| {
31363101 const struct_obj = mod.structPtrUnwrap(struct_type.index).?;
31373102 const field = struct_obj.fields.values()[index];
......@@ -3154,7 +3119,7 @@ pub const Type = struct {
31543119 }
31553120
31563121 pub fn structFieldIsComptime(ty: Type, index: usize, mod: *Module) bool {
3157 return switch (mod.intern_pool.indexToKey(ty.ip_index)) {
3122 return switch (mod.intern_pool.indexToKey(ty.toIntern())) {
31583123 .struct_type => |struct_type| {
31593124 const struct_obj = mod.structPtrUnwrap(struct_type.index).?;
31603125 if (struct_obj.layout == .Packed) return false;
......@@ -3167,7 +3132,7 @@ pub const Type = struct {
31673132 }
31683133
31693134 pub fn packedStructFieldByteOffset(ty: Type, field_index: usize, mod: *Module) u32 {
3170 const struct_type = mod.intern_pool.indexToKey(ty.ip_index).struct_type;
3135 const struct_type = mod.intern_pool.indexToKey(ty.toIntern()).struct_type;
31713136 const struct_obj = mod.structPtrUnwrap(struct_type.index).?;
31723137 assert(struct_obj.layout == .Packed);
31733138 comptime assert(Type.packed_struct_layout_version == 2);
......@@ -3229,7 +3194,7 @@ pub const Type = struct {
32293194 /// Get an iterator that iterates over all the struct field, returning the field and
32303195 /// offset of that field. Asserts that the type is a non-packed struct.
32313196 pub fn iterateStructOffsets(ty: Type, mod: *Module) StructOffsetIterator {
3232 const struct_type = mod.intern_pool.indexToKey(ty.ip_index).struct_type;
3197 const struct_type = mod.intern_pool.indexToKey(ty.toIntern()).struct_type;
32333198 const struct_obj = mod.structPtrUnwrap(struct_type.index).?;
32343199 assert(struct_obj.haveLayout());
32353200 assert(struct_obj.layout != .Packed);
......@@ -3238,7 +3203,7 @@ pub const Type = struct {
32383203
32393204 /// Supports structs and unions.
32403205 pub fn structFieldOffset(ty: Type, index: usize, mod: *Module) u64 {
3241 switch (mod.intern_pool.indexToKey(ty.ip_index)) {
3206 switch (mod.intern_pool.indexToKey(ty.toIntern())) {
32423207 .struct_type => |struct_type| {
32433208 const struct_obj = mod.structPtrUnwrap(struct_type.index).?;
32443209 assert(struct_obj.haveLayout());
......@@ -3296,7 +3261,7 @@ pub const Type = struct {
32963261 }
32973262
32983263 pub fn declSrcLocOrNull(ty: Type, mod: *Module) ?Module.SrcLoc {
3299 return switch (mod.intern_pool.indexToKey(ty.ip_index)) {
3264 return switch (mod.intern_pool.indexToKey(ty.toIntern())) {
33003265 .struct_type => |struct_type| {
33013266 const struct_obj = mod.structPtrUnwrap(struct_type.index).?;
33023267 return struct_obj.srcLoc(mod);
......@@ -3316,7 +3281,7 @@ pub const Type = struct {
33163281 }
33173282
33183283 pub fn getOwnerDeclOrNull(ty: Type, mod: *Module) ?Module.Decl.Index {
3319 return switch (mod.intern_pool.indexToKey(ty.ip_index)) {
3284 return switch (mod.intern_pool.indexToKey(ty.toIntern())) {
33203285 .struct_type => |struct_type| {
33213286 const struct_obj = mod.structPtrUnwrap(struct_type.index) orelse return null;
33223287 return struct_obj.owner_decl;
......@@ -3332,33 +3297,30 @@ pub const Type = struct {
33323297 }
33333298
33343299 pub fn isGenericPoison(ty: Type) bool {
3335 return ty.ip_index == .generic_poison_type;
3300 return ty.toIntern() == .generic_poison_type;
33363301 }
33373302
33383303 pub fn isTuple(ty: Type, mod: *Module) bool {
3339 return switch (ty.ip_index) {
3340 .none => false,
3341 else => switch (mod.intern_pool.indexToKey(ty.ip_index)) {
3342 .struct_type => |struct_type| {
3343 const struct_obj = mod.structPtrUnwrap(struct_type.index) orelse return false;
3344 return struct_obj.is_tuple;
3345 },
3346 .anon_struct_type => |anon_struct| anon_struct.names.len == 0,
3347 else => false,
3304 return switch (mod.intern_pool.indexToKey(ty.toIntern())) {
3305 .struct_type => |struct_type| {
3306 const struct_obj = mod.structPtrUnwrap(struct_type.index) orelse return false;
3307 return struct_obj.is_tuple;
33483308 },
3309 .anon_struct_type => |anon_struct| anon_struct.names.len == 0,
3310 else => false,
33493311 };
33503312 }
33513313
33523314 pub fn isAnonStruct(ty: Type, mod: *Module) bool {
3353 if (ty.ip_index == .empty_struct_type) return true;
3354 return switch (mod.intern_pool.indexToKey(ty.ip_index)) {
3315 if (ty.toIntern() == .empty_struct_type) return true;
3316 return switch (mod.intern_pool.indexToKey(ty.toIntern())) {
33553317 .anon_struct_type => |anon_struct_type| anon_struct_type.names.len > 0,
33563318 else => false,
33573319 };
33583320 }
33593321
33603322 pub fn isTupleOrAnonStruct(ty: Type, mod: *Module) bool {
3361 return switch (mod.intern_pool.indexToKey(ty.ip_index)) {
3323 return switch (mod.intern_pool.indexToKey(ty.toIntern())) {
33623324 .struct_type => |struct_type| {
33633325 const struct_obj = mod.structPtrUnwrap(struct_type.index) orelse return false;
33643326 return struct_obj.is_tuple;
......@@ -3369,14 +3331,14 @@ pub const Type = struct {
33693331 }
33703332
33713333 pub fn isSimpleTuple(ty: Type, mod: *Module) bool {
3372 return switch (mod.intern_pool.indexToKey(ty.ip_index)) {
3334 return switch (mod.intern_pool.indexToKey(ty.toIntern())) {
33733335 .anon_struct_type => |anon_struct_type| anon_struct_type.names.len == 0,
33743336 else => false,
33753337 };
33763338 }
33773339
33783340 pub fn isSimpleTupleOrAnonStruct(ty: Type, mod: *Module) bool {
3379 return switch (mod.intern_pool.indexToKey(ty.ip_index)) {
3341 return switch (mod.intern_pool.indexToKey(ty.toIntern())) {
33803342 .anon_struct_type => true,
33813343 else => false,
33823344 };
src/value.zig+33-14
......@@ -345,7 +345,7 @@ pub const Value = struct {
345345 }
346346
347347 pub fn intern(val: Value, ty: Type, mod: *Module) Allocator.Error!InternPool.Index {
348 if (val.ip_index != .none) return mod.intern_pool.getCoerced(mod.gpa, val.toIntern(), ty.toIntern());
348 if (val.ip_index != .none) return (try mod.getCoerced(val, ty)).toIntern();
349349 switch (val.tag()) {
350350 .eu_payload => {
351351 const pl = val.castTag(.eu_payload).?.data;
......@@ -506,11 +506,7 @@ pub const Value = struct {
506506 else => unreachable,
507507 };
508508 },
509 .enum_type => |enum_type| (try ip.getCoerced(
510 mod.gpa,
511 val.toIntern(),
512 enum_type.tag_ty,
513 )).toValue(),
509 .enum_type => |enum_type| try mod.getCoerced(val, enum_type.tag_ty.toType()),
514510 else => unreachable,
515511 };
516512 }
......@@ -872,10 +868,15 @@ pub const Value = struct {
872868 .Packed => {
873869 var bits: u16 = 0;
874870 const fields = ty.structFields(mod).values();
875 const field_vals = val.castTag(.aggregate).?.data;
871 const storage = mod.intern_pool.indexToKey(val.toIntern()).aggregate.storage;
876872 for (fields, 0..) |field, i| {
877873 const field_bits = @intCast(u16, field.ty.bitSize(mod));
878 try field_vals[i].writeToPackedMemory(field.ty, mod, buffer, bit_offset + bits);
874 const field_val = switch (storage) {
875 .bytes => unreachable,
876 .elems => |elems| elems[i],
877 .repeated_elem => |elem| elem,
878 };
879 try field_val.toValue().writeToPackedMemory(field.ty, mod, buffer, bit_offset + bits);
879880 bits += field_bits;
880881 }
881882 },
......@@ -2006,23 +2007,30 @@ pub const Value = struct {
20062007 }
20072008
20082009 pub fn isPtrToThreadLocal(val: Value, mod: *Module) bool {
2010 return val.ip_index != .none and switch (mod.intern_pool.indexToKey(val.toIntern())) {
2011 .variable => false,
2012 else => val.isPtrToThreadLocalInner(mod),
2013 };
2014 }
2015
2016 pub fn isPtrToThreadLocalInner(val: Value, mod: *Module) bool {
20092017 return val.ip_index != .none and switch (mod.intern_pool.indexToKey(val.toIntern())) {
20102018 .variable => |variable| variable.is_threadlocal,
20112019 .ptr => |ptr| switch (ptr.addr) {
20122020 .decl => |decl_index| {
20132021 const decl = mod.declPtr(decl_index);
20142022 assert(decl.has_tv);
2015 return decl.val.isPtrToThreadLocal(mod);
2023 return decl.val.isPtrToThreadLocalInner(mod);
20162024 },
20172025 .mut_decl => |mut_decl| {
20182026 const decl = mod.declPtr(mut_decl.decl);
20192027 assert(decl.has_tv);
2020 return decl.val.isPtrToThreadLocal(mod);
2028 return decl.val.isPtrToThreadLocalInner(mod);
20212029 },
20222030 .int => false,
2023 .eu_payload, .opt_payload => |base_ptr| base_ptr.toValue().isPtrToThreadLocal(mod),
2024 .comptime_field => |comptime_field| comptime_field.toValue().isPtrToThreadLocal(mod),
2025 .elem, .field => |base_index| base_index.base.toValue().isPtrToThreadLocal(mod),
2031 .eu_payload, .opt_payload => |base_ptr| base_ptr.toValue().isPtrToThreadLocalInner(mod),
2032 .comptime_field => |comptime_field| comptime_field.toValue().isPtrToThreadLocalInner(mod),
2033 .elem, .field => |base_index| base_index.base.toValue().isPtrToThreadLocalInner(mod),
20262034 },
20272035 else => false,
20282036 };
......@@ -2045,7 +2053,18 @@ pub const Value = struct {
20452053 else => unreachable,
20462054 },
20472055 .aggregate => |aggregate| (try mod.intern(.{ .aggregate = .{
2048 .ty = mod.intern_pool.typeOf(val.toIntern()),
2056 .ty = switch (mod.intern_pool.indexToKey(mod.intern_pool.typeOf(val.toIntern()))) {
2057 .array_type => |array_type| try mod.arrayType(.{
2058 .len = @intCast(u32, end - start),
2059 .child = array_type.child,
2060 .sentinel = if (end == array_type.len) array_type.sentinel else .none,
2061 }),
2062 .vector_type => |vector_type| try mod.vectorType(.{
2063 .len = @intCast(u32, end - start),
2064 .child = vector_type.child,
2065 }),
2066 else => unreachable,
2067 }.toIntern(),
20492068 .storage = switch (aggregate.storage) {
20502069 .bytes => |bytes| .{ .bytes = bytes[start..end] },
20512070 .elems => |elems| .{ .elems = elems[start..end] },
tools/lldb_pretty_printers.py+10-3
......@@ -347,9 +347,15 @@ class TagAndPayload_SynthProvider:
347347 except: return -1
348348 def get_child_at_index(self, index): return (self.tag, self.payload)[index] if index in range(2) else None
349349
350def Inst_Ref_SummaryProvider(value, _=None):
350def Zir_Inst__Zir_Inst_Ref_SummaryProvider(value, _=None):
351351 members = value.type.enum_members
352 return value if any(value.unsigned == member.unsigned for member in members) else 'instructions[%d]' % (value.unsigned - len(members))
352 # ignore .var_args_param_type and .none
353 return value if any(value.unsigned == member.unsigned for member in members) else 'instructions[%d]' % (value.unsigned + 2 - len(members))
354
355def Air_Inst__Air_Inst_Ref_SummaryProvider(value, _=None):
356 members = value.type.enum_members
357 # ignore .none
358 return value if any(value.unsigned == member.unsigned for member in members) else 'instructions[%d]' % (value.unsigned + 1 - len(members))
353359
354360class Module_Decl__Module_Decl_Index_SynthProvider:
355361 def __init__(self, value, _=None): self.value = value
......@@ -676,8 +682,9 @@ def __lldb_init_module(debugger, _=None):
676682 add(debugger, category='zig.stage2', type='Zir.Inst', identifier='TagAndPayload', synth=True, inline_children=True, summary=True)
677683 add(debugger, category='zig.stage2', regex=True, type=MultiArrayList_Entry('Zir\\.Inst'), identifier='TagAndPayload', synth=True, inline_children=True, summary=True)
678684 add(debugger, category='zig.stage2', regex=True, type='^Zir\\.Inst\\.Data\\.Data__struct_[1-9][0-9]*$', inline_children=True, summary=True)
679 add(debugger, category='zig.stage2', type='Zir.Inst::Zir.Inst.Ref', identifier='Inst_Ref', summary=True)
685 add(debugger, category='zig.stage2', type='Zir.Inst::Zir.Inst.Ref', summary=True)
680686 add(debugger, category='zig.stage2', type='Air.Inst', identifier='TagAndPayload', synth=True, inline_children=True, summary=True)
687 add(debugger, category='zig.stage2', type='Air.Inst::Air.Inst.Ref', summary=True)
681688 add(debugger, category='zig.stage2', regex=True, type=MultiArrayList_Entry('Air\\.Inst'), identifier='TagAndPayload', synth=True, inline_children=True, summary=True)
682689 add(debugger, category='zig.stage2', regex=True, type='^Air\\.Inst\\.Data\\.Data__struct_[1-9][0-9]*$', inline_children=True, summary=True)
683690 add(debugger, category='zig.stage2', type='Module.Decl::Module.Decl.Index', synth=True)