rustc_sanitizers/cfi/typeid/itanium_cxx_abi/
encode.rs

1//! Encodes type metadata identifiers for LLVM CFI and cross-language LLVM CFI support using Itanium
2//! C++ ABI mangling for encoding with vendor extended type qualifiers and types for Rust types that
3//! are not used across the FFI boundary.
4//!
5//! For more information about LLVM CFI and cross-language LLVM CFI support for the Rust compiler,
6//! see design document in the tracking issue #89653.
7
8use std::fmt::Write as _;
9
10use rustc_abi::{ExternAbi, Integer};
11use rustc_data_structures::base_n::{ALPHANUMERIC_ONLY, CASE_INSENSITIVE, ToBaseN};
12use rustc_data_structures::fx::FxHashMap;
13use rustc_hir as hir;
14use rustc_middle::bug;
15use rustc_middle::ty::layout::IntegerExt;
16use rustc_middle::ty::{
17    self, Const, ExistentialPredicate, FloatTy, FnSig, GenericArg, GenericArgKind, GenericArgsRef,
18    IntTy, List, Region, RegionKind, TermKind, Ty, TyCtxt, TypeFoldable, UintTy,
19};
20use rustc_span::def_id::DefId;
21use rustc_span::sym;
22use tracing::instrument;
23
24use crate::cfi::typeid::TypeIdOptions;
25use crate::cfi::typeid::itanium_cxx_abi::transform::{TransformTy, TransformTyOptions};
26
27/// Options for encode_ty.
28pub(crate) type EncodeTyOptions = TypeIdOptions;
29
30/// Substitution dictionary key.
31#[derive(Eq, Hash, PartialEq)]
32pub(crate) enum DictKey<'tcx> {
33    Ty(Ty<'tcx>, TyQ),
34    Region(Region<'tcx>),
35    Const(Const<'tcx>),
36    Predicate(ExistentialPredicate<'tcx>),
37}
38
39/// Type and extended type qualifiers.
40#[derive(Eq, Hash, PartialEq)]
41pub(crate) enum TyQ {
42    None,
43    Const,
44    Mut,
45}
46
47/// Substitutes a component if found in the substitution dictionary (see
48/// <https://itanium-cxx-abi.github.io/cxx-abi/abi.html#mangling-compression>).
49fn compress<'tcx>(
50    dict: &mut FxHashMap<DictKey<'tcx>, usize>,
51    key: DictKey<'tcx>,
52    comp: &mut String,
53) {
54    match dict.get(&key) {
55        Some(num) => {
56            comp.clear();
57            let _ = write!(comp, "S{}_", to_seq_id(*num));
58        }
59        None => {
60            dict.insert(key, dict.len());
61        }
62    }
63}
64
65/// Encodes args using the Itanium C++ ABI with vendor extended type qualifiers and types for Rust
66/// types that are not used at the FFI boundary.
67fn encode_args<'tcx>(
68    tcx: TyCtxt<'tcx>,
69    args: GenericArgsRef<'tcx>,
70    for_def: DefId,
71    has_erased_self: bool,
72    dict: &mut FxHashMap<DictKey<'tcx>, usize>,
73    options: EncodeTyOptions,
74) -> String {
75    // [I<subst1..substN>E] as part of vendor extended type
76    let mut s = String::new();
77    let args: Vec<GenericArg<'_>> = args.iter().collect();
78    if !args.is_empty() {
79        s.push('I');
80        let def_generics = tcx.generics_of(for_def);
81        for (n, arg) in args.iter().enumerate() {
82            match arg.unpack() {
83                GenericArgKind::Lifetime(region) => {
84                    s.push_str(&encode_region(region, dict));
85                }
86                GenericArgKind::Type(ty) => {
87                    s.push_str(&encode_ty(tcx, ty, dict, options));
88                }
89                GenericArgKind::Const(c) => {
90                    let n = n + (has_erased_self as usize);
91                    let ct_ty =
92                        tcx.type_of(def_generics.param_at(n, tcx).def_id).instantiate_identity();
93                    s.push_str(&encode_const(tcx, c, ct_ty, dict, options));
94                }
95            }
96        }
97        s.push('E');
98    }
99    s
100}
101
102/// Encodes a const using the Itanium C++ ABI as a literal argument (see
103/// <https://itanium-cxx-abi.github.io/cxx-abi/abi.html#mangling.literal>).
104fn encode_const<'tcx>(
105    tcx: TyCtxt<'tcx>,
106    ct: Const<'tcx>,
107    ct_ty: Ty<'tcx>,
108    dict: &mut FxHashMap<DictKey<'tcx>, usize>,
109    options: EncodeTyOptions,
110) -> String {
111    // L<element-type>[n][<element-value>]E as literal argument
112    let mut s = String::from('L');
113
114    match ct.kind() {
115        // Const parameters
116        ty::ConstKind::Param(..) => {
117            // L<element-type>E as literal argument
118
119            // Element type
120            s.push_str(&encode_ty(tcx, ct_ty, dict, options));
121        }
122
123        // Literal arguments
124        ty::ConstKind::Value(cv) => {
125            // L<element-type>[n]<element-value>E as literal argument
126
127            // Element type
128            s.push_str(&encode_ty(tcx, cv.ty, dict, options));
129
130            // The only allowed types of const values are bool, u8, u16, u32,
131            // u64, u128, usize i8, i16, i32, i64, i128, isize, and char. The
132            // bool value false is encoded as 0 and true as 1.
133            match cv.ty.kind() {
134                ty::Int(ity) => {
135                    let bits = cv
136                        .try_to_bits(tcx, ty::TypingEnv::fully_monomorphized())
137                        .expect("expected monomorphic const in cfi");
138                    let val = Integer::from_int_ty(&tcx, *ity).size().sign_extend(bits) as i128;
139                    if val < 0 {
140                        s.push('n');
141                    }
142                    let _ = write!(s, "{val}");
143                }
144                ty::Uint(_) => {
145                    let val = cv
146                        .try_to_bits(tcx, ty::TypingEnv::fully_monomorphized())
147                        .expect("expected monomorphic const in cfi");
148                    let _ = write!(s, "{val}");
149                }
150                ty::Bool => {
151                    let val = cv.try_to_bool().expect("expected monomorphic const in cfi");
152                    let _ = write!(s, "{val}");
153                }
154                _ => {
155                    bug!("encode_const: unexpected type `{:?}`", cv.ty);
156                }
157            }
158        }
159
160        _ => {
161            bug!("encode_const: unexpected kind `{:?}`", ct.kind());
162        }
163    }
164
165    // Close the "L..E" pair
166    s.push('E');
167
168    compress(dict, DictKey::Const(ct), &mut s);
169
170    s
171}
172
173/// Encodes a FnSig using the Itanium C++ ABI with vendor extended type qualifiers and types for
174/// Rust types that are not used at the FFI boundary.
175fn encode_fnsig<'tcx>(
176    tcx: TyCtxt<'tcx>,
177    fn_sig: &FnSig<'tcx>,
178    dict: &mut FxHashMap<DictKey<'tcx>, usize>,
179    options: TypeIdOptions,
180) -> String {
181    // Function types are delimited by an "F..E" pair
182    let mut s = String::from("F");
183
184    let mut encode_ty_options = EncodeTyOptions::from_bits(options.bits())
185        .unwrap_or_else(|| bug!("encode_fnsig: invalid option(s) `{:?}`", options.bits()));
186    match fn_sig.abi {
187        ExternAbi::C { .. } => {
188            encode_ty_options.insert(EncodeTyOptions::GENERALIZE_REPR_C);
189        }
190        _ => {
191            encode_ty_options.remove(EncodeTyOptions::GENERALIZE_REPR_C);
192        }
193    }
194
195    // Encode the return type
196    let transform_ty_options = TransformTyOptions::from_bits(options.bits())
197        .unwrap_or_else(|| bug!("encode_fnsig: invalid option(s) `{:?}`", options.bits()));
198    let mut type_folder = TransformTy::new(tcx, transform_ty_options);
199    let ty = fn_sig.output().fold_with(&mut type_folder);
200    s.push_str(&encode_ty(tcx, ty, dict, encode_ty_options));
201
202    // Encode the parameter types
203    let tys = fn_sig.inputs();
204    if !tys.is_empty() {
205        for ty in tys {
206            let ty = ty.fold_with(&mut type_folder);
207            s.push_str(&encode_ty(tcx, ty, dict, encode_ty_options));
208        }
209
210        if fn_sig.c_variadic {
211            s.push('z');
212        }
213    } else if fn_sig.c_variadic {
214        s.push('z');
215    } else {
216        // Empty parameter lists, whether declared as () or conventionally as (void), are
217        // encoded with a void parameter specifier "v".
218        s.push('v')
219    }
220
221    // Close the "F..E" pair
222    s.push('E');
223
224    s
225}
226
227/// Encodes a predicate using the Itanium C++ ABI with vendor extended type qualifiers and types for
228/// Rust types that are not used at the FFI boundary.
229fn encode_predicate<'tcx>(
230    tcx: TyCtxt<'tcx>,
231    predicate: ty::PolyExistentialPredicate<'tcx>,
232    dict: &mut FxHashMap<DictKey<'tcx>, usize>,
233    options: EncodeTyOptions,
234) -> String {
235    // u<length><name>[I<element-type1..element-typeN>E], where <element-type> is <subst>, as vendor
236    // extended type.
237    let mut s = String::new();
238    match predicate.as_ref().skip_binder() {
239        ty::ExistentialPredicate::Trait(trait_ref) => {
240            let name = encode_ty_name(tcx, trait_ref.def_id);
241            let _ = write!(s, "u{}{}", name.len(), name);
242            s.push_str(&encode_args(tcx, trait_ref.args, trait_ref.def_id, true, dict, options));
243        }
244        ty::ExistentialPredicate::Projection(projection) => {
245            let name = encode_ty_name(tcx, projection.def_id);
246            let _ = write!(s, "u{}{}", name.len(), name);
247            s.push_str(&encode_args(tcx, projection.args, projection.def_id, true, dict, options));
248            match projection.term.unpack() {
249                TermKind::Ty(ty) => s.push_str(&encode_ty(tcx, ty, dict, options)),
250                TermKind::Const(c) => s.push_str(&encode_const(
251                    tcx,
252                    c,
253                    tcx.type_of(projection.def_id).instantiate(tcx, projection.args),
254                    dict,
255                    options,
256                )),
257            }
258        }
259        ty::ExistentialPredicate::AutoTrait(def_id) => {
260            let name = encode_ty_name(tcx, *def_id);
261            let _ = write!(s, "u{}{}", name.len(), name);
262        }
263    };
264    compress(dict, DictKey::Predicate(*predicate.as_ref().skip_binder()), &mut s);
265    s
266}
267
268/// Encodes predicates using the Itanium C++ ABI with vendor extended type qualifiers and types for
269/// Rust types that are not used at the FFI boundary.
270fn encode_predicates<'tcx>(
271    tcx: TyCtxt<'tcx>,
272    predicates: &List<ty::PolyExistentialPredicate<'tcx>>,
273    dict: &mut FxHashMap<DictKey<'tcx>, usize>,
274    options: EncodeTyOptions,
275) -> String {
276    // <predicate1[..predicateN]>E as part of vendor extended type
277    let mut s = String::new();
278    let predicates: Vec<ty::PolyExistentialPredicate<'tcx>> = predicates.iter().collect();
279    for predicate in predicates {
280        s.push_str(&encode_predicate(tcx, predicate, dict, options));
281    }
282    s
283}
284
285/// Encodes a region using the Itanium C++ ABI as a vendor extended type.
286fn encode_region<'tcx>(region: Region<'tcx>, dict: &mut FxHashMap<DictKey<'tcx>, usize>) -> String {
287    // u6region[I[<region-disambiguator>][<region-index>]E] as vendor extended type
288    let mut s = String::new();
289    match region.kind() {
290        RegionKind::ReBound(debruijn, r) => {
291            s.push_str("u6regionI");
292            // Debruijn index, which identifies the binder, as region disambiguator
293            let num = debruijn.index() as u64;
294            if num > 0 {
295                s.push_str(&to_disambiguator(num));
296            }
297            // Index within the binder
298            let _ = write!(s, "{}", r.var.index() as u64);
299            s.push('E');
300            compress(dict, DictKey::Region(region), &mut s);
301        }
302        RegionKind::ReErased => {
303            s.push_str("u6region");
304            compress(dict, DictKey::Region(region), &mut s);
305        }
306        RegionKind::ReEarlyParam(..)
307        | RegionKind::ReLateParam(..)
308        | RegionKind::ReStatic
309        | RegionKind::ReError(_)
310        | RegionKind::ReVar(..)
311        | RegionKind::RePlaceholder(..) => {
312            bug!("encode_region: unexpected `{:?}`", region.kind());
313        }
314    }
315    s
316}
317
318/// Encodes a ty:Ty using the Itanium C++ ABI with vendor extended type qualifiers and types for
319/// Rust types that are not used at the FFI boundary.
320#[instrument(level = "trace", skip(tcx, dict))]
321pub(crate) fn encode_ty<'tcx>(
322    tcx: TyCtxt<'tcx>,
323    ty: Ty<'tcx>,
324    dict: &mut FxHashMap<DictKey<'tcx>, usize>,
325    options: EncodeTyOptions,
326) -> String {
327    let mut typeid = String::new();
328
329    match ty.kind() {
330        // Primitive types
331
332        // Rust's bool has the same layout as C17's _Bool, that is, its size and alignment are
333        // implementation-defined. Any bool can be cast into an integer, taking on the values 1
334        // (true) or 0 (false).
335        //
336        // (See https://rust-lang.github.io/unsafe-code-guidelines/layout/scalars.html#bool.)
337        ty::Bool => {
338            typeid.push('b');
339        }
340
341        ty::Int(..) | ty::Uint(..) => {
342            // u<length><type-name> as vendor extended type
343            let mut s = String::from(match ty.kind() {
344                ty::Int(IntTy::I8) => "u2i8",
345                ty::Int(IntTy::I16) => "u3i16",
346                ty::Int(IntTy::I32) => "u3i32",
347                ty::Int(IntTy::I64) => "u3i64",
348                ty::Int(IntTy::I128) => "u4i128",
349                ty::Int(IntTy::Isize) => "u5isize",
350                ty::Uint(UintTy::U8) => "u2u8",
351                ty::Uint(UintTy::U16) => "u3u16",
352                ty::Uint(UintTy::U32) => "u3u32",
353                ty::Uint(UintTy::U64) => "u3u64",
354                ty::Uint(UintTy::U128) => "u4u128",
355                ty::Uint(UintTy::Usize) => "u5usize",
356                _ => bug!("encode_ty: unexpected `{:?}`", ty.kind()),
357            });
358            compress(dict, DictKey::Ty(ty, TyQ::None), &mut s);
359            typeid.push_str(&s);
360        }
361
362        // Rust's f16, f32, f64, and f126 half (16-bit), single (32-bit), double (64-bit), and
363        // quad (128-bit)  precision floating-point types have IEEE-754 binary16, binary32,
364        // binary64, and binary128 floating-point layouts, respectively.
365        //
366        // (See https://rust-lang.github.io/unsafe-code-guidelines/layout/scalars.html#fixed-width-floating-point-types.)
367        ty::Float(float_ty) => {
368            typeid.push_str(match float_ty {
369                FloatTy::F16 => "Dh",
370                FloatTy::F32 => "f",
371                FloatTy::F64 => "d",
372                FloatTy::F128 => "g",
373            });
374        }
375
376        ty::Char => {
377            // u4char as vendor extended type
378            let mut s = String::from("u4char");
379            compress(dict, DictKey::Ty(ty, TyQ::None), &mut s);
380            typeid.push_str(&s);
381        }
382
383        ty::Str => {
384            // u3str as vendor extended type
385            let mut s = String::from("u3str");
386            compress(dict, DictKey::Ty(ty, TyQ::None), &mut s);
387            typeid.push_str(&s);
388        }
389
390        ty::Never => {
391            // u5never as vendor extended type
392            let mut s = String::from("u5never");
393            compress(dict, DictKey::Ty(ty, TyQ::None), &mut s);
394            typeid.push_str(&s);
395        }
396
397        // Compound types
398        // () in Rust is equivalent to void return type in C
399        _ if ty.is_unit() => {
400            typeid.push('v');
401        }
402
403        // Sequence types
404        ty::Tuple(tys) => {
405            // u5tupleI<element-type1..element-typeN>E as vendor extended type
406            let mut s = String::from("u5tupleI");
407            for ty in tys.iter() {
408                s.push_str(&encode_ty(tcx, ty, dict, options));
409            }
410            s.push('E');
411            compress(dict, DictKey::Ty(ty, TyQ::None), &mut s);
412            typeid.push_str(&s);
413        }
414
415        ty::Array(ty0, len) => {
416            // A<array-length><element-type>
417            let len = len.try_to_target_usize(tcx).expect("expected monomorphic const in cfi");
418            let mut s = String::from("A");
419            let _ = write!(s, "{len}");
420            s.push_str(&encode_ty(tcx, *ty0, dict, options));
421            compress(dict, DictKey::Ty(ty, TyQ::None), &mut s);
422            typeid.push_str(&s);
423        }
424
425        ty::Pat(ty0, pat) => {
426            // u3patI<element-type><pattern>E as vendor extended type
427            let mut s = String::from("u3patI");
428            s.push_str(&encode_ty(tcx, *ty0, dict, options));
429            write!(s, "{:?}", **pat).unwrap();
430            s.push('E');
431            compress(dict, DictKey::Ty(ty, TyQ::None), &mut s);
432            typeid.push_str(&s);
433        }
434
435        ty::Slice(ty0) => {
436            // u5sliceI<element-type>E as vendor extended type
437            let mut s = String::from("u5sliceI");
438            s.push_str(&encode_ty(tcx, *ty0, dict, options));
439            s.push('E');
440            compress(dict, DictKey::Ty(ty, TyQ::None), &mut s);
441            typeid.push_str(&s);
442        }
443
444        // User-defined types
445        ty::Adt(adt_def, args) => {
446            let mut s = String::new();
447            let def_id = adt_def.did();
448            if let Some(cfi_encoding) = tcx.get_attr(def_id, sym::cfi_encoding) {
449                // Use user-defined CFI encoding for type
450                if let Some(value_str) = cfi_encoding.value_str() {
451                    let value_str = value_str.as_str().trim();
452                    if !value_str.is_empty() {
453                        s.push_str(value_str);
454                        // Don't compress user-defined builtin types (see
455                        // https://itanium-cxx-abi.github.io/cxx-abi/abi.html#mangling-builtin and
456                        // https://itanium-cxx-abi.github.io/cxx-abi/abi.html#mangling-compression).
457                        let builtin_types = [
458                            "v", "w", "b", "c", "a", "h", "s", "t", "i", "j", "l", "m", "x", "y",
459                            "n", "o", "f", "d", "e", "g", "z", "Dh",
460                        ];
461                        if !builtin_types.contains(&value_str) {
462                            compress(dict, DictKey::Ty(ty, TyQ::None), &mut s);
463                        }
464                    } else {
465                        #[allow(
466                            rustc::diagnostic_outside_of_impl,
467                            rustc::untranslatable_diagnostic
468                        )]
469                        tcx.dcx()
470                            .struct_span_err(
471                                cfi_encoding.span(),
472                                format!("invalid `cfi_encoding` for `{:?}`", ty.kind()),
473                            )
474                            .emit();
475                    }
476                } else {
477                    bug!("encode_ty: invalid `cfi_encoding` for `{:?}`", ty.kind());
478                }
479            } else if options.contains(EncodeTyOptions::GENERALIZE_REPR_C) && adt_def.repr().c() {
480                // For cross-language LLVM CFI support, the encoding must be compatible at the FFI
481                // boundary. For instance:
482                //
483                //     struct type1 {};
484                //     void foo(struct type1* bar) {}
485                //
486                // Is encoded as:
487                //
488                //     _ZTSFvP5type1E
489                //
490                // So, encode any repr(C) user-defined type for extern function types with the "C"
491                // calling convention (or extern types [i.e., ty::Foreign]) as <length><name>, where
492                // <name> is <unscoped-name>.
493                let name = tcx.item_name(def_id).to_string();
494                let _ = write!(s, "{}{}", name.len(), name);
495                compress(dict, DictKey::Ty(ty, TyQ::None), &mut s);
496            } else {
497                // u<length><name>[I<element-type1..element-typeN>E], where <element-type> is
498                // <subst>, as vendor extended type.
499                let name = encode_ty_name(tcx, def_id);
500                let _ = write!(s, "u{}{}", name.len(), name);
501                s.push_str(&encode_args(tcx, args, def_id, false, dict, options));
502                compress(dict, DictKey::Ty(ty, TyQ::None), &mut s);
503            }
504            typeid.push_str(&s);
505        }
506
507        ty::Foreign(def_id) => {
508            // <length><name>, where <name> is <unscoped-name>
509            let mut s = String::new();
510            if let Some(cfi_encoding) = tcx.get_attr(*def_id, sym::cfi_encoding) {
511                // Use user-defined CFI encoding for type
512                if let Some(value_str) = cfi_encoding.value_str() {
513                    if !value_str.to_string().trim().is_empty() {
514                        s.push_str(value_str.to_string().trim());
515                    } else {
516                        #[allow(
517                            rustc::diagnostic_outside_of_impl,
518                            rustc::untranslatable_diagnostic
519                        )]
520                        tcx.dcx()
521                            .struct_span_err(
522                                cfi_encoding.span(),
523                                format!("invalid `cfi_encoding` for `{:?}`", ty.kind()),
524                            )
525                            .emit();
526                    }
527                } else {
528                    bug!("encode_ty: invalid `cfi_encoding` for `{:?}`", ty.kind());
529                }
530            } else {
531                let name = tcx.item_name(*def_id).to_string();
532                let _ = write!(s, "{}{}", name.len(), name);
533            }
534            compress(dict, DictKey::Ty(ty, TyQ::None), &mut s);
535            typeid.push_str(&s);
536        }
537
538        // Function types
539        ty::FnDef(def_id, args) | ty::Closure(def_id, args) => {
540            // u<length><name>[I<element-type1..element-typeN>E], where <element-type> is <subst>,
541            // as vendor extended type.
542            let mut s = String::new();
543            let name = encode_ty_name(tcx, *def_id);
544            let _ = write!(s, "u{}{}", name.len(), name);
545            s.push_str(&encode_args(tcx, args, *def_id, false, dict, options));
546            compress(dict, DictKey::Ty(ty, TyQ::None), &mut s);
547            typeid.push_str(&s);
548        }
549
550        ty::CoroutineClosure(def_id, args) => {
551            // u<length><name>[I<element-type1..element-typeN>E], where <element-type> is <subst>,
552            // as vendor extended type.
553            let mut s = String::new();
554            let name = encode_ty_name(tcx, *def_id);
555            let _ = write!(s, "u{}{}", name.len(), name);
556            let parent_args = tcx.mk_args(args.as_coroutine_closure().parent_args());
557            s.push_str(&encode_args(tcx, parent_args, *def_id, false, dict, options));
558            compress(dict, DictKey::Ty(ty, TyQ::None), &mut s);
559            typeid.push_str(&s);
560        }
561
562        ty::Coroutine(def_id, args, ..) => {
563            // u<length><name>[I<element-type1..element-typeN>E], where <element-type> is <subst>,
564            // as vendor extended type.
565            let mut s = String::new();
566            let name = encode_ty_name(tcx, *def_id);
567            let _ = write!(s, "u{}{}", name.len(), name);
568            // Encode parent args only
569            s.push_str(&encode_args(
570                tcx,
571                tcx.mk_args(args.as_coroutine().parent_args()),
572                *def_id,
573                false,
574                dict,
575                options,
576            ));
577            compress(dict, DictKey::Ty(ty, TyQ::None), &mut s);
578            typeid.push_str(&s);
579        }
580
581        // Pointer types
582        ty::Ref(region, ty0, ..) => {
583            // [U3mut]u3refI<element-type>E as vendor extended type qualifier and type
584            let mut s = String::new();
585            s.push_str("u3refI");
586            s.push_str(&encode_ty(tcx, *ty0, dict, options));
587            s.push('E');
588            compress(dict, DictKey::Ty(Ty::new_imm_ref(tcx, *region, *ty0), TyQ::None), &mut s);
589            if ty.is_mutable_ptr() {
590                s = format!("{}{}", "U3mut", s);
591                compress(dict, DictKey::Ty(ty, TyQ::Mut), &mut s);
592            }
593            typeid.push_str(&s);
594        }
595
596        ty::RawPtr(ptr_ty, _mutbl) => {
597            // FIXME: This can definitely not be so spaghettified.
598            // P[K]<element-type>
599            let mut s = String::new();
600            s.push_str(&encode_ty(tcx, *ptr_ty, dict, options));
601            if !ty.is_mutable_ptr() {
602                s = format!("{}{}", "K", s);
603                compress(dict, DictKey::Ty(*ptr_ty, TyQ::Const), &mut s);
604            };
605            s = format!("{}{}", "P", s);
606            compress(dict, DictKey::Ty(ty, TyQ::None), &mut s);
607            typeid.push_str(&s);
608        }
609
610        ty::FnPtr(sig_tys, hdr) => {
611            // PF<return-type><parameter-type1..parameter-typeN>E
612            let mut s = String::from("P");
613            s.push_str(&encode_fnsig(
614                tcx,
615                &sig_tys.with(*hdr).skip_binder(),
616                dict,
617                TypeIdOptions::empty(),
618            ));
619            compress(dict, DictKey::Ty(ty, TyQ::None), &mut s);
620            typeid.push_str(&s);
621        }
622
623        // FIXME(unsafe_binders): Implement this.
624        ty::UnsafeBinder(_) => {
625            todo!()
626        }
627
628        // Trait types
629        ty::Dynamic(predicates, region, kind) => {
630            // u3dynI<element-type1[..element-typeN]>E, where <element-type> is <predicate>, as
631            // vendor extended type.
632            let mut s = String::from(match kind {
633                ty::Dyn => "u3dynI",
634                ty::DynStar => "u7dynstarI",
635            });
636            s.push_str(&encode_predicates(tcx, predicates, dict, options));
637            s.push_str(&encode_region(*region, dict));
638            s.push('E');
639            compress(dict, DictKey::Ty(ty, TyQ::None), &mut s);
640            typeid.push_str(&s);
641        }
642
643        // Type parameters
644        ty::Param(..) => {
645            // u5param as vendor extended type
646            let mut s = String::from("u5param");
647            compress(dict, DictKey::Ty(ty, TyQ::None), &mut s);
648            typeid.push_str(&s);
649        }
650
651        // Unexpected types
652        ty::Alias(..)
653        | ty::Bound(..)
654        | ty::Error(..)
655        | ty::CoroutineWitness(..)
656        | ty::Infer(..)
657        | ty::Placeholder(..) => {
658            bug!("encode_ty: unexpected `{:?}`", ty.kind());
659        }
660    };
661
662    typeid
663}
664
665/// Encodes a ty:Ty name, including its crate and path disambiguators and names.
666fn encode_ty_name(tcx: TyCtxt<'_>, def_id: DefId) -> String {
667    // Encode <name> for use in u<length><name>[I<element-type1..element-typeN>E], where
668    // <element-type> is <subst>, using v0's <path> without v0's extended form of paths:
669    //
670    // N<namespace-tagN>..N<namespace-tag1>
671    // C<crate-disambiguator><crate-name>
672    // <path-disambiguator1><path-name1>..<path-disambiguatorN><path-nameN>
673    //
674    // With additional tags for DefPathData::Impl and DefPathData::ForeignMod. For instance:
675    //
676    //     pub type Type1 = impl Send;
677    //     let _: Type1 = <Struct1<i32>>::foo;
678    //     fn foo1(_: Type1) { }
679    //
680    //     pub type Type2 = impl Send;
681    //     let _: Type2 = <Trait1<i32>>::foo;
682    //     fn foo2(_: Type2) { }
683    //
684    //     pub type Type3 = impl Send;
685    //     let _: Type3 = <i32 as Trait1<i32>>::foo;
686    //     fn foo3(_: Type3) { }
687    //
688    //     pub type Type4 = impl Send;
689    //     let _: Type4 = <Struct1<i32> as Trait1<i32>>::foo;
690    //     fn foo3(_: Type4) { }
691    //
692    // Are encoded as:
693    //
694    //     _ZTSFvu29NvNIC1234_5crate8{{impl}}3fooIu3i32EE
695    //     _ZTSFvu27NvNtC1234_5crate6Trait13fooIu3dynIu21NtC1234_5crate6Trait1Iu3i32Eu6regionES_EE
696    //     _ZTSFvu27NvNtC1234_5crate6Trait13fooIu3i32S_EE
697    //     _ZTSFvu27NvNtC1234_5crate6Trait13fooIu22NtC1234_5crate7Struct1Iu3i32ES_EE
698    //
699    // The reason for not using v0's extended form of paths is to use a consistent and simpler
700    // encoding, as the reasoning for using it isn't relevant for type metadata identifiers (i.e.,
701    // keep symbol names close to how methods are represented in error messages). See
702    // https://rust-lang.github.io/rfcs/2603-rust-symbol-name-mangling-v0.html#methods.
703    let mut s = String::new();
704
705    // Start and namespace tags
706    let mut def_path = tcx.def_path(def_id);
707    def_path.data.reverse();
708    for disambiguated_data in &def_path.data {
709        s.push('N');
710        s.push_str(match disambiguated_data.data {
711            hir::definitions::DefPathData::Impl => "I", // Not specified in v0's <namespace>
712            hir::definitions::DefPathData::ForeignMod => "F", // Not specified in v0's <namespace>
713            hir::definitions::DefPathData::TypeNs(..) => "t",
714            hir::definitions::DefPathData::ValueNs(..) => "v",
715            hir::definitions::DefPathData::Closure => "C",
716            hir::definitions::DefPathData::Ctor => "c",
717            hir::definitions::DefPathData::AnonConst => "k",
718            hir::definitions::DefPathData::OpaqueTy => "i",
719            hir::definitions::DefPathData::CrateRoot
720            | hir::definitions::DefPathData::Use
721            | hir::definitions::DefPathData::GlobalAsm
722            | hir::definitions::DefPathData::MacroNs(..)
723            | hir::definitions::DefPathData::LifetimeNs(..) => {
724                bug!("encode_ty_name: unexpected `{:?}`", disambiguated_data.data);
725            }
726        });
727    }
728
729    // Crate disambiguator and name
730    s.push('C');
731    s.push_str(&to_disambiguator(tcx.stable_crate_id(def_path.krate).as_u64()));
732    let crate_name = tcx.crate_name(def_path.krate).to_string();
733    let _ = write!(s, "{}{}", crate_name.len(), crate_name);
734
735    // Disambiguators and names
736    def_path.data.reverse();
737    for disambiguated_data in &def_path.data {
738        let num = disambiguated_data.disambiguator as u64;
739        if num > 0 {
740            s.push_str(&to_disambiguator(num));
741        }
742
743        let name = disambiguated_data.data.to_string();
744        let _ = write!(s, "{}", name.len());
745
746        // Prepend a '_' if name starts with a digit or '_'
747        if let Some(first) = name.as_bytes().first() {
748            if first.is_ascii_digit() || *first == b'_' {
749                s.push('_');
750            }
751        } else {
752            bug!("encode_ty_name: invalid name `{:?}`", name);
753        }
754
755        s.push_str(&name);
756    }
757
758    s
759}
760
761/// Converts a number to a disambiguator (see
762/// <https://rust-lang.github.io/rfcs/2603-rust-symbol-name-mangling-v0.html>).
763fn to_disambiguator(num: u64) -> String {
764    if let Some(num) = num.checked_sub(1) {
765        format!("s{}_", num.to_base(ALPHANUMERIC_ONLY))
766    } else {
767        "s_".to_string()
768    }
769}
770
771/// Converts a number to a sequence number (see
772/// <https://itanium-cxx-abi.github.io/cxx-abi/abi.html#mangle.seq-id>).
773fn to_seq_id(num: usize) -> String {
774    if let Some(num) = num.checked_sub(1) {
775        (num as u64).to_base(CASE_INSENSITIVE).to_uppercase()
776    } else {
777        "".to_string()
778    }
779}