Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
DaniPopes committed Aug 5, 2023
1 parent 1323d45 commit fd0ef21
Show file tree
Hide file tree
Showing 15 changed files with 136 additions and 122 deletions.
16 changes: 9 additions & 7 deletions crates/sol-macro/src/expand/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ fn rec_expand_type(ty: &Type, tokens: &mut TokenStream) {
Type::Array(ref array) => {
let ty = expand_type(&array.ty);
let span = array.span();
if let Some(size) = &array.size {
if let Some(size) = array.size() {
quote_spanned! {span=>
::alloy_sol_types::sol_data::FixedArray<#ty, #size>
}
Expand Down Expand Up @@ -148,11 +148,13 @@ pub(super) fn type_base_data_size(cx: &ExpCtxt<'_>, ty: &Type) -> usize {
Type::String(_) | Type::Bytes(_) | Type::Array(TypeArray { size: None, .. }) => 64,

// fixed array: size * encoded size
Type::Array(TypeArray {
ty: inner,
size: Some(size),
..
}) => type_base_data_size(cx, inner) * size.base10_parse::<usize>().unwrap(),
Type::Array(
a @ TypeArray {
ty: inner,
size: Some(_),
..
},
) => type_base_data_size(cx, inner) * a.size().unwrap(),

// tuple: sum of encoded sizes
Type::Tuple(tuple) => tuple
Expand Down Expand Up @@ -290,7 +292,7 @@ impl fmt::Display for TypePrinter<'_> {
Type::Array(array) => {
Self::new(self.cx, &array.ty).fmt(f)?;
f.write_str("[")?;
if let Some(size) = &array.size {
if let Some(size) = array.size() {
size.fmt(f)?;
}
f.write_str("]")
Expand Down
15 changes: 7 additions & 8 deletions crates/syn-solidity/src/attribute/function.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::{kw, Modifier, Mutability, Override, SolPath, Spanned, VariableAttribute, Visibility};
use proc_macro2::Span;
use std::{
collections::HashSet,
fmt,
hash::{Hash, Hasher},
mem,
Expand All @@ -17,7 +16,7 @@ use syn::{
/// A list of unique function attributes. Used in
/// [ItemFunction][crate::ItemFunction].
#[derive(Clone, Default, PartialEq, Eq)]
pub struct FunctionAttributes(pub HashSet<FunctionAttribute>);
pub struct FunctionAttributes(pub Vec<FunctionAttribute>);

impl fmt::Debug for FunctionAttributes {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
Expand All @@ -26,7 +25,7 @@ impl fmt::Debug for FunctionAttributes {
}

impl Deref for FunctionAttributes {
type Target = HashSet<FunctionAttribute>;
type Target = Vec<FunctionAttribute>;

fn deref(&self) -> &Self::Target {
&self.0
Expand All @@ -41,19 +40,19 @@ impl DerefMut for FunctionAttributes {

impl Parse for FunctionAttributes {
fn parse(input: ParseStream<'_>) -> Result<Self> {
let mut attributes = HashSet::<FunctionAttribute>::new();
let mut attributes = Vec::<FunctionAttribute>::new();
while !(input.is_empty()
|| input.peek(kw::returns)
|| input.peek(Token![;])
|| input.peek(Brace))
{
let attr = input.parse()?;
if let Some(prev) = attributes.get(&attr) {
let attr: FunctionAttribute = input.parse()?;
if let Some(prev) = attributes.iter().find(|a| **a == attr) {
let mut e = Error::new(attr.span(), "duplicate attribute");
e.combine(Error::new(prev.span(), "previous declaration is here"));
return Err(e)
}
attributes.insert(attr);
attributes.push(attr);
}
Ok(Self(attributes))
}
Expand All @@ -72,7 +71,7 @@ impl Spanned for FunctionAttributes {
impl FunctionAttributes {
#[inline]
pub fn new() -> Self {
Self(HashSet::new())
Self(Vec::new())
}

pub fn visibility(&self) -> Option<Visibility> {
Expand Down
19 changes: 10 additions & 9 deletions crates/syn-solidity/src/attribute/variable.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::{kw, Override, SolPath, Spanned, Visibility};
use proc_macro2::Span;
use std::{
collections::HashSet,
fmt,
hash::{Hash, Hasher},
mem,
Expand All @@ -13,11 +12,11 @@ use syn::{

/// A list of unique variable attributes.
#[derive(Clone, Debug)]
pub struct VariableAttributes(pub HashSet<VariableAttribute>);
pub struct VariableAttributes(pub Vec<VariableAttribute>);

impl Parse for VariableAttributes {
fn parse(input: ParseStream<'_>) -> Result<Self> {
let mut attributes = HashSet::new();
let mut attributes = Vec::new();
while let Ok(attribute) = input.parse::<VariableAttribute>() {
let error = |prev: &VariableAttribute| {
let mut e = Error::new(attribute.span(), "duplicate attribute");
Expand All @@ -28,26 +27,28 @@ impl Parse for VariableAttributes {
// Only one of: `constant`, `immutable`
match attribute {
VariableAttribute::Constant(_) => {
if let Some(prev) =
attributes.get(&VariableAttribute::Immutable(Default::default()))
if let Some(prev) = attributes
.iter()
.find(|a| matches!(a, VariableAttribute::Immutable(_)))
{
return Err(error(prev))
}
}
VariableAttribute::Immutable(_) => {
if let Some(prev) =
attributes.get(&VariableAttribute::Constant(Default::default()))
if let Some(prev) = attributes
.iter()
.find(|a| matches!(a, VariableAttribute::Constant(_)))
{
return Err(error(prev))
}
}
_ => {}
}

if let Some(prev) = attributes.get(&attribute) {
if let Some(prev) = attributes.iter().find(|a| **a == attribute) {
return Err(error(prev))
}
attributes.insert(attribute);
attributes.push(attribute);
}
Ok(Self(attributes))
}
Expand Down
21 changes: 19 additions & 2 deletions crates/syn-solidity/src/expr/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use crate::{kw, utils::ParseNested, Lit, SolIdent, Spanned, Type};
use crate::{
kw, utils::ParseNested, Lit, LitDenominated, SolIdent, Spanned, SubDenomination, Type,
};
use proc_macro2::{Ident, Span};
use std::fmt;
use syn::{
Expand Down Expand Up @@ -64,6 +66,9 @@ pub enum Expr {
/// A literal: `hex"1234"`.
Lit(Lit),

/// A number literal with a sub-denomination: `1 ether`.
LitDenominated(LitDenominated),

/// Access of a named member: `obj.k`.
Member(ExprMember),

Expand Down Expand Up @@ -106,6 +111,7 @@ impl fmt::Debug for Expr {
Self::Ident(ident) => ident.fmt(f),
Self::Index(expr) => expr.fmt(f),
Self::Lit(lit) => lit.fmt(f),
Self::LitDenominated(lit) => lit.fmt(f),
Self::Member(expr) => expr.fmt(f),
Self::New(expr) => expr.fmt(f),
Self::Payable(expr) => expr.fmt(f),
Expand Down Expand Up @@ -150,6 +156,7 @@ impl Spanned for Expr {
Self::Ident(ident) => ident.span(),
Self::Index(expr) => expr.span(),
Self::Lit(lit) => lit.span(),
Self::LitDenominated(lit) => lit.span(),
Self::Member(expr) => expr.span(),
Self::New(expr) => expr.span(),
Self::Payable(expr) => expr.span(),
Expand All @@ -172,6 +179,7 @@ impl Spanned for Expr {
Self::Ident(ident) => ident.set_span(span),
Self::Index(expr) => expr.set_span(span),
Self::Lit(lit) => lit.set_span(span),
Self::LitDenominated(lit) => lit.set_span(span),
Self::Member(expr) => expr.set_span(span),
Self::New(expr) => expr.set_span(span),
Self::Payable(expr) => expr.set_span(span),
Expand All @@ -195,7 +203,16 @@ impl Expr {
} else if UnOp::peek(input, &lookahead) {
input.parse().map(Self::Unary)
} else if Lit::peek(&lookahead) {
input.parse().map(Self::Lit)
match (input.parse()?, input.call(SubDenomination::parse_opt)?) {
(Lit::Number(number), Some(denom)) => {
Ok(Self::LitDenominated(LitDenominated { number, denom }))
}
(lit, None) => Ok(Self::Lit(lit)),
(_, Some(denom)) => Err(syn::Error::new(
denom.span(),
"unexpected subdenomination for literal",
)),
}
} else if lookahead.peek(kw::payable) {
input.parse().map(Self::Payable)
} else if lookahead.peek(Token![type]) {
Expand Down
18 changes: 14 additions & 4 deletions crates/syn-solidity/src/ident/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ use syn::{
mod path;
pub use path::SolPath;

// TODO: Deny Solidity keywords

/// A Solidity identifier.
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(transparent)]
Expand Down Expand Up @@ -58,9 +56,16 @@ impl From<SolIdent> for Ident {
}
}

impl From<&str> for SolIdent {
fn from(value: &str) -> Self {
Self::new(value)
}
}

impl Parse for SolIdent {
fn parse(input: ParseStream<'_>) -> Result<Self> {
input.call(Ident::parse_any).map(Self)
// TODO: Deny Solidity keywords
Self::parse_any(input)
}
}

Expand Down Expand Up @@ -98,7 +103,12 @@ impl SolIdent {
s
}

/// See `[Ident::peek_any]`.
/// Parses any identifier including keywords.
pub fn parse_any(input: ParseStream<'_>) -> Result<Self> {
input.call(Ident::parse_any).map(Self)
}

/// Peeks any identifier including keywords.
pub fn peek_any(input: ParseStream<'_>) -> bool {
input.peek(Ident::peek_any)
}
Expand Down
6 changes: 0 additions & 6 deletions crates/syn-solidity/src/ident/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,6 @@ macro_rules! sol_path {
$(path.push($crate::SolIdent::from($e));)+
path
}};

($($id:ident).+) => {{
let mut path = $crate::SolPath::new();
$(path.push($crate::SolIdent::new(stringify!($id))));+
path
}};
}

/// A list of identifiers, separated by dots.
Expand Down
2 changes: 1 addition & 1 deletion crates/syn-solidity/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ pub use item::{

mod lit;
pub use lit::{
HexStr, Lit, LitHexStr, LitNumber, LitNumberKind, LitStr, LitUnicodeStr, SubDenomination,
HexStr, Lit, LitDenominated, LitHexStr, LitNumber, LitStr, LitUnicodeStr, SubDenomination,
UnicodeStr,
};

Expand Down
5 changes: 2 additions & 3 deletions crates/syn-solidity/src/lit/mod.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
use std::fmt;

use crate::{kw, Spanned};
use proc_macro2::Span;
use std::fmt;
use syn::{
parse::{Lookahead1, Parse, ParseStream},
LitBool, Result,
};

mod number;
pub use number::{LitNumber, LitNumberKind, SubDenomination};
pub use number::{LitDenominated, LitNumber, SubDenomination};

mod str;
pub use self::str::{HexStr, LitHexStr, LitStr, LitUnicodeStr, UnicodeStr};
Expand Down
Loading

0 comments on commit fd0ef21

Please sign in to comment.