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