From d89807efbb1bd5af0a92544298fc08ad6ba2d255 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Fri, 12 May 2023 10:59:04 -0700 Subject: [PATCH] stage2: remove legacy Type array and array_sentinel These are now handled by the InternPool. --- src/Module.zig | 5 +- src/Sema.zig | 52 ++++------- src/codegen/c.zig | 8 +- src/type.zig | 214 ++-------------------------------------------- 4 files changed, 32 insertions(+), 247 deletions(-) diff --git a/src/Module.zig b/src/Module.zig index b32904e165..426d274011 100644 --- a/src/Module.zig +++ b/src/Module.zig @@ -6543,9 +6543,10 @@ pub fn populateTestFunctions( const test_fn_vals = try arena.alloc(Value, mod.test_functions.count()); const array_decl_index = try mod.createAnonymousDeclFromDecl(decl, decl.src_namespace, null, .{ - .ty = try Type.Tag.array.create(arena, .{ + .ty = try mod.arrayType(.{ .len = test_fn_vals.len, - .elem_type = try tmp_test_fn_ty.copy(arena), + .child = tmp_test_fn_ty.ip_index, + .sentinel = .none, }), .val = try Value.Tag.aggregate.create(arena, test_fn_vals), }); diff --git a/src/Sema.zig b/src/Sema.zig index b5baf4dde3..9e89ca89ef 100644 --- a/src/Sema.zig +++ b/src/Sema.zig @@ -16015,9 +16015,10 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai const param_info_decl = mod.declPtr(param_info_decl_index); const param_ty = param_info_decl.val.toType(); const new_decl = try params_anon_decl.finish( - try Type.Tag.array.create(params_anon_decl.arena(), .{ + try mod.arrayType(.{ .len = param_vals.len, - .elem_type = try param_ty.copy(params_anon_decl.arena()), + .child = param_ty.ip_index, + .sentinel = .none, }), try Value.Tag.aggregate.create( params_anon_decl.arena(), @@ -16238,9 +16239,10 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai // Build our ?[]const Error value const errors_val = if (error_field_vals) |vals| v: { const new_decl = try fields_anon_decl.finish( - try Type.Tag.array.create(fields_anon_decl.arena(), .{ + try mod.arrayType(.{ .len = vals.len, - .elem_type = error_field_ty, + .child = error_field_ty.ip_index, + .sentinel = .none, }), try Value.Tag.aggregate.create( fields_anon_decl.arena(), @@ -16332,9 +16334,10 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai const fields_val = v: { const new_decl = try fields_anon_decl.finish( - try Type.Tag.array.create(fields_anon_decl.arena(), .{ + try mod.arrayType(.{ .len = enum_field_vals.len, - .elem_type = enum_field_ty, + .child = enum_field_ty.ip_index, + .sentinel = .none, }), try Value.Tag.aggregate.create( fields_anon_decl.arena(), @@ -16427,9 +16430,10 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai const fields_val = v: { const new_decl = try fields_anon_decl.finish( - try Type.Tag.array.create(fields_anon_decl.arena(), .{ + try mod.arrayType(.{ .len = union_field_vals.len, - .elem_type = union_field_ty, + .child = union_field_ty.ip_index, + .sentinel = .none, }), try Value.Tag.aggregate.create( fields_anon_decl.arena(), @@ -16590,9 +16594,10 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai const fields_val = v: { const new_decl = try fields_anon_decl.finish( - try Type.Tag.array.create(fields_anon_decl.arena(), .{ + try mod.arrayType(.{ .len = struct_field_vals.len, - .elem_type = struct_field_ty, + .child = struct_field_ty.ip_index, + .sentinel = .none, }), try Value.Tag.aggregate.create( fields_anon_decl.arena(), @@ -16707,9 +16712,10 @@ fn typeInfoDecls( } const new_decl = try decls_anon_decl.finish( - try Type.Tag.array.create(decls_anon_decl.arena(), .{ + try mod.arrayType(.{ .len = decl_vals.items.len, - .elem_type = declaration_ty, + .child = declaration_ty.ip_index, + .sentinel = .none, }), try Value.Tag.aggregate.create( decls_anon_decl.arena(), @@ -31574,10 +31580,6 @@ pub fn resolveTypeRequiresComptime(sema: *Sema, ty: Type) CompileError!bool { .inferred_alloc_mut => unreachable, .inferred_alloc_const => unreachable, - .array, - .array_sentinel, - => return sema.resolveTypeRequiresComptime(ty.childType(mod)), - .pointer => { const child_ty = ty.childType(mod); if (child_ty.zigTypeTag(mod) == .Fn) { @@ -32862,7 +32864,6 @@ fn generateUnionTagTypeNumbered( new_decl.name_fully_qualified = true; new_decl.owns_tv = true; new_decl.name_fully_qualified = true; - errdefer mod.abortAnonDecl(new_decl_index); const enum_ty = try mod.intern(.{ .enum_type = .{ .decl = new_decl_index, @@ -32875,7 +32876,6 @@ fn generateUnionTagTypeNumbered( .values = enum_field_vals, .tag_mode = .explicit, } }); - errdefer mod.intern_pool.remove(enum_ty); new_decl.val = enum_ty.toValue(); @@ -32924,12 +32924,10 @@ fn generateUnionTagTypeSimple( .values = &.{}, .tag_mode = .auto, } }); - errdefer mod.intern_pool.remove(enum_ty); const new_decl = mod.declPtr(new_decl_index); new_decl.owns_tv = true; new_decl.val = enum_ty.toValue(); - errdefer mod.abortAnonDecl(new_decl_index); return enum_ty.toType(); } @@ -33024,7 +33022,6 @@ pub fn typeHasOnePossibleValue(sema: *Sema, ty: Type) CompileError!?Value { .error_set_merged, .error_union, .function, - .array_sentinel, .error_set_inferred, .anyframe_T, .pointer, @@ -33050,15 +33047,6 @@ pub fn typeHasOnePossibleValue(sema: *Sema, ty: Type) CompileError!?Value { return Value.empty_struct; }, - .array => { - if (ty.arrayLen(mod) == 0) - return Value.initTag(.empty_array); - if ((try sema.typeHasOnePossibleValue(ty.childType(mod))) != null) { - return Value.initTag(.the_only_possible_value); - } - return null; - }, - .inferred_alloc_const => unreachable, .inferred_alloc_mut => unreachable, }, @@ -33631,10 +33619,6 @@ pub fn typeRequiresComptime(sema: *Sema, ty: Type) CompileError!bool { .inferred_alloc_mut => unreachable, .inferred_alloc_const => unreachable, - .array, - .array_sentinel, - => return sema.typeRequiresComptime(ty.childType(mod)), - .pointer => { const child_ty = ty.childType(mod); if (child_ty.zigTypeTag(mod) == .Fn) { diff --git a/src/codegen/c.zig b/src/codegen/c.zig index 10a4856ad0..c8e7303545 100644 --- a/src/codegen/c.zig +++ b/src/codegen/c.zig @@ -2467,11 +2467,11 @@ pub fn genErrDecls(o: *Object) !void { try writer.writeAll(";\n"); } - var name_array_ty_pl = Type.Payload.Array{ .base = .{ .tag = .array }, .data = .{ + const name_array_ty = try mod.arrayType(.{ .len = mod.error_name_list.items.len, - .elem_type = Type.const_slice_u8_sentinel_0, - } }; - const name_array_ty = Type.initPayload(&name_array_ty_pl.base); + .child = .const_slice_u8_sentinel_0_type, + .sentinel = .zero_u8, + }); try writer.writeAll("static "); try o.dg.renderTypeAndName(writer, name_array_ty, .{ .identifier = array_identifier }, Const, 0, .complete); diff --git a/src/type.zig b/src/type.zig index 2e4e9ca6fe..8358f3678d 100644 --- a/src/type.zig +++ b/src/type.zig @@ -44,10 +44,6 @@ pub const Type = struct { .function => return .Fn, - .array, - .array_sentinel, - => return .Array, - .pointer, .inferred_alloc_const, .inferred_alloc_mut, @@ -398,29 +394,6 @@ pub const Type = struct { return true; }, - .array, - .array_sentinel, - => { - if (a.zigTypeTag(mod) != b.zigTypeTag(mod)) return false; - - if (a.arrayLen(mod) != b.arrayLen(mod)) - return false; - const elem_ty = a.childType(mod); - if (!elem_ty.eql(b.childType(mod), mod)) - return false; - const sentinel_a = a.sentinel(mod); - const sentinel_b = b.sentinel(mod); - if (sentinel_a) |sa| { - if (sentinel_b) |sb| { - return sa.eql(sb, elem_ty, mod); - } else { - return false; - } - } else { - return sentinel_b == null; - } - }, - .pointer, .inferred_alloc_const, .inferred_alloc_mut, @@ -627,17 +600,6 @@ pub const Type = struct { } }, - .array, - .array_sentinel, - => { - std.hash.autoHash(hasher, std.builtin.TypeId.Array); - - const elem_ty = ty.childType(mod); - std.hash.autoHash(hasher, ty.arrayLen(mod)); - hashWithHasher(elem_ty, hasher, mod); - hashSentinel(ty.sentinel(mod), elem_ty, hasher, mod); - }, - .pointer, .inferred_alloc_const, .inferred_alloc_mut, @@ -770,21 +732,6 @@ pub const Type = struct { }; }, - .array => { - const payload = self.castTag(.array).?.data; - return Tag.array.create(allocator, .{ - .len = payload.len, - .elem_type = try payload.elem_type.copy(allocator), - }); - }, - .array_sentinel => { - const payload = self.castTag(.array_sentinel).?.data; - return Tag.array_sentinel.create(allocator, .{ - .len = payload.len, - .sentinel = try payload.sentinel.copy(allocator), - .elem_type = try payload.elem_type.copy(allocator), - }); - }, .tuple => { const payload = self.castTag(.tuple).?.data; const types = try allocator.alloc(Type, payload.types.len); @@ -987,21 +934,6 @@ pub const Type = struct { ty = return_type; continue; }, - .array => { - const payload = ty.castTag(.array).?.data; - try writer.print("[{d}]", .{payload.len}); - ty = payload.elem_type; - continue; - }, - .array_sentinel => { - const payload = ty.castTag(.array_sentinel).?.data; - try writer.print("[{d}:{}]", .{ - payload.len, - payload.sentinel.fmtDebug(), - }); - ty = payload.elem_type; - continue; - }, .tuple => { const tuple = ty.castTag(.tuple).?.data; try writer.writeAll("tuple{"); @@ -1198,19 +1130,6 @@ pub const Type = struct { try print(error_union.payload, writer, mod); }, - .array => { - const payload = ty.castTag(.array).?.data; - try writer.print("[{d}]", .{payload.len}); - try print(payload.elem_type, writer, mod); - }, - .array_sentinel => { - const payload = ty.castTag(.array_sentinel).?.data; - try writer.print("[{d}:{}]", .{ - payload.len, - payload.sentinel.fmtValue(payload.elem_type, mod), - }); - try print(payload.elem_type, writer, mod); - }, .tuple => { const tuple = ty.castTag(.tuple).?.data; @@ -1522,10 +1441,6 @@ pub const Type = struct { } }, - .array => return ty.arrayLen(mod) != 0 and - try ty.childType(mod).hasRuntimeBitsAdvanced(mod, ignore_comptime_only, strat), - .array_sentinel => return ty.childType(mod).hasRuntimeBitsAdvanced(mod, ignore_comptime_only, strat), - .tuple, .anon_struct => { const tuple = ty.tupleFields(); for (tuple.types, 0..) |field_ty, i| { @@ -1723,10 +1638,6 @@ pub const Type = struct { .inferred_alloc_mut => unreachable, .inferred_alloc_const => unreachable, - .array, - .array_sentinel, - => ty.childType(mod).hasWellDefinedLayout(mod), - .optional => ty.isPtrLikeOptional(mod), }, else => switch (mod.intern_pool.indexToKey(ty.ip_index)) { @@ -2005,8 +1916,6 @@ pub const Type = struct { .error_set_merged, => return AbiAlignmentAdvanced{ .scalar = 2 }, - .array, .array_sentinel => return ty.childType(mod).abiAlignmentAdvanced(mod, strat), - .optional => return abiAlignmentAdvancedOptional(ty, mod, strat), .error_union => return abiAlignmentAdvancedErrorUnion(ty, mod, strat), @@ -2385,29 +2294,6 @@ pub const Type = struct { return AbiSizeAdvanced{ .scalar = ty.structFieldOffset(field_count, mod) }; }, - .array => { - const payload = ty.castTag(.array).?.data; - switch (try payload.elem_type.abiSizeAdvanced(mod, strat)) { - .scalar => |elem_size| return AbiSizeAdvanced{ .scalar = payload.len * elem_size }, - .val => switch (strat) { - .sema => unreachable, - .eager => unreachable, - .lazy => |arena| return AbiSizeAdvanced{ .val = try Value.Tag.lazy_size.create(arena, ty) }, - }, - } - }, - .array_sentinel => { - const payload = ty.castTag(.array_sentinel).?.data; - switch (try payload.elem_type.abiSizeAdvanced(mod, strat)) { - .scalar => |elem_size| return AbiSizeAdvanced{ .scalar = (payload.len + 1) * elem_size }, - .val => switch (strat) { - .sema => unreachable, - .eager => unreachable, - .lazy => |arena| return AbiSizeAdvanced{ .val = try Value.Tag.lazy_size.create(arena, ty) }, - }, - } - }, - .anyframe_T => return AbiSizeAdvanced{ .scalar = @divExact(target.ptrBitWidth(), 8) }, .pointer => switch (ty.castTag(.pointer).?.data.size) { @@ -2729,24 +2615,6 @@ pub const Type = struct { return total; }, - .array => { - const payload = ty.castTag(.array).?.data; - const elem_size = std.math.max(payload.elem_type.abiAlignment(mod), payload.elem_type.abiSize(mod)); - if (elem_size == 0 or payload.len == 0) - return @as(u64, 0); - const elem_bit_size = try bitSizeAdvanced(payload.elem_type, mod, opt_sema); - return (payload.len - 1) * 8 * elem_size + elem_bit_size; - }, - .array_sentinel => { - const payload = ty.castTag(.array_sentinel).?.data; - const elem_size = std.math.max( - payload.elem_type.abiAlignment(mod), - payload.elem_type.abiSize(mod), - ); - const elem_bit_size = try bitSizeAdvanced(payload.elem_type, mod, opt_sema); - return payload.len * 8 * elem_size + elem_bit_size; - }, - .anyframe_T => return target.ptrBitWidth(), .pointer => switch (ty.castTag(.pointer).?.data.size) { @@ -3188,9 +3056,6 @@ pub const Type = struct { pub fn childTypeIp(ty: Type, ip: InternPool) Type { return switch (ty.ip_index) { .none => switch (ty.tag()) { - .array => ty.castTag(.array).?.data.elem_type, - .array_sentinel => ty.castTag(.array_sentinel).?.data.elem_type, - .pointer => ty.castTag(.pointer).?.data.pointee_type, else => unreachable, @@ -3211,9 +3076,6 @@ pub const Type = struct { pub fn elemType2(ty: Type, mod: *const Module) Type { return switch (ty.ip_index) { .none => switch (ty.tag()) { - .array => ty.castTag(.array).?.data.elem_type, - .array_sentinel => ty.castTag(.array_sentinel).?.data.elem_type, - .pointer => { const info = ty.castTag(.pointer).?.data; const child_ty = info.pointee_type; @@ -3483,8 +3345,6 @@ pub const Type = struct { return switch (ty.ip_index) { .empty_struct_type => 0, .none => switch (ty.tag()) { - .array => ty.castTag(.array).?.data.len, - .array_sentinel => ty.castTag(.array_sentinel).?.data.len, .tuple => ty.castTag(.tuple).?.data.types.len, .anon_struct => ty.castTag(.anon_struct).?.data.types.len, @@ -3524,12 +3384,9 @@ pub const Type = struct { pub fn sentinel(ty: Type, mod: *const Module) ?Value { return switch (ty.ip_index) { .none => switch (ty.tag()) { - .array, - .tuple, - => null, + .tuple => null, .pointer => ty.castTag(.pointer).?.data.sentinel, - .array_sentinel => ty.castTag(.array_sentinel).?.data.sentinel, else => unreachable, }, @@ -3832,7 +3689,6 @@ pub const Type = struct { .error_set, .error_set_merged, .function, - .array_sentinel, .error_set_inferred, .anyframe_T, .pointer, @@ -3858,14 +3714,6 @@ pub const Type = struct { return Value.empty_struct; }, - .array => { - if (ty.arrayLen(mod) == 0) - return Value.initTag(.empty_array); - if ((try ty.childType(mod).onePossibleValue(mod)) != null) - return Value.initTag(.the_only_possible_value); - return null; - }, - .inferred_alloc_const => unreachable, .inferred_alloc_mut => unreachable, }, @@ -4034,10 +3882,6 @@ pub const Type = struct { .inferred_alloc_mut => unreachable, .inferred_alloc_const => unreachable, - .array, - .array_sentinel, - => return ty.childType(mod).comptimeOnly(mod), - .pointer => { const child_ty = ty.childType(mod); if (child_ty.zigTypeTag(mod) == .Fn) { @@ -4804,8 +4648,6 @@ pub const Type = struct { inferred_alloc_const, // See last_no_payload_tag below. // After this, the tag requires a payload. - array, - array_sentinel, /// Possible Value tags for this: @"struct" tuple, /// Possible Value tags for this: @"struct" @@ -4838,8 +4680,6 @@ pub const Type = struct { .error_set_inferred => Payload.ErrorSetInferred, .error_set_merged => Payload.ErrorSetMerged, - .array => Payload.Array, - .array_sentinel => Payload.ArraySentinel, .pointer => Payload.Pointer, .function => Payload.Function, .error_union => Payload.ErrorUnion, @@ -4965,25 +4805,6 @@ pub const Type = struct { data: u64, }; - pub const Array = struct { - base: Payload, - data: struct { - len: u64, - elem_type: Type, - }, - }; - - pub const ArraySentinel = struct { - pub const base_tag = Tag.array_sentinel; - - base: Payload = Payload{ .tag = base_tag }, - data: struct { - len: u64, - sentinel: Value, - elem_type: Type, - }, - }; - pub const ElemType = struct { base: Payload, data: Type, @@ -5269,35 +5090,14 @@ pub const Type = struct { elem_type: Type, mod: *Module, ) Allocator.Error!Type { - if (elem_type.ip_index != .none) { - if (sent) |s| { - if (s.ip_index != .none) { - return mod.arrayType(.{ - .len = len, - .child = elem_type.ip_index, - .sentinel = s.ip_index, - }); - } - } else { - return mod.arrayType(.{ - .len = len, - .child = elem_type.ip_index, - .sentinel = .none, - }); - } - } + // TODO: update callsites of this function to directly call mod.arrayType + // and then delete this function. + _ = arena; - if (sent) |some| { - return Tag.array_sentinel.create(arena, .{ - .len = len, - .sentinel = some, - .elem_type = elem_type, - }); - } - - return Tag.array.create(arena, .{ + return mod.arrayType(.{ .len = len, - .elem_type = elem_type, + .child = elem_type.ip_index, + .sentinel = if (sent) |s| s.ip_index else .none, }); }