diff options
author | Aleksey Kladov <[email protected]> | 2019-11-20 11:22:06 +0000 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2019-11-20 11:22:38 +0000 |
commit | 06fa3d8389c833b01f482bf35b0f850e627612b9 (patch) | |
tree | cec2d13fdf65bbc4d4cef9f3c1118afc7f264803 /crates/ra_hir/src/traits.rs | |
parent | 3d56e3d85516fe9061aea84571ac88c3322a687c (diff) |
Move traits to hir_def
Diffstat (limited to 'crates/ra_hir/src/traits.rs')
-rw-r--r-- | crates/ra_hir/src/traits.rs | 82 |
1 files changed, 0 insertions, 82 deletions
diff --git a/crates/ra_hir/src/traits.rs b/crates/ra_hir/src/traits.rs deleted file mode 100644 index 858972c6f..000000000 --- a/crates/ra_hir/src/traits.rs +++ /dev/null | |||
@@ -1,82 +0,0 @@ | |||
1 | //! HIR for trait definitions. | ||
2 | |||
3 | use std::sync::Arc; | ||
4 | |||
5 | use hir_expand::name::AsName; | ||
6 | |||
7 | use ra_syntax::ast::{self, NameOwner}; | ||
8 | use rustc_hash::FxHashMap; | ||
9 | |||
10 | use crate::{ | ||
11 | db::{AstDatabase, DefDatabase}, | ||
12 | ids::LocationCtx, | ||
13 | AssocItem, Const, Function, HasSource, Module, Name, Trait, TypeAlias, | ||
14 | }; | ||
15 | |||
16 | #[derive(Debug, Clone, PartialEq, Eq)] | ||
17 | pub struct TraitData { | ||
18 | name: Option<Name>, | ||
19 | items: Vec<AssocItem>, | ||
20 | auto: bool, | ||
21 | } | ||
22 | |||
23 | impl TraitData { | ||
24 | pub(crate) fn trait_data_query( | ||
25 | db: &(impl DefDatabase + AstDatabase), | ||
26 | tr: Trait, | ||
27 | ) -> Arc<TraitData> { | ||
28 | let src = tr.source(db); | ||
29 | let name = src.value.name().map(|n| n.as_name()); | ||
30 | let module = tr.module(db); | ||
31 | let ctx = LocationCtx::new(db, module.id, src.file_id); | ||
32 | let auto = src.value.is_auto(); | ||
33 | let items = if let Some(item_list) = src.value.item_list() { | ||
34 | item_list | ||
35 | .impl_items() | ||
36 | .map(|item_node| match item_node { | ||
37 | ast::ImplItem::FnDef(it) => Function { id: ctx.to_def(&it) }.into(), | ||
38 | ast::ImplItem::ConstDef(it) => Const { id: ctx.to_def(&it) }.into(), | ||
39 | ast::ImplItem::TypeAliasDef(it) => TypeAlias { id: ctx.to_def(&it) }.into(), | ||
40 | }) | ||
41 | .collect() | ||
42 | } else { | ||
43 | Vec::new() | ||
44 | }; | ||
45 | Arc::new(TraitData { name, items, auto }) | ||
46 | } | ||
47 | |||
48 | pub(crate) fn name(&self) -> &Option<Name> { | ||
49 | &self.name | ||
50 | } | ||
51 | |||
52 | pub(crate) fn items(&self) -> &[AssocItem] { | ||
53 | &self.items | ||
54 | } | ||
55 | |||
56 | pub(crate) fn is_auto(&self) -> bool { | ||
57 | self.auto | ||
58 | } | ||
59 | } | ||
60 | |||
61 | #[derive(Debug, Clone, PartialEq, Eq)] | ||
62 | pub struct TraitItemsIndex { | ||
63 | traits_by_def: FxHashMap<AssocItem, Trait>, | ||
64 | } | ||
65 | |||
66 | impl TraitItemsIndex { | ||
67 | pub(crate) fn trait_items_index(db: &impl DefDatabase, module: Module) -> TraitItemsIndex { | ||
68 | let mut index = TraitItemsIndex { traits_by_def: FxHashMap::default() }; | ||
69 | for decl in module.declarations(db) { | ||
70 | if let crate::ModuleDef::Trait(tr) = decl { | ||
71 | for item in tr.trait_data(db).items() { | ||
72 | index.traits_by_def.insert(*item, tr); | ||
73 | } | ||
74 | } | ||
75 | } | ||
76 | index | ||
77 | } | ||
78 | |||
79 | pub(crate) fn get_parent_trait(&self, item: AssocItem) -> Option<Trait> { | ||
80 | self.traits_by_def.get(&item).cloned() | ||
81 | } | ||
82 | } | ||