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

Prefer optional over convertible parameters #3368

Merged
merged 8 commits into from
Dec 9, 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
1 change: 1 addition & 0 deletions crates/libs/bindgen/src/type_name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ impl TypeName {
pub const HRESULT: Self = Self("Windows.Win32.Foundation", "HRESULT");
pub const CHAR: Self = Self("Windows.Win32.Foundation", "CHAR");
pub const BOOL: Self = Self("Windows.Win32.Foundation", "BOOL");
pub const BOOLEAN: Self = Self("Windows.Win32.Foundation", "BOOLEAN");
pub const NTSTATUS: Self = Self("Windows.Win32.Foundation", "NTSTATUS");
pub const WIN32_ERROR: Self = Self("Windows.Win32.Foundation", "WIN32_ERROR");
pub const RPC_STATUS: Self = Self("Windows.Win32.System.Rpc", "RPC_STATUS");
Expand Down
20 changes: 7 additions & 13 deletions crates/libs/bindgen/src/types/cpp_method.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub enum ParamHint {
ArrayRelativeByteLen(usize),
ArrayRelativePtr(usize),
IntoParam,
OptionalPointer,
Optional,
ValueType,
Blittable,
}
Expand Down Expand Up @@ -159,13 +159,13 @@ impl CppMethod {
*hint = ParamHint::IntoParam;
} else {
let flags = signature.params[position].1.flags();
if signature.params[position].0.is_pointer()
if signature.params[position].0.is_copyable()
&& (flags.contains(ParamAttributes::Optional)
|| signature.params[position]
.1
.has_attribute("ReservedAttribute"))
{
*hint = ParamHint::OptionalPointer;
*hint = ParamHint::Optional;
} else if signature.params[position].0.is_primitive()
&& (!signature.params[position].0.is_pointer()
|| signature.params[position].0.deref().is_copyable())
Expand Down Expand Up @@ -622,7 +622,7 @@ impl CppMethod {
let kind: TokenStream = format!("P{position}").into();
tokens.combine(&quote! { #name: #kind, });
}
ParamHint::OptionalPointer => {
ParamHint::Optional => {
let kind = ty.write_default(writer);
tokens.combine(&quote! { #name: Option<#kind>, });
}
Expand Down Expand Up @@ -688,12 +688,8 @@ impl CppMethod {
ParamHint::IntoParam => {
quote! { #name.param().abi(), }
}
ParamHint::OptionalPointer => {
if flags.contains(ParamAttributes::Out) {
quote! { core::mem::transmute(#name.unwrap_or(core::ptr::null_mut())), }
} else {
quote! { core::mem::transmute(#name.unwrap_or(core::ptr::null())), }
}
ParamHint::Optional => {
quote! { core::mem::transmute(#name.unwrap_or(core::mem::zeroed())), }
}
ParamHint::ValueType => {
quote! { core::mem::transmute(#name), }
Expand Down Expand Up @@ -784,9 +780,7 @@ fn write_invoke_arg(ty: &Type, param: Param, _hint: ParamHint) -> TokenStream {
}

fn is_convertible(ty: &Type, param: Param, hint: ParamHint) -> bool {
!param.flags().contains(ParamAttributes::Out)
&& !hint.is_array()
&& (ty.is_convertible() || ty.is_handle())
!param.flags().contains(ParamAttributes::Out) && !hint.is_array() && ty.is_convertible()
}

fn is_retval(signature: &Signature, param_hints: &[ParamHint]) -> bool {
Expand Down
2 changes: 1 addition & 1 deletion crates/libs/bindgen/src/types/cpp_struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ impl CppStruct {
}

pub fn is_convertible(&self) -> bool {
self.is_handle()
matches!(self.def.type_name(), TypeName::BOOL | TypeName::BOOLEAN)
}

pub fn is_copyable(&self) -> bool {
Expand Down
8 changes: 0 additions & 8 deletions crates/libs/bindgen/src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -795,14 +795,6 @@ impl Type {
}
}

pub fn is_handle(&self) -> bool {
if let Self::CppStruct(ty) = self {
ty.is_handle()
} else {
false
}
}

pub fn is_void(&self) -> bool {
match self {
Type::PtrConst(ty, _) | Type::PtrMut(ty, _) => ty.is_void(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
#[inline]
pub unsafe fn VhfAsyncOperationComplete<P1>(vhfoperationhandle: *const core::ffi::c_void, completionstatus: P1) -> super::super::super::Win32::Foundation::NTSTATUS
where
P1: windows_core::Param<super::super::super::Win32::Foundation::NTSTATUS>,
{
pub unsafe fn VhfAsyncOperationComplete(vhfoperationhandle: *const core::ffi::c_void, completionstatus: super::super::super::Win32::Foundation::NTSTATUS) -> super::super::super::Win32::Foundation::NTSTATUS {
windows_targets::link!("vhfum.dll" "system" fn VhfAsyncOperationComplete(vhfoperationhandle : *const core::ffi::c_void, completionstatus : super::super::super::Win32::Foundation:: NTSTATUS) -> super::super::super::Win32::Foundation:: NTSTATUS);
VhfAsyncOperationComplete(core::mem::transmute(vhfoperationhandle), completionstatus.param().abi())
VhfAsyncOperationComplete(core::mem::transmute(vhfoperationhandle), core::mem::transmute(completionstatus))
}
#[inline]
pub unsafe fn VhfCreate(vhfconfig: *const VHF_CONFIG, vhfhandle: *mut *mut core::ffi::c_void) -> super::super::super::Win32::Foundation::NTSTATUS {
Expand Down
14 changes: 4 additions & 10 deletions crates/libs/windows/src/Windows/Wdk/Foundation/mod.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
#[inline]
pub unsafe fn NtClose<P0>(handle: P0) -> super::super::Win32::Foundation::NTSTATUS
where
P0: windows_core::Param<super::super::Win32::Foundation::HANDLE>,
{
pub unsafe fn NtClose(handle: super::super::Win32::Foundation::HANDLE) -> super::super::Win32::Foundation::NTSTATUS {
windows_targets::link!("ntdll.dll" "system" fn NtClose(handle : super::super::Win32::Foundation:: HANDLE) -> super::super::Win32::Foundation:: NTSTATUS);
NtClose(handle.param().abi())
NtClose(core::mem::transmute(handle))
}
#[inline]
pub unsafe fn NtQueryObject<P0>(handle: P0, objectinformationclass: OBJECT_INFORMATION_CLASS, objectinformation: Option<*mut core::ffi::c_void>, objectinformationlength: u32, returnlength: Option<*mut u32>) -> super::super::Win32::Foundation::NTSTATUS
where
P0: windows_core::Param<super::super::Win32::Foundation::HANDLE>,
{
pub unsafe fn NtQueryObject(handle: Option<super::super::Win32::Foundation::HANDLE>, objectinformationclass: OBJECT_INFORMATION_CLASS, objectinformation: Option<*mut core::ffi::c_void>, objectinformationlength: u32, returnlength: Option<*mut u32>) -> super::super::Win32::Foundation::NTSTATUS {
windows_targets::link!("ntdll.dll" "system" fn NtQueryObject(handle : super::super::Win32::Foundation:: HANDLE, objectinformationclass : OBJECT_INFORMATION_CLASS, objectinformation : *mut core::ffi::c_void, objectinformationlength : u32, returnlength : *mut u32) -> super::super::Win32::Foundation:: NTSTATUS);
NtQueryObject(handle.param().abi(), core::mem::transmute(objectinformationclass), core::mem::transmute(objectinformation.unwrap_or(core::ptr::null_mut())), core::mem::transmute(objectinformationlength), core::mem::transmute(returnlength.unwrap_or(core::ptr::null_mut())))
NtQueryObject(core::mem::transmute(handle.unwrap_or(core::mem::zeroed())), core::mem::transmute(objectinformationclass), core::mem::transmute(objectinformation.unwrap_or(core::mem::zeroed())), core::mem::transmute(objectinformationlength), core::mem::transmute(returnlength.unwrap_or(core::mem::zeroed())))
}
#[repr(C)]
#[cfg(all(feature = "Wdk_System_SystemServices", feature = "Win32_Security"))]
Expand Down
21 changes: 6 additions & 15 deletions crates/libs/windows/src/Windows/Wdk/Graphics/Direct3D/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -332,12 +332,9 @@ pub unsafe fn D3DKMTGetProcessDeviceRemovalSupport(param0: *mut D3DKMT_GETPROCES
D3DKMTGetProcessDeviceRemovalSupport(core::mem::transmute(param0))
}
#[inline]
pub unsafe fn D3DKMTGetProcessSchedulingPriorityClass<P0>(param0: P0, param1: *mut D3DKMT_SCHEDULINGPRIORITYCLASS) -> super::super::super::Win32::Foundation::NTSTATUS
where
P0: windows_core::Param<super::super::super::Win32::Foundation::HANDLE>,
{
pub unsafe fn D3DKMTGetProcessSchedulingPriorityClass(param0: super::super::super::Win32::Foundation::HANDLE, param1: *mut D3DKMT_SCHEDULINGPRIORITYCLASS) -> super::super::super::Win32::Foundation::NTSTATUS {
windows_targets::link!("gdi32.dll" "system" fn D3DKMTGetProcessSchedulingPriorityClass(param0 : super::super::super::Win32::Foundation:: HANDLE, param1 : *mut D3DKMT_SCHEDULINGPRIORITYCLASS) -> super::super::super::Win32::Foundation:: NTSTATUS);
D3DKMTGetProcessSchedulingPriorityClass(param0.param().abi(), core::mem::transmute(param1))
D3DKMTGetProcessSchedulingPriorityClass(core::mem::transmute(param0), core::mem::transmute(param1))
}
#[inline]
pub unsafe fn D3DKMTGetResourcePresentPrivateDriverData(param0: *mut D3DDDI_GETRESOURCEPRESENTPRIVATEDRIVERDATA) -> super::super::super::Win32::Foundation::NTSTATUS {
Expand Down Expand Up @@ -643,12 +640,9 @@ pub unsafe fn D3DKMTReleaseKeyedMutex2(param0: *mut D3DKMT_RELEASEKEYEDMUTEX2) -
D3DKMTReleaseKeyedMutex2(core::mem::transmute(param0))
}
#[inline]
pub unsafe fn D3DKMTReleaseProcessVidPnSourceOwners<P0>(param0: P0) -> super::super::super::Win32::Foundation::NTSTATUS
where
P0: windows_core::Param<super::super::super::Win32::Foundation::HANDLE>,
{
pub unsafe fn D3DKMTReleaseProcessVidPnSourceOwners(param0: super::super::super::Win32::Foundation::HANDLE) -> super::super::super::Win32::Foundation::NTSTATUS {
windows_targets::link!("gdi32.dll" "system" fn D3DKMTReleaseProcessVidPnSourceOwners(param0 : super::super::super::Win32::Foundation:: HANDLE) -> super::super::super::Win32::Foundation:: NTSTATUS);
D3DKMTReleaseProcessVidPnSourceOwners(param0.param().abi())
D3DKMTReleaseProcessVidPnSourceOwners(core::mem::transmute(param0))
}
#[inline]
pub unsafe fn D3DKMTRender(param0: *mut D3DKMT_RENDER) -> super::super::super::Win32::Foundation::NTSTATUS {
Expand Down Expand Up @@ -706,12 +700,9 @@ pub unsafe fn D3DKMTSetMonitorColorSpaceTransform(param0: *const D3DKMT_SET_COLO
D3DKMTSetMonitorColorSpaceTransform(core::mem::transmute(param0))
}
#[inline]
pub unsafe fn D3DKMTSetProcessSchedulingPriorityClass<P0>(param0: P0, param1: D3DKMT_SCHEDULINGPRIORITYCLASS) -> super::super::super::Win32::Foundation::NTSTATUS
where
P0: windows_core::Param<super::super::super::Win32::Foundation::HANDLE>,
{
pub unsafe fn D3DKMTSetProcessSchedulingPriorityClass(param0: super::super::super::Win32::Foundation::HANDLE, param1: D3DKMT_SCHEDULINGPRIORITYCLASS) -> super::super::super::Win32::Foundation::NTSTATUS {
windows_targets::link!("gdi32.dll" "system" fn D3DKMTSetProcessSchedulingPriorityClass(param0 : super::super::super::Win32::Foundation:: HANDLE, param1 : D3DKMT_SCHEDULINGPRIORITYCLASS) -> super::super::super::Win32::Foundation:: NTSTATUS);
D3DKMTSetProcessSchedulingPriorityClass(param0.param().abi(), core::mem::transmute(param1))
D3DKMTSetProcessSchedulingPriorityClass(core::mem::transmute(param0), core::mem::transmute(param1))
}
#[inline]
pub unsafe fn D3DKMTSetQueuedLimit(param0: *const D3DKMT_SETQUEUEDLIMIT) -> super::super::super::Win32::Foundation::NTSTATUS {
Expand Down
Loading
Loading