aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src/ty/primitive.rs
blob: b37326db7b36c44a4d5f43e047b873e0f79e72c8 (plain)
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
use std::fmt;

use crate::{Name, KnownName};

#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
pub enum Signedness {
    Signed,
    Unsigned,
}

#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
pub enum IntBitness {
    Xsize,
    X8,
    X16,
    X32,
    X64,
    X128,
}

#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
pub enum FloatBitness {
    X32,
    X64,
}

#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
pub enum UncertainIntTy {
    Unknown,
    Known(IntTy),
}

impl fmt::Display for UncertainIntTy {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            UncertainIntTy::Unknown => write!(f, "{{integer}}"),
            UncertainIntTy::Known(ty) => write!(f, "{}", ty),
        }
    }
}

#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
pub enum UncertainFloatTy {
    Unknown,
    Known(FloatTy),
}

impl fmt::Display for UncertainFloatTy {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            UncertainFloatTy::Unknown => write!(f, "{{float}}"),
            UncertainFloatTy::Known(ty) => write!(f, "{}", ty),
        }
    }
}

#[derive(Copy, Clone, Eq, PartialEq, Hash)]
pub struct IntTy {
    pub signedness: Signedness,
    pub bitness: IntBitness,
}

impl fmt::Debug for IntTy {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        fmt::Display::fmt(self, f)
    }
}

impl fmt::Display for IntTy {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", self.ty_to_string())
    }
}

impl IntTy {
    pub fn isize() -> IntTy {
        IntTy { signedness: Signedness::Signed, bitness: IntBitness::Xsize }
    }

    pub fn i8() -> IntTy {
        IntTy { signedness: Signedness::Signed, bitness: IntBitness::X8 }
    }

    pub fn i16() -> IntTy {
        IntTy { signedness: Signedness::Signed, bitness: IntBitness::X16 }
    }

    pub fn i32() -> IntTy {
        IntTy { signedness: Signedness::Signed, bitness: IntBitness::X32 }
    }

    pub fn i64() -> IntTy {
        IntTy { signedness: Signedness::Signed, bitness: IntBitness::X64 }
    }

    pub fn i128() -> IntTy {
        IntTy { signedness: Signedness::Signed, bitness: IntBitness::X128 }
    }

    pub fn usize() -> IntTy {
        IntTy { signedness: Signedness::Unsigned, bitness: IntBitness::Xsize }
    }

    pub fn u8() -> IntTy {
        IntTy { signedness: Signedness::Unsigned, bitness: IntBitness::X8 }
    }

    pub fn u16() -> IntTy {
        IntTy { signedness: Signedness::Unsigned, bitness: IntBitness::X16 }
    }

    pub fn u32() -> IntTy {
        IntTy { signedness: Signedness::Unsigned, bitness: IntBitness::X32 }
    }

    pub fn u64() -> IntTy {
        IntTy { signedness: Signedness::Unsigned, bitness: IntBitness::X64 }
    }

    pub fn u128() -> IntTy {
        IntTy { signedness: Signedness::Unsigned, bitness: IntBitness::X128 }
    }

    pub(crate) fn ty_to_string(&self) -> &'static str {
        match (self.signedness, self.bitness) {
            (Signedness::Signed, IntBitness::Xsize) => "isize",
            (Signedness::Signed, IntBitness::X8) => "i8",
            (Signedness::Signed, IntBitness::X16) => "i16",
            (Signedness::Signed, IntBitness::X32) => "i32",
            (Signedness::Signed, IntBitness::X64) => "i64",
            (Signedness::Signed, IntBitness::X128) => "i128",
            (Signedness::Unsigned, IntBitness::Xsize) => "usize",
            (Signedness::Unsigned, IntBitness::X8) => "u8",
            (Signedness::Unsigned, IntBitness::X16) => "u16",
            (Signedness::Unsigned, IntBitness::X32) => "u32",
            (Signedness::Unsigned, IntBitness::X64) => "u64",
            (Signedness::Unsigned, IntBitness::X128) => "u128",
        }
    }

    pub(crate) fn from_type_name(name: &Name) -> Option<IntTy> {
        match name.as_known_name()? {
            KnownName::Isize => Some(IntTy::isize()),
            KnownName::I8 => Some(IntTy::i8()),
            KnownName::I16 => Some(IntTy::i16()),
            KnownName::I32 => Some(IntTy::i32()),
            KnownName::I64 => Some(IntTy::i64()),
            KnownName::I128 => Some(IntTy::i128()),
            KnownName::Usize => Some(IntTy::usize()),
            KnownName::U8 => Some(IntTy::u8()),
            KnownName::U16 => Some(IntTy::u16()),
            KnownName::U32 => Some(IntTy::u32()),
            KnownName::U64 => Some(IntTy::u64()),
            KnownName::U128 => Some(IntTy::u128()),
            _ => None,
        }
    }

    pub(crate) fn from_suffix(suffix: &str) -> Option<IntTy> {
        match suffix {
            "isize" => Some(IntTy::isize()),
            "i8" => Some(IntTy::i8()),
            "i16" => Some(IntTy::i16()),
            "i32" => Some(IntTy::i32()),
            "i64" => Some(IntTy::i64()),
            "i128" => Some(IntTy::i128()),
            "usize" => Some(IntTy::usize()),
            "u8" => Some(IntTy::u8()),
            "u16" => Some(IntTy::u16()),
            "u32" => Some(IntTy::u32()),
            "u64" => Some(IntTy::u64()),
            "u128" => Some(IntTy::u128()),
            _ => None,
        }
    }
}

#[derive(Copy, Clone, PartialEq, Eq, Hash)]
pub struct FloatTy {
    pub bitness: FloatBitness,
}

impl fmt::Debug for FloatTy {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        fmt::Display::fmt(self, f)
    }
}

impl fmt::Display for FloatTy {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", self.ty_to_string())
    }
}

impl FloatTy {
    pub fn f32() -> FloatTy {
        FloatTy { bitness: FloatBitness::X32 }
    }

    pub fn f64() -> FloatTy {
        FloatTy { bitness: FloatBitness::X64 }
    }

    pub(crate) fn ty_to_string(self) -> &'static str {
        match self.bitness {
            FloatBitness::X32 => "f32",
            FloatBitness::X64 => "f64",
        }
    }

    pub(crate) fn from_type_name(name: &Name) -> Option<FloatTy> {
        match name.as_known_name()? {
            KnownName::F32 => Some(FloatTy::f32()),
            KnownName::F64 => Some(FloatTy::f64()),
            _ => None,
        }
    }

    pub(crate) fn from_suffix(suffix: &str) -> Option<FloatTy> {
        match suffix {
            "f32" => Some(FloatTy::f32()),
            "f64" => Some(FloatTy::f64()),
            _ => None,
        }
    }
}