This commit is contained in:
Aleksey Kladov 2019-03-26 19:00:11 +03:00
parent b17217b34a
commit e28db444df
7 changed files with 44 additions and 56 deletions

View file

@ -4,7 +4,7 @@ use ra_syntax::{SyntaxNode, TreeArc, SourceFile};
use ra_db::{SourceDatabase, salsa};
use crate::{
HirFileId, MacroDefId, SourceFileItems, SourceItemId, Crate, Module, HirInterner,
HirFileId, MacroDefId, AstIdMap, ErasedFileAstId, Crate, Module, HirInterner,
Function, FnSignature, ExprScopes, TypeAlias,
Struct, Enum, StructField,
Const, ConstSignature, Static,
@ -34,11 +34,11 @@ pub trait DefDatabase: SourceDatabase + AsRef<HirInterner> {
#[salsa::invoke(crate::traits::TraitData::trait_data_query)]
fn trait_data(&self, t: Trait) -> Arc<TraitData>;
#[salsa::invoke(crate::source_id::SourceFileItems::file_items_query)]
fn file_items(&self, file_id: HirFileId) -> Arc<SourceFileItems>;
#[salsa::invoke(crate::source_id::AstIdMap::ast_id_map_query)]
fn ast_id_map(&self, file_id: HirFileId) -> Arc<AstIdMap>;
#[salsa::invoke(crate::source_id::SourceFileItems::file_item_query)]
fn file_item(&self, source_item_id: SourceItemId) -> TreeArc<SyntaxNode>;
#[salsa::invoke(crate::source_id::AstIdMap::file_item_query)]
fn ast_id_to_node(&self, file_id: HirFileId, ast_id: ErasedFileAstId) -> TreeArc<SyntaxNode>;
#[salsa::invoke(RawItems::raw_items_query)]
fn raw_items(&self, file_id: HirFileId) -> Arc<RawItems>;

View file

@ -207,7 +207,7 @@ impl<'a, DB: DefDatabase> LocationCtx<&'a DB> {
pub(crate) trait AstItemDef<N: AstNode>: ArenaId + Clone {
fn interner(interner: &HirInterner) -> &LocationInterner<ItemLoc<N>, Self>;
fn from_ast(ctx: LocationCtx<&impl DefDatabase>, ast: &N) -> Self {
let items = ctx.db.file_items(ctx.file_id);
let items = ctx.db.ast_id_map(ctx.file_id);
let item_id = items.ast_id(ast);
Self::from_ast_id(ctx, item_id)
}

View file

@ -54,7 +54,7 @@ use crate::{
pub use self::{
path::{Path, PathKind},
name::Name,
source_id::{SourceFileItems, SourceItemId},
source_id::{AstIdMap, ErasedFileAstId},
ids::{HirFileId, MacroDefId, MacroCallId, MacroCallLoc, HirInterner},
nameres::{PerNs, Namespace},
ty::{Ty, ApplicationTy, TypeCtor, Substs, display::HirDisplay},

View file

@ -12,7 +12,7 @@ use ra_syntax::{
use crate::{
DefDatabase, Name, AsName, Path, HirFileId, ModuleSource,
SourceFileItems, FileAstId,
AstIdMap, FileAstId,
};
/// `RawItems` is a set of top-level items in a file (except for impls).
@ -60,7 +60,7 @@ impl RawItems {
) -> (Arc<RawItems>, Arc<ImportSourceMap>) {
let mut collector = RawItemsCollector {
raw_items: RawItems::default(),
source_file_items: db.file_items(file_id.into()),
source_ast_id_map: db.ast_id_map(file_id.into()),
source_map: ImportSourceMap::default(),
};
let source_file = db.hir_parse(file_id);
@ -167,7 +167,7 @@ pub(super) struct MacroData {
struct RawItemsCollector {
raw_items: RawItems,
source_file_items: Arc<SourceFileItems>,
source_ast_id_map: Arc<AstIdMap>,
source_map: ImportSourceMap,
}
@ -200,25 +200,25 @@ impl RawItemsCollector {
return;
}
ast::ModuleItemKind::StructDef(it) => {
(DefKind::Struct(self.source_file_items.ast_id(it)), it.name())
(DefKind::Struct(self.source_ast_id_map.ast_id(it)), it.name())
}
ast::ModuleItemKind::EnumDef(it) => {
(DefKind::Enum(self.source_file_items.ast_id(it)), it.name())
(DefKind::Enum(self.source_ast_id_map.ast_id(it)), it.name())
}
ast::ModuleItemKind::FnDef(it) => {
(DefKind::Function(self.source_file_items.ast_id(it)), it.name())
(DefKind::Function(self.source_ast_id_map.ast_id(it)), it.name())
}
ast::ModuleItemKind::TraitDef(it) => {
(DefKind::Trait(self.source_file_items.ast_id(it)), it.name())
(DefKind::Trait(self.source_ast_id_map.ast_id(it)), it.name())
}
ast::ModuleItemKind::TypeAliasDef(it) => {
(DefKind::TypeAlias(self.source_file_items.ast_id(it)), it.name())
(DefKind::TypeAlias(self.source_ast_id_map.ast_id(it)), it.name())
}
ast::ModuleItemKind::ConstDef(it) => {
(DefKind::Const(self.source_file_items.ast_id(it)), it.name())
(DefKind::Const(self.source_ast_id_map.ast_id(it)), it.name())
}
ast::ModuleItemKind::StaticDef(it) => {
(DefKind::Static(self.source_file_items.ast_id(it)), it.name())
(DefKind::Static(self.source_ast_id_map.ast_id(it)), it.name())
}
};
if let Some(name) = name {
@ -233,7 +233,7 @@ impl RawItemsCollector {
Some(it) => it.as_name(),
None => return,
};
let ast_id = self.source_file_items.ast_id(module);
let ast_id = self.source_ast_id_map.ast_id(module);
if module.has_semi() {
let item = self.raw_items.modules.alloc(ModuleData::Declaration { name, ast_id });
self.push_item(current_module, RawItem::Module(item));
@ -297,7 +297,7 @@ impl RawItemsCollector {
};
let name = m.name().map(|it| it.as_name());
let ast_id = self.source_file_items.ast_id(m);
let ast_id = self.source_ast_id_map.ast_id(m);
let export = m.has_atom_attr("macro_export");
let m = self.raw_items.macros.alloc(MacroData { ast_id, path, name, export });
self.push_item(current_module, RawItem::Macro(m));

View file

@ -54,8 +54,8 @@ fn module_from_inline(
) -> Option<Module> {
assert!(!module.has_semi());
let file_id = file_id.into();
let file_items = db.file_items(file_id);
let item_id = file_items.ast_id(module).with_file_id(file_id);
let ast_id_map = db.ast_id_map(file_id);
let item_id = ast_id_map.ast_id(module).with_file_id(file_id);
module_from_source(db, file_id, Some(item_id))
}

View file

@ -5,7 +5,9 @@ use ra_syntax::{SyntaxNodePtr, TreeArc, SyntaxNode, SourceFile, AstNode, ast};
use crate::{HirFileId, DefDatabase};
/// `AstId` points to an AST node in any file
/// `AstId` points to an AST node in any file.
///
/// It is stable across reparses, and can be used as salsa key/value.
#[derive(Debug)]
pub(crate) struct AstId<N: AstNode> {
file_id: HirFileId,
@ -37,9 +39,7 @@ impl<N: AstNode> AstId<N> {
}
pub(crate) fn to_node(&self, db: &impl DefDatabase) -> TreeArc<N> {
let source_item_id =
SourceItemId { file_id: self.file_id(), item_id: self.file_ast_id.raw };
let syntax_node = db.file_item(source_item_id);
let syntax_node = db.ast_id_to_node(self.file_id, self.file_ast_id.raw);
N::cast(&syntax_node).unwrap().to_owned()
}
}
@ -47,7 +47,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,
raw: ErasedFileAstId,
_ty: PhantomData<N>,
}
@ -76,41 +76,29 @@ impl<N: AstNode> FileAstId<N> {
}
}
/// Identifier of item within a specific file. This is stable over reparses, so
/// it's OK to use it as a salsa key/value.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
struct SourceFileItemId(RawId);
impl_arena_id!(SourceFileItemId);
pub struct ErasedFileAstId(RawId);
impl_arena_id!(ErasedFileAstId);
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct SourceItemId {
file_id: HirFileId,
item_id: SourceFileItemId,
}
/// Maps items' `SyntaxNode`s to `SourceFileItemId`s and back.
/// Maps items' `SyntaxNode`s to `ErasedFileAstId`s and back.
#[derive(Debug, PartialEq, Eq)]
pub struct SourceFileItems {
arena: Arena<SourceFileItemId, SyntaxNodePtr>,
pub struct AstIdMap {
arena: Arena<ErasedFileAstId, SyntaxNodePtr>,
}
impl SourceFileItems {
pub(crate) fn file_items_query(
db: &impl DefDatabase,
file_id: HirFileId,
) -> Arc<SourceFileItems> {
impl AstIdMap {
pub(crate) fn ast_id_map_query(db: &impl DefDatabase, file_id: HirFileId) -> Arc<AstIdMap> {
let source_file = db.hir_parse(file_id);
Arc::new(SourceFileItems::from_source_file(&source_file))
Arc::new(AstIdMap::from_source_file(&source_file))
}
pub(crate) fn file_item_query(
db: &impl DefDatabase,
source_item_id: SourceItemId,
file_id: HirFileId,
ast_id: ErasedFileAstId,
) -> TreeArc<SyntaxNode> {
let source_file = db.hir_parse(source_item_id.file_id);
db.file_items(source_item_id.file_id).arena[source_item_id.item_id]
.to_node(&source_file)
.to_owned()
let source_file = db.hir_parse(file_id);
db.ast_id_map(file_id).arena[ast_id].to_node(&source_file).to_owned()
}
pub(crate) fn ast_id<N: AstNode>(&self, item: &N) -> FileAstId<N> {
@ -118,7 +106,7 @@ impl SourceFileItems {
let raw = match self.arena.iter().find(|(_id, i)| **i == ptr) {
Some((it, _)) => it,
None => panic!(
"Can't find {:?} in SourceFileItems:\n{:?}",
"Can't find {:?} in AstIdMap:\n{:?}",
item.syntax(),
self.arena.iter().map(|(_id, i)| i).collect::<Vec<_>>(),
),
@ -127,8 +115,8 @@ impl SourceFileItems {
FileAstId { raw, _ty: PhantomData }
}
fn from_source_file(source_file: &SourceFile) -> SourceFileItems {
let mut res = SourceFileItems { arena: Arena::default() };
fn from_source_file(source_file: &SourceFile) -> AstIdMap {
let mut res = AstIdMap { 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
@ -143,7 +131,7 @@ impl SourceFileItems {
res
}
fn alloc(&mut self, item: &SyntaxNode) -> SourceFileItemId {
fn alloc(&mut self, item: &SyntaxNode) -> ErasedFileAstId {
self.arena.alloc(SyntaxNodePtr::new(item))
}
}

View file

@ -220,8 +220,8 @@ impl RootDatabase {
self.query(ra_db::ParseQuery).sweep(sweep);
self.query(hir::db::HirParseQuery).sweep(sweep);
self.query(hir::db::FileItemsQuery).sweep(sweep);
self.query(hir::db::FileItemQuery).sweep(sweep);
self.query(hir::db::AstIdMapQuery).sweep(sweep);
self.query(hir::db::AstIdToNodeQuery).sweep(sweep);
self.query(hir::db::RawItemsWithSourceMapQuery).sweep(sweep);
self.query(hir::db::BodyWithSourceMapQuery).sweep(sweep);