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
#![recursion_limit = "128"]
extern crate proc_macro;
extern crate proc_macro2;
extern crate quote;
extern crate syn;

use proc_macro2::{Span, TokenStream};
use quote::quote;
use syn::parse_macro_input;
use syn::visit::Visit;
use syn::{ArgCaptured, Block, FnArg, Ident, Pat, TraitItemMethod, Type, TypeReference};

#[derive(Default)]
struct VisitorImpls {
    tokens: TokenStream,
    count: usize,
}

impl VisitorImpls {
    fn generate_visit(
        &mut self,
        method_name: &Ident,
        arg_pat: &Pat,
        ty: &Type,
        noop: &Option<Block>,
    ) {
        self.tokens.extend(quote! {
            impl MutVisit for #ty {
                fn visit<F: MutVisitor>(&mut self, f: &mut F) {
                    f.#method_name(self)
                }
            }
        });

        let folder_name = format!("Folder{}", self.count);
        let folder_ident = Ident::new(&folder_name, Span::call_site());

        let walk = match noop {
            Some(block) => quote! { #block },
            None => {
                let noop_fn_name = format!("noop_{}", method_name);
                let noop_fn = Ident::new(&noop_fn_name, Span::call_site());
                quote! {
                    syntax::mut_visit::#noop_fn(#arg_pat, self);
                }
            }
        };
        self.tokens.extend(quote! {
            struct #folder_ident<F>
                where F: FnMut(&mut #ty)
            {
                callback: F,
            }

            impl<F> MutVisitor for #folder_ident<F>
                where F: FnMut(&mut #ty)
            {
                fn #method_name(&mut self, #arg_pat: &mut #ty) {
                    #walk
                    (self.callback)(#arg_pat)
                }
            }

            impl MutVisitNodes for #ty {
                fn visit<T, F>(target: &mut T, callback: F)
                    where T: MutVisit,
                          F: FnMut(&mut Self)
                {
                    let mut f = #folder_ident { callback };
                    target.visit(&mut f)
                }
            }
        });

        self.count += 1;
    }

    fn generate_flat_map(
        &mut self,
        method_name: &Ident,
        arg_pat: &Pat,
        ty: &Type,
        noop: &Option<Block>,
    ) {
        self.tokens.extend(quote! {
            impl MutVisit for #ty {
                fn visit<F: MutVisitor>(&mut self, f: &mut F) {
                    let new = f.#method_name(self.clone());
                    *self = new.lone();
                }

                fn flat_map<F: MutVisitor>(self, f: &mut F) -> SmallVec<[#ty; 1]> {
                    f.#method_name(self)
                }
            }
        });

        let folder_name = format!("Folder{}", self.count);
        let folder_ident = Ident::new(&folder_name, Span::call_site());

        let walk = match noop {
            Some(block) => quote! { #block },
            None => {
                let noop_fn_name = format!("noop_{}", method_name);
                let noop_fn = Ident::new(&noop_fn_name, Span::call_site());
                quote! {
                    syntax::mut_visit::#noop_fn(#arg_pat, self);
                }
            }
        };
        self.tokens.extend(quote! {
            struct #folder_ident<F>
                where F: FnMut(#ty) -> SmallVec<[#ty; 1]>
            {
                callback: F,
            }

            impl<F> MutVisitor for #folder_ident<F>
                where F: FnMut(#ty) -> SmallVec<[#ty; 1]>
            {
                fn #method_name(&mut self, #arg_pat: #ty) -> SmallVec<[#ty; 1]> {
                    let mut v = #walk;
                    v.flat_map_in_place(|x| (self.callback)(x));
                    v
                }
            }

            impl FlatMapNodes for #ty {
                fn visit<T, F>(target: &mut T, callback: F)
                    where T: MutVisit,
                          F: FnMut(#ty) -> SmallVec<[#ty; 1]>
                {
                    let mut f = #folder_ident { callback };
                    target.visit(&mut f)
                }

                fn flat_map<T, F>(target: T, callback: F) -> SmallVec<[T; 1]>
                    where T: MutVisit,
                          F: FnMut(#ty) -> SmallVec<[#ty; 1]>
                {
                    let mut f = #folder_ident { callback };
                    target.flat_map(&mut f)
                }
            }
        });

        self.count += 1;
    }
}

impl<'ast> Visit<'ast> for VisitorImpls {
    fn visit_trait_item_method(&mut self, m: &TraitItemMethod) {
        let method_name = &m.sig.ident;
        let method_noop = &m.default;
        match &m.sig.decl.inputs[1] {
            FnArg::Captured(ArgCaptured { pat, ty, .. }) => match ty {
                Type::Reference(TypeReference {
                    mutability: Some(_),
                    elem,
                    ..
                }) => self.generate_visit(method_name, &pat, &elem, method_noop),

                _ => self.generate_flat_map(method_name, &pat, &ty, method_noop),
            },

            _ => {}
        }
    }
}

#[proc_macro]
pub fn gen_visitor_impls(tokens: proc_macro::TokenStream) -> proc_macro::TokenStream {
    let visitor_trait: syn::ItemTrait = parse_macro_input!(tokens);
    let mut visitor = VisitorImpls::default();
    visitor.visit_item_trait(&visitor_trait);

    visitor.tokens.into()
}