@@ -30,7 +30,10 @@ use rustc_borrowck::graphviz as borrowck_dot;
30
30
31
31
use syntax:: ast;
32
32
use syntax:: ast_map:: { mod, blocks, NodePrinter } ;
33
+ use syntax:: codemap;
34
+ use syntax:: fold:: { mod, Folder } ;
33
35
use syntax:: print:: { pp, pprust} ;
36
+ use syntax:: ptr:: P ;
34
37
35
38
use graphviz as dot;
36
39
@@ -42,6 +45,7 @@ use arena::TypedArena;
42
45
#[ deriving( Copy , PartialEq , Show ) ]
43
46
pub enum PpSourceMode {
44
47
PpmNormal ,
48
+ PpmEveryBodyLoops ,
45
49
PpmExpanded ,
46
50
PpmTyped ,
47
51
PpmIdentified ,
@@ -61,6 +65,7 @@ pub fn parse_pretty(sess: &Session, name: &str) -> (PpMode, Option<UserIdentifie
61
65
let opt_second = split. next ( ) ;
62
66
let first = match first {
63
67
"normal" => PpmSource ( PpmNormal ) ,
68
+ "everybody_loops" => PpmSource ( PpmEveryBodyLoops ) ,
64
69
"expanded" => PpmSource ( PpmExpanded ) ,
65
70
"typed" => PpmSource ( PpmTyped ) ,
66
71
"expanded,identified" => PpmSource ( PpmExpandedIdentified ) ,
@@ -105,7 +110,7 @@ impl PpSourceMode {
105
110
F : FnOnce ( & PrinterSupport , B ) -> A ,
106
111
{
107
112
match * self {
108
- PpmNormal | PpmExpanded => {
113
+ PpmNormal | PpmEveryBodyLoops | PpmExpanded => {
109
114
let annotation = NoAnn { sess : sess, ast_map : ast_map } ;
110
115
f ( & annotation, payload)
111
116
}
@@ -384,6 +389,7 @@ impl UserIdentifiedItem {
384
389
fn needs_ast_map ( ppm : & PpMode , opt_uii : & Option < UserIdentifiedItem > ) -> bool {
385
390
match * ppm {
386
391
PpmSource ( PpmNormal ) |
392
+ PpmSource ( PpmEveryBodyLoops ) |
387
393
PpmSource ( PpmIdentified ) => opt_uii. is_some ( ) ,
388
394
389
395
PpmSource ( PpmExpanded ) |
@@ -397,6 +403,7 @@ fn needs_ast_map(ppm: &PpMode, opt_uii: &Option<UserIdentifiedItem>) -> bool {
397
403
fn needs_expansion ( ppm : & PpMode ) -> bool {
398
404
match * ppm {
399
405
PpmSource ( PpmNormal ) |
406
+ PpmSource ( PpmEveryBodyLoops ) |
400
407
PpmSource ( PpmIdentified ) => false ,
401
408
402
409
PpmSource ( PpmExpanded ) |
@@ -407,13 +414,79 @@ fn needs_expansion(ppm: &PpMode) -> bool {
407
414
}
408
415
}
409
416
417
+ struct ReplaceBodyWithLoop {
418
+ within_static_or_const : bool ,
419
+ }
420
+
421
+ impl ReplaceBodyWithLoop {
422
+ fn new ( ) -> ReplaceBodyWithLoop {
423
+ ReplaceBodyWithLoop { within_static_or_const : false }
424
+ }
425
+ }
426
+
427
+ impl fold:: Folder for ReplaceBodyWithLoop {
428
+ fn fold_item_underscore ( & mut self , i : ast:: Item_ ) -> ast:: Item_ {
429
+ match i {
430
+ ast:: ItemStatic ( ..) | ast:: ItemConst ( ..) => {
431
+ self . within_static_or_const = true ;
432
+ let ret = fold:: noop_fold_item_underscore ( i, self ) ;
433
+ self . within_static_or_const = false ;
434
+ return ret;
435
+ }
436
+ _ => {
437
+ fold:: noop_fold_item_underscore ( i, self )
438
+ }
439
+ }
440
+ }
441
+
442
+
443
+ fn fold_block ( & mut self , b : P < ast:: Block > ) -> P < ast:: Block > {
444
+ fn expr_to_block ( rules : ast:: BlockCheckMode ,
445
+ e : Option < P < ast:: Expr > > ) -> P < ast:: Block > {
446
+ P ( ast:: Block {
447
+ expr : e,
448
+ view_items : vec ! [ ] , stmts : vec ! [ ] , rules : rules,
449
+ id : ast:: DUMMY_NODE_ID , span : codemap:: DUMMY_SP ,
450
+ } )
451
+ }
452
+
453
+ if !self . within_static_or_const {
454
+
455
+ let empty_block = expr_to_block ( ast:: DefaultBlock , None ) ;
456
+ let loop_expr = P ( ast:: Expr {
457
+ node : ast:: ExprLoop ( empty_block, None ) ,
458
+ id : ast:: DUMMY_NODE_ID , span : codemap:: DUMMY_SP
459
+ } ) ;
460
+
461
+ expr_to_block ( b. rules , Some ( loop_expr) )
462
+
463
+ } else {
464
+ fold:: noop_fold_block ( b, self )
465
+ }
466
+ }
467
+
468
+ // in general the pretty printer processes unexpanded code, so
469
+ // we override the default `fold_mac` method which panics.
470
+ fn fold_mac ( & mut self , _macro : ast:: Mac ) -> ast:: Mac {
471
+ fold:: noop_fold_mac ( _macro, self )
472
+ }
473
+ }
474
+
410
475
pub fn pretty_print_input ( sess : Session ,
411
476
cfg : ast:: CrateConfig ,
412
477
input : & Input ,
413
478
ppm : PpMode ,
414
479
opt_uii : Option < UserIdentifiedItem > ,
415
480
ofile : Option < Path > ) {
416
481
let krate = driver:: phase_1_parse_input ( & sess, cfg, input) ;
482
+
483
+ let krate = if let PpmSource ( PpmEveryBodyLoops ) = ppm {
484
+ let mut fold = ReplaceBodyWithLoop :: new ( ) ;
485
+ fold. fold_crate ( krate)
486
+ } else {
487
+ krate
488
+ } ;
489
+
417
490
let id = link:: find_crate_name ( Some ( & sess) , krate. attrs . as_slice ( ) , input) ;
418
491
419
492
let is_expanded = needs_expansion ( & ppm) ;
0 commit comments