]> git.lizzy.rs Git - rust.git/blob - crates/hir/src/semantics/source_to_def.rs
Merge #10902
[rust.git] / crates / hir / src / semantics / source_to_def.rs
1 //! Maps *syntax* of various definitions to their semantic ids.
2 //!
3 //! This is a very interesting module, and, in some sense, can be considered the
4 //! heart of the IDE parts of rust-analyzer.
5 //!
6 //! This module solves the following problem:
7 //!
8 //!     Given a piece of syntax, find the corresponding semantic definition (def).
9 //!
10 //! This problem is a part of more-or-less every IDE feature implemented. Every
11 //! IDE functionality (like goto to definition), conceptually starts with a
12 //! specific cursor position in a file. Starting with this text offset, we first
13 //! figure out what syntactic construct are we at: is this a pattern, an
14 //! expression, an item definition.
15 //!
16 //! Knowing only the syntax gives us relatively little info. For example,
17 //! looking at the syntax of the function we can realise that it is a part of an
18 //! `impl` block, but we won't be able to tell what trait function the current
19 //! function overrides, and whether it does that correctly. For that, we need to
20 //! go from [`ast::Fn`] to [`crate::Function`], and that's exactly what this
21 //! module does.
22 //!
23 //! As syntax trees are values and don't know their place of origin/identity,
24 //! this module also requires [`InFile`] wrappers to understand which specific
25 //! real or macro-expanded file the tree comes from.
26 //!
27 //! The actual algorithm to resolve syntax to def is curious in two aspects:
28 //!
29 //!     * It is recursive
30 //!     * It uses the inverse algorithm (what is the syntax for this def?)
31 //!
32 //! Specifically, the algorithm goes like this:
33 //!
34 //!     1. Find the syntactic container for the syntax. For example, field's
35 //!        container is the struct, and structs container is a module.
36 //!     2. Recursively get the def corresponding to container.
37 //!     3. Ask the container def for all child defs. These child defs contain
38 //!        the answer and answer's siblings.
39 //!     4. For each child def, ask for it's source.
40 //!     5. The child def whose source is the syntax node we've started with
41 //!        is the answer.
42 //!
43 //! It's interesting that both Roslyn and Kotlin contain very similar code
44 //! shape.
45 //!
46 //! Let's take a look at Roslyn:
47 //!
48 //!   <https://github.com/dotnet/roslyn/blob/36a0c338d6621cc5fe34b79d414074a95a6a489c/src/Compilers/CSharp/Portable/Compilation/SyntaxTreeSemanticModel.cs#L1403-L1429>
49 //!   <https://sourceroslyn.io/#Microsoft.CodeAnalysis.CSharp/Compilation/SyntaxTreeSemanticModel.cs,1403>
50 //!
51 //! The `GetDeclaredType` takes `Syntax` as input, and returns `Symbol` as
52 //! output. First, it retrieves a `Symbol` for parent `Syntax`:
53 //!
54 //! * <https://sourceroslyn.io/#Microsoft.CodeAnalysis.CSharp/Compilation/SyntaxTreeSemanticModel.cs,1423>
55 //!
56 //! Then, it iterates parent symbol's children, looking for one which has the
57 //! same text span as the original node:
58 //!
59 //!   <https://sourceroslyn.io/#Microsoft.CodeAnalysis.CSharp/Compilation/SyntaxTreeSemanticModel.cs,1786>
60 //!
61 //! Now, let's look at Kotlin:
62 //!
63 //!   <https://github.com/JetBrains/kotlin/blob/a288b8b00e4754a1872b164999c6d3f3b8c8994a/idea/idea-frontend-fir/idea-fir-low-level-api/src/org/jetbrains/kotlin/idea/fir/low/level/api/FirModuleResolveStateImpl.kt#L93-L125>
64 //!
65 //! This function starts with a syntax node (`KtExpression` is syntax, like all
66 //! `Kt` nodes), and returns a def. It uses
67 //! `getNonLocalContainingOrThisDeclaration` to get syntactic container for a
68 //! current node. Then, `findSourceNonLocalFirDeclaration` gets `Fir` for this
69 //! parent. Finally, `findElementIn` function traverses `Fir` children to find
70 //! one with the same source we originally started with.
71 //!
72 //! One question is left though -- where does the recursion stops? This happens
73 //! when we get to the file syntax node, which doesn't have a syntactic parent.
74 //! In that case, we loop through all the crates that might contain this file
75 //! and look for a module whose source is the given file.
76 //!
77 //! Note that the logic in this module is somewhat fundamentally imprecise --
78 //! due to conditional compilation and `#[path]` attributes, there's no
79 //! injective mapping from syntax nodes to defs. This is not an edge case --
80 //! more or less every item in a `lib.rs` is a part of two distinct crates: a
81 //! library with `--cfg test` and a library without.
82 //!
83 //! At the moment, we don't really handle this well and return the first answer
84 //! that works. Ideally, we should first let the caller to pick a specific
85 //! active crate for a given position, and then provide an API to resolve all
86 //! syntax nodes against this specific crate.
87
88 use base_db::FileId;
89 use hir_def::{
90     child_by_source::ChildBySource,
91     dyn_map::DynMap,
92     expr::{LabelId, PatId},
93     keys::{self, Key},
94     AdtId, ConstId, ConstParamId, DefWithBodyId, EnumId, EnumVariantId, FieldId, FunctionId,
95     GenericDefId, ImplId, LifetimeParamId, ModuleId, StaticId, StructId, TraitId, TypeAliasId,
96     TypeParamId, UnionId, VariantId,
97 };
98 use hir_expand::{name::AsName, AstId, HirFileId, MacroCallId, MacroDefId, MacroDefKind};
99 use rustc_hash::FxHashMap;
100 use smallvec::SmallVec;
101 use stdx::impl_from;
102 use syntax::{
103     ast::{self, HasName},
104     match_ast, AstNode, SyntaxNode,
105 };
106
107 use crate::{db::HirDatabase, InFile};
108
109 pub(super) type SourceToDefCache = FxHashMap<(ChildContainer, HirFileId), DynMap>;
110
111 pub(super) struct SourceToDefCtx<'a, 'b> {
112     pub(super) db: &'b dyn HirDatabase,
113     pub(super) cache: &'a mut SourceToDefCache,
114 }
115
116 impl SourceToDefCtx<'_, '_> {
117     pub(super) fn file_to_def(&mut self, file: FileId) -> SmallVec<[ModuleId; 1]> {
118         let _p = profile::span("SourceBinder::to_module_def");
119         let mut mods = SmallVec::new();
120         for &crate_id in self.db.relevant_crates(file).iter() {
121             // FIXME: inner items
122             let crate_def_map = self.db.crate_def_map(crate_id);
123             mods.extend(
124                 crate_def_map
125                     .modules_for_file(file)
126                     .map(|local_id| crate_def_map.module_id(local_id)),
127             )
128         }
129         mods
130     }
131
132     pub(super) fn module_to_def(&mut self, src: InFile<ast::Module>) -> Option<ModuleId> {
133         let _p = profile::span("module_to_def");
134         let parent_declaration = src
135             .syntax()
136             .cloned()
137             .ancestors_with_macros_skip_attr_item(self.db.upcast())
138             .skip(1)
139             .find_map(|it| {
140                 let m = ast::Module::cast(it.value.clone())?;
141                 Some(it.with_value(m))
142             });
143
144         let parent_module = match parent_declaration {
145             Some(parent_declaration) => self.module_to_def(parent_declaration),
146             None => {
147                 let file_id = src.file_id.original_file(self.db.upcast());
148                 self.file_to_def(file_id).get(0).copied()
149             }
150         }?;
151
152         let child_name = src.value.name()?.as_name();
153         let def_map = parent_module.def_map(self.db.upcast());
154         let child_id = *def_map[parent_module.local_id].children.get(&child_name)?;
155         Some(def_map.module_id(child_id))
156     }
157
158     pub(super) fn source_file_to_def(&mut self, src: InFile<ast::SourceFile>) -> Option<ModuleId> {
159         let _p = profile::span("source_file_to_def");
160         let file_id = src.file_id.original_file(self.db.upcast());
161         self.file_to_def(file_id).get(0).copied()
162     }
163
164     pub(super) fn trait_to_def(&mut self, src: InFile<ast::Trait>) -> Option<TraitId> {
165         self.to_def(src, keys::TRAIT)
166     }
167     pub(super) fn impl_to_def(&mut self, src: InFile<ast::Impl>) -> Option<ImplId> {
168         self.to_def(src, keys::IMPL)
169     }
170     pub(super) fn fn_to_def(&mut self, src: InFile<ast::Fn>) -> Option<FunctionId> {
171         self.to_def(src, keys::FUNCTION)
172     }
173     pub(super) fn struct_to_def(&mut self, src: InFile<ast::Struct>) -> Option<StructId> {
174         self.to_def(src, keys::STRUCT)
175     }
176     pub(super) fn enum_to_def(&mut self, src: InFile<ast::Enum>) -> Option<EnumId> {
177         self.to_def(src, keys::ENUM)
178     }
179     pub(super) fn union_to_def(&mut self, src: InFile<ast::Union>) -> Option<UnionId> {
180         self.to_def(src, keys::UNION)
181     }
182     pub(super) fn static_to_def(&mut self, src: InFile<ast::Static>) -> Option<StaticId> {
183         self.to_def(src, keys::STATIC)
184     }
185     pub(super) fn const_to_def(&mut self, src: InFile<ast::Const>) -> Option<ConstId> {
186         self.to_def(src, keys::CONST)
187     }
188     pub(super) fn type_alias_to_def(&mut self, src: InFile<ast::TypeAlias>) -> Option<TypeAliasId> {
189         self.to_def(src, keys::TYPE_ALIAS)
190     }
191     pub(super) fn record_field_to_def(&mut self, src: InFile<ast::RecordField>) -> Option<FieldId> {
192         self.to_def(src, keys::RECORD_FIELD)
193     }
194     pub(super) fn tuple_field_to_def(&mut self, src: InFile<ast::TupleField>) -> Option<FieldId> {
195         self.to_def(src, keys::TUPLE_FIELD)
196     }
197     pub(super) fn enum_variant_to_def(
198         &mut self,
199         src: InFile<ast::Variant>,
200     ) -> Option<EnumVariantId> {
201         self.to_def(src, keys::VARIANT)
202     }
203     pub(super) fn adt_to_def(
204         &mut self,
205         InFile { file_id, value }: InFile<ast::Adt>,
206     ) -> Option<AdtId> {
207         match value {
208             ast::Adt::Enum(it) => self.enum_to_def(InFile::new(file_id, it)).map(AdtId::EnumId),
209             ast::Adt::Struct(it) => {
210                 self.struct_to_def(InFile::new(file_id, it)).map(AdtId::StructId)
211             }
212             ast::Adt::Union(it) => self.union_to_def(InFile::new(file_id, it)).map(AdtId::UnionId),
213         }
214     }
215     pub(super) fn bind_pat_to_def(
216         &mut self,
217         src: InFile<ast::IdentPat>,
218     ) -> Option<(DefWithBodyId, PatId)> {
219         let container = self.find_pat_or_label_container(src.syntax())?;
220         let (_body, source_map) = self.db.body_with_source_map(container);
221         let src = src.map(ast::Pat::from);
222         let pat_id = source_map.node_pat(src.as_ref())?;
223         Some((container, pat_id))
224     }
225     pub(super) fn self_param_to_def(
226         &mut self,
227         src: InFile<ast::SelfParam>,
228     ) -> Option<(DefWithBodyId, PatId)> {
229         let container = self.find_pat_or_label_container(src.syntax())?;
230         let (_body, source_map) = self.db.body_with_source_map(container);
231         let pat_id = source_map.node_self_param(src.as_ref())?;
232         Some((container, pat_id))
233     }
234     pub(super) fn label_to_def(
235         &mut self,
236         src: InFile<ast::Label>,
237     ) -> Option<(DefWithBodyId, LabelId)> {
238         let container = self.find_pat_or_label_container(src.syntax())?;
239         let (_body, source_map) = self.db.body_with_source_map(container);
240         let label_id = source_map.node_label(src.as_ref())?;
241         Some((container, label_id))
242     }
243
244     pub(super) fn item_to_macro_call(&mut self, src: InFile<ast::Item>) -> Option<MacroCallId> {
245         let map = self.dyn_map(src.as_ref())?;
246         map[keys::ATTR_MACRO].get(&src).copied()
247     }
248
249     pub(super) fn attr_to_derive_macro_call(
250         &mut self,
251         item: InFile<&ast::Item>,
252         src: InFile<ast::Attr>,
253     ) -> Option<&[MacroCallId]> {
254         let map = self.dyn_map(item)?;
255         map[keys::DERIVE_MACRO].get(&src).map(AsRef::as_ref)
256     }
257
258     fn to_def<Ast: AstNode + 'static, ID: Copy + 'static>(
259         &mut self,
260         src: InFile<Ast>,
261         key: Key<Ast, ID>,
262     ) -> Option<ID> {
263         self.dyn_map(src.as_ref())?[key].get(&src).copied()
264     }
265
266     fn dyn_map<Ast: AstNode + 'static>(&mut self, src: InFile<&Ast>) -> Option<&DynMap> {
267         let container = self.find_container(src.map(|it| it.syntax()))?;
268         Some(self.cache_for(container, src.file_id))
269     }
270
271     fn cache_for(&mut self, container: ChildContainer, file_id: HirFileId) -> &DynMap {
272         let db = self.db;
273         self.cache
274             .entry((container, file_id))
275             .or_insert_with(|| container.child_by_source(db, file_id))
276     }
277
278     pub(super) fn type_param_to_def(&mut self, src: InFile<ast::TypeParam>) -> Option<TypeParamId> {
279         let container: ChildContainer = self.find_generic_param_container(src.syntax())?.into();
280         let dyn_map = self.cache_for(container, src.file_id);
281         dyn_map[keys::TYPE_PARAM].get(&src).copied()
282     }
283
284     pub(super) fn lifetime_param_to_def(
285         &mut self,
286         src: InFile<ast::LifetimeParam>,
287     ) -> Option<LifetimeParamId> {
288         let container: ChildContainer = self.find_generic_param_container(src.syntax())?.into();
289         let dyn_map = self.cache_for(container, src.file_id);
290         dyn_map[keys::LIFETIME_PARAM].get(&src).copied()
291     }
292
293     pub(super) fn const_param_to_def(
294         &mut self,
295         src: InFile<ast::ConstParam>,
296     ) -> Option<ConstParamId> {
297         let container: ChildContainer = self.find_generic_param_container(src.syntax())?.into();
298         let dyn_map = self.cache_for(container, src.file_id);
299         dyn_map[keys::CONST_PARAM].get(&src).copied()
300     }
301
302     pub(super) fn macro_to_def(&mut self, src: InFile<ast::Macro>) -> Option<MacroDefId> {
303         let makro = self.dyn_map(src.as_ref()).and_then(|it| it[keys::MACRO].get(&src).copied());
304         if let res @ Some(_) = makro {
305             return res;
306         }
307
308         // Not all macros are recorded in the dyn map, only the ones behaving like items, so fall back
309         // for the non-item like definitions.
310         let file_ast_id = self.db.ast_id_map(src.file_id).ast_id(&src.value);
311         let ast_id = AstId::new(src.file_id, file_ast_id.upcast());
312         let kind = MacroDefKind::Declarative(ast_id);
313         let file_id = src.file_id.original_file(self.db.upcast());
314         let krate = self.file_to_def(file_id).get(0).copied()?.krate();
315         Some(MacroDefId { krate, kind, local_inner: false })
316     }
317
318     pub(super) fn find_container(&mut self, src: InFile<&SyntaxNode>) -> Option<ChildContainer> {
319         for container in src.cloned().ancestors_with_macros_skip_attr_item(self.db.upcast()).skip(1)
320         {
321             if let Some(res) = self.container_to_def(container) {
322                 return Some(res);
323             }
324         }
325
326         let def = self.file_to_def(src.file_id.original_file(self.db.upcast())).get(0).copied()?;
327         Some(def.into())
328     }
329
330     fn container_to_def(&mut self, container: InFile<SyntaxNode>) -> Option<ChildContainer> {
331         let cont = match_ast! {
332             match (container.value) {
333                 ast::Module(it) => {
334                     let def = self.module_to_def(container.with_value(it))?;
335                     def.into()
336                 },
337                 ast::Trait(it) => {
338                     let def = self.trait_to_def(container.with_value(it))?;
339                     def.into()
340                 },
341                 ast::Impl(it) => {
342                     let def = self.impl_to_def(container.with_value(it))?;
343                     def.into()
344                 },
345                 ast::Fn(it) => {
346                     let def = self.fn_to_def(container.with_value(it))?;
347                     DefWithBodyId::from(def).into()
348                 },
349                 ast::Struct(it) => {
350                     let def = self.struct_to_def(container.with_value(it))?;
351                     VariantId::from(def).into()
352                 },
353                 ast::Enum(it) => {
354                     let def = self.enum_to_def(container.with_value(it))?;
355                     def.into()
356                 },
357                 ast::Union(it) => {
358                     let def = self.union_to_def(container.with_value(it))?;
359                     VariantId::from(def).into()
360                 },
361                 ast::Static(it) => {
362                     let def = self.static_to_def(container.with_value(it))?;
363                     DefWithBodyId::from(def).into()
364                 },
365                 ast::Const(it) => {
366                     let def = self.const_to_def(container.with_value(it))?;
367                     DefWithBodyId::from(def).into()
368                 },
369                 ast::TypeAlias(it) => {
370                     let def = self.type_alias_to_def(container.with_value(it))?;
371                     def.into()
372                 },
373                 ast::Variant(it) => {
374                     let def = self.enum_variant_to_def(container.with_value(it))?;
375                     VariantId::from(def).into()
376                 },
377                 _ => return None,
378             }
379         };
380         Some(cont)
381     }
382
383     fn find_generic_param_container(&mut self, src: InFile<&SyntaxNode>) -> Option<GenericDefId> {
384         for container in src.cloned().ancestors_with_macros_skip_attr_item(self.db.upcast()).skip(1)
385         {
386             let res: GenericDefId = match_ast! {
387                 match (container.value) {
388                     ast::Fn(it) => self.fn_to_def(container.with_value(it))?.into(),
389                     ast::Struct(it) => self.struct_to_def(container.with_value(it))?.into(),
390                     ast::Enum(it) => self.enum_to_def(container.with_value(it))?.into(),
391                     ast::Trait(it) => self.trait_to_def(container.with_value(it))?.into(),
392                     ast::TypeAlias(it) => self.type_alias_to_def(container.with_value(it))?.into(),
393                     ast::Impl(it) => self.impl_to_def(container.with_value(it))?.into(),
394                     _ => continue,
395                 }
396             };
397             return Some(res);
398         }
399         None
400     }
401
402     fn find_pat_or_label_container(&mut self, src: InFile<&SyntaxNode>) -> Option<DefWithBodyId> {
403         for container in src.cloned().ancestors_with_macros_skip_attr_item(self.db.upcast()).skip(1)
404         {
405             let res: DefWithBodyId = match_ast! {
406                 match (container.value) {
407                     ast::Const(it) => self.const_to_def(container.with_value(it))?.into(),
408                     ast::Static(it) => self.static_to_def(container.with_value(it))?.into(),
409                     ast::Fn(it) => self.fn_to_def(container.with_value(it))?.into(),
410                     _ => continue,
411                 }
412             };
413             return Some(res);
414         }
415         None
416     }
417 }
418
419 #[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
420 pub(crate) enum ChildContainer {
421     DefWithBodyId(DefWithBodyId),
422     ModuleId(ModuleId),
423     TraitId(TraitId),
424     ImplId(ImplId),
425     EnumId(EnumId),
426     VariantId(VariantId),
427     TypeAliasId(TypeAliasId),
428     /// XXX: this might be the same def as, for example an `EnumId`. However,
429     /// here the children are generic parameters, and not, eg enum variants.
430     GenericDefId(GenericDefId),
431 }
432 impl_from! {
433     DefWithBodyId,
434     ModuleId,
435     TraitId,
436     ImplId,
437     EnumId,
438     VariantId,
439     TypeAliasId,
440     GenericDefId
441     for ChildContainer
442 }
443
444 impl ChildContainer {
445     fn child_by_source(self, db: &dyn HirDatabase, file_id: HirFileId) -> DynMap {
446         let db = db.upcast();
447         match self {
448             ChildContainer::DefWithBodyId(it) => it.child_by_source(db, file_id),
449             ChildContainer::ModuleId(it) => it.child_by_source(db, file_id),
450             ChildContainer::TraitId(it) => it.child_by_source(db, file_id),
451             ChildContainer::ImplId(it) => it.child_by_source(db, file_id),
452             ChildContainer::EnumId(it) => it.child_by_source(db, file_id),
453             ChildContainer::VariantId(it) => it.child_by_source(db, file_id),
454             ChildContainer::TypeAliasId(_) => DynMap::default(),
455             ChildContainer::GenericDefId(it) => it.child_by_source(db, file_id),
456         }
457     }
458 }