Skip to content

Commit aad0baf

Browse files
authored
Merge pull request #9228 from eeckstein/old-objc-type-mangling
2 parents edd13ec + ddecb64 commit aad0baf

File tree

10 files changed

+1938
-22
lines changed

10 files changed

+1938
-22
lines changed

include/swift/Demangling/Demangle.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,12 @@ void mangleIdentifier(const char *data, size_t length,
409409
/// This should always round-trip perfectly with demangleSymbolAsNode.
410410
std::string mangleNode(const NodePointer &root);
411411

412+
/// Remangle in the old mangling scheme.
413+
///
414+
/// This is only used for objc-runtime names and should be removed as soon as
415+
/// we switch to the new mangling for those names as well.
416+
std::string mangleNodeOld(const NodePointer &root);
417+
412418
/// \brief Transform the node structure to a string.
413419
///
414420
/// Typical usage:

lib/AST/ASTMangler.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "swift/AST/ParameterList.h"
2525
#include "swift/AST/ProtocolConformance.h"
2626
#include "swift/Demangling/ManglingUtils.h"
27+
#include "swift/Demangling/Demangler.h"
2728
#include "swift/Strings.h"
2829
#include "clang/Basic/CharInfo.h"
2930
#include "clang/AST/Attr.h"
@@ -301,12 +302,18 @@ std::string ASTMangler::mangleDeclType(const ValueDecl *decl) {
301302
return finalize();
302303
}
303304

305+
#ifdef USE_NEW_MANGLING_FOR_OBJC_RUNTIME_NAMES
304306
static bool isPrivate(const NominalTypeDecl *Nominal) {
305307
return Nominal->hasAccessibility() &&
306308
Nominal->getFormalAccess() <= Accessibility::FilePrivate;
307309
}
310+
#endif
308311

309312
std::string ASTMangler::mangleObjCRuntimeName(const NominalTypeDecl *Nominal) {
313+
#ifdef USE_NEW_MANGLING_FOR_OBJC_RUNTIME_NAMES
314+
// Using the new mangling for ObjC runtime names (except for top-level
315+
// classes). This is currently disabled to support old archives.
316+
// TODO: re-enable this as we switch to the new mangling for ObjC names.
310317
DeclContext *Ctx = Nominal->getDeclContext();
311318

312319
if (Ctx->isModuleScopeContext() && !isPrivate(Nominal)) {
@@ -338,6 +345,34 @@ std::string ASTMangler::mangleObjCRuntimeName(const NominalTypeDecl *Nominal) {
338345
beginMangling();
339346
appendAnyGenericType(Nominal);
340347
return finalize();
348+
#else
349+
// Use the old mangling for ObjC runtime names.
350+
beginMangling();
351+
appendAnyGenericType(Nominal);
352+
std::string NewName = finalize();
353+
Demangle::Demangler Dem;
354+
Demangle::Node *Root = Dem.demangleSymbol(NewName);
355+
assert(Root->getKind() == Node::Kind::Global);
356+
Node *NomTy = Root->getFirstChild();
357+
if (NomTy->getKind() == Node::Kind::Protocol) {
358+
// Protocols are actually mangled as protocol lists.
359+
Node *PTy = Dem.createNode(Node::Kind::Type);
360+
PTy->addChild(NomTy, Dem);
361+
Node *TList = Dem.createNode(Node::Kind::TypeList);
362+
TList->addChild(PTy, Dem);
363+
NomTy = Dem.createNode(Node::Kind::ProtocolList);
364+
NomTy->addChild(TList, Dem);
365+
}
366+
// Add a TypeMangling node at the top
367+
Node *Ty = Dem.createNode(Node::Kind::Type);
368+
Ty->addChild(NomTy, Dem);
369+
Node *TyMangling = Dem.createNode(Node::Kind::TypeMangling);
370+
TyMangling->addChild(Ty, Dem);
371+
Node *NewGlobal = Dem.createNode(Node::Kind::Global);
372+
NewGlobal->addChild(TyMangling, Dem);
373+
std::string OldName = mangleNodeOld(NewGlobal);
374+
return OldName;
375+
#endif
341376
}
342377

343378
std::string ASTMangler::mangleTypeAsContextUSR(const NominalTypeDecl *type) {

lib/Demangling/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ add_swift_library(swiftDemangling STATIC
55
NodeDumper.cpp
66
NodePrinter.cpp
77
OldDemangler.cpp
8+
OldRemangler.cpp
89
Punycode.cpp
910
Remangler.cpp
1011
)

0 commit comments

Comments
 (0)