Skip to content

Commit 7d8100a

Browse files
committed
Auto merge of #33119 - nrc:pretty, r=pnkfelix
Refactor pretty printing to use the compiler API
2 parents 43c5fef + c1c6e99 commit 7d8100a

File tree

4 files changed

+402
-333
lines changed

4 files changed

+402
-333
lines changed

src/librustc_driver/driver.rs

+96-55
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ pub fn compile_input(sess: &Session,
7070
control: &CompileController) -> CompileResult {
7171
macro_rules! controller_entry_point {
7272
($point: ident, $tsess: expr, $make_state: expr, $phase_result: expr) => {{
73-
let state = $make_state;
73+
let state = &mut $make_state;
7474
let phase_result: &CompileResult = &$phase_result;
7575
if phase_result.is_ok() || control.$point.run_callback_on_error {
7676
(control.$point.callback)(state);
@@ -95,17 +95,24 @@ pub fn compile_input(sess: &Session,
9595
}
9696
};
9797

98+
let mut compile_state = CompileState::state_after_parse(input,
99+
sess,
100+
outdir,
101+
output,
102+
krate,
103+
&cstore);
98104
controller_entry_point!(after_parse,
99105
sess,
100-
CompileState::state_after_parse(input, sess, outdir, &krate),
106+
compile_state,
101107
Ok(()));
108+
let krate = compile_state.krate.unwrap();
102109

103110
let outputs = build_output_filenames(input, outdir, output, &krate.attrs, sess);
104111
let id = link::find_crate_name(Some(sess), &krate.attrs, input);
105112
let expanded_crate = phase_2_configure_and_expand(sess,
106113
&cstore,
107114
krate,
108-
&id[..],
115+
&id,
109116
addl_plugins)?;
110117

111118
(outputs, expanded_crate, id)
@@ -116,8 +123,10 @@ pub fn compile_input(sess: &Session,
116123
CompileState::state_after_expand(input,
117124
sess,
118125
outdir,
126+
output,
127+
&cstore,
119128
&expanded_crate,
120-
&id[..]),
129+
&id),
121130
Ok(()));
122131

123132
let expanded_crate = assign_node_ids(sess, expanded_crate);
@@ -162,10 +171,13 @@ pub fn compile_input(sess: &Session,
162171
CompileState::state_after_write_deps(input,
163172
sess,
164173
outdir,
174+
output,
175+
&arenas,
176+
&cstore,
165177
&hir_map,
166178
&expanded_crate,
167179
&hir_map.krate(),
168-
&id[..]),
180+
&id),
169181
Ok(()));
170182
}
171183

@@ -194,16 +206,17 @@ pub fn compile_input(sess: &Session,
194206
// Eventually, we will want to track plugins.
195207
let _ignore = tcx.dep_graph.in_ignore();
196208

197-
let state = CompileState::state_after_analysis(input,
198-
&tcx.sess,
199-
outdir,
200-
opt_crate,
201-
tcx.map.krate(),
202-
&analysis,
203-
mir_map.as_ref(),
204-
tcx,
205-
&id);
206-
(control.after_analysis.callback)(state);
209+
let mut state = CompileState::state_after_analysis(input,
210+
sess,
211+
outdir,
212+
output,
213+
opt_crate,
214+
tcx.map.krate(),
215+
&analysis,
216+
mir_map.as_ref(),
217+
tcx,
218+
&id);
219+
(control.after_analysis.callback)(&mut state);
207220

208221
if control.after_analysis.stop == Compilation::Stop {
209222
return result.and_then(|_| Err(0usize));
@@ -236,7 +249,7 @@ pub fn compile_input(sess: &Session,
236249

237250
controller_entry_point!(after_llvm,
238251
sess,
239-
CompileState::state_after_llvm(input, sess, outdir, &trans),
252+
CompileState::state_after_llvm(input, sess, outdir, output, &trans),
240253
phase5_result);
241254
phase5_result?;
242255

@@ -311,7 +324,7 @@ pub struct PhaseController<'a> {
311324
// If true then the compiler will try to run the callback even if the phase
312325
// ends with an error. Note that this is not always possible.
313326
pub run_callback_on_error: bool,
314-
pub callback: Box<Fn(CompileState) -> () + 'a>,
327+
pub callback: Box<Fn(&mut CompileState) + 'a>,
315328
}
316329

317330
impl<'a> PhaseController<'a> {
@@ -327,34 +340,38 @@ impl<'a> PhaseController<'a> {
327340
/// State that is passed to a callback. What state is available depends on when
328341
/// during compilation the callback is made. See the various constructor methods
329342
/// (`state_*`) in the impl to see which data is provided for any given entry point.
330-
pub struct CompileState<'a, 'ast: 'a, 'tcx: 'a> {
343+
pub struct CompileState<'a, 'b, 'ast: 'a, 'tcx: 'b> where 'ast: 'tcx {
331344
pub input: &'a Input,
332-
pub session: &'a Session,
333-
pub cfg: Option<&'a ast::CrateConfig>,
334-
pub krate: Option<&'a ast::Crate>,
345+
pub session: &'ast Session,
346+
pub krate: Option<ast::Crate>,
347+
pub cstore: Option<&'a CStore>,
335348
pub crate_name: Option<&'a str>,
336349
pub output_filenames: Option<&'a OutputFilenames>,
337350
pub out_dir: Option<&'a Path>,
351+
pub out_file: Option<&'a Path>,
352+
pub arenas: Option<&'ast ty::CtxtArenas<'ast>>,
338353
pub expanded_crate: Option<&'a ast::Crate>,
339354
pub hir_crate: Option<&'a hir::Crate>,
340355
pub ast_map: Option<&'a hir_map::Map<'ast>>,
341-
pub mir_map: Option<&'a MirMap<'tcx>>,
356+
pub mir_map: Option<&'b MirMap<'tcx>>,
342357
pub analysis: Option<&'a ty::CrateAnalysis<'a>>,
343-
pub tcx: Option<&'a TyCtxt<'tcx>>,
358+
pub tcx: Option<&'b TyCtxt<'tcx>>,
344359
pub trans: Option<&'a trans::CrateTranslation>,
345360
}
346361

347-
impl<'a, 'ast, 'tcx> CompileState<'a, 'ast, 'tcx> {
362+
impl<'a, 'b, 'ast, 'tcx> CompileState<'a, 'b, 'ast, 'tcx> {
348363
fn empty(input: &'a Input,
349-
session: &'a Session,
364+
session: &'ast Session,
350365
out_dir: &'a Option<PathBuf>)
351-
-> CompileState<'a, 'ast, 'tcx> {
366+
-> CompileState<'a, 'b, 'ast, 'tcx> {
352367
CompileState {
353368
input: input,
354369
session: session,
355370
out_dir: out_dir.as_ref().map(|s| &**s),
356-
cfg: None,
371+
out_file: None,
372+
arenas: None,
357373
krate: None,
374+
cstore: None,
358375
crate_name: None,
359376
output_filenames: None,
360377
expanded_crate: None,
@@ -368,71 +385,95 @@ impl<'a, 'ast, 'tcx> CompileState<'a, 'ast, 'tcx> {
368385
}
369386

370387
fn state_after_parse(input: &'a Input,
371-
session: &'a Session,
388+
session: &'ast Session,
372389
out_dir: &'a Option<PathBuf>,
373-
krate: &'a ast::Crate)
374-
-> CompileState<'a, 'ast, 'tcx> {
375-
CompileState { krate: Some(krate), ..CompileState::empty(input, session, out_dir) }
390+
out_file: &'a Option<PathBuf>,
391+
krate: ast::Crate,
392+
cstore: &'a CStore)
393+
-> CompileState<'a, 'b, 'ast, 'tcx> {
394+
CompileState {
395+
krate: Some(krate),
396+
cstore: Some(cstore),
397+
out_file: out_file.as_ref().map(|s| &**s),
398+
..CompileState::empty(input, session, out_dir)
399+
}
376400
}
377401

378402
fn state_after_expand(input: &'a Input,
379-
session: &'a Session,
403+
session: &'ast Session,
380404
out_dir: &'a Option<PathBuf>,
405+
out_file: &'a Option<PathBuf>,
406+
cstore: &'a CStore,
381407
expanded_crate: &'a ast::Crate,
382408
crate_name: &'a str)
383-
-> CompileState<'a, 'ast, 'tcx> {
409+
-> CompileState<'a, 'b, 'ast, 'tcx> {
384410
CompileState {
385411
crate_name: Some(crate_name),
412+
cstore: Some(cstore),
386413
expanded_crate: Some(expanded_crate),
414+
out_file: out_file.as_ref().map(|s| &**s),
387415
..CompileState::empty(input, session, out_dir)
388416
}
389417
}
390418

391419
fn state_after_write_deps(input: &'a Input,
392-
session: &'a Session,
420+
session: &'ast Session,
393421
out_dir: &'a Option<PathBuf>,
422+
out_file: &'a Option<PathBuf>,
423+
arenas: &'ast ty::CtxtArenas<'ast>,
424+
cstore: &'a CStore,
394425
hir_map: &'a hir_map::Map<'ast>,
395426
krate: &'a ast::Crate,
396427
hir_crate: &'a hir::Crate,
397428
crate_name: &'a str)
398-
-> CompileState<'a, 'ast, 'tcx> {
429+
-> CompileState<'a, 'b, 'ast, 'tcx> {
399430
CompileState {
400431
crate_name: Some(crate_name),
432+
arenas: Some(arenas),
433+
cstore: Some(cstore),
401434
ast_map: Some(hir_map),
402-
krate: Some(krate),
435+
expanded_crate: Some(krate),
403436
hir_crate: Some(hir_crate),
437+
out_file: out_file.as_ref().map(|s| &**s),
404438
..CompileState::empty(input, session, out_dir)
405439
}
406440
}
407441

408442
fn state_after_analysis(input: &'a Input,
409-
session: &'a Session,
443+
session: &'ast Session,
410444
out_dir: &'a Option<PathBuf>,
445+
out_file: &'a Option<PathBuf>,
411446
krate: Option<&'a ast::Crate>,
412447
hir_crate: &'a hir::Crate,
413-
analysis: &'a ty::CrateAnalysis,
414-
mir_map: Option<&'a MirMap<'tcx>>,
415-
tcx: &'a TyCtxt<'tcx>,
448+
analysis: &'a ty::CrateAnalysis<'a>,
449+
mir_map: Option<&'b MirMap<'tcx>>,
450+
tcx: &'b TyCtxt<'tcx>,
416451
crate_name: &'a str)
417-
-> CompileState<'a, 'ast, 'tcx> {
452+
-> CompileState<'a, 'b, 'ast, 'tcx> {
418453
CompileState {
419454
analysis: Some(analysis),
420455
mir_map: mir_map,
421456
tcx: Some(tcx),
422-
krate: krate,
457+
expanded_crate: krate,
423458
hir_crate: Some(hir_crate),
424459
crate_name: Some(crate_name),
460+
out_file: out_file.as_ref().map(|s| &**s),
425461
..CompileState::empty(input, session, out_dir)
426462
}
427463
}
428464

429465

430466
fn state_after_llvm(input: &'a Input,
431-
session: &'a Session,
467+
session: &'ast Session,
432468
out_dir: &'a Option<PathBuf>,
469+
out_file: &'a Option<PathBuf>,
433470
trans: &'a trans::CrateTranslation)
434-
-> CompileState<'a, 'ast, 'tcx> {
435-
CompileState { trans: Some(trans), ..CompileState::empty(input, session, out_dir) }
471+
-> CompileState<'a, 'b, 'ast, 'tcx> {
472+
CompileState {
473+
trans: Some(trans),
474+
out_file: out_file.as_ref().map(|s| &**s),
475+
..CompileState::empty(input, session, out_dir)
476+
}
436477
}
437478
}
438479

@@ -798,16 +839,16 @@ pub fn phase_3_run_analysis_passes<'tcx, F, R>(sess: &'tcx Session,
798839
let index = stability::Index::new(&hir_map);
799840

800841
TyCtxt::create_and_enter(sess,
801-
arenas,
802-
def_map,
803-
named_region_map,
804-
hir_map,
805-
freevars,
806-
region_map,
807-
lang_items,
808-
index,
809-
name,
810-
|tcx| {
842+
arenas,
843+
def_map,
844+
named_region_map,
845+
hir_map,
846+
freevars,
847+
region_map,
848+
lang_items,
849+
index,
850+
name,
851+
|tcx| {
811852
time(time_passes,
812853
"load_dep_graph",
813854
|| rustc_incremental::load_dep_graph(tcx));

0 commit comments

Comments
 (0)