2019-02-09 14:11:53 +00:00
|
|
|
use crate::ty::subst::SubstsRef;
|
2020-01-22 11:17:21 +00:00
|
|
|
use crate::ty::{self, Ty, TyCtxt};
|
2020-01-05 01:37:57 +00:00
|
|
|
use rustc_hir::def_id::{DefId, LOCAL_CRATE};
|
2020-01-22 11:17:21 +00:00
|
|
|
use rustc_macros::HashStable;
|
2018-02-27 16:52:07 +00:00
|
|
|
|
2017-09-12 16:32:37 +00: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-29 20:09:54 +00:00
|
|
|
#[derive(Eq, PartialEq, Debug, Copy, Clone, TyEncodable, TyDecodable, HashStable)]
|
2017-09-12 16:32:37 +00:00
|
|
|
pub enum SymbolExportLevel {
|
|
|
|
C,
|
|
|
|
Rust,
|
|
|
|
}
|
|
|
|
|
2017-09-13 20:22:20 +00:00
|
|
|
impl SymbolExportLevel {
|
|
|
|
pub fn is_below_threshold(self, threshold: SymbolExportLevel) -> bool {
|
2018-10-02 16:05:06 +00:00
|
|
|
threshold == SymbolExportLevel::Rust // export everything from Rust dylibs
|
|
|
|
|| self == SymbolExportLevel::C
|
2017-09-12 16:32:37 +00:00
|
|
|
}
|
|
|
|
}
|
2018-02-27 16:52:07 +00:00
|
|
|
|
2022-04-02 21:54:51 +00:00
|
|
|
/// Kind of exported symbols.
|
|
|
|
#[derive(Eq, PartialEq, Debug, Copy, Clone, Encodable, Decodable, HashStable)]
|
|
|
|
pub enum SymbolExportKind {
|
|
|
|
Text,
|
|
|
|
Data,
|
|
|
|
Tls,
|
|
|
|
}
|
|
|
|
|
2022-04-02 21:27:33 +00:00
|
|
|
/// The `SymbolExportInfo` of a symbols specifies symbol-related information
|
|
|
|
/// that is relevant to code generation and linking.
|
|
|
|
#[derive(Eq, PartialEq, Debug, Copy, Clone, TyEncodable, TyDecodable, HashStable)]
|
|
|
|
pub struct SymbolExportInfo {
|
|
|
|
pub level: SymbolExportLevel,
|
2022-04-02 21:54:51 +00:00
|
|
|
pub kind: SymbolExportKind,
|
|
|
|
pub used: bool,
|
2022-04-02 21:27:33 +00:00
|
|
|
}
|
|
|
|
|
2020-06-11 14:49:57 +00:00
|
|
|
#[derive(Eq, PartialEq, Debug, Copy, Clone, TyEncodable, TyDecodable, HashStable)]
|
2018-03-01 15:50:08 +00:00
|
|
|
pub enum ExportedSymbol<'tcx> {
|
2018-02-27 16:52:07 +00:00
|
|
|
NonGeneric(DefId),
|
2019-02-09 14:11:53 +00:00
|
|
|
Generic(DefId, SubstsRef<'tcx>),
|
2020-01-22 11:17:21 +00:00
|
|
|
DropGlue(Ty<'tcx>),
|
2023-02-03 08:04:12 +00:00
|
|
|
ThreadLocalShim(DefId),
|
2020-07-10 05:45:05 +00:00
|
|
|
NoDefId(ty::SymbolName<'tcx>),
|
2018-02-27 16:52:07 +00:00
|
|
|
}
|
|
|
|
|
2018-03-01 15:50:08 +00:00
|
|
|
impl<'tcx> ExportedSymbol<'tcx> {
|
2020-01-16 12:21:10 +00:00
|
|
|
/// This is the symbol name of an instance if it is instantiated in the
|
|
|
|
/// local crate.
|
2020-07-10 05:45:05 +00:00
|
|
|
pub fn symbol_name_for_local_instance(&self, tcx: TyCtxt<'tcx>) -> ty::SymbolName<'tcx> {
|
2018-02-27 16:52:07 +00:00
|
|
|
match *self {
|
|
|
|
ExportedSymbol::NonGeneric(def_id) => tcx.symbol_name(ty::Instance::mono(tcx, def_id)),
|
2018-03-01 15:50:08 +00:00
|
|
|
ExportedSymbol::Generic(def_id, substs) => {
|
|
|
|
tcx.symbol_name(ty::Instance::new(def_id, substs))
|
|
|
|
}
|
2020-01-22 11:17:21 +00:00
|
|
|
ExportedSymbol::DropGlue(ty) => {
|
|
|
|
tcx.symbol_name(ty::Instance::resolve_drop_in_place(tcx, ty))
|
|
|
|
}
|
2023-02-03 08:04:12 +00:00
|
|
|
ExportedSymbol::ThreadLocalShim(def_id) => tcx.symbol_name(ty::Instance {
|
|
|
|
def: ty::InstanceDef::ThreadLocalShim(def_id),
|
|
|
|
substs: ty::InternalSubsts::empty(),
|
|
|
|
}),
|
2018-02-27 16:52:07 +00:00
|
|
|
ExportedSymbol::NoDefId(symbol_name) => symbol_name,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-13 21:48:52 +00:00
|
|
|
pub fn metadata_symbol_name(tcx: TyCtxt<'_>) -> String {
|
2018-03-05 16:41:11 +00:00
|
|
|
format!(
|
2021-06-08 16:36:30 +00:00
|
|
|
"rust_metadata_{}_{:08x}",
|
2021-05-10 16:23:32 +00:00
|
|
|
tcx.crate_name(LOCAL_CRATE),
|
2021-06-08 16:36:30 +00:00
|
|
|
tcx.sess.local_stable_crate_id().to_u64(),
|
2019-12-22 22:42:04 +00:00
|
|
|
)
|
2018-03-05 16:41:11 +00:00
|
|
|
}
|