@@ -101,6 +101,7 @@ struct DemangleOptions {
101
101
102
102
class Node ;
103
103
using NodePointer = Node *;
104
+ class NodePrinter ;
104
105
105
106
enum class FunctionSigSpecializationParamKind : unsigned {
106
107
// Option Flags use bits 0-5. This give us 6 bits implying 64 entries to
@@ -236,6 +237,18 @@ class Node {
236
237
public:
237
238
Kind getKind () const { return NodeKind; }
238
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
+
239
252
bool isSimilarTo (const Node *other) const {
240
253
if (NodeKind != other->NodeKind
241
254
|| NodePayloadKind != other->NodePayloadKind )
@@ -419,6 +432,10 @@ bool isOldFunctionTypeMangling(llvm::StringRef mangledName);
419
432
420
433
class Demangler ;
421
434
435
+ class DemanglerPrinter ;
436
+
437
+ class TrackingDemanglerPrinter ;
438
+
422
439
// / The demangler context.
423
440
// /
424
441
// / It owns the allocated nodes which are created during demangling.
@@ -467,16 +484,26 @@ class Context {
467
484
// / The lifetime of the returned node tree ends with the lifetime of the
468
485
// / context or with a call of clear().
469
486
NodePointer demangleTypeAsNode (llvm::StringRef MangledName);
470
-
487
+
471
488
// / Demangle the given symbol and return the readable name.
472
489
// /
473
490
// / \param MangledName The mangled symbol string, which start a mangling
474
491
// / prefix: _T, _T0, $S, _$S.
475
492
// /
476
493
// / \returns The demangled string.
477
- std::string demangleSymbolAsString (
478
- llvm::StringRef MangledName,
479
- const DemangleOptions &Options = DemangleOptions());
494
+ std::string
495
+ demangleSymbolAsString (llvm::StringRef MangledName,
496
+ const DemangleOptions &Options = DemangleOptions());
497
+
498
+ // / Demangle the given symbol.
499
+ // /
500
+ // / \param MangledName The mangled symbol string, which start a mangling
501
+ // / prefix: _T, _T0, $S, _$S.
502
+ // / \param printer The NodePrinter that will be used to demangle the symbol.
503
+ // /
504
+ // / \returns The demangled string.
505
+ void demangleSymbolAsString (llvm::StringRef MangledName,
506
+ NodePrinter *printer);
480
507
481
508
// / Demangle the given type and return the readable name.
482
509
// /
@@ -535,6 +562,18 @@ std::string
535
562
demangleSymbolAsString (const char *mangledName, size_t mangledNameLength,
536
563
const DemangleOptions &options = DemangleOptions());
537
564
565
+ // / Standalone utility function to demangle the given symbol as string.
566
+ // /
567
+ // / If performance is an issue when demangling multiple symbols,
568
+ // / Context::demangleSymbolAsString should be used instead.
569
+ // / \param mangledName The mangled name string pointer.
570
+ // / \param mangledNameLength The length of the mangledName string.
571
+ // / \param printer The NodePrinter that will be used to demangle the symbol.
572
+ // /
573
+ // / \returns The demangled string.
574
+ void demangleSymbolAsString (const char *mangledName, size_t mangledNameLength,
575
+ NodePrinter *printer);
576
+
538
577
// / Standalone utility function to demangle the given symbol as string.
539
578
// /
540
579
// / If performance is an issue when demangling multiple symbols,
@@ -547,7 +586,7 @@ demangleSymbolAsString(const std::string &mangledName,
547
586
return demangleSymbolAsString (mangledName.data (), mangledName.size (),
548
587
options);
549
588
}
550
-
589
+
551
590
// / Standalone utility function to demangle the given symbol as string.
552
591
// /
553
592
// / If performance is an issue when demangling multiple symbols,
@@ -557,8 +596,20 @@ demangleSymbolAsString(const std::string &mangledName,
557
596
inline std::string
558
597
demangleSymbolAsString (llvm::StringRef MangledName,
559
598
const DemangleOptions &Options = DemangleOptions()) {
560
- return demangleSymbolAsString (MangledName.data (),
561
- MangledName.size (), Options);
599
+ return demangleSymbolAsString (MangledName.data (), MangledName.size (),
600
+ Options);
601
+ }
602
+
603
+ // / Standalone utility function to demangle the given symbol as string.
604
+ // /
605
+ // / If performance is an issue when demangling multiple symbols,
606
+ // / Context::demangleSymbolAsString should be used instead.
607
+ // / \param MangledName The mangled name string.
608
+ // /
609
+ // / \returns The demangled string.
610
+ inline void demangleSymbolAsString (llvm::StringRef MangledName,
611
+ NodePrinter *printer) {
612
+ demangleSymbolAsString (MangledName.data (), MangledName.size (), printer);
562
613
}
563
614
564
615
// / Standalone utility function to demangle the given type as string.
@@ -734,6 +785,15 @@ ManglingErrorOr<const char *> mangleNodeAsObjcCString(NodePointer node,
734
785
std::string nodeToString (NodePointer Root,
735
786
const DemangleOptions &Options = DemangleOptions());
736
787
788
+ // / Transform the node structure to a string.
789
+ // /
790
+ // / \endcode
791
+ // /
792
+ // / \param Root A pointer to a parse tree generated by the demangler.
793
+ // / \param Printer A NodePrinter used to pretty print the demangled Node.
794
+ // /
795
+ void nodeToString (NodePointer Root, NodePrinter *Printer);
796
+
737
797
// / Transforms a mangled key path accessor thunk helper
738
798
// / into the identfier/subscript that would be used to invoke it in swift code.
739
799
std::string keyPathSourceString (const char *MangledName,
@@ -779,11 +839,14 @@ class DemanglerPrinter {
779
839
780
840
llvm::StringRef getStringRef () const { return Stream; }
781
841
842
+ size_t getStreamLength () { return Stream.length (); }
843
+
782
844
// / Shrinks the buffer.
783
845
void resetSize (size_t toPos) {
784
846
assert (toPos <= Stream.size ());
785
847
Stream.resize (toPos);
786
848
}
849
+
787
850
private:
788
851
std::string Stream;
789
852
};
@@ -825,6 +888,10 @@ std::string mangledNameForTypeMetadataAccessor(
825
888
// / NodePrinter is used to convert demangled Swift symbol nodes into
826
889
// / human-readable string representations. It handles formatting, indentation,
827
890
// / and Swift-specific syntax.
891
+ // /
892
+ // / The virtual methods in this class are meant to be overriden to allow
893
+ // / external consumers (e.g lldb) to track the ranges of components of the
894
+ // / demangled name.
828
895
class NodePrinter {
829
896
protected:
830
897
DemanglerPrinter Printer;
@@ -835,9 +902,14 @@ class NodePrinter {
835
902
public:
836
903
NodePrinter (DemangleOptions options) : Options(options) {}
837
904
838
- std::string printRoot (NodePointer root) {
905
+ virtual ~NodePrinter () {}
906
+
907
+ void printRoot (NodePointer root) {
839
908
isValid = true ;
840
909
print (root, 0 );
910
+ }
911
+
912
+ std::string takeString () {
841
913
if (isValid)
842
914
return std::move (Printer).str ();
843
915
return " " ;
@@ -846,6 +918,8 @@ class NodePrinter {
846
918
protected:
847
919
static const unsigned MaxDepth = 768 ;
848
920
921
+ size_t getStreamLength () { return Printer.getStreamLength (); }
922
+
849
923
// / Called when the node tree in valid.
850
924
// /
851
925
// / The demangler already catches most error cases and mostly produces valid
@@ -899,8 +973,9 @@ class NodePrinter {
899
973
900
974
NodePointer getChildIf (NodePointer Node, Node::Kind Kind);
901
975
902
- void printFunctionParameters (NodePointer LabelList, NodePointer ParameterType,
903
- unsigned depth, bool showTypes);
976
+ virtual void printFunctionParameters (NodePointer LabelList,
977
+ NodePointer ParameterType,
978
+ unsigned depth, bool showTypes);
904
979
905
980
void printFunctionType (NodePointer LabelList, NodePointer node,
906
981
unsigned depth);
@@ -944,6 +1019,12 @@ class NodePrinter {
944
1019
bool hasName, StringRef ExtraName = " " ,
945
1020
int ExtraIndex = -1 , StringRef OverwriteName = " " );
946
1021
1022
+ virtual void printFunctionName (bool hasName, llvm::StringRef &OverwriteName,
1023
+ llvm::StringRef &ExtraName, bool MultiWordName,
1024
+ int &ExtraIndex,
1025
+ swift::Demangle::NodePointer Entity,
1026
+ unsigned int depth);
1027
+
947
1028
// / Print the type of an entity.
948
1029
// /
949
1030
// / \param Entity The entity.
0 commit comments