use super::FunctionInliningVisitor;
use leo_ast::{Function, ProgramReconstructor, ProgramScope, StatementReconstructor};
use leo_span::Symbol;
use indexmap::IndexMap;
impl ProgramReconstructor for FunctionInliningVisitor<'_> {
fn reconstruct_program_scope(&mut self, input: ProgramScope) -> ProgramScope {
self.program = input.program_id.name.name;
let order = self.state.call_graph.post_order().unwrap();
let mut function_map: IndexMap<Symbol, Function> = input.functions.into_iter().collect();
for function_name in &order {
if let Some(function) = function_map.shift_remove(function_name) {
let reconstructed_function = self.reconstruct_function(function);
self.reconstructed_functions.push((*function_name, reconstructed_function));
}
}
assert!(function_map.is_empty(), "All functions in the program scope should have been processed.");
let functions = core::mem::take(&mut self.reconstructed_functions).into_iter().collect();
ProgramScope {
program_id: input.program_id,
structs: input.structs,
mappings: input.mappings,
functions,
consts: input.consts,
span: input.span,
}
}
fn reconstruct_function(&mut self, input: Function) -> Function {
Function {
annotations: input.annotations,
variant: input.variant,
identifier: input.identifier,
input: input.input,
output: input.output,
output_type: input.output_type,
block: {
self.is_async = input.variant.is_async_function();
let block = self.reconstruct_block(input.block).0;
self.is_async = false;
block
},
span: input.span,
id: input.id,
}
}
}