1919#ifndef SWIFT_DEMANGLING_DEMANGLE_H
2020#define SWIFT_DEMANGLING_DEMANGLE_H
2121
22+ #include " swift/Demangling/Demangle.h"
2223#include " swift/Demangling/Errors.h"
2324#include " swift/Demangling/ManglingFlavor.h"
2425#include " swift/Demangling/NamespaceMacros.h"
@@ -100,6 +101,7 @@ struct DemangleOptions {
100101
101102class Node ;
102103using NodePointer = Node *;
104+ class NodePrinter ;
103105
104106enum class FunctionSigSpecializationParamKind : unsigned {
105107 // Option Flags use bits 0-5. This give us 6 bits implying 64 entries to
@@ -466,16 +468,26 @@ class Context {
466468 // / The lifetime of the returned node tree ends with the lifetime of the
467469 // / context or with a call of clear().
468470 NodePointer demangleTypeAsNode (llvm::StringRef MangledName);
469-
471+
470472 // / Demangle the given symbol and return the readable name.
471473 // /
472474 // / \param MangledName The mangled symbol string, which start a mangling
473475 // / prefix: _T, _T0, $S, _$S.
474476 // /
475477 // / \returns The demangled string.
476- std::string demangleSymbolAsString (
477- llvm::StringRef MangledName,
478- const DemangleOptions &Options = DemangleOptions());
478+ std::string
479+ demangleSymbolAsString (llvm::StringRef MangledName,
480+ const DemangleOptions &Options = DemangleOptions());
481+
482+ // / Demangle the given symbol and store the result in the `printer`.
483+ // /
484+ // / \param MangledName The mangled symbol string, which start a mangling
485+ // / prefix: _T, _T0, $S, _$S.
486+ // / \param Printer The NodePrinter that will be used to demangle the symbol.
487+ // /
488+ // / \returns The demangled string.
489+ void demangleSymbolAsString (llvm::StringRef MangledName,
490+ NodePrinter &Printer);
479491
480492 // / Demangle the given type and return the readable name.
481493 // /
@@ -534,6 +546,16 @@ std::string
534546demangleSymbolAsString (const char *mangledName, size_t mangledNameLength,
535547 const DemangleOptions &options = DemangleOptions());
536548
549+ // / Standalone utility function to demangle the given symbol as string. The
550+ // / demangled string is stored in the `printer`.
551+ // /
552+ // / If performance is an issue when demangling multiple symbols,
553+ // / \param mangledName The mangled name string pointer.
554+ // / \param mangledNameLength The length of the mangledName string.
555+ // / \param printer The NodePrinter that will be used to demangle the symbol.
556+ void demangleSymbolAsString (const llvm::StringRef mangledName,
557+ NodePrinter &printer);
558+
537559// / Standalone utility function to demangle the given symbol as string.
538560// /
539561// / If performance is an issue when demangling multiple symbols,
@@ -546,7 +568,7 @@ demangleSymbolAsString(const std::string &mangledName,
546568 return demangleSymbolAsString (mangledName.data (), mangledName.size (),
547569 options);
548570}
549-
571+
550572// / Standalone utility function to demangle the given symbol as string.
551573// /
552574// / If performance is an issue when demangling multiple symbols,
@@ -726,13 +748,19 @@ ManglingErrorOr<const char *> mangleNodeAsObjcCString(NodePointer node,
726748// / \endcode
727749// /
728750// / \param Root A pointer to a parse tree generated by the demangler.
729- // / \param Options An object encapsulating options to use to perform this demangling.
751+ // / \param Options An object encapsulating options to use to perform this
752+ // / demangling.
730753// /
731754// / \returns A string representing the demangled name.
732- // /
733755std::string nodeToString (NodePointer Root,
734756 const DemangleOptions &Options = DemangleOptions());
735757
758+ // / Transform the node structure to a string, which is stored in the `Printer`.
759+ // /
760+ // / \param Root A pointer to a parse tree generated by the demangler.
761+ // / \param Printer A NodePrinter used to pretty print the demangled Node.
762+ void nodeToString (NodePointer Root, NodePrinter &Printer);
763+
736764// / Transforms a mangled key path accessor thunk helper
737765// / into the identfier/subscript that would be used to invoke it in swift code.
738766std::string keyPathSourceString (const char *MangledName,
@@ -778,11 +806,14 @@ class DemanglerPrinter {
778806
779807 llvm::StringRef getStringRef () const { return Stream; }
780808
809+ size_t getStreamLength () { return Stream.length (); }
810+
781811 // / Shrinks the buffer.
782812 void resetSize (size_t toPos) {
783813 assert (toPos <= Stream.size ());
784814 Stream.resize (toPos);
785815 }
816+
786817private:
787818 std::string Stream;
788819};
@@ -819,8 +850,17 @@ std::string mangledNameForTypeMetadataAccessor(
819850 llvm::StringRef moduleName, llvm::StringRef typeName, Node::Kind typeKind,
820851 Mangle::ManglingFlavor Flavor = Mangle::ManglingFlavor::Default);
821852
853+ // / Base class for printing a Swift demangled node tree.
854+ // /
855+ // / NodePrinter is used to convert demangled Swift symbol nodes into
856+ // / human-readable string representations. It handles formatting, indentation,
857+ // / and Swift-specific syntax.
858+ // /
859+ // / The virtual methods in this class are meant to be overriden to allow
860+ // / external consumers (e.g lldb) to track the ranges of components of the
861+ // / demangled name.
822862class NodePrinter {
823- private :
863+ protected :
824864 DemanglerPrinter Printer;
825865 DemangleOptions Options;
826866 bool SpecializationPrefixPrinted = false ;
@@ -829,17 +869,24 @@ class NodePrinter {
829869public:
830870 NodePrinter (DemangleOptions options) : Options(options) {}
831871
832- std::string printRoot (NodePointer root) {
872+ virtual ~NodePrinter () = default ;
873+
874+ void printRoot (NodePointer root) {
833875 isValid = true ;
834876 print (root, 0 );
877+ }
878+
879+ std::string takeString () {
835880 if (isValid)
836881 return std::move (Printer).str ();
837882 return " " ;
838883 }
839884
840- private :
885+ protected :
841886 static const unsigned MaxDepth = 768 ;
842887
888+ size_t getStreamLength () { return Printer.getStreamLength (); }
889+
843890 // / Called when the node tree in valid.
844891 // /
845892 // / The demangler already catches most error cases and mostly produces valid
@@ -864,13 +911,13 @@ class NodePrinter {
864911 node->getText () == STDLIB_NAME);
865912 }
866913
867- bool printContext (NodePointer Context);
868-
869914 static bool isIdentifier (NodePointer node, StringRef desired) {
870915 return (node->getKind () == Node::Kind::Identifier &&
871916 node->getText () == desired);
872917 }
873918
919+ bool printContext (NodePointer Context);
920+
874921 enum class SugarType {
875922 None,
876923 Optional,
@@ -893,8 +940,9 @@ class NodePrinter {
893940
894941 NodePointer getChildIf (NodePointer Node, Node::Kind Kind);
895942
896- void printFunctionParameters (NodePointer LabelList, NodePointer ParameterType,
897- unsigned depth, bool showTypes);
943+ virtual void printFunctionParameters (NodePointer LabelList,
944+ NodePointer ParameterType,
945+ unsigned depth, bool showTypes);
898946
899947 void printFunctionType (NodePointer LabelList, NodePointer node,
900948 unsigned depth);
@@ -938,6 +986,12 @@ class NodePrinter {
938986 bool hasName, StringRef ExtraName = " " ,
939987 int ExtraIndex = -1 , StringRef OverwriteName = " " );
940988
989+ virtual void printFunctionName (bool hasName, llvm::StringRef &OverwriteName,
990+ llvm::StringRef &ExtraName, bool MultiWordName,
991+ int &ExtraIndex,
992+ swift::Demangle::NodePointer Entity,
993+ unsigned int depth);
994+
941995 // / Print the type of an entity.
942996 // /
943997 // / \param Entity The entity.
0 commit comments