1
0
Fork 0
mirror of synced 2025-09-23 12:18:44 +00:00

add name to typed types

This commit is contained in:
schaeff 2020-05-14 23:03:15 +02:00
parent 2c07fee812
commit 648f71e8c1
14 changed files with 364 additions and 216 deletions

View file

@ -365,7 +365,7 @@ mod tests {
mod strict {
use super::*;
use zokrates_core::typed_absy::types::StructMember;
use zokrates_core::typed_absy::types::{StructMember, StructType};
#[test]
fn fields() {
@ -410,10 +410,11 @@ mod tests {
assert_eq!(
parse_strict::<Bn128Field>(
s,
vec![Type::Struct(vec![StructMember::new(
"a".into(),
Type::FieldElement
)])]
vec![Type::Struct(StructType::new(
"".into(),
"".into(),
vec![StructMember::new("a".into(), Type::FieldElement)]
))]
)
.unwrap(),
CheckedValues(vec![CheckedValue::Struct(
@ -427,10 +428,11 @@ mod tests {
assert_eq!(
parse_strict::<Bn128Field>(
s,
vec![Type::Struct(vec![StructMember::new(
"a".into(),
Type::FieldElement
)])]
vec![Type::Struct(StructType::new(
"".into(),
"".into(),
vec![StructMember::new("a".into(), Type::FieldElement)]
))]
)
.unwrap_err(),
Error::Type("Member with id `a` not found".into())
@ -440,10 +442,11 @@ mod tests {
assert_eq!(
parse_strict::<Bn128Field>(
s,
vec![Type::Struct(vec![StructMember::new(
"a".into(),
Type::FieldElement
)])]
vec![Type::Struct(StructType::new(
"".into(),
"".into(),
vec![StructMember::new("a".into(), Type::FieldElement)]
))]
)
.unwrap_err(),
Error::Type("Expected 1 member(s), found 0".into())
@ -453,10 +456,11 @@ mod tests {
assert_eq!(
parse_strict::<Bn128Field>(
s,
vec![Type::Struct(vec![StructMember::new(
"a".into(),
Type::FieldElement
)])]
vec![Type::Struct(StructType::new(
"".into(),
"".into(),
vec![StructMember::new("a".into(), Type::FieldElement)]
))]
)
.unwrap_err(),
Error::Type("Value `false` doesn't match expected type `field`".into())

View file

@ -0,0 +1,5 @@
struct Foo {}
struct Bar {}
def main() -> (Foo):
return Bar {}

View file

@ -47,11 +47,11 @@ impl<'ast, T: Field> From<pest::StructDefinition<'ast>> for absy::SymbolDeclarat
let id = definition.id.span.as_str();
let ty = absy::StructType {
let ty = absy::StructDefinition {
fields: definition
.fields
.into_iter()
.map(|f| absy::StructFieldNode::from(f))
.map(|f| absy::StructDefinitionFieldNode::from(f))
.collect(),
}
.span(span.clone());
@ -64,8 +64,8 @@ impl<'ast, T: Field> From<pest::StructDefinition<'ast>> for absy::SymbolDeclarat
}
}
impl<'ast> From<pest::StructField<'ast>> for absy::StructFieldNode<'ast> {
fn from(field: pest::StructField<'ast>) -> absy::StructFieldNode {
impl<'ast> From<pest::StructField<'ast>> for absy::StructDefinitionFieldNode<'ast> {
fn from(field: pest::StructField<'ast>) -> absy::StructDefinitionFieldNode {
use absy::NodeValue;
let span = field.span;
@ -74,7 +74,7 @@ impl<'ast> From<pest::StructField<'ast>> for absy::StructFieldNode<'ast> {
let ty = absy::UnresolvedTypeNode::from(field.ty);
absy::StructField { id, ty }.span(span)
absy::StructDefinitionField { id, ty }.span(span)
}
}

View file

@ -51,7 +51,7 @@ pub struct SymbolDeclaration<'ast, T> {
#[derive(PartialEq, Clone)]
pub enum Symbol<'ast, T> {
HereType(StructTypeNode<'ast>),
HereType(StructDefinitionNode<'ast>),
HereFunction(FunctionNode<'ast, T>),
There(SymbolImportNode<'ast>),
Flat(FlatEmbed),
@ -112,11 +112,11 @@ pub type UnresolvedTypeNode = Node<UnresolvedType>;
/// A struct type definition
#[derive(Debug, Clone, PartialEq)]
pub struct StructType<'ast> {
pub fields: Vec<StructFieldNode<'ast>>,
pub struct StructDefinition<'ast> {
pub fields: Vec<StructDefinitionFieldNode<'ast>>,
}
impl<'ast> fmt::Display for StructType<'ast> {
impl<'ast> fmt::Display for StructDefinition<'ast> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(
f,
@ -130,22 +130,22 @@ impl<'ast> fmt::Display for StructType<'ast> {
}
}
pub type StructTypeNode<'ast> = Node<StructType<'ast>>;
pub type StructDefinitionNode<'ast> = Node<StructDefinition<'ast>>;
/// A struct type definition
#[derive(Debug, Clone, PartialEq)]
pub struct StructField<'ast> {
pub struct StructDefinitionField<'ast> {
pub id: Identifier<'ast>,
pub ty: UnresolvedTypeNode,
}
impl<'ast> fmt::Display for StructField<'ast> {
impl<'ast> fmt::Display for StructDefinitionField<'ast> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}: {},", self.id, self.ty)
}
}
type StructFieldNode<'ast> = Node<StructField<'ast>>;
type StructDefinitionFieldNode<'ast> = Node<StructDefinitionField<'ast>>;
/// An import
#[derive(Debug, Clone, PartialEq)]

View file

@ -82,8 +82,8 @@ impl<'ast, T: fmt::Display + fmt::Debug + PartialEq> NodeValue for Assignee<'ast
impl<'ast, T: fmt::Display + fmt::Debug + PartialEq> NodeValue for Statement<'ast, T> {}
impl<'ast, T: Field> NodeValue for SymbolDeclaration<'ast, T> {}
impl NodeValue for UnresolvedType {}
impl<'ast> NodeValue for StructType<'ast> {}
impl<'ast> NodeValue for StructField<'ast> {}
impl<'ast> NodeValue for StructDefinition<'ast> {}
impl<'ast> NodeValue for StructDefinitionField<'ast> {}
impl<'ast, T: fmt::Display + fmt::Debug + PartialEq> NodeValue for Function<'ast, T> {}
impl<'ast, T: Field> NodeValue for Module<'ast, T> {}
impl<'ast> NodeValue for SymbolImport<'ast> {}

View file

@ -268,7 +268,8 @@ impl<'ast> Checker<'ast> {
fn check_struct_type_declaration(
&mut self,
s: StructTypeNode<'ast>,
id: String,
s: StructDefinitionNode<'ast>,
module_id: &ModuleId,
types: &TypeMap,
) -> Result<Type, Vec<ErrorInner>> {
@ -302,12 +303,14 @@ impl<'ast> Checker<'ast> {
return Err(errors);
}
Ok(Type::Struct(
Ok(Type::Struct(StructType::new(
module_id.into(),
id,
fields
.iter()
.map(|f| StructMember::new(f.0.clone(), f.1.clone()))
.collect(),
))
)))
}
fn check_symbol_declaration<T: Field>(
@ -325,7 +328,12 @@ impl<'ast> Checker<'ast> {
match declaration.symbol {
Symbol::HereType(t) => {
match self.check_struct_type_declaration(t.clone(), module_id, &state.types) {
match self.check_struct_type_declaration(
declaration.id.to_string(),
t.clone(),
module_id,
&state.types,
) {
Ok(ty) => {
match symbol_unifier.insert_type(declaration.id) {
false => errors.push(
@ -1820,21 +1828,21 @@ impl<'ast> Checker<'ast> {
module_id,
&types,
)?;
let members = match ty {
Type::Struct(members) => members,
let struct_type = match ty {
Type::Struct(struct_type) => struct_type,
_ => unreachable!(),
};
// check that we provided the required number of values
if members.len() != inline_members.len() {
if struct_type.len() != inline_members.len() {
return Err(ErrorInner {
pos: Some(pos),
message: format!(
"Inline struct {} does not match {} : {}",
Expression::InlineStruct(id.clone(), inline_members),
id,
Type::Struct(members)
Type::Struct(struct_type)
),
});
}
@ -1849,7 +1857,7 @@ impl<'ast> Checker<'ast> {
.collect::<HashMap<_, _>>();
let mut result: Vec<TypedExpression<'ast, T>> = vec![];
for member in &members {
for member in struct_type.iter() {
match inline_members_map.remove(member.id.as_str()) {
Some(value) => {
let expression_checked =
@ -1878,7 +1886,7 @@ impl<'ast> Checker<'ast> {
"Member {} of struct {} : {} not found in value {}",
member.id,
id.clone(),
Type::Struct(members.clone()),
Type::Struct(struct_type.clone()),
Expression::InlineStruct(id.clone(), inline_members),
),
})
@ -1887,7 +1895,7 @@ impl<'ast> Checker<'ast> {
}
Ok(StructExpressionInner::Value(result)
.annotate(members)
.annotate(struct_type)
.into())
}
Expression::And(box e1, box e2) => {
@ -2084,13 +2092,13 @@ mod tests {
.mock()
}
fn struct0() -> StructTypeNode<'static> {
StructType { fields: vec![] }.mock()
fn struct0() -> StructDefinitionNode<'static> {
StructDefinition { fields: vec![] }.mock()
}
fn struct1() -> StructTypeNode<'static> {
StructType {
fields: vec![StructField {
fn struct1() -> StructDefinitionNode<'static> {
StructDefinition {
fields: vec![StructDefinitionField {
id: "foo".into(),
ty: UnresolvedType::FieldElement.mock(),
}
@ -2318,7 +2326,7 @@ mod tests {
.mock(),
SymbolDeclaration {
id: "foo",
symbol: Symbol::HereType(StructType { fields: vec![] }.mock()),
symbol: Symbol::HereType(StructDefinition { fields: vec![] }.mock()),
}
.mock(),
],
@ -3619,7 +3627,7 @@ mod tests {
/// solver function to create a module at location "" with a single symbol `Foo { foo: field }`
fn create_module_with_foo(
s: StructType<'static>,
s: StructDefinition<'static>,
) -> (Checker<'static>, State<'static, Bn128Field>) {
let module_id: PathBuf = "".into();
@ -3650,12 +3658,17 @@ mod tests {
// an empty struct should be allowed to be defined
let module_id = "".into();
let types = HashMap::new();
let declaration = StructType { fields: vec![] }.mock();
let declaration = StructDefinition { fields: vec![] }.mock();
let expected_type = Type::Struct(vec![]);
let expected_type = Type::Struct(StructType::new("".into(), "Foo".into(), vec![]));
assert_eq!(
Checker::new().check_struct_type_declaration(declaration, &module_id, &types),
Checker::new().check_struct_type_declaration(
"Foo".into(),
declaration,
&module_id,
&types
),
Ok(expected_type)
);
}
@ -3665,14 +3678,14 @@ mod tests {
// a valid struct should be allowed to be defined
let module_id = "".into();
let types = HashMap::new();
let declaration = StructType {
let declaration = StructDefinition {
fields: vec![
StructField {
StructDefinitionField {
id: "foo",
ty: UnresolvedType::FieldElement.mock(),
}
.mock(),
StructField {
StructDefinitionField {
id: "bar",
ty: UnresolvedType::Boolean.mock(),
}
@ -3681,13 +3694,22 @@ mod tests {
}
.mock();
let expected_type = Type::Struct(vec![
StructMember::new("foo".into(), Type::FieldElement),
StructMember::new("bar".into(), Type::Boolean),
]);
let expected_type = Type::Struct(StructType::new(
"".into(),
"Foo".into(),
vec![
StructMember::new("foo".into(), Type::FieldElement),
StructMember::new("bar".into(), Type::Boolean),
],
));
assert_eq!(
Checker::new().check_struct_type_declaration(declaration, &module_id, &types),
Checker::new().check_struct_type_declaration(
"Foo".into(),
declaration,
&module_id,
&types
),
Ok(expected_type)
);
}
@ -3698,14 +3720,14 @@ mod tests {
let module_id = "".into();
let types = HashMap::new();
let declaration0 = StructType {
let declaration0 = StructDefinition {
fields: vec![
StructField {
StructDefinitionField {
id: "foo",
ty: UnresolvedType::FieldElement.mock(),
}
.mock(),
StructField {
StructDefinitionField {
id: "bar",
ty: UnresolvedType::Boolean.mock(),
}
@ -3714,14 +3736,14 @@ mod tests {
}
.mock();
let declaration1 = StructType {
let declaration1 = StructDefinition {
fields: vec![
StructField {
StructDefinitionField {
id: "bar",
ty: UnresolvedType::Boolean.mock(),
}
.mock(),
StructField {
StructDefinitionField {
id: "foo",
ty: UnresolvedType::FieldElement.mock(),
}
@ -3731,8 +3753,18 @@ mod tests {
.mock();
assert_ne!(
Checker::new().check_struct_type_declaration(declaration0, &module_id, &types),
Checker::new().check_struct_type_declaration(declaration1, &module_id, &types)
Checker::new().check_struct_type_declaration(
"Foo".into(),
declaration0,
&module_id,
&types
),
Checker::new().check_struct_type_declaration(
"Foo".into(),
declaration1,
&module_id,
&types
)
);
}
@ -3742,14 +3774,14 @@ mod tests {
let module_id = "".into();
let types = HashMap::new();
let declaration = StructType {
let declaration = StructDefinition {
fields: vec![
StructField {
StructDefinitionField {
id: "foo",
ty: UnresolvedType::FieldElement.mock(),
}
.mock(),
StructField {
StructDefinitionField {
id: "foo",
ty: UnresolvedType::Boolean.mock(),
}
@ -3760,7 +3792,12 @@ mod tests {
assert_eq!(
Checker::new()
.check_struct_type_declaration(declaration, &module_id, &types)
.check_struct_type_declaration(
"Foo".into(),
declaration,
&module_id,
&types
)
.unwrap_err()[0]
.message,
"Duplicate key foo in struct definition"
@ -3782,8 +3819,8 @@ mod tests {
SymbolDeclaration {
id: "Foo",
symbol: Symbol::HereType(
StructType {
fields: vec![StructField {
StructDefinition {
fields: vec![StructDefinitionField {
id: "foo",
ty: UnresolvedType::FieldElement.mock(),
}
@ -3796,8 +3833,8 @@ mod tests {
SymbolDeclaration {
id: "Bar",
symbol: Symbol::HereType(
StructType {
fields: vec![StructField {
StructDefinition {
fields: vec![StructDefinitionField {
id: "foo",
ty: UnresolvedType::User("Foo".into()).mock(),
}
@ -3820,10 +3857,18 @@ mod tests {
.unwrap()
.get(&"Bar".to_string())
.unwrap(),
&Type::Struct(vec![StructMember::new(
"foo".into(),
Type::Struct(vec![StructMember::new("foo".into(), Type::FieldElement)])
)])
&Type::Struct(StructType::new(
module_id.clone(),
"Bar".into(),
vec![StructMember::new(
"foo".into(),
Type::Struct(StructType::new(
module_id,
"Foo".into(),
vec![StructMember::new("foo".into(), Type::FieldElement)]
))
)]
))
);
}
@ -3840,8 +3885,8 @@ mod tests {
symbols: vec![SymbolDeclaration {
id: "Bar",
symbol: Symbol::HereType(
StructType {
fields: vec![StructField {
StructDefinition {
fields: vec![StructDefinitionField {
id: "foo",
ty: UnresolvedType::User("Foo".into()).mock(),
}
@ -3871,8 +3916,8 @@ mod tests {
symbols: vec![SymbolDeclaration {
id: "Foo",
symbol: Symbol::HereType(
StructType {
fields: vec![StructField {
StructDefinition {
fields: vec![StructDefinitionField {
id: "foo",
ty: UnresolvedType::User("Foo".into()).mock(),
}
@ -3904,8 +3949,8 @@ mod tests {
SymbolDeclaration {
id: "Foo",
symbol: Symbol::HereType(
StructType {
fields: vec![StructField {
StructDefinition {
fields: vec![StructDefinitionField {
id: "bar",
ty: UnresolvedType::User("Bar".into()).mock(),
}
@ -3918,8 +3963,8 @@ mod tests {
SymbolDeclaration {
id: "Bar",
symbol: Symbol::HereType(
StructType {
fields: vec![StructField {
StructDefinition {
fields: vec![StructDefinitionField {
id: "foo",
ty: UnresolvedType::User("Foo".into()).mock(),
}
@ -3951,8 +3996,8 @@ mod tests {
// an undefined type cannot be checked
// Bar
let (checker, state) = create_module_with_foo(StructType {
fields: vec![StructField {
let (checker, state) = create_module_with_foo(StructDefinition {
fields: vec![StructDefinitionField {
id: "foo",
ty: UnresolvedType::FieldElement.mock(),
}
@ -3965,10 +4010,11 @@ mod tests {
&PathBuf::from(MODULE_ID).into(),
&state.types
),
Ok(Type::Struct(vec![StructMember::new(
"foo".into(),
Type::FieldElement
)]))
Ok(Type::Struct(StructType::new(
"".into(),
"Foo".into(),
vec![StructMember::new("foo".into(), Type::FieldElement)]
)))
);
assert_eq!(
@ -3990,8 +4036,8 @@ mod tests {
// an undefined type cannot be used as parameter
let (checker, state) = create_module_with_foo(StructType {
fields: vec![StructField {
let (checker, state) = create_module_with_foo(StructDefinition {
fields: vec![StructDefinitionField {
id: "foo",
ty: UnresolvedType::FieldElement.mock(),
}
@ -4013,7 +4059,11 @@ mod tests {
Ok(Parameter {
id: Variable::with_id_and_type(
"a".into(),
Type::Struct(vec![StructMember::new("foo".into(), Type::FieldElement)])
Type::Struct(StructType::new(
"".into(),
"Foo".into(),
vec![StructMember::new("foo".into(), Type::FieldElement)]
))
),
private: true
})
@ -4046,8 +4096,8 @@ mod tests {
// an undefined type cannot be used in a variable declaration
let (mut checker, state) = create_module_with_foo(StructType {
fields: vec![StructField {
let (mut checker, state) = create_module_with_foo(StructDefinition {
fields: vec![StructDefinitionField {
id: "foo",
ty: UnresolvedType::FieldElement.mock(),
}
@ -4066,7 +4116,11 @@ mod tests {
),
Ok(TypedStatement::Declaration(Variable::with_id_and_type(
"a".into(),
Type::Struct(vec![StructMember::new("foo".into(), Type::FieldElement)])
Type::Struct(StructType::new(
"".into(),
"Foo".into(),
vec![StructMember::new("foo".into(), Type::FieldElement)]
))
)))
);
@ -4103,8 +4157,8 @@ mod tests {
// struct Foo = { foo: field }
// Foo { foo: 42 }.foo
let (mut checker, state) = create_module_with_foo(StructType {
fields: vec![StructField {
let (mut checker, state) = create_module_with_foo(StructDefinition {
fields: vec![StructDefinitionField {
id: "foo",
ty: UnresolvedType::FieldElement.mock(),
}
@ -4133,7 +4187,11 @@ mod tests {
Bn128Field::from(42)
)
.into()])
.annotate(vec![StructMember::new("foo".into(), Type::FieldElement)]),
.annotate(StructType::new(
"".into(),
"Foo".into(),
vec![StructMember::new("foo".into(), Type::FieldElement)]
)),
"foo".into()
)
.into())
@ -4147,8 +4205,8 @@ mod tests {
// struct Foo = { foo: field }
// Foo { foo: 42 }.bar
let (mut checker, state) = create_module_with_foo(StructType {
fields: vec![StructField {
let (mut checker, state) = create_module_with_foo(StructDefinition {
fields: vec![StructDefinitionField {
id: "foo",
ty: UnresolvedType::FieldElement.mock(),
}
@ -4188,8 +4246,8 @@ mod tests {
fn wrong_name() {
// a A value cannot be defined with B as id, even if A and B have the same members
let (mut checker, state) = create_module_with_foo(StructType {
fields: vec![StructField {
let (mut checker, state) = create_module_with_foo(StructDefinition {
fields: vec![StructDefinitionField {
id: "foo",
ty: UnresolvedType::FieldElement.mock(),
}
@ -4223,14 +4281,14 @@ mod tests {
// struct Foo = { foo: field, bar: bool }
// Foo foo = Foo { foo: 42, bar: true }
let (mut checker, state) = create_module_with_foo(StructType {
let (mut checker, state) = create_module_with_foo(StructDefinition {
fields: vec![
StructField {
StructDefinitionField {
id: "foo",
ty: UnresolvedType::FieldElement.mock(),
}
.mock(),
StructField {
StructDefinitionField {
id: "bar",
ty: UnresolvedType::Boolean.mock(),
}
@ -4258,10 +4316,14 @@ mod tests {
FieldElementExpression::Number(Bn128Field::from(42)).into(),
BooleanExpression::Value(true).into()
])
.annotate(vec![
StructMember::new("foo".into(), Type::FieldElement),
StructMember::new("bar".into(), Type::Boolean)
])
.annotate(StructType::new(
"".into(),
"Foo".into(),
vec![
StructMember::new("foo".into(), Type::FieldElement),
StructMember::new("bar".into(), Type::Boolean)
]
))
.into())
);
}
@ -4273,14 +4335,14 @@ mod tests {
// struct Foo = { foo: field, bar: bool }
// Foo foo = Foo { bar: true, foo: 42 }
let (mut checker, state) = create_module_with_foo(StructType {
let (mut checker, state) = create_module_with_foo(StructDefinition {
fields: vec![
StructField {
StructDefinitionField {
id: "foo",
ty: UnresolvedType::FieldElement.mock(),
}
.mock(),
StructField {
StructDefinitionField {
id: "bar",
ty: UnresolvedType::Boolean.mock(),
}
@ -4308,10 +4370,14 @@ mod tests {
FieldElementExpression::Number(Bn128Field::from(42)).into(),
BooleanExpression::Value(true).into()
])
.annotate(vec![
StructMember::new("foo".into(), Type::FieldElement),
StructMember::new("bar".into(), Type::Boolean)
])
.annotate(StructType::new(
"".into(),
"Foo".into(),
vec![
StructMember::new("foo".into(), Type::FieldElement),
StructMember::new("bar".into(), Type::Boolean)
]
))
.into())
);
}
@ -4323,14 +4389,14 @@ mod tests {
// struct Foo = { foo: field, bar: bool }
// Foo foo = Foo { foo: 42 }
let (mut checker, state) = create_module_with_foo(StructType {
let (mut checker, state) = create_module_with_foo(StructDefinition {
fields: vec![
StructField {
StructDefinitionField {
id: "foo",
ty: UnresolvedType::FieldElement.mock(),
}
.mock(),
StructField {
StructDefinitionField {
id: "bar",
ty: UnresolvedType::Boolean.mock(),
}
@ -4367,14 +4433,14 @@ mod tests {
// Foo { foo: 42, baz: bool } // error
// Foo { foo: 42, baz: 42 } // error
let (mut checker, state) = create_module_with_foo(StructType {
let (mut checker, state) = create_module_with_foo(StructDefinition {
fields: vec![
StructField {
StructDefinitionField {
id: "foo",
ty: UnresolvedType::FieldElement.mock(),
}
.mock(),
StructField {
StructDefinitionField {
id: "bar",
ty: UnresolvedType::Boolean.mock(),
}

View file

@ -83,7 +83,7 @@ impl<'ast, T: Field> InputConstrainer<'ast, T> {
}
}
TypedExpression::Struct(s) => {
for member in s.ty() {
for member in s.ty().iter() {
let e = match *member.ty {
Type::FieldElement => {
FieldElementExpression::member(s.clone(), member.id.clone()).into()

View file

@ -17,7 +17,7 @@
//! where any call in `main` must be to `_SHA_256_ROUND` or `_UNPACK`
use std::collections::HashMap;
use typed_absy::types::{FunctionKey, StructMember, Type};
use typed_absy::types::{FunctionKey, Type};
use typed_absy::{folder::*, *};
use zokrates_field::Field;
@ -371,7 +371,7 @@ impl<'ast, T: Field> Folder<'ast, T> for Inliner<'ast, T> {
fn fold_struct_expression_inner(
&mut self,
ty: &Vec<StructMember>,
ty: &StructType,
e: StructExpressionInner<'ast, T>,
) -> StructExpressionInner<'ast, T> {
match e {

View file

@ -14,7 +14,7 @@ use crate::typed_absy::folder::*;
use crate::typed_absy::*;
use std::collections::HashMap;
use std::convert::TryFrom;
use typed_absy::types::{StructMember, Type};
use typed_absy::types::Type;
use zokrates_field::Field;
pub struct Propagator<'ast, T: Field> {
@ -405,7 +405,7 @@ impl<'ast, T: Field> Folder<'ast, T> for Propagator<'ast, T> {
fn fold_struct_expression_inner(
&mut self,
ty: &Vec<StructMember>,
ty: &StructType,
e: StructExpressionInner<'ast, T>,
) -> StructExpressionInner<'ast, T> {
match e {

View file

@ -147,15 +147,23 @@ mod tests {
inputs: vec![AbiInput {
name: String::from("foo"),
public: true,
ty: Type::Struct(vec![
ty: Type::Struct(StructType::new(
"".into(),
"Foo".into(),
vec![
StructMember::new(String::from("a"), Type::FieldElement),
StructMember::new(String::from("b"), Type::Boolean),
],
)),
}],
outputs: vec![Type::Struct(StructType::new(
"".into(),
"Foo".into(),
vec![
StructMember::new(String::from("a"), Type::FieldElement),
StructMember::new(String::from("b"), Type::Boolean),
]),
}],
outputs: vec![Type::Struct(vec![
StructMember::new(String::from("a"), Type::FieldElement),
StructMember::new(String::from("b"), Type::Boolean),
])],
],
))],
};
let json = serde_json::to_string_pretty(&abi).unwrap();
@ -167,31 +175,37 @@ mod tests {
"name": "foo",
"public": true,
"type": "struct",
"components": [
{
"name": "a",
"type": "field"
},
{
"name": "b",
"type": "bool"
}
]
"components": {
"name": "Foo",
"members": [
{
"name": "a",
"type": "field"
},
{
"name": "b",
"type": "bool"
}
]
}
}
],
"outputs": [
{
"type": "struct",
"components": [
{
"name": "a",
"type": "field"
},
{
"name": "b",
"type": "bool"
}
]
"components": {
"name": "Foo",
"members": [
{
"name": "a",
"type": "field"
},
{
"name": "b",
"type": "bool"
}
]
}
}
]
}"#
@ -204,13 +218,21 @@ mod tests {
inputs: vec![AbiInput {
name: String::from("foo"),
public: true,
ty: Type::Struct(vec![StructMember::new(
String::from("bar"),
Type::Struct(vec![
StructMember::new(String::from("a"), Type::FieldElement),
StructMember::new(String::from("b"), Type::FieldElement),
]),
)]),
ty: Type::Struct(StructType::new(
"".into(),
"Foo".into(),
vec![StructMember::new(
String::from("bar"),
Type::Struct(StructType::new(
"".into(),
"Bar".into(),
vec![
StructMember::new(String::from("a"), Type::FieldElement),
StructMember::new(String::from("b"), Type::FieldElement),
],
)),
)],
)),
}],
outputs: vec![],
};
@ -224,22 +246,28 @@ mod tests {
"name": "foo",
"public": true,
"type": "struct",
"components": [
{
"name": "bar",
"type": "struct",
"components": [
{
"name": "a",
"type": "field"
},
{
"name": "b",
"type": "field"
"components": {
"name": "Foo",
"members": [
{
"name": "bar",
"type": "struct",
"components": {
"name": "Bar",
"members": [
{
"name": "a",
"type": "field"
},
{
"name": "b",
"type": "field"
}
]
}
]
}
]
}
]
}
}
],
"outputs": []
@ -254,10 +282,14 @@ mod tests {
name: String::from("a"),
public: false,
ty: Type::Array(ArrayType::new(
Type::Struct(vec![
StructMember::new(String::from("b"), Type::FieldElement),
StructMember::new(String::from("c"), Type::Boolean),
]),
Type::Struct(StructType::new(
"".into(),
"Foo".into(),
vec![
StructMember::new(String::from("b"), Type::FieldElement),
StructMember::new(String::from("c"), Type::Boolean),
],
)),
2,
)),
}],
@ -276,16 +308,19 @@ mod tests {
"components": {
"size": 2,
"type": "struct",
"components": [
{
"name": "b",
"type": "field"
},
{
"name": "c",
"type": "bool"
}
]
"components": {
"name": "Foo",
"members": [
{
"name": "b",
"type": "field"
},
{
"name": "c",
"type": "bool"
}
]
}
}
}
],

View file

@ -1,7 +1,6 @@
// Generic walk through a typed AST. Not mutating in place
use crate::typed_absy::*;
use typed_absy::types::StructMember;
use zokrates_field::Field;
pub trait Folder<'ast, T: Field>: Sized {
@ -117,7 +116,7 @@ pub trait Folder<'ast, T: Field>: Sized {
}
fn fold_struct_expression_inner(
&mut self,
ty: &Vec<StructMember>,
ty: &StructType,
e: StructExpressionInner<'ast, T>,
) -> StructExpressionInner<'ast, T> {
fold_struct_expression_inner(self, ty, e)
@ -209,7 +208,7 @@ pub fn fold_array_expression_inner<'ast, T: Field, F: Folder<'ast, T>>(
pub fn fold_struct_expression_inner<'ast, T: Field, F: Folder<'ast, T>>(
f: &mut F,
_: &Vec<StructMember>,
_: &StructType,
e: StructExpressionInner<'ast, T>,
) -> StructExpressionInner<'ast, T> {
match e {

View file

@ -13,7 +13,7 @@ pub mod types;
mod variable;
pub use crate::typed_absy::parameter::Parameter;
pub use crate::typed_absy::types::{Signature, Type};
pub use crate::typed_absy::types::{Signature, StructType, Type};
pub use crate::typed_absy::variable::Variable;
use std::path::PathBuf;
@ -27,7 +27,6 @@ use zokrates_field::Field;
pub use self::folder::Folder;
use typed_absy::abi::{Abi, AbiInput};
pub use typed_absy::identifier::Identifier;
use typed_absy::types::StructMember;
/// An identifier for a `TypedModule`. Typically a path or uri.
pub type TypedModuleId = PathBuf;
@ -708,12 +707,12 @@ impl<'ast, T> ArrayExpression<'ast, T> {
#[derive(Clone, PartialEq, Hash, Eq)]
pub struct StructExpression<'ast, T> {
ty: Vec<StructMember>,
ty: StructType,
inner: StructExpressionInner<'ast, T>,
}
impl<'ast, T> StructExpression<'ast, T> {
pub fn ty(&self) -> &Vec<StructMember> {
pub fn ty(&self) -> &StructType {
&self.ty
}
@ -744,7 +743,7 @@ pub enum StructExpressionInner<'ast, T> {
}
impl<'ast, T> StructExpressionInner<'ast, T> {
pub fn annotate(self, ty: Vec<StructMember>) -> StructExpression<'ast, T> {
pub fn annotate(self, ty: StructType) -> StructExpression<'ast, T> {
StructExpression { ty, inner: self }
}
}

View file

@ -1,4 +1,5 @@
use std::fmt;
use std::path::PathBuf;
pub type Identifier<'ast> = &'ast str;
@ -19,6 +20,41 @@ pub struct ArrayType {
pub ty: Box<Type>,
}
#[derive(Clone, PartialEq, Eq, Hash, Serialize, Deserialize, PartialOrd, Ord)]
pub struct StructType {
#[serde(skip)]
pub module: PathBuf,
pub name: String,
pub members: Vec<StructMember>,
}
impl StructType {
pub fn new(module: PathBuf, name: String, members: Vec<StructMember>) -> Self {
StructType {
module,
name,
members,
}
}
pub fn len(&self) -> usize {
self.members.len()
}
pub fn iter(&self) -> std::slice::Iter<StructMember> {
self.members.iter()
}
}
impl IntoIterator for StructType {
type Item = StructMember;
type IntoIter = std::vec::IntoIter<Self::Item>;
fn into_iter(self) -> Self::IntoIter {
self.members.into_iter()
}
}
#[derive(Clone, PartialEq, Eq, Hash, Serialize, Deserialize, PartialOrd, Ord)]
#[serde(tag = "type", content = "components")]
pub enum Type {
@ -29,7 +65,7 @@ pub enum Type {
#[serde(rename = "array")]
Array(ArrayType),
#[serde(rename = "struct")]
Struct(Vec<StructMember>),
Struct(StructType),
}
impl ArrayType {
@ -56,10 +92,12 @@ impl fmt::Display for Type {
Type::FieldElement => write!(f, "field"),
Type::Boolean => write!(f, "bool"),
Type::Array(ref array_type) => write!(f, "{}[{}]", array_type.ty, array_type.size),
Type::Struct(ref members) => write!(
Type::Struct(ref struct_type) => write!(
f,
"{{{}}}",
members
"{} {{{}}}",
struct_type.name,
struct_type
.members
.iter()
.map(|member| format!("{}: {}", member.id, member.ty))
.collect::<Vec<_>>()
@ -75,10 +113,12 @@ impl fmt::Debug for Type {
Type::FieldElement => write!(f, "field"),
Type::Boolean => write!(f, "bool"),
Type::Array(ref array_type) => write!(f, "{}[{}]", array_type.ty, array_type.size),
Type::Struct(ref members) => write!(
Type::Struct(ref struct_type) => write!(
f,
"{{{}}}",
members
"{} {{{}}}",
struct_type.name,
struct_type
.members
.iter()
.map(|member| format!("{}: {}", member.id, member.ty))
.collect::<Vec<_>>()
@ -98,9 +138,9 @@ impl Type {
Type::FieldElement => String::from("f"),
Type::Boolean => String::from("b"),
Type::Array(array_type) => format!("{}[{}]", array_type.ty.to_slug(), array_type.size),
Type::Struct(members) => format!(
Type::Struct(struct_type) => format!(
"{{{}}}",
members
struct_type
.iter()
.map(|member| format!("{}:{}", member.id, member.ty))
.collect::<Vec<_>>()
@ -115,7 +155,7 @@ impl Type {
Type::FieldElement => 1,
Type::Boolean => 1,
Type::Array(array_type) => array_type.size * array_type.ty.get_primitive_count(),
Type::Struct(members) => members
Type::Struct(struct_type) => struct_type
.iter()
.map(|member| member.ty.get_primitive_count())
.sum(),

View file

@ -1,7 +1,7 @@
use crate::typed_absy::types::Type;
use crate::typed_absy::Identifier;
use std::fmt;
use typed_absy::types::StructMember;
use typed_absy::types::StructType;
#[derive(Clone, PartialEq, Hash, Eq)]
pub struct Variable<'ast> {
@ -27,7 +27,7 @@ impl<'ast> Variable<'ast> {
Self::with_id_and_type(id, Type::array(ty, size))
}
pub fn struc(id: Identifier<'ast>, ty: Vec<StructMember>) -> Variable<'ast> {
pub fn struc(id: Identifier<'ast>, ty: StructType) -> Variable<'ast> {
Self::with_id_and_type(id, Type::Struct(ty))
}