]> git.proxmox.com Git - rustc.git/blame - compiler/rustc_hir/src/hir_id.rs
New upstream version 1.60.0+dfsg1
[rustc.git] / compiler / rustc_hir / src / hir_id.rs
CommitLineData
ba9703b0 1use crate::def_id::{LocalDefId, CRATE_DEF_INDEX};
dfeec247
XL
2use std::fmt;
3
4/// Uniquely identifies a node in the HIR of the current crate. It is
ba9703b0 5/// composed of the `owner`, which is the `LocalDefId` of the directly enclosing
dfeec247
XL
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.
a2a8927a 14#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
3dfed10e 15#[derive(Encodable, Decodable)]
dfeec247 16pub struct HirId {
ba9703b0 17 pub owner: LocalDefId,
dfeec247
XL
18 pub local_id: ItemLocalId,
19}
20
6a06907d 21impl HirId {
5099ac24 22 #[inline]
6a06907d
XL
23 pub fn expect_owner(self) -> LocalDefId {
24 assert_eq!(self.local_id.index(), 0);
25 self.owner
26 }
27
5099ac24 28 #[inline]
6a06907d
XL
29 pub fn as_owner(self) -> Option<LocalDefId> {
30 if self.local_id.index() == 0 { Some(self.owner) } else { None }
31 }
32
5099ac24
FG
33 #[inline]
34 pub fn is_owner(self) -> bool {
35 self.local_id.index() == 0
36 }
37
6a06907d
XL
38 #[inline]
39 pub fn make_owner(owner: LocalDefId) -> Self {
40 Self { owner, local_id: ItemLocalId::from_u32(0) }
41 }
a2a8927a
XL
42
43 pub fn index(self) -> (usize, usize) {
44 (rustc_index::vec::Idx::index(self.owner), rustc_index::vec::Idx::index(self.local_id))
45 }
6a06907d
XL
46}
47
dfeec247
XL
48impl fmt::Display for HirId {
49 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
50 write!(f, "{:?}", self)
51 }
52}
53
a2a8927a
XL
54impl Ord for HirId {
55 fn cmp(&self, other: &Self) -> std::cmp::Ordering {
56 (self.index()).cmp(&(other.index()))
57 }
58}
59
60impl PartialOrd for HirId {
61 fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
62 Some(self.cmp(&other))
63 }
64}
65
dfeec247
XL
66rustc_data_structures::define_id_collections!(HirIdMap, HirIdSet, HirId);
67rustc_data_structures::define_id_collections!(ItemLocalMap, ItemLocalSet, ItemLocalId);
68
69rustc_index::newtype_index! {
70 /// An `ItemLocalId` uniquely identifies something within a given "item-like";
71 /// that is, within a `hir::Item`, `hir::TraitItem`, or `hir::ImplItem`. There is no
72 /// guarantee that the numerical value of a given `ItemLocalId` corresponds to
73 /// the node's position within the owning item in any way, but there is a
74 /// guarantee that the `LocalItemId`s within an owner occupy a dense range of
75 /// integers starting at zero, so a mapping that maps all or most nodes within
76 /// an "item-like" to something else can be implemented by a `Vec` instead of a
77 /// tree or hash map.
78 pub struct ItemLocalId { .. }
79}
80rustc_data_structures::impl_stable_hash_via_hash!(ItemLocalId);
3c0e092e
XL
81impl ItemLocalId {
82 /// Signal local id which should never be used.
83 pub const INVALID: ItemLocalId = ItemLocalId::MAX;
84}
dfeec247
XL
85
86/// The `HirId` corresponding to `CRATE_NODE_ID` and `CRATE_DEF_INDEX`.
ba9703b0
XL
87pub const CRATE_HIR_ID: HirId = HirId {
88 owner: LocalDefId { local_def_index: CRATE_DEF_INDEX },
89 local_id: ItemLocalId::from_u32(0),
90};