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