Unverified Commit beaf989c authored by Robin Freyler's avatar Robin Freyler Committed by GitHub
Browse files

Minor simplifications and code dupe reductions in codegen (#938)

* use ir::Receiver ToTokens impl in codegen

* slightly alter codegen of fn definitions in item impls

* impl ToTokens for Visibility and use it in codegen where possible
parent 0c7e9a31
Pipeline #159295 passed with stages
in 30 minutes and 43 seconds
...@@ -61,10 +61,7 @@ impl ItemImpls<'_> { ...@@ -61,10 +61,7 @@ impl ItemImpls<'_> {
fn generate_trait_constructor(constructor: &ir::Constructor) -> TokenStream2 { fn generate_trait_constructor(constructor: &ir::Constructor) -> TokenStream2 {
let span = constructor.span(); let span = constructor.span();
let attrs = constructor.attrs(); let attrs = constructor.attrs();
let vis = match constructor.visibility() { let vis = constructor.visibility();
ir::Visibility::Inherited => None,
ir::Visibility::Public(vis_public) => Some(vis_public),
};
let ident = constructor.ident(); let ident = constructor.ident();
let output_ident = format_ident!("{}Out", ident.to_string().to_camel_case()); let output_ident = format_ident!("{}Out", ident.to_string().to_camel_case());
let inputs = constructor.inputs(); let inputs = constructor.inputs();
...@@ -83,14 +80,8 @@ impl ItemImpls<'_> { ...@@ -83,14 +80,8 @@ impl ItemImpls<'_> {
fn generate_trait_message(message: &ir::Message) -> TokenStream2 { fn generate_trait_message(message: &ir::Message) -> TokenStream2 {
let span = message.span(); let span = message.span();
let attrs = message.attrs(); let attrs = message.attrs();
let vis = match message.visibility() { let vis = message.visibility();
ir::Visibility::Inherited => None, let receiver = message.receiver();
ir::Visibility::Public(vis_public) => Some(vis_public),
};
let receiver = match message.receiver() {
ir::Receiver::RefMut => quote! { &mut self },
ir::Receiver::Ref => quote! { &self },
};
let ident = message.ident(); let ident = message.ident();
let output_ident = format_ident!("{}Out", ident.to_string().to_camel_case()); let output_ident = format_ident!("{}Out", ident.to_string().to_camel_case());
let inputs = message.inputs(); let inputs = message.inputs();
...@@ -103,7 +94,7 @@ impl ItemImpls<'_> { ...@@ -103,7 +94,7 @@ impl ItemImpls<'_> {
type #output_ident = #output; type #output_ident = #output;
#( #attrs )* #( #attrs )*
#vis fn #ident(#receiver #(, #inputs )* ) -> Self::#output_ident { #vis fn #ident(#receiver #( , #inputs )* ) -> Self::#output_ident {
#( #statements )* #( #statements )*
} }
) )
...@@ -164,10 +155,7 @@ impl ItemImpls<'_> { ...@@ -164,10 +155,7 @@ impl ItemImpls<'_> {
fn generate_inherent_constructor(constructor: &ir::Constructor) -> TokenStream2 { fn generate_inherent_constructor(constructor: &ir::Constructor) -> TokenStream2 {
let span = constructor.span(); let span = constructor.span();
let attrs = constructor.attrs(); let attrs = constructor.attrs();
let vis = match constructor.visibility() { let vis = constructor.visibility();
ir::Visibility::Inherited => None,
ir::Visibility::Public(vis_public) => Some(vis_public),
};
let ident = constructor.ident(); let ident = constructor.ident();
let inputs = constructor.inputs(); let inputs = constructor.inputs();
let statements = constructor.statements(); let statements = constructor.statements();
...@@ -183,14 +171,8 @@ impl ItemImpls<'_> { ...@@ -183,14 +171,8 @@ impl ItemImpls<'_> {
fn generate_inherent_message(message: &ir::Message) -> TokenStream2 { fn generate_inherent_message(message: &ir::Message) -> TokenStream2 {
let span = message.span(); let span = message.span();
let attrs = message.attrs(); let attrs = message.attrs();
let vis = match message.visibility() { let vis = message.visibility();
ir::Visibility::Inherited => None, let receiver = message.receiver();
ir::Visibility::Public(vis_public) => Some(vis_public),
};
let receiver = match message.receiver() {
ir::Receiver::RefMut => quote! { &mut self },
ir::Receiver::Ref => quote! { &self },
};
let ident = message.ident(); let ident = message.ident();
let inputs = message.inputs(); let inputs = message.inputs();
let output_arrow = message.output().map(|_| quote! { -> }); let output_arrow = message.output().map(|_| quote! { -> });
...@@ -198,7 +180,7 @@ impl ItemImpls<'_> { ...@@ -198,7 +180,7 @@ impl ItemImpls<'_> {
let statements = message.statements(); let statements = message.statements();
quote_spanned!(span => quote_spanned!(span =>
#( #attrs )* #( #attrs )*
#vis fn #ident(#receiver, #( #inputs ),* ) #output_arrow #output { #vis fn #ident(#receiver #( , #inputs )* ) #output_arrow #output {
#( #statements )* #( #statements )*
} }
) )
......
...@@ -440,6 +440,15 @@ pub enum Visibility { ...@@ -440,6 +440,15 @@ pub enum Visibility {
Inherited, Inherited,
} }
impl quote::ToTokens for Visibility {
fn to_tokens(&self, tokens: &mut proc_macro2::TokenStream) {
match self {
Self::Public(vis_public) => vis_public.to_tokens(tokens),
Self::Inherited => (),
}
}
}
impl Visibility { impl Visibility {
/// Returns `true` if the visibility of the ink! message of constructor is public (`pub`). /// Returns `true` if the visibility of the ink! message of constructor is public (`pub`).
/// ///
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment