@@ -119,9 +119,8 @@ use syntax::ast::*;
119
119
use syntax:: codemap:: span;
120
120
use syntax:: parse:: token:: special_idents;
121
121
use syntax:: print:: pprust:: { expr_to_str, block_to_str} ;
122
- use syntax:: oldvisit:: { fk_anon, fk_fn_block, fk_item_fn, fk_method} ;
123
- use syntax:: oldvisit:: { vt} ;
124
- use syntax:: { oldvisit, ast_util} ;
122
+ use syntax:: { visit, ast_util} ;
123
+ use syntax:: visit:: { Visitor , fn_kind} ;
125
124
126
125
#[ deriving( Eq ) ]
127
126
struct Variable ( uint ) ;
@@ -152,22 +151,27 @@ fn live_node_kind_to_str(lnk: LiveNodeKind, cx: ty::ctxt) -> ~str {
152
151
}
153
152
}
154
153
154
+ struct LivenessVisitor ;
155
+
156
+ impl Visitor < @mut IrMaps > for LivenessVisitor {
157
+ fn visit_fn ( & mut self , fk : & fn_kind , fd : & fn_decl , b : & Block , s : span , n : NodeId , e : @mut IrMaps ) {
158
+ visit_fn ( self , fk, fd, b, s, n, e) ;
159
+ }
160
+ fn visit_local ( & mut self , l : @Local , e : @mut IrMaps ) { visit_local ( self , l, e) ; }
161
+ fn visit_expr ( & mut self , ex: @expr, e : @mut IrMaps ) { visit_expr ( self , ex, e) ; }
162
+ fn visit_arm ( & mut self , a : & arm , e : @mut IrMaps ) { visit_arm ( self , a, e) ; }
163
+ }
164
+
155
165
pub fn check_crate ( tcx : ty:: ctxt ,
156
166
method_map : typeck:: method_map ,
157
167
capture_map : moves:: CaptureMap ,
158
168
crate : & Crate ) {
159
- let visitor = oldvisit:: mk_vt ( @oldvisit:: Visitor {
160
- visit_fn : visit_fn,
161
- visit_local : visit_local,
162
- visit_expr : visit_expr,
163
- visit_arm : visit_arm,
164
- .. * oldvisit:: default_visitor ( )
165
- } ) ;
169
+ let mut visitor = LivenessVisitor ;
166
170
167
171
let initial_maps = @mut IrMaps ( tcx,
168
172
method_map,
169
173
capture_map) ;
170
- oldvisit :: visit_crate ( crate , ( initial_maps , visitor ) ) ;
174
+ visit :: walk_crate ( & mut visitor , crate , initial_maps ) ;
171
175
tcx. sess . abort_if_errors ( ) ;
172
176
}
173
177
@@ -341,13 +345,30 @@ impl IrMaps {
341
345
}
342
346
}
343
347
344
- fn visit_fn ( fk : & oldvisit:: fn_kind ,
348
+ struct ErrorCheckVisitor ;
349
+
350
+ impl Visitor < @Liveness > for ErrorCheckVisitor {
351
+ fn visit_fn ( & mut self , fk : & fn_kind , fd : & fn_decl , b : & Block , s : span , n : NodeId , e : @Liveness ) {
352
+ check_fn ( self , fk, fd, b, s, n, e) ;
353
+ }
354
+ fn visit_local ( & mut self , l : @Local , e : @Liveness ) {
355
+ check_local ( self , l, e) ;
356
+ }
357
+ fn visit_expr ( & mut self , ex: @expr, e : @Liveness ) {
358
+ check_expr ( self , ex, e) ;
359
+ }
360
+ fn visit_arm ( & mut self , a : & arm , e : @Liveness ) {
361
+ check_arm ( self , a, e) ;
362
+ }
363
+ }
364
+
365
+ fn visit_fn ( v : & mut LivenessVisitor ,
366
+ fk : & visit:: fn_kind ,
345
367
decl : & fn_decl ,
346
368
body : & Block ,
347
369
sp : span ,
348
370
id : NodeId ,
349
- ( this, v) : ( @mut IrMaps ,
350
- vt < @mut IrMaps > ) ) {
371
+ this : @mut IrMaps ) {
351
372
debug ! ( "visit_fn: id=%d" , id) ;
352
373
let _i = :: util:: common:: indenter ( ) ;
353
374
@@ -371,7 +392,7 @@ fn visit_fn(fk: &oldvisit::fn_kind,
371
392
372
393
// Add `this`, whether explicit or implicit.
373
394
match * fk {
374
- fk_method( _, _, method) => {
395
+ visit :: fk_method( _, _, method) => {
375
396
match method. explicit_self . node {
376
397
sty_value | sty_region( * ) | sty_box( _) | sty_uniq => {
377
398
fn_maps. add_variable ( Arg ( method. self_id ,
@@ -380,12 +401,12 @@ fn visit_fn(fk: &oldvisit::fn_kind,
380
401
sty_static => { }
381
402
}
382
403
}
383
- fk_item_fn( * ) | fk_anon( * ) | fk_fn_block( * ) => { }
404
+ visit :: fk_item_fn( * ) | visit :: fk_anon( * ) | visit :: fk_fn_block( * ) => { }
384
405
}
385
406
386
407
// gather up the various local variables, significant expressions,
387
408
// and so forth:
388
- oldvisit :: visit_fn ( fk, decl, body, sp, id, ( fn_maps, v ) ) ;
409
+ visit :: walk_fn ( v , fk, decl, body, sp, id, fn_maps) ;
389
410
390
411
// Special nodes and variables:
391
412
// - exit_ln represents the end of the fn, either by return or fail
@@ -402,19 +423,13 @@ fn visit_fn(fk: &oldvisit::fn_kind,
402
423
let entry_ln = ( * lsets) . compute ( decl, body) ;
403
424
404
425
// check for various error conditions
405
- let check_vt = oldvisit:: mk_vt ( @oldvisit:: Visitor {
406
- visit_fn : check_fn,
407
- visit_local : check_local,
408
- visit_expr : check_expr,
409
- visit_arm : check_arm,
410
- .. * oldvisit:: default_visitor ( )
411
- } ) ;
412
- ( check_vt. visit_block ) ( body, ( lsets, check_vt) ) ;
426
+ let mut check_vt = ErrorCheckVisitor ;
427
+ check_vt. visit_block ( body, lsets) ;
413
428
lsets. check_ret ( id, sp, fk, entry_ln) ;
414
429
lsets. warn_about_unused_args ( decl, entry_ln) ;
415
430
}
416
431
417
- fn visit_local( local : @Local , ( this, vt ) : ( @mut IrMaps , vt < @ mut IrMaps > ) ) {
432
+ fn visit_local( v : & mut LivenessVisitor , local : @Local , this : @mut IrMaps ) {
418
433
let def_map = this. tcx . def_map ;
419
434
do pat_util:: pat_bindings ( def_map, local. pat ) |_bm, p_id, sp, path| {
420
435
debug ! ( "adding local variable %d" , p_id) ;
@@ -431,10 +446,10 @@ fn visit_local(local: @Local, (this, vt): (@mut IrMaps, vt<@mut IrMaps>)) {
431
446
kind : kind
432
447
} ) ) ;
433
448
}
434
- oldvisit :: visit_local ( local , ( this , vt ) ) ;
449
+ visit :: walk_local ( v , local , this ) ;
435
450
}
436
451
437
- fn visit_arm ( arm : & arm , ( this, vt ) : ( @mut IrMaps , vt < @ mut IrMaps > ) ) {
452
+ fn visit_arm ( v : & mut LivenessVisitor , arm : & arm , this : @mut IrMaps ) {
438
453
let def_map = this. tcx . def_map ;
439
454
for pat in arm. pats . iter ( ) {
440
455
do pat_util:: pat_bindings ( def_map, * pat) |bm, p_id, sp, path| {
@@ -450,10 +465,10 @@ fn visit_arm(arm: &arm, (this, vt): (@mut IrMaps, vt<@mut IrMaps>)) {
450
465
} ) ) ;
451
466
}
452
467
}
453
- oldvisit :: visit_arm ( arm , ( this , vt ) ) ;
468
+ visit :: walk_arm ( v , arm , this ) ;
454
469
}
455
470
456
- fn visit_expr ( expr : @expr, ( this , vt ) : ( @mut IrMaps , vt < @ mut IrMaps > ) ) {
471
+ fn visit_expr ( v : & mut LivenessVisitor , expr: @expr, this : @mut IrMaps ) {
457
472
match expr. node {
458
473
// live nodes required for uses or definitions of variables:
459
474
expr_path( _) | expr_self => {
@@ -462,7 +477,7 @@ fn visit_expr(expr: @expr, (this, vt): (@mut IrMaps, vt<@mut IrMaps>)) {
462
477
if moves:: moved_variable_node_id_from_def ( def) . is_some ( ) {
463
478
this. add_live_node_for_node ( expr. id , ExprNode ( expr. span ) ) ;
464
479
}
465
- oldvisit :: visit_expr ( expr , ( this , vt ) ) ;
480
+ visit :: walk_expr ( v , expr , this ) ;
466
481
}
467
482
expr_fn_block( * ) => {
468
483
// Interesting control flow (for loops can contain labeled
@@ -495,18 +510,18 @@ fn visit_expr(expr: @expr, (this, vt): (@mut IrMaps, vt<@mut IrMaps>)) {
495
510
}
496
511
this. set_captures ( expr. id , call_caps) ;
497
512
498
- oldvisit :: visit_expr ( expr , ( this , vt ) ) ;
513
+ visit :: walk_expr ( v , expr , this ) ;
499
514
}
500
515
501
516
// live nodes required for interesting control flow:
502
517
expr_if( * ) | expr_match( * ) | expr_while( * ) | expr_loop( * ) => {
503
518
this. add_live_node_for_node ( expr. id , ExprNode ( expr. span ) ) ;
504
- oldvisit :: visit_expr ( expr , ( this , vt ) ) ;
519
+ visit :: walk_expr ( v , expr , this ) ;
505
520
}
506
521
expr_for_loop( * ) => fail ! ( "non-desugared expr_for_loop" ) ,
507
522
expr_binary( _, op, _, _) if ast_util:: lazy_binop ( op) => {
508
523
this. add_live_node_for_node ( expr. id , ExprNode ( expr. span ) ) ;
509
- oldvisit :: visit_expr ( expr , ( this , vt ) ) ;
524
+ visit :: walk_expr ( v , expr , this ) ;
510
525
}
511
526
512
527
// otherwise, live nodes are not required:
@@ -518,7 +533,7 @@ fn visit_expr(expr: @expr, (this, vt): (@mut IrMaps, vt<@mut IrMaps>)) {
518
533
expr_assign( * ) | expr_assign_op( * ) | expr_mac( * ) |
519
534
expr_struct( * ) | expr_repeat( * ) | expr_paren( * ) |
520
535
expr_inline_asm( * ) => {
521
- oldvisit :: visit_expr ( expr , ( this , vt ) ) ;
536
+ visit :: walk_expr ( v , expr , this ) ;
522
537
}
523
538
}
524
539
}
@@ -1408,7 +1423,7 @@ impl Liveness {
1408
1423
// _______________________________________________________________________
1409
1424
// Checking for error conditions
1410
1425
1411
- fn check_local( local : @Local , ( this, vt ) : ( @Liveness , vt < @ Liveness > ) ) {
1426
+ fn check_local( vt : & mut ErrorCheckVisitor , local : @Local , this : @Liveness ) {
1412
1427
match local. init {
1413
1428
Some ( _) => {
1414
1429
this. warn_about_unused_or_dead_vars_in_pat ( local. pat ) ;
@@ -1434,34 +1449,34 @@ fn check_local(local: @Local, (this, vt): (@Liveness, vt<@Liveness>)) {
1434
1449
}
1435
1450
}
1436
1451
1437
- oldvisit :: visit_local ( local , ( this , vt ) ) ;
1452
+ visit :: walk_local ( vt , local , this ) ;
1438
1453
}
1439
1454
1440
- fn check_arm( arm : & arm , ( this, vt ) : ( @Liveness , vt < @ Liveness > ) ) {
1455
+ fn check_arm( vt : & mut ErrorCheckVisitor , arm : & arm , this : @Liveness ) {
1441
1456
do this. arm_pats_bindings ( arm. pats ) |ln, var, sp, id| {
1442
1457
this. warn_about_unused ( sp, id, ln, var) ;
1443
1458
}
1444
- oldvisit :: visit_arm ( arm , ( this , vt ) ) ;
1459
+ visit :: walk_arm ( vt , arm , this ) ;
1445
1460
}
1446
1461
1447
- fn check_expr ( expr : @expr, ( this , vt ) : ( @Liveness , vt < @ Liveness > ) ) {
1462
+ fn check_expr ( vt : & mut ErrorCheckVisitor , expr: @expr, this : @Liveness ) {
1448
1463
match expr. node {
1449
1464
expr_assign( l, r) => {
1450
1465
this. check_lvalue ( l, vt) ;
1451
- ( vt. visit_expr ) ( r, ( this, vt ) ) ;
1466
+ vt. visit_expr ( r, this) ;
1452
1467
1453
- oldvisit :: visit_expr ( expr , ( this , vt ) ) ;
1468
+ visit :: walk_expr ( vt , expr , this ) ;
1454
1469
}
1455
1470
1456
1471
expr_assign_op( _, _, l, _) => {
1457
1472
this. check_lvalue ( l, vt) ;
1458
1473
1459
- oldvisit :: visit_expr ( expr , ( this , vt ) ) ;
1474
+ visit :: walk_expr ( vt , expr , this ) ;
1460
1475
}
1461
1476
1462
1477
expr_inline_asm( ref ia) => {
1463
1478
for & ( _, input) in ia. inputs . iter ( ) {
1464
- ( vt. visit_expr ) ( input, ( this, vt ) ) ;
1479
+ vt. visit_expr ( input, this) ;
1465
1480
}
1466
1481
1467
1482
// Output operands must be lvalues
@@ -1472,10 +1487,10 @@ fn check_expr(expr: @expr, (this, vt): (@Liveness, vt<@Liveness>)) {
1472
1487
}
1473
1488
_ => { }
1474
1489
}
1475
- ( vt. visit_expr ) ( out, ( this, vt ) ) ;
1490
+ vt. visit_expr ( out, this) ;
1476
1491
}
1477
1492
1478
- oldvisit :: visit_expr ( expr , ( this , vt ) ) ;
1493
+ visit :: walk_expr ( vt , expr , this ) ;
1479
1494
}
1480
1495
1481
1496
// no correctness conditions related to liveness
@@ -1487,18 +1502,19 @@ fn check_expr(expr: @expr, (this, vt): (@Liveness, vt<@Liveness>)) {
1487
1502
expr_again( * ) | expr_lit( _) | expr_block( * ) |
1488
1503
expr_mac( * ) | expr_addr_of( * ) | expr_struct( * ) | expr_repeat( * ) |
1489
1504
expr_paren( * ) | expr_fn_block( * ) | expr_path( * ) | expr_self( * ) => {
1490
- oldvisit :: visit_expr ( expr , ( this , vt ) ) ;
1505
+ visit :: walk_expr ( vt , expr , this ) ;
1491
1506
}
1492
1507
expr_for_loop( * ) => fail ! ( "non-desugared expr_for_loop" )
1493
1508
}
1494
1509
}
1495
1510
1496
- fn check_fn ( _fk : & oldvisit:: fn_kind ,
1511
+ fn check_fn ( _v : & mut ErrorCheckVisitor ,
1512
+ _fk : & visit:: fn_kind ,
1497
1513
_decl : & fn_decl ,
1498
1514
_body : & Block ,
1499
1515
_sp : span ,
1500
1516
_id : NodeId ,
1501
- ( _self, _v ) : ( @Liveness , vt < @ Liveness > ) ) {
1517
+ _self : @Liveness ) {
1502
1518
// do not check contents of nested fns
1503
1519
}
1504
1520
@@ -1513,7 +1529,7 @@ impl Liveness {
1513
1529
pub fn check_ret ( & self ,
1514
1530
id : NodeId ,
1515
1531
sp : span ,
1516
- _fk : & oldvisit :: fn_kind ,
1532
+ _fk : & visit :: fn_kind ,
1517
1533
entry_ln : LiveNode ) {
1518
1534
if self . live_on_entry ( entry_ln, self . s . no_ret_var ) . is_some ( ) {
1519
1535
// if no_ret_var is live, then we fall off the end of the
@@ -1533,7 +1549,7 @@ impl Liveness {
1533
1549
}
1534
1550
}
1535
1551
1536
- pub fn check_lvalue ( @self , expr: @expr, vt : vt < @ Liveness > ) {
1552
+ pub fn check_lvalue ( @self , expr: @expr, vt : & mut ErrorCheckVisitor ) {
1537
1553
match expr. node {
1538
1554
expr_path( _) => {
1539
1555
match self . tcx . def_map . get_copy ( & expr. id ) {
@@ -1562,7 +1578,7 @@ impl Liveness {
1562
1578
_ => {
1563
1579
// For other kinds of lvalues, no checks are required,
1564
1580
// and any embedded expressions are actually rvalues
1565
- oldvisit :: visit_expr ( expr , ( self , vt ) ) ;
1581
+ visit :: walk_expr ( vt , expr , self ) ;
1566
1582
}
1567
1583
}
1568
1584
}
0 commit comments