]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_ast/src/node_id.rs
Auto merge of #106429 - djkoloski:add_vendor_to_fuchsia_target_triple, r=nagisa
[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     #[debug_format = "NodeId({})"]
12     pub struct NodeId {}
13 }
14
15 rustc_data_structures::define_id_collections!(NodeMap, NodeSet, NodeMapEntry, NodeId);
16
17 /// The [`NodeId`] used to represent the root of the crate.
18 pub const CRATE_NODE_ID: NodeId = NodeId::from_u32(0);
19
20 /// When parsing and at the beginning of doing expansions, we initially give all AST nodes
21 /// this dummy AST [`NodeId`]. Then, during a later phase of expansion, we renumber them
22 /// to have small, positive IDs.
23 pub const DUMMY_NODE_ID: NodeId = NodeId::MAX;
24
25 impl NodeId {
26     pub fn placeholder_from_expn_id(expn_id: LocalExpnId) -> Self {
27         NodeId::from_u32(expn_id.as_u32())
28     }
29
30     pub fn placeholder_to_expn_id(self) -> LocalExpnId {
31         LocalExpnId::from_u32(self.as_u32())
32     }
33 }
34
35 impl fmt::Display for NodeId {
36     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
37         fmt::Display::fmt(&self.as_u32(), f)
38     }
39 }