Skip to content

Commit df81e51

Browse files
committed
AST: Add TypeBase::getRootGenericParam() utility method
1 parent 64400c2 commit df81e51

File tree

5 files changed

+20
-19
lines changed

5 files changed

+20
-19
lines changed

include/swift/AST/Types.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,9 @@ class alignas(1 << TypeAlignInBits) TypeBase {
516516
return getRecursiveProperties().hasTypeParameter();
517517
}
518518

519+
/// Return the root generic parameter of this type parameter type.
520+
GenericTypeParamType *getRootGenericParam();
521+
519522
/// Determines whether this type is an lvalue. This includes both straight
520523
/// lvalue types as well as tuples or optionals of lvalues.
521524
bool isLValueType() {
@@ -4370,6 +4373,15 @@ inline bool TypeBase::isTypeParameter() {
43704373
return false;
43714374
}
43724375

4376+
inline GenericTypeParamType *TypeBase::getRootGenericParam() {
4377+
Type t(this);
4378+
4379+
while (auto *memberTy = t->getAs<DependentMemberType>())
4380+
t = memberTy->getBase();
4381+
4382+
return t->castTo<GenericTypeParamType>();
4383+
}
4384+
43734385
inline bool TypeBase::isExistentialType() {
43744386
return getCanonicalType().isExistentialType();
43754387
}

lib/AST/ASTMangler.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1468,12 +1468,10 @@ static bool isMethodDecl(const Decl *decl) {
14681468
}
14691469

14701470
static bool genericParamIsBelowDepth(Type type, unsigned methodDepth) {
1471-
if (auto gp = type->getAs<GenericTypeParamType>()) {
1471+
if (type->isTypeParameter()) {
1472+
auto gp = type->getRootGenericParam();
14721473
return gp->getDepth() >= methodDepth;
14731474
}
1474-
if (auto dm = type->getAs<DependentMemberType>()) {
1475-
return genericParamIsBelowDepth(dm->getBase(), methodDepth);
1476-
}
14771475
// Non-dependent types in a same-type requirement don't affect whether we
14781476
// mangle the requirement.
14791477
return false;

lib/AST/Decl.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2914,10 +2914,7 @@ findProtocolSelfReferences(const ProtocolDecl *proto, Type type,
29142914

29152915
// Special handling for associated types.
29162916
if (!skipAssocTypes && type->is<DependentMemberType>()) {
2917-
while (auto depMemTy = type->getAs<DependentMemberType>()) {
2918-
type = depMemTy->getBase();
2919-
}
2920-
2917+
type = type->getRootGenericParam();
29212918
if (proto->getProtocolSelfType()->isEqual(type))
29222919
return SelfReferenceKind::Other();
29232920
}

lib/AST/Mangle.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -470,12 +470,10 @@ static bool isMethodDecl(const Decl *decl) {
470470
}
471471

472472
static bool genericParamIsBelowDepth(Type type, unsigned methodDepth) {
473-
if (auto gp = type->getAs<GenericTypeParamType>()) {
473+
if (type->isTypeParameter()) {
474+
auto gp = type->getRootGenericParam();
474475
return gp->getDepth() >= methodDepth;
475476
}
476-
if (auto dm = type->getAs<DependentMemberType>()) {
477-
return genericParamIsBelowDepth(dm->getBase(), methodDepth);
478-
}
479477
// Non-dependent types in a same-type requirement don't affect whether we
480478
// mangle the requirement.
481479
return false;

lib/Sema/TypeCheckGeneric.cpp

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -558,13 +558,9 @@ static bool isSelfDerivedOrConcrete(Type protoSelf, Type type) {
558558
if (!type->hasTypeParameter())
559559
return true;
560560

561-
// Unwrap dependent member types.
562-
while (auto depMem = type->getAs<DependentMemberType>()) {
563-
type = depMem->getBase();
564-
}
565-
566-
if (type->is<GenericTypeParamType>())
567-
return type->isEqual(protoSelf);
561+
if (type->isTypeParameter() &&
562+
type->getRootGenericParam()->isEqual(protoSelf))
563+
return true;
568564

569565
return false;
570566
}

0 commit comments

Comments
 (0)