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.rs125
1 files changed, 117 insertions, 8 deletions
diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs
index 1a9f6cc76..a379b9f49 100644
--- a/crates/ra_hir/src/code_model.rs
+++ b/crates/ra_hir/src/code_model.rs
@@ -26,15 +26,12 @@ use hir_ty::{
26 autoderef, 26 autoderef,
27 display::{HirDisplayError, HirFormatter}, 27 display::{HirDisplayError, HirFormatter},
28 expr::ExprValidator, 28 expr::ExprValidator,
29 method_resolution, ApplicationTy, Canonical, InEnvironment, Substs, TraitEnvironment, Ty, 29 method_resolution, ApplicationTy, Canonical, GenericPredicate, InEnvironment, Substs,
30 TyDefId, TypeCtor, 30 TraitEnvironment, Ty, TyDefId, TypeCtor,
31}; 31};
32use ra_db::{CrateId, CrateName, Edition, FileId}; 32use ra_db::{CrateId, CrateName, Edition, FileId};
33use ra_prof::profile; 33use ra_prof::profile;
34use ra_syntax::{ 34use ra_syntax::ast::{self, AttrsOwner, NameOwner};
35 ast::{self, AttrsOwner, NameOwner},
36 AstNode,
37};
38use rustc_hash::FxHashSet; 35use rustc_hash::FxHashSet;
39 36
40use crate::{ 37use crate::{
@@ -186,10 +183,27 @@ impl ModuleDef {
186 183
187 module.visibility_of(db, self) 184 module.visibility_of(db, self)
188 } 185 }
186
187 pub fn name(self, db: &dyn HirDatabase) -> Option<Name> {
188 match self {
189 ModuleDef::Adt(it) => Some(it.name(db)),
190 ModuleDef::Trait(it) => Some(it.name(db)),
191 ModuleDef::Function(it) => Some(it.name(db)),
192 ModuleDef::EnumVariant(it) => Some(it.name(db)),
193 ModuleDef::TypeAlias(it) => Some(it.name(db)),
194
195 ModuleDef::Module(it) => it.name(db),
196 ModuleDef::Const(it) => it.name(db),
197 ModuleDef::Static(it) => it.name(db),
198
199 ModuleDef::BuiltinType(it) => Some(it.as_name()),
200 }
201 }
189} 202}
190 203
191pub use hir_def::{ 204pub use hir_def::{
192 attr::Attrs, item_scope::ItemInNs, visibility::Visibility, AssocItemId, AssocItemLoc, 205 attr::Attrs, item_scope::ItemInNs, item_tree::ItemTreeNode, visibility::Visibility,
206 AssocItemId, AssocItemLoc,
193}; 207};
194 208
195impl Module { 209impl Module {
@@ -856,7 +870,7 @@ where
856 ID: Lookup<Data = AssocItemLoc<AST>>, 870 ID: Lookup<Data = AssocItemLoc<AST>>,
857 DEF: From<ID>, 871 DEF: From<ID>,
858 CTOR: FnOnce(DEF) -> AssocItem, 872 CTOR: FnOnce(DEF) -> AssocItem,
859 AST: AstNode, 873 AST: ItemTreeNode,
860{ 874{
861 match id.lookup(db.upcast()).container { 875 match id.lookup(db.upcast()).container {
862 AssocContainerId::TraitId(_) | AssocContainerId::ImplId(_) => Some(ctor(DEF::from(id))), 876 AssocContainerId::TraitId(_) | AssocContainerId::ImplId(_) => Some(ctor(DEF::from(id))),
@@ -1359,6 +1373,27 @@ impl Type {
1359 Some(adt.into()) 1373 Some(adt.into())
1360 } 1374 }
1361 1375
1376 pub fn as_dyn_trait(&self) -> Option<Trait> {
1377 self.ty.value.dyn_trait().map(Into::into)
1378 }
1379
1380 pub fn as_impl_traits(&self, db: &dyn HirDatabase) -> Option<Vec<Trait>> {
1381 self.ty.value.impl_trait_bounds(db).map(|it| {
1382 it.into_iter()
1383 .filter_map(|pred| match pred {
1384 hir_ty::GenericPredicate::Implemented(trait_ref) => {
1385 Some(Trait::from(trait_ref.trait_))
1386 }
1387 _ => None,
1388 })
1389 .collect()
1390 })
1391 }
1392
1393 pub fn as_associated_type_parent_trait(&self, db: &dyn HirDatabase) -> Option<Trait> {
1394 self.ty.value.associated_type_parent_trait(db).map(Into::into)
1395 }
1396
1362 // FIXME: provide required accessors such that it becomes implementable from outside. 1397 // FIXME: provide required accessors such that it becomes implementable from outside.
1363 pub fn is_equal_for_find_impls(&self, other: &Type) -> bool { 1398 pub fn is_equal_for_find_impls(&self, other: &Type) -> bool {
1364 match (&self.ty.value, &other.ty.value) { 1399 match (&self.ty.value, &other.ty.value) {
@@ -1380,6 +1415,80 @@ impl Type {
1380 ty: InEnvironment { value: ty, environment: self.ty.environment.clone() }, 1415 ty: InEnvironment { value: ty, environment: self.ty.environment.clone() },
1381 } 1416 }
1382 } 1417 }
1418
1419 pub fn walk(&self, db: &dyn HirDatabase, mut cb: impl FnMut(Type)) {
1420 // TypeWalk::walk for a Ty at first visits parameters and only after that the Ty itself.
1421 // We need a different order here.
1422
1423 fn walk_substs(
1424 db: &dyn HirDatabase,
1425 type_: &Type,
1426 substs: &Substs,
1427 cb: &mut impl FnMut(Type),
1428 ) {
1429 for ty in substs.iter() {
1430 walk_type(db, &type_.derived(ty.clone()), cb);
1431 }
1432 }
1433
1434 fn walk_bounds(
1435 db: &dyn HirDatabase,
1436 type_: &Type,
1437 bounds: &[GenericPredicate],
1438 cb: &mut impl FnMut(Type),
1439 ) {
1440 for pred in bounds {
1441 match pred {
1442 GenericPredicate::Implemented(trait_ref) => {
1443 cb(type_.clone());
1444 walk_substs(db, type_, &trait_ref.substs, cb);
1445 }
1446 _ => (),
1447 }
1448 }
1449 }
1450
1451 fn walk_type(db: &dyn HirDatabase, type_: &Type, cb: &mut impl FnMut(Type)) {
1452 let ty = type_.ty.value.strip_references();
1453 match ty {
1454 Ty::Apply(ApplicationTy { ctor, parameters }) => {
1455 match ctor {
1456 TypeCtor::Adt(_) => {
1457 cb(type_.derived(ty.clone()));
1458 }
1459 TypeCtor::AssociatedType(_) => {
1460 if let Some(_) = ty.associated_type_parent_trait(db) {
1461 cb(type_.derived(ty.clone()));
1462 }
1463 }
1464 _ => (),
1465 }
1466
1467 // adt params, tuples, etc...
1468 walk_substs(db, type_, parameters, cb);
1469 }
1470 Ty::Opaque(opaque_ty) => {
1471 if let Some(bounds) = ty.impl_trait_bounds(db) {
1472 walk_bounds(db, &type_.derived(ty.clone()), &bounds, cb);
1473 }
1474
1475 walk_substs(db, type_, &opaque_ty.parameters, cb);
1476 }
1477 Ty::Placeholder(_) => {
1478 if let Some(bounds) = ty.impl_trait_bounds(db) {
1479 walk_bounds(db, &type_.derived(ty.clone()), &bounds, cb);
1480 }
1481 }
1482 Ty::Dyn(bounds) => {
1483 walk_bounds(db, &type_.derived(ty.clone()), bounds.as_ref(), cb);
1484 }
1485
1486 _ => (),
1487 }
1488 }
1489
1490 walk_type(db, self, &mut cb);
1491 }
1383} 1492}
1384 1493
1385impl HirDisplay for Type { 1494impl HirDisplay for Type {