Skip to content

Commit 800c67b

Browse files
committed
Add function to extract the symbol name from the attributes
1 parent 4dbafc3 commit 800c67b

1 file changed

Lines changed: 30 additions & 17 deletions

File tree

  • compiler/rustc_symbol_mangling/src

compiler/rustc_symbol_mangling/src/lib.rs

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ use rustc_hir::def_id::{CrateNum, LOCAL_CRATE};
9292
use rustc_middle::middle::codegen_fn_attrs::{CodegenFnAttrFlags, CodegenFnAttrs};
9393
use rustc_middle::mono::{InstantiationMode, MonoItem};
9494
use rustc_middle::query::Providers;
95-
use rustc_middle::ty::{self, Instance, TyCtxt};
95+
use rustc_middle::ty::{self, Instance, InstanceKind, TyCtxt};
9696
use rustc_session::config::SymbolManglingVersion;
9797
use tracing::debug;
9898

@@ -149,29 +149,22 @@ pub fn typeid_for_trait_ref<'tcx>(
149149
v0::mangle_typeid_for_trait_ref(tcx, trait_ref)
150150
}
151151

152-
/// Computes the symbol name for the given instance. This function will call
153-
/// `compute_instantiating_crate` if it needs to factor the instantiating crate
154-
/// into the symbol name.
155-
fn compute_symbol_name<'tcx>(
152+
pub fn symbol_name_from_attrs<'tcx>(
156153
tcx: TyCtxt<'tcx>,
157-
instance: Instance<'tcx>,
158-
compute_instantiating_crate: impl FnOnce() -> CrateNum,
159-
) -> String {
160-
let def_id = instance.def_id();
161-
let args = instance.args;
162-
163-
debug!("symbol_name(def_id={:?}, args={:?})", def_id, args);
154+
instance_kind: InstanceKind<'tcx>,
155+
) -> Option<String> {
156+
let def_id = instance_kind.def_id();
164157

165158
if let Some(def_id) = def_id.as_local() {
166159
if tcx.proc_macro_decls_static(()) == Some(def_id) {
167160
let stable_crate_id = tcx.stable_crate_id(LOCAL_CRATE);
168-
return tcx.sess.generate_proc_macro_decls_symbol(stable_crate_id);
161+
return Some(tcx.sess.generate_proc_macro_decls_symbol(stable_crate_id));
169162
}
170163
}
171164

172165
// FIXME(eddyb) Precompute a custom symbol name based on attributes.
173166
let attrs = if tcx.def_kind(def_id).has_codegen_attrs() {
174-
&tcx.codegen_instance_attrs(instance.def)
167+
&tcx.codegen_instance_attrs(instance_kind)
175168
} else {
176169
CodegenFnAttrs::EMPTY
177170
};
@@ -197,7 +190,7 @@ fn compute_symbol_name<'tcx>(
197190
// legacy symbol mangling scheme.
198191
let name = if let Some(name) = attrs.symbol_name { name } else { tcx.item_name(def_id) };
199192

200-
return v0::mangle_internal_symbol(tcx, name.as_str());
193+
return Some(v0::mangle_internal_symbol(tcx, name.as_str()));
201194
}
202195

203196
let wasm_import_module_exception_force_mangling = {
@@ -225,15 +218,35 @@ fn compute_symbol_name<'tcx>(
225218
if !wasm_import_module_exception_force_mangling {
226219
if let Some(name) = attrs.symbol_name {
227220
// Use provided name
228-
return name.to_string();
221+
return Some(name.to_string());
229222
}
230223

231224
if attrs.flags.contains(CodegenFnAttrFlags::NO_MANGLE) {
232225
// Don't mangle
233-
return tcx.item_name(def_id).to_string();
226+
return Some(tcx.item_name(def_id).to_string());
234227
}
235228
}
236229

230+
None
231+
}
232+
233+
/// Computes the symbol name for the given instance. This function will call
234+
/// `compute_instantiating_crate` if it needs to factor the instantiating crate
235+
/// into the symbol name.
236+
fn compute_symbol_name<'tcx>(
237+
tcx: TyCtxt<'tcx>,
238+
instance: Instance<'tcx>,
239+
compute_instantiating_crate: impl FnOnce() -> CrateNum,
240+
) -> String {
241+
let def_id = instance.def_id();
242+
let args = instance.args;
243+
244+
debug!("symbol_name(def_id={:?}, args={:?})", def_id, args);
245+
246+
if let Some(symbol) = symbol_name_from_attrs(tcx, instance.def) {
247+
return symbol;
248+
}
249+
237250
// If we're dealing with an instance of a function that's inlined from
238251
// another crate but we're marking it as globally shared to our
239252
// compilation (aka we're not making an internal copy in each of our

0 commit comments

Comments
 (0)