Skip to content

Commit d800cfa

Browse files
committed
Handle typealiases
1 parent 023b0db commit d800cfa

File tree

2 files changed

+65
-14
lines changed

2 files changed

+65
-14
lines changed

lib/Index/Index.cpp

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -878,6 +878,10 @@ class IndexSwiftASTWalker : public SourceEntityWalker {
878878
auto AfterDollar = Loc.getAdvancedLoc(1);
879879
reportRef(Wrapped, AfterDollar, Info, None);
880880
}
881+
} else if (auto *TAD = dyn_cast<TypeAliasDecl>(D)) {
882+
TypeLoc TL(TAD->getUnderlyingTypeRepr(), TAD->getUnderlyingType());
883+
if (!reportRelatedTypeRef(TL, (SymbolRoleSet)SymbolRole::Reference, D, /*isImplicit=*/true, Loc))
884+
return false;
881885
}
882886

883887
return true;
@@ -962,7 +966,8 @@ class IndexSwiftASTWalker : public SourceEntityWalker {
962966
bool startEntityDecl(ValueDecl *D);
963967

964968
bool reportRelatedRef(ValueDecl *D, SourceLoc Loc, bool isImplicit, SymbolRoleSet Relations, Decl *Related);
965-
bool reportRelatedTypeRef(const TypeLoc &Ty, SymbolRoleSet Relations, Decl *Related);
969+
bool reportRelatedTypeRef(const TypeLoc &Ty, SymbolRoleSet Relations, Decl *Related,
970+
bool isImplicit=false, const Optional<SourceLoc> Loc=Optional<SourceLoc>());
966971
bool reportInheritedTypeRefs(
967972
ArrayRef<InheritedEntry> Inherited, Decl *Inheritee);
968973
NominalTypeDecl *getTypeLocAsNominalTypeDecl(const TypeLoc &Ty);
@@ -1372,32 +1377,43 @@ bool IndexSwiftASTWalker::reportRelatedRef(ValueDecl *D, SourceLoc Loc, bool isI
13721377

13731378
bool IndexSwiftASTWalker::reportInheritedTypeRefs(ArrayRef<InheritedEntry> Inherited, Decl *Inheritee) {
13741379
for (auto Base : Inherited) {
1375-
if (auto *composite = llvm::dyn_cast_or_null<CompositionTypeRepr>(Base.getTypeRepr())) {
1376-
for (auto *type : composite->getTypes())
1377-
if (!reportRelatedTypeRef(type, (SymbolRoleSet) SymbolRole::RelationBaseOf, Inheritee))
1378-
return false;
1379-
} else {
1380-
if (!reportRelatedTypeRef(Base, (SymbolRoleSet) SymbolRole::RelationBaseOf, Inheritee))
1381-
return false;
1382-
}
1380+
if (!reportRelatedTypeRef(Base, (SymbolRoleSet) SymbolRole::RelationBaseOf, Inheritee))
1381+
return false;
13831382
}
13841383
return true;
13851384
}
13861385

1387-
bool IndexSwiftASTWalker::reportRelatedTypeRef(const TypeLoc &Ty, SymbolRoleSet Relations, Decl *Related) {
1388-
if (auto *declRefTR = dyn_cast_or_null<DeclRefTypeRepr>(Ty.getTypeRepr())) {
1389-
SourceLoc IdLoc = declRefTR->getLoc();
1386+
bool IndexSwiftASTWalker::reportRelatedTypeRef(const TypeLoc &Ty, SymbolRoleSet Relations,
1387+
Decl *Related, bool Implicit, const Optional<SourceLoc> Loc) {
1388+
if (auto *composite = llvm::dyn_cast_or_null<CompositionTypeRepr>(Ty.getTypeRepr())) {
1389+
SourceLoc IdLoc = Loc.value_or(composite->getSourceLoc());
1390+
for (auto *Type : composite->getTypes())
1391+
if (!reportRelatedTypeRef(Type, Relations, Related, /*isImplicit=*/Implicit, IdLoc))
1392+
return false;
1393+
1394+
return true;
1395+
} else if (auto *declRefTR = dyn_cast_or_null<DeclRefTypeRepr>(Ty.getTypeRepr())) {
1396+
SourceLoc IdLoc = Loc.value_or(declRefTR->getLoc());
13901397
NominalTypeDecl *NTD = nullptr;
1391-
bool isImplicit = false;
1398+
bool isImplicit = Implicit;
13921399
if (auto *VD = declRefTR->getBoundDecl()) {
13931400
if (auto *TAD = dyn_cast<TypeAliasDecl>(VD)) {
13941401
IndexSymbol Info;
1402+
Info.roles |= Relations;
1403+
if (isImplicit)
1404+
Info.roles |= (unsigned)SymbolRole::Implicit;
13951405
if (!reportRef(TAD, IdLoc, Info, None))
13961406
return false;
13971407
if (auto Ty = TAD->getUnderlyingType()) {
13981408
NTD = Ty->getAnyNominal();
13991409
isImplicit = true;
14001410
}
1411+
1412+
if (isa<CompositionTypeRepr>(TAD->getUnderlyingTypeRepr())) {
1413+
TypeLoc TL(TAD->getUnderlyingTypeRepr(), TAD->getUnderlyingType());
1414+
if (!reportRelatedTypeRef(TL, Relations, Related, /*isImplicit=*/true, IdLoc))
1415+
return false;
1416+
}
14011417
} else {
14021418
NTD = dyn_cast<NominalTypeDecl>(VD);
14031419
}

test/Index/conformances.swift

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,45 @@ class SubMultiConf: BaseMultiConf,P2,P1,P3 { // CHECK: [[@LINE]]:7 | class/Swift
8181

8282
class CompositionType: BaseMultiConf & P1 { // CHECK: [[@LINE]]:7 | class/Swift | CompositionType | [[CompositionType_USR:.*]] | Def
8383
// CHECK: [[@LINE-1]]:24 | class/Swift | BaseMultiConf | [[BaseMultiConf_USR]] | Ref,RelBase | rel: 1
84-
// CHECK: [[@LINE-2]]:40 | protocol/Swift | P1 | [[P1_USR]] | Ref,RelBase | rel: 1
84+
// CHECK: [[@LINE-2]]:24 | protocol/Swift | P1 | [[P1_USR]] | Ref,RelBase | rel: 1
8585
func foo() {}
8686
}
8787

88+
typealias CompositionTypeAlias = BaseMultiConf & P1 // CHECK: [[@LINE]]:11 | type-alias/Swift | CompositionTypeAlias | [[CompositionTypeAlias_USR:.*]] | Def
89+
// CHECK: [[@LINE-1]]:34 | class/Swift | BaseMultiConf | [[BaseMultiConf_USR]] | Ref | rel: 0
90+
// CHECK: [[@LINE-2]]:50 | protocol/Swift | P1 | [[P1_USR]] | Ref | rel: 0
91+
92+
class CompositionTypeViaAlias: CompositionTypeAlias { // CHECK: [[@LINE]]:7 | class/Swift | CompositionTypeViaAlias | [[CompositionTypeViaAlias_USR:.*]] | Def
93+
// CHECK: [[@LINE-1]]:32 | type-alias/Swift | CompositionTypeAlias | [[CompositionTypeAlias_USR]] | Ref,RelBase | rel: 0
94+
// CHECK: [[@LINE-2]]:32 | class/Swift | BaseMultiConf | [[BaseMultiConf_USR]] | Ref,Impl,RelBase | rel: 1
95+
// CHECK: [[@LINE-3]]:32 | protocol/Swift | P1 | [[P1_USR]] | Ref,Impl,RelBase | rel: 1
96+
func foo() {}
97+
}
98+
99+
typealias NestedCompositionTypeAlias = CompositionTypeAlias & P2 // CHECK: [[@LINE]]:11 | type-alias/Swift | NestedCompositionTypeAlias | [[NestedCompositionTypeAlias_USR:.*]] | Def
100+
// CHECK: [[@LINE-1]]:40 | type-alias/Swift | CompositionTypeAlias | [[CompositionTypeAlias_USR]] | Ref | rel: 0
101+
// CHECK: [[@LINE-2]]:40 | class/Swift | BaseMultiConf | [[BaseMultiConf_USR]] | Ref,Impl | rel: 1
102+
// CHECK: [[@LINE-3]]:40 | protocol/Swift | P1 | [[P1_USR]] | Ref,Impl | rel: 1
103+
// CHECK: [[@LINE-4]]:63 | protocol/Swift | P2 | [[P2_USR]] | Ref | rel: 0
104+
105+
class CompositionViaNestedAlias: NestedCompositionTypeAlias { // CHECK: [[@LINE]]:7 | class/Swift | CompositionViaNestedAlias | [[CompositionViaNestedAlias_USR:.*]] | Def
106+
// CHECK: [[@LINE-1]]:34 | type-alias/Swift | NestedCompositionTypeAlias | [[NestedCompositionTypeAlias_USR]] | Ref,RelBase | rel: 0
107+
// CHECK: [[@LINE-2]]:34 | class/Swift | BaseMultiConf | [[BaseMultiConf_USR]] | Ref,Impl,RelBase | rel: 1
108+
// CHECK: [[@LINE-3]]:34 | protocol/Swift | P1 | [[P1_USR]] | Ref,Impl,RelBase | rel: 1
109+
// CHECK: [[@LINE-4]]:34 | protocol/Swift | P2 | [[P2_USR]] | Ref,Impl,RelBase | rel: 1
110+
func foo() {}
111+
}
112+
113+
typealias ProtocolsOnly = P1 & P2 // CHECK: [[@LINE]]:11 | type-alias/Swift | ProtocolsOnly | [[ProtocolsOnly_USR:.*]] | Def
114+
// CHECK: [[@LINE-1]]:27 | protocol/Swift | P1 | [[P1_USR]] | Ref | rel: 0
115+
// CHECK: [[@LINE-2]]:32 | protocol/Swift | P2 | [[P2_USR]] | Ref | rel: 0
116+
117+
class NoInherited {} // CHECK: [[@LINE]]:7 | class/Swift | NoInherited | [[NoInherited_USR:.*]] | Def
118+
extension NoInherited: ProtocolsOnly {} // CHECK: [[@LINE]]:11 | class/Swift | NoInherited | [[NoInherited_USR:.*]] | Ref
119+
// CHECK: [[@LINE-1]]:24 | type-alias/Swift | ProtocolsOnly | [[ProtocolsOnly_USR]] | Ref | rel: 0
120+
// CHECK: [[@LINE-2]]:24 | protocol/Swift | P1 | [[P1_USR]] | Ref,Impl | rel: 1
121+
// CHECK: [[@LINE-3]]:24 | protocol/Swift | P2 | [[P2_USR]] | Ref,Impl | rel: 1
122+
88123
protocol InheritingP: P1 { // CHECK: [[@LINE]]:10 | protocol/Swift | InheritingP | [[InheritingP_USR:.*]] | Def
89124
func foo() // CHECK: [[@LINE]]:8 | instance-method/Swift | foo() | [[InheritingP_foo_USR:.*]] | Def,Dyn,RelChild,RelOver | rel: 2
90125
// CHECK-NEXT: RelOver | instance-method/Swift | foo() | s:14swift_ide_test2P1P3fooyyF

0 commit comments

Comments
 (0)