diff options
| author | Mazdak Farrokhzad <twingoow@gmail.com> | 2020-01-11 15:03:15 +0100 |
|---|---|---|
| committer | Mazdak Farrokhzad <twingoow@gmail.com> | 2020-02-01 18:58:08 +0100 |
| commit | 097d5e1c5edea1c0bf350b709087ddf5d60d2d9f (patch) | |
| tree | 5c1555c8d346774cab0028a3f3d73a78a8d7b9f5 /src/libsyntax/node_id.rs | |
| parent | 93a8283614e995a0cf7a866356609b7522cfda24 (diff) | |
| download | rust-097d5e1c5edea1c0bf350b709087ddf5d60d2d9f.tar.gz rust-097d5e1c5edea1c0bf350b709087ddf5d60d2d9f.zip | |
1. move node_id to syntax
2. invert rustc_session & syntax deps 3. drop rustc_session dep in rustc_hir
Diffstat (limited to 'src/libsyntax/node_id.rs')
| -rw-r--r-- | src/libsyntax/node_id.rs | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/src/libsyntax/node_id.rs b/src/libsyntax/node_id.rs new file mode 100644 index 00000000000..58d2334a7b1 --- /dev/null +++ b/src/libsyntax/node_id.rs @@ -0,0 +1,48 @@ +use rustc_serialize::{Decoder, Encoder}; +use rustc_span::ExpnId; +use std::fmt; + +rustc_index::newtype_index! { + pub struct NodeId { + ENCODABLE = custom + DEBUG_FORMAT = "NodeId({})" + } +} + +rustc_data_structures::define_id_collections!(NodeMap, NodeSet, NodeId); + +/// `NodeId` used to represent the root of the crate. +pub const CRATE_NODE_ID: NodeId = NodeId::from_u32_const(0); + +/// When parsing and doing expansions, we initially give all AST nodes this AST +/// node value. Then later, in the renumber pass, we renumber them to have +/// small, positive ids. +pub const DUMMY_NODE_ID: NodeId = NodeId::MAX; + +impl NodeId { + pub fn placeholder_from_expn_id(expn_id: ExpnId) -> Self { + NodeId::from_u32(expn_id.as_u32()) + } + + pub fn placeholder_to_expn_id(self) -> ExpnId { + ExpnId::from_u32(self.as_u32()) + } +} + +impl fmt::Display for NodeId { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + fmt::Display::fmt(&self.as_u32(), f) + } +} + +impl rustc_serialize::UseSpecializedEncodable for NodeId { + fn default_encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> { + s.emit_u32(self.as_u32()) + } +} + +impl rustc_serialize::UseSpecializedDecodable for NodeId { + fn default_decode<D: Decoder>(d: &mut D) -> Result<NodeId, D::Error> { + d.read_u32().map(NodeId::from_u32) + } +} |
