diff --git a/compiler/src/compiler.rs b/compiler/src/compiler.rs index 4d42d1e6c9..3c653f4a7a 100644 --- a/compiler/src/compiler.rs +++ b/compiler/src/compiler.rs @@ -29,7 +29,7 @@ use leo_imports::ImportParser; use leo_input::LeoInputParser; use leo_package::inputs::InputPairs; use leo_state::verify_local_data_commitment; -use leo_static_check::{StaticCheck, SymbolTable}; +use leo_static_check::SymbolTable; use leo_typed::{Input, LeoTypedAst, MainInput, Program}; use snarkos_dpc::{base_dpc::instantiated::Components, SystemParameters}; @@ -192,7 +192,7 @@ impl> Compiler { pub(crate) fn check_program(&self) -> Result<(), CompilerError> { // Create a new symbol table from the program, imported_programs, and program_input. let symbol_table = - SymbolTable::run(&self.program, &self.imported_programs, &self.program_input).map_err(|mut e| { + SymbolTable::new(&self.program, &self.imported_programs, &self.program_input).map_err(|mut e| { e.set_path(&self.main_file_path); e @@ -233,7 +233,7 @@ impl> Compiler { self.imported_programs = ImportParser::parse(&self.program)?; // Run static check on program. - let symbol_table = StaticCheck::new(&self.program, &self.imported_programs, &self.program_input)?; + let symbol_table = SymbolTable::new(&self.program, &self.imported_programs, &self.program_input)?; // Run dynamic check on program. DynamicCheck::new(&self.program, symbol_table)?; diff --git a/compiler/src/errors/compiler.rs b/compiler/src/errors/compiler.rs index 1854ac6100..2ebe45a579 100644 --- a/compiler/src/errors/compiler.rs +++ b/compiler/src/errors/compiler.rs @@ -16,13 +16,13 @@ use crate::errors::{FunctionError, ImportError, OutputBytesError, OutputFileError}; use leo_ast::ParserError; +use leo_dynamic_check::DynamicCheckError; use leo_imports::ImportParserError; use leo_input::InputParserError; use leo_state::LocalDataVerificationError; +use leo_static_check::SymbolTableError; use bincode::Error as SerdeError; -use leo_dynamic_check::DynamicCheckError; -use leo_static_check::{StaticCheckError, SymbolTableError}; use std::path::{Path, PathBuf}; #[derive(Debug, Error)] @@ -72,9 +72,6 @@ pub enum CompilerError { #[error("{}", _0)] SerdeError(#[from] SerdeError), - #[error("{}", _0)] - StaticCheckError(#[from] StaticCheckError), - #[error("{}", _0)] SymbolTableError(#[from] SymbolTableError), } diff --git a/compiler/tests/core/mod.rs b/compiler/tests/core/mod.rs index 6a7728361e..20943b7c36 100644 --- a/compiler/tests/core/mod.rs +++ b/compiler/tests/core/mod.rs @@ -16,14 +16,14 @@ pub mod packages; -use crate::{assert_satisfied, expect_static_check_error, parse_program}; +use crate::{assert_satisfied, expect_symbol_table_error, parse_program}; #[test] fn test_core_circuit_invalid() { let program_bytes = include_bytes!("core_package_invalid.leo"); let program = parse_program(program_bytes).err().unwrap(); - expect_static_check_error(program); + expect_symbol_table_error(program); } #[test] @@ -31,7 +31,7 @@ fn test_core_circuit_star_fail() { let program_bytes = include_bytes!("core_circuit_star_fail.leo"); let error = parse_program(program_bytes).err().unwrap(); - expect_static_check_error(error); + expect_symbol_table_error(error); } #[test] @@ -39,7 +39,7 @@ fn test_core_package_invalid() { let program_bytes = include_bytes!("core_package_invalid.leo"); let error = parse_program(program_bytes).err().unwrap(); - expect_static_check_error(error); + expect_symbol_table_error(error); } #[test] @@ -47,7 +47,7 @@ fn test_core_unstable_package_invalid() { let program_bytes = include_bytes!("core_unstable_package_invalid.leo"); let error = parse_program(program_bytes).err().unwrap(); - expect_static_check_error(error); + expect_symbol_table_error(error); } #[test] diff --git a/compiler/tests/mod.rs b/compiler/tests/mod.rs index afd4063378..e34d048dbb 100644 --- a/compiler/tests/mod.rs +++ b/compiler/tests/mod.rs @@ -185,8 +185,8 @@ pub(crate) fn expect_dynamic_check_error(error: CompilerError) { assert!(matches!(error, CompilerError::DynamicCheckError(_))) } -pub(crate) fn expect_static_check_error(error: CompilerError) { - assert!(matches!(error, CompilerError::StaticCheckError(_))) +pub(crate) fn expect_symbol_table_error(error: CompilerError) { + assert!(matches!(error, CompilerError::SymbolTableError(_))) } pub(crate) fn generate_main_input(input: Vec<(&str, Option)>) -> MainInput { diff --git a/dynamic-check/tests/mod.rs b/dynamic-check/tests/mod.rs index f19d302866..d51648c106 100644 --- a/dynamic-check/tests/mod.rs +++ b/dynamic-check/tests/mod.rs @@ -24,7 +24,7 @@ use leo_ast::LeoAst; use leo_dynamic_check::DynamicCheck; use leo_imports::ImportParser; -use leo_static_check::{StaticCheck, SymbolTable}; +use leo_static_check::SymbolTable; use leo_typed::{Input, LeoTypedAst, Program}; use std::path::PathBuf; @@ -58,8 +58,8 @@ impl TestDynamicCheck { // Create empty input. let input = Input::new(); - // Create static check. - let symbol_table = StaticCheck::new(&program, &import_parser, &input).unwrap(); + // Create symbol table. + let symbol_table = SymbolTable::new(&program, &import_parser, &input).unwrap(); // Store fields for new dynamic check. Self { program, symbol_table } diff --git a/static-check/src/errors/mod.rs b/static-check/src/errors/mod.rs index 56968c74ff..107ac193f8 100644 --- a/static-check/src/errors/mod.rs +++ b/static-check/src/errors/mod.rs @@ -14,9 +14,6 @@ // You should have received a copy of the GNU General Public License // along with the Leo library. If not, see . -pub mod static_check; -pub use self::static_check::*; - pub mod symbol_table; pub use self::symbol_table::*; diff --git a/static-check/src/errors/static_check.rs b/static-check/src/errors/static_check.rs deleted file mode 100644 index 5d3aa2f460..0000000000 --- a/static-check/src/errors/static_check.rs +++ /dev/null @@ -1,42 +0,0 @@ -// Copyright (C) 2019-2020 Aleo Systems Inc. -// This file is part of the Leo library. - -// The Leo library is free software: you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. - -// The Leo library is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. - -// You should have received a copy of the GNU General Public License -// along with the Leo library. If not, see . - -use crate::SymbolTableError; -use leo_typed::Error as FormattedError; - -use std::path::Path; - -/// Errors encountered when tracking variable, function and circuit names in a program. -#[derive(Debug, Error)] -pub enum StaticCheckError { - #[error("{}", _0)] - Error(#[from] FormattedError), - - #[error("{}", _0)] - SymbolTableError(#[from] SymbolTableError), -} - -impl StaticCheckError { - /// - /// Sets the filepath for the error stacktrace. - /// - pub fn set_path(&mut self, path: &Path) { - match self { - StaticCheckError::Error(error) => error.set_path(path), - StaticCheckError::SymbolTableError(error) => error.set_path(path), - } - } -} diff --git a/static-check/src/lib.rs b/static-check/src/lib.rs index 0522d5ef9e..2497e304eb 100644 --- a/static-check/src/lib.rs +++ b/static-check/src/lib.rs @@ -26,11 +26,8 @@ pub use self::errors::*; pub mod imports; pub use self::imports::*; -pub mod objects; -pub use self::objects::*; - -pub mod static_check; -pub use self::static_check::*; +pub mod symbol_table; +pub use self::symbol_table::*; pub mod types; pub use self::types::*; diff --git a/static-check/src/objects/mod.rs b/static-check/src/objects/mod.rs deleted file mode 100644 index bdfe157c95..0000000000 --- a/static-check/src/objects/mod.rs +++ /dev/null @@ -1,18 +0,0 @@ -// Copyright (C) 2019-2020 Aleo Systems Inc. -// This file is part of the Leo library. - -// The Leo library is free software: you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. - -// The Leo library is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. - -// You should have received a copy of the GNU General Public License -// along with the Leo library. If not, see . - -pub mod symbol_table; -pub use self::symbol_table::*; diff --git a/static-check/src/static_check.rs b/static-check/src/static_check.rs deleted file mode 100644 index d8300e4f43..0000000000 --- a/static-check/src/static_check.rs +++ /dev/null @@ -1,100 +0,0 @@ -// Copyright (C) 2019-2020 Aleo Systems Inc. -// This file is part of the Leo library. - -// The Leo library is free software: you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. - -// The Leo library is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. - -// You should have received a copy of the GNU General Public License -// along with the Leo library. If not, see . - -use crate::{StaticCheckError, SymbolTable}; -use leo_imports::ImportParser; -use leo_typed::{Input, Program}; - -/// Performs a static type check over a program. -pub struct StaticCheck { - table: SymbolTable, -} - -impl StaticCheck { - /// - /// Returns a new `SymbolTable` from a given program, input, and import parser. - /// - /// Runs pass one name checks and pass two type checks. - /// Builds a symbol table of circuit and function types to be used in the dynamic check. - /// - pub fn new( - program: &Program, - import_parser: &ImportParser, - input: &Input, - ) -> Result { - let mut check = Self::default(); - - // Run checks on program, imports, and input. - check.check(program, import_parser, input)?; - - // Return the symbol table of types. - Ok(check.table) - } - - /// - /// Computes pass one and pass two checks on self. - /// - pub fn check( - &mut self, - program: &Program, - import_parser: &ImportParser, - input: &Input, - ) -> Result<(), StaticCheckError> { - // Insert input types. - self.table - .insert_input(input) - .map_err(|err| StaticCheckError::SymbolTableError(err))?; - - // Run pass one checks. - self.pass_one(program, import_parser)?; - - // Run pass two checks. - self.pass_two(program) - } - - /// - /// Checks for duplicate circuit and function names given an unresolved program. - /// - /// If a circuit or function name has no duplicates, then it is inserted into the symbol table. - /// Variables defined later in the unresolved program cannot have the same name. - /// - pub fn pass_one(&mut self, program: &Program, import_parser: &ImportParser) -> Result<(), StaticCheckError> { - self.table - .check_names(program, import_parser) - .map_err(|err| StaticCheckError::SymbolTableError(err)) - } - - /// - /// Checks for unknown types in circuit and function definitions given an unresolved program. - /// - /// If a circuit or function definition only contains known types, then it is inserted into the - /// symbol table. Variables defined later in the unresolved program can lookup the definition and - /// refer to its expected types. - /// - pub fn pass_two(&mut self, program: &Program) -> Result<(), StaticCheckError> { - self.table - .check_types(program) - .map_err(|err| StaticCheckError::SymbolTableError(err)) - } -} - -impl Default for StaticCheck { - fn default() -> Self { - Self { - table: SymbolTable::new(None), - } - } -} diff --git a/static-check/src/objects/symbol_table.rs b/static-check/src/symbol_table.rs similarity index 98% rename from static-check/src/objects/symbol_table.rs rename to static-check/src/symbol_table.rs index dad2f78a41..eb6bf1a8da 100644 --- a/static-check/src/objects/symbol_table.rs +++ b/static-check/src/symbol_table.rs @@ -49,18 +49,6 @@ pub struct SymbolTable { } impl SymbolTable { - /// - /// Creates a new symbol table with a given parent symbol table. - /// - pub fn new(parent: Option>) -> Self { - SymbolTable { - names: HashMap::new(), - circuits: HashMap::new(), - functions: HashMap::new(), - parent, - } - } - /// /// Returns a new `SymbolTable` from a given, program, imported programs, and program input. /// @@ -69,8 +57,7 @@ impl SymbolTable { /// /// Checks that each circuit or function definition contains valid types. /// - /// - pub fn run( + pub fn new( program: &Program, import_parser: &ImportParser, input: &Input, diff --git a/static-check/tests/mod.rs b/static-check/tests/mod.rs index 8e0d41bbd0..6da6a83779 100644 --- a/static-check/tests/mod.rs +++ b/static-check/tests/mod.rs @@ -17,7 +17,7 @@ pub mod symbol_table; use leo_ast::LeoAst; -use leo_static_check::{StaticCheck, StaticCheckError, SymbolTableError}; +use leo_static_check::{SymbolTable, SymbolTableError}; use leo_typed::{Input, LeoTypedAst}; use leo_imports::ImportParser; @@ -26,11 +26,11 @@ use std::path::PathBuf; const TEST_PROGRAM_PATH: &str = ""; /// A helper struct to test a `SymbolTable`. -pub struct TestStaticCheck { +pub struct TestSymbolTable { typed: LeoTypedAst, } -impl TestStaticCheck { +impl TestSymbolTable { /// /// Returns a typed syntax tree given a Leo program. /// @@ -66,7 +66,7 @@ impl TestStaticCheck { let input = Input::new(); // Create new symbol table. - let _symbol_table = StaticCheck::new(&program, &import_parser, &input).unwrap(); + let _symbol_table = SymbolTable::new(&program, &import_parser, &input).unwrap(); } /// @@ -79,16 +79,16 @@ impl TestStaticCheck { let program = self.typed.into_repr(); // Create new symbol table. - let static_check = &mut StaticCheck::default(); + let static_check = &mut SymbolTable::default(); // Create empty import parser. let import_parser = ImportParser::new(); // Run pass one and expect an error. - let error = static_check.pass_one(&program, &import_parser).unwrap_err(); + let error = static_check.check_names(&program, &import_parser).unwrap_err(); match error { - StaticCheckError::SymbolTableError(SymbolTableError::Error(_)) => {} // Ok + SymbolTableError::Error(_) => {} // Ok error => panic!("Expected a symbol table error found `{}`", error), } } @@ -103,19 +103,19 @@ impl TestStaticCheck { let program = self.typed.into_repr(); // Create a new symbol table. - let static_check = &mut StaticCheck::default(); + let static_check = &mut SymbolTable::default(); // Create empty import parser. let import_parser = ImportParser::new(); // Run the pass one and expect no errors. - static_check.pass_one(&program, &import_parser).unwrap(); + static_check.check_names(&program, &import_parser).unwrap(); // Run the pass two and expect and error. - let error = static_check.pass_two(&program).unwrap_err(); + let error = static_check.check_types(&program).unwrap_err(); match error { - StaticCheckError::SymbolTableError(SymbolTableError::TypeError(_)) => {} //Ok + SymbolTableError::TypeError(_) => {} //Ok error => panic!("Expected a type error found `{}`", error), } } diff --git a/static-check/tests/symbol_table/mod.rs b/static-check/tests/symbol_table/mod.rs index c703db4d17..b6d511c89c 100644 --- a/static-check/tests/symbol_table/mod.rs +++ b/static-check/tests/symbol_table/mod.rs @@ -11,7 +11,7 @@ // GNU General Public License for more details. // You should have received a copy of the GNU General Public License // along with the Leo library. If not, see . -use crate::TestStaticCheck; +use crate::TestSymbolTable; /// /// Defines a circuit `Foo {}`. @@ -23,7 +23,7 @@ use crate::TestStaticCheck; #[test] fn test_duplicate_circuit() { let program_bytes = include_bytes!("duplicate_circuit.leo"); - let resolver = TestStaticCheck::new(program_bytes); + let resolver = TestSymbolTable::new(program_bytes); resolver.expect_pass_one_error(); } @@ -38,7 +38,7 @@ fn test_duplicate_circuit() { #[test] fn test_duplicate_function() { let program_bytes = include_bytes!("duplicate_function.leo"); - let resolver = TestStaticCheck::new(program_bytes); + let resolver = TestSymbolTable::new(program_bytes); resolver.expect_pass_one_error(); } @@ -52,7 +52,7 @@ fn test_duplicate_function() { #[test] fn test_self_not_available() { let program_bytes = include_bytes!("self_not_available.leo"); - let resolver = TestStaticCheck::new(program_bytes); + let resolver = TestSymbolTable::new(program_bytes); resolver.expect_pass_two_error(); } @@ -66,7 +66,7 @@ fn test_self_not_available() { #[test] fn test_undefined_circuit() { let program_bytes = include_bytes!("undefined_circuit.leo"); - let resolver = TestStaticCheck::new(program_bytes); + let resolver = TestSymbolTable::new(program_bytes); resolver.expect_pass_two_error(); }