Skip to content
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
22 changes: 15 additions & 7 deletions include/swift/AST/ASTDemangler.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@ class ASTBuilder {

GenericTypeDecl *createTypeDecl(StringRef mangledName, bool &typeAlias);

GenericTypeDecl *createTypeDecl(const Demangle::NodePointer &node,
GenericTypeDecl *createTypeDecl(NodePointer node,
bool &typeAlias);

ProtocolDecl *createProtocolDecl(const Demangle::NodePointer &node);
ProtocolDecl *createProtocolDecl(NodePointer node);

Type createNominalType(GenericTypeDecl *decl);

Expand Down Expand Up @@ -126,22 +126,30 @@ class ASTBuilder {

Type getOpaqueType();

Type createOptionalType(Type base);

Type createArrayType(Type base);

Type createDictionaryType(Type key, Type value);

Type createParenType(Type base);

private:
bool validateParentType(TypeDecl *decl, Type parent);
CanGenericSignature demangleGenericSignature(
NominalTypeDecl *nominalDecl,
const Demangle::NodePointer &node);
DeclContext *findDeclContext(const Demangle::NodePointer &node);
ModuleDecl *findModule(const Demangle::NodePointer &node);
Demangle::NodePointer findModuleNode(const Demangle::NodePointer &node);
NodePointer node);
DeclContext *findDeclContext(NodePointer node);
ModuleDecl *findModule(NodePointer node);
Demangle::NodePointer findModuleNode(NodePointer node);

enum class ForeignModuleKind {
Imported,
SynthesizedByImporter
};

Optional<ForeignModuleKind>
getForeignModuleKind(const Demangle::NodePointer &node);
getForeignModuleKind(NodePointer node);

GenericTypeDecl *findTypeDecl(DeclContext *dc,
Identifier name,
Expand Down
8 changes: 0 additions & 8 deletions include/swift/AST/ASTMangler.h
Original file line number Diff line number Diff line change
Expand Up @@ -214,14 +214,6 @@ class ASTMangler : public Mangler {

void bindGenericParameters(CanGenericSignature sig);

/// Mangles a sugared type iff we are mangling for the debugger.
template <class T> void appendSugaredType(Type type) {
assert(DWARFMangling &&
"sugared types are only legal when mangling for the debugger");
auto *BlandTy = cast<T>(type.getPointer())->getSinglyDesugaredType();
appendType(BlandTy);
}

void appendBoundGenericArgs(Type type, bool &isFirstArgList);

/// Append the bound generics arguments for the given declaration context
Expand Down
84 changes: 47 additions & 37 deletions include/swift/Demangling/Demangle.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Copyright (c) 2014 - 2019 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
Expand Down Expand Up @@ -35,6 +35,11 @@ namespace Demangle {

enum class SymbolicReferenceKind : uint8_t;

/// A simple default implementation that assigns letters to type parameters in
/// alphabetic order.
std::string genericParameterName(uint64_t depth, uint64_t index);

/// Display style options for the demangler.
struct DemangleOptions {
bool SynthesizeSugarOnTypes = false;
bool DisplayDebuggerGeneratedModule = true;
Expand All @@ -52,6 +57,8 @@ struct DemangleOptions {
bool ShortenArchetype = false;
bool ShowPrivateDiscriminators = true;
bool ShowFunctionArgumentTypes = true;
std::function<std::string(uint64_t, uint64_t)> GenericParameterName =
genericParameterName;

DemangleOptions() {}

Expand Down Expand Up @@ -142,32 +149,38 @@ class Node {
friend class NodeFactory;

private:
Kind NodeKind;

enum class PayloadKind : uint8_t {
None, Text, Index
struct NodeVector {
NodePointer *Nodes;
uint32_t Number = 0;
uint32_t Capacity = 0;
};
PayloadKind NodePayloadKind;

union {
llvm::StringRef TextPayload;
IndexType IndexPayload;
llvm::StringRef Text;
IndexType Index;
NodePointer InlineChildren[2];
NodeVector Children;
};

NodePointer *Children = nullptr;
size_t NumChildren = 0;
size_t ReservedChildren = 0;

Kind NodeKind;

enum class PayloadKind : uint8_t {
None, Text, Index, OneChild, TwoChildren, ManyChildren
};
PayloadKind NodePayloadKind;

Node(Kind k)
: NodeKind(k), NodePayloadKind(PayloadKind::None) {
}
Node(Kind k, llvm::StringRef t)
: NodeKind(k), NodePayloadKind(PayloadKind::Text) {
TextPayload = t;
Text = t;
}
Node(Kind k, IndexType index)
: NodeKind(k), NodePayloadKind(PayloadKind::Index) {
IndexPayload = index;
Index = index;
}
Node(const Node &) = delete;
Node &operator=(const Node &) = delete;
Expand All @@ -178,37 +191,33 @@ class Node {
bool hasText() const { return NodePayloadKind == PayloadKind::Text; }
llvm::StringRef getText() const {
assert(hasText());
return TextPayload;
return Text;
}

bool hasIndex() const { return NodePayloadKind == PayloadKind::Index; }
uint64_t getIndex() const {
assert(hasIndex());
return IndexPayload;
return Index;
}

using iterator = NodePointer *;
using const_iterator = const NodePointer *;
using size_type = size_t;
using iterator = const NodePointer *;

size_t getNumChildren() const;

bool hasChildren() const { return NumChildren != 0; }
size_t getNumChildren() const { return NumChildren; }
iterator begin() { return Children; }
iterator end() { return Children + NumChildren; }
const_iterator begin() const { return Children; }
const_iterator end() const { return Children + NumChildren; }
bool hasChildren() const { return getNumChildren() != 0; }

iterator begin() const;

iterator end() const;

NodePointer getFirstChild() const {
assert(NumChildren >= 1);
return Children[0];
return getChild(0);
}
NodePointer getChild(size_t index) const {
assert(NumChildren > index);
return Children[index];
assert(getNumChildren() > index);
return begin()[index];
}

// inline void addChild(NodePointer Child, Context &Ctx);

// Only to be used by the demangler parsers.
void addChild(NodePointer Child, NodeFactory &Factory);
// Only to be used by the demangler parsers.
Expand Down Expand Up @@ -344,17 +353,19 @@ class Context {
/// prefix: _T, _T0, $S, _$S.
///
/// \returns The demangled string.
std::string demangleSymbolAsString(llvm::StringRef MangledName,
const DemangleOptions &Options = DemangleOptions());
std::string demangleSymbolAsString(
llvm::StringRef MangledName,
const DemangleOptions &Options = DemangleOptions());

/// Demangle the given type and return the readable name.
///
/// \param MangledName The mangled type string, which does _not_ start with
/// a mangling prefix.
///
/// \returns The demangled string.
std::string demangleTypeAsString(llvm::StringRef MangledName,
const DemangleOptions &Options = DemangleOptions());
std::string
demangleTypeAsString(llvm::StringRef MangledName,
const DemangleOptions &Options = DemangleOptions());

/// Returns true if the mangledName refers to a thunk function.
///
Expand Down Expand Up @@ -473,7 +484,7 @@ void mangleIdentifier(const char *data, size_t length,
/// Remangle a demangled parse tree.
///
/// This should always round-trip perfectly with demangleSymbolAsNode.
std::string mangleNode(const NodePointer &root);
std::string mangleNode(NodePointer root);

using SymbolicResolver =
llvm::function_ref<Demangle::NodePointer (SymbolicReferenceKind,
Expand All @@ -483,13 +494,13 @@ using SymbolicResolver =
/// symbolic references.
///
/// This should always round-trip perfectly with demangleSymbolAsNode.
std::string mangleNode(const NodePointer &root, SymbolicResolver resolver);
std::string mangleNode(NodePointer root, SymbolicResolver resolver);

/// 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);
std::string mangleNodeOld(NodePointer root);

/// Transform the node structure to a string.
///
Expand Down Expand Up @@ -569,7 +580,6 @@ bool nodeConsumesGenericArgs(Node *node);
bool isSpecialized(Node *node);

NodePointer getUnspecialized(Node *node, NodeFactory &Factory);
std::string archetypeName(Node::IndexType index, Node::IndexType depth);

/// Form a StringRef around the mangled name starting at base, if the name may
/// contain symbolic references.
Expand Down
5 changes: 5 additions & 0 deletions include/swift/Demangling/DemangleNodes.def
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ NODE(FullTypeMetadata)
CONTEXT_NODE(Function)
NODE(FunctionSignatureSpecialization)
NODE(FunctionSignatureSpecializationParam)
NODE(FunctionSignatureSpecializationReturn)
NODE(FunctionSignatureSpecializationParamKind)
NODE(FunctionSignatureSpecializationParamPayload)
NODE(FunctionType)
Expand Down Expand Up @@ -252,5 +253,9 @@ NODE(ModuleDescriptor)
NODE(ExtensionDescriptor)
NODE(AnonymousDescriptor)
NODE(AssociatedTypeGenericParamRef)
NODE(SugaredOptional)
NODE(SugaredArray)
NODE(SugaredDictionary)
NODE(SugaredParen)
#undef CONTEXT_NODE
#undef NODE
8 changes: 4 additions & 4 deletions include/swift/Demangling/Demangler.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ class NodeFactory {
/// new memory address.
/// The \p Capacity is enlarged at least by \p MinGrowth, but can also be
/// enlarged by a bigger value.
template<typename T> void Reallocate(T *&Objects, size_t &Capacity,
template<typename T> void Reallocate(T *&Objects, uint32_t &Capacity,
size_t MinGrowth) {
size_t OldAllocSize = Capacity * sizeof(T);
size_t AdditionalAlloc = MinGrowth * sizeof(T);
Expand Down Expand Up @@ -203,8 +203,8 @@ template<typename T> class Vector {

protected:
T *Elems = nullptr;
size_t NumElems = 0;
size_t Capacity = 0;
uint32_t NumElems = 0;
uint32_t Capacity = 0;

public:
using iterator = T *;
Expand Down Expand Up @@ -469,7 +469,7 @@ class Demangler : public NodeFactory {
NodePointer demangleThunkOrSpecialization();
NodePointer demangleGenericSpecialization(Node::Kind SpecKind);
NodePointer demangleFunctionSpecialization();
NodePointer demangleFuncSpecParam(Node::IndexType ParamIdx);
NodePointer demangleFuncSpecParam(Node::Kind Kind);
NodePointer addFuncSpecParamNumber(NodePointer Param,
FunctionSigSpecializationParamKind Kind);

Expand Down
Loading