1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
|
//! Implement COM interfaces for Rust types.
//!
//! Take a look at [macro@implement] for an example.
//!
//! Learn more about Rust for Windows here: <https://github.com/microsoft/windows-rs>
use quote::{quote, ToTokens};
mod r#gen;
use r#gen::gen_all;
#[cfg(test)]
mod tests;
/// Implements one or more COM interfaces.
///
/// # Example
/// ```rust,no_run
/// use windows_core::*;
///
/// #[interface("094d70d6-5202-44b8-abb8-43860da5aca2")]
/// unsafe trait IValue: IUnknown {
/// fn GetValue(&self, value: *mut i32) -> HRESULT;
/// }
///
/// #[implement(IValue)]
/// struct Value(i32);
///
/// impl IValue_Impl for Value_Impl {
/// unsafe fn GetValue(&self, value: *mut i32) -> HRESULT {
/// *value = self.0;
/// HRESULT(0)
/// }
/// }
///
/// let object: IValue = Value(123).into();
/// // Call interface methods...
/// ```
#[proc_macro_attribute]
pub fn implement(
attributes: proc_macro::TokenStream,
type_tokens: proc_macro::TokenStream,
) -> proc_macro::TokenStream {
implement_core(attributes.into(), type_tokens.into()).into()
}
fn implement_core(
attributes: proc_macro2::TokenStream,
item_tokens: proc_macro2::TokenStream,
) -> proc_macro2::TokenStream {
let attributes = syn::parse2::<ImplementAttributes>(attributes).unwrap();
let original_type = syn::parse2::<syn::ItemStruct>(item_tokens).unwrap();
// Do a little thinking and assemble ImplementInputs. We pass ImplementInputs to
// all of our gen_* function.
let inputs = ImplementInputs {
original_ident: original_type.ident.clone(),
interface_chains: convert_implements_to_interface_chains(attributes.implement),
trust_level: attributes.trust_level,
impl_ident: quote::format_ident!("{}_Impl", &original_type.ident),
constraints: {
if let Some(where_clause) = &original_type.generics.where_clause {
where_clause.predicates.to_token_stream()
} else {
quote!()
}
},
generics: if !original_type.generics.params.is_empty() {
let mut params = quote! {};
original_type.generics.params.to_tokens(&mut params);
quote! { <#params> }
} else {
quote! { <> }
},
is_generic: !original_type.generics.params.is_empty(),
original_type,
};
let items = gen_all(&inputs);
let mut tokens = inputs.original_type.into_token_stream();
for item in items {
tokens.extend(item.into_token_stream());
}
tokens
}
/// This provides the inputs to the `gen_*` functions, which generate the proc macro output.
struct ImplementInputs {
/// The user's type that was marked with `#[implement]`.
original_type: syn::ItemStruct,
/// The identifier for the user's original type definition.
original_ident: syn::Ident,
/// The list of interface chains that this type implements.
interface_chains: Vec<InterfaceChain>,
/// The "trust level", which is returned by `IInspectable::GetTrustLevel`.
trust_level: usize,
/// The identifier of the `Foo_Impl` type.
impl_ident: syn::Ident,
/// The list of constraints needed for this `Foo_Impl` type.
constraints: proc_macro2::TokenStream,
/// The list of generic parameters for this `Foo_Impl` type, including `<` and `>`.
/// If there are no generics, this contains `<>`.
generics: proc_macro2::TokenStream,
/// True if the user type has any generic parameters.
is_generic: bool,
}
/// Describes one COM interface chain.
struct InterfaceChain {
/// The name of the field for the vtable chain, e.g. `interface4_ifoo`.
field_ident: syn::Ident,
/// The name of the associated constant item for the vtable chain's initializer,
/// e.g. `INTERFACE4_IFOO_VTABLE`.
vtable_const_ident: syn::Ident,
implement: ImplementType,
}
struct ImplementType {
type_name: String,
generics: Vec<ImplementType>,
/// The best span for diagnostics.
span: proc_macro2::Span,
}
impl ImplementType {
fn to_ident(&self) -> proc_macro2::TokenStream {
let type_name = syn::parse_str::<proc_macro2::TokenStream>(&self.type_name)
.expect("Invalid token stream");
let generics = self.generics.iter().map(|g| g.to_ident());
quote! { #type_name<#(#generics,)*> }
}
fn to_vtbl_ident(&self) -> proc_macro2::TokenStream {
let ident = self.to_ident();
quote! {
<#ident as ::windows_core::Interface>::Vtable
}
}
}
#[derive(Default)]
struct ImplementAttributes {
pub implement: Vec<ImplementType>,
pub trust_level: usize,
}
impl syn::parse::Parse for ImplementAttributes {
fn parse(cursor: syn::parse::ParseStream<'_>) -> syn::parse::Result<Self> {
let mut input = Self::default();
while !cursor.is_empty() {
input.parse_implement(cursor)?;
}
Ok(input)
}
}
impl ImplementAttributes {
fn parse_implement(&mut self, cursor: syn::parse::ParseStream<'_>) -> syn::parse::Result<()> {
let tree = cursor.parse::<UseTree2>()?;
self.walk_implement(&tree, &mut String::new())?;
if !cursor.is_empty() {
cursor.parse::<syn::Token![,]>()?;
}
Ok(())
}
fn walk_implement(
&mut self,
tree: &UseTree2,
namespace: &mut String,
) -> syn::parse::Result<()> {
match tree {
UseTree2::Path(input) => {
if !namespace.is_empty() {
namespace.push_str("::");
}
namespace.push_str(&input.ident.to_string());
self.walk_implement(&input.tree, namespace)?;
}
UseTree2::Name(_) => {
self.implement.push(tree.to_element_type(namespace)?);
}
UseTree2::Group(input) => {
for tree in &input.items {
self.walk_implement(tree, namespace)?;
}
}
UseTree2::TrustLevel(input) => self.trust_level = *input,
}
Ok(())
}
}
enum UseTree2 {
Path(UsePath2),
Name(UseName2),
Group(UseGroup2),
TrustLevel(usize),
}
impl UseTree2 {
fn to_element_type(&self, namespace: &mut String) -> syn::parse::Result<ImplementType> {
match self {
UseTree2::Path(input) => {
if !namespace.is_empty() {
namespace.push_str("::");
}
namespace.push_str(&input.ident.to_string());
input.tree.to_element_type(namespace)
}
UseTree2::Name(input) => {
let mut type_name = input.ident.to_string();
let span = input.ident.span();
if !namespace.is_empty() {
type_name = format!("{namespace}::{type_name}");
}
let mut generics = vec![];
for g in &input.generics {
generics.push(g.to_element_type(&mut String::new())?);
}
Ok(ImplementType {
type_name,
generics,
span,
})
}
UseTree2::Group(input) => Err(syn::parse::Error::new(
input.brace_token.span.join(),
"Syntax not supported",
)),
_ => unimplemented!(),
}
}
}
struct UsePath2 {
pub ident: syn::Ident,
pub tree: Box<UseTree2>,
}
struct UseName2 {
pub ident: syn::Ident,
pub generics: Vec<UseTree2>,
}
struct UseGroup2 {
pub brace_token: syn::token::Brace,
pub items: syn::punctuated::Punctuated<UseTree2, syn::Token![,]>,
}
impl syn::parse::Parse for UseTree2 {
fn parse(input: syn::parse::ParseStream<'_>) -> syn::parse::Result<UseTree2> {
let lookahead = input.lookahead1();
if lookahead.peek(syn::Ident) {
use syn::ext::IdentExt;
let ident = input.call(syn::Ident::parse_any)?;
if input.peek(syn::Token![::]) {
input.parse::<syn::Token![::]>()?;
Ok(UseTree2::Path(UsePath2 {
ident,
tree: Box::new(input.parse()?),
}))
} else if input.peek(syn::Token![=]) {
if ident != "TrustLevel" {
return Err(syn::parse::Error::new(
ident.span(),
"Unrecognized key-value pair",
));
}
input.parse::<syn::Token![=]>()?;
let span = input.span();
let value = input.call(syn::Ident::parse_any)?;
match value.to_string().as_str() {
"Partial" => Ok(UseTree2::TrustLevel(1)),
"Full" => Ok(UseTree2::TrustLevel(2)),
_ => Err(syn::parse::Error::new(
span,
"`TrustLevel` must be `Partial` or `Full`",
)),
}
} else {
let generics = if input.peek(syn::Token![<]) {
input.parse::<syn::Token![<]>()?;
let mut generics = Vec::new();
loop {
generics.push(input.parse::<UseTree2>()?);
if input.parse::<syn::Token![,]>().is_err() {
break;
}
}
input.parse::<syn::Token![>]>()?;
generics
} else {
Vec::new()
};
Ok(UseTree2::Name(UseName2 { ident, generics }))
}
} else if lookahead.peek(syn::token::Brace) {
let content;
let brace_token = syn::braced!(content in input);
let items = content.parse_terminated(UseTree2::parse, syn::Token![,])?;
Ok(UseTree2::Group(UseGroup2 { brace_token, items }))
} else {
Err(lookahead.error())
}
}
}
fn convert_implements_to_interface_chains(implements: Vec<ImplementType>) -> Vec<InterfaceChain> {
let mut chains = Vec::with_capacity(implements.len());
for (i, implement) in implements.into_iter().enumerate() {
// Create an identifier for this interface chain.
// We only use this for naming fields; it is never visible to the developer.
// This helps with debugging.
//
// We use i + 1 so that it matches the numbering of our interface offsets. Interface 0
// is the "identity" interface.
let mut ident_string = format!("interface{}", i + 1);
let suffix = get_interface_ident_suffix(&implement.type_name);
if !suffix.is_empty() {
ident_string.push('_');
ident_string.push_str(&suffix);
}
let field_ident = syn::Ident::new(&ident_string, implement.span);
let mut vtable_const_string = ident_string.clone();
vtable_const_string.make_ascii_uppercase();
vtable_const_string.insert_str(0, "VTABLE_");
let vtable_const_ident = syn::Ident::new(&vtable_const_string, implement.span);
chains.push(InterfaceChain {
implement,
field_ident,
vtable_const_ident,
});
}
chains
}
fn get_interface_ident_suffix(type_name: &str) -> String {
let mut suffix = String::new();
for c in type_name.chars() {
let c = c.to_ascii_lowercase();
if suffix.len() >= 20 {
break;
}
if c.is_ascii_alphanumeric() {
suffix.push(c);
}
}
suffix
}
|