rustc_codegen_utils: add new mangling scheme implementation.

This commit is contained in:
Eduard-Mihai Burtescu 2019-01-29 07:24:32 +02:00
parent 654d0596d3
commit 2092963a18
12 changed files with 1241 additions and 490 deletions

View File

@ -2023,6 +2023,11 @@ dependencies = [
"unicase 2.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "unicase 2.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
[[package]]
name = "punycode"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]] [[package]]
name = "quick-error" name = "quick-error"
version = "1.2.2" version = "1.2.2"
@ -2687,6 +2692,7 @@ version = "0.0.0"
dependencies = [ dependencies = [
"flate2 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)", "flate2 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
"punycode 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
"rustc 0.0.0", "rustc 0.0.0",
"rustc-demangle 0.1.15 (registry+https://github.com/rust-lang/crates.io-index)", "rustc-demangle 0.1.15 (registry+https://github.com/rust-lang/crates.io-index)",
"rustc_data_structures 0.0.0", "rustc_data_structures 0.0.0",
@ -4277,6 +4283,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
"checksum proptest 0.9.2 (registry+https://github.com/rust-lang/crates.io-index)" = "24f5844db2f839e97e3021980975f6ebf8691d9b9b2ca67ed3feb38dc3edb52c" "checksum proptest 0.9.2 (registry+https://github.com/rust-lang/crates.io-index)" = "24f5844db2f839e97e3021980975f6ebf8691d9b9b2ca67ed3feb38dc3edb52c"
"checksum pulldown-cmark 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "d6fdf85cda6cadfae5428a54661d431330b312bc767ddbc57adbedc24da66e32" "checksum pulldown-cmark 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "d6fdf85cda6cadfae5428a54661d431330b312bc767ddbc57adbedc24da66e32"
"checksum pulldown-cmark 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)" = "051e60ace841b3bfecd402fe5051c06cb3bec4a6e6fdd060a37aa8eb829a1db3" "checksum pulldown-cmark 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)" = "051e60ace841b3bfecd402fe5051c06cb3bec4a6e6fdd060a37aa8eb829a1db3"
"checksum punycode 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "6ddd112cca70a4d30883b2d21568a1d376ff8be4758649f64f973c6845128ad3"
"checksum quick-error 1.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "9274b940887ce9addde99c4eee6b5c44cc494b182b97e73dc8ffdcb3397fd3f0" "checksum quick-error 1.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "9274b940887ce9addde99c4eee6b5c44cc494b182b97e73dc8ffdcb3397fd3f0"
"checksum quine-mc_cluskey 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)" = "07589615d719a60c8dd8a4622e7946465dfef20d1a428f969e3443e7386d5f45" "checksum quine-mc_cluskey 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)" = "07589615d719a60c8dd8a4622e7946465dfef20d1a428f969e3443e7386d5f45"
"checksum quote 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)" = "7a6e920b65c65f10b2ae65c831a81a073a89edd28c7cce89475bff467ab4167a" "checksum quote 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)" = "7a6e920b65c65f10b2ae65c831a81a073a89edd28c7cce89475bff467ab4167a"

View File

@ -624,6 +624,10 @@ rustc_queries! {
fatal_cycle fatal_cycle
desc { "test whether a crate has #![no_builtins]" } desc { "test whether a crate has #![no_builtins]" }
} }
query symbol_mangling_version(_: CrateNum) -> SymbolManglingVersion {
fatal_cycle
desc { "query a crate's symbol mangling version" }
}
query extern_crate(_: DefId) -> Option<&'tcx ExternCrate> { query extern_crate(_: DefId) -> Option<&'tcx ExternCrate> {
eval_always eval_always

View File

@ -131,6 +131,14 @@ impl SwitchWithOptPath {
} }
} }
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, RustcEncodable, RustcDecodable)]
pub enum SymbolManglingVersion {
Legacy,
V0,
}
impl_stable_hash_via_hash!(SymbolManglingVersion);
#[derive(Clone, Copy, PartialEq, Hash)] #[derive(Clone, Copy, PartialEq, Hash)]
pub enum DebugInfo { pub enum DebugInfo {
None, None,
@ -838,11 +846,14 @@ macro_rules! options {
Some("an optional path to the profiling data output directory"); Some("an optional path to the profiling data output directory");
pub const parse_merge_functions: Option<&str> = pub const parse_merge_functions: Option<&str> =
Some("one of: `disabled`, `trampolines`, or `aliases`"); Some("one of: `disabled`, `trampolines`, or `aliases`");
pub const parse_symbol_mangling_version: Option<&str> =
Some("either `legacy` or `v0` (RFC 2603)");
} }
#[allow(dead_code)] #[allow(dead_code)]
mod $mod_set { mod $mod_set {
use super::{$struct_name, Passes, Sanitizer, LtoCli, LinkerPluginLto, SwitchWithOptPath}; use super::{$struct_name, Passes, Sanitizer, LtoCli, LinkerPluginLto, SwitchWithOptPath,
SymbolManglingVersion};
use rustc_target::spec::{LinkerFlavor, MergeFunctions, PanicStrategy, RelroLevel}; use rustc_target::spec::{LinkerFlavor, MergeFunctions, PanicStrategy, RelroLevel};
use std::path::PathBuf; use std::path::PathBuf;
use std::str::FromStr; use std::str::FromStr;
@ -1112,6 +1123,18 @@ macro_rules! options {
} }
true true
} }
fn parse_symbol_mangling_version(
slot: &mut SymbolManglingVersion,
v: Option<&str>,
) -> bool {
*slot = match v {
Some("legacy") => SymbolManglingVersion::Legacy,
Some("v0") => SymbolManglingVersion::V0,
_ => return false,
};
true
}
} }
) } ) }
@ -1457,6 +1480,9 @@ options! {DebuggingOptions, DebuggingSetter, basic_debugging_options,
"only allow the listed language features to be enabled in code (space separated)"), "only allow the listed language features to be enabled in code (space separated)"),
emit_artifact_notifications: bool = (false, parse_bool, [UNTRACKED], emit_artifact_notifications: bool = (false, parse_bool, [UNTRACKED],
"emit notifications after each artifact has been output (only in the JSON format)"), "emit notifications after each artifact has been output (only in the JSON format)"),
symbol_mangling_version: SymbolManglingVersion = (SymbolManglingVersion::Legacy,
parse_symbol_mangling_version, [TRACKED],
"which mangling version to use for symbol names"),
} }
pub fn default_lib_output() -> CrateType { pub fn default_lib_output() -> CrateType {
@ -2551,7 +2577,8 @@ mod dep_tracking {
use std::path::PathBuf; use std::path::PathBuf;
use std::collections::hash_map::DefaultHasher; use std::collections::hash_map::DefaultHasher;
use super::{CrateType, DebugInfo, ErrorOutputType, OptLevel, OutputTypes, use super::{CrateType, DebugInfo, ErrorOutputType, OptLevel, OutputTypes,
Passes, Sanitizer, LtoCli, LinkerPluginLto, SwitchWithOptPath}; Passes, Sanitizer, LtoCli, LinkerPluginLto, SwitchWithOptPath,
SymbolManglingVersion};
use syntax::feature_gate::UnstableFeatures; use syntax::feature_gate::UnstableFeatures;
use rustc_target::spec::{MergeFunctions, PanicStrategy, RelroLevel, TargetTriple}; use rustc_target::spec::{MergeFunctions, PanicStrategy, RelroLevel, TargetTriple};
use syntax::edition::Edition; use syntax::edition::Edition;
@ -2620,6 +2647,7 @@ mod dep_tracking {
impl_dep_tracking_hash_via_hash!(Edition); impl_dep_tracking_hash_via_hash!(Edition);
impl_dep_tracking_hash_via_hash!(LinkerPluginLto); impl_dep_tracking_hash_via_hash!(LinkerPluginLto);
impl_dep_tracking_hash_via_hash!(SwitchWithOptPath); impl_dep_tracking_hash_via_hash!(SwitchWithOptPath);
impl_dep_tracking_hash_via_hash!(SymbolManglingVersion);
impl_dep_tracking_hash_for_sortable_vec_of!(String); impl_dep_tracking_hash_for_sortable_vec_of!(String);
impl_dep_tracking_hash_for_sortable_vec_of!(PathBuf); impl_dep_tracking_hash_for_sortable_vec_of!(PathBuf);
@ -2693,7 +2721,7 @@ mod tests {
use std::collections::{BTreeMap, BTreeSet}; use std::collections::{BTreeMap, BTreeSet};
use std::iter::FromIterator; use std::iter::FromIterator;
use std::path::PathBuf; use std::path::PathBuf;
use super::{Externs, OutputType, OutputTypes}; use super::{Externs, OutputType, OutputTypes, SymbolManglingVersion};
use rustc_target::spec::{MergeFunctions, PanicStrategy, RelroLevel}; use rustc_target::spec::{MergeFunctions, PanicStrategy, RelroLevel};
use syntax::symbol::sym; use syntax::symbol::sym;
use syntax::edition::{Edition, DEFAULT_EDITION}; use syntax::edition::{Edition, DEFAULT_EDITION};
@ -3367,6 +3395,10 @@ mod tests {
opts = reference.clone(); opts = reference.clone();
opts.debugging_opts.allow_features = Some(vec![String::from("lang_items")]); opts.debugging_opts.allow_features = Some(vec![String::from("lang_items")]);
assert!(reference.dep_tracking_hash() != opts.dep_tracking_hash()); assert!(reference.dep_tracking_hash() != opts.dep_tracking_hash());
opts = reference.clone();
opts.debugging_opts.symbol_mangling_version = SymbolManglingVersion::V0;
assert!(reference.dep_tracking_hash() != opts.dep_tracking_hash());
} }
#[test] #[test]

View File

@ -20,7 +20,7 @@ use crate::mir::mono::CodegenUnit;
use crate::mir; use crate::mir;
use crate::mir::interpret::GlobalId; use crate::mir::interpret::GlobalId;
use crate::session::CrateDisambiguator; use crate::session::CrateDisambiguator;
use crate::session::config::{EntryFnType, OutputFilenames, OptLevel}; use crate::session::config::{EntryFnType, OutputFilenames, OptLevel, SymbolManglingVersion};
use crate::traits::{self, Vtable}; use crate::traits::{self, Vtable};
use crate::traits::query::{ use crate::traits::query::{
CanonicalPredicateGoal, CanonicalProjectionGoal, CanonicalPredicateGoal, CanonicalProjectionGoal,

View File

@ -13,6 +13,7 @@ test = false
[dependencies] [dependencies]
flate2 = "1.0" flate2 = "1.0"
log = "0.4" log = "0.4"
punycode = "0.4.0"
rustc-demangle = "0.1.15" rustc-demangle = "0.1.15"
syntax = { path = "../libsyntax" } syntax = { path = "../libsyntax" }

View File

@ -7,7 +7,9 @@
#![feature(arbitrary_self_types)] #![feature(arbitrary_self_types)]
#![feature(box_patterns)] #![feature(box_patterns)]
#![feature(box_syntax)] #![feature(box_syntax)]
#![feature(core_intrinsics)]
#![feature(custom_attribute)] #![feature(custom_attribute)]
#![feature(never_type)]
#![feature(nll)] #![feature(nll)]
#![allow(unused_attributes)] #![allow(unused_attributes)]
#![feature(rustc_diagnostic_macros)] #![feature(rustc_diagnostic_macros)]

View File

@ -87,18 +87,12 @@
//! virtually impossible. Thus, symbol hash generation exclusively relies on //! virtually impossible. Thus, symbol hash generation exclusively relies on
//! DefPaths which are much more robust in the face of changes to the code base. //! DefPaths which are much more robust in the face of changes to the code base.
use rustc::hir::def_id::{CrateNum, DefId, LOCAL_CRATE}; use rustc::hir::def_id::LOCAL_CRATE;
use rustc::hir::Node; use rustc::hir::Node;
use rustc::hir::CodegenFnAttrFlags; use rustc::hir::CodegenFnAttrFlags;
use rustc::hir::map::{DefPathData, DisambiguatedDefPathData}; use rustc::session::config::SymbolManglingVersion;
use rustc::ich::NodeIdHashingMode;
use rustc::ty::print::{PrettyPrinter, Printer, Print};
use rustc::ty::query::Providers; use rustc::ty::query::Providers;
use rustc::ty::subst::{Kind, SubstsRef, UnpackedKind}; use rustc::ty::{self, TyCtxt};
use rustc::ty::{self, Ty, TyCtxt, TypeFoldable};
use rustc::mir::interpret::{ConstValue, Scalar};
use rustc::util::common::record_time;
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
use rustc_mir::monomorphize::item::{InstantiationMode, MonoItem, MonoItemExt}; use rustc_mir::monomorphize::item::{InstantiationMode, MonoItem, MonoItemExt};
use rustc_mir::monomorphize::Instance; use rustc_mir::monomorphize::Instance;
@ -106,129 +100,20 @@ use syntax_pos::symbol::InternedString;
use log::debug; use log::debug;
use std::fmt::{self, Write}; mod legacy;
use std::mem::{self, discriminant}; mod v0;
pub fn provide(providers: &mut Providers<'_>) { pub fn provide(providers: &mut Providers<'_>) {
*providers = Providers { *providers = Providers {
symbol_name, symbol_name: |tcx, instance| ty::SymbolName {
name: symbol_name(tcx, instance),
},
..*providers ..*providers
}; };
} }
fn get_symbol_hash<'a, 'tcx>( fn symbol_name(tcx: TyCtxt<'_, 'tcx, 'tcx>, instance: Instance<'tcx>) -> InternedString {
tcx: TyCtxt<'a, 'tcx, 'tcx>,
// the DefId of the item this name is for
def_id: DefId,
// instance this name will be for
instance: Instance<'tcx>,
// type of the item, without any generic
// parameters substituted; this is
// included in the hash as a kind of
// safeguard.
item_type: Ty<'tcx>,
// values for generic type parameters,
// if any.
substs: SubstsRef<'tcx>,
) -> u64 {
debug!(
"get_symbol_hash(def_id={:?}, parameters={:?})",
def_id, substs
);
let mut hasher = StableHasher::<u64>::new();
let mut hcx = tcx.create_stable_hashing_context();
record_time(&tcx.sess.perf_stats.symbol_hash_time, || {
// the main symbol name is not necessarily unique; hash in the
// compiler's internal def-path, guaranteeing each symbol has a
// truly unique path
tcx.def_path_hash(def_id).hash_stable(&mut hcx, &mut hasher);
// 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());
hcx.while_hashing_spans(false, |hcx| {
hcx.with_node_id_hashing_mode(NodeIdHashingMode::HashDefPath, |hcx| {
item_type.hash_stable(hcx, &mut hasher);
});
});
// 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.
if let ty::FnDef(..) = item_type.sty {
item_type.fn_sig(tcx).hash_stable(&mut hcx, &mut hasher);
}
// also include any type parameters (for generic items)
assert!(!substs.has_erasable_regions());
assert!(!substs.needs_subst());
substs.hash_stable(&mut hcx, &mut hasher);
let is_generic = substs.non_erasable_generics().next().is_some();
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,
};
if avoid_cross_crate_conflicts {
let instantiating_crate = if is_generic {
if !def_id.is_local() && tcx.sess.opts.share_generics() {
// If we are re-using a monomorphization from another crate,
// we have to compute the symbol hash accordingly.
let upstream_monomorphizations = tcx.upstream_monomorphizations_for(def_id);
upstream_monomorphizations
.and_then(|monos| monos.get(&substs).cloned())
.unwrap_or(LOCAL_CRATE)
} else {
LOCAL_CRATE
}
} else {
LOCAL_CRATE
};
(&tcx.original_crate_name(instantiating_crate).as_str()[..])
.hash_stable(&mut hcx, &mut hasher);
(&tcx.crate_disambiguator(instantiating_crate)).hash_stable(&mut hcx, &mut hasher);
}
// 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);
});
// 64 bits should be enough to avoid collisions.
hasher.finish()
}
fn symbol_name(tcx: TyCtxt<'_, 'tcx, 'tcx>, instance: Instance<'tcx>) -> ty::SymbolName {
ty::SymbolName {
name: compute_symbol_name(tcx, instance),
}
}
fn compute_symbol_name(tcx: TyCtxt<'_, 'tcx, 'tcx>, instance: Instance<'tcx>) -> InternedString {
let def_id = instance.def_id(); let def_id = instance.def_id();
let substs = instance.substs; let substs = instance.substs;
@ -278,376 +163,65 @@ fn compute_symbol_name(tcx: TyCtxt<'_, 'tcx, 'tcx>, instance: Instance<'tcx>) ->
return tcx.item_name(def_id).as_interned_str(); return tcx.item_name(def_id).as_interned_str();
} }
// 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 {
let key = tcx.def_key(ty_def_id);
match key.disambiguated_data.data {
DefPathData::TypeNs(_) | DefPathData::ValueNs(_) => {
instance_ty = tcx.type_of(ty_def_id);
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(|| {
bug!(
"finding type for {:?}, encountered def-id {:?} with no \
parent",
def_id,
ty_def_id
);
});
}
}
}
// Erase regions because they may not be deterministic when hashed let is_generic = substs.non_erasable_generics().next().is_some();
// and should not matter anyhow. let avoid_cross_crate_conflicts =
let instance_ty = tcx.erase_regions(&instance_ty); // 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 ||
let hash = get_symbol_hash(tcx, def_id, instance, instance_ty, substs); // 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
let mut printer = SymbolPrinter { // compliation (aka we're not making an internal copy in each of our
tcx, // codegen units) then this symbol may become an exported (but hidden
path: SymbolPath::new(), // visibility) symbol. This means that multiple crates may do the same
keep_within_component: false, // and we want to be sure to avoid any symbol conflicts here.
}.print_def_path(def_id, &[]).unwrap(); match MonoItem::Fn(instance).instantiation_mode(tcx) {
InstantiationMode::GloballyShared { may_conflict: true } => true,
if instance.is_vtable_shim() { _ => false,
let _ = printer.write_str("{{vtable-shim}}");
}
InternedString::intern(&printer.path.finish(hash))
}
// 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.
#[derive(Debug)]
struct SymbolPath {
result: String,
temp_buf: String,
}
impl SymbolPath {
fn new() -> Self {
let mut result = SymbolPath {
result: String::with_capacity(64),
temp_buf: String::with_capacity(16),
}; };
result.result.push_str("_ZN"); // _Z == Begin name-sequence, N == nested
result
}
fn finalize_pending_component(&mut self) { let instantiating_crate = if avoid_cross_crate_conflicts {
if !self.temp_buf.is_empty() { Some(if is_generic {
let _ = write!(self.result, "{}{}", self.temp_buf.len(), self.temp_buf); if !def_id.is_local() && tcx.sess.opts.share_generics() {
self.temp_buf.clear(); // If we are re-using a monomorphization from another crate,
} // we have to compute the symbol hash accordingly.
} let upstream_monomorphizations = tcx.upstream_monomorphizations_for(def_id);
fn finish(mut self, hash: u64) -> String { upstream_monomorphizations
self.finalize_pending_component(); .and_then(|monos| monos.get(&substs).cloned())
// E = end name-sequence .unwrap_or(LOCAL_CRATE)
let _ = write!(self.result, "17h{:016x}E", hash); } else {
self.result LOCAL_CRATE
}
}
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,
}
// 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.
impl Printer<'tcx, 'tcx> for SymbolPrinter<'_, 'tcx> {
type Error = fmt::Error;
type Path = Self;
type Region = Self;
type Type = Self;
type DynExistential = Self;
type Const = Self;
fn tcx(&'a self) -> TyCtxt<'a, 'tcx, 'tcx> {
self.tcx
}
fn print_region(
self,
_region: ty::Region<'_>,
) -> Result<Self::Region, Self::Error> {
Ok(self)
}
fn print_type(
self,
ty: Ty<'tcx>,
) -> Result<Self::Type, Self::Error> {
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 }, _) => {
self.print_def_path(def_id, substs)
} }
_ => self.pretty_print_type(ty),
}
}
fn print_dyn_existential(
mut self,
predicates: &'tcx ty::List<ty::ExistentialPredicate<'tcx>>,
) -> Result<Self::DynExistential, Self::Error> {
let mut first = true;
for p in predicates {
if !first {
write!(self, "+")?;
}
first = false;
self = p.print(self)?;
}
Ok(self)
}
fn print_const(
mut self,
ct: &'tcx ty::Const<'tcx>,
) -> Result<Self::Const, Self::Error> {
// only print integers
if let ConstValue::Scalar(Scalar::Raw { .. }) = ct.val {
if ct.ty.is_integral() {
return self.pretty_print_const(ct);
}
}
self.write_str("_")?;
Ok(self)
}
fn path_crate(
mut self,
cnum: CrateNum,
) -> Result<Self::Path, Self::Error> {
self.write_str(&self.tcx.original_crate_name(cnum).as_str())?;
Ok(self)
}
fn path_qualified(
self,
self_ty: Ty<'tcx>,
trait_ref: Option<ty::TraitRef<'tcx>>,
) -> Result<Self::Path, Self::Error> {
// 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)
}
}
fn path_append_impl(
self,
print_prefix: impl FnOnce(Self) -> Result<Self::Path, Self::Error>,
_disambiguated_data: &DisambiguatedDefPathData,
self_ty: Ty<'tcx>,
trait_ref: Option<ty::TraitRef<'tcx>>,
) -> Result<Self::Path, Self::Error> {
self.pretty_path_append_impl(
|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)
},
self_ty,
trait_ref,
)
}
fn path_append(
mut self,
print_prefix: impl FnOnce(Self) -> Result<Self::Path, Self::Error>,
disambiguated_data: &DisambiguatedDefPathData,
) -> Result<Self::Path, Self::Error> {
self = print_prefix(self)?;
// Skip `::{{constructor}}` on tuple/unit structs.
match disambiguated_data.data {
DefPathData::Ctor => return Ok(self),
_ => {}
}
if self.keep_within_component {
// HACK(eddyb) print the path similarly to how `FmtPrinter` prints it.
self.write_str("::")?;
} else { } else {
self.path.finalize_pending_component(); LOCAL_CRATE
} })
} else {
None
};
self.write_str(&disambiguated_data.data.as_interned_str().as_str())?; // Pick the crate responsible for the symbol mangling version, which has to:
Ok(self) // 1. be stable for each instance, whether it's being defined or imported
} // 2. obey each crate's own `-Z symbol-mangling-version`, as much as possible
fn path_generic_args( // We solve these as follows:
mut self, // 1. because symbol names depend on both `def_id` and `instantiating_crate`,
print_prefix: impl FnOnce(Self) -> Result<Self::Path, Self::Error>, // both their `CrateNum`s are stable for any given instance, so we can pick
args: &[Kind<'tcx>], // either and have a stable choice of symbol mangling version
) -> Result<Self::Path, Self::Error> { // 2. we favor `instantiating_crate` where possible (i.e. when `Some`)
self = print_prefix(self)?; let mangling_version_crate = instantiating_crate.unwrap_or(def_id.krate);
let mangling_version = if mangling_version_crate == LOCAL_CRATE {
tcx.sess.opts.debugging_opts.symbol_mangling_version
} else {
tcx.symbol_mangling_version(mangling_version_crate)
};
let args = args.iter().cloned().filter(|arg| { let mangled = match mangling_version {
match arg.unpack() { SymbolManglingVersion::Legacy => legacy::mangle(tcx, instance, instantiating_crate),
UnpackedKind::Lifetime(_) => false, SymbolManglingVersion::V0 => v0::mangle(tcx, instance, instantiating_crate, true),
_ => true, };
}
});
if args.clone().next().is_some() { InternedString::intern(&mangled)
self.generic_delimiters(|cx| cx.comma_sep(args))
} else {
Ok(self)
}
}
}
impl PrettyPrinter<'tcx, 'tcx> for SymbolPrinter<'_, 'tcx> {
fn region_should_not_be_omitted(
&self,
_region: ty::Region<'_>,
) -> bool {
false
}
fn comma_sep<T>(
mut self,
mut elems: impl Iterator<Item = T>,
) -> Result<Self, Self::Error>
where T: Print<'tcx, 'tcx, Self, Output = Self, Error = Self::Error>
{
if let Some(first) = elems.next() {
self = first.print(self)?;
for elem in elems {
self.write_str(",")?;
self = elem.print(self)?;
}
}
Ok(self)
}
fn generic_delimiters(
mut self,
f: impl FnOnce(Self) -> Result<Self, Self::Error>,
) -> Result<Self, Self::Error> {
write!(self, "<")?;
let kept_within_component =
mem::replace(&mut self.keep_within_component, true);
self = f(self)?;
self.keep_within_component = kept_within_component;
write!(self, ">")?;
Ok(self)
}
}
impl fmt::Write for SymbolPrinter<'_, '_> {
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() {
if self.path.temp_buf.is_empty() {
match c {
'a'..='z' | 'A'..='Z' | '_' => {}
_ => {
// Underscore-qualify anything that didn't start as an ident.
self.path.temp_buf.push('_');
}
}
}
match c {
// Escape these with $ sequences
'@' => 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() => {
// NVPTX doesn't support these characters in symbol names.
self.path.temp_buf.push('$')
}
// '.' doesn't occur in types and functions, so reuse it
// for ':' and '-'
'-' | ':' => self.path.temp_buf.push('.'),
// Avoid segmentation fault on some platforms, see #60925.
'm' if self.path.temp_buf.ends_with(".llv") => self.path.temp_buf.push_str("$6d$"),
// These are legal symbols
'a'..='z' | 'A'..='Z' | '0'..='9' | '_' | '.' | '$' => self.path.temp_buf.push(c),
_ => {
self.path.temp_buf.push('$');
for c in c.escape_unicode().skip(1) {
match c {
'{' => {}
'}' => self.path.temp_buf.push('$'),
c => self.path.temp_buf.push(c),
}
}
}
}
}
Ok(())
}
} }

View File

@ -0,0 +1,463 @@
use rustc::hir::def_id::CrateNum;
use rustc::hir::map::{DefPathData, DisambiguatedDefPathData};
use rustc::ich::NodeIdHashingMode;
use rustc::mir::interpret::{ConstValue, Scalar};
use rustc::ty::print::{PrettyPrinter, Printer, Print};
use rustc::ty::subst::{Kind, UnpackedKind};
use rustc::ty::{self, Ty, TyCtxt, TypeFoldable};
use rustc::util::common::record_time;
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
use rustc_mir::monomorphize::Instance;
use log::debug;
use std::fmt::{self, Write};
use std::mem::{self, discriminant};
pub(super) fn mangle(
tcx: TyCtxt<'_, 'tcx, 'tcx>,
instance: Instance<'tcx>,
instantiating_crate: Option<CrateNum>,
) -> String {
let def_id = instance.def_id();
// 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 {
let key = tcx.def_key(ty_def_id);
match key.disambiguated_data.data {
DefPathData::TypeNs(_) | DefPathData::ValueNs(_) => {
instance_ty = tcx.type_of(ty_def_id);
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(|| {
bug!(
"finding type for {:?}, encountered def-id {:?} with no \
parent",
def_id,
ty_def_id
);
});
}
}
}
// Erase regions because they may not be deterministic when hashed
// and should not matter anyhow.
let instance_ty = tcx.erase_regions(&instance_ty);
let hash = get_symbol_hash(tcx, instance, instance_ty, instantiating_crate);
let mut printer = SymbolPrinter {
tcx,
path: SymbolPath::new(),
keep_within_component: false,
}.print_def_path(def_id, &[]).unwrap();
if instance.is_vtable_shim() {
let _ = printer.write_str("{{vtable-shim}}");
}
printer.path.finish(hash)
}
fn get_symbol_hash<'a, 'tcx>(
tcx: TyCtxt<'a, 'tcx, 'tcx>,
// instance this name will be for
instance: Instance<'tcx>,
// type of the item, without any generic
// parameters substituted; this is
// included in the hash as a kind of
// safeguard.
item_type: Ty<'tcx>,
instantiating_crate: Option<CrateNum>,
) -> u64 {
let def_id = instance.def_id();
let substs = instance.substs;
debug!(
"get_symbol_hash(def_id={:?}, parameters={:?})",
def_id, substs
);
let mut hasher = StableHasher::<u64>::new();
let mut hcx = tcx.create_stable_hashing_context();
record_time(&tcx.sess.perf_stats.symbol_hash_time, || {
// the main symbol name is not necessarily unique; hash in the
// compiler's internal def-path, guaranteeing each symbol has a
// truly unique path
tcx.def_path_hash(def_id).hash_stable(&mut hcx, &mut hasher);
// 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());
hcx.while_hashing_spans(false, |hcx| {
hcx.with_node_id_hashing_mode(NodeIdHashingMode::HashDefPath, |hcx| {
item_type.hash_stable(hcx, &mut hasher);
});
});
// 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.
if let ty::FnDef(..) = item_type.sty {
item_type.fn_sig(tcx).hash_stable(&mut hcx, &mut hasher);
}
// also include any type parameters (for generic items)
assert!(!substs.has_erasable_regions());
assert!(!substs.needs_subst());
substs.hash_stable(&mut hcx, &mut hasher);
if let Some(instantiating_crate) = instantiating_crate {
(&tcx.original_crate_name(instantiating_crate).as_str()[..])
.hash_stable(&mut hcx, &mut hasher);
(&tcx.crate_disambiguator(instantiating_crate)).hash_stable(&mut hcx, &mut hasher);
}
// 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);
});
// 64 bits should be enough to avoid collisions.
hasher.finish()
}
// 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.
#[derive(Debug)]
struct SymbolPath {
result: String,
temp_buf: String,
}
impl SymbolPath {
fn new() -> Self {
let mut result = SymbolPath {
result: String::with_capacity(64),
temp_buf: String::with_capacity(16),
};
result.result.push_str("_ZN"); // _Z == Begin name-sequence, N == nested
result
}
fn finalize_pending_component(&mut self) {
if !self.temp_buf.is_empty() {
let _ = write!(self.result, "{}{}", self.temp_buf.len(), self.temp_buf);
self.temp_buf.clear();
}
}
fn finish(mut self, hash: u64) -> String {
self.finalize_pending_component();
// E = end name-sequence
let _ = write!(self.result, "17h{:016x}E", hash);
self.result
}
}
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,
}
// 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.
impl Printer<'tcx, 'tcx> for SymbolPrinter<'_, 'tcx> {
type Error = fmt::Error;
type Path = Self;
type Region = Self;
type Type = Self;
type DynExistential = Self;
type Const = Self;
fn tcx(&'a self) -> TyCtxt<'a, 'tcx, 'tcx> {
self.tcx
}
fn print_region(
self,
_region: ty::Region<'_>,
) -> Result<Self::Region, Self::Error> {
Ok(self)
}
fn print_type(
self,
ty: Ty<'tcx>,
) -> Result<Self::Type, Self::Error> {
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 }, _) => {
self.print_def_path(def_id, substs)
}
_ => self.pretty_print_type(ty),
}
}
fn print_dyn_existential(
mut self,
predicates: &'tcx ty::List<ty::ExistentialPredicate<'tcx>>,
) -> Result<Self::DynExistential, Self::Error> {
let mut first = true;
for p in predicates {
if !first {
write!(self, "+")?;
}
first = false;
self = p.print(self)?;
}
Ok(self)
}
fn print_const(
mut self,
ct: &'tcx ty::Const<'tcx>,
) -> Result<Self::Const, Self::Error> {
// only print integers
if let ConstValue::Scalar(Scalar::Raw { .. }) = ct.val {
if ct.ty.is_integral() {
return self.pretty_print_const(ct);
}
}
self.write_str("_")?;
Ok(self)
}
fn path_crate(
mut self,
cnum: CrateNum,
) -> Result<Self::Path, Self::Error> {
self.write_str(&self.tcx.original_crate_name(cnum).as_str())?;
Ok(self)
}
fn path_qualified(
self,
self_ty: Ty<'tcx>,
trait_ref: Option<ty::TraitRef<'tcx>>,
) -> Result<Self::Path, Self::Error> {
// 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)
}
}
fn path_append_impl(
self,
print_prefix: impl FnOnce(Self) -> Result<Self::Path, Self::Error>,
_disambiguated_data: &DisambiguatedDefPathData,
self_ty: Ty<'tcx>,
trait_ref: Option<ty::TraitRef<'tcx>>,
) -> Result<Self::Path, Self::Error> {
self.pretty_path_append_impl(
|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)
},
self_ty,
trait_ref,
)
}
fn path_append(
mut self,
print_prefix: impl FnOnce(Self) -> Result<Self::Path, Self::Error>,
disambiguated_data: &DisambiguatedDefPathData,
) -> Result<Self::Path, Self::Error> {
self = print_prefix(self)?;
// Skip `::{{constructor}}` on tuple/unit structs.
match disambiguated_data.data {
DefPathData::Ctor => return Ok(self),
_ => {}
}
if self.keep_within_component {
// HACK(eddyb) print the path similarly to how `FmtPrinter` prints it.
self.write_str("::")?;
} else {
self.path.finalize_pending_component();
}
self.write_str(&disambiguated_data.data.as_interned_str().as_str())?;
Ok(self)
}
fn path_generic_args(
mut self,
print_prefix: impl FnOnce(Self) -> Result<Self::Path, Self::Error>,
args: &[Kind<'tcx>],
) -> Result<Self::Path, Self::Error> {
self = print_prefix(self)?;
let args = args.iter().cloned().filter(|arg| {
match arg.unpack() {
UnpackedKind::Lifetime(_) => false,
_ => true,
}
});
if args.clone().next().is_some() {
self.generic_delimiters(|cx| cx.comma_sep(args))
} else {
Ok(self)
}
}
}
impl PrettyPrinter<'tcx, 'tcx> for SymbolPrinter<'_, 'tcx> {
fn region_should_not_be_omitted(
&self,
_region: ty::Region<'_>,
) -> bool {
false
}
fn comma_sep<T>(
mut self,
mut elems: impl Iterator<Item = T>,
) -> Result<Self, Self::Error>
where T: Print<'tcx, 'tcx, Self, Output = Self, Error = Self::Error>
{
if let Some(first) = elems.next() {
self = first.print(self)?;
for elem in elems {
self.write_str(",")?;
self = elem.print(self)?;
}
}
Ok(self)
}
fn generic_delimiters(
mut self,
f: impl FnOnce(Self) -> Result<Self, Self::Error>,
) -> Result<Self, Self::Error> {
write!(self, "<")?;
let kept_within_component =
mem::replace(&mut self.keep_within_component, true);
self = f(self)?;
self.keep_within_component = kept_within_component;
write!(self, ">")?;
Ok(self)
}
}
impl fmt::Write for SymbolPrinter<'_, '_> {
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() {
if self.path.temp_buf.is_empty() {
match c {
'a'..='z' | 'A'..='Z' | '_' => {}
_ => {
// Underscore-qualify anything that didn't start as an ident.
self.path.temp_buf.push('_');
}
}
}
match c {
// Escape these with $ sequences
'@' => 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() => {
// NVPTX doesn't support these characters in symbol names.
self.path.temp_buf.push('$')
}
// '.' doesn't occur in types and functions, so reuse it
// for ':' and '-'
'-' | ':' => self.path.temp_buf.push('.'),
// Avoid crashing LLVM in certain (LTO-related) situations, see #60925.
'm' if self.path.temp_buf.ends_with(".llv") => self.path.temp_buf.push_str("$6d$"),
// These are legal symbols
'a'..='z' | 'A'..='Z' | '0'..='9' | '_' | '.' | '$' => self.path.temp_buf.push(c),
_ => {
self.path.temp_buf.push('$');
for c in c.escape_unicode().skip(1) {
match c {
'{' => {}
'}' => self.path.temp_buf.push('$'),
c => self.path.temp_buf.push(c),
}
}
}
}
}
Ok(())
}
}

View File

@ -0,0 +1,664 @@
use rustc::hir;
use rustc::hir::def_id::{CrateNum, DefId};
use rustc::hir::map::{DefPathData, DisambiguatedDefPathData};
use rustc::ty::{self, Ty, TyCtxt, TypeFoldable};
use rustc::ty::print::{Printer, Print};
use rustc::ty::subst::{Kind, Subst, UnpackedKind};
use rustc_data_structures::base_n;
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
use rustc_mir::monomorphize::Instance;
use rustc_target::spec::abi::Abi;
use syntax::ast::{IntTy, UintTy, FloatTy};
use std::fmt::Write;
use std::ops::Range;
pub(super) fn mangle(
tcx: TyCtxt<'_, 'tcx, 'tcx>,
instance: Instance<'tcx>,
instantiating_crate: Option<CrateNum>,
compress: bool,
) -> String {
let def_id = instance.def_id();
// FIXME(eddyb) this should ideally not be needed.
let substs =
tcx.normalize_erasing_regions(ty::ParamEnv::reveal_all(), instance.substs);
let prefix = "_R";
let mut cx = SymbolMangler {
tcx,
compress: if compress {
Some(Box::new(CompressionCaches {
start_offset: prefix.len(),
paths: FxHashMap::default(),
types: FxHashMap::default(),
consts: FxHashMap::default(),
}))
} else {
None
},
binders: vec![],
out: String::from(prefix),
};
cx = if instance.is_vtable_shim() {
cx.path_append_ns(
|cx| cx.print_def_path(def_id, substs),
'S',
0,
"",
).unwrap()
} else {
cx.print_def_path(def_id, substs).unwrap()
};
if let Some(instantiating_crate) = instantiating_crate {
cx = cx.print_def_path(instantiating_crate.as_def_id(), &[]).unwrap();
}
cx.out
}
struct CompressionCaches<'tcx> {
// The length of the prefix in `out` (e.g. 2 for `_R`).
start_offset: usize,
// The values are start positions in `out`, in bytes.
paths: FxHashMap<(DefId, &'tcx [Kind<'tcx>]), usize>,
types: FxHashMap<Ty<'tcx>, usize>,
consts: FxHashMap<&'tcx ty::Const<'tcx>, usize>,
}
struct BinderLevel {
/// The range of distances from the root of what's
/// being printed, to the lifetimes in a binder.
/// Specifically, a `BrAnon(i)` lifetime has depth
/// `lifetime_depths.start + i`, going away from the
/// the root and towards its use site, as `i` increases.
/// This is used to flatten rustc's pairing of `BrAnon`
/// (intra-binder disambiguation) with a `DebruijnIndex`
/// (binder addressing), to "true" de Bruijn indices,
/// by subtracting the depth of a certain lifetime, from
/// the innermost depth at its use site.
lifetime_depths: Range<u32>,
}
struct SymbolMangler<'a, 'tcx> {
tcx: TyCtxt<'a, 'tcx, 'tcx>,
compress: Option<Box<CompressionCaches<'tcx>>>,
binders: Vec<BinderLevel>,
out: String,
}
impl SymbolMangler<'_, 'tcx> {
fn push(&mut self, s: &str) {
self.out.push_str(s);
}
/// Push a `_`-terminated base 62 integer, using the format
/// specified in the RFC as `<base-62-number>`, that is:
/// * `x = 0` is encoded as just the `"_"` terminator
/// * `x > 0` is encoded as `x - 1` in base 62, followed by `"_"`,
/// e.g. `1` becomes `"0_"`, `62` becomes `"Z_"`, etc.
fn push_integer_62(&mut self, x: u64) {
if let Some(x) = x.checked_sub(1) {
base_n::push_str(x as u128, 62, &mut self.out);
}
self.push("_");
}
/// Push a `tag`-prefixed base 62 integer, when larger than `0`, that is:
/// * `x = 0` is encoded as `""` (nothing)
/// * `x > 0` is encoded as the `tag` followed by `push_integer_62(x - 1)`
/// e.g. `1` becomes `tag + "_"`, `2` becomes `tag + "0_"`, etc.
fn push_opt_integer_62(&mut self, tag: &str, x: u64) {
if let Some(x) = x.checked_sub(1) {
self.push(tag);
self.push_integer_62(x);
}
}
fn push_disambiguator(&mut self, dis: u64) {
self.push_opt_integer_62("s", dis);
}
fn push_ident(&mut self, ident: &str) {
let mut use_punycode = false;
for b in ident.bytes() {
match b {
b'_' | b'a'..=b'z' | b'A'..=b'Z' | b'0'..=b'9' => {}
0x80..=0xff => use_punycode = true,
_ => bug!("symbol_names: bad byte {} in ident {:?}", b, ident),
}
}
let punycode_string;
let ident = if use_punycode {
self.push("u");
// FIXME(eddyb) we should probably roll our own punycode implementation.
let mut punycode_bytes = match ::punycode::encode(ident) {
Ok(s) => s.into_bytes(),
Err(()) => bug!("symbol_names: punycode encoding failed for ident {:?}", ident),
};
// Replace `-` with `_`.
if let Some(c) = punycode_bytes.iter_mut().rfind(|&&mut c| c == b'-') {
*c = b'_';
}
// FIXME(eddyb) avoid rechecking UTF-8 validity.
punycode_string = String::from_utf8(punycode_bytes).unwrap();
&punycode_string
} else {
ident
};
let _ = write!(self.out, "{}", ident.len());
// Write a separating `_` if necessary (leading digit or `_`).
match ident.chars().next() {
Some('_') | Some('0'..='9') => {
self.push("_");
}
_ => {}
}
self.push(ident);
}
fn path_append_ns(
mut self,
print_prefix: impl FnOnce(Self) -> Result<Self, !>,
ns: char,
disambiguator: u64,
name: &str,
) -> Result<Self, !> {
self.push("N");
self.out.push(ns);
self = print_prefix(self)?;
self.push_disambiguator(disambiguator as u64);
self.push_ident(name);
Ok(self)
}
fn print_backref(mut self, i: usize) -> Result<Self, !> {
self.push("B");
self.push_integer_62((i - self.compress.as_ref().unwrap().start_offset) as u64);
Ok(self)
}
fn in_binder<T>(
mut self,
value: &ty::Binder<T>,
print_value: impl FnOnce(Self, &T) -> Result<Self, !>
) -> Result<Self, !>
where T: TypeFoldable<'tcx>
{
let regions = if value.has_late_bound_regions() {
self.tcx.collect_referenced_late_bound_regions(value)
} else {
FxHashSet::default()
};
let mut lifetime_depths =
self.binders.last().map(|b| b.lifetime_depths.end).map_or(0..0, |i| i..i);
let lifetimes = regions.into_iter().map(|br| {
match br {
ty::BrAnon(i) => i + 1,
_ => bug!("symbol_names: non-anonymized region `{:?}` in `{:?}`", br, value),
}
}).max().unwrap_or(0);
self.push_opt_integer_62("G", lifetimes as u64);
lifetime_depths.end += lifetimes;
self.binders.push(BinderLevel { lifetime_depths });
self = print_value(self, value.skip_binder())?;
self.binders.pop();
Ok(self)
}
}
impl Printer<'tcx, 'tcx> for SymbolMangler<'_, 'tcx> {
type Error = !;
type Path = Self;
type Region = Self;
type Type = Self;
type DynExistential = Self;
type Const = Self;
fn tcx<'a>(&'a self) -> TyCtxt<'a, 'tcx, 'tcx> {
self.tcx
}
fn print_def_path(
mut self,
def_id: DefId,
substs: &'tcx [Kind<'tcx>],
) -> Result<Self::Path, Self::Error> {
if let Some(&i) = self.compress.as_ref().and_then(|c| c.paths.get(&(def_id, substs))) {
return self.print_backref(i);
}
let start = self.out.len();
self = self.default_print_def_path(def_id, substs)?;
// Only cache paths that do not refer to an enclosing
// binder (which would change depending on context).
if !substs.iter().any(|k| k.has_escaping_bound_vars()) {
if let Some(c) = &mut self.compress {
c.paths.insert((def_id, substs), start);
}
}
Ok(self)
}
fn print_impl_path(
self,
impl_def_id: DefId,
substs: &'tcx [Kind<'tcx>],
mut self_ty: Ty<'tcx>,
mut impl_trait_ref: Option<ty::TraitRef<'tcx>>,
) -> Result<Self::Path, Self::Error> {
let key = self.tcx.def_key(impl_def_id);
let parent_def_id = DefId { index: key.parent.unwrap(), ..impl_def_id };
let mut param_env = self.tcx.param_env(impl_def_id)
.with_reveal_all();
if !substs.is_empty() {
param_env = param_env.subst(self.tcx, substs);
}
match &mut impl_trait_ref {
Some(impl_trait_ref) => {
assert_eq!(impl_trait_ref.self_ty(), self_ty);
*impl_trait_ref =
self.tcx.normalize_erasing_regions(param_env, *impl_trait_ref);
self_ty = impl_trait_ref.self_ty();
}
None => {
self_ty = self.tcx.normalize_erasing_regions(param_env, self_ty);
}
}
self.path_append_impl(
|cx| cx.print_def_path(parent_def_id, &[]),
&key.disambiguated_data,
self_ty,
impl_trait_ref,
)
}
fn print_region(
mut self,
region: ty::Region<'_>,
) -> Result<Self::Region, Self::Error> {
let i = match *region {
// Erased lifetimes use the index 0, for a
// shorter mangling of `L_`.
ty::ReErased => 0,
// Late-bound lifetimes use indices starting at 1,
// see `BinderLevel` for more details.
ty::ReLateBound(debruijn, ty::BrAnon(i)) => {
let binder = &self.binders[self.binders.len() - 1 - debruijn.index()];
let depth = binder.lifetime_depths.start + i;
1 + (self.binders.last().unwrap().lifetime_depths.end - 1 - depth)
}
_ => bug!("symbol_names: non-erased region `{:?}`", region),
};
self.push("L");
self.push_integer_62(i as u64);
Ok(self)
}
fn print_type(
mut self,
ty: Ty<'tcx>,
) -> Result<Self::Type, Self::Error> {
// Basic types, never cached (single-character).
let basic_type = match ty.sty {
ty::Bool => "b",
ty::Char => "c",
ty::Str => "e",
ty::Tuple(_) if ty.is_unit() => "u",
ty::Int(IntTy::I8) => "a",
ty::Int(IntTy::I16) => "s",
ty::Int(IntTy::I32) => "l",
ty::Int(IntTy::I64) => "x",
ty::Int(IntTy::I128) => "n",
ty::Int(IntTy::Isize) => "i",
ty::Uint(UintTy::U8) => "h",
ty::Uint(UintTy::U16) => "t",
ty::Uint(UintTy::U32) => "m",
ty::Uint(UintTy::U64) => "y",
ty::Uint(UintTy::U128) => "o",
ty::Uint(UintTy::Usize) => "j",
ty::Float(FloatTy::F32) => "f",
ty::Float(FloatTy::F64) => "d",
ty::Never => "z",
// Placeholders (should be demangled as `_`).
ty::Param(_) | ty::Bound(..) | ty::Placeholder(_) |
ty::Infer(_) | ty::Error => "p",
_ => "",
};
if !basic_type.is_empty() {
self.push(basic_type);
return Ok(self);
}
if let Some(&i) = self.compress.as_ref().and_then(|c| c.types.get(&ty)) {
return self.print_backref(i);
}
let start = self.out.len();
match ty.sty {
// Basic types, handled above.
ty::Bool | ty::Char | ty::Str |
ty::Int(_) | ty::Uint(_) | ty::Float(_) |
ty::Never => unreachable!(),
ty::Tuple(_) if ty.is_unit() => unreachable!(),
// Placeholders, also handled as part of basic types.
ty::Param(_) | ty::Bound(..) | ty::Placeholder(_) |
ty::Infer(_) | ty::Error => unreachable!(),
ty::Ref(r, ty, mutbl) => {
self.push(match mutbl {
hir::MutImmutable => "R",
hir::MutMutable => "Q",
});
if *r != ty::ReErased {
self = r.print(self)?;
}
self = ty.print(self)?;
}
ty::RawPtr(mt) => {
self.push(match mt.mutbl {
hir::MutImmutable => "P",
hir::MutMutable => "O",
});
self = mt.ty.print(self)?;
}
ty::Array(ty, len) => {
self.push("A");
self = ty.print(self)?;
self = self.print_const(len)?;
}
ty::Slice(ty) => {
self.push("S");
self = ty.print(self)?;
}
ty::Tuple(tys) => {
self.push("T");
for ty in tys.iter().map(|k| k.expect_ty()) {
self = ty.print(self)?;
}
self.push("E");
}
// Mangle all nominal types as paths.
ty::Adt(&ty::AdtDef { did: def_id, .. }, substs) |
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 }, _) => {
self = self.print_def_path(def_id, substs)?;
}
ty::Foreign(def_id) => {
self = self.print_def_path(def_id, &[])?;
}
ty::FnPtr(sig) => {
self.push("F");
self = self.in_binder(&sig, |mut cx, sig| {
if sig.unsafety == hir::Unsafety::Unsafe {
cx.push("U");
}
match sig.abi {
Abi::Rust => {}
Abi::C => cx.push("KC"),
abi => {
cx.push("K");
let name = abi.name();
if name.contains('-') {
cx.push_ident(&name.replace('-', "_"));
} else {
cx.push_ident(name);
}
}
}
for &ty in sig.inputs() {
cx = ty.print(cx)?;
}
if sig.c_variadic {
cx.push("v");
}
cx.push("E");
sig.output().print(cx)
})?;
}
ty::Dynamic(predicates, r) => {
self.push("D");
self = self.in_binder(&predicates, |cx, predicates| {
cx.print_dyn_existential(predicates)
})?;
self = r.print(self)?;
}
ty::GeneratorWitness(_) => {
bug!("symbol_names: unexpected `GeneratorWitness`")
}
}
// Only cache types that do not refer to an enclosing
// binder (which would change depending on context).
if !ty.has_escaping_bound_vars() {
if let Some(c) = &mut self.compress {
c.types.insert(ty, start);
}
}
Ok(self)
}
fn print_dyn_existential(
mut self,
predicates: &'tcx ty::List<ty::ExistentialPredicate<'tcx>>,
) -> Result<Self::DynExistential, Self::Error> {
for predicate in predicates {
match *predicate {
ty::ExistentialPredicate::Trait(trait_ref) => {
// Use a type that can't appear in defaults of type parameters.
let dummy_self = self.tcx.mk_ty_infer(ty::FreshTy(0));
let trait_ref = trait_ref.with_self_ty(self.tcx, dummy_self);
self = self.print_def_path(trait_ref.def_id, trait_ref.substs)?;
}
ty::ExistentialPredicate::Projection(projection) => {
let name = self.tcx.associated_item(projection.item_def_id).ident;
self.push("p");
self.push_ident(&name.as_str());
self = projection.ty.print(self)?;
}
ty::ExistentialPredicate::AutoTrait(def_id) => {
self = self.print_def_path(def_id, &[])?;
}
}
}
self.push("E");
Ok(self)
}
fn print_const(
mut self,
ct: &'tcx ty::Const<'tcx>,
) -> Result<Self::Const, Self::Error> {
if let Some(&i) = self.compress.as_ref().and_then(|c| c.consts.get(&ct)) {
return self.print_backref(i);
}
let start = self.out.len();
match ct.ty.sty {
ty::Uint(_) => {}
_ => {
bug!("symbol_names: unsupported constant of type `{}` ({:?})",
ct.ty, ct);
}
}
self = ct.ty.print(self)?;
if let Some(bits) = ct.assert_bits(self.tcx, ty::ParamEnv::empty().and(ct.ty)) {
let _ = write!(self.out, "{:x}_", bits);
} else {
// NOTE(eddyb) despite having the path, we need to
// encode a placeholder, as the path could refer
// back to e.g. an `impl` using the constant.
self.push("p");
}
// Only cache consts that do not refer to an enclosing
// binder (which would change depending on context).
if !ct.has_escaping_bound_vars() {
if let Some(c) = &mut self.compress {
c.consts.insert(ct, start);
}
}
Ok(self)
}
fn path_crate(
mut self,
cnum: CrateNum,
) -> Result<Self::Path, Self::Error> {
self.push("C");
let fingerprint = self.tcx.crate_disambiguator(cnum).to_fingerprint();
self.push_disambiguator(fingerprint.to_smaller_hash());
let name = self.tcx.original_crate_name(cnum).as_str();
self.push_ident(&name);
Ok(self)
}
fn path_qualified(
mut self,
self_ty: Ty<'tcx>,
trait_ref: Option<ty::TraitRef<'tcx>>,
) -> Result<Self::Path, Self::Error> {
assert!(trait_ref.is_some());
let trait_ref = trait_ref.unwrap();
self.push("Y");
self = self_ty.print(self)?;
self.print_def_path(trait_ref.def_id, trait_ref.substs)
}
fn path_append_impl(
mut self,
print_prefix: impl FnOnce(Self) -> Result<Self::Path, Self::Error>,
disambiguated_data: &DisambiguatedDefPathData,
self_ty: Ty<'tcx>,
trait_ref: Option<ty::TraitRef<'tcx>>,
) -> Result<Self::Path, Self::Error> {
self.push(match trait_ref {
Some(_) => "X",
None => "M",
});
self.push_disambiguator(disambiguated_data.disambiguator as u64);
self = print_prefix(self)?;
self = self_ty.print(self)?;
if let Some(trait_ref) = trait_ref {
self = self.print_def_path(trait_ref.def_id, trait_ref.substs)?;
}
Ok(self)
}
fn path_append(
self,
print_prefix: impl FnOnce(Self) -> Result<Self::Path, Self::Error>,
disambiguated_data: &DisambiguatedDefPathData,
) -> Result<Self::Path, Self::Error> {
let ns = match disambiguated_data.data {
// Avoid putting the burden on demanglers to ignore this.
DefPathData::Ctor => return print_prefix(self),
// Uppercase categories are more stable than lowercase ones.
DefPathData::TypeNs(_) => 't',
DefPathData::ValueNs(_) => 'v',
DefPathData::ClosureExpr => 'C',
DefPathData::AnonConst => 'k',
DefPathData::ImplTrait => 'i',
// These should never show up as `path_append` arguments.
DefPathData::CrateRoot
| DefPathData::Misc
| DefPathData::Impl
| DefPathData::MacroNs(_)
| DefPathData::LifetimeNs(_)
| DefPathData::GlobalMetaData(_) => {
bug!("symbol_names: unexpected DefPathData: {:?}", disambiguated_data.data)
}
};
let name = disambiguated_data.data.get_opt_name().map(|s| s.as_str());
self.path_append_ns(
print_prefix,
ns,
disambiguated_data.disambiguator as u64,
name.as_ref().map_or("", |s| &s[..])
)
}
fn path_generic_args(
mut self,
print_prefix: impl FnOnce(Self) -> Result<Self::Path, Self::Error>,
args: &[Kind<'tcx>],
) -> Result<Self::Path, Self::Error> {
// Don't print any regions if they're all erased.
let print_regions = args.iter().any(|arg| {
match arg.unpack() {
UnpackedKind::Lifetime(r) => *r != ty::ReErased,
_ => false,
}
});
let args = args.iter().cloned().filter(|arg| {
match arg.unpack() {
UnpackedKind::Lifetime(_) => print_regions,
_ => true,
}
});
if args.clone().next().is_none() {
return print_prefix(self);
}
self.push("I");
self = print_prefix(self)?;
for arg in args {
match arg.unpack() {
UnpackedKind::Lifetime(lt) => {
self = lt.print(self)?;
}
UnpackedKind::Type(ty) => {
self = ty.print(self)?;
}
UnpackedKind::Const(c) => {
self.push("K");
// FIXME(const_generics) implement `ty::print::Print` on `ty::Const`.
// self = c.print(self)?;
self = self.print_const(c)?;
}
}
}
self.push("E");
Ok(self)
}
}

View File

@ -176,6 +176,7 @@ provide! { <'tcx> tcx, def_id, other, cdata,
r.map(|c| &*tcx.arena.alloc(c)) r.map(|c| &*tcx.arena.alloc(c))
} }
is_no_builtins => { cdata.root.no_builtins } is_no_builtins => { cdata.root.no_builtins }
symbol_mangling_version => { cdata.root.symbol_mangling_version }
impl_defaultness => { cdata.get_impl_defaultness(def_id.index) } impl_defaultness => { cdata.get_impl_defaultness(def_id.index) }
reachable_non_generics => { reachable_non_generics => {
let reachable_non_generics = tcx let reachable_non_generics = tcx

View File

@ -498,6 +498,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
panic_runtime: attr::contains_name(&attrs, sym::panic_runtime), panic_runtime: attr::contains_name(&attrs, sym::panic_runtime),
profiler_runtime: attr::contains_name(&attrs, sym::profiler_runtime), profiler_runtime: attr::contains_name(&attrs, sym::profiler_runtime),
sanitizer_runtime: attr::contains_name(&attrs, sym::sanitizer_runtime), sanitizer_runtime: attr::contains_name(&attrs, sym::sanitizer_runtime),
symbol_mangling_version: tcx.sess.opts.debugging_opts.symbol_mangling_version,
crate_deps, crate_deps,
dylib_dependency_formats, dylib_dependency_formats,

View File

@ -8,6 +8,7 @@ use rustc::middle::cstore::{DepKind, LinkagePreference, NativeLibrary, ForeignMo
use rustc::middle::lang_items; use rustc::middle::lang_items;
use rustc::mir; use rustc::mir;
use rustc::session::CrateDisambiguator; use rustc::session::CrateDisambiguator;
use rustc::session::config::SymbolManglingVersion;
use rustc::ty::{self, Ty, ReprOptions}; use rustc::ty::{self, Ty, ReprOptions};
use rustc_target::spec::{PanicStrategy, TargetTriple}; use rustc_target::spec::{PanicStrategy, TargetTriple};
use rustc_data_structures::svh::Svh; use rustc_data_structures::svh::Svh;
@ -189,6 +190,7 @@ pub struct CrateRoot<'tcx> {
pub panic_runtime: bool, pub panic_runtime: bool,
pub profiler_runtime: bool, pub profiler_runtime: bool,
pub sanitizer_runtime: bool, pub sanitizer_runtime: bool,
pub symbol_mangling_version: SymbolManglingVersion,
} }
#[derive(RustcEncodable, RustcDecodable)] #[derive(RustcEncodable, RustcDecodable)]