1 //===- DeclarationName.h - Representation of declaration names --*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file declares the DeclarationName and DeclarationNameTable classes.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef LLVM_CLANG_AST_DECLARATIONNAME_H
14 #define LLVM_CLANG_AST_DECLARATIONNAME_H
15 
16 #include "clang/AST/Type.h"
17 #include "clang/Basic/Diagnostic.h"
18 #include "clang/Basic/IdentifierTable.h"
19 #include "clang/Basic/OperatorKinds.h"
20 #include "clang/Basic/PartialDiagnostic.h"
21 #include "clang/Basic/SourceLocation.h"
22 #include "llvm/ADT/DenseMapInfo.h"
23 #include "llvm/ADT/FoldingSet.h"
24 #include "llvm/Support/Compiler.h"
25 #include "llvm/Support/type_traits.h"
26 #include <cassert>
27 #include <cstdint>
28 #include <cstring>
29 #include <string>
30 
31 namespace clang {
32 
33 class ASTContext;
34 template <typename> class CanQual;
35 class DeclarationName;
36 class DeclarationNameTable;
37 struct PrintingPolicy;
38 class TemplateDecl;
39 class TypeSourceInfo;
40 
41 using CanQualType = CanQual<Type>;
42 
43 namespace detail {
44 
45 /// CXXSpecialNameExtra records the type associated with one of the "special"
46 /// kinds of declaration names in C++, e.g., constructors, destructors, and
47 /// conversion functions. Note that CXXSpecialName is used for C++ constructor,
48 /// destructor and conversion functions, but the actual kind is not stored in
49 /// CXXSpecialName. Instead we use three different FoldingSet<CXXSpecialName>
50 /// in DeclarationNameTable.
51 class alignas(IdentifierInfoAlignment) CXXSpecialNameExtra
52     : public llvm::FoldingSetNode {
53   friend class clang::DeclarationName;
54   friend class clang::DeclarationNameTable;
55 
56   /// The type associated with this declaration name.
57   QualType Type;
58 
59   /// Extra information associated with this declaration name that
60   /// can be used by the front end. All bits are really needed
61   /// so it is not possible to stash something in the low order bits.
62   void *FETokenInfo;
63 
64   CXXSpecialNameExtra(QualType QT) : Type(QT), FETokenInfo(nullptr) {}
65 
66 public:
67   void Profile(llvm::FoldingSetNodeID &ID) {
68     ID.AddPointer(Type.getAsOpaquePtr());
69   }
70 };
71 
72 /// Contains extra information for the name of a C++ deduction guide.
73 class alignas(IdentifierInfoAlignment) CXXDeductionGuideNameExtra
74     : public detail::DeclarationNameExtra,
75       public llvm::FoldingSetNode {
76   friend class clang::DeclarationName;
77   friend class clang::DeclarationNameTable;
78 
79   /// The template named by the deduction guide.
80   TemplateDecl *Template;
81 
82   /// Extra information associated with this operator name that
83   /// can be used by the front end. All bits are really needed
84   /// so it is not possible to stash something in the low order bits.
85   void *FETokenInfo;
86 
87   CXXDeductionGuideNameExtra(TemplateDecl *TD)
88       : DeclarationNameExtra(CXXDeductionGuideName), Template(TD),
89         FETokenInfo(nullptr) {}
90 
91 public:
92   void Profile(llvm::FoldingSetNodeID &ID) { ID.AddPointer(Template); }
93 };
94 
95 /// Contains extra information for the name of an overloaded operator
96 /// in C++, such as "operator+. This do not includes literal or conversion
97 /// operators. For literal operators see CXXLiteralOperatorIdName and for
98 /// conversion operators see CXXSpecialNameExtra.
99 class alignas(IdentifierInfoAlignment) CXXOperatorIdName {
100   friend class clang::DeclarationName;
101   friend class clang::DeclarationNameTable;
102 
103   /// The kind of this operator.
104   OverloadedOperatorKind Kind = OO_None;
105 
106   /// Extra information associated with this operator name that
107   /// can be used by the front end. All bits are really needed
108   /// so it is not possible to stash something in the low order bits.
109   void *FETokenInfo = nullptr;
110 };
111 
112 /// Contains the actual identifier that makes up the
113 /// name of a C++ literal operator.
114 class alignas(IdentifierInfoAlignment) CXXLiteralOperatorIdName
115     : public detail::DeclarationNameExtra,
116       public llvm::FoldingSetNode {
117   friend class clang::DeclarationName;
118   friend class clang::DeclarationNameTable;
119 
120   IdentifierInfo *ID;
121 
122   /// Extra information associated with this operator name that
123   /// can be used by the front end. All bits are really needed
124   /// so it is not possible to stash something in the low order bits.
125   void *FETokenInfo;
126 
127   CXXLiteralOperatorIdName(IdentifierInfo *II)
128       : DeclarationNameExtra(CXXLiteralOperatorName), ID(II),
129         FETokenInfo(nullptr) {}
130 
131 public:
132   void Profile(llvm::FoldingSetNodeID &FSID) { FSID.AddPointer(ID); }
133 };
134 
135 } // namespace detail
136 
137 /// The name of a declaration. In the common case, this just stores
138 /// an IdentifierInfo pointer to a normal name. However, it also provides
139 /// encodings for Objective-C selectors (optimizing zero- and one-argument
140 /// selectors, which make up 78% percent of all selectors in Cocoa.h),
141 /// special C++ names for constructors, destructors, and conversion functions,
142 /// and C++ overloaded operators.
143 class DeclarationName {
144   friend class DeclarationNameTable;
145   friend class NamedDecl;
146 
147   /// StoredNameKind represent the kind of name that is actually stored in the
148   /// upper bits of the Ptr field. This is only used internally.
149   ///
150   /// NameKind, StoredNameKind, and DeclarationNameExtra::ExtraKind
151   /// must satisfy the following properties. These properties enable
152   /// efficient conversion between the various kinds.
153   ///
154   /// * The first seven enumerators of StoredNameKind must have the same
155   ///   numerical value as the first seven enumerators of NameKind.
156   ///   This enable efficient conversion between the two enumerations
157   ///   in the usual case.
158   ///
159   /// * The enumerations values of DeclarationNameExtra::ExtraKind must start
160   ///   at zero, and correspond to the numerical value of the first non-inline
161   ///   enumeration values of NameKind minus an offset. This makes conversion
162   ///   between DeclarationNameExtra::ExtraKind and NameKind possible with
163   ///   a single addition/substraction.
164   ///
165   /// * The enumeration values of Selector::IdentifierInfoFlag must correspond
166   ///   to the relevant enumeration values of StoredNameKind.
167   ///   More specifically:
168   ///    * ZeroArg == StoredObjCZeroArgSelector,
169   ///    * OneArg == StoredObjCOneArgSelector,
170   ///    * MultiArg == StoredDeclarationNameExtra
171   ///
172   /// * PtrMask must mask the low 3 bits of Ptr.
173   enum StoredNameKind {
174     StoredIdentifier = 0,
175     StoredObjCZeroArgSelector = Selector::ZeroArg,
176     StoredObjCOneArgSelector = Selector::OneArg,
177     StoredCXXConstructorName = 3,
178     StoredCXXDestructorName = 4,
179     StoredCXXConversionFunctionName = 5,
180     StoredCXXOperatorName = 6,
181     StoredDeclarationNameExtra = Selector::MultiArg,
182     PtrMask = 7,
183     UncommonNameKindOffset = 8
184   };
185 
186   static_assert(alignof(IdentifierInfo) >= 8 &&
187                     alignof(detail::DeclarationNameExtra) >= 8 &&
188                     alignof(detail::CXXSpecialNameExtra) >= 8 &&
189                     alignof(detail::CXXOperatorIdName) >= 8 &&
190                     alignof(detail::CXXDeductionGuideNameExtra) >= 8 &&
191                     alignof(detail::CXXLiteralOperatorIdName) >= 8,
192                 "The various classes that DeclarationName::Ptr can point to"
193                 " must be at least aligned to 8 bytes!");
194 
195 public:
196   /// The kind of the name stored in this DeclarationName.
197   /// The first 7 enumeration values are stored inline and correspond
198   /// to frequently used kinds. The rest is stored in DeclarationNameExtra
199   /// and correspond to infrequently used kinds.
200   enum NameKind {
201     Identifier = StoredIdentifier,
202     ObjCZeroArgSelector = StoredObjCZeroArgSelector,
203     ObjCOneArgSelector = StoredObjCOneArgSelector,
204     CXXConstructorName = StoredCXXConstructorName,
205     CXXDestructorName = StoredCXXDestructorName,
206     CXXConversionFunctionName = StoredCXXConversionFunctionName,
207     CXXOperatorName = StoredCXXOperatorName,
208     CXXDeductionGuideName = UncommonNameKindOffset +
209                             detail::DeclarationNameExtra::CXXDeductionGuideName,
210     CXXLiteralOperatorName =
211         UncommonNameKindOffset +
212         detail::DeclarationNameExtra::CXXLiteralOperatorName,
213     CXXUsingDirective = UncommonNameKindOffset +
214                         detail::DeclarationNameExtra::CXXUsingDirective,
215     ObjCMultiArgSelector = UncommonNameKindOffset +
216                            detail::DeclarationNameExtra::ObjCMultiArgSelector
217   };
218 
219 private:
220   /// The lowest three bits of Ptr are used to express what kind of name
221   /// we're actually storing, using the values of StoredNameKind. Depending
222   /// on the kind of name this is, the upper bits of Ptr may have one
223   /// of several different meanings:
224   ///
225   ///   StoredIdentifier - The name is a normal identifier, and Ptr is
226   ///   a normal IdentifierInfo pointer.
227   ///
228   ///   StoredObjCZeroArgSelector - The name is an Objective-C
229   ///   selector with zero arguments, and Ptr is an IdentifierInfo
230   ///   pointer pointing to the selector name.
231   ///
232   ///   StoredObjCOneArgSelector - The name is an Objective-C selector
233   ///   with one argument, and Ptr is an IdentifierInfo pointer
234   ///   pointing to the selector name.
235   ///
236   ///   StoredCXXConstructorName - The name of a C++ constructor,
237   ///   Ptr points to a CXXSpecialNameExtra.
238   ///
239   ///   StoredCXXDestructorName - The name of a C++ destructor,
240   ///   Ptr points to a CXXSpecialNameExtra.
241   ///
242   ///   StoredCXXConversionFunctionName - The name of a C++ conversion function,
243   ///   Ptr points to a CXXSpecialNameExtra.
244   ///
245   ///   StoredCXXOperatorName - The name of an overloaded C++ operator,
246   ///   Ptr points to a CXXOperatorIdName.
247   ///
248   ///   StoredDeclarationNameExtra - Ptr is actually a pointer to a
249   ///   DeclarationNameExtra structure, whose first value will tell us
250   ///   whether this is an Objective-C selector, C++ deduction guide,
251   ///   C++ literal operator, or C++ using directive.
252   uintptr_t Ptr = 0;
253 
254   StoredNameKind getStoredNameKind() const {
255     return static_cast<StoredNameKind>(Ptr & PtrMask);
256   }
257 
258   void *getPtr() const { return reinterpret_cast<void *>(Ptr & ~PtrMask); }
259 
260   void setPtrAndKind(const void *P, StoredNameKind Kind) {
261     uintptr_t PAsInteger = reinterpret_cast<uintptr_t>(P);
262     assert((Kind & ~PtrMask) == 0 &&
263            "Invalid StoredNameKind in setPtrAndKind!");
264     assert((PAsInteger & PtrMask) == 0 &&
265            "Improperly aligned pointer in setPtrAndKind!");
266     Ptr = PAsInteger | Kind;
267   }
268 
269   /// Construct a declaration name from a DeclarationNameExtra.
270   DeclarationName(detail::DeclarationNameExtra *Name) {
271     setPtrAndKind(Name, StoredDeclarationNameExtra);
272   }
273 
274   /// Construct a declaration name from a CXXSpecialNameExtra.
275   DeclarationName(detail::CXXSpecialNameExtra *Name,
276                   StoredNameKind StoredKind) {
277     assert((StoredKind == StoredCXXConstructorName ||
278            StoredKind == StoredCXXDestructorName ||
279            StoredKind == StoredCXXConversionFunctionName) &&
280                "Invalid StoredNameKind when constructing a DeclarationName"
281                " from a CXXSpecialNameExtra!");
282     setPtrAndKind(Name, StoredKind);
283   }
284 
285   /// Construct a DeclarationName from a CXXOperatorIdName.
286   DeclarationName(detail::CXXOperatorIdName *Name) {
287     setPtrAndKind(Name, StoredCXXOperatorName);
288   }
289 
290   /// Assert that the stored pointer points to an IdentifierInfo and return it.
291   IdentifierInfo *castAsIdentifierInfo() const {
292     assert((getStoredNameKind() == StoredIdentifier) &&
293            "DeclarationName does not store an IdentifierInfo!");
294     return static_cast<IdentifierInfo *>(getPtr());
295   }
296 
297   /// Assert that the stored pointer points to a DeclarationNameExtra
298   /// and return it.
299   detail::DeclarationNameExtra *castAsExtra() const {
300     assert((getStoredNameKind() == StoredDeclarationNameExtra) &&
301            "DeclarationName does not store an Extra structure!");
302     return static_cast<detail::DeclarationNameExtra *>(getPtr());
303   }
304 
305   /// Assert that the stored pointer points to a CXXSpecialNameExtra
306   /// and return it.
307   detail::CXXSpecialNameExtra *castAsCXXSpecialNameExtra() const {
308     assert((getStoredNameKind() == StoredCXXConstructorName ||
309            getStoredNameKind() == StoredCXXDestructorName ||
310            getStoredNameKind() == StoredCXXConversionFunctionName) &&
311                "DeclarationName does not store a CXXSpecialNameExtra!");
312     return static_cast<detail::CXXSpecialNameExtra *>(getPtr());
313   }
314 
315   /// Assert that the stored pointer points to a CXXOperatorIdName
316   /// and return it.
317   detail::CXXOperatorIdName *castAsCXXOperatorIdName() const {
318     assert((getStoredNameKind() == StoredCXXOperatorName) &&
319            "DeclarationName does not store a CXXOperatorIdName!");
320     return static_cast<detail::CXXOperatorIdName *>(getPtr());
321   }
322 
323   /// Assert that the stored pointer points to a CXXDeductionGuideNameExtra
324   /// and return it.
325   detail::CXXDeductionGuideNameExtra *castAsCXXDeductionGuideNameExtra() const {
326     assert(getNameKind() == CXXDeductionGuideName &&
327            "DeclarationName does not store a CXXDeductionGuideNameExtra!");
328     return static_cast<detail::CXXDeductionGuideNameExtra *>(getPtr());
329   }
330 
331   /// Assert that the stored pointer points to a CXXLiteralOperatorIdName
332   /// and return it.
333   detail::CXXLiteralOperatorIdName *castAsCXXLiteralOperatorIdName() const {
334     assert(getNameKind() == CXXLiteralOperatorName &&
335            "DeclarationName does not store a CXXLiteralOperatorIdName!");
336     return static_cast<detail::CXXLiteralOperatorIdName *>(getPtr());
337   }
338 
339   /// Get and set the FETokenInfo in the less common cases where the
340   /// declaration name do not point to an identifier.
341   void *getFETokenInfoSlow() const;
342   void setFETokenInfoSlow(void *T);
343 
344 public:
345   /// Construct an empty declaration name.
346   DeclarationName() { setPtrAndKind(nullptr, StoredIdentifier); }
347 
348   /// Construct a declaration name from an IdentifierInfo *.
349   DeclarationName(const IdentifierInfo *II) {
350     setPtrAndKind(II, StoredIdentifier);
351   }
352 
353   /// Construct a declaration name from an Objective-C selector.
354   DeclarationName(Selector Sel) : Ptr(Sel.InfoPtr) {}
355 
356   /// Returns the name for all C++ using-directives.
357   static DeclarationName getUsingDirectiveName() {
358     // Single instance of DeclarationNameExtra for using-directive
359     static detail::DeclarationNameExtra UDirExtra(
360         detail::DeclarationNameExtra::CXXUsingDirective);
361     return DeclarationName(&UDirExtra);
362   }
363 
364   /// Evaluates true when this declaration name is non-empty.
365   explicit operator bool() const {
366     return getPtr() || (getStoredNameKind() != StoredIdentifier);
367   }
368 
369   /// Evaluates true when this declaration name is empty.
370   bool isEmpty() const { return !*this; }
371 
372   /// Predicate functions for querying what type of name this is.
373   bool isIdentifier() const { return getStoredNameKind() == StoredIdentifier; }
374   bool isObjCZeroArgSelector() const {
375     return getStoredNameKind() == StoredObjCZeroArgSelector;
376   }
377   bool isObjCOneArgSelector() const {
378     return getStoredNameKind() == StoredObjCOneArgSelector;
379   }
380 
381   /// Determine what kind of name this is.
382   NameKind getNameKind() const {
383     // We rely on the fact that the first 7 NameKind and StoredNameKind
384     // have the same numerical value. This makes the usual case efficient.
385     StoredNameKind StoredKind = getStoredNameKind();
386     if (StoredKind != StoredDeclarationNameExtra)
387       return static_cast<NameKind>(StoredKind);
388     // We have to consult DeclarationNameExtra. We rely on the fact that the
389     // enumeration values of ExtraKind correspond to the enumeration values of
390     // NameKind minus an offset of UncommonNameKindOffset.
391     unsigned ExtraKind = castAsExtra()->getKind();
392     return static_cast<NameKind>(UncommonNameKindOffset + ExtraKind);
393   }
394 
395   /// Determines whether the name itself is dependent, e.g., because it
396   /// involves a C++ type that is itself dependent.
397   ///
398   /// Note that this does not capture all of the notions of "dependent name",
399   /// because an identifier can be a dependent name if it is used as the
400   /// callee in a call expression with dependent arguments.
401   bool isDependentName() const;
402 
403   /// Retrieve the human-readable string for this name.
404   std::string getAsString() const;
405 
406   /// Retrieve the IdentifierInfo * stored in this declaration name,
407   /// or null if this declaration name isn't a simple identifier.
408   IdentifierInfo *getAsIdentifierInfo() const {
409     if (isIdentifier())
410       return castAsIdentifierInfo();
411     return nullptr;
412   }
413 
414   /// Get the representation of this declaration name as an opaque integer.
415   uintptr_t getAsOpaqueInteger() const { return Ptr; }
416 
417   /// Get the representation of this declaration name as an opaque pointer.
418   void *getAsOpaquePtr() const { return reinterpret_cast<void *>(Ptr); }
419 
420   /// Get a declaration name from an opaque pointer returned by getAsOpaquePtr.
421   static DeclarationName getFromOpaquePtr(void *P) {
422     DeclarationName N;
423     N.Ptr = reinterpret_cast<uintptr_t>(P);
424     return N;
425   }
426 
427   /// Get a declaration name from an opaque integer
428   /// returned by getAsOpaqueInteger.
429   static DeclarationName getFromOpaqueInteger(uintptr_t P) {
430     DeclarationName N;
431     N.Ptr = P;
432     return N;
433   }
434 
435   /// If this name is one of the C++ names (of a constructor, destructor,
436   /// or conversion function), return the type associated with that name.
437   QualType getCXXNameType() const {
438     if (getStoredNameKind() == StoredCXXConstructorName ||
439         getStoredNameKind() == StoredCXXDestructorName ||
440         getStoredNameKind() == StoredCXXConversionFunctionName) {
441       assert(getPtr() && "getCXXNameType on a null DeclarationName!");
442       return castAsCXXSpecialNameExtra()->Type;
443     }
444     return QualType();
445   }
446 
447   /// If this name is the name of a C++ deduction guide, return the
448   /// template associated with that name.
449   TemplateDecl *getCXXDeductionGuideTemplate() const {
450     if (getNameKind() == CXXDeductionGuideName) {
451       assert(getPtr() &&
452              "getCXXDeductionGuideTemplate on a null DeclarationName!");
453       return castAsCXXDeductionGuideNameExtra()->Template;
454     }
455     return nullptr;
456   }
457 
458   /// If this name is the name of an overloadable operator in C++
459   /// (e.g., @c operator+), retrieve the kind of overloaded operator.
460   OverloadedOperatorKind getCXXOverloadedOperator() const {
461     if (getStoredNameKind() == StoredCXXOperatorName) {
462       assert(getPtr() && "getCXXOverloadedOperator on a null DeclarationName!");
463       return castAsCXXOperatorIdName()->Kind;
464     }
465     return OO_None;
466   }
467 
468   /// If this name is the name of a literal operator,
469   /// retrieve the identifier associated with it.
470   IdentifierInfo *getCXXLiteralIdentifier() const {
471     if (getNameKind() == CXXLiteralOperatorName) {
472       assert(getPtr() && "getCXXLiteralIdentifier on a null DeclarationName!");
473       return castAsCXXLiteralOperatorIdName()->ID;
474     }
475     return nullptr;
476   }
477 
478   /// Get the Objective-C selector stored in this declaration name.
479   Selector getObjCSelector() const {
480     assert((getNameKind() == ObjCZeroArgSelector ||
481             getNameKind() == ObjCOneArgSelector ||
482             getNameKind() == ObjCMultiArgSelector || !getPtr()) &&
483            "Not a selector!");
484     return Selector(Ptr);
485   }
486 
487   /// Get and set FETokenInfo. The language front-end is allowed to associate
488   /// arbitrary metadata with some kinds of declaration names, including normal
489   /// identifiers and C++ constructors, destructors, and conversion functions.
490   void *getFETokenInfo() const {
491     assert(getPtr() && "getFETokenInfo on an empty DeclarationName!");
492     if (getStoredNameKind() == StoredIdentifier)
493       return castAsIdentifierInfo()->getFETokenInfo();
494     return getFETokenInfoSlow();
495   }
496 
497   void setFETokenInfo(void *T) {
498     assert(getPtr() && "setFETokenInfo on an empty DeclarationName!");
499     if (getStoredNameKind() == StoredIdentifier)
500       castAsIdentifierInfo()->setFETokenInfo(T);
501     else
502       setFETokenInfoSlow(T);
503   }
504 
505   /// Determine whether the specified names are identical.
506   friend bool operator==(DeclarationName LHS, DeclarationName RHS) {
507     return LHS.Ptr == RHS.Ptr;
508   }
509 
510   /// Determine whether the specified names are different.
511   friend bool operator!=(DeclarationName LHS, DeclarationName RHS) {
512     return LHS.Ptr != RHS.Ptr;
513   }
514 
515   static DeclarationName getEmptyMarker() {
516     DeclarationName Name;
517     Name.Ptr = uintptr_t(-1);
518     return Name;
519   }
520 
521   static DeclarationName getTombstoneMarker() {
522     DeclarationName Name;
523     Name.Ptr = uintptr_t(-2);
524     return Name;
525   }
526 
527   static int compare(DeclarationName LHS, DeclarationName RHS);
528 
529   void print(raw_ostream &OS, const PrintingPolicy &Policy) const;
530 
531   void dump() const;
532 };
533 
534 raw_ostream &operator<<(raw_ostream &OS, DeclarationName N);
535 
536 /// Ordering on two declaration names. If both names are identifiers,
537 /// this provides a lexicographical ordering.
538 inline bool operator<(DeclarationName LHS, DeclarationName RHS) {
539   return DeclarationName::compare(LHS, RHS) < 0;
540 }
541 
542 /// Ordering on two declaration names. If both names are identifiers,
543 /// this provides a lexicographical ordering.
544 inline bool operator>(DeclarationName LHS, DeclarationName RHS) {
545   return DeclarationName::compare(LHS, RHS) > 0;
546 }
547 
548 /// Ordering on two declaration names. If both names are identifiers,
549 /// this provides a lexicographical ordering.
550 inline bool operator<=(DeclarationName LHS, DeclarationName RHS) {
551   return DeclarationName::compare(LHS, RHS) <= 0;
552 }
553 
554 /// Ordering on two declaration names. If both names are identifiers,
555 /// this provides a lexicographical ordering.
556 inline bool operator>=(DeclarationName LHS, DeclarationName RHS) {
557   return DeclarationName::compare(LHS, RHS) >= 0;
558 }
559 
560 /// DeclarationNameTable is used to store and retrieve DeclarationName
561 /// instances for the various kinds of declaration names, e.g., normal
562 /// identifiers, C++ constructor names, etc. This class contains
563 /// uniqued versions of each of the C++ special names, which can be
564 /// retrieved using its member functions (e.g., getCXXConstructorName).
565 class DeclarationNameTable {
566   /// Used to allocate elements in the FoldingSets below.
567   const ASTContext &Ctx;
568 
569   /// Manage the uniqued CXXSpecialNameExtra representing C++ constructors.
570   /// getCXXConstructorName and getCXXSpecialName can be used to obtain
571   /// a DeclarationName from the corresponding type of the constructor.
572   llvm::FoldingSet<detail::CXXSpecialNameExtra> CXXConstructorNames;
573 
574   /// Manage the uniqued CXXSpecialNameExtra representing C++ destructors.
575   /// getCXXDestructorName and getCXXSpecialName can be used to obtain
576   /// a DeclarationName from the corresponding type of the destructor.
577   llvm::FoldingSet<detail::CXXSpecialNameExtra> CXXDestructorNames;
578 
579   /// Manage the uniqued CXXSpecialNameExtra representing C++ conversion
580   /// functions. getCXXConversionFunctionName and getCXXSpecialName can be
581   /// used to obtain a DeclarationName from the corresponding type of the
582   /// conversion function.
583   llvm::FoldingSet<detail::CXXSpecialNameExtra> CXXConversionFunctionNames;
584 
585   /// Manage the uniqued CXXOperatorIdName, which contain extra information
586   /// for the name of overloaded C++ operators. getCXXOperatorName
587   /// can be used to obtain a DeclarationName from the operator kind.
588   detail::CXXOperatorIdName CXXOperatorNames[NUM_OVERLOADED_OPERATORS];
589 
590   /// Manage the uniqued CXXLiteralOperatorIdName, which contain extra
591   /// information for the name of C++ literal operators.
592   /// getCXXLiteralOperatorName can be used to obtain a DeclarationName
593   /// from the corresponding IdentifierInfo.
594   llvm::FoldingSet<detail::CXXLiteralOperatorIdName> CXXLiteralOperatorNames;
595 
596   /// Manage the uniqued CXXDeductionGuideNameExtra, which contain
597   /// extra information for the name of a C++ deduction guide.
598   /// getCXXDeductionGuideName can be used to obtain a DeclarationName
599   /// from the corresponding template declaration.
600   llvm::FoldingSet<detail::CXXDeductionGuideNameExtra> CXXDeductionGuideNames;
601 
602 public:
603   DeclarationNameTable(const ASTContext &C);
604   DeclarationNameTable(const DeclarationNameTable &) = delete;
605   DeclarationNameTable &operator=(const DeclarationNameTable &) = delete;
606   DeclarationNameTable(DeclarationNameTable &&) = delete;
607   DeclarationNameTable &operator=(DeclarationNameTable &&) = delete;
608   ~DeclarationNameTable() = default;
609 
610   /// Create a declaration name that is a simple identifier.
611   DeclarationName getIdentifier(const IdentifierInfo *ID) {
612     return DeclarationName(ID);
613   }
614 
615   /// Returns the name of a C++ constructor for the given Type.
616   DeclarationName getCXXConstructorName(CanQualType Ty);
617 
618   /// Returns the name of a C++ destructor for the given Type.
619   DeclarationName getCXXDestructorName(CanQualType Ty);
620 
621   /// Returns the name of a C++ deduction guide for the given template.
622   DeclarationName getCXXDeductionGuideName(TemplateDecl *TD);
623 
624   /// Returns the name of a C++ conversion function for the given Type.
625   DeclarationName getCXXConversionFunctionName(CanQualType Ty);
626 
627   /// Returns a declaration name for special kind of C++ name,
628   /// e.g., for a constructor, destructor, or conversion function.
629   /// Kind must be one of:
630   ///   * DeclarationName::CXXConstructorName,
631   ///   * DeclarationName::CXXDestructorName or
632   ///   * DeclarationName::CXXConversionFunctionName
633   DeclarationName getCXXSpecialName(DeclarationName::NameKind Kind,
634                                     CanQualType Ty);
635 
636   /// Get the name of the overloadable C++ operator corresponding to Op.
637   DeclarationName getCXXOperatorName(OverloadedOperatorKind Op) {
638     return DeclarationName(&CXXOperatorNames[Op]);
639   }
640 
641   /// Get the name of the literal operator function with II as the identifier.
642   DeclarationName getCXXLiteralOperatorName(IdentifierInfo *II);
643 };
644 
645 /// DeclarationNameLoc - Additional source/type location info
646 /// for a declaration name. Needs a DeclarationName in order
647 /// to be interpreted correctly.
648 class DeclarationNameLoc {
649   // The source location for identifier stored elsewhere.
650   // struct {} Identifier;
651 
652   // Type info for constructors, destructors and conversion functions.
653   // Locations (if any) for the tilde (destructor) or operator keyword
654   // (conversion) are stored elsewhere.
655   struct NT {
656     TypeSourceInfo *TInfo;
657   };
658 
659   // The location (if any) of the operator keyword is stored elsewhere.
660   struct CXXOpName {
661     SourceLocation::UIntTy BeginOpNameLoc;
662     SourceLocation::UIntTy EndOpNameLoc;
663   };
664 
665   // The location (if any) of the operator keyword is stored elsewhere.
666   struct CXXLitOpName {
667     SourceLocation::UIntTy OpNameLoc;
668   };
669 
670   // struct {} CXXUsingDirective;
671   // struct {} ObjCZeroArgSelector;
672   // struct {} ObjCOneArgSelector;
673   // struct {} ObjCMultiArgSelector;
674   union {
675     struct NT NamedType;
676     struct CXXOpName CXXOperatorName;
677     struct CXXLitOpName CXXLiteralOperatorName;
678   };
679 
680   void setNamedTypeLoc(TypeSourceInfo *TInfo) { NamedType.TInfo = TInfo; }
681 
682   void setCXXOperatorNameRange(SourceRange Range) {
683     CXXOperatorName.BeginOpNameLoc = Range.getBegin().getRawEncoding();
684     CXXOperatorName.EndOpNameLoc = Range.getEnd().getRawEncoding();
685   }
686 
687   void setCXXLiteralOperatorNameLoc(SourceLocation Loc) {
688     CXXLiteralOperatorName.OpNameLoc = Loc.getRawEncoding();
689   }
690 
691 public:
692   DeclarationNameLoc(DeclarationName Name);
693   // FIXME: this should go away once all DNLocs are properly initialized.
694   DeclarationNameLoc() { memset((void*) this, 0, sizeof(*this)); }
695 
696   /// Returns the source type info. Assumes that the object stores location
697   /// information of a constructor, destructor or conversion operator.
698   TypeSourceInfo *getNamedTypeInfo() const { return NamedType.TInfo; }
699 
700   /// Return the beginning location of the getCXXOperatorNameRange() range.
701   SourceLocation getCXXOperatorNameBeginLoc() const {
702     return SourceLocation::getFromRawEncoding(CXXOperatorName.BeginOpNameLoc);
703   }
704 
705   /// Return the end location of the getCXXOperatorNameRange() range.
706   SourceLocation getCXXOperatorNameEndLoc() const {
707     return SourceLocation::getFromRawEncoding(CXXOperatorName.EndOpNameLoc);
708   }
709 
710   /// Return the range of the operator name (without the operator keyword).
711   /// Assumes that the object stores location information of a (non-literal)
712   /// operator.
713   SourceRange getCXXOperatorNameRange() const {
714     return SourceRange(getCXXOperatorNameBeginLoc(),
715                        getCXXOperatorNameEndLoc());
716   }
717 
718   /// Return the location of the literal operator name (without the operator
719   /// keyword). Assumes that the object stores location information of a literal
720   /// operator.
721   SourceLocation getCXXLiteralOperatorNameLoc() const {
722     return SourceLocation::getFromRawEncoding(CXXLiteralOperatorName.OpNameLoc);
723   }
724 
725   /// Construct location information for a constructor, destructor or conversion
726   /// operator.
727   static DeclarationNameLoc makeNamedTypeLoc(TypeSourceInfo *TInfo) {
728     DeclarationNameLoc DNL;
729     DNL.setNamedTypeLoc(TInfo);
730     return DNL;
731   }
732 
733   /// Construct location information for a non-literal C++ operator.
734   static DeclarationNameLoc makeCXXOperatorNameLoc(SourceLocation BeginLoc,
735                                                    SourceLocation EndLoc) {
736     return makeCXXOperatorNameLoc(SourceRange(BeginLoc, EndLoc));
737   }
738 
739   /// Construct location information for a non-literal C++ operator.
740   static DeclarationNameLoc makeCXXOperatorNameLoc(SourceRange Range) {
741     DeclarationNameLoc DNL;
742     DNL.setCXXOperatorNameRange(Range);
743     return DNL;
744   }
745 
746   /// Construct location information for a literal C++ operator.
747   static DeclarationNameLoc makeCXXLiteralOperatorNameLoc(SourceLocation Loc) {
748     DeclarationNameLoc DNL;
749     DNL.setCXXLiteralOperatorNameLoc(Loc);
750     return DNL;
751   }
752 };
753 
754 /// DeclarationNameInfo - A collector data type for bundling together
755 /// a DeclarationName and the correspnding source/type location info.
756 struct DeclarationNameInfo {
757 private:
758   /// Name - The declaration name, also encoding name kind.
759   DeclarationName Name;
760 
761   /// Loc - The main source location for the declaration name.
762   SourceLocation NameLoc;
763 
764   /// Info - Further source/type location info for special kinds of names.
765   DeclarationNameLoc LocInfo;
766 
767 public:
768   // FIXME: remove it.
769   DeclarationNameInfo() = default;
770 
771   DeclarationNameInfo(DeclarationName Name, SourceLocation NameLoc)
772       : Name(Name), NameLoc(NameLoc), LocInfo(Name) {}
773 
774   DeclarationNameInfo(DeclarationName Name, SourceLocation NameLoc,
775                       DeclarationNameLoc LocInfo)
776       : Name(Name), NameLoc(NameLoc), LocInfo(LocInfo) {}
777 
778   /// getName - Returns the embedded declaration name.
779   DeclarationName getName() const { return Name; }
780 
781   /// setName - Sets the embedded declaration name.
782   void setName(DeclarationName N) { Name = N; }
783 
784   /// getLoc - Returns the main location of the declaration name.
785   SourceLocation getLoc() const { return NameLoc; }
786 
787   /// setLoc - Sets the main location of the declaration name.
788   void setLoc(SourceLocation L) { NameLoc = L; }
789 
790   const DeclarationNameLoc &getInfo() const { return LocInfo; }
791   void setInfo(const DeclarationNameLoc &Info) { LocInfo = Info; }
792 
793   /// getNamedTypeInfo - Returns the source type info associated to
794   /// the name. Assumes it is a constructor, destructor or conversion.
795   TypeSourceInfo *getNamedTypeInfo() const {
796     if (Name.getNameKind() != DeclarationName::CXXConstructorName &&
797         Name.getNameKind() != DeclarationName::CXXDestructorName &&
798         Name.getNameKind() != DeclarationName::CXXConversionFunctionName)
799       return nullptr;
800     return LocInfo.getNamedTypeInfo();
801   }
802 
803   /// setNamedTypeInfo - Sets the source type info associated to
804   /// the name. Assumes it is a constructor, destructor or conversion.
805   void setNamedTypeInfo(TypeSourceInfo *TInfo) {
806     assert(Name.getNameKind() == DeclarationName::CXXConstructorName ||
807            Name.getNameKind() == DeclarationName::CXXDestructorName ||
808            Name.getNameKind() == DeclarationName::CXXConversionFunctionName);
809     LocInfo = DeclarationNameLoc::makeNamedTypeLoc(TInfo);
810   }
811 
812   /// getCXXOperatorNameRange - Gets the range of the operator name
813   /// (without the operator keyword). Assumes it is a (non-literal) operator.
814   SourceRange getCXXOperatorNameRange() const {
815     if (Name.getNameKind() != DeclarationName::CXXOperatorName)
816       return SourceRange();
817     return LocInfo.getCXXOperatorNameRange();
818   }
819 
820   /// setCXXOperatorNameRange - Sets the range of the operator name
821   /// (without the operator keyword). Assumes it is a C++ operator.
822   void setCXXOperatorNameRange(SourceRange R) {
823     assert(Name.getNameKind() == DeclarationName::CXXOperatorName);
824     LocInfo = DeclarationNameLoc::makeCXXOperatorNameLoc(R);
825   }
826 
827   /// getCXXLiteralOperatorNameLoc - Returns the location of the literal
828   /// operator name (not the operator keyword).
829   /// Assumes it is a literal operator.
830   SourceLocation getCXXLiteralOperatorNameLoc() const {
831     if (Name.getNameKind() != DeclarationName::CXXLiteralOperatorName)
832       return SourceLocation();
833     return LocInfo.getCXXLiteralOperatorNameLoc();
834   }
835 
836   /// setCXXLiteralOperatorNameLoc - Sets the location of the literal
837   /// operator name (not the operator keyword).
838   /// Assumes it is a literal operator.
839   void setCXXLiteralOperatorNameLoc(SourceLocation Loc) {
840     assert(Name.getNameKind() == DeclarationName::CXXLiteralOperatorName);
841     LocInfo = DeclarationNameLoc::makeCXXLiteralOperatorNameLoc(Loc);
842   }
843 
844   /// Determine whether this name involves a template parameter.
845   bool isInstantiationDependent() const;
846 
847   /// Determine whether this name contains an unexpanded
848   /// parameter pack.
849   bool containsUnexpandedParameterPack() const;
850 
851   /// getAsString - Retrieve the human-readable string for this name.
852   std::string getAsString() const;
853 
854   /// printName - Print the human-readable name to a stream.
855   void printName(raw_ostream &OS, PrintingPolicy Policy) const;
856 
857   /// getBeginLoc - Retrieve the location of the first token.
858   SourceLocation getBeginLoc() const { return NameLoc; }
859 
860   /// getSourceRange - The range of the declaration name.
861   SourceRange getSourceRange() const LLVM_READONLY {
862     return SourceRange(getBeginLoc(), getEndLoc());
863   }
864 
865   SourceLocation getEndLoc() const LLVM_READONLY {
866     SourceLocation EndLoc = getEndLocPrivate();
867     return EndLoc.isValid() ? EndLoc : getBeginLoc();
868   }
869 
870 private:
871   SourceLocation getEndLocPrivate() const;
872 };
873 
874 /// Insertion operator for partial diagnostics.  This allows binding
875 /// DeclarationName's into a partial diagnostic with <<.
876 inline const StreamingDiagnostic &operator<<(const StreamingDiagnostic &PD,
877                                              DeclarationName N) {
878   PD.AddTaggedVal(N.getAsOpaqueInteger(),
879                   DiagnosticsEngine::ak_declarationname);
880   return PD;
881 }
882 
883 raw_ostream &operator<<(raw_ostream &OS, DeclarationNameInfo DNInfo);
884 
885 } // namespace clang
886 
887 namespace llvm {
888 
889 /// Define DenseMapInfo so that DeclarationNames can be used as keys
890 /// in DenseMap and DenseSets.
891 template<>
892 struct DenseMapInfo<clang::DeclarationName> {
893   static inline clang::DeclarationName getEmptyKey() {
894     return clang::DeclarationName::getEmptyMarker();
895   }
896 
897   static inline clang::DeclarationName getTombstoneKey() {
898     return clang::DeclarationName::getTombstoneMarker();
899   }
900 
901   static unsigned getHashValue(clang::DeclarationName Name) {
902     return DenseMapInfo<void*>::getHashValue(Name.getAsOpaquePtr());
903   }
904 
905   static inline bool
906   isEqual(clang::DeclarationName LHS, clang::DeclarationName RHS) {
907     return LHS == RHS;
908   }
909 };
910 
911 template <> struct PointerLikeTypeTraits<clang::DeclarationName> {
912   static inline void *getAsVoidPointer(clang::DeclarationName P) {
913     return P.getAsOpaquePtr();
914   }
915   static inline clang::DeclarationName getFromVoidPointer(void *P) {
916     return clang::DeclarationName::getFromOpaquePtr(P);
917   }
918   static constexpr int NumLowBitsAvailable = 0;
919 };
920 
921 } // namespace llvm
922 
923 // The definition of AssumedTemplateStorage is factored out of TemplateName to
924 // resolve a cyclic dependency between it and DeclarationName (via Type).
925 namespace clang {
926 
927 /// A structure for storing the information associated with a name that has
928 /// been assumed to be a template name (despite finding no TemplateDecls).
929 class AssumedTemplateStorage : public UncommonTemplateNameStorage {
930   friend class ASTContext;
931 
932   AssumedTemplateStorage(DeclarationName Name)
933       : UncommonTemplateNameStorage(Assumed, 0), Name(Name) {}
934   DeclarationName Name;
935 
936 public:
937   /// Get the name of the template.
938   DeclarationName getDeclName() const { return Name; }
939 };
940 
941 } // namespace clang
942 
943 #endif // LLVM_CLANG_AST_DECLARATIONNAME_H
944