aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2019-10-30 10:10:38 +0000
committerAleksey Kladov <[email protected]>2019-10-30 10:11:38 +0000
commitc9cd6aa370667783292de3bc580e0503a409e453 (patch)
tree4662b1c76bed5e830c1dae9ee5dbf47520f41e1e
parenta136cc0653d2b4133fb6387009cfdbaf3e2cf275 (diff)
Move ids to hir_def crate
-rw-r--r--Cargo.lock1
-rw-r--r--crates/ra_hir/src/code_model.rs16
-rw-r--r--crates/ra_hir/src/code_model/src.rs20
-rw-r--r--crates/ra_hir/src/db.rs28
-rw-r--r--crates/ra_hir/src/from_source.rs2
-rw-r--r--crates/ra_hir/src/ids.rs167
-rw-r--r--crates/ra_hir/src/impl_block.rs2
-rw-r--r--crates/ra_hir/src/nameres/collector.rs2
-rw-r--r--crates/ra_hir/src/source_binder.rs2
-rw-r--r--crates/ra_hir/src/traits.rs2
-rw-r--r--crates/ra_hir/src/ty/traits/chalk.rs34
-rw-r--r--crates/ra_hir_def/Cargo.toml1
-rw-r--r--crates/ra_hir_def/src/db.rs22
-rw-r--r--crates/ra_hir_def/src/lib.rs204
-rw-r--r--crates/ra_ide_api/src/completion/complete_path.rs2
-rw-r--r--crates/ra_ide_api/src/impls.rs4
-rw-r--r--crates/ra_ide_api/src/parent_module.rs5
-rw-r--r--crates/ra_ide_api/src/references/search_scope.rs2
18 files changed, 258 insertions, 258 deletions
diff --git a/Cargo.lock b/Cargo.lock
index a76433620..66ff1285a 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -1021,6 +1021,7 @@ dependencies = [
1021 "log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)", 1021 "log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)",
1022 "ra_arena 0.1.0", 1022 "ra_arena 0.1.0",
1023 "ra_db 0.1.0", 1023 "ra_db 0.1.0",
1024 "ra_hir_expand 0.1.0",
1024 "ra_prof 0.1.0", 1025 "ra_prof 0.1.0",
1025 "ra_syntax 0.1.0", 1026 "ra_syntax 0.1.0",
1026] 1027]
diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs
index 3f1c36941..1a790b2f3 100644
--- a/crates/ra_hir/src/code_model.rs
+++ b/crates/ra_hir/src/code_model.rs
@@ -343,7 +343,7 @@ pub struct Struct {
343 343
344impl Struct { 344impl Struct {
345 pub fn module(self, db: &impl DefDatabase) -> Module { 345 pub fn module(self, db: &impl DefDatabase) -> Module {
346 self.id.module(db) 346 Module { id: self.id.module(db) }
347 } 347 }
348 348
349 pub fn krate(self, db: &impl DefDatabase) -> Option<Crate> { 349 pub fn krate(self, db: &impl DefDatabase) -> Option<Crate> {
@@ -405,7 +405,7 @@ impl Union {
405 } 405 }
406 406
407 pub fn module(self, db: &impl HirDatabase) -> Module { 407 pub fn module(self, db: &impl HirDatabase) -> Module {
408 self.id.module(db) 408 Module { id: self.id.module(db) }
409 } 409 }
410 410
411 pub fn ty(self, db: &impl HirDatabase) -> Ty { 411 pub fn ty(self, db: &impl HirDatabase) -> Ty {
@@ -431,7 +431,7 @@ pub struct Enum {
431 431
432impl Enum { 432impl Enum {
433 pub fn module(self, db: &impl DefDatabase) -> Module { 433 pub fn module(self, db: &impl DefDatabase) -> Module {
434 self.id.module(db) 434 Module { id: self.id.module(db) }
435 } 435 }
436 436
437 pub fn krate(self, db: &impl DefDatabase) -> Option<Crate> { 437 pub fn krate(self, db: &impl DefDatabase) -> Option<Crate> {
@@ -697,7 +697,7 @@ impl FnData {
697 697
698impl Function { 698impl Function {
699 pub fn module(self, db: &impl DefDatabase) -> Module { 699 pub fn module(self, db: &impl DefDatabase) -> Module {
700 self.id.module(db) 700 Module { id: self.id.module(db) }
701 } 701 }
702 702
703 pub fn krate(self, db: &impl DefDatabase) -> Option<Crate> { 703 pub fn krate(self, db: &impl DefDatabase) -> Option<Crate> {
@@ -775,7 +775,7 @@ pub struct Const {
775 775
776impl Const { 776impl Const {
777 pub fn module(self, db: &impl DefDatabase) -> Module { 777 pub fn module(self, db: &impl DefDatabase) -> Module {
778 self.id.module(db) 778 Module { id: self.id.module(db) }
779 } 779 }
780 780
781 pub fn krate(self, db: &impl DefDatabase) -> Option<Crate> { 781 pub fn krate(self, db: &impl DefDatabase) -> Option<Crate> {
@@ -872,7 +872,7 @@ pub struct Static {
872 872
873impl Static { 873impl Static {
874 pub fn module(self, db: &impl DefDatabase) -> Module { 874 pub fn module(self, db: &impl DefDatabase) -> Module {
875 self.id.module(db) 875 Module { id: self.id.module(db) }
876 } 876 }
877 877
878 pub fn krate(self, db: &impl DefDatabase) -> Option<Crate> { 878 pub fn krate(self, db: &impl DefDatabase) -> Option<Crate> {
@@ -901,7 +901,7 @@ pub struct Trait {
901 901
902impl Trait { 902impl Trait {
903 pub fn module(self, db: &impl DefDatabase) -> Module { 903 pub fn module(self, db: &impl DefDatabase) -> Module {
904 self.id.module(db) 904 Module { id: self.id.module(db) }
905 } 905 }
906 906
907 pub fn name(self, db: &impl DefDatabase) -> Option<Name> { 907 pub fn name(self, db: &impl DefDatabase) -> Option<Name> {
@@ -1003,7 +1003,7 @@ pub struct TypeAlias {
1003 1003
1004impl TypeAlias { 1004impl TypeAlias {
1005 pub fn module(self, db: &impl DefDatabase) -> Module { 1005 pub fn module(self, db: &impl DefDatabase) -> Module {
1006 self.id.module(db) 1006 Module { id: self.id.module(db) }
1007 } 1007 }
1008 1008
1009 pub fn krate(self, db: &impl DefDatabase) -> Option<Crate> { 1009 pub fn krate(self, db: &impl DefDatabase) -> Option<Crate> {
diff --git a/crates/ra_hir/src/code_model/src.rs b/crates/ra_hir/src/code_model/src.rs
index 8b33f25f7..5c7f61eef 100644
--- a/crates/ra_hir/src/code_model/src.rs
+++ b/crates/ra_hir/src/code_model/src.rs
@@ -1,9 +1,6 @@
1//! FIXME: write short doc here 1//! FIXME: write short doc here
2 2
3use ra_syntax::{ 3use ra_syntax::ast::{self, AstNode};
4 ast::{self, AstNode},
5 SyntaxNode,
6};
7 4
8use crate::{ 5use crate::{
9 db::{AstDatabase, DefDatabase, HirDatabase}, 6 db::{AstDatabase, DefDatabase, HirDatabase},
@@ -12,26 +9,13 @@ use crate::{
12 ModuleSource, Static, Struct, StructField, Trait, TypeAlias, Union, 9 ModuleSource, Static, Struct, StructField, Trait, TypeAlias, Union,
13}; 10};
14 11
15#[derive(Debug, PartialEq, Eq, Clone, Copy)] 12pub use hir_def::Source;
16pub struct Source<T> {
17 pub file_id: HirFileId,
18 pub ast: T,
19}
20 13
21pub trait HasSource { 14pub trait HasSource {
22 type Ast; 15 type Ast;
23 fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<Self::Ast>; 16 fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<Self::Ast>;
24} 17}
25 18
26impl<T> Source<T> {
27 pub(crate) fn map<F: FnOnce(T) -> U, U>(self, f: F) -> Source<U> {
28 Source { file_id: self.file_id, ast: f(self.ast) }
29 }
30 pub(crate) fn file_syntax(&self, db: &impl AstDatabase) -> SyntaxNode {
31 db.parse_or_expand(self.file_id).expect("source created from invalid file")
32 }
33}
34
35/// NB: Module is !HasSource, because it has two source nodes at the same time: 19/// NB: Module is !HasSource, because it has two source nodes at the same time:
36/// definition and declaration. 20/// definition and declaration.
37impl Module { 21impl Module {
diff --git a/crates/ra_hir/src/db.rs b/crates/ra_hir/src/db.rs
index da8ae6ef4..8f6cb2da7 100644
--- a/crates/ra_hir/src/db.rs
+++ b/crates/ra_hir/src/db.rs
@@ -2,8 +2,8 @@
2 2
3use std::sync::Arc; 3use std::sync::Arc;
4 4
5use ra_db::{salsa, SourceDatabase}; 5use ra_db::salsa;
6use ra_syntax::{ast, SmolStr}; 6use ra_syntax::SmolStr;
7 7
8use crate::{ 8use crate::{
9 adt::{EnumData, StructData}, 9 adt::{EnumData, StructData},
@@ -23,34 +23,12 @@ use crate::{
23 Static, Struct, StructField, Trait, TypeAlias, 23 Static, Struct, StructField, Trait, TypeAlias,
24}; 24};
25 25
26pub use hir_def::db::{InternDatabase, InternDatabaseStorage};
26pub use hir_expand::db::{ 27pub use hir_expand::db::{
27 AstDatabase, AstDatabaseStorage, AstIdMapQuery, MacroArgQuery, MacroDefQuery, MacroExpandQuery, 28 AstDatabase, AstDatabaseStorage, AstIdMapQuery, MacroArgQuery, MacroDefQuery, MacroExpandQuery,
28 ParseMacroQuery, 29 ParseMacroQuery,
29}; 30};
30 31
31/// We store all interned things in the single QueryGroup.
32///
33/// This is done mainly to allow both "volatile" `AstDatabase` and "stable"
34/// `DefDatabase` to access macros, without adding hard dependencies between the
35/// two.
36#[salsa::query_group(InternDatabaseStorage)]
37pub trait InternDatabase: SourceDatabase {
38 #[salsa::interned]
39 fn intern_function(&self, loc: ids::ItemLoc<ast::FnDef>) -> ids::FunctionId;
40 #[salsa::interned]
41 fn intern_struct(&self, loc: ids::ItemLoc<ast::StructDef>) -> ids::StructId;
42 #[salsa::interned]
43 fn intern_enum(&self, loc: ids::ItemLoc<ast::EnumDef>) -> ids::EnumId;
44 #[salsa::interned]
45 fn intern_const(&self, loc: ids::ItemLoc<ast::ConstDef>) -> ids::ConstId;
46 #[salsa::interned]
47 fn intern_static(&self, loc: ids::ItemLoc<ast::StaticDef>) -> ids::StaticId;
48 #[salsa::interned]
49 fn intern_trait(&self, loc: ids::ItemLoc<ast::TraitDef>) -> ids::TraitId;
50 #[salsa::interned]
51 fn intern_type_alias(&self, loc: ids::ItemLoc<ast::TypeAliasDef>) -> ids::TypeAliasId;
52}
53
54// This database uses `AstDatabase` internally, 32// This database uses `AstDatabase` internally,
55#[salsa::query_group(DefDatabaseStorage)] 33#[salsa::query_group(DefDatabaseStorage)]
56#[salsa::requires(AstDatabase)] 34#[salsa::requires(AstDatabase)]
diff --git a/crates/ra_hir/src/from_source.rs b/crates/ra_hir/src/from_source.rs
index 291f2a14a..93713bb14 100644
--- a/crates/ra_hir/src/from_source.rs
+++ b/crates/ra_hir/src/from_source.rs
@@ -208,6 +208,6 @@ where
208 let module_src = 208 let module_src =
209 crate::ModuleSource::from_child_node(db, src.file_id.original_file(db), &src.ast.syntax()); 209 crate::ModuleSource::from_child_node(db, src.file_id.original_file(db), &src.ast.syntax());
210 let module = Module::from_definition(db, Source { file_id: src.file_id, ast: module_src })?; 210 let module = Module::from_definition(db, Source { file_id: src.file_id, ast: module_src })?;
211 let ctx = LocationCtx::new(db, module, src.file_id); 211 let ctx = LocationCtx::new(db, module.id, src.file_id);
212 Some(DEF::from_ast(ctx, &src.ast)) 212 Some(DEF::from_ast(ctx, &src.ast))
213} 213}
diff --git a/crates/ra_hir/src/ids.rs b/crates/ra_hir/src/ids.rs
index dea288eb7..fe083c0c6 100644
--- a/crates/ra_hir/src/ids.rs
+++ b/crates/ra_hir/src/ids.rs
@@ -5,16 +5,12 @@
5//! This module defines a bunch of ids we are using. The most important ones are 5//! This module defines a bunch of ids we are using. The most important ones are
6//! probably `HirFileId` and `DefId`. 6//! probably `HirFileId` and `DefId`.
7 7
8use std::hash::{Hash, Hasher};
9
10use ra_db::salsa; 8use ra_db::salsa;
11use ra_syntax::{ast, AstNode};
12 9
13use crate::{ 10pub use hir_def::{
14 db::{AstDatabase, InternDatabase}, 11 AstItemDef, ConstId, EnumId, FunctionId, ItemLoc, LocationCtx, StaticId, StructId, TraitId,
15 AstId, FileAstId, Module, Source, 12 TypeAliasId,
16}; 13};
17
18pub use hir_expand::{HirFileId, MacroCallId, MacroCallLoc, MacroDefId, MacroFile, MacroFileKind}; 14pub use hir_expand::{HirFileId, MacroCallId, MacroCallLoc, MacroDefId, MacroFile, MacroFileKind};
19 15
20macro_rules! impl_intern_key { 16macro_rules! impl_intern_key {
@@ -30,163 +26,6 @@ macro_rules! impl_intern_key {
30 }; 26 };
31} 27}
32 28
33#[derive(Debug)]
34pub struct ItemLoc<N: AstNode> {
35 pub(crate) module: Module,
36 ast_id: AstId<N>,
37}
38
39impl<N: AstNode> PartialEq for ItemLoc<N> {
40 fn eq(&self, other: &Self) -> bool {
41 self.module == other.module && self.ast_id == other.ast_id
42 }
43}
44impl<N: AstNode> Eq for ItemLoc<N> {}
45impl<N: AstNode> Hash for ItemLoc<N> {
46 fn hash<H: Hasher>(&self, hasher: &mut H) {
47 self.module.hash(hasher);
48 self.ast_id.hash(hasher);
49 }
50}
51
52impl<N: AstNode> Clone for ItemLoc<N> {
53 fn clone(&self) -> ItemLoc<N> {
54 ItemLoc { module: self.module, ast_id: self.ast_id }
55 }
56}
57
58#[derive(Clone, Copy)]
59pub(crate) struct LocationCtx<DB> {
60 db: DB,
61 module: Module,
62 file_id: HirFileId,
63}
64
65impl<'a, DB> LocationCtx<&'a DB> {
66 pub(crate) fn new(db: &'a DB, module: Module, file_id: HirFileId) -> LocationCtx<&'a DB> {
67 LocationCtx { db, module, file_id }
68 }
69}
70
71impl<'a, DB: AstDatabase + InternDatabase> LocationCtx<&'a DB> {
72 pub(crate) fn to_def<N, DEF>(self, ast: &N) -> DEF
73 where
74 N: AstNode,
75 DEF: AstItemDef<N>,
76 {
77 DEF::from_ast(self, ast)
78 }
79}
80
81pub(crate) trait AstItemDef<N: AstNode>: salsa::InternKey + Clone {
82 fn intern(db: &impl InternDatabase, loc: ItemLoc<N>) -> Self;
83 fn lookup_intern(self, db: &impl InternDatabase) -> ItemLoc<N>;
84
85 fn from_ast(ctx: LocationCtx<&(impl AstDatabase + InternDatabase)>, ast: &N) -> Self {
86 let items = ctx.db.ast_id_map(ctx.file_id);
87 let item_id = items.ast_id(ast);
88 Self::from_ast_id(ctx, item_id)
89 }
90 fn from_ast_id(ctx: LocationCtx<&impl InternDatabase>, ast_id: FileAstId<N>) -> Self {
91 let loc = ItemLoc { module: ctx.module, ast_id: AstId::new(ctx.file_id, ast_id) };
92 Self::intern(ctx.db, loc)
93 }
94 fn source(self, db: &(impl AstDatabase + InternDatabase)) -> Source<N> {
95 let loc = self.lookup_intern(db);
96 let ast = loc.ast_id.to_node(db);
97 Source { file_id: loc.ast_id.file_id(), ast }
98 }
99 fn module(self, db: &impl InternDatabase) -> Module {
100 let loc = self.lookup_intern(db);
101 loc.module
102 }
103}
104
105#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
106pub struct FunctionId(salsa::InternId);
107impl_intern_key!(FunctionId);
108
109impl AstItemDef<ast::FnDef> for FunctionId {
110 fn intern(db: &impl InternDatabase, loc: ItemLoc<ast::FnDef>) -> Self {
111 db.intern_function(loc)
112 }
113 fn lookup_intern(self, db: &impl InternDatabase) -> ItemLoc<ast::FnDef> {
114 db.lookup_intern_function(self)
115 }
116}
117
118#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
119pub struct StructId(salsa::InternId);
120impl_intern_key!(StructId);
121impl AstItemDef<ast::StructDef> for StructId {
122 fn intern(db: &impl InternDatabase, loc: ItemLoc<ast::StructDef>) -> Self {
123 db.intern_struct(loc)
124 }
125 fn lookup_intern(self, db: &impl InternDatabase) -> ItemLoc<ast::StructDef> {
126 db.lookup_intern_struct(self)
127 }
128}
129
130#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
131pub struct EnumId(salsa::InternId);
132impl_intern_key!(EnumId);
133impl AstItemDef<ast::EnumDef> for EnumId {
134 fn intern(db: &impl InternDatabase, loc: ItemLoc<ast::EnumDef>) -> Self {
135 db.intern_enum(loc)
136 }
137 fn lookup_intern(self, db: &impl InternDatabase) -> ItemLoc<ast::EnumDef> {
138 db.lookup_intern_enum(self)
139 }
140}
141
142#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
143pub struct ConstId(salsa::InternId);
144impl_intern_key!(ConstId);
145impl AstItemDef<ast::ConstDef> for ConstId {
146 fn intern(db: &impl InternDatabase, loc: ItemLoc<ast::ConstDef>) -> Self {
147 db.intern_const(loc)
148 }
149 fn lookup_intern(self, db: &impl InternDatabase) -> ItemLoc<ast::ConstDef> {
150 db.lookup_intern_const(self)
151 }
152}
153
154#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
155pub struct StaticId(salsa::InternId);
156impl_intern_key!(StaticId);
157impl AstItemDef<ast::StaticDef> for StaticId {
158 fn intern(db: &impl InternDatabase, loc: ItemLoc<ast::StaticDef>) -> Self {
159 db.intern_static(loc)
160 }
161 fn lookup_intern(self, db: &impl InternDatabase) -> ItemLoc<ast::StaticDef> {
162 db.lookup_intern_static(self)
163 }
164}
165
166#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
167pub struct TraitId(salsa::InternId);
168impl_intern_key!(TraitId);
169impl AstItemDef<ast::TraitDef> for TraitId {
170 fn intern(db: &impl InternDatabase, loc: ItemLoc<ast::TraitDef>) -> Self {
171 db.intern_trait(loc)
172 }
173 fn lookup_intern(self, db: &impl InternDatabase) -> ItemLoc<ast::TraitDef> {
174 db.lookup_intern_trait(self)
175 }
176}
177
178#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
179pub struct TypeAliasId(salsa::InternId);
180impl_intern_key!(TypeAliasId);
181impl AstItemDef<ast::TypeAliasDef> for TypeAliasId {
182 fn intern(db: &impl InternDatabase, loc: ItemLoc<ast::TypeAliasDef>) -> Self {
183 db.intern_type_alias(loc)
184 }
185 fn lookup_intern(self, db: &impl InternDatabase) -> ItemLoc<ast::TypeAliasDef> {
186 db.lookup_intern_type_alias(self)
187 }
188}
189
190/// This exists just for Chalk, because Chalk just has a single `StructId` where 29/// This exists just for Chalk, because Chalk just has a single `StructId` where
191/// we have different kinds of ADTs, primitive types and special type 30/// we have different kinds of ADTs, primitive types and special type
192/// constructors like tuples and function pointers. 31/// constructors like tuples and function pointers.
diff --git a/crates/ra_hir/src/impl_block.rs b/crates/ra_hir/src/impl_block.rs
index fde47c264..06f21fc33 100644
--- a/crates/ra_hir/src/impl_block.rs
+++ b/crates/ra_hir/src/impl_block.rs
@@ -129,7 +129,7 @@ impl ImplData {
129 ) -> Self { 129 ) -> Self {
130 let target_trait = node.target_trait().map(TypeRef::from_ast); 130 let target_trait = node.target_trait().map(TypeRef::from_ast);
131 let target_type = TypeRef::from_ast_opt(node.target_type()); 131 let target_type = TypeRef::from_ast_opt(node.target_type());
132 let ctx = LocationCtx::new(db, module, file_id); 132 let ctx = LocationCtx::new(db, module.id, file_id);
133 let negative = node.is_negative(); 133 let negative = node.is_negative();
134 let items = if let Some(item_list) = node.item_list() { 134 let items = if let Some(item_list) = node.item_list() {
135 item_list 135 item_list
diff --git a/crates/ra_hir/src/nameres/collector.rs b/crates/ra_hir/src/nameres/collector.rs
index dd5f9d4ba..a94a0554c 100644
--- a/crates/ra_hir/src/nameres/collector.rs
+++ b/crates/ra_hir/src/nameres/collector.rs
@@ -641,7 +641,7 @@ where
641 641
642 fn define_def(&mut self, def: &raw::DefData) { 642 fn define_def(&mut self, def: &raw::DefData) {
643 let module = Module::new(self.def_collector.def_map.krate, self.module_id); 643 let module = Module::new(self.def_collector.def_map.krate, self.module_id);
644 let ctx = LocationCtx::new(self.def_collector.db, module, self.file_id); 644 let ctx = LocationCtx::new(self.def_collector.db, module.id, self.file_id);
645 645
646 macro_rules! def { 646 macro_rules! def {
647 ($kind:ident, $ast_id:ident) => { 647 ($kind:ident, $ast_id:ident) => {
diff --git a/crates/ra_hir/src/source_binder.rs b/crates/ra_hir/src/source_binder.rs
index a907d6a9f..730c33226 100644
--- a/crates/ra_hir/src/source_binder.rs
+++ b/crates/ra_hir/src/source_binder.rs
@@ -64,7 +64,7 @@ fn def_with_body_from_child_node(
64) -> Option<DefWithBody> { 64) -> Option<DefWithBody> {
65 let src = crate::ModuleSource::from_child_node(db, file_id, node); 65 let src = crate::ModuleSource::from_child_node(db, file_id, node);
66 let module = Module::from_definition(db, crate::Source { file_id: file_id.into(), ast: src })?; 66 let module = Module::from_definition(db, crate::Source { file_id: file_id.into(), ast: src })?;
67 let ctx = LocationCtx::new(db, module, file_id.into()); 67 let ctx = LocationCtx::new(db, module.id, file_id.into());
68 68
69 node.ancestors().find_map(|node| { 69 node.ancestors().find_map(|node| {
70 if let Some(def) = ast::FnDef::cast(node.clone()) { 70 if let Some(def) = ast::FnDef::cast(node.clone()) {
diff --git a/crates/ra_hir/src/traits.rs b/crates/ra_hir/src/traits.rs
index e39511518..22f188049 100644
--- a/crates/ra_hir/src/traits.rs
+++ b/crates/ra_hir/src/traits.rs
@@ -27,7 +27,7 @@ impl TraitData {
27 let src = tr.source(db); 27 let src = tr.source(db);
28 let name = src.ast.name().map(|n| n.as_name()); 28 let name = src.ast.name().map(|n| n.as_name());
29 let module = tr.module(db); 29 let module = tr.module(db);
30 let ctx = LocationCtx::new(db, module, src.file_id); 30 let ctx = LocationCtx::new(db, module.id, src.file_id);
31 let auto = src.ast.is_auto(); 31 let auto = src.ast.is_auto();
32 let items = if let Some(item_list) = src.ast.item_list() { 32 let items = if let Some(item_list) = src.ast.item_list() {
33 item_list 33 item_list
diff --git a/crates/ra_hir/src/ty/traits/chalk.rs b/crates/ra_hir/src/ty/traits/chalk.rs
index aec484feb..ab66515be 100644
--- a/crates/ra_hir/src/ty/traits/chalk.rs
+++ b/crates/ra_hir/src/ty/traits/chalk.rs
@@ -162,11 +162,11 @@ impl ToChalk for Trait {
162 type Chalk = chalk_ir::TraitId; 162 type Chalk = chalk_ir::TraitId;
163 163
164 fn to_chalk(self, _db: &impl HirDatabase) -> chalk_ir::TraitId { 164 fn to_chalk(self, _db: &impl HirDatabase) -> chalk_ir::TraitId {
165 self.id.into() 165 chalk_ir::TraitId(id_to_chalk(self.id))
166 } 166 }
167 167
168 fn from_chalk(_db: &impl HirDatabase, trait_id: chalk_ir::TraitId) -> Trait { 168 fn from_chalk(_db: &impl HirDatabase, trait_id: chalk_ir::TraitId) -> Trait {
169 Trait { id: trait_id.into() } 169 Trait { id: id_from_chalk(trait_id.0) }
170 } 170 }
171} 171}
172 172
@@ -198,11 +198,11 @@ impl ToChalk for TypeAlias {
198 type Chalk = chalk_ir::TypeId; 198 type Chalk = chalk_ir::TypeId;
199 199
200 fn to_chalk(self, _db: &impl HirDatabase) -> chalk_ir::TypeId { 200 fn to_chalk(self, _db: &impl HirDatabase) -> chalk_ir::TypeId {
201 self.id.into() 201 chalk_ir::TypeId(id_to_chalk(self.id))
202 } 202 }
203 203
204 fn from_chalk(_db: &impl HirDatabase, impl_id: chalk_ir::TypeId) -> TypeAlias { 204 fn from_chalk(_db: &impl HirDatabase, type_alias_id: chalk_ir::TypeId) -> TypeAlias {
205 TypeAlias { id: impl_id.into() } 205 TypeAlias { id: id_from_chalk(type_alias_id.0) }
206 } 206 }
207} 207}
208 208
@@ -775,30 +775,6 @@ fn id_to_chalk<T: InternKey>(salsa_id: T) -> chalk_ir::RawId {
775 chalk_ir::RawId { index: salsa_id.as_intern_id().as_u32() } 775 chalk_ir::RawId { index: salsa_id.as_intern_id().as_u32() }
776} 776}
777 777
778impl From<chalk_ir::TraitId> for crate::ids::TraitId {
779 fn from(trait_id: chalk_ir::TraitId) -> Self {
780 id_from_chalk(trait_id.0)
781 }
782}
783
784impl From<crate::ids::TraitId> for chalk_ir::TraitId {
785 fn from(trait_id: crate::ids::TraitId) -> Self {
786 chalk_ir::TraitId(id_to_chalk(trait_id))
787 }
788}
789
790impl From<chalk_ir::TypeId> for crate::ids::TypeAliasId {
791 fn from(type_id: chalk_ir::TypeId) -> Self {
792 id_from_chalk(type_id.0)
793 }
794}
795
796impl From<crate::ids::TypeAliasId> for chalk_ir::TypeId {
797 fn from(type_id: crate::ids::TypeAliasId) -> Self {
798 chalk_ir::TypeId(id_to_chalk(type_id))
799 }
800}
801
802impl From<chalk_ir::StructId> for crate::ids::TypeCtorId { 778impl From<chalk_ir::StructId> for crate::ids::TypeCtorId {
803 fn from(struct_id: chalk_ir::StructId) -> Self { 779 fn from(struct_id: chalk_ir::StructId) -> Self {
804 id_from_chalk(struct_id.0) 780 id_from_chalk(struct_id.0)
diff --git a/crates/ra_hir_def/Cargo.toml b/crates/ra_hir_def/Cargo.toml
index f22a678eb..75e93f254 100644
--- a/crates/ra_hir_def/Cargo.toml
+++ b/crates/ra_hir_def/Cargo.toml
@@ -11,3 +11,4 @@ ra_arena = { path = "../ra_arena" }
11ra_db = { path = "../ra_db" } 11ra_db = { path = "../ra_db" }
12ra_syntax = { path = "../ra_syntax" } 12ra_syntax = { path = "../ra_syntax" }
13ra_prof = { path = "../ra_prof" } 13ra_prof = { path = "../ra_prof" }
14hir_expand = { path = "../ra_hir_expand", package = "ra_hir_expand" }
diff --git a/crates/ra_hir_def/src/db.rs b/crates/ra_hir_def/src/db.rs
new file mode 100644
index 000000000..f6f976c86
--- /dev/null
+++ b/crates/ra_hir_def/src/db.rs
@@ -0,0 +1,22 @@
1//! Defines database & queries for name resolution.
2
3use ra_db::{salsa, SourceDatabase};
4use ra_syntax::ast;
5
6#[salsa::query_group(InternDatabaseStorage)]
7pub trait InternDatabase: SourceDatabase {
8 #[salsa::interned]
9 fn intern_function(&self, loc: crate::ItemLoc<ast::FnDef>) -> crate::FunctionId;
10 #[salsa::interned]
11 fn intern_struct(&self, loc: crate::ItemLoc<ast::StructDef>) -> crate::StructId;
12 #[salsa::interned]
13 fn intern_enum(&self, loc: crate::ItemLoc<ast::EnumDef>) -> crate::EnumId;
14 #[salsa::interned]
15 fn intern_const(&self, loc: crate::ItemLoc<ast::ConstDef>) -> crate::ConstId;
16 #[salsa::interned]
17 fn intern_static(&self, loc: crate::ItemLoc<ast::StaticDef>) -> crate::StaticId;
18 #[salsa::interned]
19 fn intern_trait(&self, loc: crate::ItemLoc<ast::TraitDef>) -> crate::TraitId;
20 #[salsa::interned]
21 fn intern_type_alias(&self, loc: crate::ItemLoc<ast::TypeAliasDef>) -> crate::TypeAliasId;
22}
diff --git a/crates/ra_hir_def/src/lib.rs b/crates/ra_hir_def/src/lib.rs
index f5dd2ae6f..4d6b9db03 100644
--- a/crates/ra_hir_def/src/lib.rs
+++ b/crates/ra_hir_def/src/lib.rs
@@ -1,5 +1,37 @@
1//! `hir_def` crate contains everything between macro expansion and type
2//! inference.
3//!
4//! It defines various items (structs, enums, traits) which comprises Rust code,
5//! as well as an algorithm for resolving paths to such entities.
6//!
7//! Note that `hir_def` is a work in progress, so not all of the above is
8//! actually true.
9
10pub mod db;
11
12use std::hash::{Hash, Hasher};
13
14use hir_expand::{ast_id_map::FileAstId, db::AstDatabase, AstId, HirFileId};
1use ra_arena::{impl_arena_id, RawId}; 15use ra_arena::{impl_arena_id, RawId};
2use ra_db::CrateId; 16use ra_db::{salsa, CrateId};
17use ra_syntax::{ast, AstNode, SyntaxNode};
18
19use crate::db::InternDatabase;
20
21#[derive(Debug, PartialEq, Eq, Clone, Copy)]
22pub struct Source<T> {
23 pub file_id: HirFileId,
24 pub ast: T,
25}
26
27impl<T> Source<T> {
28 pub fn map<F: FnOnce(T) -> U, U>(self, f: F) -> Source<U> {
29 Source { file_id: self.file_id, ast: f(self.ast) }
30 }
31 pub fn file_syntax(&self, db: &impl AstDatabase) -> SyntaxNode {
32 db.parse_or_expand(self.file_id).expect("source created from invalid file")
33 }
34}
3 35
4#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 36#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
5pub struct ModuleId { 37pub struct ModuleId {
@@ -12,3 +44,173 @@ pub struct ModuleId {
12#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] 44#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
13pub struct CrateModuleId(RawId); 45pub struct CrateModuleId(RawId);
14impl_arena_id!(CrateModuleId); 46impl_arena_id!(CrateModuleId);
47
48macro_rules! impl_intern_key {
49 ($name:ident) => {
50 impl salsa::InternKey for $name {
51 fn from_intern_id(v: salsa::InternId) -> Self {
52 $name(v)
53 }
54 fn as_intern_id(&self) -> salsa::InternId {
55 self.0
56 }
57 }
58 };
59}
60
61#[derive(Debug)]
62pub struct ItemLoc<N: AstNode> {
63 pub(crate) module: ModuleId,
64 ast_id: AstId<N>,
65}
66
67impl<N: AstNode> PartialEq for ItemLoc<N> {
68 fn eq(&self, other: &Self) -> bool {
69 self.module == other.module && self.ast_id == other.ast_id
70 }
71}
72impl<N: AstNode> Eq for ItemLoc<N> {}
73impl<N: AstNode> Hash for ItemLoc<N> {
74 fn hash<H: Hasher>(&self, hasher: &mut H) {
75 self.module.hash(hasher);
76 self.ast_id.hash(hasher);
77 }
78}
79
80impl<N: AstNode> Clone for ItemLoc<N> {
81 fn clone(&self) -> ItemLoc<N> {
82 ItemLoc { module: self.module, ast_id: self.ast_id }
83 }
84}
85
86#[derive(Clone, Copy)]
87pub struct LocationCtx<DB> {
88 db: DB,
89 module: ModuleId,
90 file_id: HirFileId,
91}
92
93impl<'a, DB> LocationCtx<&'a DB> {
94 pub fn new(db: &'a DB, module: ModuleId, file_id: HirFileId) -> LocationCtx<&'a DB> {
95 LocationCtx { db, module, file_id }
96 }
97}
98
99impl<'a, DB: AstDatabase + InternDatabase> LocationCtx<&'a DB> {
100 pub fn to_def<N, DEF>(self, ast: &N) -> DEF
101 where
102 N: AstNode,
103 DEF: AstItemDef<N>,
104 {
105 DEF::from_ast(self, ast)
106 }
107}
108
109pub trait AstItemDef<N: AstNode>: salsa::InternKey + Clone {
110 fn intern(db: &impl InternDatabase, loc: ItemLoc<N>) -> Self;
111 fn lookup_intern(self, db: &impl InternDatabase) -> ItemLoc<N>;
112
113 fn from_ast(ctx: LocationCtx<&(impl AstDatabase + InternDatabase)>, ast: &N) -> Self {
114 let items = ctx.db.ast_id_map(ctx.file_id);
115 let item_id = items.ast_id(ast);
116 Self::from_ast_id(ctx, item_id)
117 }
118 fn from_ast_id(ctx: LocationCtx<&impl InternDatabase>, ast_id: FileAstId<N>) -> Self {
119 let loc = ItemLoc { module: ctx.module, ast_id: AstId::new(ctx.file_id, ast_id) };
120 Self::intern(ctx.db, loc)
121 }
122 fn source(self, db: &(impl AstDatabase + InternDatabase)) -> Source<N> {
123 let loc = self.lookup_intern(db);
124 let ast = loc.ast_id.to_node(db);
125 Source { file_id: loc.ast_id.file_id(), ast }
126 }
127 fn module(self, db: &impl InternDatabase) -> ModuleId {
128 let loc = self.lookup_intern(db);
129 loc.module
130 }
131}
132
133#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
134pub struct FunctionId(salsa::InternId);
135impl_intern_key!(FunctionId);
136
137impl AstItemDef<ast::FnDef> for FunctionId {
138 fn intern(db: &impl InternDatabase, loc: ItemLoc<ast::FnDef>) -> Self {
139 db.intern_function(loc)
140 }
141 fn lookup_intern(self, db: &impl InternDatabase) -> ItemLoc<ast::FnDef> {
142 db.lookup_intern_function(self)
143 }
144}
145
146#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
147pub struct StructId(salsa::InternId);
148impl_intern_key!(StructId);
149impl AstItemDef<ast::StructDef> for StructId {
150 fn intern(db: &impl InternDatabase, loc: ItemLoc<ast::StructDef>) -> Self {
151 db.intern_struct(loc)
152 }
153 fn lookup_intern(self, db: &impl InternDatabase) -> ItemLoc<ast::StructDef> {
154 db.lookup_intern_struct(self)
155 }
156}
157
158#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
159pub struct EnumId(salsa::InternId);
160impl_intern_key!(EnumId);
161impl AstItemDef<ast::EnumDef> for EnumId {
162 fn intern(db: &impl InternDatabase, loc: ItemLoc<ast::EnumDef>) -> Self {
163 db.intern_enum(loc)
164 }
165 fn lookup_intern(self, db: &impl InternDatabase) -> ItemLoc<ast::EnumDef> {
166 db.lookup_intern_enum(self)
167 }
168}
169
170#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
171pub struct ConstId(salsa::InternId);
172impl_intern_key!(ConstId);
173impl AstItemDef<ast::ConstDef> for ConstId {
174 fn intern(db: &impl InternDatabase, loc: ItemLoc<ast::ConstDef>) -> Self {
175 db.intern_const(loc)
176 }
177 fn lookup_intern(self, db: &impl InternDatabase) -> ItemLoc<ast::ConstDef> {
178 db.lookup_intern_const(self)
179 }
180}
181
182#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
183pub struct StaticId(salsa::InternId);
184impl_intern_key!(StaticId);
185impl AstItemDef<ast::StaticDef> for StaticId {
186 fn intern(db: &impl InternDatabase, loc: ItemLoc<ast::StaticDef>) -> Self {
187 db.intern_static(loc)
188 }
189 fn lookup_intern(self, db: &impl InternDatabase) -> ItemLoc<ast::StaticDef> {
190 db.lookup_intern_static(self)
191 }
192}
193
194#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
195pub struct TraitId(salsa::InternId);
196impl_intern_key!(TraitId);
197impl AstItemDef<ast::TraitDef> for TraitId {
198 fn intern(db: &impl InternDatabase, loc: ItemLoc<ast::TraitDef>) -> Self {
199 db.intern_trait(loc)
200 }
201 fn lookup_intern(self, db: &impl InternDatabase) -> ItemLoc<ast::TraitDef> {
202 db.lookup_intern_trait(self)
203 }
204}
205
206#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
207pub struct TypeAliasId(salsa::InternId);
208impl_intern_key!(TypeAliasId);
209impl AstItemDef<ast::TypeAliasDef> for TypeAliasId {
210 fn intern(db: &impl InternDatabase, loc: ItemLoc<ast::TypeAliasDef>) -> Self {
211 db.intern_type_alias(loc)
212 }
213 fn lookup_intern(self, db: &impl InternDatabase) -> ItemLoc<ast::TypeAliasDef> {
214 db.lookup_intern_type_alias(self)
215 }
216}
diff --git a/crates/ra_ide_api/src/completion/complete_path.rs b/crates/ra_ide_api/src/completion/complete_path.rs
index 23dece73c..956d8ce49 100644
--- a/crates/ra_ide_api/src/completion/complete_path.rs
+++ b/crates/ra_ide_api/src/completion/complete_path.rs
@@ -50,7 +50,7 @@ pub(super) fn complete_path(acc: &mut Completions, ctx: &CompletionContext) {
50 hir::ModuleDef::TypeAlias(a) => a.ty(ctx.db), 50 hir::ModuleDef::TypeAlias(a) => a.ty(ctx.db),
51 _ => unreachable!(), 51 _ => unreachable!(),
52 }; 52 };
53 let krate = ctx.module.and_then(|m| m.krate(ctx.db)); 53 let krate = ctx.module.map(|m| m.krate());
54 if let Some(krate) = krate { 54 if let Some(krate) = krate {
55 ty.iterate_impl_items(ctx.db, krate, |item| { 55 ty.iterate_impl_items(ctx.db, krate, |item| {
56 match item { 56 match item {
diff --git a/crates/ra_ide_api/src/impls.rs b/crates/ra_ide_api/src/impls.rs
index 7fc1b1efa..b899ed3a5 100644
--- a/crates/ra_ide_api/src/impls.rs
+++ b/crates/ra_ide_api/src/impls.rs
@@ -51,7 +51,7 @@ fn impls_for_def(
51 } 51 }
52 }; 52 };
53 53
54 let krate = module.krate(db)?; 54 let krate = module.krate();
55 let impls = db.impls_in_crate(krate); 55 let impls = db.impls_in_crate(krate);
56 56
57 Some( 57 Some(
@@ -72,7 +72,7 @@ fn impls_for_trait(
72 let src = hir::Source { file_id: position.file_id.into(), ast: node.clone() }; 72 let src = hir::Source { file_id: position.file_id.into(), ast: node.clone() };
73 let tr = hir::Trait::from_source(db, src)?; 73 let tr = hir::Trait::from_source(db, src)?;
74 74
75 let krate = module.krate(db)?; 75 let krate = module.krate();
76 let impls = db.impls_in_crate(krate); 76 let impls = db.impls_in_crate(krate);
77 77
78 Some( 78 Some(
diff --git a/crates/ra_ide_api/src/parent_module.rs b/crates/ra_ide_api/src/parent_module.rs
index 566509849..4c57566e2 100644
--- a/crates/ra_ide_api/src/parent_module.rs
+++ b/crates/ra_ide_api/src/parent_module.rs
@@ -27,10 +27,7 @@ pub(crate) fn crate_for(db: &RootDatabase, file_id: FileId) -> Vec<CrateId> {
27 Some(it) => it, 27 Some(it) => it,
28 None => return Vec::new(), 28 None => return Vec::new(),
29 }; 29 };
30 let krate = match module.krate(db) { 30 let krate = module.krate();
31 Some(it) => it,
32 None => return Vec::new(),
33 };
34 vec![krate.crate_id()] 31 vec![krate.crate_id()]
35} 32}
36 33
diff --git a/crates/ra_ide_api/src/references/search_scope.rs b/crates/ra_ide_api/src/references/search_scope.rs
index b6eb248b7..dbd1af597 100644
--- a/crates/ra_ide_api/src/references/search_scope.rs
+++ b/crates/ra_ide_api/src/references/search_scope.rs
@@ -120,7 +120,7 @@ impl NameDefinition {
120 return SearchScope::new(res); 120 return SearchScope::new(res);
121 } 121 }
122 if vis.as_str() == "pub" { 122 if vis.as_str() == "pub" {
123 let krate = self.container.krate(db).unwrap(); 123 let krate = self.container.krate();
124 let crate_graph = db.crate_graph(); 124 let crate_graph = db.crate_graph();
125 for crate_id in crate_graph.iter() { 125 for crate_id in crate_graph.iter() {
126 let mut crate_deps = crate_graph.dependencies(crate_id); 126 let mut crate_deps = crate_graph.dependencies(crate_id);