diff options
| author | Aaron Hill <aa1ronham@gmail.com> | 2020-02-08 15:06:31 -0500 |
|---|---|---|
| committer | Aaron Hill <aa1ronham@gmail.com> | 2020-02-08 16:48:01 -0500 |
| commit | 619051e4f080e3f3ccb94d7789bbef58282e64a7 (patch) | |
| tree | d8cbede004c8bac2f52795c5d2d5d5b8bf3a6ded /src/librustc_span | |
| parent | fb29dfcc9ada6ed10308b6e7f405569f61a9af0b (diff) | |
| download | rust-619051e4f080e3f3ccb94d7789bbef58282e64a7.tar.gz rust-619051e4f080e3f3ccb94d7789bbef58282e64a7.zip | |
Move librustc_hir/def_id.rs to librustc_span/def_id.rs
For noww, librustc_hir re-exports the `def_id` module from librustc_span, so the rest of rustc can continue to reference rustc_hir::def_id
Diffstat (limited to 'src/librustc_span')
| -rw-r--r-- | src/librustc_span/def_id.rs | 217 | ||||
| -rw-r--r-- | src/librustc_span/lib.rs | 4 |
2 files changed, 220 insertions, 1 deletions
diff --git a/src/librustc_span/def_id.rs b/src/librustc_span/def_id.rs new file mode 100644 index 00000000000..6cdfd0500ca --- /dev/null +++ b/src/librustc_span/def_id.rs @@ -0,0 +1,217 @@ +use crate::HashStableContext; +use rustc_data_structures::stable_hasher::{HashStable, StableHasher}; +use rustc_data_structures::AtomicRef; +use rustc_index::vec::Idx; +use rustc_serialize::{Decoder, Encoder}; +use std::fmt; +use std::{u32, u64}; + +rustc_index::newtype_index! { + pub struct CrateId { + ENCODABLE = custom + } +} + +#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] +pub enum CrateNum { + /// A special `CrateNum` that we use for the `tcx.rcache` when decoding from + /// the incr. comp. cache. + ReservedForIncrCompCache, + Index(CrateId), +} + +/// 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)); + +impl Idx for CrateNum { + #[inline] + fn new(value: usize) -> Self { + CrateNum::Index(Idx::new(value)) + } + + #[inline] + fn index(self) -> usize { + match self { + CrateNum::Index(idx) => Idx::index(idx), + _ => panic!("Tried to get crate index of {:?}", self), + } + } +} + +impl CrateNum { + pub fn new(x: usize) -> CrateNum { + CrateNum::from_usize(x) + } + + pub fn from_usize(x: usize) -> CrateNum { + CrateNum::Index(CrateId::from_usize(x)) + } + + pub fn from_u32(x: u32) -> CrateNum { + CrateNum::Index(CrateId::from_u32(x)) + } + + pub fn as_usize(self) -> usize { + match self { + CrateNum::Index(id) => id.as_usize(), + _ => panic!("tried to get index of non-standard crate {:?}", self), + } + } + + pub fn as_u32(self) -> u32 { + match self { + CrateNum::Index(id) => id.as_u32(), + _ => panic!("tried to get index of non-standard crate {:?}", self), + } + } + + pub fn as_def_id(&self) -> DefId { + DefId { krate: *self, index: CRATE_DEF_INDEX } + } +} + +impl fmt::Display for CrateNum { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + CrateNum::Index(id) => fmt::Display::fmt(&id.private, f), + CrateNum::ReservedForIncrCompCache => write!(f, "crate for decoding incr comp cache"), + } + } +} + +/// As a local identifier, a `CrateNum` is only meaningful within its context, e.g. within a tcx. +/// Therefore, make sure to include the context when encode a `CrateNum`. +impl rustc_serialize::UseSpecializedEncodable for CrateNum { + fn default_encode<E: Encoder>(&self, e: &mut E) -> Result<(), E::Error> { + e.emit_u32(self.as_u32()) + } +} +impl rustc_serialize::UseSpecializedDecodable for CrateNum { + fn default_decode<D: Decoder>(d: &mut D) -> Result<CrateNum, D::Error> { + Ok(CrateNum::from_u32(d.read_u32()?)) + } +} + +impl ::std::fmt::Debug for CrateNum { + fn fmt(&self, fmt: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result { + match self { + CrateNum::Index(id) => write!(fmt, "crate{}", id.private), + CrateNum::ReservedForIncrCompCache => write!(fmt, "crate for decoding incr comp cache"), + } + } +} + +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 + /// shorthand for a particular DefPath. + pub struct DefIndex { + DEBUG_FORMAT = "DefIndex({})", + + /// The crate root is always assigned index 0 by the AST Map code, + /// thanks to `NodeCollector::new`. + const CRATE_DEF_INDEX = 0, + } +} + +impl rustc_serialize::UseSpecializedEncodable for DefIndex {} +impl rustc_serialize::UseSpecializedDecodable for DefIndex {} + +/// A `DefId` identifies a particular *definition*, by combining a crate +/// index and a def index. +#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Copy)] +pub struct DefId { + pub krate: CrateNum, + pub index: DefIndex, +} + +impl DefId { + /// Makes a local `DefId` from the given `DefIndex`. + #[inline] + pub fn local(index: DefIndex) -> DefId { + DefId { krate: LOCAL_CRATE, index: index } + } + + #[inline] + pub fn is_local(self) -> bool { + self.krate == LOCAL_CRATE + } + + #[inline] + pub fn to_local(self) -> LocalDefId { + LocalDefId::from_def_id(self) + } + + pub fn is_top_level_module(self) -> bool { + self.is_local() && self.index == CRATE_DEF_INDEX + } +} + +impl rustc_serialize::UseSpecializedEncodable for DefId { + fn default_encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> { + let krate = u64::from(self.krate.as_u32()); + let index = u64::from(self.index.as_u32()); + s.emit_u64((krate << 32) | index) + } +} +impl rustc_serialize::UseSpecializedDecodable for DefId { + fn default_decode<D: Decoder>(d: &mut D) -> Result<DefId, D::Error> { + let def_id = d.read_u64()?; + let krate = CrateNum::from_u32((def_id >> 32) as u32); + let index = DefIndex::from_u32((def_id & 0xffffffff) as u32); + Ok(DefId { krate, index }) + } +} + +pub fn default_def_id_debug(def_id: DefId, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_struct("DefId").field("krate", &def_id.krate).field("index", &def_id.index).finish() +} + +pub static DEF_ID_DEBUG: AtomicRef<fn(DefId, &mut fmt::Formatter<'_>) -> fmt::Result> = + AtomicRef::new(&(default_def_id_debug as fn(_, &mut fmt::Formatter<'_>) -> _)); + +impl fmt::Debug for DefId { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + (*DEF_ID_DEBUG)(*self, f) + } +} + +rustc_data_structures::define_id_collections!(DefIdMap, DefIdSet, DefId); + +/// A LocalDefId is equivalent to a DefId with `krate == LOCAL_CRATE`. Since +/// we encode this information in the type, we can ensure at compile time that +/// no DefIds from upstream crates get thrown into the mix. There are quite a +/// 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); + +impl LocalDefId { + #[inline] + pub fn from_def_id(def_id: DefId) -> LocalDefId { + assert!(def_id.is_local()); + LocalDefId(def_id.index) + } + + #[inline] + pub fn to_def_id(self) -> DefId { + DefId { krate: LOCAL_CRATE, index: self.0 } + } +} + +impl fmt::Debug for LocalDefId { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + self.to_def_id().fmt(f) + } +} + +impl rustc_serialize::UseSpecializedEncodable for LocalDefId {} +impl rustc_serialize::UseSpecializedDecodable for LocalDefId {} + +impl<CTX: HashStableContext> HashStable<CTX> for DefId { + fn hash_stable(&self, hcx: &mut CTX, hasher: &mut StableHasher) { + hcx.hash_def_id(*self, hasher) + } +} diff --git a/src/librustc_span/lib.rs b/src/librustc_span/lib.rs index 413bd77daae..87342d6a301 100644 --- a/src/librustc_span/lib.rs +++ b/src/librustc_span/lib.rs @@ -25,7 +25,8 @@ use edition::Edition; pub mod hygiene; use hygiene::Transparency; pub use hygiene::{DesugaringKind, ExpnData, ExpnId, ExpnKind, MacroKind, SyntaxContext}; - +pub mod def_id; +use def_id::DefId; mod span_encoding; pub use span_encoding::{Span, DUMMY_SP}; @@ -1561,6 +1562,7 @@ fn lookup_line(lines: &[BytePos], pos: BytePos) -> isize { /// instead of implementing everything in librustc. pub trait HashStableContext { fn hash_spans(&self) -> bool; + fn hash_def_id(&mut self, _: DefId, hasher: &mut StableHasher); fn byte_pos_to_line_and_col( &mut self, byte: BytePos, |
