fold.rs 41.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

//! A Folder represents an HIR->HIR fold; it accepts a HIR piece,
//! and returns a piece of the same type.

use hir::*;
15
use syntax::ast::{Ident, Name, NodeId, DUMMY_NODE_ID, Attribute, Attribute_, MetaItem};
16 17
use syntax::ast::{MetaWord, MetaList, MetaNameValue};
use syntax::attr::ThinAttributesExt;
18 19 20 21 22 23 24 25 26 27 28 29 30
use hir;
use syntax::codemap::{respan, Span, Spanned};
use syntax::owned_slice::OwnedSlice;
use syntax::ptr::P;
use syntax::parse::token;
use std::ptr;

// This could have a better place to live.
pub trait MoveMap<T> {
    fn move_map<F>(self, f: F) -> Self where F: FnMut(T) -> T;
}

impl<T> MoveMap<T> for Vec<T> {
N
Nick Cameron 已提交
31 32 33
    fn move_map<F>(mut self, mut f: F) -> Vec<T>
        where F: FnMut(T) -> T
    {
34 35 36 37 38 39 40 41 42 43 44
        for p in &mut self {
            unsafe {
                // FIXME(#5016) this shouldn't need to zero to be safe.
                ptr::write(p, f(ptr::read_and_drop(p)));
            }
        }
        self
    }
}

impl<T> MoveMap<T> for OwnedSlice<T> {
N
Nick Cameron 已提交
45 46 47
    fn move_map<F>(self, f: F) -> OwnedSlice<T>
        where F: FnMut(T) -> T
    {
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
        OwnedSlice::from_vec(self.into_vec().move_map(f))
    }
}

pub trait Folder : Sized {
    // Any additions to this trait should happen in form
    // of a call to a public `noop_*` function that only calls
    // out to the folder again, not other `noop_*` functions.
    //
    // This is a necessary API workaround to the problem of not
    // being able to call out to the super default method
    // in an overridden default method.

    fn fold_crate(&mut self, c: Crate) -> Crate {
        noop_fold_crate(c, self)
    }

    fn fold_meta_items(&mut self, meta_items: Vec<P<MetaItem>>) -> Vec<P<MetaItem>> {
        noop_fold_meta_items(meta_items, self)
    }

    fn fold_meta_item(&mut self, meta_item: P<MetaItem>) -> P<MetaItem> {
        noop_fold_meta_item(meta_item, self)
    }

    fn fold_view_path(&mut self, view_path: P<ViewPath>) -> P<ViewPath> {
        noop_fold_view_path(view_path, self)
    }

    fn fold_foreign_item(&mut self, ni: P<ForeignItem>) -> P<ForeignItem> {
        noop_fold_foreign_item(ni, self)
    }

81
    fn fold_item(&mut self, i: Item) -> Item {
82 83 84
        noop_fold_item(i, self)
    }

85 86 87 88
    fn fold_item_id(&mut self, i: ItemId) -> ItemId {
        noop_fold_item_id(i, self)
    }

89 90 91 92 93 94 95 96
    fn fold_struct_field(&mut self, sf: StructField) -> StructField {
        noop_fold_struct_field(sf, self)
    }

    fn fold_item_underscore(&mut self, i: Item_) -> Item_ {
        noop_fold_item_underscore(i, self)
    }

97
    fn fold_trait_item(&mut self, i: P<TraitItem>) -> P<TraitItem> {
98 99 100
        noop_fold_trait_item(i, self)
    }

101
    fn fold_impl_item(&mut self, i: P<ImplItem>) -> P<ImplItem> {
102 103 104 105 106 107 108 109 110 111 112
        noop_fold_impl_item(i, self)
    }

    fn fold_fn_decl(&mut self, d: P<FnDecl>) -> P<FnDecl> {
        noop_fold_fn_decl(d, self)
    }

    fn fold_block(&mut self, b: P<Block>) -> P<Block> {
        noop_fold_block(b, self)
    }

113 114
    fn fold_stmt(&mut self, s: P<Stmt>) -> P<Stmt> {
        noop_fold_stmt(s, self)
115 116 117 118 119 120 121 122 123 124
    }

    fn fold_arm(&mut self, a: Arm) -> Arm {
        noop_fold_arm(a, self)
    }

    fn fold_pat(&mut self, p: P<Pat>) -> P<Pat> {
        noop_fold_pat(p, self)
    }

125
    fn fold_decl(&mut self, d: P<Decl>) -> P<Decl> {
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
        noop_fold_decl(d, self)
    }

    fn fold_expr(&mut self, e: P<Expr>) -> P<Expr> {
        e.map(|e| noop_fold_expr(e, self))
    }

    fn fold_ty(&mut self, t: P<Ty>) -> P<Ty> {
        noop_fold_ty(t, self)
    }

    fn fold_ty_binding(&mut self, t: P<TypeBinding>) -> P<TypeBinding> {
        noop_fold_ty_binding(t, self)
    }

    fn fold_mod(&mut self, m: Mod) -> Mod {
        noop_fold_mod(m, self)
    }

    fn fold_foreign_mod(&mut self, nm: ForeignMod) -> ForeignMod {
        noop_fold_foreign_mod(nm, self)
    }

    fn fold_variant(&mut self, v: P<Variant>) -> P<Variant> {
        noop_fold_variant(v, self)
    }

153 154
    fn fold_name(&mut self, n: Name) -> Name {
        noop_fold_name(n, self)
155 156
    }

157 158 159 160
    fn fold_ident(&mut self, i: Ident) -> Ident {
        noop_fold_ident(i, self)
    }

161 162 163 164 165 166 167 168 169 170 171 172
    fn fold_usize(&mut self, i: usize) -> usize {
        noop_fold_usize(i, self)
    }

    fn fold_path(&mut self, p: Path) -> Path {
        noop_fold_path(p, self)
    }

    fn fold_path_parameters(&mut self, p: PathParameters) -> PathParameters {
        noop_fold_path_parameters(p, self)
    }

N
Nick Cameron 已提交
173 174 175
    fn fold_angle_bracketed_parameter_data(&mut self,
                                           p: AngleBracketedParameterData)
                                           -> AngleBracketedParameterData {
176 177 178
        noop_fold_angle_bracketed_parameter_data(p, self)
    }

N
Nick Cameron 已提交
179 180 181
    fn fold_parenthesized_parameter_data(&mut self,
                                         p: ParenthesizedParameterData)
                                         -> ParenthesizedParameterData {
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
        noop_fold_parenthesized_parameter_data(p, self)
    }

    fn fold_local(&mut self, l: P<Local>) -> P<Local> {
        noop_fold_local(l, self)
    }

    fn fold_explicit_self(&mut self, es: ExplicitSelf) -> ExplicitSelf {
        noop_fold_explicit_self(es, self)
    }

    fn fold_explicit_self_underscore(&mut self, es: ExplicitSelf_) -> ExplicitSelf_ {
        noop_fold_explicit_self_underscore(es, self)
    }

    fn fold_lifetime(&mut self, l: Lifetime) -> Lifetime {
        noop_fold_lifetime(l, self)
    }

    fn fold_lifetime_def(&mut self, l: LifetimeDef) -> LifetimeDef {
        noop_fold_lifetime_def(l, self)
    }

    fn fold_attribute(&mut self, at: Attribute) -> Option<Attribute> {
        noop_fold_attribute(at, self)
    }

    fn fold_arg(&mut self, a: Arg) -> Arg {
        noop_fold_arg(a, self)
    }

    fn fold_generics(&mut self, generics: Generics) -> Generics {
        noop_fold_generics(generics, self)
    }

    fn fold_trait_ref(&mut self, p: TraitRef) -> TraitRef {
        noop_fold_trait_ref(p, self)
    }

    fn fold_poly_trait_ref(&mut self, p: PolyTraitRef) -> PolyTraitRef {
        noop_fold_poly_trait_ref(p, self)
    }

225 226
    fn fold_variant_data(&mut self, vdata: VariantData) -> VariantData {
        noop_fold_variant_data(vdata, self)
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
    }

    fn fold_lifetimes(&mut self, lts: Vec<Lifetime>) -> Vec<Lifetime> {
        noop_fold_lifetimes(lts, self)
    }

    fn fold_lifetime_defs(&mut self, lts: Vec<LifetimeDef>) -> Vec<LifetimeDef> {
        noop_fold_lifetime_defs(lts, self)
    }

    fn fold_ty_param(&mut self, tp: TyParam) -> TyParam {
        noop_fold_ty_param(tp, self)
    }

    fn fold_ty_params(&mut self, tps: OwnedSlice<TyParam>) -> OwnedSlice<TyParam> {
        noop_fold_ty_params(tps, self)
    }

    fn fold_opt_lifetime(&mut self, o_lt: Option<Lifetime>) -> Option<Lifetime> {
        noop_fold_opt_lifetime(o_lt, self)
    }

N
Nick Cameron 已提交
249 250
    fn fold_opt_bounds(&mut self,
                       b: Option<OwnedSlice<TyParamBound>>)
251 252 253 254
                       -> Option<OwnedSlice<TyParamBound>> {
        noop_fold_opt_bounds(b, self)
    }

N
Nick Cameron 已提交
255
    fn fold_bounds(&mut self, b: OwnedSlice<TyParamBound>) -> OwnedSlice<TyParamBound> {
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
        noop_fold_bounds(b, self)
    }

    fn fold_ty_param_bound(&mut self, tpb: TyParamBound) -> TyParamBound {
        noop_fold_ty_param_bound(tpb, self)
    }

    fn fold_mt(&mut self, mt: MutTy) -> MutTy {
        noop_fold_mt(mt, self)
    }

    fn fold_field(&mut self, field: Field) -> Field {
        noop_fold_field(field, self)
    }

N
Nick Cameron 已提交
271
    fn fold_where_clause(&mut self, where_clause: WhereClause) -> WhereClause {
272 273 274
        noop_fold_where_clause(where_clause, self)
    }

N
Nick Cameron 已提交
275
    fn fold_where_predicate(&mut self, where_predicate: WherePredicate) -> WherePredicate {
276 277 278
        noop_fold_where_predicate(where_predicate, self)
    }

279
    /// called for the `id` on each declaration
280 281 282 283
    fn new_id(&mut self, i: NodeId) -> NodeId {
        i
    }

284 285 286 287 288
    /// called for ids that are references (e.g., ItemDef)
    fn map_id(&mut self, i: NodeId) -> NodeId {
        i
    }

289 290 291 292 293
    fn new_span(&mut self, sp: Span) -> Span {
        sp
    }
}

N
Nick Cameron 已提交
294 295
pub fn noop_fold_meta_items<T: Folder>(meta_items: Vec<P<MetaItem>>,
                                       fld: &mut T)
296 297 298 299 300
                                       -> Vec<P<MetaItem>> {
    meta_items.move_map(|x| fld.fold_meta_item(x))
}

pub fn noop_fold_view_path<T: Folder>(view_path: P<ViewPath>, fld: &mut T) -> P<ViewPath> {
N
Nick Cameron 已提交
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
    view_path.map(|Spanned { node, span }| {
        Spanned {
            node: match node {
                ViewPathSimple(name, path) => {
                    ViewPathSimple(name, fld.fold_path(path))
                }
                ViewPathGlob(path) => {
                    ViewPathGlob(fld.fold_path(path))
                }
                ViewPathList(path, path_list_idents) => {
                    ViewPathList(fld.fold_path(path),
                                 path_list_idents.move_map(|path_list_ident| {
                                     Spanned {
                                         node: match path_list_ident.node {
                                             PathListIdent { id, name, rename } => PathListIdent {
                                                 id: fld.new_id(id),
                                                 name: name,
                                                 rename: rename,
                                             },
                                             PathListMod { id, rename } => PathListMod {
                                                 id: fld.new_id(id),
                                                 rename: rename,
                                             },
                                         },
                                         span: fld.new_span(path_list_ident.span),
                                     }
                                 }))
                }
            },
            span: fld.new_span(span),
        }
332 333 334 335 336 337 338
    })
}

pub fn fold_attrs<T: Folder>(attrs: Vec<Attribute>, fld: &mut T) -> Vec<Attribute> {
    attrs.into_iter().flat_map(|x| fld.fold_attribute(x)).collect()
}

N
Nick Cameron 已提交
339
pub fn noop_fold_arm<T: Folder>(Arm { attrs, pats, guard, body }: Arm, fld: &mut T) -> Arm {
340 341 342 343 344 345 346 347
    Arm {
        attrs: fold_attrs(attrs, fld),
        pats: pats.move_map(|x| fld.fold_pat(x)),
        guard: guard.map(|x| fld.fold_expr(x)),
        body: fld.fold_expr(body),
    }
}

348 349
pub fn noop_fold_decl<T: Folder>(d: P<Decl>, fld: &mut T) -> P<Decl> {
    d.map(|Spanned { node, span }| {
N
Nick Cameron 已提交
350
        match node {
351
            DeclLocal(l) => Spanned {
N
Nick Cameron 已提交
352 353
                node: DeclLocal(fld.fold_local(l)),
                span: fld.new_span(span),
354 355
            },
            DeclItem(it) => Spanned {
356
                node: DeclItem(fld.fold_item_id(it)),
357 358
                span: fld.new_span(span),
            },
N
Nick Cameron 已提交
359
        }
360 361 362 363
    })
}

pub fn noop_fold_ty_binding<T: Folder>(b: P<TypeBinding>, fld: &mut T) -> P<TypeBinding> {
N
Nick Cameron 已提交
364 365 366 367 368 369 370
    b.map(|TypeBinding { id, name, ty, span }| {
        TypeBinding {
            id: fld.new_id(id),
            name: name,
            ty: fld.fold_ty(ty),
            span: fld.new_span(span),
        }
371 372 373 374
    })
}

pub fn noop_fold_ty<T: Folder>(t: P<Ty>, fld: &mut T) -> P<Ty> {
N
Nick Cameron 已提交
375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419
    t.map(|Ty { id, node, span }| {
        Ty {
            id: fld.new_id(id),
            node: match node {
                TyInfer => node,
                TyVec(ty) => TyVec(fld.fold_ty(ty)),
                TyPtr(mt) => TyPtr(fld.fold_mt(mt)),
                TyRptr(region, mt) => {
                    TyRptr(fld.fold_opt_lifetime(region), fld.fold_mt(mt))
                }
                TyBareFn(f) => {
                    TyBareFn(f.map(|BareFnTy { lifetimes, unsafety, abi, decl }| {
                        BareFnTy {
                            lifetimes: fld.fold_lifetime_defs(lifetimes),
                            unsafety: unsafety,
                            abi: abi,
                            decl: fld.fold_fn_decl(decl),
                        }
                    }))
                }
                TyTup(tys) => TyTup(tys.move_map(|ty| fld.fold_ty(ty))),
                TyPath(qself, path) => {
                    let qself = qself.map(|QSelf { ty, position }| {
                        QSelf {
                            ty: fld.fold_ty(ty),
                            position: position,
                        }
                    });
                    TyPath(qself, fld.fold_path(path))
                }
                TyObjectSum(ty, bounds) => {
                    TyObjectSum(fld.fold_ty(ty), fld.fold_bounds(bounds))
                }
                TyFixedLengthVec(ty, e) => {
                    TyFixedLengthVec(fld.fold_ty(ty), fld.fold_expr(e))
                }
                TyTypeof(expr) => {
                    TyTypeof(fld.fold_expr(expr))
                }
                TyPolyTraitRef(bounds) => {
                    TyPolyTraitRef(bounds.move_map(|b| fld.fold_ty_param_bound(b)))
                }
            },
            span: fld.new_span(span),
        }
420 421 422
    })
}

N
Nick Cameron 已提交
423 424 425
pub fn noop_fold_foreign_mod<T: Folder>(ForeignMod { abi, items }: ForeignMod,
                                        fld: &mut T)
                                        -> ForeignMod {
426 427 428 429 430 431 432
    ForeignMod {
        abi: abi,
        items: items.move_map(|x| fld.fold_foreign_item(x)),
    }
}

pub fn noop_fold_variant<T: Folder>(v: P<Variant>, fld: &mut T) -> P<Variant> {
J
Jose Narvaez 已提交
433 434 435 436 437 438 439 440 441 442
    v.map(|Spanned { node: Variant_ { name, attrs, data, disr_expr }, span }| {
        Spanned {
            node: Variant_ {
                name: name,
                attrs: fold_attrs(attrs, fld),
                data: fld.fold_variant_data(data),
                disr_expr: disr_expr.map(|e| fld.fold_expr(e)),
            },
            span: fld.new_span(span),
        }
443 444 445
    })
}

446 447
pub fn noop_fold_name<T: Folder>(n: Name, _: &mut T) -> Name {
    n
448 449
}

450 451 452 453
pub fn noop_fold_ident<T: Folder>(i: Ident, _: &mut T) -> Ident {
    i
}

454 455 456 457
pub fn noop_fold_usize<T: Folder>(i: usize, _: &mut T) -> usize {
    i
}

N
Nick Cameron 已提交
458
pub fn noop_fold_path<T: Folder>(Path { global, segments, span }: Path, fld: &mut T) -> Path {
459 460
    Path {
        global: global,
N
Nick Cameron 已提交
461 462 463 464 465
        segments: segments.move_map(|PathSegment { identifier, parameters }| {
            PathSegment {
                identifier: fld.fold_ident(identifier),
                parameters: fld.fold_path_parameters(parameters),
            }
466
        }),
N
Nick Cameron 已提交
467
        span: fld.new_span(span),
468 469 470
    }
}

N
Nick Cameron 已提交
471 472 473
pub fn noop_fold_path_parameters<T: Folder>(path_parameters: PathParameters,
                                            fld: &mut T)
                                            -> PathParameters {
474 475 476 477 478 479 480 481 482 483
    match path_parameters {
        AngleBracketedParameters(data) =>
            AngleBracketedParameters(fld.fold_angle_bracketed_parameter_data(data)),
        ParenthesizedParameters(data) =>
            ParenthesizedParameters(fld.fold_parenthesized_parameter_data(data)),
    }
}

pub fn noop_fold_angle_bracketed_parameter_data<T: Folder>(data: AngleBracketedParameterData,
                                                           fld: &mut T)
N
Nick Cameron 已提交
484
                                                           -> AngleBracketedParameterData {
485
    let AngleBracketedParameterData { lifetimes, types, bindings } = data;
N
Nick Cameron 已提交
486 487 488 489 490
    AngleBracketedParameterData {
        lifetimes: fld.fold_lifetimes(lifetimes),
        types: types.move_map(|ty| fld.fold_ty(ty)),
        bindings: bindings.move_map(|b| fld.fold_ty_binding(b)),
    }
491 492 493 494
}

pub fn noop_fold_parenthesized_parameter_data<T: Folder>(data: ParenthesizedParameterData,
                                                         fld: &mut T)
N
Nick Cameron 已提交
495
                                                         -> ParenthesizedParameterData {
496
    let ParenthesizedParameterData { inputs, output, span } = data;
N
Nick Cameron 已提交
497 498 499 500 501
    ParenthesizedParameterData {
        inputs: inputs.move_map(|ty| fld.fold_ty(ty)),
        output: output.map(|ty| fld.fold_ty(ty)),
        span: fld.new_span(span),
    }
502 503 504
}

pub fn noop_fold_local<T: Folder>(l: P<Local>, fld: &mut T) -> P<Local> {
505
    l.map(|Local { id, pat, ty, init, span, attrs }| {
N
Nick Cameron 已提交
506 507 508 509 510 511
        Local {
            id: fld.new_id(id),
            ty: ty.map(|t| fld.fold_ty(t)),
            pat: fld.fold_pat(pat),
            init: init.map(|e| fld.fold_expr(e)),
            span: fld.new_span(span),
512
            attrs: attrs.map_thin_attrs(|attrs| fold_attrs(attrs, fld)),
N
Nick Cameron 已提交
513
        }
514 515 516 517 518 519 520 521 522 523
    })
}

pub fn noop_fold_attribute<T: Folder>(at: Attribute, fld: &mut T) -> Option<Attribute> {
    let Spanned {node: Attribute_ {id, style, value, is_sugared_doc}, span} = at;
    Some(Spanned {
        node: Attribute_ {
            id: id,
            style: style,
            value: fld.fold_meta_item(value),
N
Nick Cameron 已提交
524
            is_sugared_doc: is_sugared_doc,
525
        },
N
Nick Cameron 已提交
526
        span: fld.new_span(span),
527 528 529
    })
}

N
Nick Cameron 已提交
530 531
pub fn noop_fold_explicit_self_underscore<T: Folder>(es: ExplicitSelf_,
                                                     fld: &mut T)
532 533 534
                                                     -> ExplicitSelf_ {
    match es {
        SelfStatic | SelfValue(_) => es,
535 536
        SelfRegion(lifetime, m, name) => {
            SelfRegion(fld.fold_opt_lifetime(lifetime), m, name)
537
        }
538 539
        SelfExplicit(typ, name) => {
            SelfExplicit(fld.fold_ty(typ), name)
540 541 542 543
        }
    }
}

N
Nick Cameron 已提交
544 545
pub fn noop_fold_explicit_self<T: Folder>(Spanned { span, node }: ExplicitSelf,
                                          fld: &mut T)
546 547 548
                                          -> ExplicitSelf {
    Spanned {
        node: fld.fold_explicit_self_underscore(node),
N
Nick Cameron 已提交
549
        span: fld.new_span(span),
550 551 552 553
    }
}

pub fn noop_fold_meta_item<T: Folder>(mi: P<MetaItem>, fld: &mut T) -> P<MetaItem> {
N
Nick Cameron 已提交
554 555 556 557 558 559 560 561 562 563 564
    mi.map(|Spanned { node, span }| {
        Spanned {
            node: match node {
                MetaWord(id) => MetaWord(id),
                MetaList(id, mis) => {
                    MetaList(id, mis.move_map(|e| fld.fold_meta_item(e)))
                }
                MetaNameValue(id, s) => MetaNameValue(id, s),
            },
            span: fld.new_span(span),
        }
565 566 567
    })
}

N
Nick Cameron 已提交
568
pub fn noop_fold_arg<T: Folder>(Arg { id, pat, ty }: Arg, fld: &mut T) -> Arg {
569 570 571
    Arg {
        id: fld.new_id(id),
        pat: fld.fold_pat(pat),
N
Nick Cameron 已提交
572
        ty: fld.fold_ty(ty),
573 574 575 576
    }
}

pub fn noop_fold_fn_decl<T: Folder>(decl: P<FnDecl>, fld: &mut T) -> P<FnDecl> {
N
Nick Cameron 已提交
577 578 579 580 581 582 583 584 585 586
    decl.map(|FnDecl { inputs, output, variadic }| {
        FnDecl {
            inputs: inputs.move_map(|x| fld.fold_arg(x)),
            output: match output {
                Return(ty) => Return(fld.fold_ty(ty)),
                DefaultReturn(span) => DefaultReturn(span),
                NoReturn(span) => NoReturn(span),
            },
            variadic: variadic,
        }
587 588 589
    })
}

N
Nick Cameron 已提交
590 591 592
pub fn noop_fold_ty_param_bound<T>(tpb: TyParamBound, fld: &mut T) -> TyParamBound
    where T: Folder
{
593 594 595 596 597 598 599
    match tpb {
        TraitTyParamBound(ty, modifier) => TraitTyParamBound(fld.fold_poly_trait_ref(ty), modifier),
        RegionTyParamBound(lifetime) => RegionTyParamBound(fld.fold_lifetime(lifetime)),
    }
}

pub fn noop_fold_ty_param<T: Folder>(tp: TyParam, fld: &mut T) -> TyParam {
600
    let TyParam {id, name, bounds, default, span} = tp;
601 602
    TyParam {
        id: fld.new_id(id),
603
        name: name,
604 605
        bounds: fld.fold_bounds(bounds),
        default: default.map(|x| fld.fold_ty(x)),
N
Nick Cameron 已提交
606
        span: span,
607 608 609
    }
}

N
Nick Cameron 已提交
610 611
pub fn noop_fold_ty_params<T: Folder>(tps: OwnedSlice<TyParam>,
                                      fld: &mut T)
612 613 614 615 616 617 618 619
                                      -> OwnedSlice<TyParam> {
    tps.move_map(|tp| fld.fold_ty_param(tp))
}

pub fn noop_fold_lifetime<T: Folder>(l: Lifetime, fld: &mut T) -> Lifetime {
    Lifetime {
        id: fld.new_id(l.id),
        name: l.name,
N
Nick Cameron 已提交
620
        span: fld.new_span(l.span),
621 622 623
    }
}

N
Nick Cameron 已提交
624
pub fn noop_fold_lifetime_def<T: Folder>(l: LifetimeDef, fld: &mut T) -> LifetimeDef {
625 626 627 628 629 630 631 632 633 634
    LifetimeDef {
        lifetime: fld.fold_lifetime(l.lifetime),
        bounds: fld.fold_lifetimes(l.bounds),
    }
}

pub fn noop_fold_lifetimes<T: Folder>(lts: Vec<Lifetime>, fld: &mut T) -> Vec<Lifetime> {
    lts.move_map(|l| fld.fold_lifetime(l))
}

N
Nick Cameron 已提交
635
pub fn noop_fold_lifetime_defs<T: Folder>(lts: Vec<LifetimeDef>, fld: &mut T) -> Vec<LifetimeDef> {
636 637 638
    lts.move_map(|l| fld.fold_lifetime_def(l))
}

N
Nick Cameron 已提交
639
pub fn noop_fold_opt_lifetime<T: Folder>(o_lt: Option<Lifetime>, fld: &mut T) -> Option<Lifetime> {
640 641 642
    o_lt.map(|lt| fld.fold_lifetime(lt))
}

N
Nick Cameron 已提交
643 644 645
pub fn noop_fold_generics<T: Folder>(Generics { ty_params, lifetimes, where_clause }: Generics,
                                     fld: &mut T)
                                     -> Generics {
646 647 648 649 650 651 652
    Generics {
        ty_params: fld.fold_ty_params(ty_params),
        lifetimes: fld.fold_lifetime_defs(lifetimes),
        where_clause: fld.fold_where_clause(where_clause),
    }
}

N
Nick Cameron 已提交
653 654 655
pub fn noop_fold_where_clause<T: Folder>(WhereClause { id, predicates }: WhereClause,
                                         fld: &mut T)
                                         -> WhereClause {
656 657
    WhereClause {
        id: fld.new_id(id),
N
Nick Cameron 已提交
658
        predicates: predicates.move_map(|predicate| fld.fold_where_predicate(predicate)),
659 660 661
    }
}

N
Nick Cameron 已提交
662
pub fn noop_fold_where_predicate<T: Folder>(pred: WherePredicate, fld: &mut T) -> WherePredicate {
663 664 665 666 667 668 669 670 671
    match pred {
        hir::WherePredicate::BoundPredicate(hir::WhereBoundPredicate{bound_lifetimes,
                                                                     bounded_ty,
                                                                     bounds,
                                                                     span}) => {
            hir::WherePredicate::BoundPredicate(hir::WhereBoundPredicate {
                bound_lifetimes: fld.fold_lifetime_defs(bound_lifetimes),
                bounded_ty: fld.fold_ty(bounded_ty),
                bounds: bounds.move_map(|x| fld.fold_ty_param_bound(x)),
N
Nick Cameron 已提交
672
                span: fld.new_span(span),
673 674 675 676 677 678 679 680
            })
        }
        hir::WherePredicate::RegionPredicate(hir::WhereRegionPredicate{lifetime,
                                                                       bounds,
                                                                       span}) => {
            hir::WherePredicate::RegionPredicate(hir::WhereRegionPredicate {
                span: fld.new_span(span),
                lifetime: fld.fold_lifetime(lifetime),
N
Nick Cameron 已提交
681
                bounds: bounds.move_map(|bound| fld.fold_lifetime(bound)),
682 683 684 685 686 687
            })
        }
        hir::WherePredicate::EqPredicate(hir::WhereEqPredicate{id,
                                                               path,
                                                               ty,
                                                               span}) => {
N
Nick Cameron 已提交
688
            hir::WherePredicate::EqPredicate(hir::WhereEqPredicate {
689 690
                id: fld.new_id(id),
                path: fld.fold_path(path),
N
Nick Cameron 已提交
691 692
                ty: fld.fold_ty(ty),
                span: fld.new_span(span),
693 694 695 696 697
            })
        }
    }
}

698 699 700
pub fn noop_fold_variant_data<T: Folder>(vdata: VariantData, fld: &mut T) -> VariantData {
    match vdata {
        VariantData::Struct(fields, id) => {
J
Jose Narvaez 已提交
701 702
            VariantData::Struct(fields.move_map(|f| fld.fold_struct_field(f)),
                                fld.new_id(id))
N
Nick Cameron 已提交
703
        }
704
        VariantData::Tuple(fields, id) => {
J
Jose Narvaez 已提交
705 706
            VariantData::Tuple(fields.move_map(|f| fld.fold_struct_field(f)),
                               fld.new_id(id))
707
        }
J
Jose Narvaez 已提交
708
        VariantData::Unit(id) => VariantData::Unit(fld.new_id(id)),
709
    }
710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740
}

pub fn noop_fold_trait_ref<T: Folder>(p: TraitRef, fld: &mut T) -> TraitRef {
    let id = fld.new_id(p.ref_id);
    let TraitRef {
        path,
        ref_id: _,
    } = p;
    hir::TraitRef {
        path: fld.fold_path(path),
        ref_id: id,
    }
}

pub fn noop_fold_poly_trait_ref<T: Folder>(p: PolyTraitRef, fld: &mut T) -> PolyTraitRef {
    hir::PolyTraitRef {
        bound_lifetimes: fld.fold_lifetime_defs(p.bound_lifetimes),
        trait_ref: fld.fold_trait_ref(p.trait_ref),
        span: fld.new_span(p.span),
    }
}

pub fn noop_fold_struct_field<T: Folder>(f: StructField, fld: &mut T) -> StructField {
    let StructField {node: StructField_ {id, kind, ty, attrs}, span} = f;
    Spanned {
        node: StructField_ {
            id: fld.new_id(id),
            kind: kind,
            ty: fld.fold_ty(ty),
            attrs: fold_attrs(attrs, fld),
        },
N
Nick Cameron 已提交
741
        span: fld.new_span(span),
742 743 744
    }
}

N
Nick Cameron 已提交
745
pub fn noop_fold_field<T: Folder>(Field { name, expr, span }: Field, folder: &mut T) -> Field {
746
    Field {
J
Jose Narvaez 已提交
747
        name: respan(folder.new_span(name.span), folder.fold_name(name.node)),
748
        expr: folder.fold_expr(expr),
N
Nick Cameron 已提交
749
        span: folder.new_span(span),
750 751 752
    }
}

N
Nick Cameron 已提交
753
pub fn noop_fold_mt<T: Folder>(MutTy { ty, mutbl }: MutTy, folder: &mut T) -> MutTy {
754 755 756 757 758 759
    MutTy {
        ty: folder.fold_ty(ty),
        mutbl: mutbl,
    }
}

N
Nick Cameron 已提交
760 761
pub fn noop_fold_opt_bounds<T: Folder>(b: Option<OwnedSlice<TyParamBound>>,
                                       folder: &mut T)
762 763 764 765
                                       -> Option<OwnedSlice<TyParamBound>> {
    b.map(|bounds| folder.fold_bounds(bounds))
}

N
Nick Cameron 已提交
766
fn noop_fold_bounds<T: Folder>(bounds: TyParamBounds, folder: &mut T) -> TyParamBounds {
767 768 769 770
    bounds.move_map(|bound| folder.fold_ty_param_bound(bound))
}

pub fn noop_fold_block<T: Folder>(b: P<Block>, folder: &mut T) -> P<Block> {
N
Nick Cameron 已提交
771 772 773
    b.map(|Block { id, stmts, expr, rules, span }| {
        Block {
            id: folder.new_id(id),
774
            stmts: stmts.into_iter().map(|s| folder.fold_stmt(s)).collect(),
N
Nick Cameron 已提交
775 776 777 778
            expr: expr.map(|x| folder.fold_expr(x)),
            rules: rules,
            span: folder.new_span(span),
        }
779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794
    })
}

pub fn noop_fold_item_underscore<T: Folder>(i: Item_, folder: &mut T) -> Item_ {
    match i {
        ItemExternCrate(string) => ItemExternCrate(string),
        ItemUse(view_path) => {
            ItemUse(folder.fold_view_path(view_path))
        }
        ItemStatic(t, m, e) => {
            ItemStatic(folder.fold_ty(t), m, folder.fold_expr(e))
        }
        ItemConst(t, e) => {
            ItemConst(folder.fold_ty(t), folder.fold_expr(e))
        }
        ItemFn(decl, unsafety, constness, abi, generics, body) => {
N
Nick Cameron 已提交
795 796 797 798 799 800
            ItemFn(folder.fold_fn_decl(decl),
                   unsafety,
                   constness,
                   abi,
                   folder.fold_generics(generics),
                   folder.fold_block(body))
801 802 803 804 805 806 807
        }
        ItemMod(m) => ItemMod(folder.fold_mod(m)),
        ItemForeignMod(nm) => ItemForeignMod(folder.fold_foreign_mod(nm)),
        ItemTy(t, generics) => {
            ItemTy(folder.fold_ty(t), folder.fold_generics(generics))
        }
        ItemEnum(enum_definition, generics) => {
N
Nick Cameron 已提交
808 809 810 811
            ItemEnum(hir::EnumDef {
                         variants: enum_definition.variants.move_map(|x| folder.fold_variant(x)),
                     },
                     folder.fold_generics(generics))
812 813
        }
        ItemStruct(struct_def, generics) => {
814
            let struct_def = folder.fold_variant_data(struct_def);
815 816 817
            ItemStruct(struct_def, folder.fold_generics(generics))
        }
        ItemDefaultImpl(unsafety, ref trait_ref) => {
J
Jose Narvaez 已提交
818
            ItemDefaultImpl(unsafety, folder.fold_trait_ref((*trait_ref).clone()))
819 820
        }
        ItemImpl(unsafety, polarity, generics, ifce, ty, impl_items) => {
N
Nick Cameron 已提交
821
            let new_impl_items = impl_items.into_iter()
822
                                           .map(|item| folder.fold_impl_item(item))
N
Nick Cameron 已提交
823
                                           .collect();
824 825 826 827 828 829 830 831 832 833 834 835 836 837 838
            let ifce = match ifce {
                None => None,
                Some(ref trait_ref) => {
                    Some(folder.fold_trait_ref((*trait_ref).clone()))
                }
            };
            ItemImpl(unsafety,
                     polarity,
                     folder.fold_generics(generics),
                     ifce,
                     folder.fold_ty(ty),
                     new_impl_items)
        }
        ItemTrait(unsafety, generics, bounds, items) => {
            let bounds = folder.fold_bounds(bounds);
N
Nick Cameron 已提交
839
            let items = items.into_iter()
840
                             .map(|item| folder.fold_trait_item(item))
N
Nick Cameron 已提交
841 842
                             .collect();
            ItemTrait(unsafety, folder.fold_generics(generics), bounds, items)
843 844 845 846
        }
    }
}

N
Nick Cameron 已提交
847 848
pub fn noop_fold_trait_item<T: Folder>(i: P<TraitItem>,
                                       folder: &mut T)
849 850
                                       -> P<TraitItem> {
    i.map(|TraitItem { id, name, attrs, node, span }| {
N
Nick Cameron 已提交
851 852 853 854 855 856
        TraitItem {
            id: folder.new_id(id),
            name: folder.fold_name(name),
            attrs: fold_attrs(attrs, folder),
            node: match node {
                ConstTraitItem(ty, default) => {
J
Jose Narvaez 已提交
857
                    ConstTraitItem(folder.fold_ty(ty), default.map(|x| folder.fold_expr(x)))
N
Nick Cameron 已提交
858 859 860 861 862 863 864 865 866 867 868 869
                }
                MethodTraitItem(sig, body) => {
                    MethodTraitItem(noop_fold_method_sig(sig, folder),
                                    body.map(|x| folder.fold_block(x)))
                }
                TypeTraitItem(bounds, default) => {
                    TypeTraitItem(folder.fold_bounds(bounds),
                                  default.map(|x| folder.fold_ty(x)))
                }
            },
            span: folder.new_span(span),
        }
870
    })
871 872
}

873 874
pub fn noop_fold_impl_item<T: Folder>(i: P<ImplItem>, folder: &mut T) -> P<ImplItem> {
    i.map(|ImplItem { id, name, attrs, node, vis, span }| {
N
Nick Cameron 已提交
875 876 877 878 879 880
        ImplItem {
            id: folder.new_id(id),
            name: folder.fold_name(name),
            attrs: fold_attrs(attrs, folder),
            vis: vis,
            node: match node {
881 882
                ImplItemKind::Const(ty, expr) => {
                    ImplItemKind::Const(folder.fold_ty(ty), folder.fold_expr(expr))
N
Nick Cameron 已提交
883
                }
884 885
                ImplItemKind::Method(sig, body) => {
                    ImplItemKind::Method(noop_fold_method_sig(sig, folder), folder.fold_block(body))
N
Nick Cameron 已提交
886
                }
887
                ImplItemKind::Type(ty) => ImplItemKind::Type(folder.fold_ty(ty)),
N
Nick Cameron 已提交
888 889 890
            },
            span: folder.new_span(span),
        }
891
    })
892 893
}

894
pub fn noop_fold_mod<T: Folder>(Mod { inner, item_ids }: Mod, folder: &mut T) -> Mod {
895 896
    Mod {
        inner: folder.new_span(inner),
897
        item_ids: item_ids.into_iter().map(|x| folder.fold_item_id(x)).collect(),
898 899 900
    }
}

901 902
pub fn noop_fold_crate<T: Folder>(Crate { module, attrs, config, span,
                                          exported_macros, items }: Crate,
N
Nick Cameron 已提交
903 904
                                  folder: &mut T)
                                  -> Crate {
905 906
    let config = folder.fold_meta_items(config);

907
    let crate_mod = folder.fold_item(hir::Item {
908 909 910 911 912 913
        name: token::special_idents::invalid.name,
        attrs: attrs,
        id: DUMMY_NODE_ID,
        vis: hir::Public,
        span: span,
        node: hir::ItemMod(module),
914
    });
915

916 917
    let (module, attrs, span) = match crate_mod {
        hir::Item { attrs, span, node, .. } => {
918 919 920 921
            match node {
                hir::ItemMod(m) => (m, attrs, span),
                _ => panic!("fold converted a module to not a module"),
            }
922 923 924 925 926 927
        }
    };

    let items = items.into_iter()
                     .map(|(id, item)| (id, folder.fold_item(item)))
                     .collect();
928 929 930 931 932 933 934

    Crate {
        module: module,
        attrs: attrs,
        config: config,
        span: span,
        exported_macros: exported_macros,
935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964
        items: items,
    }
}

pub fn noop_fold_item_id<T: Folder>(i: ItemId, folder: &mut T) -> ItemId {
    let id = folder.map_id(i.id);
    ItemId { id: id }
}

// fold one item into one item
pub fn noop_fold_item<T: Folder>(item: Item, folder: &mut T) -> Item {
    let Item { id, name, attrs, node, vis, span } = item;
    let id = folder.new_id(id);
    let node = folder.fold_item_underscore(node);
    // FIXME: we should update the impl_pretty_name, but it uses pretty printing.
    // let ident = match node {
    //     // The node may have changed, recompute the "pretty" impl name.
    //     ItemImpl(_, _, _, ref maybe_trait, ref ty, _) => {
    //         impl_pretty_name(maybe_trait, Some(&**ty))
    //     }
    //     _ => ident
    // };

    Item {
        id: id,
        name: folder.fold_name(name),
        attrs: fold_attrs(attrs, folder),
        node: node,
        vis: vis,
        span: folder.new_span(span),
965 966 967 968
    }
}

pub fn noop_fold_foreign_item<T: Folder>(ni: P<ForeignItem>, folder: &mut T) -> P<ForeignItem> {
N
Nick Cameron 已提交
969 970 971 972 973 974 975
    ni.map(|ForeignItem { id, name, attrs, node, span, vis }| {
        ForeignItem {
            id: folder.new_id(id),
            name: folder.fold_name(name),
            attrs: fold_attrs(attrs, folder),
            node: match node {
                ForeignItemFn(fdec, generics) => {
J
Jose Narvaez 已提交
976
                    ForeignItemFn(folder.fold_fn_decl(fdec), folder.fold_generics(generics))
N
Nick Cameron 已提交
977 978 979 980 981 982 983 984
                }
                ForeignItemStatic(t, m) => {
                    ForeignItemStatic(folder.fold_ty(t), m)
                }
            },
            vis: vis,
            span: folder.new_span(span),
        }
985 986 987 988 989 990 991 992 993 994
    })
}

pub fn noop_fold_method_sig<T: Folder>(sig: MethodSig, folder: &mut T) -> MethodSig {
    MethodSig {
        generics: folder.fold_generics(sig.generics),
        abi: sig.abi,
        explicit_self: folder.fold_explicit_self(sig.explicit_self),
        unsafety: sig.unsafety,
        constness: sig.constness,
N
Nick Cameron 已提交
995
        decl: folder.fold_fn_decl(sig.decl),
996 997 998 999
    }
}

pub fn noop_fold_pat<T: Folder>(p: P<Pat>, folder: &mut T) -> P<Pat> {
N
Nick Cameron 已提交
1000 1001 1002 1003
    p.map(|Pat { id, node, span }| {
        Pat {
            id: folder.new_id(id),
            node: match node {
V
Vadim Petrochenkov 已提交
1004
                PatWild => PatWild,
N
Nick Cameron 已提交
1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046
                PatIdent(binding_mode, pth1, sub) => {
                    PatIdent(binding_mode,
                             Spanned {
                                 span: folder.new_span(pth1.span),
                                 node: folder.fold_ident(pth1.node),
                             },
                             sub.map(|x| folder.fold_pat(x)))
                }
                PatLit(e) => PatLit(folder.fold_expr(e)),
                PatEnum(pth, pats) => {
                    PatEnum(folder.fold_path(pth),
                            pats.map(|pats| pats.move_map(|x| folder.fold_pat(x))))
                }
                PatQPath(qself, pth) => {
                    let qself = QSelf { ty: folder.fold_ty(qself.ty), ..qself };
                    PatQPath(qself, folder.fold_path(pth))
                }
                PatStruct(pth, fields, etc) => {
                    let pth = folder.fold_path(pth);
                    let fs = fields.move_map(|f| {
                        Spanned {
                            span: folder.new_span(f.span),
                            node: hir::FieldPat {
                                name: f.node.name,
                                pat: folder.fold_pat(f.node.pat),
                                is_shorthand: f.node.is_shorthand,
                            },
                        }
                    });
                    PatStruct(pth, fs, etc)
                }
                PatTup(elts) => PatTup(elts.move_map(|x| folder.fold_pat(x))),
                PatBox(inner) => PatBox(folder.fold_pat(inner)),
                PatRegion(inner, mutbl) => PatRegion(folder.fold_pat(inner), mutbl),
                PatRange(e1, e2) => {
                    PatRange(folder.fold_expr(e1), folder.fold_expr(e2))
                }
                PatVec(before, slice, after) => {
                    PatVec(before.move_map(|x| folder.fold_pat(x)),
                           slice.map(|x| folder.fold_pat(x)),
                           after.move_map(|x| folder.fold_pat(x)))
                }
1047
            },
N
Nick Cameron 已提交
1048 1049
            span: folder.new_span(span),
        }
1050 1051 1052
    })
}

1053
pub fn noop_fold_expr<T: Folder>(Expr { id, node, span, attrs }: Expr, folder: &mut T) -> Expr {
1054 1055 1056
    Expr {
        id: folder.new_id(id),
        node: match node {
1057 1058
            ExprBox(e) => {
                ExprBox(folder.fold_expr(e))
1059 1060 1061 1062 1063 1064 1065 1066 1067
            }
            ExprVec(exprs) => {
                ExprVec(exprs.move_map(|x| folder.fold_expr(x)))
            }
            ExprRepeat(expr, count) => {
                ExprRepeat(folder.fold_expr(expr), folder.fold_expr(count))
            }
            ExprTup(elts) => ExprTup(elts.move_map(|x| folder.fold_expr(x))),
            ExprCall(f, args) => {
J
Jose Narvaez 已提交
1068
                ExprCall(folder.fold_expr(f), args.move_map(|x| folder.fold_expr(x)))
1069
            }
1070
            ExprMethodCall(name, tps, args) => {
J
Jose Narvaez 已提交
1071
                ExprMethodCall(respan(folder.new_span(name.span), folder.fold_name(name.node)),
N
Nick Cameron 已提交
1072 1073
                               tps.move_map(|x| folder.fold_ty(x)),
                               args.move_map(|x| folder.fold_expr(x)))
1074 1075
            }
            ExprBinary(binop, lhs, rhs) => {
N
Nick Cameron 已提交
1076
                ExprBinary(binop, folder.fold_expr(lhs), folder.fold_expr(rhs))
1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093
            }
            ExprUnary(binop, ohs) => {
                ExprUnary(binop, folder.fold_expr(ohs))
            }
            ExprLit(l) => ExprLit(l),
            ExprCast(expr, ty) => {
                ExprCast(folder.fold_expr(expr), folder.fold_ty(ty))
            }
            ExprAddrOf(m, ohs) => ExprAddrOf(m, folder.fold_expr(ohs)),
            ExprIf(cond, tr, fl) => {
                ExprIf(folder.fold_expr(cond),
                       folder.fold_block(tr),
                       fl.map(|x| folder.fold_expr(x)))
            }
            ExprWhile(cond, body, opt_ident) => {
                ExprWhile(folder.fold_expr(cond),
                          folder.fold_block(body),
1094
                          opt_ident.map(|i| folder.fold_ident(i)))
1095 1096 1097
            }
            ExprLoop(body, opt_ident) => {
                ExprLoop(folder.fold_block(body),
N
Nick Cameron 已提交
1098
                         opt_ident.map(|i| folder.fold_ident(i)))
1099 1100 1101
            }
            ExprMatch(expr, arms, source) => {
                ExprMatch(folder.fold_expr(expr),
N
Nick Cameron 已提交
1102 1103
                          arms.move_map(|x| folder.fold_arm(x)),
                          source)
1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114
            }
            ExprClosure(capture_clause, decl, body) => {
                ExprClosure(capture_clause,
                            folder.fold_fn_decl(decl),
                            folder.fold_block(body))
            }
            ExprBlock(blk) => ExprBlock(folder.fold_block(blk)),
            ExprAssign(el, er) => {
                ExprAssign(folder.fold_expr(el), folder.fold_expr(er))
            }
            ExprAssignOp(op, el, er) => {
N
Nick Cameron 已提交
1115
                ExprAssignOp(op, folder.fold_expr(el), folder.fold_expr(er))
1116
            }
1117
            ExprField(el, name) => {
1118
                ExprField(folder.fold_expr(el),
J
Jose Narvaez 已提交
1119
                          respan(folder.new_span(name.span), folder.fold_name(name.node)))
1120
            }
1121
            ExprTupField(el, index) => {
1122
                ExprTupField(folder.fold_expr(el),
J
Jose Narvaez 已提交
1123
                             respan(folder.new_span(index.span), folder.fold_usize(index.node)))
1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135
            }
            ExprIndex(el, er) => {
                ExprIndex(folder.fold_expr(el), folder.fold_expr(er))
            }
            ExprRange(e1, e2) => {
                ExprRange(e1.map(|x| folder.fold_expr(x)),
                          e2.map(|x| folder.fold_expr(x)))
            }
            ExprPath(qself, path) => {
                let qself = qself.map(|QSelf { ty, position }| {
                    QSelf {
                        ty: folder.fold_ty(ty),
N
Nick Cameron 已提交
1136
                        position: position,
1137 1138 1139 1140
                    }
                });
                ExprPath(qself, folder.fold_path(path))
            }
N
Nick Cameron 已提交
1141
            ExprBreak(opt_ident) => ExprBreak(opt_ident.map(|label| {
J
Jose Narvaez 已提交
1142
                respan(folder.new_span(label.span), folder.fold_ident(label.node))
N
Nick Cameron 已提交
1143 1144
            })),
            ExprAgain(opt_ident) => ExprAgain(opt_ident.map(|label| {
J
Jose Narvaez 已提交
1145
                respan(folder.new_span(label.span), folder.fold_ident(label.node))
N
Nick Cameron 已提交
1146
            })),
1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158
            ExprRet(e) => ExprRet(e.map(|x| folder.fold_expr(x))),
            ExprInlineAsm(InlineAsm {
                inputs,
                outputs,
                asm,
                asm_str_style,
                clobbers,
                volatile,
                alignstack,
                dialect,
                expn_id,
            }) => ExprInlineAsm(InlineAsm {
N
Nick Cameron 已提交
1159 1160
                inputs: inputs.move_map(|(c, input)| (c, folder.fold_expr(input))),
                outputs: outputs.move_map(|(c, out, is_rw)| (c, folder.fold_expr(out), is_rw)),
1161 1162 1163 1164 1165 1166 1167 1168 1169 1170
                asm: asm,
                asm_str_style: asm_str_style,
                clobbers: clobbers,
                volatile: volatile,
                alignstack: alignstack,
                dialect: dialect,
                expn_id: expn_id,
            }),
            ExprStruct(path, fields, maybe_expr) => {
                ExprStruct(folder.fold_path(path),
N
Nick Cameron 已提交
1171 1172 1173
                           fields.move_map(|x| folder.fold_field(x)),
                           maybe_expr.map(|x| folder.fold_expr(x)))
            }
1174
        },
N
Nick Cameron 已提交
1175
        span: folder.new_span(span),
1176
        attrs: attrs.map_thin_attrs(|attrs| fold_attrs(attrs, folder)),
1177 1178 1179
    }
}

1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205
pub fn noop_fold_stmt<T: Folder>(stmt: P<Stmt>, folder: &mut T)
                                 -> P<Stmt> {
    stmt.map(|Spanned { node, span }| {
        let span = folder.new_span(span);
        match node {
            StmtDecl(d, id) => {
                let id = folder.new_id(id);
                Spanned {
                    node: StmtDecl(folder.fold_decl(d), id),
                    span: span
                }
            }
            StmtExpr(e, id) => {
                let id = folder.new_id(id);
                Spanned {
                    node: StmtExpr(folder.fold_expr(e), id),
                    span: span,
                }
            }
            StmtSemi(e, id) => {
                let id = folder.new_id(id);
                Spanned {
                    node: StmtSemi(folder.fold_expr(e), id),
                    span: span,
                }
            }
1206
        }
1207
    })
1208
}