(index<- ) ./libsyntax/visit.rs
git branch: * master 5200215 auto merge of #14035 : alexcrichton/rust/experimental, r=huonw
modified: Fri May 9 13:02:28 2014
1 // Copyright 2012-2013 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 use abi::Abi;
12 use ast::*;
13 use ast;
14 use codemap::Span;
15 use parse;
16 use owned_slice::OwnedSlice;
17
18 // Context-passing AST walker. Each overridden visit method has full control
19 // over what happens with its node, it can do its own traversal of the node's
20 // children (potentially passing in different contexts to each), call
21 // visit::visit_* to apply the default traversal algorithm (again, it can
22 // override the context), or prevent deeper traversal by doing nothing.
23 //
24 // Note: it is an important invariant that the default visitor walks the body
25 // of a function in "execution order" (more concretely, reverse post-order
26 // with respect to the CFG implied by the AST), meaning that if AST node A may
27 // execute before AST node B, then A is visited first. The borrow checker in
28 // particular relies on this property.
29
30 pub enum FnKind<'a> {
31 // fn foo() or extern "Abi" fn foo()
32 FkItemFn(Ident, &'a Generics, FnStyle, Abi),
33
34 // fn foo(&self)
35 FkMethod(Ident, &'a Generics, &'a Method),
36
37 // |x, y| ...
38 // proc(x, y) ...
39 FkFnBlock,
40 }
41
42 pub fn name_of_fn(fk: &FnKind) -> Ident {
43 match *fk {
44 FkItemFn(name, _, _, _) | FkMethod(name, _, _) => name,
45 FkFnBlock(..) => parse::token::special_idents::invalid
46 }
47 }
48
49 pub fn generics_of_fn(fk: &FnKind) -> Generics {
50 match *fk {
51 FkItemFn(_, generics, _, _) |
52 FkMethod(_, generics, _) => {
53 (*generics).clone()
54 }
55 FkFnBlock(..) => {
56 Generics {
57 lifetimes: Vec::new(),
58 ty_params: OwnedSlice::empty(),
59 }
60 }
61 }
62 }
63
64 pub trait Visitor<E: Clone> {
65 fn visit_ident(&mut self, _sp: Span, _ident: Ident, _e: E) {
66 /*! Visit the idents */
67 }
68 fn visit_mod(&mut self, m: &Mod, _s: Span, _n: NodeId, e: E) { walk_mod(self, m, e) }
69 fn visit_view_item(&mut self, i: &ViewItem, e: E) { walk_view_item(self, i, e) }
70 fn visit_foreign_item(&mut self, i: &ForeignItem, e: E) { walk_foreign_item(self, i, e) }
71 fn visit_item(&mut self, i: &Item, e: E) { walk_item(self, i, e) }
72 fn visit_local(&mut self, l: &Local, e: E) { walk_local(self, l, e) }
73 fn visit_block(&mut self, b: &Block, e: E) { walk_block(self, b, e) }
74 fn visit_stmt(&mut self, s: &Stmt, e: E) { walk_stmt(self, s, e) }
75 fn visit_arm(&mut self, a: &Arm, e: E) { walk_arm(self, a, e) }
76 fn visit_pat(&mut self, p: &Pat, e: E) { walk_pat(self, p, e) }
77 fn visit_decl(&mut self, d: &Decl, e: E) { walk_decl(self, d, e) }
78 fn visit_expr(&mut self, ex: &Expr, e: E) { walk_expr(self, ex, e) }
79 fn visit_expr_post(&mut self, _ex: &Expr, _e: E) { }
80 fn visit_ty(&mut self, t: &Ty, e: E) { walk_ty(self, t, e) }
81 fn visit_generics(&mut self, g: &Generics, e: E) { walk_generics(self, g, e) }
82 fn visit_fn(&mut self, fk: &FnKind, fd: &FnDecl, b: &Block, s: Span, n: NodeId, e: E) {
83 walk_fn(self, fk, fd, b, s, n , e)
84 }
85 fn visit_ty_method(&mut self, t: &TypeMethod, e: E) { walk_ty_method(self, t, e) }
86 fn visit_trait_method(&mut self, t: &TraitMethod, e: E) { walk_trait_method(self, t, e) }
87 fn visit_struct_def(&mut self, s: &StructDef, i: Ident, g: &Generics, n: NodeId, e: E) {
88 walk_struct_def(self, s, i, g, n, e)
89 }
90 fn visit_struct_field(&mut self, s: &StructField, e: E) { walk_struct_field(self, s, e) }
91 fn visit_variant(&mut self, v: &Variant, g: &Generics, e: E) { walk_variant(self, v, g, e) }
92 fn visit_opt_lifetime_ref(&mut self,
93 _span: Span,
94 opt_lifetime: &Option<Lifetime>,
95 env: E) {
96 /*!
97 * Visits an optional reference to a lifetime. The `span` is
98 * the span of some surrounding reference should opt_lifetime
99 * be None.
100 */
101 match *opt_lifetime {
102 Some(ref l) => self.visit_lifetime_ref(l, env),
103 None => ()
104 }
105 }
106 fn visit_lifetime_ref(&mut self, _lifetime: &Lifetime, _e: E) {
107 /*! Visits a reference to a lifetime */
108 }
109 fn visit_lifetime_decl(&mut self, _lifetime: &Lifetime, _e: E) {
110 /*! Visits a declaration of a lifetime */
111 }
112 fn visit_explicit_self(&mut self, es: &ExplicitSelf, e: E) {
113 walk_explicit_self(self, es, e)
114 }
115 fn visit_mac(&mut self, macro: &Mac, e: E) {
116 walk_mac(self, macro, e)
117 }
118 fn visit_path(&mut self, path: &Path, _id: ast::NodeId, e: E) {
119 walk_path(self, path, e)
120 }
121 }
122
123 pub fn walk_inlined_item<E: Clone, V: Visitor<E>>(visitor: &mut V,
124 item: &ast::InlinedItem,
125 env: E) {
126 match *item {
127 IIItem(i) => visitor.visit_item(i, env),
128 IIForeign(i) => visitor.visit_foreign_item(i, env),
129 IIMethod(_, _, m) => walk_method_helper(visitor, m, env),
130 }
131 }
132
133
134 pub fn walk_crate<E: Clone, V: Visitor<E>>(visitor: &mut V, krate: &Crate, env: E) {
135 visitor.visit_mod(&krate.module, krate.span, CRATE_NODE_ID, env)
136 }
137
138 pub fn walk_mod<E: Clone, V: Visitor<E>>(visitor: &mut V, module: &Mod, env: E) {
139 for view_item in module.view_items.iter() {
140 visitor.visit_view_item(view_item, env.clone())
141 }
142
143 for item in module.items.iter() {
144 visitor.visit_item(*item, env.clone())
145 }
146 }
147
148 pub fn walk_view_item<E: Clone, V: Visitor<E>>(visitor: &mut V, vi: &ViewItem, env: E) {
149 match vi.node {
150 ViewItemExternCrate(name, _, _) => {
151 visitor.visit_ident(vi.span, name, env)
152 }
153 ViewItemUse(ref vp) => {
154 match vp.node {
155 ViewPathSimple(ident, ref path, id) => {
156 visitor.visit_ident(vp.span, ident, env.clone());
157 visitor.visit_path(path, id, env.clone());
158 }
159 ViewPathGlob(ref path, id) => {
160 visitor.visit_path(path, id, env.clone());
161 }
162 ViewPathList(ref path, ref list, _) => {
163 for id in list.iter() {
164 visitor.visit_ident(id.span, id.node.name, env.clone())
165 }
166 walk_path(visitor, path, env.clone());
167 }
168 }
169 }
170 }
171 }
172
173 pub fn walk_local<E: Clone, V: Visitor<E>>(visitor: &mut V, local: &Local, env: E) {
174 visitor.visit_pat(local.pat, env.clone());
175 visitor.visit_ty(local.ty, env.clone());
176 match local.init {
177 None => {}
178 Some(initializer) => visitor.visit_expr(initializer, env),
179 }
180 }
181
182 fn walk_explicit_self<E: Clone, V: Visitor<E>>(visitor: &mut V,
183 explicit_self: &ExplicitSelf,
184 env: E) {
185 match explicit_self.node {
186 SelfStatic | SelfValue | SelfUniq => {}
187 SelfRegion(ref lifetime, _) => {
188 visitor.visit_opt_lifetime_ref(explicit_self.span, lifetime, env)
189 }
190 }
191 }
192
193 /// Like with walk_method_helper this doesn't correspond to a method
194 /// in Visitor, and so it gets a _helper suffix.
195 pub fn walk_trait_ref_helper<E: Clone, V: Visitor<E>>(visitor: &mut V,
196 trait_ref: &TraitRef,
197 env: E) {
198 visitor.visit_path(&trait_ref.path, trait_ref.ref_id, env)
199 }
200
201 pub fn walk_item<E: Clone, V: Visitor<E>>(visitor: &mut V, item: &Item, env: E) {
202 visitor.visit_ident(item.span, item.ident, env.clone());
203 match item.node {
204 ItemStatic(typ, _, expr) => {
205 visitor.visit_ty(typ, env.clone());
206 visitor.visit_expr(expr, env);
207 }
208 ItemFn(declaration, fn_style, abi, ref generics, body) => {
209 visitor.visit_fn(&FkItemFn(item.ident, generics, fn_style, abi),
210 declaration,
211 body,
212 item.span,
213 item.id,
214 env)
215 }
216 ItemMod(ref module) => {
217 visitor.visit_mod(module, item.span, item.id, env)
218 }
219 ItemForeignMod(ref foreign_module) => {
220 for view_item in foreign_module.view_items.iter() {
221 visitor.visit_view_item(view_item, env.clone())
222 }
223 for foreign_item in foreign_module.items.iter() {
224 visitor.visit_foreign_item(*foreign_item, env.clone())
225 }
226 }
227 ItemTy(typ, ref type_parameters) => {
228 visitor.visit_ty(typ, env.clone());
229 visitor.visit_generics(type_parameters, env)
230 }
231 ItemEnum(ref enum_definition, ref type_parameters) => {
232 visitor.visit_generics(type_parameters, env.clone());
233 walk_enum_def(visitor, enum_definition, type_parameters, env)
234 }
235 ItemImpl(ref type_parameters,
236 ref trait_reference,
237 typ,
238 ref methods) => {
239 visitor.visit_generics(type_parameters, env.clone());
240 match *trait_reference {
241 Some(ref trait_reference) => walk_trait_ref_helper(visitor,
242 trait_reference, env.clone()),
243 None => ()
244 }
245 visitor.visit_ty(typ, env.clone());
246 for method in methods.iter() {
247 walk_method_helper(visitor, *method, env.clone())
248 }
249 }
250 ItemStruct(struct_definition, ref generics) => {
251 visitor.visit_generics(generics, env.clone());
252 visitor.visit_struct_def(struct_definition,
253 item.ident,
254 generics,
255 item.id,
256 env)
257 }
258 ItemTrait(ref generics, _, ref trait_paths, ref methods) => {
259 visitor.visit_generics(generics, env.clone());
260 for trait_path in trait_paths.iter() {
261 visitor.visit_path(&trait_path.path,
262 trait_path.ref_id,
263 env.clone())
264 }
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),
270 }
271 }
272
273 pub fn walk_enum_def<E: Clone, V:Visitor<E>>(visitor: &mut V,
274 enum_definition: &EnumDef,
275 generics: &Generics,
276 env: E) {
277 for &variant in enum_definition.variants.iter() {
278 visitor.visit_variant(variant, generics, env.clone());
279 }
280 }
281
282 pub fn walk_variant<E: Clone, V: Visitor<E>>(visitor: &mut V,
283 variant: &Variant,
284 generics: &Generics,
285 env: E) {
286 visitor.visit_ident(variant.span, variant.node.name, env.clone());
287
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 visitor.visit_struct_def(struct_definition,
296 variant.node.name,
297 generics,
298 variant.node.id,
299 env.clone())
300 }
301 }
302 match variant.node.disr_expr {
303 Some(expr) => visitor.visit_expr(expr, env),
304 None => ()
305 }
306 }
307
308 pub fn skip_ty<E, V: Visitor<E>>(_: &mut V, _: &Ty, _: E) {
309 // Empty!
310 }
311
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 visitor.visit_ty(ty, env)
316 }
317 TyPtr(ref mutable_type) => {
318 visitor.visit_ty(mutable_type.ty, env)
319 }
320 TyRptr(ref lifetime, ref mutable_type) => {
321 visitor.visit_opt_lifetime_ref(typ.span, lifetime, env.clone());
322 visitor.visit_ty(mutable_type.ty, env)
323 }
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, ref region) => {
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 visitor.visit_opt_lifetime_ref(
338 typ.span,
339 region,
340 env.clone());
341 walk_lifetime_decls(visitor, &function_declaration.lifetimes,
342 env.clone());
343 }
344 TyProc(ref function_declaration) => {
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 for bounds in function_declaration.bounds.iter() {
350 walk_ty_param_bounds(visitor, bounds, env.clone())
351 }
352 walk_lifetime_decls(visitor, &function_declaration.lifetimes,
353 env.clone());
354 }
355 TyBareFn(ref function_declaration) => {
356 for argument in function_declaration.decl.inputs.iter() {
357 visitor.visit_ty(argument.ty, env.clone())
358 }
359 visitor.visit_ty(function_declaration.decl.output, env.clone());
360 walk_lifetime_decls(visitor, &function_declaration.lifetimes,
361 env.clone());
362 }
363 TyPath(ref path, ref bounds, id) => {
364 visitor.visit_path(path, id, env.clone());
365 for bounds in bounds.iter() {
366 walk_ty_param_bounds(visitor, bounds, env.clone())
367 }
368 }
369 TyFixedLengthVec(ty, expression) => {
370 visitor.visit_ty(ty, env.clone());
371 visitor.visit_expr(expression, env)
372 }
373 TyTypeof(expression) => {
374 visitor.visit_expr(expression, env)
375 }
376 TyNil | TyBot | TyInfer => {}
377 }
378 }
379
380 fn walk_lifetime_decls<E: Clone, V: Visitor<E>>(visitor: &mut V,
381 lifetimes: &Vec<Lifetime>,
382 env: E) {
383 for l in lifetimes.iter() {
384 visitor.visit_lifetime_decl(l, env.clone());
385 }
386 }
387
388 pub fn walk_path<E: Clone, V: Visitor<E>>(visitor: &mut V, path: &Path, env: E) {
389 for segment in path.segments.iter() {
390 visitor.visit_ident(path.span, segment.identifier, env.clone());
391
392 for &typ in segment.types.iter() {
393 visitor.visit_ty(typ, env.clone());
394 }
395 for lifetime in segment.lifetimes.iter() {
396 visitor.visit_lifetime_ref(lifetime, env.clone());
397 }
398 }
399 }
400
401 pub fn walk_pat<E: Clone, V: Visitor<E>>(visitor: &mut V, pattern: &Pat, env: E) {
402 match pattern.node {
403 PatEnum(ref path, ref children) => {
404 visitor.visit_path(path, pattern.id, env.clone());
405 for children in children.iter() {
406 for child in children.iter() {
407 visitor.visit_pat(*child, env.clone())
408 }
409 }
410 }
411 PatStruct(ref path, ref fields, _) => {
412 visitor.visit_path(path, pattern.id, env.clone());
413 for field in fields.iter() {
414 visitor.visit_pat(field.pat, env.clone())
415 }
416 }
417 PatTup(ref tuple_elements) => {
418 for tuple_element in tuple_elements.iter() {
419 visitor.visit_pat(*tuple_element, env.clone())
420 }
421 }
422 PatUniq(subpattern) |
423 PatRegion(subpattern) => {
424 visitor.visit_pat(subpattern, env)
425 }
426 PatIdent(_, ref path, ref optional_subpattern) => {
427 visitor.visit_path(path, pattern.id, env.clone());
428 match *optional_subpattern {
429 None => {}
430 Some(subpattern) => visitor.visit_pat(subpattern, env),
431 }
432 }
433 PatLit(expression) => visitor.visit_expr(expression, env),
434 PatRange(lower_bound, upper_bound) => {
435 visitor.visit_expr(lower_bound, env.clone());
436 visitor.visit_expr(upper_bound, env)
437 }
438 PatWild | PatWildMulti => (),
439 PatVec(ref prepattern, ref slice_pattern, ref postpatterns) => {
440 for prepattern in prepattern.iter() {
441 visitor.visit_pat(*prepattern, env.clone())
442 }
443 for slice_pattern in slice_pattern.iter() {
444 visitor.visit_pat(*slice_pattern, env.clone())
445 }
446 for postpattern in postpatterns.iter() {
447 visitor.visit_pat(*postpattern, env.clone())
448 }
449 }
450 }
451 }
452
453 pub fn walk_foreign_item<E: Clone, V: Visitor<E>>(visitor: &mut V,
454 foreign_item: &ForeignItem,
455 env: E) {
456 visitor.visit_ident(foreign_item.span, foreign_item.ident, env.clone());
457
458 match foreign_item.node {
459 ForeignItemFn(function_declaration, ref generics) => {
460 walk_fn_decl(visitor, function_declaration, env.clone());
461 visitor.visit_generics(generics, env)
462 }
463 ForeignItemStatic(typ, _) => visitor.visit_ty(typ, env),
464 }
465 }
466
467 pub fn walk_ty_param_bounds<E: Clone, V: Visitor<E>>(visitor: &mut V,
468 bounds: &OwnedSlice<TyParamBound>,
469 env: E) {
470 for bound in bounds.iter() {
471 match *bound {
472 TraitTyParamBound(ref typ) => {
473 walk_trait_ref_helper(visitor, typ, env.clone())
474 }
475 StaticRegionTyParamBound => {}
476 OtherRegionTyParamBound(..) => {}
477 }
478 }
479 }
480
481 pub fn walk_generics<E: Clone, V: Visitor<E>>(visitor: &mut V,
482 generics: &Generics,
483 env: E) {
484 for type_parameter in generics.ty_params.iter() {
485 walk_ty_param_bounds(visitor, &type_parameter.bounds, env.clone());
486 match type_parameter.default {
487 Some(ty) => visitor.visit_ty(ty, env.clone()),
488 None => {}
489 }
490 }
491 walk_lifetime_decls(visitor, &generics.lifetimes, env);
492 }
493
494 pub fn walk_fn_decl<E: Clone, V: Visitor<E>>(visitor: &mut V,
495 function_declaration: &FnDecl,
496 env: E) {
497 for argument in function_declaration.inputs.iter() {
498 visitor.visit_pat(argument.pat, env.clone());
499 visitor.visit_ty(argument.ty, env.clone())
500 }
501 visitor.visit_ty(function_declaration.output, env)
502 }
503
504 // Note: there is no visit_method() method in the visitor, instead override
505 // visit_fn() and check for FkMethod(). I named this visit_method_helper()
506 // because it is not a default impl of any method, though I doubt that really
507 // clarifies anything. - Niko
508 pub fn walk_method_helper<E: Clone, V: Visitor<E>>(visitor: &mut V,
509 method: &Method,
510 env: E) {
511 visitor.visit_ident(method.span, method.ident, env.clone());
512 visitor.visit_fn(&FkMethod(method.ident, &method.generics, method),
513 method.decl,
514 method.body,
515 method.span,
516 method.id,
517 env)
518 }
519
520 pub fn walk_fn<E: Clone, V: Visitor<E>>(visitor: &mut V,
521 function_kind: &FnKind,
522 function_declaration: &FnDecl,
523 function_body: &Block,
524 _span: Span,
525 _: NodeId,
526 env: E) {
527 walk_fn_decl(visitor, function_declaration, env.clone());
528
529 match *function_kind {
530 FkItemFn(_, generics, _, _) => {
531 visitor.visit_generics(generics, env.clone());
532 }
533 FkMethod(_, generics, method) => {
534 visitor.visit_generics(generics, env.clone());
535
536 visitor.visit_explicit_self(&method.explicit_self, env.clone());
537 }
538 FkFnBlock(..) => {}
539 }
540
541 visitor.visit_block(function_body, env)
542 }
543
544 pub fn walk_ty_method<E: Clone, V: Visitor<E>>(visitor: &mut V,
545 method_type: &TypeMethod,
546 env: E) {
547 visitor.visit_ident(method_type.span, method_type.ident, env.clone());
548 visitor.visit_explicit_self(&method_type.explicit_self, env.clone());
549 for argument_type in method_type.decl.inputs.iter() {
550 visitor.visit_ty(argument_type.ty, env.clone())
551 }
552 visitor.visit_generics(&method_type.generics, env.clone());
553 visitor.visit_ty(method_type.decl.output, env);
554 }
555
556 pub fn walk_trait_method<E: Clone, V: Visitor<E>>(visitor: &mut V,
557 trait_method: &TraitMethod,
558 env: E) {
559 match *trait_method {
560 Required(ref method_type) => {
561 visitor.visit_ty_method(method_type, env)
562 }
563 Provided(method) => walk_method_helper(visitor, method, env),
564 }
565 }
566
567 pub fn walk_struct_def<E: Clone, V: Visitor<E>>(visitor: &mut V,
568 struct_definition: &StructDef,
569 _: Ident,
570 _: &Generics,
571 _: NodeId,
572 env: E) {
573 match struct_definition.super_struct {
574 Some(t) => visitor.visit_ty(t, env.clone()),
575 None => {},
576 }
577 for field in struct_definition.fields.iter() {
578 visitor.visit_struct_field(field, env.clone())
579 }
580 }
581
582 pub fn walk_struct_field<E: Clone, V: Visitor<E>>(visitor: &mut V,
583 struct_field: &StructField,
584 env: E) {
585 match struct_field.node.kind {
586 NamedField(name, _) => {
587 visitor.visit_ident(struct_field.span, name, env.clone())
588 }
589 _ => {}
590 }
591
592 visitor.visit_ty(struct_field.node.ty, env)
593 }
594
595 pub fn walk_block<E: Clone, V: Visitor<E>>(visitor: &mut V, block: &Block, env: E) {
596 for view_item in block.view_items.iter() {
597 visitor.visit_view_item(view_item, env.clone())
598 }
599 for statement in block.stmts.iter() {
600 visitor.visit_stmt(*statement, env.clone())
601 }
602 walk_expr_opt(visitor, block.expr, env)
603 }
604
605 pub fn walk_stmt<E: Clone, V: Visitor<E>>(visitor: &mut V, statement: &Stmt, env: E) {
606 match statement.node {
607 StmtDecl(declaration, _) => visitor.visit_decl(declaration, env),
608 StmtExpr(expression, _) | StmtSemi(expression, _) => {
609 visitor.visit_expr(expression, env)
610 }
611 StmtMac(ref macro, _) => visitor.visit_mac(macro, env),
612 }
613 }
614
615 pub fn walk_decl<E: Clone, V: Visitor<E>>(visitor: &mut V, declaration: &Decl, env: E) {
616 match declaration.node {
617 DeclLocal(ref local) => visitor.visit_local(*local, env),
618 DeclItem(item) => visitor.visit_item(item, env),
619 }
620 }
621
622 pub fn walk_expr_opt<E: Clone, V: Visitor<E>>(visitor: &mut V,
623 optional_expression: Option<@Expr>,
624 env: E) {
625 match optional_expression {
626 None => {}
627 Some(expression) => visitor.visit_expr(expression, env),
628 }
629 }
630
631 pub fn walk_exprs<E: Clone, V: Visitor<E>>(visitor: &mut V,
632 expressions: &[@Expr],
633 env: E) {
634 for expression in expressions.iter() {
635 visitor.visit_expr(*expression, env.clone())
636 }
637 }
638
639 pub fn walk_mac<E, V: Visitor<E>>(_: &mut V, _: &Mac, _: E) {
640 // Empty!
641 }
642
643 pub fn walk_expr<E: Clone, V: Visitor<E>>(visitor: &mut V, expression: &Expr, env: E) {
644 match expression.node {
645 ExprVstore(subexpression, _) => {
646 visitor.visit_expr(subexpression, env.clone())
647 }
648 ExprBox(place, subexpression) => {
649 visitor.visit_expr(place, env.clone());
650 visitor.visit_expr(subexpression, env.clone())
651 }
652 ExprVec(ref subexpressions) => {
653 walk_exprs(visitor, subexpressions.as_slice(), env.clone())
654 }
655 ExprRepeat(element, count) => {
656 visitor.visit_expr(element, env.clone());
657 visitor.visit_expr(count, env.clone())
658 }
659 ExprStruct(ref path, ref fields, optional_base) => {
660 visitor.visit_path(path, expression.id, env.clone());
661 for field in fields.iter() {
662 visitor.visit_expr(field.expr, env.clone())
663 }
664 walk_expr_opt(visitor, optional_base, env.clone())
665 }
666 ExprTup(ref subexpressions) => {
667 for subexpression in subexpressions.iter() {
668 visitor.visit_expr(*subexpression, env.clone())
669 }
670 }
671 ExprCall(callee_expression, ref arguments) => {
672 for argument in arguments.iter() {
673 visitor.visit_expr(*argument, env.clone())
674 }
675 visitor.visit_expr(callee_expression, env.clone())
676 }
677 ExprMethodCall(_, ref types, ref arguments) => {
678 walk_exprs(visitor, arguments.as_slice(), env.clone());
679 for &typ in types.iter() {
680 visitor.visit_ty(typ, env.clone())
681 }
682 }
683 ExprBinary(_, left_expression, right_expression) => {
684 visitor.visit_expr(left_expression, env.clone());
685 visitor.visit_expr(right_expression, env.clone())
686 }
687 ExprAddrOf(_, subexpression) | ExprUnary(_, subexpression) => {
688 visitor.visit_expr(subexpression, env.clone())
689 }
690 ExprLit(_) => {}
691 ExprCast(subexpression, typ) => {
692 visitor.visit_expr(subexpression, env.clone());
693 visitor.visit_ty(typ, env.clone())
694 }
695 ExprIf(head_expression, if_block, optional_else) => {
696 visitor.visit_expr(head_expression, env.clone());
697 visitor.visit_block(if_block, env.clone());
698 walk_expr_opt(visitor, optional_else, env.clone())
699 }
700 ExprWhile(subexpression, block) => {
701 visitor.visit_expr(subexpression, env.clone());
702 visitor.visit_block(block, env.clone())
703 }
704 ExprForLoop(pattern, subexpression, block, _) => {
705 visitor.visit_pat(pattern, env.clone());
706 visitor.visit_expr(subexpression, env.clone());
707 visitor.visit_block(block, env.clone())
708 }
709 ExprLoop(block, _) => visitor.visit_block(block, env.clone()),
710 ExprMatch(subexpression, ref arms) => {
711 visitor.visit_expr(subexpression, env.clone());
712 for arm in arms.iter() {
713 visitor.visit_arm(arm, env.clone())
714 }
715 }
716 ExprFnBlock(function_declaration, body) => {
717 visitor.visit_fn(&FkFnBlock,
718 function_declaration,
719 body,
720 expression.span,
721 expression.id,
722 env.clone())
723 }
724 ExprProc(function_declaration, body) => {
725 visitor.visit_fn(&FkFnBlock,
726 function_declaration,
727 body,
728 expression.span,
729 expression.id,
730 env.clone())
731 }
732 ExprBlock(block) => visitor.visit_block(block, env.clone()),
733 ExprAssign(left_hand_expression, right_hand_expression) => {
734 visitor.visit_expr(right_hand_expression, env.clone());
735 visitor.visit_expr(left_hand_expression, env.clone())
736 }
737 ExprAssignOp(_, left_expression, right_expression) => {
738 visitor.visit_expr(right_expression, env.clone());
739 visitor.visit_expr(left_expression, env.clone())
740 }
741 ExprField(subexpression, _, ref types) => {
742 visitor.visit_expr(subexpression, env.clone());
743 for &typ in types.iter() {
744 visitor.visit_ty(typ, env.clone())
745 }
746 }
747 ExprIndex(main_expression, index_expression) => {
748 visitor.visit_expr(main_expression, env.clone());
749 visitor.visit_expr(index_expression, env.clone())
750 }
751 ExprPath(ref path) => {
752 visitor.visit_path(path, expression.id, env.clone())
753 }
754 ExprBreak(_) | ExprAgain(_) => {}
755 ExprRet(optional_expression) => {
756 walk_expr_opt(visitor, optional_expression, env.clone())
757 }
758 ExprMac(ref macro) => visitor.visit_mac(macro, env.clone()),
759 ExprParen(subexpression) => {
760 visitor.visit_expr(subexpression, env.clone())
761 }
762 ExprInlineAsm(ref assembler) => {
763 for &(_, input) in assembler.inputs.iter() {
764 visitor.visit_expr(input, env.clone())
765 }
766 for &(_, output) in assembler.outputs.iter() {
767 visitor.visit_expr(output, env.clone())
768 }
769 }
770 }
771
772 visitor.visit_expr_post(expression, env.clone())
773 }
774
775 pub fn walk_arm<E: Clone, V: Visitor<E>>(visitor: &mut V, arm: &Arm, env: E) {
776 for pattern in arm.pats.iter() {
777 visitor.visit_pat(*pattern, env.clone())
778 }
779 walk_expr_opt(visitor, arm.guard, env.clone());
780 visitor.visit_expr(arm.body, env)
781 }
libsyntax/visit.rs:63:1-63:1 -trait- definition:
pub trait Visitor<E: Clone> {
fn visit_ident(&mut self, _sp: Span, _ident: Ident, _e: E) {
/*! Visit the idents */
references:- 37libsyntax/ext/expand.rs:
libsyntax/ext/registrar.rs:
libsyntax/ast_util.rs:
libsyntax/visit.rs:
libsyntax/visit.rs:147:1-147:1 -fn- definition:
pub fn walk_view_item<E: Clone, V: Visitor<E>>(visitor: &mut V, vi: &ViewItem, env: E) {
match vi.node {
ViewItemExternCrate(name, _, _) => {
references:- 268: fn visit_mod(&mut self, m: &Mod, _s: Span, _n: NodeId, e: E) { walk_mod(self, m, e) }
69: fn visit_view_item(&mut self, i: &ViewItem, e: E) { walk_view_item(self, i, e) }
70: fn visit_foreign_item(&mut self, i: &ForeignItem, e: E) { walk_foreign_item(self, i, e) }
libsyntax/ast_util.rs:
428: }
429: visit::walk_view_item(self, view_item, env);
430: self.visited_outermost = false;
libsyntax/visit.rs:400:1-400:1 -fn- definition:
pub fn walk_pat<E: Clone, V: Visitor<E>>(visitor: &mut V, pattern: &Pat, env: E) {
match pattern.node {
PatEnum(ref path, ref children) => {
references:- 3libsyntax/ext/expand.rs:
744: // use the default traversal for non-pat_idents
745: _ => visit::walk_pat(self, pattern, ())
746: }
libsyntax/ast_util.rs:
478: self.operation.visit_id(pattern.id);
479: visit::walk_pat(self, pattern, env)
480: }
libsyntax/visit.rs:
75: fn visit_arm(&mut self, a: &Arm, e: E) { walk_arm(self, a, e) }
76: fn visit_pat(&mut self, p: &Pat, e: E) { walk_pat(self, p, e) }
77: fn visit_decl(&mut self, d: &Decl, e: E) { walk_decl(self, d, e) }
libsyntax/visit.rs:594:1-594:1 -fn- definition:
pub fn walk_block<E: Clone, V: Visitor<E>>(visitor: &mut V, block: &Block, env: E) {
for view_item in block.view_items.iter() {
visitor.visit_view_item(view_item, env.clone())
references:- 272: fn visit_local(&mut self, l: &Local, e: E) { walk_local(self, l, e) }
73: fn visit_block(&mut self, b: &Block, e: E) { walk_block(self, b, e) }
74: fn visit_stmt(&mut self, s: &Stmt, e: E) { walk_stmt(self, s, e) }
libsyntax/ast_util.rs:
468: self.operation.visit_id(block.id);
469: visit::walk_block(self, block, env)
470: }
libsyntax/visit.rs:133:1-133:1 -fn- definition:
pub fn walk_crate<E: Clone, V: Visitor<E>>(visitor: &mut V, krate: &Crate, env: E) {
visitor.visit_mod(&krate.module, krate.span, CRATE_NODE_ID, env)
}
references:- 2libsyntax/ext/registrar.rs:
40: let mut ctx = MacroRegistrarContext { registrars: Vec::new() };
41: visit::walk_crate(&mut ctx, krate, ());
libsyntax/ast_util.rs:
679: };
680: visit::walk_crate(&mut visit, self, ());
681: true
libsyntax/visit.rs:480:1-480:1 -fn- definition:
pub fn walk_generics<E: Clone, V: Visitor<E>>(visitor: &mut V,
generics: &Generics,
env: E) {
references:- 2libsyntax/ast_util.rs:
497: self.visit_generics_helper(generics);
498: visit::walk_generics(self, generics, env)
499: }
libsyntax/visit.rs:
80: fn visit_ty(&mut self, t: &Ty, e: E) { walk_ty(self, t, e) }
81: fn visit_generics(&mut self, g: &Generics, e: E) { walk_generics(self, g, e) }
82: fn visit_fn(&mut self, fk: &FnKind, fd: &FnDecl, b: &Block, s: Span, n: NodeId, e: E) {
libsyntax/visit.rs:630:1-630:1 -fn- definition:
pub fn walk_exprs<E: Clone, V: Visitor<E>>(visitor: &mut V,
expressions: &[@Expr],
env: E) {
references:- 2677: ExprMethodCall(_, ref types, ref arguments) => {
678: walk_exprs(visitor, arguments.as_slice(), env.clone());
679: for &typ in types.iter() {
libsyntax/visit.rs:452:1-452:1 -fn- definition:
pub fn walk_foreign_item<E: Clone, V: Visitor<E>>(visitor: &mut V,
foreign_item: &ForeignItem,
env: E) {
references:- 2libsyntax/ast_util.rs:
434: self.operation.visit_id(foreign_item.id);
435: visit::walk_foreign_item(self, foreign_item, env)
436: }
libsyntax/visit.rs:
69: fn visit_view_item(&mut self, i: &ViewItem, e: E) { walk_view_item(self, i, e) }
70: fn visit_foreign_item(&mut self, i: &ForeignItem, e: E) { walk_foreign_item(self, i, e) }
71: fn visit_item(&mut self, i: &Item, e: E) { walk_item(self, i, e) }
libsyntax/visit.rs:566:1-566:1 -fn- definition:
pub fn walk_struct_def<E: Clone, V: Visitor<E>>(visitor: &mut V,
struct_definition: &StructDef,
_: Ident,
references:- 2libsyntax/ast_util.rs:
558: struct_def.ctor_id.map(|ctor_id| self.operation.visit_id(ctor_id));
559: visit::walk_struct_def(self, struct_def, ident, generics, id, ());
560: }
libsyntax/visit.rs:
87: fn visit_struct_def(&mut self, s: &StructDef, i: Ident, g: &Generics, n: NodeId, e: E) {
88: walk_struct_def(self, s, i, g, n, e)
89: }
libsyntax/visit.rs:555:1-555:1 -fn- definition:
pub fn walk_trait_method<E: Clone, V: Visitor<E>>(visitor: &mut V,
trait_method: &TraitMethod,
env: E) {
references:- 2libsyntax/ast_util.rs:
566: }
567: visit::walk_trait_method(self, tm, ());
568: }
libsyntax/visit.rs:
85: fn visit_ty_method(&mut self, t: &TypeMethod, e: E) { walk_ty_method(self, t, e) }
86: fn visit_trait_method(&mut self, t: &TraitMethod, e: E) { walk_trait_method(self, t, e) }
87: fn visit_struct_def(&mut self, s: &StructDef, i: Ident, g: &Generics, n: NodeId, e: E) {
libsyntax/visit.rs:581:1-581:1 -fn- definition:
pub fn walk_struct_field<E: Clone, V: Visitor<E>>(visitor: &mut V,
struct_field: &StructField,
env: E) {
references:- 2libsyntax/ast_util.rs:
547: self.operation.visit_id(struct_field.node.id);
548: visit::walk_struct_field(self, struct_field, env)
549: }
libsyntax/visit.rs:
89: }
90: fn visit_struct_field(&mut self, s: &StructField, e: E) { walk_struct_field(self, s, e) }
91: fn visit_variant(&mut self, v: &Variant, g: &Generics, e: E) { walk_variant(self, v, g, e) }
libsyntax/visit.rs:29:1-29:1 -enum- definition:
pub enum FnKind<'a> {
// fn foo() or extern "Abi" fn foo()
FkItemFn(Ident, &'a Generics, FnStyle, Abi),
references:- 681: fn visit_generics(&mut self, g: &Generics, e: E) { walk_generics(self, g, e) }
82: fn visit_fn(&mut self, fk: &FnKind, fd: &FnDecl, b: &Block, s: Span, n: NodeId, e: E) {
83: walk_fn(self, fk, fd, b, s, n , e)
--
520: pub fn walk_fn<E: Clone, V: Visitor<E>>(visitor: &mut V,
521: function_kind: &FnKind,
522: function_declaration: &FnDecl,
libsyntax/ast_util.rs:
501: fn visit_fn(&mut self,
502: function_kind: &visit::FnKind,
503: function_declaration: &FnDecl,
--
602: pub fn compute_id_range_for_fn_body(fk: &visit::FnKind,
603: decl: &FnDecl,
libsyntax/visit.rs:
42: pub fn name_of_fn(fk: &FnKind) -> Ident {
43: match *fk {
libsyntax/visit.rs:200:1-200:1 -fn- definition:
pub fn walk_item<E: Clone, V: Visitor<E>>(visitor: &mut V, item: &Item, env: E) {
visitor.visit_ident(item.span, item.ident, env.clone());
match item.node {
references:- 3libsyntax/ext/registrar.rs:
34: visit::walk_item(self, item, ());
35: }
libsyntax/ast_util.rs:
457: visit::walk_item(self, item, env);
libsyntax/visit.rs:
70: fn visit_foreign_item(&mut self, i: &ForeignItem, e: E) { walk_foreign_item(self, i, e) }
71: fn visit_item(&mut self, i: &Item, e: E) { walk_item(self, i, e) }
72: fn visit_local(&mut self, l: &Local, e: E) { walk_local(self, l, e) }
libsyntax/visit.rs:621:1-621:1 -fn- definition:
pub fn walk_expr_opt<E: Clone, V: Visitor<E>>(visitor: &mut V,
optional_expression: Option<@Expr>,
env: E) {
references:- 5778: }
779: walk_expr_opt(visitor, arm.guard, env.clone());
780: visitor.visit_expr(arm.body, env)
libsyntax/visit.rs:642:1-642:1 -fn- definition:
pub fn walk_expr<E: Clone, V: Visitor<E>>(visitor: &mut V, expression: &Expr, env: E) {
match expression.node {
ExprVstore(subexpression, _) => {
references:- 2libsyntax/ast_util.rs:
483: self.operation.visit_id(expression.id);
484: visit::walk_expr(self, expression, env)
485: }
libsyntax/visit.rs:
77: fn visit_decl(&mut self, d: &Decl, e: E) { walk_decl(self, d, e) }
78: fn visit_expr(&mut self, ex: &Expr, e: E) { walk_expr(self, ex, e) }
79: fn visit_expr_post(&mut self, _ex: &Expr, _e: E) { }
libsyntax/visit.rs:311:1-311:1 -fn- definition:
pub fn walk_ty<E: Clone, V: Visitor<E>>(visitor: &mut V, typ: &Ty, env: E) {
match typ.node {
TyUniq(ty) | TyVec(ty) | TyBox(ty) => {
references:- 379: fn visit_expr_post(&mut self, _ex: &Expr, _e: E) { }
80: fn visit_ty(&mut self, t: &Ty, e: E) { walk_ty(self, t, e) }
81: fn visit_generics(&mut self, g: &Generics, e: E) { walk_generics(self, g, e) }
libsyntax/ext/expand.rs:
749: fn visit_ty(&mut self, typ: &ast::Ty, _: ()) {
750: visit::walk_ty(self, typ, ())
751: }
libsyntax/ast_util.rs:
492: }
493: visit::walk_ty(self, typ, env)
494: }
libsyntax/visit.rs:379:1-379:1 -fn- definition:
fn walk_lifetime_decls<E: Clone, V: Visitor<E>>(visitor: &mut V,
lifetimes: &Vec<Lifetime>,
env: E) {
references:- 4351: }
352: walk_lifetime_decls(visitor, &function_declaration.lifetimes,
353: env.clone());
--
359: visitor.visit_ty(function_declaration.decl.output, env.clone());
360: walk_lifetime_decls(visitor, &function_declaration.lifetimes,
361: env.clone());
--
490: }
491: walk_lifetime_decls(visitor, &generics.lifetimes, env);
492: }
libsyntax/visit.rs:387:1-387:1 -fn- definition:
pub fn walk_path<E: Clone, V: Visitor<E>>(visitor: &mut V, path: &Path, env: E) {
for segment in path.segments.iter() {
visitor.visit_ident(path.span, segment.identifier, env.clone());
references:- 2165: }
166: walk_path(visitor, path, env.clone());
167: }
libsyntax/visit.rs:466:1-466:1 -fn- definition:
pub fn walk_ty_param_bounds<E: Clone, V: Visitor<E>>(visitor: &mut V,
bounds: &OwnedSlice<TyParamBound>,
env: E) {
references:- 4334: for bounds in function_declaration.bounds.iter() {
335: walk_ty_param_bounds(visitor, bounds, env.clone())
336: }
--
349: for bounds in function_declaration.bounds.iter() {
350: walk_ty_param_bounds(visitor, bounds, env.clone())
351: }
--
484: for type_parameter in generics.ty_params.iter() {
485: walk_ty_param_bounds(visitor, &type_parameter.bounds, env.clone());
486: match type_parameter.default {
libsyntax/visit.rs:137:1-137:1 -fn- definition:
pub fn walk_mod<E: Clone, V: Visitor<E>>(visitor: &mut V, module: &Mod, env: E) {
for view_item in module.view_items.iter() {
visitor.visit_view_item(view_item, env.clone())
references:- 2libsyntax/ast_util.rs:
398: self.operation.visit_id(node_id);
399: visit::walk_mod(self, module, env)
400: }
libsyntax/visit.rs:
67: }
68: fn visit_mod(&mut self, m: &Mod, _s: Span, _n: NodeId, e: E) { walk_mod(self, m, e) }
69: fn visit_view_item(&mut self, i: &ViewItem, e: E) { walk_view_item(self, i, e) }
libsyntax/visit.rs:519:1-519:1 -fn- definition:
pub fn walk_fn<E: Clone, V: Visitor<E>>(visitor: &mut V,
function_kind: &FnKind,
function_declaration: &FnDecl,
references:- 282: fn visit_fn(&mut self, fk: &FnKind, fd: &FnDecl, b: &Block, s: Span, n: NodeId, e: E) {
83: walk_fn(self, fk, fd, b, s, n , e)
84: }
libsyntax/ast_util.rs:
530: visit::walk_fn(self,
531: function_kind,
libsyntax/visit.rs:172:1-172:1 -fn- definition:
pub fn walk_local<E: Clone, V: Visitor<E>>(visitor: &mut V, local: &Local, env: E) {
visitor.visit_pat(local.pat, env.clone());
visitor.visit_ty(local.ty, env.clone());
references:- 271: fn visit_item(&mut self, i: &Item, e: E) { walk_item(self, i, e) }
72: fn visit_local(&mut self, l: &Local, e: E) { walk_local(self, l, e) }
73: fn visit_block(&mut self, b: &Block, e: E) { walk_block(self, b, e) }
libsyntax/ast_util.rs:
463: self.operation.visit_id(local.id);
464: visit::walk_local(self, local, env)
465: }
libsyntax/visit.rs:604:1-604:1 -fn- definition:
pub fn walk_stmt<E: Clone, V: Visitor<E>>(visitor: &mut V, statement: &Stmt, env: E) {
match statement.node {
StmtDecl(declaration, _) => visitor.visit_decl(declaration, env),
references:- 273: fn visit_block(&mut self, b: &Block, e: E) { walk_block(self, b, e) }
74: fn visit_stmt(&mut self, s: &Stmt, e: E) { walk_stmt(self, s, e) }
75: fn visit_arm(&mut self, a: &Arm, e: E) { walk_arm(self, a, e) }
libsyntax/ast_util.rs:
473: self.operation.visit_id(ast_util::stmt_id(statement));
474: visit::walk_stmt(self, statement, env)
475: }
libsyntax/visit.rs:507:30-507:30 -fn- definition:
// clarifies anything. - Niko
pub fn walk_method_helper<E: Clone, V: Visitor<E>>(visitor: &mut V,
method: &Method,
references:- 3128: IIForeign(i) => visitor.visit_foreign_item(i, env),
129: IIMethod(_, _, m) => walk_method_helper(visitor, m, env),
130: }
--
562: }
563: Provided(method) => walk_method_helper(visitor, method, env),
564: }
libsyntax/visit.rs:493:1-493:1 -fn- definition:
pub fn walk_fn_decl<E: Clone, V: Visitor<E>>(visitor: &mut V,
function_declaration: &FnDecl,
env: E) {
references:- 2459: ForeignItemFn(function_declaration, ref generics) => {
460: walk_fn_decl(visitor, function_declaration, env.clone());
461: visitor.visit_generics(generics, env)
--
526: env: E) {
527: walk_fn_decl(visitor, function_declaration, env.clone());
libsyntax/visit.rs:194:49-194:49 -fn- definition:
/// in Visitor, and so it gets a _helper suffix.
pub fn walk_trait_ref_helper<E: Clone, V: Visitor<E>>(visitor: &mut V,
trait_ref: &TraitRef,
references:- 2240: match *trait_reference {
241: Some(ref trait_reference) => walk_trait_ref_helper(visitor,
242: trait_reference, env.clone()),
--
472: TraitTyParamBound(ref typ) => {
473: walk_trait_ref_helper(visitor, typ, env.clone())
474: }