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;
35 class UsingDirectiveDecl;
37 template <typename> class CanQual;
38 typedef CanQual<Type> CanQualType;
40 /// DeclarationName - The name of a declaration. In the common case,
41 /// this just stores an IdentifierInfo pointer to a normal
42 /// name. However, it also provides encodings for Objective-C
43 /// selectors (optimizing zero- and one-argument selectors, which make
44 /// up 78% percent of all selectors in Cocoa.h) and special C++ names
45 /// for constructors, destructors, and conversion functions.
46 class DeclarationName {
48 /// NameKind - The kind of name this object contains.
56 CXXConversionFunctionName,
58 CXXLiteralOperatorName,
63 /// StoredNameKind - The kind of name that is actually stored in the
64 /// upper bits of the Ptr field. This is only used internally.
66 /// Note: The entries here are synchronized with the entries in Selector,
67 /// for efficient translation between the two.
70 StoredObjCZeroArgSelector = 0x01,
71 StoredObjCOneArgSelector = 0x02,
72 StoredDeclarationNameExtra = 0x03,
76 /// Ptr - The lowest two bits are used to express what kind of name
77 /// we're actually storing, using the values of NameKind. Depending
78 /// on the kind of name this is, the upper bits of Ptr may have one
79 /// of several different meanings:
81 /// StoredIdentifier - The name is a normal identifier, and Ptr is
82 /// a normal IdentifierInfo pointer.
84 /// StoredObjCZeroArgSelector - The name is an Objective-C
85 /// selector with zero arguments, and Ptr is an IdentifierInfo
86 /// pointer pointing to the selector name.
88 /// StoredObjCOneArgSelector - The name is an Objective-C selector
89 /// with one argument, and Ptr is an IdentifierInfo pointer
90 /// pointing to the selector name.
92 /// StoredDeclarationNameExtra - Ptr is actually a pointer to a
93 /// DeclarationNameExtra structure, whose first value will tell us
94 /// whether this is an Objective-C selector, C++ operator-id name,
95 /// or special C++ name.
98 /// getStoredNameKind - Return the kind of object that is stored in
100 StoredNameKind getStoredNameKind() const {
101 return static_cast<StoredNameKind>(Ptr & PtrMask);
104 /// getExtra - Get the "extra" information associated with this
105 /// multi-argument selector or C++ special name.
106 DeclarationNameExtra *getExtra() const {
107 assert(getStoredNameKind() == StoredDeclarationNameExtra &&
108 "Declaration name does not store an Extra structure");
109 return reinterpret_cast<DeclarationNameExtra *>(Ptr & ~PtrMask);
112 /// getAsCXXSpecialName - If the stored pointer is actually a
113 /// CXXSpecialName, returns a pointer to it. Otherwise, returns
115 CXXSpecialName *getAsCXXSpecialName() const {
116 NameKind Kind = getNameKind();
117 if (Kind >= CXXConstructorName && Kind <= CXXConversionFunctionName)
118 return reinterpret_cast<CXXSpecialName *>(Ptr & ~PtrMask);
122 /// getAsCXXOperatorIdName
123 CXXOperatorIdName *getAsCXXOperatorIdName() const {
124 if (getNameKind() == CXXOperatorName)
125 return reinterpret_cast<CXXOperatorIdName *>(Ptr & ~PtrMask);
129 CXXLiteralOperatorIdName *getAsCXXLiteralOperatorIdName() const {
130 if (getNameKind() == CXXLiteralOperatorName)
131 return reinterpret_cast<CXXLiteralOperatorIdName *>(Ptr & ~PtrMask);
135 // Construct a declaration name from the name of a C++ constructor,
136 // destructor, or conversion function.
137 DeclarationName(CXXSpecialName *Name)
138 : Ptr(reinterpret_cast<uintptr_t>(Name)) {
139 assert((Ptr & PtrMask) == 0 && "Improperly aligned CXXSpecialName");
140 Ptr |= StoredDeclarationNameExtra;
143 // Construct a declaration name from the name of a C++ overloaded
145 DeclarationName(CXXOperatorIdName *Name)
146 : Ptr(reinterpret_cast<uintptr_t>(Name)) {
147 assert((Ptr & PtrMask) == 0 && "Improperly aligned CXXOperatorId");
148 Ptr |= StoredDeclarationNameExtra;
151 DeclarationName(CXXLiteralOperatorIdName *Name)
152 : Ptr(reinterpret_cast<uintptr_t>(Name)) {
153 assert((Ptr & PtrMask) == 0 && "Improperly aligned CXXLiteralOperatorId");
154 Ptr |= StoredDeclarationNameExtra;
157 /// Construct a declaration name from a raw pointer.
158 DeclarationName(uintptr_t Ptr) : Ptr(Ptr) { }
160 friend class DeclarationNameTable;
161 friend class NamedDecl;
163 /// getFETokenInfoAsVoidSlow - Retrieves the front end-specified pointer
164 /// for this name as a void pointer if it's not an identifier.
165 void *getFETokenInfoAsVoidSlow() const;
168 /// DeclarationName - Used to create an empty selector.
169 DeclarationName() : Ptr(0) { }
171 // Construct a declaration name from an IdentifierInfo *.
172 DeclarationName(const IdentifierInfo *II)
173 : Ptr(reinterpret_cast<uintptr_t>(II)) {
174 assert((Ptr & PtrMask) == 0 && "Improperly aligned IdentifierInfo");
177 // Construct a declaration name from an Objective-C selector.
178 DeclarationName(Selector Sel) : Ptr(Sel.InfoPtr) { }
180 /// getUsingDirectiveName - Return name for all using-directives.
181 static DeclarationName getUsingDirectiveName();
183 // operator bool() - Evaluates true when this declaration name is
185 operator bool() const {
186 return ((Ptr & PtrMask) != 0) ||
187 (reinterpret_cast<IdentifierInfo *>(Ptr & ~PtrMask));
190 /// Predicate functions for querying what type of name this is.
191 bool isIdentifier() const { return getStoredNameKind() == StoredIdentifier; }
192 bool isObjCZeroArgSelector() const {
193 return getStoredNameKind() == StoredObjCZeroArgSelector;
195 bool isObjCOneArgSelector() const {
196 return getStoredNameKind() == StoredObjCOneArgSelector;
199 /// getNameKind - Determine what kind of name this is.
200 NameKind getNameKind() const;
202 /// \brief Determines whether the name itself is dependent, e.g., because it
203 /// involves a C++ type that is itself dependent.
205 /// Note that this does not capture all of the notions of "dependent name",
206 /// because an identifier can be a dependent name if it is used as the
207 /// callee in a call expression with dependent arguments.
208 bool isDependentName() const;
210 /// getNameAsString - Retrieve the human-readable string for this name.
211 std::string getAsString() const;
213 /// printName - Print the human-readable name to a stream.
214 void printName(raw_ostream &OS) const;
216 /// getAsIdentifierInfo - Retrieve the IdentifierInfo * stored in
217 /// this declaration name, or NULL if this declaration name isn't a
218 /// simple identifier.
219 IdentifierInfo *getAsIdentifierInfo() const {
221 return reinterpret_cast<IdentifierInfo *>(Ptr);
225 /// getAsOpaqueInteger - Get the representation of this declaration
226 /// name as an opaque integer.
227 uintptr_t getAsOpaqueInteger() const { return Ptr; }
229 /// getAsOpaquePtr - Get the representation of this declaration name as
230 /// an opaque pointer.
231 void *getAsOpaquePtr() const { return reinterpret_cast<void*>(Ptr); }
233 static DeclarationName getFromOpaquePtr(void *P) {
235 N.Ptr = reinterpret_cast<uintptr_t> (P);
239 static DeclarationName getFromOpaqueInteger(uintptr_t P) {
245 /// getCXXNameType - If this name is one of the C++ names (of a
246 /// constructor, destructor, or conversion function), return the
247 /// type associated with that name.
248 QualType getCXXNameType() const;
250 /// getCXXOverloadedOperator - If this name is the name of an
251 /// overloadable operator in C++ (e.g., @c operator+), retrieve the
252 /// kind of overloaded operator.
253 OverloadedOperatorKind getCXXOverloadedOperator() const;
255 /// getCXXLiteralIdentifier - If this name is the name of a literal
256 /// operator, retrieve the identifier associated with it.
257 IdentifierInfo *getCXXLiteralIdentifier() const;
259 /// getObjCSelector - Get the Objective-C selector stored in this
260 /// declaration name.
261 Selector getObjCSelector() const {
262 assert((getNameKind() == ObjCZeroArgSelector ||
263 getNameKind() == ObjCOneArgSelector ||
264 getNameKind() == ObjCMultiArgSelector ||
265 Ptr == 0) && "Not a selector!");
266 return Selector(Ptr);
269 /// getFETokenInfo/setFETokenInfo - The language front-end is
270 /// allowed to associate arbitrary metadata with some kinds of
271 /// declaration names, including normal identifiers and C++
272 /// constructors, destructors, and conversion functions.
274 T *getFETokenInfo() const {
275 if (const IdentifierInfo *Info = getAsIdentifierInfo())
276 return Info->getFETokenInfo<T>();
277 return static_cast<T*>(getFETokenInfoAsVoidSlow());
280 void setFETokenInfo(void *T);
282 /// operator== - Determine whether the specified names are identical..
283 friend bool operator==(DeclarationName LHS, DeclarationName RHS) {
284 return LHS.Ptr == RHS.Ptr;
287 /// operator!= - Determine whether the specified names are different.
288 friend bool operator!=(DeclarationName LHS, DeclarationName RHS) {
289 return LHS.Ptr != RHS.Ptr;
292 static DeclarationName getEmptyMarker() {
293 return DeclarationName(uintptr_t(-1));
296 static DeclarationName getTombstoneMarker() {
297 return DeclarationName(uintptr_t(-2));
300 static int compare(DeclarationName LHS, DeclarationName RHS);
305 /// Ordering on two declaration names. If both names are identifiers,
306 /// this provides a lexicographical ordering.
307 inline bool operator<(DeclarationName LHS, DeclarationName RHS) {
308 return DeclarationName::compare(LHS, RHS) < 0;
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 /// DeclarationNameTable - Used to store and retrieve DeclarationName
330 /// instances for the various kinds of declaration names, e.g., normal
331 /// identifiers, C++ constructor names, etc. This class contains
332 /// uniqued versions of each of the C++ special names, which can be
333 /// retrieved using its member functions (e.g.,
334 /// getCXXConstructorName).
335 class DeclarationNameTable {
336 const ASTContext &Ctx;
337 void *CXXSpecialNamesImpl; // Actually a FoldingSet<CXXSpecialName> *
338 CXXOperatorIdName *CXXOperatorNames; // Operator names
339 void *CXXLiteralOperatorNames; // Actually a CXXOperatorIdName*
341 DeclarationNameTable(const DeclarationNameTable&) LLVM_DELETED_FUNCTION;
342 void operator=(const DeclarationNameTable&) LLVM_DELETED_FUNCTION;
345 DeclarationNameTable(const ASTContext &C);
346 ~DeclarationNameTable();
348 /// getIdentifier - Create a declaration name that is a simple
350 DeclarationName getIdentifier(const IdentifierInfo *ID) {
351 return DeclarationName(ID);
354 /// getCXXConstructorName - Returns the name of a C++ constructor
355 /// for the given Type.
356 DeclarationName getCXXConstructorName(CanQualType Ty);
358 /// getCXXDestructorName - Returns the name of a C++ destructor
359 /// for the given Type.
360 DeclarationName getCXXDestructorName(CanQualType Ty);
362 /// getCXXConversionFunctionName - Returns the name of a C++
363 /// conversion function for the given Type.
364 DeclarationName getCXXConversionFunctionName(CanQualType Ty);
366 /// getCXXSpecialName - Returns a declaration name for special kind
367 /// of C++ name, e.g., for a constructor, destructor, or conversion
369 DeclarationName getCXXSpecialName(DeclarationName::NameKind Kind,
372 /// getCXXOperatorName - Get the name of the overloadable C++
373 /// operator corresponding to Op.
374 DeclarationName getCXXOperatorName(OverloadedOperatorKind Op);
376 /// getCXXLiteralOperatorName - Get the name of the literal operator function
377 /// with II as the identifier.
378 DeclarationName getCXXLiteralOperatorName(IdentifierInfo *II);
381 /// DeclarationNameLoc - Additional source/type location info
382 /// for a declaration name. Needs a DeclarationName in order
383 /// to be interpreted correctly.
384 struct DeclarationNameLoc {
385 // The source location for identifier stored elsewhere.
386 // struct {} Identifier;
388 // Type info for constructors, destructors and conversion functions.
389 // Locations (if any) for the tilde (destructor) or operator keyword
390 // (conversion) are stored elsewhere.
392 TypeSourceInfo* TInfo;
395 // The location (if any) of the operator keyword is stored elsewhere.
397 unsigned BeginOpNameLoc;
398 unsigned EndOpNameLoc;
401 // The location (if any) of the operator keyword is stored elsewhere.
402 struct CXXLitOpName {
406 // struct {} CXXUsingDirective;
407 // struct {} ObjCZeroArgSelector;
408 // struct {} ObjCOneArgSelector;
409 // struct {} ObjCMultiArgSelector;
412 struct CXXOpName CXXOperatorName;
413 struct CXXLitOpName CXXLiteralOperatorName;
416 DeclarationNameLoc(DeclarationName Name);
417 // FIXME: this should go away once all DNLocs are properly initialized.
418 DeclarationNameLoc() { memset((void*) this, 0, sizeof(*this)); }
419 }; // struct DeclarationNameLoc
422 /// DeclarationNameInfo - A collector data type for bundling together
423 /// a DeclarationName and the correspnding source/type location info.
424 struct DeclarationNameInfo {
426 /// Name - The declaration name, also encoding name kind.
427 DeclarationName Name;
428 /// Loc - The main source location for the declaration name.
429 SourceLocation NameLoc;
430 /// Info - Further source/type location info for special kinds of names.
431 DeclarationNameLoc LocInfo;
435 DeclarationNameInfo() {}
437 DeclarationNameInfo(DeclarationName Name, SourceLocation NameLoc)
438 : Name(Name), NameLoc(NameLoc), LocInfo(Name) {}
440 DeclarationNameInfo(DeclarationName Name, SourceLocation NameLoc,
441 DeclarationNameLoc LocInfo)
442 : Name(Name), NameLoc(NameLoc), LocInfo(LocInfo) {}
444 /// getName - Returns the embedded declaration name.
445 DeclarationName getName() const { return Name; }
446 /// setName - Sets the embedded declaration name.
447 void setName(DeclarationName N) { Name = N; }
449 /// getLoc - Returns the main location of the declaration name.
450 SourceLocation getLoc() const { return NameLoc; }
451 /// setLoc - Sets the main location of the declaration name.
452 void setLoc(SourceLocation L) { NameLoc = L; }
454 const DeclarationNameLoc &getInfo() const { return LocInfo; }
455 DeclarationNameLoc &getInfo() { return LocInfo; }
456 void setInfo(const DeclarationNameLoc &Info) { LocInfo = Info; }
458 /// getNamedTypeInfo - Returns the source type info associated to
459 /// the name. Assumes it is a constructor, destructor or conversion.
460 TypeSourceInfo *getNamedTypeInfo() const {
461 assert(Name.getNameKind() == DeclarationName::CXXConstructorName ||
462 Name.getNameKind() == DeclarationName::CXXDestructorName ||
463 Name.getNameKind() == DeclarationName::CXXConversionFunctionName);
464 return LocInfo.NamedType.TInfo;
466 /// setNamedTypeInfo - Sets the source type info associated to
467 /// the name. Assumes it is a constructor, destructor or conversion.
468 void setNamedTypeInfo(TypeSourceInfo *TInfo) {
469 assert(Name.getNameKind() == DeclarationName::CXXConstructorName ||
470 Name.getNameKind() == DeclarationName::CXXDestructorName ||
471 Name.getNameKind() == DeclarationName::CXXConversionFunctionName);
472 LocInfo.NamedType.TInfo = TInfo;
475 /// getCXXOperatorNameRange - Gets the range of the operator name
476 /// (without the operator keyword). Assumes it is a (non-literal) operator.
477 SourceRange getCXXOperatorNameRange() const {
478 assert(Name.getNameKind() == DeclarationName::CXXOperatorName);
480 SourceLocation::getFromRawEncoding(LocInfo.CXXOperatorName.BeginOpNameLoc),
481 SourceLocation::getFromRawEncoding(LocInfo.CXXOperatorName.EndOpNameLoc)
484 /// setCXXOperatorNameRange - Sets the range of the operator name
485 /// (without the operator keyword). Assumes it is a C++ operator.
486 void setCXXOperatorNameRange(SourceRange R) {
487 assert(Name.getNameKind() == DeclarationName::CXXOperatorName);
488 LocInfo.CXXOperatorName.BeginOpNameLoc = R.getBegin().getRawEncoding();
489 LocInfo.CXXOperatorName.EndOpNameLoc = R.getEnd().getRawEncoding();
492 /// getCXXLiteralOperatorNameLoc - Returns the location of the literal
493 /// operator name (not the operator keyword).
494 /// Assumes it is a literal operator.
495 SourceLocation getCXXLiteralOperatorNameLoc() const {
496 assert(Name.getNameKind() == DeclarationName::CXXLiteralOperatorName);
497 return SourceLocation::
498 getFromRawEncoding(LocInfo.CXXLiteralOperatorName.OpNameLoc);
500 /// setCXXLiteralOperatorNameLoc - Sets the location of the literal
501 /// operator name (not the operator keyword).
502 /// Assumes it is a literal operator.
503 void setCXXLiteralOperatorNameLoc(SourceLocation Loc) {
504 assert(Name.getNameKind() == DeclarationName::CXXLiteralOperatorName);
505 LocInfo.CXXLiteralOperatorName.OpNameLoc = Loc.getRawEncoding();
508 /// \brief Determine whether this name involves a template parameter.
509 bool isInstantiationDependent() const;
511 /// \brief Determine whether this name contains an unexpanded
513 bool containsUnexpandedParameterPack() const;
515 /// getAsString - Retrieve the human-readable string for this name.
516 std::string getAsString() const;
518 /// printName - Print the human-readable name to a stream.
519 void printName(raw_ostream &OS) const;
521 /// getBeginLoc - Retrieve the location of the first token.
522 SourceLocation getBeginLoc() const { return NameLoc; }
523 /// getEndLoc - Retrieve the location of the last token.
524 SourceLocation getEndLoc() const;
525 /// getSourceRange - The range of the declaration name.
526 SourceRange getSourceRange() const LLVM_READONLY {
527 return SourceRange(getLocStart(), getLocEnd());
529 SourceLocation getLocStart() const LLVM_READONLY {
530 return getBeginLoc();
532 SourceLocation getLocEnd() const LLVM_READONLY {
533 SourceLocation EndLoc = getEndLoc();
534 return EndLoc.isValid() ? EndLoc : getLocStart();
538 /// Insertion operator for diagnostics. This allows sending DeclarationName's
539 /// into a diagnostic with <<.
540 inline const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB,
542 DB.AddTaggedVal(N.getAsOpaqueInteger(),
543 DiagnosticsEngine::ak_declarationname);
547 /// Insertion operator for partial diagnostics. This allows binding
548 /// DeclarationName's into a partial diagnostic with <<.
549 inline const PartialDiagnostic &operator<<(const PartialDiagnostic &PD,
551 PD.AddTaggedVal(N.getAsOpaqueInteger(),
552 DiagnosticsEngine::ak_declarationname);
556 inline raw_ostream &operator<<(raw_ostream &OS,
557 DeclarationNameInfo DNInfo) {
558 DNInfo.printName(OS);
562 } // end namespace clang
565 /// Define DenseMapInfo so that DeclarationNames can be used as keys
566 /// in DenseMap and DenseSets.
568 struct DenseMapInfo<clang::DeclarationName> {
569 static inline clang::DeclarationName getEmptyKey() {
570 return clang::DeclarationName::getEmptyMarker();
573 static inline clang::DeclarationName getTombstoneKey() {
574 return clang::DeclarationName::getTombstoneMarker();
577 static unsigned getHashValue(clang::DeclarationName Name) {
578 return DenseMapInfo<void*>::getHashValue(Name.getAsOpaquePtr());
582 isEqual(clang::DeclarationName LHS, clang::DeclarationName RHS) {
588 struct isPodLike<clang::DeclarationName> { static const bool value = true; };
590 } // end namespace llvm