merge master

This commit is contained in:
gluaxspeed 2021-08-27 09:46:24 -07:00
commit a059e09ae2
562 changed files with 3453 additions and 2696 deletions

View File

@ -97,7 +97,7 @@ jobs:
uses: actions-rs/cargo@v1
with:
command: test
args: --all --features ci_skip
args: --all
- name: Print sccache stats
run: sccache --show-stats
@ -161,7 +161,7 @@ jobs:
run: |
SDKROOT=$(xcrun -sdk macosx11.1 --show-sdk-path) \
MACOSX_DEPLOYMENT_TARGET=$(xcrun -sdk macosx11.1 --show-sdk-platform-version) \
cargo test --all --features ci_skip
cargo test --all
- name: Print sccache stats
run: sccache --show-stats
@ -297,7 +297,7 @@ jobs:
uses: actions-rs/cargo@v1
with:
command: test
args: --all --features ci_skip
args: --all
env:
CARGO_INCREMENTAL: "0"

View File

@ -1,5 +1,5 @@
[hooks]
pre-commit = "cargo clippy && cargo fmt --all -- --check"
pre-commit = "cargo clippy && cargo +nightly fmt --all -- --check"
# temp disable for this branch
[logging]
verbose = true

14
Cargo.lock generated
View File

@ -1199,6 +1199,7 @@ dependencies = [
"criterion",
"indexmap",
"leo-ast",
"leo-ast-passes",
"leo-errors",
"leo-parser",
"num-bigint",
@ -1230,6 +1231,16 @@ dependencies = [
"tendril",
]
[[package]]
name = "leo-ast-passes"
version = "1.5.3"
dependencies = [
"indexmap",
"leo-ast",
"leo-errors",
"leo-parser",
]
[[package]]
name = "leo-compiler"
version = "1.5.3"
@ -1240,6 +1251,7 @@ dependencies = [
"leo-asg",
"leo-asg-passes",
"leo-ast",
"leo-ast-passes",
"leo-errors",
"leo-imports",
"leo-input",
@ -1291,6 +1303,7 @@ dependencies = [
"indexmap",
"leo-asg",
"leo-ast",
"leo-ast-passes",
"leo-errors",
"leo-parser",
"tracing",
@ -1427,6 +1440,7 @@ version = "1.5.3"
dependencies = [
"leo-asg",
"leo-ast",
"leo-ast-passes",
"leo-compiler",
"leo-imports",
"leo-parser",

View File

@ -29,6 +29,7 @@ members = [
"asg",
"asg-passes",
"ast",
"ast-passes",
"compiler",
"errors",
"grammar",

View File

@ -17,7 +17,7 @@
use std::cell::Cell;
use leo_asg::*;
use leo_errors::LeoError;
use leo_errors::Result;
pub struct ConstantFolding<'a, 'b> {
program: &'b Program<'a>,
@ -46,7 +46,7 @@ impl<'a, 'b> StatementVisitor<'a> for ConstantFolding<'a, 'b> {}
impl<'a, 'b> ProgramVisitor<'a> for ConstantFolding<'a, 'b> {}
impl<'a, 'b> AsgPass<'a> for ConstantFolding<'a, 'b> {
fn do_pass(asg: Program<'a>) -> Result<Program<'a>, LeoError> {
fn do_pass(asg: Program<'a>) -> Result<Program<'a>> {
let pass = ConstantFolding { program: &asg };
let mut director = VisitorDirector::new(pass);
director.visit_program(&asg).ok();

View File

@ -17,7 +17,7 @@
use std::cell::Cell;
use leo_asg::*;
use leo_errors::LeoError;
use leo_errors::Result;
pub struct DeadCodeElimination {}
@ -66,7 +66,7 @@ impl<'a> ReconstructingReducerStatement<'a> for DeadCodeElimination {
}
impl<'a> AsgPass<'a> for DeadCodeElimination {
fn do_pass(asg: Program<'a>) -> Result<Program<'a>, LeoError> {
fn do_pass(asg: Program<'a>) -> Result<Program<'a>> {
let pass = DeadCodeElimination {};
let mut director = ReconstructingDirector::new(asg.context, pass);
Ok(director.reduce_program(asg))

View File

@ -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"

View File

@ -18,7 +18,7 @@ use std::{cell::Cell, unimplemented};
use typed_arena::Arena;
use crate::{ArenaNode, Circuit, Expression, Function, Scope, Statement, Variable};
use crate::{Alias, ArenaNode, Circuit, Expression, Function, Scope, Statement, Variable};
pub struct AsgContextInner<'a> {
pub arena: &'a Arena<ArenaNode<'a>>,
@ -74,6 +74,14 @@ impl<'a> AsgContextInner<'a> {
}
}
#[allow(clippy::mut_from_ref)]
pub fn alloc_alias(&'a self, expr: Alias<'a>) -> &'a Alias<'a> {
match self.arena.alloc(ArenaNode::Alias(expr)) {
ArenaNode::Alias(e) => e,
_ => unimplemented!(),
}
}
#[allow(clippy::mut_from_ref)]
pub fn alloc_circuit(&'a self, circuit: Circuit<'a>) -> &'a Circuit<'a> {
match self.arena.alloc(ArenaNode::Circuit(circuit)) {

View File

@ -1,90 +0,0 @@
// Copyright (C) 2019-2021 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 <https://www.gnu.org/licenses/>.
//! Helper methods for resolving imported packages.
use std::marker::PhantomData;
use crate::{AsgContext, Program};
use leo_errors::{Result, Span};
use indexmap::IndexMap;
pub trait ImportResolver<'a> {
fn resolve_package(
&mut self,
context: AsgContext<'a>,
package_segments: &[&str],
span: &Span,
) -> Result<Option<Program<'a>>>;
}
pub struct NullImportResolver;
impl<'a> ImportResolver<'a> for NullImportResolver {
fn resolve_package(
&mut self,
_context: AsgContext<'a>,
_package_segments: &[&str],
_span: &Span,
) -> Result<Option<Program<'a>>> {
Ok(None)
}
}
pub struct CoreImportResolver<'a, 'b, T: ImportResolver<'b>> {
inner: &'a mut T,
lifetime: PhantomData<&'b ()>,
}
impl<'a, 'b, T: ImportResolver<'b>> CoreImportResolver<'a, 'b, T> {
pub fn new(inner: &'a mut T) -> Self {
CoreImportResolver {
inner,
lifetime: PhantomData,
}
}
}
impl<'a, 'b, T: ImportResolver<'b>> ImportResolver<'b> for CoreImportResolver<'a, 'b, T> {
fn resolve_package(
&mut self,
context: AsgContext<'b>,
package_segments: &[&str],
span: &Span,
) -> Result<Option<Program<'b>>> {
if !package_segments.is_empty() && package_segments.get(0).unwrap() == &"core" {
Ok(crate::resolve_core_module(context, &*package_segments[1..].join("."))?)
} else {
self.inner.resolve_package(context, package_segments, span)
}
}
}
pub struct MockedImportResolver<'a> {
pub packages: IndexMap<String, Program<'a>>,
}
impl<'a> ImportResolver<'a> for MockedImportResolver<'a> {
fn resolve_package(
&mut self,
_context: AsgContext<'a>,
package_segments: &[&str],
_span: &Span,
) -> Result<Option<Program<'a>>> {
Ok(self.packages.get(&package_segments.join(".")).cloned())
}
}

View File

@ -35,18 +35,12 @@ pub use const_value::*;
pub mod expression;
pub use expression::*;
pub mod import;
pub use import::*;
mod input;
pub use input::*;
pub mod node;
pub use node::*;
pub mod prelude;
pub use prelude::*;
pub mod program;
pub use program::*;
@ -89,14 +83,10 @@ pub struct Asg<'a> {
impl<'a> Asg<'a> {
/// Creates a new ASG from a given AST and import resolver.
pub fn new<T: ImportResolver<'a>, Y: AsRef<leo_ast::Program>>(
context: AsgContext<'a>,
ast: Y,
resolver: &mut T,
) -> Result<Self> {
pub fn new<Y: AsRef<leo_ast::Program>>(context: AsgContext<'a>, ast: Y) -> Result<Self> {
Ok(Self {
context,
asg: Program::new(context, ast.as_ref(), resolver)?,
asg: Program::new(context, ast.as_ref())?,
})
}
@ -108,29 +98,6 @@ impl<'a> Asg<'a> {
pub fn into_repr(self) -> Program<'a> {
self.asg
}
// /// Serializes the ast into a JSON string.
// pub fn to_json_string(&self) -> Result<String, serde_json::Error> {
// serde_json::to_string_pretty(&self.asg)
// }
//
// /// Deserializes the JSON string into a ast.
// pub fn from_json_string(json: &str) -> Result<Self, serde_json::Error> {
// let ast: Program = serde_json::from_str(json)?;
// Ok(Self { ast })
// }
}
// TODO (howardwu): Remove this.
pub fn load_asg<'a, T: ImportResolver<'a>>(
context: AsgContext<'a>,
content: &str,
resolver: &mut T,
) -> Result<Program<'a>> {
// Parses the Leo file and constructs a grammar ast.
let ast = leo_parser::parse_ast("input.leo", content)?;
Program::new(context, ast.as_repr(), resolver)
}
pub fn new_alloc_context<'a>() -> Arena<ArenaNode<'a>> {

View File

@ -14,7 +14,7 @@
// You should have received a copy of the GNU General Public License
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
use crate::{AsgContextInner, Circuit, Expression, Function, PartialType, Scope, Statement, Variable};
use crate::{Alias, AsgContextInner, Circuit, Expression, Function, PartialType, Scope, Statement, Variable};
use leo_errors::{Result, Span};
@ -37,4 +37,5 @@ pub enum ArenaNode<'a> {
Circuit(Circuit<'a>),
Function(Function<'a>),
Inner(AsgContextInner<'a>),
Alias(Alias<'a>),
}

View File

@ -1,44 +0,0 @@
// Copyright (C) 2019-2021 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 <https://www.gnu.org/licenses/>.
// TODO (protryon): We should merge this with core
use crate::{AsgContext, Program};
use leo_errors::Result;
// TODO (protryon): Make asg deep copy so we can cache resolved core modules
// TODO (protryon): Figure out how to do headers without bogus returns
pub fn resolve_core_module<'a>(context: AsgContext<'a>, module: &str) -> Result<Option<Program<'a>>> {
match module {
"unstable.blake2s" => {
let asg = crate::load_asg(
context,
r#"
circuit Blake2s {
function hash(seed: [u8; 32], message: [u8; 32]) -> [u8; 32] {
return [0; 32];
}
}
"#,
&mut crate::NullImportResolver,
)?;
asg.set_core_mapping("blake2s");
Ok(Some(asg))
}
_ => Ok(None),
}
}

58
asg/src/program/alias.rs Normal file
View File

@ -0,0 +1,58 @@
// Copyright (C) 2019-2021 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 <https://www.gnu.org/licenses/>.
use crate::{Identifier, Node, Scope, Type};
use leo_errors::{Result, Span};
use std::cell::RefCell;
#[derive(Clone)]
pub struct Alias<'a> {
pub id: u32,
pub name: RefCell<Identifier>,
pub span: Option<Span>,
pub represents: Type<'a>,
}
impl<'a> PartialEq for Alias<'a> {
fn eq(&self, other: &Alias) -> bool {
if self.name != other.name {
return false;
}
self.id == other.id
}
}
impl<'a> Eq for Alias<'a> {}
impl<'a> Node for Alias<'a> {
fn span(&self) -> Option<&Span> {
self.span.as_ref()
}
}
impl<'a> Alias<'a> {
pub(super) fn init(scope: &'a Scope<'a>, value: &leo_ast::Alias) -> Result<&'a Alias<'a>> {
let alias = scope.context.alloc_alias(Alias {
id: scope.context.get_id(),
name: RefCell::new(value.name.clone()),
span: Some(value.span.clone()),
represents: scope.resolve_ast_type(&value.represents, &value.span)?,
});
Ok(alias)
}
}

View File

@ -61,7 +61,7 @@ impl<'a> Circuit<'a> {
id: scope.context.get_id(),
name: RefCell::new(value.circuit_name.clone()),
members: RefCell::new(IndexMap::new()),
core_mapping: RefCell::new(None),
core_mapping: value.core_mapping.clone(),
span: Some(value.circuit_name.span.clone()),
scope: new_scope,
});
@ -153,6 +153,7 @@ impl<'a> Into<leo_ast::Circuit> for &Circuit<'a> {
.collect();
leo_ast::Circuit {
circuit_name: self.name.borrow().clone(),
core_mapping: self.core_mapping.clone(),
members,
}
}

View File

@ -82,6 +82,14 @@ impl<'a> Function<'a> {
qualifier = FunctionQualifier::MutSelfRef;
}
FunctionInput::Variable(input_variable) => {
if arguments.contains_key(input_variable.identifier.name.as_ref()) {
return Err(AsgError::duplicate_function_input_definition(
input_variable.identifier.name.as_ref(),
&input_variable.identifier.span,
)
.into());
}
let variable = scope.context.alloc_variable(RefCell::new(crate::InnerVariable {
id: scope.context.get_id(),
name: input_variable.identifier.clone(),
@ -133,6 +141,10 @@ impl<'a> Function<'a> {
.insert("self".to_string(), self_variable);
}
for (name, argument) in self.arguments.iter() {
/* if self.scope.resolve_alias(name).is_some() {
return Err(AsgError::cannot_shadow_name("function input", name, "alias", &argument.get().borrow().name.span).into());
} */
self.scope.variables.borrow_mut().insert(name.clone(), argument.get());
}

View File

@ -18,14 +18,17 @@
//!
//!
mod alias;
pub use alias::*;
mod circuit;
pub use circuit::*;
mod function;
pub use function::*;
use crate::{node::FromAst, ArenaNode, AsgContext, DefinitionStatement, ImportResolver, Input, Scope, Statement};
use leo_ast::{Identifier, PackageAccess, PackageOrPackages};
use crate::{node::FromAst, ArenaNode, AsgContext, DefinitionStatement, Input, Scope, Statement};
use leo_ast::{PackageAccess, PackageOrPackages};
use leo_errors::{AsgError, Result, Span};
use indexmap::IndexMap;
@ -46,6 +49,9 @@ pub struct Program<'a> {
/// these should generally not be accessed directly, but through scoped imports
pub imported_modules: IndexMap<String, Program<'a>>,
/// Maps alias name => alias definition.
pub aliases: IndexMap<String, &'a Alias<'a>>,
/// Maps function name => function code block.
pub functions: IndexMap<String, &'a Function<'a>>,
@ -133,81 +139,66 @@ impl<'a> Program<'a> {
/// 3. finalize declared functions
/// 4. resolve all asg nodes
///
pub fn new<T: ImportResolver<'a>>(
context: AsgContext<'a>,
program: &leo_ast::Program,
import_resolver: &mut T,
) -> Result<Program<'a>> {
// Recursively extract imported symbols.
let mut imported_symbols: Vec<(Vec<String>, ImportSymbol, Span)> = vec![];
for import in program.imports.iter() {
resolve_import_package(&mut imported_symbols, vec![], &import.package_or_packages);
pub fn new(context: AsgContext<'a>, program: &leo_ast::Program) -> Result<Program<'a>> {
// Convert each sub AST.
let mut imported_modules: IndexMap<Vec<String>, Program> = IndexMap::new();
for (package, program) in program.imports.iter() {
imported_modules.insert(package.clone(), Program::new(context, program)?);
}
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);
}
// Create package list.
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 = crate::CoreImportResolver::new(import_resolver);
// Load imported programs.
let mut resolved_packages: IndexMap<Vec<String>, Program> = IndexMap::new();
for (package, span) in deduplicated_imports.iter() {
let pretty_package = package.join(".");
let resolved_package = match wrapped_resolver.resolve_package(
context,
&package.iter().map(|x| &**x).collect::<Vec<_>>()[..],
span,
)? {
Some(x) => x,
None => {
return Err(AsgError::unresolved_import(pretty_package, &Span::default()).into());
}
};
resolved_packages.insert(package.clone(), resolved_package);
}
let mut imported_aliases: IndexMap<String, &'a Alias<'a>> = IndexMap::new();
let mut imported_functions: IndexMap<String, &'a Function<'a>> = IndexMap::new();
let mut imported_circuits: IndexMap<String, &'a Circuit<'a>> = IndexMap::new();
let mut imported_global_consts: IndexMap<String, &'a DefinitionStatement<'a>> = IndexMap::new();
// Prepare locally relevant scope of imports.
for (package, symbol, span) in imported_symbols.into_iter() {
let pretty_package = package.join(".");
let resolved_package = resolved_packages
.get(&package)
let resolved_package = imported_modules
.get_mut(&package)
.expect("could not find preloaded package");
match symbol {
ImportSymbol::All => {
imported_aliases.extend(resolved_package.aliases.clone().into_iter());
imported_functions.extend(resolved_package.functions.clone().into_iter());
imported_circuits.extend(resolved_package.circuits.clone().into_iter());
imported_global_consts.extend(resolved_package.global_consts.clone().into_iter());
}
ImportSymbol::Direct(name) => {
if let Some(function) = resolved_package.functions.get(&name) {
if let Some(alias) = resolved_package.aliases.get(&name) {
imported_aliases.insert(name.clone(), *alias);
} else if let Some(function) = resolved_package.functions.get(&name) {
imported_functions.insert(name.clone(), *function);
} else if let Some(circuit) = resolved_package.circuits.get(&name) {
imported_circuits.insert(name.clone(), *circuit);
} else if let Some(global_const) = resolved_package.global_consts.get(&name) {
imported_global_consts.insert(name.clone(), *global_const);
} else {
return Err(AsgError::unresolved_import(format!("{}.{}", pretty_package, name), &span).into());
return Err(AsgError::unresolved_import(pretty_package, &span).into());
}
}
ImportSymbol::Alias(name, alias) => {
if let Some(function) = resolved_package.functions.get(&name) {
if let Some(type_alias) = resolved_package.aliases.get(&name) {
imported_aliases.insert(alias.clone(), *type_alias);
} else if let Some(function) = resolved_package.functions.get(&name) {
imported_functions.insert(alias.clone(), *function);
} else if let Some(circuit) = resolved_package.circuits.get(&name) {
imported_circuits.insert(alias.clone(), *circuit);
} else if let Some(global_const) = resolved_package.global_consts.get(&name) {
imported_global_consts.insert(alias.clone(), *global_const);
} else {
return Err(AsgError::unresolved_import(format!("{}.{}", pretty_package, name), &span).into());
return Err(AsgError::unresolved_import(pretty_package, &span).into());
}
}
}
@ -218,6 +209,7 @@ impl<'a> Program<'a> {
id: context.get_id(),
parent_scope: Cell::new(None),
variables: RefCell::new(IndexMap::new()),
aliases: RefCell::new(imported_aliases),
functions: RefCell::new(imported_functions),
global_consts: RefCell::new(imported_global_consts),
circuits: RefCell::new(imported_circuits),
@ -234,6 +226,7 @@ impl<'a> Program<'a> {
id: context.get_id(),
parent_scope: Cell::new(Some(import_scope)),
variables: RefCell::new(IndexMap::new()),
aliases: RefCell::new(IndexMap::new()),
functions: RefCell::new(IndexMap::new()),
global_consts: RefCell::new(IndexMap::new()),
circuits: RefCell::new(IndexMap::new()),
@ -241,6 +234,14 @@ impl<'a> Program<'a> {
});
// Prepare header-like scope entries.
// Have to do aliases first.
for (name, alias) in program.aliases.iter() {
assert_eq!(name.name, alias.name.name);
let asg_alias = Alias::init(scope, alias)?;
scope.aliases.borrow_mut().insert(name.name.to_string(), asg_alias);
}
for (name, circuit) in program.circuits.iter() {
assert_eq!(name.name, circuit.circuit_name.name);
let asg_circuit = Circuit::init(scope, circuit)?;
@ -275,6 +276,20 @@ impl<'a> Program<'a> {
}
// Load concrete definitions.
let mut aliases = IndexMap::new();
for (name, alias) in program.aliases.iter() {
assert_eq!(name.name, alias.name.name);
let asg_alias = *scope.aliases.borrow().get(name.name.as_ref()).unwrap();
let name = name.name.to_string();
if aliases.contains_key(&name) {
return Err(AsgError::duplicate_alias_definition(name, &alias.span).into());
}
aliases.insert(name, asg_alias);
}
let mut global_consts = IndexMap::new();
for (name, global_const) in program.global_consts.iter() {
global_const
@ -316,152 +331,15 @@ impl<'a> Program<'a> {
context,
id: context.get_id(),
name: program.name.clone(),
aliases,
functions,
global_consts,
circuits,
imported_modules: resolved_packages
imported_modules: imported_modules
.into_iter()
.map(|(package, program)| (package.join("."), program))
.collect(),
scope,
})
}
pub(crate) fn set_core_mapping(&self, mapping: &str) {
for (_, circuit) in self.circuits.iter() {
circuit.core_mapping.replace(Some(mapping.to_string()));
}
}
}
struct InternalIdentifierGenerator {
next: usize,
}
impl Iterator for InternalIdentifierGenerator {
type Item = String;
fn next(&mut self) -> Option<String> {
let out = format!("$_{}_", self.next);
self.next += 1;
Some(out)
}
}
/// Returns an AST from the given ASG program.
pub fn reform_ast<'a>(program: &Program<'a>) -> leo_ast::Program {
let mut all_programs: IndexMap<String, Program> = IndexMap::new();
let mut program_stack = program.imported_modules.clone();
while let Some((module, program)) = program_stack.pop() {
if all_programs.contains_key(&module) {
continue;
}
all_programs.insert(module, program.clone());
program_stack.extend(program.imported_modules.clone());
}
all_programs.insert("".to_string(), program.clone());
let core_programs: Vec<_> = all_programs
.iter()
.filter(|(module, _)| module.starts_with("core."))
.map(|(module, program)| (module.clone(), program.clone()))
.collect();
all_programs.retain(|module, _| !module.starts_with("core."));
let mut all_circuits: IndexMap<String, &'a Circuit<'a>> = IndexMap::new();
let mut all_functions: IndexMap<String, &'a Function<'a>> = IndexMap::new();
let mut all_global_consts: IndexMap<String, &'a DefinitionStatement<'a>> = IndexMap::new();
let mut identifiers = InternalIdentifierGenerator { next: 0 };
for (_, program) in all_programs.into_iter() {
for (name, circuit) in program.circuits.iter() {
let identifier = format!("{}{}", identifiers.next().unwrap(), name);
circuit.name.borrow_mut().name = identifier.clone().into();
all_circuits.insert(identifier, *circuit);
}
for (name, function) in program.functions.iter() {
let identifier = if name == "main" {
"main".to_string()
} else {
format!("{}{}", identifiers.next().unwrap(), name)
};
function.name.borrow_mut().name = identifier.clone().into();
all_functions.insert(identifier, *function);
}
for (name, global_const) in program.global_consts.iter() {
let identifier = format!("{}{}", identifiers.next().unwrap(), name);
all_global_consts.insert(identifier, *global_const);
}
}
leo_ast::Program {
name: "ast_aggregate".to_string(),
imports: core_programs
.iter()
.map(|(module, _)| leo_ast::ImportStatement {
package_or_packages: leo_ast::PackageOrPackages::Package(leo_ast::Package {
name: Identifier::new(module.clone().into()),
access: leo_ast::PackageAccess::Star { span: Span::default() },
span: Default::default(),
}),
span: Span::default(),
})
.collect(),
expected_input: vec![],
functions: all_functions
.into_iter()
.map(|(_, function)| (function.name.borrow().clone(), function.into()))
.collect(),
circuits: all_circuits
.into_iter()
.map(|(_, circuit)| (circuit.name.borrow().clone(), circuit.into()))
.collect(),
global_consts: all_global_consts
.into_iter()
.map(|(_, global_const)| {
(
global_const
.variables
.iter()
.fold("".to_string(), |joined, variable_name| {
format!("{}, {}", joined, variable_name.borrow().name.name)
}),
global_const.into(),
)
})
.collect(),
}
}
impl<'a> Into<leo_ast::Program> for &Program<'a> {
fn into(self) -> leo_ast::Program {
leo_ast::Program {
name: self.name.clone(),
imports: vec![],
expected_input: vec![],
circuits: self
.circuits
.iter()
.map(|(_, circuit)| (circuit.name.borrow().clone(), (*circuit).into()))
.collect(),
functions: self
.functions
.iter()
.map(|(_, function)| (function.name.borrow().clone(), (*function).into()))
.collect(),
global_consts: self
.global_consts
.iter()
.map(|(_, global_const)| {
(
global_const
.variables
.iter()
.fold("".to_string(), |joined, variable_name| {
format!("{}, {}", joined, variable_name.borrow().name.name)
}),
(*global_const).into(),
)
})
.collect(),
}
}
}

View File

@ -334,6 +334,7 @@ impl<'a, R: ReconstructingReducerProgram<'a>> ReconstructingDirector<'a, R> {
.iter()
.map(|(module, import)| (module.clone(), self.reduce_program(import.clone())))
.collect();
let aliases = input.aliases.iter().map(|(name, a)| (name.clone(), *a)).collect();
let functions = input
.functions
.iter()
@ -352,6 +353,6 @@ impl<'a, R: ReconstructingReducerProgram<'a>> ReconstructingDirector<'a, R> {
.collect();
self.reducer
.reduce_program(input, imported_modules, functions, circuits, global_consts)
.reduce_program(input, imported_modules, aliases, functions, circuits, global_consts)
}
}

View File

@ -396,6 +396,7 @@ pub trait ReconstructingReducerProgram<'a>: ReconstructingReducerStatement<'a> {
&mut self,
input: Program<'a>,
imported_modules: Vec<(String, Program<'a>)>,
aliases: Vec<(String, &'a Alias<'a>)>,
functions: Vec<(String, &'a Function<'a>)>,
circuits: Vec<(String, &'a Circuit<'a>)>,
global_consts: Vec<(String, &'a DefinitionStatement<'a>)>,
@ -405,6 +406,7 @@ pub trait ReconstructingReducerProgram<'a>: ReconstructingReducerStatement<'a> {
id: input.id,
name: input.name,
imported_modules: imported_modules.into_iter().collect(),
aliases: aliases.into_iter().collect(),
functions: functions.into_iter().collect(),
circuits: circuits.into_iter().collect(),
scope: input.scope,

View File

@ -14,7 +14,7 @@
// You should have received a copy of the GNU General Public License
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
use crate::{AsgContext, Circuit, DefinitionStatement, Function, Input, Type, Variable};
use crate::{Alias, AsgContext, Circuit, DefinitionStatement, Function, Input, Type, Variable};
use leo_errors::{AsgError, Result, Span};
use indexmap::IndexMap;
@ -37,6 +37,9 @@ pub struct Scope<'a> {
/// Maps variable name => variable.
pub variables: RefCell<IndexMap<String, &'a Variable<'a>>>,
/// Maps alias name => alias.
pub aliases: RefCell<IndexMap<String, &'a Alias<'a>>>,
/// Maps function name => function.
pub functions: RefCell<IndexMap<String, &'a Function<'a>>>,
@ -100,6 +103,22 @@ impl<'a> Scope<'a> {
}
}
///
/// Returns a reference to the alias corresponding to the name.
///
/// If the current scope did not have this name present, then the parent scope is checked.
/// If there is no parent scope, then `None` is returned.
///
pub fn resolve_alias(&self, name: &str) -> Option<&'a Alias<'a>> {
if let Some(resolved) = self.aliases.borrow().get(name) {
Some(*resolved)
} else if let Some(resolved) = self.parent_scope.get() {
resolved.resolve_alias(name)
} else {
None
}
}
///
/// Returns a reference to the function corresponding to the name.
///
@ -141,6 +160,7 @@ impl<'a> Scope<'a> {
id: self.context.get_id(),
parent_scope: Cell::new(Some(self)),
variables: RefCell::new(IndexMap::new()),
aliases: RefCell::new(IndexMap::new()),
functions: RefCell::new(IndexMap::new()),
circuits: RefCell::new(IndexMap::new()),
global_consts: RefCell::new(IndexMap::new()),
@ -179,10 +199,15 @@ impl<'a> Scope<'a> {
.collect::<Result<Vec<_>>>()?,
),
SelfType => return Err(AsgError::unexpected_big_self(span).into()),
Circuit(name) => Type::Circuit(
self.resolve_circuit(&name.name)
.ok_or_else(|| AsgError::unresolved_circuit(&name.name, &name.span))?,
),
CircuitOrAlias(name) => {
if let Some(circuit) = self.resolve_circuit(&name.name) {
Type::Circuit(circuit)
} else if let Some(alias) = self.resolve_alias(&name.name) {
alias.represents.clone()
} else {
return Err(AsgError::unresolved_circuit(&name.name, &name.span).into());
}
}
})
}
}

View File

@ -110,6 +110,11 @@ impl<'a> FromAst<'a, leo_ast::DefinitionStatement> for &'a Statement<'a> {
}
for (variable, type_) in statement.variable_names.iter().zip(output_types.into_iter()) {
/* let name = variable.identifier.name.as_ref();
if scope.resolve_alias(name).is_some() {
return Err(AsgError::cannot_shadow_name("function input", name, "alias", &variable.identifier.span).into());
} */
variables.push(&*scope.context.alloc_variable(RefCell::new(InnerVariable {
id: scope.context.get_id(),
name: variable.identifier.clone(),

View File

@ -212,7 +212,7 @@ impl<'a> Into<leo_ast::Type> for &Type<'a> {
}]),
),
Tuple(subtypes) => leo_ast::Type::Tuple(subtypes.iter().map(Into::into).collect()),
Circuit(circuit) => leo_ast::Type::Circuit(circuit.name.borrow().clone()),
Circuit(circuit) => leo_ast::Type::CircuitOrAlias(circuit.name.borrow().clone()),
}
}
}

View File

@ -1,3 +0,0 @@
import core.unstable.blake2s.BadCircuit; // `BadCircuit` is not included in the blake2s package
function main() {}

View File

@ -1,3 +0,0 @@
import core.*; // You cannot import all dependencies from core at once
function main() {}

View File

@ -1,3 +0,0 @@
import core.bad_circuit; // `bad_circuit` is not a core package
function main() {}

View File

@ -1,3 +0,0 @@
import core.unstable.bad_circuit; // `bad_circuit` is not a core unstable package
function main() {}

View File

@ -13,29 +13,3 @@
// You should have received a copy of the GNU General Public License
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
use crate::load_asg;
#[test]
fn test_core_circuit_invalid() {
let program_string = include_str!("core_package_invalid.leo");
load_asg(program_string).err().unwrap();
}
#[test]
fn test_core_circuit_star_fail() {
let program_string = include_str!("core_circuit_star_fail.leo");
load_asg(program_string).err().unwrap();
}
#[test]
fn test_core_package_invalid() {
let program_string = include_str!("core_package_invalid.leo");
load_asg(program_string).err().unwrap();
}
#[test]
fn test_core_unstable_package_invalid() {
let program_string = include_str!("core_unstable_package_invalid.leo");
load_asg(program_string).err().unwrap();
}

View File

@ -24,7 +24,6 @@ pub mod definition;
pub mod field;
pub mod function;
// pub mod group; // we dont do context-specific type checking for groups
pub mod import;
pub mod integers;
pub mod mutability;
pub mod statements;

View File

@ -24,22 +24,12 @@ mod pass;
const TESTING_FILEPATH: &str = "input.leo";
fn load_asg(program_string: &str) -> Result<Program<'static>, LeoError> {
load_asg_imports(make_test_context(), program_string, &mut NullImportResolver)
load_asg_imports(make_test_context(), program_string)
}
fn load_asg_imports<'a, T: ImportResolver<'a>>(
context: AsgContext<'a>,
program_string: &str,
imports: &mut T,
) -> Result<Program<'a>, LeoError> {
let mut ast = parse_ast(&TESTING_FILEPATH, program_string)?;
ast.canonicalize()?;
Program::new(context, &ast.as_repr(), imports)
}
fn mocked_resolver(_context: AsgContext<'_>) -> MockedImportResolver<'_> {
let packages = indexmap::IndexMap::new();
MockedImportResolver { packages }
fn load_asg_imports<'a>(context: AsgContext<'a>, program_string: &str) -> Result<Program<'a>, LeoError> {
let ast = parse_ast(&TESTING_FILEPATH, program_string)?;
Program::new(context, &ast.as_repr())
}
//convenience function for tests, leaks memory

View File

@ -1,15 +0,0 @@
circuit Foo {
function qux() {}
function bar() {
Self::qux();
}
function baz() {
Self::bar();
}
}
function main() {
Foo::baz();
}

View File

@ -73,12 +73,6 @@ fn test_member_static_function() {
load_asg(program_string).unwrap();
}
#[test]
fn test_member_static_function_nested() {
let program_string = include_str!("member_static_function_nested.leo");
load_asg(program_string).unwrap();
}
// Mutability
#[test]
@ -109,12 +103,6 @@ fn test_self_member_pass() {
// All
#[test]
fn test_pedersen_mock() {
let program_string = include_str!("pedersen_mock.leo");
load_asg(program_string).unwrap();
}
#[test]
fn test_define_circuit_inside_circuit_function() {
let program_string = include_str!("define_circuit_inside_circuit_function.leo");

View File

@ -1,27 +0,0 @@
circuit PedersenHash {
parameters: [u32; 512];
function new(const parameters: [u32; 512]) -> Self {
return Self { parameters: parameters };
}
function hash(self, const bits: [bool; 512]) -> u32 {
let digest: u32 = 0;
for i in 0..512 {
let base = bits[i] ? self.parameters[i] : 0u32;
digest += base;
}
return digest;
}
}
// The 'pedersen_hash' main function.
function main() {
const parameters = [0u32; 512];
const pedersen = PedersenHash::new(parameters);
const hash_input: [bool; 512] = [true; 512];
const res = pedersen.hash(hash_input);
console.assert(res == 0u32);
}

View File

@ -1,5 +0,0 @@
import core.unstable.blake2s.Blake2s;
function main(seed: [u8; 32], message: [u8; 32]) -> [u8; 32] {
return Blake2s::hash(seed, message);
}

View File

@ -1,7 +0,0 @@
import core.unstable.blake2s.Blake2s;
function main(seed: [u8; 32], message: [u8; 32], expected: [u8; 32]) {
let actual = Blake2s::hash(seed, message);
console.assert(expected == actual);
}

View File

@ -13,23 +13,3 @@
// You should have received a copy of the GNU General Public License
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
use crate::load_asg;
#[test]
fn test_unstable_blake2s() {
let program_string = include_str!("unstable_blake2s.leo");
load_asg(program_string).unwrap();
}
#[test]
fn test_blake2s_input() {
let program_string = include_str!("blake2s_input.leo");
load_asg(program_string).unwrap();
}
#[test]
fn test_blake2s_random() {
let program_string = include_str!("blake2s_random.leo");
load_asg(program_string).unwrap();
}

View File

@ -1,10 +0,0 @@
import core.unstable.blake2s.Blake2s;
function main() {
const seed: [u8; 32] = [0; 32];
const message: [u8; 32] = [0; 32];
const result = Blake2s::hash(seed, message);
console.log("Result: {}", result);
}

View File

@ -1,90 +0,0 @@
// Copyright (C) 2019-2021 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 <https://www.gnu.org/licenses/>.
use crate::{load_asg, make_test_context};
use leo_parser::parse_ast;
#[test]
fn test_basic() {
let program_string = include_str!("./circuits/pedersen_mock.leo");
let asg = load_asg(program_string).unwrap();
let reformed_ast = leo_asg::reform_ast(&asg);
println!("{}", reformed_ast);
// panic!();
}
#[test]
fn test_function_rename() {
let program_string = r#"
function iteration() -> u32 {
let a = 0u32;
for i in 0..10 {
a += 1;
}
return a;
}
function main() {
const total = iteration() + iteration();
console.assert(total == 20);
}
"#;
let asg = load_asg(program_string).unwrap();
let reformed_ast = leo_asg::reform_ast(&asg);
println!("{}", reformed_ast);
// panic!();
}
#[test]
fn test_imports() {
let import_name = "test-import".to_string();
let context = make_test_context();
let mut imports = crate::mocked_resolver(&context);
let test_import = r#"
circuit Point {
x: u32;
y: u32;
}
function foo() -> u32 {
return 1u32;
}
"#;
imports
.packages
.insert(import_name.clone(), load_asg(test_import).unwrap());
let program_string = r#"
import test-import.foo;
function main() {
console.assert(foo() == 1u32);
}
"#;
let test_import_ast = parse_ast(&import_name, test_import).unwrap();
println!("{}", serde_json::to_string(test_import_ast.as_repr()).unwrap());
let test_ast = parse_ast("test.leo", program_string).unwrap();
println!("{}", serde_json::to_string(test_ast.as_repr()).unwrap());
let asg = crate::load_asg_imports(&context, program_string, &mut imports).unwrap();
let reformed_ast = leo_asg::reform_ast(&asg);
println!("{}", serde_json::to_string(&reformed_ast).unwrap());
// panic!();
}

View File

@ -1,5 +0,0 @@
import test-import.foo as bar;
function main() {
console.assert(bar() == 1u32);
}

View File

@ -1,5 +0,0 @@
import test-import.foo;
function main() {
console.assert(foo() == 1u32);
}

View File

@ -1 +0,0 @@
output/

View File

@ -1,3 +0,0 @@
[package]
name = "bar"
version = "0.1.0"

View File

@ -1,3 +0,0 @@
circuit Bat {
t: u32;
}

View File

@ -1,7 +0,0 @@
circuit Baz {
z: u32;
}
circuit Bazzar {
a: u32;
}

View File

@ -1,3 +0,0 @@
circuit Bar {
r: u32;
}

View File

@ -1 +0,0 @@
output/

View File

@ -1,3 +0,0 @@
[package]
name = "car"
version = "0.1.0"

View File

@ -1,3 +0,0 @@
circuit Car {
c: u32;
}

View File

@ -1,26 +0,0 @@
import test-import.( // local import
Point,
foo,
);
import bar.( // imports directory import
Bar,
baz.(Baz, Bazzar),
bat.bat.Bat,
);
import car.Car; // imports directory import
function main() {
const point = Point { x: 1u32, y: 1u32 };
const foo = foo();
const bar = Bar { r: 1u32 };
const baz = Baz { z: 1u32 };
const bazzar = Bazzar { a: 1u32 };
const bat = Bat { t: 1u32 };
const car = Car { c: 1u32 };
console.assert(car.c == 1u32);
}

View File

@ -1,19 +0,0 @@
import test-import.*; // local import
import bar.*; // imports directory import
import bar.baz.*; // imports directory import
import bar.bat.bat.*; // imports directory import
import car.*; // imports directory import
function main() {
const point = Point { x: 1u32, y: 1u32 };
const foo = foo();
const bar = Bar { r: 1u32 };
const bat = Bat { t: 1u32 };
const baz = Baz { z: 1u32 };
const car = Car { c: 1u32 };
console.assert(car.c == 1u32);
}

View File

@ -1,154 +0,0 @@
// Copyright (C) 2019-2021 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 <https://www.gnu.org/licenses/>.
use crate::{load_asg, load_asg_imports, make_test_context, mocked_resolver};
#[test]
fn test_basic() {
let context = make_test_context();
let mut imports = mocked_resolver(&context);
imports.packages.insert(
"test-import".to_string(),
load_asg(include_str!("src/test-import.leo")).unwrap(),
);
let program_string = include_str!("basic.leo");
load_asg_imports(&context, program_string, &mut imports).unwrap();
}
#[test]
fn test_multiple() {
let context = make_test_context();
let mut imports = mocked_resolver(&context);
imports.packages.insert(
"test-import".to_string(),
load_asg(include_str!("src/test-import.leo")).unwrap(),
);
let program_string = include_str!("multiple.leo");
load_asg_imports(&context, program_string, &mut imports).unwrap();
}
#[test]
fn test_star() {
let context = make_test_context();
let mut imports = mocked_resolver(&context);
imports.packages.insert(
"test-import".to_string(),
load_asg(include_str!("src/test-import.leo")).unwrap(),
);
let program_string = include_str!("star.leo");
load_asg_imports(&context, program_string, &mut imports).unwrap();
}
#[test]
fn test_alias() {
let context = make_test_context();
let mut imports = mocked_resolver(&context);
imports.packages.insert(
"test-import".to_string(),
load_asg(include_str!("src/test-import.leo")).unwrap(),
);
let program_string = include_str!("alias.leo");
load_asg_imports(&context, program_string, &mut imports).unwrap();
}
// naming tests
#[test]
fn test_name() {
let context = make_test_context();
let mut imports = mocked_resolver(&context);
imports.packages.insert(
"hello-world".to_string(),
load_asg(include_str!("src/hello-world.leo")).unwrap(),
);
imports
.packages
.insert("a0-f".to_string(), load_asg(include_str!("src/a0-f.leo")).unwrap());
imports
.packages
.insert("a-9".to_string(), load_asg(include_str!("src/a-9.leo")).unwrap());
let program_string = include_str!("names.leo");
load_asg_imports(&context, program_string, &mut imports).unwrap();
}
// more complex tests
#[test]
fn test_many_import() {
let context = make_test_context();
let mut imports = mocked_resolver(&context);
imports.packages.insert(
"test-import".to_string(),
load_asg(include_str!("src/test-import.leo")).unwrap(),
);
imports.packages.insert(
"bar".to_string(),
load_asg(include_str!("imports/bar/src/lib.leo")).unwrap(),
);
imports.packages.insert(
"bar.baz".to_string(),
load_asg(include_str!("imports/bar/src/baz.leo")).unwrap(),
);
imports.packages.insert(
"bar.baz".to_string(),
load_asg(include_str!("imports/bar/src/baz.leo")).unwrap(),
);
imports.packages.insert(
"bar.bat.bat".to_string(),
load_asg(include_str!("imports/bar/src/bat/bat.leo")).unwrap(),
);
imports.packages.insert(
"car".to_string(),
load_asg(include_str!("imports/car/src/lib.leo")).unwrap(),
);
let program_string = include_str!("many_import.leo");
load_asg_imports(&context, program_string, &mut imports).unwrap();
}
#[test]
fn test_many_import_star() {
let context = make_test_context();
let mut imports = mocked_resolver(&context);
imports.packages.insert(
"test-import".to_string(),
load_asg(include_str!("src/test-import.leo")).unwrap(),
);
imports.packages.insert(
"bar".to_string(),
load_asg(include_str!("imports/bar/src/lib.leo")).unwrap(),
);
imports.packages.insert(
"bar.baz".to_string(),
load_asg(include_str!("imports/bar/src/baz.leo")).unwrap(),
);
imports.packages.insert(
"bar.baz".to_string(),
load_asg(include_str!("imports/bar/src/baz.leo")).unwrap(),
);
imports.packages.insert(
"bar.bat.bat".to_string(),
load_asg(include_str!("imports/bar/src/bat/bat.leo")).unwrap(),
);
imports.packages.insert(
"car".to_string(),
load_asg(include_str!("imports/car/src/lib.leo")).unwrap(),
);
let program_string = include_str!("many_import_star.leo");
load_asg_imports(&context, program_string, &mut imports).unwrap();
}

View File

@ -1,10 +0,0 @@
import test-import.(
Point,
foo
);
function main() {
const a = Point { x: 1u32, y: 0u32 };
console.assert(a.x == 1u32);
}

View File

@ -1,5 +0,0 @@
import a0-f.foo;
import a-9.bar;
import hello-world.hello;
function main() {}

View File

@ -1 +0,0 @@
function bar() {}

View File

@ -1 +0,0 @@
function foo() {}

View File

@ -1 +0,0 @@
function hello() {}

View File

@ -1,8 +0,0 @@
circuit Point {
x: u32;
y: u32;
}
function foo() -> u32 {
return 1u32;
}

View File

@ -1,7 +0,0 @@
import test-import.*;
function main() {
const a = Point { x: 1u32, y: 0u32 };
console.assert(foo() == 1u32);
}

View File

@ -22,10 +22,8 @@ pub mod console;
pub mod core;
pub mod definition;
pub mod field;
pub mod form_ast;
pub mod function;
pub mod group;
pub mod import;
pub mod input_files;
pub mod integers;
pub mod mutability;

36
ast-passes/Cargo.toml Normal file
View File

@ -0,0 +1,36 @@
[package]
name = "leo-ast-passes"
version = "1.5.3"
authors = [ "The Aleo Team <hello@aleo.org>" ]
description = "The Leo programming language"
homepage = "https://aleo.org"
repository = "https://github.com/AleoHQ/leo"
keywords = [
"aleo",
"cryptography",
"leo",
"programming-language",
"zero-knowledge"
]
categories = [ "cryptography::cryptocurrencies", "web-programming" ]
include = [ "Cargo.toml", "src", "README.md", "LICENSE.md" ]
license = "GPL-3.0"
edition = "2018"
[lib]
path = "src/lib.rs"
[dependencies]
indexmap = "1.7.0"
[dependencies.leo-ast]
path = "../ast"
version = "1.5.3"
[dependencies.leo-errors]
path = "../errors"
version = "1.5.3"
[dependencies.leo-parser]
path = "../parser"
version = "1.5.3"

596
ast-passes/LICENSE.md Normal file
View File

@ -0,0 +1,596 @@
GNU General Public License
==========================
Version 3, 29 June 2007
Copyright © 2007 Free Software Foundation, Inc. &lt;<https://fsf.org/>&gt;
Everyone is permitted to copy and distribute verbatim copies of this license
document, but changing it is not allowed.
## Preamble
The GNU General Public License is a free, copyleft license for software and other
kinds of works.
The licenses for most software and other practical works are designed to take away
your freedom to share and change the works. By contrast, the GNU General Public
License is intended to guarantee your freedom to share and change all versions of a
program--to make sure it remains free software for all its users. We, the Free
Software Foundation, use the GNU General Public License for most of our software; it
applies also to any other work released this way by its authors. You can apply it to
your programs, too.
When we speak of free software, we are referring to freedom, not price. Our General
Public Licenses are designed to make sure that you have the freedom to distribute
copies of free software (and charge for them if you wish), that you receive source
code or can get it if you want it, that you can change the software or use pieces of
it in new free programs, and that you know you can do these things.
To protect your rights, we need to prevent others from denying you these rights or
asking you to surrender the rights. Therefore, you have certain responsibilities if
you distribute copies of the software, or if you modify it: responsibilities to
respect the freedom of others.
For example, if you distribute copies of such a program, whether gratis or for a fee,
you must pass on to the recipients the same freedoms that you received. You must make
sure that they, too, receive or can get the source code. And you must show them these
terms so they know their rights.
Developers that use the GNU GPL protect your rights with two steps: **(1)** assert
copyright on the software, and **(2)** offer you this License giving you legal permission
to copy, distribute and/or modify it.
For the developers' and authors' protection, the GPL clearly explains that there is
no warranty for this free software. For both users' and authors' sake, the GPL
requires that modified versions be marked as changed, so that their problems will not
be attributed erroneously to authors of previous versions.
Some devices are designed to deny users access to install or run modified versions of
the software inside them, although the manufacturer can do so. This is fundamentally
incompatible with the aim of protecting users' freedom to change the software. The
systematic pattern of such abuse occurs in the area of products for individuals to
use, which is precisely where it is most unacceptable. Therefore, we have designed
this version of the GPL to prohibit the practice for those products. If such problems
arise substantially in other domains, we stand ready to extend this provision to
those domains in future versions of the GPL, as needed to protect the freedom of
users.
Finally, every program is threatened constantly by software patents. States should
not allow patents to restrict development and use of software on general-purpose
computers, but in those that do, we wish to avoid the special danger that patents
applied to a free program could make it effectively proprietary. To prevent this, the
GPL assures that patents cannot be used to render the program non-free.
The precise terms and conditions for copying, distribution and modification follow.
## TERMS AND CONDITIONS
### 0. Definitions
“This License” refers to version 3 of the GNU General Public License.
“Copyright” also means copyright-like laws that apply to other kinds of
works, such as semiconductor masks.
“The Program” refers to any copyrightable work licensed under this
License. Each licensee is addressed as “you”. “Licensees” and
“recipients” may be individuals or organizations.
To “modify” a work means to copy from or adapt all or part of the work in
a fashion requiring copyright permission, other than the making of an exact copy. The
resulting work is called a “modified version” of the earlier work or a
work “based on” the earlier work.
A “covered work” means either the unmodified Program or a work based on
the Program.
To “propagate” a work means to do anything with it that, without
permission, would make you directly or secondarily liable for infringement under
applicable copyright law, except executing it on a computer or modifying a private
copy. Propagation includes copying, distribution (with or without modification),
making available to the public, and in some countries other activities as well.
To “convey” a work means any kind of propagation that enables other
parties to make or receive copies. Mere interaction with a user through a computer
network, with no transfer of a copy, is not conveying.
An interactive user interface displays “Appropriate Legal Notices” to the
extent that it includes a convenient and prominently visible feature that **(1)**
displays an appropriate copyright notice, and **(2)** tells the user that there is no
warranty for the work (except to the extent that warranties are provided), that
licensees may convey the work under this License, and how to view a copy of this
License. If the interface presents a list of user commands or options, such as a
menu, a prominent item in the list meets this criterion.
### 1. Source Code
The “source code” for a work means the preferred form of the work for
making modifications to it. “Object code” means any non-source form of a
work.
A “Standard Interface” means an interface that either is an official
standard defined by a recognized standards body, or, in the case of interfaces
specified for a particular programming language, one that is widely used among
developers working in that language.
The “System Libraries” of an executable work include anything, other than
the work as a whole, that **(a)** is included in the normal form of packaging a Major
Component, but which is not part of that Major Component, and **(b)** serves only to
enable use of the work with that Major Component, or to implement a Standard
Interface for which an implementation is available to the public in source code form.
A “Major Component”, in this context, means a major essential component
(kernel, window system, and so on) of the specific operating system (if any) on which
the executable work runs, or a compiler used to produce the work, or an object code
interpreter used to run it.
The “Corresponding Source” for a work in object code form means all the
source code needed to generate, install, and (for an executable work) run the object
code and to modify the work, including scripts to control those activities. However,
it does not include the work's System Libraries, or general-purpose tools or
generally available free programs which are used unmodified in performing those
activities but which are not part of the work. For example, Corresponding Source
includes interface definition files associated with source files for the work, and
the source code for shared libraries and dynamically linked subprograms that the work
is specifically designed to require, such as by intimate data communication or
control flow between those subprograms and other parts of the work.
The Corresponding Source need not include anything that users can regenerate
automatically from other parts of the Corresponding Source.
The Corresponding Source for a work in source code form is that same work.
### 2. Basic Permissions
All rights granted under this License are granted for the term of copyright on the
Program, and are irrevocable provided the stated conditions are met. This License
explicitly affirms your unlimited permission to run the unmodified Program. The
output from running a covered work is covered by this License only if the output,
given its content, constitutes a covered work. This License acknowledges your rights
of fair use or other equivalent, as provided by copyright law.
You may make, run and propagate covered works that you do not convey, without
conditions so long as your license otherwise remains in force. You may convey covered
works to others for the sole purpose of having them make modifications exclusively
for you, or provide you with facilities for running those works, provided that you
comply with the terms of this License in conveying all material for which you do not
control copyright. Those thus making or running the covered works for you must do so
exclusively on your behalf, under your direction and control, on terms that prohibit
them from making any copies of your copyrighted material outside their relationship
with you.
Conveying under any other circumstances is permitted solely under the conditions
stated below. Sublicensing is not allowed; section 10 makes it unnecessary.
### 3. Protecting Users' Legal Rights From Anti-Circumvention Law
No covered work shall be deemed part of an effective technological measure under any
applicable law fulfilling obligations under article 11 of the WIPO copyright treaty
adopted on 20 December 1996, or similar laws prohibiting or restricting circumvention
of such measures.
When you convey a covered work, you waive any legal power to forbid circumvention of
technological measures to the extent such circumvention is effected by exercising
rights under this License with respect to the covered work, and you disclaim any
intention to limit operation or modification of the work as a means of enforcing,
against the work's users, your or third parties' legal rights to forbid circumvention
of technological measures.
### 4. Conveying Verbatim Copies
You may convey verbatim copies of the Program's source code as you receive it, in any
medium, provided that you conspicuously and appropriately publish on each copy an
appropriate copyright notice; keep intact all notices stating that this License and
any non-permissive terms added in accord with section 7 apply to the code; keep
intact all notices of the absence of any warranty; and give all recipients a copy of
this License along with the Program.
You may charge any price or no price for each copy that you convey, and you may offer
support or warranty protection for a fee.
### 5. Conveying Modified Source Versions
You may convey a work based on the Program, or the modifications to produce it from
the Program, in the form of source code under the terms of section 4, provided that
you also meet all of these conditions:
* **a)** The work must carry prominent notices stating that you modified it, and giving a
relevant date.
* **b)** The work must carry prominent notices stating that it is released under this
License and any conditions added under section 7. This requirement modifies the
requirement in section 4 to “keep intact all notices”.
* **c)** You must license the entire work, as a whole, under this License to anyone who
comes into possession of a copy. This License will therefore apply, along with any
applicable section 7 additional terms, to the whole of the work, and all its parts,
regardless of how they are packaged. This License gives no permission to license the
work in any other way, but it does not invalidate such permission if you have
separately received it.
* **d)** If the work has interactive user interfaces, each must display Appropriate Legal
Notices; however, if the Program has interactive interfaces that do not display
Appropriate Legal Notices, your work need not make them do so.
A compilation of a covered work with other separate and independent works, which are
not by their nature extensions of the covered work, and which are not combined with
it such as to form a larger program, in or on a volume of a storage or distribution
medium, is called an “aggregate” if the compilation and its resulting
copyright are not used to limit the access or legal rights of the compilation's users
beyond what the individual works permit. Inclusion of a covered work in an aggregate
does not cause this License to apply to the other parts of the aggregate.
### 6. Conveying Non-Source Forms
You may convey a covered work in object code form under the terms of sections 4 and
5, provided that you also convey the machine-readable Corresponding Source under the
terms of this License, in one of these ways:
* **a)** Convey the object code in, or embodied in, a physical product (including a
physical distribution medium), accompanied by the Corresponding Source fixed on a
durable physical medium customarily used for software interchange.
* **b)** Convey the object code in, or embodied in, a physical product (including a
physical distribution medium), accompanied by a written offer, valid for at least
three years and valid for as long as you offer spare parts or customer support for
that product model, to give anyone who possesses the object code either **(1)** a copy of
the Corresponding Source for all the software in the product that is covered by this
License, on a durable physical medium customarily used for software interchange, for
a price no more than your reasonable cost of physically performing this conveying of
source, or **(2)** access to copy the Corresponding Source from a network server at no
charge.
* **c)** Convey individual copies of the object code with a copy of the written offer to
provide the Corresponding Source. This alternative is allowed only occasionally and
noncommercially, and only if you received the object code with such an offer, in
accord with subsection 6b.
* **d)** Convey the object code by offering access from a designated place (gratis or for
a charge), and offer equivalent access to the Corresponding Source in the same way
through the same place at no further charge. You need not require recipients to copy
the Corresponding Source along with the object code. If the place to copy the object
code is a network server, the Corresponding Source may be on a different server
(operated by you or a third party) that supports equivalent copying facilities,
provided you maintain clear directions next to the object code saying where to find
the Corresponding Source. Regardless of what server hosts the Corresponding Source,
you remain obligated to ensure that it is available for as long as needed to satisfy
these requirements.
* **e)** Convey the object code using peer-to-peer transmission, provided you inform
other peers where the object code and Corresponding Source of the work are being
offered to the general public at no charge under subsection 6d.
A separable portion of the object code, whose source code is excluded from the
Corresponding Source as a System Library, need not be included in conveying the
object code work.
A “User Product” is either **(1)** a “consumer product”, which
means any tangible personal property which is normally used for personal, family, or
household purposes, or **(2)** anything designed or sold for incorporation into a
dwelling. In determining whether a product is a consumer product, doubtful cases
shall be resolved in favor of coverage. For a particular product received by a
particular user, “normally used” refers to a typical or common use of
that class of product, regardless of the status of the particular user or of the way
in which the particular user actually uses, or expects or is expected to use, the
product. A product is a consumer product regardless of whether the product has
substantial commercial, industrial or non-consumer uses, unless such uses represent
the only significant mode of use of the product.
“Installation Information” for a User Product means any methods,
procedures, authorization keys, or other information required to install and execute
modified versions of a covered work in that User Product from a modified version of
its Corresponding Source. The information must suffice to ensure that the continued
functioning of the modified object code is in no case prevented or interfered with
solely because modification has been made.
If you convey an object code work under this section in, or with, or specifically for
use in, a User Product, and the conveying occurs as part of a transaction in which
the right of possession and use of the User Product is transferred to the recipient
in perpetuity or for a fixed term (regardless of how the transaction is
characterized), the Corresponding Source conveyed under this section must be
accompanied by the Installation Information. But this requirement does not apply if
neither you nor any third party retains the ability to install modified object code
on the User Product (for example, the work has been installed in ROM).
The requirement to provide Installation Information does not include a requirement to
continue to provide support service, warranty, or updates for a work that has been
modified or installed by the recipient, or for the User Product in which it has been
modified or installed. Access to a network may be denied when the modification itself
materially and adversely affects the operation of the network or violates the rules
and protocols for communication across the network.
Corresponding Source conveyed, and Installation Information provided, in accord with
this section must be in a format that is publicly documented (and with an
implementation available to the public in source code form), and must require no
special password or key for unpacking, reading or copying.
### 7. Additional Terms
“Additional permissions” are terms that supplement the terms of this
License by making exceptions from one or more of its conditions. Additional
permissions that are applicable to the entire Program shall be treated as though they
were included in this License, to the extent that they are valid under applicable
law. If additional permissions apply only to part of the Program, that part may be
used separately under those permissions, but the entire Program remains governed by
this License without regard to the additional permissions.
When you convey a copy of a covered work, you may at your option remove any
additional permissions from that copy, or from any part of it. (Additional
permissions may be written to require their own removal in certain cases when you
modify the work.) You may place additional permissions on material, added by you to a
covered work, for which you have or can give appropriate copyright permission.
Notwithstanding any other provision of this License, for material you add to a
covered work, you may (if authorized by the copyright holders of that material)
supplement the terms of this License with terms:
* **a)** Disclaiming warranty or limiting liability differently from the terms of
sections 15 and 16 of this License; or
* **b)** Requiring preservation of specified reasonable legal notices or author
attributions in that material or in the Appropriate Legal Notices displayed by works
containing it; or
* **c)** Prohibiting misrepresentation of the origin of that material, or requiring that
modified versions of such material be marked in reasonable ways as different from the
original version; or
* **d)** Limiting the use for publicity purposes of names of licensors or authors of the
material; or
* **e)** Declining to grant rights under trademark law for use of some trade names,
trademarks, or service marks; or
* **f)** Requiring indemnification of licensors and authors of that material by anyone
who conveys the material (or modified versions of it) with contractual assumptions of
liability to the recipient, for any liability that these contractual assumptions
directly impose on those licensors and authors.
All other non-permissive additional terms are considered “further
restrictions” within the meaning of section 10. If the Program as you received
it, or any part of it, contains a notice stating that it is governed by this License
along with a term that is a further restriction, you may remove that term. If a
license document contains a further restriction but permits relicensing or conveying
under this License, you may add to a covered work material governed by the terms of
that license document, provided that the further restriction does not survive such
relicensing or conveying.
If you add terms to a covered work in accord with this section, you must place, in
the relevant source files, a statement of the additional terms that apply to those
files, or a notice indicating where to find the applicable terms.
Additional terms, permissive or non-permissive, may be stated in the form of a
separately written license, or stated as exceptions; the above requirements apply
either way.
### 8. Termination
You may not propagate or modify a covered work except as expressly provided under
this License. Any attempt otherwise to propagate or modify it is void, and will
automatically terminate your rights under this License (including any patent licenses
granted under the third paragraph of section 11).
However, if you cease all violation of this License, then your license from a
particular copyright holder is reinstated **(a)** provisionally, unless and until the
copyright holder explicitly and finally terminates your license, and **(b)** permanently,
if the copyright holder fails to notify you of the violation by some reasonable means
prior to 60 days after the cessation.
Moreover, your license from a particular copyright holder is reinstated permanently
if the copyright holder notifies you of the violation by some reasonable means, this
is the first time you have received notice of violation of this License (for any
work) from that copyright holder, and you cure the violation prior to 30 days after
your receipt of the notice.
Termination of your rights under this section does not terminate the licenses of
parties who have received copies or rights from you under this License. If your
rights have been terminated and not permanently reinstated, you do not qualify to
receive new licenses for the same material under section 10.
### 9. Acceptance Not Required for Having Copies
You are not required to accept this License in order to receive or run a copy of the
Program. Ancillary propagation of a covered work occurring solely as a consequence of
using peer-to-peer transmission to receive a copy likewise does not require
acceptance. However, nothing other than this License grants you permission to
propagate or modify any covered work. These actions infringe copyright if you do not
accept this License. Therefore, by modifying or propagating a covered work, you
indicate your acceptance of this License to do so.
### 10. Automatic Licensing of Downstream Recipients
Each time you convey a covered work, the recipient automatically receives a license
from the original licensors, to run, modify and propagate that work, subject to this
License. You are not responsible for enforcing compliance by third parties with this
License.
An “entity transaction” is a transaction transferring control of an
organization, or substantially all assets of one, or subdividing an organization, or
merging organizations. If propagation of a covered work results from an entity
transaction, each party to that transaction who receives a copy of the work also
receives whatever licenses to the work the party's predecessor in interest had or
could give under the previous paragraph, plus a right to possession of the
Corresponding Source of the work from the predecessor in interest, if the predecessor
has it or can get it with reasonable efforts.
You may not impose any further restrictions on the exercise of the rights granted or
affirmed under this License. For example, you may not impose a license fee, royalty,
or other charge for exercise of rights granted under this License, and you may not
initiate litigation (including a cross-claim or counterclaim in a lawsuit) alleging
that any patent claim is infringed by making, using, selling, offering for sale, or
importing the Program or any portion of it.
### 11. Patents
A “contributor” is a copyright holder who authorizes use under this
License of the Program or a work on which the Program is based. The work thus
licensed is called the contributor's “contributor version”.
A contributor's “essential patent claims” are all patent claims owned or
controlled by the contributor, whether already acquired or hereafter acquired, that
would be infringed by some manner, permitted by this License, of making, using, or
selling its contributor version, but do not include claims that would be infringed
only as a consequence of further modification of the contributor version. For
purposes of this definition, “control” includes the right to grant patent
sublicenses in a manner consistent with the requirements of this License.
Each contributor grants you a non-exclusive, worldwide, royalty-free patent license
under the contributor's essential patent claims, to make, use, sell, offer for sale,
import and otherwise run, modify and propagate the contents of its contributor
version.
In the following three paragraphs, a “patent license” is any express
agreement or commitment, however denominated, not to enforce a patent (such as an
express permission to practice a patent or covenant not to sue for patent
infringement). To “grant” such a patent license to a party means to make
such an agreement or commitment not to enforce a patent against the party.
If you convey a covered work, knowingly relying on a patent license, and the
Corresponding Source of the work is not available for anyone to copy, free of charge
and under the terms of this License, through a publicly available network server or
other readily accessible means, then you must either **(1)** cause the Corresponding
Source to be so available, or **(2)** arrange to deprive yourself of the benefit of the
patent license for this particular work, or **(3)** arrange, in a manner consistent with
the requirements of this License, to extend the patent license to downstream
recipients. “Knowingly relying” means you have actual knowledge that, but
for the patent license, your conveying the covered work in a country, or your
recipient's use of the covered work in a country, would infringe one or more
identifiable patents in that country that you have reason to believe are valid.
If, pursuant to or in connection with a single transaction or arrangement, you
convey, or propagate by procuring conveyance of, a covered work, and grant a patent
license to some of the parties receiving the covered work authorizing them to use,
propagate, modify or convey a specific copy of the covered work, then the patent
license you grant is automatically extended to all recipients of the covered work and
works based on it.
A patent license is “discriminatory” if it does not include within the
scope of its coverage, prohibits the exercise of, or is conditioned on the
non-exercise of one or more of the rights that are specifically granted under this
License. You may not convey a covered work if you are a party to an arrangement with
a third party that is in the business of distributing software, under which you make
payment to the third party based on the extent of your activity of conveying the
work, and under which the third party grants, to any of the parties who would receive
the covered work from you, a discriminatory patent license **(a)** in connection with
copies of the covered work conveyed by you (or copies made from those copies), or **(b)**
primarily for and in connection with specific products or compilations that contain
the covered work, unless you entered into that arrangement, or that patent license
was granted, prior to 28 March 2007.
Nothing in this License shall be construed as excluding or limiting any implied
license or other defenses to infringement that may otherwise be available to you
under applicable patent law.
### 12. No Surrender of Others' Freedom
If conditions are imposed on you (whether by court order, agreement or otherwise)
that contradict the conditions of this License, they do not excuse you from the
conditions of this License. If you cannot convey a covered work so as to satisfy
simultaneously your obligations under this License and any other pertinent
obligations, then as a consequence you may not convey it at all. For example, if you
agree to terms that obligate you to collect a royalty for further conveying from
those to whom you convey the Program, the only way you could satisfy both those terms
and this License would be to refrain entirely from conveying the Program.
### 13. Use with the GNU Affero General Public License
Notwithstanding any other provision of this License, you have permission to link or
combine any covered work with a work licensed under version 3 of the GNU Affero
General Public License into a single combined work, and to convey the resulting work.
The terms of this License will continue to apply to the part which is the covered
work, but the special requirements of the GNU Affero General Public License, section
13, concerning interaction through a network will apply to the combination as such.
### 14. Revised Versions of this License
The Free Software Foundation may publish revised and/or new versions of the GNU
General Public License from time to time. Such new versions will be similar in spirit
to the present version, but may differ in detail to address new problems or concerns.
Each version is given a distinguishing version number. If the Program specifies that
a certain numbered version of the GNU General Public License “or any later
version” applies to it, you have the option of following the terms and
conditions either of that numbered version or of any later version published by the
Free Software Foundation. If the Program does not specify a version number of the GNU
General Public License, you may choose any version ever published by the Free
Software Foundation.
If the Program specifies that a proxy can decide which future versions of the GNU
General Public License can be used, that proxy's public statement of acceptance of a
version permanently authorizes you to choose that version for the Program.
Later license versions may give you additional or different permissions. However, no
additional obligations are imposed on any author or copyright holder as a result of
your choosing to follow a later version.
### 15. Disclaimer of Warranty
THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW.
EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
PROVIDE THE PROGRAM “AS IS” WITHOUT WARRANTY OF ANY KIND, EITHER
EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE
QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE
DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
### 16. Limitation of Liability
IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY
COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS THE PROGRAM AS
PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL,
INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE
PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE
OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE
WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
POSSIBILITY OF SUCH DAMAGES.
### 17. Interpretation of Sections 15 and 16
If the disclaimer of warranty and limitation of liability provided above cannot be
given local legal effect according to their terms, reviewing courts shall apply local
law that most closely approximates an absolute waiver of all civil liability in
connection with the Program, unless a warranty or assumption of liability accompanies
a copy of the Program in return for a fee.
_END OF TERMS AND CONDITIONS_
## How to Apply These Terms to Your New Programs
If you develop a new program, and you want it to be of the greatest possible use to
the public, the best way to achieve this is to make it free software which everyone
can redistribute and change under these terms.
To do so, attach the following notices to the program. It is safest to attach them
to the start of each source file to most effectively state the exclusion of warranty;
and each file should have at least the “copyright” line and a pointer to
where the full notice is found.
<one line to give the program's name and a brief idea of what it does.>
Copyright (C) <year> <name of author>
This program 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.
This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
Also add information on how to contact you by electronic and paper mail.
If the program does terminal interaction, make it output a short notice like this
when it starts in an interactive mode:
<program> Copyright (C) <year> <name of author>
This program comes with ABSOLUTELY NO WARRANTY; for details type 'show w'.
This is free software, and you are welcome to redistribute it
under certain conditions; type 'show c' for details.
The hypothetical commands `show w` and `show c` should show the appropriate parts of
the General Public License. Of course, your program's commands might be different;
for a GUI interface, you would use an “about box”.
You should also get your employer (if you work as a programmer) or school, if any, to
sign a “copyright disclaimer” for the program, if necessary. For more
information on this, and how to apply and follow the GNU GPL, see
&lt;<http://www.gnu.org/licenses/>&gt;.
The GNU General Public License does not permit incorporating your program into
proprietary programs. If your program is a subroutine library, you may consider it
more useful to permit linking proprietary applications with the library. If this is
what you want to do, use the GNU Lesser General Public License instead of this
License. But first, please read
&lt;<http://www.gnu.org/philosophy/why-not-lgpl.html>&gt;.

1
ast-passes/README.md Normal file
View File

@ -0,0 +1 @@
# leo-ast-passes

View File

@ -14,7 +14,7 @@
// You should have received a copy of the GNU General Public License
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
use crate::*;
use leo_ast::*;
use leo_errors::{AstError, Result, Span};
/// Replace Self when it is in a enclosing circuit type.
@ -29,6 +29,14 @@ pub struct Canonicalizer {
in_circuit: bool,
}
impl AstPass for Canonicalizer {
fn do_pass(ast: Program) -> Result<Ast> {
Ok(Ast::new(
ReconstructingDirector::new(Self::default()).reduce_program(&ast)?,
))
}
}
impl Default for Canonicalizer {
fn default() -> Self {
Self {
@ -108,7 +116,7 @@ impl Canonicalizer {
fn canonicalize_self_type(&self, type_option: Option<&Type>) -> Option<Type> {
match type_option {
Some(type_) => match type_ {
Type::SelfType => Some(Type::Circuit(self.circuit_name.as_ref().unwrap().clone())),
Type::SelfType => Some(Type::CircuitOrAlias(self.circuit_name.as_ref().unwrap().clone())),
Type::Array(type_, dimensions) => Some(Type::Array(
Box::new(self.canonicalize_self_type(Some(type_)).unwrap()),
dimensions.clone(),
@ -467,7 +475,7 @@ impl Canonicalizer {
identifier: variable.identifier.clone(),
const_: variable.const_,
mutable: variable.mutable,
type_: Type::Circuit(self.circuit_name.as_ref().unwrap().clone()),
type_: Type::CircuitOrAlias(self.circuit_name.as_ref().unwrap().clone()),
span: variable.span.clone(),
});
}
@ -706,13 +714,14 @@ impl ReconstructingReducer for Canonicalizer {
fn reduce_circuit(
&mut self,
_circuit: &Circuit,
circuit: &Circuit,
circuit_name: Identifier,
members: Vec<CircuitMember>,
) -> Result<Circuit> {
self.circuit_name = Some(circuit_name.clone());
let circ = Circuit {
circuit_name,
core_mapping: circuit.core_mapping.clone(),
members: members
.iter()
.map(|member| self.canonicalize_circuit_member(member))

View File

@ -13,3 +13,6 @@
// You should have received a copy of the GNU General Public License
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
pub mod canonicalizer;
pub use canonicalizer::*;

View File

@ -0,0 +1,173 @@
// Copyright (C) 2019-2021 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 <https://www.gnu.org/licenses/>.
use crate::resolver::*;
use leo_ast::*;
use leo_errors::{AstError, Result, Span};
use indexmap::IndexMap;
pub struct Importer<T>
where
T: ImportResolver,
{
import_resolver: T,
}
impl<T> Importer<T>
where
T: ImportResolver,
{
pub fn new(import_resolver: T) -> Self {
Self { import_resolver }
}
pub fn do_pass(ast: Program, importer: T) -> Result<Ast> {
Ok(Ast::new(
ReconstructingDirector::new(Importer::new(importer)).reduce_program(&ast)?,
))
}
}
/// Enumerates what names are imported from a package.
#[derive(Clone)]
enum ImportSymbol {
/// Import the symbol by name.
Direct(String),
/// Import the symbol by name and store it under an alias.
Alias(String, String), // from remote -> to local
/// Import all symbols from the package.
All,
}
fn resolve_import_package(
output: &mut Vec<(Vec<String>, ImportSymbol, Span)>,
mut package_segments: Vec<String>,
package_or_packages: &PackageOrPackages,
) {
match package_or_packages {
PackageOrPackages::Package(package) => {
package_segments.push(package.name.name.to_string());
resolve_import_package_access(output, package_segments, &package.access);
}
PackageOrPackages::Packages(packages) => {
package_segments.push(packages.name.name.to_string());
for access in packages.accesses.clone() {
resolve_import_package_access(output, package_segments.clone(), &access);
}
}
}
}
fn resolve_import_package_access(
output: &mut Vec<(Vec<String>, ImportSymbol, Span)>,
mut package_segments: Vec<String>,
package: &PackageAccess,
) {
match package {
PackageAccess::Star { span } => {
output.push((package_segments, ImportSymbol::All, span.clone()));
}
PackageAccess::SubPackage(subpackage) => {
resolve_import_package(
output,
package_segments,
&PackageOrPackages::Package(*(*subpackage).clone()),
);
}
PackageAccess::Symbol(symbol) => {
let span = symbol.symbol.span.clone();
let symbol = if let Some(alias) = symbol.alias.as_ref() {
ImportSymbol::Alias(symbol.symbol.name.to_string(), alias.name.to_string())
} else {
ImportSymbol::Direct(symbol.symbol.name.to_string())
};
output.push((package_segments, symbol, span));
}
PackageAccess::Multiple(packages) => {
package_segments.push(packages.name.name.to_string());
for subaccess in packages.accesses.iter() {
resolve_import_package_access(output, package_segments.clone(), subaccess);
}
}
}
}
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<String, 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

@ -0,0 +1,21 @@
// Copyright (C) 2019-2021 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 <https://www.gnu.org/licenses/>.
pub mod importer;
pub use self::importer::*;
pub mod resolver;
pub use self::resolver::*;

View File

@ -0,0 +1,90 @@
// Copyright (C) 2019-2021 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 <https://www.gnu.org/licenses/>.
use leo_ast::Program;
use leo_errors::{Result, Span};
use indexmap::IndexMap;
pub trait ImportResolver {
fn resolve_package(&mut self, package_segments: &[&str], span: &Span) -> Result<Option<Program>>;
}
pub struct NullImportResolver;
impl ImportResolver for NullImportResolver {
fn resolve_package(&mut self, _package_segments: &[&str], _span: &Span) -> Result<Option<Program>> {
Ok(None)
}
}
pub struct CoreImportResolver<'a, T: ImportResolver> {
inner: &'a mut T,
}
impl<'a, T: ImportResolver> CoreImportResolver<'a, T> {
pub fn new(inner: &'a mut T) -> Self {
CoreImportResolver { inner }
}
}
impl<'a, T: ImportResolver> ImportResolver for CoreImportResolver<'a, T> {
fn resolve_package(&mut self, package_segments: &[&str], span: &Span) -> Result<Option<Program>> {
if !package_segments.is_empty() && package_segments.get(0).unwrap() == &"core" {
Ok(resolve_core_module(&*package_segments[1..].join("."))?)
} else {
self.inner.resolve_package(package_segments, span)
}
}
}
pub struct MockedImportResolver {
pub packages: IndexMap<String, Program>,
}
impl ImportResolver for MockedImportResolver {
fn resolve_package(&mut self, package_segments: &[&str], _span: &Span) -> Result<Option<Program>> {
Ok(self.packages.get(&package_segments.join(".")).cloned())
}
}
// TODO: Remove this.
pub fn load_ast(content: &str) -> Result<Program> {
// Parses the Leo file and constructs a grammar ast.
Ok(leo_parser::parse_ast("input.leo", content)?.into_repr())
}
// TODO: We should merge this with core
// TODO: Make asg deep copy so we can cache resolved core modules
// TODO: Figure out how to do headers without bogus returns
pub fn resolve_core_module(module: &str) -> Result<Option<Program>> {
match module {
"unstable.blake2s" => {
let ast = load_ast(
r#"
circuit Blake2s {
function hash(seed: [u8; 32], message: [u8; 32]) -> [u8; 32] {
return [0; 32];
}
}
"#,
)?;
ast.set_core_mapping("blake2s");
Ok(Some(ast))
}
_ => Ok(None),
}
}

23
ast-passes/src/lib.rs Normal file
View File

@ -0,0 +1,23 @@
// Copyright (C) 2019-2021 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 <https://www.gnu.org/licenses/>.
#![doc = include_str!("../README.md")]
pub mod canonicalization;
pub use canonicalization::*;
pub mod import_resolution;
pub use import_resolution::*;

35
ast/src/aliases/alias.rs Normal file
View File

@ -0,0 +1,35 @@
// Copyright (C) 2019-2021 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 <https://www.gnu.org/licenses/>.
use crate::{Identifier, Type};
use leo_errors::Span;
use std::fmt;
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Alias {
pub name: Identifier,
pub span: Span,
pub represents: Type,
}
impl fmt::Display for Alias {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{} : {}", self.name.name, self.represents)
}
}

View File

@ -13,3 +13,6 @@
// You should have received a copy of the GNU General Public License
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
pub mod alias;
pub use self::alias::*;

View File

@ -22,6 +22,7 @@ use std::fmt;
#[derive(Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Circuit {
pub circuit_name: Identifier,
pub core_mapping: std::cell::RefCell<Option<String>>,
pub members: Vec<CircuitMember>,
}

View File

@ -0,0 +1,49 @@
// Copyright (C) 2019-2021 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 <https://www.gnu.org/licenses/>.
use crate::Program;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use indexmap::IndexMap;
#[allow(clippy::ptr_arg)]
pub fn serialize<S: Serializer>(
imported_modules: &IndexMap<Vec<String>, Program>,
serializer: S,
) -> Result<S::Ok, S::Error> {
let joined: IndexMap<String, Program> = imported_modules
.into_iter()
.map(|(package, program)| (package.join("."), program.clone()))
.collect();
joined.serialize(serializer)
}
pub fn deserialize<'de, D: Deserializer<'de>>(deserializer: D) -> Result<IndexMap<Vec<String>, Program>, D::Error> {
Ok(IndexMap::<String, Program>::deserialize(deserializer)?
.into_iter()
.map(|(package, program)| {
(
package
.split('.')
.map(|segment| segment.to_string())
.collect::<Vec<String>>(),
program,
)
})
.collect())
}

View File

@ -23,6 +23,9 @@ pub use const_self_keyword::*;
pub mod identifier;
pub use identifier::*;
pub mod imported_modules;
pub use imported_modules::*;
pub mod mut_self_keyword;
pub use mut_self_keyword::*;

View File

@ -45,7 +45,7 @@ impl fmt::Display for ValueExpression {
Char(character) => write!(f, "{}", character),
Field(field, _) => write!(f, "{}", field),
Implicit(implicit, _) => write!(f, "{}", implicit),
Integer(value, type_, _) => write!(f, "{}{}", value, type_),
Integer(type_, value, _) => write!(f, "{}{}", value, type_),
Group(group) => write!(f, "{}", group),
String(string, _) => {
for character in string.iter() {

View File

@ -22,6 +22,9 @@
#![doc = include_str!("../README.md")]
pub mod aliases;
pub use self::aliases::*;
pub mod annotation;
pub use self::annotation::*;
@ -49,6 +52,9 @@ pub use self::imports::*;
pub mod input;
pub use self::input::*;
pub mod pass;
pub use self::pass::*;
pub mod program;
pub use self::program::*;
@ -83,11 +89,17 @@ impl Ast {
Self { ast: program }
}
/* /// Mutates the program ast by resolving the imports.
pub fn importer<T: ImportResolver>(&mut self, importer: T) -> Result<()> {
self.ast = ReconstructingDirector::new(Importer::new(importer)).reduce_program(self.as_repr())?;
Ok(())
}
/// Mutates the program ast by preforming canonicalization on it.
pub fn canonicalize(&mut self) -> Result<()> {
self.ast = ReconstructingDirector::new(Canonicalizer::default()).reduce_program(self.as_repr())?;
Ok(())
}
} */
/// Returns a reference to the inner program AST representation.
pub fn as_repr(&self) -> &Program {

22
ast/src/pass.rs Normal file
View File

@ -0,0 +1,22 @@
// Copyright (C) 2019-2021 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 <https://www.gnu.org/licenses/>.
use crate::{Ast, Program};
use leo_errors::Result;
pub trait AstPass {
fn do_pass(asg: Program) -> Result<Ast>;
}

View File

@ -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::{Circuit, DefinitionStatement, Function, FunctionInput, Identifier, ImportStatement};
use crate::{Alias, Circuit, DefinitionStatement, Function, FunctionInput, Identifier, ImportStatement};
use indexmap::IndexMap;
use serde::{Deserialize, Serialize};
@ -28,7 +28,10 @@ use std::fmt;
pub struct Program {
pub name: String,
pub expected_input: Vec<FunctionInput>,
pub imports: Vec<ImportStatement>,
pub import_statements: Vec<ImportStatement>,
#[serde(with = "crate::common::imported_modules")]
pub imports: IndexMap<Vec<String>, Program>,
pub aliases: IndexMap<Identifier, Alias>,
pub circuits: IndexMap<Identifier, Circuit>,
pub global_consts: IndexMap<String, DefinitionStatement>,
pub functions: IndexMap<Identifier, Function>,
@ -42,7 +45,17 @@ impl AsRef<Program> for Program {
impl fmt::Display for Program {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
for import in self.imports.iter() {
for import in self.import_statements.iter() {
import.fmt(f)?;
writeln!(f,)?;
}
writeln!(f,)?;
for (_, alias) in self.aliases.iter() {
alias.fmt(f)?;
writeln!(f,)?;
}
writeln!(f,)?;
for (_, import) in self.imports.iter() {
import.fmt(f)?;
writeln!(f,)?;
}
@ -65,13 +78,21 @@ impl Program {
Self {
name,
expected_input: vec![],
imports: vec![],
import_statements: vec![],
imports: IndexMap::new(),
aliases: IndexMap::new(),
circuits: IndexMap::new(),
global_consts: IndexMap::new(),
functions: IndexMap::new(),
}
}
pub fn set_core_mapping(&self, mapping: &str) {
for (_, circuit) in self.circuits.iter() {
circuit.core_mapping.replace(Some(mapping.to_string()));
}
}
pub fn get_name(&self) -> String {
self.name.to_string()
}

View File

@ -14,11 +14,8 @@
// You should have received a copy of the GNU General Public License
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
mod canonicalization;
pub use canonicalization::*;
mod reconstructing_reducer;
pub mod reconstructing_reducer;
pub use reconstructing_reducer::*;
mod reconstructing_director;
pub mod reconstructing_director;
pub use reconstructing_director::*;

View File

@ -41,7 +41,7 @@ impl<R: ReconstructingReducer> ReconstructingDirector<R> {
Type::Tuple(reduced_types)
}
Type::Circuit(identifier) => Type::Circuit(self.reduce_identifier(identifier)?),
Type::CircuitOrAlias(identifier) => Type::CircuitOrAlias(self.reduce_identifier(identifier)?),
_ => type_.clone(),
};
@ -420,21 +420,40 @@ impl<R: ReconstructingReducer> ReconstructingDirector<R> {
inputs.push(self.reduce_function_input(input)?);
}
let mut imports = vec![];
for import in program.imports.iter() {
imports.push(self.reduce_import(import)?);
let mut import_statements = vec![];
for import in program.import_statements.iter() {
import_statements.push(self.reduce_import_statement(import)?);
}
let mut imports = IndexMap::new();
for (identifier, program) in program.imports.iter() {
let (ident, import) = self.reduce_import(identifier, program)?;
imports.insert(ident, import);
}
let mut aliases = IndexMap::new();
for (name, alias) in program.aliases.iter() {
let represents = self.reduce_type(&alias.represents, &alias.name.span)?;
aliases.insert(
name.clone(),
Alias {
name: alias.name.clone(),
span: alias.span.clone(),
represents,
},
);
}
let mut circuits = IndexMap::new();
self.reducer.swap_in_circuit();
for (identifier, circuit) in program.circuits.iter() {
circuits.insert(self.reduce_identifier(identifier)?, self.reduce_circuit(circuit)?);
for (name, circuit) in program.circuits.iter() {
circuits.insert(name.clone(), self.reduce_circuit(circuit)?);
}
self.reducer.swap_in_circuit();
let mut functions = IndexMap::new();
for (identifier, function) in program.functions.iter() {
functions.insert(self.reduce_identifier(identifier)?, self.reduce_function(function)?);
for (name, function) in program.functions.iter() {
functions.insert(name.clone(), self.reduce_function(function)?);
}
let mut global_consts = IndexMap::new();
@ -442,8 +461,16 @@ impl<R: ReconstructingReducer> ReconstructingDirector<R> {
global_consts.insert(name.clone(), self.reduce_definition(definition)?);
}
self.reducer
.reduce_program(program, inputs, imports, circuits, functions, global_consts)
self.reducer.reduce_program(
program,
inputs,
import_statements,
imports,
aliases,
circuits,
functions,
global_consts,
)
}
pub fn reduce_function_input_variable(
@ -484,10 +511,16 @@ impl<R: ReconstructingReducer> ReconstructingDirector<R> {
self.reducer.reduce_package_or_packages(package_or_packages, new)
}
pub fn reduce_import(&mut self, import: &ImportStatement) -> Result<ImportStatement> {
pub fn reduce_import_statement(&mut self, import: &ImportStatement) -> Result<ImportStatement> {
let package_or_packages = self.reduce_package_or_packages(&import.package_or_packages)?;
self.reducer.reduce_import(import, package_or_packages)
self.reducer.reduce_import_statement(import, package_or_packages)
}
pub fn reduce_import(&mut self, identifier: &[String], import: &Program) -> Result<(Vec<String>, Program)> {
let new_identifer = identifier.to_vec();
let new_import = self.reduce_program(import)?;
self.reducer.reduce_import(new_identifer, new_import)
}
pub fn reduce_circuit_member(&mut self, circuit_member: &CircuitMember) -> Result<CircuitMember> {

View File

@ -376,12 +376,15 @@ pub trait ReconstructingReducer {
})
}
#[allow(clippy::too_many_arguments)]
// Program
fn reduce_program(
&mut self,
program: &Program,
expected_input: Vec<FunctionInput>,
imports: Vec<ImportStatement>,
import_statements: Vec<ImportStatement>,
imports: IndexMap<Vec<String>, Program>,
aliases: IndexMap<Identifier, Alias>,
circuits: IndexMap<Identifier, Circuit>,
functions: IndexMap<Identifier, Function>,
global_consts: IndexMap<String, DefinitionStatement>,
@ -389,7 +392,9 @@ pub trait ReconstructingReducer {
Ok(Program {
name: program.name.clone(),
expected_input,
import_statements,
imports,
aliases,
circuits,
functions,
global_consts,
@ -423,7 +428,7 @@ pub trait ReconstructingReducer {
Ok(new)
}
fn reduce_import(
fn reduce_import_statement(
&mut self,
import: &ImportStatement,
package_or_packages: PackageOrPackages,
@ -434,17 +439,25 @@ pub trait ReconstructingReducer {
})
}
fn reduce_import(&mut self, identifier: Vec<String>, import: Program) -> Result<(Vec<String>, Program)> {
Ok((identifier, import))
}
fn reduce_circuit_member(&mut self, _circuit_member: &CircuitMember, new: CircuitMember) -> Result<CircuitMember> {
Ok(new)
}
fn reduce_circuit(
&mut self,
_circuit: &Circuit,
circuit: &Circuit,
circuit_name: Identifier,
members: Vec<CircuitMember>,
) -> Result<Circuit> {
Ok(Circuit { circuit_name, members })
Ok(Circuit {
circuit_name,
core_mapping: circuit.core_mapping.clone(),
members,
})
}
fn reduce_annotation(&mut self, annotation: &Annotation, name: Identifier) -> Result<Annotation> {

View File

@ -36,7 +36,7 @@ pub enum Type {
// Data type wrappers
Array(Box<Type>, ArrayDimensions),
Tuple(Vec<Type>),
Circuit(Identifier),
CircuitOrAlias(Identifier),
SelfType,
}
@ -52,7 +52,7 @@ impl Type {
/// Returns `true` if the self `Type` is a `Circuit`.
///
pub fn is_circuit(&self) -> bool {
matches!(self, Type::Circuit(_))
matches!(self, Type::CircuitOrAlias(_))
}
///
@ -68,7 +68,7 @@ impl Type {
(Type::Field, Type::Field) => true,
(Type::Group, Type::Group) => true,
(Type::IntegerType(left), Type::IntegerType(right)) => left.eq(right),
(Type::Circuit(left), Type::Circuit(right)) => left.eq(right),
(Type::CircuitOrAlias(left), Type::CircuitOrAlias(right)) => left.eq(right),
(Type::SelfType, Type::SelfType) => true,
(Type::Array(left_type, left_dim), Type::Array(right_type, right_dim)) => {
// Convert array dimensions to owned.
@ -151,7 +151,7 @@ impl fmt::Display for Type {
Type::Field => write!(f, "field"),
Type::Group => write!(f, "group"),
Type::IntegerType(ref integer_type) => write!(f, "{}", integer_type),
Type::Circuit(ref variable) => write!(f, "circuit {}", variable),
Type::CircuitOrAlias(ref variable) => write!(f, "circuit {}", variable),
Type::SelfType => write!(f, "SelfType"),
Type::Array(ref array, ref dimensions) => write!(f, "[{}; {}]", *array, dimensions),
Type::Tuple(ref tuple) => {

View File

@ -53,6 +53,10 @@ version = "1.5.3"
path = "../asg-passes"
version = "1.5.3"
[dependencies.leo-ast-passes]
path = "../ast-passes"
version = "1.5.3"
[dependencies.leo-synthesizer]
path = "../synthesizer"
version = "1.5.3"

View File

@ -21,7 +21,7 @@ use crate::{
};
pub use leo_asg::{new_context, AsgContext as Context, AsgContext};
use leo_asg::{Asg, AsgPass, Program as AsgProgram};
use leo_ast::{Input, MainInput, Program as AstProgram};
use leo_ast::{AstPass, Input, MainInput, Program as AstProgram};
use leo_errors::{CompilerError, Result};
use leo_imports::ImportParser;
use leo_input::LeoInputParser;
@ -248,8 +248,18 @@ impl<'a, F: PrimeField, G: GroupType<F>> Compiler<'a, F, G> {
ast.to_json_file(self.output_directory.clone(), "initial_ast.json")?;
}
// Perform canonicalization of AST always.
ast.canonicalize()?;
// Preform import resolution.
ast = leo_ast_passes::Importer::do_pass(
ast.into_repr(),
ImportParser::new(self.main_file_path.clone(), self.imports_map.clone()),
)?;
if self.ast_snapshot_options.imports_resolved {
ast.to_json_file(self.output_directory.clone(), "imports_resolved.json")?;
}
// Preform canonicalization of AST always.
ast = leo_ast_passes::Canonicalizer::do_pass(ast.into_repr())?;
if self.ast_snapshot_options.canonicalized {
ast.to_json_file(self.output_directory.clone(), "canonicalization_ast.json")?;
@ -262,11 +272,7 @@ impl<'a, F: PrimeField, G: GroupType<F>> Compiler<'a, F, G> {
tracing::debug!("Program parsing complete\n{:#?}", self.program);
// Create a new symbol table from the program, imported_programs, and program_input.
let asg = Asg::new(
self.context,
&self.program,
&mut ImportParser::new(self.main_file_path.clone(), self.imports_map.clone()),
)?;
let asg = Asg::new(self.context, &self.program)?;
if self.ast_snapshot_options.type_inferenced {
let new_ast = TypeInferencePhase::default()

View File

@ -1,167 +0,0 @@
// Copyright (C) 2019-2021 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 <https://www.gnu.org/licenses/>.
use crate::errors::{
AddressError, BooleanError, CharError, FieldError, FunctionError, GroupError, IntegerError, ValueError,
};
use leo_ast::{FormattedError, Identifier, LeoError, Span};
use snarkvm_r1cs::SynthesisError;
#[derive(Debug, Error)]
pub enum ExpressionError {
#[error("{}", _0)]
AddressError(#[from] AddressError),
#[error("{}", _0)]
BooleanError(#[from] BooleanError),
#[error("{}", _0)]
CharError(#[from] CharError),
#[error("{}", _0)]
Error(#[from] FormattedError),
#[error("{}", _0)]
FieldError(#[from] FieldError),
#[error("{}", _0)]
FunctionError(#[from] Box<FunctionError>),
#[error("{}", _0)]
GroupError(#[from] GroupError),
#[error("{}", _0)]
IntegerError(#[from] IntegerError),
#[error("{}", _0)]
ValueError(#[from] ValueError),
}
impl LeoError for ExpressionError {}
impl ExpressionError {
fn new_from_span(message: String, span: &Span) -> Self {
ExpressionError::Error(FormattedError::new_from_span(message, span))
}
pub fn cannot_enforce(operation: String, error: SynthesisError, span: &Span) -> Self {
let message = format!(
"the gadget operation `{}` failed due to synthesis error `{:?}`",
operation, error,
);
Self::new_from_span(message, span)
}
pub fn cannot_evaluate(operation: String, span: &Span) -> Self {
let message = format!("Mismatched types found for operation `{}`", operation);
Self::new_from_span(message, span)
}
pub fn array_length_out_of_bounds(span: &Span) -> Self {
let message = "array length cannot be >= 2^32".to_string();
Self::new_from_span(message, span)
}
pub fn array_index_out_of_legal_bounds(span: &Span) -> Self {
let message = "array index cannot be >= 2^32".to_string();
Self::new_from_span(message, span)
}
pub fn conditional_boolean(actual: String, span: &Span) -> Self {
let message = format!("if, else conditional must resolve to a boolean, found `{}`", actual);
Self::new_from_span(message, span)
}
pub fn expected_circuit_member(expected: String, span: &Span) -> Self {
let message = format!("expected circuit member `{}`, not found", expected);
Self::new_from_span(message, span)
}
pub fn incompatible_types(operation: String, span: &Span) -> Self {
let message = format!("no implementation for `{}`", operation);
Self::new_from_span(message, span)
}
pub fn tuple_index_out_of_bounds(index: usize, span: &Span) -> Self {
let message = format!("cannot access index {} of tuple out of bounds", index);
Self::new_from_span(message, span)
}
pub fn array_index_out_of_bounds(index: usize, span: &Span) -> Self {
let message = format!("cannot access index {} of array out of bounds", index);
Self::new_from_span(message, span)
}
pub fn array_invalid_slice_length(span: &Span) -> Self {
let message = "illegal length of slice".to_string();
Self::new_from_span(message, span)
}
pub fn invalid_index(actual: String, span: &Span) -> Self {
let message = format!("index must resolve to an integer, found `{}`", actual);
Self::new_from_span(message, span)
}
pub fn invalid_length(expected: usize, actual: usize, span: &Span) -> Self {
let message = format!("expected array length {}, found one with length {}", expected, actual);
Self::new_from_span(message, span)
}
pub fn invalid_static_access(member: String, span: &Span) -> Self {
let message = format!("static member `{}` must be accessed using `::` syntax", member);
Self::new_from_span(message, span)
}
pub fn undefined_array(actual: String, span: &Span) -> Self {
let message = format!("array `{}` must be declared before it is used in an expression", actual);
Self::new_from_span(message, span)
}
pub fn undefined_circuit(actual: String, span: &Span) -> Self {
let message = format!(
"circuit `{}` must be declared before it is used in an expression",
actual
);
Self::new_from_span(message, span)
}
pub fn undefined_identifier(identifier: Identifier) -> Self {
let message = format!("Cannot find value `{}` in this scope", identifier.name);
Self::new_from_span(message, &identifier.span)
}
pub fn undefined_member_access(circuit: String, member: String, span: &Span) -> Self {
let message = format!("Circuit `{}` has no member `{}`", circuit, member);
Self::new_from_span(message, span)
}
}

View File

@ -1,138 +0,0 @@
// Copyright (C) 2019-2021 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 <https://www.gnu.org/licenses/>.
use crate::errors::{
AddressError, BooleanError, CharError, ExpressionError, FieldError, GroupError, IntegerError, OutputBytesError,
StatementError, ValueError,
};
use leo_asg::AsgConvertError;
use leo_ast::{FormattedError, LeoError, Span};
#[derive(Debug, Error)]
pub enum FunctionError {
#[error("{}", _0)]
AddressError(#[from] AddressError),
#[error("{}", _0)]
BooleanError(#[from] BooleanError),
#[error("{}", _0)]
CharError(#[from] CharError),
#[error("{}", _0)]
ExpressionError(#[from] ExpressionError),
#[error("{}", _0)]
Error(#[from] FormattedError),
#[error("{}", _0)]
FieldError(#[from] FieldError),
#[error("{}", _0)]
GroupError(#[from] GroupError),
#[error("{}", _0)]
IntegerError(#[from] IntegerError),
#[error("{}", _0)]
OutputStringError(#[from] OutputBytesError),
#[error("{}", _0)]
StatementError(#[from] StatementError),
#[error("{}", _0)]
ValueError(#[from] ValueError),
#[error("{}", _0)]
ImportASGError(#[from] AsgConvertError),
}
impl LeoError for FunctionError {}
impl FunctionError {
fn new_from_span(message: String, span: &Span) -> Self {
FunctionError::Error(FormattedError::new_from_span(message, span))
}
pub fn input_type_mismatch(expected: String, actual: String, variable: String, span: &Span) -> Self {
let message = format!(
"Expected input variable `{}` to be type `{}`, found type `{}`",
variable, expected, actual
);
Self::new_from_span(message, span)
}
pub fn expected_const_input(variable: String, span: &Span) -> Self {
let message = format!(
"Expected input variable `{}` to be constant. Move input variable `{}` to [constants] section of input file",
variable, variable
);
Self::new_from_span(message, span)
}
pub fn expected_non_const_input(variable: String, span: &Span) -> Self {
let message = format!(
"Expected input variable `{}` to be non-constant. Move input variable `{}` to [main] section of input file",
variable, variable
);
Self::new_from_span(message, span)
}
pub fn invalid_array(actual: String, span: &Span) -> Self {
let message = format!("Expected function input array, found `{}`", actual);
Self::new_from_span(message, span)
}
pub fn invalid_input_array_dimensions(expected: usize, actual: usize, span: &Span) -> Self {
let message = format!(
"Input array dimensions mismatch expected {}, found array dimensions {}",
expected, actual
);
Self::new_from_span(message, span)
}
pub fn tuple_size_mismatch(expected: usize, actual: usize, span: &Span) -> Self {
let message = format!(
"Input tuple size mismatch expected {}, found tuple with length {}",
expected, actual
);
Self::new_from_span(message, span)
}
pub fn invalid_tuple(actual: String, span: &Span) -> Self {
let message = format!("Expected function input tuple, found `{}`", actual);
Self::new_from_span(message, span)
}
pub fn input_not_found(expected: String, span: &Span) -> Self {
let message = format!("main function input {} not found", expected);
Self::new_from_span(message, span)
}
pub fn double_input_declaration(input_name: String, span: &Span) -> Self {
let message = format!("Input variable {} declared twice", input_name);
Self::new_from_span(message, span)
}
}

View File

@ -38,6 +38,7 @@ impl Default for CompilerOptions {
#[derive(Clone)]
pub struct AstSnapshotOptions {
pub initial: bool,
pub imports_resolved: bool,
pub canonicalized: bool,
pub type_inferenced: bool,
}
@ -46,6 +47,7 @@ impl Default for AstSnapshotOptions {
fn default() -> Self {
Self {
initial: false,
imports_resolved: false,
canonicalized: false,
type_inferenced: false,
}

View File

@ -14,8 +14,13 @@
// You should have received a copy of the GNU General Public License
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
use leo_asg::Program;
use leo_asg::Program as AsgProgram;
use leo_ast::Program as AstProgram;
pub trait ASGPhase {
fn apply(asg: &mut Program);
fn apply(asg: &mut AsgProgram);
}
pub trait ASTPhase {
fn apply(asg: &mut AstProgram);
}

View File

@ -95,6 +95,7 @@ impl<R: ReconstructingReducer, O: CombinerOptions> CombineAstAsgDirector<R, O> {
AstType::Tuple(reduced_types)
}
_ if self.options.type_inference_enabled() => asg.into(),
_ => ast.clone(),
};
@ -264,11 +265,8 @@ impl<R: ReconstructingReducer, O: CombinerOptions> CombineAstAsgDirector<R, O> {
ast: &CircuitMemberAccessExpression,
asg: &AsgCircuitAccessExpression,
) -> Result<CircuitMemberAccessExpression> {
// let circuit = self.reduce_expression(&circuit_member_access.circuit)?;
// let name = self.reduce_identifier(&circuit_member_access.name)?;
// let target = input.target.get().map(|e| self.reduce_expression(e));
let type_ = if self.options.type_inference_enabled() {
Some(leo_ast::Type::Circuit(asg.circuit.get().name.borrow().clone()))
Some(leo_ast::Type::CircuitOrAlias(asg.circuit.get().name.borrow().clone()))
} else {
None
};
@ -282,10 +280,6 @@ impl<R: ReconstructingReducer, O: CombinerOptions> CombineAstAsgDirector<R, O> {
ast: &CircuitStaticFunctionAccessExpression,
_asg: &AsgCircuitAccessExpression,
) -> Result<CircuitStaticFunctionAccessExpression> {
// let circuit = self.reduce_expression(&circuit_member_access.circuit)?;
// let name = self.reduce_identifier(&circuit_member_access.name)?;
// let target = input.target.get().map(|e| self.reduce_expression(e));
self.ast_reducer
.reduce_circuit_static_fn_access(ast, *ast.circuit.clone(), ast.name.clone())
}
@ -635,6 +629,11 @@ impl<R: ReconstructingReducer, O: CombinerOptions> CombineAstAsgDirector<R, O> {
}
pub fn reduce_program(&mut self, ast: &leo_ast::Program, asg: &leo_asg::Program) -> Result<leo_ast::Program> {
let mut imports = IndexMap::new();
for ((ast_ident, ast_program), (_asg_ident, asg_program)) in ast.imports.iter().zip(&asg.imported_modules) {
imports.insert(ast_ident.clone(), self.reduce_program(ast_program, asg_program)?);
}
self.ast_reducer.swap_in_circuit();
let mut circuits = IndexMap::new();
for ((ast_ident, ast_circuit), (_asg_ident, asg_circuit)) in ast.circuits.iter().zip(&asg.circuits) {
@ -656,7 +655,9 @@ impl<R: ReconstructingReducer, O: CombinerOptions> CombineAstAsgDirector<R, O> {
self.ast_reducer.reduce_program(
ast,
ast.expected_input.clone(),
ast.imports.clone(),
ast.import_statements.clone(),
imports,
ast.aliases.clone(),
circuits,
functions,
global_consts,

View File

@ -92,6 +92,7 @@ struct CompileOutput {
pub circuit: SummarizedCircuit,
pub output: Vec<OutputItem>,
pub initial_ast: String,
pub imports_resolved_ast: String,
pub canonicalized_ast: String,
pub type_inferenced_ast: String,
}
@ -117,6 +118,7 @@ impl Namespace for CompileNamespace {
&test.content,
Some(AstSnapshotOptions {
initial: true,
imports_resolved: true,
canonicalized: true,
type_inferenced: true,
}),
@ -213,19 +215,25 @@ impl Namespace for CompileNamespace {
let initial_ast: String = hash(
Ast::from_json_file("/tmp/output/initial_ast.json".into())
.unwrap_or_else(|_| Ast::new(Program::new("Error reading initial theorem.".to_string())))
.unwrap_or_else(|_| Ast::new(Program::new("Error reading initial snapshot.".to_string())))
.to_json_string()
.unwrap_or_else(|_| "Error converting ast to string.".to_string()),
);
let imports_resolved_ast: String = hash(
Ast::from_json_file("/tmp/output/imports_resolved_ast.json".into())
.unwrap_or_else(|_| Ast::new(Program::new("Error reading imports resolved snapshot.".to_string())))
.to_json_string()
.unwrap_or_else(|_| "Error converting ast to string.".to_string()),
);
let canonicalized_ast: String = hash(
Ast::from_json_file("/tmp/output/canonicalization_ast.json".into())
.unwrap_or_else(|_| Ast::new(Program::new("Error reading canonicalized theorem.".to_string())))
.unwrap_or_else(|_| Ast::new(Program::new("Error reading canonicalized snapshot.".to_string())))
.to_json_string()
.unwrap_or_else(|_| "Error converting ast to string.".to_string()),
);
let type_inferenced_ast = hash(
Ast::from_json_file("/tmp/output/type_inferenced_ast.json".into())
.unwrap_or_else(|_| Ast::new(Program::new("Error reading type inferenced theorem.".to_string())))
.unwrap_or_else(|_| Ast::new(Program::new("Error reading type inferenced snapshot.".to_string())))
.to_json_string()
.unwrap_or_else(|_| "Error converting ast to string.".to_string()),
);
@ -238,6 +246,7 @@ impl Namespace for CompileNamespace {
circuit: last_circuit.unwrap(),
output: output_items,
initial_ast,
imports_resolved_ast,
canonicalized_ast,
type_inferenced_ast,
};

View File

@ -33,14 +33,6 @@ create_errors!(
help: None,
}
/// For when a import of the specified name is unresolved.
@formatted
unresolved_import {
args: (name: impl Display),
msg: format!("failed to resolve import: '{}'", name),
help: None,
}
/// For when a circuit member of the specified name is unresolved.
@formatted
unresolved_circuit_member {
@ -183,7 +175,7 @@ create_errors!(
help: None,
}
/// For when a user defines function with the same name twice.
/// For when a user defines a function with the same name twice.
@formatted
duplicate_function_definition {
args: (name: impl Display),
@ -411,4 +403,44 @@ create_errors!(
msg: "received a Self statement, which should never happen.",
help: Some("Something went wrong during canonicalization, or you ran the ASG on an uncanonicalized AST.".to_string()),
}
/// For when a import of the specified name is unresolved.
@formatted
unresolved_import {
args: (name: impl Display),
msg: format!("failed to resolve import: '{}'", name),
help: None,
}
/// For when a user defines an alias with the same name twice.
@formatted
duplicate_alias_definition {
args: (name: impl Display),
msg: format!("a alias named \"{}\" already exists in this scope", name),
help: None,
}
/// For when a user defines a function input with the same name twice.
@formatted
duplicate_function_input_definition {
args: (name: impl Display),
msg: format!("a function input named \"{}\" already exists in this scope", name),
help: None,
}
/// For when a user defines a global const with the same name twice.
@formatted
duplicate_global_const_definition {
args: (name: impl Display),
msg: format!("a global const named \"{}\" already exists in this scope", name),
help: None,
}
/// For when a named identifier is being shadowed.
@formatted
cannot_shadow_name {
args: (type_: impl Display, name: impl Display, location: impl Display),
msg: format!("a {} cannot be named `{}` as a {} with that name already exists in this scope", type_, name, location),
help: None,
}
);

View File

@ -15,7 +15,10 @@
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
use crate::create_errors;
use std::{error::Error as ErrorArg, fmt::Debug};
use std::{
error::Error as ErrorArg,
fmt::{Debug, Display},
};
create_errors!(
/// AstError enum that represents all the errors for the `leo-ast` crate.
@ -102,4 +105,21 @@ create_errors!(
msg: "Console::Assert cannot be matched here, its handled in another case.",
help: None,
}
/// This error is for when a user tries to use the library and programatically inject an import
/// on the rust side.
@backtraced
injected_programs {
args: (injected_import_count: impl Display),
msg: format!("It seems the AST has {} injected imports. This is unexpected please import the library naturally", injected_import_count),
help: None,
}
/// For when a import of the specified name is unresolved.
@formatted
unresolved_import {
args: (name: impl Display),
msg: format!("failed to resolve import: '{}'", name),
help: None,
}
);

View File

@ -78,6 +78,7 @@ impl Serialize for Span {
state.serialize_field("line_stop", &self.line_stop)?;
state.serialize_field("col_start", &self.col_start)?;
state.serialize_field("col_stop", &self.col_stop)?;
// This is for testing purposes since the tests are run on a variety of OSes.
if std::env::var("LEO_TESTFRAMEWORK")
.unwrap_or_default()
.trim()

View File

@ -130,29 +130,3 @@ impl LeoError {
/// A global result type for all Leo crates, that defaults the errors to be a LeoError.
pub type Result<T, E = LeoError> = core::result::Result<T, E>;
// #[test]
// fn test_error() {
// let err = FormattedError {
// path: std::sync::Arc::new("file.leo".to_string()),
// line_start: 2,
// line_stop: 2,
// col_start: 9,
// col_stop: 10,
// content: "let a = x;".into(),
// message: "undefined value `x`".to_string(),
// };
// assert_eq!(
// err.to_string(),
// vec![
// " --> file.leo:2:9",
// " |",
// " 2 | let a = x;",
// " | ^",
// " |",
// " = undefined value `x`",
// ]
// .join("\n")
// );
// }

View File

@ -27,9 +27,9 @@ create_errors!(
/// For when the parser encountered an unexpected token.
@formatted
unexpected_token {
args: (message: impl Display, help: String),
args: (message: impl Display),
msg: message,
help: Some(help),
help: None,
}
/// For when the parser encoutnered an invalid address literal.

Binary file not shown.

View File

@ -395,6 +395,7 @@ keyword = %s"address"
/ %s"console"
/ %s"const"
/ %s"else"
/ %s"false"
/ %s"field"
/ %s"for"
/ %s"function"
@ -415,6 +416,8 @@ keyword = %s"address"
/ %s"self"
/ %s"static"
/ %s"string"
/ %s"true"
/ %s"type"
/ %s"u8"
/ %s"u16"
/ %s"u32"
@ -664,7 +667,7 @@ scalar-type = boolean-type / arithmetic-type / address-type / character-type
self-type = %s"Self"
circuit-type = identifier / self-type
circuit-or-alias-type = identifier / self-type
; A tuple type consists of zero, two, or more component types.
@ -680,15 +683,9 @@ array-type = "[" type ";" array-dimensions "]"
array-dimensions = natural
/ "(" natural *( "," natural ) ")"
; Circuit, tuple, and array types form the aggregate types,
; i.e. types whose values contain (sub-)values
; (with the corner-case exception of the empty tuple value).
; Scalar and the remaining types form all the types.
aggregate-type = tuple-type / array-type / circuit-type
; Scalar and aggregate types form all the types.
type = scalar-type / aggregate-type
type = scalar-type / tuple-type / array-type / circuit-or-alias-type
; The lexical grammar given earlier defines product group literals.
; The other kind of group literal is a pair of integer coordinates,
@ -774,7 +771,7 @@ array-expression = array-construction
; while the right one denotes an expression (a variable),
; so they are syntactically identical but semantically different.
circuit-construction = circuit-type "{"
circuit-construction = circuit-or-alias-type "{"
circuit-inline-element
*( "," circuit-inline-element ) [ "," ]
"}"
@ -810,7 +807,7 @@ postfix-expression = primary-expression
/ postfix-expression "." identifier
/ identifier function-arguments
/ postfix-expression "." identifier function-arguments
/ circuit-type "::" identifier function-arguments
/ circuit-or-alias-type "::" identifier function-arguments
/ postfix-expression "[" expression "]"
/ postfix-expression "[" [expression] ".." [expression] "]"
@ -1052,6 +1049,12 @@ package-path = "*"
/ package-name "." package-path
/ "(" package-path *( "," package-path ) [","] ")"
; A type declaration consists of the `type` keyword
; followed by an identifier and a type that the alias
; would refer to.
type-alias-declaration = %s"type" identifier "=" type ";"
; Finally, we define a file as a sequence of zero or more declarations.
; We allow constant declarations at the top level, for global constants.
; Currently variable declarations are disallowed at the top level.
@ -1060,6 +1063,7 @@ declaration = import-declaration
/ function-declaration
/ circuit-declaration
/ constant-declaration
/ type-alias-declaration
file = *declaration

View File

@ -21,6 +21,10 @@ edition = "2018"
path = "../ast"
version = "1.5.3"
[dependencies.leo-ast-passes]
path = "../ast-passes"
version = "1.5.3"
[dependencies.leo-errors]
path = "../errors"
version = "1.5.3"

View File

@ -14,7 +14,8 @@
// You should have received a copy of the GNU General Public License
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
use leo_asg::{AsgContext, ImportResolver, Program};
use leo_ast::Program;
use leo_ast_passes::ImportResolver;
use leo_errors::{ImportError, LeoError, Result, Span};
use indexmap::{IndexMap, IndexSet};
@ -25,14 +26,14 @@ use std::{collections::HashMap, path::PathBuf};
/// A program can import one or more packages. A package can be found locally in the source
/// directory, foreign in the imports directory, or part of the core package list.
#[derive(Clone, Default)]
pub struct ImportParser<'a> {
pub struct ImportParser {
program_path: PathBuf,
partial_imports: IndexSet<String>,
imports: IndexMap<String, Program<'a>>,
imports: IndexMap<String, Program>,
pub imports_map: HashMap<String, String>,
}
impl<'a> ImportParser<'a> {
impl ImportParser {
pub fn new(program_path: PathBuf, imports_map: HashMap<String, String>) -> Self {
ImportParser {
program_path,
@ -43,13 +44,8 @@ impl<'a> ImportParser<'a> {
}
}
impl<'a> ImportResolver<'a> for ImportParser<'a> {
fn resolve_package(
&mut self,
context: AsgContext<'a>,
package_segments: &[&str],
span: &Span,
) -> Result<Option<Program<'a>>> {
impl ImportResolver for ImportParser {
fn resolve_package(&mut self, package_segments: &[&str], span: &Span) -> Result<Option<Program>> {
let full_path = package_segments.join(".");
if self.partial_imports.contains(&full_path) {
return Err(ImportError::recursive_imports(full_path, span).into());
@ -61,7 +57,7 @@ impl<'a> ImportResolver<'a> for ImportParser<'a> {
self.partial_imports.insert(full_path.clone());
let mut imports = self.clone(); // Self::default() was previously
let program = imports
.parse_package(context, path, package_segments, span)
.parse_package(path, package_segments, span)
.map_err(|x| -> LeoError { x })?;
self.partial_imports.remove(&full_path);
self.imports.insert(full_path, program.clone());

View File

@ -15,7 +15,7 @@
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
use crate::ImportParser;
use leo_asg::{AsgContext, Program};
use leo_ast::Program;
use leo_errors::{ImportError, Result, Span};
use std::{fs, fs::DirEntry, path::PathBuf};
@ -24,21 +24,18 @@ static SOURCE_FILE_EXTENSION: &str = ".leo";
static SOURCE_DIRECTORY_NAME: &str = "src/";
static IMPORTS_DIRECTORY_NAME: &str = "imports/";
impl<'a> ImportParser<'a> {
impl ImportParser {
fn parse_package_access(
&mut self,
context: AsgContext<'a>,
package: &DirEntry,
remaining_segments: &[&str],
span: &Span,
) -> Result<Program<'a>> {
) -> Result<Program> {
if !remaining_segments.is_empty() {
return self.parse_package(context, package.path(), remaining_segments, span);
return self.parse_package(package.path(), remaining_segments, span);
}
let program = Self::parse_import_file(package, span)?;
let asg = leo_asg::Program::new(context, &program, self)?;
Ok(asg)
Self::parse_import_file(package, span)
}
///
@ -46,13 +43,7 @@ impl<'a> ImportParser<'a> {
///
/// Inserts the Leo syntax tree into the `ImportParser`.
///
pub(crate) fn parse_package(
&mut self,
context: AsgContext<'a>,
mut path: PathBuf,
segments: &[&str],
span: &Span,
) -> Result<Program<'a>> {
pub(crate) fn parse_package(&mut self, mut path: PathBuf, segments: &[&str], span: &Span) -> Result<Program> {
let error_path = path.clone();
let package_name = segments[0];
@ -121,14 +112,14 @@ impl<'a> ImportParser<'a> {
// Check if the package name was found in both the source and imports directory.
match (matched_source_entry, matched_import_entry) {
(Some(_), Some(_)) => Err(ImportError::conflicting_imports(package_name, span).into()),
(Some(source_entry), None) => self.parse_package_access(context, &source_entry, &segments[1..], span),
(None, Some(import_entry)) => self.parse_package_access(context, &import_entry, &segments[1..], span),
(Some(source_entry), None) => self.parse_package_access(&source_entry, &segments[1..], span),
(None, Some(import_entry)) => self.parse_package_access(&import_entry, &segments[1..], span),
(None, None) => Err(ImportError::unknown_package(package_name, span).into()),
}
} else {
// Enforce local package access with no found imports directory
match matched_source_entry {
Some(source_entry) => self.parse_package_access(context, &source_entry, &segments[1..], span),
Some(source_entry) => self.parse_package_access(&source_entry, &segments[1..], span),
None => Err(ImportError::unknown_package(package_name, span).into()),
}
}

View File

@ -22,7 +22,7 @@ use std::fs::DirEntry;
static MAIN_FILE: &str = "src/main.leo";
impl<'a> ImportParser<'a> {
impl ImportParser {
///
/// Returns a Leo syntax tree from a given package.
///
@ -54,8 +54,6 @@ impl<'a> ImportParser<'a> {
&std::fs::read_to_string(&file_path).map_err(|x| ImportError::io_error(file_path_str, x, span))?;
let mut program = leo_parser::parse(file_path_str, program_string)?;
program.name = file_name;
let mut ast = leo_ast::Ast::new(program);
ast.canonicalize()?;
Ok(ast.into_repr())
Ok(program)
}
}

View File

@ -47,6 +47,8 @@ pub struct BuildOptions {
pub enable_all_ast_snapshots: bool,
#[structopt(long, help = "Writes AST snapshot of the initial parse.")]
pub enable_initial_ast_snapshot: bool,
#[structopt(long, help = "Writes AST snapshot after the import resolution phase.")]
pub enable_imports_resolved_ast_snapshot: bool,
#[structopt(long, help = "Writes AST snapshot after the canonicalization phase.")]
pub enable_canonicalized_ast_snapshot: bool,
#[structopt(long, help = "Writes AST snapshot after the type inference phase.")]
@ -61,6 +63,7 @@ impl Default for BuildOptions {
disable_all_optimizations: false,
enable_all_ast_snapshots: false,
enable_initial_ast_snapshot: false,
enable_imports_resolved_ast_snapshot: false,
enable_canonicalized_ast_snapshot: false,
enable_type_inferenced_ast_snapshot: false,
}
@ -88,12 +91,14 @@ impl From<BuildOptions> for AstSnapshotOptions {
if options.enable_all_ast_snapshots {
AstSnapshotOptions {
initial: true,
imports_resolved: true,
canonicalized: true,
type_inferenced: true,
}
} else {
AstSnapshotOptions {
initial: options.enable_initial_ast_snapshot,
imports_resolved: options.enable_imports_resolved_ast_snapshot,
canonicalized: options.enable_canonicalized_ast_snapshot,
type_inferenced: options.enable_type_inferenced_ast_snapshot,
}

View File

@ -1,58 +0,0 @@
// Copyright (C) 2019-2021 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 <https://www.gnu.org/licenses/>.
use leo_ast::{FormattedError, LeoError, Span};
#[derive(Debug, Error)]
pub enum DeprecatedError {
#[error("{}", _0)]
Error(#[from] FormattedError),
}
impl DeprecatedError {
fn new_from_span(message: String, span: &Span) -> Self {
DeprecatedError::Error(FormattedError::new_from_span(message, span))
}
}
impl LeoError for DeprecatedError {}
impl DeprecatedError {
pub fn mut_function_input(mut span: Span) -> Self {
let message =
"function func(mut a: u32) { ... } is deprecated. Passed variables are mutable by default.".to_string();
span.col_start -= 1;
span.col_stop -= 1;
Self::new_from_span(message, &span)
}
pub fn let_mut_statement(mut span: Span) -> Self {
let message = "let mut = ... is deprecated. `let` keyword implies mutabality by default.".to_string();
span.col_start -= 1;
span.col_stop -= 1;
Self::new_from_span(message, &span)
}
pub fn test_function(span: &Span) -> Self {
let message = "\"test function...\" is deprecated. Did you mean @test annotation?".to_string();
Self::new_from_span(message, span)
}
pub fn context_annotation(span: &Span) -> Self {
let message = "\"@context(...)\" is deprecated. Did you mean @test annotation?".to_string();
Self::new_from_span(message, span)
}
}

Some files were not shown because too many files have changed in this diff Show More