@@ -227,12 +227,12 @@ matchCallArguments(SmallVectorImpl<AnyFunctionType::Param> &args,
227
227
228
228
// Keep track of the parameter we're matching and what argument indices
229
229
// got bound to each parameter.
230
- unsigned paramIdx, numParams = params.size ();
230
+ unsigned numParams = params.size ();
231
231
parameterBindings.clear ();
232
232
parameterBindings.resize (numParams);
233
233
234
234
// Keep track of which arguments we have claimed from the argument tuple.
235
- unsigned nextArgIdx = 0 , numArgs = args.size ();
235
+ unsigned numArgs = args.size ();
236
236
SmallVector<bool , 4 > claimedArgs (numArgs, false );
237
237
SmallVector<Identifier, 4 > actualArgNames;
238
238
unsigned numClaimedArgs = 0 ;
@@ -258,7 +258,7 @@ matchCallArguments(SmallVectorImpl<AnyFunctionType::Param> &args,
258
258
actualArgNames.resize (numArgs);
259
259
260
260
// Figure out previous argument names from the parameter bindings.
261
- for (unsigned i = 0 ; i != numParams; ++i ) {
261
+ for (auto i : indices (params) ) {
262
262
const auto ¶m = params[i];
263
263
bool firstArg = true ;
264
264
@@ -278,18 +278,18 @@ matchCallArguments(SmallVectorImpl<AnyFunctionType::Param> &args,
278
278
};
279
279
280
280
// Local function that skips over any claimed arguments.
281
- auto skipClaimedArgs = [&]() {
281
+ auto skipClaimedArgs = [&](unsigned &nextArgIdx ) {
282
282
while (nextArgIdx != numArgs && claimedArgs[nextArgIdx])
283
283
++nextArgIdx;
284
284
};
285
285
286
286
// Local function that retrieves the next unclaimed argument with the given
287
287
// 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 > {
291
291
// Skip over any claimed arguments.
292
- skipClaimedArgs ();
292
+ skipClaimedArgs (nextArgIdx );
293
293
294
294
// If we've claimed all of the arguments, there's nothing more to do.
295
295
if (numClaimedArgs == numArgs)
@@ -380,13 +380,15 @@ matchCallArguments(SmallVectorImpl<AnyFunctionType::Param> &args,
380
380
// Local function that attempts to bind the given parameter to arguments in
381
381
// the list.
382
382
bool haveUnfulfilledParams = false ;
383
- auto bindNextParameter = [&](bool ignoreNameMismatch) {
383
+ auto bindNextParameter = [&](unsigned paramIdx, unsigned &nextArgIdx,
384
+ bool ignoreNameMismatch) {
384
385
const auto ¶m = params[paramIdx];
385
386
386
387
// Handle variadic parameters.
387
388
if (param.isVariadic ()) {
388
389
// 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);
390
392
391
393
// If there was no such argument, leave the parameter unfulfilled.
392
394
if (!claimed) {
@@ -400,28 +402,27 @@ matchCallArguments(SmallVectorImpl<AnyFunctionType::Param> &args,
400
402
// If the argument is itself variadic, we're forwarding varargs
401
403
// with a VarargExpansionExpr; don't collect any more arguments.
402
404
if (args[*claimed].isVariadic ()) {
403
- skipClaimedArgs ();
404
405
return ;
405
406
}
406
407
407
408
auto currentNextArgIdx = nextArgIdx;
408
409
{
409
410
nextArgIdx = *claimed;
410
411
// Claim any additional unnamed arguments.
411
- while ((claimed = claimNextNamed (Identifier (), false , true ))) {
412
+ while (
413
+ (claimed = claimNextNamed (nextArgIdx, Identifier (), false , true ))) {
412
414
parameterBindings[paramIdx].push_back (*claimed);
413
415
}
414
416
}
415
417
416
418
nextArgIdx = currentNextArgIdx;
417
- skipClaimedArgs ();
418
419
return ;
419
420
}
420
421
421
422
// 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)) {
423
425
parameterBindings[paramIdx].push_back (*claimed);
424
- skipClaimedArgs ();
425
426
return ;
426
427
}
427
428
@@ -476,34 +477,37 @@ matchCallArguments(SmallVectorImpl<AnyFunctionType::Param> &args,
476
477
}
477
478
478
479
// Claim the parameter/argument pair.
479
- claimedArgs[numArgs- 1 ] = true ;
480
- ++numClaimedArgs ;
480
+ claim (params[lastParamIdx]. getLabel (), numArgs - 1 ,
481
+ /* ignoreNameClash= */ true ) ;
481
482
// Let's claim the trailing closure unless it's an extra argument.
482
483
if (!isExtraClosure)
483
484
parameterBindings[lastParamIdx].push_back (numArgs - 1 );
484
485
}
485
486
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
+ }
490
494
}
491
495
492
496
// If we have any unclaimed arguments, complain about those.
493
497
if (numClaimedArgs != numArgs) {
494
498
// Find all of the named, unclaimed arguments.
495
499
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 );
500
504
}
501
505
502
506
if (!unclaimedNamedArgs.empty ()) {
503
507
// Find all of the named, unfulfilled parameters.
504
508
llvm::SmallVector<unsigned , 4 > unfulfilledNamedParams;
505
509
bool hasUnfulfilledUnnamedParams = false ;
506
- for (paramIdx = 0 ; paramIdx != numParams; ++paramIdx ) {
510
+ for (auto paramIdx : indices (params) ) {
507
511
if (parameterBindings[paramIdx].empty ()) {
508
512
if (params[paramIdx].getLabel ().empty ())
509
513
hasUnfulfilledUnnamedParams = true ;
@@ -522,7 +526,7 @@ matchCallArguments(SmallVectorImpl<AnyFunctionType::Param> &args,
522
526
// Find the closest matching unfulfilled named parameter.
523
527
unsigned bestScore = 0 ;
524
528
unsigned best = 0 ;
525
- for (unsigned i = 0 , n = unfulfilledNamedParams. size (); i != n; ++i ) {
529
+ for (auto i : indices (unfulfilledNamedParams) ) {
526
530
unsigned param = unfulfilledNamedParams[i];
527
531
auto paramName = params[param].getLabel ();
528
532
@@ -539,12 +543,10 @@ matchCallArguments(SmallVectorImpl<AnyFunctionType::Param> &args,
539
543
// If we found a parameter to fulfill, do it.
540
544
if (bestScore > 0 ) {
541
545
// Bind this parameter to the argument.
542
- nextArgIdx = argIdx;
543
- paramIdx = unfulfilledNamedParams[best];
546
+ auto paramIdx = unfulfilledNamedParams[best];
544
547
auto paramLabel = params[paramIdx].getLabel ();
545
548
546
549
parameterBindings[paramIdx].push_back (claim (paramLabel, argIdx));
547
- skipClaimedArgs ();
548
550
549
551
// Erase this parameter from the list of unfulfilled named
550
552
// parameters, so we don't try to fulfill it again.
@@ -565,15 +567,14 @@ matchCallArguments(SmallVectorImpl<AnyFunctionType::Param> &args,
565
567
// semi-positionally.
566
568
if (numClaimedArgs != numArgs) {
567
569
// Restart at the first argument/parameter.
568
- nextArgIdx = 0 ;
569
- skipClaimedArgs ();
570
+ unsigned nextArgIdx = 0 ;
570
571
haveUnfulfilledParams = false ;
571
- for (paramIdx = 0 ; paramIdx != numParams; ++paramIdx ) {
572
+ for (auto paramIdx : indices (params) ) {
572
573
// Skip fulfilled parameters.
573
574
if (!parameterBindings[paramIdx].empty ())
574
575
continue ;
575
576
576
- bindNextParameter (true );
577
+ bindNextParameter (paramIdx, nextArgIdx, true );
577
578
}
578
579
}
579
580
@@ -582,7 +583,7 @@ matchCallArguments(SmallVectorImpl<AnyFunctionType::Param> &args,
582
583
// labels don't line up, if so let's try to claim arguments
583
584
// with incorrect labels, and let OoO/re-labeling logic diagnose that.
584
585
if (numArgs == numParams && numClaimedArgs != numArgs) {
585
- for (unsigned i = 0 ; i < numArgs; ++i ) {
586
+ for (auto i : indices (args) ) {
586
587
if (claimedArgs[i] || !parameterBindings[i].empty ())
587
588
continue ;
588
589
@@ -619,7 +620,7 @@ matchCallArguments(SmallVectorImpl<AnyFunctionType::Param> &args,
619
620
620
621
// If we have any unfulfilled parameters, check them now.
621
622
if (haveUnfulfilledParams) {
622
- for (paramIdx = 0 ; paramIdx != numParams; ++paramIdx ) {
623
+ for (auto paramIdx : indices (params) ) {
623
624
// If we have a binding for this parameter, we're done.
624
625
if (!parameterBindings[paramIdx].empty ())
625
626
continue ;
0 commit comments