]> git.proxmox.com Git - rustc.git/blame - src/librustc/middle/exported_symbols.rs
New upstream version 1.43.0+dfsg1
[rustc.git] / src / librustc / middle / exported_symbols.rs
CommitLineData
532ac7d7 1use crate::ty::subst::SubstsRef;
dfeec247
XL
2use crate::ty::{self, Ty, TyCtxt};
3use rustc_hir::def_id::{DefId, LOCAL_CRATE};
4use rustc_macros::HashStable;
0531ce1d 5
ea8adc8c
XL
6/// The SymbolExportLevel of a symbols specifies from which kinds of crates
7/// the symbol will be exported. `C` symbols will be exported from any
8/// kind of crate, including cdylibs which export very few things.
9/// `Rust` will only be exported if the crate produced is a Rust
10/// dylib.
60c5eb7d 11#[derive(Eq, PartialEq, Debug, Copy, Clone, RustcEncodable, RustcDecodable, HashStable)]
ea8adc8c
XL
12pub enum SymbolExportLevel {
13 C,
14 Rust,
15}
16
ea8adc8c
XL
17impl SymbolExportLevel {
18 pub fn is_below_threshold(self, threshold: SymbolExportLevel) -> bool {
0bf4aa26
XL
19 threshold == SymbolExportLevel::Rust // export everything from Rust dylibs
20 || self == SymbolExportLevel::C
ea8adc8c
XL
21 }
22}
0531ce1d 23
dfeec247 24#[derive(Eq, PartialEq, Debug, Copy, Clone, RustcEncodable, RustcDecodable, HashStable)]
83c7162d 25pub enum ExportedSymbol<'tcx> {
0531ce1d 26 NonGeneric(DefId),
532ac7d7 27 Generic(DefId, SubstsRef<'tcx>),
dfeec247 28 DropGlue(Ty<'tcx>),
0531ce1d
XL
29 NoDefId(ty::SymbolName),
30}
31
83c7162d 32impl<'tcx> ExportedSymbol<'tcx> {
dfeec247
XL
33 /// This is the symbol name of an instance if it is instantiated in the
34 /// local crate.
35 pub fn symbol_name_for_local_instance(&self, tcx: TyCtxt<'tcx>) -> ty::SymbolName {
0531ce1d 36 match *self {
dfeec247 37 ExportedSymbol::NonGeneric(def_id) => tcx.symbol_name(ty::Instance::mono(tcx, def_id)),
83c7162d
XL
38 ExportedSymbol::Generic(def_id, substs) => {
39 tcx.symbol_name(ty::Instance::new(def_id, substs))
40 }
dfeec247
XL
41 ExportedSymbol::DropGlue(ty) => {
42 tcx.symbol_name(ty::Instance::resolve_drop_in_place(tcx, ty))
0531ce1d 43 }
dfeec247 44 ExportedSymbol::NoDefId(symbol_name) => symbol_name,
0531ce1d
XL
45 }
46 }
47}
48
dc9dc135 49pub fn metadata_symbol_name(tcx: TyCtxt<'_>) -> String {
dfeec247
XL
50 format!(
51 "rust_metadata_{}_{}",
52 tcx.original_crate_name(LOCAL_CRATE),
53 tcx.crate_disambiguator(LOCAL_CRATE).to_fingerprint().to_hex()
54 )
83c7162d 55}