19
19
#ifndef SWIFT_DEMANGLING_DEMANGLE_H
20
20
#define SWIFT_DEMANGLING_DEMANGLE_H
21
21
22
+ #include " swift/Demangling/Demangle.h"
22
23
#include " swift/Demangling/Errors.h"
23
24
#include " swift/Demangling/ManglingFlavor.h"
24
25
#include " swift/Demangling/NamespaceMacros.h"
@@ -100,6 +101,7 @@ struct DemangleOptions {
100
101
101
102
class Node ;
102
103
using NodePointer = Node *;
104
+ class NodePrinter ;
103
105
104
106
enum class FunctionSigSpecializationParamKind : unsigned {
105
107
// Option Flags use bits 0-5. This give us 6 bits implying 64 entries to
@@ -466,16 +468,26 @@ class Context {
466
468
// / The lifetime of the returned node tree ends with the lifetime of the
467
469
// / context or with a call of clear().
468
470
NodePointer demangleTypeAsNode (llvm::StringRef MangledName);
469
-
471
+
470
472
// / Demangle the given symbol and return the readable name.
471
473
// /
472
474
// / \param MangledName The mangled symbol string, which start a mangling
473
475
// / prefix: _T, _T0, $S, _$S.
474
476
// /
475
477
// / \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);
479
491
480
492
// / Demangle the given type and return the readable name.
481
493
// /
@@ -534,6 +546,16 @@ std::string
534
546
demangleSymbolAsString (const char *mangledName, size_t mangledNameLength,
535
547
const DemangleOptions &options = DemangleOptions());
536
548
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
+
537
559
// / Standalone utility function to demangle the given symbol as string.
538
560
// /
539
561
// / If performance is an issue when demangling multiple symbols,
@@ -546,7 +568,7 @@ demangleSymbolAsString(const std::string &mangledName,
546
568
return demangleSymbolAsString (mangledName.data (), mangledName.size (),
547
569
options);
548
570
}
549
-
571
+
550
572
// / Standalone utility function to demangle the given symbol as string.
551
573
// /
552
574
// / If performance is an issue when demangling multiple symbols,
@@ -726,13 +748,19 @@ ManglingErrorOr<const char *> mangleNodeAsObjcCString(NodePointer node,
726
748
// / \endcode
727
749
// /
728
750
// / \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.
730
753
// /
731
754
// / \returns A string representing the demangled name.
732
- // /
733
755
std::string nodeToString (NodePointer Root,
734
756
const DemangleOptions &Options = DemangleOptions());
735
757
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
+
736
764
// / Transforms a mangled key path accessor thunk helper
737
765
// / into the identfier/subscript that would be used to invoke it in swift code.
738
766
std::string keyPathSourceString (const char *MangledName,
@@ -778,11 +806,14 @@ class DemanglerPrinter {
778
806
779
807
llvm::StringRef getStringRef () const { return Stream; }
780
808
809
+ size_t getStreamLength () { return Stream.length (); }
810
+
781
811
// / Shrinks the buffer.
782
812
void resetSize (size_t toPos) {
783
813
assert (toPos <= Stream.size ());
784
814
Stream.resize (toPos);
785
815
}
816
+
786
817
private:
787
818
std::string Stream;
788
819
};
@@ -819,8 +850,17 @@ std::string mangledNameForTypeMetadataAccessor(
819
850
llvm::StringRef moduleName, llvm::StringRef typeName, Node::Kind typeKind,
820
851
Mangle::ManglingFlavor Flavor = Mangle::ManglingFlavor::Default);
821
852
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.
822
862
class NodePrinter {
823
- private :
863
+ protected :
824
864
DemanglerPrinter Printer;
825
865
DemangleOptions Options;
826
866
bool SpecializationPrefixPrinted = false ;
@@ -829,17 +869,24 @@ class NodePrinter {
829
869
public:
830
870
NodePrinter (DemangleOptions options) : Options(options) {}
831
871
832
- std::string printRoot (NodePointer root) {
872
+ virtual ~NodePrinter () = default ;
873
+
874
+ void printRoot (NodePointer root) {
833
875
isValid = true ;
834
876
print (root, 0 );
877
+ }
878
+
879
+ std::string takeString () {
835
880
if (isValid)
836
881
return std::move (Printer).str ();
837
882
return " " ;
838
883
}
839
884
840
- private :
885
+ protected :
841
886
static const unsigned MaxDepth = 768 ;
842
887
888
+ size_t getStreamLength () { return Printer.getStreamLength (); }
889
+
843
890
// / Called when the node tree in valid.
844
891
// /
845
892
// / The demangler already catches most error cases and mostly produces valid
@@ -864,13 +911,13 @@ class NodePrinter {
864
911
node->getText () == STDLIB_NAME);
865
912
}
866
913
867
- bool printContext (NodePointer Context);
868
-
869
914
static bool isIdentifier (NodePointer node, StringRef desired) {
870
915
return (node->getKind () == Node::Kind::Identifier &&
871
916
node->getText () == desired);
872
917
}
873
918
919
+ bool printContext (NodePointer Context);
920
+
874
921
enum class SugarType {
875
922
None,
876
923
Optional,
@@ -893,8 +940,9 @@ class NodePrinter {
893
940
894
941
NodePointer getChildIf (NodePointer Node, Node::Kind Kind);
895
942
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);
898
946
899
947
void printFunctionType (NodePointer LabelList, NodePointer node,
900
948
unsigned depth);
@@ -938,6 +986,12 @@ class NodePrinter {
938
986
bool hasName, StringRef ExtraName = " " ,
939
987
int ExtraIndex = -1 , StringRef OverwriteName = " " );
940
988
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
+
941
995
// / Print the type of an entity.
942
996
// /
943
997
// / \param Entity The entity.
0 commit comments