@@ -180,6 +180,27 @@ static ValueDecl *rewriteIntegerTypes(SubstitutionMap subst, ValueDecl *oldDecl,
180180 return newDecl;
181181}
182182
183+ static Expr *createSelfExpr (FuncDecl *fnDecl) {
184+ ASTContext &ctx = fnDecl->getASTContext ();
185+
186+ auto selfDecl = fnDecl->getImplicitSelfDecl ();
187+ auto selfRefExpr = new (ctx) DeclRefExpr (selfDecl, DeclNameLoc (),
188+ /* implicit*/ true );
189+
190+ if (!fnDecl->isMutating ()) {
191+ selfRefExpr->setType (selfDecl->getInterfaceType ());
192+ return selfRefExpr;
193+ }
194+ selfRefExpr->setType (LValueType::get (selfDecl->getInterfaceType ()));
195+
196+ auto inoutSelfExpr = new (ctx) InOutExpr (
197+ SourceLoc (), selfRefExpr,
198+ fnDecl->mapTypeIntoContext (selfDecl->getValueInterfaceType ()),
199+ /* isImplicit*/ true );
200+ inoutSelfExpr->setType (InOutType::get (selfDecl->getInterfaceType ()));
201+ return inoutSelfExpr;
202+ }
203+
183204// Synthesize a thunk body for the function created in
184205// "addThunkForDependentTypes". This will just cast all params and forward them
185206// along to the specialized function. It will also cast the result before
@@ -217,10 +238,27 @@ synthesizeDependentTypeThunkParamForwarding(AbstractFunctionDecl *afd, void *con
217238 paramIndex++;
218239 }
219240
220- auto *specializedFuncDeclRef = new (ctx) DeclRefExpr (ConcreteDeclRef (specializedFuncDecl),
241+ Expr *specializedFuncDeclRef = new (ctx) DeclRefExpr (ConcreteDeclRef (specializedFuncDecl),
221242 DeclNameLoc (), true );
222243 specializedFuncDeclRef->setType (specializedFuncDecl->getInterfaceType ());
223244
245+ if (specializedFuncDecl->isInstanceMember ()) {
246+ auto selfExpr = createSelfExpr (thunkDecl);
247+ auto *memberCall = DotSyntaxCallExpr::create (ctx, specializedFuncDeclRef, SourceLoc (), selfExpr);
248+ memberCall->setThrows (false );
249+ auto resultType = specializedFuncDecl->getInterfaceType ()->getAs <FunctionType>()->getResult ();
250+ specializedFuncDeclRef = memberCall;
251+ specializedFuncDeclRef->setType (resultType);
252+ } else if (specializedFuncDecl->isStatic ()) {
253+ auto resultType = specializedFuncDecl->getInterfaceType ()->getAs <FunctionType>()->getResult ();
254+ auto selfType = cast<NominalTypeDecl>(thunkDecl->getDeclContext ()->getAsDecl ())->getDeclaredInterfaceType ();
255+ auto selfTypeExpr = TypeExpr::createImplicit (selfType, ctx);
256+ auto *memberCall = DotSyntaxCallExpr::create (ctx, specializedFuncDeclRef, SourceLoc (), selfTypeExpr);
257+ memberCall->setThrows (false );
258+ specializedFuncDeclRef = memberCall;
259+ specializedFuncDeclRef->setType (resultType);
260+ }
261+
224262 auto argList = ArgumentList::createImplicit (ctx, forwardingParams);
225263 auto *specializedFuncCallExpr = CallExpr::createImplicit (ctx, specializedFuncDeclRef, argList);
226264 specializedFuncCallExpr->setType (specializedFuncDecl->getResultInterfaceType ());
@@ -291,6 +329,7 @@ static ValueDecl *addThunkForDependentTypes(FuncDecl *oldDecl,
291329 newFnDecl->copyFormalAccessFrom (newDecl);
292330 newFnDecl->setBodySynthesizer (synthesizeDependentTypeThunkParamForwarding, newDecl);
293331 newFnDecl->setSelfAccessKind (newDecl->getSelfAccessKind ());
332+ if (newDecl->isStatic ()) newFnDecl->setStatic ();
294333 newFnDecl->getAttrs ().add (
295334 new (newDecl->getASTContext ()) TransparentAttr (/* IsImplicit=*/ true ));
296335 return newFnDecl;
@@ -320,10 +359,27 @@ synthesizeForwardingThunkBody(AbstractFunctionDecl *afd, void *context) {
320359 forwardingParams.push_back (Argument (SourceLoc (), Identifier (), paramRefExpr));
321360 }
322361
323- auto *specializedFuncDeclRef = new (ctx) DeclRefExpr (ConcreteDeclRef (specializedFuncDecl),
362+ Expr *specializedFuncDeclRef = new (ctx) DeclRefExpr (ConcreteDeclRef (specializedFuncDecl),
324363 DeclNameLoc (), true );
325364 specializedFuncDeclRef->setType (specializedFuncDecl->getInterfaceType ());
326365
366+ if (specializedFuncDecl->isInstanceMember ()) {
367+ auto selfExpr = createSelfExpr (thunkDecl);
368+ auto *memberCall = DotSyntaxCallExpr::create (ctx, specializedFuncDeclRef, SourceLoc (), selfExpr);
369+ memberCall->setThrows (false );
370+ auto resultType = specializedFuncDecl->getInterfaceType ()->getAs <FunctionType>()->getResult ();
371+ specializedFuncDeclRef = memberCall;
372+ specializedFuncDeclRef->setType (resultType);
373+ } else if (specializedFuncDecl->isStatic ()) {
374+ auto resultType = specializedFuncDecl->getInterfaceType ()->getAs <FunctionType>()->getResult ();
375+ auto selfType = cast<NominalTypeDecl>(thunkDecl->getDeclContext ()->getAsDecl ())->getDeclaredInterfaceType ();
376+ auto selfTypeExpr = TypeExpr::createImplicit (selfType, ctx);
377+ auto *memberCall = DotSyntaxCallExpr::create (ctx, specializedFuncDeclRef, SourceLoc (), selfTypeExpr);
378+ memberCall->setThrows (false );
379+ specializedFuncDeclRef = memberCall;
380+ specializedFuncDeclRef->setType (resultType);
381+ }
382+
327383 auto argList = ArgumentList::createImplicit (ctx, forwardingParams);
328384 auto *specializedFuncCallExpr = CallExpr::createImplicit (ctx, specializedFuncDeclRef, argList);
329385 specializedFuncCallExpr->setType (thunkDecl->getResultInterfaceType ());
@@ -391,6 +447,7 @@ static ValueDecl *generateThunkForExtraMetatypes(SubstitutionMap subst,
391447 thunk->copyFormalAccessFrom (newDecl);
392448 thunk->setBodySynthesizer (synthesizeForwardingThunkBody, newDecl);
393449 thunk->setSelfAccessKind (newDecl->getSelfAccessKind ());
450+ if (newDecl->isStatic ()) thunk->setStatic ();
394451 thunk->getAttrs ().add (
395452 new (newDecl->getASTContext ()) TransparentAttr (/* IsImplicit=*/ true ));
396453
0 commit comments