Store crate info in MacroDefId

This commit is contained in:
uHOOCCOOHu 2019-09-27 00:16:55 +08:00
parent 128dc5355b
commit 8cd23a4fb8
No known key found for this signature in database
GPG Key ID: CED392DE0C483D00
3 changed files with 11 additions and 5 deletions

View File

@ -119,7 +119,7 @@ impl HasSource for TypeAlias {
impl HasSource for MacroDef { impl HasSource for MacroDef {
type Ast = ast::MacroCall; type Ast = ast::MacroCall;
fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<ast::MacroCall> { fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<ast::MacroCall> {
Source { file_id: self.id.0.file_id(), ast: self.id.0.to_node(db) } Source { file_id: self.id.ast_id.file_id(), ast: self.id.ast_id.to_node(db) }
} }
} }

View File

@ -10,7 +10,7 @@ use ra_syntax::{ast, AstNode, Parse, SyntaxNode};
use crate::{ use crate::{
db::{AstDatabase, DefDatabase, InternDatabase}, db::{AstDatabase, DefDatabase, InternDatabase},
AstId, FileAstId, Module, Source, AstId, Crate, FileAstId, Module, Source,
}; };
/// hir makes heavy use of ids: integer (u32) handlers to various things. You /// hir makes heavy use of ids: integer (u32) handlers to various things. You
@ -121,10 +121,13 @@ impl From<FileId> for HirFileId {
} }
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct MacroDefId(pub(crate) AstId<ast::MacroCall>); pub struct MacroDefId {
pub(crate) ast_id: AstId<ast::MacroCall>,
pub(crate) krate: Crate,
}
pub(crate) fn macro_def_query(db: &impl AstDatabase, id: MacroDefId) -> Option<Arc<MacroRules>> { pub(crate) fn macro_def_query(db: &impl AstDatabase, id: MacroDefId) -> Option<Arc<MacroRules>> {
let macro_call = id.0.to_node(db); let macro_call = id.ast_id.to_node(db);
let arg = macro_call.token_tree()?; let arg = macro_call.token_tree()?;
let (tt, _) = mbe::ast_to_token_tree(&arg).or_else(|| { let (tt, _) = mbe::ast_to_token_tree(&arg).or_else(|| {
log::warn!("fail on macro_def to token tree: {:#?}", arg); log::warn!("fail on macro_def to token tree: {:#?}", arg);

View File

@ -662,7 +662,10 @@ where
// Case 1: macro rules, define a macro in crate-global mutable scope // Case 1: macro rules, define a macro in crate-global mutable scope
if is_macro_rules(&mac.path) { if is_macro_rules(&mac.path) {
if let Some(name) = &mac.name { if let Some(name) = &mac.name {
let macro_id = MacroDefId(mac.ast_id.with_file_id(self.file_id)); let macro_id = MacroDefId {
ast_id: mac.ast_id.with_file_id(self.file_id),
krate: self.def_collector.def_map.krate,
};
let macro_ = MacroDef { id: macro_id }; let macro_ = MacroDef { id: macro_id };
self.def_collector.define_macro(self.module_id, name.clone(), macro_, mac.export); self.def_collector.define_macro(self.module_id, name.clone(), macro_, mac.export);
} }