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/Basic/Diagnostic.h"
18 #include "clang/Basic/IdentifierTable.h"
19 #include "clang/Basic/PartialDiagnostic.h"
20 #include "clang/Basic/SourceLocation.h"
21 #include "llvm/ADT/DenseMapInfo.h"
22 #include "llvm/Support/Compiler.h"
23 #include "llvm/Support/type_traits.h"
32 template <typename> class CanQual;
33 class CXXDeductionGuideNameExtra;
34 class CXXLiteralOperatorIdName;
35 class CXXOperatorIdName;
37 class DeclarationNameExtra;
39 class MultiKeywordSelector;
40 enum OverloadedOperatorKind : int;
41 struct PrintingPolicy;
46 class UsingDirectiveDecl;
48 using CanQualType = CanQual<Type>;
50 /// DeclarationName - The name of a declaration. In the common case,
51 /// this just stores an IdentifierInfo pointer to a normal
52 /// name. However, it also provides encodings for Objective-C
53 /// selectors (optimizing zero- and one-argument selectors, which make
54 /// up 78% percent of all selectors in Cocoa.h) and special C++ names
55 /// for constructors, destructors, and conversion functions.
56 class DeclarationName {
58 /// NameKind - The kind of name this object contains.
66 CXXConversionFunctionName,
67 CXXDeductionGuideName,
69 CXXLiteralOperatorName,
73 static const unsigned NumNameKinds = CXXUsingDirective + 1;
76 friend class DeclarationNameTable;
77 friend class NamedDecl;
79 /// StoredNameKind - The kind of name that is actually stored in the
80 /// upper bits of the Ptr field. This is only used internally.
82 /// Note: The entries here are synchronized with the entries in Selector,
83 /// for efficient translation between the two.
86 StoredObjCZeroArgSelector = 0x01,
87 StoredObjCOneArgSelector = 0x02,
88 StoredDeclarationNameExtra = 0x03,
92 /// Ptr - The lowest two bits are used to express what kind of name
93 /// we're actually storing, using the values of NameKind. Depending
94 /// on the kind of name this is, the upper bits of Ptr may have one
95 /// of several different meanings:
97 /// StoredIdentifier - The name is a normal identifier, and Ptr is
98 /// a normal IdentifierInfo pointer.
100 /// StoredObjCZeroArgSelector - The name is an Objective-C
101 /// selector with zero arguments, and Ptr is an IdentifierInfo
102 /// pointer pointing to the selector name.
104 /// StoredObjCOneArgSelector - The name is an Objective-C selector
105 /// with one argument, and Ptr is an IdentifierInfo pointer
106 /// pointing to the selector name.
108 /// StoredDeclarationNameExtra - Ptr is actually a pointer to a
109 /// DeclarationNameExtra structure, whose first value will tell us
110 /// whether this is an Objective-C selector, C++ operator-id name,
111 /// or special C++ name.
114 // Construct a declaration name from the name of a C++ constructor,
115 // destructor, or conversion function.
116 DeclarationName(DeclarationNameExtra *Name)
117 : Ptr(reinterpret_cast<uintptr_t>(Name)) {
118 assert((Ptr & PtrMask) == 0 && "Improperly aligned DeclarationNameExtra");
119 Ptr |= StoredDeclarationNameExtra;
122 /// Construct a declaration name from a raw pointer.
123 DeclarationName(uintptr_t Ptr) : Ptr(Ptr) {}
125 /// getStoredNameKind - Return the kind of object that is stored in
127 StoredNameKind getStoredNameKind() const {
128 return static_cast<StoredNameKind>(Ptr & PtrMask);
131 /// getExtra - Get the "extra" information associated with this
132 /// multi-argument selector or C++ special name.
133 DeclarationNameExtra *getExtra() const {
134 assert(getStoredNameKind() == StoredDeclarationNameExtra &&
135 "Declaration name does not store an Extra structure");
136 return reinterpret_cast<DeclarationNameExtra *>(Ptr & ~PtrMask);
139 /// getAsCXXSpecialName - If the stored pointer is actually a
140 /// CXXSpecialName, returns a pointer to it. Otherwise, returns
142 CXXSpecialName *getAsCXXSpecialName() const {
143 NameKind Kind = getNameKind();
144 if (Kind >= CXXConstructorName && Kind <= CXXConversionFunctionName)
145 return reinterpret_cast<CXXSpecialName *>(getExtra());
149 /// If the stored pointer is actually a CXXDeductionGuideNameExtra, returns a
150 /// pointer to it. Otherwise, returns a NULL pointer.
151 CXXDeductionGuideNameExtra *getAsCXXDeductionGuideNameExtra() const {
152 if (getNameKind() == CXXDeductionGuideName)
153 return reinterpret_cast<CXXDeductionGuideNameExtra *>(getExtra());
157 /// getAsCXXOperatorIdName
158 CXXOperatorIdName *getAsCXXOperatorIdName() const {
159 if (getNameKind() == CXXOperatorName)
160 return reinterpret_cast<CXXOperatorIdName *>(getExtra());
164 CXXLiteralOperatorIdName *getAsCXXLiteralOperatorIdName() const {
165 if (getNameKind() == CXXLiteralOperatorName)
166 return reinterpret_cast<CXXLiteralOperatorIdName *>(getExtra());
170 /// getFETokenInfoAsVoidSlow - Retrieves the front end-specified pointer
171 /// for this name as a void pointer if it's not an identifier.
172 void *getFETokenInfoAsVoidSlow() const;
175 /// DeclarationName - Used to create an empty selector.
176 DeclarationName() = default;
178 // Construct a declaration name from an IdentifierInfo *.
179 DeclarationName(const IdentifierInfo *II)
180 : Ptr(reinterpret_cast<uintptr_t>(II)) {
181 assert((Ptr & PtrMask) == 0 && "Improperly aligned IdentifierInfo");
184 // Construct a declaration name from an Objective-C selector.
185 DeclarationName(Selector Sel) : Ptr(Sel.InfoPtr) {}
187 /// getUsingDirectiveName - Return name for all using-directives.
188 static DeclarationName getUsingDirectiveName();
190 // operator bool() - Evaluates true when this declaration name is
192 explicit operator bool() const {
193 return ((Ptr & PtrMask) != 0) ||
194 (reinterpret_cast<IdentifierInfo *>(Ptr & ~PtrMask));
197 /// Evaluates true when this declaration name is empty.
198 bool isEmpty() const {
202 /// Predicate functions for querying what type of name this is.
203 bool isIdentifier() const { return getStoredNameKind() == StoredIdentifier; }
204 bool isObjCZeroArgSelector() const {
205 return getStoredNameKind() == StoredObjCZeroArgSelector;
207 bool isObjCOneArgSelector() const {
208 return getStoredNameKind() == StoredObjCOneArgSelector;
211 /// getNameKind - Determine what kind of name this is.
212 NameKind getNameKind() const;
214 /// Determines whether the name itself is dependent, e.g., because it
215 /// involves a C++ type that is itself dependent.
217 /// Note that this does not capture all of the notions of "dependent name",
218 /// because an identifier can be a dependent name if it is used as the
219 /// callee in a call expression with dependent arguments.
220 bool isDependentName() const;
222 /// getNameAsString - Retrieve the human-readable string for this name.
223 std::string getAsString() const;
225 /// getAsIdentifierInfo - Retrieve the IdentifierInfo * stored in
226 /// this declaration name, or NULL if this declaration name isn't a
227 /// simple identifier.
228 IdentifierInfo *getAsIdentifierInfo() const {
230 return reinterpret_cast<IdentifierInfo *>(Ptr);
234 /// getAsOpaqueInteger - Get the representation of this declaration
235 /// name as an opaque integer.
236 uintptr_t getAsOpaqueInteger() const { return Ptr; }
238 /// getAsOpaquePtr - Get the representation of this declaration name as
239 /// an opaque pointer.
240 void *getAsOpaquePtr() const { return reinterpret_cast<void*>(Ptr); }
242 static DeclarationName getFromOpaquePtr(void *P) {
244 N.Ptr = reinterpret_cast<uintptr_t> (P);
248 static DeclarationName getFromOpaqueInteger(uintptr_t P) {
254 /// getCXXNameType - If this name is one of the C++ names (of a
255 /// constructor, destructor, or conversion function), return the
256 /// type associated with that name.
257 QualType getCXXNameType() const;
259 /// If this name is the name of a C++ deduction guide, return the
260 /// template associated with that name.
261 TemplateDecl *getCXXDeductionGuideTemplate() const;
263 /// getCXXOverloadedOperator - If this name is the name of an
264 /// overloadable operator in C++ (e.g., @c operator+), retrieve the
265 /// kind of overloaded operator.
266 OverloadedOperatorKind getCXXOverloadedOperator() const;
268 /// getCXXLiteralIdentifier - If this name is the name of a literal
269 /// operator, retrieve the identifier associated with it.
270 IdentifierInfo *getCXXLiteralIdentifier() const;
272 /// getObjCSelector - Get the Objective-C selector stored in this
273 /// declaration name.
274 Selector getObjCSelector() const {
275 assert((getNameKind() == ObjCZeroArgSelector ||
276 getNameKind() == ObjCOneArgSelector ||
277 getNameKind() == ObjCMultiArgSelector ||
278 Ptr == 0) && "Not a selector!");
279 return Selector(Ptr);
282 /// getFETokenInfo/setFETokenInfo - The language front-end is
283 /// allowed to associate arbitrary metadata with some kinds of
284 /// declaration names, including normal identifiers and C++
285 /// constructors, destructors, and conversion functions.
287 T *getFETokenInfo() const {
288 if (const IdentifierInfo *Info = getAsIdentifierInfo())
289 return Info->getFETokenInfo<T>();
290 return static_cast<T*>(getFETokenInfoAsVoidSlow());
293 void setFETokenInfo(void *T);
295 /// operator== - Determine whether the specified names are identical..
296 friend bool operator==(DeclarationName LHS, DeclarationName RHS) {
297 return LHS.Ptr == RHS.Ptr;
300 /// operator!= - Determine whether the specified names are different.
301 friend bool operator!=(DeclarationName LHS, DeclarationName RHS) {
302 return LHS.Ptr != RHS.Ptr;
305 static DeclarationName getEmptyMarker() {
306 return DeclarationName(uintptr_t(-1));
309 static DeclarationName getTombstoneMarker() {
310 return DeclarationName(uintptr_t(-2));
313 static int compare(DeclarationName LHS, DeclarationName RHS);
315 void print(raw_ostream &OS, const PrintingPolicy &Policy);
320 raw_ostream &operator<<(raw_ostream &OS, DeclarationName N);
322 /// Ordering on two declaration names. If both names are identifiers,
323 /// this provides a lexicographical ordering.
324 inline bool operator<(DeclarationName LHS, DeclarationName RHS) {
325 return DeclarationName::compare(LHS, RHS) < 0;
328 /// Ordering on two declaration names. If both names are identifiers,
329 /// this provides a lexicographical ordering.
330 inline bool operator>(DeclarationName LHS, DeclarationName RHS) {
331 return DeclarationName::compare(LHS, RHS) > 0;
334 /// Ordering on two declaration names. If both names are identifiers,
335 /// this provides a lexicographical ordering.
336 inline bool operator<=(DeclarationName LHS, DeclarationName RHS) {
337 return DeclarationName::compare(LHS, RHS) <= 0;
340 /// Ordering on two declaration names. If both names are identifiers,
341 /// this provides a lexicographical ordering.
342 inline bool operator>=(DeclarationName LHS, DeclarationName RHS) {
343 return DeclarationName::compare(LHS, RHS) >= 0;
346 /// DeclarationNameTable - Used to store and retrieve DeclarationName
347 /// instances for the various kinds of declaration names, e.g., normal
348 /// identifiers, C++ constructor names, etc. This class contains
349 /// uniqued versions of each of the C++ special names, which can be
350 /// retrieved using its member functions (e.g.,
351 /// getCXXConstructorName).
352 class DeclarationNameTable {
353 const ASTContext &Ctx;
355 // Actually a FoldingSet<CXXSpecialName> *
356 void *CXXSpecialNamesImpl;
359 CXXOperatorIdName *CXXOperatorNames;
361 // Actually a CXXOperatorIdName*
362 void *CXXLiteralOperatorNames;
364 // FoldingSet<CXXDeductionGuideNameExtra> *
365 void *CXXDeductionGuideNames;
368 DeclarationNameTable(const ASTContext &C);
369 DeclarationNameTable(const DeclarationNameTable &) = delete;
370 DeclarationNameTable &operator=(const DeclarationNameTable &) = delete;
372 ~DeclarationNameTable();
374 /// getIdentifier - Create a declaration name that is a simple
376 DeclarationName getIdentifier(const IdentifierInfo *ID) {
377 return DeclarationName(ID);
380 /// getCXXConstructorName - Returns the name of a C++ constructor
381 /// for the given Type.
382 DeclarationName getCXXConstructorName(CanQualType Ty);
384 /// getCXXDestructorName - Returns the name of a C++ destructor
385 /// for the given Type.
386 DeclarationName getCXXDestructorName(CanQualType Ty);
388 /// Returns the name of a C++ deduction guide for the given template.
389 DeclarationName getCXXDeductionGuideName(TemplateDecl *TD);
391 /// getCXXConversionFunctionName - Returns the name of a C++
392 /// conversion function for the given Type.
393 DeclarationName getCXXConversionFunctionName(CanQualType Ty);
395 /// getCXXSpecialName - Returns a declaration name for special kind
396 /// of C++ name, e.g., for a constructor, destructor, or conversion
398 DeclarationName getCXXSpecialName(DeclarationName::NameKind Kind,
401 /// getCXXOperatorName - Get the name of the overloadable C++
402 /// operator corresponding to Op.
403 DeclarationName getCXXOperatorName(OverloadedOperatorKind Op);
405 /// getCXXLiteralOperatorName - Get the name of the literal operator function
406 /// with II as the identifier.
407 DeclarationName getCXXLiteralOperatorName(IdentifierInfo *II);
410 /// DeclarationNameLoc - Additional source/type location info
411 /// for a declaration name. Needs a DeclarationName in order
412 /// to be interpreted correctly.
413 struct DeclarationNameLoc {
414 // The source location for identifier stored elsewhere.
415 // struct {} Identifier;
417 // Type info for constructors, destructors and conversion functions.
418 // Locations (if any) for the tilde (destructor) or operator keyword
419 // (conversion) are stored elsewhere.
421 TypeSourceInfo *TInfo;
424 // The location (if any) of the operator keyword is stored elsewhere.
426 unsigned BeginOpNameLoc;
427 unsigned EndOpNameLoc;
430 // The location (if any) of the operator keyword is stored elsewhere.
431 struct CXXLitOpName {
435 // struct {} CXXUsingDirective;
436 // struct {} ObjCZeroArgSelector;
437 // struct {} ObjCOneArgSelector;
438 // struct {} ObjCMultiArgSelector;
441 struct CXXOpName CXXOperatorName;
442 struct CXXLitOpName CXXLiteralOperatorName;
445 DeclarationNameLoc(DeclarationName Name);
447 // FIXME: this should go away once all DNLocs are properly initialized.
448 DeclarationNameLoc() { memset((void*) this, 0, sizeof(*this)); }
451 /// DeclarationNameInfo - A collector data type for bundling together
452 /// a DeclarationName and the correspnding source/type location info.
453 struct DeclarationNameInfo {
455 /// Name - The declaration name, also encoding name kind.
456 DeclarationName Name;
458 /// Loc - The main source location for the declaration name.
459 SourceLocation NameLoc;
461 /// Info - Further source/type location info for special kinds of names.
462 DeclarationNameLoc LocInfo;
466 DeclarationNameInfo() = default;
468 DeclarationNameInfo(DeclarationName Name, SourceLocation NameLoc)
469 : Name(Name), NameLoc(NameLoc), LocInfo(Name) {}
471 DeclarationNameInfo(DeclarationName Name, SourceLocation NameLoc,
472 DeclarationNameLoc LocInfo)
473 : Name(Name), NameLoc(NameLoc), LocInfo(LocInfo) {}
475 /// getName - Returns the embedded declaration name.
476 DeclarationName getName() const { return Name; }
478 /// setName - Sets the embedded declaration name.
479 void setName(DeclarationName N) { Name = N; }
481 /// getLoc - Returns the main location of the declaration name.
482 SourceLocation getLoc() const { return NameLoc; }
484 /// setLoc - Sets the main location of the declaration name.
485 void setLoc(SourceLocation L) { NameLoc = L; }
487 const DeclarationNameLoc &getInfo() const { return LocInfo; }
488 DeclarationNameLoc &getInfo() { return LocInfo; }
489 void setInfo(const DeclarationNameLoc &Info) { LocInfo = Info; }
491 /// getNamedTypeInfo - Returns the source type info associated to
492 /// the name. Assumes it is a constructor, destructor or conversion.
493 TypeSourceInfo *getNamedTypeInfo() const {
494 assert(Name.getNameKind() == DeclarationName::CXXConstructorName ||
495 Name.getNameKind() == DeclarationName::CXXDestructorName ||
496 Name.getNameKind() == DeclarationName::CXXConversionFunctionName);
497 return LocInfo.NamedType.TInfo;
500 /// setNamedTypeInfo - Sets the source type info associated to
501 /// the name. Assumes it is a constructor, destructor or conversion.
502 void setNamedTypeInfo(TypeSourceInfo *TInfo) {
503 assert(Name.getNameKind() == DeclarationName::CXXConstructorName ||
504 Name.getNameKind() == DeclarationName::CXXDestructorName ||
505 Name.getNameKind() == DeclarationName::CXXConversionFunctionName);
506 LocInfo.NamedType.TInfo = TInfo;
509 /// getCXXOperatorNameRange - Gets the range of the operator name
510 /// (without the operator keyword). Assumes it is a (non-literal) operator.
511 SourceRange getCXXOperatorNameRange() const {
512 assert(Name.getNameKind() == DeclarationName::CXXOperatorName);
514 SourceLocation::getFromRawEncoding(LocInfo.CXXOperatorName.BeginOpNameLoc),
515 SourceLocation::getFromRawEncoding(LocInfo.CXXOperatorName.EndOpNameLoc)
519 /// setCXXOperatorNameRange - Sets the range of the operator name
520 /// (without the operator keyword). Assumes it is a C++ operator.
521 void setCXXOperatorNameRange(SourceRange R) {
522 assert(Name.getNameKind() == DeclarationName::CXXOperatorName);
523 LocInfo.CXXOperatorName.BeginOpNameLoc = R.getBegin().getRawEncoding();
524 LocInfo.CXXOperatorName.EndOpNameLoc = R.getEnd().getRawEncoding();
527 /// getCXXLiteralOperatorNameLoc - Returns the location of the literal
528 /// operator name (not the operator keyword).
529 /// Assumes it is a literal operator.
530 SourceLocation getCXXLiteralOperatorNameLoc() const {
531 assert(Name.getNameKind() == DeclarationName::CXXLiteralOperatorName);
532 return SourceLocation::
533 getFromRawEncoding(LocInfo.CXXLiteralOperatorName.OpNameLoc);
536 /// setCXXLiteralOperatorNameLoc - Sets the location of the literal
537 /// operator name (not the operator keyword).
538 /// Assumes it is a literal operator.
539 void setCXXLiteralOperatorNameLoc(SourceLocation Loc) {
540 assert(Name.getNameKind() == DeclarationName::CXXLiteralOperatorName);
541 LocInfo.CXXLiteralOperatorName.OpNameLoc = Loc.getRawEncoding();
544 /// Determine whether this name involves a template parameter.
545 bool isInstantiationDependent() const;
547 /// Determine whether this name contains an unexpanded
549 bool containsUnexpandedParameterPack() const;
551 /// getAsString - Retrieve the human-readable string for this name.
552 std::string getAsString() const;
554 /// printName - Print the human-readable name to a stream.
555 void printName(raw_ostream &OS) const;
557 /// getBeginLoc - Retrieve the location of the first token.
558 SourceLocation getBeginLoc() const { return NameLoc; }
560 /// getSourceRange - The range of the declaration name.
561 SourceRange getSourceRange() const LLVM_READONLY {
562 return SourceRange(getLocStart(), getLocEnd());
565 SourceLocation getLocStart() const LLVM_READONLY { return getBeginLoc(); }
567 SourceLocation getLocEnd() const LLVM_READONLY { return getEndLoc(); }
568 SourceLocation getEndLoc() const LLVM_READONLY {
569 SourceLocation EndLoc = getEndLocPrivate();
570 return EndLoc.isValid() ? EndLoc : getLocStart();
574 SourceLocation getEndLocPrivate() const;
577 /// Insertion operator for diagnostics. This allows sending DeclarationName's
578 /// into a diagnostic with <<.
579 inline const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB,
581 DB.AddTaggedVal(N.getAsOpaqueInteger(),
582 DiagnosticsEngine::ak_declarationname);
586 /// Insertion operator for partial diagnostics. This allows binding
587 /// DeclarationName's into a partial diagnostic with <<.
588 inline const PartialDiagnostic &operator<<(const PartialDiagnostic &PD,
590 PD.AddTaggedVal(N.getAsOpaqueInteger(),
591 DiagnosticsEngine::ak_declarationname);
595 inline raw_ostream &operator<<(raw_ostream &OS,
596 DeclarationNameInfo DNInfo) {
597 DNInfo.printName(OS);
605 /// Define DenseMapInfo so that DeclarationNames can be used as keys
606 /// in DenseMap and DenseSets.
608 struct DenseMapInfo<clang::DeclarationName> {
609 static inline clang::DeclarationName getEmptyKey() {
610 return clang::DeclarationName::getEmptyMarker();
613 static inline clang::DeclarationName getTombstoneKey() {
614 return clang::DeclarationName::getTombstoneMarker();
617 static unsigned getHashValue(clang::DeclarationName Name) {
618 return DenseMapInfo<void*>::getHashValue(Name.getAsOpaquePtr());
622 isEqual(clang::DeclarationName LHS, clang::DeclarationName RHS) {
628 struct isPodLike<clang::DeclarationName> { static const bool value = true; };
632 #endif // LLVM_CLANG_AST_DECLARATIONNAME_H