Carl Lerche | 058ff47 | 2019-02-13 16:23:52 -0800 | [diff] [blame] | 1 | //! This crate automatically generates the definition of the `Visit`, |
| 2 | //! `VisitMut`, and `Fold` traits in `syn` based on the `syn` source. It |
| 3 | //! discovers structs and enums declared with the `ast_*` macros and generates |
| 4 | //! the functions for those types. |
| 5 | //! |
| 6 | //! It makes a few assumptions about the target crate: |
| 7 | //! 1. All structs which are discovered must be re-exported in the root of the |
| 8 | //! crate, even if they were declared in a submodule. |
| 9 | //! 2. This code cannot discover submodules which are located in subdirectories |
| 10 | //! - only submodules located in the same directory. |
| 11 | //! 3. The path to `syn` is hardcoded. |
| 12 | |
| 13 | use crate::types; |
| 14 | use proc_macro2::TokenStream; |
| 15 | |
| 16 | use std::fs::File; |
| 17 | use std::io::Write; |
| 18 | |
| 19 | const FOLD_SRC: &str = "../src/gen/fold.rs"; |
| 20 | const VISIT_SRC: &str = "../src/gen/visit.rs"; |
| 21 | const VISIT_MUT_SRC: &str = "../src/gen/visit_mut.rs"; |
| 22 | |
| 23 | mod codegen { |
| 24 | use crate::types; |
| 25 | use inflections::Inflect; |
| 26 | use proc_macro2::{Span, TokenStream}; |
| 27 | use quote::TokenStreamExt; |
| 28 | use syn::*; |
| 29 | |
| 30 | #[derive(Default)] |
| 31 | pub struct State { |
| 32 | pub visit_trait: TokenStream, |
| 33 | pub visit_impl: TokenStream, |
| 34 | pub visit_mut_trait: TokenStream, |
| 35 | pub visit_mut_impl: TokenStream, |
| 36 | pub fold_trait: TokenStream, |
| 37 | pub fold_impl: TokenStream, |
| 38 | } |
| 39 | |
| 40 | fn under_name(name: &str) -> Ident { |
| 41 | Ident::new(&name.to_snake_case(), Span::call_site()) |
| 42 | } |
| 43 | |
| 44 | #[derive(Debug, Eq, PartialEq, Copy, Clone)] |
| 45 | enum Kind { |
| 46 | Visit, |
| 47 | VisitMut, |
| 48 | Fold, |
| 49 | } |
| 50 | |
| 51 | enum Operand { |
| 52 | Borrowed(TokenStream), |
| 53 | Owned(TokenStream), |
| 54 | } |
| 55 | |
| 56 | use self::Kind::*; |
| 57 | use self::Operand::*; |
| 58 | |
| 59 | impl Operand { |
| 60 | fn tokens(&self) -> &TokenStream { |
| 61 | match *self { |
| 62 | Borrowed(ref n) | Owned(ref n) => n, |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | fn ref_tokens(&self) -> TokenStream { |
| 67 | match *self { |
| 68 | Borrowed(ref n) => n.clone(), |
| 69 | Owned(ref n) => quote!(&#n), |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | fn ref_mut_tokens(&self) -> TokenStream { |
| 74 | match *self { |
| 75 | Borrowed(ref n) => n.clone(), |
| 76 | Owned(ref n) => quote!(&mut #n), |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | fn owned_tokens(&self) -> TokenStream { |
| 81 | match *self { |
| 82 | Borrowed(ref n) => quote!(*#n), |
| 83 | Owned(ref n) => n.clone(), |
| 84 | } |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | fn simple_visit(item: &str, kind: Kind, name: &Operand) -> TokenStream { |
| 89 | let ident = under_name(item); |
| 90 | |
| 91 | match kind { |
| 92 | Visit => { |
| 93 | let method = Ident::new(&format!("visit_{}", ident), Span::call_site()); |
| 94 | let name = name.ref_tokens(); |
| 95 | quote! { |
| 96 | _visitor.#method(#name) |
| 97 | } |
| 98 | } |
| 99 | VisitMut => { |
| 100 | let method = Ident::new(&format!("visit_{}_mut", ident), Span::call_site()); |
| 101 | let name = name.ref_mut_tokens(); |
| 102 | quote! { |
| 103 | _visitor.#method(#name) |
| 104 | } |
| 105 | } |
| 106 | Fold => { |
| 107 | let method = Ident::new(&format!("fold_{}", ident), Span::call_site()); |
| 108 | let name = name.owned_tokens(); |
| 109 | quote! { |
| 110 | _visitor.#method(#name) |
| 111 | } |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | fn box_visit( |
| 117 | elem: &types::Type, |
| 118 | features: &types::Features, |
David Tolnay | f9bb8ff | 2019-02-15 13:10:14 -0800 | [diff] [blame^] | 119 | types: &[types::Node], |
Carl Lerche | 058ff47 | 2019-02-13 16:23:52 -0800 | [diff] [blame] | 120 | kind: Kind, |
| 121 | name: &Operand, |
| 122 | ) -> Option<TokenStream> { |
| 123 | let name = name.owned_tokens(); |
| 124 | let res = visit(elem, features, types, kind, &Owned(quote!(*#name)))?; |
| 125 | Some(match kind { |
| 126 | Fold => quote! { |
| 127 | Box::new(#res) |
| 128 | }, |
| 129 | Visit | VisitMut => res, |
| 130 | }) |
| 131 | } |
| 132 | |
| 133 | fn vec_visit( |
| 134 | elem: &types::Type, |
| 135 | features: &types::Features, |
David Tolnay | f9bb8ff | 2019-02-15 13:10:14 -0800 | [diff] [blame^] | 136 | types: &[types::Node], |
Carl Lerche | 058ff47 | 2019-02-13 16:23:52 -0800 | [diff] [blame] | 137 | kind: Kind, |
| 138 | name: &Operand, |
| 139 | ) -> Option<TokenStream> { |
| 140 | let operand = match kind { |
| 141 | Visit | VisitMut => Borrowed(quote!(it)), |
| 142 | Fold => Owned(quote!(it)), |
| 143 | }; |
| 144 | let val = visit(elem, features, types, kind, &operand)?; |
| 145 | Some(match kind { |
| 146 | Visit => { |
| 147 | let name = name.ref_tokens(); |
| 148 | quote! { |
| 149 | for it in #name { |
| 150 | #val |
| 151 | } |
| 152 | } |
| 153 | } |
| 154 | VisitMut => { |
| 155 | let name = name.ref_mut_tokens(); |
| 156 | quote! { |
| 157 | for it in #name { |
| 158 | #val |
| 159 | } |
| 160 | } |
| 161 | } |
| 162 | Fold => { |
| 163 | let name = name.owned_tokens(); |
| 164 | quote! { |
| 165 | FoldHelper::lift(#name, |it| { #val }) |
| 166 | } |
| 167 | } |
| 168 | }) |
| 169 | } |
| 170 | |
| 171 | fn punctuated_visit( |
| 172 | elem: &types::Type, |
| 173 | features: &types::Features, |
David Tolnay | f9bb8ff | 2019-02-15 13:10:14 -0800 | [diff] [blame^] | 174 | types: &[types::Node], |
Carl Lerche | 058ff47 | 2019-02-13 16:23:52 -0800 | [diff] [blame] | 175 | kind: Kind, |
| 176 | name: &Operand, |
| 177 | ) -> Option<TokenStream> { |
| 178 | let operand = match kind { |
| 179 | Visit | VisitMut => Borrowed(quote!(it)), |
| 180 | Fold => Owned(quote!(it)), |
| 181 | }; |
| 182 | let val = visit(elem, features, types, kind, &operand)?; |
| 183 | Some(match kind { |
| 184 | Visit => { |
| 185 | let name = name.ref_tokens(); |
| 186 | quote! { |
| 187 | for el in Punctuated::pairs(#name) { |
| 188 | let it = el.value(); |
| 189 | #val |
| 190 | } |
| 191 | } |
| 192 | } |
| 193 | VisitMut => { |
| 194 | let name = name.ref_mut_tokens(); |
| 195 | quote! { |
| 196 | for mut el in Punctuated::pairs_mut(#name) { |
| 197 | let it = el.value_mut(); |
| 198 | #val |
| 199 | } |
| 200 | } |
| 201 | } |
| 202 | Fold => { |
| 203 | let name = name.owned_tokens(); |
| 204 | quote! { |
| 205 | FoldHelper::lift(#name, |it| { #val }) |
| 206 | } |
| 207 | } |
| 208 | }) |
| 209 | } |
| 210 | |
| 211 | fn option_visit( |
| 212 | elem: &types::Type, |
| 213 | features: &types::Features, |
David Tolnay | f9bb8ff | 2019-02-15 13:10:14 -0800 | [diff] [blame^] | 214 | types: &[types::Node], |
Carl Lerche | 058ff47 | 2019-02-13 16:23:52 -0800 | [diff] [blame] | 215 | kind: Kind, |
| 216 | name: &Operand, |
| 217 | ) -> Option<TokenStream> { |
| 218 | let it = match kind { |
| 219 | Visit | VisitMut => Borrowed(quote!(it)), |
| 220 | Fold => Owned(quote!(it)), |
| 221 | }; |
| 222 | let val = visit(elem, features, types, kind, &it)?; |
| 223 | let name = name.owned_tokens(); |
| 224 | Some(match kind { |
| 225 | Visit => quote! { |
| 226 | if let Some(ref it) = #name { |
| 227 | #val |
| 228 | } |
| 229 | }, |
| 230 | VisitMut => quote! { |
| 231 | if let Some(ref mut it) = #name { |
| 232 | #val |
| 233 | } |
| 234 | }, |
| 235 | Fold => quote! { |
| 236 | (#name).map(|it| { #val }) |
| 237 | }, |
| 238 | }) |
| 239 | } |
| 240 | |
| 241 | fn tuple_visit( |
| 242 | elems: &[types::Type], |
| 243 | features: &types::Features, |
David Tolnay | f9bb8ff | 2019-02-15 13:10:14 -0800 | [diff] [blame^] | 244 | types: &[types::Node], |
Carl Lerche | 058ff47 | 2019-02-13 16:23:52 -0800 | [diff] [blame] | 245 | kind: Kind, |
| 246 | name: &Operand, |
| 247 | ) -> Option<TokenStream> { |
| 248 | if elems.is_empty() { |
| 249 | return None; |
| 250 | } |
| 251 | |
| 252 | let mut code = TokenStream::new(); |
| 253 | for (i, elem) in elems.iter().enumerate() { |
| 254 | let name = name.tokens(); |
| 255 | let i = Index::from(i); |
| 256 | let it = Owned(quote!((#name).#i)); |
| 257 | let val = |
| 258 | visit(elem, features, types, kind, &it).unwrap_or_else(|| noop_visit(kind, &it)); |
| 259 | code.append_all(val); |
| 260 | match kind { |
| 261 | Fold => code.append_all(quote!(,)), |
| 262 | Visit | VisitMut => code.append_all(quote!(;)), |
| 263 | } |
| 264 | } |
| 265 | Some(match kind { |
| 266 | Fold => quote! { |
| 267 | (#code) |
| 268 | }, |
| 269 | Visit | VisitMut => code, |
| 270 | }) |
| 271 | } |
| 272 | |
| 273 | fn token_punct_visit(token: &types::Token, kind: Kind, name: &Operand) -> TokenStream { |
| 274 | let ty: TokenStream = syn::parse_str(&format!("Token![{}]", token.repr())).unwrap(); |
| 275 | let name = name.tokens(); |
| 276 | match kind { |
| 277 | Fold => quote! { |
| 278 | #ty(tokens_helper(_visitor, &#name.spans)) |
| 279 | }, |
| 280 | Visit => quote! { |
| 281 | tokens_helper(_visitor, &#name.spans) |
| 282 | }, |
| 283 | VisitMut => quote! { |
| 284 | tokens_helper(_visitor, &mut #name.spans) |
| 285 | }, |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | fn token_keyword_visit(token: &types::Token, kind: Kind, name: &Operand) -> TokenStream { |
| 290 | let ty: TokenStream = syn::parse_str(&format!("Token![{}]", token.repr())).unwrap(); |
| 291 | let name = name.tokens(); |
| 292 | match kind { |
| 293 | Fold => quote! { |
| 294 | #ty(tokens_helper(_visitor, &#name.span)) |
| 295 | }, |
| 296 | Visit => quote! { |
| 297 | tokens_helper(_visitor, &#name.span) |
| 298 | }, |
| 299 | VisitMut => quote! { |
| 300 | tokens_helper(_visitor, &mut #name.span) |
| 301 | }, |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | fn token_group_visit(ty: &str, kind: Kind, name: &Operand) -> TokenStream { |
| 306 | let ty = Ident::new(ty, Span::call_site()); |
| 307 | let name = name.tokens(); |
| 308 | match kind { |
| 309 | Fold => quote! { |
| 310 | #ty(tokens_helper(_visitor, &#name.span)) |
| 311 | }, |
| 312 | Visit => quote! { |
| 313 | tokens_helper(_visitor, &#name.span) |
| 314 | }, |
| 315 | VisitMut => quote! { |
| 316 | tokens_helper(_visitor, &mut #name.span) |
| 317 | }, |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | fn noop_visit(kind: Kind, name: &Operand) -> TokenStream { |
| 322 | match kind { |
| 323 | Fold => name.owned_tokens(), |
| 324 | Visit | VisitMut => { |
| 325 | let name = name.tokens(); |
| 326 | quote! { |
| 327 | skip!(#name) |
| 328 | } |
| 329 | } |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | fn visit( |
| 334 | ty: &types::Type, |
| 335 | features: &types::Features, |
David Tolnay | f9bb8ff | 2019-02-15 13:10:14 -0800 | [diff] [blame^] | 336 | types: &[types::Node], |
Carl Lerche | 058ff47 | 2019-02-13 16:23:52 -0800 | [diff] [blame] | 337 | kind: Kind, |
| 338 | name: &Operand, |
| 339 | ) -> Option<TokenStream> { |
| 340 | match ty { |
| 341 | types::Type::Box(t) => box_visit(&*t, features, types, kind, name), |
| 342 | types::Type::Vec(t) => vec_visit(&*t, features, types, kind, name), |
David Tolnay | 295141b | 2019-02-15 12:45:33 -0800 | [diff] [blame] | 343 | types::Type::Punctuated(p) => { |
| 344 | punctuated_visit(p.element(), features, types, kind, name) |
| 345 | } |
Carl Lerche | 058ff47 | 2019-02-13 16:23:52 -0800 | [diff] [blame] | 346 | types::Type::Option(t) => option_visit(&*t, features, types, kind, name), |
| 347 | types::Type::Tuple(t) => tuple_visit(t, features, types, kind, name), |
| 348 | types::Type::Token(t) => { |
| 349 | if t.is_keyword() { |
| 350 | Some(token_keyword_visit(t, kind, name)) |
| 351 | } else { |
| 352 | Some(token_punct_visit(t, kind, name)) |
| 353 | } |
| 354 | } |
David Tolnay | 295141b | 2019-02-15 12:45:33 -0800 | [diff] [blame] | 355 | types::Type::Group(t) => Some(token_group_visit(&t[..], kind, name)), |
Carl Lerche | 058ff47 | 2019-02-13 16:23:52 -0800 | [diff] [blame] | 356 | types::Type::Item(t) => { |
| 357 | fn requires_full(features: &types::Features) -> bool { |
| 358 | features.contains("full") && features.len() == 1 |
| 359 | } |
| 360 | |
| 361 | let mut res = simple_visit(t, kind, name); |
| 362 | |
| 363 | let target = types.iter().find(|ty| ty.ident() == t).unwrap(); |
| 364 | |
| 365 | Some( |
| 366 | if requires_full(target.features()) && !requires_full(features) { |
| 367 | quote! { |
| 368 | full!(#res) |
| 369 | } |
| 370 | } else { |
| 371 | res |
| 372 | }, |
| 373 | ) |
| 374 | } |
| 375 | types::Type::Ext(_) | types::Type::Std(_) => None, |
| 376 | } |
| 377 | } |
| 378 | |
| 379 | fn visit_features(features: &types::Features) -> TokenStream { |
| 380 | match features.len() { |
| 381 | 0 => quote!(), |
| 382 | 1 => { |
| 383 | let feature = &features[0]; |
| 384 | quote!(#[cfg(feature = #feature)]) |
| 385 | } |
| 386 | _ => { |
| 387 | let features = features.iter().map(|feature| quote!(feature = #feature)); |
| 388 | |
| 389 | quote!(#[cfg(any( #(#features),* ))]) |
| 390 | } |
| 391 | } |
| 392 | } |
| 393 | |
David Tolnay | f9bb8ff | 2019-02-15 13:10:14 -0800 | [diff] [blame^] | 394 | pub fn generate(state: &mut State, s: &types::Node, types: &[types::Node]) { |
Carl Lerche | 058ff47 | 2019-02-13 16:23:52 -0800 | [diff] [blame] | 395 | let features = visit_features(s.features()); |
| 396 | let under_name = under_name(s.ident()); |
| 397 | let ty = Ident::new(s.ident(), Span::call_site()); |
| 398 | let visit_fn = Ident::new(&format!("visit_{}", under_name), Span::call_site()); |
| 399 | let visit_mut_fn = Ident::new(&format!("visit_{}_mut", under_name), Span::call_site()); |
| 400 | let fold_fn = Ident::new(&format!("fold_{}", under_name), Span::call_site()); |
| 401 | |
| 402 | let mut visit_impl = TokenStream::new(); |
| 403 | let mut visit_mut_impl = TokenStream::new(); |
| 404 | let mut fold_impl = TokenStream::new(); |
| 405 | |
| 406 | match s { |
David Tolnay | f9bb8ff | 2019-02-15 13:10:14 -0800 | [diff] [blame^] | 407 | types::Node::Enum(ref e) => { |
Carl Lerche | 058ff47 | 2019-02-13 16:23:52 -0800 | [diff] [blame] | 408 | let mut visit_variants = TokenStream::new(); |
| 409 | let mut visit_mut_variants = TokenStream::new(); |
| 410 | let mut fold_variants = TokenStream::new(); |
| 411 | |
| 412 | for variant in e.variants() { |
| 413 | let variant_ident = Ident::new(variant.ident(), Span::call_site()); |
| 414 | |
| 415 | if variant.fields().is_empty() { |
| 416 | visit_variants.append_all(quote! { |
| 417 | #ty::#variant_ident => {} |
| 418 | }); |
| 419 | visit_mut_variants.append_all(quote! { |
| 420 | #ty::#variant_ident => {} |
| 421 | }); |
| 422 | fold_variants.append_all(quote! { |
| 423 | #ty::#variant_ident => { |
| 424 | #ty::#variant_ident |
| 425 | } |
| 426 | }); |
| 427 | } else { |
| 428 | let mut bind_visit_fields = TokenStream::new(); |
| 429 | let mut bind_visit_mut_fields = TokenStream::new(); |
| 430 | let mut bind_fold_fields = TokenStream::new(); |
| 431 | |
| 432 | let mut visit_fields = TokenStream::new(); |
| 433 | let mut visit_mut_fields = TokenStream::new(); |
| 434 | let mut fold_fields = TokenStream::new(); |
| 435 | |
| 436 | for (idx, ty) in variant.fields().iter().enumerate() { |
| 437 | let name = format!("_binding_{}", idx); |
| 438 | let binding = Ident::new(&name, Span::call_site()); |
| 439 | |
| 440 | bind_visit_fields.append_all(quote! { |
| 441 | ref #binding, |
| 442 | }); |
| 443 | bind_visit_mut_fields.append_all(quote! { |
| 444 | ref mut #binding, |
| 445 | }); |
| 446 | bind_fold_fields.append_all(quote! { |
| 447 | #binding, |
| 448 | }); |
| 449 | |
| 450 | let borrowed_binding = Borrowed(quote!(#binding)); |
| 451 | let owned_binding = Owned(quote!(#binding)); |
| 452 | |
| 453 | visit_fields.append_all( |
| 454 | visit(ty, s.features(), types, Visit, &borrowed_binding) |
| 455 | .unwrap_or_else(|| noop_visit(Visit, &borrowed_binding)), |
| 456 | ); |
| 457 | visit_mut_fields.append_all( |
| 458 | visit(ty, s.features(), types, VisitMut, &borrowed_binding) |
| 459 | .unwrap_or_else(|| noop_visit(VisitMut, &borrowed_binding)), |
| 460 | ); |
| 461 | fold_fields.append_all( |
| 462 | visit(ty, s.features(), types, Fold, &owned_binding) |
| 463 | .unwrap_or_else(|| noop_visit(Fold, &owned_binding)), |
| 464 | ); |
| 465 | |
| 466 | visit_fields.append_all(quote!(;)); |
| 467 | visit_mut_fields.append_all(quote!(;)); |
| 468 | fold_fields.append_all(quote!(,)); |
| 469 | } |
| 470 | |
| 471 | visit_variants.append_all(quote! { |
| 472 | #ty::#variant_ident(#bind_visit_fields) => { |
| 473 | #visit_fields |
| 474 | } |
| 475 | }); |
| 476 | |
| 477 | visit_mut_variants.append_all(quote! { |
| 478 | #ty::#variant_ident(#bind_visit_mut_fields) => { |
| 479 | #visit_mut_fields |
| 480 | } |
| 481 | }); |
| 482 | |
| 483 | fold_variants.append_all(quote! { |
| 484 | #ty::#variant_ident(#bind_fold_fields) => { |
| 485 | #ty::#variant_ident( |
| 486 | #fold_fields |
| 487 | ) |
| 488 | } |
| 489 | }); |
| 490 | } |
| 491 | } |
| 492 | |
| 493 | visit_impl.append_all(quote! { |
| 494 | match *_i { |
| 495 | #visit_variants |
| 496 | } |
| 497 | }); |
| 498 | |
| 499 | visit_mut_impl.append_all(quote! { |
| 500 | match *_i { |
| 501 | #visit_mut_variants |
| 502 | } |
| 503 | }); |
| 504 | |
| 505 | fold_impl.append_all(quote! { |
| 506 | match _i { |
| 507 | #fold_variants |
| 508 | } |
| 509 | }); |
| 510 | } |
David Tolnay | f9bb8ff | 2019-02-15 13:10:14 -0800 | [diff] [blame^] | 511 | types::Node::Struct(ref v) => { |
Carl Lerche | 058ff47 | 2019-02-13 16:23:52 -0800 | [diff] [blame] | 512 | let mut fold_fields = TokenStream::new(); |
| 513 | |
| 514 | for field in v.fields() { |
| 515 | let id = Ident::new(field.ident(), Span::call_site()); |
| 516 | let ref_toks = Owned(quote!(_i.#id)); |
| 517 | let visit_field = visit(field.ty(), v.features(), types, Visit, &ref_toks) |
| 518 | .unwrap_or_else(|| noop_visit(Visit, &ref_toks)); |
| 519 | visit_impl.append_all(quote! { |
| 520 | #visit_field; |
| 521 | }); |
| 522 | let visit_mut_field = |
| 523 | visit(field.ty(), v.features(), types, VisitMut, &ref_toks) |
| 524 | .unwrap_or_else(|| noop_visit(VisitMut, &ref_toks)); |
| 525 | visit_mut_impl.append_all(quote! { |
| 526 | #visit_mut_field; |
| 527 | }); |
| 528 | let fold = visit(field.ty(), v.features(), types, Fold, &ref_toks) |
| 529 | .unwrap_or_else(|| noop_visit(Fold, &ref_toks)); |
| 530 | |
| 531 | fold_fields.append_all(quote! { |
| 532 | #id: #fold, |
| 533 | }); |
| 534 | } |
| 535 | |
| 536 | if !v.fields().is_empty() { |
| 537 | fold_impl.append_all(quote! { |
| 538 | #ty { |
| 539 | #fold_fields |
| 540 | } |
| 541 | }) |
| 542 | } else { |
| 543 | if ty == "Ident" { |
| 544 | fold_impl.append_all(quote! { |
| 545 | let mut _i = _i; |
| 546 | let span = _visitor.fold_span(_i.span()); |
| 547 | _i.set_span(span); |
| 548 | }); |
| 549 | } |
| 550 | fold_impl.append_all(quote! { |
| 551 | _i |
| 552 | }); |
| 553 | } |
| 554 | } |
| 555 | } |
| 556 | |
| 557 | let mut include_fold_impl = true; |
David Tolnay | f9bb8ff | 2019-02-15 13:10:14 -0800 | [diff] [blame^] | 558 | if let types::Node::Struct(ref data) = s { |
Carl Lerche | 058ff47 | 2019-02-13 16:23:52 -0800 | [diff] [blame] | 559 | if !data.all_fields_pub() { |
| 560 | include_fold_impl = false; |
| 561 | } |
| 562 | } |
| 563 | |
| 564 | state.visit_trait.append_all(quote! { |
| 565 | #features |
| 566 | fn #visit_fn(&mut self, i: &'ast #ty) { |
| 567 | #visit_fn(self, i) |
| 568 | } |
| 569 | }); |
| 570 | |
| 571 | state.visit_impl.append_all(quote! { |
| 572 | #features |
| 573 | pub fn #visit_fn<'ast, V: Visit<'ast> + ?Sized>( |
| 574 | _visitor: &mut V, _i: &'ast #ty |
| 575 | ) { |
| 576 | #visit_impl |
| 577 | } |
| 578 | }); |
| 579 | |
| 580 | state.visit_mut_trait.append_all(quote! { |
| 581 | #features |
| 582 | fn #visit_mut_fn(&mut self, i: &mut #ty) { |
| 583 | #visit_mut_fn(self, i) |
| 584 | } |
| 585 | }); |
| 586 | |
| 587 | state.visit_mut_impl.append_all(quote! { |
| 588 | #features |
| 589 | pub fn #visit_mut_fn<V: VisitMut + ?Sized>( |
| 590 | _visitor: &mut V, _i: &mut #ty |
| 591 | ) { |
| 592 | #visit_mut_impl |
| 593 | } |
| 594 | }); |
| 595 | |
| 596 | state.fold_trait.append_all(quote! { |
| 597 | #features |
| 598 | fn #fold_fn(&mut self, i: #ty) -> #ty { |
| 599 | #fold_fn(self, i) |
| 600 | } |
| 601 | }); |
| 602 | |
| 603 | if include_fold_impl { |
| 604 | state.fold_impl.append_all(quote! { |
| 605 | #features |
| 606 | pub fn #fold_fn<V: Fold + ?Sized>( |
| 607 | _visitor: &mut V, _i: #ty |
| 608 | ) -> #ty { |
| 609 | #fold_impl |
| 610 | } |
| 611 | }); |
| 612 | } |
| 613 | } |
| 614 | } |
| 615 | |
| 616 | fn write_file(path: &str, content: TokenStream) { |
| 617 | let mut file = File::create(path).unwrap(); |
| 618 | write!( |
| 619 | file, |
| 620 | "// THIS FILE IS AUTOMATICALLY GENERATED; DO NOT EDIT\n\n" |
| 621 | ) |
| 622 | .unwrap(); |
| 623 | let mut config = rustfmt::Config::default(); |
| 624 | config.set().emit_mode(rustfmt::EmitMode::Stdout); |
| 625 | config.set().verbose(rustfmt::Verbosity::Quiet); |
| 626 | config.set().format_macro_matchers(true); |
| 627 | config.set().normalize_doc_attributes(true); |
| 628 | let mut session = rustfmt::Session::new(config, Some(&mut file)); |
| 629 | session |
| 630 | .format(rustfmt::Input::Text(content.to_string())) |
| 631 | .unwrap(); |
| 632 | } |
| 633 | |
David Tolnay | f9bb8ff | 2019-02-15 13:10:14 -0800 | [diff] [blame^] | 634 | pub fn generate(defs: &types::Definitions) { |
Carl Lerche | 058ff47 | 2019-02-13 16:23:52 -0800 | [diff] [blame] | 635 | let mut state = codegen::State::default(); |
David Tolnay | f9bb8ff | 2019-02-15 13:10:14 -0800 | [diff] [blame^] | 636 | for s in &defs.types { |
| 637 | codegen::generate(&mut state, s, &defs.types); |
Carl Lerche | 058ff47 | 2019-02-13 16:23:52 -0800 | [diff] [blame] | 638 | } |
| 639 | |
| 640 | let full_macro = quote! { |
| 641 | #[cfg(feature = "full")] |
| 642 | macro_rules! full { |
| 643 | ($e:expr) => { |
| 644 | $e |
| 645 | }; |
| 646 | } |
| 647 | |
| 648 | #[cfg(all(feature = "derive", not(feature = "full")))] |
| 649 | macro_rules! full { |
| 650 | ($e:expr) => { |
| 651 | unreachable!() |
| 652 | }; |
| 653 | } |
| 654 | }; |
| 655 | |
| 656 | let skip_macro = quote! { |
| 657 | #[cfg(any(feature = "full", feature = "derive"))] |
| 658 | macro_rules! skip { |
| 659 | ($($tt:tt)*) => {}; |
| 660 | } |
| 661 | }; |
| 662 | |
| 663 | let fold_trait = state.fold_trait; |
| 664 | let fold_impl = state.fold_impl; |
| 665 | write_file( |
| 666 | FOLD_SRC, |
| 667 | quote! { |
| 668 | // Unreachable code is generated sometimes without the full feature. |
| 669 | #![allow(unreachable_code)] |
| 670 | |
| 671 | use *; |
| 672 | #[cfg(any(feature = "full", feature = "derive"))] |
| 673 | use token::{Brace, Bracket, Paren, Group}; |
| 674 | use proc_macro2::Span; |
| 675 | #[cfg(any(feature = "full", feature = "derive"))] |
| 676 | use gen::helper::fold::*; |
| 677 | |
| 678 | #full_macro |
| 679 | |
| 680 | /// Syntax tree traversal to transform the nodes of an owned syntax tree. |
| 681 | /// |
| 682 | /// See the [module documentation] for details. |
| 683 | /// |
| 684 | /// [module documentation]: index.html |
| 685 | /// |
| 686 | /// *This trait is available if Syn is built with the `"fold"` feature.* |
| 687 | pub trait Fold { |
| 688 | #fold_trait |
| 689 | } |
| 690 | |
| 691 | #[cfg(any(feature = "full", feature = "derive"))] |
| 692 | macro_rules! fold_span_only { |
| 693 | ($f:ident : $t:ident) => { |
| 694 | pub fn $f<V: Fold + ?Sized>(_visitor: &mut V, mut _i: $t) -> $t { |
| 695 | let span = _visitor.fold_span(_i.span()); |
| 696 | _i.set_span(span); |
| 697 | _i |
| 698 | } |
| 699 | } |
| 700 | } |
| 701 | |
| 702 | #[cfg(any(feature = "full", feature = "derive"))] |
| 703 | fold_span_only!(fold_lit_byte: LitByte); |
| 704 | #[cfg(any(feature = "full", feature = "derive"))] |
| 705 | fold_span_only!(fold_lit_byte_str: LitByteStr); |
| 706 | #[cfg(any(feature = "full", feature = "derive"))] |
| 707 | fold_span_only!(fold_lit_char: LitChar); |
| 708 | #[cfg(any(feature = "full", feature = "derive"))] |
| 709 | fold_span_only!(fold_lit_float: LitFloat); |
| 710 | #[cfg(any(feature = "full", feature = "derive"))] |
| 711 | fold_span_only!(fold_lit_int: LitInt); |
| 712 | #[cfg(any(feature = "full", feature = "derive"))] |
| 713 | fold_span_only!(fold_lit_str: LitStr); |
| 714 | |
| 715 | #fold_impl |
| 716 | }, |
| 717 | ); |
| 718 | |
| 719 | let visit_trait = state.visit_trait; |
| 720 | let visit_impl = state.visit_impl; |
| 721 | write_file( |
| 722 | VISIT_SRC, |
| 723 | quote! { |
| 724 | #![cfg_attr(feature = "cargo-clippy", allow(trivially_copy_pass_by_ref))] |
| 725 | |
| 726 | use *; |
| 727 | #[cfg(any(feature = "full", feature = "derive"))] |
| 728 | use punctuated::Punctuated; |
| 729 | use proc_macro2::Span; |
| 730 | #[cfg(any(feature = "full", feature = "derive"))] |
| 731 | use gen::helper::visit::*; |
| 732 | |
| 733 | #full_macro |
| 734 | #skip_macro |
| 735 | |
| 736 | /// Syntax tree traversal to walk a shared borrow of a syntax tree. |
| 737 | /// |
| 738 | /// See the [module documentation] for details. |
| 739 | /// |
| 740 | /// [module documentation]: index.html |
| 741 | /// |
| 742 | /// *This trait is available if Syn is built with the `"visit"` feature.* |
| 743 | pub trait Visit<'ast> { |
| 744 | #visit_trait |
| 745 | } |
| 746 | |
| 747 | #visit_impl |
| 748 | }, |
| 749 | ); |
| 750 | |
| 751 | let visit_mut_trait = state.visit_mut_trait; |
| 752 | let visit_mut_impl = state.visit_mut_impl; |
| 753 | write_file( |
| 754 | VISIT_MUT_SRC, |
| 755 | quote! { |
| 756 | use *; |
| 757 | #[cfg(any(feature = "full", feature = "derive"))] |
| 758 | use punctuated::Punctuated; |
| 759 | use proc_macro2::Span; |
| 760 | #[cfg(any(feature = "full", feature = "derive"))] |
| 761 | use gen::helper::visit_mut::*; |
| 762 | |
| 763 | #full_macro |
| 764 | #skip_macro |
| 765 | |
| 766 | /// Syntax tree traversal to mutate an exclusive borrow of a syntax tree in |
| 767 | /// place. |
| 768 | /// |
| 769 | /// See the [module documentation] for details. |
| 770 | /// |
| 771 | /// [module documentation]: index.html |
| 772 | /// |
| 773 | /// *This trait is available if Syn is built with the `"visit-mut"` feature.* |
| 774 | pub trait VisitMut { |
| 775 | #visit_mut_trait |
| 776 | } |
| 777 | |
| 778 | #visit_mut_impl |
| 779 | }, |
| 780 | ); |
| 781 | } |