use crate::def_id::{DefId, DefIndex, LocalDefId, CRATE_DEF_ID};
use rustc_data_structures::stable_hasher::{HashStable, StableHasher, StableOrd, ToStableHashKey};
use rustc_macros::{Decodable, Encodable, HashStable_Generic};
use rustc_span::{def_id::DefPathHash, HashStableContext};
use std::fmt::{self, Debug};
#[derive(Copy, Clone, PartialEq, Eq, Hash, Encodable, Decodable)]
pub struct OwnerId {
pub def_id: LocalDefId,
}
impl Debug for OwnerId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
Debug::fmt(&self.def_id, f)
}
}
impl From<OwnerId> for HirId {
fn from(owner: OwnerId) -> HirId {
HirId { owner, local_id: ItemLocalId::ZERO }
}
}
impl From<OwnerId> for DefId {
fn from(value: OwnerId) -> Self {
value.to_def_id()
}
}
impl OwnerId {
#[inline]
pub fn to_def_id(self) -> DefId {
self.def_id.to_def_id()
}
}
impl rustc_index::Idx for OwnerId {
#[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()
}
}
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())
}
}
#[derive(Copy, Clone, PartialEq, Eq, Hash, Encodable, Decodable, HashStable_Generic)]
#[rustc_pass_by_value]
pub struct HirId {
pub owner: OwnerId,
pub local_id: ItemLocalId,
}
impl Debug for HirId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "HirId({:?}.{:?})", self.owner, self.local_id)
}
}
impl HirId {
pub const INVALID: HirId =
HirId { owner: OwnerId { def_id: CRATE_DEF_ID }, local_id: ItemLocalId::INVALID };
#[inline]
pub fn expect_owner(self) -> OwnerId {
assert_eq!(self.local_id.index(), 0);
self.owner
}
#[inline]
pub fn as_owner(self) -> Option<OwnerId> {
if self.local_id.index() == 0 { Some(self.owner) } else { None }
}
#[inline]
pub fn is_owner(self) -> bool {
self.local_id.index() == 0
}
#[inline]
pub fn make_owner(owner: LocalDefId) -> Self {
Self { owner: OwnerId { def_id: owner }, local_id: ItemLocalId::ZERO }
}
pub fn index(self) -> (usize, usize) {
(rustc_index::Idx::index(self.owner.def_id), rustc_index::Idx::index(self.local_id))
}
}
impl fmt::Display for HirId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{self:?}")
}
}
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))
}
}
rustc_data_structures::define_stable_id_collections!(HirIdMap, HirIdSet, HirIdMapEntry, HirId);
rustc_data_structures::define_id_collections!(
ItemLocalMap,
ItemLocalSet,
ItemLocalMapEntry,
ItemLocalId
);
rustc_index::newtype_index! {
#[derive(HashStable_Generic)]
#[encodable]
#[orderable]
pub struct ItemLocalId {}
}
impl ItemLocalId {
pub const INVALID: ItemLocalId = ItemLocalId::MAX;
}
impl StableOrd for ItemLocalId {
const CAN_USE_UNSTABLE_SORT: bool = true;
const THIS_IMPLEMENTATION_HAS_BEEN_TRIPLE_CHECKED: () = ();
}
pub const CRATE_HIR_ID: HirId =
HirId { owner: OwnerId { def_id: CRATE_DEF_ID }, local_id: ItemLocalId::ZERO };
pub const CRATE_OWNER_ID: OwnerId = OwnerId { def_id: CRATE_DEF_ID };