aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src/name.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_hir/src/name.rs')
-rw-r--r--crates/ra_hir/src/name.rs142
1 files changed, 0 insertions, 142 deletions
diff --git a/crates/ra_hir/src/name.rs b/crates/ra_hir/src/name.rs
deleted file mode 100644
index 1e0b8c350..000000000
--- a/crates/ra_hir/src/name.rs
+++ /dev/null
@@ -1,142 +0,0 @@
1//! FIXME: write short doc here
2
3use std::fmt;
4
5use ra_syntax::{ast, SmolStr};
6
7/// `Name` is a wrapper around string, which is used in hir for both references
8/// and declarations. In theory, names should also carry hygiene info, but we are
9/// not there yet!
10#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
11pub struct Name(Repr);
12
13#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
14enum Repr {
15 Text(SmolStr),
16 TupleField(usize),
17}
18
19impl fmt::Display for Name {
20 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
21 match &self.0 {
22 Repr::Text(text) => fmt::Display::fmt(&text, f),
23 Repr::TupleField(idx) => fmt::Display::fmt(&idx, f),
24 }
25 }
26}
27
28impl Name {
29 /// Note: this is private to make creating name from random string hard.
30 /// Hopefully, this should allow us to integrate hygiene cleaner in the
31 /// future, and to switch to interned representation of names.
32 const fn new_text(text: SmolStr) -> Name {
33 Name(Repr::Text(text))
34 }
35
36 pub(crate) fn new_tuple_field(idx: usize) -> Name {
37 Name(Repr::TupleField(idx))
38 }
39
40 /// Shortcut to create inline plain text name
41 const fn new_inline_ascii(len: usize, text: &[u8]) -> Name {
42 Name::new_text(SmolStr::new_inline_from_ascii(len, text))
43 }
44
45 /// Resolve a name from the text of token.
46 fn resolve(raw_text: &SmolStr) -> Name {
47 let raw_start = "r#";
48 if raw_text.as_str().starts_with(raw_start) {
49 Name::new_text(SmolStr::new(&raw_text[raw_start.len()..]))
50 } else {
51 Name::new_text(raw_text.clone())
52 }
53 }
54
55 pub(crate) fn missing() -> Name {
56 Name::new_text("[missing name]".into())
57 }
58
59 pub(crate) fn as_tuple_index(&self) -> Option<usize> {
60 match self.0 {
61 Repr::TupleField(idx) => Some(idx),
62 _ => None,
63 }
64 }
65}
66
67pub(crate) trait AsName {
68 fn as_name(&self) -> Name;
69}
70
71impl AsName for ast::NameRef {
72 fn as_name(&self) -> Name {
73 match self.as_tuple_field() {
74 Some(idx) => Name::new_tuple_field(idx),
75 None => Name::resolve(self.text()),
76 }
77 }
78}
79
80impl AsName for ast::Name {
81 fn as_name(&self) -> Name {
82 Name::resolve(self.text())
83 }
84}
85
86impl AsName for ast::FieldKind {
87 fn as_name(&self) -> Name {
88 match self {
89 ast::FieldKind::Name(nr) => nr.as_name(),
90 ast::FieldKind::Index(idx) => Name::new_tuple_field(idx.text().parse().unwrap()),
91 }
92 }
93}
94
95impl AsName for ra_db::Dependency {
96 fn as_name(&self) -> Name {
97 Name::new_text(self.name.clone())
98 }
99}
100
101// Primitives
102pub(crate) const ISIZE: Name = Name::new_inline_ascii(5, b"isize");
103pub(crate) const I8: Name = Name::new_inline_ascii(2, b"i8");
104pub(crate) const I16: Name = Name::new_inline_ascii(3, b"i16");
105pub(crate) const I32: Name = Name::new_inline_ascii(3, b"i32");
106pub(crate) const I64: Name = Name::new_inline_ascii(3, b"i64");
107pub(crate) const I128: Name = Name::new_inline_ascii(4, b"i128");
108pub(crate) const USIZE: Name = Name::new_inline_ascii(5, b"usize");
109pub(crate) const U8: Name = Name::new_inline_ascii(2, b"u8");
110pub(crate) const U16: Name = Name::new_inline_ascii(3, b"u16");
111pub(crate) const U32: Name = Name::new_inline_ascii(3, b"u32");
112pub(crate) const U64: Name = Name::new_inline_ascii(3, b"u64");
113pub(crate) const U128: Name = Name::new_inline_ascii(4, b"u128");
114pub(crate) const F32: Name = Name::new_inline_ascii(3, b"f32");
115pub(crate) const F64: Name = Name::new_inline_ascii(3, b"f64");
116pub(crate) const BOOL: Name = Name::new_inline_ascii(4, b"bool");
117pub(crate) const CHAR: Name = Name::new_inline_ascii(4, b"char");
118pub(crate) const STR: Name = Name::new_inline_ascii(3, b"str");
119
120// Special names
121pub(crate) const SELF_PARAM: Name = Name::new_inline_ascii(4, b"self");
122pub(crate) const SELF_TYPE: Name = Name::new_inline_ascii(4, b"Self");
123pub(crate) const MACRO_RULES: Name = Name::new_inline_ascii(11, b"macro_rules");
124
125// Components of known path (value or mod name)
126pub(crate) const STD: Name = Name::new_inline_ascii(3, b"std");
127pub(crate) const ITER: Name = Name::new_inline_ascii(4, b"iter");
128pub(crate) const OPS: Name = Name::new_inline_ascii(3, b"ops");
129pub(crate) const FUTURE: Name = Name::new_inline_ascii(6, b"future");
130pub(crate) const RESULT: Name = Name::new_inline_ascii(6, b"result");
131pub(crate) const BOXED: Name = Name::new_inline_ascii(5, b"boxed");
132
133// Components of known path (type name)
134pub(crate) const INTO_ITERATOR_TYPE: Name = Name::new_inline_ascii(12, b"IntoIterator");
135pub(crate) const ITEM_TYPE: Name = Name::new_inline_ascii(4, b"Item");
136pub(crate) const TRY_TYPE: Name = Name::new_inline_ascii(3, b"Try");
137pub(crate) const OK_TYPE: Name = Name::new_inline_ascii(2, b"Ok");
138pub(crate) const FUTURE_TYPE: Name = Name::new_inline_ascii(6, b"Future");
139pub(crate) const RESULT_TYPE: Name = Name::new_inline_ascii(6, b"Result");
140pub(crate) const OUTPUT_TYPE: Name = Name::new_inline_ascii(6, b"Output");
141pub(crate) const TARGET_TYPE: Name = Name::new_inline_ascii(6, b"Target");
142pub(crate) const BOX_TYPE: Name = Name::new_inline_ascii(3, b"Box");