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    AstReconstructor as _,
21    ConstParameter,
22    Constructor,
23    Function,
24    Input,
25    Module,
26    Node,
27    Output,
28    ProgramReconstructor,
29    ProgramScope,
30    Statement,
31};
32
33impl ProgramReconstructor for ConstPropagationVisitor<'_> {
34    fn reconstruct_program_scope(&mut self, mut input: ProgramScope) -> ProgramScope {
35        self.program = input.program_id.name.name;
36
37        for (_sym, c) in input.consts.iter_mut() {
38            let Statement::Const(declaration) = self.reconstruct_const(std::mem::take(c)).0 else {
39                panic!("`reconstruct_const` always returns `Statement::Const`");
40            };
41            *c = declaration;
42        }
43
44        for (_sym, f) in input.functions.iter_mut() {
45            *f = self.reconstruct_function(std::mem::take(f));
46        }
47
48        if let Some(c) = input.constructor.as_mut() {
49            *c = self.reconstruct_constructor(std::mem::take(c));
50        }
51
52        input.structs = input.structs.into_iter().map(|(i, c)| (i, self.reconstruct_struct(c))).collect();
53        input.mappings =
54            input.mappings.into_iter().map(|(id, mapping)| (id, self.reconstruct_mapping(mapping))).collect();
55
56        input
57    }
58
59    fn reconstruct_module(&mut self, input: Module) -> Module {
60        self.program = input.program_name;
61        self.in_module_scope(&input.path.clone(), |slf| {
62            Module {
63                // Reconstruct consts firsts
64                consts: input
65                    .consts
66                    .into_iter()
67                    .map(|(i, c)| match slf.reconstruct_const(c) {
68                        (Statement::Const(declaration), _) => (i, declaration),
69                        _ => panic!("`reconstruct_const` can only return `Statement::Const`"),
70                    })
71                    .collect(),
72                program_name: input.program_name,
73                path: input.path,
74                structs: input.structs.into_iter().map(|(i, c)| (i, slf.reconstruct_struct(c))).collect(),
75                functions: input.functions.into_iter().map(|(i, f)| (i, slf.reconstruct_function(f))).collect(),
76            }
77        })
78    }
79
80    fn reconstruct_function(&mut self, mut function: Function) -> Function {
81        self.in_scope(function.id(), |slf| {
82            function.const_parameters = function
83                .const_parameters
84                .iter()
85                .map(|param| ConstParameter { type_: slf.reconstruct_type(param.type_.clone()).0, ..param.clone() })
86                .collect();
87            function.input = function
88                .input
89                .iter()
90                .map(|input| Input { type_: slf.reconstruct_type(input.type_.clone()).0, ..input.clone() })
91                .collect();
92            function.output = function
93                .output
94                .iter()
95                .map(|output| Output { type_: slf.reconstruct_type(output.type_.clone()).0, ..output.clone() })
96                .collect();
97            function.output_type = slf.reconstruct_type(function.output_type).0;
98            function.block = slf.reconstruct_block(function.block).0;
99            function
100        })
101    }
102
103    fn reconstruct_constructor(&mut self, mut constructor: Constructor) -> Constructor {
104        self.in_scope(constructor.id(), |slf| {
105            constructor.block = slf.reconstruct_block(constructor.block).0;
106            constructor
107        })
108    }
109}