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