Files
swift-mirror/include/swift/Basic/Demangle.h
2013-11-15 00:18:45 +00:00

225 lines
5.3 KiB
C++

//===--- Demangle.h - Interface to Swift symbol demangling -------*- 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
//
//===----------------------------------------------------------------------===//
#ifndef SWIFT_BASIC_DEMANGLE_H
#define SWIFT_BASIC_DEMANGLE_H
#include "llvm/ADT/IntrusiveRefCntPtr.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringRef.h"
#include "swift/Basic/LLVM.h"
namespace swift {
namespace Demangle {
struct DemangleOptions
{
bool SynthesizeSugarOnTypes = false;
DemangleOptions () : SynthesizeSugarOnTypes(false) {}
};
class Node;
typedef llvm::IntrusiveRefCntPtr<Node> NodePointer;
class Node : public llvm::RefCountedBase<Node> {
public:
typedef llvm::SmallVector<NodePointer,10> NodeVector;
typedef NodeVector::iterator iterator;
typedef NodeVector::const_iterator const_iterator;
typedef NodeVector::size_type size_type;
enum class Kind {
Failure = 0,
Addressor,
Allocator,
ArchetypeAndProtocol,
ArchetypeList,
ArchetypeRef,
ArgumentTuple,
ArrayType,
AssociatedTypeRef,
BoundGenericClass,
BoundGenericEnum,
BoundGenericStructure,
BridgeToBlockFunction,
BuiltinTypeName,
Class,
Constructor,
Deallocator,
Declaration,
DeclContext,
DependentProtocolWitnessTableGenerator,
DependentProtocolWitnessTableTemplate,
Destructor,
Directness,
Enum,
ErrorType,
FieldOffset,
FunctionType,
GenericType,
GenericTypeMetadataPattern,
Getter,
Identifier,
InOut,
InfixOperator,
LazyProtocolWitnessTableAccessor,
LazyProtocolWitnessTableTemplate,
LocalEntity,
MetaType,
Metaclass,
Module,
NominalTypeDescriptor,
NonVariadicTuple,
Number,
ObjCAttribute,
ObjCBlock,
Path,
PostfixOperator,
PrefixOperator,
Protocol,
ProtocolConformance,
ProtocolList,
ProtocolWitness,
ProtocolWitnessTable,
QualifiedArchetype,
ReturnType,
SelfTypeRef,
Setter,
Structure,
TupleElement,
TupleElementName,
TupleElementType,
Type,
TypeList,
TypeMetadata,
UncurriedFunctionType,
Unknown,
Unowned,
ValueWitnessKind,
ValueWitnessTable,
VariadicTuple,
Weak,
WitnessTableOffset
};
size_type size ();
iterator begin ();
iterator end ();
const_iterator begin () const;
const_iterator end () const;
NodePointer front ();
NodePointer back ();
NodePointer child_at (size_type idx);
Node (Kind k, std::string t);
Node (const Node& other);
Node* getParent ();
NodePointer getNextNode ();
Node* getPreviousNode ();
NodePointer push_back_child (NodePointer child);
void setParent (Node *parent);
void setNextNode (NodePointer successor);
Kind getKind();
void setKind (Kind k);
std::string getText();
void setText (const std::string &t);
static NodePointer makeNodePointer(Kind k, std::string t = "");
private:
struct FindPtr {
FindPtr(Node* v) : ptr_val(v) {}
bool operator () (NodePointer sp) {
return sp.getPtr() == ptr_val;
}
private:
Node* ptr_val;
};
Kind NodeKind;
std::string NodeText;
NodePointer Successor;
NodeVector Children;
Node* Parent;
Node* Predecessor;
void setParentImpl (Node *parent);
void setSuccessorImpl (NodePointer successor);
void push_back_childImpl (NodePointer child);
void insertSiblingImpl (NodePointer child);
};
/// \brief Demangle the given string as a Swift symbol.
///
/// Typical usage:
/// \code
/// NodePointer aDemangledName =
/// swift::Demangler::demangleSymbol("SomeSwiftMangledName")
/// \endcode
///
/// \param mangled The mangled string.
/// \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
/// failure.
///
NodePointer demangleSymbolAsNode(llvm::StringRef mangled, const DemangleOptions& options = DemangleOptions());
/// \brief Transform the node structure in a string.
///
/// Typical usage:
/// \code
/// std::string aDemangledName =
/// swift::Demangler::nodeToString(aNode)
/// \endcode
///
/// \param pointer A pointer to a parse tree generated by the demangler.
/// \param options An object encapsulating options to use to perform this demangling.
///
/// \returns A string representing the demangled name.
///
std::string nodeToString(NodePointer pointer, const DemangleOptions& options = DemangleOptions());
/// \brief Demangle the given string as a Swift symbol.
///
/// Typical usage:
/// \code
/// std::string aDemangledName =
/// swift::Demangler::demangleSymbol("SomeSwiftMangledName")
/// \endcode
///
/// \param mangled The mangled string.
/// \param options An object encapsulating options to use to perform this demangling.
///
///
/// \returns A string representing the demangled name.
///
std::string demangleSymbolAsString(llvm::StringRef mangled, const DemangleOptions& options = DemangleOptions());
} // end namespace Demangle
} // end namespace swift
#endif