]> git.lizzy.rs Git - rust.git/blob - src/librustc_driver/pretty.rs
Auto merge of #71292 - marmeladema:queries-local-def-id, r=eddyb
[rust.git] / src / librustc_driver / pretty.rs
1 //! The various pretty-printing routines.
2
3 use rustc_ast::ast;
4 use rustc_ast_pretty::pprust;
5 use rustc_errors::ErrorReported;
6 use rustc_hir as hir;
7 use rustc_hir::def_id::LOCAL_CRATE;
8 use rustc_hir_pretty as pprust_hir;
9 use rustc_middle::hir::map as hir_map;
10 use rustc_middle::ty::{self, TyCtxt};
11 use rustc_mir::util::{write_mir_graphviz, write_mir_pretty};
12 use rustc_session::config::{Input, PpMode, PpSourceMode};
13 use rustc_session::Session;
14 use rustc_span::FileName;
15
16 use std::cell::Cell;
17 use std::fs::File;
18 use std::io::Write;
19 use std::path::Path;
20
21 pub use self::PpMode::*;
22 pub use self::PpSourceMode::*;
23 use crate::abort_on_err;
24
25 // This slightly awkward construction is to allow for each PpMode to
26 // choose whether it needs to do analyses (which can consume the
27 // Session) and then pass through the session (now attached to the
28 // analysis results) on to the chosen pretty-printer, along with the
29 // `&PpAnn` object.
30 //
31 // Note that since the `&PrinterSupport` is freshly constructed on each
32 // call, it would not make sense to try to attach the lifetime of `self`
33 // to the lifetime of the `&PrinterObject`.
34 //
35 // (The `use_once_payload` is working around the current lack of once
36 // functions in the compiler.)
37
38 /// Constructs a `PrinterSupport` object and passes it to `f`.
39 fn call_with_pp_support<'tcx, A, F>(
40     ppmode: &PpSourceMode,
41     sess: &'tcx Session,
42     tcx: Option<TyCtxt<'tcx>>,
43     f: F,
44 ) -> A
45 where
46     F: FnOnce(&dyn PrinterSupport) -> A,
47 {
48     match *ppmode {
49         PpmNormal | PpmEveryBodyLoops | PpmExpanded => {
50             let annotation = NoAnn { sess, tcx };
51             f(&annotation)
52         }
53
54         PpmIdentified | PpmExpandedIdentified => {
55             let annotation = IdentifiedAnnotation { sess, tcx };
56             f(&annotation)
57         }
58         PpmExpandedHygiene => {
59             let annotation = HygieneAnnotation { sess };
60             f(&annotation)
61         }
62         _ => panic!("Should use call_with_pp_support_hir"),
63     }
64 }
65 fn call_with_pp_support_hir<A, F>(ppmode: &PpSourceMode, tcx: TyCtxt<'_>, f: F) -> A
66 where
67     F: FnOnce(&dyn HirPrinterSupport<'_>, &hir::Crate<'_>) -> A,
68 {
69     match *ppmode {
70         PpmNormal => {
71             let annotation = NoAnn { sess: tcx.sess, tcx: Some(tcx) };
72             f(&annotation, tcx.hir().krate())
73         }
74
75         PpmIdentified => {
76             let annotation = IdentifiedAnnotation { sess: tcx.sess, tcx: Some(tcx) };
77             f(&annotation, tcx.hir().krate())
78         }
79         PpmTyped => {
80             abort_on_err(tcx.analysis(LOCAL_CRATE), tcx.sess);
81
82             let empty_tables = ty::TypeckTables::empty(None);
83             let annotation = TypedAnnotation { tcx, tables: Cell::new(&empty_tables) };
84             tcx.dep_graph.with_ignore(|| f(&annotation, tcx.hir().krate()))
85         }
86         _ => panic!("Should use call_with_pp_support"),
87     }
88 }
89
90 trait PrinterSupport: pprust::PpAnn {
91     /// Provides a uniform interface for re-extracting a reference to a
92     /// `Session` from a value that now owns it.
93     fn sess(&self) -> &Session;
94
95     /// Produces the pretty-print annotation object.
96     ///
97     /// (Rust does not yet support upcasting from a trait object to
98     /// an object for one of its super-traits.)
99     fn pp_ann(&self) -> &dyn pprust::PpAnn;
100 }
101
102 trait HirPrinterSupport<'hir>: pprust_hir::PpAnn {
103     /// Provides a uniform interface for re-extracting a reference to a
104     /// `Session` from a value that now owns it.
105     fn sess(&self) -> &Session;
106
107     /// Provides a uniform interface for re-extracting a reference to an
108     /// `hir_map::Map` from a value that now owns it.
109     fn hir_map(&self) -> Option<hir_map::Map<'hir>>;
110
111     /// Produces the pretty-print annotation object.
112     ///
113     /// (Rust does not yet support upcasting from a trait object to
114     /// an object for one of its super-traits.)
115     fn pp_ann(&self) -> &dyn pprust_hir::PpAnn;
116
117     /// Computes an user-readable representation of a path, if possible.
118     fn node_path(&self, id: hir::HirId) -> Option<String> {
119         self.hir_map().and_then(|map| map.def_path_from_hir_id(id)).map(|path| {
120             path.data.into_iter().map(|elem| elem.data.to_string()).collect::<Vec<_>>().join("::")
121         })
122     }
123 }
124
125 struct NoAnn<'hir> {
126     sess: &'hir Session,
127     tcx: Option<TyCtxt<'hir>>,
128 }
129
130 impl<'hir> PrinterSupport for NoAnn<'hir> {
131     fn sess(&self) -> &Session {
132         self.sess
133     }
134
135     fn pp_ann(&self) -> &dyn pprust::PpAnn {
136         self
137     }
138 }
139
140 impl<'hir> HirPrinterSupport<'hir> for NoAnn<'hir> {
141     fn sess(&self) -> &Session {
142         self.sess
143     }
144
145     fn hir_map(&self) -> Option<hir_map::Map<'hir>> {
146         self.tcx.map(|tcx| tcx.hir())
147     }
148
149     fn pp_ann(&self) -> &dyn pprust_hir::PpAnn {
150         self
151     }
152 }
153
154 impl<'hir> pprust::PpAnn for NoAnn<'hir> {}
155 impl<'hir> pprust_hir::PpAnn for NoAnn<'hir> {
156     fn nested(&self, state: &mut pprust_hir::State<'_>, nested: pprust_hir::Nested) {
157         if let Some(tcx) = self.tcx {
158             pprust_hir::PpAnn::nested(&(&tcx.hir() as &dyn hir::intravisit::Map<'_>), state, nested)
159         }
160     }
161 }
162
163 struct IdentifiedAnnotation<'hir> {
164     sess: &'hir Session,
165     tcx: Option<TyCtxt<'hir>>,
166 }
167
168 impl<'hir> PrinterSupport for IdentifiedAnnotation<'hir> {
169     fn sess(&self) -> &Session {
170         self.sess
171     }
172
173     fn pp_ann(&self) -> &dyn pprust::PpAnn {
174         self
175     }
176 }
177
178 impl<'hir> pprust::PpAnn for IdentifiedAnnotation<'hir> {
179     fn pre(&self, s: &mut pprust::State<'_>, node: pprust::AnnNode<'_>) {
180         if let pprust::AnnNode::Expr(_) = node {
181             s.popen();
182         }
183     }
184     fn post(&self, s: &mut pprust::State<'_>, node: pprust::AnnNode<'_>) {
185         match node {
186             pprust::AnnNode::Crate(_) | pprust::AnnNode::Ident(_) | pprust::AnnNode::Name(_) => {}
187
188             pprust::AnnNode::Item(item) => {
189                 s.s.space();
190                 s.synth_comment(item.id.to_string())
191             }
192             pprust::AnnNode::SubItem(id) => {
193                 s.s.space();
194                 s.synth_comment(id.to_string())
195             }
196             pprust::AnnNode::Block(blk) => {
197                 s.s.space();
198                 s.synth_comment(format!("block {}", blk.id))
199             }
200             pprust::AnnNode::Expr(expr) => {
201                 s.s.space();
202                 s.synth_comment(expr.id.to_string());
203                 s.pclose()
204             }
205             pprust::AnnNode::Pat(pat) => {
206                 s.s.space();
207                 s.synth_comment(format!("pat {}", pat.id));
208             }
209         }
210     }
211 }
212
213 impl<'hir> HirPrinterSupport<'hir> for IdentifiedAnnotation<'hir> {
214     fn sess(&self) -> &Session {
215         self.sess
216     }
217
218     fn hir_map(&self) -> Option<hir_map::Map<'hir>> {
219         self.tcx.map(|tcx| tcx.hir())
220     }
221
222     fn pp_ann(&self) -> &dyn pprust_hir::PpAnn {
223         self
224     }
225 }
226
227 impl<'hir> pprust_hir::PpAnn for IdentifiedAnnotation<'hir> {
228     fn nested(&self, state: &mut pprust_hir::State<'_>, nested: pprust_hir::Nested) {
229         if let Some(ref tcx) = self.tcx {
230             pprust_hir::PpAnn::nested(&(&tcx.hir() as &dyn hir::intravisit::Map<'_>), state, nested)
231         }
232     }
233     fn pre(&self, s: &mut pprust_hir::State<'_>, node: pprust_hir::AnnNode<'_>) {
234         if let pprust_hir::AnnNode::Expr(_) = node {
235             s.popen();
236         }
237     }
238     fn post(&self, s: &mut pprust_hir::State<'_>, node: pprust_hir::AnnNode<'_>) {
239         match node {
240             pprust_hir::AnnNode::Name(_) => {}
241             pprust_hir::AnnNode::Item(item) => {
242                 s.s.space();
243                 s.synth_comment(format!("hir_id: {}", item.hir_id));
244             }
245             pprust_hir::AnnNode::SubItem(id) => {
246                 s.s.space();
247                 s.synth_comment(id.to_string());
248             }
249             pprust_hir::AnnNode::Block(blk) => {
250                 s.s.space();
251                 s.synth_comment(format!("block hir_id: {}", blk.hir_id));
252             }
253             pprust_hir::AnnNode::Expr(expr) => {
254                 s.s.space();
255                 s.synth_comment(format!("expr hir_id: {}", expr.hir_id));
256                 s.pclose();
257             }
258             pprust_hir::AnnNode::Pat(pat) => {
259                 s.s.space();
260                 s.synth_comment(format!("pat hir_id: {}", pat.hir_id));
261             }
262             pprust_hir::AnnNode::Arm(arm) => {
263                 s.s.space();
264                 s.synth_comment(format!("arm hir_id: {}", arm.hir_id));
265             }
266         }
267     }
268 }
269
270 struct HygieneAnnotation<'a> {
271     sess: &'a Session,
272 }
273
274 impl<'a> PrinterSupport for HygieneAnnotation<'a> {
275     fn sess(&self) -> &Session {
276         self.sess
277     }
278
279     fn pp_ann(&self) -> &dyn pprust::PpAnn {
280         self
281     }
282 }
283
284 impl<'a> pprust::PpAnn for HygieneAnnotation<'a> {
285     fn post(&self, s: &mut pprust::State<'_>, node: pprust::AnnNode<'_>) {
286         match node {
287             pprust::AnnNode::Ident(&ast::Ident { name, span }) => {
288                 s.s.space();
289                 s.synth_comment(format!("{}{:?}", name.as_u32(), span.ctxt()))
290             }
291             pprust::AnnNode::Name(&name) => {
292                 s.s.space();
293                 s.synth_comment(name.as_u32().to_string())
294             }
295             pprust::AnnNode::Crate(_) => {
296                 s.s.hardbreak();
297                 let verbose = self.sess.verbose();
298                 s.synth_comment(rustc_span::hygiene::debug_hygiene_data(verbose));
299                 s.s.hardbreak_if_not_bol();
300             }
301             _ => {}
302         }
303     }
304 }
305
306 struct TypedAnnotation<'a, 'tcx> {
307     tcx: TyCtxt<'tcx>,
308     tables: Cell<&'a ty::TypeckTables<'tcx>>,
309 }
310
311 impl<'b, 'tcx> HirPrinterSupport<'tcx> for TypedAnnotation<'b, 'tcx> {
312     fn sess(&self) -> &Session {
313         &self.tcx.sess
314     }
315
316     fn hir_map(&self) -> Option<hir_map::Map<'tcx>> {
317         Some(self.tcx.hir())
318     }
319
320     fn pp_ann(&self) -> &dyn pprust_hir::PpAnn {
321         self
322     }
323
324     fn node_path(&self, id: hir::HirId) -> Option<String> {
325         Some(self.tcx.def_path_str(self.tcx.hir().local_def_id(id).to_def_id()))
326     }
327 }
328
329 impl<'a, 'tcx> pprust_hir::PpAnn for TypedAnnotation<'a, 'tcx> {
330     fn nested(&self, state: &mut pprust_hir::State<'_>, nested: pprust_hir::Nested) {
331         let old_tables = self.tables.get();
332         if let pprust_hir::Nested::Body(id) = nested {
333             self.tables.set(self.tcx.body_tables(id));
334         }
335         let pp_ann = &(&self.tcx.hir() as &dyn hir::intravisit::Map<'_>);
336         pprust_hir::PpAnn::nested(pp_ann, state, nested);
337         self.tables.set(old_tables);
338     }
339     fn pre(&self, s: &mut pprust_hir::State<'_>, node: pprust_hir::AnnNode<'_>) {
340         if let pprust_hir::AnnNode::Expr(_) = node {
341             s.popen();
342         }
343     }
344     fn post(&self, s: &mut pprust_hir::State<'_>, node: pprust_hir::AnnNode<'_>) {
345         if let pprust_hir::AnnNode::Expr(expr) = node {
346             s.s.space();
347             s.s.word("as");
348             s.s.space();
349             s.s.word(self.tables.get().expr_ty(expr).to_string());
350             s.pclose();
351         }
352     }
353 }
354
355 fn get_source(input: &Input, sess: &Session) -> (String, FileName) {
356     let src_name = input.source_name();
357     let src =
358         String::clone(&sess.source_map().get_source_file(&src_name).unwrap().src.as_ref().unwrap());
359     (src, src_name)
360 }
361
362 fn write_output(out: Vec<u8>, ofile: Option<&Path>) {
363     match ofile {
364         None => print!("{}", String::from_utf8(out).unwrap()),
365         Some(p) => match File::create(p) {
366             Ok(mut w) => w.write_all(&out).unwrap(),
367             Err(e) => panic!("print-print failed to open {} due to {}", p.display(), e),
368         },
369     }
370 }
371
372 pub fn print_after_parsing(
373     sess: &Session,
374     input: &Input,
375     krate: &ast::Crate,
376     ppm: PpMode,
377     ofile: Option<&Path>,
378 ) {
379     let (src, src_name) = get_source(input, sess);
380
381     let mut out = String::new();
382
383     if let PpmSource(s) = ppm {
384         // Silently ignores an identified node.
385         let out = &mut out;
386         call_with_pp_support(&s, sess, None, move |annotation| {
387             debug!("pretty printing source code {:?}", s);
388             let sess = annotation.sess();
389             let parse = &sess.parse_sess;
390             *out = pprust::print_crate(
391                 sess.source_map(),
392                 krate,
393                 src_name,
394                 src,
395                 annotation.pp_ann(),
396                 false,
397                 parse.edition,
398                 parse.injected_crate_name.try_get().is_some(),
399             )
400         })
401     } else {
402         unreachable!();
403     };
404
405     write_output(out.into_bytes(), ofile);
406 }
407
408 pub fn print_after_hir_lowering<'tcx>(
409     tcx: TyCtxt<'tcx>,
410     input: &Input,
411     krate: &ast::Crate,
412     ppm: PpMode,
413     ofile: Option<&Path>,
414 ) {
415     if ppm.needs_analysis() {
416         abort_on_err(print_with_analysis(tcx, ppm, ofile), tcx.sess);
417         return;
418     }
419
420     let (src, src_name) = get_source(input, tcx.sess);
421
422     let mut out = String::new();
423
424     match ppm {
425         PpmSource(s) => {
426             // Silently ignores an identified node.
427             let out = &mut out;
428             call_with_pp_support(&s, tcx.sess, Some(tcx), move |annotation| {
429                 debug!("pretty printing source code {:?}", s);
430                 let sess = annotation.sess();
431                 let parse = &sess.parse_sess;
432                 *out = pprust::print_crate(
433                     sess.source_map(),
434                     krate,
435                     src_name,
436                     src,
437                     annotation.pp_ann(),
438                     true,
439                     parse.edition,
440                     parse.injected_crate_name.try_get().is_some(),
441                 )
442             })
443         }
444
445         PpmHir(s) => {
446             let out = &mut out;
447             call_with_pp_support_hir(&s, tcx, move |annotation, krate| {
448                 debug!("pretty printing source code {:?}", s);
449                 let sess = annotation.sess();
450                 let sm = sess.source_map();
451                 *out = pprust_hir::print_crate(sm, krate, src_name, src, annotation.pp_ann())
452             })
453         }
454
455         PpmHirTree(s) => {
456             let out = &mut out;
457             call_with_pp_support_hir(&s, tcx, move |_annotation, krate| {
458                 debug!("pretty printing source code {:?}", s);
459                 *out = format!("{:#?}", krate);
460             });
461         }
462
463         _ => unreachable!(),
464     }
465
466     write_output(out.into_bytes(), ofile);
467 }
468
469 // In an ideal world, this would be a public function called by the driver after
470 // analysis is performed. However, we want to call `phase_3_run_analysis_passes`
471 // with a different callback than the standard driver, so that isn't easy.
472 // Instead, we call that function ourselves.
473 fn print_with_analysis(
474     tcx: TyCtxt<'_>,
475     ppm: PpMode,
476     ofile: Option<&Path>,
477 ) -> Result<(), ErrorReported> {
478     let mut out = Vec::new();
479
480     tcx.analysis(LOCAL_CRATE)?;
481
482     match ppm {
483         PpmMir | PpmMirCFG => match ppm {
484             PpmMir => write_mir_pretty(tcx, None, &mut out),
485             PpmMirCFG => write_mir_graphviz(tcx, None, &mut out),
486             _ => unreachable!(),
487         },
488         _ => unreachable!(),
489     }
490     .unwrap();
491
492     write_output(out, ofile);
493
494     Ok(())
495 }