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
use super::TypeLike;
use crate::{ir::Type, Context, Error};
use mlir_sys::{
    mlirIntegerTypeGet, mlirIntegerTypeGetWidth, mlirIntegerTypeIsSigned,
    mlirIntegerTypeIsSignless, mlirIntegerTypeIsUnsigned, mlirIntegerTypeSignedGet,
    mlirIntegerTypeUnsignedGet, MlirType,
};

/// A integer type.
#[derive(Clone, Copy, Debug)]
pub struct IntegerType<'c> {
    r#type: Type<'c>,
}

impl<'c> IntegerType<'c> {
    /// Creates an integer type.
    pub fn new(context: &'c Context, bits: u32) -> Self {
        Self {
            r#type: unsafe { Type::from_raw(mlirIntegerTypeGet(context.to_raw(), bits)) },
        }
    }

    /// Creates a signed integer type.
    pub fn signed(context: &'c Context, bits: u32) -> Self {
        unsafe { Self::from_raw(mlirIntegerTypeSignedGet(context.to_raw(), bits)) }
    }

    /// Creates an unsigned integer type.
    pub fn unsigned(context: &'c Context, bits: u32) -> Self {
        unsafe { Self::from_raw(mlirIntegerTypeUnsignedGet(context.to_raw(), bits)) }
    }

    /// Returns a bit width.
    pub fn width(&self) -> u32 {
        unsafe { mlirIntegerTypeGetWidth(self.to_raw()) }
    }

    /// Checks if an integer type is signed.
    pub fn is_signed(&self) -> bool {
        unsafe { mlirIntegerTypeIsSigned(self.to_raw()) }
    }

    /// Checks if an integer type is signless.
    pub fn is_signless(&self) -> bool {
        unsafe { mlirIntegerTypeIsSignless(self.to_raw()) }
    }

    /// Checks if an integer type is unsigned.
    pub fn is_unsigned(&self) -> bool {
        unsafe { mlirIntegerTypeIsUnsigned(self.to_raw()) }
    }
}

type_traits!(IntegerType, is_integer, "integer");

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn new() {
        assert!(IntegerType::new(&Context::new(), 64).is_integer());
    }

    #[test]
    fn signed() {
        assert!(IntegerType::signed(&Context::new(), 64).is_integer());
    }

    #[test]
    fn unsigned() {
        assert!(IntegerType::unsigned(&Context::new(), 64).is_integer());
    }

    #[test]
    fn signed_integer() {
        let context = Context::new();

        assert_eq!(
            Type::from(IntegerType::signed(&context, 42)),
            Type::parse(&context, "si42").unwrap()
        );
    }

    #[test]
    fn unsigned_integer() {
        let context = Context::new();

        assert_eq!(
            Type::from(IntegerType::unsigned(&context, 42)),
            Type::parse(&context, "ui42").unwrap()
        );
    }

    #[test]
    fn get_width() {
        let context = Context::new();

        assert_eq!(IntegerType::new(&context, 64).width(), 64);
    }

    #[test]
    fn check_sign() {
        let context = Context::new();
        let signless = IntegerType::new(&context, 42);
        let signed = IntegerType::signed(&context, 42);
        let unsigned = IntegerType::unsigned(&context, 42);

        assert!(signless.is_signless());
        assert!(!signed.is_signless());
        assert!(!unsigned.is_signless());

        assert!(!signless.is_signed());
        assert!(signed.is_signed());
        assert!(!unsigned.is_signed());

        assert!(!signless.is_unsigned());
        assert!(!signed.is_unsigned());
        assert!(unsigned.is_unsigned());
    }
}