mirror of
https://github.com/rust-lang/rust-analyzer
synced 2025-01-14 14:13:58 +00:00
Move expansion to Expander
This commit is contained in:
parent
5c720b256f
commit
a73b7bb3f6
2 changed files with 41 additions and 27 deletions
|
@ -3,9 +3,12 @@ mod lower;
|
||||||
|
|
||||||
use std::{ops::Index, sync::Arc};
|
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_arena::{map::ArenaMap, Arena};
|
||||||
use ra_syntax::{ast, AstPtr};
|
use ra_syntax::{ast, AstNode, AstPtr};
|
||||||
use rustc_hash::FxHashMap;
|
use rustc_hash::FxHashMap;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
|
@ -37,6 +40,35 @@ impl Expander {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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(¯o_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 {
|
fn enter(&mut self, db: &impl DefDatabase2, file_id: HirFileId) -> Mark {
|
||||||
let mark = Mark { file_id: self.current_file_id };
|
let mark = Mark { file_id: self.current_file_id };
|
||||||
self.hygiene = Hygiene::new(db, file_id);
|
self.hygiene = Hygiene::new(db, file_id);
|
||||||
|
|
|
@ -3,7 +3,6 @@
|
||||||
use hir_expand::{
|
use hir_expand::{
|
||||||
either::Either,
|
either::Either,
|
||||||
name::{self, AsName, Name},
|
name::{self, AsName, Name},
|
||||||
AstId, MacroCallLoc, MacroFileKind,
|
|
||||||
};
|
};
|
||||||
use ra_arena::Arena;
|
use ra_arena::Arena;
|
||||||
use ra_syntax::{
|
use ra_syntax::{
|
||||||
|
@ -433,31 +432,14 @@ where
|
||||||
// FIXME implement HIR for these:
|
// FIXME implement HIR for these:
|
||||||
ast::Expr::Label(_e) => self.alloc_expr(Expr::Missing, syntax_ptr),
|
ast::Expr::Label(_e) => self.alloc_expr(Expr::Missing, syntax_ptr),
|
||||||
ast::Expr::RangeExpr(_e) => self.alloc_expr(Expr::Missing, syntax_ptr),
|
ast::Expr::RangeExpr(_e) => self.alloc_expr(Expr::Missing, syntax_ptr),
|
||||||
ast::Expr::MacroCall(e) => {
|
ast::Expr::MacroCall(e) => match self.expander.expand(self.db, e) {
|
||||||
let ast_id = AstId::new(
|
Some((mark, expansion)) => {
|
||||||
self.expander.current_file_id,
|
let id = self.collect_expr(expansion);
|
||||||
self.db.ast_id_map(self.expander.current_file_id).ast_id(&e),
|
self.expander.exit(self.db, mark);
|
||||||
);
|
id
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
// FIXME: Instead of just dropping the error from expansion
|
None => self.alloc_expr(Expr::Missing, syntax_ptr),
|
||||||
// report it
|
},
|
||||||
self.alloc_expr(Expr::Missing, syntax_ptr)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue