visit.rs 32.1 KB
Newer Older
1
// Copyright 2012-2015 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 attr::ThinAttributesExt;
29
use codemap::Span;
30

31
#[derive(Copy, Clone, PartialEq, Eq)]
32
pub enum FnKind<'a> {
33
    /// fn foo() or extern "Abi" fn foo()
34
    ItemFn(Ident, &'a Generics, Unsafety, Constness, Abi, Visibility),
35

36
    /// fn foo(&self)
37
    Method(Ident, &'a MethodSig, Option<Visibility>),
38

M
Manish Goregaokar 已提交
39
    /// |x, y| {}
40
    Closure,
41 42
}

43
/// Each method of the Visitor trait is a hook to be potentially
44
/// overridden.  Each method's default implementation recursively visits
45 46 47 48 49 50 51
/// 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.)
52
pub trait Visitor<'v> : Sized {
53 54 55 56
    fn visit_name(&mut self, _span: Span, _name: Name) {
        // Nothing to do.
    }
    fn visit_ident(&mut self, span: Span, ident: Ident) {
57
        walk_ident(self, span, ident);
58
    }
59 60 61 62 63 64 65 66 67 68 69 70 71 72
    fn visit_mod(&mut self, m: &'v Mod, _s: Span, _n: NodeId) { walk_mod(self, m) }
    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) {
73 74
        walk_fn(self, fk, fd, b, s)
    }
75 76
    fn visit_trait_item(&mut self, ti: &'v TraitItem) { walk_trait_item(self, ti) }
    fn visit_impl_item(&mut self, ii: &'v ImplItem) { walk_impl_item(self, ii) }
N
Niko Matsakis 已提交
77
    fn visit_trait_ref(&mut self, t: &'v TraitRef) { walk_trait_ref(self, t) }
78 79 80
    fn visit_ty_param_bound(&mut self, bounds: &'v TyParamBound) {
        walk_ty_param_bound(self, bounds)
    }
N
Nick Cameron 已提交
81 82
    fn visit_poly_trait_ref(&mut self, t: &'v PolyTraitRef, m: &'v TraitBoundModifier) {
        walk_poly_trait_ref(self, t, m)
N
Niko Matsakis 已提交
83
    }
84
    fn visit_variant_data(&mut self, s: &'v VariantData, _: Ident,
85
                        _: &'v Generics, _: NodeId, _: Span) {
86 87
        walk_struct_def(self, s)
    }
88
    fn visit_struct_field(&mut self, s: &'v StructField) { walk_struct_field(self, s) }
89
    fn visit_enum_def(&mut self, enum_definition: &'v EnumDef,
90
                      generics: &'v Generics, item_id: NodeId, _: Span) {
91 92 93 94
        walk_enum_def(self, enum_definition, generics, item_id)
    }
    fn visit_variant(&mut self, v: &'v Variant, g: &'v Generics, item_id: NodeId) {
        walk_variant(self, v, g, item_id)
95
    }
96 97
    fn visit_lifetime(&mut self, lifetime: &'v Lifetime) {
        walk_lifetime(self, lifetime)
98
    }
99 100
    fn visit_lifetime_def(&mut self, lifetime: &'v LifetimeDef) {
        walk_lifetime_def(self, lifetime)
101
    }
102
    fn visit_explicit_self(&mut self, es: &'v ExplicitSelf) {
103
        walk_explicit_self(self, es)
104
    }
K
Keegan McAllister 已提交
105
    fn visit_mac(&mut self, _mac: &'v Mac) {
S
Steve Klabnik 已提交
106
        panic!("visit_mac disabled by default");
J
John Clements 已提交
107 108 109 110
        // NB: see note about macros above.
        // if you really want a visitor that
        // works on macros, use this
        // definition in your trait impl:
K
Keegan McAllister 已提交
111
        // visit::walk_mac(self, _mac)
112
    }
113
    fn visit_path(&mut self, path: &'v Path, _id: NodeId) {
114
        walk_path(self, path)
115
    }
116 117 118
    fn visit_path_list_item(&mut self, prefix: &'v Path, item: &'v PathListItem) {
        walk_path_list_item(self, prefix, item)
    }
119 120 121 122 123 124
    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)
    }
125 126 127
    fn visit_assoc_type_binding(&mut self, type_binding: &'v TypeBinding) {
        walk_assoc_type_binding(self, type_binding)
    }
128
    fn visit_attribute(&mut self, _attr: &'v Attribute) {}
129 130 131
    fn visit_macro_def(&mut self, macro_def: &'v MacroDef) {
        walk_macro_def(self, macro_def)
    }
132 133
}

134 135
#[macro_export]
macro_rules! walk_list {
136
    ($visitor: expr, $method: ident, $list: expr) => {
137 138 139
        for elem in $list {
            $visitor.$method(elem)
        }
140 141 142 143 144
    };
    ($visitor: expr, $method: ident, $list: expr, $($extra_args: expr),*) => {
        for elem in $list {
            $visitor.$method(elem, $($extra_args,)*)
        }
145
    }
146 147
}

148 149 150
pub fn walk_opt_name<'v, V: Visitor<'v>>(visitor: &mut V, span: Span, opt_name: Option<Name>) {
    for name in opt_name {
        visitor.visit_name(span, name);
151 152 153
    }
}

154 155 156 157
pub fn walk_opt_ident<'v, V: Visitor<'v>>(visitor: &mut V, span: Span, opt_ident: Option<Ident>) {
    for ident in opt_ident {
        visitor.visit_ident(span, ident);
    }
158 159
}

160 161
pub fn walk_ident<'v, V: Visitor<'v>>(visitor: &mut V, span: Span, ident: Ident) {
    visitor.visit_name(span, ident.name);
162 163
}

164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
pub fn walk_crate<'v, V: Visitor<'v>>(visitor: &mut V, krate: &'v Crate) {
    visitor.visit_mod(&krate.module, krate.span, CRATE_NODE_ID);
    walk_list!(visitor, visit_attribute, &krate.attrs);
    walk_list!(visitor, visit_macro_def, &krate.exported_macros);
}

pub fn walk_macro_def<'v, V: Visitor<'v>>(visitor: &mut V, macro_def: &'v MacroDef) {
    visitor.visit_ident(macro_def.span, macro_def.ident);
    walk_opt_ident(visitor, macro_def.span, macro_def.imported_from);
    walk_list!(visitor, visit_attribute, &macro_def.attrs);
}

pub fn walk_mod<'v, V: Visitor<'v>>(visitor: &mut V, module: &'v Mod) {
    walk_list!(visitor, visit_item, &module.items);
}

pub fn walk_local<'v, V: Visitor<'v>>(visitor: &mut V, local: &'v Local) {
    visitor.visit_pat(&local.pat);
    walk_list!(visitor, visit_ty, &local.ty);
    walk_list!(visitor, visit_expr, &local.init);
184 185
}

186 187 188 189 190 191 192 193
pub fn walk_lifetime<'v, V: Visitor<'v>>(visitor: &mut V, lifetime: &'v Lifetime) {
    visitor.visit_name(lifetime.span, lifetime.name);
}

pub fn walk_lifetime_def<'v, V: Visitor<'v>>(visitor: &mut V,
                                              lifetime_def: &'v LifetimeDef) {
    visitor.visit_lifetime(&lifetime_def.lifetime);
    walk_list!(visitor, visit_lifetime, &lifetime_def.bounds);
194 195
}

196 197
pub fn walk_explicit_self<'v, V: Visitor<'v>>(visitor: &mut V,
                                              explicit_self: &'v ExplicitSelf) {
198
    match explicit_self.node {
199 200
        SelfKind::Static => {},
        SelfKind::Value(ident) => {
201 202
            visitor.visit_ident(explicit_self.span, ident)
        }
203
        SelfKind::Region(ref opt_lifetime, _, ident) => {
204 205 206
            visitor.visit_ident(explicit_self.span, ident);
            walk_list!(visitor, visit_lifetime, opt_lifetime);
        }
207
        SelfKind::Explicit(ref typ, ident) => {
208 209
            visitor.visit_ident(explicit_self.span, ident);
            visitor.visit_ty(typ)
210 211 212 213
        }
    }
}

N
Niko Matsakis 已提交
214
pub fn walk_poly_trait_ref<'v, V>(visitor: &mut V,
N
Nick Cameron 已提交
215 216
                                  trait_ref: &'v PolyTraitRef,
                                  _modifier: &'v TraitBoundModifier)
N
Niko Matsakis 已提交
217 218
    where V: Visitor<'v>
{
219
    walk_list!(visitor, visit_lifetime_def, &trait_ref.bound_lifetimes);
N
Niko Matsakis 已提交
220 221 222 223 224 225 226
    visitor.visit_trait_ref(&trait_ref.trait_ref);
}

pub fn walk_trait_ref<'v,V>(visitor: &mut V,
                                   trait_ref: &'v TraitRef)
    where V: Visitor<'v>
{
227
    visitor.visit_path(&trait_ref.path, trait_ref.ref_id)
228 229
}

230
pub fn walk_item<'v, V: Visitor<'v>>(visitor: &mut V, item: &'v Item) {
231
    visitor.visit_ident(item.span, item.ident);
232
    match item.node {
233
        ItemKind::ExternCrate(opt_name) => {
234 235
            walk_opt_name(visitor, item.span, opt_name)
        }
236
        ItemKind::Use(ref vp) => {
237
            match vp.node {
238 239
                ViewPathSimple(ident, ref path) => {
                    visitor.visit_ident(vp.span, ident);
240 241 242 243 244 245
                    visitor.visit_path(path, item.id);
                }
                ViewPathGlob(ref path) => {
                    visitor.visit_path(path, item.id);
                }
                ViewPathList(ref prefix, ref list) => {
246 247 248
                    if !list.is_empty() {
                        for item in list {
                            visitor.visit_path_list_item(prefix, item)
249
                        }
250
                    } else {
251
                        visitor.visit_path(prefix, item.id);
252 253 254 255
                    }
                }
            }
        }
256 257
        ItemKind::Static(ref typ, _, ref expr) |
        ItemKind::Const(ref typ, ref expr) => {
258 259
            visitor.visit_ty(typ);
            visitor.visit_expr(expr);
260
        }
261
        ItemKind::Fn(ref declaration, unsafety, constness, abi, ref generics, ref body) => {
262 263
            visitor.visit_fn(FnKind::ItemFn(item.ident, generics, unsafety,
                                            constness, abi, item.vis),
264 265
                             declaration,
                             body,
266
                             item.span,
267
                             item.id)
268
        }
269
        ItemKind::Mod(ref module) => {
270
            visitor.visit_mod(module, item.span, item.id)
271
        }
272
        ItemKind::ForeignMod(ref foreign_module) => {
273
            walk_list!(visitor, visit_foreign_item, &foreign_module.items);
274
        }
275
        ItemKind::Ty(ref typ, ref type_parameters) => {
276
            visitor.visit_ty(typ);
277
            visitor.visit_generics(type_parameters)
278
        }
279
        ItemKind::Enum(ref enum_definition, ref type_parameters) => {
280
            visitor.visit_generics(type_parameters);
281
            visitor.visit_enum_def(enum_definition, type_parameters, item.id, item.span)
282
        }
283
        ItemKind::DefaultImpl(_, ref trait_ref) => {
284 285
            visitor.visit_trait_ref(trait_ref)
        }
286
        ItemKind::Impl(_, _,
287
                 ref type_parameters,
288
                 ref opt_trait_reference,
289
                 ref typ,
290
                 ref impl_items) => {
291
            visitor.visit_generics(type_parameters);
292 293 294
            walk_list!(visitor, visit_trait_ref, opt_trait_reference);
            visitor.visit_ty(typ);
            walk_list!(visitor, visit_impl_item, impl_items);
295
        }
296
        ItemKind::Struct(ref struct_definition, ref generics) => {
297
            visitor.visit_generics(generics);
298
            visitor.visit_variant_data(struct_definition, item.ident,
299
                                     generics, item.id, item.span);
300
        }
301
        ItemKind::Trait(_, ref generics, ref bounds, ref methods) => {
302
            visitor.visit_generics(generics);
303 304
            walk_list!(visitor, visit_ty_param_bound, bounds);
            walk_list!(visitor, visit_trait_item, methods);
305
        }
306
        ItemKind::Mac(ref mac) => visitor.visit_mac(mac),
307
    }
308
    walk_list!(visitor, visit_attribute, &item.attrs);
M
Marijn Haverbeke 已提交
309 310
}

311 312
pub fn walk_enum_def<'v, V: Visitor<'v>>(visitor: &mut V,
                                         enum_definition: &'v EnumDef,
313 314
                                         generics: &'v Generics,
                                         item_id: NodeId) {
315
    walk_list!(visitor, visit_variant, &enum_definition.variants, generics, item_id);
316 317
}

318 319
pub fn walk_variant<'v, V: Visitor<'v>>(visitor: &mut V,
                                        variant: &'v Variant,
320 321
                                        generics: &'v Generics,
                                        item_id: NodeId) {
322
    visitor.visit_ident(variant.span, variant.node.name);
323
    visitor.visit_variant_data(&variant.node.data, variant.node.name,
324
                             generics, item_id, variant.span);
325 326
    walk_list!(visitor, visit_expr, &variant.node.disr_expr);
    walk_list!(visitor, visit_attribute, &variant.node.attrs);
S
Seo Sanghyeon 已提交
327 328
}

329
pub fn walk_ty<'v, V: Visitor<'v>>(visitor: &mut V, typ: &'v Ty) {
330
    match typ.node {
331
        TyKind::Vec(ref ty) | TyKind::Paren(ref ty) => {
332
            visitor.visit_ty(ty)
333
        }
334
        TyKind::Ptr(ref mutable_type) => {
335
            visitor.visit_ty(&mutable_type.ty)
336
        }
337
        TyKind::Rptr(ref opt_lifetime, ref mutable_type) => {
338 339
            walk_list!(visitor, visit_lifetime, opt_lifetime);
            visitor.visit_ty(&mutable_type.ty)
340
        }
341
        TyKind::Tup(ref tuple_element_types) => {
342
            walk_list!(visitor, visit_ty, tuple_element_types);
343
        }
344
        TyKind::BareFn(ref function_declaration) => {
345 346
            walk_fn_decl(visitor, &function_declaration.decl);
            walk_list!(visitor, visit_lifetime_def, &function_declaration.lifetimes);
347
        }
348
        TyKind::Path(ref maybe_qself, ref path) => {
349
            if let Some(ref qself) = *maybe_qself {
350 351
                visitor.visit_ty(&qself.ty);
            }
352
            visitor.visit_path(path, typ.id);
353
        }
354
        TyKind::ObjectSum(ref ty, ref bounds) => {
355 356
            visitor.visit_ty(ty);
            walk_list!(visitor, visit_ty_param_bound, bounds);
357
        }
358
        TyKind::FixedLengthVec(ref ty, ref expression) => {
359 360
            visitor.visit_ty(ty);
            visitor.visit_expr(expression)
361
        }
362
        TyKind::PolyTraitRef(ref bounds) => {
363
            walk_list!(visitor, visit_ty_param_bound, bounds);
N
Niko Matsakis 已提交
364
        }
365
        TyKind::Typeof(ref expression) => {
366
            visitor.visit_expr(expression)
367
        }
368 369
        TyKind::Infer => {}
        TyKind::Mac(ref mac) => {
370 371
            visitor.visit_mac(mac)
        }
M
Marijn Haverbeke 已提交
372 373 374
    }
}

375
pub fn walk_path<'v, V: Visitor<'v>>(visitor: &mut V, path: &'v Path) {
376
    for segment in &path.segments {
377 378 379
        visitor.visit_path_segment(path.span, segment);
    }
}
380

381 382 383 384 385 386
pub fn walk_path_list_item<'v, V: Visitor<'v>>(visitor: &mut V, prefix: &'v Path,
                                               item: &'v PathListItem) {
    for segment in &prefix.segments {
        visitor.visit_path_segment(prefix.span, segment);
    }

387 388
    walk_opt_ident(visitor, item.span, item.node.name());
    walk_opt_ident(visitor, item.span, item.node.rename());
389 390
}

391 392 393 394 395 396 397 398 399 400 401
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 {
402
        PathParameters::AngleBracketed(ref data) => {
403 404 405
            walk_list!(visitor, visit_ty, &data.types);
            walk_list!(visitor, visit_lifetime, &data.lifetimes);
            walk_list!(visitor, visit_assoc_type_binding, &data.bindings);
406
        }
407
        PathParameters::Parenthesized(ref data) => {
408 409
            walk_list!(visitor, visit_ty, &data.inputs);
            walk_list!(visitor, visit_ty, &data.output);
410
        }
411
    }
412 413
}

414 415 416
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);
417
    visitor.visit_ty(&type_binding.ty);
418 419
}

420
pub fn walk_pat<'v, V: Visitor<'v>>(visitor: &mut V, pattern: &'v Pat) {
421
    match pattern.node {
422
        PatKind::TupleStruct(ref path, ref opt_children) => {
423
            visitor.visit_path(path, pattern.id);
424
            if let Some(ref children) = *opt_children {
425
                walk_list!(visitor, visit_pat, children);
426
            }
427
        }
428 429 430
        PatKind::Path(ref path) => {
            visitor.visit_path(path, pattern.id);
        }
431
        PatKind::QPath(ref qself, ref path) => {
432 433 434
            visitor.visit_ty(&qself.ty);
            visitor.visit_path(path, pattern.id)
        }
435
        PatKind::Struct(ref path, ref fields, _) => {
436
            visitor.visit_path(path, pattern.id);
437
            for field in fields {
438 439
                visitor.visit_ident(field.span, field.node.ident);
                visitor.visit_pat(&field.node.pat)
440 441
            }
        }
442
        PatKind::Tup(ref tuple_elements) => {
443
            walk_list!(visitor, visit_pat, tuple_elements);
444
        }
445 446
        PatKind::Box(ref subpattern) |
        PatKind::Ref(ref subpattern, _) => {
447
            visitor.visit_pat(subpattern)
448
        }
449
        PatKind::Ident(_, ref pth1, ref optional_subpattern) => {
450
            visitor.visit_ident(pth1.span, pth1.node);
451
            walk_list!(visitor, visit_pat, optional_subpattern);
452
        }
453 454
        PatKind::Lit(ref expression) => visitor.visit_expr(expression),
        PatKind::Range(ref lower_bound, ref upper_bound) => {
455 456
            visitor.visit_expr(lower_bound);
            visitor.visit_expr(upper_bound)
457
        }
458 459
        PatKind::Wild => (),
        PatKind::Vec(ref prepatterns, ref slice_pattern, ref postpatterns) => {
460 461 462
            walk_list!(visitor, visit_pat, prepatterns);
            walk_list!(visitor, visit_pat, slice_pattern);
            walk_list!(visitor, visit_pat, postpatterns);
463
        }
464
        PatKind::Mac(ref mac) => visitor.visit_mac(mac),
M
Marijn Haverbeke 已提交
465 466 467
    }
}

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

472
    match foreign_item.node {
473
        ForeignItemKind::Fn(ref function_declaration, ref generics) => {
474
            walk_fn_decl(visitor, function_declaration);
475
            visitor.visit_generics(generics)
476
        }
477
        ForeignItemKind::Static(ref typ, _) => visitor.visit_ty(typ),
M
Marijn Haverbeke 已提交
478 479
    }

480
    walk_list!(visitor, visit_attribute, &foreign_item.attrs);
481 482 483 484 485
}

pub fn walk_ty_param_bound<'v, V: Visitor<'v>>(visitor: &mut V,
                                               bound: &'v TyParamBound) {
    match *bound {
N
Nick Cameron 已提交
486 487
        TraitTyParamBound(ref typ, ref modifier) => {
            visitor.visit_poly_trait_ref(typ, modifier);
488 489
        }
        RegionTyParamBound(ref lifetime) => {
490
            visitor.visit_lifetime(lifetime);
491
        }
492 493 494
    }
}

495
pub fn walk_generics<'v, V: Visitor<'v>>(visitor: &mut V, generics: &'v Generics) {
496
    for param in &generics.ty_params {
497
        visitor.visit_ident(param.span, param.ident);
498 499
        walk_list!(visitor, visit_ty_param_bound, &param.bounds);
        walk_list!(visitor, visit_ty, &param.default);
500
    }
501
    walk_list!(visitor, visit_lifetime_def, &generics.lifetimes);
502
    for predicate in &generics.where_clause.predicates {
503 504 505 506 507
        match *predicate {
            WherePredicate::BoundPredicate(WhereBoundPredicate{ref bounded_ty,
                                                               ref bounds,
                                                               ref bound_lifetimes,
                                                               ..}) => {
508 509 510
                visitor.visit_ty(bounded_ty);
                walk_list!(visitor, visit_ty_param_bound, bounds);
                walk_list!(visitor, visit_lifetime_def, bound_lifetimes);
511
            }
512 513 514
            WherePredicate::RegionPredicate(WhereRegionPredicate{ref lifetime,
                                                                 ref bounds,
                                                                 ..}) => {
515 516
                visitor.visit_lifetime(lifetime);
                walk_list!(visitor, visit_lifetime, bounds);
517
            }
518 519 520 521
            WherePredicate::EqPredicate(WhereEqPredicate{id,
                                                         ref path,
                                                         ref ty,
                                                         ..}) => {
522
                visitor.visit_path(path, id);
523
                visitor.visit_ty(ty);
524 525
            }
        }
526
    }
527 528
}

529
pub fn walk_fn_ret_ty<'v, V: Visitor<'v>>(visitor: &mut V, ret_ty: &'v FunctionRetTy) {
530
    if let FunctionRetTy::Ty(ref output_ty) = *ret_ty {
531 532 533 534
        visitor.visit_ty(output_ty)
    }
}

535
pub fn walk_fn_decl<'v, V: Visitor<'v>>(visitor: &mut V, function_declaration: &'v FnDecl) {
536
    for argument in &function_declaration.inputs {
537 538 539
        visitor.visit_pat(&argument.pat);
        visitor.visit_ty(&argument.ty)
    }
540
    walk_fn_ret_ty(visitor, &function_declaration.output)
M
Marijn Haverbeke 已提交
541 542
}

543 544
pub fn walk_fn_kind<'v, V: Visitor<'v>>(visitor: &mut V,
                                        function_kind: FnKind<'v>) {
545
    match function_kind {
546
        FnKind::ItemFn(_, generics, _, _, _, _) => {
547
            visitor.visit_generics(generics);
548
        }
549
        FnKind::Method(_, sig, _) => {
550 551
            visitor.visit_generics(&sig.generics);
            visitor.visit_explicit_self(&sig.explicit_self);
552
        }
553
        FnKind::Closure => {}
554
    }
555
}
556

557 558 559 560 561 562 563
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) {
    walk_fn_decl(visitor, function_declaration);
    walk_fn_kind(visitor, function_kind);
564
    visitor.visit_block(function_body)
M
Marijn Haverbeke 已提交
565 566
}

567 568
pub fn walk_trait_item<'v, V: Visitor<'v>>(visitor: &mut V, trait_item: &'v TraitItem) {
    visitor.visit_ident(trait_item.span, trait_item.ident);
569
    walk_list!(visitor, visit_attribute, &trait_item.attrs);
570
    match trait_item.node {
571
        TraitItemKind::Const(ref ty, ref default) => {
572
            visitor.visit_ty(ty);
573
            walk_list!(visitor, visit_expr, default);
574
        }
575
        TraitItemKind::Method(ref sig, None) => {
576 577 578
            visitor.visit_explicit_self(&sig.explicit_self);
            visitor.visit_generics(&sig.generics);
            walk_fn_decl(visitor, &sig.decl);
579
        }
580
        TraitItemKind::Method(ref sig, Some(ref body)) => {
581
            visitor.visit_fn(FnKind::Method(trait_item.ident, sig, None), &sig.decl,
582
                             body, trait_item.span, trait_item.id);
583
        }
584
        TraitItemKind::Type(ref bounds, ref default) => {
585 586
            walk_list!(visitor, visit_ty_param_bound, bounds);
            walk_list!(visitor, visit_ty, default);
587 588
        }
    }
589 590
}

591 592
pub fn walk_impl_item<'v, V: Visitor<'v>>(visitor: &mut V, impl_item: &'v ImplItem) {
    visitor.visit_ident(impl_item.span, impl_item.ident);
593
    walk_list!(visitor, visit_attribute, &impl_item.attrs);
594
    match impl_item.node {
595
        ImplItemKind::Const(ref ty, ref expr) => {
596 597 598
            visitor.visit_ty(ty);
            visitor.visit_expr(expr);
        }
599
        ImplItemKind::Method(ref sig, ref body) => {
600
            visitor.visit_fn(FnKind::Method(impl_item.ident, sig, Some(impl_item.vis)), &sig.decl,
601
                             body, impl_item.span, impl_item.id);
602
        }
603
        ImplItemKind::Type(ref ty) => {
604
            visitor.visit_ty(ty);
605
        }
606
        ImplItemKind::Macro(ref mac) => {
607 608
            visitor.visit_mac(mac);
        }
609 610 611
    }
}

612
pub fn walk_struct_def<'v, V: Visitor<'v>>(visitor: &mut V,
613
                                           struct_definition: &'v VariantData) {
614
    walk_list!(visitor, visit_struct_field, struct_definition.fields());
615 616
}

617 618
pub fn walk_struct_field<'v, V: Visitor<'v>>(visitor: &mut V,
                                             struct_field: &'v StructField) {
619 620 621
    walk_opt_ident(visitor, struct_field.span, struct_field.node.ident());
    visitor.visit_ty(&struct_field.node.ty);
    walk_list!(visitor, visit_attribute, &struct_field.node.attrs);
622 623
}

624
pub fn walk_block<'v, V: Visitor<'v>>(visitor: &mut V, block: &'v Block) {
625 626
    walk_list!(visitor, visit_stmt, &block.stmts);
    walk_list!(visitor, visit_expr, &block.expr);
M
Marijn Haverbeke 已提交
627 628
}

629
pub fn walk_stmt<'v, V: Visitor<'v>>(visitor: &mut V, statement: &'v Stmt) {
630
    match statement.node {
631 632
        StmtKind::Decl(ref declaration, _) => visitor.visit_decl(declaration),
        StmtKind::Expr(ref expression, _) | StmtKind::Semi(ref expression, _) => {
633
            visitor.visit_expr(expression)
634
        }
635
        StmtKind::Mac(ref mac, _, ref attrs) => {
636
            visitor.visit_mac(mac);
637
            for attr in attrs.as_attr_slice() {
638 639 640
                visitor.visit_attribute(attr);
            }
        }
M
Marijn Haverbeke 已提交
641 642 643
    }
}

644
pub fn walk_decl<'v, V: Visitor<'v>>(visitor: &mut V, declaration: &'v Decl) {
645
    match declaration.node {
646 647
        DeclKind::Local(ref local) => visitor.visit_local(local),
        DeclKind::Item(ref item) => visitor.visit_item(item),
648
    }
M
Marijn Haverbeke 已提交
649 650
}

651
pub fn walk_mac<'v, V: Visitor<'v>>(_: &mut V, _: &'v Mac) {
652
    // Empty!
653 654
}

655
pub fn walk_expr<'v, V: Visitor<'v>>(visitor: &mut V, expression: &'v Expr) {
656
    match expression.node {
657
        ExprKind::Box(ref subexpression) => {
658
            visitor.visit_expr(subexpression)
659
        }
660
        ExprKind::InPlace(ref place, ref subexpression) => {
661 662
            visitor.visit_expr(place);
            visitor.visit_expr(subexpression)
663
        }
664
        ExprKind::Vec(ref subexpressions) => {
665
            walk_list!(visitor, visit_expr, subexpressions);
666
        }
667
        ExprKind::Repeat(ref element, ref count) => {
668 669
            visitor.visit_expr(element);
            visitor.visit_expr(count)
670
        }
671
        ExprKind::Struct(ref path, ref fields, ref optional_base) => {
672
            visitor.visit_path(path, expression.id);
673
            for field in fields {
674 675
                visitor.visit_ident(field.ident.span, field.ident.node);
                visitor.visit_expr(&field.expr)
676
            }
677
            walk_list!(visitor, visit_expr, optional_base);
678
        }
679
        ExprKind::Tup(ref subexpressions) => {
680
            walk_list!(visitor, visit_expr, subexpressions);
681
        }
682
        ExprKind::Call(ref callee_expression, ref arguments) => {
683 684
            walk_list!(visitor, visit_expr, arguments);
            visitor.visit_expr(callee_expression)
685
        }
686
        ExprKind::MethodCall(ref ident, ref types, ref arguments) => {
687 688 689
            visitor.visit_ident(ident.span, ident.node);
            walk_list!(visitor, visit_expr, arguments);
            walk_list!(visitor, visit_ty, types);
690
        }
691
        ExprKind::Binary(_, ref left_expression, ref right_expression) => {
692 693
            visitor.visit_expr(left_expression);
            visitor.visit_expr(right_expression)
694
        }
695
        ExprKind::AddrOf(_, ref subexpression) | ExprKind::Unary(_, ref subexpression) => {
696
            visitor.visit_expr(subexpression)
697
        }
698 699
        ExprKind::Lit(_) => {}
        ExprKind::Cast(ref subexpression, ref typ) | ExprKind::Type(ref subexpression, ref typ) => {
700 701
            visitor.visit_expr(subexpression);
            visitor.visit_ty(typ)
702
        }
703
        ExprKind::If(ref head_expression, ref if_block, ref optional_else) => {
704 705 706
            visitor.visit_expr(head_expression);
            visitor.visit_block(if_block);
            walk_list!(visitor, visit_expr, optional_else);
707
        }
708
        ExprKind::While(ref subexpression, ref block, opt_ident) => {
709 710 711
            visitor.visit_expr(subexpression);
            visitor.visit_block(block);
            walk_opt_ident(visitor, expression.span, opt_ident)
712
        }
713
        ExprKind::IfLet(ref pattern, ref subexpression, ref if_block, ref optional_else) => {
714 715 716 717 718
            visitor.visit_pat(pattern);
            visitor.visit_expr(subexpression);
            visitor.visit_block(if_block);
            walk_list!(visitor, visit_expr, optional_else);
        }
719
        ExprKind::WhileLet(ref pattern, ref subexpression, ref block, opt_ident) => {
720 721 722 723 724
            visitor.visit_pat(pattern);
            visitor.visit_expr(subexpression);
            visitor.visit_block(block);
            walk_opt_ident(visitor, expression.span, opt_ident)
        }
725
        ExprKind::ForLoop(ref pattern, ref subexpression, ref block, opt_ident) => {
726 727 728 729 730
            visitor.visit_pat(pattern);
            visitor.visit_expr(subexpression);
            visitor.visit_block(block);
            walk_opt_ident(visitor, expression.span, opt_ident)
        }
731
        ExprKind::Loop(ref block, opt_ident) => {
732 733 734
            visitor.visit_block(block);
            walk_opt_ident(visitor, expression.span, opt_ident)
        }
735
        ExprKind::Match(ref subexpression, ref arms) => {
736 737
            visitor.visit_expr(subexpression);
            walk_list!(visitor, visit_arm, arms);
738
        }
739
        ExprKind::Closure(_, ref function_declaration, ref body) => {
740
            visitor.visit_fn(FnKind::Closure,
741 742
                             function_declaration,
                             body,
743
                             expression.span,
744
                             expression.id)
745
        }
746 747
        ExprKind::Block(ref block) => visitor.visit_block(block),
        ExprKind::Assign(ref left_hand_expression, ref right_hand_expression) => {
748 749
            visitor.visit_expr(right_hand_expression);
            visitor.visit_expr(left_hand_expression)
750
        }
751
        ExprKind::AssignOp(_, ref left_expression, ref right_expression) => {
752 753
            visitor.visit_expr(right_expression);
            visitor.visit_expr(left_expression)
754
        }
755
        ExprKind::Field(ref subexpression, ref ident) => {
756 757
            visitor.visit_expr(subexpression);
            visitor.visit_ident(ident.span, ident.node);
758
        }
759
        ExprKind::TupField(ref subexpression, _) => {
760
            visitor.visit_expr(subexpression);
761
        }
762
        ExprKind::Index(ref main_expression, ref index_expression) => {
763 764
            visitor.visit_expr(main_expression);
            visitor.visit_expr(index_expression)
765
        }
766
        ExprKind::Range(ref start, ref end) => {
767 768
            walk_list!(visitor, visit_expr, start);
            walk_list!(visitor, visit_expr, end);
N
Nick Cameron 已提交
769
        }
770
        ExprKind::Path(ref maybe_qself, ref path) => {
771
            if let Some(ref qself) = *maybe_qself {
772 773
                visitor.visit_ty(&qself.ty);
            }
774
            visitor.visit_path(path, expression.id)
775
        }
776
        ExprKind::Break(ref opt_sp_ident) | ExprKind::Again(ref opt_sp_ident) => {
777 778 779 780
            for sp_ident in opt_sp_ident {
                visitor.visit_ident(sp_ident.span, sp_ident.node);
            }
        }
781
        ExprKind::Ret(ref optional_expression) => {
782
            walk_list!(visitor, visit_expr, optional_expression);
783
        }
784 785
        ExprKind::Mac(ref mac) => visitor.visit_mac(mac),
        ExprKind::Paren(ref subexpression) => {
786
            visitor.visit_expr(subexpression)
787
        }
788
        ExprKind::InlineAsm(ref ia) => {
789 790
            for &(_, ref input) in &ia.inputs {
                visitor.visit_expr(&input)
791
            }
792 793
            for output in &ia.outputs {
                visitor.visit_expr(&output.expr)
794 795
            }
        }
M
Marijn Haverbeke 已提交
796
    }
797

798
    visitor.visit_expr_post(expression)
M
Marijn Haverbeke 已提交
799 800
}

801
pub fn walk_arm<'v, V: Visitor<'v>>(visitor: &mut V, arm: &'v Arm) {
802 803 804 805
    walk_list!(visitor, visit_pat, &arm.pats);
    walk_list!(visitor, visit_expr, &arm.guard);
    visitor.visit_expr(&arm.body);
    walk_list!(visitor, visit_attribute, &arm.attrs);
M
Marijn Haverbeke 已提交
806
}