]> git.lizzy.rs Git - rust.git/blob - crates/ra_hir/src/generics.rs
c494beeb01250d009230e4fe15a1541550973326
[rust.git] / crates / ra_hir / src / generics.rs
1 //! Many kinds of items or constructs can have generic parameters: functions,
2 //! structs, impls, traits, etc. This module provides a common HIR for these
3 //! generic parameters. See also the `Generics` type and the `generics_of` query
4 //! in rustc.
5
6 use std::sync::Arc;
7
8 use ra_syntax::ast::{self, NameOwner, TypeParamsOwner};
9
10 use crate::{db::PersistentHirDatabase, Name, AsName, Function, Struct, Enum, Trait, Type, ImplBlock};
11
12 /// Data about a generic parameter (to a function, struct, impl, ...).
13 #[derive(Clone, PartialEq, Eq, Debug)]
14 pub struct GenericParam {
15     // TODO: give generic params proper IDs
16     pub(crate) idx: u32,
17     pub(crate) name: Name,
18 }
19
20 /// Data about the generic parameters of a function, struct, impl, etc.
21 #[derive(Clone, PartialEq, Eq, Debug, Default)]
22 pub struct GenericParams {
23     pub(crate) parent_params: Option<Arc<GenericParams>>,
24     pub(crate) params: Vec<GenericParam>,
25 }
26
27 #[derive(Clone, Copy, PartialEq, Eq, Debug, Hash)]
28 pub enum GenericDef {
29     Function(Function),
30     Struct(Struct),
31     Enum(Enum),
32     Trait(Trait),
33     Type(Type),
34     ImplBlock(ImplBlock),
35 }
36 impl_froms!(GenericDef: Function, Struct, Enum, Trait, Type, ImplBlock);
37
38 impl GenericParams {
39     pub(crate) fn generic_params_query(
40         db: &impl PersistentHirDatabase,
41         def: GenericDef,
42     ) -> Arc<GenericParams> {
43         let mut generics = GenericParams::default();
44         let parent = match def {
45             GenericDef::Function(it) => it.impl_block(db),
46             GenericDef::Type(it) => it.impl_block(db),
47             GenericDef::Struct(_) | GenericDef::Enum(_) | GenericDef::Trait(_) => None,
48             GenericDef::ImplBlock(_) => None,
49         };
50         generics.parent_params = parent.map(|p| p.generic_params(db));
51         let start = generics.parent_params.as_ref().map(|p| p.params.len()).unwrap_or(0) as u32;
52         match def {
53             GenericDef::Function(it) => generics.fill(&*it.source(db).1, start),
54             GenericDef::Struct(it) => generics.fill(&*it.source(db).1, start),
55             GenericDef::Enum(it) => generics.fill(&*it.source(db).1, start),
56             GenericDef::Trait(it) => generics.fill(&*it.source(db).1, start),
57             GenericDef::Type(it) => generics.fill(&*it.source(db).1, start),
58             GenericDef::ImplBlock(it) => generics.fill(&*it.source(db).1, start),
59         }
60
61         Arc::new(generics)
62     }
63
64     fn fill(&mut self, node: &impl TypeParamsOwner, start: u32) {
65         if let Some(params) = node.type_param_list() {
66             self.fill_params(params, start)
67         }
68     }
69
70     fn fill_params(&mut self, params: &ast::TypeParamList, start: u32) {
71         for (idx, type_param) in params.type_params().enumerate() {
72             let name = type_param.name().map(AsName::as_name).unwrap_or_else(Name::missing);
73             let param = GenericParam { idx: idx as u32 + start, name };
74             self.params.push(param);
75         }
76     }
77
78     pub(crate) fn find_by_name(&self, name: &Name) -> Option<&GenericParam> {
79         self.params.iter().find(|p| &p.name == name)
80     }
81
82     pub fn count_parent_params(&self) -> usize {
83         self.parent_params.as_ref().map(|p| p.count_params_including_parent()).unwrap_or(0)
84     }
85
86     pub fn count_params_including_parent(&self) -> usize {
87         let parent_count = self.count_parent_params();
88         parent_count + self.params.len()
89     }
90
91     fn for_each_param<'a>(&'a self, f: &mut impl FnMut(&'a GenericParam)) {
92         if let Some(parent) = &self.parent_params {
93             parent.for_each_param(f);
94         }
95         self.params.iter().for_each(f);
96     }
97
98     pub fn params_including_parent(&self) -> Vec<&GenericParam> {
99         let mut vec = Vec::with_capacity(self.count_params_including_parent());
100         self.for_each_param(&mut |p| vec.push(p));
101         vec
102     }
103 }