]> git.lizzy.rs Git - rust.git/blob - src/librustc_ast/node_id.rs
Rework `rustc_serialize`
[rust.git] / src / librustc_ast / node_id.rs
1 use rustc_span::ExpnId;
2 use std::fmt;
3
4 rustc_index::newtype_index! {
5     pub struct NodeId {
6         DEBUG_FORMAT = "NodeId({})"
7     }
8 }
9
10 rustc_data_structures::define_id_collections!(NodeMap, NodeSet, NodeId);
11
12 /// `NodeId` used to represent the root of the crate.
13 pub const CRATE_NODE_ID: NodeId = NodeId::from_u32(0);
14
15 /// When parsing and doing expansions, we initially give all AST nodes this AST
16 /// node value. Then later, in the renumber pass, we renumber them to have
17 /// small, positive ids.
18 pub const DUMMY_NODE_ID: NodeId = NodeId::MAX;
19
20 impl NodeId {
21     pub fn placeholder_from_expn_id(expn_id: ExpnId) -> Self {
22         NodeId::from_u32(expn_id.as_u32())
23     }
24
25     pub fn placeholder_to_expn_id(self) -> ExpnId {
26         ExpnId::from_u32(self.as_u32())
27     }
28 }
29
30 impl fmt::Display for NodeId {
31     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
32         fmt::Display::fmt(&self.as_u32(), f)
33     }
34 }