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) {...@@ -650,8 +650,14 @@ pub const Key = union(enum) {
650 .enum_type => |enum_type| std.hash.autoHash(hasher, enum_type.decl),650 .enum_type => |enum_type| std.hash.autoHash(hasher, enum_type.decl),
651651
652 .variable => |variable| std.hash.autoHash(hasher, variable.decl),652 .variable => |variable| std.hash.autoHash(hasher, variable.decl),
653 .extern_func => |extern_func| std.hash.autoHash(hasher, extern_func.decl),653 .extern_func => |extern_func| {
654 .func => |func| std.hash.autoHash(hasher, func.index),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
656 .int => |int| {662 .int => |int| {
657 // Canonicalize all integers by converting them to BigIntConst.663 // Canonicalize all integers by converting them to BigIntConst.
...@@ -854,11 +860,11 @@ pub const Key = union(enum) {...@@ -854,11 +860,11 @@ pub const Key = union(enum) {
854 },860 },
855 .extern_func => |a_info| {861 .extern_func => |a_info| {
856 const b_info = b.extern_func;862 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;
858 },864 },
859 .func => |a_info| {865 .func => |a_info| {
860 const b_info = b.func;866 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;
862 },868 },
863869
864 .ptr => |a_info| {870 .ptr => |a_info| {
...@@ -1340,8 +1346,8 @@ pub const Index = enum(u32) {...@@ -1340,8 +1346,8 @@ pub const Index = enum(u32) {
1340 float_c_longdouble_f128: struct { data: *Float128 },1346 float_c_longdouble_f128: struct { data: *Float128 },
1341 float_comptime_float: struct { data: *Float128 },1347 float_comptime_float: struct { data: *Float128 },
1342 variable: struct { data: *Variable },1348 variable: struct { data: *Variable },
1343 extern_func: struct { data: void },1349 extern_func: struct { data: *Key.ExternFunc },
1344 func: struct { data: void },1350 func: struct { data: *Key.Func },
1345 only_possible_value: DataIsIndex,1351 only_possible_value: DataIsIndex,
1346 union_value: struct { data: *Key.Union },1352 union_value: struct { data: *Key.Union },
1347 bytes: struct { data: *Bytes },1353 bytes: struct { data: *Bytes },
...@@ -3216,6 +3222,7 @@ pub fn get(ip: *InternPool, gpa: Allocator, key: Key) Allocator.Error!Index {...@@ -3216,6 +3222,7 @@ pub fn get(ip: *InternPool, gpa: Allocator, key: Key) Allocator.Error!Index {
32163222
3217 .opt => |opt| {3223 .opt => |opt| {
3218 assert(ip.isOptionalType(opt.ty));3224 assert(ip.isOptionalType(opt.ty));
3225 assert(opt.val == .none or ip.indexToKey(opt.ty).opt_type == ip.typeOf(opt.val));
3219 ip.items.appendAssumeCapacity(if (opt.val == .none) .{3226 ip.items.appendAssumeCapacity(if (opt.val == .none) .{
3220 .tag = .opt_null,3227 .tag = .opt_null,
3221 .data = @enumToInt(opt.ty),3228 .data = @enumToInt(opt.ty),
...@@ -3226,23 +3233,7 @@ pub fn get(ip: *InternPool, gpa: Allocator, key: Key) Allocator.Error!Index {...@@ -3226,23 +3233,7 @@ pub fn get(ip: *InternPool, gpa: Allocator, key: Key) Allocator.Error!Index {
3226 },3233 },
32273234
3228 .int => |int| b: {3235 .int => |int| b: {
3229 switch (int.ty) {3236 assert(ip.isIntegerType(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 }
3246 switch (int.storage) {3237 switch (int.storage) {
3247 .u64, .i64, .big_int => {},3238 .u64, .i64, .big_int => {},
3248 .lazy_align, .lazy_size => |lazy_ty| {3239 .lazy_align, .lazy_size => |lazy_ty| {
...@@ -3425,13 +3416,16 @@ pub fn get(ip: *InternPool, gpa: Allocator, key: Key) Allocator.Error!Index {...@@ -3425,13 +3416,16 @@ pub fn get(ip: *InternPool, gpa: Allocator, key: Key) Allocator.Error!Index {
3425 }3416 }
3426 },3417 },
34273418
3428 .err => |err| ip.items.appendAssumeCapacity(.{3419 .err => |err| {
3429 .tag = .error_set_error,3420 assert(ip.isErrorSetType(err.ty));
3430 .data = try ip.addExtra(gpa, err),3421 ip.items.appendAssumeCapacity(.{
3431 }),3422 .tag = .error_set_error,
3423 .data = try ip.addExtra(gpa, err),
3424 });
3425 },
34323426
3433 .error_union => |error_union| {3427 .error_union => |error_union| {
3434 assert(ip.indexToKey(error_union.ty) == .error_union_type);3428 assert(ip.isErrorUnionType(error_union.ty));
3435 ip.items.appendAssumeCapacity(switch (error_union.val) {3429 ip.items.appendAssumeCapacity(switch (error_union.val) {
3436 .err_name => |err_name| .{3430 .err_name => |err_name| .{
3437 .tag = .error_union_error,3431 .tag = .error_union_error,
...@@ -3456,9 +3450,8 @@ pub fn get(ip: *InternPool, gpa: Allocator, key: Key) Allocator.Error!Index {...@@ -3456,9 +3450,8 @@ pub fn get(ip: *InternPool, gpa: Allocator, key: Key) Allocator.Error!Index {
3456 }),3450 }),
34573451
3458 .enum_tag => |enum_tag| {3452 .enum_tag => |enum_tag| {
3459 assert(enum_tag.ty != .none);3453 assert(ip.isEnumType(enum_tag.ty));
3460 assert(enum_tag.int != .none);3454 assert(ip.indexToKey(enum_tag.int) == .int);
3461
3462 ip.items.appendAssumeCapacity(.{3455 ip.items.appendAssumeCapacity(.{
3463 .tag = .enum_tag,3456 .tag = .enum_tag,
3464 .data = try ip.addExtra(gpa, enum_tag),3457 .data = try ip.addExtra(gpa, enum_tag),
...@@ -4191,69 +4184,93 @@ pub fn sliceLen(ip: InternPool, i: Index) Index {...@@ -4191,69 +4184,93 @@ pub fn sliceLen(ip: InternPool, i: Index) Index {
4191/// * identity coercion4184/// * identity coercion
4192/// * int <=> int4185/// * int <=> int
4193/// * int <=> enum4186/// * int <=> enum
4187/// * enum_literal => enum
4194/// * ptr <=> ptr4188/// * ptr <=> ptr
4195/// * null_value => opt4189/// * null_value => opt
4196/// * payload => opt4190/// * payload => opt
4197/// * error set <=> error set4191/// * error set <=> error set
4192/// * error union <=> error union
4193/// * error set => error union
4194/// * payload => error union
4195/// * fn <=> fn
4198pub fn getCoerced(ip: *InternPool, gpa: Allocator, val: Index, new_ty: Index) Allocator.Error!Index {4196pub fn getCoerced(ip: *InternPool, gpa: Allocator, val: Index, new_ty: Index) Allocator.Error!Index {
4199 const old_ty = ip.typeOf(val);4197 const old_ty = ip.typeOf(val);
4200 if (old_ty == new_ty) return val;4198 if (old_ty == new_ty) return val;
4201 switch (ip.indexToKey(val)) {4199 switch (ip.indexToKey(val)) {
4202 .int => |int| switch (ip.indexToKey(new_ty)) {4200 .extern_func => |extern_func| if (ip.isFunctionType(new_ty))
4203 .simple_type => |simple_type| switch (simple_type) {4201 return ip.get(gpa, .{ .extern_func = .{
4204 .usize,4202 .ty = new_ty,
4205 .isize,4203 .decl = extern_func.decl,
4206 .c_char,4204 .lib_name = extern_func.lib_name,
4207 .c_short,4205 } }),
4208 .c_ushort,4206 .func => |func| if (ip.isFunctionType(new_ty))
4209 .c_int,4207 return ip.get(gpa, .{ .func = .{
4210 .c_uint,4208 .ty = new_ty,
4211 .c_long,4209 .index = func.index,
4212 .c_ulong,4210 } }),
4213 .c_longlong,4211 .int => |int| if (ip.isIntegerType(new_ty))
4214 .c_ulonglong,4212 return getCoercedInts(ip, gpa, int, new_ty)
4215 .comptime_int,4213 else if (ip.isEnumType(new_ty))
4216 => return getCoercedInts(ip, gpa, int, new_ty),4214 return ip.get(gpa, .{ .enum_tag = .{
4217 else => {},
4218 },
4219 .int_type => return getCoercedInts(ip, gpa, int, new_ty),
4220 .enum_type => return ip.get(gpa, .{ .enum_tag = .{
4221 .ty = new_ty,4215 .ty = new_ty,
4222 .int = val,4216 .int = val,
4223 } }),4217 } }),
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 },
4224 else => {},4234 else => {},
4225 },4235 },
4226 .enum_tag => |enum_tag| {4236 .ptr => |ptr| if (ip.isPointerType(new_ty))
4227 // Assume new_ty is an integer type.4237 return ip.get(gpa, .{ .ptr = .{
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 = .{
4232 .ty = new_ty,4238 .ty = new_ty,
4233 .addr = ptr.addr,4239 .addr = ptr.addr,
4240 .len = ptr.len,
4234 } }),4241 } }),
4235 else => {},4242 .err => |err| if (ip.isErrorSetType(new_ty))
4236 },4243 return ip.get(gpa, .{ .err = .{
4237 .err => |err| switch (ip.indexToKey(new_ty)) {
4238 .error_set_type, .inferred_error_set_type => return ip.get(gpa, .{ .err = .{
4239 .ty = new_ty,4244 .ty = new_ty,
4240 .name = err.name,4245 .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,
4241 } }),4256 } }),
4242 else => {},
4243 },
4244 else => {},4257 else => {},
4245 }4258 }
4246 switch (ip.indexToKey(new_ty)) {4259 switch (ip.indexToKey(new_ty)) {
4247 .opt_type => |child_ty| switch (val) {4260 .opt_type => |child_type| switch (val) {
4248 .null_value => return ip.get(gpa, .{ .opt = .{4261 .null_value => return ip.get(gpa, .{ .opt = .{
4249 .ty = new_ty,4262 .ty = new_ty,
4250 .val = .none,4263 .val = .none,
4251 } }),4264 } }),
4252 else => return ip.get(gpa, .{ .opt = .{4265 else => return ip.get(gpa, .{ .opt = .{
4253 .ty = new_ty,4266 .ty = new_ty,
4254 .val = try ip.getCoerced(gpa, val, child_ty),4267 .val = try ip.getCoerced(gpa, val, child_type),
4255 } }),4268 } }),
4256 },4269 },
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 } }),
4257 else => {},4274 else => {},
4258 }4275 }
4259 if (std.debug.runtime_safety) {4276 if (std.debug.runtime_safety) {
...@@ -4271,33 +4288,24 @@ pub fn getCoercedInts(ip: *InternPool, gpa: Allocator, int: Key.Int, new_ty: Ind...@@ -4271,33 +4288,24 @@ pub fn getCoercedInts(ip: *InternPool, gpa: Allocator, int: Key.Int, new_ty: Ind
4271 // big_int storage, the limbs would be invalidated before they are read.4288 // big_int storage, the limbs would be invalidated before they are read.
4272 // Here we pre-reserve the limbs to ensure that the logic in `addInt` will4289 // Here we pre-reserve the limbs to ensure that the logic in `addInt` will
4273 // not use an invalidated limbs pointer.4290 // not use an invalidated limbs pointer.
4274 switch (int.storage) {4291 const new_storage: Key.Int.Storage = switch (int.storage) {
4275 .u64 => |x| return ip.get(gpa, .{ .int = .{4292 .u64, .i64, .lazy_align, .lazy_size => int.storage,
4276 .ty = new_ty,4293 .big_int => |big_int| storage: {
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| {
4285 const positive = big_int.positive;4294 const positive = big_int.positive;
4286 const limbs = ip.limbsSliceToIndex(big_int.limbs);4295 const limbs = ip.limbsSliceToIndex(big_int.limbs);
4287 // This line invalidates the limbs slice, but the indexes computed in the4296 // This line invalidates the limbs slice, but the indexes computed in the
4288 // previous line are still correct.4297 // previous line are still correct.
4289 try reserveLimbs(ip, gpa, @typeInfo(Int).Struct.fields.len + big_int.limbs.len);4298 try reserveLimbs(ip, gpa, @typeInfo(Int).Struct.fields.len + big_int.limbs.len);
4290 return ip.get(gpa, .{ .int = .{4299 break :storage .{ .big_int = .{
4291 .ty = new_ty,4300 .limbs = ip.limbsIndexToSlice(limbs),
4292 .storage = .{ .big_int = .{4301 .positive = positive,
4293 .limbs = ip.limbsIndexToSlice(limbs),4302 } };
4294 .positive = positive,
4295 } },
4296 } });
4297 },4303 },
42984304 };
4299 .lazy_align, .lazy_size => unreachable,4305 return ip.get(gpa, .{ .int = .{
4300 }4306 .ty = new_ty,
4307 .storage = new_storage,
4308 } });
4301}4309}
43024310
4303pub fn indexToStructType(ip: InternPool, val: Index) Module.Struct.OptionalIndex {4311pub fn indexToStructType(ip: InternPool, val: Index) Module.Struct.OptionalIndex {
...@@ -4345,25 +4353,68 @@ pub fn indexToInferredErrorSetType(ip: InternPool, val: Index) Module.Fn.Inferre...@@ -4345,25 +4353,68 @@ pub fn indexToInferredErrorSetType(ip: InternPool, val: Index) Module.Fn.Inferre
4345 return @intToEnum(Module.Fn.InferredErrorSet.Index, datas[@enumToInt(val)]).toOptional();4353 return @intToEnum(Module.Fn.InferredErrorSet.Index, datas[@enumToInt(val)]).toOptional();
4346}4354}
43474355
4348pub fn isPointerType(ip: InternPool, ty: Index) bool {4356/// includes .comptime_int_type
4349 const tags = ip.items.items(.tag);4357pub fn isIntegerType(ip: InternPool, ty: Index) bool {
4350 if (ty == .none) return false;4358 return switch (ty) {
4351 return switch (tags[@enumToInt(ty)]) {4359 .usize_type,
4352 .type_pointer, .type_slice => true,4360 .isize_type,
4353 else => false,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,
4354 };4389 };
4355}4390}
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
4357pub fn isOptionalType(ip: InternPool, ty: Index) bool {4400pub fn isOptionalType(ip: InternPool, ty: Index) bool {
4358 const tags = ip.items.items(.tag);4401 return ip.indexToKey(ty) == .opt_type;
4359 if (ty == .none) return false;4402}
4360 return tags[@enumToInt(ty)] == .type_optional;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 };
4361}4410}
43624411
4363pub fn isInferredErrorSetType(ip: InternPool, ty: Index) bool {4412pub fn isInferredErrorSetType(ip: InternPool, ty: Index) bool {
4364 const tags = ip.items.items(.tag);4413 return ip.indexToKey(ty) == .inferred_error_set_type;
4365 assert(ty != .none);4414}
4366 return tags[@enumToInt(ty)] == .type_inferred_error_set;4415
4416pub fn isErrorUnionType(ip: InternPool, ty: Index) bool {
4417 return ip.indexToKey(ty) == .error_union_type;
4367}4418}
43684419
4369/// The is only legal because the initializer is not part of the hash.4420/// 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...@@ -6699,6 +6699,11 @@ pub fn intern(mod: *Module, key: InternPool.Key) Allocator.Error!InternPool.Inde
6699 return mod.intern_pool.get(mod.gpa, key);6699 return mod.intern_pool.get(mod.gpa, key);
6700}6700}
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
6702pub fn intType(mod: *Module, signedness: std.builtin.Signedness, bits: u16) Allocator.Error!Type {6707pub fn intType(mod: *Module, signedness: std.builtin.Signedness, bits: u16) Allocator.Error!Type {
6703 const i = try intern(mod, .{ .int_type = .{6708 const i = try intern(mod, .{ .int_type = .{
6704 .signedness = signedness,6709 .signedness = signedness,
src/Sema.zig+113-105
...@@ -7821,7 +7821,6 @@ fn resolveGenericInstantiationType(...@@ -7821,7 +7821,6 @@ fn resolveGenericInstantiationType(
7821 const new_func_inst = try child_sema.resolveBody(&child_block, fn_info.param_body, fn_info.param_body_inst);7821 const new_func_inst = try child_sema.resolveBody(&child_block, fn_info.param_body, fn_info.param_body_inst);
7822 const new_func_val = child_sema.resolveConstValue(&child_block, .unneeded, new_func_inst, undefined) catch unreachable;7822 const new_func_val = child_sema.resolveConstValue(&child_block, .unneeded, new_func_inst, undefined) catch unreachable;
7823 const new_func = new_func_val.getFunctionIndex(mod).unwrap().?;7823 const new_func = new_func_val.getFunctionIndex(mod).unwrap().?;
7824 errdefer mod.destroyFunc(new_func);
7825 assert(new_func == new_module_func);7824 assert(new_func == new_module_func);
78267825
7827 arg_i = 0;7826 arg_i = 0;
...@@ -10793,7 +10792,7 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError...@@ -10793,7 +10792,7 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError
10793 check_range: {10792 check_range: {
10794 if (operand_ty.zigTypeTag(mod) == .Int) {10793 if (operand_ty.zigTypeTag(mod) == .Int) {
10795 const min_int = try operand_ty.minInt(mod);10794 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);
10797 if (try range_set.spans(min_int, max_int, operand_ty)) {10796 if (try range_set.spans(min_int, max_int, operand_ty)) {
10798 if (special_prong == .@"else") {10797 if (special_prong == .@"else") {
10799 return sema.fail(10798 return sema.fail(
...@@ -11649,7 +11648,7 @@ const RangeSetUnhandledIterator = struct {...@@ -11649,7 +11648,7 @@ const RangeSetUnhandledIterator = struct {
11649 fn init(sema: *Sema, ty: Type, range_set: RangeSet) !RangeSetUnhandledIterator {11648 fn init(sema: *Sema, ty: Type, range_set: RangeSet) !RangeSetUnhandledIterator {
11650 const mod = sema.mod;11649 const mod = sema.mod;
11651 const min = try ty.minInt(mod);11650 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
11654 return RangeSetUnhandledIterator{11653 return RangeSetUnhandledIterator{
11655 .sema = sema,11654 .sema = sema,
...@@ -15964,25 +15963,24 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai...@@ -15964,25 +15963,24 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
15964 }15963 }
1596515964
15966 const args_val = v: {15965 const args_val = v: {
15967 const args_slice_ty = try mod.ptrType(.{15966 const new_decl_ty = try mod.arrayType(.{
15968 .elem_type = param_info_ty.toIntern(),15967 .len = param_vals.len,
15969 .size = .Slice,15968 .child = param_info_ty.toIntern(),
15970 .is_const = true,
15971 });15969 });
15972 const new_decl = try params_anon_decl.finish(15970 const new_decl = try params_anon_decl.finish(
15973 try mod.arrayType(.{15971 new_decl_ty,
15974 .len = param_vals.len,
15975 .child = param_info_ty.toIntern(),
15976 .sentinel = .none,
15977 }),
15978 (try mod.intern(.{ .aggregate = .{15972 (try mod.intern(.{ .aggregate = .{
15979 .ty = args_slice_ty.toIntern(),15973 .ty = new_decl_ty.toIntern(),
15980 .storage = .{ .elems = param_vals },15974 .storage = .{ .elems = param_vals },
15981 } })).toValue(),15975 } })).toValue(),
15982 0, // default alignment15976 0, // default alignment
15983 );15977 );
15984 break :v try mod.intern(.{ .ptr = .{15978 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(),
15986 .addr = .{ .decl = new_decl },15984 .addr = .{ .decl = new_decl },
15987 .len = (try mod.intValue(Type.usize, param_vals.len)).toIntern(),15985 .len = (try mod.intValue(Type.usize, param_vals.len)).toIntern(),
15988 } });15986 } });
...@@ -16214,7 +16212,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai...@@ -16214,7 +16212,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
16214 };16212 };
16215 return sema.addConstant(type_info_ty, (try mod.intern(.{ .un = .{16213 return sema.addConstant(type_info_ty, (try mod.intern(.{ .un = .{
16216 .ty = type_info_ty.toIntern(),16214 .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(),
16218 .val = try mod.intern(.{ .aggregate = .{16216 .val = try mod.intern(.{ .aggregate = .{
16219 .ty = optional_field_ty.toIntern(),16217 .ty = optional_field_ty.toIntern(),
16220 .storage = .{ .elems = &field_values },16218 .storage = .{ .elems = &field_values },
...@@ -16258,7 +16256,6 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai...@@ -16258,7 +16256,6 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
16258 const new_decl_ty = try mod.arrayType(.{16256 const new_decl_ty = try mod.arrayType(.{
16259 .len = name.len,16257 .len = name.len,
16260 .child = .u8_type,16258 .child = .u8_type,
16261 .sentinel = .zero_u8,
16262 });16259 });
16263 const new_decl = try anon_decl.finish(16260 const new_decl = try anon_decl.finish(
16264 new_decl_ty,16261 new_decl_ty,
...@@ -16269,8 +16266,9 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai...@@ -16269,8 +16266,9 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
16269 0, // default alignment16266 0, // default alignment
16270 );16267 );
16271 break :v try mod.intern(.{ .ptr = .{16268 break :v try mod.intern(.{ .ptr = .{
16272 .ty = .slice_const_u8_sentinel_0_type,16269 .ty = .slice_const_u8_type,
16273 .addr = .{ .decl = new_decl },16270 .addr = .{ .decl = new_decl },
16271 .len = (try mod.intValue(Type.usize, name.len)).toIntern(),
16274 } });16272 } });
16275 };16273 };
1627616274
...@@ -16386,7 +16384,6 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai...@@ -16386,7 +16384,6 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
16386 const new_decl_ty = try mod.arrayType(.{16384 const new_decl_ty = try mod.arrayType(.{
16387 .len = name.len,16385 .len = name.len,
16388 .child = .u8_type,16386 .child = .u8_type,
16389 .sentinel = .zero_u8,
16390 });16387 });
16391 const new_decl = try anon_decl.finish(16388 const new_decl = try anon_decl.finish(
16392 new_decl_ty,16389 new_decl_ty,
...@@ -16397,8 +16394,9 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai...@@ -16397,8 +16394,9 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
16397 0, // default alignment16394 0, // default alignment
16398 );16395 );
16399 break :v try mod.intern(.{ .ptr = .{16396 break :v try mod.intern(.{ .ptr = .{
16400 .ty = .slice_const_u8_sentinel_0_type,16397 .ty = .slice_const_u8_type,
16401 .addr = .{ .decl = new_decl },16398 .addr = .{ .decl = new_decl },
16399 .len = (try mod.intValue(Type.usize, name.len)).toIntern(),
16402 } });16400 } });
16403 };16401 };
1640416402
...@@ -16521,7 +16519,6 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai...@@ -16521,7 +16519,6 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
16521 const new_decl_ty = try mod.arrayType(.{16519 const new_decl_ty = try mod.arrayType(.{
16522 .len = name.len,16520 .len = name.len,
16523 .child = .u8_type,16521 .child = .u8_type,
16524 .sentinel = .zero_u8,
16525 });16522 });
16526 const new_decl = try anon_decl.finish(16523 const new_decl = try anon_decl.finish(
16527 new_decl_ty,16524 new_decl_ty,
...@@ -16532,8 +16529,9 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai...@@ -16532,8 +16529,9 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
16532 0, // default alignment16529 0, // default alignment
16533 );16530 );
16534 break :v try mod.intern(.{ .ptr = .{16531 break :v try mod.intern(.{ .ptr = .{
16535 .ty = .slice_const_u8_sentinel_0_type,16532 .ty = .slice_const_u8_type,
16536 .addr = .{ .decl = new_decl },16533 .addr = .{ .decl = new_decl },
16534 .len = (try mod.intValue(Type.usize, name.len)).toIntern(),
16537 } });16535 } });
16538 };16536 };
1653916537
...@@ -16663,12 +16661,10 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai...@@ -16663,12 +16661,10 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
16663 const struct_type = switch (mod.intern_pool.indexToKey(struct_ty.toIntern())) {16661 const struct_type = switch (mod.intern_pool.indexToKey(struct_ty.toIntern())) {
16664 .anon_struct_type => |tuple| {16662 .anon_struct_type => |tuple| {
16665 struct_field_vals = try gpa.alloc(InternPool.Index, tuple.types.len);16663 struct_field_vals = try gpa.alloc(InternPool.Index, tuple.types.len);
16666 for (16664 for (struct_field_vals, 0..) |*struct_field_val, i| {
16667 tuple.types,16665 const anon_struct_type = mod.intern_pool.indexToKey(struct_ty.toIntern()).anon_struct_type;
16668 tuple.values,16666 const field_ty = anon_struct_type.types[i];
16669 struct_field_vals,16667 const field_val = anon_struct_type.values[i];
16670 0..,
16671 ) |field_ty, field_val, *struct_field_val, i| {
16672 const name_val = v: {16668 const name_val = v: {
16673 var anon_decl = try block.startAnonDecl();16669 var anon_decl = try block.startAnonDecl();
16674 defer anon_decl.deinit();16670 defer anon_decl.deinit();
...@@ -16735,7 +16731,6 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai...@@ -16735,7 +16731,6 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
16735 const new_decl_ty = try mod.arrayType(.{16731 const new_decl_ty = try mod.arrayType(.{
16736 .len = name.len,16732 .len = name.len,
16737 .child = .u8_type,16733 .child = .u8_type,
16738 .sentinel = .zero_u8,
16739 });16734 });
16740 const new_decl = try anon_decl.finish(16735 const new_decl = try anon_decl.finish(
16741 new_decl_ty,16736 new_decl_ty,
...@@ -16746,7 +16741,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai...@@ -16746,7 +16741,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
16746 0, // default alignment16741 0, // default alignment
16747 );16742 );
16748 break :v try mod.intern(.{ .ptr = .{16743 break :v try mod.intern(.{ .ptr = .{
16749 .ty = .slice_const_u8_sentinel_0_type,16744 .ty = .slice_const_u8_type,
16750 .addr = .{ .decl = new_decl },16745 .addr = .{ .decl = new_decl },
16751 .len = (try mod.intValue(Type.usize, name.len)).toIntern(),16746 .len = (try mod.intValue(Type.usize, name.len)).toIntern(),
16752 } });16747 } });
...@@ -16975,7 +16970,6 @@ fn typeInfoNamespaceDecls(...@@ -16975,7 +16970,6 @@ fn typeInfoNamespaceDecls(
16975 const new_decl_ty = try mod.arrayType(.{16970 const new_decl_ty = try mod.arrayType(.{
16976 .len = name.len,16971 .len = name.len,
16977 .child = .u8_type,16972 .child = .u8_type,
16978 .sentinel = .zero_u8,
16979 });16973 });
16980 const new_decl = try anon_decl.finish(16974 const new_decl = try anon_decl.finish(
16981 new_decl_ty,16975 new_decl_ty,
...@@ -16986,7 +16980,7 @@ fn typeInfoNamespaceDecls(...@@ -16986,7 +16980,7 @@ fn typeInfoNamespaceDecls(
16986 0, // default alignment16980 0, // default alignment
16987 );16981 );
16988 break :v try mod.intern(.{ .ptr = .{16982 break :v try mod.intern(.{ .ptr = .{
16989 .ty = .slice_const_u8_sentinel_0_type,16983 .ty = .slice_const_u8_type,
16990 .addr = .{ .decl = new_decl },16984 .addr = .{ .decl = new_decl },
16991 .len = (try mod.intValue(Type.usize, name.len)).toIntern(),16985 .len = (try mod.intValue(Type.usize, name.len)).toIntern(),
16992 } });16986 } });
...@@ -20404,7 +20398,7 @@ fn zirPtrCast(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air...@@ -20404,7 +20398,7 @@ fn zirPtrCast(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air
20404 .val = operand_val.toIntern(),20398 .val = operand_val.toIntern(),
20405 } })).toValue());20399 } })).toValue());
20406 }20400 }
20407 return sema.addConstant(aligned_dest_ty, operand_val);20401 return sema.addConstant(aligned_dest_ty, try mod.getCoerced(operand_val, aligned_dest_ty));
20408 }20402 }
2040920403
20410 try sema.requireRuntimeBlock(block, src, null);20404 try sema.requireRuntimeBlock(block, src, null);
...@@ -22401,7 +22395,7 @@ fn analyzeMinMax(...@@ -22401,7 +22395,7 @@ fn analyzeMinMax(
22401 if (std.debug.runtime_safety) {22395 if (std.debug.runtime_safety) {
22402 assert(try sema.intFitsInType(val, refined_ty, null));22396 assert(try sema.intFitsInType(val, refined_ty, null));
22403 }22397 }
22404 cur_minmax = try sema.addConstant(refined_ty, val);22398 cur_minmax = try sema.addConstant(refined_ty, try mod.getCoerced(val, refined_ty));
22405 }22399 }
2240622400
22407 break :refined refined_ty;22401 break :refined refined_ty;
...@@ -22459,8 +22453,8 @@ fn analyzeMinMax(...@@ -22459,8 +22453,8 @@ fn analyzeMinMax(
22459 else => unreachable,22453 else => unreachable,
22460 };22454 };
22461 const max_val = switch (air_tag) {22455 const max_val = switch (air_tag) {
22462 .min => try comptime_elem_ty.maxInt(mod, Type.comptime_int), // @min(ct, rt) <= ct22456 .min => try comptime_elem_ty.maxInt(mod, comptime_elem_ty), // @min(ct, rt) <= ct
22463 .max => try unrefined_elem_ty.maxInt(mod, Type.comptime_int),22457 .max => try unrefined_elem_ty.maxInt(mod, unrefined_elem_ty),
22464 else => unreachable,22458 else => unreachable,
22465 };22459 };
2246622460
...@@ -23356,11 +23350,14 @@ fn zirBuiltinExtern(...@@ -23356,11 +23350,14 @@ fn zirBuiltinExtern(
23356 try mod.declareDeclDependency(sema.owner_decl_index, new_decl_index);23350 try mod.declareDeclDependency(sema.owner_decl_index, new_decl_index);
23357 try sema.ensureDeclAnalyzed(new_decl_index);23351 try sema.ensureDeclAnalyzed(new_decl_index);
2335823352
23359 const ref = try mod.intern(.{ .ptr = .{23353 return sema.addConstant(ty, try mod.getCoerced((try mod.intern(.{ .ptr = .{
23360 .ty = (try mod.singleConstPtrType(ty)).toIntern(),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 },
23361 .addr = .{ .decl = new_decl_index },23359 .addr = .{ .decl = new_decl_index },
23362 } });23360 } })).toValue(), ty));
23363 return sema.addConstant(ty, ref.toValue());
23364}23361}
2336523362
23366fn zirWorkItem(23363fn zirWorkItem(
...@@ -25887,13 +25884,7 @@ fn coerceExtra(...@@ -25887,13 +25884,7 @@ fn coerceExtra(
25887 var in_memory_result = try sema.coerceInMemoryAllowed(block, dest_ty, inst_ty, false, target, dest_ty_src, inst_src);25884 var in_memory_result = try sema.coerceInMemoryAllowed(block, dest_ty, inst_ty, false, target, dest_ty_src, inst_src);
25888 if (in_memory_result == .ok) {25885 if (in_memory_result == .ok) {
25889 if (maybe_inst_val) |val| {25886 if (maybe_inst_val) |val| {
25890 if (val.ip_index == .none) {25887 return sema.addConstant(dest_ty, try mod.getCoerced(val, dest_ty));
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 }
25897 }25888 }
25898 try sema.requireRuntimeBlock(block, inst_src, null);25889 try sema.requireRuntimeBlock(block, inst_src, null);
25899 return block.addBitCast(dest_ty, inst);25890 return block.addBitCast(dest_ty, inst);
...@@ -26269,8 +26260,7 @@ fn coerceExtra(...@@ -26269,8 +26260,7 @@ fn coerceExtra(
26269 if (!opts.report_err) return error.NotCoercible;26260 if (!opts.report_err) return error.NotCoercible;
26270 return sema.fail(block, inst_src, "type '{}' cannot represent integer value '{}'", .{ dest_ty.fmt(sema.mod), val.fmtValue(inst_ty, sema.mod) });26261 return sema.fail(block, inst_src, "type '{}' cannot represent integer value '{}'", .{ dest_ty.fmt(sema.mod), val.fmtValue(inst_ty, sema.mod) });
26271 }26262 }
26272 const new_val = try mod.intern_pool.getCoerced(sema.gpa, val.toIntern(), dest_ty.toIntern());26263 return try sema.addConstant(dest_ty, try mod.getCoerced(val, dest_ty));
26273 return try sema.addConstant(dest_ty, new_val.toValue());
26274 }26264 }
26275 if (dest_ty.zigTypeTag(mod) == .ComptimeInt) {26265 if (dest_ty.zigTypeTag(mod) == .ComptimeInt) {
26276 if (!opts.report_err) return error.NotCoercible;26266 if (!opts.report_err) return error.NotCoercible;
...@@ -27222,68 +27212,84 @@ fn coerceInMemoryAllowedFns(...@@ -27222,68 +27212,84 @@ fn coerceInMemoryAllowedFns(
27222 src_src: LazySrcLoc,27212 src_src: LazySrcLoc,
27223) !InMemoryCoercionResult {27213) !InMemoryCoercionResult {
27224 const mod = sema.mod;27214 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) {27216 {
27229 return InMemoryCoercionResult{ .fn_var_args = dest_info.is_var_args };27217 const dest_info = mod.typeToFunc(dest_ty).?;
27230 }27218 const src_info = mod.typeToFunc(src_ty).?;
2723127219
27232 if (dest_info.is_generic != src_info.is_generic) {27220 if (dest_info.is_var_args != src_info.is_var_args) {
27233 return InMemoryCoercionResult{ .fn_generic = dest_info.is_generic };27221 return InMemoryCoercionResult{ .fn_var_args = dest_info.is_var_args };
27234 }27222 }
2723527223
27236 if (dest_info.cc != src_info.cc) {27224 if (dest_info.is_generic != src_info.is_generic) {
27237 return InMemoryCoercionResult{ .fn_cc = .{27225 return InMemoryCoercionResult{ .fn_generic = dest_info.is_generic };
27238 .actual = src_info.cc,27226 }
27239 .wanted = dest_info.cc,
27240 } };
27241 }
2724227227
27243 if (src_info.return_type != .noreturn_type) {27228 if (dest_info.cc != src_info.cc) {
27244 const rt = try sema.coerceInMemoryAllowed(block, dest_info.return_type.toType(), src_info.return_type.toType(), false, target, dest_src, src_src);27229 return InMemoryCoercionResult{ .fn_cc = .{
27245 if (rt != .ok) {27230 .actual = src_info.cc,
27246 return InMemoryCoercionResult{ .fn_return_type = .{27231 .wanted = dest_info.cc,
27247 .child = try rt.dupe(sema.arena),
27248 .actual = src_info.return_type.toType(),
27249 .wanted = dest_info.return_type.toType(),
27250 } };27232 } };
27251 }27233 }
27252 }
2725327234
27254 if (dest_info.param_types.len != src_info.param_types.len) {27235 if (src_info.return_type != .noreturn_type) {
27255 return InMemoryCoercionResult{ .fn_param_count = .{27236 const dest_return_type = dest_info.return_type.toType();
27256 .actual = src_info.param_types.len,27237 const src_return_type = src_info.return_type.toType();
27257 .wanted = dest_info.param_types.len,27238 const rt = try sema.coerceInMemoryAllowed(block, dest_return_type, src_return_type, false, target, dest_src, src_src);
27258 } };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 }
27259 }27247 }
2726027248
27261 if (dest_info.noalias_bits != src_info.noalias_bits) {27249 const params_len = params_len: {
27262 return InMemoryCoercionResult{ .fn_param_noalias = .{27250 const dest_info = mod.typeToFunc(dest_ty).?;
27263 .actual = src_info.noalias_bits,27251 const src_info = mod.typeToFunc(src_ty).?;
27264 .wanted = dest_info.noalias_bits,27252
27265 } };27253 if (dest_info.param_types.len != src_info.param_types.len) {
27266 }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| {27260 if (dest_info.noalias_bits != src_info.noalias_bits) {
27269 const src_param_ty = src_info.param_types[i].toType();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);27274 const dest_param_ty = dest_info.param_types[param_i].toType();
27272 if (dest_info.paramIsComptime(i_small) != src_info.paramIsComptime(i_small)) {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)) {
27273 return InMemoryCoercionResult{ .fn_param_comptime = .{27279 return InMemoryCoercionResult{ .fn_param_comptime = .{
27274 .index = i,27280 .index = param_i,
27275 .wanted = dest_info.paramIsComptime(i_small),27281 .wanted = dest_info.paramIsComptime(param_i_small),
27276 } };27282 } };
27277 }27283 }
2727827284
27279 // Note: Cast direction is reversed here.27285 // 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);
27281 if (param != .ok) {27287 if (param != .ok) {
27282 return InMemoryCoercionResult{ .fn_param = .{27288 return InMemoryCoercionResult{ .fn_param = .{
27283 .child = try param.dupe(sema.arena),27289 .child = try param.dupe(sema.arena),
27284 .actual = src_param_ty,27290 .actual = src_param_ty,
27285 .wanted = dest_param_ty.toType(),27291 .wanted = dest_param_ty,
27286 .index = i,27292 .index = param_i,
27287 } };27293 } };
27288 }27294 }
27289 }27295 }
...@@ -28385,7 +28391,7 @@ fn beginComptimePtrLoad(...@@ -28385,7 +28391,7 @@ fn beginComptimePtrLoad(
28385 };28391 };
28386 },28392 },
28387 .elem => |elem_ptr| blk: {28393 .elem => |elem_ptr| blk: {
28388 const elem_ty = ptr.ty.toType().childType(mod);28394 const elem_ty = ptr.ty.toType().elemType2(mod);
28389 var deref = try sema.beginComptimePtrLoad(block, src, elem_ptr.base.toValue(), null);28395 var deref = try sema.beginComptimePtrLoad(block, src, elem_ptr.base.toValue(), null);
2839028396
28391 // This code assumes that elem_ptrs have been "flattened" in order for direct dereference28397 // This code assumes that elem_ptrs have been "flattened" in order for direct dereference
...@@ -28678,11 +28684,10 @@ fn coerceCompatiblePtrs(...@@ -28678,11 +28684,10 @@ fn coerceCompatiblePtrs(
28678 return sema.fail(block, inst_src, "null pointer casted to type '{}'", .{dest_ty.fmt(sema.mod)});28684 return sema.fail(block, inst_src, "null pointer casted to type '{}'", .{dest_ty.fmt(sema.mod)});
28679 }28685 }
28680 // The comptime Value representation is compatible with both types.28686 // The comptime Value representation is compatible with both types.
28681 return sema.addConstant(dest_ty, (try mod.intern_pool.getCoerced(28687 return sema.addConstant(
28682 sema.gpa,28688 dest_ty,
28683 try val.intern(inst_ty, mod),28689 try mod.getCoerced((try val.intern(inst_ty, mod)).toValue(), dest_ty),
28684 dest_ty.toIntern(),28690 );
28685 )).toValue());
28686 }28691 }
28687 try sema.requireRuntimeBlock(block, inst_src, null);28692 try sema.requireRuntimeBlock(block, inst_src, null);
28688 const inst_allows_zero = inst_ty.zigTypeTag(mod) != .Pointer or inst_ty.ptrAllowsZero(mod);28693 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 {...@@ -29390,9 +29395,13 @@ fn refValue(sema: *Sema, block: *Block, ty: Type, val: Value) !Value {
2939029395
29391fn optRefValue(sema: *Sema, block: *Block, ty: Type, opt_val: ?Value) !Value {29396fn optRefValue(sema: *Sema, block: *Block, ty: Type, opt_val: ?Value) !Value {
29392 const mod = sema.mod;29397 const mod = sema.mod;
29398 const ptr_anyopaque_ty = try mod.singleConstPtrType(Type.anyopaque);
29393 return (try mod.intern(.{ .opt = .{29399 return (try mod.intern(.{ .opt = .{
29394 .ty = (try mod.optionalType((try mod.singleConstPtrType(Type.anyopaque)).toIntern())).toIntern(),29400 .ty = (try mod.optionalType(ptr_anyopaque_ty.toIntern())).toIntern(),
29395 .val = if (opt_val) |val| (try sema.refValue(block, ty, val)).toIntern() else .none,29401 .val = if (opt_val) |val| (try mod.getCoerced(
29402 try sema.refValue(block, ty, val),
29403 ptr_anyopaque_ty,
29404 )).toIntern() else .none,
29396 } })).toValue();29405 } })).toValue();
29397}29406}
2939829407
...@@ -30051,11 +30060,10 @@ fn analyzeSlice(...@@ -30051,11 +30060,10 @@ fn analyzeSlice(
30051 };30060 };
3005230061
30053 if (!new_ptr_val.isUndef(mod)) {30062 if (!new_ptr_val.isUndef(mod)) {
30054 return sema.addConstant(return_ty, (try mod.intern_pool.getCoerced(30063 return sema.addConstant(return_ty, try mod.getCoerced(
30055 sema.gpa,30064 (try new_ptr_val.intern(new_ptr_ty, mod)).toValue(),
30056 try new_ptr_val.intern(new_ptr_ty, mod),30065 return_ty,
30057 return_ty.toIntern(),30066 ));
30058 )).toValue());
30059 }30067 }
3006030068
30061 // Special case: @as([]i32, undefined)[x..x]30069 // Special case: @as([]i32, undefined)[x..x]
...@@ -34237,9 +34245,9 @@ fn enumHasInt(sema: *Sema, ty: Type, int: Value) CompileError!bool {...@@ -34237,9 +34245,9 @@ fn enumHasInt(sema: *Sema, ty: Type, int: Value) CompileError!bool {
34237 // The `tagValueIndex` function call below relies on the type being the integer tag type.34245 // The `tagValueIndex` function call below relies on the type being the integer tag type.
34238 // `getCoerced` assumes the value will fit the new type.34246 // `getCoerced` assumes the value will fit the new type.
34239 if (!(try sema.intFitsInType(int, enum_type.tag_ty.toType(), null))) return false;34247 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;
34243}34251}
3424434252
34245fn intAddWithOverflow(34253fn intAddWithOverflow(
src/codegen.zig+15-13
...@@ -185,7 +185,7 @@ pub fn generateSymbol(...@@ -185,7 +185,7 @@ pub fn generateSymbol(
185185
186 const mod = bin_file.options.module.?;186 const mod = bin_file.options.module.?;
187 var typed_value = arg_tv;187 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())) {
189 .runtime_value => |rt| typed_value.val = rt.val.toValue(),189 .runtime_value => |rt| typed_value.val = rt.val.toValue(),
190 else => {},190 else => {},
191 }191 }
...@@ -204,7 +204,7 @@ pub fn generateSymbol(...@@ -204,7 +204,7 @@ pub fn generateSymbol(
204 return .ok;204 return .ok;
205 }205 }
206206
207 switch (mod.intern_pool.indexToKey(typed_value.val.ip_index)) {207 switch (mod.intern_pool.indexToKey(typed_value.val.toIntern())) {
208 .int_type,208 .int_type,
209 .ptr_type,209 .ptr_type,
210 .array_type,210 .array_type,
...@@ -282,7 +282,7 @@ pub fn generateSymbol(...@@ -282,7 +282,7 @@ pub fn generateSymbol(
282 switch (try generateSymbol(bin_file, src_loc, .{282 switch (try generateSymbol(bin_file, src_loc, .{
283 .ty = payload_ty,283 .ty = payload_ty,
284 .val = switch (error_union.val) {284 .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() }),
286 .payload => |payload| payload,286 .payload => |payload| payload,
287 }.toValue(),287 }.toValue(),
288 }, code, debug_output, reloc_info)) {288 }, code, debug_output, reloc_info)) {
...@@ -315,7 +315,7 @@ pub fn generateSymbol(...@@ -315,7 +315,7 @@ pub fn generateSymbol(
315 const int_tag_ty = try typed_value.ty.intTagType(mod);315 const int_tag_ty = try typed_value.ty.intTagType(mod);
316 switch (try generateSymbol(bin_file, src_loc, .{316 switch (try generateSymbol(bin_file, src_loc, .{
317 .ty = int_tag_ty,317 .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),
319 }, code, debug_output, reloc_info)) {319 }, code, debug_output, reloc_info)) {
320 .ok => {},320 .ok => {},
321 .fail => |em| return .{ .fail = em },321 .fail => |em| return .{ .fail = em },
...@@ -337,7 +337,7 @@ pub fn generateSymbol(...@@ -337,7 +337,7 @@ pub fn generateSymbol(
337 switch (try lowerParentPtr(bin_file, src_loc, switch (ptr.len) {337 switch (try lowerParentPtr(bin_file, src_loc, switch (ptr.len) {
338 .none => typed_value.val,338 .none => typed_value.val,
339 else => typed_value.val.slicePtr(mod),339 else => typed_value.val.slicePtr(mod),
340 }.ip_index, code, debug_output, reloc_info)) {340 }.toIntern(), code, debug_output, reloc_info)) {
341 .ok => {},341 .ok => {},
342 .fail => |em| return .{ .fail = em },342 .fail => |em| return .{ .fail = em },
343 }343 }
...@@ -372,7 +372,7 @@ pub fn generateSymbol(...@@ -372,7 +372,7 @@ pub fn generateSymbol(
372 } else {372 } else {
373 const padding = abi_size - (math.cast(usize, payload_type.abiSize(mod)) orelse return error.Overflow) - 1;373 const padding = abi_size - (math.cast(usize, payload_type.abiSize(mod)) orelse return error.Overflow) - 1;
374 if (payload_type.hasRuntimeBits(mod)) {374 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();
376 switch (try generateSymbol(bin_file, src_loc, .{376 switch (try generateSymbol(bin_file, src_loc, .{
377 .ty = payload_type,377 .ty = payload_type,
378 .val = value,378 .val = value,
...@@ -385,7 +385,7 @@ pub fn generateSymbol(...@@ -385,7 +385,7 @@ pub fn generateSymbol(
385 try code.writer().writeByteNTimes(0, padding);385 try code.writer().writeByteNTimes(0, padding);
386 }386 }
387 },387 },
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())) {
389 .array_type => |array_type| {389 .array_type => |array_type| {
390 var index: u64 = 0;390 var index: u64 = 0;
391 while (index < array_type.len) : (index += 1) {391 while (index < array_type.len) : (index += 1) {
...@@ -850,7 +850,7 @@ pub fn genTypedValue(...@@ -850,7 +850,7 @@ pub fn genTypedValue(
850) CodeGenError!GenResult {850) CodeGenError!GenResult {
851 const mod = bin_file.options.module.?;851 const mod = bin_file.options.module.?;
852 var typed_value = arg_tv;852 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())) {
854 .runtime_value => |rt| typed_value.val = rt.val.toValue(),854 .runtime_value => |rt| typed_value.val = rt.val.toValue(),
855 else => {},855 else => {},
856 }856 }
...@@ -866,7 +866,7 @@ pub fn genTypedValue(...@@ -866,7 +866,7 @@ pub fn genTypedValue(
866 const target = bin_file.options.target;866 const target = bin_file.options.target;
867 const ptr_bits = target.ptrBitWidth();867 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())) {
870 .ptr => |ptr| switch (ptr.addr) {870 .ptr => |ptr| switch (ptr.addr) {
871 .decl => |decl| return genDeclRef(bin_file, src_loc, typed_value, decl),871 .decl => |decl| return genDeclRef(bin_file, src_loc, typed_value, decl),
872 .mut_decl => |mut_decl| return genDeclRef(bin_file, src_loc, typed_value, mut_decl.decl),872 .mut_decl => |mut_decl| return genDeclRef(bin_file, src_loc, typed_value, mut_decl.decl),
...@@ -879,12 +879,12 @@ pub fn genTypedValue(...@@ -879,12 +879,12 @@ pub fn genTypedValue(
879 .Void => return GenResult.mcv(.none),879 .Void => return GenResult.mcv(.none),
880 .Pointer => switch (typed_value.ty.ptrSize(mod)) {880 .Pointer => switch (typed_value.ty.ptrSize(mod)) {
881 .Slice => {},881 .Slice => {},
882 else => switch (typed_value.val.ip_index) {882 else => switch (typed_value.val.toIntern()) {
883 .null_value => {883 .null_value => {
884 return GenResult.mcv(.{ .immediate = 0 });884 return GenResult.mcv(.{ .immediate = 0 });
885 },885 },
886 .none => {},886 .none => {},
887 else => switch (mod.intern_pool.indexToKey(typed_value.val.ip_index)) {887 else => switch (mod.intern_pool.indexToKey(typed_value.val.toIntern())) {
888 .int => {888 .int => {
889 return GenResult.mcv(.{ .immediate = typed_value.val.toUnsignedInt(mod) });889 return GenResult.mcv(.{ .immediate = typed_value.val.toUnsignedInt(mod) });
890 },890 },
...@@ -916,7 +916,7 @@ pub fn genTypedValue(...@@ -916,7 +916,7 @@ pub fn genTypedValue(
916 }916 }
917 },917 },
918 .Enum => {918 .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;
920 const int_tag_ty = mod.intern_pool.typeOf(enum_tag.int);920 const int_tag_ty = mod.intern_pool.typeOf(enum_tag.int);
921 return genTypedValue(bin_file, src_loc, .{921 return genTypedValue(bin_file, src_loc, .{
922 .ty = int_tag_ty.toType(),922 .ty = int_tag_ty.toType(),
...@@ -924,7 +924,9 @@ pub fn genTypedValue(...@@ -924,7 +924,9 @@ pub fn genTypedValue(
924 }, owner_decl_index);924 }, owner_decl_index);
925 },925 },
926 .ErrorSet => {926 .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 );
928 const global_error_set = mod.global_error_set;930 const global_error_set = mod.global_error_set;
929 const error_index = global_error_set.get(err_name).?;931 const error_index = global_error_set.get(err_name).?;
930 return GenResult.mcv(.{ .immediate = error_index });932 return GenResult.mcv(.{ .immediate = error_index });
src/codegen/llvm.zig+1-1
...@@ -2329,7 +2329,7 @@ pub const Object = struct {...@@ -2329,7 +2329,7 @@ pub const Object = struct {
2329 try param_di_types.append(try o.lowerDebugType(ptr_ty, .full));2329 try param_di_types.append(try o.lowerDebugType(ptr_ty, .full));
2330 }2330 }
23312331
2332 for (fn_info.param_types) |param_ty| {2332 for (mod.typeToFunc(ty).?.param_types) |param_ty| {
2333 if (!param_ty.toType().hasRuntimeBitsIgnoreComptime(mod)) continue;2333 if (!param_ty.toType().hasRuntimeBitsIgnoreComptime(mod)) continue;
23342334
2335 if (isByRef(param_ty.toType(), mod)) {2335 if (isByRef(param_ty.toType(), mod)) {
src/type.zig+177-215
...@@ -23,7 +23,7 @@ pub const Type = struct {...@@ -23,7 +23,7 @@ pub const Type = struct {
23 }23 }
2424
25 pub fn zigTypeTagOrPoison(ty: Type, mod: *const Module) error{GenericPoison}!std.builtin.TypeId {25 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())) {
27 .int_type => .Int,27 .int_type => .Int,
28 .ptr_type => .Pointer,28 .ptr_type => .Pointer,
29 .array_type => .Array,29 .array_type => .Array,
...@@ -170,7 +170,7 @@ pub const Type = struct {...@@ -170,7 +170,7 @@ pub const Type = struct {
170170
171 /// Asserts the type is a pointer.171 /// Asserts the type is a pointer.
172 pub fn ptrIsMutable(ty: Type, mod: *const Module) bool {172 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;
174 }174 }
175175
176 pub const ArrayInfo = struct {176 pub const ArrayInfo = struct {
...@@ -199,26 +199,23 @@ pub const Type = struct {...@@ -199,26 +199,23 @@ pub const Type = struct {
199 }199 }
200200
201 pub fn ptrInfo(ty: Type, mod: *const Module) Payload.Pointer.Data {201 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()));
203 }203 }
204204
205 pub fn eql(a: Type, b: Type, mod: *const Module) bool {205 pub fn eql(a: Type, b: Type, mod: *const Module) bool {
206 _ = mod; // TODO: remove this parameter206 _ = mod; // TODO: remove this parameter
207 assert(a.ip_index != .none);
208 assert(b.ip_index != .none);
209 // The InternPool data structure hashes based on Key to make interned objects207 // The InternPool data structure hashes based on Key to make interned objects
210 // unique. An Index can be treated simply as u32 value for the208 // unique. An Index can be treated simply as u32 value for the
211 // purpose of Type/Value hashing and equality.209 // purpose of Type/Value hashing and equality.
212 return a.ip_index == b.ip_index;210 return a.toIntern() == b.toIntern();
213 }211 }
214212
215 pub fn hash(ty: Type, mod: *const Module) u32 {213 pub fn hash(ty: Type, mod: *const Module) u32 {
216 _ = mod; // TODO: remove this parameter214 _ = mod; // TODO: remove this parameter
217 assert(ty.ip_index != .none);
218 // The InternPool data structure hashes based on Key to make interned objects215 // The InternPool data structure hashes based on Key to make interned objects
219 // unique. An Index can be treated simply as u32 value for the216 // unique. An Index can be treated simply as u32 value for the
220 // purpose of Type/Value hashing and equality.217 // purpose of Type/Value hashing and equality.
221 return std.hash.uint32(@enumToInt(ty.ip_index));218 return std.hash.uint32(@enumToInt(ty.toIntern()));
222 }219 }
223220
224 pub fn format(ty: Type, comptime unused_fmt_string: []const u8, options: std.fmt.FormatOptions, writer: anytype) !void {221 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 {...@@ -280,7 +277,7 @@ pub const Type = struct {
280277
281 /// Prints a name suitable for `@typeName`.278 /// Prints a name suitable for `@typeName`.
282 pub fn print(ty: Type, writer: anytype, mod: *Module) @TypeOf(writer).Error!void {279 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())) {
284 .int_type => |int_type| {281 .int_type => |int_type| {
285 const sign_char: u8 = switch (int_type.signedness) {282 const sign_char: u8 = switch (int_type.signedness) {
286 .signed => 'i',283 .signed => 'i',
...@@ -520,10 +517,10 @@ pub const Type = struct {...@@ -520,10 +517,10 @@ pub const Type = struct {
520 ignore_comptime_only: bool,517 ignore_comptime_only: bool,
521 strat: AbiAlignmentAdvancedStrat,518 strat: AbiAlignmentAdvancedStrat,
522 ) RuntimeBitsError!bool {519 ) RuntimeBitsError!bool {
523 return switch (ty.ip_index) {520 return switch (ty.toIntern()) {
524 // False because it is a comptime-only type.521 // False because it is a comptime-only type.
525 .empty_struct_type => false,522 .empty_struct_type => false,
526 else => switch (mod.intern_pool.indexToKey(ty.ip_index)) {523 else => switch (mod.intern_pool.indexToKey(ty.toIntern())) {
527 .int_type => |int_type| int_type.bits != 0,524 .int_type => |int_type| int_type.bits != 0,
528 .ptr_type => |ptr_type| {525 .ptr_type => |ptr_type| {
529 // Pointers to zero-bit types still have a runtime address; however, pointers526 // Pointers to zero-bit types still have a runtime address; however, pointers
...@@ -710,7 +707,7 @@ pub const Type = struct {...@@ -710,7 +707,7 @@ pub const Type = struct {
710 /// readFrom/writeToMemory are supported only for types with a well-707 /// readFrom/writeToMemory are supported only for types with a well-
711 /// defined memory layout708 /// defined memory layout
712 pub fn hasWellDefinedLayout(ty: Type, mod: *Module) bool {709 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())) {
714 .int_type,711 .int_type,
715 .ptr_type,712 .ptr_type,
716 .vector_type,713 .vector_type,
...@@ -847,7 +844,7 @@ pub const Type = struct {...@@ -847,7 +844,7 @@ pub const Type = struct {
847 }844 }
848845
849 pub fn isNoReturn(ty: Type, mod: *Module) bool {846 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());
851 }848 }
852849
853 /// Returns 0 if the pointer is naturally aligned and the element type is 0-bit.850 /// Returns 0 if the pointer is naturally aligned and the element type is 0-bit.
...@@ -856,7 +853,7 @@ pub const Type = struct {...@@ -856,7 +853,7 @@ pub const Type = struct {
856 }853 }
857854
858 pub fn ptrAlignmentAdvanced(ty: Type, mod: *Module, opt_sema: ?*Sema) !u32 {855 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())) {
860 .ptr_type => |ptr_type| {857 .ptr_type => |ptr_type| {
861 if (ptr_type.alignment.toByteUnitsOptional()) |a| {858 if (ptr_type.alignment.toByteUnitsOptional()) |a| {
862 return @intCast(u32, a);859 return @intCast(u32, a);
...@@ -873,7 +870,7 @@ pub const Type = struct {...@@ -873,7 +870,7 @@ pub const Type = struct {
873 }870 }
874871
875 pub fn ptrAddressSpace(ty: Type, mod: *const Module) std.builtin.AddressSpace {872 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())) {
877 .ptr_type => |ptr_type| ptr_type.address_space,874 .ptr_type => |ptr_type| ptr_type.address_space,
878 .opt_type => |child| mod.intern_pool.indexToKey(child).ptr_type.address_space,875 .opt_type => |child| mod.intern_pool.indexToKey(child).ptr_type.address_space,
879 else => unreachable,876 else => unreachable,
...@@ -923,9 +920,9 @@ pub const Type = struct {...@@ -923,9 +920,9 @@ pub const Type = struct {
923 else => null,920 else => null,
924 };921 };
925922
926 switch (ty.ip_index) {923 switch (ty.toIntern()) {
927 .empty_struct_type => return AbiAlignmentAdvanced{ .scalar = 0 },924 .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())) {
929 .int_type => |int_type| {926 .int_type => |int_type| {
930 if (int_type.bits == 0) return AbiAlignmentAdvanced{ .scalar = 0 };927 if (int_type.bits == 0) return AbiAlignmentAdvanced{ .scalar = 0 };
931 return AbiAlignmentAdvanced{ .scalar = intAbiAlignment(int_type.bits, target) };928 return AbiAlignmentAdvanced{ .scalar = intAbiAlignment(int_type.bits, target) };
...@@ -1040,7 +1037,7 @@ pub const Type = struct {...@@ -1040,7 +1037,7 @@ pub const Type = struct {
1040 .sema => unreachable, // handled above1037 .sema => unreachable, // handled above
1041 .lazy => return .{ .val = (try mod.intern(.{ .int = .{1038 .lazy => return .{ .val = (try mod.intern(.{ .int = .{
1042 .ty = .comptime_int_type,1039 .ty = .comptime_int_type,
1043 .storage = .{ .lazy_align = ty.ip_index },1040 .storage = .{ .lazy_align = ty.toIntern() },
1044 } })).toValue() },1041 } })).toValue() },
1045 };1042 };
1046 if (struct_obj.layout == .Packed) {1043 if (struct_obj.layout == .Packed) {
...@@ -1048,7 +1045,7 @@ pub const Type = struct {...@@ -1048,7 +1045,7 @@ pub const Type = struct {
1048 .sema => |sema| try sema.resolveTypeLayout(ty),1045 .sema => |sema| try sema.resolveTypeLayout(ty),
1049 .lazy => if (!struct_obj.haveLayout()) return .{ .val = (try mod.intern(.{ .int = .{1046 .lazy => if (!struct_obj.haveLayout()) return .{ .val = (try mod.intern(.{ .int = .{
1050 .ty = .comptime_int_type,1047 .ty = .comptime_int_type,
1051 .storage = .{ .lazy_align = ty.ip_index },1048 .storage = .{ .lazy_align = ty.toIntern() },
1052 } })).toValue() },1049 } })).toValue() },
1053 .eager => {},1050 .eager => {},
1054 }1051 }
...@@ -1062,7 +1059,7 @@ pub const Type = struct {...@@ -1062,7 +1059,7 @@ pub const Type = struct {
1062 if (!(field.ty.hasRuntimeBitsAdvanced(mod, false, strat) catch |err| switch (err) {1059 if (!(field.ty.hasRuntimeBitsAdvanced(mod, false, strat) catch |err| switch (err) {
1063 error.NeedLazy => return .{ .val = (try mod.intern(.{ .int = .{1060 error.NeedLazy => return .{ .val = (try mod.intern(.{ .int = .{
1064 .ty = .comptime_int_type,1061 .ty = .comptime_int_type,
1065 .storage = .{ .lazy_align = ty.ip_index },1062 .storage = .{ .lazy_align = ty.toIntern() },
1066 } })).toValue() },1063 } })).toValue() },
1067 else => |e| return e,1064 else => |e| return e,
1068 })) continue;1065 })) continue;
...@@ -1076,7 +1073,7 @@ pub const Type = struct {...@@ -1076,7 +1073,7 @@ pub const Type = struct {
1076 .sema => unreachable, // handled above1073 .sema => unreachable, // handled above
1077 .lazy => return .{ .val = (try mod.intern(.{ .int = .{1074 .lazy => return .{ .val = (try mod.intern(.{ .int = .{
1078 .ty = .comptime_int_type,1075 .ty = .comptime_int_type,
1079 .storage = .{ .lazy_align = ty.ip_index },1076 .storage = .{ .lazy_align = ty.toIntern() },
1080 } })).toValue() },1077 } })).toValue() },
1081 },1078 },
1082 };1079 };
...@@ -1106,7 +1103,7 @@ pub const Type = struct {...@@ -1106,7 +1103,7 @@ pub const Type = struct {
1106 .sema => unreachable, // passed to abiAlignmentAdvanced above1103 .sema => unreachable, // passed to abiAlignmentAdvanced above
1107 .lazy => return .{ .val = (try mod.intern(.{ .int = .{1104 .lazy => return .{ .val = (try mod.intern(.{ .int = .{
1108 .ty = .comptime_int_type,1105 .ty = .comptime_int_type,
1109 .storage = .{ .lazy_align = ty.ip_index },1106 .storage = .{ .lazy_align = ty.toIntern() },
1110 } })).toValue() },1107 } })).toValue() },
1111 },1108 },
1112 }1109 }
...@@ -1157,7 +1154,7 @@ pub const Type = struct {...@@ -1157,7 +1154,7 @@ pub const Type = struct {
1157 if (!(payload_ty.hasRuntimeBitsAdvanced(mod, false, strat) catch |err| switch (err) {1154 if (!(payload_ty.hasRuntimeBitsAdvanced(mod, false, strat) catch |err| switch (err) {
1158 error.NeedLazy => return .{ .val = (try mod.intern(.{ .int = .{1155 error.NeedLazy => return .{ .val = (try mod.intern(.{ .int = .{
1159 .ty = .comptime_int_type,1156 .ty = .comptime_int_type,
1160 .storage = .{ .lazy_align = ty.ip_index },1157 .storage = .{ .lazy_align = ty.toIntern() },
1161 } })).toValue() },1158 } })).toValue() },
1162 else => |e| return e,1159 else => |e| return e,
1163 })) {1160 })) {
...@@ -1179,7 +1176,7 @@ pub const Type = struct {...@@ -1179,7 +1176,7 @@ pub const Type = struct {
1179 }1176 }
1180 return .{ .val = (try mod.intern(.{ .int = .{1177 return .{ .val = (try mod.intern(.{ .int = .{
1181 .ty = .comptime_int_type,1178 .ty = .comptime_int_type,
1182 .storage = .{ .lazy_align = ty.ip_index },1179 .storage = .{ .lazy_align = ty.toIntern() },
1183 } })).toValue() };1180 } })).toValue() };
1184 },1181 },
1185 }1182 }
...@@ -1205,7 +1202,7 @@ pub const Type = struct {...@@ -1205,7 +1202,7 @@ pub const Type = struct {
1205 if (!(child_type.hasRuntimeBitsAdvanced(mod, false, strat) catch |err| switch (err) {1202 if (!(child_type.hasRuntimeBitsAdvanced(mod, false, strat) catch |err| switch (err) {
1206 error.NeedLazy => return .{ .val = (try mod.intern(.{ .int = .{1203 error.NeedLazy => return .{ .val = (try mod.intern(.{ .int = .{
1207 .ty = .comptime_int_type,1204 .ty = .comptime_int_type,
1208 .storage = .{ .lazy_align = ty.ip_index },1205 .storage = .{ .lazy_align = ty.toIntern() },
1209 } })).toValue() },1206 } })).toValue() },
1210 else => |e| return e,1207 else => |e| return e,
1211 })) {1208 })) {
...@@ -1217,7 +1214,7 @@ pub const Type = struct {...@@ -1217,7 +1214,7 @@ pub const Type = struct {
1217 .scalar => |x| return AbiAlignmentAdvanced{ .scalar = @max(x, 1) },1214 .scalar => |x| return AbiAlignmentAdvanced{ .scalar = @max(x, 1) },
1218 .val => return .{ .val = (try mod.intern(.{ .int = .{1215 .val => return .{ .val = (try mod.intern(.{ .int = .{
1219 .ty = .comptime_int_type,1216 .ty = .comptime_int_type,
1220 .storage = .{ .lazy_align = ty.ip_index },1217 .storage = .{ .lazy_align = ty.toIntern() },
1221 } })).toValue() },1218 } })).toValue() },
1222 },1219 },
1223 }1220 }
...@@ -1249,7 +1246,7 @@ pub const Type = struct {...@@ -1249,7 +1246,7 @@ pub const Type = struct {
1249 .sema => unreachable, // handled above1246 .sema => unreachable, // handled above
1250 .lazy => return .{ .val = (try mod.intern(.{ .int = .{1247 .lazy => return .{ .val = (try mod.intern(.{ .int = .{
1251 .ty = .comptime_int_type,1248 .ty = .comptime_int_type,
1252 .storage = .{ .lazy_align = ty.ip_index },1249 .storage = .{ .lazy_align = ty.toIntern() },
1253 } })).toValue() },1250 } })).toValue() },
1254 };1251 };
1255 if (union_obj.fields.count() == 0) {1252 if (union_obj.fields.count() == 0) {
...@@ -1266,7 +1263,7 @@ pub const Type = struct {...@@ -1266,7 +1263,7 @@ pub const Type = struct {
1266 if (!(field.ty.hasRuntimeBitsAdvanced(mod, false, strat) catch |err| switch (err) {1263 if (!(field.ty.hasRuntimeBitsAdvanced(mod, false, strat) catch |err| switch (err) {
1267 error.NeedLazy => return .{ .val = (try mod.intern(.{ .int = .{1264 error.NeedLazy => return .{ .val = (try mod.intern(.{ .int = .{
1268 .ty = .comptime_int_type,1265 .ty = .comptime_int_type,
1269 .storage = .{ .lazy_align = ty.ip_index },1266 .storage = .{ .lazy_align = ty.toIntern() },
1270 } })).toValue() },1267 } })).toValue() },
1271 else => |e| return e,1268 else => |e| return e,
1272 })) continue;1269 })) continue;
...@@ -1280,7 +1277,7 @@ pub const Type = struct {...@@ -1280,7 +1277,7 @@ pub const Type = struct {
1280 .sema => unreachable, // handled above1277 .sema => unreachable, // handled above
1281 .lazy => return .{ .val = (try mod.intern(.{ .int = .{1278 .lazy => return .{ .val = (try mod.intern(.{ .int = .{
1282 .ty = .comptime_int_type,1279 .ty = .comptime_int_type,
1283 .storage = .{ .lazy_align = ty.ip_index },1280 .storage = .{ .lazy_align = ty.toIntern() },
1284 } })).toValue() },1281 } })).toValue() },
1285 },1282 },
1286 };1283 };
...@@ -1321,10 +1318,10 @@ pub const Type = struct {...@@ -1321,10 +1318,10 @@ pub const Type = struct {
1321 ) Module.CompileError!AbiSizeAdvanced {1318 ) Module.CompileError!AbiSizeAdvanced {
1322 const target = mod.getTarget();1319 const target = mod.getTarget();
13231320
1324 switch (ty.ip_index) {1321 switch (ty.toIntern()) {
1325 .empty_struct_type => return AbiSizeAdvanced{ .scalar = 0 },1322 .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())) {
1328 .int_type => |int_type| {1325 .int_type => |int_type| {
1329 if (int_type.bits == 0) return AbiSizeAdvanced{ .scalar = 0 };1326 if (int_type.bits == 0) return AbiSizeAdvanced{ .scalar = 0 };
1330 return AbiSizeAdvanced{ .scalar = intAbiSize(int_type.bits, target) };1327 return AbiSizeAdvanced{ .scalar = intAbiSize(int_type.bits, target) };
...@@ -1343,7 +1340,7 @@ pub const Type = struct {...@@ -1343,7 +1340,7 @@ pub const Type = struct {
1343 .sema, .eager => unreachable,1340 .sema, .eager => unreachable,
1344 .lazy => return .{ .val = (try mod.intern(.{ .int = .{1341 .lazy => return .{ .val = (try mod.intern(.{ .int = .{
1345 .ty = .comptime_int_type,1342 .ty = .comptime_int_type,
1346 .storage = .{ .lazy_size = ty.ip_index },1343 .storage = .{ .lazy_size = ty.toIntern() },
1347 } })).toValue() },1344 } })).toValue() },
1348 },1345 },
1349 }1346 }
...@@ -1354,7 +1351,7 @@ pub const Type = struct {...@@ -1354,7 +1351,7 @@ pub const Type = struct {
1354 .eager => null,1351 .eager => null,
1355 .lazy => return .{ .val = (try mod.intern(.{ .int = .{1352 .lazy => return .{ .val = (try mod.intern(.{ .int = .{
1356 .ty = .comptime_int_type,1353 .ty = .comptime_int_type,
1357 .storage = .{ .lazy_size = ty.ip_index },1354 .storage = .{ .lazy_size = ty.toIntern() },
1358 } })).toValue() },1355 } })).toValue() },
1359 };1356 };
1360 const elem_bits_u64 = try vector_type.child.toType().bitSizeAdvanced(mod, opt_sema);1357 const elem_bits_u64 = try vector_type.child.toType().bitSizeAdvanced(mod, opt_sema);
...@@ -1365,7 +1362,7 @@ pub const Type = struct {...@@ -1365,7 +1362,7 @@ pub const Type = struct {
1365 .scalar => |x| x,1362 .scalar => |x| x,
1366 .val => return .{ .val = (try mod.intern(.{ .int = .{1363 .val => return .{ .val = (try mod.intern(.{ .int = .{
1367 .ty = .comptime_int_type,1364 .ty = .comptime_int_type,
1368 .storage = .{ .lazy_size = ty.ip_index },1365 .storage = .{ .lazy_size = ty.toIntern() },
1369 } })).toValue() },1366 } })).toValue() },
1370 };1367 };
1371 const result = std.mem.alignForwardGeneric(u32, total_bytes, alignment);1368 const result = std.mem.alignForwardGeneric(u32, total_bytes, alignment);
...@@ -1385,7 +1382,7 @@ pub const Type = struct {...@@ -1385,7 +1382,7 @@ pub const Type = struct {
1385 if (!(payload_ty.hasRuntimeBitsAdvanced(mod, false, strat) catch |err| switch (err) {1382 if (!(payload_ty.hasRuntimeBitsAdvanced(mod, false, strat) catch |err| switch (err) {
1386 error.NeedLazy => return .{ .val = (try mod.intern(.{ .int = .{1383 error.NeedLazy => return .{ .val = (try mod.intern(.{ .int = .{
1387 .ty = .comptime_int_type,1384 .ty = .comptime_int_type,
1388 .storage = .{ .lazy_size = ty.ip_index },1385 .storage = .{ .lazy_size = ty.toIntern() },
1389 } })).toValue() },1386 } })).toValue() },
1390 else => |e| return e,1387 else => |e| return e,
1391 })) {1388 })) {
...@@ -1401,7 +1398,7 @@ pub const Type = struct {...@@ -1401,7 +1398,7 @@ pub const Type = struct {
1401 .eager => unreachable,1398 .eager => unreachable,
1402 .lazy => return .{ .val = (try mod.intern(.{ .int = .{1399 .lazy => return .{ .val = (try mod.intern(.{ .int = .{
1403 .ty = .comptime_int_type,1400 .ty = .comptime_int_type,
1404 .storage = .{ .lazy_size = ty.ip_index },1401 .storage = .{ .lazy_size = ty.toIntern() },
1405 } })).toValue() },1402 } })).toValue() },
1406 },1403 },
1407 };1404 };
...@@ -1489,7 +1486,7 @@ pub const Type = struct {...@@ -1489,7 +1486,7 @@ pub const Type = struct {
1489 .sema => |sema| try sema.resolveTypeLayout(ty),1486 .sema => |sema| try sema.resolveTypeLayout(ty),
1490 .lazy => if (!struct_obj.haveLayout()) return .{ .val = (try mod.intern(.{ .int = .{1487 .lazy => if (!struct_obj.haveLayout()) return .{ .val = (try mod.intern(.{ .int = .{
1491 .ty = .comptime_int_type,1488 .ty = .comptime_int_type,
1492 .storage = .{ .lazy_size = ty.ip_index },1489 .storage = .{ .lazy_size = ty.toIntern() },
1493 } })).toValue() },1490 } })).toValue() },
1494 .eager => {},1491 .eager => {},
1495 }1492 }
...@@ -1504,7 +1501,7 @@ pub const Type = struct {...@@ -1504,7 +1501,7 @@ pub const Type = struct {
1504 return AbiSizeAdvanced{ .scalar = 0 };1501 return AbiSizeAdvanced{ .scalar = 0 };
1505 if (!struct_obj.haveLayout()) return .{ .val = (try mod.intern(.{ .int = .{1502 if (!struct_obj.haveLayout()) return .{ .val = (try mod.intern(.{ .int = .{
1506 .ty = .comptime_int_type,1503 .ty = .comptime_int_type,
1507 .storage = .{ .lazy_size = ty.ip_index },1504 .storage = .{ .lazy_size = ty.toIntern() },
1508 } })).toValue() };1505 } })).toValue() };
1509 },1506 },
1510 .eager => {},1507 .eager => {},
...@@ -1568,7 +1565,7 @@ pub const Type = struct {...@@ -1568,7 +1565,7 @@ pub const Type = struct {
1568 .sema => |sema| try sema.resolveTypeLayout(ty),1565 .sema => |sema| try sema.resolveTypeLayout(ty),
1569 .lazy => if (!union_obj.haveLayout()) return .{ .val = (try mod.intern(.{ .int = .{1566 .lazy => if (!union_obj.haveLayout()) return .{ .val = (try mod.intern(.{ .int = .{
1570 .ty = .comptime_int_type,1567 .ty = .comptime_int_type,
1571 .storage = .{ .lazy_size = ty.ip_index },1568 .storage = .{ .lazy_size = ty.toIntern() },
1572 } })).toValue() },1569 } })).toValue() },
1573 .eager => {},1570 .eager => {},
1574 }1571 }
...@@ -1589,7 +1586,7 @@ pub const Type = struct {...@@ -1589,7 +1586,7 @@ pub const Type = struct {
1589 if (!(child_ty.hasRuntimeBitsAdvanced(mod, false, strat) catch |err| switch (err) {1586 if (!(child_ty.hasRuntimeBitsAdvanced(mod, false, strat) catch |err| switch (err) {
1590 error.NeedLazy => return .{ .val = (try mod.intern(.{ .int = .{1587 error.NeedLazy => return .{ .val = (try mod.intern(.{ .int = .{
1591 .ty = .comptime_int_type,1588 .ty = .comptime_int_type,
1592 .storage = .{ .lazy_size = ty.ip_index },1589 .storage = .{ .lazy_size = ty.toIntern() },
1593 } })).toValue() },1590 } })).toValue() },
1594 else => |e| return e,1591 else => |e| return e,
1595 })) return AbiSizeAdvanced{ .scalar = 1 };1592 })) return AbiSizeAdvanced{ .scalar = 1 };
...@@ -1605,7 +1602,7 @@ pub const Type = struct {...@@ -1605,7 +1602,7 @@ pub const Type = struct {
1605 .eager => unreachable,1602 .eager => unreachable,
1606 .lazy => return .{ .val = (try mod.intern(.{ .int = .{1603 .lazy => return .{ .val = (try mod.intern(.{ .int = .{
1607 .ty = .comptime_int_type,1604 .ty = .comptime_int_type,
1608 .storage = .{ .lazy_size = ty.ip_index },1605 .storage = .{ .lazy_size = ty.toIntern() },
1609 } })).toValue() },1606 } })).toValue() },
1610 },1607 },
1611 };1608 };
...@@ -1647,7 +1644,7 @@ pub const Type = struct {...@@ -1647,7 +1644,7 @@ pub const Type = struct {
16471644
1648 const strat: AbiAlignmentAdvancedStrat = if (opt_sema) |sema| .{ .sema = sema } else .eager;1645 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())) {
1651 .int_type => |int_type| return int_type.bits,1648 .int_type => |int_type| return int_type.bits,
1652 .ptr_type => |ptr_type| switch (ptr_type.size) {1649 .ptr_type => |ptr_type| switch (ptr_type.size) {
1653 .Slice => return target.ptrBitWidth() * 2,1650 .Slice => return target.ptrBitWidth() * 2,
...@@ -1820,7 +1817,7 @@ pub const Type = struct {...@@ -1820,7 +1817,7 @@ pub const Type = struct {
1820 }1817 }
18211818
1822 pub fn isSinglePointer(ty: Type, mod: *const Module) bool {1819 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())) {
1824 .ptr_type => |ptr_info| ptr_info.size == .One,1821 .ptr_type => |ptr_info| ptr_info.size == .One,
1825 else => false,1822 else => false,
1826 };1823 };
...@@ -1833,33 +1830,27 @@ pub const Type = struct {...@@ -1833,33 +1830,27 @@ pub const Type = struct {
18331830
1834 /// Returns `null` if `ty` is not a pointer.1831 /// Returns `null` if `ty` is not a pointer.
1835 pub fn ptrSizeOrNull(ty: Type, mod: *const Module) ?std.builtin.Type.Pointer.Size {1832 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())) {
1837 .ptr_type => |ptr_info| ptr_info.size,1834 .ptr_type => |ptr_info| ptr_info.size,
1838 else => null,1835 else => null,
1839 };1836 };
1840 }1837 }
18411838
1842 pub fn isSlice(ty: Type, mod: *const Module) bool {1839 pub fn isSlice(ty: Type, mod: *const Module) bool {
1843 return switch (ty.ip_index) {1840 return switch (mod.intern_pool.indexToKey(ty.toIntern())) {
1844 .none => false,1841 .ptr_type => |ptr_type| ptr_type.size == .Slice,
1845 else => switch (mod.intern_pool.indexToKey(ty.ip_index)) {1842 else => false,
1846 .ptr_type => |ptr_type| ptr_type.size == .Slice,
1847 else => false,
1848 },
1849 };1843 };
1850 }1844 }
18511845
1852 pub fn slicePtrFieldType(ty: Type, mod: *const Module) Type {1846 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();
1854 }1848 }
18551849
1856 pub fn isConstPtr(ty: Type, mod: *const Module) bool {1850 pub fn isConstPtr(ty: Type, mod: *const Module) bool {
1857 return switch (ty.ip_index) {1851 return switch (mod.intern_pool.indexToKey(ty.toIntern())) {
1858 .none => false,1852 .ptr_type => |ptr_type| ptr_type.is_const,
1859 else => return switch (mod.intern_pool.indexToKey(ty.ip_index)) {1853 else => false,
1860 .ptr_type => |ptr_type| ptr_type.is_const,
1861 else => false,
1862 },
1863 };1854 };
1864 }1855 }
18651856
...@@ -1868,53 +1859,41 @@ pub const Type = struct {...@@ -1868,53 +1859,41 @@ pub const Type = struct {
1868 }1859 }
18691860
1870 pub fn isVolatilePtrIp(ty: Type, ip: InternPool) bool {1861 pub fn isVolatilePtrIp(ty: Type, ip: InternPool) bool {
1871 return switch (ty.ip_index) {1862 return switch (ip.indexToKey(ty.toIntern())) {
1872 .none => false,1863 .ptr_type => |ptr_type| ptr_type.is_volatile,
1873 else => switch (ip.indexToKey(ty.ip_index)) {1864 else => false,
1874 .ptr_type => |ptr_type| ptr_type.is_volatile,
1875 else => false,
1876 },
1877 };1865 };
1878 }1866 }
18791867
1880 pub fn isAllowzeroPtr(ty: Type, mod: *const Module) bool {1868 pub fn isAllowzeroPtr(ty: Type, mod: *const Module) bool {
1881 return switch (ty.ip_index) {1869 return switch (mod.intern_pool.indexToKey(ty.toIntern())) {
1882 .none => false,1870 .ptr_type => |ptr_type| ptr_type.is_allowzero,
1883 else => switch (mod.intern_pool.indexToKey(ty.ip_index)) {1871 .opt_type => true,
1884 .ptr_type => |ptr_type| ptr_type.is_allowzero,1872 else => false,
1885 .opt_type => true,
1886 else => false,
1887 },
1888 };1873 };
1889 }1874 }
18901875
1891 pub fn isCPtr(ty: Type, mod: *const Module) bool {1876 pub fn isCPtr(ty: Type, mod: *const Module) bool {
1892 return switch (ty.ip_index) {1877 return switch (mod.intern_pool.indexToKey(ty.toIntern())) {
1893 .none => false,1878 .ptr_type => |ptr_type| ptr_type.size == .C,
1894 else => switch (mod.intern_pool.indexToKey(ty.ip_index)) {1879 else => false,
1895 .ptr_type => |ptr_type| ptr_type.size == .C,
1896 else => false,
1897 },
1898 };1880 };
1899 }1881 }
19001882
1901 pub fn isPtrAtRuntime(ty: Type, mod: *const Module) bool {1883 pub fn isPtrAtRuntime(ty: Type, mod: *const Module) bool {
1902 return switch (ty.ip_index) {1884 return switch (mod.intern_pool.indexToKey(ty.toIntern())) {
1903 .none => false,1885 .ptr_type => |ptr_type| switch (ptr_type.size) {
1904 else => switch (mod.intern_pool.indexToKey(ty.ip_index)) {1886 .Slice => false,
1905 .ptr_type => |ptr_type| switch (ptr_type.size) {1887 .One, .Many, .C => true,
1906 .Slice => false,1888 },
1907 .One, .Many, .C => true,1889 .opt_type => |child| switch (mod.intern_pool.indexToKey(child)) {
1908 },1890 .ptr_type => |p| switch (p.size) {
1909 .opt_type => |child| switch (mod.intern_pool.indexToKey(child)) {1891 .Slice, .C => false,
1910 .ptr_type => |p| switch (p.size) {1892 .Many, .One => !p.is_allowzero,
1911 .Slice, .C => false,
1912 .Many, .One => !p.is_allowzero,
1913 },
1914 else => false,
1915 },1893 },
1916 else => false,1894 else => false,
1917 },1895 },
1896 else => false,
1918 };1897 };
1919 }1898 }
19201899
...@@ -1929,22 +1908,19 @@ pub const Type = struct {...@@ -1929,22 +1908,19 @@ pub const Type = struct {
19291908
1930 /// See also `isPtrLikeOptional`.1909 /// See also `isPtrLikeOptional`.
1931 pub fn optionalReprIsPayload(ty: Type, mod: *const Module) bool {1910 pub fn optionalReprIsPayload(ty: Type, mod: *const Module) bool {
1932 return switch (ty.ip_index) {1911 return switch (mod.intern_pool.indexToKey(ty.toIntern())) {
1933 .none => false,1912 .opt_type => |child| switch (child.toType().zigTypeTag(mod)) {
1934 else => switch (mod.intern_pool.indexToKey(ty.ip_index)) {1913 .Pointer => {
1935 .opt_type => |child| switch (child.toType().zigTypeTag(mod)) {1914 const info = child.toType().ptrInfo(mod);
1936 .Pointer => {1915 return switch (info.size) {
1937 const info = child.toType().ptrInfo(mod);1916 .C => false,
1938 return switch (info.size) {1917 else => !info.@"allowzero",
1939 .C => false,1918 };
1940 else => !info.@"allowzero",
1941 };
1942 },
1943 .ErrorSet => true,
1944 else => false,
1945 },1919 },
1920 .ErrorSet => true,
1946 else => false,1921 else => false,
1947 },1922 },
1923 else => false,
1948 };1924 };
1949 }1925 }
19501926
...@@ -1952,19 +1928,16 @@ pub const Type = struct {...@@ -1952,19 +1928,16 @@ pub const Type = struct {
1952 /// address value, using 0 for null. Note that this returns true for C pointers.1928 /// address value, using 0 for null. Note that this returns true for C pointers.
1953 /// This function must be kept in sync with `Sema.typePtrOrOptionalPtrTy`.1929 /// This function must be kept in sync with `Sema.typePtrOrOptionalPtrTy`.
1954 pub fn isPtrLikeOptional(ty: Type, mod: *const Module) bool {1930 pub fn isPtrLikeOptional(ty: Type, mod: *const Module) bool {
1955 return switch (ty.ip_index) {1931 return switch (mod.intern_pool.indexToKey(ty.toIntern())) {
1956 .none => false,1932 .ptr_type => |ptr_type| ptr_type.size == .C,
1957 else => switch (mod.intern_pool.indexToKey(ty.ip_index)) {1933 .opt_type => |child| switch (mod.intern_pool.indexToKey(child)) {
1958 .ptr_type => |ptr_type| ptr_type.size == .C,1934 .ptr_type => |ptr_type| switch (ptr_type.size) {
1959 .opt_type => |child| switch (mod.intern_pool.indexToKey(child)) {1935 .Slice, .C => false,
1960 .ptr_type => |ptr_type| switch (ptr_type.size) {1936 .Many, .One => !ptr_type.is_allowzero,
1961 .Slice, .C => false,
1962 .Many, .One => !ptr_type.is_allowzero,
1963 },
1964 else => false,
1965 },1937 },
1966 else => false,1938 else => false,
1967 },1939 },
1940 else => false,
1968 };1941 };
1969 }1942 }
19701943
...@@ -1976,7 +1949,7 @@ pub const Type = struct {...@@ -1976,7 +1949,7 @@ pub const Type = struct {
1976 }1949 }
19771950
1978 pub fn childTypeIp(ty: Type, ip: InternPool) Type {1951 pub fn childTypeIp(ty: Type, ip: InternPool) Type {
1979 return ip.childType(ty.ip_index).toType();1952 return ip.childType(ty.toIntern()).toType();
1980 }1953 }
19811954
1982 /// For *[N]T, returns T.1955 /// For *[N]T, returns T.
...@@ -1989,7 +1962,7 @@ pub const Type = struct {...@@ -1989,7 +1962,7 @@ pub const Type = struct {
1989 /// For []T, returns T.1962 /// For []T, returns T.
1990 /// For anyframe->T, returns T.1963 /// For anyframe->T, returns T.
1991 pub fn elemType2(ty: Type, mod: *const Module) Type {1964 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())) {
1993 .ptr_type => |ptr_type| switch (ptr_type.size) {1966 .ptr_type => |ptr_type| switch (ptr_type.size) {
1994 .One => ptr_type.elem_type.toType().shallowElemType(mod),1967 .One => ptr_type.elem_type.toType().shallowElemType(mod),
1995 .Many, .C, .Slice => ptr_type.elem_type.toType(),1968 .Many, .C, .Slice => ptr_type.elem_type.toType(),
...@@ -2023,7 +1996,7 @@ pub const Type = struct {...@@ -2023,7 +1996,7 @@ pub const Type = struct {
2023 /// Asserts that the type is an optional.1996 /// Asserts that the type is an optional.
2024 /// Note that for C pointers this returns the type unmodified.1997 /// Note that for C pointers this returns the type unmodified.
2025 pub fn optionalChild(ty: Type, mod: *const Module) Type {1998 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())) {
2027 .opt_type => |child| child.toType(),2000 .opt_type => |child| child.toType(),
2028 .ptr_type => |ptr_type| b: {2001 .ptr_type => |ptr_type| b: {
2029 assert(ptr_type.size == .C);2002 assert(ptr_type.size == .C);
...@@ -2036,7 +2009,7 @@ pub const Type = struct {...@@ -2036,7 +2009,7 @@ pub const Type = struct {
2036 /// Returns the tag type of a union, if the type is a union and it has a tag type.2009 /// Returns the tag type of a union, if the type is a union and it has a tag type.
2037 /// Otherwise, returns `null`.2010 /// Otherwise, returns `null`.
2038 pub fn unionTagType(ty: Type, mod: *Module) ?Type {2011 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())) {
2040 .union_type => |union_type| switch (union_type.runtime_tag) {2013 .union_type => |union_type| switch (union_type.runtime_tag) {
2041 .tagged => {2014 .tagged => {
2042 const union_obj = mod.unionPtr(union_type.index);2015 const union_obj = mod.unionPtr(union_type.index);
...@@ -2052,7 +2025,7 @@ pub const Type = struct {...@@ -2052,7 +2025,7 @@ pub const Type = struct {
2052 /// Same as `unionTagType` but includes safety tag.2025 /// Same as `unionTagType` but includes safety tag.
2053 /// Codegen should use this version.2026 /// Codegen should use this version.
2054 pub fn unionTagTypeSafety(ty: Type, mod: *Module) ?Type {2027 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())) {
2056 .union_type => |union_type| {2029 .union_type => |union_type| {
2057 if (!union_type.hasTag()) return null;2030 if (!union_type.hasTag()) return null;
2058 const union_obj = mod.unionPtr(union_type.index);2031 const union_obj = mod.unionPtr(union_type.index);
...@@ -2097,13 +2070,13 @@ pub const Type = struct {...@@ -2097,13 +2070,13 @@ pub const Type = struct {
2097 }2070 }
20982071
2099 pub fn unionGetLayout(ty: Type, mod: *Module) Module.Union.Layout {2072 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;
2101 const union_obj = mod.unionPtr(union_type.index);2074 const union_obj = mod.unionPtr(union_type.index);
2102 return union_obj.getLayout(mod, union_type.hasTag());2075 return union_obj.getLayout(mod, union_type.hasTag());
2103 }2076 }
21042077
2105 pub fn containerLayout(ty: Type, mod: *Module) std.builtin.Type.ContainerLayout {2078 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())) {
2107 .struct_type => |struct_type| {2080 .struct_type => |struct_type| {
2108 const struct_obj = mod.structPtrUnwrap(struct_type.index) orelse return .Auto;2081 const struct_obj = mod.structPtrUnwrap(struct_type.index) orelse return .Auto;
2109 return struct_obj.layout;2082 return struct_obj.layout;
...@@ -2119,19 +2092,19 @@ pub const Type = struct {...@@ -2119,19 +2092,19 @@ pub const Type = struct {
21192092
2120 /// Asserts that the type is an error union.2093 /// Asserts that the type is an error union.
2121 pub fn errorUnionPayload(ty: Type, mod: *Module) Type {2094 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();
2123 }2096 }
21242097
2125 /// Asserts that the type is an error union.2098 /// Asserts that the type is an error union.
2126 pub fn errorUnionSet(ty: Type, mod: *Module) Type {2099 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();
2128 }2101 }
21292102
2130 /// Returns false for unresolved inferred error sets.2103 /// Returns false for unresolved inferred error sets.
2131 pub fn errorSetIsEmpty(ty: Type, mod: *Module) bool {2104 pub fn errorSetIsEmpty(ty: Type, mod: *Module) bool {
2132 return switch (ty.ip_index) {2105 return switch (ty.toIntern()) {
2133 .anyerror_type => false,2106 .anyerror_type => false,
2134 else => switch (mod.intern_pool.indexToKey(ty.ip_index)) {2107 else => switch (mod.intern_pool.indexToKey(ty.toIntern())) {
2135 .error_set_type => |error_set_type| error_set_type.names.len == 0,2108 .error_set_type => |error_set_type| error_set_type.names.len == 0,
2136 .inferred_error_set_type => |index| {2109 .inferred_error_set_type => |index| {
2137 const inferred_error_set = mod.inferredErrorSetPtr(index);2110 const inferred_error_set = mod.inferredErrorSetPtr(index);
...@@ -2149,9 +2122,9 @@ pub const Type = struct {...@@ -2149,9 +2122,9 @@ pub const Type = struct {
2149 /// Note that the result may be a false negative if the type did not get error set2122 /// Note that the result may be a false negative if the type did not get error set
2150 /// resolution prior to this call.2123 /// resolution prior to this call.
2151 pub fn isAnyError(ty: Type, mod: *Module) bool {2124 pub fn isAnyError(ty: Type, mod: *Module) bool {
2152 return switch (ty.ip_index) {2125 return switch (ty.toIntern()) {
2153 .anyerror_type => true,2126 .anyerror_type => true,
2154 else => switch (mod.intern_pool.indexToKey(ty.ip_index)) {2127 else => switch (mod.intern_pool.indexToKey(ty.toIntern())) {
2155 .inferred_error_set_type => |i| mod.inferredErrorSetPtr(i).is_anyerror,2128 .inferred_error_set_type => |i| mod.inferredErrorSetPtr(i).is_anyerror,
2156 else => false,2129 else => false,
2157 },2130 },
...@@ -2194,9 +2167,9 @@ pub const Type = struct {...@@ -2194,9 +2167,9 @@ pub const Type = struct {
2194 /// resolved yet.2167 /// resolved yet.
2195 pub fn errorSetHasField(ty: Type, name: []const u8, mod: *Module) bool {2168 pub fn errorSetHasField(ty: Type, name: []const u8, mod: *Module) bool {
2196 const ip = &mod.intern_pool;2169 const ip = &mod.intern_pool;
2197 return switch (ty.ip_index) {2170 return switch (ty.toIntern()) {
2198 .anyerror_type => true,2171 .anyerror_type => true,
2199 else => switch (ip.indexToKey(ty.ip_index)) {2172 else => switch (ip.indexToKey(ty.toIntern())) {
2200 .error_set_type => |error_set_type| {2173 .error_set_type => |error_set_type| {
2201 // If the string is not interned, then the field certainly is not present.2174 // If the string is not interned, then the field certainly is not present.
2202 const field_name_interned = ip.getString(name).unwrap() orelse return false;2175 const field_name_interned = ip.getString(name).unwrap() orelse return false;
...@@ -2220,7 +2193,7 @@ pub const Type = struct {...@@ -2220,7 +2193,7 @@ pub const Type = struct {
2220 }2193 }
22212194
2222 pub fn arrayLenIp(ty: Type, ip: InternPool) u64 {2195 pub fn arrayLenIp(ty: Type, ip: InternPool) u64 {
2223 return switch (ip.indexToKey(ty.ip_index)) {2196 return switch (ip.indexToKey(ty.toIntern())) {
2224 .vector_type => |vector_type| vector_type.len,2197 .vector_type => |vector_type| vector_type.len,
2225 .array_type => |array_type| array_type.len,2198 .array_type => |array_type| array_type.len,
2226 .struct_type => |struct_type| {2199 .struct_type => |struct_type| {
...@@ -2238,7 +2211,7 @@ pub const Type = struct {...@@ -2238,7 +2211,7 @@ pub const Type = struct {
2238 }2211 }
22392212
2240 pub fn vectorLen(ty: Type, mod: *const Module) u32 {2213 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())) {
2242 .vector_type => |vector_type| vector_type.len,2215 .vector_type => |vector_type| vector_type.len,
2243 .anon_struct_type => |tuple| @intCast(u32, tuple.types.len),2216 .anon_struct_type => |tuple| @intCast(u32, tuple.types.len),
2244 else => unreachable,2217 else => unreachable,
...@@ -2247,7 +2220,7 @@ pub const Type = struct {...@@ -2247,7 +2220,7 @@ pub const Type = struct {
22472220
2248 /// Asserts the type is an array, pointer or vector.2221 /// Asserts the type is an array, pointer or vector.
2249 pub fn sentinel(ty: Type, mod: *const Module) ?Value {2222 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())) {
2251 .vector_type,2224 .vector_type,
2252 .struct_type,2225 .struct_type,
2253 .anon_struct_type,2226 .anon_struct_type,
...@@ -2267,10 +2240,9 @@ pub const Type = struct {...@@ -2267,10 +2240,9 @@ pub const Type = struct {
22672240
2268 /// Returns true if and only if the type is a fixed-width, signed integer.2241 /// Returns true if and only if the type is a fixed-width, signed integer.
2269 pub fn isSignedInt(ty: Type, mod: *const Module) bool {2242 pub fn isSignedInt(ty: Type, mod: *const Module) bool {
2270 return switch (ty.ip_index) {2243 return switch (ty.toIntern()) {
2271 .c_char_type, .isize_type, .c_short_type, .c_int_type, .c_long_type, .c_longlong_type => true,2244 .c_char_type, .isize_type, .c_short_type, .c_int_type, .c_long_type, .c_longlong_type => true,
2272 .none => false,2245 else => switch (mod.intern_pool.indexToKey(ty.toIntern())) {
2273 else => switch (mod.intern_pool.indexToKey(ty.ip_index)) {
2274 .int_type => |int_type| int_type.signedness == .signed,2246 .int_type => |int_type| int_type.signedness == .signed,
2275 else => false,2247 else => false,
2276 },2248 },
...@@ -2279,10 +2251,9 @@ pub const Type = struct {...@@ -2279,10 +2251,9 @@ pub const Type = struct {
22792251
2280 /// Returns true if and only if the type is a fixed-width, unsigned integer.2252 /// Returns true if and only if the type is a fixed-width, unsigned integer.
2281 pub fn isUnsignedInt(ty: Type, mod: *const Module) bool {2253 pub fn isUnsignedInt(ty: Type, mod: *const Module) bool {
2282 return switch (ty.ip_index) {2254 return switch (ty.toIntern()) {
2283 .usize_type, .c_ushort_type, .c_uint_type, .c_ulong_type, .c_ulonglong_type => true,2255 .usize_type, .c_ushort_type, .c_uint_type, .c_ulong_type, .c_ulonglong_type => true,
2284 .none => false,2256 else => switch (mod.intern_pool.indexToKey(ty.toIntern())) {
2285 else => switch (mod.intern_pool.indexToKey(ty.ip_index)) {
2286 .int_type => |int_type| int_type.signedness == .unsigned,2257 .int_type => |int_type| int_type.signedness == .unsigned,
2287 else => false,2258 else => false,
2288 },2259 },
...@@ -2304,7 +2275,7 @@ pub const Type = struct {...@@ -2304,7 +2275,7 @@ pub const Type = struct {
2304 const target = mod.getTarget();2275 const target = mod.getTarget();
2305 var ty = starting_ty;2276 var ty = starting_ty;
23062277
2307 while (true) switch (ty.ip_index) {2278 while (true) switch (ty.toIntern()) {
2308 .anyerror_type => {2279 .anyerror_type => {
2309 // TODO revisit this when error sets support custom int types2280 // TODO revisit this when error sets support custom int types
2310 return .{ .signedness = .unsigned, .bits = 16 };2281 return .{ .signedness = .unsigned, .bits = 16 };
...@@ -2320,7 +2291,7 @@ pub const Type = struct {...@@ -2320,7 +2291,7 @@ pub const Type = struct {
2320 .c_ulong_type => return .{ .signedness = .unsigned, .bits = target.c_type_bit_size(.ulong) },2291 .c_ulong_type => return .{ .signedness = .unsigned, .bits = target.c_type_bit_size(.ulong) },
2321 .c_longlong_type => return .{ .signedness = .signed, .bits = target.c_type_bit_size(.longlong) },2292 .c_longlong_type => return .{ .signedness = .signed, .bits = target.c_type_bit_size(.longlong) },
2322 .c_ulonglong_type => return .{ .signedness = .unsigned, .bits = target.c_type_bit_size(.ulonglong) },2293 .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())) {
2324 .int_type => |int_type| return int_type,2295 .int_type => |int_type| return int_type,
2325 .struct_type => |struct_type| {2296 .struct_type => |struct_type| {
2326 const struct_obj = mod.structPtrUnwrap(struct_type.index).?;2297 const struct_obj = mod.structPtrUnwrap(struct_type.index).?;
...@@ -2370,7 +2341,7 @@ pub const Type = struct {...@@ -2370,7 +2341,7 @@ pub const Type = struct {
2370 }2341 }
23712342
2372 pub fn isNamedInt(ty: Type) bool {2343 pub fn isNamedInt(ty: Type) bool {
2373 return switch (ty.ip_index) {2344 return switch (ty.toIntern()) {
2374 .usize_type,2345 .usize_type,
2375 .isize_type,2346 .isize_type,
2376 .c_char_type,2347 .c_char_type,
...@@ -2390,7 +2361,7 @@ pub const Type = struct {...@@ -2390,7 +2361,7 @@ pub const Type = struct {
23902361
2391 /// Returns `false` for `comptime_float`.2362 /// Returns `false` for `comptime_float`.
2392 pub fn isRuntimeFloat(ty: Type) bool {2363 pub fn isRuntimeFloat(ty: Type) bool {
2393 return switch (ty.ip_index) {2364 return switch (ty.toIntern()) {
2394 .f16_type,2365 .f16_type,
2395 .f32_type,2366 .f32_type,
2396 .f64_type,2367 .f64_type,
...@@ -2405,7 +2376,7 @@ pub const Type = struct {...@@ -2405,7 +2376,7 @@ pub const Type = struct {
24052376
2406 /// Returns `true` for `comptime_float`.2377 /// Returns `true` for `comptime_float`.
2407 pub fn isAnyFloat(ty: Type) bool {2378 pub fn isAnyFloat(ty: Type) bool {
2408 return switch (ty.ip_index) {2379 return switch (ty.toIntern()) {
2409 .f16_type,2380 .f16_type,
2410 .f32_type,2381 .f32_type,
2411 .f64_type,2382 .f64_type,
...@@ -2422,7 +2393,7 @@ pub const Type = struct {...@@ -2422,7 +2393,7 @@ pub const Type = struct {
2422 /// Asserts the type is a fixed-size float or comptime_float.2393 /// Asserts the type is a fixed-size float or comptime_float.
2423 /// Returns 128 for comptime_float types.2394 /// Returns 128 for comptime_float types.
2424 pub fn floatBits(ty: Type, target: Target) u16 {2395 pub fn floatBits(ty: Type, target: Target) u16 {
2425 return switch (ty.ip_index) {2396 return switch (ty.toIntern()) {
2426 .f16_type => 16,2397 .f16_type => 16,
2427 .f32_type => 32,2398 .f32_type => 32,
2428 .f64_type => 64,2399 .f64_type => 64,
...@@ -2440,7 +2411,7 @@ pub const Type = struct {...@@ -2440,7 +2411,7 @@ pub const Type = struct {
2440 }2411 }
24412412
2442 pub fn fnReturnTypeIp(ty: Type, ip: InternPool) Type {2413 pub fn fnReturnTypeIp(ty: Type, ip: InternPool) Type {
2443 return switch (ip.indexToKey(ty.ip_index)) {2414 return switch (ip.indexToKey(ty.toIntern())) {
2444 .ptr_type => |ptr_type| ip.indexToKey(ptr_type.elem_type).func_type.return_type,2415 .ptr_type => |ptr_type| ip.indexToKey(ptr_type.elem_type).func_type.return_type,
2445 .func_type => |func_type| func_type.return_type,2416 .func_type => |func_type| func_type.return_type,
2446 else => unreachable,2417 else => unreachable,
...@@ -2449,7 +2420,7 @@ pub const Type = struct {...@@ -2449,7 +2420,7 @@ pub const Type = struct {
24492420
2450 /// Asserts the type is a function.2421 /// Asserts the type is a function.
2451 pub fn fnCallingConvention(ty: Type, mod: *Module) std.builtin.CallingConvention {2422 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;
2453 }2424 }
24542425
2455 pub fn isValidParamType(self: Type, mod: *const Module) bool {2426 pub fn isValidParamType(self: Type, mod: *const Module) bool {
...@@ -2468,11 +2439,11 @@ pub const Type = struct {...@@ -2468,11 +2439,11 @@ pub const Type = struct {
24682439
2469 /// Asserts the type is a function.2440 /// Asserts the type is a function.
2470 pub fn fnIsVarArgs(ty: Type, mod: *Module) bool {2441 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;
2472 }2443 }
24732444
2474 pub fn isNumeric(ty: Type, mod: *const Module) bool {2445 pub fn isNumeric(ty: Type, mod: *const Module) bool {
2475 return switch (ty.ip_index) {2446 return switch (ty.toIntern()) {
2476 .f16_type,2447 .f16_type,
2477 .f32_type,2448 .f32_type,
2478 .f64_type,2449 .f64_type,
...@@ -2494,9 +2465,7 @@ pub const Type = struct {...@@ -2494,9 +2465,7 @@ pub const Type = struct {
2494 .c_ulonglong_type,2465 .c_ulonglong_type,
2495 => true,2466 => true,
24962467
2497 .none => false,2468 else => switch (mod.intern_pool.indexToKey(ty.toIntern())) {
2498
2499 else => switch (mod.intern_pool.indexToKey(ty.ip_index)) {
2500 .int_type => true,2469 .int_type => true,
2501 else => false,2470 else => false,
2502 },2471 },
...@@ -2508,10 +2477,10 @@ pub const Type = struct {...@@ -2508,10 +2477,10 @@ pub const Type = struct {
2508 pub fn onePossibleValue(starting_type: Type, mod: *Module) !?Value {2477 pub fn onePossibleValue(starting_type: Type, mod: *Module) !?Value {
2509 var ty = starting_type;2478 var ty = starting_type;
25102479
2511 while (true) switch (ty.ip_index) {2480 while (true) switch (ty.toIntern()) {
2512 .empty_struct_type => return Value.empty_struct,2481 .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())) {
2515 .int_type => |int_type| {2484 .int_type => |int_type| {
2516 if (int_type.bits == 0) {2485 if (int_type.bits == 0) {
2517 return try mod.intValue(ty, 0);2486 return try mod.intValue(ty, 0);
...@@ -2530,13 +2499,13 @@ pub const Type = struct {...@@ -2530,13 +2499,13 @@ pub const Type = struct {
25302499
2531 inline .array_type, .vector_type => |seq_type| {2500 inline .array_type, .vector_type => |seq_type| {
2532 if (seq_type.len == 0) return (try mod.intern(.{ .aggregate = .{2501 if (seq_type.len == 0) return (try mod.intern(.{ .aggregate = .{
2533 .ty = ty.ip_index,2502 .ty = ty.toIntern(),
2534 .storage = .{ .elems = &.{} },2503 .storage = .{ .elems = &.{} },
2535 } })).toValue();2504 } })).toValue();
2536 if (try seq_type.child.toType().onePossibleValue(mod)) |opv| {2505 if (try seq_type.child.toType().onePossibleValue(mod)) |opv| {
2537 return (try mod.intern(.{ .aggregate = .{2506 return (try mod.intern(.{ .aggregate = .{
2538 .ty = ty.ip_index,2507 .ty = ty.toIntern(),
2539 .storage = .{ .repeated_elem = opv.ip_index },2508 .storage = .{ .repeated_elem = opv.toIntern() },
2540 } })).toValue();2509 } })).toValue();
2541 }2510 }
2542 return null;2511 return null;
...@@ -2612,7 +2581,7 @@ pub const Type = struct {...@@ -2612,7 +2581,7 @@ pub const Type = struct {
2612 // This TODO is repeated in the redundant implementation of2581 // This TODO is repeated in the redundant implementation of
2613 // one-possible-value logic in Sema.zig.2582 // one-possible-value logic in Sema.zig.
2614 const empty = try mod.intern(.{ .aggregate = .{2583 const empty = try mod.intern(.{ .aggregate = .{
2615 .ty = ty.ip_index,2584 .ty = ty.toIntern(),
2616 .storage = .{ .elems = &.{} },2585 .storage = .{ .elems = &.{} },
2617 } });2586 } });
2618 return empty.toValue();2587 return empty.toValue();
...@@ -2625,7 +2594,7 @@ pub const Type = struct {...@@ -2625,7 +2594,7 @@ pub const Type = struct {
2625 // In this case the struct has all comptime-known fields and2594 // In this case the struct has all comptime-known fields and
2626 // therefore has one possible value.2595 // therefore has one possible value.
2627 return (try mod.intern(.{ .aggregate = .{2596 return (try mod.intern(.{ .aggregate = .{
2628 .ty = ty.ip_index,2597 .ty = ty.toIntern(),
2629 .storage = .{ .elems = tuple.values },2598 .storage = .{ .elems = tuple.values },
2630 } })).toValue();2599 } })).toValue();
2631 },2600 },
...@@ -2637,9 +2606,9 @@ pub const Type = struct {...@@ -2637,9 +2606,9 @@ pub const Type = struct {
2637 const only_field = union_obj.fields.values()[0];2606 const only_field = union_obj.fields.values()[0];
2638 const val_val = (try only_field.ty.onePossibleValue(mod)) orelse return null;2607 const val_val = (try only_field.ty.onePossibleValue(mod)) orelse return null;
2639 const only = try mod.intern(.{ .un = .{2608 const only = try mod.intern(.{ .un = .{
2640 .ty = ty.ip_index,2609 .ty = ty.toIntern(),
2641 .tag = tag_val.ip_index,2610 .tag = tag_val.toIntern(),
2642 .val = val_val.ip_index,2611 .val = val_val.toIntern(),
2643 } });2612 } });
2644 return only.toValue();2613 return only.toValue();
2645 },2614 },
...@@ -2650,8 +2619,8 @@ pub const Type = struct {...@@ -2650,8 +2619,8 @@ pub const Type = struct {
26502619
2651 if (try enum_type.tag_ty.toType().onePossibleValue(mod)) |int_opv| {2620 if (try enum_type.tag_ty.toType().onePossibleValue(mod)) |int_opv| {
2652 const only = try mod.intern(.{ .enum_tag = .{2621 const only = try mod.intern(.{ .enum_tag = .{
2653 .ty = ty.ip_index,2622 .ty = ty.toIntern(),
2654 .int = int_opv.ip_index,2623 .int = int_opv.toIntern(),
2655 } });2624 } });
2656 return only.toValue();2625 return only.toValue();
2657 }2626 }
...@@ -2663,7 +2632,7 @@ pub const Type = struct {...@@ -2663,7 +2632,7 @@ pub const Type = struct {
2663 1 => {2632 1 => {
2664 if (enum_type.values.len == 0) {2633 if (enum_type.values.len == 0) {
2665 const only = try mod.intern(.{ .enum_tag = .{2634 const only = try mod.intern(.{ .enum_tag = .{
2666 .ty = ty.ip_index,2635 .ty = ty.toIntern(),
2667 .int = try mod.intern(.{ .int = .{2636 .int = try mod.intern(.{ .int = .{
2668 .ty = enum_type.tag_ty,2637 .ty = enum_type.tag_ty,
2669 .storage = .{ .u64 = 0 },2638 .storage = .{ .u64 = 0 },
...@@ -2705,10 +2674,10 @@ pub const Type = struct {...@@ -2705,10 +2674,10 @@ pub const Type = struct {
2705 /// TODO merge these implementations together with the "advanced" pattern seen2674 /// TODO merge these implementations together with the "advanced" pattern seen
2706 /// elsewhere in this file.2675 /// elsewhere in this file.
2707 pub fn comptimeOnly(ty: Type, mod: *Module) bool {2676 pub fn comptimeOnly(ty: Type, mod: *Module) bool {
2708 return switch (ty.ip_index) {2677 return switch (ty.toIntern()) {
2709 .empty_struct_type => false,2678 .empty_struct_type => false,
27102679
2711 else => switch (mod.intern_pool.indexToKey(ty.ip_index)) {2680 else => switch (mod.intern_pool.indexToKey(ty.toIntern())) {
2712 .int_type => false,2681 .int_type => false,
2713 .ptr_type => |ptr_type| {2682 .ptr_type => |ptr_type| {
2714 const child_ty = ptr_type.elem_type.toType();2683 const child_ty = ptr_type.elem_type.toType();
...@@ -2880,8 +2849,7 @@ pub const Type = struct {...@@ -2880,8 +2849,7 @@ pub const Type = struct {
28802849
2881 /// Returns null if the type has no namespace.2850 /// Returns null if the type has no namespace.
2882 pub fn getNamespaceIndex(ty: Type, mod: *Module) Module.Namespace.OptionalIndex {2851 pub fn getNamespaceIndex(ty: Type, mod: *Module) Module.Namespace.OptionalIndex {
2883 if (ty.ip_index == .none) return .none;2852 return switch (mod.intern_pool.indexToKey(ty.toIntern())) {
2884 return switch (mod.intern_pool.indexToKey(ty.ip_index)) {
2885 .opaque_type => |opaque_type| opaque_type.namespace.toOptional(),2853 .opaque_type => |opaque_type| opaque_type.namespace.toOptional(),
2886 .struct_type => |struct_type| struct_type.namespace,2854 .struct_type => |struct_type| struct_type.namespace,
2887 .union_type => |union_type| mod.unionPtr(union_type.index).namespace.toOptional(),2855 .union_type => |union_type| mod.unionPtr(union_type.index).namespace.toOptional(),
...@@ -2900,8 +2868,8 @@ pub const Type = struct {...@@ -2900,8 +2868,8 @@ pub const Type = struct {
2900 pub fn minInt(ty: Type, mod: *Module) !Value {2868 pub fn minInt(ty: Type, mod: *Module) !Value {
2901 const scalar = try minIntScalar(ty.scalarType(mod), mod);2869 const scalar = try minIntScalar(ty.scalarType(mod), mod);
2902 return if (ty.zigTypeTag(mod) == .Vector) (try mod.intern(.{ .aggregate = .{2870 return if (ty.zigTypeTag(mod) == .Vector) (try mod.intern(.{ .aggregate = .{
2903 .ty = ty.ip_index,2871 .ty = ty.toIntern(),
2904 .storage = .{ .repeated_elem = scalar.ip_index },2872 .storage = .{ .repeated_elem = scalar.toIntern() },
2905 } })).toValue() else scalar;2873 } })).toValue() else scalar;
2906 }2874 }
29072875
...@@ -2929,8 +2897,8 @@ pub const Type = struct {...@@ -2929,8 +2897,8 @@ pub const Type = struct {
2929 pub fn maxInt(ty: Type, mod: *Module, dest_ty: Type) !Value {2897 pub fn maxInt(ty: Type, mod: *Module, dest_ty: Type) !Value {
2930 const scalar = try maxIntScalar(ty.scalarType(mod), mod, dest_ty);2898 const scalar = try maxIntScalar(ty.scalarType(mod), mod, dest_ty);
2931 return if (ty.zigTypeTag(mod) == .Vector) (try mod.intern(.{ .aggregate = .{2899 return if (ty.zigTypeTag(mod) == .Vector) (try mod.intern(.{ .aggregate = .{
2932 .ty = ty.ip_index,2900 .ty = ty.toIntern(),
2933 .storage = .{ .repeated_elem = scalar.ip_index },2901 .storage = .{ .repeated_elem = scalar.toIntern() },
2934 } })).toValue() else scalar;2902 } })).toValue() else scalar;
2935 }2903 }
29362904
...@@ -2971,7 +2939,7 @@ pub const Type = struct {...@@ -2971,7 +2939,7 @@ pub const Type = struct {
29712939
2972 /// Asserts the type is an enum or a union.2940 /// Asserts the type is an enum or a union.
2973 pub fn intTagType(ty: Type, mod: *Module) !Type {2941 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())) {
2975 .union_type => |union_type| mod.unionPtr(union_type.index).tag_ty.intTagType(mod),2943 .union_type => |union_type| mod.unionPtr(union_type.index).tag_ty.intTagType(mod),
2976 .enum_type => |enum_type| enum_type.tag_ty.toType(),2944 .enum_type => |enum_type| enum_type.tag_ty.toType(),
2977 else => unreachable,2945 else => unreachable,
...@@ -2979,21 +2947,18 @@ pub const Type = struct {...@@ -2979,21 +2947,18 @@ pub const Type = struct {
2979 }2947 }
29802948
2981 pub fn isNonexhaustiveEnum(ty: Type, mod: *Module) bool {2949 pub fn isNonexhaustiveEnum(ty: Type, mod: *Module) bool {
2982 return switch (ty.ip_index) {2950 return switch (mod.intern_pool.indexToKey(ty.toIntern())) {
2983 .none => false,2951 .enum_type => |enum_type| switch (enum_type.tag_mode) {
2984 else => switch (mod.intern_pool.indexToKey(ty.ip_index)) {2952 .nonexhaustive => true,
2985 .enum_type => |enum_type| switch (enum_type.tag_mode) {2953 .auto, .explicit => false,
2986 .nonexhaustive => true,
2987 .auto, .explicit => false,
2988 },
2989 else => false,
2990 },2954 },
2955 else => false,
2991 };2956 };
2992 }2957 }
29932958
2994 // Asserts that `ty` is an error set and not `anyerror`.2959 // Asserts that `ty` is an error set and not `anyerror`.
2995 pub fn errorSetNames(ty: Type, mod: *Module) []const InternPool.NullTerminatedString {2960 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())) {
2997 .error_set_type => |x| x.names,2962 .error_set_type => |x| x.names,
2998 .inferred_error_set_type => |index| {2963 .inferred_error_set_type => |index| {
2999 const inferred_error_set = mod.inferredErrorSetPtr(index);2964 const inferred_error_set = mod.inferredErrorSetPtr(index);
...@@ -3006,22 +2971,22 @@ pub const Type = struct {...@@ -3006,22 +2971,22 @@ pub const Type = struct {
3006 }2971 }
30072972
3008 pub fn enumFields(ty: Type, mod: *Module) []const InternPool.NullTerminatedString {2973 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;
3010 }2975 }
30112976
3012 pub fn enumFieldCount(ty: Type, mod: *Module) usize {2977 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;
3014 }2979 }
30152980
3016 pub fn enumFieldName(ty: Type, field_index: usize, mod: *Module) [:0]const u8 {2981 pub fn enumFieldName(ty: Type, field_index: usize, mod: *Module) [:0]const u8 {
3017 const ip = &mod.intern_pool;2982 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];
3019 return ip.stringToSlice(field_name);2984 return ip.stringToSlice(field_name);
3020 }2985 }
30212986
3022 pub fn enumFieldIndex(ty: Type, field_name: []const u8, mod: *Module) ?u32 {2987 pub fn enumFieldIndex(ty: Type, field_name: []const u8, mod: *Module) ?u32 {
3023 const ip = &mod.intern_pool;2988 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;
3025 // If the string is not interned, then the field certainly is not present.2990 // If the string is not interned, then the field certainly is not present.
3026 const field_name_interned = ip.getString(field_name).unwrap() orelse return null;2991 const field_name_interned = ip.getString(field_name).unwrap() orelse return null;
3027 return enum_type.nameIndex(ip, field_name_interned);2992 return enum_type.nameIndex(ip, field_name_interned);
...@@ -3032,9 +2997,9 @@ pub const Type = struct {...@@ -3032,9 +2997,9 @@ pub const Type = struct {
3032 /// declaration order, or `null` if `enum_tag` does not match any field.2997 /// declaration order, or `null` if `enum_tag` does not match any field.
3033 pub fn enumTagFieldIndex(ty: Type, enum_tag: Value, mod: *Module) ?u32 {2998 pub fn enumTagFieldIndex(ty: Type, enum_tag: Value, mod: *Module) ?u32 {
3034 const ip = &mod.intern_pool;2999 const ip = &mod.intern_pool;
3035 const enum_type = ip.indexToKey(ty.ip_index).enum_type;3000 const enum_type = ip.indexToKey(ty.toIntern()).enum_type;
3036 const int_tag = switch (ip.indexToKey(enum_tag.ip_index)) {3001 const int_tag = switch (ip.indexToKey(enum_tag.toIntern())) {
3037 .int => enum_tag.ip_index,3002 .int => enum_tag.toIntern(),
3038 .enum_tag => |info| info.int,3003 .enum_tag => |info| info.int,
3039 else => unreachable,3004 else => unreachable,
3040 };3005 };
...@@ -3043,7 +3008,7 @@ pub const Type = struct {...@@ -3043,7 +3008,7 @@ pub const Type = struct {
3043 }3008 }
30443009
3045 pub fn structFields(ty: Type, mod: *Module) Module.Struct.Fields {3010 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())) {
3047 .struct_type => |struct_type| {3012 .struct_type => |struct_type| {
3048 const struct_obj = mod.structPtrUnwrap(struct_type.index) orelse return .{};3013 const struct_obj = mod.structPtrUnwrap(struct_type.index) orelse return .{};
3049 assert(struct_obj.haveFieldTypes());3014 assert(struct_obj.haveFieldTypes());
...@@ -3054,7 +3019,7 @@ pub const Type = struct {...@@ -3054,7 +3019,7 @@ pub const Type = struct {
3054 }3019 }
30553020
3056 pub fn structFieldName(ty: Type, field_index: usize, mod: *Module) []const u8 {3021 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())) {
3058 .struct_type => |struct_type| {3023 .struct_type => |struct_type| {
3059 const struct_obj = mod.structPtrUnwrap(struct_type.index).?;3024 const struct_obj = mod.structPtrUnwrap(struct_type.index).?;
3060 assert(struct_obj.haveFieldTypes());3025 assert(struct_obj.haveFieldTypes());
...@@ -3069,7 +3034,7 @@ pub const Type = struct {...@@ -3069,7 +3034,7 @@ pub const Type = struct {
3069 }3034 }
30703035
3071 pub fn structFieldCount(ty: Type, mod: *Module) usize {3036 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())) {
3073 .struct_type => |struct_type| {3038 .struct_type => |struct_type| {
3074 const struct_obj = mod.structPtrUnwrap(struct_type.index) orelse return 0;3039 const struct_obj = mod.structPtrUnwrap(struct_type.index) orelse return 0;
3075 assert(struct_obj.haveFieldTypes());3040 assert(struct_obj.haveFieldTypes());
...@@ -3082,7 +3047,7 @@ pub const Type = struct {...@@ -3082,7 +3047,7 @@ pub const Type = struct {
30823047
3083 /// Supports structs and unions.3048 /// Supports structs and unions.
3084 pub fn structFieldType(ty: Type, index: usize, mod: *Module) Type {3049 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())) {
3086 .struct_type => |struct_type| {3051 .struct_type => |struct_type| {
3087 const struct_obj = mod.structPtrUnwrap(struct_type.index).?;3052 const struct_obj = mod.structPtrUnwrap(struct_type.index).?;
3088 return struct_obj.fields.values()[index].ty;3053 return struct_obj.fields.values()[index].ty;
...@@ -3097,7 +3062,7 @@ pub const Type = struct {...@@ -3097,7 +3062,7 @@ pub const Type = struct {
3097 }3062 }
30983063
3099 pub fn structFieldAlign(ty: Type, index: usize, mod: *Module) u32 {3064 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())) {
3101 .struct_type => |struct_type| {3066 .struct_type => |struct_type| {
3102 const struct_obj = mod.structPtrUnwrap(struct_type.index).?;3067 const struct_obj = mod.structPtrUnwrap(struct_type.index).?;
3103 assert(struct_obj.layout != .Packed);3068 assert(struct_obj.layout != .Packed);
...@@ -3115,7 +3080,7 @@ pub const Type = struct {...@@ -3115,7 +3080,7 @@ pub const Type = struct {
3115 }3080 }
31163081
3117 pub fn structFieldDefaultValue(ty: Type, index: usize, mod: *Module) Value {3082 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())) {
3119 .struct_type => |struct_type| {3084 .struct_type => |struct_type| {
3120 const struct_obj = mod.structPtrUnwrap(struct_type.index).?;3085 const struct_obj = mod.structPtrUnwrap(struct_type.index).?;
3121 return struct_obj.fields.values()[index].default_val;3086 return struct_obj.fields.values()[index].default_val;
...@@ -3131,7 +3096,7 @@ pub const Type = struct {...@@ -3131,7 +3096,7 @@ pub const Type = struct {
3131 }3096 }
31323097
3133 pub fn structFieldValueComptime(ty: Type, mod: *Module, index: usize) !?Value {3098 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())) {
3135 .struct_type => |struct_type| {3100 .struct_type => |struct_type| {
3136 const struct_obj = mod.structPtrUnwrap(struct_type.index).?;3101 const struct_obj = mod.structPtrUnwrap(struct_type.index).?;
3137 const field = struct_obj.fields.values()[index];3102 const field = struct_obj.fields.values()[index];
...@@ -3154,7 +3119,7 @@ pub const Type = struct {...@@ -3154,7 +3119,7 @@ pub const Type = struct {
3154 }3119 }
31553120
3156 pub fn structFieldIsComptime(ty: Type, index: usize, mod: *Module) bool {3121 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())) {
3158 .struct_type => |struct_type| {3123 .struct_type => |struct_type| {
3159 const struct_obj = mod.structPtrUnwrap(struct_type.index).?;3124 const struct_obj = mod.structPtrUnwrap(struct_type.index).?;
3160 if (struct_obj.layout == .Packed) return false;3125 if (struct_obj.layout == .Packed) return false;
...@@ -3167,7 +3132,7 @@ pub const Type = struct {...@@ -3167,7 +3132,7 @@ pub const Type = struct {
3167 }3132 }
31683133
3169 pub fn packedStructFieldByteOffset(ty: Type, field_index: usize, mod: *Module) u32 {3134 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;
3171 const struct_obj = mod.structPtrUnwrap(struct_type.index).?;3136 const struct_obj = mod.structPtrUnwrap(struct_type.index).?;
3172 assert(struct_obj.layout == .Packed);3137 assert(struct_obj.layout == .Packed);
3173 comptime assert(Type.packed_struct_layout_version == 2);3138 comptime assert(Type.packed_struct_layout_version == 2);
...@@ -3229,7 +3194,7 @@ pub const Type = struct {...@@ -3229,7 +3194,7 @@ pub const Type = struct {
3229 /// Get an iterator that iterates over all the struct field, returning the field and3194 /// Get an iterator that iterates over all the struct field, returning the field and
3230 /// offset of that field. Asserts that the type is a non-packed struct.3195 /// offset of that field. Asserts that the type is a non-packed struct.
3231 pub fn iterateStructOffsets(ty: Type, mod: *Module) StructOffsetIterator {3196 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;
3233 const struct_obj = mod.structPtrUnwrap(struct_type.index).?;3198 const struct_obj = mod.structPtrUnwrap(struct_type.index).?;
3234 assert(struct_obj.haveLayout());3199 assert(struct_obj.haveLayout());
3235 assert(struct_obj.layout != .Packed);3200 assert(struct_obj.layout != .Packed);
...@@ -3238,7 +3203,7 @@ pub const Type = struct {...@@ -3238,7 +3203,7 @@ pub const Type = struct {
32383203
3239 /// Supports structs and unions.3204 /// Supports structs and unions.
3240 pub fn structFieldOffset(ty: Type, index: usize, mod: *Module) u64 {3205 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())) {
3242 .struct_type => |struct_type| {3207 .struct_type => |struct_type| {
3243 const struct_obj = mod.structPtrUnwrap(struct_type.index).?;3208 const struct_obj = mod.structPtrUnwrap(struct_type.index).?;
3244 assert(struct_obj.haveLayout());3209 assert(struct_obj.haveLayout());
...@@ -3296,7 +3261,7 @@ pub const Type = struct {...@@ -3296,7 +3261,7 @@ pub const Type = struct {
3296 }3261 }
32973262
3298 pub fn declSrcLocOrNull(ty: Type, mod: *Module) ?Module.SrcLoc {3263 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())) {
3300 .struct_type => |struct_type| {3265 .struct_type => |struct_type| {
3301 const struct_obj = mod.structPtrUnwrap(struct_type.index).?;3266 const struct_obj = mod.structPtrUnwrap(struct_type.index).?;
3302 return struct_obj.srcLoc(mod);3267 return struct_obj.srcLoc(mod);
...@@ -3316,7 +3281,7 @@ pub const Type = struct {...@@ -3316,7 +3281,7 @@ pub const Type = struct {
3316 }3281 }
33173282
3318 pub fn getOwnerDeclOrNull(ty: Type, mod: *Module) ?Module.Decl.Index {3283 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())) {
3320 .struct_type => |struct_type| {3285 .struct_type => |struct_type| {
3321 const struct_obj = mod.structPtrUnwrap(struct_type.index) orelse return null;3286 const struct_obj = mod.structPtrUnwrap(struct_type.index) orelse return null;
3322 return struct_obj.owner_decl;3287 return struct_obj.owner_decl;
...@@ -3332,33 +3297,30 @@ pub const Type = struct {...@@ -3332,33 +3297,30 @@ pub const Type = struct {
3332 }3297 }
33333298
3334 pub fn isGenericPoison(ty: Type) bool {3299 pub fn isGenericPoison(ty: Type) bool {
3335 return ty.ip_index == .generic_poison_type;3300 return ty.toIntern() == .generic_poison_type;
3336 }3301 }
33373302
3338 pub fn isTuple(ty: Type, mod: *Module) bool {3303 pub fn isTuple(ty: Type, mod: *Module) bool {
3339 return switch (ty.ip_index) {3304 return switch (mod.intern_pool.indexToKey(ty.toIntern())) {
3340 .none => false,3305 .struct_type => |struct_type| {
3341 else => switch (mod.intern_pool.indexToKey(ty.ip_index)) {3306 const struct_obj = mod.structPtrUnwrap(struct_type.index) orelse return false;
3342 .struct_type => |struct_type| {3307 return struct_obj.is_tuple;
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,
3348 },3308 },
3309 .anon_struct_type => |anon_struct| anon_struct.names.len == 0,
3310 else => false,
3349 };3311 };
3350 }3312 }
33513313
3352 pub fn isAnonStruct(ty: Type, mod: *Module) bool {3314 pub fn isAnonStruct(ty: Type, mod: *Module) bool {
3353 if (ty.ip_index == .empty_struct_type) return true;3315 if (ty.toIntern() == .empty_struct_type) return true;
3354 return switch (mod.intern_pool.indexToKey(ty.ip_index)) {3316 return switch (mod.intern_pool.indexToKey(ty.toIntern())) {
3355 .anon_struct_type => |anon_struct_type| anon_struct_type.names.len > 0,3317 .anon_struct_type => |anon_struct_type| anon_struct_type.names.len > 0,
3356 else => false,3318 else => false,
3357 };3319 };
3358 }3320 }
33593321
3360 pub fn isTupleOrAnonStruct(ty: Type, mod: *Module) bool {3322 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())) {
3362 .struct_type => |struct_type| {3324 .struct_type => |struct_type| {
3363 const struct_obj = mod.structPtrUnwrap(struct_type.index) orelse return false;3325 const struct_obj = mod.structPtrUnwrap(struct_type.index) orelse return false;
3364 return struct_obj.is_tuple;3326 return struct_obj.is_tuple;
...@@ -3369,14 +3331,14 @@ pub const Type = struct {...@@ -3369,14 +3331,14 @@ pub const Type = struct {
3369 }3331 }
33703332
3371 pub fn isSimpleTuple(ty: Type, mod: *Module) bool {3333 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())) {
3373 .anon_struct_type => |anon_struct_type| anon_struct_type.names.len == 0,3335 .anon_struct_type => |anon_struct_type| anon_struct_type.names.len == 0,
3374 else => false,3336 else => false,
3375 };3337 };
3376 }3338 }
33773339
3378 pub fn isSimpleTupleOrAnonStruct(ty: Type, mod: *Module) bool {3340 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())) {
3380 .anon_struct_type => true,3342 .anon_struct_type => true,
3381 else => false,3343 else => false,
3382 };3344 };
src/value.zig+33-14
...@@ -345,7 +345,7 @@ pub const Value = struct {...@@ -345,7 +345,7 @@ pub const Value = struct {
345 }345 }
346346
347 pub fn intern(val: Value, ty: Type, mod: *Module) Allocator.Error!InternPool.Index {347 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();
349 switch (val.tag()) {349 switch (val.tag()) {
350 .eu_payload => {350 .eu_payload => {
351 const pl = val.castTag(.eu_payload).?.data;351 const pl = val.castTag(.eu_payload).?.data;
...@@ -506,11 +506,7 @@ pub const Value = struct {...@@ -506,11 +506,7 @@ pub const Value = struct {
506 else => unreachable,506 else => unreachable,
507 };507 };
508 },508 },
509 .enum_type => |enum_type| (try ip.getCoerced(509 .enum_type => |enum_type| try mod.getCoerced(val, enum_type.tag_ty.toType()),
510 mod.gpa,
511 val.toIntern(),
512 enum_type.tag_ty,
513 )).toValue(),
514 else => unreachable,510 else => unreachable,
515 };511 };
516 }512 }
...@@ -872,10 +868,15 @@ pub const Value = struct {...@@ -872,10 +868,15 @@ pub const Value = struct {
872 .Packed => {868 .Packed => {
873 var bits: u16 = 0;869 var bits: u16 = 0;
874 const fields = ty.structFields(mod).values();870 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;
876 for (fields, 0..) |field, i| {872 for (fields, 0..) |field, i| {
877 const field_bits = @intCast(u16, field.ty.bitSize(mod));873 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);
879 bits += field_bits;880 bits += field_bits;
880 }881 }
881 },882 },
...@@ -2006,23 +2007,30 @@ pub const Value = struct {...@@ -2006,23 +2007,30 @@ pub const Value = struct {
2006 }2007 }
20072008
2008 pub fn isPtrToThreadLocal(val: Value, mod: *Module) bool {2009 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 {
2009 return val.ip_index != .none and switch (mod.intern_pool.indexToKey(val.toIntern())) {2017 return val.ip_index != .none and switch (mod.intern_pool.indexToKey(val.toIntern())) {
2010 .variable => |variable| variable.is_threadlocal,2018 .variable => |variable| variable.is_threadlocal,
2011 .ptr => |ptr| switch (ptr.addr) {2019 .ptr => |ptr| switch (ptr.addr) {
2012 .decl => |decl_index| {2020 .decl => |decl_index| {
2013 const decl = mod.declPtr(decl_index);2021 const decl = mod.declPtr(decl_index);
2014 assert(decl.has_tv);2022 assert(decl.has_tv);
2015 return decl.val.isPtrToThreadLocal(mod);2023 return decl.val.isPtrToThreadLocalInner(mod);
2016 },2024 },
2017 .mut_decl => |mut_decl| {2025 .mut_decl => |mut_decl| {
2018 const decl = mod.declPtr(mut_decl.decl);2026 const decl = mod.declPtr(mut_decl.decl);
2019 assert(decl.has_tv);2027 assert(decl.has_tv);
2020 return decl.val.isPtrToThreadLocal(mod);2028 return decl.val.isPtrToThreadLocalInner(mod);
2021 },2029 },
2022 .int => false,2030 .int => false,
2023 .eu_payload, .opt_payload => |base_ptr| base_ptr.toValue().isPtrToThreadLocal(mod),2031 .eu_payload, .opt_payload => |base_ptr| base_ptr.toValue().isPtrToThreadLocalInner(mod),
2024 .comptime_field => |comptime_field| comptime_field.toValue().isPtrToThreadLocal(mod),2032 .comptime_field => |comptime_field| comptime_field.toValue().isPtrToThreadLocalInner(mod),
2025 .elem, .field => |base_index| base_index.base.toValue().isPtrToThreadLocal(mod),2033 .elem, .field => |base_index| base_index.base.toValue().isPtrToThreadLocalInner(mod),
2026 },2034 },
2027 else => false,2035 else => false,
2028 };2036 };
...@@ -2045,7 +2053,18 @@ pub const Value = struct {...@@ -2045,7 +2053,18 @@ pub const Value = struct {
2045 else => unreachable,2053 else => unreachable,
2046 },2054 },
2047 .aggregate => |aggregate| (try mod.intern(.{ .aggregate = .{2055 .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(),
2049 .storage = switch (aggregate.storage) {2068 .storage = switch (aggregate.storage) {
2050 .bytes => |bytes| .{ .bytes = bytes[start..end] },2069 .bytes => |bytes| .{ .bytes = bytes[start..end] },
2051 .elems => |elems| .{ .elems = elems[start..end] },2070 .elems => |elems| .{ .elems = elems[start..end] },
tools/lldb_pretty_printers.py+10-3
...@@ -347,9 +347,15 @@ class TagAndPayload_SynthProvider:...@@ -347,9 +347,15 @@ class TagAndPayload_SynthProvider:
347 except: return -1347 except: return -1
348 def get_child_at_index(self, index): return (self.tag, self.payload)[index] if index in range(2) else None348 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):
351 members = value.type.enum_members351 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
354class Module_Decl__Module_Decl_Index_SynthProvider:360class Module_Decl__Module_Decl_Index_SynthProvider:
355 def __init__(self, value, _=None): self.value = value361 def __init__(self, value, _=None): self.value = value
...@@ -676,8 +682,9 @@ def __lldb_init_module(debugger, _=None):...@@ -676,8 +682,9 @@ def __lldb_init_module(debugger, _=None):
676 add(debugger, category='zig.stage2', type='Zir.Inst', identifier='TagAndPayload', synth=True, inline_children=True, summary=True)682 add(debugger, category='zig.stage2', type='Zir.Inst', identifier='TagAndPayload', synth=True, inline_children=True, summary=True)
677 add(debugger, category='zig.stage2', regex=True, type=MultiArrayList_Entry('Zir\\.Inst'), identifier='TagAndPayload', synth=True, inline_children=True, summary=True)683 add(debugger, category='zig.stage2', regex=True, type=MultiArrayList_Entry('Zir\\.Inst'), identifier='TagAndPayload', synth=True, inline_children=True, summary=True)
678 add(debugger, category='zig.stage2', regex=True, type='^Zir\\.Inst\\.Data\\.Data__struct_[1-9][0-9]*$', inline_children=True, summary=True)684 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)
680 add(debugger, category='zig.stage2', type='Air.Inst', identifier='TagAndPayload', synth=True, inline_children=True, summary=True)686 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)
681 add(debugger, category='zig.stage2', regex=True, type=MultiArrayList_Entry('Air\\.Inst'), identifier='TagAndPayload', synth=True, inline_children=True, summary=True)688 add(debugger, category='zig.stage2', regex=True, type=MultiArrayList_Entry('Air\\.Inst'), identifier='TagAndPayload', synth=True, inline_children=True, summary=True)
682 add(debugger, category='zig.stage2', regex=True, type='^Air\\.Inst\\.Data\\.Data__struct_[1-9][0-9]*$', inline_children=True, summary=True)689 add(debugger, category='zig.stage2', regex=True, type='^Air\\.Inst\\.Data\\.Data__struct_[1-9][0-9]*$', inline_children=True, summary=True)
683 add(debugger, category='zig.stage2', type='Module.Decl::Module.Decl.Index', synth=True)690 add(debugger, category='zig.stage2', type='Module.Decl::Module.Decl.Index', synth=True)