leo_ast/passes/
consumer.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
17//! This module contains a Consumer trait for the AST.
18//! Consumers are used to completely transform the AST without any restrictions on the output.
19
20use crate::*;
21
22/// A Consumer trait for expressions in the AST.
23pub trait ExpressionConsumer {
24    type Output;
25
26    fn consume_expression(&mut self, input: Expression) -> Self::Output {
27        match input {
28            Expression::Array(array) => self.consume_array(array),
29            Expression::ArrayAccess(access) => self.consume_array_access(*access),
30            Expression::AssociatedConstant(constant) => self.consume_associated_constant(constant),
31            Expression::AssociatedFunction(function) => self.consume_associated_function(function),
32            Expression::Async(async_) => self.consume_async(async_),
33            Expression::Binary(binary) => self.consume_binary(*binary),
34            Expression::Call(call) => self.consume_call(*call),
35            Expression::Cast(cast) => self.consume_cast(*cast),
36            Expression::Struct(struct_) => self.consume_struct_init(struct_),
37            Expression::Err(err) => self.consume_err(err),
38            Expression::Path(path) => self.consume_path(path),
39            Expression::Literal(value) => self.consume_literal(value),
40            Expression::Locator(locator) => self.consume_locator(locator),
41            Expression::MemberAccess(access) => self.consume_member_access(*access),
42            Expression::Repeat(repeat) => self.consume_repeat(*repeat),
43            Expression::Ternary(ternary) => self.consume_ternary(*ternary),
44            Expression::Tuple(tuple) => self.consume_tuple(tuple),
45            Expression::TupleAccess(access) => self.consume_tuple_access(*access),
46            Expression::Unary(unary) => self.consume_unary(*unary),
47            Expression::Unit(unit) => self.consume_unit(unit),
48        }
49    }
50
51    fn consume_array_access(&mut self, _input: ArrayAccess) -> Self::Output;
52
53    fn consume_member_access(&mut self, _input: MemberAccess) -> Self::Output;
54
55    fn consume_tuple_access(&mut self, _input: TupleAccess) -> Self::Output;
56
57    fn consume_associated_constant(&mut self, _input: AssociatedConstantExpression) -> Self::Output;
58
59    fn consume_associated_function(&mut self, _input: AssociatedFunctionExpression) -> Self::Output;
60
61    fn consume_async(&mut self, _input: AsyncExpression) -> Self::Output;
62
63    fn consume_array(&mut self, _input: ArrayExpression) -> Self::Output;
64
65    fn consume_binary(&mut self, _input: BinaryExpression) -> Self::Output;
66
67    fn consume_call(&mut self, _input: CallExpression) -> Self::Output;
68
69    fn consume_cast(&mut self, _input: CastExpression) -> Self::Output;
70
71    fn consume_struct_init(&mut self, _input: StructExpression) -> Self::Output;
72
73    fn consume_err(&mut self, _input: ErrExpression) -> Self::Output {
74        panic!("`ErrExpression`s should not be in the AST at this phase of compilation.")
75    }
76
77    fn consume_path(&mut self, _input: Path) -> Self::Output;
78
79    fn consume_literal(&mut self, _input: Literal) -> Self::Output;
80
81    fn consume_locator(&mut self, _input: LocatorExpression) -> Self::Output;
82
83    fn consume_repeat(&mut self, _input: RepeatExpression) -> Self::Output;
84
85    fn consume_ternary(&mut self, _input: TernaryExpression) -> Self::Output;
86
87    fn consume_tuple(&mut self, _input: TupleExpression) -> Self::Output;
88
89    fn consume_unary(&mut self, _input: UnaryExpression) -> Self::Output;
90
91    fn consume_unit(&mut self, _input: UnitExpression) -> Self::Output;
92}
93
94/// A Consumer trait for statements in the AST.
95pub trait StatementConsumer {
96    type Output;
97
98    fn consume_statement(&mut self, input: Statement) -> Self::Output {
99        match input {
100            Statement::Assert(assert) => self.consume_assert(assert),
101            Statement::Assign(stmt) => self.consume_assign(*stmt),
102            Statement::Block(stmt) => self.consume_block(stmt),
103            Statement::Conditional(stmt) => self.consume_conditional(stmt),
104            Statement::Const(stmt) => self.consume_const(stmt),
105            Statement::Definition(stmt) => self.consume_definition(stmt),
106            Statement::Expression(stmt) => self.consume_expression_statement(stmt),
107            Statement::Iteration(stmt) => self.consume_iteration(*stmt),
108            Statement::Return(stmt) => self.consume_return(stmt),
109        }
110    }
111
112    fn consume_assert(&mut self, input: AssertStatement) -> Self::Output;
113
114    fn consume_assign(&mut self, input: AssignStatement) -> Self::Output;
115
116    fn consume_block(&mut self, input: Block) -> Self::Output;
117
118    fn consume_conditional(&mut self, input: ConditionalStatement) -> Self::Output;
119
120    fn consume_const(&mut self, input: ConstDeclaration) -> Self::Output;
121
122    fn consume_definition(&mut self, input: DefinitionStatement) -> Self::Output;
123
124    fn consume_expression_statement(&mut self, input: ExpressionStatement) -> Self::Output;
125
126    fn consume_iteration(&mut self, input: IterationStatement) -> Self::Output;
127
128    fn consume_return(&mut self, input: ReturnStatement) -> Self::Output;
129}
130
131/// A Consumer trait for functions in the AST.
132pub trait FunctionConsumer {
133    type Output;
134
135    fn consume_function(&mut self, input: Function) -> Self::Output;
136}
137
138/// A Consumer trait for constructors in the AST.
139pub trait ConstructorConsumer {
140    type Output;
141
142    fn consume_constructor(&mut self, input: Constructor) -> Self::Output;
143}
144
145/// A Consumer trait for structs in the AST.
146pub trait StructConsumer {
147    type Output;
148
149    fn consume_struct(&mut self, input: Composite) -> Self::Output;
150}
151
152/// A Consumer trait for imported programs in the AST.
153pub trait ImportConsumer {
154    type Output;
155
156    fn consume_import(&mut self, input: Program) -> Self::Output;
157}
158
159/// A Consumer trait for mappings in the AST.
160pub trait MappingConsumer {
161    type Output;
162
163    fn consume_mapping(&mut self, input: Mapping) -> Self::Output;
164}
165
166/// A Consumer trait for program scopes in the AST.
167pub trait ProgramScopeConsumer {
168    type Output;
169
170    fn consume_program_scope(&mut self, input: ProgramScope) -> Self::Output;
171}
172
173/// A Consumer trait for the program represented by the AST.
174pub trait ProgramConsumer {
175    type Output;
176    fn consume_program(&mut self, input: Program) -> Self::Output;
177}
178
179/// A Consumer trait for modules in the AST.
180pub trait ModuleConsumer {
181    type Output;
182    fn consume_module(&mut self, input: Module) -> Self::Output;
183}