From 9fe09db771aa3890ac8a0eeb1d9e6097060fad06 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 24 Jan 2019 12:41:08 +0300 Subject: encapsulate hir locations --- crates/ra_hir/src/db.rs | 10 +++------- crates/ra_hir/src/ids.rs | 34 ++++++++++++++++++++-------------- crates/ra_hir/src/impl_block.rs | 6 +++--- crates/ra_hir/src/lib.rs | 2 +- crates/ra_hir/src/mock.rs | 27 ++++++++------------------- crates/ra_ide_api/src/db.rs | 36 ++++++++---------------------------- crates/ra_ide_api/src/status.rs | 4 ++-- 7 files changed, 45 insertions(+), 74 deletions(-) (limited to 'crates') diff --git a/crates/ra_hir/src/db.rs b/crates/ra_hir/src/db.rs index f383701d7..cc03da1e2 100644 --- a/crates/ra_hir/src/db.rs +++ b/crates/ra_hir/src/db.rs @@ -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> - + AsRef> -{ +pub trait HirDatabase: SyntaxDatabase + AsRef { #[salsa::invoke(HirFileId::hir_source_file)] fn hir_source_file(&self, file_id: HirFileId) -> TreeArc; diff --git a/crates/ra_hir/src/ids.rs b/crates/ra_hir/src/ids.rs index b93b7f397..43f0e81f9 100644 --- a/crates/ra_hir/src/ids.rs +++ b/crates/ra_hir/src/ids.rs @@ -8,6 +8,18 @@ use crate::{ module_tree::ModuleId, }; +#[derive(Debug, Default)] +pub struct HirInterner { + defs: LocationIntener, + macros: LocationIntener, +} + +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>, - ) -> MacroCallLoc { - db.as_ref().id2loc(self) + pub(crate) fn loc(self, db: &impl AsRef) -> MacroCallLoc { + db.as_ref().macros.id2loc(self) } } impl MacroCallLoc { #[allow(unused)] - pub(crate) fn id( - &self, - db: &impl AsRef>, - ) -> MacroCallId { - db.as_ref().loc2id(&self) + pub(crate) fn id(&self, db: &impl AsRef) -> 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>) -> DefLoc { - db.as_ref().id2loc(self) + pub(crate) fn loc(self, db: &impl AsRef) -> 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>) -> DefId { - db.as_ref().loc2id(&self) + pub(crate) fn id(&self, db: &impl AsRef) -> DefId { + db.as_ref().defs.loc2id(&self) } } diff --git a/crates/ra_hir/src/impl_block.rs b/crates/ra_hir/src/impl_block.rs index ab996a12c..551d0d149 100644 --- a/crates/ra_hir/src/impl_block.rs +++ b/crates/ra_hir/src/impl_block.rs @@ -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>, + db: &impl AsRef, file_items: &SourceFileItems, module: &Module, node: &ast::ImplBlock, diff --git a/crates/ra_hir/src/lib.rs b/crates/ra_hir/src/lib.rs index f517f71e0..87b5a8b8a 100644 --- a/crates/ra_hir/src/lib.rs +++ b/crates/ra_hir/src/lib.rs @@ -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, diff --git a/crates/ra_hir/src/mock.rs b/crates/ra_hir/src/mock.rs index 6e6f2f04b..4145c8048 100644 --- a/crates/ra_hir/src/mock.rs +++ b/crates/ra_hir/src/mock.rs @@ -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>>>, runtime: salsa::Runtime, - id_maps: Arc, + interner: Arc, file_counter: u32, } @@ -123,12 +123,6 @@ impl MockDatabase { } } -#[derive(Debug, Default)] -struct IdMaps { - defs: LocationIntener, - macros: LocationIntener, -} - impl salsa::Database for MockDatabase { fn salsa_runtime(&self) -> &salsa::Runtime { &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> for MockDatabase { - fn as_ref(&self) -> &LocationIntener { - &self.id_maps.defs - } -} -impl AsRef> for MockDatabase { - fn as_ref(&self) -> &LocationIntener { - &self.id_maps.macros +impl AsRef for MockDatabase { + fn as_ref(&self) -> &HirInterner { + &self.interner } } diff --git a/crates/ra_ide_api/src/db.rs b/crates/ra_ide_api/src/db.rs index a1b666899..ba0eb1cb8 100644 --- a/crates/ra_ide_api/src/db.rs +++ b/crates/ra_ide_api/src/db.rs @@ -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, - id_maps: Arc, -} - -#[derive(Default)] -struct IdMaps { - defs: LocationIntener, - macros: LocationIntener, -} - -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, } 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 { 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> for RootDatabase { - fn as_ref(&self) -> &LocationIntener { - &self.id_maps.defs - } -} - -impl AsRef> for RootDatabase { - fn as_ref(&self) -> &LocationIntener { - &self.id_maps.macros +impl AsRef for RootDatabase { + fn as_ref(&self) -> &hir::HirInterner { + &self.interner } } diff --git a/crates/ra_ide_api/src/status.rs b/crates/ra_ide_api/src/status.rs index d3e04be23..5c14cbdeb 100644 --- a/crates/ra_ide_api/src/status.rs +++ b/crates/ra_ide_api/src/status.rs @@ -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::>().len(); let n_defs = { - let interner: &LocationIntener = 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) -- cgit v1.2.3