/
methods.rs
332 lines (299 loc) · 13.8 KB
/
methods.rs
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
use super::*;
pub fn gen_winrt_method(def: &TypeDef, kind: InterfaceKind, method: &MethodDef, method_names: &mut MethodNames, virtual_names: &mut MethodNames, gen: &Gen) -> TokenStream {
let signature = method.signature(&def.generics);
let params = if kind == InterfaceKind::Composable { &signature.params[..signature.params.len() - 2] } else { &signature.params };
let (name, name_compose) = if kind == InterfaceKind::Composable && signature.params.len() == 2 {
("new".into(), "compose".into())
} else {
let name = method_names.add(method);
let name_compose = name.join("_compose");
(name, name_compose)
};
let vname = virtual_names.add(method);
let constraints = gen_param_constraints(params, gen);
let cfg = gen.method_cfg(def, method).gen_with_doc(gen);
let args = params.iter().map(gen_winrt_abi_arg);
let params = gen_winrt_params(params, gen);
let interface_name = gen_type_name(def, gen);
let return_type_tokens = if let Some(return_sig) = &signature.return_sig {
let tokens = gen_result_sig(return_sig, gen);
if return_sig.is_array {
quote! { ::windows::core::Array<#tokens> }
} else {
quote! { #tokens }
}
} else {
quote! { () }
};
let return_arg = if let Some(return_sig) = &signature.return_sig {
if return_sig.is_array {
let return_sig = gen_element_name(&return_sig.kind, gen);
quote! { ::windows::core::Array::<#return_sig>::set_abi_len(&mut result__), &mut result__ as *mut _ as _ }
} else {
quote! { &mut result__ }
}
} else {
quote! {}
};
let composable_args = match kind {
InterfaceKind::Composable => quote! {
::core::mem::transmute_copy(&derived__), base__ as *mut _ as _,
},
_ => quote! {},
};
let (vcall, vcall_none) = if let Some(return_sig) = &signature.return_sig {
if return_sig.is_array {
(
quote! {
let mut result__: #return_type_tokens = ::core::mem::zeroed();
(::windows::core::Interface::vtable(this).#vname)(::core::mem::transmute_copy(this), #(#args,)* #composable_args #return_arg)
.and_then(|| result__ )
},
quote! {},
)
} else {
let abi_type_name = gen_abi_element_name(return_sig, gen);
let args = quote! { #(#args,)* };
(
quote! {
let mut result__: #abi_type_name = ::core::mem::zeroed();
(::windows::core::Interface::vtable(this).#vname)(::core::mem::transmute_copy(this), #args #composable_args #return_arg)
.from_abi::<#return_type_tokens>(result__ )
},
quote! {
let mut result__: #abi_type_name = ::core::mem::zeroed();
(::windows::core::Interface::vtable(this).#vname)(::core::mem::transmute_copy(this), #args ::core::ptr::null_mut(), &mut ::core::option::Option::<::windows::core::IInspectable>::None as *mut _ as _, #return_arg)
.from_abi::<#return_type_tokens>(result__ )
},
)
}
} else {
(
quote! {
(::windows::core::Interface::vtable(this).#vname)(::core::mem::transmute_copy(this), #(#args,)* #composable_args).ok()
},
quote! {},
)
};
match kind {
InterfaceKind::Default => quote! {
#cfg
pub fn #name<#constraints>(&self, #params) -> ::windows::core::Result<#return_type_tokens> {
let this = self;
unsafe {
#vcall
}
}
},
InterfaceKind::NonDefault | InterfaceKind::Base => {
quote! {
#cfg
pub fn #name<#constraints>(&self, #params) -> ::windows::core::Result<#return_type_tokens> {
let this = &::windows::core::Interface::cast::<#interface_name>(self)?;
unsafe {
#vcall
}
}
}
}
InterfaceKind::Static => {
quote! {
#cfg
pub fn #name<#constraints>(#params) -> ::windows::core::Result<#return_type_tokens> {
Self::#interface_name(|this| unsafe { #vcall })
}
}
}
InterfaceKind::Composable => {
quote! {
#cfg
pub fn #name<#constraints>(#params) -> ::windows::core::Result<#return_type_tokens> {
Self::#interface_name(|this| unsafe { #vcall_none })
}
#cfg
pub fn #name_compose<#constraints T: ::windows::core::Compose>(#params compose: T) -> ::windows::core::Result<#return_type_tokens> {
Self::#interface_name(|this| unsafe {
let (derived__, base__) = ::windows::core::Compose::compose(compose);
#vcall
})
}
}
}
}
}
pub fn gen_com_method(def: &TypeDef, method: &MethodDef, method_names: &mut MethodNames, virtual_names: &mut MethodNames, base_count: usize, gen: &Gen) -> TokenStream {
let signature = method.signature(&def.generics);
let name = method_names.add(method);
let vname = virtual_names.add(method);
let constraints = gen_param_constraints(&signature.params, gen);
let cfg = gen.method_cfg(def, method).gen_with_doc(gen);
let mut bases = quote! {};
for _ in 0..base_count {
bases.combine("e! { .base });
}
match signature.kind() {
SignatureKind::Query => {
let leading_params = &signature.params[..signature.params.len() - 2];
let args = leading_params.iter().map(gen_win32_abi_arg);
let params = gen_win32_params(leading_params, gen);
quote! {
#cfg
pub unsafe fn #name<#constraints T: ::windows::core::Interface>(&self, #params) -> ::windows::core::Result<T> {
let mut result__ = ::core::option::Option::None;
(::windows::core::Interface::vtable(self)#bases.#vname)(::core::mem::transmute_copy(self), #(#args,)* &<T as ::windows::core::Interface>::IID, &mut result__ as *mut _ as *mut _).and_some(result__)
}
}
}
SignatureKind::QueryOptional => {
let leading_params = &signature.params[..signature.params.len() - 2];
let args = leading_params.iter().map(gen_win32_abi_arg);
let params = gen_win32_params(leading_params, gen);
quote! {
#cfg
pub unsafe fn #name<#constraints T: ::windows::core::Interface>(&self, #params result__: *mut ::core::option::Option<T>) -> ::windows::core::Result<()> {
(::windows::core::Interface::vtable(self)#bases.#vname)(::core::mem::transmute_copy(self), #(#args,)* &<T as ::windows::core::Interface>::IID, result__ as *mut _ as *mut _).ok()
}
}
}
SignatureKind::ResultValue => {
let leading_params = &signature.params[..signature.params.len() - 1];
let args = leading_params.iter().map(gen_win32_abi_arg);
let params = gen_win32_params(leading_params, gen);
let mut return_sig = signature.params[signature.params.len() - 1].signature.clone();
return_sig.pointers -= 1;
let return_type_tokens = gen_result_sig(&return_sig, gen);
let abi_return_type_tokens = gen_abi_sig(&return_sig, gen);
quote! {
#cfg
pub unsafe fn #name<#constraints>(&self, #params) -> ::windows::core::Result<#return_type_tokens> {
let mut result__: #abi_return_type_tokens = ::core::mem::zeroed();
(::windows::core::Interface::vtable(self)#bases.#vname)(::core::mem::transmute_copy(self), #(#args,)* ::core::mem::transmute(&mut result__))
.from_abi::<#return_type_tokens>(result__ )
}
}
}
SignatureKind::ResultVoid => {
let params = gen_win32_params(&signature.params, gen);
let args = signature.params.iter().map(gen_win32_abi_arg);
quote! {
#cfg
pub unsafe fn #name<#constraints>(&self, #params) -> ::windows::core::Result<()> {
(::windows::core::Interface::vtable(self)#bases.#vname)(::core::mem::transmute_copy(self), #(#args,)*).ok()
}
}
}
SignatureKind::ReturnStruct => {
let params = gen_win32_params(&signature.params, gen);
let args = signature.params.iter().map(gen_win32_abi_arg);
// TODO: why is this using gen_abi_element_name?
let return_sig = gen_abi_element_name(&signature.return_sig.unwrap(), gen);
quote! {
#cfg
pub unsafe fn #name<#constraints>(&self, #params) -> #return_sig {
let mut result__: #return_sig = :: core::mem::zeroed();
(::windows::core::Interface::vtable(self)#bases.#vname)(::core::mem::transmute_copy(self), &mut result__ #(,#args)*);
result__
}
}
}
SignatureKind::PreserveSig => {
let params = gen_win32_params(&signature.params, gen);
let args = signature.params.iter().map(gen_win32_abi_arg);
// TODO: why gen_return_sig exists? Don't we always know it will be not ReturnVoid?
let return_sig = gen_return_sig(&signature, gen);
quote! {
#cfg
pub unsafe fn #name<#constraints>(&self, #params) #return_sig {
::core::mem::transmute((::windows::core::Interface::vtable(self)#bases.#vname)(::core::mem::transmute_copy(self), #(#args,)*))
}
}
}
SignatureKind::ReturnVoid => {
let params = gen_win32_params(&signature.params, gen);
let args = signature.params.iter().map(gen_win32_abi_arg);
quote! {
#cfg
pub unsafe fn #name<#constraints>(&self, #params) {
(::windows::core::Interface::vtable(self)#bases.#vname)(::core::mem::transmute_copy(self), #(#args,)*)
}
}
}
}
}
pub fn gen_win32_params(params: &[MethodParam], gen: &Gen) -> TokenStream {
let mut tokens = quote! {};
for (position, param) in params.iter().enumerate() {
let name = gen_param_name(¶m.param);
let kind = if param.is_convertible() { format!("Param{}", position).into() } else { gen_param_sig(param, gen) };
tokens.combine("e! { #name: #kind, });
}
tokens
}
pub fn gen_winrt_params(params: &[MethodParam], gen: &Gen) -> TokenStream {
let mut result = quote! {};
for (position, param) in params.iter().enumerate() {
let name = gen_param_name(¶m.param);
let kind = gen_element_name(¶m.signature.kind, gen);
if param.signature.is_array {
if param.param.is_input() {
result.combine("e! { #name: &[<#kind as ::windows::core::DefaultType>::DefaultType], });
} else if param.signature.by_ref {
result.combine("e! { #name: &mut ::windows::core::Array<#kind>, });
} else {
result.combine("e! { #name: &mut [<#kind as ::windows::core::DefaultType>::DefaultType], });
}
} else if param.param.is_input() {
if param.is_convertible() {
let kind: TokenStream = format!("Param{}", position).into();
result.combine("e! { #name: #kind, });
} else {
result.combine("e! { #name: #kind, });
}
} else if param.signature.kind.is_nullable() {
result.combine("e! { #name: &mut ::core::option::Option<#kind>, });
} else if let ElementType::GenericParam(_) = param.signature.kind {
result.combine("e! { &mut <#kind as ::windows::core::DefaultType>::DefaultType, });
} else {
result.combine("e! { #name: &mut #kind, });
};
}
result
}
pub fn gen_win32_abi_arg(param: &MethodParam) -> TokenStream {
let name = gen_param_name(¶m.param);
if param.is_convertible() {
quote! { #name.into_param().abi() }
} else {
quote! { ::core::mem::transmute(#name) }
}
}
pub fn gen_winrt_abi_arg(param: &MethodParam) -> TokenStream {
let name = gen_param_name(¶m.param);
if param.signature.is_array {
if param.param.is_input() {
quote! { #name.len() as u32, ::core::mem::transmute(#name.as_ptr()) }
} else if param.signature.by_ref {
quote! { #name.set_abi_len(), #name as *mut _ as _ }
} else {
quote! { #name.len() as u32, ::core::mem::transmute_copy(&#name) }
}
} else if param.param.is_input() {
if param.is_convertible() {
if param.signature.is_const {
quote! { &#name.into_param().abi() }
} else {
quote! { #name.into_param().abi() }
}
} else if param.signature.kind.is_blittable() {
quote! { #name }
} else if param.signature.pointers == 0 {
quote! { ::core::mem::transmute_copy(#name) }
} else {
quote! { ::core::mem::transmute(#name) }
}
} else if param.signature.kind.is_blittable() || (param.signature.pointers > 0 && !param.signature.kind.is_nullable()) {
quote! { #name }
} else {
quote! { #name as *mut _ as _ }
}
}