aboutsummaryrefslogtreecommitdiff
path: root/crates/hir_ty/src/primitive.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/hir_ty/src/primitive.rs')
-rw-r--r--crates/hir_ty/src/primitive.rs157
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
6use std::fmt; 6use std::fmt;
7 7
8pub use hir_def::builtin_type::{BuiltinFloat, BuiltinInt, FloatBitness, IntBitness, Signedness}; 8pub use hir_def::builtin_type::{BuiltinFloat, BuiltinInt, BuiltinUint};
9 9
10#[derive(Copy, Clone, Eq, PartialEq, Hash)] 10/// Different signed int types.
11pub struct IntTy { 11#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
12 pub signedness: Signedness, 12pub enum IntTy {
13 pub bitness: IntBitness, 13 Isize,
14 I8,
15 I16,
16 I32,
17 I64,
18 I128,
14} 19}
15 20
16impl 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) 23pub enum UintTy {
19 } 24 Usize,
25 U8,
26 U16,
27 U32,
28 U64,
29 U128,
20} 30}
21 31
22impl fmt::Display for IntTy { 32impl fmt::Display for IntTy {
@@ -26,75 +36,41 @@ impl fmt::Display for IntTy {
26} 36}
27 37
28impl IntTy { 38impl 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 { 51impl 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
57impl 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)]
96pub struct FloatTy { 71pub enum FloatTy {
97 pub bitness: FloatBitness, 72 F32,
73 F64,
98} 74}
99 75
100impl fmt::Debug for FloatTy { 76impl fmt::Debug for FloatTy {
@@ -110,30 +86,45 @@ impl fmt::Display for FloatTy {
110} 86}
111 87
112impl FloatTy { 88impl 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
129impl From<BuiltinInt> for IntTy { 97impl 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
110impl 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
135impl From<BuiltinFloat> for FloatTy { 123impl 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}