From 5dc384132f7fe6f610ec21e62830ab83f45274a9 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 4 May 2019 17:38:09 +0300 Subject: 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. --- crates/ra_hir/src/db.rs | 5 ++++- crates/ra_hir/src/expr.rs | 33 ++++++++++++++------------------- crates/ra_hir/src/ids.rs | 14 +++++++++----- 3 files changed, 27 insertions(+), 25 deletions(-) (limited to 'crates/ra_hir') diff --git a/crates/ra_hir/src/db.rs b/crates/ra_hir/src/db.rs index 8af0a3176..f88ae61bb 100644 --- a/crates/ra_hir/src/db.rs +++ b/crates/ra_hir/src/db.rs @@ -42,7 +42,10 @@ pub trait DefDatabase: SourceDatabase { #[salsa::invoke(crate::ids::macro_def_query)] fn macro_def(&self, macro_id: MacroDefId) -> Option>; - #[salsa::invoke(HirFileId::hir_parse_query)] + #[salsa::invoke(crate::ids::macro_arg_query)] + fn macro_arg(&self, macro_call: ids::MacroCallId) -> Option>; + + #[salsa::invoke(crate::ids::HirFileId::hir_parse_query)] fn hir_parse(&self, file_id: HirFileId) -> TreeArc; #[salsa::invoke(crate::adt::StructData::struct_data_query)] diff --git a/crates/ra_hir/src/expr.rs b/crates/ra_hir/src/expr.rs index cdadf3ba6..f9b3f5443 100644 --- a/crates/ra_hir/src/expr.rs +++ b/crates/ra_hir/src/expr.rs @@ -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> { 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() } diff --git a/crates/ra_hir/src/ids.rs b/crates/ra_hir/src/ids.rs index b0e9b1f9a..24c5f412b 100644 --- a/crates/ra_hir/src/ids.rs +++ b/crates/ra_hir/src/ids.rs @@ -86,11 +86,7 @@ fn parse_macro( macro_call_id: MacroCallId, ) -> Result, 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 Option> { + 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 { -- cgit v1.2.3