1 use crate::hir::def_id::{DefId, LOCAL_CRATE};
2 use crate::ich::StableHashingContext;
3 use rustc_data_structures::stable_hasher::{StableHasher, HashStable,
7 use crate::ty::{self, TyCtxt};
8 use crate::ty::subst::SubstsRef;
10 /// The SymbolExportLevel of a symbols specifies from which kinds of crates
11 /// the symbol will be exported. `C` symbols will be exported from any
12 /// kind of crate, including cdylibs which export very few things.
13 /// `Rust` will only be exported if the crate produced is a Rust
15 #[derive(Eq, PartialEq, Debug, Copy, Clone, RustcEncodable, RustcDecodable)]
16 pub enum SymbolExportLevel {
21 impl_stable_hash_for!(enum self::SymbolExportLevel {
26 impl SymbolExportLevel {
27 pub fn is_below_threshold(self, threshold: SymbolExportLevel) -> bool {
28 threshold == SymbolExportLevel::Rust // export everything from Rust dylibs
29 || self == SymbolExportLevel::C
33 #[derive(Eq, PartialEq, Debug, Copy, Clone, RustcEncodable, RustcDecodable)]
34 pub enum ExportedSymbol<'tcx> {
36 Generic(DefId, SubstsRef<'tcx>),
37 NoDefId(ty::SymbolName),
40 impl<'tcx> ExportedSymbol<'tcx> {
41 pub fn symbol_name(&self,
42 tcx: TyCtxt<'_, 'tcx, '_>)
45 ExportedSymbol::NonGeneric(def_id) => {
46 tcx.symbol_name(ty::Instance::mono(tcx, def_id))
48 ExportedSymbol::Generic(def_id, substs) => {
49 tcx.symbol_name(ty::Instance::new(def_id, substs))
51 ExportedSymbol::NoDefId(symbol_name) => {
57 pub fn compare_stable(&self,
58 tcx: TyCtxt<'_, 'tcx, '_>,
59 other: &ExportedSymbol<'tcx>)
62 ExportedSymbol::NonGeneric(self_def_id) => match *other {
63 ExportedSymbol::NonGeneric(other_def_id) => {
64 tcx.def_path_hash(self_def_id).cmp(&tcx.def_path_hash(other_def_id))
66 ExportedSymbol::Generic(..) |
67 ExportedSymbol::NoDefId(_) => {
71 ExportedSymbol::Generic(..) => match *other {
72 ExportedSymbol::NonGeneric(_) => {
73 cmp::Ordering::Greater
75 ExportedSymbol::Generic(..) => {
76 self.symbol_name(tcx).cmp(&other.symbol_name(tcx))
78 ExportedSymbol::NoDefId(_) => {
82 ExportedSymbol::NoDefId(self_symbol_name) => match *other {
83 ExportedSymbol::NonGeneric(_) |
84 ExportedSymbol::Generic(..) => {
85 cmp::Ordering::Greater
87 ExportedSymbol::NoDefId(ref other_symbol_name) => {
88 self_symbol_name.cmp(other_symbol_name)
95 pub fn metadata_symbol_name(tcx: TyCtxt<'_, '_, '_>) -> String {
96 format!("rust_metadata_{}_{}",
97 tcx.original_crate_name(LOCAL_CRATE),
98 tcx.crate_disambiguator(LOCAL_CRATE).to_fingerprint().to_hex())
101 impl<'a, 'gcx> HashStable<StableHashingContext<'a>> for ExportedSymbol<'gcx> {
102 fn hash_stable<W: StableHasherResult>(&self,
103 hcx: &mut StableHashingContext<'a>,
104 hasher: &mut StableHasher<W>) {
105 mem::discriminant(self).hash_stable(hcx, hasher);
107 ExportedSymbol::NonGeneric(def_id) => {
108 def_id.hash_stable(hcx, hasher);
110 ExportedSymbol::Generic(def_id, substs) => {
111 def_id.hash_stable(hcx, hasher);
112 substs.hash_stable(hcx, hasher);
114 ExportedSymbol::NoDefId(symbol_name) => {
115 symbol_name.hash_stable(hcx, hasher);