leo_passes/const_propagation/
program.rs1use 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}