use serde::{Deserialize, Serialize};
#[cfg(feature = "rustc")]
use crate::{
rustc::{def_id, hir, mir},
rustc_impls::*,
};
macro_rules! proxy_struct {
($(
$(#[$attr:meta])*
$name:ident($rustc:expr) {
$($field:ident : $rustc_ty:ty => $proxy_ty:ty , $proxy_str:expr),*
}
)*) => {
$(
#[derive(PartialEq, Eq, Hash, Clone, Copy, Debug, Serialize, Deserialize)]
#[cfg_attr(feature = "rustc", serde(remote = $rustc))]
$(#[$attr])*
pub struct $name {
$(
#[cfg(feature = "rustc")]
#[serde(with = $proxy_str)]
pub $field: $rustc_ty,
#[cfg(not(feature = "rustc"))]
pub $field: $proxy_ty,
)*
}
)*
}
}
macro_rules! proxy_index {
($(
$(#[$attr:meta])*
$name:ident($rustc:expr) from $fn:expr
);*) => {
$(
#[derive(PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Copy, Debug, Serialize, Deserialize)]
#[cfg_attr(feature = "rustc", serde(remote = $rustc))]
$(#[$attr])*
pub struct $name {
#[cfg_attr(feature = "rustc", serde(getter = $fn))]
pub(crate) private: u32
}
#[cfg(not(feature = "rustc"))]
impl $name {
pub fn index(self) -> usize {
self.private as usize
}
}
)*
}
}
proxy_index! {
BasicBlock("mir::BasicBlock") from "bbref_to_u32";
ItemLocalId("hir::ItemLocalId") from "item_local_id_as_u32";
DefIndex("def_id::DefIndex") from "def_index_as_u32";
CrateNum("hir::def_id::CrateNum") from "crate_num_as_u32"
}
proxy_struct! {
#[derive(PartialOrd, Ord)]
Location("mir::Location") {
block: mir::BasicBlock => BasicBlock, "BasicBlock",
statement_index: usize => usize, "usize"
}
LocalDefId("def_id::LocalDefId") {
local_def_index: def_id::DefIndex => DefIndex, "DefIndex"
}
OwnerId("hir::hir_id::OwnerId") {
def_id: def_id::LocalDefId => LocalDefId, "LocalDefId"
}
HirId("hir::HirId") {
owner: hir::OwnerId => OwnerId, "OwnerId",
local_id: hir::ItemLocalId => ItemLocalId, "ItemLocalId"
}
BodyId("hir::BodyId") {
hir_id: hir::HirId => HirId, "HirId"
}
#[derive(Ord, PartialOrd)]
DefId("def_id::DefId") {
index: def_id::DefIndex => DefIndex, "DefIndex",
krate: hir::def_id::CrateNum => CrateNum, "CrateNum"
}
}
impl HirId {
fn index(self) -> (usize, usize) {
(
self.owner.def_id.local_def_index.index(),
self.local_id.index(),
)
}
}
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))
}
}
#[cfg(not(feature = "rustc"))]
pub(crate) const LOCAL_CRATE: CrateNum = CrateNum { private: 0 };
impl LocalDefId {
#[cfg(not(feature = "rustc"))]
pub fn to_def_id(self) -> DefId {
DefId {
index: self.local_def_index,
krate: LOCAL_CRATE,
}
}
}