diff options
Diffstat (limited to 'crates/ra_hir/src/semantics')
-rw-r--r-- | crates/ra_hir/src/semantics/source_to_def.rs | 271 |
1 files changed, 271 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..303610dc4 --- /dev/null +++ b/crates/ra_hir/src/semantics/source_to_def.rs | |||
@@ -0,0 +1,271 @@ | |||
1 | //! Maps *syntax* of various definitions to their semantic ids. | ||
2 | |||
3 | use 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 | }; | ||
11 | use hir_expand::{name::AsName, AstId, MacroDefKind}; | ||
12 | use ra_db::FileId; | ||
13 | use ra_prof::profile; | ||
14 | use ra_syntax::{ | ||
15 | ast::{self, NameOwner}, | ||
16 | match_ast, AstNode, SyntaxNode, | ||
17 | }; | ||
18 | use rustc_hash::FxHashMap; | ||
19 | |||
20 | use crate::{db::HirDatabase, InFile, MacroDefId}; | ||
21 | |||
22 | pub(super) type SourceToDefCache = FxHashMap<ChildContainer, DynMap>; | ||
23 | |||
24 | pub(super) struct SourceToDefCtx<'a, DB> { | ||
25 | pub(super) db: DB, | ||
26 | pub(super) cache: &'a mut SourceToDefCache, | ||
27 | } | ||
28 | |||
29 | impl<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 | //TODO: tuple field | ||
98 | pub(super) fn record_field_to_def( | ||
99 | &mut self, | ||
100 | src: InFile<ast::RecordFieldDef>, | ||
101 | ) -> Option<StructFieldId> { | ||
102 | self.to_def(src, keys::RECORD_FIELD) | ||
103 | } | ||
104 | pub(super) fn enum_variant_to_def( | ||
105 | &mut self, | ||
106 | src: InFile<ast::EnumVariant>, | ||
107 | ) -> Option<EnumVariantId> { | ||
108 | self.to_def(src, keys::ENUM_VARIANT) | ||
109 | } | ||
110 | pub(super) fn bind_pat_to_def( | ||
111 | &mut self, | ||
112 | src: InFile<ast::BindPat>, | ||
113 | ) -> Option<(DefWithBodyId, PatId)> { | ||
114 | let container = self.find_pat_container(src.as_ref().map(|it| it.syntax()))?; | ||
115 | let (_body, source_map) = self.db.body_with_source_map(container); | ||
116 | let src = src.map(ast::Pat::from); | ||
117 | let pat_id = source_map.node_pat(src.as_ref())?; | ||
118 | Some((container, pat_id)) | ||
119 | } | ||
120 | |||
121 | fn to_def<Ast: AstNode + 'static, ID: Copy + 'static>( | ||
122 | &mut self, | ||
123 | src: InFile<Ast>, | ||
124 | key: Key<Ast, ID>, | ||
125 | ) -> Option<ID> { | ||
126 | let container = self.find_container(src.as_ref().map(|it| it.syntax()))?; | ||
127 | let db = self.db; | ||
128 | let dyn_map = | ||
129 | &*self.cache.entry(container).or_insert_with(|| container.child_by_source(db)); | ||
130 | dyn_map[key].get(&src).copied() | ||
131 | } | ||
132 | |||
133 | pub(super) fn type_param_to_def(&mut self, src: InFile<ast::TypeParam>) -> Option<TypeParamId> { | ||
134 | let container: ChildContainer = | ||
135 | self.find_type_param_container(src.as_ref().map(|it| it.syntax()))?.into(); | ||
136 | let db = self.db; | ||
137 | let dyn_map = | ||
138 | &*self.cache.entry(container).or_insert_with(|| container.child_by_source(db)); | ||
139 | dyn_map[keys::TYPE_PARAM].get(&src).copied() | ||
140 | } | ||
141 | |||
142 | // FIXME: use DynMap as well? | ||
143 | pub(super) fn macro_call_to_def(&mut self, src: InFile<ast::MacroCall>) -> Option<MacroDefId> { | ||
144 | let kind = MacroDefKind::Declarative; | ||
145 | let file_id = src.file_id.original_file(self.db); | ||
146 | let krate = self.file_to_def(file_id)?.krate; | ||
147 | let file_ast_id = self.db.ast_id_map(src.file_id).ast_id(&src.value); | ||
148 | let ast_id = Some(AstId::new(src.file_id, file_ast_id)); | ||
149 | Some(MacroDefId { krate: Some(krate), ast_id, kind }) | ||
150 | } | ||
151 | |||
152 | pub(super) fn find_container(&mut self, src: InFile<&SyntaxNode>) -> Option<ChildContainer> { | ||
153 | for container in src.cloned().ancestors_with_macros(self.db).skip(1) { | ||
154 | let res: ChildContainer = match_ast! { | ||
155 | match (container.value) { | ||
156 | ast::Module(it) => { | ||
157 | let def = self.module_to_def(container.with_value(it))?; | ||
158 | def.into() | ||
159 | }, | ||
160 | ast::TraitDef(it) => { | ||
161 | let def = self.trait_to_def(container.with_value(it))?; | ||
162 | def.into() | ||
163 | }, | ||
164 | ast::ImplBlock(it) => { | ||
165 | let def = self.impl_to_def(container.with_value(it))?; | ||
166 | def.into() | ||
167 | }, | ||
168 | ast::FnDef(it) => { | ||
169 | let def = self.fn_to_def(container.with_value(it))?; | ||
170 | DefWithBodyId::from(def).into() | ||
171 | }, | ||
172 | ast::StructDef(it) => { | ||
173 | let def = self.struct_to_def(container.with_value(it))?; | ||
174 | VariantId::from(def).into() | ||
175 | }, | ||
176 | ast::EnumDef(it) => { | ||
177 | let def = self.enum_to_def(container.with_value(it))?; | ||
178 | def.into() | ||
179 | }, | ||
180 | ast::UnionDef(it) => { | ||
181 | let def = self.union_to_def(container.with_value(it))?; | ||
182 | VariantId::from(def).into() | ||
183 | }, | ||
184 | ast::StaticDef(it) => { | ||
185 | let def = self.static_to_def(container.with_value(it))?; | ||
186 | DefWithBodyId::from(def).into() | ||
187 | }, | ||
188 | ast::ConstDef(it) => { | ||
189 | let def = self.const_to_def(container.with_value(it))?; | ||
190 | DefWithBodyId::from(def).into() | ||
191 | }, | ||
192 | _ => continue, | ||
193 | } | ||
194 | }; | ||
195 | return Some(res); | ||
196 | } | ||
197 | |||
198 | let def = self.file_to_def(src.file_id.original_file(self.db))?; | ||
199 | Some(def.into()) | ||
200 | } | ||
201 | |||
202 | fn find_type_param_container(&mut self, src: InFile<&SyntaxNode>) -> Option<GenericDefId> { | ||
203 | for container in src.cloned().ancestors_with_macros(self.db).skip(1) { | ||
204 | let res: GenericDefId = match_ast! { | ||
205 | match (container.value) { | ||
206 | ast::FnDef(it) => { self.fn_to_def(container.with_value(it))?.into() }, | ||
207 | ast::StructDef(it) => { self.struct_to_def(container.with_value(it))?.into() }, | ||
208 | ast::EnumDef(it) => { self.enum_to_def(container.with_value(it))?.into() }, | ||
209 | ast::TraitDef(it) => { self.trait_to_def(container.with_value(it))?.into() }, | ||
210 | ast::TypeAliasDef(it) => { self.type_alias_to_def(container.with_value(it))?.into() }, | ||
211 | ast::ImplBlock(it) => { self.impl_to_def(container.with_value(it))?.into() }, | ||
212 | _ => continue, | ||
213 | } | ||
214 | }; | ||
215 | return Some(res); | ||
216 | } | ||
217 | None | ||
218 | } | ||
219 | |||
220 | fn find_pat_container(&mut self, src: InFile<&SyntaxNode>) -> Option<DefWithBodyId> { | ||
221 | for container in src.cloned().ancestors_with_macros(self.db).skip(1) { | ||
222 | let res: DefWithBodyId = match_ast! { | ||
223 | match (container.value) { | ||
224 | ast::ConstDef(it) => { self.const_to_def(container.with_value(it))?.into() }, | ||
225 | ast::StaticDef(it) => { self.static_to_def(container.with_value(it))?.into() }, | ||
226 | ast::FnDef(it) => { self.fn_to_def(container.with_value(it))?.into() }, | ||
227 | _ => continue, | ||
228 | } | ||
229 | }; | ||
230 | return Some(res); | ||
231 | } | ||
232 | None | ||
233 | } | ||
234 | } | ||
235 | |||
236 | #[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)] | ||
237 | pub(crate) enum ChildContainer { | ||
238 | DefWithBodyId(DefWithBodyId), | ||
239 | ModuleId(ModuleId), | ||
240 | TraitId(TraitId), | ||
241 | ImplId(ImplId), | ||
242 | EnumId(EnumId), | ||
243 | VariantId(VariantId), | ||
244 | /// XXX: this might be the same def as, for example an `EnumId`. However, | ||
245 | /// here the children generic parameters, and not, eg enum variants. | ||
246 | GenericDefId(GenericDefId), | ||
247 | } | ||
248 | impl_froms! { | ||
249 | ChildContainer: | ||
250 | DefWithBodyId, | ||
251 | ModuleId, | ||
252 | TraitId, | ||
253 | ImplId, | ||
254 | EnumId, | ||
255 | VariantId, | ||
256 | GenericDefId | ||
257 | } | ||
258 | |||
259 | impl ChildContainer { | ||
260 | fn child_by_source(self, db: &impl HirDatabase) -> DynMap { | ||
261 | match self { | ||
262 | ChildContainer::DefWithBodyId(it) => it.child_by_source(db), | ||
263 | ChildContainer::ModuleId(it) => it.child_by_source(db), | ||
264 | ChildContainer::TraitId(it) => it.child_by_source(db), | ||
265 | ChildContainer::ImplId(it) => it.child_by_source(db), | ||
266 | ChildContainer::EnumId(it) => it.child_by_source(db), | ||
267 | ChildContainer::VariantId(it) => it.child_by_source(db), | ||
268 | ChildContainer::GenericDefId(it) => it.child_by_source(db), | ||
269 | } | ||
270 | } | ||
271 | } | ||