DeclarationName.h revision 1eb4433ac451dc16f4133a88af2d002ac26c58ef
1//===-- DeclarationName.h - Representation of declaration names -*- C++ -*-===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9// 10// This file declares the DeclarationName and DeclarationNameTable classes. 11// 12//===----------------------------------------------------------------------===// 13#ifndef LLVM_CLANG_AST_DECLARATIONNAME_H 14#define LLVM_CLANG_AST_DECLARATIONNAME_H 15 16#include "clang/Basic/IdentifierTable.h" 17#include "clang/AST/Type.h" 18#include "clang/AST/CanonicalType.h" 19 20namespace llvm { 21 template <typename T> struct DenseMapInfo; 22} 23 24namespace clang { 25 class CXXSpecialName; 26 class CXXOperatorIdName; 27 class DeclarationNameExtra; 28 class IdentifierInfo; 29 class MultiKeywordSelector; 30 class UsingDirectiveDecl; 31 32/// DeclarationName - The name of a declaration. In the common case, 33/// this just stores an IdentifierInfo pointer to a normal 34/// name. However, it also provides encodings for Objective-C 35/// selectors (optimizing zero- and one-argument selectors, which make 36/// up 78% percent of all selectors in Cocoa.h) and special C++ names 37/// for constructors, destructors, and conversion functions. 38class DeclarationName { 39public: 40 /// NameKind - The kind of name this object contains. 41 enum NameKind { 42 Identifier, 43 ObjCZeroArgSelector, 44 ObjCOneArgSelector, 45 ObjCMultiArgSelector, 46 CXXConstructorName, 47 CXXDestructorName, 48 CXXConversionFunctionName, 49 CXXOperatorName, 50 CXXUsingDirective 51 }; 52 53private: 54 /// StoredNameKind - The kind of name that is actually stored in the 55 /// upper bits of the Ptr field. This is only used internally. 56 enum StoredNameKind { 57 StoredIdentifier = 0, 58 StoredObjCZeroArgSelector, 59 StoredObjCOneArgSelector, 60 StoredDeclarationNameExtra, 61 PtrMask = 0x03 62 }; 63 64 /// Ptr - The lowest two bits are used to express what kind of name 65 /// we're actually storing, using the values of NameKind. Depending 66 /// on the kind of name this is, the upper bits of Ptr may have one 67 /// of several different meanings: 68 /// 69 /// StoredIdentifier - The name is a normal identifier, and Ptr is 70 /// a normal IdentifierInfo pointer. 71 /// 72 /// StoredObjCZeroArgSelector - The name is an Objective-C 73 /// selector with zero arguments, and Ptr is an IdentifierInfo 74 /// pointer pointing to the selector name. 75 /// 76 /// StoredObjCOneArgSelector - The name is an Objective-C selector 77 /// with one argument, and Ptr is an IdentifierInfo pointer 78 /// pointing to the selector name. 79 /// 80 /// StoredDeclarationNameExtra - Ptr is actually a pointer to a 81 /// DeclarationNameExtra structure, whose first value will tell us 82 /// whether this is an Objective-C selector, C++ operator-id name, 83 /// or special C++ name. 84 uintptr_t Ptr; 85 86 /// getStoredNameKind - Return the kind of object that is stored in 87 /// Ptr. 88 StoredNameKind getStoredNameKind() const { 89 return static_cast<StoredNameKind>(Ptr & PtrMask); 90 } 91 92 /// getExtra - Get the "extra" information associated with this 93 /// multi-argument selector or C++ special name. 94 DeclarationNameExtra *getExtra() const { 95 assert(getStoredNameKind() == StoredDeclarationNameExtra && 96 "Declaration name does not store an Extra structure"); 97 return reinterpret_cast<DeclarationNameExtra *>(Ptr & ~PtrMask); 98 } 99 100 /// getAsCXXSpecialName - If the stored pointer is actually a 101 /// CXXSpecialName, returns a pointer to it. Otherwise, returns 102 /// a NULL pointer. 103 CXXSpecialName *getAsCXXSpecialName() const { 104 if (getNameKind() >= CXXConstructorName && 105 getNameKind() <= CXXConversionFunctionName) 106 return reinterpret_cast<CXXSpecialName *>(Ptr & ~PtrMask); 107 return 0; 108 } 109 110 /// getAsCXXOperatorIdName 111 CXXOperatorIdName *getAsCXXOperatorIdName() const { 112 if (getNameKind() == CXXOperatorName) 113 return reinterpret_cast<CXXOperatorIdName *>(Ptr & ~PtrMask); 114 return 0; 115 } 116 117 // Construct a declaration name from the name of a C++ constructor, 118 // destructor, or conversion function. 119 DeclarationName(CXXSpecialName *Name) 120 : Ptr(reinterpret_cast<uintptr_t>(Name)) { 121 assert((Ptr & PtrMask) == 0 && "Improperly aligned CXXSpecialName"); 122 Ptr |= StoredDeclarationNameExtra; 123 } 124 125 // Construct a declaration name from the name of a C++ overloaded 126 // operator. 127 DeclarationName(CXXOperatorIdName *Name) 128 : Ptr(reinterpret_cast<uintptr_t>(Name)) { 129 assert((Ptr & PtrMask) == 0 && "Improperly aligned CXXOperatorId"); 130 Ptr |= StoredDeclarationNameExtra; 131 } 132 133 /// Construct a declaration name from a raw pointer. 134 DeclarationName(uintptr_t Ptr) : Ptr(Ptr) { } 135 136 friend class DeclarationNameTable; 137 friend class NamedDecl; 138 139 /// getFETokenInfoAsVoid - Retrieves the front end-specified pointer 140 /// for this name as a void pointer. 141 void *getFETokenInfoAsVoid() const; 142 143public: 144 /// DeclarationName - Used to create an empty selector. 145 DeclarationName() : Ptr(0) { } 146 147 // Construct a declaration name from an IdentifierInfo *. 148 DeclarationName(const IdentifierInfo *II) 149 : Ptr(reinterpret_cast<uintptr_t>(II)) { 150 assert((Ptr & PtrMask) == 0 && "Improperly aligned IdentifierInfo"); 151 } 152 153 // Construct a declaration name from an Objective-C selector. 154 DeclarationName(Selector Sel); 155 156 /// getUsingDirectiveName - Return name for all using-directives. 157 static DeclarationName getUsingDirectiveName(); 158 159 // operator bool() - Evaluates true when this declaration name is 160 // non-empty. 161 operator bool() const { 162 return ((Ptr & PtrMask) != 0) || 163 (reinterpret_cast<IdentifierInfo *>(Ptr & ~PtrMask)); 164 } 165 166 /// Predicate functions for querying what type of name this is. 167 bool isIdentifier() const { return getStoredNameKind() == StoredIdentifier; } 168 bool isObjCZeroArgSelector() const { 169 return getStoredNameKind() == StoredObjCZeroArgSelector; 170 } 171 bool isObjCOneArgSelector() const { 172 return getStoredNameKind() == StoredObjCOneArgSelector; 173 } 174 175 /// getNameKind - Determine what kind of name this is. 176 NameKind getNameKind() const; 177 178 179 /// getName - Retrieve the human-readable string for this name. 180 std::string getAsString() const; 181 182 /// getAsIdentifierInfo - Retrieve the IdentifierInfo * stored in 183 /// this declaration name, or NULL if this declaration name isn't a 184 /// simple identifier. 185 IdentifierInfo *getAsIdentifierInfo() const { 186 if (isIdentifier()) 187 return reinterpret_cast<IdentifierInfo *>(Ptr); 188 return 0; 189 } 190 191 /// getAsOpaqueInteger - Get the representation of this declaration 192 /// name as an opaque integer. 193 uintptr_t getAsOpaqueInteger() const { return Ptr; } 194 195 /// getAsOpaquePtr - Get the representation of this declaration name as 196 /// an opaque pointer. 197 void *getAsOpaquePtr() const { return reinterpret_cast<void*>(Ptr); } 198 199 static DeclarationName getFromOpaqueInteger(uintptr_t P) { 200 DeclarationName N; 201 N.Ptr = P; 202 return N; 203 } 204 205 /// getCXXNameType - If this name is one of the C++ names (of a 206 /// constructor, destructor, or conversion function), return the 207 /// type associated with that name. 208 QualType getCXXNameType() const; 209 210 /// getCXXOverloadedOperator - If this name is the name of an 211 /// overloadable operator in C++ (e.g., @c operator+), retrieve the 212 /// kind of overloaded operator. 213 OverloadedOperatorKind getCXXOverloadedOperator() const; 214 215 /// getObjCSelector - Get the Objective-C selector stored in this 216 /// declaration name. 217 Selector getObjCSelector() const; 218 219 /// getFETokenInfo/setFETokenInfo - The language front-end is 220 /// allowed to associate arbitrary metadata with some kinds of 221 /// declaration names, including normal identifiers and C++ 222 /// constructors, destructors, and conversion functions. 223 template<typename T> 224 T *getFETokenInfo() const { return static_cast<T*>(getFETokenInfoAsVoid()); } 225 226 void setFETokenInfo(void *T); 227 228 /// operator== - Determine whether the specified names are identical.. 229 friend bool operator==(DeclarationName LHS, DeclarationName RHS) { 230 return LHS.Ptr == RHS.Ptr; 231 } 232 233 /// operator!= - Determine whether the specified names are different. 234 friend bool operator!=(DeclarationName LHS, DeclarationName RHS) { 235 return LHS.Ptr != RHS.Ptr; 236 } 237 238 static DeclarationName getEmptyMarker() { 239 return DeclarationName(uintptr_t(-1)); 240 } 241 242 static DeclarationName getTombstoneMarker() { 243 return DeclarationName(uintptr_t(-2)); 244 } 245}; 246 247/// Ordering on two declaration names. If both names are identifiers, 248/// this provides a lexicographical ordering. 249bool operator<(DeclarationName LHS, DeclarationName RHS); 250 251/// Ordering on two declaration names. If both names are identifiers, 252/// this provides a lexicographical ordering. 253inline bool operator>(DeclarationName LHS, DeclarationName RHS) { 254 return RHS < LHS; 255} 256 257/// Ordering on two declaration names. If both names are identifiers, 258/// this provides a lexicographical ordering. 259inline bool operator<=(DeclarationName LHS, DeclarationName RHS) { 260 return !(RHS < LHS); 261} 262 263/// Ordering on two declaration names. If both names are identifiers, 264/// this provides a lexicographical ordering. 265inline bool operator>=(DeclarationName LHS, DeclarationName RHS) { 266 return !(LHS < RHS); 267} 268 269/// DeclarationNameTable - Used to store and retrieve DeclarationName 270/// instances for the various kinds of declaration names, e.g., normal 271/// identifiers, C++ constructor names, etc. This class contains 272/// uniqued versions of each of the C++ special names, which can be 273/// retrieved using its member functions (e.g., 274/// getCXXConstructorName). 275class DeclarationNameTable { 276 void *CXXSpecialNamesImpl; // Actually a FoldingSet<CXXSpecialName> * 277 CXXOperatorIdName *CXXOperatorNames; // Operator names 278 279 DeclarationNameTable(const DeclarationNameTable&); // NONCOPYABLE 280 DeclarationNameTable& operator=(const DeclarationNameTable&); // NONCOPYABLE 281 282public: 283 DeclarationNameTable(); 284 ~DeclarationNameTable(); 285 286 /// getIdentifier - Create a declaration name that is a simple 287 /// identifier. 288 DeclarationName getIdentifier(IdentifierInfo *ID) { 289 return DeclarationName(ID); 290 } 291 292 /// getCXXConstructorName - Returns the name of a C++ constructor 293 /// for the given Type. 294 DeclarationName getCXXConstructorName(CanQualType Ty) { 295 return getCXXSpecialName(DeclarationName::CXXConstructorName, Ty); 296 } 297 298 /// getCXXDestructorName - Returns the name of a C++ destructor 299 /// for the given Type. 300 DeclarationName getCXXDestructorName(CanQualType Ty) { 301 return getCXXSpecialName(DeclarationName::CXXDestructorName, Ty); 302 } 303 304 /// getCXXConversionFunctionName - Returns the name of a C++ 305 /// conversion function for the given Type. 306 DeclarationName getCXXConversionFunctionName(CanQualType Ty) { 307 return getCXXSpecialName(DeclarationName::CXXConversionFunctionName, Ty); 308 } 309 310 /// getCXXSpecialName - Returns a declaration name for special kind 311 /// of C++ name, e.g., for a constructor, destructor, or conversion 312 /// function. 313 DeclarationName getCXXSpecialName(DeclarationName::NameKind Kind, 314 CanQualType Ty); 315 316 /// getCXXOperatorName - Get the name of the overloadable C++ 317 /// operator corresponding to Op. 318 DeclarationName getCXXOperatorName(OverloadedOperatorKind Op); 319}; 320 321/// Insertion operator for diagnostics. This allows sending DeclarationName's 322/// into a diagnostic with <<. 323inline const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB, 324 DeclarationName N) { 325 DB.AddTaggedVal(N.getAsOpaqueInteger(), 326 Diagnostic::ak_declarationname); 327 return DB; 328} 329 330 331} // end namespace clang 332 333namespace llvm { 334/// Define DenseMapInfo so that DeclarationNames can be used as keys 335/// in DenseMap and DenseSets. 336template<> 337struct DenseMapInfo<clang::DeclarationName> { 338 static inline clang::DeclarationName getEmptyKey() { 339 return clang::DeclarationName::getEmptyMarker(); 340 } 341 342 static inline clang::DeclarationName getTombstoneKey() { 343 return clang::DeclarationName::getTombstoneMarker(); 344 } 345 346 static unsigned getHashValue(clang::DeclarationName); 347 348 static inline bool 349 isEqual(clang::DeclarationName LHS, clang::DeclarationName RHS) { 350 return LHS == RHS; 351 } 352 353 static inline bool isPod() { return true; } 354}; 355 356} // end namespace llvm 357 358#endif 359