leo_passes/common_subexpression_elimination/
ast.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
17use super::CommonSubexpressionEliminatingVisitor;
18
19use leo_ast::*;
20
21impl AstReconstructor for CommonSubexpressionEliminatingVisitor<'_> {
22    type AdditionalInput = ();
23    type AdditionalOutput = ();
24
25    fn reconstruct_expression(&mut self, input: Expression, _additional: &()) -> (Expression, Self::AdditionalOutput) {
26        // We simply forward every expression to `try_expr` rather than using the individual reconstruct
27        // functions from the `AstReconstructor` trait.
28        (self.try_expr(input, None).expect("CSE Error").0, Default::default())
29    }
30
31    fn reconstruct_block(&mut self, mut block: Block) -> (Block, Self::AdditionalOutput) {
32        self.in_scope(|slf| {
33            block.statements = block.statements.into_iter().map(|s| slf.reconstruct_statement(s).0).collect();
34            (block, Default::default())
35        })
36    }
37
38    fn reconstruct_definition(&mut self, mut input: DefinitionStatement) -> (Statement, Self::AdditionalOutput) {
39        match input.place {
40            DefinitionPlace::Single(place) => {
41                let (value, definition_not_needed) = self.try_expr(input.value, Some(place.name)).expect("CSE Error");
42
43                if definition_not_needed {
44                    // We don't need this definition - everywhere its variable is referred to, we'll map it to some other
45                    // Path.
46                    (Statement::dummy(), Default::default())
47                } else {
48                    input.value = value;
49                    (input.into(), Default::default())
50                }
51            }
52            DefinitionPlace::Multiple(_) => {
53                let (value, _) = self.try_expr(input.value, None).expect("CSE Error");
54                input.value = value;
55                (input.into(), Default::default())
56            }
57        }
58    }
59
60    fn reconstruct_iteration(&mut self, _: IterationStatement) -> (Statement, Self::AdditionalOutput) {
61        panic!("`IterationStatement`s should not be in the AST at this phase of compilation.");
62    }
63}