1 //! Contains basic data about various HIR declarations.
3 use std::{mem, sync::Arc};
5 use hir_expand::{name::Name, AstId, ExpandResult, HirFileId, InFile, MacroCallId, MacroDefKind};
10 body::{Expander, Mark},
13 item_tree::{self, AssocItem, FnFlags, ItemTreeId, ModItem, Param, TreeId},
14 nameres::{attr_resolution::ResolvedAttr, DefMap},
15 type_ref::{TraitRef, TypeBound, TypeRef},
16 visibility::RawVisibility,
17 AssocItemId, AstIdWithPath, ConstId, ConstLoc, FunctionId, FunctionLoc, HasModule, ImplId,
18 Intern, ItemContainerId, Lookup, Macro2Id, MacroRulesId, ModuleId, ProcMacroId, StaticId,
19 TraitId, TypeAliasId, TypeAliasLoc,
22 #[derive(Debug, Clone, PartialEq, Eq)]
23 pub struct FunctionData {
25 pub params: Vec<(Option<Name>, Interned<TypeRef>)>,
26 pub ret_type: Interned<TypeRef>,
27 pub async_ret_type: Option<Interned<TypeRef>>,
29 pub visibility: RawVisibility,
30 pub abi: Option<Interned<str>>,
31 pub legacy_const_generics_indices: Box<[u32]>,
36 pub(crate) fn fn_data_query(db: &dyn DefDatabase, func: FunctionId) -> Arc<FunctionData> {
37 let loc = func.lookup(db);
38 let krate = loc.container.module(db).krate;
39 let crate_graph = db.crate_graph();
40 let cfg_options = &crate_graph[krate].cfg_options;
41 let item_tree = loc.id.item_tree(db);
42 let func = &item_tree[loc.id.value];
43 let visibility = if let ItemContainerId::TraitId(trait_id) = loc.container {
44 db.trait_data(trait_id).visibility.clone()
46 item_tree[func.visibility].clone()
49 let enabled_params = func
52 .filter(|¶m| item_tree.attrs(db, krate, param.into()).is_cfg_enabled(cfg_options));
54 // If last cfg-enabled param is a `...` param, it's a varargs function.
55 let is_varargs = enabled_params
58 .map_or(false, |param| matches!(item_tree[param], Param::Varargs));
60 let mut flags = func.flags;
62 flags |= FnFlags::IS_VARARGS;
64 if flags.contains(FnFlags::HAS_SELF_PARAM) {
65 // If there's a self param in the syntax, but it is cfg'd out, remove the flag.
66 let is_cfgd_out = match func.params.clone().next() {
68 !item_tree.attrs(db, krate, param.into()).is_cfg_enabled(cfg_options)
71 stdx::never!("fn HAS_SELF_PARAM but no parameters allocated");
76 cov_mark::hit!(cfgd_out_self_param);
77 flags.remove(FnFlags::HAS_SELF_PARAM);
81 let legacy_const_generics_indices = item_tree
82 .attrs(db, krate, ModItem::from(loc.id.value).into())
83 .by_key("rustc_legacy_const_generics")
86 .map(parse_rustc_legacy_const_generics)
89 Arc::new(FunctionData {
90 name: func.name.clone(),
91 params: enabled_params
93 .filter_map(|id| match &item_tree[id] {
94 Param::Normal(name, ty) => Some((name.clone(), ty.clone())),
95 Param::Varargs => None,
98 ret_type: func.ret_type.clone(),
99 async_ret_type: func.async_ret_type.clone(),
100 attrs: item_tree.attrs(db, krate, ModItem::from(loc.id.value).into()),
102 abi: func.abi.clone(),
103 legacy_const_generics_indices,
108 pub fn has_body(&self) -> bool {
109 self.flags.contains(FnFlags::HAS_BODY)
112 /// True if the first param is `self`. This is relevant to decide whether this
113 /// can be called as a method.
114 pub fn has_self_param(&self) -> bool {
115 self.flags.contains(FnFlags::HAS_SELF_PARAM)
118 pub fn has_default_kw(&self) -> bool {
119 self.flags.contains(FnFlags::HAS_DEFAULT_KW)
122 pub fn has_const_kw(&self) -> bool {
123 self.flags.contains(FnFlags::HAS_CONST_KW)
126 pub fn has_async_kw(&self) -> bool {
127 self.flags.contains(FnFlags::HAS_ASYNC_KW)
130 pub fn has_unsafe_kw(&self) -> bool {
131 self.flags.contains(FnFlags::HAS_UNSAFE_KW)
134 pub fn is_varargs(&self) -> bool {
135 self.flags.contains(FnFlags::IS_VARARGS)
139 fn parse_rustc_legacy_const_generics(tt: &tt::Subtree) -> Box<[u32]> {
140 let mut indices = Vec::new();
141 for args in tt.token_trees.chunks(2) {
143 tt::TokenTree::Leaf(tt::Leaf::Literal(lit)) => match lit.text.parse() {
144 Ok(index) => indices.push(index),
150 if let Some(comma) = args.get(1) {
152 tt::TokenTree::Leaf(tt::Leaf::Punct(punct)) if punct.char == ',' => {}
158 indices.into_boxed_slice()
161 #[derive(Debug, Clone, PartialEq, Eq)]
162 pub struct TypeAliasData {
164 pub type_ref: Option<Interned<TypeRef>>,
165 pub visibility: RawVisibility,
167 /// Bounds restricting the type alias itself (eg. `type Ty: Bound;` in a trait or impl).
168 pub bounds: Vec<Interned<TypeBound>>,
172 pub(crate) fn type_alias_data_query(
173 db: &dyn DefDatabase,
175 ) -> Arc<TypeAliasData> {
176 let loc = typ.lookup(db);
177 let item_tree = loc.id.item_tree(db);
178 let typ = &item_tree[loc.id.value];
179 let visibility = if let ItemContainerId::TraitId(trait_id) = loc.container {
180 db.trait_data(trait_id).visibility.clone()
182 item_tree[typ.visibility].clone()
185 Arc::new(TypeAliasData {
186 name: typ.name.clone(),
187 type_ref: typ.type_ref.clone(),
189 is_extern: matches!(loc.container, ItemContainerId::ExternBlockId(_)),
190 bounds: typ.bounds.to_vec(),
195 #[derive(Debug, Clone, PartialEq, Eq)]
196 pub struct TraitData {
198 pub items: Vec<(Name, AssocItemId)>,
201 pub visibility: RawVisibility,
202 /// Whether the trait has `#[rust_skip_array_during_method_dispatch]`. `hir_ty` will ignore
203 /// method calls to this trait's methods when the receiver is an array and the crate edition is
205 pub skip_array_during_method_dispatch: bool,
206 // box it as the vec is usually empty anyways
207 pub attribute_calls: Option<Box<Vec<(AstId<ast::Item>, MacroCallId)>>>,
211 pub(crate) fn trait_data_query(db: &dyn DefDatabase, tr: TraitId) -> Arc<TraitData> {
212 let tr_loc = tr.lookup(db);
213 let item_tree = tr_loc.id.item_tree(db);
214 let tr_def = &item_tree[tr_loc.id.value];
215 let _cx = stdx::panic_context::enter(format!(
216 "trait_data_query({:?} -> {:?} -> {:?})",
219 let name = tr_def.name.clone();
220 let is_auto = tr_def.is_auto;
221 let is_unsafe = tr_def.is_unsafe;
222 let module_id = tr_loc.container;
223 let visibility = item_tree[tr_def.visibility].clone();
224 let skip_array_during_method_dispatch = item_tree
225 .attrs(db, tr_loc.container.krate(), ModItem::from(tr_loc.id.value).into())
226 .by_key("rustc_skip_array_during_method_dispatch")
229 let mut collector = AssocItemCollector::new(
233 ItemContainerId::TraitId(tr),
235 collector.collect(tr_loc.id.tree_id(), &tr_def.items);
239 attribute_calls: collector.take_attr_calls(),
240 items: collector.items,
244 skip_array_during_method_dispatch,
248 pub fn associated_types(&self) -> impl Iterator<Item = TypeAliasId> + '_ {
249 self.items.iter().filter_map(|(_name, item)| match item {
250 AssocItemId::TypeAliasId(t) => Some(*t),
255 pub fn associated_type_by_name(&self, name: &Name) -> Option<TypeAliasId> {
256 self.items.iter().find_map(|(item_name, item)| match item {
257 AssocItemId::TypeAliasId(t) if item_name == name => Some(*t),
262 pub fn method_by_name(&self, name: &Name) -> Option<FunctionId> {
263 self.items.iter().find_map(|(item_name, item)| match item {
264 AssocItemId::FunctionId(t) if item_name == name => Some(*t),
269 pub fn attribute_calls(&self) -> impl Iterator<Item = (AstId<ast::Item>, MacroCallId)> + '_ {
270 self.attribute_calls.iter().flat_map(|it| it.iter()).copied()
274 #[derive(Debug, Clone, PartialEq, Eq)]
275 pub struct ImplData {
276 pub target_trait: Option<Interned<TraitRef>>,
277 pub self_ty: Interned<TypeRef>,
278 pub items: Vec<AssocItemId>,
279 pub is_negative: bool,
280 // box it as the vec is usually empty anyways
281 pub attribute_calls: Option<Box<Vec<(AstId<ast::Item>, MacroCallId)>>>,
285 pub(crate) fn impl_data_query(db: &dyn DefDatabase, id: ImplId) -> Arc<ImplData> {
286 let _p = profile::span("impl_data_query");
287 let impl_loc = id.lookup(db);
289 let item_tree = impl_loc.id.item_tree(db);
290 let impl_def = &item_tree[impl_loc.id.value];
291 let target_trait = impl_def.target_trait.clone();
292 let self_ty = impl_def.self_ty.clone();
293 let is_negative = impl_def.is_negative;
294 let module_id = impl_loc.container;
296 let mut collector = AssocItemCollector::new(
299 impl_loc.id.file_id(),
300 ItemContainerId::ImplId(id),
302 collector.collect(impl_loc.id.tree_id(), &impl_def.items);
304 let attribute_calls = collector.take_attr_calls();
305 let items = collector.items.into_iter().map(|(_, item)| item).collect();
307 Arc::new(ImplData { target_trait, self_ty, items, is_negative, attribute_calls })
310 pub fn attribute_calls(&self) -> impl Iterator<Item = (AstId<ast::Item>, MacroCallId)> + '_ {
311 self.attribute_calls.iter().flat_map(|it| it.iter()).copied()
315 #[derive(Debug, Clone, PartialEq, Eq)]
316 pub struct Macro2Data {
318 pub visibility: RawVisibility,
322 pub(crate) fn macro2_data_query(db: &dyn DefDatabase, makro: Macro2Id) -> Arc<Macro2Data> {
323 let loc = makro.lookup(db);
324 let item_tree = loc.id.item_tree(db);
325 let makro = &item_tree[loc.id.value];
327 Arc::new(Macro2Data {
328 name: makro.name.clone(),
329 visibility: item_tree[makro.visibility].clone(),
333 #[derive(Debug, Clone, PartialEq, Eq)]
334 pub struct MacroRulesData {
336 pub macro_export: bool,
339 impl MacroRulesData {
340 pub(crate) fn macro_rules_data_query(
341 db: &dyn DefDatabase,
343 ) -> Arc<MacroRulesData> {
344 let loc = makro.lookup(db);
345 let item_tree = loc.id.item_tree(db);
346 let makro = &item_tree[loc.id.value];
348 let macro_export = item_tree
349 .attrs(db, loc.container.krate(), ModItem::from(loc.id.value).into())
350 .by_key("macro_export")
353 Arc::new(MacroRulesData { name: makro.name.clone(), macro_export })
356 #[derive(Debug, Clone, PartialEq, Eq)]
357 pub struct ProcMacroData {
359 // FIXME: Record deriver helper here?
363 pub(crate) fn proc_macro_data_query(
364 db: &dyn DefDatabase,
366 ) -> Arc<ProcMacroData> {
367 let loc = makro.lookup(db);
368 let item_tree = loc.id.item_tree(db);
369 let makro = &item_tree[loc.id.value];
371 let name = if let Some(def) = item_tree
372 .attrs(db, loc.container.krate(), ModItem::from(loc.id.value).into())
373 .parse_proc_macro_decl(&makro.name)
378 stdx::never!("proc macro declaration is not a proc macro");
381 Arc::new(ProcMacroData { name })
385 #[derive(Debug, Clone, PartialEq, Eq)]
386 pub struct ConstData {
387 /// `None` for `const _: () = ();`
388 pub name: Option<Name>,
389 pub type_ref: Interned<TypeRef>,
390 pub visibility: RawVisibility,
394 pub(crate) fn const_data_query(db: &dyn DefDatabase, konst: ConstId) -> Arc<ConstData> {
395 let loc = konst.lookup(db);
396 let item_tree = loc.id.item_tree(db);
397 let konst = &item_tree[loc.id.value];
398 let visibility = if let ItemContainerId::TraitId(trait_id) = loc.container {
399 db.trait_data(trait_id).visibility.clone()
401 item_tree[konst.visibility].clone()
405 name: konst.name.clone(),
406 type_ref: konst.type_ref.clone(),
412 #[derive(Debug, Clone, PartialEq, Eq)]
413 pub struct StaticData {
415 pub type_ref: Interned<TypeRef>,
416 pub visibility: RawVisibility,
422 pub(crate) fn static_data_query(db: &dyn DefDatabase, konst: StaticId) -> Arc<StaticData> {
423 let loc = konst.lookup(db);
424 let item_tree = loc.id.item_tree(db);
425 let statik = &item_tree[loc.id.value];
427 Arc::new(StaticData {
428 name: statik.name.clone(),
429 type_ref: statik.type_ref.clone(),
430 visibility: item_tree[statik.visibility].clone(),
431 mutable: statik.mutable,
432 is_extern: matches!(loc.container, ItemContainerId::ExternBlockId(_)),
437 struct AssocItemCollector<'a> {
438 db: &'a dyn DefDatabase,
440 def_map: Arc<DefMap>,
441 container: ItemContainerId,
444 items: Vec<(Name, AssocItemId)>,
445 attr_calls: Vec<(AstId<ast::Item>, MacroCallId)>,
448 impl<'a> AssocItemCollector<'a> {
450 db: &'a dyn DefDatabase,
453 container: ItemContainerId,
458 def_map: module_id.def_map(db),
460 expander: Expander::new(db, file_id, module_id),
462 attr_calls: Vec::new(),
466 fn take_attr_calls(&mut self) -> Option<Box<Vec<(AstId<ast::Item>, MacroCallId)>>> {
467 let attribute_calls = mem::take(&mut self.attr_calls);
468 if attribute_calls.is_empty() {
471 Some(Box::new(attribute_calls))
475 // FIXME: proc-macro diagnostics
476 fn collect(&mut self, tree_id: TreeId, assoc_items: &[AssocItem]) {
477 let item_tree = tree_id.item_tree(self.db);
479 'items: for &item in assoc_items {
480 let attrs = item_tree.attrs(self.db, self.module_id.krate, ModItem::from(item).into());
481 if !attrs.is_cfg_enabled(self.expander.cfg_options()) {
485 'attrs: for attr in &*attrs {
487 AstId::new(self.expander.current_file_id(), item.ast_id(&item_tree).upcast());
488 let ast_id_with_path = AstIdWithPath { path: (*attr.path).clone(), ast_id };
490 if let Ok(ResolvedAttr::Macro(call_id)) = self.def_map.resolve_attr_macro(
492 self.module_id.local_id,
496 self.attr_calls.push((ast_id, call_id));
497 // If proc attribute macro expansion is disabled, skip expanding it here
498 if !self.db.enable_proc_attr_macros() {
501 let loc = self.db.lookup_intern_macro_call(call_id);
502 if let MacroDefKind::ProcMacro(exp, ..) = loc.def.kind {
503 // If there's no expander for the proc macro (e.g. the
504 // proc macro is ignored, or building the proc macro
505 // crate failed), skip expansion like we would if it was
506 // disabled. This is analogous to the handling in
507 // `DefCollector::collect_macros`.
512 match self.expander.enter_expand_id(self.db, call_id) {
513 ExpandResult { value: Some((mark, mac)), .. } => {
514 self.collect_macro_items(mark, mac);
517 ExpandResult { .. } => {}
523 AssocItem::Function(id) => {
524 let item = &item_tree[id];
526 FunctionLoc { container: self.container, id: ItemTreeId::new(tree_id, id) }
528 self.items.push((item.name.clone(), def.into()));
530 AssocItem::Const(id) => {
531 let item = &item_tree[id];
532 let name = match item.name.clone() {
537 ConstLoc { container: self.container, id: ItemTreeId::new(tree_id, id) }
539 self.items.push((name, def.into()));
541 AssocItem::TypeAlias(id) => {
542 let item = &item_tree[id];
543 let def = TypeAliasLoc {
544 container: self.container,
545 id: ItemTreeId::new(tree_id, id),
548 self.items.push((item.name.clone(), def.into()));
550 AssocItem::MacroCall(call) => {
551 let call = &item_tree[call];
552 let ast_id_map = self.db.ast_id_map(self.expander.current_file_id());
553 let root = self.db.parse_or_expand(self.expander.current_file_id()).unwrap();
554 let call = ast_id_map.get(call.ast_id).to_node(&root);
556 stdx::panic_context::enter(format!("collect_items MacroCall: {}", call));
557 let res = self.expander.enter_expand(self.db, call);
559 if let Ok(ExpandResult { value: Some((mark, mac)), .. }) = res {
560 self.collect_macro_items(mark, mac);
567 fn collect_macro_items(&mut self, mark: Mark, mac: ast::MacroItems) {
568 let src: InFile<ast::MacroItems> = self.expander.to_source(mac);
569 let tree_id = item_tree::TreeId::new(src.file_id, None);
570 let item_tree = tree_id.item_tree(self.db);
572 item_tree.top_level_items().iter().filter_map(ModItem::as_assoc_item).collect();
574 self.collect(tree_id, &iter);
576 self.expander.exit(self.db, mark);