Skip to content

Commit

Permalink
Merge branch 'master' into feature/515-eliminate-const-vars
Browse files Browse the repository at this point in the history
  • Loading branch information
collinc97 committed Feb 18, 2021
2 parents b96dc78 + 6715f91 commit 9baa938
Show file tree
Hide file tree
Showing 157 changed files with 3,016 additions and 2,324 deletions.
11 changes: 9 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ members = [
"input",
"linter",
"package",
"state"
"state",
]

[dependencies.leo-ast]
Expand Down
3 changes: 3 additions & 0 deletions asg/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,8 @@ features = [ "v4", "serde" ]
[dependencies.num-bigint]
version = "0.3"

[dependencies.typed-arena]
version = "2.0"

[dev-dependencies.criterion]
version = "0.3"
10 changes: 4 additions & 6 deletions asg/src/checks/return_path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ use crate::{
Span,
};

use std::sync::Arc;

pub struct ReturnPathReducer {
pub errors: Vec<(Span, String)>,
}
Expand All @@ -48,14 +46,14 @@ impl Default for ReturnPathReducer {
}

#[allow(unused_variables)]
impl MonoidalReducerExpression<BoolAnd> for ReturnPathReducer {
fn reduce_expression(&mut self, input: &Arc<Expression>, value: BoolAnd) -> BoolAnd {
impl<'a> MonoidalReducerExpression<'a, BoolAnd> for ReturnPathReducer {
fn reduce_expression(&mut self, input: &'a Expression<'a>, value: BoolAnd) -> BoolAnd {
BoolAnd(false)
}
}

#[allow(unused_variables)]
impl MonoidalReducerStatement<BoolAnd> for ReturnPathReducer {
impl<'a> MonoidalReducerStatement<'a, BoolAnd> for ReturnPathReducer {
fn reduce_assign_access(&mut self, input: &AssignAccess, left: Option<BoolAnd>, right: Option<BoolAnd>) -> BoolAnd {
BoolAnd(false)
}
Expand All @@ -69,7 +67,7 @@ impl MonoidalReducerStatement<BoolAnd> for ReturnPathReducer {
BoolAnd(false)
} else if let Some(index) = statements[..statements.len() - 1].iter().map(|x| x.0).position(|x| x) {
self.record_error(
input.statements[index].span(),
input.statements[index].get().span(),
"dead code due to unconditional early return".to_string(),
);
BoolAnd(true)
Expand Down
4 changes: 2 additions & 2 deletions asg/src/const_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ impl ConstInt {
}
}

pub fn get_type(&self) -> Type {
pub fn get_type<'a>(&self) -> Type<'a> {
Type::Integer(self.get_int_type())
}

Expand All @@ -247,7 +247,7 @@ impl ConstInt {
}

impl ConstValue {
pub fn get_type(&self) -> Option<Type> {
pub fn get_type<'a>(&self) -> Option<Type<'a>> {
Some(match self {
ConstValue::Int(i) => i.get_type(),
ConstValue::Group(_) => Type::Group,
Expand Down
69 changes: 33 additions & 36 deletions asg/src/expression/array_access.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,56 +17,53 @@
use crate::{AsgConvertError, ConstValue, Expression, ExpressionNode, FromAst, Node, PartialType, Scope, Span, Type};
use leo_ast::IntegerType;

use std::{
cell::RefCell,
sync::{Arc, Weak},
};

#[derive(Debug)]
pub struct ArrayAccessExpression {
pub parent: RefCell<Option<Weak<Expression>>>,
use std::cell::Cell;

#[derive(Clone)]
pub struct ArrayAccessExpression<'a> {
pub parent: Cell<Option<&'a Expression<'a>>>,
pub span: Option<Span>,
pub array: Arc<Expression>,
pub index: Arc<Expression>,
pub array: Cell<&'a Expression<'a>>,
pub index: Cell<&'a Expression<'a>>,
}

impl Node for ArrayAccessExpression {
impl<'a> Node for ArrayAccessExpression<'a> {
fn span(&self) -> Option<&Span> {
self.span.as_ref()
}
}

impl ExpressionNode for ArrayAccessExpression {
fn set_parent(&self, parent: Weak<Expression>) {
impl<'a> ExpressionNode<'a> for ArrayAccessExpression<'a> {
fn set_parent(&self, parent: &'a Expression<'a>) {
self.parent.replace(Some(parent));
}

fn get_parent(&self) -> Option<Arc<Expression>> {
self.parent.borrow().as_ref().map(Weak::upgrade).flatten()
fn get_parent(&self) -> Option<&'a Expression<'a>> {
self.parent.get()
}

fn enforce_parents(&self, expr: &Arc<Expression>) {
self.array.set_parent(Arc::downgrade(expr));
self.index.set_parent(Arc::downgrade(expr));
fn enforce_parents(&self, expr: &'a Expression<'a>) {
self.array.get().set_parent(expr);
self.index.get().set_parent(expr);
}

fn get_type(&self) -> Option<Type> {
match self.array.get_type() {
fn get_type(&self) -> Option<Type<'a>> {
match self.array.get().get_type() {
Some(Type::Array(element, _)) => Some(*element),
_ => None,
}
}

fn is_mut_ref(&self) -> bool {
self.array.is_mut_ref()
self.array.get().is_mut_ref()
}

fn const_value(&self) -> Option<ConstValue> {
let mut array = match self.array.const_value()? {
let mut array = match self.array.get().const_value()? {
ConstValue::Array(values) => values,
_ => return None,
};
let const_index = match self.index.const_value()? {
let const_index = match self.index.get().const_value()? {
ConstValue::Int(x) => x.to_usize()?,
_ => return None,
};
Expand All @@ -77,17 +74,17 @@ impl ExpressionNode for ArrayAccessExpression {
}

fn is_consty(&self) -> bool {
self.array.is_consty()
self.array.get().is_consty()
}
}

impl FromAst<leo_ast::ArrayAccessExpression> for ArrayAccessExpression {
impl<'a> FromAst<'a, leo_ast::ArrayAccessExpression> for ArrayAccessExpression<'a> {
fn from_ast(
scope: &Scope,
scope: &'a Scope<'a>,
value: &leo_ast::ArrayAccessExpression,
expected_type: Option<PartialType>,
) -> Result<ArrayAccessExpression, AsgConvertError> {
let array = Arc::<Expression>::from_ast(
expected_type: Option<PartialType<'a>>,
) -> Result<ArrayAccessExpression<'a>, AsgConvertError> {
let array = <&Expression<'a>>::from_ast(
scope,
&*value.array,
Some(PartialType::Array(expected_type.map(Box::new), None)),
Expand All @@ -103,7 +100,7 @@ impl FromAst<leo_ast::ArrayAccessExpression> for ArrayAccessExpression {
}
}

let index = Arc::<Expression>::from_ast(
let index = <&Expression<'a>>::from_ast(
scope,
&*value.index,
Some(PartialType::Integer(None, Some(IntegerType::U32))),
Expand All @@ -116,19 +113,19 @@ impl FromAst<leo_ast::ArrayAccessExpression> for ArrayAccessExpression {
}

Ok(ArrayAccessExpression {
parent: RefCell::new(None),
parent: Cell::new(None),
span: Some(value.span.clone()),
array,
index,
array: Cell::new(array),
index: Cell::new(index),
})
}
}

impl Into<leo_ast::ArrayAccessExpression> for &ArrayAccessExpression {
impl<'a> Into<leo_ast::ArrayAccessExpression> for &ArrayAccessExpression<'a> {
fn into(self) -> leo_ast::ArrayAccessExpression {
leo_ast::ArrayAccessExpression {
array: Box::new(self.array.as_ref().into()),
index: Box::new(self.index.as_ref().into()),
array: Box::new(self.array.get().into()),
index: Box::new(self.index.get().into()),
span: self.span.clone().unwrap_or_default(),
}
}
Expand Down
61 changes: 30 additions & 31 deletions asg/src/expression/array_init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,40 +16,37 @@

use crate::{AsgConvertError, ConstValue, Expression, ExpressionNode, FromAst, Node, PartialType, Scope, Span, Type};

use std::{
cell::RefCell,
sync::{Arc, Weak},
};

#[derive(Debug)]
pub struct ArrayInitExpression {
pub parent: RefCell<Option<Weak<Expression>>>,
use std::cell::Cell;

#[derive(Clone)]
pub struct ArrayInitExpression<'a> {
pub parent: Cell<Option<&'a Expression<'a>>>,
pub span: Option<Span>,
pub element: Arc<Expression>,
pub element: Cell<&'a Expression<'a>>,
pub len: usize,
}

impl Node for ArrayInitExpression {
impl<'a> Node for ArrayInitExpression<'a> {
fn span(&self) -> Option<&Span> {
self.span.as_ref()
}
}

impl ExpressionNode for ArrayInitExpression {
fn set_parent(&self, parent: Weak<Expression>) {
impl<'a> ExpressionNode<'a> for ArrayInitExpression<'a> {
fn set_parent(&self, parent: &'a Expression<'a>) {
self.parent.replace(Some(parent));
}

fn get_parent(&self) -> Option<Arc<Expression>> {
self.parent.borrow().as_ref().map(Weak::upgrade).flatten()
fn get_parent(&self) -> Option<&'a Expression<'a>> {
self.parent.get()
}

fn enforce_parents(&self, expr: &Arc<Expression>) {
self.element.set_parent(Arc::downgrade(expr));
fn enforce_parents(&self, expr: &'a Expression<'a>) {
self.element.get().set_parent(expr);
}

fn get_type(&self) -> Option<Type> {
Some(Type::Array(Box::new(self.element.get_type()?), self.len))
fn get_type(&self) -> Option<Type<'a>> {
Some(Type::Array(Box::new(self.element.get().get_type()?), self.len))
}

fn is_mut_ref(&self) -> bool {
Expand All @@ -62,16 +59,16 @@ impl ExpressionNode for ArrayInitExpression {
}

fn is_consty(&self) -> bool {
self.element.is_consty()
self.element.get().is_consty()
}
}

impl FromAst<leo_ast::ArrayInitExpression> for ArrayInitExpression {
impl<'a> FromAst<'a, leo_ast::ArrayInitExpression> for ArrayInitExpression<'a> {
fn from_ast(
scope: &Scope,
scope: &'a Scope<'a>,
value: &leo_ast::ArrayInitExpression,
expected_type: Option<PartialType>,
) -> Result<ArrayInitExpression, AsgConvertError> {
expected_type: Option<PartialType<'a>>,
) -> Result<ArrayInitExpression<'a>, AsgConvertError> {
let (mut expected_item, expected_len) = match expected_type {
Some(PartialType::Array(item, dims)) => (item.map(|x| *x), dims),
None => (None, None),
Expand Down Expand Up @@ -130,28 +127,30 @@ impl FromAst<leo_ast::ArrayInitExpression> for ArrayInitExpression {
}
}
}
let mut element = Some(Arc::<Expression>::from_ast(scope, &*value.element, expected_item)?);
let mut element = Some(<&'a Expression<'a>>::from_ast(scope, &*value.element, expected_item)?);
let mut output = None;

for dimension in dimensions.iter().rev().copied() {
output = Some(ArrayInitExpression {
parent: RefCell::new(None),
parent: Cell::new(None),
span: Some(value.span.clone()),
element: output
.map(Expression::ArrayInit)
.map(Arc::new)
.unwrap_or_else(|| element.take().unwrap()),
element: Cell::new(
output
.map(Expression::ArrayInit)
.map(|expr| &*scope.alloc_expression(expr))
.unwrap_or_else(|| element.take().unwrap()),
),
len: dimension,
});
}
Ok(output.unwrap())
}
}

impl Into<leo_ast::ArrayInitExpression> for &ArrayInitExpression {
impl<'a> Into<leo_ast::ArrayInitExpression> for &ArrayInitExpression<'a> {
fn into(self) -> leo_ast::ArrayInitExpression {
leo_ast::ArrayInitExpression {
element: Box::new(self.element.as_ref().into()),
element: Box::new(self.element.get().into()),
dimensions: leo_ast::ArrayDimensions(vec![leo_ast::PositiveNumber {
value: self.len.to_string(),
}]),
Expand Down
Loading

0 comments on commit 9baa938

Please sign in to comment.