aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir_def/src/adt.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_hir_def/src/adt.rs')
-rw-r--r--crates/ra_hir_def/src/adt.rs31
1 files changed, 28 insertions, 3 deletions
diff --git a/crates/ra_hir_def/src/adt.rs b/crates/ra_hir_def/src/adt.rs
index de07fc952..8527a6cad 100644
--- a/crates/ra_hir_def/src/adt.rs
+++ b/crates/ra_hir_def/src/adt.rs
@@ -4,17 +4,19 @@ use std::sync::Arc;
4 4
5use either::Either; 5use either::Either;
6use hir_expand::{ 6use hir_expand::{
7 hygiene::Hygiene,
7 name::{AsName, Name}, 8 name::{AsName, Name},
8 InFile, 9 InFile,
9}; 10};
10use ra_arena::{map::ArenaMap, Arena}; 11use ra_arena::{map::ArenaMap, Arena};
12use ra_cfg::CfgOptions;
11use ra_prof::profile; 13use ra_prof::profile;
12use ra_syntax::ast::{self, NameOwner, TypeAscriptionOwner, VisibilityOwner}; 14use ra_syntax::ast::{self, NameOwner, TypeAscriptionOwner, VisibilityOwner};
13 15
14use crate::{ 16use crate::{
15 db::DefDatabase, src::HasChildSource, src::HasSource, trace::Trace, type_ref::TypeRef, 17 attr::Attrs, db::DefDatabase, src::HasChildSource, src::HasSource, trace::Trace,
16 visibility::RawVisibility, EnumId, LocalEnumVariantId, LocalStructFieldId, Lookup, StructId, 18 type_ref::TypeRef, visibility::RawVisibility, EnumId, LocalEnumVariantId, LocalStructFieldId,
17 UnionId, VariantId, 19 Lookup, StructId, UnionId, VariantId,
18}; 20};
19 21
20/// Note that we use `StructData` for unions as well! 22/// Note that we use `StructData` for unions as well!
@@ -49,11 +51,14 @@ pub struct StructFieldData {
49 pub name: Name, 51 pub name: Name,
50 pub type_ref: TypeRef, 52 pub type_ref: TypeRef,
51 pub visibility: RawVisibility, 53 pub visibility: RawVisibility,
54 pub attrs: Attrs,
55 // TODO: add attributes
52} 56}
53 57
54impl StructData { 58impl StructData {
55 pub(crate) fn struct_data_query(db: &dyn DefDatabase, id: StructId) -> Arc<StructData> { 59 pub(crate) fn struct_data_query(db: &dyn DefDatabase, id: StructId) -> Arc<StructData> {
56 let src = id.lookup(db).source(db); 60 let src = id.lookup(db).source(db);
61
57 let name = src.value.name().map_or_else(Name::missing, |n| n.as_name()); 62 let name = src.value.name().map_or_else(Name::missing, |n| n.as_name());
58 let variant_data = VariantData::new(db, src.map(|s| s.kind())); 63 let variant_data = VariantData::new(db, src.map(|s| s.kind()));
59 let variant_data = Arc::new(variant_data); 64 let variant_data = Arc::new(variant_data);
@@ -181,6 +186,10 @@ pub enum StructKind {
181 Unit, 186 Unit,
182} 187}
183 188
189fn is_cfg_enabled(cfg_options: &CfgOptions, attrs: &Attrs) -> bool {
190 attrs.by_key("cfg").tt_values().all(|tt| cfg_options.is_cfg_enabled(tt) != Some(false))
191}
192
184fn lower_struct( 193fn lower_struct(
185 db: &dyn DefDatabase, 194 db: &dyn DefDatabase,
186 trace: &mut Trace<StructFieldData, Either<ast::TupleFieldDef, ast::RecordFieldDef>>, 195 trace: &mut Trace<StructFieldData, Either<ast::TupleFieldDef, ast::RecordFieldDef>>,
@@ -189,12 +198,21 @@ fn lower_struct(
189 match &ast.value { 198 match &ast.value {
190 ast::StructKind::Tuple(fl) => { 199 ast::StructKind::Tuple(fl) => {
191 for (i, fd) in fl.fields().enumerate() { 200 for (i, fd) in fl.fields().enumerate() {
201 let attrs = Attrs::new(&fd, &Hygiene::new(db.upcast(), ast.file_id));
202
203 // Need verification about parent cfg_options and current with current attributes
204 // If it is we are in a case where the cfg is not enabled then we don't have to add this field to check
205 // if !is_cfg_enabled(&crate_graph[module_id.krate].cfg_options, &attrs) {
206 // continue;
207 // }
208
192 trace.alloc( 209 trace.alloc(
193 || Either::Left(fd.clone()), 210 || Either::Left(fd.clone()),
194 || StructFieldData { 211 || StructFieldData {
195 name: Name::new_tuple_field(i), 212 name: Name::new_tuple_field(i),
196 type_ref: TypeRef::from_ast_opt(fd.type_ref()), 213 type_ref: TypeRef::from_ast_opt(fd.type_ref()),
197 visibility: RawVisibility::from_ast(db, ast.with_value(fd.visibility())), 214 visibility: RawVisibility::from_ast(db, ast.with_value(fd.visibility())),
215 attrs: Attrs::new(&fd, &Hygiene::new(db.upcast(), ast.file_id)),
198 }, 216 },
199 ); 217 );
200 } 218 }
@@ -202,12 +220,19 @@ fn lower_struct(
202 } 220 }
203 ast::StructKind::Record(fl) => { 221 ast::StructKind::Record(fl) => {
204 for fd in fl.fields() { 222 for fd in fl.fields() {
223 let attrs = Attrs::new(&fd, &Hygiene::new(db.upcast(), ast.file_id));
224 // Need verification about parent cfg_options and current with current attributes
225 // If it is we are in a case where the cfg is not enabled then we don't have to add this field to check
226 // if !is_cfg_enabled(&crate_graph[module_id.krate].cfg_options, &attrs) {
227 // continue;
228 // }
205 trace.alloc( 229 trace.alloc(
206 || Either::Right(fd.clone()), 230 || Either::Right(fd.clone()),
207 || StructFieldData { 231 || StructFieldData {
208 name: fd.name().map(|n| n.as_name()).unwrap_or_else(Name::missing), 232 name: fd.name().map(|n| n.as_name()).unwrap_or_else(Name::missing),
209 type_ref: TypeRef::from_ast_opt(fd.ascribed_type()), 233 type_ref: TypeRef::from_ast_opt(fd.ascribed_type()),
210 visibility: RawVisibility::from_ast(db, ast.with_value(fd.visibility())), 234 visibility: RawVisibility::from_ast(db, ast.with_value(fd.visibility())),
235 attrs: Attrs::new(&fd, &Hygiene::new(db.upcast(), ast.file_id)),
211 }, 236 },
212 ); 237 );
213 } 238 }