diff options
author | Zac Pullar-Strecker <[email protected]> | 2020-08-24 10:19:53 +0100 |
---|---|---|
committer | Zac Pullar-Strecker <[email protected]> | 2020-08-24 10:20:13 +0100 |
commit | 7bbca7a1b3f9293d2f5cc5745199bc5f8396f2f0 (patch) | |
tree | bdb47765991cb973b2cd5481a088fac636bd326c /crates/hir_def/src/lang_item.rs | |
parent | ca464650eeaca6195891199a93f4f76cf3e7e697 (diff) | |
parent | e65d48d1fb3d4d91d9dc1148a7a836ff5c9a3c87 (diff) |
Merge remote-tracking branch 'upstream/master' into 503-hover-doc-links
Diffstat (limited to 'crates/hir_def/src/lang_item.rs')
-rw-r--r-- | crates/hir_def/src/lang_item.rs | 174 |
1 files changed, 174 insertions, 0 deletions
diff --git a/crates/hir_def/src/lang_item.rs b/crates/hir_def/src/lang_item.rs new file mode 100644 index 000000000..063eadccb --- /dev/null +++ b/crates/hir_def/src/lang_item.rs | |||
@@ -0,0 +1,174 @@ | |||
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. | ||
5 | use std::sync::Arc; | ||
6 | |||
7 | use rustc_hash::FxHashMap; | ||
8 | use syntax::SmolStr; | ||
9 | |||
10 | use 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)] | ||
16 | pub enum LangItemTarget { | ||
17 | EnumId(EnumId), | ||
18 | FunctionId(FunctionId), | ||
19 | ImplDefId(ImplId), | ||
20 | StaticId(StaticId), | ||
21 | StructId(StructId), | ||
22 | TraitId(TraitId), | ||
23 | } | ||
24 | |||
25 | impl LangItemTarget { | ||
26 | pub fn as_enum(self) -> Option<EnumId> { | ||
27 | match self { | ||
28 | LangItemTarget::EnumId(id) => Some(id), | ||
29 | _ => None, | ||
30 | } | ||
31 | } | ||
32 | |||
33 | pub fn as_function(self) -> Option<FunctionId> { | ||
34 | match self { | ||
35 | LangItemTarget::FunctionId(id) => Some(id), | ||
36 | _ => None, | ||
37 | } | ||
38 | } | ||
39 | |||
40 | pub fn as_impl_def(self) -> Option<ImplId> { | ||
41 | match self { | ||
42 | LangItemTarget::ImplDefId(id) => Some(id), | ||
43 | _ => None, | ||
44 | } | ||
45 | } | ||
46 | |||
47 | pub fn as_static(self) -> Option<StaticId> { | ||
48 | match self { | ||
49 | LangItemTarget::StaticId(id) => Some(id), | ||
50 | _ => None, | ||
51 | } | ||
52 | } | ||
53 | |||
54 | pub fn as_struct(self) -> Option<StructId> { | ||
55 | match self { | ||
56 | LangItemTarget::StructId(id) => Some(id), | ||
57 | _ => None, | ||
58 | } | ||
59 | } | ||
60 | |||
61 | pub fn as_trait(self) -> Option<TraitId> { | ||
62 | match self { | ||
63 | LangItemTarget::TraitId(id) => Some(id), | ||
64 | _ => None, | ||
65 | } | ||
66 | } | ||
67 | } | ||
68 | |||
69 | #[derive(Default, Debug, Clone, PartialEq, Eq)] | ||
70 | pub struct LangItems { | ||
71 | items: FxHashMap<SmolStr, LangItemTarget>, | ||
72 | } | ||
73 | |||
74 | impl LangItems { | ||
75 | pub fn target(&self, item: &str) -> Option<LangItemTarget> { | ||
76 | self.items.get(item).copied() | ||
77 | } | ||
78 | |||
79 | /// Salsa query. This will look for lang items in a specific crate. | ||
80 | pub(crate) fn crate_lang_items_query(db: &dyn DefDatabase, krate: CrateId) -> Arc<LangItems> { | ||
81 | let _p = profile::span("crate_lang_items_query"); | ||
82 | |||
83 | let mut lang_items = LangItems::default(); | ||
84 | |||
85 | let crate_def_map = db.crate_def_map(krate); | ||
86 | |||
87 | crate_def_map | ||
88 | .modules | ||
89 | .iter() | ||
90 | .filter_map(|(local_id, _)| db.module_lang_items(ModuleId { krate, local_id })) | ||
91 | .for_each(|it| lang_items.items.extend(it.items.iter().map(|(k, v)| (k.clone(), *v)))); | ||
92 | |||
93 | Arc::new(lang_items) | ||
94 | } | ||
95 | |||
96 | pub(crate) fn module_lang_items_query( | ||
97 | db: &dyn DefDatabase, | ||
98 | module: ModuleId, | ||
99 | ) -> Option<Arc<LangItems>> { | ||
100 | let _p = profile::span("module_lang_items_query"); | ||
101 | let mut lang_items = LangItems::default(); | ||
102 | lang_items.collect_lang_items(db, module); | ||
103 | if lang_items.items.is_empty() { | ||
104 | None | ||
105 | } else { | ||
106 | Some(Arc::new(lang_items)) | ||
107 | } | ||
108 | } | ||
109 | |||
110 | /// Salsa query. Look for a lang item, starting from the specified crate and recursively | ||
111 | /// traversing its dependencies. | ||
112 | pub(crate) fn lang_item_query( | ||
113 | db: &dyn DefDatabase, | ||
114 | start_crate: CrateId, | ||
115 | item: SmolStr, | ||
116 | ) -> Option<LangItemTarget> { | ||
117 | let _p = profile::span("lang_item_query"); | ||
118 | let lang_items = db.crate_lang_items(start_crate); | ||
119 | let start_crate_target = lang_items.items.get(&item); | ||
120 | if let Some(target) = start_crate_target { | ||
121 | return Some(*target); | ||
122 | } | ||
123 | db.crate_graph()[start_crate] | ||
124 | .dependencies | ||
125 | .iter() | ||
126 | .find_map(|dep| db.lang_item(dep.crate_id, item.clone())) | ||
127 | } | ||
128 | |||
129 | fn collect_lang_items(&mut self, db: &dyn DefDatabase, module: ModuleId) { | ||
130 | // Look for impl targets | ||
131 | let def_map = db.crate_def_map(module.krate); | ||
132 | let module_data = &def_map[module.local_id]; | ||
133 | for impl_def in module_data.scope.impls() { | ||
134 | self.collect_lang_item(db, impl_def, LangItemTarget::ImplDefId) | ||
135 | } | ||
136 | |||
137 | for def in module_data.scope.declarations() { | ||
138 | match def { | ||
139 | ModuleDefId::TraitId(trait_) => { | ||
140 | self.collect_lang_item(db, trait_, LangItemTarget::TraitId) | ||
141 | } | ||
142 | ModuleDefId::AdtId(AdtId::EnumId(e)) => { | ||
143 | self.collect_lang_item(db, e, LangItemTarget::EnumId) | ||
144 | } | ||
145 | ModuleDefId::AdtId(AdtId::StructId(s)) => { | ||
146 | self.collect_lang_item(db, s, LangItemTarget::StructId) | ||
147 | } | ||
148 | ModuleDefId::FunctionId(f) => { | ||
149 | self.collect_lang_item(db, f, LangItemTarget::FunctionId) | ||
150 | } | ||
151 | ModuleDefId::StaticId(s) => self.collect_lang_item(db, s, LangItemTarget::StaticId), | ||
152 | _ => {} | ||
153 | } | ||
154 | } | ||
155 | } | ||
156 | |||
157 | fn collect_lang_item<T>( | ||
158 | &mut self, | ||
159 | db: &dyn DefDatabase, | ||
160 | item: T, | ||
161 | constructor: fn(T) -> LangItemTarget, | ||
162 | ) where | ||
163 | T: Into<AttrDefId> + Copy, | ||
164 | { | ||
165 | if let Some(lang_item_name) = lang_attr(db, item) { | ||
166 | self.items.entry(lang_item_name).or_insert_with(|| constructor(item)); | ||
167 | } | ||
168 | } | ||
169 | } | ||
170 | |||
171 | pub fn lang_attr(db: &dyn DefDatabase, item: impl Into<AttrDefId> + Copy) -> Option<SmolStr> { | ||
172 | let attrs = db.attrs(item.into()); | ||
173 | attrs.by_key("lang").string_value().cloned() | ||
174 | } | ||