difftreelog
refactor(evm-coder)! manual macro parsing
in: master
We have plans to allow evm-coder to generate Callables for externally defined contracts, however our current attribute parsing was limiting code maintanability, rework was needed before starting to implement new features BREAKING CHANGE: solidity_interface definitions may now look slightly different, for example interface names now should be identifiers, not strings
8 files changed
crates/evm-coder-macros/Cargo.tomldiffbeforeafterboth1[package]1[package]2name = "evm-coder-macros"2name = "evm-coder-macros"3version = "0.1.0"3version = "0.2.0"4license = "GPLv3"4license = "GPLv3"5edition = "2021"5edition = "2021"667[lib]7[lib]8proc-macro = true8proc-macro = true9910[dependencies]10[dependencies]11sha3 = "0.9.1"11sha3 = "0.10.1"12quote = "1.0"12quote = "1.0"13proc-macro2 = "1.0"13proc-macro2 = "1.0"14syn = { version = "1.0", features = ["full"] }14syn = { version = "1.0", features = ["full"] }15hex = "0.4.3"15hex = "0.4.3"16Inflector = "0.11.4"16Inflector = "0.11.4"17darling = "0.13.0"1817crates/evm-coder-macros/src/lib.rsdiffbeforeafterboth161617#![allow(dead_code)]17#![allow(dead_code)]181819use darling::FromMeta;20use inflector::cases;19use inflector::cases;21use proc_macro::TokenStream;20use proc_macro::TokenStream;22use quote::quote;21use quote::quote;23use sha3::{Digest, Keccak256};22use sha3::{Digest, Keccak256};24use syn::{23use syn::{25 AttributeArgs, DeriveInput, GenericArgument, Ident, ItemImpl, Pat, Path, PathArguments,24 DeriveInput, GenericArgument, Ident, ItemImpl, Pat, Path, PathArguments,26 PathSegment, Type, parse_macro_input, spanned::Spanned,25 PathSegment, Type, parse_macro_input, spanned::Spanned, Attribute, parse::Parse,27};26};282729mod solidity_interface;28mod solidity_interface;254/// ```253/// ```255#[proc_macro_attribute]254#[proc_macro_attribute]256pub fn solidity_interface(args: TokenStream, stream: TokenStream) -> TokenStream {255pub fn solidity_interface(args: TokenStream, stream: TokenStream) -> TokenStream {257 let args = parse_macro_input!(args as AttributeArgs);256 let args = parse_macro_input!(args as solidity_interface::InterfaceInfo);258 let args = solidity_interface::InterfaceInfo::from_list(&args).unwrap();259257260 let input: ItemImpl = match syn::parse(stream) {258 let input: ItemImpl = match syn::parse(stream) {261 Ok(t) => t,259 Ok(t) => t,crates/evm-coder-macros/src/solidity_interface.rsdiffbeforeafterboth161617#![allow(dead_code)]17#![allow(dead_code)]181819use quote::quote;20use darling::{FromMeta, ToTokens};19use quote::{quote, ToTokens};21use inflector::cases;20use inflector::cases;22use std::fmt::Write;21use std::fmt::Write;23use syn::{22use syn::{24 Expr, FnArg, GenericArgument, Generics, Ident, ImplItem, ImplItemMethod, ItemImpl, Lit, Meta,23 Expr, FnArg, GenericArgument, Generics, Ident, ImplItem, ImplItemMethod, ItemImpl, Lit, Meta,25 MetaNameValue, NestedMeta, PatType, Path, PathArguments, ReturnType, Type, spanned::Spanned,24 MetaNameValue, PatType, PathArguments, ReturnType, Type,25 spanned::Spanned,26 parse_str,26 parse::{Parse, ParseStream},27 parenthesized, Token, LitInt, LitStr,27};28};282929use crate::{30use crate::{39 via: Option<(Type, Ident)>,40 via: Option<(Type, Ident)>,40}41}41impl Is {42impl Is {42 fn new_via(path: &Path, via: Option<(Type, Ident)>) -> syn::Result<Self> {43 let name = parse_ident_from_path(path, false)?.clone();44 Ok(Self {45 pascal_call_name: pascal_ident_to_call(&name),46 snake_call_name: pascal_ident_to_snake_call(&name),47 name,48 via,49 })50 }51 fn new(path: &Path) -> syn::Result<Self> {52 Self::new_via(path, None)53 }5455 fn expand_call_def(&self, gen_ref: &proc_macro2::TokenStream) -> proc_macro2::TokenStream {43 fn expand_call_def(&self, gen_ref: &proc_macro2::TokenStream) -> proc_macro2::TokenStream {56 let name = &self.name;44 let name = &self.name;137125138#[derive(Default)]126#[derive(Default)]139struct IsList(Vec<Is>);127struct IsList(Vec<Is>);140impl FromMeta for IsList {128impl Parse for IsList {129 fn parse(input: ParseStream) -> syn::Result<Self> {130 let mut out = vec![];131 loop {132 if input.is_empty() {133 break;134 }135 let name = input.parse::<Ident>()?;136 let lookahead = input.lookahead1();137 let via = if lookahead.peek(syn::token::Paren) {138 let contents;139 parenthesized!(contents in input);140 let method = contents.parse::<Ident>()?;141 contents.parse::<Token![,]>()?;142 let ty = contents.parse::<Type>()?;143 Some((ty, method))144 } else if lookahead.peek(Token![,]) {145 None146 } else if input.is_empty() {147 None148 } else {149 return Err(lookahead.error());150 };151 out.push(Is {152 pascal_call_name: pascal_ident_to_call(&name),153 snake_call_name: pascal_ident_to_snake_call(&name),154 name,155 via,156 });157 if input.peek(Token![,]) {158 input.parse::<Token![,]>()?;159 continue;160 } else {161 break;162 }163 }164 Ok(Self(out))165 }166}167168pub struct InterfaceInfo {169 name: Ident,170 is: IsList,171 inline_is: IsList,172 events: IsList,173 expect_selector: Option<u32>,174}175impl Parse for InterfaceInfo {141 fn from_list(items: &[NestedMeta]) -> darling::Result<Self> {176 fn parse(input: ParseStream) -> syn::Result<Self> {142 let mut out = Vec::new();177 let mut name = None;143 for item in items {178 let mut is = None;144 match item {179 let mut inline_is = None;145 NestedMeta::Meta(Meta::Path(path)) => out.push(Is::new(path)?),180 let mut events = None;146 // TODO: replace meta parsing with manual181 let mut expect_selector = None;147 NestedMeta::Meta(Meta::List(list))182 // TODO: create proc-macro to optimize proc-macro boilerplate? :D183 loop {184 let lookahead = input.lookahead1();148 if list.path.is_ident("via") && list.nested.len() == 3 =>185 if lookahead.peek(kw::name) {149 {186 let k = input.parse::<kw::name>()?;150 let mut data = list.nested.iter();187 input.parse::<Token![=]>()?;151 let typ = match data.next().expect("len == 3") {188 if name.replace(input.parse::<Ident>()?).is_some() {152 NestedMeta::Lit(Lit::Str(s)) => {153 let v = s.value();154 let typ: Type = parse_str(&v)?;155 typ156 }157 _ => {158 return Err(syn::Error::new(189 return Err(syn::Error::new(k.span(), "name is already set"));159 item.span(),160 "via typ should be type in string",161 )162 .into())163 }164 };190 }191 } else if lookahead.peek(kw::is) {165 let via = match data.next().expect("len == 3") {192 let k = input.parse::<kw::is>()?;193 let contents;194 parenthesized!(contents in input);195 if is.replace(contents.parse::<IsList>()?).is_some() {196 return Err(syn::Error::new(k.span(), "is is already set"));197 }198 } else if lookahead.peek(kw::inline_is) {199 let k = input.parse::<kw::inline_is>()?;200 let contents;201 parenthesized!(contents in input);166 NestedMeta::Meta(Meta::Path(path)) => path202 if inline_is.replace(contents.parse::<IsList>()?).is_some() {167 .get_ident()168 .ok_or_else(|| syn::Error::new(item.span(), "via should be ident"))?,203 return Err(syn::Error::new(k.span(), "inline_is is already set"));204 }205 } else if lookahead.peek(kw::events) {206 let k = input.parse::<kw::events>()?;207 let contents;208 parenthesized!(contents in input);209 if events.replace(contents.parse::<IsList>()?).is_some() {169 _ => return Err(syn::Error::new(item.span(), "via should be ident").into()),210 return Err(syn::Error::new(k.span(), "events is already set"));170 };211 }212 } else if lookahead.peek(kw::expect_selector) {213 let k = input.parse::<kw::expect_selector>()?;214 input.parse::<Token![=]>()?;215 let value = input.parse::<LitInt>()?;216 if expect_selector217 .replace(value.base10_parse::<u32>()?)218 .is_some()219 {220 return Err(syn::Error::new(k.span(), "expect_selector is already set"));221 }222 } else if input.is_empty() {223 break;224 } else {225 return Err(lookahead.error());226 }171 let path = match data.next().expect("len == 3") {227 if input.peek(Token![,]) {172 NestedMeta::Meta(Meta::Path(path)) => path,173 _ => return Err(syn::Error::new(item.span(), "path should be path").into()),228 input.parse::<Token![,]>()?;174 };229 } else {175230 break;176 out.push(Is::new_via(path, Some((typ, via.clone())))?)231 }177 }232 }178 _ => {233 Ok(Self {179 return Err(syn::Error::new(234 name: name.ok_or_else(|| syn::Error::new(input.span(), "missing name"))?,180 item.span(),235 is: is.unwrap_or_default(),181 "expected either Name or via(\"Type\", getter, Name)",236 inline_is: inline_is.unwrap_or_default(),182 )237 events: events.unwrap_or_default(),183 .into())238 expect_selector,184 }239 })185 }186 }187 Ok(Self(out))188 }240 }189}241}190242191#[derive(FromMeta)]192pub struct InterfaceInfo {243struct MethodInfo {193 name: Ident,244 rename_selector: Option<String>,194 #[darling(default)]195 is: IsList,196 #[darling(default)]197 inline_is: IsList,198 #[darling(default)]199 events: IsList,200}245}201246impl Parse for MethodInfo {202#[derive(FromMeta)]203struct MethodInfo {247 fn parse(input: ParseStream) -> syn::Result<Self> {248 let mut rename_selector = None;204 #[darling(default)]249 let lookahead = input.lookahead1();205 rename_selector: Option<String>,250 if lookahead.peek(kw::rename_selector) {251 let k = input.parse::<kw::rename_selector>()?;252 input.parse::<Token![=]>()?;253 if rename_selector254 .replace(input.parse::<LitStr>()?.value())255 .is_some()256 {257 return Err(syn::Error::new(k.span(), "rename_selector is already set"));258 }259 }260 Ok(Self { rename_selector })206}261 }262}207263208enum AbiType {264enum AbiType {209 // type265 // type258 "expected only one generic for vec",314 "expected only one generic for vec",259 ));315 ));260 }316 }261 let arg = args.first().unwrap();317 let arg = args.first().expect("first arg");262318263 let ty = match arg {319 let ty = match arg {264 GenericArgument::Type(ty) => ty,320 GenericArgument::Type(ty) => ty,429 Pure,485 Pure,430}486}431432pub struct WeightAttr(syn::Expr);433487434mod keyword {488mod kw {435 syn::custom_keyword!(weight);489 syn::custom_keyword!(weight);436}437490438impl syn::parse::Parse for WeightAttr {491 syn::custom_keyword!(via);439 fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {492 syn::custom_keyword!(name);440 input.parse::<syn::Token![#]>()?;493 syn::custom_keyword!(is);441 let content;442 syn::bracketed!(content in input);494 syn::custom_keyword!(inline_is);443 content.parse::<keyword::weight>()?;495 syn::custom_keyword!(events);444445 let weight_content;446 syn::parenthesized!(weight_content in content);496 syn::custom_keyword!(expect_selector);497447 Ok(WeightAttr(weight_content.parse::<syn::Expr>()?))498 syn::custom_keyword!(rename_selector);448 }499}449}450500451struct Method {501struct Method {452 name: Ident,502 name: Ident,472 for attr in &value.attrs {522 for attr in &value.attrs {473 let ident = parse_ident_from_path(&attr.path, false)?;523 let ident = parse_ident_from_path(&attr.path, false)?;474 if ident == "solidity" {524 if ident == "solidity" {475 let args = attr.parse_meta().unwrap();476 info = MethodInfo::from_meta(&args).unwrap();525 info = attr.parse_args::<MethodInfo>()?;477 } else if ident == "doc" {526 } else if ident == "doc" {478 let args = attr.parse_meta().unwrap();527 let args = attr.parse_meta().unwrap();479 let value = match args {528 let value = match args {484 };533 };485 docs.push(value);534 docs.push(value);486 } else if ident == "weight" {535 } else if ident == "weight" {487 weight = Some(syn::parse2::<WeightAttr>(attr.to_token_stream())?.0);536 weight = Some(attr.parse_args::<Expr>()?);488 }537 }489 }538 }490 let ident = &value.sig.ident;539 let ident = &value.sig.ident;869 .map(|is| Is::expand_generator(is, &gen_ref));918 .map(|is| Is::expand_generator(is, &gen_ref));870 let solidity_event_generators = self.info.events.0.iter().map(Is::expand_event_generator);919 let solidity_event_generators = self.info.events.0.iter().map(Is::expand_event_generator);871920921 if let Some(expect_selector) = &self.info.expect_selector {922 if !self.info.inline_is.0.is_empty() {923 return syn::Error::new(924 name.span(),925 "expect_selector is not compatible with inline_is",926 )927 .to_compile_error();928 }929 let selector = self930 .methods931 .iter()932 .map(|m| m.selector)933 .fold(0, |a, b| a ^ b);934935 if *expect_selector != selector {936 let mut methods = String::new();937 for meth in self.methods.iter() {938 write!(methods, "\n- {}", meth.selector_str).expect("write to string");939 }940 return syn::Error::new(name.span(), format!("expected selector mismatch, expected {expect_selector:0>8x}, but implementation has {selector:0>8x}{methods}")).to_compile_error();941 }942 }872 // let methods = self.methods.iter().map(Method::solidity_def);943 // let methods = self.methods.iter().map(Method::solidity_def);873944874 quote! {945 quote! {917 )*),988 )*),918 };989 };919 if is_impl {990 if is_impl {920 tc.collect("// Common stubs holder\ncontract Dummy {\n\tuint8 dummy;\n\tstring stub_error = \"this contract is implemented in native\";\n}\ncontract ERC165 is Dummy {\n\tfunction supportsInterface(bytes4 interfaceID) external view returns (bool) {\n\t\trequire(false, stub_error);\n\t\tinterfaceID;\n\t\treturn true;\n\t}\n}\n".into());991 tc.collect("/// @dev common stubs holder\ncontract Dummy {\n\tuint8 dummy;\n\tstring stub_error = \"this contract is implemented in native\";\n}\ncontract ERC165 is Dummy {\n\tfunction supportsInterface(bytes4 interfaceID) external view returns (bool) {\n\t\trequire(false, stub_error);\n\t\tinterfaceID;\n\t\treturn true;\n\t}\n}\n".into());921 } else {992 } else {922 tc.collect("// Common stubs holder\ninterface Dummy {\n}\ninterface ERC165 is Dummy {\n\tfunction supportsInterface(bytes4 interfaceID) external view returns (bool);\n}\n".into());993 tc.collect("/// @dev common stubs holder\ninterface Dummy {\n}\ninterface ERC165 is Dummy {\n\tfunction supportsInterface(bytes4 interfaceID) external view returns (bool);\n}\n".into());923 }994 }924 #(995 #(925 #solidity_generators996 #solidity_generators9301001931 let mut out = string::new();1002 let mut out = string::new();932 // In solidity interface usage (is) should be preceeded by interface definition1003 // In solidity interface usage (is) should be preceeded by interface definition933 // This comment helps to sort it in a set1004 // HACK: this comment helps to sort it in a set934 if #solidity_name.starts_with("Inline") {1005 if #solidity_name.starts_with("Inline") {935 out.push_str("// Inline\n");1006 out.push_str("/// @dev inlined interface\n");936 }1007 }937 let _ = interface.format(is_impl, &mut out, tc);1008 let _ = interface.format(is_impl, &mut out, tc);938 tc.collect(out);1009 tc.collect(out);crates/evm-coder-macros/src/to_log.rsdiffbeforeafterboth207 )*),207 )*),208 };208 };209 let mut out = string::new();209 let mut out = string::new();210 out.push_str("// Inline\n");210 out.push_str("/// @dev inlined interface\n");211 let _ = interface.format(is_impl, &mut out, tc);211 let _ = interface.format(is_impl, &mut out, tc);212 tc.collect(out);212 tc.collect(out);213 }213 }crates/evm-coder/src/solidity.rsdiffbeforeafterboth56 }56 }57 let id = self.next_id();57 let id = self.next_id();58 let mut str = String::new();58 let mut str = String::new();59 writeln!(str, "// Anonymous struct").unwrap();59 writeln!(str, "/// @dev anonymous struct").unwrap();60 writeln!(str, "struct Tuple{} {{", id).unwrap();60 writeln!(str, "struct Tuple{} {{", id).unwrap();61 for (i, name) in names.iter().enumerate() {61 for (i, name) in names.iter().enumerate() {62 writeln!(str, "\t{} field_{};", name, i).unwrap();62 writeln!(str, "\t{} field_{};", name, i).unwrap();416 tc: &TypeCollector,416 tc: &TypeCollector,417 ) -> fmt::Result {417 ) -> fmt::Result {418 for doc in self.docs {418 for doc in self.docs {419 writeln!(writer, "\t//{}", doc)?;419 writeln!(writer, "\t///{}", doc)?;420 }420 }421 if !self.docs.is_empty() {421 if !self.docs.is_empty() {422 writeln!(writer, "\t//")?;422 writeln!(writer, "\t///")?;423 }423 }424 writeln!(writer, "\t// Selector: {}", self.selector)?;424 writeln!(writer, "\t/// Selector: {}", self.selector)?;425 write!(writer, "\tfunction {}(", self.name)?;425 write!(writer, "\tfunction {}(", self.name)?;426 self.args.solidity_name(writer, tc)?;426 self.args.solidity_name(writer, tc)?;427 write!(writer, ")")?;427 write!(writer, ")")?;498 if self.selector != ZERO_BYTES {498 if self.selector != ZERO_BYTES {499 writeln!(499 writeln!(500 out,500 out,501 "// Selector: {:0>8x}",501 "/// @dev the ERC-165 identifier for this interface is 0x{:0>8x}",502 u32::from_be_bytes(self.selector)502 u32::from_be_bytes(self.selector)503 )?;503 )?;504 }504 }crates/evm-coder/tests/generics.rsdiffbeforeafterboth191920struct Generic<T>(PhantomData<T>);20struct Generic<T>(PhantomData<T>);212122#[solidity_interface(name = "GenericIs")]22#[solidity_interface(name = GenericIs)]23impl<T> Generic<T> {23impl<T> Generic<T> {24 fn test_1(&self) -> Result<uint256> {24 fn test_1(&self) -> Result<uint256> {25 unreachable!()25 unreachable!()26 }26 }27}27}282829#[solidity_interface(name = "Generic", is(GenericIs))]29#[solidity_interface(name = Generic, is(GenericIs))]30impl<T: Into<u32>> Generic<T> {30impl<T: Into<u32>> Generic<T> {31 fn test_2(&self) -> Result<uint256> {31 fn test_2(&self) -> Result<uint256> {32 unreachable!()32 unreachable!()353536generate_stubgen!(gen_iface, GenericCall<()>, false);36generate_stubgen!(gen_iface, GenericCall<()>, false);373738#[solidity_interface(name = "GenericWhere")]38#[solidity_interface(name = GenericWhere)]39impl<T> Generic<T>39impl<T> Generic<T>40where40where41 T: core::fmt::Debug,41 T: core::fmt::Debug,crates/evm-coder/tests/random.rsdiffbeforeafterboth212122struct Impls;22struct Impls;232324#[solidity_interface(name = "OurInterface")]24#[solidity_interface(name = OurInterface)]25impl Impls {25impl Impls {26 fn fn_a(&self, _input: uint256) -> Result<bool> {26 fn fn_a(&self, _input: uint256) -> Result<bool> {27 unreachable!()27 unreachable!()28 }28 }29}29}303031#[solidity_interface(name = "OurInterface1")]31#[solidity_interface(name = OurInterface1)]32impl Impls {32impl Impls {33 fn fn_b(&self, _input: uint128) -> Result<uint32> {33 fn fn_b(&self, _input: uint128) -> Result<uint32> {34 unreachable!()34 unreachable!()48}48}494950#[solidity_interface(50#[solidity_interface(51 name = "OurInterface2",51 name = OurInterface2,52 is(OurInterface),52 is(OurInterface),53 inline_is(OurInterface1),53 inline_is(OurInterface1),54 events(OurEvents)54 events(OurEvents)80 }80 }81}81}8283#[solidity_interface(84 name = ValidSelector,85 expect_selector = 0x00000000,86)]87impl Impls {}8288crates/evm-coder/tests/solidity_generation.rsdiffbeforeafterboth181819struct ERC20;19struct ERC20;202021#[solidity_interface(name = "ERC20")]21#[solidity_interface(name = ERC20)]22impl ERC20 {22impl ERC20 {23 fn decimals(&self) -> Result<uint8> {23 fn decimals(&self) -> Result<uint8> {24 unreachable!()24 unreachable!()