Skip to content

Commit d6a9399

Browse files
authored
Merge pull request swiftlang#30202 from omochi/refactor-matchCallArguments
[ConstraintSystem] Refactor matchCallArguments
2 parents c0a866d + dfd8af1 commit d6a9399

File tree

1 file changed

+37
-36
lines changed

1 file changed

+37
-36
lines changed

lib/Sema/CSSimplify.cpp

Lines changed: 37 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -227,12 +227,12 @@ matchCallArguments(SmallVectorImpl<AnyFunctionType::Param> &args,
227227

228228
// Keep track of the parameter we're matching and what argument indices
229229
// got bound to each parameter.
230-
unsigned paramIdx, numParams = params.size();
230+
unsigned numParams = params.size();
231231
parameterBindings.clear();
232232
parameterBindings.resize(numParams);
233233

234234
// Keep track of which arguments we have claimed from the argument tuple.
235-
unsigned nextArgIdx = 0, numArgs = args.size();
235+
unsigned numArgs = args.size();
236236
SmallVector<bool, 4> claimedArgs(numArgs, false);
237237
SmallVector<Identifier, 4> actualArgNames;
238238
unsigned numClaimedArgs = 0;
@@ -258,7 +258,7 @@ matchCallArguments(SmallVectorImpl<AnyFunctionType::Param> &args,
258258
actualArgNames.resize(numArgs);
259259

260260
// Figure out previous argument names from the parameter bindings.
261-
for (unsigned i = 0; i != numParams; ++i) {
261+
for (auto i : indices(params)) {
262262
const auto &param = params[i];
263263
bool firstArg = true;
264264

@@ -278,18 +278,18 @@ matchCallArguments(SmallVectorImpl<AnyFunctionType::Param> &args,
278278
};
279279

280280
// Local function that skips over any claimed arguments.
281-
auto skipClaimedArgs = [&]() {
281+
auto skipClaimedArgs = [&](unsigned &nextArgIdx) {
282282
while (nextArgIdx != numArgs && claimedArgs[nextArgIdx])
283283
++nextArgIdx;
284284
};
285285

286286
// Local function that retrieves the next unclaimed argument with the given
287287
// name (which may be empty). This routine claims the argument.
288-
auto claimNextNamed
289-
= [&](Identifier paramLabel, bool ignoreNameMismatch,
290-
bool forVariadic = false) -> Optional<unsigned> {
288+
auto claimNextNamed = [&](unsigned &nextArgIdx, Identifier paramLabel,
289+
bool ignoreNameMismatch,
290+
bool forVariadic = false) -> Optional<unsigned> {
291291
// Skip over any claimed arguments.
292-
skipClaimedArgs();
292+
skipClaimedArgs(nextArgIdx);
293293

294294
// If we've claimed all of the arguments, there's nothing more to do.
295295
if (numClaimedArgs == numArgs)
@@ -380,13 +380,15 @@ matchCallArguments(SmallVectorImpl<AnyFunctionType::Param> &args,
380380
// Local function that attempts to bind the given parameter to arguments in
381381
// the list.
382382
bool haveUnfulfilledParams = false;
383-
auto bindNextParameter = [&](bool ignoreNameMismatch) {
383+
auto bindNextParameter = [&](unsigned paramIdx, unsigned &nextArgIdx,
384+
bool ignoreNameMismatch) {
384385
const auto &param = params[paramIdx];
385386

386387
// Handle variadic parameters.
387388
if (param.isVariadic()) {
388389
// Claim the next argument with the name of this parameter.
389-
auto claimed = claimNextNamed(param.getLabel(), ignoreNameMismatch);
390+
auto claimed =
391+
claimNextNamed(nextArgIdx, param.getLabel(), ignoreNameMismatch);
390392

391393
// If there was no such argument, leave the parameter unfulfilled.
392394
if (!claimed) {
@@ -400,28 +402,27 @@ matchCallArguments(SmallVectorImpl<AnyFunctionType::Param> &args,
400402
// If the argument is itself variadic, we're forwarding varargs
401403
// with a VarargExpansionExpr; don't collect any more arguments.
402404
if (args[*claimed].isVariadic()) {
403-
skipClaimedArgs();
404405
return;
405406
}
406407

407408
auto currentNextArgIdx = nextArgIdx;
408409
{
409410
nextArgIdx = *claimed;
410411
// Claim any additional unnamed arguments.
411-
while ((claimed = claimNextNamed(Identifier(), false, true))) {
412+
while (
413+
(claimed = claimNextNamed(nextArgIdx, Identifier(), false, true))) {
412414
parameterBindings[paramIdx].push_back(*claimed);
413415
}
414416
}
415417

416418
nextArgIdx = currentNextArgIdx;
417-
skipClaimedArgs();
418419
return;
419420
}
420421

421422
// Try to claim an argument for this parameter.
422-
if (auto claimed = claimNextNamed(param.getLabel(), ignoreNameMismatch)) {
423+
if (auto claimed =
424+
claimNextNamed(nextArgIdx, param.getLabel(), ignoreNameMismatch)) {
423425
parameterBindings[paramIdx].push_back(*claimed);
424-
skipClaimedArgs();
425426
return;
426427
}
427428

@@ -476,34 +477,37 @@ matchCallArguments(SmallVectorImpl<AnyFunctionType::Param> &args,
476477
}
477478

478479
// Claim the parameter/argument pair.
479-
claimedArgs[numArgs-1] = true;
480-
++numClaimedArgs;
480+
claim(params[lastParamIdx].getLabel(), numArgs - 1,
481+
/*ignoreNameClash=*/true);
481482
// Let's claim the trailing closure unless it's an extra argument.
482483
if (!isExtraClosure)
483484
parameterBindings[lastParamIdx].push_back(numArgs - 1);
484485
}
485486

486-
// Mark through the parameters, binding them to their arguments.
487-
for (paramIdx = 0; paramIdx != numParams; ++paramIdx) {
488-
if (parameterBindings[paramIdx].empty())
489-
bindNextParameter(false);
487+
{
488+
unsigned nextArgIdx = 0;
489+
// Mark through the parameters, binding them to their arguments.
490+
for (auto paramIdx : indices(params)) {
491+
if (parameterBindings[paramIdx].empty())
492+
bindNextParameter(paramIdx, nextArgIdx, false);
493+
}
490494
}
491495

492496
// If we have any unclaimed arguments, complain about those.
493497
if (numClaimedArgs != numArgs) {
494498
// Find all of the named, unclaimed arguments.
495499
llvm::SmallVector<unsigned, 4> unclaimedNamedArgs;
496-
for (nextArgIdx = 0; skipClaimedArgs(), nextArgIdx != numArgs;
497-
++nextArgIdx) {
498-
if (!args[nextArgIdx].getLabel().empty())
499-
unclaimedNamedArgs.push_back(nextArgIdx);
500+
for (auto argIdx : indices(args)) {
501+
if (claimedArgs[argIdx]) continue;
502+
if (!args[argIdx].getLabel().empty())
503+
unclaimedNamedArgs.push_back(argIdx);
500504
}
501505

502506
if (!unclaimedNamedArgs.empty()) {
503507
// Find all of the named, unfulfilled parameters.
504508
llvm::SmallVector<unsigned, 4> unfulfilledNamedParams;
505509
bool hasUnfulfilledUnnamedParams = false;
506-
for (paramIdx = 0; paramIdx != numParams; ++paramIdx) {
510+
for (auto paramIdx : indices(params)) {
507511
if (parameterBindings[paramIdx].empty()) {
508512
if (params[paramIdx].getLabel().empty())
509513
hasUnfulfilledUnnamedParams = true;
@@ -522,7 +526,7 @@ matchCallArguments(SmallVectorImpl<AnyFunctionType::Param> &args,
522526
// Find the closest matching unfulfilled named parameter.
523527
unsigned bestScore = 0;
524528
unsigned best = 0;
525-
for (unsigned i = 0, n = unfulfilledNamedParams.size(); i != n; ++i) {
529+
for (auto i : indices(unfulfilledNamedParams)) {
526530
unsigned param = unfulfilledNamedParams[i];
527531
auto paramName = params[param].getLabel();
528532

@@ -539,12 +543,10 @@ matchCallArguments(SmallVectorImpl<AnyFunctionType::Param> &args,
539543
// If we found a parameter to fulfill, do it.
540544
if (bestScore > 0) {
541545
// Bind this parameter to the argument.
542-
nextArgIdx = argIdx;
543-
paramIdx = unfulfilledNamedParams[best];
546+
auto paramIdx = unfulfilledNamedParams[best];
544547
auto paramLabel = params[paramIdx].getLabel();
545548

546549
parameterBindings[paramIdx].push_back(claim(paramLabel, argIdx));
547-
skipClaimedArgs();
548550

549551
// Erase this parameter from the list of unfulfilled named
550552
// parameters, so we don't try to fulfill it again.
@@ -565,15 +567,14 @@ matchCallArguments(SmallVectorImpl<AnyFunctionType::Param> &args,
565567
// semi-positionally.
566568
if (numClaimedArgs != numArgs) {
567569
// Restart at the first argument/parameter.
568-
nextArgIdx = 0;
569-
skipClaimedArgs();
570+
unsigned nextArgIdx = 0;
570571
haveUnfulfilledParams = false;
571-
for (paramIdx = 0; paramIdx != numParams; ++paramIdx) {
572+
for (auto paramIdx : indices(params)) {
572573
// Skip fulfilled parameters.
573574
if (!parameterBindings[paramIdx].empty())
574575
continue;
575576

576-
bindNextParameter(true);
577+
bindNextParameter(paramIdx, nextArgIdx, true);
577578
}
578579
}
579580

@@ -582,7 +583,7 @@ matchCallArguments(SmallVectorImpl<AnyFunctionType::Param> &args,
582583
// labels don't line up, if so let's try to claim arguments
583584
// with incorrect labels, and let OoO/re-labeling logic diagnose that.
584585
if (numArgs == numParams && numClaimedArgs != numArgs) {
585-
for (unsigned i = 0; i < numArgs; ++i) {
586+
for (auto i : indices(args)) {
586587
if (claimedArgs[i] || !parameterBindings[i].empty())
587588
continue;
588589

@@ -619,7 +620,7 @@ matchCallArguments(SmallVectorImpl<AnyFunctionType::Param> &args,
619620

620621
// If we have any unfulfilled parameters, check them now.
621622
if (haveUnfulfilledParams) {
622-
for (paramIdx = 0; paramIdx != numParams; ++paramIdx) {
623+
for (auto paramIdx : indices(params)) {
623624
// If we have a binding for this parameter, we're done.
624625
if (!parameterBindings[paramIdx].empty())
625626
continue;

0 commit comments

Comments
 (0)