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