From 2f024c83462598efa7c5f2a47ae95a4af0a70965 Mon Sep 17 00:00:00 2001 From: Pranav Gaddamadugu Date: Wed, 9 Aug 2023 10:13:59 -0400 Subject: [PATCH] Add id field to AST nodes --- .../src/access/associated_constant_access.rs | 4 +- .../src/access/associated_function_access.rs | 4 +- compiler/ast/src/access/member_access.rs | 4 +- compiler/ast/src/access/tuple_access.rs | 4 +- compiler/ast/src/common/identifier.rs | 4 +- compiler/ast/src/common/node.rs | 13 +++++-- compiler/ast/src/expressions/access.rs | 21 +++++++++- compiler/ast/src/expressions/binary.rs | 2 + compiler/ast/src/expressions/call.rs | 2 + compiler/ast/src/expressions/cast.rs | 2 + compiler/ast/src/expressions/err.rs | 2 + compiler/ast/src/expressions/literal.rs | 8 ++++ compiler/ast/src/expressions/mod.rs | 38 ++++++++++++++++++- compiler/ast/src/expressions/struct_init.rs | 4 ++ compiler/ast/src/expressions/ternary.rs | 2 + compiler/ast/src/expressions/tuple.rs | 2 + compiler/ast/src/expressions/unary.rs | 2 + compiler/ast/src/expressions/unit.rs | 2 + compiler/ast/src/functions/annotation.rs | 4 +- compiler/ast/src/functions/external.rs | 4 +- compiler/ast/src/functions/finalize.rs | 4 +- compiler/ast/src/functions/input.rs | 20 +++++++++- compiler/ast/src/functions/mod.rs | 4 +- compiler/ast/src/functions/output.rs | 20 +++++++++- compiler/ast/src/mapping/mod.rs | 4 +- compiler/ast/src/statement/assert.rs | 4 +- compiler/ast/src/statement/assign.rs | 4 +- compiler/ast/src/statement/block.rs | 4 +- compiler/ast/src/statement/conditional.rs | 4 +- .../statement/console/console_statement.rs | 4 +- compiler/ast/src/statement/definition/mod.rs | 4 +- compiler/ast/src/statement/expression.rs | 4 +- compiler/ast/src/statement/iteration.rs | 4 +- compiler/ast/src/statement/mod.rs | 34 ++++++++++++++++- compiler/ast/src/statement/return_.rs | 4 +- compiler/ast/src/struct/member.rs | 4 +- compiler/ast/src/struct/mod.rs | 4 +- 37 files changed, 226 insertions(+), 32 deletions(-) diff --git a/compiler/ast/src/access/associated_constant_access.rs b/compiler/ast/src/access/associated_constant_access.rs index f0dba58799..a0e0900fbb 100644 --- a/compiler/ast/src/access/associated_constant_access.rs +++ b/compiler/ast/src/access/associated_constant_access.rs @@ -14,7 +14,7 @@ // You should have received a copy of the GNU General Public License // along with the Leo library. If not, see . -use crate::{Identifier, Node, Type}; +use crate::{Identifier, Node, NodeID, Type}; use leo_span::Span; use serde::{Deserialize, Serialize}; @@ -29,6 +29,8 @@ pub struct AssociatedConstant { pub name: Identifier, /// The span for the entire expression `Foo::bar()`. pub span: Span, + /// The ID of the node. + pub id: NodeID, } impl fmt::Display for AssociatedConstant { diff --git a/compiler/ast/src/access/associated_function_access.rs b/compiler/ast/src/access/associated_function_access.rs index 6f98a8ac0d..3324d7c699 100644 --- a/compiler/ast/src/access/associated_function_access.rs +++ b/compiler/ast/src/access/associated_function_access.rs @@ -14,7 +14,7 @@ // You should have received a copy of the GNU General Public License // along with the Leo library. If not, see . -use crate::{Expression, Identifier, Node, Type}; +use crate::{Expression, Identifier, Node, NodeID, Type}; use leo_span::Span; use serde::{Deserialize, Serialize}; @@ -31,6 +31,8 @@ pub struct AssociatedFunction { pub arguments: Vec, /// The span for the entire expression `Foo::bar()`. pub span: Span, + /// The ID of the node. + pub id: NodeID, } impl fmt::Display for AssociatedFunction { diff --git a/compiler/ast/src/access/member_access.rs b/compiler/ast/src/access/member_access.rs index 25d14da86e..888c63a75b 100644 --- a/compiler/ast/src/access/member_access.rs +++ b/compiler/ast/src/access/member_access.rs @@ -14,7 +14,7 @@ // You should have received a copy of the GNU General Public License // along with the Leo library. If not, see . -use crate::{Expression, Identifier, Node}; +use crate::{Expression, Identifier, Node, NodeID}; use leo_span::Span; use serde::{Deserialize, Serialize}; @@ -29,6 +29,8 @@ pub struct MemberAccess { pub name: Identifier, /// The span covering all of `inner.name`. pub span: Span, + /// The ID of the node. + pub id: NodeID, } impl fmt::Display for MemberAccess { diff --git a/compiler/ast/src/access/tuple_access.rs b/compiler/ast/src/access/tuple_access.rs index 762b7350c5..4799dd6c7b 100644 --- a/compiler/ast/src/access/tuple_access.rs +++ b/compiler/ast/src/access/tuple_access.rs @@ -14,7 +14,7 @@ // You should have received a copy of the GNU General Public License // along with the Leo library. If not, see . -use crate::{Expression, Node, PositiveNumber}; +use crate::{Expression, Node, NodeID, PositiveNumber}; use leo_span::Span; use serde::{Deserialize, Serialize}; @@ -29,6 +29,8 @@ pub struct TupleAccess { pub index: PositiveNumber, /// The span for the entire expression `tuple.index`. pub span: Span, + /// The ID of the node. + pub id: NodeID, } impl fmt::Display for TupleAccess { diff --git a/compiler/ast/src/common/identifier.rs b/compiler/ast/src/common/identifier.rs index 03dd0ae659..a876a401c1 100644 --- a/compiler/ast/src/common/identifier.rs +++ b/compiler/ast/src/common/identifier.rs @@ -17,7 +17,7 @@ use leo_errors::Result; use leo_span::{Span, Symbol}; -use crate::{simple_node_impl, Node}; +use crate::{simple_node_impl, Node, NodeID}; use serde::{ de::{ Visitor, @@ -45,6 +45,8 @@ pub struct Identifier { pub name: Symbol, /// A span locating where the identifier occurred in the source. pub span: Span, + /// The ID of the node. + pub id: NodeID, } simple_node_impl!(Identifier); diff --git a/compiler/ast/src/common/node.rs b/compiler/ast/src/common/node.rs index d06318c9c3..a0a5080cc9 100644 --- a/compiler/ast/src/common/node.rs +++ b/compiler/ast/src/common/node.rs @@ -16,6 +16,11 @@ use leo_span::Span; +/// A node ID. +// Development Note: +// A `NodeID` must implement: `Copy`, `Default`, among others. +pub type NodeID = usize; + /// A node in the AST. pub trait Node: std::fmt::Debug + std::fmt::Display + Clone + PartialEq + Eq + serde::Serialize + serde::de::DeserializeOwned @@ -27,10 +32,10 @@ pub trait Node: fn set_span(&mut self, span: Span); /// Returns the ID of the node. - fn id(&self) -> usize; + fn id(&self) -> NodeID; /// Sets the ID of the node. - fn set_id(&mut self, id: usize); + fn set_id(&mut self, id: NodeID); } #[macro_export] @@ -45,11 +50,11 @@ macro_rules! simple_node_impl { self.span = span; } - fn id(&self) -> usize { + fn id(&self) -> NodeID { self.id } - fn set_id(&mut self, id: usize) { + fn set_id(&mut self, id: NodeID) { self.id = id; } } diff --git a/compiler/ast/src/expressions/access.rs b/compiler/ast/src/expressions/access.rs index d63f227170..6178100512 100644 --- a/compiler/ast/src/expressions/access.rs +++ b/compiler/ast/src/expressions/access.rs @@ -14,7 +14,7 @@ // You should have received a copy of the GNU General Public License // along with the Leo library. If not, see . -use crate::{access::*, Node}; +use crate::{access::*, Node, NodeID}; use leo_span::Span; use serde::{Deserialize, Serialize}; @@ -55,12 +55,29 @@ impl Node for AccessExpression { AccessExpression::Tuple(n) => n.set_span(span), } } + + fn id(&self) -> NodeID { + match self { + AccessExpression::AssociatedConstant(n) => n.id(), + AccessExpression::AssociatedFunction(n) => n.id(), + AccessExpression::Member(n) => n.id(), + AccessExpression::Tuple(n) => n.id(), + } + } + + fn set_id(&mut self, id: NodeID) { + match self { + AccessExpression::AssociatedConstant(n) => n.set_id(id), + AccessExpression::AssociatedFunction(n) => n.set_id(id), + AccessExpression::Member(n) => n.set_id(id), + AccessExpression::Tuple(n) => n.set_id(id), + } + } } impl fmt::Display for AccessExpression { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { use AccessExpression::*; - match self { AssociatedConstant(access) => access.fmt(f), AssociatedFunction(access) => access.fmt(f), diff --git a/compiler/ast/src/expressions/binary.rs b/compiler/ast/src/expressions/binary.rs index 565039c78d..7a50fc202f 100644 --- a/compiler/ast/src/expressions/binary.rs +++ b/compiler/ast/src/expressions/binary.rs @@ -171,6 +171,8 @@ pub struct BinaryExpression { pub op: BinaryOperation, /// The span from `left` to `right`. pub span: Span, + /// The ID of the expression. + pub id: NodeID, } impl fmt::Display for BinaryExpression { diff --git a/compiler/ast/src/expressions/call.rs b/compiler/ast/src/expressions/call.rs index d3672f243c..a46238c4e2 100644 --- a/compiler/ast/src/expressions/call.rs +++ b/compiler/ast/src/expressions/call.rs @@ -28,6 +28,8 @@ pub struct CallExpression { pub external: Option>, /// Span of the entire call `function(arguments)`. pub span: Span, + /// The ID of the node. + pub id: NodeID, } impl fmt::Display for CallExpression { diff --git a/compiler/ast/src/expressions/cast.rs b/compiler/ast/src/expressions/cast.rs index a39fc09474..59999805a8 100644 --- a/compiler/ast/src/expressions/cast.rs +++ b/compiler/ast/src/expressions/cast.rs @@ -27,6 +27,8 @@ pub struct CastExpression { pub type_: Type, /// Span of the entire cast `42u8 as u16`. pub span: Span, + /// The ID of the node. + pub id: NodeID, } impl fmt::Display for CastExpression { diff --git a/compiler/ast/src/expressions/err.rs b/compiler/ast/src/expressions/err.rs index 3c34abcc65..f3a30e96e7 100644 --- a/compiler/ast/src/expressions/err.rs +++ b/compiler/ast/src/expressions/err.rs @@ -21,6 +21,8 @@ use super::*; pub struct ErrExpression { /// The span of the invalid expression. pub span: Span, + /// The ID of the node. + pub id: NodeID, } impl fmt::Display for ErrExpression { diff --git a/compiler/ast/src/expressions/literal.rs b/compiler/ast/src/expressions/literal.rs index 6c15713eee..193919d180 100644 --- a/compiler/ast/src/expressions/literal.rs +++ b/compiler/ast/src/expressions/literal.rs @@ -86,4 +86,12 @@ impl Node for Literal { }, } } + + fn id(&self) -> NodeID { + todo!() + } + + fn set_id(&mut self, id: NodeID) { + todo!() + } } diff --git a/compiler/ast/src/expressions/mod.rs b/compiler/ast/src/expressions/mod.rs index b5f1b2b959..c396c8e9fc 100644 --- a/compiler/ast/src/expressions/mod.rs +++ b/compiler/ast/src/expressions/mod.rs @@ -14,7 +14,7 @@ // You should have received a copy of the GNU General Public License // along with the Leo library. If not, see . -use crate::{Identifier, Node}; +use crate::{Identifier, Node, NodeID}; use leo_span::Span; use serde::{Deserialize, Serialize}; @@ -119,6 +119,42 @@ impl Node for Expression { Unit(n) => n.set_span(span), } } + + fn id(&self) -> NodeID { + use Expression::*; + match self { + Access(n) => n.id(), + Binary(n) => n.id(), + Call(n) => n.id(), + Cast(n) => n.id(), + Struct(n) => n.id(), + Identifier(n) => n.id(), + Literal(n) => n.id(), + Err(n) => n.id(), + Ternary(n) => n.id(), + Tuple(n) => n.id(), + Unary(n) => n.id(), + Unit(n) => n.id(), + } + } + + fn set_id(&mut self, id: NodeID) { + use Expression::*; + match self { + Access(n) => n.set_id(id), + Binary(n) => n.set_id(id), + Call(n) => n.set_id(id), + Cast(n) => n.set_id(id), + Struct(n) => n.set_id(id), + Identifier(n) => n.set_id(id), + Literal(n) => n.set_id(id), + Err(n) => n.set_id(id), + Ternary(n) => n.set_id(id), + Tuple(n) => n.set_id(id), + Unary(n) => n.set_id(id), + Unit(n) => n.set_id(id), + } + } } impl fmt::Display for Expression { diff --git a/compiler/ast/src/expressions/struct_init.rs b/compiler/ast/src/expressions/struct_init.rs index 1bcc6f1179..a549de49aa 100644 --- a/compiler/ast/src/expressions/struct_init.rs +++ b/compiler/ast/src/expressions/struct_init.rs @@ -26,6 +26,8 @@ pub struct StructVariableInitializer { /// The expression to initialize the field with. /// When `None`, a binding, in scope, with the name will be used instead. pub expression: Option, + /// The ID of the node. + pub id: NodeID, } impl fmt::Display for StructVariableInitializer { @@ -50,6 +52,8 @@ pub struct StructExpression { pub members: Vec, /// A span from `name` to `}`. pub span: Span, + /// The ID of the node. + pub id: NodeID, } impl StructExpression { diff --git a/compiler/ast/src/expressions/ternary.rs b/compiler/ast/src/expressions/ternary.rs index f4420b4162..44c066d812 100644 --- a/compiler/ast/src/expressions/ternary.rs +++ b/compiler/ast/src/expressions/ternary.rs @@ -27,6 +27,8 @@ pub struct TernaryExpression { pub if_false: Box, /// The span from `condition` to `if_false`. pub span: Span, + /// The ID of the node. + pub id: NodeID, } impl fmt::Display for TernaryExpression { diff --git a/compiler/ast/src/expressions/tuple.rs b/compiler/ast/src/expressions/tuple.rs index a31970bdc7..bc9b47baa9 100644 --- a/compiler/ast/src/expressions/tuple.rs +++ b/compiler/ast/src/expressions/tuple.rs @@ -26,6 +26,8 @@ pub struct TupleExpression { pub elements: Vec, /// The span from `(` to `)`. pub span: Span, + /// The ID of the node. + pub id: NodeID, } impl fmt::Display for TupleExpression { diff --git a/compiler/ast/src/expressions/unary.rs b/compiler/ast/src/expressions/unary.rs index 0355179f57..8ed413c982 100644 --- a/compiler/ast/src/expressions/unary.rs +++ b/compiler/ast/src/expressions/unary.rs @@ -86,6 +86,8 @@ pub struct UnaryExpression { pub op: UnaryOperation, /// The span covering `op inner`. pub span: Span, + /// The ID of the node. + pub id: NodeID, } impl fmt::Display for UnaryExpression { diff --git a/compiler/ast/src/expressions/unit.rs b/compiler/ast/src/expressions/unit.rs index 4994d3ee3d..82d27c35de 100644 --- a/compiler/ast/src/expressions/unit.rs +++ b/compiler/ast/src/expressions/unit.rs @@ -21,6 +21,8 @@ use super::*; pub struct UnitExpression { /// The span of the unit expression. pub span: Span, + /// The ID of the node. + pub id: NodeID, } impl fmt::Display for UnitExpression { diff --git a/compiler/ast/src/functions/annotation.rs b/compiler/ast/src/functions/annotation.rs index f8f8aab36f..bac22b8d69 100644 --- a/compiler/ast/src/functions/annotation.rs +++ b/compiler/ast/src/functions/annotation.rs @@ -14,7 +14,7 @@ // You should have received a copy of the GNU General Public License // along with the Leo library. If not, see . -use crate::{simple_node_impl, Identifier, Node}; +use crate::{simple_node_impl, Identifier, Node, NodeID}; use leo_span::Span; @@ -29,6 +29,8 @@ pub struct Annotation { pub identifier: Identifier, /// A span locating where the annotation occurred in the source. pub span: Span, + /// The ID of the node. + pub id: NodeID, } simple_node_impl!(Annotation); diff --git a/compiler/ast/src/functions/external.rs b/compiler/ast/src/functions/external.rs index faa9f08a80..c77bca6b3c 100644 --- a/compiler/ast/src/functions/external.rs +++ b/compiler/ast/src/functions/external.rs @@ -14,7 +14,7 @@ // You should have received a copy of the GNU General Public License // along with the Leo library. If not, see . -use crate::{Identifier, Node, Type}; +use crate::{Identifier, Node, NodeID, Type}; use leo_span::Span; use serde::{Deserialize, Serialize}; @@ -31,6 +31,8 @@ pub struct External { pub record: Identifier, /// The parameters span from any annotations to its type. pub span: Span, + /// The ID of the node. + pub id: NodeID, } impl External { diff --git a/compiler/ast/src/functions/finalize.rs b/compiler/ast/src/functions/finalize.rs index 135929a0eb..878f4f23ef 100644 --- a/compiler/ast/src/functions/finalize.rs +++ b/compiler/ast/src/functions/finalize.rs @@ -14,7 +14,7 @@ // You should have received a copy of the GNU General Public License // along with the Leo library. If not, see . -use crate::{Block, Identifier, Input, Node, Output, Tuple, Type}; +use crate::{Block, Identifier, Input, Node, NodeID, Output, Tuple, Type}; use leo_span::Span; @@ -36,6 +36,8 @@ pub struct Finalize { pub block: Block, /// The entire span of the finalize block. pub span: Span, + /// The ID of the node. + pub id: NodeID, } impl Finalize { diff --git a/compiler/ast/src/functions/input.rs b/compiler/ast/src/functions/input.rs index dcec2928ea..8a6aba8c23 100644 --- a/compiler/ast/src/functions/input.rs +++ b/compiler/ast/src/functions/input.rs @@ -14,7 +14,7 @@ // You should have received a copy of the GNU General Public License // along with the Leo library. If not, see . -use crate::{External, Identifier, Mode, Node, Type}; +use crate::{External, Identifier, Mode, Node, NodeID, Type}; use leo_span::Span; use serde::{Deserialize, Serialize}; @@ -78,6 +78,22 @@ impl Node for Input { External(input) => input.set_span(span), } } + + fn id(&self) -> usize { + use Input::*; + match self { + Internal(input) => input.id(), + External(input) => input.id(), + } + } + + fn set_id(&mut self, id: usize) { + use Input::*; + match self { + Internal(input) => input.set_id(id), + External(input) => input.set_id(id), + } + } } /// A function parameter. @@ -91,6 +107,8 @@ pub struct FunctionInput { pub type_: Type, /// The parameters span from any annotations to its type. pub span: Span, + /// The ID of the node. + pub id: NodeID, } impl FunctionInput { diff --git a/compiler/ast/src/functions/mod.rs b/compiler/ast/src/functions/mod.rs index 15d29c27a8..c823f53400 100644 --- a/compiler/ast/src/functions/mod.rs +++ b/compiler/ast/src/functions/mod.rs @@ -38,7 +38,7 @@ pub use output::*; pub mod mode; pub use mode::*; -use crate::{Block, Identifier, Node, Tuple, Type}; +use crate::{Block, Identifier, Node, NodeID, Tuple, Type}; use leo_span::{sym, Span, Symbol}; use serde::{Deserialize, Serialize}; @@ -65,6 +65,8 @@ pub struct Function { pub finalize: Option, /// The entire span of the function definition. pub span: Span, + /// The ID of the node. + pub id: NodeID, } impl PartialEq for Function { diff --git a/compiler/ast/src/functions/output.rs b/compiler/ast/src/functions/output.rs index 35ca5a8baa..7357b075c9 100644 --- a/compiler/ast/src/functions/output.rs +++ b/compiler/ast/src/functions/output.rs @@ -14,7 +14,7 @@ // You should have received a copy of the GNU General Public License // along with the Leo library. If not, see . -use crate::{External, Mode, Node, Type}; +use crate::{External, Mode, Node, NodeID, Type}; use leo_span::Span; use serde::{Deserialize, Serialize}; @@ -68,6 +68,22 @@ impl Node for Output { External(output) => output.set_span(span), } } + + fn id(&self) -> NodeID { + use Output::*; + match self { + Internal(output) => output.id(), + External(output) => output.id(), + } + } + + fn set_id(&mut self, id: NodeID) { + use Output::*; + match self { + Internal(output) => output.set_id(id), + External(output) => output.set_id(id), + } + } } /// A function output. @@ -79,6 +95,8 @@ pub struct FunctionOutput { pub type_: Type, /// The parameters span from any annotations to its type. pub span: Span, + /// The ID of the node. + pub id: NodeID, } impl fmt::Display for FunctionOutput { diff --git a/compiler/ast/src/mapping/mod.rs b/compiler/ast/src/mapping/mod.rs index 9212a8f55b..6ca05fac57 100644 --- a/compiler/ast/src/mapping/mod.rs +++ b/compiler/ast/src/mapping/mod.rs @@ -14,7 +14,7 @@ // You should have received a copy of the GNU General Public License // along with the Leo library. If not, see . -use crate::{Identifier, Node, Type}; +use crate::{Identifier, Node, NodeID, Type}; use leo_span::Span; @@ -32,6 +32,8 @@ pub struct Mapping { pub value_type: Type, /// The entire span of the mapping declaration. pub span: Span, + /// The ID of the node. + pub id: NodeID, } impl fmt::Display for Mapping { diff --git a/compiler/ast/src/statement/assert.rs b/compiler/ast/src/statement/assert.rs index 7c827cb04b..b00b8e8a15 100644 --- a/compiler/ast/src/statement/assert.rs +++ b/compiler/ast/src/statement/assert.rs @@ -14,7 +14,7 @@ // You should have received a copy of the GNU General Public License // along with the Leo library. If not, see . -use crate::{Expression, Node}; +use crate::{Expression, Node, NodeID}; use leo_span::Span; @@ -39,6 +39,8 @@ pub struct AssertStatement { pub variant: AssertVariant, /// The span, excluding the semicolon. pub span: Span, + /// The ID of the node. + pub id: NodeID, } impl fmt::Display for AssertStatement { diff --git a/compiler/ast/src/statement/assign.rs b/compiler/ast/src/statement/assign.rs index 6b9225c1a3..748b453074 100644 --- a/compiler/ast/src/statement/assign.rs +++ b/compiler/ast/src/statement/assign.rs @@ -14,7 +14,7 @@ // You should have received a copy of the GNU General Public License // along with the Leo library. If not, see . -use crate::{Expression, Node}; +use crate::{Expression, Node, NodeID}; use leo_span::Span; use serde::{Deserialize, Serialize}; @@ -31,6 +31,8 @@ pub struct AssignStatement { pub value: Expression, /// The span, excluding the semicolon. pub span: Span, + /// The ID of the node. + pub id: NodeID, } impl fmt::Display for AssignStatement { diff --git a/compiler/ast/src/statement/block.rs b/compiler/ast/src/statement/block.rs index 15b959e3db..e57f5821d9 100644 --- a/compiler/ast/src/statement/block.rs +++ b/compiler/ast/src/statement/block.rs @@ -14,7 +14,7 @@ // You should have received a copy of the GNU General Public License // along with the Leo library. If not, see . -use crate::{Node, Statement}; +use crate::{Node, NodeID, Statement}; use leo_span::Span; use serde::{Deserialize, Serialize}; @@ -27,6 +27,8 @@ pub struct Block { pub statements: Vec, /// The span from `{` to `}`. pub span: Span, + /// The ID of the node. + pub id: NodeID, } impl fmt::Display for Block { diff --git a/compiler/ast/src/statement/conditional.rs b/compiler/ast/src/statement/conditional.rs index eaee3a5c46..773bf5d761 100644 --- a/compiler/ast/src/statement/conditional.rs +++ b/compiler/ast/src/statement/conditional.rs @@ -14,7 +14,7 @@ // You should have received a copy of the GNU General Public License // along with the Leo library. If not, see . -use crate::{Block, Expression, Node, Statement}; +use crate::{Block, Expression, Node, NodeID, Statement}; use leo_span::Span; use serde::{Deserialize, Serialize}; @@ -31,6 +31,8 @@ pub struct ConditionalStatement { pub otherwise: Option>, /// The span from `if` to `next` or to `block`. pub span: Span, + /// The ID of the node. + pub id: NodeID, } impl fmt::Display for ConditionalStatement { diff --git a/compiler/ast/src/statement/console/console_statement.rs b/compiler/ast/src/statement/console/console_statement.rs index fc8c67d6e0..f305510a32 100644 --- a/compiler/ast/src/statement/console/console_statement.rs +++ b/compiler/ast/src/statement/console/console_statement.rs @@ -14,7 +14,7 @@ // You should have received a copy of the GNU General Public License // along with the Leo library. If not, see . -use crate::{ConsoleFunction, Node}; +use crate::{ConsoleFunction, Node, NodeID}; use leo_span::Span; use serde::{Deserialize, Serialize}; @@ -27,6 +27,8 @@ pub struct ConsoleStatement { pub function: ConsoleFunction, /// The span excluding the semicolon. pub span: Span, + /// The ID of the node. + pub id: NodeID, } impl fmt::Display for ConsoleStatement { diff --git a/compiler/ast/src/statement/definition/mod.rs b/compiler/ast/src/statement/definition/mod.rs index 5367f68000..aaf67a5202 100644 --- a/compiler/ast/src/statement/definition/mod.rs +++ b/compiler/ast/src/statement/definition/mod.rs @@ -14,7 +14,7 @@ // You should have received a copy of the GNU General Public License // along with the Leo library. If not, see . -use crate::{Expression, Node, Type}; +use crate::{Expression, Node, NodeID, Type}; use leo_span::Span; use serde::{Deserialize, Serialize}; @@ -36,6 +36,8 @@ pub struct DefinitionStatement { pub value: Expression, /// The span excluding the semicolon. pub span: Span, + /// The ID of the node. + pub id: NodeID, } impl fmt::Display for DefinitionStatement { diff --git a/compiler/ast/src/statement/expression.rs b/compiler/ast/src/statement/expression.rs index 8b4192bb19..8a8c6dfcb2 100644 --- a/compiler/ast/src/statement/expression.rs +++ b/compiler/ast/src/statement/expression.rs @@ -14,7 +14,7 @@ // You should have received a copy of the GNU General Public License // along with the Leo library. If not, see . -use crate::{Expression, Node}; +use crate::{Expression, Node, NodeID}; use leo_span::Span; use serde::{Deserialize, Serialize}; @@ -27,6 +27,8 @@ pub struct ExpressionStatement { pub expression: Expression, /// The span. pub span: Span, + /// The ID of the node. + pub id: NodeID, } impl fmt::Display for ExpressionStatement { diff --git a/compiler/ast/src/statement/iteration.rs b/compiler/ast/src/statement/iteration.rs index b79cae2a19..7982fa1ae9 100644 --- a/compiler/ast/src/statement/iteration.rs +++ b/compiler/ast/src/statement/iteration.rs @@ -14,7 +14,7 @@ // You should have received a copy of the GNU General Public License // along with the Leo library. If not, see . -use crate::{Block, Expression, Identifier, Node, Type, Value}; +use crate::{Block, Expression, Identifier, Node, NodeID, Type, Value}; use leo_span::Span; @@ -45,6 +45,8 @@ pub struct IterationStatement { pub block: Block, /// The span from `for` to `block`. pub span: Span, + /// The ID of the node. + pub id: NodeID, } impl fmt::Display for IterationStatement { diff --git a/compiler/ast/src/statement/mod.rs b/compiler/ast/src/statement/mod.rs index 972967e9c3..a982a11d41 100644 --- a/compiler/ast/src/statement/mod.rs +++ b/compiler/ast/src/statement/mod.rs @@ -41,7 +41,7 @@ pub use iteration::*; pub mod return_; pub use return_::*; -use crate::Node; +use crate::{Node, NodeID}; use leo_span::Span; @@ -74,7 +74,7 @@ pub enum Statement { impl Statement { /// Returns a dummy statement made from an empty block `{}`. pub fn dummy(span: Span) -> Self { - Self::Block(Block { statements: Vec::new(), span }) + Self::Block(Block { statements: Vec::new(), span, id: NodeID::default() }) } } @@ -124,4 +124,34 @@ impl Node for Statement { Return(n) => n.set_span(span), } } + + fn id(&self) -> NodeID { + use Statement::*; + match self { + Assert(n) => n.id(), + Assign(n) => n.id(), + Block(n) => n.id(), + Conditional(n) => n.id(), + Console(n) => n.id(), + Definition(n) => n.id(), + Expression(n) => n.id(), + Iteration(n) => n.id(), + Return(n) => n.id(), + } + } + + fn set_id(&mut self, id: NodeID) { + use Statement::*; + match self { + Assert(n) => n.set_id(id), + Assign(n) => n.set_id(id), + Block(n) => n.set_id(id), + Conditional(n) => n.set_id(id), + Console(n) => n.set_id(id), + Definition(n) => n.set_id(id), + Expression(n) => n.set_id(id), + Iteration(n) => n.set_id(id), + Return(n) => n.set_id(id), + } + } } diff --git a/compiler/ast/src/statement/return_.rs b/compiler/ast/src/statement/return_.rs index 8cd55f5c3b..307f04264c 100644 --- a/compiler/ast/src/statement/return_.rs +++ b/compiler/ast/src/statement/return_.rs @@ -14,7 +14,7 @@ // You should have received a copy of the GNU General Public License // along with the Leo library. If not, see . -use crate::{Expression, Node}; +use crate::{Expression, Node, NodeID}; use leo_span::Span; use serde::{Deserialize, Serialize}; @@ -29,6 +29,8 @@ pub struct ReturnStatement { pub finalize_arguments: Option>, /// The span of `return expression` excluding the semicolon. pub span: Span, + /// The ID of the node. + pub id: NodeID, } impl fmt::Display for ReturnStatement { diff --git a/compiler/ast/src/struct/member.rs b/compiler/ast/src/struct/member.rs index 1826d9a832..874fa0236a 100644 --- a/compiler/ast/src/struct/member.rs +++ b/compiler/ast/src/struct/member.rs @@ -14,7 +14,7 @@ // You should have received a copy of the GNU General Public License // along with the Leo library. If not, see . -use crate::{Identifier, Mode, Node, Type}; +use crate::{Identifier, Mode, Node, NodeID, Type}; use leo_span::{Span, Symbol}; @@ -32,6 +32,8 @@ pub struct Member { pub type_: Type, /// The span of the member. pub span: Span, + /// The ID of the node. + pub id: NodeID, } impl Member { diff --git a/compiler/ast/src/struct/mod.rs b/compiler/ast/src/struct/mod.rs index 5b591b8b69..bec8684036 100644 --- a/compiler/ast/src/struct/mod.rs +++ b/compiler/ast/src/struct/mod.rs @@ -17,7 +17,7 @@ pub mod member; pub use member::*; -use crate::{Identifier, Node}; +use crate::{Identifier, Node, NodeID}; use leo_span::{Span, Symbol}; use serde::{Deserialize, Serialize}; @@ -40,6 +40,8 @@ pub struct Struct { pub is_record: bool, /// The entire span of the struct definition. pub span: Span, + /// The ID of the node. + pub id: NodeID, } impl PartialEq for Struct {