624: encapsulate hir locations r=matklad a=matklad



Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
This commit is contained in:
bors[bot] 2019-01-24 09:41:32 +00:00
commit cfb085ded8
7 changed files with 45 additions and 74 deletions

View File

@ -1,10 +1,10 @@
use std::sync::Arc; use std::sync::Arc;
use ra_syntax::{SyntaxNode, TreeArc, SourceFile}; use ra_syntax::{SyntaxNode, TreeArc, SourceFile};
use ra_db::{SourceRootId, LocationIntener, SyntaxDatabase, salsa}; use ra_db::{SourceRootId, SyntaxDatabase, salsa};
use crate::{ use crate::{
DefLoc, DefId, MacroCallLoc, MacroCallId, Name, HirFileId, HirInterner, DefId, MacroCallId, Name, HirFileId,
SourceFileItems, SourceItemId, Crate, SourceFileItems, SourceItemId, Crate,
query_definitions, query_definitions,
FnSignature, FnScopes, FnSignature, FnScopes,
@ -18,11 +18,7 @@ use crate::{
}; };
#[salsa::query_group] #[salsa::query_group]
pub trait HirDatabase: pub trait HirDatabase: SyntaxDatabase + AsRef<HirInterner> {
SyntaxDatabase
+ AsRef<LocationIntener<DefLoc, DefId>>
+ AsRef<LocationIntener<MacroCallLoc, MacroCallId>>
{
#[salsa::invoke(HirFileId::hir_source_file)] #[salsa::invoke(HirFileId::hir_source_file)]
fn hir_source_file(&self, file_id: HirFileId) -> TreeArc<SourceFile>; fn hir_source_file(&self, file_id: HirFileId) -> TreeArc<SourceFile>;

View File

@ -8,6 +8,18 @@ use crate::{
module_tree::ModuleId, module_tree::ModuleId,
}; };
#[derive(Debug, Default)]
pub struct HirInterner {
defs: LocationIntener<DefLoc, DefId>,
macros: LocationIntener<MacroCallLoc, MacroCallId>,
}
impl HirInterner {
pub fn len(&self) -> usize {
self.defs.len() + self.macros.len()
}
}
/// 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
/// can think of id as a pointer (but without a lifetime) or a file descriptor /// can think of id as a pointer (but without a lifetime) or a file descriptor
/// (but for hir objects). /// (but for hir objects).
@ -106,21 +118,15 @@ pub struct MacroCallLoc {
} }
impl MacroCallId { impl MacroCallId {
pub(crate) fn loc( pub(crate) fn loc(self, db: &impl AsRef<HirInterner>) -> MacroCallLoc {
self, db.as_ref().macros.id2loc(self)
db: &impl AsRef<LocationIntener<MacroCallLoc, MacroCallId>>,
) -> MacroCallLoc {
db.as_ref().id2loc(self)
} }
} }
impl MacroCallLoc { impl MacroCallLoc {
#[allow(unused)] #[allow(unused)]
pub(crate) fn id( pub(crate) fn id(&self, db: &impl AsRef<HirInterner>) -> MacroCallId {
&self, db.as_ref().macros.loc2id(&self)
db: &impl AsRef<LocationIntener<MacroCallLoc, MacroCallId>>,
) -> MacroCallId {
db.as_ref().loc2id(&self)
} }
} }
@ -164,8 +170,8 @@ pub(crate) enum DefKind {
} }
impl DefId { impl DefId {
pub(crate) fn loc(self, db: &impl AsRef<LocationIntener<DefLoc, DefId>>) -> DefLoc { pub(crate) fn loc(self, db: &impl AsRef<HirInterner>) -> DefLoc {
db.as_ref().id2loc(self) db.as_ref().defs.id2loc(self)
} }
pub fn resolve(self, db: &impl HirDatabase) -> Def { pub fn resolve(self, db: &impl HirDatabase) -> Def {
@ -233,8 +239,8 @@ impl DefId {
} }
impl DefLoc { impl DefLoc {
pub(crate) fn id(&self, db: &impl AsRef<LocationIntener<DefLoc, DefId>>) -> DefId { pub(crate) fn id(&self, db: &impl AsRef<HirInterner>) -> DefId {
db.as_ref().loc2id(&self) db.as_ref().defs.loc2id(&self)
} }
} }

View File

@ -3,11 +3,11 @@ use rustc_hash::FxHashMap;
use ra_arena::{Arena, RawId, impl_arena_id}; use ra_arena::{Arena, RawId, impl_arena_id};
use ra_syntax::ast::{self, AstNode}; use ra_syntax::ast::{self, AstNode};
use ra_db::{LocationIntener, SourceRootId}; use ra_db::{SourceRootId};
use crate::{ use crate::{
DefId, DefLoc, DefKind, SourceItemId, SourceFileItems, DefId, DefLoc, DefKind, SourceItemId, SourceFileItems,
Function, Function, HirInterner,
db::HirDatabase, db::HirDatabase,
type_ref::TypeRef, type_ref::TypeRef,
module_tree::ModuleId, module_tree::ModuleId,
@ -66,7 +66,7 @@ pub struct ImplData {
impl ImplData { impl ImplData {
pub(crate) fn from_ast( pub(crate) fn from_ast(
db: &impl AsRef<LocationIntener<DefLoc, DefId>>, db: &impl AsRef<HirInterner>,
file_items: &SourceFileItems, file_items: &SourceFileItems,
module: &Module, module: &Module,
node: &ast::ImplBlock, node: &ast::ImplBlock,

View File

@ -40,7 +40,7 @@ use crate::{
pub use self::{ pub use self::{
path::{Path, PathKind}, path::{Path, PathKind},
name::Name, name::Name,
ids::{HirFileId, DefId, DefLoc, MacroCallId, MacroCallLoc}, ids::{HirFileId, DefId, DefLoc, MacroCallId, MacroCallLoc, HirInterner},
macros::{MacroDef, MacroInput, MacroExpansion}, macros::{MacroDef, MacroInput, MacroExpansion},
nameres::{ItemMap, PerNs, Namespace, Resolution}, nameres::{ItemMap, PerNs, Namespace, Resolution},
ty::Ty, ty::Ty,

View File

@ -2,13 +2,13 @@ use std::{sync::Arc, panic};
use parking_lot::Mutex; use parking_lot::Mutex;
use ra_db::{ use ra_db::{
LocationIntener, BaseDatabase, FilePosition, FileId, CrateGraph, SourceRoot, SourceRootId, BaseDatabase, FilePosition, FileId, CrateGraph, SourceRoot, SourceRootId,
salsa::{self, Database}, salsa::{self, Database},
}; };
use relative_path::RelativePathBuf; use relative_path::RelativePathBuf;
use test_utils::{parse_fixture, CURSOR_MARKER, extract_offset}; use test_utils::{parse_fixture, CURSOR_MARKER, extract_offset};
use crate::{db, DefId, DefLoc, MacroCallId, MacroCallLoc}; use crate::{db, HirInterner};
pub const WORKSPACE: SourceRootId = SourceRootId(0); pub const WORKSPACE: SourceRootId = SourceRootId(0);
@ -16,7 +16,7 @@ pub const WORKSPACE: SourceRootId = SourceRootId(0);
pub(crate) struct MockDatabase { pub(crate) struct MockDatabase {
events: Mutex<Option<Vec<salsa::Event<MockDatabase>>>>, events: Mutex<Option<Vec<salsa::Event<MockDatabase>>>>,
runtime: salsa::Runtime<MockDatabase>, runtime: salsa::Runtime<MockDatabase>,
id_maps: Arc<IdMaps>, interner: Arc<HirInterner>,
file_counter: u32, file_counter: u32,
} }
@ -123,12 +123,6 @@ impl MockDatabase {
} }
} }
#[derive(Debug, Default)]
struct IdMaps {
defs: LocationIntener<DefLoc, DefId>,
macros: LocationIntener<MacroCallLoc, MacroCallId>,
}
impl salsa::Database for MockDatabase { impl salsa::Database for MockDatabase {
fn salsa_runtime(&self) -> &salsa::Runtime<MockDatabase> { fn salsa_runtime(&self) -> &salsa::Runtime<MockDatabase> {
&self.runtime &self.runtime
@ -147,7 +141,7 @@ impl Default for MockDatabase {
let mut db = MockDatabase { let mut db = MockDatabase {
events: Default::default(), events: Default::default(),
runtime: salsa::Runtime::default(), runtime: salsa::Runtime::default(),
id_maps: Default::default(), interner: Default::default(),
file_counter: 0, file_counter: 0,
}; };
db.query_mut(ra_db::CrateGraphQuery) db.query_mut(ra_db::CrateGraphQuery)
@ -165,7 +159,7 @@ impl salsa::ParallelDatabase for MockDatabase {
salsa::Snapshot::new(MockDatabase { salsa::Snapshot::new(MockDatabase {
events: Default::default(), events: Default::default(),
runtime: self.runtime.snapshot(self), runtime: self.runtime.snapshot(self),
id_maps: self.id_maps.clone(), interner: Arc::clone(&self.interner),
file_counter: self.file_counter, file_counter: self.file_counter,
}) })
} }
@ -173,14 +167,9 @@ impl salsa::ParallelDatabase for MockDatabase {
impl BaseDatabase for MockDatabase {} impl BaseDatabase for MockDatabase {}
impl AsRef<LocationIntener<DefLoc, DefId>> for MockDatabase { impl AsRef<HirInterner> for MockDatabase {
fn as_ref(&self) -> &LocationIntener<DefLoc, DefId> { fn as_ref(&self) -> &HirInterner {
&self.id_maps.defs &self.interner
}
}
impl AsRef<LocationIntener<MacroCallLoc, MacroCallId>> for MockDatabase {
fn as_ref(&self) -> &LocationIntener<MacroCallLoc, MacroCallId> {
&self.id_maps.macros
} }
} }

View File

@ -1,7 +1,7 @@
use std::{fmt, sync::Arc}; use std::sync::Arc;
use ra_db::{ use ra_db::{
LocationIntener, BaseDatabase, FileId, Canceled, BaseDatabase, FileId, Canceled,
salsa::{self, Database}, salsa::{self, Database},
}; };
@ -10,21 +10,7 @@ use crate::{symbol_index, LineIndex};
#[derive(Debug)] #[derive(Debug)]
pub(crate) struct RootDatabase { pub(crate) struct RootDatabase {
runtime: salsa::Runtime<RootDatabase>, runtime: salsa::Runtime<RootDatabase>,
id_maps: Arc<IdMaps>, interner: Arc<hir::HirInterner>,
}
#[derive(Default)]
struct IdMaps {
defs: LocationIntener<hir::DefLoc, hir::DefId>,
macros: LocationIntener<hir::MacroCallLoc, hir::MacroCallId>,
}
impl fmt::Debug for IdMaps {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("IdMaps")
.field("n_defs", &self.defs.len())
.finish()
}
} }
impl salsa::Database for RootDatabase { impl salsa::Database for RootDatabase {
@ -40,7 +26,7 @@ impl Default for RootDatabase {
fn default() -> RootDatabase { fn default() -> RootDatabase {
let mut db = RootDatabase { let mut db = RootDatabase {
runtime: salsa::Runtime::default(), runtime: salsa::Runtime::default(),
id_maps: Default::default(), interner: Default::default(),
}; };
db.query_mut(ra_db::CrateGraphQuery) db.query_mut(ra_db::CrateGraphQuery)
.set((), Default::default()); .set((), Default::default());
@ -56,22 +42,16 @@ impl salsa::ParallelDatabase for RootDatabase {
fn snapshot(&self) -> salsa::Snapshot<RootDatabase> { fn snapshot(&self) -> salsa::Snapshot<RootDatabase> {
salsa::Snapshot::new(RootDatabase { salsa::Snapshot::new(RootDatabase {
runtime: self.runtime.snapshot(self), runtime: self.runtime.snapshot(self),
id_maps: self.id_maps.clone(), interner: Arc::clone(&self.interner),
}) })
} }
} }
impl BaseDatabase for RootDatabase {} impl BaseDatabase for RootDatabase {}
impl AsRef<LocationIntener<hir::DefLoc, hir::DefId>> for RootDatabase { impl AsRef<hir::HirInterner> for RootDatabase {
fn as_ref(&self) -> &LocationIntener<hir::DefLoc, hir::DefId> { fn as_ref(&self) -> &hir::HirInterner {
&self.id_maps.defs &self.interner
}
}
impl AsRef<LocationIntener<hir::MacroCallLoc, hir::MacroCallId>> for RootDatabase {
fn as_ref(&self) -> &LocationIntener<hir::MacroCallLoc, hir::MacroCallId> {
&self.id_maps.macros
} }
} }

View File

@ -1,5 +1,5 @@
use ra_db::{ use ra_db::{
LocationIntener, SourceFileQuery, SourceFileQuery,
salsa::{Database, debug::DebugQueryTable}, salsa::{Database, debug::DebugQueryTable},
}; };
@ -8,7 +8,7 @@ use crate::db::RootDatabase;
pub(crate) fn status(db: &RootDatabase) -> String { pub(crate) fn status(db: &RootDatabase) -> String {
let n_parsed_files = db.query(SourceFileQuery).keys::<Vec<_>>().len(); let n_parsed_files = db.query(SourceFileQuery).keys::<Vec<_>>().len();
let n_defs = { let n_defs = {
let interner: &LocationIntener<hir::DefLoc, hir::DefId> = db.as_ref(); let interner: &hir::HirInterner = db.as_ref();
interner.len() interner.len()
}; };
format!("#n_parsed_files {}\n#n_defs {}\n", n_parsed_files, n_defs) format!("#n_parsed_files {}\n#n_defs {}\n", n_parsed_files, n_defs)