visit.rs 32.4 KB
Newer Older
1
// Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT
2 3 4 5 6
// 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
A
Andrew Cann 已提交
7
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 9 10
// 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 rustc_target::spec::abi::Abi;
P
Patrick Walton 已提交
27
use ast::*;
28 29
use syntax_pos::Span;
use codemap::Spanned;
30 31
use parse::token::Token;
use tokenstream::{TokenTree, TokenStream};
32

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

38
    /// fn foo(&self)
39
    Method(Ident, &'a MethodSig, Option<&'a Visibility>, &'a Block),
40

41 42
    /// |x, y| body
    Closure(&'a Expr),
43 44
}

45
/// Each method of the Visitor trait is a hook to be potentially
46
/// overridden.  Each method's default implementation recursively visits
47 48 49 50 51 52 53
/// the substructure of the input via the corresponding `walk` method;
/// e.g. the `visit_mod` method by default calls `visit::walk_mod`.
///
/// If you want to ensure that your code handles every variant
/// explicitly, you need to override each method.  (And you also need
/// to monitor future changes to `Visitor` in case a new method with a
/// new default implementation gets introduced.)
54
pub trait Visitor<'ast>: Sized {
55 56 57
    fn visit_name(&mut self, _span: Span, _name: Name) {
        // Nothing to do.
    }
58 59
    fn visit_ident(&mut self, ident: Ident) {
        walk_ident(self, ident);
60
    }
N
Nick Cameron 已提交
61 62 63
    fn visit_mod(&mut self, m: &'ast Mod, _s: Span, _attrs: &[Attribute], _n: NodeId) {
        walk_mod(self, m);
    }
64
    fn visit_foreign_item(&mut self, i: &'ast ForeignItem) { walk_foreign_item(self, i) }
65
    fn visit_global_asm(&mut self, ga: &'ast GlobalAsm) { walk_global_asm(self, ga) }
66 67 68 69 70 71
    fn visit_item(&mut self, i: &'ast Item) { walk_item(self, i) }
    fn visit_local(&mut self, l: &'ast Local) { walk_local(self, l) }
    fn visit_block(&mut self, b: &'ast Block) { walk_block(self, b) }
    fn visit_stmt(&mut self, s: &'ast Stmt) { walk_stmt(self, s) }
    fn visit_arm(&mut self, a: &'ast Arm) { walk_arm(self, a) }
    fn visit_pat(&mut self, p: &'ast Pat) { walk_pat(self, p) }
72
    fn visit_anon_const(&mut self, c: &'ast AnonConst) { walk_anon_const(self, c) }
73 74 75
    fn visit_expr(&mut self, ex: &'ast Expr) { walk_expr(self, ex) }
    fn visit_expr_post(&mut self, _ex: &'ast Expr) { }
    fn visit_ty(&mut self, t: &'ast Ty) { walk_ty(self, t) }
76
    fn visit_generic_param(&mut self, param: &'ast GenericParam) { walk_generic_param(self, param) }
77
    fn visit_generics(&mut self, g: &'ast Generics) { walk_generics(self, g) }
78 79 80
    fn visit_where_predicate(&mut self, p: &'ast WherePredicate) {
        walk_where_predicate(self, p)
    }
81
    fn visit_fn(&mut self, fk: FnKind<'ast>, fd: &'ast FnDecl, s: Span, _: NodeId) {
82
        walk_fn(self, fk, fd, s)
83
    }
84 85 86 87
    fn visit_trait_item(&mut self, ti: &'ast TraitItem) { walk_trait_item(self, ti) }
    fn visit_impl_item(&mut self, ii: &'ast ImplItem) { walk_impl_item(self, ii) }
    fn visit_trait_ref(&mut self, t: &'ast TraitRef) { walk_trait_ref(self, t) }
    fn visit_ty_param_bound(&mut self, bounds: &'ast TyParamBound) {
88 89
        walk_ty_param_bound(self, bounds)
    }
90
    fn visit_poly_trait_ref(&mut self, t: &'ast PolyTraitRef, m: &'ast TraitBoundModifier) {
N
Nick Cameron 已提交
91
        walk_poly_trait_ref(self, t, m)
N
Niko Matsakis 已提交
92
    }
93 94
    fn visit_variant_data(&mut self, s: &'ast VariantData, _: Ident,
                          _: &'ast Generics, _: NodeId, _: Span) {
95 96
        walk_struct_def(self, s)
    }
97 98 99
    fn visit_struct_field(&mut self, s: &'ast StructField) { walk_struct_field(self, s) }
    fn visit_enum_def(&mut self, enum_definition: &'ast EnumDef,
                      generics: &'ast Generics, item_id: NodeId, _: Span) {
100 101
        walk_enum_def(self, enum_definition, generics, item_id)
    }
102
    fn visit_variant(&mut self, v: &'ast Variant, g: &'ast Generics, item_id: NodeId) {
103
        walk_variant(self, v, g, item_id)
104
    }
105 106 107
    fn visit_label(&mut self, label: &'ast Label) {
        walk_label(self, label)
    }
108
    fn visit_lifetime(&mut self, lifetime: &'ast Lifetime) {
109
        walk_lifetime(self, lifetime)
110
    }
111
    fn visit_mac(&mut self, _mac: &'ast Mac) {
S
Steve Klabnik 已提交
112
        panic!("visit_mac disabled by default");
J
John Clements 已提交
113 114 115 116
        // 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 已提交
117
        // visit::walk_mac(self, _mac)
118
    }
J
Jeffrey Seyfried 已提交
119
    fn visit_mac_def(&mut self, _mac: &'ast MacroDef, _id: NodeId) {
120 121
        // Nothing to do
    }
122
    fn visit_path(&mut self, path: &'ast Path, _id: NodeId) {
123
        walk_path(self, path)
124
    }
125 126
    fn visit_use_tree(&mut self, use_tree: &'ast UseTree, id: NodeId, _nested: bool) {
        walk_use_tree(self, use_tree, id)
127
    }
128
    fn visit_path_segment(&mut self, path_span: Span, path_segment: &'ast PathSegment) {
129 130
        walk_path_segment(self, path_span, path_segment)
    }
131
    fn visit_path_parameters(&mut self, path_span: Span, path_parameters: &'ast PathParameters) {
132 133
        walk_path_parameters(self, path_span, path_parameters)
    }
134
    fn visit_assoc_type_binding(&mut self, type_binding: &'ast TypeBinding) {
135 136
        walk_assoc_type_binding(self, type_binding)
    }
137 138 139 140 141 142 143 144 145 146
    fn visit_attribute(&mut self, attr: &'ast Attribute) {
        walk_attribute(self, attr)
    }
    fn visit_tt(&mut self, tt: TokenTree) {
        walk_tt(self, tt)
    }
    fn visit_tts(&mut self, tts: TokenStream) {
        walk_tts(self, tts)
    }
    fn visit_token(&mut self, _t: Token) {}
S
sinkuu 已提交
147
    // FIXME: add `visit_interpolated` and `walk_interpolated`
148
    fn visit_vis(&mut self, vis: &'ast Visibility) {
149 150
        walk_vis(self, vis)
    }
151
    fn visit_fn_ret_ty(&mut self, ret_ty: &'ast FunctionRetTy) {
152 153
        walk_fn_ret_ty(self, ret_ty)
    }
154 155
}

156 157
#[macro_export]
macro_rules! walk_list {
158
    ($visitor: expr, $method: ident, $list: expr) => {
159 160 161
        for elem in $list {
            $visitor.$method(elem)
        }
162 163 164 165 166
    };
    ($visitor: expr, $method: ident, $list: expr, $($extra_args: expr),*) => {
        for elem in $list {
            $visitor.$method(elem, $($extra_args,)*)
        }
167
    }
168 169
}

170 171
pub fn walk_ident<'a, V: Visitor<'a>>(visitor: &mut V, ident: Ident) {
    visitor.visit_name(ident.span, ident.name);
172 173
}

174
pub fn walk_crate<'a, V: Visitor<'a>>(visitor: &mut V, krate: &'a Crate) {
N
Nick Cameron 已提交
175
    visitor.visit_mod(&krate.module, krate.span, &krate.attrs, CRATE_NODE_ID);
176 177 178
    walk_list!(visitor, visit_attribute, &krate.attrs);
}

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

183
pub fn walk_local<'a, V: Visitor<'a>>(visitor: &mut V, local: &'a Local) {
184
    for attr in local.attrs.iter() {
185 186
        visitor.visit_attribute(attr);
    }
187 188 189
    visitor.visit_pat(&local.pat);
    walk_list!(visitor, visit_ty, &local.ty);
    walk_list!(visitor, visit_expr, &local.init);
190 191
}

192
pub fn walk_label<'a, V: Visitor<'a>>(visitor: &mut V, label: &'a Label) {
193
    visitor.visit_ident(label.ident);
194 195
}

196
pub fn walk_lifetime<'a, V: Visitor<'a>>(visitor: &mut V, lifetime: &'a Lifetime) {
197
    visitor.visit_ident(lifetime.ident);
198 199
}

200 201 202 203
pub fn walk_poly_trait_ref<'a, V>(visitor: &mut V,
                                  trait_ref: &'a PolyTraitRef,
                                  _: &TraitBoundModifier)
    where V: Visitor<'a>,
N
Niko Matsakis 已提交
204
{
205
    walk_list!(visitor, visit_generic_param, &trait_ref.bound_generic_params);
N
Niko Matsakis 已提交
206 207 208
    visitor.visit_trait_ref(&trait_ref.trait_ref);
}

209
pub fn walk_trait_ref<'a, V: Visitor<'a>>(visitor: &mut V, trait_ref: &'a TraitRef) {
210
    visitor.visit_path(&trait_ref.path, trait_ref.ref_id)
211 212
}

213
pub fn walk_item<'a, V: Visitor<'a>>(visitor: &mut V, item: &'a Item) {
214
    visitor.visit_vis(&item.vis);
215
    visitor.visit_ident(item.ident);
216
    match item.node {
217 218 219
        ItemKind::ExternCrate(orig_name) => {
            if let Some(orig_name) = orig_name {
                visitor.visit_name(item.span, orig_name);
220
            }
221
        }
222 223
        ItemKind::Use(ref use_tree) => {
            visitor.visit_use_tree(use_tree, item.id, false)
224
        }
225 226
        ItemKind::Static(ref typ, _, ref expr) |
        ItemKind::Const(ref typ, ref expr) => {
227 228
            visitor.visit_ty(typ);
            visitor.visit_expr(expr);
229
        }
230
        ItemKind::Fn(ref declaration, unsafety, constness, abi, ref generics, ref body) => {
231 232
            visitor.visit_generics(generics);
            visitor.visit_fn(FnKind::ItemFn(item.ident, unsafety,
233
                                            constness, abi, &item.vis, body),
234
                             declaration,
235
                             item.span,
236
                             item.id)
237
        }
238
        ItemKind::Mod(ref module) => {
N
Nick Cameron 已提交
239
            visitor.visit_mod(module, item.span, &item.attrs, item.id)
240
        }
241
        ItemKind::ForeignMod(ref foreign_module) => {
242
            walk_list!(visitor, visit_foreign_item, &foreign_module.items);
243
        }
244
        ItemKind::GlobalAsm(ref ga) => visitor.visit_global_asm(ga),
245
        ItemKind::Ty(ref typ, ref type_parameters) => {
246
            visitor.visit_ty(typ);
247
            visitor.visit_generics(type_parameters)
248
        }
249
        ItemKind::Enum(ref enum_definition, ref type_parameters) => {
250
            visitor.visit_generics(type_parameters);
251
            visitor.visit_enum_def(enum_definition, type_parameters, item.id, item.span)
252
        }
253
        ItemKind::Impl(_, _, _,
254
                 ref type_parameters,
255
                 ref opt_trait_reference,
256
                 ref typ,
257
                 ref impl_items) => {
258
            visitor.visit_generics(type_parameters);
259 260 261
            walk_list!(visitor, visit_trait_ref, opt_trait_reference);
            visitor.visit_ty(typ);
            walk_list!(visitor, visit_impl_item, impl_items);
262
        }
263 264
        ItemKind::Struct(ref struct_definition, ref generics) |
        ItemKind::Union(ref struct_definition, ref generics) => {
265
            visitor.visit_generics(generics);
266
            visitor.visit_variant_data(struct_definition, item.ident,
267
                                     generics, item.id, item.span);
268
        }
269
        ItemKind::Trait(.., ref generics, ref bounds, ref methods) => {
270
            visitor.visit_generics(generics);
271 272
            walk_list!(visitor, visit_ty_param_bound, bounds);
            walk_list!(visitor, visit_trait_item, methods);
273
        }
A
Alex Burka 已提交
274 275 276 277
        ItemKind::TraitAlias(ref generics, ref bounds) => {
            visitor.visit_generics(generics);
            walk_list!(visitor, visit_ty_param_bound, bounds);
        }
278
        ItemKind::Mac(ref mac) => visitor.visit_mac(mac),
279
        ItemKind::MacroDef(ref ts) => visitor.visit_mac_def(ts, item.id),
280
    }
281
    walk_list!(visitor, visit_attribute, &item.attrs);
M
Marijn Haverbeke 已提交
282 283
}

284 285 286
pub fn walk_enum_def<'a, V: Visitor<'a>>(visitor: &mut V,
                                 enum_definition: &'a EnumDef,
                                 generics: &'a Generics,
287
                                 item_id: NodeId) {
288
    walk_list!(visitor, visit_variant, &enum_definition.variants, generics, item_id);
289 290
}

291 292 293 294 295
pub fn walk_variant<'a, V>(visitor: &mut V,
                           variant: &'a Variant,
                           generics: &'a Generics,
                           item_id: NodeId)
    where V: Visitor<'a>,
296
{
297
    visitor.visit_ident(variant.node.ident);
298
    visitor.visit_variant_data(&variant.node.data, variant.node.ident,
299
                             generics, item_id, variant.span);
300
    walk_list!(visitor, visit_anon_const, &variant.node.disr_expr);
301
    walk_list!(visitor, visit_attribute, &variant.node.attrs);
S
Seo Sanghyeon 已提交
302 303
}

304
pub fn walk_ty<'a, V: Visitor<'a>>(visitor: &mut V, typ: &'a Ty) {
305
    match typ.node {
306
        TyKind::Slice(ref ty) | TyKind::Paren(ref ty) => {
307
            visitor.visit_ty(ty)
308
        }
309
        TyKind::Ptr(ref mutable_type) => {
310
            visitor.visit_ty(&mutable_type.ty)
311
        }
312
        TyKind::Rptr(ref opt_lifetime, ref mutable_type) => {
313 314
            walk_list!(visitor, visit_lifetime, opt_lifetime);
            visitor.visit_ty(&mutable_type.ty)
315
        }
A
Andrew Cann 已提交
316
        TyKind::Never => {},
317
        TyKind::Tup(ref tuple_element_types) => {
318
            walk_list!(visitor, visit_ty, tuple_element_types);
319
        }
320
        TyKind::BareFn(ref function_declaration) => {
321
            walk_fn_decl(visitor, &function_declaration.decl);
322
            walk_list!(visitor, visit_generic_param, &function_declaration.generic_params);
323
        }
324
        TyKind::Path(ref maybe_qself, ref path) => {
325
            if let Some(ref qself) = *maybe_qself {
326 327
                visitor.visit_ty(&qself.ty);
            }
328
            visitor.visit_path(path, typ.id);
329
        }
330
        TyKind::Array(ref ty, ref length) => {
331
            visitor.visit_ty(ty);
332
            visitor.visit_anon_const(length)
333
        }
V
Vadim Petrochenkov 已提交
334
        TyKind::TraitObject(ref bounds, ..) |
335 336 337
        TyKind::ImplTrait(ref bounds) => {
            walk_list!(visitor, visit_ty_param_bound, bounds);
        }
338
        TyKind::Typeof(ref expression) => {
339
            visitor.visit_anon_const(expression)
340
        }
341
        TyKind::Infer | TyKind::ImplicitSelf | TyKind::Err => {}
342
        TyKind::Mac(ref mac) => {
343 344
            visitor.visit_mac(mac)
        }
M
Marijn Haverbeke 已提交
345 346 347
    }
}

348
pub fn walk_path<'a, V: Visitor<'a>>(visitor: &mut V, path: &'a Path) {
349
    for segment in &path.segments {
350 351 352
        visitor.visit_path_segment(path.span, segment);
    }
}
353

354 355 356 357 358
pub fn walk_use_tree<'a, V: Visitor<'a>>(
    visitor: &mut V, use_tree: &'a UseTree, id: NodeId,
) {
    visitor.visit_path(&use_tree.prefix, id);
    match use_tree.kind {
359 360
        UseTreeKind::Simple(rename) => {
            if let Some(rename) = rename {
361
                visitor.visit_ident(rename);
362
            }
363 364 365 366 367 368 369 370
        }
        UseTreeKind::Glob => {},
        UseTreeKind::Nested(ref use_trees) => {
            for &(ref nested_tree, nested_id) in use_trees {
                visitor.visit_use_tree(nested_tree, nested_id, true);
            }
        }
    }
371 372
}

373 374 375
pub fn walk_path_segment<'a, V: Visitor<'a>>(visitor: &mut V,
                                             path_span: Span,
                                             segment: &'a PathSegment) {
376
    visitor.visit_ident(segment.ident);
J
Jeffrey Seyfried 已提交
377 378 379
    if let Some(ref parameters) = segment.parameters {
        visitor.visit_path_parameters(path_span, parameters);
    }
380 381
}

382 383 384 385
pub fn walk_path_parameters<'a, V>(visitor: &mut V,
                                   _path_span: Span,
                                   path_parameters: &'a PathParameters)
    where V: Visitor<'a>,
386
{
387
    match *path_parameters {
388
        PathParameters::AngleBracketed(ref data) => {
389 390 391
            walk_list!(visitor, visit_ty, &data.types);
            walk_list!(visitor, visit_lifetime, &data.lifetimes);
            walk_list!(visitor, visit_assoc_type_binding, &data.bindings);
392
        }
393
        PathParameters::Parenthesized(ref data) => {
394 395
            walk_list!(visitor, visit_ty, &data.inputs);
            walk_list!(visitor, visit_ty, &data.output);
396
        }
397
    }
398 399
}

400 401
pub fn walk_assoc_type_binding<'a, V: Visitor<'a>>(visitor: &mut V,
                                                   type_binding: &'a TypeBinding) {
402
    visitor.visit_ident(type_binding.ident);
403
    visitor.visit_ty(&type_binding.ty);
404 405
}

406
pub fn walk_pat<'a, V: Visitor<'a>>(visitor: &mut V, pattern: &'a Pat) {
407
    match pattern.node {
408
        PatKind::TupleStruct(ref path, ref children, _) => {
409
            visitor.visit_path(path, pattern.id);
410
            walk_list!(visitor, visit_pat, children);
411
        }
412 413 414 415
        PatKind::Path(ref opt_qself, ref path) => {
            if let Some(ref qself) = *opt_qself {
                visitor.visit_ty(&qself.ty);
            }
416 417
            visitor.visit_path(path, pattern.id)
        }
418
        PatKind::Struct(ref path, ref fields, _) => {
419
            visitor.visit_path(path, pattern.id);
420
            for field in fields {
421
                walk_list!(visitor, visit_attribute, field.node.attrs.iter());
422
                visitor.visit_ident(field.node.ident);
423
                visitor.visit_pat(&field.node.pat)
424 425
            }
        }
426
        PatKind::Tuple(ref tuple_elements, _) => {
427
            walk_list!(visitor, visit_pat, tuple_elements);
428
        }
429
        PatKind::Box(ref subpattern) |
430 431
        PatKind::Ref(ref subpattern, _) |
        PatKind::Paren(ref subpattern) => {
432
            visitor.visit_pat(subpattern)
433
        }
V
Vadim Petrochenkov 已提交
434
        PatKind::Ident(_, ident, ref optional_subpattern) => {
435
            visitor.visit_ident(ident);
436
            walk_list!(visitor, visit_pat, optional_subpattern);
437
        }
438
        PatKind::Lit(ref expression) => visitor.visit_expr(expression),
439
        PatKind::Range(ref lower_bound, ref upper_bound, _) => {
440
            visitor.visit_expr(lower_bound);
441
            visitor.visit_expr(upper_bound);
442
        }
443
        PatKind::Wild => (),
444
        PatKind::Slice(ref prepatterns, ref slice_pattern, ref postpatterns) => {
445 446 447
            walk_list!(visitor, visit_pat, prepatterns);
            walk_list!(visitor, visit_pat, slice_pattern);
            walk_list!(visitor, visit_pat, postpatterns);
448
        }
449
        PatKind::Mac(ref mac) => visitor.visit_mac(mac),
M
Marijn Haverbeke 已提交
450 451 452
    }
}

453
pub fn walk_foreign_item<'a, V: Visitor<'a>>(visitor: &mut V, foreign_item: &'a ForeignItem) {
454
    visitor.visit_vis(&foreign_item.vis);
455
    visitor.visit_ident(foreign_item.ident);
456

457
    match foreign_item.node {
458
        ForeignItemKind::Fn(ref function_declaration, ref generics) => {
459
            walk_fn_decl(visitor, function_declaration);
460
            visitor.visit_generics(generics)
461
        }
462
        ForeignItemKind::Static(ref typ, _) => visitor.visit_ty(typ),
P
Paul Lietar 已提交
463
        ForeignItemKind::Ty => (),
464
        ForeignItemKind::Macro(ref mac) => visitor.visit_mac(mac),
M
Marijn Haverbeke 已提交
465 466
    }

467
    walk_list!(visitor, visit_attribute, &foreign_item.attrs);
468 469
}

470 471 472 473
pub fn walk_global_asm<'a, V: Visitor<'a>>(_: &mut V, _: &'a GlobalAsm) {
    // Empty!
}

474
pub fn walk_ty_param_bound<'a, V: Visitor<'a>>(visitor: &mut V, bound: &'a TyParamBound) {
475
    match *bound {
N
Nick Cameron 已提交
476 477
        TraitTyParamBound(ref typ, ref modifier) => {
            visitor.visit_poly_trait_ref(typ, modifier);
478 479
        }
        RegionTyParamBound(ref lifetime) => {
480
            visitor.visit_lifetime(lifetime);
481
        }
482 483 484
    }
}

485 486 487 488 489 490 491 492
pub fn walk_generic_param<'a, V: Visitor<'a>>(visitor: &mut V, param: &'a GenericParam) {
    match *param {
        GenericParam::Lifetime(ref l) => {
            visitor.visit_lifetime(&l.lifetime);
            walk_list!(visitor, visit_lifetime, &l.bounds);
            walk_list!(visitor, visit_attribute, &*l.attrs);
        }
        GenericParam::Type(ref t) => {
493
            visitor.visit_ident(t.ident);
494 495 496 497
            walk_list!(visitor, visit_ty_param_bound, &t.bounds);
            walk_list!(visitor, visit_ty, &t.default);
            walk_list!(visitor, visit_attribute, &*t.attrs);
        }
498
    }
499 500 501 502
}

pub fn walk_generics<'a, V: Visitor<'a>>(visitor: &mut V, generics: &'a Generics) {
    walk_list!(visitor, visit_generic_param, &generics.params);
503 504 505 506 507 508 509
    walk_list!(visitor, visit_where_predicate, &generics.where_clause.predicates);
}

pub fn walk_where_predicate<'a, V: Visitor<'a>>(visitor: &mut V, predicate: &'a WherePredicate) {
    match *predicate {
        WherePredicate::BoundPredicate(WhereBoundPredicate{ref bounded_ty,
                                                           ref bounds,
510
                                                           ref bound_generic_params,
511 512 513
                                                           ..}) => {
            visitor.visit_ty(bounded_ty);
            walk_list!(visitor, visit_ty_param_bound, bounds);
514
            walk_list!(visitor, visit_generic_param, bound_generic_params);
515 516 517 518 519 520 521 522 523 524 525 526
        }
        WherePredicate::RegionPredicate(WhereRegionPredicate{ref lifetime,
                                                             ref bounds,
                                                             ..}) => {
            visitor.visit_lifetime(lifetime);
            walk_list!(visitor, visit_lifetime, bounds);
        }
        WherePredicate::EqPredicate(WhereEqPredicate{ref lhs_ty,
                                                     ref rhs_ty,
                                                     ..}) => {
            visitor.visit_ty(lhs_ty);
            visitor.visit_ty(rhs_ty);
527
        }
528
    }
529 530
}

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

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

545 546
pub fn walk_fn<'a, V>(visitor: &mut V, kind: FnKind<'a>, declaration: &'a FnDecl, _span: Span)
    where V: Visitor<'a>,
547 548
{
    match kind {
549
        FnKind::ItemFn(_, _, _, _, _, body) => {
550 551
            walk_fn_decl(visitor, declaration);
            visitor.visit_block(body);
552
        }
553
        FnKind::Method(_, _, _, body) => {
554 555 556 557 558 559
            walk_fn_decl(visitor, declaration);
            visitor.visit_block(body);
        }
        FnKind::Closure(body) => {
            walk_fn_decl(visitor, declaration);
            visitor.visit_expr(body);
560 561
        }
    }
562
}
563

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

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

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

617
pub fn walk_struct_field<'a, V: Visitor<'a>>(visitor: &mut V, struct_field: &'a StructField) {
618
    visitor.visit_vis(&struct_field.vis);
619
    if let Some(ident) = struct_field.ident {
620
        visitor.visit_ident(ident);
621
    }
622 623
    visitor.visit_ty(&struct_field.ty);
    walk_list!(visitor, visit_attribute, &struct_field.attrs);
624 625
}

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

630
pub fn walk_stmt<'a, V: Visitor<'a>>(visitor: &mut V, statement: &'a Stmt) {
631
    match statement.node {
J
Jeffrey Seyfried 已提交
632 633 634
        StmtKind::Local(ref local) => visitor.visit_local(local),
        StmtKind::Item(ref item) => visitor.visit_item(item),
        StmtKind::Expr(ref expression) | StmtKind::Semi(ref expression) => {
635
            visitor.visit_expr(expression)
636
        }
J
Jeffrey Seyfried 已提交
637 638
        StmtKind::Mac(ref mac) => {
            let (ref mac, _, ref attrs) = **mac;
639
            visitor.visit_mac(mac);
640
            for attr in attrs.iter() {
641 642 643
                visitor.visit_attribute(attr);
            }
        }
M
Marijn Haverbeke 已提交
644 645 646
    }
}

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

651 652 653 654
pub fn walk_anon_const<'a, V: Visitor<'a>>(visitor: &mut V, constant: &'a AnonConst) {
    visitor.visit_expr(&constant.value);
}

655
pub fn walk_expr<'a, V: Visitor<'a>>(visitor: &mut V, expression: &'a Expr) {
656
    for attr in expression.attrs.iter() {
657 658
        visitor.visit_attribute(attr);
    }
659
    match expression.node {
660
        ExprKind::Box(ref subexpression) => {
661
            visitor.visit_expr(subexpression)
662
        }
663
        ExprKind::Array(ref subexpressions) => {
664
            walk_list!(visitor, visit_expr, subexpressions);
665
        }
666
        ExprKind::Repeat(ref element, ref count) => {
667
            visitor.visit_expr(element);
668
            visitor.visit_anon_const(count)
669
        }
670
        ExprKind::Struct(ref path, ref fields, ref optional_base) => {
671
            visitor.visit_path(path, expression.id);
672
            for field in fields {
673
                walk_list!(visitor, visit_attribute, field.attrs.iter());
674
                visitor.visit_ident(field.ident);
675
                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
            visitor.visit_expr(callee_expression);
684
            walk_list!(visitor, visit_expr, arguments);
685
        }
686 687
        ExprKind::MethodCall(ref segment, ref arguments) => {
            visitor.visit_path_segment(expression.span, segment);
688
            walk_list!(visitor, visit_expr, arguments);
689
        }
690
        ExprKind::Binary(_, ref left_expression, ref right_expression) => {
691 692
            visitor.visit_expr(left_expression);
            visitor.visit_expr(right_expression)
693
        }
694
        ExprKind::AddrOf(_, ref subexpression) | ExprKind::Unary(_, ref subexpression) => {
695
            visitor.visit_expr(subexpression)
696
        }
697 698
        ExprKind::Lit(_) => {}
        ExprKind::Cast(ref subexpression, ref typ) | ExprKind::Type(ref subexpression, ref typ) => {
699 700
            visitor.visit_expr(subexpression);
            visitor.visit_ty(typ)
701
        }
702
        ExprKind::If(ref head_expression, ref if_block, ref optional_else) => {
703 704 705
            visitor.visit_expr(head_expression);
            visitor.visit_block(if_block);
            walk_list!(visitor, visit_expr, optional_else);
706
        }
707 708
        ExprKind::While(ref subexpression, ref block, ref opt_label) => {
            walk_list!(visitor, visit_label, opt_label);
709 710
            visitor.visit_expr(subexpression);
            visitor.visit_block(block);
711
        }
712 713
        ExprKind::IfLet(ref pats, ref subexpression, ref if_block, ref optional_else) => {
            walk_list!(visitor, visit_pat, pats);
714 715 716 717
            visitor.visit_expr(subexpression);
            visitor.visit_block(if_block);
            walk_list!(visitor, visit_expr, optional_else);
        }
718
        ExprKind::WhileLet(ref pats, ref subexpression, ref block, ref opt_label) => {
719
            walk_list!(visitor, visit_label, opt_label);
720
            walk_list!(visitor, visit_pat, pats);
721 722 723
            visitor.visit_expr(subexpression);
            visitor.visit_block(block);
        }
724 725
        ExprKind::ForLoop(ref pattern, ref subexpression, ref block, ref opt_label) => {
            walk_list!(visitor, visit_label, opt_label);
726 727 728 729
            visitor.visit_pat(pattern);
            visitor.visit_expr(subexpression);
            visitor.visit_block(block);
        }
730 731
        ExprKind::Loop(ref block, ref opt_label) => {
            walk_list!(visitor, visit_label, opt_label);
732 733
            visitor.visit_block(block);
        }
734
        ExprKind::Match(ref subexpression, ref arms) => {
735 736
            visitor.visit_expr(subexpression);
            walk_list!(visitor, visit_arm, arms);
737
        }
738
        ExprKind::Closure(_, _, ref function_declaration, ref body, _decl_span) => {
739
            visitor.visit_fn(FnKind::Closure(body),
740
                             function_declaration,
741
                             expression.span,
742
                             expression.id)
743
        }
744 745 746 747
        ExprKind::Block(ref block, ref opt_label) => {
            walk_list!(visitor, visit_label, opt_label);
            visitor.visit_block(block);
        }
748
        ExprKind::Assign(ref left_hand_expression, ref right_hand_expression) => {
749
            visitor.visit_expr(left_hand_expression);
750
            visitor.visit_expr(right_hand_expression);
751
        }
752
        ExprKind::AssignOp(_, ref left_expression, ref right_expression) => {
753
            visitor.visit_expr(left_expression);
754
            visitor.visit_expr(right_expression);
755
        }
V
Vadim Petrochenkov 已提交
756
        ExprKind::Field(ref subexpression, ident) => {
757
            visitor.visit_expr(subexpression);
758
            visitor.visit_ident(ident);
759
        }
760
        ExprKind::Index(ref main_expression, ref index_expression) => {
761 762
            visitor.visit_expr(main_expression);
            visitor.visit_expr(index_expression)
763
        }
A
Alex Burka 已提交
764
        ExprKind::Range(ref start, ref end, _) => {
765 766
            walk_list!(visitor, visit_expr, start);
            walk_list!(visitor, visit_expr, end);
N
Nick Cameron 已提交
767
        }
768
        ExprKind::Path(ref maybe_qself, ref path) => {
769
            if let Some(ref qself) = *maybe_qself {
770 771
                visitor.visit_ty(&qself.ty);
            }
772
            visitor.visit_path(path, expression.id)
773
        }
774 775
        ExprKind::Break(ref opt_label, ref opt_expr) => {
            walk_list!(visitor, visit_label, opt_label);
776 777
            walk_list!(visitor, visit_expr, opt_expr);
        }
778 779
        ExprKind::Continue(ref opt_label) => {
            walk_list!(visitor, visit_label, opt_label);
780
        }
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
            for &(_, ref input) in &ia.inputs {
790
                visitor.visit_expr(input)
791
            }
792 793
            for output in &ia.outputs {
                visitor.visit_expr(&output.expr)
794 795
            }
        }
J
John Kåre Alsaker 已提交
796 797 798
        ExprKind::Yield(ref optional_expression) => {
            walk_list!(visitor, visit_expr, optional_expression);
        }
J
Jorge Aparicio 已提交
799 800 801
        ExprKind::Try(ref subexpression) => {
            visitor.visit_expr(subexpression)
        }
802 803 804
        ExprKind::Catch(ref body) => {
            visitor.visit_block(body)
        }
M
Marijn Haverbeke 已提交
805
    }
806

807
    visitor.visit_expr_post(expression)
M
Marijn Haverbeke 已提交
808 809
}

810
pub fn walk_arm<'a, V: Visitor<'a>>(visitor: &mut V, arm: &'a Arm) {
811 812 813 814
    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 已提交
815
}
816

817
pub fn walk_vis<'a, V: Visitor<'a>>(visitor: &mut V, vis: &'a Visibility) {
818
    if let VisibilityKind::Restricted { ref path, id } = vis.node {
819
        visitor.visit_path(path, id);
820 821
    }
}
822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838

pub fn walk_attribute<'a, V: Visitor<'a>>(visitor: &mut V, attr: &'a Attribute) {
    visitor.visit_tts(attr.tokens.clone());
}

pub fn walk_tt<'a, V: Visitor<'a>>(visitor: &mut V, tt: TokenTree) {
    match tt {
        TokenTree::Token(_, tok) => visitor.visit_token(tok),
        TokenTree::Delimited(_, delimed) => visitor.visit_tts(delimed.stream()),
    }
}

pub fn walk_tts<'a, V: Visitor<'a>>(visitor: &mut V, tts: TokenStream) {
    for tt in tts.trees() {
        visitor.visit_tt(tt);
    }
}