]> git.lizzy.rs Git - rust.git/blob - src/libsyntax/visit.rs
return &mut T from the arenas, not &T
[rust.git] / src / libsyntax / visit.rs
1 // Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 //! AST walker. Each overridden visit method has full control over what
12 //! happens with its node, it can do its own traversal of the node's children,
13 //! call `visit::walk_*` to apply the default traversal algorithm, or prevent
14 //! deeper traversal by doing nothing.
15 //!
16 //! Note: it is an important invariant that the default visitor walks the body
17 //! of a function in "execution order" (more concretely, reverse post-order
18 //! with respect to the CFG implied by the AST), meaning that if AST node A may
19 //! execute before AST node B, then A is visited first.  The borrow checker in
20 //! particular relies on this property.
21 //!
22 //! Note: walking an AST before macro expansion is probably a bad idea. For
23 //! instance, a walker looking for item names in a module will miss all of
24 //! those that are created by the expansion of a macro.
25
26 use abi::Abi;
27 use ast::*;
28 use ast;
29 use codemap::Span;
30 use ptr::P;
31 use owned_slice::OwnedSlice;
32
33 pub enum FnKind<'a> {
34     /// fn foo() or extern "Abi" fn foo()
35     FkItemFn(Ident, &'a Generics, FnStyle, Abi),
36
37     /// fn foo(&self)
38     FkMethod(Ident, &'a Generics, &'a Method),
39
40     /// |x, y| ...
41     /// proc(x, y) ...
42     FkFnBlock,
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 /// the substructure of the input via the corresponding `walk` method;
48 /// e.g. the `visit_mod` method by default calls `visit::walk_mod`.
49 ///
50 /// If you want to ensure that your code handles every variant
51 /// explicitly, you need to override each method.  (And you also need
52 /// to monitor future changes to `Visitor` in case a new method with a
53 /// new default implementation gets introduced.)
54 pub trait Visitor<'v> {
55
56     fn visit_ident(&mut self, _sp: Span, _ident: Ident) {
57         /*! Visit the idents */
58     }
59     fn visit_mod(&mut self, m: &'v Mod, _s: Span, _n: NodeId) { walk_mod(self, m) }
60     fn visit_view_item(&mut self, i: &'v ViewItem) { walk_view_item(self, i) }
61     fn visit_foreign_item(&mut self, i: &'v ForeignItem) { walk_foreign_item(self, i) }
62     fn visit_item(&mut self, i: &'v Item) { walk_item(self, i) }
63     fn visit_local(&mut self, l: &'v Local) { walk_local(self, l) }
64     fn visit_block(&mut self, b: &'v Block) { walk_block(self, b) }
65     fn visit_stmt(&mut self, s: &'v Stmt) { walk_stmt(self, s) }
66     fn visit_arm(&mut self, a: &'v Arm) { walk_arm(self, a) }
67     fn visit_pat(&mut self, p: &'v Pat) { walk_pat(self, p) }
68     fn visit_decl(&mut self, d: &'v Decl) { walk_decl(self, d) }
69     fn visit_expr(&mut self, ex: &'v Expr) { walk_expr(self, ex) }
70     fn visit_expr_post(&mut self, _ex: &'v Expr) { }
71     fn visit_ty(&mut self, t: &'v Ty) { walk_ty(self, t) }
72     fn visit_generics(&mut self, g: &'v Generics) { walk_generics(self, g) }
73     fn visit_fn(&mut self, fk: FnKind<'v>, fd: &'v FnDecl, b: &'v Block, s: Span, _: NodeId) {
74         walk_fn(self, fk, fd, b, s)
75     }
76     fn visit_ty_method(&mut self, t: &'v TypeMethod) { walk_ty_method(self, t) }
77     fn visit_trait_item(&mut self, t: &'v TraitItem) { walk_trait_item(self, t) }
78     fn visit_struct_def(&mut self, s: &'v StructDef, _: Ident, _: &'v Generics, _: NodeId) {
79         walk_struct_def(self, s)
80     }
81     fn visit_struct_field(&mut self, s: &'v StructField) { walk_struct_field(self, s) }
82     fn visit_variant(&mut self, v: &'v Variant, g: &'v Generics) { walk_variant(self, v, g) }
83     fn visit_opt_lifetime_ref(&mut self,
84                               _span: Span,
85                               opt_lifetime: &'v Option<Lifetime>) {
86         /*!
87          * Visits an optional reference to a lifetime. The `span` is
88          * the span of some surrounding reference should opt_lifetime
89          * be None.
90          */
91         match *opt_lifetime {
92             Some(ref l) => self.visit_lifetime_ref(l),
93             None => ()
94         }
95     }
96     fn visit_lifetime_ref(&mut self, _lifetime: &'v Lifetime) {
97         /*! Visits a reference to a lifetime */
98     }
99     fn visit_lifetime_decl(&mut self, _lifetime: &'v LifetimeDef) {
100         /*! Visits a declaration of a lifetime */
101     }
102     fn visit_explicit_self(&mut self, es: &'v ExplicitSelf) {
103         walk_explicit_self(self, es)
104     }
105     fn visit_mac(&mut self, _macro: &'v Mac) {
106         fail!("visit_mac disabled by default");
107         // NB: see note about macros above.
108         // if you really want a visitor that
109         // works on macros, use this
110         // definition in your trait impl:
111         // visit::walk_mac(self, _macro)
112     }
113     fn visit_path(&mut self, path: &'v Path, _id: ast::NodeId) {
114         walk_path(self, path)
115     }
116     fn visit_attribute(&mut self, _attr: &'v Attribute) {}
117 }
118
119 pub fn walk_inlined_item<'v,V>(visitor: &mut V, item: &'v InlinedItem)
120                          where V: Visitor<'v> {
121     match *item {
122         IIItem(ref i) => visitor.visit_item(&**i),
123         IIForeign(ref i) => visitor.visit_foreign_item(&**i),
124         IITraitItem(_, ref ti) => visitor.visit_trait_item(ti),
125         IIImplItem(_, MethodImplItem(ref m)) => {
126             walk_method_helper(visitor, &**m)
127         }
128         IIImplItem(_, TypeImplItem(ref typedef)) => {
129             visitor.visit_ident(typedef.span, typedef.ident);
130             visitor.visit_ty(&*typedef.typ);
131         }
132     }
133 }
134
135
136 pub fn walk_crate<'v, V: Visitor<'v>>(visitor: &mut V, krate: &'v Crate) {
137     visitor.visit_mod(&krate.module, krate.span, CRATE_NODE_ID);
138     for attr in krate.attrs.iter() {
139         visitor.visit_attribute(attr);
140     }
141 }
142
143 pub fn walk_mod<'v, V: Visitor<'v>>(visitor: &mut V, module: &'v Mod) {
144     for view_item in module.view_items.iter() {
145         visitor.visit_view_item(view_item)
146     }
147
148     for item in module.items.iter() {
149         visitor.visit_item(&**item)
150     }
151 }
152
153 pub fn walk_view_item<'v, V: Visitor<'v>>(visitor: &mut V, vi: &'v ViewItem) {
154     match vi.node {
155         ViewItemExternCrate(name, _, _) => {
156             visitor.visit_ident(vi.span, name)
157         }
158         ViewItemUse(ref vp) => {
159             match vp.node {
160                 ViewPathSimple(ident, ref path, id) => {
161                     visitor.visit_ident(vp.span, ident);
162                     visitor.visit_path(path, id);
163                 }
164                 ViewPathGlob(ref path, id) => {
165                     visitor.visit_path(path, id);
166                 }
167                 ViewPathList(ref path, ref list, _) => {
168                     for id in list.iter() {
169                         match id.node {
170                             PathListIdent { name, .. } => {
171                                 visitor.visit_ident(id.span, name);
172                             }
173                             PathListMod { .. } => ()
174                         }
175                     }
176                     walk_path(visitor, path);
177                 }
178             }
179         }
180     }
181     for attr in vi.attrs.iter() {
182         visitor.visit_attribute(attr);
183     }
184 }
185
186 pub fn walk_local<'v, V: Visitor<'v>>(visitor: &mut V, local: &'v Local) {
187     visitor.visit_pat(&*local.pat);
188     visitor.visit_ty(&*local.ty);
189     walk_expr_opt(visitor, &local.init);
190 }
191
192 pub fn walk_explicit_self<'v, V: Visitor<'v>>(visitor: &mut V,
193                                               explicit_self: &'v ExplicitSelf) {
194     match explicit_self.node {
195         SelfStatic | SelfValue(_) => {},
196         SelfRegion(ref lifetime, _, _) => {
197             visitor.visit_opt_lifetime_ref(explicit_self.span, lifetime)
198         }
199         SelfExplicit(ref typ, _) => visitor.visit_ty(&**typ),
200     }
201 }
202
203 /// Like with walk_method_helper this doesn't correspond to a method
204 /// in Visitor, and so it gets a _helper suffix.
205 pub fn walk_trait_ref_helper<'v,V>(visitor: &mut V, trait_ref: &'v TraitRef)
206                                    where V: Visitor<'v> {
207     walk_lifetime_decls(visitor, &trait_ref.lifetimes);
208     visitor.visit_path(&trait_ref.path, trait_ref.ref_id)
209 }
210
211 pub fn walk_item<'v, V: Visitor<'v>>(visitor: &mut V, item: &'v Item) {
212     visitor.visit_ident(item.span, item.ident);
213     match item.node {
214         ItemStatic(ref typ, _, ref expr) |
215         ItemConst(ref typ, ref expr) => {
216             visitor.visit_ty(&**typ);
217             visitor.visit_expr(&**expr);
218         }
219         ItemFn(ref declaration, fn_style, abi, ref generics, ref body) => {
220             visitor.visit_fn(FkItemFn(item.ident, generics, fn_style, abi),
221                              &**declaration,
222                              &**body,
223                              item.span,
224                              item.id)
225         }
226         ItemMod(ref module) => {
227             visitor.visit_mod(module, item.span, item.id)
228         }
229         ItemForeignMod(ref foreign_module) => {
230             for view_item in foreign_module.view_items.iter() {
231                 visitor.visit_view_item(view_item)
232             }
233             for foreign_item in foreign_module.items.iter() {
234                 visitor.visit_foreign_item(&**foreign_item)
235             }
236         }
237         ItemTy(ref typ, ref type_parameters) => {
238             visitor.visit_ty(&**typ);
239             visitor.visit_generics(type_parameters)
240         }
241         ItemEnum(ref enum_definition, ref type_parameters) => {
242             visitor.visit_generics(type_parameters);
243             walk_enum_def(visitor, enum_definition, type_parameters)
244         }
245         ItemImpl(ref type_parameters,
246                  ref trait_reference,
247                  ref typ,
248                  ref impl_items) => {
249             visitor.visit_generics(type_parameters);
250             match *trait_reference {
251                 Some(ref trait_reference) => walk_trait_ref_helper(visitor,
252                                                                    trait_reference),
253                 None => ()
254             }
255             visitor.visit_ty(&**typ);
256             for impl_item in impl_items.iter() {
257                 match *impl_item {
258                     MethodImplItem(ref method) => {
259                         walk_method_helper(visitor, &**method)
260                     }
261                     TypeImplItem(ref typedef) => {
262                         visitor.visit_ident(typedef.span, typedef.ident);
263                         visitor.visit_ty(&*typedef.typ);
264                     }
265                 }
266             }
267         }
268         ItemStruct(ref struct_definition, ref generics) => {
269             visitor.visit_generics(generics);
270             visitor.visit_struct_def(&**struct_definition,
271                                      item.ident,
272                                      generics,
273                                      item.id)
274         }
275         ItemTrait(ref generics, _, ref bounds, ref methods) => {
276             visitor.visit_generics(generics);
277             walk_ty_param_bounds(visitor, bounds);
278             for method in methods.iter() {
279                 visitor.visit_trait_item(method)
280             }
281         }
282         ItemMac(ref macro) => visitor.visit_mac(macro),
283     }
284     for attr in item.attrs.iter() {
285         visitor.visit_attribute(attr);
286     }
287 }
288
289 pub fn walk_enum_def<'v, V: Visitor<'v>>(visitor: &mut V,
290                                          enum_definition: &'v EnumDef,
291                                          generics: &'v Generics) {
292     for variant in enum_definition.variants.iter() {
293         visitor.visit_variant(&**variant, generics);
294     }
295 }
296
297 pub fn walk_variant<'v, V: Visitor<'v>>(visitor: &mut V,
298                                         variant: &'v Variant,
299                                         generics: &'v Generics) {
300     visitor.visit_ident(variant.span, variant.node.name);
301
302     match variant.node.kind {
303         TupleVariantKind(ref variant_arguments) => {
304             for variant_argument in variant_arguments.iter() {
305                 visitor.visit_ty(&*variant_argument.ty)
306             }
307         }
308         StructVariantKind(ref struct_definition) => {
309             visitor.visit_struct_def(&**struct_definition,
310                                      variant.node.name,
311                                      generics,
312                                      variant.node.id)
313         }
314     }
315     match variant.node.disr_expr {
316         Some(ref expr) => visitor.visit_expr(&**expr),
317         None => ()
318     }
319     for attr in variant.node.attrs.iter() {
320         visitor.visit_attribute(attr);
321     }
322 }
323
324 pub fn skip_ty<'v, V: Visitor<'v>>(_: &mut V, _: &'v Ty) {
325     // Empty!
326 }
327
328 pub fn walk_ty<'v, V: Visitor<'v>>(visitor: &mut V, typ: &'v Ty) {
329     match typ.node {
330         TyUniq(ref ty) | TyVec(ref ty) | TyParen(ref ty) => {
331             visitor.visit_ty(&**ty)
332         }
333         TyPtr(ref mutable_type) => {
334             visitor.visit_ty(&*mutable_type.ty)
335         }
336         TyRptr(ref lifetime, ref mutable_type) => {
337             visitor.visit_opt_lifetime_ref(typ.span, lifetime);
338             visitor.visit_ty(&*mutable_type.ty)
339         }
340         TyTup(ref tuple_element_types) => {
341             for tuple_element_type in tuple_element_types.iter() {
342                 visitor.visit_ty(&**tuple_element_type)
343             }
344         }
345         TyClosure(ref function_declaration) => {
346             for argument in function_declaration.decl.inputs.iter() {
347                 visitor.visit_ty(&*argument.ty)
348             }
349             visitor.visit_ty(&*function_declaration.decl.output);
350             walk_ty_param_bounds(visitor, &function_declaration.bounds);
351             walk_lifetime_decls(visitor, &function_declaration.lifetimes);
352         }
353         TyProc(ref function_declaration) => {
354             for argument in function_declaration.decl.inputs.iter() {
355                 visitor.visit_ty(&*argument.ty)
356             }
357             visitor.visit_ty(&*function_declaration.decl.output);
358             walk_ty_param_bounds(visitor, &function_declaration.bounds);
359             walk_lifetime_decls(visitor, &function_declaration.lifetimes);
360         }
361         TyBareFn(ref function_declaration) => {
362             for argument in function_declaration.decl.inputs.iter() {
363                 visitor.visit_ty(&*argument.ty)
364             }
365             visitor.visit_ty(&*function_declaration.decl.output);
366             walk_lifetime_decls(visitor, &function_declaration.lifetimes);
367         }
368         TyUnboxedFn(ref function_declaration) => {
369             for argument in function_declaration.decl.inputs.iter() {
370                 visitor.visit_ty(&*argument.ty)
371             }
372             visitor.visit_ty(&*function_declaration.decl.output);
373         }
374         TyPath(ref path, ref opt_bounds, id) => {
375             visitor.visit_path(path, id);
376             match *opt_bounds {
377                 Some(ref bounds) => {
378                     walk_ty_param_bounds(visitor, bounds);
379                 }
380                 None => { }
381             }
382         }
383         TyQPath(ref qpath) => {
384             visitor.visit_ty(&*qpath.for_type);
385             visitor.visit_path(&qpath.trait_name, typ.id);
386             visitor.visit_ident(typ.span, qpath.item_name);
387         }
388         TyFixedLengthVec(ref ty, ref expression) => {
389             visitor.visit_ty(&**ty);
390             visitor.visit_expr(&**expression)
391         }
392         TyTypeof(ref expression) => {
393             visitor.visit_expr(&**expression)
394         }
395         TyNil | TyBot | TyInfer => {}
396     }
397 }
398
399 fn walk_lifetime_decls<'v, V: Visitor<'v>>(visitor: &mut V,
400                                            lifetimes: &'v Vec<LifetimeDef>) {
401     for l in lifetimes.iter() {
402         visitor.visit_lifetime_decl(l);
403     }
404 }
405
406 pub fn walk_path<'v, V: Visitor<'v>>(visitor: &mut V, path: &'v Path) {
407     for segment in path.segments.iter() {
408         visitor.visit_ident(path.span, segment.identifier);
409
410         for typ in segment.types.iter() {
411             visitor.visit_ty(&**typ);
412         }
413         for lifetime in segment.lifetimes.iter() {
414             visitor.visit_lifetime_ref(lifetime);
415         }
416     }
417 }
418
419 pub fn walk_pat<'v, V: Visitor<'v>>(visitor: &mut V, pattern: &'v Pat) {
420     match pattern.node {
421         PatEnum(ref path, ref children) => {
422             visitor.visit_path(path, pattern.id);
423             for children in children.iter() {
424                 for child in children.iter() {
425                     visitor.visit_pat(&**child)
426                 }
427             }
428         }
429         PatStruct(ref path, ref fields, _) => {
430             visitor.visit_path(path, pattern.id);
431             for field in fields.iter() {
432                 visitor.visit_pat(&*field.node.pat)
433             }
434         }
435         PatTup(ref tuple_elements) => {
436             for tuple_element in tuple_elements.iter() {
437                 visitor.visit_pat(&**tuple_element)
438             }
439         }
440         PatBox(ref subpattern) |
441         PatRegion(ref subpattern) => {
442             visitor.visit_pat(&**subpattern)
443         }
444         PatIdent(_, ref pth1, ref optional_subpattern) => {
445             visitor.visit_ident(pth1.span, pth1.node);
446             match *optional_subpattern {
447                 None => {}
448                 Some(ref subpattern) => visitor.visit_pat(&**subpattern),
449             }
450         }
451         PatLit(ref expression) => visitor.visit_expr(&**expression),
452         PatRange(ref lower_bound, ref upper_bound) => {
453             visitor.visit_expr(&**lower_bound);
454             visitor.visit_expr(&**upper_bound)
455         }
456         PatWild(_) => (),
457         PatVec(ref prepattern, ref slice_pattern, ref postpatterns) => {
458             for prepattern in prepattern.iter() {
459                 visitor.visit_pat(&**prepattern)
460             }
461             for slice_pattern in slice_pattern.iter() {
462                 visitor.visit_pat(&**slice_pattern)
463             }
464             for postpattern in postpatterns.iter() {
465                 visitor.visit_pat(&**postpattern)
466             }
467         }
468         PatMac(ref macro) => visitor.visit_mac(macro),
469     }
470 }
471
472 pub fn walk_foreign_item<'v, V: Visitor<'v>>(visitor: &mut V,
473                                              foreign_item: &'v ForeignItem) {
474     visitor.visit_ident(foreign_item.span, foreign_item.ident);
475
476     match foreign_item.node {
477         ForeignItemFn(ref function_declaration, ref generics) => {
478             walk_fn_decl(visitor, &**function_declaration);
479             visitor.visit_generics(generics)
480         }
481         ForeignItemStatic(ref typ, _) => visitor.visit_ty(&**typ),
482     }
483
484     for attr in foreign_item.attrs.iter() {
485         visitor.visit_attribute(attr);
486     }
487 }
488
489 pub fn walk_ty_param_bounds<'v, V: Visitor<'v>>(visitor: &mut V,
490                                                 bounds: &'v OwnedSlice<TyParamBound>) {
491     for bound in bounds.iter() {
492         match *bound {
493             TraitTyParamBound(ref typ) => {
494                 walk_trait_ref_helper(visitor, typ)
495             }
496             UnboxedFnTyParamBound(ref function_declaration) => {
497                 for argument in function_declaration.decl.inputs.iter() {
498                     visitor.visit_ty(&*argument.ty)
499                 }
500                 visitor.visit_ty(&*function_declaration.decl.output);
501                 walk_lifetime_decls(visitor, &function_declaration.lifetimes);
502             }
503             RegionTyParamBound(ref lifetime) => {
504                 visitor.visit_lifetime_ref(lifetime);
505             }
506         }
507     }
508 }
509
510 pub fn walk_generics<'v, V: Visitor<'v>>(visitor: &mut V, generics: &'v Generics) {
511     for type_parameter in generics.ty_params.iter() {
512         walk_ty_param_bounds(visitor, &type_parameter.bounds);
513         match type_parameter.default {
514             Some(ref ty) => visitor.visit_ty(&**ty),
515             None => {}
516         }
517     }
518     walk_lifetime_decls(visitor, &generics.lifetimes);
519     for predicate in generics.where_clause.predicates.iter() {
520         visitor.visit_ident(predicate.span, predicate.ident);
521         walk_ty_param_bounds(visitor, &predicate.bounds);
522     }
523 }
524
525 pub fn walk_fn_decl<'v, V: Visitor<'v>>(visitor: &mut V, function_declaration: &'v FnDecl) {
526     for argument in function_declaration.inputs.iter() {
527         visitor.visit_pat(&*argument.pat);
528         visitor.visit_ty(&*argument.ty)
529     }
530     visitor.visit_ty(&*function_declaration.output)
531 }
532
533 // Note: there is no visit_method() method in the visitor, instead override
534 // visit_fn() and check for FkMethod().  I named this visit_method_helper()
535 // because it is not a default impl of any method, though I doubt that really
536 // clarifies anything. - Niko
537 pub fn walk_method_helper<'v, V: Visitor<'v>>(visitor: &mut V, method: &'v Method) {
538     match method.node {
539         MethDecl(ident, ref generics, _, _, _, ref decl, ref body, _) => {
540             visitor.visit_ident(method.span, ident);
541             visitor.visit_fn(FkMethod(ident, generics, method),
542                              &**decl,
543                              &**body,
544                              method.span,
545                              method.id);
546             for attr in method.attrs.iter() {
547                 visitor.visit_attribute(attr);
548             }
549
550         },
551         MethMac(ref mac) => visitor.visit_mac(mac)
552     }
553 }
554
555 pub fn walk_fn<'v, V: Visitor<'v>>(visitor: &mut V,
556                                    function_kind: FnKind<'v>,
557                                    function_declaration: &'v FnDecl,
558                                    function_body: &'v Block,
559                                    _span: Span) {
560     walk_fn_decl(visitor, function_declaration);
561
562     match function_kind {
563         FkItemFn(_, generics, _, _) => {
564             visitor.visit_generics(generics);
565         }
566         FkMethod(_, generics, method) => {
567             visitor.visit_generics(generics);
568             match method.node {
569                 MethDecl(_, _, _, ref explicit_self, _, _, _, _) =>
570                     visitor.visit_explicit_self(explicit_self),
571                 MethMac(ref mac) =>
572                     visitor.visit_mac(mac)
573             }
574         }
575         FkFnBlock(..) => {}
576     }
577
578     visitor.visit_block(function_body)
579 }
580
581 pub fn walk_ty_method<'v, V: Visitor<'v>>(visitor: &mut V, method_type: &'v TypeMethod) {
582     visitor.visit_ident(method_type.span, method_type.ident);
583     visitor.visit_explicit_self(&method_type.explicit_self);
584     for argument_type in method_type.decl.inputs.iter() {
585         visitor.visit_ty(&*argument_type.ty)
586     }
587     visitor.visit_generics(&method_type.generics);
588     visitor.visit_ty(&*method_type.decl.output);
589     for attr in method_type.attrs.iter() {
590         visitor.visit_attribute(attr);
591     }
592 }
593
594 pub fn walk_trait_item<'v, V: Visitor<'v>>(visitor: &mut V, trait_method: &'v TraitItem) {
595     match *trait_method {
596         RequiredMethod(ref method_type) => visitor.visit_ty_method(method_type),
597         ProvidedMethod(ref method) => walk_method_helper(visitor, &**method),
598         TypeTraitItem(ref associated_type) => {
599             visitor.visit_ident(associated_type.span, associated_type.ident)
600         }
601     }
602 }
603
604 pub fn walk_struct_def<'v, V: Visitor<'v>>(visitor: &mut V,
605                                            struct_definition: &'v StructDef) {
606     for field in struct_definition.fields.iter() {
607         visitor.visit_struct_field(field)
608     }
609 }
610
611 pub fn walk_struct_field<'v, V: Visitor<'v>>(visitor: &mut V,
612                                              struct_field: &'v StructField) {
613     match struct_field.node.kind {
614         NamedField(name, _) => {
615             visitor.visit_ident(struct_field.span, name)
616         }
617         _ => {}
618     }
619
620     visitor.visit_ty(&*struct_field.node.ty);
621
622     for attr in struct_field.node.attrs.iter() {
623         visitor.visit_attribute(attr);
624     }
625 }
626
627 pub fn walk_block<'v, V: Visitor<'v>>(visitor: &mut V, block: &'v Block) {
628     for view_item in block.view_items.iter() {
629         visitor.visit_view_item(view_item)
630     }
631     for statement in block.stmts.iter() {
632         visitor.visit_stmt(&**statement)
633     }
634     walk_expr_opt(visitor, &block.expr)
635 }
636
637 pub fn walk_stmt<'v, V: Visitor<'v>>(visitor: &mut V, statement: &'v Stmt) {
638     match statement.node {
639         StmtDecl(ref declaration, _) => visitor.visit_decl(&**declaration),
640         StmtExpr(ref expression, _) | StmtSemi(ref expression, _) => {
641             visitor.visit_expr(&**expression)
642         }
643         StmtMac(ref macro, _) => visitor.visit_mac(macro),
644     }
645 }
646
647 pub fn walk_decl<'v, V: Visitor<'v>>(visitor: &mut V, declaration: &'v Decl) {
648     match declaration.node {
649         DeclLocal(ref local) => visitor.visit_local(&**local),
650         DeclItem(ref item) => visitor.visit_item(&**item),
651     }
652 }
653
654 pub fn walk_expr_opt<'v, V: Visitor<'v>>(visitor: &mut V,
655                                          optional_expression: &'v Option<P<Expr>>) {
656     match *optional_expression {
657         None => {}
658         Some(ref expression) => visitor.visit_expr(&**expression),
659     }
660 }
661
662 pub fn walk_exprs<'v, V: Visitor<'v>>(visitor: &mut V, expressions: &'v [P<Expr>]) {
663     for expression in expressions.iter() {
664         visitor.visit_expr(&**expression)
665     }
666 }
667
668 pub fn walk_mac<'v, V: Visitor<'v>>(_: &mut V, _: &'v Mac) {
669     // Empty!
670 }
671
672 pub fn walk_expr<'v, V: Visitor<'v>>(visitor: &mut V, expression: &'v Expr) {
673     match expression.node {
674         ExprBox(ref place, ref subexpression) => {
675             visitor.visit_expr(&**place);
676             visitor.visit_expr(&**subexpression)
677         }
678         ExprVec(ref subexpressions) => {
679             walk_exprs(visitor, subexpressions.as_slice())
680         }
681         ExprRepeat(ref element, ref count) => {
682             visitor.visit_expr(&**element);
683             visitor.visit_expr(&**count)
684         }
685         ExprStruct(ref path, ref fields, ref optional_base) => {
686             visitor.visit_path(path, expression.id);
687             for field in fields.iter() {
688                 visitor.visit_expr(&*field.expr)
689             }
690             walk_expr_opt(visitor, optional_base)
691         }
692         ExprTup(ref subexpressions) => {
693             for subexpression in subexpressions.iter() {
694                 visitor.visit_expr(&**subexpression)
695             }
696         }
697         ExprCall(ref callee_expression, ref arguments) => {
698             for argument in arguments.iter() {
699                 visitor.visit_expr(&**argument)
700             }
701             visitor.visit_expr(&**callee_expression)
702         }
703         ExprMethodCall(_, ref types, ref arguments) => {
704             walk_exprs(visitor, arguments.as_slice());
705             for typ in types.iter() {
706                 visitor.visit_ty(&**typ)
707             }
708         }
709         ExprBinary(_, ref left_expression, ref right_expression) => {
710             visitor.visit_expr(&**left_expression);
711             visitor.visit_expr(&**right_expression)
712         }
713         ExprAddrOf(_, ref subexpression) | ExprUnary(_, ref subexpression) => {
714             visitor.visit_expr(&**subexpression)
715         }
716         ExprLit(_) => {}
717         ExprCast(ref subexpression, ref typ) => {
718             visitor.visit_expr(&**subexpression);
719             visitor.visit_ty(&**typ)
720         }
721         ExprIf(ref head_expression, ref if_block, ref optional_else) => {
722             visitor.visit_expr(&**head_expression);
723             visitor.visit_block(&**if_block);
724             walk_expr_opt(visitor, optional_else)
725         }
726         ExprWhile(ref subexpression, ref block, _) => {
727             visitor.visit_expr(&**subexpression);
728             visitor.visit_block(&**block)
729         }
730         ExprIfLet(ref pattern, ref subexpression, ref if_block, ref optional_else) => {
731             visitor.visit_pat(&**pattern);
732             visitor.visit_expr(&**subexpression);
733             visitor.visit_block(&**if_block);
734             walk_expr_opt(visitor, optional_else);
735         }
736         ExprWhileLet(ref pattern, ref subexpression, ref block, _) => {
737             visitor.visit_pat(&**pattern);
738             visitor.visit_expr(&**subexpression);
739             visitor.visit_block(&**block);
740         }
741         ExprForLoop(ref pattern, ref subexpression, ref block, _) => {
742             visitor.visit_pat(&**pattern);
743             visitor.visit_expr(&**subexpression);
744             visitor.visit_block(&**block)
745         }
746         ExprLoop(ref block, _) => visitor.visit_block(&**block),
747         ExprMatch(ref subexpression, ref arms, _) => {
748             visitor.visit_expr(&**subexpression);
749             for arm in arms.iter() {
750                 visitor.visit_arm(arm)
751             }
752         }
753         ExprFnBlock(_, ref function_declaration, ref body) => {
754             visitor.visit_fn(FkFnBlock,
755                              &**function_declaration,
756                              &**body,
757                              expression.span,
758                              expression.id)
759         }
760         ExprUnboxedFn(_, _, ref function_declaration, ref body) => {
761             visitor.visit_fn(FkFnBlock,
762                              &**function_declaration,
763                              &**body,
764                              expression.span,
765                              expression.id)
766         }
767         ExprProc(ref function_declaration, ref body) => {
768             visitor.visit_fn(FkFnBlock,
769                              &**function_declaration,
770                              &**body,
771                              expression.span,
772                              expression.id)
773         }
774         ExprBlock(ref block) => visitor.visit_block(&**block),
775         ExprAssign(ref left_hand_expression, ref right_hand_expression) => {
776             visitor.visit_expr(&**right_hand_expression);
777             visitor.visit_expr(&**left_hand_expression)
778         }
779         ExprAssignOp(_, ref left_expression, ref right_expression) => {
780             visitor.visit_expr(&**right_expression);
781             visitor.visit_expr(&**left_expression)
782         }
783         ExprField(ref subexpression, _, ref types) => {
784             visitor.visit_expr(&**subexpression);
785             for typ in types.iter() {
786                 visitor.visit_ty(&**typ)
787             }
788         }
789         ExprTupField(ref subexpression, _, ref types) => {
790             visitor.visit_expr(&**subexpression);
791             for typ in types.iter() {
792                 visitor.visit_ty(&**typ)
793             }
794         }
795         ExprIndex(ref main_expression, ref index_expression) => {
796             visitor.visit_expr(&**main_expression);
797             visitor.visit_expr(&**index_expression)
798         }
799         ExprSlice(ref main_expression, ref start, ref end, _) => {
800             visitor.visit_expr(&**main_expression);
801             walk_expr_opt(visitor, start);
802             walk_expr_opt(visitor, end)
803         }
804         ExprPath(ref path) => {
805             visitor.visit_path(path, expression.id)
806         }
807         ExprBreak(_) | ExprAgain(_) => {}
808         ExprRet(ref optional_expression) => {
809             walk_expr_opt(visitor, optional_expression)
810         }
811         ExprMac(ref macro) => visitor.visit_mac(macro),
812         ExprParen(ref subexpression) => {
813             visitor.visit_expr(&**subexpression)
814         }
815         ExprInlineAsm(ref ia) => {
816             for input in ia.inputs.iter() {
817                 let (_, ref input) = *input;
818                 visitor.visit_expr(&**input)
819             }
820             for output in ia.outputs.iter() {
821                 let (_, ref output, _) = *output;
822                 visitor.visit_expr(&**output)
823             }
824         }
825     }
826
827     visitor.visit_expr_post(expression)
828 }
829
830 pub fn walk_arm<'v, V: Visitor<'v>>(visitor: &mut V, arm: &'v Arm) {
831     for pattern in arm.pats.iter() {
832         visitor.visit_pat(&**pattern)
833     }
834     walk_expr_opt(visitor, &arm.guard);
835     visitor.visit_expr(&*arm.body);
836     for attr in arm.attrs.iter() {
837         visitor.visit_attribute(attr);
838     }
839 }