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

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);3334	Ok(quote! {35		#can_be_plcaed_in_vec36		#abi_type37		#abi_read38		#abi_write39	})40}4142fn impl_can_be_placed_in_vec(ident: &syn::Ident) -> proc_macro2::TokenStream {43	quote! {44		impl ::evm_coder::abi::sealed::CanBePlacedInVec for #ident {}45	}46}4748fn map_field_to_name(field: (usize, &syn::Field)) -> syn::Ident {49	match field.1.ident.as_ref() {50		Some(name) => name.clone(),51		None => {52			let mut name = "field".to_string();53			name.push_str(field.0.to_string().as_str());54			syn::Ident::new(name.as_str(), proc_macro2::Span::call_site())55		}56	}57}5859fn map_field_to_type<'a>(field: &'a syn::Field) -> &'a syn::Type {60	&field.ty61}6263fn impl_abi_type<'a>(64	name: &syn::Ident,65	field_types: impl Iterator<Item = &'a syn::Type> + Clone,66) -> proc_macro2::TokenStream {67	let mut params_signature = {68		let types = field_types.clone();69		quote!(70			#(nameof(<#types as ::evm_coder::abi::AbiType>::SIGNATURE) fixed(","))*71		)72	};7374	params_signature.extend(quote!(shift_left(1)));7576	let fields_for_dynamic = field_types.clone();7778	quote! {79		impl ::evm_coder::abi::AbiType for #name {80			const SIGNATURE: ::evm_coder::custom_signature::SignatureUnit = ::evm_coder::make_signature!(81				new fixed("(")82				#params_signature83				fixed(")")84			);85			fn is_dynamic() -> bool {86				false87				#(88					|| <#fields_for_dynamic as ::evm_coder::abi::AbiType>::is_dynamic()89				)*90			}91			fn size() -> usize {92				0 #(+ <#field_types as ::evm_coder::abi::AbiType>::size())*93			}94		}95	}96}9798fn impl_abi_read<'a>(99	name: &syn::Ident,100	is_named_fields: bool,101	field_names: impl Iterator<Item = proc_macro2::Ident> + Clone,102	field_types: impl Iterator<Item = &'a syn::Type> + Clone,103) -> proc_macro2::TokenStream {104	let field_names1 = field_names.clone();105106	let struct_constructor = if is_named_fields {107		quote!(Ok(Self { #(#field_names1),* }))108	} else {109		quote!(Ok(Self ( #(#field_names1),* )))110	};111	quote!(112		impl ::evm_coder::abi::AbiRead for #name {113			fn abi_read(reader: &mut ::evm_coder::abi::AbiReader) -> ::evm_coder::execution::Result<Self> {114				let is_dynamic = <Self as ::evm_coder::abi::AbiType>::is_dynamic();115				let size = if !is_dynamic {116					Some(<Self as ::evm_coder::abi::AbiType>::size())117				} else {118					None119				};120				let mut subresult = reader.subresult(size)?;121				#(122					let #field_names = {123						let value = <#field_types as ::evm_coder::abi::AbiRead>::abi_read(&mut subresult)?;124						if !is_dynamic {subresult.seek(<#field_types as ::evm_coder::abi::AbiType>::size())};125						value126					};127				)*128129				#struct_constructor130			}131		}132	)133}134135fn impl_abi_write<'a>(136	name: &syn::Ident,137	is_named_fields: bool,138	params_count: usize,139	field_names: impl Iterator<Item = proc_macro2::Ident> + Clone,140) -> proc_macro2::TokenStream {141	let abi_write = if is_named_fields {142		quote!(143			#(144				::evm_coder::abi::AbiWrite::abi_write(&self.#field_names, sub);145			)*146		)147	} else {148		let field_names = (0..params_count)149			.into_iter()150			.map(proc_macro2::Literal::usize_unsuffixed);151		quote!(152			#(153				::evm_coder::abi::AbiWrite::abi_write(&self.#field_names, sub);154			)*155		)156	};157	quote!(158		impl ::evm_coder::abi::AbiWrite for #name {159			fn abi_write(&self, writer: &mut ::evm_coder::abi::AbiWriter) {160				if <Self as ::evm_coder::abi::AbiType>::is_dynamic() {161					let mut sub = ::evm_coder::abi::AbiWriter::new();162					{163						let sub = &mut sub;164						#abi_write165					}166					writer.write_subresult(sub);167				} else {168					let sub = writer;169					#abi_write170				}171			}172		}173	)174}