aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src
diff options
context:
space:
mode:
authorFlorian Diebold <[email protected]>2018-12-22 21:17:55 +0000
committerFlorian Diebold <[email protected]>2018-12-23 12:48:04 +0000
commit3899898d75176ce3cd87f9e2acecd7e3a987dda5 (patch)
tree00dc54ff21b509d1c68ba7d1d0d1bddd2848b45f /crates/ra_hir/src
parent3ac605e6876056fa56098231cc2f96553faab8f0 (diff)
Parse integer / float types
Diffstat (limited to 'crates/ra_hir/src')
-rw-r--r--crates/ra_hir/src/ty.rs22
-rw-r--r--crates/ra_hir/src/ty/primitive.rs32
2 files changed, 52 insertions, 2 deletions
diff --git a/crates/ra_hir/src/ty.rs b/crates/ra_hir/src/ty.rs
index 36dc5d137..087385b98 100644
--- a/crates/ra_hir/src/ty.rs
+++ b/crates/ra_hir/src/ty.rs
@@ -9,7 +9,7 @@ use std::collections::HashMap;
9 9
10use ra_db::LocalSyntaxPtr; 10use ra_db::LocalSyntaxPtr;
11use ra_syntax::{ 11use ra_syntax::{
12 TextRange, TextUnit, 12 TextRange, TextUnit, SmolStr,
13 algo::visit::{visitor, Visitor}, 13 algo::visit::{visitor, Visitor},
14 ast::{self, AstNode, DocCommentsOwner, NameOwner, LoopBodyOwner, ArgListOwner}, 14 ast::{self, AstNode, DocCommentsOwner, NameOwner, LoopBodyOwner, ArgListOwner},
15 SyntaxNodeRef 15 SyntaxNodeRef
@@ -148,7 +148,25 @@ impl Ty {
148 ParenType(_inner) => Ty::Unknown, // TODO 148 ParenType(_inner) => Ty::Unknown, // TODO
149 TupleType(_inner) => Ty::Unknown, // TODO 149 TupleType(_inner) => Ty::Unknown, // TODO
150 NeverType(..) => Ty::Never, 150 NeverType(..) => Ty::Never,
151 PathType(_inner) => Ty::Unknown, // TODO 151 PathType(inner) => {
152 let path = if let Some(p) = inner.path() { p } else { return Ty::Unknown };
153 if path.qualifier().is_none() {
154 let name = path.segment().and_then(|s| s.name_ref()).map(|n| n.text()).unwrap_or(SmolStr::new(""));
155 if let Some(int_ty) = primitive::IntTy::from_string(&name) {
156 Ty::Int(int_ty)
157 } else if let Some(uint_ty) = primitive::UintTy::from_string(&name) {
158 Ty::Uint(uint_ty)
159 } else if let Some(float_ty) = primitive::FloatTy::from_string(&name) {
160 Ty::Float(float_ty)
161 } else {
162 // TODO
163 Ty::Unknown
164 }
165 } else {
166 // TODO
167 Ty::Unknown
168 }
169 },
152 PointerType(_inner) => Ty::Unknown, // TODO 170 PointerType(_inner) => Ty::Unknown, // TODO
153 ArrayType(_inner) => Ty::Unknown, // TODO 171 ArrayType(_inner) => Ty::Unknown, // TODO
154 SliceType(_inner) => Ty::Unknown, // TODO 172 SliceType(_inner) => Ty::Unknown, // TODO
diff --git a/crates/ra_hir/src/ty/primitive.rs b/crates/ra_hir/src/ty/primitive.rs
index 4a5ce5a97..ad79b17e4 100644
--- a/crates/ra_hir/src/ty/primitive.rs
+++ b/crates/ra_hir/src/ty/primitive.rs
@@ -33,6 +33,18 @@ impl IntTy {
33 IntTy::I128 => "i128", 33 IntTy::I128 => "i128",
34 } 34 }
35 } 35 }
36
37 pub fn from_string(s: &str) -> Option<IntTy> {
38 match s {
39 "isize" => Some(IntTy::Isize),
40 "i8" => Some(IntTy::I8),
41 "i16" => Some(IntTy::I16),
42 "i32" => Some(IntTy::I32),
43 "i64" => Some(IntTy::I64),
44 "i128" => Some(IntTy::I128),
45 _ => None,
46 }
47 }
36} 48}
37 49
38#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Copy)] 50#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Copy)]
@@ -56,6 +68,18 @@ impl UintTy {
56 UintTy::U128 => "u128", 68 UintTy::U128 => "u128",
57 } 69 }
58 } 70 }
71
72 pub fn from_string(s: &str) -> Option<UintTy> {
73 match s {
74 "usize" => Some(UintTy::Usize),
75 "u8" => Some(UintTy::U8),
76 "u16" => Some(UintTy::U16),
77 "u32" => Some(UintTy::U32),
78 "u64" => Some(UintTy::U64),
79 "u128" => Some(UintTy::U128),
80 _ => None,
81 }
82 }
59} 83}
60 84
61impl fmt::Debug for UintTy { 85impl fmt::Debug for UintTy {
@@ -95,4 +119,12 @@ impl FloatTy {
95 FloatTy::F64 => "f64", 119 FloatTy::F64 => "f64",
96 } 120 }
97 } 121 }
122
123 pub fn from_string(s: &str) -> Option<FloatTy> {
124 match s {
125 "f32" => Some(FloatTy::F32),
126 "f64" => Some(FloatTy::F64),
127 _ => None,
128 }
129 }
98} 130}