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
|
use super::*;
pub fn gen(gen: &Gen, def: TypeDef) -> TokenStream {
if gen.reader.type_def_is_contract(def) {
return quote! {};
}
if gen.reader.type_def_is_handle(def) {
return handles::gen(gen, def);
}
gen_struct_with_name(gen, def, gen.reader.type_def_name(def), &Cfg::default())
}
fn gen_struct_with_name(gen: &Gen, def: TypeDef, struct_name: &str, cfg: &Cfg) -> TokenStream {
let name = to_ident(struct_name);
if gen.reader.type_def_fields(def).next().is_none() {
let mut tokens = quote! {
#[repr(C)]
pub struct #name(pub u8);
impl ::core::marker::Copy for #name {}
impl ::core::clone::Clone for #name {
fn clone(&self) -> Self {
*self
}
}
};
if !gen.sys {
tokens.combine("e! {
impl ::windows::core::TypeKind for #name {
type TypeKind = ::windows::core::CopyType;
}
});
}
return tokens;
}
let flags = gen.reader.type_def_flags(def);
let cfg = cfg.union(&gen.reader.type_def_cfg(def, &[]));
let repr = if let Some(layout) = gen.reader.type_def_class_layout(def) {
let packing = Literal::usize_unsuffixed(gen.reader.class_layout_packing_size(layout));
quote! { #[repr(C, packed(#packing))] }
} else {
quote! { #[repr(C)] }
};
let fields = gen.reader.type_def_fields(def).map(|f| {
let name = to_ident(gen.reader.field_name(f));
let ty = gen.reader.field_type(f, Some(def));
if gen.reader.field_flags(f).contains(FieldAttributes::LITERAL) {
quote! {}
} else if !gen.sys
&& flags.contains(TypeAttributes::EXPLICIT_LAYOUT)
&& !gen.reader.field_is_copyable(f, def)
{
// Rust can't tell that the type is copyable and won't accept windows::core::ManuallyDrop
let ty = gen.type_default_name(&ty);
quote! { pub #name: ::std::mem::ManuallyDrop<#ty>, }
} else if !gen.sys
&& !flags.contains(TypeAttributes::WINRT)
&& !gen.reader.field_is_blittable(f, def)
{
if let Type::Win32Array((ty, len)) = ty {
let ty = gen.type_default_name(&ty);
quote! { pub #name: [::std::mem::ManuallyDrop<#ty>; #len], }
} else {
let ty = gen.type_default_name(&ty);
quote! { pub #name: ::std::mem::ManuallyDrop<#ty>, }
}
} else {
let ty = gen.type_default_name(&ty);
quote! { pub #name: #ty, }
}
});
let struct_or_union = if flags.contains(TypeAttributes::EXPLICIT_LAYOUT) {
quote! { union }
} else {
quote! { struct }
};
let doc = gen.cfg_doc(&cfg);
let features = gen.cfg_features(&cfg);
let mut tokens = quote! {
#repr
#doc
#features
pub #struct_or_union #name {#(#fields)*}
};
tokens.combine(&gen_struct_constants(gen, def, &name, &cfg));
tokens.combine(&gen_copy_clone(gen, def, &name, &cfg));
tokens.combine(&gen_debug(gen, def, &name, &cfg));
tokens.combine(&gen_windows_traits(gen, def, &name, &cfg));
tokens.combine(&gen_compare_traits(gen, def, &name, &cfg));
if !gen.sys {
tokens.combine("e! {
#features
impl ::core::default::Default for #name {
fn default() -> Self {
unsafe { ::core::mem::zeroed() }
}
}
});
}
for (index, nested_type) in gen.reader.nested_types(def).enumerate() {
let nested_name = format!("{struct_name}_{index}");
tokens.combine(&gen_struct_with_name(gen, nested_type, &nested_name, &cfg));
}
tokens
}
fn gen_windows_traits(gen: &Gen, def: TypeDef, name: &TokenStream, cfg: &Cfg) -> TokenStream {
if gen.sys {
quote! {}
} else {
let features = gen.cfg_features(cfg);
let is_copy = gen.reader.type_def_is_blittable(def);
let type_kind = if is_copy {
quote! { CopyType }
} else {
quote! { ValueType }
};
let mut tokens = quote! {
#features
impl ::windows::core::TypeKind for #name {
type TypeKind = ::windows::core::#type_kind;
}
};
if gen
.reader
.type_def_flags(def)
.contains(TypeAttributes::WINRT)
{
let signature =
Literal::byte_string(gen.reader.type_def_signature(def, &[]).as_bytes());
tokens.combine("e! {
#features
impl ::windows::core::RuntimeType for #name {
const SIGNATURE: ::windows::imp::ConstBuffer = ::windows::imp::ConstBuffer::from_slice(#signature);
}
});
}
tokens
}
}
fn gen_compare_traits(gen: &Gen, def: TypeDef, name: &TokenStream, cfg: &Cfg) -> TokenStream {
let features = gen.cfg_features(cfg);
if gen.sys
|| gen.reader.type_def_has_explicit_layout(def)
|| gen.reader.type_def_has_packing(def)
|| gen.reader.type_def_has_callback(def)
{
quote! {}
} else {
let fields = gen.reader.type_def_fields(def).filter_map(|f| {
let name = to_ident(gen.reader.field_name(f));
if gen.reader.field_flags(f).contains(FieldAttributes::LITERAL) {
None
} else {
Some(quote! { self.#name == other.#name })
}
});
quote! {
#features
impl ::core::cmp::PartialEq for #name {
fn eq(&self, other: &Self) -> bool {
#(#fields)&&*
}
}
#features
impl ::core::cmp::Eq for #name {}
}
}
}
fn gen_debug(gen: &Gen, def: TypeDef, ident: &TokenStream, cfg: &Cfg) -> TokenStream {
if gen.sys
|| gen.reader.type_def_has_explicit_layout(def)
|| gen.reader.type_def_has_packing(def)
{
quote! {}
} else {
let name = ident.as_str();
let features = gen.cfg_features(cfg);
let fields = gen.reader.type_def_fields(def).filter_map(|f| {
if gen.reader.field_flags(f).contains(FieldAttributes::LITERAL) {
None
} else {
let name = gen.reader.field_name(f);
let ident = to_ident(name);
let ty = gen.reader.field_type(f, Some(def));
if gen.reader.type_has_callback(&ty) {
None
} else {
Some(quote! { .field(#name, &self.#ident) })
}
}
});
quote! {
#features
impl ::core::fmt::Debug for #ident {
fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
f.debug_struct(#name) #(#fields)* .finish()
}
}
}
}
}
fn gen_copy_clone(gen: &Gen, def: TypeDef, name: &TokenStream, cfg: &Cfg) -> TokenStream {
let features = gen.cfg_features(cfg);
if gen.sys || gen.reader.type_def_is_copyable(def) {
quote! {
#features
impl ::core::marker::Copy for #name {}
#features
impl ::core::clone::Clone for #name {
fn clone(&self) -> Self {
*self
}
}
}
} else if gen.reader.type_def_class_layout(def).is_some() {
// Don't support copy/clone of packed structs: https://github.com/rust-lang/rust/issues/82523
quote! {}
} else if !gen
.reader
.type_def_flags(def)
.contains(TypeAttributes::WINRT)
{
quote! {
#features
impl ::core::clone::Clone for #name {
fn clone(&self) -> Self {
unsafe { ::core::mem::transmute_copy(self) }
}
}
}
} else {
let fields = gen.reader.type_def_fields(def).map(|f| {
let name = to_ident(gen.reader.field_name(f));
if gen.reader.field_flags(f).contains(FieldAttributes::LITERAL) {
quote! {}
} else if gen.reader.field_is_blittable(f, def) {
quote! { #name: self.#name }
} else {
quote! { #name: self.#name.clone() }
}
});
quote! {
#features
impl ::core::clone::Clone for #name {
fn clone(&self) -> Self {
Self { #(#fields),* }
}
}
}
}
}
fn gen_struct_constants(
gen: &Gen,
def: TypeDef,
struct_name: &TokenStream,
cfg: &Cfg,
) -> TokenStream {
let features = gen.cfg_features(cfg);
let constants = gen.reader.type_def_fields(def).filter_map(|f| {
if gen.reader.field_flags(f).contains(FieldAttributes::LITERAL) {
if let Some(constant) = gen.reader.field_constant(f) {
let name = to_ident(gen.reader.field_name(f));
let value = gen.typed_value(&gen.reader.constant_value(constant));
return Some(quote! {
pub const #name: #value;
});
}
}
None
});
let mut tokens = quote! { #(#constants)* };
if !tokens.is_empty() {
tokens = quote! {
#features
impl #struct_name {
#tokens
}
};
}
tokens
}
|