aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src/code_model.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_hir/src/code_model.rs')
-rw-r--r--crates/ra_hir/src/code_model.rs121
1 files changed, 10 insertions, 111 deletions
diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs
index 9b6276b51..a132d128b 100644
--- a/crates/ra_hir/src/code_model.rs
+++ b/crates/ra_hir/src/code_model.rs
@@ -30,7 +30,7 @@ use crate::{
30 AstItemDef, ConstId, EnumId, FunctionId, MacroDefId, StaticId, StructId, TraitId, 30 AstItemDef, ConstId, EnumId, FunctionId, MacroDefId, StaticId, StructId, TraitId,
31 TypeAliasId, 31 TypeAliasId,
32 }, 32 },
33 resolve::{Resolver, Scope, TypeNs}, 33 resolve::{HasResolver, TypeNs},
34 ty::{InferenceResult, Namespace, TraitRef}, 34 ty::{InferenceResult, Namespace, TraitRef},
35 Either, HasSource, ImportId, Name, ScopeDef, Source, Ty, 35 Either, HasSource, ImportId, Name, ScopeDef, Source, Ty,
36}; 36};
@@ -223,11 +223,6 @@ impl Module {
223 } 223 }
224 } 224 }
225 225
226 pub(crate) fn resolver(self, db: &impl DefDatabase) -> Resolver {
227 let def_map = db.crate_def_map(self.id.krate);
228 Resolver::default().push_module_scope(def_map, self.id.module_id)
229 }
230
231 pub fn declarations(self, db: &impl DefDatabase) -> Vec<ModuleDef> { 226 pub fn declarations(self, db: &impl DefDatabase) -> Vec<ModuleDef> {
232 let def_map = db.crate_def_map(self.id.krate); 227 let def_map = db.crate_def_map(self.id.krate);
233 def_map[self.id.module_id].scope.declarations().map(ModuleDef::from).collect() 228 def_map[self.id.module_id].scope.declarations().map(ModuleDef::from).collect()
@@ -315,15 +310,6 @@ impl Struct {
315 db.type_for_def(self.into(), Namespace::Values) 310 db.type_for_def(self.into(), Namespace::Values)
316 } 311 }
317 312
318 // FIXME move to a more general type
319 /// Builds a resolver for type references inside this struct.
320 pub(crate) fn resolver(self, db: &impl HirDatabase) -> Resolver {
321 // take the outer scope...
322 let r = self.module(db).resolver(db);
323 // ...and add generic params, if present
324 r.push_generic_params_scope(db, self.into())
325 }
326
327 fn variant_data(self, db: &impl DefDatabase) -> Arc<VariantData> { 313 fn variant_data(self, db: &impl DefDatabase) -> Arc<VariantData> {
328 db.struct_data(self.id.into()).variant_data.clone() 314 db.struct_data(self.id.into()).variant_data.clone()
329 } 315 }
@@ -339,22 +325,13 @@ impl Union {
339 db.struct_data(self.id.into()).name.clone() 325 db.struct_data(self.id.into()).name.clone()
340 } 326 }
341 327
342 pub fn module(self, db: &impl HirDatabase) -> Module { 328 pub fn module(self, db: &impl DefDatabase) -> Module {
343 Module { id: self.id.0.module(db) } 329 Module { id: self.id.0.module(db) }
344 } 330 }
345 331
346 pub fn ty(self, db: &impl HirDatabase) -> Ty { 332 pub fn ty(self, db: &impl HirDatabase) -> Ty {
347 db.type_for_def(self.into(), Namespace::Types) 333 db.type_for_def(self.into(), Namespace::Types)
348 } 334 }
349
350 // FIXME move to a more general type
351 /// Builds a resolver for type references inside this union.
352 pub(crate) fn resolver(self, db: &impl HirDatabase) -> Resolver {
353 // take the outer scope...
354 let r = self.module(db).resolver(db);
355 // ...and add generic params, if present
356 r.push_generic_params_scope(db, self.into())
357 }
358} 335}
359 336
360#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 337#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
@@ -394,16 +371,6 @@ impl Enum {
394 pub fn ty(self, db: &impl HirDatabase) -> Ty { 371 pub fn ty(self, db: &impl HirDatabase) -> Ty {
395 db.type_for_def(self.into(), Namespace::Types) 372 db.type_for_def(self.into(), Namespace::Types)
396 } 373 }
397
398 // FIXME: move to a more general type
399 /// Builds a resolver for type references inside this struct.
400 pub(crate) fn resolver(self, db: &impl HirDatabase) -> Resolver {
401 // take the outer scope...
402 let r = self.module(db).resolver(db);
403 // ...and add generic params, if present
404 let r = r.push_generic_params_scope(db, self.into());
405 r.push_scope(Scope::AdtScope(self.into()))
406 }
407} 374}
408 375
409#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 376#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
@@ -465,24 +432,17 @@ impl Adt {
465 } 432 }
466 } 433 }
467 434
468 pub fn krate(self, db: &impl HirDatabase) -> Option<Crate> { 435 pub fn module(self, db: &impl DefDatabase) -> Module {
469 Some(
470 match self {
471 Adt::Struct(s) => s.module(db),
472 Adt::Union(s) => s.module(db),
473 Adt::Enum(e) => e.module(db),
474 }
475 .krate(),
476 )
477 }
478
479 pub(crate) fn resolver(self, db: &impl HirDatabase) -> Resolver {
480 match self { 436 match self {
481 Adt::Struct(it) => it.resolver(db), 437 Adt::Struct(s) => s.module(db),
482 Adt::Union(it) => it.resolver(db), 438 Adt::Union(s) => s.module(db),
483 Adt::Enum(it) => it.resolver(db), 439 Adt::Enum(e) => e.module(db),
484 } 440 }
485 } 441 }
442
443 pub fn krate(self, db: &impl HirDatabase) -> Option<Crate> {
444 Some(self.module(db).krate())
445 }
486} 446}
487 447
488#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] 448#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
@@ -533,15 +493,6 @@ pub enum DefWithBody {
533impl_froms!(DefWithBody: Function, Const, Static); 493impl_froms!(DefWithBody: Function, Const, Static);
534 494
535impl DefWithBody { 495impl DefWithBody {
536 /// Builds a resolver for code inside this item.
537 pub(crate) fn resolver(self, db: &impl HirDatabase) -> Resolver {
538 match self {
539 DefWithBody::Const(c) => c.resolver(db),
540 DefWithBody::Function(f) => f.resolver(db),
541 DefWithBody::Static(s) => s.resolver(db),
542 }
543 }
544
545 pub(crate) fn krate(self, db: &impl HirDatabase) -> Option<Crate> { 496 pub(crate) fn krate(self, db: &impl HirDatabase) -> Option<Crate> {
546 match self { 497 match self {
547 DefWithBody::Const(c) => c.krate(db), 498 DefWithBody::Const(c) => c.krate(db),
@@ -738,15 +689,6 @@ impl Function {
738 } 689 }
739 } 690 }
740 691
741 // FIXME: move to a more general type for 'body-having' items
742 /// Builds a resolver for code inside this item.
743 pub(crate) fn resolver(self, db: &impl HirDatabase) -> Resolver {
744 // take the outer scope...
745 let r = self.container(db).map_or_else(|| self.module(db).resolver(db), |c| c.resolver(db));
746 // ...and add generic params, if present
747 r.push_generic_params_scope(db, self.into())
748 }
749
750 pub fn diagnostics(self, db: &impl HirDatabase, sink: &mut DiagnosticSink) { 692 pub fn diagnostics(self, db: &impl HirDatabase, sink: &mut DiagnosticSink) {
751 let infer = self.infer(db); 693 let infer = self.infer(db);
752 infer.add_diagnostics(db, self, sink); 694 infer.add_diagnostics(db, self, sink);
@@ -804,17 +746,6 @@ impl Const {
804 ContainerId::ModuleId(_) => None, 746 ContainerId::ModuleId(_) => None,
805 } 747 }
806 } 748 }
807
808 // FIXME: move to a more general type for 'body-having' items
809 /// Builds a resolver for code inside this item.
810 pub(crate) fn resolver(self, db: &impl HirDatabase) -> Resolver {
811 // take the outer scope...
812 let r = self
813 .impl_block(db)
814 .map(|ib| ib.resolver(db))
815 .unwrap_or_else(|| self.module(db).resolver(db));
816 r
817 }
818} 749}
819 750
820#[derive(Debug, Clone, PartialEq, Eq)] 751#[derive(Debug, Clone, PartialEq, Eq)]
@@ -874,12 +805,6 @@ impl Static {
874 db.static_data(self) 805 db.static_data(self)
875 } 806 }
876 807
877 /// Builds a resolver for code inside this item.
878 pub(crate) fn resolver(self, db: &impl HirDatabase) -> Resolver {
879 // take the outer scope...
880 self.module(db).resolver(db)
881 }
882
883 pub fn infer(self, db: &impl HirDatabase) -> Arc<InferenceResult> { 808 pub fn infer(self, db: &impl HirDatabase) -> Arc<InferenceResult> {
884 db.infer(self.into()) 809 db.infer(self.into())
885 } 810 }
@@ -975,12 +900,6 @@ impl Trait {
975 pub fn is_auto(self, db: &impl DefDatabase) -> bool { 900 pub fn is_auto(self, db: &impl DefDatabase) -> bool {
976 self.trait_data(db).auto 901 self.trait_data(db).auto
977 } 902 }
978
979 pub(crate) fn resolver(self, db: &impl DefDatabase) -> Resolver {
980 let r = self.module(db).resolver(db);
981 // add generic params, if present
982 r.push_generic_params_scope(db, self.into())
983 }
984} 903}
985 904
986#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 905#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
@@ -1032,17 +951,6 @@ impl TypeAlias {
1032 pub fn name(self, db: &impl DefDatabase) -> Name { 951 pub fn name(self, db: &impl DefDatabase) -> Name {
1033 db.type_alias_data(self).name.clone() 952 db.type_alias_data(self).name.clone()
1034 } 953 }
1035
1036 /// Builds a resolver for the type references in this type alias.
1037 pub(crate) fn resolver(self, db: &impl HirDatabase) -> Resolver {
1038 // take the outer scope...
1039 let r = self
1040 .impl_block(db)
1041 .map(|ib| ib.resolver(db))
1042 .unwrap_or_else(|| self.module(db).resolver(db));
1043 // ...and add generic params, if present
1044 r.push_generic_params_scope(db, self.into())
1045 }
1046} 954}
1047 955
1048#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 956#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
@@ -1058,15 +966,6 @@ pub enum Container {
1058} 966}
1059impl_froms!(Container: Trait, ImplBlock); 967impl_froms!(Container: Trait, ImplBlock);
1060 968
1061impl Container {
1062 pub(crate) fn resolver(self, db: &impl DefDatabase) -> Resolver {
1063 match self {
1064 Container::Trait(trait_) => trait_.resolver(db),
1065 Container::ImplBlock(impl_block) => impl_block.resolver(db),
1066 }
1067 }
1068}
1069
1070#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] 969#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
1071pub enum AssocItem { 970pub enum AssocItem {
1072 Function(Function), 971 Function(Function),