Skip to content

Commit 11212b7

Browse files
move NodePrinter declarations to a header
1 parent 7bb7326 commit 11212b7

File tree

2 files changed

+896
-857
lines changed

2 files changed

+896
-857
lines changed

include/swift/Demangling/Demangle.h

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,11 @@
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"
26+
#include "swift/Strings.h"
2527
#include "llvm/ADT/StringRef.h"
2628
#include "llvm/Support/Compiler.h"
2729

@@ -818,6 +820,141 @@ std::string mangledNameForTypeMetadataAccessor(
818820
llvm::StringRef moduleName, llvm::StringRef typeName, Node::Kind typeKind,
819821
Mangle::ManglingFlavor Flavor = Mangle::ManglingFlavor::Default);
820822

823+
/// Base class for printing a Swift demangled node tree.
824+
///
825+
/// NodePrinter is used to convert demangled Swift symbol nodes into
826+
/// human-readable string representations. It handles formatting, indentation,
827+
/// and Swift-specific syntax.
828+
class NodePrinter {
829+
protected:
830+
DemanglerPrinter Printer;
831+
DemangleOptions Options;
832+
bool SpecializationPrefixPrinted = false;
833+
bool isValid = true;
834+
835+
public:
836+
NodePrinter(DemangleOptions options) : Options(options) {}
837+
838+
std::string printRoot(NodePointer root) {
839+
isValid = true;
840+
print(root, 0);
841+
if (isValid)
842+
return std::move(Printer).str();
843+
return "";
844+
}
845+
846+
protected:
847+
static const unsigned MaxDepth = 768;
848+
849+
/// Called when the node tree in valid.
850+
///
851+
/// The demangler already catches most error cases and mostly produces valid
852+
/// node trees. But some cases are difficult to catch in the demangler and
853+
/// instead the NodePrinter bails.
854+
void setInvalid() { isValid = false; }
855+
856+
void printChildren(Node::iterator begin, Node::iterator end, unsigned depth,
857+
const char *sep = nullptr);
858+
859+
void printChildren(NodePointer Node, unsigned depth,
860+
const char *sep = nullptr);
861+
862+
NodePointer getFirstChildOfKind(NodePointer Node, Node::Kind kind);
863+
864+
void printBoundGenericNoSugar(NodePointer Node, unsigned depth);
865+
866+
void printOptionalIndex(NodePointer node);
867+
868+
static bool isSwiftModule(NodePointer node) {
869+
return (node->getKind() == Node::Kind::Module &&
870+
node->getText() == STDLIB_NAME);
871+
}
872+
873+
static bool isIdentifier(NodePointer node, StringRef desired) {
874+
return (node->getKind() == Node::Kind::Identifier &&
875+
node->getText() == desired);
876+
}
877+
878+
bool printContext(NodePointer Context);
879+
880+
enum class SugarType {
881+
None,
882+
Optional,
883+
ImplicitlyUnwrappedOptional,
884+
Array,
885+
Dictionary
886+
};
887+
888+
enum class TypePrinting { NoType, WithColon, FunctionStyle };
889+
890+
/// Determine whether this is a "simple" type, from the type-simple
891+
/// production.
892+
bool isSimpleType(NodePointer Node);
893+
894+
void printWithParens(NodePointer type, unsigned depth);
895+
896+
SugarType findSugar(NodePointer Node);
897+
898+
void printBoundGeneric(NodePointer Node, unsigned depth);
899+
900+
NodePointer getChildIf(NodePointer Node, Node::Kind Kind);
901+
902+
void printFunctionParameters(NodePointer LabelList, NodePointer ParameterType,
903+
unsigned depth, bool showTypes);
904+
905+
void printFunctionType(NodePointer LabelList, NodePointer node,
906+
unsigned depth);
907+
908+
void printImplFunctionType(NodePointer fn, unsigned depth);
909+
910+
void printGenericSignature(NodePointer Node, unsigned depth);
911+
912+
void printFunctionSigSpecializationParams(NodePointer Node, unsigned depth);
913+
914+
void printSpecializationPrefix(NodePointer node, StringRef Description,
915+
unsigned depth,
916+
StringRef ParamPrefix = StringRef());
917+
918+
/// The main big print function.
919+
NodePointer print(NodePointer Node, unsigned depth,
920+
bool asPrefixContext = false);
921+
922+
NodePointer printAbstractStorage(NodePointer Node, unsigned depth,
923+
bool asPrefixContent, StringRef ExtraName);
924+
925+
/// Utility function to print entities.
926+
///
927+
/// \param Entity The entity node to print
928+
/// \param depth The depth in the print() call tree.
929+
/// \param asPrefixContext Should the entity printed as a context which as a
930+
/// prefix to another entity, e.g. the Abc in Abc.def()
931+
/// \param TypePr How should the type of the entity be printed, if at all.
932+
/// E.g. with a colon for properties or as a function type.
933+
/// \param hasName Does the entity has a name, e.g. a function in contrast to
934+
/// an initializer.
935+
/// \param ExtraName An extra name added to the entity name (if any).
936+
/// \param ExtraIndex An extra index added to the entity name (if any),
937+
/// e.g. closure #1
938+
/// \param OverwriteName If non-empty, print this name instead of the one
939+
/// provided by the node. Gets printed even if hasName is false.
940+
/// \return If a non-null node is returned it's a context which must be
941+
/// printed in postfix-form after the entity: "<entity> in <context>".
942+
NodePointer printEntity(NodePointer Entity, unsigned depth,
943+
bool asPrefixContext, TypePrinting TypePr,
944+
bool hasName, StringRef ExtraName = "",
945+
int ExtraIndex = -1, StringRef OverwriteName = "");
946+
947+
/// Print the type of an entity.
948+
///
949+
/// \param Entity The entity.
950+
/// \param type The type of the entity.
951+
/// \param genericFunctionTypeList If not null, the generic argument types
952+
/// which is printed in the generic signature.
953+
/// \param depth The depth in the print() call tree.
954+
void printEntityType(NodePointer Entity, NodePointer type,
955+
NodePointer genericFunctionTypeList, unsigned depth);
956+
};
957+
821958
SWIFT_END_INLINE_NAMESPACE
822959
} // end namespace Demangle
823960
} // end namespace swift

0 commit comments

Comments
 (0)