Commit 9cd0ca9f48

Jacob Young <jacobly0@users.noreply.github.com>
2023-05-26 22:04:53
Module: rename functions to make ownership checks explicit
This makes the difference between `decl.getOwnedFunction` and `decl.val.getFunction` more clear when reading the code.
1 parent d5f0ee0
src/arch/wasm/CodeGen.zig
@@ -2210,7 +2210,7 @@ fn airCall(func: *CodeGen, inst: Air.Inst.Index, modifier: std.builtin.CallModif
             try func.bin_file.addOrUpdateImport(
                 mem.sliceTo(ext_decl.name, 0),
                 atom.getSymbolIndex().?,
-                mod.intern_pool.stringToSliceUnwrap(ext_decl.getExternFunc(mod).?.lib_name),
+                mod.intern_pool.stringToSliceUnwrap(ext_decl.getOwnedExternFunc(mod).?.lib_name),
                 type_index,
             );
             break :blk extern_func.decl;
src/codegen/c.zig
@@ -549,12 +549,12 @@ pub const DeclGen = struct {
         }
 
         // Chase function values in order to be able to reference the original function.
-        if (decl.getFunction(mod)) |func| if (func.owner_decl != decl_index)
+        if (decl.val.getFunction(mod)) |func| if (func.owner_decl != decl_index)
             return dg.renderDeclValue(writer, ty, val, func.owner_decl, location);
-        if (decl.getExternFunc(mod)) |extern_func| if (extern_func.decl != decl_index)
+        if (decl.val.getExternFunc(mod)) |extern_func| if (extern_func.decl != decl_index)
             return dg.renderDeclValue(writer, ty, val, extern_func.decl, location);
 
-        if (decl.getVariable(mod)) |variable| try dg.renderFwdDecl(decl_index, variable);
+        if (decl.val.getVariable(mod)) |variable| try dg.renderFwdDecl(decl_index, variable);
 
         // We shouldn't cast C function pointers as this is UB (when you call
         // them).  The analysis until now should ensure that the C function
@@ -1580,7 +1580,7 @@ pub const DeclGen = struct {
                 else => unreachable,
             }
         }
-        if (fn_decl.getFunction(mod)) |func| if (func.is_cold) try w.writeAll("zig_cold ");
+        if (fn_decl.val.getFunction(mod)) |func| if (func.is_cold) try w.writeAll("zig_cold ");
         if (fn_info.return_type == .noreturn_type) try w.writeAll("zig_noreturn ");
 
         const trailing = try renderTypePrefix(
@@ -2740,13 +2740,13 @@ pub fn genDecl(o: *Object) !void {
     const tv: TypedValue = .{ .ty = decl.ty, .val = decl.val };
 
     if (!tv.ty.isFnOrHasRuntimeBitsIgnoreComptime(mod)) return;
-    if (decl.getExternFunc(mod)) |_| {
+    if (tv.val.getExternFunc(mod)) |_| {
         const fwd_decl_writer = o.dg.fwd_decl.writer();
         try fwd_decl_writer.writeAll("zig_extern ");
         try o.dg.renderFunctionSignature(fwd_decl_writer, decl_c_value.decl, .forward, .{ .export_index = 0 });
         try fwd_decl_writer.writeAll(";\n");
         try genExports(o);
-    } else if (decl.getVariable(mod)) |variable| {
+    } else if (tv.val.getVariable(mod)) |variable| {
         try o.dg.renderFwdDecl(decl_c_value.decl, variable);
         try genExports(o);
 
src/codegen/llvm.zig
@@ -1165,7 +1165,7 @@ pub const Object = struct {
             di_file = try dg.object.getDIFile(gpa, mod.namespacePtr(decl.src_namespace).file_scope);
 
             const line_number = decl.src_line + 1;
-            const is_internal_linkage = decl.getExternFunc(mod) == null and
+            const is_internal_linkage = decl.val.getExternFunc(mod) == null and
                 !mod.decl_exports.contains(decl_index);
             const noret_bit: c_uint = if (fn_info.return_type == .noreturn_type)
                 llvm.DIFlags.NoReturn
@@ -1274,7 +1274,7 @@ pub const Object = struct {
             var free_decl_name = false;
             const decl_name = decl_name: {
                 if (mod.getTarget().isWasm() and try decl.isFunction(mod)) {
-                    if (mod.intern_pool.stringToSliceUnwrap(decl.getExternFunc(mod).?.lib_name)) |lib_name| {
+                    if (mod.intern_pool.stringToSliceUnwrap(decl.getOwnedExternFunc(mod).?.lib_name)) |lib_name| {
                         if (!std.mem.eql(u8, lib_name, "c")) {
                             free_decl_name = true;
                             break :decl_name try std.fmt.allocPrintZ(gpa, "{s}|{s}", .{ decl.name, lib_name });
@@ -1306,7 +1306,7 @@ pub const Object = struct {
                     di_global.replaceLinkageName(linkage_name);
                 }
             }
-            if (decl.getVariable(mod)) |variable| {
+            if (decl.val.getVariable(mod)) |variable| {
                 if (variable.is_threadlocal) {
                     llvm_global.setThreadLocalMode(.GeneralDynamicTLSModel);
                 } else {
@@ -1348,7 +1348,7 @@ pub const Object = struct {
                 defer gpa.free(section_z);
                 llvm_global.setSection(section_z);
             }
-            if (decl.getVariable(mod)) |variable| {
+            if (decl.val.getVariable(mod)) |variable| {
                 if (variable.is_threadlocal) {
                     llvm_global.setThreadLocalMode(.GeneralDynamicTLSModel);
                 }
@@ -1382,7 +1382,7 @@ pub const Object = struct {
             llvm_global.setLinkage(.Internal);
             if (mod.wantDllExports()) llvm_global.setDLLStorageClass(.Default);
             llvm_global.setUnnamedAddr(.True);
-            if (decl.getVariable(mod)) |variable| {
+            if (decl.val.getVariable(mod)) |variable| {
                 const single_threaded = mod.comp.bin_file.options.single_threaded;
                 if (variable.is_threadlocal and !single_threaded) {
                     llvm_global.setThreadLocalMode(.GeneralDynamicTLSModel);
@@ -2452,7 +2452,7 @@ pub const DeclGen = struct {
         log.debug("gen: {s} type: {}, value: {}", .{
             decl.name, decl.ty.fmtDebug(), decl.val.fmtDebug(),
         });
-        if (decl.getExternFunc(mod)) |extern_func| {
+        if (decl.val.getExternFunc(mod)) |extern_func| {
             _ = try dg.resolveLlvmFunction(extern_func.decl);
         } else {
             const target = mod.getTarget();
@@ -2460,7 +2460,7 @@ pub const DeclGen = struct {
             global.setAlignment(decl.getAlignment(mod));
             if (decl.@"linksection") |section| global.setSection(section);
             assert(decl.has_tv);
-            const init_val = if (decl.getVariable(mod)) |variable| init_val: {
+            const init_val = if (decl.val.getVariable(mod)) |variable| init_val: {
                 break :init_val variable.init.toValue();
             } else init_val: {
                 global.setGlobalConstant(.True);
@@ -2555,7 +2555,7 @@ pub const DeclGen = struct {
         } else {
             if (target.isWasm()) {
                 dg.addFnAttrString(llvm_fn, "wasm-import-name", std.mem.sliceTo(decl.name, 0));
-                if (mod.intern_pool.stringToSliceUnwrap(decl.getExternFunc(mod).?.lib_name)) |lib_name| {
+                if (mod.intern_pool.stringToSliceUnwrap(decl.getOwnedExternFunc(mod).?.lib_name)) |lib_name| {
                     if (!std.mem.eql(u8, lib_name, "c")) {
                         dg.addFnAttrString(llvm_fn, "wasm-import-module", lib_name);
                     }
@@ -2716,7 +2716,7 @@ pub const DeclGen = struct {
             llvm_global.setValueName(decl.name);
             llvm_global.setUnnamedAddr(.False);
             llvm_global.setLinkage(.External);
-            if (decl.getVariable(mod)) |variable| {
+            if (decl.val.getVariable(mod)) |variable| {
                 const single_threaded = mod.comp.bin_file.options.single_threaded;
                 if (variable.is_threadlocal and !single_threaded) {
                     llvm_global.setThreadLocalMode(.GeneralDynamicTLSModel);
@@ -3993,11 +3993,11 @@ pub const DeclGen = struct {
         // ... &bar;
         // `bar` is just an alias and we actually want to lower a reference to `foo`.
         const decl = mod.declPtr(decl_index);
-        if (decl.getFunction(mod)) |func| {
+        if (decl.val.getFunction(mod)) |func| {
             if (func.owner_decl != decl_index) {
                 return self.lowerDeclRefValue(tv, func.owner_decl);
             }
-        } else if (decl.getExternFunc(mod)) |func| {
+        } else if (decl.val.getExternFunc(mod)) |func| {
             if (func.decl != decl_index) {
                 return self.lowerDeclRefValue(tv, func.decl);
             }
@@ -7939,7 +7939,7 @@ pub const FuncGen = struct {
             }
 
             const src_index = self.air.instructions.items(.data)[inst].arg.src_index;
-            const func = self.dg.decl.getFunction(mod).?;
+            const func = self.dg.decl.getOwnedFunction(mod).?;
             const lbrace_line = mod.declPtr(func.owner_decl).src_line + func.lbrace_line + 1;
             const lbrace_col = func.lbrace_column + 1;
             const di_local_var = dib.createParameterVariable(
src/codegen/spirv.zig
@@ -261,7 +261,7 @@ pub const DeclGen = struct {
         const entry = try self.decl_link.getOrPut(decl_index);
         if (!entry.found_existing) {
             // TODO: Extern fn?
-            const kind: SpvModule.DeclKind = if (decl.getFunctionIndex(self.module) != .none)
+            const kind: SpvModule.DeclKind = if (decl.val.getFunctionIndex(self.module) != .none)
                 .func
             else
                 .global;
@@ -1544,7 +1544,7 @@ pub const DeclGen = struct {
         const decl_id = self.spv.declPtr(spv_decl_index).result_id;
         log.debug("genDecl: id = {}, index = {}, name = {s}", .{ decl_id.id, @enumToInt(spv_decl_index), decl.name });
 
-        if (decl.getFunction(mod)) |_| {
+        if (decl.val.getFunction(mod)) |_| {
             assert(decl.ty.zigTypeTag(mod) == .Fn);
             const prototype_id = try self.resolveTypeId(decl.ty);
             try self.func.prologue.emit(self.spv.gpa, .OpFunction, .{
@@ -1597,7 +1597,7 @@ pub const DeclGen = struct {
                 try self.generateTestEntryPoint(fqn, spv_decl_index);
             }
         } else {
-            const init_val = if (decl.getVariable(mod)) |payload|
+            const init_val = if (decl.val.getVariable(mod)) |payload|
                 payload.init.toValue()
             else
                 decl.val;
src/link/Coff.zig
@@ -1156,10 +1156,10 @@ pub fn updateDecl(
 
     const decl = mod.declPtr(decl_index);
 
-    if (decl.getExternFunc(mod)) |_| {
+    if (decl.val.getExternFunc(mod)) |_| {
         return; // TODO Should we do more when front-end analyzed extern decl?
     }
-    if (decl.getVariable(mod)) |variable| {
+    if (decl.val.getVariable(mod)) |variable| {
         if (variable.is_extern) {
             return; // TODO Should we do more when front-end analyzed extern decl?
         }
@@ -1172,7 +1172,7 @@ pub fn updateDecl(
     var code_buffer = std.ArrayList(u8).init(self.base.allocator);
     defer code_buffer.deinit();
 
-    const decl_val = if (decl.getVariable(mod)) |variable| variable.init.toValue() else decl.val;
+    const decl_val = if (decl.val.getVariable(mod)) |variable| variable.init.toValue() else decl.val;
     const res = try codegen.generateSymbol(&self.base, decl.srcLoc(mod), .{
         .ty = decl.ty,
         .val = decl_val,
@@ -1313,7 +1313,7 @@ fn getDeclOutputSection(self: *Coff, decl_index: Module.Decl.Index) u16 {
             // TODO: what if this is a function pointer?
             .Fn => break :blk self.text_section_index.?,
             else => {
-                if (decl.getVariable(mod)) |_| {
+                if (val.getVariable(mod)) |_| {
                     break :blk self.data_section_index.?;
                 }
                 break :blk self.rdata_section_index.?;
@@ -1425,7 +1425,7 @@ pub fn updateDeclExports(
         // detect the default subsystem.
         for (exports) |exp| {
             const exported_decl = mod.declPtr(exp.exported_decl);
-            if (exported_decl.getFunctionIndex(mod) == .none) continue;
+            if (exported_decl.getOwnedFunctionIndex(mod) == .none) continue;
             const winapi_cc = switch (self.base.options.target.cpu.arch) {
                 .x86 => std.builtin.CallingConvention.Stdcall,
                 else => std.builtin.CallingConvention.C,
src/link/Dwarf.zig
@@ -971,7 +971,7 @@ pub fn initDeclState(self: *Dwarf, mod: *Module, decl_index: Module.Decl.Index)
             // For functions we need to add a prologue to the debug line program.
             try dbg_line_buffer.ensureTotalCapacity(26);
 
-            const func = decl.getFunction(mod).?;
+            const func = decl.val.getFunction(mod).?;
             log.debug("decl.src_line={d}, func.lbrace_line={d}, func.rbrace_line={d}", .{
                 decl.src_line,
                 func.lbrace_line,
@@ -1523,7 +1523,7 @@ pub fn updateDeclLineNumber(self: *Dwarf, mod: *Module, decl_index: Module.Decl.
     if (atom.len == 0) return;
 
     const decl = mod.declPtr(decl_index);
-    const func = decl.getFunction(mod).?;
+    const func = decl.val.getFunction(mod).?;
     log.debug("decl.src_line={d}, func.lbrace_line={d}, func.rbrace_line={d}", .{
         decl.src_line,
         func.lbrace_line,
src/link/Elf.zig
@@ -2465,7 +2465,7 @@ fn getDeclShdrIndex(self: *Elf, decl_index: Module.Decl.Index) u16 {
             // TODO: what if this is a function pointer?
             .Fn => break :blk self.text_section_index.?,
             else => {
-                if (decl.getVariable(mod)) |_| {
+                if (val.getVariable(mod)) |_| {
                     break :blk self.data_section_index.?;
                 }
                 break :blk self.rodata_section_index.?;
@@ -2647,10 +2647,10 @@ pub fn updateDecl(
 
     const decl = mod.declPtr(decl_index);
 
-    if (decl.getExternFunc(mod)) |_| {
+    if (decl.val.getExternFunc(mod)) |_| {
         return; // TODO Should we do more when front-end analyzed extern decl?
     }
-    if (decl.getVariable(mod)) |variable| {
+    if (decl.val.getVariable(mod)) |variable| {
         if (variable.is_extern) {
             return; // TODO Should we do more when front-end analyzed extern decl?
         }
@@ -2667,7 +2667,7 @@ pub fn updateDecl(
     defer if (decl_state) |*ds| ds.deinit();
 
     // TODO implement .debug_info for global variables
-    const decl_val = if (decl.getVariable(mod)) |variable| variable.init.toValue() else decl.val;
+    const decl_val = if (decl.val.getVariable(mod)) |variable| variable.init.toValue() else decl.val;
     const res = if (decl_state) |*ds|
         try codegen.generateSymbol(&self.base, decl.srcLoc(mod), .{
             .ty = decl.ty,
src/link/MachO.zig
@@ -1984,16 +1984,16 @@ pub fn updateDecl(self: *MachO, mod: *Module, decl_index: Module.Decl.Index) !vo
 
     const decl = mod.declPtr(decl_index);
 
-    if (decl.getExternFunc(mod)) |_| {
+    if (decl.val.getExternFunc(mod)) |_| {
         return; // TODO Should we do more when front-end analyzed extern decl?
     }
-    if (decl.getVariable(mod)) |variable| {
+    if (decl.val.getVariable(mod)) |variable| {
         if (variable.is_extern) {
             return; // TODO Should we do more when front-end analyzed extern decl?
         }
     }
 
-    const is_threadlocal = if (decl.getVariable(mod)) |variable|
+    const is_threadlocal = if (decl.val.getVariable(mod)) |variable|
         variable.is_threadlocal and !self.base.options.single_threaded
     else
         false;
@@ -2012,7 +2012,7 @@ pub fn updateDecl(self: *MachO, mod: *Module, decl_index: Module.Decl.Index) !vo
         null;
     defer if (decl_state) |*ds| ds.deinit();
 
-    const decl_val = if (decl.getVariable(mod)) |variable| variable.init.toValue() else decl.val;
+    const decl_val = if (decl.val.getVariable(mod)) |variable| variable.init.toValue() else decl.val;
     const res = if (decl_state) |*ds|
         try codegen.generateSymbol(&self.base, decl.srcLoc(mod), .{
             .ty = decl.ty,
@@ -2177,7 +2177,7 @@ fn updateThreadlocalVariable(self: *MachO, module: *Module, decl_index: Module.D
 
     const decl = module.declPtr(decl_index);
     const decl_metadata = self.decls.get(decl_index).?;
-    const decl_val = decl.getVariable(mod).?.init.toValue();
+    const decl_val = decl.val.getVariable(mod).?.init.toValue();
     const res = if (decl_state) |*ds|
         try codegen.generateSymbol(&self.base, decl.srcLoc(mod), .{
             .ty = decl.ty,
@@ -2278,7 +2278,7 @@ fn getDeclOutputSection(self: *MachO, decl_index: Module.Decl.Index) u8 {
             }
         }
 
-        if (decl.getVariable(mod)) |variable| {
+        if (val.getVariable(mod)) |variable| {
             if (variable.is_threadlocal and !single_threaded) {
                 break :blk self.thread_data_section_index.?;
             }
@@ -2289,7 +2289,7 @@ fn getDeclOutputSection(self: *MachO, decl_index: Module.Decl.Index) u8 {
             // TODO: what if this is a function pointer?
             .Fn => break :blk self.text_section_index.?,
             else => {
-                if (decl.getVariable(mod)) |_| {
+                if (val.getVariable(mod)) |_| {
                     break :blk self.data_section_index.?;
                 }
                 break :blk self.data_const_section_index.?;
src/link/Plan9.zig
@@ -392,10 +392,10 @@ pub fn lowerUnnamedConst(self: *Plan9, tv: TypedValue, decl_index: Module.Decl.I
 pub fn updateDecl(self: *Plan9, mod: *Module, decl_index: Module.Decl.Index) !void {
     const decl = mod.declPtr(decl_index);
 
-    if (decl.getExternFunc(mod)) |_| {
+    if (decl.val.getExternFunc(mod)) |_| {
         return; // TODO Should we do more when front-end analyzed extern decl?
     }
-    if (decl.getVariable(mod)) |variable| {
+    if (decl.val.getVariable(mod)) |variable| {
         if (variable.is_extern) {
             return; // TODO Should we do more when front-end analyzed extern decl?
         }
@@ -407,7 +407,7 @@ pub fn updateDecl(self: *Plan9, mod: *Module, decl_index: Module.Decl.Index) !vo
 
     var code_buffer = std.ArrayList(u8).init(self.base.allocator);
     defer code_buffer.deinit();
-    const decl_val = if (decl.getVariable(mod)) |variable| variable.init.toValue() else decl.val;
+    const decl_val = if (decl.val.getVariable(mod)) |variable| variable.init.toValue() else decl.val;
     // TODO we need the symbol index for symbol in the table of locals for the containing atom
     const res = try codegen.generateSymbol(&self.base, decl.srcLoc(mod), .{
         .ty = decl.ty,
@@ -771,7 +771,7 @@ pub fn freeDecl(self: *Plan9, decl_index: Module.Decl.Index) void {
     // in the deleteUnusedDecl function.
     const mod = self.base.options.module.?;
     const decl = mod.declPtr(decl_index);
-    const is_fn = decl.getFunctionIndex(mod) != .none;
+    const is_fn = decl.val.getFunctionIndex(mod) != .none;
     if (is_fn) {
         var symidx_and_submap = self.fn_decl_table.get(decl.getFileScope(mod)).?;
         var submap = symidx_and_submap.functions;
src/link/SpirV.zig
@@ -138,7 +138,7 @@ pub fn updateDeclExports(
     exports: []const *Module.Export,
 ) !void {
     const decl = mod.declPtr(decl_index);
-    if (decl.getFunctionIndex(mod) != .none and decl.ty.fnCallingConvention(mod) == .Kernel) {
+    if (decl.val.getFunctionIndex(mod) != .none and decl.ty.fnCallingConvention(mod) == .Kernel) {
         // TODO: Unify with resolveDecl in spirv.zig.
         const entry = try self.decl_link.getOrPut(decl_index);
         if (!entry.found_existing) {
src/link/Wasm.zig
@@ -1404,9 +1404,9 @@ pub fn updateDecl(wasm: *Wasm, mod: *Module, decl_index: Module.Decl.Index) !voi
     defer tracy.end();
 
     const decl = mod.declPtr(decl_index);
-    if (decl.getFunction(mod)) |_| {
+    if (decl.val.getFunction(mod)) |_| {
         return;
-    } else if (decl.getExternFunc(mod)) |_| {
+    } else if (decl.val.getExternFunc(mod)) |_| {
         return;
     }
 
@@ -1415,12 +1415,12 @@ pub fn updateDecl(wasm: *Wasm, mod: *Module, decl_index: Module.Decl.Index) !voi
     atom.clear();
 
     if (decl.isExtern(mod)) {
-        const variable = decl.getVariable(mod).?;
+        const variable = decl.getOwnedVariable(mod).?;
         const name = mem.sliceTo(decl.name, 0);
         const lib_name = mod.intern_pool.stringToSliceUnwrap(variable.lib_name);
         return wasm.addOrUpdateImport(name, atom.sym_index, lib_name, null);
     }
-    const val = if (decl.getVariable(mod)) |variable| variable.init.toValue() else decl.val;
+    const val = if (decl.val.getVariable(mod)) |variable| variable.init.toValue() else decl.val;
 
     var code_writer = std.ArrayList(u8).init(wasm.base.allocator);
     defer code_writer.deinit();
@@ -3373,7 +3373,7 @@ pub fn flushModule(wasm: *Wasm, comp: *Compilation, prog_node: *std.Progress.Nod
             const atom = wasm.getAtomPtr(atom_index);
             if (decl.ty.zigTypeTag(mod) == .Fn) {
                 try wasm.parseAtom(atom_index, .function);
-            } else if (decl.getVariable(mod)) |variable| {
+            } else if (decl.getOwnedVariable(mod)) |variable| {
                 if (variable.is_const) {
                     try wasm.parseAtom(atom_index, .{ .data = .read_only });
                 } else if (variable.init.toValue().isUndefDeep(mod)) {
src/Module.zig
@@ -613,7 +613,7 @@ pub const Decl = struct {
 
     pub fn clearValues(decl: *Decl, mod: *Module) void {
         const gpa = mod.gpa;
-        if (decl.getFunctionIndex(mod).unwrap()) |func| {
+        if (decl.getOwnedFunctionIndex(mod).unwrap()) |func| {
             _ = mod.align_stack_fns.remove(func);
             if (mod.funcPtr(func).comptime_args != null) {
                 _ = mod.monomorphed_funcs.removeContext(func, .{ .mod = mod });
@@ -772,52 +772,52 @@ pub const Decl = struct {
         return tv.ty.zigTypeTag(mod) == .Fn;
     }
 
-    /// If the Decl has a value and it is a struct, return it,
+    /// If the Decl owns its value and it is a struct, return it,
     /// otherwise null.
-    pub fn getStruct(decl: Decl, mod: *Module) ?*Struct {
-        return mod.structPtrUnwrap(decl.getStructIndex(mod));
+    pub fn getOwnedStruct(decl: Decl, mod: *Module) ?*Struct {
+        return mod.structPtrUnwrap(decl.getOwnedStructIndex(mod));
     }
 
-    pub fn getStructIndex(decl: Decl, mod: *Module) Struct.OptionalIndex {
+    pub fn getOwnedStructIndex(decl: Decl, mod: *Module) Struct.OptionalIndex {
         if (!decl.owns_tv) return .none;
         if (decl.val.ip_index == .none) return .none;
         return mod.intern_pool.indexToStructType(decl.val.toIntern());
     }
 
-    /// If the Decl has a value and it is a union, return it,
+    /// If the Decl owns its value and it is a union, return it,
     /// otherwise null.
-    pub fn getUnion(decl: Decl, mod: *Module) ?*Union {
+    pub fn getOwnedUnion(decl: Decl, mod: *Module) ?*Union {
         if (!decl.owns_tv) return null;
         if (decl.val.ip_index == .none) return null;
         return mod.typeToUnion(decl.val.toType());
     }
 
-    /// If the Decl has a value and it is a function, return it,
+    /// If the Decl owns its value and it is a function, return it,
     /// otherwise null.
-    pub fn getFunction(decl: Decl, mod: *Module) ?*Fn {
-        return mod.funcPtrUnwrap(decl.getFunctionIndex(mod));
+    pub fn getOwnedFunction(decl: Decl, mod: *Module) ?*Fn {
+        return mod.funcPtrUnwrap(decl.getOwnedFunctionIndex(mod));
     }
 
-    pub fn getFunctionIndex(decl: Decl, mod: *Module) Fn.OptionalIndex {
+    pub fn getOwnedFunctionIndex(decl: Decl, mod: *Module) Fn.OptionalIndex {
         return if (decl.owns_tv) decl.val.getFunctionIndex(mod) else .none;
     }
 
-    /// If the Decl has a value and it is an extern function, returns it,
+    /// If the Decl owns its value and it is an extern function, returns it,
     /// otherwise null.
-    pub fn getExternFunc(decl: Decl, mod: *Module) ?InternPool.Key.ExternFunc {
+    pub fn getOwnedExternFunc(decl: Decl, mod: *Module) ?InternPool.Key.ExternFunc {
         return if (decl.owns_tv) decl.val.getExternFunc(mod) else null;
     }
 
-    /// If the Decl has a value and it is a variable, returns it,
+    /// If the Decl owns its value and it is a variable, returns it,
     /// otherwise null.
-    pub fn getVariable(decl: Decl, mod: *Module) ?InternPool.Key.Variable {
+    pub fn getOwnedVariable(decl: Decl, mod: *Module) ?InternPool.Key.Variable {
         return if (decl.owns_tv) decl.val.getVariable(mod) else null;
     }
 
     /// Gets the namespace that this Decl creates by being a struct, union,
     /// enum, or opaque.
     /// Only returns it if the Decl is the owner.
-    pub fn getInnerNamespaceIndex(decl: Decl, mod: *Module) Namespace.OptionalIndex {
+    pub fn getOwnedInnerNamespaceIndex(decl: Decl, mod: *Module) Namespace.OptionalIndex {
         if (!decl.owns_tv) return .none;
         return switch (decl.val.ip_index) {
             .empty_struct_type => .none,
@@ -833,8 +833,8 @@ pub const Decl = struct {
     }
 
     /// Same as `getInnerNamespaceIndex` but additionally obtains the pointer.
-    pub fn getInnerNamespace(decl: Decl, mod: *Module) ?*Namespace {
-        return if (decl.getInnerNamespaceIndex(mod).unwrap()) |i| mod.namespacePtr(i) else null;
+    pub fn getOwnedInnerNamespace(decl: Decl, mod: *Module) ?*Namespace {
+        return mod.namespacePtrUnwrap(decl.getOwnedInnerNamespaceIndex(mod));
     }
 
     pub fn dump(decl: *Decl) void {
@@ -3361,7 +3361,7 @@ pub fn destroyDecl(mod: *Module, decl_index: Decl.Index) void {
             gpa.free(kv.value);
         }
         if (decl.has_tv) {
-            if (decl.getInnerNamespaceIndex(mod).unwrap()) |i| {
+            if (decl.getOwnedInnerNamespaceIndex(mod).unwrap()) |i| {
                 mod.namespacePtr(i).destroyDecls(mod);
                 mod.destroyNamespace(i);
             }
@@ -3407,6 +3407,10 @@ pub fn inferredErrorSetPtr(mod: *Module, index: Fn.InferredErrorSet.Index) *Fn.I
     return mod.intern_pool.inferredErrorSetPtr(index);
 }
 
+pub fn namespacePtrUnwrap(mod: *Module, index: Namespace.OptionalIndex) ?*Namespace {
+    return mod.namespacePtr(index.unwrap() orelse return null);
+}
+
 /// This one accepts an index from the InternPool and asserts that it is not
 /// the anonymous empty struct type.
 pub fn structPtrUnwrap(mod: *Module, index: Struct.OptionalIndex) ?*Struct {
@@ -3873,28 +3877,28 @@ fn updateZirRefs(mod: *Module, file: *File, old_zir: Zir) !void {
 
         if (!decl.owns_tv) continue;
 
-        if (decl.getStruct(mod)) |struct_obj| {
+        if (decl.getOwnedStruct(mod)) |struct_obj| {
             struct_obj.zir_index = inst_map.get(struct_obj.zir_index) orelse {
                 try file.deleted_decls.append(gpa, decl_index);
                 continue;
             };
         }
 
-        if (decl.getUnion(mod)) |union_obj| {
+        if (decl.getOwnedUnion(mod)) |union_obj| {
             union_obj.zir_index = inst_map.get(union_obj.zir_index) orelse {
                 try file.deleted_decls.append(gpa, decl_index);
                 continue;
             };
         }
 
-        if (decl.getFunction(mod)) |func| {
+        if (decl.getOwnedFunction(mod)) |func| {
             func.zir_body_inst = inst_map.get(func.zir_body_inst) orelse {
                 try file.deleted_decls.append(gpa, decl_index);
                 continue;
             };
         }
 
-        if (decl.getInnerNamespace(mod)) |namespace| {
+        if (decl.getOwnedInnerNamespace(mod)) |namespace| {
             for (namespace.decls.keys()) |sub_decl| {
                 try decl_stack.append(gpa, sub_decl);
             }
@@ -4074,7 +4078,7 @@ pub fn ensureDeclAnalyzed(mod: *Module, decl_index: Decl.Index) SemaError!void {
             try mod.deleteDeclExports(decl_index);
 
             // Similarly, `@setAlignStack` invocations will be re-discovered.
-            if (decl.getFunctionIndex(mod).unwrap()) |func| {
+            if (decl.getOwnedFunctionIndex(mod).unwrap()) |func| {
                 _ = mod.align_stack_fns.remove(func);
             }
 
@@ -4577,7 +4581,7 @@ fn semaDecl(mod: *Module, decl_index: Decl.Index) !bool {
     if (mod.declIsRoot(decl_index)) {
         log.debug("semaDecl root {*} ({s})", .{ decl, decl.name });
         const main_struct_inst = Zir.main_struct_inst;
-        const struct_index = decl.getStructIndex(mod).unwrap().?;
+        const struct_index = decl.getOwnedStructIndex(mod).unwrap().?;
         const struct_obj = mod.structPtr(struct_index);
         // This might not have gotten set in `semaFile` if the first time had
         // a ZIR failure, so we set it here in case.
@@ -4659,7 +4663,7 @@ fn semaDecl(mod: *Module, decl_index: Decl.Index) !bool {
             if (decl.has_tv) {
                 prev_type_has_bits = decl.ty.isFnOrHasRuntimeBits(mod);
                 type_changed = !decl.ty.eql(decl_tv.ty, mod);
-                if (decl.getFunction(mod)) |prev_func| {
+                if (decl.getOwnedFunction(mod)) |prev_func| {
                     prev_is_inline = prev_func.state == .inline_only;
                 }
             }
@@ -5313,7 +5317,7 @@ fn scanDecl(iter: *ScanDeclIter, decl_sub_index: usize, flags: u4) Allocator.Err
     decl.has_align = has_align;
     decl.has_linksection_or_addrspace = has_linksection_or_addrspace;
     decl.zir_decl_index = @intCast(u32, decl_sub_index);
-    if (decl.getFunctionIndex(mod) != .none) {
+    if (decl.getOwnedFunctionIndex(mod) != .none) {
         switch (comp.bin_file.tag) {
             .coff, .elf, .macho, .plan9 => {
                 // TODO Look into detecting when this would be unnecessary by storing enough state
@@ -5390,7 +5394,7 @@ pub fn clearDecl(
         if (decl.ty.isFnOrHasRuntimeBits(mod)) {
             mod.comp.bin_file.freeDecl(decl_index);
         }
-        if (decl.getInnerNamespace(mod)) |namespace| {
+        if (decl.getOwnedInnerNamespace(mod)) |namespace| {
             try namespace.deleteAllDecls(mod, outdated_decls);
         }
     }
@@ -5733,10 +5737,8 @@ fn markOutdatedDecl(mod: *Module, decl_index: Decl.Index) !void {
     if (mod.cimport_errors.fetchSwapRemove(decl_index)) |kv| {
         for (kv.value) |err| err.deinit(mod.gpa);
     }
-    if (decl.has_tv and decl.owns_tv) {
-        if (decl.getFunctionIndex(mod).unwrap()) |func| {
-            _ = mod.align_stack_fns.remove(func);
-        }
+    if (decl.getOwnedFunctionIndex(mod).unwrap()) |func| {
+        _ = mod.align_stack_fns.remove(func);
     }
     if (mod.emit_h) |emit_h| {
         if (emit_h.failed_decls.fetchSwapRemove(decl_index)) |kv| {
src/Sema.zig
@@ -5730,7 +5730,7 @@ fn zirExport(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void
     {
         try mod.ensureDeclAnalyzed(decl_index);
         const exported_decl = mod.declPtr(decl_index);
-        if (exported_decl.getFunction(mod)) |function| {
+        if (exported_decl.val.getFunction(mod)) |function| {
             return sema.analyzeExport(block, src, options, function.owner_decl);
         }
     }
@@ -6206,7 +6206,7 @@ fn funcDeclSrc(sema: *Sema, func_inst: Air.Inst.Ref) !?*Decl {
         .extern_func => |extern_func| extern_func.decl,
         .func => |func| mod.funcPtr(func.index).owner_decl,
         .ptr => |ptr| switch (ptr.addr) {
-            .decl => |decl| decl,
+            .decl => |decl| mod.declPtr(decl).val.getFunction(mod).?.owner_decl,
             else => return null,
         },
         else => return null,
@@ -6782,7 +6782,7 @@ fn analyzeCall(
             }),
             .func => |function| function.index,
             .ptr => |ptr| switch (ptr.addr) {
-                .decl => |decl| mod.declPtr(decl).getFunctionIndex(mod).unwrap().?,
+                .decl => |decl| mod.declPtr(decl).val.getFunctionIndex(mod).unwrap().?,
                 else => {
                     assert(callee_ty.isPtrAtRuntime(mod));
                     return sema.fail(block, call_src, "{s} call of function pointer", .{
@@ -7403,7 +7403,7 @@ fn instantiateGenericCall(
     const func_val = try sema.resolveConstValue(block, func_src, func, "generic function being called must be comptime-known");
     const module_fn = mod.funcPtr(switch (mod.intern_pool.indexToKey(func_val.toIntern())) {
         .func => |function| function.index,
-        .ptr => |ptr| mod.declPtr(ptr.addr.decl).getFunctionIndex(mod).unwrap().?,
+        .ptr => |ptr| mod.declPtr(ptr.addr.decl).val.getFunctionIndex(mod).unwrap().?,
         else => unreachable,
     });
     // Check the Module's generic function map with an adapted context, so that we
@@ -28336,7 +28336,7 @@ fn beginComptimePtrLoad(
                 const is_mutable = ptr.addr == .mut_decl;
                 const decl = mod.declPtr(decl_index);
                 const decl_tv = try decl.typedValue();
-                if (decl.getVariable(mod) != null) return error.RuntimeLoad;
+                if (decl.val.getVariable(mod) != null) return error.RuntimeLoad;
 
                 const layout_defined = decl.ty.hasWellDefinedLayout(mod);
                 break :blk ComptimePtrLoadKit{
@@ -29423,7 +29423,7 @@ fn analyzeDeclRefInner(sema: *Sema, decl_index: Decl.Index, analyze_fn_body: boo
     const ptr_ty = try mod.ptrType(.{
         .elem_type = decl_tv.ty.toIntern(),
         .alignment = InternPool.Alignment.fromByteUnits(decl.@"align"),
-        .is_const = if (decl.getVariable(mod)) |variable| variable.is_const else false,
+        .is_const = if (decl.val.getVariable(mod)) |variable| variable.is_const else false,
         .address_space = decl.@"addrspace",
     });
     if (analyze_fn_body) {