/compiler/rustc_middle/src/middle/exported_symbols.rs

https://gitlab.com/rust-lang/rust · Rust · 72 lines · 55 code · 7 blank · 10 comment · 4 complexity · 66c3ce66c1359f291d1fb3bd83025b2c MD5 · raw file

  1. use crate::ty::subst::SubstsRef;
  2. use crate::ty::{self, Ty, TyCtxt};
  3. use rustc_hir::def_id::{DefId, LOCAL_CRATE};
  4. use rustc_macros::HashStable;
  5. /// The SymbolExportLevel of a symbols specifies from which kinds of crates
  6. /// the symbol will be exported. `C` symbols will be exported from any
  7. /// kind of crate, including cdylibs which export very few things.
  8. /// `Rust` will only be exported if the crate produced is a Rust
  9. /// dylib.
  10. #[derive(Eq, PartialEq, Debug, Copy, Clone, TyEncodable, TyDecodable, HashStable)]
  11. pub enum SymbolExportLevel {
  12. C,
  13. Rust,
  14. }
  15. impl SymbolExportLevel {
  16. pub fn is_below_threshold(self, threshold: SymbolExportLevel) -> bool {
  17. threshold == SymbolExportLevel::Rust // export everything from Rust dylibs
  18. || self == SymbolExportLevel::C
  19. }
  20. }
  21. /// Kind of exported symbols.
  22. #[derive(Eq, PartialEq, Debug, Copy, Clone, Encodable, Decodable, HashStable)]
  23. pub enum SymbolExportKind {
  24. Text,
  25. Data,
  26. Tls,
  27. }
  28. /// The `SymbolExportInfo` of a symbols specifies symbol-related information
  29. /// that is relevant to code generation and linking.
  30. #[derive(Eq, PartialEq, Debug, Copy, Clone, TyEncodable, TyDecodable, HashStable)]
  31. pub struct SymbolExportInfo {
  32. pub level: SymbolExportLevel,
  33. pub kind: SymbolExportKind,
  34. pub used: bool,
  35. }
  36. #[derive(Eq, PartialEq, Debug, Copy, Clone, TyEncodable, TyDecodable, HashStable)]
  37. pub enum ExportedSymbol<'tcx> {
  38. NonGeneric(DefId),
  39. Generic(DefId, SubstsRef<'tcx>),
  40. DropGlue(Ty<'tcx>),
  41. NoDefId(ty::SymbolName<'tcx>),
  42. }
  43. impl<'tcx> ExportedSymbol<'tcx> {
  44. /// This is the symbol name of an instance if it is instantiated in the
  45. /// local crate.
  46. pub fn symbol_name_for_local_instance(&self, tcx: TyCtxt<'tcx>) -> ty::SymbolName<'tcx> {
  47. match *self {
  48. ExportedSymbol::NonGeneric(def_id) => tcx.symbol_name(ty::Instance::mono(tcx, def_id)),
  49. ExportedSymbol::Generic(def_id, substs) => {
  50. tcx.symbol_name(ty::Instance::new(def_id, substs))
  51. }
  52. ExportedSymbol::DropGlue(ty) => {
  53. tcx.symbol_name(ty::Instance::resolve_drop_in_place(tcx, ty))
  54. }
  55. ExportedSymbol::NoDefId(symbol_name) => symbol_name,
  56. }
  57. }
  58. }
  59. pub fn metadata_symbol_name(tcx: TyCtxt<'_>) -> String {
  60. format!(
  61. "rust_metadata_{}_{:08x}",
  62. tcx.crate_name(LOCAL_CRATE),
  63. tcx.sess.local_stable_crate_id().to_u64(),
  64. )
  65. }