use super::*;
pub fn is_agile(def: TypeDef) -> bool {
def.attributes().any(|attribute| {
attribute.namespace() == "Windows.Win32.Foundation.Metadata"
&& attribute.name() == "AgileAttribute"
})
}
fn size(ty: &Type, reader: &Reader) -> usize {
match ty {
Type::I8 | Type::U8 => 1,
Type::I16 | Type::U16 => 2,
Type::I64 | Type::U64 | Type::F64 => 8,
Type::GUID => 16,
Type::ArrayFixed(ty, len) => size(ty, reader) * len,
Type::CppStruct(ty) => {
let fields = ty.def.fields().map(|field| sizing_type(field, ty, reader));
if ty.def.flags().contains(TypeAttributes::ExplicitLayout) {
fields.map(|ty| size(&ty, reader)).max().unwrap_or(1)
} else {
fields.fold(0, |sum, ty| {
let align = align(&ty, reader);
((sum + (align - 1)) & !(align - 1)) + size(&ty, reader)
})
}
}
Type::Struct(ty) => ty
.def
.fields()
.map(|field| field.field_type(None, reader))
.fold(0, |sum, ty| {
let align = align(&ty, reader);
((sum + (align - 1)) & !(align - 1)) + size(&ty, reader)
}),
Type::CppEnum(ty) => size(&ty.def.underlying_type_ext(reader), reader),
_ => 4,
}
}
fn align(ty: &Type, reader: &Reader) -> usize {
match ty {
Type::I8 | Type::U8 => 1,
Type::I16 | Type::U16 => 2,
Type::I64 | Type::U64 | Type::F64 => 8,
Type::ArrayFixed(ty, len) => align(ty, reader) * len,
Type::CppStruct(ty) => ty
.def
.fields()
.map(|field| align(&sizing_type(field, ty, reader), reader))
.max()
.unwrap_or(1),
Type::Struct(ty) => ty
.def
.fields()
.map(|field| align(&field.field_type(None, reader), reader))
.max()
.unwrap_or(1),
Type::CppEnum(ty) => align(&ty.def.underlying_type_ext(reader), reader),
_ => 4,
}
}
fn is_boolean(ty: &windows_metadata::Type) -> bool {
matches!(ty, windows_metadata::Type::ValueName(name)
if name.namespace == "Windows.Win32.Foundation" && name.name == "BOOLEAN")
}
fn sizing_type(field: Field, enclosing: &CppStruct, reader: &Reader) -> Type {
match field.ty() {
ty if is_boolean(&ty) => Type::Bool,
windows_metadata::Type::ArrayFixed(ty, len) if is_boolean(&ty) => {
Type::ArrayFixed(Box::new(Type::Bool), len)
}
_ => field.field_type(Some(enclosing), reader),
}
}
fn is_byte_size(ty: &Type) -> bool {
match ty {
Type::PtrConst(ty, _) | Type::PtrMut(ty, _) => is_byte_size(ty),
Type::I8 | Type::U8 | Type::PSTR | Type::PCSTR => true,
_ => false,
}
}
fn is_void(ty: &Type) -> bool {
match ty {
Type::PtrConst(ty, _) | Type::PtrMut(ty, _) => is_void(ty),
Type::Void => true,
_ => false,
}
}
fn is_array_attribute(param: &Param) -> bool {
param.attributes().any(|attribute| {
matches!(
attribute.name(),
"NativeArrayInfoAttribute" | "MemorySizeAttribute"
)
})
}
pub fn is_retval(signature: &Signature, reader: &Reader) -> bool {
let Some((last, rest)) = signature.params.split_last() else {
return false;
};
if last.is_retval_attribute() {
return true;
}
let Some(def) = last.def else {
return false;
};
let flags = def.flags();
last.ty.is_pointer()
&& !is_void(&last.ty)
&& !flags.contains(windows_metadata::ParamAttributes::In)
&& flags.contains(windows_metadata::ParamAttributes::Out)
&& !flags.contains(windows_metadata::ParamAttributes::Optional)
&& !is_array_attribute(last)
&& size(&last.ty.deref(), reader) <= 16
&& rest.iter().all(Param::is_input_only)
}
pub fn return_hint(
def: MethodDef,
signature: &Signature,
is_retval: bool,
reader: &Reader,
) -> ReturnHint {
if def.has_attribute("CanReturnMultipleSuccessValuesAttribute") {
return ReturnHint::None;
}
match signature.return_type.projection_type(reader) {
Type::Void if is_retval => ReturnHint::ReturnValue,
Type::HRESULT => {
if signature.params.len() >= 2
&& let Some((guid, object)) = signature_param_is_query(&signature.params, reader)
{
if signature.params[object].is_optional_or_reserved() {
ReturnHint::QueryOptional(object, guid)
} else {
ReturnHint::Query(object, guid)
}
} else if is_retval {
ReturnHint::ResultValue
} else {
ReturnHint::ResultVoid
}
}
Type::BOOL if supports_last_error(def) => ReturnHint::ResultVoid,
Type::GUID => ReturnHint::ReturnStruct,
Type::CppStruct(ty) if !ty.is_handle(reader) => ReturnHint::ReturnStruct,
_ => ReturnHint::None,
}
}
fn supports_last_error(def: MethodDef) -> bool {
def.impl_map().is_some_and(|map| {
map.flags()
.contains(windows_metadata::PInvokeAttributes::SupportsLastError)
})
}
pub fn handle_last_error(def: MethodDef, signature: &Signature, reader: &Reader) -> bool {
if def.name() == "LocalFree" || !supports_last_error(def) {
return false;
}
let Type::CppStruct(ty) = &signature.return_type else {
return false;
};
ty.is_handle(reader)
&& (ty.def.underlying_type_ext(reader).is_pointer()
|| ty.def.has_attribute("InvalidHandleValueAttribute"))
}
pub fn module_name(def: MethodDef) -> String {
if matches!(
def.name(),
"CoCreateFreeThreadedMarshaler"
| "CoIncrementMTAUsage"
| "CoTaskMemAlloc"
| "CoTaskMemFree"
| "RoGetAgileReference"
) {
"combase.dll".to_string()
} else {
def.module_name()
}
}
pub fn is_variant(name: TypeName) -> bool {
name == TypeName("Windows.Win32.System.Variant", "VARIANT")
|| name == TypeName("Windows.Win32.System.Com.StructuredStorage", "PROPVARIANT")
}
pub fn associated_enum(param: MethodParam, namespace: &str, reader: &Reader) -> Option<Type> {
let attribute = param
.attributes()
.find(|attribute| attribute.name() == "AssociatedEnumAttribute")?;
let values = attribute.value();
let [(_, Value::Utf8(name))] = values.as_slice() else {
panic!("AssociatedEnumAttribute requires one string argument");
};
let ty = reader.with_full_name(namespace, name).next();
assert!(
matches!(ty, Some(Type::CppEnum(_))),
"AssociatedEnumAttribute target `{namespace}.{name}` must be a Win32 enum"
);
ty
}
fn array_hint(param: &Param) -> ParamHint {
for attribute in param.attributes() {
match attribute.name() {
"NativeArrayInfoAttribute" => {
for (_, value) in attribute.value() {
match value {
Value::I16(value) => {
return ParamHint::ArrayRelativeLen(value as usize);
}
Value::I32(value) => return ParamHint::ArrayFixed(value as usize),
_ => {}
}
}
}
"MemorySizeAttribute" => {
for (_, value) in attribute.value() {
if let Value::I16(value) = value {
return ParamHint::ArrayRelativeByteLen(value as usize);
}
}
}
_ => {}
}
}
ParamHint::None
}
pub fn array_hints(signature: &Signature) -> Vec<ParamHint> {
let params = &signature.params;
let mut hints: Vec<_> = params.iter().map(array_hint).collect();
for position in 0..hints.len() {
if let ParamHint::ArrayRelativeLen(relative) | ParamHint::ArrayRelativeByteLen(relative) =
hints[position]
{
if position != relative
&& params
.get(relative)
.is_some_and(|len| len.is_input_only() && !len.ty.is_pointer())
{
hints[relative] = ParamHint::ArrayRelativePtr(position);
} else {
hints[position] = ParamHint::None;
}
}
}
let mut sets = BTreeMap::<usize, Vec<usize>>::new();
for (position, hint) in hints.iter().enumerate() {
if let ParamHint::ArrayRelativeLen(relative) | ParamHint::ArrayRelativeByteLen(relative) =
hint
{
sets.entry(*relative).or_default().push(position);
}
}
for (len, ptrs) in sets {
if ptrs.len() > 1 {
hints[len] = ParamHint::None;
for ptr in ptrs {
hints[ptr] = ParamHint::None;
}
}
}
for position in 0..hints.len() {
if let ParamHint::ArrayRelativeByteLen(relative) = hints[position]
&& !is_byte_size(¶ms[position].ty)
{
hints[position] = ParamHint::None;
hints[relative] = ParamHint::None;
}
}
hints
}