DeclarationName.h revision 82047c16486a28537746cfcc0d63745292b9fc54
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#include "clang/Basic/PartialDiagnostic.h" 20#include "llvm/Support/Compiler.h" 21 22namespace llvm { 23 template <typename T> struct DenseMapInfo; 24} 25 26namespace clang { 27 class CXXSpecialName; 28 class CXXOperatorIdName; 29 class CXXLiteralOperatorIdName; 30 class DeclarationNameExtra; 31 class IdentifierInfo; 32 class MultiKeywordSelector; 33 class UsingDirectiveDecl; 34 class TypeSourceInfo; 35 36/// DeclarationName - The name of a declaration. In the common case, 37/// this just stores an IdentifierInfo pointer to a normal 38/// name. However, it also provides encodings for Objective-C 39/// selectors (optimizing zero- and one-argument selectors, which make 40/// up 78% percent of all selectors in Cocoa.h) and special C++ names 41/// for constructors, destructors, and conversion functions. 42class DeclarationName { 43public: 44 /// NameKind - The kind of name this object contains. 45 enum NameKind { 46 Identifier, 47 ObjCZeroArgSelector, 48 ObjCOneArgSelector, 49 ObjCMultiArgSelector, 50 CXXConstructorName, 51 CXXDestructorName, 52 CXXConversionFunctionName, 53 CXXOperatorName, 54 CXXLiteralOperatorName, 55 CXXUsingDirective 56 }; 57 58private: 59 /// StoredNameKind - The kind of name that is actually stored in the 60 /// upper bits of the Ptr field. This is only used internally. 61 enum StoredNameKind { 62 StoredIdentifier = 0, 63 StoredObjCZeroArgSelector, 64 StoredObjCOneArgSelector, 65 StoredDeclarationNameExtra, 66 PtrMask = 0x03 67 }; 68 69 /// Ptr - The lowest two bits are used to express what kind of name 70 /// we're actually storing, using the values of NameKind. Depending 71 /// on the kind of name this is, the upper bits of Ptr may have one 72 /// of several different meanings: 73 /// 74 /// StoredIdentifier - The name is a normal identifier, and Ptr is 75 /// a normal IdentifierInfo pointer. 76 /// 77 /// StoredObjCZeroArgSelector - The name is an Objective-C 78 /// selector with zero arguments, and Ptr is an IdentifierInfo 79 /// pointer pointing to the selector name. 80 /// 81 /// StoredObjCOneArgSelector - The name is an Objective-C selector 82 /// with one argument, and Ptr is an IdentifierInfo pointer 83 /// pointing to the selector name. 84 /// 85 /// StoredDeclarationNameExtra - Ptr is actually a pointer to a 86 /// DeclarationNameExtra structure, whose first value will tell us 87 /// whether this is an Objective-C selector, C++ operator-id name, 88 /// or special C++ name. 89 uintptr_t Ptr; 90 91 /// getStoredNameKind - Return the kind of object that is stored in 92 /// Ptr. 93 StoredNameKind getStoredNameKind() const { 94 return static_cast<StoredNameKind>(Ptr & PtrMask); 95 } 96 97 /// getExtra - Get the "extra" information associated with this 98 /// multi-argument selector or C++ special name. 99 DeclarationNameExtra *getExtra() const { 100 assert(getStoredNameKind() == StoredDeclarationNameExtra && 101 "Declaration name does not store an Extra structure"); 102 return reinterpret_cast<DeclarationNameExtra *>(Ptr & ~PtrMask); 103 } 104 105 /// getAsCXXSpecialName - If the stored pointer is actually a 106 /// CXXSpecialName, returns a pointer to it. Otherwise, returns 107 /// a NULL pointer. 108 CXXSpecialName *getAsCXXSpecialName() const { 109 if (getNameKind() >= CXXConstructorName && 110 getNameKind() <= CXXConversionFunctionName) 111 return reinterpret_cast<CXXSpecialName *>(Ptr & ~PtrMask); 112 return 0; 113 } 114 115 /// getAsCXXOperatorIdName 116 CXXOperatorIdName *getAsCXXOperatorIdName() const { 117 if (getNameKind() == CXXOperatorName) 118 return reinterpret_cast<CXXOperatorIdName *>(Ptr & ~PtrMask); 119 return 0; 120 } 121 122 CXXLiteralOperatorIdName *getAsCXXLiteralOperatorIdName() const { 123 if (getNameKind() == CXXLiteralOperatorName) 124 return reinterpret_cast<CXXLiteralOperatorIdName *>(Ptr & ~PtrMask); 125 return 0; 126 } 127 128 // Construct a declaration name from the name of a C++ constructor, 129 // destructor, or conversion function. 130 DeclarationName(CXXSpecialName *Name) 131 : Ptr(reinterpret_cast<uintptr_t>(Name)) { 132 assert((Ptr & PtrMask) == 0 && "Improperly aligned CXXSpecialName"); 133 Ptr |= StoredDeclarationNameExtra; 134 } 135 136 // Construct a declaration name from the name of a C++ overloaded 137 // operator. 138 DeclarationName(CXXOperatorIdName *Name) 139 : Ptr(reinterpret_cast<uintptr_t>(Name)) { 140 assert((Ptr & PtrMask) == 0 && "Improperly aligned CXXOperatorId"); 141 Ptr |= StoredDeclarationNameExtra; 142 } 143 144 DeclarationName(CXXLiteralOperatorIdName *Name) 145 : Ptr(reinterpret_cast<uintptr_t>(Name)) { 146 assert((Ptr & PtrMask) == 0 && "Improperly aligned CXXLiteralOperatorId"); 147 Ptr |= StoredDeclarationNameExtra; 148 } 149 150 /// Construct a declaration name from a raw pointer. 151 DeclarationName(uintptr_t Ptr) : Ptr(Ptr) { } 152 153 friend class DeclarationNameTable; 154 friend class NamedDecl; 155 156 /// getFETokenInfoAsVoid - Retrieves the front end-specified pointer 157 /// for this name as a void pointer. 158 void *getFETokenInfoAsVoid() const { 159 if (getNameKind() == Identifier) 160 return getAsIdentifierInfo()->getFETokenInfo<void>(); 161 return getFETokenInfoAsVoidSlow(); 162 } 163 164 void *getFETokenInfoAsVoidSlow() const; 165 166public: 167 /// DeclarationName - Used to create an empty selector. 168 DeclarationName() : Ptr(0) { } 169 170 // Construct a declaration name from an IdentifierInfo *. 171 DeclarationName(const IdentifierInfo *II) 172 : Ptr(reinterpret_cast<uintptr_t>(II)) { 173 assert((Ptr & PtrMask) == 0 && "Improperly aligned IdentifierInfo"); 174 } 175 176 // Construct a declaration name from an Objective-C selector. 177 DeclarationName(Selector Sel); 178 179 /// getUsingDirectiveName - Return name for all using-directives. 180 static DeclarationName getUsingDirectiveName(); 181 182 // operator bool() - Evaluates true when this declaration name is 183 // non-empty. 184 operator bool() const { 185 return ((Ptr & PtrMask) != 0) || 186 (reinterpret_cast<IdentifierInfo *>(Ptr & ~PtrMask)); 187 } 188 189 /// Predicate functions for querying what type of name this is. 190 bool isIdentifier() const { return getStoredNameKind() == StoredIdentifier; } 191 bool isObjCZeroArgSelector() const { 192 return getStoredNameKind() == StoredObjCZeroArgSelector; 193 } 194 bool isObjCOneArgSelector() const { 195 return getStoredNameKind() == StoredObjCOneArgSelector; 196 } 197 198 /// getNameKind - Determine what kind of name this is. 199 NameKind getNameKind() const; 200 201 /// \brief Determines whether the name itself is dependent, e.g., because it 202 /// involves a C++ type that is itself dependent. 203 /// 204 /// Note that this does not capture all of the notions of "dependent name", 205 /// because an identifier can be a dependent name if it is used as the 206 /// callee in a call expression with dependent arguments. 207 bool isDependentName() const; 208 209 /// getNameAsString - Retrieve the human-readable string for this name. 210 std::string getAsString() const; 211 212 /// printName - Print the human-readable name to a stream. 213 void printName(raw_ostream &OS) const; 214 215 /// getAsIdentifierInfo - Retrieve the IdentifierInfo * stored in 216 /// this declaration name, or NULL if this declaration name isn't a 217 /// simple identifier. 218 IdentifierInfo *getAsIdentifierInfo() const { 219 if (isIdentifier()) 220 return reinterpret_cast<IdentifierInfo *>(Ptr); 221 return 0; 222 } 223 224 /// getAsOpaqueInteger - Get the representation of this declaration 225 /// name as an opaque integer. 226 uintptr_t getAsOpaqueInteger() const { return Ptr; } 227 228 /// getAsOpaquePtr - Get the representation of this declaration name as 229 /// an opaque pointer. 230 void *getAsOpaquePtr() const { return reinterpret_cast<void*>(Ptr); } 231 232 static DeclarationName getFromOpaquePtr(void *P) { 233 DeclarationName N; 234 N.Ptr = reinterpret_cast<uintptr_t> (P); 235 return N; 236 } 237 238 static DeclarationName getFromOpaqueInteger(uintptr_t P) { 239 DeclarationName N; 240 N.Ptr = P; 241 return N; 242 } 243 244 /// getCXXNameType - If this name is one of the C++ names (of a 245 /// constructor, destructor, or conversion function), return the 246 /// type associated with that name. 247 QualType getCXXNameType() const; 248 249 /// getCXXOverloadedOperator - If this name is the name of an 250 /// overloadable operator in C++ (e.g., @c operator+), retrieve the 251 /// kind of overloaded operator. 252 OverloadedOperatorKind getCXXOverloadedOperator() const; 253 254 /// getCXXLiteralIdentifier - If this name is the name of a literal 255 /// operator, retrieve the identifier associated with it. 256 IdentifierInfo *getCXXLiteralIdentifier() const; 257 258 /// getObjCSelector - Get the Objective-C selector stored in this 259 /// declaration name. 260 Selector getObjCSelector() const; 261 262 /// getFETokenInfo/setFETokenInfo - The language front-end is 263 /// allowed to associate arbitrary metadata with some kinds of 264 /// declaration names, including normal identifiers and C++ 265 /// constructors, destructors, and conversion functions. 266 template<typename T> 267 T *getFETokenInfo() const { return static_cast<T*>(getFETokenInfoAsVoid()); } 268 269 void setFETokenInfo(void *T); 270 271 /// operator== - Determine whether the specified names are identical.. 272 friend bool operator==(DeclarationName LHS, DeclarationName RHS) { 273 return LHS.Ptr == RHS.Ptr; 274 } 275 276 /// operator!= - Determine whether the specified names are different. 277 friend bool operator!=(DeclarationName LHS, DeclarationName RHS) { 278 return LHS.Ptr != RHS.Ptr; 279 } 280 281 static DeclarationName getEmptyMarker() { 282 return DeclarationName(uintptr_t(-1)); 283 } 284 285 static DeclarationName getTombstoneMarker() { 286 return DeclarationName(uintptr_t(-2)); 287 } 288 289 static int compare(DeclarationName LHS, DeclarationName RHS); 290 291 void dump() const; 292}; 293 294/// Ordering on two declaration names. If both names are identifiers, 295/// this provides a lexicographical ordering. 296inline bool operator<(DeclarationName LHS, DeclarationName RHS) { 297 return DeclarationName::compare(LHS, RHS) < 0; 298} 299 300/// Ordering on two declaration names. If both names are identifiers, 301/// this provides a lexicographical ordering. 302inline bool operator>(DeclarationName LHS, DeclarationName RHS) { 303 return DeclarationName::compare(LHS, RHS) > 0; 304} 305 306/// Ordering on two declaration names. If both names are identifiers, 307/// this provides a lexicographical ordering. 308inline bool operator<=(DeclarationName LHS, DeclarationName RHS) { 309 return DeclarationName::compare(LHS, RHS) <= 0; 310} 311 312/// Ordering on two declaration names. If both names are identifiers, 313/// this provides a lexicographical ordering. 314inline bool operator>=(DeclarationName LHS, DeclarationName RHS) { 315 return DeclarationName::compare(LHS, RHS) >= 0; 316} 317 318/// DeclarationNameTable - Used to store and retrieve DeclarationName 319/// instances for the various kinds of declaration names, e.g., normal 320/// identifiers, C++ constructor names, etc. This class contains 321/// uniqued versions of each of the C++ special names, which can be 322/// retrieved using its member functions (e.g., 323/// getCXXConstructorName). 324class DeclarationNameTable { 325 const ASTContext &Ctx; 326 void *CXXSpecialNamesImpl; // Actually a FoldingSet<CXXSpecialName> * 327 CXXOperatorIdName *CXXOperatorNames; // Operator names 328 void *CXXLiteralOperatorNames; // Actually a CXXOperatorIdName* 329 330 DeclarationNameTable(const DeclarationNameTable&); // NONCOPYABLE 331 DeclarationNameTable& operator=(const DeclarationNameTable&); // NONCOPYABLE 332 333public: 334 DeclarationNameTable(const ASTContext &C); 335 ~DeclarationNameTable(); 336 337 /// getIdentifier - Create a declaration name that is a simple 338 /// identifier. 339 DeclarationName getIdentifier(const IdentifierInfo *ID) { 340 return DeclarationName(ID); 341 } 342 343 /// getCXXConstructorName - Returns the name of a C++ constructor 344 /// for the given Type. 345 DeclarationName getCXXConstructorName(CanQualType Ty) { 346 return getCXXSpecialName(DeclarationName::CXXConstructorName, 347 Ty.getUnqualifiedType()); 348 } 349 350 /// getCXXDestructorName - Returns the name of a C++ destructor 351 /// for the given Type. 352 DeclarationName getCXXDestructorName(CanQualType Ty) { 353 return getCXXSpecialName(DeclarationName::CXXDestructorName, 354 Ty.getUnqualifiedType()); 355 } 356 357 /// getCXXConversionFunctionName - Returns the name of a C++ 358 /// conversion function for the given Type. 359 DeclarationName getCXXConversionFunctionName(CanQualType Ty) { 360 return getCXXSpecialName(DeclarationName::CXXConversionFunctionName, Ty); 361 } 362 363 /// getCXXSpecialName - Returns a declaration name for special kind 364 /// of C++ name, e.g., for a constructor, destructor, or conversion 365 /// function. 366 DeclarationName getCXXSpecialName(DeclarationName::NameKind Kind, 367 CanQualType Ty); 368 369 /// getCXXOperatorName - Get the name of the overloadable C++ 370 /// operator corresponding to Op. 371 DeclarationName getCXXOperatorName(OverloadedOperatorKind Op); 372 373 /// getCXXLiteralOperatorName - Get the name of the literal operator function 374 /// with II as the identifier. 375 DeclarationName getCXXLiteralOperatorName(IdentifierInfo *II); 376}; 377 378/// DeclarationNameLoc - Additional source/type location info 379/// for a declaration name. Needs a DeclarationName in order 380/// to be interpreted correctly. 381struct DeclarationNameLoc { 382 union { 383 // The source location for identifier stored elsewhere. 384 // struct {} Identifier; 385 386 // Type info for constructors, destructors and conversion functions. 387 // Locations (if any) for the tilde (destructor) or operator keyword 388 // (conversion) are stored elsewhere. 389 struct { 390 TypeSourceInfo* TInfo; 391 } NamedType; 392 393 // The location (if any) of the operator keyword is stored elsewhere. 394 struct { 395 unsigned BeginOpNameLoc; 396 unsigned EndOpNameLoc; 397 } CXXOperatorName; 398 399 // The location (if any) of the operator keyword is stored elsewhere. 400 struct { 401 unsigned OpNameLoc; 402 } CXXLiteralOperatorName; 403 404 // struct {} CXXUsingDirective; 405 // struct {} ObjCZeroArgSelector; 406 // struct {} ObjCOneArgSelector; 407 // struct {} ObjCMultiArgSelector; 408 }; 409 410 DeclarationNameLoc(DeclarationName Name); 411 // FIXME: this should go away once all DNLocs are properly initialized. 412 DeclarationNameLoc() { memset((void*) this, 0, sizeof(*this)); } 413}; // struct DeclarationNameLoc 414 415 416/// DeclarationNameInfo - A collector data type for bundling together 417/// a DeclarationName and the correspnding source/type location info. 418struct DeclarationNameInfo { 419private: 420 /// Name - The declaration name, also encoding name kind. 421 DeclarationName Name; 422 /// Loc - The main source location for the declaration name. 423 SourceLocation NameLoc; 424 /// Info - Further source/type location info for special kinds of names. 425 DeclarationNameLoc LocInfo; 426 427public: 428 // FIXME: remove it. 429 DeclarationNameInfo() {} 430 431 DeclarationNameInfo(DeclarationName Name, SourceLocation NameLoc) 432 : Name(Name), NameLoc(NameLoc), LocInfo(Name) {} 433 434 DeclarationNameInfo(DeclarationName Name, SourceLocation NameLoc, 435 DeclarationNameLoc LocInfo) 436 : Name(Name), NameLoc(NameLoc), LocInfo(LocInfo) {} 437 438 /// getName - Returns the embedded declaration name. 439 DeclarationName getName() const { return Name; } 440 /// setName - Sets the embedded declaration name. 441 void setName(DeclarationName N) { Name = N; } 442 443 /// getLoc - Returns the main location of the declaration name. 444 SourceLocation getLoc() const { return NameLoc; } 445 /// setLoc - Sets the main location of the declaration name. 446 void setLoc(SourceLocation L) { NameLoc = L; } 447 448 const DeclarationNameLoc &getInfo() const { return LocInfo; } 449 DeclarationNameLoc &getInfo() { return LocInfo; } 450 void setInfo(const DeclarationNameLoc &Info) { LocInfo = Info; } 451 452 /// getNamedTypeInfo - Returns the source type info associated to 453 /// the name. Assumes it is a constructor, destructor or conversion. 454 TypeSourceInfo *getNamedTypeInfo() const { 455 assert(Name.getNameKind() == DeclarationName::CXXConstructorName || 456 Name.getNameKind() == DeclarationName::CXXDestructorName || 457 Name.getNameKind() == DeclarationName::CXXConversionFunctionName); 458 return LocInfo.NamedType.TInfo; 459 } 460 /// setNamedTypeInfo - Sets the source type info associated to 461 /// the name. Assumes it is a constructor, destructor or conversion. 462 void setNamedTypeInfo(TypeSourceInfo *TInfo) { 463 assert(Name.getNameKind() == DeclarationName::CXXConstructorName || 464 Name.getNameKind() == DeclarationName::CXXDestructorName || 465 Name.getNameKind() == DeclarationName::CXXConversionFunctionName); 466 LocInfo.NamedType.TInfo = TInfo; 467 } 468 469 /// getCXXOperatorNameRange - Gets the range of the operator name 470 /// (without the operator keyword). Assumes it is a (non-literal) operator. 471 SourceRange getCXXOperatorNameRange() const { 472 assert(Name.getNameKind() == DeclarationName::CXXOperatorName); 473 return SourceRange( 474 SourceLocation::getFromRawEncoding(LocInfo.CXXOperatorName.BeginOpNameLoc), 475 SourceLocation::getFromRawEncoding(LocInfo.CXXOperatorName.EndOpNameLoc) 476 ); 477 } 478 /// setCXXOperatorNameRange - Sets the range of the operator name 479 /// (without the operator keyword). Assumes it is a C++ operator. 480 void setCXXOperatorNameRange(SourceRange R) { 481 assert(Name.getNameKind() == DeclarationName::CXXOperatorName); 482 LocInfo.CXXOperatorName.BeginOpNameLoc = R.getBegin().getRawEncoding(); 483 LocInfo.CXXOperatorName.EndOpNameLoc = R.getEnd().getRawEncoding(); 484 } 485 486 /// getCXXLiteralOperatorNameLoc - Returns the location of the literal 487 /// operator name (not the operator keyword). 488 /// Assumes it is a literal operator. 489 SourceLocation getCXXLiteralOperatorNameLoc() const { 490 assert(Name.getNameKind() == DeclarationName::CXXLiteralOperatorName); 491 return SourceLocation:: 492 getFromRawEncoding(LocInfo.CXXLiteralOperatorName.OpNameLoc); 493 } 494 /// setCXXLiteralOperatorNameLoc - Sets the location of the literal 495 /// operator name (not the operator keyword). 496 /// Assumes it is a literal operator. 497 void setCXXLiteralOperatorNameLoc(SourceLocation Loc) { 498 assert(Name.getNameKind() == DeclarationName::CXXLiteralOperatorName); 499 LocInfo.CXXLiteralOperatorName.OpNameLoc = Loc.getRawEncoding(); 500 } 501 502 /// \brief Determine whether this name involves a template parameter. 503 bool isInstantiationDependent() const; 504 505 /// \brief Determine whether this name contains an unexpanded 506 /// parameter pack. 507 bool containsUnexpandedParameterPack() const; 508 509 /// getAsString - Retrieve the human-readable string for this name. 510 std::string getAsString() const; 511 512 /// printName - Print the human-readable name to a stream. 513 void printName(raw_ostream &OS) const; 514 515 /// getBeginLoc - Retrieve the location of the first token. 516 SourceLocation getBeginLoc() const { return NameLoc; } 517 /// getEndLoc - Retrieve the location of the last token. 518 SourceLocation getEndLoc() const; 519 /// getSourceRange - The range of the declaration name. 520 SourceRange getSourceRange() const LLVM_READONLY { 521 SourceLocation BeginLoc = getBeginLoc(); 522 SourceLocation EndLoc = getEndLoc(); 523 return SourceRange(BeginLoc, EndLoc.isValid() ? EndLoc : BeginLoc); 524 } 525 SourceLocation getLocStart() const LLVM_READONLY { 526 return getBeginLoc(); 527 } 528 SourceLocation getLocEnd() const LLVM_READONLY { 529 SourceLocation EndLoc = getEndLoc(); 530 return EndLoc.isValid() ? EndLoc : getLocStart(); 531 } 532}; 533 534/// Insertion operator for diagnostics. This allows sending DeclarationName's 535/// into a diagnostic with <<. 536inline const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB, 537 DeclarationName N) { 538 DB.AddTaggedVal(N.getAsOpaqueInteger(), 539 DiagnosticsEngine::ak_declarationname); 540 return DB; 541} 542 543/// Insertion operator for partial diagnostics. This allows binding 544/// DeclarationName's into a partial diagnostic with <<. 545inline const PartialDiagnostic &operator<<(const PartialDiagnostic &PD, 546 DeclarationName N) { 547 PD.AddTaggedVal(N.getAsOpaqueInteger(), 548 DiagnosticsEngine::ak_declarationname); 549 return PD; 550} 551 552inline raw_ostream &operator<<(raw_ostream &OS, 553 DeclarationNameInfo DNInfo) { 554 DNInfo.printName(OS); 555 return OS; 556} 557 558} // end namespace clang 559 560namespace llvm { 561/// Define DenseMapInfo so that DeclarationNames can be used as keys 562/// in DenseMap and DenseSets. 563template<> 564struct DenseMapInfo<clang::DeclarationName> { 565 static inline clang::DeclarationName getEmptyKey() { 566 return clang::DeclarationName::getEmptyMarker(); 567 } 568 569 static inline clang::DeclarationName getTombstoneKey() { 570 return clang::DeclarationName::getTombstoneMarker(); 571 } 572 573 static unsigned getHashValue(clang::DeclarationName Name) { 574 return DenseMapInfo<void*>::getHashValue(Name.getAsOpaquePtr()); 575 } 576 577 static inline bool 578 isEqual(clang::DeclarationName LHS, clang::DeclarationName RHS) { 579 return LHS == RHS; 580 } 581}; 582 583template <> 584struct isPodLike<clang::DeclarationName> { static const bool value = true; }; 585 586} // end namespace llvm 587 588#endif 589