@@ -121,318 +121,6 @@ static FuncDecl *deriveDistributedActor_resolve(DerivedConformance &derived) {
121
121
return factoryDecl;
122
122
}
123
123
124
- /* *****************************************************************************/
125
- /* ************** INVOKE HANDLER ON-RETURN FUNCTION ****************************/
126
- /* *****************************************************************************/
127
-
128
- namespace {
129
- struct DoInvokeOnReturnContext {
130
- ParamDecl *handlerParam;
131
- ParamDecl *resultBufferParam;
132
- };
133
- } // namespace
134
-
135
- static std::pair<BraceStmt *, bool >
136
- deriveBodyDistributed_doInvokeOnReturn (AbstractFunctionDecl *afd, void *arg) {
137
- auto &C = afd->getASTContext ();
138
- auto *context = static_cast <DoInvokeOnReturnContext *>(arg);
139
-
140
- // mock locations, we're a thunk and don't really need detailed locations
141
- const SourceLoc sloc = SourceLoc ();
142
- const DeclNameLoc dloc = DeclNameLoc ();
143
- bool implicit = true ;
144
-
145
- auto returnTypeParam = afd->getParameters ()->get (0 );
146
- SmallVector<ASTNode, 8 > stmts;
147
-
148
- VarDecl *resultVar =
149
- new (C) VarDecl (/* isStatic=*/ false , VarDecl::Introducer::Let, sloc,
150
- C.getIdentifier (" result" ), afd);
151
- {
152
- auto resultLoadCall = CallExpr::createImplicit (
153
- C,
154
- UnresolvedDotExpr::createImplicit (
155
- C,
156
- /* base=*/
157
- new (C) DeclRefExpr (ConcreteDeclRef (context->resultBufferParam ),
158
- dloc, implicit),
159
- /* baseName=*/ DeclBaseName (C.getIdentifier (" load" )),
160
- /* argLabels=*/
161
- {C.getIdentifier (" fromByteOffset" ), C.getIdentifier (" as" )}),
162
- ArgumentList::createImplicit (
163
- C, {Argument (sloc, C.getIdentifier (" as" ),
164
- new (C) DeclRefExpr (ConcreteDeclRef (returnTypeParam),
165
- dloc, implicit))}));
166
-
167
- auto resultPattern = NamedPattern::createImplicit (C, resultVar);
168
- auto resultPB = PatternBindingDecl::createImplicit (
169
- C, swift::StaticSpellingKind::None, resultPattern,
170
- /* expr=*/ resultLoadCall, afd);
171
-
172
- stmts.push_back (resultPB);
173
- stmts.push_back (resultVar);
174
- }
175
-
176
- // call the ad-hoc `handler.onReturn`
177
- {
178
- // Find the ad-hoc requirement ensured function on the concrete handler:
179
- auto onReturnFunc = C.getOnReturnOnDistributedTargetInvocationResultHandler (
180
- context->handlerParam ->getInterfaceType ()->getAnyNominal ());
181
- assert (onReturnFunc && " did not find ad-hoc requirement witness!" );
182
-
183
- Expr *callExpr = CallExpr::createImplicit (
184
- C,
185
- UnresolvedDotExpr::createImplicit (
186
- C,
187
- /* base=*/
188
- new (C) DeclRefExpr (ConcreteDeclRef (context->handlerParam ), dloc,
189
- implicit),
190
- /* baseName=*/ onReturnFunc->getBaseName (),
191
- /* paramList=*/ onReturnFunc->getParameters ()),
192
- ArgumentList::forImplicitCallTo (
193
- DeclNameRef (onReturnFunc->getName ()),
194
- {new (C) DeclRefExpr (ConcreteDeclRef (resultVar), dloc, implicit)},
195
- C));
196
- callExpr = TryExpr::createImplicit (C, sloc, callExpr);
197
- callExpr = AwaitExpr::createImplicit (C, sloc, callExpr);
198
-
199
- stmts.push_back (callExpr);
200
- }
201
-
202
- auto body = BraceStmt::create (C, sloc, {stmts}, sloc, implicit);
203
- return {body, /* isTypeChecked=*/ false };
204
- }
205
-
206
- // Create local function:
207
- // func invokeOnReturn<R: Self.SerializationRequirement>(
208
- // _ returnType: R.Type
209
- // ) async throws {
210
- // let value = resultBuffer.load(as: returnType)
211
- // try await handler.onReturn(value: value)
212
- // }
213
- static FuncDecl* createLocalFunc_doInvokeOnReturn (
214
- ASTContext& C, FuncDecl* parentFunc,
215
- NominalTypeDecl* systemNominal,
216
- ParamDecl* handlerParam,
217
- ParamDecl* resultBufParam) {
218
- auto DC = parentFunc;
219
- auto DAS = C.getDistributedActorSystemDecl ();
220
- auto doInvokeLocalFuncIdent = C.getIdentifier (" doInvokeOnReturn" );
221
-
222
- // mock locations, we're a synthesized func and don't need real locations
223
- const SourceLoc sloc = SourceLoc ();
224
-
225
- // <R: Self.SerializationRequirement>
226
- // We create the generic param at invalid depth, which means it'll be filled
227
- // by semantic analysis.
228
- auto *resultGenericParamDecl = GenericTypeParamDecl::createImplicit (
229
- parentFunc, C.getIdentifier (" R" ), /* depth*/ 0 , /* index*/ 0 );
230
- GenericParamList *doInvokeGenericParamList =
231
- GenericParamList::create (C, sloc, {resultGenericParamDecl}, sloc);
232
-
233
- auto returnTypeIdent = C.getIdentifier (" returnType" );
234
- auto resultTyParamDecl =
235
- ParamDecl::createImplicit (C,
236
- /* argument=*/ returnTypeIdent,
237
- /* parameter=*/ returnTypeIdent,
238
- resultGenericParamDecl->getInterfaceType (), DC);
239
- ParameterList *doInvokeParamsList =
240
- ParameterList::create (C, {resultTyParamDecl});
241
-
242
- SmallVector<Requirement, 2 > requirements;
243
- for (auto p : getDistributedSerializationRequirementProtocols (systemNominal, DAS)) {
244
- auto requirement =
245
- Requirement (RequirementKind::Conformance,
246
- resultGenericParamDecl->getDeclaredInterfaceType (),
247
- p->getDeclaredInterfaceType ());
248
- requirements.push_back (requirement);
249
- }
250
- GenericSignature doInvokeGenSig =
251
- buildGenericSignature (C, parentFunc->getGenericSignature (),
252
- {resultGenericParamDecl->getDeclaredInterfaceType ()
253
- ->castTo <GenericTypeParamType>()},
254
- std::move (requirements));
255
-
256
- FuncDecl *doInvokeOnReturnFunc = FuncDecl::createImplicit (
257
- C, swift::StaticSpellingKind::None,
258
- DeclName (C, doInvokeLocalFuncIdent, doInvokeParamsList),
259
- sloc,
260
- /* async=*/ true ,
261
- /* throws=*/ true ,
262
- /* ThrownType=*/ Type (),
263
- doInvokeGenericParamList, doInvokeParamsList,
264
- /* returnType=*/ C.TheEmptyTupleType , parentFunc);
265
- doInvokeOnReturnFunc->setImplicit ();
266
- doInvokeOnReturnFunc->setSynthesized ();
267
- doInvokeOnReturnFunc->setGenericSignature (doInvokeGenSig);
268
-
269
- auto *doInvokeContext = C.Allocate <DoInvokeOnReturnContext>();
270
- doInvokeContext->handlerParam = handlerParam;
271
- doInvokeContext->resultBufferParam = resultBufParam;
272
- doInvokeOnReturnFunc->setBodySynthesizer (
273
- deriveBodyDistributed_doInvokeOnReturn, doInvokeContext);
274
-
275
- return doInvokeOnReturnFunc;
276
- }
277
-
278
- static std::pair<BraceStmt *, bool >
279
- deriveBodyDistributed_invokeHandlerOnReturn (AbstractFunctionDecl *afd,
280
- void *context) {
281
- auto implicit = true ;
282
- ASTContext &C = afd->getASTContext ();
283
- auto DC = afd->getDeclContext ();
284
- auto DAS = C.getDistributedActorSystemDecl ();
285
-
286
- // mock locations, we're a thunk and don't really need detailed locations
287
- const SourceLoc sloc = SourceLoc ();
288
- const DeclNameLoc dloc = DeclNameLoc ();
289
-
290
- NominalTypeDecl *nominal = dyn_cast<NominalTypeDecl>(DC);
291
- assert (nominal);
292
-
293
- auto func = dyn_cast<FuncDecl>(afd);
294
- assert (func);
295
-
296
- // === parameters
297
- auto params = func->getParameters ();
298
- assert (params->size () == 3 );
299
- auto handlerParam = params->get (0 );
300
- auto resultBufParam = params->get (1 );
301
- auto metatypeParam = params->get (2 );
302
-
303
- auto serializationRequirementTypeTy =
304
- getDistributedSerializationRequirementType (nominal, DAS);
305
-
306
- auto serializationRequirementMetaTypeTy =
307
- ExistentialMetatypeType::get (serializationRequirementTypeTy);
308
-
309
- // Statements
310
- SmallVector<ASTNode, 8 > stmts;
311
-
312
- // --- `let m = metatype as! SerializationRequirement.Type`
313
- VarDecl *metatypeVar =
314
- new (C) VarDecl (/* isStatic=*/ false , VarDecl::Introducer::Let, sloc,
315
- C.getIdentifier (" m" ), func);
316
- {
317
- metatypeVar->setImplicit ();
318
- metatypeVar->setSynthesized ();
319
-
320
- // metatype as! <<concrete SerializationRequirement.Type>>
321
- auto metatypeRef =
322
- new (C) DeclRefExpr (ConcreteDeclRef (metatypeParam), dloc, implicit);
323
- auto metatypeSRCastExpr = ForcedCheckedCastExpr::createImplicit (
324
- C, metatypeRef, serializationRequirementMetaTypeTy);
325
-
326
- auto metatypePattern = NamedPattern::createImplicit (C, metatypeVar);
327
- auto metatypePB = PatternBindingDecl::createImplicit (
328
- C, swift::StaticSpellingKind::None, metatypePattern,
329
- /* expr=*/ metatypeSRCastExpr, func);
330
-
331
- stmts.push_back (metatypePB);
332
- stmts.push_back (metatypeVar);
333
- }
334
-
335
- // --- Declare the local function `doInvokeOnReturn`...
336
- FuncDecl *doInvokeOnReturnFunc = createLocalFunc_doInvokeOnReturn (
337
- C, func,
338
- nominal, handlerParam, resultBufParam);
339
- stmts.push_back (doInvokeOnReturnFunc);
340
-
341
- // --- try await _openExistential(metatypeVar, do: <<doInvokeLocalFunc>>)
342
- {
343
- auto openExistentialBaseIdent = C.getIdentifier (" _openExistential" );
344
- auto doIdent = C.getIdentifier (" do" );
345
-
346
- auto openExArgs = ArgumentList::createImplicit (
347
- C, {
348
- Argument (sloc, Identifier (),
349
- new (C) DeclRefExpr (ConcreteDeclRef (metatypeVar), dloc,
350
- implicit)),
351
- Argument (sloc, doIdent,
352
- new (C) DeclRefExpr (ConcreteDeclRef (doInvokeOnReturnFunc),
353
- dloc, implicit)),
354
- });
355
- Expr *tryAwaitDoOpenExistential =
356
- CallExpr::createImplicit (C,
357
- UnresolvedDeclRefExpr::createImplicit (
358
- C, openExistentialBaseIdent),
359
- openExArgs);
360
-
361
- tryAwaitDoOpenExistential =
362
- AwaitExpr::createImplicit (C, sloc, tryAwaitDoOpenExistential);
363
- tryAwaitDoOpenExistential =
364
- TryExpr::createImplicit (C, sloc, tryAwaitDoOpenExistential);
365
-
366
- stmts.push_back (tryAwaitDoOpenExistential);
367
- }
368
-
369
- auto body = BraceStmt::create (C, sloc, {stmts}, sloc, implicit);
370
- return {body, /* isTypeChecked=*/ false };
371
- }
372
-
373
- // / Synthesizes the
374
- // /
375
- // / \verbatim
376
- // / static func invokeHandlerOnReturn(
377
- // // handler: ResultHandler,
378
- // // resultBuffer: UnsafeRawPointer,
379
- // // metatype _metatype: Any.Type
380
- // // ) async throws
381
- // / \endverbatim
382
- static FuncDecl *deriveDistributedActorSystem_invokeHandlerOnReturn (
383
- DerivedConformance &derived) {
384
- auto system = derived.Nominal ;
385
- auto &C = system->getASTContext ();
386
-
387
- // auto serializationRequirementType = getDistributedActorSystemType(decl);
388
- auto resultHandlerType = getDistributedActorSystemResultHandlerType (system);
389
- auto unsafeRawPointerType = C.getUnsafeRawPointerType ();
390
- auto anyTypeType = ExistentialMetatypeType::get (C.TheAnyType ); // Any.Type
391
-
392
- // auto serializationRequirementType =
393
- // getDistributedSerializationRequirementType(system, DAS);
394
-
395
- // params:
396
- // - handler: Self.ResultHandler
397
- // - resultBuffer:
398
- // - metatype _metatype: Any.Type
399
- auto *params = ParameterList::create (
400
- C,
401
- /* LParenLoc=*/ SourceLoc (),
402
- /* params=*/
403
- {
404
- ParamDecl::createImplicit (
405
- C, C.Id_handler , C.Id_handler ,
406
- system->mapTypeIntoContext (resultHandlerType), system),
407
- ParamDecl::createImplicit (
408
- C, C.Id_resultBuffer , C.Id_resultBuffer ,
409
- unsafeRawPointerType, system),
410
- ParamDecl::createImplicit (
411
- C, C.Id_metatype , C.Id_metatype ,
412
- anyTypeType, system)
413
- },
414
- /* RParenLoc=*/ SourceLoc ());
415
-
416
- // Func name: invokeHandlerOnReturn(handler:resultBuffer:metatype)
417
- DeclName name (C, C.Id_invokeHandlerOnReturn , params);
418
-
419
- // Expected type: (Self.ResultHandler, UnsafeRawPointer, any Any.Type) async
420
- // throws -> ()
421
- auto *funcDecl =
422
- FuncDecl::createImplicit (C, StaticSpellingKind::None, name, SourceLoc (),
423
- /* async=*/ true ,
424
- /* throws=*/ true ,
425
- /* ThrownType=*/ Type (),
426
- /* genericParams=*/ nullptr , params,
427
- /* returnType*/ TupleType::getEmpty (C), system);
428
- funcDecl->setSynthesized (true );
429
- funcDecl->copyFormalAccessFrom (system, /* sourceIsParentContext=*/ true );
430
- funcDecl->setBodySynthesizer (deriveBodyDistributed_invokeHandlerOnReturn);
431
-
432
- derived.addMembersToConformanceContext ({funcDecl});
433
- return funcDecl;
434
- }
435
-
436
124
/* *****************************************************************************/
437
125
/* ****************************** PROPERTIES ***********************************/
438
126
/* *****************************************************************************/
@@ -935,14 +623,6 @@ std::pair<Type, TypeDecl *> DerivedConformance::deriveDistributedActor(
935
623
936
624
ValueDecl *
937
625
DerivedConformance::deriveDistributedActorSystem (ValueDecl *requirement) {
938
- if (auto func = dyn_cast<FuncDecl>(requirement)) {
939
- // just a simple name check is enough here,
940
- // if we are invoked here we know for sure it is for the "right" function
941
- if (func->getName ().getBaseName () == Context.Id_invokeHandlerOnReturn ) {
942
- return deriveDistributedActorSystem_invokeHandlerOnReturn (*this );
943
- }
944
- }
945
-
946
626
return nullptr ;
947
627
}
948
628
0 commit comments