diff options
Diffstat (limited to 'crates/ra_hir_expand/src')
-rw-r--r-- | crates/ra_hir_expand/src/either.rs | 54 | ||||
-rw-r--r-- | crates/ra_hir_expand/src/hygiene.rs | 46 | ||||
-rw-r--r-- | crates/ra_hir_expand/src/lib.rs | 14 | ||||
-rw-r--r-- | crates/ra_hir_expand/src/name.rs | 142 |
4 files changed, 245 insertions, 11 deletions
diff --git a/crates/ra_hir_expand/src/either.rs b/crates/ra_hir_expand/src/either.rs new file mode 100644 index 000000000..83583ef8b --- /dev/null +++ b/crates/ra_hir_expand/src/either.rs | |||
@@ -0,0 +1,54 @@ | |||
1 | //! FIXME: write short doc here | ||
2 | |||
3 | #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] | ||
4 | pub enum Either<A, B> { | ||
5 | A(A), | ||
6 | B(B), | ||
7 | } | ||
8 | |||
9 | impl<A, B> Either<A, B> { | ||
10 | pub fn either<R, F1, F2>(self, f1: F1, f2: F2) -> R | ||
11 | where | ||
12 | F1: FnOnce(A) -> R, | ||
13 | F2: FnOnce(B) -> R, | ||
14 | { | ||
15 | match self { | ||
16 | Either::A(a) => f1(a), | ||
17 | Either::B(b) => f2(b), | ||
18 | } | ||
19 | } | ||
20 | pub fn map<U, V, F1, F2>(self, f1: F1, f2: F2) -> Either<U, V> | ||
21 | where | ||
22 | F1: FnOnce(A) -> U, | ||
23 | F2: FnOnce(B) -> V, | ||
24 | { | ||
25 | match self { | ||
26 | Either::A(a) => Either::A(f1(a)), | ||
27 | Either::B(b) => Either::B(f2(b)), | ||
28 | } | ||
29 | } | ||
30 | pub fn map_a<U, F>(self, f: F) -> Either<U, B> | ||
31 | where | ||
32 | F: FnOnce(A) -> U, | ||
33 | { | ||
34 | self.map(f, |it| it) | ||
35 | } | ||
36 | pub fn a(self) -> Option<A> { | ||
37 | match self { | ||
38 | Either::A(it) => Some(it), | ||
39 | Either::B(_) => None, | ||
40 | } | ||
41 | } | ||
42 | pub fn b(self) -> Option<B> { | ||
43 | match self { | ||
44 | Either::A(_) => None, | ||
45 | Either::B(it) => Some(it), | ||
46 | } | ||
47 | } | ||
48 | pub fn as_ref(&self) -> Either<&A, &B> { | ||
49 | match self { | ||
50 | Either::A(it) => Either::A(it), | ||
51 | Either::B(it) => Either::B(it), | ||
52 | } | ||
53 | } | ||
54 | } | ||
diff --git a/crates/ra_hir_expand/src/hygiene.rs b/crates/ra_hir_expand/src/hygiene.rs new file mode 100644 index 000000000..77428ec99 --- /dev/null +++ b/crates/ra_hir_expand/src/hygiene.rs | |||
@@ -0,0 +1,46 @@ | |||
1 | //! This modules handles hygiene information. | ||
2 | //! | ||
3 | //! Specifically, `ast` + `Hygiene` allows you to create a `Name`. Note that, at | ||
4 | //! this moment, this is horribly incomplete and handles only `$crate`. | ||
5 | use ra_db::CrateId; | ||
6 | use ra_syntax::ast; | ||
7 | |||
8 | use crate::{ | ||
9 | db::AstDatabase, | ||
10 | either::Either, | ||
11 | name::{AsName, Name}, | ||
12 | HirFileId, HirFileIdRepr, | ||
13 | }; | ||
14 | |||
15 | #[derive(Debug)] | ||
16 | pub struct Hygiene { | ||
17 | // This is what `$crate` expands to | ||
18 | def_crate: Option<CrateId>, | ||
19 | } | ||
20 | |||
21 | impl Hygiene { | ||
22 | pub fn new(db: &impl AstDatabase, file_id: HirFileId) -> Hygiene { | ||
23 | let def_crate = match file_id.0 { | ||
24 | HirFileIdRepr::FileId(_) => None, | ||
25 | HirFileIdRepr::MacroFile(macro_file) => { | ||
26 | let loc = db.lookup_intern_macro(macro_file.macro_call_id); | ||
27 | Some(loc.def.krate) | ||
28 | } | ||
29 | }; | ||
30 | Hygiene { def_crate } | ||
31 | } | ||
32 | |||
33 | pub fn new_unhygienic() -> Hygiene { | ||
34 | Hygiene { def_crate: None } | ||
35 | } | ||
36 | |||
37 | // FIXME: this should just return name | ||
38 | pub fn name_ref_to_name(&self, name_ref: ast::NameRef) -> Either<Name, CrateId> { | ||
39 | if let Some(def_crate) = self.def_crate { | ||
40 | if name_ref.text() == "$crate" { | ||
41 | return Either::B(def_crate); | ||
42 | } | ||
43 | } | ||
44 | Either::A(name_ref.as_name()) | ||
45 | } | ||
46 | } | ||
diff --git a/crates/ra_hir_expand/src/lib.rs b/crates/ra_hir_expand/src/lib.rs index 3c0ef8f1c..5a0e5a19c 100644 --- a/crates/ra_hir_expand/src/lib.rs +++ b/crates/ra_hir_expand/src/lib.rs | |||
@@ -6,6 +6,9 @@ | |||
6 | 6 | ||
7 | pub mod db; | 7 | pub mod db; |
8 | pub mod ast_id_map; | 8 | pub mod ast_id_map; |
9 | pub mod either; | ||
10 | pub mod name; | ||
11 | pub mod hygiene; | ||
9 | 12 | ||
10 | use std::hash::{Hash, Hasher}; | 13 | use std::hash::{Hash, Hasher}; |
11 | 14 | ||
@@ -59,17 +62,6 @@ impl HirFileId { | |||
59 | } | 62 | } |
60 | } | 63 | } |
61 | } | 64 | } |
62 | |||
63 | /// Get the crate which the macro lives in, if it is a macro file. | ||
64 | pub fn macro_crate(self, db: &dyn db::AstDatabase) -> Option<CrateId> { | ||
65 | match self.0 { | ||
66 | HirFileIdRepr::FileId(_) => None, | ||
67 | HirFileIdRepr::MacroFile(macro_file) => { | ||
68 | let loc = db.lookup_intern_macro(macro_file.macro_call_id); | ||
69 | Some(loc.def.krate) | ||
70 | } | ||
71 | } | ||
72 | } | ||
73 | } | 65 | } |
74 | 66 | ||
75 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | 67 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
diff --git a/crates/ra_hir_expand/src/name.rs b/crates/ra_hir_expand/src/name.rs new file mode 100644 index 000000000..720896ee8 --- /dev/null +++ b/crates/ra_hir_expand/src/name.rs | |||
@@ -0,0 +1,142 @@ | |||
1 | //! FIXME: write short doc here | ||
2 | |||
3 | use std::fmt; | ||
4 | |||
5 | use 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)] | ||
11 | pub struct Name(Repr); | ||
12 | |||
13 | #[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)] | ||
14 | enum Repr { | ||
15 | Text(SmolStr), | ||
16 | TupleField(usize), | ||
17 | } | ||
18 | |||
19 | impl 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 | |||
28 | impl 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 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 fn missing() -> Name { | ||
56 | Name::new_text("[missing name]".into()) | ||
57 | } | ||
58 | |||
59 | pub fn as_tuple_index(&self) -> Option<usize> { | ||
60 | match self.0 { | ||
61 | Repr::TupleField(idx) => Some(idx), | ||
62 | _ => None, | ||
63 | } | ||
64 | } | ||
65 | } | ||
66 | |||
67 | pub trait AsName { | ||
68 | fn as_name(&self) -> Name; | ||
69 | } | ||
70 | |||
71 | impl 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 | |||
80 | impl AsName for ast::Name { | ||
81 | fn as_name(&self) -> Name { | ||
82 | Name::resolve(self.text()) | ||
83 | } | ||
84 | } | ||
85 | |||
86 | impl 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 | |||
95 | impl AsName for ra_db::Dependency { | ||
96 | fn as_name(&self) -> Name { | ||
97 | Name::new_text(self.name.clone()) | ||
98 | } | ||
99 | } | ||
100 | |||
101 | // Primitives | ||
102 | pub const ISIZE: Name = Name::new_inline_ascii(5, b"isize"); | ||
103 | pub const I8: Name = Name::new_inline_ascii(2, b"i8"); | ||
104 | pub const I16: Name = Name::new_inline_ascii(3, b"i16"); | ||
105 | pub const I32: Name = Name::new_inline_ascii(3, b"i32"); | ||
106 | pub const I64: Name = Name::new_inline_ascii(3, b"i64"); | ||
107 | pub const I128: Name = Name::new_inline_ascii(4, b"i128"); | ||
108 | pub const USIZE: Name = Name::new_inline_ascii(5, b"usize"); | ||
109 | pub const U8: Name = Name::new_inline_ascii(2, b"u8"); | ||
110 | pub const U16: Name = Name::new_inline_ascii(3, b"u16"); | ||
111 | pub const U32: Name = Name::new_inline_ascii(3, b"u32"); | ||
112 | pub const U64: Name = Name::new_inline_ascii(3, b"u64"); | ||
113 | pub const U128: Name = Name::new_inline_ascii(4, b"u128"); | ||
114 | pub const F32: Name = Name::new_inline_ascii(3, b"f32"); | ||
115 | pub const F64: Name = Name::new_inline_ascii(3, b"f64"); | ||
116 | pub const BOOL: Name = Name::new_inline_ascii(4, b"bool"); | ||
117 | pub const CHAR: Name = Name::new_inline_ascii(4, b"char"); | ||
118 | pub const STR: Name = Name::new_inline_ascii(3, b"str"); | ||
119 | |||
120 | // Special names | ||
121 | pub const SELF_PARAM: Name = Name::new_inline_ascii(4, b"self"); | ||
122 | pub const SELF_TYPE: Name = Name::new_inline_ascii(4, b"Self"); | ||
123 | pub const MACRO_RULES: Name = Name::new_inline_ascii(11, b"macro_rules"); | ||
124 | |||
125 | // Components of known path (value or mod name) | ||
126 | pub const STD: Name = Name::new_inline_ascii(3, b"std"); | ||
127 | pub const ITER: Name = Name::new_inline_ascii(4, b"iter"); | ||
128 | pub const OPS: Name = Name::new_inline_ascii(3, b"ops"); | ||
129 | pub const FUTURE: Name = Name::new_inline_ascii(6, b"future"); | ||
130 | pub const RESULT: Name = Name::new_inline_ascii(6, b"result"); | ||
131 | pub const BOXED: Name = Name::new_inline_ascii(5, b"boxed"); | ||
132 | |||
133 | // Components of known path (type name) | ||
134 | pub const INTO_ITERATOR_TYPE: Name = Name::new_inline_ascii(12, b"IntoIterator"); | ||
135 | pub const ITEM_TYPE: Name = Name::new_inline_ascii(4, b"Item"); | ||
136 | pub const TRY_TYPE: Name = Name::new_inline_ascii(3, b"Try"); | ||
137 | pub const OK_TYPE: Name = Name::new_inline_ascii(2, b"Ok"); | ||
138 | pub const FUTURE_TYPE: Name = Name::new_inline_ascii(6, b"Future"); | ||
139 | pub const RESULT_TYPE: Name = Name::new_inline_ascii(6, b"Result"); | ||
140 | pub const OUTPUT_TYPE: Name = Name::new_inline_ascii(6, b"Output"); | ||
141 | pub const TARGET_TYPE: Name = Name::new_inline_ascii(6, b"Target"); | ||
142 | pub const BOX_TYPE: Name = Name::new_inline_ascii(3, b"Box"); | ||