1use std::fmt::{self, Write};
2
3use rustc_middle::thir::*;
4use rustc_middle::ty;
5use rustc_middle::ty::TyCtxt;
6use rustc_span::def_id::LocalDefId;
7
8pub fn thir_tree(tcx: TyCtxt<'_>, owner_def: LocalDefId) -> String {
10 match super::cx::thir_body(tcx, owner_def) {
11 Ok((thir, expr)) => {
12 let thir = thir.steal();
13 let mut printer = ThirPrinter::new(&thir);
14 printer.print(expr);
15 printer.into_buffer()
16 }
17 Err(_) => "error".into(),
18 }
19}
20
21pub fn thir_flat(tcx: TyCtxt<'_>, owner_def: LocalDefId) -> String {
23 match super::cx::thir_body(tcx, owner_def) {
24 Ok((thir, _)) => format!("{:#?}", thir.steal()),
25 Err(_) => "error".into(),
26 }
27}
28
29struct ThirPrinter<'a, 'tcx> {
30 thir: &'a Thir<'tcx>,
31 fmt: String,
32}
33
34const INDENT: &str = " ";
35
36macro_rules! print_indented {
37 ($writer:ident, $s:expr, $indent_lvl:expr) => {
38 $writer.indent($indent_lvl);
39 writeln!($writer, "{}", $s).expect("unable to write to ThirPrinter");
40 };
41}
42
43impl<'a, 'tcx> Write for ThirPrinter<'a, 'tcx> {
44 fn write_str(&mut self, s: &str) -> fmt::Result {
45 self.fmt.push_str(s);
46 Ok(())
47 }
48}
49
50impl<'a, 'tcx> ThirPrinter<'a, 'tcx> {
51 fn new(thir: &'a Thir<'tcx>) -> Self {
52 Self { thir, fmt: String::new() }
53 }
54
55 fn indent(&mut self, level: usize) {
56 for _ in 0..level {
57 self.fmt.push_str(INDENT);
58 }
59 }
60
61 fn print(&mut self, body_expr: ExprId) {
62 print_indented!(self, "params: [", 0);
63 for param in self.thir.params.iter() {
64 self.print_param(param, 1);
65 }
66 print_indented!(self, "]", 0);
67
68 print_indented!(self, "body:", 0);
69 self.print_expr(body_expr, 1);
70 }
71
72 fn into_buffer(self) -> String {
73 self.fmt
74 }
75
76 fn print_param(&mut self, param: &Param<'tcx>, depth_lvl: usize) {
77 let Param { pat, ty, ty_span, self_kind, hir_id } = param;
78
79 print_indented!(self, "Param {", depth_lvl);
80 print_indented!(self, format!("ty: {:?}", ty), depth_lvl + 1);
81 print_indented!(self, format!("ty_span: {:?}", ty_span), depth_lvl + 1);
82 print_indented!(self, format!("self_kind: {:?}", self_kind), depth_lvl + 1);
83 print_indented!(self, format!("hir_id: {:?}", hir_id), depth_lvl + 1);
84
85 if let Some(pat) = pat {
86 print_indented!(self, "param: Some( ", depth_lvl + 1);
87 self.print_pat(pat, depth_lvl + 2);
88 print_indented!(self, ")", depth_lvl + 1);
89 } else {
90 print_indented!(self, "param: None", depth_lvl + 1);
91 }
92
93 print_indented!(self, "}", depth_lvl);
94 }
95
96 fn print_block(&mut self, block_id: BlockId, depth_lvl: usize) {
97 let Block { targeted_by_break, span, region_scope, stmts, expr, safety_mode } =
98 &self.thir.blocks[block_id];
99
100 print_indented!(self, "Block {", depth_lvl);
101 print_indented!(self, format!("targeted_by_break: {}", targeted_by_break), depth_lvl + 1);
102 print_indented!(self, format!("span: {:?}", span), depth_lvl + 1);
103 print_indented!(self, format!("region_scope: {:?}", region_scope), depth_lvl + 1);
104 print_indented!(self, format!("safety_mode: {:?}", safety_mode), depth_lvl + 1);
105
106 if stmts.len() > 0 {
107 print_indented!(self, "stmts: [", depth_lvl + 1);
108 for stmt in stmts.iter() {
109 self.print_stmt(*stmt, depth_lvl + 2);
110 }
111 print_indented!(self, "]", depth_lvl + 1);
112 } else {
113 print_indented!(self, "stmts: []", depth_lvl + 1);
114 }
115
116 if let Some(expr_id) = expr {
117 print_indented!(self, "expr:", depth_lvl + 1);
118 self.print_expr(*expr_id, depth_lvl + 2);
119 } else {
120 print_indented!(self, "expr: []", depth_lvl + 1);
121 }
122
123 print_indented!(self, "}", depth_lvl);
124 }
125
126 fn print_stmt(&mut self, stmt_id: StmtId, depth_lvl: usize) {
127 let Stmt { kind } = &self.thir.stmts[stmt_id];
128
129 print_indented!(self, "Stmt {", depth_lvl);
130
131 match kind {
132 StmtKind::Expr { scope, expr } => {
133 print_indented!(self, "kind: Expr {", depth_lvl + 1);
134 print_indented!(self, format!("scope: {:?}", scope), depth_lvl + 2);
135 print_indented!(self, "expr:", depth_lvl + 2);
136 self.print_expr(*expr, depth_lvl + 3);
137 print_indented!(self, "}", depth_lvl + 1);
138 }
139 StmtKind::Let {
140 remainder_scope,
141 init_scope,
142 pattern,
143 initializer,
144 else_block,
145 lint_level,
146 span,
147 } => {
148 print_indented!(self, "kind: Let {", depth_lvl + 1);
149 print_indented!(
150 self,
151 format!("remainder_scope: {:?}", remainder_scope),
152 depth_lvl + 2
153 );
154 print_indented!(self, format!("init_scope: {:?}", init_scope), depth_lvl + 2);
155
156 print_indented!(self, "pattern: ", depth_lvl + 2);
157 self.print_pat(pattern, depth_lvl + 3);
158 print_indented!(self, ",", depth_lvl + 2);
159
160 if let Some(init) = initializer {
161 print_indented!(self, "initializer: Some(", depth_lvl + 2);
162 self.print_expr(*init, depth_lvl + 3);
163 print_indented!(self, ")", depth_lvl + 2);
164 } else {
165 print_indented!(self, "initializer: None", depth_lvl + 2);
166 }
167
168 if let Some(else_block) = else_block {
169 print_indented!(self, "else_block: Some(", depth_lvl + 2);
170 self.print_block(*else_block, depth_lvl + 3);
171 print_indented!(self, ")", depth_lvl + 2);
172 } else {
173 print_indented!(self, "else_block: None", depth_lvl + 2);
174 }
175
176 print_indented!(self, format!("lint_level: {:?}", lint_level), depth_lvl + 2);
177 print_indented!(self, format!("span: {:?}", span), depth_lvl + 2);
178 print_indented!(self, "}", depth_lvl + 1);
179 }
180 }
181
182 print_indented!(self, "}", depth_lvl);
183 }
184
185 fn print_expr(&mut self, expr: ExprId, depth_lvl: usize) {
186 let Expr { ty, temp_lifetime, span, kind } = &self.thir[expr];
187 print_indented!(self, "Expr {", depth_lvl);
188 print_indented!(self, format!("ty: {:?}", ty), depth_lvl + 1);
189 print_indented!(self, format!("temp_lifetime: {:?}", temp_lifetime), depth_lvl + 1);
190 print_indented!(self, format!("span: {:?}", span), depth_lvl + 1);
191 print_indented!(self, "kind: ", depth_lvl + 1);
192 self.print_expr_kind(kind, depth_lvl + 2);
193 print_indented!(self, "}", depth_lvl);
194 }
195
196 fn print_expr_kind(&mut self, expr_kind: &ExprKind<'tcx>, depth_lvl: usize) {
197 use rustc_middle::thir::ExprKind::*;
198
199 match expr_kind {
200 Scope { region_scope, value, lint_level } => {
201 print_indented!(self, "Scope {", depth_lvl);
202 print_indented!(self, format!("region_scope: {:?}", region_scope), depth_lvl + 1);
203 print_indented!(self, format!("lint_level: {:?}", lint_level), depth_lvl + 1);
204 print_indented!(self, "value:", depth_lvl + 1);
205 self.print_expr(*value, depth_lvl + 2);
206 print_indented!(self, "}", depth_lvl);
207 }
208 Box { value } => {
209 print_indented!(self, "Box {", depth_lvl);
210 self.print_expr(*value, depth_lvl + 1);
211 print_indented!(self, "}", depth_lvl);
212 }
213 If { if_then_scope, cond, then, else_opt } => {
214 print_indented!(self, "If {", depth_lvl);
215 print_indented!(self, format!("if_then_scope: {:?}", if_then_scope), depth_lvl + 1);
216 print_indented!(self, "cond:", depth_lvl + 1);
217 self.print_expr(*cond, depth_lvl + 2);
218 print_indented!(self, "then:", depth_lvl + 1);
219 self.print_expr(*then, depth_lvl + 2);
220
221 if let Some(else_expr) = else_opt {
222 print_indented!(self, "else:", depth_lvl + 1);
223 self.print_expr(*else_expr, depth_lvl + 2);
224 }
225
226 print_indented!(self, "}", depth_lvl);
227 }
228 Call { fun, args, ty, from_hir_call, fn_span } => {
229 print_indented!(self, "Call {", depth_lvl);
230 print_indented!(self, format!("ty: {:?}", ty), depth_lvl + 1);
231 print_indented!(self, format!("from_hir_call: {}", from_hir_call), depth_lvl + 1);
232 print_indented!(self, format!("fn_span: {:?}", fn_span), depth_lvl + 1);
233 print_indented!(self, "fun:", depth_lvl + 1);
234 self.print_expr(*fun, depth_lvl + 2);
235
236 if args.len() > 0 {
237 print_indented!(self, "args: [", depth_lvl + 1);
238 for arg in args.iter() {
239 self.print_expr(*arg, depth_lvl + 2);
240 }
241 print_indented!(self, "]", depth_lvl + 1);
242 } else {
243 print_indented!(self, "args: []", depth_lvl + 1);
244 }
245
246 print_indented!(self, "}", depth_lvl);
247 }
248 ByUse { expr, span } => {
249 print_indented!(self, "ByUse {", depth_lvl);
250 print_indented!(self, "expr:", depth_lvl + 1);
251 self.print_expr(*expr, depth_lvl + 2);
252 print_indented!(self, format!("span: {:?}", span), depth_lvl + 1);
253 print_indented!(self, "}", depth_lvl);
254 }
255 Deref { arg } => {
256 print_indented!(self, "Deref {", depth_lvl);
257 self.print_expr(*arg, depth_lvl + 1);
258 print_indented!(self, "}", depth_lvl);
259 }
260 Binary { op, lhs, rhs } => {
261 print_indented!(self, "Binary {", depth_lvl);
262 print_indented!(self, format!("op: {:?}", op), depth_lvl + 1);
263 print_indented!(self, "lhs:", depth_lvl + 1);
264 self.print_expr(*lhs, depth_lvl + 2);
265 print_indented!(self, "rhs:", depth_lvl + 1);
266 self.print_expr(*rhs, depth_lvl + 2);
267 print_indented!(self, "}", depth_lvl);
268 }
269 LogicalOp { op, lhs, rhs } => {
270 print_indented!(self, "LogicalOp {", depth_lvl);
271 print_indented!(self, format!("op: {:?}", op), depth_lvl + 1);
272 print_indented!(self, "lhs:", depth_lvl + 1);
273 self.print_expr(*lhs, depth_lvl + 2);
274 print_indented!(self, "rhs:", depth_lvl + 1);
275 self.print_expr(*rhs, depth_lvl + 2);
276 print_indented!(self, "}", depth_lvl);
277 }
278 Unary { op, arg } => {
279 print_indented!(self, "Unary {", depth_lvl);
280 print_indented!(self, format!("op: {:?}", op), depth_lvl + 1);
281 print_indented!(self, "arg:", depth_lvl + 1);
282 self.print_expr(*arg, depth_lvl + 2);
283 print_indented!(self, "}", depth_lvl);
284 }
285 Cast { source } => {
286 print_indented!(self, "Cast {", depth_lvl);
287 print_indented!(self, "source:", depth_lvl + 1);
288 self.print_expr(*source, depth_lvl + 2);
289 print_indented!(self, "}", depth_lvl);
290 }
291 Use { source } => {
292 print_indented!(self, "Use {", depth_lvl);
293 print_indented!(self, "source:", depth_lvl + 1);
294 self.print_expr(*source, depth_lvl + 2);
295 print_indented!(self, "}", depth_lvl);
296 }
297 NeverToAny { source } => {
298 print_indented!(self, "NeverToAny {", depth_lvl);
299 print_indented!(self, "source:", depth_lvl + 1);
300 self.print_expr(*source, depth_lvl + 2);
301 print_indented!(self, "}", depth_lvl);
302 }
303 PointerCoercion { cast, is_from_as_cast, source } => {
304 print_indented!(self, "Pointer {", depth_lvl);
305 print_indented!(self, format!("cast: {:?}", cast), depth_lvl + 1);
306 print_indented!(
307 self,
308 format!("is_from_as_cast: {:?}", is_from_as_cast),
309 depth_lvl + 1
310 );
311 print_indented!(self, "source:", depth_lvl + 1);
312 self.print_expr(*source, depth_lvl + 2);
313 print_indented!(self, "}", depth_lvl);
314 }
315 Loop { body } => {
316 print_indented!(self, "Loop (", depth_lvl);
317 print_indented!(self, "body:", depth_lvl + 1);
318 self.print_expr(*body, depth_lvl + 2);
319 print_indented!(self, ")", depth_lvl);
320 }
321 Let { expr, pat } => {
322 print_indented!(self, "Let {", depth_lvl);
323 print_indented!(self, "expr:", depth_lvl + 1);
324 self.print_expr(*expr, depth_lvl + 2);
325 print_indented!(self, format!("pat: {:?}", pat), depth_lvl + 1);
326 print_indented!(self, "}", depth_lvl);
327 }
328 Match { scrutinee, arms, .. } => {
329 print_indented!(self, "Match {", depth_lvl);
330 print_indented!(self, "scrutinee:", depth_lvl + 1);
331 self.print_expr(*scrutinee, depth_lvl + 2);
332
333 print_indented!(self, "arms: [", depth_lvl + 1);
334 for arm_id in arms.iter() {
335 self.print_arm(*arm_id, depth_lvl + 2);
336 }
337 print_indented!(self, "]", depth_lvl + 1);
338 print_indented!(self, "}", depth_lvl);
339 }
340 Block { block } => self.print_block(*block, depth_lvl),
341 Assign { lhs, rhs } => {
342 print_indented!(self, "Assign {", depth_lvl);
343 print_indented!(self, "lhs:", depth_lvl + 1);
344 self.print_expr(*lhs, depth_lvl + 2);
345 print_indented!(self, "rhs:", depth_lvl + 1);
346 self.print_expr(*rhs, depth_lvl + 2);
347 print_indented!(self, "}", depth_lvl);
348 }
349 AssignOp { op, lhs, rhs } => {
350 print_indented!(self, "AssignOp {", depth_lvl);
351 print_indented!(self, format!("op: {:?}", op), depth_lvl + 1);
352 print_indented!(self, "lhs:", depth_lvl + 1);
353 self.print_expr(*lhs, depth_lvl + 2);
354 print_indented!(self, "rhs:", depth_lvl + 1);
355 self.print_expr(*rhs, depth_lvl + 2);
356 print_indented!(self, "}", depth_lvl);
357 }
358 Field { lhs, variant_index, name } => {
359 print_indented!(self, "Field {", depth_lvl);
360 print_indented!(self, format!("variant_index: {:?}", variant_index), depth_lvl + 1);
361 print_indented!(self, format!("name: {:?}", name), depth_lvl + 1);
362 print_indented!(self, "lhs:", depth_lvl + 1);
363 self.print_expr(*lhs, depth_lvl + 2);
364 print_indented!(self, "}", depth_lvl);
365 }
366 Index { lhs, index } => {
367 print_indented!(self, "Index {", depth_lvl);
368 print_indented!(self, format!("index: {:?}", index), depth_lvl + 1);
369 print_indented!(self, "lhs:", depth_lvl + 1);
370 self.print_expr(*lhs, depth_lvl + 2);
371 print_indented!(self, "}", depth_lvl);
372 }
373 VarRef { id } => {
374 print_indented!(self, "VarRef {", depth_lvl);
375 print_indented!(self, format!("id: {:?}", id), depth_lvl + 1);
376 print_indented!(self, "}", depth_lvl);
377 }
378 UpvarRef { closure_def_id, var_hir_id } => {
379 print_indented!(self, "UpvarRef {", depth_lvl);
380 print_indented!(
381 self,
382 format!("closure_def_id: {:?}", closure_def_id),
383 depth_lvl + 1
384 );
385 print_indented!(self, format!("var_hir_id: {:?}", var_hir_id), depth_lvl + 1);
386 print_indented!(self, "}", depth_lvl);
387 }
388 Borrow { borrow_kind, arg } => {
389 print_indented!(self, "Borrow (", depth_lvl);
390 print_indented!(self, format!("borrow_kind: {:?}", borrow_kind), depth_lvl + 1);
391 print_indented!(self, "arg:", depth_lvl + 1);
392 self.print_expr(*arg, depth_lvl + 2);
393 print_indented!(self, ")", depth_lvl);
394 }
395 RawBorrow { mutability, arg } => {
396 print_indented!(self, "RawBorrow {", depth_lvl);
397 print_indented!(self, format!("mutability: {:?}", mutability), depth_lvl + 1);
398 print_indented!(self, "arg:", depth_lvl + 1);
399 self.print_expr(*arg, depth_lvl + 2);
400 print_indented!(self, "}", depth_lvl);
401 }
402 Break { label, value } => {
403 print_indented!(self, "Break (", depth_lvl);
404 print_indented!(self, format!("label: {:?}", label), depth_lvl + 1);
405
406 if let Some(value) = value {
407 print_indented!(self, "value:", depth_lvl + 1);
408 self.print_expr(*value, depth_lvl + 2);
409 }
410
411 print_indented!(self, ")", depth_lvl);
412 }
413 Continue { label } => {
414 print_indented!(self, "Continue {", depth_lvl);
415 print_indented!(self, format!("label: {:?}", label), depth_lvl + 1);
416 print_indented!(self, "}", depth_lvl);
417 }
418 Return { value } => {
419 print_indented!(self, "Return {", depth_lvl);
420 print_indented!(self, "value:", depth_lvl + 1);
421
422 if let Some(value) = value {
423 self.print_expr(*value, depth_lvl + 2);
424 }
425
426 print_indented!(self, "}", depth_lvl);
427 }
428 Become { value } => {
429 print_indented!(self, "Become {", depth_lvl);
430 print_indented!(self, "value:", depth_lvl + 1);
431 self.print_expr(*value, depth_lvl + 2);
432 print_indented!(self, "}", depth_lvl);
433 }
434 ConstBlock { did, args } => {
435 print_indented!(self, "ConstBlock {", depth_lvl);
436 print_indented!(self, format!("did: {:?}", did), depth_lvl + 1);
437 print_indented!(self, format!("args: {:?}", args), depth_lvl + 1);
438 print_indented!(self, "}", depth_lvl);
439 }
440 Repeat { value, count } => {
441 print_indented!(self, "Repeat {", depth_lvl);
442 print_indented!(self, format!("count: {:?}", count), depth_lvl + 1);
443 print_indented!(self, "value:", depth_lvl + 1);
444 self.print_expr(*value, depth_lvl + 2);
445 print_indented!(self, "}", depth_lvl);
446 }
447 Array { fields } => {
448 print_indented!(self, "Array {", depth_lvl);
449 print_indented!(self, "fields: [", depth_lvl + 1);
450 for field_id in fields.iter() {
451 self.print_expr(*field_id, depth_lvl + 2);
452 }
453 print_indented!(self, "]", depth_lvl + 1);
454 print_indented!(self, "}", depth_lvl);
455 }
456 Tuple { fields } => {
457 print_indented!(self, "Tuple {", depth_lvl);
458 print_indented!(self, "fields: [", depth_lvl + 1);
459 for field_id in fields.iter() {
460 self.print_expr(*field_id, depth_lvl + 2);
461 }
462 print_indented!(self, "]", depth_lvl + 1);
463 print_indented!(self, "}", depth_lvl);
464 }
465 Adt(adt_expr) => {
466 print_indented!(self, "Adt {", depth_lvl);
467 self.print_adt_expr(&**adt_expr, depth_lvl + 1);
468 print_indented!(self, "}", depth_lvl);
469 }
470 PlaceTypeAscription { source, user_ty, user_ty_span } => {
471 print_indented!(self, "PlaceTypeAscription {", depth_lvl);
472 print_indented!(self, format!("user_ty: {:?}", user_ty), depth_lvl + 1);
473 print_indented!(self, format!("user_ty_span: {:?}", user_ty_span), depth_lvl + 1);
474 print_indented!(self, "source:", depth_lvl + 1);
475 self.print_expr(*source, depth_lvl + 2);
476 print_indented!(self, "}", depth_lvl);
477 }
478 ValueTypeAscription { source, user_ty, user_ty_span } => {
479 print_indented!(self, "ValueTypeAscription {", depth_lvl);
480 print_indented!(self, format!("user_ty: {:?}", user_ty), depth_lvl + 1);
481 print_indented!(self, format!("user_ty_span: {:?}", user_ty_span), depth_lvl + 1);
482 print_indented!(self, "source:", depth_lvl + 1);
483 self.print_expr(*source, depth_lvl + 2);
484 print_indented!(self, "}", depth_lvl);
485 }
486 PlaceUnwrapUnsafeBinder { source } => {
487 print_indented!(self, "PlaceUnwrapUnsafeBinder {", depth_lvl);
488 print_indented!(self, "source:", depth_lvl + 1);
489 self.print_expr(*source, depth_lvl + 2);
490 print_indented!(self, "}", depth_lvl);
491 }
492 ValueUnwrapUnsafeBinder { source } => {
493 print_indented!(self, "ValueUnwrapUnsafeBinder {", depth_lvl);
494 print_indented!(self, "source:", depth_lvl + 1);
495 self.print_expr(*source, depth_lvl + 2);
496 print_indented!(self, "}", depth_lvl);
497 }
498 WrapUnsafeBinder { source } => {
499 print_indented!(self, "WrapUnsafeBinder {", depth_lvl);
500 print_indented!(self, "source:", depth_lvl + 1);
501 self.print_expr(*source, depth_lvl + 2);
502 print_indented!(self, "}", depth_lvl);
503 }
504 Closure(closure_expr) => {
505 print_indented!(self, "Closure {", depth_lvl);
506 print_indented!(self, "closure_expr:", depth_lvl + 1);
507 self.print_closure_expr(&**closure_expr, depth_lvl + 2);
508 print_indented!(self, "}", depth_lvl);
509 }
510 Literal { lit, neg } => {
511 print_indented!(
512 self,
513 format!("Literal( lit: {:?}, neg: {:?})\n", lit, neg),
514 depth_lvl
515 );
516 }
517 NonHirLiteral { lit, user_ty } => {
518 print_indented!(self, "NonHirLiteral {", depth_lvl);
519 print_indented!(self, format!("lit: {:?}", lit), depth_lvl + 1);
520 print_indented!(self, format!("user_ty: {:?}", user_ty), depth_lvl + 1);
521 print_indented!(self, "}", depth_lvl);
522 }
523 ZstLiteral { user_ty } => {
524 print_indented!(self, format!("ZstLiteral(user_ty: {:?})", user_ty), depth_lvl);
525 }
526 NamedConst { def_id, args, user_ty } => {
527 print_indented!(self, "NamedConst {", depth_lvl);
528 print_indented!(self, format!("def_id: {:?}", def_id), depth_lvl + 1);
529 print_indented!(self, format!("user_ty: {:?}", user_ty), depth_lvl + 1);
530 print_indented!(self, format!("args: {:?}", args), depth_lvl + 1);
531 print_indented!(self, "}", depth_lvl);
532 }
533 ConstParam { param, def_id } => {
534 print_indented!(self, "ConstParam {", depth_lvl);
535 print_indented!(self, format!("def_id: {:?}", def_id), depth_lvl + 1);
536 print_indented!(self, format!("param: {:?}", param), depth_lvl + 1);
537 print_indented!(self, "}", depth_lvl);
538 }
539 StaticRef { alloc_id, ty, def_id } => {
540 print_indented!(self, "StaticRef {", depth_lvl);
541 print_indented!(self, format!("def_id: {:?}", def_id), depth_lvl + 1);
542 print_indented!(self, format!("ty: {:?}", ty), depth_lvl + 1);
543 print_indented!(self, format!("alloc_id: {:?}", alloc_id), depth_lvl + 1);
544 print_indented!(self, "}", depth_lvl);
545 }
546 InlineAsm(expr) => {
547 print_indented!(self, "InlineAsm {", depth_lvl);
548 print_indented!(self, "expr:", depth_lvl + 1);
549 self.print_inline_asm_expr(&**expr, depth_lvl + 2);
550 print_indented!(self, "}", depth_lvl);
551 }
552 OffsetOf { container, fields } => {
553 print_indented!(self, "OffsetOf {", depth_lvl);
554 print_indented!(self, format!("container: {:?}", container), depth_lvl + 1);
555 print_indented!(self, "fields: [", depth_lvl + 1);
556
557 for field in fields.iter() {
558 print_indented!(self, format!("{:?}", field), depth_lvl + 2);
559 print_indented!(self, ",", depth_lvl + 1);
560 }
561
562 print_indented!(self, "]", depth_lvl + 1);
563 print_indented!(self, "}", depth_lvl);
564 }
565 ThreadLocalRef(def_id) => {
566 print_indented!(self, "ThreadLocalRef {", depth_lvl);
567 print_indented!(self, format!("def_id: {:?}", def_id), depth_lvl + 1);
568 print_indented!(self, "}", depth_lvl);
569 }
570 Yield { value } => {
571 print_indented!(self, "Yield {", depth_lvl);
572 print_indented!(self, "value:", depth_lvl + 1);
573 self.print_expr(*value, depth_lvl + 2);
574 print_indented!(self, "}", depth_lvl);
575 }
576 }
577 }
578
579 fn print_adt_expr(&mut self, adt_expr: &AdtExpr<'tcx>, depth_lvl: usize) {
580 print_indented!(self, "adt_def:", depth_lvl);
581 self.print_adt_def(adt_expr.adt_def, depth_lvl + 1);
582 print_indented!(
583 self,
584 format!("variant_index: {:?}", adt_expr.variant_index),
585 depth_lvl + 1
586 );
587 print_indented!(self, format!("args: {:?}", adt_expr.args), depth_lvl + 1);
588 print_indented!(self, format!("user_ty: {:?}", adt_expr.user_ty), depth_lvl + 1);
589
590 for (i, field_expr) in adt_expr.fields.iter().enumerate() {
591 print_indented!(self, format!("field {}:", i), depth_lvl + 1);
592 self.print_expr(field_expr.expr, depth_lvl + 2);
593 }
594
595 match adt_expr.base {
596 AdtExprBase::Base(ref base) => {
597 print_indented!(self, "base:", depth_lvl + 1);
598 self.print_fru_info(base, depth_lvl + 2);
599 }
600 AdtExprBase::DefaultFields(_) => {
601 print_indented!(self, "base: {{ defaulted fields }}", depth_lvl + 1);
602 }
603 AdtExprBase::None => {
604 print_indented!(self, "base: None", depth_lvl + 1);
605 }
606 }
607 }
608
609 fn print_adt_def(&mut self, adt_def: ty::AdtDef<'tcx>, depth_lvl: usize) {
610 print_indented!(self, "AdtDef {", depth_lvl);
611 print_indented!(self, format!("did: {:?}", adt_def.did()), depth_lvl + 1);
612 print_indented!(self, format!("variants: {:?}", adt_def.variants()), depth_lvl + 1);
613 print_indented!(self, format!("flags: {:?}", adt_def.flags()), depth_lvl + 1);
614 print_indented!(self, format!("repr: {:?}", adt_def.repr()), depth_lvl + 1);
615 }
616
617 fn print_fru_info(&mut self, fru_info: &FruInfo<'tcx>, depth_lvl: usize) {
618 print_indented!(self, "FruInfo {", depth_lvl);
619 print_indented!(self, "base: ", depth_lvl + 1);
620 self.print_expr(fru_info.base, depth_lvl + 2);
621 print_indented!(self, "field_types: [", depth_lvl + 1);
622 for ty in fru_info.field_types.iter() {
623 print_indented!(self, format!("ty: {:?}", ty), depth_lvl + 2);
624 }
625 print_indented!(self, "}", depth_lvl);
626 }
627
628 fn print_arm(&mut self, arm_id: ArmId, depth_lvl: usize) {
629 print_indented!(self, "Arm {", depth_lvl);
630
631 let arm = &self.thir.arms[arm_id];
632 let Arm { pattern, guard, body, lint_level, scope, span } = arm;
633
634 print_indented!(self, "pattern: ", depth_lvl + 1);
635 self.print_pat(pattern, depth_lvl + 2);
636
637 if let Some(guard) = *guard {
638 print_indented!(self, "guard: ", depth_lvl + 1);
639 self.print_expr(guard, depth_lvl + 2);
640 } else {
641 print_indented!(self, "guard: None", depth_lvl + 1);
642 }
643
644 print_indented!(self, "body: ", depth_lvl + 1);
645 self.print_expr(*body, depth_lvl + 2);
646 print_indented!(self, format!("lint_level: {:?}", lint_level), depth_lvl + 1);
647 print_indented!(self, format!("scope: {:?}", scope), depth_lvl + 1);
648 print_indented!(self, format!("span: {:?}", span), depth_lvl + 1);
649 print_indented!(self, "}", depth_lvl);
650 }
651
652 fn print_pat(&mut self, pat: &Pat<'tcx>, depth_lvl: usize) {
653 let &Pat { ty, span, ref kind } = pat;
654
655 print_indented!(self, "Pat: {", depth_lvl);
656 print_indented!(self, format!("ty: {:?}", ty), depth_lvl + 1);
657 print_indented!(self, format!("span: {:?}", span), depth_lvl + 1);
658 self.print_pat_kind(kind, depth_lvl + 1);
659 print_indented!(self, "}", depth_lvl);
660 }
661
662 fn print_pat_kind(&mut self, pat_kind: &PatKind<'tcx>, depth_lvl: usize) {
663 print_indented!(self, "kind: PatKind {", depth_lvl);
664
665 match pat_kind {
666 PatKind::Missing => unreachable!(),
667 PatKind::Wild => {
668 print_indented!(self, "Wild", depth_lvl + 1);
669 }
670 PatKind::Never => {
671 print_indented!(self, "Never", depth_lvl + 1);
672 }
673 PatKind::AscribeUserType { ascription, subpattern } => {
674 print_indented!(self, "AscribeUserType: {", depth_lvl + 1);
675 print_indented!(self, format!("ascription: {:?}", ascription), depth_lvl + 2);
676 print_indented!(self, "subpattern: ", depth_lvl + 2);
677 self.print_pat(subpattern, depth_lvl + 3);
678 print_indented!(self, "}", depth_lvl + 1);
679 }
680 PatKind::Binding { name, mode, var, ty, subpattern, is_primary } => {
681 print_indented!(self, "Binding {", depth_lvl + 1);
682 print_indented!(self, format!("name: {:?}", name), depth_lvl + 2);
683 print_indented!(self, format!("mode: {:?}", mode), depth_lvl + 2);
684 print_indented!(self, format!("var: {:?}", var), depth_lvl + 2);
685 print_indented!(self, format!("ty: {:?}", ty), depth_lvl + 2);
686 print_indented!(self, format!("is_primary: {:?}", is_primary), depth_lvl + 2);
687
688 if let Some(subpattern) = subpattern {
689 print_indented!(self, "subpattern: Some( ", depth_lvl + 2);
690 self.print_pat(subpattern, depth_lvl + 3);
691 print_indented!(self, ")", depth_lvl + 2);
692 } else {
693 print_indented!(self, "subpattern: None", depth_lvl + 2);
694 }
695
696 print_indented!(self, "}", depth_lvl + 1);
697 }
698 PatKind::Variant { adt_def, args, variant_index, subpatterns } => {
699 print_indented!(self, "Variant {", depth_lvl + 1);
700 print_indented!(self, "adt_def: ", depth_lvl + 2);
701 self.print_adt_def(*adt_def, depth_lvl + 3);
702 print_indented!(self, format!("args: {:?}", args), depth_lvl + 2);
703 print_indented!(self, format!("variant_index: {:?}", variant_index), depth_lvl + 2);
704
705 if subpatterns.len() > 0 {
706 print_indented!(self, "subpatterns: [", depth_lvl + 2);
707 for field_pat in subpatterns.iter() {
708 self.print_pat(&field_pat.pattern, depth_lvl + 3);
709 }
710 print_indented!(self, "]", depth_lvl + 2);
711 } else {
712 print_indented!(self, "subpatterns: []", depth_lvl + 2);
713 }
714
715 print_indented!(self, "}", depth_lvl + 1);
716 }
717 PatKind::Leaf { subpatterns } => {
718 print_indented!(self, "Leaf { ", depth_lvl + 1);
719 print_indented!(self, "subpatterns: [", depth_lvl + 2);
720 for field_pat in subpatterns.iter() {
721 self.print_pat(&field_pat.pattern, depth_lvl + 3);
722 }
723 print_indented!(self, "]", depth_lvl + 2);
724 print_indented!(self, "}", depth_lvl + 1);
725 }
726 PatKind::Deref { subpattern } => {
727 print_indented!(self, "Deref { ", depth_lvl + 1);
728 print_indented!(self, "subpattern:", depth_lvl + 2);
729 self.print_pat(subpattern, depth_lvl + 2);
730 print_indented!(self, "}", depth_lvl + 1);
731 }
732 PatKind::DerefPattern { subpattern, .. } => {
733 print_indented!(self, "DerefPattern { ", depth_lvl + 1);
734 print_indented!(self, "subpattern:", depth_lvl + 2);
735 self.print_pat(subpattern, depth_lvl + 2);
736 print_indented!(self, "}", depth_lvl + 1);
737 }
738 PatKind::Constant { value } => {
739 print_indented!(self, "Constant {", depth_lvl + 1);
740 print_indented!(self, format!("value: {:?}", value), depth_lvl + 2);
741 print_indented!(self, "}", depth_lvl + 1);
742 }
743 PatKind::ExpandedConstant { def_id, subpattern } => {
744 print_indented!(self, "ExpandedConstant {", depth_lvl + 1);
745 print_indented!(self, format!("def_id: {def_id:?}"), depth_lvl + 2);
746 print_indented!(self, "subpattern:", depth_lvl + 2);
747 self.print_pat(subpattern, depth_lvl + 2);
748 print_indented!(self, "}", depth_lvl + 1);
749 }
750 PatKind::Range(pat_range) => {
751 print_indented!(self, format!("Range ( {:?} )", pat_range), depth_lvl + 1);
752 }
753 PatKind::Slice { prefix, slice, suffix } => {
754 print_indented!(self, "Slice {", depth_lvl + 1);
755
756 print_indented!(self, "prefix: [", depth_lvl + 2);
757 for prefix_pat in prefix.iter() {
758 self.print_pat(prefix_pat, depth_lvl + 3);
759 }
760 print_indented!(self, "]", depth_lvl + 2);
761
762 if let Some(slice) = slice {
763 print_indented!(self, "slice: ", depth_lvl + 2);
764 self.print_pat(slice, depth_lvl + 3);
765 }
766
767 print_indented!(self, "suffix: [", depth_lvl + 2);
768 for suffix_pat in suffix.iter() {
769 self.print_pat(suffix_pat, depth_lvl + 3);
770 }
771 print_indented!(self, "]", depth_lvl + 2);
772
773 print_indented!(self, "}", depth_lvl + 1);
774 }
775 PatKind::Array { prefix, slice, suffix } => {
776 print_indented!(self, "Array {", depth_lvl + 1);
777
778 print_indented!(self, "prefix: [", depth_lvl + 2);
779 for prefix_pat in prefix.iter() {
780 self.print_pat(prefix_pat, depth_lvl + 3);
781 }
782 print_indented!(self, "]", depth_lvl + 2);
783
784 if let Some(slice) = slice {
785 print_indented!(self, "slice: ", depth_lvl + 2);
786 self.print_pat(slice, depth_lvl + 3);
787 }
788
789 print_indented!(self, "suffix: [", depth_lvl + 2);
790 for suffix_pat in suffix.iter() {
791 self.print_pat(suffix_pat, depth_lvl + 3);
792 }
793 print_indented!(self, "]", depth_lvl + 2);
794
795 print_indented!(self, "}", depth_lvl + 1);
796 }
797 PatKind::Or { pats } => {
798 print_indented!(self, "Or {", depth_lvl + 1);
799 print_indented!(self, "pats: [", depth_lvl + 2);
800 for pat in pats.iter() {
801 self.print_pat(pat, depth_lvl + 3);
802 }
803 print_indented!(self, "]", depth_lvl + 2);
804 print_indented!(self, "}", depth_lvl + 1);
805 }
806 PatKind::Error(_) => {
807 print_indented!(self, "Error", depth_lvl + 1);
808 }
809 }
810
811 print_indented!(self, "}", depth_lvl);
812 }
813
814 fn print_closure_expr(&mut self, expr: &ClosureExpr<'tcx>, depth_lvl: usize) {
815 let ClosureExpr { closure_id, args, upvars, movability, fake_reads } = expr;
816
817 print_indented!(self, "ClosureExpr {", depth_lvl);
818 print_indented!(self, format!("closure_id: {:?}", closure_id), depth_lvl + 1);
819 print_indented!(self, format!("args: {:?}", args), depth_lvl + 1);
820
821 if upvars.len() > 0 {
822 print_indented!(self, "upvars: [", depth_lvl + 1);
823 for upvar in upvars.iter() {
824 self.print_expr(*upvar, depth_lvl + 2);
825 print_indented!(self, ",", depth_lvl + 1);
826 }
827 print_indented!(self, "]", depth_lvl + 1);
828 } else {
829 print_indented!(self, "upvars: []", depth_lvl + 1);
830 }
831
832 print_indented!(self, format!("movability: {:?}", movability), depth_lvl + 1);
833
834 if fake_reads.len() > 0 {
835 print_indented!(self, "fake_reads: [", depth_lvl + 1);
836 for (fake_read_expr, cause, hir_id) in fake_reads.iter() {
837 print_indented!(self, "(", depth_lvl + 2);
838 self.print_expr(*fake_read_expr, depth_lvl + 3);
839 print_indented!(self, ",", depth_lvl + 2);
840 print_indented!(self, format!("cause: {:?}", cause), depth_lvl + 3);
841 print_indented!(self, ",", depth_lvl + 2);
842 print_indented!(self, format!("hir_id: {:?}", hir_id), depth_lvl + 3);
843 print_indented!(self, "),", depth_lvl + 2);
844 }
845 print_indented!(self, "]", depth_lvl + 1);
846 } else {
847 print_indented!(self, "fake_reads: []", depth_lvl + 1);
848 }
849
850 print_indented!(self, "}", depth_lvl);
851 }
852
853 fn print_inline_asm_expr(&mut self, expr: &InlineAsmExpr<'tcx>, depth_lvl: usize) {
854 let InlineAsmExpr { asm_macro, template, operands, options, line_spans } = expr;
855
856 print_indented!(self, "InlineAsmExpr {", depth_lvl);
857
858 print_indented!(self, format!("asm_macro: {:?}", asm_macro), depth_lvl + 1);
859
860 print_indented!(self, "template: [", depth_lvl + 1);
861 for template_piece in template.iter() {
862 print_indented!(self, format!("{:?}", template_piece), depth_lvl + 2);
863 }
864 print_indented!(self, "]", depth_lvl + 1);
865
866 print_indented!(self, "operands: [", depth_lvl + 1);
867 for operand in operands.iter() {
868 self.print_inline_operand(operand, depth_lvl + 2);
869 }
870 print_indented!(self, "]", depth_lvl + 1);
871
872 print_indented!(self, format!("options: {:?}", options), depth_lvl + 1);
873 print_indented!(self, format!("line_spans: {:?}", line_spans), depth_lvl + 1);
874 }
875
876 fn print_inline_operand(&mut self, operand: &InlineAsmOperand<'tcx>, depth_lvl: usize) {
877 match operand {
878 InlineAsmOperand::In { reg, expr } => {
879 print_indented!(self, "InlineAsmOperand::In {", depth_lvl);
880 print_indented!(self, format!("reg: {:?}", reg), depth_lvl + 1);
881 print_indented!(self, "expr: ", depth_lvl + 1);
882 self.print_expr(*expr, depth_lvl + 2);
883 print_indented!(self, "}", depth_lvl + 1);
884 }
885 InlineAsmOperand::Out { reg, late, expr } => {
886 print_indented!(self, "InlineAsmOperand::Out {", depth_lvl);
887 print_indented!(self, format!("reg: {:?}", reg), depth_lvl + 1);
888 print_indented!(self, format!("late: {:?}", late), depth_lvl + 1);
889
890 if let Some(out) = expr {
891 print_indented!(self, "place: Some( ", depth_lvl + 1);
892 self.print_expr(*out, depth_lvl + 2);
893 print_indented!(self, ")", depth_lvl + 1);
894 } else {
895 print_indented!(self, "place: None", depth_lvl + 1);
896 }
897 print_indented!(self, "}", depth_lvl + 1);
898 }
899 InlineAsmOperand::InOut { reg, late, expr } => {
900 print_indented!(self, "InlineAsmOperand::InOut {", depth_lvl);
901 print_indented!(self, format!("reg: {:?}", reg), depth_lvl + 1);
902 print_indented!(self, format!("late: {:?}", late), depth_lvl + 1);
903 print_indented!(self, "expr: ", depth_lvl + 1);
904 self.print_expr(*expr, depth_lvl + 2);
905 print_indented!(self, "}", depth_lvl + 1);
906 }
907 InlineAsmOperand::SplitInOut { reg, late, in_expr, out_expr } => {
908 print_indented!(self, "InlineAsmOperand::SplitInOut {", depth_lvl);
909 print_indented!(self, format!("reg: {:?}", reg), depth_lvl + 1);
910 print_indented!(self, format!("late: {:?}", late), depth_lvl + 1);
911 print_indented!(self, "in_expr: ", depth_lvl + 1);
912 self.print_expr(*in_expr, depth_lvl + 2);
913
914 if let Some(out_expr) = out_expr {
915 print_indented!(self, "out_expr: Some( ", depth_lvl + 1);
916 self.print_expr(*out_expr, depth_lvl + 2);
917 print_indented!(self, ")", depth_lvl + 1);
918 } else {
919 print_indented!(self, "out_expr: None", depth_lvl + 1);
920 }
921
922 print_indented!(self, "}", depth_lvl + 1);
923 }
924 InlineAsmOperand::Const { value, span } => {
925 print_indented!(self, "InlineAsmOperand::Const {", depth_lvl);
926 print_indented!(self, format!("value: {:?}", value), depth_lvl + 1);
927 print_indented!(self, format!("span: {:?}", span), depth_lvl + 1);
928 print_indented!(self, "}", depth_lvl + 1);
929 }
930 InlineAsmOperand::SymFn { value } => {
931 print_indented!(self, "InlineAsmOperand::SymFn {", depth_lvl);
932 print_indented!(self, "value: ", depth_lvl + 1);
933 self.print_expr(*value, depth_lvl + 2);
934 print_indented!(self, "}", depth_lvl + 1);
935 }
936 InlineAsmOperand::SymStatic { def_id } => {
937 print_indented!(self, "InlineAsmOperand::SymStatic {", depth_lvl);
938 print_indented!(self, format!("def_id: {:?}", def_id), depth_lvl + 1);
939 print_indented!(self, "}", depth_lvl + 1);
940 }
941 InlineAsmOperand::Label { block } => {
942 print_indented!(self, "InlineAsmOperand::Block {", depth_lvl);
943 print_indented!(self, "block:", depth_lvl + 1);
944 self.print_block(*block, depth_lvl + 2);
945 print_indented!(self, "}", depth_lvl + 1);
946 }
947 }
948 }
949}