Skip to main content

leo_ast/common/
node.rs

1// Copyright (C) 2019-2026 Provable Inc.
2// This file is part of the Leo library.
3
4// The Leo library is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8
9// The Leo library is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13
14// You should have received a copy of the GNU General Public License
15// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
16
17use leo_span::Span;
18
19/// A node ID.
20// Development Note:
21// A `NodeID` must implement: `Copy`, `Default`, among others.
22pub type NodeID = usize;
23
24/// A node in the AST.
25pub trait Node: std::fmt::Debug + std::fmt::Display + Clone + PartialEq + Eq + serde::Serialize {
26    /// Returns the span of the node.
27    fn span(&self) -> Span;
28
29    /// Sets the span of the node.
30    fn set_span(&mut self, span: Span);
31
32    /// Returns the ID of the node.
33    fn id(&self) -> NodeID;
34
35    /// Sets the ID of the node.
36    fn set_id(&mut self, id: NodeID);
37}
38
39#[macro_export]
40macro_rules! simple_node_impl {
41    ($ty:ty) => {
42        impl Node for $ty {
43            fn span(&self) -> Span {
44                self.span
45            }
46
47            fn set_span(&mut self, span: Span) {
48                self.span = span;
49            }
50
51            fn id(&self) -> NodeID {
52                self.id
53            }
54
55            fn set_id(&mut self, id: NodeID) {
56                self.id = id;
57            }
58        }
59    };
60}