leo_passes/loop_unrolling/
mod.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 crate::Pass;
18
19use leo_ast::ProgramReconstructor;
20use leo_errors::Result;
21use leo_span::{Span, Symbol};
22
23mod duplicate;
24
25mod range_iterator;
26use range_iterator::*;
27
28mod statement;
29
30mod program;
31
32mod visitor;
33use visitor::*;
34
35pub struct UnrollingOutput {
36    /// If we encountered a loop that was not unrolled, here's it's span.
37    pub loop_not_unrolled: Option<Span>,
38    /// Did we unroll any loop?
39    pub loop_unrolled: bool,
40}
41
42pub struct Unrolling;
43
44impl Pass for Unrolling {
45    type Input = ();
46    type Output = UnrollingOutput;
47
48    const NAME: &str = "Unrolling";
49
50    fn do_pass(_input: Self::Input, state: &mut crate::CompilerState) -> Result<Self::Output> {
51        let mut ast = std::mem::take(&mut state.ast);
52        let mut visitor =
53            UnrollingVisitor { state, program: Symbol::intern(""), loop_not_unrolled: None, loop_unrolled: false };
54        ast.ast = visitor.reconstruct_program(ast.ast);
55        visitor.state.handler.last_err().map_err(|e| *e)?;
56        visitor.state.ast = ast;
57        Ok(UnrollingOutput { loop_not_unrolled: visitor.loop_not_unrolled, loop_unrolled: visitor.loop_unrolled })
58    }
59}