leo_passes/loop_unrolling/
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 leo_ast::*;
18
19use super::UnrollingVisitor;
20
21impl ProgramReconstructor for UnrollingVisitor<'_> {
22    fn reconstruct_stub(&mut self, input: Stub) -> Stub {
23        // Set the current program.
24        self.program = input.stub_id.name.name;
25        Stub {
26            functions: input.functions.into_iter().map(|(i, f)| (i, self.reconstruct_function_stub(f))).collect(),
27            ..input
28        }
29    }
30
31    fn reconstruct_program_scope(&mut self, input: ProgramScope) -> ProgramScope {
32        // Set the current program.
33        self.program = input.program_id.name.name;
34        ProgramScope {
35            functions: input.functions.into_iter().map(|(i, f)| (i, self.reconstruct_function(f))).collect(),
36            constructor: input.constructor.map(|c| self.reconstruct_constructor(c)),
37            ..input
38        }
39    }
40
41    // Reconstruct the function body, entering the associated scopes as needed.
42    fn reconstruct_function(&mut self, function: Function) -> Function {
43        self.in_scope(function.id(), |slf| Function { block: slf.reconstruct_block(function.block).0, ..function })
44    }
45
46    // Reconstruct the constructor body, entering the associated scopes as needed.
47    fn reconstruct_constructor(&mut self, constructor: Constructor) -> Constructor {
48        self.in_scope(constructor.id(), |slf| Constructor {
49            block: slf.reconstruct_block(constructor.block).0,
50            ..constructor
51        })
52    }
53}