2022-04-15 19:27:53 +02:00
|
|
|
use crate::def_id::{LocalDefId, CRATE_DEF_ID};
|
2019-12-25 15:26:30 +01:00
|
|
|
use std::fmt;
|
|
|
|
|
|
|
|
/// Uniquely identifies a node in the HIR of the current crate. It is
|
2020-03-18 20:27:59 +02:00
|
|
|
/// composed of the `owner`, which is the `LocalDefId` of the directly enclosing
|
2019-12-25 15:26:30 +01:00
|
|
|
/// `hir::Item`, `hir::TraitItem`, or `hir::ImplItem` (i.e., the closest "item-like"),
|
|
|
|
/// and the `local_id` which is unique within the given owner.
|
|
|
|
///
|
|
|
|
/// This two-level structure makes for more stable values: One can move an item
|
|
|
|
/// around within the source code, or add or remove stuff before it, without
|
|
|
|
/// the `local_id` part of the `HirId` changing, which is a very useful property in
|
|
|
|
/// incremental compilation where we have to persist things through changes to
|
|
|
|
/// the code base.
|
2021-10-30 10:11:50 -05:00
|
|
|
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
|
2022-04-04 22:19:25 +02:00
|
|
|
#[derive(Encodable, Decodable, HashStable_Generic)]
|
2022-03-08 15:39:52 +01:00
|
|
|
#[rustc_pass_by_value]
|
2019-12-25 15:26:30 +01:00
|
|
|
pub struct HirId {
|
2020-03-18 20:27:59 +02:00
|
|
|
pub owner: LocalDefId,
|
2019-12-25 15:26:30 +01:00
|
|
|
pub local_id: ItemLocalId,
|
|
|
|
}
|
|
|
|
|
2021-01-30 17:47:51 +01:00
|
|
|
impl HirId {
|
2021-12-10 13:23:48 +01:00
|
|
|
#[inline]
|
2021-01-30 17:47:51 +01:00
|
|
|
pub fn expect_owner(self) -> LocalDefId {
|
|
|
|
assert_eq!(self.local_id.index(), 0);
|
|
|
|
self.owner
|
|
|
|
}
|
|
|
|
|
2021-12-10 13:23:48 +01:00
|
|
|
#[inline]
|
2021-01-30 17:47:51 +01:00
|
|
|
pub fn as_owner(self) -> Option<LocalDefId> {
|
|
|
|
if self.local_id.index() == 0 { Some(self.owner) } else { None }
|
|
|
|
}
|
|
|
|
|
2021-09-22 19:28:20 +02:00
|
|
|
#[inline]
|
|
|
|
pub fn is_owner(self) -> bool {
|
|
|
|
self.local_id.index() == 0
|
|
|
|
}
|
|
|
|
|
2021-02-26 00:00:00 +00:00
|
|
|
#[inline]
|
2021-01-30 17:47:51 +01:00
|
|
|
pub fn make_owner(owner: LocalDefId) -> Self {
|
|
|
|
Self { owner, local_id: ItemLocalId::from_u32(0) }
|
|
|
|
}
|
2021-10-30 10:11:50 -05:00
|
|
|
|
|
|
|
pub fn index(self) -> (usize, usize) {
|
|
|
|
(rustc_index::vec::Idx::index(self.owner), rustc_index::vec::Idx::index(self.local_id))
|
|
|
|
}
|
2021-01-30 17:47:51 +01:00
|
|
|
}
|
|
|
|
|
2019-12-25 15:26:30 +01:00
|
|
|
impl fmt::Display for HirId {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
write!(f, "{:?}", self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-30 10:11:50 -05:00
|
|
|
impl Ord for HirId {
|
|
|
|
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
|
|
|
|
(self.index()).cmp(&(other.index()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl PartialOrd for HirId {
|
|
|
|
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
|
|
|
|
Some(self.cmp(&other))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-25 15:26:30 +01:00
|
|
|
rustc_data_structures::define_id_collections!(HirIdMap, HirIdSet, HirId);
|
|
|
|
rustc_data_structures::define_id_collections!(ItemLocalMap, ItemLocalSet, ItemLocalId);
|
|
|
|
|
|
|
|
rustc_index::newtype_index! {
|
|
|
|
/// An `ItemLocalId` uniquely identifies something within a given "item-like";
|
|
|
|
/// that is, within a `hir::Item`, `hir::TraitItem`, or `hir::ImplItem`. There is no
|
|
|
|
/// guarantee that the numerical value of a given `ItemLocalId` corresponds to
|
|
|
|
/// the node's position within the owning item in any way, but there is a
|
|
|
|
/// guarantee that the `LocalItemId`s within an owner occupy a dense range of
|
|
|
|
/// integers starting at zero, so a mapping that maps all or most nodes within
|
|
|
|
/// an "item-like" to something else can be implemented by a `Vec` instead of a
|
|
|
|
/// tree or hash map.
|
2022-04-19 10:43:09 +02:00
|
|
|
#[derive(HashStable_Generic)]
|
2019-12-25 15:26:30 +01:00
|
|
|
pub struct ItemLocalId { .. }
|
|
|
|
}
|
2022-04-19 10:43:09 +02:00
|
|
|
|
2021-10-12 08:34:38 +02:00
|
|
|
impl ItemLocalId {
|
|
|
|
/// Signal local id which should never be used.
|
|
|
|
pub const INVALID: ItemLocalId = ItemLocalId::MAX;
|
|
|
|
}
|
2019-12-25 15:26:30 +01:00
|
|
|
|
2022-04-15 19:27:53 +02:00
|
|
|
/// The `HirId` corresponding to `CRATE_NODE_ID` and `CRATE_DEF_ID`.
|
|
|
|
pub const CRATE_HIR_ID: HirId = HirId { owner: CRATE_DEF_ID, local_id: ItemLocalId::from_u32(0) };
|