leo_passes/function_inlining/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::FunctionInliningVisitor;
18use leo_ast::{AstReconstructor, Function, ProgramReconstructor, ProgramScope};
19use leo_span::Symbol;
20
21use indexmap::IndexMap;
22
23impl ProgramReconstructor for FunctionInliningVisitor<'_> {
24 fn reconstruct_program_scope(&mut self, input: ProgramScope) -> ProgramScope {
25 // Set the program name.
26 self.program = input.program_id.name.name;
27
28 // Get the post-order ordering of the call graph.
29 // Note that the post-order always contains all nodes in the call graph.
30 // Note that the unwrap is safe since type checking guarantees that the call graph is acyclic.
31 let order = self.state.call_graph.post_order().unwrap();
32
33 // Construct map to provide faster lookup of functions
34 let mut function_map: IndexMap<Symbol, Function> = input.functions.into_iter().collect();
35
36 // Reconstruct and accumulate each of the functions in post-order.
37 for function_name in &order {
38 // None: If `function_name` is not in `input.functions`, then it must be an external function.
39 // TODO: Check that this is indeed an external function. Requires a redesign of the symbol table.
40 if let Some(function) = function_map.shift_remove(function_name) {
41 // Reconstruct the function.
42 let reconstructed_function = self.reconstruct_function(function);
43 // Add the reconstructed function to the mapping.
44 self.reconstructed_functions.push((*function_name, reconstructed_function));
45 }
46 }
47 // This is a sanity check to ensure that functions in the program scope have been processed.
48 assert!(function_map.is_empty(), "All functions in the program scope should have been processed.");
49
50 // Note that this intentionally clears `self.reconstructed_functions` for the next program scope.
51 let functions = core::mem::take(&mut self.reconstructed_functions).into_iter().collect();
52
53 ProgramScope {
54 program_id: input.program_id,
55 structs: input.structs,
56 mappings: input.mappings,
57 functions,
58 consts: input.consts,
59 span: input.span,
60 }
61 }
62
63 fn reconstruct_function(&mut self, input: Function) -> Function {
64 Function {
65 annotations: input.annotations,
66 variant: input.variant,
67 identifier: input.identifier,
68 const_parameters: input.const_parameters,
69 input: input.input,
70 output: input.output,
71 output_type: input.output_type,
72 block: {
73 // Set the `is_async` flag before reconstructing the block.
74 self.is_async = input.variant.is_async_function();
75 // Reconstruct the block.
76 let block = self.reconstruct_block(input.block).0;
77 // Reset the `is_async` flag.
78 self.is_async = false;
79 block
80 },
81 span: input.span,
82 id: input.id,
83 }
84 }
85}