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<'_> {
fn generate_trait_constructor(constructor: &ir::Constructor) -> TokenStream2 {
let span = constructor.span();
let attrs = constructor.attrs();
let vis = match constructor.visibility() {
ir::Visibility::Inherited => None,
ir::Visibility::Public(vis_public) => Some(vis_public),
};
let vis = constructor.visibility();
let ident = constructor.ident();
let output_ident = format_ident!("{}Out", ident.to_string().to_camel_case());
let inputs = constructor.inputs();
......@@ -83,14 +80,8 @@ impl ItemImpls<'_> {
fn generate_trait_message(message: &ir::Message) -> TokenStream2 {
let span = message.span();
let attrs = message.attrs();
let vis = match message.visibility() {
ir::Visibility::Inherited => None,
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 vis = message.visibility();
let receiver = message.receiver();
let ident = message.ident();
let output_ident = format_ident!("{}Out", ident.to_string().to_camel_case());
let inputs = message.inputs();
......@@ -103,7 +94,7 @@ impl ItemImpls<'_> {
type #output_ident = #output;
#( #attrs )*
#vis fn #ident(#receiver #(, #inputs )* ) -> Self::#output_ident {
#vis fn #ident(#receiver #( , #inputs )* ) -> Self::#output_ident {
#( #statements )*
}
)
......@@ -164,10 +155,7 @@ impl ItemImpls<'_> {
fn generate_inherent_constructor(constructor: &ir::Constructor) -> TokenStream2 {
let span = constructor.span();
let attrs = constructor.attrs();
let vis = match constructor.visibility() {
ir::Visibility::Inherited => None,
ir::Visibility::Public(vis_public) => Some(vis_public),
};
let vis = constructor.visibility();
let ident = constructor.ident();
let inputs = constructor.inputs();
let statements = constructor.statements();
......@@ -183,14 +171,8 @@ impl ItemImpls<'_> {
fn generate_inherent_message(message: &ir::Message) -> TokenStream2 {
let span = message.span();
let attrs = message.attrs();
let vis = match message.visibility() {
ir::Visibility::Inherited => None,
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 vis = message.visibility();
let receiver = message.receiver();
let ident = message.ident();
let inputs = message.inputs();
let output_arrow = message.output().map(|_| quote! { -> });
......@@ -198,7 +180,7 @@ impl ItemImpls<'_> {
let statements = message.statements();
quote_spanned!(span =>
#( #attrs )*
#vis fn #ident(#receiver, #( #inputs ),* ) #output_arrow #output {
#vis fn #ident(#receiver #( , #inputs )* ) #output_arrow #output {
#( #statements )*
}
)
......
......@@ -440,6 +440,15 @@ pub enum Visibility {
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 {
/// Returns `true` if the visibility of the ink! message of constructor is public (`pub`).
///
......
Markdown is supported
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