aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir_def/src
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_hir_def/src')
-rw-r--r--crates/ra_hir_def/src/db.rs11
-rw-r--r--crates/ra_hir_def/src/impls.rs (renamed from crates/ra_hir_def/src/imp.rs)0
-rw-r--r--crates/ra_hir_def/src/lib.rs3
-rw-r--r--crates/ra_hir_def/src/nameres.rs8
-rw-r--r--crates/ra_hir_def/src/traits.rs67
5 files changed, 86 insertions, 3 deletions
diff --git a/crates/ra_hir_def/src/db.rs b/crates/ra_hir_def/src/db.rs
index 348aca07f..fb4402463 100644
--- a/crates/ra_hir_def/src/db.rs
+++ b/crates/ra_hir_def/src/db.rs
@@ -8,12 +8,13 @@ use ra_syntax::ast;
8use crate::{ 8use crate::{
9 adt::{EnumData, StructData}, 9 adt::{EnumData, StructData},
10 body::{scope::ExprScopes, Body, BodySourceMap}, 10 body::{scope::ExprScopes, Body, BodySourceMap},
11 imp::ImplData, 11 impls::ImplData,
12 nameres::{ 12 nameres::{
13 raw::{ImportSourceMap, RawItems}, 13 raw::{ImportSourceMap, RawItems},
14 CrateDefMap, 14 CrateDefMap,
15 }, 15 },
16 DefWithBodyId, EnumId, ImplId, ItemLoc, StructOrUnionId, 16 traits::{TraitData, TraitItemsIndex},
17 DefWithBodyId, EnumId, ImplId, ItemLoc, ModuleId, StructOrUnionId, TraitId,
17}; 18};
18 19
19#[salsa::query_group(InternDatabaseStorage)] 20#[salsa::query_group(InternDatabaseStorage)]
@@ -59,6 +60,12 @@ pub trait DefDatabase2: InternDatabase + AstDatabase {
59 #[salsa::invoke(ImplData::impl_data_query)] 60 #[salsa::invoke(ImplData::impl_data_query)]
60 fn impl_data(&self, e: ImplId) -> Arc<ImplData>; 61 fn impl_data(&self, e: ImplId) -> Arc<ImplData>;
61 62
63 #[salsa::invoke(TraitData::trait_data_query)]
64 fn trait_data(&self, e: TraitId) -> Arc<TraitData>;
65
66 #[salsa::invoke(TraitItemsIndex::trait_items_index)]
67 fn trait_items_index(&self, module: ModuleId) -> TraitItemsIndex;
68
62 #[salsa::invoke(Body::body_with_source_map_query)] 69 #[salsa::invoke(Body::body_with_source_map_query)]
63 fn body_with_source_map(&self, def: DefWithBodyId) -> (Arc<Body>, Arc<BodySourceMap>); 70 fn body_with_source_map(&self, def: DefWithBodyId) -> (Arc<Body>, Arc<BodySourceMap>);
64 71
diff --git a/crates/ra_hir_def/src/imp.rs b/crates/ra_hir_def/src/impls.rs
index 4323dfcb6..4323dfcb6 100644
--- a/crates/ra_hir_def/src/imp.rs
+++ b/crates/ra_hir_def/src/impls.rs
diff --git a/crates/ra_hir_def/src/lib.rs b/crates/ra_hir_def/src/lib.rs
index dffc82ff8..38c110570 100644
--- a/crates/ra_hir_def/src/lib.rs
+++ b/crates/ra_hir_def/src/lib.rs
@@ -13,11 +13,12 @@ pub mod path;
13pub mod type_ref; 13pub mod type_ref;
14pub mod builtin_type; 14pub mod builtin_type;
15pub mod adt; 15pub mod adt;
16pub mod imp; 16pub mod impls;
17pub mod diagnostics; 17pub mod diagnostics;
18pub mod expr; 18pub mod expr;
19pub mod body; 19pub mod body;
20pub mod generics; 20pub mod generics;
21pub mod traits;
21 22
22#[cfg(test)] 23#[cfg(test)]
23mod test_db; 24mod test_db;
diff --git a/crates/ra_hir_def/src/nameres.rs b/crates/ra_hir_def/src/nameres.rs
index e5b073a0f..c01e020ef 100644
--- a/crates/ra_hir_def/src/nameres.rs
+++ b/crates/ra_hir_def/src/nameres.rs
@@ -165,6 +165,14 @@ impl ModuleScope {
165 self.items.iter().chain(BUILTIN_SCOPE.iter()) 165 self.items.iter().chain(BUILTIN_SCOPE.iter())
166 } 166 }
167 167
168 pub fn declarations(&self) -> impl Iterator<Item = ModuleDefId> + '_ {
169 self.entries()
170 .filter_map(|(_name, res)| if res.import.is_none() { Some(res.def) } else { None })
171 .flat_map(|per_ns| {
172 per_ns.take_types().into_iter().chain(per_ns.take_values().into_iter())
173 })
174 }
175
168 /// Iterate over all module scoped macros 176 /// Iterate over all module scoped macros
169 pub fn macros<'a>(&'a self) -> impl Iterator<Item = (&'a Name, MacroDefId)> + 'a { 177 pub fn macros<'a>(&'a self) -> impl Iterator<Item = (&'a Name, MacroDefId)> + 'a {
170 self.items 178 self.items
diff --git a/crates/ra_hir_def/src/traits.rs b/crates/ra_hir_def/src/traits.rs
new file mode 100644
index 000000000..a8ba31594
--- /dev/null
+++ b/crates/ra_hir_def/src/traits.rs
@@ -0,0 +1,67 @@
1//! HIR for trait definitions.
2
3use std::sync::Arc;
4
5use hir_expand::name::{AsName, Name};
6
7use ra_syntax::ast::{self, NameOwner};
8use rustc_hash::FxHashMap;
9
10use crate::{
11 db::DefDatabase2, AssocItemId, AstItemDef, ConstId, FunctionId, LocationCtx, ModuleDefId,
12 ModuleId, TraitId, TypeAliasId,
13};
14
15#[derive(Debug, Clone, PartialEq, Eq)]
16pub struct TraitData {
17 pub name: Option<Name>,
18 pub items: Vec<AssocItemId>,
19 pub auto: bool,
20}
21
22impl TraitData {
23 pub(crate) fn trait_data_query(db: &impl DefDatabase2, tr: TraitId) -> Arc<TraitData> {
24 let src = tr.source(db);
25 let name = src.value.name().map(|n| n.as_name());
26 let module = tr.module(db);
27 let ctx = LocationCtx::new(db, module, src.file_id);
28 let auto = src.value.is_auto();
29 let items = if let Some(item_list) = src.value.item_list() {
30 item_list
31 .impl_items()
32 .map(|item_node| match item_node {
33 ast::ImplItem::FnDef(it) => FunctionId::from_ast(ctx, &it).into(),
34 ast::ImplItem::ConstDef(it) => ConstId::from_ast(ctx, &it).into(),
35 ast::ImplItem::TypeAliasDef(it) => TypeAliasId::from_ast(ctx, &it).into(),
36 })
37 .collect()
38 } else {
39 Vec::new()
40 };
41 Arc::new(TraitData { name, items, auto })
42 }
43}
44
45#[derive(Debug, Clone, PartialEq, Eq)]
46pub struct TraitItemsIndex {
47 traits_by_def: FxHashMap<AssocItemId, TraitId>,
48}
49
50impl TraitItemsIndex {
51 pub fn trait_items_index(db: &impl DefDatabase2, module: ModuleId) -> TraitItemsIndex {
52 let mut index = TraitItemsIndex { traits_by_def: FxHashMap::default() };
53 let crate_def_map = db.crate_def_map(module.krate);
54 for decl in crate_def_map[module.module_id].scope.declarations() {
55 if let ModuleDefId::TraitId(tr) = decl {
56 for item in db.trait_data(tr).items.iter() {
57 index.traits_by_def.insert(*item, tr);
58 }
59 }
60 }
61 index
62 }
63
64 pub fn get_parent_trait(&self, item: AssocItemId) -> Option<TraitId> {
65 self.traits_by_def.get(&item).cloned()
66 }
67}