Skip to content

Commit cb65358

Browse files
committed
Handle typealiases
1 parent 023b0db commit cb65358

File tree

2 files changed

+68
-13
lines changed

2 files changed

+68
-13
lines changed

lib/Index/Index.cpp

Lines changed: 32 additions & 12 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,47 @@ 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())) {
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 = composite->getSourceLoc();
1390+
if (auto ParentLoc = Loc)
1391+
IdLoc = *ParentLoc;
1392+
for (auto *Type : composite->getTypes())
1393+
if (!reportRelatedTypeRef(Type, Relations, Related, /*isImplicit=*/Implicit, IdLoc))
1394+
return false;
1395+
1396+
return true;
1397+
} else if (auto *declRefTR = dyn_cast_or_null<DeclRefTypeRepr>(Ty.getTypeRepr())) {
13891398
SourceLoc IdLoc = declRefTR->getLoc();
1399+
if (auto ParentLoc = Loc)
1400+
IdLoc = *ParentLoc;
13901401
NominalTypeDecl *NTD = nullptr;
1391-
bool isImplicit = false;
1402+
bool isImplicit = Implicit;
13921403
if (auto *VD = declRefTR->getBoundDecl()) {
13931404
if (auto *TAD = dyn_cast<TypeAliasDecl>(VD)) {
13941405
IndexSymbol Info;
1406+
Info.roles |= Relations;
1407+
if (isImplicit)
1408+
Info.roles |= (unsigned)SymbolRole::Implicit;
13951409
if (!reportRef(TAD, IdLoc, Info, None))
13961410
return false;
13971411
if (auto Ty = TAD->getUnderlyingType()) {
13981412
NTD = Ty->getAnyNominal();
13991413
isImplicit = true;
14001414
}
1415+
1416+
if (auto *composite = llvm::dyn_cast_or_null<CompositionTypeRepr>(TAD->getUnderlyingTypeRepr())) {
1417+
TypeLoc TL(TAD->getUnderlyingTypeRepr(), TAD->getUnderlyingType());
1418+
if (!reportRelatedTypeRef(TL, Relations, Related, /*isImplicit=*/true, IdLoc))
1419+
return false;
1420+
}
14011421
} else {
14021422
NTD = dyn_cast<NominalTypeDecl>(VD);
14031423
}

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)