diff --git a/Cargo.lock b/Cargo.lock index 8fc6e9e67e..eb0766ec51 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1199,6 +1199,7 @@ dependencies = [ "criterion", "indexmap", "leo-ast", + "leo-ast-passes", "leo-errors", "leo-parser", "num-bigint", diff --git a/asg/Cargo.toml b/asg/Cargo.toml index 37a691a34e..003030be02 100644 --- a/asg/Cargo.toml +++ b/asg/Cargo.toml @@ -30,6 +30,10 @@ version = "1.7" version = "1.5.3" path = "../ast" +[dependencies.leo-ast-passes] +path = "../ast-passes" +version = "1.5.3" + [dependencies.leo-errors] path = "../errors" version = "1.5.3" diff --git a/asg/src/program/mod.rs b/asg/src/program/mod.rs index 4b64bd8c0f..37473af719 100644 --- a/asg/src/program/mod.rs +++ b/asg/src/program/mod.rs @@ -103,22 +103,25 @@ impl<'a> Program<'a> { // Prepare header-like scope entries. for (name, circuit) in program.circuits.iter() { + assert_eq!(name.name, circuit.circuit_name.name); let asg_circuit = Circuit::init(scope, circuit)?; - scope.circuits.borrow_mut().insert(name.clone(), asg_circuit); + scope.circuits.borrow_mut().insert(name.name.to_string(), asg_circuit); } // Second pass for circuit members. for (name, circuit) in program.circuits.iter() { + assert_eq!(name.name, circuit.circuit_name.name); let asg_circuit = Circuit::init_member(scope, circuit)?; - scope.circuits.borrow_mut().insert(name.clone(), asg_circuit); + scope.circuits.borrow_mut().insert(name.name.to_string(), asg_circuit); } for (name, function) in program.functions.iter() { + assert_eq!(name.name, function.identifier.name); let function = Function::init(scope, function)?; - scope.functions.borrow_mut().insert(name.clone(), function); + scope.functions.borrow_mut().insert(name.name.to_string(), function); } for (name, global_const) in program.global_consts.iter() { @@ -146,11 +149,12 @@ impl<'a> Program<'a> { let mut functions = IndexMap::new(); for (name, function) in program.functions.iter() { - let asg_function = *scope.functions.borrow().get(name).unwrap(); + assert_eq!(name.name, function.identifier.name); + let asg_function = *scope.functions.borrow().get(name.name.as_ref()).unwrap(); asg_function.fill_from_ast(function)?; - let name = name.clone(); + let name = name.name.to_string(); if functions.contains_key(&name) { return Err(AsgError::duplicate_function_definition(name, &function.span).into()); @@ -161,11 +165,12 @@ impl<'a> Program<'a> { let mut circuits = IndexMap::new(); for (name, circuit) in program.circuits.iter() { - let asg_circuit = *scope.circuits.borrow().get(name).unwrap(); + assert_eq!(name.name, circuit.circuit_name.name); + let asg_circuit = *scope.circuits.borrow().get(name.name.as_ref()).unwrap(); asg_circuit.fill_from_ast(circuit)?; - circuits.insert(name.clone(), asg_circuit); + circuits.insert(name.name.to_string(), asg_circuit); } Ok(Program { diff --git a/ast-passes/src/canonicalization/canonicalizer.rs b/ast-passes/src/canonicalization/canonicalizer.rs index 5cf086df3f..ab40cb9918 100644 --- a/ast-passes/src/canonicalization/canonicalizer.rs +++ b/ast-passes/src/canonicalization/canonicalizer.rs @@ -29,7 +29,7 @@ pub struct Canonicalizer { // If we are in a circuit keep track of the circuit name. circuit_name: Option, in_circuit: bool, - alias_lookup: Box Option>, + alias_lookup: Box Option>, } impl AstPass for Canonicalizer { @@ -41,11 +41,11 @@ impl AstPass for Canonicalizer { } impl Canonicalizer { - pub fn new(aliases: IndexMap) -> Self { + pub fn new(aliases: IndexMap) -> Self { Self { circuit_name: None, in_circuit: false, - alias_lookup: Box::new(move |alias: String| -> Option { aliases.get(&alias).cloned() }), + alias_lookup: Box::new(move |alias: Identifier| -> Option { aliases.get(&alias).cloned() }), } } @@ -501,7 +501,7 @@ impl ReconstructingReducer for Canonicalizer { Ok(array) } Type::CircuitOrAlias(identifier) => { - if let Some(alias_type) = (self.alias_lookup)(identifier.name.to_string()) { + if let Some(alias_type) = (self.alias_lookup)(identifier.clone()) { return self.reduce_type(type_, alias_type.represents, &alias_type.name.span); } diff --git a/ast-passes/src/import_resolution/importer.rs b/ast-passes/src/import_resolution/importer.rs index 5f36586827..bc71e12ccb 100644 --- a/ast-passes/src/import_resolution/importer.rs +++ b/ast-passes/src/import_resolution/importer.rs @@ -125,9 +125,9 @@ where expected_input: Vec, import_statements: Vec, empty_imports: IndexMap, - mut aliases: IndexMap, - mut circuits: IndexMap, - mut functions: IndexMap, + mut aliases: IndexMap, + mut circuits: IndexMap, + mut functions: IndexMap, mut global_consts: IndexMap, ) -> Result { if !empty_imports.is_empty() { @@ -160,7 +160,7 @@ where } // TODO copyable AST. - for (package, symbol, span) in imported_symbols.into_iter() { + /* for (package, symbol, span) in imported_symbols.into_iter() { let pretty_package = package.join("."); let resolved_package = resolved_packages @@ -201,7 +201,7 @@ where } } } - } + } */ Ok(Program { name: program.name.clone(), diff --git a/ast/src/program.rs b/ast/src/program.rs index 5506ec9076..de1054a992 100644 --- a/ast/src/program.rs +++ b/ast/src/program.rs @@ -17,7 +17,7 @@ //! A Leo program consists of import, circuit, and function definitions. //! Each defined type consists of ast statements and expressions. -use crate::{Alias, Circuit, DefinitionStatement, Function, FunctionInput, ImportStatement}; +use crate::{Alias, Circuit, DefinitionStatement, Function, FunctionInput, Identifier, ImportStatement}; use indexmap::IndexMap; use serde::{Deserialize, Serialize}; @@ -30,10 +30,10 @@ pub struct Program { pub expected_input: Vec, pub import_statements: Vec, pub imports: IndexMap, - pub aliases: IndexMap, - pub circuits: IndexMap, + pub aliases: IndexMap, + pub circuits: IndexMap, pub global_consts: IndexMap, - pub functions: IndexMap, + pub functions: IndexMap, } impl AsRef for Program { diff --git a/ast/src/reducer/reconstructing_reducer.rs b/ast/src/reducer/reconstructing_reducer.rs index 4c4b847d43..a5f273a66b 100644 --- a/ast/src/reducer/reconstructing_reducer.rs +++ b/ast/src/reducer/reconstructing_reducer.rs @@ -382,9 +382,9 @@ pub trait ReconstructingReducer { expected_input: Vec, import_statements: Vec, imports: IndexMap, - aliases: IndexMap, - circuits: IndexMap, - functions: IndexMap, + aliases: IndexMap, + circuits: IndexMap, + functions: IndexMap, global_consts: IndexMap, ) -> Result { Ok(Program { diff --git a/parser/src/parser/file.rs b/parser/src/parser/file.rs index 271c76f61b..2cb38fc93e 100644 --- a/parser/src/parser/file.rs +++ b/parser/src/parser/file.rs @@ -50,12 +50,6 @@ impl ParserContext { } Token::Ident(ident) if ident.as_ref() == "test" => { return Err(ParserError::test_function(&token.span).into()); - // self.expect(Token::Test)?; - // let (id, function) = self.parse_function_declaration()?; - // tests.insert(id, TestFunction { - // function, - // input_file: None, - // }); } Token::Const => { let (name, global_const) = self.parse_global_const_declaration()?; @@ -408,14 +402,14 @@ impl ParserContext { /// Returns an [`(Identifier, Circuit)`] tuple of AST nodes if the next tokens represent a /// circuit name and definition statement. /// - pub fn parse_circuit(&mut self) -> Result<(String, Circuit)> { + pub fn parse_circuit(&mut self) -> Result<(Identifier, Circuit)> { self.expect(Token::Circuit)?; let name = self.expect_ident()?; self.expect(Token::LeftCurly)?; let members = self.parse_circuit_declaration()?; Ok(( - name.name.to_string(), + name.clone(), Circuit { circuit_name: name, core_mapping: std::cell::RefCell::new(None), @@ -473,7 +467,7 @@ impl ParserContext { /// Returns an [`(Identifier, Function)`] AST node if the next tokens represent a function name /// and function definition. /// - pub fn parse_function_declaration(&mut self) -> Result<(String, Function)> { + pub fn parse_function_declaration(&mut self) -> Result<(Identifier, Function)> { let mut annotations = Vec::new(); while self.peek_token().as_ref() == &Token::At { annotations.push(self.parse_annotation()?); @@ -497,7 +491,7 @@ impl ParserContext { }; let block = self.parse_block()?; Ok(( - name.name.to_string(), + name.clone(), Function { annotations, identifier: name, @@ -529,7 +523,7 @@ impl ParserContext { /// Returns an [`(String, Alias)`] AST node if the next tokens represent a global /// const definition statement and assignment. /// - pub fn parse_type_alias(&mut self) -> Result<(String, Alias)> { + pub fn parse_type_alias(&mut self) -> Result<(Identifier, Alias)> { self.expect(Token::Type)?; let name = self.expect_ident()?; self.expect(Token::Assign)?; @@ -537,7 +531,7 @@ impl ParserContext { self.expect(Token::Semicolon)?; Ok(( - name.name.to_string(), + name.clone(), Alias { represents: type_, name,