Index: lib/Demangle/ItaniumDemangle.cpp =================================================================== --- lib/Demangle/ItaniumDemangle.cpp +++ lib/Demangle/ItaniumDemangle.cpp @@ -11,10 +11,16 @@ // file does not yet support: // - C++ modules TS +#include "llvm/Demangle/Demangle.h" + #include "Compiler.h" #include "StringView.h" #include "Utility.h" -#include "llvm/Demangle/Demangle.h" + +#ifndef STANDALONE_DEMANGLER +#include "llvm/ADT/FoldingSet.h" +#include "llvm/ADT/StringRef.h" +#endif #include #include @@ -26,6 +32,12 @@ #include namespace { +#ifndef STANDALONE_DEMANGLER + void addString(llvm::FoldingSetNodeID &ID, StringView Str) { + ID.AddString(llvm::StringRef(Str.begin(), Str.size())); + } +#endif + // Base class of all AST nodes. The AST is built by the parser, then is // traversed by the printLeft/Right functions to produce a demangled string. class Node { @@ -100,6 +112,20 @@ : K(K_), RHSComponentCache(RHSComponentCache_), ArrayCache(ArrayCache_), FunctionCache(FunctionCache_) {} +#ifndef STANDALONE_DEMANGLER + /// Compute a FoldingSetNodeID for the node created by invoking the + /// constructor with the given arguments. + static void profileCtor(llvm::FoldingSetNodeID &ID, Kind K, Cache = Cache::No, + Cache = Cache::No, Cache = Cache::No) { + ID.AddInteger(K); + // Don't profile the cached values. They're not part of the value of the + // node for profiling purposes. + } + + /// Compute a FoldingSetNodeID for this node. + virtual void profile(llvm::FoldingSetNodeID &ID) const { profileCtor(ID, K); } +#endif + bool hasRHSComponent(OutputStream &S) const { if (RHSComponentCache != Cache::Unknown) return RHSComponentCache == Cache::Yes; @@ -171,6 +197,14 @@ NodeArray(Node **Elements_, size_t NumElements_) : Elements(Elements_), NumElements(NumElements_) {} +#ifndef STANDALONE_DEMANGLER + void profile(llvm::FoldingSetNodeID &ID) { + ID.AddInteger(size()); + for (Node *N : *this) + ID.AddPointer(N); + } +#endif + bool empty() const { return NumElements == 0; } size_t size() const { return NumElements; } @@ -203,6 +237,17 @@ struct NodeArrayNode : Node { NodeArray Array; NodeArrayNode(NodeArray Array_) : Node(KNodeArrayNode), Array(Array_) {} + +#ifndef STANDALONE_DEMANGLER + static void profileCtor(llvm::FoldingSetNodeID &ID, NodeArray A) { + Node::profileCtor(ID, KNodeArrayNode); + A.profile(ID); + } + void profile(llvm::FoldingSetNodeID &ID) const override { + profileCtor(ID, Array); + } +#endif + void printLeft(OutputStream &S) const override { Array.printWithComma(S); } @@ -213,9 +258,21 @@ const StringView Suffix; public: - DotSuffix(Node *Prefix_, StringView Suffix_) + DotSuffix(const Node *Prefix_, StringView Suffix_) : Node(KDotSuffix), Prefix(Prefix_), Suffix(Suffix_) {} +#ifndef STANDALONE_DEMANGLER + static void profileCtor(llvm::FoldingSetNodeID &ID, const Node *Prefix, + StringView Suffix) { + Node::profileCtor(ID, KDotSuffix); + ID.AddPointer(Prefix); + addString(ID, Suffix); + } + void profile(llvm::FoldingSetNodeID &ID) const override { + profileCtor(ID, Prefix, Suffix); + } +#endif + void printLeft(OutputStream &s) const override { Prefix->print(s); s += " ("; @@ -229,9 +286,21 @@ StringView Ext; public: - VendorExtQualType(Node *Ty_, StringView Ext_) + VendorExtQualType(const Node *Ty_, StringView Ext_) : Node(KVendorExtQualType), Ty(Ty_), Ext(Ext_) {} +#ifndef STANDALONE_DEMANGLER + static void profileCtor(llvm::FoldingSetNodeID &ID, const Node *Ty, + StringView Ext) { + Node::profileCtor(ID, KVendorExtQualType); + ID.AddPointer(Ty); + addString(ID, Ext); + } + void profile(llvm::FoldingSetNodeID &ID) const override { + profileCtor(ID, Ty, Ext); + } +#endif + void printLeft(OutputStream &S) const override { Ty->print(S); S += " "; @@ -271,11 +340,23 @@ } public: - QualType(Node *Child_, Qualifiers Quals_) + QualType(const Node *Child_, Qualifiers Quals_) : Node(KQualType, Child_->RHSComponentCache, Child_->ArrayCache, Child_->FunctionCache), Quals(Quals_), Child(Child_) {} +#ifndef STANDALONE_DEMANGLER + static void profileCtor(llvm::FoldingSetNodeID &ID, const Node *Child, + Qualifiers Quals) { + Node::profileCtor(ID, KQualType); + ID.AddPointer(Child); + ID.AddInteger(Quals); + } + void profile(llvm::FoldingSetNodeID &ID) const override { + profileCtor(ID, Child, Quals); + } +#endif + bool hasRHSComponentSlow(OutputStream &S) const override { return Child->hasRHSComponent(S); } @@ -298,9 +379,19 @@ const Node *Ty; public: - ConversionOperatorType(Node *Ty_) + ConversionOperatorType(const Node *Ty_) : Node(KConversionOperatorType), Ty(Ty_) {} +#ifndef STANDALONE_DEMANGLER + static void profileCtor(llvm::FoldingSetNodeID &ID, const Node *Ty) { + Node::profileCtor(ID, KConversionOperatorType); + ID.AddPointer(Ty); + } + void profile(llvm::FoldingSetNodeID &ID) const override { + profileCtor(ID, Ty); + } +#endif + void printLeft(OutputStream &S) const override { S += "operator "; Ty->print(S); @@ -315,6 +406,18 @@ PostfixQualifiedType(Node *Ty_, StringView Postfix_) : Node(KPostfixQualifiedType), Ty(Ty_), Postfix(Postfix_) {} +#ifndef STANDALONE_DEMANGLER + static void profileCtor(llvm::FoldingSetNodeID &ID, const Node *Ty, + StringView Postfix) { + Node::profileCtor(ID, KPostfixQualifiedType); + ID.AddPointer(Ty); + addString(ID, Postfix); + } + void profile(llvm::FoldingSetNodeID &ID) const override { + profileCtor(ID, Ty, Postfix); + } +#endif + void printLeft(OutputStream &s) const override { Ty->printLeft(s); s += Postfix; @@ -327,6 +430,16 @@ public: NameType(StringView Name_) : Node(KNameType), Name(Name_) {} +#ifndef STANDALONE_DEMANGLER + static void profileCtor(llvm::FoldingSetNodeID &ID, StringView Name) { + Node::profileCtor(ID, KNameType); + addString(ID, Name); + } + void profile(llvm::FoldingSetNodeID &ID) const override { + profileCtor(ID, Name); + } +#endif + StringView getName() const { return Name; } StringView getBaseName() const override { return Name; } @@ -340,6 +453,18 @@ ElaboratedTypeSpefType(StringView Kind_, Node *Child_) : Node(KElaboratedTypeSpefType), Kind(Kind_), Child(Child_) {} +#ifndef STANDALONE_DEMANGLER + static void profileCtor(llvm::FoldingSetNodeID &ID, StringView Kind, + Node *Child) { + Node::profileCtor(ID, KElaboratedTypeSpefType); + addString(ID, Kind); + ID.AddPointer(Child); + } + void profile(llvm::FoldingSetNodeID &ID) const override { + profileCtor(ID, Kind, Child); + } +#endif + void printLeft(OutputStream &S) const override { S += Kind; S += ' '; @@ -356,6 +481,18 @@ Base_->ArrayCache, Base_->FunctionCache), Base(Base_), Tag(Tag_) {} +#ifndef STANDALONE_DEMANGLER + static void profileCtor(llvm::FoldingSetNodeID &ID, Node *Base, + StringView Tag) { + Node::profileCtor(ID, KAbiTagAttr); + ID.AddPointer(Base); + addString(ID, Tag); + } + void profile(llvm::FoldingSetNodeID &ID) const override { + profileCtor(ID, Base, Tag); + } +#endif + void printLeft(OutputStream &S) const override { Base->printLeft(S); S += "[abi:"; @@ -370,6 +507,16 @@ EnableIfAttr(NodeArray Conditions_) : Node(KEnableIfAttr), Conditions(Conditions_) {} +#ifndef STANDALONE_DEMANGLER + static void profileCtor(llvm::FoldingSetNodeID &ID, NodeArray Conditions) { + Node::profileCtor(ID, KEnableIfAttr); + Conditions.profile(ID); + } + void profile(llvm::FoldingSetNodeID &ID) const override { + profileCtor(ID, Conditions); + } +#endif + void printLeft(OutputStream &S) const override { S += " [enable_if:"; Conditions.printWithComma(S); @@ -378,18 +525,30 @@ }; class ObjCProtoName : public Node { - Node *Ty; + const Node *Ty; StringView Protocol; friend class PointerType; public: - ObjCProtoName(Node *Ty_, StringView Protocol_) + ObjCProtoName(const Node *Ty_, StringView Protocol_) : Node(KObjCProtoName), Ty(Ty_), Protocol(Protocol_) {} +#ifndef STANDALONE_DEMANGLER + static void profileCtor(llvm::FoldingSetNodeID &ID, const Node *Ty, + StringView Protocol) { + Node::profileCtor(ID, KObjCProtoName); + ID.AddPointer(Ty); + addString(ID, Protocol); + } + void profile(llvm::FoldingSetNodeID &ID) const override { + profileCtor(ID, Ty, Protocol); + } +#endif + bool isObjCObject() const { return Ty->getKind() == KNameType && - static_cast(Ty)->getName() == "objc_object"; + static_cast(Ty)->getName() == "objc_object"; } void printLeft(OutputStream &S) const override { @@ -404,10 +563,20 @@ const Node *Pointee; public: - PointerType(Node *Pointee_) + PointerType(const Node *Pointee_) : Node(KPointerType, Pointee_->RHSComponentCache), Pointee(Pointee_) {} +#ifndef STANDALONE_DEMANGLER + static void profileCtor(llvm::FoldingSetNodeID &ID, const Node *Pointee) { + Node::profileCtor(ID, KPointerType); + ID.AddPointer(Pointee); + } + void profile(llvm::FoldingSetNodeID &ID) const override { + profileCtor(ID, Pointee); + } +#endif + bool hasRHSComponentSlow(OutputStream &S) const override { return Pointee->hasRHSComponent(S); } @@ -469,10 +638,22 @@ } public: - ReferenceType(Node *Pointee_, ReferenceKind RK_) + ReferenceType(const Node *Pointee_, ReferenceKind RK_) : Node(KReferenceType, Pointee_->RHSComponentCache), Pointee(Pointee_), RK(RK_) {} +#ifndef STANDALONE_DEMANGLER + static void profileCtor(llvm::FoldingSetNodeID &ID, const Node *Pointee, + ReferenceKind RK) { + Node::profileCtor(ID, KReferenceType); + ID.AddPointer(Pointee); + ID.AddInteger((unsigned)RK); + } + void profile(llvm::FoldingSetNodeID &ID) const override { + profileCtor(ID, Pointee, RK); + } +#endif + bool hasRHSComponentSlow(OutputStream &S) const override { return Pointee->hasRHSComponent(S); } @@ -506,10 +687,22 @@ const Node *MemberType; public: - PointerToMemberType(Node *ClassType_, Node *MemberType_) + PointerToMemberType(const Node *ClassType_, const Node *MemberType_) : Node(KPointerToMemberType, MemberType_->RHSComponentCache), ClassType(ClassType_), MemberType(MemberType_) {} +#ifndef STANDALONE_DEMANGLER + static void profileCtor(llvm::FoldingSetNodeID &ID, const Node *ClassType, + const Node *MemberType) { + Node::profileCtor(ID, KPointerToMemberType); + ID.AddPointer(ClassType); + ID.AddPointer(MemberType); + } + void profile(llvm::FoldingSetNodeID &ID) const override { + profileCtor(ID, ClassType, MemberType); + } +#endif + bool hasRHSComponentSlow(OutputStream &S) const override { return MemberType->hasRHSComponent(S); } @@ -551,6 +744,18 @@ : First(static_cast(N)), Second(nullptr) {} NodeOrString() : First(nullptr), Second(nullptr) {} + void profile(llvm::FoldingSetNodeID &ID) { + if (isEmpty()) { + ID.AddInteger(0); + } else if (isNode()) { + ID.AddInteger(1); + ID.AddPointer(asNode()); + } else { + ID.AddInteger(2); + addString(ID, asString()); + } + } + bool isString() const { return Second && First; } bool isNode() const { return First && !Second; } bool isEmpty() const { return !First && !Second; } @@ -568,22 +773,27 @@ }; class ArrayType final : public Node { - Node *Base; + const Node *Base; NodeOrString Dimension; public: - ArrayType(Node *Base_, NodeOrString Dimension_) + ArrayType(const Node *Base_, NodeOrString Dimension_ = NodeOrString()) : Node(KArrayType, /*RHSComponentCache=*/Cache::Yes, /*ArrayCache=*/Cache::Yes), Base(Base_), Dimension(Dimension_) {} - // Incomplete array type. - ArrayType(Node *Base_) - : Node(KArrayType, - /*RHSComponentCache=*/Cache::Yes, - /*ArrayCache=*/Cache::Yes), - Base(Base_) {} +#ifndef STANDALONE_DEMANGLER + static void profileCtor(llvm::FoldingSetNodeID &ID, const Node *Base, + NodeOrString Dimension = NodeOrString()) { + Node::profileCtor(ID, KArrayType); + ID.AddPointer(Base); + Dimension.profile(ID); + } + void profile(llvm::FoldingSetNodeID &ID) const override { + profileCtor(ID, Base, Dimension); + } +#endif bool hasRHSComponentSlow(OutputStream &) const override { return true; } bool hasArraySlow(OutputStream &) const override { return true; } @@ -604,21 +814,37 @@ }; class FunctionType final : public Node { - Node *Ret; + const Node *Ret; NodeArray Params; Qualifiers CVQuals; FunctionRefQual RefQual; - Node *ExceptionSpec; + const Node *ExceptionSpec; public: - FunctionType(Node *Ret_, NodeArray Params_, Qualifiers CVQuals_, - FunctionRefQual RefQual_, Node *ExceptionSpec_) + FunctionType(const Node *Ret_, NodeArray Params_, Qualifiers CVQuals_, + FunctionRefQual RefQual_, const Node *ExceptionSpec_) : Node(KFunctionType, /*RHSComponentCache=*/Cache::Yes, /*ArrayCache=*/Cache::No, /*FunctionCache=*/Cache::Yes), Ret(Ret_), Params(Params_), CVQuals(CVQuals_), RefQual(RefQual_), ExceptionSpec(ExceptionSpec_) {} +#ifndef STANDALONE_DEMANGLER + static void profileCtor(llvm::FoldingSetNodeID &ID, const Node *Ret, + NodeArray Params, Qualifiers CVQuals, + FunctionRefQual RefQual, const Node *ExceptionSpec) { + Node::profileCtor(ID, KFunctionType); + ID.AddPointer(Ret); + Params.profile(ID); + ID.AddInteger((unsigned)CVQuals); + ID.AddInteger((unsigned)RefQual); + ID.AddPointer(ExceptionSpec); + } + void profile(llvm::FoldingSetNodeID &ID) const override { + profileCtor(ID, Ret, Params, CVQuals, RefQual, ExceptionSpec); + } +#endif + bool hasRHSComponentSlow(OutputStream &) const override { return true; } bool hasFunctionSlow(OutputStream &) const override { return true; } @@ -660,9 +886,19 @@ }; class NoexceptSpec : public Node { - Node *E; + const Node *E; public: - NoexceptSpec(Node *E_) : Node(KNoexceptSpec), E(E_) {} + NoexceptSpec(const Node *E_) : Node(KNoexceptSpec), E(E_) {} + +#ifndef STANDALONE_DEMANGLER + static void profileCtor(llvm::FoldingSetNodeID &ID, const Node *E) { + Node::profileCtor(ID, KNoexceptSpec); + ID.AddPointer(E); + } + void profile(llvm::FoldingSetNodeID &ID) const override { + profileCtor(ID, E); + } +#endif void printLeft(OutputStream &S) const override { S += "noexcept("; @@ -677,6 +913,16 @@ DynamicExceptionSpec(NodeArray Types_) : Node(KDynamicExceptionSpec), Types(Types_) {} +#ifndef STANDALONE_DEMANGLER + static void profileCtor(llvm::FoldingSetNodeID &ID, NodeArray Types) { + Node::profileCtor(ID, KDynamicExceptionSpec); + Types.profile(ID); + } + void profile(llvm::FoldingSetNodeID &ID) const override { + profileCtor(ID, Types); + } +#endif + void printLeft(OutputStream &S) const override { S += "throw("; Types.printWithComma(S); @@ -685,31 +931,49 @@ }; class FunctionEncoding final : public Node { - Node *Ret; - Node *Name; + const Node *Ret; + const Node *Name; NodeArray Params; - Node *Attrs; + const Node *Attrs; Qualifiers CVQuals; FunctionRefQual RefQual; public: - FunctionEncoding(Node *Ret_, Node *Name_, NodeArray Params_, - Node *Attrs_, Qualifiers CVQuals_, FunctionRefQual RefQual_) + FunctionEncoding(const Node *Ret_, const Node *Name_, NodeArray Params_, + const Node *Attrs_, Qualifiers CVQuals_, + FunctionRefQual RefQual_) : Node(KFunctionEncoding, /*RHSComponentCache=*/Cache::Yes, /*ArrayCache=*/Cache::No, /*FunctionCache=*/Cache::Yes), Ret(Ret_), Name(Name_), Params(Params_), Attrs(Attrs_), CVQuals(CVQuals_), RefQual(RefQual_) {} +#ifndef STANDALONE_DEMANGLER + static void profileCtor(llvm::FoldingSetNodeID &ID, const Node *Ret, + const Node *Name, NodeArray Params, const Node *Attrs, + Qualifiers CVQuals, FunctionRefQual RefQual) { + Node::profileCtor(ID, KFunctionEncoding); + ID.AddPointer(Ret); + ID.AddPointer(Name); + Params.profile(ID); + ID.AddPointer(Attrs); + ID.AddInteger((unsigned)CVQuals); + ID.AddInteger((unsigned)RefQual); + } + void profile(llvm::FoldingSetNodeID &ID) const override { + profileCtor(ID, Ret, Name, Params, Attrs, CVQuals, RefQual); + } +#endif + Qualifiers getCVQuals() const { return CVQuals; } FunctionRefQual getRefQual() const { return RefQual; } NodeArray getParams() const { return Params; } - Node *getReturnType() const { return Ret; } + const Node *getReturnType() const { return Ret; } bool hasRHSComponentSlow(OutputStream &) const override { return true; } bool hasFunctionSlow(OutputStream &) const override { return true; } - Node *getName() { return const_cast(Name); } + const Node *getName() const { return Name; } void printLeft(OutputStream &S) const override { if (Ret) { @@ -748,7 +1012,18 @@ const Node *OpName; public: - LiteralOperator(Node *OpName_) : Node(KLiteralOperator), OpName(OpName_) {} + LiteralOperator(const Node *OpName_) + : Node(KLiteralOperator), OpName(OpName_) {} + +#ifndef STANDALONE_DEMANGLER + static void profileCtor(llvm::FoldingSetNodeID &ID, const Node *OpName) { + Node::profileCtor(ID, KLiteralOperator); + ID.AddPointer(OpName); + } + void profile(llvm::FoldingSetNodeID &ID) const override { + profileCtor(ID, OpName); + } +#endif void printLeft(OutputStream &S) const override { S += "operator\"\" "; @@ -761,9 +1036,21 @@ const Node *Child; public: - SpecialName(StringView Special_, Node* Child_) + SpecialName(StringView Special_, const Node *Child_) : Node(KSpecialName), Special(Special_), Child(Child_) {} +#ifndef STANDALONE_DEMANGLER + static void profileCtor(llvm::FoldingSetNodeID &ID, StringView Special, + const Node *Child) { + Node::profileCtor(ID, KSpecialName); + addString(ID, Special); + ID.AddPointer(Child); + } + void profile(llvm::FoldingSetNodeID &ID) const override { + profileCtor(ID, Special, Child); + } +#endif + void printLeft(OutputStream &S) const override { S += Special; Child->print(S); @@ -775,10 +1062,22 @@ const Node *SecondType; public: - CtorVtableSpecialName(Node *FirstType_, Node *SecondType_) + CtorVtableSpecialName(const Node *FirstType_, const Node *SecondType_) : Node(KCtorVtableSpecialName), FirstType(FirstType_), SecondType(SecondType_) {} +#ifndef STANDALONE_DEMANGLER + static void profileCtor(llvm::FoldingSetNodeID &ID, const Node *FirstType, + const Node *SecondType) { + Node::profileCtor(ID, KCtorVtableSpecialName); + ID.AddPointer(FirstType); + ID.AddPointer(SecondType); + } + void profile(llvm::FoldingSetNodeID &ID) const override { + profileCtor(ID, FirstType, SecondType); + } +#endif + void printLeft(OutputStream &S) const override { S += "construction vtable for "; FirstType->print(S); @@ -794,6 +1093,18 @@ NestedName(Node *Qual_, Node *Name_) : Node(KNestedName), Qual(Qual_), Name(Name_) {} +#ifndef STANDALONE_DEMANGLER + static void profileCtor(llvm::FoldingSetNodeID &ID, Node *Qual, + Node *Name) { + Node::profileCtor(ID, KNestedName); + ID.AddPointer(Qual); + ID.AddPointer(Name); + } + void profile(llvm::FoldingSetNodeID &ID) const override { + profileCtor(ID, Qual, Name); + } +#endif + StringView getBaseName() const override { return Name->getBaseName(); } void printLeft(OutputStream &S) const override { @@ -810,6 +1121,18 @@ LocalName(Node *Encoding_, Node *Entity_) : Node(KLocalName), Encoding(Encoding_), Entity(Entity_) {} +#ifndef STANDALONE_DEMANGLER + static void profileCtor(llvm::FoldingSetNodeID &ID, Node *Encoding, + Node *Entity) { + Node::profileCtor(ID, KLocalName); + ID.AddPointer(Encoding); + ID.AddPointer(Entity); + } + void profile(llvm::FoldingSetNodeID &ID) const override { + profileCtor(ID, Encoding, Entity); + } +#endif + void printLeft(OutputStream &S) const override { Encoding->print(S); S += "::"; @@ -823,9 +1146,21 @@ const Node *Name; public: - QualifiedName(Node* Qualifier_, Node* Name_) + QualifiedName(const Node *Qualifier_, const Node *Name_) : Node(KQualifiedName), Qualifier(Qualifier_), Name(Name_) {} +#ifndef STANDALONE_DEMANGLER + static void profileCtor(llvm::FoldingSetNodeID &ID, const Node *Qualifier, + const Node *Name) { + Node::profileCtor(ID, KQualifiedName); + ID.AddPointer(Qualifier); + ID.AddPointer(Name); + } + void profile(llvm::FoldingSetNodeID &ID) const override { + profileCtor(ID, Qualifier, Name); + } +#endif + StringView getBaseName() const override { return Name->getBaseName(); } void printLeft(OutputStream &S) const override { @@ -838,16 +1173,38 @@ class VectorType final : public Node { const Node *BaseType; const NodeOrString Dimension; + // FIXME: Is this just the same as '!BaseType'? const bool IsPixel; public: VectorType(NodeOrString Dimension_) : Node(KVectorType), BaseType(nullptr), Dimension(Dimension_), IsPixel(true) {} - VectorType(Node *BaseType_, NodeOrString Dimension_) + VectorType(const Node *BaseType_, NodeOrString Dimension_) : Node(KVectorType), BaseType(BaseType_), Dimension(Dimension_), IsPixel(false) {} +#ifndef STANDALONE_DEMANGLER + static void profileCtor(llvm::FoldingSetNodeID &ID, NodeOrString Dimension) { + Node::profileCtor(ID, KVectorType); + ID.AddBoolean(true); // IsPixel + Dimension.profile(ID); + } + static void profileCtor(llvm::FoldingSetNodeID &ID, const Node *BaseType, + NodeOrString Dimension) { + Node::profileCtor(ID, KVectorType); + ID.AddBoolean(false); // IsPixel + ID.AddPointer(BaseType); + Dimension.profile(ID); + } + void profile(llvm::FoldingSetNodeID &ID) const override { + if (IsPixel) + profileCtor(ID, Dimension); + else + profileCtor(ID, BaseType, Dimension); + } +#endif + void printLeft(OutputStream &S) const override { if (IsPixel) { S += "pixel vector["; @@ -901,6 +1258,16 @@ RHSComponentCache = Cache::No; } +#ifndef STANDALONE_DEMANGLER + static void profileCtor(llvm::FoldingSetNodeID &ID, NodeArray Data) { + Node::profileCtor(ID, KParameterPack); + Data.profile(ID); + } + void profile(llvm::FoldingSetNodeID &ID) const override { + profileCtor(ID, Data); + } +#endif + bool hasRHSComponentSlow(OutputStream &S) const override { initializePackExpansion(S); size_t Idx = S.CurrentPackIndex; @@ -947,6 +1314,16 @@ TemplateArgumentPack(NodeArray Elements_) : Node(KTemplateArgumentPack), Elements(Elements_) {} +#ifndef STANDALONE_DEMANGLER + static void profileCtor(llvm::FoldingSetNodeID &ID, NodeArray Elements) { + Node::profileCtor(ID, KTemplateArgumentPack); + Elements.profile(ID); + } + void profile(llvm::FoldingSetNodeID &ID) const override { + profileCtor(ID, Elements); + } +#endif + NodeArray getElements() const { return Elements; } void printLeft(OutputStream &S) const override { @@ -960,9 +1337,19 @@ const Node *Child; public: - ParameterPackExpansion(Node* Child_) + ParameterPackExpansion(const Node *Child_) : Node(KParameterPackExpansion), Child(Child_) {} +#ifndef STANDALONE_DEMANGLER + static void profileCtor(llvm::FoldingSetNodeID &ID, const Node *Child) { + Node::profileCtor(ID, KParameterPackExpansion); + ID.AddPointer(Child); + } + void profile(llvm::FoldingSetNodeID &ID) const override { + profileCtor(ID, Child); + } +#endif + const Node *getChild() const { return Child; } void printLeft(OutputStream &S) const override { @@ -1004,6 +1391,16 @@ public: TemplateArgs(NodeArray Params_) : Node(KTemplateArgs), Params(Params_) {} +#ifndef STANDALONE_DEMANGLER + static void profileCtor(llvm::FoldingSetNodeID &ID, NodeArray Params) { + Node::profileCtor(ID, KTemplateArgs); + Params.profile(ID); + } + void profile(llvm::FoldingSetNodeID &ID) const override { + profileCtor(ID, Params); + } +#endif + NodeArray getParams() { return Params; } void printLeft(OutputStream &S) const override { @@ -1030,6 +1427,16 @@ Cache::Unknown), Index(Index_) {} + // We don't canonicalize these, because the index would mean different things + // in different mangled names, and we can't rely on the Ref being known in + // time. +#ifndef STANDALONE_DEMANGLER + static void profileCtor(llvm::FoldingSetNodeID &ID, size_t Index) = delete; + void profile(llvm::FoldingSetNodeID &ID) const override { + llvm_unreachable("forward template reference should not be canonicalized"); + } +#endif + bool hasRHSComponentSlow(OutputStream &S) const override { if (Printing) return false; @@ -1077,6 +1484,18 @@ NameWithTemplateArgs(Node *Name_, Node *TemplateArgs_) : Node(KNameWithTemplateArgs), Name(Name_), TemplateArgs(TemplateArgs_) {} +#ifndef STANDALONE_DEMANGLER + static void profileCtor(llvm::FoldingSetNodeID &ID, Node *Name, + Node *TemplateArgs) { + Node::profileCtor(ID, KNameWithTemplateArgs); + ID.AddPointer(Name); + ID.AddPointer(TemplateArgs); + } + void profile(llvm::FoldingSetNodeID &ID) const override { + profileCtor(ID, Name, TemplateArgs); + } +#endif + StringView getBaseName() const override { return Name->getBaseName(); } void printLeft(OutputStream &S) const override { @@ -1092,6 +1511,16 @@ GlobalQualifiedName(Node* Child_) : Node(KGlobalQualifiedName), Child(Child_) {} +#ifndef STANDALONE_DEMANGLER + static void profileCtor(llvm::FoldingSetNodeID &ID, Node *Child) { + Node::profileCtor(ID, KGlobalQualifiedName); + ID.AddPointer(Child); + } + void profile(llvm::FoldingSetNodeID &ID) const override { + profileCtor(ID, Child); + } +#endif + StringView getBaseName() const override { return Child->getBaseName(); } void printLeft(OutputStream &S) const override { @@ -1105,6 +1534,16 @@ StdQualifiedName(Node *Child_) : Node(KStdQualifiedName), Child(Child_) {} +#ifndef STANDALONE_DEMANGLER + static void profileCtor(llvm::FoldingSetNodeID &ID, Node *Child) { + Node::profileCtor(ID, KStdQualifiedName); + ID.AddPointer(Child); + } + void profile(llvm::FoldingSetNodeID &ID) const override { + profileCtor(ID, Child); + } +#endif + StringView getBaseName() const override { return Child->getBaseName(); } void printLeft(OutputStream &S) const override { @@ -1129,6 +1568,16 @@ ExpandedSpecialSubstitution(SpecialSubKind SSK_) : Node(KExpandedSpecialSubstitution), SSK(SSK_) {} +#ifndef STANDALONE_DEMANGLER + static void profileCtor(llvm::FoldingSetNodeID &ID, SpecialSubKind SSK) { + Node::profileCtor(ID, KExpandedSpecialSubstitution); + ID.AddInteger((unsigned)SSK); + } + void profile(llvm::FoldingSetNodeID &ID) const override { + profileCtor(ID, SSK); + } +#endif + StringView getBaseName() const override { switch (SSK) { case SpecialSubKind::allocator: @@ -1178,6 +1627,16 @@ SpecialSubstitution(SpecialSubKind SSK_) : Node(KSpecialSubstitution), SSK(SSK_) {} +#ifndef STANDALONE_DEMANGLER + static void profileCtor(llvm::FoldingSetNodeID &ID, SpecialSubKind SSK) { + Node::profileCtor(ID, KSpecialSubstitution); + ID.AddInteger((unsigned)SSK); + } + void profile(llvm::FoldingSetNodeID &ID) const override { + profileCtor(ID, SSK); + } +#endif + StringView getBaseName() const override { switch (SSK) { case SpecialSubKind::allocator: @@ -1225,9 +1684,21 @@ const bool IsDtor; public: - CtorDtorName(Node *Basename_, bool IsDtor_) + CtorDtorName(const Node *Basename_, bool IsDtor_) : Node(KCtorDtorName), Basename(Basename_), IsDtor(IsDtor_) {} +#ifndef STANDALONE_DEMANGLER + static void profileCtor(llvm::FoldingSetNodeID &ID, const Node *Basename, + bool IsDtor) { + Node::profileCtor(ID, KCtorDtorName); + ID.AddPointer(Basename); + ID.AddBoolean(IsDtor); + } + void profile(llvm::FoldingSetNodeID &ID) const override { + profileCtor(ID, Basename, IsDtor); + } +#endif + void printLeft(OutputStream &S) const override { if (IsDtor) S += "~"; @@ -1239,7 +1710,17 @@ const Node *Base; public: - DtorName(Node *Base_) : Node(KDtorName), Base(Base_) {} + DtorName(const Node *Base_) : Node(KDtorName), Base(Base_) {} + +#ifndef STANDALONE_DEMANGLER + static void profileCtor(llvm::FoldingSetNodeID &ID, const Node *Base) { + Node::profileCtor(ID, KDtorName); + ID.AddPointer(Base); + } + void profile(llvm::FoldingSetNodeID &ID) const override { + profileCtor(ID, Base); + } +#endif void printLeft(OutputStream &S) const override { S += "~"; @@ -1253,6 +1734,16 @@ public: UnnamedTypeName(StringView Count_) : Node(KUnnamedTypeName), Count(Count_) {} +#ifndef STANDALONE_DEMANGLER + static void profileCtor(llvm::FoldingSetNodeID &ID, StringView Count) { + Node::profileCtor(ID, KUnnamedTypeName); + addString(ID, Count); + } + void profile(llvm::FoldingSetNodeID &ID) const override { + profileCtor(ID, Count); + } +#endif + void printLeft(OutputStream &S) const override { S += "'unnamed"; S += Count; @@ -1268,6 +1759,18 @@ ClosureTypeName(NodeArray Params_, StringView Count_) : Node(KClosureTypeName), Params(Params_), Count(Count_) {} +#ifndef STANDALONE_DEMANGLER + static void profileCtor(llvm::FoldingSetNodeID &ID, NodeArray Params, + StringView Count) { + Node::profileCtor(ID, KClosureTypeName); + Params.profile(ID); + addString(ID, Count); + } + void profile(llvm::FoldingSetNodeID &ID) const override { + profileCtor(ID, Params, Count); + } +#endif + void printLeft(OutputStream &S) const override { S += "\'lambda"; S += Count; @@ -1283,6 +1786,16 @@ StructuredBindingName(NodeArray Bindings_) : Node(KStructuredBindingName), Bindings(Bindings_) {} +#ifndef STANDALONE_DEMANGLER + static void profileCtor(llvm::FoldingSetNodeID &ID, NodeArray Bindings) { + Node::profileCtor(ID, KStructuredBindingName); + Bindings.profile(ID); + } + void profile(llvm::FoldingSetNodeID &ID) const override { + profileCtor(ID, Bindings); + } +#endif + void printLeft(OutputStream &S) const override { S += '['; Bindings.printWithComma(S); @@ -1293,8 +1806,42 @@ // -- Expression Nodes -- struct Expr : public Node { + /// Expr kinds, used for profiling. + enum ExprKind : unsigned char { + KBinaryExpr, + KArraySubscriptExpr, + KPostfixExpr, + KConditionalExpr, + KMemberExpr, + KEnclosingExpr, + KCastExpr, + KSizeofParamPackExpr, + KCallExpr, + KNewExpr, + KDeleteExpr, + KPrefixExpr, + KFunctionParam, + KConversionExpr, + KInitListExpr, + KFoldExpr, + KThrowExpr, + KBoolExpr, + KIntegerCastExpr, + KIntegerExpr, + KFloatExpr, + }; + Expr(Kind K = KExpr) : Node(K) {} + +#ifndef STANDALONE_DEMANGLER + static void profileCtor(llvm::FoldingSetNodeID &ID, Kind K = KExpr) { + Node::profileCtor(ID, K); + } + void profile(llvm::FoldingSetNodeID &ID) const override { + profileCtor(ID, K); + } }; +#endif class BinaryExpr : public Expr { const Node *LHS; @@ -1302,9 +1849,23 @@ const Node *RHS; public: - BinaryExpr(Node *LHS_, StringView InfixOperator_, Node *RHS_) + BinaryExpr(const Node *LHS_, StringView InfixOperator_, const Node *RHS_) : LHS(LHS_), InfixOperator(InfixOperator_), RHS(RHS_) {} +#ifndef STANDALONE_DEMANGLER + static void profileCtor(llvm::FoldingSetNodeID &ID, const Node *LHS, + StringView InfixOperator, const Node *RHS) { + Expr::profileCtor(ID); + ID.AddInteger(KBinaryExpr); + ID.AddPointer(LHS); + addString(ID, InfixOperator); + ID.AddPointer(RHS); + } + void profile(llvm::FoldingSetNodeID &ID) const override { + profileCtor(ID, LHS, InfixOperator, RHS); + } +#endif + void printLeft(OutputStream &S) const override { // might be a template argument expression, then we need to disambiguate // with parens. @@ -1329,7 +1890,21 @@ const Node *Op2; public: - ArraySubscriptExpr(Node *Op1_, Node *Op2_) : Op1(Op1_), Op2(Op2_) {} + ArraySubscriptExpr(const Node *Op1_, const Node *Op2_) + : Op1(Op1_), Op2(Op2_) {} + +#ifndef STANDALONE_DEMANGLER + static void profileCtor(llvm::FoldingSetNodeID &ID, const Node *Op1, + const Node *Op2) { + Expr::profileCtor(ID); + ID.AddInteger(KArraySubscriptExpr); + ID.AddPointer(Op1); + ID.AddPointer(Op2); + } + void profile(llvm::FoldingSetNodeID &ID) const override { + profileCtor(ID, Op1, Op2); + } +#endif void printLeft(OutputStream &S) const override { S += "("; @@ -1342,17 +1917,30 @@ class PostfixExpr : public Expr { const Node *Child; - const StringView Operand; + const StringView Operator; public: - PostfixExpr(Node *Child_, StringView Operand_) - : Child(Child_), Operand(Operand_) {} + PostfixExpr(const Node *Child_, StringView Operator_) + : Child(Child_), Operator(Operator_) {} + +#ifndef STANDALONE_DEMANGLER + static void profileCtor(llvm::FoldingSetNodeID &ID, const Node *Child, + StringView Operator) { + Expr::profileCtor(ID); + ID.AddInteger(KPostfixExpr); + ID.AddPointer(Child); + addString(ID, Operator); + } + void profile(llvm::FoldingSetNodeID &ID) const override { + profileCtor(ID, Child, Operator); + } +#endif void printLeft(OutputStream &S) const override { S += "("; Child->print(S); S += ")"; - S += Operand; + S += Operator; } }; @@ -1362,9 +1950,23 @@ const Node *Else; public: - ConditionalExpr(Node *Cond_, Node *Then_, Node *Else_) + ConditionalExpr(const Node *Cond_, const Node *Then_, const Node *Else_) : Cond(Cond_), Then(Then_), Else(Else_) {} +#ifndef STANDALONE_DEMANGLER + static void profileCtor(llvm::FoldingSetNodeID &ID, const Node *Cond, + const Node *Then, const Node *Else) { + Expr::profileCtor(ID); + ID.AddInteger(KConditionalExpr); + ID.AddPointer(Cond); + ID.AddPointer(Then); + ID.AddPointer(Else); + } + void profile(llvm::FoldingSetNodeID &ID) const override { + profileCtor(ID, Cond, Then, Else); + } +#endif + void printLeft(OutputStream &S) const override { S += "("; Cond->print(S); @@ -1382,9 +1984,23 @@ const Node *RHS; public: - MemberExpr(Node *LHS_, StringView Kind_, Node *RHS_) + MemberExpr(const Node *LHS_, StringView Kind_, const Node *RHS_) : LHS(LHS_), Kind(Kind_), RHS(RHS_) {} +#ifndef STANDALONE_DEMANGLER + static void profileCtor(llvm::FoldingSetNodeID &ID, const Node *LHS, + StringView Kind, const Node *RHS) { + Expr::profileCtor(ID); + ID.AddInteger(KMemberExpr); + ID.AddPointer(LHS); + addString(ID, Kind); + ID.AddPointer(RHS); + } + void profile(llvm::FoldingSetNodeID &ID) const override { + profileCtor(ID, LHS, Kind, RHS); + } +#endif + void printLeft(OutputStream &S) const override { LHS->print(S); S += Kind; @@ -1401,6 +2017,20 @@ EnclosingExpr(StringView Prefix_, Node *Infix_, StringView Postfix_) : Prefix(Prefix_), Infix(Infix_), Postfix(Postfix_) {} +#ifndef STANDALONE_DEMANGLER + static void profileCtor(llvm::FoldingSetNodeID &ID, StringView Prefix, + const Node *Infix, StringView Postfix) { + Expr::profileCtor(ID); + ID.AddInteger(KEnclosingExpr); + addString(ID, Prefix); + ID.AddPointer(Infix); + addString(ID, Postfix); + } + void profile(llvm::FoldingSetNodeID &ID) const override { + profileCtor(ID, Prefix, Infix, Postfix); + } +#endif + void printLeft(OutputStream &S) const override { S += Prefix; Infix->print(S); @@ -1415,9 +2045,23 @@ const Node *From; public: - CastExpr(StringView CastKind_, Node *To_, Node *From_) + CastExpr(StringView CastKind_, const Node *To_, const Node *From_) : CastKind(CastKind_), To(To_), From(From_) {} +#ifndef STANDALONE_DEMANGLER + static void profileCtor(llvm::FoldingSetNodeID &ID, StringView CastKind, + const Node *To, const Node *From) { + Expr::profileCtor(ID); + ID.AddInteger(KCastExpr); + addString(ID, CastKind); + ID.AddPointer(To); + ID.AddPointer(From); + } + void profile(llvm::FoldingSetNodeID &ID) const override { + profileCtor(ID, CastKind, To, From); + } +#endif + void printLeft(OutputStream &S) const override { S += CastKind; S += "<"; @@ -1429,10 +2073,21 @@ }; class SizeofParamPackExpr : public Expr { - Node *Pack; + const Node *Pack; public: - SizeofParamPackExpr(Node *Pack_) : Pack(Pack_) {} + SizeofParamPackExpr(const Node *Pack_) : Pack(Pack_) {} + +#ifndef STANDALONE_DEMANGLER + static void profileCtor(llvm::FoldingSetNodeID &ID, const Node *Pack) { + Expr::profileCtor(ID); + ID.AddInteger(KSizeofParamPackExpr); + ID.AddPointer(Pack); + } + void profile(llvm::FoldingSetNodeID &ID) const override { + profileCtor(ID, Pack); + } +#endif void printLeft(OutputStream &S) const override { S += "sizeof...("; @@ -1447,7 +2102,21 @@ NodeArray Args; public: - CallExpr(Node *Callee_, NodeArray Args_) : Callee(Callee_), Args(Args_) {} + CallExpr(const Node *Callee_, NodeArray Args_) + : Callee(Callee_), Args(Args_) {} + +#ifndef STANDALONE_DEMANGLER + static void profileCtor(llvm::FoldingSetNodeID &ID, const Node *Callee, + NodeArray Args) { + Expr::profileCtor(ID); + ID.AddInteger(KCallExpr); + ID.AddPointer(Callee); + Args.profile(ID); + } + void profile(llvm::FoldingSetNodeID &ID) const override { + profileCtor(ID, Callee, Args); + } +#endif void printLeft(OutputStream &S) const override { Callee->print(S); @@ -1470,6 +2139,23 @@ : ExprList(ExprList_), Type(Type_), InitList(InitList_), IsGlobal(IsGlobal_), IsArray(IsArray_) {} +#ifndef STANDALONE_DEMANGLER + static void profileCtor(llvm::FoldingSetNodeID &ID, NodeArray ExprList, + Node *Type, NodeArray InitList, bool IsGlobal, + bool IsArray) { + Expr::profileCtor(ID); + ID.AddInteger(KNewExpr); + ExprList.profile(ID); + ID.AddPointer(Type); + InitList.profile(ID); + ID.AddBoolean(IsGlobal); + ID.AddBoolean(IsArray); + } + void profile(llvm::FoldingSetNodeID &ID) const override { + profileCtor(ID, ExprList, Type, InitList, IsGlobal, IsArray); + } +#endif + void printLeft(OutputStream &S) const override { if (IsGlobal) S += "::operator "; @@ -1501,6 +2187,20 @@ DeleteExpr(Node *Op_, bool IsGlobal_, bool IsArray_) : Op(Op_), IsGlobal(IsGlobal_), IsArray(IsArray_) {} +#ifndef STANDALONE_DEMANGLER + static void profileCtor(llvm::FoldingSetNodeID &ID, Node *Op, bool IsGlobal, + bool IsArray) { + Expr::profileCtor(ID); + ID.AddInteger(KDeleteExpr); + ID.AddPointer(Op); + ID.AddBoolean(IsGlobal); + ID.AddBoolean(IsArray); + } + void profile(llvm::FoldingSetNodeID &ID) const override { + profileCtor(ID, Op, IsGlobal, IsArray); + } +#endif + void printLeft(OutputStream &S) const override { if (IsGlobal) S += "::"; @@ -1516,7 +2216,21 @@ Node *Child; public: - PrefixExpr(StringView Prefix_, Node *Child_) : Prefix(Prefix_), Child(Child_) {} + PrefixExpr(StringView Prefix_, Node *Child_) + : Prefix(Prefix_), Child(Child_) {} + +#ifndef STANDALONE_DEMANGLER + static void profileCtor(llvm::FoldingSetNodeID &ID, StringView Prefix, + Node *Child) { + Expr::profileCtor(ID); + ID.AddInteger(KPrefixExpr); + addString(ID, Prefix); + ID.AddPointer(Child); + } + void profile(llvm::FoldingSetNodeID &ID) const override { + profileCtor(ID, Prefix, Child); + } +#endif void printLeft(OutputStream &S) const override { S += Prefix; @@ -1532,6 +2246,17 @@ public: FunctionParam(StringView Number_) : Number(Number_) {} +#ifndef STANDALONE_DEMANGLER + static void profileCtor(llvm::FoldingSetNodeID &ID, StringView Number) { + Expr::profileCtor(ID); + ID.AddInteger(KFunctionParam); + addString(ID, Number); + } + void profile(llvm::FoldingSetNodeID &ID) const override { + profileCtor(ID, Number); + } +#endif + void printLeft(OutputStream &S) const override { S += "fp"; S += Number; @@ -1546,6 +2271,19 @@ ConversionExpr(const Node *Type_, NodeArray Expressions_) : Type(Type_), Expressions(Expressions_) {} +#ifndef STANDALONE_DEMANGLER + static void profileCtor(llvm::FoldingSetNodeID &ID, const Node *Type, + NodeArray Expressions) { + Expr::profileCtor(ID); + ID.AddInteger(KConversionExpr); + ID.AddPointer(Type); + Expressions.profile(ID); + } + void profile(llvm::FoldingSetNodeID &ID) const override { + profileCtor(ID, Type, Expressions); + } +#endif + void printLeft(OutputStream &S) const override { S += "("; Type->print(S); @@ -1556,10 +2294,23 @@ }; class InitListExpr : public Expr { - Node *Ty; + const Node *Ty; NodeArray Inits; public: - InitListExpr(Node *Ty_, NodeArray Inits_) : Ty(Ty_), Inits(Inits_) {} + InitListExpr(const Node *Ty_, NodeArray Inits_) : Ty(Ty_), Inits(Inits_) {} + +#ifndef STANDALONE_DEMANGLER + static void profileCtor(llvm::FoldingSetNodeID &ID, const Node *Type, + NodeArray Inits) { + Expr::profileCtor(ID); + ID.AddInteger(KInitListExpr); + ID.AddPointer(Type); + Inits.profile(ID); + } + void profile(llvm::FoldingSetNodeID &ID) const override { + profileCtor(ID, Ty, Inits); + } +#endif void printLeft(OutputStream &S) const override { if (Ty) @@ -1571,13 +2322,26 @@ }; class BracedExpr : public Expr { - Node *Elem; - Node *Init; + const Node *Elem; + const Node *Init; bool IsArray; public: - BracedExpr(Node *Elem_, Node *Init_, bool IsArray_) + BracedExpr(const Node *Elem_, const Node *Init_, bool IsArray_) : Expr(KBracedExpr), Elem(Elem_), Init(Init_), IsArray(IsArray_) {} +#ifndef STANDALONE_DEMANGLER + static void profileCtor(llvm::FoldingSetNodeID &ID, const Node *Elem, + const Node *Init, bool IsArray) { + Expr::profileCtor(ID, KBracedExpr); + ID.AddPointer(Elem); + ID.AddPointer(Init); + ID.AddBoolean(IsArray); + } + void profile(llvm::FoldingSetNodeID &ID) const override { + profileCtor(ID, Elem, Init, IsArray); + } +#endif + void printLeft(OutputStream &S) const override { if (IsArray) { S += '['; @@ -1594,13 +2358,26 @@ }; class BracedRangeExpr : public Expr { - Node *First; - Node *Last; - Node *Init; + const Node *First; + const Node *Last; + const Node *Init; public: - BracedRangeExpr(Node *First_, Node *Last_, Node *Init_) + BracedRangeExpr(const Node *First_, const Node *Last_, const Node *Init_) : Expr(KBracedRangeExpr), First(First_), Last(Last_), Init(Init_) {} +#ifndef STANDALONE_DEMANGLER + static void profileCtor(llvm::FoldingSetNodeID &ID, const Node *First, + const Node *Last, const Node *Init) { + Expr::profileCtor(ID, KBracedRangeExpr); + ID.AddPointer(First); + ID.AddPointer(Last); + ID.AddPointer(Init); + } + void profile(llvm::FoldingSetNodeID &ID) const override { + profileCtor(ID, First, Last, Init); + } +#endif + void printLeft(OutputStream &S) const override { S += '['; First->print(S); @@ -1614,14 +2391,31 @@ }; struct FoldExpr : Expr { - Node *Pack, *Init; + const Node *Pack, *Init; StringView OperatorName; bool IsLeftFold; - FoldExpr(bool IsLeftFold_, StringView OperatorName_, Node *Pack_, Node *Init_) + FoldExpr(bool IsLeftFold_, StringView OperatorName_, const Node *Pack_, + const Node *Init_) : Pack(Pack_), Init(Init_), OperatorName(OperatorName_), IsLeftFold(IsLeftFold_) {} +#ifndef STANDALONE_DEMANGLER + static void profileCtor(llvm::FoldingSetNodeID &ID, bool IsLeftFold, + StringView OperatorName, const Node *Pack, + const Node *Init) { + Expr::profileCtor(ID); + ID.AddInteger(KFoldExpr); + ID.AddBoolean(IsLeftFold); + addString(ID, OperatorName); + ID.AddPointer(Pack); + ID.AddPointer(Init); + } + void profile(llvm::FoldingSetNodeID &ID) const override { + profileCtor(ID, IsLeftFold, OperatorName, Pack, Init); + } +#endif + void printLeft(OutputStream &S) const override { auto PrintPack = [&] { S += '('; @@ -1666,7 +2460,18 @@ const Node *Op; public: - ThrowExpr(Node *Op_) : Op(Op_) {} + ThrowExpr(const Node *Op_) : Op(Op_) {} + +#ifndef STANDALONE_DEMANGLER + static void profileCtor(llvm::FoldingSetNodeID &ID, const Node *Op) { + Expr::profileCtor(ID); + ID.AddInteger(KThrowExpr); + ID.AddPointer(Op); + } + void profile(llvm::FoldingSetNodeID &ID) const override { + profileCtor(ID, Op); + } +#endif void printLeft(OutputStream &S) const override { S += "throw "; @@ -1680,6 +2485,17 @@ public: BoolExpr(bool Value_) : Value(Value_) {} +#ifndef STANDALONE_DEMANGLER + static void profileCtor(llvm::FoldingSetNodeID &ID, bool Value) { + Expr::profileCtor(ID); + ID.AddInteger(KBoolExpr); + ID.AddBoolean(Value); + } + void profile(llvm::FoldingSetNodeID &ID) const override { + profileCtor(ID, Value); + } +#endif + void printLeft(OutputStream &S) const override { S += Value ? StringView("true") : StringView("false"); } @@ -1687,13 +2503,26 @@ class IntegerCastExpr : public Expr { // ty(integer) - Node *Ty; + const Node *Ty; StringView Integer; public: - IntegerCastExpr(Node *Ty_, StringView Integer_) + IntegerCastExpr(const Node *Ty_, StringView Integer_) : Ty(Ty_), Integer(Integer_) {} +#ifndef STANDALONE_DEMANGLER + static void profileCtor(llvm::FoldingSetNodeID &ID, const Node *Ty, + StringView Integer) { + Expr::profileCtor(ID); + ID.AddInteger(KIntegerCastExpr); + ID.AddPointer(Ty); + addString(ID, Integer); + } + void profile(llvm::FoldingSetNodeID &ID) const override { + profileCtor(ID, Ty, Integer); + } +#endif + void printLeft(OutputStream &S) const override { S += "("; Ty->print(S); @@ -1709,6 +2538,19 @@ public: IntegerExpr(StringView Type_, StringView Value_) : Type(Type_), Value(Value_) {} +#ifndef STANDALONE_DEMANGLER + static void profileCtor(llvm::FoldingSetNodeID &ID, StringView Type, + StringView Value) { + Expr::profileCtor(ID); + ID.AddInteger(KIntegerExpr); + addString(ID, Type); + addString(ID, Value); + } + void profile(llvm::FoldingSetNodeID &ID) const override { + profileCtor(ID, Type, Value); + } +#endif + void printLeft(OutputStream &S) const override { if (Type.size() > 3) { S += "("; @@ -1735,6 +2577,17 @@ public: FloatExpr(StringView Contents_) : Contents(Contents_) {} +#ifndef STANDALONE_DEMANGLER + static void profileCtor(llvm::FoldingSetNodeID &ID, StringView Contents) { + Expr::profileCtor(ID); + ID.AddInteger(KFloatExpr); + addString(ID, Contents); + } + void profile(llvm::FoldingSetNodeID &ID) const override { + profileCtor(ID, Contents); + } +#endif + void printLeft(OutputStream &s) const override { const char *first = Contents.begin(); const char *last = Contents.end() + 1; @@ -1958,7 +2811,55 @@ } }; -template +#ifndef STANDALONE_DEMANGLER +class FoldingNodeAllocator { + class alignas(alignof(Node *)) NodeHeader : public llvm::FoldingSetNode { + public: + ::Node *getNode() { return reinterpret_cast<::Node*>(this + 1); } + void Profile(llvm::FoldingSetNodeID &ID) { + getNode()->profile(ID); + } + }; + + BumpPointerAllocator RawAlloc; + llvm::FoldingSet Nodes; + +public: + void reset() {} + + template T *makeNode(Args &&...args) { + llvm::FoldingSetNodeID ID; + T::profileCtor(ID, args...); + + void *InsertPos; + if (NodeHeader *Existing = Nodes.FindNodeOrInsertPos(ID, InsertPos)) + return static_cast(Existing->getNode()); + + void *Storage = RawAlloc.allocate(sizeof(NodeHeader) + sizeof(T)); + NodeHeader *New = new (Storage) NodeHeader; + T *Result = new (New->getNode()) T(std::forward(args)...); + Nodes.InsertNode(New, InsertPos); + return Result; + } + + void *allocateNodeArray(size_t sz) { + return RawAlloc.allocate(sizeof(Node *) * sz); + } +}; + +// FIXME: Don't canonicalize forward template references for now, because they +// contain state (the resolved template node) that's not known at their point +// of creation. +template <> +ForwardTemplateReference * +FoldingNodeAllocator::makeNode( + size_t &Index) { + return new (RawAlloc.allocate(sizeof(ForwardTemplateReference))) + ForwardTemplateReference(Index); +} +#endif + +template struct Db { const char *First; const char *Last; @@ -5054,7 +5955,7 @@ return RootNode == nullptr; } -static char *printNode(Node *RootNode, char *Buf, size_t *N) { +static char *printNode(const Node *RootNode, char *Buf, size_t *N) { OutputStream S; if (initializeOutputStream(Buf, N, S, 128)) return nullptr; @@ -5069,24 +5970,24 @@ if (!isFunction()) return nullptr; - Node *Name = static_cast(RootNode)->getName(); + const Node *Name = static_cast(RootNode)->getName(); while (true) { switch (Name->getKind()) { case Node::KAbiTagAttr: - Name = static_cast(Name)->Base; + Name = static_cast(Name)->Base; continue; case Node::KStdQualifiedName: - Name = static_cast(Name)->Child; + Name = static_cast(Name)->Child; continue; case Node::KNestedName: - Name = static_cast(Name)->Name; + Name = static_cast(Name)->Name; continue; case Node::KLocalName: - Name = static_cast(Name)->Entity; + Name = static_cast(Name)->Entity; continue; case Node::KNameWithTemplateArgs: - Name = static_cast(Name)->Name; + Name = static_cast(Name)->Name; continue; default: return printNode(Name, Buf, N); @@ -5098,7 +5999,7 @@ size_t *N) const { if (!isFunction()) return nullptr; - Node *Name = static_cast(RootNode)->getName(); + const Node *Name = static_cast(RootNode)->getName(); OutputStream S; if (initializeOutputStream(Buf, N, S, 128)) @@ -5107,11 +6008,11 @@ KeepGoingLocalFunction: while (true) { if (Name->getKind() == Node::KAbiTagAttr) { - Name = static_cast(Name)->Base; + Name = static_cast(Name)->Base; continue; } if (Name->getKind() == Node::KNameWithTemplateArgs) { - Name = static_cast(Name)->Name; + Name = static_cast(Name)->Name; continue; } break; @@ -5122,10 +6023,10 @@ S += "std"; break; case Node::KNestedName: - static_cast(Name)->Qual->print(S); + static_cast(Name)->Qual->print(S); break; case Node::KLocalName: { - auto *LN = static_cast(Name); + auto *LN = static_cast(Name); LN->Encoding->print(S); S += "::"; Name = LN->Entity; @@ -5175,7 +6076,8 @@ if (initializeOutputStream(Buf, N, S, 128)) return nullptr; - if (Node *Ret = static_cast(RootNode)->getReturnType()) + if (const Node *Ret = + static_cast(RootNode)->getReturnType()) Ret->print(S); S += '\0'; @@ -5193,12 +6095,12 @@ assert(RootNode != nullptr && "must call partialDemangle()"); if (!isFunction()) return false; - auto *E = static_cast(RootNode); + auto *E = static_cast(RootNode); return E->getCVQuals() != QualNone || E->getRefQual() != FrefQualNone; } bool ItaniumPartialDemangler::isCtorOrDtor() const { - Node *N = static_cast(RootNode); + const Node *N = static_cast(RootNode); while (N) { switch (N->getKind()) { default: @@ -5207,22 +6109,22 @@ return true; case Node::KAbiTagAttr: - N = static_cast(N)->Base; + N = static_cast(N)->Base; break; case Node::KFunctionEncoding: - N = static_cast(N)->getName(); + N = static_cast(N)->getName(); break; case Node::KLocalName: - N = static_cast(N)->Entity; + N = static_cast(N)->Entity; break; case Node::KNameWithTemplateArgs: - N = static_cast(N)->Name; + N = static_cast(N)->Name; break; case Node::KNestedName: - N = static_cast(N)->Name; + N = static_cast(N)->Name; break; case Node::KStdQualifiedName: - N = static_cast(N)->Child; + N = static_cast(N)->Child; break; } } @@ -5231,12 +6133,13 @@ bool ItaniumPartialDemangler::isFunction() const { assert(RootNode != nullptr && "must call partialDemangle()"); - return static_cast(RootNode)->getKind() == Node::KFunctionEncoding; + return static_cast(RootNode)->getKind() == + Node::KFunctionEncoding; } bool ItaniumPartialDemangler::isSpecialName() const { assert(RootNode != nullptr && "must call partialDemangle()"); - auto K = static_cast(RootNode)->getKind(); + auto K = static_cast(RootNode)->getKind(); return K == Node::KSpecialName || K == Node::KCtorVtableSpecialName; }