Add id field to AST nodes

This commit is contained in:
Pranav Gaddamadugu 2023-08-09 10:13:59 -04:00
parent 6ab280a935
commit 2f024c8346
37 changed files with 226 additions and 32 deletions

View File

@ -14,7 +14,7 @@
// You should have received a copy of the GNU General Public License
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
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 {

View File

@ -14,7 +14,7 @@
// You should have received a copy of the GNU General Public License
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
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<Expression>,
/// The span for the entire expression `Foo::bar()`.
pub span: Span,
/// The ID of the node.
pub id: NodeID,
}
impl fmt::Display for AssociatedFunction {

View File

@ -14,7 +14,7 @@
// You should have received a copy of the GNU General Public License
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
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 {

View File

@ -14,7 +14,7 @@
// You should have received a copy of the GNU General Public License
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
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 {

View File

@ -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);

View File

@ -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;
}
}

View File

@ -14,7 +14,7 @@
// You should have received a copy of the GNU General Public License
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
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),

View File

@ -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 {

View File

@ -28,6 +28,8 @@ pub struct CallExpression {
pub external: Option<Box<Expression>>,
/// Span of the entire call `function(arguments)`.
pub span: Span,
/// The ID of the node.
pub id: NodeID,
}
impl fmt::Display for CallExpression {

View File

@ -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 {

View File

@ -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 {

View File

@ -86,4 +86,12 @@ impl Node for Literal {
},
}
}
fn id(&self) -> NodeID {
todo!()
}
fn set_id(&mut self, id: NodeID) {
todo!()
}
}

View File

@ -14,7 +14,7 @@
// You should have received a copy of the GNU General Public License
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
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 {

View File

@ -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<Expression>,
/// The ID of the node.
pub id: NodeID,
}
impl fmt::Display for StructVariableInitializer {
@ -50,6 +52,8 @@ pub struct StructExpression {
pub members: Vec<StructVariableInitializer>,
/// A span from `name` to `}`.
pub span: Span,
/// The ID of the node.
pub id: NodeID,
}
impl StructExpression {

View File

@ -27,6 +27,8 @@ pub struct TernaryExpression {
pub if_false: Box<Expression>,
/// The span from `condition` to `if_false`.
pub span: Span,
/// The ID of the node.
pub id: NodeID,
}
impl fmt::Display for TernaryExpression {

View File

@ -26,6 +26,8 @@ pub struct TupleExpression {
pub elements: Vec<Expression>,
/// The span from `(` to `)`.
pub span: Span,
/// The ID of the node.
pub id: NodeID,
}
impl fmt::Display for TupleExpression {

View File

@ -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 {

View File

@ -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 {

View File

@ -14,7 +14,7 @@
// You should have received a copy of the GNU General Public License
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
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);

View File

@ -14,7 +14,7 @@
// You should have received a copy of the GNU General Public License
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
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 {

View File

@ -14,7 +14,7 @@
// You should have received a copy of the GNU General Public License
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
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 {

View File

@ -14,7 +14,7 @@
// You should have received a copy of the GNU General Public License
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
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 {

View File

@ -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<Finalize>,
/// The entire span of the function definition.
pub span: Span,
/// The ID of the node.
pub id: NodeID,
}
impl PartialEq for Function {

View File

@ -14,7 +14,7 @@
// You should have received a copy of the GNU General Public License
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
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 {

View File

@ -14,7 +14,7 @@
// You should have received a copy of the GNU General Public License
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
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 {

View File

@ -14,7 +14,7 @@
// You should have received a copy of the GNU General Public License
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
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 {

View File

@ -14,7 +14,7 @@
// You should have received a copy of the GNU General Public License
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
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 {

View File

@ -14,7 +14,7 @@
// You should have received a copy of the GNU General Public License
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
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<Statement>,
/// The span from `{` to `}`.
pub span: Span,
/// The ID of the node.
pub id: NodeID,
}
impl fmt::Display for Block {

View File

@ -14,7 +14,7 @@
// You should have received a copy of the GNU General Public License
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
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<Box<Statement>>,
/// 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 {

View File

@ -14,7 +14,7 @@
// You should have received a copy of the GNU General Public License
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
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 {

View File

@ -14,7 +14,7 @@
// You should have received a copy of the GNU General Public License
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
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 {

View File

@ -14,7 +14,7 @@
// You should have received a copy of the GNU General Public License
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
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 {

View File

@ -14,7 +14,7 @@
// You should have received a copy of the GNU General Public License
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
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 {

View File

@ -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),
}
}
}

View File

@ -14,7 +14,7 @@
// You should have received a copy of the GNU General Public License
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
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<Vec<Expression>>,
/// The span of `return expression` excluding the semicolon.
pub span: Span,
/// The ID of the node.
pub id: NodeID,
}
impl fmt::Display for ReturnStatement {

View File

@ -14,7 +14,7 @@
// You should have received a copy of the GNU General Public License
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
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 {

View File

@ -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 {