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

Simpler code gen for Boolean parameters #3373

Merged
merged 8 commits into from
Dec 10, 2024
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
47 changes: 25 additions & 22 deletions crates/libs/bindgen/src/types/cpp_method.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ pub enum ParamHint {
Optional,
ValueType,
Blittable,
Bool,
}

impl ParamHint {
Expand Down Expand Up @@ -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;
}
}
}
Expand Down Expand Up @@ -612,6 +609,9 @@ impl CppMethod {
let kind = ty.write_default(writer);
tokens.combine(&quote! { #name: Option<#kind>, });
}
ParamHint::Bool => {
tokens.combine(&quote! { #name: bool, });
}
ParamHint::ValueType | ParamHint::Blittable => {
let kind = ty.write_default(writer);
tokens.combine(&quote! { #name: #kind, });
Expand Down Expand Up @@ -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), }
}
Expand Down
4 changes: 0 additions & 4 deletions crates/libs/bindgen/src/types/cpp_struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
22 changes: 13 additions & 9 deletions crates/libs/bindgen/src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -721,14 +721,18 @@ 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
}
Self::PCSTR | Self::PCWSTR | Self::Object | Self::IUnknown | Self::Param(_) => true,
_ => false,
}
matches!(
self,
Self::Delegate(..)
| Self::Interface(..)
| Self::Class(..)
| Self::CppInterface(..)
| Self::PCSTR
| Self::PCWSTR
| Self::Object
| Self::IUnknown
| Self::Param(_)
)
}

pub fn is_const_ref(&self) -> bool {
Expand Down Expand Up @@ -950,7 +954,7 @@ impl Type {
Self::String => TypeName("", "String"),
Self::Object => TypeName("", "Object"),

rest => panic!("{rest:?}"),
_ => TypeName("", ""),
}
}

Expand Down
3 changes: 0 additions & 3 deletions crates/libs/bindgen/src/writer/cpp_handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,9 @@ pub unsafe fn VhfCreate(vhfconfig: *const VHF_CONFIG, vhfhandle: *mut *mut core:
VhfCreate(core::mem::transmute(vhfconfig), core::mem::transmute(vhfhandle))
}
#[inline]
pub unsafe fn VhfDelete<P1>(vhfhandle: *const core::ffi::c_void, wait: P1)
where
P1: windows_core::Param<super::super::super::Win32::Foundation::BOOLEAN>,
{
pub unsafe fn VhfDelete(vhfhandle: *const core::ffi::c_void, wait: bool) {
windows_targets::link!("vhfum.dll" "system" fn VhfDelete(vhfhandle : *const core::ffi::c_void, wait : super::super::super::Win32::Foundation:: BOOLEAN));
VhfDelete(core::mem::transmute(vhfhandle), wait.param().abi())
VhfDelete(core::mem::transmute(vhfhandle), wait.into())
}
#[inline]
pub unsafe fn VhfReadReportSubmit(vhfhandle: *const core::ffi::c_void, hidtransferpacket: *const HID_XFER_PACKET) -> super::super::super::Win32::Foundation::NTSTATUS {
Expand Down
Loading
Loading