use super::*;
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Param {
pub def: Option<MethodParam>,
pub name: String,
pub ty: Type,
pub implied_input: bool,
pub associated_enum: Option<Type>,
}
impl std::ops::Deref for Param {
type Target = Type;
fn deref(&self) -> &Self::Target {
&self.ty
}
}
impl Param {
pub fn name(&self) -> &str {
&self.name
}
pub fn attributes(
&self,
) -> impl Iterator<Item = windows_metadata::reader::Attribute<'static>> + '_ {
self.def.into_iter().flat_map(|def| def.attributes())
}
pub fn has_attribute(&self, name: &str) -> bool {
self.def.is_some_and(|def| def.has_attribute(name))
}
pub fn is_convertible(&self) -> bool {
self.is_input_only() && self.ty.is_convertible()
}
pub fn is_convertible_with_reader(&self, reader: &Reader) -> bool {
self.is_input_only() && self.projection_type(reader).is_convertible()
}
pub fn projection_type(&self, reader: &Reader) -> Type {
self.ty.projection_type(reader)
}
pub fn projection_pointee(&self, reader: &Reader) -> Type {
self.ty.projection_pointee(reader)
}
pub fn ireference_inner(&self, reader: &Reader) -> Option<&Type> {
if !self.is_input_only() {
return None;
}
self.ty.ireference_inner_for_sugar(reader)
}
pub fn is_input_only(&self) -> bool {
self.implied_input
|| self.def.is_some_and(|def| {
def.direction() == windows_metadata::reader::ParamDirection::Input
})
}
pub fn has_direction(&self) -> bool {
self.implied_input
|| self.def.is_some_and(|def| {
def.direction() != windows_metadata::reader::ParamDirection::Unspecified
})
}
pub fn is_metadata_output_only(&self) -> bool {
self.def
.is_some_and(|def| def.direction() == windows_metadata::reader::ParamDirection::Output)
}
pub fn is_optional_or_reserved(&self) -> bool {
self.def
.is_some_and(|def| def.is_optional() || def.is_reserved())
}
pub fn is_retval_attribute(&self) -> bool {
self.def.is_some_and(|def| def.is_retval_attribute())
}
pub fn has_buffer_attributes(&self) -> bool {
self.buffer_attributes().next().is_some()
}
fn buffer_attributes(
&self,
) -> impl Iterator<Item = windows_metadata::reader::Attribute<'static>> + '_ {
self.attributes().filter(|attribute| {
attribute.namespace() == "Windows.Win32.Metadata"
&& attribute.name() == "BufferAttribute"
})
}
pub fn has_unresolved_contracts(&self) -> bool {
self.attributes().any(|attribute| {
attribute.namespace() == "Windows.Win32.Metadata"
&& attribute.name() == "IncompleteAttribute"
})
}
pub fn is_retval_candidate(&self, reader: &Reader) -> bool {
if !self.is_retval_attribute() || !self.projection_type(reader).is_pointer() {
return false;
}
let Some(def) = self.def else {
return false;
};
if def.direction() != windows_metadata::reader::ParamDirection::Output
|| def.is_optional()
|| def.is_reserved()
{
return false;
}
!self.has_buffer_attributes() && !self.has_unresolved_contracts()
}
pub fn write_ident(&self) -> TokenStream {
to_ident(&self.name.to_lowercase())
}
}