diff options
Diffstat (limited to 'crates/ra_hir_expand/src/lib.rs')
-rw-r--r-- | crates/ra_hir_expand/src/lib.rs | 452 |
1 files changed, 0 insertions, 452 deletions
diff --git a/crates/ra_hir_expand/src/lib.rs b/crates/ra_hir_expand/src/lib.rs deleted file mode 100644 index 2e8d63691..000000000 --- a/crates/ra_hir_expand/src/lib.rs +++ /dev/null | |||
@@ -1,452 +0,0 @@ | |||
1 | //! `ra_hir_expand` deals with macro expansion. | ||
2 | //! | ||
3 | //! Specifically, it implements a concept of `MacroFile` -- a file whose syntax | ||
4 | //! tree originates not from the text of some `FileId`, but from some macro | ||
5 | //! expansion. | ||
6 | |||
7 | pub mod db; | ||
8 | pub mod ast_id_map; | ||
9 | pub mod name; | ||
10 | pub mod hygiene; | ||
11 | pub mod diagnostics; | ||
12 | pub mod builtin_derive; | ||
13 | pub mod builtin_macro; | ||
14 | pub mod proc_macro; | ||
15 | pub mod quote; | ||
16 | pub mod eager; | ||
17 | |||
18 | use std::hash::Hash; | ||
19 | use std::sync::Arc; | ||
20 | |||
21 | use ra_db::{impl_intern_key, salsa, CrateId, FileId}; | ||
22 | use ra_syntax::{ | ||
23 | algo, | ||
24 | ast::{self, AstNode}, | ||
25 | SyntaxNode, SyntaxToken, TextSize, | ||
26 | }; | ||
27 | |||
28 | use crate::ast_id_map::FileAstId; | ||
29 | use crate::builtin_derive::BuiltinDeriveExpander; | ||
30 | use crate::builtin_macro::{BuiltinFnLikeExpander, EagerExpander}; | ||
31 | use crate::proc_macro::ProcMacroExpander; | ||
32 | |||
33 | #[cfg(test)] | ||
34 | mod test_db; | ||
35 | |||
36 | /// Input to the analyzer is a set of files, where each file is identified by | ||
37 | /// `FileId` and contains source code. However, another source of source code in | ||
38 | /// Rust are macros: each macro can be thought of as producing a "temporary | ||
39 | /// file". To assign an id to such a file, we use the id of the macro call that | ||
40 | /// produced the file. So, a `HirFileId` is either a `FileId` (source code | ||
41 | /// written by user), or a `MacroCallId` (source code produced by macro). | ||
42 | /// | ||
43 | /// What is a `MacroCallId`? Simplifying, it's a `HirFileId` of a file | ||
44 | /// containing the call plus the offset of the macro call in the file. Note that | ||
45 | /// this is a recursive definition! However, the size_of of `HirFileId` is | ||
46 | /// finite (because everything bottoms out at the real `FileId`) and small | ||
47 | /// (`MacroCallId` uses the location interner). | ||
48 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | ||
49 | pub struct HirFileId(HirFileIdRepr); | ||
50 | |||
51 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | ||
52 | enum HirFileIdRepr { | ||
53 | FileId(FileId), | ||
54 | MacroFile(MacroFile), | ||
55 | } | ||
56 | |||
57 | impl From<FileId> for HirFileId { | ||
58 | fn from(id: FileId) -> Self { | ||
59 | HirFileId(HirFileIdRepr::FileId(id)) | ||
60 | } | ||
61 | } | ||
62 | |||
63 | impl From<MacroFile> for HirFileId { | ||
64 | fn from(id: MacroFile) -> Self { | ||
65 | HirFileId(HirFileIdRepr::MacroFile(id)) | ||
66 | } | ||
67 | } | ||
68 | |||
69 | impl HirFileId { | ||
70 | /// For macro-expansion files, returns the file original source file the | ||
71 | /// expansion originated from. | ||
72 | pub fn original_file(self, db: &dyn db::AstDatabase) -> FileId { | ||
73 | match self.0 { | ||
74 | HirFileIdRepr::FileId(file_id) => file_id, | ||
75 | HirFileIdRepr::MacroFile(macro_file) => { | ||
76 | let file_id = match macro_file.macro_call_id { | ||
77 | MacroCallId::LazyMacro(id) => { | ||
78 | let loc = db.lookup_intern_macro(id); | ||
79 | loc.kind.file_id() | ||
80 | } | ||
81 | MacroCallId::EagerMacro(id) => { | ||
82 | let loc = db.lookup_intern_eager_expansion(id); | ||
83 | loc.file_id | ||
84 | } | ||
85 | }; | ||
86 | file_id.original_file(db) | ||
87 | } | ||
88 | } | ||
89 | } | ||
90 | |||
91 | pub fn expansion_level(self, db: &dyn db::AstDatabase) -> u32 { | ||
92 | let mut level = 0; | ||
93 | let mut curr = self; | ||
94 | while let HirFileIdRepr::MacroFile(macro_file) = curr.0 { | ||
95 | level += 1; | ||
96 | curr = match macro_file.macro_call_id { | ||
97 | MacroCallId::LazyMacro(id) => { | ||
98 | let loc = db.lookup_intern_macro(id); | ||
99 | loc.kind.file_id() | ||
100 | } | ||
101 | MacroCallId::EagerMacro(id) => { | ||
102 | let loc = db.lookup_intern_eager_expansion(id); | ||
103 | loc.file_id | ||
104 | } | ||
105 | }; | ||
106 | } | ||
107 | level | ||
108 | } | ||
109 | |||
110 | /// If this is a macro call, returns the syntax node of the call. | ||
111 | pub fn call_node(self, db: &dyn db::AstDatabase) -> Option<InFile<SyntaxNode>> { | ||
112 | match self.0 { | ||
113 | HirFileIdRepr::FileId(_) => None, | ||
114 | HirFileIdRepr::MacroFile(macro_file) => { | ||
115 | let lazy_id = match macro_file.macro_call_id { | ||
116 | MacroCallId::LazyMacro(id) => id, | ||
117 | MacroCallId::EagerMacro(_id) => { | ||
118 | // FIXME: handle call node for eager macro | ||
119 | return None; | ||
120 | } | ||
121 | }; | ||
122 | let loc = db.lookup_intern_macro(lazy_id); | ||
123 | Some(loc.kind.node(db)) | ||
124 | } | ||
125 | } | ||
126 | } | ||
127 | |||
128 | /// Return expansion information if it is a macro-expansion file | ||
129 | pub fn expansion_info(self, db: &dyn db::AstDatabase) -> Option<ExpansionInfo> { | ||
130 | match self.0 { | ||
131 | HirFileIdRepr::FileId(_) => None, | ||
132 | HirFileIdRepr::MacroFile(macro_file) => { | ||
133 | let lazy_id = match macro_file.macro_call_id { | ||
134 | MacroCallId::LazyMacro(id) => id, | ||
135 | MacroCallId::EagerMacro(_id) => { | ||
136 | // FIXME: handle expansion_info for eager macro | ||
137 | return None; | ||
138 | } | ||
139 | }; | ||
140 | let loc: MacroCallLoc = db.lookup_intern_macro(lazy_id); | ||
141 | |||
142 | let arg_tt = loc.kind.arg(db)?; | ||
143 | let def_tt = loc.def.ast_id?.to_node(db).token_tree()?; | ||
144 | |||
145 | let macro_def = db.macro_def(loc.def)?; | ||
146 | let (parse, exp_map) = db.parse_macro(macro_file)?; | ||
147 | let macro_arg = db.macro_arg(macro_file.macro_call_id)?; | ||
148 | |||
149 | Some(ExpansionInfo { | ||
150 | expanded: InFile::new(self, parse.syntax_node()), | ||
151 | arg: InFile::new(loc.kind.file_id(), arg_tt), | ||
152 | def: InFile::new(loc.def.ast_id?.file_id, def_tt), | ||
153 | macro_arg, | ||
154 | macro_def, | ||
155 | exp_map, | ||
156 | }) | ||
157 | } | ||
158 | } | ||
159 | } | ||
160 | |||
161 | /// Indicate it is macro file generated for builtin derive | ||
162 | pub fn is_builtin_derive(&self, db: &dyn db::AstDatabase) -> Option<InFile<ast::Item>> { | ||
163 | match self.0 { | ||
164 | HirFileIdRepr::FileId(_) => None, | ||
165 | HirFileIdRepr::MacroFile(macro_file) => { | ||
166 | let lazy_id = match macro_file.macro_call_id { | ||
167 | MacroCallId::LazyMacro(id) => id, | ||
168 | MacroCallId::EagerMacro(_id) => { | ||
169 | return None; | ||
170 | } | ||
171 | }; | ||
172 | let loc: MacroCallLoc = db.lookup_intern_macro(lazy_id); | ||
173 | let item = match loc.def.kind { | ||
174 | MacroDefKind::BuiltInDerive(_) => loc.kind.node(db), | ||
175 | _ => return None, | ||
176 | }; | ||
177 | Some(item.with_value(ast::Item::cast(item.value.clone())?)) | ||
178 | } | ||
179 | } | ||
180 | } | ||
181 | } | ||
182 | |||
183 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | ||
184 | pub struct MacroFile { | ||
185 | macro_call_id: MacroCallId, | ||
186 | } | ||
187 | |||
188 | /// `MacroCallId` identifies a particular macro invocation, like | ||
189 | /// `println!("Hello, {}", world)`. | ||
190 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | ||
191 | pub enum MacroCallId { | ||
192 | LazyMacro(LazyMacroId), | ||
193 | EagerMacro(EagerMacroId), | ||
194 | } | ||
195 | |||
196 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | ||
197 | pub struct LazyMacroId(salsa::InternId); | ||
198 | impl_intern_key!(LazyMacroId); | ||
199 | |||
200 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | ||
201 | pub struct EagerMacroId(salsa::InternId); | ||
202 | impl_intern_key!(EagerMacroId); | ||
203 | |||
204 | impl From<LazyMacroId> for MacroCallId { | ||
205 | fn from(it: LazyMacroId) -> Self { | ||
206 | MacroCallId::LazyMacro(it) | ||
207 | } | ||
208 | } | ||
209 | impl From<EagerMacroId> for MacroCallId { | ||
210 | fn from(it: EagerMacroId) -> Self { | ||
211 | MacroCallId::EagerMacro(it) | ||
212 | } | ||
213 | } | ||
214 | |||
215 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | ||
216 | pub struct MacroDefId { | ||
217 | // FIXME: krate and ast_id are currently optional because we don't have a | ||
218 | // definition location for built-in derives. There is one, though: the | ||
219 | // standard library defines them. The problem is that it uses the new | ||
220 | // `macro` syntax for this, which we don't support yet. As soon as we do | ||
221 | // (which will probably require touching this code), we can instead use | ||
222 | // that (and also remove the hacks for resolving built-in derives). | ||
223 | pub krate: Option<CrateId>, | ||
224 | pub ast_id: Option<AstId<ast::MacroCall>>, | ||
225 | pub kind: MacroDefKind, | ||
226 | |||
227 | pub local_inner: bool, | ||
228 | } | ||
229 | |||
230 | impl MacroDefId { | ||
231 | pub fn as_lazy_macro( | ||
232 | self, | ||
233 | db: &dyn db::AstDatabase, | ||
234 | krate: CrateId, | ||
235 | kind: MacroCallKind, | ||
236 | ) -> LazyMacroId { | ||
237 | db.intern_macro(MacroCallLoc { def: self, krate, kind }) | ||
238 | } | ||
239 | } | ||
240 | |||
241 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | ||
242 | pub enum MacroDefKind { | ||
243 | Declarative, | ||
244 | BuiltIn(BuiltinFnLikeExpander), | ||
245 | // FIXME: maybe just Builtin and rename BuiltinFnLikeExpander to BuiltinExpander | ||
246 | BuiltInDerive(BuiltinDeriveExpander), | ||
247 | BuiltInEager(EagerExpander), | ||
248 | CustomDerive(ProcMacroExpander), | ||
249 | } | ||
250 | |||
251 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
252 | pub struct MacroCallLoc { | ||
253 | pub(crate) def: MacroDefId, | ||
254 | pub(crate) krate: CrateId, | ||
255 | pub(crate) kind: MacroCallKind, | ||
256 | } | ||
257 | |||
258 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
259 | pub enum MacroCallKind { | ||
260 | FnLike(AstId<ast::MacroCall>), | ||
261 | Attr(AstId<ast::Item>, String), | ||
262 | } | ||
263 | |||
264 | impl MacroCallKind { | ||
265 | fn file_id(&self) -> HirFileId { | ||
266 | match self { | ||
267 | MacroCallKind::FnLike(ast_id) => ast_id.file_id, | ||
268 | MacroCallKind::Attr(ast_id, _) => ast_id.file_id, | ||
269 | } | ||
270 | } | ||
271 | |||
272 | fn node(&self, db: &dyn db::AstDatabase) -> InFile<SyntaxNode> { | ||
273 | match self { | ||
274 | MacroCallKind::FnLike(ast_id) => ast_id.with_value(ast_id.to_node(db).syntax().clone()), | ||
275 | MacroCallKind::Attr(ast_id, _) => { | ||
276 | ast_id.with_value(ast_id.to_node(db).syntax().clone()) | ||
277 | } | ||
278 | } | ||
279 | } | ||
280 | |||
281 | fn arg(&self, db: &dyn db::AstDatabase) -> Option<SyntaxNode> { | ||
282 | match self { | ||
283 | MacroCallKind::FnLike(ast_id) => { | ||
284 | Some(ast_id.to_node(db).token_tree()?.syntax().clone()) | ||
285 | } | ||
286 | MacroCallKind::Attr(ast_id, _) => Some(ast_id.to_node(db).syntax().clone()), | ||
287 | } | ||
288 | } | ||
289 | } | ||
290 | |||
291 | impl MacroCallId { | ||
292 | pub fn as_file(self) -> HirFileId { | ||
293 | MacroFile { macro_call_id: self }.into() | ||
294 | } | ||
295 | } | ||
296 | |||
297 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
298 | pub struct EagerCallLoc { | ||
299 | pub(crate) def: MacroDefId, | ||
300 | pub(crate) fragment: FragmentKind, | ||
301 | pub(crate) subtree: Arc<tt::Subtree>, | ||
302 | pub(crate) krate: CrateId, | ||
303 | pub(crate) file_id: HirFileId, | ||
304 | } | ||
305 | |||
306 | /// ExpansionInfo mainly describes how to map text range between src and expanded macro | ||
307 | #[derive(Debug, Clone, PartialEq, Eq)] | ||
308 | pub struct ExpansionInfo { | ||
309 | expanded: InFile<SyntaxNode>, | ||
310 | arg: InFile<SyntaxNode>, | ||
311 | def: InFile<ast::TokenTree>, | ||
312 | |||
313 | macro_def: Arc<(db::TokenExpander, mbe::TokenMap)>, | ||
314 | macro_arg: Arc<(tt::Subtree, mbe::TokenMap)>, | ||
315 | exp_map: Arc<mbe::TokenMap>, | ||
316 | } | ||
317 | |||
318 | pub use mbe::Origin; | ||
319 | use ra_parser::FragmentKind; | ||
320 | |||
321 | impl ExpansionInfo { | ||
322 | pub fn call_node(&self) -> Option<InFile<SyntaxNode>> { | ||
323 | Some(self.arg.with_value(self.arg.value.parent()?)) | ||
324 | } | ||
325 | |||
326 | pub fn map_token_down(&self, token: InFile<&SyntaxToken>) -> Option<InFile<SyntaxToken>> { | ||
327 | assert_eq!(token.file_id, self.arg.file_id); | ||
328 | let range = token.value.text_range().checked_sub(self.arg.value.text_range().start())?; | ||
329 | let token_id = self.macro_arg.1.token_by_range(range)?; | ||
330 | let token_id = self.macro_def.0.map_id_down(token_id); | ||
331 | |||
332 | let range = self.exp_map.range_by_token(token_id)?.by_kind(token.value.kind())?; | ||
333 | |||
334 | let token = algo::find_covering_element(&self.expanded.value, range).into_token()?; | ||
335 | |||
336 | Some(self.expanded.with_value(token)) | ||
337 | } | ||
338 | |||
339 | pub fn map_token_up( | ||
340 | &self, | ||
341 | token: InFile<&SyntaxToken>, | ||
342 | ) -> Option<(InFile<SyntaxToken>, Origin)> { | ||
343 | let token_id = self.exp_map.token_by_range(token.value.text_range())?; | ||
344 | |||
345 | let (token_id, origin) = self.macro_def.0.map_id_up(token_id); | ||
346 | let (token_map, tt) = match origin { | ||
347 | mbe::Origin::Call => (&self.macro_arg.1, self.arg.clone()), | ||
348 | mbe::Origin::Def => { | ||
349 | (&self.macro_def.1, self.def.as_ref().map(|tt| tt.syntax().clone())) | ||
350 | } | ||
351 | }; | ||
352 | |||
353 | let range = token_map.range_by_token(token_id)?.by_kind(token.value.kind())?; | ||
354 | let token = algo::find_covering_element(&tt.value, range + tt.value.text_range().start()) | ||
355 | .into_token()?; | ||
356 | Some((tt.with_value(token), origin)) | ||
357 | } | ||
358 | } | ||
359 | |||
360 | /// `AstId` points to an AST node in any file. | ||
361 | /// | ||
362 | /// It is stable across reparses, and can be used as salsa key/value. | ||
363 | // FIXME: isn't this just a `Source<FileAstId<N>>` ? | ||
364 | pub type AstId<N> = InFile<FileAstId<N>>; | ||
365 | |||
366 | impl<N: AstNode> AstId<N> { | ||
367 | pub fn to_node(&self, db: &dyn db::AstDatabase) -> N { | ||
368 | let root = db.parse_or_expand(self.file_id).unwrap(); | ||
369 | db.ast_id_map(self.file_id).get(self.value).to_node(&root) | ||
370 | } | ||
371 | } | ||
372 | |||
373 | /// `InFile<T>` stores a value of `T` inside a particular file/syntax tree. | ||
374 | /// | ||
375 | /// Typical usages are: | ||
376 | /// | ||
377 | /// * `InFile<SyntaxNode>` -- syntax node in a file | ||
378 | /// * `InFile<ast::FnDef>` -- ast node in a file | ||
379 | /// * `InFile<TextSize>` -- offset in a file | ||
380 | #[derive(Debug, PartialEq, Eq, Clone, Copy, Hash)] | ||
381 | pub struct InFile<T> { | ||
382 | pub file_id: HirFileId, | ||
383 | pub value: T, | ||
384 | } | ||
385 | |||
386 | impl<T> InFile<T> { | ||
387 | pub fn new(file_id: HirFileId, value: T) -> InFile<T> { | ||
388 | InFile { file_id, value } | ||
389 | } | ||
390 | |||
391 | // Similarly, naming here is stupid... | ||
392 | pub fn with_value<U>(&self, value: U) -> InFile<U> { | ||
393 | InFile::new(self.file_id, value) | ||
394 | } | ||
395 | |||
396 | pub fn map<F: FnOnce(T) -> U, U>(self, f: F) -> InFile<U> { | ||
397 | InFile::new(self.file_id, f(self.value)) | ||
398 | } | ||
399 | pub fn as_ref(&self) -> InFile<&T> { | ||
400 | self.with_value(&self.value) | ||
401 | } | ||
402 | pub fn file_syntax(&self, db: &dyn db::AstDatabase) -> SyntaxNode { | ||
403 | db.parse_or_expand(self.file_id).expect("source created from invalid file") | ||
404 | } | ||
405 | } | ||
406 | |||
407 | impl<T: Clone> InFile<&T> { | ||
408 | pub fn cloned(&self) -> InFile<T> { | ||
409 | self.with_value(self.value.clone()) | ||
410 | } | ||
411 | } | ||
412 | |||
413 | impl<T> InFile<Option<T>> { | ||
414 | pub fn transpose(self) -> Option<InFile<T>> { | ||
415 | let value = self.value?; | ||
416 | Some(InFile::new(self.file_id, value)) | ||
417 | } | ||
418 | } | ||
419 | |||
420 | impl InFile<SyntaxNode> { | ||
421 | pub fn ancestors_with_macros( | ||
422 | self, | ||
423 | db: &dyn db::AstDatabase, | ||
424 | ) -> impl Iterator<Item = InFile<SyntaxNode>> + '_ { | ||
425 | std::iter::successors(Some(self), move |node| match node.value.parent() { | ||
426 | Some(parent) => Some(node.with_value(parent)), | ||
427 | None => { | ||
428 | let parent_node = node.file_id.call_node(db)?; | ||
429 | Some(parent_node) | ||
430 | } | ||
431 | }) | ||
432 | } | ||
433 | } | ||
434 | |||
435 | impl InFile<SyntaxToken> { | ||
436 | pub fn ancestors_with_macros( | ||
437 | self, | ||
438 | db: &dyn db::AstDatabase, | ||
439 | ) -> impl Iterator<Item = InFile<SyntaxNode>> + '_ { | ||
440 | self.map(|it| it.parent()).ancestors_with_macros(db) | ||
441 | } | ||
442 | } | ||
443 | |||
444 | impl<N: AstNode> InFile<N> { | ||
445 | pub fn descendants<T: AstNode>(self) -> impl Iterator<Item = InFile<T>> { | ||
446 | self.value.syntax().descendants().filter_map(T::cast).map(move |n| self.with_value(n)) | ||
447 | } | ||
448 | |||
449 | pub fn syntax(&self) -> InFile<&SyntaxNode> { | ||
450 | self.with_value(self.value.syntax()) | ||
451 | } | ||
452 | } | ||