File size: 2,856 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
use proc_macro::TokenStream;
use quote::quote;
use syn::{ItemFn, parse_macro_input, parse_quote};
use turbo_tasks_macros_shared::{get_native_function_ident, is_self_used};

use crate::func::{
    DefinitionContext, FunctionArguments, NativeFn, TurboFn, filter_inline_attributes,
};

/// This macro generates the virtual function that powers turbo tasks.
/// An annotated task is replaced with a stub function that returns a
/// lazy completion (Vc), and stamps out the concrete implementation
/// of the task alongside that the Vc uses to resolve itself.
///
/// Functions support being tagged for informational purposes. This
/// is currently only used in turbo-static for doing static analysis
/// of tasks.
///
/// # Examples
///
/// ```rust
/// use turbo_tasks::{Vc};
///
/// #[turbo_tasks::function(fs)]
/// async fn my_task() -> Vc<usize> {
///     // access filesystem
/// }
/// ```
pub fn function(args: TokenStream, input: TokenStream) -> TokenStream {
    let mut errors = Vec::new();

    let ItemFn {
        attrs,
        vis,
        sig,
        block,
    } = parse_macro_input!(input as ItemFn);

    let args = syn::parse::<FunctionArguments>(args)
        .inspect_err(|err| errors.push(err.to_compile_error()))
        .unwrap_or_default();
    let local = args.local.is_some();
    let is_self_used = args.operation.is_some() || is_self_used(&block);

    let Some(turbo_fn) = TurboFn::new(&sig, DefinitionContext::NakedFn, args) else {
        return quote! {
            // An error occurred while parsing the function signature.
        }
        .into();
    };

    let ident = &sig.ident;

    let inline_function_ident = turbo_fn.inline_ident();
    let (inline_signature, inline_block) =
        turbo_fn.inline_signature_and_block(&block, is_self_used);
    let inline_attrs = filter_inline_attributes(&attrs[..]);

    let native_fn = NativeFn {
        function_path_string: ident.to_string(),
        function_path: parse_quote! { #inline_function_ident },
        is_method: turbo_fn.is_method(),
        is_self_used,
        filter_trait_call_args: None, // not a trait method
        local,
    };
    let native_function_ident = get_native_function_ident(ident);
    let native_function_ty = native_fn.ty();
    let native_function_def = native_fn.definition();

    let exposed_signature = turbo_fn.signature();
    let exposed_block = turbo_fn.static_block(&native_function_ident);

    quote! {
        #(#attrs)*
        #vis #exposed_signature #exposed_block

        #(#inline_attrs)*
        #[doc(hidden)]
        #inline_signature #inline_block

        #[doc(hidden)]
        pub(crate) static #native_function_ident:
            turbo_tasks::macro_helpers::Lazy<#native_function_ty> =
                turbo_tasks::macro_helpers::Lazy::new(|| #native_function_def);

        #(#errors)*
    }
    .into()
}