Make compatible w/ LU and CG passes

This commit is contained in:
evan-schott 2023-10-12 15:46:09 -07:00
parent 5441c23a51
commit b85c5b285e
3 changed files with 34 additions and 2 deletions

View File

@ -30,6 +30,7 @@ use leo_ast::{
Identifier, Identifier,
Literal, Literal,
MemberAccess, MemberAccess,
ProgramScope,
StructExpression, StructExpression,
TernaryExpression, TernaryExpression,
TupleExpression, TupleExpression,
@ -528,6 +529,7 @@ impl<'a> CodeGenerator<'a> {
Expression::Identifier(identifier) => identifier.name, Expression::Identifier(identifier) => identifier.name,
_ => unreachable!("Parsing guarantees that a program name is always an identifier."), _ => unreachable!("Parsing guarantees that a program name is always an identifier."),
}; };
let stub_scope: ProgramScope;
// Lookup the imported program scope. // Lookup the imported program scope.
let imported_program_scope = match self let imported_program_scope = match self
.program .program
@ -536,7 +538,15 @@ impl<'a> CodeGenerator<'a> {
.and_then(|(program, _)| program.program_scopes.get(&program_name)) .and_then(|(program, _)| program.program_scopes.get(&program_name))
{ {
Some(program) => program, Some(program) => program,
None => unreachable!("Type checking guarantees that imported programs are well defined."), None => {
let stub_program = self.program.stubs.get(&program_name);
if stub_program.is_some() {
stub_scope = ProgramScope::from(stub_program.unwrap().clone());
&stub_scope
} else {
unreachable!("Type checking guarantees that imported and stub programs are well defined.")
}
}
}; };
// Check if the external function has a finalize block. // Check if the external function has a finalize block.
let function_name = match *input.function { let function_name = match *input.function {

View File

@ -16,7 +16,7 @@
use crate::CodeGenerator; use crate::CodeGenerator;
use leo_ast::{functions, Function, Mapping, Mode, Program, ProgramScope, Struct, Type, Variant}; use leo_ast::{functions, Function, Mapping, Mode, Program, ProgramScope, Struct, Stub, Type, Variant};
use indexmap::IndexMap; use indexmap::IndexMap;
use itertools::Itertools; use itertools::Itertools;
@ -42,6 +42,9 @@ impl<'a> CodeGenerator<'a> {
program_string.push('\n'); program_string.push('\n');
} }
// Import stub programs
program_string.push_str(&input.stubs.values().map(|stub| self.visit_stub(stub)).join("\n"));
// Retrieve the program scope. // Retrieve the program scope.
// Note that type checking guarantees that there is exactly one program scope. // Note that type checking guarantees that there is exactly one program scope.
let program_scope: &ProgramScope = input.program_scopes.values().next().unwrap(); let program_scope: &ProgramScope = input.program_scopes.values().next().unwrap();
@ -109,6 +112,10 @@ impl<'a> CodeGenerator<'a> {
program_string program_string
} }
fn visit_stub(&mut self, input: &'a Stub) -> String {
format!("import {}.aleo;", input.stub_id.name)
}
fn visit_import(&mut self, import_name: &'a Symbol, import_program: &'a Program) -> String { fn visit_import(&mut self, import_name: &'a Symbol, import_program: &'a Program) -> String {
// Load symbols into composite mapping. // Load symbols into composite mapping.
let _import_program_string = self.visit_program(import_program); let _import_program_string = self.visit_program(import_program);

View File

@ -34,6 +34,21 @@ impl ProgramReconstructor for Unroller<'_> {
} }
} }
// Don't need to reconstruct anything, just need to add child scopes for constant propagation table
fn reconstruct_function_stub(&mut self, input: FunctionStub) -> FunctionStub {
// Lookup function metadata in the symbol table.
// Note that this unwrap is safe since function metadata is stored in a prior pass.
let function_index = self.symbol_table.borrow().lookup_fn_symbol(input.identifier.name).unwrap().id;
// Enter the function's scope.
let previous_function_index = self.enter_scope(function_index);
// Exit the function's scope.
self.exit_scope(previous_function_index);
input
}
fn reconstruct_function(&mut self, function: Function) -> Function { fn reconstruct_function(&mut self, function: Function) -> Function {
// Lookup function metadata in the symbol table. // Lookup function metadata in the symbol table.
// Note that this unwrap is safe since function metadata is stored in a prior pass. // Note that this unwrap is safe since function metadata is stored in a prior pass.