From 1dabc3ab2df8bc6dbc639fa8bfd5d562324bdf3c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matheus=20Catarino=20Fran=C3=A7a?= Date: Thu, 7 Apr 2022 10:32:20 -0300 Subject: [PATCH] typedef fixed --- src/bindgen/cdecl.rs | 6 +++++- src/bindgen/config.rs | 2 +- src/bindgen/ir/constant.rs | 2 +- src/bindgen/ir/field.rs | 14 ++++++++++++-- src/bindgen/ir/typedef.rs | 2 +- 5 files changed, 20 insertions(+), 6 deletions(-) diff --git a/src/bindgen/cdecl.rs b/src/bindgen/cdecl.rs index aefea19fe..e8d9be878 100644 --- a/src/bindgen/cdecl.rs +++ b/src/bindgen/cdecl.rs @@ -267,7 +267,11 @@ impl CDecl { // Write the identifier if let Some(ident) = ident { if config.language == Language::Zig && self.declarators.is_empty() { - write!(out, "{}: {}", ident, self.type_name); + if ident.is_empty() { + write!(out, "{}", self.type_name); + } else { + write!(out, "{}: {}", ident, self.type_name); + } } else { write!(out, "{}", ident); } diff --git a/src/bindgen/config.rs b/src/bindgen/config.rs index 3ef194b75..cb9edeefa 100644 --- a/src/bindgen/config.rs +++ b/src/bindgen/config.rs @@ -250,7 +250,7 @@ impl Style { pub fn zig_def(self) -> &'static str { if self.generate_tag() { - "pub const" + "pub const " } else { "pub extern" } diff --git a/src/bindgen/ir/constant.rs b/src/bindgen/ir/constant.rs index 165bedae5..113f552a4 100644 --- a/src/bindgen/ir/constant.rs +++ b/src/bindgen/ir/constant.rs @@ -613,7 +613,7 @@ impl Constant { Language::Zig => { out.write(config.style.zig_def()); self.ty.write(config, out); - write!(out, " {} = ", name); + write!(out, "{} = ", name); value.write(config, out); write!(out, ";"); } diff --git a/src/bindgen/ir/field.rs b/src/bindgen/ir/field.rs index 6e132bfaf..d7a8826f0 100644 --- a/src/bindgen/ir/field.rs +++ b/src/bindgen/ir/field.rs @@ -28,6 +28,16 @@ impl Field { } } + pub fn from_type(ty: Type) -> Field { + Field { + name: "".to_string(), + ty, + cfg: None, + annotations: AnnotationSet::new(), + documentation: Documentation::none(), + } + } + pub fn load(field: &syn::Field, self_path: &Path) -> Result, String> { Ok(if let Some(mut ty) = Type::load(&field.ty)? { ty.replace_self_with(self_path); @@ -61,13 +71,13 @@ impl Source for Field { cdecl::write_field(out, &self.ty, &self.name, config); // Cython extern declarations don't manage layouts, layouts are defined entierly by the // corresponding C code. So we can omit bitfield sizes which are not supported by Cython. - if config.language != Language::Cython { + if config.language != Language::Cython || config.language != Language::Zig { if let Some(bitfield) = self.annotations.atom("bitfield") { write!(out, ": {}", bitfield.unwrap_or_default()); } } - if config.language != Language::Cython { + if config.language != Language::Cython || config.language != Language::Zig { condition.write_after(config, out); // FIXME(#634): `write_vertical_source_list` should support // configuring list elements natively. For now we print a newline diff --git a/src/bindgen/ir/typedef.rs b/src/bindgen/ir/typedef.rs index f53f8efa4..9579121e0 100644 --- a/src/bindgen/ir/typedef.rs +++ b/src/bindgen/ir/typedef.rs @@ -218,7 +218,7 @@ impl Source for Typedef { } Language::Zig => { write!(out, "{}{} = ", config.style.zig_def(), self.export_name()); - self.aliased.write(config, out); + Field::from_type(self.aliased.clone()).write(config, out); } }