Skip to content

Commit 7793f63

Browse files
committed
[NFC] AST: Consolidate some repeated DependentMemberType logic in Type methods
1 parent ddfb26b commit 7793f63

File tree

4 files changed

+32
-42
lines changed

4 files changed

+32
-42
lines changed

include/swift/AST/Types.h

Lines changed: 6 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -821,6 +821,12 @@ class alignas(1 << TypeAlignInBits) TypeBase
821821
/// type variables referenced by this type.
822822
void getTypeVariables(SmallPtrSetImpl<TypeVariableType *> &typeVariables);
823823

824+
private:
825+
/// If the receiver is a `DependentMemberType`, returns its root. Otherwise,
826+
/// returns the receiver.
827+
Type getDependentMemberRoot();
828+
829+
public:
824830
/// Determine whether this type is a type parameter, which is either a
825831
/// GenericTypeParamType or a DependentMemberType.
826832
///
@@ -7862,38 +7868,11 @@ inline ASTContext &TypeBase::getASTContext() const {
78627868
return *const_cast<ASTContext*>(getCanonicalType()->Context);
78637869
}
78647870

7865-
inline bool TypeBase::isTypeVariableOrMember() {
7866-
Type t(this);
7867-
7868-
while (auto *memberTy = t->getAs<DependentMemberType>())
7869-
t = memberTy->getBase();
7870-
7871-
return t->is<TypeVariableType>();
7872-
}
7873-
7874-
inline bool TypeBase::isTypeParameter() {
7875-
Type t(this);
7876-
7877-
while (auto *memberTy = t->getAs<DependentMemberType>())
7878-
t = memberTy->getBase();
7879-
7880-
return t->is<GenericTypeParamType>();
7881-
}
7882-
78837871
// TODO: This will become redundant once InOutType is removed.
78847872
inline bool TypeBase::isMaterializable() {
78857873
return !(hasLValueType() || is<InOutType>());
78867874
}
78877875

7888-
inline GenericTypeParamType *TypeBase::getRootGenericParam() {
7889-
Type t(this);
7890-
7891-
while (auto *memberTy = t->getAs<DependentMemberType>())
7892-
t = memberTy->getBase();
7893-
7894-
return t->castTo<GenericTypeParamType>();
7895-
}
7896-
78977876
inline bool TypeBase::isConstraintType() const {
78987877
return getCanonicalType().isConstraintType();
78997878
}

lib/AST/ParameterPack.cpp

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -177,12 +177,7 @@ void TypeBase::getTypeParameterPacks(
177177
}
178178

179179
bool TypeBase::isParameterPack() {
180-
Type t(this);
181-
182-
while (auto *memberTy = t->getAs<DependentMemberType>())
183-
t = memberTy->getBase();
184-
185-
return t->isRootParameterPack();
180+
return getDependentMemberRoot()->isRootParameterPack();
186181
}
187182

188183
bool TypeBase::isRootParameterPack() {

lib/AST/Type.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -603,6 +603,27 @@ void TypeBase::getTypeVariables(
603603
"Did not find type variables!");
604604
}
605605

606+
Type TypeBase::getDependentMemberRoot() {
607+
Type t(this);
608+
609+
while (auto *dmt = t->getAs<DependentMemberType>())
610+
t = dmt->getBase();
611+
612+
return t;
613+
}
614+
615+
bool TypeBase::isTypeVariableOrMember() {
616+
return getDependentMemberRoot()->is<TypeVariableType>();
617+
}
618+
619+
bool TypeBase::isTypeParameter() {
620+
return getDependentMemberRoot()->is<GenericTypeParamType>();
621+
}
622+
623+
GenericTypeParamType *TypeBase::getRootGenericParam() {
624+
return getDependentMemberRoot()->castTo<GenericTypeParamType>();
625+
}
626+
606627
static bool isLegalSILType(CanType type);
607628

608629
static bool isLegalSILTypeOrPackExpansion(CanType type) {

lib/IRGen/GenMeta.cpp

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2758,15 +2758,10 @@ ProtocolDecl *irgen::opaqueTypeRequiresWitnessTable(
27582758

27592759
// The type itself must be anchored on one of the generic parameters of
27602760
// the opaque type (not an outer context).
2761-
Type subject = req.getFirstType();
2762-
while (auto depMember = subject->getAs<DependentMemberType>()) {
2763-
subject = depMember->getBase();
2764-
}
2765-
2766-
if (auto genericParam = subject->getAs<GenericTypeParamType>()) {
2767-
unsigned opaqueDepth = opaque->getOpaqueGenericParams().front()->getDepth();
2768-
if (genericParam->getDepth() == opaqueDepth)
2769-
return proto;
2761+
auto *genericParam = req.getFirstType()->getRootGenericParam();
2762+
unsigned opaqueDepth = opaque->getOpaqueGenericParams().front()->getDepth();
2763+
if (genericParam->getDepth() == opaqueDepth) {
2764+
return proto;
27702765
}
27712766

27722767
return nullptr;

0 commit comments

Comments
 (0)