Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor simplifications and code dupe reductions in codegen #938

Merged
merged 3 commits into from
Sep 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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