]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_symbol_mangling/src/typeid.rs
Rollup merge of #105239 - gh2o:no-heap-alloc-on-thread-start, r=cuviper
[rust.git] / compiler / rustc_symbol_mangling / src / typeid.rs
1 // For more information about type metadata and type metadata identifiers for cross-language LLVM
2 // CFI support, see Type metadata in the design document in the tracking issue #89653.
3
4 use rustc_middle::ty::{FnSig, Ty, TyCtxt};
5 use rustc_target::abi::call::FnAbi;
6 use std::hash::Hasher;
7 use twox_hash::XxHash64;
8
9 mod typeid_itanium_cxx_abi;
10 use typeid_itanium_cxx_abi::TypeIdOptions;
11
12 /// Returns a type metadata identifier for the specified FnAbi.
13 pub fn typeid_for_fnabi<'tcx>(tcx: TyCtxt<'tcx>, fn_abi: &FnAbi<'tcx, Ty<'tcx>>) -> String {
14     typeid_itanium_cxx_abi::typeid_for_fnabi(tcx, fn_abi, TypeIdOptions::NO_OPTIONS)
15 }
16
17 /// Returns a type metadata identifier for the specified FnSig.
18 pub fn typeid_for_fnsig<'tcx>(tcx: TyCtxt<'tcx>, fn_sig: &FnSig<'tcx>) -> String {
19     typeid_itanium_cxx_abi::typeid_for_fnsig(tcx, fn_sig, TypeIdOptions::NO_OPTIONS)
20 }
21
22 /// Returns an LLVM KCFI type metadata identifier for the specified FnAbi.
23 pub fn kcfi_typeid_for_fnabi<'tcx>(tcx: TyCtxt<'tcx>, fn_abi: &FnAbi<'tcx, Ty<'tcx>>) -> u32 {
24     // An LLVM KCFI type metadata identifier is a 32-bit constant produced by taking the lower half
25     // of the xxHash64 of the type metadata identifier. (See llvm/llvm-project@cff5bef.)
26     let mut hash: XxHash64 = Default::default();
27     hash.write(
28         typeid_itanium_cxx_abi::typeid_for_fnabi(tcx, fn_abi, TypeIdOptions::NO_OPTIONS).as_bytes(),
29     );
30     hash.finish() as u32
31 }
32
33 /// Returns an LLVM KCFI type metadata identifier for the specified FnSig.
34 pub fn kcfi_typeid_for_fnsig<'tcx>(tcx: TyCtxt<'tcx>, fn_sig: &FnSig<'tcx>) -> u32 {
35     // An LLVM KCFI type metadata identifier is a 32-bit constant produced by taking the lower half
36     // of the xxHash64 of the type metadata identifier. (See llvm/llvm-project@cff5bef.)
37     let mut hash: XxHash64 = Default::default();
38     hash.write(
39         typeid_itanium_cxx_abi::typeid_for_fnsig(tcx, fn_sig, TypeIdOptions::NO_OPTIONS).as_bytes(),
40     );
41     hash.finish() as u32
42 }