leo_passes/const_propagation/
program.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::ConstPropagationVisitor;
18
19use leo_ast::{
20    ConstParameter,
21    Function,
22    Input,
23    Node,
24    Output,
25    ProgramReconstructor,
26    ProgramScope,
27    Statement,
28    StatementReconstructor as _,
29    TypeReconstructor,
30};
31
32impl ProgramReconstructor for ConstPropagationVisitor<'_> {
33    fn reconstruct_program_scope(&mut self, mut input: ProgramScope) -> ProgramScope {
34        self.program = input.program_id.name.name;
35
36        for (_sym, c) in input.consts.iter_mut() {
37            let Statement::Const(declaration) = self.reconstruct_const(std::mem::take(c)).0 else {
38                panic!("`reconstruct_const` always returns `Statement::Const`");
39            };
40            *c = declaration;
41        }
42
43        for (_sym, f) in input.functions.iter_mut() {
44            *f = self.reconstruct_function(std::mem::take(f));
45        }
46
47        input.structs = input.structs.into_iter().map(|(i, c)| (i, self.reconstruct_struct(c))).collect();
48        input.mappings =
49            input.mappings.into_iter().map(|(id, mapping)| (id, self.reconstruct_mapping(mapping))).collect();
50
51        input
52    }
53
54    fn reconstruct_function(&mut self, mut function: Function) -> Function {
55        self.in_scope(function.id(), |slf| {
56            function.const_parameters = function
57                .const_parameters
58                .iter()
59                .map(|param| ConstParameter { type_: slf.reconstruct_type(param.type_.clone()).0, ..param.clone() })
60                .collect();
61            function.input = function
62                .input
63                .iter()
64                .map(|input| Input { type_: slf.reconstruct_type(input.type_.clone()).0, ..input.clone() })
65                .collect();
66            function.output = function
67                .output
68                .iter()
69                .map(|output| Output { type_: slf.reconstruct_type(output.type_.clone()).0, ..output.clone() })
70                .collect();
71            function.output_type = slf.reconstruct_type(function.output_type).0;
72            function.block = slf.reconstruct_block(function.block).0;
73            function
74        })
75    }
76}