diff options
Diffstat (limited to 'crates/ra_hir/src/name.rs')
-rw-r--r-- | crates/ra_hir/src/name.rs | 136 |
1 files changed, 25 insertions, 111 deletions
diff --git a/crates/ra_hir/src/name.rs b/crates/ra_hir/src/name.rs index b71590560..718795314 100644 --- a/crates/ra_hir/src/name.rs +++ b/crates/ra_hir/src/name.rs | |||
@@ -26,7 +26,7 @@ impl Name { | |||
26 | /// Note: this is private to make creating name from random string hard. | 26 | /// Note: this is private to make creating name from random string hard. |
27 | /// Hopefully, this should allow us to integrate hygiene cleaner in the | 27 | /// Hopefully, this should allow us to integrate hygiene cleaner in the |
28 | /// future, and to switch to interned representation of names. | 28 | /// future, and to switch to interned representation of names. |
29 | fn new(text: SmolStr) -> Name { | 29 | const fn new(text: SmolStr) -> Name { |
30 | Name { text } | 30 | Name { text } |
31 | } | 31 | } |
32 | 32 | ||
@@ -34,14 +34,6 @@ impl Name { | |||
34 | Name::new("[missing name]".into()) | 34 | Name::new("[missing name]".into()) |
35 | } | 35 | } |
36 | 36 | ||
37 | pub(crate) fn self_param() -> Name { | ||
38 | Name::new("self".into()) | ||
39 | } | ||
40 | |||
41 | pub(crate) fn self_type() -> Name { | ||
42 | Name::new("Self".into()) | ||
43 | } | ||
44 | |||
45 | pub(crate) fn tuple_field_name(idx: usize) -> Name { | 37 | pub(crate) fn tuple_field_name(idx: usize) -> Name { |
46 | Name::new(idx.to_string().into()) | 38 | Name::new(idx.to_string().into()) |
47 | } | 39 | } |
@@ -63,38 +55,6 @@ impl Name { | |||
63 | pub fn as_smolstr(&self) -> &SmolStr { | 55 | pub fn as_smolstr(&self) -> &SmolStr { |
64 | &self.text | 56 | &self.text |
65 | } | 57 | } |
66 | |||
67 | pub(crate) fn as_known_name(&self) -> Option<KnownName> { | ||
68 | let name = match self.text.as_str() { | ||
69 | "isize" => KnownName::Isize, | ||
70 | "i8" => KnownName::I8, | ||
71 | "i16" => KnownName::I16, | ||
72 | "i32" => KnownName::I32, | ||
73 | "i64" => KnownName::I64, | ||
74 | "i128" => KnownName::I128, | ||
75 | "usize" => KnownName::Usize, | ||
76 | "u8" => KnownName::U8, | ||
77 | "u16" => KnownName::U16, | ||
78 | "u32" => KnownName::U32, | ||
79 | "u64" => KnownName::U64, | ||
80 | "u128" => KnownName::U128, | ||
81 | "f32" => KnownName::F32, | ||
82 | "f64" => KnownName::F64, | ||
83 | "bool" => KnownName::Bool, | ||
84 | "char" => KnownName::Char, | ||
85 | "str" => KnownName::Str, | ||
86 | "Self" => KnownName::SelfType, | ||
87 | "self" => KnownName::SelfParam, | ||
88 | "macro_rules" => KnownName::MacroRules, | ||
89 | |||
90 | "std" => KnownName::Std, | ||
91 | "iter" => KnownName::Iter, | ||
92 | "IntoIterator" => KnownName::IntoIterator, | ||
93 | "Item" => KnownName::Item, | ||
94 | _ => return None, | ||
95 | }; | ||
96 | Some(name) | ||
97 | } | ||
98 | } | 58 | } |
99 | 59 | ||
100 | pub(crate) trait AsName { | 60 | pub(crate) trait AsName { |
@@ -130,76 +90,30 @@ impl AsName for ra_db::Dependency { | |||
130 | } | 90 | } |
131 | } | 91 | } |
132 | 92 | ||
133 | // Ideally, should be replaced with | 93 | pub(crate) const ISIZE: Name = Name::new(SmolStr::new_inline_from_ascii(5, b"isize")); |
134 | // ``` | 94 | pub(crate) const I8: Name = Name::new(SmolStr::new_inline_from_ascii(2, b"i8")); |
135 | // const ISIZE: Name = Name::new("isize") | 95 | pub(crate) const I16: Name = Name::new(SmolStr::new_inline_from_ascii(3, b"i16")); |
136 | // ``` | 96 | pub(crate) const I32: Name = Name::new(SmolStr::new_inline_from_ascii(3, b"i32")); |
137 | // but const-fn is not that powerful yet. | 97 | pub(crate) const I64: Name = Name::new(SmolStr::new_inline_from_ascii(3, b"i64")); |
138 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] | 98 | pub(crate) const I128: Name = Name::new(SmolStr::new_inline_from_ascii(4, b"i128")); |
139 | pub(crate) enum KnownName { | 99 | pub(crate) const USIZE: Name = Name::new(SmolStr::new_inline_from_ascii(5, b"usize")); |
140 | Isize, | 100 | pub(crate) const U8: Name = Name::new(SmolStr::new_inline_from_ascii(2, b"u8")); |
141 | I8, | 101 | pub(crate) const U16: Name = Name::new(SmolStr::new_inline_from_ascii(3, b"u16")); |
142 | I16, | 102 | pub(crate) const U32: Name = Name::new(SmolStr::new_inline_from_ascii(3, b"u32")); |
143 | I32, | 103 | pub(crate) const U64: Name = Name::new(SmolStr::new_inline_from_ascii(3, b"u64")); |
144 | I64, | 104 | pub(crate) const U128: Name = Name::new(SmolStr::new_inline_from_ascii(4, b"u128")); |
145 | I128, | 105 | pub(crate) const F32: Name = Name::new(SmolStr::new_inline_from_ascii(3, b"f32")); |
146 | 106 | pub(crate) const F64: Name = Name::new(SmolStr::new_inline_from_ascii(3, b"f64")); | |
147 | Usize, | 107 | pub(crate) const BOOL: Name = Name::new(SmolStr::new_inline_from_ascii(4, b"bool")); |
148 | U8, | 108 | pub(crate) const CHAR: Name = Name::new(SmolStr::new_inline_from_ascii(4, b"char")); |
149 | U16, | 109 | pub(crate) const STR: Name = Name::new(SmolStr::new_inline_from_ascii(3, b"str")); |
150 | U32, | 110 | pub(crate) const SELF_PARAM: Name = Name::new(SmolStr::new_inline_from_ascii(4, b"self")); |
151 | U64, | 111 | pub(crate) const SELF_TYPE: Name = Name::new(SmolStr::new_inline_from_ascii(4, b"Self")); |
152 | U128, | 112 | pub(crate) const MACRO_RULES: Name = Name::new(SmolStr::new_inline_from_ascii(11, b"macro_rules")); |
153 | 113 | pub(crate) const STD: Name = Name::new(SmolStr::new_inline_from_ascii(3, b"std")); | |
154 | F32, | 114 | pub(crate) const ITER: Name = Name::new(SmolStr::new_inline_from_ascii(4, b"iter")); |
155 | F64, | 115 | pub(crate) const INTO_ITERATOR: Name = Name::new(SmolStr::new_inline_from_ascii(12, b"IntoIterator")); |
156 | 116 | pub(crate) const ITEM: Name = Name::new(SmolStr::new_inline_from_ascii(4, b"Item")); | |
157 | Bool, | ||
158 | Char, | ||
159 | Str, | ||
160 | |||
161 | SelfType, | ||
162 | SelfParam, | ||
163 | |||
164 | MacroRules, | ||
165 | |||
166 | Std, | ||
167 | Iter, | ||
168 | IntoIterator, | ||
169 | Item, | ||
170 | } | ||
171 | |||
172 | impl AsName for KnownName { | ||
173 | fn as_name(&self) -> Name { | ||
174 | let s = match self { | ||
175 | KnownName::Isize => "isize", | ||
176 | KnownName::I8 => "i8", | ||
177 | KnownName::I16 => "i16", | ||
178 | KnownName::I32 => "i32", | ||
179 | KnownName::I64 => "i64", | ||
180 | KnownName::I128 => "i128", | ||
181 | KnownName::Usize => "usize", | ||
182 | KnownName::U8 => "u8", | ||
183 | KnownName::U16 => "u16", | ||
184 | KnownName::U32 => "u32", | ||
185 | KnownName::U64 => "u64", | ||
186 | KnownName::U128 => "u128", | ||
187 | KnownName::F32 => "f32", | ||
188 | KnownName::F64 => "f64", | ||
189 | KnownName::Bool => "bool", | ||
190 | KnownName::Char => "char", | ||
191 | KnownName::Str => "str", | ||
192 | KnownName::SelfType => "Self", | ||
193 | KnownName::SelfParam => "self", | ||
194 | KnownName::MacroRules => "macro_rules", | ||
195 | KnownName::Std => "std", | ||
196 | KnownName::Iter => "iter", | ||
197 | KnownName::IntoIterator => "IntoIterator", | ||
198 | KnownName::Item => "Item", | ||
199 | }; | ||
200 | Name::new(s.into()) | ||
201 | } | ||
202 | } | ||
203 | 117 | ||
204 | fn resolve_name(text: &SmolStr) -> SmolStr { | 118 | fn resolve_name(text: &SmolStr) -> SmolStr { |
205 | let raw_start = "r#"; | 119 | let raw_start = "r#"; |