|
|
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, |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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! { |
|
|
|
|
|
} |
|
|
.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, |
|
|
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() |
|
|
} |
|
|
|