aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src/semantics
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2020-08-13 15:36:55 +0100
committerAleksey Kladov <[email protected]>2020-08-13 15:36:55 +0100
commitae71a631fd657368e8593feb5e025d23147afe60 (patch)
treef38493871f6598f37a9c342713ce3faff0057646 /crates/ra_hir/src/semantics
parent6a77ec7bbe6ddbf663dce9529d11d1bb56c5489a (diff)
Rename ra_hir -> hir
Diffstat (limited to 'crates/ra_hir/src/semantics')
-rw-r--r--crates/ra_hir/src/semantics/source_to_def.rs275
1 files changed, 0 insertions, 275 deletions
diff --git a/crates/ra_hir/src/semantics/source_to_def.rs b/crates/ra_hir/src/semantics/source_to_def.rs
deleted file mode 100644
index 5918b9541..000000000
--- a/crates/ra_hir/src/semantics/source_to_def.rs
+++ /dev/null
@@ -1,275 +0,0 @@
1//! Maps *syntax* of various definitions to their semantic ids.
2
3use base_db::FileId;
4use hir_def::{
5 child_by_source::ChildBySource,
6 dyn_map::DynMap,
7 expr::PatId,
8 keys::{self, Key},
9 ConstId, DefWithBodyId, EnumId, EnumVariantId, FieldId, FunctionId, GenericDefId, ImplId,
10 ModuleId, StaticId, StructId, TraitId, TypeAliasId, TypeParamId, UnionId, VariantId,
11};
12use hir_expand::{name::AsName, AstId, MacroDefKind};
13use rustc_hash::FxHashMap;
14use stdx::impl_from;
15use syntax::{
16 ast::{self, NameOwner},
17 match_ast, AstNode, SyntaxNode,
18};
19
20use crate::{db::HirDatabase, InFile, MacroDefId};
21
22pub(super) type SourceToDefCache = FxHashMap<ChildContainer, DynMap>;
23
24pub(super) struct SourceToDefCtx<'a, 'b> {
25 pub(super) db: &'b dyn HirDatabase,
26 pub(super) cache: &'a mut SourceToDefCache,
27}
28
29impl SourceToDefCtx<'_, '_> {
30 pub(super) fn file_to_def(&mut self, file: FileId) -> Option<ModuleId> {
31 let _p = profile::span("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::span("module_to_def");
42 let parent_declaration = src
43 .as_ref()
44 .map(|it| it.syntax())
45 .cloned()
46 .ancestors_with_macros(self.db.upcast())
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.upcast());
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::Trait>) -> Option<TraitId> {
68 self.to_def(src, keys::TRAIT)
69 }
70 pub(super) fn impl_to_def(&mut self, src: InFile<ast::Impl>) -> Option<ImplId> {
71 self.to_def(src, keys::IMPL)
72 }
73 pub(super) fn fn_to_def(&mut self, src: InFile<ast::Fn>) -> Option<FunctionId> {
74 self.to_def(src, keys::FUNCTION)
75 }
76 pub(super) fn struct_to_def(&mut self, src: InFile<ast::Struct>) -> Option<StructId> {
77 self.to_def(src, keys::STRUCT)
78 }
79 pub(super) fn enum_to_def(&mut self, src: InFile<ast::Enum>) -> Option<EnumId> {
80 self.to_def(src, keys::ENUM)
81 }
82 pub(super) fn union_to_def(&mut self, src: InFile<ast::Union>) -> Option<UnionId> {
83 self.to_def(src, keys::UNION)
84 }
85 pub(super) fn static_to_def(&mut self, src: InFile<ast::Static>) -> Option<StaticId> {
86 self.to_def(src, keys::STATIC)
87 }
88 pub(super) fn const_to_def(&mut self, src: InFile<ast::Const>) -> Option<ConstId> {
89 self.to_def(src, keys::CONST)
90 }
91 pub(super) fn type_alias_to_def(&mut self, src: InFile<ast::TypeAlias>) -> Option<TypeAliasId> {
92 self.to_def(src, keys::TYPE_ALIAS)
93 }
94 pub(super) fn record_field_to_def(&mut self, src: InFile<ast::RecordField>) -> Option<FieldId> {
95 self.to_def(src, keys::RECORD_FIELD)
96 }
97 pub(super) fn tuple_field_to_def(&mut self, src: InFile<ast::TupleField>) -> Option<FieldId> {
98 self.to_def(src, keys::TUPLE_FIELD)
99 }
100 pub(super) fn enum_variant_to_def(
101 &mut self,
102 src: InFile<ast::Variant>,
103 ) -> Option<EnumVariantId> {
104 self.to_def(src, keys::VARIANT)
105 }
106 pub(super) fn bind_pat_to_def(
107 &mut self,
108 src: InFile<ast::IdentPat>,
109 ) -> Option<(DefWithBodyId, PatId)> {
110 let container = self.find_pat_container(src.as_ref().map(|it| it.syntax()))?;
111 let (_body, source_map) = self.db.body_with_source_map(container);
112 let src = src.map(ast::Pat::from);
113 let pat_id = source_map.node_pat(src.as_ref())?;
114 Some((container, pat_id))
115 }
116
117 fn to_def<Ast: AstNode + 'static, ID: Copy + 'static>(
118 &mut self,
119 src: InFile<Ast>,
120 key: Key<Ast, ID>,
121 ) -> Option<ID> {
122 let container = self.find_container(src.as_ref().map(|it| it.syntax()))?;
123 let db = self.db;
124 let dyn_map =
125 &*self.cache.entry(container).or_insert_with(|| container.child_by_source(db));
126 dyn_map[key].get(&src).copied()
127 }
128
129 pub(super) fn type_param_to_def(&mut self, src: InFile<ast::TypeParam>) -> Option<TypeParamId> {
130 let container: ChildContainer =
131 self.find_type_param_container(src.as_ref().map(|it| it.syntax()))?.into();
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[keys::TYPE_PARAM].get(&src).copied()
136 }
137
138 // FIXME: use DynMap as well?
139 pub(super) fn macro_call_to_def(&mut self, src: InFile<ast::MacroCall>) -> Option<MacroDefId> {
140 let kind = MacroDefKind::Declarative;
141 let file_id = src.file_id.original_file(self.db.upcast());
142 let krate = self.file_to_def(file_id)?.krate;
143 let file_ast_id = self.db.ast_id_map(src.file_id).ast_id(&src.value);
144 let ast_id = Some(AstId::new(src.file_id, file_ast_id));
145 Some(MacroDefId { krate: Some(krate), ast_id, kind, local_inner: false })
146 }
147
148 pub(super) fn find_container(&mut self, src: InFile<&SyntaxNode>) -> Option<ChildContainer> {
149 for container in src.cloned().ancestors_with_macros(self.db.upcast()).skip(1) {
150 let res: ChildContainer = match_ast! {
151 match (container.value) {
152 ast::Module(it) => {
153 let def = self.module_to_def(container.with_value(it))?;
154 def.into()
155 },
156 ast::Trait(it) => {
157 let def = self.trait_to_def(container.with_value(it))?;
158 def.into()
159 },
160 ast::Impl(it) => {
161 let def = self.impl_to_def(container.with_value(it))?;
162 def.into()
163 },
164 ast::Fn(it) => {
165 let def = self.fn_to_def(container.with_value(it))?;
166 DefWithBodyId::from(def).into()
167 },
168 ast::Struct(it) => {
169 let def = self.struct_to_def(container.with_value(it))?;
170 VariantId::from(def).into()
171 },
172 ast::Enum(it) => {
173 let def = self.enum_to_def(container.with_value(it))?;
174 def.into()
175 },
176 ast::Union(it) => {
177 let def = self.union_to_def(container.with_value(it))?;
178 VariantId::from(def).into()
179 },
180 ast::Static(it) => {
181 let def = self.static_to_def(container.with_value(it))?;
182 DefWithBodyId::from(def).into()
183 },
184 ast::Const(it) => {
185 let def = self.const_to_def(container.with_value(it))?;
186 DefWithBodyId::from(def).into()
187 },
188 ast::TypeAlias(it) => {
189 let def = self.type_alias_to_def(container.with_value(it))?;
190 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.upcast()))?;
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.upcast()).skip(1) {
204 let res: GenericDefId = match_ast! {
205 match (container.value) {
206 ast::Fn(it) => self.fn_to_def(container.with_value(it))?.into(),
207 ast::Struct(it) => self.struct_to_def(container.with_value(it))?.into(),
208 ast::Enum(it) => self.enum_to_def(container.with_value(it))?.into(),
209 ast::Trait(it) => self.trait_to_def(container.with_value(it))?.into(),
210 ast::TypeAlias(it) => self.type_alias_to_def(container.with_value(it))?.into(),
211 ast::Impl(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.upcast()).skip(1) {
222 let res: DefWithBodyId = match_ast! {
223 match (container.value) {
224 ast::Const(it) => self.const_to_def(container.with_value(it))?.into(),
225 ast::Static(it) => self.static_to_def(container.with_value(it))?.into(),
226 ast::Fn(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)]
237pub(crate) enum ChildContainer {
238 DefWithBodyId(DefWithBodyId),
239 ModuleId(ModuleId),
240 TraitId(TraitId),
241 ImplId(ImplId),
242 EnumId(EnumId),
243 VariantId(VariantId),
244 TypeAliasId(TypeAliasId),
245 /// XXX: this might be the same def as, for example an `EnumId`. However,
246 /// here the children generic parameters, and not, eg enum variants.
247 GenericDefId(GenericDefId),
248}
249impl_from! {
250 DefWithBodyId,
251 ModuleId,
252 TraitId,
253 ImplId,
254 EnumId,
255 VariantId,
256 TypeAliasId,
257 GenericDefId
258 for ChildContainer
259}
260
261impl ChildContainer {
262 fn child_by_source(self, db: &dyn HirDatabase) -> DynMap {
263 let db = db.upcast();
264 match self {
265 ChildContainer::DefWithBodyId(it) => it.child_by_source(db),
266 ChildContainer::ModuleId(it) => it.child_by_source(db),
267 ChildContainer::TraitId(it) => it.child_by_source(db),
268 ChildContainer::ImplId(it) => it.child_by_source(db),
269 ChildContainer::EnumId(it) => it.child_by_source(db),
270 ChildContainer::VariantId(it) => it.child_by_source(db),
271 ChildContainer::TypeAliasId(_) => DynMap::default(),
272 ChildContainer::GenericDefId(it) => it.child_by_source(db),
273 }
274 }
275}