visit.rs 34.5 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
#[derive(Copy)]
36
pub enum FnKind<'a> {
37
    /// fn foo() or extern "Abi" fn foo()
N
Niko Matsakis 已提交
38
    FkItemFn(Ident, &'a Generics, Unsafety, Abi),
39

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

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

48
/// Each method of the Visitor trait is a hook to be potentially
49
/// overridden.  Each method's default implementation recursively visits
50 51 52 53 54 55 56
/// 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.)
57
pub trait Visitor<'v> : Sized {
58 59 60 61 62
    fn visit_name(&mut self, _span: Span, _name: Name) {
        // Nothing to do.
    }
    fn visit_ident(&mut self, span: Span, ident: Ident) {
        self.visit_name(span, ident.name);
63
    }
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
    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) {
79 80
        walk_fn(self, fk, fd, b, s)
    }
81 82
    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 已提交
83
    fn visit_trait_ref(&mut self, t: &'v TraitRef) { walk_trait_ref(self, t) }
84 85 86
    fn visit_ty_param_bound(&mut self, bounds: &'v TyParamBound) {
        walk_ty_param_bound(self, bounds)
    }
N
Nick Cameron 已提交
87 88
    fn visit_poly_trait_ref(&mut self, t: &'v PolyTraitRef, m: &'v TraitBoundModifier) {
        walk_poly_trait_ref(self, t, m)
N
Niko Matsakis 已提交
89
    }
90
    fn visit_struct_def(&mut self, s: &'v StructDef, _: Ident, _: &'v Generics, _: NodeId) {
91 92
        walk_struct_def(self, s)
    }
93 94
    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) }
S
Steve Klabnik 已提交
95 96 97

    /// Visits an optional reference to a lifetime. The `span` is the span of some surrounding
    /// reference should opt_lifetime be None.
98 99
    fn visit_opt_lifetime_ref(&mut self,
                              _span: Span,
100
                              opt_lifetime: &'v Option<Lifetime>) {
101
        match *opt_lifetime {
102
            Some(ref l) => self.visit_lifetime_ref(l),
103 104 105
            None => ()
        }
    }
106 107 108
    fn visit_lifetime_bound(&mut self, lifetime: &'v Lifetime) {
        walk_lifetime_bound(self, lifetime)
    }
109
    fn visit_lifetime_ref(&mut self, lifetime: &'v Lifetime) {
110
        walk_lifetime_ref(self, lifetime)
111
    }
112 113
    fn visit_lifetime_def(&mut self, lifetime: &'v LifetimeDef) {
        walk_lifetime_def(self, lifetime)
114
    }
115
    fn visit_explicit_self(&mut self, es: &'v ExplicitSelf) {
116
        walk_explicit_self(self, es)
117
    }
118
    fn visit_mac(&mut self, _macro: &'v Mac) {
S
Steve Klabnik 已提交
119
        panic!("visit_mac disabled by default");
J
John Clements 已提交
120 121 122 123
        // NB: see note about macros above.
        // if you really want a visitor that
        // works on macros, use this
        // definition in your trait impl:
124
        // visit::walk_mac(self, _macro)
125
    }
126
    fn visit_path(&mut self, path: &'v Path, _id: ast::NodeId) {
127
        walk_path(self, path)
128
    }
129 130 131 132 133 134
    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)
    }
135 136 137
    fn visit_assoc_type_binding(&mut self, type_binding: &'v TypeBinding) {
        walk_assoc_type_binding(self, type_binding)
    }
138
    fn visit_attribute(&mut self, _attr: &'v Attribute) {}
139 140
}

141 142
pub fn walk_inlined_item<'v,V>(visitor: &mut V, item: &'v InlinedItem)
                         where V: Visitor<'v> {
143
    match *item {
144 145
        IIItem(ref i) => visitor.visit_item(&**i),
        IIForeign(ref i) => visitor.visit_foreign_item(&**i),
146
        IITraitItem(_, ref ti) => visitor.visit_trait_item(ti),
147 148 149 150 151 152 153
        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);
        }
154 155 156 157
    }
}


158
pub fn walk_crate<'v, V: Visitor<'v>>(visitor: &mut V, krate: &'v Crate) {
159
    visitor.visit_mod(&krate.module, krate.span, CRATE_NODE_ID);
160
    for attr in krate.attrs.iter() {
161
        visitor.visit_attribute(attr);
162
    }
163 164
}

165
pub fn walk_mod<'v, V: Visitor<'v>>(visitor: &mut V, module: &'v Mod) {
D
Daniel Micay 已提交
166
    for view_item in module.view_items.iter() {
167
        visitor.visit_view_item(view_item)
168
    }
169

D
Daniel Micay 已提交
170
    for item in module.items.iter() {
171
        visitor.visit_item(&**item)
172 173 174
    }
}

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

                    // Note that the `prefix` here is not a complete
                    // path, so we don't use `visit_path`.
                    walk_path(visitor, prefix);
202
                }
203 204 205
            }
        }
    }
206
    for attr in vi.attrs.iter() {
207
        visitor.visit_attribute(attr);
208
    }
209 210
}

211
pub fn walk_local<'v, V: Visitor<'v>>(visitor: &mut V, local: &'v Local) {
212
    visitor.visit_pat(&*local.pat);
S
Seo Sanghyeon 已提交
213
    walk_ty_opt(visitor, &local.ty);
214
    walk_expr_opt(visitor, &local.init);
215 216
}

217 218
pub fn walk_lifetime_def<'v, V: Visitor<'v>>(visitor: &mut V,
                                              lifetime_def: &'v LifetimeDef) {
J
Jakub Bukaj 已提交
219
    visitor.visit_name(lifetime_def.lifetime.span, lifetime_def.lifetime.name);
220
    for bound in lifetime_def.bounds.iter() {
221
        visitor.visit_lifetime_bound(bound);
222 223 224
    }
}

225 226 227 228 229 230 231 232 233 234
pub fn walk_lifetime_bound<'v, V: Visitor<'v>>(visitor: &mut V,
                                               lifetime_ref: &'v Lifetime) {
    visitor.visit_lifetime_ref(lifetime_ref)
}

pub fn walk_lifetime_ref<'v, V: Visitor<'v>>(visitor: &mut V,
                                             lifetime_ref: &'v Lifetime) {
    visitor.visit_name(lifetime_ref.span, lifetime_ref.name)
}

235 236
pub fn walk_explicit_self<'v, V: Visitor<'v>>(visitor: &mut V,
                                              explicit_self: &'v ExplicitSelf) {
237
    match explicit_self.node {
238
        SelfStatic | SelfValue(_) => {},
239
        SelfRegion(ref lifetime, _, _) => {
240
            visitor.visit_opt_lifetime_ref(explicit_self.span, lifetime)
241
        }
242
        SelfExplicit(ref typ, _) => visitor.visit_ty(&**typ),
243 244 245
    }
}

246 247
/// Like with walk_method_helper this doesn't correspond to a method
/// in Visitor, and so it gets a _helper suffix.
N
Niko Matsakis 已提交
248
pub fn walk_poly_trait_ref<'v, V>(visitor: &mut V,
N
Nick Cameron 已提交
249 250
                                  trait_ref: &'v PolyTraitRef,
                                  _modifier: &'v TraitBoundModifier)
N
Niko Matsakis 已提交
251 252
    where V: Visitor<'v>
{
253
    walk_lifetime_decls_helper(visitor, &trait_ref.bound_lifetimes);
N
Niko Matsakis 已提交
254 255 256 257 258 259 260 261 262
    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>
{
263
    visitor.visit_path(&trait_ref.path, trait_ref.ref_id)
264 265
}

266
pub fn walk_item<'v, V: Visitor<'v>>(visitor: &mut V, item: &'v Item) {
267
    visitor.visit_ident(item.span, item.ident);
268
    match item.node {
269 270
        ItemStatic(ref typ, _, ref expr) |
        ItemConst(ref typ, ref expr) => {
271 272
            visitor.visit_ty(&**typ);
            visitor.visit_expr(&**expr);
273
        }
274 275 276 277
        ItemFn(ref declaration, fn_style, abi, ref generics, ref body) => {
            visitor.visit_fn(FkItemFn(item.ident, generics, fn_style, abi),
                             &**declaration,
                             &**body,
278
                             item.span,
279
                             item.id)
280
        }
281
        ItemMod(ref module) => {
282
            visitor.visit_mod(module, item.span, item.id)
283
        }
284
        ItemForeignMod(ref foreign_module) => {
D
Daniel Micay 已提交
285
            for view_item in foreign_module.view_items.iter() {
286
                visitor.visit_view_item(view_item)
287
            }
D
Daniel Micay 已提交
288
            for foreign_item in foreign_module.items.iter() {
289
                visitor.visit_foreign_item(&**foreign_item)
290
            }
291
        }
292
        ItemTy(ref typ, ref type_parameters) => {
293 294
            visitor.visit_ty(&**typ);
            visitor.visit_generics(type_parameters)
295
        }
296
        ItemEnum(ref enum_definition, ref type_parameters) => {
297 298
            visitor.visit_generics(type_parameters);
            walk_enum_def(visitor, enum_definition, type_parameters)
299
        }
300
        ItemImpl(_, _,
301
                 ref type_parameters,
302
                 ref trait_reference,
303
                 ref typ,
304
                 ref impl_items) => {
305
            visitor.visit_generics(type_parameters);
306
            match *trait_reference {
N
Niko Matsakis 已提交
307
                Some(ref trait_reference) => visitor.visit_trait_ref(trait_reference),
308
                None => ()
309
            }
310
            visitor.visit_ty(&**typ);
311 312
            for impl_item in impl_items.iter() {
                match *impl_item {
313 314
                    MethodImplItem(ref method) => {
                        walk_method_helper(visitor, &**method)
315
                    }
316 317 318 319
                    TypeImplItem(ref typedef) => {
                        visitor.visit_ident(typedef.span, typedef.ident);
                        visitor.visit_ty(&*typedef.typ);
                    }
320
                }
321 322
            }
        }
323
        ItemStruct(ref struct_definition, ref generics) => {
324
            visitor.visit_generics(generics);
325
            visitor.visit_struct_def(&**struct_definition,
326 327
                                     item.ident,
                                     generics,
328
                                     item.id)
329
        }
N
Nick Cameron 已提交
330
        ItemTrait(_, ref generics, ref bounds, ref methods) => {
331
            visitor.visit_generics(generics);
332
            walk_ty_param_bounds_helper(visitor, bounds);
D
Daniel Micay 已提交
333
            for method in methods.iter() {
334
                visitor.visit_trait_item(method)
335 336
            }
        }
337
        ItemMac(ref macro) => visitor.visit_mac(macro),
338 339
    }
    for attr in item.attrs.iter() {
340
        visitor.visit_attribute(attr);
M
Marijn Haverbeke 已提交
341 342 343
    }
}

344 345 346 347 348
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);
349 350 351
    }
}

352 353 354
pub fn walk_variant<'v, V: Visitor<'v>>(visitor: &mut V,
                                        variant: &'v Variant,
                                        generics: &'v Generics) {
355
    visitor.visit_ident(variant.span, variant.node.name);
356

357
    match variant.node.kind {
358
        TupleVariantKind(ref variant_arguments) => {
359
            for variant_argument in variant_arguments.iter() {
360
                visitor.visit_ty(&*variant_argument.ty)
361 362
            }
        }
363 364
        StructVariantKind(ref struct_definition) => {
            visitor.visit_struct_def(&**struct_definition,
365 366
                                     variant.node.name,
                                     generics,
367
                                     variant.node.id)
368
        }
369
    }
370
    match variant.node.disr_expr {
371
        Some(ref expr) => visitor.visit_expr(&**expr),
372 373
        None => ()
    }
374
    for attr in variant.node.attrs.iter() {
375
        visitor.visit_attribute(attr);
376
    }
377 378
}

379
pub fn skip_ty<'v, V: Visitor<'v>>(_: &mut V, _: &'v Ty) {
380 381
    // Empty!
}
382

S
Seo Sanghyeon 已提交
383 384 385 386 387 388 389
pub fn walk_ty_opt<'v, V: Visitor<'v>>(visitor: &mut V, optional_type: &'v Option<P<Ty>>) {
    match *optional_type {
        Some(ref ty) => visitor.visit_ty(&**ty),
        None => ()
    }
}

390
pub fn walk_ty<'v, V: Visitor<'v>>(visitor: &mut V, typ: &'v Ty) {
391
    match typ.node {
392
        TyVec(ref ty) | TyParen(ref ty) => {
393
            visitor.visit_ty(&**ty)
394
        }
395
        TyPtr(ref mutable_type) => {
396
            visitor.visit_ty(&*mutable_type.ty)
397
        }
398
        TyRptr(ref lifetime, ref mutable_type) => {
399 400
            visitor.visit_opt_lifetime_ref(typ.span, lifetime);
            visitor.visit_ty(&*mutable_type.ty)
401
        }
402
        TyTup(ref tuple_element_types) => {
403 404
            for tuple_element_type in tuple_element_types.iter() {
                visitor.visit_ty(&**tuple_element_type)
405
            }
406
        }
407
        TyClosure(ref function_declaration) => {
408
            for argument in function_declaration.decl.inputs.iter() {
409
                visitor.visit_ty(&*argument.ty)
410
            }
411
            walk_fn_ret_ty(visitor, &function_declaration.decl.output);
412 413
            walk_ty_param_bounds_helper(visitor, &function_declaration.bounds);
            walk_lifetime_decls_helper(visitor, &function_declaration.lifetimes);
414
        }
415
        TyBareFn(ref function_declaration) => {
D
Daniel Micay 已提交
416
            for argument in function_declaration.decl.inputs.iter() {
417
                visitor.visit_ty(&*argument.ty)
418
            }
419
            walk_fn_ret_ty(visitor, &function_declaration.decl.output);
420
            walk_lifetime_decls_helper(visitor, &function_declaration.lifetimes);
421
        }
422
        TyPath(ref path, id) => {
423
            visitor.visit_path(path, id);
424 425 426 427
        }
        TyObjectSum(ref ty, ref bounds) => {
            visitor.visit_ty(&**ty);
            walk_ty_param_bounds_helper(visitor, bounds);
428
        }
429
        TyQPath(ref qpath) => {
430 431
            visitor.visit_ty(&*qpath.self_type);
            visitor.visit_trait_ref(&*qpath.trait_ref);
432 433
            visitor.visit_ident(typ.span, qpath.item_name);
        }
434
        TyFixedLengthVec(ref ty, ref expression) => {
435 436
            visitor.visit_ty(&**ty);
            visitor.visit_expr(&**expression)
437
        }
438 439
        TyPolyTraitRef(ref bounds) => {
            walk_ty_param_bounds_helper(visitor, bounds)
N
Niko Matsakis 已提交
440
        }
441
        TyTypeof(ref expression) => {
442
            visitor.visit_expr(&**expression)
443
        }
444
        TyInfer => {}
M
Marijn Haverbeke 已提交
445 446 447
    }
}

448 449
pub fn walk_lifetime_decls_helper<'v, V: Visitor<'v>>(visitor: &mut V,
                                                      lifetimes: &'v Vec<LifetimeDef>) {
450
    for l in lifetimes.iter() {
451
        visitor.visit_lifetime_def(l);
452 453 454
    }
}

455
pub fn walk_path<'v, V: Visitor<'v>>(visitor: &mut V, path: &'v Path) {
456
    for segment in path.segments.iter() {
457 458 459
        visitor.visit_path_segment(path.span, segment);
    }
}
460

461 462 463 464 465 466 467 468 469 470 471 472 473 474
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);
475
            }
476 477 478
            for lifetime in data.lifetimes.iter() {
                visitor.visit_lifetime_ref(lifetime);
            }
479 480 481
            for binding in data.bindings.iter() {
                visitor.visit_assoc_type_binding(&**binding);
            }
482 483 484 485 486 487 488
        }
        ast::ParenthesizedParameters(ref data) => {
            for typ in data.inputs.iter() {
                visitor.visit_ty(&**typ);
            }
            for typ in data.output.iter() {
                visitor.visit_ty(&**typ);
489
            }
490
        }
491
    }
492 493
}

494 495 496 497 498 499
pub fn walk_assoc_type_binding<'v, V: Visitor<'v>>(visitor: &mut V,
                                                   type_binding: &'v TypeBinding) {
    visitor.visit_ident(type_binding.span, type_binding.ident);
    visitor.visit_ty(&*type_binding.ty);
}

500
pub fn walk_pat<'v, V: Visitor<'v>>(visitor: &mut V, pattern: &'v Pat) {
501
    match pattern.node {
502
        PatEnum(ref path, ref children) => {
503
            visitor.visit_path(path, pattern.id);
D
Daniel Micay 已提交
504 505
            for children in children.iter() {
                for child in children.iter() {
506
                    visitor.visit_pat(&**child)
507
                }
508
            }
509
        }
510
        PatStruct(ref path, ref fields, _) => {
511
            visitor.visit_path(path, pattern.id);
D
Daniel Micay 已提交
512
            for field in fields.iter() {
513
                visitor.visit_pat(&*field.node.pat)
514 515
            }
        }
516
        PatTup(ref tuple_elements) => {
D
Daniel Micay 已提交
517
            for tuple_element in tuple_elements.iter() {
518
                visitor.visit_pat(&**tuple_element)
519
            }
520
        }
521 522
        PatBox(ref subpattern) |
        PatRegion(ref subpattern) => {
523
            visitor.visit_pat(&**subpattern)
524
        }
525
        PatIdent(_, ref pth1, ref optional_subpattern) => {
526
            visitor.visit_ident(pth1.span, pth1.node);
527 528
            match *optional_subpattern {
                None => {}
529
                Some(ref subpattern) => visitor.visit_pat(&**subpattern),
530
            }
531
        }
532
        PatLit(ref expression) => visitor.visit_expr(&**expression),
533
        PatRange(ref lower_bound, ref upper_bound) => {
534 535
            visitor.visit_expr(&**lower_bound);
            visitor.visit_expr(&**upper_bound)
536
        }
537
        PatWild(_) => (),
538
        PatVec(ref prepattern, ref slice_pattern, ref postpatterns) => {
D
Daniel Micay 已提交
539
            for prepattern in prepattern.iter() {
540
                visitor.visit_pat(&**prepattern)
541
            }
D
Daniel Micay 已提交
542
            for slice_pattern in slice_pattern.iter() {
543
                visitor.visit_pat(&**slice_pattern)
544
            }
D
Daniel Micay 已提交
545
            for postpattern in postpatterns.iter() {
546
                visitor.visit_pat(&**postpattern)
547
            }
548
        }
549
        PatMac(ref macro) => visitor.visit_mac(macro),
M
Marijn Haverbeke 已提交
550 551 552
    }
}

553 554
pub fn walk_foreign_item<'v, V: Visitor<'v>>(visitor: &mut V,
                                             foreign_item: &'v ForeignItem) {
555
    visitor.visit_ident(foreign_item.span, foreign_item.ident);
556

557
    match foreign_item.node {
558
        ForeignItemFn(ref function_declaration, ref generics) => {
559 560
            walk_fn_decl(visitor, &**function_declaration);
            visitor.visit_generics(generics)
561
        }
562
        ForeignItemStatic(ref typ, _) => visitor.visit_ty(&**typ),
563 564 565
    }

    for attr in foreign_item.attrs.iter() {
566
        visitor.visit_attribute(attr);
M
Marijn Haverbeke 已提交
567 568 569
    }
}

570 571
pub fn walk_ty_param_bounds_helper<'v, V: Visitor<'v>>(visitor: &mut V,
                                                       bounds: &'v OwnedSlice<TyParamBound>) {
D
Daniel Micay 已提交
572
    for bound in bounds.iter() {
573 574 575 576 577 578 579
        visitor.visit_ty_param_bound(bound)
    }
}

pub fn walk_ty_param_bound<'v, V: Visitor<'v>>(visitor: &mut V,
                                               bound: &'v TyParamBound) {
    match *bound {
N
Nick Cameron 已提交
580 581
        TraitTyParamBound(ref typ, ref modifier) => {
            visitor.visit_poly_trait_ref(typ, modifier);
582 583
        }
        RegionTyParamBound(ref lifetime) => {
584
            visitor.visit_lifetime_bound(lifetime);
585
        }
586 587 588
    }
}

589 590 591
pub fn walk_ty_param<'v, V: Visitor<'v>>(visitor: &mut V, param: &'v TyParam) {
    visitor.visit_ident(param.span, param.ident);
    walk_ty_param_bounds_helper(visitor, &param.bounds);
S
Seo Sanghyeon 已提交
592
    walk_ty_opt(visitor, &param.default);
593 594
}

595
pub fn walk_generics<'v, V: Visitor<'v>>(visitor: &mut V, generics: &'v Generics) {
D
Daniel Micay 已提交
596
    for type_parameter in generics.ty_params.iter() {
597
        walk_ty_param(visitor, type_parameter);
598
    }
599
    walk_lifetime_decls_helper(visitor, &generics.lifetimes);
600
    for predicate in generics.where_clause.predicates.iter() {
601
        match predicate {
602
            &ast::WherePredicate::BoundPredicate(ast::WhereBoundPredicate{ref bounded_ty,
603 604
                                                                          ref bounds,
                                                                          ..}) => {
605
                visitor.visit_ty(&**bounded_ty);
606 607
                walk_ty_param_bounds_helper(visitor, bounds);
            }
608
            &ast::WherePredicate::RegionPredicate(ast::WhereRegionPredicate{ref lifetime,
609
                                                                            ref bounds,
610 611
                                                                            ..}) => {
                visitor.visit_lifetime_ref(lifetime);
612 613 614 615

                for bound in bounds.iter() {
                    visitor.visit_lifetime_ref(bound);
                }
616
            }
617 618 619 620 621 622 623 624
            &ast::WherePredicate::EqPredicate(ast::WhereEqPredicate{id,
                                                                    ref path,
                                                                    ref ty,
                                                                    ..}) => {
                visitor.visit_path(path, id);
                visitor.visit_ty(&**ty);
            }
        }
625
    }
626 627
}

628 629 630 631 632 633
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)
    }
}

634
pub fn walk_fn_decl<'v, V: Visitor<'v>>(visitor: &mut V, function_declaration: &'v FnDecl) {
D
Daniel Micay 已提交
635
    for argument in function_declaration.inputs.iter() {
636 637
        visitor.visit_pat(&*argument.pat);
        visitor.visit_ty(&*argument.ty)
638
    }
639
    walk_fn_ret_ty(visitor, &function_declaration.output)
M
Marijn Haverbeke 已提交
640 641
}

642
// Note: there is no visit_method() method in the visitor, instead override
643
// visit_fn() and check for FkMethod().  I named this visit_method_helper()
644 645
// because it is not a default impl of any method, though I doubt that really
// clarifies anything. - Niko
646
pub fn walk_method_helper<'v, V: Visitor<'v>>(visitor: &mut V, method: &'v Method) {
647
    match method.node {
648
        MethDecl(ident, ref generics, _, _, _, ref decl, ref body, _) => {
649
            visitor.visit_ident(method.span, ident);
650 651 652
            visitor.visit_fn(FkMethod(ident, generics, method),
                             &**decl,
                             &**body,
653
                             method.span,
654
                             method.id);
655
            for attr in method.attrs.iter() {
656
                visitor.visit_attribute(attr);
657 658 659
            }

        },
660
        MethMac(ref mac) => visitor.visit_mac(mac)
661
    }
662 663
}

664 665 666 667 668
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) {
669
    walk_fn_decl(visitor, function_declaration);
670

671
    match function_kind {
672
        FkItemFn(_, generics, _, _) => {
673
            visitor.visit_generics(generics);
674
        }
675
        FkMethod(_, generics, method) => {
676
            visitor.visit_generics(generics);
677
            match method.node {
678
                MethDecl(_, _, _, ref explicit_self, _, _, _, _) =>
679
                    visitor.visit_explicit_self(explicit_self),
680
                MethMac(ref mac) =>
681
                    visitor.visit_mac(mac)
682
            }
683
        }
684
        FkFnBlock(..) => {}
685 686
    }

687
    visitor.visit_block(function_body)
M
Marijn Haverbeke 已提交
688 689
}

690
pub fn walk_ty_method<'v, V: Visitor<'v>>(visitor: &mut V, method_type: &'v TypeMethod) {
691 692
    visitor.visit_ident(method_type.span, method_type.ident);
    visitor.visit_explicit_self(&method_type.explicit_self);
D
Daniel Micay 已提交
693
    for argument_type in method_type.decl.inputs.iter() {
694
        visitor.visit_ty(&*argument_type.ty)
695
    }
696
    visitor.visit_generics(&method_type.generics);
697
    walk_fn_ret_ty(visitor, &method_type.decl.output);
698
    for attr in method_type.attrs.iter() {
699
        visitor.visit_attribute(attr);
700
    }
701 702
}

703
pub fn walk_trait_item<'v, V: Visitor<'v>>(visitor: &mut V, trait_method: &'v TraitItem) {
704
    match *trait_method {
705
        RequiredMethod(ref method_type) => visitor.visit_ty_method(method_type),
706
        ProvidedMethod(ref method) => walk_method_helper(visitor, &**method),
707
        TypeTraitItem(ref associated_type) => {
708
            walk_ty_param(visitor, &associated_type.ty_param);
709
        }
710 711 712
    }
}

713 714
pub fn walk_struct_def<'v, V: Visitor<'v>>(visitor: &mut V,
                                           struct_definition: &'v StructDef) {
D
Daniel Micay 已提交
715
    for field in struct_definition.fields.iter() {
716
        visitor.visit_struct_field(field)
717
    }
718 719
}

720 721
pub fn walk_struct_field<'v, V: Visitor<'v>>(visitor: &mut V,
                                             struct_field: &'v StructField) {
722 723
    if let NamedField(name, _) = struct_field.node.kind {
        visitor.visit_ident(struct_field.span, name);
724 725
    }

726
    visitor.visit_ty(&*struct_field.node.ty);
727 728

    for attr in struct_field.node.attrs.iter() {
729
        visitor.visit_attribute(attr);
730
    }
731 732
}

733
pub fn walk_block<'v, V: Visitor<'v>>(visitor: &mut V, block: &'v Block) {
D
Daniel Micay 已提交
734
    for view_item in block.view_items.iter() {
735
        visitor.visit_view_item(view_item)
736
    }
D
Daniel Micay 已提交
737
    for statement in block.stmts.iter() {
738
        visitor.visit_stmt(&**statement)
739
    }
740
    walk_expr_opt(visitor, &block.expr)
M
Marijn Haverbeke 已提交
741 742
}

743
pub fn walk_stmt<'v, V: Visitor<'v>>(visitor: &mut V, statement: &'v Stmt) {
744
    match statement.node {
745
        StmtDecl(ref declaration, _) => visitor.visit_decl(&**declaration),
746
        StmtExpr(ref expression, _) | StmtSemi(ref expression, _) => {
747
            visitor.visit_expr(&**expression)
748
        }
749
        StmtMac(ref macro, _) => visitor.visit_mac(&**macro),
M
Marijn Haverbeke 已提交
750 751 752
    }
}

753
pub fn walk_decl<'v, V: Visitor<'v>>(visitor: &mut V, declaration: &'v Decl) {
754
    match declaration.node {
755 756
        DeclLocal(ref local) => visitor.visit_local(&**local),
        DeclItem(ref item) => visitor.visit_item(&**item),
M
Marijn Haverbeke 已提交
757 758 759
    }
}

760
pub fn walk_expr_opt<'v, V: Visitor<'v>>(visitor: &mut V,
761
                                         optional_expression: &'v Option<P<Expr>>) {
762
    match *optional_expression {
763
        None => {}
764
        Some(ref expression) => visitor.visit_expr(&**expression),
765
    }
M
Marijn Haverbeke 已提交
766 767
}

768
pub fn walk_exprs<'v, V: Visitor<'v>>(visitor: &mut V, expressions: &'v [P<Expr>]) {
D
Daniel Micay 已提交
769
    for expression in expressions.iter() {
770
        visitor.visit_expr(&**expression)
771
    }
M
Marijn Haverbeke 已提交
772 773
}

774
pub fn walk_mac<'v, V: Visitor<'v>>(_: &mut V, _: &'v Mac) {
775
    // Empty!
776 777
}

778
pub fn walk_expr<'v, V: Visitor<'v>>(visitor: &mut V, expression: &'v Expr) {
779
    match expression.node {
780
        ExprBox(ref place, ref subexpression) => {
781
            place.as_ref().map(|e|visitor.visit_expr(&**e));
782
            visitor.visit_expr(&**subexpression)
783
        }
784
        ExprVec(ref subexpressions) => {
785
            walk_exprs(visitor, subexpressions.as_slice())
786
        }
787
        ExprRepeat(ref element, ref count) => {
788 789
            visitor.visit_expr(&**element);
            visitor.visit_expr(&**count)
790
        }
791
        ExprStruct(ref path, ref fields, ref optional_base) => {
792
            visitor.visit_path(path, expression.id);
D
Daniel Micay 已提交
793
            for field in fields.iter() {
794
                visitor.visit_expr(&*field.expr)
795
            }
796
            walk_expr_opt(visitor, optional_base)
797
        }
798
        ExprTup(ref subexpressions) => {
D
Daniel Micay 已提交
799
            for subexpression in subexpressions.iter() {
800
                visitor.visit_expr(&**subexpression)
801
            }
802
        }
803
        ExprCall(ref callee_expression, ref arguments) => {
D
Daniel Micay 已提交
804
            for argument in arguments.iter() {
805
                visitor.visit_expr(&**argument)
806
            }
807
            visitor.visit_expr(&**callee_expression)
808
        }
809
        ExprMethodCall(_, ref types, ref arguments) => {
810
            walk_exprs(visitor, arguments.as_slice());
811
            for typ in types.iter() {
812
                visitor.visit_ty(&**typ)
813 814
            }
        }
815
        ExprBinary(_, ref left_expression, ref right_expression) => {
816 817
            visitor.visit_expr(&**left_expression);
            visitor.visit_expr(&**right_expression)
818
        }
819
        ExprAddrOf(_, ref subexpression) | ExprUnary(_, ref subexpression) => {
820
            visitor.visit_expr(&**subexpression)
821
        }
822
        ExprLit(_) => {}
823
        ExprCast(ref subexpression, ref typ) => {
824 825
            visitor.visit_expr(&**subexpression);
            visitor.visit_ty(&**typ)
826
        }
827
        ExprIf(ref head_expression, ref if_block, ref optional_else) => {
828 829 830
            visitor.visit_expr(&**head_expression);
            visitor.visit_block(&**if_block);
            walk_expr_opt(visitor, optional_else)
831
        }
P
Pythoner6 已提交
832
        ExprWhile(ref subexpression, ref block, _) => {
833 834
            visitor.visit_expr(&**subexpression);
            visitor.visit_block(&**block)
835
        }
K
Kevin Ballard 已提交
836 837 838 839 840 841
        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 已提交
842 843 844 845 846
        ExprWhileLet(ref pattern, ref subexpression, ref block, _) => {
            visitor.visit_pat(&**pattern);
            visitor.visit_expr(&**subexpression);
            visitor.visit_block(&**block);
        }
847
        ExprForLoop(ref pattern, ref subexpression, ref block, _) => {
848 849 850
            visitor.visit_pat(&**pattern);
            visitor.visit_expr(&**subexpression);
            visitor.visit_block(&**block)
851
        }
852
        ExprLoop(ref block, _) => visitor.visit_block(&**block),
K
Kevin Ballard 已提交
853
        ExprMatch(ref subexpression, ref arms, _) => {
854
            visitor.visit_expr(&**subexpression);
D
Daniel Micay 已提交
855
            for arm in arms.iter() {
856
                visitor.visit_arm(arm)
857
            }
858
        }
859
        ExprClosure(_, _, ref function_declaration, ref body) => {
860
            visitor.visit_fn(FkFnBlock,
861 862 863
                             &**function_declaration,
                             &**body,
                             expression.span,
864
                             expression.id)
865
        }
866
        ExprBlock(ref block) => visitor.visit_block(&**block),
867
        ExprAssign(ref left_hand_expression, ref right_hand_expression) => {
868 869
            visitor.visit_expr(&**right_hand_expression);
            visitor.visit_expr(&**left_hand_expression)
870
        }
871
        ExprAssignOp(_, ref left_expression, ref right_expression) => {
872 873
            visitor.visit_expr(&**right_expression);
            visitor.visit_expr(&**left_expression)
874
        }
875
        ExprField(ref subexpression, _) => {
876
            visitor.visit_expr(&**subexpression);
877
        }
878
        ExprTupField(ref subexpression, _) => {
879
            visitor.visit_expr(&**subexpression);
880
        }
881
        ExprIndex(ref main_expression, ref index_expression) => {
882 883
            visitor.visit_expr(&**main_expression);
            visitor.visit_expr(&**index_expression)
884
        }
N
Nick Cameron 已提交
885
        ExprRange(ref start, ref end) => {
886
            walk_expr_opt(visitor, start);
N
Nick Cameron 已提交
887 888
            walk_expr_opt(visitor, end)
        }
889
        ExprPath(ref path) => {
890
            visitor.visit_path(path, expression.id)
891
        }
892
        ExprBreak(_) | ExprAgain(_) => {}
893
        ExprRet(ref optional_expression) => {
894
            walk_expr_opt(visitor, optional_expression)
895
        }
896
        ExprMac(ref macro) => visitor.visit_mac(macro),
897
        ExprParen(ref subexpression) => {
898
            visitor.visit_expr(&**subexpression)
899
        }
900
        ExprInlineAsm(ref ia) => {
901 902
            for input in ia.inputs.iter() {
                let (_, ref input) = *input;
903
                visitor.visit_expr(&**input)
904
            }
905 906
            for output in ia.outputs.iter() {
                let (_, ref output, _) = *output;
907
                visitor.visit_expr(&**output)
908 909
            }
        }
M
Marijn Haverbeke 已提交
910
    }
911

912
    visitor.visit_expr_post(expression)
M
Marijn Haverbeke 已提交
913 914
}

915
pub fn walk_arm<'v, V: Visitor<'v>>(visitor: &mut V, arm: &'v Arm) {
D
Daniel Micay 已提交
916
    for pattern in arm.pats.iter() {
917
        visitor.visit_pat(&**pattern)
918
    }
919
    walk_expr_opt(visitor, &arm.guard);
920
    visitor.visit_expr(&*arm.body);
921
    for attr in arm.attrs.iter() {
922
        visitor.visit_attribute(attr);
923
    }
M
Marijn Haverbeke 已提交
924
}