rustc_middle/middle/
exported_symbols.rs
1use 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)]
26pub enum SymbolExportKind {
27 Text,
28 Data,
29 Tls,
30}
31
32#[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, GenericArgsRef<'tcx>),
45 DropGlue(Ty<'tcx>),
46 AsyncDropGlueCtorShim(Ty<'tcx>),
47 ThreadLocalShim(DefId),
48 NoDefId(ty::SymbolName<'tcx>),
49}
50
51impl<'tcx> ExportedSymbol<'tcx> {
52 pub fn symbol_name_for_local_instance(&self, tcx: TyCtxt<'tcx>) -> ty::SymbolName<'tcx> {
55 match *self {
56 ExportedSymbol::NonGeneric(def_id) => tcx.symbol_name(ty::Instance::mono(tcx, def_id)),
57 ExportedSymbol::Generic(def_id, args) => {
58 tcx.symbol_name(ty::Instance::new(def_id, args))
59 }
60 ExportedSymbol::DropGlue(ty) => {
61 tcx.symbol_name(ty::Instance::resolve_drop_in_place(tcx, ty))
62 }
63 ExportedSymbol::AsyncDropGlueCtorShim(ty) => {
64 tcx.symbol_name(ty::Instance::resolve_async_drop_in_place(tcx, ty))
65 }
66 ExportedSymbol::ThreadLocalShim(def_id) => tcx.symbol_name(ty::Instance {
67 def: ty::InstanceKind::ThreadLocalShim(def_id),
68 args: ty::GenericArgs::empty(),
69 }),
70 ExportedSymbol::NoDefId(symbol_name) => symbol_name,
71 }
72 }
73}
74
75pub fn metadata_symbol_name(tcx: TyCtxt<'_>) -> String {
76 format!(
77 "rust_metadata_{}_{:08x}",
78 tcx.crate_name(LOCAL_CRATE),
79 tcx.stable_crate_id(LOCAL_CRATE),
80 )
81}