DeclarationName.h revision ac8d75fe94f2aefde5179d53e230b99a1fe1201a
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 "llvm/Bitcode/SerializationFwd.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 /// getUsingDirectiveName - Return name for all using-directives. 137 static DeclarationName getUsingDirectiveName(); 138 139 friend class DeclarationNameTable; 140 friend class UsingDirectiveDecl; 141 friend class NamedDecl; 142 143 /// getFETokenInfoAsVoid - Retrieves the front end-specified pointer 144 /// for this name as a void pointer. 145 void *getFETokenInfoAsVoid() const; 146 147public: 148 /// DeclarationName - Used to create an empty selector. 149 DeclarationName() : Ptr(0) { } 150 151 // Construct a declaration name from an IdentifierInfo *. 152 DeclarationName(IdentifierInfo *II) : Ptr(reinterpret_cast<uintptr_t>(II)) { 153 assert((Ptr & PtrMask) == 0 && "Improperly aligned IdentifierInfo"); 154 } 155 156 // Construct a declaration name from an Objective-C selector. 157 DeclarationName(Selector Sel); 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 /// getNameKind - Determine what kind of name this is. 167 NameKind getNameKind() const; 168 169 /// getName - Retrieve the human-readable string for this name. 170 std::string getAsString() const; 171 172 /// getAsIdentifierInfo - Retrieve the IdentifierInfo * stored in 173 /// this declaration name, or NULL if this declaration name isn't a 174 /// simple identifier. 175 IdentifierInfo *getAsIdentifierInfo() const { 176 if (getNameKind() == Identifier) 177 return reinterpret_cast<IdentifierInfo *>(Ptr); 178 return 0; 179 } 180 181 /// getAsOpaqueInteger - Get the representation of this declaration 182 /// name as an opaque integer. 183 uintptr_t getAsOpaqueInteger() const { return Ptr; } 184 185 /// getAsOpaquePtr - Get the representation of this declaration name as 186 /// an opaque pointer. 187 void *getAsOpaquePtr() const { return reinterpret_cast<void*>(Ptr); } 188 189 static DeclarationName getFromOpaqueInteger(uintptr_t P) { 190 DeclarationName N; 191 N.Ptr = P; 192 return N; 193 } 194 195 /// getCXXNameType - If this name is one of the C++ names (of a 196 /// constructor, destructor, or conversion function), return the 197 /// type associated with that name. 198 QualType getCXXNameType() const; 199 200 /// getCXXOverloadedOperator - If this name is the name of an 201 /// overloadable operator in C++ (e.g., @c operator+), retrieve the 202 /// kind of overloaded operator. 203 OverloadedOperatorKind getCXXOverloadedOperator() const; 204 205 /// getObjCSelector - Get the Objective-C selector stored in this 206 /// declaration name. 207 Selector getObjCSelector() const; 208 209 /// getFETokenInfo/setFETokenInfo - The language front-end is 210 /// allowed to associate arbitrary metadata with some kinds of 211 /// declaration names, including normal identifiers and C++ 212 /// constructors, destructors, and conversion functions. 213 template<typename T> 214 T *getFETokenInfo() const { return static_cast<T*>(getFETokenInfoAsVoid()); } 215 216 void setFETokenInfo(void *T); 217 218 /// operator== - Determine whether the specified names are identical.. 219 friend bool operator==(DeclarationName LHS, DeclarationName RHS) { 220 return LHS.Ptr == RHS.Ptr; 221 } 222 223 /// operator!= - Determine whether the specified names are different. 224 friend bool operator!=(DeclarationName LHS, DeclarationName RHS) { 225 return LHS.Ptr != RHS.Ptr; 226 } 227 228 static DeclarationName getEmptyMarker() { 229 return DeclarationName(uintptr_t(-1)); 230 } 231 232 static DeclarationName getTombstoneMarker() { 233 return DeclarationName(uintptr_t(-2)); 234 } 235}; 236 237/// Ordering on two declaration names. If both names are identifiers, 238/// this provides a lexicographical ordering. 239bool operator<(DeclarationName LHS, DeclarationName RHS); 240 241/// Ordering on two declaration names. If both names are identifiers, 242/// this provides a lexicographical ordering. 243inline bool operator>(DeclarationName LHS, DeclarationName RHS) { 244 return RHS < LHS; 245} 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 !(LHS < RHS); 257} 258 259/// DeclarationNameTable - Used to store and retrieve DeclarationName 260/// instances for the various kinds of declaration names, e.g., normal 261/// identifiers, C++ constructor names, etc. This class contains 262/// uniqued versions of each of the C++ special names, which can be 263/// retrieved using its member functions (e.g., 264/// getCXXConstructorName). 265class DeclarationNameTable { 266 void *CXXSpecialNamesImpl; // Actually a FoldingSet<CXXSpecialName> * 267 CXXOperatorIdName *CXXOperatorNames; // Operator names 268 269 DeclarationNameTable(const DeclarationNameTable&); // NONCOPYABLE 270 DeclarationNameTable& operator=(const DeclarationNameTable&); // NONCOPYABLE 271 272public: 273 DeclarationNameTable(); 274 ~DeclarationNameTable(); 275 276 /// getIdentifier - Create a declaration name that is a simple 277 /// identifier. 278 DeclarationName getIdentifier(IdentifierInfo *ID) { 279 return DeclarationName(ID); 280 } 281 282 /// getCXXConstructorName - Returns the name of a C++ constructor 283 /// for the given Type. 284 DeclarationName getCXXConstructorName(QualType Ty) { 285 return getCXXSpecialName(DeclarationName::CXXConstructorName, Ty); 286 } 287 288 /// getCXXDestructorName - Returns the name of a C++ destructor 289 /// for the given Type. 290 DeclarationName getCXXDestructorName(QualType Ty) { 291 return getCXXSpecialName(DeclarationName::CXXDestructorName, Ty); 292 } 293 294 /// getCXXConversionFunctionName - Returns the name of a C++ 295 /// conversion function for the given Type. 296 DeclarationName getCXXConversionFunctionName(QualType Ty) { 297 return getCXXSpecialName(DeclarationName::CXXConversionFunctionName, Ty); 298 } 299 300 /// getCXXSpecialName - Returns a declaration name for special kind 301 /// of C++ name, e.g., for a constructor, destructor, or conversion 302 /// function. 303 DeclarationName getCXXSpecialName(DeclarationName::NameKind Kind, 304 QualType Ty); 305 306 /// getCXXOperatorName - Get the name of the overloadable C++ 307 /// operator corresponding to Op. 308 DeclarationName getCXXOperatorName(OverloadedOperatorKind Op); 309}; 310 311/// Insertion operator for diagnostics. This allows sending DeclarationName's 312/// into a diagnostic with <<. 313inline const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB, 314 DeclarationName N) { 315 DB.AddTaggedVal(N.getAsOpaqueInteger(), 316 Diagnostic::ak_declarationname); 317 return DB; 318} 319 320 321} // end namespace clang 322 323namespace llvm { 324/// Define DenseMapInfo so that DeclarationNames can be used as keys 325/// in DenseMap and DenseSets. 326template<> 327struct DenseMapInfo<clang::DeclarationName> { 328 static inline clang::DeclarationName getEmptyKey() { 329 return clang::DeclarationName::getEmptyMarker(); 330 } 331 332 static inline clang::DeclarationName getTombstoneKey() { 333 return clang::DeclarationName::getTombstoneMarker(); 334 } 335 336 static unsigned getHashValue(clang::DeclarationName); 337 338 static inline bool 339 isEqual(clang::DeclarationName LHS, clang::DeclarationName RHS) { 340 return LHS == RHS; 341 } 342 343 static inline bool isPod() { return true; } 344}; 345 346} // end namespace llvm 347 348#endif 349