llvm_scratch/core/function/
param_attrs.rs

1use fmt::Formatter;
2use std::collections::BTreeSet;
3use std::fmt;
4
5/// ReturnType/ParameterSet may have a set of parameter attributes.
6/// 返り値,引数が持つAttributeの集合
7#[derive(Eq, PartialEq, PartialOrd, Ord, Hash)]
8pub struct ParameterAttributes {
9    attrs: BTreeSet<ParameterAttribute>,
10}
11
12impl Default for ParameterAttributes {
13    fn default() -> Self {
14        Self {
15            attrs: BTreeSet::new(),
16        }
17    }
18}
19
20impl ParameterAttributes {
21    pub fn add_attr(&mut self, attr: ParameterAttribute) {
22        self.attrs.insert(attr);
23    }
24}
25
26impl fmt::Display for ParameterAttributes {
27    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
28        let fmt_string = self
29            .attrs
30            .iter()
31            .map(|attr| attr.to_string())
32            .collect::<Vec<String>>()
33            .join(", ");
34
35        write!(f, "{}", fmt_string)
36    }
37}
38
39/// WIP: all attributes aren't defined yet.
40/// see [LLVM LangRef#parameter-attributes](https://llvm.org/docs/LangRef.html#parameter-attributes)
41#[derive(Eq, PartialEq, PartialOrd, Ord, Hash)]
42pub enum ParameterAttribute {
43    /// the parameter or return value should be zero-extended.
44    ZEROEXT,
45    /// the parameter or return value should be sign-extended.
46    SIGNEXT,
47}
48
49impl fmt::Display for ParameterAttribute {
50    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
51        let attr_string = match self {
52            Self::ZEROEXT => "zeroext",
53            Self::SIGNEXT => "signext",
54        };
55
56        write!(f, "{}", attr_string)
57    }
58}