|
| 1 | +// Copyright (c) 2022-present INESC-ID. |
| 2 | +// Distributed under the MIT license that can be found in the LICENSE file. |
| 3 | + |
| 4 | +use proc_macro::TokenStream; |
| 5 | +use quote::quote; |
| 6 | +use syn::parse::{Parse, ParseStream}; |
| 7 | +use syn::{Block, Error, ExprClosure, Pat, Result, Stmt, Token, parse_macro_input}; |
| 8 | + |
| 9 | +struct Lambda { |
| 10 | + captures: Block, |
| 11 | + closure: ExprClosure, |
| 12 | +} |
| 13 | + |
| 14 | +impl Parse for Lambda { |
| 15 | + fn parse(input: ParseStream) -> Result<Self> { |
| 16 | + let captures = input.parse()?; |
| 17 | + input.parse::<Token![,]>()?; |
| 18 | + let closure = input.parse()?; |
| 19 | + input.parse::<Option<Token![,]>>()?; |
| 20 | + Ok(Lambda { captures, closure }) |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +fn expand_lambda(lambda: Lambda, is_unsafe: bool) -> Result<proc_macro2::TokenStream> { |
| 25 | + let mut names = Vec::new(); |
| 26 | + let mut types = Vec::new(); |
| 27 | + let mut inits = Vec::new(); |
| 28 | + for stmt in &lambda.captures.stmts { |
| 29 | + let Stmt::Local(local) = stmt else { |
| 30 | + return Err(Error::new_spanned(stmt, "expected a `let` for a capture")); |
| 31 | + }; |
| 32 | + let (Pat::Type(pat), Some(init)) = (&local.pat, &local.init) else { |
| 33 | + return Err(Error::new_spanned( |
| 34 | + local, |
| 35 | + "expected `let <name>: <type> = <init>;` for a capture", |
| 36 | + )); |
| 37 | + }; |
| 38 | + names.push(&pat.pat); |
| 39 | + types.push(&pat.ty); |
| 40 | + inits.push(&init.expr); |
| 41 | + } |
| 42 | + |
| 43 | + let mut params = Vec::new(); |
| 44 | + let mut param_types = Vec::new(); |
| 45 | + for input in &lambda.closure.inputs { |
| 46 | + let Pat::Type(pat) = input else { |
| 47 | + return Err(Error::new_spanned(input, "expected a typed parameter")); |
| 48 | + }; |
| 49 | + params.push(pat); |
| 50 | + param_types.push(&pat.ty); |
| 51 | + } |
| 52 | + |
| 53 | + let output = &lambda.closure.output; |
| 54 | + let body = &lambda.closure.body; |
| 55 | + let (receiver, body, constructor) = if is_unsafe { |
| 56 | + ( |
| 57 | + quote! { &mut self }, |
| 58 | + quote! { unsafe { #body } }, |
| 59 | + quote! { from_lambda_unsafe }, |
| 60 | + ) |
| 61 | + } else { |
| 62 | + (quote! { &self }, quote! { #body }, quote! { from_lambda }) |
| 63 | + }; |
| 64 | + |
| 65 | + Ok(quote! {{ |
| 66 | + struct __Lambda { |
| 67 | + #(#names: #types,)* |
| 68 | + } |
| 69 | + impl __Lambda { |
| 70 | + #[allow(unused_unsafe, clippy::too_many_arguments)] |
| 71 | + fn call(#receiver #(, #params)*) #output { |
| 72 | + #body |
| 73 | + } |
| 74 | + } |
| 75 | + ::libcc2rs::FnPtr::<fn(#(#param_types),*) #output>::#constructor( |
| 76 | + __Lambda { #(#names: #inits,)* }, |
| 77 | + __Lambda::call, |
| 78 | + ) |
| 79 | + }}) |
| 80 | +} |
| 81 | + |
| 82 | +pub fn expand(input: TokenStream, is_unsafe: bool) -> TokenStream { |
| 83 | + let lambda = parse_macro_input!(input as Lambda); |
| 84 | + expand_lambda(lambda, is_unsafe) |
| 85 | + .unwrap_or_else(Error::into_compile_error) |
| 86 | + .into() |
| 87 | +} |
0 commit comments