diff options
Diffstat (limited to 'crates/ra_hir_expand/src/lib.rs')
-rw-r--r-- | crates/ra_hir_expand/src/lib.rs | 59 |
1 files changed, 52 insertions, 7 deletions
diff --git a/crates/ra_hir_expand/src/lib.rs b/crates/ra_hir_expand/src/lib.rs index 3a1c6d2b0..3fce73e8a 100644 --- a/crates/ra_hir_expand/src/lib.rs +++ b/crates/ra_hir_expand/src/lib.rs | |||
@@ -12,6 +12,7 @@ pub mod diagnostics; | |||
12 | pub mod builtin_derive; | 12 | pub mod builtin_derive; |
13 | pub mod builtin_macro; | 13 | pub mod builtin_macro; |
14 | pub mod quote; | 14 | pub mod quote; |
15 | pub mod eager; | ||
15 | 16 | ||
16 | use std::hash::Hash; | 17 | use std::hash::Hash; |
17 | use std::sync::Arc; | 18 | use std::sync::Arc; |
@@ -25,7 +26,7 @@ use ra_syntax::{ | |||
25 | 26 | ||
26 | use crate::ast_id_map::FileAstId; | 27 | use crate::ast_id_map::FileAstId; |
27 | use crate::builtin_derive::BuiltinDeriveExpander; | 28 | use crate::builtin_derive::BuiltinDeriveExpander; |
28 | use crate::builtin_macro::BuiltinFnLikeExpander; | 29 | use crate::builtin_macro::{BuiltinFnLikeExpander, EagerExpander}; |
29 | 30 | ||
30 | #[cfg(test)] | 31 | #[cfg(test)] |
31 | mod test_db; | 32 | mod test_db; |
@@ -70,11 +71,17 @@ impl HirFileId { | |||
70 | match self.0 { | 71 | match self.0 { |
71 | HirFileIdRepr::FileId(file_id) => file_id, | 72 | HirFileIdRepr::FileId(file_id) => file_id, |
72 | HirFileIdRepr::MacroFile(macro_file) => { | 73 | HirFileIdRepr::MacroFile(macro_file) => { |
73 | let lazy_id = match macro_file.macro_call_id { | 74 | let file_id = match macro_file.macro_call_id { |
74 | MacroCallId::LazyMacro(id) => id, | 75 | MacroCallId::LazyMacro(id) => { |
76 | let loc = db.lookup_intern_macro(id); | ||
77 | loc.kind.file_id() | ||
78 | } | ||
79 | MacroCallId::EagerMacro(id) => { | ||
80 | let loc = db.lookup_intern_eager_expansion(id); | ||
81 | loc.file_id | ||
82 | } | ||
75 | }; | 83 | }; |
76 | let loc = db.lookup_intern_macro(lazy_id); | 84 | file_id.original_file(db) |
77 | loc.kind.file_id().original_file(db) | ||
78 | } | 85 | } |
79 | } | 86 | } |
80 | } | 87 | } |
@@ -86,6 +93,10 @@ impl HirFileId { | |||
86 | HirFileIdRepr::MacroFile(macro_file) => { | 93 | HirFileIdRepr::MacroFile(macro_file) => { |
87 | let lazy_id = match macro_file.macro_call_id { | 94 | let lazy_id = match macro_file.macro_call_id { |
88 | MacroCallId::LazyMacro(id) => id, | 95 | MacroCallId::LazyMacro(id) => id, |
96 | MacroCallId::EagerMacro(_id) => { | ||
97 | // FIXME: handle call node for eager macro | ||
98 | return None; | ||
99 | } | ||
89 | }; | 100 | }; |
90 | let loc = db.lookup_intern_macro(lazy_id); | 101 | let loc = db.lookup_intern_macro(lazy_id); |
91 | Some(loc.kind.node(db)) | 102 | Some(loc.kind.node(db)) |
@@ -100,6 +111,10 @@ impl HirFileId { | |||
100 | HirFileIdRepr::MacroFile(macro_file) => { | 111 | HirFileIdRepr::MacroFile(macro_file) => { |
101 | let lazy_id = match macro_file.macro_call_id { | 112 | let lazy_id = match macro_file.macro_call_id { |
102 | MacroCallId::LazyMacro(id) => id, | 113 | MacroCallId::LazyMacro(id) => id, |
114 | MacroCallId::EagerMacro(_id) => { | ||
115 | // FIXME: handle expansion_info for eager macro | ||
116 | return None; | ||
117 | } | ||
103 | }; | 118 | }; |
104 | let loc: MacroCallLoc = db.lookup_intern_macro(lazy_id); | 119 | let loc: MacroCallLoc = db.lookup_intern_macro(lazy_id); |
105 | 120 | ||
@@ -129,6 +144,9 @@ impl HirFileId { | |||
129 | HirFileIdRepr::MacroFile(macro_file) => { | 144 | HirFileIdRepr::MacroFile(macro_file) => { |
130 | let lazy_id = match macro_file.macro_call_id { | 145 | let lazy_id = match macro_file.macro_call_id { |
131 | MacroCallId::LazyMacro(id) => id, | 146 | MacroCallId::LazyMacro(id) => id, |
147 | MacroCallId::EagerMacro(_id) => { | ||
148 | return None; | ||
149 | } | ||
132 | }; | 150 | }; |
133 | let loc: MacroCallLoc = db.lookup_intern_macro(lazy_id); | 151 | let loc: MacroCallLoc = db.lookup_intern_macro(lazy_id); |
134 | let item = match loc.def.kind { | 152 | let item = match loc.def.kind { |
@@ -151,6 +169,7 @@ pub struct MacroFile { | |||
151 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | 169 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
152 | pub enum MacroCallId { | 170 | pub enum MacroCallId { |
153 | LazyMacro(LazyMacroId), | 171 | LazyMacro(LazyMacroId), |
172 | EagerMacro(EagerMacroId), | ||
154 | } | 173 | } |
155 | 174 | ||
156 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | 175 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
@@ -164,11 +183,27 @@ impl salsa::InternKey for LazyMacroId { | |||
164 | } | 183 | } |
165 | } | 184 | } |
166 | 185 | ||
186 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | ||
187 | pub struct EagerMacroId(salsa::InternId); | ||
188 | impl salsa::InternKey for EagerMacroId { | ||
189 | fn from_intern_id(v: salsa::InternId) -> Self { | ||
190 | EagerMacroId(v) | ||
191 | } | ||
192 | fn as_intern_id(&self) -> salsa::InternId { | ||
193 | self.0 | ||
194 | } | ||
195 | } | ||
196 | |||
167 | impl From<LazyMacroId> for MacroCallId { | 197 | impl From<LazyMacroId> for MacroCallId { |
168 | fn from(it: LazyMacroId) -> Self { | 198 | fn from(it: LazyMacroId) -> Self { |
169 | MacroCallId::LazyMacro(it) | 199 | MacroCallId::LazyMacro(it) |
170 | } | 200 | } |
171 | } | 201 | } |
202 | impl From<EagerMacroId> for MacroCallId { | ||
203 | fn from(it: EagerMacroId) -> Self { | ||
204 | MacroCallId::EagerMacro(it) | ||
205 | } | ||
206 | } | ||
172 | 207 | ||
173 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | 208 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
174 | pub struct MacroDefId { | 209 | pub struct MacroDefId { |
@@ -184,8 +219,8 @@ pub struct MacroDefId { | |||
184 | } | 219 | } |
185 | 220 | ||
186 | impl MacroDefId { | 221 | impl MacroDefId { |
187 | pub fn as_call_id(self, db: &dyn db::AstDatabase, kind: MacroCallKind) -> MacroCallId { | 222 | pub fn as_lazy_macro(self, db: &dyn db::AstDatabase, kind: MacroCallKind) -> LazyMacroId { |
188 | db.intern_macro(MacroCallLoc { def: self, kind }).into() | 223 | db.intern_macro(MacroCallLoc { def: self, kind }) |
189 | } | 224 | } |
190 | } | 225 | } |
191 | 226 | ||
@@ -195,6 +230,7 @@ pub enum MacroDefKind { | |||
195 | BuiltIn(BuiltinFnLikeExpander), | 230 | BuiltIn(BuiltinFnLikeExpander), |
196 | // FIXME: maybe just Builtin and rename BuiltinFnLikeExpander to BuiltinExpander | 231 | // FIXME: maybe just Builtin and rename BuiltinFnLikeExpander to BuiltinExpander |
197 | BuiltInDerive(BuiltinDeriveExpander), | 232 | BuiltInDerive(BuiltinDeriveExpander), |
233 | BuiltInEager(EagerExpander), | ||
198 | } | 234 | } |
199 | 235 | ||
200 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | 236 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] |
@@ -240,6 +276,14 @@ impl MacroCallId { | |||
240 | } | 276 | } |
241 | } | 277 | } |
242 | 278 | ||
279 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
280 | pub struct EagerCallLoc { | ||
281 | pub(crate) def: MacroDefId, | ||
282 | pub(crate) fragment: FragmentKind, | ||
283 | pub(crate) subtree: Arc<tt::Subtree>, | ||
284 | pub(crate) file_id: HirFileId, | ||
285 | } | ||
286 | |||
243 | /// ExpansionInfo mainly describes how to map text range between src and expanded macro | 287 | /// ExpansionInfo mainly describes how to map text range between src and expanded macro |
244 | #[derive(Debug, Clone, PartialEq, Eq)] | 288 | #[derive(Debug, Clone, PartialEq, Eq)] |
245 | pub struct ExpansionInfo { | 289 | pub struct ExpansionInfo { |
@@ -253,6 +297,7 @@ pub struct ExpansionInfo { | |||
253 | } | 297 | } |
254 | 298 | ||
255 | pub use mbe::Origin; | 299 | pub use mbe::Origin; |
300 | use ra_parser::FragmentKind; | ||
256 | 301 | ||
257 | impl ExpansionInfo { | 302 | impl ExpansionInfo { |
258 | pub fn call_node(&self) -> Option<InFile<SyntaxNode>> { | 303 | pub fn call_node(&self) -> Option<InFile<SyntaxNode>> { |