rustc_symbol_mangling/
hashed.rs
1use std::fmt::Write;
2
3use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
4use rustc_hashes::Hash64;
5use rustc_hir::def_id::CrateNum;
6use rustc_middle::ty::{Instance, TyCtxt};
7
8use crate::v0;
9
10pub(super) fn mangle<'tcx>(
11 tcx: TyCtxt<'tcx>,
12 instance: Instance<'tcx>,
13 instantiating_crate: Option<CrateNum>,
14 full_mangling_name: impl FnOnce() -> String,
15) -> String {
16 let crate_num =
23 if let Some(krate) = instantiating_crate { krate } else { instance.def_id().krate };
24
25 let mut symbol = "_RNxC".to_string();
26 v0::push_ident(tcx.crate_name(crate_num).as_str(), &mut symbol);
27
28 let hash = tcx.with_stable_hashing_context(|mut hcx| {
29 let mut hasher = StableHasher::new();
30 full_mangling_name().hash_stable(&mut hcx, &mut hasher);
31 hasher.finish::<Hash64>().as_u64()
32 });
33
34 push_hash64(hash, &mut symbol);
35
36 symbol
37}
38
39fn push_hash64(hash: u64, output: &mut String) {
42 let hash = v0::encode_integer_62(hash);
43 let hash_len = hash.len();
44 let _ = write!(output, "{hash_len}H{}", &hash[..hash_len - 1]);
45}