]> git.lizzy.rs Git - rust.git/blob - crates/hir_def/src/data.rs
Ignore extern items in incorrect-case check
[rust.git] / crates / hir_def / src / data.rs
1 //! Contains basic data about various HIR declarations.
2
3 use std::sync::Arc;
4
5 use hir_expand::{name::Name, InFile};
6 use syntax::ast;
7
8 use crate::{
9     attr::Attrs,
10     body::Expander,
11     db::DefDatabase,
12     item_tree::{AssocItem, ItemTreeId, ModItem},
13     type_ref::{TypeBound, TypeRef},
14     visibility::RawVisibility,
15     AssocContainerId, AssocItemId, ConstId, ConstLoc, FunctionId, FunctionLoc, HasModule, ImplId,
16     Intern, Lookup, ModuleId, StaticId, TraitId, TypeAliasId, TypeAliasLoc,
17 };
18
19 #[derive(Debug, Clone, PartialEq, Eq)]
20 pub struct FunctionData {
21     pub name: Name,
22     pub params: Vec<TypeRef>,
23     pub ret_type: TypeRef,
24     pub attrs: Attrs,
25     /// True if the first param is `self`. This is relevant to decide whether this
26     /// can be called as a method.
27     pub has_self_param: bool,
28     pub has_body: bool,
29     pub is_unsafe: bool,
30     pub is_varargs: bool,
31     pub is_extern: bool,
32     pub visibility: RawVisibility,
33 }
34
35 impl FunctionData {
36     pub(crate) fn fn_data_query(db: &dyn DefDatabase, func: FunctionId) -> Arc<FunctionData> {
37         let loc = func.lookup(db);
38         let item_tree = db.item_tree(loc.id.file_id);
39         let func = &item_tree[loc.id.value];
40
41         Arc::new(FunctionData {
42             name: func.name.clone(),
43             params: func.params.to_vec(),
44             ret_type: func.ret_type.clone(),
45             attrs: item_tree.attrs(ModItem::from(loc.id.value).into()).clone(),
46             has_self_param: func.has_self_param,
47             has_body: func.has_body,
48             is_unsafe: func.is_unsafe,
49             is_varargs: func.is_varargs,
50             is_extern: func.is_extern,
51             visibility: item_tree[func.visibility].clone(),
52         })
53     }
54 }
55
56 #[derive(Debug, Clone, PartialEq, Eq)]
57 pub struct TypeAliasData {
58     pub name: Name,
59     pub type_ref: Option<TypeRef>,
60     pub visibility: RawVisibility,
61     pub is_extern: bool,
62     /// Bounds restricting the type alias itself (eg. `type Ty: Bound;` in a trait or impl).
63     pub bounds: Vec<TypeBound>,
64 }
65
66 impl TypeAliasData {
67     pub(crate) fn type_alias_data_query(
68         db: &dyn DefDatabase,
69         typ: TypeAliasId,
70     ) -> Arc<TypeAliasData> {
71         let loc = typ.lookup(db);
72         let item_tree = db.item_tree(loc.id.file_id);
73         let typ = &item_tree[loc.id.value];
74
75         Arc::new(TypeAliasData {
76             name: typ.name.clone(),
77             type_ref: typ.type_ref.clone(),
78             visibility: item_tree[typ.visibility].clone(),
79             is_extern: typ.is_extern,
80             bounds: typ.bounds.to_vec(),
81         })
82     }
83 }
84
85 #[derive(Debug, Clone, PartialEq, Eq)]
86 pub struct TraitData {
87     pub name: Name,
88     pub items: Vec<(Name, AssocItemId)>,
89     pub auto: bool,
90 }
91
92 impl TraitData {
93     pub(crate) fn trait_data_query(db: &dyn DefDatabase, tr: TraitId) -> Arc<TraitData> {
94         let tr_loc = tr.lookup(db);
95         let item_tree = db.item_tree(tr_loc.id.file_id);
96         let tr_def = &item_tree[tr_loc.id.value];
97         let name = tr_def.name.clone();
98         let auto = tr_def.auto;
99         let module_id = tr_loc.container.module(db);
100         let container = AssocContainerId::TraitId(tr);
101         let mut expander = Expander::new(db, tr_loc.id.file_id, module_id);
102
103         let items = collect_items(
104             db,
105             module_id,
106             &mut expander,
107             tr_def.items.iter().copied(),
108             tr_loc.id.file_id,
109             container,
110             100,
111         );
112
113         Arc::new(TraitData { name, items, auto })
114     }
115
116     pub fn associated_types(&self) -> impl Iterator<Item = TypeAliasId> + '_ {
117         self.items.iter().filter_map(|(_name, item)| match item {
118             AssocItemId::TypeAliasId(t) => Some(*t),
119             _ => None,
120         })
121     }
122
123     pub fn associated_type_by_name(&self, name: &Name) -> Option<TypeAliasId> {
124         self.items.iter().find_map(|(item_name, item)| match item {
125             AssocItemId::TypeAliasId(t) if item_name == name => Some(*t),
126             _ => None,
127         })
128     }
129 }
130
131 #[derive(Debug, Clone, PartialEq, Eq)]
132 pub struct ImplData {
133     pub target_trait: Option<TypeRef>,
134     pub target_type: TypeRef,
135     pub items: Vec<AssocItemId>,
136     pub is_negative: bool,
137 }
138
139 impl ImplData {
140     pub(crate) fn impl_data_query(db: &dyn DefDatabase, id: ImplId) -> Arc<ImplData> {
141         let _p = profile::span("impl_data_query");
142         let impl_loc = id.lookup(db);
143
144         let item_tree = db.item_tree(impl_loc.id.file_id);
145         let impl_def = &item_tree[impl_loc.id.value];
146         let target_trait = impl_def.target_trait.clone();
147         let target_type = impl_def.target_type.clone();
148         let is_negative = impl_def.is_negative;
149         let module_id = impl_loc.container.module(db);
150         let container = AssocContainerId::ImplId(id);
151         let mut expander = Expander::new(db, impl_loc.id.file_id, module_id);
152
153         let items = collect_items(
154             db,
155             module_id,
156             &mut expander,
157             impl_def.items.iter().copied(),
158             impl_loc.id.file_id,
159             container,
160             100,
161         );
162         let items = items.into_iter().map(|(_, item)| item).collect();
163
164         Arc::new(ImplData { target_trait, target_type, items, is_negative })
165     }
166 }
167
168 #[derive(Debug, Clone, PartialEq, Eq)]
169 pub struct ConstData {
170     /// const _: () = ();
171     pub name: Option<Name>,
172     pub type_ref: TypeRef,
173     pub visibility: RawVisibility,
174 }
175
176 impl ConstData {
177     pub(crate) fn const_data_query(db: &dyn DefDatabase, konst: ConstId) -> Arc<ConstData> {
178         let loc = konst.lookup(db);
179         let item_tree = db.item_tree(loc.id.file_id);
180         let konst = &item_tree[loc.id.value];
181
182         Arc::new(ConstData {
183             name: konst.name.clone(),
184             type_ref: konst.type_ref.clone(),
185             visibility: item_tree[konst.visibility].clone(),
186         })
187     }
188 }
189
190 #[derive(Debug, Clone, PartialEq, Eq)]
191 pub struct StaticData {
192     pub name: Option<Name>,
193     pub type_ref: TypeRef,
194     pub visibility: RawVisibility,
195     pub mutable: bool,
196     pub is_extern: bool,
197 }
198
199 impl StaticData {
200     pub(crate) fn static_data_query(db: &dyn DefDatabase, konst: StaticId) -> Arc<StaticData> {
201         let node = konst.lookup(db);
202         let item_tree = db.item_tree(node.id.file_id);
203         let statik = &item_tree[node.id.value];
204
205         Arc::new(StaticData {
206             name: Some(statik.name.clone()),
207             type_ref: statik.type_ref.clone(),
208             visibility: item_tree[statik.visibility].clone(),
209             mutable: statik.mutable,
210             is_extern: statik.is_extern,
211         })
212     }
213 }
214
215 fn collect_items(
216     db: &dyn DefDatabase,
217     module: ModuleId,
218     expander: &mut Expander,
219     assoc_items: impl Iterator<Item = AssocItem>,
220     file_id: crate::HirFileId,
221     container: AssocContainerId,
222     limit: usize,
223 ) -> Vec<(Name, AssocItemId)> {
224     if limit == 0 {
225         return Vec::new();
226     }
227
228     let item_tree = db.item_tree(file_id);
229     let cfg_options = db.crate_graph()[module.krate].cfg_options.clone();
230
231     let mut items = Vec::new();
232     for item in assoc_items {
233         match item {
234             AssocItem::Function(id) => {
235                 let item = &item_tree[id];
236                 let attrs = item_tree.attrs(ModItem::from(id).into());
237                 if !attrs.is_cfg_enabled(&cfg_options) {
238                     continue;
239                 }
240                 let def = FunctionLoc { container, id: ItemTreeId::new(file_id, id) }.intern(db);
241                 items.push((item.name.clone(), def.into()));
242             }
243             // FIXME: cfg?
244             AssocItem::Const(id) => {
245                 let item = &item_tree[id];
246                 let name = match item.name.clone() {
247                     Some(name) => name,
248                     None => continue,
249                 };
250                 let def = ConstLoc { container, id: ItemTreeId::new(file_id, id) }.intern(db);
251                 items.push((name, def.into()));
252             }
253             AssocItem::TypeAlias(id) => {
254                 let item = &item_tree[id];
255                 let def = TypeAliasLoc { container, id: ItemTreeId::new(file_id, id) }.intern(db);
256                 items.push((item.name.clone(), def.into()));
257             }
258             AssocItem::MacroCall(call) => {
259                 let call = &item_tree[call];
260                 let ast_id_map = db.ast_id_map(file_id);
261                 let root = db.parse_or_expand(file_id).unwrap();
262                 let call = ast_id_map.get(call.ast_id).to_node(&root);
263
264                 if let Some((mark, mac)) = expander.enter_expand(db, None, call).value {
265                     let src: InFile<ast::MacroItems> = expander.to_source(mac);
266                     let item_tree = db.item_tree(src.file_id);
267                     let iter =
268                         item_tree.top_level_items().iter().filter_map(ModItem::as_assoc_item);
269                     items.extend(collect_items(
270                         db,
271                         module,
272                         expander,
273                         iter,
274                         src.file_id,
275                         container,
276                         limit - 1,
277                     ));
278
279                     expander.exit(db, mark);
280                 }
281             }
282         }
283     }
284
285     items
286 }