fix(macros/generate/convert/auxiliary/c): Correctly generate function types

This commit is contained in:
Benedikt Peetz 2024-03-27 10:54:01 +01:00
parent 055cf2d5ce
commit a66c687421
Signed by: bpeetz
GPG Key ID: A5E94010C3A642AD
6 changed files with 40 additions and 19 deletions

View File

@ -29,10 +29,14 @@ impl Function {
pub fn to_auxiliary_c(&self, namespaces: &[&Identifier]) -> String {
let doc_comments: String = Attribute::to_auxiliary_c_merged(&self.attributes);
let ident = self.identifier.to_c_with_path(namespaces);
let inputs: Vec<TokenStream2> = self.inputs.iter().map(NamedType::to_auxiliary_c).collect();
let inputs: Vec<TokenStream2> = self
.inputs
.iter()
.map(NamedType::to_auxiliary_c)
.collect();
let function_output = if let Some(out) = &self.output {
let type_name = &out.to_auxiliary_c(true);
let type_name = &out.to_auxiliary_c(true, None);
let comma = if !inputs.is_empty() {
quote! {
,
@ -66,7 +70,7 @@ impl Function {
let ident = self.identifier.to_rust();
let (output, output_comma) = if let Some(output) = &self.output {
let output = output.to_auxiliary_c(true);
let output = output.to_auxiliary_c(true, None);
(quote! { #output }, quote! {,})
} else {
(TokenStream2::default(), TokenStream2::default())
@ -79,7 +83,7 @@ impl Function {
.inputs
.iter()
.map(|named_type| &named_type.r#type)
.map(|r#type| r#type.to_auxiliary_c(false))
.map(|r#type| r#type.to_auxiliary_c(false, None))
.collect();
quote! {
#output_comma #(#inputs),*

View File

@ -25,7 +25,7 @@ use crate::parser::command_spec::{Attribute, DocNamedType, Structure};
impl Structure {
pub fn to_auxiliary_c(&self) -> String {
let doc_comments: String = Attribute::to_auxiliary_c_merged(&self.attributes);
let ident = self.identifier.to_rust();
let ident = self.identifier.to_auxiliary_c();
let contents = self
.contents
.iter()

View File

@ -20,13 +20,18 @@
* If not, see <https://www.gnu.org/licenses/>.
*/
use crate::parser::command_spec::{Attribute, DocNamedType};
use crate::parser::command_spec::{Attribute, DocNamedType, Type};
impl DocNamedType {
pub fn to_auxiliary_c(&self) -> String {
let doc_comments: String = Attribute::to_auxiliary_c_merged(&self.attributes);
let ident = &self.name.to_rust();
let r#type = self.r#type.to_auxiliary_c(false);
format!("{}{} {};", doc_comments, r#type, ident)
let r#type = self.r#type.to_auxiliary_c(false, Some(&self.name));
if let Type::Function { .. } = self.r#type {
format!("{}{};\n", doc_comments, r#type)
} else {
format!("{}{} {};\n", doc_comments, r#type, ident)
}
}
}

View File

@ -23,7 +23,7 @@
use proc_macro2::TokenStream as TokenStream2;
use quote::quote;
use crate::parser::command_spec::{NamedType, Type};
use crate::parser::command_spec::{Identifier, Type};
mod doc_named_type;
mod named_type;
@ -32,7 +32,7 @@ mod named_type;
// pub use named_type::*;
impl Type {
pub fn to_auxiliary_c(&self, is_output: bool) -> TokenStream2 {
pub fn to_auxiliary_c(&self, is_output: bool, field_name: Option<&Identifier>) -> TokenStream2 {
match self {
Type::Typical {
identifier,
@ -55,18 +55,23 @@ impl Type {
assert_eq!(is_output, false);
let output = if let Some(output) = output {
output.to_auxiliary_c(false)
output.to_auxiliary_c(false, None)
} else {
quote! {
void
}
};
let inputs: Vec<TokenStream2> =
inputs.iter().map(NamedType::to_auxiliary_c).collect();
let inputs: Vec<TokenStream2> = inputs
.iter()
.map(|input| &input.r#type)
.map(|r#type| r#type.to_auxiliary_c(false, field_name))
.collect();
let field_name = field_name.expect("This is some, when I'm a function type").to_rust();
quote! {
#output (*name) (#(#inputs),*)
#output (*#field_name) (#(#inputs),*)
}
}
}

View File

@ -23,14 +23,21 @@
use proc_macro2::TokenStream as TokenStream2;
use quote::quote;
use crate::parser::command_spec::NamedType;
use crate::parser::command_spec::{NamedType, Type};
impl NamedType {
pub fn to_auxiliary_c(&self) -> TokenStream2 {
let ident = self.name.to_rust();
let c_type = self.r#type.to_auxiliary_c(false);
let c_type = self.r#type.to_auxiliary_c(false, Some(&self.name));
if let Type::Function { .. } = self.r#type {
quote! {
#c_type
}
} else {
quote! {
#c_type #ident
}
}
}
}

View File

@ -162,7 +162,7 @@ struct Callback
/**
* Very important field
*/
const char *(*name) (const char *name)func;
const char *(*func) (const char *);
/**
* Very important field for keeping time constant
*/