Skip to content

Switch back to the old mangling for ObjC runtime names. #9228

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 3, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions include/swift/Demangling/Demangle.h
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,12 @@ void mangleIdentifier(const char *data, size_t length,
/// This should always round-trip perfectly with demangleSymbolAsNode.
std::string mangleNode(const NodePointer &root);

/// Remangle in the old mangling scheme.
///
/// This is only used for objc-runtime names and should be removed as soon as
/// we switch to the new mangling for those names as well.
std::string mangleNodeOld(const NodePointer &root);

/// \brief Transform the node structure to a string.
///
/// Typical usage:
Expand Down
35 changes: 35 additions & 0 deletions lib/AST/ASTMangler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "swift/AST/ParameterList.h"
#include "swift/AST/ProtocolConformance.h"
#include "swift/Demangling/ManglingUtils.h"
#include "swift/Demangling/Demangler.h"
#include "swift/Strings.h"
#include "clang/Basic/CharInfo.h"
#include "clang/AST/Attr.h"
Expand Down Expand Up @@ -301,12 +302,18 @@ std::string ASTMangler::mangleDeclType(const ValueDecl *decl) {
return finalize();
}

#ifdef USE_NEW_MANGLING_FOR_OBJC_RUNTIME_NAMES
static bool isPrivate(const NominalTypeDecl *Nominal) {
return Nominal->hasAccessibility() &&
Nominal->getFormalAccess() <= Accessibility::FilePrivate;
}
#endif

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

if (Ctx->isModuleScopeContext() && !isPrivate(Nominal)) {
Expand Down Expand Up @@ -338,6 +345,34 @@ std::string ASTMangler::mangleObjCRuntimeName(const NominalTypeDecl *Nominal) {
beginMangling();
appendAnyGenericType(Nominal);
return finalize();
#else
// Use the old mangling for ObjC runtime names.
beginMangling();
appendAnyGenericType(Nominal);
std::string NewName = finalize();
Demangle::Demangler Dem;
Demangle::Node *Root = Dem.demangleSymbol(NewName);
assert(Root->getKind() == Node::Kind::Global);
Node *NomTy = Root->getFirstChild();
if (NomTy->getKind() == Node::Kind::Protocol) {
// Protocols are actually mangled as protocol lists.
Node *PTy = Dem.createNode(Node::Kind::Type);
PTy->addChild(NomTy, Dem);
Node *TList = Dem.createNode(Node::Kind::TypeList);
TList->addChild(PTy, Dem);
NomTy = Dem.createNode(Node::Kind::ProtocolList);
NomTy->addChild(TList, Dem);
}
// Add a TypeMangling node at the top
Node *Ty = Dem.createNode(Node::Kind::Type);
Ty->addChild(NomTy, Dem);
Node *TyMangling = Dem.createNode(Node::Kind::TypeMangling);
TyMangling->addChild(Ty, Dem);
Node *NewGlobal = Dem.createNode(Node::Kind::Global);
NewGlobal->addChild(TyMangling, Dem);
std::string OldName = mangleNodeOld(NewGlobal);
return OldName;
#endif
}

std::string ASTMangler::mangleTypeAsContextUSR(const NominalTypeDecl *type) {
Expand Down
1 change: 1 addition & 0 deletions lib/Demangling/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ add_swift_library(swiftDemangling STATIC
NodeDumper.cpp
NodePrinter.cpp
OldDemangler.cpp
OldRemangler.cpp
Punycode.cpp
Remangler.cpp
)
Expand Down
Loading