fix imported circuit return from function in import

This commit is contained in:
gluaxspeed 2021-09-06 06:12:58 -07:00
parent cfb8720af7
commit dcdb234637
10 changed files with 62 additions and 94 deletions

View File

@ -21,25 +21,42 @@ use leo_errors::{AstError, Result, Span};
use indexmap::IndexMap;
pub struct Importer<T>
where
T: ImportResolver,
{
import_resolver: T,
}
pub struct Importer {}
impl<T> Importer<T>
where
T: ImportResolver,
{
pub fn new(import_resolver: T) -> Self {
Self { import_resolver }
}
impl Importer {
pub fn do_pass<T>(program: Program, importer: &mut T) -> Result<Ast>
where
T: ImportResolver,
{
let mut imported_symbols: Vec<(Vec<String>, ImportSymbol, Span)> = vec![];
for import_statement in program.import_statements.iter() {
resolve_import_package(&mut imported_symbols, vec![], &import_statement.package_or_packages);
}
pub fn do_pass(ast: Program, importer: T) -> Result<Ast> {
Ok(Ast::new(
ReconstructingDirector::new(Importer::new(importer)).reduce_program(&ast)?,
))
let mut deduplicated_imports: IndexMap<Vec<String>, Span> = IndexMap::new();
for (package, _symbol, span) in imported_symbols.iter() {
deduplicated_imports.insert(package.clone(), span.clone());
}
let mut wrapped_resolver = CoreImportResolver::new(importer);
let mut resolved_packages: IndexMap<Vec<String>, Program> = IndexMap::new();
for (package, span) in deduplicated_imports {
let pretty_package = package.join(".");
let resolved_package =
match wrapped_resolver.resolve_package(&package.iter().map(|x| &**x).collect::<Vec<_>>()[..], &span)? {
Some(x) => x,
None => return Err(AstError::unresolved_import(pretty_package, &span).into()),
};
resolved_packages.insert(package.clone(), resolved_package);
}
let mut ast = program;
ast.imports = resolved_packages;
Ok(Ast::new(ast))
}
}
@ -108,66 +125,3 @@ fn resolve_import_package_access(
}
}
}
impl<T> ReconstructingReducer for Importer<T>
where
T: ImportResolver,
{
fn in_circuit(&self) -> bool {
false
}
fn swap_in_circuit(&mut self) {}
fn reduce_program(
&mut self,
program: &Program,
expected_input: Vec<FunctionInput>,
import_statements: Vec<ImportStatement>,
empty_imports: IndexMap<Vec<String>, Program>,
aliases: IndexMap<Identifier, Alias>,
circuits: IndexMap<Identifier, Circuit>,
functions: IndexMap<Identifier, Function>,
global_consts: IndexMap<Vec<Identifier>, DefinitionStatement>,
) -> Result<Program> {
if !empty_imports.is_empty() {
return Err(AstError::injected_programs(empty_imports.len()).into());
}
let mut imported_symbols: Vec<(Vec<String>, ImportSymbol, Span)> = vec![];
for import_statement in import_statements.iter() {
resolve_import_package(&mut imported_symbols, vec![], &import_statement.package_or_packages);
}
let mut deduplicated_imports: IndexMap<Vec<String>, Span> = IndexMap::new();
for (package, _symbol, span) in imported_symbols.iter() {
deduplicated_imports.insert(package.clone(), span.clone());
}
let mut wrapped_resolver = CoreImportResolver::new(&mut self.import_resolver);
let mut resolved_packages: IndexMap<Vec<String>, Program> = IndexMap::new();
for (package, span) in deduplicated_imports {
let pretty_package = package.join(".");
let resolved_package =
match wrapped_resolver.resolve_package(&package.iter().map(|x| &**x).collect::<Vec<_>>()[..], &span)? {
Some(x) => x,
None => return Err(AstError::unresolved_import(pretty_package, &span).into()),
};
resolved_packages.insert(package.clone(), resolved_package);
}
Ok(Program {
name: program.name.clone(),
expected_input,
import_statements,
imports: resolved_packages,
aliases,
circuits,
functions,
global_consts,
})
}
}

View File

@ -251,7 +251,7 @@ impl<'a, F: PrimeField, G: GroupType<F>> Compiler<'a, F, G> {
// Preform import resolution.
ast = leo_ast_passes::Importer::do_pass(
ast.into_repr(),
ImportParser::new(self.main_file_path.clone(), self.imports_map.clone()),
&mut ImportParser::new(self.main_file_path.clone(), self.imports_map.clone()),
)?;
if self.ast_snapshot_options.imports_resolved {

View File

@ -35,7 +35,10 @@ impl ImportParser {
return self.parse_package(package.path(), remaining_segments, span);
}
Self::parse_import_file(package, span)
let program = Self::parse_import_file(package, span)?;
let ast = leo_ast_passes::Importer::do_pass(program, self)?.into_repr();
Ok(ast)
}
///

View File

@ -143,7 +143,7 @@ fn generate_asts(path: PathBuf, text: &str) -> Result<(String, String, String, S
let mut ast = leo_parser::parse_ast(path.clone().into_os_string().into_string().unwrap(), text)?;
let initial = ast.to_json_string()?;
ast = leo_ast_passes::Importer::do_pass(ast.into_repr(), ImportParser::new(path, Default::default()))?;
ast = leo_ast_passes::Importer::do_pass(ast.into_repr(), &mut ImportParser::new(path, Default::default()))?;
let imports_resolved = ast.to_json_string()?;
ast = leo_ast_passes::Canonicalizer::do_pass(ast.into_repr())?;

View File

@ -12,6 +12,7 @@ function main(y: bool) -> bool {
const hello_alias: char5 = "hello";
const hello = "hello";
const eight = 8u8;
const fab = fab_gen();
return( (foo() == 1u32) && hello_alias == hello && EIGHT == eight) == y;
}

View File

@ -9,4 +9,10 @@ function foo() -> u32 {
type char5 = [char; 5];
const EIGHT = 8u8;
const EIGHT = 8u8;
import nested.c-d.Fab;
function fab_gen() -> Fab {
return Fab { x: 3 };
}

View File

@ -1,3 +1,7 @@
circuit Fab {
x: u8;
}
function cd() -> bool {
return true;
}

View File

@ -16,7 +16,7 @@ outputs:
r0:
type: bool
value: "true"
initial_ast: d2d8f4a8a59423b81f6f31d335762b57cf771ade0241acc6d61614d1fabfc618
imports_resolved_ast: 95c9a6a9d333cdaff558735babd0d9a200d29fe752cb3a80288a69abecdc872d
canonicalized_ast: a2ef4029e1d95374b6b7f6b9e601acb6e4ee7481f7387fd0724149c548e46399
type_inferenced_ast: b7d5149d5beae517ba02c0af1a23183ec28e3f763252be89baed9ba96a3e2786
initial_ast: af29b4526e16fa59b0c58106f77bc4453a845480a04c82a321157d667c6d07c9
imports_resolved_ast: ecd27f76f14e754b00f5c1b4d3f14092e76775865933668c7072885b77067e86
canonicalized_ast: dedb5d6243d79db6a186b4cc8bfbf652c8dcf982c4fc8a32b4bf51e8cf689253
type_inferenced_ast: 316391d0eec112e0fea34a14f2388c320f99d5da63211aea90cd7ca92eb81cf9

View File

@ -17,6 +17,6 @@ outputs:
type: bool
value: "true"
initial_ast: ae6826642faa492e34507695dbd11e5b44c319aecb0b1e78b29ce03ae446d907
imports_resolved_ast: 718ca8903a55a3b9208aea6c80d8969a5413a0499266c887b68dfea620f689e9
canonicalized_ast: 718ca8903a55a3b9208aea6c80d8969a5413a0499266c887b68dfea620f689e9
type_inferenced_ast: 9470aa103176806fd2d40b69224ec249e086fd5c70d56ebe22922e6cf39156e5
imports_resolved_ast: 38cdae0ceb9feea0550ae88df86e9d0676c592fdc7a0a37f56da8c2d62dd3199
canonicalized_ast: 38cdae0ceb9feea0550ae88df86e9d0676c592fdc7a0a37f56da8c2d62dd3199
type_inferenced_ast: 93e0e825fc6daeabfd3891441979f4575d87a019996de7ce43241d1b535c5604

View File

@ -17,6 +17,6 @@ outputs:
type: bool
value: "true"
initial_ast: bb86f336b58de89c79741628133e6aa997f3f49a6f066b8c054261e91e3f18a8
imports_resolved_ast: 1fe862bf85cf0c88ce3c52066118d544f367984dbe97665b2719281de15c449c
canonicalized_ast: 1fe862bf85cf0c88ce3c52066118d544f367984dbe97665b2719281de15c449c
type_inferenced_ast: d8f6f5bde53232553d1ff891e7f78823645d9a8984139d06409cb2ccde562e76
imports_resolved_ast: 7f53319b8eeb7fd2e7e76e7cbe6f130b3af9918060519cc111a45f9739cd8085
canonicalized_ast: 7f53319b8eeb7fd2e7e76e7cbe6f130b3af9918060519cc111a45f9739cd8085
type_inferenced_ast: 1fcfebcdbf04cba7f2878b8efe881f598407a0f15c5419bb7da7a8ea9ec37438