leo_passes/dead_code_elimination/expression.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::DeadCodeEliminatingVisitor;
18
19use leo_ast::{Expression, ExpressionReconstructor, Identifier};
20
21impl ExpressionReconstructor for DeadCodeEliminatingVisitor<'_> {
22 type AdditionalOutput = ();
23
24 // Use and reconstruct an identifier.
25 fn reconstruct_identifier(&mut self, input: Identifier) -> (Expression, Self::AdditionalOutput) {
26 self.used_variables.insert(input.name);
27 (input.into(), Default::default())
28 }
29
30 // We need to make sure we hit identifiers, so do our own traversal
31 // rather than relying on the default.
32 fn reconstruct_struct_init(
33 &mut self,
34 mut input: leo_ast::StructExpression,
35 ) -> (Expression, Self::AdditionalOutput) {
36 for member in input.members.iter_mut() {
37 if let Some(expr) = std::mem::take(&mut member.expression) {
38 member.expression = Some(self.reconstruct_expression(expr).0);
39 } else {
40 // We're not actually going to modify it.
41 self.reconstruct_identifier(member.identifier);
42 }
43 }
44
45 (input.into(), Default::default())
46 }
47}