Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:36:31

0001 //===- DeclarationName.h - Representation of declaration names --*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 //
0009 // This file declares the DeclarationName and DeclarationNameTable classes.
0010 //
0011 //===----------------------------------------------------------------------===//
0012 
0013 #ifndef LLVM_CLANG_AST_DECLARATIONNAME_H
0014 #define LLVM_CLANG_AST_DECLARATIONNAME_H
0015 
0016 #include "clang/AST/Type.h"
0017 #include "clang/Basic/Diagnostic.h"
0018 #include "clang/Basic/IdentifierTable.h"
0019 #include "clang/Basic/OperatorKinds.h"
0020 #include "clang/Basic/PartialDiagnostic.h"
0021 #include "clang/Basic/SourceLocation.h"
0022 #include "llvm/ADT/DenseMapInfo.h"
0023 #include "llvm/ADT/FoldingSet.h"
0024 #include "llvm/ADT/STLExtras.h"
0025 #include "llvm/Support/Compiler.h"
0026 #include "llvm/Support/type_traits.h"
0027 #include <cassert>
0028 #include <cstdint>
0029 #include <cstring>
0030 #include <string>
0031 
0032 namespace clang {
0033 
0034 class ASTContext;
0035 template <typename> class CanQual;
0036 class DeclarationName;
0037 class DeclarationNameTable;
0038 struct PrintingPolicy;
0039 class TemplateDecl;
0040 class TypeSourceInfo;
0041 
0042 using CanQualType = CanQual<Type>;
0043 
0044 namespace detail {
0045 
0046 /// CXXSpecialNameExtra records the type associated with one of the "special"
0047 /// kinds of declaration names in C++, e.g., constructors, destructors, and
0048 /// conversion functions. Note that CXXSpecialName is used for C++ constructor,
0049 /// destructor and conversion functions, but the actual kind is not stored in
0050 /// CXXSpecialName. Instead we use three different FoldingSet<CXXSpecialName>
0051 /// in DeclarationNameTable.
0052 class alignas(IdentifierInfoAlignment) CXXSpecialNameExtra
0053     : public llvm::FoldingSetNode {
0054   friend class clang::DeclarationName;
0055   friend class clang::DeclarationNameTable;
0056 
0057   /// The type associated with this declaration name.
0058   QualType Type;
0059 
0060   /// Extra information associated with this declaration name that
0061   /// can be used by the front end. All bits are really needed
0062   /// so it is not possible to stash something in the low order bits.
0063   void *FETokenInfo;
0064 
0065   CXXSpecialNameExtra(QualType QT) : Type(QT), FETokenInfo(nullptr) {}
0066 
0067 public:
0068   void Profile(llvm::FoldingSetNodeID &ID) {
0069     ID.AddPointer(Type.getAsOpaquePtr());
0070   }
0071 };
0072 
0073 /// Contains extra information for the name of a C++ deduction guide.
0074 class alignas(IdentifierInfoAlignment) CXXDeductionGuideNameExtra
0075     : public detail::DeclarationNameExtra,
0076       public llvm::FoldingSetNode {
0077   friend class clang::DeclarationName;
0078   friend class clang::DeclarationNameTable;
0079 
0080   /// The template named by the deduction guide.
0081   TemplateDecl *Template;
0082 
0083   /// Extra information associated with this operator name that
0084   /// can be used by the front end. All bits are really needed
0085   /// so it is not possible to stash something in the low order bits.
0086   void *FETokenInfo;
0087 
0088   CXXDeductionGuideNameExtra(TemplateDecl *TD)
0089       : DeclarationNameExtra(CXXDeductionGuideName), Template(TD),
0090         FETokenInfo(nullptr) {}
0091 
0092 public:
0093   void Profile(llvm::FoldingSetNodeID &ID) { ID.AddPointer(Template); }
0094 };
0095 
0096 /// Contains extra information for the name of an overloaded operator
0097 /// in C++, such as "operator+. This do not includes literal or conversion
0098 /// operators. For literal operators see CXXLiteralOperatorIdName and for
0099 /// conversion operators see CXXSpecialNameExtra.
0100 class alignas(IdentifierInfoAlignment) CXXOperatorIdName {
0101   friend class clang::DeclarationName;
0102   friend class clang::DeclarationNameTable;
0103 
0104   /// The kind of this operator.
0105   OverloadedOperatorKind Kind = OO_None;
0106 
0107   /// Extra information associated with this operator name that
0108   /// can be used by the front end. All bits are really needed
0109   /// so it is not possible to stash something in the low order bits.
0110   void *FETokenInfo = nullptr;
0111 };
0112 
0113 /// Contains the actual identifier that makes up the
0114 /// name of a C++ literal operator.
0115 class alignas(IdentifierInfoAlignment) CXXLiteralOperatorIdName
0116     : public detail::DeclarationNameExtra,
0117       public llvm::FoldingSetNode {
0118   friend class clang::DeclarationName;
0119   friend class clang::DeclarationNameTable;
0120 
0121   const IdentifierInfo *ID;
0122 
0123   /// Extra information associated with this operator name that
0124   /// can be used by the front end. All bits are really needed
0125   /// so it is not possible to stash something in the low order bits.
0126   void *FETokenInfo;
0127 
0128   CXXLiteralOperatorIdName(const IdentifierInfo *II)
0129       : DeclarationNameExtra(CXXLiteralOperatorName), ID(II),
0130         FETokenInfo(nullptr) {}
0131 
0132 public:
0133   void Profile(llvm::FoldingSetNodeID &FSID) { FSID.AddPointer(ID); }
0134 };
0135 
0136 } // namespace detail
0137 
0138 /// The name of a declaration. In the common case, this just stores
0139 /// an IdentifierInfo pointer to a normal name. However, it also provides
0140 /// encodings for Objective-C selectors (optimizing zero- and one-argument
0141 /// selectors, which make up 78% percent of all selectors in Cocoa.h),
0142 /// special C++ names for constructors, destructors, and conversion functions,
0143 /// and C++ overloaded operators.
0144 class DeclarationName {
0145   friend class DeclarationNameTable;
0146   friend class NamedDecl;
0147 
0148   /// StoredNameKind represent the kind of name that is actually stored in the
0149   /// upper bits of the Ptr field. This is only used internally.
0150   ///
0151   /// NameKind, StoredNameKind, and DeclarationNameExtra::ExtraKind
0152   /// must satisfy the following properties. These properties enable
0153   /// efficient conversion between the various kinds.
0154   ///
0155   /// * The first seven enumerators of StoredNameKind must have the same
0156   ///   numerical value as the first seven enumerators of NameKind.
0157   ///   This enable efficient conversion between the two enumerations
0158   ///   in the usual case.
0159   ///
0160   /// * The enumerations values of DeclarationNameExtra::ExtraKind must start
0161   ///   at zero, and correspond to the numerical value of the first non-inline
0162   ///   enumeration values of NameKind minus an offset. This makes conversion
0163   ///   between DeclarationNameExtra::ExtraKind and NameKind possible with
0164   ///   a single addition/substraction.
0165   ///
0166   /// * The enumeration values of Selector::IdentifierInfoFlag must correspond
0167   ///   to the relevant enumeration values of StoredNameKind.
0168   ///   More specifically:
0169   ///    * ZeroArg == StoredObjCZeroArgSelector,
0170   ///    * OneArg == StoredObjCOneArgSelector,
0171   ///    * MultiArg == StoredDeclarationNameExtra
0172   ///
0173   /// * PtrMask must mask the low 3 bits of Ptr.
0174   enum StoredNameKind {
0175     StoredIdentifier = 0,
0176     StoredObjCZeroArgSelector = Selector::ZeroArg,
0177     StoredObjCOneArgSelector = Selector::OneArg,
0178     StoredCXXConstructorName = 3,
0179     StoredCXXDestructorName = 4,
0180     StoredCXXConversionFunctionName = 5,
0181     StoredCXXOperatorName = 6,
0182     StoredDeclarationNameExtra = Selector::MultiArg,
0183     PtrMask = 7,
0184     UncommonNameKindOffset = 8
0185   };
0186 
0187   static_assert(alignof(IdentifierInfo) >= 8 &&
0188                     alignof(detail::DeclarationNameExtra) >= 8 &&
0189                     alignof(detail::CXXSpecialNameExtra) >= 8 &&
0190                     alignof(detail::CXXOperatorIdName) >= 8 &&
0191                     alignof(detail::CXXDeductionGuideNameExtra) >= 8 &&
0192                     alignof(detail::CXXLiteralOperatorIdName) >= 8,
0193                 "The various classes that DeclarationName::Ptr can point to"
0194                 " must be at least aligned to 8 bytes!");
0195 
0196   static_assert(
0197       std::is_same<std::underlying_type_t<StoredNameKind>,
0198                    std::underlying_type_t<
0199                        detail::DeclarationNameExtra::ExtraKind>>::value,
0200       "The various enums used to compute values for NameKind should "
0201       "all have the same underlying type");
0202 
0203 public:
0204   /// The kind of the name stored in this DeclarationName.
0205   /// The first 7 enumeration values are stored inline and correspond
0206   /// to frequently used kinds. The rest is stored in DeclarationNameExtra
0207   /// and correspond to infrequently used kinds.
0208   enum NameKind {
0209     Identifier = StoredIdentifier,
0210     ObjCZeroArgSelector = StoredObjCZeroArgSelector,
0211     ObjCOneArgSelector = StoredObjCOneArgSelector,
0212     CXXConstructorName = StoredCXXConstructorName,
0213     CXXDestructorName = StoredCXXDestructorName,
0214     CXXConversionFunctionName = StoredCXXConversionFunctionName,
0215     CXXOperatorName = StoredCXXOperatorName,
0216     CXXDeductionGuideName = llvm::addEnumValues(
0217         UncommonNameKindOffset,
0218         detail::DeclarationNameExtra::CXXDeductionGuideName),
0219     CXXLiteralOperatorName = llvm::addEnumValues(
0220         UncommonNameKindOffset,
0221         detail::DeclarationNameExtra::CXXLiteralOperatorName),
0222     CXXUsingDirective =
0223         llvm::addEnumValues(UncommonNameKindOffset,
0224                             detail::DeclarationNameExtra::CXXUsingDirective),
0225     ObjCMultiArgSelector =
0226         llvm::addEnumValues(UncommonNameKindOffset,
0227                             detail::DeclarationNameExtra::ObjCMultiArgSelector),
0228   };
0229 
0230 private:
0231   /// The lowest three bits of Ptr are used to express what kind of name
0232   /// we're actually storing, using the values of StoredNameKind. Depending
0233   /// on the kind of name this is, the upper bits of Ptr may have one
0234   /// of several different meanings:
0235   ///
0236   ///   StoredIdentifier - The name is a normal identifier, and Ptr is
0237   ///   a normal IdentifierInfo pointer.
0238   ///
0239   ///   StoredObjCZeroArgSelector - The name is an Objective-C
0240   ///   selector with zero arguments, and Ptr is an IdentifierInfo
0241   ///   pointer pointing to the selector name.
0242   ///
0243   ///   StoredObjCOneArgSelector - The name is an Objective-C selector
0244   ///   with one argument, and Ptr is an IdentifierInfo pointer
0245   ///   pointing to the selector name.
0246   ///
0247   ///   StoredCXXConstructorName - The name of a C++ constructor,
0248   ///   Ptr points to a CXXSpecialNameExtra.
0249   ///
0250   ///   StoredCXXDestructorName - The name of a C++ destructor,
0251   ///   Ptr points to a CXXSpecialNameExtra.
0252   ///
0253   ///   StoredCXXConversionFunctionName - The name of a C++ conversion function,
0254   ///   Ptr points to a CXXSpecialNameExtra.
0255   ///
0256   ///   StoredCXXOperatorName - The name of an overloaded C++ operator,
0257   ///   Ptr points to a CXXOperatorIdName.
0258   ///
0259   ///   StoredDeclarationNameExtra - Ptr is actually a pointer to a
0260   ///   DeclarationNameExtra structure, whose first value will tell us
0261   ///   whether this is an Objective-C selector, C++ deduction guide,
0262   ///   C++ literal operator, or C++ using directive.
0263   uintptr_t Ptr = 0;
0264 
0265   StoredNameKind getStoredNameKind() const {
0266     return static_cast<StoredNameKind>(Ptr & PtrMask);
0267   }
0268 
0269   void *getPtr() const { return reinterpret_cast<void *>(Ptr & ~PtrMask); }
0270 
0271   void setPtrAndKind(const void *P, StoredNameKind Kind) {
0272     uintptr_t PAsInteger = reinterpret_cast<uintptr_t>(P);
0273     assert((Kind & ~PtrMask) == 0 &&
0274            "Invalid StoredNameKind in setPtrAndKind!");
0275     assert((PAsInteger & PtrMask) == 0 &&
0276            "Improperly aligned pointer in setPtrAndKind!");
0277     Ptr = PAsInteger | Kind;
0278   }
0279 
0280   /// Construct a declaration name from a DeclarationNameExtra.
0281   DeclarationName(detail::DeclarationNameExtra *Name) {
0282     setPtrAndKind(Name, StoredDeclarationNameExtra);
0283   }
0284 
0285   /// Construct a declaration name from a CXXSpecialNameExtra.
0286   DeclarationName(detail::CXXSpecialNameExtra *Name,
0287                   StoredNameKind StoredKind) {
0288     assert((StoredKind == StoredCXXConstructorName ||
0289            StoredKind == StoredCXXDestructorName ||
0290            StoredKind == StoredCXXConversionFunctionName) &&
0291                "Invalid StoredNameKind when constructing a DeclarationName"
0292                " from a CXXSpecialNameExtra!");
0293     setPtrAndKind(Name, StoredKind);
0294   }
0295 
0296   /// Construct a DeclarationName from a CXXOperatorIdName.
0297   DeclarationName(detail::CXXOperatorIdName *Name) {
0298     setPtrAndKind(Name, StoredCXXOperatorName);
0299   }
0300 
0301   /// Assert that the stored pointer points to an IdentifierInfo and return it.
0302   IdentifierInfo *castAsIdentifierInfo() const {
0303     assert((getStoredNameKind() == StoredIdentifier) &&
0304            "DeclarationName does not store an IdentifierInfo!");
0305     return static_cast<IdentifierInfo *>(getPtr());
0306   }
0307 
0308   /// Assert that the stored pointer points to a DeclarationNameExtra
0309   /// and return it.
0310   detail::DeclarationNameExtra *castAsExtra() const {
0311     assert((getStoredNameKind() == StoredDeclarationNameExtra) &&
0312            "DeclarationName does not store an Extra structure!");
0313     return static_cast<detail::DeclarationNameExtra *>(getPtr());
0314   }
0315 
0316   /// Assert that the stored pointer points to a CXXSpecialNameExtra
0317   /// and return it.
0318   detail::CXXSpecialNameExtra *castAsCXXSpecialNameExtra() const {
0319     assert((getStoredNameKind() == StoredCXXConstructorName ||
0320            getStoredNameKind() == StoredCXXDestructorName ||
0321            getStoredNameKind() == StoredCXXConversionFunctionName) &&
0322                "DeclarationName does not store a CXXSpecialNameExtra!");
0323     return static_cast<detail::CXXSpecialNameExtra *>(getPtr());
0324   }
0325 
0326   /// Assert that the stored pointer points to a CXXOperatorIdName
0327   /// and return it.
0328   detail::CXXOperatorIdName *castAsCXXOperatorIdName() const {
0329     assert((getStoredNameKind() == StoredCXXOperatorName) &&
0330            "DeclarationName does not store a CXXOperatorIdName!");
0331     return static_cast<detail::CXXOperatorIdName *>(getPtr());
0332   }
0333 
0334   /// Assert that the stored pointer points to a CXXDeductionGuideNameExtra
0335   /// and return it.
0336   detail::CXXDeductionGuideNameExtra *castAsCXXDeductionGuideNameExtra() const {
0337     assert(getNameKind() == CXXDeductionGuideName &&
0338            "DeclarationName does not store a CXXDeductionGuideNameExtra!");
0339     return static_cast<detail::CXXDeductionGuideNameExtra *>(getPtr());
0340   }
0341 
0342   /// Assert that the stored pointer points to a CXXLiteralOperatorIdName
0343   /// and return it.
0344   detail::CXXLiteralOperatorIdName *castAsCXXLiteralOperatorIdName() const {
0345     assert(getNameKind() == CXXLiteralOperatorName &&
0346            "DeclarationName does not store a CXXLiteralOperatorIdName!");
0347     return static_cast<detail::CXXLiteralOperatorIdName *>(getPtr());
0348   }
0349 
0350   /// Get and set the FETokenInfo in the less common cases where the
0351   /// declaration name do not point to an identifier.
0352   void *getFETokenInfoSlow() const;
0353   void setFETokenInfoSlow(void *T);
0354 
0355 public:
0356   /// Construct an empty declaration name.
0357   DeclarationName() { setPtrAndKind(nullptr, StoredIdentifier); }
0358 
0359   /// Construct a declaration name from an IdentifierInfo *.
0360   DeclarationName(const IdentifierInfo *II) {
0361     setPtrAndKind(II, StoredIdentifier);
0362   }
0363 
0364   /// Construct a declaration name from an Objective-C selector.
0365   DeclarationName(Selector Sel)
0366       : Ptr(reinterpret_cast<uintptr_t>(Sel.InfoPtr.getOpaqueValue())) {}
0367 
0368   /// Returns the name for all C++ using-directives.
0369   static DeclarationName getUsingDirectiveName() {
0370     // Single instance of DeclarationNameExtra for using-directive
0371     static detail::DeclarationNameExtra UDirExtra(
0372         detail::DeclarationNameExtra::CXXUsingDirective);
0373     return DeclarationName(&UDirExtra);
0374   }
0375 
0376   /// Evaluates true when this declaration name is non-empty.
0377   explicit operator bool() const {
0378     return getPtr() || (getStoredNameKind() != StoredIdentifier);
0379   }
0380 
0381   /// Evaluates true when this declaration name is empty.
0382   bool isEmpty() const { return !*this; }
0383 
0384   /// Predicate functions for querying what type of name this is.
0385   bool isIdentifier() const { return getStoredNameKind() == StoredIdentifier; }
0386   bool isObjCZeroArgSelector() const {
0387     return getStoredNameKind() == StoredObjCZeroArgSelector;
0388   }
0389   bool isObjCOneArgSelector() const {
0390     return getStoredNameKind() == StoredObjCOneArgSelector;
0391   }
0392 
0393   /// Determine what kind of name this is.
0394   NameKind getNameKind() const {
0395     // We rely on the fact that the first 7 NameKind and StoredNameKind
0396     // have the same numerical value. This makes the usual case efficient.
0397     StoredNameKind StoredKind = getStoredNameKind();
0398     if (StoredKind != StoredDeclarationNameExtra)
0399       return static_cast<NameKind>(StoredKind);
0400     // We have to consult DeclarationNameExtra. We rely on the fact that the
0401     // enumeration values of ExtraKind correspond to the enumeration values of
0402     // NameKind minus an offset of UncommonNameKindOffset.
0403     unsigned ExtraKind = castAsExtra()->getKind();
0404     return static_cast<NameKind>(UncommonNameKindOffset + ExtraKind);
0405   }
0406 
0407   /// Determines whether the name itself is dependent, e.g., because it
0408   /// involves a C++ type that is itself dependent.
0409   ///
0410   /// Note that this does not capture all of the notions of "dependent name",
0411   /// because an identifier can be a dependent name if it is used as the
0412   /// callee in a call expression with dependent arguments.
0413   bool isDependentName() const;
0414 
0415   /// Retrieve the human-readable string for this name.
0416   std::string getAsString() const;
0417 
0418   /// Retrieve the IdentifierInfo * stored in this declaration name,
0419   /// or null if this declaration name isn't a simple identifier.
0420   IdentifierInfo *getAsIdentifierInfo() const {
0421     if (isIdentifier())
0422       return castAsIdentifierInfo();
0423     return nullptr;
0424   }
0425 
0426   /// Get the representation of this declaration name as an opaque integer.
0427   uintptr_t getAsOpaqueInteger() const { return Ptr; }
0428 
0429   /// Get the representation of this declaration name as an opaque pointer.
0430   void *getAsOpaquePtr() const { return reinterpret_cast<void *>(Ptr); }
0431 
0432   /// Get a declaration name from an opaque pointer returned by getAsOpaquePtr.
0433   static DeclarationName getFromOpaquePtr(void *P) {
0434     DeclarationName N;
0435     N.Ptr = reinterpret_cast<uintptr_t>(P);
0436     return N;
0437   }
0438 
0439   /// Get a declaration name from an opaque integer
0440   /// returned by getAsOpaqueInteger.
0441   static DeclarationName getFromOpaqueInteger(uintptr_t P) {
0442     DeclarationName N;
0443     N.Ptr = P;
0444     return N;
0445   }
0446 
0447   /// If this name is one of the C++ names (of a constructor, destructor,
0448   /// or conversion function), return the type associated with that name.
0449   QualType getCXXNameType() const {
0450     if (getStoredNameKind() == StoredCXXConstructorName ||
0451         getStoredNameKind() == StoredCXXDestructorName ||
0452         getStoredNameKind() == StoredCXXConversionFunctionName) {
0453       assert(getPtr() && "getCXXNameType on a null DeclarationName!");
0454       return castAsCXXSpecialNameExtra()->Type;
0455     }
0456     return QualType();
0457   }
0458 
0459   /// If this name is the name of a C++ deduction guide, return the
0460   /// template associated with that name.
0461   TemplateDecl *getCXXDeductionGuideTemplate() const {
0462     if (getNameKind() == CXXDeductionGuideName) {
0463       assert(getPtr() &&
0464              "getCXXDeductionGuideTemplate on a null DeclarationName!");
0465       return castAsCXXDeductionGuideNameExtra()->Template;
0466     }
0467     return nullptr;
0468   }
0469 
0470   /// If this name is the name of an overloadable operator in C++
0471   /// (e.g., @c operator+), retrieve the kind of overloaded operator.
0472   OverloadedOperatorKind getCXXOverloadedOperator() const {
0473     if (getStoredNameKind() == StoredCXXOperatorName) {
0474       assert(getPtr() && "getCXXOverloadedOperator on a null DeclarationName!");
0475       return castAsCXXOperatorIdName()->Kind;
0476     }
0477     return OO_None;
0478   }
0479 
0480   /// If this name is the name of a literal operator,
0481   /// retrieve the identifier associated with it.
0482   const IdentifierInfo *getCXXLiteralIdentifier() const {
0483     if (getNameKind() == CXXLiteralOperatorName) {
0484       assert(getPtr() && "getCXXLiteralIdentifier on a null DeclarationName!");
0485       return castAsCXXLiteralOperatorIdName()->ID;
0486     }
0487     return nullptr;
0488   }
0489 
0490   /// Get the Objective-C selector stored in this declaration name.
0491   Selector getObjCSelector() const {
0492     assert((getNameKind() == ObjCZeroArgSelector ||
0493             getNameKind() == ObjCOneArgSelector ||
0494             getNameKind() == ObjCMultiArgSelector || !getPtr()) &&
0495            "Not a selector!");
0496     return Selector(Ptr);
0497   }
0498 
0499   /// Get and set FETokenInfo. The language front-end is allowed to associate
0500   /// arbitrary metadata with some kinds of declaration names, including normal
0501   /// identifiers and C++ constructors, destructors, and conversion functions.
0502   void *getFETokenInfo() const {
0503     assert(getPtr() && "getFETokenInfo on an empty DeclarationName!");
0504     if (getStoredNameKind() == StoredIdentifier)
0505       return castAsIdentifierInfo()->getFETokenInfo();
0506     return getFETokenInfoSlow();
0507   }
0508 
0509   void setFETokenInfo(void *T) {
0510     assert(getPtr() && "setFETokenInfo on an empty DeclarationName!");
0511     if (getStoredNameKind() == StoredIdentifier)
0512       castAsIdentifierInfo()->setFETokenInfo(T);
0513     else
0514       setFETokenInfoSlow(T);
0515   }
0516 
0517   /// Determine whether the specified names are identical.
0518   friend bool operator==(DeclarationName LHS, DeclarationName RHS) {
0519     return LHS.Ptr == RHS.Ptr;
0520   }
0521 
0522   /// Determine whether the specified names are different.
0523   friend bool operator!=(DeclarationName LHS, DeclarationName RHS) {
0524     return LHS.Ptr != RHS.Ptr;
0525   }
0526 
0527   static DeclarationName getEmptyMarker() {
0528     DeclarationName Name;
0529     Name.Ptr = uintptr_t(-1);
0530     return Name;
0531   }
0532 
0533   static DeclarationName getTombstoneMarker() {
0534     DeclarationName Name;
0535     Name.Ptr = uintptr_t(-2);
0536     return Name;
0537   }
0538 
0539   static int compare(DeclarationName LHS, DeclarationName RHS);
0540 
0541   void print(raw_ostream &OS, const PrintingPolicy &Policy) const;
0542 
0543   void dump() const;
0544 };
0545 
0546 raw_ostream &operator<<(raw_ostream &OS, DeclarationName N);
0547 
0548 /// Ordering on two declaration names. If both names are identifiers,
0549 /// this provides a lexicographical ordering.
0550 inline bool operator<(DeclarationName LHS, DeclarationName RHS) {
0551   return DeclarationName::compare(LHS, RHS) < 0;
0552 }
0553 
0554 /// Ordering on two declaration names. If both names are identifiers,
0555 /// this provides a lexicographical ordering.
0556 inline bool operator>(DeclarationName LHS, DeclarationName RHS) {
0557   return DeclarationName::compare(LHS, RHS) > 0;
0558 }
0559 
0560 /// Ordering on two declaration names. If both names are identifiers,
0561 /// this provides a lexicographical ordering.
0562 inline bool operator<=(DeclarationName LHS, DeclarationName RHS) {
0563   return DeclarationName::compare(LHS, RHS) <= 0;
0564 }
0565 
0566 /// Ordering on two declaration names. If both names are identifiers,
0567 /// this provides a lexicographical ordering.
0568 inline bool operator>=(DeclarationName LHS, DeclarationName RHS) {
0569   return DeclarationName::compare(LHS, RHS) >= 0;
0570 }
0571 
0572 /// DeclarationNameTable is used to store and retrieve DeclarationName
0573 /// instances for the various kinds of declaration names, e.g., normal
0574 /// identifiers, C++ constructor names, etc. This class contains
0575 /// uniqued versions of each of the C++ special names, which can be
0576 /// retrieved using its member functions (e.g., getCXXConstructorName).
0577 class DeclarationNameTable {
0578   /// Used to allocate elements in the FoldingSets below.
0579   const ASTContext &Ctx;
0580 
0581   /// Manage the uniqued CXXSpecialNameExtra representing C++ constructors.
0582   /// getCXXConstructorName and getCXXSpecialName can be used to obtain
0583   /// a DeclarationName from the corresponding type of the constructor.
0584   llvm::FoldingSet<detail::CXXSpecialNameExtra> CXXConstructorNames;
0585 
0586   /// Manage the uniqued CXXSpecialNameExtra representing C++ destructors.
0587   /// getCXXDestructorName and getCXXSpecialName can be used to obtain
0588   /// a DeclarationName from the corresponding type of the destructor.
0589   llvm::FoldingSet<detail::CXXSpecialNameExtra> CXXDestructorNames;
0590 
0591   /// Manage the uniqued CXXSpecialNameExtra representing C++ conversion
0592   /// functions. getCXXConversionFunctionName and getCXXSpecialName can be
0593   /// used to obtain a DeclarationName from the corresponding type of the
0594   /// conversion function.
0595   llvm::FoldingSet<detail::CXXSpecialNameExtra> CXXConversionFunctionNames;
0596 
0597   /// Manage the uniqued CXXOperatorIdName, which contain extra information
0598   /// for the name of overloaded C++ operators. getCXXOperatorName
0599   /// can be used to obtain a DeclarationName from the operator kind.
0600   detail::CXXOperatorIdName CXXOperatorNames[NUM_OVERLOADED_OPERATORS];
0601 
0602   /// Manage the uniqued CXXLiteralOperatorIdName, which contain extra
0603   /// information for the name of C++ literal operators.
0604   /// getCXXLiteralOperatorName can be used to obtain a DeclarationName
0605   /// from the corresponding IdentifierInfo.
0606   llvm::FoldingSet<detail::CXXLiteralOperatorIdName> CXXLiteralOperatorNames;
0607 
0608   /// Manage the uniqued CXXDeductionGuideNameExtra, which contain
0609   /// extra information for the name of a C++ deduction guide.
0610   /// getCXXDeductionGuideName can be used to obtain a DeclarationName
0611   /// from the corresponding template declaration.
0612   llvm::FoldingSet<detail::CXXDeductionGuideNameExtra> CXXDeductionGuideNames;
0613 
0614 public:
0615   DeclarationNameTable(const ASTContext &C);
0616   DeclarationNameTable(const DeclarationNameTable &) = delete;
0617   DeclarationNameTable &operator=(const DeclarationNameTable &) = delete;
0618   DeclarationNameTable(DeclarationNameTable &&) = delete;
0619   DeclarationNameTable &operator=(DeclarationNameTable &&) = delete;
0620   ~DeclarationNameTable() = default;
0621 
0622   /// Create a declaration name that is a simple identifier.
0623   DeclarationName getIdentifier(const IdentifierInfo *ID) {
0624     return DeclarationName(ID);
0625   }
0626 
0627   /// Returns the name of a C++ constructor for the given Type.
0628   DeclarationName getCXXConstructorName(CanQualType Ty);
0629 
0630   /// Returns the name of a C++ destructor for the given Type.
0631   DeclarationName getCXXDestructorName(CanQualType Ty);
0632 
0633   /// Returns the name of a C++ deduction guide for the given template.
0634   DeclarationName getCXXDeductionGuideName(TemplateDecl *TD);
0635 
0636   /// Returns the name of a C++ conversion function for the given Type.
0637   DeclarationName getCXXConversionFunctionName(CanQualType Ty);
0638 
0639   /// Returns a declaration name for special kind of C++ name,
0640   /// e.g., for a constructor, destructor, or conversion function.
0641   /// Kind must be one of:
0642   ///   * DeclarationName::CXXConstructorName,
0643   ///   * DeclarationName::CXXDestructorName or
0644   ///   * DeclarationName::CXXConversionFunctionName
0645   DeclarationName getCXXSpecialName(DeclarationName::NameKind Kind,
0646                                     CanQualType Ty);
0647 
0648   /// Get the name of the overloadable C++ operator corresponding to Op.
0649   DeclarationName getCXXOperatorName(OverloadedOperatorKind Op) {
0650     return DeclarationName(&CXXOperatorNames[Op]);
0651   }
0652 
0653   /// Get the name of the literal operator function with II as the identifier.
0654   DeclarationName getCXXLiteralOperatorName(const IdentifierInfo *II);
0655 };
0656 
0657 /// DeclarationNameLoc - Additional source/type location info
0658 /// for a declaration name. Needs a DeclarationName in order
0659 /// to be interpreted correctly.
0660 class DeclarationNameLoc {
0661   // The source location for identifier stored elsewhere.
0662   // struct {} Identifier;
0663 
0664   // Type info for constructors, destructors and conversion functions.
0665   // Locations (if any) for the tilde (destructor) or operator keyword
0666   // (conversion) are stored elsewhere.
0667   struct NT {
0668     TypeSourceInfo *TInfo;
0669   };
0670 
0671   // The location (if any) of the operator keyword is stored elsewhere.
0672   struct CXXOpName {
0673     SourceLocation::UIntTy BeginOpNameLoc;
0674     SourceLocation::UIntTy EndOpNameLoc;
0675   };
0676 
0677   // The location (if any) of the operator keyword is stored elsewhere.
0678   struct CXXLitOpName {
0679     SourceLocation::UIntTy OpNameLoc;
0680   };
0681 
0682   // struct {} CXXUsingDirective;
0683   // struct {} ObjCZeroArgSelector;
0684   // struct {} ObjCOneArgSelector;
0685   // struct {} ObjCMultiArgSelector;
0686   union {
0687     struct NT NamedType;
0688     struct CXXOpName CXXOperatorName;
0689     struct CXXLitOpName CXXLiteralOperatorName;
0690   };
0691 
0692   void setNamedTypeLoc(TypeSourceInfo *TInfo) { NamedType.TInfo = TInfo; }
0693 
0694   void setCXXOperatorNameRange(SourceRange Range) {
0695     CXXOperatorName.BeginOpNameLoc = Range.getBegin().getRawEncoding();
0696     CXXOperatorName.EndOpNameLoc = Range.getEnd().getRawEncoding();
0697   }
0698 
0699   void setCXXLiteralOperatorNameLoc(SourceLocation Loc) {
0700     CXXLiteralOperatorName.OpNameLoc = Loc.getRawEncoding();
0701   }
0702 
0703 public:
0704   DeclarationNameLoc(DeclarationName Name);
0705   // FIXME: this should go away once all DNLocs are properly initialized.
0706   DeclarationNameLoc() { memset((void*) this, 0, sizeof(*this)); }
0707 
0708   /// Returns the source type info. Assumes that the object stores location
0709   /// information of a constructor, destructor or conversion operator.
0710   TypeSourceInfo *getNamedTypeInfo() const { return NamedType.TInfo; }
0711 
0712   /// Return the beginning location of the getCXXOperatorNameRange() range.
0713   SourceLocation getCXXOperatorNameBeginLoc() const {
0714     return SourceLocation::getFromRawEncoding(CXXOperatorName.BeginOpNameLoc);
0715   }
0716 
0717   /// Return the end location of the getCXXOperatorNameRange() range.
0718   SourceLocation getCXXOperatorNameEndLoc() const {
0719     return SourceLocation::getFromRawEncoding(CXXOperatorName.EndOpNameLoc);
0720   }
0721 
0722   /// Return the range of the operator name (without the operator keyword).
0723   /// Assumes that the object stores location information of a (non-literal)
0724   /// operator.
0725   SourceRange getCXXOperatorNameRange() const {
0726     return SourceRange(getCXXOperatorNameBeginLoc(),
0727                        getCXXOperatorNameEndLoc());
0728   }
0729 
0730   /// Return the location of the literal operator name (without the operator
0731   /// keyword). Assumes that the object stores location information of a literal
0732   /// operator.
0733   SourceLocation getCXXLiteralOperatorNameLoc() const {
0734     return SourceLocation::getFromRawEncoding(CXXLiteralOperatorName.OpNameLoc);
0735   }
0736 
0737   /// Construct location information for a constructor, destructor or conversion
0738   /// operator.
0739   static DeclarationNameLoc makeNamedTypeLoc(TypeSourceInfo *TInfo) {
0740     DeclarationNameLoc DNL;
0741     DNL.setNamedTypeLoc(TInfo);
0742     return DNL;
0743   }
0744 
0745   /// Construct location information for a non-literal C++ operator.
0746   static DeclarationNameLoc makeCXXOperatorNameLoc(SourceLocation BeginLoc,
0747                                                    SourceLocation EndLoc) {
0748     return makeCXXOperatorNameLoc(SourceRange(BeginLoc, EndLoc));
0749   }
0750 
0751   /// Construct location information for a non-literal C++ operator.
0752   static DeclarationNameLoc makeCXXOperatorNameLoc(SourceRange Range) {
0753     DeclarationNameLoc DNL;
0754     DNL.setCXXOperatorNameRange(Range);
0755     return DNL;
0756   }
0757 
0758   /// Construct location information for a literal C++ operator.
0759   static DeclarationNameLoc makeCXXLiteralOperatorNameLoc(SourceLocation Loc) {
0760     DeclarationNameLoc DNL;
0761     DNL.setCXXLiteralOperatorNameLoc(Loc);
0762     return DNL;
0763   }
0764 };
0765 
0766 /// DeclarationNameInfo - A collector data type for bundling together
0767 /// a DeclarationName and the corresponding source/type location info.
0768 struct DeclarationNameInfo {
0769 private:
0770   /// Name - The declaration name, also encoding name kind.
0771   DeclarationName Name;
0772 
0773   /// Loc - The main source location for the declaration name.
0774   SourceLocation NameLoc;
0775 
0776   /// Info - Further source/type location info for special kinds of names.
0777   DeclarationNameLoc LocInfo;
0778 
0779 public:
0780   // FIXME: remove it.
0781   DeclarationNameInfo() = default;
0782 
0783   DeclarationNameInfo(DeclarationName Name, SourceLocation NameLoc)
0784       : Name(Name), NameLoc(NameLoc), LocInfo(Name) {}
0785 
0786   DeclarationNameInfo(DeclarationName Name, SourceLocation NameLoc,
0787                       DeclarationNameLoc LocInfo)
0788       : Name(Name), NameLoc(NameLoc), LocInfo(LocInfo) {}
0789 
0790   /// getName - Returns the embedded declaration name.
0791   DeclarationName getName() const { return Name; }
0792 
0793   /// setName - Sets the embedded declaration name.
0794   void setName(DeclarationName N) { Name = N; }
0795 
0796   /// getLoc - Returns the main location of the declaration name.
0797   SourceLocation getLoc() const { return NameLoc; }
0798 
0799   /// setLoc - Sets the main location of the declaration name.
0800   void setLoc(SourceLocation L) { NameLoc = L; }
0801 
0802   const DeclarationNameLoc &getInfo() const { return LocInfo; }
0803   void setInfo(const DeclarationNameLoc &Info) { LocInfo = Info; }
0804 
0805   /// getNamedTypeInfo - Returns the source type info associated to
0806   /// the name. Assumes it is a constructor, destructor or conversion.
0807   TypeSourceInfo *getNamedTypeInfo() const {
0808     if (Name.getNameKind() != DeclarationName::CXXConstructorName &&
0809         Name.getNameKind() != DeclarationName::CXXDestructorName &&
0810         Name.getNameKind() != DeclarationName::CXXConversionFunctionName)
0811       return nullptr;
0812     return LocInfo.getNamedTypeInfo();
0813   }
0814 
0815   /// setNamedTypeInfo - Sets the source type info associated to
0816   /// the name. Assumes it is a constructor, destructor or conversion.
0817   void setNamedTypeInfo(TypeSourceInfo *TInfo) {
0818     assert(Name.getNameKind() == DeclarationName::CXXConstructorName ||
0819            Name.getNameKind() == DeclarationName::CXXDestructorName ||
0820            Name.getNameKind() == DeclarationName::CXXConversionFunctionName);
0821     LocInfo = DeclarationNameLoc::makeNamedTypeLoc(TInfo);
0822   }
0823 
0824   /// getCXXOperatorNameRange - Gets the range of the operator name
0825   /// (without the operator keyword). Assumes it is a (non-literal) operator.
0826   SourceRange getCXXOperatorNameRange() const {
0827     if (Name.getNameKind() != DeclarationName::CXXOperatorName)
0828       return SourceRange();
0829     return LocInfo.getCXXOperatorNameRange();
0830   }
0831 
0832   /// setCXXOperatorNameRange - Sets the range of the operator name
0833   /// (without the operator keyword). Assumes it is a C++ operator.
0834   void setCXXOperatorNameRange(SourceRange R) {
0835     assert(Name.getNameKind() == DeclarationName::CXXOperatorName);
0836     LocInfo = DeclarationNameLoc::makeCXXOperatorNameLoc(R);
0837   }
0838 
0839   /// getCXXLiteralOperatorNameLoc - Returns the location of the literal
0840   /// operator name (not the operator keyword).
0841   /// Assumes it is a literal operator.
0842   SourceLocation getCXXLiteralOperatorNameLoc() const {
0843     if (Name.getNameKind() != DeclarationName::CXXLiteralOperatorName)
0844       return SourceLocation();
0845     return LocInfo.getCXXLiteralOperatorNameLoc();
0846   }
0847 
0848   /// setCXXLiteralOperatorNameLoc - Sets the location of the literal
0849   /// operator name (not the operator keyword).
0850   /// Assumes it is a literal operator.
0851   void setCXXLiteralOperatorNameLoc(SourceLocation Loc) {
0852     assert(Name.getNameKind() == DeclarationName::CXXLiteralOperatorName);
0853     LocInfo = DeclarationNameLoc::makeCXXLiteralOperatorNameLoc(Loc);
0854   }
0855 
0856   /// Determine whether this name involves a template parameter.
0857   bool isInstantiationDependent() const;
0858 
0859   /// Determine whether this name contains an unexpanded
0860   /// parameter pack.
0861   bool containsUnexpandedParameterPack() const;
0862 
0863   /// getAsString - Retrieve the human-readable string for this name.
0864   std::string getAsString() const;
0865 
0866   /// printName - Print the human-readable name to a stream.
0867   void printName(raw_ostream &OS, PrintingPolicy Policy) const;
0868 
0869   /// getBeginLoc - Retrieve the location of the first token.
0870   SourceLocation getBeginLoc() const { return NameLoc; }
0871 
0872   /// getSourceRange - The range of the declaration name.
0873   SourceRange getSourceRange() const LLVM_READONLY {
0874     return SourceRange(getBeginLoc(), getEndLoc());
0875   }
0876 
0877   SourceLocation getEndLoc() const LLVM_READONLY {
0878     SourceLocation EndLoc = getEndLocPrivate();
0879     return EndLoc.isValid() ? EndLoc : getBeginLoc();
0880   }
0881 
0882 private:
0883   SourceLocation getEndLocPrivate() const;
0884 };
0885 
0886 /// Insertion operator for partial diagnostics.  This allows binding
0887 /// DeclarationName's into a partial diagnostic with <<.
0888 inline const StreamingDiagnostic &operator<<(const StreamingDiagnostic &PD,
0889                                              DeclarationName N) {
0890   PD.AddTaggedVal(N.getAsOpaqueInteger(),
0891                   DiagnosticsEngine::ak_declarationname);
0892   return PD;
0893 }
0894 
0895 raw_ostream &operator<<(raw_ostream &OS, DeclarationNameInfo DNInfo);
0896 
0897 } // namespace clang
0898 
0899 namespace llvm {
0900 
0901 /// Define DenseMapInfo so that DeclarationNames can be used as keys
0902 /// in DenseMap and DenseSets.
0903 template<>
0904 struct DenseMapInfo<clang::DeclarationName> {
0905   static inline clang::DeclarationName getEmptyKey() {
0906     return clang::DeclarationName::getEmptyMarker();
0907   }
0908 
0909   static inline clang::DeclarationName getTombstoneKey() {
0910     return clang::DeclarationName::getTombstoneMarker();
0911   }
0912 
0913   static unsigned getHashValue(clang::DeclarationName Name) {
0914     return DenseMapInfo<void*>::getHashValue(Name.getAsOpaquePtr());
0915   }
0916 
0917   static inline bool
0918   isEqual(clang::DeclarationName LHS, clang::DeclarationName RHS) {
0919     return LHS == RHS;
0920   }
0921 };
0922 
0923 template <> struct PointerLikeTypeTraits<clang::DeclarationName> {
0924   static inline void *getAsVoidPointer(clang::DeclarationName P) {
0925     return P.getAsOpaquePtr();
0926   }
0927   static inline clang::DeclarationName getFromVoidPointer(void *P) {
0928     return clang::DeclarationName::getFromOpaquePtr(P);
0929   }
0930   static constexpr int NumLowBitsAvailable = 0;
0931 };
0932 
0933 } // namespace llvm
0934 
0935 // The definition of AssumedTemplateStorage is factored out of TemplateName to
0936 // resolve a cyclic dependency between it and DeclarationName (via Type).
0937 namespace clang {
0938 
0939 /// A structure for storing the information associated with a name that has
0940 /// been assumed to be a template name (despite finding no TemplateDecls).
0941 class AssumedTemplateStorage : public UncommonTemplateNameStorage {
0942   friend class ASTContext;
0943 
0944   AssumedTemplateStorage(DeclarationName Name)
0945       : UncommonTemplateNameStorage(Assumed, 0, 0), Name(Name) {}
0946   DeclarationName Name;
0947 
0948 public:
0949   /// Get the name of the template.
0950   DeclarationName getDeclName() const { return Name; }
0951 };
0952 
0953 } // namespace clang
0954 
0955 #endif // LLVM_CLANG_AST_DECLARATIONNAME_H