]> git.lizzy.rs Git - rust.git/blob - src/librustc/middle/exported_symbols.rs
Run `rustfmt --file-lines ...` for changes from previous commits.
[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::{self, TyCtxt};
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, tcx: TyCtxt<'tcx, '_>) -> ty::SymbolName {
42         match *self {
43             ExportedSymbol::NonGeneric(def_id) => {
44                 tcx.symbol_name(ty::Instance::mono(tcx, def_id))
45             }
46             ExportedSymbol::Generic(def_id, substs) => {
47                 tcx.symbol_name(ty::Instance::new(def_id, substs))
48             }
49             ExportedSymbol::NoDefId(symbol_name) => {
50                 symbol_name
51             }
52         }
53     }
54
55     pub fn compare_stable(
56         &self,
57         tcx: TyCtxt<'tcx, '_>,
58         other: &ExportedSymbol<'tcx>,
59     ) -> cmp::Ordering {
60         match *self {
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))
64                 }
65                 ExportedSymbol::Generic(..) |
66                 ExportedSymbol::NoDefId(_) => {
67                     cmp::Ordering::Less
68                 }
69             }
70             ExportedSymbol::Generic(..) => match *other {
71                 ExportedSymbol::NonGeneric(_) => {
72                     cmp::Ordering::Greater
73                 }
74                 ExportedSymbol::Generic(..) => {
75                     self.symbol_name(tcx).cmp(&other.symbol_name(tcx))
76                 }
77                 ExportedSymbol::NoDefId(_) => {
78                     cmp::Ordering::Less
79                 }
80             }
81             ExportedSymbol::NoDefId(self_symbol_name) => match *other {
82                 ExportedSymbol::NonGeneric(_) |
83                 ExportedSymbol::Generic(..) => {
84                     cmp::Ordering::Greater
85                 }
86                 ExportedSymbol::NoDefId(ref other_symbol_name) => {
87                     self_symbol_name.cmp(other_symbol_name)
88                 }
89             }
90         }
91     }
92 }
93
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())
98 }
99
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);
105         match *self {
106             ExportedSymbol::NonGeneric(def_id) => {
107                 def_id.hash_stable(hcx, hasher);
108             }
109             ExportedSymbol::Generic(def_id, substs) => {
110                 def_id.hash_stable(hcx, hasher);
111                 substs.hash_stable(hcx, hasher);
112             }
113             ExportedSymbol::NoDefId(symbol_name) => {
114                 symbol_name.hash_stable(hcx, hasher);
115             }
116         }
117     }
118 }