rust/compiler/rustc_ast/src/node_id.rs

41 lines
1.2 KiB
Rust
Raw Normal View History

2021-07-10 16:34:41 -05:00
use rustc_span::{ExpnId, LocalExpnId};
2019-12-22 16:42:04 -06:00
use std::fmt;
rustc_index::newtype_index! {
2021-01-09 19:44:10 -06: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
pub struct NodeId {
DEBUG_FORMAT = "NodeId({})"
}
}
rustc_data_structures::define_id_collections!(NodeMap, NodeSet, NodeId);
2021-01-09 19:44:10 -06:00
/// The [`NodeId`] used to represent the root of the crate.
2020-03-10 15:44:53 -05:00
pub const CRATE_NODE_ID: NodeId = NodeId::from_u32(0);
2021-02-07 21:42:12 -06: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.
pub const DUMMY_NODE_ID: NodeId = NodeId::MAX;
impl NodeId {
pub fn placeholder_from_expn_id(expn_id: ExpnId) -> Self {
2021-07-10 16:34:41 -05:00
NodeId::from_u32(expn_id.expect_local().as_u32())
}
pub fn placeholder_to_expn_id(self) -> ExpnId {
2021-07-10 16:34:41 -05:00
LocalExpnId::from_u32(self.as_u32()).to_expn_id()
}
}
impl fmt::Display for NodeId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(&self.as_u32(), f)
}
}