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.
This commit is contained in:
mlugg 2024-03-26 04:39:01 +00:00
parent 0d8c7ae007
commit b8d114a29e
No known key found for this signature in database
GPG Key ID: 58978E823BDE3EF9
2 changed files with 121 additions and 105 deletions

View File

@ -22,7 +22,6 @@ const Compilation = @import("Compilation.zig");
const Cache = std.Build.Cache;
const Value = @import("Value.zig");
const Type = @import("type.zig").Type;
const TypedValue = @import("TypedValue.zig");
const Package = @import("Package.zig");
const link = @import("link.zig");
const Air = @import("Air.zig");
@ -4827,40 +4826,18 @@ pub fn errorSetBits(mod: *Module) u16 {
return std.math.log2_int_ceil(ErrorInt, mod.error_limit + 1); // +1 for no error
}
pub fn createAnonymousDecl(mod: *Module, block: *Sema.Block, typed_value: TypedValue) !Decl.Index {
const src_decl = mod.declPtr(block.src_decl);
return mod.createAnonymousDeclFromDecl(src_decl, block.namespace, typed_value);
}
pub fn createAnonymousDeclFromDecl(
mod: *Module,
src_decl: *Decl,
namespace: Namespace.Index,
tv: TypedValue,
) !Decl.Index {
const new_decl_index = try mod.allocateNewDecl(namespace, src_decl.src_node);
errdefer mod.destroyDecl(new_decl_index);
const name = try mod.intern_pool.getOrPutStringFmt(mod.gpa, "{}__anon_{d}", .{
src_decl.name.fmt(&mod.intern_pool), @intFromEnum(new_decl_index),
});
try mod.initNewAnonDecl(new_decl_index, src_decl.src_line, tv, name);
return new_decl_index;
}
pub fn initNewAnonDecl(
mod: *Module,
new_decl_index: Decl.Index,
src_line: u32,
typed_value: TypedValue,
val: Value,
name: InternPool.NullTerminatedString,
) Allocator.Error!void {
assert(typed_value.ty.toIntern() == mod.intern_pool.typeOf(typed_value.val.toIntern()));
const new_decl = mod.declPtr(new_decl_index);
new_decl.name = name;
new_decl.src_line = src_line;
new_decl.val = typed_value.val;
new_decl.val = val;
new_decl.alignment = .none;
new_decl.@"linksection" = .none;
new_decl.has_tv = true;
@ -5391,7 +5368,7 @@ pub fn populateTestFunctions(
const decl = mod.declPtr(decl_index);
const test_fn_ty = decl.typeOf(mod).slicePtrFieldType(mod).childType(mod);
const array_decl_index = d: {
const array_anon_decl: InternPool.Key.Ptr.Addr.AnonDecl = array: {
// Add mod.test_functions to an array decl then make the test_functions
// decl reference it as a slice.
const test_fn_vals = try gpa.alloc(InternPool.Index, mod.test_functions.count());
@ -5401,21 +5378,20 @@ pub fn populateTestFunctions(
const test_decl = mod.declPtr(test_decl_index);
const test_decl_name = try gpa.dupe(u8, ip.stringToSlice(try test_decl.fullyQualifiedName(mod)));
defer gpa.free(test_decl_name);
const test_name_decl_index = n: {
const test_name_decl_ty = try mod.arrayType(.{
const test_name_anon_decl: InternPool.Key.Ptr.Addr.AnonDecl = n: {
const test_name_ty = try mod.arrayType(.{
.len = test_decl_name.len,
.child = .u8_type,
});
const test_name_decl_index = try mod.createAnonymousDeclFromDecl(decl, decl.src_namespace, .{
.ty = test_name_decl_ty,
.val = Value.fromInterned((try mod.intern(.{ .aggregate = .{
.ty = test_name_decl_ty.toIntern(),
.storage = .{ .bytes = test_decl_name },
} }))),
});
break :n test_name_decl_index;
const test_name_val = try mod.intern(.{ .aggregate = .{
.ty = test_name_ty.toIntern(),
.storage = .{ .bytes = test_decl_name },
} });
break :n .{
.orig_ty = (try mod.singleConstPtrType(test_name_ty)).toIntern(),
.val = test_name_val,
};
};
try mod.linkerUpdateDecl(test_name_decl_index);
const test_fn_fields = .{
// name
@ -5423,7 +5399,7 @@ pub fn populateTestFunctions(
.ty = .slice_const_u8_type,
.ptr = try mod.intern(.{ .ptr = .{
.ty = .manyptr_const_u8_type,
.addr = .{ .decl = test_name_decl_index },
.addr = .{ .anon_decl = test_name_anon_decl },
} }),
.len = try mod.intern(.{ .int = .{
.ty = .usize_type,
@ -5447,22 +5423,20 @@ pub fn populateTestFunctions(
} });
}
const array_decl_ty = try mod.arrayType(.{
const array_ty = try mod.arrayType(.{
.len = test_fn_vals.len,
.child = test_fn_ty.toIntern(),
.sentinel = .none,
});
const array_decl_index = try mod.createAnonymousDeclFromDecl(decl, decl.src_namespace, .{
.ty = array_decl_ty,
.val = Value.fromInterned((try mod.intern(.{ .aggregate = .{
.ty = array_decl_ty.toIntern(),
.storage = .{ .elems = test_fn_vals },
} }))),
});
break :d array_decl_index;
const array_val = try mod.intern(.{ .aggregate = .{
.ty = array_ty.toIntern(),
.storage = .{ .elems = test_fn_vals },
} });
break :array .{
.orig_ty = (try mod.singleConstPtrType(array_ty)).toIntern(),
.val = array_val,
};
};
try mod.linkerUpdateDecl(array_decl_index);
{
const new_ty = try mod.ptrType(.{
@ -5477,7 +5451,7 @@ pub fn populateTestFunctions(
.ty = new_ty.toIntern(),
.ptr = try mod.intern(.{ .ptr = .{
.ty = new_ty.slicePtrFieldType(mod).toIntern(),
.addr = .{ .decl = array_decl_index },
.addr = .{ .anon_decl = array_anon_decl },
} }),
.len = (try mod.intValue(Type.usize, mod.test_functions.count())).toIntern(),
} });

View File

@ -2825,10 +2825,14 @@ fn zirStructDecl(
});
errdefer wip_ty.cancel(ip);
const new_decl_index = try sema.createAnonymousDeclTypeNamed(block, src, .{
.ty = Type.type,
.val = Value.fromInterned(wip_ty.index),
}, small.name_strategy, "struct", inst);
const new_decl_index = try sema.createAnonymousDeclTypeNamed(
block,
src,
Value.fromInterned(wip_ty.index),
small.name_strategy,
"struct",
inst,
);
mod.declPtr(new_decl_index).owns_tv = true;
errdefer mod.abortAnonDecl(new_decl_index);
@ -2861,7 +2865,7 @@ fn createAnonymousDeclTypeNamed(
sema: *Sema,
block: *Block,
src: LazySrcLoc,
typed_value: TypedValue,
val: Value,
name_strategy: Zir.Inst.NameStrategy,
anon_prefix: []const u8,
inst: ?Zir.Inst.Index,
@ -2887,12 +2891,12 @@ fn createAnonymousDeclTypeNamed(
const name = mod.intern_pool.getOrPutStringFmt(gpa, "{}__{s}_{d}", .{
src_decl.name.fmt(&mod.intern_pool), anon_prefix, @intFromEnum(new_decl_index),
}) catch unreachable;
try mod.initNewAnonDecl(new_decl_index, src_decl.src_line, typed_value, name);
try mod.initNewAnonDecl(new_decl_index, src_decl.src_line, val, name);
return new_decl_index;
},
.parent => {
const name = mod.declPtr(block.src_decl).name;
try mod.initNewAnonDecl(new_decl_index, src_decl.src_line, typed_value, name);
try mod.initNewAnonDecl(new_decl_index, src_decl.src_line, val, name);
return new_decl_index;
},
.func => {
@ -2915,7 +2919,7 @@ fn createAnonymousDeclTypeNamed(
// function and the name doesn't matter since it will later
// result in a compile error.
const arg_val = sema.resolveConstValue(block, .unneeded, arg, undefined) catch
return sema.createAnonymousDeclTypeNamed(block, src, typed_value, .anon, anon_prefix, null);
return sema.createAnonymousDeclTypeNamed(block, src, val, .anon, anon_prefix, null);
if (arg_i != 0) try writer.writeByte(',');
try writer.print("{}", .{arg_val.fmtValue(sema.typeOf(arg), sema.mod)});
@ -2928,7 +2932,7 @@ fn createAnonymousDeclTypeNamed(
try writer.writeByte(')');
const name = try mod.intern_pool.getOrPutString(gpa, buf.items);
try mod.initNewAnonDecl(new_decl_index, src_decl.src_line, typed_value, name);
try mod.initNewAnonDecl(new_decl_index, src_decl.src_line, val, name);
return new_decl_index;
},
.dbg_var => {
@ -2943,12 +2947,12 @@ fn createAnonymousDeclTypeNamed(
src_decl.name.fmt(&mod.intern_pool), zir_data[i].str_op.getStr(sema.code),
});
try mod.initNewAnonDecl(new_decl_index, src_decl.src_line, typed_value, name);
try mod.initNewAnonDecl(new_decl_index, src_decl.src_line, val, name);
return new_decl_index;
},
else => {},
};
return sema.createAnonymousDeclTypeNamed(block, src, typed_value, .anon, anon_prefix, null);
return sema.createAnonymousDeclTypeNamed(block, src, val, .anon, anon_prefix, null);
},
}
}
@ -3048,10 +3052,14 @@ fn zirEnumDecl(
errdefer if (!done) wip_ty.cancel(ip);
const new_decl_index = try sema.createAnonymousDeclTypeNamed(block, src, .{
.ty = Type.type,
.val = Value.fromInterned(wip_ty.index),
}, small.name_strategy, "enum", inst);
const new_decl_index = try sema.createAnonymousDeclTypeNamed(
block,
src,
Value.fromInterned(wip_ty.index),
small.name_strategy,
"enum",
inst,
);
const new_decl = mod.declPtr(new_decl_index);
new_decl.owns_tv = true;
errdefer if (!done) mod.abortAnonDecl(new_decl_index);
@ -3315,10 +3323,14 @@ fn zirUnionDecl(
});
errdefer wip_ty.cancel(ip);
const new_decl_index = try sema.createAnonymousDeclTypeNamed(block, src, .{
.ty = Type.type,
.val = Value.fromInterned(wip_ty.index),
}, small.name_strategy, "union", inst);
const new_decl_index = try sema.createAnonymousDeclTypeNamed(
block,
src,
Value.fromInterned(wip_ty.index),
small.name_strategy,
"union",
inst,
);
mod.declPtr(new_decl_index).owns_tv = true;
errdefer mod.abortAnonDecl(new_decl_index);
@ -3399,10 +3411,14 @@ fn zirOpaqueDecl(
};
errdefer wip_ty.cancel(ip);
const new_decl_index = try sema.createAnonymousDeclTypeNamed(block, src, .{
.ty = Type.type,
.val = Value.fromInterned(wip_ty.index),
}, small.name_strategy, "opaque", inst);
const new_decl_index = try sema.createAnonymousDeclTypeNamed(
block,
src,
Value.fromInterned(wip_ty.index),
small.name_strategy,
"opaque",
inst,
);
mod.declPtr(new_decl_index).owns_tv = true;
errdefer mod.abortAnonDecl(new_decl_index);
@ -3462,10 +3478,14 @@ fn zirErrorSetDecl(
const error_set_ty = try mod.errorSetFromUnsortedNames(names.keys());
const new_decl_index = try sema.createAnonymousDeclTypeNamed(block, src, .{
.ty = Type.type,
.val = error_set_ty.toValue(),
}, name_strategy, "error", inst);
const new_decl_index = try sema.createAnonymousDeclTypeNamed(
block,
src,
error_set_ty.toValue(),
name_strategy,
"error",
inst,
);
const new_decl = mod.declPtr(new_decl_index);
new_decl.owns_tv = true;
errdefer mod.abortAnonDecl(new_decl_index);
@ -21490,10 +21510,14 @@ fn zirReify(
};
errdefer wip_ty.cancel(ip);
const new_decl_index = try sema.createAnonymousDeclTypeNamed(block, src, .{
.ty = Type.type,
.val = Value.fromInterned(wip_ty.index),
}, name_strategy, "opaque", inst);
const new_decl_index = try sema.createAnonymousDeclTypeNamed(
block,
src,
Value.fromInterned(wip_ty.index),
name_strategy,
"opaque",
inst,
);
mod.declPtr(new_decl_index).owns_tv = true;
errdefer mod.abortAnonDecl(new_decl_index);
@ -21686,10 +21710,14 @@ fn reifyEnum(
return sema.fail(block, src, "Type.Enum.tag_type must be an integer type", .{});
}
const new_decl_index = try sema.createAnonymousDeclTypeNamed(block, src, .{
.ty = Type.type,
.val = Value.fromInterned(wip_ty.index),
}, name_strategy, "enum", inst);
const new_decl_index = try sema.createAnonymousDeclTypeNamed(
block,
src,
Value.fromInterned(wip_ty.index),
name_strategy,
"enum",
inst,
);
mod.declPtr(new_decl_index).owns_tv = true;
errdefer mod.abortAnonDecl(new_decl_index);
@ -21829,10 +21857,14 @@ fn reifyUnion(
};
errdefer wip_ty.cancel(ip);
const new_decl_index = try sema.createAnonymousDeclTypeNamed(block, src, .{
.ty = Type.type,
.val = Value.fromInterned(wip_ty.index),
}, name_strategy, "union", inst);
const new_decl_index = try sema.createAnonymousDeclTypeNamed(
block,
src,
Value.fromInterned(wip_ty.index),
name_strategy,
"union",
inst,
);
mod.declPtr(new_decl_index).owns_tv = true;
errdefer mod.abortAnonDecl(new_decl_index);
@ -22084,10 +22116,14 @@ fn reifyStruct(
.auto => {},
};
const new_decl_index = try sema.createAnonymousDeclTypeNamed(block, src, .{
.ty = Type.type,
.val = Value.fromInterned(wip_ty.index),
}, name_strategy, "struct", inst);
const new_decl_index = try sema.createAnonymousDeclTypeNamed(
block,
src,
Value.fromInterned(wip_ty.index),
name_strategy,
"struct",
inst,
);
mod.declPtr(new_decl_index).owns_tv = true;
errdefer mod.abortAnonDecl(new_decl_index);
@ -26139,9 +26175,10 @@ fn zirBuiltinExtern(
const new_decl_index = try mod.allocateNewDecl(sema.owner_decl.src_namespace, sema.owner_decl.src_node);
errdefer mod.destroyDecl(new_decl_index);
const new_decl = mod.declPtr(new_decl_index);
try mod.initNewAnonDecl(new_decl_index, sema.owner_decl.src_line, .{
.ty = Type.fromInterned(ptr_info.child),
.val = Value.fromInterned(
try mod.initNewAnonDecl(
new_decl_index,
sema.owner_decl.src_line,
Value.fromInterned(
if (Type.fromInterned(ptr_info.child).zigTypeTag(mod) == .Fn)
try ip.getExternFunc(sema.gpa, .{
.ty = ptr_info.child,
@ -26160,7 +26197,8 @@ fn zirBuiltinExtern(
.is_weak_linkage = options.linkage == .weak,
} }),
),
}, options.name);
options.name,
);
new_decl.owns_tv = true;
// Note that this will queue the anon decl for codegen, so that the backend can
// correctly handle the extern, including duplicate detection.
@ -37381,10 +37419,12 @@ fn generateUnionTagTypeNumbered(
errdefer mod.destroyDecl(new_decl_index);
const fqn = try union_owner_decl.fullyQualifiedName(mod);
const name = try ip.getOrPutStringFmt(gpa, "@typeInfo({}).Union.tag_type.?", .{fqn.fmt(ip)});
try mod.initNewAnonDecl(new_decl_index, src_decl.src_line, .{
.ty = Type.noreturn,
.val = Value.@"unreachable",
}, name);
try mod.initNewAnonDecl(
new_decl_index,
src_decl.src_line,
Value.@"unreachable",
name,
);
errdefer mod.abortAnonDecl(new_decl_index);
const new_decl = mod.declPtr(new_decl_index);
@ -37425,10 +37465,12 @@ fn generateUnionTagTypeSimple(
const new_decl_index = try mod.allocateNewDecl(block.namespace, src_decl.src_node);
errdefer mod.destroyDecl(new_decl_index);
const name = try ip.getOrPutStringFmt(gpa, "@typeInfo({}).Union.tag_type.?", .{fqn.fmt(ip)});
try mod.initNewAnonDecl(new_decl_index, src_decl.src_line, .{
.ty = Type.noreturn,
.val = Value.@"unreachable",
}, name);
try mod.initNewAnonDecl(
new_decl_index,
src_decl.src_line,
Value.@"unreachable",
name,
);
mod.declPtr(new_decl_index).name_fully_qualified = true;
break :new_decl_index new_decl_index;
};