Skip to content

demangling: Drop the support of "unmangled suffix" #9918

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 25, 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
3 changes: 2 additions & 1 deletion docs/ABI.rst
Original file line number Diff line number Diff line change
Expand Up @@ -826,7 +826,8 @@ types where the metadata itself has unknown layout.)
global ::= protocol-conformance entity 'TW' // protocol witness thunk
global ::= context identifier identifier 'TB' // property behavior initializer thunk (not used currently)
global ::= context identifier identifier 'Tb' // property behavior setter thunk (not used currently)
global ::= global 'T_' specialization // reset substitutions before demangling specialization
global ::= global specialization // function specialization
global ::= global 'Tm' // merged function
global ::= entity // some identifiable thing
global ::= type type generic-signature? 'T' REABSTRACT-THUNK-TYPE // reabstraction thunk helper function

Expand Down
5 changes: 4 additions & 1 deletion include/swift/Basic/Mangler.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,10 @@ class Mangler {

Mangler() : Buffer(Storage) { }

/// Adds the mangling prefix.
/// Begins a new mangling but does not add the mangling prefix.
void beginManglingWithoutPrefix();

/// Begins a new mangling but and adds the mangling prefix.
void beginMangling();

/// Finish the mangling of the symbol and return the mangled name.
Expand Down
1 change: 1 addition & 0 deletions include/swift/Demangling/DemangleNodes.def
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ NODE(LazyProtocolWitnessTableAccessor)
NODE(LazyProtocolWitnessTableCacheVariable)
NODE(LocalDeclName)
CONTEXT_NODE(MaterializeForSet)
NODE(MergedFunction)
NODE(Metatype)
NODE(MetatypeRepresentation)
NODE(Metaclass)
Expand Down
19 changes: 7 additions & 12 deletions include/swift/Demangling/Demangler.h
Original file line number Diff line number Diff line change
Expand Up @@ -286,16 +286,11 @@ class CharVector : public Vector<char> {
/// It de-mangles a string and it also owns the returned node-tree. This means
/// The nodes of the tree only live as long as the Demangler itself.
class Demangler : public NodeFactory {
private:
protected:
StringRef Text;
size_t Pos = 0;

struct NodeWithPos {
NodePointer Node;
size_t Pos;
};

Vector<NodeWithPos> NodeStack;
Vector<NodePointer> NodeStack;
Vector<NodePointer> Substitutions;
Vector<unsigned> PendingSubstitutions;

Expand Down Expand Up @@ -334,18 +329,18 @@ class Demangler : public NodeFactory {
}

void pushNode(NodePointer Nd) {
NodeStack.push_back({ Nd, Pos }, *this);
NodeStack.push_back(Nd, *this);
}

NodePointer popNode() {
return NodeStack.pop_back_val().Node;
return NodeStack.pop_back_val();
}

NodePointer popNode(Node::Kind kind) {
if (NodeStack.empty())
return nullptr;

Node::Kind NdKind = NodeStack.back().Node->getKind();
Node::Kind NdKind = NodeStack.back()->getKind();
if (NdKind != kind)
return nullptr;

Expand All @@ -356,7 +351,7 @@ class Demangler : public NodeFactory {
if (NodeStack.empty())
return nullptr;

Node::Kind NdKind = NodeStack.back().Node->getKind();
Node::Kind NdKind = NodeStack.back()->getKind();
if (!pred(NdKind))
return nullptr;

Expand All @@ -381,7 +376,7 @@ class Demangler : public NodeFactory {
return createWithChild(kind, popNode(Node::Kind::Type));
}

void parseAndPushNodes();
bool parseAndPushNodes();

NodePointer changeKind(NodePointer Node, Node::Kind NewKind);

Expand Down
3 changes: 3 additions & 0 deletions lib/AST/ASTMangler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -386,13 +386,15 @@ std::string ASTMangler::mangleObjCRuntimeName(const NominalTypeDecl *Nominal) {
}

std::string ASTMangler::mangleTypeAsContextUSR(const NominalTypeDecl *type) {
beginManglingWithoutPrefix();
llvm::SaveAndRestore<bool> allowUnnamedRAII(AllowNamelessEntities, true);
appendContext(type);
return finalize();
}

std::string ASTMangler::mangleDeclAsUSR(const ValueDecl *Decl,
StringRef USRPrefix) {
beginManglingWithoutPrefix();
llvm::SaveAndRestore<bool> allowUnnamedRAII(AllowNamelessEntities, true);
Buffer << USRPrefix;
bindGenericParameters(Decl->getDeclContext());
Expand All @@ -419,6 +421,7 @@ std::string ASTMangler::mangleAccessorEntityAsUSR(AccessorKind kind,
AddressorKind addressorKind,
const ValueDecl *decl,
StringRef USRPrefix) {
beginManglingWithoutPrefix();
llvm::SaveAndRestore<bool> allowUnnamedRAII(AllowNamelessEntities, true);
Buffer << USRPrefix;
appendAccessorEntity(kind, addressorKind, decl, /*isStatic*/ false);
Expand Down
6 changes: 5 additions & 1 deletion lib/Basic/Mangler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,16 @@ void Mangle::printManglingStats() {
#endif
}

void Mangler::beginMangling() {
void Mangler::beginManglingWithoutPrefix() {
Storage.clear();
Substitutions.clear();
StringSubstitutions.clear();
Words.clear();
SubstMerging.clear();
}

void Mangler::beginMangling() {
beginManglingWithoutPrefix();
Buffer << MANGLING_PREFIX_STR;
}

Expand Down
26 changes: 10 additions & 16 deletions lib/Demangling/Demangler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ static bool isFunctionAttr(Node::Kind kind) {
case Node::Kind::VTableAttribute:
case Node::Kind::PartialApplyForwarder:
case Node::Kind::PartialApplyObjCForwarder:
case Node::Kind::MergedFunction:
return true;
default:
return false;
Expand Down Expand Up @@ -233,14 +234,10 @@ NodePointer Demangler::demangleSymbol(StringRef MangledName) {

// If any other prefixes are accepted, please update Mangler::verify.

NodePointer topLevel = createNode(Node::Kind::Global);

parseAndPushNodes();

// Let a trailing '_' be part of the not demangled suffix.
popNode(Node::Kind::FirstElementMarker);
if (!parseAndPushNodes())
return nullptr;

size_t EndPos = (NodeStack.empty() ? 0 : NodeStack.back().Pos);
NodePointer topLevel = createNode(Node::Kind::Global);

NodePointer Parent = topLevel;
while (NodePointer FuncAttr = popNode(isFunctionAttr)) {
Expand All @@ -249,8 +246,7 @@ NodePointer Demangler::demangleSymbol(StringRef MangledName) {
FuncAttr->getKind() == Node::Kind::PartialApplyObjCForwarder)
Parent = FuncAttr;
}
for (const NodeWithPos &NWP : NodeStack) {
NodePointer Nd = NWP.Node;
for (Node *Nd : NodeStack) {
switch (Nd->getKind()) {
case Node::Kind::Type:
Parent->addChild(Nd->getFirstChild(), *this);
Expand All @@ -263,10 +259,6 @@ NodePointer Demangler::demangleSymbol(StringRef MangledName) {
if (topLevel->getNumChildren() == 0)
return nullptr;

if (EndPos < Text.size()) {
topLevel->addChild(createNode(Node::Kind::Suffix, Text.substr(EndPos)), *this);
}

return topLevel;
}

Expand All @@ -281,15 +273,16 @@ NodePointer Demangler::demangleType(StringRef MangledName) {
return createNode(Node::Kind::Suffix, Text);
}

void Demangler::parseAndPushNodes() {
bool Demangler::parseAndPushNodes() {
int Idx = 0;
while (!Text.empty()) {
while (Pos < Text.size()) {
NodePointer Node = demangleOperator();
if (!Node)
break;
return false;
pushNode(Node);
Idx++;
}
return true;
}

NodePointer Demangler::addChild(NodePointer Parent, NodePointer Child) {
Expand Down Expand Up @@ -1219,6 +1212,7 @@ NodePointer Demangler::demangleThunkOrSpecialization() {
case 'd': return createNode(Node::Kind::DirectMethodReferenceAttribute);
case 'a': return createNode(Node::Kind::PartialApplyObjCForwarder);
case 'A': return createNode(Node::Kind::PartialApplyForwarder);
case 'm': return createNode(Node::Kind::MergedFunction);
case 'V': {
NodePointer Base = popNode(isEntity);
NodePointer Derived = popNode(isEntity);
Expand Down
4 changes: 2 additions & 2 deletions lib/Demangling/NodeDumper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ void swift::Demangle::Node::dump() {

void Demangler::dump() {
for (unsigned Idx = 0; Idx < NodeStack.size(); ++Idx) {
fprintf(stderr, "NodeStack[%u] at position %zd:\n", Idx, NodeStack[Idx].Pos);
NodeStack[Idx].Node->dump();
fprintf(stderr, "NodeStack[%u]:\n", Idx);
NodeStack[Idx]->dump();
fprintf(stderr, "\n");
}
fprintf(stderr, "Position = %zd:\n%.*s\n%*s\n", Pos,
Expand Down
6 changes: 6 additions & 0 deletions lib/Demangling/NodePrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,7 @@ class NodePrinter {
case Node::Kind::LocalDeclName:
case Node::Kind::PrivateDeclName:
case Node::Kind::MaterializeForSet:
case Node::Kind::MergedFunction:
case Node::Kind::Metaclass:
case Node::Kind::NativeOwningAddressor:
case Node::Kind::NativeOwningMutableAddressor:
Expand Down Expand Up @@ -1238,6 +1239,11 @@ NodePointer NodePrinter::print(NodePointer Node, bool asPrefixContext) {
print(Node->getChild(Node->getNumChildren() - 1));
return nullptr;
}
case Node::Kind::MergedFunction:
if (!Options.ShortenThunk) {
Printer << "merged ";
}
return nullptr;
case Node::Kind::GenericTypeMetadataPattern:
Printer << "generic type metadata pattern for ";
print(Node->getChild(0));
Expand Down
4 changes: 4 additions & 0 deletions lib/Demangling/OldRemangler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -653,6 +653,10 @@ void Remangler::manglePartialApplyObjCForwarder(Node *node) {
mangleSingleChildNode(node); // global
}

void Remangler::mangleMergedFunction(Node *node) {
Out << "Tm";
}

void Remangler::mangleDirectness(Node *node) {
auto getChar = [](Directness d) -> char {
switch (d) {
Expand Down
5 changes: 5 additions & 0 deletions lib/Demangling/Remangler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1063,6 +1063,7 @@ void Remangler::mangleGlobal(Node *node) {
case Node::Kind::DynamicAttribute:
case Node::Kind::VTableAttribute:
case Node::Kind::DirectMethodReferenceAttribute:
case Node::Kind::MergedFunction:
mangleInReverseOrder = true;
break;
default:
Expand Down Expand Up @@ -1368,6 +1369,10 @@ void Remangler::manglePartialApplyObjCForwarder(Node *node) {
Buffer << "Ta";
}

void Remangler::mangleMergedFunction(Node *node) {
Buffer << "Tm";
}

void Remangler::manglePostfixOperator(Node *node) {
mangleIdentifierImpl(node, /*isOperator*/ true);
Buffer << "oP";
Expand Down
2 changes: 1 addition & 1 deletion lib/LLVMPasses/LLVMMergeFunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -861,7 +861,7 @@ void SwiftMergeFunctions::mergeWithParams(const FunctionInfos &FInfos,
// a name which can be demangled in a meaningful way.
Function *NewFunction = Function::Create(funcType,
FirstF->getLinkage(),
FirstF->getName() + "_merged");
FirstF->getName() + "Tm");
NewFunction->copyAttributesFrom(FirstF);
// NOTE: this function is not externally available, do ensure that we reset
// the DLL storage
Expand Down
29 changes: 24 additions & 5 deletions lib/SILOptimizer/Utils/SpecializationMangler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,37 @@ using namespace swift;
using namespace Mangle;

void SpecializationMangler::beginMangling() {
ASTMangler::beginMangling();
ASTMangler::beginManglingWithoutPrefix();
if (Serialized)
ArgOpBuffer << 'q';
ArgOpBuffer << char(uint8_t(Pass) + '0');
}

namespace {

/// Utility class for demangling specialization attributes.
class AttributeDemangler : public Demangle::Demangler {
public:
void demangleAndAddAsChildren(StringRef MangledSpecialization,
NodePointer Parent) {
init(MangledSpecialization);
if (!parseAndPushNodes()) {
llvm::errs() << "Can't demangle: " << MangledSpecialization << '\n';
abort();
}
for (Node *Nd : NodeStack) {
addChild(Parent, Nd);
}
}
};

} // namespace

std::string SpecializationMangler::finalize() {
StringRef MangledSpecialization(Storage.data(), Storage.size());
Demangle::Demangler D;
NodePointer TopLevel = D.demangleSymbol(MangledSpecialization);
AttributeDemangler D;
NodePointer TopLevel = D.createNode(Node::Kind::Global);
D.demangleAndAddAsChildren(MangledSpecialization, TopLevel);

StringRef FuncName = Function->getName();
NodePointer FuncTopLevel = nullptr;
Expand All @@ -41,8 +62,6 @@ std::string SpecializationMangler::finalize() {
FuncTopLevel->addChild(D.createNode(Node::Kind::Identifier, FuncName), D);
}
for (NodePointer FuncChild : *FuncTopLevel) {
assert(FuncChild->getKind() != Node::Kind::Suffix ||
FuncChild->getText() == "merged");
TopLevel->addChild(FuncChild, D);
}
std::string mangledName = Demangle::mangleNode(TopLevel);
Expand Down
1 change: 1 addition & 0 deletions test/Demangle/Inputs/manglings.txt
Original file line number Diff line number Diff line change
Expand Up @@ -258,4 +258,5 @@ _T0Rml ---> _T0Rml
_T0Tk ---> _T0Tk
_T0A8 ---> _T0A8
_T0s30ReversedRandomAccessCollectionVyxGTfq3nnpf_nTfq1cn_nTfq4x_n ---> _T0s30ReversedRandomAccessCollectionVyxGTfq3nnpf_nTfq1cn_nTfq4x_n
_T03abc6testitySiFTm ---> merged abc.testit(Swift.Int) -> ()

2 changes: 2 additions & 0 deletions test/Demangle/Inputs/simplified-manglings.txt
Original file line number Diff line number Diff line change
Expand Up @@ -203,3 +203,5 @@ _TFC4testP33_83378C430F65473055F1BD53F3ADCDB71C5doFoofT_T_ ---> C.doFoo()
_TTRXFo_oCSo13SKPhysicsBodydVSC7CGPointdVSC8CGVectordGSpV10ObjectiveC8ObjCBool___XFdCb_dS_dS0_dS1_dGSpS3____ ---> thunk for @callee_owned (@owned SKPhysicsBody, @unowned CGPoint, @unowned CGVector, @unowned UnsafeMutablePointer<ObjCBool>) -> ()
_T0So13SKPhysicsBodyCSC7CGPointVSC8CGVectorVSpy10ObjectiveC8ObjCBoolVGIxxyyy_AbdFSpyAIGIyByyyy_TR ---> thunk for @callee_owned (@owned SKPhysicsBody, @unowned CGPoint, @unowned CGVector, @unowned UnsafeMutablePointer<ObjCBool>) -> ()
_T04main1_yyF ---> _()
_T03abc6testitySiFTm ---> testit(_:)

Loading