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: Error reconstructing expression").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
42                    .try_expr(input.value, Some(place.name))
43                    .expect("CSE Error: Error reconstructing single definition");
44
45                if definition_not_needed {
46                    // We don't need this definition - everywhere its variable is referred to, we'll map it to some other
47                    // Path.
48                    (Statement::dummy(), Default::default())
49                } else {
50                    input.value = value;
51                    (input.into(), Default::default())
52                }
53            }
54            DefinitionPlace::Multiple(_) => {
55                let (value, _) =
56                    self.try_expr(input.value, None).expect("CSE Error: Error reconstructing multiple definitions");
57                input.value = value;
58                (input.into(), Default::default())
59            }
60        }
61    }
62
63    fn reconstruct_iteration(&mut self, _: IterationStatement) -> (Statement, Self::AdditionalOutput) {
64        panic!("`IterationStatement`s should not be in the AST at this phase of compilation.");
65    }
66}