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
@@ -235,6 +237,18 @@ class Node {
235
237
public:
236
238
Kind getKind () const { return NodeKind; }
237
239
240
+ bool shouldTrackNameRange () const {
241
+ switch (getKind ()) {
242
+ case Kind::Function:
243
+ case Kind::Constructor:
244
+ case Kind::Allocator:
245
+ case Kind::ExplicitClosure:
246
+ return true ;
247
+ default :
248
+ return false ;
249
+ }
250
+ }
251
+
238
252
bool isSimilarTo (const Node *other) const {
239
253
if (NodeKind != other->NodeKind
240
254
|| NodePayloadKind != other->NodePayloadKind )
@@ -466,16 +480,26 @@ class Context {
466
480
// / The lifetime of the returned node tree ends with the lifetime of the
467
481
// / context or with a call of clear().
468
482
NodePointer demangleTypeAsNode (llvm::StringRef MangledName);
469
-
483
+
470
484
// / Demangle the given symbol and return the readable name.
471
485
// /
472
486
// / \param MangledName The mangled symbol string, which start a mangling
473
487
// / prefix: _T, _T0, $S, _$S.
474
488
// /
475
489
// / \returns The demangled string.
476
- std::string demangleSymbolAsString (
477
- llvm::StringRef MangledName,
478
- const DemangleOptions &Options = DemangleOptions());
490
+ std::string
491
+ demangleSymbolAsString (llvm::StringRef MangledName,
492
+ const DemangleOptions &Options = DemangleOptions());
493
+
494
+ // / Demangle the given symbol and store the result in the `printer`.
495
+ // /
496
+ // / \param MangledName The mangled symbol string, which start a mangling
497
+ // / prefix: _T, _T0, $S, _$S.
498
+ // / \param printer The NodePrinter that will be used to demangle the symbol.
499
+ // /
500
+ // / \returns The demangled string.
501
+ void demangleSymbolAsString (llvm::StringRef MangledName,
502
+ NodePrinter *printer);
479
503
480
504
// / Demangle the given type and return the readable name.
481
505
// /
@@ -534,6 +558,17 @@ std::string
534
558
demangleSymbolAsString (const char *mangledName, size_t mangledNameLength,
535
559
const DemangleOptions &options = DemangleOptions());
536
560
561
+ // / Standalone utility function to demangle the given symbol as string. The
562
+ // / demangled string is stored in the `printer`.
563
+ // /
564
+ // / If performance is an issue when demangling multiple symbols,
565
+ // / `Context::demangleSymbolAsString` should be used instead.
566
+ // / \param mangledName The mangled name string pointer.
567
+ // / \param mangledNameLength The length of the mangledName string.
568
+ // / \param printer The NodePrinter that will be used to demangle the symbol.
569
+ void demangleSymbolAsString (const char *mangledName, size_t mangledNameLength,
570
+ NodePrinter *printer);
571
+
537
572
// / Standalone utility function to demangle the given symbol as string.
538
573
// /
539
574
// / If performance is an issue when demangling multiple symbols,
@@ -546,7 +581,7 @@ demangleSymbolAsString(const std::string &mangledName,
546
581
return demangleSymbolAsString (mangledName.data (), mangledName.size (),
547
582
options);
548
583
}
549
-
584
+
550
585
// / Standalone utility function to demangle the given symbol as string.
551
586
// /
552
587
// / If performance is an issue when demangling multiple symbols,
@@ -560,6 +595,17 @@ demangleSymbolAsString(llvm::StringRef MangledName,
560
595
MangledName.size (), Options);
561
596
}
562
597
598
+ // / Standalone utility function to demangle the given symbol as string. The
599
+ // / result is stored in the `printer`.
600
+ // /
601
+ // / If performance is an issue when demangling multiple symbols,
602
+ // / Context::demangleSymbolAsString should be used instead.
603
+ // / \param MangledName The mangled name string.
604
+ inline void demangleSymbolAsString (llvm::StringRef MangledName,
605
+ NodePrinter *printer) {
606
+ demangleSymbolAsString (MangledName.data (), MangledName.size (), printer);
607
+ }
608
+
563
609
// / Standalone utility function to demangle the given type as string.
564
610
// /
565
611
// / If performance is an issue when demangling multiple symbols,
@@ -726,13 +772,19 @@ ManglingErrorOr<const char *> mangleNodeAsObjcCString(NodePointer node,
726
772
// / \endcode
727
773
// /
728
774
// / \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.
775
+ // / \param Options An object encapsulating options to use to perform this
776
+ // / demangling.
730
777
// /
731
778
// / \returns A string representing the demangled name.
732
- // /
733
779
std::string nodeToString (NodePointer Root,
734
780
const DemangleOptions &Options = DemangleOptions());
735
781
782
+ // / Transform the node structure to a string, which is stored in the `Printer`.
783
+ // /
784
+ // / \param Root A pointer to a parse tree generated by the demangler.
785
+ // / \param Printer A NodePrinter used to pretty print the demangled Node.
786
+ void nodeToString (NodePointer Root, NodePrinter *Printer);
787
+
736
788
// / Transforms a mangled key path accessor thunk helper
737
789
// / into the identfier/subscript that would be used to invoke it in swift code.
738
790
std::string keyPathSourceString (const char *MangledName,
@@ -778,11 +830,14 @@ class DemanglerPrinter {
778
830
779
831
llvm::StringRef getStringRef () const { return Stream; }
780
832
833
+ size_t getStreamLength () { return Stream.length (); }
834
+
781
835
// / Shrinks the buffer.
782
836
void resetSize (size_t toPos) {
783
837
assert (toPos <= Stream.size ());
784
838
Stream.resize (toPos);
785
839
}
840
+
786
841
private:
787
842
std::string Stream;
788
843
};
@@ -819,8 +874,17 @@ std::string mangledNameForTypeMetadataAccessor(
819
874
llvm::StringRef moduleName, llvm::StringRef typeName, Node::Kind typeKind,
820
875
Mangle::ManglingFlavor Flavor = Mangle::ManglingFlavor::Default);
821
876
877
+ // / Base class for printing a Swift demangled node tree.
878
+ // /
879
+ // / NodePrinter is used to convert demangled Swift symbol nodes into
880
+ // / human-readable string representations. It handles formatting, indentation,
881
+ // / and Swift-specific syntax.
882
+ // /
883
+ // / The virtual methods in this class are meant to be overriden to allow
884
+ // / external consumers (e.g lldb) to track the ranges of components of the
885
+ // / demangled name.
822
886
class NodePrinter {
823
- private :
887
+ protected :
824
888
DemanglerPrinter Printer;
825
889
DemangleOptions Options;
826
890
bool SpecializationPrefixPrinted = false ;
@@ -829,17 +893,24 @@ class NodePrinter {
829
893
public:
830
894
NodePrinter (DemangleOptions options) : Options(options) {}
831
895
832
- std::string printRoot (NodePointer root) {
896
+ virtual ~NodePrinter () = default ;
897
+
898
+ void printRoot (NodePointer root) {
833
899
isValid = true ;
834
900
print (root, 0 );
901
+ }
902
+
903
+ std::string takeString () {
835
904
if (isValid)
836
905
return std::move (Printer).str ();
837
906
return " " ;
838
907
}
839
908
840
- private :
909
+ protected :
841
910
static const unsigned MaxDepth = 768 ;
842
911
912
+ size_t getStreamLength () { return Printer.getStreamLength (); }
913
+
843
914
// / Called when the node tree in valid.
844
915
// /
845
916
// / The demangler already catches most error cases and mostly produces valid
@@ -864,13 +935,13 @@ class NodePrinter {
864
935
node->getText () == STDLIB_NAME);
865
936
}
866
937
867
- bool printContext (NodePointer Context);
868
-
869
938
static bool isIdentifier (NodePointer node, StringRef desired) {
870
939
return (node->getKind () == Node::Kind::Identifier &&
871
940
node->getText () == desired);
872
941
}
873
942
943
+ bool printContext (NodePointer Context);
944
+
874
945
enum class SugarType {
875
946
None,
876
947
Optional,
@@ -893,8 +964,9 @@ class NodePrinter {
893
964
894
965
NodePointer getChildIf (NodePointer Node, Node::Kind Kind);
895
966
896
- void printFunctionParameters (NodePointer LabelList, NodePointer ParameterType,
897
- unsigned depth, bool showTypes);
967
+ virtual void printFunctionParameters (NodePointer LabelList,
968
+ NodePointer ParameterType,
969
+ unsigned depth, bool showTypes);
898
970
899
971
void printFunctionType (NodePointer LabelList, NodePointer node,
900
972
unsigned depth);
@@ -938,6 +1010,12 @@ class NodePrinter {
938
1010
bool hasName, StringRef ExtraName = " " ,
939
1011
int ExtraIndex = -1 , StringRef OverwriteName = " " );
940
1012
1013
+ virtual void printFunctionName (bool hasName, llvm::StringRef &OverwriteName,
1014
+ llvm::StringRef &ExtraName, bool MultiWordName,
1015
+ int &ExtraIndex,
1016
+ swift::Demangle::NodePointer Entity,
1017
+ unsigned int depth);
1018
+
941
1019
// / Print the type of an entity.
942
1020
// /
943
1021
// / \param Entity The entity.
0 commit comments