1use std::cell::Cell;
4use std::fmt::Write;
5
6use rustc_ast_pretty::pprust as pprust_ast;
7use rustc_middle::bug;
8use rustc_middle::mir::{write_mir_graphviz, write_mir_pretty};
9use rustc_middle::ty::{self, TyCtxt};
10use rustc_mir_build::thir::print::{thir_flat, thir_tree};
11use rustc_session::Session;
12use rustc_session::config::{OutFileName, PpHirMode, PpMode, PpSourceMode};
13use rustc_smir::rustc_internal::pretty::write_smir_pretty;
14use rustc_span::{FileName, Ident};
15use tracing::debug;
16use {rustc_ast as ast, rustc_hir_pretty as pprust_hir};
17
18pub use self::PpMode::*;
19pub use self::PpSourceMode::*;
20
21struct AstNoAnn;
22
23impl pprust_ast::PpAnn for AstNoAnn {}
24
25struct AstIdentifiedAnn;
26
27impl pprust_ast::PpAnn for AstIdentifiedAnn {
28 fn pre(&self, s: &mut pprust_ast::State<'_>, node: pprust_ast::AnnNode<'_>) {
29 if let pprust_ast::AnnNode::Expr(_) = node {
30 s.popen();
31 }
32 }
33
34 fn post(&self, s: &mut pprust_ast::State<'_>, node: pprust_ast::AnnNode<'_>) {
35 match node {
36 pprust_ast::AnnNode::Crate(_)
37 | pprust_ast::AnnNode::Ident(_)
38 | pprust_ast::AnnNode::Name(_) => {}
39
40 pprust_ast::AnnNode::Item(item) => {
41 s.s.space();
42 s.synth_comment(item.id.to_string())
43 }
44 pprust_ast::AnnNode::SubItem(id) => {
45 s.s.space();
46 s.synth_comment(id.to_string())
47 }
48 pprust_ast::AnnNode::Block(blk) => {
49 s.s.space();
50 s.synth_comment(format!("block {}", blk.id))
51 }
52 pprust_ast::AnnNode::Expr(expr) => {
53 s.s.space();
54 s.synth_comment(expr.id.to_string());
55 s.pclose()
56 }
57 pprust_ast::AnnNode::Pat(pat) => {
58 s.s.space();
59 s.synth_comment(format!("pat {}", pat.id));
60 }
61 }
62 }
63}
64
65struct HirIdentifiedAnn<'tcx> {
66 tcx: TyCtxt<'tcx>,
67}
68
69impl<'tcx> pprust_hir::PpAnn for HirIdentifiedAnn<'tcx> {
70 fn nested(&self, state: &mut pprust_hir::State<'_>, nested: pprust_hir::Nested) {
71 self.tcx.nested(state, nested)
72 }
73
74 fn pre(&self, s: &mut pprust_hir::State<'_>, node: pprust_hir::AnnNode<'_>) {
75 if let pprust_hir::AnnNode::Expr(_) = node {
76 s.popen();
77 }
78 }
79
80 fn post(&self, s: &mut pprust_hir::State<'_>, node: pprust_hir::AnnNode<'_>) {
81 match node {
82 pprust_hir::AnnNode::Name(_) => {}
83 pprust_hir::AnnNode::Item(item) => {
84 s.s.space();
85 s.synth_comment(format!("hir_id: {}", item.hir_id()));
86 }
87 pprust_hir::AnnNode::SubItem(id) => {
88 s.s.space();
89 s.synth_comment(id.to_string());
90 }
91 pprust_hir::AnnNode::Block(blk) => {
92 s.s.space();
93 s.synth_comment(format!("block hir_id: {}", blk.hir_id));
94 }
95 pprust_hir::AnnNode::Expr(expr) => {
96 s.s.space();
97 s.synth_comment(format!("expr hir_id: {}", expr.hir_id));
98 s.pclose();
99 }
100 pprust_hir::AnnNode::Pat(pat) => {
101 s.s.space();
102 s.synth_comment(format!("pat hir_id: {}", pat.hir_id));
103 }
104 pprust_hir::AnnNode::TyPat(pat) => {
105 s.s.space();
106 s.synth_comment(format!("ty pat hir_id: {}", pat.hir_id));
107 }
108 pprust_hir::AnnNode::Arm(arm) => {
109 s.s.space();
110 s.synth_comment(format!("arm hir_id: {}", arm.hir_id));
111 }
112 }
113 }
114}
115
116struct AstHygieneAnn<'a> {
117 sess: &'a Session,
118}
119
120impl<'a> pprust_ast::PpAnn for AstHygieneAnn<'a> {
121 fn post(&self, s: &mut pprust_ast::State<'_>, node: pprust_ast::AnnNode<'_>) {
122 match node {
123 pprust_ast::AnnNode::Ident(&Ident { name, span }) => {
124 s.s.space();
125 s.synth_comment(format!("{}{:?}", name.as_u32(), span.ctxt()))
126 }
127 pprust_ast::AnnNode::Name(&name) => {
128 s.s.space();
129 s.synth_comment(name.as_u32().to_string())
130 }
131 pprust_ast::AnnNode::Crate(_) => {
132 s.s.hardbreak();
133 let verbose = self.sess.verbose_internals();
134 s.synth_comment(rustc_span::hygiene::debug_hygiene_data(verbose));
135 s.s.hardbreak_if_not_bol();
136 }
137 _ => {}
138 }
139 }
140}
141
142struct HirTypedAnn<'tcx> {
143 tcx: TyCtxt<'tcx>,
144 maybe_typeck_results: Cell<Option<&'tcx ty::TypeckResults<'tcx>>>,
145}
146
147impl<'tcx> pprust_hir::PpAnn for HirTypedAnn<'tcx> {
148 fn nested(&self, state: &mut pprust_hir::State<'_>, nested: pprust_hir::Nested) {
149 let old_maybe_typeck_results = self.maybe_typeck_results.get();
150 if let pprust_hir::Nested::Body(id) = nested {
151 self.maybe_typeck_results.set(Some(self.tcx.typeck_body(id)));
152 }
153 self.tcx.nested(state, nested);
154 self.maybe_typeck_results.set(old_maybe_typeck_results);
155 }
156
157 fn pre(&self, s: &mut pprust_hir::State<'_>, node: pprust_hir::AnnNode<'_>) {
158 if let pprust_hir::AnnNode::Expr(_) = node {
159 s.popen();
160 }
161 }
162
163 fn post(&self, s: &mut pprust_hir::State<'_>, node: pprust_hir::AnnNode<'_>) {
164 if let pprust_hir::AnnNode::Expr(expr) = node {
165 let typeck_results = self.maybe_typeck_results.get().or_else(|| {
166 self.tcx
167 .hir()
168 .maybe_body_owned_by(expr.hir_id.owner.def_id)
169 .map(|body_id| self.tcx.typeck_body(body_id.id()))
170 });
171
172 if let Some(typeck_results) = typeck_results {
173 s.s.space();
174 s.s.word("as");
175 s.s.space();
176 s.s.word(typeck_results.expr_ty(expr).to_string());
177 }
178
179 s.pclose();
180 }
181 }
182}
183
184fn get_source(sess: &Session) -> (String, FileName) {
185 let src_name = sess.io.input.source_name();
186 let src = String::clone(
187 sess.source_map()
188 .get_source_file(&src_name)
189 .expect("get_source_file")
190 .src
191 .as_ref()
192 .expect("src"),
193 );
194 (src, src_name)
195}
196
197fn write_or_print(out: &str, sess: &Session) {
198 sess.io.output_file.as_ref().unwrap_or(&OutFileName::Stdout).overwrite(out, sess);
199}
200
201pub enum PrintExtra<'tcx> {
204 AfterParsing { krate: &'tcx ast::Crate },
205 NeedsAstMap { tcx: TyCtxt<'tcx> },
206}
207
208impl<'tcx> PrintExtra<'tcx> {
209 fn with_krate<F, R>(&self, f: F) -> R
210 where
211 F: FnOnce(&ast::Crate) -> R,
212 {
213 match self {
214 PrintExtra::AfterParsing { krate, .. } => f(krate),
215 PrintExtra::NeedsAstMap { tcx } => f(&tcx.resolver_for_lowering().borrow().1),
216 }
217 }
218
219 fn tcx(&self) -> TyCtxt<'tcx> {
220 match self {
221 PrintExtra::AfterParsing { .. } => bug!("PrintExtra::tcx"),
222 PrintExtra::NeedsAstMap { tcx } => *tcx,
223 }
224 }
225}
226
227pub fn print<'tcx>(sess: &Session, ppm: PpMode, ex: PrintExtra<'tcx>) {
228 if ppm.needs_analysis() {
229 ex.tcx().ensure_ok().analysis(());
230 }
231
232 let (src, src_name) = get_source(sess);
233
234 let out = match ppm {
235 Source(s) => {
236 debug!("pretty printing source code {:?}", s);
237 let annotation: Box<dyn pprust_ast::PpAnn> = match s {
238 Normal => Box::new(AstNoAnn),
239 Expanded => Box::new(AstNoAnn),
240 Identified => Box::new(AstIdentifiedAnn),
241 ExpandedIdentified => Box::new(AstIdentifiedAnn),
242 ExpandedHygiene => Box::new(AstHygieneAnn { sess }),
243 };
244 let psess = &sess.psess;
245 let is_expanded = ppm.needs_ast_map();
246 ex.with_krate(|krate| {
247 pprust_ast::print_crate(
248 sess.source_map(),
249 krate,
250 src_name,
251 src,
252 &*annotation,
253 is_expanded,
254 psess.edition,
255 &sess.psess.attr_id_generator,
256 )
257 })
258 }
259 AstTree => {
260 debug!("pretty printing AST tree");
261 ex.with_krate(|krate| format!("{krate:#?}"))
262 }
263 AstTreeExpanded => {
264 debug!("pretty-printing expanded AST");
265 format!("{:#?}", ex.tcx().resolver_for_lowering().borrow().1)
266 }
267 Hir(s) => {
268 debug!("pretty printing HIR {:?}", s);
269 let tcx = ex.tcx();
270 let f = |annotation: &dyn pprust_hir::PpAnn| {
271 let sm = sess.source_map();
272 let hir_map = tcx.hir();
273 let attrs = |id| hir_map.attrs(id);
274 pprust_hir::print_crate(
275 sm,
276 hir_map.root_module(),
277 src_name,
278 src,
279 &attrs,
280 annotation,
281 )
282 };
283 match s {
284 PpHirMode::Normal => f(&tcx),
285 PpHirMode::Identified => {
286 let annotation = HirIdentifiedAnn { tcx };
287 f(&annotation)
288 }
289 PpHirMode::Typed => {
290 let annotation = HirTypedAnn { tcx, maybe_typeck_results: Cell::new(None) };
291 tcx.dep_graph.with_ignore(|| f(&annotation))
292 }
293 }
294 }
295 HirTree => {
296 debug!("pretty printing HIR tree");
297 format!("{:#?}", ex.tcx().hir().krate())
298 }
299 Mir => {
300 let mut out = Vec::new();
301 write_mir_pretty(ex.tcx(), None, &mut out).unwrap();
302 String::from_utf8(out).unwrap()
303 }
304 MirCFG => {
305 let mut out = Vec::new();
306 write_mir_graphviz(ex.tcx(), None, &mut out).unwrap();
307 String::from_utf8(out).unwrap()
308 }
309 StableMir => {
310 let mut out = Vec::new();
311 write_smir_pretty(ex.tcx(), &mut out).unwrap();
312 String::from_utf8(out).unwrap()
313 }
314 ThirTree => {
315 let tcx = ex.tcx();
316 let mut out = String::new();
317 rustc_hir_analysis::check_crate(tcx);
318 tcx.dcx().abort_if_errors();
319 debug!("pretty printing THIR tree");
320 for did in tcx.hir().body_owners() {
321 let _ = writeln!(out, "{:?}:\n{}\n", did, thir_tree(tcx, did));
322 }
323 out
324 }
325 ThirFlat => {
326 let tcx = ex.tcx();
327 let mut out = String::new();
328 rustc_hir_analysis::check_crate(tcx);
329 tcx.dcx().abort_if_errors();
330 debug!("pretty printing THIR flat");
331 for did in tcx.hir().body_owners() {
332 let _ = writeln!(out, "{:?}:\n{}\n", did, thir_flat(tcx, did));
333 }
334 out
335 }
336 };
337
338 write_or_print(&out, sess);
339}