]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_hir/src/hir_id.rs
Rollup merge of #101655 - dns2utf8:box_docs, r=dtolnay
[rust.git] / compiler / rustc_hir / src / hir_id.rs
1 use crate::def_id::{DefId, LocalDefId, CRATE_DEF_ID};
2 use rustc_data_structures::stable_hasher::{HashStable, StableHasher, ToStableHashKey};
3 use rustc_span::{def_id::DefPathHash, HashStableContext};
4 use std::fmt;
5
6 #[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
7 #[derive(Encodable, Decodable)]
8 pub struct OwnerId {
9     pub def_id: LocalDefId,
10 }
11
12 impl From<OwnerId> for HirId {
13     fn from(owner: OwnerId) -> HirId {
14         HirId { owner, local_id: ItemLocalId::from_u32(0) }
15     }
16 }
17
18 impl OwnerId {
19     #[inline]
20     pub fn to_def_id(self) -> DefId {
21         self.def_id.to_def_id()
22     }
23 }
24
25 impl<CTX: HashStableContext> HashStable<CTX> for OwnerId {
26     #[inline]
27     fn hash_stable(&self, hcx: &mut CTX, hasher: &mut StableHasher) {
28         self.to_stable_hash_key(hcx).hash_stable(hcx, hasher);
29     }
30 }
31
32 impl<CTX: HashStableContext> ToStableHashKey<CTX> for OwnerId {
33     type KeyType = DefPathHash;
34
35     #[inline]
36     fn to_stable_hash_key(&self, hcx: &CTX) -> DefPathHash {
37         hcx.def_path_hash(self.to_def_id())
38     }
39 }
40
41 /// Uniquely identifies a node in the HIR of the current crate. It is
42 /// composed of the `owner`, which is the `LocalDefId` of the directly enclosing
43 /// `hir::Item`, `hir::TraitItem`, or `hir::ImplItem` (i.e., the closest "item-like"),
44 /// and the `local_id` which is unique within the given owner.
45 ///
46 /// This two-level structure makes for more stable values: One can move an item
47 /// around within the source code, or add or remove stuff before it, without
48 /// the `local_id` part of the `HirId` changing, which is a very useful property in
49 /// incremental compilation where we have to persist things through changes to
50 /// the code base.
51 #[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
52 #[derive(Encodable, Decodable, HashStable_Generic)]
53 #[rustc_pass_by_value]
54 pub struct HirId {
55     pub owner: OwnerId,
56     pub local_id: ItemLocalId,
57 }
58
59 impl HirId {
60     /// Signal local id which should never be used.
61     pub const INVALID: HirId =
62         HirId { owner: OwnerId { def_id: CRATE_DEF_ID }, local_id: ItemLocalId::INVALID };
63
64     #[inline]
65     pub fn expect_owner(self) -> OwnerId {
66         assert_eq!(self.local_id.index(), 0);
67         self.owner
68     }
69
70     #[inline]
71     pub fn as_owner(self) -> Option<OwnerId> {
72         if self.local_id.index() == 0 { Some(self.owner) } else { None }
73     }
74
75     #[inline]
76     pub fn is_owner(self) -> bool {
77         self.local_id.index() == 0
78     }
79
80     #[inline]
81     pub fn make_owner(owner: LocalDefId) -> Self {
82         Self { owner: OwnerId { def_id: owner }, local_id: ItemLocalId::from_u32(0) }
83     }
84
85     pub fn index(self) -> (usize, usize) {
86         (
87             rustc_index::vec::Idx::index(self.owner.def_id),
88             rustc_index::vec::Idx::index(self.local_id),
89         )
90     }
91 }
92
93 impl fmt::Display for HirId {
94     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
95         write!(f, "{:?}", self)
96     }
97 }
98
99 impl Ord for HirId {
100     fn cmp(&self, other: &Self) -> std::cmp::Ordering {
101         (self.index()).cmp(&(other.index()))
102     }
103 }
104
105 impl PartialOrd for HirId {
106     fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
107         Some(self.cmp(&other))
108     }
109 }
110
111 rustc_data_structures::define_stable_id_collections!(HirIdMap, HirIdSet, HirIdMapEntry, HirId);
112 rustc_data_structures::define_id_collections!(
113     ItemLocalMap,
114     ItemLocalSet,
115     ItemLocalMapEntry,
116     ItemLocalId
117 );
118
119 rustc_index::newtype_index! {
120     /// An `ItemLocalId` uniquely identifies something within a given "item-like";
121     /// that is, within a `hir::Item`, `hir::TraitItem`, or `hir::ImplItem`. There is no
122     /// guarantee that the numerical value of a given `ItemLocalId` corresponds to
123     /// the node's position within the owning item in any way, but there is a
124     /// guarantee that the `LocalItemId`s within an owner occupy a dense range of
125     /// integers starting at zero, so a mapping that maps all or most nodes within
126     /// an "item-like" to something else can be implemented by a `Vec` instead of a
127     /// tree or hash map.
128     #[derive(HashStable_Generic)]
129     pub struct ItemLocalId { .. }
130 }
131
132 impl ItemLocalId {
133     /// Signal local id which should never be used.
134     pub const INVALID: ItemLocalId = ItemLocalId::MAX;
135 }
136
137 /// The `HirId` corresponding to `CRATE_NODE_ID` and `CRATE_DEF_ID`.
138 pub const CRATE_HIR_ID: HirId =
139     HirId { owner: OwnerId { def_id: CRATE_DEF_ID }, local_id: ItemLocalId::from_u32(0) };
140
141 pub const CRATE_OWNER_ID: OwnerId = OwnerId { def_id: CRATE_DEF_ID };