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