2
2
3
3
use rustc_ast as ast;
4
4
use rustc_ast_pretty:: pprust as pprust_ast;
5
- use rustc_errors:: ErrorGuaranteed ;
6
5
use rustc_hir as hir;
7
6
use rustc_hir_pretty as pprust_hir;
8
7
use rustc_middle:: hir:: map as hir_map;
@@ -310,106 +309,92 @@ fn write_or_print(out: &str, sess: &Session) {
310
309
sess. io . output_file . as_ref ( ) . unwrap_or ( & OutFileName :: Stdout ) . overwrite ( out, sess) ;
311
310
}
312
311
313
- pub fn print_after_parsing ( sess : & Session , krate : & ast:: Crate , ppm : PpMode ) {
314
- let ( src, src_name) = get_source ( sess) ;
312
+ // Extra data for pretty-printing, the form of which depends on what kind of
313
+ // pretty-printing we are doing.
314
+ pub enum PrintExtra < ' tcx > {
315
+ AfterParsing { krate : ast:: Crate } ,
316
+ NeedsAstMap { tcx : TyCtxt < ' tcx > } ,
317
+ }
315
318
316
- let out = match ppm {
317
- Source ( s) => {
318
- // Silently ignores an identified node.
319
- call_with_pp_support_ast ( & s, sess, None , move |annotation| {
320
- debug ! ( "pretty printing source code {:?}" , s) ;
321
- let sess = annotation. sess ( ) ;
322
- let parse = & sess. parse_sess ;
323
- pprust_ast:: print_crate (
324
- sess. source_map ( ) ,
325
- krate,
326
- src_name,
327
- src,
328
- annotation,
329
- false ,
330
- parse. edition ,
331
- & sess. parse_sess . attr_id_generator ,
332
- )
333
- } )
319
+ impl < ' tcx > PrintExtra < ' tcx > {
320
+ fn with_krate < F , R > ( & self , f : F ) -> R
321
+ where
322
+ F : FnOnce ( & ast:: Crate ) -> R
323
+ {
324
+ match self {
325
+ PrintExtra :: AfterParsing { krate, .. } => f ( krate) ,
326
+ PrintExtra :: NeedsAstMap { tcx } => f ( & tcx. resolver_for_lowering ( ( ) ) . borrow ( ) . 1 ) ,
334
327
}
335
- AstTree => {
336
- debug ! ( "pretty printing AST tree" ) ;
337
- format ! ( "{krate:#?}" )
338
- }
339
- _ => unreachable ! ( ) ,
340
- } ;
328
+ }
341
329
342
- write_or_print ( & out, sess) ;
330
+ fn tcx ( & self ) -> TyCtxt < ' tcx > {
331
+ match self {
332
+ PrintExtra :: AfterParsing { .. } => panic ! ( "PrintExtra::tcx" ) ,
333
+ PrintExtra :: NeedsAstMap { tcx } => * tcx,
334
+ }
335
+ }
343
336
}
344
337
345
- pub fn print_after_hir_lowering < ' tcx > ( tcx : TyCtxt < ' tcx > , ppm : PpMode ) {
338
+ pub fn print < ' tcx > ( sess : & Session , ppm : PpMode , ex : PrintExtra < ' tcx > ) {
346
339
if ppm. needs_analysis ( ) {
347
- abort_on_err ( print_with_analysis ( tcx, ppm) , tcx. sess ) ;
348
- return ;
340
+ abort_on_err ( ex. tcx ( ) . analysis ( ( ) ) , sess) ;
349
341
}
350
342
351
- let ( src, src_name) = get_source ( tcx . sess ) ;
343
+ let ( src, src_name) = get_source ( sess) ;
352
344
353
345
let out = match ppm {
354
346
Source ( s) => {
355
347
// Silently ignores an identified node.
356
- call_with_pp_support_ast ( & s, tcx . sess , Some ( tcx ) , move |annotation| {
348
+ call_with_pp_support_ast ( & s, sess, None , move |annotation| {
357
349
debug ! ( "pretty printing source code {:?}" , s) ;
358
350
let sess = annotation. sess ( ) ;
359
351
let parse = & sess. parse_sess ;
360
- pprust_ast:: print_crate (
361
- sess. source_map ( ) ,
362
- & tcx. resolver_for_lowering ( ( ) ) . borrow ( ) . 1 ,
363
- src_name,
364
- src,
365
- annotation,
366
- true ,
367
- parse. edition ,
368
- & sess. parse_sess . attr_id_generator ,
352
+ let is_expanded = ppm. needs_ast_map ( ) ;
353
+ ex. with_krate ( |krate|
354
+ pprust_ast:: print_crate (
355
+ sess. source_map ( ) ,
356
+ krate,
357
+ src_name,
358
+ src,
359
+ annotation,
360
+ is_expanded,
361
+ parse. edition ,
362
+ & sess. parse_sess . attr_id_generator ,
363
+ )
369
364
)
370
365
} )
371
366
}
372
-
367
+ AstTree => {
368
+ debug ! ( "pretty printing AST tree" ) ;
369
+ ex. with_krate ( |krate| format ! ( "{krate:#?}" ) )
370
+ }
373
371
AstTreeExpanded => {
374
372
debug ! ( "pretty-printing expanded AST" ) ;
375
- format ! ( "{:#?}" , tcx. resolver_for_lowering( ( ) ) . borrow( ) . 1 )
373
+ format ! ( "{:#?}" , ex . tcx( ) . resolver_for_lowering( ( ) ) . borrow( ) . 1 )
376
374
}
377
-
378
- Hir ( s) => call_with_pp_support_hir ( & s, tcx, move |annotation, hir_map| {
375
+ Hir ( s) => call_with_pp_support_hir ( & s, ex. tcx ( ) , move |annotation, hir_map| {
379
376
debug ! ( "pretty printing HIR {:?}" , s) ;
380
377
let sess = annotation. sess ( ) ;
381
378
let sm = sess. source_map ( ) ;
382
379
let attrs = |id| hir_map. attrs ( id) ;
383
380
pprust_hir:: print_crate ( sm, hir_map. root_module ( ) , src_name, src, & attrs, annotation)
384
381
} ) ,
385
-
386
382
HirTree => {
387
383
debug ! ( "pretty printing HIR tree" ) ;
388
- format ! ( "{:#?}" , tcx. hir( ) . krate( ) )
384
+ format ! ( "{:#?}" , ex . tcx( ) . hir( ) . krate( ) )
389
385
}
390
-
391
- _ => unreachable ! ( ) ,
392
- } ;
393
-
394
- write_or_print ( & out, tcx. sess ) ;
395
- }
396
-
397
- fn print_with_analysis ( tcx : TyCtxt < ' _ > , ppm : PpMode ) -> Result < ( ) , ErrorGuaranteed > {
398
- tcx. analysis ( ( ) ) ?;
399
- let out = match ppm {
400
386
Mir => {
401
387
let mut out = Vec :: new ( ) ;
402
- write_mir_pretty ( tcx, None , & mut out) . unwrap ( ) ;
388
+ write_mir_pretty ( ex . tcx ( ) , None , & mut out) . unwrap ( ) ;
403
389
String :: from_utf8 ( out) . unwrap ( )
404
390
}
405
-
406
391
MirCFG => {
407
392
let mut out = Vec :: new ( ) ;
408
- write_mir_graphviz ( tcx, None , & mut out) . unwrap ( ) ;
393
+ write_mir_graphviz ( ex . tcx ( ) , None , & mut out) . unwrap ( ) ;
409
394
String :: from_utf8 ( out) . unwrap ( )
410
395
}
411
-
412
396
ThirTree => {
397
+ let tcx = ex. tcx ( ) ;
413
398
let mut out = String :: new ( ) ;
414
399
abort_on_err ( rustc_hir_analysis:: check_crate ( tcx) , tcx. sess ) ;
415
400
debug ! ( "pretty printing THIR tree" ) ;
@@ -418,8 +403,8 @@ fn print_with_analysis(tcx: TyCtxt<'_>, ppm: PpMode) -> Result<(), ErrorGuarante
418
403
}
419
404
out
420
405
}
421
-
422
406
ThirFlat => {
407
+ let tcx = ex. tcx ( ) ;
423
408
let mut out = String :: new ( ) ;
424
409
abort_on_err ( rustc_hir_analysis:: check_crate ( tcx) , tcx. sess ) ;
425
410
debug ! ( "pretty printing THIR flat" ) ;
@@ -428,11 +413,7 @@ fn print_with_analysis(tcx: TyCtxt<'_>, ppm: PpMode) -> Result<(), ErrorGuarante
428
413
}
429
414
out
430
415
}
431
-
432
- _ => unreachable ! ( ) ,
433
416
} ;
434
417
435
- write_or_print ( & out, tcx. sess ) ;
436
-
437
- Ok ( ( ) )
418
+ write_or_print ( & out, sess) ;
438
419
}
0 commit comments