1 //===- DeclarationName.h - Representation of declaration names --*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file declares the DeclarationName and DeclarationNameTable classes.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_CLANG_AST_DECLARATIONNAME_H
15 #define LLVM_CLANG_AST_DECLARATIONNAME_H
17 #include "clang/AST/Type.h"
18 #include "clang/Basic/Diagnostic.h"
19 #include "clang/Basic/IdentifierTable.h"
20 #include "clang/Basic/OperatorKinds.h"
21 #include "clang/Basic/PartialDiagnostic.h"
22 #include "clang/Basic/SourceLocation.h"
23 #include "llvm/ADT/DenseMapInfo.h"
24 #include "llvm/ADT/FoldingSet.h"
25 #include "llvm/Support/Compiler.h"
26 #include "llvm/Support/type_traits.h"
35 template <typename> class CanQual;
36 class DeclarationName;
37 class DeclarationNameTable;
38 class MultiKeywordSelector;
39 struct PrintingPolicy;
42 class UsingDirectiveDecl;
44 using CanQualType = CanQual<Type>;
48 /// CXXSpecialNameExtra records the type associated with one of the "special"
49 /// kinds of declaration names in C++, e.g., constructors, destructors, and
50 /// conversion functions. Note that CXXSpecialName is used for C++ constructor,
51 /// destructor and conversion functions, but the actual kind is not stored in
52 /// CXXSpecialName. Instead we use three different FoldingSet<CXXSpecialName>
53 /// in DeclarationNameTable.
54 class alignas(IdentifierInfoAlignment) CXXSpecialNameExtra
55 : public llvm::FoldingSetNode {
56 friend class clang::DeclarationName;
57 friend class clang::DeclarationNameTable;
59 /// The type associated with this declaration name.
62 /// Extra information associated with this declaration name that
63 /// can be used by the front end. All bits are really needed
64 /// so it is not possible to stash something in the low order bits.
67 CXXSpecialNameExtra(QualType QT) : Type(QT), FETokenInfo(nullptr) {}
70 void Profile(llvm::FoldingSetNodeID &ID) {
71 ID.AddPointer(Type.getAsOpaquePtr());
75 /// Contains extra information for the name of a C++ deduction guide.
76 class alignas(IdentifierInfoAlignment) CXXDeductionGuideNameExtra
77 : public detail::DeclarationNameExtra,
78 public llvm::FoldingSetNode {
79 friend class clang::DeclarationName;
80 friend class clang::DeclarationNameTable;
82 /// The template named by the deduction guide.
83 TemplateDecl *Template;
85 /// Extra information associated with this operator name that
86 /// can be used by the front end. All bits are really needed
87 /// so it is not possible to stash something in the low order bits.
90 CXXDeductionGuideNameExtra(TemplateDecl *TD)
91 : DeclarationNameExtra(CXXDeductionGuideName), Template(TD),
92 FETokenInfo(nullptr) {}
95 void Profile(llvm::FoldingSetNodeID &ID) { ID.AddPointer(Template); }
98 /// Contains extra information for the name of an overloaded operator
99 /// in C++, such as "operator+. This do not includes literal or conversion
100 /// operators. For literal operators see CXXLiteralOperatorIdName and for
101 /// conversion operators see CXXSpecialNameExtra.
102 class alignas(IdentifierInfoAlignment) CXXOperatorIdName {
103 friend class clang::DeclarationName;
104 friend class clang::DeclarationNameTable;
106 /// The kind of this operator.
107 OverloadedOperatorKind Kind = OO_None;
109 /// Extra information associated with this operator name that
110 /// can be used by the front end. All bits are really needed
111 /// so it is not possible to stash something in the low order bits.
112 void *FETokenInfo = nullptr;
115 /// Contains the actual identifier that makes up the
116 /// name of a C++ literal operator.
117 class alignas(IdentifierInfoAlignment) CXXLiteralOperatorIdName
118 : public detail::DeclarationNameExtra,
119 public llvm::FoldingSetNode {
120 friend class clang::DeclarationName;
121 friend class clang::DeclarationNameTable;
125 /// Extra information associated with this operator name that
126 /// can be used by the front end. All bits are really needed
127 /// so it is not possible to stash something in the low order bits.
130 CXXLiteralOperatorIdName(IdentifierInfo *II)
131 : DeclarationNameExtra(CXXLiteralOperatorName), ID(II),
132 FETokenInfo(nullptr) {}
135 void Profile(llvm::FoldingSetNodeID &FSID) { FSID.AddPointer(ID); }
138 } // namespace detail
140 /// The name of a declaration. In the common case, this just stores
141 /// an IdentifierInfo pointer to a normal name. However, it also provides
142 /// encodings for Objective-C selectors (optimizing zero- and one-argument
143 /// selectors, which make up 78% percent of all selectors in Cocoa.h),
144 /// special C++ names for constructors, destructors, and conversion functions,
145 /// and C++ overloaded operators.
146 class DeclarationName {
147 friend class DeclarationNameTable;
148 friend class NamedDecl;
150 /// StoredNameKind represent the kind of name that is actually stored in the
151 /// upper bits of the Ptr field. This is only used internally.
153 /// NameKind, StoredNameKind, and DeclarationNameExtra::ExtraKind
154 /// must satisfy the following properties. These properties enable
155 /// efficient conversion between the various kinds.
157 /// * The first seven enumerators of StoredNameKind must have the same
158 /// numerical value as the first seven enumerators of NameKind.
159 /// This enable efficient conversion between the two enumerations
160 /// in the usual case.
162 /// * The enumerations values of DeclarationNameExtra::ExtraKind must start
163 /// at zero, and correspond to the numerical value of the first non-inline
164 /// enumeration values of NameKind minus an offset. This makes conversion
165 /// between DeclarationNameExtra::ExtraKind and NameKind possible with
166 /// a single addition/substraction.
168 /// * The enumeration values of Selector::IdentifierInfoFlag must correspond
169 /// to the relevant enumeration values of StoredNameKind.
170 /// More specifically:
171 /// * ZeroArg == StoredObjCZeroArgSelector,
172 /// * OneArg == StoredObjCOneArgSelector,
173 /// * MultiArg == StoredDeclarationNameExtra
175 /// * PtrMask must mask the low 3 bits of Ptr.
176 enum StoredNameKind {
177 StoredIdentifier = 0,
178 StoredObjCZeroArgSelector = Selector::ZeroArg,
179 StoredObjCOneArgSelector = Selector::OneArg,
180 StoredCXXConstructorName = 3,
181 StoredCXXDestructorName = 4,
182 StoredCXXConversionFunctionName = 5,
183 StoredCXXOperatorName = 6,
184 StoredDeclarationNameExtra = Selector::MultiArg,
186 UncommonNameKindOffset = 8
189 static_assert(alignof(IdentifierInfo) >= 8 &&
190 alignof(detail::DeclarationNameExtra) >= 8 &&
191 alignof(detail::CXXSpecialNameExtra) >= 8 &&
192 alignof(detail::CXXOperatorIdName) >= 8 &&
193 alignof(detail::CXXDeductionGuideNameExtra) >= 8 &&
194 alignof(detail::CXXLiteralOperatorIdName) >= 8,
195 "The various classes that DeclarationName::Ptr can point to"
196 " must be at least aligned to 8 bytes!");
199 /// The kind of the name stored in this DeclarationName.
200 /// The first 7 enumeration values are stored inline and correspond
201 /// to frequently used kinds. The rest is stored in DeclarationNameExtra
202 /// and correspond to infrequently used kinds.
204 Identifier = StoredIdentifier,
205 ObjCZeroArgSelector = StoredObjCZeroArgSelector,
206 ObjCOneArgSelector = StoredObjCOneArgSelector,
207 CXXConstructorName = StoredCXXConstructorName,
208 CXXDestructorName = StoredCXXDestructorName,
209 CXXConversionFunctionName = StoredCXXConversionFunctionName,
210 CXXOperatorName = StoredCXXOperatorName,
211 CXXDeductionGuideName = UncommonNameKindOffset +
212 detail::DeclarationNameExtra::CXXDeductionGuideName,
213 CXXLiteralOperatorName =
214 UncommonNameKindOffset +
215 detail::DeclarationNameExtra::CXXLiteralOperatorName,
216 CXXUsingDirective = UncommonNameKindOffset +
217 detail::DeclarationNameExtra::CXXUsingDirective,
218 ObjCMultiArgSelector = UncommonNameKindOffset +
219 detail::DeclarationNameExtra::ObjCMultiArgSelector
223 /// The lowest three bits of Ptr are used to express what kind of name
224 /// we're actually storing, using the values of StoredNameKind. Depending
225 /// on the kind of name this is, the upper bits of Ptr may have one
226 /// of several different meanings:
228 /// StoredIdentifier - The name is a normal identifier, and Ptr is
229 /// a normal IdentifierInfo pointer.
231 /// StoredObjCZeroArgSelector - The name is an Objective-C
232 /// selector with zero arguments, and Ptr is an IdentifierInfo
233 /// pointer pointing to the selector name.
235 /// StoredObjCOneArgSelector - The name is an Objective-C selector
236 /// with one argument, and Ptr is an IdentifierInfo pointer
237 /// pointing to the selector name.
239 /// StoredCXXConstructorName - The name of a C++ constructor,
240 /// Ptr points to a CXXSpecialNameExtra.
242 /// StoredCXXDestructorName - The name of a C++ destructor,
243 /// Ptr points to a CXXSpecialNameExtra.
245 /// StoredCXXConversionFunctionName - The name of a C++ conversion function,
246 /// Ptr points to a CXXSpecialNameExtra.
248 /// StoredCXXOperatorName - The name of an overloaded C++ operator,
249 /// Ptr points to a CXXOperatorIdName.
251 /// StoredDeclarationNameExtra - Ptr is actually a pointer to a
252 /// DeclarationNameExtra structure, whose first value will tell us
253 /// whether this is an Objective-C selector, C++ deduction guide,
254 /// C++ literal operator, or C++ using directive.
257 StoredNameKind getStoredNameKind() const {
258 return static_cast<StoredNameKind>(Ptr & PtrMask);
261 void *getPtr() const { return reinterpret_cast<void *>(Ptr & ~PtrMask); }
263 void setPtrAndKind(const void *P, StoredNameKind Kind) {
264 uintptr_t PAsInteger = reinterpret_cast<uintptr_t>(P);
265 assert((Kind & ~PtrMask) == 0 &&
266 "Invalid StoredNameKind in setPtrAndKind!");
267 assert((PAsInteger & PtrMask) == 0 &&
268 "Improperly aligned pointer in setPtrAndKind!");
269 Ptr = PAsInteger | Kind;
272 /// Construct a declaration name from a DeclarationNameExtra.
273 DeclarationName(detail::DeclarationNameExtra *Name) {
274 setPtrAndKind(Name, StoredDeclarationNameExtra);
277 /// Construct a declaration name from a CXXSpecialNameExtra.
278 DeclarationName(detail::CXXSpecialNameExtra *Name,
279 StoredNameKind StoredKind) {
280 assert((StoredKind == StoredCXXConstructorName ||
281 StoredKind == StoredCXXDestructorName ||
282 StoredKind == StoredCXXConversionFunctionName) &&
283 "Invalid StoredNameKind when constructing a DeclarationName"
284 " from a CXXSpecialNameExtra!");
285 setPtrAndKind(Name, StoredKind);
288 /// Construct a DeclarationName from a CXXOperatorIdName.
289 DeclarationName(detail::CXXOperatorIdName *Name) {
290 setPtrAndKind(Name, StoredCXXOperatorName);
293 /// Assert that the stored pointer points to an IdentifierInfo and return it.
294 IdentifierInfo *castAsIdentifierInfo() const {
295 assert((getStoredNameKind() == StoredIdentifier) &&
296 "DeclarationName does not store an IdentifierInfo!");
297 return static_cast<IdentifierInfo *>(getPtr());
300 /// Assert that the stored pointer points to a DeclarationNameExtra
302 detail::DeclarationNameExtra *castAsExtra() const {
303 assert((getStoredNameKind() == StoredDeclarationNameExtra) &&
304 "DeclarationName does not store an Extra structure!");
305 return static_cast<detail::DeclarationNameExtra *>(getPtr());
308 /// Assert that the stored pointer points to a CXXSpecialNameExtra
310 detail::CXXSpecialNameExtra *castAsCXXSpecialNameExtra() const {
311 assert((getStoredNameKind() == StoredCXXConstructorName ||
312 getStoredNameKind() == StoredCXXDestructorName ||
313 getStoredNameKind() == StoredCXXConversionFunctionName) &&
314 "DeclarationName does not store a CXXSpecialNameExtra!");
315 return static_cast<detail::CXXSpecialNameExtra *>(getPtr());
318 /// Assert that the stored pointer points to a CXXOperatorIdName
320 detail::CXXOperatorIdName *castAsCXXOperatorIdName() const {
321 assert((getStoredNameKind() == StoredCXXOperatorName) &&
322 "DeclarationName does not store a CXXOperatorIdName!");
323 return static_cast<detail::CXXOperatorIdName *>(getPtr());
326 /// Assert that the stored pointer points to a CXXDeductionGuideNameExtra
328 detail::CXXDeductionGuideNameExtra *castAsCXXDeductionGuideNameExtra() const {
329 assert(getNameKind() == CXXDeductionGuideName &&
330 "DeclarationName does not store a CXXDeductionGuideNameExtra!");
331 return static_cast<detail::CXXDeductionGuideNameExtra *>(getPtr());
334 /// Assert that the stored pointer points to a CXXLiteralOperatorIdName
336 detail::CXXLiteralOperatorIdName *castAsCXXLiteralOperatorIdName() const {
337 assert(getNameKind() == CXXLiteralOperatorName &&
338 "DeclarationName does not store a CXXLiteralOperatorIdName!");
339 return static_cast<detail::CXXLiteralOperatorIdName *>(getPtr());
342 /// Get and set the FETokenInfo in the less common cases where the
343 /// declaration name do not point to an identifier.
344 void *getFETokenInfoSlow() const;
345 void setFETokenInfoSlow(void *T);
348 /// Construct an empty declaration name.
349 DeclarationName() { setPtrAndKind(nullptr, StoredIdentifier); }
351 /// Construct a declaration name from an IdentifierInfo *.
352 DeclarationName(const IdentifierInfo *II) {
353 setPtrAndKind(II, StoredIdentifier);
356 /// Construct a declaration name from an Objective-C selector.
357 DeclarationName(Selector Sel) : Ptr(Sel.InfoPtr) {}
359 /// Returns the name for all C++ using-directives.
360 static DeclarationName getUsingDirectiveName() {
361 // Single instance of DeclarationNameExtra for using-directive
362 static detail::DeclarationNameExtra UDirExtra(
363 detail::DeclarationNameExtra::CXXUsingDirective);
364 return DeclarationName(&UDirExtra);
367 /// Evaluates true when this declaration name is non-empty.
368 explicit operator bool() const {
369 return getPtr() || (getStoredNameKind() != StoredIdentifier);
372 /// Evaluates true when this declaration name is empty.
373 bool isEmpty() const { return !*this; }
375 /// Predicate functions for querying what type of name this is.
376 bool isIdentifier() const { return getStoredNameKind() == StoredIdentifier; }
377 bool isObjCZeroArgSelector() const {
378 return getStoredNameKind() == StoredObjCZeroArgSelector;
380 bool isObjCOneArgSelector() const {
381 return getStoredNameKind() == StoredObjCOneArgSelector;
384 /// Determine what kind of name this is.
385 NameKind getNameKind() const {
386 // We rely on the fact that the first 7 NameKind and StoredNameKind
387 // have the same numerical value. This makes the usual case efficient.
388 StoredNameKind StoredKind = getStoredNameKind();
389 if (StoredKind != StoredDeclarationNameExtra)
390 return static_cast<NameKind>(StoredKind);
391 // We have to consult DeclarationNameExtra. We rely on the fact that the
392 // enumeration values of ExtraKind correspond to the enumeration values of
393 // NameKind minus an offset of UncommonNameKindOffset.
394 unsigned ExtraKind = castAsExtra()->getKind();
395 return static_cast<NameKind>(UncommonNameKindOffset + ExtraKind);
398 /// Determines whether the name itself is dependent, e.g., because it
399 /// involves a C++ type that is itself dependent.
401 /// Note that this does not capture all of the notions of "dependent name",
402 /// because an identifier can be a dependent name if it is used as the
403 /// callee in a call expression with dependent arguments.
404 bool isDependentName() const;
406 /// Retrieve the human-readable string for this name.
407 std::string getAsString() const;
409 /// Retrieve the IdentifierInfo * stored in this declaration name,
410 /// or null if this declaration name isn't a simple identifier.
411 IdentifierInfo *getAsIdentifierInfo() const {
413 return castAsIdentifierInfo();
417 /// Get the representation of this declaration name as an opaque integer.
418 uintptr_t getAsOpaqueInteger() const { return Ptr; }
420 /// Get the representation of this declaration name as an opaque pointer.
421 void *getAsOpaquePtr() const { return reinterpret_cast<void *>(Ptr); }
423 /// Get a declaration name from an opaque pointer returned by getAsOpaquePtr.
424 static DeclarationName getFromOpaquePtr(void *P) {
426 N.Ptr = reinterpret_cast<uintptr_t>(P);
430 /// Get a declaration name from an opaque integer
431 /// returned by getAsOpaqueInteger.
432 static DeclarationName getFromOpaqueInteger(uintptr_t P) {
438 /// If this name is one of the C++ names (of a constructor, destructor,
439 /// or conversion function), return the type associated with that name.
440 QualType getCXXNameType() const {
441 if (getStoredNameKind() == StoredCXXConstructorName ||
442 getStoredNameKind() == StoredCXXDestructorName ||
443 getStoredNameKind() == StoredCXXConversionFunctionName) {
444 assert(getPtr() && "getCXXNameType on a null DeclarationName!");
445 return castAsCXXSpecialNameExtra()->Type;
450 /// If this name is the name of a C++ deduction guide, return the
451 /// template associated with that name.
452 TemplateDecl *getCXXDeductionGuideTemplate() const {
453 if (getNameKind() == CXXDeductionGuideName) {
455 "getCXXDeductionGuideTemplate on a null DeclarationName!");
456 return castAsCXXDeductionGuideNameExtra()->Template;
461 /// If this name is the name of an overloadable operator in C++
462 /// (e.g., @c operator+), retrieve the kind of overloaded operator.
463 OverloadedOperatorKind getCXXOverloadedOperator() const {
464 if (getStoredNameKind() == StoredCXXOperatorName) {
465 assert(getPtr() && "getCXXOverloadedOperator on a null DeclarationName!");
466 return castAsCXXOperatorIdName()->Kind;
471 /// If this name is the name of a literal operator,
472 /// retrieve the identifier associated with it.
473 IdentifierInfo *getCXXLiteralIdentifier() const {
474 if (getNameKind() == CXXLiteralOperatorName) {
475 assert(getPtr() && "getCXXLiteralIdentifier on a null DeclarationName!");
476 return castAsCXXLiteralOperatorIdName()->ID;
481 /// Get the Objective-C selector stored in this declaration name.
482 Selector getObjCSelector() const {
483 assert((getNameKind() == ObjCZeroArgSelector ||
484 getNameKind() == ObjCOneArgSelector ||
485 getNameKind() == ObjCMultiArgSelector || !getPtr()) &&
487 return Selector(Ptr);
490 /// Get and set FETokenInfo. The language front-end is allowed to associate
491 /// arbitrary metadata with some kinds of declaration names, including normal
492 /// identifiers and C++ constructors, destructors, and conversion functions.
493 void *getFETokenInfo() const {
494 assert(getPtr() && "getFETokenInfo on an empty DeclarationName!");
495 if (getStoredNameKind() == StoredIdentifier)
496 return castAsIdentifierInfo()->getFETokenInfo();
497 return getFETokenInfoSlow();
500 void setFETokenInfo(void *T) {
501 assert(getPtr() && "setFETokenInfo on an empty DeclarationName!");
502 if (getStoredNameKind() == StoredIdentifier)
503 castAsIdentifierInfo()->setFETokenInfo(T);
505 setFETokenInfoSlow(T);
508 /// Determine whether the specified names are identical.
509 friend bool operator==(DeclarationName LHS, DeclarationName RHS) {
510 return LHS.Ptr == RHS.Ptr;
513 /// Determine whether the specified names are different.
514 friend bool operator!=(DeclarationName LHS, DeclarationName RHS) {
515 return LHS.Ptr != RHS.Ptr;
518 static DeclarationName getEmptyMarker() {
519 DeclarationName Name;
520 Name.Ptr = uintptr_t(-1);
524 static DeclarationName getTombstoneMarker() {
525 DeclarationName Name;
526 Name.Ptr = uintptr_t(-2);
530 static int compare(DeclarationName LHS, DeclarationName RHS);
532 void print(raw_ostream &OS, const PrintingPolicy &Policy);
537 raw_ostream &operator<<(raw_ostream &OS, DeclarationName N);
539 /// Ordering on two declaration names. If both names are identifiers,
540 /// this provides a lexicographical ordering.
541 inline bool operator<(DeclarationName LHS, DeclarationName RHS) {
542 return DeclarationName::compare(LHS, RHS) < 0;
545 /// Ordering on two declaration names. If both names are identifiers,
546 /// this provides a lexicographical ordering.
547 inline bool operator>(DeclarationName LHS, DeclarationName RHS) {
548 return DeclarationName::compare(LHS, RHS) > 0;
551 /// Ordering on two declaration names. If both names are identifiers,
552 /// this provides a lexicographical ordering.
553 inline bool operator<=(DeclarationName LHS, DeclarationName RHS) {
554 return DeclarationName::compare(LHS, RHS) <= 0;
557 /// Ordering on two declaration names. If both names are identifiers,
558 /// this provides a lexicographical ordering.
559 inline bool operator>=(DeclarationName LHS, DeclarationName RHS) {
560 return DeclarationName::compare(LHS, RHS) >= 0;
563 /// DeclarationNameTable is used to store and retrieve DeclarationName
564 /// instances for the various kinds of declaration names, e.g., normal
565 /// identifiers, C++ constructor names, etc. This class contains
566 /// uniqued versions of each of the C++ special names, which can be
567 /// retrieved using its member functions (e.g., getCXXConstructorName).
568 class DeclarationNameTable {
569 /// Used to allocate elements in the FoldingSets below.
570 const ASTContext &Ctx;
572 /// Manage the uniqued CXXSpecialNameExtra representing C++ constructors.
573 /// getCXXConstructorName and getCXXSpecialName can be used to obtain
574 /// a DeclarationName from the corresponding type of the constructor.
575 llvm::FoldingSet<detail::CXXSpecialNameExtra> CXXConstructorNames;
577 /// Manage the uniqued CXXSpecialNameExtra representing C++ destructors.
578 /// getCXXDestructorName and getCXXSpecialName can be used to obtain
579 /// a DeclarationName from the corresponding type of the destructor.
580 llvm::FoldingSet<detail::CXXSpecialNameExtra> CXXDestructorNames;
582 /// Manage the uniqued CXXSpecialNameExtra representing C++ conversion
583 /// functions. getCXXConversionFunctionName and getCXXSpecialName can be
584 /// used to obtain a DeclarationName from the corresponding type of the
585 /// conversion function.
586 llvm::FoldingSet<detail::CXXSpecialNameExtra> CXXConversionFunctionNames;
588 /// Manage the uniqued CXXOperatorIdName, which contain extra information
589 /// for the name of overloaded C++ operators. getCXXOperatorName
590 /// can be used to obtain a DeclarationName from the operator kind.
591 detail::CXXOperatorIdName CXXOperatorNames[NUM_OVERLOADED_OPERATORS];
593 /// Manage the uniqued CXXLiteralOperatorIdName, which contain extra
594 /// information for the name of C++ literal operators.
595 /// getCXXLiteralOperatorName can be used to obtain a DeclarationName
596 /// from the corresponding IdentifierInfo.
597 llvm::FoldingSet<detail::CXXLiteralOperatorIdName> CXXLiteralOperatorNames;
599 /// Manage the uniqued CXXDeductionGuideNameExtra, which contain
600 /// extra information for the name of a C++ deduction guide.
601 /// getCXXDeductionGuideName can be used to obtain a DeclarationName
602 /// from the corresponding template declaration.
603 llvm::FoldingSet<detail::CXXDeductionGuideNameExtra> CXXDeductionGuideNames;
606 DeclarationNameTable(const ASTContext &C);
607 DeclarationNameTable(const DeclarationNameTable &) = delete;
608 DeclarationNameTable &operator=(const DeclarationNameTable &) = delete;
609 DeclarationNameTable(DeclarationNameTable &&) = delete;
610 DeclarationNameTable &operator=(DeclarationNameTable &&) = delete;
611 ~DeclarationNameTable() = default;
613 /// Create a declaration name that is a simple identifier.
614 DeclarationName getIdentifier(const IdentifierInfo *ID) {
615 return DeclarationName(ID);
618 /// Returns the name of a C++ constructor for the given Type.
619 DeclarationName getCXXConstructorName(CanQualType Ty);
621 /// Returns the name of a C++ destructor for the given Type.
622 DeclarationName getCXXDestructorName(CanQualType Ty);
624 /// Returns the name of a C++ deduction guide for the given template.
625 DeclarationName getCXXDeductionGuideName(TemplateDecl *TD);
627 /// Returns the name of a C++ conversion function for the given Type.
628 DeclarationName getCXXConversionFunctionName(CanQualType Ty);
630 /// Returns a declaration name for special kind of C++ name,
631 /// e.g., for a constructor, destructor, or conversion function.
632 /// Kind must be one of:
633 /// * DeclarationName::CXXConstructorName,
634 /// * DeclarationName::CXXDestructorName or
635 /// * DeclarationName::CXXConversionFunctionName
636 DeclarationName getCXXSpecialName(DeclarationName::NameKind Kind,
639 /// Get the name of the overloadable C++ operator corresponding to Op.
640 DeclarationName getCXXOperatorName(OverloadedOperatorKind Op) {
641 return DeclarationName(&CXXOperatorNames[Op]);
644 /// Get the name of the literal operator function with II as the identifier.
645 DeclarationName getCXXLiteralOperatorName(IdentifierInfo *II);
648 /// DeclarationNameLoc - Additional source/type location info
649 /// for a declaration name. Needs a DeclarationName in order
650 /// to be interpreted correctly.
651 struct DeclarationNameLoc {
652 // The source location for identifier stored elsewhere.
653 // struct {} Identifier;
655 // Type info for constructors, destructors and conversion functions.
656 // Locations (if any) for the tilde (destructor) or operator keyword
657 // (conversion) are stored elsewhere.
659 TypeSourceInfo *TInfo;
662 // The location (if any) of the operator keyword is stored elsewhere.
664 unsigned BeginOpNameLoc;
665 unsigned EndOpNameLoc;
668 // The location (if any) of the operator keyword is stored elsewhere.
669 struct CXXLitOpName {
673 // struct {} CXXUsingDirective;
674 // struct {} ObjCZeroArgSelector;
675 // struct {} ObjCOneArgSelector;
676 // struct {} ObjCMultiArgSelector;
679 struct CXXOpName CXXOperatorName;
680 struct CXXLitOpName CXXLiteralOperatorName;
683 DeclarationNameLoc(DeclarationName Name);
685 // FIXME: this should go away once all DNLocs are properly initialized.
686 DeclarationNameLoc() { memset((void*) this, 0, sizeof(*this)); }
689 /// DeclarationNameInfo - A collector data type for bundling together
690 /// a DeclarationName and the correspnding source/type location info.
691 struct DeclarationNameInfo {
693 /// Name - The declaration name, also encoding name kind.
694 DeclarationName Name;
696 /// Loc - The main source location for the declaration name.
697 SourceLocation NameLoc;
699 /// Info - Further source/type location info for special kinds of names.
700 DeclarationNameLoc LocInfo;
704 DeclarationNameInfo() = default;
706 DeclarationNameInfo(DeclarationName Name, SourceLocation NameLoc)
707 : Name(Name), NameLoc(NameLoc), LocInfo(Name) {}
709 DeclarationNameInfo(DeclarationName Name, SourceLocation NameLoc,
710 DeclarationNameLoc LocInfo)
711 : Name(Name), NameLoc(NameLoc), LocInfo(LocInfo) {}
713 /// getName - Returns the embedded declaration name.
714 DeclarationName getName() const { return Name; }
716 /// setName - Sets the embedded declaration name.
717 void setName(DeclarationName N) { Name = N; }
719 /// getLoc - Returns the main location of the declaration name.
720 SourceLocation getLoc() const { return NameLoc; }
722 /// setLoc - Sets the main location of the declaration name.
723 void setLoc(SourceLocation L) { NameLoc = L; }
725 const DeclarationNameLoc &getInfo() const { return LocInfo; }
726 DeclarationNameLoc &getInfo() { return LocInfo; }
727 void setInfo(const DeclarationNameLoc &Info) { LocInfo = Info; }
729 /// getNamedTypeInfo - Returns the source type info associated to
730 /// the name. Assumes it is a constructor, destructor or conversion.
731 TypeSourceInfo *getNamedTypeInfo() const {
732 if (Name.getNameKind() != DeclarationName::CXXConstructorName &&
733 Name.getNameKind() != DeclarationName::CXXDestructorName &&
734 Name.getNameKind() != DeclarationName::CXXConversionFunctionName)
736 return LocInfo.NamedType.TInfo;
739 /// setNamedTypeInfo - Sets the source type info associated to
740 /// the name. Assumes it is a constructor, destructor or conversion.
741 void setNamedTypeInfo(TypeSourceInfo *TInfo) {
742 assert(Name.getNameKind() == DeclarationName::CXXConstructorName ||
743 Name.getNameKind() == DeclarationName::CXXDestructorName ||
744 Name.getNameKind() == DeclarationName::CXXConversionFunctionName);
745 LocInfo.NamedType.TInfo = TInfo;
748 /// getCXXOperatorNameRange - Gets the range of the operator name
749 /// (without the operator keyword). Assumes it is a (non-literal) operator.
750 SourceRange getCXXOperatorNameRange() const {
751 if (Name.getNameKind() != DeclarationName::CXXOperatorName)
752 return SourceRange();
754 SourceLocation::getFromRawEncoding(LocInfo.CXXOperatorName.BeginOpNameLoc),
755 SourceLocation::getFromRawEncoding(LocInfo.CXXOperatorName.EndOpNameLoc)
759 /// setCXXOperatorNameRange - Sets the range of the operator name
760 /// (without the operator keyword). Assumes it is a C++ operator.
761 void setCXXOperatorNameRange(SourceRange R) {
762 assert(Name.getNameKind() == DeclarationName::CXXOperatorName);
763 LocInfo.CXXOperatorName.BeginOpNameLoc = R.getBegin().getRawEncoding();
764 LocInfo.CXXOperatorName.EndOpNameLoc = R.getEnd().getRawEncoding();
767 /// getCXXLiteralOperatorNameLoc - Returns the location of the literal
768 /// operator name (not the operator keyword).
769 /// Assumes it is a literal operator.
770 SourceLocation getCXXLiteralOperatorNameLoc() const {
771 if (Name.getNameKind() != DeclarationName::CXXLiteralOperatorName)
772 return SourceLocation();
773 return SourceLocation::
774 getFromRawEncoding(LocInfo.CXXLiteralOperatorName.OpNameLoc);
777 /// setCXXLiteralOperatorNameLoc - Sets the location of the literal
778 /// operator name (not the operator keyword).
779 /// Assumes it is a literal operator.
780 void setCXXLiteralOperatorNameLoc(SourceLocation Loc) {
781 assert(Name.getNameKind() == DeclarationName::CXXLiteralOperatorName);
782 LocInfo.CXXLiteralOperatorName.OpNameLoc = Loc.getRawEncoding();
785 /// Determine whether this name involves a template parameter.
786 bool isInstantiationDependent() const;
788 /// Determine whether this name contains an unexpanded
790 bool containsUnexpandedParameterPack() const;
792 /// getAsString - Retrieve the human-readable string for this name.
793 std::string getAsString() const;
795 /// printName - Print the human-readable name to a stream.
796 void printName(raw_ostream &OS) const;
798 /// getBeginLoc - Retrieve the location of the first token.
799 SourceLocation getBeginLoc() const { return NameLoc; }
801 /// getSourceRange - The range of the declaration name.
802 SourceRange getSourceRange() const LLVM_READONLY {
803 return SourceRange(getBeginLoc(), getEndLoc());
806 SourceLocation getEndLoc() const LLVM_READONLY {
807 SourceLocation EndLoc = getEndLocPrivate();
808 return EndLoc.isValid() ? EndLoc : getBeginLoc();
812 SourceLocation getEndLocPrivate() const;
815 /// Insertion operator for diagnostics. This allows sending DeclarationName's
816 /// into a diagnostic with <<.
817 inline const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB,
819 DB.AddTaggedVal(N.getAsOpaqueInteger(),
820 DiagnosticsEngine::ak_declarationname);
824 /// Insertion operator for partial diagnostics. This allows binding
825 /// DeclarationName's into a partial diagnostic with <<.
826 inline const PartialDiagnostic &operator<<(const PartialDiagnostic &PD,
828 PD.AddTaggedVal(N.getAsOpaqueInteger(),
829 DiagnosticsEngine::ak_declarationname);
833 inline raw_ostream &operator<<(raw_ostream &OS,
834 DeclarationNameInfo DNInfo) {
835 DNInfo.printName(OS);
843 /// Define DenseMapInfo so that DeclarationNames can be used as keys
844 /// in DenseMap and DenseSets.
846 struct DenseMapInfo<clang::DeclarationName> {
847 static inline clang::DeclarationName getEmptyKey() {
848 return clang::DeclarationName::getEmptyMarker();
851 static inline clang::DeclarationName getTombstoneKey() {
852 return clang::DeclarationName::getTombstoneMarker();
855 static unsigned getHashValue(clang::DeclarationName Name) {
856 return DenseMapInfo<void*>::getHashValue(Name.getAsOpaquePtr());
860 isEqual(clang::DeclarationName LHS, clang::DeclarationName RHS) {
866 struct isPodLike<clang::DeclarationName> { static const bool value = true; };
870 #endif // LLVM_CLANG_AST_DECLARATIONNAME_H