2019-12-22 23:42:04 +01:00
|
|
|
use crate::ty::subst::SubstsRef;
|
2020-01-22 12:17:21 +01:00
|
|
|
use crate::ty::{self, Ty, TyCtxt};
|
2020-01-05 02:37:57 +01:00
|
|
|
use rustc_hir::def_id::{DefId, LOCAL_CRATE};
|
2020-01-22 12:17:21 +01:00
|
|
|
use rustc_macros::HashStable;
|
2018-02-27 17:52:07 +01:00
|
|
|
|
2017-09-12 18:32:37 +02:00
|
|
|
/// The SymbolExportLevel of a symbols specifies from which kinds of crates
|
|
|
|
/// the symbol will be exported. `C` symbols will be exported from any
|
|
|
|
/// kind of crate, including cdylibs which export very few things.
|
|
|
|
/// `Rust` will only be exported if the crate produced is a Rust
|
|
|
|
/// dylib.
|
2020-06-11 16:49:57 +02:00
|
|
|
#[derive(Eq, PartialEq, Debug, Copy, Clone, Encodable, Decodable, HashStable)]
|
2017-09-12 18:32:37 +02:00
|
|
|
pub enum SymbolExportLevel {
|
|
|
|
C,
|
|
|
|
Rust,
|
|
|
|
}
|
|
|
|
|
2017-09-13 22:22:20 +02:00
|
|
|
impl SymbolExportLevel {
|
|
|
|
pub fn is_below_threshold(self, threshold: SymbolExportLevel) -> bool {
|
2018-10-02 18:05:06 +02:00
|
|
|
threshold == SymbolExportLevel::Rust // export everything from Rust dylibs
|
|
|
|
|| self == SymbolExportLevel::C
|
2017-09-12 18:32:37 +02:00
|
|
|
}
|
|
|
|
}
|
2018-02-27 17:52:07 +01:00
|
|
|
|
2020-06-11 16:49:57 +02:00
|
|
|
#[derive(Eq, PartialEq, Debug, Copy, Clone, TyEncodable, TyDecodable, HashStable)]
|
2018-03-01 16:50:08 +01:00
|
|
|
pub enum ExportedSymbol<'tcx> {
|
2018-02-27 17:52:07 +01:00
|
|
|
NonGeneric(DefId),
|
2019-02-09 15:11:53 +01:00
|
|
|
Generic(DefId, SubstsRef<'tcx>),
|
2020-01-22 12:17:21 +01:00
|
|
|
DropGlue(Ty<'tcx>),
|
2020-07-10 07:45:05 +02:00
|
|
|
NoDefId(ty::SymbolName<'tcx>),
|
2018-02-27 17:52:07 +01:00
|
|
|
}
|
|
|
|
|
2018-03-01 16:50:08 +01:00
|
|
|
impl<'tcx> ExportedSymbol<'tcx> {
|
2020-01-16 13:21:10 +01:00
|
|
|
/// This is the symbol name of an instance if it is instantiated in the
|
|
|
|
/// local crate.
|
2020-07-10 07:45:05 +02:00
|
|
|
pub fn symbol_name_for_local_instance(&self, tcx: TyCtxt<'tcx>) -> ty::SymbolName<'tcx> {
|
2018-02-27 17:52:07 +01:00
|
|
|
match *self {
|
2019-12-22 23:42:04 +01:00
|
|
|
ExportedSymbol::NonGeneric(def_id) => tcx.symbol_name(ty::Instance::mono(tcx, def_id)),
|
2018-03-01 16:50:08 +01:00
|
|
|
ExportedSymbol::Generic(def_id, substs) => {
|
|
|
|
tcx.symbol_name(ty::Instance::new(def_id, substs))
|
|
|
|
}
|
2020-01-22 12:17:21 +01:00
|
|
|
ExportedSymbol::DropGlue(ty) => {
|
|
|
|
tcx.symbol_name(ty::Instance::resolve_drop_in_place(tcx, ty))
|
|
|
|
}
|
2019-12-22 23:42:04 +01:00
|
|
|
ExportedSymbol::NoDefId(symbol_name) => symbol_name,
|
2018-02-27 17:52:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-13 23:48:52 +02:00
|
|
|
pub fn metadata_symbol_name(tcx: TyCtxt<'_>) -> String {
|
2019-12-22 23:42:04 +01:00
|
|
|
format!(
|
|
|
|
"rust_metadata_{}_{}",
|
|
|
|
tcx.original_crate_name(LOCAL_CRATE),
|
|
|
|
tcx.crate_disambiguator(LOCAL_CRATE).to_fingerprint().to_hex()
|
|
|
|
)
|
2018-03-05 17:41:11 +01:00
|
|
|
}
|