Skip to content

Revert "Use the new mangling for reflection." #7337

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 2 commits into from
Feb 8, 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
8 changes: 4 additions & 4 deletions include/swift/Basic/Demangle.h
Original file line number Diff line number Diff line change
Expand Up @@ -323,14 +323,14 @@ void mangleIdentifier(const char *data, size_t length,
/// \brief Remangle a demangled parse tree.
///
/// This should always round-trip perfectly with demangleSymbolAsNode.
std::string mangleNodeOld(const NodePointer &root);

std::string mangleNode(const NodePointer &root);

std::string mangleNodeNew(const NodePointer &root);

inline std::string mangleNode(const NodePointer &root, bool NewMangling) {
if (NewMangling)
return mangleNode(root);
return mangleNodeOld(root);
return mangleNodeNew(root);
return mangleNode(root);
}

/// \brief Transform the node structure to a string.
Expand Down
4 changes: 2 additions & 2 deletions include/swift/Reflection/TypeRef.h
Original file line number Diff line number Diff line change
Expand Up @@ -397,11 +397,11 @@ class ProtocolTypeRef final : public TypeRef {
}

bool isAnyObject() const {
return MangledName == "s9AnyObject_p";
return MangledName == "Ps9AnyObject_";
}

bool isError() const {
return MangledName == "s5Error_p";
return MangledName == "Ps5Error_";
}

const std::string &getMangledName() const {
Expand Down
12 changes: 10 additions & 2 deletions lib/Basic/Demangle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@

#include "swift/Basic/Demangle.h"
#include "swift/Basic/Fallthrough.h"
#ifndef NO_NEW_DEMANGLING
#include "swift/Basic/Demangler.h"
#include "swift/Basic/ManglingMacros.h"
#endif
#include "swift/Strings.h"
#include "swift/Basic/LLVM.h"
#include "swift/Basic/Punycode.h"
Expand Down Expand Up @@ -323,10 +325,12 @@ class Demangler {
///
/// \return true if the mangling succeeded
NodePointer demangleTopLevel() {
#ifndef NO_NEW_DEMANGLING
if (Mangled.str().startswith(MANGLING_PREFIX_STR)) {
NewMangling::Demangler D(Mangled.str());
return D.demangleTopLevel();
}
#endif
if (!Mangled.nextIf("_T"))
return nullptr;

Expand Down Expand Up @@ -2340,16 +2344,19 @@ swift::Demangle::isSwiftSymbol(const char *mangledName,
if (Name.startswith("_T"))
return true;

#ifndef NO_NEW_DEMANGLING
// The new mangling.
if (Name.startswith(MANGLING_PREFIX_STR))
return true;
#endif // !NO_NEW_DEMANGLING
return false;
}

bool
swift::Demangle::isThunkSymbol(const char *mangledName,
size_t mangledNameLength) {
StringRef Name(mangledName, mangledNameLength);
#ifndef NO_NEW_DEMANGLING
if (Name.startswith(MANGLING_PREFIX_STR)) {
// First do a quick check
if (Name.endswith("TA") || // partial application forwarder
Expand All @@ -2375,6 +2382,7 @@ swift::Demangle::isThunkSymbol(const char *mangledName,
}
return false;
}
#endif // !NO_NEW_DEMANGLING

if (Name.startswith("_T")) {
// Old mangling.
Expand All @@ -2400,8 +2408,8 @@ NodePointer
swift::Demangle::demangleTypeAsNode(const char *MangledName,
size_t MangledNameLength,
const DemangleOptions &Options) {
NewMangling::Demangler D(StringRef(MangledName, MangledNameLength));
return D.demangleType();
Demangler demangler(StringRef(MangledName, MangledNameLength));
return demangler.demangleTypeName();
}

namespace {
Expand Down
2 changes: 1 addition & 1 deletion lib/Basic/Mangler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ std::string NewMangling::selectMangling(const std::string &Old,
assert(false);
}
if (StringRef(New).startswith(MANGLING_PREFIX_STR)) {
std::string Remangled = mangleNode(NewNode);
std::string Remangled = mangleNodeNew(NewNode);
if (New != Remangled) {
bool isEqual = false;
if (treeContains(NewNode,
Expand Down
117 changes: 116 additions & 1 deletion lib/Basic/Remangle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,67 @@ void Remangler::resetSubstitutions() {
Substitutions.clear();
}

static bool isInSwiftModule(Node *node) {
auto context = node->begin()->get();
return (context->getKind() == Node::Kind::Module &&
context->getText() == STDLIB_NAME);
};

bool Demangle::mangleStandardSubstitution(Node *node, DemanglerPrinter &Out) {
// Look for known substitutions.
switch (node->getKind()) {
#define SUCCESS_IF_IS(VALUE, EXPECTED, SUBSTITUTION) \
do { \
if ((VALUE) == (EXPECTED)) { \
Out << SUBSTITUTION; \
return true; \
} \
} while (0)
#define SUCCESS_IF_TEXT_IS(EXPECTED, SUBSTITUTION) \
SUCCESS_IF_IS(node->getText(), EXPECTED, SUBSTITUTION)
#define SUCCESS_IF_DECLNAME_IS(EXPECTED, SUBSTITUTION) \
SUCCESS_IF_IS(node->getChild(1)->getText(), EXPECTED, SUBSTITUTION)

case Node::Kind::Module:
SUCCESS_IF_TEXT_IS(STDLIB_NAME, "s");
SUCCESS_IF_TEXT_IS(MANGLING_MODULE_OBJC, "So");
SUCCESS_IF_TEXT_IS(MANGLING_MODULE_C, "SC");
break;
case Node::Kind::Structure:
if (isInSwiftModule(node)) {
SUCCESS_IF_DECLNAME_IS("Array", "Sa");
SUCCESS_IF_DECLNAME_IS("Bool", "Sb");
SUCCESS_IF_DECLNAME_IS("UnicodeScalar", "Sc");
SUCCESS_IF_DECLNAME_IS("Double", "Sd");
SUCCESS_IF_DECLNAME_IS("Float", "Sf");
SUCCESS_IF_DECLNAME_IS("Int", "Si");
SUCCESS_IF_DECLNAME_IS("UnsafeRawPointer", "SV");
SUCCESS_IF_DECLNAME_IS("UnsafeMutableRawPointer", "Sv");
SUCCESS_IF_DECLNAME_IS("UnsafePointer", "SP");
SUCCESS_IF_DECLNAME_IS("UnsafeMutablePointer", "Sp");
SUCCESS_IF_DECLNAME_IS("UnsafeBufferPointer", "SR");
SUCCESS_IF_DECLNAME_IS("UnsafeMutableBufferPointer", "Sr");
SUCCESS_IF_DECLNAME_IS("String", "SS");
SUCCESS_IF_DECLNAME_IS("UInt", "Su");
}
break;
case Node::Kind::Enum:
if (isInSwiftModule(node)) {
SUCCESS_IF_DECLNAME_IS("Optional", "Sq");
SUCCESS_IF_DECLNAME_IS("ImplicitlyUnwrappedOptional", "SQ");
}
break;

default:
break;

#undef SUCCESS_IF_DECLNAME_IS
#undef SUCCESS_IF_TEXT_IS
#undef SUCCESS_IF_IS
}
return false;
}

bool Remangler::trySubstitution(Node *node, SubstitutionEntry &entry) {
if (Demangle::mangleStandardSubstitution(node, Out))
return true;
Expand Down Expand Up @@ -1457,6 +1518,60 @@ void Remangler::mangleProtocolWithoutPrefix(Node *node) {
mangleNominalType(node, '\0', ctx);
}

bool Demangle::isSpecialized(Node *node) {
switch (node->getKind()) {
case Node::Kind::BoundGenericStructure:
case Node::Kind::BoundGenericEnum:
case Node::Kind::BoundGenericClass:
return true;

case Node::Kind::Structure:
case Node::Kind::Enum:
case Node::Kind::Class: {
Node *parentOrModule = node->getChild(0).get();
if (isSpecialized(parentOrModule))
return true;

return false;
}

default:
return false;
}
}

NodePointer Demangle::getUnspecialized(Node *node) {
switch (node->getKind()) {
case Node::Kind::Structure:
case Node::Kind::Enum:
case Node::Kind::Class: {
NodePointer result = NodeFactory::create(node->getKind());
NodePointer parentOrModule = node->getChild(0);
if (isSpecialized(parentOrModule.get()))
result->addChild(getUnspecialized(parentOrModule.get()));
else
result->addChild(parentOrModule);
result->addChild(node->getChild(1));
return result;
}

case Node::Kind::BoundGenericStructure:
case Node::Kind::BoundGenericEnum:
case Node::Kind::BoundGenericClass: {
NodePointer unboundType = node->getChild(0);
assert(unboundType->getKind() == Node::Kind::Type);
NodePointer nominalType = unboundType->getChild(0);
if (isSpecialized(nominalType.get()))
return getUnspecialized(nominalType.get());
else
return nominalType;
}

default:
unreachable("bad nominal type kind");
}
}

void Remangler::mangleGenericArgs(Node *node, EntityContext &ctx) {
switch (node->getKind()) {
case Node::Kind::Structure:
Expand Down Expand Up @@ -1654,7 +1769,7 @@ void Remangler::mangleSILBoxImmutableField(Node *node) {
}

/// The top-level interface to the remangler.
std::string Demangle::mangleNodeOld(const NodePointer &node) {
std::string Demangle::mangleNode(const NodePointer &node) {
if (!node) return "";

DemanglerPrinter printer;
Expand Down
117 changes: 1 addition & 116 deletions lib/Basic/Remangler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1687,126 +1687,11 @@ void Remangler::mangleSILBoxImmutableField(Node *node) {
} // anonymous namespace

/// The top-level interface to the remangler.
std::string Demangle::mangleNode(const NodePointer &node) {
std::string Demangle::mangleNodeNew(const NodePointer &node) {
if (!node) return "";

DemanglerPrinter printer;
Remangler(printer).mangle(node.get());

return std::move(printer).str();
}

static bool isInSwiftModule(Node *node) {
auto context = node->begin()->get();
return (context->getKind() == Node::Kind::Module &&
context->getText() == STDLIB_NAME);
};

bool Demangle::mangleStandardSubstitution(Node *node, DemanglerPrinter &Out) {
// Look for known substitutions.
switch (node->getKind()) {
#define SUCCESS_IF_IS(VALUE, EXPECTED, SUBSTITUTION) \
do { \
if ((VALUE) == (EXPECTED)) { \
Out << SUBSTITUTION; \
return true; \
} \
} while (0)
#define SUCCESS_IF_TEXT_IS(EXPECTED, SUBSTITUTION) \
SUCCESS_IF_IS(node->getText(), EXPECTED, SUBSTITUTION)
#define SUCCESS_IF_DECLNAME_IS(EXPECTED, SUBSTITUTION) \
SUCCESS_IF_IS(node->getChild(1)->getText(), EXPECTED, SUBSTITUTION)

case Node::Kind::Module:
SUCCESS_IF_TEXT_IS(STDLIB_NAME, "s");
SUCCESS_IF_TEXT_IS(MANGLING_MODULE_OBJC, "So");
SUCCESS_IF_TEXT_IS(MANGLING_MODULE_C, "SC");
break;
case Node::Kind::Structure:
if (isInSwiftModule(node)) {
SUCCESS_IF_DECLNAME_IS("Array", "Sa");
SUCCESS_IF_DECLNAME_IS("Bool", "Sb");
SUCCESS_IF_DECLNAME_IS("UnicodeScalar", "Sc");
SUCCESS_IF_DECLNAME_IS("Double", "Sd");
SUCCESS_IF_DECLNAME_IS("Float", "Sf");
SUCCESS_IF_DECLNAME_IS("Int", "Si");
SUCCESS_IF_DECLNAME_IS("UnsafeRawPointer", "SV");
SUCCESS_IF_DECLNAME_IS("UnsafeMutableRawPointer", "Sv");
SUCCESS_IF_DECLNAME_IS("UnsafePointer", "SP");
SUCCESS_IF_DECLNAME_IS("UnsafeMutablePointer", "Sp");
SUCCESS_IF_DECLNAME_IS("UnsafeBufferPointer", "SR");
SUCCESS_IF_DECLNAME_IS("UnsafeMutableBufferPointer", "Sr");
SUCCESS_IF_DECLNAME_IS("String", "SS");
SUCCESS_IF_DECLNAME_IS("UInt", "Su");
}
break;
case Node::Kind::Enum:
if (isInSwiftModule(node)) {
SUCCESS_IF_DECLNAME_IS("Optional", "Sq");
SUCCESS_IF_DECLNAME_IS("ImplicitlyUnwrappedOptional", "SQ");
}
break;

default:
break;

#undef SUCCESS_IF_DECLNAME_IS
#undef SUCCESS_IF_TEXT_IS
#undef SUCCESS_IF_IS
}
return false;
}

bool Demangle::isSpecialized(Node *node) {
switch (node->getKind()) {
case Node::Kind::BoundGenericStructure:
case Node::Kind::BoundGenericEnum:
case Node::Kind::BoundGenericClass:
return true;

case Node::Kind::Structure:
case Node::Kind::Enum:
case Node::Kind::Class: {
Node *parentOrModule = node->getChild(0).get();
if (isSpecialized(parentOrModule))
return true;

return false;
}

default:
return false;
}
}

NodePointer Demangle::getUnspecialized(Node *node) {
switch (node->getKind()) {
case Node::Kind::Structure:
case Node::Kind::Enum:
case Node::Kind::Class: {
NodePointer result = NodeFactory::create(node->getKind());
NodePointer parentOrModule = node->getChild(0);
if (isSpecialized(parentOrModule.get()))
result->addChild(getUnspecialized(parentOrModule.get()));
else
result->addChild(parentOrModule);
result->addChild(node->getChild(1));
return result;
}

case Node::Kind::BoundGenericStructure:
case Node::Kind::BoundGenericEnum:
case Node::Kind::BoundGenericClass: {
NodePointer unboundType = node->getChild(0);
assert(unboundType->getKind() == Node::Kind::Type);
NodePointer nominalType = unboundType->getChild(0);
if (isSpecialized(nominalType.get()))
return getUnspecialized(nominalType.get());
else
return nominalType;
}

default:
unreachable("bad nominal type kind");
}
}
8 changes: 4 additions & 4 deletions lib/IRGen/GenMeta.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@
#include "StructMetadataLayout.h"
#include "StructLayout.h"
#include "EnumMetadataLayout.h"
#include "IRGenMangler.h"

#include "GenMeta.h"

Expand Down Expand Up @@ -123,9 +122,10 @@ static Address createPointerSizedGEP(IRGenFunction &IGF,
// It should be removed when fixed. rdar://problem/22674524
static llvm::Constant *getMangledTypeName(IRGenModule &IGM, CanType type,
bool willBeRelativelyAddressed = false) {
IRGenMangler Mangler;
std::string Name = Mangler.mangleTypeForMetadata(type);
return IGM.getAddrOfGlobalString(Name, willBeRelativelyAddressed);
auto name = LinkEntity::forTypeMangling(type);
llvm::SmallString<32> mangling;
name.mangle(mangling);
return IGM.getAddrOfGlobalString(mangling, willBeRelativelyAddressed);
}

llvm::Value *irgen::emitObjCMetadataRefForMetadata(IRGenFunction &IGF,
Expand Down
Loading