Skip to content

Commit b48a064

Browse files
committed
[ConstraintSystem] Add storage for implicitly generated roots of .callAsFunction
Some implicit calls to `.callAsFunction` require that a new root expression to be created for them in order to record argument list and resolved overload choice.
1 parent c206216 commit b48a064

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

include/swift/Sema/ConstraintSystem.h

+13
Original file line numberDiff line numberDiff line change
@@ -1220,6 +1220,10 @@ class Solution {
12201220
/// constructions) to the argument lists for the call to that locator.
12211221
llvm::MapVector<ConstraintLocator *, ArgumentList *> argumentLists;
12221222

1223+
/// The set of implicitly generated `.callAsFunction` root expressions.
1224+
llvm::DenseMap<ConstraintLocator *, UnresolvedDotExpr *>
1225+
ImplicitCallAsFunctionRoots;
1226+
12231227
/// Record a new argument matching choice for given locator that maps a
12241228
/// single argument to a single parameter.
12251229
void recordSingleArgMatchingChoice(ConstraintLocator *locator);
@@ -2468,6 +2472,12 @@ class ConstraintSystem {
24682472
/// types.
24692473
llvm::DenseMap<CanType, DynamicCallableMethods> DynamicCallableCache;
24702474

2475+
/// A cache of implicitly generated dot-member expressions used as roots
2476+
/// for some `.callAsFunction` calls. The key here is "base" locator for
2477+
/// the `.callAsFunction` member reference.
2478+
llvm::SmallMapVector<ConstraintLocator *, UnresolvedDotExpr *, 2>
2479+
ImplicitCallAsFunctionRoots;
2480+
24712481
private:
24722482
/// Describe the candidate expression for partial solving.
24732483
/// This class used by shrink & solve methods which apply
@@ -2951,6 +2961,9 @@ class ConstraintSystem {
29512961
/// The length of \c ArgumentLists.
29522962
unsigned numArgumentLists;
29532963

2964+
/// The length of \c ImplicitCallAsFunctionRoots.
2965+
unsigned numImplicitCallAsFunctionRoots;
2966+
29542967
/// The previous score.
29552968
Score PreviousScore;
29562969

lib/Sema/CSSolver.cpp

+13
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,10 @@ Solution ConstraintSystem::finalize() {
201201
solution.argumentLists.insert(argListMapping);
202202
}
203203

204+
for (const auto &implicitRoot : ImplicitCallAsFunctionRoots) {
205+
solution.ImplicitCallAsFunctionRoots.insert(implicitRoot);
206+
}
207+
204208
return solution;
205209
}
206210

@@ -308,6 +312,10 @@ void ConstraintSystem::applySolution(const Solution &solution) {
308312
ArgumentLists.insert(argListMapping);
309313
}
310314

315+
for (auto &implicitRoot : solution.ImplicitCallAsFunctionRoots) {
316+
ImplicitCallAsFunctionRoots.insert(implicitRoot);
317+
}
318+
311319
// Register any fixes produced along this path.
312320
Fixes.insert(solution.Fixes.begin(), solution.Fixes.end());
313321
}
@@ -529,6 +537,7 @@ ConstraintSystem::SolverScope::SolverScope(ConstraintSystem &cs)
529537
numIsolatedParams = cs.isolatedParams.size();
530538
numImplicitValueConversions = cs.ImplicitValueConversions.size();
531539
numArgumentLists = cs.ArgumentLists.size();
540+
numImplicitCallAsFunctionRoots = cs.ImplicitCallAsFunctionRoots.size();
532541

533542
PreviousScore = cs.CurrentScore;
534543

@@ -643,6 +652,10 @@ ConstraintSystem::SolverScope::~SolverScope() {
643652
// Remove any argument lists no longer in scope.
644653
truncate(cs.ArgumentLists, numArgumentLists);
645654

655+
// Remove any implicitly generated root expressions for `.callAsFunction`
656+
// which are no longer in scope.
657+
truncate(cs.ImplicitCallAsFunctionRoots, numImplicitCallAsFunctionRoots);
658+
646659
// Reset the previous score.
647660
cs.CurrentScore = PreviousScore;
648661

lib/Sema/ConstraintSystem.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -3239,7 +3239,8 @@ size_t Solution::getTotalMemory() const {
32393239
ConstraintRestrictions.getMemorySize() +
32403240
llvm::capacity_in_bytes(Fixes) + DisjunctionChoices.getMemorySize() +
32413241
OpenedTypes.getMemorySize() + OpenedExistentialTypes.getMemorySize() +
3242-
(DefaultedConstraints.size() * sizeof(void *));
3242+
(DefaultedConstraints.size() * sizeof(void *)) +
3243+
ImplicitCallAsFunctionRoots.getMemorySize();
32433244
}
32443245

32453246
DeclContext *Solution::getDC() const { return constraintSystem->DC; }

0 commit comments

Comments
 (0)