aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src/traits.rs
blob: e39511518d10031a4db48c20ed7a2ab38f910fc5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
//! HIR for trait definitions.

use rustc_hash::FxHashMap;
use std::sync::Arc;

use ra_syntax::ast::{self, NameOwner};

use crate::{
    db::{AstDatabase, DefDatabase},
    ids::LocationCtx,
    name::AsName,
    AssocItem, Const, Function, HasSource, Module, Name, Trait, TypeAlias,
};

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TraitData {
    name: Option<Name>,
    items: Vec<AssocItem>,
    auto: bool,
}

impl TraitData {
    pub(crate) fn trait_data_query(
        db: &(impl DefDatabase + AstDatabase),
        tr: Trait,
    ) -> Arc<TraitData> {
        let src = tr.source(db);
        let name = src.ast.name().map(|n| n.as_name());
        let module = tr.module(db);
        let ctx = LocationCtx::new(db, module, src.file_id);
        let auto = src.ast.is_auto();
        let items = if let Some(item_list) = src.ast.item_list() {
            item_list
                .impl_items()
                .map(|item_node| match item_node {
                    ast::ImplItem::FnDef(it) => Function { id: ctx.to_def(&it) }.into(),
                    ast::ImplItem::ConstDef(it) => Const { id: ctx.to_def(&it) }.into(),
                    ast::ImplItem::TypeAliasDef(it) => TypeAlias { id: ctx.to_def(&it) }.into(),
                })
                .collect()
        } else {
            Vec::new()
        };
        Arc::new(TraitData { name, items, auto })
    }

    pub(crate) fn name(&self) -> &Option<Name> {
        &self.name
    }

    pub(crate) fn items(&self) -> &[AssocItem] {
        &self.items
    }

    pub(crate) fn is_auto(&self) -> bool {
        self.auto
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TraitItemsIndex {
    traits_by_def: FxHashMap<AssocItem, Trait>,
}

impl TraitItemsIndex {
    pub(crate) fn trait_items_index(db: &impl DefDatabase, module: Module) -> TraitItemsIndex {
        let mut index = TraitItemsIndex { traits_by_def: FxHashMap::default() };
        for decl in module.declarations(db) {
            if let crate::ModuleDef::Trait(tr) = decl {
                for item in tr.trait_data(db).items() {
                    index.traits_by_def.insert(*item, tr);
                }
            }
        }
        index
    }

    pub(crate) fn get_parent_trait(&self, item: AssocItem) -> Option<Trait> {
        self.traits_by_def.get(&item).cloned()
    }
}