Skip to content

Commit 8595ebf

Browse files
committed
Rework opaque archetype mangling to be representation agnostic.
1 parent 2dbaab9 commit 8595ebf

File tree

2 files changed

+55
-37
lines changed

2 files changed

+55
-37
lines changed

include/swift/AST/ASTMangler.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,11 @@ class ASTMangler : public Mangler {
365365
ModuleDecl *fromModule);
366366
void appendImplFunctionType(SILFunctionType *fn, GenericSignature sig,
367367
const ValueDecl *forDecl = nullptr);
368+
void appendOpaqueTypeArchetype(ArchetypeType *archetype,
369+
OpaqueTypeDecl *opaqueDecl,
370+
SubstitutionMap subs,
371+
GenericSignature sig,
372+
const ValueDecl *forDecl);
368373

369374
void appendContextOf(const ValueDecl *decl);
370375

lib/AST/ASTMangler.cpp

Lines changed: 50 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1318,35 +1318,10 @@ void ASTMangler::appendType(Type type, GenericSignature sig,
13181318
llvm_unreachable("Cannot mangle free-standing archetypes");
13191319

13201320
case TypeKind::OpaqueTypeArchetype: {
1321-
// If this is the opaque return type of the declaration currently being
1322-
// mangled, use a short mangling to represent it.
13231321
auto opaqueType = cast<OpaqueTypeArchetypeType>(tybase);
13241322
auto opaqueDecl = opaqueType->getDecl();
1325-
if (opaqueDecl->getNamingDecl() == forDecl) {
1326-
assert(opaqueType->getSubstitutions().isIdentity());
1327-
if (opaqueType->getOrdinal() == 0)
1328-
return appendOperator("Qr");
1329-
1330-
return appendOperator("QR", Index(opaqueType->getOrdinal() - 1));
1331-
}
1332-
1333-
// Otherwise, try to substitute it.
1334-
if (tryMangleTypeSubstitution(type, sig))
1335-
return;
1336-
1337-
// Use the fully elaborated explicit mangling.
1338-
appendOpaqueDeclName(opaqueDecl);
1339-
bool isFirstArgList = true;
1340-
appendBoundGenericArgs(opaqueDecl, sig,
1341-
opaqueType->getSubstitutions(),
1342-
isFirstArgList, forDecl);
1343-
appendRetroactiveConformances(opaqueType->getSubstitutions(), sig,
1344-
opaqueDecl->getParentModule());
1345-
1346-
appendOperator("Qo", Index(opaqueType->getOrdinal()));
1347-
1348-
addTypeSubstitution(type, sig);
1349-
return;
1323+
return appendOpaqueTypeArchetype(
1324+
opaqueType, opaqueDecl, opaqueType->getSubstitutions(), sig, forDecl);
13501325
}
13511326

13521327
case TypeKind::NestedArchetype: {
@@ -1356,16 +1331,9 @@ void ASTMangler::appendType(Type type, GenericSignature sig,
13561331
// types, so that they can be accurately demangled at runtime.
13571332
if (auto opaque =
13581333
dyn_cast<OpaqueTypeArchetypeType>(nestedType->getRoot())) {
1359-
if (tryMangleTypeSubstitution(nestedType, sig))
1360-
return;
1361-
1362-
appendType(opaque, sig, forDecl);
1363-
bool isAssocTypeAtDepth = false;
1364-
appendAssocType(nestedType->getInterfaceType(),
1365-
sig, isAssocTypeAtDepth);
1366-
appendOperator(isAssocTypeAtDepth ? "QX" : "Qx");
1367-
addTypeSubstitution(nestedType, sig);
1368-
return;
1334+
return appendOpaqueTypeArchetype(
1335+
nestedType, opaque->getDecl(), opaque->getSubstitutions(), sig,
1336+
forDecl);
13691337
}
13701338

13711339
// FIXME: Never actually used.
@@ -1958,6 +1926,51 @@ void ASTMangler::appendImplFunctionType(SILFunctionType *fn,
19581926
appendOperator("I", StringRef(OpArgs.data(), OpArgs.size()));
19591927
}
19601928

1929+
void ASTMangler::appendOpaqueTypeArchetype(ArchetypeType *archetype,
1930+
OpaqueTypeDecl *opaqueDecl,
1931+
SubstitutionMap subs,
1932+
GenericSignature sig,
1933+
const ValueDecl *forDecl) {
1934+
Type interfaceType = archetype->getInterfaceType();
1935+
auto genericParam = interfaceType->getAs<GenericTypeParamType>();
1936+
1937+
// If this is the opaque return type of the declaration currently being
1938+
// mangled, use a short mangling to represent it.
1939+
if (genericParam && opaqueDecl->getNamingDecl() == forDecl) {
1940+
assert(subs.isIdentity());
1941+
if (genericParam->getIndex() == 0)
1942+
return appendOperator("Qr");
1943+
1944+
return appendOperator("QR", Index(genericParam->getIndex() - 1));
1945+
}
1946+
1947+
// Otherwise, try to substitute it.
1948+
if (tryMangleTypeSubstitution(Type(archetype), sig))
1949+
return;
1950+
1951+
// Mangling at the root, described by a generic parameter.
1952+
if (genericParam) {
1953+
// Use the fully elaborated explicit mangling.
1954+
appendOpaqueDeclName(opaqueDecl);
1955+
bool isFirstArgList = true;
1956+
appendBoundGenericArgs(opaqueDecl, sig, subs, isFirstArgList, forDecl);
1957+
appendRetroactiveConformances(subs, sig, opaqueDecl->getParentModule());
1958+
1959+
appendOperator("Qo", Index(genericParam->getIndex()));
1960+
} else {
1961+
// Mangle associated types of opaque archetypes like dependent member
1962+
// types, so that they can be accurately demangled at runtime.
1963+
appendType(Type(archetype->getRoot()), sig, forDecl);
1964+
bool isAssocTypeAtDepth = false;
1965+
appendAssocType(
1966+
archetype->getInterfaceType()->castTo<DependentMemberType>(),
1967+
sig, isAssocTypeAtDepth);
1968+
appendOperator(isAssocTypeAtDepth ? "QX" : "Qx");
1969+
}
1970+
1971+
addTypeSubstitution(Type(archetype), sig);
1972+
}
1973+
19611974
Optional<ASTMangler::SpecialContext>
19621975
ASTMangler::getSpecialManglingContext(const ValueDecl *decl,
19631976
bool useObjCProtocolNames) {

0 commit comments

Comments
 (0)