/src/librustc/middle/exported_symbols.rs

https://gitlab.com/jianglu/rust · Rust · 133 lines · 109 code · 9 blank · 15 comment · 11 complexity · 91a13fa98115da99facaf4b559c96775 MD5 · raw file

  1. // Copyright 2016 The Rust Project Developers. See the COPYRIGHT
  2. // file at the top-level directory of this distribution and at
  3. // http://rust-lang.org/COPYRIGHT.
  4. //
  5. // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
  6. // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
  7. // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
  8. // option. This file may not be copied, modified, or distributed
  9. // except according to those terms.
  10. use hir::def_id::{DefId, LOCAL_CRATE};
  11. use ich::StableHashingContext;
  12. use rustc_data_structures::stable_hasher::{StableHasher, HashStable,
  13. StableHasherResult};
  14. use std::cmp;
  15. use std::mem;
  16. use ty;
  17. use ty::subst::Substs;
  18. /// The SymbolExportLevel of a symbols specifies from which kinds of crates
  19. /// the symbol will be exported. `C` symbols will be exported from any
  20. /// kind of crate, including cdylibs which export very few things.
  21. /// `Rust` will only be exported if the crate produced is a Rust
  22. /// dylib.
  23. #[derive(Eq, PartialEq, Debug, Copy, Clone, RustcEncodable, RustcDecodable)]
  24. pub enum SymbolExportLevel {
  25. C,
  26. Rust,
  27. }
  28. impl_stable_hash_for!(enum self::SymbolExportLevel {
  29. C,
  30. Rust
  31. });
  32. impl SymbolExportLevel {
  33. pub fn is_below_threshold(self, threshold: SymbolExportLevel) -> bool {
  34. if threshold == SymbolExportLevel::Rust {
  35. // We export everything from Rust dylibs
  36. true
  37. } else {
  38. self == SymbolExportLevel::C
  39. }
  40. }
  41. }
  42. #[derive(Eq, PartialEq, Debug, Copy, Clone, RustcEncodable, RustcDecodable)]
  43. pub enum ExportedSymbol<'tcx> {
  44. NonGeneric(DefId),
  45. Generic(DefId, &'tcx Substs<'tcx>),
  46. NoDefId(ty::SymbolName),
  47. }
  48. impl<'tcx> ExportedSymbol<'tcx> {
  49. pub fn symbol_name(&self,
  50. tcx: ty::TyCtxt<'_, 'tcx, '_>)
  51. -> ty::SymbolName {
  52. match *self {
  53. ExportedSymbol::NonGeneric(def_id) => {
  54. tcx.symbol_name(ty::Instance::mono(tcx, def_id))
  55. }
  56. ExportedSymbol::Generic(def_id, substs) => {
  57. tcx.symbol_name(ty::Instance::new(def_id, substs))
  58. }
  59. ExportedSymbol::NoDefId(symbol_name) => {
  60. symbol_name
  61. }
  62. }
  63. }
  64. pub fn compare_stable(&self,
  65. tcx: ty::TyCtxt<'_, 'tcx, '_>,
  66. other: &ExportedSymbol<'tcx>)
  67. -> cmp::Ordering {
  68. match *self {
  69. ExportedSymbol::NonGeneric(self_def_id) => match *other {
  70. ExportedSymbol::NonGeneric(other_def_id) => {
  71. tcx.def_path_hash(self_def_id).cmp(&tcx.def_path_hash(other_def_id))
  72. }
  73. ExportedSymbol::Generic(..) |
  74. ExportedSymbol::NoDefId(_) => {
  75. cmp::Ordering::Less
  76. }
  77. }
  78. ExportedSymbol::Generic(..) => match *other {
  79. ExportedSymbol::NonGeneric(_) => {
  80. cmp::Ordering::Greater
  81. }
  82. ExportedSymbol::Generic(..) => {
  83. self.symbol_name(tcx).cmp(&other.symbol_name(tcx))
  84. }
  85. ExportedSymbol::NoDefId(_) => {
  86. cmp::Ordering::Less
  87. }
  88. }
  89. ExportedSymbol::NoDefId(self_symbol_name) => match *other {
  90. ExportedSymbol::NonGeneric(_) |
  91. ExportedSymbol::Generic(..) => {
  92. cmp::Ordering::Greater
  93. }
  94. ExportedSymbol::NoDefId(ref other_symbol_name) => {
  95. self_symbol_name.cmp(other_symbol_name)
  96. }
  97. }
  98. }
  99. }
  100. }
  101. pub fn metadata_symbol_name(tcx: ty::TyCtxt) -> String {
  102. format!("rust_metadata_{}_{}",
  103. tcx.original_crate_name(LOCAL_CRATE),
  104. tcx.crate_disambiguator(LOCAL_CRATE).to_fingerprint().to_hex())
  105. }
  106. impl<'a, 'gcx> HashStable<StableHashingContext<'a>> for ExportedSymbol<'gcx> {
  107. fn hash_stable<W: StableHasherResult>(&self,
  108. hcx: &mut StableHashingContext<'a>,
  109. hasher: &mut StableHasher<W>) {
  110. mem::discriminant(self).hash_stable(hcx, hasher);
  111. match *self {
  112. ExportedSymbol::NonGeneric(def_id) => {
  113. def_id.hash_stable(hcx, hasher);
  114. }
  115. ExportedSymbol::Generic(def_id, substs) => {
  116. def_id.hash_stable(hcx, hasher);
  117. substs.hash_stable(hcx, hasher);
  118. }
  119. ExportedSymbol::NoDefId(symbol_name) => {
  120. symbol_name.hash_stable(hcx, hasher);
  121. }
  122. }
  123. }
  124. }