Zcu.Decl: remove ty field

`Decl` can no longer store un-interned values, so this field is now
unnecessary. The type can instead be fetched with the new `typeOf`
helper method, which just gets the type of the Decl's `Value`.
This commit is contained in:
mlugg
2024-03-25 19:02:21 +00:00
parent 341857e5cd
commit c6f3e9d79c
23 changed files with 120 additions and 123 deletions

View File

@@ -657,7 +657,7 @@ pub const DeclGen = struct {
assert(decl.has_tv);
// Render an undefined pointer if we have a pointer to a zero-bit or comptime type.
if (ty.isPtrAtRuntime(mod) and !decl.ty.isFnOrHasRuntimeBits(mod)) {
if (ty.isPtrAtRuntime(mod) and !decl.typeOf(mod).isFnOrHasRuntimeBits(mod)) {
return dg.writeCValue(writer, .{ .undef = ty });
}
@@ -673,7 +673,7 @@ pub const DeclGen = struct {
// them). The analysis until now should ensure that the C function
// pointers are compatible. If they are not, then there is a bug
// somewhere and we should let the C compiler tell us about it.
const need_typecast = if (ty.castPtrToFn(mod)) |_| false else !ty.childType(mod).eql(decl.ty, mod);
const need_typecast = if (ty.castPtrToFn(mod)) |_| false else !ty.childType(mod).eql(decl.typeOf(mod), mod);
if (need_typecast) {
try writer.writeAll("((");
try dg.renderType(writer, ty);
@@ -1588,9 +1588,10 @@ pub const DeclGen = struct {
const ip = &mod.intern_pool;
const fn_decl = mod.declPtr(fn_decl_index);
const fn_cty_idx = try dg.typeToIndex(fn_decl.ty, kind);
const fn_ty = fn_decl.typeOf(mod);
const fn_cty_idx = try dg.typeToIndex(fn_ty, kind);
const fn_info = mod.typeToFunc(fn_decl.ty).?;
const fn_info = mod.typeToFunc(fn_ty).?;
if (fn_info.cc == .Naked) {
switch (kind) {
.forward => try w.writeAll("zig_naked_decl "),
@@ -1971,7 +1972,7 @@ pub const DeclGen = struct {
) !void {
const decl = dg.module.declPtr(decl_index);
const fwd = dg.fwdDeclWriter();
const is_global = variable.is_extern or dg.declIsGlobal(.{ .ty = decl.ty, .val = decl.val });
const is_global = variable.is_extern or dg.declIsGlobal(.{ .ty = decl.typeOf(dg.module), .val = decl.val });
try fwd.writeAll(if (is_global) "zig_extern " else "static ");
const maybe_exports = dg.module.decl_exports.get(decl_index);
const export_weak_linkage = if (maybe_exports) |exports|
@@ -1982,7 +1983,7 @@ pub const DeclGen = struct {
if (variable.is_threadlocal) try fwd.writeAll("zig_threadlocal ");
try dg.renderTypeAndName(
fwd,
decl.ty,
decl.typeOf(dg.module),
.{ .decl = decl_index },
CQualifiers.init(.{ .@"const" = variable.is_const }),
decl.alignment,
@@ -2656,7 +2657,7 @@ fn genExports(o: *Object) !void {
.anon, .flush => return,
};
const decl = mod.declPtr(decl_index);
const tv: TypedValue = .{ .ty = decl.ty, .val = Value.fromInterned((try decl.internValue(mod))) };
const tv: TypedValue = .{ .ty = decl.typeOf(mod), .val = Value.fromInterned((try decl.internValue(mod))) };
const fwd = o.dg.fwdDeclWriter();
const exports = mod.decl_exports.get(decl_index) orelse return;
@@ -2687,7 +2688,7 @@ fn genExports(o: *Object) !void {
const export_name = ip.stringToSlice(@"export".opts.name);
try o.dg.renderTypeAndName(
fwd,
decl.ty,
decl.typeOf(mod),
.{ .identifier = export_name },
CQualifiers.init(.{ .@"const" = is_variable_const }),
decl.alignment,
@@ -2769,7 +2770,7 @@ pub fn genLazyFn(o: *Object, lazy_fn: LazyFnMap.Entry) !void {
},
.never_tail, .never_inline => |fn_decl_index| {
const fn_decl = mod.declPtr(fn_decl_index);
const fn_cty = try o.dg.typeToCType(fn_decl.ty, .complete);
const fn_cty = try o.dg.typeToCType(fn_decl.typeOf(mod), .complete);
const fn_info = fn_cty.cast(CType.Payload.Function).?.data;
const fwd_decl_writer = o.dg.fwdDeclWriter();
@@ -2806,7 +2807,7 @@ pub fn genFunc(f: *Function) !void {
const decl_index = o.dg.pass.decl;
const decl = mod.declPtr(decl_index);
const tv: TypedValue = .{
.ty = decl.ty,
.ty = decl.typeOf(mod),
.val = decl.val,
};
@@ -2893,7 +2894,7 @@ pub fn genDecl(o: *Object) !void {
const mod = o.dg.module;
const decl_index = o.dg.pass.decl;
const decl = mod.declPtr(decl_index);
const tv: TypedValue = .{ .ty = decl.ty, .val = Value.fromInterned((try decl.internValue(mod))) };
const tv: TypedValue = .{ .ty = decl.typeOf(mod), .val = Value.fromInterned((try decl.internValue(mod))) };
if (!tv.ty.isFnOrHasRuntimeBitsIgnoreComptime(mod)) return;
if (tv.val.getExternFunc(mod)) |_| {
@@ -2979,7 +2980,7 @@ pub fn genHeader(dg: *DeclGen) error{ AnalysisFail, OutOfMemory }!void {
const decl_index = dg.pass.decl;
const decl = mod.declPtr(decl_index);
const tv: TypedValue = .{
.ty = decl.ty,
.ty = decl.typeOf(mod),
.val = decl.val,
};
const writer = dg.fwdDeclWriter();
@@ -7392,7 +7393,7 @@ fn airCVaStart(f: *Function, inst: Air.Inst.Index) !CValue {
const inst_ty = f.typeOfIndex(inst);
const decl_index = f.object.dg.pass.decl;
const decl = mod.declPtr(decl_index);
const fn_cty = try f.typeToCType(decl.ty, .complete);
const fn_cty = try f.typeToCType(decl.typeOf(mod), .complete);
const param_len = fn_cty.castTag(.varargs_function).?.data.param_types.len;
const writer = f.object.writer();

View File

@@ -1384,7 +1384,7 @@ pub const Object = struct {
const decl = zcu.declPtr(decl_index);
const namespace = zcu.namespacePtr(decl.src_namespace);
const owner_mod = namespace.file_scope.mod;
const fn_info = zcu.typeToFunc(decl.ty).?;
const fn_info = zcu.typeToFunc(decl.typeOf(zcu)).?;
const target = zcu.getTarget();
const ip = &zcu.intern_pool;
@@ -1659,7 +1659,7 @@ pub const Object = struct {
const line_number = decl.src_line + 1;
const is_internal_linkage = decl.val.getExternFunc(zcu) == null and
!zcu.decl_exports.contains(decl_index);
const debug_decl_type = try o.lowerDebugType(decl.ty);
const debug_decl_type = try o.lowerDebugType(decl.typeOf(zcu));
const subprogram = try o.builder.debugSubprogram(
file,
@@ -2881,7 +2881,7 @@ pub const Object = struct {
const decl = zcu.declPtr(decl_index);
const namespace = zcu.namespacePtr(decl.src_namespace);
const owner_mod = namespace.file_scope.mod;
const zig_fn_type = decl.ty;
const zig_fn_type = decl.typeOf(zcu);
const gop = try o.decl_map.getOrPut(gpa, decl_index);
if (gop.found_existing) return gop.value_ptr.ptr(&o.builder).kind.function;
@@ -3112,7 +3112,7 @@ pub const Object = struct {
try o.builder.strtabString(mod.intern_pool.stringToSlice(
if (is_extern) decl.name else try decl.fullyQualifiedName(mod),
)),
try o.lowerType(decl.ty),
try o.lowerType(decl.typeOf(mod)),
toLlvmGlobalAddressSpace(decl.@"addrspace", mod.getTarget()),
);
gop.value_ptr.* = variable_index.ptrConst(&o.builder).global;
@@ -4263,7 +4263,7 @@ pub const Object = struct {
const mod = o.module;
const decl = mod.declPtr(decl_index);
try mod.markDeclAlive(decl);
const ptr_ty = try mod.singleMutPtrType(decl.ty);
const ptr_ty = try mod.singleMutPtrType(decl.typeOf(mod));
return o.lowerDeclRefValue(ptr_ty, decl_index);
}
@@ -4450,9 +4450,10 @@ pub const Object = struct {
}
}
const is_fn_body = decl.ty.zigTypeTag(mod) == .Fn;
if ((!is_fn_body and !decl.ty.hasRuntimeBits(mod)) or
(is_fn_body and mod.typeToFunc(decl.ty).?.is_generic)) return o.lowerPtrToVoid(ty);
const decl_ty = decl.typeOf(mod);
const is_fn_body = decl_ty.zigTypeTag(mod) == .Fn;
if ((!is_fn_body and !decl_ty.hasRuntimeBits(mod)) or
(is_fn_body and mod.typeToFunc(decl_ty).?.is_generic)) return o.lowerPtrToVoid(ty);
try mod.markDeclAlive(decl);
@@ -4740,7 +4741,7 @@ pub const DeclGen = struct {
debug_file, // File
debug_file, // Scope
line_number,
try o.lowerDebugType(decl.ty),
try o.lowerDebugType(decl.typeOf(zcu)),
variable_index,
.{ .local = is_internal_linkage },
);
@@ -5530,7 +5531,7 @@ pub const FuncGen = struct {
const mod = o.module;
const msg_decl_index = mod.panic_messages[@intFromEnum(panic_id)].unwrap().?;
const msg_decl = mod.declPtr(msg_decl_index);
const msg_len = msg_decl.ty.childType(mod).arrayLen(mod);
const msg_len = msg_decl.typeOf(mod).childType(mod).arrayLen(mod);
const msg_ptr = try o.lowerValue(try msg_decl.internValue(mod));
const null_opt_addr_global = try fg.resolveNullOptUsize();
const target = mod.getTarget();
@@ -5544,7 +5545,7 @@ pub const FuncGen = struct {
// )
const panic_func = mod.funcInfo(mod.panic_func_index);
const panic_decl = mod.declPtr(panic_func.owner_decl);
const fn_info = mod.typeToFunc(panic_decl.ty).?;
const fn_info = mod.typeToFunc(panic_decl.typeOf(mod)).?;
const panic_global = try o.resolveLlvmFunction(panic_func.owner_decl);
_ = try fg.wip.call(
.normal,
@@ -5612,7 +5613,7 @@ pub const FuncGen = struct {
_ = try self.wip.retVoid();
return .none;
}
const fn_info = mod.typeToFunc(self.dg.decl.ty).?;
const fn_info = mod.typeToFunc(self.dg.decl.typeOf(mod)).?;
if (!ret_ty.hasRuntimeBitsIgnoreComptime(mod)) {
if (Type.fromInterned(fn_info.return_type).isError(mod)) {
// Functions with an empty error set are emitted with an error code
@@ -5674,7 +5675,7 @@ pub const FuncGen = struct {
const un_op = self.air.instructions.items(.data)[@intFromEnum(inst)].un_op;
const ptr_ty = self.typeOf(un_op);
const ret_ty = ptr_ty.childType(mod);
const fn_info = mod.typeToFunc(self.dg.decl.ty).?;
const fn_info = mod.typeToFunc(self.dg.decl.typeOf(mod)).?;
if (!ret_ty.hasRuntimeBitsIgnoreComptime(mod)) {
if (Type.fromInterned(fn_info.return_type).isError(mod)) {
// Functions with an empty error set are emitted with an error code

View File

@@ -1221,7 +1221,7 @@ const DeclGen = struct {
else => {},
}
if (!decl.ty.isFnOrHasRuntimeBitsIgnoreComptime(mod)) {
if (!decl.typeOf(mod).isFnOrHasRuntimeBitsIgnoreComptime(mod)) {
// Pointer to nothing - return undefined.
return self.spv.constUndef(ty_ref);
}
@@ -1237,7 +1237,7 @@ const DeclGen = struct {
const final_storage_class = self.spvStorageClass(decl.@"addrspace");
try self.addFunctionDep(spv_decl_index, final_storage_class);
const decl_ptr_ty_ref = try self.ptrType(decl.ty, final_storage_class);
const decl_ptr_ty_ref = try self.ptrType(decl.typeOf(mod), final_storage_class);
const ptr_id = switch (final_storage_class) {
.Generic => try self.castToGeneric(self.typeId(decl_ptr_ty_ref), decl_id),
@@ -2044,11 +2044,11 @@ const DeclGen = struct {
switch (self.spv.declPtr(spv_decl_index).kind) {
.func => {
assert(decl.ty.zigTypeTag(mod) == .Fn);
const fn_info = mod.typeToFunc(decl.ty).?;
assert(decl.typeOf(mod).zigTypeTag(mod) == .Fn);
const fn_info = mod.typeToFunc(decl.typeOf(mod)).?;
const return_ty_ref = try self.resolveFnReturnType(Type.fromInterned(fn_info.return_type));
const prototype_ty_ref = try self.resolveType(decl.ty, .direct);
const prototype_ty_ref = try self.resolveType(decl.typeOf(mod), .direct);
try self.func.prologue.emit(self.spv.gpa, .OpFunction, .{
.id_result_type = self.typeId(return_ty_ref),
.id_result = result_id,
@@ -2121,7 +2121,7 @@ const DeclGen = struct {
const final_storage_class = self.spvStorageClass(decl.@"addrspace");
assert(final_storage_class != .Generic); // These should be instance globals
const ptr_ty_ref = try self.ptrType(decl.ty, final_storage_class);
const ptr_ty_ref = try self.ptrType(decl.typeOf(mod), final_storage_class);
try self.spv.sections.types_globals_constants.emit(self.spv.gpa, .OpVariable, .{
.id_result_type = self.typeId(ptr_ty_ref),
@@ -2144,7 +2144,7 @@ const DeclGen = struct {
try self.spv.declareDeclDeps(spv_decl_index, &.{});
const ptr_ty_ref = try self.ptrType(decl.ty, .Function);
const ptr_ty_ref = try self.ptrType(decl.typeOf(mod), .Function);
if (maybe_init_val) |init_val| {
// TODO: Combine with resolveAnonDecl?
@@ -2168,7 +2168,7 @@ const DeclGen = struct {
});
self.current_block_label = root_block_id;
const val_id = try self.constant(decl.ty, init_val, .indirect);
const val_id = try self.constant(decl.typeOf(mod), init_val, .indirect);
try self.func.body.emit(self.spv.gpa, .OpStore, .{
.pointer = result_id,
.object = val_id,
@@ -4785,7 +4785,7 @@ const DeclGen = struct {
const mod = self.module;
if (!ret_ty.hasRuntimeBitsIgnoreComptime(mod)) {
const decl = mod.declPtr(self.decl_index);
const fn_info = mod.typeToFunc(decl.ty).?;
const fn_info = mod.typeToFunc(decl.typeOf(mod)).?;
if (Type.fromInterned(fn_info.return_type).isError(mod)) {
// Functions with an empty error set are emitted with an error code
// return type and return zero so they can be function pointers coerced
@@ -4810,7 +4810,7 @@ const DeclGen = struct {
if (!ret_ty.hasRuntimeBitsIgnoreComptime(mod)) {
const decl = mod.declPtr(self.decl_index);
const fn_info = mod.typeToFunc(decl.ty).?;
const fn_info = mod.typeToFunc(decl.typeOf(mod)).?;
if (Type.fromInterned(fn_info.return_type).isError(mod)) {
// Functions with an empty error set are emitted with an error code
// return type and return zero so they can be function pointers coerced