diff options
Diffstat (limited to 'crates/ra_hir_ty')
-rw-r--r-- | crates/ra_hir_ty/Cargo.toml | 32 | ||||
-rw-r--r-- | crates/ra_hir_ty/src/lib.rs | 4 | ||||
-rw-r--r-- | crates/ra_hir_ty/src/primitive.rs | 193 |
3 files changed, 229 insertions, 0 deletions
diff --git a/crates/ra_hir_ty/Cargo.toml b/crates/ra_hir_ty/Cargo.toml new file mode 100644 index 000000000..70216ab24 --- /dev/null +++ b/crates/ra_hir_ty/Cargo.toml | |||
@@ -0,0 +1,32 @@ | |||
1 | [package] | ||
2 | edition = "2018" | ||
3 | name = "ra_hir_ty" | ||
4 | version = "0.1.0" | ||
5 | authors = ["rust-analyzer developers"] | ||
6 | |||
7 | [lib] | ||
8 | doctest = false | ||
9 | |||
10 | [dependencies] | ||
11 | log = "0.4.5" | ||
12 | rustc-hash = "1.0" | ||
13 | parking_lot = "0.9.0" | ||
14 | ena = "0.13" | ||
15 | |||
16 | ra_syntax = { path = "../ra_syntax" } | ||
17 | ra_arena = { path = "../ra_arena" } | ||
18 | ra_db = { path = "../ra_db" } | ||
19 | hir_expand = { path = "../ra_hir_expand", package = "ra_hir_expand" } | ||
20 | hir_def = { path = "../ra_hir_def", package = "ra_hir_def" } | ||
21 | test_utils = { path = "../test_utils" } | ||
22 | ra_prof = { path = "../ra_prof" } | ||
23 | |||
24 | # https://github.com/rust-lang/chalk/pull/294 | ||
25 | chalk-solve = { git = "https://github.com/jackh726/chalk.git", rev = "095cd38a4f16337913bba487f2055b9ca0179f30" } | ||
26 | chalk-rust-ir = { git = "https://github.com/jackh726/chalk.git", rev = "095cd38a4f16337913bba487f2055b9ca0179f30" } | ||
27 | chalk-ir = { git = "https://github.com/jackh726/chalk.git", rev = "095cd38a4f16337913bba487f2055b9ca0179f30" } | ||
28 | |||
29 | lalrpop-intern = "0.15.1" | ||
30 | |||
31 | [dev-dependencies] | ||
32 | insta = "0.12.0" | ||
diff --git a/crates/ra_hir_ty/src/lib.rs b/crates/ra_hir_ty/src/lib.rs new file mode 100644 index 000000000..28859ba63 --- /dev/null +++ b/crates/ra_hir_ty/src/lib.rs | |||
@@ -0,0 +1,4 @@ | |||
1 | //! Work in Progress: everything related to types, type inference and trait | ||
2 | //! solving. | ||
3 | |||
4 | pub mod primitive; | ||
diff --git a/crates/ra_hir_ty/src/primitive.rs b/crates/ra_hir_ty/src/primitive.rs new file mode 100644 index 000000000..02a8179d9 --- /dev/null +++ b/crates/ra_hir_ty/src/primitive.rs | |||
@@ -0,0 +1,193 @@ | |||
1 | //! Defines primitive types, which have a couple of peculiarities: | ||
2 | //! | ||
3 | //! * during type inference, they can be uncertain (ie, `let x = 92;`) | ||
4 | //! * they don't belong to any particular crate. | ||
5 | |||
6 | use std::fmt; | ||
7 | |||
8 | pub use hir_def::builtin_type::{BuiltinFloat, BuiltinInt, FloatBitness, IntBitness, Signedness}; | ||
9 | |||
10 | #[derive(Clone, Copy, Eq, PartialEq, Hash, Debug)] | ||
11 | pub enum Uncertain<T> { | ||
12 | Unknown, | ||
13 | Known(T), | ||
14 | } | ||
15 | |||
16 | impl From<IntTy> for Uncertain<IntTy> { | ||
17 | fn from(ty: IntTy) -> Self { | ||
18 | Uncertain::Known(ty) | ||
19 | } | ||
20 | } | ||
21 | |||
22 | impl fmt::Display for Uncertain<IntTy> { | ||
23 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
24 | match *self { | ||
25 | Uncertain::Unknown => write!(f, "{{integer}}"), | ||
26 | Uncertain::Known(ty) => write!(f, "{}", ty), | ||
27 | } | ||
28 | } | ||
29 | } | ||
30 | |||
31 | impl From<FloatTy> for Uncertain<FloatTy> { | ||
32 | fn from(ty: FloatTy) -> Self { | ||
33 | Uncertain::Known(ty) | ||
34 | } | ||
35 | } | ||
36 | |||
37 | impl fmt::Display for Uncertain<FloatTy> { | ||
38 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
39 | match *self { | ||
40 | Uncertain::Unknown => write!(f, "{{float}}"), | ||
41 | Uncertain::Known(ty) => write!(f, "{}", ty), | ||
42 | } | ||
43 | } | ||
44 | } | ||
45 | |||
46 | #[derive(Copy, Clone, Eq, PartialEq, Hash)] | ||
47 | pub struct IntTy { | ||
48 | pub signedness: Signedness, | ||
49 | pub bitness: IntBitness, | ||
50 | } | ||
51 | |||
52 | impl fmt::Debug for IntTy { | ||
53 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
54 | fmt::Display::fmt(self, f) | ||
55 | } | ||
56 | } | ||
57 | |||
58 | impl fmt::Display for IntTy { | ||
59 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
60 | write!(f, "{}", self.ty_to_string()) | ||
61 | } | ||
62 | } | ||
63 | |||
64 | impl IntTy { | ||
65 | pub fn isize() -> IntTy { | ||
66 | IntTy { signedness: Signedness::Signed, bitness: IntBitness::Xsize } | ||
67 | } | ||
68 | |||
69 | pub fn i8() -> IntTy { | ||
70 | IntTy { signedness: Signedness::Signed, bitness: IntBitness::X8 } | ||
71 | } | ||
72 | |||
73 | pub fn i16() -> IntTy { | ||
74 | IntTy { signedness: Signedness::Signed, bitness: IntBitness::X16 } | ||
75 | } | ||
76 | |||
77 | pub fn i32() -> IntTy { | ||
78 | IntTy { signedness: Signedness::Signed, bitness: IntBitness::X32 } | ||
79 | } | ||
80 | |||
81 | pub fn i64() -> IntTy { | ||
82 | IntTy { signedness: Signedness::Signed, bitness: IntBitness::X64 } | ||
83 | } | ||
84 | |||
85 | pub fn i128() -> IntTy { | ||
86 | IntTy { signedness: Signedness::Signed, bitness: IntBitness::X128 } | ||
87 | } | ||
88 | |||
89 | pub fn usize() -> IntTy { | ||
90 | IntTy { signedness: Signedness::Unsigned, bitness: IntBitness::Xsize } | ||
91 | } | ||
92 | |||
93 | pub fn u8() -> IntTy { | ||
94 | IntTy { signedness: Signedness::Unsigned, bitness: IntBitness::X8 } | ||
95 | } | ||
96 | |||
97 | pub fn u16() -> IntTy { | ||
98 | IntTy { signedness: Signedness::Unsigned, bitness: IntBitness::X16 } | ||
99 | } | ||
100 | |||
101 | pub fn u32() -> IntTy { | ||
102 | IntTy { signedness: Signedness::Unsigned, bitness: IntBitness::X32 } | ||
103 | } | ||
104 | |||
105 | pub fn u64() -> IntTy { | ||
106 | IntTy { signedness: Signedness::Unsigned, bitness: IntBitness::X64 } | ||
107 | } | ||
108 | |||
109 | pub fn u128() -> IntTy { | ||
110 | IntTy { signedness: Signedness::Unsigned, bitness: IntBitness::X128 } | ||
111 | } | ||
112 | |||
113 | pub fn ty_to_string(self) -> &'static str { | ||
114 | match (self.signedness, self.bitness) { | ||
115 | (Signedness::Signed, IntBitness::Xsize) => "isize", | ||
116 | (Signedness::Signed, IntBitness::X8) => "i8", | ||
117 | (Signedness::Signed, IntBitness::X16) => "i16", | ||
118 | (Signedness::Signed, IntBitness::X32) => "i32", | ||
119 | (Signedness::Signed, IntBitness::X64) => "i64", | ||
120 | (Signedness::Signed, IntBitness::X128) => "i128", | ||
121 | (Signedness::Unsigned, IntBitness::Xsize) => "usize", | ||
122 | (Signedness::Unsigned, IntBitness::X8) => "u8", | ||
123 | (Signedness::Unsigned, IntBitness::X16) => "u16", | ||
124 | (Signedness::Unsigned, IntBitness::X32) => "u32", | ||
125 | (Signedness::Unsigned, IntBitness::X64) => "u64", | ||
126 | (Signedness::Unsigned, IntBitness::X128) => "u128", | ||
127 | } | ||
128 | } | ||
129 | } | ||
130 | |||
131 | #[derive(Copy, Clone, PartialEq, Eq, Hash)] | ||
132 | pub struct FloatTy { | ||
133 | pub bitness: FloatBitness, | ||
134 | } | ||
135 | |||
136 | impl fmt::Debug for FloatTy { | ||
137 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
138 | fmt::Display::fmt(self, f) | ||
139 | } | ||
140 | } | ||
141 | |||
142 | impl fmt::Display for FloatTy { | ||
143 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
144 | write!(f, "{}", self.ty_to_string()) | ||
145 | } | ||
146 | } | ||
147 | |||
148 | impl FloatTy { | ||
149 | pub fn f32() -> FloatTy { | ||
150 | FloatTy { bitness: FloatBitness::X32 } | ||
151 | } | ||
152 | |||
153 | pub fn f64() -> FloatTy { | ||
154 | FloatTy { bitness: FloatBitness::X64 } | ||
155 | } | ||
156 | |||
157 | pub fn ty_to_string(self) -> &'static str { | ||
158 | match self.bitness { | ||
159 | FloatBitness::X32 => "f32", | ||
160 | FloatBitness::X64 => "f64", | ||
161 | } | ||
162 | } | ||
163 | } | ||
164 | |||
165 | impl From<BuiltinInt> for IntTy { | ||
166 | fn from(t: BuiltinInt) -> Self { | ||
167 | IntTy { signedness: t.signedness, bitness: t.bitness } | ||
168 | } | ||
169 | } | ||
170 | |||
171 | impl From<BuiltinFloat> for FloatTy { | ||
172 | fn from(t: BuiltinFloat) -> Self { | ||
173 | FloatTy { bitness: t.bitness } | ||
174 | } | ||
175 | } | ||
176 | |||
177 | impl From<Option<BuiltinInt>> for Uncertain<IntTy> { | ||
178 | fn from(t: Option<BuiltinInt>) -> Self { | ||
179 | match t { | ||
180 | None => Uncertain::Unknown, | ||
181 | Some(t) => Uncertain::Known(t.into()), | ||
182 | } | ||
183 | } | ||
184 | } | ||
185 | |||
186 | impl From<Option<BuiltinFloat>> for Uncertain<FloatTy> { | ||
187 | fn from(t: Option<BuiltinFloat>) -> Self { | ||
188 | match t { | ||
189 | None => Uncertain::Unknown, | ||
190 | Some(t) => Uncertain::Known(t.into()), | ||
191 | } | ||
192 | } | ||
193 | } | ||