@@ -6286,22 +6286,46 @@ class AsyncConverter : private SourceEntityWalker {
6286
6286
// / 'await' keyword.
6287
6287
void addCallToAsyncMethod (const FuncDecl *FD,
6288
6288
const AsyncHandlerDesc &HandlerDesc) {
6289
+ // The call to the async function is the same as the call to the old
6290
+ // completion handler function, minus the completion handler arg.
6291
+ addForwardingCallTo (FD, HandlerDesc, /* HandlerReplacement*/ " " );
6292
+ }
6293
+
6294
+ // / Adds a forwarding call to the old completion handler function, with
6295
+ // / \p HandlerReplacement that allows for a custom replacement or, if empty,
6296
+ // / removal of the completion handler closure.
6297
+ void addForwardingCallTo (
6298
+ const FuncDecl *FD, const AsyncHandlerDesc &HandlerDesc,
6299
+ StringRef HandlerReplacement, bool CanUseTrailingClosure = true ) {
6289
6300
OS << FD->getBaseName () << tok::l_paren;
6290
- bool FirstParam = true ;
6291
- for (auto Param : *FD->getParameters ()) {
6301
+
6302
+ auto *Params = FD->getParameters ();
6303
+ for (auto Param : *Params) {
6292
6304
if (Param == HandlerDesc.getHandler ()) {
6293
- // / We don't need to pass the completion handler to the async method.
6294
- continue ;
6305
+ // / If we're not replacing the handler with anything, drop it.
6306
+ if (HandlerReplacement.empty ())
6307
+ continue ;
6308
+
6309
+ // If this is the last param, and we can use a trailing closure, do so.
6310
+ if (CanUseTrailingClosure && Param == Params->back ()) {
6311
+ OS << tok::r_paren << " " ;
6312
+ OS << HandlerReplacement;
6313
+ return ;
6314
+ }
6315
+ // Otherwise fall through to do the replacement.
6295
6316
}
6296
- if (!FirstParam) {
6317
+
6318
+ if (Param != Params->front ())
6297
6319
OS << tok::comma << " " ;
6298
- } else {
6299
- FirstParam = false ;
6300
- }
6301
- if (!Param->getArgumentName ().empty ()) {
6320
+
6321
+ if (!Param->getArgumentName ().empty ())
6302
6322
OS << Param->getArgumentName () << tok::colon << " " ;
6323
+
6324
+ if (Param == HandlerDesc.getHandler ()) {
6325
+ OS << HandlerReplacement;
6326
+ } else {
6327
+ OS << Param->getParameterName ();
6303
6328
}
6304
- OS << Param->getParameterName ();
6305
6329
}
6306
6330
OS << tok::r_paren;
6307
6331
}
0 commit comments