visit.rs 30.2 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 29
use syntax_pos::Span;
use codemap::Spanned;
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, &'a Visibility),
35

36
    /// fn foo(&self)
37
    Method(Ident, &'a MethodSig, Option<&'a 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: 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
    fn visit_mod(&mut self, m: &Mod, _s: Span, _n: NodeId) { walk_mod(self, m) }
    fn visit_foreign_item(&mut self, i: &ForeignItem) { walk_foreign_item(self, i) }
    fn visit_item(&mut self, i: &Item) { walk_item(self, i) }
    fn visit_local(&mut self, l: &Local) { walk_local(self, l) }
    fn visit_block(&mut self, b: &Block) { walk_block(self, b) }
    fn visit_stmt(&mut self, s: &Stmt) { walk_stmt(self, s) }
    fn visit_arm(&mut self, a: &Arm) { walk_arm(self, a) }
    fn visit_pat(&mut self, p: &Pat) { walk_pat(self, p) }
    fn visit_expr(&mut self, ex: &Expr) { walk_expr(self, ex) }
    fn visit_expr_post(&mut self, _ex: &Expr) { }
    fn visit_ty(&mut self, t: &Ty) { walk_ty(self, t) }
    fn visit_generics(&mut self, g: &Generics) { walk_generics(self, g) }
    fn visit_fn(&mut self, fk: FnKind, fd: &FnDecl, b: &Block, s: Span, _: NodeId) {
72 73
        walk_fn(self, fk, fd, b, s)
    }
74 75 76 77
    fn visit_trait_item(&mut self, ti: &TraitItem) { walk_trait_item(self, ti) }
    fn visit_impl_item(&mut self, ii: &ImplItem) { walk_impl_item(self, ii) }
    fn visit_trait_ref(&mut self, t: &TraitRef) { walk_trait_ref(self, t) }
    fn visit_ty_param_bound(&mut self, bounds: &TyParamBound) {
78 79
        walk_ty_param_bound(self, bounds)
    }
80
    fn visit_poly_trait_ref(&mut self, t: &PolyTraitRef, m: &TraitBoundModifier) {
N
Nick Cameron 已提交
81
        walk_poly_trait_ref(self, t, m)
N
Niko Matsakis 已提交
82
    }
83 84
    fn visit_variant_data(&mut self, s: &VariantData, _: Ident,
                          _: &Generics, _: NodeId, _: Span) {
85 86
        walk_struct_def(self, s)
    }
87 88 89
    fn visit_struct_field(&mut self, s: &StructField) { walk_struct_field(self, s) }
    fn visit_enum_def(&mut self, enum_definition: &EnumDef,
                      generics: &Generics, item_id: NodeId, _: Span) {
90 91
        walk_enum_def(self, enum_definition, generics, item_id)
    }
92
    fn visit_variant(&mut self, v: &Variant, g: &Generics, item_id: NodeId) {
93
        walk_variant(self, v, g, item_id)
94
    }
95
    fn visit_lifetime(&mut self, lifetime: &Lifetime) {
96
        walk_lifetime(self, lifetime)
97
    }
98
    fn visit_lifetime_def(&mut self, lifetime: &LifetimeDef) {
99
        walk_lifetime_def(self, lifetime)
100
    }
101
    fn visit_mac(&mut self, _mac: &Mac) {
S
Steve Klabnik 已提交
102
        panic!("visit_mac disabled by default");
J
John Clements 已提交
103 104 105 106
        // 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 已提交
107
        // visit::walk_mac(self, _mac)
108
    }
109
    fn visit_path(&mut self, path: &Path, _id: NodeId) {
110
        walk_path(self, path)
111
    }
112
    fn visit_path_list_item(&mut self, prefix: &Path, item: &PathListItem) {
113 114
        walk_path_list_item(self, prefix, item)
    }
115
    fn visit_path_segment(&mut self, path_span: Span, path_segment: &PathSegment) {
116 117
        walk_path_segment(self, path_span, path_segment)
    }
118
    fn visit_path_parameters(&mut self, path_span: Span, path_parameters: &PathParameters) {
119 120
        walk_path_parameters(self, path_span, path_parameters)
    }
121
    fn visit_assoc_type_binding(&mut self, type_binding: &TypeBinding) {
122 123
        walk_assoc_type_binding(self, type_binding)
    }
124 125
    fn visit_attribute(&mut self, _attr: &Attribute) {}
    fn visit_macro_def(&mut self, macro_def: &MacroDef) {
126 127
        walk_macro_def(self, macro_def)
    }
128
    fn visit_vis(&mut self, vis: &Visibility) {
129 130
        walk_vis(self, vis)
    }
131 132
}

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

147
pub fn walk_opt_name<V: Visitor>(visitor: &mut V, span: Span, opt_name: Option<Name>) {
148
    if let Some(name) = opt_name {
149
        visitor.visit_name(span, name);
150 151 152
    }
}

153
pub fn walk_opt_ident<V: Visitor>(visitor: &mut V, span: Span, opt_ident: Option<Ident>) {
154
    if let Some(ident) = opt_ident {
155 156
        visitor.visit_ident(span, ident);
    }
157 158
}

159
pub fn walk_opt_sp_ident<V: Visitor>(visitor: &mut V, opt_sp_ident: &Option<Spanned<Ident>>) {
160 161 162 163 164
    if let Some(ref sp_ident) = *opt_sp_ident {
        visitor.visit_ident(sp_ident.span, sp_ident.node);
    }
}

165
pub fn walk_ident<V: Visitor>(visitor: &mut V, span: Span, ident: Ident) {
166
    visitor.visit_name(span, ident.name);
167 168
}

169
pub fn walk_crate<V: Visitor>(visitor: &mut V, krate: &Crate) {
170 171 172 173 174
    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);
}

175
pub fn walk_macro_def<V: Visitor>(visitor: &mut V, macro_def: &MacroDef) {
176 177 178 179 180
    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);
}

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

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

194
pub fn walk_lifetime<V: Visitor>(visitor: &mut V, lifetime: &Lifetime) {
195 196 197
    visitor.visit_name(lifetime.span, lifetime.name);
}

198
pub fn walk_lifetime_def<V: Visitor>(visitor: &mut V, lifetime_def: &LifetimeDef) {
199 200
    visitor.visit_lifetime(&lifetime_def.lifetime);
    walk_list!(visitor, visit_lifetime, &lifetime_def.bounds);
201 202
}

203 204
pub fn walk_poly_trait_ref<V>(visitor: &mut V, trait_ref: &PolyTraitRef, _: &TraitBoundModifier)
    where V: Visitor,
N
Niko Matsakis 已提交
205
{
206
    walk_list!(visitor, visit_lifetime_def, &trait_ref.bound_lifetimes);
N
Niko Matsakis 已提交
207 208 209
    visitor.visit_trait_ref(&trait_ref.trait_ref);
}

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

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

293 294 295 296
pub fn walk_enum_def<V: Visitor>(visitor: &mut V,
                                 enum_definition: &EnumDef,
                                 generics: &Generics,
                                 item_id: NodeId) {
297
    walk_list!(visitor, visit_variant, &enum_definition.variants, generics, item_id);
298 299
}

300 301 302
pub fn walk_variant<V>(visitor: &mut V, variant: &Variant, generics: &Generics, item_id: NodeId)
    where V: Visitor,
{
303
    visitor.visit_ident(variant.span, variant.node.name);
304
    visitor.visit_variant_data(&variant.node.data, variant.node.name,
305
                             generics, item_id, variant.span);
306 307
    walk_list!(visitor, visit_expr, &variant.node.disr_expr);
    walk_list!(visitor, visit_attribute, &variant.node.attrs);
S
Seo Sanghyeon 已提交
308 309
}

310
pub fn walk_ty<V: Visitor>(visitor: &mut V, typ: &Ty) {
311
    match typ.node {
312
        TyKind::Vec(ref ty) | TyKind::Paren(ref ty) => {
313
            visitor.visit_ty(ty)
314
        }
315
        TyKind::Ptr(ref mutable_type) => {
316
            visitor.visit_ty(&mutable_type.ty)
317
        }
318
        TyKind::Rptr(ref opt_lifetime, ref mutable_type) => {
319 320
            walk_list!(visitor, visit_lifetime, opt_lifetime);
            visitor.visit_ty(&mutable_type.ty)
321
        }
322
        TyKind::Empty => {},
323
        TyKind::Tup(ref tuple_element_types) => {
324
            walk_list!(visitor, visit_ty, tuple_element_types);
325
        }
326
        TyKind::BareFn(ref function_declaration) => {
327 328
            walk_fn_decl(visitor, &function_declaration.decl);
            walk_list!(visitor, visit_lifetime_def, &function_declaration.lifetimes);
329
        }
330
        TyKind::Path(ref maybe_qself, ref path) => {
331
            if let Some(ref qself) = *maybe_qself {
332 333
                visitor.visit_ty(&qself.ty);
            }
334
            visitor.visit_path(path, typ.id);
335
        }
336
        TyKind::ObjectSum(ref ty, ref bounds) => {
337 338
            visitor.visit_ty(ty);
            walk_list!(visitor, visit_ty_param_bound, bounds);
339
        }
340
        TyKind::FixedLengthVec(ref ty, ref expression) => {
341 342
            visitor.visit_ty(ty);
            visitor.visit_expr(expression)
343
        }
344
        TyKind::PolyTraitRef(ref bounds) => {
345
            walk_list!(visitor, visit_ty_param_bound, bounds);
N
Niko Matsakis 已提交
346
        }
347 348 349
        TyKind::ImplTrait(ref bounds) => {
            walk_list!(visitor, visit_ty_param_bound, bounds);
        }
350
        TyKind::Typeof(ref expression) => {
351
            visitor.visit_expr(expression)
352
        }
353
        TyKind::Infer | TyKind::ImplicitSelf => {}
354
        TyKind::Mac(ref mac) => {
355 356
            visitor.visit_mac(mac)
        }
M
Marijn Haverbeke 已提交
357 358 359
    }
}

360
pub fn walk_path<V: Visitor>(visitor: &mut V, path: &Path) {
361
    for segment in &path.segments {
362 363 364
        visitor.visit_path_segment(path.span, segment);
    }
}
365

366
pub fn walk_path_list_item<V: Visitor>(visitor: &mut V, _prefix: &Path, item: &PathListItem) {
367 368
    walk_opt_ident(visitor, item.span, item.node.name());
    walk_opt_ident(visitor, item.span, item.node.rename());
369 370
}

371
pub fn walk_path_segment<V: Visitor>(visitor: &mut V, path_span: Span, segment: &PathSegment) {
372 373 374 375
    visitor.visit_ident(path_span, segment.identifier);
    visitor.visit_path_parameters(path_span, &segment.parameters);
}

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

392
pub fn walk_assoc_type_binding<V: Visitor>(visitor: &mut V, type_binding: &TypeBinding) {
393
    visitor.visit_ident(type_binding.span, type_binding.ident);
394
    visitor.visit_ty(&type_binding.ty);
395 396
}

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

442
pub fn walk_foreign_item<V: Visitor>(visitor: &mut V, foreign_item: &ForeignItem) {
443
    visitor.visit_vis(&foreign_item.vis);
444
    visitor.visit_ident(foreign_item.span, foreign_item.ident);
445

446
    match foreign_item.node {
447
        ForeignItemKind::Fn(ref function_declaration, ref generics) => {
448
            walk_fn_decl(visitor, function_declaration);
449
            visitor.visit_generics(generics)
450
        }
451
        ForeignItemKind::Static(ref typ, _) => visitor.visit_ty(typ),
M
Marijn Haverbeke 已提交
452 453
    }

454
    walk_list!(visitor, visit_attribute, &foreign_item.attrs);
455 456
}

457
pub fn walk_ty_param_bound<V: Visitor>(visitor: &mut V, bound: &TyParamBound) {
458
    match *bound {
N
Nick Cameron 已提交
459 460
        TraitTyParamBound(ref typ, ref modifier) => {
            visitor.visit_poly_trait_ref(typ, modifier);
461 462
        }
        RegionTyParamBound(ref lifetime) => {
463
            visitor.visit_lifetime(lifetime);
464
        }
465 466 467
    }
}

468
pub fn walk_generics<V: Visitor>(visitor: &mut V, generics: &Generics) {
469
    for param in &generics.ty_params {
470
        visitor.visit_ident(param.span, param.ident);
471 472
        walk_list!(visitor, visit_ty_param_bound, &param.bounds);
        walk_list!(visitor, visit_ty, &param.default);
473
    }
474
    walk_list!(visitor, visit_lifetime_def, &generics.lifetimes);
475
    for predicate in &generics.where_clause.predicates {
476 477 478 479 480
        match *predicate {
            WherePredicate::BoundPredicate(WhereBoundPredicate{ref bounded_ty,
                                                               ref bounds,
                                                               ref bound_lifetimes,
                                                               ..}) => {
481 482 483
                visitor.visit_ty(bounded_ty);
                walk_list!(visitor, visit_ty_param_bound, bounds);
                walk_list!(visitor, visit_lifetime_def, bound_lifetimes);
484
            }
485 486 487
            WherePredicate::RegionPredicate(WhereRegionPredicate{ref lifetime,
                                                                 ref bounds,
                                                                 ..}) => {
488 489
                visitor.visit_lifetime(lifetime);
                walk_list!(visitor, visit_lifetime, bounds);
490
            }
491 492 493 494
            WherePredicate::EqPredicate(WhereEqPredicate{id,
                                                         ref path,
                                                         ref ty,
                                                         ..}) => {
495
                visitor.visit_path(path, id);
496
                visitor.visit_ty(ty);
497 498
            }
        }
499
    }
500 501
}

502
pub fn walk_fn_ret_ty<V: Visitor>(visitor: &mut V, ret_ty: &FunctionRetTy) {
503
    if let FunctionRetTy::Ty(ref output_ty) = *ret_ty {
504 505 506 507
        visitor.visit_ty(output_ty)
    }
}

508
pub fn walk_fn_decl<V: Visitor>(visitor: &mut V, function_declaration: &FnDecl) {
509
    for argument in &function_declaration.inputs {
510 511 512
        visitor.visit_pat(&argument.pat);
        visitor.visit_ty(&argument.ty)
    }
513
    walk_fn_ret_ty(visitor, &function_declaration.output)
M
Marijn Haverbeke 已提交
514 515
}

516
pub fn walk_fn_kind<V: Visitor>(visitor: &mut V, function_kind: FnKind) {
517
    match function_kind {
518
        FnKind::ItemFn(_, generics, _, _, _, _) => {
519
            visitor.visit_generics(generics);
520
        }
521
        FnKind::Method(_, ref sig, _) => {
522
            visitor.visit_generics(&sig.generics);
523
        }
524
        FnKind::Closure => {}
525
    }
526
}
527

528 529 530 531 532 533
pub fn walk_fn<V>(visitor: &mut V, kind: FnKind, declaration: &FnDecl, body: &Block, _span: Span)
    where V: Visitor,
{
    walk_fn_decl(visitor, declaration);
    walk_fn_kind(visitor, kind);
    visitor.visit_block(body)
M
Marijn Haverbeke 已提交
534 535
}

536
pub fn walk_trait_item<V: Visitor>(visitor: &mut V, trait_item: &TraitItem) {
537
    visitor.visit_ident(trait_item.span, trait_item.ident);
538
    walk_list!(visitor, visit_attribute, &trait_item.attrs);
539
    match trait_item.node {
540
        TraitItemKind::Const(ref ty, ref default) => {
541
            visitor.visit_ty(ty);
542
            walk_list!(visitor, visit_expr, default);
543
        }
544
        TraitItemKind::Method(ref sig, None) => {
545 546
            visitor.visit_generics(&sig.generics);
            walk_fn_decl(visitor, &sig.decl);
547
        }
548
        TraitItemKind::Method(ref sig, Some(ref body)) => {
549
            visitor.visit_fn(FnKind::Method(trait_item.ident, sig, None), &sig.decl,
550
                             body, trait_item.span, trait_item.id);
551
        }
552
        TraitItemKind::Type(ref bounds, ref default) => {
553 554
            walk_list!(visitor, visit_ty_param_bound, bounds);
            walk_list!(visitor, visit_ty, default);
555
        }
556 557 558
        TraitItemKind::Macro(ref mac) => {
            visitor.visit_mac(mac);
        }
559
    }
560 561
}

562
pub fn walk_impl_item<V: Visitor>(visitor: &mut V, impl_item: &ImplItem) {
563
    visitor.visit_vis(&impl_item.vis);
564
    visitor.visit_ident(impl_item.span, impl_item.ident);
565
    walk_list!(visitor, visit_attribute, &impl_item.attrs);
566
    match impl_item.node {
567
        ImplItemKind::Const(ref ty, ref expr) => {
568 569 570
            visitor.visit_ty(ty);
            visitor.visit_expr(expr);
        }
571
        ImplItemKind::Method(ref sig, ref body) => {
572
            visitor.visit_fn(FnKind::Method(impl_item.ident, sig, Some(&impl_item.vis)), &sig.decl,
573
                             body, impl_item.span, impl_item.id);
574
        }
575
        ImplItemKind::Type(ref ty) => {
576
            visitor.visit_ty(ty);
577
        }
578
        ImplItemKind::Macro(ref mac) => {
579 580
            visitor.visit_mac(mac);
        }
581 582 583
    }
}

584
pub fn walk_struct_def<V: Visitor>(visitor: &mut V, struct_definition: &VariantData) {
585
    walk_list!(visitor, visit_struct_field, struct_definition.fields());
586 587
}

588
pub fn walk_struct_field<V: Visitor>(visitor: &mut V, struct_field: &StructField) {
589
    visitor.visit_vis(&struct_field.vis);
590 591 592
    walk_opt_ident(visitor, struct_field.span, struct_field.ident);
    visitor.visit_ty(&struct_field.ty);
    walk_list!(visitor, visit_attribute, &struct_field.attrs);
593 594
}

595
pub fn walk_block<V: Visitor>(visitor: &mut V, block: &Block) {
596
    walk_list!(visitor, visit_stmt, &block.stmts);
M
Marijn Haverbeke 已提交
597 598
}

599
pub fn walk_stmt<V: Visitor>(visitor: &mut V, statement: &Stmt) {
600
    match statement.node {
J
Jeffrey Seyfried 已提交
601 602 603
        StmtKind::Local(ref local) => visitor.visit_local(local),
        StmtKind::Item(ref item) => visitor.visit_item(item),
        StmtKind::Expr(ref expression) | StmtKind::Semi(ref expression) => {
604
            visitor.visit_expr(expression)
605
        }
J
Jeffrey Seyfried 已提交
606 607
        StmtKind::Mac(ref mac) => {
            let (ref mac, _, ref attrs) = **mac;
608
            visitor.visit_mac(mac);
609
            for attr in attrs.iter() {
610 611 612
                visitor.visit_attribute(attr);
            }
        }
M
Marijn Haverbeke 已提交
613 614 615
    }
}

616
pub fn walk_mac<V: Visitor>(_: &mut V, _: &Mac) {
617
    // Empty!
618 619
}

620
pub fn walk_expr<V: Visitor>(visitor: &mut V, expression: &Expr) {
621
    for attr in expression.attrs.iter() {
622 623
        visitor.visit_attribute(attr);
    }
624
    match expression.node {
625
        ExprKind::Box(ref subexpression) => {
626
            visitor.visit_expr(subexpression)
627
        }
628
        ExprKind::InPlace(ref place, ref subexpression) => {
629 630
            visitor.visit_expr(place);
            visitor.visit_expr(subexpression)
631
        }
632
        ExprKind::Vec(ref subexpressions) => {
633
            walk_list!(visitor, visit_expr, subexpressions);
634
        }
635
        ExprKind::Repeat(ref element, ref count) => {
636 637
            visitor.visit_expr(element);
            visitor.visit_expr(count)
638
        }
639
        ExprKind::Struct(ref path, ref fields, ref optional_base) => {
640
            visitor.visit_path(path, expression.id);
641
            for field in fields {
642 643
                visitor.visit_ident(field.ident.span, field.ident.node);
                visitor.visit_expr(&field.expr)
644
            }
645
            walk_list!(visitor, visit_expr, optional_base);
646
        }
647
        ExprKind::Tup(ref subexpressions) => {
648
            walk_list!(visitor, visit_expr, subexpressions);
649
        }
650
        ExprKind::Call(ref callee_expression, ref arguments) => {
651 652
            walk_list!(visitor, visit_expr, arguments);
            visitor.visit_expr(callee_expression)
653
        }
654
        ExprKind::MethodCall(ref ident, ref types, ref arguments) => {
655 656 657
            visitor.visit_ident(ident.span, ident.node);
            walk_list!(visitor, visit_expr, arguments);
            walk_list!(visitor, visit_ty, types);
658
        }
659
        ExprKind::Binary(_, ref left_expression, ref right_expression) => {
660 661
            visitor.visit_expr(left_expression);
            visitor.visit_expr(right_expression)
662
        }
663
        ExprKind::AddrOf(_, ref subexpression) | ExprKind::Unary(_, ref subexpression) => {
664
            visitor.visit_expr(subexpression)
665
        }
666 667
        ExprKind::Lit(_) => {}
        ExprKind::Cast(ref subexpression, ref typ) | ExprKind::Type(ref subexpression, ref typ) => {
668 669
            visitor.visit_expr(subexpression);
            visitor.visit_ty(typ)
670
        }
671
        ExprKind::If(ref head_expression, ref if_block, ref optional_else) => {
672 673 674
            visitor.visit_expr(head_expression);
            visitor.visit_block(if_block);
            walk_list!(visitor, visit_expr, optional_else);
675
        }
676
        ExprKind::While(ref subexpression, ref block, ref opt_sp_ident) => {
677 678
            visitor.visit_expr(subexpression);
            visitor.visit_block(block);
679
            walk_opt_sp_ident(visitor, opt_sp_ident);
680
        }
681
        ExprKind::IfLet(ref pattern, ref subexpression, ref if_block, ref optional_else) => {
682 683 684 685 686
            visitor.visit_pat(pattern);
            visitor.visit_expr(subexpression);
            visitor.visit_block(if_block);
            walk_list!(visitor, visit_expr, optional_else);
        }
687
        ExprKind::WhileLet(ref pattern, ref subexpression, ref block, ref opt_sp_ident) => {
688 689 690
            visitor.visit_pat(pattern);
            visitor.visit_expr(subexpression);
            visitor.visit_block(block);
691
            walk_opt_sp_ident(visitor, opt_sp_ident);
692
        }
693
        ExprKind::ForLoop(ref pattern, ref subexpression, ref block, ref opt_sp_ident) => {
694 695 696
            visitor.visit_pat(pattern);
            visitor.visit_expr(subexpression);
            visitor.visit_block(block);
697
            walk_opt_sp_ident(visitor, opt_sp_ident);
698
        }
699
        ExprKind::Loop(ref block, ref opt_sp_ident) => {
700
            visitor.visit_block(block);
701
            walk_opt_sp_ident(visitor, opt_sp_ident);
702
        }
703
        ExprKind::Match(ref subexpression, ref arms) => {
704 705
            visitor.visit_expr(subexpression);
            walk_list!(visitor, visit_arm, arms);
706
        }
707
        ExprKind::Closure(_, ref function_declaration, ref body, _decl_span) => {
708
            visitor.visit_fn(FnKind::Closure,
709 710
                             function_declaration,
                             body,
711
                             expression.span,
712
                             expression.id)
713
        }
714 715
        ExprKind::Block(ref block) => visitor.visit_block(block),
        ExprKind::Assign(ref left_hand_expression, ref right_hand_expression) => {
716 717
            visitor.visit_expr(right_hand_expression);
            visitor.visit_expr(left_hand_expression)
718
        }
719
        ExprKind::AssignOp(_, ref left_expression, ref right_expression) => {
720 721
            visitor.visit_expr(right_expression);
            visitor.visit_expr(left_expression)
722
        }
723
        ExprKind::Field(ref subexpression, ref ident) => {
724 725
            visitor.visit_expr(subexpression);
            visitor.visit_ident(ident.span, ident.node);
726
        }
727
        ExprKind::TupField(ref subexpression, _) => {
728
            visitor.visit_expr(subexpression);
729
        }
730
        ExprKind::Index(ref main_expression, ref index_expression) => {
731 732
            visitor.visit_expr(main_expression);
            visitor.visit_expr(index_expression)
733
        }
A
Alex Burka 已提交
734
        ExprKind::Range(ref start, ref end, _) => {
735 736
            walk_list!(visitor, visit_expr, start);
            walk_list!(visitor, visit_expr, end);
N
Nick Cameron 已提交
737
        }
738
        ExprKind::Path(ref maybe_qself, ref path) => {
739
            if let Some(ref qself) = *maybe_qself {
740 741
                visitor.visit_ty(&qself.ty);
            }
742
            visitor.visit_path(path, expression.id)
743
        }
744
        ExprKind::Break(ref opt_sp_ident) | ExprKind::Continue(ref opt_sp_ident) => {
745
            walk_opt_sp_ident(visitor, opt_sp_ident);
746
        }
747
        ExprKind::Ret(ref optional_expression) => {
748
            walk_list!(visitor, visit_expr, optional_expression);
749
        }
750 751
        ExprKind::Mac(ref mac) => visitor.visit_mac(mac),
        ExprKind::Paren(ref subexpression) => {
752
            visitor.visit_expr(subexpression)
753
        }
754
        ExprKind::InlineAsm(ref ia) => {
755 756
            for &(_, ref input) in &ia.inputs {
                visitor.visit_expr(&input)
757
            }
758 759
            for output in &ia.outputs {
                visitor.visit_expr(&output.expr)
760 761
            }
        }
J
Jorge Aparicio 已提交
762 763 764
        ExprKind::Try(ref subexpression) => {
            visitor.visit_expr(subexpression)
        }
M
Marijn Haverbeke 已提交
765
    }
766

767
    visitor.visit_expr_post(expression)
M
Marijn Haverbeke 已提交
768 769
}

770
pub fn walk_arm<V: Visitor>(visitor: &mut V, arm: &Arm) {
771 772 773 774
    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 已提交
775
}
776

777
pub fn walk_vis<V: Visitor>(visitor: &mut V, vis: &Visibility) {
778 779
    if let Visibility::Restricted { ref path, id } = *vis {
        visitor.visit_path(path, id);
780 781
    }
}