]> git.proxmox.com Git - rustc.git/blob - src/librustc/hir/fold.rs
Imported Upstream version 1.9.0+dfsg1
[rustc.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 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 StructField {
704 span: fld.new_span(f.span),
705 id: fld.new_id(f.id),
706 name: f.name,
707 vis: f.vis,
708 ty: fld.fold_ty(f.ty),
709 attrs: fold_attrs(f.attrs, fld),
710 }
711 }
712
713 pub fn noop_fold_field<T: Folder>(Field { name, expr, span }: Field, folder: &mut T) -> Field {
714 Field {
715 name: respan(folder.new_span(name.span), folder.fold_name(name.node)),
716 expr: folder.fold_expr(expr),
717 span: folder.new_span(span),
718 }
719 }
720
721 pub fn noop_fold_mt<T: Folder>(MutTy { ty, mutbl }: MutTy, folder: &mut T) -> MutTy {
722 MutTy {
723 ty: folder.fold_ty(ty),
724 mutbl: mutbl,
725 }
726 }
727
728 pub fn noop_fold_opt_bounds<T: Folder>(b: Option<TyParamBounds>,
729 folder: &mut T)
730 -> Option<TyParamBounds> {
731 b.map(|bounds| folder.fold_bounds(bounds))
732 }
733
734 fn noop_fold_bounds<T: Folder>(bounds: TyParamBounds, folder: &mut T) -> TyParamBounds {
735 bounds.move_map(|bound| folder.fold_ty_param_bound(bound))
736 }
737
738 pub fn noop_fold_block<T: Folder>(b: P<Block>, folder: &mut T) -> P<Block> {
739 b.map(|Block { id, stmts, expr, rules, span }| {
740 Block {
741 id: folder.new_id(id),
742 stmts: stmts.move_map(|s| folder.fold_stmt(s)),
743 expr: expr.map(|x| folder.fold_expr(x)),
744 rules: rules,
745 span: folder.new_span(span),
746 }
747 })
748 }
749
750 pub fn noop_fold_item_underscore<T: Folder>(i: Item_, folder: &mut T) -> Item_ {
751 match i {
752 ItemExternCrate(string) => ItemExternCrate(string),
753 ItemUse(view_path) => {
754 ItemUse(folder.fold_view_path(view_path))
755 }
756 ItemStatic(t, m, e) => {
757 ItemStatic(folder.fold_ty(t), m, folder.fold_expr(e))
758 }
759 ItemConst(t, e) => {
760 ItemConst(folder.fold_ty(t), folder.fold_expr(e))
761 }
762 ItemFn(decl, unsafety, constness, abi, generics, body) => {
763 ItemFn(folder.fold_fn_decl(decl),
764 unsafety,
765 constness,
766 abi,
767 folder.fold_generics(generics),
768 folder.fold_block(body))
769 }
770 ItemMod(m) => ItemMod(folder.fold_mod(m)),
771 ItemForeignMod(nm) => ItemForeignMod(folder.fold_foreign_mod(nm)),
772 ItemTy(t, generics) => {
773 ItemTy(folder.fold_ty(t), folder.fold_generics(generics))
774 }
775 ItemEnum(enum_definition, generics) => {
776 ItemEnum(hir::EnumDef {
777 variants: enum_definition.variants.move_map(|x| folder.fold_variant(x)),
778 },
779 folder.fold_generics(generics))
780 }
781 ItemStruct(struct_def, generics) => {
782 let struct_def = folder.fold_variant_data(struct_def);
783 ItemStruct(struct_def, folder.fold_generics(generics))
784 }
785 ItemDefaultImpl(unsafety, ref trait_ref) => {
786 ItemDefaultImpl(unsafety, folder.fold_trait_ref((*trait_ref).clone()))
787 }
788 ItemImpl(unsafety, polarity, generics, ifce, ty, impl_items) => {
789 let new_impl_items = impl_items
790 .move_map(|item| folder.fold_impl_item(item));
791 let ifce = match ifce {
792 None => None,
793 Some(ref trait_ref) => {
794 Some(folder.fold_trait_ref((*trait_ref).clone()))
795 }
796 };
797 ItemImpl(unsafety,
798 polarity,
799 folder.fold_generics(generics),
800 ifce,
801 folder.fold_ty(ty),
802 new_impl_items)
803 }
804 ItemTrait(unsafety, generics, bounds, items) => {
805 let bounds = folder.fold_bounds(bounds);
806 let items = items.move_map(|item| folder.fold_trait_item(item));
807 ItemTrait(unsafety, folder.fold_generics(generics), bounds, items)
808 }
809 }
810 }
811
812 pub fn noop_fold_trait_item<T: Folder>(i: TraitItem,
813 folder: &mut T)
814 -> TraitItem {
815 TraitItem {
816 id: folder.new_id(i.id),
817 name: folder.fold_name(i.name),
818 attrs: fold_attrs(i.attrs, folder),
819 node: match i.node {
820 ConstTraitItem(ty, default) => {
821 ConstTraitItem(folder.fold_ty(ty), default.map(|x| folder.fold_expr(x)))
822 }
823 MethodTraitItem(sig, body) => {
824 MethodTraitItem(noop_fold_method_sig(sig, folder),
825 body.map(|x| folder.fold_block(x)))
826 }
827 TypeTraitItem(bounds, default) => {
828 TypeTraitItem(folder.fold_bounds(bounds),
829 default.map(|x| folder.fold_ty(x)))
830 }
831 },
832 span: folder.new_span(i.span),
833 }
834 }
835
836 pub fn noop_fold_impl_item<T: Folder>(i: ImplItem, folder: &mut T) -> ImplItem {
837 ImplItem {
838 id: folder.new_id(i.id),
839 name: folder.fold_name(i.name),
840 attrs: fold_attrs(i.attrs, folder),
841 vis: i.vis,
842 defaultness: i.defaultness,
843 node: match i.node {
844 ImplItemKind::Const(ty, expr) => {
845 ImplItemKind::Const(folder.fold_ty(ty), folder.fold_expr(expr))
846 }
847 ImplItemKind::Method(sig, body) => {
848 ImplItemKind::Method(noop_fold_method_sig(sig, folder), folder.fold_block(body))
849 }
850 ImplItemKind::Type(ty) => ImplItemKind::Type(folder.fold_ty(ty)),
851 },
852 span: folder.new_span(i.span),
853 }
854 }
855
856 pub fn noop_fold_mod<T: Folder>(Mod { inner, item_ids }: Mod, folder: &mut T) -> Mod {
857 Mod {
858 inner: folder.new_span(inner),
859 item_ids: item_ids.move_map(|x| folder.fold_item_id(x)),
860 }
861 }
862
863 pub fn noop_fold_crate<T: Folder>(Crate { module, attrs, config, span,
864 exported_macros, items }: Crate,
865 folder: &mut T)
866 -> Crate {
867 let config = folder.fold_meta_items(config);
868
869 let crate_mod = folder.fold_item(hir::Item {
870 name: token::special_idents::invalid.name,
871 attrs: attrs,
872 id: DUMMY_NODE_ID,
873 vis: hir::Public,
874 span: span,
875 node: hir::ItemMod(module),
876 });
877
878 let (module, attrs, span) = match crate_mod {
879 hir::Item { attrs, span, node, .. } => {
880 match node {
881 hir::ItemMod(m) => (m, attrs, span),
882 _ => panic!("fold converted a module to not a module"),
883 }
884 }
885 };
886
887 let items = items.into_iter()
888 .map(|(id, item)| (id, folder.fold_item(item)))
889 .collect();
890
891 Crate {
892 module: module,
893 attrs: attrs,
894 config: config,
895 span: span,
896 exported_macros: exported_macros,
897 items: items,
898 }
899 }
900
901 pub fn noop_fold_item_id<T: Folder>(i: ItemId, folder: &mut T) -> ItemId {
902 let id = folder.map_id(i.id);
903 ItemId { id: id }
904 }
905
906 // fold one item into one item
907 pub fn noop_fold_item<T: Folder>(item: Item, folder: &mut T) -> Item {
908 let Item { id, name, attrs, node, vis, span } = item;
909 let id = folder.new_id(id);
910 let node = folder.fold_item_underscore(node);
911
912 Item {
913 id: id,
914 name: folder.fold_name(name),
915 attrs: fold_attrs(attrs, folder),
916 node: node,
917 vis: vis,
918 span: folder.new_span(span),
919 }
920 }
921
922 pub fn noop_fold_foreign_item<T: Folder>(ni: ForeignItem, folder: &mut T) -> ForeignItem {
923 ForeignItem {
924 id: folder.new_id(ni.id),
925 name: folder.fold_name(ni.name),
926 attrs: fold_attrs(ni.attrs, folder),
927 node: match ni.node {
928 ForeignItemFn(fdec, generics) => {
929 ForeignItemFn(folder.fold_fn_decl(fdec), folder.fold_generics(generics))
930 }
931 ForeignItemStatic(t, m) => {
932 ForeignItemStatic(folder.fold_ty(t), m)
933 }
934 },
935 vis: ni.vis,
936 span: folder.new_span(ni.span),
937 }
938 }
939
940 pub fn noop_fold_method_sig<T: Folder>(sig: MethodSig, folder: &mut T) -> MethodSig {
941 MethodSig {
942 generics: folder.fold_generics(sig.generics),
943 abi: sig.abi,
944 explicit_self: folder.fold_explicit_self(sig.explicit_self),
945 unsafety: sig.unsafety,
946 constness: sig.constness,
947 decl: folder.fold_fn_decl(sig.decl),
948 }
949 }
950
951 pub fn noop_fold_pat<T: Folder>(p: P<Pat>, folder: &mut T) -> P<Pat> {
952 p.map(|Pat { id, node, span }| {
953 Pat {
954 id: folder.new_id(id),
955 node: match node {
956 PatKind::Wild => PatKind::Wild,
957 PatKind::Ident(binding_mode, pth1, sub) => {
958 PatKind::Ident(binding_mode,
959 Spanned {
960 span: folder.new_span(pth1.span),
961 node: folder.fold_ident(pth1.node),
962 },
963 sub.map(|x| folder.fold_pat(x)))
964 }
965 PatKind::Lit(e) => PatKind::Lit(folder.fold_expr(e)),
966 PatKind::TupleStruct(pth, pats) => {
967 PatKind::TupleStruct(folder.fold_path(pth),
968 pats.map(|pats| pats.move_map(|x| folder.fold_pat(x))))
969 }
970 PatKind::Path(pth) => {
971 PatKind::Path(folder.fold_path(pth))
972 }
973 PatKind::QPath(qself, pth) => {
974 let qself = QSelf { ty: folder.fold_ty(qself.ty), ..qself };
975 PatKind::QPath(qself, folder.fold_path(pth))
976 }
977 PatKind::Struct(pth, fields, etc) => {
978 let pth = folder.fold_path(pth);
979 let fs = fields.move_map(|f| {
980 Spanned {
981 span: folder.new_span(f.span),
982 node: hir::FieldPat {
983 name: f.node.name,
984 pat: folder.fold_pat(f.node.pat),
985 is_shorthand: f.node.is_shorthand,
986 },
987 }
988 });
989 PatKind::Struct(pth, fs, etc)
990 }
991 PatKind::Tup(elts) => PatKind::Tup(elts.move_map(|x| folder.fold_pat(x))),
992 PatKind::Box(inner) => PatKind::Box(folder.fold_pat(inner)),
993 PatKind::Ref(inner, mutbl) => PatKind::Ref(folder.fold_pat(inner), mutbl),
994 PatKind::Range(e1, e2) => {
995 PatKind::Range(folder.fold_expr(e1), folder.fold_expr(e2))
996 }
997 PatKind::Vec(before, slice, after) => {
998 PatKind::Vec(before.move_map(|x| folder.fold_pat(x)),
999 slice.map(|x| folder.fold_pat(x)),
1000 after.move_map(|x| folder.fold_pat(x)))
1001 }
1002 },
1003 span: folder.new_span(span),
1004 }
1005 })
1006 }
1007
1008 pub fn noop_fold_expr<T: Folder>(Expr { id, node, span, attrs }: Expr, folder: &mut T) -> Expr {
1009 Expr {
1010 id: folder.new_id(id),
1011 node: match node {
1012 ExprBox(e) => {
1013 ExprBox(folder.fold_expr(e))
1014 }
1015 ExprVec(exprs) => {
1016 ExprVec(exprs.move_map(|x| folder.fold_expr(x)))
1017 }
1018 ExprRepeat(expr, count) => {
1019 ExprRepeat(folder.fold_expr(expr), folder.fold_expr(count))
1020 }
1021 ExprTup(elts) => ExprTup(elts.move_map(|x| folder.fold_expr(x))),
1022 ExprCall(f, args) => {
1023 ExprCall(folder.fold_expr(f), args.move_map(|x| folder.fold_expr(x)))
1024 }
1025 ExprMethodCall(name, tps, args) => {
1026 ExprMethodCall(respan(folder.new_span(name.span), folder.fold_name(name.node)),
1027 tps.move_map(|x| folder.fold_ty(x)),
1028 args.move_map(|x| folder.fold_expr(x)))
1029 }
1030 ExprBinary(binop, lhs, rhs) => {
1031 ExprBinary(binop, folder.fold_expr(lhs), folder.fold_expr(rhs))
1032 }
1033 ExprUnary(binop, ohs) => {
1034 ExprUnary(binop, folder.fold_expr(ohs))
1035 }
1036 ExprLit(l) => ExprLit(l),
1037 ExprCast(expr, ty) => {
1038 ExprCast(folder.fold_expr(expr), folder.fold_ty(ty))
1039 }
1040 ExprType(expr, ty) => {
1041 ExprType(folder.fold_expr(expr), folder.fold_ty(ty))
1042 }
1043 ExprAddrOf(m, ohs) => ExprAddrOf(m, folder.fold_expr(ohs)),
1044 ExprIf(cond, tr, fl) => {
1045 ExprIf(folder.fold_expr(cond),
1046 folder.fold_block(tr),
1047 fl.map(|x| folder.fold_expr(x)))
1048 }
1049 ExprWhile(cond, body, opt_ident) => {
1050 ExprWhile(folder.fold_expr(cond),
1051 folder.fold_block(body),
1052 opt_ident.map(|i| folder.fold_ident(i)))
1053 }
1054 ExprLoop(body, opt_ident) => {
1055 ExprLoop(folder.fold_block(body),
1056 opt_ident.map(|i| folder.fold_ident(i)))
1057 }
1058 ExprMatch(expr, arms, source) => {
1059 ExprMatch(folder.fold_expr(expr),
1060 arms.move_map(|x| folder.fold_arm(x)),
1061 source)
1062 }
1063 ExprClosure(capture_clause, decl, body) => {
1064 ExprClosure(capture_clause,
1065 folder.fold_fn_decl(decl),
1066 folder.fold_block(body))
1067 }
1068 ExprBlock(blk) => ExprBlock(folder.fold_block(blk)),
1069 ExprAssign(el, er) => {
1070 ExprAssign(folder.fold_expr(el), folder.fold_expr(er))
1071 }
1072 ExprAssignOp(op, el, er) => {
1073 ExprAssignOp(op, folder.fold_expr(el), folder.fold_expr(er))
1074 }
1075 ExprField(el, name) => {
1076 ExprField(folder.fold_expr(el),
1077 respan(folder.new_span(name.span), folder.fold_name(name.node)))
1078 }
1079 ExprTupField(el, index) => {
1080 ExprTupField(folder.fold_expr(el),
1081 respan(folder.new_span(index.span), folder.fold_usize(index.node)))
1082 }
1083 ExprIndex(el, er) => {
1084 ExprIndex(folder.fold_expr(el), folder.fold_expr(er))
1085 }
1086 ExprPath(qself, path) => {
1087 let qself = qself.map(|QSelf { ty, position }| {
1088 QSelf {
1089 ty: folder.fold_ty(ty),
1090 position: position,
1091 }
1092 });
1093 ExprPath(qself, folder.fold_path(path))
1094 }
1095 ExprBreak(opt_ident) => ExprBreak(opt_ident.map(|label| {
1096 respan(folder.new_span(label.span), folder.fold_ident(label.node))
1097 })),
1098 ExprAgain(opt_ident) => ExprAgain(opt_ident.map(|label| {
1099 respan(folder.new_span(label.span), folder.fold_ident(label.node))
1100 })),
1101 ExprRet(e) => ExprRet(e.map(|x| folder.fold_expr(x))),
1102 ExprInlineAsm(asm, outputs, inputs) => {
1103 ExprInlineAsm(asm,
1104 outputs.move_map(|x| folder.fold_expr(x)),
1105 inputs.move_map(|x| folder.fold_expr(x)))
1106 }
1107 ExprStruct(path, fields, maybe_expr) => {
1108 ExprStruct(folder.fold_path(path),
1109 fields.move_map(|x| folder.fold_field(x)),
1110 maybe_expr.map(|x| folder.fold_expr(x)))
1111 }
1112 },
1113 span: folder.new_span(span),
1114 attrs: attrs.map_thin_attrs(|attrs| fold_attrs(attrs.into(), folder).into()),
1115 }
1116 }
1117
1118 pub fn noop_fold_stmt<T: Folder>(stmt: Stmt, folder: &mut T) -> Stmt {
1119 let span = folder.new_span(stmt.span);
1120 match stmt.node {
1121 StmtDecl(d, id) => {
1122 let id = folder.new_id(id);
1123 Spanned {
1124 node: StmtDecl(folder.fold_decl(d), id),
1125 span: span
1126 }
1127 }
1128 StmtExpr(e, id) => {
1129 let id = folder.new_id(id);
1130 Spanned {
1131 node: StmtExpr(folder.fold_expr(e), id),
1132 span: span,
1133 }
1134 }
1135 StmtSemi(e, id) => {
1136 let id = folder.new_id(id);
1137 Spanned {
1138 node: StmtSemi(folder.fold_expr(e), id),
1139 span: span,
1140 }
1141 }
1142 }
1143 }