diff options
author | Lukas Wirth <[email protected]> | 2021-02-28 00:20:04 +0000 |
---|---|---|
committer | Lukas Wirth <[email protected]> | 2021-02-28 00:20:04 +0000 |
commit | 5183c9f08345c664237ae138e86f96ff46714f15 (patch) | |
tree | a0e660cb49fd67951ee2209d9fc75a2108243df7 /crates/hir_ty/src/primitive.rs | |
parent | 2a4076c14d0e3f7ae03908c2b9cd1a52851d401c (diff) |
Introduce TypeCtor::Scalar
Diffstat (limited to 'crates/hir_ty/src/primitive.rs')
-rw-r--r-- | crates/hir_ty/src/primitive.rs | 157 |
1 files changed, 74 insertions, 83 deletions
diff --git a/crates/hir_ty/src/primitive.rs b/crates/hir_ty/src/primitive.rs index 37966b709..e727c9581 100644 --- a/crates/hir_ty/src/primitive.rs +++ b/crates/hir_ty/src/primitive.rs | |||
@@ -5,18 +5,28 @@ | |||
5 | 5 | ||
6 | use std::fmt; | 6 | use std::fmt; |
7 | 7 | ||
8 | pub use hir_def::builtin_type::{BuiltinFloat, BuiltinInt, FloatBitness, IntBitness, Signedness}; | 8 | pub use hir_def::builtin_type::{BuiltinFloat, BuiltinInt, BuiltinUint}; |
9 | 9 | ||
10 | #[derive(Copy, Clone, Eq, PartialEq, Hash)] | 10 | /// Different signed int types. |
11 | pub struct IntTy { | 11 | #[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] |
12 | pub signedness: Signedness, | 12 | pub enum IntTy { |
13 | pub bitness: IntBitness, | 13 | Isize, |
14 | I8, | ||
15 | I16, | ||
16 | I32, | ||
17 | I64, | ||
18 | I128, | ||
14 | } | 19 | } |
15 | 20 | ||
16 | impl fmt::Debug for IntTy { | 21 | /// Different unsigned int types. |
17 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | 22 | #[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] |
18 | fmt::Display::fmt(self, f) | 23 | pub enum UintTy { |
19 | } | 24 | Usize, |
25 | U8, | ||
26 | U16, | ||
27 | U32, | ||
28 | U64, | ||
29 | U128, | ||
20 | } | 30 | } |
21 | 31 | ||
22 | impl fmt::Display for IntTy { | 32 | impl fmt::Display for IntTy { |
@@ -26,75 +36,41 @@ impl fmt::Display for IntTy { | |||
26 | } | 36 | } |
27 | 37 | ||
28 | impl IntTy { | 38 | impl IntTy { |
29 | pub fn isize() -> IntTy { | 39 | pub fn ty_to_string(self) -> &'static str { |
30 | IntTy { signedness: Signedness::Signed, bitness: IntBitness::Xsize } | 40 | match self { |
31 | } | 41 | IntTy::Isize => "isize", |
32 | 42 | IntTy::I8 => "i8", | |
33 | pub fn i8() -> IntTy { | 43 | IntTy::I16 => "i16", |
34 | IntTy { signedness: Signedness::Signed, bitness: IntBitness::X8 } | 44 | IntTy::I32 => "i32", |
35 | } | 45 | IntTy::I64 => "i64", |
36 | 46 | IntTy::I128 => "i128", | |
37 | pub fn i16() -> IntTy { | 47 | } |
38 | IntTy { signedness: Signedness::Signed, bitness: IntBitness::X16 } | ||
39 | } | ||
40 | |||
41 | pub fn i32() -> IntTy { | ||
42 | IntTy { signedness: Signedness::Signed, bitness: IntBitness::X32 } | ||
43 | } | ||
44 | |||
45 | pub fn i64() -> IntTy { | ||
46 | IntTy { signedness: Signedness::Signed, bitness: IntBitness::X64 } | ||
47 | } | ||
48 | |||
49 | pub fn i128() -> IntTy { | ||
50 | IntTy { signedness: Signedness::Signed, bitness: IntBitness::X128 } | ||
51 | } | ||
52 | |||
53 | pub fn usize() -> IntTy { | ||
54 | IntTy { signedness: Signedness::Unsigned, bitness: IntBitness::Xsize } | ||
55 | } | ||
56 | |||
57 | pub fn u8() -> IntTy { | ||
58 | IntTy { signedness: Signedness::Unsigned, bitness: IntBitness::X8 } | ||
59 | } | ||
60 | |||
61 | pub fn u16() -> IntTy { | ||
62 | IntTy { signedness: Signedness::Unsigned, bitness: IntBitness::X16 } | ||
63 | } | ||
64 | |||
65 | pub fn u32() -> IntTy { | ||
66 | IntTy { signedness: Signedness::Unsigned, bitness: IntBitness::X32 } | ||
67 | } | ||
68 | |||
69 | pub fn u64() -> IntTy { | ||
70 | IntTy { signedness: Signedness::Unsigned, bitness: IntBitness::X64 } | ||
71 | } | 48 | } |
49 | } | ||
72 | 50 | ||
73 | pub fn u128() -> IntTy { | 51 | impl fmt::Display for UintTy { |
74 | IntTy { signedness: Signedness::Unsigned, bitness: IntBitness::X128 } | 52 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
53 | write!(f, "{}", self.ty_to_string()) | ||
75 | } | 54 | } |
55 | } | ||
76 | 56 | ||
57 | impl UintTy { | ||
77 | pub fn ty_to_string(self) -> &'static str { | 58 | pub fn ty_to_string(self) -> &'static str { |
78 | match (self.signedness, self.bitness) { | 59 | match self { |
79 | (Signedness::Signed, IntBitness::Xsize) => "isize", | 60 | UintTy::Usize => "usize", |
80 | (Signedness::Signed, IntBitness::X8) => "i8", | 61 | UintTy::U8 => "u8", |
81 | (Signedness::Signed, IntBitness::X16) => "i16", | 62 | UintTy::U16 => "u16", |
82 | (Signedness::Signed, IntBitness::X32) => "i32", | 63 | UintTy::U32 => "u32", |
83 | (Signedness::Signed, IntBitness::X64) => "i64", | 64 | UintTy::U64 => "u64", |
84 | (Signedness::Signed, IntBitness::X128) => "i128", | 65 | UintTy::U128 => "u128", |
85 | (Signedness::Unsigned, IntBitness::Xsize) => "usize", | ||
86 | (Signedness::Unsigned, IntBitness::X8) => "u8", | ||
87 | (Signedness::Unsigned, IntBitness::X16) => "u16", | ||
88 | (Signedness::Unsigned, IntBitness::X32) => "u32", | ||
89 | (Signedness::Unsigned, IntBitness::X64) => "u64", | ||
90 | (Signedness::Unsigned, IntBitness::X128) => "u128", | ||
91 | } | 66 | } |
92 | } | 67 | } |
93 | } | 68 | } |
94 | 69 | ||
95 | #[derive(Copy, Clone, PartialEq, Eq, Hash)] | 70 | #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] |
96 | pub struct FloatTy { | 71 | pub enum FloatTy { |
97 | pub bitness: FloatBitness, | 72 | F32, |
73 | F64, | ||
98 | } | 74 | } |
99 | 75 | ||
100 | impl fmt::Debug for FloatTy { | 76 | impl fmt::Debug for FloatTy { |
@@ -110,30 +86,45 @@ impl fmt::Display for FloatTy { | |||
110 | } | 86 | } |
111 | 87 | ||
112 | impl FloatTy { | 88 | impl FloatTy { |
113 | pub fn f32() -> FloatTy { | ||
114 | FloatTy { bitness: FloatBitness::X32 } | ||
115 | } | ||
116 | |||
117 | pub fn f64() -> FloatTy { | ||
118 | FloatTy { bitness: FloatBitness::X64 } | ||
119 | } | ||
120 | |||
121 | pub fn ty_to_string(self) -> &'static str { | 89 | pub fn ty_to_string(self) -> &'static str { |
122 | match self.bitness { | 90 | match self { |
123 | FloatBitness::X32 => "f32", | 91 | FloatTy::F32 => "f32", |
124 | FloatBitness::X64 => "f64", | 92 | FloatTy::F64 => "f64", |
125 | } | 93 | } |
126 | } | 94 | } |
127 | } | 95 | } |
128 | 96 | ||
129 | impl From<BuiltinInt> for IntTy { | 97 | impl From<BuiltinInt> for IntTy { |
130 | fn from(t: BuiltinInt) -> Self { | 98 | fn from(t: BuiltinInt) -> Self { |
131 | IntTy { signedness: t.signedness, bitness: t.bitness } | 99 | match t { |
100 | BuiltinInt::Isize => Self::Isize, | ||
101 | BuiltinInt::I8 => Self::I8, | ||
102 | BuiltinInt::I16 => Self::I16, | ||
103 | BuiltinInt::I32 => Self::I32, | ||
104 | BuiltinInt::I64 => Self::I64, | ||
105 | BuiltinInt::I128 => Self::I128, | ||
106 | } | ||
107 | } | ||
108 | } | ||
109 | |||
110 | impl From<BuiltinUint> for UintTy { | ||
111 | fn from(t: BuiltinUint) -> Self { | ||
112 | match t { | ||
113 | BuiltinUint::Usize => Self::Usize, | ||
114 | BuiltinUint::U8 => Self::U8, | ||
115 | BuiltinUint::U16 => Self::U16, | ||
116 | BuiltinUint::U32 => Self::U32, | ||
117 | BuiltinUint::U64 => Self::U64, | ||
118 | BuiltinUint::U128 => Self::U128, | ||
119 | } | ||
132 | } | 120 | } |
133 | } | 121 | } |
134 | 122 | ||
135 | impl From<BuiltinFloat> for FloatTy { | 123 | impl From<BuiltinFloat> for FloatTy { |
136 | fn from(t: BuiltinFloat) -> Self { | 124 | fn from(t: BuiltinFloat) -> Self { |
137 | FloatTy { bitness: t.bitness } | 125 | match t { |
126 | BuiltinFloat::F32 => Self::F32, | ||
127 | BuiltinFloat::F64 => Self::F64, | ||
128 | } | ||
138 | } | 129 | } |
139 | } | 130 | } |