2018-11-27 02:59:49 +00:00
|
|
|
//! The various pretty-printing routines.
|
2014-08-11 10:59:35 +00:00
|
|
|
|
2020-04-27 17:56:11 +00:00
|
|
|
use rustc_ast as ast;
|
2023-10-09 23:01:52 +00:00
|
|
|
use rustc_ast_pretty::pprust as pprust_ast;
|
2020-01-05 01:37:57 +00:00
|
|
|
use rustc_hir as hir;
|
2020-03-23 19:59:19 +00:00
|
|
|
use rustc_hir_pretty as pprust_hir;
|
2023-10-10 21:49:24 +00:00
|
|
|
use rustc_middle::bug;
|
2020-03-29 15:19:48 +00:00
|
|
|
use rustc_middle::hir::map as hir_map;
|
2021-01-05 18:53:07 +00:00
|
|
|
use rustc_middle::mir::{write_mir_graphviz, write_mir_pretty};
|
2020-03-29 15:19:48 +00:00
|
|
|
use rustc_middle::ty::{self, TyCtxt};
|
2023-10-10 01:17:06 +00:00
|
|
|
use rustc_session::config::{OutFileName, PpHirMode, PpMode, PpSourceMode};
|
2020-03-11 11:49:08 +00:00
|
|
|
use rustc_session::Session;
|
2020-04-19 11:00:18 +00:00
|
|
|
use rustc_span::symbol::Ident;
|
2019-12-31 17:15:40 +00:00
|
|
|
use rustc_span::FileName;
|
2014-08-11 10:59:35 +00:00
|
|
|
|
2017-01-06 19:54:24 +00:00
|
|
|
use std::cell::Cell;
|
2021-07-24 21:18:15 +00:00
|
|
|
use std::fmt::Write;
|
2014-08-11 10:59:35 +00:00
|
|
|
|
2018-11-27 02:59:49 +00:00
|
|
|
pub use self::PpMode::*;
|
|
|
|
pub use self::PpSourceMode::*;
|
2019-03-26 20:35:18 +00:00
|
|
|
use crate::abort_on_err;
|
2018-12-08 19:30:23 +00:00
|
|
|
|
2014-08-11 10:59:35 +00:00
|
|
|
// This slightly awkward construction is to allow for each PpMode to
|
|
|
|
// choose whether it needs to do analyses (which can consume the
|
|
|
|
// Session) and then pass through the session (now attached to the
|
|
|
|
// analysis results) on to the chosen pretty-printer, along with the
|
|
|
|
// `&PpAnn` object.
|
|
|
|
//
|
2023-10-09 23:01:52 +00:00
|
|
|
// Note that since the `&AstPrinterSupport` is freshly constructed on each
|
2014-08-11 10:59:35 +00:00
|
|
|
// call, it would not make sense to try to attach the lifetime of `self`
|
|
|
|
// to the lifetime of the `&PrinterObject`.
|
|
|
|
|
2023-10-09 23:01:52 +00:00
|
|
|
/// Constructs an `AstPrinterSupport` object and passes it to `f`.
|
|
|
|
fn call_with_pp_support_ast<'tcx, A, F>(
|
2019-11-04 02:42:03 +00:00
|
|
|
ppmode: &PpSourceMode,
|
|
|
|
sess: &'tcx Session,
|
|
|
|
tcx: Option<TyCtxt<'tcx>>,
|
|
|
|
f: F,
|
|
|
|
) -> A
|
|
|
|
where
|
2023-10-09 23:01:52 +00:00
|
|
|
F: FnOnce(&dyn AstPrinterSupport) -> A,
|
2019-11-04 02:42:03 +00:00
|
|
|
{
|
|
|
|
match *ppmode {
|
2021-06-25 09:56:14 +00:00
|
|
|
Normal | Expanded => {
|
2019-11-04 02:42:03 +00:00
|
|
|
let annotation = NoAnn { sess, tcx };
|
|
|
|
f(&annotation)
|
|
|
|
}
|
2021-02-18 19:21:18 +00:00
|
|
|
Identified | ExpandedIdentified => {
|
2019-11-04 02:42:03 +00:00
|
|
|
let annotation = IdentifiedAnnotation { sess, tcx };
|
|
|
|
f(&annotation)
|
|
|
|
}
|
2021-02-18 19:21:18 +00:00
|
|
|
ExpandedHygiene => {
|
2019-11-04 02:42:03 +00:00
|
|
|
let annotation = HygieneAnnotation { sess };
|
|
|
|
f(&annotation)
|
2015-07-31 07:04:06 +00:00
|
|
|
}
|
|
|
|
}
|
2019-11-04 02:42:03 +00:00
|
|
|
}
|
2023-10-10 01:58:12 +00:00
|
|
|
|
2021-02-18 19:21:18 +00:00
|
|
|
fn call_with_pp_support_hir<A, F>(ppmode: &PpHirMode, tcx: TyCtxt<'_>, f: F) -> A
|
2019-11-04 02:42:03 +00:00
|
|
|
where
|
2023-10-10 01:58:12 +00:00
|
|
|
F: FnOnce(&dyn HirPrinterSupport, hir_map::Map<'_>) -> A,
|
2019-11-04 02:42:03 +00:00
|
|
|
{
|
|
|
|
match *ppmode {
|
2021-02-18 19:21:18 +00:00
|
|
|
PpHirMode::Normal => {
|
2019-11-04 02:42:03 +00:00
|
|
|
let annotation = NoAnn { sess: tcx.sess, tcx: Some(tcx) };
|
2021-09-19 21:16:28 +00:00
|
|
|
f(&annotation, tcx.hir())
|
2019-11-04 02:42:03 +00:00
|
|
|
}
|
2021-02-18 19:21:18 +00:00
|
|
|
PpHirMode::Identified => {
|
2019-11-04 02:42:03 +00:00
|
|
|
let annotation = IdentifiedAnnotation { sess: tcx.sess, tcx: Some(tcx) };
|
2021-09-19 21:16:28 +00:00
|
|
|
f(&annotation, tcx.hir())
|
2019-11-04 02:42:03 +00:00
|
|
|
}
|
2021-02-18 19:21:18 +00:00
|
|
|
PpHirMode::Typed => {
|
2021-05-11 12:50:54 +00:00
|
|
|
abort_on_err(tcx.analysis(()), tcx.sess);
|
2019-11-04 02:42:03 +00:00
|
|
|
|
2020-07-17 08:47:04 +00:00
|
|
|
let annotation = TypedAnnotation { tcx, maybe_typeck_results: Cell::new(None) };
|
2021-09-19 21:16:28 +00:00
|
|
|
tcx.dep_graph.with_ignore(|| f(&annotation, tcx.hir()))
|
2014-08-11 11:12:23 +00:00
|
|
|
}
|
|
|
|
}
|
2014-08-11 10:59:35 +00:00
|
|
|
}
|
|
|
|
|
2023-10-10 01:58:12 +00:00
|
|
|
trait Sess {
|
2014-08-11 10:59:35 +00:00
|
|
|
/// Provides a uniform interface for re-extracting a reference to a
|
2023-10-10 01:58:12 +00:00
|
|
|
/// `Session`.
|
2019-06-21 18:27:44 +00:00
|
|
|
fn sess(&self) -> &Session;
|
2014-08-11 10:59:35 +00:00
|
|
|
}
|
|
|
|
|
2023-10-10 01:58:12 +00:00
|
|
|
trait AstPrinterSupport: pprust_ast::PpAnn + Sess {}
|
|
|
|
trait HirPrinterSupport: pprust_hir::PpAnn + Sess {}
|
2015-07-31 07:04:06 +00:00
|
|
|
|
2017-01-26 01:21:50 +00:00
|
|
|
struct NoAnn<'hir> {
|
|
|
|
sess: &'hir Session,
|
2019-06-13 21:48:52 +00:00
|
|
|
tcx: Option<TyCtxt<'hir>>,
|
2014-08-11 10:59:35 +00:00
|
|
|
}
|
|
|
|
|
2023-10-10 01:58:12 +00:00
|
|
|
impl<'hir> Sess for NoAnn<'hir> {
|
2019-06-21 18:27:44 +00:00
|
|
|
fn sess(&self) -> &Session {
|
2015-11-10 20:48:44 +00:00
|
|
|
self.sess
|
|
|
|
}
|
2014-08-11 10:59:35 +00:00
|
|
|
}
|
|
|
|
|
2023-10-10 01:58:12 +00:00
|
|
|
impl<'tcx> AstPrinterSupport for NoAnn<'tcx> {}
|
|
|
|
impl<'hir> HirPrinterSupport for NoAnn<'hir> {}
|
2015-07-31 07:04:06 +00:00
|
|
|
|
2023-10-09 23:01:52 +00:00
|
|
|
impl<'hir> pprust_ast::PpAnn for NoAnn<'hir> {}
|
2017-01-26 01:21:50 +00:00
|
|
|
impl<'hir> pprust_hir::PpAnn for NoAnn<'hir> {
|
2019-06-24 18:15:11 +00:00
|
|
|
fn nested(&self, state: &mut pprust_hir::State<'_>, nested: pprust_hir::Nested) {
|
2018-12-08 19:30:23 +00:00
|
|
|
if let Some(tcx) = self.tcx {
|
2020-03-24 01:44:41 +00:00
|
|
|
pprust_hir::PpAnn::nested(&(&tcx.hir() as &dyn hir::intravisit::Map<'_>), state, nested)
|
2016-12-27 08:00:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-08-11 10:59:35 +00:00
|
|
|
|
2017-01-26 01:21:50 +00:00
|
|
|
struct IdentifiedAnnotation<'hir> {
|
|
|
|
sess: &'hir Session,
|
2019-06-13 21:48:52 +00:00
|
|
|
tcx: Option<TyCtxt<'hir>>,
|
2014-08-11 10:59:35 +00:00
|
|
|
}
|
|
|
|
|
2023-10-10 01:58:12 +00:00
|
|
|
impl<'hir> Sess for IdentifiedAnnotation<'hir> {
|
2019-06-21 18:27:44 +00:00
|
|
|
fn sess(&self) -> &Session {
|
2015-11-10 20:48:44 +00:00
|
|
|
self.sess
|
|
|
|
}
|
2014-08-11 10:59:35 +00:00
|
|
|
}
|
|
|
|
|
2023-10-10 01:58:12 +00:00
|
|
|
impl<'hir> AstPrinterSupport for IdentifiedAnnotation<'hir> {}
|
|
|
|
|
2023-10-09 23:01:52 +00:00
|
|
|
impl<'hir> pprust_ast::PpAnn for IdentifiedAnnotation<'hir> {
|
|
|
|
fn pre(&self, s: &mut pprust_ast::State<'_>, node: pprust_ast::AnnNode<'_>) {
|
|
|
|
if let pprust_ast::AnnNode::Expr(_) = node {
|
2020-03-22 12:36:56 +00:00
|
|
|
s.popen();
|
2014-08-11 10:59:35 +00:00
|
|
|
}
|
|
|
|
}
|
2023-10-10 01:58:12 +00:00
|
|
|
|
2023-10-09 23:01:52 +00:00
|
|
|
fn post(&self, s: &mut pprust_ast::State<'_>, node: pprust_ast::AnnNode<'_>) {
|
2014-08-11 10:59:35 +00:00
|
|
|
match node {
|
2023-10-09 23:01:52 +00:00
|
|
|
pprust_ast::AnnNode::Crate(_)
|
|
|
|
| pprust_ast::AnnNode::Ident(_)
|
|
|
|
| pprust_ast::AnnNode::Name(_) => {}
|
2014-08-11 12:01:37 +00:00
|
|
|
|
2023-10-09 23:01:52 +00:00
|
|
|
pprust_ast::AnnNode::Item(item) => {
|
2019-06-24 18:15:11 +00:00
|
|
|
s.s.space();
|
2014-08-11 10:59:35 +00:00
|
|
|
s.synth_comment(item.id.to_string())
|
|
|
|
}
|
2023-10-09 23:01:52 +00:00
|
|
|
pprust_ast::AnnNode::SubItem(id) => {
|
2019-06-24 18:15:11 +00:00
|
|
|
s.s.space();
|
2015-03-24 01:52:55 +00:00
|
|
|
s.synth_comment(id.to_string())
|
|
|
|
}
|
2023-10-09 23:01:52 +00:00
|
|
|
pprust_ast::AnnNode::Block(blk) => {
|
2019-06-24 18:15:11 +00:00
|
|
|
s.s.space();
|
2014-08-11 10:59:35 +00:00
|
|
|
s.synth_comment(format!("block {}", blk.id))
|
|
|
|
}
|
2023-10-09 23:01:52 +00:00
|
|
|
pprust_ast::AnnNode::Expr(expr) => {
|
2019-06-24 18:15:11 +00:00
|
|
|
s.s.space();
|
|
|
|
s.synth_comment(expr.id.to_string());
|
2014-08-11 10:59:35 +00:00
|
|
|
s.pclose()
|
|
|
|
}
|
2023-10-09 23:01:52 +00:00
|
|
|
pprust_ast::AnnNode::Pat(pat) => {
|
2019-06-24 18:15:11 +00:00
|
|
|
s.s.space();
|
|
|
|
s.synth_comment(format!("pat {}", pat.id));
|
2014-08-11 10:59:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-10 01:58:12 +00:00
|
|
|
impl<'hir> HirPrinterSupport for IdentifiedAnnotation<'hir> {}
|
2015-07-31 07:04:06 +00:00
|
|
|
|
2017-01-26 01:21:50 +00:00
|
|
|
impl<'hir> pprust_hir::PpAnn for IdentifiedAnnotation<'hir> {
|
2019-06-24 18:15:11 +00:00
|
|
|
fn nested(&self, state: &mut pprust_hir::State<'_>, nested: pprust_hir::Nested) {
|
2018-12-08 19:30:23 +00:00
|
|
|
if let Some(ref tcx) = self.tcx {
|
2020-03-24 01:44:41 +00:00
|
|
|
pprust_hir::PpAnn::nested(&(&tcx.hir() as &dyn hir::intravisit::Map<'_>), state, nested)
|
2016-12-27 08:00:18 +00:00
|
|
|
}
|
|
|
|
}
|
2023-10-10 01:58:12 +00:00
|
|
|
|
2019-06-24 18:15:11 +00:00
|
|
|
fn pre(&self, s: &mut pprust_hir::State<'_>, node: pprust_hir::AnnNode<'_>) {
|
2020-03-22 12:36:56 +00:00
|
|
|
if let pprust_hir::AnnNode::Expr(_) = node {
|
|
|
|
s.popen();
|
2015-07-31 07:04:06 +00:00
|
|
|
}
|
|
|
|
}
|
2023-10-10 01:58:12 +00:00
|
|
|
|
2019-06-24 18:15:11 +00:00
|
|
|
fn post(&self, s: &mut pprust_hir::State<'_>, node: pprust_hir::AnnNode<'_>) {
|
2015-07-31 07:04:06 +00:00
|
|
|
match node {
|
2019-06-24 18:15:11 +00:00
|
|
|
pprust_hir::AnnNode::Name(_) => {}
|
2018-08-22 21:05:19 +00:00
|
|
|
pprust_hir::AnnNode::Item(item) => {
|
2019-06-24 18:15:11 +00:00
|
|
|
s.s.space();
|
2021-01-30 16:47:51 +00:00
|
|
|
s.synth_comment(format!("hir_id: {}", item.hir_id()));
|
2015-07-31 07:04:06 +00:00
|
|
|
}
|
2018-08-22 21:05:19 +00:00
|
|
|
pprust_hir::AnnNode::SubItem(id) => {
|
2019-06-24 18:15:11 +00:00
|
|
|
s.s.space();
|
2019-06-29 14:31:43 +00:00
|
|
|
s.synth_comment(id.to_string());
|
2015-07-31 07:04:06 +00:00
|
|
|
}
|
2018-08-22 21:05:19 +00:00
|
|
|
pprust_hir::AnnNode::Block(blk) => {
|
2019-06-24 18:15:11 +00:00
|
|
|
s.s.space();
|
2019-06-29 14:31:43 +00:00
|
|
|
s.synth_comment(format!("block hir_id: {}", blk.hir_id));
|
2015-07-31 07:04:06 +00:00
|
|
|
}
|
2018-08-22 21:05:19 +00:00
|
|
|
pprust_hir::AnnNode::Expr(expr) => {
|
2019-06-24 18:15:11 +00:00
|
|
|
s.s.space();
|
2019-06-29 14:31:43 +00:00
|
|
|
s.synth_comment(format!("expr hir_id: {}", expr.hir_id));
|
|
|
|
s.pclose();
|
2015-07-31 07:04:06 +00:00
|
|
|
}
|
2018-08-22 21:05:19 +00:00
|
|
|
pprust_hir::AnnNode::Pat(pat) => {
|
2019-06-24 18:15:11 +00:00
|
|
|
s.s.space();
|
2019-06-29 14:31:43 +00:00
|
|
|
s.synth_comment(format!("pat hir_id: {}", pat.hir_id));
|
|
|
|
}
|
|
|
|
pprust_hir::AnnNode::Arm(arm) => {
|
|
|
|
s.s.space();
|
|
|
|
s.synth_comment(format!("arm hir_id: {}", arm.hir_id));
|
2015-07-31 07:04:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-26 01:21:50 +00:00
|
|
|
struct HygieneAnnotation<'a> {
|
|
|
|
sess: &'a Session,
|
2014-08-11 12:01:37 +00:00
|
|
|
}
|
|
|
|
|
2023-10-10 01:58:12 +00:00
|
|
|
impl<'a> Sess for HygieneAnnotation<'a> {
|
2017-01-26 01:21:50 +00:00
|
|
|
fn sess(&self) -> &Session {
|
2015-11-10 20:48:44 +00:00
|
|
|
self.sess
|
|
|
|
}
|
2014-08-11 12:01:37 +00:00
|
|
|
}
|
|
|
|
|
2023-10-10 01:58:12 +00:00
|
|
|
impl<'a> AstPrinterSupport for HygieneAnnotation<'a> {}
|
|
|
|
|
2023-10-09 23:01:52 +00:00
|
|
|
impl<'a> pprust_ast::PpAnn for HygieneAnnotation<'a> {
|
|
|
|
fn post(&self, s: &mut pprust_ast::State<'_>, node: pprust_ast::AnnNode<'_>) {
|
2014-08-11 12:01:37 +00:00
|
|
|
match node {
|
2023-10-09 23:01:52 +00:00
|
|
|
pprust_ast::AnnNode::Ident(&Ident { name, span }) => {
|
2019-06-24 18:15:11 +00:00
|
|
|
s.s.space();
|
2018-03-17 23:57:23 +00:00
|
|
|
s.synth_comment(format!("{}{:?}", name.as_u32(), span.ctxt()))
|
2014-08-11 12:01:37 +00:00
|
|
|
}
|
2023-10-09 23:01:52 +00:00
|
|
|
pprust_ast::AnnNode::Name(&name) => {
|
2019-06-24 18:15:11 +00:00
|
|
|
s.s.space();
|
2016-11-16 08:21:52 +00:00
|
|
|
s.synth_comment(name.as_u32().to_string())
|
2014-08-11 12:01:37 +00:00
|
|
|
}
|
2023-10-09 23:01:52 +00:00
|
|
|
pprust_ast::AnnNode::Crate(_) => {
|
2019-07-14 20:17:37 +00:00
|
|
|
s.s.hardbreak();
|
|
|
|
let verbose = self.sess.verbose();
|
2019-12-31 17:15:40 +00:00
|
|
|
s.synth_comment(rustc_span::hygiene::debug_hygiene_data(verbose));
|
2019-07-14 20:17:37 +00:00
|
|
|
s.s.hardbreak_if_not_bol();
|
|
|
|
}
|
2019-06-24 18:15:11 +00:00
|
|
|
_ => {}
|
2014-08-11 12:01:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-25 17:17:45 +00:00
|
|
|
struct TypedAnnotation<'tcx> {
|
2019-06-13 21:48:52 +00:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2020-07-17 08:47:04 +00:00
|
|
|
maybe_typeck_results: Cell<Option<&'tcx ty::TypeckResults<'tcx>>>,
|
2014-08-11 10:59:35 +00:00
|
|
|
}
|
|
|
|
|
2023-10-10 01:58:12 +00:00
|
|
|
impl<'tcx> Sess for TypedAnnotation<'tcx> {
|
2019-06-21 18:27:44 +00:00
|
|
|
fn sess(&self) -> &Session {
|
2021-09-30 17:38:50 +00:00
|
|
|
self.tcx.sess
|
2015-11-10 20:48:44 +00:00
|
|
|
}
|
2014-08-11 10:59:35 +00:00
|
|
|
}
|
|
|
|
|
2023-10-10 01:58:12 +00:00
|
|
|
impl<'tcx> HirPrinterSupport for TypedAnnotation<'tcx> {}
|
|
|
|
|
2020-06-25 17:17:45 +00:00
|
|
|
impl<'tcx> pprust_hir::PpAnn for TypedAnnotation<'tcx> {
|
2019-06-24 18:15:11 +00:00
|
|
|
fn nested(&self, state: &mut pprust_hir::State<'_>, nested: pprust_hir::Nested) {
|
2020-07-17 08:47:04 +00:00
|
|
|
let old_maybe_typeck_results = self.maybe_typeck_results.get();
|
2017-01-06 19:54:24 +00:00
|
|
|
if let pprust_hir::Nested::Body(id) = nested {
|
2020-07-17 08:47:04 +00:00
|
|
|
self.maybe_typeck_results.set(Some(self.tcx.typeck_body(id)));
|
2017-01-06 19:54:24 +00:00
|
|
|
}
|
2020-03-24 01:44:41 +00:00
|
|
|
let pp_ann = &(&self.tcx.hir() as &dyn hir::intravisit::Map<'_>);
|
|
|
|
pprust_hir::PpAnn::nested(pp_ann, state, nested);
|
2020-07-17 08:47:04 +00:00
|
|
|
self.maybe_typeck_results.set(old_maybe_typeck_results);
|
2016-12-27 08:00:18 +00:00
|
|
|
}
|
2023-10-10 01:58:12 +00:00
|
|
|
|
2019-06-24 18:15:11 +00:00
|
|
|
fn pre(&self, s: &mut pprust_hir::State<'_>, node: pprust_hir::AnnNode<'_>) {
|
2020-03-22 12:36:56 +00:00
|
|
|
if let pprust_hir::AnnNode::Expr(_) = node {
|
|
|
|
s.popen();
|
2014-08-11 10:59:35 +00:00
|
|
|
}
|
|
|
|
}
|
2023-10-10 01:58:12 +00:00
|
|
|
|
2019-06-24 18:15:11 +00:00
|
|
|
fn post(&self, s: &mut pprust_hir::State<'_>, node: pprust_hir::AnnNode<'_>) {
|
2020-03-22 12:36:56 +00:00
|
|
|
if let pprust_hir::AnnNode::Expr(expr) = node {
|
2021-06-26 20:26:26 +00:00
|
|
|
let typeck_results = self.maybe_typeck_results.get().or_else(|| {
|
|
|
|
self.tcx
|
|
|
|
.hir()
|
2022-09-20 05:11:23 +00:00
|
|
|
.maybe_body_owned_by(expr.hir_id.owner.def_id)
|
2021-06-26 20:26:26 +00:00
|
|
|
.map(|body_id| self.tcx.typeck_body(body_id))
|
|
|
|
});
|
2021-06-26 14:05:53 +00:00
|
|
|
|
|
|
|
if let Some(typeck_results) = typeck_results {
|
|
|
|
s.s.space();
|
|
|
|
s.s.word("as");
|
|
|
|
s.s.space();
|
|
|
|
s.s.word(typeck_results.expr_ty(expr).to_string());
|
|
|
|
}
|
|
|
|
|
2020-03-22 12:36:56 +00:00
|
|
|
s.pclose();
|
2014-08-11 10:59:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-07 09:24:00 +00:00
|
|
|
fn get_source(sess: &Session) -> (String, FileName) {
|
|
|
|
let src_name = sess.io.input.source_name();
|
2021-01-10 00:04:48 +00:00
|
|
|
let src = String::clone(
|
2021-09-30 17:38:50 +00:00
|
|
|
sess.source_map()
|
2021-01-10 00:04:48 +00:00
|
|
|
.get_source_file(&src_name)
|
|
|
|
.expect("get_source_file")
|
|
|
|
.src
|
|
|
|
.as_ref()
|
|
|
|
.expect("src"),
|
|
|
|
);
|
2016-04-20 22:29:49 +00:00
|
|
|
(src, src_name)
|
|
|
|
}
|
|
|
|
|
2022-12-07 09:24:00 +00:00
|
|
|
fn write_or_print(out: &str, sess: &Session) {
|
2023-07-17 05:13:08 +00:00
|
|
|
sess.io.output_file.as_ref().unwrap_or(&OutFileName::Stdout).overwrite(out, sess);
|
2016-04-20 22:29:49 +00:00
|
|
|
}
|
|
|
|
|
2023-10-10 21:49:24 +00:00
|
|
|
// Extra data for pretty-printing, the form of which depends on what kind of
|
|
|
|
// pretty-printing we are doing.
|
|
|
|
pub enum PrintExtra<'tcx> {
|
|
|
|
AfterParsing { krate: ast::Crate },
|
|
|
|
NeedsAstMap { tcx: TyCtxt<'tcx> },
|
|
|
|
}
|
2016-04-20 22:29:49 +00:00
|
|
|
|
2023-10-10 21:49:24 +00:00
|
|
|
impl<'tcx> PrintExtra<'tcx> {
|
|
|
|
fn with_krate<F, R>(&self, f: F) -> R
|
|
|
|
where
|
|
|
|
F: FnOnce(&ast::Crate) -> R
|
|
|
|
{
|
|
|
|
match self {
|
|
|
|
PrintExtra::AfterParsing { krate, .. } => f(krate),
|
|
|
|
PrintExtra::NeedsAstMap { tcx } => f(&tcx.resolver_for_lowering(()).borrow().1),
|
2021-02-19 21:40:28 +00:00
|
|
|
}
|
2023-10-10 21:49:24 +00:00
|
|
|
}
|
2016-04-20 22:29:49 +00:00
|
|
|
|
2023-10-10 21:49:24 +00:00
|
|
|
fn tcx(&self) -> TyCtxt<'tcx> {
|
|
|
|
match self {
|
|
|
|
PrintExtra::AfterParsing { .. } => bug!("PrintExtra::tcx"),
|
|
|
|
PrintExtra::NeedsAstMap { tcx } => *tcx,
|
|
|
|
}
|
|
|
|
}
|
2016-04-20 22:29:49 +00:00
|
|
|
}
|
|
|
|
|
2023-10-10 21:49:24 +00:00
|
|
|
pub fn print<'tcx>(sess: &Session, ppm: PpMode, ex: PrintExtra<'tcx>) {
|
2016-04-20 22:29:49 +00:00
|
|
|
if ppm.needs_analysis() {
|
2023-10-10 21:49:24 +00:00
|
|
|
abort_on_err(ex.tcx().analysis(()), sess);
|
2016-04-20 22:29:49 +00:00
|
|
|
}
|
|
|
|
|
2023-10-10 21:49:24 +00:00
|
|
|
let (src, src_name) = get_source(sess);
|
2016-04-20 22:29:49 +00:00
|
|
|
|
2021-02-19 18:08:12 +00:00
|
|
|
let out = match ppm {
|
2021-02-18 19:21:18 +00:00
|
|
|
Source(s) => {
|
2016-10-20 01:38:43 +00:00
|
|
|
// Silently ignores an identified node.
|
2023-10-10 21:49:24 +00:00
|
|
|
call_with_pp_support_ast(&s, sess, None, move |annotation| {
|
2016-10-20 01:38:43 +00:00
|
|
|
debug!("pretty printing source code {:?}", s);
|
|
|
|
let sess = annotation.sess();
|
2020-01-11 09:33:18 +00:00
|
|
|
let parse = &sess.parse_sess;
|
2023-10-10 21:49:24 +00:00
|
|
|
let is_expanded = ppm.needs_ast_map();
|
|
|
|
ex.with_krate(|krate|
|
|
|
|
pprust_ast::print_crate(
|
|
|
|
sess.source_map(),
|
|
|
|
krate,
|
|
|
|
src_name,
|
|
|
|
src,
|
|
|
|
annotation,
|
|
|
|
is_expanded,
|
|
|
|
parse.edition,
|
|
|
|
&sess.parse_sess.attr_id_generator,
|
|
|
|
)
|
2019-05-09 16:04:04 +00:00
|
|
|
)
|
2016-10-20 01:38:43 +00:00
|
|
|
})
|
|
|
|
}
|
2023-10-10 21:49:24 +00:00
|
|
|
AstTree => {
|
|
|
|
debug!("pretty printing AST tree");
|
|
|
|
ex.with_krate(|krate| format!("{krate:#?}"))
|
|
|
|
}
|
2023-10-10 01:17:06 +00:00
|
|
|
AstTreeExpanded => {
|
2021-02-19 21:40:28 +00:00
|
|
|
debug!("pretty-printing expanded AST");
|
2023-10-10 21:49:24 +00:00
|
|
|
format!("{:#?}", ex.tcx().resolver_for_lowering(()).borrow().1)
|
2021-02-19 21:40:28 +00:00
|
|
|
}
|
2023-10-10 21:49:24 +00:00
|
|
|
Hir(s) => call_with_pp_support_hir(&s, ex.tcx(), move |annotation, hir_map| {
|
2021-02-19 18:08:12 +00:00
|
|
|
debug!("pretty printing HIR {:?}", s);
|
|
|
|
let sess = annotation.sess();
|
|
|
|
let sm = sess.source_map();
|
2021-09-19 21:16:28 +00:00
|
|
|
let attrs = |id| hir_map.attrs(id);
|
2023-10-10 01:58:12 +00:00
|
|
|
pprust_hir::print_crate(sm, hir_map.root_module(), src_name, src, &attrs, annotation)
|
2021-02-19 18:08:12 +00:00
|
|
|
}),
|
2021-09-19 21:16:28 +00:00
|
|
|
HirTree => {
|
2023-10-09 23:53:12 +00:00
|
|
|
debug!("pretty printing HIR tree");
|
2023-10-10 21:49:24 +00:00
|
|
|
format!("{:#?}", ex.tcx().hir().krate())
|
2021-09-19 21:16:28 +00:00
|
|
|
}
|
2021-03-12 07:02:51 +00:00
|
|
|
Mir => {
|
|
|
|
let mut out = Vec::new();
|
2023-10-10 21:49:24 +00:00
|
|
|
write_mir_pretty(ex.tcx(), None, &mut out).unwrap();
|
2021-03-12 07:02:51 +00:00
|
|
|
String::from_utf8(out).unwrap()
|
|
|
|
}
|
|
|
|
MirCFG => {
|
|
|
|
let mut out = Vec::new();
|
2023-10-10 21:49:24 +00:00
|
|
|
write_mir_graphviz(ex.tcx(), None, &mut out).unwrap();
|
2021-03-12 07:02:51 +00:00
|
|
|
String::from_utf8(out).unwrap()
|
|
|
|
}
|
|
|
|
ThirTree => {
|
2023-10-10 21:49:24 +00:00
|
|
|
let tcx = ex.tcx();
|
2021-07-24 21:18:15 +00:00
|
|
|
let mut out = String::new();
|
2022-09-26 11:00:29 +00:00
|
|
|
abort_on_err(rustc_hir_analysis::check_crate(tcx), tcx.sess);
|
2021-07-24 21:18:15 +00:00
|
|
|
debug!("pretty printing THIR tree");
|
2021-09-12 09:33:16 +00:00
|
|
|
for did in tcx.hir().body_owners() {
|
2022-05-08 13:53:19 +00:00
|
|
|
let _ = writeln!(out, "{:?}:\n{}\n", did, tcx.thir_tree(did));
|
2021-07-24 21:18:15 +00:00
|
|
|
}
|
|
|
|
out
|
2021-03-12 07:02:51 +00:00
|
|
|
}
|
2023-01-26 22:35:24 +00:00
|
|
|
ThirFlat => {
|
2023-10-10 21:49:24 +00:00
|
|
|
let tcx = ex.tcx();
|
2023-01-26 22:35:24 +00:00
|
|
|
let mut out = String::new();
|
|
|
|
abort_on_err(rustc_hir_analysis::check_crate(tcx), tcx.sess);
|
|
|
|
debug!("pretty printing THIR flat");
|
|
|
|
for did in tcx.hir().body_owners() {
|
2022-05-08 13:53:19 +00:00
|
|
|
let _ = writeln!(out, "{:?}:\n{}\n", did, tcx.thir_flat(did));
|
2023-01-26 22:35:24 +00:00
|
|
|
}
|
|
|
|
out
|
|
|
|
}
|
2021-03-12 07:02:51 +00:00
|
|
|
};
|
2014-08-11 10:59:35 +00:00
|
|
|
|
2023-10-10 21:49:24 +00:00
|
|
|
write_or_print(&out, sess);
|
2014-08-11 10:59:35 +00:00
|
|
|
}
|