Skip to content

Commit 8f9fcaa

Browse files
authored
Merge pull request #18325 from rudkx/minor-refactoring
[ConstraintSystem] Some small clean-ups to getFixedType, getFixedTypeRecursive, and simplifyType
2 parents 520ae6f + a5da413 commit 8f9fcaa

File tree

2 files changed

+32
-44
lines changed

2 files changed

+32
-44
lines changed

lib/Sema/ConstraintSystem.cpp

Lines changed: 30 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -700,34 +700,41 @@ bool ConstraintSystem::isAnyHashableType(Type type) {
700700
return false;
701701
}
702702

703+
static Type withParens(ConstraintSystem &cs, Type type, ParenType *parenType) {
704+
auto flags = parenType->getParameterFlags().withInOut(type->is<InOutType>());
705+
return ParenType::get(cs.getASTContext(), type->getInOutObjectType(), flags);
706+
}
707+
703708
Type ConstraintSystem::getFixedTypeRecursive(Type type,
704709
TypeMatchOptions &flags,
705710
bool wantRValue,
706711
bool retainParens) {
707712

708-
if (wantRValue)
709-
type = type->getRValueType();
713+
// FIXME: This function doesn't strictly honor retainParens
714+
// everywhere.
710715

711716
if (retainParens) {
712-
if (auto parenTy = dyn_cast<ParenType>(type.getPointer())) {
713-
type = getFixedTypeRecursive(parenTy->getUnderlyingType(), flags,
714-
wantRValue, retainParens);
715-
auto flags = parenTy->getParameterFlags().withInOut(type->is<InOutType>());
716-
return ParenType::get(getASTContext(), type->getInOutObjectType(), flags);
717+
if (wantRValue)
718+
type = type->getRValueType();
719+
720+
if (auto *parenTy = dyn_cast<ParenType>(type.getPointer())) {
721+
auto fixed = getFixedTypeRecursive(parenTy->getUnderlyingType(), flags,
722+
wantRValue, retainParens);
723+
return withParens(*this, fixed, parenTy);
717724
}
718725
}
719726

720727
while (true) {
728+
if (wantRValue)
729+
type = type->getRValueType();
730+
721731
if (auto depMemType = type->getAs<DependentMemberType>()) {
722732
if (!depMemType->getBase()->isTypeVariableOrMember()) return type;
723733

724734
// FIXME: Perform a more limited simplification?
725735
Type newType = simplifyType(type);
726736
if (newType.getPointer() == type.getPointer()) return type;
727737

728-
if (wantRValue)
729-
newType = newType->getRValueType();
730-
731738
type = newType;
732739

733740
// Once we've simplified a dependent member type, we need to generate a
@@ -736,35 +743,21 @@ Type ConstraintSystem::getFixedTypeRecursive(Type type,
736743
continue;
737744
}
738745

739-
if (auto typeVar = type->getAs<TypeVariableType>()) {
740-
bool hasRepresentative = false;
741-
if (auto *repr = getRepresentative(typeVar)) {
742-
if (typeVar != repr) {
743-
hasRepresentative = true;
744-
typeVar = repr;
745-
}
746-
}
747-
748-
if (auto fixed = getFixedType(typeVar)) {
749-
if (wantRValue)
750-
fixed = fixed->getRValueType();
751-
752-
type = fixed;
753-
continue;
754-
}
755-
756-
// If type variable has a representative but
757-
// no fixed type, reflect that in the type itself.
758-
if (hasRepresentative)
759-
type = typeVar;
746+
auto typeVar = type->getAs<TypeVariableType>();
747+
if (!typeVar)
748+
return type;
760749

761-
break;
750+
if (auto fixed = getFixedType(typeVar)) {
751+
type = fixed;
752+
continue;
762753
}
763754

764-
break;
765-
}
755+
if (retainParens)
756+
if (auto *parenType = dyn_cast<ParenType>(type.getPointer()))
757+
return withParens(*this, getRepresentative(typeVar), parenType);
766758

767-
return type;
759+
return getRepresentative(typeVar);
760+
}
768761
}
769762

770763
/// Does a var or subscript produce an l-value?
@@ -2022,12 +2015,10 @@ Type ConstraintSystem::simplifyType(Type type) {
20222015
return simplifyTypeImpl(
20232016
*this, type,
20242017
[&](TypeVariableType *tvt) -> Type {
2025-
tvt = getRepresentative(tvt);
2026-
if (auto fixed = getFixedType(tvt)) {
2018+
if (auto fixed = getFixedType(tvt))
20272019
return simplifyType(fixed);
2028-
}
20292020

2030-
return tvt;
2021+
return getRepresentative(tvt);
20312022
});
20322023
}
20332024

lib/Sema/ConstraintSystem.h

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -365,11 +365,8 @@ class TypeVariableType::Implementation {
365365
auto rep = getRepresentative(record);
366366
Implementation &repImpl = rep->getImpl();
367367

368-
// Check whether it has a fixed type.
369-
if (auto type = repImpl.ParentOrFixed.dyn_cast<TypeBase *>())
370-
return type;
371-
372-
return Type();
368+
// Return the bound type if there is one, otherwise, null.
369+
return repImpl.ParentOrFixed.dyn_cast<TypeBase *>();
373370
}
374371

375372
/// \brief Assign a fixed type to this equivalence class.

0 commit comments

Comments
 (0)