]> git.proxmox.com Git - rustc.git/blob - compiler/rustc_symbol_mangling/src/legacy.rs
New upstream version 1.65.0+dfsg1
[rustc.git] / compiler / rustc_symbol_mangling / src / legacy.rs
1 use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
2 use rustc_hir::def_id::CrateNum;
3 use rustc_hir::definitions::{DefPathData, DisambiguatedDefPathData};
4 use rustc_middle::ty::print::{PrettyPrinter, Print, Printer};
5 use rustc_middle::ty::subst::{GenericArg, GenericArgKind};
6 use rustc_middle::ty::{self, Instance, Ty, TyCtxt, TypeVisitable};
7 use rustc_middle::util::common::record_time;
8
9 use std::fmt::{self, Write};
10 use std::mem::{self, discriminant};
11
12 pub(super) fn mangle<'tcx>(
13 tcx: TyCtxt<'tcx>,
14 instance: Instance<'tcx>,
15 instantiating_crate: Option<CrateNum>,
16 ) -> String {
17 let def_id = instance.def_id();
18
19 // We want to compute the "type" of this item. Unfortunately, some
20 // kinds of items (e.g., closures) don't have an entry in the
21 // item-type array. So walk back up the find the closest parent
22 // that DOES have an entry.
23 let mut ty_def_id = def_id;
24 let instance_ty;
25 loop {
26 let key = tcx.def_key(ty_def_id);
27 match key.disambiguated_data.data {
28 DefPathData::TypeNs(_) | DefPathData::ValueNs(_) => {
29 instance_ty = tcx.type_of(ty_def_id);
30 debug!(?instance_ty);
31 break;
32 }
33 _ => {
34 // if we're making a symbol for something, there ought
35 // to be a value or type-def or something in there
36 // *somewhere*
37 ty_def_id.index = key.parent.unwrap_or_else(|| {
38 bug!(
39 "finding type for {:?}, encountered def-id {:?} with no \
40 parent",
41 def_id,
42 ty_def_id
43 );
44 });
45 }
46 }
47 }
48
49 // Erase regions because they may not be deterministic when hashed
50 // and should not matter anyhow.
51 let instance_ty = tcx.erase_regions(instance_ty);
52
53 let hash = get_symbol_hash(tcx, instance, instance_ty, instantiating_crate);
54
55 let mut printer = SymbolPrinter { tcx, path: SymbolPath::new(), keep_within_component: false };
56 printer
57 .print_def_path(
58 def_id,
59 if let ty::InstanceDef::DropGlue(_, _) = instance.def {
60 // Add the name of the dropped type to the symbol name
61 &*instance.substs
62 } else {
63 &[]
64 },
65 )
66 .unwrap();
67
68 if let ty::InstanceDef::VTableShim(..) = instance.def {
69 let _ = printer.write_str("{{vtable-shim}}");
70 }
71
72 if let ty::InstanceDef::ReifyShim(..) = instance.def {
73 let _ = printer.write_str("{{reify-shim}}");
74 }
75
76 printer.path.finish(hash)
77 }
78
79 fn get_symbol_hash<'tcx>(
80 tcx: TyCtxt<'tcx>,
81
82 // instance this name will be for
83 instance: Instance<'tcx>,
84
85 // type of the item, without any generic
86 // parameters substituted; this is
87 // included in the hash as a kind of
88 // safeguard.
89 item_type: Ty<'tcx>,
90
91 instantiating_crate: Option<CrateNum>,
92 ) -> u64 {
93 let def_id = instance.def_id();
94 let substs = instance.substs;
95 debug!("get_symbol_hash(def_id={:?}, parameters={:?})", def_id, substs);
96
97 tcx.with_stable_hashing_context(|mut hcx| {
98 let mut hasher = StableHasher::new();
99
100 record_time(&tcx.sess.perf_stats.symbol_hash_time, || {
101 // the main symbol name is not necessarily unique; hash in the
102 // compiler's internal def-path, guaranteeing each symbol has a
103 // truly unique path
104 tcx.def_path_hash(def_id).hash_stable(&mut hcx, &mut hasher);
105
106 // Include the main item-type. Note that, in this case, the
107 // assertions about `needs_subst` may not hold, but this item-type
108 // ought to be the same for every reference anyway.
109 assert!(!item_type.has_erasable_regions());
110 hcx.while_hashing_spans(false, |hcx| {
111 item_type.hash_stable(hcx, &mut hasher);
112
113 // If this is a function, we hash the signature as well.
114 // This is not *strictly* needed, but it may help in some
115 // situations, see the `run-make/a-b-a-linker-guard` test.
116 if let ty::FnDef(..) = item_type.kind() {
117 item_type.fn_sig(tcx).hash_stable(hcx, &mut hasher);
118 }
119
120 // also include any type parameters (for generic items)
121 substs.hash_stable(hcx, &mut hasher);
122
123 if let Some(instantiating_crate) = instantiating_crate {
124 tcx.def_path_hash(instantiating_crate.as_def_id())
125 .stable_crate_id()
126 .hash_stable(hcx, &mut hasher);
127 }
128
129 // We want to avoid accidental collision between different types of instances.
130 // Especially, `VTableShim`s and `ReifyShim`s may overlap with their original
131 // instances without this.
132 discriminant(&instance.def).hash_stable(hcx, &mut hasher);
133 });
134 });
135
136 // 64 bits should be enough to avoid collisions.
137 hasher.finish::<u64>()
138 })
139 }
140
141 // Follow C++ namespace-mangling style, see
142 // https://en.wikipedia.org/wiki/Name_mangling for more info.
143 //
144 // It turns out that on macOS you can actually have arbitrary symbols in
145 // function names (at least when given to LLVM), but this is not possible
146 // when using unix's linker. Perhaps one day when we just use a linker from LLVM
147 // we won't need to do this name mangling. The problem with name mangling is
148 // that it seriously limits the available characters. For example we can't
149 // have things like &T in symbol names when one would theoretically
150 // want them for things like impls of traits on that type.
151 //
152 // To be able to work on all platforms and get *some* reasonable output, we
153 // use C++ name-mangling.
154 #[derive(Debug)]
155 struct SymbolPath {
156 result: String,
157 temp_buf: String,
158 }
159
160 impl SymbolPath {
161 fn new() -> Self {
162 let mut result =
163 SymbolPath { result: String::with_capacity(64), temp_buf: String::with_capacity(16) };
164 result.result.push_str("_ZN"); // _Z == Begin name-sequence, N == nested
165 result
166 }
167
168 fn finalize_pending_component(&mut self) {
169 if !self.temp_buf.is_empty() {
170 let _ = write!(self.result, "{}{}", self.temp_buf.len(), self.temp_buf);
171 self.temp_buf.clear();
172 }
173 }
174
175 fn finish(mut self, hash: u64) -> String {
176 self.finalize_pending_component();
177 // E = end name-sequence
178 let _ = write!(self.result, "17h{:016x}E", hash);
179 self.result
180 }
181 }
182
183 struct SymbolPrinter<'tcx> {
184 tcx: TyCtxt<'tcx>,
185 path: SymbolPath,
186
187 // When `true`, `finalize_pending_component` isn't used.
188 // This is needed when recursing into `path_qualified`,
189 // or `path_generic_args`, as any nested paths are
190 // logically within one component.
191 keep_within_component: bool,
192 }
193
194 // HACK(eddyb) this relies on using the `fmt` interface to get
195 // `PrettyPrinter` aka pretty printing of e.g. types in paths,
196 // symbol names should have their own printing machinery.
197
198 impl<'tcx> Printer<'tcx> for &mut SymbolPrinter<'tcx> {
199 type Error = fmt::Error;
200
201 type Path = Self;
202 type Region = Self;
203 type Type = Self;
204 type DynExistential = Self;
205 type Const = Self;
206
207 fn tcx(&self) -> TyCtxt<'tcx> {
208 self.tcx
209 }
210
211 fn print_region(self, _region: ty::Region<'_>) -> Result<Self::Region, Self::Error> {
212 Ok(self)
213 }
214
215 fn print_type(mut self, ty: Ty<'tcx>) -> Result<Self::Type, Self::Error> {
216 match *ty.kind() {
217 // Print all nominal types as paths (unlike `pretty_print_type`).
218 ty::FnDef(def_id, substs)
219 | ty::Opaque(def_id, substs)
220 | ty::Projection(ty::ProjectionTy { item_def_id: def_id, substs })
221 | ty::Closure(def_id, substs)
222 | ty::Generator(def_id, substs, _) => self.print_def_path(def_id, substs),
223
224 // The `pretty_print_type` formatting of array size depends on
225 // -Zverbose flag, so we cannot reuse it here.
226 ty::Array(ty, size) => {
227 self.write_str("[")?;
228 self = self.print_type(ty)?;
229 self.write_str("; ")?;
230 if let Some(size) = size.kind().try_to_bits(self.tcx().data_layout.pointer_size) {
231 write!(self, "{}", size)?
232 } else if let ty::ConstKind::Param(param) = size.kind() {
233 self = param.print(self)?
234 } else {
235 self.write_str("_")?
236 }
237 self.write_str("]")?;
238 Ok(self)
239 }
240
241 _ => self.pretty_print_type(ty),
242 }
243 }
244
245 fn print_dyn_existential(
246 mut self,
247 predicates: &'tcx ty::List<ty::Binder<'tcx, ty::ExistentialPredicate<'tcx>>>,
248 ) -> Result<Self::DynExistential, Self::Error> {
249 let mut first = true;
250 for p in predicates {
251 if !first {
252 write!(self, "+")?;
253 }
254 first = false;
255 self = p.print(self)?;
256 }
257 Ok(self)
258 }
259
260 fn print_const(self, ct: ty::Const<'tcx>) -> Result<Self::Const, Self::Error> {
261 // only print integers
262 match (ct.kind(), ct.ty().kind()) {
263 (ty::ConstKind::Value(ty::ValTree::Leaf(scalar)), ty::Int(_) | ty::Uint(_)) => {
264 // The `pretty_print_const` formatting depends on -Zverbose
265 // flag, so we cannot reuse it here.
266 let signed = matches!(ct.ty().kind(), ty::Int(_));
267 write!(
268 self,
269 "{:#?}",
270 ty::ConstInt::new(scalar, signed, ct.ty().is_ptr_sized_integral())
271 )?;
272 }
273 _ => self.write_str("_")?,
274 }
275 Ok(self)
276 }
277
278 fn path_crate(self, cnum: CrateNum) -> Result<Self::Path, Self::Error> {
279 self.write_str(self.tcx.crate_name(cnum).as_str())?;
280 Ok(self)
281 }
282 fn path_qualified(
283 self,
284 self_ty: Ty<'tcx>,
285 trait_ref: Option<ty::TraitRef<'tcx>>,
286 ) -> Result<Self::Path, Self::Error> {
287 // Similar to `pretty_path_qualified`, but for the other
288 // types that are printed as paths (see `print_type` above).
289 match self_ty.kind() {
290 ty::FnDef(..)
291 | ty::Opaque(..)
292 | ty::Projection(_)
293 | ty::Closure(..)
294 | ty::Generator(..)
295 if trait_ref.is_none() =>
296 {
297 self.print_type(self_ty)
298 }
299
300 _ => self.pretty_path_qualified(self_ty, trait_ref),
301 }
302 }
303
304 fn path_append_impl(
305 self,
306 print_prefix: impl FnOnce(Self) -> Result<Self::Path, Self::Error>,
307 _disambiguated_data: &DisambiguatedDefPathData,
308 self_ty: Ty<'tcx>,
309 trait_ref: Option<ty::TraitRef<'tcx>>,
310 ) -> Result<Self::Path, Self::Error> {
311 self.pretty_path_append_impl(
312 |mut cx| {
313 cx = print_prefix(cx)?;
314
315 if cx.keep_within_component {
316 // HACK(eddyb) print the path similarly to how `FmtPrinter` prints it.
317 cx.write_str("::")?;
318 } else {
319 cx.path.finalize_pending_component();
320 }
321
322 Ok(cx)
323 },
324 self_ty,
325 trait_ref,
326 )
327 }
328 fn path_append(
329 mut self,
330 print_prefix: impl FnOnce(Self) -> Result<Self::Path, Self::Error>,
331 disambiguated_data: &DisambiguatedDefPathData,
332 ) -> Result<Self::Path, Self::Error> {
333 self = print_prefix(self)?;
334
335 // Skip `::{{extern}}` blocks and `::{{constructor}}` on tuple/unit structs.
336 if let DefPathData::ForeignMod | DefPathData::Ctor = disambiguated_data.data {
337 return Ok(self);
338 }
339
340 if self.keep_within_component {
341 // HACK(eddyb) print the path similarly to how `FmtPrinter` prints it.
342 self.write_str("::")?;
343 } else {
344 self.path.finalize_pending_component();
345 }
346
347 write!(self, "{}", disambiguated_data.data)?;
348
349 Ok(self)
350 }
351 fn path_generic_args(
352 mut self,
353 print_prefix: impl FnOnce(Self) -> Result<Self::Path, Self::Error>,
354 args: &[GenericArg<'tcx>],
355 ) -> Result<Self::Path, Self::Error> {
356 self = print_prefix(self)?;
357
358 let args =
359 args.iter().cloned().filter(|arg| !matches!(arg.unpack(), GenericArgKind::Lifetime(_)));
360
361 if args.clone().next().is_some() {
362 self.generic_delimiters(|cx| cx.comma_sep(args))
363 } else {
364 Ok(self)
365 }
366 }
367 }
368
369 impl<'tcx> PrettyPrinter<'tcx> for &mut SymbolPrinter<'tcx> {
370 fn should_print_region(&self, _region: ty::Region<'_>) -> bool {
371 false
372 }
373 fn comma_sep<T>(mut self, mut elems: impl Iterator<Item = T>) -> Result<Self, Self::Error>
374 where
375 T: Print<'tcx, Self, Output = Self, Error = Self::Error>,
376 {
377 if let Some(first) = elems.next() {
378 self = first.print(self)?;
379 for elem in elems {
380 self.write_str(",")?;
381 self = elem.print(self)?;
382 }
383 }
384 Ok(self)
385 }
386
387 fn generic_delimiters(
388 mut self,
389 f: impl FnOnce(Self) -> Result<Self, Self::Error>,
390 ) -> Result<Self, Self::Error> {
391 write!(self, "<")?;
392
393 let kept_within_component = mem::replace(&mut self.keep_within_component, true);
394 self = f(self)?;
395 self.keep_within_component = kept_within_component;
396
397 write!(self, ">")?;
398
399 Ok(self)
400 }
401 }
402
403 impl fmt::Write for SymbolPrinter<'_> {
404 fn write_str(&mut self, s: &str) -> fmt::Result {
405 // Name sanitation. LLVM will happily accept identifiers with weird names, but
406 // gas doesn't!
407 // gas accepts the following characters in symbols: a-z, A-Z, 0-9, ., _, $
408 // NVPTX assembly has more strict naming rules than gas, so additionally, dots
409 // are replaced with '$' there.
410
411 for c in s.chars() {
412 if self.path.temp_buf.is_empty() {
413 match c {
414 'a'..='z' | 'A'..='Z' | '_' => {}
415 _ => {
416 // Underscore-qualify anything that didn't start as an ident.
417 self.path.temp_buf.push('_');
418 }
419 }
420 }
421 match c {
422 // Escape these with $ sequences
423 '@' => self.path.temp_buf.push_str("$SP$"),
424 '*' => self.path.temp_buf.push_str("$BP$"),
425 '&' => self.path.temp_buf.push_str("$RF$"),
426 '<' => self.path.temp_buf.push_str("$LT$"),
427 '>' => self.path.temp_buf.push_str("$GT$"),
428 '(' => self.path.temp_buf.push_str("$LP$"),
429 ')' => self.path.temp_buf.push_str("$RP$"),
430 ',' => self.path.temp_buf.push_str("$C$"),
431
432 '-' | ':' | '.' if self.tcx.has_strict_asm_symbol_naming() => {
433 // NVPTX doesn't support these characters in symbol names.
434 self.path.temp_buf.push('$')
435 }
436
437 // '.' doesn't occur in types and functions, so reuse it
438 // for ':' and '-'
439 '-' | ':' => self.path.temp_buf.push('.'),
440
441 // Avoid crashing LLVM in certain (LTO-related) situations, see #60925.
442 'm' if self.path.temp_buf.ends_with(".llv") => self.path.temp_buf.push_str("$u6d$"),
443
444 // These are legal symbols
445 'a'..='z' | 'A'..='Z' | '0'..='9' | '_' | '.' | '$' => self.path.temp_buf.push(c),
446
447 _ => {
448 self.path.temp_buf.push('$');
449 for c in c.escape_unicode().skip(1) {
450 match c {
451 '{' => {}
452 '}' => self.path.temp_buf.push('$'),
453 c => self.path.temp_buf.push(c),
454 }
455 }
456 }
457 }
458 }
459
460 Ok(())
461 }
462 }