]> git.lizzy.rs Git - rust.git/blob - src/librustc/middle/exported_symbols.rs
Auto merge of #65694 - wesleywiser:uninhabited_enum_variants_pass, r=oli-obk
[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 use std::cmp;
5 use std::mem;
6 use crate::ty::{self, TyCtxt};
7 use crate::ty::subst::SubstsRef;
8
9 /// The SymbolExportLevel of a symbols specifies from which kinds of crates
10 /// the symbol will be exported. `C` symbols will be exported from any
11 /// kind of crate, including cdylibs which export very few things.
12 /// `Rust` will only be exported if the crate produced is a Rust
13 /// dylib.
14 #[derive(Eq, PartialEq, Debug, Copy, Clone, RustcEncodable, RustcDecodable)]
15 pub enum SymbolExportLevel {
16     C,
17     Rust,
18 }
19
20 impl_stable_hash_for!(enum self::SymbolExportLevel {
21     C,
22     Rust
23 });
24
25 impl SymbolExportLevel {
26     pub fn is_below_threshold(self, threshold: SymbolExportLevel) -> bool {
27         threshold == SymbolExportLevel::Rust // export everything from Rust dylibs
28           || self == SymbolExportLevel::C
29     }
30 }
31
32 #[derive(Eq, PartialEq, Debug, Copy, Clone, RustcEncodable, RustcDecodable)]
33 pub enum ExportedSymbol<'tcx> {
34     NonGeneric(DefId),
35     Generic(DefId, SubstsRef<'tcx>),
36     NoDefId(ty::SymbolName),
37 }
38
39 impl<'tcx> ExportedSymbol<'tcx> {
40     pub fn symbol_name(&self, tcx: TyCtxt<'tcx>) -> ty::SymbolName {
41         match *self {
42             ExportedSymbol::NonGeneric(def_id) => {
43                 tcx.symbol_name(ty::Instance::mono(tcx, def_id))
44             }
45             ExportedSymbol::Generic(def_id, substs) => {
46                 tcx.symbol_name(ty::Instance::new(def_id, substs))
47             }
48             ExportedSymbol::NoDefId(symbol_name) => {
49                 symbol_name
50             }
51         }
52     }
53
54     pub fn compare_stable(&self, tcx: TyCtxt<'tcx>, other: &ExportedSymbol<'tcx>) -> cmp::Ordering {
55         match *self {
56             ExportedSymbol::NonGeneric(self_def_id) => match *other {
57                 ExportedSymbol::NonGeneric(other_def_id) => {
58                     tcx.def_path_hash(self_def_id).cmp(&tcx.def_path_hash(other_def_id))
59                 }
60                 ExportedSymbol::Generic(..) |
61                 ExportedSymbol::NoDefId(_) => {
62                     cmp::Ordering::Less
63                 }
64             }
65             ExportedSymbol::Generic(..) => match *other {
66                 ExportedSymbol::NonGeneric(_) => {
67                     cmp::Ordering::Greater
68                 }
69                 ExportedSymbol::Generic(..) => {
70                     self.symbol_name(tcx).cmp(&other.symbol_name(tcx))
71                 }
72                 ExportedSymbol::NoDefId(_) => {
73                     cmp::Ordering::Less
74                 }
75             }
76             ExportedSymbol::NoDefId(self_symbol_name) => match *other {
77                 ExportedSymbol::NonGeneric(_) |
78                 ExportedSymbol::Generic(..) => {
79                     cmp::Ordering::Greater
80                 }
81                 ExportedSymbol::NoDefId(ref other_symbol_name) => {
82                     self_symbol_name.cmp(other_symbol_name)
83                 }
84             }
85         }
86     }
87 }
88
89 pub fn metadata_symbol_name(tcx: TyCtxt<'_>) -> String {
90     format!("rust_metadata_{}_{}",
91             tcx.original_crate_name(LOCAL_CRATE),
92             tcx.crate_disambiguator(LOCAL_CRATE).to_fingerprint().to_hex())
93 }
94
95 impl<'a, 'tcx> HashStable<StableHashingContext<'a>> for ExportedSymbol<'tcx> {
96     fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
97         mem::discriminant(self).hash_stable(hcx, hasher);
98         match *self {
99             ExportedSymbol::NonGeneric(def_id) => {
100                 def_id.hash_stable(hcx, hasher);
101             }
102             ExportedSymbol::Generic(def_id, substs) => {
103                 def_id.hash_stable(hcx, hasher);
104                 substs.hash_stable(hcx, hasher);
105             }
106             ExportedSymbol::NoDefId(symbol_name) => {
107                 symbol_name.hash_stable(hcx, hasher);
108             }
109         }
110     }
111 }