From 879b7afdb2498e1790deed6eda50a0bfb704a185 Mon Sep 17 00:00:00 2001 From: Kenny Kerr Date: Tue, 10 Dec 2024 10:20:49 -0600 Subject: [PATCH] bindgen --- crates/libs/bindgen/src/types/cpp_method.rs | 47 +++++++++++--------- crates/libs/bindgen/src/types/cpp_struct.rs | 4 -- crates/libs/bindgen/src/types/mod.rs | 3 +- crates/libs/bindgen/src/writer/cpp_handle.rs | 3 -- 4 files changed, 26 insertions(+), 31 deletions(-) diff --git a/crates/libs/bindgen/src/types/cpp_method.rs b/crates/libs/bindgen/src/types/cpp_method.rs index eadda6ff33..b7b1065e12 100644 --- a/crates/libs/bindgen/src/types/cpp_method.rs +++ b/crates/libs/bindgen/src/types/cpp_method.rs @@ -31,6 +31,7 @@ pub enum ParamHint { Optional, ValueType, Blittable, + Bool, } impl ParamHint { @@ -150,29 +151,25 @@ impl CppMethod { for (position, hint) in param_hints.iter_mut().enumerate() { if *hint == ParamHint::None { - if is_convertible( - &signature.params[position].0, - signature.params[position].1, - *hint, - ) { + let ty = &signature.params[position].0; + let param = signature.params[position].1; + let flags = param.flags(); + + if is_convertible(ty, param, *hint) { *hint = ParamHint::IntoParam; - } else { - let flags = signature.params[position].1.flags(); - if signature.params[position].0.is_copyable() - && (flags.contains(ParamAttributes::Optional) - || signature.params[position] - .1 - .has_attribute("ReservedAttribute")) - { - *hint = ParamHint::Optional; - } else if signature.params[position].0.is_primitive() - && (!signature.params[position].0.is_pointer() - || signature.params[position].0.deref().is_copyable()) - { - *hint = ParamHint::ValueType; - } else if signature.params[position].0.is_copyable() { - *hint = ParamHint::Blittable; - } + } else if ty.is_copyable() + && (flags.contains(ParamAttributes::Optional) + || param.has_attribute("ReservedAttribute")) + { + *hint = ParamHint::Optional; + } else if !flags.contains(ParamAttributes::Out) + && matches!(ty.type_name(), TypeName::BOOL | TypeName::BOOLEAN) + { + *hint = ParamHint::Bool; + } else if ty.is_primitive() && (!ty.is_pointer() || ty.deref().is_copyable()) { + *hint = ParamHint::ValueType; + } else if ty.is_copyable() { + *hint = ParamHint::Blittable; } } } @@ -612,6 +609,9 @@ impl CppMethod { let kind = ty.write_default(writer); tokens.combine("e! { #name: Option<#kind>, }); } + ParamHint::Bool => { + tokens.combine("e! { #name: bool, }); + } ParamHint::ValueType | ParamHint::Blittable => { let kind = ty.write_default(writer); tokens.combine("e! { #name: #kind, }); @@ -677,6 +677,9 @@ impl CppMethod { ParamHint::Optional => { quote! { core::mem::transmute(#name.unwrap_or(core::mem::zeroed())), } } + ParamHint::Bool => { + quote! { #name.into(), } + } ParamHint::ValueType => { quote! { core::mem::transmute(#name), } } diff --git a/crates/libs/bindgen/src/types/cpp_struct.rs b/crates/libs/bindgen/src/types/cpp_struct.rs index 77e7679ace..291069b085 100644 --- a/crates/libs/bindgen/src/types/cpp_struct.rs +++ b/crates/libs/bindgen/src/types/cpp_struct.rs @@ -240,10 +240,6 @@ impl CppStruct { } } - pub fn is_convertible(&self) -> bool { - matches!(self.def.type_name(), TypeName::BOOL | TypeName::BOOLEAN) - } - pub fn is_copyable(&self) -> bool { if matches!( self.def.type_name(), diff --git a/crates/libs/bindgen/src/types/mod.rs b/crates/libs/bindgen/src/types/mod.rs index 433a6476e6..9f3657de36 100644 --- a/crates/libs/bindgen/src/types/mod.rs +++ b/crates/libs/bindgen/src/types/mod.rs @@ -722,7 +722,6 @@ impl Type { pub fn is_convertible(&self) -> bool { match self { - Self::CppStruct(ty) => ty.is_convertible(), Self::Delegate(..) | Self::Interface(..) | Self::Class(..) | Self::CppInterface(..) => { true } @@ -950,7 +949,7 @@ impl Type { Self::String => TypeName("", "String"), Self::Object => TypeName("", "Object"), - rest => panic!("{rest:?}"), + _ => TypeName("", ""), } } diff --git a/crates/libs/bindgen/src/writer/cpp_handle.rs b/crates/libs/bindgen/src/writer/cpp_handle.rs index cd1215201c..f7703f2b27 100644 --- a/crates/libs/bindgen/src/writer/cpp_handle.rs +++ b/crates/libs/bindgen/src/writer/cpp_handle.rs @@ -104,9 +104,6 @@ impl Writer { #[repr(transparent)] #[derive(#derive)] pub struct #name(pub #ty_name); - impl windows_core::TypeKind for #name { // TODO: get rid of TypeKind on Win32 types - type TypeKind = windows_core::CopyType; - } #is_invalid #free #default