xabi-macros 0.1.2

Procedural macros for xabi
Documentation
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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
mod export;
mod handle;
mod shape;

use proc_macro2::TokenStream as TokenStream2;
use quote::{format_ident, quote};
use syn::{Error, FnArg, Ident, Path, PathArguments, TraitItemFn, Type};

use crate::type_shape::erase_lifetime_arguments;

pub(crate) use handle::HandleDecode;
use shape::{parse_arg, parse_ret, validate_shape};

#[derive(Clone)]
pub(crate) struct MethodSpec {
    pub(crate) name: Ident,
    pub(super) receiver_mut: bool,
    pub(super) asyncness: bool,
    pub(super) args: Vec<MethodArg>,
    pub(super) ret: MethodRet,
}

#[derive(Clone)]
pub(super) struct MethodArg {
    pub(super) name: Ident,
    pub(super) ty: Type,
    pub(super) kind: ArgKind,
}

#[derive(Clone, Copy, PartialEq, Eq)]
pub(super) enum ArgKind {
    Bytes,
    Str,
    Value,
}

#[derive(Clone, PartialEq, Eq)]
pub(super) enum MethodRet {
    String,
    U32,
    Bool,
    Value(Type),
    ResultUnit(Type),
    ResultBytes(Type),
    ResultString(Type),
    ResultValue {
        ok: Type,
        error: Type,
    },
    ResultObject {
        trait_path: Path,
        error: Type,
    },
    ResultObjectPair {
        ok: Type,
        trait_path: Path,
        error: Type,
    },
}

impl MethodSpec {
    pub(crate) fn parse(method: &TraitItemFn) -> syn::Result<Self> {
        if !method.sig.generics.params.is_empty() {
            return Err(Error::new_spanned(
                &method.sig.generics,
                "xabi does not support generic methods",
            ));
        }

        let mut inputs = method.sig.inputs.iter();
        let receiver_mut = match inputs.next() {
            Some(FnArg::Receiver(receiver)) if receiver.reference.is_some() => {
                receiver.mutability.is_some()
            }
            _ => {
                return Err(Error::new_spanned(
                    &method.sig.inputs,
                    "xabi methods must take &self or &mut self",
                ));
            }
        };

        let mut args = Vec::new();
        for input in inputs {
            match input {
                FnArg::Typed(arg) => args.push(parse_arg(arg)?),
                FnArg::Receiver(_) => {
                    return Err(Error::new_spanned(
                        &method.sig.inputs,
                        "xabi methods must take exactly one self receiver",
                    ));
                }
            }
        }
        if args.len() > 16 {
            return Err(Error::new_spanned(
                &method.sig.inputs,
                "xabi methods support at most 16 non-self arguments",
            ));
        }

        let ret = parse_ret(&method.sig.output)?;
        let asyncness = method.sig.asyncness.is_some();
        validate_shape(method, &args, &ret, asyncness)?;

        Ok(Self {
            name: method.sig.ident.clone(),
            receiver_mut,
            asyncness,
            args,
            ret,
        })
    }

    pub(crate) fn ffi_type(&self) -> syn::Result<TokenStream2> {
        let args = self.ffi_arg_types();
        if self.asyncness {
            return Ok(quote! {
                unsafe extern "C" fn(
                    *mut std::ffi::c_void,
                    #(#args)*
                    *mut ::xabi::XabiFuture,
                ) -> i32
            });
        }

        Ok(match self.ret.clone() {
            MethodRet::String if self.args.is_empty() => {
                quote!(unsafe extern "C" fn(*mut std::ffi::c_void) -> ::xabi::XabiOwnedBytes)
            }
            MethodRet::U32 if self.args.is_empty() => {
                quote!(unsafe extern "C" fn(*mut std::ffi::c_void) -> u32)
            }
            MethodRet::Bool if self.args.is_empty() => {
                quote!(unsafe extern "C" fn(*mut std::ffi::c_void) -> u8)
            }
            MethodRet::Value(_) if self.args.is_empty() => {
                quote!(unsafe extern "C" fn(*mut std::ffi::c_void) -> ::xabi::XabiOwnedBytes)
            }
            MethodRet::ResultUnit(_)
            | MethodRet::ResultBytes(_)
            | MethodRet::ResultString(_)
            | MethodRet::ResultValue { .. }
            | MethodRet::ResultObject { .. }
            | MethodRet::ResultObjectPair { .. } => {
                quote!(
                    unsafe extern "C" fn(
                        *mut std::ffi::c_void,
                        #(#args)*
                        *mut ::xabi::XabiOwnedBytes,
                    ) -> i32
                )
            }
            _ => {
                return Err(Error::new_spanned(
                    &self.name,
                    "unsupported xabi method shape",
                ));
            }
        })
    }

    pub(crate) fn layout_dependencies(&self) -> Vec<TokenStream2> {
        let mut deps = Vec::new();
        for arg in &self.args {
            if arg.kind == ArgKind::Value {
                let ty = &arg.ty;
                deps.push(quote! {
                    <#ty as ::xabi::XabiType>::collect_xabi_layout(collector);
                });
            }
        }

        match &self.ret {
            MethodRet::String | MethodRet::U32 | MethodRet::Bool => {}
            MethodRet::Value(ok) => {
                deps.push(quote! {
                    <#ok as ::xabi::XabiType>::collect_xabi_layout(collector);
                });
            }
            MethodRet::ResultUnit(error)
            | MethodRet::ResultBytes(error)
            | MethodRet::ResultString(error) => {
                deps.push(quote! {
                    <#error as ::xabi::XabiType>::collect_xabi_layout(collector);
                });
            }
            MethodRet::ResultValue { ok, error } => {
                deps.push(quote! {
                    <#ok as ::xabi::XabiType>::collect_xabi_layout(collector);
                    <#error as ::xabi::XabiType>::collect_xabi_layout(collector);
                });
            }
            MethodRet::ResultObject { trait_path, error } => {
                let abi_ident = generated_trait_type_path(trait_path, "XabiV1AbiTrait");
                deps.push(quote! {
                    <#abi_ident as ::xabi::XabiLayoutSource>::collect_xabi_layout(collector);
                    <#error as ::xabi::XabiType>::collect_xabi_layout(collector);
                });
            }
            MethodRet::ResultObjectPair {
                ok,
                trait_path,
                error,
            } => {
                let abi_ident = generated_trait_type_path(trait_path, "XabiV1AbiTrait");
                deps.push(quote! {
                    <#ok as ::xabi::XabiType>::collect_xabi_layout(collector);
                    <#abi_ident as ::xabi::XabiLayoutSource>::collect_xabi_layout(collector);
                    <#error as ::xabi::XabiType>::collect_xabi_layout(collector);
                });
            }
        }

        deps
    }

    pub(crate) fn ffi_type_name(&self) -> String {
        let mut args = vec!["*mut c_void".to_string()];
        args.extend(self.args.iter().map(|arg| match arg.kind {
            ArgKind::Bytes => "XabiBytes".to_string(),
            ArgKind::Str => "XabiStr".to_string(),
            ArgKind::Value => format!(
                "*const <{} as XabiType>::Wire",
                normalized_type_name(&arg.ty)
            ),
        }));

        if self.asyncness {
            args.push("*mut XabiFuture".to_string());
            return format!("unsafe extern \"C\" fn({}) -> i32", args.join(", "));
        }

        match &self.ret {
            MethodRet::String => {
                "unsafe extern \"C\" fn(*mut c_void) -> XabiOwnedBytes".to_string()
            }
            MethodRet::U32 => "unsafe extern \"C\" fn(*mut c_void) -> u32".to_string(),
            MethodRet::Bool => "unsafe extern \"C\" fn(*mut c_void) -> u8".to_string(),
            MethodRet::Value(_) => {
                "unsafe extern \"C\" fn(*mut c_void) -> XabiOwnedBytes".to_string()
            }
            MethodRet::ResultUnit(_)
            | MethodRet::ResultBytes(_)
            | MethodRet::ResultString(_)
            | MethodRet::ResultValue { .. }
            | MethodRet::ResultObject { .. }
            | MethodRet::ResultObjectPair { .. } => {
                args.push("*mut XabiOwnedBytes".to_string());
                format!("unsafe extern \"C\" fn({}) -> i32", args.join(", "))
            }
        }
    }

    fn ffi_arg_types(&self) -> Vec<TokenStream2> {
        self.args
            .iter()
            .map(|arg| {
                let ty = &arg.ty;
                match arg.kind {
                    ArgKind::Bytes => quote!(::xabi::XabiBytes,),
                    ArgKind::Str => quote!(::xabi::XabiStr,),
                    ArgKind::Value => quote!(*const <#ty as ::xabi::XabiType>::Wire,),
                }
            })
            .collect()
    }

    pub(super) fn ffi_arg_defs(&self) -> Vec<TokenStream2> {
        self.args
            .iter()
            .map(|arg| {
                let name = &arg.name;
                let ty = &arg.ty;
                match arg.kind {
                    ArgKind::Bytes => quote!(#name: ::xabi::XabiBytes,),
                    ArgKind::Str => quote!(#name: ::xabi::XabiStr,),
                    ArgKind::Value => quote!(#name: *const <#ty as ::xabi::XabiType>::Wire,),
                }
            })
            .collect()
    }

    pub(super) fn handle_arg_defs(&self) -> Vec<TokenStream2> {
        self.args
            .iter()
            .map(|arg| {
                let name = &arg.name;
                let ty = &arg.ty;
                quote!(#name: #ty,)
            })
            .collect()
    }

    pub(super) fn handle_arg_lowering(&self) -> (Vec<TokenStream2>, Vec<TokenStream2>) {
        let mut locals = Vec::new();
        let mut calls = Vec::new();
        for arg in &self.args {
            let name = &arg.name;
            match arg.kind {
                ArgKind::Bytes => calls.push(quote!(::xabi::XabiBytes::from_slice(#name),)),
                ArgKind::Str => calls.push(quote!(::xabi::XabiStr::from_borrowed(#name),)),
                ArgKind::Value => {
                    let wire = Ident::new(&format!("__xabi_wire_{name}"), name.span());
                    locals.push(quote! {
                        let #wire = ::xabi::__private::XabiWire::new(#name);
                    });
                    calls.push(quote!(#wire.as_ptr(),));
                }
            }
        }
        (locals, calls)
    }

    pub(super) fn export_arg_decoding(
        &self,
        asyncness: bool,
    ) -> (Vec<TokenStream2>, Vec<TokenStream2>) {
        let mut decoders = Vec::new();
        let mut calls = Vec::new();
        for arg in &self.args {
            let name = &arg.name;
            let ty = &arg.ty;
            match (arg.kind, asyncness) {
                (ArgKind::Bytes, false) => {
                    decoders.push(quote! {
                        let Ok(#name) = (unsafe { #name.as_slice() }) else {
                            return ::xabi::ERR_INVALID_ARGUMENT;
                        };
                    });
                    calls.push(quote!(#name,));
                }
                (ArgKind::Bytes, true) => {
                    decoders.push(quote! {
                        let Ok(#name) = (unsafe { #name.as_slice() }) else {
                            return ::xabi::ERR_INVALID_ARGUMENT;
                        };
                        let #name = #name.to_vec();
                    });
                    calls.push(quote!(&#name,));
                }
                (ArgKind::Str, false) => {
                    decoders.push(quote! {
                        let Ok(#name) = (unsafe { #name.as_str() }) else {
                            return ::xabi::ERR_INVALID_ARGUMENT;
                        };
                    });
                    calls.push(quote!(#name,));
                }
                (ArgKind::Str, true) => {
                    decoders.push(quote! {
                        let Ok(#name) = (unsafe { #name.as_str() }) else {
                            return ::xabi::ERR_INVALID_ARGUMENT;
                        };
                        let #name = #name.to_string();
                    });
                    calls.push(quote!(&#name,));
                }
                (ArgKind::Value, _) => {
                    decoders.push(quote! {
                        let Ok(#name) = (unsafe {
                            <#ty as ::xabi::XabiType>::xabi_take_from_wire(#name.cast_mut())
                        }) else {
                            return ::xabi::ERR_INVALID_ARGUMENT;
                        };
                    });
                    calls.push(quote!(#name,));
                }
            }
        }
        (decoders, calls)
    }

    pub(super) fn error_ty(&self) -> Option<&Type> {
        match &self.ret {
            MethodRet::ResultUnit(error)
            | MethodRet::ResultBytes(error)
            | MethodRet::ResultString(error) => Some(error),
            MethodRet::ResultValue { error, .. } => Some(error),
            MethodRet::ResultObject { error, .. } => Some(error),
            MethodRet::ResultObjectPair { error, .. } => Some(error),
            _ => None,
        }
    }
}

pub(super) fn generated_trait_type_path(trait_path: &Path, prefix: &str) -> TokenStream2 {
    let mut generated_path = trait_path.clone();
    let segment = generated_path
        .segments
        .last_mut()
        .expect("xabi object trait path has a final segment");
    let trait_ident = segment.ident.clone();
    segment.ident = format_ident!("{}{}", prefix, trait_ident);
    segment.arguments = PathArguments::None;
    quote!(#generated_path)
}

fn normalized_type_name(ty: &Type) -> String {
    let ty = erase_lifetime_arguments(ty);
    let mut value = quote!(#ty)
        .to_string()
        .split_whitespace()
        .collect::<Vec<_>>()
        .join(" ");
    for (from, to) in [
        (" :: ", "::"),
        (":: ", "::"),
        (" ::", "::"),
        (" < ", "<"),
        ("< ", "<"),
        (" >", ">"),
        (" ,", ","),
    ] {
        value = value.replace(from, to);
    }
    value
}