]> git.lizzy.rs Git - rust.git/blob - src/librustc/hir/fold.rs
Changed issue number to 36105
[rust.git] / src / librustc / hir / fold.rs
1 // Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 //! A Folder represents an HIR->HIR fold; it accepts a HIR piece,
12 //! and returns a piece of the same type.
13
14 use hir::*;
15 use syntax::ast::{Name, NodeId, DUMMY_NODE_ID, Attribute, Attribute_, MetaItem};
16 use syntax::ast::MetaItemKind;
17 use hir;
18 use syntax_pos::Span;
19 use syntax::codemap::{respan, Spanned};
20 use syntax::ptr::P;
21 use syntax::parse::token::keywords;
22 use syntax::util::move_map::MoveMap;
23
24 pub trait Folder : Sized {
25     // Any additions to this trait should happen in form
26     // of a call to a public `noop_*` function that only calls
27     // out to the folder again, not other `noop_*` functions.
28     //
29     // This is a necessary API workaround to the problem of not
30     // being able to call out to the super default method
31     // in an overridden default method.
32
33     fn fold_crate(&mut self, c: Crate) -> Crate {
34         noop_fold_crate(c, self)
35     }
36
37     fn fold_meta_items(&mut self, meta_items: HirVec<P<MetaItem>>) -> HirVec<P<MetaItem>> {
38         noop_fold_meta_items(meta_items, self)
39     }
40
41     fn fold_meta_item(&mut self, meta_item: P<MetaItem>) -> P<MetaItem> {
42         noop_fold_meta_item(meta_item, self)
43     }
44
45     fn fold_view_path(&mut self, view_path: P<ViewPath>) -> P<ViewPath> {
46         noop_fold_view_path(view_path, self)
47     }
48
49     fn fold_foreign_item(&mut self, ni: ForeignItem) -> ForeignItem {
50         noop_fold_foreign_item(ni, self)
51     }
52
53     fn fold_item(&mut self, i: Item) -> Item {
54         noop_fold_item(i, self)
55     }
56
57     fn fold_item_id(&mut self, i: ItemId) -> ItemId {
58         noop_fold_item_id(i, self)
59     }
60
61     fn fold_struct_field(&mut self, sf: StructField) -> StructField {
62         noop_fold_struct_field(sf, self)
63     }
64
65     fn fold_item_underscore(&mut self, i: Item_) -> Item_ {
66         noop_fold_item_underscore(i, self)
67     }
68
69     fn fold_trait_item(&mut self, i: TraitItem) -> TraitItem {
70         noop_fold_trait_item(i, self)
71     }
72
73     fn fold_impl_item(&mut self, i: ImplItem) -> ImplItem {
74         noop_fold_impl_item(i, self)
75     }
76
77     fn fold_fn_decl(&mut self, d: P<FnDecl>) -> P<FnDecl> {
78         noop_fold_fn_decl(d, self)
79     }
80
81     fn fold_block(&mut self, b: P<Block>) -> P<Block> {
82         noop_fold_block(b, self)
83     }
84
85     fn fold_stmt(&mut self, s: Stmt) -> Stmt {
86         noop_fold_stmt(s, self)
87     }
88
89     fn fold_arm(&mut self, a: Arm) -> Arm {
90         noop_fold_arm(a, self)
91     }
92
93     fn fold_pat(&mut self, p: P<Pat>) -> P<Pat> {
94         noop_fold_pat(p, self)
95     }
96
97     fn fold_decl(&mut self, d: P<Decl>) -> P<Decl> {
98         noop_fold_decl(d, self)
99     }
100
101     fn fold_expr(&mut self, e: P<Expr>) -> P<Expr> {
102         e.map(|e| noop_fold_expr(e, self))
103     }
104
105     fn fold_ty(&mut self, t: P<Ty>) -> P<Ty> {
106         noop_fold_ty(t, self)
107     }
108
109     fn fold_ty_binding(&mut self, t: TypeBinding) -> TypeBinding {
110         noop_fold_ty_binding(t, self)
111     }
112
113     fn fold_mod(&mut self, m: Mod) -> Mod {
114         noop_fold_mod(m, self)
115     }
116
117     fn fold_foreign_mod(&mut self, nm: ForeignMod) -> ForeignMod {
118         noop_fold_foreign_mod(nm, self)
119     }
120
121     fn fold_variant(&mut self, v: Variant) -> Variant {
122         noop_fold_variant(v, self)
123     }
124
125     fn fold_name(&mut self, n: Name) -> Name {
126         noop_fold_name(n, self)
127     }
128
129     fn fold_usize(&mut self, i: usize) -> usize {
130         noop_fold_usize(i, self)
131     }
132
133     fn fold_path(&mut self, p: Path) -> Path {
134         noop_fold_path(p, self)
135     }
136
137     fn fold_path_parameters(&mut self, p: PathParameters) -> PathParameters {
138         noop_fold_path_parameters(p, self)
139     }
140
141     fn fold_angle_bracketed_parameter_data(&mut self,
142                                            p: AngleBracketedParameterData)
143                                            -> AngleBracketedParameterData {
144         noop_fold_angle_bracketed_parameter_data(p, self)
145     }
146
147     fn fold_parenthesized_parameter_data(&mut self,
148                                          p: ParenthesizedParameterData)
149                                          -> ParenthesizedParameterData {
150         noop_fold_parenthesized_parameter_data(p, self)
151     }
152
153     fn fold_local(&mut self, l: P<Local>) -> P<Local> {
154         noop_fold_local(l, self)
155     }
156
157     fn fold_lifetime(&mut self, l: Lifetime) -> Lifetime {
158         noop_fold_lifetime(l, self)
159     }
160
161     fn fold_lifetime_def(&mut self, l: LifetimeDef) -> LifetimeDef {
162         noop_fold_lifetime_def(l, self)
163     }
164
165     fn fold_attribute(&mut self, at: Attribute) -> Option<Attribute> {
166         noop_fold_attribute(at, self)
167     }
168
169     fn fold_arg(&mut self, a: Arg) -> Arg {
170         noop_fold_arg(a, self)
171     }
172
173     fn fold_generics(&mut self, generics: Generics) -> Generics {
174         noop_fold_generics(generics, self)
175     }
176
177     fn fold_trait_ref(&mut self, p: TraitRef) -> TraitRef {
178         noop_fold_trait_ref(p, self)
179     }
180
181     fn fold_poly_trait_ref(&mut self, p: PolyTraitRef) -> PolyTraitRef {
182         noop_fold_poly_trait_ref(p, self)
183     }
184
185     fn fold_variant_data(&mut self, vdata: VariantData) -> VariantData {
186         noop_fold_variant_data(vdata, self)
187     }
188
189     fn fold_lifetimes(&mut self, lts: HirVec<Lifetime>) -> HirVec<Lifetime> {
190         noop_fold_lifetimes(lts, self)
191     }
192
193     fn fold_lifetime_defs(&mut self, lts: HirVec<LifetimeDef>) -> HirVec<LifetimeDef> {
194         noop_fold_lifetime_defs(lts, self)
195     }
196
197     fn fold_ty_param(&mut self, tp: TyParam) -> TyParam {
198         noop_fold_ty_param(tp, self)
199     }
200
201     fn fold_ty_params(&mut self, tps: HirVec<TyParam>) -> HirVec<TyParam> {
202         noop_fold_ty_params(tps, self)
203     }
204
205     fn fold_opt_lifetime(&mut self, o_lt: Option<Lifetime>) -> Option<Lifetime> {
206         noop_fold_opt_lifetime(o_lt, self)
207     }
208
209     fn fold_opt_bounds(&mut self,
210                        b: Option<TyParamBounds>)
211                        -> Option<TyParamBounds> {
212         noop_fold_opt_bounds(b, self)
213     }
214
215     fn fold_bounds(&mut self, b: TyParamBounds) -> TyParamBounds {
216         noop_fold_bounds(b, self)
217     }
218
219     fn fold_ty_param_bound(&mut self, tpb: TyParamBound) -> TyParamBound {
220         noop_fold_ty_param_bound(tpb, self)
221     }
222
223     fn fold_mt(&mut self, mt: MutTy) -> MutTy {
224         noop_fold_mt(mt, self)
225     }
226
227     fn fold_field(&mut self, field: Field) -> Field {
228         noop_fold_field(field, self)
229     }
230
231     fn fold_where_clause(&mut self, where_clause: WhereClause) -> WhereClause {
232         noop_fold_where_clause(where_clause, self)
233     }
234
235     fn fold_where_predicate(&mut self, where_predicate: WherePredicate) -> WherePredicate {
236         noop_fold_where_predicate(where_predicate, self)
237     }
238
239     /// called for the `id` on each declaration
240     fn new_id(&mut self, i: NodeId) -> NodeId {
241         i
242     }
243
244     /// called for ids that are references (e.g., ItemDef)
245     fn map_id(&mut self, i: NodeId) -> NodeId {
246         i
247     }
248
249     fn new_span(&mut self, sp: Span) -> Span {
250         sp
251     }
252 }
253
254 pub fn noop_fold_meta_items<T: Folder>(meta_items: HirVec<P<MetaItem>>,
255                                        fld: &mut T)
256                                        -> HirVec<P<MetaItem>> {
257     meta_items.move_map(|x| fld.fold_meta_item(x))
258 }
259
260 pub fn noop_fold_view_path<T: Folder>(view_path: P<ViewPath>, fld: &mut T) -> P<ViewPath> {
261     view_path.map(|Spanned { node, span }| {
262         Spanned {
263             node: match node {
264                 ViewPathSimple(name, path) => {
265                     ViewPathSimple(name, fld.fold_path(path))
266                 }
267                 ViewPathGlob(path) => {
268                     ViewPathGlob(fld.fold_path(path))
269                 }
270                 ViewPathList(path, path_list_idents) => {
271                     ViewPathList(fld.fold_path(path),
272                                  path_list_idents.move_map(|path_list_ident| {
273                                      Spanned {
274                                          node: match path_list_ident.node {
275                                              PathListIdent { id, name, rename } => PathListIdent {
276                                                  id: fld.new_id(id),
277                                                  name: name,
278                                                  rename: rename,
279                                              },
280                                              PathListMod { id, rename } => PathListMod {
281                                                  id: fld.new_id(id),
282                                                  rename: rename,
283                                              },
284                                          },
285                                          span: fld.new_span(path_list_ident.span),
286                                      }
287                                  }))
288                 }
289             },
290             span: fld.new_span(span),
291         }
292     })
293 }
294
295 pub fn fold_attrs<T, F>(attrs: T, fld: &mut F) -> T
296     where T: Into<Vec<Attribute>> + From<Vec<Attribute>>,
297           F: Folder,
298 {
299     attrs.into().move_flat_map(|x| fld.fold_attribute(x)).into()
300 }
301
302 pub fn noop_fold_arm<T: Folder>(Arm { attrs, pats, guard, body }: Arm, fld: &mut T) -> Arm {
303     Arm {
304         attrs: fold_attrs(attrs, fld),
305         pats: pats.move_map(|x| fld.fold_pat(x)),
306         guard: guard.map(|x| fld.fold_expr(x)),
307         body: fld.fold_expr(body),
308     }
309 }
310
311 pub fn noop_fold_decl<T: Folder>(d: P<Decl>, fld: &mut T) -> P<Decl> {
312     d.map(|Spanned { node, span }| {
313         match node {
314             DeclLocal(l) => Spanned {
315                 node: DeclLocal(fld.fold_local(l)),
316                 span: fld.new_span(span),
317             },
318             DeclItem(it) => Spanned {
319                 node: DeclItem(fld.fold_item_id(it)),
320                 span: fld.new_span(span),
321             },
322         }
323     })
324 }
325
326 pub fn noop_fold_ty_binding<T: Folder>(b: TypeBinding, fld: &mut T) -> TypeBinding {
327     TypeBinding {
328         id: fld.new_id(b.id),
329         name: b.name,
330         ty: fld.fold_ty(b.ty),
331         span: fld.new_span(b.span),
332     }
333 }
334
335 pub fn noop_fold_ty<T: Folder>(t: P<Ty>, fld: &mut T) -> P<Ty> {
336     t.map(|Ty { id, node, span }| {
337         Ty {
338             id: fld.new_id(id),
339             node: match node {
340                 TyInfer => node,
341                 TyVec(ty) => TyVec(fld.fold_ty(ty)),
342                 TyPtr(mt) => TyPtr(fld.fold_mt(mt)),
343                 TyRptr(region, mt) => {
344                     TyRptr(fld.fold_opt_lifetime(region), fld.fold_mt(mt))
345                 }
346                 TyBareFn(f) => {
347                     TyBareFn(f.map(|BareFnTy { lifetimes, unsafety, abi, decl }| {
348                         BareFnTy {
349                             lifetimes: fld.fold_lifetime_defs(lifetimes),
350                             unsafety: unsafety,
351                             abi: abi,
352                             decl: fld.fold_fn_decl(decl),
353                         }
354                     }))
355                 }
356                 TyNever => node,
357                 TyTup(tys) => TyTup(tys.move_map(|ty| fld.fold_ty(ty))),
358                 TyPath(qself, path) => {
359                     let qself = qself.map(|QSelf { ty, position }| {
360                         QSelf {
361                             ty: fld.fold_ty(ty),
362                             position: position,
363                         }
364                     });
365                     TyPath(qself, fld.fold_path(path))
366                 }
367                 TyObjectSum(ty, bounds) => {
368                     TyObjectSum(fld.fold_ty(ty), fld.fold_bounds(bounds))
369                 }
370                 TyFixedLengthVec(ty, e) => {
371                     TyFixedLengthVec(fld.fold_ty(ty), fld.fold_expr(e))
372                 }
373                 TyTypeof(expr) => {
374                     TyTypeof(fld.fold_expr(expr))
375                 }
376                 TyPolyTraitRef(bounds) => {
377                     TyPolyTraitRef(bounds.move_map(|b| fld.fold_ty_param_bound(b)))
378                 }
379                 TyImplTrait(bounds) => {
380                     TyImplTrait(bounds.move_map(|b| fld.fold_ty_param_bound(b)))
381                 }
382             },
383             span: fld.new_span(span),
384         }
385     })
386 }
387
388 pub fn noop_fold_foreign_mod<T: Folder>(ForeignMod { abi, items }: ForeignMod,
389                                         fld: &mut T)
390                                         -> ForeignMod {
391     ForeignMod {
392         abi: abi,
393         items: items.move_map(|x| fld.fold_foreign_item(x)),
394     }
395 }
396
397 pub fn noop_fold_variant<T: Folder>(v: Variant, fld: &mut T) -> Variant {
398     Spanned {
399         node: Variant_ {
400             name: v.node.name,
401             attrs: fold_attrs(v.node.attrs, fld),
402             data: fld.fold_variant_data(v.node.data),
403             disr_expr: v.node.disr_expr.map(|e| fld.fold_expr(e)),
404         },
405         span: fld.new_span(v.span),
406     }
407 }
408
409 pub fn noop_fold_name<T: Folder>(n: Name, _: &mut T) -> Name {
410     n
411 }
412
413 pub fn noop_fold_usize<T: Folder>(i: usize, _: &mut T) -> usize {
414     i
415 }
416
417 pub fn noop_fold_path<T: Folder>(Path { global, segments, span }: Path, fld: &mut T) -> Path {
418     Path {
419         global: global,
420         segments: segments.move_map(|PathSegment { name, parameters }| {
421             PathSegment {
422                 name: fld.fold_name(name),
423                 parameters: fld.fold_path_parameters(parameters),
424             }
425         }),
426         span: fld.new_span(span),
427     }
428 }
429
430 pub fn noop_fold_path_parameters<T: Folder>(path_parameters: PathParameters,
431                                             fld: &mut T)
432                                             -> PathParameters {
433     match path_parameters {
434         AngleBracketedParameters(data) =>
435             AngleBracketedParameters(fld.fold_angle_bracketed_parameter_data(data)),
436         ParenthesizedParameters(data) =>
437             ParenthesizedParameters(fld.fold_parenthesized_parameter_data(data)),
438     }
439 }
440
441 pub fn noop_fold_angle_bracketed_parameter_data<T: Folder>(data: AngleBracketedParameterData,
442                                                            fld: &mut T)
443                                                            -> AngleBracketedParameterData {
444     let AngleBracketedParameterData { lifetimes, types, bindings } = data;
445     AngleBracketedParameterData {
446         lifetimes: fld.fold_lifetimes(lifetimes),
447         types: types.move_map(|ty| fld.fold_ty(ty)),
448         bindings: bindings.move_map(|b| fld.fold_ty_binding(b)),
449     }
450 }
451
452 pub fn noop_fold_parenthesized_parameter_data<T: Folder>(data: ParenthesizedParameterData,
453                                                          fld: &mut T)
454                                                          -> ParenthesizedParameterData {
455     let ParenthesizedParameterData { inputs, output, span } = data;
456     ParenthesizedParameterData {
457         inputs: inputs.move_map(|ty| fld.fold_ty(ty)),
458         output: output.map(|ty| fld.fold_ty(ty)),
459         span: fld.new_span(span),
460     }
461 }
462
463 pub fn noop_fold_local<T: Folder>(l: P<Local>, fld: &mut T) -> P<Local> {
464     l.map(|Local { id, pat, ty, init, span, attrs }| {
465         Local {
466             id: fld.new_id(id),
467             ty: ty.map(|t| fld.fold_ty(t)),
468             pat: fld.fold_pat(pat),
469             init: init.map(|e| fld.fold_expr(e)),
470             span: fld.new_span(span),
471             attrs: fold_attrs(attrs, fld),
472         }
473     })
474 }
475
476 pub fn noop_fold_attribute<T: Folder>(at: Attribute, fld: &mut T) -> Option<Attribute> {
477     let Spanned {node: Attribute_ {id, style, value, is_sugared_doc}, span} = at;
478     Some(Spanned {
479         node: Attribute_ {
480             id: id,
481             style: style,
482             value: fld.fold_meta_item(value),
483             is_sugared_doc: is_sugared_doc,
484         },
485         span: fld.new_span(span),
486     })
487 }
488
489 pub fn noop_fold_meta_item<T: Folder>(mi: P<MetaItem>, fld: &mut T) -> P<MetaItem> {
490     mi.map(|Spanned { node, span }| {
491         Spanned {
492             node: match node {
493                 MetaItemKind::Word(id) => MetaItemKind::Word(id),
494                 MetaItemKind::List(id, mis) => {
495                     MetaItemKind::List(id, mis.move_map(|e| fld.fold_meta_item(e)))
496                 }
497                 MetaItemKind::NameValue(id, s) => MetaItemKind::NameValue(id, s),
498             },
499             span: fld.new_span(span),
500         }
501     })
502 }
503
504 pub fn noop_fold_arg<T: Folder>(Arg { id, pat, ty }: Arg, fld: &mut T) -> Arg {
505     Arg {
506         id: fld.new_id(id),
507         pat: fld.fold_pat(pat),
508         ty: fld.fold_ty(ty),
509     }
510 }
511
512 pub fn noop_fold_fn_decl<T: Folder>(decl: P<FnDecl>, fld: &mut T) -> P<FnDecl> {
513     decl.map(|FnDecl { inputs, output, variadic }| {
514         FnDecl {
515             inputs: inputs.move_map(|x| fld.fold_arg(x)),
516             output: match output {
517                 Return(ty) => Return(fld.fold_ty(ty)),
518                 DefaultReturn(span) => DefaultReturn(span),
519             },
520             variadic: variadic,
521         }
522     })
523 }
524
525 pub fn noop_fold_ty_param_bound<T>(tpb: TyParamBound, fld: &mut T) -> TyParamBound
526     where T: Folder
527 {
528     match tpb {
529         TraitTyParamBound(ty, modifier) => TraitTyParamBound(fld.fold_poly_trait_ref(ty), modifier),
530         RegionTyParamBound(lifetime) => RegionTyParamBound(fld.fold_lifetime(lifetime)),
531     }
532 }
533
534 pub fn noop_fold_ty_param<T: Folder>(tp: TyParam, fld: &mut T) -> TyParam {
535     let TyParam {id, name, bounds, default, span} = tp;
536     TyParam {
537         id: fld.new_id(id),
538         name: name,
539         bounds: fld.fold_bounds(bounds),
540         default: default.map(|x| fld.fold_ty(x)),
541         span: span,
542     }
543 }
544
545 pub fn noop_fold_ty_params<T: Folder>(tps: HirVec<TyParam>,
546                                       fld: &mut T)
547                                       -> HirVec<TyParam> {
548     tps.move_map(|tp| fld.fold_ty_param(tp))
549 }
550
551 pub fn noop_fold_lifetime<T: Folder>(l: Lifetime, fld: &mut T) -> Lifetime {
552     Lifetime {
553         id: fld.new_id(l.id),
554         name: l.name,
555         span: fld.new_span(l.span),
556     }
557 }
558
559 pub fn noop_fold_lifetime_def<T: Folder>(l: LifetimeDef, fld: &mut T) -> LifetimeDef {
560     LifetimeDef {
561         lifetime: fld.fold_lifetime(l.lifetime),
562         bounds: fld.fold_lifetimes(l.bounds),
563     }
564 }
565
566 pub fn noop_fold_lifetimes<T: Folder>(lts: HirVec<Lifetime>, fld: &mut T) -> HirVec<Lifetime> {
567     lts.move_map(|l| fld.fold_lifetime(l))
568 }
569
570 pub fn noop_fold_lifetime_defs<T: Folder>(lts: HirVec<LifetimeDef>,
571                                           fld: &mut T)
572                                           -> HirVec<LifetimeDef> {
573     lts.move_map(|l| fld.fold_lifetime_def(l))
574 }
575
576 pub fn noop_fold_opt_lifetime<T: Folder>(o_lt: Option<Lifetime>, fld: &mut T) -> Option<Lifetime> {
577     o_lt.map(|lt| fld.fold_lifetime(lt))
578 }
579
580 pub fn noop_fold_generics<T: Folder>(Generics { ty_params, lifetimes, where_clause }: Generics,
581                                      fld: &mut T)
582                                      -> Generics {
583     Generics {
584         ty_params: fld.fold_ty_params(ty_params),
585         lifetimes: fld.fold_lifetime_defs(lifetimes),
586         where_clause: fld.fold_where_clause(where_clause),
587     }
588 }
589
590 pub fn noop_fold_where_clause<T: Folder>(WhereClause { id, predicates }: WhereClause,
591                                          fld: &mut T)
592                                          -> WhereClause {
593     WhereClause {
594         id: fld.new_id(id),
595         predicates: predicates.move_map(|predicate| fld.fold_where_predicate(predicate)),
596     }
597 }
598
599 pub fn noop_fold_where_predicate<T: Folder>(pred: WherePredicate, fld: &mut T) -> WherePredicate {
600     match pred {
601         hir::WherePredicate::BoundPredicate(hir::WhereBoundPredicate{bound_lifetimes,
602                                                                      bounded_ty,
603                                                                      bounds,
604                                                                      span}) => {
605             hir::WherePredicate::BoundPredicate(hir::WhereBoundPredicate {
606                 bound_lifetimes: fld.fold_lifetime_defs(bound_lifetimes),
607                 bounded_ty: fld.fold_ty(bounded_ty),
608                 bounds: bounds.move_map(|x| fld.fold_ty_param_bound(x)),
609                 span: fld.new_span(span),
610             })
611         }
612         hir::WherePredicate::RegionPredicate(hir::WhereRegionPredicate{lifetime,
613                                                                        bounds,
614                                                                        span}) => {
615             hir::WherePredicate::RegionPredicate(hir::WhereRegionPredicate {
616                 span: fld.new_span(span),
617                 lifetime: fld.fold_lifetime(lifetime),
618                 bounds: bounds.move_map(|bound| fld.fold_lifetime(bound)),
619             })
620         }
621         hir::WherePredicate::EqPredicate(hir::WhereEqPredicate{id,
622                                                                path,
623                                                                ty,
624                                                                span}) => {
625             hir::WherePredicate::EqPredicate(hir::WhereEqPredicate {
626                 id: fld.new_id(id),
627                 path: fld.fold_path(path),
628                 ty: fld.fold_ty(ty),
629                 span: fld.new_span(span),
630             })
631         }
632     }
633 }
634
635 pub fn noop_fold_variant_data<T: Folder>(vdata: VariantData, fld: &mut T) -> VariantData {
636     match vdata {
637         VariantData::Struct(fields, id) => {
638             VariantData::Struct(fields.move_map(|f| fld.fold_struct_field(f)),
639                                 fld.new_id(id))
640         }
641         VariantData::Tuple(fields, id) => {
642             VariantData::Tuple(fields.move_map(|f| fld.fold_struct_field(f)),
643                                fld.new_id(id))
644         }
645         VariantData::Unit(id) => VariantData::Unit(fld.new_id(id)),
646     }
647 }
648
649 pub fn noop_fold_trait_ref<T: Folder>(p: TraitRef, fld: &mut T) -> TraitRef {
650     let id = fld.new_id(p.ref_id);
651     let TraitRef {
652         path,
653         ref_id: _,
654     } = p;
655     hir::TraitRef {
656         path: fld.fold_path(path),
657         ref_id: id,
658     }
659 }
660
661 pub fn noop_fold_poly_trait_ref<T: Folder>(p: PolyTraitRef, fld: &mut T) -> PolyTraitRef {
662     hir::PolyTraitRef {
663         bound_lifetimes: fld.fold_lifetime_defs(p.bound_lifetimes),
664         trait_ref: fld.fold_trait_ref(p.trait_ref),
665         span: fld.new_span(p.span),
666     }
667 }
668
669 pub fn noop_fold_struct_field<T: Folder>(f: StructField, fld: &mut T) -> StructField {
670     StructField {
671         span: fld.new_span(f.span),
672         id: fld.new_id(f.id),
673         name: f.name,
674         vis: f.vis,
675         ty: fld.fold_ty(f.ty),
676         attrs: fold_attrs(f.attrs, fld),
677     }
678 }
679
680 pub fn noop_fold_field<T: Folder>(Field { name, expr, span }: Field, folder: &mut T) -> Field {
681     Field {
682         name: respan(folder.new_span(name.span), folder.fold_name(name.node)),
683         expr: folder.fold_expr(expr),
684         span: folder.new_span(span),
685     }
686 }
687
688 pub fn noop_fold_mt<T: Folder>(MutTy { ty, mutbl }: MutTy, folder: &mut T) -> MutTy {
689     MutTy {
690         ty: folder.fold_ty(ty),
691         mutbl: mutbl,
692     }
693 }
694
695 pub fn noop_fold_opt_bounds<T: Folder>(b: Option<TyParamBounds>,
696                                        folder: &mut T)
697                                        -> Option<TyParamBounds> {
698     b.map(|bounds| folder.fold_bounds(bounds))
699 }
700
701 fn noop_fold_bounds<T: Folder>(bounds: TyParamBounds, folder: &mut T) -> TyParamBounds {
702     bounds.move_map(|bound| folder.fold_ty_param_bound(bound))
703 }
704
705 pub fn noop_fold_block<T: Folder>(b: P<Block>, folder: &mut T) -> P<Block> {
706     b.map(|Block { id, stmts, expr, rules, span }| {
707         Block {
708             id: folder.new_id(id),
709             stmts: stmts.move_map(|s| folder.fold_stmt(s)),
710             expr: expr.map(|x| folder.fold_expr(x)),
711             rules: rules,
712             span: folder.new_span(span),
713         }
714     })
715 }
716
717 pub fn noop_fold_item_underscore<T: Folder>(i: Item_, folder: &mut T) -> Item_ {
718     match i {
719         ItemExternCrate(string) => ItemExternCrate(string),
720         ItemUse(view_path) => {
721             ItemUse(folder.fold_view_path(view_path))
722         }
723         ItemStatic(t, m, e) => {
724             ItemStatic(folder.fold_ty(t), m, folder.fold_expr(e))
725         }
726         ItemConst(t, e) => {
727             ItemConst(folder.fold_ty(t), folder.fold_expr(e))
728         }
729         ItemFn(decl, unsafety, constness, abi, generics, body) => {
730             ItemFn(folder.fold_fn_decl(decl),
731                    unsafety,
732                    constness,
733                    abi,
734                    folder.fold_generics(generics),
735                    folder.fold_block(body))
736         }
737         ItemMod(m) => ItemMod(folder.fold_mod(m)),
738         ItemForeignMod(nm) => ItemForeignMod(folder.fold_foreign_mod(nm)),
739         ItemTy(t, generics) => {
740             ItemTy(folder.fold_ty(t), folder.fold_generics(generics))
741         }
742         ItemEnum(enum_definition, generics) => {
743             ItemEnum(hir::EnumDef {
744                          variants: enum_definition.variants.move_map(|x| folder.fold_variant(x)),
745                      },
746                      folder.fold_generics(generics))
747         }
748         ItemStruct(struct_def, generics) => {
749             let struct_def = folder.fold_variant_data(struct_def);
750             ItemStruct(struct_def, folder.fold_generics(generics))
751         }
752         ItemDefaultImpl(unsafety, ref trait_ref) => {
753             ItemDefaultImpl(unsafety, folder.fold_trait_ref((*trait_ref).clone()))
754         }
755         ItemImpl(unsafety, polarity, generics, ifce, ty, impl_items) => {
756             let new_impl_items = impl_items
757                 .move_map(|item| folder.fold_impl_item(item));
758             let ifce = match ifce {
759                 None => None,
760                 Some(ref trait_ref) => {
761                     Some(folder.fold_trait_ref((*trait_ref).clone()))
762                 }
763             };
764             ItemImpl(unsafety,
765                      polarity,
766                      folder.fold_generics(generics),
767                      ifce,
768                      folder.fold_ty(ty),
769                      new_impl_items)
770         }
771         ItemTrait(unsafety, generics, bounds, items) => {
772             let bounds = folder.fold_bounds(bounds);
773             let items = items.move_map(|item| folder.fold_trait_item(item));
774             ItemTrait(unsafety, folder.fold_generics(generics), bounds, items)
775         }
776     }
777 }
778
779 pub fn noop_fold_trait_item<T: Folder>(i: TraitItem,
780                                        folder: &mut T)
781                                        -> TraitItem {
782     TraitItem {
783         id: folder.new_id(i.id),
784         name: folder.fold_name(i.name),
785         attrs: fold_attrs(i.attrs, folder),
786         node: match i.node {
787             ConstTraitItem(ty, default) => {
788                 ConstTraitItem(folder.fold_ty(ty), default.map(|x| folder.fold_expr(x)))
789             }
790             MethodTraitItem(sig, body) => {
791                 MethodTraitItem(noop_fold_method_sig(sig, folder),
792                                 body.map(|x| folder.fold_block(x)))
793             }
794             TypeTraitItem(bounds, default) => {
795                 TypeTraitItem(folder.fold_bounds(bounds),
796                               default.map(|x| folder.fold_ty(x)))
797             }
798         },
799         span: folder.new_span(i.span),
800     }
801 }
802
803 pub fn noop_fold_impl_item<T: Folder>(i: ImplItem, folder: &mut T) -> ImplItem {
804     ImplItem {
805         id: folder.new_id(i.id),
806         name: folder.fold_name(i.name),
807         attrs: fold_attrs(i.attrs, folder),
808         vis: i.vis,
809         defaultness: i.defaultness,
810         node: match i.node {
811             ImplItemKind::Const(ty, expr) => {
812                 ImplItemKind::Const(folder.fold_ty(ty), folder.fold_expr(expr))
813             }
814             ImplItemKind::Method(sig, body) => {
815                 ImplItemKind::Method(noop_fold_method_sig(sig, folder), folder.fold_block(body))
816             }
817             ImplItemKind::Type(ty) => ImplItemKind::Type(folder.fold_ty(ty)),
818         },
819         span: folder.new_span(i.span),
820     }
821 }
822
823 pub fn noop_fold_mod<T: Folder>(Mod { inner, item_ids }: Mod, folder: &mut T) -> Mod {
824     Mod {
825         inner: folder.new_span(inner),
826         item_ids: item_ids.move_map(|x| folder.fold_item_id(x)),
827     }
828 }
829
830 pub fn noop_fold_crate<T: Folder>(Crate { module, attrs, config, span,
831                                           exported_macros, items }: Crate,
832                                   folder: &mut T)
833                                   -> Crate {
834     let config = folder.fold_meta_items(config);
835
836     let crate_mod = folder.fold_item(hir::Item {
837         name: keywords::Invalid.name(),
838         attrs: attrs,
839         id: DUMMY_NODE_ID,
840         vis: hir::Public,
841         span: span,
842         node: hir::ItemMod(module),
843     });
844
845     let (module, attrs, span) = match crate_mod {
846         hir::Item { attrs, span, node, .. } => {
847             match node {
848                 hir::ItemMod(m) => (m, attrs, span),
849                 _ => panic!("fold converted a module to not a module"),
850             }
851         }
852     };
853
854     let items = items.into_iter()
855                      .map(|(id, item)| (id, folder.fold_item(item)))
856                      .collect();
857
858     Crate {
859         module: module,
860         attrs: attrs,
861         config: config,
862         span: span,
863         exported_macros: exported_macros,
864         items: items,
865     }
866 }
867
868 pub fn noop_fold_item_id<T: Folder>(i: ItemId, folder: &mut T) -> ItemId {
869     let id = folder.map_id(i.id);
870     ItemId { id: id }
871 }
872
873 // fold one item into one item
874 pub fn noop_fold_item<T: Folder>(item: Item, folder: &mut T) -> Item {
875     let Item { id, name, attrs, node, vis, span } = item;
876     let id = folder.new_id(id);
877     let node = folder.fold_item_underscore(node);
878
879     Item {
880         id: id,
881         name: folder.fold_name(name),
882         attrs: fold_attrs(attrs, folder),
883         node: node,
884         vis: vis,
885         span: folder.new_span(span),
886     }
887 }
888
889 pub fn noop_fold_foreign_item<T: Folder>(ni: ForeignItem, folder: &mut T) -> ForeignItem {
890     ForeignItem {
891         id: folder.new_id(ni.id),
892         name: folder.fold_name(ni.name),
893         attrs: fold_attrs(ni.attrs, folder),
894         node: match ni.node {
895             ForeignItemFn(fdec, generics) => {
896                 ForeignItemFn(folder.fold_fn_decl(fdec), folder.fold_generics(generics))
897             }
898             ForeignItemStatic(t, m) => {
899                 ForeignItemStatic(folder.fold_ty(t), m)
900             }
901         },
902         vis: ni.vis,
903         span: folder.new_span(ni.span),
904     }
905 }
906
907 pub fn noop_fold_method_sig<T: Folder>(sig: MethodSig, folder: &mut T) -> MethodSig {
908     MethodSig {
909         generics: folder.fold_generics(sig.generics),
910         abi: sig.abi,
911         unsafety: sig.unsafety,
912         constness: sig.constness,
913         decl: folder.fold_fn_decl(sig.decl),
914     }
915 }
916
917 pub fn noop_fold_pat<T: Folder>(p: P<Pat>, folder: &mut T) -> P<Pat> {
918     p.map(|Pat { id, node, span }| {
919         Pat {
920             id: folder.new_id(id),
921             node: match node {
922                 PatKind::Wild => PatKind::Wild,
923                 PatKind::Binding(binding_mode, pth1, sub) => {
924                     PatKind::Binding(binding_mode,
925                              Spanned {
926                                  span: folder.new_span(pth1.span),
927                                  node: folder.fold_name(pth1.node),
928                              },
929                              sub.map(|x| folder.fold_pat(x)))
930                 }
931                 PatKind::Lit(e) => PatKind::Lit(folder.fold_expr(e)),
932                 PatKind::TupleStruct(pth, pats, ddpos) => {
933                     PatKind::TupleStruct(folder.fold_path(pth),
934                             pats.move_map(|x| folder.fold_pat(x)), ddpos)
935                 }
936                 PatKind::Path(opt_qself, pth) => {
937                     let opt_qself = opt_qself.map(|qself| {
938                         QSelf { ty: folder.fold_ty(qself.ty), position: qself.position }
939                     });
940                     PatKind::Path(opt_qself, folder.fold_path(pth))
941                 }
942                 PatKind::Struct(pth, fields, etc) => {
943                     let pth = folder.fold_path(pth);
944                     let fs = fields.move_map(|f| {
945                         Spanned {
946                             span: folder.new_span(f.span),
947                             node: hir::FieldPat {
948                                 name: f.node.name,
949                                 pat: folder.fold_pat(f.node.pat),
950                                 is_shorthand: f.node.is_shorthand,
951                             },
952                         }
953                     });
954                     PatKind::Struct(pth, fs, etc)
955                 }
956                 PatKind::Tuple(elts, ddpos) => {
957                     PatKind::Tuple(elts.move_map(|x| folder.fold_pat(x)), ddpos)
958                 }
959                 PatKind::Box(inner) => PatKind::Box(folder.fold_pat(inner)),
960                 PatKind::Ref(inner, mutbl) => PatKind::Ref(folder.fold_pat(inner), mutbl),
961                 PatKind::Range(e1, e2) => {
962                     PatKind::Range(folder.fold_expr(e1), folder.fold_expr(e2))
963                 }
964                 PatKind::Vec(before, slice, after) => {
965                     PatKind::Vec(before.move_map(|x| folder.fold_pat(x)),
966                            slice.map(|x| folder.fold_pat(x)),
967                            after.move_map(|x| folder.fold_pat(x)))
968                 }
969             },
970             span: folder.new_span(span),
971         }
972     })
973 }
974
975 pub fn noop_fold_expr<T: Folder>(Expr { id, node, span, attrs }: Expr, folder: &mut T) -> Expr {
976     Expr {
977         id: folder.new_id(id),
978         node: match node {
979             ExprBox(e) => {
980                 ExprBox(folder.fold_expr(e))
981             }
982             ExprVec(exprs) => {
983                 ExprVec(exprs.move_map(|x| folder.fold_expr(x)))
984             }
985             ExprRepeat(expr, count) => {
986                 ExprRepeat(folder.fold_expr(expr), folder.fold_expr(count))
987             }
988             ExprTup(elts) => ExprTup(elts.move_map(|x| folder.fold_expr(x))),
989             ExprCall(f, args) => {
990                 ExprCall(folder.fold_expr(f), args.move_map(|x| folder.fold_expr(x)))
991             }
992             ExprMethodCall(name, tps, args) => {
993                 ExprMethodCall(respan(folder.new_span(name.span), folder.fold_name(name.node)),
994                                tps.move_map(|x| folder.fold_ty(x)),
995                                args.move_map(|x| folder.fold_expr(x)))
996             }
997             ExprBinary(binop, lhs, rhs) => {
998                 ExprBinary(binop, folder.fold_expr(lhs), folder.fold_expr(rhs))
999             }
1000             ExprUnary(binop, ohs) => {
1001                 ExprUnary(binop, folder.fold_expr(ohs))
1002             }
1003             ExprLit(l) => ExprLit(l),
1004             ExprCast(expr, ty) => {
1005                 ExprCast(folder.fold_expr(expr), folder.fold_ty(ty))
1006             }
1007             ExprType(expr, ty) => {
1008                 ExprType(folder.fold_expr(expr), folder.fold_ty(ty))
1009             }
1010             ExprAddrOf(m, ohs) => ExprAddrOf(m, folder.fold_expr(ohs)),
1011             ExprIf(cond, tr, fl) => {
1012                 ExprIf(folder.fold_expr(cond),
1013                        folder.fold_block(tr),
1014                        fl.map(|x| folder.fold_expr(x)))
1015             }
1016             ExprWhile(cond, body, opt_name) => {
1017                 ExprWhile(folder.fold_expr(cond),
1018                           folder.fold_block(body),
1019                           opt_name.map(|label| {
1020                               respan(folder.new_span(label.span), folder.fold_name(label.node))
1021                           }))
1022             }
1023             ExprLoop(body, opt_name) => {
1024                 ExprLoop(folder.fold_block(body),
1025                          opt_name.map(|label| {
1026                              respan(folder.new_span(label.span), folder.fold_name(label.node))
1027                          }))
1028             }
1029             ExprMatch(expr, arms, source) => {
1030                 ExprMatch(folder.fold_expr(expr),
1031                           arms.move_map(|x| folder.fold_arm(x)),
1032                           source)
1033             }
1034             ExprClosure(capture_clause, decl, body, fn_decl_span) => {
1035                 ExprClosure(capture_clause,
1036                             folder.fold_fn_decl(decl),
1037                             folder.fold_block(body),
1038                             folder.new_span(fn_decl_span))
1039             }
1040             ExprBlock(blk) => ExprBlock(folder.fold_block(blk)),
1041             ExprAssign(el, er) => {
1042                 ExprAssign(folder.fold_expr(el), folder.fold_expr(er))
1043             }
1044             ExprAssignOp(op, el, er) => {
1045                 ExprAssignOp(op, folder.fold_expr(el), folder.fold_expr(er))
1046             }
1047             ExprField(el, name) => {
1048                 ExprField(folder.fold_expr(el),
1049                           respan(folder.new_span(name.span), folder.fold_name(name.node)))
1050             }
1051             ExprTupField(el, index) => {
1052                 ExprTupField(folder.fold_expr(el),
1053                              respan(folder.new_span(index.span), folder.fold_usize(index.node)))
1054             }
1055             ExprIndex(el, er) => {
1056                 ExprIndex(folder.fold_expr(el), folder.fold_expr(er))
1057             }
1058             ExprPath(qself, path) => {
1059                 let qself = qself.map(|QSelf { ty, position }| {
1060                     QSelf {
1061                         ty: folder.fold_ty(ty),
1062                         position: position,
1063                     }
1064                 });
1065                 ExprPath(qself, folder.fold_path(path))
1066             }
1067             ExprBreak(opt_name) => ExprBreak(opt_name.map(|label| {
1068                 respan(folder.new_span(label.span), folder.fold_name(label.node))
1069             })),
1070             ExprAgain(opt_name) => ExprAgain(opt_name.map(|label| {
1071                 respan(folder.new_span(label.span), folder.fold_name(label.node))
1072             })),
1073             ExprRet(e) => ExprRet(e.map(|x| folder.fold_expr(x))),
1074             ExprInlineAsm(asm, outputs, inputs) => {
1075                 ExprInlineAsm(asm,
1076                               outputs.move_map(|x| folder.fold_expr(x)),
1077                               inputs.move_map(|x| folder.fold_expr(x)))
1078             }
1079             ExprStruct(path, fields, maybe_expr) => {
1080                 ExprStruct(folder.fold_path(path),
1081                            fields.move_map(|x| folder.fold_field(x)),
1082                            maybe_expr.map(|x| folder.fold_expr(x)))
1083             }
1084         },
1085         span: folder.new_span(span),
1086         attrs: fold_attrs(attrs, folder),
1087     }
1088 }
1089
1090 pub fn noop_fold_stmt<T: Folder>(stmt: Stmt, folder: &mut T) -> Stmt {
1091     let span = folder.new_span(stmt.span);
1092     match stmt.node {
1093         StmtDecl(d, id) => {
1094             let id = folder.new_id(id);
1095             Spanned {
1096                 node: StmtDecl(folder.fold_decl(d), id),
1097                 span: span
1098             }
1099         }
1100         StmtExpr(e, id) => {
1101             let id = folder.new_id(id);
1102             Spanned {
1103                 node: StmtExpr(folder.fold_expr(e), id),
1104                 span: span,
1105             }
1106         }
1107         StmtSemi(e, id) => {
1108             let id = folder.new_id(id);
1109             Spanned {
1110                 node: StmtSemi(folder.fold_expr(e), id),
1111                 span: span,
1112             }
1113         }
1114     }
1115 }