authorgravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2024-03-26 04:19:50+00:00
committergravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2024-03-26 13:48:07+00:00
log0d8c7ae0078e970af39a3be760f25c51829b44f9
tree1d83b176db748416d4c0c9621c2c1141401dfd5c
parent920f2c7794ba9b3eb3f3b78c60a0caf544f68927
signaturelock-open Commit is signed but in an unrecognized format.

Zcu.Decl: replace `typedValue` with `valueOrFail`

Now that the legacy `Value` representation is eliminated, we can begin to phase out the redundant `TypedValue` type.

3 files changed, 18 insertions(+), 28 deletions(-)

src/Module.zig+4-10
...@@ -480,17 +480,11 @@ pub const Decl = struct {...@@ -480,17 +480,11 @@ pub const Decl = struct {
480 return decl.val.typeOf(zcu);480 return decl.val.typeOf(zcu);
481 }481 }
482482
483 pub fn typedValue(decl: Decl, zcu: *const Zcu) error{AnalysisFail}!TypedValue {483 /// Small wrapper for Sema to use over direct access to the `val` field.
484 /// If the value is not populated, instead returns `error.AnalysisFail`.
485 pub fn valueOrFail(decl: Decl) error{AnalysisFail}!Value {
484 if (!decl.has_tv) return error.AnalysisFail;486 if (!decl.has_tv) return error.AnalysisFail;
485 return .{487 return decl.val;
486 .ty = decl.typeOf(zcu),
487 .val = decl.val,
488 };
489 }
490
491 pub fn isFunction(decl: Decl, zcu: *const Zcu) !bool {
492 const tv = try decl.typedValue(zcu);
493 return tv.ty.zigTypeTag(zcu) == .Fn;
494 }488 }
495489
496 /// If the Decl owns its value and it is a struct, return it,490 /// If the Decl owns its value and it is a struct, return it,
src/Sema.zig+13-17
...@@ -26660,13 +26660,12 @@ fn prepareSimplePanic(sema: *Sema, block: *Block) !void {...@@ -26660,13 +26660,12 @@ fn prepareSimplePanic(sema: *Sema, block: *Block) !void {
26660 // decl_index may be an alias; we must find the decl that actually26660 // decl_index may be an alias; we must find the decl that actually
26661 // owns the function.26661 // owns the function.
26662 try sema.ensureDeclAnalyzed(decl_index);26662 try sema.ensureDeclAnalyzed(decl_index);
26663 const tv = try mod.declPtr(decl_index).typedValue(mod);26663 const fn_val = try mod.declPtr(decl_index).valueOrFail();
26664 try sema.declareDependency(.{ .decl_val = decl_index });26664 try sema.declareDependency(.{ .decl_val = decl_index });
26665 assert(tv.ty.zigTypeTag(mod) == .Fn);26665 assert(fn_val.typeOf(mod).zigTypeTag(mod) == .Fn);
26666 assert(try sema.fnHasRuntimeBits(tv.ty));26666 assert(try sema.fnHasRuntimeBits(fn_val.typeOf(mod)));
26667 const func_index = tv.val.toIntern();26667 try mod.ensureFuncBodyAnalysisQueued(fn_val.toIntern());
26668 try mod.ensureFuncBodyAnalysisQueued(func_index);26668 mod.panic_func_index = fn_val.toIntern();
26669 mod.panic_func_index = func_index;
26670 }26669 }
2667126670
26672 if (mod.null_stack_trace == .none) {26671 if (mod.null_stack_trace == .none) {
...@@ -32449,8 +32448,8 @@ fn analyzeDeclRefInner(sema: *Sema, decl_index: InternPool.DeclIndex, analyze_fn...@@ -32449,8 +32448,8 @@ fn analyzeDeclRefInner(sema: *Sema, decl_index: InternPool.DeclIndex, analyze_fn
32449 const mod = sema.mod;32448 const mod = sema.mod;
32450 try sema.ensureDeclAnalyzed(decl_index);32449 try sema.ensureDeclAnalyzed(decl_index);
3245132450
32452 const decl_tv = try mod.declPtr(decl_index).typedValue(mod);32451 const decl_val = try mod.declPtr(decl_index).valueOrFail();
32453 const owner_decl = mod.declPtr(switch (mod.intern_pool.indexToKey(decl_tv.val.toIntern())) {32452 const owner_decl = mod.declPtr(switch (mod.intern_pool.indexToKey(decl_val.toIntern())) {
32454 .variable => |variable| variable.decl,32453 .variable => |variable| variable.decl,
32455 .extern_func => |extern_func| extern_func.decl,32454 .extern_func => |extern_func| extern_func.decl,
32456 .func => |func| func.owner_decl,32455 .func => |func| func.owner_decl,
...@@ -32459,10 +32458,10 @@ fn analyzeDeclRefInner(sema: *Sema, decl_index: InternPool.DeclIndex, analyze_fn...@@ -32459,10 +32458,10 @@ fn analyzeDeclRefInner(sema: *Sema, decl_index: InternPool.DeclIndex, analyze_fn
32459 // TODO: if this is a `decl_ref` of a non-variable decl, only depend on decl type32458 // TODO: if this is a `decl_ref` of a non-variable decl, only depend on decl type
32460 try sema.declareDependency(.{ .decl_val = decl_index });32459 try sema.declareDependency(.{ .decl_val = decl_index });
32461 const ptr_ty = try sema.ptrType(.{32460 const ptr_ty = try sema.ptrType(.{
32462 .child = decl_tv.ty.toIntern(),32461 .child = decl_val.typeOf(mod).toIntern(),
32463 .flags = .{32462 .flags = .{
32464 .alignment = owner_decl.alignment,32463 .alignment = owner_decl.alignment,
32465 .is_const = if (decl_tv.val.getVariable(mod)) |variable| variable.is_const else true,32464 .is_const = if (decl_val.getVariable(mod)) |variable| variable.is_const else true,
32466 .address_space = owner_decl.@"addrspace",32465 .address_space = owner_decl.@"addrspace",
32467 },32466 },
32468 });32467 });
...@@ -32478,12 +32477,10 @@ fn analyzeDeclRefInner(sema: *Sema, decl_index: InternPool.DeclIndex, analyze_fn...@@ -32478,12 +32477,10 @@ fn analyzeDeclRefInner(sema: *Sema, decl_index: InternPool.DeclIndex, analyze_fn
32478fn maybeQueueFuncBodyAnalysis(sema: *Sema, decl_index: InternPool.DeclIndex) !void {32477fn maybeQueueFuncBodyAnalysis(sema: *Sema, decl_index: InternPool.DeclIndex) !void {
32479 const mod = sema.mod;32478 const mod = sema.mod;
32480 const decl = mod.declPtr(decl_index);32479 const decl = mod.declPtr(decl_index);
32481 const tv = try decl.typedValue(mod);32480 const decl_val = try decl.valueOrFail();
32482 if (tv.ty.zigTypeTag(mod) != .Fn) return;32481 if (!mod.intern_pool.isFuncBody(decl_val.toIntern())) return;
32483 if (!try sema.fnHasRuntimeBits(tv.ty)) return;32482 if (!try sema.fnHasRuntimeBits(decl_val.typeOf(mod))) return;
32484 const func_index = tv.val.toIntern();32483 try mod.ensureFuncBodyAnalysisQueued(decl_val.toIntern());
32485 if (!mod.intern_pool.isFuncBody(func_index)) return; // undef or extern function
32486 try mod.ensureFuncBodyAnalysisQueued(func_index);
32487}32484}
3248832485
32489fn analyzeRef(32486fn analyzeRef(
...@@ -39049,7 +39046,6 @@ fn sliceToIpString(...@@ -39049,7 +39046,6 @@ fn sliceToIpString(
39049 reason: NeededComptimeReason,39046 reason: NeededComptimeReason,
39050) CompileError!InternPool.NullTerminatedString {39047) CompileError!InternPool.NullTerminatedString {
39051 const zcu = sema.mod;39048 const zcu = sema.mod;
39052 const ip = &zcu.intern_pool;
39053 const slice_ty = slice_val.typeOf(zcu);39049 const slice_ty = slice_val.typeOf(zcu);
39054 assert(slice_ty.isSlice(zcu));39050 assert(slice_ty.isSlice(zcu));
39055 assert(slice_ty.childType(zcu).toIntern() == .u8_type);39051 assert(slice_ty.childType(zcu).toIntern() == .u8_type);
src/codegen/llvm.zig+1-1
...@@ -1762,7 +1762,7 @@ pub const Object = struct {...@@ -1762,7 +1762,7 @@ pub const Object = struct {
1762 const decl_name = decl_name: {1762 const decl_name = decl_name: {
1763 const decl_name = mod.intern_pool.stringToSlice(decl.name);1763 const decl_name = mod.intern_pool.stringToSlice(decl.name);
17641764
1765 if (mod.getTarget().isWasm() and try decl.isFunction(mod)) {1765 if (mod.getTarget().isWasm() and decl.val.typeOf(mod).zigTypeTag(mod) == .Fn) {
1766 if (mod.intern_pool.stringToSliceUnwrap(decl.getOwnedExternFunc(mod).?.lib_name)) |lib_name| {1766 if (mod.intern_pool.stringToSliceUnwrap(decl.getOwnedExternFunc(mod).?.lib_name)) |lib_name| {
1767 if (!std.mem.eql(u8, lib_name, "c")) {1767 if (!std.mem.eql(u8, lib_name, "c")) {
1768 break :decl_name try self.builder.strtabStringFmt("{s}|{s}", .{ decl_name, lib_name });1768 break :decl_name try self.builder.strtabStringFmt("{s}|{s}", .{ decl_name, lib_name });