Skip to content

Commit 548374d

Browse files
committed
[Index] Split up reportRelatedTypeRef
There's really two separate code paths here, one for handling cases where we have a TypeRepr available, and one for handling cases where we're e.g indexing a swiftmodule. IMO it's simpler to handle these cases separately.
1 parent 5e8c1a4 commit 548374d

File tree

1 file changed

+97
-43
lines changed

1 file changed

+97
-43
lines changed

lib/Index/Index.cpp

Lines changed: 97 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1028,17 +1028,46 @@ class IndexSwiftASTWalker : public SourceEntityWalker {
10281028

10291029
bool reportRelatedRef(ValueDecl *D, SourceLoc Loc, bool isImplicit, SymbolRoleSet Relations, Decl *Related);
10301030

1031-
/// Report references for dependent types
1031+
/// Report a related type relation for a given TypeRepr.
10321032
///
1033-
/// NOTE: If the dependent type is a typealias, report the underlying types as well.
1033+
/// NOTE: If the dependent type is a typealias, report the underlying types as
1034+
/// well.
1035+
///
1036+
/// \param TR The type being referenced.
1037+
/// \param Relations The relationship between the referenced type and the
1038+
/// passed Decl.
1039+
/// \param Related The Decl that is referencing the type.
1040+
/// \param Implicit Whether the reference is implicit, such as for a
1041+
/// typealias' underlying type.
1042+
/// \param ParentLoc The parent location of the reference that should be used
1043+
/// for implicit references.
1044+
bool reportRelatedTypeRepr(const TypeRepr *TR, SymbolRoleSet Relations,
1045+
Decl *Related, bool Implicit, SourceLoc ParentLoc);
1046+
1047+
/// Report a related type relation for a Type at a given location.
10341048
///
10351049
/// \param Ty The type being referenced.
1036-
/// \param Relations The relationship between the referenced type and the passed Decl.
1050+
/// \param Relations The relationship between the referenced type and the
1051+
/// passed Decl.
10371052
/// \param Related The Decl that is referencing the type.
1038-
/// \param isImplicit Whether the reference is implicit, such as for a typealias' underlying type.
1039-
/// \param Loc The location of the reference, otherwise the location of the TypeLoc is used.
1040-
bool reportRelatedTypeRef(const TypeLoc &Ty, SymbolRoleSet Relations, Decl *Related,
1041-
bool isImplicit=false, SourceLoc Loc={});
1053+
/// \param Implicit Whether the reference is implicit, such as for a
1054+
/// typealias' underlying type.
1055+
/// \param Loc The location of the reference.
1056+
bool reportRelatedType(Type Ty, SymbolRoleSet Relations, Decl *Related,
1057+
bool Implicit, SourceLoc Loc);
1058+
1059+
/// Report references for dependent types.
1060+
///
1061+
/// NOTE: If the dependent type is a typealias, report the underlying types as
1062+
/// well.
1063+
///
1064+
/// \param TL The type being referenced.
1065+
/// \param Relations The relationship between the referenced type and the
1066+
/// passed Decl.
1067+
/// \param Related The Decl that is referencing the type.
1068+
bool reportRelatedTypeRef(const TypeLoc &TL, SymbolRoleSet Relations,
1069+
Decl *Related);
1070+
10421071
bool reportInheritedTypeRefs(InheritedTypes Inherited, Decl *Inheritee);
10431072

10441073
bool reportPseudoGetterDecl(VarDecl *D) {
@@ -1483,51 +1512,76 @@ bool IndexSwiftASTWalker::reportInheritedTypeRefs(InheritedTypes Inherited,
14831512
return true;
14841513
}
14851514

1486-
bool IndexSwiftASTWalker::reportRelatedTypeRef(const TypeLoc &Ty, SymbolRoleSet Relations,
1487-
Decl *Related, bool Implicit, SourceLoc Loc) {
1488-
if (auto *composite = llvm::dyn_cast_or_null<CompositionTypeRepr>(Ty.getTypeRepr())) {
1515+
bool IndexSwiftASTWalker::reportRelatedTypeRepr(const TypeRepr *TR,
1516+
SymbolRoleSet Relations,
1517+
Decl *Related, bool Implicit,
1518+
SourceLoc ParentLoc) {
1519+
if (auto *composite = dyn_cast<CompositionTypeRepr>(TR)) {
14891520
for (auto *Type : composite->getTypes()) {
1490-
if (!reportRelatedTypeRef(Type, Relations, Related, /*isImplicit=*/Implicit, Loc))
1521+
if (!reportRelatedTypeRepr(Type, Relations, Related, Implicit,
1522+
ParentLoc)) {
14911523
return false;
1524+
}
14921525
}
1526+
}
1527+
auto *declRefTR = dyn_cast<DeclRefTypeRepr>(TR);
1528+
if (!declRefTR)
1529+
return true;
14931530

1531+
auto *VD = declRefTR->getBoundDecl();
1532+
if (!VD)
14941533
return true;
1495-
} else if (auto *declRefTR = dyn_cast_or_null<DeclRefTypeRepr>(Ty.getTypeRepr())) {
1496-
SourceLoc IdLoc = Loc.isValid() ? Loc : declRefTR->getLoc();
1497-
NominalTypeDecl *NTD = nullptr;
1498-
bool isImplicit = Implicit;
1499-
if (auto *VD = declRefTR->getBoundDecl()) {
1500-
if (auto *TAD = dyn_cast<TypeAliasDecl>(VD)) {
1501-
IndexSymbol Info;
1502-
if (isImplicit)
1503-
Info.roles |= (unsigned)SymbolRole::Implicit;
1504-
if (!reportRef(TAD, IdLoc, Info, std::nullopt))
1505-
return false;
1506-
if (auto Ty = TAD->getUnderlyingType()) {
1507-
NTD = Ty->getAnyNominal();
1508-
isImplicit = true;
1509-
}
15101534

1511-
if (isa_and_nonnull<CompositionTypeRepr>(TAD->getUnderlyingTypeRepr())) {
1512-
TypeLoc TL(TAD->getUnderlyingTypeRepr(), TAD->getUnderlyingType());
1513-
if (!reportRelatedTypeRef(TL, Relations, Related, /*isImplicit=*/true, IdLoc))
1514-
return false;
1515-
}
1516-
} else {
1517-
NTD = dyn_cast<NominalTypeDecl>(VD);
1518-
}
1519-
}
1520-
if (NTD) {
1521-
if (!reportRelatedRef(NTD, IdLoc, isImplicit, Relations, Related))
1522-
return false;
1535+
SourceLoc IdLoc = ParentLoc.isValid() ? ParentLoc : declRefTR->getLoc();
1536+
if (auto *TAD = dyn_cast<TypeAliasDecl>(VD)) {
1537+
IndexSymbol Info;
1538+
if (Implicit)
1539+
Info.roles |= (unsigned)SymbolRole::Implicit;
1540+
if (!reportRef(TAD, IdLoc, Info, std::nullopt))
1541+
return false;
1542+
1543+
// Recurse into the underlying type and report any found references as
1544+
// implicit references at the location of the typealias reference.
1545+
if (auto *UTR = TAD->getUnderlyingTypeRepr()) {
1546+
return reportRelatedTypeRepr(UTR, Relations, Related,
1547+
/*Implicit*/ true, /*ParentLoc*/ IdLoc);
15231548
}
1524-
return true;
1549+
// If we don't have a TypeRepr available, this is a typealias in another
1550+
// module, consult the computed underlying type.
1551+
return reportRelatedType(TAD->getUnderlyingType(), Relations, Related,
1552+
/*Implicit*/ true, /*ParentLoc*/ IdLoc);
1553+
}
1554+
if (auto *NTD = dyn_cast<NominalTypeDecl>(VD)) {
1555+
if (!reportRelatedRef(NTD, IdLoc, Implicit, Relations, Related))
1556+
return false;
15251557
}
1558+
return true;
1559+
}
15261560

1527-
if (Ty.getType()) {
1528-
if (auto nominal = Ty.getType()->getAnyNominal())
1529-
if (!reportRelatedRef(nominal, Ty.getLoc(), /*isImplicit=*/false, Relations, Related))
1530-
return false;
1561+
bool IndexSwiftASTWalker::reportRelatedType(Type Ty, SymbolRoleSet Relations,
1562+
Decl *Related, bool Implicit,
1563+
SourceLoc Loc) {
1564+
if (auto *nominal = Ty->getAnyNominal()) {
1565+
if (!reportRelatedRef(nominal, Loc, Implicit, Relations, Related))
1566+
return false;
1567+
}
1568+
return true;
1569+
}
1570+
1571+
bool IndexSwiftASTWalker::reportRelatedTypeRef(const TypeLoc &TL,
1572+
SymbolRoleSet Relations,
1573+
Decl *Related) {
1574+
// If we have a TypeRepr, prefer that since it lets us match up source
1575+
// locations with the code the user wrote.
1576+
if (auto *TR = TL.getTypeRepr()) {
1577+
return reportRelatedTypeRepr(TR, Relations, Related, /*Implicit*/ false,
1578+
/*ParentLoc*/ SourceLoc());
1579+
}
1580+
// Otherwise fall back to reporting the Type, this is necessary when indexing
1581+
// swiftmodules.
1582+
if (auto Ty = TL.getType()) {
1583+
return reportRelatedType(Ty, Relations, Related,
1584+
/*Implicit*/ false, SourceLoc());
15311585
}
15321586
return true;
15331587
}

0 commit comments

Comments
 (0)