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