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.
This commit is contained in:
committed by
Andrew Kelley
parent
d5f0ee0d62
commit
9cd0ca9f48
@@ -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);
|
||||
|
||||
|
||||
@@ -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(
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user