Skip to content

Commit

Permalink
Minor simplifications and code dupe reductions in codegen (#938)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
Robbepop authored Sep 23, 2021
1 parent 0c7e9a3 commit beaf989
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 26 deletions.
34 changes: 8 additions & 26 deletions crates/lang/codegen/src/generator/item_impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();
Expand All @@ -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 )*
}
)
Expand Down Expand Up @@ -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();
Expand All @@ -183,22 +171,16 @@ 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! { -> });
let output = message.output();
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 )*
}
)
Expand Down
9 changes: 9 additions & 0 deletions crates/lang/ir/src/ir/item_impl/callable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`).
///
Expand Down

0 comments on commit beaf989

Please sign in to comment.