diff options
author | Aleksey Kladov <[email protected]> | 2019-01-24 09:41:08 +0000 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2019-01-24 09:41:08 +0000 |
commit | 9fe09db771aa3890ac8a0eeb1d9e6097060fad06 (patch) | |
tree | 18b34194e0d01f5c186bc69f50879c568237dcf0 /crates | |
parent | 6a0a4a564accb12b48e703245655e3e3a0637445 (diff) |
encapsulate hir locations
Diffstat (limited to 'crates')
-rw-r--r-- | crates/ra_hir/src/db.rs | 10 | ||||
-rw-r--r-- | crates/ra_hir/src/ids.rs | 34 | ||||
-rw-r--r-- | crates/ra_hir/src/impl_block.rs | 6 | ||||
-rw-r--r-- | crates/ra_hir/src/lib.rs | 2 | ||||
-rw-r--r-- | crates/ra_hir/src/mock.rs | 27 | ||||
-rw-r--r-- | crates/ra_ide_api/src/db.rs | 36 | ||||
-rw-r--r-- | crates/ra_ide_api/src/status.rs | 4 |
7 files changed, 45 insertions, 74 deletions
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 @@ | |||
1 | use std::sync::Arc; | 1 | use std::sync::Arc; |
2 | 2 | ||
3 | use ra_syntax::{SyntaxNode, TreeArc, SourceFile}; | 3 | use ra_syntax::{SyntaxNode, TreeArc, SourceFile}; |
4 | use ra_db::{SourceRootId, LocationIntener, SyntaxDatabase, salsa}; | 4 | use ra_db::{SourceRootId, SyntaxDatabase, salsa}; |
5 | 5 | ||
6 | use crate::{ | 6 | use crate::{ |
7 | DefLoc, DefId, MacroCallLoc, MacroCallId, Name, HirFileId, | 7 | HirInterner, DefId, MacroCallId, Name, HirFileId, |
8 | SourceFileItems, SourceItemId, Crate, | 8 | SourceFileItems, SourceItemId, Crate, |
9 | query_definitions, | 9 | query_definitions, |
10 | FnSignature, FnScopes, | 10 | FnSignature, FnScopes, |
@@ -18,11 +18,7 @@ use crate::{ | |||
18 | }; | 18 | }; |
19 | 19 | ||
20 | #[salsa::query_group] | 20 | #[salsa::query_group] |
21 | pub trait HirDatabase: | 21 | pub trait HirDatabase: SyntaxDatabase + AsRef<HirInterner> { |
22 | SyntaxDatabase | ||
23 | + AsRef<LocationIntener<DefLoc, DefId>> | ||
24 | + AsRef<LocationIntener<MacroCallLoc, MacroCallId>> | ||
25 | { | ||
26 | #[salsa::invoke(HirFileId::hir_source_file)] | 22 | #[salsa::invoke(HirFileId::hir_source_file)] |
27 | fn hir_source_file(&self, file_id: HirFileId) -> TreeArc<SourceFile>; | 23 | fn hir_source_file(&self, file_id: HirFileId) -> TreeArc<SourceFile>; |
28 | 24 | ||
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::{ | |||
8 | module_tree::ModuleId, | 8 | module_tree::ModuleId, |
9 | }; | 9 | }; |
10 | 10 | ||
11 | #[derive(Debug, Default)] | ||
12 | pub struct HirInterner { | ||
13 | defs: LocationIntener<DefLoc, DefId>, | ||
14 | macros: LocationIntener<MacroCallLoc, MacroCallId>, | ||
15 | } | ||
16 | |||
17 | impl HirInterner { | ||
18 | pub fn len(&self) -> usize { | ||
19 | self.defs.len() + self.macros.len() | ||
20 | } | ||
21 | } | ||
22 | |||
11 | /// hir makes heavy use of ids: integer (u32) handlers to various things. You | 23 | /// hir makes heavy use of ids: integer (u32) handlers to various things. You |
12 | /// can think of id as a pointer (but without a lifetime) or a file descriptor | 24 | /// can think of id as a pointer (but without a lifetime) or a file descriptor |
13 | /// (but for hir objects). | 25 | /// (but for hir objects). |
@@ -106,21 +118,15 @@ pub struct MacroCallLoc { | |||
106 | } | 118 | } |
107 | 119 | ||
108 | impl MacroCallId { | 120 | impl MacroCallId { |
109 | pub(crate) fn loc( | 121 | pub(crate) fn loc(self, db: &impl AsRef<HirInterner>) -> MacroCallLoc { |
110 | self, | 122 | db.as_ref().macros.id2loc(self) |
111 | db: &impl AsRef<LocationIntener<MacroCallLoc, MacroCallId>>, | ||
112 | ) -> MacroCallLoc { | ||
113 | db.as_ref().id2loc(self) | ||
114 | } | 123 | } |
115 | } | 124 | } |
116 | 125 | ||
117 | impl MacroCallLoc { | 126 | impl MacroCallLoc { |
118 | #[allow(unused)] | 127 | #[allow(unused)] |
119 | pub(crate) fn id( | 128 | pub(crate) fn id(&self, db: &impl AsRef<HirInterner>) -> MacroCallId { |
120 | &self, | 129 | db.as_ref().macros.loc2id(&self) |
121 | db: &impl AsRef<LocationIntener<MacroCallLoc, MacroCallId>>, | ||
122 | ) -> MacroCallId { | ||
123 | db.as_ref().loc2id(&self) | ||
124 | } | 130 | } |
125 | } | 131 | } |
126 | 132 | ||
@@ -164,8 +170,8 @@ pub(crate) enum DefKind { | |||
164 | } | 170 | } |
165 | 171 | ||
166 | impl DefId { | 172 | impl DefId { |
167 | pub(crate) fn loc(self, db: &impl AsRef<LocationIntener<DefLoc, DefId>>) -> DefLoc { | 173 | pub(crate) fn loc(self, db: &impl AsRef<HirInterner>) -> DefLoc { |
168 | db.as_ref().id2loc(self) | 174 | db.as_ref().defs.id2loc(self) |
169 | } | 175 | } |
170 | 176 | ||
171 | pub fn resolve(self, db: &impl HirDatabase) -> Def { | 177 | pub fn resolve(self, db: &impl HirDatabase) -> Def { |
@@ -233,8 +239,8 @@ impl DefId { | |||
233 | } | 239 | } |
234 | 240 | ||
235 | impl DefLoc { | 241 | impl DefLoc { |
236 | pub(crate) fn id(&self, db: &impl AsRef<LocationIntener<DefLoc, DefId>>) -> DefId { | 242 | pub(crate) fn id(&self, db: &impl AsRef<HirInterner>) -> DefId { |
237 | db.as_ref().loc2id(&self) | 243 | db.as_ref().defs.loc2id(&self) |
238 | } | 244 | } |
239 | } | 245 | } |
240 | 246 | ||
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; | |||
3 | 3 | ||
4 | use ra_arena::{Arena, RawId, impl_arena_id}; | 4 | use ra_arena::{Arena, RawId, impl_arena_id}; |
5 | use ra_syntax::ast::{self, AstNode}; | 5 | use ra_syntax::ast::{self, AstNode}; |
6 | use ra_db::{LocationIntener, SourceRootId}; | 6 | use ra_db::{SourceRootId}; |
7 | 7 | ||
8 | use crate::{ | 8 | use crate::{ |
9 | DefId, DefLoc, DefKind, SourceItemId, SourceFileItems, | 9 | DefId, DefLoc, DefKind, SourceItemId, SourceFileItems, |
10 | Function, | 10 | Function, HirInterner, |
11 | db::HirDatabase, | 11 | db::HirDatabase, |
12 | type_ref::TypeRef, | 12 | type_ref::TypeRef, |
13 | module_tree::ModuleId, | 13 | module_tree::ModuleId, |
@@ -66,7 +66,7 @@ pub struct ImplData { | |||
66 | 66 | ||
67 | impl ImplData { | 67 | impl ImplData { |
68 | pub(crate) fn from_ast( | 68 | pub(crate) fn from_ast( |
69 | db: &impl AsRef<LocationIntener<DefLoc, DefId>>, | 69 | db: &impl AsRef<HirInterner>, |
70 | file_items: &SourceFileItems, | 70 | file_items: &SourceFileItems, |
71 | module: &Module, | 71 | module: &Module, |
72 | node: &ast::ImplBlock, | 72 | 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::{ | |||
40 | pub use self::{ | 40 | pub use self::{ |
41 | path::{Path, PathKind}, | 41 | path::{Path, PathKind}, |
42 | name::Name, | 42 | name::Name, |
43 | ids::{HirFileId, DefId, DefLoc, MacroCallId, MacroCallLoc}, | 43 | ids::{HirFileId, DefId, DefLoc, MacroCallId, MacroCallLoc, HirInterner}, |
44 | macros::{MacroDef, MacroInput, MacroExpansion}, | 44 | macros::{MacroDef, MacroInput, MacroExpansion}, |
45 | nameres::{ItemMap, PerNs, Namespace, Resolution}, | 45 | nameres::{ItemMap, PerNs, Namespace, Resolution}, |
46 | ty::Ty, | 46 | 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}; | |||
2 | 2 | ||
3 | use parking_lot::Mutex; | 3 | use parking_lot::Mutex; |
4 | use ra_db::{ | 4 | use ra_db::{ |
5 | LocationIntener, BaseDatabase, FilePosition, FileId, CrateGraph, SourceRoot, SourceRootId, | 5 | BaseDatabase, FilePosition, FileId, CrateGraph, SourceRoot, SourceRootId, |
6 | salsa::{self, Database}, | 6 | salsa::{self, Database}, |
7 | }; | 7 | }; |
8 | use relative_path::RelativePathBuf; | 8 | use relative_path::RelativePathBuf; |
9 | use test_utils::{parse_fixture, CURSOR_MARKER, extract_offset}; | 9 | use test_utils::{parse_fixture, CURSOR_MARKER, extract_offset}; |
10 | 10 | ||
11 | use crate::{db, DefId, DefLoc, MacroCallId, MacroCallLoc}; | 11 | use crate::{db, HirInterner}; |
12 | 12 | ||
13 | pub const WORKSPACE: SourceRootId = SourceRootId(0); | 13 | pub const WORKSPACE: SourceRootId = SourceRootId(0); |
14 | 14 | ||
@@ -16,7 +16,7 @@ pub const WORKSPACE: SourceRootId = SourceRootId(0); | |||
16 | pub(crate) struct MockDatabase { | 16 | pub(crate) struct MockDatabase { |
17 | events: Mutex<Option<Vec<salsa::Event<MockDatabase>>>>, | 17 | events: Mutex<Option<Vec<salsa::Event<MockDatabase>>>>, |
18 | runtime: salsa::Runtime<MockDatabase>, | 18 | runtime: salsa::Runtime<MockDatabase>, |
19 | id_maps: Arc<IdMaps>, | 19 | interner: Arc<HirInterner>, |
20 | file_counter: u32, | 20 | file_counter: u32, |
21 | } | 21 | } |
22 | 22 | ||
@@ -123,12 +123,6 @@ impl MockDatabase { | |||
123 | } | 123 | } |
124 | } | 124 | } |
125 | 125 | ||
126 | #[derive(Debug, Default)] | ||
127 | struct IdMaps { | ||
128 | defs: LocationIntener<DefLoc, DefId>, | ||
129 | macros: LocationIntener<MacroCallLoc, MacroCallId>, | ||
130 | } | ||
131 | |||
132 | impl salsa::Database for MockDatabase { | 126 | impl salsa::Database for MockDatabase { |
133 | fn salsa_runtime(&self) -> &salsa::Runtime<MockDatabase> { | 127 | fn salsa_runtime(&self) -> &salsa::Runtime<MockDatabase> { |
134 | &self.runtime | 128 | &self.runtime |
@@ -147,7 +141,7 @@ impl Default for MockDatabase { | |||
147 | let mut db = MockDatabase { | 141 | let mut db = MockDatabase { |
148 | events: Default::default(), | 142 | events: Default::default(), |
149 | runtime: salsa::Runtime::default(), | 143 | runtime: salsa::Runtime::default(), |
150 | id_maps: Default::default(), | 144 | interner: Default::default(), |
151 | file_counter: 0, | 145 | file_counter: 0, |
152 | }; | 146 | }; |
153 | db.query_mut(ra_db::CrateGraphQuery) | 147 | db.query_mut(ra_db::CrateGraphQuery) |
@@ -165,7 +159,7 @@ impl salsa::ParallelDatabase for MockDatabase { | |||
165 | salsa::Snapshot::new(MockDatabase { | 159 | salsa::Snapshot::new(MockDatabase { |
166 | events: Default::default(), | 160 | events: Default::default(), |
167 | runtime: self.runtime.snapshot(self), | 161 | runtime: self.runtime.snapshot(self), |
168 | id_maps: self.id_maps.clone(), | 162 | interner: Arc::clone(&self.interner), |
169 | file_counter: self.file_counter, | 163 | file_counter: self.file_counter, |
170 | }) | 164 | }) |
171 | } | 165 | } |
@@ -173,14 +167,9 @@ impl salsa::ParallelDatabase for MockDatabase { | |||
173 | 167 | ||
174 | impl BaseDatabase for MockDatabase {} | 168 | impl BaseDatabase for MockDatabase {} |
175 | 169 | ||
176 | impl AsRef<LocationIntener<DefLoc, DefId>> for MockDatabase { | 170 | impl AsRef<HirInterner> for MockDatabase { |
177 | fn as_ref(&self) -> &LocationIntener<DefLoc, DefId> { | 171 | fn as_ref(&self) -> &HirInterner { |
178 | &self.id_maps.defs | 172 | &self.interner |
179 | } | ||
180 | } | ||
181 | impl AsRef<LocationIntener<MacroCallLoc, MacroCallId>> for MockDatabase { | ||
182 | fn as_ref(&self) -> &LocationIntener<MacroCallLoc, MacroCallId> { | ||
183 | &self.id_maps.macros | ||
184 | } | 173 | } |
185 | } | 174 | } |
186 | 175 | ||
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 @@ | |||
1 | use std::{fmt, sync::Arc}; | 1 | use std::sync::Arc; |
2 | 2 | ||
3 | use ra_db::{ | 3 | use ra_db::{ |
4 | LocationIntener, BaseDatabase, FileId, Canceled, | 4 | BaseDatabase, FileId, Canceled, |
5 | salsa::{self, Database}, | 5 | salsa::{self, Database}, |
6 | }; | 6 | }; |
7 | 7 | ||
@@ -10,21 +10,7 @@ use crate::{symbol_index, LineIndex}; | |||
10 | #[derive(Debug)] | 10 | #[derive(Debug)] |
11 | pub(crate) struct RootDatabase { | 11 | pub(crate) struct RootDatabase { |
12 | runtime: salsa::Runtime<RootDatabase>, | 12 | runtime: salsa::Runtime<RootDatabase>, |
13 | id_maps: Arc<IdMaps>, | 13 | interner: Arc<hir::HirInterner>, |
14 | } | ||
15 | |||
16 | #[derive(Default)] | ||
17 | struct IdMaps { | ||
18 | defs: LocationIntener<hir::DefLoc, hir::DefId>, | ||
19 | macros: LocationIntener<hir::MacroCallLoc, hir::MacroCallId>, | ||
20 | } | ||
21 | |||
22 | impl fmt::Debug for IdMaps { | ||
23 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
24 | f.debug_struct("IdMaps") | ||
25 | .field("n_defs", &self.defs.len()) | ||
26 | .finish() | ||
27 | } | ||
28 | } | 14 | } |
29 | 15 | ||
30 | impl salsa::Database for RootDatabase { | 16 | impl salsa::Database for RootDatabase { |
@@ -40,7 +26,7 @@ impl Default for RootDatabase { | |||
40 | fn default() -> RootDatabase { | 26 | fn default() -> RootDatabase { |
41 | let mut db = RootDatabase { | 27 | let mut db = RootDatabase { |
42 | runtime: salsa::Runtime::default(), | 28 | runtime: salsa::Runtime::default(), |
43 | id_maps: Default::default(), | 29 | interner: Default::default(), |
44 | }; | 30 | }; |
45 | db.query_mut(ra_db::CrateGraphQuery) | 31 | db.query_mut(ra_db::CrateGraphQuery) |
46 | .set((), Default::default()); | 32 | .set((), Default::default()); |
@@ -56,22 +42,16 @@ impl salsa::ParallelDatabase for RootDatabase { | |||
56 | fn snapshot(&self) -> salsa::Snapshot<RootDatabase> { | 42 | fn snapshot(&self) -> salsa::Snapshot<RootDatabase> { |
57 | salsa::Snapshot::new(RootDatabase { | 43 | salsa::Snapshot::new(RootDatabase { |
58 | runtime: self.runtime.snapshot(self), | 44 | runtime: self.runtime.snapshot(self), |
59 | id_maps: self.id_maps.clone(), | 45 | interner: Arc::clone(&self.interner), |
60 | }) | 46 | }) |
61 | } | 47 | } |
62 | } | 48 | } |
63 | 49 | ||
64 | impl BaseDatabase for RootDatabase {} | 50 | impl BaseDatabase for RootDatabase {} |
65 | 51 | ||
66 | impl AsRef<LocationIntener<hir::DefLoc, hir::DefId>> for RootDatabase { | 52 | impl AsRef<hir::HirInterner> for RootDatabase { |
67 | fn as_ref(&self) -> &LocationIntener<hir::DefLoc, hir::DefId> { | 53 | fn as_ref(&self) -> &hir::HirInterner { |
68 | &self.id_maps.defs | 54 | &self.interner |
69 | } | ||
70 | } | ||
71 | |||
72 | impl AsRef<LocationIntener<hir::MacroCallLoc, hir::MacroCallId>> for RootDatabase { | ||
73 | fn as_ref(&self) -> &LocationIntener<hir::MacroCallLoc, hir::MacroCallId> { | ||
74 | &self.id_maps.macros | ||
75 | } | 55 | } |
76 | } | 56 | } |
77 | 57 | ||
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 @@ | |||
1 | use ra_db::{ | 1 | use ra_db::{ |
2 | LocationIntener, SourceFileQuery, | 2 | SourceFileQuery, |
3 | salsa::{Database, debug::DebugQueryTable}, | 3 | salsa::{Database, debug::DebugQueryTable}, |
4 | }; | 4 | }; |
5 | 5 | ||
@@ -8,7 +8,7 @@ use crate::db::RootDatabase; | |||
8 | pub(crate) fn status(db: &RootDatabase) -> String { | 8 | pub(crate) fn status(db: &RootDatabase) -> String { |
9 | let n_parsed_files = db.query(SourceFileQuery).keys::<Vec<_>>().len(); | 9 | let n_parsed_files = db.query(SourceFileQuery).keys::<Vec<_>>().len(); |
10 | let n_defs = { | 10 | let n_defs = { |
11 | let interner: &LocationIntener<hir::DefLoc, hir::DefId> = db.as_ref(); | 11 | let interner: &hir::HirInterner = db.as_ref(); |
12 | interner.len() | 12 | interner.len() |
13 | }; | 13 | }; |
14 | format!("#n_parsed_files {}\n#n_defs {}\n", n_parsed_files, n_defs) | 14 | format!("#n_parsed_files {}\n#n_defs {}\n", n_parsed_files, n_defs) |