Rename MacroResolver -> Expander

This commit is contained in:
Aleksey Kladov 2019-11-14 09:38:25 +03:00
parent b3175b7077
commit 8c8ef1432e
3 changed files with 25 additions and 29 deletions

View file

@ -40,8 +40,8 @@ pub(crate) fn body_with_source_map_query(
(src.file_id, src.ast.body()) (src.file_id, src.ast.body())
} }
}; };
let resolver = hir_def::body::MacroResolver::new(db, file_id, def.module(db).id); let expander = hir_def::body::Expander::new(db, file_id, def.module(db).id);
let (body, source_map) = Body::new(db, resolver, params, body); let (body, source_map) = Body::new(db, expander, params, body);
(Arc::new(body), Arc::new(source_map)) (Arc::new(body), Arc::new(source_map))
} }

View file

@ -16,20 +16,16 @@ use crate::{
ModuleId, ModuleId,
}; };
pub struct MacroResolver { pub struct Expander {
crate_def_map: Arc<CrateDefMap>, crate_def_map: Arc<CrateDefMap>,
current_file_id: HirFileId, current_file_id: HirFileId,
module: ModuleId, module: ModuleId,
} }
impl MacroResolver { impl Expander {
pub fn new( pub fn new(db: &impl DefDatabase2, current_file_id: HirFileId, module: ModuleId) -> Expander {
db: &impl DefDatabase2,
current_file_id: HirFileId,
module: ModuleId,
) -> MacroResolver {
let crate_def_map = db.crate_def_map(module.krate); let crate_def_map = db.crate_def_map(module.krate);
MacroResolver { crate_def_map, current_file_id, module } Expander { crate_def_map, current_file_id, module }
} }
fn resolve_path_as_macro(&self, db: &impl DefDatabase2, path: &Path) -> Option<MacroDefId> { fn resolve_path_as_macro(&self, db: &impl DefDatabase2, path: &Path) -> Option<MacroDefId> {
@ -82,11 +78,11 @@ pub struct BodySourceMap {
impl Body { impl Body {
pub fn new( pub fn new(
db: &impl DefDatabase2, db: &impl DefDatabase2,
resolver: MacroResolver, expander: Expander,
params: Option<ast::ParamList>, params: Option<ast::ParamList>,
body: Option<ast::Expr>, body: Option<ast::Expr>,
) -> (Body, BodySourceMap) { ) -> (Body, BodySourceMap) {
lower::lower(db, resolver, params, body) lower::lower(db, expander, params, body)
} }
pub fn params(&self) -> &[PatId] { pub fn params(&self) -> &[PatId] {

View file

@ -16,7 +16,7 @@ use ra_syntax::{
}; };
use crate::{ use crate::{
body::{Body, BodySourceMap, MacroResolver, PatPtr}, body::{Body, BodySourceMap, Expander, PatPtr},
builtin_type::{BuiltinFloat, BuiltinInt}, builtin_type::{BuiltinFloat, BuiltinInt},
db::DefDatabase2, db::DefDatabase2,
expr::{ expr::{
@ -30,14 +30,14 @@ use crate::{
pub(super) fn lower( pub(super) fn lower(
db: &impl DefDatabase2, db: &impl DefDatabase2,
resolver: MacroResolver, expander: Expander,
params: Option<ast::ParamList>, params: Option<ast::ParamList>,
body: Option<ast::Expr>, body: Option<ast::Expr>,
) -> (Body, BodySourceMap) { ) -> (Body, BodySourceMap) {
let original_file_id = resolver.current_file_id; let original_file_id = expander.current_file_id;
ExprCollector { ExprCollector {
resolver, expander,
db, db,
original_file_id, original_file_id,
source_map: BodySourceMap::default(), source_map: BodySourceMap::default(),
@ -53,7 +53,7 @@ pub(super) fn lower(
struct ExprCollector<DB> { struct ExprCollector<DB> {
db: DB, db: DB,
resolver: MacroResolver, expander: Expander,
original_file_id: HirFileId, original_file_id: HirFileId,
body: Body, body: Body,
@ -100,12 +100,12 @@ where
fn alloc_expr(&mut self, expr: Expr, ptr: AstPtr<ast::Expr>) -> ExprId { fn alloc_expr(&mut self, expr: Expr, ptr: AstPtr<ast::Expr>) -> ExprId {
let ptr = Either::A(ptr); let ptr = Either::A(ptr);
let id = self.body.exprs.alloc(expr); let id = self.body.exprs.alloc(expr);
if self.resolver.current_file_id == self.original_file_id { if self.expander.current_file_id == self.original_file_id {
self.source_map.expr_map.insert(ptr, id); self.source_map.expr_map.insert(ptr, id);
} }
self.source_map self.source_map
.expr_map_back .expr_map_back
.insert(id, Source { file_id: self.resolver.current_file_id, ast: ptr }); .insert(id, Source { file_id: self.expander.current_file_id, ast: ptr });
id id
} }
// desugared exprs don't have ptr, that's wrong and should be fixed // desugared exprs don't have ptr, that's wrong and should be fixed
@ -116,22 +116,22 @@ where
fn alloc_expr_field_shorthand(&mut self, expr: Expr, ptr: AstPtr<ast::RecordField>) -> ExprId { fn alloc_expr_field_shorthand(&mut self, expr: Expr, ptr: AstPtr<ast::RecordField>) -> ExprId {
let ptr = Either::B(ptr); let ptr = Either::B(ptr);
let id = self.body.exprs.alloc(expr); let id = self.body.exprs.alloc(expr);
if self.resolver.current_file_id == self.original_file_id { if self.expander.current_file_id == self.original_file_id {
self.source_map.expr_map.insert(ptr, id); self.source_map.expr_map.insert(ptr, id);
} }
self.source_map self.source_map
.expr_map_back .expr_map_back
.insert(id, Source { file_id: self.resolver.current_file_id, ast: ptr }); .insert(id, Source { file_id: self.expander.current_file_id, ast: ptr });
id id
} }
fn alloc_pat(&mut self, pat: Pat, ptr: PatPtr) -> PatId { fn alloc_pat(&mut self, pat: Pat, ptr: PatPtr) -> PatId {
let id = self.body.pats.alloc(pat); let id = self.body.pats.alloc(pat);
if self.resolver.current_file_id == self.original_file_id { if self.expander.current_file_id == self.original_file_id {
self.source_map.pat_map.insert(ptr, id); self.source_map.pat_map.insert(ptr, id);
} }
self.source_map self.source_map
.pat_map_back .pat_map_back
.insert(id, Source { file_id: self.resolver.current_file_id, ast: ptr }); .insert(id, Source { file_id: self.expander.current_file_id, ast: ptr });
id id
} }
@ -446,21 +446,21 @@ where
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) => {
let ast_id = AstId::new( let ast_id = AstId::new(
self.resolver.current_file_id, self.expander.current_file_id,
self.db.ast_id_map(self.resolver.current_file_id).ast_id(&e), self.db.ast_id_map(self.expander.current_file_id).ast_id(&e),
); );
if let Some(path) = e.path().and_then(|path| self.parse_path(path)) { if let Some(path) = e.path().and_then(|path| self.parse_path(path)) {
if let Some(def) = self.resolver.resolve_path_as_macro(self.db, &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 call_id = self.db.intern_macro(MacroCallLoc { def, ast_id });
let file_id = call_id.as_file(MacroFileKind::Expr); let file_id = call_id.as_file(MacroFileKind::Expr);
if let Some(node) = self.db.parse_or_expand(file_id) { if let Some(node) = self.db.parse_or_expand(file_id) {
if let Some(expr) = ast::Expr::cast(node) { if let Some(expr) = ast::Expr::cast(node) {
log::debug!("macro expansion {:#?}", expr.syntax()); log::debug!("macro expansion {:#?}", expr.syntax());
let old_file_id = let old_file_id =
std::mem::replace(&mut self.resolver.current_file_id, file_id); std::mem::replace(&mut self.expander.current_file_id, file_id);
let id = self.collect_expr(expr); let id = self.collect_expr(expr);
self.resolver.current_file_id = old_file_id; self.expander.current_file_id = old_file_id;
return id; return id;
} }
} }
@ -582,7 +582,7 @@ where
} }
fn parse_path(&mut self, path: ast::Path) -> Option<Path> { fn parse_path(&mut self, path: ast::Path) -> Option<Path> {
let hygiene = Hygiene::new(self.db, self.resolver.current_file_id); let hygiene = Hygiene::new(self.db, self.expander.current_file_id);
Path::from_src(path, &hygiene) Path::from_src(path, &hygiene)
} }
} }