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, tcx: TyCtxt<'tcx, '_>) -> ty::SymbolName {
43 ExportedSymbol::NonGeneric(def_id) => {
44 tcx.symbol_name(ty::Instance::mono(tcx, def_id))
46 ExportedSymbol::Generic(def_id, substs) => {
47 tcx.symbol_name(ty::Instance::new(def_id, substs))
49 ExportedSymbol::NoDefId(symbol_name) => {
55 pub fn compare_stable(
57 tcx: TyCtxt<'tcx, '_>,
58 other: &ExportedSymbol<'tcx>,
61 ExportedSymbol::NonGeneric(self_def_id) => match *other {
62 ExportedSymbol::NonGeneric(other_def_id) => {
63 tcx.def_path_hash(self_def_id).cmp(&tcx.def_path_hash(other_def_id))
65 ExportedSymbol::Generic(..) |
66 ExportedSymbol::NoDefId(_) => {
70 ExportedSymbol::Generic(..) => match *other {
71 ExportedSymbol::NonGeneric(_) => {
72 cmp::Ordering::Greater
74 ExportedSymbol::Generic(..) => {
75 self.symbol_name(tcx).cmp(&other.symbol_name(tcx))
77 ExportedSymbol::NoDefId(_) => {
81 ExportedSymbol::NoDefId(self_symbol_name) => match *other {
82 ExportedSymbol::NonGeneric(_) |
83 ExportedSymbol::Generic(..) => {
84 cmp::Ordering::Greater
86 ExportedSymbol::NoDefId(ref other_symbol_name) => {
87 self_symbol_name.cmp(other_symbol_name)
94 pub fn metadata_symbol_name(tcx: TyCtxt<'_, '_>) -> String {
95 format!("rust_metadata_{}_{}",
96 tcx.original_crate_name(LOCAL_CRATE),
97 tcx.crate_disambiguator(LOCAL_CRATE).to_fingerprint().to_hex())
100 impl<'a, 'gcx> HashStable<StableHashingContext<'a>> for ExportedSymbol<'gcx> {
101 fn hash_stable<W: StableHasherResult>(&self,
102 hcx: &mut StableHashingContext<'a>,
103 hasher: &mut StableHasher<W>) {
104 mem::discriminant(self).hash_stable(hcx, hasher);
106 ExportedSymbol::NonGeneric(def_id) => {
107 def_id.hash_stable(hcx, hasher);
109 ExportedSymbol::Generic(def_id, substs) => {
110 def_id.hash_stable(hcx, hasher);
111 substs.hash_stable(hcx, hasher);
113 ExportedSymbol::NoDefId(symbol_name) => {
114 symbol_name.hash_stable(hcx, hasher);