| 1 |
/*! |
| 2 |
Learn more about Rust for Windows here: <https://github.com/microsoft/windows-rs> |
| 3 |
*/ |
| 4 |
|
| 5 |
use quote::quote; |
| 6 |
use syn::spanned::Spanned; |
| 7 |
|
| 8 |
/// Defines a COM interface to call or implement. |
| 9 |
/// |
| 10 |
/// # Example |
| 11 |
/// ```rust,ignore |
| 12 |
/// #[interface("094d70d6-5202-44b8-abb8-43860da5aca2")] |
| 13 |
/// unsafe trait IValue: IUnknown { |
| 14 |
/// fn GetValue(&self, value: *mut i32) -> HRESULT; |
| 15 |
/// } |
| 16 |
/// |
| 17 |
/// #[implement(IValue)] |
| 18 |
/// struct Value(i32); |
| 19 |
/// |
| 20 |
/// impl IValue_Impl for Value { |
| 21 |
/// unsafe fn GetValue(&self, value: *mut i32) -> HRESULT { |
| 22 |
/// *value = self.0; |
| 23 |
/// HRESULT(0) |
| 24 |
/// } |
| 25 |
/// } |
| 26 |
/// |
| 27 |
/// fn main() { |
| 28 |
/// let object: IValue = Value(123).into(); |
| 29 |
/// // Call interface methods... |
| 30 |
/// } |
| 31 |
/// ``` |
| 32 |
#[proc_macro_attribute] |
| 33 |
pub fn interface( |
| 34 |
attributes: proc_macro::TokenStream, |
| 35 |
original_type: proc_macro::TokenStream, |
| 36 |
) -> proc_macro::TokenStream { |
| 37 |
let guid = syn::parse_macro_input!(attributes as Guid); |
| 38 |
let interface = syn::parse_macro_input!(original_type as Interface); |
| 39 |
let tokens = match interface.gen_tokens(&guid) { |
| 40 |
Ok(t) => t, |
| 41 |
Err(e) => return e.to_compile_error().into(), |
| 42 |
}; |
| 43 |
tokens.into() |
| 44 |
} |
| 45 |
|
| 46 |
macro_rules! bail { |
| 47 |
($item:expr, $($msg:tt),*) => { |
| 48 |
return Err(syn::Error::new($item.span(), std::fmt::format(format_args!($($msg),*)))); |
| 49 |
}; |
| 50 |
|
| 51 |
} |
| 52 |
|
| 53 |
macro_rules! unexpected_token { |
| 54 |
($item:expr, $msg:expr) => { |
| 55 |
if let Some(i) = $item { |
| 56 |
bail!(i, "unexpected {}", $msg); |
| 57 |
} |
| 58 |
}; |
| 59 |
} |
| 60 |
macro_rules! expected_token { |
| 61 |
($sig:tt.$item:tt(), $msg:expr) => { |
| 62 |
if let None = $sig.$item() { |
| 63 |
bail!($sig, "expected {}", $msg); |
| 64 |
} |
| 65 |
}; |
| 66 |
} |
| 67 |
|
| 68 |
/// Parsed interface |
| 69 |
/// |
| 70 |
/// ```rust,ignore |
| 71 |
/// #[windows_interface::interface("8CEEB155-2849-4ce5-9448-91FF70E1E4D9")] |
| 72 |
/// unsafe trait IUIAnimationVariable: IUnknown { |
| 73 |
/// //^ parses this |
| 74 |
/// fn GetValue(&self, value: *mut f64) -> HRESULT; |
| 75 |
/// } |
| 76 |
/// ``` |
| 77 |
struct Interface { |
| 78 |
visibility: syn::Visibility, |
| 79 |
name: syn::Ident, |
| 80 |
parent: Option<syn::Path>, |
| 81 |
methods: Vec<InterfaceMethod>, |
| 82 |
docs: Vec<syn::Attribute>, |
| 83 |
} |
| 84 |
|
| 85 |
impl Interface { |
| 86 |
/// Generates all the code needed for a COM interface |
| 87 |
fn gen_tokens(&self, guid: &Guid) -> syn::Result<proc_macro2::TokenStream> { |
| 88 |
let vis = &self.visibility; |
| 89 |
let name = &self.name; |
| 90 |
let docs = &self.docs; |
| 91 |
let parent = self.parent_type(); |
| 92 |
let vtable_name = quote::format_ident!("{}_Vtbl", name); |
| 93 |
let guid = guid.to_tokens()?; |
| 94 |
let implementation = self.gen_implementation(); |
| 95 |
let com_trait = self.get_com_trait(); |
| 96 |
let vtable = self.gen_vtable(&vtable_name); |
| 97 |
let conversions = self.gen_conversions(); |
| 98 |
|
| 99 |
Ok(quote! { |
| 100 |
#[repr(transparent)] |
| 101 |
#(#docs)* |
| 102 |
#vis struct #name(#parent); |
| 103 |
#implementation |
| 104 |
unsafe impl ::windows_core::Interface for #name { |
| 105 |
type Vtable = #vtable_name; |
| 106 |
const IID: ::windows_core::GUID = #guid; |
| 107 |
} |
| 108 |
impl ::windows_core::RuntimeName for #name {} |
| 109 |
impl ::core::ops::Deref for #name { |
| 110 |
type Target = #parent; |
| 111 |
fn deref(&self) -> &Self::Target { |
| 112 |
unsafe { ::core::mem::transmute(self) } |
| 113 |
} |
| 114 |
} |
| 115 |
#com_trait |
| 116 |
#vtable |
| 117 |
#conversions |
| 118 |
}) |
| 119 |
} |
| 120 |
|
| 121 |
/// Generates the methods users can call on the COM interface pointer |
| 122 |
fn gen_implementation(&self) -> proc_macro2::TokenStream { |
| 123 |
let name = &self.name; |
| 124 |
let methods = self |
| 125 |
.methods |
| 126 |
.iter() |
| 127 |
.map(|m| { |
| 128 |
let vis = &m.visibility; |
| 129 |
let name = &m.name; |
| 130 |
|
| 131 |
let generics = m.gen_consume_generics(); |
| 132 |
let params = m.gen_consume_params(); |
| 133 |
let args = m.gen_consume_args(); |
| 134 |
let ret = &m.ret; |
| 135 |
|
| 136 |
if m.is_result() { |
| 137 |
quote! { |
| 138 |
#[inline(always)] |
| 139 |
#vis unsafe fn #name<#(#generics),*>(&self, #(#params),*) #ret { |
| 140 |
(::windows_core::Interface::vtable(self).#name)(::windows_core::Interface::as_raw(self), #(#args),*).ok() |
| 141 |
} |
| 142 |
} |
| 143 |
} else { |
| 144 |
quote! { |
| 145 |
#[inline(always)] |
| 146 |
#vis unsafe fn #name<#(#generics),*>(&self, #(#params),*) #ret { |
| 147 |
(::windows_core::Interface::vtable(self).#name)(::windows_core::Interface::as_raw(self), #(#args),*) |
| 148 |
} |
| 149 |
} |
| 150 |
} |
| 151 |
}) |
| 152 |
.collect::<Vec<_>>(); |
| 153 |
quote! { |
| 154 |
impl #name { |
| 155 |
#(#methods)* |
| 156 |
} |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
fn get_com_trait(&self) -> proc_macro2::TokenStream { |
| 161 |
let name = quote::format_ident!("{}_Impl", self.name); |
| 162 |
let vis = &self.visibility; |
| 163 |
let methods = self |
| 164 |
.methods |
| 165 |
.iter() |
| 166 |
.map(|m| { |
| 167 |
let name = &m.name; |
| 168 |
let docs = &m.docs; |
| 169 |
let args = m.gen_args(); |
| 170 |
let ret = &m.ret; |
| 171 |
quote! { |
| 172 |
#(#docs)* |
| 173 |
unsafe fn #name(&self, #(#args),*) #ret; |
| 174 |
} |
| 175 |
}) |
| 176 |
.collect::<Vec<_>>(); |
| 177 |
let parent = self.parent_trait_constraint(); |
| 178 |
|
| 179 |
quote! { |
| 180 |
#[allow(non_camel_case_types)] |
| 181 |
#vis trait #name: Sized + #parent { |
| 182 |
#(#methods)* |
| 183 |
} |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
/// Generates the vtable for a COM interface |
| 188 |
fn gen_vtable(&self, vtable_name: &syn::Ident) -> proc_macro2::TokenStream { |
| 189 |
let vis = &self.visibility; |
| 190 |
let name = &self.name; |
| 191 |
let trait_name = quote::format_ident!("{}_Impl", name); |
| 192 |
let implvtbl_name = quote::format_ident!("{}_ImplVtbl", name); |
| 193 |
|
| 194 |
let vtable_entries = self |
| 195 |
.methods |
| 196 |
.iter() |
| 197 |
.map(|m| { |
| 198 |
let name = &m.name; |
| 199 |
let ret = &m.ret; |
| 200 |
let args = m.gen_args(); |
| 201 |
|
| 202 |
if m.is_result() { |
| 203 |
quote! { |
| 204 |
pub #name: unsafe extern "system" fn(this: *mut ::core::ffi::c_void, #(#args),*) -> ::windows_core::HRESULT, |
| 205 |
} |
| 206 |
} else { |
| 207 |
quote! { |
| 208 |
pub #name: unsafe extern "system" fn(this: *mut ::core::ffi::c_void, #(#args),*) #ret, |
| 209 |
} |
| 210 |
} |
| 211 |
}) |
| 212 |
.collect::<Vec<_>>(); |
| 213 |
|
| 214 |
let parent_vtable_generics = quote!(Identity, OFFSET); |
| 215 |
let parent_vtable = self.parent_vtable(); |
| 216 |
|
| 217 |
// or_parent_matches will be `|| parent::matches(iid)` if this interface inherits from another |
| 218 |
// interface (except for IUnknown) or will be empty if this is not applicable. This is what allows |
| 219 |
// QueryInterface to work correctly for all interfaces in an inheritance chain, e.g. |
| 220 |
// IFoo3 derives from IFoo2 derives from IFoo. |
| 221 |
// |
| 222 |
// We avoid matching IUnknown because object identity depends on the uniqueness of the IUnknown pointer. |
| 223 |
let or_parent_matches = match parent_vtable.as_ref() { |
| 224 |
Some(parent) if !self.parent_is_iunknown() => quote! (|| <#parent>::matches(iid)), |
| 225 |
_ => quote!(), |
| 226 |
}; |
| 227 |
|
| 228 |
let functions = self |
| 229 |
.methods |
| 230 |
.iter() |
| 231 |
.map(|m| { |
| 232 |
let name = &m.name; |
| 233 |
let args = m.gen_args(); |
| 234 |
let params = &m |
| 235 |
.args |
| 236 |
.iter() |
| 237 |
.map(|a| { |
| 238 |
let pat = &a.pat; |
| 239 |
quote! { #pat } |
| 240 |
}) |
| 241 |
.collect::<Vec<_>>(); |
| 242 |
let ret = &m.ret; |
| 243 |
|
| 244 |
let ret = if m.is_result() { |
| 245 |
quote! { -> ::windows_core::HRESULT } |
| 246 |
} else { |
| 247 |
quote! { #ret } |
| 248 |
}; |
| 249 |
|
| 250 |
if parent_vtable.is_some() { |
| 251 |
quote! { |
| 252 |
unsafe extern "system" fn #name< |
| 253 |
Identity: ::windows_core::IUnknownImpl, |
| 254 |
const OFFSET: isize |
| 255 |
>( |
| 256 |
this: *mut ::core::ffi::c_void, // <-- This is the COM "this" pointer, which is not the same as &T or &T_Impl. |
| 257 |
#(#args),* |
| 258 |
) #ret |
| 259 |
where |
| 260 |
Identity : #trait_name |
| 261 |
{ |
| 262 |
// This step is essentially a virtual dispatch adjustor thunk. Its purpose is to adjust |
| 263 |
// the "this" pointer from the address used by the COM interface to the root of the |
| 264 |
// MyApp_Impl object. Since a given MyApp_Impl may implement more than one COM interface |
| 265 |
// (and more than one COM interface chain), we need to know how to get from COM's "this" |
| 266 |
// back to &MyApp_Impl. The OFFSET constant gives us the value (in pointer-sized units). |
| 267 |
let this_outer: &Identity = &*((this as *const *const ()).offset(OFFSET) as *const Identity); |
| 268 |
|
| 269 |
// Last, we invoke the implementation function. |
| 270 |
// We use explicit <Impl as IFoo_Impl> so that we can select the correct method |
| 271 |
// for situations where IFoo3 derives from IFoo2 and both declare a method with |
| 272 |
// the same name. |
| 273 |
<Identity as #trait_name>::#name(this_outer, #(#params),*).into() |
| 274 |
} |
| 275 |
} |
| 276 |
} else { |
| 277 |
quote! { |
| 278 |
unsafe extern "system" fn #name<Impl: #trait_name>(this: *mut ::core::ffi::c_void, #(#args),*) #ret { |
| 279 |
let this = (this as *mut *mut ::core::ffi::c_void) as *const ::windows_core::ScopedHeap; |
| 280 |
let this = (*this).this as *const Impl; |
| 281 |
(*this).#name(#(#params),*).into() |
| 282 |
} |
| 283 |
} |
| 284 |
} |
| 285 |
}) |
| 286 |
.collect::<Vec<_>>(); |
| 287 |
|
| 288 |
if let Some(parent_vtable) = parent_vtable { |
| 289 |
let entries = self |
| 290 |
.methods |
| 291 |
.iter() |
| 292 |
.map(|m| { |
| 293 |
let name = &m.name; |
| 294 |
quote!(#name: #name::<Identity, OFFSET>) |
| 295 |
}) |
| 296 |
.collect::<Vec<_>>(); |
| 297 |
|
| 298 |
quote! { |
| 299 |
#[repr(C)] |
| 300 |
#[doc(hidden)] |
| 301 |
#vis struct #vtable_name { |
| 302 |
pub base__: #parent_vtable, |
| 303 |
#(#vtable_entries)* |
| 304 |
} |
| 305 |
impl #vtable_name { |
| 306 |
pub const fn new< |
| 307 |
Identity: ::windows_core::IUnknownImpl, |
| 308 |
const OFFSET: isize, |
| 309 |
>() -> Self |
| 310 |
where |
| 311 |
Identity : #trait_name |
| 312 |
{ |
| 313 |
#(#functions)* |
| 314 |
Self { base__: #parent_vtable::new::<#parent_vtable_generics>(), #(#entries),* } |
| 315 |
} |
| 316 |
|
| 317 |
#[inline(always)] |
| 318 |
pub fn matches(iid: &::windows_core::GUID) -> bool { |
| 319 |
*iid == <#name as ::windows_core::Interface>::IID |
| 320 |
#or_parent_matches |
| 321 |
} |
| 322 |
} |
| 323 |
} |
| 324 |
} else { |
| 325 |
let entries = self |
| 326 |
.methods |
| 327 |
.iter() |
| 328 |
.map(|m| { |
| 329 |
let name = &m.name; |
| 330 |
quote!(#name: #name::<Impl>) |
| 331 |
}) |
| 332 |
.collect::<Vec<_>>(); |
| 333 |
|
| 334 |
quote! { |
| 335 |
#[repr(C)] |
| 336 |
#[doc(hidden)] |
| 337 |
#vis struct #vtable_name { |
| 338 |
#(#vtable_entries)* |
| 339 |
} |
| 340 |
impl #vtable_name { |
| 341 |
pub const fn new<Impl: #trait_name>() -> Self { |
| 342 |
#(#functions)* |
| 343 |
Self { #(#entries),* } |
| 344 |
} |
| 345 |
} |
| 346 |
struct #implvtbl_name<T: #trait_name> (::core::marker::PhantomData<T>); |
| 347 |
impl<T: #trait_name> #implvtbl_name<T> { |
| 348 |
const VTABLE: #vtable_name = #vtable_name::new::<T>(); |
| 349 |
} |
| 350 |
impl #name { |
| 351 |
fn new<'a, T: #trait_name>(this: &'a T) -> ::windows_core::ScopedInterface<'a, #name> { |
| 352 |
let this = ::windows_core::ScopedHeap { vtable: &#implvtbl_name::<T>::VTABLE as *const _ as *const _, this: this as *const _ as *const _ }; |
| 353 |
let this = ::core::mem::ManuallyDrop::new(::windows_core::imp::Box::new(this)); |
| 354 |
unsafe { ::windows_core::ScopedInterface::new(::core::mem::transmute(&this.vtable)) } |
| 355 |
} |
| 356 |
} |
| 357 |
} |
| 358 |
} |
| 359 |
} |
| 360 |
|
| 361 |
/// Generates various conversions such as from and to `IUnknown` |
| 362 |
fn gen_conversions(&self) -> proc_macro2::TokenStream { |
| 363 |
let name = &self.name; |
| 364 |
let name_string = format!("{name}"); |
| 365 |
quote! { |
| 366 |
impl ::core::convert::From<#name> for ::windows_core::IUnknown { |
| 367 |
fn from(value: #name) -> Self { |
| 368 |
unsafe { ::core::mem::transmute(value) } |
| 369 |
} |
| 370 |
} |
| 371 |
impl ::core::convert::From<&#name> for ::windows_core::IUnknown { |
| 372 |
fn from(value: &#name) -> Self { |
| 373 |
::core::convert::From::from(::core::clone::Clone::clone(value)) |
| 374 |
} |
| 375 |
} |
| 376 |
impl ::core::clone::Clone for #name { |
| 377 |
fn clone(&self) -> Self { |
| 378 |
Self(self.0.clone()) |
| 379 |
} |
| 380 |
} |
| 381 |
impl ::core::cmp::PartialEq for #name { |
| 382 |
fn eq(&self, other: &Self) -> bool { |
| 383 |
self.0 == other.0 |
| 384 |
} |
| 385 |
} |
| 386 |
impl ::core::cmp::Eq for #name {} |
| 387 |
impl ::core::fmt::Debug for #name { |
| 388 |
fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result { |
| 389 |
f.debug_tuple(#name_string).field(&::windows_core::Interface::as_raw(self)).finish() |
| 390 |
} |
| 391 |
} |
| 392 |
} |
| 393 |
} |
| 394 |
|
| 395 |
fn parent_type(&self) -> proc_macro2::TokenStream { |
| 396 |
if let Some(parent) = &self.parent { |
| 397 |
quote!(#parent) |
| 398 |
} else { |
| 399 |
quote!(::core::ptr::NonNull<::core::ffi::c_void>) |
| 400 |
} |
| 401 |
} |
| 402 |
|
| 403 |
fn parent_vtable(&self) -> Option<proc_macro2::TokenStream> { |
| 404 |
if let Some((ident, path)) = self.parent_path().split_last() { |
| 405 |
let ident = quote::format_ident!("{}_Vtbl", ident); |
| 406 |
Some(quote! { #(#path::)* #ident }) |
| 407 |
} else { |
| 408 |
None |
| 409 |
} |
| 410 |
} |
| 411 |
|
| 412 |
fn parent_is_iunknown(&self) -> bool { |
| 413 |
if let Some(ident) = self.parent_path().last() { |
| 414 |
ident == "IUnknown" |
| 415 |
} else { |
| 416 |
false |
| 417 |
} |
| 418 |
} |
| 419 |
|
| 420 |
fn parent_path(&self) -> Vec<syn::Ident> { |
| 421 |
if let Some(parent) = &self.parent { |
| 422 |
parent |
| 423 |
.segments |
| 424 |
.iter() |
| 425 |
.map(|segment| segment.ident.clone()) |
| 426 |
.collect() |
| 427 |
} else { |
| 428 |
vec![] |
| 429 |
} |
| 430 |
} |
| 431 |
|
| 432 |
/// Gets the parent trait constrait which is nothing if the parent is IUnknown |
| 433 |
fn parent_trait_constraint(&self) -> proc_macro2::TokenStream { |
| 434 |
if let Some((ident, path)) = self.parent_path().split_last() { |
| 435 |
if ident != "IUnknown" { |
| 436 |
let ident = quote::format_ident!("{}_Impl", ident); |
| 437 |
return quote! { #(#path::)* #ident }; |
| 438 |
} |
| 439 |
} |
| 440 |
|
| 441 |
quote! {} |
| 442 |
} |
| 443 |
} |
| 444 |
|
| 445 |
impl syn::parse::Parse for Interface { |
| 446 |
fn parse(input: syn::parse::ParseStream<'_>) -> syn::Result<Self> { |
| 447 |
let attributes = input.call(syn::Attribute::parse_outer)?; |
| 448 |
let mut docs = Vec::new(); |
| 449 |
for attr in attributes.into_iter() { |
| 450 |
let path = attr.path(); |
| 451 |
if path.is_ident("doc") { |
| 452 |
docs.push(attr); |
| 453 |
} else { |
| 454 |
return Err(syn::Error::new(path.span(), "Unrecognized attribute ")); |
| 455 |
} |
| 456 |
} |
| 457 |
|
| 458 |
let visibility = input.parse::<syn::Visibility>()?; |
| 459 |
_ = input.parse::<syn::Token![unsafe]>()?; |
| 460 |
_ = input.parse::<syn::Token![trait]>()?; |
| 461 |
let name = input.parse::<syn::Ident>()?; |
| 462 |
_ = input.parse::<syn::Token![:]>(); |
| 463 |
let parent = input.parse::<syn::Path>().ok(); |
| 464 |
let content; |
| 465 |
syn::braced!(content in input); |
| 466 |
let mut methods = Vec::new(); |
| 467 |
while !content.is_empty() { |
| 468 |
methods.push(content.parse::<InterfaceMethod>()?); |
| 469 |
} |
| 470 |
Ok(Self { |
| 471 |
visibility, |
| 472 |
methods, |
| 473 |
name, |
| 474 |
parent, |
| 475 |
docs, |
| 476 |
}) |
| 477 |
} |
| 478 |
} |
| 479 |
|
| 480 |
/// Parsed interface guid attribute |
| 481 |
/// |
| 482 |
/// ```rust,ignore |
| 483 |
/// #[windows_interface::interface("8CEEB155-2849-4ce5-9448-91FF70E1E4D9")] |
| 484 |
/// //^ parses this |
| 485 |
/// unsafe trait IUIAnimationVariable: IUnknown { |
| 486 |
/// fn GetValue(&self, value: *mut f64) -> HRESULT; |
| 487 |
/// } |
| 488 |
/// ``` |
| 489 |
struct Guid(Option<syn::LitStr>); |
| 490 |
|
| 491 |
impl Guid { |
| 492 |
fn to_tokens(&self) -> syn::Result<proc_macro2::TokenStream> { |
| 493 |
fn hex_lit(num: &str) -> syn::LitInt { |
| 494 |
syn::LitInt::new(&format!("0x{num}"), proc_macro2::Span::call_site()) |
| 495 |
} |
| 496 |
|
| 497 |
fn ensure_length( |
| 498 |
part: Option<&str>, |
| 499 |
index: usize, |
| 500 |
length: usize, |
| 501 |
span: proc_macro2::Span, |
| 502 |
) -> syn::Result<String> { |
| 503 |
let part = match part { |
| 504 |
Some(p) => p, |
| 505 |
None => { |
| 506 |
return Err(syn::Error::new( |
| 507 |
span, |
| 508 |
format!("The IID missing part at index {index}"), |
| 509 |
)) |
| 510 |
} |
| 511 |
}; |
| 512 |
|
| 513 |
if part.len() != length { |
| 514 |
return Err(syn::Error::new( |
| 515 |
span, |
| 516 |
format!( |
| 517 |
"The IID part at index {} must be {} characters long but was {} characters", |
| 518 |
index, |
| 519 |
length, |
| 520 |
part.len() |
| 521 |
), |
| 522 |
)); |
| 523 |
} |
| 524 |
|
| 525 |
Ok(part.to_owned()) |
| 526 |
} |
| 527 |
|
| 528 |
if let Some(value) = &self.0 { |
| 529 |
let guid_value = value.value(); |
| 530 |
let mut delimited = guid_value.split('-').fuse(); |
| 531 |
let chunks = [ |
| 532 |
ensure_length(delimited.next(), 0, 8, value.span())?, |
| 533 |
ensure_length(delimited.next(), 1, 4, value.span())?, |
| 534 |
ensure_length(delimited.next(), 2, 4, value.span())?, |
| 535 |
ensure_length(delimited.next(), 3, 4, value.span())?, |
| 536 |
ensure_length(delimited.next(), 4, 12, value.span())?, |
| 537 |
]; |
| 538 |
|
| 539 |
let data1 = hex_lit(&chunks[0]); |
| 540 |
let data2 = hex_lit(&chunks[1]); |
| 541 |
let data3 = hex_lit(&chunks[2]); |
| 542 |
let (data4_1, data4_2) = chunks[3].split_at(2); |
| 543 |
let data4_1 = hex_lit(data4_1); |
| 544 |
let data4_2 = hex_lit(data4_2); |
| 545 |
let (data4_3, rest) = chunks[4].split_at(2); |
| 546 |
let data4_3 = hex_lit(data4_3); |
| 547 |
|
| 548 |
let (data4_4, rest) = rest.split_at(2); |
| 549 |
let data4_4 = hex_lit(data4_4); |
| 550 |
|
| 551 |
let (data4_5, rest) = rest.split_at(2); |
| 552 |
let data4_5 = hex_lit(data4_5); |
| 553 |
|
| 554 |
let (data4_6, rest) = rest.split_at(2); |
| 555 |
let data4_6 = hex_lit(data4_6); |
| 556 |
|
| 557 |
let (data4_7, data4_8) = rest.split_at(2); |
| 558 |
let data4_7 = hex_lit(data4_7); |
| 559 |
let data4_8 = hex_lit(data4_8); |
| 560 |
Ok(quote! { |
| 561 |
::windows_core::GUID { |
| 562 |
data1: #data1, |
| 563 |
data2: #data2, |
| 564 |
data3: #data3, |
| 565 |
data4: [#data4_1, #data4_2, #data4_3, #data4_4, #data4_5, #data4_6, #data4_7, #data4_8] |
| 566 |
} |
| 567 |
}) |
| 568 |
} else { |
| 569 |
Ok(quote! { |
| 570 |
::windows_core::GUID::zeroed() |
| 571 |
}) |
| 572 |
} |
| 573 |
} |
| 574 |
} |
| 575 |
|
| 576 |
impl syn::parse::Parse for Guid { |
| 577 |
fn parse(cursor: syn::parse::ParseStream<'_>) -> syn::Result<Self> { |
| 578 |
let string: Option<syn::LitStr> = cursor.parse().ok(); |
| 579 |
|
| 580 |
Ok(Self(string)) |
| 581 |
} |
| 582 |
} |
| 583 |
|
| 584 |
/// A parsed interface method |
| 585 |
/// |
| 586 |
/// ```rust,ignore |
| 587 |
/// #[windows_interface::interface("8CEEB155-2849-4ce5-9448-91FF70E1E4D9")] |
| 588 |
/// unsafe trait IUIAnimationVariable: IUnknown { |
| 589 |
/// fn GetValue(&self, value: *mut f64) -> HRESULT; |
| 590 |
/// //^ parses this |
| 591 |
/// } |
| 592 |
/// ``` |
| 593 |
struct InterfaceMethod { |
| 594 |
pub name: syn::Ident, |
| 595 |
pub visibility: syn::Visibility, |
| 596 |
pub args: Vec<InterfaceMethodArg>, |
| 597 |
pub ret: syn::ReturnType, |
| 598 |
pub docs: Vec<syn::Attribute>, |
| 599 |
} |
| 600 |
|
| 601 |
impl InterfaceMethod { |
| 602 |
fn is_result(&self) -> bool { |
| 603 |
if let syn::ReturnType::Type(_, ty) = &self.ret { |
| 604 |
if let syn::Type::Path(path) = &**ty { |
| 605 |
if let Some(segment) = path.path.segments.last() { |
| 606 |
let ident = segment.ident.to_string(); |
| 607 |
if ident == "Result" { |
| 608 |
if let syn::PathArguments::AngleBracketed(args) = &segment.arguments { |
| 609 |
if args.args.len() == 1 { |
| 610 |
return true; |
| 611 |
} |
| 612 |
} |
| 613 |
} |
| 614 |
} |
| 615 |
} |
| 616 |
} |
| 617 |
|
| 618 |
false |
| 619 |
} |
| 620 |
|
| 621 |
/// Generates arguments (of the form `$pat: $type`) |
| 622 |
fn gen_args(&self) -> Vec<proc_macro2::TokenStream> { |
| 623 |
self.args |
| 624 |
.iter() |
| 625 |
.map(|a| { |
| 626 |
let pat = &a.pat; |
| 627 |
let ty = &a.ty; |
| 628 |
quote! { #pat: #ty } |
| 629 |
}) |
| 630 |
.collect::<Vec<_>>() |
| 631 |
} |
| 632 |
|
| 633 |
fn gen_consume_generics(&self) -> Vec<proc_macro2::TokenStream> { |
| 634 |
self.args |
| 635 |
.iter() |
| 636 |
.enumerate() |
| 637 |
.filter_map(|(generic_index, a)| { |
| 638 |
if let Some((ty, ident)) = a.borrow_type() { |
| 639 |
let generic_ident = quote::format_ident!("P{generic_index}"); |
| 640 |
if ident == "Ref" { |
| 641 |
Some(quote! { #generic_ident: ::windows_core::Param<#ty> }) |
| 642 |
} else { |
| 643 |
Some(quote! { #generic_ident: ::windows_core::OutParam<#ty> }) |
| 644 |
} |
| 645 |
} else { |
| 646 |
None |
| 647 |
} |
| 648 |
}) |
| 649 |
.collect::<Vec<_>>() |
| 650 |
} |
| 651 |
|
| 652 |
fn gen_consume_params(&self) -> Vec<proc_macro2::TokenStream> { |
| 653 |
self.args |
| 654 |
.iter() |
| 655 |
.enumerate() |
| 656 |
.map(|(generic_index, a)| { |
| 657 |
let pat = &a.pat; |
| 658 |
|
| 659 |
if a.borrow_type().is_some() { |
| 660 |
let generic_ident = quote::format_ident!("P{generic_index}"); |
| 661 |
quote! { #pat: #generic_ident } |
| 662 |
} else { |
| 663 |
let ty = &a.ty; |
| 664 |
quote! { #pat: #ty } |
| 665 |
} |
| 666 |
}) |
| 667 |
.collect::<Vec<_>>() |
| 668 |
} |
| 669 |
|
| 670 |
fn gen_consume_args(&self) -> Vec<proc_macro2::TokenStream> { |
| 671 |
self.args |
| 672 |
.iter() |
| 673 |
.map(|a| { |
| 674 |
let pat = &a.pat; |
| 675 |
|
| 676 |
if let Some((_, ident)) = a.borrow_type() { |
| 677 |
if ident == "Ref" { |
| 678 |
quote! { #pat.param().borrow() } |
| 679 |
} else { |
| 680 |
quote! { #pat.borrow_mut() } |
| 681 |
} |
| 682 |
} else { |
| 683 |
quote! { #pat } |
| 684 |
} |
| 685 |
}) |
| 686 |
.collect::<Vec<_>>() |
| 687 |
} |
| 688 |
} |
| 689 |
|
| 690 |
impl syn::parse::Parse for InterfaceMethod { |
| 691 |
fn parse(input: syn::parse::ParseStream<'_>) -> syn::Result<Self> { |
| 692 |
let docs = input.call(syn::Attribute::parse_outer)?; |
| 693 |
let visibility = input.parse::<syn::Visibility>()?; |
| 694 |
let method = input.parse::<syn::TraitItemFn>()?; |
| 695 |
unexpected_token!(docs.iter().find(|a| !a.path().is_ident("doc")), "attribute"); |
| 696 |
unexpected_token!(method.default, "default method implementation"); |
| 697 |
let sig = method.sig; |
| 698 |
unexpected_token!(sig.abi, "abi declaration"); |
| 699 |
unexpected_token!(sig.asyncness, "async declaration"); |
| 700 |
unexpected_token!(sig.generics.params.iter().next(), "generics declaration"); |
| 701 |
unexpected_token!(sig.constness, "const declaration"); |
| 702 |
expected_token!( |
| 703 |
sig.receiver(), |
| 704 |
"the method to have &self as its first argument" |
| 705 |
); |
| 706 |
unexpected_token!(sig.variadic, "variadic args"); |
| 707 |
let args = sig |
| 708 |
.inputs |
| 709 |
.into_iter() |
| 710 |
.filter_map(|a| match a { |
| 711 |
syn::FnArg::Receiver(_) => None, |
| 712 |
syn::FnArg::Typed(p) => Some(p), |
| 713 |
}) |
| 714 |
.map(|p| { |
| 715 |
Ok(InterfaceMethodArg { |
| 716 |
ty: p.ty, |
| 717 |
pat: p.pat, |
| 718 |
}) |
| 719 |
}) |
| 720 |
.collect::<Result<Vec<InterfaceMethodArg>, syn::Error>>()?; |
| 721 |
|
| 722 |
let ret = sig.output; |
| 723 |
Ok(InterfaceMethod { |
| 724 |
name: sig.ident, |
| 725 |
visibility, |
| 726 |
args, |
| 727 |
ret, |
| 728 |
docs, |
| 729 |
}) |
| 730 |
} |
| 731 |
} |
| 732 |
|
| 733 |
/// An argument to an interface method |
| 734 |
struct InterfaceMethodArg { |
| 735 |
/// The type of the argument |
| 736 |
pub ty: Box<syn::Type>, |
| 737 |
/// The name of the argument |
| 738 |
pub pat: Box<syn::Pat>, |
| 739 |
} |
| 740 |
|
| 741 |
impl InterfaceMethodArg { |
| 742 |
fn borrow_type(&self) -> Option<(syn::Type, String)> { |
| 743 |
if let syn::Type::Path(path) = &*self.ty { |
| 744 |
if let Some(segment) = path.path.segments.last() { |
| 745 |
let ident = segment.ident.to_string(); |
| 746 |
if matches!(ident.as_str(), "Ref" | "OutRef") { |
| 747 |
if let syn::PathArguments::AngleBracketed(args) = &segment.arguments { |
| 748 |
if args.args.len() == 1 { |
| 749 |
if let Some(syn::GenericArgument::Type(ty)) = args.args.first() { |
| 750 |
return Some((ty.clone(), ident)); |
| 751 |
} |
| 752 |
} |
| 753 |
} |
| 754 |
} |
| 755 |
} |
| 756 |
} |
| 757 |
|
| 758 |
None |
| 759 |
} |
| 760 |
} |