aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src/name.rs
diff options
context:
space:
mode:
authorbors[bot] <bors[bot]@users.noreply.github.com>2018-12-27 20:33:47 +0000
committerbors[bot] <bors[bot]@users.noreply.github.com>2018-12-27 20:33:47 +0000
commitefb63a7666cc9532d97fa7e0da14b540ae8bd5df (patch)
treec4c1b80d8e2dfbb6533cfbdf0bb647ddbeff9419 /crates/ra_hir/src/name.rs
parentb26ab3603d8f73c8e57e9e90d44486a608bc9370 (diff)
parente0660506719476a0546e10bee816d7220be85440 (diff)
Merge #330
330: WIP: introduce hir::Name r=matklad a=matklad Currently we are using `SmolStr` throughout the hir as a name, but that is really suboptimal choice: we'll probably want some kind of interning in the future, and we'll definitely need to add hygene info to names. This PR aims to replace strings with a slightly more abstract `Name` type. Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates/ra_hir/src/name.rs')
-rw-r--r--crates/ra_hir/src/name.rs97
1 files changed, 97 insertions, 0 deletions
diff --git a/crates/ra_hir/src/name.rs b/crates/ra_hir/src/name.rs
new file mode 100644
index 000000000..e4fc141a6
--- /dev/null
+++ b/crates/ra_hir/src/name.rs
@@ -0,0 +1,97 @@
1use std::fmt;
2
3use ra_syntax::{ast, SmolStr};
4
5/// `Name` is a wrapper around string, which is used in hir for both references
6/// and declarations. In theory, names should also carry hygene info, but we are
7/// not there yet!
8#[derive(Clone, PartialEq, Eq, Hash)]
9pub struct Name {
10 text: SmolStr,
11}
12
13impl fmt::Display for Name {
14 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
15 fmt::Display::fmt(&self.text, f)
16 }
17}
18
19impl fmt::Debug for Name {
20 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
21 fmt::Debug::fmt(&self.text, f)
22 }
23}
24
25impl Name {
26 pub(crate) fn as_known_name(&self) -> Option<KnownName> {
27 let name = match self.text.as_str() {
28 "isize" => KnownName::Isize,
29 "i8" => KnownName::I8,
30 "i16" => KnownName::I16,
31 "i32" => KnownName::I32,
32 "i64" => KnownName::I64,
33 "i128" => KnownName::I128,
34 "usize" => KnownName::Usize,
35 "u8" => KnownName::U8,
36 "u16" => KnownName::U16,
37 "u32" => KnownName::U32,
38 "u64" => KnownName::U64,
39 "u128" => KnownName::U128,
40 "f32" => KnownName::F32,
41 "f64" => KnownName::F64,
42 _ => return None,
43 };
44 Some(name)
45 }
46
47 fn new(text: SmolStr) -> Name {
48 Name { text }
49 }
50}
51
52pub(crate) trait AsName {
53 fn as_name(&self) -> Name;
54}
55
56impl AsName for ast::NameRef<'_> {
57 fn as_name(&self) -> Name {
58 Name::new(self.text())
59 }
60}
61
62impl AsName for ast::Name<'_> {
63 fn as_name(&self) -> Name {
64 Name::new(self.text())
65 }
66}
67
68impl AsName for ra_db::Dependency {
69 fn as_name(&self) -> Name {
70 Name::new(self.name.clone())
71 }
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)]
80pub(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}