aboutsummaryrefslogtreecommitdiff
path: root/crates/hir/src/semantics/source_to_def.rs
blob: c6ad5ecb5fb7fd558a0d8a718e6fd8a87b37bec1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
//! Maps *syntax* of various definitions to their semantic ids.

use base_db::FileId;
use hir_def::{
    child_by_source::ChildBySource,
    dyn_map::DynMap,
    expr::{LabelId, PatId},
    keys::{self, Key},
    ConstId, ConstParamId, DefWithBodyId, EnumId, EnumVariantId, FieldId, FunctionId, GenericDefId,
    ImplId, LifetimeParamId, ModuleId, StaticId, StructId, TraitId, TypeAliasId, TypeParamId,
    UnionId, VariantId,
};
use hir_expand::{name::AsName, AstId, MacroDefKind};
use rustc_hash::FxHashMap;
use smallvec::SmallVec;
use stdx::impl_from;
use syntax::{
    ast::{self, NameOwner},
    match_ast, AstNode, SyntaxNode,
};

use crate::{db::HirDatabase, InFile, MacroDefId};

pub(super) type SourceToDefCache = FxHashMap<ChildContainer, DynMap>;

pub(super) struct SourceToDefCtx<'a, 'b> {
    pub(super) db: &'b dyn HirDatabase,
    pub(super) cache: &'a mut SourceToDefCache,
}

impl SourceToDefCtx<'_, '_> {
    pub(super) fn file_to_def(&mut self, file: FileId) -> SmallVec<[ModuleId; 1]> {
        let _p = profile::span("SourceBinder::to_module_def");
        let mut mods = SmallVec::new();
        for &crate_id in self.db.relevant_crates(file).iter() {
            // FIXME: inner items
            let crate_def_map = self.db.crate_def_map(crate_id);
            mods.extend(
                crate_def_map
                    .modules_for_file(file)
                    .map(|local_id| crate_def_map.module_id(local_id)),
            )
        }
        mods
    }

    pub(super) fn module_to_def(&mut self, src: InFile<ast::Module>) -> Option<ModuleId> {
        let _p = profile::span("module_to_def");
        let parent_declaration = src
            .as_ref()
            .map(|it| it.syntax())
            .cloned()
            .ancestors_with_macros(self.db.upcast())
            .skip(1)
            .find_map(|it| {
                let m = ast::Module::cast(it.value.clone())?;
                Some(it.with_value(m))
            });

        let parent_module = match parent_declaration {
            Some(parent_declaration) => self.module_to_def(parent_declaration),
            None => {
                let file_id = src.file_id.original_file(self.db.upcast());
                self.file_to_def(file_id).get(0).copied()
            }
        }?;

        let child_name = src.value.name()?.as_name();
        let def_map = parent_module.def_map(self.db.upcast());
        let child_id = *def_map[parent_module.local_id].children.get(&child_name)?;
        Some(def_map.module_id(child_id))
    }

    pub(super) fn source_file_to_def(&mut self, src: InFile<ast::SourceFile>) -> Option<ModuleId> {
        let _p = profile::span("source_file_to_def");
        let file_id = src.file_id.original_file(self.db.upcast());
        self.file_to_def(file_id).get(0).copied()
    }

    pub(super) fn trait_to_def(&mut self, src: InFile<ast::Trait>) -> Option<TraitId> {
        self.to_def(src, keys::TRAIT)
    }
    pub(super) fn impl_to_def(&mut self, src: InFile<ast::Impl>) -> Option<ImplId> {
        self.to_def(src, keys::IMPL)
    }
    pub(super) fn fn_to_def(&mut self, src: InFile<ast::Fn>) -> Option<FunctionId> {
        self.to_def(src, keys::FUNCTION)
    }
    pub(super) fn struct_to_def(&mut self, src: InFile<ast::Struct>) -> Option<StructId> {
        self.to_def(src, keys::STRUCT)
    }
    pub(super) fn enum_to_def(&mut self, src: InFile<ast::Enum>) -> Option<EnumId> {
        self.to_def(src, keys::ENUM)
    }
    pub(super) fn union_to_def(&mut self, src: InFile<ast::Union>) -> Option<UnionId> {
        self.to_def(src, keys::UNION)
    }
    pub(super) fn static_to_def(&mut self, src: InFile<ast::Static>) -> Option<StaticId> {
        self.to_def(src, keys::STATIC)
    }
    pub(super) fn const_to_def(&mut self, src: InFile<ast::Const>) -> Option<ConstId> {
        self.to_def(src, keys::CONST)
    }
    pub(super) fn type_alias_to_def(&mut self, src: InFile<ast::TypeAlias>) -> Option<TypeAliasId> {
        self.to_def(src, keys::TYPE_ALIAS)
    }
    pub(super) fn record_field_to_def(&mut self, src: InFile<ast::RecordField>) -> Option<FieldId> {
        self.to_def(src, keys::RECORD_FIELD)
    }
    pub(super) fn tuple_field_to_def(&mut self, src: InFile<ast::TupleField>) -> Option<FieldId> {
        self.to_def(src, keys::TUPLE_FIELD)
    }
    pub(super) fn enum_variant_to_def(
        &mut self,
        src: InFile<ast::Variant>,
    ) -> Option<EnumVariantId> {
        self.to_def(src, keys::VARIANT)
    }
    pub(super) fn bind_pat_to_def(
        &mut self,
        src: InFile<ast::IdentPat>,
    ) -> Option<(DefWithBodyId, PatId)> {
        let container = self.find_pat_or_label_container(src.as_ref().map(|it| it.syntax()))?;
        let (_body, source_map) = self.db.body_with_source_map(container);
        let src = src.map(ast::Pat::from);
        let pat_id = source_map.node_pat(src.as_ref())?;
        Some((container, pat_id))
    }
    pub(super) fn self_param_to_def(
        &mut self,
        src: InFile<ast::SelfParam>,
    ) -> Option<(DefWithBodyId, PatId)> {
        let container = self.find_pat_or_label_container(src.as_ref().map(|it| it.syntax()))?;
        let (_body, source_map) = self.db.body_with_source_map(container);
        let pat_id = source_map.node_self_param(src.as_ref())?;
        Some((container, pat_id))
    }
    pub(super) fn label_to_def(
        &mut self,
        src: InFile<ast::Label>,
    ) -> Option<(DefWithBodyId, LabelId)> {
        let container = self.find_pat_or_label_container(src.as_ref().map(|it| it.syntax()))?;
        let (_body, source_map) = self.db.body_with_source_map(container);
        let label_id = source_map.node_label(src.as_ref())?;
        Some((container, label_id))
    }

    fn to_def<Ast: AstNode + 'static, ID: Copy + 'static>(
        &mut self,
        src: InFile<Ast>,
        key: Key<Ast, ID>,
    ) -> Option<ID> {
        let container = self.find_container(src.as_ref().map(|it| it.syntax()))?;
        let db = self.db;
        let dyn_map =
            &*self.cache.entry(container).or_insert_with(|| container.child_by_source(db));
        dyn_map[key].get(&src).copied()
    }

    pub(super) fn type_param_to_def(&mut self, src: InFile<ast::TypeParam>) -> Option<TypeParamId> {
        let container: ChildContainer =
            self.find_generic_param_container(src.as_ref().map(|it| it.syntax()))?.into();
        let db = self.db;
        let dyn_map =
            &*self.cache.entry(container).or_insert_with(|| container.child_by_source(db));
        dyn_map[keys::TYPE_PARAM].get(&src).copied()
    }

    pub(super) fn lifetime_param_to_def(
        &mut self,
        src: InFile<ast::LifetimeParam>,
    ) -> Option<LifetimeParamId> {
        let container: ChildContainer =
            self.find_generic_param_container(src.as_ref().map(|it| it.syntax()))?.into();
        let db = self.db;
        let dyn_map =
            &*self.cache.entry(container).or_insert_with(|| container.child_by_source(db));
        dyn_map[keys::LIFETIME_PARAM].get(&src).copied()
    }

    pub(super) fn const_param_to_def(
        &mut self,
        src: InFile<ast::ConstParam>,
    ) -> Option<ConstParamId> {
        let container: ChildContainer =
            self.find_generic_param_container(src.as_ref().map(|it| it.syntax()))?.into();
        let db = self.db;
        let dyn_map =
            &*self.cache.entry(container).or_insert_with(|| container.child_by_source(db));
        dyn_map[keys::CONST_PARAM].get(&src).copied()
    }

    // FIXME: use DynMap as well?
    pub(super) fn macro_rules_to_def(
        &mut self,
        src: InFile<ast::MacroRules>,
    ) -> Option<MacroDefId> {
        let kind = MacroDefKind::Declarative;
        let file_id = src.file_id.original_file(self.db.upcast());
        let krate = self.file_to_def(file_id).get(0).copied()?.krate();
        let file_ast_id = self.db.ast_id_map(src.file_id).ast_id(&src.value);
        let ast_id = Some(AstId::new(src.file_id, file_ast_id.upcast()));
        Some(MacroDefId { krate, ast_id, kind, local_inner: false })
    }

    pub(super) fn find_container(&mut self, src: InFile<&SyntaxNode>) -> Option<ChildContainer> {
        for container in src.cloned().ancestors_with_macros(self.db.upcast()).skip(1) {
            let res: ChildContainer = match_ast! {
                match (container.value) {
                    ast::Module(it) => {
                        let def = self.module_to_def(container.with_value(it))?;
                        def.into()
                    },
                    ast::Trait(it) => {
                        let def = self.trait_to_def(container.with_value(it))?;
                        def.into()
                    },
                    ast::Impl(it) => {
                        let def = self.impl_to_def(container.with_value(it))?;
                        def.into()
                    },
                    ast::Fn(it) => {
                        let def = self.fn_to_def(container.with_value(it))?;
                        DefWithBodyId::from(def).into()
                    },
                    ast::Struct(it) => {
                        let def = self.struct_to_def(container.with_value(it))?;
                        VariantId::from(def).into()
                    },
                    ast::Enum(it) => {
                        let def = self.enum_to_def(container.with_value(it))?;
                        def.into()
                    },
                    ast::Union(it) => {
                        let def = self.union_to_def(container.with_value(it))?;
                        VariantId::from(def).into()
                    },
                    ast::Static(it) => {
                        let def = self.static_to_def(container.with_value(it))?;
                        DefWithBodyId::from(def).into()
                    },
                    ast::Const(it) => {
                        let def = self.const_to_def(container.with_value(it))?;
                        DefWithBodyId::from(def).into()
                    },
                    ast::TypeAlias(it) => {
                        let def = self.type_alias_to_def(container.with_value(it))?;
                        def.into()
                    },
                    ast::Variant(it) => {
                        let def = self.enum_variant_to_def(container.with_value(it))?;
                        VariantId::from(def).into()
                    },
                    _ => continue,
                }
            };
            return Some(res);
        }

        let def = self.file_to_def(src.file_id.original_file(self.db.upcast())).get(0).copied()?;
        Some(def.into())
    }

    fn find_generic_param_container(&mut self, src: InFile<&SyntaxNode>) -> Option<GenericDefId> {
        for container in src.cloned().ancestors_with_macros(self.db.upcast()).skip(1) {
            let res: GenericDefId = match_ast! {
                match (container.value) {
                    ast::Fn(it) => self.fn_to_def(container.with_value(it))?.into(),
                    ast::Struct(it) => self.struct_to_def(container.with_value(it))?.into(),
                    ast::Enum(it) => self.enum_to_def(container.with_value(it))?.into(),
                    ast::Trait(it) => self.trait_to_def(container.with_value(it))?.into(),
                    ast::TypeAlias(it) => self.type_alias_to_def(container.with_value(it))?.into(),
                    ast::Impl(it) => self.impl_to_def(container.with_value(it))?.into(),
                    _ => continue,
                }
            };
            return Some(res);
        }
        None
    }

    fn find_pat_or_label_container(&mut self, src: InFile<&SyntaxNode>) -> Option<DefWithBodyId> {
        for container in src.cloned().ancestors_with_macros(self.db.upcast()).skip(1) {
            let res: DefWithBodyId = match_ast! {
                match (container.value) {
                    ast::Const(it) => self.const_to_def(container.with_value(it))?.into(),
                    ast::Static(it) => self.static_to_def(container.with_value(it))?.into(),
                    ast::Fn(it) => self.fn_to_def(container.with_value(it))?.into(),
                    _ => continue,
                }
            };
            return Some(res);
        }
        None
    }
}

#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
pub(crate) enum ChildContainer {
    DefWithBodyId(DefWithBodyId),
    ModuleId(ModuleId),
    TraitId(TraitId),
    ImplId(ImplId),
    EnumId(EnumId),
    VariantId(VariantId),
    TypeAliasId(TypeAliasId),
    /// XXX: this might be the same def as, for example an `EnumId`. However,
    /// here the children are generic parameters, and not, eg enum variants.
    GenericDefId(GenericDefId),
}
impl_from! {
    DefWithBodyId,
    ModuleId,
    TraitId,
    ImplId,
    EnumId,
    VariantId,
    TypeAliasId,
    GenericDefId
    for ChildContainer
}

impl ChildContainer {
    fn child_by_source(self, db: &dyn HirDatabase) -> DynMap {
        let db = db.upcast();
        match self {
            ChildContainer::DefWithBodyId(it) => it.child_by_source(db),
            ChildContainer::ModuleId(it) => it.child_by_source(db),
            ChildContainer::TraitId(it) => it.child_by_source(db),
            ChildContainer::ImplId(it) => it.child_by_source(db),
            ChildContainer::EnumId(it) => it.child_by_source(db),
            ChildContainer::VariantId(it) => it.child_by_source(db),
            ChildContainer::TypeAliasId(_) => DynMap::default(),
            ChildContainer::GenericDefId(it) => it.child_by_source(db),
        }
    }
}