Move expansion to Expander

This commit is contained in:
Aleksey Kladov 2019-11-14 10:04:39 +03:00
parent 5c720b256f
commit a73b7bb3f6
2 changed files with 41 additions and 27 deletions

View File

@ -3,9 +3,12 @@
use std::{ops::Index, sync::Arc};
use hir_expand::{either::Either, hygiene::Hygiene, HirFileId, MacroDefId, Source};
use hir_expand::{
either::Either, hygiene::Hygiene, AstId, HirFileId, MacroCallLoc, MacroDefId, MacroFileKind,
Source,
};
use ra_arena::{map::ArenaMap, Arena};
use ra_syntax::{ast, AstPtr};
use ra_syntax::{ast, AstNode, AstPtr};
use rustc_hash::FxHashMap;
use crate::{
@ -37,6 +40,35 @@ pub fn new(db: &impl DefDatabase2, current_file_id: HirFileId, module: ModuleId)
}
}
fn expand(
&mut self,
db: &impl DefDatabase2,
macro_call: ast::MacroCall,
) -> Option<(Mark, ast::Expr)> {
let ast_id = AstId::new(
self.current_file_id,
db.ast_id_map(self.current_file_id).ast_id(&macro_call),
);
if let Some(path) = macro_call.path().and_then(|path| self.parse_path(path)) {
if let Some(def) = self.resolve_path_as_macro(db, &path) {
let call_id = db.intern_macro(MacroCallLoc { def, ast_id });
let file_id = call_id.as_file(MacroFileKind::Expr);
if let Some(node) = db.parse_or_expand(file_id) {
if let Some(expr) = ast::Expr::cast(node) {
log::debug!("macro expansion {:#?}", expr.syntax());
let mark = self.enter(db, file_id);
return Some((mark, expr));
}
}
}
}
// FIXME: Instead of just dropping the error from expansion
// report it
None
}
fn enter(&mut self, db: &impl DefDatabase2, file_id: HirFileId) -> Mark {
let mark = Mark { file_id: self.current_file_id };
self.hygiene = Hygiene::new(db, file_id);

View File

@ -3,7 +3,6 @@
use hir_expand::{
either::Either,
name::{self, AsName, Name},
AstId, MacroCallLoc, MacroFileKind,
};
use ra_arena::Arena;
use ra_syntax::{
@ -433,31 +432,14 @@ fn collect_expr(&mut self, expr: ast::Expr) -> ExprId {
// FIXME implement HIR for these:
ast::Expr::Label(_e) => self.alloc_expr(Expr::Missing, syntax_ptr),
ast::Expr::RangeExpr(_e) => self.alloc_expr(Expr::Missing, syntax_ptr),
ast::Expr::MacroCall(e) => {
let ast_id = AstId::new(
self.expander.current_file_id,
self.db.ast_id_map(self.expander.current_file_id).ast_id(&e),
);
if let Some(path) = e.path().and_then(|path| self.expander.parse_path(path)) {
if let Some(def) = self.expander.resolve_path_as_macro(self.db, &path) {
let call_id = self.db.intern_macro(MacroCallLoc { def, ast_id });
let file_id = call_id.as_file(MacroFileKind::Expr);
if let Some(node) = self.db.parse_or_expand(file_id) {
if let Some(expr) = ast::Expr::cast(node) {
log::debug!("macro expansion {:#?}", expr.syntax());
let mark = self.expander.enter(self.db, file_id);
let id = self.collect_expr(expr);
self.expander.exit(self.db, mark);
return id;
}
}
}
ast::Expr::MacroCall(e) => match self.expander.expand(self.db, e) {
Some((mark, expansion)) => {
let id = self.collect_expr(expansion);
self.expander.exit(self.db, mark);
id
}
// FIXME: Instead of just dropping the error from expansion
// report it
self.alloc_expr(Expr::Missing, syntax_ptr)
}
None => self.alloc_expr(Expr::Missing, syntax_ptr),
},
}
}