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::Binary(binary) => self.consume_binary(*binary),
33            Expression::Call(call) => self.consume_call(*call),
34            Expression::Cast(cast) => self.consume_cast(*cast),
35            Expression::Struct(struct_) => self.consume_struct_init(struct_),
36            Expression::Err(err) => self.consume_err(err),
37            Expression::Identifier(identifier) => self.consume_identifier(identifier),
38            Expression::Literal(value) => self.consume_literal(value),
39            Expression::Locator(locator) => self.consume_locator(locator),
40            Expression::MemberAccess(access) => self.consume_member_access(*access),
41            Expression::Ternary(ternary) => self.consume_ternary(*ternary),
42            Expression::Tuple(tuple) => self.consume_tuple(tuple),
43            Expression::TupleAccess(access) => self.consume_tuple_access(*access),
44            Expression::Unary(unary) => self.consume_unary(*unary),
45            Expression::Unit(unit) => self.consume_unit(unit),
46        }
47    }
48
49    fn consume_array_access(&mut self, _input: ArrayAccess) -> Self::Output;
50
51    fn consume_member_access(&mut self, _input: MemberAccess) -> Self::Output;
52
53    fn consume_tuple_access(&mut self, _input: TupleAccess) -> Self::Output;
54
55    fn consume_associated_constant(&mut self, _input: AssociatedConstantExpression) -> Self::Output;
56
57    fn consume_associated_function(&mut self, _input: AssociatedFunctionExpression) -> Self::Output;
58
59    fn consume_array(&mut self, _input: ArrayExpression) -> Self::Output;
60
61    fn consume_binary(&mut self, _input: BinaryExpression) -> Self::Output;
62
63    fn consume_call(&mut self, _input: CallExpression) -> Self::Output;
64
65    fn consume_cast(&mut self, _input: CastExpression) -> Self::Output;
66
67    fn consume_struct_init(&mut self, _input: StructExpression) -> Self::Output;
68
69    fn consume_err(&mut self, _input: ErrExpression) -> Self::Output {
70        panic!("`ErrExpression`s should not be in the AST at this phase of compilation.")
71    }
72
73    fn consume_identifier(&mut self, _input: Identifier) -> Self::Output;
74
75    fn consume_literal(&mut self, _input: Literal) -> Self::Output;
76
77    fn consume_locator(&mut self, _input: LocatorExpression) -> Self::Output;
78
79    fn consume_ternary(&mut self, _input: TernaryExpression) -> Self::Output;
80
81    fn consume_tuple(&mut self, _input: TupleExpression) -> Self::Output;
82
83    fn consume_unary(&mut self, _input: UnaryExpression) -> Self::Output;
84
85    fn consume_unit(&mut self, _input: UnitExpression) -> Self::Output;
86}
87
88/// A Consumer trait for statements in the AST.
89pub trait StatementConsumer {
90    type Output;
91
92    fn consume_statement(&mut self, input: Statement) -> Self::Output {
93        match input {
94            Statement::Assert(assert) => self.consume_assert(assert),
95            Statement::Assign(stmt) => self.consume_assign(*stmt),
96            Statement::Block(stmt) => self.consume_block(stmt),
97            Statement::Conditional(stmt) => self.consume_conditional(stmt),
98            Statement::Const(stmt) => self.consume_const(stmt),
99            Statement::Definition(stmt) => self.consume_definition(stmt),
100            Statement::Expression(stmt) => self.consume_expression_statement(stmt),
101            Statement::Iteration(stmt) => self.consume_iteration(*stmt),
102            Statement::Return(stmt) => self.consume_return(stmt),
103        }
104    }
105
106    fn consume_assert(&mut self, input: AssertStatement) -> Self::Output;
107
108    fn consume_assign(&mut self, input: AssignStatement) -> Self::Output;
109
110    fn consume_block(&mut self, input: Block) -> Self::Output;
111
112    fn consume_conditional(&mut self, input: ConditionalStatement) -> Self::Output;
113
114    fn consume_const(&mut self, input: ConstDeclaration) -> Self::Output;
115
116    fn consume_definition(&mut self, input: DefinitionStatement) -> Self::Output;
117
118    fn consume_expression_statement(&mut self, input: ExpressionStatement) -> Self::Output;
119
120    fn consume_iteration(&mut self, input: IterationStatement) -> Self::Output;
121
122    fn consume_return(&mut self, input: ReturnStatement) -> Self::Output;
123}
124
125/// A Consumer trait for functions in the AST.
126pub trait FunctionConsumer {
127    type Output;
128
129    fn consume_function(&mut self, input: Function) -> Self::Output;
130}
131
132/// A Consumer trait for structs in the AST.
133pub trait StructConsumer {
134    type Output;
135
136    fn consume_struct(&mut self, input: Composite) -> Self::Output;
137}
138
139/// A Consumer trait for imported programs in the AST.
140pub trait ImportConsumer {
141    type Output;
142
143    fn consume_import(&mut self, input: Program) -> Self::Output;
144}
145
146/// A Consumer trait for mappings in the AST.
147pub trait MappingConsumer {
148    type Output;
149
150    fn consume_mapping(&mut self, input: Mapping) -> Self::Output;
151}
152
153/// A Consumer trait for program scopes in the AST.
154pub trait ProgramScopeConsumer {
155    type Output;
156
157    fn consume_program_scope(&mut self, input: ProgramScope) -> Self::Output;
158}
159
160/// A Consumer trait for the program represented by the AST.
161pub trait ProgramConsumer {
162    type Output;
163    fn consume_program(&mut self, input: Program) -> Self::Output;
164}