aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src/generics.rs
blob: d8248ad4917ddcff0f8195ec2286333d535693fb (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
//! Many kinds of items or constructs can have generic parameters: functions,
//! structs, impls, traits, etc. This module provides a common HIR for these
//! generic parameters. See also the `Generics` type and the `generics_of` query
//! in rustc.

use std::sync::Arc;

use ra_syntax::ast::{TypeParamList, AstNode, NameOwner};

use crate::{db::HirDatabase, DefId, Name, AsName};

/// Data about a generic parameter (to a function, struct, impl, ...).
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct GenericParam {
    pub(crate) idx: u32,
    pub(crate) name: Name,
}

/// Data about the generic parameters of a function, struct, impl, etc.
#[derive(Clone, PartialEq, Eq, Debug, Default)]
pub struct GenericParams {
    pub(crate) params: Vec<GenericParam>,
}

impl GenericParams {
    pub(crate) fn generic_params_query(db: &impl HirDatabase, def_id: DefId) -> Arc<GenericParams> {
        let (_file_id, node) = def_id.source(db);
        let mut generics = GenericParams::default();
        if let Some(type_param_list) = node.children().find_map(TypeParamList::cast) {
            for (idx, type_param) in type_param_list.type_params().enumerate() {
                let name = type_param
                    .name()
                    .map(AsName::as_name)
                    .unwrap_or_else(Name::missing);
                let param = GenericParam {
                    idx: idx as u32,
                    name,
                };
                generics.params.push(param);
            }
        }
        Arc::new(generics)
    }

    pub(crate) fn find_by_name(&self, name: &Name) -> Option<&GenericParam> {
        self.params.iter().find(|p| &p.name == name)
    }
}