]> git.lizzy.rs Git - rust.git/blob - src/librustc_hir/hir_id.rs
Rework `rustc_serialize`
[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)]
15 #[derive(Encodable, Decodable)]
16 pub struct HirId {
17     pub owner: LocalDefId,
18     pub local_id: ItemLocalId,
19 }
20
21 impl fmt::Display for HirId {
22     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
23         write!(f, "{:?}", self)
24     }
25 }
26
27 rustc_data_structures::define_id_collections!(HirIdMap, HirIdSet, HirId);
28 rustc_data_structures::define_id_collections!(ItemLocalMap, ItemLocalSet, ItemLocalId);
29
30 rustc_index::newtype_index! {
31     /// An `ItemLocalId` uniquely identifies something within a given "item-like";
32     /// that is, within a `hir::Item`, `hir::TraitItem`, or `hir::ImplItem`. There is no
33     /// guarantee that the numerical value of a given `ItemLocalId` corresponds to
34     /// the node's position within the owning item in any way, but there is a
35     /// guarantee that the `LocalItemId`s within an owner occupy a dense range of
36     /// integers starting at zero, so a mapping that maps all or most nodes within
37     /// an "item-like" to something else can be implemented by a `Vec` instead of a
38     /// tree or hash map.
39     pub struct ItemLocalId { .. }
40 }
41 rustc_data_structures::impl_stable_hash_via_hash!(ItemLocalId);
42
43 /// The `HirId` corresponding to `CRATE_NODE_ID` and `CRATE_DEF_INDEX`.
44 pub const CRATE_HIR_ID: HirId = HirId {
45     owner: LocalDefId { local_def_index: CRATE_DEF_INDEX },
46     local_id: ItemLocalId::from_u32(0),
47 };
48
49 pub const DUMMY_ITEM_LOCAL_ID: ItemLocalId = ItemLocalId::MAX;