Skip to content

Commit c344d6b

Browse files
committed
Sema: Rename getTypeOf{Member,}ReferenceImpl() to Pre() and factor out Post() versions
1 parent 182c384 commit c344d6b

File tree

4 files changed

+94
-73
lines changed

4 files changed

+94
-73
lines changed

include/swift/Sema/ConstraintSystem.h

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3795,7 +3795,7 @@ class ConstraintSystem {
37953795
/// Add a constraint that binds an overload set to a specific choice.
37963796
void addBindOverloadConstraint(Type boundTy, OverloadChoice choice,
37973797
ConstraintLocator *locator, DeclContext *useDC) {
3798-
resolveOverload(locator, boundTy, choice, useDC,
3798+
resolveOverload(choice, useDC, locator, boundTy,
37993799
/*preparedOverload=*/nullptr);
38003800
}
38013801

@@ -4439,13 +4439,6 @@ class ConstraintSystem {
44394439
unsigned numApplies, bool isMainDispatchQueue,
44404440
bool openGlobalActorType, ConstraintLocatorBuilder locator);
44414441

4442-
/// \returns The opened type and the thrown error type.
4443-
std::pair<Type, Type> getTypeOfReferenceImpl(
4444-
OverloadChoice choice,
4445-
ConstraintLocatorBuilder locator,
4446-
DeclContext *useDC,
4447-
PreparedOverloadBuilder *preparedOverload);
4448-
44494442
/// Retrieve the type of a reference to the given value declaration.
44504443
///
44514444
/// For references to polymorphic function types, this routine "opens up"
@@ -4454,14 +4447,7 @@ class ConstraintSystem {
44544447
///
44554448
/// \returns a description of the type of this declaration reference.
44564449
DeclReferenceType getTypeOfReference(
4457-
OverloadChoice choice,
4458-
ConstraintLocatorBuilder locator,
4459-
DeclContext *useDC,
4460-
PreparedOverloadBuilder *preparedOverload);
4461-
4462-
/// \returns the opened type and the thrown error type.
4463-
std::pair<Type, Type> getTypeOfMemberReferenceImpl(
4464-
OverloadChoice choice, DeclContext *useDC, ConstraintLocator *locator,
4450+
OverloadChoice choice, DeclContext *useDC, ConstraintLocatorBuilder locator,
44654451
PreparedOverloadBuilder *preparedOverload);
44664452

44674453
/// Retrieve the type of a reference to the given value declaration,
@@ -4486,6 +4472,24 @@ class ConstraintSystem {
44864472
}
44874473

44884474
private:
4475+
/// \returns The opened type and the thrown error type.
4476+
std::pair<Type, Type> getTypeOfReferencePre(
4477+
OverloadChoice choice, DeclContext *useDC, ConstraintLocatorBuilder locator,
4478+
PreparedOverloadBuilder *preparedOverload);
4479+
4480+
DeclReferenceType getTypeOfReferencePost(
4481+
OverloadChoice choice, DeclContext *useDC, ConstraintLocatorBuilder locator,
4482+
Type openedType, Type thrownErrorType);
4483+
4484+
/// \returns the opened type and the thrown error type.
4485+
std::pair<Type, Type> getTypeOfMemberReferencePre(
4486+
OverloadChoice choice, DeclContext *useDC, ConstraintLocator *locator,
4487+
PreparedOverloadBuilder *preparedOverload);
4488+
4489+
DeclReferenceType getTypeOfMemberReferencePost(
4490+
OverloadChoice choice, DeclContext *useDC, ConstraintLocator *locator,
4491+
Type openedType, Type thrownErrorType);
4492+
44894493
Type getTypeOfMemberTypeReference(
44904494
Type baseObjTy, TypeDecl *typeDecl, ConstraintLocator *locator,
44914495
PreparedOverloadBuilder *preparedOverload);
@@ -4920,26 +4924,26 @@ class ConstraintSystem {
49204924
SelectedOverload choice);
49214925

49224926
/// Build and allocate a prepared overload in the solver arena.
4923-
PreparedOverload *prepareOverload(ConstraintLocator *locator,
4924-
OverloadChoice choice,
4925-
DeclContext *useDC);
4927+
PreparedOverload *prepareOverload(OverloadChoice choice,
4928+
DeclContext *useDC,
4929+
ConstraintLocator *locator);
49264930

49274931
/// Populate the prepared overload with all type variables and constraints
49284932
/// that are to be introduced into the constraint system when this choice
49294933
/// is taken.
49304934
DeclReferenceType
4931-
prepareOverloadImpl(ConstraintLocator *locator,
4932-
OverloadChoice choice,
4935+
prepareOverloadImpl(OverloadChoice choice,
49334936
DeclContext *useDC,
4937+
ConstraintLocator *locator,
49344938
PreparedOverloadBuilder *preparedOverload);
49354939

49364940
void replayChanges(
49374941
ConstraintLocator *locator,
49384942
PreparedOverload *preparedOverload);
49394943

49404944
/// Resolve the given overload set to the given choice.
4941-
void resolveOverload(ConstraintLocator *locator, Type boundType,
4942-
OverloadChoice choice, DeclContext *useDC,
4945+
void resolveOverload(OverloadChoice choice, DeclContext *useDC,
4946+
ConstraintLocator *locator, Type boundType,
49434947
PreparedOverload *preparedOverload);
49444948

49454949
/// Simplify a type, by replacing type variables with either their

lib/Sema/CSSimplify.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16742,16 +16742,17 @@ ConstraintSystem::simplifyConstraint(const Constraint &constraint) {
1674216742
if (!preparedOverload) {
1674316743
if (enablePreparedOverloads &&
1674416744
constraint.getOverloadChoice().canBePrepared()) {
16745-
preparedOverload = prepareOverload(constraint.getLocator(),
16746-
constraint.getOverloadChoice(),
16747-
constraint.getDeclContext());
16745+
preparedOverload = prepareOverload(constraint.getOverloadChoice(),
16746+
constraint.getDeclContext(),
16747+
constraint.getLocator());
1674816748
const_cast<Constraint &>(constraint).setPreparedOverload(preparedOverload);
1674916749
}
1675016750
}
1675116751

16752-
resolveOverload(constraint.getLocator(), constraint.getFirstType(),
16753-
constraint.getOverloadChoice(),
16752+
resolveOverload(constraint.getOverloadChoice(),
1675416753
constraint.getDeclContext(),
16754+
constraint.getLocator(),
16755+
constraint.getFirstType(),
1675516756
preparedOverload);
1675616757
return SolutionKind::Solved;
1675716758
}

lib/Sema/TypeCheckProtocol.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1208,15 +1208,14 @@ swift::matchWitness(WitnessChecker::RequirementEnvironmentCache &reqEnvCache,
12081208
if (witness->getDeclContext()->isTypeContext()) {
12091209
OverloadChoice witnessChoice(selfTy, witness, FunctionRefInfo::doubleBaseNameApply());
12101210
openWitnessTypeInfo =
1211-
cs->getTypeOfMemberReference(witnessChoice, dc,
1212-
witnessLocator,
1211+
cs->getTypeOfMemberReference(witnessChoice, dc, witnessLocator,
12131212
/*preparedOverload=*/nullptr);
12141213
} else {
12151214
OverloadChoice witnessChoice(Type(), witness, FunctionRefInfo::doubleBaseNameApply());
12161215
openWitnessTypeInfo =
12171216
cs->getTypeOfReference(
1218-
witnessChoice, witnessLocator,
1219-
/*useDC=*/nullptr, /*preparedOverload=*/nullptr);
1217+
witnessChoice, /*useDC=*/nullptr, witnessLocator,
1218+
/*preparedOverload=*/nullptr);
12201219
}
12211220

12221221
openWitnessType = openWitnessTypeInfo.adjustedReferenceType;

lib/Sema/TypeOfReference.cpp

Lines changed: 58 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1109,10 +1109,10 @@ recordFixIfNeededForPlaceholderInDecl(ConstraintSystem &cs, ValueDecl *D,
11091109
}
11101110

11111111
std::pair<Type, Type>
1112-
ConstraintSystem::getTypeOfReferenceImpl(OverloadChoice choice,
1113-
ConstraintLocatorBuilder locator,
1114-
DeclContext *useDC,
1115-
PreparedOverloadBuilder *preparedOverload) {
1112+
ConstraintSystem::getTypeOfReferencePre(OverloadChoice choice,
1113+
DeclContext *useDC,
1114+
ConstraintLocatorBuilder locator,
1115+
PreparedOverloadBuilder *preparedOverload) {
11161116
auto *value = choice.getDecl();
11171117

11181118
ASSERT(!!preparedOverload == PreparingOverload);
@@ -1237,16 +1237,10 @@ ConstraintSystem::getTypeOfReferenceImpl(OverloadChoice choice,
12371237
}
12381238

12391239
DeclReferenceType
1240-
ConstraintSystem::getTypeOfReference(OverloadChoice choice,
1241-
ConstraintLocatorBuilder locator,
1242-
DeclContext *useDC,
1243-
PreparedOverloadBuilder *preparedOverload) {
1244-
ASSERT(!!preparedOverload == PreparingOverload);
1245-
1246-
Type openedType, thrownErrorType;
1247-
std::tie(openedType, thrownErrorType) = getTypeOfReferenceImpl(
1248-
choice, locator, useDC, preparedOverload);
1249-
1240+
ConstraintSystem::getTypeOfReferencePost(OverloadChoice choice,
1241+
DeclContext *useDC,
1242+
ConstraintLocatorBuilder locator,
1243+
Type openedType, Type thrownErrorType) {
12501244
auto *value = choice.getDecl();
12511245

12521246
if (value->getDeclContext()->isTypeContext() && isa<FuncDecl>(value)) {
@@ -1331,7 +1325,24 @@ ConstraintSystem::getTypeOfReference(OverloadChoice choice,
13311325
ClosureIsolatedByPreconcurrency{*this});
13321326
}
13331327

1334-
return { origOpenedType, openedType, origOpenedType, openedType, thrownErrorType };
1328+
return { origOpenedType, openedType,
1329+
origOpenedType, openedType,
1330+
thrownErrorType };
1331+
}
1332+
1333+
DeclReferenceType
1334+
ConstraintSystem::getTypeOfReference(OverloadChoice choice,
1335+
DeclContext *useDC,
1336+
ConstraintLocatorBuilder locator,
1337+
PreparedOverloadBuilder *preparedOverload) {
1338+
ASSERT(!!preparedOverload == PreparingOverload);
1339+
1340+
Type openedType, thrownErrorType;
1341+
std::tie(openedType, thrownErrorType) = getTypeOfReferencePre(
1342+
choice, useDC, locator, preparedOverload);
1343+
1344+
return getTypeOfReferencePost(choice, useDC, locator,
1345+
openedType, thrownErrorType);
13351346
}
13361347

13371348
/// Bind type variables for archetypes that are determined from
@@ -1856,7 +1867,7 @@ static FunctionType *applyOptionality(ValueDecl *value, FunctionType *fnTy) {
18561867
}
18571868

18581869
std::pair<Type, Type>
1859-
ConstraintSystem::getTypeOfMemberReferenceImpl(
1870+
ConstraintSystem::getTypeOfMemberReferencePre(
18601871
OverloadChoice choice, DeclContext *useDC,
18611872
ConstraintLocator *locator,
18621873
PreparedOverloadBuilder *preparedOverload) {
@@ -2032,13 +2043,16 @@ ConstraintSystem::getTypeOfMemberReferenceImpl(
20322043
return { openedType, thrownErrorType };
20332044
}
20342045

2035-
DeclReferenceType ConstraintSystem::getTypeOfMemberReference(
2046+
DeclReferenceType ConstraintSystem::getTypeOfMemberReferencePost(
20362047
OverloadChoice choice, DeclContext *useDC, ConstraintLocator *locator,
2037-
PreparedOverloadBuilder *preparedOverload) {
2038-
ASSERT(!!preparedOverload == PreparingOverload);
2039-
2048+
Type openedType, Type thrownErrorType) {
20402049
auto *value = choice.getDecl();
20412050

2051+
if (isa<TypeDecl>(value)) {
2052+
auto type = openedType->castTo<FunctionType>()->getResult();
2053+
return { openedType, openedType, type, type, Type() };
2054+
}
2055+
20422056
// Figure out the instance type used for the base.
20432057
Type baseTy = choice.getBaseType();
20442058
Type baseRValueTy = baseTy->getRValueType();
@@ -2048,15 +2062,6 @@ DeclReferenceType ConstraintSystem::getTypeOfMemberReference(
20482062
// be handled by the caller via getTypeOfReference().
20492063
ASSERT(!baseObjTy->is<ModuleType>());
20502064

2051-
Type openedType, thrownErrorType;
2052-
std::tie(openedType, thrownErrorType)
2053-
= getTypeOfMemberReferenceImpl(choice, useDC, locator, preparedOverload);
2054-
2055-
if (isa<TypeDecl>(value)) {
2056-
auto type = openedType->castTo<FunctionType>()->getResult();
2057-
return { openedType, openedType, type, type, Type() };
2058-
}
2059-
20602065
auto hasAppliedSelf = doesMemberRefApplyCurriedSelf(baseRValueTy, value);
20612066

20622067
// Adjust the opened type for concurrency.
@@ -2113,6 +2118,19 @@ DeclReferenceType ConstraintSystem::getTypeOfMemberReference(
21132118
return { origOpenedType, openedType, origType, type, thrownErrorType };
21142119
}
21152120

2121+
DeclReferenceType ConstraintSystem::getTypeOfMemberReference(
2122+
OverloadChoice choice, DeclContext *useDC, ConstraintLocator *locator,
2123+
PreparedOverloadBuilder *preparedOverload) {
2124+
ASSERT(!!preparedOverload == PreparingOverload);
2125+
2126+
Type openedType, thrownErrorType;
2127+
std::tie(openedType, thrownErrorType)
2128+
= getTypeOfMemberReferencePre(choice, useDC, locator, preparedOverload);
2129+
2130+
return getTypeOfMemberReferencePost(
2131+
choice, useDC, locator, openedType, thrownErrorType);
2132+
}
2133+
21162134
Type ConstraintSystem::getEffectiveOverloadType(ConstraintLocator *locator,
21172135
const OverloadChoice &overload,
21182136
bool allowMembers,
@@ -2825,9 +2843,9 @@ void ConstraintSystem::replayChanges(
28252843
/// FIXME: As a transitional mechanism, if preparedOverload is nullptr, this
28262844
/// immediately performs all operations.
28272845
DeclReferenceType
2828-
ConstraintSystem::prepareOverloadImpl(ConstraintLocator *locator,
2829-
OverloadChoice choice,
2846+
ConstraintSystem::prepareOverloadImpl(OverloadChoice choice,
28302847
DeclContext *useDC,
2848+
ConstraintLocator *locator,
28312849
PreparedOverloadBuilder *preparedOverload) {
28322850
// If we refer to a top-level decl with special type-checking semantics,
28332851
// handle it now.
@@ -2842,23 +2860,23 @@ ConstraintSystem::prepareOverloadImpl(ConstraintLocator *locator,
28422860

28432861
// If the base is a module type, it's an unqualified reference.
28442862
if (baseTy->getMetatypeInstanceType()->is<ModuleType>()) {
2845-
return getTypeOfReference(choice, locator, useDC, preparedOverload);
2863+
return getTypeOfReference(choice, useDC, locator, preparedOverload);
28462864
}
28472865

28482866
return getTypeOfMemberReference(choice, useDC, locator, preparedOverload);
28492867
} else {
2850-
return getTypeOfReference(choice, locator, useDC, preparedOverload);
2868+
return getTypeOfReference(choice, useDC, locator, preparedOverload);
28512869
}
28522870
}
28532871

2854-
PreparedOverload *ConstraintSystem::prepareOverload(ConstraintLocator *locator,
2855-
OverloadChoice choice,
2856-
DeclContext *useDC) {
2872+
PreparedOverload *ConstraintSystem::prepareOverload(OverloadChoice choice,
2873+
DeclContext *useDC,
2874+
ConstraintLocator *locator) {
28572875
ASSERT(!PreparingOverload);
28582876
PreparingOverload = true;
28592877

28602878
PreparedOverloadBuilder builder;
2861-
auto declRefType = prepareOverloadImpl(locator, choice, useDC, &builder);
2879+
auto declRefType = prepareOverloadImpl(choice, useDC, locator, &builder);
28622880

28632881
PreparingOverload = false;
28642882

@@ -2869,9 +2887,8 @@ PreparedOverload *ConstraintSystem::prepareOverload(ConstraintLocator *locator,
28692887
return new (mem) PreparedOverload(declRefType, builder.Changes);
28702888
}
28712889

2872-
void ConstraintSystem::resolveOverload(ConstraintLocator *locator,
2873-
Type boundType, OverloadChoice choice,
2874-
DeclContext *useDC,
2890+
void ConstraintSystem::resolveOverload(OverloadChoice choice, DeclContext *useDC,
2891+
ConstraintLocator *locator, Type boundType,
28752892
PreparedOverload *preparedOverload) {
28762893
// Determine the type to which we'll bind the overload set's type.
28772894
Type openedType;
@@ -2896,7 +2913,7 @@ void ConstraintSystem::resolveOverload(ConstraintLocator *locator,
28962913
adjustedRefType = preparedOverload->getAdjustedReferenceType();
28972914
thrownErrorTypeOnAccess = preparedOverload->getThrownErrorTypeOnAccess();
28982915
} else {
2899-
auto declRefType = prepareOverloadImpl(locator, choice, useDC, nullptr);
2916+
auto declRefType = prepareOverloadImpl(choice, useDC, locator, nullptr);
29002917

29012918
openedType = declRefType.openedType;
29022919
adjustedOpenedType = declRefType.adjustedOpenedType;

0 commit comments

Comments
 (0)