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