Introduced resolve_macro_call on resolver

changed to manual expansion

fix for nested macros
This commit is contained in:
Lenard Pratt 2019-04-18 19:35:47 +01:00
parent ce211434a6
commit 1ab7066e32
6 changed files with 112 additions and 57 deletions

View file

@ -1,12 +1,15 @@
//! Name resolution.
use std::sync::Arc;
use ra_syntax::ast;
use rustc_hash::FxHashMap;
use crate::{
ModuleDef,
code_model_api::Crate,
MacroDefId,
MacroCallId,
MacroCallLoc,
db::HirDatabase,
name::{Name, KnownName},
nameres::{PerNs, CrateDefMap, CrateModuleId},
@ -131,8 +134,29 @@ impl Resolver {
resolution
}
pub fn resolve_macro_call(&self, name: &Name) -> Option<&MacroDefId> {
self.module().and_then(|(module, _)| module.find_macro(name))
pub fn resolve_macro_call(
&self,
db: &impl HirDatabase,
path: Option<Path>,
call: &ast::MacroCall,
) -> Option<MacroCallId> {
let name = path.and_then(|path| path.expand_macro_expr()).unwrap_or_else(Name::missing);
let macro_def_id = self.module().and_then(|(module, _)| module.find_macro(&name));
if let Some(def_id) = macro_def_id {
self.module().and_then(|(module, _)| {
// we do this to get the ast_id for the macro call
// if we used the ast_id from the def_id variable
// it gives us the ast_id of the defenition site
let module = module.mk_module(module.root());
let hir_file_id = module.definition_source(db).0;
let ast_id = db.ast_id_map(hir_file_id).ast_id(call).with_file_id(hir_file_id);
let call_loc = MacroCallLoc { def: *def_id, ast_id }.id(db);
Some(call_loc)
})
} else {
None
}
}
/// Returns the resolved path segments
@ -197,7 +221,7 @@ impl Resolver {
.flatten()
}
pub(crate) fn module(&self) -> Option<(&CrateDefMap, CrateModuleId)> {
fn module(&self) -> Option<(&CrateDefMap, CrateModuleId)> {
self.scopes.iter().rev().find_map(|scope| match scope {
Scope::ModuleScope(m) => Some((&*m.crate_def_map, m.module_id)),