aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src/ty/primitive.rs
blob: 5741ca90d3763ad397ddb4dd018be7bb70714655 (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
use std::fmt;

use crate::{Name, KnownName};

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

impl UncertainIntTy {
    pub fn ty_to_string(&self) -> &'static str {
        match *self {
            UncertainIntTy::Unknown => "{integer}",
            UncertainIntTy::Signed(ty) => ty.ty_to_string(),
            UncertainIntTy::Unsigned(ty) => ty.ty_to_string(),
        }
    }

    pub fn from_name(name: &Name) -> Option<UncertainIntTy> {
        if let Some(ty) = IntTy::from_name(name) {
            Some(UncertainIntTy::Signed(ty))
        } else if let Some(ty) = UintTy::from_name(name) {
            Some(UncertainIntTy::Unsigned(ty))
        } else {
            None
        }
    }
}

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

impl UncertainFloatTy {
    pub fn ty_to_string(&self) -> &'static str {
        match *self {
            UncertainFloatTy::Unknown => "{float}",
            UncertainFloatTy::Known(ty) => ty.ty_to_string(),
        }
    }

    pub fn from_name(name: &Name) -> Option<UncertainFloatTy> {
        if let Some(ty) = FloatTy::from_name(name) {
            Some(UncertainFloatTy::Known(ty))
        } else {
            None
        }
    }
}

#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Copy)]
pub enum IntTy {
    Isize,
    I8,
    I16,
    I32,
    I64,
    I128,
}

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 ty_to_string(&self) -> &'static str {
        match *self {
            IntTy::Isize => "isize",
            IntTy::I8 => "i8",
            IntTy::I16 => "i16",
            IntTy::I32 => "i32",
            IntTy::I64 => "i64",
            IntTy::I128 => "i128",
        }
    }

    pub fn from_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),
            _ => None,
        }
    }
}

#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Copy)]
pub enum UintTy {
    Usize,
    U8,
    U16,
    U32,
    U64,
    U128,
}

impl UintTy {
    pub fn ty_to_string(&self) -> &'static str {
        match *self {
            UintTy::Usize => "usize",
            UintTy::U8 => "u8",
            UintTy::U16 => "u16",
            UintTy::U32 => "u32",
            UintTy::U64 => "u64",
            UintTy::U128 => "u128",
        }
    }

    pub fn from_name(name: &Name) -> Option<UintTy> {
        match name.as_known_name()? {
            KnownName::Usize => Some(UintTy::Usize),
            KnownName::U8 => Some(UintTy::U8),
            KnownName::U16 => Some(UintTy::U16),
            KnownName::U32 => Some(UintTy::U32),
            KnownName::U64 => Some(UintTy::U64),
            KnownName::U128 => Some(UintTy::U128),
            _ => None,
        }
    }
}

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

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

#[derive(Clone, PartialEq, Eq, Hash, Copy, PartialOrd, Ord)]
pub enum FloatTy {
    F32,
    F64,
}

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 ty_to_string(self) -> &'static str {
        match self {
            FloatTy::F32 => "f32",
            FloatTy::F64 => "f64",
        }
    }

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