mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-11-15 01:17:27 +00:00
Move current file to MacroResolver
This commit is contained in:
parent
debf95eb1b
commit
b3175b7077
3 changed files with 27 additions and 21 deletions
|
@ -40,8 +40,8 @@ pub(crate) fn body_with_source_map_query(
|
|||
(src.file_id, src.ast.body())
|
||||
}
|
||||
};
|
||||
let resolver = hir_def::body::MacroResolver::new(db, def.module(db).id);
|
||||
let (body, source_map) = Body::new(db, resolver, file_id, params, body);
|
||||
let resolver = hir_def::body::MacroResolver::new(db, file_id, def.module(db).id);
|
||||
let (body, source_map) = Body::new(db, resolver, params, body);
|
||||
(Arc::new(body), Arc::new(source_map))
|
||||
}
|
||||
|
||||
|
|
|
@ -18,12 +18,18 @@ use crate::{
|
|||
|
||||
pub struct MacroResolver {
|
||||
crate_def_map: Arc<CrateDefMap>,
|
||||
current_file_id: HirFileId,
|
||||
module: ModuleId,
|
||||
}
|
||||
|
||||
impl MacroResolver {
|
||||
pub fn new(db: &impl DefDatabase2, module: ModuleId) -> MacroResolver {
|
||||
MacroResolver { crate_def_map: db.crate_def_map(module.krate), module }
|
||||
pub fn new(
|
||||
db: &impl DefDatabase2,
|
||||
current_file_id: HirFileId,
|
||||
module: ModuleId,
|
||||
) -> MacroResolver {
|
||||
let crate_def_map = db.crate_def_map(module.krate);
|
||||
MacroResolver { crate_def_map, current_file_id, module }
|
||||
}
|
||||
|
||||
fn resolve_path_as_macro(&self, db: &impl DefDatabase2, path: &Path) -> Option<MacroDefId> {
|
||||
|
@ -77,11 +83,10 @@ impl Body {
|
|||
pub fn new(
|
||||
db: &impl DefDatabase2,
|
||||
resolver: MacroResolver,
|
||||
file_id: HirFileId,
|
||||
params: Option<ast::ParamList>,
|
||||
body: Option<ast::Expr>,
|
||||
) -> (Body, BodySourceMap) {
|
||||
lower::lower(db, resolver, file_id, params, body)
|
||||
lower::lower(db, resolver, params, body)
|
||||
}
|
||||
|
||||
pub fn params(&self) -> &[PatId] {
|
||||
|
|
|
@ -31,15 +31,15 @@ use crate::{
|
|||
pub(super) fn lower(
|
||||
db: &impl DefDatabase2,
|
||||
resolver: MacroResolver,
|
||||
file_id: HirFileId,
|
||||
params: Option<ast::ParamList>,
|
||||
body: Option<ast::Expr>,
|
||||
) -> (Body, BodySourceMap) {
|
||||
let original_file_id = resolver.current_file_id;
|
||||
|
||||
ExprCollector {
|
||||
resolver,
|
||||
db,
|
||||
original_file_id: file_id,
|
||||
current_file_id: file_id,
|
||||
original_file_id,
|
||||
source_map: BodySourceMap::default(),
|
||||
body: Body {
|
||||
exprs: Arena::default(),
|
||||
|
@ -55,7 +55,6 @@ struct ExprCollector<DB> {
|
|||
db: DB,
|
||||
resolver: MacroResolver,
|
||||
original_file_id: HirFileId,
|
||||
current_file_id: HirFileId,
|
||||
|
||||
body: Body,
|
||||
source_map: BodySourceMap,
|
||||
|
@ -101,12 +100,12 @@ where
|
|||
fn alloc_expr(&mut self, expr: Expr, ptr: AstPtr<ast::Expr>) -> ExprId {
|
||||
let ptr = Either::A(ptr);
|
||||
let id = self.body.exprs.alloc(expr);
|
||||
if self.current_file_id == self.original_file_id {
|
||||
if self.resolver.current_file_id == self.original_file_id {
|
||||
self.source_map.expr_map.insert(ptr, id);
|
||||
}
|
||||
self.source_map
|
||||
.expr_map_back
|
||||
.insert(id, Source { file_id: self.current_file_id, ast: ptr });
|
||||
.insert(id, Source { file_id: self.resolver.current_file_id, ast: ptr });
|
||||
id
|
||||
}
|
||||
// desugared exprs don't have ptr, that's wrong and should be fixed
|
||||
|
@ -117,20 +116,22 @@ where
|
|||
fn alloc_expr_field_shorthand(&mut self, expr: Expr, ptr: AstPtr<ast::RecordField>) -> ExprId {
|
||||
let ptr = Either::B(ptr);
|
||||
let id = self.body.exprs.alloc(expr);
|
||||
if self.current_file_id == self.original_file_id {
|
||||
if self.resolver.current_file_id == self.original_file_id {
|
||||
self.source_map.expr_map.insert(ptr, id);
|
||||
}
|
||||
self.source_map
|
||||
.expr_map_back
|
||||
.insert(id, Source { file_id: self.current_file_id, ast: ptr });
|
||||
.insert(id, Source { file_id: self.resolver.current_file_id, ast: ptr });
|
||||
id
|
||||
}
|
||||
fn alloc_pat(&mut self, pat: Pat, ptr: PatPtr) -> PatId {
|
||||
let id = self.body.pats.alloc(pat);
|
||||
if self.current_file_id == self.original_file_id {
|
||||
if self.resolver.current_file_id == self.original_file_id {
|
||||
self.source_map.pat_map.insert(ptr, id);
|
||||
}
|
||||
self.source_map.pat_map_back.insert(id, Source { file_id: self.current_file_id, ast: ptr });
|
||||
self.source_map
|
||||
.pat_map_back
|
||||
.insert(id, Source { file_id: self.resolver.current_file_id, ast: ptr });
|
||||
id
|
||||
}
|
||||
|
||||
|
@ -445,8 +446,8 @@ where
|
|||
ast::Expr::RangeExpr(_e) => self.alloc_expr(Expr::Missing, syntax_ptr),
|
||||
ast::Expr::MacroCall(e) => {
|
||||
let ast_id = AstId::new(
|
||||
self.current_file_id,
|
||||
self.db.ast_id_map(self.current_file_id).ast_id(&e),
|
||||
self.resolver.current_file_id,
|
||||
self.db.ast_id_map(self.resolver.current_file_id).ast_id(&e),
|
||||
);
|
||||
|
||||
if let Some(path) = e.path().and_then(|path| self.parse_path(path)) {
|
||||
|
@ -457,9 +458,9 @@ where
|
|||
if let Some(expr) = ast::Expr::cast(node) {
|
||||
log::debug!("macro expansion {:#?}", expr.syntax());
|
||||
let old_file_id =
|
||||
std::mem::replace(&mut self.current_file_id, file_id);
|
||||
std::mem::replace(&mut self.resolver.current_file_id, file_id);
|
||||
let id = self.collect_expr(expr);
|
||||
self.current_file_id = old_file_id;
|
||||
self.resolver.current_file_id = old_file_id;
|
||||
return id;
|
||||
}
|
||||
}
|
||||
|
@ -581,7 +582,7 @@ where
|
|||
}
|
||||
|
||||
fn parse_path(&mut self, path: ast::Path) -> Option<Path> {
|
||||
let hygiene = Hygiene::new(self.db, self.current_file_id);
|
||||
let hygiene = Hygiene::new(self.db, self.resolver.current_file_id);
|
||||
Path::from_src(path, &hygiene)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue