aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src/generics.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_hir/src/generics.rs')
-rw-r--r--crates/ra_hir/src/generics.rs182
1 files changed, 16 insertions, 166 deletions
diff --git a/crates/ra_hir/src/generics.rs b/crates/ra_hir/src/generics.rs
index 78fab1a13..caedb90e6 100644
--- a/crates/ra_hir/src/generics.rs
+++ b/crates/ra_hir/src/generics.rs
@@ -1,47 +1,12 @@
1//! Many kinds of items or constructs can have generic parameters: functions, 1//! Temp module to wrap hir_def::generics
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
6use std::sync::Arc; 2use std::sync::Arc;
7 3
8use hir_def::type_ref::{TypeBound, TypeRef};
9use hir_expand::name::{self, AsName};
10use ra_syntax::ast::{self, NameOwner, TypeBoundsOwner, TypeParamsOwner};
11
12use crate::{ 4use crate::{
13 db::{AstDatabase, DefDatabase, HirDatabase}, 5 db::{AstDatabase, DefDatabase, HirDatabase},
14 Adt, Const, Container, Enum, EnumVariant, Function, HasSource, ImplBlock, Name, Struct, Trait, 6 Adt, Const, Container, Enum, EnumVariant, Function, ImplBlock, Struct, Trait, TypeAlias, Union,
15 TypeAlias, Union,
16}; 7};
17 8
18/// Data about a generic parameter (to a function, struct, impl, ...). 9pub use hir_def::generics::{GenericParam, GenericParams, WherePredicate};
19#[derive(Clone, PartialEq, Eq, Debug)]
20pub struct GenericParam {
21 // FIXME: give generic params proper IDs
22 pub idx: u32,
23 pub name: Name,
24 pub default: Option<TypeRef>,
25}
26
27/// Data about the generic parameters of a function, struct, impl, etc.
28#[derive(Clone, PartialEq, Eq, Debug)]
29pub struct GenericParams {
30 pub(crate) def: GenericDef,
31 pub(crate) parent_params: Option<Arc<GenericParams>>,
32 pub(crate) params: Vec<GenericParam>,
33 pub(crate) where_predicates: Vec<WherePredicate>,
34}
35
36/// A single predicate from a where clause, i.e. `where Type: Trait`. Combined
37/// where clauses like `where T: Foo + Bar` are turned into multiple of these.
38/// It might still result in multiple actual predicates though, because of
39/// associated type bindings like `Iterator<Item = u32>`.
40#[derive(Clone, PartialEq, Eq, Debug)]
41pub struct WherePredicate {
42 pub(crate) type_ref: TypeRef,
43 pub(crate) bound: TypeBound,
44}
45 10
46#[derive(Clone, Copy, PartialEq, Eq, Debug, Hash)] 11#[derive(Clone, Copy, PartialEq, Eq, Debug, Hash)]
47pub enum GenericDef { 12pub enum GenericDef {
@@ -66,134 +31,19 @@ impl_froms!(
66 Const 31 Const
67); 32);
68 33
69impl GenericParams { 34pub(crate) fn generic_params_query(
70 pub(crate) fn generic_params_query( 35 db: &(impl DefDatabase + AstDatabase),
71 db: &(impl DefDatabase + AstDatabase), 36 def: GenericDef,
72 def: GenericDef, 37) -> Arc<GenericParams> {
73 ) -> Arc<GenericParams> { 38 let parent = match def {
74 let parent = match def { 39 GenericDef::Function(it) => it.container(db).map(GenericDef::from),
75 GenericDef::Function(it) => it.container(db).map(GenericDef::from), 40 GenericDef::TypeAlias(it) => it.container(db).map(GenericDef::from),
76 GenericDef::TypeAlias(it) => it.container(db).map(GenericDef::from), 41 GenericDef::Const(it) => it.container(db).map(GenericDef::from),
77 GenericDef::Const(it) => it.container(db).map(GenericDef::from), 42 GenericDef::EnumVariant(it) => Some(it.parent_enum(db).into()),
78 GenericDef::EnumVariant(it) => Some(it.parent_enum(db).into()), 43 GenericDef::Adt(_) | GenericDef::Trait(_) => None,
79 GenericDef::Adt(_) | GenericDef::Trait(_) => None, 44 GenericDef::ImplBlock(_) => None,
80 GenericDef::ImplBlock(_) => None, 45 };
81 }; 46 Arc::new(GenericParams::new(db, def.into(), parent.map(|it| db.generic_params(it))))
82 let mut generics = GenericParams {
83 def,
84 params: Vec::new(),
85 parent_params: parent.map(|p| db.generic_params(p)),
86 where_predicates: Vec::new(),
87 };
88 let start = generics.parent_params.as_ref().map(|p| p.params.len()).unwrap_or(0) as u32;
89 // FIXME: add `: Sized` bound for everything except for `Self` in traits
90 match def {
91 GenericDef::Function(it) => generics.fill(&it.source(db).value, start),
92 GenericDef::Adt(Adt::Struct(it)) => generics.fill(&it.source(db).value, start),
93 GenericDef::Adt(Adt::Union(it)) => generics.fill(&it.source(db).value, start),
94 GenericDef::Adt(Adt::Enum(it)) => generics.fill(&it.source(db).value, start),
95 GenericDef::Trait(it) => {
96 // traits get the Self type as an implicit first type parameter
97 generics.params.push(GenericParam {
98 idx: start,
99 name: name::SELF_TYPE,
100 default: None,
101 });
102 generics.fill(&it.source(db).value, start + 1);
103 // add super traits as bounds on Self
104 // i.e., trait Foo: Bar is equivalent to trait Foo where Self: Bar
105 let self_param = TypeRef::Path(name::SELF_TYPE.into());
106 generics.fill_bounds(&it.source(db).value, self_param);
107 }
108 GenericDef::TypeAlias(it) => generics.fill(&it.source(db).value, start),
109 // Note that we don't add `Self` here: in `impl`s, `Self` is not a
110 // type-parameter, but rather is a type-alias for impl's target
111 // type, so this is handled by the resolver.
112 GenericDef::ImplBlock(it) => generics.fill(&it.source(db).value, start),
113 GenericDef::EnumVariant(_) | GenericDef::Const(_) => {}
114 }
115
116 Arc::new(generics)
117 }
118
119 fn fill(&mut self, node: &impl TypeParamsOwner, start: u32) {
120 if let Some(params) = node.type_param_list() {
121 self.fill_params(params, start)
122 }
123 if let Some(where_clause) = node.where_clause() {
124 self.fill_where_predicates(where_clause);
125 }
126 }
127
128 fn fill_bounds(&mut self, node: &impl ast::TypeBoundsOwner, type_ref: TypeRef) {
129 for bound in
130 node.type_bound_list().iter().flat_map(|type_bound_list| type_bound_list.bounds())
131 {
132 self.add_where_predicate_from_bound(bound, type_ref.clone());
133 }
134 }
135
136 fn fill_params(&mut self, params: ast::TypeParamList, start: u32) {
137 for (idx, type_param) in params.type_params().enumerate() {
138 let name = type_param.name().map_or_else(Name::missing, |it| it.as_name());
139 // FIXME: Use `Path::from_src`
140 let default = type_param.default_type().map(TypeRef::from_ast);
141
142 let param = GenericParam { idx: idx as u32 + start, name: name.clone(), default };
143 self.params.push(param);
144
145 let type_ref = TypeRef::Path(name.into());
146 self.fill_bounds(&type_param, type_ref);
147 }
148 }
149
150 fn fill_where_predicates(&mut self, where_clause: ast::WhereClause) {
151 for pred in where_clause.predicates() {
152 let type_ref = match pred.type_ref() {
153 Some(type_ref) => type_ref,
154 None => continue,
155 };
156 let type_ref = TypeRef::from_ast(type_ref);
157 for bound in pred.type_bound_list().iter().flat_map(|l| l.bounds()) {
158 self.add_where_predicate_from_bound(bound, type_ref.clone());
159 }
160 }
161 }
162
163 fn add_where_predicate_from_bound(&mut self, bound: ast::TypeBound, type_ref: TypeRef) {
164 if bound.has_question_mark() {
165 // FIXME: remove this bound
166 return;
167 }
168 let bound = TypeBound::from_ast(bound);
169 self.where_predicates.push(WherePredicate { type_ref, bound });
170 }
171
172 pub(crate) fn find_by_name(&self, name: &Name) -> Option<&GenericParam> {
173 self.params.iter().find(|p| &p.name == name)
174 }
175
176 pub fn count_parent_params(&self) -> usize {
177 self.parent_params.as_ref().map(|p| p.count_params_including_parent()).unwrap_or(0)
178 }
179
180 pub fn count_params_including_parent(&self) -> usize {
181 let parent_count = self.count_parent_params();
182 parent_count + self.params.len()
183 }
184
185 fn for_each_param<'a>(&'a self, f: &mut impl FnMut(&'a GenericParam)) {
186 if let Some(parent) = &self.parent_params {
187 parent.for_each_param(f);
188 }
189 self.params.iter().for_each(f);
190 }
191
192 pub fn params_including_parent(&self) -> Vec<&GenericParam> {
193 let mut vec = Vec::with_capacity(self.count_params_including_parent());
194 self.for_each_param(&mut |p| vec.push(p));
195 vec
196 }
197} 47}
198 48
199impl GenericDef { 49impl GenericDef {