rustc_middle/middle/
exported_symbols.rs1use rustc_hir::def_id::{DefId, LOCAL_CRATE};
2use rustc_macros::{Decodable, Encodable, HashStable, TyDecodable, TyEncodable};
3
4use crate::ty::{self, GenericArgsRef, Ty, TyCtxt};
5
6#[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 || self == SymbolExportLevel::C
21 }
22}
23
24#[derive(Eq, PartialEq, Debug, Copy, Clone, Encodable, Decodable, HashStable, Hash)]
26pub enum SymbolExportKind {
27 Text,
28 Data,
29 Tls,
30}
31
32#[derive(Eq, PartialEq, Debug, Copy, Clone, TyEncodable, TyDecodable, HashStable)]
38pub struct SymbolExportInfo {
39 pub level: SymbolExportLevel,
40 pub kind: SymbolExportKind,
41 pub used: bool,
43 pub rustc_std_internal_symbol: bool,
46}
47
48#[derive(Eq, PartialEq, Debug, Copy, Clone, TyEncodable, TyDecodable, HashStable)]
49pub enum ExportedSymbol<'tcx> {
50 NonGeneric(DefId),
51 Generic(DefId, GenericArgsRef<'tcx>),
52 DropGlue(Ty<'tcx>),
53 AsyncDropGlueCtorShim(Ty<'tcx>),
54 AsyncDropGlue(DefId, Ty<'tcx>),
55 ThreadLocalShim(DefId),
56 NoDefId(ty::SymbolName<'tcx>),
57}
58
59impl<'tcx> ExportedSymbol<'tcx> {
60 pub fn symbol_name_for_local_instance(&self, tcx: TyCtxt<'tcx>) -> ty::SymbolName<'tcx> {
63 match *self {
64 ExportedSymbol::NonGeneric(def_id) => tcx.symbol_name(ty::Instance::mono(tcx, def_id)),
65 ExportedSymbol::Generic(def_id, args) => {
66 tcx.symbol_name(ty::Instance::new_raw(def_id, args))
67 }
68 ExportedSymbol::DropGlue(ty) => {
69 tcx.symbol_name(ty::Instance::resolve_drop_in_place(tcx, ty))
70 }
71 ExportedSymbol::AsyncDropGlueCtorShim(ty) => {
72 tcx.symbol_name(ty::Instance::resolve_async_drop_in_place(tcx, ty))
73 }
74 ExportedSymbol::AsyncDropGlue(def_id, ty) => {
75 tcx.symbol_name(ty::Instance::resolve_async_drop_in_place_poll(tcx, def_id, ty))
76 }
77 ExportedSymbol::ThreadLocalShim(def_id) => tcx.symbol_name(ty::Instance {
78 def: ty::InstanceKind::ThreadLocalShim(def_id),
79 args: ty::GenericArgs::empty(),
80 }),
81 ExportedSymbol::NoDefId(symbol_name) => symbol_name,
82 }
83 }
84}
85
86pub fn metadata_symbol_name(tcx: TyCtxt<'_>) -> String {
87 format!(
88 "rust_metadata_{}_{:08x}",
89 tcx.crate_name(LOCAL_CRATE),
90 tcx.stable_crate_id(LOCAL_CRATE),
91 )
92}