Skip to content

[ConstraintSystem] Some small clean-ups to getFixedType, getFixedTypeRecursive, and simplifyType #18325

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 29, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 30 additions & 39 deletions lib/Sema/ConstraintSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -700,34 +700,41 @@ bool ConstraintSystem::isAnyHashableType(Type type) {
return false;
}

static Type withParens(ConstraintSystem &cs, Type type, ParenType *parenType) {
auto flags = parenType->getParameterFlags().withInOut(type->is<InOutType>());
return ParenType::get(cs.getASTContext(), type->getInOutObjectType(), flags);
}

Type ConstraintSystem::getFixedTypeRecursive(Type type,
TypeMatchOptions &flags,
bool wantRValue,
bool retainParens) {

if (wantRValue)
type = type->getRValueType();
// FIXME: This function doesn't strictly honor retainParens
// everywhere.

if (retainParens) {
if (auto parenTy = dyn_cast<ParenType>(type.getPointer())) {
type = getFixedTypeRecursive(parenTy->getUnderlyingType(), flags,
wantRValue, retainParens);
auto flags = parenTy->getParameterFlags().withInOut(type->is<InOutType>());
return ParenType::get(getASTContext(), type->getInOutObjectType(), flags);
if (wantRValue)
type = type->getRValueType();

if (auto *parenTy = dyn_cast<ParenType>(type.getPointer())) {
auto fixed = getFixedTypeRecursive(parenTy->getUnderlyingType(), flags,
wantRValue, retainParens);
return withParens(*this, fixed, parenTy);
}
}

while (true) {
if (wantRValue)
type = type->getRValueType();

if (auto depMemType = type->getAs<DependentMemberType>()) {
if (!depMemType->getBase()->isTypeVariableOrMember()) return type;

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

if (wantRValue)
newType = newType->getRValueType();

type = newType;

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

if (auto typeVar = type->getAs<TypeVariableType>()) {
bool hasRepresentative = false;
if (auto *repr = getRepresentative(typeVar)) {
if (typeVar != repr) {
hasRepresentative = true;
typeVar = repr;
}
}

if (auto fixed = getFixedType(typeVar)) {
if (wantRValue)
fixed = fixed->getRValueType();

type = fixed;
continue;
}

// If type variable has a representative but
// no fixed type, reflect that in the type itself.
if (hasRepresentative)
type = typeVar;
auto typeVar = type->getAs<TypeVariableType>();
if (!typeVar)
return type;

break;
if (auto fixed = getFixedType(typeVar)) {
type = fixed;
continue;
}

break;
}
if (retainParens)
if (auto *parenType = dyn_cast<ParenType>(type.getPointer()))
return withParens(*this, getRepresentative(typeVar), parenType);

return type;
return getRepresentative(typeVar);
}
}

/// Does a var or subscript produce an l-value?
Expand Down Expand Up @@ -2022,12 +2015,10 @@ Type ConstraintSystem::simplifyType(Type type) {
return simplifyTypeImpl(
*this, type,
[&](TypeVariableType *tvt) -> Type {
tvt = getRepresentative(tvt);
if (auto fixed = getFixedType(tvt)) {
if (auto fixed = getFixedType(tvt))
return simplifyType(fixed);
}

return tvt;
return getRepresentative(tvt);
});
}

Expand Down
7 changes: 2 additions & 5 deletions lib/Sema/ConstraintSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -365,11 +365,8 @@ class TypeVariableType::Implementation {
auto rep = getRepresentative(record);
Implementation &repImpl = rep->getImpl();

// Check whether it has a fixed type.
if (auto type = repImpl.ParentOrFixed.dyn_cast<TypeBase *>())
return type;

return Type();
// Return the bound type if there is one, otherwise, null.
return repImpl.ParentOrFixed.dyn_cast<TypeBase *>();
}

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