aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src/source_analyzer.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_hir/src/source_analyzer.rs')
-rw-r--r--crates/ra_hir/src/source_analyzer.rs89
1 files changed, 47 insertions, 42 deletions
diff --git a/crates/ra_hir/src/source_analyzer.rs b/crates/ra_hir/src/source_analyzer.rs
index 331ecdd9c..e8afef328 100644
--- a/crates/ra_hir/src/source_analyzer.rs
+++ b/crates/ra_hir/src/source_analyzer.rs
@@ -42,7 +42,7 @@ pub(crate) struct SourceAnalyzer {
42 42
43impl SourceAnalyzer { 43impl SourceAnalyzer {
44 pub(crate) fn new_for_body( 44 pub(crate) fn new_for_body(
45 db: &impl HirDatabase, 45 db: &dyn HirDatabase,
46 def: DefWithBodyId, 46 def: DefWithBodyId,
47 node: InFile<&SyntaxNode>, 47 node: InFile<&SyntaxNode>,
48 offset: Option<TextUnit>, 48 offset: Option<TextUnit>,
@@ -53,7 +53,7 @@ impl SourceAnalyzer {
53 None => scope_for(&scopes, &source_map, node), 53 None => scope_for(&scopes, &source_map, node),
54 Some(offset) => scope_for_offset(&scopes, &source_map, node.with_value(offset)), 54 Some(offset) => scope_for_offset(&scopes, &source_map, node.with_value(offset)),
55 }; 55 };
56 let resolver = resolver_for_scope(db, def, scope); 56 let resolver = resolver_for_scope(db.upcast(), def, scope);
57 SourceAnalyzer { 57 SourceAnalyzer {
58 resolver, 58 resolver,
59 body: Some(body), 59 body: Some(body),
@@ -90,7 +90,7 @@ impl SourceAnalyzer {
90 90
91 fn expand_expr( 91 fn expand_expr(
92 &self, 92 &self,
93 db: &impl HirDatabase, 93 db: &dyn HirDatabase,
94 expr: InFile<ast::MacroCall>, 94 expr: InFile<ast::MacroCall>,
95 ) -> Option<InFile<ast::Expr>> { 95 ) -> Option<InFile<ast::Expr>> {
96 let macro_file = self.body_source_map.as_ref()?.node_macro_file(expr.as_ref())?; 96 let macro_file = self.body_source_map.as_ref()?.node_macro_file(expr.as_ref())?;
@@ -103,11 +103,11 @@ impl SourceAnalyzer {
103 Some(res) 103 Some(res)
104 } 104 }
105 105
106 fn trait_env(&self, db: &impl HirDatabase) -> Arc<TraitEnvironment> { 106 fn trait_env(&self, db: &dyn HirDatabase) -> Arc<TraitEnvironment> {
107 TraitEnvironment::lower(db, &self.resolver) 107 TraitEnvironment::lower(db, &self.resolver)
108 } 108 }
109 109
110 pub(crate) fn type_of(&self, db: &impl HirDatabase, expr: &ast::Expr) -> Option<Type> { 110 pub(crate) fn type_of(&self, db: &dyn HirDatabase, expr: &ast::Expr) -> Option<Type> {
111 let expr_id = match expr { 111 let expr_id = match expr {
112 ast::Expr::MacroCall(call) => { 112 ast::Expr::MacroCall(call) => {
113 let expr = self.expand_expr(db, InFile::new(self.file_id, call.clone()))?; 113 let expr = self.expand_expr(db, InFile::new(self.file_id, call.clone()))?;
@@ -121,7 +121,7 @@ impl SourceAnalyzer {
121 Some(Type { krate: self.resolver.krate()?, ty: InEnvironment { value: ty, environment } }) 121 Some(Type { krate: self.resolver.krate()?, ty: InEnvironment { value: ty, environment } })
122 } 122 }
123 123
124 pub(crate) fn type_of_pat(&self, db: &impl HirDatabase, pat: &ast::Pat) -> Option<Type> { 124 pub(crate) fn type_of_pat(&self, db: &dyn HirDatabase, pat: &ast::Pat) -> Option<Type> {
125 let pat_id = self.pat_id(pat)?; 125 let pat_id = self.pat_id(pat)?;
126 let ty = self.infer.as_ref()?[pat_id].clone(); 126 let ty = self.infer.as_ref()?[pat_id].clone();
127 let environment = self.trait_env(db); 127 let environment = self.trait_env(db);
@@ -140,7 +140,7 @@ impl SourceAnalyzer {
140 140
141 pub(crate) fn resolve_record_field( 141 pub(crate) fn resolve_record_field(
142 &self, 142 &self,
143 db: &impl HirDatabase, 143 db: &dyn HirDatabase,
144 field: &ast::RecordField, 144 field: &ast::RecordField,
145 ) -> Option<(crate::StructField, Option<Local>)> { 145 ) -> Option<(crate::StructField, Option<Local>)> {
146 let (expr_id, local) = match field.expr() { 146 let (expr_id, local) = match field.expr() {
@@ -150,7 +150,7 @@ impl SourceAnalyzer {
150 let expr_id = self.body_source_map.as_ref()?.field_init_shorthand_expr(src)?; 150 let expr_id = self.body_source_map.as_ref()?.field_init_shorthand_expr(src)?;
151 let local_name = field.name_ref()?.as_name(); 151 let local_name = field.name_ref()?.as_name();
152 let path = ModPath::from_segments(PathKind::Plain, once(local_name)); 152 let path = ModPath::from_segments(PathKind::Plain, once(local_name));
153 let local = match self.resolver.resolve_path_in_value_ns_fully(db, &path) { 153 let local = match self.resolver.resolve_path_in_value_ns_fully(db.upcast(), &path) {
154 Some(ValueNs::LocalBinding(pat_id)) => { 154 Some(ValueNs::LocalBinding(pat_id)) => {
155 Some(Local { pat_id, parent: self.resolver.body_owner()? }) 155 Some(Local { pat_id, parent: self.resolver.body_owner()? })
156 } 156 }
@@ -181,17 +181,17 @@ impl SourceAnalyzer {
181 181
182 pub(crate) fn resolve_macro_call( 182 pub(crate) fn resolve_macro_call(
183 &self, 183 &self,
184 db: &impl HirDatabase, 184 db: &dyn HirDatabase,
185 macro_call: InFile<&ast::MacroCall>, 185 macro_call: InFile<&ast::MacroCall>,
186 ) -> Option<MacroDef> { 186 ) -> Option<MacroDef> {
187 let hygiene = Hygiene::new(db, macro_call.file_id); 187 let hygiene = Hygiene::new(db.upcast(), macro_call.file_id);
188 let path = macro_call.value.path().and_then(|ast| Path::from_src(ast, &hygiene))?; 188 let path = macro_call.value.path().and_then(|ast| Path::from_src(ast, &hygiene))?;
189 self.resolver.resolve_path_as_macro(db, path.mod_path()).map(|it| it.into()) 189 self.resolver.resolve_path_as_macro(db.upcast(), path.mod_path()).map(|it| it.into())
190 } 190 }
191 191
192 pub(crate) fn resolve_bind_pat_to_const( 192 pub(crate) fn resolve_bind_pat_to_const(
193 &self, 193 &self,
194 db: &impl HirDatabase, 194 db: &dyn HirDatabase,
195 pat: &ast::BindPat, 195 pat: &ast::BindPat,
196 ) -> Option<ModuleDef> { 196 ) -> Option<ModuleDef> {
197 let pat_id = self.pat_id(&pat.clone().into())?; 197 let pat_id = self.pat_id(&pat.clone().into())?;
@@ -209,7 +209,7 @@ impl SourceAnalyzer {
209 209
210 pub(crate) fn resolve_path( 210 pub(crate) fn resolve_path(
211 &self, 211 &self,
212 db: &impl HirDatabase, 212 db: &dyn HirDatabase,
213 path: &ast::Path, 213 path: &ast::Path,
214 ) -> Option<PathResolution> { 214 ) -> Option<PathResolution> {
215 if let Some(path_expr) = path.syntax().parent().and_then(ast::PathExpr::cast) { 215 if let Some(path_expr) = path.syntax().parent().and_then(ast::PathExpr::cast) {
@@ -231,11 +231,12 @@ impl SourceAnalyzer {
231 231
232 pub(crate) fn expand( 232 pub(crate) fn expand(
233 &self, 233 &self,
234 db: &impl HirDatabase, 234 db: &dyn HirDatabase,
235 macro_call: InFile<&ast::MacroCall>, 235 macro_call: InFile<&ast::MacroCall>,
236 ) -> Option<HirFileId> { 236 ) -> Option<HirFileId> {
237 let macro_call_id = 237 let macro_call_id = macro_call.as_call_id(db.upcast(), |path| {
238 macro_call.as_call_id(db, |path| self.resolver.resolve_path_as_macro(db, &path))?; 238 self.resolver.resolve_path_as_macro(db.upcast(), &path)
239 })?;
239 Some(macro_call_id.as_file()) 240 Some(macro_call_id.as_file())
240 } 241 }
241} 242}
@@ -283,42 +284,46 @@ fn scope_for_offset(
283} 284}
284 285
285pub(crate) fn resolve_hir_path( 286pub(crate) fn resolve_hir_path(
286 db: &impl HirDatabase, 287 db: &dyn HirDatabase,
287 resolver: &Resolver, 288 resolver: &Resolver,
288 path: &crate::Path, 289 path: &crate::Path,
289) -> Option<PathResolution> { 290) -> Option<PathResolution> {
290 let types = resolver.resolve_path_in_type_ns_fully(db, path.mod_path()).map(|ty| match ty { 291 let types =
291 TypeNs::SelfType(it) => PathResolution::SelfType(it.into()), 292 resolver.resolve_path_in_type_ns_fully(db.upcast(), path.mod_path()).map(|ty| match ty {
292 TypeNs::GenericParam(id) => PathResolution::TypeParam(TypeParam { id }), 293 TypeNs::SelfType(it) => PathResolution::SelfType(it.into()),
293 TypeNs::AdtSelfType(it) | TypeNs::AdtId(it) => PathResolution::Def(Adt::from(it).into()), 294 TypeNs::GenericParam(id) => PathResolution::TypeParam(TypeParam { id }),
294 TypeNs::EnumVariantId(it) => PathResolution::Def(EnumVariant::from(it).into()), 295 TypeNs::AdtSelfType(it) | TypeNs::AdtId(it) => {
295 TypeNs::TypeAliasId(it) => PathResolution::Def(TypeAlias::from(it).into()), 296 PathResolution::Def(Adt::from(it).into())
296 TypeNs::BuiltinType(it) => PathResolution::Def(it.into()),
297 TypeNs::TraitId(it) => PathResolution::Def(Trait::from(it).into()),
298 });
299 let body_owner = resolver.body_owner();
300 let values = resolver.resolve_path_in_value_ns_fully(db, path.mod_path()).and_then(|val| {
301 let res = match val {
302 ValueNs::LocalBinding(pat_id) => {
303 let var = Local { parent: body_owner?.into(), pat_id };
304 PathResolution::Local(var)
305 } 297 }
306 ValueNs::FunctionId(it) => PathResolution::Def(Function::from(it).into()), 298 TypeNs::EnumVariantId(it) => PathResolution::Def(EnumVariant::from(it).into()),
307 ValueNs::ConstId(it) => PathResolution::Def(Const::from(it).into()), 299 TypeNs::TypeAliasId(it) => PathResolution::Def(TypeAlias::from(it).into()),
308 ValueNs::StaticId(it) => PathResolution::Def(Static::from(it).into()), 300 TypeNs::BuiltinType(it) => PathResolution::Def(it.into()),
309 ValueNs::StructId(it) => PathResolution::Def(Struct::from(it).into()), 301 TypeNs::TraitId(it) => PathResolution::Def(Trait::from(it).into()),
310 ValueNs::EnumVariantId(it) => PathResolution::Def(EnumVariant::from(it).into()), 302 });
311 }; 303 let body_owner = resolver.body_owner();
312 Some(res) 304 let values =
313 }); 305 resolver.resolve_path_in_value_ns_fully(db.upcast(), path.mod_path()).and_then(|val| {
306 let res = match val {
307 ValueNs::LocalBinding(pat_id) => {
308 let var = Local { parent: body_owner?.into(), pat_id };
309 PathResolution::Local(var)
310 }
311 ValueNs::FunctionId(it) => PathResolution::Def(Function::from(it).into()),
312 ValueNs::ConstId(it) => PathResolution::Def(Const::from(it).into()),
313 ValueNs::StaticId(it) => PathResolution::Def(Static::from(it).into()),
314 ValueNs::StructId(it) => PathResolution::Def(Struct::from(it).into()),
315 ValueNs::EnumVariantId(it) => PathResolution::Def(EnumVariant::from(it).into()),
316 };
317 Some(res)
318 });
314 319
315 let items = resolver 320 let items = resolver
316 .resolve_module_path_in_items(db, path.mod_path()) 321 .resolve_module_path_in_items(db.upcast(), path.mod_path())
317 .take_types() 322 .take_types()
318 .map(|it| PathResolution::Def(it.into())); 323 .map(|it| PathResolution::Def(it.into()));
319 types.or(values).or(items).or_else(|| { 324 types.or(values).or(items).or_else(|| {
320 resolver 325 resolver
321 .resolve_path_as_macro(db, path.mod_path()) 326 .resolve_path_as_macro(db.upcast(), path.mod_path())
322 .map(|def| PathResolution::Macro(def.into())) 327 .map(|def| PathResolution::Macro(def.into()))
323 }) 328 })
324} 329}