Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

unions abi wip #912

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions pint-abi-gen-tests/test-pkgs/simple/src/contract.pnt
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
union UU = A(int) | B | C(b256);
union WW = D | E(UU);

storage {
s0: bool,
s1: int,
Expand All @@ -9,6 +12,10 @@ storage {
my_nested_map0: (int => (int => int)),
my_nested_map1: (int => (b256 => { int, { b256, int } })),
my_array: int[5],
u1: UU,
u2: UU,
u3: UU,
w1: WW,
}

predicate Foo {
Expand Down Expand Up @@ -73,6 +80,16 @@ predicate Foo {
state my_nested_map1_key2_3: { int, { b256, int } } = mut storage::my_nested_map1[2][0x3333333333333333333333333333333333333333333333333333333333333333];
state my_array: int[5] = mut storage::my_array;

state u1 = mut storage::u1;
state u2 = mut storage::u2;
state u3 = mut storage::u3;
state w1 = mut storage::w1;

constraint u1' == UU::A(69);
constraint u2' == UU::B;
constraint u3' == UU::C(0x6969696969696969696969696969696969696969696969696969696969696969);
constraint w1' == WW::E(UU::A(69));

// Check the state.
constraint s0' == true;
constraint s1' == 42;
Expand Down
4 changes: 4 additions & 0 deletions pint-abi-gen-tests/tests/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ async fn test_solution_foo() {
.enumerate()
.fold(arr, |arr, (ix, val)| arr.entry(ix, val))
})
.u1(simple::UU::A(69))
.u2(simple::UU::B)
.u3(simple::UU::C([0x6969696969696969; 4]))
.w1(simple::WW::E(simple::UU::A(69)))
.into();

// Build the same set of keys, so we can ensure they match the mutations.
Expand Down
14 changes: 10 additions & 4 deletions pint-abi-gen/src/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use crate::{
construct_key_expr, keys, map, mutations, nesting_expr, nesting_key_doc_str, nesting_ty_str,
tuple, SingleKeyTy, TypeABI,
tuple, ModLevel, SingleKeyTy, TypeABI,
};
use pint_abi_visit::{KeyedVarTree, Nesting, NodeIx};
use proc_macro2::Span;
Expand Down Expand Up @@ -111,7 +111,12 @@ fn key_method_for_single_key(elem_nesting: &[Nesting]) -> syn::ImplItemFn {
fn key_method(tree: &KeyedVarTree, elem: NodeIx, elem_ty: &TypeABI) -> syn::ImplItemFn {
let elem_nesting = tree.nesting(elem);
match elem_ty {
TypeABI::Bool | TypeABI::Int | TypeABI::Real | TypeABI::String | TypeABI::B256 => (),
TypeABI::Bool
| TypeABI::Int
| TypeABI::Real
| TypeABI::String
| TypeABI::B256
| TypeABI::Union { .. } => (),
TypeABI::Array { .. } => return key_method_for_array(&elem_nesting),
TypeABI::Tuple { .. } => {
return key_method_for_tuple(&elem_nesting);
Expand Down Expand Up @@ -219,7 +224,7 @@ fn mutation_method_for_tuple(tup_nesting: &[Nesting]) -> syn::ImplItemFn {

/// An array mutation builder method for an element with a single-key value.
fn mutation_method_for_single_key(ty: &SingleKeyTy, elem_nesting: &[Nesting]) -> syn::ImplItemFn {
let ty = ty.syn_ty();
let ty = ty.syn_ty(&ModLevel::Mutations);
let nesting_expr: syn::ExprArray = nesting_expr(elem_nesting);
let construct_key_expr: syn::Expr = construct_key_expr();
let nesting_key_doc_str = nesting_key_doc_str(elem_nesting);
Expand Down Expand Up @@ -259,12 +264,13 @@ fn mutation_method(tree: &KeyedVarTree, elem: NodeIx, elem_ty: &TypeABI) -> syn:
TypeABI::Bool => SingleKeyTy::Bool,
TypeABI::Int => SingleKeyTy::Int,
TypeABI::Real => SingleKeyTy::Real,
TypeABI::Array { .. } => return mutation_method_for_array(&elem_nesting),
TypeABI::String => SingleKeyTy::String,
TypeABI::B256 => SingleKeyTy::B256,
TypeABI::Array { .. } => return mutation_method_for_array(&elem_nesting),
TypeABI::Tuple { .. } => {
return mutation_method_for_tuple(&elem_nesting);
}
TypeABI::Union { name, .. } => SingleKeyTy::Union(name.clone()),
TypeABI::Map { .. } => {
return mutation_method_for_map(&elem_nesting);
}
Expand Down
15 changes: 13 additions & 2 deletions pint-abi-gen/src/keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,12 @@ fn nested_items_from_node(tree: &KeyedVarTree, n: NodeIx) -> Vec<syn::Item> {
TypeABI::Tuple(_fields) => {
items.extend(tuple::keys_items(tree, n));
}
TypeABI::Bool | TypeABI::Int | TypeABI::Real | TypeABI::String | TypeABI::B256 => (),
TypeABI::Bool
| TypeABI::Int
| TypeABI::Real
| TypeABI::String
| TypeABI::B256
| TypeABI::Union { .. } => (),
}
items
}
Expand Down Expand Up @@ -128,7 +133,12 @@ fn method_for_single_key(name: &str, nesting: &[Nesting]) -> syn::ImplItemFn {
pub(crate) fn method_from_node(tree: &KeyedVarTree, n: NodeIx, name: &str) -> syn::ImplItemFn {
let nesting = tree.nesting(n);
match &tree[n].ty {
TypeABI::Bool | TypeABI::Int | TypeABI::Real | TypeABI::String | TypeABI::B256 => (),
TypeABI::Bool
| TypeABI::Int
| TypeABI::Real
| TypeABI::String
| TypeABI::B256
| TypeABI::Union { .. } => (),
TypeABI::Array { ty: _, size: _ } => {
return method_for_array(name, &nesting);
}
Expand Down Expand Up @@ -236,6 +246,7 @@ fn items(vars: &[VarABI]) -> Vec<syn::Item> {
/// A `keys` module for all `Keys`-related items.
pub(crate) fn module(vars: &[VarABI]) -> syn::ItemMod {
let items = items(vars);

syn::parse_quote! {
pub mod keys {
//! All items related to building a set of [`Keys`].
Expand Down
Loading
Loading