visit.rs 33.0 KB
Newer Older
1
// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
2 3 4 5 6 7 8 9 10
// 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.

11 12 13 14
//! AST walker. Each overridden visit method has full control over what
//! happens with its node, it can do its own traversal of the node's children,
//! call `visit::walk_*` to apply the default traversal algorithm, or prevent
//! deeper traversal by doing nothing.
15 16 17 18 19 20 21
//!
//! Note: it is an important invariant that the default visitor walks the body
//! of a function in "execution order" (more concretely, reverse post-order
//! with respect to the CFG implied by the AST), meaning that if AST node A may
//! execute before AST node B, then A is visited first.  The borrow checker in
//! particular relies on this property.
//!
J
John Clements 已提交
22 23 24 25
//! Note: walking an AST before macro expansion is probably a bad idea. For
//! instance, a walker looking for item names in a module will miss all of
//! those that are created by the expansion of a macro.

S
Steven Fackler 已提交
26 27
pub use self::FnKind::*;

28
use abi::Abi;
P
Patrick Walton 已提交
29
use ast::*;
30
use ast;
31
use codemap::Span;
32
use ptr::P;
33
use owned_slice::OwnedSlice;
34

35
pub enum FnKind<'a> {
36
    /// fn foo() or extern "Abi" fn foo()
37
    FkItemFn(Ident, &'a Generics, FnStyle, Abi),
38

39
    /// fn foo(&self)
40
    FkMethod(Ident, &'a Generics, &'a Method),
41

42 43
    /// |x, y| ...
    /// proc(x, y) ...
44
    FkFnBlock,
45 46
}

47
/// Each method of the Visitor trait is a hook to be potentially
48
/// overridden.  Each method's default implementation recursively visits
49 50 51 52 53 54 55
/// the substructure of the input via the corresponding `walk` method;
/// e.g. the `visit_mod` method by default calls `visit::walk_mod`.
///
/// If you want to ensure that your code handles every variant
/// explicitly, you need to override each method.  (And you also need
/// to monitor future changes to `Visitor` in case a new method with a
/// new default implementation gets introduced.)
56
pub trait Visitor<'v> {
57

58
    fn visit_ident(&mut self, _sp: Span, _ident: Ident) {
59 60
        /*! Visit the idents */
    }
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
    fn visit_mod(&mut self, m: &'v Mod, _s: Span, _n: NodeId) { walk_mod(self, m) }
    fn visit_view_item(&mut self, i: &'v ViewItem) { walk_view_item(self, i) }
    fn visit_foreign_item(&mut self, i: &'v ForeignItem) { walk_foreign_item(self, i) }
    fn visit_item(&mut self, i: &'v Item) { walk_item(self, i) }
    fn visit_local(&mut self, l: &'v Local) { walk_local(self, l) }
    fn visit_block(&mut self, b: &'v Block) { walk_block(self, b) }
    fn visit_stmt(&mut self, s: &'v Stmt) { walk_stmt(self, s) }
    fn visit_arm(&mut self, a: &'v Arm) { walk_arm(self, a) }
    fn visit_pat(&mut self, p: &'v Pat) { walk_pat(self, p) }
    fn visit_decl(&mut self, d: &'v Decl) { walk_decl(self, d) }
    fn visit_expr(&mut self, ex: &'v Expr) { walk_expr(self, ex) }
    fn visit_expr_post(&mut self, _ex: &'v Expr) { }
    fn visit_ty(&mut self, t: &'v Ty) { walk_ty(self, t) }
    fn visit_generics(&mut self, g: &'v Generics) { walk_generics(self, g) }
    fn visit_fn(&mut self, fk: FnKind<'v>, fd: &'v FnDecl, b: &'v Block, s: Span, _: NodeId) {
76 77
        walk_fn(self, fk, fd, b, s)
    }
78 79
    fn visit_ty_method(&mut self, t: &'v TypeMethod) { walk_ty_method(self, t) }
    fn visit_trait_item(&mut self, t: &'v TraitItem) { walk_trait_item(self, t) }
N
Niko Matsakis 已提交
80
    fn visit_trait_ref(&mut self, t: &'v TraitRef) { walk_trait_ref(self, t) }
81 82 83
    fn visit_ty_param_bound(&mut self, bounds: &'v TyParamBound) {
        walk_ty_param_bound(self, bounds)
    }
N
Niko Matsakis 已提交
84 85 86
    fn visit_poly_trait_ref(&mut self, t: &'v PolyTraitRef) {
        walk_poly_trait_ref(self, t)
    }
87
    fn visit_struct_def(&mut self, s: &'v StructDef, _: Ident, _: &'v Generics, _: NodeId) {
88 89
        walk_struct_def(self, s)
    }
90 91
    fn visit_struct_field(&mut self, s: &'v StructField) { walk_struct_field(self, s) }
    fn visit_variant(&mut self, v: &'v Variant, g: &'v Generics) { walk_variant(self, v, g) }
92 93
    fn visit_opt_lifetime_ref(&mut self,
                              _span: Span,
94
                              opt_lifetime: &'v Option<Lifetime>) {
95 96 97 98 99 100
        /*!
         * Visits an optional reference to a lifetime. The `span` is
         * the span of some surrounding reference should opt_lifetime
         * be None.
         */
        match *opt_lifetime {
101
            Some(ref l) => self.visit_lifetime_ref(l),
102 103 104
            None => ()
        }
    }
105
    fn visit_lifetime_ref(&mut self, _lifetime: &'v Lifetime) {
106 107
        /*! Visits a reference to a lifetime */
    }
108
    fn visit_lifetime_decl(&mut self, _lifetime: &'v LifetimeDef) {
109 110
        /*! Visits a declaration of a lifetime */
    }
111
    fn visit_explicit_self(&mut self, es: &'v ExplicitSelf) {
112
        walk_explicit_self(self, es)
113
    }
114
    fn visit_mac(&mut self, _macro: &'v Mac) {
S
Steve Klabnik 已提交
115
        panic!("visit_mac disabled by default");
J
John Clements 已提交
116 117 118 119
        // NB: see note about macros above.
        // if you really want a visitor that
        // works on macros, use this
        // definition in your trait impl:
120
        // visit::walk_mac(self, _macro)
121
    }
122
    fn visit_path(&mut self, path: &'v Path, _id: ast::NodeId) {
123
        walk_path(self, path)
124
    }
125 126 127 128 129 130
    fn visit_path_segment(&mut self, path_span: Span, path_segment: &'v PathSegment) {
        walk_path_segment(self, path_span, path_segment)
    }
    fn visit_path_parameters(&mut self, path_span: Span, path_parameters: &'v PathParameters) {
        walk_path_parameters(self, path_span, path_parameters)
    }
131
    fn visit_attribute(&mut self, _attr: &'v Attribute) {}
132 133
}

134 135
pub fn walk_inlined_item<'v,V>(visitor: &mut V, item: &'v InlinedItem)
                         where V: Visitor<'v> {
136
    match *item {
137 138
        IIItem(ref i) => visitor.visit_item(&**i),
        IIForeign(ref i) => visitor.visit_foreign_item(&**i),
139
        IITraitItem(_, ref ti) => visitor.visit_trait_item(ti),
140 141 142 143 144 145 146
        IIImplItem(_, MethodImplItem(ref m)) => {
            walk_method_helper(visitor, &**m)
        }
        IIImplItem(_, TypeImplItem(ref typedef)) => {
            visitor.visit_ident(typedef.span, typedef.ident);
            visitor.visit_ty(&*typedef.typ);
        }
147 148 149 150
    }
}


151
pub fn walk_crate<'v, V: Visitor<'v>>(visitor: &mut V, krate: &'v Crate) {
152
    visitor.visit_mod(&krate.module, krate.span, CRATE_NODE_ID);
153
    for attr in krate.attrs.iter() {
154
        visitor.visit_attribute(attr);
155
    }
156 157
}

158
pub fn walk_mod<'v, V: Visitor<'v>>(visitor: &mut V, module: &'v Mod) {
D
Daniel Micay 已提交
159
    for view_item in module.view_items.iter() {
160
        visitor.visit_view_item(view_item)
161
    }
162

D
Daniel Micay 已提交
163
    for item in module.items.iter() {
164
        visitor.visit_item(&**item)
165 166 167
    }
}

168
pub fn walk_view_item<'v, V: Visitor<'v>>(visitor: &mut V, vi: &'v ViewItem) {
169
    match vi.node {
170
        ViewItemExternCrate(name, _, _) => {
171
            visitor.visit_ident(vi.span, name)
172
        }
173 174 175
        ViewItemUse(ref vp) => {
            match vp.node {
                ViewPathSimple(ident, ref path, id) => {
176 177
                    visitor.visit_ident(vp.span, ident);
                    visitor.visit_path(path, id);
178 179
                }
                ViewPathGlob(ref path, id) => {
180
                    visitor.visit_path(path, id);
181
                }
182
                ViewPathList(ref prefix, ref list, _) => {
183
                    for id in list.iter() {
J
Jakub Wieczorek 已提交
184 185
                        match id.node {
                            PathListIdent { name, .. } => {
186
                                visitor.visit_ident(id.span, name);
J
Jakub Wieczorek 已提交
187 188 189
                            }
                            PathListMod { .. } => ()
                        }
190
                    }
191 192 193 194

                    // Note that the `prefix` here is not a complete
                    // path, so we don't use `visit_path`.
                    walk_path(visitor, prefix);
195
                }
196 197 198
            }
        }
    }
199
    for attr in vi.attrs.iter() {
200
        visitor.visit_attribute(attr);
201
    }
202 203
}

204
pub fn walk_local<'v, V: Visitor<'v>>(visitor: &mut V, local: &'v Local) {
205 206
    visitor.visit_pat(&*local.pat);
    visitor.visit_ty(&*local.ty);
207
    walk_expr_opt(visitor, &local.init);
208 209
}

210 211
pub fn walk_explicit_self<'v, V: Visitor<'v>>(visitor: &mut V,
                                              explicit_self: &'v ExplicitSelf) {
212
    match explicit_self.node {
213
        SelfStatic | SelfValue(_) => {},
214
        SelfRegion(ref lifetime, _, _) => {
215
            visitor.visit_opt_lifetime_ref(explicit_self.span, lifetime)
216
        }
217
        SelfExplicit(ref typ, _) => visitor.visit_ty(&**typ),
218 219 220
    }
}

221 222
/// Like with walk_method_helper this doesn't correspond to a method
/// in Visitor, and so it gets a _helper suffix.
N
Niko Matsakis 已提交
223 224 225 226
pub fn walk_poly_trait_ref<'v, V>(visitor: &mut V,
                                         trait_ref: &'v PolyTraitRef)
    where V: Visitor<'v>
{
227
    walk_lifetime_decls_helper(visitor, &trait_ref.bound_lifetimes);
N
Niko Matsakis 已提交
228 229 230 231 232 233 234 235 236
    visitor.visit_trait_ref(&trait_ref.trait_ref);
}

/// Like with walk_method_helper this doesn't correspond to a method
/// in Visitor, and so it gets a _helper suffix.
pub fn walk_trait_ref<'v,V>(visitor: &mut V,
                                   trait_ref: &'v TraitRef)
    where V: Visitor<'v>
{
237
    visitor.visit_path(&trait_ref.path, trait_ref.ref_id)
238 239
}

240
pub fn walk_item<'v, V: Visitor<'v>>(visitor: &mut V, item: &'v Item) {
241
    visitor.visit_ident(item.span, item.ident);
242
    match item.node {
243 244
        ItemStatic(ref typ, _, ref expr) |
        ItemConst(ref typ, ref expr) => {
245 246
            visitor.visit_ty(&**typ);
            visitor.visit_expr(&**expr);
247
        }
248 249 250 251
        ItemFn(ref declaration, fn_style, abi, ref generics, ref body) => {
            visitor.visit_fn(FkItemFn(item.ident, generics, fn_style, abi),
                             &**declaration,
                             &**body,
252
                             item.span,
253
                             item.id)
254
        }
255
        ItemMod(ref module) => {
256
            visitor.visit_mod(module, item.span, item.id)
257
        }
258
        ItemForeignMod(ref foreign_module) => {
D
Daniel Micay 已提交
259
            for view_item in foreign_module.view_items.iter() {
260
                visitor.visit_view_item(view_item)
261
            }
D
Daniel Micay 已提交
262
            for foreign_item in foreign_module.items.iter() {
263
                visitor.visit_foreign_item(&**foreign_item)
264
            }
265
        }
266
        ItemTy(ref typ, ref type_parameters) => {
267 268
            visitor.visit_ty(&**typ);
            visitor.visit_generics(type_parameters)
269
        }
270
        ItemEnum(ref enum_definition, ref type_parameters) => {
271 272
            visitor.visit_generics(type_parameters);
            walk_enum_def(visitor, enum_definition, type_parameters)
273
        }
274
        ItemImpl(ref type_parameters,
275
                 ref trait_reference,
276
                 ref typ,
277
                 ref impl_items) => {
278
            visitor.visit_generics(type_parameters);
279
            match *trait_reference {
N
Niko Matsakis 已提交
280
                Some(ref trait_reference) => visitor.visit_trait_ref(trait_reference),
281
                None => ()
282
            }
283
            visitor.visit_ty(&**typ);
284 285
            for impl_item in impl_items.iter() {
                match *impl_item {
286 287
                    MethodImplItem(ref method) => {
                        walk_method_helper(visitor, &**method)
288
                    }
289 290 291 292
                    TypeImplItem(ref typedef) => {
                        visitor.visit_ident(typedef.span, typedef.ident);
                        visitor.visit_ty(&*typedef.typ);
                    }
293
                }
294 295
            }
        }
296
        ItemStruct(ref struct_definition, ref generics) => {
297
            visitor.visit_generics(generics);
298
            visitor.visit_struct_def(&**struct_definition,
299 300
                                     item.ident,
                                     generics,
301
                                     item.id)
302
        }
303
        ItemTrait(ref generics, _, ref bounds, ref methods) => {
304
            visitor.visit_generics(generics);
305
            walk_ty_param_bounds_helper(visitor, bounds);
D
Daniel Micay 已提交
306
            for method in methods.iter() {
307
                visitor.visit_trait_item(method)
308 309
            }
        }
310
        ItemMac(ref macro) => visitor.visit_mac(macro),
311 312
    }
    for attr in item.attrs.iter() {
313
        visitor.visit_attribute(attr);
M
Marijn Haverbeke 已提交
314 315 316
    }
}

317 318 319 320 321
pub fn walk_enum_def<'v, V: Visitor<'v>>(visitor: &mut V,
                                         enum_definition: &'v EnumDef,
                                         generics: &'v Generics) {
    for variant in enum_definition.variants.iter() {
        visitor.visit_variant(&**variant, generics);
322 323 324
    }
}

325 326 327
pub fn walk_variant<'v, V: Visitor<'v>>(visitor: &mut V,
                                        variant: &'v Variant,
                                        generics: &'v Generics) {
328
    visitor.visit_ident(variant.span, variant.node.name);
329

330
    match variant.node.kind {
331
        TupleVariantKind(ref variant_arguments) => {
332
            for variant_argument in variant_arguments.iter() {
333
                visitor.visit_ty(&*variant_argument.ty)
334 335
            }
        }
336 337
        StructVariantKind(ref struct_definition) => {
            visitor.visit_struct_def(&**struct_definition,
338 339
                                     variant.node.name,
                                     generics,
340
                                     variant.node.id)
341
        }
342
    }
343
    match variant.node.disr_expr {
344
        Some(ref expr) => visitor.visit_expr(&**expr),
345 346
        None => ()
    }
347
    for attr in variant.node.attrs.iter() {
348
        visitor.visit_attribute(attr);
349
    }
350 351
}

352
pub fn skip_ty<'v, V: Visitor<'v>>(_: &mut V, _: &'v Ty) {
353 354
    // Empty!
}
355

356
pub fn walk_ty<'v, V: Visitor<'v>>(visitor: &mut V, typ: &'v Ty) {
357
    match typ.node {
358
        TyVec(ref ty) | TyParen(ref ty) => {
359
            visitor.visit_ty(&**ty)
360
        }
361
        TyPtr(ref mutable_type) => {
362
            visitor.visit_ty(&*mutable_type.ty)
363
        }
364
        TyRptr(ref lifetime, ref mutable_type) => {
365 366
            visitor.visit_opt_lifetime_ref(typ.span, lifetime);
            visitor.visit_ty(&*mutable_type.ty)
367
        }
368
        TyTup(ref tuple_element_types) => {
369 370
            for tuple_element_type in tuple_element_types.iter() {
                visitor.visit_ty(&**tuple_element_type)
371
            }
372
        }
373
        TyClosure(ref function_declaration) => {
374
            for argument in function_declaration.decl.inputs.iter() {
375
                visitor.visit_ty(&*argument.ty)
376
            }
377
            walk_fn_ret_ty(visitor, &function_declaration.decl.output);
378 379
            walk_ty_param_bounds_helper(visitor, &function_declaration.bounds);
            walk_lifetime_decls_helper(visitor, &function_declaration.lifetimes);
380
        }
E
Eduard Burtescu 已提交
381 382
        TyProc(ref function_declaration) => {
            for argument in function_declaration.decl.inputs.iter() {
383
                visitor.visit_ty(&*argument.ty)
E
Eduard Burtescu 已提交
384
            }
385
            walk_fn_ret_ty(visitor, &function_declaration.decl.output);
386 387
            walk_ty_param_bounds_helper(visitor, &function_declaration.bounds);
            walk_lifetime_decls_helper(visitor, &function_declaration.lifetimes);
E
Eduard Burtescu 已提交
388
        }
389
        TyBareFn(ref function_declaration) => {
D
Daniel Micay 已提交
390
            for argument in function_declaration.decl.inputs.iter() {
391
                visitor.visit_ty(&*argument.ty)
392
            }
393
            walk_fn_ret_ty(visitor, &function_declaration.decl.output);
394
            walk_lifetime_decls_helper(visitor, &function_declaration.lifetimes);
395
        }
396
        TyPath(ref path, ref opt_bounds, id) => {
397
            visitor.visit_path(path, id);
398 399
            match *opt_bounds {
                Some(ref bounds) => {
400
                    walk_ty_param_bounds_helper(visitor, bounds);
401 402
                }
                None => { }
403
            }
404
        }
405 406 407 408 409
        TyQPath(ref qpath) => {
            visitor.visit_ty(&*qpath.for_type);
            visitor.visit_path(&qpath.trait_name, typ.id);
            visitor.visit_ident(typ.span, qpath.item_name);
        }
410
        TyFixedLengthVec(ref ty, ref expression) => {
411 412
            visitor.visit_ty(&**ty);
            visitor.visit_expr(&**expression)
413
        }
414 415
        TyPolyTraitRef(ref bounds) => {
            walk_ty_param_bounds_helper(visitor, bounds)
N
Niko Matsakis 已提交
416
        }
417
        TyTypeof(ref expression) => {
418
            visitor.visit_expr(&**expression)
419
        }
420
        TyInfer => {}
M
Marijn Haverbeke 已提交
421 422 423
    }
}

424 425
pub fn walk_lifetime_decls_helper<'v, V: Visitor<'v>>(visitor: &mut V,
                                                      lifetimes: &'v Vec<LifetimeDef>) {
426
    for l in lifetimes.iter() {
427
        visitor.visit_lifetime_decl(l);
428 429 430
    }
}

431
pub fn walk_path<'v, V: Visitor<'v>>(visitor: &mut V, path: &'v Path) {
432
    for segment in path.segments.iter() {
433 434 435
        visitor.visit_path_segment(path.span, segment);
    }
}
436

437 438 439 440 441 442 443 444 445 446 447 448 449 450
pub fn walk_path_segment<'v, V: Visitor<'v>>(visitor: &mut V,
                                             path_span: Span,
                                             segment: &'v PathSegment) {
    visitor.visit_ident(path_span, segment.identifier);
    visitor.visit_path_parameters(path_span, &segment.parameters);
}

pub fn walk_path_parameters<'v, V: Visitor<'v>>(visitor: &mut V,
                                                _path_span: Span,
                                                path_parameters: &'v PathParameters) {
    match *path_parameters {
        ast::AngleBracketedParameters(ref data) => {
            for typ in data.types.iter() {
                visitor.visit_ty(&**typ);
451
            }
452 453 454 455 456 457 458 459 460 461
            for lifetime in data.lifetimes.iter() {
                visitor.visit_lifetime_ref(lifetime);
            }
        }
        ast::ParenthesizedParameters(ref data) => {
            for typ in data.inputs.iter() {
                visitor.visit_ty(&**typ);
            }
            for typ in data.output.iter() {
                visitor.visit_ty(&**typ);
462
            }
463
        }
464
    }
465 466
}

467
pub fn walk_pat<'v, V: Visitor<'v>>(visitor: &mut V, pattern: &'v Pat) {
468
    match pattern.node {
469
        PatEnum(ref path, ref children) => {
470
            visitor.visit_path(path, pattern.id);
D
Daniel Micay 已提交
471 472
            for children in children.iter() {
                for child in children.iter() {
473
                    visitor.visit_pat(&**child)
474
                }
475
            }
476
        }
477
        PatStruct(ref path, ref fields, _) => {
478
            visitor.visit_path(path, pattern.id);
D
Daniel Micay 已提交
479
            for field in fields.iter() {
480
                visitor.visit_pat(&*field.node.pat)
481 482
            }
        }
483
        PatTup(ref tuple_elements) => {
D
Daniel Micay 已提交
484
            for tuple_element in tuple_elements.iter() {
485
                visitor.visit_pat(&**tuple_element)
486
            }
487
        }
488 489
        PatBox(ref subpattern) |
        PatRegion(ref subpattern) => {
490
            visitor.visit_pat(&**subpattern)
491
        }
492
        PatIdent(_, ref pth1, ref optional_subpattern) => {
493
            visitor.visit_ident(pth1.span, pth1.node);
494 495
            match *optional_subpattern {
                None => {}
496
                Some(ref subpattern) => visitor.visit_pat(&**subpattern),
497
            }
498
        }
499
        PatLit(ref expression) => visitor.visit_expr(&**expression),
500
        PatRange(ref lower_bound, ref upper_bound) => {
501 502
            visitor.visit_expr(&**lower_bound);
            visitor.visit_expr(&**upper_bound)
503
        }
504
        PatWild(_) => (),
505
        PatVec(ref prepattern, ref slice_pattern, ref postpatterns) => {
D
Daniel Micay 已提交
506
            for prepattern in prepattern.iter() {
507
                visitor.visit_pat(&**prepattern)
508
            }
D
Daniel Micay 已提交
509
            for slice_pattern in slice_pattern.iter() {
510
                visitor.visit_pat(&**slice_pattern)
511
            }
D
Daniel Micay 已提交
512
            for postpattern in postpatterns.iter() {
513
                visitor.visit_pat(&**postpattern)
514
            }
515
        }
516
        PatMac(ref macro) => visitor.visit_mac(macro),
M
Marijn Haverbeke 已提交
517 518 519
    }
}

520 521
pub fn walk_foreign_item<'v, V: Visitor<'v>>(visitor: &mut V,
                                             foreign_item: &'v ForeignItem) {
522
    visitor.visit_ident(foreign_item.span, foreign_item.ident);
523

524
    match foreign_item.node {
525
        ForeignItemFn(ref function_declaration, ref generics) => {
526 527
            walk_fn_decl(visitor, &**function_declaration);
            visitor.visit_generics(generics)
528
        }
529
        ForeignItemStatic(ref typ, _) => visitor.visit_ty(&**typ),
530 531 532
    }

    for attr in foreign_item.attrs.iter() {
533
        visitor.visit_attribute(attr);
M
Marijn Haverbeke 已提交
534 535 536
    }
}

537 538
pub fn walk_ty_param_bounds_helper<'v, V: Visitor<'v>>(visitor: &mut V,
                                                       bounds: &'v OwnedSlice<TyParamBound>) {
D
Daniel Micay 已提交
539
    for bound in bounds.iter() {
540 541 542 543 544 545 546 547 548 549 550 551
        visitor.visit_ty_param_bound(bound)
    }
}

pub fn walk_ty_param_bound<'v, V: Visitor<'v>>(visitor: &mut V,
                                               bound: &'v TyParamBound) {
    match *bound {
        TraitTyParamBound(ref typ) => {
            visitor.visit_poly_trait_ref(typ);
        }
        RegionTyParamBound(ref lifetime) => {
            visitor.visit_lifetime_ref(lifetime);
552
        }
553 554 555
    }
}

556
pub fn walk_generics<'v, V: Visitor<'v>>(visitor: &mut V, generics: &'v Generics) {
D
Daniel Micay 已提交
557
    for type_parameter in generics.ty_params.iter() {
558
        walk_ty_param_bounds_helper(visitor, &type_parameter.bounds);
559
        match type_parameter.default {
560
            Some(ref ty) => visitor.visit_ty(&**ty),
561 562
            None => {}
        }
563
    }
564
    walk_lifetime_decls_helper(visitor, &generics.lifetimes);
565
    for predicate in generics.where_clause.predicates.iter() {
566
        visitor.visit_ident(predicate.span, predicate.ident);
567
        walk_ty_param_bounds_helper(visitor, &predicate.bounds);
568
    }
569 570
}

571 572 573 574 575 576
pub fn walk_fn_ret_ty<'v, V: Visitor<'v>>(visitor: &mut V, ret_ty: &'v FunctionRetTy) {
    if let Return(ref output_ty) = *ret_ty {
        visitor.visit_ty(&**output_ty)
    }
}

577
pub fn walk_fn_decl<'v, V: Visitor<'v>>(visitor: &mut V, function_declaration: &'v FnDecl) {
D
Daniel Micay 已提交
578
    for argument in function_declaration.inputs.iter() {
579 580
        visitor.visit_pat(&*argument.pat);
        visitor.visit_ty(&*argument.ty)
581
    }
582
    walk_fn_ret_ty(visitor, &function_declaration.output)
M
Marijn Haverbeke 已提交
583 584
}

585
// Note: there is no visit_method() method in the visitor, instead override
586
// visit_fn() and check for FkMethod().  I named this visit_method_helper()
587 588
// because it is not a default impl of any method, though I doubt that really
// clarifies anything. - Niko
589
pub fn walk_method_helper<'v, V: Visitor<'v>>(visitor: &mut V, method: &'v Method) {
590
    match method.node {
591
        MethDecl(ident, ref generics, _, _, _, ref decl, ref body, _) => {
592
            visitor.visit_ident(method.span, ident);
593 594 595
            visitor.visit_fn(FkMethod(ident, generics, method),
                             &**decl,
                             &**body,
596
                             method.span,
597
                             method.id);
598
            for attr in method.attrs.iter() {
599
                visitor.visit_attribute(attr);
600 601 602
            }

        },
603
        MethMac(ref mac) => visitor.visit_mac(mac)
604
    }
605 606
}

607 608 609 610 611
pub fn walk_fn<'v, V: Visitor<'v>>(visitor: &mut V,
                                   function_kind: FnKind<'v>,
                                   function_declaration: &'v FnDecl,
                                   function_body: &'v Block,
                                   _span: Span) {
612
    walk_fn_decl(visitor, function_declaration);
613

614
    match function_kind {
615
        FkItemFn(_, generics, _, _) => {
616
            visitor.visit_generics(generics);
617
        }
618
        FkMethod(_, generics, method) => {
619
            visitor.visit_generics(generics);
620
            match method.node {
621
                MethDecl(_, _, _, ref explicit_self, _, _, _, _) =>
622
                    visitor.visit_explicit_self(explicit_self),
623
                MethMac(ref mac) =>
624
                    visitor.visit_mac(mac)
625
            }
626
        }
627
        FkFnBlock(..) => {}
628 629
    }

630
    visitor.visit_block(function_body)
M
Marijn Haverbeke 已提交
631 632
}

633
pub fn walk_ty_method<'v, V: Visitor<'v>>(visitor: &mut V, method_type: &'v TypeMethod) {
634 635
    visitor.visit_ident(method_type.span, method_type.ident);
    visitor.visit_explicit_self(&method_type.explicit_self);
D
Daniel Micay 已提交
636
    for argument_type in method_type.decl.inputs.iter() {
637
        visitor.visit_ty(&*argument_type.ty)
638
    }
639
    visitor.visit_generics(&method_type.generics);
640
    walk_fn_ret_ty(visitor, &method_type.decl.output);
641
    for attr in method_type.attrs.iter() {
642
        visitor.visit_attribute(attr);
643
    }
644 645
}

646
pub fn walk_trait_item<'v, V: Visitor<'v>>(visitor: &mut V, trait_method: &'v TraitItem) {
647
    match *trait_method {
648
        RequiredMethod(ref method_type) => visitor.visit_ty_method(method_type),
649
        ProvidedMethod(ref method) => walk_method_helper(visitor, &**method),
650
        TypeTraitItem(ref associated_type) => {
651 652
            visitor.visit_ident(associated_type.ty_param.span,
                                associated_type.ty_param.ident)
653
        }
654 655 656
    }
}

657 658
pub fn walk_struct_def<'v, V: Visitor<'v>>(visitor: &mut V,
                                           struct_definition: &'v StructDef) {
D
Daniel Micay 已提交
659
    for field in struct_definition.fields.iter() {
660
        visitor.visit_struct_field(field)
661
    }
662 663
}

664 665
pub fn walk_struct_field<'v, V: Visitor<'v>>(visitor: &mut V,
                                             struct_field: &'v StructField) {
666
    match struct_field.node.kind {
667
        NamedField(name, _) => {
668
            visitor.visit_ident(struct_field.span, name)
669 670 671 672
        }
        _ => {}
    }

673
    visitor.visit_ty(&*struct_field.node.ty);
674 675

    for attr in struct_field.node.attrs.iter() {
676
        visitor.visit_attribute(attr);
677
    }
678 679
}

680
pub fn walk_block<'v, V: Visitor<'v>>(visitor: &mut V, block: &'v Block) {
D
Daniel Micay 已提交
681
    for view_item in block.view_items.iter() {
682
        visitor.visit_view_item(view_item)
683
    }
D
Daniel Micay 已提交
684
    for statement in block.stmts.iter() {
685
        visitor.visit_stmt(&**statement)
686
    }
687
    walk_expr_opt(visitor, &block.expr)
M
Marijn Haverbeke 已提交
688 689
}

690
pub fn walk_stmt<'v, V: Visitor<'v>>(visitor: &mut V, statement: &'v Stmt) {
691
    match statement.node {
692
        StmtDecl(ref declaration, _) => visitor.visit_decl(&**declaration),
693
        StmtExpr(ref expression, _) | StmtSemi(ref expression, _) => {
694
            visitor.visit_expr(&**expression)
695
        }
696
        StmtMac(ref macro, _) => visitor.visit_mac(macro),
M
Marijn Haverbeke 已提交
697 698 699
    }
}

700
pub fn walk_decl<'v, V: Visitor<'v>>(visitor: &mut V, declaration: &'v Decl) {
701
    match declaration.node {
702 703
        DeclLocal(ref local) => visitor.visit_local(&**local),
        DeclItem(ref item) => visitor.visit_item(&**item),
M
Marijn Haverbeke 已提交
704 705 706
    }
}

707
pub fn walk_expr_opt<'v, V: Visitor<'v>>(visitor: &mut V,
708
                                         optional_expression: &'v Option<P<Expr>>) {
709
    match *optional_expression {
710
        None => {}
711
        Some(ref expression) => visitor.visit_expr(&**expression),
712
    }
M
Marijn Haverbeke 已提交
713 714
}

715
pub fn walk_exprs<'v, V: Visitor<'v>>(visitor: &mut V, expressions: &'v [P<Expr>]) {
D
Daniel Micay 已提交
716
    for expression in expressions.iter() {
717
        visitor.visit_expr(&**expression)
718
    }
M
Marijn Haverbeke 已提交
719 720
}

721
pub fn walk_mac<'v, V: Visitor<'v>>(_: &mut V, _: &'v Mac) {
722
    // Empty!
723 724
}

725
pub fn walk_expr<'v, V: Visitor<'v>>(visitor: &mut V, expression: &'v Expr) {
726
    match expression.node {
727
        ExprBox(ref place, ref subexpression) => {
728 729
            visitor.visit_expr(&**place);
            visitor.visit_expr(&**subexpression)
730
        }
731
        ExprVec(ref subexpressions) => {
732
            walk_exprs(visitor, subexpressions.as_slice())
733
        }
734
        ExprRepeat(ref element, ref count) => {
735 736
            visitor.visit_expr(&**element);
            visitor.visit_expr(&**count)
737
        }
738
        ExprStruct(ref path, ref fields, ref optional_base) => {
739
            visitor.visit_path(path, expression.id);
D
Daniel Micay 已提交
740
            for field in fields.iter() {
741
                visitor.visit_expr(&*field.expr)
742
            }
743
            walk_expr_opt(visitor, optional_base)
744
        }
745
        ExprTup(ref subexpressions) => {
D
Daniel Micay 已提交
746
            for subexpression in subexpressions.iter() {
747
                visitor.visit_expr(&**subexpression)
748
            }
749
        }
750
        ExprCall(ref callee_expression, ref arguments) => {
D
Daniel Micay 已提交
751
            for argument in arguments.iter() {
752
                visitor.visit_expr(&**argument)
753
            }
754
            visitor.visit_expr(&**callee_expression)
755
        }
756
        ExprMethodCall(_, ref types, ref arguments) => {
757
            walk_exprs(visitor, arguments.as_slice());
758
            for typ in types.iter() {
759
                visitor.visit_ty(&**typ)
760 761
            }
        }
762
        ExprBinary(_, ref left_expression, ref right_expression) => {
763 764
            visitor.visit_expr(&**left_expression);
            visitor.visit_expr(&**right_expression)
765
        }
766
        ExprAddrOf(_, ref subexpression) | ExprUnary(_, ref subexpression) => {
767
            visitor.visit_expr(&**subexpression)
768
        }
769
        ExprLit(_) => {}
770
        ExprCast(ref subexpression, ref typ) => {
771 772
            visitor.visit_expr(&**subexpression);
            visitor.visit_ty(&**typ)
773
        }
774
        ExprIf(ref head_expression, ref if_block, ref optional_else) => {
775 776 777
            visitor.visit_expr(&**head_expression);
            visitor.visit_block(&**if_block);
            walk_expr_opt(visitor, optional_else)
778
        }
P
Pythoner6 已提交
779
        ExprWhile(ref subexpression, ref block, _) => {
780 781
            visitor.visit_expr(&**subexpression);
            visitor.visit_block(&**block)
782
        }
K
Kevin Ballard 已提交
783 784 785 786 787 788
        ExprIfLet(ref pattern, ref subexpression, ref if_block, ref optional_else) => {
            visitor.visit_pat(&**pattern);
            visitor.visit_expr(&**subexpression);
            visitor.visit_block(&**if_block);
            walk_expr_opt(visitor, optional_else);
        }
J
John Gallagher 已提交
789 790 791 792 793
        ExprWhileLet(ref pattern, ref subexpression, ref block, _) => {
            visitor.visit_pat(&**pattern);
            visitor.visit_expr(&**subexpression);
            visitor.visit_block(&**block);
        }
794
        ExprForLoop(ref pattern, ref subexpression, ref block, _) => {
795 796 797
            visitor.visit_pat(&**pattern);
            visitor.visit_expr(&**subexpression);
            visitor.visit_block(&**block)
798
        }
799
        ExprLoop(ref block, _) => visitor.visit_block(&**block),
K
Kevin Ballard 已提交
800
        ExprMatch(ref subexpression, ref arms, _) => {
801
            visitor.visit_expr(&**subexpression);
D
Daniel Micay 已提交
802
            for arm in arms.iter() {
803
                visitor.visit_arm(arm)
804
            }
805
        }
806
        ExprFnBlock(_, ref function_declaration, ref body) => {
807
            visitor.visit_fn(FkFnBlock,
808 809
                             &**function_declaration,
                             &**body,
810
                             expression.span,
811
                             expression.id)
812
        }
813
        ExprUnboxedFn(_, _, ref function_declaration, ref body) => {
814
            visitor.visit_fn(FkFnBlock,
815 816 817
                             &**function_declaration,
                             &**body,
                             expression.span,
818
                             expression.id)
819
        }
820
        ExprProc(ref function_declaration, ref body) => {
821
            visitor.visit_fn(FkFnBlock,
822 823
                             &**function_declaration,
                             &**body,
824
                             expression.span,
825
                             expression.id)
826
        }
827
        ExprBlock(ref block) => visitor.visit_block(&**block),
828
        ExprAssign(ref left_hand_expression, ref right_hand_expression) => {
829 830
            visitor.visit_expr(&**right_hand_expression);
            visitor.visit_expr(&**left_hand_expression)
831
        }
832
        ExprAssignOp(_, ref left_expression, ref right_expression) => {
833 834
            visitor.visit_expr(&**right_expression);
            visitor.visit_expr(&**left_expression)
835
        }
836
        ExprField(ref subexpression, _, ref types) => {
837
            visitor.visit_expr(&**subexpression);
838
            for typ in types.iter() {
839
                visitor.visit_ty(&**typ)
840
            }
841
        }
842
        ExprTupField(ref subexpression, _, ref types) => {
843
            visitor.visit_expr(&**subexpression);
844
            for typ in types.iter() {
845
                visitor.visit_ty(&**typ)
846 847
            }
        }
848
        ExprIndex(ref main_expression, ref index_expression) => {
849 850
            visitor.visit_expr(&**main_expression);
            visitor.visit_expr(&**index_expression)
851
        }
N
Nick Cameron 已提交
852 853 854 855 856
        ExprSlice(ref main_expression, ref start, ref end, _) => {
            visitor.visit_expr(&**main_expression);
            walk_expr_opt(visitor, start);
            walk_expr_opt(visitor, end)
        }
857
        ExprPath(ref path) => {
858
            visitor.visit_path(path, expression.id)
859
        }
860
        ExprBreak(_) | ExprAgain(_) => {}
861
        ExprRet(ref optional_expression) => {
862
            walk_expr_opt(visitor, optional_expression)
863
        }
864
        ExprMac(ref macro) => visitor.visit_mac(macro),
865
        ExprParen(ref subexpression) => {
866
            visitor.visit_expr(&**subexpression)
867
        }
868
        ExprInlineAsm(ref ia) => {
869 870
            for input in ia.inputs.iter() {
                let (_, ref input) = *input;
871
                visitor.visit_expr(&**input)
872
            }
873 874
            for output in ia.outputs.iter() {
                let (_, ref output, _) = *output;
875
                visitor.visit_expr(&**output)
876 877
            }
        }
M
Marijn Haverbeke 已提交
878
    }
879

880
    visitor.visit_expr_post(expression)
M
Marijn Haverbeke 已提交
881 882
}

883
pub fn walk_arm<'v, V: Visitor<'v>>(visitor: &mut V, arm: &'v Arm) {
D
Daniel Micay 已提交
884
    for pattern in arm.pats.iter() {
885
        visitor.visit_pat(&**pattern)
886
    }
887
    walk_expr_opt(visitor, &arm.guard);
888
    visitor.visit_expr(&*arm.body);
889
    for attr in arm.attrs.iter() {
890
        visitor.visit_attribute(attr);
891
    }
M
Marijn Haverbeke 已提交
892
}