]> git.proxmox.com Git - rustc.git/blame_incremental - compiler/rustc_middle/src/middle/exported_symbols.rs
New upstream version 1.63.0+dfsg1
[rustc.git] / compiler / rustc_middle / src / middle / exported_symbols.rs
... / ...
CommitLineData
1use crate::ty::subst::SubstsRef;
2use crate::ty::{self, Ty, TyCtxt};
3use rustc_hir::def_id::{DefId, LOCAL_CRATE};
4use rustc_macros::HashStable;
5
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.
11#[derive(Eq, PartialEq, Debug, Copy, Clone, TyEncodable, TyDecodable, HashStable)]
12pub enum SymbolExportLevel {
13 C,
14 Rust,
15}
16
17impl SymbolExportLevel {
18 pub fn is_below_threshold(self, threshold: SymbolExportLevel) -> bool {
19 threshold == SymbolExportLevel::Rust // export everything from Rust dylibs
20 || self == SymbolExportLevel::C
21 }
22}
23
24/// Kind of exported symbols.
25#[derive(Eq, PartialEq, Debug, Copy, Clone, Encodable, Decodable, HashStable)]
26pub enum SymbolExportKind {
27 Text,
28 Data,
29 Tls,
30}
31
32/// The `SymbolExportInfo` of a symbols specifies symbol-related information
33/// that is relevant to code generation and linking.
34#[derive(Eq, PartialEq, Debug, Copy, Clone, TyEncodable, TyDecodable, HashStable)]
35pub struct SymbolExportInfo {
36 pub level: SymbolExportLevel,
37 pub kind: SymbolExportKind,
38 pub used: bool,
39}
40
41#[derive(Eq, PartialEq, Debug, Copy, Clone, TyEncodable, TyDecodable, HashStable)]
42pub enum ExportedSymbol<'tcx> {
43 NonGeneric(DefId),
44 Generic(DefId, SubstsRef<'tcx>),
45 DropGlue(Ty<'tcx>),
46 NoDefId(ty::SymbolName<'tcx>),
47}
48
49impl<'tcx> ExportedSymbol<'tcx> {
50 /// This is the symbol name of an instance if it is instantiated in the
51 /// local crate.
52 pub fn symbol_name_for_local_instance(&self, tcx: TyCtxt<'tcx>) -> ty::SymbolName<'tcx> {
53 match *self {
54 ExportedSymbol::NonGeneric(def_id) => tcx.symbol_name(ty::Instance::mono(tcx, def_id)),
55 ExportedSymbol::Generic(def_id, substs) => {
56 tcx.symbol_name(ty::Instance::new(def_id, substs))
57 }
58 ExportedSymbol::DropGlue(ty) => {
59 tcx.symbol_name(ty::Instance::resolve_drop_in_place(tcx, ty))
60 }
61 ExportedSymbol::NoDefId(symbol_name) => symbol_name,
62 }
63 }
64}
65
66pub fn metadata_symbol_name(tcx: TyCtxt<'_>) -> String {
67 format!(
68 "rust_metadata_{}_{:08x}",
69 tcx.crate_name(LOCAL_CRATE),
70 tcx.sess.local_stable_crate_id().to_u64(),
71 )
72}