diff --git a/src/librustc/hir/mod.rs b/src/librustc/hir/mod.rs index d7d9990bb88..f6085c5f873 100644 --- a/src/librustc/hir/mod.rs +++ b/src/librustc/hir/mod.rs @@ -9,14 +9,13 @@ pub use self::UnOp::*; pub use self::UnsafeSource::*; use crate::hir::def::{DefKind, Res}; -use crate::hir::def_id::{DefId, DefIndex, LocalDefId, CRATE_DEF_INDEX}; +use crate::hir::def_id::{DefId, DefIndex}; use crate::ty::query::Providers; use errors::FatalError; use rustc_data_structures::fx::FxHashSet; use rustc_data_structures::sync::{par_for_each_in, Send, Sync}; use rustc_macros::HashStable; -use rustc_serialize::{self, Decodable, Decoder, Encodable, Encoder}; use rustc_session::node_id::NodeMap; use rustc_span::source_map::{SourceMap, Spanned}; use rustc_span::symbol::{kw, sym, Symbol}; @@ -35,6 +34,7 @@ use syntax::util::parser::ExprPrecedence; pub mod check_attr; pub mod def; pub use rustc_hir::def_id; +pub use rustc_hir::hir_id::*; pub mod intravisit; pub mod itemlikevisit; pub mod map; @@ -42,89 +42,6 @@ pub mod pat_util; pub mod print; pub mod upvars; -/// Uniquely identifies a node in the HIR of the current crate. It is -/// composed of the `owner`, which is the `DefIndex` of the directly enclosing -/// `hir::Item`, `hir::TraitItem`, or `hir::ImplItem` (i.e., the closest "item-like"), -/// and the `local_id` which is unique within the given owner. -/// -/// This two-level structure makes for more stable values: One can move an item -/// around within the source code, or add or remove stuff before it, without -/// the `local_id` part of the `HirId` changing, which is a very useful property in -/// incremental compilation where we have to persist things through changes to -/// the code base. -#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug, PartialOrd, Ord)] -pub struct HirId { - pub owner: DefIndex, - pub local_id: ItemLocalId, -} - -impl HirId { - pub fn owner_def_id(self) -> DefId { - DefId::local(self.owner) - } - - pub fn owner_local_def_id(self) -> LocalDefId { - LocalDefId::from_def_id(DefId::local(self.owner)) - } -} - -impl rustc_serialize::UseSpecializedEncodable for HirId { - fn default_encode(&self, s: &mut S) -> Result<(), S::Error> { - let HirId { owner, local_id } = *self; - - owner.encode(s)?; - local_id.encode(s)?; - Ok(()) - } -} - -impl rustc_serialize::UseSpecializedDecodable for HirId { - fn default_decode(d: &mut D) -> Result { - let owner = DefIndex::decode(d)?; - let local_id = ItemLocalId::decode(d)?; - - Ok(HirId { owner, local_id }) - } -} - -impl fmt::Display for HirId { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!(f, "{:?}", self) - } -} - -rustc_data_structures::define_id_collections!(HirIdMap, HirIdSet, HirId); -rustc_data_structures::define_id_collections!(ItemLocalMap, ItemLocalSet, ItemLocalId); - -// Hack to ensure that we don't try to access the private parts of `ItemLocalId` in this module. -mod item_local_id_inner { - use rustc_index::vec::Idx; - use rustc_macros::HashStable; - rustc_index::newtype_index! { - /// An `ItemLocalId` uniquely identifies something within a given "item-like"; - /// that is, within a `hir::Item`, `hir::TraitItem`, or `hir::ImplItem`. There is no - /// guarantee that the numerical value of a given `ItemLocalId` corresponds to - /// the node's position within the owning item in any way, but there is a - /// guarantee that the `LocalItemId`s within an owner occupy a dense range of - /// integers starting at zero, so a mapping that maps all or most nodes within - /// an "item-like" to something else can be implemented by a `Vec` instead of a - /// tree or hash map. - pub struct ItemLocalId { - derive [HashStable] - } - } -} - -pub use self::item_local_id_inner::ItemLocalId; - -/// The `HirId` corresponding to `CRATE_NODE_ID` and `CRATE_DEF_INDEX`. -pub const CRATE_HIR_ID: HirId = - HirId { owner: CRATE_DEF_INDEX, local_id: ItemLocalId::from_u32_const(0) }; - -pub const DUMMY_HIR_ID: HirId = HirId { owner: CRATE_DEF_INDEX, local_id: DUMMY_ITEM_LOCAL_ID }; - -pub const DUMMY_ITEM_LOCAL_ID: ItemLocalId = ItemLocalId::MAX; - #[derive(Copy, Clone, RustcEncodable, RustcDecodable, HashStable)] pub struct Lifetime { pub hir_id: HirId, diff --git a/src/librustc_hir/hir_id.rs b/src/librustc_hir/hir_id.rs new file mode 100644 index 00000000000..46294641171 --- /dev/null +++ b/src/librustc_hir/hir_id.rs @@ -0,0 +1,79 @@ +use crate::def_id::{DefId, DefIndex, LocalDefId, CRATE_DEF_INDEX}; +use rustc_serialize::{self, Decodable, Decoder, Encodable, Encoder}; +use std::fmt; + +/// Uniquely identifies a node in the HIR of the current crate. It is +/// composed of the `owner`, which is the `DefIndex` of the directly enclosing +/// `hir::Item`, `hir::TraitItem`, or `hir::ImplItem` (i.e., the closest "item-like"), +/// and the `local_id` which is unique within the given owner. +/// +/// This two-level structure makes for more stable values: One can move an item +/// around within the source code, or add or remove stuff before it, without +/// the `local_id` part of the `HirId` changing, which is a very useful property in +/// incremental compilation where we have to persist things through changes to +/// the code base. +#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug, PartialOrd, Ord)] +pub struct HirId { + pub owner: DefIndex, + pub local_id: ItemLocalId, +} + +impl HirId { + pub fn owner_def_id(self) -> DefId { + DefId::local(self.owner) + } + + pub fn owner_local_def_id(self) -> LocalDefId { + LocalDefId::from_def_id(DefId::local(self.owner)) + } +} + +impl rustc_serialize::UseSpecializedEncodable for HirId { + fn default_encode(&self, s: &mut S) -> Result<(), S::Error> { + let HirId { owner, local_id } = *self; + + owner.encode(s)?; + local_id.encode(s)?; + Ok(()) + } +} + +impl rustc_serialize::UseSpecializedDecodable for HirId { + fn default_decode(d: &mut D) -> Result { + let owner = DefIndex::decode(d)?; + let local_id = ItemLocalId::decode(d)?; + + Ok(HirId { owner, local_id }) + } +} + +impl fmt::Display for HirId { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "{:?}", self) + } +} + +rustc_data_structures::define_id_collections!(HirIdMap, HirIdSet, HirId); +rustc_data_structures::define_id_collections!(ItemLocalMap, ItemLocalSet, ItemLocalId); + +use rustc_index::vec::Idx; +rustc_index::newtype_index! { + /// An `ItemLocalId` uniquely identifies something within a given "item-like"; + /// that is, within a `hir::Item`, `hir::TraitItem`, or `hir::ImplItem`. There is no + /// guarantee that the numerical value of a given `ItemLocalId` corresponds to + /// the node's position within the owning item in any way, but there is a + /// guarantee that the `LocalItemId`s within an owner occupy a dense range of + /// integers starting at zero, so a mapping that maps all or most nodes within + /// an "item-like" to something else can be implemented by a `Vec` instead of a + /// tree or hash map. + pub struct ItemLocalId { .. } +} +rustc_data_structures::impl_stable_hash_via_hash!(ItemLocalId); + +/// The `HirId` corresponding to `CRATE_NODE_ID` and `CRATE_DEF_INDEX`. +pub const CRATE_HIR_ID: HirId = + HirId { owner: CRATE_DEF_INDEX, local_id: ItemLocalId::from_u32_const(0) }; + +pub const DUMMY_HIR_ID: HirId = HirId { owner: CRATE_DEF_INDEX, local_id: DUMMY_ITEM_LOCAL_ID }; + +pub const DUMMY_ITEM_LOCAL_ID: ItemLocalId = ItemLocalId::MAX; diff --git a/src/librustc_hir/lib.rs b/src/librustc_hir/lib.rs index 1ef1694c320..7e778736634 100644 --- a/src/librustc_hir/lib.rs +++ b/src/librustc_hir/lib.rs @@ -1,3 +1,5 @@ #![feature(specialization)] pub mod def_id; +pub mod hir_id; +pub use hir_id::*;