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
#![feature(proc_macro_diagnostic)]
extern crate proc_macro;
use proc_macro::TokenStream;
use proc_macro2;
use quote::quote;
use syn;
use syn::spanned::Spanned;

/// Turns function into partially applicable functions.
#[proc_macro_attribute]
pub fn part_app(attr: TokenStream, item: TokenStream) -> TokenStream {
    let func_item: syn::Item = syn::parse(item).expect("failed to parse input");
    let attr_options = MacroOptions::from(attr);
    attr_options.check(&func_item);

    match func_item {
        syn::Item::Fn(ref func) => {
            let fn_info = FunctionInformation::from(func);
            fn_info.check();

            // disallow where clauses
            if let Some(w) = &func.sig.generics.where_clause {
                w.span()
                    .unstable()
                    .error("part_app does not allow where clauses")
                    .emit();
            }

            let func_struct = main_struct(&fn_info, &attr_options);
            let generator_func = generator_func(&fn_info, &attr_options);
            let final_call = final_call(&fn_info, &attr_options);
            let argument_calls = argument_calls(&fn_info, &attr_options);

            let unit_structs = {
                let added_unit = fn_info.unit.added;
                let empty_unit = fn_info.unit.empty;
                let vis = fn_info.public;
                quote! {
                    #[allow(non_camel_case_types,non_snake_case)]
                    #vis struct #added_unit;
                    #[allow(non_camel_case_types,non_snake_case)]
                    #vis struct #empty_unit;
                }
            };

            // assemble output
            let mut out = proc_macro2::TokenStream::new();
            out.extend(unit_structs);
            out.extend(func_struct);
            out.extend(generator_func);
            out.extend(argument_calls);
            out.extend(final_call);
            // println!("{}", out);
            TokenStream::from(out)
        }
        _ => {
            func_item
                .span()
                .unstable()
                .error(
                    "Only functions can be partially applied, for structs use the builder pattern",
                )
                .emit();
            proc_macro::TokenStream::from(quote! { #func_item })
        }
    }
}

/// The portion of the signature necessary for each impl block
fn impl_signature<'a>(
    args: &Vec<&syn::PatType>,
    ret_type: &'a syn::ReturnType,
    generics: &Vec<&syn::GenericParam>,
    opts: &MacroOptions,
) -> proc_macro2::TokenStream {
    let arg_names = arg_names(&args);
    let arg_types = arg_types(&args);
    let augmented_names = if !(opts.impl_poly || opts.by_value) {
        augmented_argument_names(&arg_names)
    } else {
        Vec::new()
    };

    quote! {
        #(#generics,)*
        #(#augmented_names: Fn() -> #arg_types,)*
        BODYFN: Fn(#(#arg_types,)*) #ret_type
    }
}

/// Generates the methods used to add argument values to a partially applied function. One method is generate for each
/// argument and each method is contained in it's own impl block.
fn argument_calls<'a>(
    fn_info: &FunctionInformation,
    opts: &MacroOptions,
) -> proc_macro2::TokenStream {
    let impl_sig = impl_signature(
        &fn_info.argument_vec,
        fn_info.ret_type,
        &fn_info.generics,
        opts,
    );
    let arg_name_vec = arg_names(&fn_info.argument_vec);
    let aug_arg_names = augmented_argument_names(&arg_name_vec);
    let arg_types = arg_types(&fn_info.argument_vec);
    arg_names(&fn_info.argument_vec)
        .into_iter()
        .zip(&aug_arg_names)
        .zip(arg_types)
        .map(|((n, n_fn), n_type)| {
            // All variable names except the name of this function
            let free_vars: Vec<_> = arg_name_vec.iter().filter(|&x| x != &n).collect();
            let associated_vals_out: Vec<_> = arg_name_vec
                .iter()
                .map(|x| {
                    if &n == x {
                        fn_info.unit.added.clone()
                    } else {
                        x.clone()
                    }
                })
                .collect();
            let val_list_out = if opts.impl_poly || opts.by_value {
                quote! {#(#associated_vals_out,)*}
            } else {
                quote! {#(#associated_vals_out, #aug_arg_names,)*}
            };
            let associated_vals_in: Vec<_> = associated_vals_out
                .iter()
                .map(|x| {
                    if x == &fn_info.unit.added {
                        &fn_info.unit.empty
                    } else {
                        x
                    }
                })
                .collect();
            let val_list_in = if opts.impl_poly || opts.by_value {
                quote! {#(#associated_vals_in,)*}
            } else {
                quote! {#(#associated_vals_in, #aug_arg_names,)*}
            };
            let (transmute, self_type) = if opts.impl_poly || opts.impl_clone {
                (quote!(transmute), quote!(self))
            } else {
                // if by_value
                (quote!(transmute_copy), quote!(&self))
            };
            let some = if opts.impl_poly {
                quote! {Some(::std::sync::Arc::from(#n))}
            } else {
                // || by_value
                quote! {Some(#n)}
            };
            let in_type = if opts.impl_poly {
                quote! { Box<dyn Fn() -> #n_type> }
            } else if opts.by_value {
                quote! {#n_type}
            } else {
                quote! { #n_fn }
            };
            let struct_name = &fn_info.struct_name;
            let generics = &fn_info.generics;
            let vis = fn_info.public;
            quote! {
                #[allow(non_camel_case_types,non_snake_case)]
                impl< #impl_sig, #(#free_vars,)* > // The impl signature
                    #struct_name<#(#generics,)* #val_list_in BODYFN> // The struct signature
                {
                    #vis fn #n (mut self, #n: #in_type) ->
                        #struct_name<#(#generics,)* #val_list_out BODYFN>{
                        self.#n = #some;
                        unsafe {
                            ::std::mem::#transmute::<
                                #struct_name<#(#generics,)* #val_list_in BODYFN>,
                            #struct_name<#(#generics,)* #val_list_out BODYFN>,
                            >(#self_type)
                        }
                    }
                }
            }
        })
        .collect()
}

/// Generates the call function, which executes a fully filled out partially applicable struct.
fn final_call<'a>(fn_info: &FunctionInformation, opts: &MacroOptions) -> proc_macro2::TokenStream {
    let ret_type = fn_info.ret_type;
    let generics = &fn_info.generics;
    let unit_added = &fn_info.unit.added;
    let struct_name = &fn_info.struct_name;
    let impl_sig = impl_signature(&fn_info.argument_vec, ret_type, generics, opts);
    let arg_names = arg_names(&fn_info.argument_vec);
    let aug_args = augmented_argument_names(&arg_names);
    let vis = fn_info.public;
    let arg_list: proc_macro2::TokenStream = if opts.impl_poly || opts.by_value {
        aug_args.iter().map(|_| quote! {#unit_added,}).collect()
    } else {
        aug_args.iter().map(|a| quote! {#unit_added, #a,}).collect()
    };
    let call = if !opts.by_value {
        quote! {()}
    } else {
        quote! {}
    };
    quote! {
        #[allow(non_camel_case_types,non_snake_case)]
        impl <#impl_sig> // impl signature
            // struct signature
            #struct_name<#(#generics,)* #arg_list BODYFN>
        {
            #vis fn call(self) #ret_type { // final call
                (self.body)(#(self.#arg_names.unwrap()#call,)*)
            }
        }
    }
}

/// The function called by the user to create an instance of a partially applicable function. This function always has
/// the name of the original function the macro is called on.
fn generator_func<'a>(
    fn_info: &FunctionInformation,
    opts: &MacroOptions,
) -> proc_macro2::TokenStream {
    // because the quote! macro cannot expand fields
    let arg_names = arg_names(&fn_info.argument_vec);
    let arg_types = arg_types(&fn_info.argument_vec);
    let marker_names = marker_names(&arg_names);
    let generics = &fn_info.generics;
    let empty_unit = &fn_info.unit.empty;
    let body = fn_info.block;
    let name = fn_info.fn_name;
    let struct_name = &fn_info.struct_name;
    let ret_type = fn_info.ret_type;
    let vis = fn_info.public;

    let gen_types = if opts.impl_poly || opts.by_value {
        quote! {#(#generics,)*}
    } else {
        quote! {#(#generics,)* #(#arg_names,)*}
    };
    let struct_types = if opts.impl_poly || opts.by_value {
        arg_names.iter().map(|_| quote! {#empty_unit,}).collect()
    } else {
        quote! {#(#empty_unit,#arg_names,)*}
    };
    let body_fn = if opts.impl_poly || opts.impl_clone {
        quote! {::std::sync::Arc::new(|#(#arg_names,)*| #body),}
    } else {
        quote! {|#(#arg_names,)*| #body,}
    };
    let where_clause = if opts.impl_poly || opts.by_value {
        quote!()
    } else {
        quote! {
            where
                #(#arg_names: Fn() -> #arg_types,)*
        }
    };
    quote! {
        #[allow(non_camel_case_types,non_snake_case)]
        #vis fn #name<#gen_types>() -> #struct_name<#(#generics,)* #struct_types
        impl Fn(#(#arg_types,)*) #ret_type>
            #where_clause
        {
            #struct_name {
                #(#arg_names: None,)*
                #(#marker_names: ::std::marker::PhantomData,)*
                body: #body_fn
            }
        }

    }
}

/// A vector of all argument names. Simple parsing.
fn arg_names<'a>(args: &Vec<&syn::PatType>) -> Vec<syn::Ident> {
    args.iter()
        .map(|f| {
            let f_pat = &f.pat;
            syn::Ident::new(&format!("{}", quote!(#f_pat)), f.span())
        })
        .collect()
}

/// The vector of names used to hold PhantomData.
fn marker_names(names: &Vec<syn::Ident>) -> Vec<syn::Ident> {
    names.iter().map(|f| concat_ident(f, "m")).collect()
}

/// Concatenates a identity with a string, returning a new identity with the same span.
fn concat_ident<'a>(ident: &'a syn::Ident, end: &str) -> syn::Ident {
    let name = format!("{}___{}", quote! {#ident}, end);
    syn::Ident::new(&name, ident.span())
}

/// Filter an argument list to a pattern type
fn argument_vector<'a>(
    args: &'a syn::punctuated::Punctuated<syn::FnArg, syn::token::Comma>,
) -> Vec<&syn::PatType> {
    args.iter()
        .map(|fn_arg| match fn_arg {
            syn::FnArg::Receiver(_) => panic!("should filter out reciever arguments"),
            syn::FnArg::Typed(t) => {
                if let syn::Type::Reference(r) = t.ty.as_ref() {
                    if r.lifetime.is_none() {
                        t.span()
                            .unstable()
                            .error("part_app does not support lifetime elision")
                            .emit();
                    }
                }

                t
            }
        })
        .collect()
}

/// Retrieves the identities of an the argument list
fn arg_types<'a>(args: &Vec<&'a syn::PatType>) -> Vec<&'a syn::Type> {
    args.iter().map(|f| f.ty.as_ref()).collect()
}

/// Names to hold function types
fn augmented_argument_names<'a>(arg_names: &Vec<syn::Ident>) -> Vec<syn::Ident> {
    arg_names.iter().map(|f| concat_ident(f, "FN")).collect()
}

/// Generates the main struct for the partially applicable function.
/// All other functions are methods on this struct.
fn main_struct<'a>(fn_info: &FunctionInformation, opts: &MacroOptions) -> proc_macro2::TokenStream {
    let arg_types = arg_types(&fn_info.argument_vec);

    let arg_names = arg_names(&fn_info.argument_vec);
    let arg_augmented = augmented_argument_names(&arg_names);
    let ret_type = fn_info.ret_type;

    let arg_list: Vec<_> = if !(opts.impl_poly || opts.by_value) {
        arg_names
            .iter()
            .zip(arg_augmented.iter())
            .flat_map(|(n, a)| vec![n, a])
            .collect()
    } else {
        arg_names.iter().collect()
    };
    let bodyfn = if opts.impl_poly || opts.impl_clone {
        quote! {::std::sync::Arc<BODYFN>}
    } else {
        quote! { BODYFN }
    };
    let where_clause = if opts.impl_poly || opts.by_value {
        quote!(BODYFN: Fn(#(#arg_types,)*) #ret_type,)
    } else {
        quote! {
            #(#arg_augmented: Fn() -> #arg_types,)*
            BODYFN: Fn(#(#arg_types,)*) #ret_type,
        }
    };
    let names_with_m = marker_names(&arg_names);
    let option_list = if opts.impl_poly {
        quote! {#(#arg_names: Option<::std::sync::Arc<dyn Fn() -> #arg_types>>,)*}
    } else if opts.by_value {
        quote! {#(#arg_names: Option<#arg_types>,)*}
    } else {
        quote! {#(#arg_names: Option<#arg_augmented>,)*}
    };
    let name = &fn_info.struct_name;

    let clone = if opts.impl_clone {
        let sig = impl_signature(
            &fn_info.argument_vec,
            fn_info.ret_type,
            &fn_info.generics,
            opts,
        );
        quote! {
            #[allow(non_camel_case_types,non_snake_case)]
            impl<#sig, #(#arg_list,)*> ::std::clone::Clone for #name <#(#arg_list,)* BODYFN>
            where #where_clause
            {
                fn clone(&self) -> Self {
                    Self {
                        #(#names_with_m: ::std::marker::PhantomData,)*
                        #(#arg_names: self.#arg_names.clone(),)*
                        body: self.body.clone(),
                    }
                }
            }
        }
    } else {
        quote! {}
    };
    let generics = &fn_info.generics;
    let vis = fn_info.public;
    quote! {
        #[allow(non_camel_case_types,non_snake_case)]
        #vis struct #name <#(#generics,)* #(#arg_list,)*BODYFN>
        where #where_clause
        {
            // These hold the (phantom) types which represent if a field has
            // been filled
            #(#names_with_m: ::std::marker::PhantomData<#arg_names>,)*
            // These hold the closures representing each argument
            #option_list
            // This holds the executable function
            body: #bodyfn,
        }

        #clone
    }
}

/// Contains options used to customize output
struct MacroOptions {
    attr: proc_macro::TokenStream,
    by_value: bool,
    impl_clone: bool,
    impl_poly: bool,
}

impl MacroOptions {
    fn new(attr: proc_macro::TokenStream) -> Self {
        Self {
            attr,
            by_value: false,
            impl_clone: false,
            impl_poly: false,
        }
    }
    fn from(attr: proc_macro::TokenStream) -> Self {
        let attributes: Vec<String> = attr
            .to_string()
            .split(",")
            .map(|s| s.trim().to_string())
            .collect();
        let mut attr_options = MacroOptions::new(attr);
        attr_options.impl_poly = attributes.contains(&"poly".to_string());
        attr_options.by_value = attributes.contains(&"value".to_string());
        attr_options.impl_clone = attributes.contains(&"Clone".to_string());
        attr_options
    }
    fn check(&self, span: &syn::Item) {
        if self.impl_poly && self.by_value {
            span.span()
                .unstable()
                .error(r#"Cannot implement "poly" and "value" at the same time"#)
                .emit()
        }

        if self.impl_clone && !(self.impl_poly || self.by_value) {
            span.span()
                .unstable()
                .error(r#"Cannot implement "Clone" without "poly" or "value""#)
                .emit()
        }
        if !self.attr.is_empty() && !self.impl_poly && !self.by_value && !self.impl_clone {
            span.span()
                .unstable()
                .error(
                    r#"Unknown attribute. Acceptable attributes are "poly", "Clone" and "value""#,
                )
                .emit()
        }
    }
}

/// Contains prepossesses information about
struct FunctionInformation<'a> {
    argument_vec: Vec<&'a syn::PatType>,
    ret_type: &'a syn::ReturnType,
    generics: Vec<&'a syn::GenericParam>,
    fn_name: &'a syn::Ident,
    struct_name: syn::Ident,
    unit: Units,
    block: &'a syn::Block,
    public: &'a syn::Visibility,
    orignal_fn: &'a syn::ItemFn,
}

/// Contains Idents for the unit structs
struct Units {
    added: syn::Ident,
    empty: syn::Ident,
}

impl<'a> FunctionInformation<'a> {
    fn from(func: &'a syn::ItemFn) -> Self {
        let func_name = &func.sig.ident;
        Self {
            argument_vec: argument_vector(&func.sig.inputs),
            ret_type: &func.sig.output,
            generics: func.sig.generics.params.iter().map(|f| f).collect(),
            fn_name: func_name,
            struct_name: syn::Ident::new(
                &format!("__PartialApplication__{}_", func_name),
                func_name.span(),
            ),
            unit: Units {
                added: concat_ident(func_name, "Added"),
                empty: concat_ident(func_name, "Empty"),
            },
            block: &func.block,
            public: &func.vis,
            orignal_fn: func,
        }
    }
    fn check(&self) {
        if let Some(r) = self.orignal_fn.sig.receiver() {
            r.span()
                .unstable()
                .error("Cannot make methods partially applicable yet")
                .emit();
        }
    }
}