1 //! Maps *syntax* of various definitions to their semantic ids.
5 child_by_source::ChildBySource,
7 expr::{LabelId, PatId},
9 ConstId, ConstParamId, DefWithBodyId, EnumId, EnumVariantId, FieldId, FunctionId, GenericDefId,
10 ImplId, LifetimeParamId, ModuleId, StaticId, StructId, TraitId, TypeAliasId, TypeParamId,
13 use hir_expand::{name::AsName, AstId, MacroCallId, MacroDefKind};
14 use rustc_hash::FxHashMap;
15 use smallvec::SmallVec;
18 ast::{self, NameOwner},
19 match_ast, AstNode, SyntaxNode,
22 use crate::{db::HirDatabase, InFile, MacroDefId};
24 pub(super) type SourceToDefCache = FxHashMap<ChildContainer, DynMap>;
26 pub(super) struct SourceToDefCtx<'a, 'b> {
27 pub(super) db: &'b dyn HirDatabase,
28 pub(super) cache: &'a mut SourceToDefCache,
31 impl SourceToDefCtx<'_, '_> {
32 pub(super) fn file_to_def(&mut self, file: FileId) -> SmallVec<[ModuleId; 1]> {
33 let _p = profile::span("SourceBinder::to_module_def");
34 let mut mods = SmallVec::new();
35 for &crate_id in self.db.relevant_crates(file).iter() {
37 let crate_def_map = self.db.crate_def_map(crate_id);
40 .modules_for_file(file)
41 .map(|local_id| crate_def_map.module_id(local_id)),
47 pub(super) fn module_to_def(&mut self, src: InFile<ast::Module>) -> Option<ModuleId> {
48 let _p = profile::span("module_to_def");
49 let parent_declaration = src
51 .map(|it| it.syntax())
53 .ancestors_with_macros(self.db.upcast())
56 let m = ast::Module::cast(it.value.clone())?;
57 Some(it.with_value(m))
60 let parent_module = match parent_declaration {
61 Some(parent_declaration) => self.module_to_def(parent_declaration),
63 let file_id = src.file_id.original_file(self.db.upcast());
64 self.file_to_def(file_id).get(0).copied()
68 let child_name = src.value.name()?.as_name();
69 let def_map = parent_module.def_map(self.db.upcast());
70 let child_id = *def_map[parent_module.local_id].children.get(&child_name)?;
71 Some(def_map.module_id(child_id))
74 pub(super) fn source_file_to_def(&mut self, src: InFile<ast::SourceFile>) -> Option<ModuleId> {
75 let _p = profile::span("source_file_to_def");
76 let file_id = src.file_id.original_file(self.db.upcast());
77 self.file_to_def(file_id).get(0).copied()
80 pub(super) fn trait_to_def(&mut self, src: InFile<ast::Trait>) -> Option<TraitId> {
81 self.to_def(src, keys::TRAIT)
83 pub(super) fn impl_to_def(&mut self, src: InFile<ast::Impl>) -> Option<ImplId> {
84 self.to_def(src, keys::IMPL)
86 pub(super) fn fn_to_def(&mut self, src: InFile<ast::Fn>) -> Option<FunctionId> {
87 self.to_def(src, keys::FUNCTION)
89 pub(super) fn struct_to_def(&mut self, src: InFile<ast::Struct>) -> Option<StructId> {
90 self.to_def(src, keys::STRUCT)
92 pub(super) fn enum_to_def(&mut self, src: InFile<ast::Enum>) -> Option<EnumId> {
93 self.to_def(src, keys::ENUM)
95 pub(super) fn union_to_def(&mut self, src: InFile<ast::Union>) -> Option<UnionId> {
96 self.to_def(src, keys::UNION)
98 pub(super) fn static_to_def(&mut self, src: InFile<ast::Static>) -> Option<StaticId> {
99 self.to_def(src, keys::STATIC)
101 pub(super) fn const_to_def(&mut self, src: InFile<ast::Const>) -> Option<ConstId> {
102 self.to_def(src, keys::CONST)
104 pub(super) fn type_alias_to_def(&mut self, src: InFile<ast::TypeAlias>) -> Option<TypeAliasId> {
105 self.to_def(src, keys::TYPE_ALIAS)
107 pub(super) fn record_field_to_def(&mut self, src: InFile<ast::RecordField>) -> Option<FieldId> {
108 self.to_def(src, keys::RECORD_FIELD)
110 pub(super) fn tuple_field_to_def(&mut self, src: InFile<ast::TupleField>) -> Option<FieldId> {
111 self.to_def(src, keys::TUPLE_FIELD)
113 pub(super) fn enum_variant_to_def(
115 src: InFile<ast::Variant>,
116 ) -> Option<EnumVariantId> {
117 self.to_def(src, keys::VARIANT)
119 pub(super) fn bind_pat_to_def(
121 src: InFile<ast::IdentPat>,
122 ) -> Option<(DefWithBodyId, PatId)> {
123 let container = self.find_pat_or_label_container(src.as_ref().map(|it| it.syntax()))?;
124 let (_body, source_map) = self.db.body_with_source_map(container);
125 let src = src.map(ast::Pat::from);
126 let pat_id = source_map.node_pat(src.as_ref())?;
127 Some((container, pat_id))
129 pub(super) fn self_param_to_def(
131 src: InFile<ast::SelfParam>,
132 ) -> Option<(DefWithBodyId, PatId)> {
133 let container = self.find_pat_or_label_container(src.as_ref().map(|it| it.syntax()))?;
134 let (_body, source_map) = self.db.body_with_source_map(container);
135 let pat_id = source_map.node_self_param(src.as_ref())?;
136 Some((container, pat_id))
138 pub(super) fn label_to_def(
140 src: InFile<ast::Label>,
141 ) -> Option<(DefWithBodyId, LabelId)> {
142 let container = self.find_pat_or_label_container(src.as_ref().map(|it| it.syntax()))?;
143 let (_body, source_map) = self.db.body_with_source_map(container);
144 let label_id = source_map.node_label(src.as_ref())?;
145 Some((container, label_id))
148 pub(super) fn item_to_macro_call(&mut self, src: InFile<ast::Item>) -> Option<MacroCallId> {
149 let map = self.dyn_map(src.as_ref())?;
150 map[keys::ATTR_MACRO].get(&src).copied()
153 fn to_def<Ast: AstNode + 'static, ID: Copy + 'static>(
158 self.dyn_map(src.as_ref())?[key].get(&src).copied()
161 fn dyn_map<Ast: AstNode + 'static>(&mut self, src: InFile<&Ast>) -> Option<&DynMap> {
162 let container = self.find_container(src.map(|it| it.syntax()))?;
165 &*self.cache.entry(container).or_insert_with(|| container.child_by_source(db));
169 pub(super) fn type_param_to_def(&mut self, src: InFile<ast::TypeParam>) -> Option<TypeParamId> {
170 let container: ChildContainer =
171 self.find_generic_param_container(src.as_ref().map(|it| it.syntax()))?.into();
174 &*self.cache.entry(container).or_insert_with(|| container.child_by_source(db));
175 dyn_map[keys::TYPE_PARAM].get(&src).copied()
178 pub(super) fn lifetime_param_to_def(
180 src: InFile<ast::LifetimeParam>,
181 ) -> Option<LifetimeParamId> {
182 let container: ChildContainer =
183 self.find_generic_param_container(src.as_ref().map(|it| it.syntax()))?.into();
186 &*self.cache.entry(container).or_insert_with(|| container.child_by_source(db));
187 dyn_map[keys::LIFETIME_PARAM].get(&src).copied()
190 pub(super) fn const_param_to_def(
192 src: InFile<ast::ConstParam>,
193 ) -> Option<ConstParamId> {
194 let container: ChildContainer =
195 self.find_generic_param_container(src.as_ref().map(|it| it.syntax()))?.into();
198 &*self.cache.entry(container).or_insert_with(|| container.child_by_source(db));
199 dyn_map[keys::CONST_PARAM].get(&src).copied()
202 // FIXME: use DynMap as well?
203 pub(super) fn macro_to_def(&mut self, src: InFile<ast::Macro>) -> Option<MacroDefId> {
204 let file_ast_id = self.db.ast_id_map(src.file_id).ast_id(&src.value);
205 let ast_id = AstId::new(src.file_id, file_ast_id.upcast());
206 let kind = MacroDefKind::Declarative(ast_id);
207 let file_id = src.file_id.original_file(self.db.upcast());
208 let krate = self.file_to_def(file_id).get(0).copied()?.krate();
209 Some(MacroDefId { krate, kind, local_inner: false })
212 pub(super) fn find_container(&mut self, src: InFile<&SyntaxNode>) -> Option<ChildContainer> {
213 for container in src.cloned().ancestors_with_macros(self.db.upcast()).skip(1) {
214 let res: ChildContainer = match_ast! {
215 match (container.value) {
217 let def = self.module_to_def(container.with_value(it))?;
221 let def = self.trait_to_def(container.with_value(it))?;
225 let def = self.impl_to_def(container.with_value(it))?;
229 let def = self.fn_to_def(container.with_value(it))?;
230 DefWithBodyId::from(def).into()
233 let def = self.struct_to_def(container.with_value(it))?;
234 VariantId::from(def).into()
237 let def = self.enum_to_def(container.with_value(it))?;
241 let def = self.union_to_def(container.with_value(it))?;
242 VariantId::from(def).into()
245 let def = self.static_to_def(container.with_value(it))?;
246 DefWithBodyId::from(def).into()
249 let def = self.const_to_def(container.with_value(it))?;
250 DefWithBodyId::from(def).into()
252 ast::TypeAlias(it) => {
253 let def = self.type_alias_to_def(container.with_value(it))?;
256 ast::Variant(it) => {
257 let def = self.enum_variant_to_def(container.with_value(it))?;
258 VariantId::from(def).into()
266 let def = self.file_to_def(src.file_id.original_file(self.db.upcast())).get(0).copied()?;
270 fn find_generic_param_container(&mut self, src: InFile<&SyntaxNode>) -> Option<GenericDefId> {
271 for container in src.cloned().ancestors_with_macros(self.db.upcast()).skip(1) {
272 let res: GenericDefId = match_ast! {
273 match (container.value) {
274 ast::Fn(it) => self.fn_to_def(container.with_value(it))?.into(),
275 ast::Struct(it) => self.struct_to_def(container.with_value(it))?.into(),
276 ast::Enum(it) => self.enum_to_def(container.with_value(it))?.into(),
277 ast::Trait(it) => self.trait_to_def(container.with_value(it))?.into(),
278 ast::TypeAlias(it) => self.type_alias_to_def(container.with_value(it))?.into(),
279 ast::Impl(it) => self.impl_to_def(container.with_value(it))?.into(),
288 fn find_pat_or_label_container(&mut self, src: InFile<&SyntaxNode>) -> Option<DefWithBodyId> {
289 for container in src.cloned().ancestors_with_macros(self.db.upcast()).skip(1) {
290 let res: DefWithBodyId = match_ast! {
291 match (container.value) {
292 ast::Const(it) => self.const_to_def(container.with_value(it))?.into(),
293 ast::Static(it) => self.static_to_def(container.with_value(it))?.into(),
294 ast::Fn(it) => self.fn_to_def(container.with_value(it))?.into(),
304 #[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
305 pub(crate) enum ChildContainer {
306 DefWithBodyId(DefWithBodyId),
311 VariantId(VariantId),
312 TypeAliasId(TypeAliasId),
313 /// XXX: this might be the same def as, for example an `EnumId`. However,
314 /// here the children are generic parameters, and not, eg enum variants.
315 GenericDefId(GenericDefId),
329 impl ChildContainer {
330 fn child_by_source(self, db: &dyn HirDatabase) -> DynMap {
331 let db = db.upcast();
333 ChildContainer::DefWithBodyId(it) => it.child_by_source(db),
334 ChildContainer::ModuleId(it) => it.child_by_source(db),
335 ChildContainer::TraitId(it) => it.child_by_source(db),
336 ChildContainer::ImplId(it) => it.child_by_source(db),
337 ChildContainer::EnumId(it) => it.child_by_source(db),
338 ChildContainer::VariantId(it) => it.child_by_source(db),
339 ChildContainer::TypeAliasId(_) => DynMap::default(),
340 ChildContainer::GenericDefId(it) => it.child_by_source(db),