aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir_def/src/lang_item.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_hir_def/src/lang_item.rs')
-rw-r--r--crates/ra_hir_def/src/lang_item.rs121
1 files changed, 121 insertions, 0 deletions
diff --git a/crates/ra_hir_def/src/lang_item.rs b/crates/ra_hir_def/src/lang_item.rs
new file mode 100644
index 000000000..f15c23db9
--- /dev/null
+++ b/crates/ra_hir_def/src/lang_item.rs
@@ -0,0 +1,121 @@
1//! Collects lang items: items marked with `#[lang = "..."]` attribute.
2//!
3//! This attribute to tell the compiler about semi built-in std library
4//! features, such as Fn family of traits.
5use std::sync::Arc;
6
7use ra_syntax::SmolStr;
8use rustc_hash::FxHashMap;
9
10use crate::{
11 db::DefDatabase, AdtId, AttrDefId, CrateId, EnumId, FunctionId, ImplId, ModuleDefId, ModuleId,
12 StaticId, StructId, TraitId,
13};
14
15#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
16pub enum LangItemTarget {
17 EnumId(EnumId),
18 FunctionId(FunctionId),
19 ImplBlockId(ImplId),
20 StaticId(StaticId),
21 StructId(StructId),
22 TraitId(TraitId),
23}
24
25#[derive(Default, Debug, Clone, PartialEq, Eq)]
26pub struct LangItems {
27 items: FxHashMap<SmolStr, LangItemTarget>,
28}
29
30impl LangItems {
31 pub fn target<'a>(&'a self, item: &str) -> Option<&'a LangItemTarget> {
32 self.items.get(item)
33 }
34
35 /// Salsa query. This will look for lang items in a specific crate.
36 pub(crate) fn crate_lang_items_query(db: &impl DefDatabase, krate: CrateId) -> Arc<LangItems> {
37 let mut lang_items = LangItems::default();
38
39 let crate_def_map = db.crate_def_map(krate);
40
41 crate_def_map
42 .modules
43 .iter()
44 .filter_map(|(module_id, _)| db.module_lang_items(ModuleId { krate, module_id }))
45 .for_each(|it| lang_items.items.extend(it.items.iter().map(|(k, v)| (k.clone(), *v))));
46
47 Arc::new(lang_items)
48 }
49
50 pub(crate) fn module_lang_items_query(
51 db: &impl DefDatabase,
52 module: ModuleId,
53 ) -> Option<Arc<LangItems>> {
54 let mut lang_items = LangItems::default();
55 lang_items.collect_lang_items(db, module);
56 if lang_items.items.is_empty() {
57 None
58 } else {
59 Some(Arc::new(lang_items))
60 }
61 }
62
63 /// Salsa query. Look for a lang item, starting from the specified crate and recursively
64 /// traversing its dependencies.
65 pub(crate) fn lang_item_query(
66 db: &impl DefDatabase,
67 start_crate: CrateId,
68 item: SmolStr,
69 ) -> Option<LangItemTarget> {
70 let lang_items = db.crate_lang_items(start_crate);
71 let start_crate_target = lang_items.items.get(&item);
72 if let Some(target) = start_crate_target {
73 return Some(*target);
74 }
75 db.crate_graph()
76 .dependencies(start_crate)
77 .find_map(|dep| db.lang_item(dep.crate_id, item.clone()))
78 }
79
80 fn collect_lang_items(&mut self, db: &impl DefDatabase, module: ModuleId) {
81 // Look for impl targets
82 let def_map = db.crate_def_map(module.krate);
83 let module_data = &def_map[module.module_id];
84 for &impl_block in module_data.impls.iter() {
85 self.collect_lang_item(db, impl_block, LangItemTarget::ImplBlockId)
86 }
87
88 for def in module_data.scope.declarations() {
89 match def {
90 ModuleDefId::TraitId(trait_) => {
91 self.collect_lang_item(db, trait_, LangItemTarget::TraitId)
92 }
93 ModuleDefId::AdtId(AdtId::EnumId(e)) => {
94 self.collect_lang_item(db, e, LangItemTarget::EnumId)
95 }
96 ModuleDefId::AdtId(AdtId::StructId(s)) => {
97 self.collect_lang_item(db, s, LangItemTarget::StructId)
98 }
99 ModuleDefId::FunctionId(f) => {
100 self.collect_lang_item(db, f, LangItemTarget::FunctionId)
101 }
102 ModuleDefId::StaticId(s) => self.collect_lang_item(db, s, LangItemTarget::StaticId),
103 _ => {}
104 }
105 }
106 }
107
108 fn collect_lang_item<T>(
109 &mut self,
110 db: &impl DefDatabase,
111 item: T,
112 constructor: fn(T) -> LangItemTarget,
113 ) where
114 T: Into<AttrDefId> + Copy,
115 {
116 let attrs = db.attrs(item.into());
117 if let Some(lang_item_name) = attrs.by_key("lang").string_value() {
118 self.items.entry(lang_item_name.clone()).or_insert_with(|| constructor(item));
119 }
120 }
121}