visit.rs 29.5 KB
Newer Older
1
// Copyright 2012-2013 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
use abi::AbiSet;
P
Patrick Walton 已提交
12
use ast::*;
13
use ast;
14
use codemap::Span;
15
use parse;
16
use opt_vec;
17
use opt_vec::OptVec;
18

M
Marijn Haverbeke 已提交
19 20 21 22 23
// Context-passing 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 (potentially passing in different contexts to each), call
// visit::visit_* to apply the default traversal algorithm (again, it can
// override the context), or prevent deeper traversal by doing nothing.
N
Niko Matsakis 已提交
24 25 26 27 28 29
//
// 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.
M
Marijn Haverbeke 已提交
30

31
pub enum FnKind<'a> {
32
    // fn foo() or extern "Abi" fn foo()
33
    FkItemFn(Ident, &'a Generics, Purity, AbiSet),
34 35

    // fn foo(&self)
36
    FkMethod(Ident, &'a Generics, &'a Method),
37 38

    // |x, y| ...
S
Seo Sanghyeon 已提交
39
    // proc(x, y) ...
40
    FkFnBlock,
41 42
}

43
pub fn name_of_fn(fk: &FnKind) -> Ident {
44
    match *fk {
45 46
        FkItemFn(name, _, _, _) | FkMethod(name, _, _) => name,
        FkFnBlock(..) => parse::token::special_idents::anon
47 48 49
    }
}

50
pub fn generics_of_fn(fk: &FnKind) -> Generics {
51
    match *fk {
52 53
        FkItemFn(_, generics, _, _) |
        FkMethod(_, generics, _) => {
54
            (*generics).clone()
55
        }
56
        FkFnBlock(..) => {
57 58 59 60
            Generics {
                lifetimes: opt_vec::Empty,
                ty_params: opt_vec::Empty,
            }
61
        }
62 63 64
    }
}

E
Eduard Burtescu 已提交
65
pub trait Visitor<E: Clone> {
66 67 68
    fn visit_ident(&mut self, _sp: Span, _ident: Ident, _e: E) {
        /*! Visit the idents */
    }
69 70 71 72
    fn visit_mod(&mut self, m: &Mod, _s: Span, _n: NodeId, e: E) { walk_mod(self, m, e) }
    fn visit_view_item(&mut self, i: &ViewItem, e: E) { walk_view_item(self, i, e) }
    fn visit_foreign_item(&mut self, i: &ForeignItem, e: E) { walk_foreign_item(self, i, e) }
    fn visit_item(&mut self, i: &Item, e: E) { walk_item(self, i, e) }
E
Eduard Burtescu 已提交
73 74 75 76 77 78 79 80 81 82
    fn visit_local(&mut self, l: &Local, e: E) { walk_local(self, l, e) }
    fn visit_block(&mut self, b: &Block, e: E) { walk_block(self, b, e) }
    fn visit_stmt(&mut self, s: &Stmt, e: E) { walk_stmt(self, s, e) }
    fn visit_arm(&mut self, a: &Arm, e: E) { walk_arm(self, a, e) }
    fn visit_pat(&mut self, p: &Pat, e: E) { walk_pat(self, p, e) }
    fn visit_decl(&mut self, d: &Decl, e: E) { walk_decl(self, d, e) }
    fn visit_expr(&mut self, ex: &Expr, e: E) { walk_expr(self, ex, e) }
    fn visit_expr_post(&mut self, _ex: &Expr, _e: E) { }
    fn visit_ty(&mut self, t: &Ty, e: E) { walk_ty(self, t, e) }
    fn visit_generics(&mut self, g: &Generics, e: E) { walk_generics(self, g, e) }
83
    fn visit_fn(&mut self, fk: &FnKind, fd: &FnDecl, b: &Block, s: Span, n: NodeId, e: E) {
84 85
        walk_fn(self, fk, fd, b, s, n , e)
    }
E
Eduard Burtescu 已提交
86
    fn visit_ty_method(&mut self, t: &TypeMethod, e: E) { walk_ty_method(self, t, e) }
87 88
    fn visit_trait_method(&mut self, t: &TraitMethod, e: E) { walk_trait_method(self, t, e) }
    fn visit_struct_def(&mut self, s: &StructDef, i: Ident, g: &Generics, n: NodeId, e: E) {
89 90
        walk_struct_def(self, s, i, g, n, e)
    }
91 92
    fn visit_struct_field(&mut self, s: &StructField, e: E) { walk_struct_field(self, s, e) }
    fn visit_variant(&mut self, v: &Variant, g: &Generics, e: E) { walk_variant(self, v, g, e) }
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
    fn visit_opt_lifetime_ref(&mut self,
                              _span: Span,
                              opt_lifetime: &Option<Lifetime>,
                              env: E) {
        /*!
         * Visits an optional reference to a lifetime. The `span` is
         * the span of some surrounding reference should opt_lifetime
         * be None.
         */
        match *opt_lifetime {
            Some(ref l) => self.visit_lifetime_ref(l, env),
            None => ()
        }
    }
    fn visit_lifetime_ref(&mut self, _lifetime: &Lifetime, _e: E) {
        /*! Visits a reference to a lifetime */
    }
    fn visit_lifetime_decl(&mut self, _lifetime: &Lifetime, _e: E) {
        /*! Visits a declaration of a lifetime */
    }
113
    fn visit_explicit_self(&mut self, es: &ExplicitSelf, e: E) {
114 115
        walk_explicit_self(self, es, e)
    }
116
    fn visit_mac(&mut self, macro: &Mac, e: E) {
117 118
        walk_mac(self, macro, e)
    }
119 120 121
    fn visit_path(&mut self, path: &Path, _id: ast::NodeId, e: E) {
        walk_path(self, path, e)
    }
122 123
}

124 125 126 127 128 129 130 131 132 133 134
pub fn walk_inlined_item<E: Clone, V: Visitor<E>>(visitor: &mut V,
                                                  item: &ast::InlinedItem,
                                                  env: E) {
    match *item {
        IIItem(i) => visitor.visit_item(i, env),
        IIForeign(i) => visitor.visit_foreign_item(i, env),
        IIMethod(_, _, m) => walk_method_helper(visitor, m, env),
    }
}


E
Eduard Burtescu 已提交
135
pub fn walk_crate<E: Clone, V: Visitor<E>>(visitor: &mut V, crate: &Crate, env: E) {
136 137 138
    visitor.visit_mod(&crate.module, crate.span, CRATE_NODE_ID, env)
}

139
pub fn walk_mod<E: Clone, V: Visitor<E>>(visitor: &mut V, module: &Mod, env: E) {
D
Daniel Micay 已提交
140
    for view_item in module.view_items.iter() {
141 142
        visitor.visit_view_item(view_item, env.clone())
    }
143

D
Daniel Micay 已提交
144
    for item in module.items.iter() {
145 146 147 148
        visitor.visit_item(*item, env.clone())
    }
}

149
pub fn walk_view_item<E: Clone, V: Visitor<E>>(visitor: &mut V, vi: &ViewItem, env: E) {
150
    match vi.node {
151
        ViewItemExternMod(name, _, _) => {
152 153
            visitor.visit_ident(vi.span, name, env)
        }
154
        ViewItemUse(ref paths) => {
155
            for vp in paths.iter() {
156
                match vp.node {
157
                    ViewPathSimple(ident, ref path, id) => {
158
                        visitor.visit_ident(vp.span, ident, env.clone());
159 160
                        visitor.visit_path(path, id, env.clone());
                    }
161
                    ViewPathGlob(ref path, id) => {
162
                        visitor.visit_path(path, id, env.clone());
163
                    }
164
                    ViewPathList(ref path, ref list, _) => {
165 166 167
                        for id in list.iter() {
                            visitor.visit_ident(id.span, id.node.name, env.clone())
                        }
168
                        walk_path(visitor, path, env.clone());
169
                    }
170
                }
171 172 173
            }
        }
    }
174 175
}

E
Eduard Burtescu 已提交
176
pub fn walk_local<E: Clone, V: Visitor<E>>(visitor: &mut V, local: &Local, env: E) {
177
    visitor.visit_pat(local.pat, env.clone());
178
    visitor.visit_ty(local.ty, env.clone());
179 180 181 182 183 184
    match local.init {
        None => {}
        Some(initializer) => visitor.visit_expr(initializer, env),
    }
}

E
Eduard Burtescu 已提交
185
fn walk_explicit_self<E: Clone, V: Visitor<E>>(visitor: &mut V,
186
                                               explicit_self: &ExplicitSelf,
E
Eduard Burtescu 已提交
187
                                               env: E) {
188
    match explicit_self.node {
189
        SelfStatic | SelfValue(_) | SelfBox | SelfUniq(_) => {}
190
        SelfRegion(ref lifetime, _) => {
191 192 193 194 195
            visitor.visit_opt_lifetime_ref(explicit_self.span, lifetime, env)
        }
    }
}

E
Eduard Burtescu 已提交
196
fn walk_trait_ref<E: Clone, V: Visitor<E>>(visitor: &mut V,
197
                                           trait_ref: &TraitRef,
E
Eduard Burtescu 已提交
198
                                           env: E) {
199
    visitor.visit_path(&trait_ref.path, trait_ref.ref_id, env)
200 201
}

202
pub fn walk_item<E: Clone, V: Visitor<E>>(visitor: &mut V, item: &Item, env: E) {
203
    visitor.visit_ident(item.span, item.ident, env.clone());
204
    match item.node {
205
        ItemStatic(typ, _, expr) => {
206 207 208
            visitor.visit_ty(typ, env.clone());
            visitor.visit_expr(expr, env);
        }
209 210
        ItemFn(declaration, purity, abi, ref generics, body) => {
            visitor.visit_fn(&FkItemFn(item.ident, generics, purity, abi),
211 212 213 214 215 216
                             declaration,
                             body,
                             item.span,
                             item.id,
                             env)
        }
217
        ItemMod(ref module) => {
218 219
            visitor.visit_mod(module, item.span, item.id, env)
        }
220
        ItemForeignMod(ref foreign_module) => {
D
Daniel Micay 已提交
221
            for view_item in foreign_module.view_items.iter() {
222
                visitor.visit_view_item(view_item, env.clone())
223
            }
D
Daniel Micay 已提交
224
            for foreign_item in foreign_module.items.iter() {
225
                visitor.visit_foreign_item(*foreign_item, env.clone())
226
            }
227
        }
228
        ItemTy(typ, ref type_parameters) => {
229 230 231
            visitor.visit_ty(typ, env.clone());
            visitor.visit_generics(type_parameters, env)
        }
232
        ItemEnum(ref enum_definition, ref type_parameters) => {
233
            visitor.visit_generics(type_parameters, env.clone());
234
            walk_enum_def(visitor, enum_definition, type_parameters, env)
235
        }
236
        ItemImpl(ref type_parameters,
237 238 239
                 ref trait_reference,
                 typ,
                 ref methods) => {
240
            visitor.visit_generics(type_parameters, env.clone());
241 242 243
            match *trait_reference {
                Some(ref trait_reference) => walk_trait_ref(visitor, trait_reference, env.clone()),
                None => ()
244
            }
245
            visitor.visit_ty(typ, env.clone());
D
Daniel Micay 已提交
246
            for method in methods.iter() {
247
                walk_method_helper(visitor, *method, env.clone())
248 249
            }
        }
250
        ItemStruct(struct_definition, ref generics) => {
251 252 253 254 255 256 257
            visitor.visit_generics(generics, env.clone());
            visitor.visit_struct_def(struct_definition,
                                     item.ident,
                                     generics,
                                     item.id,
                                     env)
        }
258
        ItemTrait(ref generics, ref trait_paths, ref methods) => {
259
            visitor.visit_generics(generics, env.clone());
D
Daniel Micay 已提交
260
            for trait_path in trait_paths.iter() {
261 262 263
                visitor.visit_path(&trait_path.path,
                                   trait_path.ref_id,
                                   env.clone())
264
            }
D
Daniel Micay 已提交
265
            for method in methods.iter() {
266
                visitor.visit_trait_method(method, env.clone())
267 268
            }
        }
269
        ItemMac(ref macro) => visitor.visit_mac(macro, env),
M
Marijn Haverbeke 已提交
270 271 272
    }
}

E
Eduard Burtescu 已提交
273
pub fn walk_enum_def<E: Clone, V:Visitor<E>>(visitor: &mut V,
274
                                             enum_definition: &EnumDef,
E
Eduard Burtescu 已提交
275 276
                                             generics: &Generics,
                                             env: E) {
277
    for &variant in enum_definition.variants.iter() {
278 279 280 281
        visitor.visit_variant(variant, generics, env.clone());
    }
}

E
Eduard Burtescu 已提交
282
pub fn walk_variant<E: Clone, V: Visitor<E>>(visitor: &mut V,
283
                                             variant: &Variant,
E
Eduard Burtescu 已提交
284 285
                                             generics: &Generics,
                                             env: E) {
286 287
    visitor.visit_ident(variant.span, variant.node.name, env.clone());

288
    match variant.node.kind {
289
        TupleVariantKind(ref variant_arguments) => {
290
            for variant_argument in variant_arguments.iter() {
291
                visitor.visit_ty(variant_argument.ty, env.clone())
292 293
            }
        }
294
        StructVariantKind(struct_definition) => {
295 296 297 298 299 300
            visitor.visit_struct_def(struct_definition,
                                     variant.node.name,
                                     generics,
                                     variant.node.id,
                                     env.clone())
        }
301
    }
302 303 304 305
    match variant.node.disr_expr {
        Some(expr) => visitor.visit_expr(expr, env),
        None => ()
    }
306 307
}

E
Eduard Burtescu 已提交
308
pub fn skip_ty<E, V: Visitor<E>>(_: &mut V, _: &Ty, _: E) {
309 310
    // Empty!
}
311

E
Eduard Burtescu 已提交
312
pub fn walk_ty<E: Clone, V: Visitor<E>>(visitor: &mut V, typ: &Ty, env: E) {
313
    match typ.node {
314
        TyUniq(ty) | TyVec(ty) | TyBox(ty) => {
315 316
            visitor.visit_ty(ty, env)
        }
317
        TyPtr(ref mutable_type) => {
318 319
            visitor.visit_ty(mutable_type.ty, env)
        }
320
        TyRptr(ref lifetime, ref mutable_type) => {
321
            visitor.visit_opt_lifetime_ref(typ.span, lifetime, env.clone());
322 323
            visitor.visit_ty(mutable_type.ty, env)
        }
324
        TyTup(ref tuple_element_types) => {
325
            for &tuple_element_type in tuple_element_types.iter() {
326
                visitor.visit_ty(tuple_element_type, env.clone())
327
            }
328
        }
329
        TyClosure(ref function_declaration) => {
330
            for argument in function_declaration.decl.inputs.iter() {
331
                visitor.visit_ty(argument.ty, env.clone())
332
            }
333
            visitor.visit_ty(function_declaration.decl.output, env.clone());
334
            for bounds in function_declaration.bounds.iter() {
335
                walk_ty_param_bounds(visitor, bounds, env.clone())
336 337 338 339 340 341 342
            }
            visitor.visit_opt_lifetime_ref(
                typ.span,
                &function_declaration.region,
                env.clone());
            walk_lifetime_decls(visitor, &function_declaration.lifetimes,
                                env.clone());
343
        }
344
        TyBareFn(ref function_declaration) => {
D
Daniel Micay 已提交
345
            for argument in function_declaration.decl.inputs.iter() {
346
                visitor.visit_ty(argument.ty, env.clone())
347
            }
348
            visitor.visit_ty(function_declaration.decl.output, env.clone());
349 350
            walk_lifetime_decls(visitor, &function_declaration.lifetimes,
                                env.clone());
351
        }
352
        TyPath(ref path, ref bounds, id) => {
353
            visitor.visit_path(path, id, env.clone());
D
Daniel Micay 已提交
354
            for bounds in bounds.iter() {
355
                walk_ty_param_bounds(visitor, bounds, env.clone())
356
            }
357
        }
358
        TyFixedLengthVec(ty, expression) => {
359
            visitor.visit_ty(ty, env.clone());
360 361
            visitor.visit_expr(expression, env)
        }
362
        TyTypeof(expression) => {
363 364
            visitor.visit_expr(expression, env)
        }
365
        TyNil | TyBot | TyInfer => {}
M
Marijn Haverbeke 已提交
366 367 368
    }
}

E
Eduard Burtescu 已提交
369 370 371
fn walk_lifetime_decls<E: Clone, V: Visitor<E>>(visitor: &mut V,
                                                lifetimes: &OptVec<Lifetime>,
                                                env: E) {
372 373 374 375 376
    for l in lifetimes.iter() {
        visitor.visit_lifetime_decl(l, env.clone());
    }
}

E
Eduard Burtescu 已提交
377
pub fn walk_path<E: Clone, V: Visitor<E>>(visitor: &mut V, path: &Path, env: E) {
378
    for segment in path.segments.iter() {
379 380
        visitor.visit_ident(path.span, segment.identifier, env.clone());

381
        for &typ in segment.types.iter() {
382 383 384 385
            visitor.visit_ty(typ, env.clone());
        }
        for lifetime in segment.lifetimes.iter() {
            visitor.visit_lifetime_ref(lifetime, env.clone());
386
        }
387
    }
388 389
}

E
Eduard Burtescu 已提交
390
pub fn walk_pat<E: Clone, V: Visitor<E>>(visitor: &mut V, pattern: &Pat, env: E) {
391
    match pattern.node {
392
        PatEnum(ref path, ref children) => {
393
            visitor.visit_path(path, pattern.id, env.clone());
D
Daniel Micay 已提交
394 395
            for children in children.iter() {
                for child in children.iter() {
396
                    visitor.visit_pat(*child, env.clone())
397
                }
398
            }
399
        }
400
        PatStruct(ref path, ref fields, _) => {
401
            visitor.visit_path(path, pattern.id, env.clone());
D
Daniel Micay 已提交
402
            for field in fields.iter() {
403
                visitor.visit_pat(field.pat, env.clone())
404 405
            }
        }
406
        PatTup(ref tuple_elements) => {
D
Daniel Micay 已提交
407
            for tuple_element in tuple_elements.iter() {
408
                visitor.visit_pat(*tuple_element, env.clone())
409
            }
410
        }
411 412
        PatUniq(subpattern) |
        PatRegion(subpattern) => {
413 414
            visitor.visit_pat(subpattern, env)
        }
415
        PatIdent(_, ref path, ref optional_subpattern) => {
416
            visitor.visit_path(path, pattern.id, env.clone());
417 418 419
            match *optional_subpattern {
                None => {}
                Some(subpattern) => visitor.visit_pat(subpattern, env),
420
            }
421
        }
422 423
        PatLit(expression) => visitor.visit_expr(expression, env),
        PatRange(lower_bound, upper_bound) => {
424 425
            visitor.visit_expr(lower_bound, env.clone());
            visitor.visit_expr(upper_bound, env)
426
        }
427
        PatWild | PatWildMulti => (),
428
        PatVec(ref prepattern, ref slice_pattern, ref postpatterns) => {
D
Daniel Micay 已提交
429
            for prepattern in prepattern.iter() {
430
                visitor.visit_pat(*prepattern, env.clone())
431
            }
D
Daniel Micay 已提交
432
            for slice_pattern in slice_pattern.iter() {
433
                visitor.visit_pat(*slice_pattern, env.clone())
434
            }
D
Daniel Micay 已提交
435
            for postpattern in postpatterns.iter() {
436
                visitor.visit_pat(*postpattern, env.clone())
437
            }
438
        }
M
Marijn Haverbeke 已提交
439 440 441
    }
}

E
Eduard Burtescu 已提交
442
pub fn walk_foreign_item<E: Clone, V: Visitor<E>>(visitor: &mut V,
443
                                                  foreign_item: &ForeignItem,
E
Eduard Burtescu 已提交
444
                                                  env: E) {
445 446
    visitor.visit_ident(foreign_item.span, foreign_item.ident, env.clone());

447
    match foreign_item.node {
448
        ForeignItemFn(function_declaration, ref generics) => {
449
            walk_fn_decl(visitor, function_declaration, env.clone());
450
            visitor.visit_generics(generics, env)
451
        }
452
        ForeignItemStatic(typ, _) => visitor.visit_ty(typ, env),
M
Marijn Haverbeke 已提交
453 454 455
    }
}

E
Eduard Burtescu 已提交
456 457 458
pub fn walk_ty_param_bounds<E: Clone, V: Visitor<E>>(visitor: &mut V,
                                                     bounds: &OptVec<TyParamBound>,
                                                     env: E) {
D
Daniel Micay 已提交
459
    for bound in bounds.iter() {
460
        match *bound {
461
            TraitTyParamBound(ref typ) => {
462
                walk_trait_ref(visitor, typ, env.clone())
463
            }
464
            RegionTyParamBound => {}
465
        }
466 467 468
    }
}

E
Eduard Burtescu 已提交
469 470 471
pub fn walk_generics<E: Clone, V: Visitor<E>>(visitor: &mut V,
                                              generics: &Generics,
                                              env: E) {
D
Daniel Micay 已提交
472
    for type_parameter in generics.ty_params.iter() {
473
        walk_ty_param_bounds(visitor, &type_parameter.bounds, env.clone())
474
    }
475
    walk_lifetime_decls(visitor, &generics.lifetimes, env);
476 477
}

E
Eduard Burtescu 已提交
478
pub fn walk_fn_decl<E: Clone, V: Visitor<E>>(visitor: &mut V,
479
                                             function_declaration: &FnDecl,
E
Eduard Burtescu 已提交
480
                                             env: E) {
D
Daniel Micay 已提交
481
    for argument in function_declaration.inputs.iter() {
482
        visitor.visit_pat(argument.pat, env.clone());
483
        visitor.visit_ty(argument.ty, env.clone())
484
    }
485
    visitor.visit_ty(function_declaration.output, env)
M
Marijn Haverbeke 已提交
486 487
}

488
// Note: there is no visit_method() method in the visitor, instead override
489
// visit_fn() and check for FkMethod().  I named this visit_method_helper()
490 491
// because it is not a default impl of any method, though I doubt that really
// clarifies anything. - Niko
E
Eduard Burtescu 已提交
492
pub fn walk_method_helper<E: Clone, V: Visitor<E>>(visitor: &mut V,
493
                                                   method: &Method,
E
Eduard Burtescu 已提交
494
                                                   env: E) {
495
    visitor.visit_ident(method.span, method.ident, env.clone());
496
    visitor.visit_fn(&FkMethod(method.ident, &method.generics, method),
497 498
                     method.decl,
                     method.body,
499 500 501
                     method.span,
                     method.id,
                     env)
502 503
}

E
Eduard Burtescu 已提交
504
pub fn walk_fn<E: Clone, V: Visitor<E>>(visitor: &mut V,
505 506
                                        function_kind: &FnKind,
                                        function_declaration: &FnDecl,
E
Eduard Burtescu 已提交
507 508 509 510
                                        function_body: &Block,
                                        _span: Span,
                                        _: NodeId,
                                        env: E) {
511
    walk_fn_decl(visitor, function_declaration, env.clone());
512 513

    match *function_kind {
514
        FkItemFn(_, generics, _, _) => {
515 516
            visitor.visit_generics(generics, env.clone());
        }
517
        FkMethod(_, generics, method) => {
518 519 520 521
            visitor.visit_generics(generics, env.clone());

            visitor.visit_explicit_self(&method.explicit_self, env.clone());
        }
522
        FkFnBlock(..) => {}
523 524
    }

525
    visitor.visit_block(function_body, env)
M
Marijn Haverbeke 已提交
526 527
}

E
Eduard Burtescu 已提交
528 529 530
pub fn walk_ty_method<E: Clone, V: Visitor<E>>(visitor: &mut V,
                                               method_type: &TypeMethod,
                                               env: E) {
531
    visitor.visit_ident(method_type.span, method_type.ident, env.clone());
532
    visitor.visit_explicit_self(&method_type.explicit_self, env.clone());
D
Daniel Micay 已提交
533
    for argument_type in method_type.decl.inputs.iter() {
534
        visitor.visit_ty(argument_type.ty, env.clone())
535
    }
536
    visitor.visit_generics(&method_type.generics, env.clone());
537
    visitor.visit_ty(method_type.decl.output, env);
538 539
}

E
Eduard Burtescu 已提交
540
pub fn walk_trait_method<E: Clone, V: Visitor<E>>(visitor: &mut V,
541
                                                  trait_method: &TraitMethod,
E
Eduard Burtescu 已提交
542
                                                  env: E) {
543
    match *trait_method {
544
        Required(ref method_type) => {
545 546
            visitor.visit_ty_method(method_type, env)
        }
547
        Provided(method) => walk_method_helper(visitor, method, env),
548 549 550
    }
}

E
Eduard Burtescu 已提交
551
pub fn walk_struct_def<E: Clone, V: Visitor<E>>(visitor: &mut V,
552
                                                struct_definition: &StructDef,
E
Eduard Burtescu 已提交
553 554 555 556
                                                _: Ident,
                                                _: &Generics,
                                                _: NodeId,
                                                env: E) {
D
Daniel Micay 已提交
557
    for field in struct_definition.fields.iter() {
558
        visitor.visit_struct_field(field, env.clone())
559
    }
560 561
}

E
Eduard Burtescu 已提交
562
pub fn walk_struct_field<E: Clone, V: Visitor<E>>(visitor: &mut V,
563
                                                  struct_field: &StructField,
E
Eduard Burtescu 已提交
564
                                                  env: E) {
565
    match struct_field.node.kind {
566
        NamedField(name, _) => {
567 568 569 570 571
            visitor.visit_ident(struct_field.span, name, env.clone())
        }
        _ => {}
    }

572
    visitor.visit_ty(struct_field.node.ty, env)
573 574
}

E
Eduard Burtescu 已提交
575
pub fn walk_block<E: Clone, V: Visitor<E>>(visitor: &mut V, block: &Block, env: E) {
D
Daniel Micay 已提交
576
    for view_item in block.view_items.iter() {
577
        visitor.visit_view_item(view_item, env.clone())
578
    }
D
Daniel Micay 已提交
579
    for statement in block.stmts.iter() {
580
        visitor.visit_stmt(*statement, env.clone())
581
    }
582
    walk_expr_opt(visitor, block.expr, env)
M
Marijn Haverbeke 已提交
583 584
}

E
Eduard Burtescu 已提交
585
pub fn walk_stmt<E: Clone, V: Visitor<E>>(visitor: &mut V, statement: &Stmt, env: E) {
586
    match statement.node {
587 588
        StmtDecl(declaration, _) => visitor.visit_decl(declaration, env),
        StmtExpr(expression, _) | StmtSemi(expression, _) => {
589 590
            visitor.visit_expr(expression, env)
        }
L
Léo Testard 已提交
591
        StmtMac(ref macro, _) => visitor.visit_mac(macro, env),
M
Marijn Haverbeke 已提交
592 593 594
    }
}

E
Eduard Burtescu 已提交
595
pub fn walk_decl<E: Clone, V: Visitor<E>>(visitor: &mut V, declaration: &Decl, env: E) {
596
    match declaration.node {
597 598
        DeclLocal(ref local) => visitor.visit_local(*local, env),
        DeclItem(item) => visitor.visit_item(item, env),
M
Marijn Haverbeke 已提交
599 600 601
    }
}

E
Eduard Burtescu 已提交
602 603 604
pub fn walk_expr_opt<E: Clone, V: Visitor<E>>(visitor: &mut V,
                                              optional_expression: Option<@Expr>,
                                              env: E) {
605 606 607 608
    match optional_expression {
        None => {}
        Some(expression) => visitor.visit_expr(expression, env),
    }
M
Marijn Haverbeke 已提交
609 610
}

E
Eduard Burtescu 已提交
611 612 613
pub fn walk_exprs<E: Clone, V: Visitor<E>>(visitor: &mut V,
                                           expressions: &[@Expr],
                                           env: E) {
D
Daniel Micay 已提交
614
    for expression in expressions.iter() {
615 616
        visitor.visit_expr(*expression, env.clone())
    }
M
Marijn Haverbeke 已提交
617 618
}

619
pub fn walk_mac<E, V: Visitor<E>>(_: &mut V, _: &Mac, _: E) {
620
    // Empty!
621 622
}

E
Eduard Burtescu 已提交
623
pub fn walk_expr<E: Clone, V: Visitor<E>>(visitor: &mut V, expression: &Expr, env: E) {
624
    match expression.node {
625
        ExprVstore(subexpression, _) => {
626 627
            visitor.visit_expr(subexpression, env.clone())
        }
628 629 630 631
        ExprBox(place, subexpression) => {
            visitor.visit_expr(place, env.clone());
            visitor.visit_expr(subexpression, env.clone())
        }
632
        ExprVec(ref subexpressions, _) => {
633
            walk_exprs(visitor, *subexpressions, env.clone())
634
        }
635
        ExprRepeat(element, count, _) => {
636 637
            visitor.visit_expr(element, env.clone());
            visitor.visit_expr(count, env.clone())
638
        }
639
        ExprStruct(ref path, ref fields, optional_base) => {
640
            visitor.visit_path(path, expression.id, env.clone());
D
Daniel Micay 已提交
641
            for field in fields.iter() {
642
                visitor.visit_expr(field.expr, env.clone())
643
            }
644
            walk_expr_opt(visitor, optional_base, env.clone())
645
        }
646
        ExprTup(ref subexpressions) => {
D
Daniel Micay 已提交
647
            for subexpression in subexpressions.iter() {
648 649
                visitor.visit_expr(*subexpression, env.clone())
            }
650
        }
651
        ExprCall(callee_expression, ref arguments, _) => {
D
Daniel Micay 已提交
652
            for argument in arguments.iter() {
653 654 655
                visitor.visit_expr(*argument, env.clone())
            }
            visitor.visit_expr(callee_expression, env.clone())
656
        }
657
        ExprMethodCall(_, callee, _, ref types, ref arguments, _) => {
658
            walk_exprs(visitor, *arguments, env.clone());
659
            for &typ in types.iter() {
660 661 662 663
                visitor.visit_ty(typ, env.clone())
            }
            visitor.visit_expr(callee, env.clone())
        }
664
        ExprBinary(_, _, left_expression, right_expression) => {
665 666 667
            visitor.visit_expr(left_expression, env.clone());
            visitor.visit_expr(right_expression, env.clone())
        }
668 669 670
        ExprAddrOf(_, subexpression) |
        ExprUnary(_, _, subexpression) |
        ExprDoBody(subexpression) => {
671 672
            visitor.visit_expr(subexpression, env.clone())
        }
673
        ExprLit(_) => {}
674
        ExprCast(subexpression, typ) => {
675 676 677
            visitor.visit_expr(subexpression, env.clone());
            visitor.visit_ty(typ, env.clone())
        }
678
        ExprIf(head_expression, if_block, optional_else) => {
679 680
            visitor.visit_expr(head_expression, env.clone());
            visitor.visit_block(if_block, env.clone());
681
            walk_expr_opt(visitor, optional_else, env.clone())
682
        }
683
        ExprWhile(subexpression, block) => {
684 685 686
            visitor.visit_expr(subexpression, env.clone());
            visitor.visit_block(block, env.clone())
        }
687
        ExprForLoop(pattern, subexpression, block, _) => {
688 689 690 691
            visitor.visit_pat(pattern, env.clone());
            visitor.visit_expr(subexpression, env.clone());
            visitor.visit_block(block, env.clone())
        }
692
        ExprLoop(block, _) => visitor.visit_block(block, env.clone()),
693
        ExprMatch(subexpression, ref arms) => {
694
            visitor.visit_expr(subexpression, env.clone());
D
Daniel Micay 已提交
695
            for arm in arms.iter() {
696
                visitor.visit_arm(arm, env.clone())
697
            }
698
        }
699
        ExprFnBlock(function_declaration, body) => {
700
            visitor.visit_fn(&FkFnBlock,
701 702 703 704 705 706
                             function_declaration,
                             body,
                             expression.span,
                             expression.id,
                             env.clone())
        }
707
        ExprProc(function_declaration, body) => {
708
            visitor.visit_fn(&FkFnBlock,
709 710 711 712 713 714
                             function_declaration,
                             body,
                             expression.span,
                             expression.id,
                             env.clone())
        }
715
        ExprBlock(block) => visitor.visit_block(block, env.clone()),
716
        ExprAssign(left_hand_expression, right_hand_expression) => {
717 718 719
            visitor.visit_expr(right_hand_expression, env.clone());
            visitor.visit_expr(left_hand_expression, env.clone())
        }
720
        ExprAssignOp(_, _, left_expression, right_expression) => {
721 722 723
            visitor.visit_expr(right_expression, env.clone());
            visitor.visit_expr(left_expression, env.clone())
        }
724
        ExprField(subexpression, _, ref types) => {
725
            visitor.visit_expr(subexpression, env.clone());
726
            for &typ in types.iter() {
727
                visitor.visit_ty(typ, env.clone())
728
            }
729
        }
730
        ExprIndex(_, main_expression, index_expression) => {
731 732 733
            visitor.visit_expr(main_expression, env.clone());
            visitor.visit_expr(index_expression, env.clone())
        }
734 735 736
        ExprPath(ref path) => {
            visitor.visit_path(path, expression.id, env.clone())
        }
737 738
        ExprSelf | ExprBreak(_) | ExprAgain(_) => {}
        ExprRet(optional_expression) => {
739
            walk_expr_opt(visitor, optional_expression, env.clone())
740
        }
741
        ExprLogLevel => {}
L
Léo Testard 已提交
742
        ExprMac(ref macro) => visitor.visit_mac(macro, env.clone()),
743
        ExprParen(subexpression) => {
744 745
            visitor.visit_expr(subexpression, env.clone())
        }
746
        ExprInlineAsm(ref assembler) => {
D
Daniel Micay 已提交
747
            for &(_, input) in assembler.inputs.iter() {
748
                visitor.visit_expr(input, env.clone())
749
            }
D
Daniel Micay 已提交
750
            for &(_, output) in assembler.outputs.iter() {
751
                visitor.visit_expr(output, env.clone())
752 753
            }
        }
M
Marijn Haverbeke 已提交
754
    }
755 756

    visitor.visit_expr_post(expression, env.clone())
M
Marijn Haverbeke 已提交
757 758
}

E
Eduard Burtescu 已提交
759
pub fn walk_arm<E: Clone, V: Visitor<E>>(visitor: &mut V, arm: &Arm, env: E) {
D
Daniel Micay 已提交
760
    for pattern in arm.pats.iter() {
761 762
        visitor.visit_pat(*pattern, env.clone())
    }
763
    walk_expr_opt(visitor, arm.guard, env.clone());
764
    visitor.visit_block(arm.body, env)
M
Marijn Haverbeke 已提交
765
}