diff options
Diffstat (limited to 'crates/ra_hir_def')
-rw-r--r-- | crates/ra_hir_def/src/adt.rs | 44 | ||||
-rw-r--r-- | crates/ra_hir_def/src/db.rs | 6 | ||||
-rw-r--r-- | crates/ra_hir_def/src/lib.rs | 23 | ||||
-rw-r--r-- | crates/ra_hir_def/src/visibility.rs | 60 |
4 files changed, 33 insertions, 100 deletions
diff --git a/crates/ra_hir_def/src/adt.rs b/crates/ra_hir_def/src/adt.rs index d9ea693e3..aac5f3e15 100644 --- a/crates/ra_hir_def/src/adt.rs +++ b/crates/ra_hir_def/src/adt.rs | |||
@@ -9,11 +9,12 @@ use hir_expand::{ | |||
9 | }; | 9 | }; |
10 | use ra_arena::{map::ArenaMap, Arena}; | 10 | use ra_arena::{map::ArenaMap, Arena}; |
11 | use ra_prof::profile; | 11 | use ra_prof::profile; |
12 | use ra_syntax::ast::{self, NameOwner, TypeAscriptionOwner}; | 12 | use ra_syntax::ast::{self, NameOwner, TypeAscriptionOwner, VisibilityOwner}; |
13 | 13 | ||
14 | use crate::{ | 14 | use crate::{ |
15 | db::DefDatabase, src::HasChildSource, src::HasSource, trace::Trace, type_ref::TypeRef, EnumId, | 15 | db::DefDatabase, src::HasChildSource, src::HasSource, trace::Trace, type_ref::TypeRef, |
16 | LocalEnumVariantId, LocalStructFieldId, Lookup, StructId, UnionId, VariantId, | 16 | visibility::RawVisibility, EnumId, LocalEnumVariantId, LocalStructFieldId, Lookup, StructId, |
17 | UnionId, VariantId, | ||
17 | }; | 18 | }; |
18 | 19 | ||
19 | /// Note that we use `StructData` for unions as well! | 20 | /// Note that we use `StructData` for unions as well! |
@@ -47,13 +48,14 @@ pub enum VariantData { | |||
47 | pub struct StructFieldData { | 48 | pub struct StructFieldData { |
48 | pub name: Name, | 49 | pub name: Name, |
49 | pub type_ref: TypeRef, | 50 | pub type_ref: TypeRef, |
51 | pub visibility: RawVisibility, | ||
50 | } | 52 | } |
51 | 53 | ||
52 | impl StructData { | 54 | impl StructData { |
53 | pub(crate) fn struct_data_query(db: &impl DefDatabase, id: StructId) -> Arc<StructData> { | 55 | pub(crate) fn struct_data_query(db: &impl DefDatabase, id: StructId) -> Arc<StructData> { |
54 | let src = id.lookup(db).source(db); | 56 | let src = id.lookup(db).source(db); |
55 | let name = src.value.name().map_or_else(Name::missing, |n| n.as_name()); | 57 | let name = src.value.name().map_or_else(Name::missing, |n| n.as_name()); |
56 | let variant_data = VariantData::new(src.value.kind()); | 58 | let variant_data = VariantData::new(db, src.map(|s| s.kind())); |
57 | let variant_data = Arc::new(variant_data); | 59 | let variant_data = Arc::new(variant_data); |
58 | Arc::new(StructData { name, variant_data }) | 60 | Arc::new(StructData { name, variant_data }) |
59 | } | 61 | } |
@@ -61,10 +63,12 @@ impl StructData { | |||
61 | let src = id.lookup(db).source(db); | 63 | let src = id.lookup(db).source(db); |
62 | let name = src.value.name().map_or_else(Name::missing, |n| n.as_name()); | 64 | let name = src.value.name().map_or_else(Name::missing, |n| n.as_name()); |
63 | let variant_data = VariantData::new( | 65 | let variant_data = VariantData::new( |
64 | src.value | 66 | db, |
65 | .record_field_def_list() | 67 | src.map(|s| { |
66 | .map(ast::StructKind::Record) | 68 | s.record_field_def_list() |
67 | .unwrap_or(ast::StructKind::Unit), | 69 | .map(ast::StructKind::Record) |
70 | .unwrap_or(ast::StructKind::Unit) | ||
71 | }), | ||
68 | ); | 72 | ); |
69 | let variant_data = Arc::new(variant_data); | 73 | let variant_data = Arc::new(variant_data); |
70 | Arc::new(StructData { name, variant_data }) | 74 | Arc::new(StructData { name, variant_data }) |
@@ -77,7 +81,7 @@ impl EnumData { | |||
77 | let src = e.lookup(db).source(db); | 81 | let src = e.lookup(db).source(db); |
78 | let name = src.value.name().map_or_else(Name::missing, |n| n.as_name()); | 82 | let name = src.value.name().map_or_else(Name::missing, |n| n.as_name()); |
79 | let mut trace = Trace::new_for_arena(); | 83 | let mut trace = Trace::new_for_arena(); |
80 | lower_enum(&mut trace, &src.value); | 84 | lower_enum(db, &mut trace, &src); |
81 | Arc::new(EnumData { name, variants: trace.into_arena() }) | 85 | Arc::new(EnumData { name, variants: trace.into_arena() }) |
82 | } | 86 | } |
83 | 87 | ||
@@ -93,30 +97,31 @@ impl HasChildSource for EnumId { | |||
93 | fn child_source(&self, db: &impl DefDatabase) -> InFile<ArenaMap<Self::ChildId, Self::Value>> { | 97 | fn child_source(&self, db: &impl DefDatabase) -> InFile<ArenaMap<Self::ChildId, Self::Value>> { |
94 | let src = self.lookup(db).source(db); | 98 | let src = self.lookup(db).source(db); |
95 | let mut trace = Trace::new_for_map(); | 99 | let mut trace = Trace::new_for_map(); |
96 | lower_enum(&mut trace, &src.value); | 100 | lower_enum(db, &mut trace, &src); |
97 | src.with_value(trace.into_map()) | 101 | src.with_value(trace.into_map()) |
98 | } | 102 | } |
99 | } | 103 | } |
100 | 104 | ||
101 | fn lower_enum( | 105 | fn lower_enum( |
106 | db: &impl DefDatabase, | ||
102 | trace: &mut Trace<LocalEnumVariantId, EnumVariantData, ast::EnumVariant>, | 107 | trace: &mut Trace<LocalEnumVariantId, EnumVariantData, ast::EnumVariant>, |
103 | ast: &ast::EnumDef, | 108 | ast: &InFile<ast::EnumDef>, |
104 | ) { | 109 | ) { |
105 | for var in ast.variant_list().into_iter().flat_map(|it| it.variants()) { | 110 | for var in ast.value.variant_list().into_iter().flat_map(|it| it.variants()) { |
106 | trace.alloc( | 111 | trace.alloc( |
107 | || var.clone(), | 112 | || var.clone(), |
108 | || EnumVariantData { | 113 | || EnumVariantData { |
109 | name: var.name().map_or_else(Name::missing, |it| it.as_name()), | 114 | name: var.name().map_or_else(Name::missing, |it| it.as_name()), |
110 | variant_data: Arc::new(VariantData::new(var.kind())), | 115 | variant_data: Arc::new(VariantData::new(db, ast.with_value(var.kind()))), |
111 | }, | 116 | }, |
112 | ); | 117 | ); |
113 | } | 118 | } |
114 | } | 119 | } |
115 | 120 | ||
116 | impl VariantData { | 121 | impl VariantData { |
117 | fn new(flavor: ast::StructKind) -> Self { | 122 | fn new(db: &impl DefDatabase, flavor: InFile<ast::StructKind>) -> Self { |
118 | let mut trace = Trace::new_for_arena(); | 123 | let mut trace = Trace::new_for_arena(); |
119 | match lower_struct(&mut trace, &flavor) { | 124 | match lower_struct(db, &mut trace, &flavor) { |
120 | StructKind::Tuple => VariantData::Tuple(trace.into_arena()), | 125 | StructKind::Tuple => VariantData::Tuple(trace.into_arena()), |
121 | StructKind::Record => VariantData::Record(trace.into_arena()), | 126 | StructKind::Record => VariantData::Record(trace.into_arena()), |
122 | StructKind::Unit => VariantData::Unit, | 127 | StructKind::Unit => VariantData::Unit, |
@@ -163,7 +168,7 @@ impl HasChildSource for VariantId { | |||
163 | }), | 168 | }), |
164 | }; | 169 | }; |
165 | let mut trace = Trace::new_for_map(); | 170 | let mut trace = Trace::new_for_map(); |
166 | lower_struct(&mut trace, &src.value); | 171 | lower_struct(db, &mut trace, &src); |
167 | src.with_value(trace.into_map()) | 172 | src.with_value(trace.into_map()) |
168 | } | 173 | } |
169 | } | 174 | } |
@@ -175,14 +180,15 @@ enum StructKind { | |||
175 | } | 180 | } |
176 | 181 | ||
177 | fn lower_struct( | 182 | fn lower_struct( |
183 | db: &impl DefDatabase, | ||
178 | trace: &mut Trace< | 184 | trace: &mut Trace< |
179 | LocalStructFieldId, | 185 | LocalStructFieldId, |
180 | StructFieldData, | 186 | StructFieldData, |
181 | Either<ast::TupleFieldDef, ast::RecordFieldDef>, | 187 | Either<ast::TupleFieldDef, ast::RecordFieldDef>, |
182 | >, | 188 | >, |
183 | ast: &ast::StructKind, | 189 | ast: &InFile<ast::StructKind>, |
184 | ) -> StructKind { | 190 | ) -> StructKind { |
185 | match ast { | 191 | match &ast.value { |
186 | ast::StructKind::Tuple(fl) => { | 192 | ast::StructKind::Tuple(fl) => { |
187 | for (i, fd) in fl.fields().enumerate() { | 193 | for (i, fd) in fl.fields().enumerate() { |
188 | trace.alloc( | 194 | trace.alloc( |
@@ -190,6 +196,7 @@ fn lower_struct( | |||
190 | || StructFieldData { | 196 | || StructFieldData { |
191 | name: Name::new_tuple_field(i), | 197 | name: Name::new_tuple_field(i), |
192 | type_ref: TypeRef::from_ast_opt(fd.type_ref()), | 198 | type_ref: TypeRef::from_ast_opt(fd.type_ref()), |
199 | visibility: RawVisibility::from_ast(db, ast.with_value(fd.visibility())), | ||
193 | }, | 200 | }, |
194 | ); | 201 | ); |
195 | } | 202 | } |
@@ -202,6 +209,7 @@ fn lower_struct( | |||
202 | || StructFieldData { | 209 | || StructFieldData { |
203 | name: fd.name().map(|n| n.as_name()).unwrap_or_else(Name::missing), | 210 | name: fd.name().map(|n| n.as_name()).unwrap_or_else(Name::missing), |
204 | type_ref: TypeRef::from_ast_opt(fd.ascribed_type()), | 211 | type_ref: TypeRef::from_ast_opt(fd.ascribed_type()), |
212 | visibility: RawVisibility::from_ast(db, ast.with_value(fd.visibility())), | ||
205 | }, | 213 | }, |
206 | ); | 214 | ); |
207 | } | 215 | } |
diff --git a/crates/ra_hir_def/src/db.rs b/crates/ra_hir_def/src/db.rs index 6bc0a8486..c55fd4111 100644 --- a/crates/ra_hir_def/src/db.rs +++ b/crates/ra_hir_def/src/db.rs | |||
@@ -14,10 +14,9 @@ use crate::{ | |||
14 | generics::GenericParams, | 14 | generics::GenericParams, |
15 | lang_item::{LangItemTarget, LangItems}, | 15 | lang_item::{LangItemTarget, LangItems}, |
16 | nameres::{raw::RawItems, CrateDefMap}, | 16 | nameres::{raw::RawItems, CrateDefMap}, |
17 | visibility::RawVisibility, | ||
18 | AttrDefId, ConstId, ConstLoc, DefWithBodyId, EnumId, EnumLoc, FunctionId, FunctionLoc, | 17 | AttrDefId, ConstId, ConstLoc, DefWithBodyId, EnumId, EnumLoc, FunctionId, FunctionLoc, |
19 | GenericDefId, ImplId, ImplLoc, ModuleId, StaticId, StaticLoc, StructId, StructLoc, TraitId, | 18 | GenericDefId, ImplId, ImplLoc, ModuleId, StaticId, StaticLoc, StructId, StructLoc, TraitId, |
20 | TraitLoc, TypeAliasId, TypeAliasLoc, UnionId, UnionLoc, VisibilityDefId, | 19 | TraitLoc, TypeAliasId, TypeAliasLoc, UnionId, UnionLoc, |
21 | }; | 20 | }; |
22 | 21 | ||
23 | #[salsa::query_group(InternDatabaseStorage)] | 22 | #[salsa::query_group(InternDatabaseStorage)] |
@@ -91,9 +90,6 @@ pub trait DefDatabase: InternDatabase + AstDatabase { | |||
91 | #[salsa::invoke(Attrs::attrs_query)] | 90 | #[salsa::invoke(Attrs::attrs_query)] |
92 | fn attrs(&self, def: AttrDefId) -> Attrs; | 91 | fn attrs(&self, def: AttrDefId) -> Attrs; |
93 | 92 | ||
94 | #[salsa::invoke(RawVisibility::visibility_query)] | ||
95 | fn visibility(&self, def: VisibilityDefId) -> RawVisibility; | ||
96 | |||
97 | #[salsa::invoke(LangItems::module_lang_items_query)] | 93 | #[salsa::invoke(LangItems::module_lang_items_query)] |
98 | fn module_lang_items(&self, module: ModuleId) -> Option<Arc<LangItems>>; | 94 | fn module_lang_items(&self, module: ModuleId) -> Option<Arc<LangItems>>; |
99 | 95 | ||
diff --git a/crates/ra_hir_def/src/lib.rs b/crates/ra_hir_def/src/lib.rs index 72a59d867..61f044ecf 100644 --- a/crates/ra_hir_def/src/lib.rs +++ b/crates/ra_hir_def/src/lib.rs | |||
@@ -325,29 +325,6 @@ impl_froms!( | |||
325 | ImplId | 325 | ImplId |
326 | ); | 326 | ); |
327 | 327 | ||
328 | #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] | ||
329 | pub enum VisibilityDefId { | ||
330 | ModuleId(ModuleId), | ||
331 | StructFieldId(StructFieldId), | ||
332 | AdtId(AdtId), | ||
333 | FunctionId(FunctionId), | ||
334 | StaticId(StaticId), | ||
335 | ConstId(ConstId), | ||
336 | TraitId(TraitId), | ||
337 | TypeAliasId(TypeAliasId), | ||
338 | } | ||
339 | |||
340 | impl_froms!( | ||
341 | VisibilityDefId: ModuleId, | ||
342 | StructFieldId, | ||
343 | AdtId(StructId, EnumId, UnionId), | ||
344 | StaticId, | ||
345 | ConstId, | ||
346 | FunctionId, | ||
347 | TraitId, | ||
348 | TypeAliasId | ||
349 | ); | ||
350 | |||
351 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | 328 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
352 | pub enum VariantId { | 329 | pub enum VariantId { |
353 | EnumVariantId(EnumVariantId), | 330 | EnumVariantId(EnumVariantId), |
diff --git a/crates/ra_hir_def/src/visibility.rs b/crates/ra_hir_def/src/visibility.rs index e4c25ab7d..dccf2776e 100644 --- a/crates/ra_hir_def/src/visibility.rs +++ b/crates/ra_hir_def/src/visibility.rs | |||
@@ -2,16 +2,13 @@ | |||
2 | 2 | ||
3 | use std::sync::Arc; | 3 | use std::sync::Arc; |
4 | 4 | ||
5 | use either::Either; | ||
6 | |||
7 | use hir_expand::{hygiene::Hygiene, InFile}; | 5 | use hir_expand::{hygiene::Hygiene, InFile}; |
8 | use ra_syntax::ast::{self, VisibilityOwner}; | 6 | use ra_syntax::ast; |
9 | 7 | ||
10 | use crate::{ | 8 | use crate::{ |
11 | db::DefDatabase, | 9 | db::DefDatabase, |
12 | path::{ModPath, PathKind}, | 10 | path::{ModPath, PathKind}, |
13 | src::{HasChildSource, HasSource}, | 11 | ModuleId, |
14 | AdtId, Lookup, ModuleId, VisibilityDefId, | ||
15 | }; | 12 | }; |
16 | 13 | ||
17 | /// Visibility of an item, not yet resolved. | 14 | /// Visibility of an item, not yet resolved. |
@@ -28,51 +25,15 @@ pub enum RawVisibility { | |||
28 | } | 25 | } |
29 | 26 | ||
30 | impl RawVisibility { | 27 | impl RawVisibility { |
31 | pub(crate) fn visibility_query(db: &impl DefDatabase, def: VisibilityDefId) -> RawVisibility { | ||
32 | match def { | ||
33 | VisibilityDefId::ModuleId(module) => { | ||
34 | let def_map = db.crate_def_map(module.krate); | ||
35 | let src = match def_map[module.local_id].declaration_source(db) { | ||
36 | Some(it) => it, | ||
37 | None => return RawVisibility::private(), | ||
38 | }; | ||
39 | RawVisibility::from_ast(db, src.map(|it| it.visibility())) | ||
40 | } | ||
41 | VisibilityDefId::StructFieldId(it) => { | ||
42 | let src = it.parent.child_source(db); | ||
43 | let is_enum = match it.parent { | ||
44 | crate::VariantId::EnumVariantId(_) => true, | ||
45 | _ => false, | ||
46 | }; | ||
47 | let vis_node = src.map(|m| match &m[it.local_id] { | ||
48 | Either::Left(tuple) => tuple.visibility(), | ||
49 | Either::Right(record) => record.visibility(), | ||
50 | }); | ||
51 | if vis_node.value.is_none() && is_enum { | ||
52 | RawVisibility::Public | ||
53 | } else { | ||
54 | RawVisibility::from_ast(db, vis_node) | ||
55 | } | ||
56 | } | ||
57 | VisibilityDefId::AdtId(it) => match it { | ||
58 | AdtId::StructId(it) => visibility_from_loc(it.lookup(db), db), | ||
59 | AdtId::EnumId(it) => visibility_from_loc(it.lookup(db), db), | ||
60 | AdtId::UnionId(it) => visibility_from_loc(it.lookup(db), db), | ||
61 | }, | ||
62 | VisibilityDefId::TraitId(it) => visibility_from_loc(it.lookup(db), db), | ||
63 | VisibilityDefId::ConstId(it) => visibility_from_loc(it.lookup(db), db), | ||
64 | VisibilityDefId::StaticId(it) => visibility_from_loc(it.lookup(db), db), | ||
65 | VisibilityDefId::FunctionId(it) => visibility_from_loc(it.lookup(db), db), | ||
66 | VisibilityDefId::TypeAliasId(it) => visibility_from_loc(it.lookup(db), db), | ||
67 | } | ||
68 | } | ||
69 | |||
70 | fn private() -> RawVisibility { | 28 | fn private() -> RawVisibility { |
71 | let path = ModPath { kind: PathKind::Super(0), segments: Vec::new() }; | 29 | let path = ModPath { kind: PathKind::Super(0), segments: Vec::new() }; |
72 | RawVisibility::Module(Arc::new(path)) | 30 | RawVisibility::Module(Arc::new(path)) |
73 | } | 31 | } |
74 | 32 | ||
75 | fn from_ast(db: &impl DefDatabase, node: InFile<Option<ast::Visibility>>) -> RawVisibility { | 33 | pub(crate) fn from_ast( |
34 | db: &impl DefDatabase, | ||
35 | node: InFile<Option<ast::Visibility>>, | ||
36 | ) -> RawVisibility { | ||
76 | Self::from_ast_with_hygiene(node.value, &Hygiene::new(db, node.file_id)) | 37 | Self::from_ast_with_hygiene(node.value, &Hygiene::new(db, node.file_id)) |
77 | } | 38 | } |
78 | 39 | ||
@@ -155,12 +116,3 @@ impl Visibility { | |||
155 | ancestors.any(|m| m == to_module.local_id) | 116 | ancestors.any(|m| m == to_module.local_id) |
156 | } | 117 | } |
157 | } | 118 | } |
158 | |||
159 | fn visibility_from_loc<T>(node: T, db: &impl DefDatabase) -> RawVisibility | ||
160 | where | ||
161 | T: HasSource, | ||
162 | T::Value: ast::VisibilityOwner, | ||
163 | { | ||
164 | let src = node.source(db); | ||
165 | RawVisibility::from_ast(db, src.map(|n| n.visibility())) | ||
166 | } | ||