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
use fmt::Formatter;
use std::collections::BTreeSet;
use std::fmt;

/// ReturnType/ParameterSet may have a set of parameter attributes.
/// 返り値,引数が持つAttributeの集合
#[derive(Eq, PartialEq, PartialOrd, Ord, Hash)]
pub struct ParameterAttributes {
    attrs: BTreeSet<ParameterAttribute>,
}

impl Default for ParameterAttributes {
    fn default() -> Self {
        Self {
            attrs: BTreeSet::new(),
        }
    }
}

impl ParameterAttributes {
    pub fn add_attr(&mut self, attr: ParameterAttribute) {
        self.attrs.insert(attr);
    }
}

impl fmt::Display for ParameterAttributes {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        let fmt_string = self
            .attrs
            .iter()
            .map(|attr| attr.to_string())
            .collect::<Vec<String>>()
            .join(", ");

        write!(f, "{}", fmt_string)
    }
}

/// WIP: all attributes aren't defined yet.
/// see [LLVM LangRef#parameter-attributes](https://llvm.org/docs/LangRef.html#parameter-attributes)
#[derive(Eq, PartialEq, PartialOrd, Ord, Hash)]
pub enum ParameterAttribute {
    /// the parameter or return value should be zero-extended.
    ZEROEXT,
    /// the parameter or return value should be sign-extended.
    SIGNEXT,
}

impl fmt::Display for ParameterAttribute {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        let attr_string = match self {
            Self::ZEROEXT => "zeroext",
            Self::SIGNEXT => "signext",
        };

        write!(f, "{}", attr_string)
    }
}