mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-10-02 06:41:48 +00:00
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:
parent
8876f44054
commit
5dc384132f
3 changed files with 27 additions and 25 deletions
|
@ -42,7 +42,10 @@ pub trait DefDatabase: SourceDatabase {
|
|||
#[salsa::invoke(crate::ids::macro_def_query)]
|
||||
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>;
|
||||
|
||||
#[salsa::invoke(crate::adt::StructData::struct_data_query)]
|
||||
|
|
|
@ -826,21 +826,20 @@ where
|
|||
.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(expr) = expand_macro_to_expr(self.db, call_id, e.token_tree()) {
|
||||
log::debug!("macro expansion {}", expr.syntax().debug_dump());
|
||||
let old_file_id =
|
||||
std::mem::replace(&mut self.current_file_id, call_id.into());
|
||||
let id = self.collect_expr(&expr);
|
||||
self.current_file_id = old_file_id;
|
||||
id
|
||||
} else {
|
||||
// FIXME: Instead of just dropping the error from expansion
|
||||
// report it
|
||||
self.alloc_expr(Expr::Missing, syntax_ptr)
|
||||
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());
|
||||
let old_file_id =
|
||||
std::mem::replace(&mut self.current_file_id, call_id.into());
|
||||
let id = self.collect_expr(&expr);
|
||||
self.current_file_id = old_file_id;
|
||||
return id;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
self.alloc_expr(Expr::Missing, syntax_ptr)
|
||||
}
|
||||
// FIXME: Instead of just dropping the error from expansion
|
||||
// report it
|
||||
self.alloc_expr(Expr::Missing, syntax_ptr)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1002,14 +1001,10 @@ where
|
|||
fn expand_macro_to_expr(
|
||||
db: &impl HirDatabase,
|
||||
macro_call: MacroCallId,
|
||||
args: Option<&ast::TokenTree>,
|
||||
arg: &tt::Subtree,
|
||||
) -> Option<TreeArc<ast::Expr>> {
|
||||
let rules = db.macro_def(macro_call.loc(db).def)?;
|
||||
|
||||
let args = mbe::ast_to_token_tree(args?)?.0;
|
||||
|
||||
let expanded = rules.expand(&args).ok()?;
|
||||
|
||||
let expanded = rules.expand(&arg).ok()?;
|
||||
mbe::token_tree_to_expr(&expanded).ok()
|
||||
}
|
||||
|
||||
|
|
|
@ -86,11 +86,7 @@ fn parse_macro(
|
|||
macro_call_id: MacroCallId,
|
||||
) -> Result<TreeArc<SourceFile>, String> {
|
||||
let loc = macro_call_id.loc(db);
|
||||
let macro_call = loc.ast_id.to_node(db);
|
||||
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_arg = db.macro_arg(macro_call_id).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 tt = macro_rules.expand(¯o_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))
|
||||
}
|
||||
|
||||
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 {
|
||||
($name:ident) => {
|
||||
impl salsa::InternKey for $name {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue