rune_macros/
function.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
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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
use proc_macro2::TokenStream;
use quote::{quote, quote_spanned, ToTokens};
use syn::parse::ParseStream;
use syn::punctuated::Punctuated;
use syn::spanned::Spanned;
use syn::Token;

#[derive(Default)]
enum Path {
    #[default]
    None,
    Rename(syn::PathSegment),
    Protocol(syn::Path),
}

#[derive(Default)]
pub(crate) struct FunctionAttrs {
    instance: bool,
    /// A free function.
    free: bool,
    /// Keep the existing function in place, and generate a separate hidden meta function.
    keep: bool,
    /// Path to register in.
    path: Path,
    /// Looks like an associated type.
    self_type: Option<syn::PathSegment>,
    /// Defines a fallible function which can make use of the `?` operator.
    vm_result: bool,
    /// The function is deprecated.
    deprecated: Option<syn::LitStr>,
}

impl FunctionAttrs {
    /// Parse the given parse stream.
    pub(crate) fn parse(input: ParseStream) -> syn::Result<Self> {
        let mut out = Self::default();

        while !input.is_empty() {
            let ident = input.parse::<syn::Ident>()?;

            if ident == "instance" {
                out.instance = true;
            } else if ident == "free" {
                out.free = true;
            } else if ident == "keep" {
                out.keep = true;
            } else if ident == "vm_result" {
                out.vm_result = true;
            } else if ident == "protocol" {
                input.parse::<Token![=]>()?;
                let protocol: syn::Path = input.parse()?;
                out.path = Path::Protocol(if let Some(protocol) = protocol.get_ident() {
                    syn::Path {
                        leading_colon: None,
                        segments: ["rune", "runtime", "Protocol"]
                            .into_iter()
                            .map(|i| syn::Ident::new(i, protocol.span()))
                            .chain(Some(protocol.clone()))
                            .map(syn::PathSegment::from)
                            .collect(),
                    }
                } else {
                    protocol
                })
            } else if ident == "path" {
                input.parse::<Token![=]>()?;

                let path = input.parse::<syn::Path>()?;

                if path.segments.len() > 2 {
                    return Err(syn::Error::new_spanned(
                        path,
                        "Expected at most two path segments",
                    ));
                }

                let mut it = path.segments.into_iter();

                let Some(first) = it.next() else {
                    return Err(syn::Error::new(
                        input.span(),
                        "Expected at least one path segment",
                    ));
                };

                if let Some(second) = it.next() {
                    let syn::PathArguments::None = &first.arguments else {
                        return Err(syn::Error::new_spanned(
                            first.arguments,
                            "Unsupported arguments",
                        ));
                    };

                    out.self_type = Some(first);
                    out.path = Path::Rename(second);
                } else if first.ident == "Self" {
                    out.self_type = Some(first);
                } else {
                    out.path = Path::Rename(first);
                }
            } else if ident == "deprecated" {
                input.parse::<Token![=]>()?;
                out.deprecated = Some(input.parse()?);
            } else {
                return Err(syn::Error::new_spanned(ident, "Unsupported option"));
            }

            if input.parse::<Option<Token![,]>>()?.is_none() {
                break;
            }
        }

        let stream = input.parse::<TokenStream>()?;

        if !stream.is_empty() {
            return Err(syn::Error::new_spanned(stream, "Unexpected input"));
        }

        Ok(out)
    }
}

pub(crate) struct Function {
    attributes: Vec<syn::Attribute>,
    vis: syn::Visibility,
    sig: syn::Signature,
    remainder: TokenStream,
    docs: syn::ExprArray,
    arguments: syn::ExprArray,
    takes_self: bool,
}

impl Function {
    /// Parse the given parse stream.
    pub(crate) fn parse(input: ParseStream) -> syn::Result<Self> {
        let parsed_attributes = input.call(syn::Attribute::parse_outer)?;
        let vis = input.parse::<syn::Visibility>()?;
        let sig = input.parse::<syn::Signature>()?;

        let mut attributes = Vec::new();

        let mut docs = syn::ExprArray {
            attrs: Vec::new(),
            bracket_token: syn::token::Bracket::default(),
            elems: Punctuated::default(),
        };

        for attr in parsed_attributes {
            if attr.path().is_ident("doc") {
                if let syn::Meta::NameValue(name_value) = &attr.meta {
                    docs.elems.push(name_value.value.clone());
                }
            }

            attributes.push(attr);
        }

        let mut arguments = syn::ExprArray {
            attrs: Vec::new(),
            bracket_token: syn::token::Bracket::default(),
            elems: Punctuated::default(),
        };

        let mut takes_self = false;

        for arg in &sig.inputs {
            let argument_name = match arg {
                syn::FnArg::Typed(ty) => argument_ident(&ty.pat),
                syn::FnArg::Receiver(..) => {
                    takes_self = true;
                    syn::LitStr::new("self", arg.span())
                }
            };

            arguments.elems.push(syn::Expr::Lit(syn::ExprLit {
                attrs: Vec::new(),
                lit: syn::Lit::Str(argument_name),
            }));
        }

        let remainder = input.parse::<TokenStream>()?;

        Ok(Self {
            attributes,
            vis,
            sig,
            remainder,
            docs,
            arguments,
            takes_self,
        })
    }

    /// Expand the function declaration.
    pub(crate) fn expand(mut self, attrs: FunctionAttrs) -> syn::Result<TokenStream> {
        let instance = attrs.instance || self.takes_self;

        let (meta_fn, real_fn, mut sig, real_fn_mangled) = if attrs.keep {
            let meta_fn =
                syn::Ident::new(&format!("{}__meta", self.sig.ident), self.sig.ident.span());
            let real_fn = self.sig.ident.clone();
            (meta_fn, real_fn, self.sig.clone(), false)
        } else {
            let meta_fn = self.sig.ident.clone();
            let real_fn = syn::Ident::new(
                &format!("__rune_fn__{}", self.sig.ident),
                self.sig.ident.span(),
            );
            let mut sig = self.sig.clone();
            sig.ident = real_fn.clone();
            (meta_fn, real_fn, sig, true)
        };

        let mut path = syn::Path {
            leading_colon: None,
            segments: Punctuated::default(),
        };

        match (self.takes_self, attrs.free, &attrs.self_type) {
            (true, _, _) => {
                path.segments
                    .push(syn::PathSegment::from(<Token![Self]>::default()));
                path.segments.push(syn::PathSegment::from(real_fn));
            }
            (_, false, Some(self_type)) => {
                path.segments.push(self_type.clone());
                path.segments.push(syn::PathSegment::from(real_fn));
            }
            _ => {
                path.segments.push(syn::PathSegment::from(real_fn));
            }
        }

        let real_fn_path = path;

        let name_string = syn::LitStr::new(&self.sig.ident.to_string(), self.sig.ident.span());

        let name = if instance {
            'out: {
                syn::Expr::Lit(syn::ExprLit {
                    attrs: Vec::new(),
                    lit: syn::Lit::Str(match &attrs.path {
                        Path::Protocol(protocol) => {
                            break 'out syn::parse_quote!(&#protocol);
                        }
                        Path::None => name_string.clone(),
                        Path::Rename(last) => {
                            syn::LitStr::new(&last.ident.to_string(), last.ident.span())
                        }
                    }),
                })
            }
        } else {
            match &attrs.path {
                Path::None => expr_lit(&self.sig.ident),
                Path::Rename(last) => expr_lit(&last.ident),
                Path::Protocol(protocol) => syn::parse_quote!(&#protocol),
            }
        };

        let arguments = match &attrs.path {
            Path::None | Path::Protocol(_) => Punctuated::default(),
            Path::Rename(last) => match &last.arguments {
                syn::PathArguments::AngleBracketed(arguments) => arguments.args.clone(),
                syn::PathArguments::None => Punctuated::default(),
                arguments => {
                    return Err(syn::Error::new_spanned(
                        arguments,
                        "Unsupported path segments",
                    ));
                }
            },
        };

        let name = if !arguments.is_empty() {
            let mut array = syn::ExprArray {
                attrs: Vec::new(),
                bracket_token: <syn::token::Bracket>::default(),
                elems: Punctuated::default(),
            };

            for argument in arguments {
                array.elems.push(syn::Expr::Verbatim(quote! {
                    <#argument as rune::__private::TypeHash>::HASH
                }));
            }

            quote!(rune::__private::Params::new(#name, #array))
        } else {
            quote!(#name)
        };

        if instance {
            // Ensure that the first argument is called `self`.
            if let Some(argument) = self.arguments.elems.first_mut() {
                let span = argument.span();

                *argument = syn::Expr::Lit(syn::ExprLit {
                    attrs: Vec::new(),
                    lit: syn::Lit::Str(syn::LitStr::new("self", span)),
                });
            }
        }

        let meta_kind = syn::Ident::new(
            if instance { "instance" } else { "function" },
            self.sig.span(),
        );

        let mut stream = TokenStream::new();

        for attr in self.attributes {
            stream.extend(attr.into_token_stream());
        }

        if real_fn_mangled {
            stream.extend(quote!(#[allow(non_snake_case)]));
            stream.extend(quote!(#[doc(hidden)]));
        }

        stream.extend(self.vis.to_token_stream());

        let vm_result = VmResult::new();

        if attrs.vm_result {
            let vm_result = &vm_result.vm_result;

            sig.output = match sig.output {
                syn::ReturnType::Default => syn::ReturnType::Type(
                    <Token![->]>::default(),
                    Box::new(syn::Type::Verbatim(quote!(#vm_result<()>))),
                ),
                syn::ReturnType::Type(arrow, ty) => syn::ReturnType::Type(
                    arrow,
                    Box::new(syn::Type::Verbatim(quote!(#vm_result<#ty>))),
                ),
            };
        }

        let generics = sig.generics.clone();
        stream.extend(sig.into_token_stream());

        if attrs.vm_result {
            let mut block: syn::Block = syn::parse2(self.remainder)?;
            vm_result.block(&mut block, true)?;
            block.to_tokens(&mut stream);
        } else {
            stream.extend(self.remainder);
        }

        let arguments = &self.arguments;
        let docs = &self.docs;

        let build_with = if instance {
            None
        } else if let Some(self_type) = &attrs.self_type {
            Some(quote!(.build_associated::<#self_type>()?))
        } else {
            Some(quote!(.build()?))
        };

        let attributes = (!real_fn_mangled).then(|| quote!(#[allow(non_snake_case)]));

        let deprecated = match &attrs.deprecated {
            Some(message) => quote!(Some(#message)),
            None => quote!(None),
        };

        let (impl_generics, type_generics, where_clause) = generics.split_for_impl();
        let type_generics = type_generics.as_turbofish();

        stream.extend(quote! {
            /// Get function metadata.
            #[automatically_derived]
            #attributes
            #[doc(hidden)]
            pub(crate) fn #meta_fn #impl_generics() -> rune::alloc::Result<rune::__private::FunctionMetaData>
            #where_clause
            {
                Ok(rune::__private::FunctionMetaData {
                    kind: rune::__private::FunctionMetaKind::#meta_kind(#name, #real_fn_path #type_generics)?#build_with,
                    statics: rune::__private::FunctionMetaStatics {
                        name: #name_string,
                        deprecated: #deprecated,
                        docs: &#docs[..],
                        arguments: &#arguments[..],
                    },
                })
            }
        });

        Ok(stream)
    }
}

/// The identifier of an argument.
fn argument_ident(pat: &syn::Pat) -> syn::LitStr {
    match pat {
        syn::Pat::Type(pat) => argument_ident(&pat.pat),
        syn::Pat::Path(pat) => argument_path_ident(&pat.path),
        syn::Pat::Ident(pat) => syn::LitStr::new(&pat.ident.to_string(), pat.span()),
        _ => syn::LitStr::new(&pat.to_token_stream().to_string(), pat.span()),
    }
}

/// Argument path identifier.
fn argument_path_ident(path: &syn::Path) -> syn::LitStr {
    match path.get_ident() {
        Some(ident) => syn::LitStr::new(&ident.to_string(), path.span()),
        None => syn::LitStr::new(&path.to_token_stream().to_string(), path.span()),
    }
}

fn expr_lit(ident: &syn::Ident) -> syn::Expr {
    syn::Expr::Lit(syn::ExprLit {
        attrs: Vec::new(),
        lit: syn::Lit::Str(syn::LitStr::new(&ident.to_string(), ident.span())),
    })
}

struct VmResult {
    vm_result: syn::Path,
    from: syn::Path,
    result: syn::Path,
}

impl VmResult {
    fn new() -> Self {
        Self {
            vm_result: syn::parse_quote!(rune::runtime::VmResult),
            from: syn::parse_quote!(core::convert::From),
            result: syn::parse_quote!(core::result::Result),
        }
    }

    /// Modify the block so that it is fallible.
    fn block(&self, ast: &mut syn::Block, top_level: bool) -> syn::Result<()> {
        let vm_result = &self.vm_result;

        for stmt in &mut ast.stmts {
            match stmt {
                syn::Stmt::Expr(expr, _) => {
                    self.expr(expr)?;
                }
                syn::Stmt::Local(local) => {
                    let Some(init) = &mut local.init else {
                        continue;
                    };

                    self.expr(&mut init.expr)?;

                    let Some((_, expr)) = &mut init.diverge else {
                        continue;
                    };

                    self.expr(expr)?;
                }
                _ => {}
            };
        }

        if top_level {
            let mut found = false;

            for stmt in ast.stmts.iter_mut().rev() {
                if let syn::Stmt::Expr(expr, semi) = stmt {
                    if semi.is_none() {
                        found = true;

                        *expr = syn::Expr::Verbatim(quote_spanned! {
                            expr.span() => #vm_result::Ok(#expr)
                        });
                    }

                    break;
                }
            }

            if !found {
                ast.stmts.push(syn::Stmt::Expr(
                    syn::Expr::Verbatim(quote!(#vm_result::Ok(()))),
                    None,
                ));
            }
        }

        Ok(())
    }

    fn expr(&self, ast: &mut syn::Expr) -> syn::Result<()> {
        let Self {
            vm_result,
            from,
            result,
        } = self;

        let outcome = 'outcome: {
            match ast {
                syn::Expr::Array(expr) => {
                    for expr in &mut expr.elems {
                        self.expr(expr)?;
                    }
                }
                syn::Expr::Assign(expt) => {
                    self.expr(&mut expt.right)?;
                }
                syn::Expr::Async(..) => {}
                syn::Expr::Await(expr) => {
                    self.expr(&mut expr.base)?;
                }
                syn::Expr::Binary(expr) => {
                    self.expr(&mut expr.left)?;
                    self.expr(&mut expr.right)?;
                }
                syn::Expr::Block(block) => {
                    self.block(&mut block.block, false)?;
                }
                syn::Expr::Break(expr) => {
                    if let Some(expr) = &mut expr.expr {
                        self.expr(expr)?;
                    }
                }
                syn::Expr::Call(expr) => {
                    self.expr(&mut expr.func)?;

                    for expr in &mut expr.args {
                        self.expr(expr)?;
                    }
                }
                syn::Expr::Field(expr) => {
                    self.expr(&mut expr.base)?;
                }
                syn::Expr::ForLoop(expr) => {
                    self.expr(&mut expr.expr)?;
                    self.block(&mut expr.body, false)?;
                }
                syn::Expr::Group(expr) => {
                    self.expr(&mut expr.expr)?;
                }
                syn::Expr::If(expr) => {
                    self.expr(&mut expr.cond)?;
                    self.block(&mut expr.then_branch, false)?;

                    if let Some((_, expr)) = &mut expr.else_branch {
                        self.expr(expr)?;
                    }
                }
                syn::Expr::Index(expr) => {
                    self.expr(&mut expr.expr)?;
                    self.expr(&mut expr.index)?;
                }
                syn::Expr::Let(expr) => {
                    self.expr(&mut expr.expr)?;
                }
                syn::Expr::Loop(expr) => {
                    self.block(&mut expr.body, false)?;
                }
                syn::Expr::Match(expr) => {
                    self.expr(&mut expr.expr)?;

                    for arm in &mut expr.arms {
                        if let Some((_, expr)) = &mut arm.guard {
                            self.expr(expr)?;
                        }

                        self.expr(&mut arm.body)?;
                    }
                }
                syn::Expr::MethodCall(expr) => {
                    self.expr(&mut expr.receiver)?;

                    for expr in &mut expr.args {
                        self.expr(expr)?;
                    }
                }
                syn::Expr::Paren(expr) => {
                    self.expr(&mut expr.expr)?;
                }
                syn::Expr::Range(expr) => {
                    if let Some(expr) = &mut expr.start {
                        self.expr(expr)?;
                    }

                    if let Some(expr) = &mut expr.end {
                        self.expr(expr)?;
                    }
                }
                syn::Expr::Reference(expr) => {
                    self.expr(&mut expr.expr)?;
                }
                syn::Expr::Repeat(expr) => {
                    self.expr(&mut expr.expr)?;
                    self.expr(&mut expr.len)?;
                }
                syn::Expr::Return(expr) => {
                    if let Some(expr) = &mut expr.expr {
                        self.expr(expr)?;
                    }

                    expr.expr = Some(Box::new(match expr.expr.take() {
                        Some(expr) => syn::Expr::Verbatim(quote_spanned! {
                            expr.span() =>
                            #vm_result::Ok(#expr)
                        }),
                        None => syn::Expr::Verbatim(quote!(#vm_result::Ok(()))),
                    }));
                }
                syn::Expr::Struct(expr) => {
                    for field in &mut expr.fields {
                        self.expr(&mut field.expr)?;
                    }
                }
                syn::Expr::Try(expr) => {
                    let span = expr.span();

                    self.expr(&mut expr.expr)?;

                    break 'outcome if let Some((expr, ident)) = as_vm_expr(&mut expr.expr) {
                        let vm_try = syn::Ident::new("vm_try", ident.span());
                        quote_spanned!(span => rune::#vm_try!(#expr))
                    } else {
                        let value = &mut expr.expr;
                        let from = quote_spanned!(expr.question_token.span() => #from::from);

                        quote_spanned! {
                            span =>
                            match #value {
                                #result::Ok(value) => value,
                                #result::Err(error) => return #vm_result::Ok(#result::Err(#[allow(clippy::useless_conversion)] #from(error))),
                            }
                        }
                    };
                }
                syn::Expr::Tuple(expr) => {
                    for expr in &mut expr.elems {
                        self.expr(expr)?;
                    }
                }
                syn::Expr::Unary(expr) => {
                    self.expr(&mut expr.expr)?;
                }
                syn::Expr::Unsafe(expr) => {
                    self.block(&mut expr.block, false)?;
                }
                syn::Expr::While(expr) => {
                    self.expr(&mut expr.cond)?;
                    self.block(&mut expr.body, false)?;
                }
                syn::Expr::Yield(expr) => {
                    if let Some(expr) = &mut expr.expr {
                        self.expr(expr)?;
                    }
                }
                _ => {}
            }

            return Ok(());
        };

        *ast = syn::Expr::Verbatim(outcome);
        Ok(())
    }
}

/// If this is a field expression like `<expr>.vm`.
fn as_vm_expr(expr: &mut syn::Expr) -> Option<(&mut syn::Expr, &syn::Ident)> {
    let syn::Expr::Field(expr) = expr else {
        return None;
    };

    let syn::Member::Named(ident) = &expr.member else {
        return None;
    };

    (ident == "vm").then_some((&mut expr.base, ident))
}