2022-10-31 11:06:56 -05:00
|
|
|
use crate::def_id::{DefId, DefIndex, LocalDefId, CRATE_DEF_ID};
|
2022-12-02 08:14:49 -06:00
|
|
|
use rustc_data_structures::stable_hasher::{HashStable, StableHasher, StableOrd, ToStableHashKey};
|
2022-09-20 00:11:23 -05:00
|
|
|
use rustc_span::{def_id::DefPathHash, HashStableContext};
|
2022-12-11 05:10:55 -06:00
|
|
|
use std::fmt::{self, Debug};
|
2019-12-25 08:26:30 -06:00
|
|
|
|
2022-12-11 05:10:55 -06:00
|
|
|
#[derive(Copy, Clone, PartialEq, Eq, Hash)]
|
2022-09-20 00:11:23 -05:00
|
|
|
#[derive(Encodable, Decodable)]
|
|
|
|
pub struct OwnerId {
|
|
|
|
pub def_id: LocalDefId,
|
|
|
|
}
|
|
|
|
|
2022-12-11 05:10:55 -06:00
|
|
|
impl Debug for OwnerId {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
// Example: DefId(0:1 ~ aa[7697]::{use#0})
|
|
|
|
Debug::fmt(&self.def_id, f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-20 00:11:23 -05:00
|
|
|
impl From<OwnerId> for HirId {
|
|
|
|
fn from(owner: OwnerId) -> HirId {
|
|
|
|
HirId { owner, local_id: ItemLocalId::from_u32(0) }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-13 13:54:05 -05:00
|
|
|
impl From<OwnerId> for DefId {
|
|
|
|
fn from(value: OwnerId) -> Self {
|
|
|
|
value.to_def_id()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-20 00:11:23 -05:00
|
|
|
impl OwnerId {
|
|
|
|
#[inline]
|
|
|
|
pub fn to_def_id(self) -> DefId {
|
|
|
|
self.def_id.to_def_id()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-19 05:57:17 -05:00
|
|
|
impl rustc_index::Idx for OwnerId {
|
2022-10-31 11:06:56 -05:00
|
|
|
#[inline]
|
|
|
|
fn new(idx: usize) -> Self {
|
|
|
|
OwnerId { def_id: LocalDefId { local_def_index: DefIndex::from_usize(idx) } }
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn index(self) -> usize {
|
|
|
|
self.def_id.local_def_index.as_usize()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-20 00:11:23 -05:00
|
|
|
impl<CTX: HashStableContext> HashStable<CTX> for OwnerId {
|
|
|
|
#[inline]
|
|
|
|
fn hash_stable(&self, hcx: &mut CTX, hasher: &mut StableHasher) {
|
|
|
|
self.to_stable_hash_key(hcx).hash_stable(hcx, hasher);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<CTX: HashStableContext> ToStableHashKey<CTX> for OwnerId {
|
|
|
|
type KeyType = DefPathHash;
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn to_stable_hash_key(&self, hcx: &CTX) -> DefPathHash {
|
|
|
|
hcx.def_path_hash(self.to_def_id())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-25 08:26:30 -06:00
|
|
|
/// Uniquely identifies a node in the HIR of the current crate. It is
|
2020-03-18 13:27:59 -05:00
|
|
|
/// composed of the `owner`, which is the `LocalDefId` of the directly enclosing
|
2019-12-25 08:26:30 -06: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.
|
2022-12-11 05:10:55 -06:00
|
|
|
#[derive(Copy, Clone, PartialEq, Eq, Hash)]
|
2022-04-04 15:19:25 -05:00
|
|
|
#[derive(Encodable, Decodable, HashStable_Generic)]
|
2022-03-08 08:39:52 -06:00
|
|
|
#[rustc_pass_by_value]
|
2019-12-25 08:26:30 -06:00
|
|
|
pub struct HirId {
|
2022-09-20 00:11:23 -05:00
|
|
|
pub owner: OwnerId,
|
2019-12-25 08:26:30 -06:00
|
|
|
pub local_id: ItemLocalId,
|
|
|
|
}
|
|
|
|
|
2022-12-11 05:10:55 -06:00
|
|
|
impl Debug for HirId {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
// Example: HirId(DefId(0:1 ~ aa[7697]::{use#0}).10)
|
|
|
|
// Don't use debug_tuple to always keep this on one line.
|
|
|
|
write!(f, "HirId({:?}.{:?})", self.owner, self.local_id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-30 10:47:51 -06:00
|
|
|
impl HirId {
|
2022-08-30 01:54:42 -05:00
|
|
|
/// Signal local id which should never be used.
|
2022-09-20 00:11:23 -05:00
|
|
|
pub const INVALID: HirId =
|
|
|
|
HirId { owner: OwnerId { def_id: CRATE_DEF_ID }, local_id: ItemLocalId::INVALID };
|
2022-08-30 01:54:42 -05:00
|
|
|
|
2021-12-10 06:23:48 -06:00
|
|
|
#[inline]
|
2022-09-20 00:11:23 -05:00
|
|
|
pub fn expect_owner(self) -> OwnerId {
|
2021-01-30 10:47:51 -06:00
|
|
|
assert_eq!(self.local_id.index(), 0);
|
|
|
|
self.owner
|
|
|
|
}
|
|
|
|
|
2021-12-10 06:23:48 -06:00
|
|
|
#[inline]
|
2022-09-20 00:11:23 -05:00
|
|
|
pub fn as_owner(self) -> Option<OwnerId> {
|
2021-01-30 10:47:51 -06:00
|
|
|
if self.local_id.index() == 0 { Some(self.owner) } else { None }
|
|
|
|
}
|
|
|
|
|
2021-09-22 12:28:20 -05:00
|
|
|
#[inline]
|
|
|
|
pub fn is_owner(self) -> bool {
|
|
|
|
self.local_id.index() == 0
|
|
|
|
}
|
|
|
|
|
2021-02-25 18:00:00 -06:00
|
|
|
#[inline]
|
2021-01-30 10:47:51 -06:00
|
|
|
pub fn make_owner(owner: LocalDefId) -> Self {
|
2022-09-20 00:11:23 -05:00
|
|
|
Self { owner: OwnerId { def_id: owner }, local_id: ItemLocalId::from_u32(0) }
|
2021-01-30 10:47:51 -06:00
|
|
|
}
|
2021-10-30 10:11:50 -05:00
|
|
|
|
|
|
|
pub fn index(self) -> (usize, usize) {
|
2023-04-19 05:57:17 -05:00
|
|
|
(rustc_index::Idx::index(self.owner.def_id), rustc_index::Idx::index(self.local_id))
|
2021-10-30 10:11:50 -05:00
|
|
|
}
|
2021-01-30 10:47:51 -06:00
|
|
|
}
|
|
|
|
|
2019-12-25 08:26:30 -06:00
|
|
|
impl fmt::Display for HirId {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2022-12-19 03:31:55 -06:00
|
|
|
write!(f, "{self:?}")
|
2019-12-25 08:26:30 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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> {
|
2022-11-29 05:01:17 -06:00
|
|
|
Some(self.cmp(other))
|
2021-10-30 10:11:50 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-16 08:16:57 -05:00
|
|
|
rustc_data_structures::define_stable_id_collections!(HirIdMap, HirIdSet, HirIdMapEntry, HirId);
|
|
|
|
rustc_data_structures::define_id_collections!(
|
|
|
|
ItemLocalMap,
|
|
|
|
ItemLocalSet,
|
|
|
|
ItemLocalMapEntry,
|
|
|
|
ItemLocalId
|
|
|
|
);
|
2019-12-25 08:26:30 -06:00
|
|
|
|
|
|
|
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
|
2023-01-11 06:45:52 -06:00
|
|
|
/// guarantee that the `ItemLocalId`s within an owner occupy a dense range of
|
2019-12-25 08:26:30 -06:00
|
|
|
/// 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 03:43:09 -05:00
|
|
|
#[derive(HashStable_Generic)]
|
2022-12-18 14:47:28 -06:00
|
|
|
pub struct ItemLocalId {}
|
2019-12-25 08:26:30 -06:00
|
|
|
}
|
2022-04-19 03:43:09 -05:00
|
|
|
|
2021-10-12 01:34:38 -05:00
|
|
|
impl ItemLocalId {
|
|
|
|
/// Signal local id which should never be used.
|
|
|
|
pub const INVALID: ItemLocalId = ItemLocalId::MAX;
|
|
|
|
}
|
2019-12-25 08:26:30 -06:00
|
|
|
|
2023-01-11 06:45:52 -06:00
|
|
|
// Safety: Ord is implement as just comparing the ItemLocalId's numerical
|
2022-12-02 08:14:49 -06:00
|
|
|
// values and these are not changed by (de-)serialization.
|
2023-06-07 23:38:50 -05:00
|
|
|
unsafe impl StableOrd for ItemLocalId {
|
|
|
|
const CAN_USE_UNSTABLE_SORT: bool = true;
|
|
|
|
}
|
2022-12-02 08:14:49 -06:00
|
|
|
|
2022-04-15 12:27:53 -05:00
|
|
|
/// The `HirId` corresponding to `CRATE_NODE_ID` and `CRATE_DEF_ID`.
|
2022-09-20 00:11:23 -05:00
|
|
|
pub const CRATE_HIR_ID: HirId =
|
|
|
|
HirId { owner: OwnerId { def_id: CRATE_DEF_ID }, local_id: ItemLocalId::from_u32(0) };
|
|
|
|
|
|
|
|
pub const CRATE_OWNER_ID: OwnerId = OwnerId { def_id: CRATE_DEF_ID };
|