aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir_def/src
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2019-12-06 16:35:05 +0000
committerAleksey Kladov <[email protected]>2019-12-07 12:05:56 +0000
commit30fefcc08cc0c670ce541476491238d258ca55c1 (patch)
tree998e7ddf231d0da32d08641fea17c1662f26f0e1 /crates/ra_hir_def/src
parent35fc983dd9e904ad4961b9c10be3397bad33da0c (diff)
Store GenericParams in arena
Diffstat (limited to 'crates/ra_hir_def/src')
-rw-r--r--crates/ra_hir_def/src/generics.rs25
-rw-r--r--crates/ra_hir_def/src/lib.rs10
-rw-r--r--crates/ra_hir_def/src/resolver.rs2
3 files changed, 24 insertions, 13 deletions
diff --git a/crates/ra_hir_def/src/generics.rs b/crates/ra_hir_def/src/generics.rs
index 5f648ffc3..abe749a40 100644
--- a/crates/ra_hir_def/src/generics.rs
+++ b/crates/ra_hir_def/src/generics.rs
@@ -5,18 +5,19 @@
5use std::sync::Arc; 5use std::sync::Arc;
6 6
7use hir_expand::name::{self, AsName, Name}; 7use hir_expand::name::{self, AsName, Name};
8use ra_arena::Arena;
8use ra_syntax::ast::{self, NameOwner, TypeBoundsOwner, TypeParamsOwner}; 9use ra_syntax::ast::{self, NameOwner, TypeBoundsOwner, TypeParamsOwner};
9 10
10use crate::{ 11use crate::{
11 db::DefDatabase, 12 db::DefDatabase,
12 src::HasSource, 13 src::HasSource,
13 type_ref::{TypeBound, TypeRef}, 14 type_ref::{TypeBound, TypeRef},
14 AdtId, AstItemDef, ContainerId, GenericDefId, Lookup, 15 AdtId, AstItemDef, ContainerId, GenericDefId, LocalGenericParamId, Lookup,
15}; 16};
16 17
17/// Data about a generic parameter (to a function, struct, impl, ...). 18/// Data about a generic parameter (to a function, struct, impl, ...).
18#[derive(Clone, PartialEq, Eq, Debug)] 19#[derive(Clone, PartialEq, Eq, Debug)]
19pub struct GenericParam { 20pub struct GenericParamData {
20 // FIXME: give generic params proper IDs 21 // FIXME: give generic params proper IDs
21 pub idx: u32, 22 pub idx: u32,
22 pub name: Name, 23 pub name: Name,
@@ -27,7 +28,7 @@ pub struct GenericParam {
27#[derive(Clone, PartialEq, Eq, Debug)] 28#[derive(Clone, PartialEq, Eq, Debug)]
28pub struct GenericParams { 29pub struct GenericParams {
29 pub parent_params: Option<Arc<GenericParams>>, 30 pub parent_params: Option<Arc<GenericParams>>,
30 pub params: Vec<GenericParam>, 31 pub params: Arena<LocalGenericParamId, GenericParamData>,
31 pub where_predicates: Vec<WherePredicate>, 32 pub where_predicates: Vec<WherePredicate>,
32} 33}
33 34
@@ -56,7 +57,7 @@ impl GenericParams {
56 parent_params: Option<Arc<GenericParams>>, 57 parent_params: Option<Arc<GenericParams>>,
57 ) -> GenericParams { 58 ) -> GenericParams {
58 let mut generics = 59 let mut generics =
59 GenericParams { params: Vec::new(), parent_params, where_predicates: Vec::new() }; 60 GenericParams { params: Arena::default(), parent_params, where_predicates: Vec::new() };
60 let start = generics.parent_params.as_ref().map(|p| p.params.len()).unwrap_or(0) as u32; 61 let start = generics.parent_params.as_ref().map(|p| p.params.len()).unwrap_or(0) as u32;
61 // FIXME: add `: Sized` bound for everything except for `Self` in traits 62 // FIXME: add `: Sized` bound for everything except for `Self` in traits
62 match def { 63 match def {
@@ -66,7 +67,7 @@ impl GenericParams {
66 GenericDefId::AdtId(AdtId::EnumId(it)) => generics.fill(&it.source(db).value, start), 67 GenericDefId::AdtId(AdtId::EnumId(it)) => generics.fill(&it.source(db).value, start),
67 GenericDefId::TraitId(it) => { 68 GenericDefId::TraitId(it) => {
68 // traits get the Self type as an implicit first type parameter 69 // traits get the Self type as an implicit first type parameter
69 generics.params.push(GenericParam { 70 generics.params.alloc(GenericParamData {
70 idx: start, 71 idx: start,
71 name: name::SELF_TYPE, 72 name: name::SELF_TYPE,
72 default: None, 73 default: None,
@@ -110,8 +111,8 @@ impl GenericParams {
110 let name = type_param.name().map_or_else(Name::missing, |it| it.as_name()); 111 let name = type_param.name().map_or_else(Name::missing, |it| it.as_name());
111 // FIXME: Use `Path::from_src` 112 // FIXME: Use `Path::from_src`
112 let default = type_param.default_type().map(TypeRef::from_ast); 113 let default = type_param.default_type().map(TypeRef::from_ast);
113 let param = GenericParam { idx: idx as u32 + start, name: name.clone(), default }; 114 let param = GenericParamData { idx: idx as u32 + start, name: name.clone(), default };
114 self.params.push(param); 115 self.params.alloc(param);
115 116
116 let type_ref = TypeRef::Path(name.into()); 117 let type_ref = TypeRef::Path(name.into());
117 self.fill_bounds(&type_param, type_ref); 118 self.fill_bounds(&type_param, type_ref);
@@ -140,8 +141,8 @@ impl GenericParams {
140 self.where_predicates.push(WherePredicate { type_ref, bound }); 141 self.where_predicates.push(WherePredicate { type_ref, bound });
141 } 142 }
142 143
143 pub fn find_by_name(&self, name: &Name) -> Option<&GenericParam> { 144 pub fn find_by_name(&self, name: &Name) -> Option<&GenericParamData> {
144 self.params.iter().find(|p| &p.name == name) 145 self.params.iter().map(|(_id, data)| data).find(|p| &p.name == name)
145 } 146 }
146 147
147 pub fn count_parent_params(&self) -> usize { 148 pub fn count_parent_params(&self) -> usize {
@@ -153,14 +154,14 @@ impl GenericParams {
153 parent_count + self.params.len() 154 parent_count + self.params.len()
154 } 155 }
155 156
156 fn for_each_param<'a>(&'a self, f: &mut impl FnMut(&'a GenericParam)) { 157 fn for_each_param<'a>(&'a self, f: &mut impl FnMut(&'a GenericParamData)) {
157 if let Some(parent) = &self.parent_params { 158 if let Some(parent) = &self.parent_params {
158 parent.for_each_param(f); 159 parent.for_each_param(f);
159 } 160 }
160 self.params.iter().for_each(f); 161 self.params.iter().map(|(_id, data)| data).for_each(f);
161 } 162 }
162 163
163 pub fn params_including_parent(&self) -> Vec<&GenericParam> { 164 pub fn params_including_parent(&self) -> Vec<&GenericParamData> {
164 let mut vec = Vec::with_capacity(self.count_params_including_parent()); 165 let mut vec = Vec::with_capacity(self.count_params_including_parent());
165 self.for_each_param(&mut |p| vec.push(p)); 166 self.for_each_param(&mut |p| vec.push(p));
166 vec 167 vec
diff --git a/crates/ra_hir_def/src/lib.rs b/crates/ra_hir_def/src/lib.rs
index 68e66d276..b8dfc0ab1 100644
--- a/crates/ra_hir_def/src/lib.rs
+++ b/crates/ra_hir_def/src/lib.rs
@@ -318,6 +318,16 @@ macro_rules! impl_froms {
318} 318}
319 319
320#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 320#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
321pub struct GenericParamId {
322 pub parent: GenericDefId,
323 pub local_id: LocalGenericParamId,
324}
325
326#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
327pub struct LocalGenericParamId(RawId);
328impl_arena_id!(LocalGenericParamId);
329
330#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
321pub enum ContainerId { 331pub enum ContainerId {
322 ModuleId(ModuleId), 332 ModuleId(ModuleId),
323 ImplId(ImplId), 333 ImplId(ImplId),
diff --git a/crates/ra_hir_def/src/resolver.rs b/crates/ra_hir_def/src/resolver.rs
index 7d4df222e..ee19d79a7 100644
--- a/crates/ra_hir_def/src/resolver.rs
+++ b/crates/ra_hir_def/src/resolver.rs
@@ -426,7 +426,7 @@ impl Scope {
426 } 426 }
427 } 427 }
428 Scope::GenericParams { params, .. } => { 428 Scope::GenericParams { params, .. } => {
429 for param in params.params.iter() { 429 for (_id, param) in params.params.iter() {
430 f(param.name.clone(), ScopeDef::GenericParam(param.idx)) 430 f(param.name.clone(), ScopeDef::GenericParam(param.idx))
431 } 431 }
432 } 432 }