introduce macro_arg intermediate query

Currently, when expanding macros, we look at the source code
directly (we invoke ast_id_to_node query via to_node method).

This is less then ideal, because it make us re-expand macros after
every source change.

This commit establishes a salsa-firewall: a query to get macro call's
token tree. Unlike the syntax tree, token tree changes only if we
actually modify the macro itself.
This commit is contained in:
Aleksey Kladov 2019-05-04 17:38:09 +03:00
parent 8876f44054
commit 5dc384132f
3 changed files with 27 additions and 25 deletions

View file

@ -42,7 +42,10 @@ pub trait DefDatabase: SourceDatabase {
#[salsa::invoke(crate::ids::macro_def_query)] #[salsa::invoke(crate::ids::macro_def_query)]
fn macro_def(&self, macro_id: MacroDefId) -> Option<Arc<mbe::MacroRules>>; fn macro_def(&self, macro_id: MacroDefId) -> Option<Arc<mbe::MacroRules>>;
#[salsa::invoke(HirFileId::hir_parse_query)] #[salsa::invoke(crate::ids::macro_arg_query)]
fn macro_arg(&self, macro_call: ids::MacroCallId) -> Option<Arc<tt::Subtree>>;
#[salsa::invoke(crate::ids::HirFileId::hir_parse_query)]
fn hir_parse(&self, file_id: HirFileId) -> TreeArc<SourceFile>; fn hir_parse(&self, file_id: HirFileId) -> TreeArc<SourceFile>;
#[salsa::invoke(crate::adt::StructData::struct_data_query)] #[salsa::invoke(crate::adt::StructData::struct_data_query)]

View file

@ -826,22 +826,21 @@ where
.with_file_id(self.current_file_id); .with_file_id(self.current_file_id);
if let Some(call_id) = self.resolver.resolve_macro_call(self.db, path, ast_id) { if let Some(call_id) = self.resolver.resolve_macro_call(self.db, path, ast_id) {
if let Some(expr) = expand_macro_to_expr(self.db, call_id, e.token_tree()) { if let Some(arg) = self.db.macro_arg(call_id) {
if let Some(expr) = expand_macro_to_expr(self.db, call_id, &arg) {
log::debug!("macro expansion {}", expr.syntax().debug_dump()); log::debug!("macro expansion {}", expr.syntax().debug_dump());
let old_file_id = let old_file_id =
std::mem::replace(&mut self.current_file_id, call_id.into()); std::mem::replace(&mut self.current_file_id, call_id.into());
let id = self.collect_expr(&expr); let id = self.collect_expr(&expr);
self.current_file_id = old_file_id; self.current_file_id = old_file_id;
id return id;
} else { }
}
}
// FIXME: Instead of just dropping the error from expansion // FIXME: Instead of just dropping the error from expansion
// report it // report it
self.alloc_expr(Expr::Missing, syntax_ptr) self.alloc_expr(Expr::Missing, syntax_ptr)
} }
} else {
self.alloc_expr(Expr::Missing, syntax_ptr)
}
}
} }
} }
@ -1002,14 +1001,10 @@ where
fn expand_macro_to_expr( fn expand_macro_to_expr(
db: &impl HirDatabase, db: &impl HirDatabase,
macro_call: MacroCallId, macro_call: MacroCallId,
args: Option<&ast::TokenTree>, arg: &tt::Subtree,
) -> Option<TreeArc<ast::Expr>> { ) -> Option<TreeArc<ast::Expr>> {
let rules = db.macro_def(macro_call.loc(db).def)?; let rules = db.macro_def(macro_call.loc(db).def)?;
let expanded = rules.expand(&arg).ok()?;
let args = mbe::ast_to_token_tree(args?)?.0;
let expanded = rules.expand(&args).ok()?;
mbe::token_tree_to_expr(&expanded).ok() mbe::token_tree_to_expr(&expanded).ok()
} }

View file

@ -86,11 +86,7 @@ fn parse_macro(
macro_call_id: MacroCallId, macro_call_id: MacroCallId,
) -> Result<TreeArc<SourceFile>, String> { ) -> Result<TreeArc<SourceFile>, String> {
let loc = macro_call_id.loc(db); let loc = macro_call_id.loc(db);
let macro_call = loc.ast_id.to_node(db); let macro_arg = db.macro_arg(macro_call_id).ok_or("Fail to args in to tt::TokenTree")?;
let (macro_arg, _) = macro_call
.token_tree()
.and_then(mbe::ast_to_token_tree)
.ok_or("Fail to args in to tt::TokenTree")?;
let macro_rules = db.macro_def(loc.def).ok_or("Fail to find macro definition")?; let macro_rules = db.macro_def(loc.def).ok_or("Fail to find macro definition")?;
let tt = macro_rules.expand(&macro_arg).map_err(|err| format!("{:?}", err))?; let tt = macro_rules.expand(&macro_arg).map_err(|err| format!("{:?}", err))?;
@ -139,6 +135,14 @@ pub(crate) fn macro_def_query(db: &impl DefDatabase, id: MacroDefId) -> Option<A
Some(Arc::new(rules)) Some(Arc::new(rules))
} }
pub(crate) fn macro_arg_query(db: &impl DefDatabase, id: MacroCallId) -> Option<Arc<tt::Subtree>> {
let loc = id.loc(db);
let macro_call = loc.ast_id.to_node(db);
let arg = macro_call.token_tree()?;
let (tt, _) = mbe::ast_to_token_tree(arg)?;
Some(Arc::new(tt))
}
macro_rules! impl_intern_key { macro_rules! impl_intern_key {
($name:ident) => { ($name:ident) => {
impl salsa::InternKey for $name { impl salsa::InternKey for $name {