Skip to content

Commit f9c6ed5

Browse files
committed
[Sema] NFC: Refactor getUnopenedTypeOfReference to accept callback for type
1 parent 0a40452 commit f9c6ed5

File tree

3 files changed

+70
-33
lines changed

3 files changed

+70
-33
lines changed

lib/Sema/ConstraintSystem.cpp

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -800,23 +800,22 @@ static bool doesStorageProduceLValue(TypeChecker &TC,
800800
!storage->isSetterMutating();
801801
}
802802

803-
Type TypeChecker::getUnopenedTypeOfReference(VarDecl *value, Type baseType,
804-
DeclContext *UseDC, Type valueType,
805-
const DeclRefExpr *base,
806-
bool wantInterfaceType) {
807-
808-
Type requestedType = valueType;
809-
if (!requestedType) {
810-
validateDecl(value);
811-
if (!value->hasValidSignature() || value->isInvalid())
812-
return ErrorType::get(Context);
813-
814-
requestedType =
815-
(wantInterfaceType ? value->getInterfaceType() : value->getType());
816-
}
803+
Type ConstraintSystem::getUnopenedTypeOfReference(VarDecl *value, Type baseType,
804+
DeclContext *UseDC,
805+
const DeclRefExpr *base,
806+
bool wantInterfaceType) {
807+
return TC.getUnopenedTypeOfReference(
808+
value, baseType, UseDC,
809+
[&](VarDecl *var) -> Type { return getType(var, wantInterfaceType); },
810+
base, wantInterfaceType);
811+
}
817812

818-
requestedType = requestedType->getWithoutSpecifierType()
819-
->getReferenceStorageReferent();
813+
Type TypeChecker::getUnopenedTypeOfReference(
814+
VarDecl *value, Type baseType, DeclContext *UseDC,
815+
llvm::function_ref<Type(VarDecl *)> getType, const DeclRefExpr *base,
816+
bool wantInterfaceType) {
817+
Type requestedType =
818+
getType(value)->getWithoutSpecifierType()->getReferenceStorageReferent();
820819

821820
// If we're dealing with contextual types, and we referenced this type from
822821
// a different context, map the type.
@@ -1016,8 +1015,8 @@ ConstraintSystem::getTypeOfReference(ValueDecl *value,
10161015

10171016
// Determine the type of the value, opening up that type if necessary.
10181017
bool wantInterfaceType = !varDecl->getDeclContext()->isLocalContext();
1019-
Type valueType = TC.getUnopenedTypeOfReference(
1020-
varDecl, Type(), DC, getTypeOrNull(varDecl), base, wantInterfaceType);
1018+
Type valueType =
1019+
getUnopenedTypeOfReference(varDecl, Type(), DC, base, wantInterfaceType);
10211020

10221021
assert(!valueType->hasUnboundGenericType() &&
10231022
!valueType->hasTypeParameter());
@@ -1337,9 +1336,8 @@ ConstraintSystem::getTypeOfMemberReference(
13371336
refType = FunctionType::get(indicesTy, elementTy,
13381337
AnyFunctionType::ExtInfo());
13391338
} else {
1340-
refType = TC.getUnopenedTypeOfReference(cast<VarDecl>(value), baseTy,
1341-
useDC, getTypeOrNull(value), base,
1342-
/*wantInterfaceType=*/true);
1339+
refType = getUnopenedTypeOfReference(cast<VarDecl>(value), baseTy, useDC,
1340+
base, /*wantInterfaceType=*/true);
13431341
}
13441342

13451343
auto selfTy = outerDC->getSelfInterfaceType();

lib/Sema/ConstraintSystem.h

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1654,18 +1654,12 @@ class ConstraintSystem {
16541654
return ParamTypes.find(P)->second;
16551655
}
16561656

1657-
Type getTypeOrNull(const ValueDecl *D) const {
1658-
if (auto *P = dyn_cast<ParamDecl>(D))
1659-
return getType(P);
1660-
return Type();
1661-
}
1662-
1663-
Type getTypeOrInterfaceType(const ValueDecl *D) const {
1657+
Type getType(const VarDecl *D, bool wantInterfaceType = true) const {
16641658
if (auto *P = dyn_cast<ParamDecl>(D))
16651659
return getType(P);
16661660

16671661
assert(D->hasValidSignature());
1668-
return D->getInterfaceType();
1662+
return wantInterfaceType ? D->getInterfaceType() : D->getType();
16691663
}
16701664

16711665
/// Cache the type of the expression argument and return that same
@@ -2240,6 +2234,22 @@ class ConstraintSystem {
22402234
DeclContext *useDC,
22412235
const DeclRefExpr *base = nullptr);
22422236

2237+
/// Return the type-of-reference of the given value.
2238+
///
2239+
/// \param baseType if non-null, return the type of a member reference to
2240+
/// this value when the base has the given type
2241+
///
2242+
/// \param UseDC The context of the access. Some variables have different
2243+
/// types depending on where they are used.
2244+
///
2245+
/// \param base The optional base expression of this value reference
2246+
///
2247+
/// \param wantInterfaceType Whether we want the interface type, if available.
2248+
Type getUnopenedTypeOfReference(VarDecl *value, Type baseType,
2249+
DeclContext *UseDC,
2250+
const DeclRefExpr *base = nullptr,
2251+
bool wantInterfaceType = false);
2252+
22432253
/// \brief Retrieve the type of a reference to the given value declaration,
22442254
/// as a member with a base of the given type.
22452255
///

lib/Sema/TypeChecker.h

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1745,17 +1745,46 @@ class TypeChecker final : public LazyResolver {
17451745
/// \param UseDC The context of the access. Some variables have different
17461746
/// types depending on where they are used.
17471747
///
1748-
/// \param valueType The optional type associated with given value, if it's
1749-
/// not set, type of the declaration is going to be used instead.
1750-
///
17511748
/// \param base The optional base expression of this value reference
17521749
///
17531750
/// \param wantInterfaceType Whether we want the interface type, if available.
1751+
///
1752+
/// \param getType Optional callback to extract a type for given declaration.
17541753
Type getUnopenedTypeOfReference(VarDecl *value, Type baseType,
1755-
DeclContext *UseDC, Type valueType = Type(),
1754+
DeclContext *UseDC,
1755+
llvm::function_ref<Type(VarDecl *)> getType,
17561756
const DeclRefExpr *base = nullptr,
17571757
bool wantInterfaceType = false);
17581758

1759+
/// Return the type-of-reference of the given value.
1760+
///
1761+
/// \param baseType if non-null, return the type of a member reference to
1762+
/// this value when the base has the given type
1763+
///
1764+
/// \param UseDC The context of the access. Some variables have different
1765+
/// types depending on where they are used.
1766+
///
1767+
/// \param base The optional base expression of this value reference
1768+
///
1769+
/// \param wantInterfaceType Whether we want the interface type, if available.
1770+
Type getUnopenedTypeOfReference(VarDecl *value, Type baseType,
1771+
DeclContext *UseDC,
1772+
const DeclRefExpr *base = nullptr,
1773+
bool wantInterfaceType = false) {
1774+
return getUnopenedTypeOfReference(
1775+
value, baseType, UseDC,
1776+
[&](VarDecl *var) -> Type {
1777+
validateDecl(var);
1778+
1779+
if (!var->hasValidSignature() || var->isInvalid())
1780+
return ErrorType::get(Context);
1781+
1782+
return wantInterfaceType ? value->getInterfaceType()
1783+
: value->getType();
1784+
},
1785+
base, wantInterfaceType);
1786+
}
1787+
17591788
/// \brief Retrieve the default type for the given protocol.
17601789
///
17611790
/// Some protocols, particularly those that correspond to literals, have

0 commit comments

Comments
 (0)