From 060f57d97147b816f5c8ee07dea09e9477e4702a Mon Sep 17 00:00:00 2001 From: d0cd Date: Fri, 14 Oct 2022 21:34:56 -0400 Subject: [PATCH] Remove standalone finalize statement from the AST --- compiler/ast/src/passes/consumer.rs | 3 -- compiler/ast/src/passes/reconstructor.rs | 21 ++++------- compiler/ast/src/passes/visitor.rs | 12 +++---- compiler/ast/src/statement/finalize.rs | 46 ------------------------ compiler/ast/src/statement/mod.rs | 8 ----- compiler/ast/src/statement/return_.rs | 2 ++ 6 files changed, 13 insertions(+), 79 deletions(-) delete mode 100644 compiler/ast/src/statement/finalize.rs diff --git a/compiler/ast/src/passes/consumer.rs b/compiler/ast/src/passes/consumer.rs index ae5d5230b2..bc7369d268 100644 --- a/compiler/ast/src/passes/consumer.rs +++ b/compiler/ast/src/passes/consumer.rs @@ -77,7 +77,6 @@ pub trait StatementConsumer { Statement::Decrement(stmt) => self.consume_decrement(stmt), Statement::Definition(stmt) => self.consume_definition(stmt), Statement::Expression(stmt) => self.consume_expression_statement(stmt), - Statement::Finalize(stmt) => self.consume_finalize(stmt), Statement::Increment(stmt) => self.consume_increment(stmt), Statement::Iteration(stmt) => self.consume_iteration(*stmt), Statement::Return(stmt) => self.consume_return(stmt), @@ -98,8 +97,6 @@ pub trait StatementConsumer { fn consume_expression_statement(&mut self, input: ExpressionStatement) -> Self::Output; - fn consume_finalize(&mut self, input: FinalizeStatement) -> Self::Output; - fn consume_increment(&mut self, input: IncrementStatement) -> Self::Output; fn consume_iteration(&mut self, input: IterationStatement) -> Self::Output; diff --git a/compiler/ast/src/passes/reconstructor.rs b/compiler/ast/src/passes/reconstructor.rs index 123c0510ae..cf69557a6e 100644 --- a/compiler/ast/src/passes/reconstructor.rs +++ b/compiler/ast/src/passes/reconstructor.rs @@ -171,7 +171,6 @@ pub trait StatementReconstructor: ExpressionReconstructor { Statement::Decrement(stmt) => self.reconstruct_decrement(stmt), Statement::Definition(stmt) => self.reconstruct_definition(stmt), Statement::Expression(stmt) => self.reconstruct_expression_statement(stmt), - Statement::Finalize(stmt) => self.reconstruct_finalize(stmt), Statement::Increment(stmt) => self.reconstruct_increment(stmt), Statement::Iteration(stmt) => self.reconstruct_iteration(*stmt), Statement::Return(stmt) => self.reconstruct_return(stmt), @@ -270,20 +269,6 @@ pub trait StatementReconstructor: ExpressionReconstructor { ) } - fn reconstruct_finalize(&mut self, input: FinalizeStatement) -> (Statement, Self::AdditionalOutput) { - ( - Statement::Finalize(FinalizeStatement { - arguments: input - .arguments - .into_iter() - .map(|arg| self.reconstruct_expression(arg).0) - .collect(), - span: input.span, - }), - Default::default(), - ) - } - fn reconstruct_increment(&mut self, input: IncrementStatement) -> (Statement, Self::AdditionalOutput) { ( Statement::Increment(IncrementStatement { @@ -317,6 +302,12 @@ pub trait StatementReconstructor: ExpressionReconstructor { ( Statement::Return(ReturnStatement { expression: self.reconstruct_expression(input.expression).0, + finalize_args: input.finalize_args.map(|arguments| { + arguments + .into_iter() + .map(|argument| self.reconstruct_expression(argument).0) + .collect() + }), span: input.span, }), Default::default(), diff --git a/compiler/ast/src/passes/visitor.rs b/compiler/ast/src/passes/visitor.rs index 1262bb311e..6e18c3ded9 100644 --- a/compiler/ast/src/passes/visitor.rs +++ b/compiler/ast/src/passes/visitor.rs @@ -124,7 +124,6 @@ pub trait StatementVisitor<'a>: ExpressionVisitor<'a> { Statement::Decrement(stmt) => self.visit_decrement(stmt), Statement::Definition(stmt) => self.visit_definition(stmt), Statement::Expression(stmt) => self.visit_expression_statement(stmt), - Statement::Finalize(stmt) => self.visit_finalize(stmt), Statement::Increment(stmt) => self.visit_increment(stmt), Statement::Iteration(stmt) => self.visit_iteration(stmt), Statement::Return(stmt) => self.visit_return(stmt), @@ -177,12 +176,6 @@ pub trait StatementVisitor<'a>: ExpressionVisitor<'a> { self.visit_expression(&input.expression, &Default::default()); } - fn visit_finalize(&mut self, input: &'a FinalizeStatement) { - input.arguments.iter().for_each(|expr| { - self.visit_expression(expr, &Default::default()); - }); - } - fn visit_increment(&mut self, input: &'a IncrementStatement) { self.visit_expression(&input.amount, &Default::default()); self.visit_expression(&input.index, &Default::default()); @@ -197,6 +190,11 @@ pub trait StatementVisitor<'a>: ExpressionVisitor<'a> { fn visit_return(&mut self, input: &'a ReturnStatement) { self.visit_expression(&input.expression, &Default::default()); + if let Some(arguments) = &input.finalize_args { + arguments.iter().for_each(|argument| { + self.visit_expression(argument, &Default::default()); + }) + } } } diff --git a/compiler/ast/src/statement/finalize.rs b/compiler/ast/src/statement/finalize.rs deleted file mode 100644 index c915eff41a..0000000000 --- a/compiler/ast/src/statement/finalize.rs +++ /dev/null @@ -1,46 +0,0 @@ -// Copyright (C) 2019-2022 Aleo Systems Inc. -// This file is part of the Leo library. - -// The Leo library is free software: you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. - -// The Leo library is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. - -// 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 leo_span::Span; - -use core::fmt; -use serde::{Deserialize, Serialize}; - -/// A return statement `finalize(arg1, ..., argN);`. -#[derive(Clone, PartialEq, Eq, Serialize, Deserialize, Debug)] -pub struct FinalizeStatement { - /// The arguments to pass to the finalize block. - pub arguments: Vec, - /// The span of `finalize(arg1, ..., argN)` excluding the semicolon. - pub span: Span, -} - -impl fmt::Display for FinalizeStatement { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "finalize(")?; - for (i, param) in self.arguments.iter().enumerate() { - write!(f, "{param}")?; - if i < self.arguments.len() - 1 { - write!(f, ", ")?; - } - } - write!(f, ");") - } -} - -crate::simple_node_impl!(FinalizeStatement); diff --git a/compiler/ast/src/statement/mod.rs b/compiler/ast/src/statement/mod.rs index db7ef4772d..98f6ed551b 100644 --- a/compiler/ast/src/statement/mod.rs +++ b/compiler/ast/src/statement/mod.rs @@ -35,9 +35,6 @@ pub use definition::*; pub mod expression; pub use expression::*; -pub mod finalize; -pub use finalize::*; - pub mod increment; pub use increment::*; @@ -71,8 +68,6 @@ pub enum Statement { Definition(DefinitionStatement), /// An expression statement Expression(ExpressionStatement), - /// A finalize statement. - Finalize(FinalizeStatement), /// An increment statement. Increment(IncrementStatement), /// A `for` statement. @@ -101,7 +96,6 @@ impl fmt::Display for Statement { Statement::Decrement(x) => x.fmt(f), Statement::Definition(x) => x.fmt(f), Statement::Expression(x) => x.fmt(f), - Statement::Finalize(x) => x.fmt(f), Statement::Increment(x) => x.fmt(f), Statement::Iteration(x) => x.fmt(f), Statement::Return(x) => x.fmt(f), @@ -120,7 +114,6 @@ impl Node for Statement { Decrement(n) => n.span(), Definition(n) => n.span(), Expression(n) => n.span(), - Finalize(n) => n.span(), Increment(n) => n.span(), Iteration(n) => n.span(), Return(n) => n.span(), @@ -137,7 +130,6 @@ impl Node for Statement { Decrement(n) => n.set_span(span), Definition(n) => n.set_span(span), Expression(n) => n.set_span(span), - Finalize(n) => n.set_span(span), Increment(n) => n.set_span(span), Iteration(n) => n.set_span(span), Return(n) => n.set_span(span), diff --git a/compiler/ast/src/statement/return_.rs b/compiler/ast/src/statement/return_.rs index 5b85d01f90..05b4b521c4 100644 --- a/compiler/ast/src/statement/return_.rs +++ b/compiler/ast/src/statement/return_.rs @@ -25,6 +25,8 @@ use std::fmt; pub struct ReturnStatement { /// The expression to return to the function caller. pub expression: Expression, + /// Arguments to the finalize block. + pub finalize_args: Option>, /// The span of `return expression` excluding the semicolon. pub span: Span, }