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,17 @@ 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
+ // / `Context::demangleSymbolAsString` should be used instead.
554
+ // / \param mangledName The mangled name string pointer.
555
+ // / \param mangledNameLength The length of the mangledName string.
556
+ // / \param printer The NodePrinter that will be used to demangle the symbol.
557
+ void demangleSymbolAsString (const char *mangledName, size_t mangledNameLength,
558
+ NodePrinter *printer);
559
+
537
560
// / Standalone utility function to demangle the given symbol as string.
538
561
// /
539
562
// / If performance is an issue when demangling multiple symbols,
@@ -546,7 +569,7 @@ demangleSymbolAsString(const std::string &mangledName,
546
569
return demangleSymbolAsString (mangledName.data (), mangledName.size (),
547
570
options);
548
571
}
549
-
572
+
550
573
// / Standalone utility function to demangle the given symbol as string.
551
574
// /
552
575
// / If performance is an issue when demangling multiple symbols,
@@ -560,6 +583,17 @@ demangleSymbolAsString(llvm::StringRef MangledName,
560
583
MangledName.size (), Options);
561
584
}
562
585
586
+ // / Standalone utility function to demangle the given symbol as string. The
587
+ // / result is stored in the `printer`.
588
+ // /
589
+ // / If performance is an issue when demangling multiple symbols,
590
+ // / Context::demangleSymbolAsString should be used instead.
591
+ // / \param MangledName The mangled name string.
592
+ inline void demangleSymbolAsString (llvm::StringRef MangledName,
593
+ NodePrinter *printer) {
594
+ demangleSymbolAsString (MangledName.data (), MangledName.size (), printer);
595
+ }
596
+
563
597
// / Standalone utility function to demangle the given type as string.
564
598
// /
565
599
// / If performance is an issue when demangling multiple symbols,
@@ -726,13 +760,19 @@ ManglingErrorOr<const char *> mangleNodeAsObjcCString(NodePointer node,
726
760
// / \endcode
727
761
// /
728
762
// / \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.
763
+ // / \param Options An object encapsulating options to use to perform this
764
+ // / demangling.
730
765
// /
731
766
// / \returns A string representing the demangled name.
732
- // /
733
767
std::string nodeToString (NodePointer Root,
734
768
const DemangleOptions &Options = DemangleOptions());
735
769
770
+ // / Transform the node structure to a string, which is stored in the `Printer`.
771
+ // /
772
+ // / \param Root A pointer to a parse tree generated by the demangler.
773
+ // / \param Printer A NodePrinter used to pretty print the demangled Node.
774
+ void nodeToString (NodePointer Root, NodePrinter *Printer);
775
+
736
776
// / Transforms a mangled key path accessor thunk helper
737
777
// / into the identfier/subscript that would be used to invoke it in swift code.
738
778
std::string keyPathSourceString (const char *MangledName,
@@ -778,11 +818,14 @@ class DemanglerPrinter {
778
818
779
819
llvm::StringRef getStringRef () const { return Stream; }
780
820
821
+ size_t getStreamLength () { return Stream.length (); }
822
+
781
823
// / Shrinks the buffer.
782
824
void resetSize (size_t toPos) {
783
825
assert (toPos <= Stream.size ());
784
826
Stream.resize (toPos);
785
827
}
828
+
786
829
private:
787
830
std::string Stream;
788
831
};
@@ -819,8 +862,17 @@ std::string mangledNameForTypeMetadataAccessor(
819
862
llvm::StringRef moduleName, llvm::StringRef typeName, Node::Kind typeKind,
820
863
Mangle::ManglingFlavor Flavor = Mangle::ManglingFlavor::Default);
821
864
865
+ // / Base class for printing a Swift demangled node tree.
866
+ // /
867
+ // / NodePrinter is used to convert demangled Swift symbol nodes into
868
+ // / human-readable string representations. It handles formatting, indentation,
869
+ // / and Swift-specific syntax.
870
+ // /
871
+ // / The virtual methods in this class are meant to be overriden to allow
872
+ // / external consumers (e.g lldb) to track the ranges of components of the
873
+ // / demangled name.
822
874
class NodePrinter {
823
- private :
875
+ protected :
824
876
DemanglerPrinter Printer;
825
877
DemangleOptions Options;
826
878
bool SpecializationPrefixPrinted = false ;
@@ -829,17 +881,24 @@ class NodePrinter {
829
881
public:
830
882
NodePrinter (DemangleOptions options) : Options(options) {}
831
883
832
- std::string printRoot (NodePointer root) {
884
+ virtual ~NodePrinter () = default ;
885
+
886
+ void printRoot (NodePointer root) {
833
887
isValid = true ;
834
888
print (root, 0 );
889
+ }
890
+
891
+ std::string takeString () {
835
892
if (isValid)
836
893
return std::move (Printer).str ();
837
894
return " " ;
838
895
}
839
896
840
- private :
897
+ protected :
841
898
static const unsigned MaxDepth = 768 ;
842
899
900
+ size_t getStreamLength () { return Printer.getStreamLength (); }
901
+
843
902
// / Called when the node tree in valid.
844
903
// /
845
904
// / The demangler already catches most error cases and mostly produces valid
@@ -864,13 +923,13 @@ class NodePrinter {
864
923
node->getText () == STDLIB_NAME);
865
924
}
866
925
867
- bool printContext (NodePointer Context);
868
-
869
926
static bool isIdentifier (NodePointer node, StringRef desired) {
870
927
return (node->getKind () == Node::Kind::Identifier &&
871
928
node->getText () == desired);
872
929
}
873
930
931
+ bool printContext (NodePointer Context);
932
+
874
933
enum class SugarType {
875
934
None,
876
935
Optional,
@@ -893,8 +952,9 @@ class NodePrinter {
893
952
894
953
NodePointer getChildIf (NodePointer Node, Node::Kind Kind);
895
954
896
- void printFunctionParameters (NodePointer LabelList, NodePointer ParameterType,
897
- unsigned depth, bool showTypes);
955
+ virtual void printFunctionParameters (NodePointer LabelList,
956
+ NodePointer ParameterType,
957
+ unsigned depth, bool showTypes);
898
958
899
959
void printFunctionType (NodePointer LabelList, NodePointer node,
900
960
unsigned depth);
@@ -938,6 +998,12 @@ class NodePrinter {
938
998
bool hasName, StringRef ExtraName = " " ,
939
999
int ExtraIndex = -1 , StringRef OverwriteName = " " );
940
1000
1001
+ virtual void printFunctionName (bool hasName, llvm::StringRef &OverwriteName,
1002
+ llvm::StringRef &ExtraName, bool MultiWordName,
1003
+ int &ExtraIndex,
1004
+ swift::Demangle::NodePointer Entity,
1005
+ unsigned int depth);
1006
+
941
1007
// / Print the type of an entity.
942
1008
// /
943
1009
// / \param Entity The entity.
0 commit comments