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 {
480480 return decl.val.typeOf(zcu);
481481 }
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 {
484486 if (!decl.has_tv) return error.AnalysisFail;
485 return .{
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;
487 return decl.val;
494488 }
495489
496490 /// 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 {
2666026660 // decl_index may be an alias; we must find the decl that actually
2666126661 // owns the function.
2666226662 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();
2666426664 try sema.declareDependency(.{ .decl_val = decl_index });
26665 assert(tv.ty.zigTypeTag(mod) == .Fn);
26666 assert(try sema.fnHasRuntimeBits(tv.ty));
26667 const func_index = tv.val.toIntern();
26668 try mod.ensureFuncBodyAnalysisQueued(func_index);
26669 mod.panic_func_index = func_index;
26665 assert(fn_val.typeOf(mod).zigTypeTag(mod) == .Fn);
26666 assert(try sema.fnHasRuntimeBits(fn_val.typeOf(mod)));
26667 try mod.ensureFuncBodyAnalysisQueued(fn_val.toIntern());
26668 mod.panic_func_index = fn_val.toIntern();
2667026669 }
2667126670
2667226671 if (mod.null_stack_trace == .none) {
......@@ -32449,8 +32448,8 @@ fn analyzeDeclRefInner(sema: *Sema, decl_index: InternPool.DeclIndex, analyze_fn
3244932448 const mod = sema.mod;
3245032449 try sema.ensureDeclAnalyzed(decl_index);
3245132450
32452 const decl_tv = try mod.declPtr(decl_index).typedValue(mod);
32453 const owner_decl = mod.declPtr(switch (mod.intern_pool.indexToKey(decl_tv.val.toIntern())) {
32451 const decl_val = try mod.declPtr(decl_index).valueOrFail();
32452 const owner_decl = mod.declPtr(switch (mod.intern_pool.indexToKey(decl_val.toIntern())) {
3245432453 .variable => |variable| variable.decl,
3245532454 .extern_func => |extern_func| extern_func.decl,
3245632455 .func => |func| func.owner_decl,
......@@ -32459,10 +32458,10 @@ fn analyzeDeclRefInner(sema: *Sema, decl_index: InternPool.DeclIndex, analyze_fn
3245932458 // TODO: if this is a `decl_ref` of a non-variable decl, only depend on decl type
3246032459 try sema.declareDependency(.{ .decl_val = decl_index });
3246132460 const ptr_ty = try sema.ptrType(.{
32462 .child = decl_tv.ty.toIntern(),
32461 .child = decl_val.typeOf(mod).toIntern(),
3246332462 .flags = .{
3246432463 .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,
3246632465 .address_space = owner_decl.@"addrspace",
3246732466 },
3246832467 });
......@@ -32478,12 +32477,10 @@ fn analyzeDeclRefInner(sema: *Sema, decl_index: InternPool.DeclIndex, analyze_fn
3247832477fn maybeQueueFuncBodyAnalysis(sema: *Sema, decl_index: InternPool.DeclIndex) !void {
3247932478 const mod = sema.mod;
3248032479 const decl = mod.declPtr(decl_index);
32481 const tv = try decl.typedValue(mod);
32482 if (tv.ty.zigTypeTag(mod) != .Fn) return;
32483 if (!try sema.fnHasRuntimeBits(tv.ty)) return;
32484 const func_index = tv.val.toIntern();
32485 if (!mod.intern_pool.isFuncBody(func_index)) return; // undef or extern function
32486 try mod.ensureFuncBodyAnalysisQueued(func_index);
32480 const decl_val = try decl.valueOrFail();
32481 if (!mod.intern_pool.isFuncBody(decl_val.toIntern())) return;
32482 if (!try sema.fnHasRuntimeBits(decl_val.typeOf(mod))) return;
32483 try mod.ensureFuncBodyAnalysisQueued(decl_val.toIntern());
3248732484}
3248832485
3248932486fn analyzeRef(
......@@ -39049,7 +39046,6 @@ fn sliceToIpString(
3904939046 reason: NeededComptimeReason,
3905039047) CompileError!InternPool.NullTerminatedString {
3905139048 const zcu = sema.mod;
39052 const ip = &zcu.intern_pool;
3905339049 const slice_ty = slice_val.typeOf(zcu);
3905439050 assert(slice_ty.isSlice(zcu));
3905539051 assert(slice_ty.childType(zcu).toIntern() == .u8_type);
src/codegen/llvm.zig+1-1
......@@ -1762,7 +1762,7 @@ pub const Object = struct {
17621762 const decl_name = decl_name: {
17631763 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) {
17661766 if (mod.intern_pool.stringToSliceUnwrap(decl.getOwnedExternFunc(mod).?.lib_name)) |lib_name| {
17671767 if (!std.mem.eql(u8, lib_name, "c")) {
17681768 break :decl_name try self.builder.strtabStringFmt("{s}|{s}", .{ decl_name, lib_name });