visit.rs 25.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
// Copyright 2012 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.

11
use abi::AbiSet;
P
Patrick Walton 已提交
12
use ast::*;
13
use ast;
P
Patrick Walton 已提交
14
use codemap::span;
15
use parse;
16
use opt_vec;
17
use opt_vec::OptVec;
18

M
Marijn Haverbeke 已提交
19 20 21 22 23
// Context-passing AST walker. Each overridden visit method has full control
// over what happens with its node, it can do its own traversal of the node's
// children (potentially passing in different contexts to each), call
// visit::visit_* to apply the default traversal algorithm (again, it can
// override the context), or prevent deeper traversal by doing nothing.
N
Niko Matsakis 已提交
24 25 26 27 28 29
//
// Note: it is an important invariant that the default visitor walks the body
// of a function in "execution order" (more concretely, reverse post-order
// with respect to the CFG implied by the AST), meaning that if AST node A may
// execute before AST node B, then A is visited first.  The borrow checker in
// particular relies on this property.
M
Marijn Haverbeke 已提交
30

31
// Our typesystem doesn't do circular types, so the visitor record can not
P
Patrick Walton 已提交
32
// hold functions that take visitors. A vt enum is used to break the cycle.
33
pub enum vt<E> { mk_vt(visitor<E>), }
M
Marijn Haverbeke 已提交
34

35
pub enum fn_kind<'self> {
36 37 38 39 40 41
    // fn foo() or extern "Abi" fn foo()
    fk_item_fn(ident, &'self Generics, purity, AbiSet),

    // fn foo(&self)
    fk_method(ident, &'self Generics, &'self method),

42
    // @fn(x, y) { ... }
43 44 45 46
    fk_anon(ast::Sigil),

    // |x, y| ...
    fk_fn_block,
47 48
}

49 50
pub fn name_of_fn(fk: &fn_kind) -> ident {
    match *fk {
51 52
      fk_item_fn(name, _, _, _) | fk_method(name, _, _) => {
          name
53
      }
P
Paul Stansifer 已提交
54
      fk_anon(*) | fk_fn_block(*) => parse::token::special_idents::anon,
55 56 57
    }
}

58
pub fn generics_of_fn(fk: &fn_kind) -> Generics {
59
    match *fk {
60
        fk_item_fn(_, generics, _, _) |
E
Erick Tryzelaar 已提交
61
        fk_method(_, generics, _) => {
62
            (*generics).clone()
63 64
        }
        fk_anon(*) | fk_fn_block(*) => {
65 66 67 68
            Generics {
                lifetimes: opt_vec::Empty,
                ty_params: opt_vec::Empty,
            }
69
        }
70 71 72
    }
}

73
pub struct Visitor<E> {
74
    visit_mod: @fn(&_mod, span, NodeId, (E, vt<E>)),
J
James Miller 已提交
75
    visit_view_item: @fn(&view_item, (E, vt<E>)),
76 77
    visit_foreign_item: @fn(@foreign_item, (E, vt<E>)),
    visit_item: @fn(@item, (E, vt<E>)),
78 79
    visit_local: @fn(@Local, (E, vt<E>)),
    visit_block: @fn(&Block, (E, vt<E>)),
80 81 82 83 84 85
    visit_stmt: @fn(@stmt, (E, vt<E>)),
    visit_arm: @fn(&arm, (E, vt<E>)),
    visit_pat: @fn(@pat, (E, vt<E>)),
    visit_decl: @fn(@decl, (E, vt<E>)),
    visit_expr: @fn(@expr, (E, vt<E>)),
    visit_expr_post: @fn(@expr, (E, vt<E>)),
J
James Miller 已提交
86
    visit_ty: @fn(&Ty, (E, vt<E>)),
87
    visit_generics: @fn(&Generics, (E, vt<E>)),
88 89
    visit_fn: @fn(&fn_kind, &fn_decl, &Block, span, NodeId, (E, vt<E>)),
    visit_ty_method: @fn(&TypeMethod, (E, vt<E>)),
90
    visit_trait_method: @fn(&trait_method, (E, vt<E>)),
91
    visit_struct_def: @fn(@struct_def, ident, &Generics, NodeId, (E, vt<E>)),
92
    visit_struct_field: @fn(@struct_field, (E, vt<E>)),
93 94
}

95
pub type visitor<E> = @Visitor<E>;
M
Marijn Haverbeke 已提交
96

97
pub fn default_visitor<E:Clone>() -> visitor<E> {
98
    return @Visitor {
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
        visit_mod: |a,b,c,d|visit_mod::<E>(a, b, c, d),
        visit_view_item: |a,b|visit_view_item::<E>(a, b),
        visit_foreign_item: |a,b|visit_foreign_item::<E>(a, b),
        visit_item: |a,b|visit_item::<E>(a, b),
        visit_local: |a,b|visit_local::<E>(a, b),
        visit_block: |a,b|visit_block::<E>(a, b),
        visit_stmt: |a,b|visit_stmt::<E>(a, b),
        visit_arm: |a,b|visit_arm::<E>(a, b),
        visit_pat: |a,b|visit_pat::<E>(a, b),
        visit_decl: |a,b|visit_decl::<E>(a, b),
        visit_expr: |a,b|visit_expr::<E>(a, b),
        visit_expr_post: |_a,_b| (),
        visit_ty: |a,b|skip_ty::<E>(a, b),
        visit_generics: |a,b|visit_generics::<E>(a, b),
        visit_fn: |a,b,c,d,e,f|visit_fn::<E>(a, b, c, d, e, f),
        visit_ty_method: |a,b|visit_ty_method::<E>(a, b),
        visit_trait_method: |a,b|visit_trait_method::<E>(a, b),
        visit_struct_def: |a,b,c,d,e|visit_struct_def::<E>(a, b, c, d, e),
        visit_struct_field: |a,b|visit_struct_field::<E>(a, b),
118
    };
M
Marijn Haverbeke 已提交
119 120
}

121
pub fn visit_crate<E:Clone>(c: &Crate, (e, v): (E, vt<E>)) {
122
    (v.visit_mod)(&c.module, c.span, CRATE_NODE_ID, (e, v));
M
Marijn Haverbeke 已提交
123 124
}

125 126
pub fn visit_mod<E:Clone>(m: &_mod,
                          _sp: span,
127
                          _id: NodeId,
128
                          (e, v): (E, vt<E>)) {
129
    foreach vi in m.view_items.iter() {
130 131
        (v.visit_view_item)(vi, (e.clone(), v));
    }
132
    foreach i in m.items.iter() {
133 134
        (v.visit_item)(*i, (e.clone(), v));
    }
M
Marijn Haverbeke 已提交
135 136
}

137
pub fn visit_view_item<E>(_vi: &view_item, (_e, _v): (E, vt<E>)) { }
138

139 140 141 142
pub fn visit_local<E:Clone>(loc: &Local, (e, v): (E, vt<E>)) {
    (v.visit_pat)(loc.pat, (e.clone(), v));
    (v.visit_ty)(&loc.ty, (e.clone(), v));
    match loc.init {
B
Brian Anderson 已提交
143
      None => (),
144
      Some(ex) => (v.visit_expr)(ex, (e, v))
B
Brian Anderson 已提交
145
    }
146 147
}

148
fn visit_trait_ref<E:Clone>(tref: &ast::trait_ref, (e, v): (E, vt<E>)) {
J
James Miller 已提交
149
    visit_path(&tref.path, (e, v));
150 151
}

152
pub fn visit_item<E:Clone>(i: &item, (e, v): (E, vt<E>)) {
153
    match i.node {
J
James Miller 已提交
154
        item_static(ref t, _, ex) => {
155 156
            (v.visit_ty)(t, (e.clone(), v));
            (v.visit_expr)(ex, (e.clone(), v));
157
        }
158
        item_fn(ref decl, purity, abi, ref generics, ref body) => {
159 160
            (v.visit_fn)(
                &fk_item_fn(
161
                    i.ident,
162
                    generics,
163 164
                    purity,
                    abi
165
                ),
166 167
                decl,
                body,
168 169
                i.span,
                i.id,
170 171
                (e,
                 v)
172
            );
173
        }
174
        item_mod(ref m) => (v.visit_mod)(m, i.span, i.id, (e, v)),
175
        item_foreign_mod(ref nm) => {
176
            foreach vi in nm.view_items.iter() {
177 178
                (v.visit_view_item)(vi, (e.clone(), v));
            }
179
            foreach ni in nm.items.iter() {
180 181
                (v.visit_foreign_item)(*ni, (e.clone(), v));
            }
182
        }
J
James Miller 已提交
183
        item_ty(ref t, ref tps) => {
184
            (v.visit_ty)(t, (e.clone(), v));
185
            (v.visit_generics)(tps, (e, v));
186 187
        }
        item_enum(ref enum_definition, ref tps) => {
188
            (v.visit_generics)(tps, (e.clone(), v));
189
            visit_enum_def(
190
                enum_definition,
191
                tps,
192
                (e, v)
193 194
            );
        }
J
James Miller 已提交
195
        item_impl(ref tps, ref traits, ref ty, ref methods) => {
196
            (v.visit_generics)(tps, (e.clone(), v));
197
            foreach p in traits.iter() {
198
                visit_trait_ref(p, (e.clone(), v));
199
            }
200
            (v.visit_ty)(ty, (e.clone(), v));
201
            foreach m in methods.iter() {
202
                visit_method_helper(*m, (e.clone(), v))
203 204
            }
        }
205
        item_struct(struct_def, ref generics) => {
206
            (v.visit_generics)(generics, (e.clone(), v));
207
            (v.visit_struct_def)(struct_def, i.ident, generics, i.id, (e, v));
208
        }
209
        item_trait(ref generics, ref traits, ref methods) => {
210
            (v.visit_generics)(generics, (e.clone(), v));
211
            foreach p in traits.iter() {
212 213
                visit_path(&p.path, (e.clone(), v));
            }
214
            foreach m in methods.iter() {
215
                (v.visit_trait_method)(m, (e.clone(), v));
216 217
            }
        }
218
        item_mac(ref m) => visit_mac(m, (e, v))
M
Marijn Haverbeke 已提交
219 220 221
    }
}

222
pub fn visit_enum_def<E:Clone>(enum_definition: &ast::enum_def,
223
                               tps: &Generics,
224
                               (e, v): (E, vt<E>)) {
225
    foreach vr in enum_definition.variants.iter() {
226
        match vr.node.kind {
227
            tuple_variant_kind(ref variant_args) => {
228
                foreach va in variant_args.iter() {
229
                    (v.visit_ty)(&va.ty, (e.clone(), v));
230
                }
231 232
            }
            struct_variant_kind(struct_def) => {
233
                (v.visit_struct_def)(struct_def, vr.node.name, tps,
234
                                     vr.node.id, (e.clone(), v));
235 236
            }
        }
237
        // Visit the disr expr if it exists
238
        foreach ex in vr.node.disr_expr.iter() {
239
            (v.visit_expr)(*ex, (e.clone(), v))
240
        }
241 242 243
    }
}

244
pub fn skip_ty<E>(_t: &Ty, (_e,_v): (E, vt<E>)) {}
245

246
pub fn visit_ty<E:Clone>(t: &Ty, (e, v): (E, vt<E>)) {
247
    match t.node {
J
James Miller 已提交
248 249
        ty_box(ref mt) | ty_uniq(ref mt) |
        ty_vec(ref mt) | ty_ptr(ref mt) | ty_rptr(_, ref mt) => {
250
            (v.visit_ty)(mt.ty, (e, v));
251 252
        },
        ty_tup(ref ts) => {
253
            foreach tt in ts.iter() {
254
                (v.visit_ty)(tt, (e.clone(), v));
255 256 257
            }
        },
        ty_closure(ref f) => {
258
            foreach a in f.decl.inputs.iter() {
259 260 261
                (v.visit_ty)(&a.ty, (e.clone(), v));
            }
            (v.visit_ty)(&f.decl.output, (e.clone(), v));
262
            do f.bounds.map |bounds| {
263
                visit_ty_param_bounds(bounds, (e.clone(), v));
264
            };
265 266
        },
        ty_bare_fn(ref f) => {
267
            foreach a in f.decl.inputs.iter() {
268 269
                (v.visit_ty)(&a.ty, (e.clone(), v));
            }
J
James Miller 已提交
270
            (v.visit_ty)(&f.decl.output, (e, v));
271
        },
J
James Miller 已提交
272
        ty_path(ref p, ref bounds, _) => {
273
            visit_path(p, (e.clone(), v));
274
            do bounds.map |bounds| {
275
                visit_ty_param_bounds(bounds, (e.clone(), v));
276
            };
277
        },
278
        ty_fixed_length_vec(ref mt, ex) => {
279 280
            (v.visit_ty)(mt.ty, (e.clone(), v));
            (v.visit_expr)(ex, (e.clone(), v));
281
        },
282
        ty_nil | ty_bot | ty_mac(_) | ty_infer => ()
M
Marijn Haverbeke 已提交
283 284 285
    }
}

286
pub fn visit_path<E:Clone>(p: &Path, (e, v): (E, vt<E>)) {
287
    foreach tp in p.types.iter() { (v.visit_ty)(tp, (e.clone(), v)); }
288 289
}

290
pub fn visit_pat<E:Clone>(p: &pat, (e, v): (E, vt<E>)) {
291
    match p.node {
J
James Miller 已提交
292
        pat_enum(ref path, ref children) => {
293
            visit_path(path, (e.clone(), v));
294 295
            foreach children in children.iter() {
                foreach child in children.iter() {
296
                    (v.visit_pat)(*child, (e.clone(), v));
297
                }
298
            }
299
        }
J
James Miller 已提交
300
        pat_struct(ref path, ref fields, _) => {
301
            visit_path(path, (e.clone(), v));
302
            foreach f in fields.iter() {
303
                (v.visit_pat)(f.pat, (e.clone(), v));
304 305 306
            }
        }
        pat_tup(ref elts) => {
307
            foreach elt in elts.iter() {
308
                (v.visit_pat)(*elt, (e.clone(), v))
309 310 311
            }
        },
        pat_box(inner) | pat_uniq(inner) | pat_region(inner) => {
312
            (v.visit_pat)(inner, (e, v))
313
        },
J
James Miller 已提交
314
        pat_ident(_, ref path, ref inner) => {
315
            visit_path(path, (e.clone(), v));
316
            foreach subpat in inner.iter() {
317
                (v.visit_pat)(*subpat, (e.clone(), v))
318
            }
319
        }
320
        pat_lit(ex) => (v.visit_expr)(ex, (e, v)),
321
        pat_range(e1, e2) => {
322
            (v.visit_expr)(e1, (e.clone(), v));
323
            (v.visit_expr)(e2, (e, v));
324 325
        }
        pat_wild => (),
326
        pat_vec(ref before, ref slice, ref after) => {
327
            foreach elt in before.iter() {
328
                (v.visit_pat)(*elt, (e.clone(), v));
329
            }
330
            foreach elt in slice.iter() {
331
                (v.visit_pat)(*elt, (e.clone(), v));
332
            }
333
            foreach tail in after.iter() {
334
                (v.visit_pat)(*tail, (e.clone(), v));
335
            }
336
        }
M
Marijn Haverbeke 已提交
337 338 339
    }
}

340
pub fn visit_foreign_item<E:Clone>(ni: &foreign_item, (e, v): (E, vt<E>)) {
341
    match ni.node {
342
        foreign_item_fn(ref fd, _, ref generics) => {
343
            visit_fn_decl(fd, (e.clone(), v));
344
            (v.visit_generics)(generics, (e, v));
345
        }
J
James Miller 已提交
346
        foreign_item_static(ref t, _) => {
347
            (v.visit_ty)(t, (e, v));
348
        }
M
Marijn Haverbeke 已提交
349 350 351
    }
}

352
pub fn visit_ty_param_bounds<E:Clone>(bounds: &OptVec<TyParamBound>,
353
                                      (e, v): (E, vt<E>)) {
354
    foreach bound in bounds.iter() {
355
        match *bound {
356
            TraitTyParamBound(ref ty) => visit_trait_ref(ty, (e.clone(), v)),
357
            RegionTyParamBound => {}
358
        }
359 360 361
    }
}

362
pub fn visit_generics<E:Clone>(generics: &Generics, (e, v): (E, vt<E>)) {
363
    foreach tp in generics.ty_params.iter() {
364
        visit_ty_param_bounds(&tp.bounds, (e.clone(), v));
365 366 367
    }
}

368
pub fn visit_fn_decl<E:Clone>(fd: &fn_decl, (e, v): (E, vt<E>)) {
369
    foreach a in fd.inputs.iter() {
370 371
        (v.visit_pat)(a.pat, (e.clone(), v));
        (v.visit_ty)(&a.ty, (e.clone(), v));
372
    }
J
James Miller 已提交
373
    (v.visit_ty)(&fd.output, (e, v));
M
Marijn Haverbeke 已提交
374 375
}

376 377 378 379
// 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
380
pub fn visit_method_helper<E:Clone>(m: &method, (e, v): (E, vt<E>)) {
381 382 383 384 385 386
    (v.visit_fn)(&fk_method(m.ident, &m.generics, m),
                 &m.decl,
                 &m.body,
                 m.span,
                 m.id,
                 (e, v));
387 388
}

389
pub fn visit_fn<E:Clone>(fk: &fn_kind, decl: &fn_decl, body: &Block, _sp: span,
390
                         _id: NodeId, (e, v): (E, vt<E>)) {
391
    visit_fn_decl(decl, (e.clone(), v));
392
    let generics = generics_of_fn(fk);
393
    (v.visit_generics)(&generics, (e.clone(), v));
394
    (v.visit_block)(body, (e, v));
M
Marijn Haverbeke 已提交
395 396
}

397
pub fn visit_ty_method<E:Clone>(m: &TypeMethod, (e, v): (E, vt<E>)) {
398
    foreach a in m.decl.inputs.iter() {
399 400 401
        (v.visit_ty)(&a.ty, (e.clone(), v));
    }
    (v.visit_generics)(&m.generics, (e.clone(), v));
J
James Miller 已提交
402
    (v.visit_ty)(&m.decl.output, (e, v));
403 404
}

405
pub fn visit_trait_method<E:Clone>(m: &trait_method, (e, v): (E, vt<E>)) {
406
    match *m {
407 408
      required(ref ty_m) => (v.visit_ty_method)(ty_m, (e, v)),
      provided(m) => visit_method_helper(m, (e, v))
409 410 411
    }
}

412
pub fn visit_struct_def<E:Clone>(
413 414
    sd: @struct_def,
    _nm: ast::ident,
E
Erick Tryzelaar 已提交
415
    _generics: &Generics,
416
    _id: NodeId,
417
    (e, v): (E, vt<E>)
418
) {
419
    foreach f in sd.fields.iter() {
420
        (v.visit_struct_field)(*f, (e.clone(), v));
421
    }
422 423
}

424
pub fn visit_struct_field<E:Clone>(sf: &struct_field, (e, v): (E, vt<E>)) {
J
James Miller 已提交
425
    (v.visit_ty)(&sf.node.ty, (e, v));
426 427
}

428
pub fn visit_block<E:Clone>(b: &Block, (e, v): (E, vt<E>)) {
429
    foreach vi in b.view_items.iter() {
430
        (v.visit_view_item)(vi, (e.clone(), v));
431
    }
432
    foreach s in b.stmts.iter() {
433
        (v.visit_stmt)(*s, (e.clone(), v));
434
    }
435
    visit_expr_opt(b.expr, (e, v));
M
Marijn Haverbeke 已提交
436 437
}

438
pub fn visit_stmt<E>(s: &stmt, (e, v): (E, vt<E>)) {
439
    match s.node {
440 441 442 443
      stmt_decl(d, _) => (v.visit_decl)(d, (e, v)),
      stmt_expr(ex, _) => (v.visit_expr)(ex, (e, v)),
      stmt_semi(ex, _) => (v.visit_expr)(ex, (e, v)),
      stmt_mac(ref mac, _) => visit_mac(mac, (e, v))
M
Marijn Haverbeke 已提交
444 445 446
    }
}

447
pub fn visit_decl<E:Clone>(d: &decl, (e, v): (E, vt<E>)) {
448
    match d.node {
449 450
        decl_local(ref loc) => (v.visit_local)(*loc, (e, v)),
        decl_item(it) => (v.visit_item)(it, (e, v))
M
Marijn Haverbeke 已提交
451 452 453
    }
}

454 455
pub fn visit_expr_opt<E>(eo: Option<@expr>, (e, v): (E, vt<E>)) {
    match eo { None => (), Some(ex) => (v.visit_expr)(ex, (e, v)) }
M
Marijn Haverbeke 已提交
456 457
}

458
pub fn visit_exprs<E:Clone>(exprs: &[@expr], (e, v): (E, vt<E>)) {
459
    foreach ex in exprs.iter() { (v.visit_expr)(*ex, (e.clone(), v)); }
M
Marijn Haverbeke 已提交
460 461
}

462
pub fn visit_mac<E>(_m: &mac, (_e, _v): (E, vt<E>)) {
463
    /* no user-serviceable parts inside */
464 465
}

466
pub fn visit_expr<E:Clone>(ex: @expr, (e, v): (E, vt<E>)) {
467
    match ex.node {
468 469
        expr_vstore(x, _) => (v.visit_expr)(x, (e.clone(), v)),
        expr_vec(ref es, _) => visit_exprs(*es, (e.clone(), v)),
470
        expr_repeat(element, count, _) => {
471 472
            (v.visit_expr)(element, (e.clone(), v));
            (v.visit_expr)(count, (e.clone(), v));
473
        }
J
James Miller 已提交
474
        expr_struct(ref p, ref flds, base) => {
475
            visit_path(p, (e.clone(), v));
476
            foreach f in flds.iter() {
477
                (v.visit_expr)(f.expr, (e.clone(), v));
478
            }
479
            visit_expr_opt(base, (e.clone(), v));
480 481
        }
        expr_tup(ref elts) => {
482
            foreach el in elts.iter() { (v.visit_expr)(*el, (e.clone(), v)) }
483 484
        }
        expr_call(callee, ref args, _) => {
485 486
            visit_exprs(*args, (e.clone(), v));
            (v.visit_expr)(callee, (e.clone(), v));
487
        }
488
        expr_method_call(_, callee, _, ref tys, ref args, _) => {
489
            visit_exprs(*args, (e.clone(), v));
490
            foreach tp in tys.iter() {
491
                (v.visit_ty)(tp, (e.clone(), v));
492
            }
493
            (v.visit_expr)(callee, (e.clone(), v));
494
        }
495
        expr_binary(_, _, a, b) => {
496 497
            (v.visit_expr)(a, (e.clone(), v));
            (v.visit_expr)(b, (e.clone(), v));
498
        }
499
        expr_addr_of(_, x) | expr_unary(_, _, x) |
500
        expr_loop_body(x) | expr_do_body(x) => (v.visit_expr)(x, (e.clone(), v)),
501
        expr_lit(_) => (),
J
James Miller 已提交
502
        expr_cast(x, ref t) => {
503 504
            (v.visit_expr)(x, (e.clone(), v));
            (v.visit_ty)(t, (e.clone(), v));
505 506
        }
        expr_if(x, ref b, eo) => {
507 508 509
            (v.visit_expr)(x, (e.clone(), v));
            (v.visit_block)(b, (e.clone(), v));
            visit_expr_opt(eo, (e.clone(), v));
510 511
        }
        expr_while(x, ref b) => {
512 513
            (v.visit_expr)(x, (e.clone(), v));
            (v.visit_block)(b, (e.clone(), v));
514
        }
515 516 517 518 519
        expr_for_loop(pat, iter, ref b) => {
            (v.visit_pat)(pat, (e.clone(), v));
            (v.visit_expr)(iter, (e.clone(), v));
            (v.visit_block)(b, (e.clone(), v));
        }
520
        expr_loop(ref b, _) => (v.visit_block)(b, (e.clone(), v)),
521
        expr_match(x, ref arms) => {
522
            (v.visit_expr)(x, (e.clone(), v));
523
            foreach a in arms.iter() { (v.visit_arm)(a, (e.clone(), v)); }
524 525 526 527 528 529 530 531
        }
        expr_fn_block(ref decl, ref body) => {
            (v.visit_fn)(
                &fk_fn_block,
                decl,
                body,
                ex.span,
                ex.id,
532
                (e.clone(), v)
533 534
            );
        }
535
        expr_block(ref b) => (v.visit_block)(b, (e.clone(), v)),
536
        expr_assign(a, b) => {
537 538
            (v.visit_expr)(b, (e.clone(), v));
            (v.visit_expr)(a, (e.clone(), v));
539
        }
540
        expr_assign_op(_, _, a, b) => {
541 542
            (v.visit_expr)(b, (e.clone(), v));
            (v.visit_expr)(a, (e.clone(), v));
543 544
        }
        expr_field(x, _, ref tys) => {
545
            (v.visit_expr)(x, (e.clone(), v));
546
            foreach tp in tys.iter() {
547
                (v.visit_ty)(tp, (e.clone(), v));
548
            }
549
        }
550
        expr_index(_, a, b) => {
551 552
            (v.visit_expr)(a, (e.clone(), v));
            (v.visit_expr)(b, (e.clone(), v));
553
        }
554
        expr_path(ref p) => visit_path(p, (e.clone(), v)),
555
        expr_self => (),
556 557
        expr_break(_) => (),
        expr_again(_) => (),
558
        expr_ret(eo) => visit_expr_opt(eo, (e.clone(), v)),
559
        expr_log(lv, x) => {
560 561
            (v.visit_expr)(lv, (e.clone(), v));
            (v.visit_expr)(x, (e.clone(), v));
562
        }
563 564
        expr_mac(ref mac) => visit_mac(mac, (e.clone(), v)),
        expr_paren(x) => (v.visit_expr)(x, (e.clone(), v)),
565
        expr_inline_asm(ref a) => {
566
            foreach &(_, input) in a.inputs.iter() {
567
                (v.visit_expr)(input, (e.clone(), v));
568
            }
569
            foreach &(_, out) in a.outputs.iter() {
570
                (v.visit_expr)(out, (e.clone(), v));
571 572
            }
        }
M
Marijn Haverbeke 已提交
573
    }
574
    (v.visit_expr_post)(ex, (e, v));
M
Marijn Haverbeke 已提交
575 576
}

577
pub fn visit_arm<E:Clone>(a: &arm, (e, v): (E, vt<E>)) {
578
    foreach p in a.pats.iter() { (v.visit_pat)(*p, (e.clone(), v)); }
579 580
    visit_expr_opt(a.guard, (e.clone(), v));
    (v.visit_block)(&a.body, (e.clone(), v));
M
Marijn Haverbeke 已提交
581
}
582

583 584 585
// Simpler, non-context passing interface. Always walks the whole tree, simply
// calls the given functions on the nodes.

586
pub struct SimpleVisitor {
587
    visit_mod: @fn(&_mod, span, NodeId),
J
James Miller 已提交
588
    visit_view_item: @fn(&view_item),
589 590
    visit_foreign_item: @fn(@foreign_item),
    visit_item: @fn(@item),
591 592
    visit_local: @fn(@Local),
    visit_block: @fn(&Block),
593 594 595 596 597 598
    visit_stmt: @fn(@stmt),
    visit_arm: @fn(&arm),
    visit_pat: @fn(@pat),
    visit_decl: @fn(@decl),
    visit_expr: @fn(@expr),
    visit_expr_post: @fn(@expr),
J
James Miller 已提交
599
    visit_ty: @fn(&Ty),
600
    visit_generics: @fn(&Generics),
601 602
    visit_fn: @fn(&fn_kind, &fn_decl, &Block, span, NodeId),
    visit_ty_method: @fn(&TypeMethod),
603
    visit_trait_method: @fn(&trait_method),
604
    visit_struct_def: @fn(@struct_def, ident, &Generics, NodeId),
605 606
    visit_struct_field: @fn(@struct_field),
    visit_struct_method: @fn(@method)
607 608
}

609
pub type simple_visitor = @SimpleVisitor;
610

J
James Miller 已提交
611
pub fn simple_ignore_ty(_t: &Ty) {}
612

613
pub fn default_simple_visitor() -> @SimpleVisitor {
E
Erick Tryzelaar 已提交
614 615 616 617 618 619 620 621 622 623 624 625 626 627
    @SimpleVisitor {
        visit_mod: |_m, _sp, _id| { },
        visit_view_item: |_vi| { },
        visit_foreign_item: |_ni| { },
        visit_item: |_i| { },
        visit_local: |_l| { },
        visit_block: |_b| { },
        visit_stmt: |_s| { },
        visit_arm: |_a| { },
        visit_pat: |_p| { },
        visit_decl: |_d| { },
        visit_expr: |_e| { },
        visit_expr_post: |_e| { },
        visit_ty: simple_ignore_ty,
628 629 630 631 632 633 634
        visit_generics: |_| {},
        visit_fn: |_, _, _, _, _| {},
        visit_ty_method: |_| {},
        visit_trait_method: |_| {},
        visit_struct_def: |_, _, _, _| {},
        visit_struct_field: |_| {},
        visit_struct_method: |_| {},
E
Erick Tryzelaar 已提交
635
    }
636 637
}

638
pub fn mk_simple_visitor(v: simple_visitor) -> vt<()> {
639
    fn v_mod(
640
        f: @fn(&_mod, span, NodeId),
641 642
        m: &_mod,
        sp: span,
643
        id: NodeId,
644
        (e, v): ((), vt<()>)
645
    ) {
646
        f(m, sp, id);
647
        visit_mod(m, sp, id, (e, v));
648
    }
J
James Miller 已提交
649
    fn v_view_item(f: @fn(&view_item), vi: &view_item, (e, v): ((), vt<()>)) {
M
Marijn Haverbeke 已提交
650
        f(vi);
651
        visit_view_item(vi, (e, v));
652
    }
653
    fn v_foreign_item(f: @fn(@foreign_item), ni: @foreign_item, (e, v): ((), vt<()>)) {
M
Marijn Haverbeke 已提交
654
        f(ni);
655
        visit_foreign_item(ni, (e, v));
656
    }
657
    fn v_item(f: @fn(@item), i: @item, (e, v): ((), vt<()>)) {
M
Marijn Haverbeke 已提交
658
        f(i);
659
        visit_item(i, (e, v));
660
    }
661
    fn v_local(f: @fn(@Local), l: @Local, (e, v): ((), vt<()>)) {
M
Marijn Haverbeke 已提交
662
        f(l);
663
        visit_local(l, (e, v));
664
    }
665
    fn v_block(f: @fn(&ast::Block), bl: &ast::Block, (e, v): ((), vt<()>)) {
M
Marijn Haverbeke 已提交
666
        f(bl);
667
        visit_block(bl, (e, v));
668
    }
669
    fn v_stmt(f: @fn(@stmt), st: @stmt, (e, v): ((), vt<()>)) {
M
Marijn Haverbeke 已提交
670
        f(st);
671
        visit_stmt(st, (e, v));
672
    }
673
    fn v_arm(f: @fn(&arm), a: &arm, (e, v): ((), vt<()>)) {
M
Marijn Haverbeke 已提交
674
        f(a);
675
        visit_arm(a, (e, v));
676
    }
677
    fn v_pat(f: @fn(@pat), p: @pat, (e, v): ((), vt<()>)) {
M
Marijn Haverbeke 已提交
678
        f(p);
679
        visit_pat(p, (e, v));
680
    }
681
    fn v_decl(f: @fn(@decl), d: @decl, (e, v): ((), vt<()>)) {
M
Marijn Haverbeke 已提交
682
        f(d);
683
        visit_decl(d, (e, v));
684
    }
685
    fn v_expr(f: @fn(@expr), ex: @expr, (e, v): ((), vt<()>)) {
M
Marijn Haverbeke 已提交
686
        f(ex);
687
        visit_expr(ex, (e, v));
688
    }
689
    fn v_expr_post(f: @fn(@expr), ex: @expr, (_e, _v): ((), vt<()>)) {
690 691
        f(ex);
    }
J
James Miller 已提交
692
    fn v_ty(f: @fn(&Ty), ty: &Ty, (e, v): ((), vt<()>)) {
M
Marijn Haverbeke 已提交
693
        f(ty);
694
        visit_ty(ty, (e, v));
695
    }
696
    fn v_ty_method(f: @fn(&TypeMethod), ty: &TypeMethod, (e, v): ((), vt<()>)) {
697
        f(ty);
698
        visit_ty_method(ty, (e, v));
699
    }
700 701
    fn v_trait_method(f: @fn(&trait_method),
                      m: &trait_method,
702
                      (e, v): ((), vt<()>)) {
703
        f(m);
704
        visit_trait_method(m, (e, v));
705
    }
706
    fn v_struct_def(
707
        f: @fn(@struct_def, ident, &Generics, NodeId),
708 709 710
        sd: @struct_def,
        nm: ident,
        generics: &Generics,
711
        id: NodeId,
712
        (e, v): ((), vt<()>)
713
    ) {
714
        f(sd, nm, generics, id);
715
        visit_struct_def(sd, nm, generics, id, (e, v));
716
    }
717
    fn v_generics(
718
        f: @fn(&Generics),
719
        ps: &Generics,
720
        (e, v): ((), vt<()>)
721
    ) {
722
        f(ps);
723
        visit_generics(ps, (e, v));
724
    }
725
    fn v_fn(
726
        f: @fn(&fn_kind, &fn_decl, &Block, span, NodeId),
727 728
        fk: &fn_kind,
        decl: &fn_decl,
729
        body: &Block,
730
        sp: span,
731
        id: NodeId,
732
        (e, v): ((), vt<()>)
733
    ) {
734
        f(fk, decl, body, sp, id);
735
        visit_fn(fk, decl, body, sp, id, (e, v));
736
    }
J
James Miller 已提交
737
    let visit_ty: @fn(&Ty, ((), vt<()>)) =
738 739
        |a,b| v_ty(v.visit_ty, a, b);
    fn v_struct_field(f: @fn(@struct_field), sf: @struct_field, (e, v): ((), vt<()>)) {
740
        f(sf);
741
        visit_struct_field(sf, (e, v));
742
    }
743
    return mk_vt(@Visitor {
744 745
        visit_mod: |a,b,c,d|v_mod(v.visit_mod, a, b, c, d),
        visit_view_item: |a,b| v_view_item(v.visit_view_item, a, b),
746
        visit_foreign_item:
747 748 749 750 751 752 753 754 755 756
            |a,b|v_foreign_item(v.visit_foreign_item, a, b),
        visit_item: |a,b|v_item(v.visit_item, a, b),
        visit_local: |a,b|v_local(v.visit_local, a, b),
        visit_block: |a,b|v_block(v.visit_block, a, b),
        visit_stmt: |a,b|v_stmt(v.visit_stmt, a, b),
        visit_arm: |a,b|v_arm(v.visit_arm, a, b),
        visit_pat: |a,b|v_pat(v.visit_pat, a, b),
        visit_decl: |a,b|v_decl(v.visit_decl, a, b),
        visit_expr: |a,b|v_expr(v.visit_expr, a, b),
        visit_expr_post: |a,b| v_expr_post(v.visit_expr_post, a, b),
757
        visit_ty: visit_ty,
758 759 760 761 762 763 764 765 766 767 768 769
        visit_generics: |a,b|
            v_generics(v.visit_generics, a, b),
        visit_fn: |a,b,c,d,e,f|
            v_fn(v.visit_fn, a, b, c, d, e, f),
        visit_ty_method: |a,b|
            v_ty_method(v.visit_ty_method, a, b),
        visit_trait_method: |a,b|
            v_trait_method(v.visit_trait_method, a, b),
        visit_struct_def: |a,b,c,d,e|
            v_struct_def(v.visit_struct_def, a, b, c, d, e),
        visit_struct_field: |a,b|
            v_struct_field(v.visit_struct_field, a, b),
770
    });
771
}