aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src/semantics/source_to_def.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_hir/src/semantics/source_to_def.rs')
-rw-r--r--crates/ra_hir/src/semantics/source_to_def.rs276
1 files changed, 276 insertions, 0 deletions
diff --git a/crates/ra_hir/src/semantics/source_to_def.rs b/crates/ra_hir/src/semantics/source_to_def.rs
new file mode 100644
index 000000000..884b535b2
--- /dev/null
+++ b/crates/ra_hir/src/semantics/source_to_def.rs
@@ -0,0 +1,276 @@
1//! Maps *syntax* of various definitions to their semantic ids.
2
3use hir_def::{
4 child_by_source::ChildBySource,
5 dyn_map::DynMap,
6 expr::PatId,
7 keys::{self, Key},
8 ConstId, DefWithBodyId, EnumId, EnumVariantId, FunctionId, GenericDefId, ImplId, ModuleId,
9 StaticId, StructFieldId, StructId, TraitId, TypeAliasId, TypeParamId, UnionId, VariantId,
10};
11use hir_expand::{name::AsName, AstId, MacroDefKind};
12use ra_db::FileId;
13use ra_prof::profile;
14use ra_syntax::{
15 ast::{self, NameOwner},
16 match_ast, AstNode, SyntaxNode,
17};
18use rustc_hash::FxHashMap;
19
20use crate::{db::HirDatabase, InFile, MacroDefId};
21
22pub(super) type SourceToDefCache = FxHashMap<ChildContainer, DynMap>;
23
24pub(super) struct SourceToDefCtx<'a, DB> {
25 pub(super) db: DB,
26 pub(super) cache: &'a mut SourceToDefCache,
27}
28
29impl<DB: HirDatabase> SourceToDefCtx<'_, &'_ DB> {
30 pub(super) fn file_to_def(&mut self, file: FileId) -> Option<ModuleId> {
31 let _p = profile("SourceBinder::to_module_def");
32 let (krate, local_id) = self.db.relevant_crates(file).iter().find_map(|&crate_id| {
33 let crate_def_map = self.db.crate_def_map(crate_id);
34 let local_id = crate_def_map.modules_for_file(file).next()?;
35 Some((crate_id, local_id))
36 })?;
37 Some(ModuleId { krate, local_id })
38 }
39
40 pub(super) fn module_to_def(&mut self, src: InFile<ast::Module>) -> Option<ModuleId> {
41 let _p = profile("module_to_def");
42 let parent_declaration = src
43 .as_ref()
44 .map(|it| it.syntax())
45 .cloned()
46 .ancestors_with_macros(self.db)
47 .skip(1)
48 .find_map(|it| {
49 let m = ast::Module::cast(it.value.clone())?;
50 Some(it.with_value(m))
51 });
52
53 let parent_module = match parent_declaration {
54 Some(parent_declaration) => self.module_to_def(parent_declaration),
55 None => {
56 let file_id = src.file_id.original_file(self.db);
57 self.file_to_def(file_id)
58 }
59 }?;
60
61 let child_name = src.value.name()?.as_name();
62 let def_map = self.db.crate_def_map(parent_module.krate);
63 let child_id = *def_map[parent_module.local_id].children.get(&child_name)?;
64 Some(ModuleId { krate: parent_module.krate, local_id: child_id })
65 }
66
67 pub(super) fn trait_to_def(&mut self, src: InFile<ast::TraitDef>) -> Option<TraitId> {
68 self.to_def(src, keys::TRAIT)
69 }
70 pub(super) fn impl_to_def(&mut self, src: InFile<ast::ImplBlock>) -> Option<ImplId> {
71 self.to_def(src, keys::IMPL)
72 }
73 pub(super) fn fn_to_def(&mut self, src: InFile<ast::FnDef>) -> Option<FunctionId> {
74 self.to_def(src, keys::FUNCTION)
75 }
76 pub(super) fn struct_to_def(&mut self, src: InFile<ast::StructDef>) -> Option<StructId> {
77 self.to_def(src, keys::STRUCT)
78 }
79 pub(super) fn enum_to_def(&mut self, src: InFile<ast::EnumDef>) -> Option<EnumId> {
80 self.to_def(src, keys::ENUM)
81 }
82 pub(super) fn union_to_def(&mut self, src: InFile<ast::UnionDef>) -> Option<UnionId> {
83 self.to_def(src, keys::UNION)
84 }
85 pub(super) fn static_to_def(&mut self, src: InFile<ast::StaticDef>) -> Option<StaticId> {
86 self.to_def(src, keys::STATIC)
87 }
88 pub(super) fn const_to_def(&mut self, src: InFile<ast::ConstDef>) -> Option<ConstId> {
89 self.to_def(src, keys::CONST)
90 }
91 pub(super) fn type_alias_to_def(
92 &mut self,
93 src: InFile<ast::TypeAliasDef>,
94 ) -> Option<TypeAliasId> {
95 self.to_def(src, keys::TYPE_ALIAS)
96 }
97 pub(super) fn record_field_to_def(
98 &mut self,
99 src: InFile<ast::RecordFieldDef>,
100 ) -> Option<StructFieldId> {
101 self.to_def(src, keys::RECORD_FIELD)
102 }
103 pub(super) fn tuple_field_to_def(
104 &mut self,
105 src: InFile<ast::TupleFieldDef>,
106 ) -> Option<StructFieldId> {
107 self.to_def(src, keys::TUPLE_FIELD)
108 }
109 pub(super) fn enum_variant_to_def(
110 &mut self,
111 src: InFile<ast::EnumVariant>,
112 ) -> Option<EnumVariantId> {
113 self.to_def(src, keys::ENUM_VARIANT)
114 }
115 pub(super) fn bind_pat_to_def(
116 &mut self,
117 src: InFile<ast::BindPat>,
118 ) -> Option<(DefWithBodyId, PatId)> {
119 let container = self.find_pat_container(src.as_ref().map(|it| it.syntax()))?;
120 let (_body, source_map) = self.db.body_with_source_map(container);
121 let src = src.map(ast::Pat::from);
122 let pat_id = source_map.node_pat(src.as_ref())?;
123 Some((container, pat_id))
124 }
125
126 fn to_def<Ast: AstNode + 'static, ID: Copy + 'static>(
127 &mut self,
128 src: InFile<Ast>,
129 key: Key<Ast, ID>,
130 ) -> Option<ID> {
131 let container = self.find_container(src.as_ref().map(|it| it.syntax()))?;
132 let db = self.db;
133 let dyn_map =
134 &*self.cache.entry(container).or_insert_with(|| container.child_by_source(db));
135 dyn_map[key].get(&src).copied()
136 }
137
138 pub(super) fn type_param_to_def(&mut self, src: InFile<ast::TypeParam>) -> Option<TypeParamId> {
139 let container: ChildContainer =
140 self.find_type_param_container(src.as_ref().map(|it| it.syntax()))?.into();
141 let db = self.db;
142 let dyn_map =
143 &*self.cache.entry(container).or_insert_with(|| container.child_by_source(db));
144 dyn_map[keys::TYPE_PARAM].get(&src).copied()
145 }
146
147 // FIXME: use DynMap as well?
148 pub(super) fn macro_call_to_def(&mut self, src: InFile<ast::MacroCall>) -> Option<MacroDefId> {
149 let kind = MacroDefKind::Declarative;
150 let file_id = src.file_id.original_file(self.db);
151 let krate = self.file_to_def(file_id)?.krate;
152 let file_ast_id = self.db.ast_id_map(src.file_id).ast_id(&src.value);
153 let ast_id = Some(AstId::new(src.file_id, file_ast_id));
154 Some(MacroDefId { krate: Some(krate), ast_id, kind })
155 }
156
157 pub(super) fn find_container(&mut self, src: InFile<&SyntaxNode>) -> Option<ChildContainer> {
158 for container in src.cloned().ancestors_with_macros(self.db).skip(1) {
159 let res: ChildContainer = match_ast! {
160 match (container.value) {
161 ast::Module(it) => {
162 let def = self.module_to_def(container.with_value(it))?;
163 def.into()
164 },
165 ast::TraitDef(it) => {
166 let def = self.trait_to_def(container.with_value(it))?;
167 def.into()
168 },
169 ast::ImplBlock(it) => {
170 let def = self.impl_to_def(container.with_value(it))?;
171 def.into()
172 },
173 ast::FnDef(it) => {
174 let def = self.fn_to_def(container.with_value(it))?;
175 DefWithBodyId::from(def).into()
176 },
177 ast::StructDef(it) => {
178 let def = self.struct_to_def(container.with_value(it))?;
179 VariantId::from(def).into()
180 },
181 ast::EnumDef(it) => {
182 let def = self.enum_to_def(container.with_value(it))?;
183 def.into()
184 },
185 ast::UnionDef(it) => {
186 let def = self.union_to_def(container.with_value(it))?;
187 VariantId::from(def).into()
188 },
189 ast::StaticDef(it) => {
190 let def = self.static_to_def(container.with_value(it))?;
191 DefWithBodyId::from(def).into()
192 },
193 ast::ConstDef(it) => {
194 let def = self.const_to_def(container.with_value(it))?;
195 DefWithBodyId::from(def).into()
196 },
197 _ => continue,
198 }
199 };
200 return Some(res);
201 }
202
203 let def = self.file_to_def(src.file_id.original_file(self.db))?;
204 Some(def.into())
205 }
206
207 fn find_type_param_container(&mut self, src: InFile<&SyntaxNode>) -> Option<GenericDefId> {
208 for container in src.cloned().ancestors_with_macros(self.db).skip(1) {
209 let res: GenericDefId = match_ast! {
210 match (container.value) {
211 ast::FnDef(it) => { self.fn_to_def(container.with_value(it))?.into() },
212 ast::StructDef(it) => { self.struct_to_def(container.with_value(it))?.into() },
213 ast::EnumDef(it) => { self.enum_to_def(container.with_value(it))?.into() },
214 ast::TraitDef(it) => { self.trait_to_def(container.with_value(it))?.into() },
215 ast::TypeAliasDef(it) => { self.type_alias_to_def(container.with_value(it))?.into() },
216 ast::ImplBlock(it) => { self.impl_to_def(container.with_value(it))?.into() },
217 _ => continue,
218 }
219 };
220 return Some(res);
221 }
222 None
223 }
224
225 fn find_pat_container(&mut self, src: InFile<&SyntaxNode>) -> Option<DefWithBodyId> {
226 for container in src.cloned().ancestors_with_macros(self.db).skip(1) {
227 let res: DefWithBodyId = match_ast! {
228 match (container.value) {
229 ast::ConstDef(it) => { self.const_to_def(container.with_value(it))?.into() },
230 ast::StaticDef(it) => { self.static_to_def(container.with_value(it))?.into() },
231 ast::FnDef(it) => { self.fn_to_def(container.with_value(it))?.into() },
232 _ => continue,
233 }
234 };
235 return Some(res);
236 }
237 None
238 }
239}
240
241#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
242pub(crate) enum ChildContainer {
243 DefWithBodyId(DefWithBodyId),
244 ModuleId(ModuleId),
245 TraitId(TraitId),
246 ImplId(ImplId),
247 EnumId(EnumId),
248 VariantId(VariantId),
249 /// XXX: this might be the same def as, for example an `EnumId`. However,
250 /// here the children generic parameters, and not, eg enum variants.
251 GenericDefId(GenericDefId),
252}
253impl_froms! {
254 ChildContainer:
255 DefWithBodyId,
256 ModuleId,
257 TraitId,
258 ImplId,
259 EnumId,
260 VariantId,
261 GenericDefId
262}
263
264impl ChildContainer {
265 fn child_by_source(self, db: &impl HirDatabase) -> DynMap {
266 match self {
267 ChildContainer::DefWithBodyId(it) => it.child_by_source(db),
268 ChildContainer::ModuleId(it) => it.child_by_source(db),
269 ChildContainer::TraitId(it) => it.child_by_source(db),
270 ChildContainer::ImplId(it) => it.child_by_source(db),
271 ChildContainer::EnumId(it) => it.child_by_source(db),
272 ChildContainer::VariantId(it) => it.child_by_source(db),
273 ChildContainer::GenericDefId(it) => it.child_by_source(db),
274 }
275 }
276}