authorgravatar for jacobly@ziglang.orgJacob Young <jacobly@ziglang.org> 2023-05-26 16:04:53-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-06-10 20:47:55-07:00
log9cd0ca9f482ef7f76d3f3ca683913e9aceaa47fe
tree86e9cfb93f1cf8b68aebc83877bef7c24aed47a6
parentd5f0ee0d62e48e623625779a0cf722c3f0f66921

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.

13 files changed, 90 insertions(+), 88 deletions(-)

src/Module.zig+34-32
......@@ -613,7 +613,7 @@ pub const Decl = struct {
613613
614614 pub fn clearValues(decl: *Decl, mod: *Module) void {
615615 const gpa = mod.gpa;
616 if (decl.getFunctionIndex(mod).unwrap()) |func| {
616 if (decl.getOwnedFunctionIndex(mod).unwrap()) |func| {
617617 _ = mod.align_stack_fns.remove(func);
618618 if (mod.funcPtr(func).comptime_args != null) {
619619 _ = mod.monomorphed_funcs.removeContext(func, .{ .mod = mod });
......@@ -772,52 +772,52 @@ pub const Decl = struct {
772772 return tv.ty.zigTypeTag(mod) == .Fn;
773773 }
774774
775 /// If the Decl has a value and it is a struct, return it,
775 /// If the Decl owns its value and it is a struct, return it,
776776 /// otherwise null.
777 pub fn getStruct(decl: Decl, mod: *Module) ?*Struct {
778 return mod.structPtrUnwrap(decl.getStructIndex(mod));
777 pub fn getOwnedStruct(decl: Decl, mod: *Module) ?*Struct {
778 return mod.structPtrUnwrap(decl.getOwnedStructIndex(mod));
779779 }
780780
781 pub fn getStructIndex(decl: Decl, mod: *Module) Struct.OptionalIndex {
781 pub fn getOwnedStructIndex(decl: Decl, mod: *Module) Struct.OptionalIndex {
782782 if (!decl.owns_tv) return .none;
783783 if (decl.val.ip_index == .none) return .none;
784784 return mod.intern_pool.indexToStructType(decl.val.toIntern());
785785 }
786786
787 /// If the Decl has a value and it is a union, return it,
787 /// If the Decl owns its value and it is a union, return it,
788788 /// otherwise null.
789 pub fn getUnion(decl: Decl, mod: *Module) ?*Union {
789 pub fn getOwnedUnion(decl: Decl, mod: *Module) ?*Union {
790790 if (!decl.owns_tv) return null;
791791 if (decl.val.ip_index == .none) return null;
792792 return mod.typeToUnion(decl.val.toType());
793793 }
794794
795 /// If the Decl has a value and it is a function, return it,
795 /// If the Decl owns its value and it is a function, return it,
796796 /// otherwise null.
797 pub fn getFunction(decl: Decl, mod: *Module) ?*Fn {
798 return mod.funcPtrUnwrap(decl.getFunctionIndex(mod));
797 pub fn getOwnedFunction(decl: Decl, mod: *Module) ?*Fn {
798 return mod.funcPtrUnwrap(decl.getOwnedFunctionIndex(mod));
799799 }
800800
801 pub fn getFunctionIndex(decl: Decl, mod: *Module) Fn.OptionalIndex {
801 pub fn getOwnedFunctionIndex(decl: Decl, mod: *Module) Fn.OptionalIndex {
802802 return if (decl.owns_tv) decl.val.getFunctionIndex(mod) else .none;
803803 }
804804
805 /// If the Decl has a value and it is an extern function, returns it,
805 /// If the Decl owns its value and it is an extern function, returns it,
806806 /// otherwise null.
807 pub fn getExternFunc(decl: Decl, mod: *Module) ?InternPool.Key.ExternFunc {
807 pub fn getOwnedExternFunc(decl: Decl, mod: *Module) ?InternPool.Key.ExternFunc {
808808 return if (decl.owns_tv) decl.val.getExternFunc(mod) else null;
809809 }
810810
811 /// If the Decl has a value and it is a variable, returns it,
811 /// If the Decl owns its value and it is a variable, returns it,
812812 /// otherwise null.
813 pub fn getVariable(decl: Decl, mod: *Module) ?InternPool.Key.Variable {
813 pub fn getOwnedVariable(decl: Decl, mod: *Module) ?InternPool.Key.Variable {
814814 return if (decl.owns_tv) decl.val.getVariable(mod) else null;
815815 }
816816
817817 /// Gets the namespace that this Decl creates by being a struct, union,
818818 /// enum, or opaque.
819819 /// Only returns it if the Decl is the owner.
820 pub fn getInnerNamespaceIndex(decl: Decl, mod: *Module) Namespace.OptionalIndex {
820 pub fn getOwnedInnerNamespaceIndex(decl: Decl, mod: *Module) Namespace.OptionalIndex {
821821 if (!decl.owns_tv) return .none;
822822 return switch (decl.val.ip_index) {
823823 .empty_struct_type => .none,
......@@ -833,8 +833,8 @@ pub const Decl = struct {
833833 }
834834
835835 /// Same as `getInnerNamespaceIndex` but additionally obtains the pointer.
836 pub fn getInnerNamespace(decl: Decl, mod: *Module) ?*Namespace {
837 return if (decl.getInnerNamespaceIndex(mod).unwrap()) |i| mod.namespacePtr(i) else null;
836 pub fn getOwnedInnerNamespace(decl: Decl, mod: *Module) ?*Namespace {
837 return mod.namespacePtrUnwrap(decl.getOwnedInnerNamespaceIndex(mod));
838838 }
839839
840840 pub fn dump(decl: *Decl) void {
......@@ -3361,7 +3361,7 @@ pub fn destroyDecl(mod: *Module, decl_index: Decl.Index) void {
33613361 gpa.free(kv.value);
33623362 }
33633363 if (decl.has_tv) {
3364 if (decl.getInnerNamespaceIndex(mod).unwrap()) |i| {
3364 if (decl.getOwnedInnerNamespaceIndex(mod).unwrap()) |i| {
33653365 mod.namespacePtr(i).destroyDecls(mod);
33663366 mod.destroyNamespace(i);
33673367 }
......@@ -3407,6 +3407,10 @@ pub fn inferredErrorSetPtr(mod: *Module, index: Fn.InferredErrorSet.Index) *Fn.I
34073407 return mod.intern_pool.inferredErrorSetPtr(index);
34083408}
34093409
3410pub fn namespacePtrUnwrap(mod: *Module, index: Namespace.OptionalIndex) ?*Namespace {
3411 return mod.namespacePtr(index.unwrap() orelse return null);
3412}
3413
34103414/// This one accepts an index from the InternPool and asserts that it is not
34113415/// the anonymous empty struct type.
34123416pub fn structPtrUnwrap(mod: *Module, index: Struct.OptionalIndex) ?*Struct {
......@@ -3873,28 +3877,28 @@ fn updateZirRefs(mod: *Module, file: *File, old_zir: Zir) !void {
38733877
38743878 if (!decl.owns_tv) continue;
38753879
3876 if (decl.getStruct(mod)) |struct_obj| {
3880 if (decl.getOwnedStruct(mod)) |struct_obj| {
38773881 struct_obj.zir_index = inst_map.get(struct_obj.zir_index) orelse {
38783882 try file.deleted_decls.append(gpa, decl_index);
38793883 continue;
38803884 };
38813885 }
38823886
3883 if (decl.getUnion(mod)) |union_obj| {
3887 if (decl.getOwnedUnion(mod)) |union_obj| {
38843888 union_obj.zir_index = inst_map.get(union_obj.zir_index) orelse {
38853889 try file.deleted_decls.append(gpa, decl_index);
38863890 continue;
38873891 };
38883892 }
38893893
3890 if (decl.getFunction(mod)) |func| {
3894 if (decl.getOwnedFunction(mod)) |func| {
38913895 func.zir_body_inst = inst_map.get(func.zir_body_inst) orelse {
38923896 try file.deleted_decls.append(gpa, decl_index);
38933897 continue;
38943898 };
38953899 }
38963900
3897 if (decl.getInnerNamespace(mod)) |namespace| {
3901 if (decl.getOwnedInnerNamespace(mod)) |namespace| {
38983902 for (namespace.decls.keys()) |sub_decl| {
38993903 try decl_stack.append(gpa, sub_decl);
39003904 }
......@@ -4074,7 +4078,7 @@ pub fn ensureDeclAnalyzed(mod: *Module, decl_index: Decl.Index) SemaError!void {
40744078 try mod.deleteDeclExports(decl_index);
40754079
40764080 // Similarly, `@setAlignStack` invocations will be re-discovered.
4077 if (decl.getFunctionIndex(mod).unwrap()) |func| {
4081 if (decl.getOwnedFunctionIndex(mod).unwrap()) |func| {
40784082 _ = mod.align_stack_fns.remove(func);
40794083 }
40804084
......@@ -4577,7 +4581,7 @@ fn semaDecl(mod: *Module, decl_index: Decl.Index) !bool {
45774581 if (mod.declIsRoot(decl_index)) {
45784582 log.debug("semaDecl root {*} ({s})", .{ decl, decl.name });
45794583 const main_struct_inst = Zir.main_struct_inst;
4580 const struct_index = decl.getStructIndex(mod).unwrap().?;
4584 const struct_index = decl.getOwnedStructIndex(mod).unwrap().?;
45814585 const struct_obj = mod.structPtr(struct_index);
45824586 // This might not have gotten set in `semaFile` if the first time had
45834587 // a ZIR failure, so we set it here in case.
......@@ -4659,7 +4663,7 @@ fn semaDecl(mod: *Module, decl_index: Decl.Index) !bool {
46594663 if (decl.has_tv) {
46604664 prev_type_has_bits = decl.ty.isFnOrHasRuntimeBits(mod);
46614665 type_changed = !decl.ty.eql(decl_tv.ty, mod);
4662 if (decl.getFunction(mod)) |prev_func| {
4666 if (decl.getOwnedFunction(mod)) |prev_func| {
46634667 prev_is_inline = prev_func.state == .inline_only;
46644668 }
46654669 }
......@@ -5313,7 +5317,7 @@ fn scanDecl(iter: *ScanDeclIter, decl_sub_index: usize, flags: u4) Allocator.Err
53135317 decl.has_align = has_align;
53145318 decl.has_linksection_or_addrspace = has_linksection_or_addrspace;
53155319 decl.zir_decl_index = @intCast(u32, decl_sub_index);
5316 if (decl.getFunctionIndex(mod) != .none) {
5320 if (decl.getOwnedFunctionIndex(mod) != .none) {
53175321 switch (comp.bin_file.tag) {
53185322 .coff, .elf, .macho, .plan9 => {
53195323 // TODO Look into detecting when this would be unnecessary by storing enough state
......@@ -5390,7 +5394,7 @@ pub fn clearDecl(
53905394 if (decl.ty.isFnOrHasRuntimeBits(mod)) {
53915395 mod.comp.bin_file.freeDecl(decl_index);
53925396 }
5393 if (decl.getInnerNamespace(mod)) |namespace| {
5397 if (decl.getOwnedInnerNamespace(mod)) |namespace| {
53945398 try namespace.deleteAllDecls(mod, outdated_decls);
53955399 }
53965400 }
......@@ -5733,10 +5737,8 @@ fn markOutdatedDecl(mod: *Module, decl_index: Decl.Index) !void {
57335737 if (mod.cimport_errors.fetchSwapRemove(decl_index)) |kv| {
57345738 for (kv.value) |err| err.deinit(mod.gpa);
57355739 }
5736 if (decl.has_tv and decl.owns_tv) {
5737 if (decl.getFunctionIndex(mod).unwrap()) |func| {
5738 _ = mod.align_stack_fns.remove(func);
5739 }
5740 if (decl.getOwnedFunctionIndex(mod).unwrap()) |func| {
5741 _ = mod.align_stack_fns.remove(func);
57405742 }
57415743 if (mod.emit_h) |emit_h| {
57425744 if (emit_h.failed_decls.fetchSwapRemove(decl_index)) |kv| {
src/Sema.zig+6-6
......@@ -5730,7 +5730,7 @@ fn zirExport(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void
57305730 {
57315731 try mod.ensureDeclAnalyzed(decl_index);
57325732 const exported_decl = mod.declPtr(decl_index);
5733 if (exported_decl.getFunction(mod)) |function| {
5733 if (exported_decl.val.getFunction(mod)) |function| {
57345734 return sema.analyzeExport(block, src, options, function.owner_decl);
57355735 }
57365736 }
......@@ -6206,7 +6206,7 @@ fn funcDeclSrc(sema: *Sema, func_inst: Air.Inst.Ref) !?*Decl {
62066206 .extern_func => |extern_func| extern_func.decl,
62076207 .func => |func| mod.funcPtr(func.index).owner_decl,
62086208 .ptr => |ptr| switch (ptr.addr) {
6209 .decl => |decl| decl,
6209 .decl => |decl| mod.declPtr(decl).val.getFunction(mod).?.owner_decl,
62106210 else => return null,
62116211 },
62126212 else => return null,
......@@ -6782,7 +6782,7 @@ fn analyzeCall(
67826782 }),
67836783 .func => |function| function.index,
67846784 .ptr => |ptr| switch (ptr.addr) {
6785 .decl => |decl| mod.declPtr(decl).getFunctionIndex(mod).unwrap().?,
6785 .decl => |decl| mod.declPtr(decl).val.getFunctionIndex(mod).unwrap().?,
67866786 else => {
67876787 assert(callee_ty.isPtrAtRuntime(mod));
67886788 return sema.fail(block, call_src, "{s} call of function pointer", .{
......@@ -7403,7 +7403,7 @@ fn instantiateGenericCall(
74037403 const func_val = try sema.resolveConstValue(block, func_src, func, "generic function being called must be comptime-known");
74047404 const module_fn = mod.funcPtr(switch (mod.intern_pool.indexToKey(func_val.toIntern())) {
74057405 .func => |function| function.index,
7406 .ptr => |ptr| mod.declPtr(ptr.addr.decl).getFunctionIndex(mod).unwrap().?,
7406 .ptr => |ptr| mod.declPtr(ptr.addr.decl).val.getFunctionIndex(mod).unwrap().?,
74077407 else => unreachable,
74087408 });
74097409 // Check the Module's generic function map with an adapted context, so that we
......@@ -28336,7 +28336,7 @@ fn beginComptimePtrLoad(
2833628336 const is_mutable = ptr.addr == .mut_decl;
2833728337 const decl = mod.declPtr(decl_index);
2833828338 const decl_tv = try decl.typedValue();
28339 if (decl.getVariable(mod) != null) return error.RuntimeLoad;
28339 if (decl.val.getVariable(mod) != null) return error.RuntimeLoad;
2834028340
2834128341 const layout_defined = decl.ty.hasWellDefinedLayout(mod);
2834228342 break :blk ComptimePtrLoadKit{
......@@ -29423,7 +29423,7 @@ fn analyzeDeclRefInner(sema: *Sema, decl_index: Decl.Index, analyze_fn_body: boo
2942329423 const ptr_ty = try mod.ptrType(.{
2942429424 .elem_type = decl_tv.ty.toIntern(),
2942529425 .alignment = InternPool.Alignment.fromByteUnits(decl.@"align"),
29426 .is_const = if (decl.getVariable(mod)) |variable| variable.is_const else false,
29426 .is_const = if (decl.val.getVariable(mod)) |variable| variable.is_const else false,
2942729427 .address_space = decl.@"addrspace",
2942829428 });
2942929429 if (analyze_fn_body) {
src/arch/wasm/CodeGen.zig+1-1
......@@ -2210,7 +2210,7 @@ fn airCall(func: *CodeGen, inst: Air.Inst.Index, modifier: std.builtin.CallModif
22102210 try func.bin_file.addOrUpdateImport(
22112211 mem.sliceTo(ext_decl.name, 0),
22122212 atom.getSymbolIndex().?,
2213 mod.intern_pool.stringToSliceUnwrap(ext_decl.getExternFunc(mod).?.lib_name),
2213 mod.intern_pool.stringToSliceUnwrap(ext_decl.getOwnedExternFunc(mod).?.lib_name),
22142214 type_index,
22152215 );
22162216 break :blk extern_func.decl;
src/codegen/c.zig+6-6
......@@ -549,12 +549,12 @@ pub const DeclGen = struct {
549549 }
550550
551551 // Chase function values in order to be able to reference the original function.
552 if (decl.getFunction(mod)) |func| if (func.owner_decl != decl_index)
552 if (decl.val.getFunction(mod)) |func| if (func.owner_decl != decl_index)
553553 return dg.renderDeclValue(writer, ty, val, func.owner_decl, location);
554 if (decl.getExternFunc(mod)) |extern_func| if (extern_func.decl != decl_index)
554 if (decl.val.getExternFunc(mod)) |extern_func| if (extern_func.decl != decl_index)
555555 return dg.renderDeclValue(writer, ty, val, extern_func.decl, location);
556556
557 if (decl.getVariable(mod)) |variable| try dg.renderFwdDecl(decl_index, variable);
557 if (decl.val.getVariable(mod)) |variable| try dg.renderFwdDecl(decl_index, variable);
558558
559559 // We shouldn't cast C function pointers as this is UB (when you call
560560 // them). The analysis until now should ensure that the C function
......@@ -1580,7 +1580,7 @@ pub const DeclGen = struct {
15801580 else => unreachable,
15811581 }
15821582 }
1583 if (fn_decl.getFunction(mod)) |func| if (func.is_cold) try w.writeAll("zig_cold ");
1583 if (fn_decl.val.getFunction(mod)) |func| if (func.is_cold) try w.writeAll("zig_cold ");
15841584 if (fn_info.return_type == .noreturn_type) try w.writeAll("zig_noreturn ");
15851585
15861586 const trailing = try renderTypePrefix(
......@@ -2740,13 +2740,13 @@ pub fn genDecl(o: *Object) !void {
27402740 const tv: TypedValue = .{ .ty = decl.ty, .val = decl.val };
27412741
27422742 if (!tv.ty.isFnOrHasRuntimeBitsIgnoreComptime(mod)) return;
2743 if (decl.getExternFunc(mod)) |_| {
2743 if (tv.val.getExternFunc(mod)) |_| {
27442744 const fwd_decl_writer = o.dg.fwd_decl.writer();
27452745 try fwd_decl_writer.writeAll("zig_extern ");
27462746 try o.dg.renderFunctionSignature(fwd_decl_writer, decl_c_value.decl, .forward, .{ .export_index = 0 });
27472747 try fwd_decl_writer.writeAll(";\n");
27482748 try genExports(o);
2749 } else if (decl.getVariable(mod)) |variable| {
2749 } else if (tv.val.getVariable(mod)) |variable| {
27502750 try o.dg.renderFwdDecl(decl_c_value.decl, variable);
27512751 try genExports(o);
27522752
src/codegen/llvm.zig+12-12
......@@ -1165,7 +1165,7 @@ pub const Object = struct {
11651165 di_file = try dg.object.getDIFile(gpa, mod.namespacePtr(decl.src_namespace).file_scope);
11661166
11671167 const line_number = decl.src_line + 1;
1168 const is_internal_linkage = decl.getExternFunc(mod) == null and
1168 const is_internal_linkage = decl.val.getExternFunc(mod) == null and
11691169 !mod.decl_exports.contains(decl_index);
11701170 const noret_bit: c_uint = if (fn_info.return_type == .noreturn_type)
11711171 llvm.DIFlags.NoReturn
......@@ -1274,7 +1274,7 @@ pub const Object = struct {
12741274 var free_decl_name = false;
12751275 const decl_name = decl_name: {
12761276 if (mod.getTarget().isWasm() and try decl.isFunction(mod)) {
1277 if (mod.intern_pool.stringToSliceUnwrap(decl.getExternFunc(mod).?.lib_name)) |lib_name| {
1277 if (mod.intern_pool.stringToSliceUnwrap(decl.getOwnedExternFunc(mod).?.lib_name)) |lib_name| {
12781278 if (!std.mem.eql(u8, lib_name, "c")) {
12791279 free_decl_name = true;
12801280 break :decl_name try std.fmt.allocPrintZ(gpa, "{s}|{s}", .{ decl.name, lib_name });
......@@ -1306,7 +1306,7 @@ pub const Object = struct {
13061306 di_global.replaceLinkageName(linkage_name);
13071307 }
13081308 }
1309 if (decl.getVariable(mod)) |variable| {
1309 if (decl.val.getVariable(mod)) |variable| {
13101310 if (variable.is_threadlocal) {
13111311 llvm_global.setThreadLocalMode(.GeneralDynamicTLSModel);
13121312 } else {
......@@ -1348,7 +1348,7 @@ pub const Object = struct {
13481348 defer gpa.free(section_z);
13491349 llvm_global.setSection(section_z);
13501350 }
1351 if (decl.getVariable(mod)) |variable| {
1351 if (decl.val.getVariable(mod)) |variable| {
13521352 if (variable.is_threadlocal) {
13531353 llvm_global.setThreadLocalMode(.GeneralDynamicTLSModel);
13541354 }
......@@ -1382,7 +1382,7 @@ pub const Object = struct {
13821382 llvm_global.setLinkage(.Internal);
13831383 if (mod.wantDllExports()) llvm_global.setDLLStorageClass(.Default);
13841384 llvm_global.setUnnamedAddr(.True);
1385 if (decl.getVariable(mod)) |variable| {
1385 if (decl.val.getVariable(mod)) |variable| {
13861386 const single_threaded = mod.comp.bin_file.options.single_threaded;
13871387 if (variable.is_threadlocal and !single_threaded) {
13881388 llvm_global.setThreadLocalMode(.GeneralDynamicTLSModel);
......@@ -2452,7 +2452,7 @@ pub const DeclGen = struct {
24522452 log.debug("gen: {s} type: {}, value: {}", .{
24532453 decl.name, decl.ty.fmtDebug(), decl.val.fmtDebug(),
24542454 });
2455 if (decl.getExternFunc(mod)) |extern_func| {
2455 if (decl.val.getExternFunc(mod)) |extern_func| {
24562456 _ = try dg.resolveLlvmFunction(extern_func.decl);
24572457 } else {
24582458 const target = mod.getTarget();
......@@ -2460,7 +2460,7 @@ pub const DeclGen = struct {
24602460 global.setAlignment(decl.getAlignment(mod));
24612461 if (decl.@"linksection") |section| global.setSection(section);
24622462 assert(decl.has_tv);
2463 const init_val = if (decl.getVariable(mod)) |variable| init_val: {
2463 const init_val = if (decl.val.getVariable(mod)) |variable| init_val: {
24642464 break :init_val variable.init.toValue();
24652465 } else init_val: {
24662466 global.setGlobalConstant(.True);
......@@ -2555,7 +2555,7 @@ pub const DeclGen = struct {
25552555 } else {
25562556 if (target.isWasm()) {
25572557 dg.addFnAttrString(llvm_fn, "wasm-import-name", std.mem.sliceTo(decl.name, 0));
2558 if (mod.intern_pool.stringToSliceUnwrap(decl.getExternFunc(mod).?.lib_name)) |lib_name| {
2558 if (mod.intern_pool.stringToSliceUnwrap(decl.getOwnedExternFunc(mod).?.lib_name)) |lib_name| {
25592559 if (!std.mem.eql(u8, lib_name, "c")) {
25602560 dg.addFnAttrString(llvm_fn, "wasm-import-module", lib_name);
25612561 }
......@@ -2716,7 +2716,7 @@ pub const DeclGen = struct {
27162716 llvm_global.setValueName(decl.name);
27172717 llvm_global.setUnnamedAddr(.False);
27182718 llvm_global.setLinkage(.External);
2719 if (decl.getVariable(mod)) |variable| {
2719 if (decl.val.getVariable(mod)) |variable| {
27202720 const single_threaded = mod.comp.bin_file.options.single_threaded;
27212721 if (variable.is_threadlocal and !single_threaded) {
27222722 llvm_global.setThreadLocalMode(.GeneralDynamicTLSModel);
......@@ -3993,11 +3993,11 @@ pub const DeclGen = struct {
39933993 // ... &bar;
39943994 // `bar` is just an alias and we actually want to lower a reference to `foo`.
39953995 const decl = mod.declPtr(decl_index);
3996 if (decl.getFunction(mod)) |func| {
3996 if (decl.val.getFunction(mod)) |func| {
39973997 if (func.owner_decl != decl_index) {
39983998 return self.lowerDeclRefValue(tv, func.owner_decl);
39993999 }
4000 } else if (decl.getExternFunc(mod)) |func| {
4000 } else if (decl.val.getExternFunc(mod)) |func| {
40014001 if (func.decl != decl_index) {
40024002 return self.lowerDeclRefValue(tv, func.decl);
40034003 }
......@@ -7939,7 +7939,7 @@ pub const FuncGen = struct {
79397939 }
79407940
79417941 const src_index = self.air.instructions.items(.data)[inst].arg.src_index;
7942 const func = self.dg.decl.getFunction(mod).?;
7942 const func = self.dg.decl.getOwnedFunction(mod).?;
79437943 const lbrace_line = mod.declPtr(func.owner_decl).src_line + func.lbrace_line + 1;
79447944 const lbrace_col = func.lbrace_column + 1;
79457945 const di_local_var = dib.createParameterVariable(
src/codegen/spirv.zig+3-3
......@@ -261,7 +261,7 @@ pub const DeclGen = struct {
261261 const entry = try self.decl_link.getOrPut(decl_index);
262262 if (!entry.found_existing) {
263263 // TODO: Extern fn?
264 const kind: SpvModule.DeclKind = if (decl.getFunctionIndex(self.module) != .none)
264 const kind: SpvModule.DeclKind = if (decl.val.getFunctionIndex(self.module) != .none)
265265 .func
266266 else
267267 .global;
......@@ -1544,7 +1544,7 @@ pub const DeclGen = struct {
15441544 const decl_id = self.spv.declPtr(spv_decl_index).result_id;
15451545 log.debug("genDecl: id = {}, index = {}, name = {s}", .{ decl_id.id, @enumToInt(spv_decl_index), decl.name });
15461546
1547 if (decl.getFunction(mod)) |_| {
1547 if (decl.val.getFunction(mod)) |_| {
15481548 assert(decl.ty.zigTypeTag(mod) == .Fn);
15491549 const prototype_id = try self.resolveTypeId(decl.ty);
15501550 try self.func.prologue.emit(self.spv.gpa, .OpFunction, .{
......@@ -1597,7 +1597,7 @@ pub const DeclGen = struct {
15971597 try self.generateTestEntryPoint(fqn, spv_decl_index);
15981598 }
15991599 } else {
1600 const init_val = if (decl.getVariable(mod)) |payload|
1600 const init_val = if (decl.val.getVariable(mod)) |payload|
16011601 payload.init.toValue()
16021602 else
16031603 decl.val;
src/link/Coff.zig+5-5
......@@ -1156,10 +1156,10 @@ pub fn updateDecl(
11561156
11571157 const decl = mod.declPtr(decl_index);
11581158
1159 if (decl.getExternFunc(mod)) |_| {
1159 if (decl.val.getExternFunc(mod)) |_| {
11601160 return; // TODO Should we do more when front-end analyzed extern decl?
11611161 }
1162 if (decl.getVariable(mod)) |variable| {
1162 if (decl.val.getVariable(mod)) |variable| {
11631163 if (variable.is_extern) {
11641164 return; // TODO Should we do more when front-end analyzed extern decl?
11651165 }
......@@ -1172,7 +1172,7 @@ pub fn updateDecl(
11721172 var code_buffer = std.ArrayList(u8).init(self.base.allocator);
11731173 defer code_buffer.deinit();
11741174
1175 const decl_val = if (decl.getVariable(mod)) |variable| variable.init.toValue() else decl.val;
1175 const decl_val = if (decl.val.getVariable(mod)) |variable| variable.init.toValue() else decl.val;
11761176 const res = try codegen.generateSymbol(&self.base, decl.srcLoc(mod), .{
11771177 .ty = decl.ty,
11781178 .val = decl_val,
......@@ -1313,7 +1313,7 @@ fn getDeclOutputSection(self: *Coff, decl_index: Module.Decl.Index) u16 {
13131313 // TODO: what if this is a function pointer?
13141314 .Fn => break :blk self.text_section_index.?,
13151315 else => {
1316 if (decl.getVariable(mod)) |_| {
1316 if (val.getVariable(mod)) |_| {
13171317 break :blk self.data_section_index.?;
13181318 }
13191319 break :blk self.rdata_section_index.?;
......@@ -1425,7 +1425,7 @@ pub fn updateDeclExports(
14251425 // detect the default subsystem.
14261426 for (exports) |exp| {
14271427 const exported_decl = mod.declPtr(exp.exported_decl);
1428 if (exported_decl.getFunctionIndex(mod) == .none) continue;
1428 if (exported_decl.getOwnedFunctionIndex(mod) == .none) continue;
14291429 const winapi_cc = switch (self.base.options.target.cpu.arch) {
14301430 .x86 => std.builtin.CallingConvention.Stdcall,
14311431 else => std.builtin.CallingConvention.C,
src/link/Dwarf.zig+2-2
......@@ -971,7 +971,7 @@ pub fn initDeclState(self: *Dwarf, mod: *Module, decl_index: Module.Decl.Index)
971971 // For functions we need to add a prologue to the debug line program.
972972 try dbg_line_buffer.ensureTotalCapacity(26);
973973
974 const func = decl.getFunction(mod).?;
974 const func = decl.val.getFunction(mod).?;
975975 log.debug("decl.src_line={d}, func.lbrace_line={d}, func.rbrace_line={d}", .{
976976 decl.src_line,
977977 func.lbrace_line,
......@@ -1523,7 +1523,7 @@ pub fn updateDeclLineNumber(self: *Dwarf, mod: *Module, decl_index: Module.Decl.
15231523 if (atom.len == 0) return;
15241524
15251525 const decl = mod.declPtr(decl_index);
1526 const func = decl.getFunction(mod).?;
1526 const func = decl.val.getFunction(mod).?;
15271527 log.debug("decl.src_line={d}, func.lbrace_line={d}, func.rbrace_line={d}", .{
15281528 decl.src_line,
15291529 func.lbrace_line,
src/link/Elf.zig+4-4
......@@ -2465,7 +2465,7 @@ fn getDeclShdrIndex(self: *Elf, decl_index: Module.Decl.Index) u16 {
24652465 // TODO: what if this is a function pointer?
24662466 .Fn => break :blk self.text_section_index.?,
24672467 else => {
2468 if (decl.getVariable(mod)) |_| {
2468 if (val.getVariable(mod)) |_| {
24692469 break :blk self.data_section_index.?;
24702470 }
24712471 break :blk self.rodata_section_index.?;
......@@ -2647,10 +2647,10 @@ pub fn updateDecl(
26472647
26482648 const decl = mod.declPtr(decl_index);
26492649
2650 if (decl.getExternFunc(mod)) |_| {
2650 if (decl.val.getExternFunc(mod)) |_| {
26512651 return; // TODO Should we do more when front-end analyzed extern decl?
26522652 }
2653 if (decl.getVariable(mod)) |variable| {
2653 if (decl.val.getVariable(mod)) |variable| {
26542654 if (variable.is_extern) {
26552655 return; // TODO Should we do more when front-end analyzed extern decl?
26562656 }
......@@ -2667,7 +2667,7 @@ pub fn updateDecl(
26672667 defer if (decl_state) |*ds| ds.deinit();
26682668
26692669 // TODO implement .debug_info for global variables
2670 const decl_val = if (decl.getVariable(mod)) |variable| variable.init.toValue() else decl.val;
2670 const decl_val = if (decl.val.getVariable(mod)) |variable| variable.init.toValue() else decl.val;
26712671 const res = if (decl_state) |*ds|
26722672 try codegen.generateSymbol(&self.base, decl.srcLoc(mod), .{
26732673 .ty = decl.ty,
src/link/MachO.zig+7-7
......@@ -1984,16 +1984,16 @@ pub fn updateDecl(self: *MachO, mod: *Module, decl_index: Module.Decl.Index) !vo
19841984
19851985 const decl = mod.declPtr(decl_index);
19861986
1987 if (decl.getExternFunc(mod)) |_| {
1987 if (decl.val.getExternFunc(mod)) |_| {
19881988 return; // TODO Should we do more when front-end analyzed extern decl?
19891989 }
1990 if (decl.getVariable(mod)) |variable| {
1990 if (decl.val.getVariable(mod)) |variable| {
19911991 if (variable.is_extern) {
19921992 return; // TODO Should we do more when front-end analyzed extern decl?
19931993 }
19941994 }
19951995
1996 const is_threadlocal = if (decl.getVariable(mod)) |variable|
1996 const is_threadlocal = if (decl.val.getVariable(mod)) |variable|
19971997 variable.is_threadlocal and !self.base.options.single_threaded
19981998 else
19991999 false;
......@@ -2012,7 +2012,7 @@ pub fn updateDecl(self: *MachO, mod: *Module, decl_index: Module.Decl.Index) !vo
20122012 null;
20132013 defer if (decl_state) |*ds| ds.deinit();
20142014
2015 const decl_val = if (decl.getVariable(mod)) |variable| variable.init.toValue() else decl.val;
2015 const decl_val = if (decl.val.getVariable(mod)) |variable| variable.init.toValue() else decl.val;
20162016 const res = if (decl_state) |*ds|
20172017 try codegen.generateSymbol(&self.base, decl.srcLoc(mod), .{
20182018 .ty = decl.ty,
......@@ -2177,7 +2177,7 @@ fn updateThreadlocalVariable(self: *MachO, module: *Module, decl_index: Module.D
21772177
21782178 const decl = module.declPtr(decl_index);
21792179 const decl_metadata = self.decls.get(decl_index).?;
2180 const decl_val = decl.getVariable(mod).?.init.toValue();
2180 const decl_val = decl.val.getVariable(mod).?.init.toValue();
21812181 const res = if (decl_state) |*ds|
21822182 try codegen.generateSymbol(&self.base, decl.srcLoc(mod), .{
21832183 .ty = decl.ty,
......@@ -2278,7 +2278,7 @@ fn getDeclOutputSection(self: *MachO, decl_index: Module.Decl.Index) u8 {
22782278 }
22792279 }
22802280
2281 if (decl.getVariable(mod)) |variable| {
2281 if (val.getVariable(mod)) |variable| {
22822282 if (variable.is_threadlocal and !single_threaded) {
22832283 break :blk self.thread_data_section_index.?;
22842284 }
......@@ -2289,7 +2289,7 @@ fn getDeclOutputSection(self: *MachO, decl_index: Module.Decl.Index) u8 {
22892289 // TODO: what if this is a function pointer?
22902290 .Fn => break :blk self.text_section_index.?,
22912291 else => {
2292 if (decl.getVariable(mod)) |_| {
2292 if (val.getVariable(mod)) |_| {
22932293 break :blk self.data_section_index.?;
22942294 }
22952295 break :blk self.data_const_section_index.?;
src/link/Plan9.zig+4-4
......@@ -392,10 +392,10 @@ pub fn lowerUnnamedConst(self: *Plan9, tv: TypedValue, decl_index: Module.Decl.I
392392pub fn updateDecl(self: *Plan9, mod: *Module, decl_index: Module.Decl.Index) !void {
393393 const decl = mod.declPtr(decl_index);
394394
395 if (decl.getExternFunc(mod)) |_| {
395 if (decl.val.getExternFunc(mod)) |_| {
396396 return; // TODO Should we do more when front-end analyzed extern decl?
397397 }
398 if (decl.getVariable(mod)) |variable| {
398 if (decl.val.getVariable(mod)) |variable| {
399399 if (variable.is_extern) {
400400 return; // TODO Should we do more when front-end analyzed extern decl?
401401 }
......@@ -407,7 +407,7 @@ pub fn updateDecl(self: *Plan9, mod: *Module, decl_index: Module.Decl.Index) !vo
407407
408408 var code_buffer = std.ArrayList(u8).init(self.base.allocator);
409409 defer code_buffer.deinit();
410 const decl_val = if (decl.getVariable(mod)) |variable| variable.init.toValue() else decl.val;
410 const decl_val = if (decl.val.getVariable(mod)) |variable| variable.init.toValue() else decl.val;
411411 // TODO we need the symbol index for symbol in the table of locals for the containing atom
412412 const res = try codegen.generateSymbol(&self.base, decl.srcLoc(mod), .{
413413 .ty = decl.ty,
......@@ -771,7 +771,7 @@ pub fn freeDecl(self: *Plan9, decl_index: Module.Decl.Index) void {
771771 // in the deleteUnusedDecl function.
772772 const mod = self.base.options.module.?;
773773 const decl = mod.declPtr(decl_index);
774 const is_fn = decl.getFunctionIndex(mod) != .none;
774 const is_fn = decl.val.getFunctionIndex(mod) != .none;
775775 if (is_fn) {
776776 var symidx_and_submap = self.fn_decl_table.get(decl.getFileScope(mod)).?;
777777 var submap = symidx_and_submap.functions;
src/link/SpirV.zig+1-1
......@@ -138,7 +138,7 @@ pub fn updateDeclExports(
138138 exports: []const *Module.Export,
139139) !void {
140140 const decl = mod.declPtr(decl_index);
141 if (decl.getFunctionIndex(mod) != .none and decl.ty.fnCallingConvention(mod) == .Kernel) {
141 if (decl.val.getFunctionIndex(mod) != .none and decl.ty.fnCallingConvention(mod) == .Kernel) {
142142 // TODO: Unify with resolveDecl in spirv.zig.
143143 const entry = try self.decl_link.getOrPut(decl_index);
144144 if (!entry.found_existing) {
src/link/Wasm.zig+5-5
......@@ -1404,9 +1404,9 @@ pub fn updateDecl(wasm: *Wasm, mod: *Module, decl_index: Module.Decl.Index) !voi
14041404 defer tracy.end();
14051405
14061406 const decl = mod.declPtr(decl_index);
1407 if (decl.getFunction(mod)) |_| {
1407 if (decl.val.getFunction(mod)) |_| {
14081408 return;
1409 } else if (decl.getExternFunc(mod)) |_| {
1409 } else if (decl.val.getExternFunc(mod)) |_| {
14101410 return;
14111411 }
14121412
......@@ -1415,12 +1415,12 @@ pub fn updateDecl(wasm: *Wasm, mod: *Module, decl_index: Module.Decl.Index) !voi
14151415 atom.clear();
14161416
14171417 if (decl.isExtern(mod)) {
1418 const variable = decl.getVariable(mod).?;
1418 const variable = decl.getOwnedVariable(mod).?;
14191419 const name = mem.sliceTo(decl.name, 0);
14201420 const lib_name = mod.intern_pool.stringToSliceUnwrap(variable.lib_name);
14211421 return wasm.addOrUpdateImport(name, atom.sym_index, lib_name, null);
14221422 }
1423 const val = if (decl.getVariable(mod)) |variable| variable.init.toValue() else decl.val;
1423 const val = if (decl.val.getVariable(mod)) |variable| variable.init.toValue() else decl.val;
14241424
14251425 var code_writer = std.ArrayList(u8).init(wasm.base.allocator);
14261426 defer code_writer.deinit();
......@@ -3373,7 +3373,7 @@ pub fn flushModule(wasm: *Wasm, comp: *Compilation, prog_node: *std.Progress.Nod
33733373 const atom = wasm.getAtomPtr(atom_index);
33743374 if (decl.ty.zigTypeTag(mod) == .Fn) {
33753375 try wasm.parseAtom(atom_index, .function);
3376 } else if (decl.getVariable(mod)) |variable| {
3376 } else if (decl.getOwnedVariable(mod)) |variable| {
33773377 if (variable.is_const) {
33783378 try wasm.parseAtom(atom_index, .{ .data = .read_only });
33793379 } else if (variable.init.toValue().isUndefDeep(mod)) {