use super::*;
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct CppFn {
pub namespace: &'static str,
pub method: MethodDef,
}
impl Ord for CppFn {
fn cmp(&self, other: &Self) -> Ordering {
(self.method.name(), self.method).cmp(&(other.method.name(), other.method))
}
}
impl PartialOrd for CppFn {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl CppFn {
pub fn type_name(&self) -> TypeName {
TypeName(self.namespace, self.method.name())
}
pub fn write_name(&self, config: &Config) -> TokenStream {
self.type_name().write(config, &[])
}
fn write_extern_signature(&self, config: &Config<'_>, underlying_types: bool) -> TokenStream {
let signature = self.method.method_signature(&[], config.reader);
let params = signature.params.iter().map(|param| {
let name = param.write_ident();
let ty = if underlying_types {
param.underlying_type(config.reader).write_abi(config)
} else {
param.write_abi(config)
};
quote! { #name: #ty }
});
let return_sig = config.write_return_sig(self.method, &signature, underlying_types);
let vararg = if signature.call_flags.contains(MethodCallAttributes::VARARG) {
if signature.params.is_empty() {
quote! { ... }
} else {
quote! { , ... }
}
} else {
quote! {}
};
quote! {
(#(#params),* #vararg) #return_sig
}
}
fn abi(&self, config: &Config<'_>) -> &'static str {
self.variadic_abi(config)
.unwrap_or_else(|| self.method.calling_convention())
}
fn variadic_abi(&self, config: &Config<'_>) -> Option<&'static str> {
if !self
.method
.method_signature(&[], config.reader)
.call_flags
.contains(MethodCallAttributes::VARARG)
{
return None;
}
let flags = self.method.impl_map()?.flags();
if flags.contains(windows_metadata::PInvokeAttributes::CallConvFastcall) {
None
} else if flags.contains(windows_metadata::PInvokeAttributes::CallConvCdecl) {
Some("C")
} else if flags.contains(windows_metadata::PInvokeAttributes::CallConvPlatformapi) {
Some("system")
} else {
None
}
}
fn reject_variadic(&self, config: &Config<'_>, reason: &str) -> TokenStream {
assert!(
!config
.plan
.selection
.names_type(self.namespace, self.method.name()),
"windows-bindgen: selected variadic function `{}.{}` {reason}",
self.namespace,
self.method.name()
);
quote! {}
}
pub fn write_fn_ptr(&self, config: &Config<'_>, underlying_types: bool) -> TokenStream {
let ptr_name = self.method.name().to_string();
let name = to_ident(&ptr_name);
let abi = self.abi(config);
let signature = self.write_extern_signature(config, underlying_types);
quote! {
pub type #name = unsafe extern #abi fn #signature;
}
}
pub fn write_link(&self, config: &Config, underlying_types: bool) -> TokenStream {
let library = if config.reader.legacy() {
legacy::module_name(self.method)
} else {
self.method.module_name()
};
let symbol = self.method.import_name();
let ordinal = windows_metadata::import_ordinal(symbol.unwrap_or(self.method.name()))
.unwrap_or_else(|error| {
panic!(
"windows-bindgen: function `{}.{}`: {error}",
self.namespace,
self.method.name()
)
});
let name = to_ident(self.method.name());
let abi = self.abi(config);
let signature = self.write_extern_signature(config, underlying_types);
let link = to_ident(config.link);
if config.bindgen.extern_fns {
quote! {
unsafe extern #abi {
pub fn #name #signature;
}
}
} else if let Some(ordinal) = ordinal {
let ordinal = Literal::u16_unsuffixed(ordinal);
quote! {
#link::link!(#library #abi ordinal(#ordinal) fn #name #signature);
}
} else {
quote! {
#link::link!(#library #abi #symbol fn #name #signature);
}
}
}
pub fn write_cfg(&self, config: &Config) -> TokenStream {
write_simple_cfg(self, config)
}
pub fn write(&self, config: &Config) -> TokenStream {
let name = to_ident(self.method.name());
let signature = self.method.method_signature(&[], config.reader);
let variadic = signature.call_flags.contains(MethodCallAttributes::VARARG);
if variadic && config.bindgen.style == Style::Full && !config.reader.legacy() {
return self.reject_variadic(
config,
"cannot be projected by full bindings; use default or `--sys` for its raw \
declaration",
);
}
if variadic && self.variadic_abi(config).is_none() {
return self.reject_variadic(
config,
"uses a calling convention that stable Rust cannot represent for C variadics",
);
}
let link = self.write_link(config, false);
let arches = write_arches(self.method);
let cfg = self.write_cfg(config);
let cfg = quote! { #arches #cfg };
let window_long = self.write_window_long(config.reader);
if config.bindgen.style.is_sys() || config.bindgen.style.is_minimal() {
let fn_ptr = if config.bindgen.extern_fns {
let fn_ptr = self.write_fn_ptr(config, false);
quote! {
#cfg
#fn_ptr
}
} else {
quote! {}
};
return quote! {
#fn_ptr
#cfg
#link
#window_long
};
}
let method = CppMethod::new(self.method, config.reader);
let args = method.write_args(config);
let params = method.write_params(config);
let generics = method.write_generics();
let abi_return_type = method.write_return(config);
let result = config.write_core();
let must_use = method.write_must_use(config.reader);
let wrapper = match method.return_hint {
ReturnHint::Query(..) => {
let where_clause = method.write_where(config, true);
quote! {
#cfg
#[inline]
pub unsafe fn #name<#generics T>(#params) -> #result Result<T> #where_clause {
#link
let mut result__ = core::ptr::null_mut();
unsafe { #name(#args).and_then(|| windows_core::imp::from_abi(result__)) }
}
}
}
ReturnHint::QueryOptional(..) => {
let where_clause = method.write_where(config, true);
quote! {
#cfg
#[inline]
pub unsafe fn #name<#generics T>(#params result__: *mut Option<T>) -> #result Result<()> #where_clause {
#link
unsafe { #name(#args).ok() }
}
}
}
ReturnHint::ResultValue => {
let where_clause = method.write_where(config, false);
let ty = signature
.params
.last()
.unwrap()
.projection_pointee(config.reader);
let map = ty
.projection_type(config.reader)
.write_result_map(config.reader);
let init = ty.projection_type(config.reader).write_result_init(config);
let return_type = ty.write_name(config);
quote! {
#cfg
#[inline]
pub unsafe fn #name<#generics>(#params) -> #result Result<#return_type> #where_clause {
#link
unsafe {
#init
#name(#args).#map
}
}
}
}
ReturnHint::ReturnValue => {
let where_clause = method.write_where(config, false);
let return_type = method.signature.params[method.signature.params.len() - 1]
.projection_pointee(config.reader);
let projection_type = return_type.projection_type(config.reader);
if projection_type.is_interface_with_reader(config.reader) {
let return_type = return_type.write_name(config);
quote! {
#cfg
#[inline]
pub unsafe fn #name<#generics>(#params) -> #result Result<#return_type> #where_clause {
#link
unsafe {
let mut result__ = core::mem::zeroed();
#name(#args);
windows_core::imp::from_abi(result__)
}
}
}
} else {
let map = if projection_type.is_copyable(config.reader) {
quote! { result__ }
} else {
quote! { core::mem::transmute(result__) }
};
let where_clause = method.write_where(config, false);
let return_type = return_type.write_name(config);
quote! {
#cfg
#[inline]
pub unsafe fn #name<#generics>(#params) -> #return_type #where_clause {
#link
unsafe {
let mut result__ = core::mem::zeroed();
#name(#args);
#map
}
}
}
}
}
ReturnHint::ResultVoid => {
let where_clause = method.write_where(config, false);
quote! {
#cfg
#[inline]
pub unsafe fn #name<#generics>(#params) -> #result Result<()> #where_clause {
#link
unsafe { #name(#args).ok() }
}
}
}
ReturnHint::ReturnStruct | ReturnHint::None
if config.reader.legacy()
&& legacy::handle_last_error(self.method, &signature, config.reader) =>
{
let where_clause = method.write_where(config, false);
let return_type = signature.return_type.write_name(config);
quote! {
#cfg
#[inline]
pub unsafe fn #name<#generics>(#params) -> #result Result<#return_type> #where_clause {
#link
let result__ = unsafe { #name(#args) };
(!result__.is_invalid()).then_some(result__).ok_or_else(windows_core::Error::from_thread)
}
}
}
ReturnHint::ReturnStruct | ReturnHint::None => {
let where_clause = method.write_where(config, false);
quote! {
#cfg
#[inline]
pub unsafe fn #name<#generics>(#params) #abi_return_type #where_clause {
#link
unsafe { #name(#args) }
}
}
}
};
quote! {
#must_use
#wrapper
#window_long
}
}
pub fn window_long_dependency(&self, reader: &Reader) -> Option<&'static str> {
let dependency = match self.method.name() {
"GetWindowLongPtrA" => "GetWindowLongA",
"GetWindowLongPtrW" => "GetWindowLongW",
"SetWindowLongPtrA" => "SetWindowLongA",
"SetWindowLongPtrW" => "SetWindowLongW",
_ => return None,
};
let mut declarations = reader
.with_full_name(self.namespace, self.method.name())
.filter_map(|ty| match ty {
Type::CppFn(function) => Some(function),
_ => None,
});
let first = declarations.next()?;
if first != *self
|| std::iter::once(first).chain(declarations).any(|function| {
let arches = function.method.arches();
arches == 0 || arches & 1 != 0
})
{
return None;
}
Some(dependency)
}
fn write_window_long(&self, reader: &Reader) -> TokenStream {
let Some(dependency) = self.window_long_dependency(reader) else {
return quote! {};
};
let dependency = to_ident(dependency);
let name = to_ident(self.method.name());
quote! {
#[cfg(target_pointer_width = "32")]
pub use #dependency as #name;
}
}
}
impl Dependencies for CppFn {
fn combine(&self, dependencies: &mut TypeMap, reader: &Reader) {
for ty in self.method.associated_enums(reader) {
ty.combine(dependencies, reader);
}
self.method
.method_signature(&[], reader)
.combine(dependencies, reader);
let dependency = self.window_long_dependency(reader);
if let Some(dependency) = dependency {
reader
.unwrap_full_name(self.namespace, dependency)
.combine(dependencies, reader);
}
}
}
impl Config<'_> {
pub fn write_return_sig(
&self,
method: MethodDef,
signature: &Signature,
underlying_types: bool,
) -> TokenStream {
match &signature.return_type {
Type::Void => {
if method.has_attribute("DoesNotReturnAttribute") {
quote! { -> ! }
} else {
quote! {}
}
}
ty => {
let ty = if underlying_types {
ty.underlying_type(self.reader).write_default(self)
} else {
ty.write_default(self)
};
quote! { -> #ty }
}
}
}
}