fe_mir/pretty_print/
value.rs1use std::fmt::{self, Write};
2
3use crate::{
4 db::MirDb,
5 ir::{
6 constant::ConstantValue, function::BodyDataStore, value::AssignableValue, Value, ValueId,
7 },
8};
9
10use super::PrettyPrint;
11
12impl PrettyPrint for ValueId {
13 fn pretty_print<W: Write>(
14 &self,
15 db: &dyn MirDb,
16 store: &BodyDataStore,
17 w: &mut W,
18 ) -> fmt::Result {
19 match store.value_data(*self) {
20 Value::Temporary { .. } | Value::Local(_) => write!(w, "_{}", self.index()),
21 Value::Immediate { imm, .. } => write!(w, "{imm}"),
22 Value::Constant { constant, .. } => {
23 let const_value = constant.data(db);
24 write!(w, "const ")?;
25 match &const_value.value {
26 ConstantValue::Immediate(num) => write!(w, "{num}"),
27 ConstantValue::Str(s) => write!(w, r#""{s}""#),
28 ConstantValue::Bool(b) => write!(w, "{b}"),
29 }
30 }
31 Value::Unit { .. } => write!(w, "()"),
32 }
33 }
34}
35
36impl PrettyPrint for &[ValueId] {
37 fn pretty_print<W: Write>(
38 &self,
39 db: &dyn MirDb,
40 store: &BodyDataStore,
41 w: &mut W,
42 ) -> fmt::Result {
43 if self.is_empty() {
44 return Ok(());
45 }
46
47 let arg_len = self.len();
48 for arg in self.iter().take(arg_len - 1) {
49 arg.pretty_print(db, store, w)?;
50 write!(w, ", ")?;
51 }
52 let arg = self[arg_len - 1];
53 arg.pretty_print(db, store, w)
54 }
55}
56
57impl PrettyPrint for AssignableValue {
58 fn pretty_print<W: Write>(
59 &self,
60 db: &dyn MirDb,
61 store: &BodyDataStore,
62 w: &mut W,
63 ) -> fmt::Result {
64 match self {
65 Self::Value(value) => value.pretty_print(db, store, w),
66 Self::Aggregate { lhs, idx } => {
67 lhs.pretty_print(db, store, w)?;
68 write!(w, ".<")?;
69 idx.pretty_print(db, store, w)?;
70 write!(w, ">")
71 }
72
73 Self::Map { lhs, key } => {
74 lhs.pretty_print(db, store, w)?;
75 write!(w, "{{")?;
76 key.pretty_print(db, store, w)?;
77 write!(w, "}}")
78 }
79 }
80 }
81}