leo_passes/flattening/
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::{FlatteningVisitor, ReturnGuard};
18
19use leo_ast::{
20    AstReconstructor,
21    Constructor,
22    Expression,
23    Function,
24    ProgramReconstructor,
25    ProgramScope,
26    ReturnStatement,
27    Statement,
28};
29
30impl ProgramReconstructor for FlatteningVisitor<'_> {
31    /// Flattens a program scope.
32    fn reconstruct_program_scope(&mut self, input: ProgramScope) -> ProgramScope {
33        self.program = input.program_id.name.name;
34        ProgramScope {
35            program_id: input.program_id,
36            consts: input
37                .consts
38                .into_iter()
39                .map(|(i, c)| match self.reconstruct_const(c) {
40                    (Statement::Const(declaration), _) => (i, declaration),
41                    _ => panic!("`reconstruct_const` can only return `Statement::Const`"),
42                })
43                .collect(),
44            structs: input.structs.into_iter().map(|(i, c)| (i, self.reconstruct_struct(c))).collect(),
45            mappings: input.mappings.into_iter().map(|(id, mapping)| (id, self.reconstruct_mapping(mapping))).collect(),
46            functions: input.functions.into_iter().map(|(i, f)| (i, self.reconstruct_function(f))).collect(),
47            constructor: input.constructor.map(|c| self.reconstruct_constructor(c)),
48            span: input.span,
49        }
50    }
51
52    /// Flattens a function's body
53    fn reconstruct_function(&mut self, function: Function) -> Function {
54        // Set when the function is an async function.
55        self.is_async = function.variant.is_async_function();
56
57        // Flatten the function body.
58        let mut block = self.reconstruct_block(function.block).0;
59
60        // Fold the return statements into the block.
61        let returns = std::mem::take(&mut self.returns);
62        let expression_returns: Vec<(Option<Expression>, ReturnStatement)> = returns
63            .into_iter()
64            .map(|(guard, statement)| match guard {
65                ReturnGuard::None => (None, statement),
66                ReturnGuard::Unconstructed(plain) | ReturnGuard::Constructed { plain, .. } => {
67                    (Some(leo_ast::Path::from(plain).into_absolute().into()), statement)
68                }
69            })
70            .collect();
71
72        self.fold_returns(&mut block, expression_returns);
73
74        Function {
75            annotations: function.annotations,
76            variant: function.variant,
77            identifier: function.identifier,
78            const_parameters: function.const_parameters,
79            input: function.input,
80            output: function.output,
81            output_type: function.output_type,
82            block,
83            span: function.span,
84            id: function.id,
85        }
86    }
87
88    /// Flattens a constructor's body.
89    fn reconstruct_constructor(&mut self, constructor: Constructor) -> Constructor {
90        // A constructor is always async.
91        self.is_async = true;
92
93        // Flatten the function body.
94        let mut block = self.reconstruct_block(constructor.block).0;
95
96        // Fold the return statements into the block.
97        let returns = std::mem::take(&mut self.returns);
98        let expression_returns: Vec<(Option<Expression>, ReturnStatement)> = returns
99            .into_iter()
100            .map(|(guard, statement)| match guard {
101                ReturnGuard::None => (None, statement),
102                ReturnGuard::Unconstructed(plain) | ReturnGuard::Constructed { plain, .. } => {
103                    (Some(plain.into()), statement)
104                }
105            })
106            .collect();
107
108        self.fold_returns(&mut block, expression_returns);
109
110        Constructor { annotations: constructor.annotations, block, span: constructor.span, id: constructor.id }
111    }
112}