/
visit.rs
754 lines (705 loc) · 28.4 KB
/
visit.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
// 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.
use abi::AbiSet;
use ast::*;
use ast;
use codemap::Span;
use parse;
use opt_vec;
use opt_vec::OptVec;
// 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.
//
// 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.
pub enum fn_kind<'a> {
// fn foo() or extern "Abi" fn foo()
fk_item_fn(Ident, &'a Generics, purity, AbiSet),
// fn foo(&self)
fk_method(Ident, &'a Generics, &'a method),
// |x, y| ...
// proc(x, y) ...
fk_fn_block,
}
pub fn name_of_fn(fk: &fn_kind) -> Ident {
match *fk {
fk_item_fn(name, _, _, _) | fk_method(name, _, _) => {
name
}
fk_fn_block(..) => parse::token::special_idents::anon,
}
}
pub fn generics_of_fn(fk: &fn_kind) -> Generics {
match *fk {
fk_item_fn(_, generics, _, _) |
fk_method(_, generics, _) => {
(*generics).clone()
}
fk_fn_block(..) => {
Generics {
lifetimes: opt_vec::Empty,
ty_params: opt_vec::Empty,
}
}
}
}
pub trait Visitor<E:Clone> {
fn visit_ident(&mut self, _sp: Span, _ident: Ident, _e: E) {
/*! Visit the idents */
}
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:&view_item, e:E) { walk_view_item(self, i, e) }
fn visit_foreign_item(&mut self, i:@foreign_item, e:E) { walk_foreign_item(self, i, e) }
fn visit_item(&mut self, i:@item, e:E) { walk_item(self, i, e) }
fn visit_local(&mut self, l:@Local, e:E) { walk_local(self, l, e) }
fn visit_block(&mut self, b:P<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) }
fn visit_fn(&mut self, fk:&fn_kind, fd:&fn_decl, b:P<Block>, s:Span, n:NodeId, e:E) {
walk_fn(self, fk, fd, b, s, n , e)
}
fn visit_ty_method(&mut self, t:&TypeMethod, e:E) { walk_ty_method(self, t, e) }
fn visit_trait_method(&mut self, t:&trait_method, e:E) { walk_trait_method(self, t, e) }
fn visit_struct_def(&mut self, s:@struct_def, i:Ident, g:&Generics, n:NodeId, e:E) {
walk_struct_def(self, s, i, g, n, e)
}
fn visit_struct_field(&mut self, s:&struct_field, 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) }
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 */
}
fn visit_explicit_self(&mut self, es: &explicit_self, e: E) {
walk_explicit_self(self, es, e)
}
fn visit_mac(&mut self, macro:&mac, e:E) {
walk_mac(self, macro, e)
}
fn visit_path(&mut self, path: &Path, _id: ast::NodeId, e: E) {
walk_path(self, path, e)
}
}
pub fn walk_crate<E:Clone, V:Visitor<E>>(visitor: &mut V, crate: &Crate, env: E) {
visitor.visit_mod(&crate.module, crate.span, CRATE_NODE_ID, env)
}
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())
}
for item in module.items.iter() {
visitor.visit_item(*item, env.clone())
}
}
pub fn walk_view_item<E:Clone, V:Visitor<E>>(visitor: &mut V, vi: &view_item, env: E) {
match vi.node {
view_item_extern_mod(name, _, _) => {
visitor.visit_ident(vi.span, name, env)
}
view_item_use(ref paths) => {
for vp in paths.iter() {
match vp.node {
view_path_simple(ident, ref path, id) => {
visitor.visit_ident(vp.span, ident, env.clone());
visitor.visit_path(path, id, env.clone());
}
view_path_glob(ref path, id) => {
visitor.visit_path(path, id, env.clone());
}
view_path_list(ref path, ref list, _) => {
for id in list.iter() {
visitor.visit_ident(id.span, id.node.name, env.clone())
}
walk_path(visitor, path, env.clone());
}
}
}
}
}
}
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());
match local.init {
None => {}
Some(initializer) => visitor.visit_expr(initializer, env),
}
}
fn walk_explicit_self<E:Clone, V:Visitor<E>>(visitor: &mut V,
explicit_self: &explicit_self,
env: E) {
match explicit_self.node {
sty_static | sty_value(_) | sty_box(_) | sty_uniq(_) => {
}
sty_region(ref lifetime, _) => {
visitor.visit_opt_lifetime_ref(explicit_self.span, lifetime, env)
}
}
}
fn walk_trait_ref<E:Clone, V:Visitor<E>>(visitor: &mut V,
trait_ref: &ast::trait_ref,
env: E) {
visitor.visit_path(&trait_ref.path, trait_ref.ref_id, env)
}
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 {
item_static(typ, _, expr) => {
visitor.visit_ty(typ, env.clone());
visitor.visit_expr(expr, env);
}
item_fn(declaration, purity, abi, ref generics, body) => {
visitor.visit_fn(&fk_item_fn(item.ident, generics, purity, abi),
declaration,
body,
item.span,
item.id,
env)
}
item_mod(ref module) => {
visitor.visit_mod(module, item.span, item.id, env)
}
item_foreign_mod(ref foreign_module) => {
for view_item in foreign_module.view_items.iter() {
visitor.visit_view_item(view_item, env.clone())
}
for foreign_item in foreign_module.items.iter() {
visitor.visit_foreign_item(*foreign_item, env.clone())
}
}
item_ty(typ, ref type_parameters) => {
visitor.visit_ty(typ, env.clone());
visitor.visit_generics(type_parameters, env)
}
item_enum(ref enum_definition, ref type_parameters) => {
visitor.visit_generics(type_parameters, env.clone());
walk_enum_def(visitor, enum_definition, type_parameters, env)
}
item_impl(ref type_parameters,
ref trait_references,
typ,
ref methods) => {
visitor.visit_generics(type_parameters, env.clone());
for trait_reference in trait_references.iter() {
walk_trait_ref(visitor, trait_reference, env.clone())
}
visitor.visit_ty(typ, env.clone());
for method in methods.iter() {
walk_method_helper(visitor, *method, env.clone())
}
}
item_struct(struct_definition, ref generics) => {
visitor.visit_generics(generics, env.clone());
visitor.visit_struct_def(struct_definition,
item.ident,
generics,
item.id,
env)
}
item_trait(ref generics, ref trait_paths, ref methods) => {
visitor.visit_generics(generics, env.clone());
for trait_path in trait_paths.iter() {
visitor.visit_path(&trait_path.path,
trait_path.ref_id,
env.clone())
}
for method in methods.iter() {
visitor.visit_trait_method(method, env.clone())
}
}
item_mac(ref macro) => visitor.visit_mac(macro, env),
}
}
pub fn walk_enum_def<E:Clone, V:Visitor<E>>(visitor: &mut V,
enum_definition: &ast::enum_def,
generics: &Generics,
env: E) {
for &variant in enum_definition.variants.iter() {
visitor.visit_variant(variant, generics, env.clone());
}
}
pub fn walk_variant<E:Clone, V:Visitor<E>>(visitor:&mut V,
variant: &variant,
generics: &Generics,
env: E) {
visitor.visit_ident(variant.span, variant.node.name, env.clone());
match variant.node.kind {
tuple_variant_kind(ref variant_arguments) => {
for variant_argument in variant_arguments.iter() {
visitor.visit_ty(variant_argument.ty, env.clone())
}
}
struct_variant_kind(struct_definition) => {
visitor.visit_struct_def(struct_definition,
variant.node.name,
generics,
variant.node.id,
env.clone())
}
}
match variant.node.disr_expr {
Some(expr) => visitor.visit_expr(expr, env),
None => ()
}
}
pub fn skip_ty<E, V:Visitor<E>>(_: &mut V, _: &Ty, _: E) {
// Empty!
}
pub fn walk_ty<E:Clone, V:Visitor<E>>(visitor: &mut V, typ: &Ty, env: E) {
match typ.node {
ty_uniq(ty) | ty_vec(ty) => {
visitor.visit_ty(ty, env)
}
ty_box(ref mutable_type) | ty_ptr(ref mutable_type) => {
visitor.visit_ty(mutable_type.ty, env)
}
ty_rptr(ref lifetime, ref mutable_type) => {
visitor.visit_opt_lifetime_ref(typ.span, lifetime, env.clone());
visitor.visit_ty(mutable_type.ty, env)
}
ty_tup(ref tuple_element_types) => {
for &tuple_element_type in tuple_element_types.iter() {
visitor.visit_ty(tuple_element_type, env.clone())
}
}
ty_closure(ref function_declaration) => {
for argument in function_declaration.decl.inputs.iter() {
visitor.visit_ty(argument.ty, env.clone())
}
visitor.visit_ty(function_declaration.decl.output, env.clone());
for bounds in function_declaration.bounds.iter() {
walk_ty_param_bounds(visitor, bounds, env.clone())
}
visitor.visit_opt_lifetime_ref(
typ.span,
&function_declaration.region,
env.clone());
walk_lifetime_decls(visitor, &function_declaration.lifetimes,
env.clone());
}
ty_bare_fn(ref function_declaration) => {
for argument in function_declaration.decl.inputs.iter() {
visitor.visit_ty(argument.ty, env.clone())
}
visitor.visit_ty(function_declaration.decl.output, env.clone());
walk_lifetime_decls(visitor, &function_declaration.lifetimes,
env.clone());
}
ty_path(ref path, ref bounds, id) => {
visitor.visit_path(path, id, env.clone());
for bounds in bounds.iter() {
walk_ty_param_bounds(visitor, bounds, env.clone())
}
}
ty_fixed_length_vec(ty, expression) => {
visitor.visit_ty(ty, env.clone());
visitor.visit_expr(expression, env)
}
ty_typeof(expression) => {
visitor.visit_expr(expression, env)
}
ty_nil | ty_bot | ty_infer => ()
}
}
fn walk_lifetime_decls<E:Clone, V:Visitor<E>>(visitor: &mut V,
lifetimes: &OptVec<Lifetime>,
env: E) {
for l in lifetimes.iter() {
visitor.visit_lifetime_decl(l, env.clone());
}
}
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());
for &typ in segment.types.iter() {
visitor.visit_ty(typ, env.clone());
}
for lifetime in segment.lifetimes.iter() {
visitor.visit_lifetime_ref(lifetime, env.clone());
}
}
}
pub fn walk_pat<E:Clone, V:Visitor<E>>(visitor: &mut V, pattern: &Pat, env: E) {
match pattern.node {
PatEnum(ref path, ref children) => {
visitor.visit_path(path, pattern.id, env.clone());
for children in children.iter() {
for child in children.iter() {
visitor.visit_pat(*child, env.clone())
}
}
}
PatStruct(ref path, ref fields, _) => {
visitor.visit_path(path, pattern.id, env.clone());
for field in fields.iter() {
visitor.visit_pat(field.pat, env.clone())
}
}
PatTup(ref tuple_elements) => {
for tuple_element in tuple_elements.iter() {
visitor.visit_pat(*tuple_element, env.clone())
}
}
PatBox(subpattern) |
PatUniq(subpattern) |
PatRegion(subpattern) => {
visitor.visit_pat(subpattern, env)
}
PatIdent(_, ref path, ref optional_subpattern) => {
visitor.visit_path(path, pattern.id, env.clone());
match *optional_subpattern {
None => {}
Some(subpattern) => visitor.visit_pat(subpattern, env),
}
}
PatLit(expression) => visitor.visit_expr(expression, env),
PatRange(lower_bound, upper_bound) => {
visitor.visit_expr(lower_bound, env.clone());
visitor.visit_expr(upper_bound, env)
}
PatWild | PatWildMulti => (),
PatVec(ref prepattern, ref slice_pattern, ref postpatterns) => {
for prepattern in prepattern.iter() {
visitor.visit_pat(*prepattern, env.clone())
}
for slice_pattern in slice_pattern.iter() {
visitor.visit_pat(*slice_pattern, env.clone())
}
for postpattern in postpatterns.iter() {
visitor.visit_pat(*postpattern, env.clone())
}
}
}
}
pub fn walk_foreign_item<E:Clone, V:Visitor<E>>(visitor: &mut V,
foreign_item: &foreign_item,
env: E) {
visitor.visit_ident(foreign_item.span, foreign_item.ident, env.clone());
match foreign_item.node {
foreign_item_fn(function_declaration, ref generics) => {
walk_fn_decl(visitor, function_declaration, env.clone());
visitor.visit_generics(generics, env)
}
foreign_item_static(typ, _) => visitor.visit_ty(typ, env),
}
}
pub fn walk_ty_param_bounds<E:Clone, V:Visitor<E>>(visitor: &mut V,
bounds: &OptVec<TyParamBound>,
env: E) {
for bound in bounds.iter() {
match *bound {
TraitTyParamBound(ref typ) => {
walk_trait_ref(visitor, typ, env.clone())
}
RegionTyParamBound => {}
}
}
}
pub fn walk_generics<E:Clone, V:Visitor<E>>(visitor: &mut V,
generics: &Generics,
env: E) {
for type_parameter in generics.ty_params.iter() {
walk_ty_param_bounds(visitor, &type_parameter.bounds, env.clone())
}
walk_lifetime_decls(visitor, &generics.lifetimes, env);
}
pub fn walk_fn_decl<E:Clone, V:Visitor<E>>(visitor: &mut V,
function_declaration: &fn_decl,
env: E) {
for argument in function_declaration.inputs.iter() {
visitor.visit_pat(argument.pat, env.clone());
visitor.visit_ty(argument.ty, env.clone())
}
visitor.visit_ty(function_declaration.output, env)
}
// Note: there is no visit_method() method in the visitor, instead override
// visit_fn() and check for fk_method(). I named this visit_method_helper()
// because it is not a default impl of any method, though I doubt that really
// clarifies anything. - Niko
pub fn walk_method_helper<E:Clone, V:Visitor<E>>(visitor: &mut V,
method: &method,
env: E) {
visitor.visit_ident(method.span, method.ident, env.clone());
visitor.visit_fn(&fk_method(method.ident, &method.generics, method),
method.decl,
method.body,
method.span,
method.id,
env)
}
pub fn walk_fn<E:Clone, V:Visitor<E>>(visitor: &mut V,
function_kind: &fn_kind,
function_declaration: &fn_decl,
function_body: P<Block>,
_span: Span,
_: NodeId,
env: E) {
walk_fn_decl(visitor, function_declaration, env.clone());
match *function_kind {
fk_item_fn(_, generics, _, _) => {
visitor.visit_generics(generics, env.clone());
}
fk_method(_, generics, method) => {
visitor.visit_generics(generics, env.clone());
visitor.visit_explicit_self(&method.explicit_self, env.clone());
}
fk_fn_block(..) => {
}
}
visitor.visit_block(function_body, env)
}
pub fn walk_ty_method<E:Clone, V:Visitor<E>>(visitor: &mut V,
method_type: &TypeMethod,
env: E) {
visitor.visit_ident(method_type.span, method_type.ident, env.clone());
visitor.visit_explicit_self(&method_type.explicit_self, env.clone());
for argument_type in method_type.decl.inputs.iter() {
visitor.visit_ty(argument_type.ty, env.clone())
}
visitor.visit_generics(&method_type.generics, env.clone());
visitor.visit_ty(method_type.decl.output, env);
}
pub fn walk_trait_method<E:Clone, V:Visitor<E>>(visitor: &mut V,
trait_method: &trait_method,
env: E) {
match *trait_method {
required(ref method_type) => {
visitor.visit_ty_method(method_type, env)
}
provided(method) => walk_method_helper(visitor, method, env),
}
}
pub fn walk_struct_def<E:Clone, V:Visitor<E>>(visitor: &mut V,
struct_definition: @struct_def,
_: ast::Ident,
_: &Generics,
_: NodeId,
env: E) {
for field in struct_definition.fields.iter() {
visitor.visit_struct_field(field, env.clone())
}
}
pub fn walk_struct_field<E:Clone, V:Visitor<E>>(visitor: &mut V,
struct_field: &struct_field,
env: E) {
match struct_field.node.kind {
named_field(name, _) => {
visitor.visit_ident(struct_field.span, name, env.clone())
}
_ => {}
}
visitor.visit_ty(struct_field.node.ty, env)
}
pub fn walk_block<E:Clone, V:Visitor<E>>(visitor: &mut V, block: P<Block>, env: E) {
for view_item in block.view_items.iter() {
visitor.visit_view_item(view_item, env.clone())
}
for statement in block.stmts.iter() {
visitor.visit_stmt(*statement, env.clone())
}
walk_expr_opt(visitor, block.expr, env)
}
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),
StmtExpr(expression, _) | StmtSemi(expression, _) => {
visitor.visit_expr(expression, env)
}
StmtMac(ref macro, _) => visitor.visit_mac(macro, env),
}
}
pub fn walk_decl<E:Clone, V:Visitor<E>>(visitor: &mut V, declaration: &Decl, env: E) {
match declaration.node {
DeclLocal(ref local) => visitor.visit_local(*local, env),
DeclItem(item) => visitor.visit_item(item, env),
}
}
pub fn walk_expr_opt<E:Clone, V:Visitor<E>>(visitor: &mut V,
optional_expression: Option<@Expr>,
env: E) {
match optional_expression {
None => {}
Some(expression) => visitor.visit_expr(expression, env),
}
}
pub fn walk_exprs<E:Clone, V:Visitor<E>>(visitor: &mut V,
expressions: &[@Expr],
env: E) {
for expression in expressions.iter() {
visitor.visit_expr(*expression, env.clone())
}
}
pub fn walk_mac<E, V:Visitor<E>>(_: &mut V, _: &mac, _: E) {
// Empty!
}
pub fn walk_expr<E:Clone, V:Visitor<E>>(visitor: &mut V, expression: @Expr, env: E) {
match expression.node {
ExprVstore(subexpression, _) => {
visitor.visit_expr(subexpression, env.clone())
}
ExprVec(ref subexpressions, _) => {
walk_exprs(visitor, *subexpressions, env.clone())
}
ExprRepeat(element, count, _) => {
visitor.visit_expr(element, env.clone());
visitor.visit_expr(count, env.clone())
}
ExprStruct(ref path, ref fields, optional_base) => {
visitor.visit_path(path, expression.id, env.clone());
for field in fields.iter() {
visitor.visit_expr(field.expr, env.clone())
}
walk_expr_opt(visitor, optional_base, env.clone())
}
ExprTup(ref subexpressions) => {
for subexpression in subexpressions.iter() {
visitor.visit_expr(*subexpression, env.clone())
}
}
ExprCall(callee_expression, ref arguments, _) => {
for argument in arguments.iter() {
visitor.visit_expr(*argument, env.clone())
}
visitor.visit_expr(callee_expression, env.clone())
}
ExprMethodCall(_, callee, _, ref types, ref arguments, _) => {
walk_exprs(visitor, *arguments, env.clone());
for &typ in types.iter() {
visitor.visit_ty(typ, env.clone())
}
visitor.visit_expr(callee, env.clone())
}
ExprBinary(_, _, left_expression, right_expression) => {
visitor.visit_expr(left_expression, env.clone());
visitor.visit_expr(right_expression, env.clone())
}
ExprAddrOf(_, subexpression) |
ExprUnary(_, _, subexpression) |
ExprDoBody(subexpression) => {
visitor.visit_expr(subexpression, env.clone())
}
ExprLit(_) => {}
ExprCast(subexpression, typ) => {
visitor.visit_expr(subexpression, env.clone());
visitor.visit_ty(typ, env.clone())
}
ExprIf(head_expression, if_block, optional_else) => {
visitor.visit_expr(head_expression, env.clone());
visitor.visit_block(if_block, env.clone());
walk_expr_opt(visitor, optional_else, env.clone())
}
ExprWhile(subexpression, block) => {
visitor.visit_expr(subexpression, env.clone());
visitor.visit_block(block, env.clone())
}
ExprForLoop(pattern, subexpression, block, _) => {
visitor.visit_pat(pattern, env.clone());
visitor.visit_expr(subexpression, env.clone());
visitor.visit_block(block, env.clone())
}
ExprLoop(block, _) => visitor.visit_block(block, env.clone()),
ExprMatch(subexpression, ref arms) => {
visitor.visit_expr(subexpression, env.clone());
for arm in arms.iter() {
visitor.visit_arm(arm, env.clone())
}
}
ExprFnBlock(function_declaration, body) => {
visitor.visit_fn(&fk_fn_block,
function_declaration,
body,
expression.span,
expression.id,
env.clone())
}
ExprProc(function_declaration, body) => {
visitor.visit_fn(&fk_fn_block,
function_declaration,
body,
expression.span,
expression.id,
env.clone())
}
ExprBlock(block) => visitor.visit_block(block, env.clone()),
ExprAssign(left_hand_expression, right_hand_expression) => {
visitor.visit_expr(right_hand_expression, env.clone());
visitor.visit_expr(left_hand_expression, env.clone())
}
ExprAssignOp(_, _, left_expression, right_expression) => {
visitor.visit_expr(right_expression, env.clone());
visitor.visit_expr(left_expression, env.clone())
}
ExprField(subexpression, _, ref types) => {
visitor.visit_expr(subexpression, env.clone());
for &typ in types.iter() {
visitor.visit_ty(typ, env.clone())
}
}
ExprIndex(_, main_expression, index_expression) => {
visitor.visit_expr(main_expression, env.clone());
visitor.visit_expr(index_expression, env.clone())
}
ExprPath(ref path) => {
visitor.visit_path(path, expression.id, env.clone())
}
ExprSelf | ExprBreak(_) | ExprAgain(_) => {}
ExprRet(optional_expression) => {
walk_expr_opt(visitor, optional_expression, env.clone())
}
ExprLogLevel => {}
ExprMac(ref macro) => visitor.visit_mac(macro, env.clone()),
ExprParen(subexpression) => {
visitor.visit_expr(subexpression, env.clone())
}
ExprInlineAsm(ref assembler) => {
for &(_, input) in assembler.inputs.iter() {
visitor.visit_expr(input, env.clone())
}
for &(_, output) in assembler.outputs.iter() {
visitor.visit_expr(output, env.clone())
}
}
}
visitor.visit_expr_post(expression, env.clone())
}
pub fn walk_arm<E:Clone, V:Visitor<E>>(visitor: &mut V, arm: &Arm, env: E) {
for pattern in arm.pats.iter() {
visitor.visit_pat(*pattern, env.clone())
}
walk_expr_opt(visitor, arm.guard, env.clone());
visitor.visit_block(arm.body, env)
}