1 use crate::hir::def_id::{DefId, LOCAL_CRATE};
2 use crate::ich::StableHashingContext;
3 use rustc_data_structures::stable_hasher::{StableHasher, HashStable};
6 use crate::ty::{self, TyCtxt};
7 use crate::ty::subst::SubstsRef;
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
14 #[derive(Eq, PartialEq, Debug, Copy, Clone, RustcEncodable, RustcDecodable)]
15 pub enum SymbolExportLevel {
20 impl_stable_hash_for!(enum self::SymbolExportLevel {
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
32 #[derive(Eq, PartialEq, Debug, Copy, Clone, RustcEncodable, RustcDecodable)]
33 pub enum ExportedSymbol<'tcx> {
35 Generic(DefId, SubstsRef<'tcx>),
36 NoDefId(ty::SymbolName),
39 impl<'tcx> ExportedSymbol<'tcx> {
40 pub fn symbol_name(&self, tcx: TyCtxt<'tcx>) -> ty::SymbolName {
42 ExportedSymbol::NonGeneric(def_id) => {
43 tcx.symbol_name(ty::Instance::mono(tcx, def_id))
45 ExportedSymbol::Generic(def_id, substs) => {
46 tcx.symbol_name(ty::Instance::new(def_id, substs))
48 ExportedSymbol::NoDefId(symbol_name) => {
54 pub fn compare_stable(&self, tcx: TyCtxt<'tcx>, other: &ExportedSymbol<'tcx>) -> cmp::Ordering {
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))
60 ExportedSymbol::Generic(..) |
61 ExportedSymbol::NoDefId(_) => {
65 ExportedSymbol::Generic(..) => match *other {
66 ExportedSymbol::NonGeneric(_) => {
67 cmp::Ordering::Greater
69 ExportedSymbol::Generic(..) => {
70 self.symbol_name(tcx).cmp(&other.symbol_name(tcx))
72 ExportedSymbol::NoDefId(_) => {
76 ExportedSymbol::NoDefId(self_symbol_name) => match *other {
77 ExportedSymbol::NonGeneric(_) |
78 ExportedSymbol::Generic(..) => {
79 cmp::Ordering::Greater
81 ExportedSymbol::NoDefId(ref other_symbol_name) => {
82 self_symbol_name.cmp(other_symbol_name)
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())
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);
99 ExportedSymbol::NonGeneric(def_id) => {
100 def_id.hash_stable(hcx, hasher);
102 ExportedSymbol::Generic(def_id, substs) => {
103 def_id.hash_stable(hcx, hasher);
104 substs.hash_stable(hcx, hasher);
106 ExportedSymbol::NoDefId(symbol_name) => {
107 symbol_name.hash_stable(hcx, hasher);