|
| 1 | +use proc_macro2::TokenStream; |
| 2 | +use quote::{format_ident, quote, quote_spanned, ToTokens}; |
| 3 | +use syn::spanned::Spanned; |
| 4 | +use syn::{ |
| 5 | + parse_macro_input, parse_quote, Attribute, Data, DeriveInput, Fields, GenericParam, Generics, |
| 6 | + Ident, Index, Lit, Meta, MetaNameValue, NestedMeta, |
| 7 | +}; |
| 8 | + |
| 9 | +/// Implementation of `[#derive(Visit)]` |
| 10 | +#[proc_macro_derive(Visit, attributes(visit))] |
| 11 | +pub fn derive_visit(input: proc_macro::TokenStream) -> proc_macro::TokenStream { |
| 12 | + // Parse the input tokens into a syntax tree. |
| 13 | + let input = parse_macro_input!(input as DeriveInput); |
| 14 | + let name = input.ident; |
| 15 | + |
| 16 | + let attributes = Attributes::parse(&input.attrs); |
| 17 | + // Add a bound `T: HeapSize` to every type parameter T. |
| 18 | + let generics = add_trait_bounds(input.generics); |
| 19 | + let (impl_generics, ty_generics, where_clause) = generics.split_for_impl(); |
| 20 | + |
| 21 | + let (pre_visit, post_visit) = attributes.visit(quote!(self)); |
| 22 | + let children = visit_children(&input.data); |
| 23 | + |
| 24 | + let expanded = quote! { |
| 25 | + // The generated impl. |
| 26 | + impl #impl_generics sqlparser::ast::Visit for #name #ty_generics #where_clause { |
| 27 | + fn visit<V: sqlparser::ast::Visitor>(&self, visitor: &mut V) -> ::std::ops::ControlFlow<V::Break> { |
| 28 | + #pre_visit |
| 29 | + #children |
| 30 | + #post_visit |
| 31 | + ::std::ops::ControlFlow::Continue(()) |
| 32 | + } |
| 33 | + } |
| 34 | + }; |
| 35 | + |
| 36 | + proc_macro::TokenStream::from(expanded) |
| 37 | +} |
| 38 | + |
| 39 | +/// Parses attributes that can be provided to this macro |
| 40 | +/// |
| 41 | +/// `#[visit(leaf, with = "visit_expr")]` |
| 42 | +#[derive(Default)] |
| 43 | +struct Attributes { |
| 44 | + /// Content for the `with` attribute |
| 45 | + with: Option<Ident>, |
| 46 | +} |
| 47 | + |
| 48 | +impl Attributes { |
| 49 | + fn parse(attrs: &[Attribute]) -> Self { |
| 50 | + let mut out = Self::default(); |
| 51 | + for attr in attrs.iter().filter(|a| a.path.is_ident("visit")) { |
| 52 | + let meta = attr.parse_meta().expect("visit attribute"); |
| 53 | + match meta { |
| 54 | + Meta::List(l) => { |
| 55 | + for nested in &l.nested { |
| 56 | + match nested { |
| 57 | + NestedMeta::Meta(Meta::NameValue(v)) => out.parse_name_value(v), |
| 58 | + _ => panic!("Expected #[visit(key = \"value\")]"), |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | + _ => panic!("Expected #[visit(...)]"), |
| 63 | + } |
| 64 | + } |
| 65 | + out |
| 66 | + } |
| 67 | + |
| 68 | + /// Updates self with a name value attribute |
| 69 | + fn parse_name_value(&mut self, v: &MetaNameValue) { |
| 70 | + if v.path.is_ident("with") { |
| 71 | + match &v.lit { |
| 72 | + Lit::Str(s) => self.with = Some(format_ident!("{}", s.value(), span = s.span())), |
| 73 | + _ => panic!("Expected a string value, got {}", v.lit.to_token_stream()), |
| 74 | + } |
| 75 | + return; |
| 76 | + } |
| 77 | + panic!("Unrecognised kv attribute {}", v.path.to_token_stream()) |
| 78 | + } |
| 79 | + |
| 80 | + /// Returns the pre and post visit token streams |
| 81 | + fn visit(&self, s: TokenStream) -> (Option<TokenStream>, Option<TokenStream>) { |
| 82 | + let pre_visit = self.with.as_ref().map(|m| { |
| 83 | + let m = format_ident!("pre_{}", m); |
| 84 | + quote!(visitor.#m(#s)?;) |
| 85 | + }); |
| 86 | + let post_visit = self.with.as_ref().map(|m| { |
| 87 | + let m = format_ident!("post_{}", m); |
| 88 | + quote!(visitor.#m(#s)?;) |
| 89 | + }); |
| 90 | + (pre_visit, post_visit) |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +// Add a bound `T: Visit` to every type parameter T. |
| 95 | +fn add_trait_bounds(mut generics: Generics) -> Generics { |
| 96 | + for param in &mut generics.params { |
| 97 | + if let GenericParam::Type(ref mut type_param) = *param { |
| 98 | + type_param.bounds.push(parse_quote!(sqlparser::ast::Visit)); |
| 99 | + } |
| 100 | + } |
| 101 | + generics |
| 102 | +} |
| 103 | + |
| 104 | +// Generate the body of the visit implementation for the given type |
| 105 | +fn visit_children(data: &Data) -> TokenStream { |
| 106 | + match data { |
| 107 | + Data::Struct(data) => match &data.fields { |
| 108 | + Fields::Named(fields) => { |
| 109 | + let recurse = fields.named.iter().map(|f| { |
| 110 | + let name = &f.ident; |
| 111 | + let attributes = Attributes::parse(&f.attrs); |
| 112 | + let (pre_visit, post_visit) = attributes.visit(quote!(&self.#name)); |
| 113 | + quote_spanned!(f.span() => #pre_visit sqlparser::ast::Visit::visit(&self.#name, visitor)?; #post_visit) |
| 114 | + }); |
| 115 | + quote! { |
| 116 | + #(#recurse)* |
| 117 | + } |
| 118 | + } |
| 119 | + Fields::Unnamed(fields) => { |
| 120 | + let recurse = fields.unnamed.iter().enumerate().map(|(i, f)| { |
| 121 | + let index = Index::from(i); |
| 122 | + let attributes = Attributes::parse(&f.attrs); |
| 123 | + let (pre_visit, post_visit) = attributes.visit(quote!(&self.#index)); |
| 124 | + quote_spanned!(f.span() => #pre_visit sqlparser::ast::Visit::visit(&self.#index, visitor)?; #post_visit) |
| 125 | + }); |
| 126 | + quote! { |
| 127 | + #(#recurse)* |
| 128 | + } |
| 129 | + } |
| 130 | + Fields::Unit => { |
| 131 | + quote!() |
| 132 | + } |
| 133 | + }, |
| 134 | + Data::Enum(data) => { |
| 135 | + let statements = data.variants.iter().map(|v| { |
| 136 | + let name = &v.ident; |
| 137 | + match &v.fields { |
| 138 | + Fields::Named(fields) => { |
| 139 | + let names = fields.named.iter().map(|f| &f.ident); |
| 140 | + let visit = fields.named.iter().map(|f| { |
| 141 | + let name = &f.ident; |
| 142 | + let attributes = Attributes::parse(&f.attrs); |
| 143 | + let (pre_visit, post_visit) = attributes.visit(quote!(&#name)); |
| 144 | + quote_spanned!(f.span() => #pre_visit sqlparser::ast::Visit::visit(#name, visitor)?; #post_visit) |
| 145 | + }); |
| 146 | + |
| 147 | + quote!( |
| 148 | + Self::#name { #(#names),* } => { |
| 149 | + #(#visit)* |
| 150 | + } |
| 151 | + ) |
| 152 | + } |
| 153 | + Fields::Unnamed(fields) => { |
| 154 | + let names = fields.unnamed.iter().enumerate().map(|(i, f)| format_ident!("_{}", i, span = f.span())); |
| 155 | + let visit = fields.unnamed.iter().enumerate().map(|(i, f)| { |
| 156 | + let name = format_ident!("_{}", i); |
| 157 | + let attributes = Attributes::parse(&f.attrs); |
| 158 | + let (pre_visit, post_visit) = attributes.visit(quote!(&#name)); |
| 159 | + quote_spanned!(f.span() => #pre_visit sqlparser::ast::Visit::visit(#name, visitor)?; #post_visit) |
| 160 | + }); |
| 161 | + |
| 162 | + quote! { |
| 163 | + Self::#name ( #(#names),*) => { |
| 164 | + #(#visit)* |
| 165 | + } |
| 166 | + } |
| 167 | + } |
| 168 | + Fields::Unit => { |
| 169 | + quote! { |
| 170 | + Self::#name => {} |
| 171 | + } |
| 172 | + } |
| 173 | + } |
| 174 | + }); |
| 175 | + |
| 176 | + quote! { |
| 177 | + match self { |
| 178 | + #(#statements),* |
| 179 | + } |
| 180 | + } |
| 181 | + } |
| 182 | + Data::Union(_) => unimplemented!(), |
| 183 | + } |
| 184 | +} |
0 commit comments