store macro kind in HirFileId
This commit is contained in:
parent
ee0ab7c00b
commit
101b3abfd7
4 changed files with 38 additions and 21 deletions
|
@ -6,11 +6,11 @@ use rustc_hash::FxHashMap;
|
|||
use ra_arena::{Arena, RawId, impl_arena_id, map::ArenaMap};
|
||||
use ra_syntax::{
|
||||
SyntaxNodePtr, AstPtr, AstNode,
|
||||
ast::{self, LoopBodyOwner, ArgListOwner, NameOwner, LiteralKind,ArrayExprKind, TypeAscriptionOwner}
|
||||
ast::{self, LoopBodyOwner, ArgListOwner, NameOwner, LiteralKind,ArrayExprKind, TypeAscriptionOwner},
|
||||
};
|
||||
|
||||
use crate::{
|
||||
Path, Name, HirDatabase, Resolver,DefWithBody, Either, HirFileId, MacroCallLoc,
|
||||
Path, Name, HirDatabase, Resolver,DefWithBody, Either, HirFileId, MacroCallLoc, MacroFileKind,
|
||||
name::AsName,
|
||||
type_ref::{Mutability, TypeRef},
|
||||
};
|
||||
|
@ -833,8 +833,11 @@ where
|
|||
if let Some(tt) = self.db.macro_expand(call_id).ok() {
|
||||
if let Some(expr) = mbe::token_tree_to_expr(&tt).ok() {
|
||||
log::debug!("macro expansion {}", expr.syntax().debug_dump());
|
||||
let old_file_id =
|
||||
std::mem::replace(&mut self.current_file_id, call_id.into());
|
||||
let old_file_id = std::mem::replace(
|
||||
&mut self.current_file_id,
|
||||
//BUG
|
||||
call_id.as_file(MacroFileKind::Items),
|
||||
);
|
||||
let id = self.collect_expr(&expr);
|
||||
self.current_file_id = old_file_id;
|
||||
return id;
|
||||
|
|
|
@ -39,8 +39,8 @@ impl HirFileId {
|
|||
pub fn original_file(self, db: &impl DefDatabase) -> FileId {
|
||||
match self.0 {
|
||||
HirFileIdRepr::File(file_id) => file_id,
|
||||
HirFileIdRepr::Macro(macro_call_id) => {
|
||||
let loc = macro_call_id.loc(db);
|
||||
HirFileIdRepr::Macro(macro_file) => {
|
||||
let loc = macro_file.macro_call_id.loc(db);
|
||||
loc.ast_id.file_id().original_file(db)
|
||||
}
|
||||
}
|
||||
|
@ -62,9 +62,10 @@ impl HirFileId {
|
|||
) -> TreeArc<SourceFile> {
|
||||
match file_id.0 {
|
||||
HirFileIdRepr::File(file_id) => db.parse(file_id),
|
||||
HirFileIdRepr::Macro(macro_call_id) => {
|
||||
match db.macro_expand(macro_call_id) {
|
||||
Ok(tt) => mbe::token_tree_to_ast_item_list(&tt),
|
||||
HirFileIdRepr::Macro(macro_file) => {
|
||||
let macro_call_id = macro_file.macro_call_id;
|
||||
let tt = match db.macro_expand(macro_call_id) {
|
||||
Ok(it) => it,
|
||||
Err(err) => {
|
||||
// Note:
|
||||
// The final goal we would like to make all parse_macro success,
|
||||
|
@ -74,10 +75,12 @@ impl HirFileId {
|
|||
err,
|
||||
macro_call_id.debug_dump(db)
|
||||
);
|
||||
|
||||
// returning an empty string looks fishy...
|
||||
SourceFile::parse("")
|
||||
return SourceFile::parse("");
|
||||
}
|
||||
};
|
||||
match macro_file.macro_file_kind {
|
||||
MacroFileKind::Items => mbe::token_tree_to_ast_item_list(&tt),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -87,7 +90,18 @@ impl HirFileId {
|
|||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||
enum HirFileIdRepr {
|
||||
File(FileId),
|
||||
Macro(MacroCallId),
|
||||
Macro(MacroFile),
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||
struct MacroFile {
|
||||
macro_call_id: MacroCallId,
|
||||
macro_file_kind: MacroFileKind,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||
pub(crate) enum MacroFileKind {
|
||||
Items,
|
||||
}
|
||||
|
||||
impl From<FileId> for HirFileId {
|
||||
|
@ -96,12 +110,6 @@ impl From<FileId> for HirFileId {
|
|||
}
|
||||
}
|
||||
|
||||
impl From<MacroCallId> for HirFileId {
|
||||
fn from(macro_call_id: MacroCallId) -> HirFileId {
|
||||
HirFileId(HirFileIdRepr::Macro(macro_call_id))
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||
pub struct MacroDefId(pub(crate) AstId<ast::MacroCall>);
|
||||
|
||||
|
@ -173,6 +181,11 @@ impl MacroCallId {
|
|||
pub(crate) fn loc(self, db: &impl DefDatabase) -> MacroCallLoc {
|
||||
db.lookup_intern_macro(self)
|
||||
}
|
||||
|
||||
pub(crate) fn as_file(self, kind: MacroFileKind) -> HirFileId {
|
||||
let macro_file = MacroFile { macro_call_id: self, macro_file_kind: kind };
|
||||
HirFileId(HirFileIdRepr::Macro(macro_file))
|
||||
}
|
||||
}
|
||||
|
||||
impl MacroCallLoc {
|
||||
|
@ -342,7 +355,7 @@ impl MacroCallId {
|
|||
let syntax_str = node.syntax().text().chunks().collect::<Vec<_>>().join(" ");
|
||||
|
||||
// dump the file name
|
||||
let file_id: HirFileId = self.clone().into();
|
||||
let file_id: HirFileId = self.loc(db).ast_id.file_id();
|
||||
let original = file_id.original_file(db);
|
||||
let macro_rules = db.macro_def(loc.def);
|
||||
|
||||
|
|
|
@ -53,6 +53,7 @@ use crate::{
|
|||
name::{AsName, KnownName},
|
||||
source_id::{FileAstId, AstId},
|
||||
resolve::Resolver,
|
||||
ids::MacroFileKind,
|
||||
};
|
||||
|
||||
pub use self::{
|
||||
|
|
|
@ -15,7 +15,7 @@ use crate::{
|
|||
diagnostics::DefDiagnostic,
|
||||
raw,
|
||||
},
|
||||
ids::{AstItemDef, LocationCtx, MacroCallLoc, MacroCallId, MacroDefId},
|
||||
ids::{AstItemDef, LocationCtx, MacroCallLoc, MacroCallId, MacroDefId, MacroFileKind},
|
||||
AstId,
|
||||
};
|
||||
|
||||
|
@ -371,7 +371,7 @@ where
|
|||
self.macro_stack_monitor.increase(macro_def_id);
|
||||
|
||||
if !self.macro_stack_monitor.is_poison(macro_def_id) {
|
||||
let file_id: HirFileId = macro_call_id.into();
|
||||
let file_id: HirFileId = macro_call_id.as_file(MacroFileKind::Items);
|
||||
let raw_items = self.db.raw_items(file_id);
|
||||
ModCollector { def_collector: &mut *self, file_id, module_id, raw_items: &raw_items }
|
||||
.collect(raw_items.items());
|
||||
|
|
Loading…
Add table
Reference in a new issue