git.delta.rocks / unique-network / refs/commits / a16933bbf61b

difftreelog

source

crates/evm-coder/procedural/src/abi_derive.rs4.7 KiBsourcehistory
1use quote::quote;23pub(crate) fn impl_abi_macro(ast: &syn::DeriveInput) -> syn::Result<proc_macro2::TokenStream> {4	let name = &ast.ident;5	let (is_named_fields, field_names, field_types, params_count) = match &ast.data {6		syn::Data::Struct(ds) => match ds.fields {7			syn::Fields::Named(ref fields) => Ok((8				true,9				fields.named.iter().enumerate().map(map_field_to_name),10				fields.named.iter().map(map_field_to_type),11				fields.named.len(),12			)),13			syn::Fields::Unnamed(ref fields) => Ok((14				false,15				fields.unnamed.iter().enumerate().map(map_field_to_name),16				fields.unnamed.iter().map(map_field_to_type),17				fields.unnamed.len(),18			)),19			syn::Fields::Unit => Err(syn::Error::new(name.span(), "Unit structs not supported")),20		},21		syn::Data::Enum(_) => Err(syn::Error::new(name.span(), "Enums not supported")),22		syn::Data::Union(_) => Err(syn::Error::new(name.span(), "Unions not supported")),23	}?;2425	if params_count == 0 {26		return Err(syn::Error::new(name.span(), "Empty structs not supported"));27	};2829	let can_be_plcaed_in_vec = impl_can_be_placed_in_vec(name);30	let abi_type = impl_abi_type(name, field_types.clone());31	let abi_read = impl_abi_read(name, is_named_fields, field_names.clone(), field_types);32	let abi_write = impl_abi_write(name, is_named_fields, params_count, field_names);33	// let solidity_tuple_type =3435	Ok(quote! {36		#can_be_plcaed_in_vec37		#abi_type38		#abi_read39		#abi_write40	})41}4243fn impl_can_be_placed_in_vec(ident: &syn::Ident) -> proc_macro2::TokenStream {44	quote! {45		impl ::evm_coder::sealed::CanBePlacedInVec for #ident {}46	}47}4849fn map_field_to_name(field: (usize, &syn::Field)) -> syn::Ident {50	match field.1.ident.as_ref() {51		Some(name) => name.clone(),52		None => {53			let mut name = "field".to_string();54			name.push_str(field.0.to_string().as_str());55			syn::Ident::new(name.as_str(), proc_macro2::Span::call_site())56		}57	}58}5960fn map_field_to_type<'a>(field: &'a syn::Field) -> &'a syn::Type {61	&field.ty62}6364fn impl_abi_type<'a>(65	name: &syn::Ident,66	field_types: impl Iterator<Item = &'a syn::Type> + Clone,67) -> proc_macro2::TokenStream {68	let mut params_signature = {69		let types = field_types.clone();70		quote!(71			#(nameof(<#types as ::evm_coder::abi::AbiType>::SIGNATURE) fixed(","))*72		)73	};7475	params_signature.extend(quote!(shift_left(1)));7677	let fields_for_dynamic = field_types.clone();7879	quote! {80		impl ::evm_coder::abi::AbiType for #name {81			const SIGNATURE: ::evm_coder::custom_signature::SignatureUnit = ::evm_coder::make_signature!(82				new fixed("(")83				#params_signature84				fixed(")")85			);86			fn is_dynamic() -> bool {87				false88				#(89					|| <#fields_for_dynamic as ::evm_coder::abi::AbiType>::is_dynamic()90				)*91			}92			fn size() -> usize {93				0 #(+ <#field_types as ::evm_coder::abi::AbiType>::size())*94			}95		}96	}97}9899fn impl_abi_read<'a>(100	name: &syn::Ident,101	is_named_fields: bool,102	field_names: impl Iterator<Item = proc_macro2::Ident> + Clone,103	field_types: impl Iterator<Item = &'a syn::Type> + Clone,104) -> proc_macro2::TokenStream {105	let field_names1 = field_names.clone();106107	let struct_constructor = if is_named_fields {108		quote!(Ok(Self { #(#field_names1),* }))109	} else {110		quote!(Ok(Self ( #(#field_names1),* )))111	};112	quote!(113		impl ::evm_coder::abi::AbiRead for #name {114			fn abi_read(reader: &mut ::evm_coder::abi::AbiReader) -> ::evm_coder::execution::Result<Self> {115				let is_dynamic = <Self as ::evm_coder::abi::AbiType>::is_dynamic();116				let size = if !is_dynamic {117					Some(<Self as ::evm_coder::abi::AbiType>::size())118				} else {119					None120				};121				let mut subresult = reader.subresult(size)?;122				#(123					let #field_names = {124						let value = <#field_types as ::evm_coder::abi::AbiRead>::abi_read(&mut subresult)?;125						if !is_dynamic {subresult.bytes_read(<#field_types as ::evm_coder::abi::AbiType>::size())};126						value127					};128				)*129130				#struct_constructor131			}132		}133	)134}135136fn impl_abi_write<'a>(137	name: &syn::Ident,138	is_named_fields: bool,139	params_count: usize,140	field_names: impl Iterator<Item = proc_macro2::Ident> + Clone,141) -> proc_macro2::TokenStream {142	let abi_write = if is_named_fields {143		quote!(144			#(145				::evm_coder::abi::AbiWrite::abi_write(&self.#field_names, sub);146			)*147		)148	} else {149		let field_names = (0..params_count)150			.into_iter()151			.map(proc_macro2::Literal::usize_unsuffixed);152		quote!(153			#(154				::evm_coder::abi::AbiWrite::abi_write(&self.#field_names, sub);155			)*156		)157	};158	quote!(159		impl ::evm_coder::abi::AbiWrite for #name {160			fn abi_write(&self, writer: &mut ::evm_coder::abi::AbiWriter) {161				if <Self as ::evm_coder::abi::AbiType>::is_dynamic() {162					let mut sub = ::evm_coder::abi::AbiWriter::new();163					{164						let sub = &mut sub;165						#abi_write166					}167					writer.write_subresult(sub);168				} else {169					let sub = writer;170					#abi_write171				}172			}173		}174	)175}