2016-02-14 12:01:44 -06:00
|
|
|
//! The Rust Linkage Model and Symbol Names
|
|
|
|
//! =======================================
|
|
|
|
//!
|
|
|
|
//! The semantic model of Rust linkage is, broadly, that "there's no global
|
|
|
|
//! namespace" between crates. Our aim is to preserve the illusion of this
|
|
|
|
//! model despite the fact that it's not *quite* possible to implement on
|
|
|
|
//! modern linkers. We initially didn't use system linkers at all, but have
|
|
|
|
//! been convinced of their utility.
|
|
|
|
//!
|
|
|
|
//! There are a few issues to handle:
|
|
|
|
//!
|
|
|
|
//! - Linkers operate on a flat namespace, so we have to flatten names.
|
|
|
|
//! We do this using the C++ namespace-mangling technique. Foo::bar
|
|
|
|
//! symbols and such.
|
|
|
|
//!
|
|
|
|
//! - Symbols for distinct items with the same *name* need to get different
|
|
|
|
//! linkage-names. Examples of this are monomorphizations of functions or
|
|
|
|
//! items within anonymous scopes that end up having the same path.
|
|
|
|
//!
|
|
|
|
//! - Symbols in different crates but with same names "within" the crate need
|
|
|
|
//! to get different linkage-names.
|
|
|
|
//!
|
|
|
|
//! - Symbol names should be deterministic: Two consecutive runs of the
|
|
|
|
//! compiler over the same code base should produce the same symbol names for
|
|
|
|
//! the same items.
|
|
|
|
//!
|
|
|
|
//! - Symbol names should not depend on any global properties of the code base,
|
|
|
|
//! so that small modifications to the code base do not result in all symbols
|
|
|
|
//! changing. In previous versions of the compiler, symbol names incorporated
|
|
|
|
//! the SVH (Stable Version Hash) of the crate. This scheme turned out to be
|
|
|
|
//! infeasible when used in conjunction with incremental compilation because
|
|
|
|
//! small code changes would invalidate all symbols generated previously.
|
|
|
|
//!
|
|
|
|
//! - Even symbols from different versions of the same crate should be able to
|
|
|
|
//! live next to each other without conflict.
|
|
|
|
//!
|
|
|
|
//! In order to fulfill the above requirements the following scheme is used by
|
|
|
|
//! the compiler:
|
|
|
|
//!
|
|
|
|
//! The main tool for avoiding naming conflicts is the incorporation of a 64-bit
|
|
|
|
//! hash value into every exported symbol name. Anything that makes a difference
|
|
|
|
//! to the symbol being named, but does not show up in the regular path needs to
|
|
|
|
//! be fed into this hash:
|
|
|
|
//!
|
|
|
|
//! - Different monomorphizations of the same item have the same path but differ
|
|
|
|
//! in their concrete type parameters, so these parameters are part of the
|
|
|
|
//! data being digested for the symbol hash.
|
|
|
|
//!
|
|
|
|
//! - Rust allows items to be defined in anonymous scopes, such as in
|
|
|
|
//! `fn foo() { { fn bar() {} } { fn bar() {} } }`. Both `bar` functions have
|
|
|
|
//! the path `foo::bar`, since the anonymous scopes do not contribute to the
|
|
|
|
//! path of an item. The compiler already handles this case via so-called
|
|
|
|
//! disambiguating `DefPaths` which use indices to distinguish items with the
|
|
|
|
//! same name. The DefPaths of the functions above are thus `foo[0]::bar[0]`
|
|
|
|
//! and `foo[0]::bar[1]`. In order to incorporate this disambiguation
|
|
|
|
//! information into the symbol name too, these indices are fed into the
|
|
|
|
//! symbol hash, so that the above two symbols would end up with different
|
|
|
|
//! hash values.
|
|
|
|
//!
|
|
|
|
//! The two measures described above suffice to avoid intra-crate conflicts. In
|
|
|
|
//! order to also avoid inter-crate conflicts two more measures are taken:
|
|
|
|
//!
|
|
|
|
//! - The name of the crate containing the symbol is prepended to the symbol
|
2018-11-26 20:59:49 -06:00
|
|
|
//! name, i.e., symbols are "crate qualified". For example, a function `foo` in
|
2016-02-14 12:01:44 -06:00
|
|
|
//! module `bar` in crate `baz` would get a symbol name like
|
|
|
|
//! `baz::bar::foo::{hash}` instead of just `bar::foo::{hash}`. This avoids
|
|
|
|
//! simple conflicts between functions from different crates.
|
|
|
|
//!
|
|
|
|
//! - In order to be able to also use symbols from two versions of the same
|
|
|
|
//! crate (which naturally also have the same name), a stronger measure is
|
2016-02-26 15:25:25 -06:00
|
|
|
//! required: The compiler accepts an arbitrary "disambiguator" value via the
|
2018-11-12 12:05:20 -06:00
|
|
|
//! `-C metadata` command-line argument. This disambiguator is then fed into
|
2016-02-26 15:25:25 -06:00
|
|
|
//! the symbol hash of every exported item. Consequently, the symbols in two
|
|
|
|
//! identical crates but with different disambiguators are not in conflict
|
|
|
|
//! with each other. This facility is mainly intended to be used by build
|
|
|
|
//! tools like Cargo.
|
2016-02-14 12:01:44 -06:00
|
|
|
//!
|
|
|
|
//! A note on symbol name stability
|
|
|
|
//! -------------------------------
|
|
|
|
//! Previous versions of the compiler resorted to feeding NodeIds into the
|
|
|
|
//! symbol hash in order to disambiguate between items with the same path. The
|
|
|
|
//! current version of the name generation algorithm takes great care not to do
|
|
|
|
//! that, since NodeIds are notoriously unstable: A small change to the
|
|
|
|
//! code base will offset all NodeIds after the change and thus, much as using
|
|
|
|
//! the SVH in the hash, invalidate an unbounded number of symbol names. This
|
|
|
|
//! makes re-using previously compiled code for incremental compilation
|
|
|
|
//! virtually impossible. Thus, symbol hash generation exclusively relies on
|
|
|
|
//! DefPaths which are much more robust in the face of changes to the code base.
|
|
|
|
|
2018-12-10 04:59:08 -06:00
|
|
|
use rustc::hir::def_id::{CrateNum, DefId, LOCAL_CRATE};
|
2018-08-25 09:56:16 -05:00
|
|
|
use rustc::hir::Node;
|
2018-08-23 02:33:32 -05:00
|
|
|
use rustc::hir::CodegenFnAttrFlags;
|
2019-02-03 04:59:37 -06:00
|
|
|
use rustc::hir::map::{DefPathData, DisambiguatedDefPathData};
|
2018-05-08 06:58:33 -05:00
|
|
|
use rustc::ich::NodeIdHashingMode;
|
2019-01-25 04:11:50 -06:00
|
|
|
use rustc::ty::print::{PrettyPrinter, Printer, Print};
|
2018-06-13 08:44:43 -05:00
|
|
|
use rustc::ty::query::Providers;
|
2019-01-24 11:52:43 -06:00
|
|
|
use rustc::ty::subst::{Kind, SubstsRef, UnpackedKind};
|
2018-05-08 08:38:29 -05:00
|
|
|
use rustc::ty::{self, Ty, TyCtxt, TypeFoldable};
|
2016-08-23 12:23:58 -05:00
|
|
|
use rustc::util::common::record_time;
|
2018-05-08 08:38:29 -05:00
|
|
|
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
|
|
|
|
use rustc_mir::monomorphize::item::{InstantiationMode, MonoItem, MonoItemExt};
|
|
|
|
use rustc_mir::monomorphize::Instance;
|
2016-02-14 12:01:44 -06:00
|
|
|
|
2019-05-13 23:55:15 -05:00
|
|
|
use syntax_pos::symbol::InternedString;
|
2017-04-20 10:38:35 -05:00
|
|
|
|
2019-02-08 06:06:07 -06:00
|
|
|
use log::debug;
|
|
|
|
|
2018-12-21 09:10:21 -06:00
|
|
|
use std::fmt::{self, Write};
|
|
|
|
use std::mem::{self, discriminant};
|
2016-02-14 12:01:44 -06:00
|
|
|
|
2019-02-08 06:06:07 -06:00
|
|
|
pub fn provide(providers: &mut Providers<'_>) {
|
2017-04-24 11:35:47 -05:00
|
|
|
*providers = Providers {
|
|
|
|
symbol_name,
|
2017-09-12 14:18:11 -05:00
|
|
|
|
2017-04-24 11:35:47 -05:00
|
|
|
..*providers
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-05-08 08:38:29 -05:00
|
|
|
fn get_symbol_hash<'a, 'tcx>(
|
|
|
|
tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
2016-03-24 12:19:36 -05:00
|
|
|
|
2018-05-08 08:38:29 -05:00
|
|
|
// the DefId of the item this name is for
|
|
|
|
def_id: DefId,
|
2017-10-06 16:59:33 -05:00
|
|
|
|
2018-05-08 08:38:29 -05:00
|
|
|
// instance this name will be for
|
|
|
|
instance: Instance<'tcx>,
|
2016-03-24 12:19:36 -05:00
|
|
|
|
2018-05-08 08:38:29 -05:00
|
|
|
// type of the item, without any generic
|
|
|
|
// parameters substituted; this is
|
|
|
|
// included in the hash as a kind of
|
|
|
|
// safeguard.
|
|
|
|
item_type: Ty<'tcx>,
|
2016-03-24 12:19:36 -05:00
|
|
|
|
2018-05-08 08:38:29 -05:00
|
|
|
// values for generic type parameters,
|
|
|
|
// if any.
|
2019-02-09 08:11:53 -06:00
|
|
|
substs: SubstsRef<'tcx>,
|
2018-05-08 08:38:29 -05:00
|
|
|
) -> u64 {
|
|
|
|
debug!(
|
|
|
|
"get_symbol_hash(def_id={:?}, parameters={:?})",
|
|
|
|
def_id, substs
|
|
|
|
);
|
2016-03-16 04:57:03 -05:00
|
|
|
|
2018-05-08 06:58:33 -05:00
|
|
|
let mut hasher = StableHasher::<u64>::new();
|
|
|
|
let mut hcx = tcx.create_stable_hashing_context();
|
2016-08-23 12:23:58 -05:00
|
|
|
|
2016-10-26 21:42:48 -05:00
|
|
|
record_time(&tcx.sess.perf_stats.symbol_hash_time, || {
|
2016-08-23 12:23:58 -05:00
|
|
|
// the main symbol name is not necessarily unique; hash in the
|
|
|
|
// compiler's internal def-path, guaranteeing each symbol has a
|
|
|
|
// truly unique path
|
2018-05-08 06:58:33 -05:00
|
|
|
tcx.def_path_hash(def_id).hash_stable(&mut hcx, &mut hasher);
|
2016-08-23 12:23:58 -05:00
|
|
|
|
|
|
|
// Include the main item-type. Note that, in this case, the
|
|
|
|
// assertions about `needs_subst` may not hold, but this item-type
|
|
|
|
// ought to be the same for every reference anyway.
|
|
|
|
assert!(!item_type.has_erasable_regions());
|
2018-05-08 06:58:33 -05:00
|
|
|
hcx.while_hashing_spans(false, |hcx| {
|
|
|
|
hcx.with_node_id_hashing_mode(NodeIdHashingMode::HashDefPath, |hcx| {
|
|
|
|
item_type.hash_stable(hcx, &mut hasher);
|
|
|
|
});
|
|
|
|
});
|
2017-05-13 09:11:52 -05:00
|
|
|
|
2018-05-08 18:57:45 -05:00
|
|
|
// If this is a function, we hash the signature as well.
|
|
|
|
// This is not *strictly* needed, but it may help in some
|
|
|
|
// situations, see the `run-make/a-b-a-linker-guard` test.
|
2018-08-21 19:35:02 -05:00
|
|
|
if let ty::FnDef(..) = item_type.sty {
|
2018-05-08 18:57:45 -05:00
|
|
|
item_type.fn_sig(tcx).hash_stable(&mut hcx, &mut hasher);
|
|
|
|
}
|
|
|
|
|
2016-08-23 12:23:58 -05:00
|
|
|
// also include any type parameters (for generic items)
|
2017-10-06 16:59:33 -05:00
|
|
|
assert!(!substs.has_erasable_regions());
|
|
|
|
assert!(!substs.needs_subst());
|
2018-05-08 06:58:33 -05:00
|
|
|
substs.hash_stable(&mut hcx, &mut hasher);
|
2017-10-06 16:59:33 -05:00
|
|
|
|
2019-02-19 19:11:10 -06:00
|
|
|
let is_generic = substs.non_erasable_generics().next().is_some();
|
2018-03-06 03:33:42 -06:00
|
|
|
let avoid_cross_crate_conflicts =
|
|
|
|
// If this is an instance of a generic function, we also hash in
|
|
|
|
// the ID of the instantiating crate. This avoids symbol conflicts
|
|
|
|
// in case the same instances is emitted in two crates of the same
|
|
|
|
// project.
|
|
|
|
is_generic ||
|
|
|
|
|
|
|
|
// If we're dealing with an instance of a function that's inlined from
|
|
|
|
// another crate but we're marking it as globally shared to our
|
|
|
|
// compliation (aka we're not making an internal copy in each of our
|
|
|
|
// codegen units) then this symbol may become an exported (but hidden
|
|
|
|
// visibility) symbol. This means that multiple crates may do the same
|
|
|
|
// and we want to be sure to avoid any symbol conflicts here.
|
|
|
|
match MonoItem::Fn(instance).instantiation_mode(tcx) {
|
|
|
|
InstantiationMode::GloballyShared { may_conflict: true } => true,
|
|
|
|
_ => false,
|
|
|
|
};
|
2017-10-06 16:59:33 -05:00
|
|
|
|
|
|
|
if avoid_cross_crate_conflicts {
|
2018-03-06 03:33:42 -06:00
|
|
|
let instantiating_crate = if is_generic {
|
2018-07-26 14:20:47 -05:00
|
|
|
if !def_id.is_local() && tcx.sess.opts.share_generics() {
|
2018-03-06 03:33:42 -06:00
|
|
|
// If we are re-using a monomorphization from another crate,
|
|
|
|
// we have to compute the symbol hash accordingly.
|
2018-05-08 08:38:29 -05:00
|
|
|
let upstream_monomorphizations = tcx.upstream_monomorphizations_for(def_id);
|
2018-03-06 03:33:42 -06:00
|
|
|
|
2018-05-08 08:38:29 -05:00
|
|
|
upstream_monomorphizations
|
|
|
|
.and_then(|monos| monos.get(&substs).cloned())
|
|
|
|
.unwrap_or(LOCAL_CRATE)
|
2018-03-06 03:33:42 -06:00
|
|
|
} else {
|
|
|
|
LOCAL_CRATE
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
LOCAL_CRATE
|
|
|
|
};
|
|
|
|
|
2018-05-08 08:38:29 -05:00
|
|
|
(&tcx.original_crate_name(instantiating_crate).as_str()[..])
|
|
|
|
.hash_stable(&mut hcx, &mut hasher);
|
2018-05-08 06:58:33 -05:00
|
|
|
(&tcx.crate_disambiguator(instantiating_crate)).hash_stable(&mut hcx, &mut hasher);
|
2016-08-18 00:32:50 -05:00
|
|
|
}
|
2018-09-10 09:01:46 -05:00
|
|
|
|
2018-09-26 10:14:38 -05:00
|
|
|
// We want to avoid accidental collision between different types of instances.
|
|
|
|
// Especially, VtableShim may overlap with its original instance without this.
|
|
|
|
discriminant(&instance.def).hash_stable(&mut hcx, &mut hasher);
|
2016-08-23 12:23:58 -05:00
|
|
|
});
|
2016-09-01 03:04:21 -05:00
|
|
|
|
2016-09-19 04:47:47 -05:00
|
|
|
// 64 bits should be enough to avoid collisions.
|
2017-04-24 12:32:11 -05:00
|
|
|
hasher.finish()
|
2016-02-14 12:01:44 -06:00
|
|
|
}
|
|
|
|
|
2019-03-21 11:06:04 -05:00
|
|
|
fn symbol_name(tcx: TyCtxt<'_, 'tcx, 'tcx>, instance: Instance<'tcx>) -> ty::SymbolName {
|
2018-05-08 08:38:29 -05:00
|
|
|
ty::SymbolName {
|
2019-03-21 11:06:04 -05:00
|
|
|
name: compute_symbol_name(tcx, instance),
|
2018-05-08 08:38:29 -05:00
|
|
|
}
|
2017-04-24 11:35:47 -05:00
|
|
|
}
|
|
|
|
|
2019-03-21 11:06:04 -05:00
|
|
|
fn compute_symbol_name(tcx: TyCtxt<'_, 'tcx, 'tcx>, instance: Instance<'tcx>) -> InternedString {
|
2017-02-08 11:31:03 -06:00
|
|
|
let def_id = instance.def_id();
|
|
|
|
let substs = instance.substs;
|
2016-02-14 12:01:44 -06:00
|
|
|
|
2018-05-08 08:38:29 -05:00
|
|
|
debug!("symbol_name(def_id={:?}, substs={:?})", def_id, substs);
|
2016-03-16 04:57:03 -05:00
|
|
|
|
2019-03-04 02:00:30 -06:00
|
|
|
let hir_id = tcx.hir().as_local_hir_id(def_id);
|
2016-05-24 17:34:17 -05:00
|
|
|
|
2019-01-12 18:06:50 -06:00
|
|
|
if def_id.is_local() {
|
|
|
|
if tcx.plugin_registrar_fn(LOCAL_CRATE) == Some(def_id) {
|
2017-04-14 14:30:06 -05:00
|
|
|
let disambiguator = tcx.sess.local_crate_disambiguator();
|
2019-05-13 23:55:15 -05:00
|
|
|
return
|
|
|
|
InternedString::intern(&tcx.sess.generate_plugin_registrar_symbol(disambiguator));
|
2016-02-14 12:01:44 -06:00
|
|
|
}
|
2019-01-12 14:49:18 -06:00
|
|
|
if tcx.proc_macro_decls_static(LOCAL_CRATE) == Some(def_id) {
|
2017-04-14 14:30:06 -05:00
|
|
|
let disambiguator = tcx.sess.local_crate_disambiguator();
|
2019-05-13 23:55:15 -05:00
|
|
|
return
|
|
|
|
InternedString::intern(&tcx.sess.generate_proc_macro_decls_symbol(disambiguator));
|
2016-05-12 11:52:38 -05:00
|
|
|
}
|
2017-02-08 11:31:03 -06:00
|
|
|
}
|
2016-05-24 17:34:17 -05:00
|
|
|
|
2017-02-08 11:31:03 -06:00
|
|
|
// FIXME(eddyb) Precompute a custom symbol name based on attributes.
|
2019-03-04 02:00:30 -06:00
|
|
|
let is_foreign = if let Some(id) = hir_id {
|
|
|
|
match tcx.hir().get_by_hir_id(id) {
|
2018-08-25 09:56:16 -05:00
|
|
|
Node::ForeignItem(_) => true,
|
2018-05-08 08:38:29 -05:00
|
|
|
_ => false,
|
2016-05-12 11:52:38 -05:00
|
|
|
}
|
2017-02-08 11:31:03 -06:00
|
|
|
} else {
|
2017-05-05 08:15:08 -05:00
|
|
|
tcx.is_foreign_item(def_id)
|
2017-02-08 11:31:03 -06:00
|
|
|
};
|
|
|
|
|
2018-08-23 02:33:32 -05:00
|
|
|
let attrs = tcx.codegen_fn_attrs(def_id);
|
2017-02-08 11:31:03 -06:00
|
|
|
if is_foreign {
|
2018-08-23 02:33:32 -05:00
|
|
|
if let Some(name) = attrs.link_name {
|
2019-03-21 11:06:04 -05:00
|
|
|
return name.as_interned_str();
|
2016-05-12 11:52:38 -05:00
|
|
|
}
|
2017-02-08 11:31:03 -06:00
|
|
|
// Don't mangle foreign items.
|
2019-05-14 15:32:44 -05:00
|
|
|
return tcx.item_name(def_id).as_interned_str();
|
2017-02-08 11:31:03 -06:00
|
|
|
}
|
2016-05-12 11:52:38 -05:00
|
|
|
|
2018-08-23 02:33:32 -05:00
|
|
|
if let Some(name) = &attrs.export_name {
|
2017-02-08 11:31:03 -06:00
|
|
|
// Use provided name
|
2019-03-21 11:06:04 -05:00
|
|
|
return name.as_interned_str();
|
2017-02-08 11:31:03 -06:00
|
|
|
}
|
|
|
|
|
2018-08-23 02:33:32 -05:00
|
|
|
if attrs.flags.contains(CodegenFnAttrFlags::NO_MANGLE) {
|
2017-02-08 11:31:03 -06:00
|
|
|
// Don't mangle
|
2019-05-14 15:32:44 -05:00
|
|
|
return tcx.item_name(def_id).as_interned_str();
|
2017-02-08 11:31:03 -06:00
|
|
|
}
|
2016-05-12 11:52:38 -05:00
|
|
|
|
2017-02-08 11:31:03 -06:00
|
|
|
// We want to compute the "type" of this item. Unfortunately, some
|
|
|
|
// kinds of items (e.g., closures) don't have an entry in the
|
|
|
|
// item-type array. So walk back up the find the closest parent
|
|
|
|
// that DOES have an entry.
|
|
|
|
let mut ty_def_id = def_id;
|
|
|
|
let instance_ty;
|
|
|
|
loop {
|
2017-04-14 14:30:06 -05:00
|
|
|
let key = tcx.def_key(ty_def_id);
|
2017-02-08 11:31:03 -06:00
|
|
|
match key.disambiguated_data.data {
|
2018-05-08 08:38:29 -05:00
|
|
|
DefPathData::TypeNs(_) | DefPathData::ValueNs(_) => {
|
2017-04-24 07:20:46 -05:00
|
|
|
instance_ty = tcx.type_of(ty_def_id);
|
2017-02-08 11:31:03 -06:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
// if we're making a symbol for something, there ought
|
|
|
|
// to be a value or type-def or something in there
|
|
|
|
// *somewhere*
|
|
|
|
ty_def_id.index = key.parent.unwrap_or_else(|| {
|
2018-05-08 08:38:29 -05:00
|
|
|
bug!(
|
|
|
|
"finding type for {:?}, encountered def-id {:?} with no \
|
|
|
|
parent",
|
|
|
|
def_id,
|
|
|
|
ty_def_id
|
|
|
|
);
|
2017-02-08 11:31:03 -06:00
|
|
|
});
|
2016-03-24 12:19:36 -05:00
|
|
|
}
|
|
|
|
}
|
2017-02-08 11:31:03 -06:00
|
|
|
}
|
2016-03-24 12:19:36 -05:00
|
|
|
|
2017-02-08 11:31:03 -06:00
|
|
|
// Erase regions because they may not be deterministic when hashed
|
|
|
|
// and should not matter anyhow.
|
2017-04-14 14:30:06 -05:00
|
|
|
let instance_ty = tcx.erase_regions(&instance_ty);
|
2016-03-24 12:19:36 -05:00
|
|
|
|
2017-10-06 16:59:33 -05:00
|
|
|
let hash = get_symbol_hash(tcx, def_id, instance, instance_ty, substs);
|
2016-02-14 12:01:44 -06:00
|
|
|
|
2019-01-25 04:11:50 -06:00
|
|
|
let mut printer = SymbolPrinter {
|
|
|
|
tcx,
|
2019-03-21 11:06:04 -05:00
|
|
|
path: SymbolPath::new(),
|
2019-01-25 04:11:50 -06:00
|
|
|
keep_within_component: false,
|
2019-03-21 11:06:04 -05:00
|
|
|
}.print_def_path(def_id, &[]).unwrap();
|
2018-09-10 09:01:46 -05:00
|
|
|
|
|
|
|
if instance.is_vtable_shim() {
|
2019-01-25 04:11:50 -06:00
|
|
|
let _ = printer.write_str("{{vtable-shim}}");
|
2018-09-10 09:01:46 -05:00
|
|
|
}
|
|
|
|
|
2019-05-13 23:55:15 -05:00
|
|
|
InternedString::intern(&printer.path.finish(hash))
|
2016-03-16 04:57:03 -05:00
|
|
|
}
|
2016-02-14 12:01:44 -06:00
|
|
|
|
2017-04-20 10:38:35 -05:00
|
|
|
// Follow C++ namespace-mangling style, see
|
|
|
|
// http://en.wikipedia.org/wiki/Name_mangling for more info.
|
|
|
|
//
|
|
|
|
// It turns out that on macOS you can actually have arbitrary symbols in
|
|
|
|
// function names (at least when given to LLVM), but this is not possible
|
|
|
|
// when using unix's linker. Perhaps one day when we just use a linker from LLVM
|
|
|
|
// we won't need to do this name mangling. The problem with name mangling is
|
|
|
|
// that it seriously limits the available characters. For example we can't
|
|
|
|
// have things like &T in symbol names when one would theoretically
|
|
|
|
// want them for things like impls of traits on that type.
|
|
|
|
//
|
|
|
|
// To be able to work on all platforms and get *some* reasonable output, we
|
|
|
|
// use C++ name-mangling.
|
2019-01-27 18:16:59 -06:00
|
|
|
#[derive(Debug)]
|
2018-12-09 13:05:22 -06:00
|
|
|
struct SymbolPath {
|
2017-04-20 10:38:35 -05:00
|
|
|
result: String,
|
2018-05-08 08:38:29 -05:00
|
|
|
temp_buf: String,
|
2017-04-20 10:38:35 -05:00
|
|
|
}
|
|
|
|
|
2018-12-09 13:05:22 -06:00
|
|
|
impl SymbolPath {
|
2019-01-25 04:11:50 -06:00
|
|
|
fn new() -> Self {
|
2018-12-09 13:05:22 -06:00
|
|
|
let mut result = SymbolPath {
|
2017-04-20 10:38:35 -05:00
|
|
|
result: String::with_capacity(64),
|
2018-05-08 08:38:29 -05:00
|
|
|
temp_buf: String::with_capacity(16),
|
2017-04-20 10:38:35 -05:00
|
|
|
};
|
|
|
|
result.result.push_str("_ZN"); // _Z == Begin name-sequence, N == nested
|
|
|
|
result
|
|
|
|
}
|
|
|
|
|
2018-12-21 09:10:21 -06:00
|
|
|
fn finalize_pending_component(&mut self) {
|
2018-12-27 22:09:22 -06:00
|
|
|
if !self.temp_buf.is_empty() {
|
2018-12-21 09:10:21 -06:00
|
|
|
let _ = write!(self.result, "{}{}", self.temp_buf.len(), self.temp_buf);
|
|
|
|
self.temp_buf.clear();
|
2017-04-20 10:38:35 -05:00
|
|
|
}
|
2016-03-16 04:57:03 -05:00
|
|
|
}
|
2018-12-09 13:05:22 -06:00
|
|
|
|
|
|
|
fn finish(mut self, hash: u64) -> String {
|
2018-12-21 09:10:21 -06:00
|
|
|
self.finalize_pending_component();
|
2018-12-09 13:05:22 -06:00
|
|
|
// E = end name-sequence
|
|
|
|
let _ = write!(self.result, "17h{:016x}E", hash);
|
|
|
|
self.result
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-25 04:11:50 -06:00
|
|
|
struct SymbolPrinter<'a, 'tcx> {
|
|
|
|
tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
|
|
|
path: SymbolPath,
|
|
|
|
|
|
|
|
// When `true`, `finalize_pending_component` isn't used.
|
|
|
|
// This is needed when recursing into `path_qualified`,
|
|
|
|
// or `path_generic_args`, as any nested paths are
|
|
|
|
// logically within one component.
|
|
|
|
keep_within_component: bool,
|
|
|
|
}
|
|
|
|
|
2018-12-21 09:10:21 -06:00
|
|
|
// HACK(eddyb) this relies on using the `fmt` interface to get
|
|
|
|
// `PrettyPrinter` aka pretty printing of e.g. types in paths,
|
|
|
|
// symbol names should have their own printing machinery.
|
2018-12-09 13:05:22 -06:00
|
|
|
|
2019-01-25 04:11:50 -06:00
|
|
|
impl Printer<'tcx, 'tcx> for SymbolPrinter<'_, 'tcx> {
|
2019-01-09 08:30:10 -06:00
|
|
|
type Error = fmt::Error;
|
2018-12-09 13:05:22 -06:00
|
|
|
|
2019-01-10 05:27:11 -06:00
|
|
|
type Path = Self;
|
2019-01-14 09:55:57 -06:00
|
|
|
type Region = Self;
|
2019-01-14 11:56:46 -06:00
|
|
|
type Type = Self;
|
2019-01-24 12:47:02 -06:00
|
|
|
type DynExistential = Self;
|
2019-01-14 09:55:57 -06:00
|
|
|
|
2019-01-25 04:11:50 -06:00
|
|
|
fn tcx(&'a self) -> TyCtxt<'a, 'tcx, 'tcx> {
|
|
|
|
self.tcx
|
|
|
|
}
|
|
|
|
|
2019-01-14 09:55:57 -06:00
|
|
|
fn print_region(
|
2019-01-25 04:11:50 -06:00
|
|
|
self,
|
2019-01-14 09:55:57 -06:00
|
|
|
_region: ty::Region<'_>,
|
|
|
|
) -> Result<Self::Region, Self::Error> {
|
2019-01-25 04:11:50 -06:00
|
|
|
Ok(self)
|
2019-01-14 09:55:57 -06:00
|
|
|
}
|
2019-01-09 08:30:10 -06:00
|
|
|
|
2019-01-14 11:56:46 -06:00
|
|
|
fn print_type(
|
2019-01-25 04:11:50 -06:00
|
|
|
self,
|
2019-01-14 11:56:46 -06:00
|
|
|
ty: Ty<'tcx>,
|
|
|
|
) -> Result<Self::Type, Self::Error> {
|
2019-01-14 16:41:14 -06:00
|
|
|
match ty.sty {
|
|
|
|
// Print all nominal types as paths (unlike `pretty_print_type`).
|
|
|
|
ty::FnDef(def_id, substs) |
|
|
|
|
ty::Opaque(def_id, substs) |
|
|
|
|
ty::Projection(ty::ProjectionTy { item_def_id: def_id, substs }) |
|
|
|
|
ty::UnnormalizedProjection(ty::ProjectionTy { item_def_id: def_id, substs }) |
|
|
|
|
ty::Closure(def_id, ty::ClosureSubsts { substs }) |
|
|
|
|
ty::Generator(def_id, ty::GeneratorSubsts { substs }, _) => {
|
2019-01-28 23:21:11 -06:00
|
|
|
self.print_def_path(def_id, substs)
|
2019-01-14 16:41:14 -06:00
|
|
|
}
|
|
|
|
_ => self.pretty_print_type(ty),
|
|
|
|
}
|
2019-01-14 11:56:46 -06:00
|
|
|
}
|
|
|
|
|
2019-01-24 12:47:02 -06:00
|
|
|
fn print_dyn_existential(
|
2019-01-25 04:11:50 -06:00
|
|
|
mut self,
|
2019-01-24 12:47:02 -06:00
|
|
|
predicates: &'tcx ty::List<ty::ExistentialPredicate<'tcx>>,
|
|
|
|
) -> Result<Self::DynExistential, Self::Error> {
|
|
|
|
let mut first = false;
|
|
|
|
for p in predicates {
|
|
|
|
if !first {
|
|
|
|
write!(self, "+")?;
|
|
|
|
}
|
|
|
|
first = false;
|
2019-01-25 04:11:50 -06:00
|
|
|
self = p.print(self)?;
|
2019-01-24 12:47:02 -06:00
|
|
|
}
|
2019-01-25 04:11:50 -06:00
|
|
|
Ok(self)
|
2019-01-24 12:47:02 -06:00
|
|
|
}
|
|
|
|
|
2019-01-09 08:30:10 -06:00
|
|
|
fn path_crate(
|
2019-01-25 04:11:50 -06:00
|
|
|
mut self,
|
2019-01-09 08:30:10 -06:00
|
|
|
cnum: CrateNum,
|
|
|
|
) -> Result<Self::Path, Self::Error> {
|
2019-01-20 11:46:47 -06:00
|
|
|
self.write_str(&self.tcx.original_crate_name(cnum).as_str())?;
|
2019-01-25 04:11:50 -06:00
|
|
|
Ok(self)
|
2018-12-21 09:10:21 -06:00
|
|
|
}
|
|
|
|
fn path_qualified(
|
2019-01-25 04:11:50 -06:00
|
|
|
self,
|
2018-12-21 09:10:21 -06:00
|
|
|
self_ty: Ty<'tcx>,
|
|
|
|
trait_ref: Option<ty::TraitRef<'tcx>>,
|
2019-01-09 08:30:10 -06:00
|
|
|
) -> Result<Self::Path, Self::Error> {
|
2019-01-14 16:41:14 -06:00
|
|
|
// Similar to `pretty_path_qualified`, but for the other
|
|
|
|
// types that are printed as paths (see `print_type` above).
|
|
|
|
match self_ty.sty {
|
|
|
|
ty::FnDef(..) |
|
|
|
|
ty::Opaque(..) |
|
|
|
|
ty::Projection(_) |
|
|
|
|
ty::UnnormalizedProjection(_) |
|
|
|
|
ty::Closure(..) |
|
|
|
|
ty::Generator(..)
|
|
|
|
if trait_ref.is_none() =>
|
|
|
|
{
|
|
|
|
self.print_type(self_ty)
|
|
|
|
}
|
|
|
|
|
|
|
|
_ => self.pretty_path_qualified(self_ty, trait_ref)
|
|
|
|
}
|
2019-01-10 05:27:11 -06:00
|
|
|
}
|
|
|
|
|
2019-01-25 04:11:50 -06:00
|
|
|
fn path_append_impl(
|
|
|
|
self,
|
|
|
|
print_prefix: impl FnOnce(Self) -> Result<Self::Path, Self::Error>,
|
2019-02-03 04:59:37 -06:00
|
|
|
_disambiguated_data: &DisambiguatedDefPathData,
|
2019-01-10 05:27:11 -06:00
|
|
|
self_ty: Ty<'tcx>,
|
|
|
|
trait_ref: Option<ty::TraitRef<'tcx>>,
|
|
|
|
) -> Result<Self::Path, Self::Error> {
|
2019-01-14 14:07:38 -06:00
|
|
|
self.pretty_path_append_impl(
|
2019-02-03 04:59:37 -06:00
|
|
|
|mut cx| {
|
|
|
|
cx = print_prefix(cx)?;
|
|
|
|
|
|
|
|
if cx.keep_within_component {
|
|
|
|
// HACK(eddyb) print the path similarly to how `FmtPrinter` prints it.
|
|
|
|
cx.write_str("::")?;
|
|
|
|
} else {
|
|
|
|
cx.path.finalize_pending_component();
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(cx)
|
|
|
|
},
|
2019-01-10 05:27:11 -06:00
|
|
|
self_ty,
|
|
|
|
trait_ref,
|
2019-01-14 14:07:38 -06:00
|
|
|
)
|
2018-12-09 13:05:22 -06:00
|
|
|
}
|
2019-01-25 04:11:50 -06:00
|
|
|
fn path_append(
|
|
|
|
mut self,
|
|
|
|
print_prefix: impl FnOnce(Self) -> Result<Self::Path, Self::Error>,
|
2019-02-03 04:59:37 -06:00
|
|
|
disambiguated_data: &DisambiguatedDefPathData,
|
2019-01-09 08:30:10 -06:00
|
|
|
) -> Result<Self::Path, Self::Error> {
|
2019-01-25 04:11:50 -06:00
|
|
|
self = print_prefix(self)?;
|
2019-01-10 05:27:11 -06:00
|
|
|
|
2019-02-03 04:59:37 -06:00
|
|
|
// Skip `::{{constructor}}` on tuple/unit structs.
|
|
|
|
match disambiguated_data.data {
|
2019-03-24 09:49:58 -05:00
|
|
|
DefPathData::Ctor => return Ok(self),
|
2019-02-03 04:59:37 -06:00
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
|
2019-01-25 04:11:50 -06:00
|
|
|
if self.keep_within_component {
|
2018-12-27 22:09:22 -06:00
|
|
|
// HACK(eddyb) print the path similarly to how `FmtPrinter` prints it.
|
2019-01-25 04:11:50 -06:00
|
|
|
self.write_str("::")?;
|
2018-12-27 22:09:22 -06:00
|
|
|
} else {
|
2019-01-25 04:11:50 -06:00
|
|
|
self.path.finalize_pending_component();
|
2018-12-27 22:09:22 -06:00
|
|
|
}
|
|
|
|
|
2019-02-03 04:59:37 -06:00
|
|
|
self.write_str(&disambiguated_data.data.as_interned_str().as_str())?;
|
2019-01-25 04:11:50 -06:00
|
|
|
Ok(self)
|
2018-12-21 09:10:21 -06:00
|
|
|
}
|
2019-01-25 04:11:50 -06:00
|
|
|
fn path_generic_args(
|
|
|
|
mut self,
|
|
|
|
print_prefix: impl FnOnce(Self) -> Result<Self::Path, Self::Error>,
|
2019-01-24 12:47:02 -06:00
|
|
|
args: &[Kind<'tcx>],
|
2019-01-09 08:30:10 -06:00
|
|
|
) -> Result<Self::Path, Self::Error> {
|
2019-01-25 04:11:50 -06:00
|
|
|
self = print_prefix(self)?;
|
2019-01-24 12:47:02 -06:00
|
|
|
|
|
|
|
let args = args.iter().cloned().filter(|arg| {
|
2019-01-24 11:52:43 -06:00
|
|
|
match arg.unpack() {
|
|
|
|
UnpackedKind::Lifetime(_) => false,
|
|
|
|
_ => true,
|
|
|
|
}
|
|
|
|
});
|
2019-01-24 12:47:02 -06:00
|
|
|
|
|
|
|
if args.clone().next().is_some() {
|
|
|
|
self.generic_delimiters(|cx| cx.comma_sep(args))
|
|
|
|
} else {
|
2019-01-25 04:11:50 -06:00
|
|
|
Ok(self)
|
2019-01-24 12:47:02 -06:00
|
|
|
}
|
2018-12-09 13:05:22 -06:00
|
|
|
}
|
2016-02-14 12:01:44 -06:00
|
|
|
}
|
|
|
|
|
2019-01-25 04:11:50 -06:00
|
|
|
impl PrettyPrinter<'tcx, 'tcx> for SymbolPrinter<'_, 'tcx> {
|
2019-01-23 11:36:39 -06:00
|
|
|
fn region_should_not_be_omitted(
|
2019-01-25 04:11:50 -06:00
|
|
|
&self,
|
2019-01-14 09:55:57 -06:00
|
|
|
_region: ty::Region<'_>,
|
|
|
|
) -> bool {
|
|
|
|
false
|
|
|
|
}
|
2019-01-24 12:47:02 -06:00
|
|
|
fn comma_sep<T>(
|
2019-01-25 04:11:50 -06:00
|
|
|
mut self,
|
2019-01-24 12:47:02 -06:00
|
|
|
mut elems: impl Iterator<Item = T>,
|
|
|
|
) -> Result<Self, Self::Error>
|
2019-01-25 04:11:50 -06:00
|
|
|
where T: Print<'tcx, 'tcx, Self, Output = Self, Error = Self::Error>
|
2019-01-24 12:47:02 -06:00
|
|
|
{
|
|
|
|
if let Some(first) = elems.next() {
|
2019-01-25 04:11:50 -06:00
|
|
|
self = first.print(self)?;
|
2019-01-24 12:47:02 -06:00
|
|
|
for elem in elems {
|
|
|
|
self.write_str(",")?;
|
2019-01-25 04:11:50 -06:00
|
|
|
self = elem.print(self)?;
|
2019-01-24 12:47:02 -06:00
|
|
|
}
|
|
|
|
}
|
2019-01-25 04:11:50 -06:00
|
|
|
Ok(self)
|
2019-01-24 12:47:02 -06:00
|
|
|
}
|
2019-01-14 14:07:38 -06:00
|
|
|
|
2019-01-25 04:11:50 -06:00
|
|
|
fn generic_delimiters(
|
|
|
|
mut self,
|
|
|
|
f: impl FnOnce(Self) -> Result<Self, Self::Error>,
|
2019-01-14 14:07:38 -06:00
|
|
|
) -> Result<Self, Self::Error> {
|
2019-01-20 11:46:47 -06:00
|
|
|
write!(self, "<")?;
|
2019-01-14 14:07:38 -06:00
|
|
|
|
|
|
|
let kept_within_component =
|
2019-01-20 11:46:47 -06:00
|
|
|
mem::replace(&mut self.keep_within_component, true);
|
2019-01-25 04:11:50 -06:00
|
|
|
self = f(self)?;
|
|
|
|
self.keep_within_component = kept_within_component;
|
2019-01-14 14:07:38 -06:00
|
|
|
|
2019-01-25 04:11:50 -06:00
|
|
|
write!(self, ">")?;
|
2019-01-14 14:07:38 -06:00
|
|
|
|
2019-01-25 04:11:50 -06:00
|
|
|
Ok(self)
|
2019-01-14 14:07:38 -06:00
|
|
|
}
|
2019-01-14 09:55:57 -06:00
|
|
|
}
|
2018-12-21 09:10:21 -06:00
|
|
|
|
2019-01-25 04:11:50 -06:00
|
|
|
impl fmt::Write for SymbolPrinter<'_, '_> {
|
2018-12-21 09:10:21 -06:00
|
|
|
fn write_str(&mut self, s: &str) -> fmt::Result {
|
|
|
|
// Name sanitation. LLVM will happily accept identifiers with weird names, but
|
|
|
|
// gas doesn't!
|
|
|
|
// gas accepts the following characters in symbols: a-z, A-Z, 0-9, ., _, $
|
|
|
|
// NVPTX assembly has more strict naming rules than gas, so additionally, dots
|
|
|
|
// are replaced with '$' there.
|
|
|
|
|
|
|
|
for c in s.chars() {
|
2019-01-25 04:11:50 -06:00
|
|
|
if self.path.temp_buf.is_empty() {
|
2018-12-21 09:10:21 -06:00
|
|
|
match c {
|
|
|
|
'a'..='z' | 'A'..='Z' | '_' => {}
|
|
|
|
_ => {
|
|
|
|
// Underscore-qualify anything that didn't start as an ident.
|
2019-01-25 04:11:50 -06:00
|
|
|
self.path.temp_buf.push('_');
|
2018-12-21 09:10:21 -06:00
|
|
|
}
|
|
|
|
}
|
2019-02-03 04:24:29 -06:00
|
|
|
}
|
2018-12-21 09:10:21 -06:00
|
|
|
match c {
|
|
|
|
// Escape these with $ sequences
|
2019-01-25 04:11:50 -06:00
|
|
|
'@' => self.path.temp_buf.push_str("$SP$"),
|
|
|
|
'*' => self.path.temp_buf.push_str("$BP$"),
|
|
|
|
'&' => self.path.temp_buf.push_str("$RF$"),
|
|
|
|
'<' => self.path.temp_buf.push_str("$LT$"),
|
|
|
|
'>' => self.path.temp_buf.push_str("$GT$"),
|
|
|
|
'(' => self.path.temp_buf.push_str("$LP$"),
|
|
|
|
')' => self.path.temp_buf.push_str("$RP$"),
|
|
|
|
',' => self.path.temp_buf.push_str("$C$"),
|
|
|
|
|
|
|
|
'-' | ':' | '.' if self.tcx.has_strict_asm_symbol_naming() => {
|
2018-12-21 09:10:21 -06:00
|
|
|
// NVPTX doesn't support these characters in symbol names.
|
2019-01-25 04:11:50 -06:00
|
|
|
self.path.temp_buf.push('$')
|
2018-12-21 09:10:21 -06:00
|
|
|
}
|
2016-03-01 07:18:21 -06:00
|
|
|
|
2018-12-21 09:10:21 -06:00
|
|
|
// '.' doesn't occur in types and functions, so reuse it
|
|
|
|
// for ':' and '-'
|
2019-01-25 04:11:50 -06:00
|
|
|
'-' | ':' => self.path.temp_buf.push('.'),
|
2018-12-21 09:10:21 -06:00
|
|
|
|
|
|
|
// These are legal symbols
|
2019-01-25 04:11:50 -06:00
|
|
|
'a'..='z' | 'A'..='Z' | '0'..='9' | '_' | '.' | '$' => self.path.temp_buf.push(c),
|
2018-12-21 09:10:21 -06:00
|
|
|
|
|
|
|
_ => {
|
2019-01-25 04:11:50 -06:00
|
|
|
self.path.temp_buf.push('$');
|
2018-12-21 09:10:21 -06:00
|
|
|
for c in c.escape_unicode().skip(1) {
|
|
|
|
match c {
|
|
|
|
'{' => {}
|
2019-01-25 04:11:50 -06:00
|
|
|
'}' => self.path.temp_buf.push('$'),
|
|
|
|
c => self.path.temp_buf.push(c),
|
2018-12-21 09:10:21 -06:00
|
|
|
}
|
2019-02-03 04:24:29 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-21 09:10:21 -06:00
|
|
|
Ok(())
|
|
|
|
}
|
2016-02-14 16:38:49 -06:00
|
|
|
}
|