leo_ast/common/
node.rs

1// Copyright (C) 2019-2025 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:
26    std::fmt::Debug + std::fmt::Display + Clone + PartialEq + Eq + serde::Serialize + serde::de::DeserializeOwned
27{
28    /// Returns the span of the node.
29    fn span(&self) -> Span;
30
31    /// Sets the span of the node.
32    fn set_span(&mut self, span: Span);
33
34    /// Returns the ID of the node.
35    fn id(&self) -> NodeID;
36
37    /// Sets the ID of the node.
38    fn set_id(&mut self, id: NodeID);
39}
40
41#[macro_export]
42macro_rules! simple_node_impl {
43    ($ty:ty) => {
44        impl Node for $ty {
45            fn span(&self) -> Span {
46                self.span
47            }
48
49            fn set_span(&mut self, span: Span) {
50                self.span = span;
51            }
52
53            fn id(&self) -> NodeID {
54                self.id
55            }
56
57            fn set_id(&mut self, id: NodeID) {
58                self.id = id;
59            }
60        }
61    };
62}