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 {...@@ -613,7 +613,7 @@ pub const Decl = struct {
613613
614 pub fn clearValues(decl: *Decl, mod: *Module) void {614 pub fn clearValues(decl: *Decl, mod: *Module) void {
615 const gpa = mod.gpa;615 const gpa = mod.gpa;
616 if (decl.getFunctionIndex(mod).unwrap()) |func| {616 if (decl.getOwnedFunctionIndex(mod).unwrap()) |func| {
617 _ = mod.align_stack_fns.remove(func);617 _ = mod.align_stack_fns.remove(func);
618 if (mod.funcPtr(func).comptime_args != null) {618 if (mod.funcPtr(func).comptime_args != null) {
619 _ = mod.monomorphed_funcs.removeContext(func, .{ .mod = mod });619 _ = mod.monomorphed_funcs.removeContext(func, .{ .mod = mod });
...@@ -772,52 +772,52 @@ pub const Decl = struct {...@@ -772,52 +772,52 @@ pub const Decl = struct {
772 return tv.ty.zigTypeTag(mod) == .Fn;772 return tv.ty.zigTypeTag(mod) == .Fn;
773 }773 }
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,
776 /// otherwise null.776 /// otherwise null.
777 pub fn getStruct(decl: Decl, mod: *Module) ?*Struct {777 pub fn getOwnedStruct(decl: Decl, mod: *Module) ?*Struct {
778 return mod.structPtrUnwrap(decl.getStructIndex(mod));778 return mod.structPtrUnwrap(decl.getOwnedStructIndex(mod));
779 }779 }
780780
781 pub fn getStructIndex(decl: Decl, mod: *Module) Struct.OptionalIndex {781 pub fn getOwnedStructIndex(decl: Decl, mod: *Module) Struct.OptionalIndex {
782 if (!decl.owns_tv) return .none;782 if (!decl.owns_tv) return .none;
783 if (decl.val.ip_index == .none) return .none;783 if (decl.val.ip_index == .none) return .none;
784 return mod.intern_pool.indexToStructType(decl.val.toIntern());784 return mod.intern_pool.indexToStructType(decl.val.toIntern());
785 }785 }
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,
788 /// otherwise null.788 /// otherwise null.
789 pub fn getUnion(decl: Decl, mod: *Module) ?*Union {789 pub fn getOwnedUnion(decl: Decl, mod: *Module) ?*Union {
790 if (!decl.owns_tv) return null;790 if (!decl.owns_tv) return null;
791 if (decl.val.ip_index == .none) return null;791 if (decl.val.ip_index == .none) return null;
792 return mod.typeToUnion(decl.val.toType());792 return mod.typeToUnion(decl.val.toType());
793 }793 }
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,
796 /// otherwise null.796 /// otherwise null.
797 pub fn getFunction(decl: Decl, mod: *Module) ?*Fn {797 pub fn getOwnedFunction(decl: Decl, mod: *Module) ?*Fn {
798 return mod.funcPtrUnwrap(decl.getFunctionIndex(mod));798 return mod.funcPtrUnwrap(decl.getOwnedFunctionIndex(mod));
799 }799 }
800800
801 pub fn getFunctionIndex(decl: Decl, mod: *Module) Fn.OptionalIndex {801 pub fn getOwnedFunctionIndex(decl: Decl, mod: *Module) Fn.OptionalIndex {
802 return if (decl.owns_tv) decl.val.getFunctionIndex(mod) else .none;802 return if (decl.owns_tv) decl.val.getFunctionIndex(mod) else .none;
803 }803 }
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,
806 /// otherwise null.806 /// otherwise null.
807 pub fn getExternFunc(decl: Decl, mod: *Module) ?InternPool.Key.ExternFunc {807 pub fn getOwnedExternFunc(decl: Decl, mod: *Module) ?InternPool.Key.ExternFunc {
808 return if (decl.owns_tv) decl.val.getExternFunc(mod) else null;808 return if (decl.owns_tv) decl.val.getExternFunc(mod) else null;
809 }809 }
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,
812 /// otherwise null.812 /// otherwise null.
813 pub fn getVariable(decl: Decl, mod: *Module) ?InternPool.Key.Variable {813 pub fn getOwnedVariable(decl: Decl, mod: *Module) ?InternPool.Key.Variable {
814 return if (decl.owns_tv) decl.val.getVariable(mod) else null;814 return if (decl.owns_tv) decl.val.getVariable(mod) else null;
815 }815 }
816816
817 /// Gets the namespace that this Decl creates by being a struct, union,817 /// Gets the namespace that this Decl creates by being a struct, union,
818 /// enum, or opaque.818 /// enum, or opaque.
819 /// Only returns it if the Decl is the owner.819 /// 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 {
821 if (!decl.owns_tv) return .none;821 if (!decl.owns_tv) return .none;
822 return switch (decl.val.ip_index) {822 return switch (decl.val.ip_index) {
823 .empty_struct_type => .none,823 .empty_struct_type => .none,
...@@ -833,8 +833,8 @@ pub const Decl = struct {...@@ -833,8 +833,8 @@ pub const Decl = struct {
833 }833 }
834834
835 /// Same as `getInnerNamespaceIndex` but additionally obtains the pointer.835 /// Same as `getInnerNamespaceIndex` but additionally obtains the pointer.
836 pub fn getInnerNamespace(decl: Decl, mod: *Module) ?*Namespace {836 pub fn getOwnedInnerNamespace(decl: Decl, mod: *Module) ?*Namespace {
837 return if (decl.getInnerNamespaceIndex(mod).unwrap()) |i| mod.namespacePtr(i) else null;837 return mod.namespacePtrUnwrap(decl.getOwnedInnerNamespaceIndex(mod));
838 }838 }
839839
840 pub fn dump(decl: *Decl) void {840 pub fn dump(decl: *Decl) void {
...@@ -3361,7 +3361,7 @@ pub fn destroyDecl(mod: *Module, decl_index: Decl.Index) void {...@@ -3361,7 +3361,7 @@ pub fn destroyDecl(mod: *Module, decl_index: Decl.Index) void {
3361 gpa.free(kv.value);3361 gpa.free(kv.value);
3362 }3362 }
3363 if (decl.has_tv) {3363 if (decl.has_tv) {
3364 if (decl.getInnerNamespaceIndex(mod).unwrap()) |i| {3364 if (decl.getOwnedInnerNamespaceIndex(mod).unwrap()) |i| {
3365 mod.namespacePtr(i).destroyDecls(mod);3365 mod.namespacePtr(i).destroyDecls(mod);
3366 mod.destroyNamespace(i);3366 mod.destroyNamespace(i);
3367 }3367 }
...@@ -3407,6 +3407,10 @@ pub fn inferredErrorSetPtr(mod: *Module, index: Fn.InferredErrorSet.Index) *Fn.I...@@ -3407,6 +3407,10 @@ pub fn inferredErrorSetPtr(mod: *Module, index: Fn.InferredErrorSet.Index) *Fn.I
3407 return mod.intern_pool.inferredErrorSetPtr(index);3407 return mod.intern_pool.inferredErrorSetPtr(index);
3408}3408}
34093409
3410pub fn namespacePtrUnwrap(mod: *Module, index: Namespace.OptionalIndex) ?*Namespace {
3411 return mod.namespacePtr(index.unwrap() orelse return null);
3412}
3413
3410/// This one accepts an index from the InternPool and asserts that it is not3414/// This one accepts an index from the InternPool and asserts that it is not
3411/// the anonymous empty struct type.3415/// the anonymous empty struct type.
3412pub fn structPtrUnwrap(mod: *Module, index: Struct.OptionalIndex) ?*Struct {3416pub fn structPtrUnwrap(mod: *Module, index: Struct.OptionalIndex) ?*Struct {
...@@ -3873,28 +3877,28 @@ fn updateZirRefs(mod: *Module, file: *File, old_zir: Zir) !void {...@@ -3873,28 +3877,28 @@ fn updateZirRefs(mod: *Module, file: *File, old_zir: Zir) !void {
38733877
3874 if (!decl.owns_tv) continue;3878 if (!decl.owns_tv) continue;
38753879
3876 if (decl.getStruct(mod)) |struct_obj| {3880 if (decl.getOwnedStruct(mod)) |struct_obj| {
3877 struct_obj.zir_index = inst_map.get(struct_obj.zir_index) orelse {3881 struct_obj.zir_index = inst_map.get(struct_obj.zir_index) orelse {
3878 try file.deleted_decls.append(gpa, decl_index);3882 try file.deleted_decls.append(gpa, decl_index);
3879 continue;3883 continue;
3880 };3884 };
3881 }3885 }
38823886
3883 if (decl.getUnion(mod)) |union_obj| {3887 if (decl.getOwnedUnion(mod)) |union_obj| {
3884 union_obj.zir_index = inst_map.get(union_obj.zir_index) orelse {3888 union_obj.zir_index = inst_map.get(union_obj.zir_index) orelse {
3885 try file.deleted_decls.append(gpa, decl_index);3889 try file.deleted_decls.append(gpa, decl_index);
3886 continue;3890 continue;
3887 };3891 };
3888 }3892 }
38893893
3890 if (decl.getFunction(mod)) |func| {3894 if (decl.getOwnedFunction(mod)) |func| {
3891 func.zir_body_inst = inst_map.get(func.zir_body_inst) orelse {3895 func.zir_body_inst = inst_map.get(func.zir_body_inst) orelse {
3892 try file.deleted_decls.append(gpa, decl_index);3896 try file.deleted_decls.append(gpa, decl_index);
3893 continue;3897 continue;
3894 };3898 };
3895 }3899 }
38963900
3897 if (decl.getInnerNamespace(mod)) |namespace| {3901 if (decl.getOwnedInnerNamespace(mod)) |namespace| {
3898 for (namespace.decls.keys()) |sub_decl| {3902 for (namespace.decls.keys()) |sub_decl| {
3899 try decl_stack.append(gpa, sub_decl);3903 try decl_stack.append(gpa, sub_decl);
3900 }3904 }
...@@ -4074,7 +4078,7 @@ pub fn ensureDeclAnalyzed(mod: *Module, decl_index: Decl.Index) SemaError!void {...@@ -4074,7 +4078,7 @@ pub fn ensureDeclAnalyzed(mod: *Module, decl_index: Decl.Index) SemaError!void {
4074 try mod.deleteDeclExports(decl_index);4078 try mod.deleteDeclExports(decl_index);
40754079
4076 // Similarly, `@setAlignStack` invocations will be re-discovered.4080 // Similarly, `@setAlignStack` invocations will be re-discovered.
4077 if (decl.getFunctionIndex(mod).unwrap()) |func| {4081 if (decl.getOwnedFunctionIndex(mod).unwrap()) |func| {
4078 _ = mod.align_stack_fns.remove(func);4082 _ = mod.align_stack_fns.remove(func);
4079 }4083 }
40804084
...@@ -4577,7 +4581,7 @@ fn semaDecl(mod: *Module, decl_index: Decl.Index) !bool {...@@ -4577,7 +4581,7 @@ fn semaDecl(mod: *Module, decl_index: Decl.Index) !bool {
4577 if (mod.declIsRoot(decl_index)) {4581 if (mod.declIsRoot(decl_index)) {
4578 log.debug("semaDecl root {*} ({s})", .{ decl, decl.name });4582 log.debug("semaDecl root {*} ({s})", .{ decl, decl.name });
4579 const main_struct_inst = Zir.main_struct_inst;4583 const main_struct_inst = Zir.main_struct_inst;
4580 const struct_index = decl.getStructIndex(mod).unwrap().?;4584 const struct_index = decl.getOwnedStructIndex(mod).unwrap().?;
4581 const struct_obj = mod.structPtr(struct_index);4585 const struct_obj = mod.structPtr(struct_index);
4582 // This might not have gotten set in `semaFile` if the first time had4586 // This might not have gotten set in `semaFile` if the first time had
4583 // a ZIR failure, so we set it here in case.4587 // a ZIR failure, so we set it here in case.
...@@ -4659,7 +4663,7 @@ fn semaDecl(mod: *Module, decl_index: Decl.Index) !bool {...@@ -4659,7 +4663,7 @@ fn semaDecl(mod: *Module, decl_index: Decl.Index) !bool {
4659 if (decl.has_tv) {4663 if (decl.has_tv) {
4660 prev_type_has_bits = decl.ty.isFnOrHasRuntimeBits(mod);4664 prev_type_has_bits = decl.ty.isFnOrHasRuntimeBits(mod);
4661 type_changed = !decl.ty.eql(decl_tv.ty, mod);4665 type_changed = !decl.ty.eql(decl_tv.ty, mod);
4662 if (decl.getFunction(mod)) |prev_func| {4666 if (decl.getOwnedFunction(mod)) |prev_func| {
4663 prev_is_inline = prev_func.state == .inline_only;4667 prev_is_inline = prev_func.state == .inline_only;
4664 }4668 }
4665 }4669 }
...@@ -5313,7 +5317,7 @@ fn scanDecl(iter: *ScanDeclIter, decl_sub_index: usize, flags: u4) Allocator.Err...@@ -5313,7 +5317,7 @@ fn scanDecl(iter: *ScanDeclIter, decl_sub_index: usize, flags: u4) Allocator.Err
5313 decl.has_align = has_align;5317 decl.has_align = has_align;
5314 decl.has_linksection_or_addrspace = has_linksection_or_addrspace;5318 decl.has_linksection_or_addrspace = has_linksection_or_addrspace;
5315 decl.zir_decl_index = @intCast(u32, decl_sub_index);5319 decl.zir_decl_index = @intCast(u32, decl_sub_index);
5316 if (decl.getFunctionIndex(mod) != .none) {5320 if (decl.getOwnedFunctionIndex(mod) != .none) {
5317 switch (comp.bin_file.tag) {5321 switch (comp.bin_file.tag) {
5318 .coff, .elf, .macho, .plan9 => {5322 .coff, .elf, .macho, .plan9 => {
5319 // TODO Look into detecting when this would be unnecessary by storing enough state5323 // TODO Look into detecting when this would be unnecessary by storing enough state
...@@ -5390,7 +5394,7 @@ pub fn clearDecl(...@@ -5390,7 +5394,7 @@ pub fn clearDecl(
5390 if (decl.ty.isFnOrHasRuntimeBits(mod)) {5394 if (decl.ty.isFnOrHasRuntimeBits(mod)) {
5391 mod.comp.bin_file.freeDecl(decl_index);5395 mod.comp.bin_file.freeDecl(decl_index);
5392 }5396 }
5393 if (decl.getInnerNamespace(mod)) |namespace| {5397 if (decl.getOwnedInnerNamespace(mod)) |namespace| {
5394 try namespace.deleteAllDecls(mod, outdated_decls);5398 try namespace.deleteAllDecls(mod, outdated_decls);
5395 }5399 }
5396 }5400 }
...@@ -5733,10 +5737,8 @@ fn markOutdatedDecl(mod: *Module, decl_index: Decl.Index) !void {...@@ -5733,10 +5737,8 @@ fn markOutdatedDecl(mod: *Module, decl_index: Decl.Index) !void {
5733 if (mod.cimport_errors.fetchSwapRemove(decl_index)) |kv| {5737 if (mod.cimport_errors.fetchSwapRemove(decl_index)) |kv| {
5734 for (kv.value) |err| err.deinit(mod.gpa);5738 for (kv.value) |err| err.deinit(mod.gpa);
5735 }5739 }
5736 if (decl.has_tv and decl.owns_tv) {5740 if (decl.getOwnedFunctionIndex(mod).unwrap()) |func| {
5737 if (decl.getFunctionIndex(mod).unwrap()) |func| {5741 _ = mod.align_stack_fns.remove(func);
5738 _ = mod.align_stack_fns.remove(func);
5739 }
5740 }5742 }
5741 if (mod.emit_h) |emit_h| {5743 if (mod.emit_h) |emit_h| {
5742 if (emit_h.failed_decls.fetchSwapRemove(decl_index)) |kv| {5744 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...@@ -5730,7 +5730,7 @@ fn zirExport(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void
5730 {5730 {
5731 try mod.ensureDeclAnalyzed(decl_index);5731 try mod.ensureDeclAnalyzed(decl_index);
5732 const exported_decl = mod.declPtr(decl_index);5732 const exported_decl = mod.declPtr(decl_index);
5733 if (exported_decl.getFunction(mod)) |function| {5733 if (exported_decl.val.getFunction(mod)) |function| {
5734 return sema.analyzeExport(block, src, options, function.owner_decl);5734 return sema.analyzeExport(block, src, options, function.owner_decl);
5735 }5735 }
5736 }5736 }
...@@ -6206,7 +6206,7 @@ fn funcDeclSrc(sema: *Sema, func_inst: Air.Inst.Ref) !?*Decl {...@@ -6206,7 +6206,7 @@ fn funcDeclSrc(sema: *Sema, func_inst: Air.Inst.Ref) !?*Decl {
6206 .extern_func => |extern_func| extern_func.decl,6206 .extern_func => |extern_func| extern_func.decl,
6207 .func => |func| mod.funcPtr(func.index).owner_decl,6207 .func => |func| mod.funcPtr(func.index).owner_decl,
6208 .ptr => |ptr| switch (ptr.addr) {6208 .ptr => |ptr| switch (ptr.addr) {
6209 .decl => |decl| decl,6209 .decl => |decl| mod.declPtr(decl).val.getFunction(mod).?.owner_decl,
6210 else => return null,6210 else => return null,
6211 },6211 },
6212 else => return null,6212 else => return null,
...@@ -6782,7 +6782,7 @@ fn analyzeCall(...@@ -6782,7 +6782,7 @@ fn analyzeCall(
6782 }),6782 }),
6783 .func => |function| function.index,6783 .func => |function| function.index,
6784 .ptr => |ptr| switch (ptr.addr) {6784 .ptr => |ptr| switch (ptr.addr) {
6785 .decl => |decl| mod.declPtr(decl).getFunctionIndex(mod).unwrap().?,6785 .decl => |decl| mod.declPtr(decl).val.getFunctionIndex(mod).unwrap().?,
6786 else => {6786 else => {
6787 assert(callee_ty.isPtrAtRuntime(mod));6787 assert(callee_ty.isPtrAtRuntime(mod));
6788 return sema.fail(block, call_src, "{s} call of function pointer", .{6788 return sema.fail(block, call_src, "{s} call of function pointer", .{
...@@ -7403,7 +7403,7 @@ fn instantiateGenericCall(...@@ -7403,7 +7403,7 @@ fn instantiateGenericCall(
7403 const func_val = try sema.resolveConstValue(block, func_src, func, "generic function being called must be comptime-known");7403 const func_val = try sema.resolveConstValue(block, func_src, func, "generic function being called must be comptime-known");
7404 const module_fn = mod.funcPtr(switch (mod.intern_pool.indexToKey(func_val.toIntern())) {7404 const module_fn = mod.funcPtr(switch (mod.intern_pool.indexToKey(func_val.toIntern())) {
7405 .func => |function| function.index,7405 .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().?,
7407 else => unreachable,7407 else => unreachable,
7408 });7408 });
7409 // Check the Module's generic function map with an adapted context, so that we7409 // Check the Module's generic function map with an adapted context, so that we
...@@ -28336,7 +28336,7 @@ fn beginComptimePtrLoad(...@@ -28336,7 +28336,7 @@ fn beginComptimePtrLoad(
28336 const is_mutable = ptr.addr == .mut_decl;28336 const is_mutable = ptr.addr == .mut_decl;
28337 const decl = mod.declPtr(decl_index);28337 const decl = mod.declPtr(decl_index);
28338 const decl_tv = try decl.typedValue();28338 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
28341 const layout_defined = decl.ty.hasWellDefinedLayout(mod);28341 const layout_defined = decl.ty.hasWellDefinedLayout(mod);
28342 break :blk ComptimePtrLoadKit{28342 break :blk ComptimePtrLoadKit{
...@@ -29423,7 +29423,7 @@ fn analyzeDeclRefInner(sema: *Sema, decl_index: Decl.Index, analyze_fn_body: boo...@@ -29423,7 +29423,7 @@ fn analyzeDeclRefInner(sema: *Sema, decl_index: Decl.Index, analyze_fn_body: boo
29423 const ptr_ty = try mod.ptrType(.{29423 const ptr_ty = try mod.ptrType(.{
29424 .elem_type = decl_tv.ty.toIntern(),29424 .elem_type = decl_tv.ty.toIntern(),
29425 .alignment = InternPool.Alignment.fromByteUnits(decl.@"align"),29425 .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,
29427 .address_space = decl.@"addrspace",29427 .address_space = decl.@"addrspace",
29428 });29428 });
29429 if (analyze_fn_body) {29429 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...@@ -2210,7 +2210,7 @@ fn airCall(func: *CodeGen, inst: Air.Inst.Index, modifier: std.builtin.CallModif
2210 try func.bin_file.addOrUpdateImport(2210 try func.bin_file.addOrUpdateImport(
2211 mem.sliceTo(ext_decl.name, 0),2211 mem.sliceTo(ext_decl.name, 0),
2212 atom.getSymbolIndex().?,2212 atom.getSymbolIndex().?,
2213 mod.intern_pool.stringToSliceUnwrap(ext_decl.getExternFunc(mod).?.lib_name),2213 mod.intern_pool.stringToSliceUnwrap(ext_decl.getOwnedExternFunc(mod).?.lib_name),
2214 type_index,2214 type_index,
2215 );2215 );
2216 break :blk extern_func.decl;2216 break :blk extern_func.decl;
src/codegen/c.zig+6-6
...@@ -549,12 +549,12 @@ pub const DeclGen = struct {...@@ -549,12 +549,12 @@ pub const DeclGen = struct {
549 }549 }
550550
551 // Chase function values in order to be able to reference the original function.551 // 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)
553 return dg.renderDeclValue(writer, ty, val, func.owner_decl, location);553 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)
555 return dg.renderDeclValue(writer, ty, val, extern_func.decl, location);555 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
559 // We shouldn't cast C function pointers as this is UB (when you call559 // We shouldn't cast C function pointers as this is UB (when you call
560 // them). The analysis until now should ensure that the C function560 // them). The analysis until now should ensure that the C function
...@@ -1580,7 +1580,7 @@ pub const DeclGen = struct {...@@ -1580,7 +1580,7 @@ pub const DeclGen = struct {
1580 else => unreachable,1580 else => unreachable,
1581 }1581 }
1582 }1582 }
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 ");
1584 if (fn_info.return_type == .noreturn_type) try w.writeAll("zig_noreturn ");1584 if (fn_info.return_type == .noreturn_type) try w.writeAll("zig_noreturn ");
15851585
1586 const trailing = try renderTypePrefix(1586 const trailing = try renderTypePrefix(
...@@ -2740,13 +2740,13 @@ pub fn genDecl(o: *Object) !void {...@@ -2740,13 +2740,13 @@ pub fn genDecl(o: *Object) !void {
2740 const tv: TypedValue = .{ .ty = decl.ty, .val = decl.val };2740 const tv: TypedValue = .{ .ty = decl.ty, .val = decl.val };
27412741
2742 if (!tv.ty.isFnOrHasRuntimeBitsIgnoreComptime(mod)) return;2742 if (!tv.ty.isFnOrHasRuntimeBitsIgnoreComptime(mod)) return;
2743 if (decl.getExternFunc(mod)) |_| {2743 if (tv.val.getExternFunc(mod)) |_| {
2744 const fwd_decl_writer = o.dg.fwd_decl.writer();2744 const fwd_decl_writer = o.dg.fwd_decl.writer();
2745 try fwd_decl_writer.writeAll("zig_extern ");2745 try fwd_decl_writer.writeAll("zig_extern ");
2746 try o.dg.renderFunctionSignature(fwd_decl_writer, decl_c_value.decl, .forward, .{ .export_index = 0 });2746 try o.dg.renderFunctionSignature(fwd_decl_writer, decl_c_value.decl, .forward, .{ .export_index = 0 });
2747 try fwd_decl_writer.writeAll(";\n");2747 try fwd_decl_writer.writeAll(";\n");
2748 try genExports(o);2748 try genExports(o);
2749 } else if (decl.getVariable(mod)) |variable| {2749 } else if (tv.val.getVariable(mod)) |variable| {
2750 try o.dg.renderFwdDecl(decl_c_value.decl, variable);2750 try o.dg.renderFwdDecl(decl_c_value.decl, variable);
2751 try genExports(o);2751 try genExports(o);
27522752
src/codegen/llvm.zig+12-12
...@@ -1165,7 +1165,7 @@ pub const Object = struct {...@@ -1165,7 +1165,7 @@ pub const Object = struct {
1165 di_file = try dg.object.getDIFile(gpa, mod.namespacePtr(decl.src_namespace).file_scope);1165 di_file = try dg.object.getDIFile(gpa, mod.namespacePtr(decl.src_namespace).file_scope);
11661166
1167 const line_number = decl.src_line + 1;1167 const line_number = decl.src_line + 1;
1168 const is_internal_linkage = decl.getExternFunc(mod) == null and1168 const is_internal_linkage = decl.val.getExternFunc(mod) == null and
1169 !mod.decl_exports.contains(decl_index);1169 !mod.decl_exports.contains(decl_index);
1170 const noret_bit: c_uint = if (fn_info.return_type == .noreturn_type)1170 const noret_bit: c_uint = if (fn_info.return_type == .noreturn_type)
1171 llvm.DIFlags.NoReturn1171 llvm.DIFlags.NoReturn
...@@ -1274,7 +1274,7 @@ pub const Object = struct {...@@ -1274,7 +1274,7 @@ pub const Object = struct {
1274 var free_decl_name = false;1274 var free_decl_name = false;
1275 const decl_name = decl_name: {1275 const decl_name = decl_name: {
1276 if (mod.getTarget().isWasm() and try decl.isFunction(mod)) {1276 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| {
1278 if (!std.mem.eql(u8, lib_name, "c")) {1278 if (!std.mem.eql(u8, lib_name, "c")) {
1279 free_decl_name = true;1279 free_decl_name = true;
1280 break :decl_name try std.fmt.allocPrintZ(gpa, "{s}|{s}", .{ decl.name, lib_name });1280 break :decl_name try std.fmt.allocPrintZ(gpa, "{s}|{s}", .{ decl.name, lib_name });
...@@ -1306,7 +1306,7 @@ pub const Object = struct {...@@ -1306,7 +1306,7 @@ pub const Object = struct {
1306 di_global.replaceLinkageName(linkage_name);1306 di_global.replaceLinkageName(linkage_name);
1307 }1307 }
1308 }1308 }
1309 if (decl.getVariable(mod)) |variable| {1309 if (decl.val.getVariable(mod)) |variable| {
1310 if (variable.is_threadlocal) {1310 if (variable.is_threadlocal) {
1311 llvm_global.setThreadLocalMode(.GeneralDynamicTLSModel);1311 llvm_global.setThreadLocalMode(.GeneralDynamicTLSModel);
1312 } else {1312 } else {
...@@ -1348,7 +1348,7 @@ pub const Object = struct {...@@ -1348,7 +1348,7 @@ pub const Object = struct {
1348 defer gpa.free(section_z);1348 defer gpa.free(section_z);
1349 llvm_global.setSection(section_z);1349 llvm_global.setSection(section_z);
1350 }1350 }
1351 if (decl.getVariable(mod)) |variable| {1351 if (decl.val.getVariable(mod)) |variable| {
1352 if (variable.is_threadlocal) {1352 if (variable.is_threadlocal) {
1353 llvm_global.setThreadLocalMode(.GeneralDynamicTLSModel);1353 llvm_global.setThreadLocalMode(.GeneralDynamicTLSModel);
1354 }1354 }
...@@ -1382,7 +1382,7 @@ pub const Object = struct {...@@ -1382,7 +1382,7 @@ pub const Object = struct {
1382 llvm_global.setLinkage(.Internal);1382 llvm_global.setLinkage(.Internal);
1383 if (mod.wantDllExports()) llvm_global.setDLLStorageClass(.Default);1383 if (mod.wantDllExports()) llvm_global.setDLLStorageClass(.Default);
1384 llvm_global.setUnnamedAddr(.True);1384 llvm_global.setUnnamedAddr(.True);
1385 if (decl.getVariable(mod)) |variable| {1385 if (decl.val.getVariable(mod)) |variable| {
1386 const single_threaded = mod.comp.bin_file.options.single_threaded;1386 const single_threaded = mod.comp.bin_file.options.single_threaded;
1387 if (variable.is_threadlocal and !single_threaded) {1387 if (variable.is_threadlocal and !single_threaded) {
1388 llvm_global.setThreadLocalMode(.GeneralDynamicTLSModel);1388 llvm_global.setThreadLocalMode(.GeneralDynamicTLSModel);
...@@ -2452,7 +2452,7 @@ pub const DeclGen = struct {...@@ -2452,7 +2452,7 @@ pub const DeclGen = struct {
2452 log.debug("gen: {s} type: {}, value: {}", .{2452 log.debug("gen: {s} type: {}, value: {}", .{
2453 decl.name, decl.ty.fmtDebug(), decl.val.fmtDebug(),2453 decl.name, decl.ty.fmtDebug(), decl.val.fmtDebug(),
2454 });2454 });
2455 if (decl.getExternFunc(mod)) |extern_func| {2455 if (decl.val.getExternFunc(mod)) |extern_func| {
2456 _ = try dg.resolveLlvmFunction(extern_func.decl);2456 _ = try dg.resolveLlvmFunction(extern_func.decl);
2457 } else {2457 } else {
2458 const target = mod.getTarget();2458 const target = mod.getTarget();
...@@ -2460,7 +2460,7 @@ pub const DeclGen = struct {...@@ -2460,7 +2460,7 @@ pub const DeclGen = struct {
2460 global.setAlignment(decl.getAlignment(mod));2460 global.setAlignment(decl.getAlignment(mod));
2461 if (decl.@"linksection") |section| global.setSection(section);2461 if (decl.@"linksection") |section| global.setSection(section);
2462 assert(decl.has_tv);2462 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: {
2464 break :init_val variable.init.toValue();2464 break :init_val variable.init.toValue();
2465 } else init_val: {2465 } else init_val: {
2466 global.setGlobalConstant(.True);2466 global.setGlobalConstant(.True);
...@@ -2555,7 +2555,7 @@ pub const DeclGen = struct {...@@ -2555,7 +2555,7 @@ pub const DeclGen = struct {
2555 } else {2555 } else {
2556 if (target.isWasm()) {2556 if (target.isWasm()) {
2557 dg.addFnAttrString(llvm_fn, "wasm-import-name", std.mem.sliceTo(decl.name, 0));2557 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| {
2559 if (!std.mem.eql(u8, lib_name, "c")) {2559 if (!std.mem.eql(u8, lib_name, "c")) {
2560 dg.addFnAttrString(llvm_fn, "wasm-import-module", lib_name);2560 dg.addFnAttrString(llvm_fn, "wasm-import-module", lib_name);
2561 }2561 }
...@@ -2716,7 +2716,7 @@ pub const DeclGen = struct {...@@ -2716,7 +2716,7 @@ pub const DeclGen = struct {
2716 llvm_global.setValueName(decl.name);2716 llvm_global.setValueName(decl.name);
2717 llvm_global.setUnnamedAddr(.False);2717 llvm_global.setUnnamedAddr(.False);
2718 llvm_global.setLinkage(.External);2718 llvm_global.setLinkage(.External);
2719 if (decl.getVariable(mod)) |variable| {2719 if (decl.val.getVariable(mod)) |variable| {
2720 const single_threaded = mod.comp.bin_file.options.single_threaded;2720 const single_threaded = mod.comp.bin_file.options.single_threaded;
2721 if (variable.is_threadlocal and !single_threaded) {2721 if (variable.is_threadlocal and !single_threaded) {
2722 llvm_global.setThreadLocalMode(.GeneralDynamicTLSModel);2722 llvm_global.setThreadLocalMode(.GeneralDynamicTLSModel);
...@@ -3993,11 +3993,11 @@ pub const DeclGen = struct {...@@ -3993,11 +3993,11 @@ pub const DeclGen = struct {
3993 // ... &bar;3993 // ... &bar;
3994 // `bar` is just an alias and we actually want to lower a reference to `foo`.3994 // `bar` is just an alias and we actually want to lower a reference to `foo`.
3995 const decl = mod.declPtr(decl_index);3995 const decl = mod.declPtr(decl_index);
3996 if (decl.getFunction(mod)) |func| {3996 if (decl.val.getFunction(mod)) |func| {
3997 if (func.owner_decl != decl_index) {3997 if (func.owner_decl != decl_index) {
3998 return self.lowerDeclRefValue(tv, func.owner_decl);3998 return self.lowerDeclRefValue(tv, func.owner_decl);
3999 }3999 }
4000 } else if (decl.getExternFunc(mod)) |func| {4000 } else if (decl.val.getExternFunc(mod)) |func| {
4001 if (func.decl != decl_index) {4001 if (func.decl != decl_index) {
4002 return self.lowerDeclRefValue(tv, func.decl);4002 return self.lowerDeclRefValue(tv, func.decl);
4003 }4003 }
...@@ -7939,7 +7939,7 @@ pub const FuncGen = struct {...@@ -7939,7 +7939,7 @@ pub const FuncGen = struct {
7939 }7939 }
79407940
7941 const src_index = self.air.instructions.items(.data)[inst].arg.src_index;7941 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).?;
7943 const lbrace_line = mod.declPtr(func.owner_decl).src_line + func.lbrace_line + 1;7943 const lbrace_line = mod.declPtr(func.owner_decl).src_line + func.lbrace_line + 1;
7944 const lbrace_col = func.lbrace_column + 1;7944 const lbrace_col = func.lbrace_column + 1;
7945 const di_local_var = dib.createParameterVariable(7945 const di_local_var = dib.createParameterVariable(
src/codegen/spirv.zig+3-3
...@@ -261,7 +261,7 @@ pub const DeclGen = struct {...@@ -261,7 +261,7 @@ pub const DeclGen = struct {
261 const entry = try self.decl_link.getOrPut(decl_index);261 const entry = try self.decl_link.getOrPut(decl_index);
262 if (!entry.found_existing) {262 if (!entry.found_existing) {
263 // TODO: Extern fn?263 // 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)
265 .func265 .func
266 else266 else
267 .global;267 .global;
...@@ -1544,7 +1544,7 @@ pub const DeclGen = struct {...@@ -1544,7 +1544,7 @@ pub const DeclGen = struct {
1544 const decl_id = self.spv.declPtr(spv_decl_index).result_id;1544 const decl_id = self.spv.declPtr(spv_decl_index).result_id;
1545 log.debug("genDecl: id = {}, index = {}, name = {s}", .{ decl_id.id, @enumToInt(spv_decl_index), decl.name });1545 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)) |_| {
1548 assert(decl.ty.zigTypeTag(mod) == .Fn);1548 assert(decl.ty.zigTypeTag(mod) == .Fn);
1549 const prototype_id = try self.resolveTypeId(decl.ty);1549 const prototype_id = try self.resolveTypeId(decl.ty);
1550 try self.func.prologue.emit(self.spv.gpa, .OpFunction, .{1550 try self.func.prologue.emit(self.spv.gpa, .OpFunction, .{
...@@ -1597,7 +1597,7 @@ pub const DeclGen = struct {...@@ -1597,7 +1597,7 @@ pub const DeclGen = struct {
1597 try self.generateTestEntryPoint(fqn, spv_decl_index);1597 try self.generateTestEntryPoint(fqn, spv_decl_index);
1598 }1598 }
1599 } else {1599 } else {
1600 const init_val = if (decl.getVariable(mod)) |payload|1600 const init_val = if (decl.val.getVariable(mod)) |payload|
1601 payload.init.toValue()1601 payload.init.toValue()
1602 else1602 else
1603 decl.val;1603 decl.val;
src/link/Coff.zig+5-5
...@@ -1156,10 +1156,10 @@ pub fn updateDecl(...@@ -1156,10 +1156,10 @@ pub fn updateDecl(
11561156
1157 const decl = mod.declPtr(decl_index);1157 const decl = mod.declPtr(decl_index);
11581158
1159 if (decl.getExternFunc(mod)) |_| {1159 if (decl.val.getExternFunc(mod)) |_| {
1160 return; // TODO Should we do more when front-end analyzed extern decl?1160 return; // TODO Should we do more when front-end analyzed extern decl?
1161 }1161 }
1162 if (decl.getVariable(mod)) |variable| {1162 if (decl.val.getVariable(mod)) |variable| {
1163 if (variable.is_extern) {1163 if (variable.is_extern) {
1164 return; // TODO Should we do more when front-end analyzed extern decl?1164 return; // TODO Should we do more when front-end analyzed extern decl?
1165 }1165 }
...@@ -1172,7 +1172,7 @@ pub fn updateDecl(...@@ -1172,7 +1172,7 @@ pub fn updateDecl(
1172 var code_buffer = std.ArrayList(u8).init(self.base.allocator);1172 var code_buffer = std.ArrayList(u8).init(self.base.allocator);
1173 defer code_buffer.deinit();1173 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;
1176 const res = try codegen.generateSymbol(&self.base, decl.srcLoc(mod), .{1176 const res = try codegen.generateSymbol(&self.base, decl.srcLoc(mod), .{
1177 .ty = decl.ty,1177 .ty = decl.ty,
1178 .val = decl_val,1178 .val = decl_val,
...@@ -1313,7 +1313,7 @@ fn getDeclOutputSection(self: *Coff, decl_index: Module.Decl.Index) u16 {...@@ -1313,7 +1313,7 @@ fn getDeclOutputSection(self: *Coff, decl_index: Module.Decl.Index) u16 {
1313 // TODO: what if this is a function pointer?1313 // TODO: what if this is a function pointer?
1314 .Fn => break :blk self.text_section_index.?,1314 .Fn => break :blk self.text_section_index.?,
1315 else => {1315 else => {
1316 if (decl.getVariable(mod)) |_| {1316 if (val.getVariable(mod)) |_| {
1317 break :blk self.data_section_index.?;1317 break :blk self.data_section_index.?;
1318 }1318 }
1319 break :blk self.rdata_section_index.?;1319 break :blk self.rdata_section_index.?;
...@@ -1425,7 +1425,7 @@ pub fn updateDeclExports(...@@ -1425,7 +1425,7 @@ pub fn updateDeclExports(
1425 // detect the default subsystem.1425 // detect the default subsystem.
1426 for (exports) |exp| {1426 for (exports) |exp| {
1427 const exported_decl = mod.declPtr(exp.exported_decl);1427 const exported_decl = mod.declPtr(exp.exported_decl);
1428 if (exported_decl.getFunctionIndex(mod) == .none) continue;1428 if (exported_decl.getOwnedFunctionIndex(mod) == .none) continue;
1429 const winapi_cc = switch (self.base.options.target.cpu.arch) {1429 const winapi_cc = switch (self.base.options.target.cpu.arch) {
1430 .x86 => std.builtin.CallingConvention.Stdcall,1430 .x86 => std.builtin.CallingConvention.Stdcall,
1431 else => std.builtin.CallingConvention.C,1431 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)...@@ -971,7 +971,7 @@ pub fn initDeclState(self: *Dwarf, mod: *Module, decl_index: Module.Decl.Index)
971 // For functions we need to add a prologue to the debug line program.971 // For functions we need to add a prologue to the debug line program.
972 try dbg_line_buffer.ensureTotalCapacity(26);972 try dbg_line_buffer.ensureTotalCapacity(26);
973973
974 const func = decl.getFunction(mod).?;974 const func = decl.val.getFunction(mod).?;
975 log.debug("decl.src_line={d}, func.lbrace_line={d}, func.rbrace_line={d}", .{975 log.debug("decl.src_line={d}, func.lbrace_line={d}, func.rbrace_line={d}", .{
976 decl.src_line,976 decl.src_line,
977 func.lbrace_line,977 func.lbrace_line,
...@@ -1523,7 +1523,7 @@ pub fn updateDeclLineNumber(self: *Dwarf, mod: *Module, decl_index: Module.Decl....@@ -1523,7 +1523,7 @@ pub fn updateDeclLineNumber(self: *Dwarf, mod: *Module, decl_index: Module.Decl.
1523 if (atom.len == 0) return;1523 if (atom.len == 0) return;
15241524
1525 const decl = mod.declPtr(decl_index);1525 const decl = mod.declPtr(decl_index);
1526 const func = decl.getFunction(mod).?;1526 const func = decl.val.getFunction(mod).?;
1527 log.debug("decl.src_line={d}, func.lbrace_line={d}, func.rbrace_line={d}", .{1527 log.debug("decl.src_line={d}, func.lbrace_line={d}, func.rbrace_line={d}", .{
1528 decl.src_line,1528 decl.src_line,
1529 func.lbrace_line,1529 func.lbrace_line,
src/link/Elf.zig+4-4
...@@ -2465,7 +2465,7 @@ fn getDeclShdrIndex(self: *Elf, decl_index: Module.Decl.Index) u16 {...@@ -2465,7 +2465,7 @@ fn getDeclShdrIndex(self: *Elf, decl_index: Module.Decl.Index) u16 {
2465 // TODO: what if this is a function pointer?2465 // TODO: what if this is a function pointer?
2466 .Fn => break :blk self.text_section_index.?,2466 .Fn => break :blk self.text_section_index.?,
2467 else => {2467 else => {
2468 if (decl.getVariable(mod)) |_| {2468 if (val.getVariable(mod)) |_| {
2469 break :blk self.data_section_index.?;2469 break :blk self.data_section_index.?;
2470 }2470 }
2471 break :blk self.rodata_section_index.?;2471 break :blk self.rodata_section_index.?;
...@@ -2647,10 +2647,10 @@ pub fn updateDecl(...@@ -2647,10 +2647,10 @@ pub fn updateDecl(
26472647
2648 const decl = mod.declPtr(decl_index);2648 const decl = mod.declPtr(decl_index);
26492649
2650 if (decl.getExternFunc(mod)) |_| {2650 if (decl.val.getExternFunc(mod)) |_| {
2651 return; // TODO Should we do more when front-end analyzed extern decl?2651 return; // TODO Should we do more when front-end analyzed extern decl?
2652 }2652 }
2653 if (decl.getVariable(mod)) |variable| {2653 if (decl.val.getVariable(mod)) |variable| {
2654 if (variable.is_extern) {2654 if (variable.is_extern) {
2655 return; // TODO Should we do more when front-end analyzed extern decl?2655 return; // TODO Should we do more when front-end analyzed extern decl?
2656 }2656 }
...@@ -2667,7 +2667,7 @@ pub fn updateDecl(...@@ -2667,7 +2667,7 @@ pub fn updateDecl(
2667 defer if (decl_state) |*ds| ds.deinit();2667 defer if (decl_state) |*ds| ds.deinit();
26682668
2669 // TODO implement .debug_info for global variables2669 // 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;
2671 const res = if (decl_state) |*ds|2671 const res = if (decl_state) |*ds|
2672 try codegen.generateSymbol(&self.base, decl.srcLoc(mod), .{2672 try codegen.generateSymbol(&self.base, decl.srcLoc(mod), .{
2673 .ty = decl.ty,2673 .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...@@ -1984,16 +1984,16 @@ pub fn updateDecl(self: *MachO, mod: *Module, decl_index: Module.Decl.Index) !vo
19841984
1985 const decl = mod.declPtr(decl_index);1985 const decl = mod.declPtr(decl_index);
19861986
1987 if (decl.getExternFunc(mod)) |_| {1987 if (decl.val.getExternFunc(mod)) |_| {
1988 return; // TODO Should we do more when front-end analyzed extern decl?1988 return; // TODO Should we do more when front-end analyzed extern decl?
1989 }1989 }
1990 if (decl.getVariable(mod)) |variable| {1990 if (decl.val.getVariable(mod)) |variable| {
1991 if (variable.is_extern) {1991 if (variable.is_extern) {
1992 return; // TODO Should we do more when front-end analyzed extern decl?1992 return; // TODO Should we do more when front-end analyzed extern decl?
1993 }1993 }
1994 }1994 }
19951995
1996 const is_threadlocal = if (decl.getVariable(mod)) |variable|1996 const is_threadlocal = if (decl.val.getVariable(mod)) |variable|
1997 variable.is_threadlocal and !self.base.options.single_threaded1997 variable.is_threadlocal and !self.base.options.single_threaded
1998 else1998 else
1999 false;1999 false;
...@@ -2012,7 +2012,7 @@ pub fn updateDecl(self: *MachO, mod: *Module, decl_index: Module.Decl.Index) !vo...@@ -2012,7 +2012,7 @@ pub fn updateDecl(self: *MachO, mod: *Module, decl_index: Module.Decl.Index) !vo
2012 null;2012 null;
2013 defer if (decl_state) |*ds| ds.deinit();2013 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;
2016 const res = if (decl_state) |*ds|2016 const res = if (decl_state) |*ds|
2017 try codegen.generateSymbol(&self.base, decl.srcLoc(mod), .{2017 try codegen.generateSymbol(&self.base, decl.srcLoc(mod), .{
2018 .ty = decl.ty,2018 .ty = decl.ty,
...@@ -2177,7 +2177,7 @@ fn updateThreadlocalVariable(self: *MachO, module: *Module, decl_index: Module.D...@@ -2177,7 +2177,7 @@ fn updateThreadlocalVariable(self: *MachO, module: *Module, decl_index: Module.D
21772177
2178 const decl = module.declPtr(decl_index);2178 const decl = module.declPtr(decl_index);
2179 const decl_metadata = self.decls.get(decl_index).?;2179 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();
2181 const res = if (decl_state) |*ds|2181 const res = if (decl_state) |*ds|
2182 try codegen.generateSymbol(&self.base, decl.srcLoc(mod), .{2182 try codegen.generateSymbol(&self.base, decl.srcLoc(mod), .{
2183 .ty = decl.ty,2183 .ty = decl.ty,
...@@ -2278,7 +2278,7 @@ fn getDeclOutputSection(self: *MachO, decl_index: Module.Decl.Index) u8 {...@@ -2278,7 +2278,7 @@ fn getDeclOutputSection(self: *MachO, decl_index: Module.Decl.Index) u8 {
2278 }2278 }
2279 }2279 }
22802280
2281 if (decl.getVariable(mod)) |variable| {2281 if (val.getVariable(mod)) |variable| {
2282 if (variable.is_threadlocal and !single_threaded) {2282 if (variable.is_threadlocal and !single_threaded) {
2283 break :blk self.thread_data_section_index.?;2283 break :blk self.thread_data_section_index.?;
2284 }2284 }
...@@ -2289,7 +2289,7 @@ fn getDeclOutputSection(self: *MachO, decl_index: Module.Decl.Index) u8 {...@@ -2289,7 +2289,7 @@ fn getDeclOutputSection(self: *MachO, decl_index: Module.Decl.Index) u8 {
2289 // TODO: what if this is a function pointer?2289 // TODO: what if this is a function pointer?
2290 .Fn => break :blk self.text_section_index.?,2290 .Fn => break :blk self.text_section_index.?,
2291 else => {2291 else => {
2292 if (decl.getVariable(mod)) |_| {2292 if (val.getVariable(mod)) |_| {
2293 break :blk self.data_section_index.?;2293 break :blk self.data_section_index.?;
2294 }2294 }
2295 break :blk self.data_const_section_index.?;2295 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...@@ -392,10 +392,10 @@ pub fn lowerUnnamedConst(self: *Plan9, tv: TypedValue, decl_index: Module.Decl.I
392pub fn updateDecl(self: *Plan9, mod: *Module, decl_index: Module.Decl.Index) !void {392pub fn updateDecl(self: *Plan9, mod: *Module, decl_index: Module.Decl.Index) !void {
393 const decl = mod.declPtr(decl_index);393 const decl = mod.declPtr(decl_index);
394394
395 if (decl.getExternFunc(mod)) |_| {395 if (decl.val.getExternFunc(mod)) |_| {
396 return; // TODO Should we do more when front-end analyzed extern decl?396 return; // TODO Should we do more when front-end analyzed extern decl?
397 }397 }
398 if (decl.getVariable(mod)) |variable| {398 if (decl.val.getVariable(mod)) |variable| {
399 if (variable.is_extern) {399 if (variable.is_extern) {
400 return; // TODO Should we do more when front-end analyzed extern decl?400 return; // TODO Should we do more when front-end analyzed extern decl?
401 }401 }
...@@ -407,7 +407,7 @@ pub fn updateDecl(self: *Plan9, mod: *Module, decl_index: Module.Decl.Index) !vo...@@ -407,7 +407,7 @@ pub fn updateDecl(self: *Plan9, mod: *Module, decl_index: Module.Decl.Index) !vo
407407
408 var code_buffer = std.ArrayList(u8).init(self.base.allocator);408 var code_buffer = std.ArrayList(u8).init(self.base.allocator);
409 defer code_buffer.deinit();409 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;
411 // TODO we need the symbol index for symbol in the table of locals for the containing atom411 // TODO we need the symbol index for symbol in the table of locals for the containing atom
412 const res = try codegen.generateSymbol(&self.base, decl.srcLoc(mod), .{412 const res = try codegen.generateSymbol(&self.base, decl.srcLoc(mod), .{
413 .ty = decl.ty,413 .ty = decl.ty,
...@@ -771,7 +771,7 @@ pub fn freeDecl(self: *Plan9, decl_index: Module.Decl.Index) void {...@@ -771,7 +771,7 @@ pub fn freeDecl(self: *Plan9, decl_index: Module.Decl.Index) void {
771 // in the deleteUnusedDecl function.771 // in the deleteUnusedDecl function.
772 const mod = self.base.options.module.?;772 const mod = self.base.options.module.?;
773 const decl = mod.declPtr(decl_index);773 const decl = mod.declPtr(decl_index);
774 const is_fn = decl.getFunctionIndex(mod) != .none;774 const is_fn = decl.val.getFunctionIndex(mod) != .none;
775 if (is_fn) {775 if (is_fn) {
776 var symidx_and_submap = self.fn_decl_table.get(decl.getFileScope(mod)).?;776 var symidx_and_submap = self.fn_decl_table.get(decl.getFileScope(mod)).?;
777 var submap = symidx_and_submap.functions;777 var submap = symidx_and_submap.functions;
src/link/SpirV.zig+1-1
...@@ -138,7 +138,7 @@ pub fn updateDeclExports(...@@ -138,7 +138,7 @@ pub fn updateDeclExports(
138 exports: []const *Module.Export,138 exports: []const *Module.Export,
139) !void {139) !void {
140 const decl = mod.declPtr(decl_index);140 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) {
142 // TODO: Unify with resolveDecl in spirv.zig.142 // TODO: Unify with resolveDecl in spirv.zig.
143 const entry = try self.decl_link.getOrPut(decl_index);143 const entry = try self.decl_link.getOrPut(decl_index);
144 if (!entry.found_existing) {144 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...@@ -1404,9 +1404,9 @@ pub fn updateDecl(wasm: *Wasm, mod: *Module, decl_index: Module.Decl.Index) !voi
1404 defer tracy.end();1404 defer tracy.end();
14051405
1406 const decl = mod.declPtr(decl_index);1406 const decl = mod.declPtr(decl_index);
1407 if (decl.getFunction(mod)) |_| {1407 if (decl.val.getFunction(mod)) |_| {
1408 return;1408 return;
1409 } else if (decl.getExternFunc(mod)) |_| {1409 } else if (decl.val.getExternFunc(mod)) |_| {
1410 return;1410 return;
1411 }1411 }
14121412
...@@ -1415,12 +1415,12 @@ pub fn updateDecl(wasm: *Wasm, mod: *Module, decl_index: Module.Decl.Index) !voi...@@ -1415,12 +1415,12 @@ pub fn updateDecl(wasm: *Wasm, mod: *Module, decl_index: Module.Decl.Index) !voi
1415 atom.clear();1415 atom.clear();
14161416
1417 if (decl.isExtern(mod)) {1417 if (decl.isExtern(mod)) {
1418 const variable = decl.getVariable(mod).?;1418 const variable = decl.getOwnedVariable(mod).?;
1419 const name = mem.sliceTo(decl.name, 0);1419 const name = mem.sliceTo(decl.name, 0);
1420 const lib_name = mod.intern_pool.stringToSliceUnwrap(variable.lib_name);1420 const lib_name = mod.intern_pool.stringToSliceUnwrap(variable.lib_name);
1421 return wasm.addOrUpdateImport(name, atom.sym_index, lib_name, null);1421 return wasm.addOrUpdateImport(name, atom.sym_index, lib_name, null);
1422 }1422 }
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
1425 var code_writer = std.ArrayList(u8).init(wasm.base.allocator);1425 var code_writer = std.ArrayList(u8).init(wasm.base.allocator);
1426 defer code_writer.deinit();1426 defer code_writer.deinit();
...@@ -3373,7 +3373,7 @@ pub fn flushModule(wasm: *Wasm, comp: *Compilation, prog_node: *std.Progress.Nod...@@ -3373,7 +3373,7 @@ pub fn flushModule(wasm: *Wasm, comp: *Compilation, prog_node: *std.Progress.Nod
3373 const atom = wasm.getAtomPtr(atom_index);3373 const atom = wasm.getAtomPtr(atom_index);
3374 if (decl.ty.zigTypeTag(mod) == .Fn) {3374 if (decl.ty.zigTypeTag(mod) == .Fn) {
3375 try wasm.parseAtom(atom_index, .function);3375 try wasm.parseAtom(atom_index, .function);
3376 } else if (decl.getVariable(mod)) |variable| {3376 } else if (decl.getOwnedVariable(mod)) |variable| {
3377 if (variable.is_const) {3377 if (variable.is_const) {
3378 try wasm.parseAtom(atom_index, .{ .data = .read_only });3378 try wasm.parseAtom(atom_index, .{ .data = .read_only });
3379 } else if (variable.init.toValue().isUndefDeep(mod)) {3379 } else if (variable.init.toValue().isUndefDeep(mod)) {