Demangler: remove StringRef dependencies in the demangler interface and

PrettyStackTrace dependencies in the implementation


Swift SVN r20248
This commit is contained in:
Dmitri Hrybenko
2014-07-21 12:46:58 +00:00
parent 18c5b7325e
commit 26277fc41d
11 changed files with 343 additions and 224 deletions

View File

@@ -13,8 +13,8 @@
#ifndef SWIFT_BASIC_DEMANGLE_H #ifndef SWIFT_BASIC_DEMANGLE_H
#define SWIFT_BASIC_DEMANGLE_H #define SWIFT_BASIC_DEMANGLE_H
#include "llvm/ADT/StringRef.h"
#include <memory> #include <memory>
#include <string>
#include <vector> #include <vector>
namespace llvm { namespace llvm {
@@ -63,10 +63,6 @@ private:
Node(Kind k) Node(Kind k)
: NodeKind(k), NodePayloadKind(PayloadKind::None) { : NodeKind(k), NodePayloadKind(PayloadKind::None) {
} }
Node(Kind k, llvm::StringRef t)
: NodeKind(k), NodePayloadKind(PayloadKind::Text) {
new (&TextPayload) std::string(t);
}
Node(Kind k, std::string &&t) Node(Kind k, std::string &&t)
: NodeKind(k), NodePayloadKind(PayloadKind::Text) { : NodeKind(k), NodePayloadKind(PayloadKind::Text) {
new (&TextPayload) std::string(std::move(t)); new (&TextPayload) std::string(std::move(t));
@@ -77,24 +73,10 @@ private:
} }
Node(const Node &) = delete; Node(const Node &) = delete;
Node &operator=(const Node &) = delete; Node &operator=(const Node &) = delete;
public:
static NodePointer create(Kind k) {
return NodePointer(new Node(k));
}
static NodePointer create(Kind k, IndexType index) {
return NodePointer(new Node(k, index));
}
static NodePointer create(Kind k, llvm::StringRef text) {
return NodePointer(new Node(k, text));
}
static NodePointer create(Kind k, std::string &&text) {
return NodePointer(new Node(k, std::move(text)));
}
template <size_t N>
static NodePointer create(Kind k, const char (&text)[N]) {
return NodePointer(new Node(k, llvm::StringRef(text)));
}
friend struct NodeFactory;
public:
~Node(); ~Node();
Kind getKind() const { return NodeKind; } Kind getKind() const { return NodeKind; }
@@ -141,11 +123,8 @@ public:
addChild(std::move(child1)); addChild(std::move(child1));
addChild(std::move(child2)); addChild(std::move(child2));
} }
void dump() const;
void print(llvm::raw_ostream &out) const;
}; };
/// \brief Demangle the given string as a Swift symbol. /// \brief Demangle the given string as a Swift symbol.
/// ///
/// Typical usage: /// Typical usage:
@@ -154,14 +133,16 @@ public:
/// swift::Demangler::demangleSymbol("SomeSwiftMangledName") /// swift::Demangler::demangleSymbol("SomeSwiftMangledName")
/// \endcode /// \endcode
/// ///
/// \param mangled The mangled string. /// \param MangledName The mangled string.
/// \param options An object encapsulating options to use to perform this demangling. /// \param Options An object encapsulating options to use to perform this demangling.
/// ///
/// ///
/// \returns A parse tree for the demangled string - or a Failure node on /// \returns A parse tree for the demangled string - or a Failure node on
/// failure. /// failure.
/// ///
NodePointer demangleSymbolAsNode(llvm::StringRef mangled, const DemangleOptions& options = DemangleOptions()); NodePointer
demangleSymbolAsNode(const char *MangledName, size_t MangledNameLength,
const DemangleOptions &Options = DemangleOptions());
/// \brief Transform the node structure in a string. /// \brief Transform the node structure in a string.
/// ///
@@ -171,12 +152,13 @@ NodePointer demangleSymbolAsNode(llvm::StringRef mangled, const DemangleOptions&
/// swift::Demangler::nodeToString(aNode) /// swift::Demangler::nodeToString(aNode)
/// \endcode /// \endcode
/// ///
/// \param pointer A pointer to a parse tree generated by the demangler. /// \param Root A pointer to a parse tree generated by the demangler.
/// \param options An object encapsulating options to use to perform this demangling. /// \param Options An object encapsulating options to use to perform this demangling.
/// ///
/// \returns A string representing the demangled name. /// \returns A string representing the demangled name.
/// ///
std::string nodeToString(NodePointer pointer, const DemangleOptions& options = DemangleOptions()); std::string nodeToString(NodePointer Root,
const DemangleOptions &Options = DemangleOptions());
/// \brief Demangle the given string as a Swift symbol. /// \brief Demangle the given string as a Swift symbol.
/// ///
@@ -186,15 +168,18 @@ std::string nodeToString(NodePointer pointer, const DemangleOptions& options = D
/// swift::Demangler::demangleSymbol("SomeSwiftMangledName") /// swift::Demangler::demangleSymbol("SomeSwiftMangledName")
/// \endcode /// \endcode
/// ///
/// \param mangled The mangled string. /// \param MangledName The mangled string.
/// \param options An object encapsulating options to use to perform this demangling. /// \param Options An object encapsulating options to use to perform this demangling.
/// ///
/// ///
/// \returns A string representing the demangled name. /// \returns A string representing the demangled name.
/// ///
std::string demangleSymbolAsString(llvm::StringRef mangled, const DemangleOptions& options = DemangleOptions()); std::string
demangleSymbolAsString(const char *MangledName, size_t MangledNameLength,
const DemangleOptions &Options = DemangleOptions());
} // end namespace Demangle } // end namespace Demangle
} // end namespace swift } // end namespace swift
#endif #endif

View File

@@ -0,0 +1,57 @@
//===--- DemangleWrappers.h --------------------------------------*- C++ -*-==//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2015 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
///
/// \file
/// This file defines wrappers for the Swift demangler that use LLVM data
/// types.
///
//===----------------------------------------------------------------------===//
#ifndef SWIFT_BASIC_DEMANGLE_WRAPPERS_H
#define SWIFT_BASIC_DEMANGLE_WRAPPERS_H
#include "swift/Basic/Demangle.h"
#include "swift/Basic/LLVM.h"
#include "llvm/ADT/StringRef.h"
namespace swift {
namespace demangle_wrappers {
using swift::Demangle::Node;
using swift::Demangle::NodePointer;
using swift::Demangle::DemangleOptions;
class NodeDumper {
NodePointer Root;
public:
NodeDumper(NodePointer Root): Root(std::move(Root)) {}
void dump() const;
void print(llvm::raw_ostream &Out) const;
};
NodePointer
demangleSymbolAsNode(StringRef MangledName,
const DemangleOptions &Options = DemangleOptions());
std::string nodeToString(NodePointer Root,
const DemangleOptions &Options = DemangleOptions());
std::string
demangleSymbolAsString(StringRef MangledName,
const DemangleOptions &Options = DemangleOptions());
} // end namespace demangle_wrappers
} // end namespace swift
#endif // LLVM_SWIFT_BASIC_DEMANGLE_WRAPPERS_H

View File

@@ -3,6 +3,7 @@ set(UNICODE_TABLES UnicodeExtendedGraphemeClusters.cpp.gyb)
add_swift_library(swiftBasic add_swift_library(swiftBasic
Cache.cpp Cache.cpp
Demangle.cpp Demangle.cpp
DemangleWrappers.cpp
DiagnosticConsumer.cpp DiagnosticConsumer.cpp
DiverseStack.cpp DiverseStack.cpp
LangOptions.cpp LangOptions.cpp

View File

@@ -17,72 +17,47 @@
#include "swift/Basic/Demangle.h" #include "swift/Basic/Demangle.h"
#include "swift/Strings.h" #include "swift/Strings.h"
#include "swift/Basic/LLVM.h" #include "swift/Basic/LLVM.h"
#include "swift/Basic/PrettyStackTrace.h"
#include "swift/Basic/Punycode.h" #include "swift/Basic/Punycode.h"
#include "swift/Basic/QuotedString.h" #include "swift/Basic/QuotedString.h"
#include "llvm/ADT/SmallString.h" #include "llvm/ADT/SmallString.h"
#include "llvm/Support/ErrorHandling.h" #include "llvm/ADT/StringRef.h"
#include "llvm/Support/raw_ostream.h" #include "llvm/Support/raw_ostream.h"
#include <functional> #include <functional>
#include <tuple> #include <tuple>
#include <vector> #include <vector>
#include <cstdlib>
using namespace swift; using namespace swift;
using namespace Demangle; using namespace Demangle;
static StringRef getNodeKindString(swift::Demangle::Node::Kind k) { [[noreturn]]
switch(k) { static void unreachable(const char *Message) {
#define NODE(ID) case Node::Kind::ID: return #ID; printf("fatal error: %s\n", Message);
#include "swift/Basic/DemangleNodes.def" std::abort();
}
namespace swift {
namespace Demangle {
struct NodeFactory {
static NodePointer create(Node::Kind K) {
return NodePointer(new Node(K));
} }
llvm_unreachable("bad node kind"); static NodePointer create(Node::Kind K, Node::IndexType Index) {
} return NodePointer(new Node(K, Index));
static void printNode(llvm::raw_ostream &out, const Node *node,
unsigned depth) {
// Indent two spaces per depth.
out.indent(depth * 2);
out << "kind=" << getNodeKindString(node->getKind());
if (node->hasText()) {
out << ", text=\"" << node->getText() << '\"';
} }
if (node->hasIndex()) { static NodePointer create(Node::Kind K, llvm::StringRef Text) {
out << ", index=" << node->getIndex(); return NodePointer(new Node(K, Text));
} }
out << '\n'; static NodePointer create(Node::Kind K, std::string &&Text) {
for (auto &child : *node) { return NodePointer(new Node(K, std::move(Text)));
printNode(out, child.get(), depth + 1);
} }
} template <size_t N>
static NodePointer create(Node::Kind K, const char (&Text)[N]) {
void Node::dump() const { return NodePointer(new Node(K, llvm::StringRef(Text)));
print(llvm::errs()); }
} };
} // end namespace Demangle
void Node::print(llvm::raw_ostream &out) const { } // end namespace swift
printNode(out, this, 0);
}
namespace {
/// A pretty-stack-trace node for demangling trees.
class PrettyStackTraceNode : public llvm::PrettyStackTraceEntry {
const char *Action;
Node *TheNode;
public:
PrettyStackTraceNode(const char *action, Node *node)
: Action(action), TheNode(node) {}
void print(llvm::raw_ostream &out) const override {
out << "While " << Action << ' ';
if (!TheNode) {
out << "<<null demangling node>>\n";
} else {
out << "demangling tree:\n";
printNode(out, TheNode, 4);
}
}
};
}
Node::~Node() { Node::~Node() {
switch (NodePayloadKind) { switch (NodePayloadKind) {
@@ -90,7 +65,7 @@ Node::~Node() {
case PayloadKind::Index: return; case PayloadKind::Index: return;
case PayloadKind::Text: TextPayload.~basic_string(); return; case PayloadKind::Text: TextPayload.~basic_string(); return;
} }
llvm_unreachable("bad payload kind"); unreachable("bad payload kind");
} }
namespace { namespace {
@@ -295,7 +270,7 @@ public:
private: private:
Node *getRootNode() { Node *getRootNode() {
if (!RootNode) { if (!RootNode) {
RootNode = Node::create(Node::Kind::Global); RootNode = NodeFactory::create(Node::Kind::Global);
} }
return RootNode.get(); return RootNode.get();
} }
@@ -305,7 +280,7 @@ private:
} }
Node *appendNode(Node::Kind k, std::string &&t = "") { Node *appendNode(Node::Kind k, std::string &&t = "") {
return appendNode(Node::create(k, std::move(t))); return appendNode(NodeFactory::create(k, std::move(t)));
} }
/// Try to demangle a child node of the given kind. If that fails, /// Try to demangle a child node of the given kind. If that fails,
@@ -321,8 +296,8 @@ private:
#define DEMANGLE_CHILD_AS_NODE_OR_RETURN(PARENT, CHILD_KIND) do { \ #define DEMANGLE_CHILD_AS_NODE_OR_RETURN(PARENT, CHILD_KIND) do { \
auto _kind = demangle##CHILD_KIND(); \ auto _kind = demangle##CHILD_KIND(); \
if (_kind == CHILD_KIND::Unknown) return nullptr; \ if (_kind == CHILD_KIND::Unknown) return nullptr; \
(PARENT)->addChild(Node::create(Node::Kind::CHILD_KIND, \ (PARENT)->addChild(NodeFactory::create(Node::Kind::CHILD_KIND, \
toString(_kind))); \ toString(_kind))); \
} while (false) } while (false)
enum class IsProtocol { enum class IsProtocol {
@@ -344,13 +319,13 @@ private:
case Directness::Indirect: case Directness::Indirect:
return "indirect"; return "indirect";
case Directness::Unknown: case Directness::Unknown:
llvm_unreachable("shouldn't toString an unknown directness"); unreachable("shouldn't toString an unknown directness");
} }
llvm_unreachable("bad directness"); unreachable("bad directness");
} }
bool failure() { bool failure() {
RootNode = Node::create(Node::Kind::Failure); RootNode = NodeFactory::create(Node::Kind::Failure);
return false; return false;
} }
@@ -461,9 +436,9 @@ private:
case ValueWitnessKind::InplaceProjectEnumData: case ValueWitnessKind::InplaceProjectEnumData:
return "inplaceProjectEnumData"; return "inplaceProjectEnumData";
case ValueWitnessKind::Unknown: case ValueWitnessKind::Unknown:
llvm_unreachable("stringifying the unknown value witness kind?"); unreachable("stringifying the unknown value witness kind?");
} }
llvm_unreachable("bad value witness kind"); unreachable("bad value witness kind");
} }
ValueWitnessKind demangleValueWitnessKind() { ValueWitnessKind demangleValueWitnessKind() {
@@ -525,22 +500,24 @@ private:
// Type metadata. // Type metadata.
if (Mangled.nextIf('M')) { if (Mangled.nextIf('M')) {
if (Mangled.nextIf('P')) { if (Mangled.nextIf('P')) {
auto pattern = Node::create(Node::Kind::GenericTypeMetadataPattern); auto pattern =
NodeFactory::create(Node::Kind::GenericTypeMetadataPattern);
DEMANGLE_CHILD_AS_NODE_OR_RETURN(pattern, Directness); DEMANGLE_CHILD_AS_NODE_OR_RETURN(pattern, Directness);
DEMANGLE_CHILD_OR_RETURN(pattern, Type); DEMANGLE_CHILD_OR_RETURN(pattern, Type);
return pattern; return pattern;
} }
if (Mangled.nextIf('m')) { if (Mangled.nextIf('m')) {
auto metaclass = Node::create(Node::Kind::Metaclass); auto metaclass = NodeFactory::create(Node::Kind::Metaclass);
DEMANGLE_CHILD_OR_RETURN(metaclass, Type); DEMANGLE_CHILD_OR_RETURN(metaclass, Type);
return metaclass; return metaclass;
} }
if (Mangled.nextIf('n')) { if (Mangled.nextIf('n')) {
auto nominalType = Node::create(Node::Kind::NominalTypeDescriptor); auto nominalType =
NodeFactory::create(Node::Kind::NominalTypeDescriptor);
DEMANGLE_CHILD_OR_RETURN(nominalType, Type); DEMANGLE_CHILD_OR_RETURN(nominalType, Type);
return nominalType; return nominalType;
} }
auto metadata = Node::create(Node::Kind::TypeMetadata); auto metadata = NodeFactory::create(Node::Kind::TypeMetadata);
DEMANGLE_CHILD_AS_NODE_OR_RETURN(metadata, Directness); DEMANGLE_CHILD_AS_NODE_OR_RETURN(metadata, Directness);
DEMANGLE_CHILD_OR_RETURN(metadata, Type); DEMANGLE_CHILD_OR_RETURN(metadata, Type);
return metadata; return metadata;
@@ -552,7 +529,7 @@ private:
Node::Kind kind = Node::Kind::PartialApplyForwarder; Node::Kind kind = Node::Kind::PartialApplyForwarder;
if (Mangled.nextIf('o')) if (Mangled.nextIf('o'))
kind = Node::Kind::PartialApplyObjCForwarder; kind = Node::Kind::PartialApplyObjCForwarder;
auto forwarder = Node::create(kind); auto forwarder = NodeFactory::create(kind);
if (Mangled.nextIf("__T")) if (Mangled.nextIf("__T"))
DEMANGLE_CHILD_OR_RETURN(forwarder, Global); DEMANGLE_CHILD_OR_RETURN(forwarder, Global);
return forwarder; return forwarder;
@@ -568,7 +545,8 @@ private:
ValueWitnessKind w = demangleValueWitnessKind(); ValueWitnessKind w = demangleValueWitnessKind();
if (w == ValueWitnessKind::Unknown) if (w == ValueWitnessKind::Unknown)
return nullptr; return nullptr;
auto witness = Node::create(Node::Kind::ValueWitness, toString(w)); auto witness =
NodeFactory::create(Node::Kind::ValueWitness, toString(w));
DEMANGLE_CHILD_OR_RETURN(witness, Type); DEMANGLE_CHILD_OR_RETURN(witness, Type);
return witness; return witness;
} }
@@ -576,47 +554,49 @@ private:
// Offsets, value witness tables, and protocol witnesses. // Offsets, value witness tables, and protocol witnesses.
if (Mangled.nextIf('W')) { if (Mangled.nextIf('W')) {
if (Mangled.nextIf('V')) { if (Mangled.nextIf('V')) {
auto witnessTable = Node::create(Node::Kind::ValueWitnessTable); auto witnessTable = NodeFactory::create(Node::Kind::ValueWitnessTable);
DEMANGLE_CHILD_OR_RETURN(witnessTable, Type); DEMANGLE_CHILD_OR_RETURN(witnessTable, Type);
return witnessTable; return witnessTable;
} }
if (Mangled.nextIf('o')) { if (Mangled.nextIf('o')) {
auto witnessTableOffset = Node::create(Node::Kind::WitnessTableOffset); auto witnessTableOffset =
NodeFactory::create(Node::Kind::WitnessTableOffset);
DEMANGLE_CHILD_OR_RETURN(witnessTableOffset, Entity); DEMANGLE_CHILD_OR_RETURN(witnessTableOffset, Entity);
return witnessTableOffset; return witnessTableOffset;
} }
if (Mangled.nextIf('v')) { if (Mangled.nextIf('v')) {
auto fieldOffset = Node::create(Node::Kind::FieldOffset); auto fieldOffset = NodeFactory::create(Node::Kind::FieldOffset);
DEMANGLE_CHILD_AS_NODE_OR_RETURN(fieldOffset, Directness); DEMANGLE_CHILD_AS_NODE_OR_RETURN(fieldOffset, Directness);
DEMANGLE_CHILD_OR_RETURN(fieldOffset, Entity); DEMANGLE_CHILD_OR_RETURN(fieldOffset, Entity);
return fieldOffset; return fieldOffset;
} }
if (Mangled.nextIf('P')) { if (Mangled.nextIf('P')) {
auto witnessTable = Node::create(Node::Kind::ProtocolWitnessTable); auto witnessTable =
NodeFactory::create(Node::Kind::ProtocolWitnessTable);
DEMANGLE_CHILD_OR_RETURN(witnessTable, ProtocolConformance); DEMANGLE_CHILD_OR_RETURN(witnessTable, ProtocolConformance);
return witnessTable; return witnessTable;
} }
if (Mangled.nextIf('Z')) { if (Mangled.nextIf('Z')) {
auto accessor = auto accessor =
Node::create(Node::Kind::LazyProtocolWitnessTableAccessor); NodeFactory::create(Node::Kind::LazyProtocolWitnessTableAccessor);
DEMANGLE_CHILD_OR_RETURN(accessor, ProtocolConformance); DEMANGLE_CHILD_OR_RETURN(accessor, ProtocolConformance);
return accessor; return accessor;
} }
if (Mangled.nextIf('z')) { if (Mangled.nextIf('z')) {
auto tableTemplate = auto tableTemplate =
Node::create(Node::Kind::LazyProtocolWitnessTableTemplate); NodeFactory::create(Node::Kind::LazyProtocolWitnessTableTemplate);
DEMANGLE_CHILD_OR_RETURN(tableTemplate, ProtocolConformance); DEMANGLE_CHILD_OR_RETURN(tableTemplate, ProtocolConformance);
return tableTemplate; return tableTemplate;
} }
if (Mangled.nextIf('D')) { if (Mangled.nextIf('D')) {
auto tableGenerator = auto tableGenerator = NodeFactory::create(
Node::create(Node::Kind::DependentProtocolWitnessTableGenerator); Node::Kind::DependentProtocolWitnessTableGenerator);
DEMANGLE_CHILD_OR_RETURN(tableGenerator, ProtocolConformance); DEMANGLE_CHILD_OR_RETURN(tableGenerator, ProtocolConformance);
return tableGenerator; return tableGenerator;
} }
if (Mangled.nextIf('d')) { if (Mangled.nextIf('d')) {
auto tableTemplate = auto tableTemplate = NodeFactory::create(
Node::create(Node::Kind::DependentProtocolWitnessTableTemplate); Node::Kind::DependentProtocolWitnessTableTemplate);
DEMANGLE_CHILD_OR_RETURN(tableTemplate, ProtocolConformance); DEMANGLE_CHILD_OR_RETURN(tableTemplate, ProtocolConformance);
return tableTemplate; return tableTemplate;
} }
@@ -626,19 +606,19 @@ private:
// Other thunks. // Other thunks.
if (Mangled.nextIf('T')) { if (Mangled.nextIf('T')) {
if (Mangled.nextIf('R')) { if (Mangled.nextIf('R')) {
NodePointer thunk = Node::create(Node::Kind::ReabstractionThunkHelper); auto thunk = NodeFactory::create(Node::Kind::ReabstractionThunkHelper);
if (!demangleReabstractSignature(thunk)) if (!demangleReabstractSignature(thunk))
return nullptr; return nullptr;
return thunk; return thunk;
} }
if (Mangled.nextIf('r')) { if (Mangled.nextIf('r')) {
NodePointer thunk = Node::create(Node::Kind::ReabstractionThunk); auto thunk = NodeFactory::create(Node::Kind::ReabstractionThunk);
if (!demangleReabstractSignature(thunk)) if (!demangleReabstractSignature(thunk))
return nullptr; return nullptr;
return thunk; return thunk;
} }
if (Mangled.nextIf('W')) { if (Mangled.nextIf('W')) {
NodePointer thunk = Node::create(Node::Kind::ProtocolWitness); NodePointer thunk = NodeFactory::create(Node::Kind::ProtocolWitness);
DEMANGLE_CHILD_OR_RETURN(thunk, ProtocolConformance); DEMANGLE_CHILD_OR_RETURN(thunk, ProtocolConformance);
DEMANGLE_CHILD_OR_RETURN(thunk, Entity); DEMANGLE_CHILD_OR_RETURN(thunk, Entity);
return thunk; return thunk;
@@ -651,9 +631,9 @@ private:
} }
NodePointer demangleSpecializedAttribute() { NodePointer demangleSpecializedAttribute() {
NodePointer specialization = Node::create(Node::Kind::SpecializedAttribute); auto specialization = NodeFactory::create(Node::Kind::SpecializedAttribute);
while (!Mangled.nextIf('_')) { while (!Mangled.nextIf('_')) {
NodePointer param = Node::create(Node::Kind::SpecializationParam); NodePointer param = NodeFactory::create(Node::Kind::SpecializationParam);
NodePointer type = demangleType(); NodePointer type = demangleType();
if (!type) if (!type)
return nullptr; return nullptr;
@@ -679,7 +659,7 @@ private:
NodePointer name = demangleIdentifier(); NodePointer name = demangleIdentifier();
if (!name) return nullptr; if (!name) return nullptr;
NodePointer localName = Node::create(Node::Kind::LocalDeclName); NodePointer localName = NodeFactory::create(Node::Kind::LocalDeclName);
localName->addChild(std::move(discriminator)); localName->addChild(std::move(discriminator));
localName->addChild(std::move(name)); localName->addChild(std::move(name));
return localName; return localName;
@@ -764,7 +744,7 @@ private:
identifier = opDecodeBuffer; identifier = opDecodeBuffer;
} }
return Node::create(kind, identifier); return NodeFactory::create(kind, identifier);
} }
bool demangleIndex(Node::IndexType &natural) { bool demangleIndex(Node::IndexType &natural) {
@@ -786,26 +766,26 @@ private:
Node::IndexType index; Node::IndexType index;
if (!demangleIndex(index)) if (!demangleIndex(index))
return nullptr; return nullptr;
return Node::create(kind, index); return NodeFactory::create(kind, index);
} }
NodePointer createSwiftType(Node::Kind typeKind, StringRef name) { NodePointer createSwiftType(Node::Kind typeKind, StringRef name) {
NodePointer type = Node::create(typeKind); NodePointer type = NodeFactory::create(typeKind);
type->addChild(Node::create(Node::Kind::Module, STDLIB_NAME)); type->addChild(NodeFactory::create(Node::Kind::Module, STDLIB_NAME));
type->addChild(Node::create(Node::Kind::Identifier, name)); type->addChild(NodeFactory::create(Node::Kind::Identifier, name));
return type; return type;
} }
/// Demangle a <substitution>, given that we've already consumed the 'S'. /// Demangle a <substitution>, given that we've already consumed the 'S'.
NodePointer demangleSubstitutionIndex() { NodePointer demangleSubstitutionIndex() {
if (!Mangled) if (!Mangled)
return Node::create(Node::Kind::Failure); return NodeFactory::create(Node::Kind::Failure);
if (Mangled.nextIf('o')) if (Mangled.nextIf('o'))
return Node::create(Node::Kind::Module, "ObjectiveC"); return NodeFactory::create(Node::Kind::Module, "ObjectiveC");
if (Mangled.nextIf('C')) if (Mangled.nextIf('C'))
return Node::create(Node::Kind::Module, "C"); return NodeFactory::create(Node::Kind::Module, "C");
if (Mangled.nextIf('s')) if (Mangled.nextIf('s'))
return Node::create(Node::Kind::Module, STDLIB_NAME); return NodeFactory::create(Node::Kind::Module, STDLIB_NAME);
if (Mangled.nextIf('a')) if (Mangled.nextIf('a'))
return createSwiftType(Node::Kind::Structure, "Array"); return createSwiftType(Node::Kind::Structure, "Array");
if (Mangled.nextIf('b')) if (Mangled.nextIf('b'))
@@ -828,9 +808,9 @@ private:
return createSwiftType(Node::Kind::Structure, "UInt"); return createSwiftType(Node::Kind::Structure, "UInt");
Node::IndexType index_sub; Node::IndexType index_sub;
if (!demangleIndex(index_sub)) if (!demangleIndex(index_sub))
return Node::create(Node::Kind::Failure); return NodeFactory::create(Node::Kind::Failure);
if (index_sub >= Substitutions.size()) if (index_sub >= Substitutions.size())
return Node::create(Node::Kind::Failure); return NodeFactory::create(Node::Kind::Failure);
return Substitutions[index_sub]; return Substitutions[index_sub];
} }
@@ -857,7 +837,7 @@ private:
auto name = demangleDeclName(); auto name = demangleDeclName();
if (!name) return nullptr; if (!name) return nullptr;
auto decl = Node::create(kind); auto decl = NodeFactory::create(kind);
decl->addChild(context); decl->addChild(context);
decl->addChild(name); decl->addChild(name);
Substitutions.push_back(decl); Substitutions.push_back(decl);
@@ -868,7 +848,7 @@ private:
NodePointer proto = demangleProtocolNameImpl(); NodePointer proto = demangleProtocolNameImpl();
if (!proto) return nullptr; if (!proto) return nullptr;
NodePointer type = Node::create(Node::Kind::Type); NodePointer type = NodeFactory::create(Node::Kind::Type);
type->addChild(proto); type->addChild(proto);
return type; return type;
} }
@@ -890,7 +870,7 @@ private:
NodePointer name = demangleDeclName(); NodePointer name = demangleDeclName();
if (!name) return nullptr; if (!name) return nullptr;
auto proto = Node::create(Node::Kind::Protocol); auto proto = NodeFactory::create(Node::Kind::Protocol);
proto->addChild(std::move(sub)); proto->addChild(std::move(sub));
proto->addChild(std::move(name)); proto->addChild(std::move(name));
Substitutions.push_back(proto); Substitutions.push_back(proto);
@@ -926,8 +906,8 @@ private:
} }
NodePointer demangleProtocolList() { NodePointer demangleProtocolList() {
NodePointer proto_list = Node::create(Node::Kind::ProtocolList); NodePointer proto_list = NodeFactory::create(Node::Kind::ProtocolList);
NodePointer type_list = Node::create(Node::Kind::TypeList); NodePointer type_list = NodeFactory::create(Node::Kind::TypeList);
proto_list->addChild(type_list); proto_list->addChild(type_list);
if (Mangled.nextIf('_')) { if (Mangled.nextIf('_')) {
return proto_list; return proto_list;
@@ -958,7 +938,7 @@ private:
return nullptr; return nullptr;
#endif #endif
NodePointer proto_conformance = NodePointer proto_conformance =
Node::create(Node::Kind::ProtocolConformance); NodeFactory::create(Node::Kind::ProtocolConformance);
proto_conformance->addChild(type); proto_conformance->addChild(type);
proto_conformance->addChild(protocol); proto_conformance->addChild(protocol);
#if 0 #if 0
@@ -1068,7 +1048,7 @@ private:
if (!name) return nullptr; if (!name) return nullptr;
} }
NodePointer entity = Node::create(entityKind); NodePointer entity = NodeFactory::create(entityKind);
entity->addChild(context); entity->addChild(context);
if (name) entity->addChild(name); if (name) entity->addChild(name);
@@ -1101,7 +1081,7 @@ private:
/// has thought to enter a generic context /// has thought to enter a generic context
NodePointer demangleGenerics(GenericContext &C) { NodePointer demangleGenerics(GenericContext &C) {
DemanglerPrinter result_printer; DemanglerPrinter result_printer;
NodePointer archetypes = Node::create(Node::Kind::Generics); NodePointer archetypes = NodeFactory::create(Node::Kind::Generics);
// FIXME: Swallow the mangled associated type constraints. // FIXME: Swallow the mangled associated type constraints.
bool assocTypes = false; bool assocTypes = false;
while (true) { while (true) {
@@ -1118,7 +1098,7 @@ private:
&& !isStartOfIdentifier(c)) && !isStartOfIdentifier(c))
break; break;
if (!assocTypes) if (!assocTypes)
archetypes->addChild(Node::create( archetypes->addChild(NodeFactory::create(
Node::Kind::ArchetypeRef, archetypeName(ArchetypeCount))); Node::Kind::ArchetypeRef, archetypeName(ArchetypeCount)));
} else { } else {
NodePointer proto_list = demangleProtocolList(); NodePointer proto_list = demangleProtocolList();
@@ -1127,8 +1107,8 @@ private:
if (assocTypes) if (assocTypes)
continue; continue;
NodePointer arch_and_proto = NodePointer arch_and_proto =
Node::create(Node::Kind::ArchetypeAndProtocol); NodeFactory::create(Node::Kind::ArchetypeAndProtocol);
arch_and_proto->addChild(Node::create( arch_and_proto->addChild(NodeFactory::create(
Node::Kind::ArchetypeRef, archetypeName(ArchetypeCount))); Node::Kind::ArchetypeRef, archetypeName(ArchetypeCount)));
arch_and_proto->addChild(proto_list); arch_and_proto->addChild(proto_list);
archetypes->addChild(arch_and_proto); archetypes->addChild(arch_and_proto);
@@ -1140,7 +1120,7 @@ private:
NodePointer demangleArchetypeRef(Node::IndexType depth, Node::IndexType i) { NodePointer demangleArchetypeRef(Node::IndexType depth, Node::IndexType i) {
if (depth == 0 && ArchetypeCount == 0) if (depth == 0 && ArchetypeCount == 0)
return Node::create(Node::Kind::ArchetypeRef, archetypeName(i)); return NodeFactory::create(Node::Kind::ArchetypeRef, archetypeName(i));
size_t length = ArchetypeCounts.size(); size_t length = ArchetypeCounts.size();
if (depth >= length) if (depth >= length)
return nullptr; return nullptr;
@@ -1149,7 +1129,7 @@ private:
(depth == 0) ? ArchetypeCount : ArchetypeCounts[length - depth]; (depth == 0) ? ArchetypeCount : ArchetypeCounts[length - depth];
if (index >= max) if (index >= max)
return nullptr; return nullptr;
return Node::create(Node::Kind::ArchetypeRef, return NodeFactory::create(Node::Kind::ArchetypeRef,
archetypeName(index)); archetypeName(index));
} }
@@ -1187,20 +1167,20 @@ private:
os.flush(); os.flush();
} }
NodePointer paramTy = Node::create(Node::Kind::DependentGenericParamType, auto paramTy = NodeFactory::create(Node::Kind::DependentGenericParamType,
std::move(name)); std::move(name));
return paramTy; return paramTy;
} }
NodePointer demangleGenericSignature() { NodePointer demangleGenericSignature() {
NodePointer sig = Node::create(Node::Kind::DependentGenericSignature); auto sig = NodeFactory::create(Node::Kind::DependentGenericSignature);
// First read in the parameter counts at each depth. // First read in the parameter counts at each depth.
while (!Mangled.nextIf('R')) { while (!Mangled.nextIf('R')) {
Node::IndexType count; Node::IndexType count;
if (!demangleIndex(count)) if (!demangleIndex(count))
return nullptr; return nullptr;
NodePointer countNode = Node::create(Node::Kind::DependentGenericParamCount, auto countNode =
count); NodeFactory::create(Node::Kind::DependentGenericParamCount, count);
sig->addChild(countNode); sig->addChild(countNode);
} }
@@ -1220,8 +1200,8 @@ private:
if (!type) return nullptr; if (!type) return nullptr;
NodePointer requirement = demangleType(); NodePointer requirement = demangleType();
if (!requirement) return nullptr; if (!requirement) return nullptr;
NodePointer reqt auto reqt = NodeFactory::create(
= Node::create(Node::Kind::DependentGenericConformanceRequirement); Node::Kind::DependentGenericConformanceRequirement);
reqt->addChild(type); reqt->addChild(type);
reqt->addChild(requirement); reqt->addChild(requirement);
return reqt; return reqt;
@@ -1231,8 +1211,8 @@ private:
if (!first) return nullptr; if (!first) return nullptr;
NodePointer second = demangleType(); NodePointer second = demangleType();
if (!second) return nullptr; if (!second) return nullptr;
NodePointer reqt auto reqt = NodeFactory::create(
= Node::create(Node::Kind::DependentGenericSameTypeRequirement); Node::Kind::DependentGenericSameTypeRequirement);
reqt->addChild(first); reqt->addChild(first);
reqt->addChild(second); reqt->addChild(second);
return reqt; return reqt;
@@ -1242,8 +1222,7 @@ private:
NodePointer demangleArchetypeType() { NodePointer demangleArchetypeType() {
auto makeSelfType = [&](NodePointer proto) -> NodePointer { auto makeSelfType = [&](NodePointer proto) -> NodePointer {
NodePointer selfType auto selfType = NodeFactory::create(Node::Kind::SelfTypeRef);
= Node::create(Node::Kind::SelfTypeRef);
selfType->addChild(proto); selfType->addChild(proto);
Substitutions.push_back(selfType); Substitutions.push_back(selfType);
return selfType; return selfType;
@@ -1252,8 +1231,7 @@ private:
auto makeAssociatedType = [&](NodePointer root) -> NodePointer { auto makeAssociatedType = [&](NodePointer root) -> NodePointer {
NodePointer name = demangleIdentifier(); NodePointer name = demangleIdentifier();
if (!name) return nullptr; if (!name) return nullptr;
NodePointer assocType auto assocType = NodeFactory::create(Node::Kind::AssociatedTypeRef);
= Node::create(Node::Kind::AssociatedTypeRef);
assocType->addChild(root); assocType->addChild(root);
assocType->addChild(name); assocType->addChild(name);
Substitutions.push_back(assocType); Substitutions.push_back(assocType);
@@ -1291,12 +1269,12 @@ private:
NodePointer index = demangleIndexAsNode(); NodePointer index = demangleIndexAsNode();
if (!index) if (!index)
return nullptr; return nullptr;
NodePointer decl_ctx = Node::create(Node::Kind::DeclContext); NodePointer decl_ctx = NodeFactory::create(Node::Kind::DeclContext);
NodePointer ctx = demangleContext(); NodePointer ctx = demangleContext();
if (!ctx) if (!ctx)
return nullptr; return nullptr;
decl_ctx->addChild(ctx); decl_ctx->addChild(ctx);
NodePointer qual_atype = Node::create(Node::Kind::QualifiedArchetype); auto qual_atype = NodeFactory::create(Node::Kind::QualifiedArchetype);
qual_atype->addChild(index); qual_atype->addChild(index);
qual_atype->addChild(decl_ctx); qual_atype->addChild(decl_ctx);
return qual_atype; return qual_atype;
@@ -1308,13 +1286,13 @@ private:
} }
NodePointer demangleTuple(IsVariadic isV) { NodePointer demangleTuple(IsVariadic isV) {
NodePointer tuple = Node::create( NodePointer tuple = NodeFactory::create(
isV == IsVariadic::yes ? Node::Kind::VariadicTuple isV == IsVariadic::yes ? Node::Kind::VariadicTuple
: Node::Kind::NonVariadicTuple); : Node::Kind::NonVariadicTuple);
while (!Mangled.nextIf('_')) { while (!Mangled.nextIf('_')) {
if (!Mangled) if (!Mangled)
return nullptr; return nullptr;
NodePointer elt = Node::create(Node::Kind::TupleElement); NodePointer elt = NodeFactory::create(Node::Kind::TupleElement);
if (isStartOfIdentifier(Mangled.peek())) { if (isStartOfIdentifier(Mangled.peek())) {
NodePointer label = demangleIdentifier(Node::Kind::TupleElementName); NodePointer label = demangleIdentifier(Node::Kind::TupleElementName);
@@ -1334,7 +1312,7 @@ private:
} }
NodePointer postProcessReturnTypeNode (NodePointer out_args) { NodePointer postProcessReturnTypeNode (NodePointer out_args) {
NodePointer out_node = Node::create(Node::Kind::ReturnType); NodePointer out_node = NodeFactory::create(Node::Kind::ReturnType);
out_node->addChild(out_args); out_node->addChild(out_args);
return out_node; return out_node;
} }
@@ -1343,7 +1321,7 @@ private:
NodePointer type = demangleTypeImpl(); NodePointer type = demangleTypeImpl();
if (!type) if (!type)
return nullptr; return nullptr;
NodePointer nodeType = Node::create(Node::Kind::Type); NodePointer nodeType = NodeFactory::create(Node::Kind::Type);
nodeType->addChild(type); nodeType->addChild(type);
return nodeType; return nodeType;
} }
@@ -1355,8 +1333,8 @@ private:
NodePointer out_args = demangleType(); NodePointer out_args = demangleType();
if (!out_args) if (!out_args)
return nullptr; return nullptr;
NodePointer block = Node::create(kind); NodePointer block = NodeFactory::create(kind);
NodePointer in_node = Node::create(Node::Kind::ArgumentTuple); NodePointer in_node = NodeFactory::create(Node::Kind::ArgumentTuple);
block->addChild(in_node); block->addChild(in_node);
in_node->addChild(in_args); in_node->addChild(in_args);
block->addChild(postProcessReturnTypeNode(out_args)); block->addChild(postProcessReturnTypeNode(out_args));
@@ -1374,7 +1352,7 @@ private:
if (c == 'f') { if (c == 'f') {
Node::IndexType size; Node::IndexType size;
if (demangleBuiltinSize(size)) { if (demangleBuiltinSize(size)) {
return Node::create( return NodeFactory::create(
Node::Kind::BuiltinTypeName, Node::Kind::BuiltinTypeName,
(DemanglerPrinter() << "Builtin.Float" << size).str()); (DemanglerPrinter() << "Builtin.Float" << size).str());
} }
@@ -1382,7 +1360,7 @@ private:
if (c == 'i') { if (c == 'i') {
Node::IndexType size; Node::IndexType size;
if (demangleBuiltinSize(size)) { if (demangleBuiltinSize(size)) {
return Node::create( return NodeFactory::create(
Node::Kind::BuiltinTypeName, Node::Kind::BuiltinTypeName,
(DemanglerPrinter() << "Builtin.Int" << size).str()); (DemanglerPrinter() << "Builtin.Int" << size).str());
} }
@@ -1396,7 +1374,7 @@ private:
Node::IndexType size; Node::IndexType size;
if (!demangleBuiltinSize(size)) if (!demangleBuiltinSize(size))
return nullptr; return nullptr;
return Node::create( return NodeFactory::create(
Node::Kind::BuiltinTypeName, Node::Kind::BuiltinTypeName,
(DemanglerPrinter() << "Builtin.Vec" << elts << "xInt" << size) (DemanglerPrinter() << "Builtin.Vec" << elts << "xInt" << size)
.str()); .str());
@@ -1405,29 +1383,29 @@ private:
Node::IndexType size; Node::IndexType size;
if (!demangleBuiltinSize(size)) if (!demangleBuiltinSize(size))
return nullptr; return nullptr;
return Node::create( return NodeFactory::create(
Node::Kind::BuiltinTypeName, Node::Kind::BuiltinTypeName,
(DemanglerPrinter() << "Builtin.Vec" << elts << "xFloat" (DemanglerPrinter() << "Builtin.Vec" << elts << "xFloat"
<< size).str()); << size).str());
} }
if (Mangled.nextIf('p')) if (Mangled.nextIf('p'))
return Node::create( return NodeFactory::create(
Node::Kind::BuiltinTypeName, Node::Kind::BuiltinTypeName,
(DemanglerPrinter() << "Builtin.Vec" << elts << "xRawPointer") (DemanglerPrinter() << "Builtin.Vec" << elts << "xRawPointer")
.str()); .str());
} }
} }
if (c == 'O') if (c == 'O')
return Node::create(Node::Kind::BuiltinTypeName, return NodeFactory::create(Node::Kind::BuiltinTypeName,
"Builtin.UnknownObject"); "Builtin.UnknownObject");
if (c == 'o') if (c == 'o')
return Node::create(Node::Kind::BuiltinTypeName, return NodeFactory::create(Node::Kind::BuiltinTypeName,
"Builtin.NativeObject"); "Builtin.NativeObject");
if (c == 'p') if (c == 'p')
return Node::create(Node::Kind::BuiltinTypeName, return NodeFactory::create(Node::Kind::BuiltinTypeName,
"Builtin.RawPointer"); "Builtin.RawPointer");
if (c == 'w') if (c == 'w')
return Node::create(Node::Kind::BuiltinTypeName, return NodeFactory::create(Node::Kind::BuiltinTypeName,
"Builtin.Word"); "Builtin.Word");
return nullptr; return nullptr;
} }
@@ -1441,8 +1419,8 @@ private:
NodePointer out_args = demangleType(); NodePointer out_args = demangleType();
if (!out_args) if (!out_args)
return nullptr; return nullptr;
NodePointer block = Node::create(Node::Kind::ObjCBlock); NodePointer block = NodeFactory::create(Node::Kind::ObjCBlock);
NodePointer in_node = Node::create(Node::Kind::ArgumentTuple); NodePointer in_node = NodeFactory::create(Node::Kind::ArgumentTuple);
block->addChild(in_node); block->addChild(in_node);
in_node->addChild(in_args); in_node->addChild(in_args);
block->addChild(postProcessReturnTypeNode(out_args)); block->addChild(postProcessReturnTypeNode(out_args));
@@ -1453,7 +1431,7 @@ private:
if (!type) if (!type)
return nullptr; return nullptr;
NodePointer dynamicSelf = Node::create(Node::Kind::DynamicSelf); NodePointer dynamicSelf = NodeFactory::create(Node::Kind::DynamicSelf);
dynamicSelf->addChild(type); dynamicSelf->addChild(type);
return dynamicSelf; return dynamicSelf;
} }
@@ -1462,7 +1440,7 @@ private:
return nullptr; return nullptr;
if (!Mangled.nextIf('R')) if (!Mangled.nextIf('R'))
return nullptr; return nullptr;
return Node::create(Node::Kind::ErrorType, std::string()); return NodeFactory::create(Node::Kind::ErrorType, std::string());
} }
if (c == 'F') { if (c == 'F') {
return demangleFunctionType(Node::Kind::FunctionType); return demangleFunctionType(Node::Kind::FunctionType);
@@ -1475,13 +1453,13 @@ private:
if (!out_args) if (!out_args)
return nullptr; return nullptr;
NodePointer block = NodePointer block =
Node::create(Node::Kind::UncurriedFunctionType); NodeFactory::create(Node::Kind::UncurriedFunctionType);
block->addChild(in_args); block->addChild(in_args);
block->addChild(postProcessReturnTypeNode(out_args)); block->addChild(postProcessReturnTypeNode(out_args));
return block; return block;
} }
if (c == 'G') { if (c == 'G') {
NodePointer type_list = Node::create(Node::Kind::TypeList); NodePointer type_list = NodeFactory::create(Node::Kind::TypeList);
NodePointer unboundType = demangleType(); NodePointer unboundType = demangleType();
if (!unboundType) if (!unboundType)
return nullptr; return nullptr;
@@ -1511,7 +1489,7 @@ private:
assert(false && "trying to make a generic type application for a non class|struct|enum"); assert(false && "trying to make a generic type application for a non class|struct|enum");
} }
NodePointer type_application = NodePointer type_application =
Node::create(bound_type_kind); NodeFactory::create(bound_type_kind);
type_application->addChild(unboundType); type_application->addChild(unboundType);
type_application->addChild(type_list); type_application->addChild(type_list);
return type_application; return type_application;
@@ -1523,7 +1501,7 @@ private:
NodePointer type = demangleType(); NodePointer type = demangleType();
if (!type) if (!type)
return nullptr; return nullptr;
NodePointer metatype = Node::create(Node::Kind::Metatype); NodePointer metatype = NodeFactory::create(Node::Kind::Metatype);
metatype->addChild(type); metatype->addChild(type);
return metatype; return metatype;
} }
@@ -1531,7 +1509,7 @@ private:
if (c == 'M') { if (c == 'M') {
NodePointer type = demangleType(); NodePointer type = demangleType();
if (!type) return nullptr; if (!type) return nullptr;
NodePointer metatype = Node::create(Node::Kind::ExistentialMetatype); auto metatype = NodeFactory::create(Node::Kind::ExistentialMetatype);
metatype->addChild(type); metatype->addChild(type);
return metatype; return metatype;
} }
@@ -1545,7 +1523,7 @@ private:
return demangleDependentType(); return demangleDependentType();
} }
if (c == 'R') { if (c == 'R') {
NodePointer inout = Node::create(Node::Kind::InOut); NodePointer inout = NodeFactory::create(Node::Kind::InOut);
NodePointer type = demangleTypeImpl(); NodePointer type = demangleTypeImpl();
if (!type) if (!type)
return nullptr; return nullptr;
@@ -1567,7 +1545,7 @@ private:
NodePointer sub = demangleType(); NodePointer sub = demangleType();
if (!sub) return nullptr; if (!sub) return nullptr;
NodePointer dependentGenericType NodePointer dependentGenericType
= Node::create(Node::Kind::DependentGenericType); = NodeFactory::create(Node::Kind::DependentGenericType);
dependentGenericType->addChild(sig); dependentGenericType->addChild(sig);
dependentGenericType->addChild(sub); dependentGenericType->addChild(sub);
return dependentGenericType; return dependentGenericType;
@@ -1580,7 +1558,7 @@ private:
NodePointer base = demangleType(); NodePointer base = demangleType();
if (!base) if (!base)
return nullptr; return nullptr;
NodePointer genericType = Node::create(Node::Kind::GenericType); NodePointer genericType = NodeFactory::create(Node::Kind::GenericType);
genericType->addChild(generics); genericType->addChild(generics);
genericType->addChild(base); genericType->addChild(base);
return genericType; return genericType;
@@ -1590,7 +1568,7 @@ private:
NodePointer type = demangleType(); NodePointer type = demangleType();
if (!type) if (!type)
return nullptr; return nullptr;
NodePointer unowned = Node::create(Node::Kind::Unowned); NodePointer unowned = NodeFactory::create(Node::Kind::Unowned);
unowned->addChild(type); unowned->addChild(type);
return unowned; return unowned;
} }
@@ -1598,7 +1576,7 @@ private:
NodePointer type = demangleType(); NodePointer type = demangleType();
if (!type) if (!type)
return nullptr; return nullptr;
NodePointer unowned = Node::create(Node::Kind::Unmanaged); NodePointer unowned = NodeFactory::create(Node::Kind::Unmanaged);
unowned->addChild(type); unowned->addChild(type);
return unowned; return unowned;
} }
@@ -1606,7 +1584,7 @@ private:
NodePointer type = demangleType(); NodePointer type = demangleType();
if (!type) if (!type)
return nullptr; return nullptr;
NodePointer weak = Node::create(Node::Kind::Weak); NodePointer weak = NodeFactory::create(Node::Kind::Weak);
weak->addChild(type); weak->addChild(type);
return weak; return weak;
} }
@@ -1653,7 +1631,7 @@ private:
// impl-function-attribute ::= 'N' // noreturn // impl-function-attribute ::= 'N' // noreturn
// impl-function-attribute ::= 'G' // generic // impl-function-attribute ::= 'G' // generic
NodePointer demangleImplFunctionType() { NodePointer demangleImplFunctionType() {
NodePointer type = Node::create(Node::Kind::ImplFunctionType); NodePointer type = NodeFactory::create(Node::Kind::ImplFunctionType);
if (!demangleImplCalleeConvention(type)) if (!demangleImplCalleeConvention(type))
return nullptr; return nullptr;
@@ -1722,7 +1700,7 @@ private:
case ImplConventionContext::Parameter: return (FOR_PARAMETER); \ case ImplConventionContext::Parameter: return (FOR_PARAMETER); \
case ImplConventionContext::Result: return (FOR_RESULT); \ case ImplConventionContext::Result: return (FOR_RESULT); \
} \ } \
llvm_unreachable("bad context"); \ unreachable("bad context"); \
} }
auto Nothing = StringRef(); auto Nothing = StringRef();
CASE('a', Nothing, Nothing, "@autoreleased") CASE('a', Nothing, Nothing, "@autoreleased")
@@ -1748,13 +1726,13 @@ private:
if (attr.empty()) { if (attr.empty()) {
return failure(); return failure();
} }
type->addChild(Node::create(Node::Kind::ImplConvention, attr)); type->addChild(NodeFactory::create(Node::Kind::ImplConvention, attr));
return true; return true;
} }
void addImplFunctionAttribute(NodePointer parent, StringRef attr, void addImplFunctionAttribute(NodePointer parent, StringRef attr,
Node::Kind kind = Node::Kind::ImplFunctionAttribute) { Node::Kind kind = Node::Kind::ImplFunctionAttribute) {
parent->addChild(Node::create(kind, attr)); parent->addChild(NodeFactory::create(kind, attr));
} }
// impl-parameter ::= impl-convention type // impl-parameter ::= impl-convention type
@@ -1784,7 +1762,7 @@ private:
else if (kind == Node::Kind::ImplResult) else if (kind == Node::Kind::ImplResult)
return ImplConventionContext::Result; return ImplConventionContext::Result;
else else
llvm_unreachable("unexpected node kind"); unreachable("unexpected node kind");
}; };
auto convention = demangleImplConvention(getContext(kind)); auto convention = demangleImplConvention(getContext(kind));
@@ -1792,19 +1770,20 @@ private:
auto type = demangleType(); auto type = demangleType();
if (!type) return nullptr; if (!type) return nullptr;
NodePointer node = Node::create(kind); NodePointer node = NodeFactory::create(kind);
node->addChild(Node::create(Node::Kind::ImplConvention, node->addChild(NodeFactory::create(Node::Kind::ImplConvention,
convention)); convention));
node->addChild(type); node->addChild(type);
return node; return node;
} }
}; };
} // end anonymous namespace } // end anonymous namespace
NodePointer Demangle::demangleSymbolAsNode(llvm::StringRef mangled, NodePointer
const DemangleOptions &options) { swift::Demangle::demangleSymbolAsNode(const char *MangledName,
PrettyStackTraceStringAction prettyStackTrace("demangling string", mangled); size_t MangledNameLength,
Demangler demangler(mangled); const DemangleOptions &Options) {
Demangler demangler(StringRef(MangledName, MangledNameLength));
demangler.demangle(); demangler.demangle();
return demangler.getDemangled(); return demangler.getDemangled();
} }
@@ -2128,9 +2107,9 @@ private:
switch (curState) { switch (curState) {
case Attrs: Printer << '('; continue; case Attrs: Printer << '('; continue;
case Inputs: Printer << ") -> ("; continue; case Inputs: Printer << ") -> ("; continue;
case Results: llvm_unreachable("no state after Results"); case Results: unreachable("no state after Results");
} }
llvm_unreachable("bad state"); unreachable("bad state");
} }
}; };
@@ -2671,8 +2650,8 @@ void NodePrinter::print(Node *pointer, bool asContext, bool suppressType) {
return; return;
} }
case Node::Kind::DependentGenericParamCount: case Node::Kind::DependentGenericParamCount:
llvm_unreachable("should be printed as a child of a " unreachable("should be printed as a child of a "
"DependentGenericSignature"); "DependentGenericSignature");
case Node::Kind::DependentGenericConformanceRequirement: { case Node::Kind::DependentGenericConformanceRequirement: {
Node *type = pointer->getChild(0); Node *type = pointer->getChild(0);
Node *reqt = pointer->getChild(1); Node *reqt = pointer->getChild(1);
@@ -2709,7 +2688,7 @@ void NodePrinter::print(Node *pointer, bool asContext, bool suppressType) {
return; return;
} }
} }
llvm_unreachable("bad node kind!"); unreachable("bad node kind!");
} }
std::string Demangle::nodeToString(NodePointer root, std::string Demangle::nodeToString(NodePointer root,
@@ -2717,18 +2696,19 @@ std::string Demangle::nodeToString(NodePointer root,
if (!root) if (!root)
return ""; return "";
PrettyStackTraceNode trace("printing", root.get());
return NodePrinter(options).printRoot(root.get()); return NodePrinter(options).printRoot(root.get());
} }
std::string Demangle::demangleSymbolAsString(llvm::StringRef mangled, std::string Demangle::demangleSymbolAsString(const char *MangledName,
const DemangleOptions &options) { size_t MangledNameLength,
auto root = demangleSymbolAsNode(mangled, options); const DemangleOptions &Options) {
auto mangled = StringRef(MangledName, MangledNameLength);
auto root = demangleSymbolAsNode(MangledName, MangledNameLength, Options);
if (!root) return mangled.str(); if (!root) return mangled.str();
PrettyStackTraceStringAction trace("printing the demangling of", mangled); std::string demangling = nodeToString(std::move(root), Options);
std::string demangling = nodeToString(std::move(root), options);
if (demangling.empty()) if (demangling.empty())
return mangled.str(); return mangled.str();
return demangling; return demangling;
} }

View File

@@ -0,0 +1,96 @@
//===--- DemangleWrappers.cpp - Swift Name Demangling --------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2015 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===---------------------------------------------------------------------===//
#include "swift/Basic/DemangleWrappers.h"
#include "swift/Basic/PrettyStackTrace.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Support/ErrorHandling.h"
using namespace swift;
using namespace demangle_wrappers;
static StringRef getNodeKindString(swift::Demangle::Node::Kind k) {
switch (k) {
#define NODE(ID) \
case Node::Kind::ID: \
return #ID;
#include "swift/Basic/DemangleNodes.def"
}
llvm_unreachable("bad node kind");
}
static void printNode(llvm::raw_ostream &Out, const Node *node,
unsigned depth) {
// Indent two spaces per depth.
Out.indent(depth * 2);
Out << "kind=" << getNodeKindString(node->getKind());
if (node->hasText()) {
Out << ", text=\"" << node->getText() << '\"';
}
if (node->hasIndex()) {
Out << ", index=" << node->getIndex();
}
Out << '\n';
for (auto &child : *node) {
printNode(Out, child.get(), depth + 1);
}
}
void NodeDumper::dump() const { print(llvm::errs()); }
void NodeDumper::print(llvm::raw_ostream &Out) const {
printNode(Out, Root.get(), 0);
}
namespace {
/// A pretty-stack-trace node for demangling trees.
class PrettyStackTraceNode : public llvm::PrettyStackTraceEntry {
const char *Action;
Node *TheNode;
public:
PrettyStackTraceNode(const char *action, Node *node)
: Action(action), TheNode(node) {}
void print(llvm::raw_ostream &out) const override {
out << "While " << Action << ' ';
if (!TheNode) {
out << "<<null demangling node>>\n";
} else {
out << "demangling tree:\n";
printNode(out, TheNode, 4);
}
}
};
} // end unnamed namespace
NodePointer
swift::demangle_wrappers::demangleSymbolAsNode(llvm::StringRef MangledName,
const DemangleOptions &Options) {
PrettyStackTraceStringAction prettyStackTrace("demangling string",
MangledName);
return swift::Demangle::demangleSymbolAsNode(MangledName.data(),
MangledName.size(), Options);
}
std::string nodeToString(NodePointer Root,
const DemangleOptions &Options) {
PrettyStackTraceNode trace("printing", Root.get());
return swift::Demangle::nodeToString(Root, Options);
}
std::string swift::demangle_wrappers::demangleSymbolAsString(
llvm::StringRef MangledName, const DemangleOptions &Options) {
PrettyStackTraceStringAction prettyStackTrace("demangling string",
MangledName);
return swift::Demangle::demangleSymbolAsString(MangledName.data(),
MangledName.size(), Options);
}

View File

@@ -15,8 +15,8 @@
// //
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
#include <swift/Basic/Demangle.h> #include "swift/Basic/DemangleWrappers.h"
#include <swift/FunctionNameDemangle/FunctionNameDemangle.h> #include "swift/FunctionNameDemangle/FunctionNameDemangle.h"
/// \returns true if \p MangledName starts with Swift prefix, "_T". /// \returns true if \p MangledName starts with Swift prefix, "_T".
static bool isSwiftPrefixed(const char *MangledName) { static bool isSwiftPrefixed(const char *MangledName) {
@@ -34,8 +34,8 @@ size_t fnd_get_demangled_name(const char *MangledName, char *OutputBuffer,
swift::Demangle::DemangleOptions DemangleOptions; swift::Demangle::DemangleOptions DemangleOptions;
DemangleOptions.SynthesizeSugarOnTypes = true; DemangleOptions.SynthesizeSugarOnTypes = true;
std::string Result = std::string Result = swift::demangle_wrappers::demangleSymbolAsString(
swift::Demangle::demangleSymbolAsString(MangledName, DemangleOptions); MangledName, DemangleOptions);
if (Result == MangledName) if (Result == MangledName)
return 0; // Not a mangled name return 0; // Not a mangled name

View File

@@ -15,7 +15,7 @@
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
#include "swift/Strings.h" #include "swift/Strings.h"
#include "swift/Basic/Demangle.h" #include "swift/Basic/DemangleWrappers.h"
#include "swift/Basic/QuotedString.h" #include "swift/Basic/QuotedString.h"
#include "swift/SIL/SILDebugScope.h" #include "swift/SIL/SILDebugScope.h"
#include "swift/SIL/SILDeclRef.h" #include "swift/SIL/SILDeclRef.h"
@@ -33,10 +33,11 @@
#include "llvm/ADT/APInt.h" #include "llvm/ADT/APInt.h"
#include "llvm/ADT/SmallString.h" #include "llvm/ADT/SmallString.h"
#include "llvm/ADT/StringExtras.h" #include "llvm/ADT/StringExtras.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/FormattedStream.h" #include "llvm/Support/FormattedStream.h"
using namespace swift; using namespace swift;
using namespace Demangle; using namespace demangle_wrappers;
struct ID { struct ID {
enum ID_Kind { enum ID_Kind {

View File

@@ -11,7 +11,6 @@
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
#define DEBUG_TYPE "array-opts" #define DEBUG_TYPE "array-opts"
#include "swift/Basic/Demangle.h"
#include "swift/SIL/CFG.h" #include "swift/SIL/CFG.h"
#include "swift/SIL/SILArgument.h" #include "swift/SIL/SILArgument.h"
#include "swift/SIL/SILInstruction.h" #include "swift/SIL/SILInstruction.h"

View File

@@ -15,7 +15,7 @@
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
#define DEBUG_TYPE "sil-devirtualizer" #define DEBUG_TYPE "sil-devirtualizer"
#include "swift/Basic/Demangle.h" #include "swift/Basic/DemangleWrappers.h"
#include "swift/Basic/Fallthrough.h" #include "swift/Basic/Fallthrough.h"
#include "swift/SIL/CallGraph.h" #include "swift/SIL/CallGraph.h"
#include "swift/SIL/SILArgument.h" #include "swift/SIL/SILArgument.h"
@@ -802,8 +802,8 @@ public:
// arguments for all existing functions. // arguments for all existing functions.
for (auto &F : *getModule()) { for (auto &F : *getModule()) {
DEBUG(llvm::dbgs() << "*** Devirtualizing Function: " DEBUG(llvm::dbgs() << "*** Devirtualizing Function: "
<< Demangle::demangleSymbolAsString(F.getName()) << demangle_wrappers::demangleSymbolAsString(F.getName())
<< "\n"); << "\n");
for (auto &BB : F) { for (auto &BB : F) {
for (auto II = BB.begin(), IE = BB.end(); II != IE;) { for (auto II = BB.begin(), IE = BB.end(); II != IE;) {
ApplyInst *AI = dyn_cast<ApplyInst>(&*II); ApplyInst *AI = dyn_cast<ApplyInst>(&*II);

View File

@@ -11,7 +11,7 @@
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
#define DEBUG_TYPE "globalopt" #define DEBUG_TYPE "globalopt"
#include "swift/Basic/Demangle.h" #include "swift/Basic/DemangleWrappers.h"
#include "swift/SIL/CFG.h" #include "swift/SIL/CFG.h"
#include "swift/SIL/SILInstruction.h" #include "swift/SIL/SILInstruction.h"
#include "swift/SILAnalysis/ColdBlockInfo.h" #include "swift/SILAnalysis/ColdBlockInfo.h"
@@ -109,7 +109,7 @@ bool SILGlobalOpt::isInLoop(SILBasicBlock *CurBB) {
void SILGlobalOpt::placeInitializers(SILFunction *InitF, void SILGlobalOpt::placeInitializers(SILFunction *InitF,
ArrayRef<ApplyInst*> Calls) { ArrayRef<ApplyInst*> Calls) {
DEBUG(llvm::dbgs() << "GlobalOpt: calls to " DEBUG(llvm::dbgs() << "GlobalOpt: calls to "
<< Demangle::demangleSymbolAsString(InitF->getName()) << demangle_wrappers::demangleSymbolAsString(InitF->getName())
<< " : " << Calls.size() << "\n"); << " : " << Calls.size() << "\n");
// Map each initializer-containing function to its final initializer call. // Map each initializer-containing function to its final initializer call.
llvm::DenseMap<SILFunction*, ApplyInst*> ParentFuncs; llvm::DenseMap<SILFunction*, ApplyInst*> ParentFuncs;

View File

@@ -14,7 +14,7 @@
// //
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
#include "swift/Basic/Demangle.h" #include "swift/Basic/DemangleWrappers.h"
#include "llvm/Support/CommandLine.h" #include "llvm/Support/CommandLine.h"
#include "llvm/Support/MemoryBuffer.h" #include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/PrettyStackTrace.h" #include "llvm/Support/PrettyStackTrace.h"
@@ -49,10 +49,10 @@ static void demangle(llvm::raw_ostream &os, llvm::StringRef name,
if (name.startswith("__")) if (name.startswith("__"))
name = name.substr(1); name = name.substr(1);
swift::Demangle::NodePointer pointer = swift::Demangle::NodePointer pointer =
swift::Demangle::demangleSymbolAsNode(name); swift::demangle_wrappers::demangleSymbolAsNode(name);
if (ExpandMode) { if (ExpandMode) {
llvm::outs() << "Demangling for " << name << '\n'; llvm::outs() << "Demangling for " << name << '\n';
pointer->print(llvm::outs()); swift::demangle_wrappers::NodeDumper(pointer).print(llvm::outs());
} }
if (!TreeOnly) { if (!TreeOnly) {
std::string string = swift::Demangle::nodeToString(pointer, options); std::string string = swift::Demangle::nodeToString(pointer, options);