Skip to content

Commit 1332b65

Browse files
authored
Simpler code gen for Boolean parameters (#3373)
1 parent 65acfb7 commit 1332b65

File tree

226 files changed

+3682
-9447
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

226 files changed

+3682
-9447
lines changed

crates/libs/bindgen/src/types/cpp_method.rs

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ pub enum ParamHint {
3131
Optional,
3232
ValueType,
3333
Blittable,
34+
Bool,
3435
}
3536

3637
impl ParamHint {
@@ -150,29 +151,25 @@ impl CppMethod {
150151

151152
for (position, hint) in param_hints.iter_mut().enumerate() {
152153
if *hint == ParamHint::None {
153-
if is_convertible(
154-
&signature.params[position].0,
155-
signature.params[position].1,
156-
*hint,
157-
) {
154+
let ty = &signature.params[position].0;
155+
let param = signature.params[position].1;
156+
let flags = param.flags();
157+
158+
if is_convertible(ty, param, *hint) {
158159
*hint = ParamHint::IntoParam;
159-
} else {
160-
let flags = signature.params[position].1.flags();
161-
if signature.params[position].0.is_copyable()
162-
&& (flags.contains(ParamAttributes::Optional)
163-
|| signature.params[position]
164-
.1
165-
.has_attribute("ReservedAttribute"))
166-
{
167-
*hint = ParamHint::Optional;
168-
} else if signature.params[position].0.is_primitive()
169-
&& (!signature.params[position].0.is_pointer()
170-
|| signature.params[position].0.deref().is_copyable())
171-
{
172-
*hint = ParamHint::ValueType;
173-
} else if signature.params[position].0.is_copyable() {
174-
*hint = ParamHint::Blittable;
175-
}
160+
} else if ty.is_copyable()
161+
&& (flags.contains(ParamAttributes::Optional)
162+
|| param.has_attribute("ReservedAttribute"))
163+
{
164+
*hint = ParamHint::Optional;
165+
} else if !flags.contains(ParamAttributes::Out)
166+
&& matches!(ty.type_name(), TypeName::BOOL | TypeName::BOOLEAN)
167+
{
168+
*hint = ParamHint::Bool;
169+
} else if ty.is_primitive() && (!ty.is_pointer() || ty.deref().is_copyable()) {
170+
*hint = ParamHint::ValueType;
171+
} else if ty.is_copyable() {
172+
*hint = ParamHint::Blittable;
176173
}
177174
}
178175
}
@@ -612,6 +609,9 @@ impl CppMethod {
612609
let kind = ty.write_default(writer);
613610
tokens.combine(&quote! { #name: Option<#kind>, });
614611
}
612+
ParamHint::Bool => {
613+
tokens.combine(&quote! { #name: bool, });
614+
}
615615
ParamHint::ValueType | ParamHint::Blittable => {
616616
let kind = ty.write_default(writer);
617617
tokens.combine(&quote! { #name: #kind, });
@@ -677,6 +677,9 @@ impl CppMethod {
677677
ParamHint::Optional => {
678678
quote! { core::mem::transmute(#name.unwrap_or(core::mem::zeroed())), }
679679
}
680+
ParamHint::Bool => {
681+
quote! { #name.into(), }
682+
}
680683
ParamHint::ValueType => {
681684
quote! { core::mem::transmute(#name), }
682685
}

crates/libs/bindgen/src/types/cpp_struct.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -240,10 +240,6 @@ impl CppStruct {
240240
}
241241
}
242242

243-
pub fn is_convertible(&self) -> bool {
244-
matches!(self.def.type_name(), TypeName::BOOL | TypeName::BOOLEAN)
245-
}
246-
247243
pub fn is_copyable(&self) -> bool {
248244
if matches!(
249245
self.def.type_name(),

crates/libs/bindgen/src/types/mod.rs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -721,14 +721,18 @@ impl Type {
721721
}
722722

723723
pub fn is_convertible(&self) -> bool {
724-
match self {
725-
Self::CppStruct(ty) => ty.is_convertible(),
726-
Self::Delegate(..) | Self::Interface(..) | Self::Class(..) | Self::CppInterface(..) => {
727-
true
728-
}
729-
Self::PCSTR | Self::PCWSTR | Self::Object | Self::IUnknown | Self::Param(_) => true,
730-
_ => false,
731-
}
724+
matches!(
725+
self,
726+
Self::Delegate(..)
727+
| Self::Interface(..)
728+
| Self::Class(..)
729+
| Self::CppInterface(..)
730+
| Self::PCSTR
731+
| Self::PCWSTR
732+
| Self::Object
733+
| Self::IUnknown
734+
| Self::Param(_)
735+
)
732736
}
733737

734738
pub fn is_const_ref(&self) -> bool {
@@ -950,7 +954,7 @@ impl Type {
950954
Self::String => TypeName("", "String"),
951955
Self::Object => TypeName("", "Object"),
952956

953-
rest => panic!("{rest:?}"),
957+
_ => TypeName("", ""),
954958
}
955959
}
956960

crates/libs/bindgen/src/writer/cpp_handle.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,6 @@ impl Writer {
104104
#[repr(transparent)]
105105
#[derive(#derive)]
106106
pub struct #name(pub #ty_name);
107-
impl windows_core::TypeKind for #name { // TODO: get rid of TypeKind on Win32 types
108-
type TypeKind = windows_core::CopyType;
109-
}
110107
#is_invalid
111108
#free
112109
#default

crates/libs/windows/src/Windows/Wdk/Devices/HumanInterfaceDevice/mod.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,9 @@ pub unsafe fn VhfCreate(vhfconfig: *const VHF_CONFIG, vhfhandle: *mut *mut core:
99
VhfCreate(core::mem::transmute(vhfconfig), core::mem::transmute(vhfhandle))
1010
}
1111
#[inline]
12-
pub unsafe fn VhfDelete<P1>(vhfhandle: *const core::ffi::c_void, wait: P1)
13-
where
14-
P1: windows_core::Param<super::super::super::Win32::Foundation::BOOLEAN>,
15-
{
12+
pub unsafe fn VhfDelete(vhfhandle: *const core::ffi::c_void, wait: bool) {
1613
windows_targets::link!("vhfum.dll" "system" fn VhfDelete(vhfhandle : *const core::ffi::c_void, wait : super::super::super::Win32::Foundation:: BOOLEAN));
17-
VhfDelete(core::mem::transmute(vhfhandle), wait.param().abi())
14+
VhfDelete(core::mem::transmute(vhfhandle), wait.into())
1815
}
1916
#[inline]
2017
pub unsafe fn VhfReadReportSubmit(vhfhandle: *const core::ffi::c_void, hidtransferpacket: *const HID_XFER_PACKET) -> super::super::super::Win32::Foundation::NTSTATUS {

0 commit comments

Comments
 (0)