authorgravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2024-03-26 04:39:01+00:00
committergravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2024-03-26 13:48:07+00:00
logb8d114a29e341a0cbd4c22cf02cb1588b0154446
treed20f9c0ac9f6f4307445ecdff3abbfb596d798d4
parent0d8c7ae0078e970af39a3be760f25c51829b44f9
signaturelock-open Commit is signed but in an unrecognized format.

Zcu: use `Value` instead of `TypedValue` when initializing legacy anon decls

Also removes some unnecessary uses of legacy anon decls for constructing the array of test functions for the test runner.

2 files changed, 121 insertions(+), 105 deletions(-)

src/Module.zig+24-50
......@@ -22,7 +22,6 @@ const Compilation = @import("Compilation.zig");
2222const Cache = std.Build.Cache;
2323const Value = @import("Value.zig");
2424const Type = @import("type.zig").Type;
25const TypedValue = @import("TypedValue.zig");
2625const Package = @import("Package.zig");
2726const link = @import("link.zig");
2827const Air = @import("Air.zig");
......@@ -4827,40 +4826,18 @@ pub fn errorSetBits(mod: *Module) u16 {
48274826 return std.math.log2_int_ceil(ErrorInt, mod.error_limit + 1); // +1 for no error
48284827}
48294828
4830pub fn createAnonymousDecl(mod: *Module, block: *Sema.Block, typed_value: TypedValue) !Decl.Index {
4831 const src_decl = mod.declPtr(block.src_decl);
4832 return mod.createAnonymousDeclFromDecl(src_decl, block.namespace, typed_value);
4833}
4834
4835pub fn createAnonymousDeclFromDecl(
4836 mod: *Module,
4837 src_decl: *Decl,
4838 namespace: Namespace.Index,
4839 tv: TypedValue,
4840) !Decl.Index {
4841 const new_decl_index = try mod.allocateNewDecl(namespace, src_decl.src_node);
4842 errdefer mod.destroyDecl(new_decl_index);
4843 const name = try mod.intern_pool.getOrPutStringFmt(mod.gpa, "{}__anon_{d}", .{
4844 src_decl.name.fmt(&mod.intern_pool), @intFromEnum(new_decl_index),
4845 });
4846 try mod.initNewAnonDecl(new_decl_index, src_decl.src_line, tv, name);
4847 return new_decl_index;
4848}
4849
48504829pub fn initNewAnonDecl(
48514830 mod: *Module,
48524831 new_decl_index: Decl.Index,
48534832 src_line: u32,
4854 typed_value: TypedValue,
4833 val: Value,
48554834 name: InternPool.NullTerminatedString,
48564835) Allocator.Error!void {
4857 assert(typed_value.ty.toIntern() == mod.intern_pool.typeOf(typed_value.val.toIntern()));
4858
48594836 const new_decl = mod.declPtr(new_decl_index);
48604837
48614838 new_decl.name = name;
48624839 new_decl.src_line = src_line;
4863 new_decl.val = typed_value.val;
4840 new_decl.val = val;
48644841 new_decl.alignment = .none;
48654842 new_decl.@"linksection" = .none;
48664843 new_decl.has_tv = true;
......@@ -5391,7 +5368,7 @@ pub fn populateTestFunctions(
53915368 const decl = mod.declPtr(decl_index);
53925369 const test_fn_ty = decl.typeOf(mod).slicePtrFieldType(mod).childType(mod);
53935370
5394 const array_decl_index = d: {
5371 const array_anon_decl: InternPool.Key.Ptr.Addr.AnonDecl = array: {
53955372 // Add mod.test_functions to an array decl then make the test_functions
53965373 // decl reference it as a slice.
53975374 const test_fn_vals = try gpa.alloc(InternPool.Index, mod.test_functions.count());
......@@ -5401,21 +5378,20 @@ pub fn populateTestFunctions(
54015378 const test_decl = mod.declPtr(test_decl_index);
54025379 const test_decl_name = try gpa.dupe(u8, ip.stringToSlice(try test_decl.fullyQualifiedName(mod)));
54035380 defer gpa.free(test_decl_name);
5404 const test_name_decl_index = n: {
5405 const test_name_decl_ty = try mod.arrayType(.{
5381 const test_name_anon_decl: InternPool.Key.Ptr.Addr.AnonDecl = n: {
5382 const test_name_ty = try mod.arrayType(.{
54065383 .len = test_decl_name.len,
54075384 .child = .u8_type,
54085385 });
5409 const test_name_decl_index = try mod.createAnonymousDeclFromDecl(decl, decl.src_namespace, .{
5410 .ty = test_name_decl_ty,
5411 .val = Value.fromInterned((try mod.intern(.{ .aggregate = .{
5412 .ty = test_name_decl_ty.toIntern(),
5413 .storage = .{ .bytes = test_decl_name },
5414 } }))),
5415 });
5416 break :n test_name_decl_index;
5386 const test_name_val = try mod.intern(.{ .aggregate = .{
5387 .ty = test_name_ty.toIntern(),
5388 .storage = .{ .bytes = test_decl_name },
5389 } });
5390 break :n .{
5391 .orig_ty = (try mod.singleConstPtrType(test_name_ty)).toIntern(),
5392 .val = test_name_val,
5393 };
54175394 };
5418 try mod.linkerUpdateDecl(test_name_decl_index);
54195395
54205396 const test_fn_fields = .{
54215397 // name
......@@ -5423,7 +5399,7 @@ pub fn populateTestFunctions(
54235399 .ty = .slice_const_u8_type,
54245400 .ptr = try mod.intern(.{ .ptr = .{
54255401 .ty = .manyptr_const_u8_type,
5426 .addr = .{ .decl = test_name_decl_index },
5402 .addr = .{ .anon_decl = test_name_anon_decl },
54275403 } }),
54285404 .len = try mod.intern(.{ .int = .{
54295405 .ty = .usize_type,
......@@ -5447,22 +5423,20 @@ pub fn populateTestFunctions(
54475423 } });
54485424 }
54495425
5450 const array_decl_ty = try mod.arrayType(.{
5426 const array_ty = try mod.arrayType(.{
54515427 .len = test_fn_vals.len,
54525428 .child = test_fn_ty.toIntern(),
54535429 .sentinel = .none,
54545430 });
5455 const array_decl_index = try mod.createAnonymousDeclFromDecl(decl, decl.src_namespace, .{
5456 .ty = array_decl_ty,
5457 .val = Value.fromInterned((try mod.intern(.{ .aggregate = .{
5458 .ty = array_decl_ty.toIntern(),
5459 .storage = .{ .elems = test_fn_vals },
5460 } }))),
5461 });
5462
5463 break :d array_decl_index;
5431 const array_val = try mod.intern(.{ .aggregate = .{
5432 .ty = array_ty.toIntern(),
5433 .storage = .{ .elems = test_fn_vals },
5434 } });
5435 break :array .{
5436 .orig_ty = (try mod.singleConstPtrType(array_ty)).toIntern(),
5437 .val = array_val,
5438 };
54645439 };
5465 try mod.linkerUpdateDecl(array_decl_index);
54665440
54675441 {
54685442 const new_ty = try mod.ptrType(.{
......@@ -5477,7 +5451,7 @@ pub fn populateTestFunctions(
54775451 .ty = new_ty.toIntern(),
54785452 .ptr = try mod.intern(.{ .ptr = .{
54795453 .ty = new_ty.slicePtrFieldType(mod).toIntern(),
5480 .addr = .{ .decl = array_decl_index },
5454 .addr = .{ .anon_decl = array_anon_decl },
54815455 } }),
54825456 .len = (try mod.intValue(Type.usize, mod.test_functions.count())).toIntern(),
54835457 } });
src/Sema.zig+97-55
......@@ -2825,10 +2825,14 @@ fn zirStructDecl(
28252825 });
28262826 errdefer wip_ty.cancel(ip);
28272827
2828 const new_decl_index = try sema.createAnonymousDeclTypeNamed(block, src, .{
2829 .ty = Type.type,
2830 .val = Value.fromInterned(wip_ty.index),
2831 }, small.name_strategy, "struct", inst);
2828 const new_decl_index = try sema.createAnonymousDeclTypeNamed(
2829 block,
2830 src,
2831 Value.fromInterned(wip_ty.index),
2832 small.name_strategy,
2833 "struct",
2834 inst,
2835 );
28322836 mod.declPtr(new_decl_index).owns_tv = true;
28332837 errdefer mod.abortAnonDecl(new_decl_index);
28342838
......@@ -2861,7 +2865,7 @@ fn createAnonymousDeclTypeNamed(
28612865 sema: *Sema,
28622866 block: *Block,
28632867 src: LazySrcLoc,
2864 typed_value: TypedValue,
2868 val: Value,
28652869 name_strategy: Zir.Inst.NameStrategy,
28662870 anon_prefix: []const u8,
28672871 inst: ?Zir.Inst.Index,
......@@ -2887,12 +2891,12 @@ fn createAnonymousDeclTypeNamed(
28872891 const name = mod.intern_pool.getOrPutStringFmt(gpa, "{}__{s}_{d}", .{
28882892 src_decl.name.fmt(&mod.intern_pool), anon_prefix, @intFromEnum(new_decl_index),
28892893 }) catch unreachable;
2890 try mod.initNewAnonDecl(new_decl_index, src_decl.src_line, typed_value, name);
2894 try mod.initNewAnonDecl(new_decl_index, src_decl.src_line, val, name);
28912895 return new_decl_index;
28922896 },
28932897 .parent => {
28942898 const name = mod.declPtr(block.src_decl).name;
2895 try mod.initNewAnonDecl(new_decl_index, src_decl.src_line, typed_value, name);
2899 try mod.initNewAnonDecl(new_decl_index, src_decl.src_line, val, name);
28962900 return new_decl_index;
28972901 },
28982902 .func => {
......@@ -2915,7 +2919,7 @@ fn createAnonymousDeclTypeNamed(
29152919 // function and the name doesn't matter since it will later
29162920 // result in a compile error.
29172921 const arg_val = sema.resolveConstValue(block, .unneeded, arg, undefined) catch
2918 return sema.createAnonymousDeclTypeNamed(block, src, typed_value, .anon, anon_prefix, null);
2922 return sema.createAnonymousDeclTypeNamed(block, src, val, .anon, anon_prefix, null);
29192923
29202924 if (arg_i != 0) try writer.writeByte(',');
29212925 try writer.print("{}", .{arg_val.fmtValue(sema.typeOf(arg), sema.mod)});
......@@ -2928,7 +2932,7 @@ fn createAnonymousDeclTypeNamed(
29282932
29292933 try writer.writeByte(')');
29302934 const name = try mod.intern_pool.getOrPutString(gpa, buf.items);
2931 try mod.initNewAnonDecl(new_decl_index, src_decl.src_line, typed_value, name);
2935 try mod.initNewAnonDecl(new_decl_index, src_decl.src_line, val, name);
29322936 return new_decl_index;
29332937 },
29342938 .dbg_var => {
......@@ -2943,12 +2947,12 @@ fn createAnonymousDeclTypeNamed(
29432947 src_decl.name.fmt(&mod.intern_pool), zir_data[i].str_op.getStr(sema.code),
29442948 });
29452949
2946 try mod.initNewAnonDecl(new_decl_index, src_decl.src_line, typed_value, name);
2950 try mod.initNewAnonDecl(new_decl_index, src_decl.src_line, val, name);
29472951 return new_decl_index;
29482952 },
29492953 else => {},
29502954 };
2951 return sema.createAnonymousDeclTypeNamed(block, src, typed_value, .anon, anon_prefix, null);
2955 return sema.createAnonymousDeclTypeNamed(block, src, val, .anon, anon_prefix, null);
29522956 },
29532957 }
29542958}
......@@ -3048,10 +3052,14 @@ fn zirEnumDecl(
30483052
30493053 errdefer if (!done) wip_ty.cancel(ip);
30503054
3051 const new_decl_index = try sema.createAnonymousDeclTypeNamed(block, src, .{
3052 .ty = Type.type,
3053 .val = Value.fromInterned(wip_ty.index),
3054 }, small.name_strategy, "enum", inst);
3055 const new_decl_index = try sema.createAnonymousDeclTypeNamed(
3056 block,
3057 src,
3058 Value.fromInterned(wip_ty.index),
3059 small.name_strategy,
3060 "enum",
3061 inst,
3062 );
30553063 const new_decl = mod.declPtr(new_decl_index);
30563064 new_decl.owns_tv = true;
30573065 errdefer if (!done) mod.abortAnonDecl(new_decl_index);
......@@ -3315,10 +3323,14 @@ fn zirUnionDecl(
33153323 });
33163324 errdefer wip_ty.cancel(ip);
33173325
3318 const new_decl_index = try sema.createAnonymousDeclTypeNamed(block, src, .{
3319 .ty = Type.type,
3320 .val = Value.fromInterned(wip_ty.index),
3321 }, small.name_strategy, "union", inst);
3326 const new_decl_index = try sema.createAnonymousDeclTypeNamed(
3327 block,
3328 src,
3329 Value.fromInterned(wip_ty.index),
3330 small.name_strategy,
3331 "union",
3332 inst,
3333 );
33223334 mod.declPtr(new_decl_index).owns_tv = true;
33233335 errdefer mod.abortAnonDecl(new_decl_index);
33243336
......@@ -3399,10 +3411,14 @@ fn zirOpaqueDecl(
33993411 };
34003412 errdefer wip_ty.cancel(ip);
34013413
3402 const new_decl_index = try sema.createAnonymousDeclTypeNamed(block, src, .{
3403 .ty = Type.type,
3404 .val = Value.fromInterned(wip_ty.index),
3405 }, small.name_strategy, "opaque", inst);
3414 const new_decl_index = try sema.createAnonymousDeclTypeNamed(
3415 block,
3416 src,
3417 Value.fromInterned(wip_ty.index),
3418 small.name_strategy,
3419 "opaque",
3420 inst,
3421 );
34063422 mod.declPtr(new_decl_index).owns_tv = true;
34073423 errdefer mod.abortAnonDecl(new_decl_index);
34083424
......@@ -3462,10 +3478,14 @@ fn zirErrorSetDecl(
34623478
34633479 const error_set_ty = try mod.errorSetFromUnsortedNames(names.keys());
34643480
3465 const new_decl_index = try sema.createAnonymousDeclTypeNamed(block, src, .{
3466 .ty = Type.type,
3467 .val = error_set_ty.toValue(),
3468 }, name_strategy, "error", inst);
3481 const new_decl_index = try sema.createAnonymousDeclTypeNamed(
3482 block,
3483 src,
3484 error_set_ty.toValue(),
3485 name_strategy,
3486 "error",
3487 inst,
3488 );
34693489 const new_decl = mod.declPtr(new_decl_index);
34703490 new_decl.owns_tv = true;
34713491 errdefer mod.abortAnonDecl(new_decl_index);
......@@ -21490,10 +21510,14 @@ fn zirReify(
2149021510 };
2149121511 errdefer wip_ty.cancel(ip);
2149221512
21493 const new_decl_index = try sema.createAnonymousDeclTypeNamed(block, src, .{
21494 .ty = Type.type,
21495 .val = Value.fromInterned(wip_ty.index),
21496 }, name_strategy, "opaque", inst);
21513 const new_decl_index = try sema.createAnonymousDeclTypeNamed(
21514 block,
21515 src,
21516 Value.fromInterned(wip_ty.index),
21517 name_strategy,
21518 "opaque",
21519 inst,
21520 );
2149721521 mod.declPtr(new_decl_index).owns_tv = true;
2149821522 errdefer mod.abortAnonDecl(new_decl_index);
2149921523
......@@ -21686,10 +21710,14 @@ fn reifyEnum(
2168621710 return sema.fail(block, src, "Type.Enum.tag_type must be an integer type", .{});
2168721711 }
2168821712
21689 const new_decl_index = try sema.createAnonymousDeclTypeNamed(block, src, .{
21690 .ty = Type.type,
21691 .val = Value.fromInterned(wip_ty.index),
21692 }, name_strategy, "enum", inst);
21713 const new_decl_index = try sema.createAnonymousDeclTypeNamed(
21714 block,
21715 src,
21716 Value.fromInterned(wip_ty.index),
21717 name_strategy,
21718 "enum",
21719 inst,
21720 );
2169321721 mod.declPtr(new_decl_index).owns_tv = true;
2169421722 errdefer mod.abortAnonDecl(new_decl_index);
2169521723
......@@ -21829,10 +21857,14 @@ fn reifyUnion(
2182921857 };
2183021858 errdefer wip_ty.cancel(ip);
2183121859
21832 const new_decl_index = try sema.createAnonymousDeclTypeNamed(block, src, .{
21833 .ty = Type.type,
21834 .val = Value.fromInterned(wip_ty.index),
21835 }, name_strategy, "union", inst);
21860 const new_decl_index = try sema.createAnonymousDeclTypeNamed(
21861 block,
21862 src,
21863 Value.fromInterned(wip_ty.index),
21864 name_strategy,
21865 "union",
21866 inst,
21867 );
2183621868 mod.declPtr(new_decl_index).owns_tv = true;
2183721869 errdefer mod.abortAnonDecl(new_decl_index);
2183821870
......@@ -22084,10 +22116,14 @@ fn reifyStruct(
2208422116 .auto => {},
2208522117 };
2208622118
22087 const new_decl_index = try sema.createAnonymousDeclTypeNamed(block, src, .{
22088 .ty = Type.type,
22089 .val = Value.fromInterned(wip_ty.index),
22090 }, name_strategy, "struct", inst);
22119 const new_decl_index = try sema.createAnonymousDeclTypeNamed(
22120 block,
22121 src,
22122 Value.fromInterned(wip_ty.index),
22123 name_strategy,
22124 "struct",
22125 inst,
22126 );
2209122127 mod.declPtr(new_decl_index).owns_tv = true;
2209222128 errdefer mod.abortAnonDecl(new_decl_index);
2209322129
......@@ -26139,9 +26175,10 @@ fn zirBuiltinExtern(
2613926175 const new_decl_index = try mod.allocateNewDecl(sema.owner_decl.src_namespace, sema.owner_decl.src_node);
2614026176 errdefer mod.destroyDecl(new_decl_index);
2614126177 const new_decl = mod.declPtr(new_decl_index);
26142 try mod.initNewAnonDecl(new_decl_index, sema.owner_decl.src_line, .{
26143 .ty = Type.fromInterned(ptr_info.child),
26144 .val = Value.fromInterned(
26178 try mod.initNewAnonDecl(
26179 new_decl_index,
26180 sema.owner_decl.src_line,
26181 Value.fromInterned(
2614526182 if (Type.fromInterned(ptr_info.child).zigTypeTag(mod) == .Fn)
2614626183 try ip.getExternFunc(sema.gpa, .{
2614726184 .ty = ptr_info.child,
......@@ -26160,7 +26197,8 @@ fn zirBuiltinExtern(
2616026197 .is_weak_linkage = options.linkage == .weak,
2616126198 } }),
2616226199 ),
26163 }, options.name);
26200 options.name,
26201 );
2616426202 new_decl.owns_tv = true;
2616526203 // Note that this will queue the anon decl for codegen, so that the backend can
2616626204 // correctly handle the extern, including duplicate detection.
......@@ -37381,10 +37419,12 @@ fn generateUnionTagTypeNumbered(
3738137419 errdefer mod.destroyDecl(new_decl_index);
3738237420 const fqn = try union_owner_decl.fullyQualifiedName(mod);
3738337421 const name = try ip.getOrPutStringFmt(gpa, "@typeInfo({}).Union.tag_type.?", .{fqn.fmt(ip)});
37384 try mod.initNewAnonDecl(new_decl_index, src_decl.src_line, .{
37385 .ty = Type.noreturn,
37386 .val = Value.@"unreachable",
37387 }, name);
37422 try mod.initNewAnonDecl(
37423 new_decl_index,
37424 src_decl.src_line,
37425 Value.@"unreachable",
37426 name,
37427 );
3738837428 errdefer mod.abortAnonDecl(new_decl_index);
3738937429
3739037430 const new_decl = mod.declPtr(new_decl_index);
......@@ -37425,10 +37465,12 @@ fn generateUnionTagTypeSimple(
3742537465 const new_decl_index = try mod.allocateNewDecl(block.namespace, src_decl.src_node);
3742637466 errdefer mod.destroyDecl(new_decl_index);
3742737467 const name = try ip.getOrPutStringFmt(gpa, "@typeInfo({}).Union.tag_type.?", .{fqn.fmt(ip)});
37428 try mod.initNewAnonDecl(new_decl_index, src_decl.src_line, .{
37429 .ty = Type.noreturn,
37430 .val = Value.@"unreachable",
37431 }, name);
37468 try mod.initNewAnonDecl(
37469 new_decl_index,
37470 src_decl.src_line,
37471 Value.@"unreachable",
37472 name,
37473 );
3743237474 mod.declPtr(new_decl_index).name_fully_qualified = true;
3743337475 break :new_decl_index new_decl_index;
3743437476 };