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

View File

@ -8,6 +8,18 @@ use crate::{
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
/// can think of id as a pointer (but without a lifetime) or a file descriptor
/// (but for hir objects).
@ -106,21 +118,15 @@ pub struct MacroCallLoc {
}
impl MacroCallId {
pub(crate) fn loc(
self,
db: &impl AsRef<LocationIntener<MacroCallLoc, MacroCallId>>,
) -> MacroCallLoc {
db.as_ref().id2loc(self)
pub(crate) fn loc(self, db: &impl AsRef<HirInterner>) -> MacroCallLoc {
db.as_ref().macros.id2loc(self)
}
}
impl MacroCallLoc {
#[allow(unused)]
pub(crate) fn id(
&self,
db: &impl AsRef<LocationIntener<MacroCallLoc, MacroCallId>>,
) -> MacroCallId {
db.as_ref().loc2id(&self)
pub(crate) fn id(&self, db: &impl AsRef<HirInterner>) -> MacroCallId {
db.as_ref().macros.loc2id(&self)
}
}
@ -164,8 +170,8 @@ pub(crate) enum DefKind {
}
impl DefId {
pub(crate) fn loc(self, db: &impl AsRef<LocationIntener<DefLoc, DefId>>) -> DefLoc {
db.as_ref().id2loc(self)
pub(crate) fn loc(self, db: &impl AsRef<HirInterner>) -> DefLoc {
db.as_ref().defs.id2loc(self)
}
pub fn resolve(self, db: &impl HirDatabase) -> Def {
@ -233,8 +239,8 @@ impl DefId {
}
impl DefLoc {
pub(crate) fn id(&self, db: &impl AsRef<LocationIntener<DefLoc, DefId>>) -> DefId {
db.as_ref().loc2id(&self)
pub(crate) fn id(&self, db: &impl AsRef<HirInterner>) -> DefId {
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_syntax::ast::{self, AstNode};
use ra_db::{LocationIntener, SourceRootId};
use ra_db::{SourceRootId};
use crate::{
DefId, DefLoc, DefKind, SourceItemId, SourceFileItems,
Function,
Function, HirInterner,
db::HirDatabase,
type_ref::TypeRef,
module_tree::ModuleId,
@ -66,7 +66,7 @@ pub struct ImplData {
impl ImplData {
pub(crate) fn from_ast(
db: &impl AsRef<LocationIntener<DefLoc, DefId>>,
db: &impl AsRef<HirInterner>,
file_items: &SourceFileItems,
module: &Module,
node: &ast::ImplBlock,

View File

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

View File

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

View File

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

View File

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