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