blob: f8523087c301957b2055f679aefa6f2e611218a6 [file] [log] [blame]
Carl Lerche058ff472019-02-13 16:23:52 -08001//! 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
13use crate::types;
14use proc_macro2::TokenStream;
15
16use std::fs::File;
17use std::io::Write;
18
19const FOLD_SRC: &str = "../src/gen/fold.rs";
20const VISIT_SRC: &str = "../src/gen/visit.rs";
21const VISIT_MUT_SRC: &str = "../src/gen/visit_mut.rs";
22
23mod 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 Tolnay157c7eb2019-02-15 13:21:48 -0800119 defs: &types::Definitions,
Carl Lerche058ff472019-02-13 16:23:52 -0800120 kind: Kind,
121 name: &Operand,
122 ) -> Option<TokenStream> {
123 let name = name.owned_tokens();
David Tolnay157c7eb2019-02-15 13:21:48 -0800124 let res = visit(elem, features, defs, kind, &Owned(quote!(*#name)))?;
Carl Lerche058ff472019-02-13 16:23:52 -0800125 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 Tolnay157c7eb2019-02-15 13:21:48 -0800136 defs: &types::Definitions,
Carl Lerche058ff472019-02-13 16:23:52 -0800137 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 };
David Tolnay157c7eb2019-02-15 13:21:48 -0800144 let val = visit(elem, features, defs, kind, &operand)?;
Carl Lerche058ff472019-02-13 16:23:52 -0800145 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 Tolnay157c7eb2019-02-15 13:21:48 -0800174 defs: &types::Definitions,
Carl Lerche058ff472019-02-13 16:23:52 -0800175 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 };
David Tolnay157c7eb2019-02-15 13:21:48 -0800182 let val = visit(elem, features, defs, kind, &operand)?;
Carl Lerche058ff472019-02-13 16:23:52 -0800183 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 Tolnay157c7eb2019-02-15 13:21:48 -0800214 defs: &types::Definitions,
Carl Lerche058ff472019-02-13 16:23:52 -0800215 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 };
David Tolnay157c7eb2019-02-15 13:21:48 -0800222 let val = visit(elem, features, defs, kind, &it)?;
Carl Lerche058ff472019-02-13 16:23:52 -0800223 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 Tolnay157c7eb2019-02-15 13:21:48 -0800244 defs: &types::Definitions,
Carl Lerche058ff472019-02-13 16:23:52 -0800245 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 =
David Tolnay157c7eb2019-02-15 13:21:48 -0800258 visit(elem, features, defs, kind, &it).unwrap_or_else(|| noop_visit(kind, &it));
Carl Lerche058ff472019-02-13 16:23:52 -0800259 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
David Tolnay157c7eb2019-02-15 13:21:48 -0800273 fn token_punct_visit(repr: &str, kind: Kind, name: &Operand) -> TokenStream {
274 let ty: TokenStream = syn::parse_str(&format!("Token![{}]", repr)).unwrap();
Carl Lerche058ff472019-02-13 16:23:52 -0800275 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
David Tolnay157c7eb2019-02-15 13:21:48 -0800289 fn token_keyword_visit(repr: &str, kind: Kind, name: &Operand) -> TokenStream {
290 let ty: TokenStream = syn::parse_str(&format!("Token![{}]", repr)).unwrap();
Carl Lerche058ff472019-02-13 16:23:52 -0800291 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 Tolnay157c7eb2019-02-15 13:21:48 -0800336 defs: &types::Definitions,
Carl Lerche058ff472019-02-13 16:23:52 -0800337 kind: Kind,
338 name: &Operand,
339 ) -> Option<TokenStream> {
340 match ty {
David Tolnay157c7eb2019-02-15 13:21:48 -0800341 types::Type::Box(t) => box_visit(&*t, features, defs, kind, name),
342 types::Type::Vec(t) => vec_visit(&*t, features, defs, kind, name),
David Tolnay295141b2019-02-15 12:45:33 -0800343 types::Type::Punctuated(p) => {
David Tolnay157c7eb2019-02-15 13:21:48 -0800344 punctuated_visit(p.element(), features, defs, kind, name)
David Tolnay295141b2019-02-15 12:45:33 -0800345 }
David Tolnay157c7eb2019-02-15 13:21:48 -0800346 types::Type::Option(t) => option_visit(&*t, features, defs, kind, name),
347 types::Type::Tuple(t) => tuple_visit(t, features, defs, kind, name),
Carl Lerche058ff472019-02-13 16:23:52 -0800348 types::Type::Token(t) => {
David Tolnay157c7eb2019-02-15 13:21:48 -0800349 let repr = &defs.tokens[t];
350 let is_keyword = repr.chars().next().unwrap().is_alphabetic();
351 if is_keyword {
352 Some(token_keyword_visit(repr, kind, name))
Carl Lerche058ff472019-02-13 16:23:52 -0800353 } else {
David Tolnay157c7eb2019-02-15 13:21:48 -0800354 Some(token_punct_visit(repr, kind, name))
Carl Lerche058ff472019-02-13 16:23:52 -0800355 }
356 }
David Tolnay295141b2019-02-15 12:45:33 -0800357 types::Type::Group(t) => Some(token_group_visit(&t[..], kind, name)),
David Tolnayd3076572019-02-15 13:32:44 -0800358 types::Type::Syn(t) => {
Carl Lerche058ff472019-02-13 16:23:52 -0800359 fn requires_full(features: &types::Features) -> bool {
360 features.contains("full") && features.len() == 1
361 }
362
363 let mut res = simple_visit(t, kind, name);
364
David Tolnay157c7eb2019-02-15 13:21:48 -0800365 let target = defs.types.iter().find(|ty| ty.ident() == t).unwrap();
Carl Lerche058ff472019-02-13 16:23:52 -0800366
367 Some(
368 if requires_full(target.features()) && !requires_full(features) {
369 quote! {
370 full!(#res)
371 }
372 } else {
373 res
374 },
375 )
376 }
Carl Lerchecbf7cc12019-02-15 14:09:31 -0800377 types::Type::Ext(t) if super::TERMINAL_TYPES.contains(&&t[..]) => {
378 Some(simple_visit(t, kind, name))
379 }
Carl Lerche058ff472019-02-13 16:23:52 -0800380 types::Type::Ext(_) | types::Type::Std(_) => None,
381 }
382 }
383
384 fn visit_features(features: &types::Features) -> TokenStream {
385 match features.len() {
386 0 => quote!(),
387 1 => {
388 let feature = &features[0];
389 quote!(#[cfg(feature = #feature)])
390 }
391 _ => {
392 let features = features.iter().map(|feature| quote!(feature = #feature));
393
394 quote!(#[cfg(any( #(#features),* ))])
395 }
396 }
397 }
398
David Tolnay157c7eb2019-02-15 13:21:48 -0800399 pub fn generate(state: &mut State, s: &types::Node, defs: &types::Definitions) {
Carl Lerche058ff472019-02-13 16:23:52 -0800400 let features = visit_features(s.features());
401 let under_name = under_name(s.ident());
402 let ty = Ident::new(s.ident(), Span::call_site());
403 let visit_fn = Ident::new(&format!("visit_{}", under_name), Span::call_site());
404 let visit_mut_fn = Ident::new(&format!("visit_{}_mut", under_name), Span::call_site());
405 let fold_fn = Ident::new(&format!("fold_{}", under_name), Span::call_site());
406
407 let mut visit_impl = TokenStream::new();
408 let mut visit_mut_impl = TokenStream::new();
409 let mut fold_impl = TokenStream::new();
410
411 match s {
David Tolnayf9bb8ff2019-02-15 13:10:14 -0800412 types::Node::Enum(ref e) => {
Carl Lerche058ff472019-02-13 16:23:52 -0800413 let mut visit_variants = TokenStream::new();
414 let mut visit_mut_variants = TokenStream::new();
415 let mut fold_variants = TokenStream::new();
416
417 for variant in e.variants() {
418 let variant_ident = Ident::new(variant.ident(), Span::call_site());
419
420 if variant.fields().is_empty() {
421 visit_variants.append_all(quote! {
422 #ty::#variant_ident => {}
423 });
424 visit_mut_variants.append_all(quote! {
425 #ty::#variant_ident => {}
426 });
427 fold_variants.append_all(quote! {
428 #ty::#variant_ident => {
429 #ty::#variant_ident
430 }
431 });
432 } else {
433 let mut bind_visit_fields = TokenStream::new();
434 let mut bind_visit_mut_fields = TokenStream::new();
435 let mut bind_fold_fields = TokenStream::new();
436
437 let mut visit_fields = TokenStream::new();
438 let mut visit_mut_fields = TokenStream::new();
439 let mut fold_fields = TokenStream::new();
440
441 for (idx, ty) in variant.fields().iter().enumerate() {
442 let name = format!("_binding_{}", idx);
443 let binding = Ident::new(&name, Span::call_site());
444
445 bind_visit_fields.append_all(quote! {
446 ref #binding,
447 });
448 bind_visit_mut_fields.append_all(quote! {
449 ref mut #binding,
450 });
451 bind_fold_fields.append_all(quote! {
452 #binding,
453 });
454
455 let borrowed_binding = Borrowed(quote!(#binding));
456 let owned_binding = Owned(quote!(#binding));
457
458 visit_fields.append_all(
David Tolnay157c7eb2019-02-15 13:21:48 -0800459 visit(ty, s.features(), defs, Visit, &borrowed_binding)
Carl Lerche058ff472019-02-13 16:23:52 -0800460 .unwrap_or_else(|| noop_visit(Visit, &borrowed_binding)),
461 );
462 visit_mut_fields.append_all(
David Tolnay157c7eb2019-02-15 13:21:48 -0800463 visit(ty, s.features(), defs, VisitMut, &borrowed_binding)
Carl Lerche058ff472019-02-13 16:23:52 -0800464 .unwrap_or_else(|| noop_visit(VisitMut, &borrowed_binding)),
465 );
466 fold_fields.append_all(
David Tolnay157c7eb2019-02-15 13:21:48 -0800467 visit(ty, s.features(), defs, Fold, &owned_binding)
Carl Lerche058ff472019-02-13 16:23:52 -0800468 .unwrap_or_else(|| noop_visit(Fold, &owned_binding)),
469 );
470
471 visit_fields.append_all(quote!(;));
472 visit_mut_fields.append_all(quote!(;));
473 fold_fields.append_all(quote!(,));
474 }
475
476 visit_variants.append_all(quote! {
477 #ty::#variant_ident(#bind_visit_fields) => {
478 #visit_fields
479 }
480 });
481
482 visit_mut_variants.append_all(quote! {
483 #ty::#variant_ident(#bind_visit_mut_fields) => {
484 #visit_mut_fields
485 }
486 });
487
488 fold_variants.append_all(quote! {
489 #ty::#variant_ident(#bind_fold_fields) => {
490 #ty::#variant_ident(
491 #fold_fields
492 )
493 }
494 });
495 }
496 }
497
498 visit_impl.append_all(quote! {
499 match *_i {
500 #visit_variants
501 }
502 });
503
504 visit_mut_impl.append_all(quote! {
505 match *_i {
506 #visit_mut_variants
507 }
508 });
509
510 fold_impl.append_all(quote! {
511 match _i {
512 #fold_variants
513 }
514 });
515 }
David Tolnayf9bb8ff2019-02-15 13:10:14 -0800516 types::Node::Struct(ref v) => {
Carl Lerche058ff472019-02-13 16:23:52 -0800517 let mut fold_fields = TokenStream::new();
518
519 for field in v.fields() {
520 let id = Ident::new(field.ident(), Span::call_site());
521 let ref_toks = Owned(quote!(_i.#id));
David Tolnay157c7eb2019-02-15 13:21:48 -0800522 let visit_field = visit(field.ty(), v.features(), defs, Visit, &ref_toks)
Carl Lerche058ff472019-02-13 16:23:52 -0800523 .unwrap_or_else(|| noop_visit(Visit, &ref_toks));
524 visit_impl.append_all(quote! {
525 #visit_field;
526 });
527 let visit_mut_field =
David Tolnay157c7eb2019-02-15 13:21:48 -0800528 visit(field.ty(), v.features(), defs, VisitMut, &ref_toks)
Carl Lerche058ff472019-02-13 16:23:52 -0800529 .unwrap_or_else(|| noop_visit(VisitMut, &ref_toks));
530 visit_mut_impl.append_all(quote! {
531 #visit_mut_field;
532 });
David Tolnay157c7eb2019-02-15 13:21:48 -0800533 let fold = visit(field.ty(), v.features(), defs, Fold, &ref_toks)
Carl Lerche058ff472019-02-13 16:23:52 -0800534 .unwrap_or_else(|| noop_visit(Fold, &ref_toks));
535
536 fold_fields.append_all(quote! {
537 #id: #fold,
538 });
539 }
540
541 if !v.fields().is_empty() {
542 fold_impl.append_all(quote! {
543 #ty {
544 #fold_fields
545 }
546 })
547 } else {
548 if ty == "Ident" {
549 fold_impl.append_all(quote! {
550 let mut _i = _i;
551 let span = _visitor.fold_span(_i.span());
552 _i.set_span(span);
553 });
554 }
555 fold_impl.append_all(quote! {
556 _i
557 });
558 }
559 }
560 }
561
562 let mut include_fold_impl = true;
David Tolnayf9bb8ff2019-02-15 13:10:14 -0800563 if let types::Node::Struct(ref data) = s {
Carl Lerche058ff472019-02-13 16:23:52 -0800564 if !data.all_fields_pub() {
565 include_fold_impl = false;
566 }
567 }
568
569 state.visit_trait.append_all(quote! {
570 #features
571 fn #visit_fn(&mut self, i: &'ast #ty) {
572 #visit_fn(self, i)
573 }
574 });
575
576 state.visit_impl.append_all(quote! {
577 #features
578 pub fn #visit_fn<'ast, V: Visit<'ast> + ?Sized>(
579 _visitor: &mut V, _i: &'ast #ty
580 ) {
581 #visit_impl
582 }
583 });
584
585 state.visit_mut_trait.append_all(quote! {
586 #features
587 fn #visit_mut_fn(&mut self, i: &mut #ty) {
588 #visit_mut_fn(self, i)
589 }
590 });
591
592 state.visit_mut_impl.append_all(quote! {
593 #features
594 pub fn #visit_mut_fn<V: VisitMut + ?Sized>(
595 _visitor: &mut V, _i: &mut #ty
596 ) {
597 #visit_mut_impl
598 }
599 });
600
601 state.fold_trait.append_all(quote! {
602 #features
603 fn #fold_fn(&mut self, i: #ty) -> #ty {
604 #fold_fn(self, i)
605 }
606 });
607
608 if include_fold_impl {
609 state.fold_impl.append_all(quote! {
610 #features
611 pub fn #fold_fn<V: Fold + ?Sized>(
612 _visitor: &mut V, _i: #ty
613 ) -> #ty {
614 #fold_impl
615 }
616 });
617 }
618 }
619}
620
621fn write_file(path: &str, content: TokenStream) {
622 let mut file = File::create(path).unwrap();
623 write!(
624 file,
625 "// THIS FILE IS AUTOMATICALLY GENERATED; DO NOT EDIT\n\n"
626 )
627 .unwrap();
628 let mut config = rustfmt::Config::default();
629 config.set().emit_mode(rustfmt::EmitMode::Stdout);
630 config.set().verbose(rustfmt::Verbosity::Quiet);
631 config.set().format_macro_matchers(true);
632 config.set().normalize_doc_attributes(true);
633 let mut session = rustfmt::Session::new(config, Some(&mut file));
634 session
635 .format(rustfmt::Input::Text(content.to_string()))
636 .unwrap();
637}
638
Carl Lerchecbf7cc12019-02-15 14:09:31 -0800639const TERMINAL_TYPES: &[&str] = &["Span", "Ident"];
640
David Tolnayf9bb8ff2019-02-15 13:10:14 -0800641pub fn generate(defs: &types::Definitions) {
Carl Lerchecbf7cc12019-02-15 14:09:31 -0800642 let mut defs = defs.clone();
643
644 for &tt in TERMINAL_TYPES {
645 defs.insert(types::Node::Struct(types::Struct::new(
646 tt.to_string(),
647 types::Features::default(),
648 vec![],
649 true)
650 ));
651 }
652
Carl Lerche058ff472019-02-13 16:23:52 -0800653 let mut state = codegen::State::default();
David Tolnayf9bb8ff2019-02-15 13:10:14 -0800654 for s in &defs.types {
Carl Lerchecbf7cc12019-02-15 14:09:31 -0800655 codegen::generate(&mut state, s, &defs);
Carl Lerche058ff472019-02-13 16:23:52 -0800656 }
657
658 let full_macro = quote! {
659 #[cfg(feature = "full")]
660 macro_rules! full {
661 ($e:expr) => {
662 $e
663 };
664 }
665
666 #[cfg(all(feature = "derive", not(feature = "full")))]
667 macro_rules! full {
668 ($e:expr) => {
669 unreachable!()
670 };
671 }
672 };
673
674 let skip_macro = quote! {
675 #[cfg(any(feature = "full", feature = "derive"))]
676 macro_rules! skip {
677 ($($tt:tt)*) => {};
678 }
679 };
680
681 let fold_trait = state.fold_trait;
682 let fold_impl = state.fold_impl;
683 write_file(
684 FOLD_SRC,
685 quote! {
686 // Unreachable code is generated sometimes without the full feature.
687 #![allow(unreachable_code)]
688
689 use *;
690 #[cfg(any(feature = "full", feature = "derive"))]
691 use token::{Brace, Bracket, Paren, Group};
692 use proc_macro2::Span;
693 #[cfg(any(feature = "full", feature = "derive"))]
694 use gen::helper::fold::*;
695
696 #full_macro
697
698 /// Syntax tree traversal to transform the nodes of an owned syntax tree.
699 ///
700 /// See the [module documentation] for details.
701 ///
702 /// [module documentation]: index.html
703 ///
704 /// *This trait is available if Syn is built with the `"fold"` feature.*
705 pub trait Fold {
706 #fold_trait
707 }
708
709 #[cfg(any(feature = "full", feature = "derive"))]
710 macro_rules! fold_span_only {
711 ($f:ident : $t:ident) => {
712 pub fn $f<V: Fold + ?Sized>(_visitor: &mut V, mut _i: $t) -> $t {
713 let span = _visitor.fold_span(_i.span());
714 _i.set_span(span);
715 _i
716 }
717 }
718 }
719
720 #[cfg(any(feature = "full", feature = "derive"))]
721 fold_span_only!(fold_lit_byte: LitByte);
722 #[cfg(any(feature = "full", feature = "derive"))]
723 fold_span_only!(fold_lit_byte_str: LitByteStr);
724 #[cfg(any(feature = "full", feature = "derive"))]
725 fold_span_only!(fold_lit_char: LitChar);
726 #[cfg(any(feature = "full", feature = "derive"))]
727 fold_span_only!(fold_lit_float: LitFloat);
728 #[cfg(any(feature = "full", feature = "derive"))]
729 fold_span_only!(fold_lit_int: LitInt);
730 #[cfg(any(feature = "full", feature = "derive"))]
731 fold_span_only!(fold_lit_str: LitStr);
732
733 #fold_impl
734 },
735 );
736
737 let visit_trait = state.visit_trait;
738 let visit_impl = state.visit_impl;
739 write_file(
740 VISIT_SRC,
741 quote! {
742 #![cfg_attr(feature = "cargo-clippy", allow(trivially_copy_pass_by_ref))]
743
744 use *;
745 #[cfg(any(feature = "full", feature = "derive"))]
746 use punctuated::Punctuated;
747 use proc_macro2::Span;
748 #[cfg(any(feature = "full", feature = "derive"))]
749 use gen::helper::visit::*;
750
751 #full_macro
752 #skip_macro
753
754 /// Syntax tree traversal to walk a shared borrow of a syntax tree.
755 ///
756 /// See the [module documentation] for details.
757 ///
758 /// [module documentation]: index.html
759 ///
760 /// *This trait is available if Syn is built with the `"visit"` feature.*
761 pub trait Visit<'ast> {
762 #visit_trait
763 }
764
765 #visit_impl
766 },
767 );
768
769 let visit_mut_trait = state.visit_mut_trait;
770 let visit_mut_impl = state.visit_mut_impl;
771 write_file(
772 VISIT_MUT_SRC,
773 quote! {
774 use *;
775 #[cfg(any(feature = "full", feature = "derive"))]
776 use punctuated::Punctuated;
777 use proc_macro2::Span;
778 #[cfg(any(feature = "full", feature = "derive"))]
779 use gen::helper::visit_mut::*;
780
781 #full_macro
782 #skip_macro
783
784 /// Syntax tree traversal to mutate an exclusive borrow of a syntax tree in
785 /// place.
786 ///
787 /// See the [module documentation] for details.
788 ///
789 /// [module documentation]: index.html
790 ///
791 /// *This trait is available if Syn is built with the `"visit-mut"` feature.*
792 pub trait VisitMut {
793 #visit_mut_trait
794 }
795
796 #visit_mut_impl
797 },
798 );
799}