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 //===----------------------------------------------------------------------===//
13 #ifndef LLVM_CLANG_AST_DECLARATIONNAME_H
14 #define LLVM_CLANG_AST_DECLARATIONNAME_H
16 #include "clang/Basic/IdentifierTable.h"
17 #include "clang/Basic/PartialDiagnostic.h"
18 #include "llvm/Support/Compiler.h"
21 template <typename T> struct DenseMapInfo;
26 class CXXLiteralOperatorIdName;
27 class CXXOperatorIdName;
29 class DeclarationNameExtra;
31 class MultiKeywordSelector;
32 enum OverloadedOperatorKind : int;
36 class UsingDirectiveDecl;
38 template <typename> class CanQual;
39 typedef CanQual<Type> CanQualType;
41 /// DeclarationName - The name of a declaration. In the common case,
42 /// this just stores an IdentifierInfo pointer to a normal
43 /// name. However, it also provides encodings for Objective-C
44 /// selectors (optimizing zero- and one-argument selectors, which make
45 /// up 78% percent of all selectors in Cocoa.h) and special C++ names
46 /// for constructors, destructors, and conversion functions.
47 class DeclarationName {
49 /// NameKind - The kind of name this object contains.
57 CXXConversionFunctionName,
59 CXXLiteralOperatorName,
62 static const unsigned NumNameKinds = CXXUsingDirective + 1;
65 /// StoredNameKind - The kind of name that is actually stored in the
66 /// upper bits of the Ptr field. This is only used internally.
68 /// Note: The entries here are synchronized with the entries in Selector,
69 /// for efficient translation between the two.
72 StoredObjCZeroArgSelector = 0x01,
73 StoredObjCOneArgSelector = 0x02,
74 StoredDeclarationNameExtra = 0x03,
78 /// Ptr - The lowest two bits are used to express what kind of name
79 /// we're actually storing, using the values of NameKind. Depending
80 /// on the kind of name this is, the upper bits of Ptr may have one
81 /// of several different meanings:
83 /// StoredIdentifier - The name is a normal identifier, and Ptr is
84 /// a normal IdentifierInfo pointer.
86 /// StoredObjCZeroArgSelector - The name is an Objective-C
87 /// selector with zero arguments, and Ptr is an IdentifierInfo
88 /// pointer pointing to the selector name.
90 /// StoredObjCOneArgSelector - The name is an Objective-C selector
91 /// with one argument, and Ptr is an IdentifierInfo pointer
92 /// pointing to the selector name.
94 /// StoredDeclarationNameExtra - Ptr is actually a pointer to a
95 /// DeclarationNameExtra structure, whose first value will tell us
96 /// whether this is an Objective-C selector, C++ operator-id name,
97 /// or special C++ name.
100 /// getStoredNameKind - Return the kind of object that is stored in
102 StoredNameKind getStoredNameKind() const {
103 return static_cast<StoredNameKind>(Ptr & PtrMask);
106 /// getExtra - Get the "extra" information associated with this
107 /// multi-argument selector or C++ special name.
108 DeclarationNameExtra *getExtra() const {
109 assert(getStoredNameKind() == StoredDeclarationNameExtra &&
110 "Declaration name does not store an Extra structure");
111 return reinterpret_cast<DeclarationNameExtra *>(Ptr & ~PtrMask);
114 /// getAsCXXSpecialName - If the stored pointer is actually a
115 /// CXXSpecialName, returns a pointer to it. Otherwise, returns
117 CXXSpecialName *getAsCXXSpecialName() const {
118 NameKind Kind = getNameKind();
119 if (Kind >= CXXConstructorName && Kind <= CXXConversionFunctionName)
120 return reinterpret_cast<CXXSpecialName *>(Ptr & ~PtrMask);
124 /// getAsCXXOperatorIdName
125 CXXOperatorIdName *getAsCXXOperatorIdName() const {
126 if (getNameKind() == CXXOperatorName)
127 return reinterpret_cast<CXXOperatorIdName *>(Ptr & ~PtrMask);
131 CXXLiteralOperatorIdName *getAsCXXLiteralOperatorIdName() const {
132 if (getNameKind() == CXXLiteralOperatorName)
133 return reinterpret_cast<CXXLiteralOperatorIdName *>(Ptr & ~PtrMask);
137 // Construct a declaration name from the name of a C++ constructor,
138 // destructor, or conversion function.
139 DeclarationName(CXXSpecialName *Name)
140 : Ptr(reinterpret_cast<uintptr_t>(Name)) {
141 assert((Ptr & PtrMask) == 0 && "Improperly aligned CXXSpecialName");
142 Ptr |= StoredDeclarationNameExtra;
145 // Construct a declaration name from the name of a C++ overloaded
147 DeclarationName(CXXOperatorIdName *Name)
148 : Ptr(reinterpret_cast<uintptr_t>(Name)) {
149 assert((Ptr & PtrMask) == 0 && "Improperly aligned CXXOperatorId");
150 Ptr |= StoredDeclarationNameExtra;
153 DeclarationName(CXXLiteralOperatorIdName *Name)
154 : Ptr(reinterpret_cast<uintptr_t>(Name)) {
155 assert((Ptr & PtrMask) == 0 && "Improperly aligned CXXLiteralOperatorId");
156 Ptr |= StoredDeclarationNameExtra;
159 /// Construct a declaration name from a raw pointer.
160 DeclarationName(uintptr_t Ptr) : Ptr(Ptr) { }
162 friend class DeclarationNameTable;
163 friend class NamedDecl;
165 /// getFETokenInfoAsVoidSlow - Retrieves the front end-specified pointer
166 /// for this name as a void pointer if it's not an identifier.
167 void *getFETokenInfoAsVoidSlow() const;
170 /// DeclarationName - Used to create an empty selector.
171 DeclarationName() : Ptr(0) { }
173 // Construct a declaration name from an IdentifierInfo *.
174 DeclarationName(const IdentifierInfo *II)
175 : Ptr(reinterpret_cast<uintptr_t>(II)) {
176 assert((Ptr & PtrMask) == 0 && "Improperly aligned IdentifierInfo");
179 // Construct a declaration name from an Objective-C selector.
180 DeclarationName(Selector Sel) : Ptr(Sel.InfoPtr) { }
182 /// getUsingDirectiveName - Return name for all using-directives.
183 static DeclarationName getUsingDirectiveName();
185 // operator bool() - Evaluates true when this declaration name is
187 explicit operator bool() const {
188 return ((Ptr & PtrMask) != 0) ||
189 (reinterpret_cast<IdentifierInfo *>(Ptr & ~PtrMask));
192 /// \brief Evaluates true when this declaration name is empty.
193 bool isEmpty() const {
197 /// Predicate functions for querying what type of name this is.
198 bool isIdentifier() const { return getStoredNameKind() == StoredIdentifier; }
199 bool isObjCZeroArgSelector() const {
200 return getStoredNameKind() == StoredObjCZeroArgSelector;
202 bool isObjCOneArgSelector() const {
203 return getStoredNameKind() == StoredObjCOneArgSelector;
206 /// getNameKind - Determine what kind of name this is.
207 NameKind getNameKind() const;
209 /// \brief Determines whether the name itself is dependent, e.g., because it
210 /// involves a C++ type that is itself dependent.
212 /// Note that this does not capture all of the notions of "dependent name",
213 /// because an identifier can be a dependent name if it is used as the
214 /// callee in a call expression with dependent arguments.
215 bool isDependentName() const;
217 /// getNameAsString - Retrieve the human-readable string for this name.
218 std::string getAsString() const;
220 /// getAsIdentifierInfo - Retrieve the IdentifierInfo * stored in
221 /// this declaration name, or NULL if this declaration name isn't a
222 /// simple identifier.
223 IdentifierInfo *getAsIdentifierInfo() const {
225 return reinterpret_cast<IdentifierInfo *>(Ptr);
229 /// getAsOpaqueInteger - Get the representation of this declaration
230 /// name as an opaque integer.
231 uintptr_t getAsOpaqueInteger() const { return Ptr; }
233 /// getAsOpaquePtr - Get the representation of this declaration name as
234 /// an opaque pointer.
235 void *getAsOpaquePtr() const { return reinterpret_cast<void*>(Ptr); }
237 static DeclarationName getFromOpaquePtr(void *P) {
239 N.Ptr = reinterpret_cast<uintptr_t> (P);
243 static DeclarationName getFromOpaqueInteger(uintptr_t P) {
249 /// getCXXNameType - If this name is one of the C++ names (of a
250 /// constructor, destructor, or conversion function), return the
251 /// type associated with that name.
252 QualType getCXXNameType() const;
254 /// getCXXOverloadedOperator - If this name is the name of an
255 /// overloadable operator in C++ (e.g., @c operator+), retrieve the
256 /// kind of overloaded operator.
257 OverloadedOperatorKind getCXXOverloadedOperator() const;
259 /// getCXXLiteralIdentifier - If this name is the name of a literal
260 /// operator, retrieve the identifier associated with it.
261 IdentifierInfo *getCXXLiteralIdentifier() const;
263 /// getObjCSelector - Get the Objective-C selector stored in this
264 /// declaration name.
265 Selector getObjCSelector() const {
266 assert((getNameKind() == ObjCZeroArgSelector ||
267 getNameKind() == ObjCOneArgSelector ||
268 getNameKind() == ObjCMultiArgSelector ||
269 Ptr == 0) && "Not a selector!");
270 return Selector(Ptr);
273 /// getFETokenInfo/setFETokenInfo - The language front-end is
274 /// allowed to associate arbitrary metadata with some kinds of
275 /// declaration names, including normal identifiers and C++
276 /// constructors, destructors, and conversion functions.
278 T *getFETokenInfo() const {
279 if (const IdentifierInfo *Info = getAsIdentifierInfo())
280 return Info->getFETokenInfo<T>();
281 return static_cast<T*>(getFETokenInfoAsVoidSlow());
284 void setFETokenInfo(void *T);
286 /// operator== - Determine whether the specified names are identical..
287 friend bool operator==(DeclarationName LHS, DeclarationName RHS) {
288 return LHS.Ptr == RHS.Ptr;
291 /// operator!= - Determine whether the specified names are different.
292 friend bool operator!=(DeclarationName LHS, DeclarationName RHS) {
293 return LHS.Ptr != RHS.Ptr;
296 static DeclarationName getEmptyMarker() {
297 return DeclarationName(uintptr_t(-1));
300 static DeclarationName getTombstoneMarker() {
301 return DeclarationName(uintptr_t(-2));
304 static int compare(DeclarationName LHS, DeclarationName RHS);
309 raw_ostream &operator<<(raw_ostream &OS, DeclarationName N);
311 /// Ordering on two declaration names. If both names are identifiers,
312 /// this provides a lexicographical ordering.
313 inline bool operator<(DeclarationName LHS, DeclarationName RHS) {
314 return DeclarationName::compare(LHS, RHS) < 0;
317 /// Ordering on two declaration names. If both names are identifiers,
318 /// this provides a lexicographical ordering.
319 inline bool operator>(DeclarationName LHS, DeclarationName RHS) {
320 return DeclarationName::compare(LHS, RHS) > 0;
323 /// Ordering on two declaration names. If both names are identifiers,
324 /// this provides a lexicographical ordering.
325 inline bool operator<=(DeclarationName LHS, DeclarationName RHS) {
326 return DeclarationName::compare(LHS, RHS) <= 0;
329 /// Ordering on two declaration names. If both names are identifiers,
330 /// this provides a lexicographical ordering.
331 inline bool operator>=(DeclarationName LHS, DeclarationName RHS) {
332 return DeclarationName::compare(LHS, RHS) >= 0;
335 /// DeclarationNameTable - Used to store and retrieve DeclarationName
336 /// instances for the various kinds of declaration names, e.g., normal
337 /// identifiers, C++ constructor names, etc. This class contains
338 /// uniqued versions of each of the C++ special names, which can be
339 /// retrieved using its member functions (e.g.,
340 /// getCXXConstructorName).
341 class DeclarationNameTable {
342 const ASTContext &Ctx;
343 void *CXXSpecialNamesImpl; // Actually a FoldingSet<CXXSpecialName> *
344 CXXOperatorIdName *CXXOperatorNames; // Operator names
345 void *CXXLiteralOperatorNames; // Actually a CXXOperatorIdName*
347 DeclarationNameTable(const DeclarationNameTable&) = delete;
348 void operator=(const DeclarationNameTable&) = delete;
351 DeclarationNameTable(const ASTContext &C);
352 ~DeclarationNameTable();
354 /// getIdentifier - Create a declaration name that is a simple
356 DeclarationName getIdentifier(const IdentifierInfo *ID) {
357 return DeclarationName(ID);
360 /// getCXXConstructorName - Returns the name of a C++ constructor
361 /// for the given Type.
362 DeclarationName getCXXConstructorName(CanQualType Ty);
364 /// getCXXDestructorName - Returns the name of a C++ destructor
365 /// for the given Type.
366 DeclarationName getCXXDestructorName(CanQualType Ty);
368 /// getCXXConversionFunctionName - Returns the name of a C++
369 /// conversion function for the given Type.
370 DeclarationName getCXXConversionFunctionName(CanQualType Ty);
372 /// getCXXSpecialName - Returns a declaration name for special kind
373 /// of C++ name, e.g., for a constructor, destructor, or conversion
375 DeclarationName getCXXSpecialName(DeclarationName::NameKind Kind,
378 /// getCXXOperatorName - Get the name of the overloadable C++
379 /// operator corresponding to Op.
380 DeclarationName getCXXOperatorName(OverloadedOperatorKind Op);
382 /// getCXXLiteralOperatorName - Get the name of the literal operator function
383 /// with II as the identifier.
384 DeclarationName getCXXLiteralOperatorName(IdentifierInfo *II);
387 /// DeclarationNameLoc - Additional source/type location info
388 /// for a declaration name. Needs a DeclarationName in order
389 /// to be interpreted correctly.
390 struct DeclarationNameLoc {
391 // The source location for identifier stored elsewhere.
392 // struct {} Identifier;
394 // Type info for constructors, destructors and conversion functions.
395 // Locations (if any) for the tilde (destructor) or operator keyword
396 // (conversion) are stored elsewhere.
398 TypeSourceInfo *TInfo;
401 // The location (if any) of the operator keyword is stored elsewhere.
403 unsigned BeginOpNameLoc;
404 unsigned EndOpNameLoc;
407 // The location (if any) of the operator keyword is stored elsewhere.
408 struct CXXLitOpName {
412 // struct {} CXXUsingDirective;
413 // struct {} ObjCZeroArgSelector;
414 // struct {} ObjCOneArgSelector;
415 // struct {} ObjCMultiArgSelector;
418 struct CXXOpName CXXOperatorName;
419 struct CXXLitOpName CXXLiteralOperatorName;
422 DeclarationNameLoc(DeclarationName Name);
423 // FIXME: this should go away once all DNLocs are properly initialized.
424 DeclarationNameLoc() { memset((void*) this, 0, sizeof(*this)); }
425 }; // struct DeclarationNameLoc
428 /// DeclarationNameInfo - A collector data type for bundling together
429 /// a DeclarationName and the correspnding source/type location info.
430 struct DeclarationNameInfo {
432 /// Name - The declaration name, also encoding name kind.
433 DeclarationName Name;
434 /// Loc - The main source location for the declaration name.
435 SourceLocation NameLoc;
436 /// Info - Further source/type location info for special kinds of names.
437 DeclarationNameLoc LocInfo;
441 DeclarationNameInfo() {}
443 DeclarationNameInfo(DeclarationName Name, SourceLocation NameLoc)
444 : Name(Name), NameLoc(NameLoc), LocInfo(Name) {}
446 DeclarationNameInfo(DeclarationName Name, SourceLocation NameLoc,
447 DeclarationNameLoc LocInfo)
448 : Name(Name), NameLoc(NameLoc), LocInfo(LocInfo) {}
450 /// getName - Returns the embedded declaration name.
451 DeclarationName getName() const { return Name; }
452 /// setName - Sets the embedded declaration name.
453 void setName(DeclarationName N) { Name = N; }
455 /// getLoc - Returns the main location of the declaration name.
456 SourceLocation getLoc() const { return NameLoc; }
457 /// setLoc - Sets the main location of the declaration name.
458 void setLoc(SourceLocation L) { NameLoc = L; }
460 const DeclarationNameLoc &getInfo() const { return LocInfo; }
461 DeclarationNameLoc &getInfo() { return LocInfo; }
462 void setInfo(const DeclarationNameLoc &Info) { LocInfo = Info; }
464 /// getNamedTypeInfo - Returns the source type info associated to
465 /// the name. Assumes it is a constructor, destructor or conversion.
466 TypeSourceInfo *getNamedTypeInfo() const {
467 assert(Name.getNameKind() == DeclarationName::CXXConstructorName ||
468 Name.getNameKind() == DeclarationName::CXXDestructorName ||
469 Name.getNameKind() == DeclarationName::CXXConversionFunctionName);
470 return LocInfo.NamedType.TInfo;
472 /// setNamedTypeInfo - Sets the source type info associated to
473 /// the name. Assumes it is a constructor, destructor or conversion.
474 void setNamedTypeInfo(TypeSourceInfo *TInfo) {
475 assert(Name.getNameKind() == DeclarationName::CXXConstructorName ||
476 Name.getNameKind() == DeclarationName::CXXDestructorName ||
477 Name.getNameKind() == DeclarationName::CXXConversionFunctionName);
478 LocInfo.NamedType.TInfo = TInfo;
481 /// getCXXOperatorNameRange - Gets the range of the operator name
482 /// (without the operator keyword). Assumes it is a (non-literal) operator.
483 SourceRange getCXXOperatorNameRange() const {
484 assert(Name.getNameKind() == DeclarationName::CXXOperatorName);
486 SourceLocation::getFromRawEncoding(LocInfo.CXXOperatorName.BeginOpNameLoc),
487 SourceLocation::getFromRawEncoding(LocInfo.CXXOperatorName.EndOpNameLoc)
490 /// setCXXOperatorNameRange - Sets the range of the operator name
491 /// (without the operator keyword). Assumes it is a C++ operator.
492 void setCXXOperatorNameRange(SourceRange R) {
493 assert(Name.getNameKind() == DeclarationName::CXXOperatorName);
494 LocInfo.CXXOperatorName.BeginOpNameLoc = R.getBegin().getRawEncoding();
495 LocInfo.CXXOperatorName.EndOpNameLoc = R.getEnd().getRawEncoding();
498 /// getCXXLiteralOperatorNameLoc - Returns the location of the literal
499 /// operator name (not the operator keyword).
500 /// Assumes it is a literal operator.
501 SourceLocation getCXXLiteralOperatorNameLoc() const {
502 assert(Name.getNameKind() == DeclarationName::CXXLiteralOperatorName);
503 return SourceLocation::
504 getFromRawEncoding(LocInfo.CXXLiteralOperatorName.OpNameLoc);
506 /// setCXXLiteralOperatorNameLoc - Sets the location of the literal
507 /// operator name (not the operator keyword).
508 /// Assumes it is a literal operator.
509 void setCXXLiteralOperatorNameLoc(SourceLocation Loc) {
510 assert(Name.getNameKind() == DeclarationName::CXXLiteralOperatorName);
511 LocInfo.CXXLiteralOperatorName.OpNameLoc = Loc.getRawEncoding();
514 /// \brief Determine whether this name involves a template parameter.
515 bool isInstantiationDependent() const;
517 /// \brief Determine whether this name contains an unexpanded
519 bool containsUnexpandedParameterPack() const;
521 /// getAsString - Retrieve the human-readable string for this name.
522 std::string getAsString() const;
524 /// printName - Print the human-readable name to a stream.
525 void printName(raw_ostream &OS) const;
527 /// getBeginLoc - Retrieve the location of the first token.
528 SourceLocation getBeginLoc() const { return NameLoc; }
529 /// getEndLoc - Retrieve the location of the last token.
530 SourceLocation getEndLoc() const;
531 /// getSourceRange - The range of the declaration name.
532 SourceRange getSourceRange() const LLVM_READONLY {
533 return SourceRange(getLocStart(), getLocEnd());
535 SourceLocation getLocStart() const LLVM_READONLY {
536 return getBeginLoc();
538 SourceLocation getLocEnd() const LLVM_READONLY {
539 SourceLocation EndLoc = getEndLoc();
540 return EndLoc.isValid() ? EndLoc : getLocStart();
544 /// Insertion operator for diagnostics. This allows sending DeclarationName's
545 /// into a diagnostic with <<.
546 inline const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB,
548 DB.AddTaggedVal(N.getAsOpaqueInteger(),
549 DiagnosticsEngine::ak_declarationname);
553 /// Insertion operator for partial diagnostics. This allows binding
554 /// DeclarationName's into a partial diagnostic with <<.
555 inline const PartialDiagnostic &operator<<(const PartialDiagnostic &PD,
557 PD.AddTaggedVal(N.getAsOpaqueInteger(),
558 DiagnosticsEngine::ak_declarationname);
562 inline raw_ostream &operator<<(raw_ostream &OS,
563 DeclarationNameInfo DNInfo) {
564 DNInfo.printName(OS);
568 } // end namespace clang
571 /// Define DenseMapInfo so that DeclarationNames can be used as keys
572 /// in DenseMap and DenseSets.
574 struct DenseMapInfo<clang::DeclarationName> {
575 static inline clang::DeclarationName getEmptyKey() {
576 return clang::DeclarationName::getEmptyMarker();
579 static inline clang::DeclarationName getTombstoneKey() {
580 return clang::DeclarationName::getTombstoneMarker();
583 static unsigned getHashValue(clang::DeclarationName Name) {
584 return DenseMapInfo<void*>::getHashValue(Name.getAsOpaquePtr());
588 isEqual(clang::DeclarationName LHS, clang::DeclarationName RHS) {
594 struct isPodLike<clang::DeclarationName> { static const bool value = true; };
596 } // end namespace llvm