]> git.lizzy.rs Git - rust.git/blob - src/librustc_hir/hir_id.rs
pin docs: add some forward references
[rust.git] / src / librustc_hir / hir_id.rs
1 use crate::def_id::{LocalDefId, CRATE_DEF_INDEX};
2 use std::fmt;
3
4 /// Uniquely identifies a node in the HIR of the current crate. It is
5 /// composed of the `owner`, which is the `LocalDefId` of the directly enclosing
6 /// `hir::Item`, `hir::TraitItem`, or `hir::ImplItem` (i.e., the closest "item-like"),
7 /// and the `local_id` which is unique within the given owner.
8 ///
9 /// This two-level structure makes for more stable values: One can move an item
10 /// around within the source code, or add or remove stuff before it, without
11 /// the `local_id` part of the `HirId` changing, which is a very useful property in
12 /// incremental compilation where we have to persist things through changes to
13 /// the code base.
14 #[derive(Copy, Clone, PartialEq, Eq, Hash, Debug, PartialOrd, Ord, RustcEncodable, RustcDecodable)]
15 pub struct HirId {
16     pub owner: LocalDefId,
17     pub local_id: ItemLocalId,
18 }
19
20 impl fmt::Display for HirId {
21     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
22         write!(f, "{:?}", self)
23     }
24 }
25
26 rustc_data_structures::define_id_collections!(HirIdMap, HirIdSet, HirId);
27 rustc_data_structures::define_id_collections!(ItemLocalMap, ItemLocalSet, ItemLocalId);
28
29 rustc_index::newtype_index! {
30     /// An `ItemLocalId` uniquely identifies something within a given "item-like";
31     /// that is, within a `hir::Item`, `hir::TraitItem`, or `hir::ImplItem`. There is no
32     /// guarantee that the numerical value of a given `ItemLocalId` corresponds to
33     /// the node's position within the owning item in any way, but there is a
34     /// guarantee that the `LocalItemId`s within an owner occupy a dense range of
35     /// integers starting at zero, so a mapping that maps all or most nodes within
36     /// an "item-like" to something else can be implemented by a `Vec` instead of a
37     /// tree or hash map.
38     pub struct ItemLocalId { .. }
39 }
40 rustc_data_structures::impl_stable_hash_via_hash!(ItemLocalId);
41
42 /// The `HirId` corresponding to `CRATE_NODE_ID` and `CRATE_DEF_INDEX`.
43 pub const CRATE_HIR_ID: HirId = HirId {
44     owner: LocalDefId { local_def_index: CRATE_DEF_INDEX },
45     local_id: ItemLocalId::from_u32(0),
46 };
47
48 pub const DUMMY_ITEM_LOCAL_ID: ItemLocalId = ItemLocalId::MAX;