This commit is contained in:
Pranav Gaddamadugu 2022-09-30 11:55:42 -07:00
parent f51a36aeaf
commit c551179b42
12 changed files with 36 additions and 55 deletions

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::{normalize_json_value, remove_key_from_json, Struct, Expression, Type};
use crate::{normalize_json_value, remove_key_from_json, Expression, Struct, Type};
use super::*;
use leo_errors::{AstError, Result};

View File

@ -72,11 +72,7 @@ pub trait ExpressionVisitor<'a> {
Default::default()
}
fn visit_struct_init(
&mut self,
_input: &'a StructExpression,
_additional: &Self::AdditionalInput,
) -> Self::Output {
fn visit_struct_init(&mut self, _input: &'a StructExpression, _additional: &Self::AdditionalInput) -> Self::Output {
Default::default()
}
@ -199,10 +195,7 @@ pub trait ProgramVisitor<'a>: StatementVisitor<'a> {
fn visit_program(&mut self, input: &'a Program) {
input.imports.values().for_each(|import| self.visit_import(import));
input
.structs
.values()
.for_each(|function| self.visit_struct(function));
input.structs.values().for_each(|function| self.visit_struct(function));
input.mappings.values().for_each(|mapping| self.visit_mapping(mapping));

View File

@ -17,7 +17,7 @@
//! A Leo program consists of import, struct, and function definitions.
//! Each defined type consists of ast statements and expressions.
use crate::{Struct, Function, FunctionInput, Identifier, Mapping};
use crate::{Function, FunctionInput, Identifier, Mapping, Struct};
use indexmap::IndexMap;
use serde::{Deserialize, Serialize};

View File

@ -171,7 +171,6 @@ impl ParserContext<'_> {
Ok((name, type_))
}
/// Returns a [`Member`] AST node if the next tokens represent a struct member variable.
fn parse_member_variable_declaration(&mut self) -> Result<Member> {
let (name, type_) = self.parse_typed_ident()?;

View File

@ -16,9 +16,9 @@
use crate::CodeGenerator;
use leo_ast::{
AccessExpression, AssociatedFunction, BinaryExpression, BinaryOperation, CallExpression, StructExpression,
ErrExpression, Expression, Identifier, Literal, MemberAccess, TernaryExpression, TupleExpression, Type,
UnaryExpression, UnaryOperation,
AccessExpression, AssociatedFunction, BinaryExpression, BinaryOperation, CallExpression, ErrExpression, Expression,
Identifier, Literal, MemberAccess, StructExpression, TernaryExpression, TupleExpression, Type, UnaryExpression,
UnaryOperation,
};
use leo_span::sym;

View File

@ -16,7 +16,7 @@
use crate::CodeGenerator;
use leo_ast::{functions, Struct, Member, Function, Identifier, Mapping, Mode, Program, Type};
use leo_ast::{functions, Function, Identifier, Mapping, Member, Mode, Program, Struct, Type};
use indexmap::IndexMap;
use itertools::Itertools;

View File

@ -18,8 +18,8 @@ use crate::Flattener;
use itertools::Itertools;
use leo_ast::{
AccessExpression, StructExpression, Member, StructVariableInitializer, Expression,
ExpressionReconstructor, MemberAccess, Statement, TernaryExpression, TupleExpression,
AccessExpression, Expression, ExpressionReconstructor, Member, MemberAccess, Statement, StructExpression,
StructVariableInitializer, TernaryExpression, TupleExpression,
};
// TODO: Clean up logic. To be done in a follow-up PR (feat/tuples)

View File

@ -17,8 +17,7 @@
use crate::{Assigner, SymbolTable};
use leo_ast::{
AccessExpression, Member, Expression, ExpressionReconstructor, Identifier, Statement, TernaryExpression,
Type,
AccessExpression, Expression, ExpressionReconstructor, Identifier, Member, Statement, TernaryExpression, Type,
};
use leo_span::Symbol;

View File

@ -17,9 +17,9 @@
use crate::StaticSingleAssigner;
use leo_ast::{
AccessExpression, AssociatedFunction, BinaryExpression, CallExpression, StructExpression,
StructVariableInitializer, Expression, ExpressionConsumer, Identifier, Literal, MemberAccess, Statement,
TernaryExpression, TupleAccess, TupleExpression, UnaryExpression,
AccessExpression, AssociatedFunction, BinaryExpression, CallExpression, Expression, ExpressionConsumer, Identifier,
Literal, MemberAccess, Statement, StructExpression, StructVariableInitializer, TernaryExpression, TupleAccess,
TupleExpression, UnaryExpression,
};
use leo_span::sym;

View File

@ -16,7 +16,7 @@
use std::cell::RefCell;
use leo_ast::{Struct, Function};
use leo_ast::{Function, Struct};
use leo_errors::{AstError, Result};
use leo_span::{Span, Symbol};

View File

@ -141,11 +141,7 @@ impl<'a> ExpressionVisitor<'a> for TypeChecker<'a> {
let struct_ = self.symbol_table.borrow().lookup_struct(identifier.name).cloned();
if let Some(struct_) = struct_ {
// Check that `access.name` is a member of the struct.
match struct_
.members
.iter()
.find(|member| member.name() == access.name.name)
{
match struct_.members.iter().find(|member| member.name() == access.name.name) {
// Case where `access.name` is a member of the struct.
Some(Member::StructVariable(_, type_)) => return Some(type_.clone()),
// Case where `access.name` is not a member of the struct.
@ -486,22 +482,20 @@ impl<'a> ExpressionVisitor<'a> for TypeChecker<'a> {
}
// Check struct member types.
struct_.members
.iter()
.for_each(|Member::StructVariable(name, ty)| {
// Lookup struct variable name.
if let Some(actual) = input.members.iter().find(|member| member.identifier.name == name.name) {
if let Some(expr) = &actual.expression {
self.visit_expression(expr, &Some(ty.clone()));
}
} else {
self.emit_err(TypeCheckerError::missing_struct_member(
struct_.identifier,
name,
input.span(),
));
};
});
struct_.members.iter().for_each(|Member::StructVariable(name, ty)| {
// Lookup struct variable name.
if let Some(actual) = input.members.iter().find(|member| member.identifier.name == name.name) {
if let Some(expr) = &actual.expression {
self.visit_expression(expr, &Some(ty.clone()));
}
} else {
self.emit_err(TypeCheckerError::missing_struct_member(
struct_.identifier,
name,
input.span(),
));
};
});
Some(ret)
} else {

View File

@ -29,16 +29,12 @@ impl<'a> ProgramVisitor<'a> for TypeChecker<'a> {
fn visit_struct(&mut self, input: &'a Struct) {
// Check for conflicting struct/record member names.
let mut used = HashSet::new();
if !input
.members
.iter()
.all(|Member::StructVariable(ident, type_)| {
// TODO: Better spans.
// Check that the member types are valid.
self.assert_type_is_valid(input.span, type_);
used.insert(ident.name)
})
{
if !input.members.iter().all(|Member::StructVariable(ident, type_)| {
// TODO: Better spans.
// Check that the member types are valid.
self.assert_type_is_valid(input.span, type_);
used.insert(ident.name)
}) {
self.emit_err(if input.is_record {
TypeCheckerError::duplicate_record_variable(input.name(), input.span())
} else {