Skip to content

Commit 85a776d

Browse files
authored
Merge pull request #24062 from slavapestov/dynamic-self-fixes-5.1
DynamicSelfType fixes [5.1]
2 parents cbe56ea + c7fe50d commit 85a776d

18 files changed

+161
-89
lines changed

docs/ABI/Mangling.rst

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,9 @@ types where the metadata itself has unknown layout.)
191191
global ::= global specialization // function specialization
192192
global ::= global 'Tm' // merged function
193193
global ::= entity // some identifiable thing
194-
global ::= type type generic-signature? 'T' REABSTRACT-THUNK-TYPE // reabstraction thunk helper function
194+
global ::= from-type to-type generic-signature? 'TR' // reabstraction thunk
195+
global ::= from-type to-type self-type generic-signature? 'Ty' // reabstraction thunk with dynamic 'Self' capture
196+
global ::= from-type to-type generic-signature? 'Tr' // obsolete mangling for reabstraction thunk
195197
global ::= entity generic-signature? type type* 'TK' // key path getter
196198
global ::= entity generic-signature? type type* 'Tk' // key path setter
197199
global ::= type generic-signature 'TH' // key path equality
@@ -204,11 +206,11 @@ types where the metadata itself has unknown layout.)
204206
global ::= type assoc-type-list protocol 'TN' // default associated conformance witness accessor
205207
global ::= type protocol 'Tb' // base conformance descriptor
206208

207-
REABSTRACT-THUNK-TYPE ::= 'R' // reabstraction thunk helper function
208-
REABSTRACT-THUNK-TYPE ::= 'r' // reabstraction thunk
209+
REABSTRACT-THUNK-TYPE ::= 'R' // reabstraction thunk
210+
REABSTRACT-THUNK-TYPE ::= 'r' // reabstraction thunk (obsolete)
209211

210-
The types in a reabstraction thunk helper function are always non-polymorphic
211-
``<impl-function-type>`` types.
212+
The `from-type` and `to-type` in a reabstraction thunk helper function
213+
are always non-polymorphic ``<impl-function-type>`` types.
212214

213215
::
214216

include/swift/AST/ASTMangler.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ class ASTMangler : public Mangler {
143143

144144
std::string mangleReabstractionThunkHelper(CanSILFunctionType ThunkType,
145145
Type FromType, Type ToType,
146+
Type SelfType,
146147
ModuleDecl *Module);
147148

148149
std::string mangleKeyPathGetterThunkHelper(const AbstractStorageDecl *property,

include/swift/Demangling/DemangleNodes.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ NODE(ProtocolWitnessTableAccessor)
176176
NODE(ProtocolWitnessTablePattern)
177177
NODE(ReabstractionThunk)
178178
NODE(ReabstractionThunkHelper)
179+
NODE(ReabstractionThunkHelperWithSelf)
179180
CONTEXT_NODE(ReadAccessor)
180181
NODE(RelatedEntityDeclName)
181182
NODE(RetroactiveConformance)

lib/AST/ASTMangler.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,7 @@ std::string ASTMangler::mangleReabstractionThunkHelper(
344344
CanSILFunctionType ThunkType,
345345
Type FromType,
346346
Type ToType,
347+
Type SelfType,
347348
ModuleDecl *Module) {
348349
Mod = Module;
349350
GenericSignature *GenSig = ThunkType->getGenericSignature();
@@ -353,10 +354,17 @@ std::string ASTMangler::mangleReabstractionThunkHelper(
353354
beginMangling();
354355
appendType(FromType);
355356
appendType(ToType);
357+
if (SelfType)
358+
appendType(SelfType);
359+
356360
if (GenSig)
357361
appendGenericSignature(GenSig);
358-
// TODO: mangle ThunkType->isPseudogeneric()
359-
appendOperator("TR");
362+
363+
if (SelfType)
364+
appendOperator("Ty");
365+
else
366+
appendOperator("TR");
367+
360368
return finalize();
361369
}
362370

lib/Demangling/Demangler.cpp

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2057,15 +2057,20 @@ NodePointer Demangler::demangleThunkOrSpecialization() {
20572057
return createWithChild(Node::Kind::ProtocolSelfConformanceWitness,
20582058
popNode(isEntity));
20592059
case 'R':
2060-
case 'r': {
2061-
NodePointer Thunk = createNode(c == 'R' ?
2062-
Node::Kind::ReabstractionThunkHelper :
2063-
Node::Kind::ReabstractionThunk);
2060+
case 'r':
2061+
case 'y': {
2062+
Node::Kind kind;
2063+
if (c == 'R') kind = Node::Kind::ReabstractionThunkHelper;
2064+
else if (c == 'y') kind = Node::Kind::ReabstractionThunkHelperWithSelf;
2065+
else kind = Node::Kind::ReabstractionThunk;
2066+
NodePointer Thunk = createNode(kind);
20642067
if (NodePointer GenSig = popNode(Node::Kind::DependentGenericSignature))
20652068
addChild(Thunk, GenSig);
2066-
NodePointer Ty2 = popNode(Node::Kind::Type);
2067-
Thunk = addChild(Thunk, popNode(Node::Kind::Type));
2068-
return addChild(Thunk, Ty2);
2069+
if (kind == Node::Kind::ReabstractionThunkHelperWithSelf)
2070+
addChild(Thunk, popNode(Node::Kind::Type));
2071+
addChild(Thunk, popNode(Node::Kind::Type));
2072+
addChild(Thunk, popNode(Node::Kind::Type));
2073+
return Thunk;
20692074
}
20702075
case 'g':
20712076
return demangleGenericSpecialization(Node::Kind::GenericSpecialization);

lib/Demangling/NodePrinter.cpp

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,7 @@ class NodePrinter {
435435
case Node::Kind::ProtocolWitnessTablePattern:
436436
case Node::Kind::ReabstractionThunk:
437437
case Node::Kind::ReabstractionThunkHelper:
438+
case Node::Kind::ReabstractionThunkHelperWithSelf:
438439
case Node::Kind::ReadAccessor:
439440
case Node::Kind::RelatedEntityDeclName:
440441
case Node::Kind::RetroactiveConformance:
@@ -1536,22 +1537,40 @@ NodePointer NodePrinter::print(NodePointer Node, bool asPrefixContext) {
15361537
case Node::Kind::ReabstractionThunkHelper: {
15371538
if (Options.ShortenThunk) {
15381539
Printer << "thunk for ";
1539-
print(Node->getChild(Node->getNumChildren() - 2));
1540+
print(Node->getChild(Node->getNumChildren() - 1));
15401541
return nullptr;
15411542
}
15421543
Printer << "reabstraction thunk ";
15431544
if (Node->getKind() == Node::Kind::ReabstractionThunkHelper)
15441545
Printer << "helper ";
1545-
auto generics = getFirstChildOfKind(Node, Node::Kind::DependentGenericSignature);
1546-
assert(Node->getNumChildren() == 2 + unsigned(generics != nullptr));
1547-
if (generics) {
1546+
unsigned idx = 0;
1547+
if (Node->getNumChildren() == 3) {
1548+
auto generics = Node->getChild(0);
1549+
idx = 1;
1550+
print(generics);
1551+
Printer << " ";
1552+
}
1553+
Printer << "from ";
1554+
print(Node->getChild(idx + 1));
1555+
Printer << " to ";
1556+
print(Node->getChild(idx));
1557+
return nullptr;
1558+
}
1559+
case Node::Kind::ReabstractionThunkHelperWithSelf: {
1560+
Printer << "reabstraction thunk ";
1561+
unsigned idx = 0;
1562+
if (Node->getNumChildren() == 4) {
1563+
auto generics = Node->getChild(0);
1564+
idx = 1;
15481565
print(generics);
15491566
Printer << " ";
15501567
}
15511568
Printer << "from ";
1552-
print(Node->getChild(Node->getNumChildren() - 2));
1569+
print(Node->getChild(idx + 2));
15531570
Printer << " to ";
1554-
print(Node->getChild(Node->getNumChildren() - 1));
1571+
print(Node->getChild(idx + 1));
1572+
Printer << " self ";
1573+
print(Node->getChild(idx));
15551574
return nullptr;
15561575
}
15571576
case Node::Kind::MergedFunction:

lib/Demangling/OldRemangler.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -715,15 +715,15 @@ void Remangler::mangleBaseWitnessTableAccessor(Node *node) {
715715
}
716716

717717
void Remangler::mangleReabstractionThunkHelper(Node *node) {
718-
Buffer << "TR";
719-
if (node->getNumChildren() == 3) Buffer << 'G';
720-
mangleChildNodes(node); // generic signature?, type, type
718+
Buffer << "<reabstraction-thunk-helper>";
719+
}
720+
721+
void Remangler::mangleReabstractionThunkHelperWithSelf(Node *node) {
722+
Buffer << "<reabstraction-thunk-helper-with-self>";
721723
}
722724

723725
void Remangler::mangleReabstractionThunk(Node *node) {
724-
Buffer << "Tr";
725-
if (node->getNumChildren() == 3) Buffer << 'G';
726-
mangleChildNodes(node); // generic signature?, type, type
726+
Buffer << "<reabstraction-thunk>";
727727
}
728728

729729
void Remangler::mangleProtocolSelfConformanceWitness(Node *node) {

lib/Demangling/Remangler.cpp

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1856,27 +1856,20 @@ void Remangler::mangleProtocolWitnessTableAccessor(Node *node) {
18561856
}
18571857

18581858
void Remangler::mangleReabstractionThunk(Node *node) {
1859-
if (node->getNumChildren() == 3) {
1860-
mangleChildNode(node, 1); // type 1
1861-
mangleChildNode(node, 2); // type 2
1862-
mangleChildNode(node, 0); // generic signature
1863-
} else {
1864-
mangleChildNodes(node);
1865-
}
1859+
mangleChildNodesReversed(node);
18661860
Buffer << "Tr";
18671861
}
18681862

18691863
void Remangler::mangleReabstractionThunkHelper(Node *node) {
1870-
if (node->getNumChildren() == 3) {
1871-
mangleChildNode(node, 1); // type 1
1872-
mangleChildNode(node, 2); // type 2
1873-
mangleChildNode(node, 0); // generic signature
1874-
} else {
1875-
mangleChildNodes(node);
1876-
}
1864+
mangleChildNodesReversed(node);
18771865
Buffer << "TR";
18781866
}
18791867

1868+
void Remangler::mangleReabstractionThunkHelperWithSelf(Node *node) {
1869+
mangleChildNodesReversed(node);
1870+
Buffer << "Ty";
1871+
}
1872+
18801873
void Remangler::mangleReadAccessor(Node *node) {
18811874
mangleAbstractStorage(node->getFirstChild(), "r");
18821875
}

lib/SILGen/SILGen.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ class LLVM_LIBRARY_VISIBILITY SILGenModule : public ASTVisitor<SILGenModule> {
173173
CanSILFunctionType thunkType,
174174
CanSILFunctionType fromType,
175175
CanSILFunctionType toType,
176-
IsSerialized_t Serialized);
176+
CanType dynamicSelfType);
177177

178178
/// Determine whether the given class has any instance variables that
179179
/// need to be destroyed.

lib/SILGen/SILGenBridging.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -577,7 +577,7 @@ ManagedValue SILGenFunction::emitFuncToBlock(SILLocation loc,
577577
auto thunk = SGM.getOrCreateReabstractionThunk(invokeTy,
578578
loweredFuncTy,
579579
loweredBlockTy,
580-
F.isSerialized());
580+
/*dynamicSelfType=*/CanType());
581581

582582
// Build it if necessary.
583583
if (thunk->empty()) {
@@ -937,7 +937,7 @@ SILGenFunction::emitBlockToFunc(SILLocation loc,
937937
auto thunk = SGM.getOrCreateReabstractionThunk(thunkTy,
938938
loweredBlockTy,
939939
loweredFuncTyWithoutNoEscape,
940-
F.isSerialized());
940+
/*dynamicSelfType=*/CanType());
941941

942942
// Build it if necessary.
943943
if (thunk->empty()) {

lib/SILGen/SILGenPoly.cpp

Lines changed: 44 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3041,16 +3041,23 @@ CanSILFunctionType SILGenFunction::buildThunkType(
30413041
expectedType = cast<SILFunctionType>(
30423042
substIntoThunkContext(expectedType));
30433043

3044+
bool hasDynamicSelf = false;
3045+
30443046
if (inputSubstType) {
30453047
inputSubstType = cast<AnyFunctionType>(
30463048
substIntoThunkContext(inputSubstType));
3049+
hasDynamicSelf |= inputSubstType->hasDynamicSelfType();
30473050
}
30483051

30493052
if (outputSubstType) {
30503053
outputSubstType = cast<AnyFunctionType>(
30513054
substIntoThunkContext(outputSubstType));
3055+
hasDynamicSelf |= outputSubstType->hasDynamicSelfType();
30523056
}
30533057

3058+
hasDynamicSelf |= sourceType->hasDynamicSelfType();
3059+
hasDynamicSelf |= expectedType->hasDynamicSelfType();
3060+
30543061
// If our parent function was pseudogeneric, this thunk must also be
30553062
// pseudogeneric, since we have no way to pass generic parameters.
30563063
if (genericSig)
@@ -3072,8 +3079,7 @@ CanSILFunctionType SILGenFunction::buildThunkType(
30723079

30733080
// If this thunk involves DynamicSelfType in any way, add a capture for it
30743081
// in case we need to recover metadata.
3075-
if (sourceType->hasDynamicSelfType() ||
3076-
expectedType->hasDynamicSelfType()) {
3082+
if (hasDynamicSelf) {
30773083
dynamicSelfType = F.getSelfMetadataArgument()->getType().getASTType();
30783084
if (!isa<MetatypeType>(dynamicSelfType)) {
30793085
dynamicSelfType = CanMetatypeType::get(dynamicSelfType,
@@ -3127,6 +3133,36 @@ CanSILFunctionType SILGenFunction::buildThunkType(
31273133
getASTContext());
31283134
}
31293135

3136+
static ManagedValue createPartialApplyOfThunk(SILGenFunction &SGF,
3137+
SILLocation loc,
3138+
SILFunction *thunk,
3139+
SubstitutionMap interfaceSubs,
3140+
CanType dynamicSelfType,
3141+
CanSILFunctionType toType,
3142+
ManagedValue fn) {
3143+
CanSILFunctionType substFnType = thunk->getLoweredFunctionType();
3144+
3145+
if (substFnType->getGenericSignature()) {
3146+
substFnType = substFnType->substGenericArgs(SGF.F.getModule(),
3147+
interfaceSubs);
3148+
}
3149+
3150+
auto thunkValue = SGF.B.createFunctionRefFor(loc, thunk);
3151+
SmallVector<ManagedValue, 2> thunkArgs;
3152+
thunkArgs.push_back(fn);
3153+
if (dynamicSelfType) {
3154+
SILType dynamicSILType = SGF.getLoweredType(dynamicSelfType);
3155+
SILValue value = SGF.B.createMetatype(loc, dynamicSILType);
3156+
thunkArgs.push_back(ManagedValue::forUnmanaged(value));
3157+
}
3158+
3159+
return
3160+
SGF.B.createPartialApply(loc, thunkValue,
3161+
SILType::getPrimitiveObjectType(substFnType),
3162+
interfaceSubs, thunkArgs,
3163+
SILType::getPrimitiveObjectType(toType));
3164+
}
3165+
31303166
/// Create a reabstraction thunk.
31313167
static ManagedValue createThunk(SILGenFunction &SGF,
31323168
SILLocation loc,
@@ -3160,7 +3196,7 @@ static ManagedValue createThunk(SILGenFunction &SGF,
31603196
thunkType,
31613197
sourceType,
31623198
toType,
3163-
SGF.F.isSerialized());
3199+
dynamicSelfType);
31643200

31653201
// Build it if necessary.
31663202
if (thunk->empty()) {
@@ -3175,28 +3211,9 @@ static ManagedValue createThunk(SILGenFunction &SGF,
31753211
dynamicSelfType);
31763212
}
31773213

3178-
CanSILFunctionType substFnType = thunkType;
3179-
3180-
if (thunkType->getGenericSignature()) {
3181-
substFnType = thunkType->substGenericArgs(SGF.F.getModule(),
3182-
interfaceSubs);
3183-
}
3184-
3185-
// Create it in our current function.
3186-
auto thunkValue = SGF.B.createFunctionRefFor(loc, thunk);
3187-
SmallVector<ManagedValue, 2> thunkArgs;
3188-
thunkArgs.push_back(fn.ensurePlusOne(SGF, loc));
3189-
if (dynamicSelfType) {
3190-
SILType dynamicSILType = SGF.getLoweredType(dynamicSelfType);
3191-
SILValue value = SGF.B.createMetatype(loc, dynamicSILType);
3192-
thunkArgs.push_back(ManagedValue::forUnmanaged(value));
3193-
}
3194-
3195-
ManagedValue thunkedFn =
3196-
SGF.B.createPartialApply(loc, thunkValue,
3197-
SILType::getPrimitiveObjectType(substFnType),
3198-
interfaceSubs, thunkArgs,
3199-
SILType::getPrimitiveObjectType(toType));
3214+
auto thunkedFn =
3215+
createPartialApplyOfThunk(SGF, loc, thunk, interfaceSubs, dynamicSelfType,
3216+
toType, fn.ensurePlusOne(SGF, loc));
32003217

32013218
if (!expectedType->isNoEscape()) {
32023219
return thunkedFn;
@@ -3284,7 +3301,8 @@ SILGenFunction::createWithoutActuallyEscapingClosure(
32843301
*this, noEscapingFnTy, escapingFnTy, genericEnv, interfaceSubs);
32853302

32863303
auto *thunk = SGM.getOrCreateReabstractionThunk(
3287-
thunkType, noEscapingFnTy, escapingFnTy, F.isSerialized());
3304+
thunkType, noEscapingFnTy, escapingFnTy,
3305+
/*dynamicSelfType=*/CanType());
32883306

32893307
if (thunk->empty()) {
32903308
thunk->setWithoutActuallyEscapingThunk();

lib/SILGen/SILGenThunk.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ SILFunction *SILGenModule::
300300
getOrCreateReabstractionThunk(CanSILFunctionType thunkType,
301301
CanSILFunctionType fromType,
302302
CanSILFunctionType toType,
303-
IsSerialized_t Serialized) {
303+
CanType dynamicSelfType) {
304304
// The reference to the thunk is likely @noescape, but declarations are always
305305
// escaping.
306306
auto thunkDeclType =
@@ -315,7 +315,8 @@ getOrCreateReabstractionThunk(CanSILFunctionType thunkType,
315315

316316
Mangle::ASTMangler NewMangler;
317317
std::string name = NewMangler.mangleReabstractionThunkHelper(thunkType,
318-
fromInterfaceType, toInterfaceType, M.getSwiftModule());
318+
fromInterfaceType, toInterfaceType, dynamicSelfType,
319+
M.getSwiftModule());
319320

320321
auto loc = RegularLocation::getAutoGeneratedLocation();
321322

lib/Sema/ConstraintSystem.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2197,7 +2197,8 @@ Type simplifyTypeImpl(ConstraintSystem &cs, Type type, Fn getFixedTypeFn) {
21972197
// through lvalue, inout and IUO types here
21982198
Type lookupBaseType = newBase->getWithoutSpecifierType();
21992199

2200-
if (lookupBaseType->mayHaveMembers()) {
2200+
if (lookupBaseType->mayHaveMembers() ||
2201+
lookupBaseType->is<DynamicSelfType>()) {
22012202
auto *proto = assocType->getProtocol();
22022203
auto conformance = cs.DC->getParentModule()->lookupConformance(
22032204
lookupBaseType, proto);

0 commit comments

Comments
 (0)