aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
Diffstat (limited to 'crates')
-rw-r--r--crates/ra_hir_def/src/body.rs36
-rw-r--r--crates/ra_hir_def/src/body/lower.rs32
2 files changed, 41 insertions, 27 deletions
diff --git a/crates/ra_hir_def/src/body.rs b/crates/ra_hir_def/src/body.rs
index afceeb8de..65fefd912 100644
--- a/crates/ra_hir_def/src/body.rs
+++ b/crates/ra_hir_def/src/body.rs
@@ -3,9 +3,12 @@ mod lower;
3 3
4use std::{ops::Index, sync::Arc}; 4use std::{ops::Index, sync::Arc};
5 5
6use hir_expand::{either::Either, hygiene::Hygiene, HirFileId, MacroDefId, Source}; 6use hir_expand::{
7 either::Either, hygiene::Hygiene, AstId, HirFileId, MacroCallLoc, MacroDefId, MacroFileKind,
8 Source,
9};
7use ra_arena::{map::ArenaMap, Arena}; 10use ra_arena::{map::ArenaMap, Arena};
8use ra_syntax::{ast, AstPtr}; 11use ra_syntax::{ast, AstNode, AstPtr};
9use rustc_hash::FxHashMap; 12use rustc_hash::FxHashMap;
10 13
11use crate::{ 14use crate::{
@@ -37,6 +40,35 @@ impl Expander {
37 } 40 }
38 } 41 }
39 42
43 fn expand(
44 &mut self,
45 db: &impl DefDatabase2,
46 macro_call: ast::MacroCall,
47 ) -> Option<(Mark, ast::Expr)> {
48 let ast_id = AstId::new(
49 self.current_file_id,
50 db.ast_id_map(self.current_file_id).ast_id(&macro_call),
51 );
52
53 if let Some(path) = macro_call.path().and_then(|path| self.parse_path(path)) {
54 if let Some(def) = self.resolve_path_as_macro(db, &path) {
55 let call_id = db.intern_macro(MacroCallLoc { def, ast_id });
56 let file_id = call_id.as_file(MacroFileKind::Expr);
57 if let Some(node) = db.parse_or_expand(file_id) {
58 if let Some(expr) = ast::Expr::cast(node) {
59 log::debug!("macro expansion {:#?}", expr.syntax());
60 let mark = self.enter(db, file_id);
61 return Some((mark, expr));
62 }
63 }
64 }
65 }
66
67 // FIXME: Instead of just dropping the error from expansion
68 // report it
69 None
70 }
71
40 fn enter(&mut self, db: &impl DefDatabase2, file_id: HirFileId) -> Mark { 72 fn enter(&mut self, db: &impl DefDatabase2, file_id: HirFileId) -> Mark {
41 let mark = Mark { file_id: self.current_file_id }; 73 let mark = Mark { file_id: self.current_file_id };
42 self.hygiene = Hygiene::new(db, file_id); 74 self.hygiene = Hygiene::new(db, file_id);
diff --git a/crates/ra_hir_def/src/body/lower.rs b/crates/ra_hir_def/src/body/lower.rs
index 29c1ec2a1..c45500195 100644
--- a/crates/ra_hir_def/src/body/lower.rs
+++ b/crates/ra_hir_def/src/body/lower.rs
@@ -3,7 +3,6 @@
3use hir_expand::{ 3use hir_expand::{
4 either::Either, 4 either::Either,
5 name::{self, AsName, Name}, 5 name::{self, AsName, Name},
6 AstId, MacroCallLoc, MacroFileKind,
7}; 6};
8use ra_arena::Arena; 7use ra_arena::Arena;
9use ra_syntax::{ 8use ra_syntax::{
@@ -433,31 +432,14 @@ where
433 // FIXME implement HIR for these: 432 // FIXME implement HIR for these:
434 ast::Expr::Label(_e) => self.alloc_expr(Expr::Missing, syntax_ptr), 433 ast::Expr::Label(_e) => self.alloc_expr(Expr::Missing, syntax_ptr),
435 ast::Expr::RangeExpr(_e) => self.alloc_expr(Expr::Missing, syntax_ptr), 434 ast::Expr::RangeExpr(_e) => self.alloc_expr(Expr::Missing, syntax_ptr),
436 ast::Expr::MacroCall(e) => { 435 ast::Expr::MacroCall(e) => match self.expander.expand(self.db, e) {
437 let ast_id = AstId::new( 436 Some((mark, expansion)) => {
438 self.expander.current_file_id, 437 let id = self.collect_expr(expansion);
439 self.db.ast_id_map(self.expander.current_file_id).ast_id(&e), 438 self.expander.exit(self.db, mark);
440 ); 439 id
441
442 if let Some(path) = e.path().and_then(|path| self.expander.parse_path(path)) {
443 if let Some(def) = self.expander.resolve_path_as_macro(self.db, &path) {
444 let call_id = self.db.intern_macro(MacroCallLoc { def, ast_id });
445 let file_id = call_id.as_file(MacroFileKind::Expr);
446 if let Some(node) = self.db.parse_or_expand(file_id) {
447 if let Some(expr) = ast::Expr::cast(node) {
448 log::debug!("macro expansion {:#?}", expr.syntax());
449 let mark = self.expander.enter(self.db, file_id);
450 let id = self.collect_expr(expr);
451 self.expander.exit(self.db, mark);
452 return id;
453 }
454 }
455 }
456 } 440 }
457 // FIXME: Instead of just dropping the error from expansion 441 None => self.alloc_expr(Expr::Missing, syntax_ptr),
458 // report it 442 },
459 self.alloc_expr(Expr::Missing, syntax_ptr)
460 }
461 } 443 }
462 } 444 }
463 445