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
pub mod parser;

pub use parser::operand as parse_operand;
pub use parser::parse;

use crate::ir::{module::name::Name, value::ConstantInt};
use std::fmt;

#[derive(PartialEq, Clone)]
pub enum Metadata {
    String(String),
    Name(Name),
    Int(ConstantInt),
    Node(Vec<Self>),
}

// struct MetadataNode(Vec<Metadata>);

// Metadata Node

impl fmt::Debug for Metadata {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        fn fmt_int(i: &ConstantInt, f: &mut fmt::Formatter<'_>) -> fmt::Result {
            // TODO: Find another simpler way
            match i {
                ConstantInt::Int1(_) => write!(f, "i1 "),
                ConstantInt::Int8(_) => write!(f, "i8 "),
                ConstantInt::Int32(_) => write!(f, "i32 "),
                ConstantInt::Int64(_) => write!(f, "i64 "),
            }?;
            write!(f, "{}", i)
        }

        match self {
            Self::String(s) => write!(f, "!\"{}\"", s),
            Self::Name(n) => write!(f, "!{}", n),
            Self::Int(i) => fmt_int(i, f),
            Self::Node(list) => {
                write!(f, "!{{")?;
                for (k, m) in list.iter().enumerate() {
                    if k == 0 {
                        write!(f, "{:?}", m)?;
                    } else {
                        write!(f, ", {:?}", m)?;
                    }
                }
                write!(f, "}}")
            }
        }
    }
}