visit.rs 31.9 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
        PatEnum(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 431
        PatQPath(ref qself, ref path) => {
            visitor.visit_ty(&qself.ty);
            visitor.visit_path(path, pattern.id)
        }
432
        PatStruct(ref path, ref fields, _) => {
433
            visitor.visit_path(path, pattern.id);
434
            for field in fields {
435 436
                visitor.visit_ident(field.span, field.node.ident);
                visitor.visit_pat(&field.node.pat)
437 438
            }
        }
439
        PatTup(ref tuple_elements) => {
440
            walk_list!(visitor, visit_pat, tuple_elements);
441
        }
442
        PatBox(ref subpattern) |
443
        PatRegion(ref subpattern, _) => {
444
            visitor.visit_pat(subpattern)
445
        }
446
        PatIdent(_, ref pth1, ref optional_subpattern) => {
447
            visitor.visit_ident(pth1.span, pth1.node);
448
            walk_list!(visitor, visit_pat, optional_subpattern);
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
        }
V
Vadim Petrochenkov 已提交
455
        PatWild => (),
456 457 458 459
        PatVec(ref prepatterns, ref slice_pattern, ref postpatterns) => {
            walk_list!(visitor, visit_pat, prepatterns);
            walk_list!(visitor, visit_pat, slice_pattern);
            walk_list!(visitor, visit_pat, postpatterns);
460
        }
K
Keegan McAllister 已提交
461
        PatMac(ref mac) => visitor.visit_mac(mac),
M
Marijn Haverbeke 已提交
462 463 464
    }
}

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

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

477
    walk_list!(visitor, visit_attribute, &foreign_item.attrs);
478 479 480 481 482
}

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

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

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

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

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

554 555 556 557 558 559 560
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);
561
    visitor.visit_block(function_body)
M
Marijn Haverbeke 已提交
562 563
}

564 565
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);
566
    walk_list!(visitor, visit_attribute, &trait_item.attrs);
567
    match trait_item.node {
568
        TraitItemKind::Const(ref ty, ref default) => {
569
            visitor.visit_ty(ty);
570
            walk_list!(visitor, visit_expr, default);
571
        }
572
        TraitItemKind::Method(ref sig, None) => {
573 574 575
            visitor.visit_explicit_self(&sig.explicit_self);
            visitor.visit_generics(&sig.generics);
            walk_fn_decl(visitor, &sig.decl);
576
        }
577
        TraitItemKind::Method(ref sig, Some(ref body)) => {
578
            visitor.visit_fn(FnKind::Method(trait_item.ident, sig, None), &sig.decl,
579
                             body, trait_item.span, trait_item.id);
580
        }
581
        TraitItemKind::Type(ref bounds, ref default) => {
582 583
            walk_list!(visitor, visit_ty_param_bound, bounds);
            walk_list!(visitor, visit_ty, default);
584 585
        }
    }
586 587
}

588 589
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);
590
    walk_list!(visitor, visit_attribute, &impl_item.attrs);
591
    match impl_item.node {
592
        ImplItemKind::Const(ref ty, ref expr) => {
593 594 595
            visitor.visit_ty(ty);
            visitor.visit_expr(expr);
        }
596
        ImplItemKind::Method(ref sig, ref body) => {
597
            visitor.visit_fn(FnKind::Method(impl_item.ident, sig, Some(impl_item.vis)), &sig.decl,
598
                             body, impl_item.span, impl_item.id);
599
        }
600
        ImplItemKind::Type(ref ty) => {
601
            visitor.visit_ty(ty);
602
        }
603
        ImplItemKind::Macro(ref mac) => {
604 605
            visitor.visit_mac(mac);
        }
606 607 608
    }
}

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

614 615
pub fn walk_struct_field<'v, V: Visitor<'v>>(visitor: &mut V,
                                             struct_field: &'v StructField) {
616 617 618
    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);
619 620
}

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

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

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

648
pub fn walk_mac<'v, V: Visitor<'v>>(_: &mut V, _: &'v Mac) {
649
    // Empty!
650 651
}

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

795
    visitor.visit_expr_post(expression)
M
Marijn Haverbeke 已提交
796 797
}

798
pub fn walk_arm<'v, V: Visitor<'v>>(visitor: &mut V, arm: &'v Arm) {
799 800 801 802
    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 已提交
803
}