DeclarationName.h revision 909e58988b3a3bb2ad36bec03aafa1302544fd73
12a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)//===-- DeclarationName.h - Representation of declaration names -*- C++ -*-===// 22a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// 32a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// The LLVM Compiler Infrastructure 42a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// 52a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// This file is distributed under the University of Illinois Open Source 62a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// License. See LICENSE.TXT for details. 7c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// 85f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)//===----------------------------------------------------------------------===// 9c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// 105f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)// This file declares the DeclarationName and DeclarationNameTable classes. 112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// 122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)//===----------------------------------------------------------------------===// 132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#ifndef LLVM_CLANG_AST_DECLARATIONNAME_H 142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#define LLVM_CLANG_AST_DECLARATIONNAME_H 155f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) 167dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch#include "clang/Basic/IdentifierTable.h" 17424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)#include "clang/AST/Type.h" 185f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#include "clang/AST/CanonicalType.h" 19424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)#include "clang/Basic/PartialDiagnostic.h" 205f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) 21424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)namespace llvm { 225f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) template <typename T> struct DenseMapInfo; 23424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)} 245f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) 25424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)namespace clang { 265f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) class CXXSpecialName; 27424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) class CXXOperatorIdName; 285f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) class DeclarationNameExtra; 29424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) class IdentifierInfo; 305f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) class MultiKeywordSelector; 31424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) class UsingDirectiveDecl; 325f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) 33424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)/// DeclarationName - The name of a declaration. In the common case, 34424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)/// this just stores an IdentifierInfo pointer to a normal 355f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)/// name. However, it also provides encodings for Objective-C 367dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch/// selectors (optimizing zero- and one-argument selectors, which make 375f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)/// up 78% percent of all selectors in Cocoa.h) and special C++ names 387dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch/// for constructors, destructors, and conversion functions. 397dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdochclass DeclarationName { 402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)public: 41868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) /// NameKind - The kind of name this object contains. 42424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) enum NameKind { 432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) Identifier, 4490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) ObjCZeroArgSelector, 455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) ObjCOneArgSelector, 465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) ObjCMultiArgSelector, 472385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch CXXConstructorName, 485d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) CXXDestructorName, 492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) CXXConversionFunctionName, 505f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) CXXOperatorName, 515f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) CXXUsingDirective 52f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) }; 532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)private: 552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) /// StoredNameKind - The kind of name that is actually stored in the 5690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) /// upper bits of the Ptr field. This is only used internally. 572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) enum StoredNameKind { 58c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) StoredIdentifier = 0, 597d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) StoredObjCZeroArgSelector, 602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) StoredObjCOneArgSelector, 612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) StoredDeclarationNameExtra, 622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) PtrMask = 0x03 632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) }; 64424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) 65424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) /// Ptr - The lowest two bits are used to express what kind of name 662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) /// we're actually storing, using the values of NameKind. Depending 672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) /// on the kind of name this is, the upper bits of Ptr may have one 6890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) /// of several different meanings: 6990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) /// 7090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) /// StoredIdentifier - The name is a normal identifier, and Ptr is 7190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) /// a normal IdentifierInfo pointer. 7290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) /// 7390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) /// StoredObjCZeroArgSelector - The name is an Objective-C 745d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) /// selector with zero arguments, and Ptr is an IdentifierInfo 752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) /// pointer pointing to the selector name. 765f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) /// 774e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) /// StoredObjCOneArgSelector - The name is an Objective-C selector 784e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) /// with one argument, and Ptr is an IdentifierInfo pointer 794e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) /// pointing to the selector name. 804e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) /// 81a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) /// StoredDeclarationNameExtra - Ptr is actually a pointer to a 824e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) /// DeclarationNameExtra structure, whose first value will tell us 834e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) /// whether this is an Objective-C selector, C++ operator-id name, 844e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) /// or special C++ name. 854e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) uintptr_t Ptr; 864e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) 874e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) /// getStoredNameKind - Return the kind of object that is stored in 885f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) /// Ptr. 895f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) StoredNameKind getStoredNameKind() const { 905f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) return static_cast<StoredNameKind>(Ptr & PtrMask); 915f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) } 925f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) 9390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) /// getExtra - Get the "extra" information associated with this 947d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) /// multi-argument selector or C++ special name. 957d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) DeclarationNameExtra *getExtra() const { 967d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) assert(getStoredNameKind() == StoredDeclarationNameExtra && 977d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) "Declaration name does not store an Extra structure"); 987d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) return reinterpret_cast<DeclarationNameExtra *>(Ptr & ~PtrMask); 997d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) } 1007d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) 1012385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch /// getAsCXXSpecialName - If the stored pointer is actually a 1022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) /// CXXSpecialName, returns a pointer to it. Otherwise, returns 1032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) /// a NULL pointer. 1042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) CXXSpecialName *getAsCXXSpecialName() const { 105 if (getNameKind() >= CXXConstructorName && 106 getNameKind() <= CXXConversionFunctionName) 107 return reinterpret_cast<CXXSpecialName *>(Ptr & ~PtrMask); 108 return 0; 109 } 110 111 /// getAsCXXOperatorIdName 112 CXXOperatorIdName *getAsCXXOperatorIdName() const { 113 if (getNameKind() == CXXOperatorName) 114 return reinterpret_cast<CXXOperatorIdName *>(Ptr & ~PtrMask); 115 return 0; 116 } 117 118 // Construct a declaration name from the name of a C++ constructor, 119 // destructor, or conversion function. 120 DeclarationName(CXXSpecialName *Name) 121 : Ptr(reinterpret_cast<uintptr_t>(Name)) { 122 assert((Ptr & PtrMask) == 0 && "Improperly aligned CXXSpecialName"); 123 Ptr |= StoredDeclarationNameExtra; 124 } 125 126 // Construct a declaration name from the name of a C++ overloaded 127 // operator. 128 DeclarationName(CXXOperatorIdName *Name) 129 : Ptr(reinterpret_cast<uintptr_t>(Name)) { 130 assert((Ptr & PtrMask) == 0 && "Improperly aligned CXXOperatorId"); 131 Ptr |= StoredDeclarationNameExtra; 132 } 133 134 /// Construct a declaration name from a raw pointer. 135 DeclarationName(uintptr_t Ptr) : Ptr(Ptr) { } 136 137 friend class DeclarationNameTable; 138 friend class NamedDecl; 139 140 /// getFETokenInfoAsVoid - Retrieves the front end-specified pointer 141 /// for this name as a void pointer. 142 void *getFETokenInfoAsVoid() const; 143 144public: 145 /// DeclarationName - Used to create an empty selector. 146 DeclarationName() : Ptr(0) { } 147 148 // Construct a declaration name from an IdentifierInfo *. 149 DeclarationName(const IdentifierInfo *II) 150 : Ptr(reinterpret_cast<uintptr_t>(II)) { 151 assert((Ptr & PtrMask) == 0 && "Improperly aligned IdentifierInfo"); 152 } 153 154 // Construct a declaration name from an Objective-C selector. 155 DeclarationName(Selector Sel); 156 157 /// getUsingDirectiveName - Return name for all using-directives. 158 static DeclarationName getUsingDirectiveName(); 159 160 // operator bool() - Evaluates true when this declaration name is 161 // non-empty. 162 operator bool() const { 163 return ((Ptr & PtrMask) != 0) || 164 (reinterpret_cast<IdentifierInfo *>(Ptr & ~PtrMask)); 165 } 166 167 /// Predicate functions for querying what type of name this is. 168 bool isIdentifier() const { return getStoredNameKind() == StoredIdentifier; } 169 bool isObjCZeroArgSelector() const { 170 return getStoredNameKind() == StoredObjCZeroArgSelector; 171 } 172 bool isObjCOneArgSelector() const { 173 return getStoredNameKind() == StoredObjCOneArgSelector; 174 } 175 176 /// getNameKind - Determine what kind of name this is. 177 NameKind getNameKind() const; 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 getFromOpaquePtr(void *P) { 200 DeclarationName N; 201 N.Ptr = reinterpret_cast<uintptr_t> (P); 202 return N; 203 } 204 205 static DeclarationName getFromOpaqueInteger(uintptr_t P) { 206 DeclarationName N; 207 N.Ptr = P; 208 return N; 209 } 210 211 /// getCXXNameType - If this name is one of the C++ names (of a 212 /// constructor, destructor, or conversion function), return the 213 /// type associated with that name. 214 QualType getCXXNameType() const; 215 216 /// getCXXOverloadedOperator - If this name is the name of an 217 /// overloadable operator in C++ (e.g., @c operator+), retrieve the 218 /// kind of overloaded operator. 219 OverloadedOperatorKind getCXXOverloadedOperator() const; 220 221 /// getObjCSelector - Get the Objective-C selector stored in this 222 /// declaration name. 223 Selector getObjCSelector() const; 224 225 /// getFETokenInfo/setFETokenInfo - The language front-end is 226 /// allowed to associate arbitrary metadata with some kinds of 227 /// declaration names, including normal identifiers and C++ 228 /// constructors, destructors, and conversion functions. 229 template<typename T> 230 T *getFETokenInfo() const { return static_cast<T*>(getFETokenInfoAsVoid()); } 231 232 void setFETokenInfo(void *T); 233 234 /// operator== - Determine whether the specified names are identical.. 235 friend bool operator==(DeclarationName LHS, DeclarationName RHS) { 236 return LHS.Ptr == RHS.Ptr; 237 } 238 239 /// operator!= - Determine whether the specified names are different. 240 friend bool operator!=(DeclarationName LHS, DeclarationName RHS) { 241 return LHS.Ptr != RHS.Ptr; 242 } 243 244 static DeclarationName getEmptyMarker() { 245 return DeclarationName(uintptr_t(-1)); 246 } 247 248 static DeclarationName getTombstoneMarker() { 249 return DeclarationName(uintptr_t(-2)); 250 } 251 252 void dump() const; 253}; 254 255/// Ordering on two declaration names. If both names are identifiers, 256/// this provides a lexicographical ordering. 257bool operator<(DeclarationName LHS, DeclarationName RHS); 258 259/// Ordering on two declaration names. If both names are identifiers, 260/// this provides a lexicographical ordering. 261inline bool operator>(DeclarationName LHS, DeclarationName RHS) { 262 return RHS < LHS; 263} 264 265/// Ordering on two declaration names. If both names are identifiers, 266/// this provides a lexicographical ordering. 267inline bool operator<=(DeclarationName LHS, DeclarationName RHS) { 268 return !(RHS < LHS); 269} 270 271/// Ordering on two declaration names. If both names are identifiers, 272/// this provides a lexicographical ordering. 273inline bool operator>=(DeclarationName LHS, DeclarationName RHS) { 274 return !(LHS < RHS); 275} 276 277/// DeclarationNameTable - Used to store and retrieve DeclarationName 278/// instances for the various kinds of declaration names, e.g., normal 279/// identifiers, C++ constructor names, etc. This class contains 280/// uniqued versions of each of the C++ special names, which can be 281/// retrieved using its member functions (e.g., 282/// getCXXConstructorName). 283class DeclarationNameTable { 284 void *CXXSpecialNamesImpl; // Actually a FoldingSet<CXXSpecialName> * 285 CXXOperatorIdName *CXXOperatorNames; // Operator names 286 287 DeclarationNameTable(const DeclarationNameTable&); // NONCOPYABLE 288 DeclarationNameTable& operator=(const DeclarationNameTable&); // NONCOPYABLE 289 290public: 291 DeclarationNameTable(); 292 ~DeclarationNameTable(); 293 294 /// getIdentifier - Create a declaration name that is a simple 295 /// identifier. 296 DeclarationName getIdentifier(const IdentifierInfo *ID) { 297 return DeclarationName(ID); 298 } 299 300 /// getCXXConstructorName - Returns the name of a C++ constructor 301 /// for the given Type. 302 DeclarationName getCXXConstructorName(CanQualType Ty) { 303 return getCXXSpecialName(DeclarationName::CXXConstructorName, Ty); 304 } 305 306 /// getCXXDestructorName - Returns the name of a C++ destructor 307 /// for the given Type. 308 DeclarationName getCXXDestructorName(CanQualType Ty) { 309 return getCXXSpecialName(DeclarationName::CXXDestructorName, Ty); 310 } 311 312 /// getCXXConversionFunctionName - Returns the name of a C++ 313 /// conversion function for the given Type. 314 DeclarationName getCXXConversionFunctionName(CanQualType Ty) { 315 return getCXXSpecialName(DeclarationName::CXXConversionFunctionName, Ty); 316 } 317 318 /// getCXXSpecialName - Returns a declaration name for special kind 319 /// of C++ name, e.g., for a constructor, destructor, or conversion 320 /// function. 321 DeclarationName getCXXSpecialName(DeclarationName::NameKind Kind, 322 CanQualType Ty); 323 324 /// getCXXOperatorName - Get the name of the overloadable C++ 325 /// operator corresponding to Op. 326 DeclarationName getCXXOperatorName(OverloadedOperatorKind Op); 327}; 328 329/// Insertion operator for diagnostics. This allows sending DeclarationName's 330/// into a diagnostic with <<. 331inline const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB, 332 DeclarationName N) { 333 DB.AddTaggedVal(N.getAsOpaqueInteger(), 334 Diagnostic::ak_declarationname); 335 return DB; 336} 337 338/// Insertion operator for partial diagnostics. This allows binding 339/// DeclarationName's into a partial diagnostic with <<. 340inline const PartialDiagnostic &operator<<(const PartialDiagnostic &PD, 341 DeclarationName N) { 342 PD.AddTaggedVal(N.getAsOpaqueInteger(), 343 Diagnostic::ak_declarationname); 344 return PD; 345} 346 347} // end namespace clang 348 349namespace llvm { 350/// Define DenseMapInfo so that DeclarationNames can be used as keys 351/// in DenseMap and DenseSets. 352template<> 353struct DenseMapInfo<clang::DeclarationName> { 354 static inline clang::DeclarationName getEmptyKey() { 355 return clang::DeclarationName::getEmptyMarker(); 356 } 357 358 static inline clang::DeclarationName getTombstoneKey() { 359 return clang::DeclarationName::getTombstoneMarker(); 360 } 361 362 static unsigned getHashValue(clang::DeclarationName); 363 364 static inline bool 365 isEqual(clang::DeclarationName LHS, clang::DeclarationName RHS) { 366 return LHS == RHS; 367 } 368 369 static inline bool isPod() { return true; } 370}; 371 372} // end namespace llvm 373 374#endif 375