2018-11-26 20:59:49 -06:00
|
|
|
//! The various pretty-printing routines.
|
2014-08-11 05:59:35 -05:00
|
|
|
|
2020-04-27 12:56:11 -05:00
|
|
|
use rustc_ast as ast;
|
2020-01-11 10:02:46 -06:00
|
|
|
use rustc_ast_pretty::pprust;
|
2020-03-31 14:18:30 -05:00
|
|
|
use rustc_errors::ErrorReported;
|
2020-01-04 19:37:57 -06:00
|
|
|
use rustc_hir as hir;
|
2020-03-23 14:59:19 -05:00
|
|
|
use rustc_hir_pretty as pprust_hir;
|
2020-03-29 10:19:48 -05:00
|
|
|
use rustc_middle::hir::map as hir_map;
|
|
|
|
use rustc_middle::ty::{self, TyCtxt};
|
2019-12-22 16:42:04 -06:00
|
|
|
use rustc_mir::util::{write_mir_graphviz, write_mir_pretty};
|
2021-02-19 15:40:28 -06:00
|
|
|
use rustc_session::config::{Input, PpAstTreeMode, PpHirMode, PpMode, PpSourceMode};
|
2020-03-11 06:49:08 -05:00
|
|
|
use rustc_session::Session;
|
2020-04-19 06:00:18 -05:00
|
|
|
use rustc_span::symbol::Ident;
|
2019-12-31 11:15:40 -06:00
|
|
|
use rustc_span::FileName;
|
2014-08-11 05:59:35 -05:00
|
|
|
|
2017-01-06 13:54:24 -06:00
|
|
|
use std::cell::Cell;
|
2021-07-24 16:18:15 -05:00
|
|
|
use std::fmt::Write;
|
2016-04-19 23:24:14 -05:00
|
|
|
use std::path::Path;
|
2014-08-11 05:59:35 -05:00
|
|
|
|
2018-11-26 20:59:49 -06:00
|
|
|
pub use self::PpMode::*;
|
2019-12-22 16:42:04 -06:00
|
|
|
pub use self::PpSourceMode::*;
|
2019-03-26 15:35:18 -05:00
|
|
|
use crate::abort_on_err;
|
2018-12-08 13:30:23 -06:00
|
|
|
|
2014-08-11 05:59:35 -05: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.
|
|
|
|
//
|
|
|
|
// Note that since the `&PrinterSupport` is freshly constructed on each
|
|
|
|
// call, it would not make sense to try to attach the lifetime of `self`
|
|
|
|
// to the lifetime of the `&PrinterObject`.
|
|
|
|
|
2019-11-03 20:42:03 -06:00
|
|
|
/// Constructs a `PrinterSupport` object and passes it to `f`.
|
|
|
|
fn call_with_pp_support<'tcx, A, F>(
|
|
|
|
ppmode: &PpSourceMode,
|
|
|
|
sess: &'tcx Session,
|
|
|
|
tcx: Option<TyCtxt<'tcx>>,
|
|
|
|
f: F,
|
|
|
|
) -> A
|
|
|
|
where
|
|
|
|
F: FnOnce(&dyn PrinterSupport) -> A,
|
|
|
|
{
|
|
|
|
match *ppmode {
|
2021-02-18 13:21:18 -06:00
|
|
|
Normal | EveryBodyLoops | Expanded => {
|
2019-12-22 16:42:04 -06:00
|
|
|
let annotation = NoAnn { sess, tcx };
|
2019-11-03 20:42:03 -06:00
|
|
|
f(&annotation)
|
|
|
|
}
|
2014-08-11 06:12:23 -05:00
|
|
|
|
2021-02-18 13:21:18 -06:00
|
|
|
Identified | ExpandedIdentified => {
|
2019-12-22 16:42:04 -06:00
|
|
|
let annotation = IdentifiedAnnotation { sess, tcx };
|
2019-11-03 20:42:03 -06:00
|
|
|
f(&annotation)
|
|
|
|
}
|
2021-02-18 13:21:18 -06:00
|
|
|
ExpandedHygiene => {
|
2019-12-22 16:42:04 -06:00
|
|
|
let annotation = HygieneAnnotation { sess };
|
2019-11-03 20:42:03 -06:00
|
|
|
f(&annotation)
|
2015-07-31 02:04:06 -05:00
|
|
|
}
|
|
|
|
}
|
2019-11-03 20:42:03 -06:00
|
|
|
}
|
2021-02-18 13:21:18 -06:00
|
|
|
fn call_with_pp_support_hir<A, F>(ppmode: &PpHirMode, tcx: TyCtxt<'_>, f: F) -> A
|
2019-11-03 20:42:03 -06:00
|
|
|
where
|
2019-11-28 04:49:29 -06:00
|
|
|
F: FnOnce(&dyn HirPrinterSupport<'_>, &hir::Crate<'_>) -> A,
|
2019-11-03 20:42:03 -06:00
|
|
|
{
|
|
|
|
match *ppmode {
|
2021-02-18 13:21:18 -06:00
|
|
|
PpHirMode::Normal => {
|
2019-12-22 16:42:04 -06:00
|
|
|
let annotation = NoAnn { sess: tcx.sess, tcx: Some(tcx) };
|
2020-02-06 05:46:26 -06:00
|
|
|
f(&annotation, tcx.hir().krate())
|
2019-11-03 20:42:03 -06:00
|
|
|
}
|
2015-07-31 02:04:06 -05:00
|
|
|
|
2021-02-18 13:21:18 -06:00
|
|
|
PpHirMode::Identified => {
|
2019-12-22 16:42:04 -06:00
|
|
|
let annotation = IdentifiedAnnotation { sess: tcx.sess, tcx: Some(tcx) };
|
2020-02-06 05:46:26 -06:00
|
|
|
f(&annotation, tcx.hir().krate())
|
2019-11-03 20:42:03 -06:00
|
|
|
}
|
2021-02-18 13:21:18 -06:00
|
|
|
PpHirMode::Typed => {
|
2021-05-11 07:50:54 -05:00
|
|
|
abort_on_err(tcx.analysis(()), tcx.sess);
|
2019-11-03 20:42:03 -06:00
|
|
|
|
2020-07-17 03:47:04 -05:00
|
|
|
let annotation = TypedAnnotation { tcx, maybe_typeck_results: Cell::new(None) };
|
2020-02-06 05:46:26 -06:00
|
|
|
tcx.dep_graph.with_ignore(|| f(&annotation, tcx.hir().krate()))
|
2014-08-11 06:12:23 -05:00
|
|
|
}
|
|
|
|
}
|
2014-08-11 05:59:35 -05:00
|
|
|
}
|
|
|
|
|
2017-01-25 19:21:50 -06:00
|
|
|
trait PrinterSupport: pprust::PpAnn {
|
2014-08-11 05:59:35 -05:00
|
|
|
/// Provides a uniform interface for re-extracting a reference to a
|
|
|
|
/// `Session` from a value that now owns it.
|
2019-06-21 13:27:44 -05:00
|
|
|
fn sess(&self) -> &Session;
|
2014-08-11 05:59:35 -05:00
|
|
|
|
|
|
|
/// Produces the pretty-print annotation object.
|
|
|
|
///
|
|
|
|
/// (Rust does not yet support upcasting from a trait object to
|
|
|
|
/// an object for one of its super-traits.)
|
2020-03-06 17:56:32 -06:00
|
|
|
fn pp_ann(&self) -> &dyn pprust::PpAnn;
|
2014-08-11 05:59:35 -05:00
|
|
|
}
|
|
|
|
|
2017-01-25 19:21:50 -06:00
|
|
|
trait HirPrinterSupport<'hir>: pprust_hir::PpAnn {
|
2015-07-31 02:04:06 -05:00
|
|
|
/// Provides a uniform interface for re-extracting a reference to a
|
|
|
|
/// `Session` from a value that now owns it.
|
2019-06-21 13:27:44 -05:00
|
|
|
fn sess(&self) -> &Session;
|
2015-07-31 02:04:06 -05:00
|
|
|
|
|
|
|
/// Provides a uniform interface for re-extracting a reference to an
|
|
|
|
/// `hir_map::Map` from a value that now owns it.
|
2020-02-09 08:32:00 -06:00
|
|
|
fn hir_map(&self) -> Option<hir_map::Map<'hir>>;
|
2015-07-31 02:04:06 -05:00
|
|
|
|
|
|
|
/// Produces the pretty-print annotation object.
|
|
|
|
///
|
|
|
|
/// (Rust does not yet support upcasting from a trait object to
|
|
|
|
/// an object for one of its super-traits.)
|
2020-03-06 17:56:32 -06:00
|
|
|
fn pp_ann(&self) -> &dyn pprust_hir::PpAnn;
|
2015-07-31 02:04:06 -05:00
|
|
|
}
|
|
|
|
|
2017-01-25 19:21:50 -06:00
|
|
|
struct NoAnn<'hir> {
|
|
|
|
sess: &'hir Session,
|
2019-06-13 16:48:52 -05:00
|
|
|
tcx: Option<TyCtxt<'hir>>,
|
2014-08-11 05:59:35 -05:00
|
|
|
}
|
|
|
|
|
2017-01-25 19:21:50 -06:00
|
|
|
impl<'hir> PrinterSupport for NoAnn<'hir> {
|
2019-06-21 13:27:44 -05:00
|
|
|
fn sess(&self) -> &Session {
|
2015-11-10 14:48:44 -06:00
|
|
|
self.sess
|
|
|
|
}
|
2014-08-11 05:59:35 -05:00
|
|
|
|
2020-03-06 17:56:32 -06:00
|
|
|
fn pp_ann(&self) -> &dyn pprust::PpAnn {
|
2015-11-10 14:48:44 -06:00
|
|
|
self
|
|
|
|
}
|
2014-08-11 05:59:35 -05:00
|
|
|
}
|
|
|
|
|
2017-01-25 19:21:50 -06:00
|
|
|
impl<'hir> HirPrinterSupport<'hir> for NoAnn<'hir> {
|
2019-06-21 13:27:44 -05:00
|
|
|
fn sess(&self) -> &Session {
|
2015-11-10 14:48:44 -06:00
|
|
|
self.sess
|
|
|
|
}
|
2015-07-31 02:04:06 -05:00
|
|
|
|
2020-02-09 08:32:00 -06:00
|
|
|
fn hir_map(&self) -> Option<hir_map::Map<'hir>> {
|
|
|
|
self.tcx.map(|tcx| tcx.hir())
|
2015-07-31 02:04:06 -05:00
|
|
|
}
|
|
|
|
|
2020-03-06 17:56:32 -06:00
|
|
|
fn pp_ann(&self) -> &dyn pprust_hir::PpAnn {
|
2015-11-10 14:48:44 -06:00
|
|
|
self
|
|
|
|
}
|
2015-07-31 02:04:06 -05:00
|
|
|
}
|
|
|
|
|
2017-01-25 19:21:50 -06:00
|
|
|
impl<'hir> pprust::PpAnn for NoAnn<'hir> {}
|
|
|
|
impl<'hir> pprust_hir::PpAnn for NoAnn<'hir> {
|
2019-06-24 13:15:11 -05:00
|
|
|
fn nested(&self, state: &mut pprust_hir::State<'_>, nested: pprust_hir::Nested) {
|
2018-12-08 13:30:23 -06:00
|
|
|
if let Some(tcx) = self.tcx {
|
2020-03-23 20:44:41 -05:00
|
|
|
pprust_hir::PpAnn::nested(&(&tcx.hir() as &dyn hir::intravisit::Map<'_>), state, nested)
|
2016-12-27 02:00:18 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-08-11 05:59:35 -05:00
|
|
|
|
2017-01-25 19:21:50 -06:00
|
|
|
struct IdentifiedAnnotation<'hir> {
|
|
|
|
sess: &'hir Session,
|
2019-06-13 16:48:52 -05:00
|
|
|
tcx: Option<TyCtxt<'hir>>,
|
2014-08-11 05:59:35 -05:00
|
|
|
}
|
|
|
|
|
2017-01-25 19:21:50 -06:00
|
|
|
impl<'hir> PrinterSupport for IdentifiedAnnotation<'hir> {
|
2019-06-21 13:27:44 -05:00
|
|
|
fn sess(&self) -> &Session {
|
2015-11-10 14:48:44 -06:00
|
|
|
self.sess
|
|
|
|
}
|
2014-08-11 05:59:35 -05:00
|
|
|
|
2020-03-06 17:56:32 -06:00
|
|
|
fn pp_ann(&self) -> &dyn pprust::PpAnn {
|
2015-11-10 14:48:44 -06:00
|
|
|
self
|
|
|
|
}
|
2014-08-11 05:59:35 -05:00
|
|
|
}
|
|
|
|
|
2017-01-25 19:21:50 -06:00
|
|
|
impl<'hir> pprust::PpAnn for IdentifiedAnnotation<'hir> {
|
2019-06-24 13:15:11 -05:00
|
|
|
fn pre(&self, s: &mut pprust::State<'_>, node: pprust::AnnNode<'_>) {
|
2020-03-22 07:36:56 -05:00
|
|
|
if let pprust::AnnNode::Expr(_) = node {
|
|
|
|
s.popen();
|
2014-08-11 05:59:35 -05:00
|
|
|
}
|
|
|
|
}
|
2019-06-24 13:15:11 -05:00
|
|
|
fn post(&self, s: &mut pprust::State<'_>, node: pprust::AnnNode<'_>) {
|
2014-08-11 05:59:35 -05:00
|
|
|
match node {
|
2019-12-22 16:42:04 -06:00
|
|
|
pprust::AnnNode::Crate(_) | pprust::AnnNode::Ident(_) | pprust::AnnNode::Name(_) => {}
|
2014-08-11 07:01:37 -05:00
|
|
|
|
2018-08-22 16:05:19 -05:00
|
|
|
pprust::AnnNode::Item(item) => {
|
2019-06-24 13:15:11 -05:00
|
|
|
s.s.space();
|
2014-08-11 05:59:35 -05:00
|
|
|
s.synth_comment(item.id.to_string())
|
|
|
|
}
|
2018-08-22 16:05:19 -05:00
|
|
|
pprust::AnnNode::SubItem(id) => {
|
2019-06-24 13:15:11 -05:00
|
|
|
s.s.space();
|
2015-03-23 20:52:55 -05:00
|
|
|
s.synth_comment(id.to_string())
|
|
|
|
}
|
2018-08-22 16:05:19 -05:00
|
|
|
pprust::AnnNode::Block(blk) => {
|
2019-06-24 13:15:11 -05:00
|
|
|
s.s.space();
|
2014-08-11 05:59:35 -05:00
|
|
|
s.synth_comment(format!("block {}", blk.id))
|
|
|
|
}
|
2018-08-22 16:05:19 -05:00
|
|
|
pprust::AnnNode::Expr(expr) => {
|
2019-06-24 13:15:11 -05:00
|
|
|
s.s.space();
|
|
|
|
s.synth_comment(expr.id.to_string());
|
2014-08-11 05:59:35 -05:00
|
|
|
s.pclose()
|
|
|
|
}
|
2018-08-22 16:05:19 -05:00
|
|
|
pprust::AnnNode::Pat(pat) => {
|
2019-06-24 13:15:11 -05:00
|
|
|
s.s.space();
|
|
|
|
s.synth_comment(format!("pat {}", pat.id));
|
2014-08-11 05:59:35 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-25 19:21:50 -06:00
|
|
|
impl<'hir> HirPrinterSupport<'hir> for IdentifiedAnnotation<'hir> {
|
2019-06-21 13:27:44 -05:00
|
|
|
fn sess(&self) -> &Session {
|
2015-11-10 14:48:44 -06:00
|
|
|
self.sess
|
|
|
|
}
|
2015-07-31 02:04:06 -05:00
|
|
|
|
2020-02-09 08:32:00 -06:00
|
|
|
fn hir_map(&self) -> Option<hir_map::Map<'hir>> {
|
|
|
|
self.tcx.map(|tcx| tcx.hir())
|
2015-07-31 02:04:06 -05:00
|
|
|
}
|
|
|
|
|
2020-03-06 17:56:32 -06:00
|
|
|
fn pp_ann(&self) -> &dyn pprust_hir::PpAnn {
|
2015-11-10 14:48:44 -06:00
|
|
|
self
|
|
|
|
}
|
2015-07-31 02:04:06 -05:00
|
|
|
}
|
|
|
|
|
2017-01-25 19:21:50 -06:00
|
|
|
impl<'hir> pprust_hir::PpAnn for IdentifiedAnnotation<'hir> {
|
2019-06-24 13:15:11 -05:00
|
|
|
fn nested(&self, state: &mut pprust_hir::State<'_>, nested: pprust_hir::Nested) {
|
2018-12-08 13:30:23 -06:00
|
|
|
if let Some(ref tcx) = self.tcx {
|
2020-03-23 20:44:41 -05:00
|
|
|
pprust_hir::PpAnn::nested(&(&tcx.hir() as &dyn hir::intravisit::Map<'_>), state, nested)
|
2016-12-27 02:00:18 -06:00
|
|
|
}
|
|
|
|
}
|
2019-06-24 13:15:11 -05:00
|
|
|
fn pre(&self, s: &mut pprust_hir::State<'_>, node: pprust_hir::AnnNode<'_>) {
|
2020-03-22 07:36:56 -05:00
|
|
|
if let pprust_hir::AnnNode::Expr(_) = node {
|
|
|
|
s.popen();
|
2015-07-31 02:04:06 -05:00
|
|
|
}
|
|
|
|
}
|
2019-06-24 13:15:11 -05:00
|
|
|
fn post(&self, s: &mut pprust_hir::State<'_>, node: pprust_hir::AnnNode<'_>) {
|
2015-07-31 02:04:06 -05:00
|
|
|
match node {
|
2019-12-22 16:42:04 -06:00
|
|
|
pprust_hir::AnnNode::Name(_) => {}
|
2018-08-22 16:05:19 -05:00
|
|
|
pprust_hir::AnnNode::Item(item) => {
|
2019-06-24 13:15:11 -05:00
|
|
|
s.s.space();
|
2021-01-30 10:47:51 -06:00
|
|
|
s.synth_comment(format!("hir_id: {}", item.hir_id()));
|
2015-07-31 02:04:06 -05:00
|
|
|
}
|
2018-08-22 16:05:19 -05:00
|
|
|
pprust_hir::AnnNode::SubItem(id) => {
|
2019-06-24 13:15:11 -05:00
|
|
|
s.s.space();
|
2019-06-29 09:31:43 -05:00
|
|
|
s.synth_comment(id.to_string());
|
2015-07-31 02:04:06 -05:00
|
|
|
}
|
2018-08-22 16:05:19 -05:00
|
|
|
pprust_hir::AnnNode::Block(blk) => {
|
2019-06-24 13:15:11 -05:00
|
|
|
s.s.space();
|
2019-06-29 09:31:43 -05:00
|
|
|
s.synth_comment(format!("block hir_id: {}", blk.hir_id));
|
2015-07-31 02:04:06 -05:00
|
|
|
}
|
2018-08-22 16:05:19 -05:00
|
|
|
pprust_hir::AnnNode::Expr(expr) => {
|
2019-06-24 13:15:11 -05:00
|
|
|
s.s.space();
|
2019-06-29 09:31:43 -05:00
|
|
|
s.synth_comment(format!("expr hir_id: {}", expr.hir_id));
|
|
|
|
s.pclose();
|
2015-07-31 02:04:06 -05:00
|
|
|
}
|
2018-08-22 16:05:19 -05:00
|
|
|
pprust_hir::AnnNode::Pat(pat) => {
|
2019-06-24 13:15:11 -05:00
|
|
|
s.s.space();
|
2019-06-29 09:31:43 -05: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 02:04:06 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-25 19:21:50 -06:00
|
|
|
struct HygieneAnnotation<'a> {
|
2019-12-22 16:42:04 -06:00
|
|
|
sess: &'a Session,
|
2014-08-11 07:01:37 -05:00
|
|
|
}
|
|
|
|
|
2017-01-25 19:21:50 -06:00
|
|
|
impl<'a> PrinterSupport for HygieneAnnotation<'a> {
|
|
|
|
fn sess(&self) -> &Session {
|
2015-11-10 14:48:44 -06:00
|
|
|
self.sess
|
|
|
|
}
|
2014-08-11 07:01:37 -05:00
|
|
|
|
2018-07-12 02:11:39 -05:00
|
|
|
fn pp_ann(&self) -> &dyn pprust::PpAnn {
|
2015-11-10 14:48:44 -06:00
|
|
|
self
|
|
|
|
}
|
2014-08-11 07:01:37 -05:00
|
|
|
}
|
|
|
|
|
2017-01-25 19:21:50 -06:00
|
|
|
impl<'a> pprust::PpAnn for HygieneAnnotation<'a> {
|
2019-06-24 13:15:11 -05:00
|
|
|
fn post(&self, s: &mut pprust::State<'_>, node: pprust::AnnNode<'_>) {
|
2014-08-11 07:01:37 -05:00
|
|
|
match node {
|
2020-04-19 06:00:18 -05:00
|
|
|
pprust::AnnNode::Ident(&Ident { name, span }) => {
|
2019-06-24 13:15:11 -05:00
|
|
|
s.s.space();
|
2018-03-17 18:57:23 -05:00
|
|
|
s.synth_comment(format!("{}{:?}", name.as_u32(), span.ctxt()))
|
2014-08-11 07:01:37 -05:00
|
|
|
}
|
2018-08-22 16:05:19 -05:00
|
|
|
pprust::AnnNode::Name(&name) => {
|
2019-06-24 13:15:11 -05:00
|
|
|
s.s.space();
|
2016-11-16 02:21:52 -06:00
|
|
|
s.synth_comment(name.as_u32().to_string())
|
2014-08-11 07:01:37 -05:00
|
|
|
}
|
2019-07-14 15:17:37 -05:00
|
|
|
pprust::AnnNode::Crate(_) => {
|
|
|
|
s.s.hardbreak();
|
|
|
|
let verbose = self.sess.verbose();
|
2019-12-31 11:15:40 -06:00
|
|
|
s.synth_comment(rustc_span::hygiene::debug_hygiene_data(verbose));
|
2019-07-14 15:17:37 -05:00
|
|
|
s.s.hardbreak_if_not_bol();
|
|
|
|
}
|
2019-06-24 13:15:11 -05:00
|
|
|
_ => {}
|
2014-08-11 07:01:37 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-25 12:17:45 -05:00
|
|
|
struct TypedAnnotation<'tcx> {
|
2019-06-13 16:48:52 -05:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2020-07-17 03:47:04 -05:00
|
|
|
maybe_typeck_results: Cell<Option<&'tcx ty::TypeckResults<'tcx>>>,
|
2014-08-11 05:59:35 -05:00
|
|
|
}
|
|
|
|
|
2020-06-25 12:17:45 -05:00
|
|
|
impl<'tcx> HirPrinterSupport<'tcx> for TypedAnnotation<'tcx> {
|
2019-06-21 13:27:44 -05:00
|
|
|
fn sess(&self) -> &Session {
|
2015-11-10 14:48:44 -06:00
|
|
|
&self.tcx.sess
|
|
|
|
}
|
2014-08-11 05:59:35 -05:00
|
|
|
|
2020-02-09 08:32:00 -06:00
|
|
|
fn hir_map(&self) -> Option<hir_map::Map<'tcx>> {
|
|
|
|
Some(self.tcx.hir())
|
2014-08-11 05:59:35 -05:00
|
|
|
}
|
2014-12-15 20:11:09 -06:00
|
|
|
|
2020-03-06 17:56:32 -06:00
|
|
|
fn pp_ann(&self) -> &dyn pprust_hir::PpAnn {
|
2015-11-10 14:48:44 -06:00
|
|
|
self
|
|
|
|
}
|
2014-08-11 05:59:35 -05:00
|
|
|
}
|
|
|
|
|
2020-06-25 12:17:45 -05:00
|
|
|
impl<'tcx> pprust_hir::PpAnn for TypedAnnotation<'tcx> {
|
2019-06-24 13:15:11 -05:00
|
|
|
fn nested(&self, state: &mut pprust_hir::State<'_>, nested: pprust_hir::Nested) {
|
2020-07-17 03:47:04 -05:00
|
|
|
let old_maybe_typeck_results = self.maybe_typeck_results.get();
|
2017-01-06 13:54:24 -06:00
|
|
|
if let pprust_hir::Nested::Body(id) = nested {
|
2020-07-17 03:47:04 -05:00
|
|
|
self.maybe_typeck_results.set(Some(self.tcx.typeck_body(id)));
|
2017-01-06 13:54:24 -06:00
|
|
|
}
|
2020-03-23 20:44:41 -05:00
|
|
|
let pp_ann = &(&self.tcx.hir() as &dyn hir::intravisit::Map<'_>);
|
|
|
|
pprust_hir::PpAnn::nested(pp_ann, state, nested);
|
2020-07-17 03:47:04 -05:00
|
|
|
self.maybe_typeck_results.set(old_maybe_typeck_results);
|
2016-12-27 02:00:18 -06:00
|
|
|
}
|
2019-06-24 13:15:11 -05:00
|
|
|
fn pre(&self, s: &mut pprust_hir::State<'_>, node: pprust_hir::AnnNode<'_>) {
|
2020-03-22 07:36:56 -05:00
|
|
|
if let pprust_hir::AnnNode::Expr(_) = node {
|
|
|
|
s.popen();
|
2014-08-11 05:59:35 -05:00
|
|
|
}
|
|
|
|
}
|
2019-06-24 13:15:11 -05:00
|
|
|
fn post(&self, s: &mut pprust_hir::State<'_>, node: pprust_hir::AnnNode<'_>) {
|
2020-03-22 07:36:56 -05:00
|
|
|
if let pprust_hir::AnnNode::Expr(expr) = node {
|
2021-06-26 15:26:26 -05:00
|
|
|
let typeck_results = self.maybe_typeck_results.get().or_else(|| {
|
|
|
|
self.tcx
|
|
|
|
.hir()
|
|
|
|
.maybe_body_owned_by(self.tcx.hir().local_def_id_to_hir_id(expr.hir_id.owner))
|
|
|
|
.map(|body_id| self.tcx.typeck_body(body_id))
|
|
|
|
});
|
2021-06-26 09:05:53 -05: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 07:36:56 -05:00
|
|
|
s.pclose();
|
2014-08-11 05:59:35 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-05 16:12:11 -05:00
|
|
|
fn get_source(input: &Input, sess: &Session) -> (String, FileName) {
|
2019-11-19 07:12:30 -06:00
|
|
|
let src_name = input.source_name();
|
2021-01-09 18:04:48 -06:00
|
|
|
let src = String::clone(
|
|
|
|
&sess
|
|
|
|
.source_map()
|
|
|
|
.get_source_file(&src_name)
|
|
|
|
.expect("get_source_file")
|
|
|
|
.src
|
|
|
|
.as_ref()
|
|
|
|
.expect("src"),
|
|
|
|
);
|
2016-04-20 17:29:49 -05:00
|
|
|
(src, src_name)
|
|
|
|
}
|
|
|
|
|
2021-02-16 01:09:07 -06:00
|
|
|
fn write_or_print(out: &str, ofile: Option<&Path>) {
|
2016-04-20 17:29:49 -05:00
|
|
|
match ofile {
|
2021-02-16 01:09:07 -06:00
|
|
|
None => print!("{}", out),
|
|
|
|
Some(p) => {
|
|
|
|
if let Err(e) = std::fs::write(p, out) {
|
|
|
|
panic!("print-print failed to write {} due to {}", p.display(), e);
|
|
|
|
}
|
|
|
|
}
|
2016-04-20 17:29:49 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-22 16:42:04 -06:00
|
|
|
pub fn print_after_parsing(
|
|
|
|
sess: &Session,
|
|
|
|
input: &Input,
|
|
|
|
krate: &ast::Crate,
|
|
|
|
ppm: PpMode,
|
|
|
|
ofile: Option<&Path>,
|
|
|
|
) {
|
2016-04-20 17:29:49 -05:00
|
|
|
let (src, src_name) = get_source(input, sess);
|
|
|
|
|
2021-02-19 15:40:28 -06:00
|
|
|
let out = match ppm {
|
|
|
|
Source(s) => {
|
|
|
|
// Silently ignores an identified node.
|
|
|
|
call_with_pp_support(&s, sess, None, move |annotation| {
|
|
|
|
debug!("pretty printing source code {:?}", s);
|
|
|
|
let sess = annotation.sess();
|
|
|
|
let parse = &sess.parse_sess;
|
|
|
|
pprust::print_crate(
|
|
|
|
sess.source_map(),
|
|
|
|
krate,
|
|
|
|
src_name,
|
|
|
|
src,
|
|
|
|
annotation.pp_ann(),
|
|
|
|
false,
|
|
|
|
parse.edition,
|
|
|
|
)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
AstTree(PpAstTreeMode::Normal) => {
|
|
|
|
debug!("pretty printing AST tree");
|
|
|
|
format!("{:#?}", krate)
|
|
|
|
}
|
|
|
|
_ => unreachable!(),
|
2016-04-20 17:29:49 -05:00
|
|
|
};
|
|
|
|
|
2021-02-16 01:09:07 -06:00
|
|
|
write_or_print(&out, ofile);
|
2016-04-20 17:29:49 -05:00
|
|
|
}
|
|
|
|
|
2018-12-08 13:30:23 -06:00
|
|
|
pub fn print_after_hir_lowering<'tcx>(
|
2019-06-13 16:48:52 -05:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2018-12-08 13:30:23 -06:00
|
|
|
input: &Input,
|
|
|
|
krate: &ast::Crate,
|
|
|
|
ppm: PpMode,
|
2019-06-11 16:11:55 -05:00
|
|
|
ofile: Option<&Path>,
|
|
|
|
) {
|
2016-04-20 17:29:49 -05:00
|
|
|
if ppm.needs_analysis() {
|
2019-12-22 16:42:04 -06:00
|
|
|
abort_on_err(print_with_analysis(tcx, ppm, ofile), tcx.sess);
|
2016-04-20 17:29:49 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-12-08 13:30:23 -06:00
|
|
|
let (src, src_name) = get_source(input, tcx.sess);
|
2016-04-20 17:29:49 -05:00
|
|
|
|
2021-02-19 12:08:12 -06:00
|
|
|
let out = match ppm {
|
2021-02-18 13:21:18 -06:00
|
|
|
Source(s) => {
|
2019-12-22 16:42:04 -06:00
|
|
|
// Silently ignores an identified node.
|
|
|
|
call_with_pp_support(&s, tcx.sess, Some(tcx), move |annotation| {
|
|
|
|
debug!("pretty printing source code {:?}", s);
|
|
|
|
let sess = annotation.sess();
|
2020-01-11 03:33:18 -06:00
|
|
|
let parse = &sess.parse_sess;
|
2021-02-19 12:08:12 -06:00
|
|
|
pprust::print_crate(
|
2019-12-22 16:42:04 -06:00
|
|
|
sess.source_map(),
|
|
|
|
krate,
|
|
|
|
src_name,
|
|
|
|
src,
|
|
|
|
annotation.pp_ann(),
|
|
|
|
true,
|
2020-01-11 03:33:18 -06:00
|
|
|
parse.edition,
|
2019-12-22 16:42:04 -06:00
|
|
|
)
|
|
|
|
})
|
|
|
|
}
|
2015-07-31 02:04:06 -05:00
|
|
|
|
2021-02-19 15:40:28 -06:00
|
|
|
AstTree(PpAstTreeMode::Expanded) => {
|
|
|
|
debug!("pretty-printing expanded AST");
|
|
|
|
format!("{:#?}", krate)
|
|
|
|
}
|
|
|
|
|
2021-02-19 12:08:12 -06:00
|
|
|
Hir(s) => call_with_pp_support_hir(&s, tcx, move |annotation, krate| {
|
|
|
|
debug!("pretty printing HIR {:?}", s);
|
|
|
|
let sess = annotation.sess();
|
|
|
|
let sm = sess.source_map();
|
|
|
|
pprust_hir::print_crate(sm, krate, src_name, src, annotation.pp_ann())
|
|
|
|
}),
|
2017-11-02 07:58:13 -05:00
|
|
|
|
2021-02-19 12:08:12 -06:00
|
|
|
HirTree => call_with_pp_support_hir(&PpHirMode::Normal, tcx, move |_annotation, krate| {
|
|
|
|
debug!("pretty printing HIR tree");
|
|
|
|
format!("{:#?}", krate)
|
|
|
|
}),
|
2016-04-20 17:29:49 -05:00
|
|
|
|
2019-12-22 16:42:04 -06:00
|
|
|
_ => unreachable!(),
|
2021-02-19 12:08:12 -06:00
|
|
|
};
|
2019-12-22 16:42:04 -06:00
|
|
|
|
2021-02-16 01:09:07 -06:00
|
|
|
write_or_print(&out, ofile);
|
2016-04-20 17:29:49 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// In an ideal world, this would be a public function called by the driver after
|
2019-01-07 04:46:44 -06:00
|
|
|
// analysis is performed. However, we want to call `phase_3_run_analysis_passes`
|
2016-04-20 17:29:49 -05:00
|
|
|
// with a different callback than the standard driver, so that isn't easy.
|
|
|
|
// Instead, we call that function ourselves.
|
2019-06-21 13:27:44 -05:00
|
|
|
fn print_with_analysis(
|
|
|
|
tcx: TyCtxt<'_>,
|
2018-12-08 13:30:23 -06:00
|
|
|
ppm: PpMode,
|
2019-06-11 16:11:55 -05:00
|
|
|
ofile: Option<&Path>,
|
2018-12-08 13:30:23 -06:00
|
|
|
) -> Result<(), ErrorReported> {
|
2021-05-11 07:50:54 -05:00
|
|
|
tcx.analysis(())?;
|
2021-03-12 01:02:51 -06:00
|
|
|
let out = match ppm {
|
|
|
|
Mir => {
|
|
|
|
let mut out = Vec::new();
|
|
|
|
write_mir_pretty(tcx, None, &mut out).unwrap();
|
|
|
|
String::from_utf8(out).unwrap()
|
|
|
|
}
|
|
|
|
|
|
|
|
MirCFG => {
|
|
|
|
let mut out = Vec::new();
|
|
|
|
write_mir_graphviz(tcx, None, &mut out).unwrap();
|
|
|
|
String::from_utf8(out).unwrap()
|
|
|
|
}
|
|
|
|
|
|
|
|
ThirTree => {
|
2021-07-24 16:18:15 -05:00
|
|
|
let mut out = String::new();
|
|
|
|
abort_on_err(rustc_typeck::check_crate(tcx), tcx.sess);
|
|
|
|
debug!("pretty printing THIR tree");
|
|
|
|
for did in tcx.body_owners() {
|
|
|
|
let _ = writeln!(
|
|
|
|
out,
|
|
|
|
"{:?}:\n{}\n",
|
|
|
|
did,
|
|
|
|
tcx.thir_tree(ty::WithOptConstParam::unknown(did))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
out
|
2021-03-12 01:02:51 -06:00
|
|
|
}
|
|
|
|
|
2018-12-08 13:30:23 -06:00
|
|
|
_ => unreachable!(),
|
2021-03-12 01:02:51 -06:00
|
|
|
};
|
2014-08-11 05:59:35 -05:00
|
|
|
|
2021-03-12 01:02:51 -06:00
|
|
|
write_or_print(&out, ofile);
|
2018-12-08 13:30:23 -06:00
|
|
|
|
|
|
|
Ok(())
|
2014-08-11 05:59:35 -05:00
|
|
|
}
|