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 LLVM_EXPLICIT operator bool() const {
186 return ((Ptr & PtrMask) != 0) ||
187 (reinterpret_cast<IdentifierInfo *>(Ptr & ~PtrMask));
190 /// \brief Evaluates true when this declaration name is empty.
191 bool isEmpty() const {
195 /// Predicate functions for querying what type of name this is.
196 bool isIdentifier() const { return getStoredNameKind() == StoredIdentifier; }
197 bool isObjCZeroArgSelector() const {
198 return getStoredNameKind() == StoredObjCZeroArgSelector;
200 bool isObjCOneArgSelector() const {
201 return getStoredNameKind() == StoredObjCOneArgSelector;
204 /// getNameKind - Determine what kind of name this is.
205 NameKind getNameKind() const;
207 /// \brief Determines whether the name itself is dependent, e.g., because it
208 /// involves a C++ type that is itself dependent.
210 /// Note that this does not capture all of the notions of "dependent name",
211 /// because an identifier can be a dependent name if it is used as the
212 /// callee in a call expression with dependent arguments.
213 bool isDependentName() const;
215 /// getNameAsString - Retrieve the human-readable string for this name.
216 std::string getAsString() const;
218 /// getAsIdentifierInfo - Retrieve the IdentifierInfo * stored in
219 /// this declaration name, or NULL if this declaration name isn't a
220 /// simple identifier.
221 IdentifierInfo *getAsIdentifierInfo() const {
223 return reinterpret_cast<IdentifierInfo *>(Ptr);
227 /// getAsOpaqueInteger - Get the representation of this declaration
228 /// name as an opaque integer.
229 uintptr_t getAsOpaqueInteger() const { return Ptr; }
231 /// getAsOpaquePtr - Get the representation of this declaration name as
232 /// an opaque pointer.
233 void *getAsOpaquePtr() const { return reinterpret_cast<void*>(Ptr); }
235 static DeclarationName getFromOpaquePtr(void *P) {
237 N.Ptr = reinterpret_cast<uintptr_t> (P);
241 static DeclarationName getFromOpaqueInteger(uintptr_t P) {
247 /// getCXXNameType - If this name is one of the C++ names (of a
248 /// constructor, destructor, or conversion function), return the
249 /// type associated with that name.
250 QualType getCXXNameType() const;
252 /// getCXXOverloadedOperator - If this name is the name of an
253 /// overloadable operator in C++ (e.g., @c operator+), retrieve the
254 /// kind of overloaded operator.
255 OverloadedOperatorKind getCXXOverloadedOperator() const;
257 /// getCXXLiteralIdentifier - If this name is the name of a literal
258 /// operator, retrieve the identifier associated with it.
259 IdentifierInfo *getCXXLiteralIdentifier() const;
261 /// getObjCSelector - Get the Objective-C selector stored in this
262 /// declaration name.
263 Selector getObjCSelector() const {
264 assert((getNameKind() == ObjCZeroArgSelector ||
265 getNameKind() == ObjCOneArgSelector ||
266 getNameKind() == ObjCMultiArgSelector ||
267 Ptr == 0) && "Not a selector!");
268 return Selector(Ptr);
271 /// getFETokenInfo/setFETokenInfo - The language front-end is
272 /// allowed to associate arbitrary metadata with some kinds of
273 /// declaration names, including normal identifiers and C++
274 /// constructors, destructors, and conversion functions.
276 T *getFETokenInfo() const {
277 if (const IdentifierInfo *Info = getAsIdentifierInfo())
278 return Info->getFETokenInfo<T>();
279 return static_cast<T*>(getFETokenInfoAsVoidSlow());
282 void setFETokenInfo(void *T);
284 /// operator== - Determine whether the specified names are identical..
285 friend bool operator==(DeclarationName LHS, DeclarationName RHS) {
286 return LHS.Ptr == RHS.Ptr;
289 /// operator!= - Determine whether the specified names are different.
290 friend bool operator!=(DeclarationName LHS, DeclarationName RHS) {
291 return LHS.Ptr != RHS.Ptr;
294 static DeclarationName getEmptyMarker() {
295 return DeclarationName(uintptr_t(-1));
298 static DeclarationName getTombstoneMarker() {
299 return DeclarationName(uintptr_t(-2));
302 static int compare(DeclarationName LHS, DeclarationName RHS);
307 raw_ostream &operator<<(raw_ostream &OS, DeclarationName N);
309 /// Ordering on two declaration names. If both names are identifiers,
310 /// this provides a lexicographical ordering.
311 inline bool operator<(DeclarationName LHS, DeclarationName RHS) {
312 return DeclarationName::compare(LHS, RHS) < 0;
315 /// Ordering on two declaration names. If both names are identifiers,
316 /// this provides a lexicographical ordering.
317 inline bool operator>(DeclarationName LHS, DeclarationName RHS) {
318 return DeclarationName::compare(LHS, RHS) > 0;
321 /// Ordering on two declaration names. If both names are identifiers,
322 /// this provides a lexicographical ordering.
323 inline bool operator<=(DeclarationName LHS, DeclarationName RHS) {
324 return DeclarationName::compare(LHS, RHS) <= 0;
327 /// Ordering on two declaration names. If both names are identifiers,
328 /// this provides a lexicographical ordering.
329 inline bool operator>=(DeclarationName LHS, DeclarationName RHS) {
330 return DeclarationName::compare(LHS, RHS) >= 0;
333 /// DeclarationNameTable - Used to store and retrieve DeclarationName
334 /// instances for the various kinds of declaration names, e.g., normal
335 /// identifiers, C++ constructor names, etc. This class contains
336 /// uniqued versions of each of the C++ special names, which can be
337 /// retrieved using its member functions (e.g.,
338 /// getCXXConstructorName).
339 class DeclarationNameTable {
340 const ASTContext &Ctx;
341 void *CXXSpecialNamesImpl; // Actually a FoldingSet<CXXSpecialName> *
342 CXXOperatorIdName *CXXOperatorNames; // Operator names
343 void *CXXLiteralOperatorNames; // Actually a CXXOperatorIdName*
345 DeclarationNameTable(const DeclarationNameTable&) LLVM_DELETED_FUNCTION;
346 void operator=(const DeclarationNameTable&) LLVM_DELETED_FUNCTION;
349 DeclarationNameTable(const ASTContext &C);
350 ~DeclarationNameTable();
352 /// getIdentifier - Create a declaration name that is a simple
354 DeclarationName getIdentifier(const IdentifierInfo *ID) {
355 return DeclarationName(ID);
358 /// getCXXConstructorName - Returns the name of a C++ constructor
359 /// for the given Type.
360 DeclarationName getCXXConstructorName(CanQualType Ty);
362 /// getCXXDestructorName - Returns the name of a C++ destructor
363 /// for the given Type.
364 DeclarationName getCXXDestructorName(CanQualType Ty);
366 /// getCXXConversionFunctionName - Returns the name of a C++
367 /// conversion function for the given Type.
368 DeclarationName getCXXConversionFunctionName(CanQualType Ty);
370 /// getCXXSpecialName - Returns a declaration name for special kind
371 /// of C++ name, e.g., for a constructor, destructor, or conversion
373 DeclarationName getCXXSpecialName(DeclarationName::NameKind Kind,
376 /// getCXXOperatorName - Get the name of the overloadable C++
377 /// operator corresponding to Op.
378 DeclarationName getCXXOperatorName(OverloadedOperatorKind Op);
380 /// getCXXLiteralOperatorName - Get the name of the literal operator function
381 /// with II as the identifier.
382 DeclarationName getCXXLiteralOperatorName(IdentifierInfo *II);
385 /// DeclarationNameLoc - Additional source/type location info
386 /// for a declaration name. Needs a DeclarationName in order
387 /// to be interpreted correctly.
388 struct DeclarationNameLoc {
389 // The source location for identifier stored elsewhere.
390 // struct {} Identifier;
392 // Type info for constructors, destructors and conversion functions.
393 // Locations (if any) for the tilde (destructor) or operator keyword
394 // (conversion) are stored elsewhere.
396 TypeSourceInfo* TInfo;
399 // The location (if any) of the operator keyword is stored elsewhere.
401 unsigned BeginOpNameLoc;
402 unsigned EndOpNameLoc;
405 // The location (if any) of the operator keyword is stored elsewhere.
406 struct CXXLitOpName {
410 // struct {} CXXUsingDirective;
411 // struct {} ObjCZeroArgSelector;
412 // struct {} ObjCOneArgSelector;
413 // struct {} ObjCMultiArgSelector;
416 struct CXXOpName CXXOperatorName;
417 struct CXXLitOpName CXXLiteralOperatorName;
420 DeclarationNameLoc(DeclarationName Name);
421 // FIXME: this should go away once all DNLocs are properly initialized.
422 DeclarationNameLoc() { memset((void*) this, 0, sizeof(*this)); }
423 }; // struct DeclarationNameLoc
426 /// DeclarationNameInfo - A collector data type for bundling together
427 /// a DeclarationName and the correspnding source/type location info.
428 struct DeclarationNameInfo {
430 /// Name - The declaration name, also encoding name kind.
431 DeclarationName Name;
432 /// Loc - The main source location for the declaration name.
433 SourceLocation NameLoc;
434 /// Info - Further source/type location info for special kinds of names.
435 DeclarationNameLoc LocInfo;
439 DeclarationNameInfo() {}
441 DeclarationNameInfo(DeclarationName Name, SourceLocation NameLoc)
442 : Name(Name), NameLoc(NameLoc), LocInfo(Name) {}
444 DeclarationNameInfo(DeclarationName Name, SourceLocation NameLoc,
445 DeclarationNameLoc LocInfo)
446 : Name(Name), NameLoc(NameLoc), LocInfo(LocInfo) {}
448 /// getName - Returns the embedded declaration name.
449 DeclarationName getName() const { return Name; }
450 /// setName - Sets the embedded declaration name.
451 void setName(DeclarationName N) { Name = N; }
453 /// getLoc - Returns the main location of the declaration name.
454 SourceLocation getLoc() const { return NameLoc; }
455 /// setLoc - Sets the main location of the declaration name.
456 void setLoc(SourceLocation L) { NameLoc = L; }
458 const DeclarationNameLoc &getInfo() const { return LocInfo; }
459 DeclarationNameLoc &getInfo() { return LocInfo; }
460 void setInfo(const DeclarationNameLoc &Info) { LocInfo = Info; }
462 /// getNamedTypeInfo - Returns the source type info associated to
463 /// the name. Assumes it is a constructor, destructor or conversion.
464 TypeSourceInfo *getNamedTypeInfo() const {
465 assert(Name.getNameKind() == DeclarationName::CXXConstructorName ||
466 Name.getNameKind() == DeclarationName::CXXDestructorName ||
467 Name.getNameKind() == DeclarationName::CXXConversionFunctionName);
468 return LocInfo.NamedType.TInfo;
470 /// setNamedTypeInfo - Sets the source type info associated to
471 /// the name. Assumes it is a constructor, destructor or conversion.
472 void setNamedTypeInfo(TypeSourceInfo *TInfo) {
473 assert(Name.getNameKind() == DeclarationName::CXXConstructorName ||
474 Name.getNameKind() == DeclarationName::CXXDestructorName ||
475 Name.getNameKind() == DeclarationName::CXXConversionFunctionName);
476 LocInfo.NamedType.TInfo = TInfo;
479 /// getCXXOperatorNameRange - Gets the range of the operator name
480 /// (without the operator keyword). Assumes it is a (non-literal) operator.
481 SourceRange getCXXOperatorNameRange() const {
482 assert(Name.getNameKind() == DeclarationName::CXXOperatorName);
484 SourceLocation::getFromRawEncoding(LocInfo.CXXOperatorName.BeginOpNameLoc),
485 SourceLocation::getFromRawEncoding(LocInfo.CXXOperatorName.EndOpNameLoc)
488 /// setCXXOperatorNameRange - Sets the range of the operator name
489 /// (without the operator keyword). Assumes it is a C++ operator.
490 void setCXXOperatorNameRange(SourceRange R) {
491 assert(Name.getNameKind() == DeclarationName::CXXOperatorName);
492 LocInfo.CXXOperatorName.BeginOpNameLoc = R.getBegin().getRawEncoding();
493 LocInfo.CXXOperatorName.EndOpNameLoc = R.getEnd().getRawEncoding();
496 /// getCXXLiteralOperatorNameLoc - Returns the location of the literal
497 /// operator name (not the operator keyword).
498 /// Assumes it is a literal operator.
499 SourceLocation getCXXLiteralOperatorNameLoc() const {
500 assert(Name.getNameKind() == DeclarationName::CXXLiteralOperatorName);
501 return SourceLocation::
502 getFromRawEncoding(LocInfo.CXXLiteralOperatorName.OpNameLoc);
504 /// setCXXLiteralOperatorNameLoc - Sets the location of the literal
505 /// operator name (not the operator keyword).
506 /// Assumes it is a literal operator.
507 void setCXXLiteralOperatorNameLoc(SourceLocation Loc) {
508 assert(Name.getNameKind() == DeclarationName::CXXLiteralOperatorName);
509 LocInfo.CXXLiteralOperatorName.OpNameLoc = Loc.getRawEncoding();
512 /// \brief Determine whether this name involves a template parameter.
513 bool isInstantiationDependent() const;
515 /// \brief Determine whether this name contains an unexpanded
517 bool containsUnexpandedParameterPack() const;
519 /// getAsString - Retrieve the human-readable string for this name.
520 std::string getAsString() const;
522 /// printName - Print the human-readable name to a stream.
523 void printName(raw_ostream &OS) const;
525 /// getBeginLoc - Retrieve the location of the first token.
526 SourceLocation getBeginLoc() const { return NameLoc; }
527 /// getEndLoc - Retrieve the location of the last token.
528 SourceLocation getEndLoc() const;
529 /// getSourceRange - The range of the declaration name.
530 SourceRange getSourceRange() const LLVM_READONLY {
531 return SourceRange(getLocStart(), getLocEnd());
533 SourceLocation getLocStart() const LLVM_READONLY {
534 return getBeginLoc();
536 SourceLocation getLocEnd() const LLVM_READONLY {
537 SourceLocation EndLoc = getEndLoc();
538 return EndLoc.isValid() ? EndLoc : getLocStart();
542 /// Insertion operator for diagnostics. This allows sending DeclarationName's
543 /// into a diagnostic with <<.
544 inline const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB,
546 DB.AddTaggedVal(N.getAsOpaqueInteger(),
547 DiagnosticsEngine::ak_declarationname);
551 /// Insertion operator for partial diagnostics. This allows binding
552 /// DeclarationName's into a partial diagnostic with <<.
553 inline const PartialDiagnostic &operator<<(const PartialDiagnostic &PD,
555 PD.AddTaggedVal(N.getAsOpaqueInteger(),
556 DiagnosticsEngine::ak_declarationname);
560 inline raw_ostream &operator<<(raw_ostream &OS,
561 DeclarationNameInfo DNInfo) {
562 DNInfo.printName(OS);
566 } // end namespace clang
569 /// Define DenseMapInfo so that DeclarationNames can be used as keys
570 /// in DenseMap and DenseSets.
572 struct DenseMapInfo<clang::DeclarationName> {
573 static inline clang::DeclarationName getEmptyKey() {
574 return clang::DeclarationName::getEmptyMarker();
577 static inline clang::DeclarationName getTombstoneKey() {
578 return clang::DeclarationName::getTombstoneMarker();
581 static unsigned getHashValue(clang::DeclarationName Name) {
582 return DenseMapInfo<void*>::getHashValue(Name.getAsOpaquePtr());
586 isEqual(clang::DeclarationName LHS, clang::DeclarationName RHS) {
592 struct isPodLike<clang::DeclarationName> { static const bool value = true; };
594 } // end namespace llvm