]> git.lizzy.rs Git - rust.git/blob - src/librustc/middle/exported_symbols.rs
Rollup merge of #59847 - Kampfkarren:try-block-catch, r=estebank
[rust.git] / src / librustc / middle / exported_symbols.rs
1 use crate::hir::def_id::{DefId, LOCAL_CRATE};
2 use crate::ich::StableHashingContext;
3 use rustc_data_structures::stable_hasher::{StableHasher, HashStable,
4                                            StableHasherResult};
5 use std::cmp;
6 use std::mem;
7 use crate::ty;
8 use crate::ty::subst::SubstsRef;
9
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
14 /// dylib.
15 #[derive(Eq, PartialEq, Debug, Copy, Clone, RustcEncodable, RustcDecodable)]
16 pub enum SymbolExportLevel {
17     C,
18     Rust,
19 }
20
21 impl_stable_hash_for!(enum self::SymbolExportLevel {
22     C,
23     Rust
24 });
25
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
30     }
31 }
32
33 #[derive(Eq, PartialEq, Debug, Copy, Clone, RustcEncodable, RustcDecodable)]
34 pub enum ExportedSymbol<'tcx> {
35     NonGeneric(DefId),
36     Generic(DefId, SubstsRef<'tcx>),
37     NoDefId(ty::SymbolName),
38 }
39
40 impl<'tcx> ExportedSymbol<'tcx> {
41     pub fn symbol_name(&self,
42                        tcx: ty::TyCtxt<'_, 'tcx, '_>)
43                        -> ty::SymbolName {
44         match *self {
45             ExportedSymbol::NonGeneric(def_id) => {
46                 tcx.symbol_name(ty::Instance::mono(tcx, def_id))
47             }
48             ExportedSymbol::Generic(def_id, substs) => {
49                 tcx.symbol_name(ty::Instance::new(def_id, substs))
50             }
51             ExportedSymbol::NoDefId(symbol_name) => {
52                 symbol_name
53             }
54         }
55     }
56
57     pub fn compare_stable(&self,
58                           tcx: ty::TyCtxt<'_, 'tcx, '_>,
59                           other: &ExportedSymbol<'tcx>)
60                           -> cmp::Ordering {
61         match *self {
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))
65                 }
66                 ExportedSymbol::Generic(..) |
67                 ExportedSymbol::NoDefId(_) => {
68                     cmp::Ordering::Less
69                 }
70             }
71             ExportedSymbol::Generic(..) => match *other {
72                 ExportedSymbol::NonGeneric(_) => {
73                     cmp::Ordering::Greater
74                 }
75                 ExportedSymbol::Generic(..) => {
76                     self.symbol_name(tcx).cmp(&other.symbol_name(tcx))
77                 }
78                 ExportedSymbol::NoDefId(_) => {
79                     cmp::Ordering::Less
80                 }
81             }
82             ExportedSymbol::NoDefId(self_symbol_name) => match *other {
83                 ExportedSymbol::NonGeneric(_) |
84                 ExportedSymbol::Generic(..) => {
85                     cmp::Ordering::Greater
86                 }
87                 ExportedSymbol::NoDefId(ref other_symbol_name) => {
88                     self_symbol_name.cmp(other_symbol_name)
89                 }
90             }
91         }
92     }
93 }
94
95 pub fn metadata_symbol_name(tcx: ty::TyCtxt<'_, '_, '_>) -> String {
96     format!("rust_metadata_{}_{}",
97             tcx.original_crate_name(LOCAL_CRATE),
98             tcx.crate_disambiguator(LOCAL_CRATE).to_fingerprint().to_hex())
99 }
100
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);
106         match *self {
107             ExportedSymbol::NonGeneric(def_id) => {
108                 def_id.hash_stable(hcx, hasher);
109             }
110             ExportedSymbol::Generic(def_id, substs) => {
111                 def_id.hash_stable(hcx, hasher);
112                 substs.hash_stable(hcx, hasher);
113             }
114             ExportedSymbol::NoDefId(symbol_name) => {
115                 symbol_name.hash_stable(hcx, hasher);
116             }
117         }
118     }
119 }