]> git.lizzy.rs Git - rust.git/blob - src/librustc_ast/node_id.rs
Rollup merge of #69722 - estebank:negative-impl-span-ast, r=Centril
[rust.git] / src / librustc_ast / node_id.rs
1 use rustc_serialize::{Decoder, Encoder};
2 use rustc_span::ExpnId;
3 use std::fmt;
4
5 rustc_index::newtype_index! {
6     pub struct NodeId {
7         ENCODABLE = custom
8         DEBUG_FORMAT = "NodeId({})"
9     }
10 }
11
12 rustc_data_structures::define_id_collections!(NodeMap, NodeSet, NodeId);
13
14 /// `NodeId` used to represent the root of the crate.
15 pub const CRATE_NODE_ID: NodeId = NodeId::from_u32_const(0);
16
17 /// When parsing and doing expansions, we initially give all AST nodes this AST
18 /// node value. Then later, in the renumber pass, we renumber them to have
19 /// small, positive ids.
20 pub const DUMMY_NODE_ID: NodeId = NodeId::MAX;
21
22 impl NodeId {
23     pub fn placeholder_from_expn_id(expn_id: ExpnId) -> Self {
24         NodeId::from_u32(expn_id.as_u32())
25     }
26
27     pub fn placeholder_to_expn_id(self) -> ExpnId {
28         ExpnId::from_u32(self.as_u32())
29     }
30 }
31
32 impl fmt::Display for NodeId {
33     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
34         fmt::Display::fmt(&self.as_u32(), f)
35     }
36 }
37
38 impl rustc_serialize::UseSpecializedEncodable for NodeId {
39     fn default_encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
40         s.emit_u32(self.as_u32())
41     }
42 }
43
44 impl rustc_serialize::UseSpecializedDecodable for NodeId {
45     fn default_decode<D: Decoder>(d: &mut D) -> Result<NodeId, D::Error> {
46         d.read_u32().map(NodeId::from_u32)
47     }
48 }