Skip to content

Commit 6f5cffb

Browse files
committed
Mangling: add support for mangling the body-function of asyncHandlers
We don't introduce a new mangling here. To distinguish the names of the original asyncHandler function and it's generated "body-function", we just mangle the body-function with an async attribute, i.e. as if it was declared as async. This change is mostly to pass information to the ASTMangler to mangle a not async function as "async".
1 parent c724153 commit 6f5cffb

File tree

4 files changed

+39
-18
lines changed

4 files changed

+39
-18
lines changed

include/swift/AST/ASTMangler.h

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ class ASTMangler : public Mangler {
7777
public:
7878
enum class SymbolKind {
7979
Default,
80+
AsyncHandlerBody,
8081
DynamicThunk,
8182
SwiftAsObjCThunk,
8283
ObjCAsSwiftThunk,
@@ -323,16 +324,24 @@ class ASTMangler : public Mangler {
323324

324325
void appendAnyGenericType(const GenericTypeDecl *decl);
325326

326-
void appendFunction(AnyFunctionType *fn, bool isFunctionMangling = false,
327-
const ValueDecl *forDecl = nullptr);
327+
enum FunctionManglingKind {
328+
NoFunctionMangling,
329+
FunctionMangling,
330+
AsyncHandlerBodyMangling
331+
};
332+
333+
void appendFunction(AnyFunctionType *fn,
334+
FunctionManglingKind functionMangling = NoFunctionMangling,
335+
const ValueDecl *forDecl = nullptr);
328336
void appendFunctionType(AnyFunctionType *fn, bool isAutoClosure = false,
329337
const ValueDecl *forDecl = nullptr);
330338
void appendClangType(AnyFunctionType *fn);
331339
template <typename FnType>
332340
void appendClangType(FnType *fn, llvm::raw_svector_ostream &os);
333341

334342
void appendFunctionSignature(AnyFunctionType *fn,
335-
const ValueDecl *forDecl = nullptr);
343+
const ValueDecl *forDecl,
344+
FunctionManglingKind functionMangling);
336345

337346
void appendFunctionInputType(ArrayRef<AnyFunctionType::Param> params,
338347
const ValueDecl *forDecl = nullptr);
@@ -383,7 +392,10 @@ class ASTMangler : public Mangler {
383392
GenericSignature &genericSig,
384393
GenericSignature &parentGenericSig);
385394

386-
void appendDeclType(const ValueDecl *decl, bool isFunctionMangling = false);
395+
396+
397+
void appendDeclType(const ValueDecl *decl,
398+
FunctionManglingKind functionMangling = NoFunctionMangling);
387399

388400
bool tryAppendStandardSubstitution(const GenericTypeDecl *type);
389401

@@ -400,7 +412,7 @@ class ASTMangler : public Mangler {
400412

401413
void appendEntity(const ValueDecl *decl, StringRef EntityOp, bool isStatic);
402414

403-
void appendEntity(const ValueDecl *decl);
415+
void appendEntity(const ValueDecl *decl, bool isAsyncHandlerBody = false);
404416

405417
void appendProtocolConformance(const ProtocolConformance *conformance);
406418
void appendProtocolConformanceRef(const RootProtocolConformance *conformance);

include/swift/SIL/SILDeclRef.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@ struct SILDeclRef {
221221
enum class ManglingKind {
222222
Default,
223223
DynamicThunk,
224+
AsyncHandlerBody
224225
};
225226

226227
/// Produce a mangled form of this constant.

lib/AST/ASTMangler.cpp

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ std::string ASTMangler::mangleClosureEntity(const AbstractClosureExpr *closure,
8888

8989
std::string ASTMangler::mangleEntity(const ValueDecl *decl, SymbolKind SKind) {
9090
beginMangling();
91-
appendEntity(decl);
91+
appendEntity(decl, SKind == SymbolKind::AsyncHandlerBody);
9292
appendSymbolKind(SKind);
9393
return finalize();
9494
}
@@ -657,7 +657,7 @@ std::string ASTMangler::mangleTypeAsUSR(Type Ty) {
657657
Ty = getTypeForDWARFMangling(Ty);
658658

659659
if (auto *fnType = Ty->getAs<AnyFunctionType>()) {
660-
appendFunction(fnType, false);
660+
appendFunction(fnType);
661661
} else {
662662
appendType(Ty);
663663
}
@@ -744,6 +744,7 @@ std::string ASTMangler::mangleOpaqueTypeDecl(const ValueDecl *decl) {
744744
void ASTMangler::appendSymbolKind(SymbolKind SKind) {
745745
switch (SKind) {
746746
case SymbolKind::Default: return;
747+
case SymbolKind::AsyncHandlerBody: return;
747748
case SymbolKind::DynamicThunk: return appendOperator("TD");
748749
case SymbolKind::SwiftAsObjCThunk: return appendOperator("To");
749750
case SymbolKind::ObjCAsSwiftThunk: return appendOperator("TO");
@@ -2249,7 +2250,8 @@ void ASTMangler::appendAnyGenericType(const GenericTypeDecl *decl) {
22492250
addSubstitution(cast<TypeAliasDecl>(decl));
22502251
}
22512252

2252-
void ASTMangler::appendFunction(AnyFunctionType *fn, bool isFunctionMangling,
2253+
void ASTMangler::appendFunction(AnyFunctionType *fn,
2254+
FunctionManglingKind functionMangling,
22532255
const ValueDecl *forDecl) {
22542256
// Append parameter labels right before the signature/type.
22552257
auto parameters = fn->getParams();
@@ -2269,8 +2271,8 @@ void ASTMangler::appendFunction(AnyFunctionType *fn, bool isFunctionMangling,
22692271
appendOperator("y");
22702272
}
22712273

2272-
if (isFunctionMangling) {
2273-
appendFunctionSignature(fn, forDecl);
2274+
if (functionMangling != NoFunctionMangling) {
2275+
appendFunctionSignature(fn, forDecl, functionMangling);
22742276
} else {
22752277
appendFunctionType(fn, /*autoclosure*/ false, forDecl);
22762278
}
@@ -2281,7 +2283,7 @@ void ASTMangler::appendFunctionType(AnyFunctionType *fn, bool isAutoClosure,
22812283
assert((DWARFMangling || fn->isCanonical()) &&
22822284
"expecting canonical types when not mangling for the debugger");
22832285

2284-
appendFunctionSignature(fn, forDecl);
2286+
appendFunctionSignature(fn, forDecl, NoFunctionMangling);
22852287

22862288
bool mangleClangType = fn->getASTContext().LangOpts.UseClangFunctionTypes &&
22872289
fn->hasNonDerivableClangType();
@@ -2359,10 +2361,11 @@ void ASTMangler::appendClangType(AnyFunctionType *fn) {
23592361
}
23602362

23612363
void ASTMangler::appendFunctionSignature(AnyFunctionType *fn,
2362-
const ValueDecl *forDecl) {
2364+
const ValueDecl *forDecl,
2365+
FunctionManglingKind functionMangling) {
23632366
appendFunctionResultType(fn->getResult(), forDecl);
23642367
appendFunctionInputType(fn->getParams(), forDecl);
2365-
if (fn->isAsync())
2368+
if (fn->isAsync() || functionMangling == AsyncHandlerBodyMangling)
23662369
appendOperator("Y");
23672370
if (fn->isThrowing())
23682371
appendOperator("K");
@@ -2780,22 +2783,23 @@ CanType ASTMangler::getDeclTypeForMangling(
27802783
return canTy;
27812784
}
27822785

2783-
void ASTMangler::appendDeclType(const ValueDecl *decl, bool isFunctionMangling) {
2786+
void ASTMangler::appendDeclType(const ValueDecl *decl,
2787+
FunctionManglingKind functionMangling) {
27842788
Mod = decl->getModuleContext();
27852789
GenericSignature genericSig;
27862790
GenericSignature parentGenericSig;
27872791
auto type = getDeclTypeForMangling(decl, genericSig, parentGenericSig);
27882792

27892793
if (AnyFunctionType *FuncTy = type->getAs<AnyFunctionType>()) {
2790-
appendFunction(FuncTy, isFunctionMangling, decl);
2794+
appendFunction(FuncTy, functionMangling, decl);
27912795
} else {
27922796
appendType(type, decl);
27932797
}
27942798

27952799
// Mangle the generic signature, if any.
27962800
if (genericSig && appendGenericSignature(genericSig, parentGenericSig)) {
27972801
// The 'F' function mangling doesn't need a 'u' for its generic signature.
2798-
if (!isFunctionMangling)
2802+
if (functionMangling == NoFunctionMangling)
27992803
appendOperator("u");
28002804
}
28012805
}
@@ -2870,7 +2874,7 @@ void ASTMangler::appendEntity(const ValueDecl *decl, StringRef EntityOp,
28702874
appendOperator("Z");
28712875
}
28722876

2873-
void ASTMangler::appendEntity(const ValueDecl *decl) {
2877+
void ASTMangler::appendEntity(const ValueDecl *decl, bool isAsyncHandlerBody) {
28742878
assert(!isa<ConstructorDecl>(decl));
28752879
assert(!isa<DestructorDecl>(decl));
28762880

@@ -2891,7 +2895,8 @@ void ASTMangler::appendEntity(const ValueDecl *decl) {
28912895

28922896
appendContextOf(decl);
28932897
appendDeclName(decl);
2894-
appendDeclType(decl, /*isFunctionMangling*/ true);
2898+
appendDeclType(decl, isAsyncHandlerBody ? AsyncHandlerBodyMangling
2899+
: FunctionMangling);
28952900
appendOperator("F");
28962901
if (decl->isStatic())
28972902
appendOperator("Z");

lib/SIL/IR/SILDeclRef.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -764,6 +764,9 @@ std::string SILDeclRef::mangle(ManglingKind MKind) const {
764764
case SILDeclRef::ManglingKind::DynamicThunk:
765765
SKind = ASTMangler::SymbolKind::DynamicThunk;
766766
break;
767+
case SILDeclRef::ManglingKind::AsyncHandlerBody:
768+
SKind = ASTMangler::SymbolKind::AsyncHandlerBody;
769+
break;
767770
}
768771

769772
switch (kind) {

0 commit comments

Comments
 (0)