authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-05-12 10:59:04-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-06-10 20:42:31-07:00
logd89807efbb1bd5af0a92544298fc08ad6ba2d255
tree6df53b53276ff7facead371acde63ad68bb0032f
parent466328d1ca29f3f6dd142f74dda13b26687e71e0

stage2: remove legacy Type array and array_sentinel

These are now handled by the InternPool.

4 files changed, 32 insertions(+), 247 deletions(-)

src/Module.zig+3-2
......@@ -6543,9 +6543,10 @@ pub fn populateTestFunctions(
65436543
65446544 const test_fn_vals = try arena.alloc(Value, mod.test_functions.count());
65456545 const array_decl_index = try mod.createAnonymousDeclFromDecl(decl, decl.src_namespace, null, .{
6546 .ty = try Type.Tag.array.create(arena, .{
6546 .ty = try mod.arrayType(.{
65476547 .len = test_fn_vals.len,
6548 .elem_type = try tmp_test_fn_ty.copy(arena),
6548 .child = tmp_test_fn_ty.ip_index,
6549 .sentinel = .none,
65496550 }),
65506551 .val = try Value.Tag.aggregate.create(arena, test_fn_vals),
65516552 });
src/Sema.zig+18-34
......@@ -16015,9 +16015,10 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1601516015 const param_info_decl = mod.declPtr(param_info_decl_index);
1601616016 const param_ty = param_info_decl.val.toType();
1601716017 const new_decl = try params_anon_decl.finish(
16018 try Type.Tag.array.create(params_anon_decl.arena(), .{
16018 try mod.arrayType(.{
1601916019 .len = param_vals.len,
16020 .elem_type = try param_ty.copy(params_anon_decl.arena()),
16020 .child = param_ty.ip_index,
16021 .sentinel = .none,
1602116022 }),
1602216023 try Value.Tag.aggregate.create(
1602316024 params_anon_decl.arena(),
......@@ -16238,9 +16239,10 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1623816239 // Build our ?[]const Error value
1623916240 const errors_val = if (error_field_vals) |vals| v: {
1624016241 const new_decl = try fields_anon_decl.finish(
16241 try Type.Tag.array.create(fields_anon_decl.arena(), .{
16242 try mod.arrayType(.{
1624216243 .len = vals.len,
16243 .elem_type = error_field_ty,
16244 .child = error_field_ty.ip_index,
16245 .sentinel = .none,
1624416246 }),
1624516247 try Value.Tag.aggregate.create(
1624616248 fields_anon_decl.arena(),
......@@ -16332,9 +16334,10 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1633216334
1633316335 const fields_val = v: {
1633416336 const new_decl = try fields_anon_decl.finish(
16335 try Type.Tag.array.create(fields_anon_decl.arena(), .{
16337 try mod.arrayType(.{
1633616338 .len = enum_field_vals.len,
16337 .elem_type = enum_field_ty,
16339 .child = enum_field_ty.ip_index,
16340 .sentinel = .none,
1633816341 }),
1633916342 try Value.Tag.aggregate.create(
1634016343 fields_anon_decl.arena(),
......@@ -16427,9 +16430,10 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1642716430
1642816431 const fields_val = v: {
1642916432 const new_decl = try fields_anon_decl.finish(
16430 try Type.Tag.array.create(fields_anon_decl.arena(), .{
16433 try mod.arrayType(.{
1643116434 .len = union_field_vals.len,
16432 .elem_type = union_field_ty,
16435 .child = union_field_ty.ip_index,
16436 .sentinel = .none,
1643316437 }),
1643416438 try Value.Tag.aggregate.create(
1643516439 fields_anon_decl.arena(),
......@@ -16590,9 +16594,10 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
1659016594
1659116595 const fields_val = v: {
1659216596 const new_decl = try fields_anon_decl.finish(
16593 try Type.Tag.array.create(fields_anon_decl.arena(), .{
16597 try mod.arrayType(.{
1659416598 .len = struct_field_vals.len,
16595 .elem_type = struct_field_ty,
16599 .child = struct_field_ty.ip_index,
16600 .sentinel = .none,
1659616601 }),
1659716602 try Value.Tag.aggregate.create(
1659816603 fields_anon_decl.arena(),
......@@ -16707,9 +16712,10 @@ fn typeInfoDecls(
1670716712 }
1670816713
1670916714 const new_decl = try decls_anon_decl.finish(
16710 try Type.Tag.array.create(decls_anon_decl.arena(), .{
16715 try mod.arrayType(.{
1671116716 .len = decl_vals.items.len,
16712 .elem_type = declaration_ty,
16717 .child = declaration_ty.ip_index,
16718 .sentinel = .none,
1671316719 }),
1671416720 try Value.Tag.aggregate.create(
1671516721 decls_anon_decl.arena(),
......@@ -31574,10 +31580,6 @@ pub fn resolveTypeRequiresComptime(sema: *Sema, ty: Type) CompileError!bool {
3157431580 .inferred_alloc_mut => unreachable,
3157531581 .inferred_alloc_const => unreachable,
3157631582
31577 .array,
31578 .array_sentinel,
31579 => return sema.resolveTypeRequiresComptime(ty.childType(mod)),
31580
3158131583 .pointer => {
3158231584 const child_ty = ty.childType(mod);
3158331585 if (child_ty.zigTypeTag(mod) == .Fn) {
......@@ -32862,7 +32864,6 @@ fn generateUnionTagTypeNumbered(
3286232864 new_decl.name_fully_qualified = true;
3286332865 new_decl.owns_tv = true;
3286432866 new_decl.name_fully_qualified = true;
32865 errdefer mod.abortAnonDecl(new_decl_index);
3286632867
3286732868 const enum_ty = try mod.intern(.{ .enum_type = .{
3286832869 .decl = new_decl_index,
......@@ -32875,7 +32876,6 @@ fn generateUnionTagTypeNumbered(
3287532876 .values = enum_field_vals,
3287632877 .tag_mode = .explicit,
3287732878 } });
32878 errdefer mod.intern_pool.remove(enum_ty);
3287932879
3288032880 new_decl.val = enum_ty.toValue();
3288132881
......@@ -32924,12 +32924,10 @@ fn generateUnionTagTypeSimple(
3292432924 .values = &.{},
3292532925 .tag_mode = .auto,
3292632926 } });
32927 errdefer mod.intern_pool.remove(enum_ty);
3292832927
3292932928 const new_decl = mod.declPtr(new_decl_index);
3293032929 new_decl.owns_tv = true;
3293132930 new_decl.val = enum_ty.toValue();
32932 errdefer mod.abortAnonDecl(new_decl_index);
3293332931
3293432932 return enum_ty.toType();
3293532933}
......@@ -33024,7 +33022,6 @@ pub fn typeHasOnePossibleValue(sema: *Sema, ty: Type) CompileError!?Value {
3302433022 .error_set_merged,
3302533023 .error_union,
3302633024 .function,
33027 .array_sentinel,
3302833025 .error_set_inferred,
3302933026 .anyframe_T,
3303033027 .pointer,
......@@ -33050,15 +33047,6 @@ pub fn typeHasOnePossibleValue(sema: *Sema, ty: Type) CompileError!?Value {
3305033047 return Value.empty_struct;
3305133048 },
3305233049
33053 .array => {
33054 if (ty.arrayLen(mod) == 0)
33055 return Value.initTag(.empty_array);
33056 if ((try sema.typeHasOnePossibleValue(ty.childType(mod))) != null) {
33057 return Value.initTag(.the_only_possible_value);
33058 }
33059 return null;
33060 },
33061
3306233050 .inferred_alloc_const => unreachable,
3306333051 .inferred_alloc_mut => unreachable,
3306433052 },
......@@ -33631,10 +33619,6 @@ pub fn typeRequiresComptime(sema: *Sema, ty: Type) CompileError!bool {
3363133619 .inferred_alloc_mut => unreachable,
3363233620 .inferred_alloc_const => unreachable,
3363333621
33634 .array,
33635 .array_sentinel,
33636 => return sema.typeRequiresComptime(ty.childType(mod)),
33637
3363833622 .pointer => {
3363933623 const child_ty = ty.childType(mod);
3364033624 if (child_ty.zigTypeTag(mod) == .Fn) {
src/codegen/c.zig+4-4
......@@ -2467,11 +2467,11 @@ pub fn genErrDecls(o: *Object) !void {
24672467 try writer.writeAll(";\n");
24682468 }
24692469
2470 var name_array_ty_pl = Type.Payload.Array{ .base = .{ .tag = .array }, .data = .{
2470 const name_array_ty = try mod.arrayType(.{
24712471 .len = mod.error_name_list.items.len,
2472 .elem_type = Type.const_slice_u8_sentinel_0,
2473 } };
2474 const name_array_ty = Type.initPayload(&name_array_ty_pl.base);
2472 .child = .const_slice_u8_sentinel_0_type,
2473 .sentinel = .zero_u8,
2474 });
24752475
24762476 try writer.writeAll("static ");
24772477 try o.dg.renderTypeAndName(writer, name_array_ty, .{ .identifier = array_identifier }, Const, 0, .complete);
src/type.zig+7-207
......@@ -44,10 +44,6 @@ pub const Type = struct {
4444
4545 .function => return .Fn,
4646
47 .array,
48 .array_sentinel,
49 => return .Array,
50
5147 .pointer,
5248 .inferred_alloc_const,
5349 .inferred_alloc_mut,
......@@ -398,29 +394,6 @@ pub const Type = struct {
398394 return true;
399395 },
400396
401 .array,
402 .array_sentinel,
403 => {
404 if (a.zigTypeTag(mod) != b.zigTypeTag(mod)) return false;
405
406 if (a.arrayLen(mod) != b.arrayLen(mod))
407 return false;
408 const elem_ty = a.childType(mod);
409 if (!elem_ty.eql(b.childType(mod), mod))
410 return false;
411 const sentinel_a = a.sentinel(mod);
412 const sentinel_b = b.sentinel(mod);
413 if (sentinel_a) |sa| {
414 if (sentinel_b) |sb| {
415 return sa.eql(sb, elem_ty, mod);
416 } else {
417 return false;
418 }
419 } else {
420 return sentinel_b == null;
421 }
422 },
423
424397 .pointer,
425398 .inferred_alloc_const,
426399 .inferred_alloc_mut,
......@@ -627,17 +600,6 @@ pub const Type = struct {
627600 }
628601 },
629602
630 .array,
631 .array_sentinel,
632 => {
633 std.hash.autoHash(hasher, std.builtin.TypeId.Array);
634
635 const elem_ty = ty.childType(mod);
636 std.hash.autoHash(hasher, ty.arrayLen(mod));
637 hashWithHasher(elem_ty, hasher, mod);
638 hashSentinel(ty.sentinel(mod), elem_ty, hasher, mod);
639 },
640
641603 .pointer,
642604 .inferred_alloc_const,
643605 .inferred_alloc_mut,
......@@ -770,21 +732,6 @@ pub const Type = struct {
770732 };
771733 },
772734
773 .array => {
774 const payload = self.castTag(.array).?.data;
775 return Tag.array.create(allocator, .{
776 .len = payload.len,
777 .elem_type = try payload.elem_type.copy(allocator),
778 });
779 },
780 .array_sentinel => {
781 const payload = self.castTag(.array_sentinel).?.data;
782 return Tag.array_sentinel.create(allocator, .{
783 .len = payload.len,
784 .sentinel = try payload.sentinel.copy(allocator),
785 .elem_type = try payload.elem_type.copy(allocator),
786 });
787 },
788735 .tuple => {
789736 const payload = self.castTag(.tuple).?.data;
790737 const types = try allocator.alloc(Type, payload.types.len);
......@@ -987,21 +934,6 @@ pub const Type = struct {
987934 ty = return_type;
988935 continue;
989936 },
990 .array => {
991 const payload = ty.castTag(.array).?.data;
992 try writer.print("[{d}]", .{payload.len});
993 ty = payload.elem_type;
994 continue;
995 },
996 .array_sentinel => {
997 const payload = ty.castTag(.array_sentinel).?.data;
998 try writer.print("[{d}:{}]", .{
999 payload.len,
1000 payload.sentinel.fmtDebug(),
1001 });
1002 ty = payload.elem_type;
1003 continue;
1004 },
1005937 .tuple => {
1006938 const tuple = ty.castTag(.tuple).?.data;
1007939 try writer.writeAll("tuple{");
......@@ -1198,19 +1130,6 @@ pub const Type = struct {
11981130 try print(error_union.payload, writer, mod);
11991131 },
12001132
1201 .array => {
1202 const payload = ty.castTag(.array).?.data;
1203 try writer.print("[{d}]", .{payload.len});
1204 try print(payload.elem_type, writer, mod);
1205 },
1206 .array_sentinel => {
1207 const payload = ty.castTag(.array_sentinel).?.data;
1208 try writer.print("[{d}:{}]", .{
1209 payload.len,
1210 payload.sentinel.fmtValue(payload.elem_type, mod),
1211 });
1212 try print(payload.elem_type, writer, mod);
1213 },
12141133 .tuple => {
12151134 const tuple = ty.castTag(.tuple).?.data;
12161135
......@@ -1522,10 +1441,6 @@ pub const Type = struct {
15221441 }
15231442 },
15241443
1525 .array => return ty.arrayLen(mod) != 0 and
1526 try ty.childType(mod).hasRuntimeBitsAdvanced(mod, ignore_comptime_only, strat),
1527 .array_sentinel => return ty.childType(mod).hasRuntimeBitsAdvanced(mod, ignore_comptime_only, strat),
1528
15291444 .tuple, .anon_struct => {
15301445 const tuple = ty.tupleFields();
15311446 for (tuple.types, 0..) |field_ty, i| {
......@@ -1723,10 +1638,6 @@ pub const Type = struct {
17231638 .inferred_alloc_mut => unreachable,
17241639 .inferred_alloc_const => unreachable,
17251640
1726 .array,
1727 .array_sentinel,
1728 => ty.childType(mod).hasWellDefinedLayout(mod),
1729
17301641 .optional => ty.isPtrLikeOptional(mod),
17311642 },
17321643 else => switch (mod.intern_pool.indexToKey(ty.ip_index)) {
......@@ -2005,8 +1916,6 @@ pub const Type = struct {
20051916 .error_set_merged,
20061917 => return AbiAlignmentAdvanced{ .scalar = 2 },
20071918
2008 .array, .array_sentinel => return ty.childType(mod).abiAlignmentAdvanced(mod, strat),
2009
20101919 .optional => return abiAlignmentAdvancedOptional(ty, mod, strat),
20111920 .error_union => return abiAlignmentAdvancedErrorUnion(ty, mod, strat),
20121921
......@@ -2385,29 +2294,6 @@ pub const Type = struct {
23852294 return AbiSizeAdvanced{ .scalar = ty.structFieldOffset(field_count, mod) };
23862295 },
23872296
2388 .array => {
2389 const payload = ty.castTag(.array).?.data;
2390 switch (try payload.elem_type.abiSizeAdvanced(mod, strat)) {
2391 .scalar => |elem_size| return AbiSizeAdvanced{ .scalar = payload.len * elem_size },
2392 .val => switch (strat) {
2393 .sema => unreachable,
2394 .eager => unreachable,
2395 .lazy => |arena| return AbiSizeAdvanced{ .val = try Value.Tag.lazy_size.create(arena, ty) },
2396 },
2397 }
2398 },
2399 .array_sentinel => {
2400 const payload = ty.castTag(.array_sentinel).?.data;
2401 switch (try payload.elem_type.abiSizeAdvanced(mod, strat)) {
2402 .scalar => |elem_size| return AbiSizeAdvanced{ .scalar = (payload.len + 1) * elem_size },
2403 .val => switch (strat) {
2404 .sema => unreachable,
2405 .eager => unreachable,
2406 .lazy => |arena| return AbiSizeAdvanced{ .val = try Value.Tag.lazy_size.create(arena, ty) },
2407 },
2408 }
2409 },
2410
24112297 .anyframe_T => return AbiSizeAdvanced{ .scalar = @divExact(target.ptrBitWidth(), 8) },
24122298
24132299 .pointer => switch (ty.castTag(.pointer).?.data.size) {
......@@ -2729,24 +2615,6 @@ pub const Type = struct {
27292615 return total;
27302616 },
27312617
2732 .array => {
2733 const payload = ty.castTag(.array).?.data;
2734 const elem_size = std.math.max(payload.elem_type.abiAlignment(mod), payload.elem_type.abiSize(mod));
2735 if (elem_size == 0 or payload.len == 0)
2736 return @as(u64, 0);
2737 const elem_bit_size = try bitSizeAdvanced(payload.elem_type, mod, opt_sema);
2738 return (payload.len - 1) * 8 * elem_size + elem_bit_size;
2739 },
2740 .array_sentinel => {
2741 const payload = ty.castTag(.array_sentinel).?.data;
2742 const elem_size = std.math.max(
2743 payload.elem_type.abiAlignment(mod),
2744 payload.elem_type.abiSize(mod),
2745 );
2746 const elem_bit_size = try bitSizeAdvanced(payload.elem_type, mod, opt_sema);
2747 return payload.len * 8 * elem_size + elem_bit_size;
2748 },
2749
27502618 .anyframe_T => return target.ptrBitWidth(),
27512619
27522620 .pointer => switch (ty.castTag(.pointer).?.data.size) {
......@@ -3188,9 +3056,6 @@ pub const Type = struct {
31883056 pub fn childTypeIp(ty: Type, ip: InternPool) Type {
31893057 return switch (ty.ip_index) {
31903058 .none => switch (ty.tag()) {
3191 .array => ty.castTag(.array).?.data.elem_type,
3192 .array_sentinel => ty.castTag(.array_sentinel).?.data.elem_type,
3193
31943059 .pointer => ty.castTag(.pointer).?.data.pointee_type,
31953060
31963061 else => unreachable,
......@@ -3211,9 +3076,6 @@ pub const Type = struct {
32113076 pub fn elemType2(ty: Type, mod: *const Module) Type {
32123077 return switch (ty.ip_index) {
32133078 .none => switch (ty.tag()) {
3214 .array => ty.castTag(.array).?.data.elem_type,
3215 .array_sentinel => ty.castTag(.array_sentinel).?.data.elem_type,
3216
32173079 .pointer => {
32183080 const info = ty.castTag(.pointer).?.data;
32193081 const child_ty = info.pointee_type;
......@@ -3483,8 +3345,6 @@ pub const Type = struct {
34833345 return switch (ty.ip_index) {
34843346 .empty_struct_type => 0,
34853347 .none => switch (ty.tag()) {
3486 .array => ty.castTag(.array).?.data.len,
3487 .array_sentinel => ty.castTag(.array_sentinel).?.data.len,
34883348 .tuple => ty.castTag(.tuple).?.data.types.len,
34893349 .anon_struct => ty.castTag(.anon_struct).?.data.types.len,
34903350
......@@ -3524,12 +3384,9 @@ pub const Type = struct {
35243384 pub fn sentinel(ty: Type, mod: *const Module) ?Value {
35253385 return switch (ty.ip_index) {
35263386 .none => switch (ty.tag()) {
3527 .array,
3528 .tuple,
3529 => null,
3387 .tuple => null,
35303388
35313389 .pointer => ty.castTag(.pointer).?.data.sentinel,
3532 .array_sentinel => ty.castTag(.array_sentinel).?.data.sentinel,
35333390
35343391 else => unreachable,
35353392 },
......@@ -3832,7 +3689,6 @@ pub const Type = struct {
38323689 .error_set,
38333690 .error_set_merged,
38343691 .function,
3835 .array_sentinel,
38363692 .error_set_inferred,
38373693 .anyframe_T,
38383694 .pointer,
......@@ -3858,14 +3714,6 @@ pub const Type = struct {
38583714 return Value.empty_struct;
38593715 },
38603716
3861 .array => {
3862 if (ty.arrayLen(mod) == 0)
3863 return Value.initTag(.empty_array);
3864 if ((try ty.childType(mod).onePossibleValue(mod)) != null)
3865 return Value.initTag(.the_only_possible_value);
3866 return null;
3867 },
3868
38693717 .inferred_alloc_const => unreachable,
38703718 .inferred_alloc_mut => unreachable,
38713719 },
......@@ -4034,10 +3882,6 @@ pub const Type = struct {
40343882 .inferred_alloc_mut => unreachable,
40353883 .inferred_alloc_const => unreachable,
40363884
4037 .array,
4038 .array_sentinel,
4039 => return ty.childType(mod).comptimeOnly(mod),
4040
40413885 .pointer => {
40423886 const child_ty = ty.childType(mod);
40433887 if (child_ty.zigTypeTag(mod) == .Fn) {
......@@ -4804,8 +4648,6 @@ pub const Type = struct {
48044648 inferred_alloc_const, // See last_no_payload_tag below.
48054649 // After this, the tag requires a payload.
48064650
4807 array,
4808 array_sentinel,
48094651 /// Possible Value tags for this: @"struct"
48104652 tuple,
48114653 /// Possible Value tags for this: @"struct"
......@@ -4838,8 +4680,6 @@ pub const Type = struct {
48384680 .error_set_inferred => Payload.ErrorSetInferred,
48394681 .error_set_merged => Payload.ErrorSetMerged,
48404682
4841 .array => Payload.Array,
4842 .array_sentinel => Payload.ArraySentinel,
48434683 .pointer => Payload.Pointer,
48444684 .function => Payload.Function,
48454685 .error_union => Payload.ErrorUnion,
......@@ -4965,25 +4805,6 @@ pub const Type = struct {
49654805 data: u64,
49664806 };
49674807
4968 pub const Array = struct {
4969 base: Payload,
4970 data: struct {
4971 len: u64,
4972 elem_type: Type,
4973 },
4974 };
4975
4976 pub const ArraySentinel = struct {
4977 pub const base_tag = Tag.array_sentinel;
4978
4979 base: Payload = Payload{ .tag = base_tag },
4980 data: struct {
4981 len: u64,
4982 sentinel: Value,
4983 elem_type: Type,
4984 },
4985 };
4986
49874808 pub const ElemType = struct {
49884809 base: Payload,
49894810 data: Type,
......@@ -5269,35 +5090,14 @@ pub const Type = struct {
52695090 elem_type: Type,
52705091 mod: *Module,
52715092 ) Allocator.Error!Type {
5272 if (elem_type.ip_index != .none) {
5273 if (sent) |s| {
5274 if (s.ip_index != .none) {
5275 return mod.arrayType(.{
5276 .len = len,
5277 .child = elem_type.ip_index,
5278 .sentinel = s.ip_index,
5279 });
5280 }
5281 } else {
5282 return mod.arrayType(.{
5283 .len = len,
5284 .child = elem_type.ip_index,
5285 .sentinel = .none,
5286 });
5287 }
5288 }
5289
5290 if (sent) |some| {
5291 return Tag.array_sentinel.create(arena, .{
5292 .len = len,
5293 .sentinel = some,
5294 .elem_type = elem_type,
5295 });
5296 }
5093 // TODO: update callsites of this function to directly call mod.arrayType
5094 // and then delete this function.
5095 _ = arena;
52975096
5298 return Tag.array.create(arena, .{
5097 return mod.arrayType(.{
52995098 .len = len,
5300 .elem_type = elem_type,
5099 .child = elem_type.ip_index,
5100 .sentinel = if (sent) |s| s.ip_index else .none,
53015101 });
53025102 }
53035103