visit.rs 30.7 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.

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

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

37
    /// fn foo(&self)
38
    FkMethod(Ident, &'a Generics, &'a Method),
39

40 41
    /// |x, y| ...
    /// proc(x, y) ...
42
    FkFnBlock,
43 44
}

45
/// Each method of the Visitor trait is a hook to be potentially
46
/// overridden.  Each method's default implementation recursively visits
47 48 49 50 51 52 53
/// 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.)
54
pub trait Visitor<'v> {
55

56
    fn visit_ident(&mut self, _sp: Span, _ident: Ident) {
57 58
        /*! Visit the idents */
    }
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
    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) {
74 75
        walk_fn(self, fk, fd, b, s)
    }
76 77 78
    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) }
    fn visit_struct_def(&mut self, s: &'v StructDef, _: Ident, _: &'v Generics, _: NodeId) {
79 80
        walk_struct_def(self, s)
    }
81 82
    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) }
83 84
    fn visit_opt_lifetime_ref(&mut self,
                              _span: Span,
85
                              opt_lifetime: &'v Option<Lifetime>) {
86 87 88 89 90 91
        /*!
         * Visits an optional reference to a lifetime. The `span` is
         * the span of some surrounding reference should opt_lifetime
         * be None.
         */
        match *opt_lifetime {
92
            Some(ref l) => self.visit_lifetime_ref(l),
93 94 95
            None => ()
        }
    }
96
    fn visit_lifetime_ref(&mut self, _lifetime: &'v Lifetime) {
97 98
        /*! Visits a reference to a lifetime */
    }
99
    fn visit_lifetime_decl(&mut self, _lifetime: &'v LifetimeDef) {
100 101
        /*! Visits a declaration of a lifetime */
    }
102
    fn visit_explicit_self(&mut self, es: &'v ExplicitSelf) {
103
        walk_explicit_self(self, es)
104
    }
105
    fn visit_mac(&mut self, _macro: &'v Mac) {
J
John Clements 已提交
106 107 108 109 110
        fail!("visit_mac disabled by default");
        // NB: see note about macros above.
        // if you really want a visitor that
        // works on macros, use this
        // definition in your trait impl:
111
        // visit::walk_mac(self, _macro)
112
    }
113
    fn visit_path(&mut self, path: &'v Path, _id: ast::NodeId) {
114
        walk_path(self, path)
115
    }
116
    fn visit_attribute(&mut self, _attr: &'v Attribute) {}
117 118
}

119 120
pub fn walk_inlined_item<'v,V>(visitor: &mut V, item: &'v InlinedItem)
                         where V: Visitor<'v> {
121
    match *item {
122 123
        IIItem(ref i) => visitor.visit_item(&**i),
        IIForeign(ref i) => visitor.visit_foreign_item(&**i),
124
        IITraitItem(_, ref ti) => visitor.visit_trait_item(ti),
125 126 127 128 129 130 131
        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);
        }
132 133 134 135
    }
}


136
pub fn walk_crate<'v, V: Visitor<'v>>(visitor: &mut V, krate: &'v Crate) {
137
    visitor.visit_mod(&krate.module, krate.span, CRATE_NODE_ID);
138
    for attr in krate.attrs.iter() {
139
        visitor.visit_attribute(attr);
140
    }
141 142
}

143
pub fn walk_mod<'v, V: Visitor<'v>>(visitor: &mut V, module: &'v Mod) {
D
Daniel Micay 已提交
144
    for view_item in module.view_items.iter() {
145
        visitor.visit_view_item(view_item)
146
    }
147

D
Daniel Micay 已提交
148
    for item in module.items.iter() {
149
        visitor.visit_item(&**item)
150 151 152
    }
}

153
pub fn walk_view_item<'v, V: Visitor<'v>>(visitor: &mut V, vi: &'v ViewItem) {
154
    match vi.node {
155
        ViewItemExternCrate(name, _, _) => {
156
            visitor.visit_ident(vi.span, name)
157
        }
158 159 160
        ViewItemUse(ref vp) => {
            match vp.node {
                ViewPathSimple(ident, ref path, id) => {
161 162
                    visitor.visit_ident(vp.span, ident);
                    visitor.visit_path(path, id);
163 164
                }
                ViewPathGlob(ref path, id) => {
165
                    visitor.visit_path(path, id);
166 167 168
                }
                ViewPathList(ref path, ref list, _) => {
                    for id in list.iter() {
J
Jakub Wieczorek 已提交
169 170
                        match id.node {
                            PathListIdent { name, .. } => {
171
                                visitor.visit_ident(id.span, name);
J
Jakub Wieczorek 已提交
172 173 174
                            }
                            PathListMod { .. } => ()
                        }
175
                    }
176
                    walk_path(visitor, path);
177
                }
178 179 180
            }
        }
    }
181
    for attr in vi.attrs.iter() {
182
        visitor.visit_attribute(attr);
183
    }
184 185
}

186
pub fn walk_local<'v, V: Visitor<'v>>(visitor: &mut V, local: &'v Local) {
187 188
    visitor.visit_pat(&*local.pat);
    visitor.visit_ty(&*local.ty);
189
    walk_expr_opt(visitor, &local.init);
190 191
}

192 193
pub fn walk_explicit_self<'v, V: Visitor<'v>>(visitor: &mut V,
                                              explicit_self: &'v ExplicitSelf) {
194
    match explicit_self.node {
195
        SelfStatic | SelfValue(_) => {},
196
        SelfRegion(ref lifetime, _, _) => {
197
            visitor.visit_opt_lifetime_ref(explicit_self.span, lifetime)
198
        }
199
        SelfExplicit(ref typ, _) => visitor.visit_ty(&**typ),
200 201 202
    }
}

203 204
/// Like with walk_method_helper this doesn't correspond to a method
/// in Visitor, and so it gets a _helper suffix.
205 206 207
pub fn walk_trait_ref_helper<'v,V>(visitor: &mut V, trait_ref: &'v TraitRef)
                                   where V: Visitor<'v> {
    walk_lifetime_decls(visitor, &trait_ref.lifetimes);
208
    visitor.visit_path(&trait_ref.path, trait_ref.ref_id)
209 210
}

211
pub fn walk_item<'v, V: Visitor<'v>>(visitor: &mut V, item: &'v Item) {
212
    visitor.visit_ident(item.span, item.ident);
213
    match item.node {
214
        ItemStatic(ref typ, _, ref expr) => {
215 216
            visitor.visit_ty(&**typ);
            visitor.visit_expr(&**expr);
217
        }
218 219 220 221
        ItemFn(ref declaration, fn_style, abi, ref generics, ref body) => {
            visitor.visit_fn(FkItemFn(item.ident, generics, fn_style, abi),
                             &**declaration,
                             &**body,
222
                             item.span,
223
                             item.id)
224
        }
225
        ItemMod(ref module) => {
226
            visitor.visit_mod(module, item.span, item.id)
227
        }
228
        ItemForeignMod(ref foreign_module) => {
D
Daniel Micay 已提交
229
            for view_item in foreign_module.view_items.iter() {
230
                visitor.visit_view_item(view_item)
231
            }
D
Daniel Micay 已提交
232
            for foreign_item in foreign_module.items.iter() {
233
                visitor.visit_foreign_item(&**foreign_item)
234
            }
235
        }
236
        ItemTy(ref typ, ref type_parameters) => {
237 238
            visitor.visit_ty(&**typ);
            visitor.visit_generics(type_parameters)
239
        }
240
        ItemEnum(ref enum_definition, ref type_parameters) => {
241 242
            visitor.visit_generics(type_parameters);
            walk_enum_def(visitor, enum_definition, type_parameters)
243
        }
244
        ItemImpl(ref type_parameters,
245
                 ref trait_reference,
246
                 ref typ,
247
                 ref impl_items) => {
248
            visitor.visit_generics(type_parameters);
249
            match *trait_reference {
250
                Some(ref trait_reference) => walk_trait_ref_helper(visitor,
251
                                                                   trait_reference),
252
                None => ()
253
            }
254
            visitor.visit_ty(&**typ);
255 256
            for impl_item in impl_items.iter() {
                match *impl_item {
257 258
                    MethodImplItem(ref method) => {
                        walk_method_helper(visitor, &**method)
259
                    }
260 261 262 263
                    TypeImplItem(ref typedef) => {
                        visitor.visit_ident(typedef.span, typedef.ident);
                        visitor.visit_ty(&*typedef.typ);
                    }
264
                }
265 266
            }
        }
267
        ItemStruct(ref struct_definition, ref generics) => {
268
            visitor.visit_generics(generics);
269
            visitor.visit_struct_def(&**struct_definition,
270 271
                                     item.ident,
                                     generics,
272
                                     item.id)
273
        }
274
        ItemTrait(ref generics, _, ref bounds, ref methods) => {
275 276
            visitor.visit_generics(generics);
            walk_ty_param_bounds(visitor, bounds);
D
Daniel Micay 已提交
277
            for method in methods.iter() {
278
                visitor.visit_trait_item(method)
279 280
            }
        }
281
        ItemMac(ref macro) => visitor.visit_mac(macro),
282 283
    }
    for attr in item.attrs.iter() {
284
        visitor.visit_attribute(attr);
M
Marijn Haverbeke 已提交
285 286 287
    }
}

288 289 290 291 292
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);
293 294 295
    }
}

296 297 298
pub fn walk_variant<'v, V: Visitor<'v>>(visitor: &mut V,
                                        variant: &'v Variant,
                                        generics: &'v Generics) {
299
    visitor.visit_ident(variant.span, variant.node.name);
300

301
    match variant.node.kind {
302
        TupleVariantKind(ref variant_arguments) => {
303
            for variant_argument in variant_arguments.iter() {
304
                visitor.visit_ty(&*variant_argument.ty)
305 306
            }
        }
307 308
        StructVariantKind(ref struct_definition) => {
            visitor.visit_struct_def(&**struct_definition,
309 310
                                     variant.node.name,
                                     generics,
311
                                     variant.node.id)
312
        }
313
    }
314
    match variant.node.disr_expr {
315
        Some(ref expr) => visitor.visit_expr(&**expr),
316 317
        None => ()
    }
318
    for attr in variant.node.attrs.iter() {
319
        visitor.visit_attribute(attr);
320
    }
321 322
}

323
pub fn skip_ty<'v, V: Visitor<'v>>(_: &mut V, _: &'v Ty) {
324 325
    // Empty!
}
326

327
pub fn walk_ty<'v, V: Visitor<'v>>(visitor: &mut V, typ: &'v Ty) {
328
    match typ.node {
329 330
        TyUniq(ref ty) | TyVec(ref ty) | TyBox(ref ty) | TyParen(ref ty) => {
            visitor.visit_ty(&**ty)
331
        }
332
        TyPtr(ref mutable_type) => {
333
            visitor.visit_ty(&*mutable_type.ty)
334
        }
335
        TyRptr(ref lifetime, ref mutable_type) => {
336 337
            visitor.visit_opt_lifetime_ref(typ.span, lifetime);
            visitor.visit_ty(&*mutable_type.ty)
338
        }
339
        TyTup(ref tuple_element_types) => {
340 341
            for tuple_element_type in tuple_element_types.iter() {
                visitor.visit_ty(&**tuple_element_type)
342
            }
343
        }
344
        TyClosure(ref function_declaration) => {
345
            for argument in function_declaration.decl.inputs.iter() {
346
                visitor.visit_ty(&*argument.ty)
347
            }
348 349 350
            visitor.visit_ty(&*function_declaration.decl.output);
            walk_ty_param_bounds(visitor, &function_declaration.bounds);
            walk_lifetime_decls(visitor, &function_declaration.lifetimes);
351
        }
E
Eduard Burtescu 已提交
352 353
        TyProc(ref function_declaration) => {
            for argument in function_declaration.decl.inputs.iter() {
354
                visitor.visit_ty(&*argument.ty)
E
Eduard Burtescu 已提交
355
            }
356 357 358
            visitor.visit_ty(&*function_declaration.decl.output);
            walk_ty_param_bounds(visitor, &function_declaration.bounds);
            walk_lifetime_decls(visitor, &function_declaration.lifetimes);
E
Eduard Burtescu 已提交
359
        }
360
        TyBareFn(ref function_declaration) => {
D
Daniel Micay 已提交
361
            for argument in function_declaration.decl.inputs.iter() {
362
                visitor.visit_ty(&*argument.ty)
363
            }
364 365
            visitor.visit_ty(&*function_declaration.decl.output);
            walk_lifetime_decls(visitor, &function_declaration.lifetimes);
366
        }
367 368
        TyUnboxedFn(ref function_declaration) => {
            for argument in function_declaration.decl.inputs.iter() {
369
                visitor.visit_ty(&*argument.ty)
370
            }
371
            visitor.visit_ty(&*function_declaration.decl.output);
372
        }
373
        TyPath(ref path, ref opt_bounds, id) => {
374
            visitor.visit_path(path, id);
375 376
            match *opt_bounds {
                Some(ref bounds) => {
377
                    walk_ty_param_bounds(visitor, bounds);
378 379
                }
                None => { }
380
            }
381
        }
382 383 384 385 386
        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);
        }
387
        TyFixedLengthVec(ref ty, ref expression) => {
388 389
            visitor.visit_ty(&**ty);
            visitor.visit_expr(&**expression)
390
        }
391
        TyTypeof(ref expression) => {
392
            visitor.visit_expr(&**expression)
393
        }
394
        TyNil | TyBot | TyInfer => {}
M
Marijn Haverbeke 已提交
395 396 397
    }
}

398 399
fn walk_lifetime_decls<'v, V: Visitor<'v>>(visitor: &mut V,
                                           lifetimes: &'v Vec<LifetimeDef>) {
400
    for l in lifetimes.iter() {
401
        visitor.visit_lifetime_decl(l);
402 403 404
    }
}

405
pub fn walk_path<'v, V: Visitor<'v>>(visitor: &mut V, path: &'v Path) {
406
    for segment in path.segments.iter() {
407
        visitor.visit_ident(path.span, segment.identifier);
408

409
        for typ in segment.types.iter() {
410
            visitor.visit_ty(&**typ);
411 412
        }
        for lifetime in segment.lifetimes.iter() {
413
            visitor.visit_lifetime_ref(lifetime);
414
        }
415
    }
416 417
}

418
pub fn walk_pat<'v, V: Visitor<'v>>(visitor: &mut V, pattern: &'v Pat) {
419
    match pattern.node {
420
        PatEnum(ref path, ref children) => {
421
            visitor.visit_path(path, pattern.id);
D
Daniel Micay 已提交
422 423
            for children in children.iter() {
                for child in children.iter() {
424
                    visitor.visit_pat(&**child)
425
                }
426
            }
427
        }
428
        PatStruct(ref path, ref fields, _) => {
429
            visitor.visit_path(path, pattern.id);
D
Daniel Micay 已提交
430
            for field in fields.iter() {
431
                visitor.visit_pat(&*field.pat)
432 433
            }
        }
434
        PatTup(ref tuple_elements) => {
D
Daniel Micay 已提交
435
            for tuple_element in tuple_elements.iter() {
436
                visitor.visit_pat(&**tuple_element)
437
            }
438
        }
439 440
        PatBox(ref subpattern) |
        PatRegion(ref subpattern) => {
441
            visitor.visit_pat(&**subpattern)
442
        }
443
        PatIdent(_, ref pth1, ref optional_subpattern) => {
444
            visitor.visit_ident(pth1.span, pth1.node);
445 446
            match *optional_subpattern {
                None => {}
447
                Some(ref subpattern) => visitor.visit_pat(&**subpattern),
448
            }
449
        }
450
        PatLit(ref expression) => visitor.visit_expr(&**expression),
451
        PatRange(ref lower_bound, ref upper_bound) => {
452 453
            visitor.visit_expr(&**lower_bound);
            visitor.visit_expr(&**upper_bound)
454
        }
455
        PatWild(_) => (),
456
        PatVec(ref prepattern, ref slice_pattern, ref postpatterns) => {
D
Daniel Micay 已提交
457
            for prepattern in prepattern.iter() {
458
                visitor.visit_pat(&**prepattern)
459
            }
D
Daniel Micay 已提交
460
            for slice_pattern in slice_pattern.iter() {
461
                visitor.visit_pat(&**slice_pattern)
462
            }
D
Daniel Micay 已提交
463
            for postpattern in postpatterns.iter() {
464
                visitor.visit_pat(&**postpattern)
465
            }
466
        }
467
        PatMac(ref macro) => visitor.visit_mac(macro),
M
Marijn Haverbeke 已提交
468 469 470
    }
}

471 472
pub fn walk_foreign_item<'v, V: Visitor<'v>>(visitor: &mut V,
                                             foreign_item: &'v ForeignItem) {
473
    visitor.visit_ident(foreign_item.span, foreign_item.ident);
474

475
    match foreign_item.node {
476
        ForeignItemFn(ref function_declaration, ref generics) => {
477 478
            walk_fn_decl(visitor, &**function_declaration);
            visitor.visit_generics(generics)
479
        }
480
        ForeignItemStatic(ref typ, _) => visitor.visit_ty(&**typ),
481 482 483
    }

    for attr in foreign_item.attrs.iter() {
484
        visitor.visit_attribute(attr);
M
Marijn Haverbeke 已提交
485 486 487
    }
}

488 489
pub fn walk_ty_param_bounds<'v, V: Visitor<'v>>(visitor: &mut V,
                                                bounds: &'v OwnedSlice<TyParamBound>) {
D
Daniel Micay 已提交
490
    for bound in bounds.iter() {
491
        match *bound {
492
            TraitTyParamBound(ref typ) => {
493
                walk_trait_ref_helper(visitor, typ)
494
            }
495 496
            UnboxedFnTyParamBound(ref function_declaration) => {
                for argument in function_declaration.decl.inputs.iter() {
497
                    visitor.visit_ty(&*argument.ty)
498
                }
499
                visitor.visit_ty(&*function_declaration.decl.output);
500
                walk_lifetime_decls(visitor, &function_declaration.lifetimes);
501
            }
502
            RegionTyParamBound(ref lifetime) => {
503
                visitor.visit_lifetime_ref(lifetime);
504
            }
505
        }
506 507 508
    }
}

509
pub fn walk_generics<'v, V: Visitor<'v>>(visitor: &mut V, generics: &'v Generics) {
D
Daniel Micay 已提交
510
    for type_parameter in generics.ty_params.iter() {
511
        walk_ty_param_bounds(visitor, &type_parameter.bounds);
512
        match type_parameter.default {
513
            Some(ref ty) => visitor.visit_ty(&**ty),
514 515
            None => {}
        }
516
    }
517
    walk_lifetime_decls(visitor, &generics.lifetimes);
518
    for predicate in generics.where_clause.predicates.iter() {
519 520
        visitor.visit_ident(predicate.span, predicate.ident);
        walk_ty_param_bounds(visitor, &predicate.bounds);
521
    }
522 523
}

524
pub fn walk_fn_decl<'v, V: Visitor<'v>>(visitor: &mut V, function_declaration: &'v FnDecl) {
D
Daniel Micay 已提交
525
    for argument in function_declaration.inputs.iter() {
526 527
        visitor.visit_pat(&*argument.pat);
        visitor.visit_ty(&*argument.ty)
528
    }
529
    visitor.visit_ty(&*function_declaration.output)
M
Marijn Haverbeke 已提交
530 531
}

532
// Note: there is no visit_method() method in the visitor, instead override
533
// visit_fn() and check for FkMethod().  I named this visit_method_helper()
534 535
// because it is not a default impl of any method, though I doubt that really
// clarifies anything. - Niko
536
pub fn walk_method_helper<'v, V: Visitor<'v>>(visitor: &mut V, method: &'v Method) {
537
    match method.node {
538
        MethDecl(ident, ref generics, _, _, _, ref decl, ref body, _) => {
539
            visitor.visit_ident(method.span, ident);
540 541 542
            visitor.visit_fn(FkMethod(ident, generics, method),
                             &**decl,
                             &**body,
543
                             method.span,
544
                             method.id);
545
            for attr in method.attrs.iter() {
546
                visitor.visit_attribute(attr);
547 548 549
            }

        },
550
        MethMac(ref mac) => visitor.visit_mac(mac)
551
    }
552 553
}

554 555 556 557 558
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) {
559
    walk_fn_decl(visitor, function_declaration);
560

561
    match function_kind {
562
        FkItemFn(_, generics, _, _) => {
563
            visitor.visit_generics(generics);
564
        }
565
        FkMethod(_, generics, method) => {
566
            visitor.visit_generics(generics);
567
            match method.node {
568
                MethDecl(_, _, _, ref explicit_self, _, _, _, _) =>
569
                    visitor.visit_explicit_self(explicit_self),
570
                MethMac(ref mac) =>
571
                    visitor.visit_mac(mac)
572
            }
573
        }
574
        FkFnBlock(..) => {}
575 576
    }

577
    visitor.visit_block(function_body)
M
Marijn Haverbeke 已提交
578 579
}

580
pub fn walk_ty_method<'v, V: Visitor<'v>>(visitor: &mut V, method_type: &'v TypeMethod) {
581 582
    visitor.visit_ident(method_type.span, method_type.ident);
    visitor.visit_explicit_self(&method_type.explicit_self);
D
Daniel Micay 已提交
583
    for argument_type in method_type.decl.inputs.iter() {
584
        visitor.visit_ty(&*argument_type.ty)
585
    }
586 587
    visitor.visit_generics(&method_type.generics);
    visitor.visit_ty(&*method_type.decl.output);
588
    for attr in method_type.attrs.iter() {
589
        visitor.visit_attribute(attr);
590
    }
591 592
}

593
pub fn walk_trait_item<'v, V: Visitor<'v>>(visitor: &mut V, trait_method: &'v TraitItem) {
594
    match *trait_method {
595
        RequiredMethod(ref method_type) => visitor.visit_ty_method(method_type),
596
        ProvidedMethod(ref method) => walk_method_helper(visitor, &**method),
597 598 599
        TypeTraitItem(ref associated_type) => {
            visitor.visit_ident(associated_type.span, associated_type.ident)
        }
600 601 602
    }
}

603 604
pub fn walk_struct_def<'v, V: Visitor<'v>>(visitor: &mut V,
                                           struct_definition: &'v StructDef) {
605
    match struct_definition.super_struct {
606
        Some(ref t) => visitor.visit_ty(&**t),
607 608
        None => {},
    }
D
Daniel Micay 已提交
609
    for field in struct_definition.fields.iter() {
610
        visitor.visit_struct_field(field)
611
    }
612 613
}

614 615
pub fn walk_struct_field<'v, V: Visitor<'v>>(visitor: &mut V,
                                             struct_field: &'v StructField) {
616
    match struct_field.node.kind {
617
        NamedField(name, _) => {
618
            visitor.visit_ident(struct_field.span, name)
619 620 621 622
        }
        _ => {}
    }

623
    visitor.visit_ty(&*struct_field.node.ty);
624 625

    for attr in struct_field.node.attrs.iter() {
626
        visitor.visit_attribute(attr);
627
    }
628 629
}

630
pub fn walk_block<'v, V: Visitor<'v>>(visitor: &mut V, block: &'v Block) {
D
Daniel Micay 已提交
631
    for view_item in block.view_items.iter() {
632
        visitor.visit_view_item(view_item)
633
    }
D
Daniel Micay 已提交
634
    for statement in block.stmts.iter() {
635
        visitor.visit_stmt(&**statement)
636
    }
637
    walk_expr_opt(visitor, &block.expr)
M
Marijn Haverbeke 已提交
638 639
}

640
pub fn walk_stmt<'v, V: Visitor<'v>>(visitor: &mut V, statement: &'v Stmt) {
641
    match statement.node {
642
        StmtDecl(ref declaration, _) => visitor.visit_decl(&**declaration),
643
        StmtExpr(ref expression, _) | StmtSemi(ref expression, _) => {
644
            visitor.visit_expr(&**expression)
645
        }
646
        StmtMac(ref macro, _) => visitor.visit_mac(macro),
M
Marijn Haverbeke 已提交
647 648 649
    }
}

650
pub fn walk_decl<'v, V: Visitor<'v>>(visitor: &mut V, declaration: &'v Decl) {
651
    match declaration.node {
652 653
        DeclLocal(ref local) => visitor.visit_local(&**local),
        DeclItem(ref item) => visitor.visit_item(&**item),
M
Marijn Haverbeke 已提交
654 655 656
    }
}

657
pub fn walk_expr_opt<'v, V: Visitor<'v>>(visitor: &mut V,
658
                                         optional_expression: &'v Option<P<Expr>>) {
659
    match *optional_expression {
660
        None => {}
661
        Some(ref expression) => visitor.visit_expr(&**expression),
662
    }
M
Marijn Haverbeke 已提交
663 664
}

665
pub fn walk_exprs<'v, V: Visitor<'v>>(visitor: &mut V, expressions: &'v [P<Expr>]) {
D
Daniel Micay 已提交
666
    for expression in expressions.iter() {
667
        visitor.visit_expr(&**expression)
668
    }
M
Marijn Haverbeke 已提交
669 670
}

671
pub fn walk_mac<'v, V: Visitor<'v>>(_: &mut V, _: &'v Mac) {
672
    // Empty!
673 674
}

675
pub fn walk_expr<'v, V: Visitor<'v>>(visitor: &mut V, expression: &'v Expr) {
676
    match expression.node {
677
        ExprBox(ref place, ref subexpression) => {
678 679
            visitor.visit_expr(&**place);
            visitor.visit_expr(&**subexpression)
680
        }
681
        ExprVec(ref subexpressions) => {
682
            walk_exprs(visitor, subexpressions.as_slice())
683
        }
684
        ExprRepeat(ref element, ref count) => {
685 686
            visitor.visit_expr(&**element);
            visitor.visit_expr(&**count)
687
        }
688
        ExprStruct(ref path, ref fields, ref optional_base) => {
689
            visitor.visit_path(path, expression.id);
D
Daniel Micay 已提交
690
            for field in fields.iter() {
691
                visitor.visit_expr(&*field.expr)
692
            }
693
            walk_expr_opt(visitor, optional_base)
694
        }
695
        ExprTup(ref subexpressions) => {
D
Daniel Micay 已提交
696
            for subexpression in subexpressions.iter() {
697
                visitor.visit_expr(&**subexpression)
698
            }
699
        }
700
        ExprCall(ref callee_expression, ref arguments) => {
D
Daniel Micay 已提交
701
            for argument in arguments.iter() {
702
                visitor.visit_expr(&**argument)
703
            }
704
            visitor.visit_expr(&**callee_expression)
705
        }
706
        ExprMethodCall(_, ref types, ref arguments) => {
707
            walk_exprs(visitor, arguments.as_slice());
708
            for typ in types.iter() {
709
                visitor.visit_ty(&**typ)
710 711
            }
        }
712
        ExprBinary(_, ref left_expression, ref right_expression) => {
713 714
            visitor.visit_expr(&**left_expression);
            visitor.visit_expr(&**right_expression)
715
        }
716
        ExprAddrOf(_, ref subexpression) | ExprUnary(_, ref subexpression) => {
717
            visitor.visit_expr(&**subexpression)
718
        }
719
        ExprLit(_) => {}
720
        ExprCast(ref subexpression, ref typ) => {
721 722
            visitor.visit_expr(&**subexpression);
            visitor.visit_ty(&**typ)
723
        }
724
        ExprIf(ref head_expression, ref if_block, ref optional_else) => {
725 726 727
            visitor.visit_expr(&**head_expression);
            visitor.visit_block(&**if_block);
            walk_expr_opt(visitor, optional_else)
728
        }
P
Pythoner6 已提交
729
        ExprWhile(ref subexpression, ref block, _) => {
730 731
            visitor.visit_expr(&**subexpression);
            visitor.visit_block(&**block)
732
        }
733
        ExprForLoop(ref pattern, ref subexpression, ref block, _) => {
734 735 736
            visitor.visit_pat(&**pattern);
            visitor.visit_expr(&**subexpression);
            visitor.visit_block(&**block)
737
        }
738
        ExprLoop(ref block, _) => visitor.visit_block(&**block),
739
        ExprMatch(ref subexpression, ref arms) => {
740
            visitor.visit_expr(&**subexpression);
D
Daniel Micay 已提交
741
            for arm in arms.iter() {
742
                visitor.visit_arm(arm)
743
            }
744
        }
745
        ExprFnBlock(_, ref function_declaration, ref body) => {
746
            visitor.visit_fn(FkFnBlock,
747 748
                             &**function_declaration,
                             &**body,
749
                             expression.span,
750
                             expression.id)
751
        }
752
        ExprUnboxedFn(_, _, ref function_declaration, ref body) => {
753
            visitor.visit_fn(FkFnBlock,
754 755 756
                             &**function_declaration,
                             &**body,
                             expression.span,
757
                             expression.id)
758
        }
759
        ExprProc(ref function_declaration, ref body) => {
760
            visitor.visit_fn(FkFnBlock,
761 762
                             &**function_declaration,
                             &**body,
763
                             expression.span,
764
                             expression.id)
765
        }
766
        ExprBlock(ref block) => visitor.visit_block(&**block),
767
        ExprAssign(ref left_hand_expression, ref right_hand_expression) => {
768 769
            visitor.visit_expr(&**right_hand_expression);
            visitor.visit_expr(&**left_hand_expression)
770
        }
771
        ExprAssignOp(_, ref left_expression, ref right_expression) => {
772 773
            visitor.visit_expr(&**right_expression);
            visitor.visit_expr(&**left_expression)
774
        }
775
        ExprField(ref subexpression, _, ref types) => {
776
            visitor.visit_expr(&**subexpression);
777
            for typ in types.iter() {
778
                visitor.visit_ty(&**typ)
779
            }
780
        }
781
        ExprTupField(ref subexpression, _, ref types) => {
782
            visitor.visit_expr(&**subexpression);
783
            for typ in types.iter() {
784
                visitor.visit_ty(&**typ)
785 786
            }
        }
787
        ExprIndex(ref main_expression, ref index_expression) => {
788 789
            visitor.visit_expr(&**main_expression);
            visitor.visit_expr(&**index_expression)
790
        }
N
Nick Cameron 已提交
791 792 793 794 795
        ExprSlice(ref main_expression, ref start, ref end, _) => {
            visitor.visit_expr(&**main_expression);
            walk_expr_opt(visitor, start);
            walk_expr_opt(visitor, end)
        }
796
        ExprPath(ref path) => {
797
            visitor.visit_path(path, expression.id)
798
        }
799
        ExprBreak(_) | ExprAgain(_) => {}
800
        ExprRet(ref optional_expression) => {
801
            walk_expr_opt(visitor, optional_expression)
802
        }
803
        ExprMac(ref macro) => visitor.visit_mac(macro),
804
        ExprParen(ref subexpression) => {
805
            visitor.visit_expr(&**subexpression)
806
        }
807
        ExprInlineAsm(ref ia) => {
808 809
            for input in ia.inputs.iter() {
                let (_, ref input) = *input;
810
                visitor.visit_expr(&**input)
811
            }
812 813
            for output in ia.outputs.iter() {
                let (_, ref output, _) = *output;
814
                visitor.visit_expr(&**output)
815 816
            }
        }
M
Marijn Haverbeke 已提交
817
    }
818

819
    visitor.visit_expr_post(expression)
M
Marijn Haverbeke 已提交
820 821
}

822
pub fn walk_arm<'v, V: Visitor<'v>>(visitor: &mut V, arm: &'v Arm) {
D
Daniel Micay 已提交
823
    for pattern in arm.pats.iter() {
824
        visitor.visit_pat(&**pattern)
825
    }
826
    walk_expr_opt(visitor, &arm.guard);
827
    visitor.visit_expr(&*arm.body);
828
    for attr in arm.attrs.iter() {
829
        visitor.visit_attribute(attr);
830
    }
M
Marijn Haverbeke 已提交
831
}