rustc_ast/
node_id.rs

1use std::fmt;
2
3use rustc_span::LocalExpnId;
4
5rustc_index::newtype_index! {
6    /// Identifies an AST node.
7    ///
8    /// This identifies top-level definitions, expressions, and everything in between.
9    /// This is later turned into [`DefId`] and `HirId` for the HIR.
10    ///
11    /// [`DefId`]: rustc_span::def_id::DefId
12    #[encodable]
13    #[orderable]
14    #[debug_format = "NodeId({})"]
15    pub struct NodeId {
16        /// The [`NodeId`] used to represent the root of the crate.
17        const CRATE_NODE_ID = 0;
18    }
19}
20
21rustc_data_structures::define_id_collections!(NodeMap, NodeSet, NodeMapEntry, NodeId);
22
23/// When parsing and at the beginning of doing expansions, we initially give all AST nodes
24/// this dummy AST [`NodeId`]. Then, during a later phase of expansion, we renumber them
25/// to have small, positive IDs.
26pub const DUMMY_NODE_ID: NodeId = NodeId::MAX;
27
28impl NodeId {
29    pub fn placeholder_from_expn_id(expn_id: LocalExpnId) -> Self {
30        NodeId::from_u32(expn_id.as_u32())
31    }
32
33    pub fn placeholder_to_expn_id(self) -> LocalExpnId {
34        LocalExpnId::from_u32(self.as_u32())
35    }
36}
37
38impl fmt::Display for NodeId {
39    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
40        fmt::Display::fmt(&self.as_u32(), f)
41    }
42}