This commit is contained in:
Aleksey Kladov 2019-03-26 18:57:57 +03:00
parent cffa3f960e
commit b17217b34a
3 changed files with 21 additions and 21 deletions

View File

@ -22,7 +22,7 @@ pub trait DefDatabase: SourceDatabase + AsRef<HirInterner> {
#[salsa::invoke(crate::ids::macro_def_query)]
fn macro_def(&self, macro_id: MacroDefId) -> Option<Arc<mbe::MacroRules>>;
#[salsa::invoke(HirFileId::hir_parse)]
#[salsa::invoke(HirFileId::hir_parse_query)]
fn hir_parse(&self, file_id: HirFileId) -> TreeArc<SourceFile>;
#[salsa::invoke(crate::adt::StructData::struct_data_query)]

View File

@ -82,7 +82,10 @@ impl HirFileId {
}
}
pub(crate) fn hir_parse(db: &impl DefDatabase, file_id: HirFileId) -> TreeArc<SourceFile> {
pub(crate) fn hir_parse_query(
db: &impl DefDatabase,
file_id: HirFileId,
) -> TreeArc<SourceFile> {
match file_id.0 {
HirFileIdRepr::File(file_id) => db.parse(file_id),
HirFileIdRepr::Macro(macro_call_id) => {
@ -122,7 +125,6 @@ impl From<MacroCallId> for HirFileId {
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct MacroDefId(pub(crate) AstId<ast::MacroCall>);
pub(crate) fn macro_def_query(db: &impl DefDatabase, id: MacroDefId) -> Option<Arc<MacroRules>> {
@ -152,7 +154,6 @@ impl MacroCallId {
}
impl MacroCallLoc {
#[allow(unused)]
pub(crate) fn id(&self, db: &impl AsRef<HirInterner>) -> MacroCallId {
db.as_ref().macros.loc2id(&self)
}

View File

@ -5,6 +5,7 @@ use ra_syntax::{SyntaxNodePtr, TreeArc, SyntaxNode, SourceFile, AstNode, ast};
use crate::{HirFileId, DefDatabase};
/// `AstId` points to an AST node in any file
#[derive(Debug)]
pub(crate) struct AstId<N: AstNode> {
file_id: HirFileId,
@ -43,6 +44,7 @@ impl<N: AstNode> AstId<N> {
}
}
/// `AstId` points to an AST node in a specific file.
#[derive(Debug)]
pub(crate) struct FileAstId<N: AstNode> {
raw: SourceFileItemId,
@ -89,7 +91,6 @@ pub struct SourceItemId {
/// Maps items' `SyntaxNode`s to `SourceFileItemId`s and back.
#[derive(Debug, PartialEq, Eq)]
pub struct SourceFileItems {
file_id: HirFileId,
arena: Arena<SourceFileItemId, SyntaxNodePtr>,
}
@ -99,7 +100,7 @@ impl SourceFileItems {
file_id: HirFileId,
) -> Arc<SourceFileItems> {
let source_file = db.hir_parse(file_id);
Arc::new(SourceFileItems::from_source_file(&source_file, file_id))
Arc::new(SourceFileItems::from_source_file(&source_file))
}
pub(crate) fn file_item_query(
@ -113,11 +114,21 @@ impl SourceFileItems {
}
pub(crate) fn ast_id<N: AstNode>(&self, item: &N) -> FileAstId<N> {
FileAstId { raw: self.id_of_unchecked(item.syntax()), _ty: PhantomData }
let ptr = SyntaxNodePtr::new(item.syntax());
let raw = match self.arena.iter().find(|(_id, i)| **i == ptr) {
Some((it, _)) => it,
None => panic!(
"Can't find {:?} in SourceFileItems:\n{:?}",
item.syntax(),
self.arena.iter().map(|(_id, i)| i).collect::<Vec<_>>(),
),
};
FileAstId { raw, _ty: PhantomData }
}
fn from_source_file(source_file: &SourceFile, file_id: HirFileId) -> SourceFileItems {
let mut res = SourceFileItems { file_id, arena: Arena::default() };
fn from_source_file(source_file: &SourceFile) -> SourceFileItems {
let mut res = SourceFileItems { arena: Arena::default() };
// By walking the tree in bread-first order we make sure that parents
// get lower ids then children. That is, adding a new child does not
// change parent's id. This means that, say, adding a new function to a
@ -135,18 +146,6 @@ impl SourceFileItems {
fn alloc(&mut self, item: &SyntaxNode) -> SourceFileItemId {
self.arena.alloc(SyntaxNodePtr::new(item))
}
fn id_of_unchecked(&self, item: &SyntaxNode) -> SourceFileItemId {
let ptr = SyntaxNodePtr::new(item);
if let Some((id, _)) = self.arena.iter().find(|(_id, i)| **i == ptr) {
return id;
}
panic!(
"Can't find {:?} in SourceFileItems:\n{:?}",
item,
self.arena.iter().map(|(_id, i)| i).collect::<Vec<_>>(),
);
}
}
/// Walks the subtree in bfs order, calling `f` for each node.