2014-08-11 20:59:35 +10:00
|
|
|
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
|
|
|
//! The various pretty print routines.
|
|
|
|
|
2014-11-06 00:05:53 -08:00
|
|
|
pub use self::UserIdentifiedItem::*;
|
|
|
|
pub use self::PpSourceMode::*;
|
|
|
|
pub use self::PpMode::*;
|
|
|
|
use self::NodesMatchingUII::*;
|
|
|
|
|
2016-12-15 11:13:24 +00:00
|
|
|
use {abort_on_err, driver};
|
2014-11-27 09:57:47 -05:00
|
|
|
|
2017-12-03 13:57:25 +01:00
|
|
|
use rustc::ty::{self, TyCtxt, Resolutions, AllArenas};
|
2016-03-22 17:30:57 +02:00
|
|
|
use rustc::cfg;
|
|
|
|
use rustc::cfg::graphviz::LabelledCFG;
|
2014-11-27 09:57:47 -05:00
|
|
|
use rustc::session::Session;
|
2017-09-13 20:26:39 -07:00
|
|
|
use rustc::session::config::{Input, OutputFilenames};
|
2014-12-05 14:17:35 -05:00
|
|
|
use rustc_borrowck as borrowck;
|
|
|
|
use rustc_borrowck::graphviz as borrowck_dot;
|
2018-08-05 12:04:56 +02:00
|
|
|
use rustc_data_structures::thin_vec::ThinVec;
|
2018-07-31 15:23:31 -06:00
|
|
|
use rustc_metadata::cstore::CStore;
|
2014-08-11 20:59:35 +10:00
|
|
|
|
2017-03-09 20:10:05 +02:00
|
|
|
use rustc_mir::util::{write_mir_pretty, write_mir_graphviz};
|
2016-02-14 19:23:05 +01:00
|
|
|
|
2016-02-08 12:44:45 +01:00
|
|
|
use syntax::ast::{self, BlockCheckMode};
|
2015-01-03 22:42:21 -05:00
|
|
|
use syntax::fold::{self, Folder};
|
2017-06-24 21:22:42 -06:00
|
|
|
use syntax::print::{pprust};
|
2015-09-14 21:58:20 +12:00
|
|
|
use syntax::print::pprust::PrintState;
|
2014-12-17 13:37:26 +01:00
|
|
|
use syntax::ptr::P;
|
2017-12-14 08:09:19 +01:00
|
|
|
use syntax_pos::{self, FileName};
|
2014-08-11 20:59:35 +10:00
|
|
|
|
|
|
|
use graphviz as dot;
|
2018-08-30 11:42:16 +02:00
|
|
|
use smallvec::SmallVec;
|
2014-08-11 20:59:35 +10:00
|
|
|
|
2017-01-06 21:54:24 +02:00
|
|
|
use std::cell::Cell;
|
2015-02-26 21:00:43 -08:00
|
|
|
use std::fs::File;
|
|
|
|
use std::io::{self, Write};
|
2014-08-11 20:59:35 +10:00
|
|
|
use std::option;
|
2016-04-20 16:24:14 +12:00
|
|
|
use std::path::Path;
|
2014-11-15 15:52:00 +11:00
|
|
|
use std::str::FromStr;
|
2017-07-19 16:09:22 +08:00
|
|
|
use std::mem;
|
2014-08-11 20:59:35 +10:00
|
|
|
|
2016-03-29 08:50:44 +03:00
|
|
|
use rustc::hir::map as hir_map;
|
2016-12-27 10:00:18 +02:00
|
|
|
use rustc::hir::map::blocks;
|
2016-03-29 08:50:44 +03:00
|
|
|
use rustc::hir;
|
|
|
|
use rustc::hir::print as pprust_hir;
|
2015-07-31 00:04:06 -07:00
|
|
|
|
2015-03-30 09:38:44 -04:00
|
|
|
#[derive(Copy, Clone, PartialEq, Debug)]
|
2014-08-11 20:59:35 +10:00
|
|
|
pub enum PpSourceMode {
|
|
|
|
PpmNormal,
|
2014-12-17 13:37:26 +01:00
|
|
|
PpmEveryBodyLoops,
|
2014-08-11 20:59:35 +10:00
|
|
|
PpmExpanded,
|
|
|
|
PpmIdentified,
|
|
|
|
PpmExpandedIdentified,
|
2014-08-11 22:01:37 +10:00
|
|
|
PpmExpandedHygiene,
|
2015-07-31 00:04:06 -07:00
|
|
|
PpmTyped,
|
2014-08-11 20:59:35 +10:00
|
|
|
}
|
|
|
|
|
2015-03-30 09:38:44 -04:00
|
|
|
#[derive(Copy, Clone, PartialEq, Debug)]
|
2015-01-12 22:42:12 +01:00
|
|
|
pub enum PpFlowGraphMode {
|
|
|
|
Default,
|
|
|
|
/// Drops the labels from the edges in the flowgraph output. This
|
2018-01-14 21:11:40 -06:00
|
|
|
/// is mostly for use in the -Z unpretty flowgraph run-make tests,
|
2015-01-12 22:42:12 +01:00
|
|
|
/// since the labels are largely uninteresting in those cases and
|
|
|
|
/// have become a pain to maintain.
|
|
|
|
UnlabelledEdges,
|
|
|
|
}
|
2015-03-30 09:38:44 -04:00
|
|
|
#[derive(Copy, Clone, PartialEq, Debug)]
|
2014-08-11 20:59:35 +10:00
|
|
|
pub enum PpMode {
|
|
|
|
PpmSource(PpSourceMode),
|
2015-07-31 00:04:06 -07:00
|
|
|
PpmHir(PpSourceMode),
|
2017-11-02 08:58:13 -04:00
|
|
|
PpmHirTree(PpSourceMode),
|
2015-01-12 22:42:12 +01:00
|
|
|
PpmFlowGraph(PpFlowGraphMode),
|
2016-02-14 19:23:05 +01:00
|
|
|
PpmMir,
|
2016-03-12 19:07:00 +02:00
|
|
|
PpmMirCFG,
|
2014-08-11 20:59:35 +10:00
|
|
|
}
|
|
|
|
|
2016-04-21 10:29:49 +12:00
|
|
|
impl PpMode {
|
|
|
|
pub fn needs_ast_map(&self, opt_uii: &Option<UserIdentifiedItem>) -> bool {
|
|
|
|
match *self {
|
|
|
|
PpmSource(PpmNormal) |
|
|
|
|
PpmSource(PpmEveryBodyLoops) |
|
|
|
|
PpmSource(PpmIdentified) => opt_uii.is_some(),
|
|
|
|
|
|
|
|
PpmSource(PpmExpanded) |
|
|
|
|
PpmSource(PpmExpandedIdentified) |
|
|
|
|
PpmSource(PpmExpandedHygiene) |
|
|
|
|
PpmHir(_) |
|
2017-11-02 08:58:13 -04:00
|
|
|
PpmHirTree(_) |
|
2016-04-21 10:29:49 +12:00
|
|
|
PpmMir |
|
|
|
|
PpmMirCFG |
|
|
|
|
PpmFlowGraph(_) => true,
|
|
|
|
PpmSource(PpmTyped) => panic!("invalid state"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn needs_analysis(&self) -> bool {
|
|
|
|
match *self {
|
2016-10-20 07:08:43 +05:30
|
|
|
PpmMir | PpmMirCFG | PpmFlowGraph(_) => true,
|
|
|
|
_ => false,
|
2016-04-21 10:29:49 +12:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-17 15:13:38 +01:00
|
|
|
pub fn parse_pretty(sess: &Session,
|
|
|
|
name: &str,
|
2015-11-10 20:48:44 +00:00
|
|
|
extended: bool)
|
|
|
|
-> (PpMode, Option<UserIdentifiedItem>) {
|
2015-04-01 11:28:34 -07:00
|
|
|
let mut split = name.splitn(2, '=');
|
2014-08-11 20:59:35 +10:00
|
|
|
let first = split.next().unwrap();
|
|
|
|
let opt_second = split.next();
|
2014-12-17 15:13:38 +01:00
|
|
|
let first = match (first, extended) {
|
2015-11-10 20:48:44 +00:00
|
|
|
("normal", _) => PpmSource(PpmNormal),
|
|
|
|
("identified", _) => PpmSource(PpmIdentified),
|
2014-12-17 15:13:38 +01:00
|
|
|
("everybody_loops", true) => PpmSource(PpmEveryBodyLoops),
|
2015-11-10 20:48:44 +00:00
|
|
|
("expanded", _) => PpmSource(PpmExpanded),
|
2014-12-17 15:13:38 +01:00
|
|
|
("expanded,identified", _) => PpmSource(PpmExpandedIdentified),
|
|
|
|
("expanded,hygiene", _) => PpmSource(PpmExpandedHygiene),
|
2015-11-10 20:48:44 +00:00
|
|
|
("hir", true) => PpmHir(PpmNormal),
|
2015-10-22 16:05:51 -04:00
|
|
|
("hir,identified", true) => PpmHir(PpmIdentified),
|
2015-11-10 20:48:44 +00:00
|
|
|
("hir,typed", true) => PpmHir(PpmTyped),
|
2017-11-02 08:58:13 -04:00
|
|
|
("hir-tree", true) => PpmHirTree(PpmNormal),
|
2016-02-14 19:23:05 +01:00
|
|
|
("mir", true) => PpmMir,
|
2016-03-12 19:07:00 +02:00
|
|
|
("mir-cfg", true) => PpmMirCFG,
|
2015-11-10 20:48:44 +00:00
|
|
|
("flowgraph", true) => PpmFlowGraph(PpFlowGraphMode::Default),
|
|
|
|
("flowgraph,unlabelled", true) => PpmFlowGraph(PpFlowGraphMode::UnlabelledEdges),
|
2014-08-11 20:59:35 +10:00
|
|
|
_ => {
|
2014-12-17 15:13:38 +01:00
|
|
|
if extended {
|
2015-11-10 20:48:44 +00:00
|
|
|
sess.fatal(&format!("argument to `unpretty` must be one of `normal`, \
|
|
|
|
`expanded`, `flowgraph[,unlabelled]=<nodeid>`, \
|
|
|
|
`identified`, `expanded,identified`, `everybody_loops`, \
|
2016-02-14 19:23:05 +01:00
|
|
|
`hir`, `hir,identified`, `hir,typed`, or `mir`; got {}",
|
2015-11-10 20:48:44 +00:00
|
|
|
name));
|
2014-12-17 15:13:38 +01:00
|
|
|
} else {
|
2015-11-10 20:48:44 +00:00
|
|
|
sess.fatal(&format!("argument to `pretty` must be one of `normal`, `expanded`, \
|
|
|
|
`identified`, or `expanded,identified`; got {}",
|
|
|
|
name));
|
2014-12-17 15:13:38 +01:00
|
|
|
}
|
2014-08-11 20:59:35 +10:00
|
|
|
}
|
|
|
|
};
|
2015-01-27 22:52:32 -08:00
|
|
|
let opt_second = opt_second.and_then(|s| s.parse::<UserIdentifiedItem>().ok());
|
2014-08-11 20:59:35 +10:00
|
|
|
(first, opt_second)
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 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`.
|
|
|
|
//
|
|
|
|
// (The `use_once_payload` is working around the current lack of once
|
|
|
|
// functions in the compiler.)
|
|
|
|
|
2014-08-11 21:12:23 +10:00
|
|
|
impl PpSourceMode {
|
2014-08-11 20:59:35 +10:00
|
|
|
/// Constructs a `PrinterSupport` object and passes it to `f`.
|
2017-06-25 09:54:09 -06:00
|
|
|
fn call_with_pp_support<'tcx, A, F>(&self,
|
2018-10-12 14:36:10 +02:00
|
|
|
sess: &'tcx Session,
|
|
|
|
hir_map: Option<&hir_map::Map<'tcx>>,
|
|
|
|
f: F)
|
|
|
|
-> A
|
2018-07-12 09:11:39 +02:00
|
|
|
where F: FnOnce(&dyn PrinterSupport) -> A
|
2014-12-09 16:32:45 -05:00
|
|
|
{
|
2014-08-11 21:12:23 +10:00
|
|
|
match *self {
|
2014-12-17 13:37:26 +01:00
|
|
|
PpmNormal | PpmEveryBodyLoops | PpmExpanded => {
|
2015-11-10 20:48:44 +00:00
|
|
|
let annotation = NoAnn {
|
2017-08-06 22:54:09 -07:00
|
|
|
sess,
|
2017-01-26 03:21:50 +02:00
|
|
|
hir_map: hir_map.map(|m| m.clone()),
|
2015-11-10 20:48:44 +00:00
|
|
|
};
|
2017-06-25 09:54:09 -06:00
|
|
|
f(&annotation)
|
2014-08-11 21:12:23 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
PpmIdentified | PpmExpandedIdentified => {
|
2015-11-10 20:48:44 +00:00
|
|
|
let annotation = IdentifiedAnnotation {
|
2017-08-06 22:54:09 -07:00
|
|
|
sess,
|
2017-01-26 03:21:50 +02:00
|
|
|
hir_map: hir_map.map(|m| m.clone()),
|
2015-11-10 20:48:44 +00:00
|
|
|
};
|
2017-06-25 09:54:09 -06:00
|
|
|
f(&annotation)
|
2014-08-11 21:12:23 +10:00
|
|
|
}
|
2014-08-11 22:01:37 +10:00
|
|
|
PpmExpandedHygiene => {
|
2015-11-10 20:48:44 +00:00
|
|
|
let annotation = HygieneAnnotation {
|
2017-08-06 22:54:09 -07:00
|
|
|
sess,
|
2015-11-10 20:48:44 +00:00
|
|
|
};
|
2017-06-25 09:54:09 -06:00
|
|
|
f(&annotation)
|
2014-08-11 22:01:37 +10:00
|
|
|
}
|
2015-07-31 00:04:06 -07:00
|
|
|
_ => panic!("Should use call_with_pp_support_hir"),
|
|
|
|
}
|
|
|
|
}
|
2018-10-12 14:36:10 +02:00
|
|
|
fn call_with_pp_support_hir<'tcx, A, F>(
|
|
|
|
&self,
|
|
|
|
sess: &'tcx Session,
|
|
|
|
cstore: &'tcx CStore,
|
|
|
|
hir_map: &hir_map::Map<'tcx>,
|
|
|
|
analysis: &ty::CrateAnalysis,
|
|
|
|
resolutions: &Resolutions,
|
|
|
|
arenas: &'tcx AllArenas<'tcx>,
|
|
|
|
output_filenames: &OutputFilenames,
|
|
|
|
id: &str,
|
|
|
|
f: F
|
|
|
|
) -> A
|
2018-07-12 09:11:39 +02:00
|
|
|
where F: FnOnce(&dyn HirPrinterSupport, &hir::Crate) -> A
|
2015-07-31 00:04:06 -07:00
|
|
|
{
|
|
|
|
match *self {
|
|
|
|
PpmNormal => {
|
2015-11-10 20:48:44 +00:00
|
|
|
let annotation = NoAnn {
|
2017-08-06 22:54:09 -07:00
|
|
|
sess,
|
2017-01-26 03:21:50 +02:00
|
|
|
hir_map: Some(hir_map.clone()),
|
2015-11-10 20:48:44 +00:00
|
|
|
};
|
2017-06-25 09:54:09 -06:00
|
|
|
f(&annotation, hir_map.forest.krate())
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
PpmIdentified => {
|
|
|
|
let annotation = IdentifiedAnnotation {
|
2017-08-06 22:54:09 -07:00
|
|
|
sess,
|
2017-01-26 03:21:50 +02:00
|
|
|
hir_map: Some(hir_map.clone()),
|
2015-07-31 00:04:06 -07:00
|
|
|
};
|
2017-06-25 09:54:09 -06:00
|
|
|
f(&annotation, hir_map.forest.krate())
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
2014-08-11 21:12:23 +10:00
|
|
|
PpmTyped => {
|
2017-11-12 18:20:38 +02:00
|
|
|
let control = &driver::CompileController::basic();
|
2018-05-08 16:10:16 +03:00
|
|
|
let codegen_backend = ::get_codegen_backend(sess);
|
|
|
|
abort_on_err(driver::phase_3_run_analysis_passes(&*codegen_backend,
|
2017-10-30 18:42:21 +01:00
|
|
|
control,
|
2017-11-12 18:20:38 +02:00
|
|
|
sess,
|
2017-09-05 16:48:24 +02:00
|
|
|
cstore,
|
2017-01-26 03:21:50 +02:00
|
|
|
hir_map.clone(),
|
2016-05-05 08:54:18 +00:00
|
|
|
analysis.clone(),
|
|
|
|
resolutions.clone(),
|
2016-01-21 13:19:20 +13:00
|
|
|
arenas,
|
|
|
|
id,
|
2017-09-13 20:26:39 -07:00
|
|
|
output_filenames,
|
2017-09-18 18:03:09 +02:00
|
|
|
|tcx, _, _, _| {
|
2017-08-11 11:56:26 +02:00
|
|
|
let empty_tables = ty::TypeckTables::empty(None);
|
2017-01-06 21:54:24 +02:00
|
|
|
let annotation = TypedAnnotation {
|
2017-08-06 22:54:09 -07:00
|
|
|
tcx,
|
2017-01-06 21:54:24 +02:00
|
|
|
tables: Cell::new(&empty_tables)
|
|
|
|
};
|
2017-12-28 06:05:45 +01:00
|
|
|
tcx.dep_graph.with_ignore(|| {
|
|
|
|
f(&annotation, hir_map.forest.krate())
|
|
|
|
})
|
2016-10-20 07:08:43 +05:30
|
|
|
}),
|
|
|
|
sess)
|
2014-08-11 21:12:23 +10:00
|
|
|
}
|
2015-07-31 00:04:06 -07:00
|
|
|
_ => panic!("Should use call_with_pp_support"),
|
2014-08-11 21:12:23 +10:00
|
|
|
}
|
|
|
|
}
|
2014-08-11 20:59:35 +10:00
|
|
|
}
|
|
|
|
|
2017-01-26 03:21:50 +02:00
|
|
|
trait PrinterSupport: pprust::PpAnn {
|
2014-08-11 20:59:35 +10:00
|
|
|
/// Provides a uniform interface for re-extracting a reference to a
|
|
|
|
/// `Session` from a value that now owns it.
|
|
|
|
fn sess<'a>(&'a self) -> &'a Session;
|
|
|
|
|
|
|
|
/// 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.)
|
2018-07-12 09:11:39 +02:00
|
|
|
fn pp_ann<'a>(&'a self) -> &'a dyn pprust::PpAnn;
|
2014-08-11 20:59:35 +10:00
|
|
|
}
|
|
|
|
|
2017-01-26 03:21:50 +02:00
|
|
|
trait HirPrinterSupport<'hir>: pprust_hir::PpAnn {
|
2015-07-31 00:04:06 -07:00
|
|
|
/// Provides a uniform interface for re-extracting a reference to a
|
|
|
|
/// `Session` from a value that now owns it.
|
|
|
|
fn sess<'a>(&'a self) -> &'a Session;
|
|
|
|
|
|
|
|
/// Provides a uniform interface for re-extracting a reference to an
|
|
|
|
/// `hir_map::Map` from a value that now owns it.
|
2017-01-26 03:21:50 +02:00
|
|
|
fn hir_map<'a>(&'a self) -> Option<&'a hir_map::Map<'hir>>;
|
2015-07-31 00:04:06 -07: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.)
|
2018-07-12 09:11:39 +02:00
|
|
|
fn pp_ann<'a>(&'a self) -> &'a dyn pprust_hir::PpAnn;
|
2016-04-06 13:51:55 +03:00
|
|
|
|
|
|
|
/// Computes an user-readable representation of a path, if possible.
|
|
|
|
fn node_path(&self, id: ast::NodeId) -> Option<String> {
|
2017-01-26 03:21:50 +02:00
|
|
|
self.hir_map().and_then(|map| map.def_path_from_id(id)).map(|path| {
|
2016-10-20 07:08:43 +05:30
|
|
|
path.data
|
|
|
|
.into_iter()
|
|
|
|
.map(|elem| elem.data.to_string())
|
|
|
|
.collect::<Vec<_>>()
|
|
|
|
.join("::")
|
2016-04-06 13:51:55 +03:00
|
|
|
})
|
|
|
|
}
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
|
|
|
|
2017-01-26 03:21:50 +02:00
|
|
|
struct NoAnn<'hir> {
|
|
|
|
sess: &'hir Session,
|
|
|
|
hir_map: Option<hir_map::Map<'hir>>,
|
2014-08-11 20:59:35 +10:00
|
|
|
}
|
|
|
|
|
2017-01-26 03:21:50 +02:00
|
|
|
impl<'hir> PrinterSupport for NoAnn<'hir> {
|
2015-11-10 20:48:44 +00:00
|
|
|
fn sess<'a>(&'a self) -> &'a Session {
|
|
|
|
self.sess
|
|
|
|
}
|
2014-08-11 20:59:35 +10:00
|
|
|
|
2018-07-12 09:11:39 +02:00
|
|
|
fn pp_ann<'a>(&'a self) -> &'a dyn pprust::PpAnn {
|
2015-11-10 20:48:44 +00:00
|
|
|
self
|
|
|
|
}
|
2014-08-11 20:59:35 +10:00
|
|
|
}
|
|
|
|
|
2017-01-26 03:21:50 +02:00
|
|
|
impl<'hir> HirPrinterSupport<'hir> for NoAnn<'hir> {
|
2015-11-10 20:48:44 +00:00
|
|
|
fn sess<'a>(&'a self) -> &'a Session {
|
|
|
|
self.sess
|
|
|
|
}
|
2015-07-31 00:04:06 -07:00
|
|
|
|
2017-01-26 03:21:50 +02:00
|
|
|
fn hir_map<'a>(&'a self) -> Option<&'a hir_map::Map<'hir>> {
|
|
|
|
self.hir_map.as_ref()
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
|
|
|
|
2018-07-12 09:11:39 +02:00
|
|
|
fn pp_ann<'a>(&'a self) -> &'a dyn pprust_hir::PpAnn {
|
2015-11-10 20:48:44 +00:00
|
|
|
self
|
|
|
|
}
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
|
|
|
|
2017-01-26 03:21:50 +02:00
|
|
|
impl<'hir> pprust::PpAnn for NoAnn<'hir> {}
|
|
|
|
impl<'hir> pprust_hir::PpAnn for NoAnn<'hir> {
|
2016-12-27 10:00:18 +02:00
|
|
|
fn nested(&self, state: &mut pprust_hir::State, nested: pprust_hir::Nested)
|
|
|
|
-> io::Result<()> {
|
2017-01-26 03:21:50 +02:00
|
|
|
if let Some(ref map) = self.hir_map {
|
2016-12-27 10:00:18 +02:00
|
|
|
pprust_hir::PpAnn::nested(map, state, nested)
|
|
|
|
} else {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-08-11 20:59:35 +10:00
|
|
|
|
2017-01-26 03:21:50 +02:00
|
|
|
struct IdentifiedAnnotation<'hir> {
|
|
|
|
sess: &'hir Session,
|
|
|
|
hir_map: Option<hir_map::Map<'hir>>,
|
2014-08-11 20:59:35 +10:00
|
|
|
}
|
|
|
|
|
2017-01-26 03:21:50 +02:00
|
|
|
impl<'hir> PrinterSupport for IdentifiedAnnotation<'hir> {
|
2015-11-10 20:48:44 +00:00
|
|
|
fn sess<'a>(&'a self) -> &'a Session {
|
|
|
|
self.sess
|
|
|
|
}
|
2014-08-11 20:59:35 +10:00
|
|
|
|
2018-07-12 09:11:39 +02:00
|
|
|
fn pp_ann<'a>(&'a self) -> &'a dyn pprust::PpAnn {
|
2015-11-10 20:48:44 +00:00
|
|
|
self
|
|
|
|
}
|
2014-08-11 20:59:35 +10:00
|
|
|
}
|
|
|
|
|
2017-01-26 03:21:50 +02:00
|
|
|
impl<'hir> pprust::PpAnn for IdentifiedAnnotation<'hir> {
|
2015-11-10 20:48:44 +00:00
|
|
|
fn pre(&self, s: &mut pprust::State, node: pprust::AnnNode) -> io::Result<()> {
|
2014-08-11 20:59:35 +10:00
|
|
|
match node {
|
2018-08-22 22:05:19 +01:00
|
|
|
pprust::AnnNode::Expr(_) => s.popen(),
|
2015-11-10 20:48:44 +00:00
|
|
|
_ => Ok(()),
|
2014-08-11 20:59:35 +10:00
|
|
|
}
|
|
|
|
}
|
2015-11-10 20:48:44 +00:00
|
|
|
fn post(&self, s: &mut pprust::State, node: pprust::AnnNode) -> io::Result<()> {
|
2014-08-11 20:59:35 +10:00
|
|
|
match node {
|
2018-08-22 22:05:19 +01:00
|
|
|
pprust::AnnNode::Ident(_) |
|
|
|
|
pprust::AnnNode::Name(_) => Ok(()),
|
2014-08-11 22:01:37 +10:00
|
|
|
|
2018-08-22 22:05:19 +01:00
|
|
|
pprust::AnnNode::Item(item) => {
|
2017-06-24 21:22:42 -06:00
|
|
|
s.s.space()?;
|
2014-08-11 20:59:35 +10:00
|
|
|
s.synth_comment(item.id.to_string())
|
|
|
|
}
|
2018-08-22 22:05:19 +01:00
|
|
|
pprust::AnnNode::SubItem(id) => {
|
2017-06-24 21:22:42 -06:00
|
|
|
s.s.space()?;
|
2015-03-24 14:52:55 +13:00
|
|
|
s.synth_comment(id.to_string())
|
|
|
|
}
|
2018-08-22 22:05:19 +01:00
|
|
|
pprust::AnnNode::Block(blk) => {
|
2017-06-24 21:22:42 -06:00
|
|
|
s.s.space()?;
|
2014-08-11 20:59:35 +10:00
|
|
|
s.synth_comment(format!("block {}", blk.id))
|
|
|
|
}
|
2018-08-22 22:05:19 +01:00
|
|
|
pprust::AnnNode::Expr(expr) => {
|
2017-06-24 21:22:42 -06:00
|
|
|
s.s.space()?;
|
2016-03-22 22:01:37 -05:00
|
|
|
s.synth_comment(expr.id.to_string())?;
|
2014-08-11 20:59:35 +10:00
|
|
|
s.pclose()
|
|
|
|
}
|
2018-08-22 22:05:19 +01:00
|
|
|
pprust::AnnNode::Pat(pat) => {
|
2017-06-24 21:22:42 -06:00
|
|
|
s.s.space()?;
|
2014-08-11 20:59:35 +10:00
|
|
|
s.synth_comment(format!("pat {}", pat.id))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-26 03:21:50 +02:00
|
|
|
impl<'hir> HirPrinterSupport<'hir> for IdentifiedAnnotation<'hir> {
|
2015-11-10 20:48:44 +00:00
|
|
|
fn sess<'a>(&'a self) -> &'a Session {
|
|
|
|
self.sess
|
|
|
|
}
|
2015-07-31 00:04:06 -07:00
|
|
|
|
2017-01-26 03:21:50 +02:00
|
|
|
fn hir_map<'a>(&'a self) -> Option<&'a hir_map::Map<'hir>> {
|
|
|
|
self.hir_map.as_ref()
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
|
|
|
|
2018-07-12 09:11:39 +02:00
|
|
|
fn pp_ann<'a>(&'a self) -> &'a dyn pprust_hir::PpAnn {
|
2015-11-10 20:48:44 +00:00
|
|
|
self
|
|
|
|
}
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
|
|
|
|
2017-01-26 03:21:50 +02:00
|
|
|
impl<'hir> pprust_hir::PpAnn for IdentifiedAnnotation<'hir> {
|
2016-12-27 10:00:18 +02:00
|
|
|
fn nested(&self, state: &mut pprust_hir::State, nested: pprust_hir::Nested)
|
|
|
|
-> io::Result<()> {
|
2017-01-26 03:21:50 +02:00
|
|
|
if let Some(ref map) = self.hir_map {
|
2016-12-27 10:00:18 +02:00
|
|
|
pprust_hir::PpAnn::nested(map, state, nested)
|
|
|
|
} else {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
2015-11-10 20:48:44 +00:00
|
|
|
fn pre(&self, s: &mut pprust_hir::State, node: pprust_hir::AnnNode) -> io::Result<()> {
|
2015-07-31 00:04:06 -07:00
|
|
|
match node {
|
2018-08-22 22:05:19 +01:00
|
|
|
pprust_hir::AnnNode::Expr(_) => s.popen(),
|
2015-11-10 20:48:44 +00:00
|
|
|
_ => Ok(()),
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
|
|
|
}
|
2015-11-10 20:48:44 +00:00
|
|
|
fn post(&self, s: &mut pprust_hir::State, node: pprust_hir::AnnNode) -> io::Result<()> {
|
2015-07-31 00:04:06 -07:00
|
|
|
match node {
|
2018-08-22 22:05:19 +01:00
|
|
|
pprust_hir::AnnNode::Name(_) => Ok(()),
|
|
|
|
pprust_hir::AnnNode::Item(item) => {
|
2017-06-24 21:22:42 -06:00
|
|
|
s.s.space()?;
|
2017-11-24 13:44:36 +01:00
|
|
|
s.synth_comment(format!("node_id: {} hir local_id: {}",
|
2018-11-07 11:01:18 +01:00
|
|
|
item.id, item.hir_id.local_id.as_u32()))
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
2018-08-22 22:05:19 +01:00
|
|
|
pprust_hir::AnnNode::SubItem(id) => {
|
2017-06-24 21:22:42 -06:00
|
|
|
s.s.space()?;
|
2015-07-31 00:04:06 -07:00
|
|
|
s.synth_comment(id.to_string())
|
|
|
|
}
|
2018-08-22 22:05:19 +01:00
|
|
|
pprust_hir::AnnNode::Block(blk) => {
|
2017-06-24 21:22:42 -06:00
|
|
|
s.s.space()?;
|
2017-11-24 13:44:36 +01:00
|
|
|
s.synth_comment(format!("block node_id: {} hir local_id: {}",
|
2018-11-07 11:01:18 +01:00
|
|
|
blk.id, blk.hir_id.local_id.as_u32()))
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
2018-08-22 22:05:19 +01:00
|
|
|
pprust_hir::AnnNode::Expr(expr) => {
|
2017-06-24 21:22:42 -06:00
|
|
|
s.s.space()?;
|
2017-11-24 13:44:36 +01:00
|
|
|
s.synth_comment(format!("node_id: {} hir local_id: {}",
|
2018-11-07 11:01:18 +01:00
|
|
|
expr.id, expr.hir_id.local_id.as_u32()))?;
|
2015-07-31 00:04:06 -07:00
|
|
|
s.pclose()
|
|
|
|
}
|
2018-08-22 22:05:19 +01:00
|
|
|
pprust_hir::AnnNode::Pat(pat) => {
|
2017-06-24 21:22:42 -06:00
|
|
|
s.s.space()?;
|
2017-11-24 13:44:36 +01:00
|
|
|
s.synth_comment(format!("pat node_id: {} hir local_id: {}",
|
2018-11-07 11:01:18 +01:00
|
|
|
pat.id, pat.hir_id.local_id.as_u32()))
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-26 03:21:50 +02:00
|
|
|
struct HygieneAnnotation<'a> {
|
|
|
|
sess: &'a Session
|
2014-08-11 22:01:37 +10:00
|
|
|
}
|
|
|
|
|
2017-01-26 03:21:50 +02:00
|
|
|
impl<'a> PrinterSupport for HygieneAnnotation<'a> {
|
|
|
|
fn sess(&self) -> &Session {
|
2015-11-10 20:48:44 +00:00
|
|
|
self.sess
|
|
|
|
}
|
2014-08-11 22:01:37 +10:00
|
|
|
|
2018-07-12 09:11:39 +02:00
|
|
|
fn pp_ann(&self) -> &dyn pprust::PpAnn {
|
2015-11-10 20:48:44 +00:00
|
|
|
self
|
|
|
|
}
|
2014-08-11 22:01:37 +10:00
|
|
|
}
|
|
|
|
|
2017-01-26 03:21:50 +02:00
|
|
|
impl<'a> pprust::PpAnn for HygieneAnnotation<'a> {
|
2015-11-10 20:48:44 +00:00
|
|
|
fn post(&self, s: &mut pprust::State, node: pprust::AnnNode) -> io::Result<()> {
|
2014-08-11 22:01:37 +10:00
|
|
|
match node {
|
2018-08-22 22:05:19 +01:00
|
|
|
pprust::AnnNode::Ident(&ast::Ident { name, span }) => {
|
2017-06-24 21:22:42 -06:00
|
|
|
s.s.space()?;
|
2014-08-11 22:01:37 +10:00
|
|
|
// FIXME #16420: this doesn't display the connections
|
|
|
|
// between syntax contexts
|
2018-03-18 02:57:23 +03:00
|
|
|
s.synth_comment(format!("{}{:?}", name.as_u32(), span.ctxt()))
|
2014-08-11 22:01:37 +10:00
|
|
|
}
|
2018-08-22 22:05:19 +01:00
|
|
|
pprust::AnnNode::Name(&name) => {
|
2017-06-24 21:22:42 -06:00
|
|
|
s.s.space()?;
|
2016-11-16 08:21:52 +00:00
|
|
|
s.synth_comment(name.as_u32().to_string())
|
2014-08-11 22:01:37 +10:00
|
|
|
}
|
2015-11-10 20:48:44 +00:00
|
|
|
_ => Ok(()),
|
2014-08-11 22:01:37 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-06-14 04:50:23 +03:00
|
|
|
struct TypedAnnotation<'a, 'tcx: 'a> {
|
2016-05-03 05:23:22 +03:00
|
|
|
tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
2017-01-25 16:24:00 -05:00
|
|
|
tables: Cell<&'a ty::TypeckTables<'tcx>>,
|
2014-08-11 20:59:35 +10:00
|
|
|
}
|
|
|
|
|
2015-07-31 00:04:06 -07:00
|
|
|
impl<'b, 'tcx> HirPrinterSupport<'tcx> for TypedAnnotation<'b, 'tcx> {
|
2015-11-10 20:48:44 +00:00
|
|
|
fn sess<'a>(&'a self) -> &'a Session {
|
|
|
|
&self.tcx.sess
|
|
|
|
}
|
2014-08-11 20:59:35 +10:00
|
|
|
|
2017-01-26 03:21:50 +02:00
|
|
|
fn hir_map<'a>(&'a self) -> Option<&'a hir_map::Map<'tcx>> {
|
2017-01-26 02:41:06 +02:00
|
|
|
Some(&self.tcx.hir)
|
2014-08-11 20:59:35 +10:00
|
|
|
}
|
2014-12-15 21:11:09 -05:00
|
|
|
|
2018-07-12 09:11:39 +02:00
|
|
|
fn pp_ann<'a>(&'a self) -> &'a dyn pprust_hir::PpAnn {
|
2015-11-10 20:48:44 +00:00
|
|
|
self
|
|
|
|
}
|
2016-04-06 13:51:55 +03:00
|
|
|
|
|
|
|
fn node_path(&self, id: ast::NodeId) -> Option<String> {
|
|
|
|
Some(self.tcx.node_path_str(id))
|
|
|
|
}
|
2014-08-11 20:59:35 +10:00
|
|
|
}
|
|
|
|
|
2015-07-31 00:04:06 -07:00
|
|
|
impl<'a, 'tcx> pprust_hir::PpAnn for TypedAnnotation<'a, 'tcx> {
|
2016-12-27 10:00:18 +02:00
|
|
|
fn nested(&self, state: &mut pprust_hir::State, nested: pprust_hir::Nested)
|
|
|
|
-> io::Result<()> {
|
2017-01-06 21:54:24 +02:00
|
|
|
let old_tables = self.tables.get();
|
|
|
|
if let pprust_hir::Nested::Body(id) = nested {
|
|
|
|
self.tables.set(self.tcx.body_tables(id));
|
|
|
|
}
|
2017-01-26 02:41:06 +02:00
|
|
|
pprust_hir::PpAnn::nested(&self.tcx.hir, state, nested)?;
|
2017-01-06 21:54:24 +02:00
|
|
|
self.tables.set(old_tables);
|
|
|
|
Ok(())
|
2016-12-27 10:00:18 +02:00
|
|
|
}
|
2015-11-10 20:48:44 +00:00
|
|
|
fn pre(&self, s: &mut pprust_hir::State, node: pprust_hir::AnnNode) -> io::Result<()> {
|
2014-08-11 20:59:35 +10:00
|
|
|
match node {
|
2018-08-22 22:05:19 +01:00
|
|
|
pprust_hir::AnnNode::Expr(_) => s.popen(),
|
2015-11-10 20:48:44 +00:00
|
|
|
_ => Ok(()),
|
2014-08-11 20:59:35 +10:00
|
|
|
}
|
|
|
|
}
|
2015-11-10 20:48:44 +00:00
|
|
|
fn post(&self, s: &mut pprust_hir::State, node: pprust_hir::AnnNode) -> io::Result<()> {
|
2014-08-11 20:59:35 +10:00
|
|
|
match node {
|
2018-08-22 22:05:19 +01:00
|
|
|
pprust_hir::AnnNode::Expr(expr) => {
|
2017-06-24 21:22:42 -06:00
|
|
|
s.s.space()?;
|
|
|
|
s.s.word("as")?;
|
|
|
|
s.s.space()?;
|
|
|
|
s.s.word(&self.tables.get().expr_ty(expr).to_string())?;
|
2014-08-11 20:59:35 +10:00
|
|
|
s.pclose()
|
|
|
|
}
|
2015-11-10 20:48:44 +00:00
|
|
|
_ => Ok(()),
|
2014-08-11 20:59:35 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn gather_flowgraph_variants(sess: &Session) -> Vec<borrowck_dot::Variant> {
|
2014-12-09 15:25:49 +05:30
|
|
|
let print_loans = sess.opts.debugging_opts.flowgraph_print_loans;
|
|
|
|
let print_moves = sess.opts.debugging_opts.flowgraph_print_moves;
|
|
|
|
let print_assigns = sess.opts.debugging_opts.flowgraph_print_assigns;
|
|
|
|
let print_all = sess.opts.debugging_opts.flowgraph_print_all;
|
2014-08-11 20:59:35 +10:00
|
|
|
let mut variants = Vec::new();
|
2014-12-09 15:25:49 +05:30
|
|
|
if print_all || print_loans {
|
2014-08-11 20:59:35 +10:00
|
|
|
variants.push(borrowck_dot::Loans);
|
|
|
|
}
|
2014-12-09 15:25:49 +05:30
|
|
|
if print_all || print_moves {
|
2014-08-11 20:59:35 +10:00
|
|
|
variants.push(borrowck_dot::Moves);
|
|
|
|
}
|
2014-12-09 15:25:49 +05:30
|
|
|
if print_all || print_assigns {
|
2014-08-11 20:59:35 +10:00
|
|
|
variants.push(borrowck_dot::Assigns);
|
|
|
|
}
|
|
|
|
variants
|
|
|
|
}
|
|
|
|
|
2015-01-28 08:34:18 -05:00
|
|
|
#[derive(Clone, Debug)]
|
2014-08-11 20:59:35 +10:00
|
|
|
pub enum UserIdentifiedItem {
|
|
|
|
ItemViaNode(ast::NodeId),
|
|
|
|
ItemViaPath(Vec<String>),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FromStr for UserIdentifiedItem {
|
2015-01-27 22:52:32 -08:00
|
|
|
type Err = ();
|
|
|
|
fn from_str(s: &str) -> Result<UserIdentifiedItem, ()> {
|
2015-11-10 20:48:44 +00:00
|
|
|
Ok(s.parse()
|
2018-11-07 10:08:41 +01:00
|
|
|
.map(ast::NodeId::from_u32)
|
2015-11-10 20:48:44 +00:00
|
|
|
.map(ItemViaNode)
|
|
|
|
.unwrap_or_else(|_| ItemViaPath(s.split("::").map(|s| s.to_string()).collect())))
|
2014-08-11 20:59:35 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-26 03:21:50 +02:00
|
|
|
enum NodesMatchingUII<'a, 'hir: 'a> {
|
2014-12-14 10:34:23 -08:00
|
|
|
NodesMatchingDirect(option::IntoIter<ast::NodeId>),
|
2017-01-26 03:21:50 +02:00
|
|
|
NodesMatchingSuffix(hir_map::NodesMatchingSuffix<'a, 'hir>),
|
2014-08-11 20:59:35 +10:00
|
|
|
}
|
|
|
|
|
2017-01-26 03:21:50 +02:00
|
|
|
impl<'a, 'hir> Iterator for NodesMatchingUII<'a, 'hir> {
|
2015-01-02 08:15:20 -05:00
|
|
|
type Item = ast::NodeId;
|
|
|
|
|
2014-08-11 20:59:35 +10:00
|
|
|
fn next(&mut self) -> Option<ast::NodeId> {
|
|
|
|
match self {
|
2015-01-07 19:26:00 -05:00
|
|
|
&mut NodesMatchingDirect(ref mut iter) => iter.next(),
|
|
|
|
&mut NodesMatchingSuffix(ref mut iter) => iter.next(),
|
2014-08-11 20:59:35 +10:00
|
|
|
}
|
|
|
|
}
|
2018-03-20 05:33:59 -04:00
|
|
|
|
|
|
|
fn size_hint(&self) -> (usize, Option<usize>) {
|
|
|
|
match self {
|
|
|
|
&NodesMatchingDirect(ref iter) => iter.size_hint(),
|
|
|
|
&NodesMatchingSuffix(ref iter) => iter.size_hint(),
|
|
|
|
}
|
|
|
|
}
|
2014-08-11 20:59:35 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
impl UserIdentifiedItem {
|
|
|
|
fn reconstructed_input(&self) -> String {
|
|
|
|
match *self {
|
|
|
|
ItemViaNode(node_id) => node_id.to_string(),
|
2015-07-10 08:19:21 -04:00
|
|
|
ItemViaPath(ref parts) => parts.join("::"),
|
2014-08-11 20:59:35 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-26 03:21:50 +02:00
|
|
|
fn all_matching_node_ids<'a, 'hir>(&'a self,
|
|
|
|
map: &'a hir_map::Map<'hir>)
|
|
|
|
-> NodesMatchingUII<'a, 'hir> {
|
2014-08-11 20:59:35 +10:00
|
|
|
match *self {
|
2015-11-10 20:48:44 +00:00
|
|
|
ItemViaNode(node_id) => NodesMatchingDirect(Some(node_id).into_iter()),
|
2017-03-24 09:31:26 +01:00
|
|
|
ItemViaPath(ref parts) => NodesMatchingSuffix(map.nodes_matching_suffix(&parts)),
|
2014-08-11 20:59:35 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-31 00:04:06 -07:00
|
|
|
fn to_one_node_id(self, user_option: &str, sess: &Session, map: &hir_map::Map) -> ast::NodeId {
|
2015-02-01 12:44:15 -05:00
|
|
|
let fail_because = |is_wrong_because| -> ast::NodeId {
|
2015-11-10 20:48:44 +00:00
|
|
|
let message = format!("{} needs NodeId (int) or unique path suffix (b::c::d); got \
|
|
|
|
{}, which {}",
|
|
|
|
user_option,
|
|
|
|
self.reconstructed_input(),
|
|
|
|
is_wrong_because);
|
2017-03-24 09:31:26 +01:00
|
|
|
sess.fatal(&message)
|
2014-08-11 20:59:35 +10:00
|
|
|
};
|
|
|
|
|
|
|
|
let mut saw_node = ast::DUMMY_NODE_ID;
|
2015-01-24 14:39:32 +00:00
|
|
|
let mut seen = 0;
|
2014-08-11 20:59:35 +10:00
|
|
|
for node in self.all_matching_node_ids(map) {
|
|
|
|
saw_node = node;
|
|
|
|
seen += 1;
|
|
|
|
if seen > 1 {
|
|
|
|
fail_because("does not resolve uniquely");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if seen == 0 {
|
|
|
|
fail_because("does not resolve to any item");
|
|
|
|
}
|
|
|
|
|
|
|
|
assert!(seen == 1);
|
|
|
|
return saw_node;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-19 16:09:22 +08:00
|
|
|
// Note: Also used by librustdoc, see PR #43348. Consider moving this struct elsewhere.
|
2017-08-15 06:58:47 +08:00
|
|
|
//
|
|
|
|
// FIXME: Currently the `everybody_loops` transformation is not applied to:
|
|
|
|
// * `const fn`, due to issue #43636 that `loop` is not supported for const evaluation. We are
|
|
|
|
// waiting for miri to fix that.
|
|
|
|
// * `impl Trait`, due to issue #43869 that functions returning impl Trait cannot be diverging.
|
|
|
|
// Solving this may require `!` to implement every trait, which relies on the an even more
|
|
|
|
// ambitious form of the closed RFC #1637. See also [#34511].
|
|
|
|
//
|
|
|
|
// [#34511]: https://github.com/rust-lang/rust/issues/34511#issuecomment-322340401
|
2017-11-20 06:22:17 -08:00
|
|
|
pub struct ReplaceBodyWithLoop<'a> {
|
2014-12-17 13:37:26 +01:00
|
|
|
within_static_or_const: bool,
|
2018-08-01 21:12:19 -05:00
|
|
|
nested_blocks: Option<Vec<ast::Block>>,
|
2017-11-20 06:22:17 -08:00
|
|
|
sess: &'a Session,
|
2014-12-17 13:37:26 +01:00
|
|
|
}
|
|
|
|
|
2017-11-20 06:22:17 -08:00
|
|
|
impl<'a> ReplaceBodyWithLoop<'a> {
|
|
|
|
pub fn new(sess: &'a Session) -> ReplaceBodyWithLoop<'a> {
|
2018-08-01 21:12:19 -05:00
|
|
|
ReplaceBodyWithLoop {
|
|
|
|
within_static_or_const: false,
|
|
|
|
nested_blocks: None,
|
|
|
|
sess
|
|
|
|
}
|
2014-12-17 13:37:26 +01:00
|
|
|
}
|
2017-07-19 16:09:22 +08:00
|
|
|
|
|
|
|
fn run<R, F: FnOnce(&mut Self) -> R>(&mut self, is_const: bool, action: F) -> R {
|
|
|
|
let old_const = mem::replace(&mut self.within_static_or_const, is_const);
|
2018-08-02 15:30:57 -05:00
|
|
|
let old_blocks = self.nested_blocks.take();
|
2017-07-19 16:09:22 +08:00
|
|
|
let ret = action(self);
|
|
|
|
self.within_static_or_const = old_const;
|
2018-08-02 15:30:57 -05:00
|
|
|
self.nested_blocks = old_blocks;
|
2017-07-19 16:09:22 +08:00
|
|
|
ret
|
|
|
|
}
|
2017-08-15 06:58:47 +08:00
|
|
|
|
|
|
|
fn should_ignore_fn(ret_ty: &ast::FnDecl) -> bool {
|
|
|
|
if let ast::FunctionRetTy::Ty(ref ty) = ret_ty.output {
|
|
|
|
fn involves_impl_trait(ty: &ast::Ty) -> bool {
|
|
|
|
match ty.node {
|
2018-06-18 16:23:13 +02:00
|
|
|
ast::TyKind::ImplTrait(..) => true,
|
2017-08-15 06:58:47 +08:00
|
|
|
ast::TyKind::Slice(ref subty) |
|
|
|
|
ast::TyKind::Array(ref subty, _) |
|
|
|
|
ast::TyKind::Ptr(ast::MutTy { ty: ref subty, .. }) |
|
|
|
|
ast::TyKind::Rptr(_, ast::MutTy { ty: ref subty, .. }) |
|
|
|
|
ast::TyKind::Paren(ref subty) => involves_impl_trait(subty),
|
2017-09-26 16:44:28 +08:00
|
|
|
ast::TyKind::Tup(ref tys) => any_involves_impl_trait(tys.iter()),
|
|
|
|
ast::TyKind::Path(_, ref path) => path.segments.iter().any(|seg| {
|
2018-05-16 12:57:45 +01:00
|
|
|
match seg.args.as_ref().map(|generic_arg| &**generic_arg) {
|
2017-09-26 16:44:28 +08:00
|
|
|
None => false,
|
2018-05-26 23:54:48 +01:00
|
|
|
Some(&ast::GenericArgs::AngleBracketed(ref data)) => {
|
|
|
|
let types = data.args.iter().filter_map(|arg| match arg {
|
2018-05-27 20:07:09 +01:00
|
|
|
ast::GenericArg::Type(ty) => Some(ty),
|
2018-05-26 23:54:48 +01:00
|
|
|
_ => None,
|
|
|
|
});
|
|
|
|
any_involves_impl_trait(types.into_iter()) ||
|
|
|
|
any_involves_impl_trait(data.bindings.iter().map(|b| &b.ty))
|
|
|
|
},
|
|
|
|
Some(&ast::GenericArgs::Parenthesized(ref data)) => {
|
2017-09-26 16:44:28 +08:00
|
|
|
any_involves_impl_trait(data.inputs.iter()) ||
|
2018-05-26 23:54:48 +01:00
|
|
|
any_involves_impl_trait(data.output.iter())
|
|
|
|
}
|
2017-09-26 16:44:28 +08:00
|
|
|
}
|
|
|
|
}),
|
2017-08-15 06:58:47 +08:00
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
2017-09-26 16:44:28 +08:00
|
|
|
|
|
|
|
fn any_involves_impl_trait<'a, I: Iterator<Item = &'a P<ast::Ty>>>(mut it: I) -> bool {
|
|
|
|
it.any(|subty| involves_impl_trait(subty))
|
|
|
|
}
|
|
|
|
|
2017-08-15 06:58:47 +08:00
|
|
|
involves_impl_trait(ty)
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
2014-12-17 13:37:26 +01:00
|
|
|
}
|
|
|
|
|
2017-11-20 06:22:17 -08:00
|
|
|
impl<'a> fold::Folder for ReplaceBodyWithLoop<'a> {
|
2016-02-09 11:36:51 +01:00
|
|
|
fn fold_item_kind(&mut self, i: ast::ItemKind) -> ast::ItemKind {
|
2017-07-19 16:09:22 +08:00
|
|
|
let is_const = match i {
|
|
|
|
ast::ItemKind::Static(..) | ast::ItemKind::Const(..) => true,
|
2018-05-16 22:55:18 -07:00
|
|
|
ast::ItemKind::Fn(ref decl, ref header, _, _) =>
|
|
|
|
header.constness.node == ast::Constness::Const || Self::should_ignore_fn(decl),
|
2017-07-19 16:09:22 +08:00
|
|
|
_ => false,
|
|
|
|
};
|
|
|
|
self.run(is_const, |s| fold::noop_fold_item_kind(i, s))
|
2014-12-17 13:37:26 +01:00
|
|
|
}
|
|
|
|
|
2018-08-30 11:42:16 +02:00
|
|
|
fn fold_trait_item(&mut self, i: ast::TraitItem) -> SmallVec<[ast::TraitItem; 1]> {
|
2017-07-19 16:09:22 +08:00
|
|
|
let is_const = match i.node {
|
|
|
|
ast::TraitItemKind::Const(..) => true,
|
2018-05-16 22:55:18 -07:00
|
|
|
ast::TraitItemKind::Method(ast::MethodSig { ref decl, ref header, .. }, _) =>
|
|
|
|
header.constness.node == ast::Constness::Const || Self::should_ignore_fn(decl),
|
2017-07-19 16:09:22 +08:00
|
|
|
_ => false,
|
|
|
|
};
|
|
|
|
self.run(is_const, |s| fold::noop_fold_trait_item(i, s))
|
2015-03-15 19:35:25 -06:00
|
|
|
}
|
|
|
|
|
2018-08-30 11:42:16 +02:00
|
|
|
fn fold_impl_item(&mut self, i: ast::ImplItem) -> SmallVec<[ast::ImplItem; 1]> {
|
2017-07-19 16:09:22 +08:00
|
|
|
let is_const = match i.node {
|
|
|
|
ast::ImplItemKind::Const(..) => true,
|
2018-05-16 22:55:18 -07:00
|
|
|
ast::ImplItemKind::Method(ast::MethodSig { ref decl, ref header, .. }, _) =>
|
|
|
|
header.constness.node == ast::Constness::Const || Self::should_ignore_fn(decl),
|
2017-07-19 16:09:22 +08:00
|
|
|
_ => false,
|
|
|
|
};
|
|
|
|
self.run(is_const, |s| fold::noop_fold_impl_item(i, s))
|
2015-03-15 19:35:25 -06:00
|
|
|
}
|
2014-12-17 13:37:26 +01:00
|
|
|
|
2018-08-02 15:30:57 -05:00
|
|
|
fn fold_anon_const(&mut self, c: ast::AnonConst) -> ast::AnonConst {
|
|
|
|
self.run(true, |s| fold::noop_fold_anon_const(c, s))
|
|
|
|
}
|
|
|
|
|
2014-12-17 13:37:26 +01:00
|
|
|
fn fold_block(&mut self, b: P<ast::Block>) -> P<ast::Block> {
|
2018-08-01 21:12:19 -05:00
|
|
|
fn stmt_to_block(rules: ast::BlockCheckMode,
|
2017-12-13 23:05:49 -08:00
|
|
|
recovered: bool,
|
2018-08-01 21:12:19 -05:00
|
|
|
s: Option<ast::Stmt>,
|
|
|
|
sess: &Session) -> ast::Block {
|
|
|
|
ast::Block {
|
|
|
|
stmts: s.into_iter().collect(),
|
2017-08-06 22:54:09 -07:00
|
|
|
rules,
|
2017-11-20 06:22:17 -08:00
|
|
|
id: sess.next_node_id(),
|
2016-06-21 18:08:13 -04:00
|
|
|
span: syntax_pos::DUMMY_SP,
|
2017-12-13 23:05:49 -08:00
|
|
|
recovered,
|
2018-08-01 21:12:19 -05:00
|
|
|
}
|
2014-12-17 13:37:26 +01:00
|
|
|
}
|
|
|
|
|
2018-08-01 21:12:19 -05:00
|
|
|
fn block_to_stmt(b: ast::Block, sess: &Session) -> ast::Stmt {
|
|
|
|
let expr = P(ast::Expr {
|
|
|
|
id: sess.next_node_id(),
|
|
|
|
node: ast::ExprKind::Block(P(b), None),
|
2016-06-21 18:08:13 -04:00
|
|
|
span: syntax_pos::DUMMY_SP,
|
2018-08-01 21:12:19 -05:00
|
|
|
attrs: ThinVec::new(),
|
2014-12-17 13:37:26 +01:00
|
|
|
});
|
|
|
|
|
2018-08-01 21:12:19 -05:00
|
|
|
ast::Stmt {
|
|
|
|
id: sess.next_node_id(),
|
|
|
|
node: ast::StmtKind::Expr(expr),
|
|
|
|
span: syntax_pos::DUMMY_SP,
|
|
|
|
}
|
|
|
|
}
|
2014-12-17 13:37:26 +01:00
|
|
|
|
2018-08-01 21:12:19 -05:00
|
|
|
let empty_block = stmt_to_block(BlockCheckMode::Default, false, None, self.sess);
|
|
|
|
let loop_expr = P(ast::Expr {
|
|
|
|
node: ast::ExprKind::Loop(P(empty_block), None),
|
|
|
|
id: self.sess.next_node_id(),
|
|
|
|
span: syntax_pos::DUMMY_SP,
|
2018-08-05 12:04:56 +02:00
|
|
|
attrs: ThinVec::new(),
|
2018-08-01 21:12:19 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
let loop_stmt = ast::Stmt {
|
|
|
|
id: self.sess.next_node_id(),
|
|
|
|
span: syntax_pos::DUMMY_SP,
|
|
|
|
node: ast::StmtKind::Expr(loop_expr),
|
|
|
|
};
|
|
|
|
|
|
|
|
if self.within_static_or_const {
|
2014-12-17 13:37:26 +01:00
|
|
|
fold::noop_fold_block(b, self)
|
2018-08-01 21:12:19 -05:00
|
|
|
} else {
|
|
|
|
b.map(|b| {
|
2018-08-02 16:24:20 -05:00
|
|
|
let mut stmts = vec![];
|
|
|
|
for s in b.stmts {
|
|
|
|
let old_blocks = self.nested_blocks.replace(vec![]);
|
2018-08-01 21:12:19 -05:00
|
|
|
|
2018-08-02 16:24:20 -05:00
|
|
|
stmts.extend(self.fold_stmt(s).into_iter().filter(|s| s.is_item()));
|
2018-08-01 21:12:19 -05:00
|
|
|
|
2018-08-02 16:24:20 -05:00
|
|
|
// we put a Some in there earlier with that replace(), so this is valid
|
|
|
|
let new_blocks = self.nested_blocks.take().unwrap();
|
|
|
|
self.nested_blocks = old_blocks;
|
|
|
|
stmts.extend(new_blocks.into_iter().map(|b| block_to_stmt(b, &self.sess)));
|
|
|
|
}
|
2018-08-01 21:12:19 -05:00
|
|
|
|
|
|
|
let mut new_block = ast::Block {
|
|
|
|
stmts,
|
|
|
|
..b
|
|
|
|
};
|
|
|
|
|
|
|
|
if let Some(old_blocks) = self.nested_blocks.as_mut() {
|
|
|
|
//push our fresh block onto the cache and yield an empty block with `loop {}`
|
2018-08-02 15:30:57 -05:00
|
|
|
if !new_block.stmts.is_empty() {
|
|
|
|
old_blocks.push(new_block);
|
|
|
|
}
|
2018-08-01 21:12:19 -05:00
|
|
|
|
|
|
|
stmt_to_block(b.rules, b.recovered, Some(loop_stmt), self.sess)
|
|
|
|
} else {
|
|
|
|
//push `loop {}` onto the end of our fresh block and yield that
|
|
|
|
new_block.stmts.push(loop_stmt);
|
|
|
|
|
|
|
|
new_block
|
|
|
|
}
|
|
|
|
})
|
2014-12-17 13:37:26 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// in general the pretty printer processes unexpanded code, so
|
|
|
|
// we override the default `fold_mac` method which panics.
|
2015-01-02 13:39:05 -08:00
|
|
|
fn fold_mac(&mut self, mac: ast::Mac) -> ast::Mac {
|
|
|
|
fold::noop_fold_mac(mac, self)
|
2014-12-17 13:37:26 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-03 04:56:42 +03:00
|
|
|
fn print_flowgraph<'a, 'tcx, W: Write>(variants: Vec<borrowck_dot::Variant>,
|
2016-05-03 05:23:22 +03:00
|
|
|
tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
2016-10-29 15:03:21 +02:00
|
|
|
code: blocks::Code<'tcx>,
|
2016-05-03 04:56:42 +03:00
|
|
|
mode: PpFlowGraphMode,
|
|
|
|
mut out: W)
|
|
|
|
-> io::Result<()> {
|
2017-02-18 06:52:16 -05:00
|
|
|
let body_id = match code {
|
|
|
|
blocks::Code::Expr(expr) => {
|
|
|
|
// Find the function this expression is from.
|
|
|
|
let mut node_id = expr.id;
|
|
|
|
loop {
|
|
|
|
let node = tcx.hir.get(node_id);
|
|
|
|
if let Some(n) = hir::map::blocks::FnLikeNode::from_node(node) {
|
|
|
|
break n.body();
|
|
|
|
}
|
|
|
|
let parent = tcx.hir.get_parent_node(node_id);
|
2018-10-12 14:01:25 +02:00
|
|
|
assert_ne!(node_id, parent);
|
2017-02-18 06:52:16 -05:00
|
|
|
node_id = parent;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
blocks::Code::FnLike(fn_like) => fn_like.body(),
|
2016-04-21 10:29:49 +12:00
|
|
|
};
|
2017-02-18 06:52:16 -05:00
|
|
|
let body = tcx.hir.body(body_id);
|
|
|
|
let cfg = cfg::CFG::new(tcx, &body);
|
2016-04-21 10:29:49 +12:00
|
|
|
let labelled_edges = mode != PpFlowGraphMode::UnlabelledEdges;
|
|
|
|
let lcfg = LabelledCFG {
|
2017-08-29 19:27:30 +03:00
|
|
|
tcx,
|
2016-04-21 10:29:49 +12:00
|
|
|
cfg: &cfg,
|
|
|
|
name: format!("node_{}", code.id()),
|
2017-08-06 22:54:09 -07:00
|
|
|
labelled_edges,
|
2014-12-17 13:37:26 +01:00
|
|
|
};
|
|
|
|
|
2016-04-21 10:29:49 +12:00
|
|
|
match code {
|
|
|
|
_ if variants.is_empty() => {
|
|
|
|
let r = dot::render(&lcfg, &mut out);
|
|
|
|
return expand_err_details(r);
|
|
|
|
}
|
2016-10-26 02:27:14 +03:00
|
|
|
blocks::Code::Expr(_) => {
|
2016-04-21 10:29:49 +12:00
|
|
|
tcx.sess.err("--pretty flowgraph with -Z flowgraph-print annotations requires \
|
|
|
|
fn-like node id.");
|
|
|
|
return Ok(());
|
|
|
|
}
|
2016-10-26 02:27:14 +03:00
|
|
|
blocks::Code::FnLike(fn_like) => {
|
2016-04-21 10:29:49 +12:00
|
|
|
let (bccx, analysis_data) =
|
2017-01-06 21:54:24 +02:00
|
|
|
borrowck::build_borrowck_dataflow_data_for_fn(tcx, fn_like.body(), &cfg);
|
2014-08-11 20:59:35 +10:00
|
|
|
|
2016-04-21 10:29:49 +12:00
|
|
|
let lcfg = borrowck_dot::DataflowLabeller {
|
|
|
|
inner: lcfg,
|
2017-08-06 22:54:09 -07:00
|
|
|
variants,
|
2016-04-21 10:29:49 +12:00
|
|
|
borrowck_ctxt: &bccx,
|
|
|
|
analysis_data: &analysis_data,
|
|
|
|
};
|
|
|
|
let r = dot::render(&lcfg, &mut out);
|
|
|
|
return expand_err_details(r);
|
2014-09-07 20:09:06 +03:00
|
|
|
}
|
2016-04-21 10:29:49 +12:00
|
|
|
}
|
2014-09-07 20:09:06 +03:00
|
|
|
|
2016-04-21 10:29:49 +12:00
|
|
|
fn expand_err_details(r: io::Result<()>) -> io::Result<()> {
|
|
|
|
r.map_err(|ioerr| {
|
|
|
|
io::Error::new(io::ErrorKind::Other,
|
2017-03-24 09:31:26 +01:00
|
|
|
format!("graphviz::render failed: {}", ioerr))
|
2016-04-21 10:29:49 +12:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-20 06:22:17 -08:00
|
|
|
pub fn fold_crate(sess: &Session, krate: ast::Crate, ppm: PpMode) -> ast::Crate {
|
2016-04-21 10:29:49 +12:00
|
|
|
if let PpmSource(PpmEveryBodyLoops) = ppm {
|
2017-11-20 06:22:17 -08:00
|
|
|
let mut fold = ReplaceBodyWithLoop::new(sess);
|
2016-04-21 10:29:49 +12:00
|
|
|
fold.fold_crate(krate)
|
2014-08-11 20:59:35 +10:00
|
|
|
} else {
|
2016-04-21 10:29:49 +12:00
|
|
|
krate
|
|
|
|
}
|
|
|
|
}
|
2014-08-11 20:59:35 +10:00
|
|
|
|
2017-12-14 08:09:19 +01:00
|
|
|
fn get_source(input: &Input, sess: &Session) -> (Vec<u8>, FileName) {
|
2014-08-11 20:59:35 +10:00
|
|
|
let src_name = driver::source_name(input);
|
2018-08-18 12:14:09 +02:00
|
|
|
let src = sess.source_map()
|
2018-08-18 12:13:56 +02:00
|
|
|
.get_source_file(&src_name)
|
2016-10-20 07:08:43 +05:30
|
|
|
.unwrap()
|
|
|
|
.src
|
|
|
|
.as_ref()
|
|
|
|
.unwrap()
|
|
|
|
.as_bytes()
|
|
|
|
.to_vec();
|
2016-04-21 10:29:49 +12:00
|
|
|
(src, src_name)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn write_output(out: Vec<u8>, ofile: Option<&Path>) {
|
|
|
|
match ofile {
|
|
|
|
None => print!("{}", String::from_utf8(out).unwrap()),
|
|
|
|
Some(p) => {
|
|
|
|
match File::create(p) {
|
|
|
|
Ok(mut w) => w.write_all(&out).unwrap(),
|
|
|
|
Err(e) => panic!("print-print failed to open {} due to {}", p.display(), e),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn print_after_parsing(sess: &Session,
|
|
|
|
input: &Input,
|
|
|
|
krate: &ast::Crate,
|
|
|
|
ppm: PpMode,
|
|
|
|
ofile: Option<&Path>) {
|
|
|
|
let (src, src_name) = get_source(input, sess);
|
|
|
|
|
2016-04-18 10:30:55 +12:00
|
|
|
let mut rdr = &*src;
|
2016-04-21 10:29:49 +12:00
|
|
|
let mut out = Vec::new();
|
|
|
|
|
|
|
|
if let PpmSource(s) = ppm {
|
|
|
|
// Silently ignores an identified node.
|
2018-07-12 09:11:39 +02:00
|
|
|
let out: &mut dyn Write = &mut out;
|
2017-06-25 09:54:09 -06:00
|
|
|
s.call_with_pp_support(sess, None, move |annotation| {
|
2018-10-12 14:36:10 +02:00
|
|
|
debug!("pretty printing source code {:?}", s);
|
|
|
|
let sess = annotation.sess();
|
|
|
|
pprust::print_crate(sess.source_map(),
|
|
|
|
&sess.parse_sess,
|
|
|
|
krate,
|
|
|
|
src_name,
|
|
|
|
&mut rdr,
|
|
|
|
box out,
|
|
|
|
annotation.pp_ann(),
|
|
|
|
false)
|
|
|
|
}).unwrap()
|
2016-04-21 10:29:49 +12:00
|
|
|
} else {
|
|
|
|
unreachable!();
|
|
|
|
};
|
|
|
|
|
|
|
|
write_output(out, ofile);
|
|
|
|
}
|
|
|
|
|
2016-05-10 19:21:44 -07:00
|
|
|
pub fn print_after_hir_lowering<'tcx, 'a: 'tcx>(sess: &'a Session,
|
2018-07-31 15:23:31 -06:00
|
|
|
cstore: &'tcx CStore,
|
2017-01-26 03:21:50 +02:00
|
|
|
hir_map: &hir_map::Map<'tcx>,
|
2016-10-04 02:19:40 +03:00
|
|
|
analysis: &ty::CrateAnalysis,
|
2016-05-10 19:21:44 -07:00
|
|
|
resolutions: &Resolutions,
|
|
|
|
input: &Input,
|
|
|
|
krate: &ast::Crate,
|
|
|
|
crate_name: &str,
|
|
|
|
ppm: PpMode,
|
2017-12-03 13:57:25 +01:00
|
|
|
arenas: &'tcx AllArenas<'tcx>,
|
2017-09-13 20:26:39 -07:00
|
|
|
output_filenames: &OutputFilenames,
|
2016-05-10 19:21:44 -07:00
|
|
|
opt_uii: Option<UserIdentifiedItem>,
|
|
|
|
ofile: Option<&Path>) {
|
2016-04-21 10:29:49 +12:00
|
|
|
if ppm.needs_analysis() {
|
2016-10-20 07:08:43 +05:30
|
|
|
print_with_analysis(sess,
|
2017-09-05 16:48:24 +02:00
|
|
|
cstore,
|
2017-01-26 03:21:50 +02:00
|
|
|
hir_map,
|
2016-10-20 07:08:43 +05:30
|
|
|
analysis,
|
|
|
|
resolutions,
|
|
|
|
crate_name,
|
|
|
|
arenas,
|
2017-09-13 20:26:39 -07:00
|
|
|
output_filenames,
|
2016-10-20 07:08:43 +05:30
|
|
|
ppm,
|
|
|
|
opt_uii,
|
|
|
|
ofile);
|
2016-04-21 10:29:49 +12:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let (src, src_name) = get_source(input, sess);
|
|
|
|
|
|
|
|
let mut rdr = &src[..];
|
2015-02-26 21:00:43 -08:00
|
|
|
let mut out = Vec::new();
|
2014-08-11 20:59:35 +10:00
|
|
|
|
|
|
|
match (ppm, opt_uii) {
|
2016-10-20 07:08:43 +05:30
|
|
|
(PpmSource(s), _) => {
|
|
|
|
// Silently ignores an identified node.
|
2018-07-12 09:11:39 +02:00
|
|
|
let out: &mut dyn Write = &mut out;
|
2017-06-25 09:54:09 -06:00
|
|
|
s.call_with_pp_support(sess, Some(hir_map), move |annotation| {
|
2016-10-20 07:08:43 +05:30
|
|
|
debug!("pretty printing source code {:?}", s);
|
|
|
|
let sess = annotation.sess();
|
2018-08-18 12:14:09 +02:00
|
|
|
pprust::print_crate(sess.source_map(),
|
2017-01-17 01:14:53 +00:00
|
|
|
&sess.parse_sess,
|
2016-10-20 07:08:43 +05:30
|
|
|
krate,
|
2017-12-14 08:09:19 +01:00
|
|
|
src_name,
|
2016-10-20 07:08:43 +05:30
|
|
|
&mut rdr,
|
2017-06-25 09:54:09 -06:00
|
|
|
box out,
|
2016-10-20 07:08:43 +05:30
|
|
|
annotation.pp_ann(),
|
|
|
|
true)
|
|
|
|
})
|
|
|
|
}
|
2014-08-11 20:59:35 +10:00
|
|
|
|
2016-10-20 07:08:43 +05:30
|
|
|
(PpmHir(s), None) => {
|
2018-07-12 09:11:39 +02:00
|
|
|
let out: &mut dyn Write = &mut out;
|
2016-10-20 07:08:43 +05:30
|
|
|
s.call_with_pp_support_hir(sess,
|
2017-09-05 16:48:24 +02:00
|
|
|
cstore,
|
2017-01-26 03:21:50 +02:00
|
|
|
hir_map,
|
2016-10-20 07:08:43 +05:30
|
|
|
analysis,
|
|
|
|
resolutions,
|
|
|
|
arenas,
|
2017-09-13 20:26:39 -07:00
|
|
|
output_filenames,
|
2016-10-20 07:08:43 +05:30
|
|
|
crate_name,
|
2017-06-25 09:54:09 -06:00
|
|
|
move |annotation, krate| {
|
2016-10-20 07:08:43 +05:30
|
|
|
debug!("pretty printing source code {:?}", s);
|
|
|
|
let sess = annotation.sess();
|
2018-08-18 12:14:09 +02:00
|
|
|
pprust_hir::print_crate(sess.source_map(),
|
2017-01-17 01:14:53 +00:00
|
|
|
&sess.parse_sess,
|
2016-10-20 07:08:43 +05:30
|
|
|
krate,
|
2017-12-14 08:09:19 +01:00
|
|
|
src_name,
|
2016-10-20 07:08:43 +05:30
|
|
|
&mut rdr,
|
2017-06-25 09:54:09 -06:00
|
|
|
box out,
|
2016-10-20 07:08:43 +05:30
|
|
|
annotation.pp_ann(),
|
|
|
|
true)
|
|
|
|
})
|
|
|
|
}
|
2015-07-31 00:04:06 -07:00
|
|
|
|
2017-11-02 08:58:13 -04:00
|
|
|
(PpmHirTree(s), None) => {
|
2018-07-12 09:11:39 +02:00
|
|
|
let out: &mut dyn Write = &mut out;
|
2017-11-02 08:58:13 -04:00
|
|
|
s.call_with_pp_support_hir(sess,
|
|
|
|
cstore,
|
|
|
|
hir_map,
|
|
|
|
analysis,
|
|
|
|
resolutions,
|
|
|
|
arenas,
|
|
|
|
output_filenames,
|
|
|
|
crate_name,
|
|
|
|
move |_annotation, krate| {
|
|
|
|
debug!("pretty printing source code {:?}", s);
|
|
|
|
write!(out, "{:#?}", krate)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-10-20 07:08:43 +05:30
|
|
|
(PpmHir(s), Some(uii)) => {
|
2018-07-12 09:11:39 +02:00
|
|
|
let out: &mut dyn Write = &mut out;
|
2016-10-20 07:08:43 +05:30
|
|
|
s.call_with_pp_support_hir(sess,
|
2017-09-05 16:48:24 +02:00
|
|
|
cstore,
|
2017-01-26 03:21:50 +02:00
|
|
|
hir_map,
|
2016-10-20 07:08:43 +05:30
|
|
|
analysis,
|
|
|
|
resolutions,
|
|
|
|
arenas,
|
2017-09-13 20:26:39 -07:00
|
|
|
output_filenames,
|
2016-10-20 07:08:43 +05:30
|
|
|
crate_name,
|
2017-06-25 09:54:09 -06:00
|
|
|
move |annotation, _| {
|
2016-10-20 07:08:43 +05:30
|
|
|
debug!("pretty printing source code {:?}", s);
|
|
|
|
let sess = annotation.sess();
|
2018-01-14 21:11:40 -06:00
|
|
|
let hir_map = annotation.hir_map().expect("-Z unpretty missing HIR map");
|
2018-08-18 12:14:09 +02:00
|
|
|
let mut pp_state = pprust_hir::State::new_from_input(sess.source_map(),
|
2017-01-17 01:14:53 +00:00
|
|
|
&sess.parse_sess,
|
2017-12-14 08:09:19 +01:00
|
|
|
src_name,
|
2016-10-20 07:08:43 +05:30
|
|
|
&mut rdr,
|
|
|
|
box out,
|
|
|
|
annotation.pp_ann(),
|
2016-12-27 10:00:18 +02:00
|
|
|
true);
|
2017-01-26 03:21:50 +02:00
|
|
|
for node_id in uii.all_matching_node_ids(hir_map) {
|
|
|
|
let node = hir_map.get(node_id);
|
2016-12-27 10:00:18 +02:00
|
|
|
pp_state.print_node(node)?;
|
2017-06-24 21:22:42 -06:00
|
|
|
pp_state.s.space()?;
|
2016-10-20 07:08:43 +05:30
|
|
|
let path = annotation.node_path(node_id)
|
2018-01-14 21:11:40 -06:00
|
|
|
.expect("-Z unpretty missing node paths");
|
2016-10-20 07:08:43 +05:30
|
|
|
pp_state.synth_comment(path)?;
|
2017-06-24 21:22:42 -06:00
|
|
|
pp_state.s.hardbreak()?;
|
2016-10-20 07:08:43 +05:30
|
|
|
}
|
2017-06-24 21:22:42 -06:00
|
|
|
pp_state.s.eof()
|
2016-10-20 07:08:43 +05:30
|
|
|
})
|
|
|
|
}
|
2017-11-02 08:58:13 -04:00
|
|
|
|
|
|
|
(PpmHirTree(s), Some(uii)) => {
|
2018-07-12 09:11:39 +02:00
|
|
|
let out: &mut dyn Write = &mut out;
|
2017-11-02 08:58:13 -04:00
|
|
|
s.call_with_pp_support_hir(sess,
|
|
|
|
cstore,
|
|
|
|
hir_map,
|
|
|
|
analysis,
|
|
|
|
resolutions,
|
|
|
|
arenas,
|
|
|
|
output_filenames,
|
|
|
|
crate_name,
|
|
|
|
move |_annotation, _krate| {
|
|
|
|
debug!("pretty printing source code {:?}", s);
|
|
|
|
for node_id in uii.all_matching_node_ids(hir_map) {
|
|
|
|
let node = hir_map.get(node_id);
|
|
|
|
write!(out, "{:#?}", node)?;
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-10-20 07:08:43 +05:30
|
|
|
_ => unreachable!(),
|
|
|
|
}
|
|
|
|
.unwrap();
|
2016-04-21 10:29:49 +12:00
|
|
|
|
|
|
|
write_output(out, ofile);
|
|
|
|
}
|
|
|
|
|
|
|
|
// In an ideal world, this would be a public function called by the driver after
|
|
|
|
// analsysis is performed. However, we want to call `phase_3_run_analysis_passes`
|
|
|
|
// with a different callback than the standard driver, so that isn't easy.
|
|
|
|
// Instead, we call that function ourselves.
|
|
|
|
fn print_with_analysis<'tcx, 'a: 'tcx>(sess: &'a Session,
|
2018-07-31 15:23:31 -06:00
|
|
|
cstore: &'a CStore,
|
2017-01-26 03:21:50 +02:00
|
|
|
hir_map: &hir_map::Map<'tcx>,
|
2016-10-04 02:19:40 +03:00
|
|
|
analysis: &ty::CrateAnalysis,
|
2016-05-05 08:54:18 +00:00
|
|
|
resolutions: &Resolutions,
|
2016-04-21 10:29:49 +12:00
|
|
|
crate_name: &str,
|
2017-12-03 13:57:25 +01:00
|
|
|
arenas: &'tcx AllArenas<'tcx>,
|
2017-09-13 20:26:39 -07:00
|
|
|
output_filenames: &OutputFilenames,
|
2016-04-21 10:29:49 +12:00
|
|
|
ppm: PpMode,
|
|
|
|
uii: Option<UserIdentifiedItem>,
|
|
|
|
ofile: Option<&Path>) {
|
|
|
|
let nodeid = if let Some(uii) = uii {
|
|
|
|
debug!("pretty printing for {:?}", uii);
|
2018-01-14 21:11:40 -06:00
|
|
|
Some(uii.to_one_node_id("-Z unpretty", sess, &hir_map))
|
2016-04-21 10:29:49 +12:00
|
|
|
} else {
|
|
|
|
debug!("pretty printing for whole crate");
|
|
|
|
None
|
|
|
|
};
|
2014-08-11 20:59:35 +10:00
|
|
|
|
2016-04-21 10:29:49 +12:00
|
|
|
let mut out = Vec::new();
|
|
|
|
|
2017-11-12 18:20:38 +02:00
|
|
|
let control = &driver::CompileController::basic();
|
2018-05-08 16:10:16 +03:00
|
|
|
let codegen_backend = ::get_codegen_backend(sess);
|
|
|
|
abort_on_err(driver::phase_3_run_analysis_passes(&*codegen_backend,
|
2017-10-30 18:42:21 +01:00
|
|
|
control,
|
2017-11-12 18:20:38 +02:00
|
|
|
sess,
|
2017-09-05 16:48:24 +02:00
|
|
|
cstore,
|
2017-01-26 03:21:50 +02:00
|
|
|
hir_map.clone(),
|
2016-05-05 08:54:18 +00:00
|
|
|
analysis.clone(),
|
|
|
|
resolutions.clone(),
|
2016-04-21 10:29:49 +12:00
|
|
|
arenas,
|
|
|
|
crate_name,
|
2017-09-13 20:26:39 -07:00
|
|
|
output_filenames,
|
2017-09-18 18:03:09 +02:00
|
|
|
|tcx, _, _, _| {
|
2016-04-21 10:29:49 +12:00
|
|
|
match ppm {
|
|
|
|
PpmMir | PpmMirCFG => {
|
2016-10-28 13:55:49 +03:00
|
|
|
if let Some(nodeid) = nodeid {
|
2017-01-26 02:41:06 +02:00
|
|
|
let def_id = tcx.hir.local_def_id(nodeid);
|
2016-10-28 13:55:49 +03:00
|
|
|
match ppm {
|
2017-04-25 15:56:02 -04:00
|
|
|
PpmMir => write_mir_pretty(tcx, Some(def_id), &mut out),
|
|
|
|
PpmMirCFG => write_mir_graphviz(tcx, Some(def_id), &mut out),
|
2016-10-28 13:55:49 +03:00
|
|
|
_ => unreachable!(),
|
|
|
|
}?;
|
|
|
|
} else {
|
|
|
|
match ppm {
|
2017-04-25 15:56:02 -04:00
|
|
|
PpmMir => write_mir_pretty(tcx, None, &mut out),
|
|
|
|
PpmMirCFG => write_mir_graphviz(tcx, None, &mut out),
|
2016-10-28 13:55:49 +03:00
|
|
|
_ => unreachable!(),
|
|
|
|
}?;
|
2016-02-14 19:23:05 +01:00
|
|
|
}
|
|
|
|
Ok(())
|
2016-04-21 10:29:49 +12:00
|
|
|
}
|
|
|
|
PpmFlowGraph(mode) => {
|
2016-10-20 07:08:43 +05:30
|
|
|
let nodeid =
|
|
|
|
nodeid.expect("`pretty flowgraph=..` needs NodeId (int) or unique path \
|
|
|
|
suffix (b::c::d)");
|
2017-01-26 02:41:06 +02:00
|
|
|
let node = tcx.hir.find(nodeid).unwrap_or_else(|| {
|
2016-04-21 10:29:49 +12:00
|
|
|
tcx.sess.fatal(&format!("--pretty flowgraph couldn't find id: {}", nodeid))
|
|
|
|
});
|
2014-08-11 20:59:35 +10:00
|
|
|
|
2017-01-26 02:41:06 +02:00
|
|
|
match blocks::Code::from_node(&tcx.hir, nodeid) {
|
2016-04-21 10:29:49 +12:00
|
|
|
Some(code) => {
|
|
|
|
let variants = gather_flowgraph_variants(tcx.sess);
|
2014-08-11 20:59:35 +10:00
|
|
|
|
2018-07-12 09:11:39 +02:00
|
|
|
let out: &mut dyn Write = &mut out;
|
2014-08-11 20:59:35 +10:00
|
|
|
|
2016-10-28 13:55:49 +03:00
|
|
|
print_flowgraph(variants, tcx, code, mode, out)
|
2016-04-21 10:29:49 +12:00
|
|
|
}
|
|
|
|
None => {
|
2016-10-20 07:08:43 +05:30
|
|
|
let message = format!("--pretty=flowgraph needs block, fn, or method; \
|
|
|
|
got {:?}",
|
2016-04-21 10:29:49 +12:00
|
|
|
node);
|
|
|
|
|
2017-01-26 02:41:06 +02:00
|
|
|
tcx.sess.span_fatal(tcx.hir.span(nodeid), &message)
|
2014-08-11 20:59:35 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-04-21 10:29:49 +12:00
|
|
|
_ => unreachable!(),
|
2014-08-11 20:59:35 +10:00
|
|
|
}
|
2016-10-20 07:08:43 +05:30
|
|
|
}),
|
|
|
|
sess)
|
|
|
|
.unwrap();
|
2014-08-11 20:59:35 +10:00
|
|
|
|
2016-04-21 10:29:49 +12:00
|
|
|
write_output(out, ofile);
|
2014-08-11 20:59:35 +10:00
|
|
|
}
|