aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src
diff options
context:
space:
mode:
authoryanchith <[email protected]>2019-03-22 09:09:35 +0000
committeryanchith <[email protected]>2019-03-22 09:09:35 +0000
commit504c8c7c2e371f2adccbc292ad09af2b31e05787 (patch)
treee8682a3f3a5017932dd31decc979daca7d9a2daa /crates/ra_hir/src
parent2a6544f906818263e2791bc4cdf4fcbdf7260ab9 (diff)
Refactor primitive types into more orthogonal representation
Diffstat (limited to 'crates/ra_hir/src')
-rw-r--r--crates/ra_hir/src/expr.rs10
-rw-r--r--crates/ra_hir/src/ty/infer.rs15
-rw-r--r--crates/ra_hir/src/ty/lower.rs8
-rw-r--r--crates/ra_hir/src/ty/primitive.rs266
4 files changed, 155 insertions, 144 deletions
diff --git a/crates/ra_hir/src/expr.rs b/crates/ra_hir/src/expr.rs
index 486314cc5..0fadab560 100644
--- a/crates/ra_hir/src/expr.rs
+++ b/crates/ra_hir/src/expr.rs
@@ -14,7 +14,7 @@ use crate::{
14 name::AsName, 14 name::AsName,
15 type_ref::{Mutability, TypeRef}, 15 type_ref::{Mutability, TypeRef},
16}; 16};
17use crate::{ path::GenericArgs, ty::primitive::{UintTy, UncertainIntTy, UncertainFloatTy}}; 17use crate::{ path::GenericArgs, ty::primitive::{IntTy, UncertainIntTy, FloatTy, UncertainFloatTy}};
18 18
19pub use self::scope::{ExprScopes, ScopesWithSourceMap, ScopeEntryWithSyntax}; 19pub use self::scope::{ExprScopes, ScopesWithSourceMap, ScopeEntryWithSyntax};
20 20
@@ -723,7 +723,8 @@ impl ExprCollector {
723 723
724 let lit = match child.flavor() { 724 let lit = match child.flavor() {
725 LiteralFlavor::IntNumber { suffix } => { 725 LiteralFlavor::IntNumber { suffix } => {
726 let known_name = suffix.and_then(|it| UncertainIntTy::from_suffix(&it)); 726 let known_name = suffix
727 .and_then(|it| IntTy::from_suffix(&it).map(UncertainIntTy::Known));
727 728
728 Literal::Int( 729 Literal::Int(
729 Default::default(), 730 Default::default(),
@@ -731,7 +732,8 @@ impl ExprCollector {
731 ) 732 )
732 } 733 }
733 LiteralFlavor::FloatNumber { suffix } => { 734 LiteralFlavor::FloatNumber { suffix } => {
734 let known_name = suffix.and_then(|it| UncertainFloatTy::from_suffix(&it)); 735 let known_name = suffix
736 .and_then(|it| FloatTy::from_suffix(&it).map(UncertainFloatTy::Known));
735 737
736 Literal::Float( 738 Literal::Float(
737 Default::default(), 739 Default::default(),
@@ -741,7 +743,7 @@ impl ExprCollector {
741 LiteralFlavor::ByteString => Literal::ByteString(Default::default()), 743 LiteralFlavor::ByteString => Literal::ByteString(Default::default()),
742 LiteralFlavor::String => Literal::String(Default::default()), 744 LiteralFlavor::String => Literal::String(Default::default()),
743 LiteralFlavor::Byte => { 745 LiteralFlavor::Byte => {
744 Literal::Int(Default::default(), UncertainIntTy::Unsigned(UintTy::U8)) 746 Literal::Int(Default::default(), UncertainIntTy::Known(IntTy::u8()))
745 } 747 }
746 LiteralFlavor::Bool => Literal::Bool(Default::default()), 748 LiteralFlavor::Bool => Literal::Bool(Default::default()),
747 LiteralFlavor::Char => Literal::Char(Default::default()), 749 LiteralFlavor::Char => Literal::Char(Default::default()),
diff --git a/crates/ra_hir/src/ty/infer.rs b/crates/ra_hir/src/ty/infer.rs
index bf42befbb..7cf465266 100644
--- a/crates/ra_hir/src/ty/infer.rs
+++ b/crates/ra_hir/src/ty/infer.rs
@@ -990,7 +990,12 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
990 match &inner_ty { 990 match &inner_ty {
991 Ty::Apply(a_ty) => match a_ty.ctor { 991 Ty::Apply(a_ty) => match a_ty.ctor {
992 TypeCtor::Int(primitive::UncertainIntTy::Unknown) 992 TypeCtor::Int(primitive::UncertainIntTy::Unknown)
993 | TypeCtor::Int(primitive::UncertainIntTy::Signed(..)) 993 | TypeCtor::Int(primitive::UncertainIntTy::Known(
994 primitive::IntTy {
995 signedness: primitive::Signedness::Signed,
996 ..
997 },
998 ))
994 | TypeCtor::Float(..) => inner_ty, 999 | TypeCtor::Float(..) => inner_ty,
995 _ => Ty::Unknown, 1000 _ => Ty::Unknown,
996 }, 1001 },
@@ -1064,8 +1069,8 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
1064 Ty::apply_one(TypeCtor::Ref(Mutability::Shared), Ty::simple(TypeCtor::Str)) 1069 Ty::apply_one(TypeCtor::Ref(Mutability::Shared), Ty::simple(TypeCtor::Str))
1065 } 1070 }
1066 Literal::ByteString(..) => { 1071 Literal::ByteString(..) => {
1067 let byte_type = Ty::simple(TypeCtor::Int(primitive::UncertainIntTy::Unsigned( 1072 let byte_type = Ty::simple(TypeCtor::Int(primitive::UncertainIntTy::Known(
1068 primitive::UintTy::U8, 1073 primitive::IntTy::u8(),
1069 ))); 1074 )));
1070 let slice_type = Ty::apply_one(TypeCtor::Slice, byte_type); 1075 let slice_type = Ty::apply_one(TypeCtor::Slice, byte_type);
1071 Ty::apply_one(TypeCtor::Ref(Mutability::Shared), slice_type) 1076 Ty::apply_one(TypeCtor::Ref(Mutability::Shared), slice_type)
@@ -1208,10 +1213,10 @@ impl InferTy {
1208 match self { 1213 match self {
1209 InferTy::TypeVar(..) => Ty::Unknown, 1214 InferTy::TypeVar(..) => Ty::Unknown,
1210 InferTy::IntVar(..) => { 1215 InferTy::IntVar(..) => {
1211 Ty::simple(TypeCtor::Int(primitive::UncertainIntTy::Signed(primitive::IntTy::I32))) 1216 Ty::simple(TypeCtor::Int(primitive::UncertainIntTy::Known(primitive::IntTy::i32())))
1212 } 1217 }
1213 InferTy::FloatVar(..) => Ty::simple(TypeCtor::Float( 1218 InferTy::FloatVar(..) => Ty::simple(TypeCtor::Float(
1214 primitive::UncertainFloatTy::Known(primitive::FloatTy::F64), 1219 primitive::UncertainFloatTy::Known(primitive::FloatTy::f64()),
1215 )), 1220 )),
1216 } 1221 }
1217 } 1222 }
diff --git a/crates/ra_hir/src/ty/lower.rs b/crates/ra_hir/src/ty/lower.rs
index 72b1234bf..7f9af307b 100644
--- a/crates/ra_hir/src/ty/lower.rs
+++ b/crates/ra_hir/src/ty/lower.rs
@@ -61,10 +61,10 @@ impl Ty {
61 pub(crate) fn from_hir_path(db: &impl HirDatabase, resolver: &Resolver, path: &Path) -> Self { 61 pub(crate) fn from_hir_path(db: &impl HirDatabase, resolver: &Resolver, path: &Path) -> Self {
62 if let Some(name) = path.as_ident() { 62 if let Some(name) = path.as_ident() {
63 // TODO handle primitive type names in resolver as well? 63 // TODO handle primitive type names in resolver as well?
64 if let Some(int_ty) = primitive::UncertainIntTy::from_type_name(name) { 64 if let Some(int_ty) = primitive::IntTy::from_type_name(name) {
65 return Ty::simple(TypeCtor::Int(int_ty)); 65 return Ty::simple(TypeCtor::Int(primitive::UncertainIntTy::Known(int_ty)));
66 } else if let Some(float_ty) = primitive::UncertainFloatTy::from_type_name(name) { 66 } else if let Some(float_ty) = primitive::FloatTy::from_type_name(name) {
67 return Ty::simple(TypeCtor::Float(float_ty)); 67 return Ty::simple(TypeCtor::Float(primitive::UncertainFloatTy::Known(float_ty)));
68 } else if let Some(known) = name.as_known_name() { 68 } else if let Some(known) = name.as_known_name() {
69 match known { 69 match known {
70 KnownName::Bool => return Ty::simple(TypeCtor::Bool), 70 KnownName::Bool => return Ty::simple(TypeCtor::Bool),
diff --git a/crates/ra_hir/src/ty/primitive.rs b/crates/ra_hir/src/ty/primitive.rs
index 421f7e980..b37326db7 100644
--- a/crates/ra_hir/src/ty/primitive.rs
+++ b/crates/ra_hir/src/ty/primitive.rs
@@ -2,61 +2,49 @@ use std::fmt;
2 2
3use crate::{Name, KnownName}; 3use crate::{Name, KnownName};
4 4
5#[derive(Debug, Clone, Eq, PartialEq, Hash, Copy)] 5#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
6pub enum UncertainIntTy { 6pub enum Signedness {
7 Unknown, 7 Signed,
8 Unsigned(UintTy), 8 Unsigned,
9 Signed(IntTy),
10} 9}
11 10
12impl UncertainIntTy { 11#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
13 pub(crate) fn from_type_name(name: &Name) -> Option<UncertainIntTy> { 12pub enum IntBitness {
14 if let Some(ty) = IntTy::from_type_name(name) { 13 Xsize,
15 Some(UncertainIntTy::Signed(ty)) 14 X8,
16 } else if let Some(ty) = UintTy::from_type_name(name) { 15 X16,
17 Some(UncertainIntTy::Unsigned(ty)) 16 X32,
18 } else { 17 X64,
19 None 18 X128,
20 } 19}
21 }
22 20
23 pub(crate) fn from_suffix(suffix: &str) -> Option<UncertainIntTy> { 21#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
24 if let Some(ty) = IntTy::from_suffix(suffix) { 22pub enum FloatBitness {
25 Some(UncertainIntTy::Signed(ty)) 23 X32,
26 } else if let Some(ty) = UintTy::from_suffix(suffix) { 24 X64,
27 Some(UncertainIntTy::Unsigned(ty)) 25}
28 } else { 26
29 None 27#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
30 } 28pub enum UncertainIntTy {
31 } 29 Unknown,
30 Known(IntTy),
32} 31}
33 32
34impl fmt::Display for UncertainIntTy { 33impl fmt::Display for UncertainIntTy {
35 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 34 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
36 match *self { 35 match *self {
37 UncertainIntTy::Unknown => write!(f, "{{integer}}"), 36 UncertainIntTy::Unknown => write!(f, "{{integer}}"),
38 UncertainIntTy::Signed(ty) => write!(f, "{}", ty), 37 UncertainIntTy::Known(ty) => write!(f, "{}", ty),
39 UncertainIntTy::Unsigned(ty) => write!(f, "{}", ty),
40 } 38 }
41 } 39 }
42} 40}
43 41
44#[derive(Debug, Clone, Eq, PartialEq, Hash, Copy)] 42#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
45pub enum UncertainFloatTy { 43pub enum UncertainFloatTy {
46 Unknown, 44 Unknown,
47 Known(FloatTy), 45 Known(FloatTy),
48} 46}
49 47
50impl UncertainFloatTy {
51 pub(crate) fn from_type_name(name: &Name) -> Option<UncertainFloatTy> {
52 FloatTy::from_type_name(name).map(UncertainFloatTy::Known)
53 }
54
55 pub(crate) fn from_suffix(suffix: &str) -> Option<UncertainFloatTy> {
56 FloatTy::from_suffix(suffix).map(UncertainFloatTy::Known)
57 }
58}
59
60impl fmt::Display for UncertainFloatTy { 48impl fmt::Display for UncertainFloatTy {
61 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 49 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
62 match *self { 50 match *self {
@@ -66,14 +54,10 @@ impl fmt::Display for UncertainFloatTy {
66 } 54 }
67} 55}
68 56
69#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Copy)] 57#[derive(Copy, Clone, Eq, PartialEq, Hash)]
70pub enum IntTy { 58pub struct IntTy {
71 Isize, 59 pub signedness: Signedness,
72 I8, 60 pub bitness: IntBitness,
73 I16,
74 I32,
75 I64,
76 I128,
77} 61}
78 62
79impl fmt::Debug for IntTy { 63impl fmt::Debug for IntTy {
@@ -84,104 +68,116 @@ impl fmt::Debug for IntTy {
84 68
85impl fmt::Display for IntTy { 69impl fmt::Display for IntTy {
86 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 70 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
87 let s = match *self { 71 write!(f, "{}", self.ty_to_string())
88 IntTy::Isize => "isize",
89 IntTy::I8 => "i8",
90 IntTy::I16 => "i16",
91 IntTy::I32 => "i32",
92 IntTy::I64 => "i64",
93 IntTy::I128 => "i128",
94 };
95 write!(f, "{}", s)
96 } 72 }
97} 73}
98 74
99impl IntTy { 75impl IntTy {
100 fn from_type_name(name: &Name) -> Option<IntTy> { 76 pub fn isize() -> IntTy {
101 match name.as_known_name()? { 77 IntTy { signedness: Signedness::Signed, bitness: IntBitness::Xsize }
102 KnownName::Isize => Some(IntTy::Isize),
103 KnownName::I8 => Some(IntTy::I8),
104 KnownName::I16 => Some(IntTy::I16),
105 KnownName::I32 => Some(IntTy::I32),
106 KnownName::I64 => Some(IntTy::I64),
107 KnownName::I128 => Some(IntTy::I128),
108 _ => None,
109 }
110 } 78 }
111 79
112 fn from_suffix(suffix: &str) -> Option<IntTy> { 80 pub fn i8() -> IntTy {
113 match suffix { 81 IntTy { signedness: Signedness::Signed, bitness: IntBitness::X8 }
114 "isize" => Some(IntTy::Isize),
115 "i8" => Some(IntTy::I8),
116 "i16" => Some(IntTy::I16),
117 "i32" => Some(IntTy::I32),
118 "i64" => Some(IntTy::I64),
119 "i128" => Some(IntTy::I128),
120 _ => None,
121 }
122 } 82 }
123}
124 83
125#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Copy)] 84 pub fn i16() -> IntTy {
126pub enum UintTy { 85 IntTy { signedness: Signedness::Signed, bitness: IntBitness::X16 }
127 Usize, 86 }
128 U8,
129 U16,
130 U32,
131 U64,
132 U128,
133}
134 87
135impl fmt::Display for UintTy { 88 pub fn i32() -> IntTy {
136 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 89 IntTy { signedness: Signedness::Signed, bitness: IntBitness::X32 }
137 let s = match *self {
138 UintTy::Usize => "usize",
139 UintTy::U8 => "u8",
140 UintTy::U16 => "u16",
141 UintTy::U32 => "u32",
142 UintTy::U64 => "u64",
143 UintTy::U128 => "u128",
144 };
145 write!(f, "{}", s)
146 } 90 }
147}
148 91
149impl UintTy { 92 pub fn i64() -> IntTy {
150 fn from_type_name(name: &Name) -> Option<UintTy> { 93 IntTy { signedness: Signedness::Signed, bitness: IntBitness::X64 }
94 }
95
96 pub fn i128() -> IntTy {
97 IntTy { signedness: Signedness::Signed, bitness: IntBitness::X128 }
98 }
99
100 pub fn usize() -> IntTy {
101 IntTy { signedness: Signedness::Unsigned, bitness: IntBitness::Xsize }
102 }
103
104 pub fn u8() -> IntTy {
105 IntTy { signedness: Signedness::Unsigned, bitness: IntBitness::X8 }
106 }
107
108 pub fn u16() -> IntTy {
109 IntTy { signedness: Signedness::Unsigned, bitness: IntBitness::X16 }
110 }
111
112 pub fn u32() -> IntTy {
113 IntTy { signedness: Signedness::Unsigned, bitness: IntBitness::X32 }
114 }
115
116 pub fn u64() -> IntTy {
117 IntTy { signedness: Signedness::Unsigned, bitness: IntBitness::X64 }
118 }
119
120 pub fn u128() -> IntTy {
121 IntTy { signedness: Signedness::Unsigned, bitness: IntBitness::X128 }
122 }
123
124 pub(crate) fn ty_to_string(&self) -> &'static str {
125 match (self.signedness, self.bitness) {
126 (Signedness::Signed, IntBitness::Xsize) => "isize",
127 (Signedness::Signed, IntBitness::X8) => "i8",
128 (Signedness::Signed, IntBitness::X16) => "i16",
129 (Signedness::Signed, IntBitness::X32) => "i32",
130 (Signedness::Signed, IntBitness::X64) => "i64",
131 (Signedness::Signed, IntBitness::X128) => "i128",
132 (Signedness::Unsigned, IntBitness::Xsize) => "usize",
133 (Signedness::Unsigned, IntBitness::X8) => "u8",
134 (Signedness::Unsigned, IntBitness::X16) => "u16",
135 (Signedness::Unsigned, IntBitness::X32) => "u32",
136 (Signedness::Unsigned, IntBitness::X64) => "u64",
137 (Signedness::Unsigned, IntBitness::X128) => "u128",
138 }
139 }
140
141 pub(crate) fn from_type_name(name: &Name) -> Option<IntTy> {
151 match name.as_known_name()? { 142 match name.as_known_name()? {
152 KnownName::Usize => Some(UintTy::Usize), 143 KnownName::Isize => Some(IntTy::isize()),
153 KnownName::U8 => Some(UintTy::U8), 144 KnownName::I8 => Some(IntTy::i8()),
154 KnownName::U16 => Some(UintTy::U16), 145 KnownName::I16 => Some(IntTy::i16()),
155 KnownName::U32 => Some(UintTy::U32), 146 KnownName::I32 => Some(IntTy::i32()),
156 KnownName::U64 => Some(UintTy::U64), 147 KnownName::I64 => Some(IntTy::i64()),
157 KnownName::U128 => Some(UintTy::U128), 148 KnownName::I128 => Some(IntTy::i128()),
149 KnownName::Usize => Some(IntTy::usize()),
150 KnownName::U8 => Some(IntTy::u8()),
151 KnownName::U16 => Some(IntTy::u16()),
152 KnownName::U32 => Some(IntTy::u32()),
153 KnownName::U64 => Some(IntTy::u64()),
154 KnownName::U128 => Some(IntTy::u128()),
158 _ => None, 155 _ => None,
159 } 156 }
160 } 157 }
161 158
162 fn from_suffix(suffix: &str) -> Option<UintTy> { 159 pub(crate) fn from_suffix(suffix: &str) -> Option<IntTy> {
163 match suffix { 160 match suffix {
164 "usize" => Some(UintTy::Usize), 161 "isize" => Some(IntTy::isize()),
165 "u8" => Some(UintTy::U8), 162 "i8" => Some(IntTy::i8()),
166 "u16" => Some(UintTy::U16), 163 "i16" => Some(IntTy::i16()),
167 "u32" => Some(UintTy::U32), 164 "i32" => Some(IntTy::i32()),
168 "u64" => Some(UintTy::U64), 165 "i64" => Some(IntTy::i64()),
169 "u128" => Some(UintTy::U128), 166 "i128" => Some(IntTy::i128()),
167 "usize" => Some(IntTy::usize()),
168 "u8" => Some(IntTy::u8()),
169 "u16" => Some(IntTy::u16()),
170 "u32" => Some(IntTy::u32()),
171 "u64" => Some(IntTy::u64()),
172 "u128" => Some(IntTy::u128()),
170 _ => None, 173 _ => None,
171 } 174 }
172 } 175 }
173} 176}
174 177
175impl fmt::Debug for UintTy { 178#[derive(Copy, Clone, PartialEq, Eq, Hash)]
176 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 179pub struct FloatTy {
177 fmt::Display::fmt(self, f) 180 pub bitness: FloatBitness,
178 }
179}
180
181#[derive(Clone, PartialEq, Eq, Hash, Copy, PartialOrd, Ord)]
182pub enum FloatTy {
183 F32,
184 F64,
185} 181}
186 182
187impl fmt::Debug for FloatTy { 183impl fmt::Debug for FloatTy {
@@ -197,25 +193,33 @@ impl fmt::Display for FloatTy {
197} 193}
198 194
199impl FloatTy { 195impl FloatTy {
200 pub fn ty_to_string(self) -> &'static str { 196 pub fn f32() -> FloatTy {
201 match self { 197 FloatTy { bitness: FloatBitness::X32 }
202 FloatTy::F32 => "f32", 198 }
203 FloatTy::F64 => "f64", 199
200 pub fn f64() -> FloatTy {
201 FloatTy { bitness: FloatBitness::X64 }
202 }
203
204 pub(crate) fn ty_to_string(self) -> &'static str {
205 match self.bitness {
206 FloatBitness::X32 => "f32",
207 FloatBitness::X64 => "f64",
204 } 208 }
205 } 209 }
206 210
207 fn from_type_name(name: &Name) -> Option<FloatTy> { 211 pub(crate) fn from_type_name(name: &Name) -> Option<FloatTy> {
208 match name.as_known_name()? { 212 match name.as_known_name()? {
209 KnownName::F32 => Some(FloatTy::F32), 213 KnownName::F32 => Some(FloatTy::f32()),
210 KnownName::F64 => Some(FloatTy::F64), 214 KnownName::F64 => Some(FloatTy::f64()),
211 _ => None, 215 _ => None,
212 } 216 }
213 } 217 }
214 218
215 fn from_suffix(suffix: &str) -> Option<FloatTy> { 219 pub(crate) fn from_suffix(suffix: &str) -> Option<FloatTy> {
216 match suffix { 220 match suffix {
217 "f32" => Some(FloatTy::F32), 221 "f32" => Some(FloatTy::f32()),
218 "f64" => Some(FloatTy::F64), 222 "f64" => Some(FloatTy::f64()),
219 _ => None, 223 _ => None,
220 } 224 }
221 } 225 }