Skip to main content

rustc_ast/
node_id.rs

1use std::fmt;
2
3use rustc_data_structures::stable_hasher::{StableHash, StableHashCtxt, StableHasher};
4use rustc_span::LocalExpnId;
5
6impl ::std::fmt::Debug for NodeId {
    fn fmt(&self, fmt: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
        fmt.write_fmt(format_args!("NodeId({0})", self.as_u32()))
    }
}rustc_index::newtype_index! {
7    /// Identifies an AST node.
8    ///
9    /// This identifies top-level definitions, expressions, and everything in between.
10    /// This is later turned into [`DefId`] and `HirId` for the HIR.
11    ///
12    /// [`DefId`]: rustc_span::def_id::DefId
13    #[encodable]
14    #[orderable]
15    #[debug_format = "NodeId({})"]
16    pub struct NodeId {
17        /// The [`NodeId`] used to represent the root of the crate.
18        const CRATE_NODE_ID = 0;
19    }
20}
21
22impl StableHash for NodeId {
23    #[inline]
24    fn stable_hash<Hcx: StableHashCtxt>(&self, _: &mut Hcx, _: &mut StableHasher) {
25        // This impl is never called but is necessary for types implementing `StableHash` such as
26        // `MainDefinition` and `DocLinkResMap` (both of which occur in `ResolverGlobalCtxt`).
27        {
    ::core::panicking::panic_fmt(format_args!("Node IDs should not appear in incremental state"));
};panic!("Node IDs should not appear in incremental state");
28    }
29}
30
31pub type NodeMap<T> = ::rustc_data_structures::unord::UnordMap<NodeId, T>;
pub type NodeSet = ::rustc_data_structures::unord::UnordSet<NodeId>;
pub type NodeMapEntry<'a, T> =
    ::rustc_data_structures::fx::StdEntry<'a, NodeId, T>;rustc_data_structures::define_id_collections!(NodeMap, NodeSet, NodeMapEntry, NodeId);
32
33/// When parsing and at the beginning of doing expansions, we initially give all AST nodes
34/// this dummy AST [`NodeId`]. Then, during a later phase of expansion, we renumber them
35/// to have small, positive IDs.
36pub const DUMMY_NODE_ID: NodeId = NodeId::MAX;
37
38impl NodeId {
39    pub fn placeholder_from_expn_id(expn_id: LocalExpnId) -> Self {
40        NodeId::from_u32(expn_id.as_u32())
41    }
42
43    pub fn placeholder_to_expn_id(self) -> LocalExpnId {
44        LocalExpnId::from_u32(self.as_u32())
45    }
46}
47
48impl fmt::Display for NodeId {
49    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
50        fmt::Display::fmt(&self.as_u32(), f)
51    }
52}