1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
//! [Display] implementations for [TypeKind], [Adt], and [Intrinsic]

use crate::node::Node;

use super::{Adt, Def, DefKind, Intrinsic, TypeKind, ValueKind};
use cl_ast::format::FmtAdapter;
use std::{
    fmt::{self, Display, Write},
    iter,
};

fn sep<'f, 's, Item, F>(
    before: &'s str,
    after: &'s str,
    t: F,
) -> impl FnOnce(&mut fmt::Formatter<'f>) -> fmt::Result + 's
where
    Item: FnMut(&mut fmt::Formatter<'f>) -> fmt::Result,
    F: FnMut() -> Option<Item> + 's,
{
    move |f| {
        f.write_str(before)?;
        for (idx, mut disp) in iter::from_fn(t).enumerate() {
            if idx > 0 {
                f.write_str(", ")?;
            }
            disp(f)?;
        }
        f.write_str(after)
    }
}

impl Display for Def<'_> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let Self { module, node: Node { in_path: _, span: _, meta, vis, kind: source }, kind } =
            self;
        if !meta.is_empty() {
            writeln!(f, "#{meta:?}")?;
        }
        if let Some(source) = source {
            if let Some(name) = source.name() {
                writeln!(f, "{vis}{name}:")?;
            }
            writeln!(f.indent(), "source:\n{source}")?;
        } else {
            writeln!(f, "{vis}: ")?;
        }
        writeln!(f, "kind: {kind}")?;
        write!(f, "module: {module}")
    }
}

impl Display for DefKind {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            DefKind::Undecided => write!(f, "undecided"),
            DefKind::Impl(id) => write!(f, "impl {id}"),
            DefKind::Use(id) => write!(f, "use (inside {id})"),
            DefKind::Type(kind) => write!(f, "{kind}"),
            DefKind::Value(kind) => write!(f, "{kind}"),
        }
    }
}

impl std::fmt::Display for ValueKind {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            ValueKind::Const(id) => write!(f, "const ({id})"),
            ValueKind::Static(id) => write!(f, "static ({id})"),
            ValueKind::Local(id) => write!(f, "let ({id})"),
            ValueKind::Fn(id) => write!(f, "fn def ({id})"),
        }
    }
}

impl Display for TypeKind {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            TypeKind::Alias(def) => match def {
                Some(def) => write!(f, "alias to #{def}"),
                None => f.write_str("type"),
            },
            TypeKind::Intrinsic(i) => i.fmt(f),
            TypeKind::Adt(a) => a.fmt(f),
            TypeKind::Ref(cnt, def) => {
                for _ in 0..*cnt {
                    f.write_str("&")?;
                }
                def.fmt(f)
            }
            TypeKind::Slice(def) => write!(f, "slice [#{def}]"),
            TypeKind::Array(def, size) => write!(f, "array [#{def}; {size}]"),
            TypeKind::Tuple(defs) => {
                let mut defs = defs.iter();
                sep("tuple (", ")", || {
                    let def = defs.next()?;
                    Some(move |f: &mut fmt::Formatter| write!(f, "#{def}"))
                })(f)
            }
            TypeKind::FnSig { args, rety } => write!(f, "fn (#{args}) -> #{rety}"),
            TypeKind::Empty => f.write_str("()"),
            TypeKind::Never => f.write_str("!"),
            TypeKind::SelfTy => f.write_str("Self"),
            TypeKind::Module => f.write_str("mod"),
        }
    }
}

impl Display for Adt {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Adt::Enum(variants) => {
                let mut variants = variants.iter();
                sep("enum {", "}", || {
                    let (name, def) = variants.next()?;
                    Some(move |f: &mut fmt::Formatter| match def {
                        Some(def) => write!(f, "{name}: #{def}"),
                        None => write!(f, "{name}"),
                    })
                })(f)
            }
            Adt::CLikeEnum(variants) => {
                let mut variants = variants.iter();
                sep("enum {", "}", || {
                    let (name, descrim) = variants.next()?;
                    Some(move |f: &mut fmt::Formatter| write!(f, "{name} = {descrim}"))
                })(f)
            }
            Adt::FieldlessEnum => write!(f, "enum"),
            Adt::Struct(members) => {
                let mut members = members.iter();
                sep("struct {", "}", || {
                    let (name, vis, def) = members.next()?;
                    Some(move |f: &mut fmt::Formatter| write!(f, "{vis}{name}: #{def}"))
                })(f)
            }
            Adt::TupleStruct(members) => {
                let mut members = members.iter();
                sep("struct (", ")", || {
                    let (vis, def) = members.next()?;
                    Some(move |f: &mut fmt::Formatter| write!(f, "{vis}#{def}"))
                })(f)
            }
            Adt::UnitStruct => write!(f, "struct ()"),
            Adt::Union(variants) => {
                let mut variants = variants.iter();
                sep("union {", "}", || {
                    let (name, def) = variants.next()?;
                    Some(move |f: &mut fmt::Formatter| write!(f, "{name}: #{def}"))
                })(f)
            }
        }
    }
}

impl Display for Intrinsic {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Intrinsic::I8 => f.write_str("i8"),
            Intrinsic::I16 => f.write_str("i16"),
            Intrinsic::I32 => f.write_str("i32"),
            Intrinsic::I64 => f.write_str("i64"),
            Intrinsic::Isize => f.write_str("isize"),
            Intrinsic::U8 => f.write_str("u8"),
            Intrinsic::U16 => f.write_str("u16"),
            Intrinsic::U32 => f.write_str("u32"),
            Intrinsic::U64 => f.write_str("u64"),
            Intrinsic::Usize => f.write_str("usize"),
            Intrinsic::Bool => f.write_str("bool"),
            Intrinsic::Char => f.write_str("char"),
        }
    }
}