use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
use rustc_hir::def_id::CrateNum;
use rustc_hir::definitions::{DefPathData, DisambiguatedDefPathData};
use rustc_middle::ich::NodeIdHashingMode;
use rustc_middle::mir::interpret::{ConstValue, Scalar};
use rustc_middle::ty::print::{PrettyPrinter, Print, Printer};
use rustc_middle::ty::subst::{GenericArg, GenericArgKind};
use rustc_middle::ty::{self, Instance, Ty, TyCtxt, TypeFoldable};
use rustc_middle::util::common::record_time;
use tracing::debug;
use std::fmt::{self, Write};
use std::mem::{self, discriminant};
pub(super) fn mangle(
tcx: TyCtxt<'tcx>,
instance: Instance<'tcx>,
instantiating_crate: Option<CrateNum>,
) -> String {
let def_id = instance.def_id();
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;
}
_ => {
ty_def_id.index = key.parent.unwrap_or_else(|| {
bug!(
"finding type for {:?}, encountered def-id {:?} with no \
parent",
def_id,
ty_def_id
);
});
}
}
}
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 let ty::InstanceDef::VtableShim(..) = instance.def {
let _ = printer.write_str("{{vtable-shim}}");
}
if let ty::InstanceDef::ReifyShim(..) = instance.def {
let _ = printer.write_str("{{reify-shim}}");
}
printer.path.finish(hash)
}
fn get_symbol_hash<'tcx>(
tcx: TyCtxt<'tcx>,
instance: Instance<'tcx>,
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::new();
let mut hcx = tcx.create_stable_hashing_context();
record_time(&tcx.sess.perf_stats.symbol_hash_time, || {
tcx.def_path_hash(def_id).hash_stable(&mut hcx, &mut hasher);
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 let ty::FnDef(..) = item_type.kind() {
item_type.fn_sig(tcx).hash_stable(&mut hcx, &mut hasher);
}
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);
}
discriminant(&instance.def).hash_stable(&mut hcx, &mut hasher);
});
hasher.finish::<u64>()
}
#[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");
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();
let _ = write!(self.result, "17h{:016x}E", hash);
self.result
}
}
struct SymbolPrinter<'tcx> {
tcx: TyCtxt<'tcx>,
path: SymbolPath,
keep_within_component: bool,
}
impl Printer<'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(&self) -> TyCtxt<'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.kind() {
ty::FnDef(def_id, substs)
| ty::Opaque(def_id, substs)
| ty::Projection(ty::ProjectionTy { item_def_id: def_id, substs })
| ty::Closure(def_id, substs)
| ty::Generator(def_id, 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> {
if let ty::ConstKind::Value(ConstValue::Scalar(Scalar::Int { .. })) = ct.val {
if ct.ty.is_integral() {
return self.pretty_print_const(ct, true);
}
}
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> {
match self_ty.kind() {
ty::FnDef(..)
| ty::Opaque(..)
| ty::Projection(_)
| 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 {
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)?;
if let DefPathData::Ctor = disambiguated_data.data {
return Ok(self);
}
if self.keep_within_component {
self.write_str("::")?;
} else {
self.path.finalize_pending_component();
}
write!(self, "{}", disambiguated_data.data)?;
Ok(self)
}
fn path_generic_args(
mut self,
print_prefix: impl FnOnce(Self) -> Result<Self::Path, Self::Error>,
args: &[GenericArg<'tcx>],
) -> Result<Self::Path, Self::Error> {
self = print_prefix(self)?;
let args =
args.iter().cloned().filter(|arg| !matches!(arg.unpack(), GenericArgKind::Lifetime(_)));
if args.clone().next().is_some() {
self.generic_delimiters(|cx| cx.comma_sep(args))
} else {
Ok(self)
}
}
}
impl PrettyPrinter<'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, 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 {
for c in s.chars() {
if self.path.temp_buf.is_empty() {
match c {
'a'..='z' | 'A'..='Z' | '_' => {}
_ => {
self.path.temp_buf.push('_');
}
}
}
match c {
'@' => 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() => {
self.path.temp_buf.push('$')
}
'-' | ':' => self.path.temp_buf.push('.'),
'm' if self.path.temp_buf.ends_with(".llv") => self.path.temp_buf.push_str("$u6d$"),
'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(())
}
}