2021-06-25 20:43:04 +02:00
|
|
|
use rustc_span::LocalExpnId;
|
2019-12-22 17:42:04 -05:00
|
|
|
use std::fmt;
|
2019-11-29 15:41:22 -05:00
|
|
|
|
|
|
|
rustc_index::newtype_index! {
|
2021-01-09 17:44:10 -08:00
|
|
|
/// Identifies an AST node.
|
|
|
|
///
|
|
|
|
/// This identifies top-level definitions, expressions, and everything in between.
|
|
|
|
/// This is later turned into [`DefId`] and `HirId` for the HIR.
|
|
|
|
///
|
|
|
|
/// [`DefId`]: rustc_span::def_id::DefId
|
2019-11-29 15:41:22 -05:00
|
|
|
pub struct NodeId {
|
|
|
|
DEBUG_FORMAT = "NodeId({})"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-24 22:42:56 +01:00
|
|
|
rustc_data_structures::define_id_collections!(NodeMap, NodeSet, NodeId);
|
|
|
|
|
2021-01-09 17:44:10 -08:00
|
|
|
/// The [`NodeId`] used to represent the root of the crate.
|
2020-03-10 13:44:53 -07:00
|
|
|
pub const CRATE_NODE_ID: NodeId = NodeId::from_u32(0);
|
2020-01-11 15:03:15 +01:00
|
|
|
|
2021-02-07 19:42:12 -08:00
|
|
|
/// When parsing and at the beginning of doing expansions, we initially give all AST nodes
|
|
|
|
/// this dummy AST [`NodeId`]. Then, during a later phase of expansion, we renumber them
|
|
|
|
/// to have small, positive IDs.
|
2020-01-11 15:03:15 +01:00
|
|
|
pub const DUMMY_NODE_ID: NodeId = NodeId::MAX;
|
|
|
|
|
2019-11-29 15:41:22 -05:00
|
|
|
impl NodeId {
|
2021-06-25 20:43:04 +02:00
|
|
|
pub fn placeholder_from_expn_id(expn_id: LocalExpnId) -> Self {
|
|
|
|
NodeId::from_u32(expn_id.as_u32())
|
2019-11-29 15:41:22 -05:00
|
|
|
}
|
|
|
|
|
2021-06-25 20:43:04 +02:00
|
|
|
pub fn placeholder_to_expn_id(self) -> LocalExpnId {
|
|
|
|
LocalExpnId::from_u32(self.as_u32())
|
2019-11-29 15:41:22 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for NodeId {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
fmt::Display::fmt(&self.as_u32(), f)
|
|
|
|
}
|
|
|
|
}
|