visit.rs 32.5 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 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 58
    fn visit_name(&mut self, _span: Span, _name: Name) {
        // Nothing to do.
    }
    fn visit_ident(&mut self, span: Span, ident: Ident) {
59
        walk_ident(self, span, 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 72 73 74
    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) }
    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) }
75
    fn visit_generic_param(&mut self, param: &'ast GenericParam) { walk_generic_param(self, param) }
76
    fn visit_generics(&mut self, g: &'ast Generics) { walk_generics(self, g) }
77 78 79
    fn visit_where_predicate(&mut self, p: &'ast WherePredicate) {
        walk_where_predicate(self, p)
    }
80
    fn visit_fn(&mut self, fk: FnKind<'ast>, fd: &'ast FnDecl, s: Span, _: NodeId) {
81
        walk_fn(self, fk, fd, s)
82
    }
83 84 85 86
    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) {
87 88
        walk_ty_param_bound(self, bounds)
    }
89
    fn visit_poly_trait_ref(&mut self, t: &'ast PolyTraitRef, m: &'ast TraitBoundModifier) {
N
Nick Cameron 已提交
90
        walk_poly_trait_ref(self, t, m)
N
Niko Matsakis 已提交
91
    }
92 93
    fn visit_variant_data(&mut self, s: &'ast VariantData, _: Ident,
                          _: &'ast Generics, _: NodeId, _: Span) {
94 95
        walk_struct_def(self, s)
    }
96 97 98
    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) {
99 100
        walk_enum_def(self, enum_definition, generics, item_id)
    }
101
    fn visit_variant(&mut self, v: &'ast Variant, g: &'ast Generics, item_id: NodeId) {
102
        walk_variant(self, v, g, item_id)
103
    }
104 105 106
    fn visit_label(&mut self, label: &'ast Label) {
        walk_label(self, label)
    }
107
    fn visit_lifetime(&mut self, lifetime: &'ast Lifetime) {
108
        walk_lifetime(self, lifetime)
109
    }
110
    fn visit_mac(&mut self, _mac: &'ast Mac) {
S
Steve Klabnik 已提交
111
        panic!("visit_mac disabled by default");
J
John Clements 已提交
112 113 114 115
        // 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 已提交
116
        // visit::walk_mac(self, _mac)
117
    }
J
Jeffrey Seyfried 已提交
118
    fn visit_mac_def(&mut self, _mac: &'ast MacroDef, _id: NodeId) {
119 120
        // Nothing to do
    }
121
    fn visit_path(&mut self, path: &'ast Path, _id: NodeId) {
122
        walk_path(self, path)
123
    }
124 125
    fn visit_use_tree(&mut self, use_tree: &'ast UseTree, id: NodeId, _nested: bool) {
        walk_use_tree(self, use_tree, id)
126
    }
127
    fn visit_path_segment(&mut self, path_span: Span, path_segment: &'ast PathSegment) {
128 129
        walk_path_segment(self, path_span, path_segment)
    }
130
    fn visit_path_parameters(&mut self, path_span: Span, path_parameters: &'ast PathParameters) {
131 132
        walk_path_parameters(self, path_span, path_parameters)
    }
133
    fn visit_assoc_type_binding(&mut self, type_binding: &'ast TypeBinding) {
134 135
        walk_assoc_type_binding(self, type_binding)
    }
136 137 138 139 140 141 142 143 144 145
    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 已提交
146
    // FIXME: add `visit_interpolated` and `walk_interpolated`
147
    fn visit_vis(&mut self, vis: &'ast Visibility) {
148 149
        walk_vis(self, vis)
    }
150
    fn visit_fn_ret_ty(&mut self, ret_ty: &'ast FunctionRetTy) {
151 152
        walk_fn_ret_ty(self, ret_ty)
    }
153 154
}

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

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

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

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

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

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

195
pub fn walk_lifetime<'a, V: Visitor<'a>>(visitor: &mut V, lifetime: &'a Lifetime) {
J
Jeffrey Seyfried 已提交
196
    visitor.visit_ident(lifetime.span, lifetime.ident);
197 198
}

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

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

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

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

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

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

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

353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
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 {
        UseTreeKind::Simple(ident) => {
            visitor.visit_ident(use_tree.span, ident);
        }
        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);
            }
        }
    }
369 370
}

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

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

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

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

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

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

464
    walk_list!(visitor, visit_attribute, &foreign_item.attrs);
465 466
}

467 468 469 470
pub fn walk_global_asm<'a, V: Visitor<'a>>(_: &mut V, _: &'a GlobalAsm) {
    // Empty!
}

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

482 483 484 485 486 487 488 489 490 491 492 493 494
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) => {
            visitor.visit_ident(t.span, t.ident);
            walk_list!(visitor, visit_ty_param_bound, &t.bounds);
            walk_list!(visitor, visit_ty, &t.default);
            walk_list!(visitor, visit_attribute, &*t.attrs);
        }
495
    }
496 497 498 499
}

pub fn walk_generics<'a, V: Visitor<'a>>(visitor: &mut V, generics: &'a Generics) {
    walk_list!(visitor, visit_generic_param, &generics.params);
500 501 502 503 504 505 506
    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,
507
                                                           ref bound_generic_params,
508 509 510
                                                           ..}) => {
            visitor.visit_ty(bounded_ty);
            walk_list!(visitor, visit_ty_param_bound, bounds);
511
            walk_list!(visitor, visit_generic_param, bound_generic_params);
512 513 514 515 516 517 518 519 520 521 522 523
        }
        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);
524
        }
525
    }
526 527
}

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

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

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

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

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

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

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

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

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

644
pub fn walk_mac<'a, V: Visitor<'a>>(_: &mut V, _: &Mac) {
645
    // Empty!
646 647
}

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

804
    visitor.visit_expr_post(expression)
M
Marijn Haverbeke 已提交
805 806
}

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

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

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);
    }
}