diff options
Diffstat (limited to 'src/librustc_span/def_id.rs')
| -rw-r--r-- | src/librustc_span/def_id.rs | 54 |
1 files changed, 42 insertions, 12 deletions
diff --git a/src/librustc_span/def_id.rs b/src/librustc_span/def_id.rs index 6cdfd0500ca..fad9f2f6130 100644 --- a/src/librustc_span/def_id.rs +++ b/src/librustc_span/def_id.rs @@ -1,10 +1,12 @@ use crate::HashStableContext; +use rustc_data_structures::fingerprint::Fingerprint; use rustc_data_structures::stable_hasher::{HashStable, StableHasher}; use rustc_data_structures::AtomicRef; use rustc_index::vec::Idx; +use rustc_macros::HashStable_Generic; use rustc_serialize::{Decoder, Encoder}; +use std::borrow::Borrow; use std::fmt; -use std::{u32, u64}; rustc_index::newtype_index! { pub struct CrateId { @@ -22,7 +24,7 @@ pub enum CrateNum { /// Item definitions in the currently-compiled crate would have the `CrateNum` /// `LOCAL_CRATE` in their `DefId`. -pub const LOCAL_CRATE: CrateNum = CrateNum::Index(CrateId::from_u32_const(0)); +pub const LOCAL_CRATE: CrateNum = CrateNum::Index(CrateId::from_u32(0)); impl Idx for CrateNum { #[inline] @@ -102,6 +104,17 @@ impl ::std::fmt::Debug for CrateNum { } } +#[derive(Copy, Clone, Hash, PartialEq, Eq, PartialOrd, Ord, Debug, RustcEncodable, RustcDecodable)] +#[derive(HashStable_Generic)] +pub struct DefPathHash(pub Fingerprint); + +impl Borrow<Fingerprint> for DefPathHash { + #[inline] + fn borrow(&self) -> &Fingerprint { + &self.0 + } +} + rustc_index::newtype_index! { /// A DefIndex is an index into the hir-map for a crate, identifying a /// particular definition. It should really be considered an interned @@ -130,7 +143,7 @@ impl DefId { /// Makes a local `DefId` from the given `DefIndex`. #[inline] pub fn local(index: DefIndex) -> DefId { - DefId { krate: LOCAL_CRATE, index: index } + DefId { krate: LOCAL_CRATE, index } } #[inline] @@ -139,8 +152,13 @@ impl DefId { } #[inline] - pub fn to_local(self) -> LocalDefId { - LocalDefId::from_def_id(self) + pub fn as_local(self) -> Option<LocalDefId> { + if self.is_local() { Some(LocalDefId { local_def_index: self.index }) } else { None } + } + + #[inline] + pub fn expect_local(self) -> LocalDefId { + self.as_local().unwrap_or_else(|| panic!("DefId::expect_local: `{:?}` isn't local", self)) } pub fn is_top_level_module(self) -> bool { @@ -185,19 +203,31 @@ rustc_data_structures::define_id_collections!(DefIdMap, DefIdSet, DefId); /// few cases where we know that only DefIds from the local crate are expected /// and a DefId from a different crate would signify a bug somewhere. This /// is when LocalDefId comes in handy. -#[derive(Clone, Copy, PartialEq, Eq, Hash)] -pub struct LocalDefId(DefIndex); +#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] +pub struct LocalDefId { + pub local_def_index: DefIndex, +} -impl LocalDefId { +impl Idx for LocalDefId { #[inline] - pub fn from_def_id(def_id: DefId) -> LocalDefId { - assert!(def_id.is_local()); - LocalDefId(def_id.index) + fn new(idx: usize) -> Self { + LocalDefId { local_def_index: Idx::new(idx) } } + #[inline] + fn index(self) -> usize { + self.local_def_index.index() + } +} +impl LocalDefId { #[inline] pub fn to_def_id(self) -> DefId { - DefId { krate: LOCAL_CRATE, index: self.0 } + DefId { krate: LOCAL_CRATE, index: self.local_def_index } + } + + #[inline] + pub fn is_top_level_module(self) -> bool { + self.local_def_index == CRATE_DEF_INDEX } } |
