Skip to content

Commit 8527e88

Browse files
committed
Sema: Extract prepareOverload() from resolveOverload()
1 parent 8f0eaff commit 8527e88

File tree

2 files changed

+59
-22
lines changed

2 files changed

+59
-22
lines changed

include/swift/Sema/ConstraintSystem.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4929,6 +4929,15 @@ class ConstraintSystem {
49294929
void recordResolvedOverload(ConstraintLocator *locator,
49304930
SelectedOverload choice);
49314931

4932+
/// Populate the prepared overload with all type variables and constraints
4933+
/// that are to be introduced into the constraint system when this choice
4934+
/// is taken.
4935+
DeclReferenceType
4936+
prepareOverload(ConstraintLocator *locator,
4937+
OverloadChoice choice,
4938+
DeclContext *useDC,
4939+
PreparedOverload *preparedOverload);
4940+
49324941
/// Resolve the given overload set to the given choice.
49334942
void resolveOverload(ConstraintLocator *locator, Type boundType,
49344943
OverloadChoice choice, DeclContext *useDC);

lib/Sema/TypeOfReference.cpp

Lines changed: 50 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2550,6 +2550,39 @@ void PreparedOverload::discharge(ConstraintSystem &cs,
25502550
}
25512551
}
25522552

2553+
/// Populate the prepared overload with all type variables and constraints
2554+
/// that are to be introduced into the constraint system when this choice
2555+
/// is taken.
2556+
///
2557+
/// FIXME: As a transitional mechanism, if preparedOverload is nullptr, this
2558+
/// immediately performs all operations.
2559+
DeclReferenceType
2560+
ConstraintSystem::prepareOverload(ConstraintLocator *locator,
2561+
OverloadChoice choice,
2562+
DeclContext *useDC,
2563+
PreparedOverload *preparedOverload) {
2564+
// If we refer to a top-level decl with special type-checking semantics,
2565+
// handle it now.
2566+
auto semantics =
2567+
TypeChecker::getDeclTypeCheckingSemantics(choice.getDecl());
2568+
if (semantics != DeclTypeCheckingSemantics::Normal) {
2569+
return getTypeOfReferenceWithSpecialTypeCheckingSemantics(
2570+
*this, locator, semantics, preparedOverload);
2571+
} else if (auto baseTy = choice.getBaseType()) {
2572+
// Retrieve the type of a reference to the specific declaration choice.
2573+
assert(!baseTy->hasTypeParameter());
2574+
2575+
return getTypeOfMemberReference(
2576+
baseTy, choice.getDecl(), useDC,
2577+
(choice.getKind() == OverloadChoiceKind::DeclViaDynamic),
2578+
choice.getFunctionRefInfo(), locator, nullptr, preparedOverload);
2579+
} else {
2580+
return getTypeOfReference(
2581+
choice.getDecl(), choice.getFunctionRefInfo(), locator, useDC,
2582+
preparedOverload);
2583+
}
2584+
}
2585+
25532586
void ConstraintSystem::resolveOverload(ConstraintLocator *locator,
25542587
Type boundType, OverloadChoice choice,
25552588
DeclContext *useDC) {
@@ -2560,34 +2593,29 @@ void ConstraintSystem::resolveOverload(ConstraintLocator *locator,
25602593
Type adjustedRefType;
25612594
Type thrownErrorTypeOnAccess;
25622595

2563-
switch (auto kind = choice.getKind()) {
2596+
switch (choice.getKind()) {
25642597
case OverloadChoiceKind::Decl:
25652598
case OverloadChoiceKind::DeclViaBridge:
25662599
case OverloadChoiceKind::DeclViaDynamic:
25672600
case OverloadChoiceKind::DeclViaUnwrappedOptional:
25682601
case OverloadChoiceKind::DynamicMemberLookup:
25692602
case OverloadChoiceKind::KeyPathDynamicMemberLookup: {
2570-
// If we refer to a top-level decl with special type-checking semantics,
2571-
// handle it now.
2572-
const auto semantics =
2573-
TypeChecker::getDeclTypeCheckingSemantics(choice.getDecl());
2574-
DeclReferenceType declRefType;
2575-
if (semantics != DeclTypeCheckingSemantics::Normal) {
2576-
declRefType = getTypeOfReferenceWithSpecialTypeCheckingSemantics(
2577-
*this, locator, semantics);
2578-
} else if (auto baseTy = choice.getBaseType()) {
2579-
// Retrieve the type of a reference to the specific declaration choice.
2580-
assert(!baseTy->hasTypeParameter());
2581-
2582-
declRefType = getTypeOfMemberReference(
2583-
baseTy, choice.getDecl(), useDC,
2584-
(kind == OverloadChoiceKind::DeclViaDynamic),
2585-
choice.getFunctionRefInfo(), locator, nullptr,
2586-
/*preparedOverload=*/nullptr);
2587-
} else {
2588-
declRefType = getTypeOfReference(
2589-
choice.getDecl(), choice.getFunctionRefInfo(), locator, useDC,
2590-
/*preparedOverload=*/nullptr);
2603+
bool enablePreparedOverloads = false;
2604+
2605+
PreparedOverload preparedOverload;
2606+
if (enablePreparedOverloads) {
2607+
ASSERT(!PreparingOverload);
2608+
PreparingOverload = true;
2609+
}
2610+
2611+
auto declRefType = prepareOverload(locator, choice, useDC,
2612+
(enablePreparedOverloads
2613+
? &preparedOverload
2614+
: nullptr));
2615+
2616+
if (enablePreparedOverloads) {
2617+
PreparingOverload = false;
2618+
preparedOverload.discharge(*this, locator);
25912619
}
25922620

25932621
openedType = declRefType.openedType;

0 commit comments

Comments
 (0)