diff options
author | Aleksey Kladov <[email protected]> | 2018-12-27 17:26:15 +0000 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2018-12-27 17:26:15 +0000 |
commit | a9f55029b9db3bcd439d31c5007785299f7d4025 (patch) | |
tree | 9f6b93060cc56d5c96f4c61d5502b7267a626827 /crates/ra_hir | |
parent | d963042ca9da93be8d5922ce46ea26dc6a79c929 (diff) |
introduce known names
Diffstat (limited to 'crates/ra_hir')
-rw-r--r-- | crates/ra_hir/src/lib.rs | 2 | ||||
-rw-r--r-- | crates/ra_hir/src/name.rs | 47 | ||||
-rw-r--r-- | crates/ra_hir/src/ty.rs | 9 | ||||
-rw-r--r-- | crates/ra_hir/src/ty/primitive.rs | 42 |
4 files changed, 71 insertions, 29 deletions
diff --git a/crates/ra_hir/src/lib.rs b/crates/ra_hir/src/lib.rs index bf43cd0ae..5bbb09c01 100644 --- a/crates/ra_hir/src/lib.rs +++ b/crates/ra_hir/src/lib.rs | |||
@@ -38,7 +38,7 @@ use ra_db::{LocationIntener, SourceRootId, FileId, Cancelable}; | |||
38 | use crate::{ | 38 | use crate::{ |
39 | db::HirDatabase, | 39 | db::HirDatabase, |
40 | arena::{Arena, Id}, | 40 | arena::{Arena, Id}, |
41 | name::AsName, | 41 | name::{AsName, KnownName}, |
42 | }; | 42 | }; |
43 | 43 | ||
44 | pub use self::{ | 44 | pub use self::{ |
diff --git a/crates/ra_hir/src/name.rs b/crates/ra_hir/src/name.rs index 7f42c9f04..cdad31be7 100644 --- a/crates/ra_hir/src/name.rs +++ b/crates/ra_hir/src/name.rs | |||
@@ -17,9 +17,25 @@ impl fmt::Display for Name { | |||
17 | } | 17 | } |
18 | 18 | ||
19 | impl Name { | 19 | impl Name { |
20 | // TODO: get rid of this? | 20 | pub(crate) fn as_known_name(&self) -> Option<KnownName> { |
21 | pub(crate) fn as_str(&self) -> &str { | 21 | let name = match self.text.as_str() { |
22 | self.text.as_str() | 22 | "isize" => KnownName::Isize, |
23 | "i8" => KnownName::I8, | ||
24 | "i16" => KnownName::I16, | ||
25 | "i32" => KnownName::I32, | ||
26 | "i64" => KnownName::I64, | ||
27 | "i128" => KnownName::I128, | ||
28 | "usize" => KnownName::Usize, | ||
29 | "u8" => KnownName::U8, | ||
30 | "u16" => KnownName::U16, | ||
31 | "u32" => KnownName::U32, | ||
32 | "u64" => KnownName::U64, | ||
33 | "u128" => KnownName::U128, | ||
34 | "f32" => KnownName::F32, | ||
35 | "f64" => KnownName::F64, | ||
36 | _ => return None, | ||
37 | }; | ||
38 | Some(name) | ||
23 | } | 39 | } |
24 | 40 | ||
25 | #[cfg(not(test))] | 41 | #[cfg(not(test))] |
@@ -54,3 +70,28 @@ impl AsName for ra_db::Dependency { | |||
54 | Name::new(self.name.clone()) | 70 | Name::new(self.name.clone()) |
55 | } | 71 | } |
56 | } | 72 | } |
73 | |||
74 | // Ideally, should be replaced with | ||
75 | // ``` | ||
76 | // const ISIZE: Name = Name::new("isize") | ||
77 | // ``` | ||
78 | // but const-fn is not that powerful yet. | ||
79 | #[derive(Debug)] | ||
80 | pub(crate) enum KnownName { | ||
81 | Isize, | ||
82 | I8, | ||
83 | I16, | ||
84 | I32, | ||
85 | I64, | ||
86 | I128, | ||
87 | |||
88 | Usize, | ||
89 | U8, | ||
90 | U16, | ||
91 | U32, | ||
92 | U64, | ||
93 | U128, | ||
94 | |||
95 | F32, | ||
96 | F64, | ||
97 | } | ||
diff --git a/crates/ra_hir/src/ty.rs b/crates/ra_hir/src/ty.rs index dc3323b1a..ad097d1f1 100644 --- a/crates/ra_hir/src/ty.rs +++ b/crates/ra_hir/src/ty.rs | |||
@@ -95,7 +95,7 @@ pub enum Ty { | |||
95 | Tuple(Vec<Ty>), | 95 | Tuple(Vec<Ty>), |
96 | 96 | ||
97 | // The projection of an associated type. For example, | 97 | // The projection of an associated type. For example, |
98 | // `<T as Trait<..>>::N`. | 98 | // `<T as Trait<..>>::N`.pub |
99 | // Projection(ProjectionTy), | 99 | // Projection(ProjectionTy), |
100 | 100 | ||
101 | // Opaque (`impl Trait`) type found in a return type. | 101 | // Opaque (`impl Trait`) type found in a return type. |
@@ -180,12 +180,11 @@ impl Ty { | |||
180 | path: &Path, | 180 | path: &Path, |
181 | ) -> Cancelable<Self> { | 181 | ) -> Cancelable<Self> { |
182 | if let Some(name) = path.as_ident() { | 182 | if let Some(name) = path.as_ident() { |
183 | let name = name.as_str(); // :-( | 183 | if let Some(int_ty) = primitive::IntTy::from_name(name) { |
184 | if let Some(int_ty) = primitive::IntTy::from_string(name) { | ||
185 | return Ok(Ty::Int(int_ty)); | 184 | return Ok(Ty::Int(int_ty)); |
186 | } else if let Some(uint_ty) = primitive::UintTy::from_string(name) { | 185 | } else if let Some(uint_ty) = primitive::UintTy::from_name(name) { |
187 | return Ok(Ty::Uint(uint_ty)); | 186 | return Ok(Ty::Uint(uint_ty)); |
188 | } else if let Some(float_ty) = primitive::FloatTy::from_string(name) { | 187 | } else if let Some(float_ty) = primitive::FloatTy::from_name(name) { |
189 | return Ok(Ty::Float(float_ty)); | 188 | return Ok(Ty::Float(float_ty)); |
190 | } | 189 | } |
191 | } | 190 | } |
diff --git a/crates/ra_hir/src/ty/primitive.rs b/crates/ra_hir/src/ty/primitive.rs index ad79b17e4..498d42d52 100644 --- a/crates/ra_hir/src/ty/primitive.rs +++ b/crates/ra_hir/src/ty/primitive.rs | |||
@@ -1,5 +1,7 @@ | |||
1 | use std::fmt; | 1 | use std::fmt; |
2 | 2 | ||
3 | use crate::{Name, KnownName}; | ||
4 | |||
3 | #[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Copy)] | 5 | #[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Copy)] |
4 | pub enum IntTy { | 6 | pub enum IntTy { |
5 | Isize, | 7 | Isize, |
@@ -34,14 +36,14 @@ impl IntTy { | |||
34 | } | 36 | } |
35 | } | 37 | } |
36 | 38 | ||
37 | pub fn from_string(s: &str) -> Option<IntTy> { | 39 | pub fn from_name(name: &Name) -> Option<IntTy> { |
38 | match s { | 40 | match name.as_known_name()? { |
39 | "isize" => Some(IntTy::Isize), | 41 | KnownName::Isize => Some(IntTy::Isize), |
40 | "i8" => Some(IntTy::I8), | 42 | KnownName::I8 => Some(IntTy::I8), |
41 | "i16" => Some(IntTy::I16), | 43 | KnownName::I16 => Some(IntTy::I16), |
42 | "i32" => Some(IntTy::I32), | 44 | KnownName::I32 => Some(IntTy::I32), |
43 | "i64" => Some(IntTy::I64), | 45 | KnownName::I64 => Some(IntTy::I64), |
44 | "i128" => Some(IntTy::I128), | 46 | KnownName::I128 => Some(IntTy::I128), |
45 | _ => None, | 47 | _ => None, |
46 | } | 48 | } |
47 | } | 49 | } |
@@ -69,14 +71,14 @@ impl UintTy { | |||
69 | } | 71 | } |
70 | } | 72 | } |
71 | 73 | ||
72 | pub fn from_string(s: &str) -> Option<UintTy> { | 74 | pub fn from_name(name: &Name) -> Option<UintTy> { |
73 | match s { | 75 | match name.as_known_name()? { |
74 | "usize" => Some(UintTy::Usize), | 76 | KnownName::Usize => Some(UintTy::Usize), |
75 | "u8" => Some(UintTy::U8), | 77 | KnownName::U8 => Some(UintTy::U8), |
76 | "u16" => Some(UintTy::U16), | 78 | KnownName::U16 => Some(UintTy::U16), |
77 | "u32" => Some(UintTy::U32), | 79 | KnownName::U32 => Some(UintTy::U32), |
78 | "u64" => Some(UintTy::U64), | 80 | KnownName::U64 => Some(UintTy::U64), |
79 | "u128" => Some(UintTy::U128), | 81 | KnownName::U128 => Some(UintTy::U128), |
80 | _ => None, | 82 | _ => None, |
81 | } | 83 | } |
82 | } | 84 | } |
@@ -120,10 +122,10 @@ impl FloatTy { | |||
120 | } | 122 | } |
121 | } | 123 | } |
122 | 124 | ||
123 | pub fn from_string(s: &str) -> Option<FloatTy> { | 125 | pub fn from_name(name: &Name) -> Option<FloatTy> { |
124 | match s { | 126 | match name.as_known_name()? { |
125 | "f32" => Some(FloatTy::F32), | 127 | KnownName::F32 => Some(FloatTy::F32), |
126 | "f64" => Some(FloatTy::F64), | 128 | KnownName::F64 => Some(FloatTy::F64), |
127 | _ => None, | 129 | _ => None, |
128 | } | 130 | } |
129 | } | 131 | } |