1 //===- DeclarationName.h - Representation of declaration names --*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file declares the DeclarationName and DeclarationNameTable classes. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #ifndef LLVM_CLANG_AST_DECLARATIONNAME_H 14 #define LLVM_CLANG_AST_DECLARATIONNAME_H 15 16 #include "clang/AST/Type.h" 17 #include "clang/Basic/Diagnostic.h" 18 #include "clang/Basic/IdentifierTable.h" 19 #include "clang/Basic/OperatorKinds.h" 20 #include "clang/Basic/PartialDiagnostic.h" 21 #include "clang/Basic/SourceLocation.h" 22 #include "llvm/ADT/DenseMapInfo.h" 23 #include "llvm/ADT/FoldingSet.h" 24 #include "llvm/ADT/STLExtras.h" 25 #include "llvm/Support/Compiler.h" 26 #include "llvm/Support/type_traits.h" 27 #include <cassert> 28 #include <cstdint> 29 #include <cstring> 30 #include <string> 31 32 namespace clang { 33 34 class ASTContext; 35 template <typename> class CanQual; 36 class DeclarationName; 37 class DeclarationNameTable; 38 struct PrintingPolicy; 39 class TemplateDecl; 40 class TypeSourceInfo; 41 42 using CanQualType = CanQual<Type>; 43 44 namespace detail { 45 46 /// CXXSpecialNameExtra records the type associated with one of the "special" 47 /// kinds of declaration names in C++, e.g., constructors, destructors, and 48 /// conversion functions. Note that CXXSpecialName is used for C++ constructor, 49 /// destructor and conversion functions, but the actual kind is not stored in 50 /// CXXSpecialName. Instead we use three different FoldingSet<CXXSpecialName> 51 /// in DeclarationNameTable. 52 class alignas(IdentifierInfoAlignment) CXXSpecialNameExtra 53 : public llvm::FoldingSetNode { 54 friend class clang::DeclarationName; 55 friend class clang::DeclarationNameTable; 56 57 /// The type associated with this declaration name. 58 QualType Type; 59 60 /// Extra information associated with this declaration name that 61 /// can be used by the front end. All bits are really needed 62 /// so it is not possible to stash something in the low order bits. 63 void *FETokenInfo; 64 CXXSpecialNameExtra(QualType QT)65 CXXSpecialNameExtra(QualType QT) : Type(QT), FETokenInfo(nullptr) {} 66 67 public: Profile(llvm::FoldingSetNodeID & ID)68 void Profile(llvm::FoldingSetNodeID &ID) { 69 ID.AddPointer(Type.getAsOpaquePtr()); 70 } 71 }; 72 73 /// Contains extra information for the name of a C++ deduction guide. 74 class alignas(IdentifierInfoAlignment) CXXDeductionGuideNameExtra 75 : public detail::DeclarationNameExtra, 76 public llvm::FoldingSetNode { 77 friend class clang::DeclarationName; 78 friend class clang::DeclarationNameTable; 79 80 /// The template named by the deduction guide. 81 TemplateDecl *Template; 82 83 /// Extra information associated with this operator name that 84 /// can be used by the front end. All bits are really needed 85 /// so it is not possible to stash something in the low order bits. 86 void *FETokenInfo; 87 CXXDeductionGuideNameExtra(TemplateDecl * TD)88 CXXDeductionGuideNameExtra(TemplateDecl *TD) 89 : DeclarationNameExtra(CXXDeductionGuideName), Template(TD), 90 FETokenInfo(nullptr) {} 91 92 public: Profile(llvm::FoldingSetNodeID & ID)93 void Profile(llvm::FoldingSetNodeID &ID) { ID.AddPointer(Template); } 94 }; 95 96 /// Contains extra information for the name of an overloaded operator 97 /// in C++, such as "operator+. This do not includes literal or conversion 98 /// operators. For literal operators see CXXLiteralOperatorIdName and for 99 /// conversion operators see CXXSpecialNameExtra. 100 class alignas(IdentifierInfoAlignment) CXXOperatorIdName { 101 friend class clang::DeclarationName; 102 friend class clang::DeclarationNameTable; 103 104 /// The kind of this operator. 105 OverloadedOperatorKind Kind = OO_None; 106 107 /// Extra information associated with this operator name that 108 /// can be used by the front end. All bits are really needed 109 /// so it is not possible to stash something in the low order bits. 110 void *FETokenInfo = nullptr; 111 }; 112 113 /// Contains the actual identifier that makes up the 114 /// name of a C++ literal operator. 115 class alignas(IdentifierInfoAlignment) CXXLiteralOperatorIdName 116 : public detail::DeclarationNameExtra, 117 public llvm::FoldingSetNode { 118 friend class clang::DeclarationName; 119 friend class clang::DeclarationNameTable; 120 121 const IdentifierInfo *ID; 122 123 /// Extra information associated with this operator name that 124 /// can be used by the front end. All bits are really needed 125 /// so it is not possible to stash something in the low order bits. 126 void *FETokenInfo; 127 CXXLiteralOperatorIdName(const IdentifierInfo * II)128 CXXLiteralOperatorIdName(const IdentifierInfo *II) 129 : DeclarationNameExtra(CXXLiteralOperatorName), ID(II), 130 FETokenInfo(nullptr) {} 131 132 public: Profile(llvm::FoldingSetNodeID & FSID)133 void Profile(llvm::FoldingSetNodeID &FSID) { FSID.AddPointer(ID); } 134 }; 135 136 } // namespace detail 137 138 /// The name of a declaration. In the common case, this just stores 139 /// an IdentifierInfo pointer to a normal name. However, it also provides 140 /// encodings for Objective-C selectors (optimizing zero- and one-argument 141 /// selectors, which make up 78% percent of all selectors in Cocoa.h), 142 /// special C++ names for constructors, destructors, and conversion functions, 143 /// and C++ overloaded operators. 144 class DeclarationName { 145 friend class DeclarationNameTable; 146 friend class NamedDecl; 147 148 /// StoredNameKind represent the kind of name that is actually stored in the 149 /// upper bits of the Ptr field. This is only used internally. 150 /// 151 /// NameKind, StoredNameKind, and DeclarationNameExtra::ExtraKind 152 /// must satisfy the following properties. These properties enable 153 /// efficient conversion between the various kinds. 154 /// 155 /// * The first seven enumerators of StoredNameKind must have the same 156 /// numerical value as the first seven enumerators of NameKind. 157 /// This enable efficient conversion between the two enumerations 158 /// in the usual case. 159 /// 160 /// * The enumerations values of DeclarationNameExtra::ExtraKind must start 161 /// at zero, and correspond to the numerical value of the first non-inline 162 /// enumeration values of NameKind minus an offset. This makes conversion 163 /// between DeclarationNameExtra::ExtraKind and NameKind possible with 164 /// a single addition/substraction. 165 /// 166 /// * The enumeration values of Selector::IdentifierInfoFlag must correspond 167 /// to the relevant enumeration values of StoredNameKind. 168 /// More specifically: 169 /// * ZeroArg == StoredObjCZeroArgSelector, 170 /// * OneArg == StoredObjCOneArgSelector, 171 /// * MultiArg == StoredDeclarationNameExtra 172 /// 173 /// * PtrMask must mask the low 3 bits of Ptr. 174 enum StoredNameKind { 175 StoredIdentifier = 0, 176 StoredObjCZeroArgSelector = Selector::ZeroArg, 177 StoredObjCOneArgSelector = Selector::OneArg, 178 StoredCXXConstructorName = 3, 179 StoredCXXDestructorName = 4, 180 StoredCXXConversionFunctionName = 5, 181 StoredCXXOperatorName = 6, 182 StoredDeclarationNameExtra = Selector::MultiArg, 183 PtrMask = 7, 184 UncommonNameKindOffset = 8 185 }; 186 187 static_assert(alignof(IdentifierInfo) >= 8 && 188 alignof(detail::DeclarationNameExtra) >= 8 && 189 alignof(detail::CXXSpecialNameExtra) >= 8 && 190 alignof(detail::CXXOperatorIdName) >= 8 && 191 alignof(detail::CXXDeductionGuideNameExtra) >= 8 && 192 alignof(detail::CXXLiteralOperatorIdName) >= 8, 193 "The various classes that DeclarationName::Ptr can point to" 194 " must be at least aligned to 8 bytes!"); 195 196 static_assert( 197 std::is_same<std::underlying_type_t<StoredNameKind>, 198 std::underlying_type_t< 199 detail::DeclarationNameExtra::ExtraKind>>::value, 200 "The various enums used to compute values for NameKind should " 201 "all have the same underlying type"); 202 203 public: 204 /// The kind of the name stored in this DeclarationName. 205 /// The first 7 enumeration values are stored inline and correspond 206 /// to frequently used kinds. The rest is stored in DeclarationNameExtra 207 /// and correspond to infrequently used kinds. 208 enum NameKind { 209 Identifier = StoredIdentifier, 210 ObjCZeroArgSelector = StoredObjCZeroArgSelector, 211 ObjCOneArgSelector = StoredObjCOneArgSelector, 212 CXXConstructorName = StoredCXXConstructorName, 213 CXXDestructorName = StoredCXXDestructorName, 214 CXXConversionFunctionName = StoredCXXConversionFunctionName, 215 CXXOperatorName = StoredCXXOperatorName, 216 CXXDeductionGuideName = llvm::addEnumValues( 217 UncommonNameKindOffset, 218 detail::DeclarationNameExtra::CXXDeductionGuideName), 219 CXXLiteralOperatorName = llvm::addEnumValues( 220 UncommonNameKindOffset, 221 detail::DeclarationNameExtra::CXXLiteralOperatorName), 222 CXXUsingDirective = 223 llvm::addEnumValues(UncommonNameKindOffset, 224 detail::DeclarationNameExtra::CXXUsingDirective), 225 ObjCMultiArgSelector = 226 llvm::addEnumValues(UncommonNameKindOffset, 227 detail::DeclarationNameExtra::ObjCMultiArgSelector), 228 }; 229 230 private: 231 /// The lowest three bits of Ptr are used to express what kind of name 232 /// we're actually storing, using the values of StoredNameKind. Depending 233 /// on the kind of name this is, the upper bits of Ptr may have one 234 /// of several different meanings: 235 /// 236 /// StoredIdentifier - The name is a normal identifier, and Ptr is 237 /// a normal IdentifierInfo pointer. 238 /// 239 /// StoredObjCZeroArgSelector - The name is an Objective-C 240 /// selector with zero arguments, and Ptr is an IdentifierInfo 241 /// pointer pointing to the selector name. 242 /// 243 /// StoredObjCOneArgSelector - The name is an Objective-C selector 244 /// with one argument, and Ptr is an IdentifierInfo pointer 245 /// pointing to the selector name. 246 /// 247 /// StoredCXXConstructorName - The name of a C++ constructor, 248 /// Ptr points to a CXXSpecialNameExtra. 249 /// 250 /// StoredCXXDestructorName - The name of a C++ destructor, 251 /// Ptr points to a CXXSpecialNameExtra. 252 /// 253 /// StoredCXXConversionFunctionName - The name of a C++ conversion function, 254 /// Ptr points to a CXXSpecialNameExtra. 255 /// 256 /// StoredCXXOperatorName - The name of an overloaded C++ operator, 257 /// Ptr points to a CXXOperatorIdName. 258 /// 259 /// StoredDeclarationNameExtra - Ptr is actually a pointer to a 260 /// DeclarationNameExtra structure, whose first value will tell us 261 /// whether this is an Objective-C selector, C++ deduction guide, 262 /// C++ literal operator, or C++ using directive. 263 uintptr_t Ptr = 0; 264 getStoredNameKind()265 StoredNameKind getStoredNameKind() const { 266 return static_cast<StoredNameKind>(Ptr & PtrMask); 267 } 268 getPtr()269 void *getPtr() const { return reinterpret_cast<void *>(Ptr & ~PtrMask); } 270 setPtrAndKind(const void * P,StoredNameKind Kind)271 void setPtrAndKind(const void *P, StoredNameKind Kind) { 272 uintptr_t PAsInteger = reinterpret_cast<uintptr_t>(P); 273 assert((Kind & ~PtrMask) == 0 && 274 "Invalid StoredNameKind in setPtrAndKind!"); 275 assert((PAsInteger & PtrMask) == 0 && 276 "Improperly aligned pointer in setPtrAndKind!"); 277 Ptr = PAsInteger | Kind; 278 } 279 280 /// Construct a declaration name from a DeclarationNameExtra. DeclarationName(detail::DeclarationNameExtra * Name)281 DeclarationName(detail::DeclarationNameExtra *Name) { 282 setPtrAndKind(Name, StoredDeclarationNameExtra); 283 } 284 285 /// Construct a declaration name from a CXXSpecialNameExtra. DeclarationName(detail::CXXSpecialNameExtra * Name,StoredNameKind StoredKind)286 DeclarationName(detail::CXXSpecialNameExtra *Name, 287 StoredNameKind StoredKind) { 288 assert((StoredKind == StoredCXXConstructorName || 289 StoredKind == StoredCXXDestructorName || 290 StoredKind == StoredCXXConversionFunctionName) && 291 "Invalid StoredNameKind when constructing a DeclarationName" 292 " from a CXXSpecialNameExtra!"); 293 setPtrAndKind(Name, StoredKind); 294 } 295 296 /// Construct a DeclarationName from a CXXOperatorIdName. DeclarationName(detail::CXXOperatorIdName * Name)297 DeclarationName(detail::CXXOperatorIdName *Name) { 298 setPtrAndKind(Name, StoredCXXOperatorName); 299 } 300 301 /// Assert that the stored pointer points to an IdentifierInfo and return it. castAsIdentifierInfo()302 IdentifierInfo *castAsIdentifierInfo() const { 303 assert((getStoredNameKind() == StoredIdentifier) && 304 "DeclarationName does not store an IdentifierInfo!"); 305 return static_cast<IdentifierInfo *>(getPtr()); 306 } 307 308 /// Assert that the stored pointer points to a DeclarationNameExtra 309 /// and return it. castAsExtra()310 detail::DeclarationNameExtra *castAsExtra() const { 311 assert((getStoredNameKind() == StoredDeclarationNameExtra) && 312 "DeclarationName does not store an Extra structure!"); 313 return static_cast<detail::DeclarationNameExtra *>(getPtr()); 314 } 315 316 /// Assert that the stored pointer points to a CXXSpecialNameExtra 317 /// and return it. castAsCXXSpecialNameExtra()318 detail::CXXSpecialNameExtra *castAsCXXSpecialNameExtra() const { 319 assert((getStoredNameKind() == StoredCXXConstructorName || 320 getStoredNameKind() == StoredCXXDestructorName || 321 getStoredNameKind() == StoredCXXConversionFunctionName) && 322 "DeclarationName does not store a CXXSpecialNameExtra!"); 323 return static_cast<detail::CXXSpecialNameExtra *>(getPtr()); 324 } 325 326 /// Assert that the stored pointer points to a CXXOperatorIdName 327 /// and return it. castAsCXXOperatorIdName()328 detail::CXXOperatorIdName *castAsCXXOperatorIdName() const { 329 assert((getStoredNameKind() == StoredCXXOperatorName) && 330 "DeclarationName does not store a CXXOperatorIdName!"); 331 return static_cast<detail::CXXOperatorIdName *>(getPtr()); 332 } 333 334 /// Assert that the stored pointer points to a CXXDeductionGuideNameExtra 335 /// and return it. castAsCXXDeductionGuideNameExtra()336 detail::CXXDeductionGuideNameExtra *castAsCXXDeductionGuideNameExtra() const { 337 assert(getNameKind() == CXXDeductionGuideName && 338 "DeclarationName does not store a CXXDeductionGuideNameExtra!"); 339 return static_cast<detail::CXXDeductionGuideNameExtra *>(getPtr()); 340 } 341 342 /// Assert that the stored pointer points to a CXXLiteralOperatorIdName 343 /// and return it. castAsCXXLiteralOperatorIdName()344 detail::CXXLiteralOperatorIdName *castAsCXXLiteralOperatorIdName() const { 345 assert(getNameKind() == CXXLiteralOperatorName && 346 "DeclarationName does not store a CXXLiteralOperatorIdName!"); 347 return static_cast<detail::CXXLiteralOperatorIdName *>(getPtr()); 348 } 349 350 /// Get and set the FETokenInfo in the less common cases where the 351 /// declaration name do not point to an identifier. 352 void *getFETokenInfoSlow() const; 353 void setFETokenInfoSlow(void *T); 354 355 public: 356 /// Construct an empty declaration name. DeclarationName()357 DeclarationName() { setPtrAndKind(nullptr, StoredIdentifier); } 358 359 /// Construct a declaration name from an IdentifierInfo *. DeclarationName(const IdentifierInfo * II)360 DeclarationName(const IdentifierInfo *II) { 361 setPtrAndKind(II, StoredIdentifier); 362 } 363 364 /// Construct a declaration name from an Objective-C selector. DeclarationName(Selector Sel)365 DeclarationName(Selector Sel) 366 : Ptr(reinterpret_cast<uintptr_t>(Sel.InfoPtr.getOpaqueValue())) {} 367 368 /// Returns the name for all C++ using-directives. getUsingDirectiveName()369 static DeclarationName getUsingDirectiveName() { 370 // Single instance of DeclarationNameExtra for using-directive 371 static detail::DeclarationNameExtra UDirExtra( 372 detail::DeclarationNameExtra::CXXUsingDirective); 373 return DeclarationName(&UDirExtra); 374 } 375 376 /// Evaluates true when this declaration name is non-empty. 377 explicit operator bool() const { 378 return getPtr() || (getStoredNameKind() != StoredIdentifier); 379 } 380 381 /// Evaluates true when this declaration name is empty. isEmpty()382 bool isEmpty() const { return !*this; } 383 384 /// Predicate functions for querying what type of name this is. isIdentifier()385 bool isIdentifier() const { return getStoredNameKind() == StoredIdentifier; } isObjCZeroArgSelector()386 bool isObjCZeroArgSelector() const { 387 return getStoredNameKind() == StoredObjCZeroArgSelector; 388 } isObjCOneArgSelector()389 bool isObjCOneArgSelector() const { 390 return getStoredNameKind() == StoredObjCOneArgSelector; 391 } 392 393 /// Determine what kind of name this is. getNameKind()394 NameKind getNameKind() const { 395 // We rely on the fact that the first 7 NameKind and StoredNameKind 396 // have the same numerical value. This makes the usual case efficient. 397 StoredNameKind StoredKind = getStoredNameKind(); 398 if (StoredKind != StoredDeclarationNameExtra) 399 return static_cast<NameKind>(StoredKind); 400 // We have to consult DeclarationNameExtra. We rely on the fact that the 401 // enumeration values of ExtraKind correspond to the enumeration values of 402 // NameKind minus an offset of UncommonNameKindOffset. 403 unsigned ExtraKind = castAsExtra()->getKind(); 404 return static_cast<NameKind>(UncommonNameKindOffset + ExtraKind); 405 } 406 407 /// Determines whether the name itself is dependent, e.g., because it 408 /// involves a C++ type that is itself dependent. 409 /// 410 /// Note that this does not capture all of the notions of "dependent name", 411 /// because an identifier can be a dependent name if it is used as the 412 /// callee in a call expression with dependent arguments. 413 bool isDependentName() const; 414 415 /// Retrieve the human-readable string for this name. 416 std::string getAsString() const; 417 418 /// Retrieve the IdentifierInfo * stored in this declaration name, 419 /// or null if this declaration name isn't a simple identifier. getAsIdentifierInfo()420 IdentifierInfo *getAsIdentifierInfo() const { 421 if (isIdentifier()) 422 return castAsIdentifierInfo(); 423 return nullptr; 424 } 425 426 /// Get the representation of this declaration name as an opaque integer. getAsOpaqueInteger()427 uintptr_t getAsOpaqueInteger() const { return Ptr; } 428 429 /// Get the representation of this declaration name as an opaque pointer. getAsOpaquePtr()430 void *getAsOpaquePtr() const { return reinterpret_cast<void *>(Ptr); } 431 432 /// Get a declaration name from an opaque pointer returned by getAsOpaquePtr. getFromOpaquePtr(void * P)433 static DeclarationName getFromOpaquePtr(void *P) { 434 DeclarationName N; 435 N.Ptr = reinterpret_cast<uintptr_t>(P); 436 return N; 437 } 438 439 /// Get a declaration name from an opaque integer 440 /// returned by getAsOpaqueInteger. getFromOpaqueInteger(uintptr_t P)441 static DeclarationName getFromOpaqueInteger(uintptr_t P) { 442 DeclarationName N; 443 N.Ptr = P; 444 return N; 445 } 446 447 /// If this name is one of the C++ names (of a constructor, destructor, 448 /// or conversion function), return the type associated with that name. getCXXNameType()449 QualType getCXXNameType() const { 450 if (getStoredNameKind() == StoredCXXConstructorName || 451 getStoredNameKind() == StoredCXXDestructorName || 452 getStoredNameKind() == StoredCXXConversionFunctionName) { 453 assert(getPtr() && "getCXXNameType on a null DeclarationName!"); 454 return castAsCXXSpecialNameExtra()->Type; 455 } 456 return QualType(); 457 } 458 459 /// If this name is the name of a C++ deduction guide, return the 460 /// template associated with that name. getCXXDeductionGuideTemplate()461 TemplateDecl *getCXXDeductionGuideTemplate() const { 462 if (getNameKind() == CXXDeductionGuideName) { 463 assert(getPtr() && 464 "getCXXDeductionGuideTemplate on a null DeclarationName!"); 465 return castAsCXXDeductionGuideNameExtra()->Template; 466 } 467 return nullptr; 468 } 469 470 /// If this name is the name of an overloadable operator in C++ 471 /// (e.g., @c operator+), retrieve the kind of overloaded operator. getCXXOverloadedOperator()472 OverloadedOperatorKind getCXXOverloadedOperator() const { 473 if (getStoredNameKind() == StoredCXXOperatorName) { 474 assert(getPtr() && "getCXXOverloadedOperator on a null DeclarationName!"); 475 return castAsCXXOperatorIdName()->Kind; 476 } 477 return OO_None; 478 } 479 isAnyOperatorNew()480 bool isAnyOperatorNew() const { 481 if (getNameKind() != DeclarationName::CXXOperatorName) 482 return false; 483 switch (getCXXOverloadedOperator()) { 484 case OO_New: 485 case OO_Array_New: 486 return true; 487 default: 488 return false; 489 } 490 } 491 isAnyOperatorDelete()492 bool isAnyOperatorDelete() const { 493 if (getNameKind() != DeclarationName::CXXOperatorName) 494 return false; 495 switch (getCXXOverloadedOperator()) { 496 case OO_Delete: 497 case OO_Array_Delete: 498 return true; 499 default: 500 return false; 501 } 502 } 503 isAnyOperatorNewOrDelete()504 bool isAnyOperatorNewOrDelete() const { 505 return isAnyOperatorNew() || isAnyOperatorDelete(); 506 } 507 508 /// If this name is the name of a literal operator, 509 /// retrieve the identifier associated with it. getCXXLiteralIdentifier()510 const IdentifierInfo *getCXXLiteralIdentifier() const { 511 if (getNameKind() == CXXLiteralOperatorName) { 512 assert(getPtr() && "getCXXLiteralIdentifier on a null DeclarationName!"); 513 return castAsCXXLiteralOperatorIdName()->ID; 514 } 515 return nullptr; 516 } 517 518 /// Get the Objective-C selector stored in this declaration name. getObjCSelector()519 Selector getObjCSelector() const { 520 assert((getNameKind() == ObjCZeroArgSelector || 521 getNameKind() == ObjCOneArgSelector || 522 getNameKind() == ObjCMultiArgSelector || !getPtr()) && 523 "Not a selector!"); 524 return Selector(Ptr); 525 } 526 527 /// Get and set FETokenInfo. The language front-end is allowed to associate 528 /// arbitrary metadata with some kinds of declaration names, including normal 529 /// identifiers and C++ constructors, destructors, and conversion functions. getFETokenInfo()530 void *getFETokenInfo() const { 531 assert(getPtr() && "getFETokenInfo on an empty DeclarationName!"); 532 if (getStoredNameKind() == StoredIdentifier) 533 return castAsIdentifierInfo()->getFETokenInfo(); 534 return getFETokenInfoSlow(); 535 } 536 setFETokenInfo(void * T)537 void setFETokenInfo(void *T) { 538 assert(getPtr() && "setFETokenInfo on an empty DeclarationName!"); 539 if (getStoredNameKind() == StoredIdentifier) 540 castAsIdentifierInfo()->setFETokenInfo(T); 541 else 542 setFETokenInfoSlow(T); 543 } 544 545 /// Determine whether the specified names are identical. 546 friend bool operator==(DeclarationName LHS, DeclarationName RHS) { 547 return LHS.Ptr == RHS.Ptr; 548 } 549 550 /// Determine whether the specified names are different. 551 friend bool operator!=(DeclarationName LHS, DeclarationName RHS) { 552 return LHS.Ptr != RHS.Ptr; 553 } 554 getEmptyMarker()555 static DeclarationName getEmptyMarker() { 556 DeclarationName Name; 557 Name.Ptr = uintptr_t(-1); 558 return Name; 559 } 560 getTombstoneMarker()561 static DeclarationName getTombstoneMarker() { 562 DeclarationName Name; 563 Name.Ptr = uintptr_t(-2); 564 return Name; 565 } 566 567 static int compare(DeclarationName LHS, DeclarationName RHS); 568 569 void print(raw_ostream &OS, const PrintingPolicy &Policy) const; 570 571 void dump() const; 572 }; 573 574 raw_ostream &operator<<(raw_ostream &OS, DeclarationName N); 575 576 /// Ordering on two declaration names. If both names are identifiers, 577 /// this provides a lexicographical ordering. 578 inline bool operator<(DeclarationName LHS, DeclarationName RHS) { 579 return DeclarationName::compare(LHS, RHS) < 0; 580 } 581 582 /// Ordering on two declaration names. If both names are identifiers, 583 /// this provides a lexicographical ordering. 584 inline bool operator>(DeclarationName LHS, DeclarationName RHS) { 585 return DeclarationName::compare(LHS, RHS) > 0; 586 } 587 588 /// Ordering on two declaration names. If both names are identifiers, 589 /// this provides a lexicographical ordering. 590 inline bool operator<=(DeclarationName LHS, DeclarationName RHS) { 591 return DeclarationName::compare(LHS, RHS) <= 0; 592 } 593 594 /// Ordering on two declaration names. If both names are identifiers, 595 /// this provides a lexicographical ordering. 596 inline bool operator>=(DeclarationName LHS, DeclarationName RHS) { 597 return DeclarationName::compare(LHS, RHS) >= 0; 598 } 599 600 /// DeclarationNameTable is used to store and retrieve DeclarationName 601 /// instances for the various kinds of declaration names, e.g., normal 602 /// identifiers, C++ constructor names, etc. This class contains 603 /// uniqued versions of each of the C++ special names, which can be 604 /// retrieved using its member functions (e.g., getCXXConstructorName). 605 class DeclarationNameTable { 606 /// Used to allocate elements in the FoldingSets below. 607 const ASTContext &Ctx; 608 609 /// Manage the uniqued CXXSpecialNameExtra representing C++ constructors. 610 /// getCXXConstructorName and getCXXSpecialName can be used to obtain 611 /// a DeclarationName from the corresponding type of the constructor. 612 llvm::FoldingSet<detail::CXXSpecialNameExtra> CXXConstructorNames; 613 614 /// Manage the uniqued CXXSpecialNameExtra representing C++ destructors. 615 /// getCXXDestructorName and getCXXSpecialName can be used to obtain 616 /// a DeclarationName from the corresponding type of the destructor. 617 llvm::FoldingSet<detail::CXXSpecialNameExtra> CXXDestructorNames; 618 619 /// Manage the uniqued CXXSpecialNameExtra representing C++ conversion 620 /// functions. getCXXConversionFunctionName and getCXXSpecialName can be 621 /// used to obtain a DeclarationName from the corresponding type of the 622 /// conversion function. 623 llvm::FoldingSet<detail::CXXSpecialNameExtra> CXXConversionFunctionNames; 624 625 /// Manage the uniqued CXXOperatorIdName, which contain extra information 626 /// for the name of overloaded C++ operators. getCXXOperatorName 627 /// can be used to obtain a DeclarationName from the operator kind. 628 detail::CXXOperatorIdName CXXOperatorNames[NUM_OVERLOADED_OPERATORS]; 629 630 /// Manage the uniqued CXXLiteralOperatorIdName, which contain extra 631 /// information for the name of C++ literal operators. 632 /// getCXXLiteralOperatorName can be used to obtain a DeclarationName 633 /// from the corresponding IdentifierInfo. 634 llvm::FoldingSet<detail::CXXLiteralOperatorIdName> CXXLiteralOperatorNames; 635 636 /// Manage the uniqued CXXDeductionGuideNameExtra, which contain 637 /// extra information for the name of a C++ deduction guide. 638 /// getCXXDeductionGuideName can be used to obtain a DeclarationName 639 /// from the corresponding template declaration. 640 llvm::FoldingSet<detail::CXXDeductionGuideNameExtra> CXXDeductionGuideNames; 641 642 public: 643 DeclarationNameTable(const ASTContext &C); 644 DeclarationNameTable(const DeclarationNameTable &) = delete; 645 DeclarationNameTable &operator=(const DeclarationNameTable &) = delete; 646 DeclarationNameTable(DeclarationNameTable &&) = delete; 647 DeclarationNameTable &operator=(DeclarationNameTable &&) = delete; 648 ~DeclarationNameTable() = default; 649 650 /// Create a declaration name that is a simple identifier. getIdentifier(const IdentifierInfo * ID)651 DeclarationName getIdentifier(const IdentifierInfo *ID) { 652 return DeclarationName(ID); 653 } 654 655 /// Returns the name of a C++ constructor for the given Type. 656 DeclarationName getCXXConstructorName(CanQualType Ty); 657 658 /// Returns the name of a C++ destructor for the given Type. 659 DeclarationName getCXXDestructorName(CanQualType Ty); 660 661 /// Returns the name of a C++ deduction guide for the given template. 662 DeclarationName getCXXDeductionGuideName(TemplateDecl *TD); 663 664 /// Returns the name of a C++ conversion function for the given Type. 665 DeclarationName getCXXConversionFunctionName(CanQualType Ty); 666 667 /// Returns a declaration name for special kind of C++ name, 668 /// e.g., for a constructor, destructor, or conversion function. 669 /// Kind must be one of: 670 /// * DeclarationName::CXXConstructorName, 671 /// * DeclarationName::CXXDestructorName or 672 /// * DeclarationName::CXXConversionFunctionName 673 DeclarationName getCXXSpecialName(DeclarationName::NameKind Kind, 674 CanQualType Ty); 675 676 /// Get the name of the overloadable C++ operator corresponding to Op. getCXXOperatorName(OverloadedOperatorKind Op)677 DeclarationName getCXXOperatorName(OverloadedOperatorKind Op) { 678 return DeclarationName(&CXXOperatorNames[Op]); 679 } 680 681 /// Get the name of the literal operator function with II as the identifier. 682 DeclarationName getCXXLiteralOperatorName(const IdentifierInfo *II); 683 }; 684 685 /// DeclarationNameLoc - Additional source/type location info 686 /// for a declaration name. Needs a DeclarationName in order 687 /// to be interpreted correctly. 688 class DeclarationNameLoc { 689 // The source location for identifier stored elsewhere. 690 // struct {} Identifier; 691 692 // Type info for constructors, destructors and conversion functions. 693 // Locations (if any) for the tilde (destructor) or operator keyword 694 // (conversion) are stored elsewhere. 695 struct NT { 696 TypeSourceInfo *TInfo; 697 }; 698 699 // The location (if any) of the operator keyword is stored elsewhere. 700 struct CXXOpName { 701 SourceLocation BeginOpNameLoc; 702 SourceLocation EndOpNameLoc; 703 }; 704 705 // The location (if any) of the operator keyword is stored elsewhere. 706 struct CXXLitOpName { 707 SourceLocation OpNameLoc; 708 }; 709 710 // struct {} CXXUsingDirective; 711 // struct {} ObjCZeroArgSelector; 712 // struct {} ObjCOneArgSelector; 713 // struct {} ObjCMultiArgSelector; 714 union { 715 struct NT NamedType; 716 struct CXXOpName CXXOperatorName; 717 struct CXXLitOpName CXXLiteralOperatorName; 718 }; 719 setNamedTypeLoc(TypeSourceInfo * TInfo)720 void setNamedTypeLoc(TypeSourceInfo *TInfo) { NamedType.TInfo = TInfo; } 721 setCXXOperatorNameRange(SourceRange Range)722 void setCXXOperatorNameRange(SourceRange Range) { 723 CXXOperatorName.BeginOpNameLoc = Range.getBegin(); 724 CXXOperatorName.EndOpNameLoc = Range.getEnd(); 725 } 726 setCXXLiteralOperatorNameLoc(SourceLocation Loc)727 void setCXXLiteralOperatorNameLoc(SourceLocation Loc) { 728 CXXLiteralOperatorName.OpNameLoc = Loc; 729 } 730 731 public: 732 DeclarationNameLoc(DeclarationName Name); 733 // FIXME: this should go away once all DNLocs are properly initialized. DeclarationNameLoc()734 DeclarationNameLoc() { memset((void*) this, 0, sizeof(*this)); } 735 736 /// Returns the source type info. Assumes that the object stores location 737 /// information of a constructor, destructor or conversion operator. getNamedTypeInfo()738 TypeSourceInfo *getNamedTypeInfo() const { return NamedType.TInfo; } 739 740 /// Return the beginning location of the getCXXOperatorNameRange() range. getCXXOperatorNameBeginLoc()741 SourceLocation getCXXOperatorNameBeginLoc() const { 742 return CXXOperatorName.BeginOpNameLoc; 743 } 744 745 /// Return the end location of the getCXXOperatorNameRange() range. getCXXOperatorNameEndLoc()746 SourceLocation getCXXOperatorNameEndLoc() const { 747 return CXXOperatorName.EndOpNameLoc; 748 } 749 750 /// Return the range of the operator name (without the operator keyword). 751 /// Assumes that the object stores location information of a (non-literal) 752 /// operator. getCXXOperatorNameRange()753 SourceRange getCXXOperatorNameRange() const { 754 return SourceRange(getCXXOperatorNameBeginLoc(), 755 getCXXOperatorNameEndLoc()); 756 } 757 758 /// Return the location of the literal operator name (without the operator 759 /// keyword). Assumes that the object stores location information of a literal 760 /// operator. getCXXLiteralOperatorNameLoc()761 SourceLocation getCXXLiteralOperatorNameLoc() const { 762 return CXXLiteralOperatorName.OpNameLoc; 763 } 764 765 /// Construct location information for a constructor, destructor or conversion 766 /// operator. makeNamedTypeLoc(TypeSourceInfo * TInfo)767 static DeclarationNameLoc makeNamedTypeLoc(TypeSourceInfo *TInfo) { 768 DeclarationNameLoc DNL; 769 DNL.setNamedTypeLoc(TInfo); 770 return DNL; 771 } 772 773 /// Construct location information for a non-literal C++ operator. makeCXXOperatorNameLoc(SourceLocation BeginLoc,SourceLocation EndLoc)774 static DeclarationNameLoc makeCXXOperatorNameLoc(SourceLocation BeginLoc, 775 SourceLocation EndLoc) { 776 return makeCXXOperatorNameLoc(SourceRange(BeginLoc, EndLoc)); 777 } 778 779 /// Construct location information for a non-literal C++ operator. makeCXXOperatorNameLoc(SourceRange Range)780 static DeclarationNameLoc makeCXXOperatorNameLoc(SourceRange Range) { 781 DeclarationNameLoc DNL; 782 DNL.setCXXOperatorNameRange(Range); 783 return DNL; 784 } 785 786 /// Construct location information for a literal C++ operator. makeCXXLiteralOperatorNameLoc(SourceLocation Loc)787 static DeclarationNameLoc makeCXXLiteralOperatorNameLoc(SourceLocation Loc) { 788 DeclarationNameLoc DNL; 789 DNL.setCXXLiteralOperatorNameLoc(Loc); 790 return DNL; 791 } 792 }; 793 794 /// DeclarationNameInfo - A collector data type for bundling together 795 /// a DeclarationName and the corresponding source/type location info. 796 struct DeclarationNameInfo { 797 private: 798 /// Name - The declaration name, also encoding name kind. 799 DeclarationName Name; 800 801 /// Loc - The main source location for the declaration name. 802 SourceLocation NameLoc; 803 804 /// Info - Further source/type location info for special kinds of names. 805 DeclarationNameLoc LocInfo; 806 807 public: 808 // FIXME: remove it. 809 DeclarationNameInfo() = default; 810 DeclarationNameInfoDeclarationNameInfo811 DeclarationNameInfo(DeclarationName Name, SourceLocation NameLoc) 812 : Name(Name), NameLoc(NameLoc), LocInfo(Name) {} 813 DeclarationNameInfoDeclarationNameInfo814 DeclarationNameInfo(DeclarationName Name, SourceLocation NameLoc, 815 DeclarationNameLoc LocInfo) 816 : Name(Name), NameLoc(NameLoc), LocInfo(LocInfo) {} 817 818 /// getName - Returns the embedded declaration name. getNameDeclarationNameInfo819 DeclarationName getName() const { return Name; } 820 821 /// setName - Sets the embedded declaration name. setNameDeclarationNameInfo822 void setName(DeclarationName N) { Name = N; } 823 824 /// getLoc - Returns the main location of the declaration name. getLocDeclarationNameInfo825 SourceLocation getLoc() const { return NameLoc; } 826 827 /// setLoc - Sets the main location of the declaration name. setLocDeclarationNameInfo828 void setLoc(SourceLocation L) { NameLoc = L; } 829 getInfoDeclarationNameInfo830 const DeclarationNameLoc &getInfo() const { return LocInfo; } setInfoDeclarationNameInfo831 void setInfo(const DeclarationNameLoc &Info) { LocInfo = Info; } 832 833 /// getNamedTypeInfo - Returns the source type info associated to 834 /// the name. Assumes it is a constructor, destructor or conversion. getNamedTypeInfoDeclarationNameInfo835 TypeSourceInfo *getNamedTypeInfo() const { 836 if (Name.getNameKind() != DeclarationName::CXXConstructorName && 837 Name.getNameKind() != DeclarationName::CXXDestructorName && 838 Name.getNameKind() != DeclarationName::CXXConversionFunctionName) 839 return nullptr; 840 return LocInfo.getNamedTypeInfo(); 841 } 842 843 /// setNamedTypeInfo - Sets the source type info associated to 844 /// the name. Assumes it is a constructor, destructor or conversion. setNamedTypeInfoDeclarationNameInfo845 void setNamedTypeInfo(TypeSourceInfo *TInfo) { 846 assert(Name.getNameKind() == DeclarationName::CXXConstructorName || 847 Name.getNameKind() == DeclarationName::CXXDestructorName || 848 Name.getNameKind() == DeclarationName::CXXConversionFunctionName); 849 LocInfo = DeclarationNameLoc::makeNamedTypeLoc(TInfo); 850 } 851 852 /// getCXXOperatorNameRange - Gets the range of the operator name 853 /// (without the operator keyword). Assumes it is a (non-literal) operator. getCXXOperatorNameRangeDeclarationNameInfo854 SourceRange getCXXOperatorNameRange() const { 855 if (Name.getNameKind() != DeclarationName::CXXOperatorName) 856 return SourceRange(); 857 return LocInfo.getCXXOperatorNameRange(); 858 } 859 860 /// setCXXOperatorNameRange - Sets the range of the operator name 861 /// (without the operator keyword). Assumes it is a C++ operator. setCXXOperatorNameRangeDeclarationNameInfo862 void setCXXOperatorNameRange(SourceRange R) { 863 assert(Name.getNameKind() == DeclarationName::CXXOperatorName); 864 LocInfo = DeclarationNameLoc::makeCXXOperatorNameLoc(R); 865 } 866 867 /// getCXXLiteralOperatorNameLoc - Returns the location of the literal 868 /// operator name (not the operator keyword). 869 /// Assumes it is a literal operator. getCXXLiteralOperatorNameLocDeclarationNameInfo870 SourceLocation getCXXLiteralOperatorNameLoc() const { 871 if (Name.getNameKind() != DeclarationName::CXXLiteralOperatorName) 872 return SourceLocation(); 873 return LocInfo.getCXXLiteralOperatorNameLoc(); 874 } 875 876 /// setCXXLiteralOperatorNameLoc - Sets the location of the literal 877 /// operator name (not the operator keyword). 878 /// Assumes it is a literal operator. setCXXLiteralOperatorNameLocDeclarationNameInfo879 void setCXXLiteralOperatorNameLoc(SourceLocation Loc) { 880 assert(Name.getNameKind() == DeclarationName::CXXLiteralOperatorName); 881 LocInfo = DeclarationNameLoc::makeCXXLiteralOperatorNameLoc(Loc); 882 } 883 884 /// Determine whether this name involves a template parameter. 885 bool isInstantiationDependent() const; 886 887 /// Determine whether this name contains an unexpanded 888 /// parameter pack. 889 bool containsUnexpandedParameterPack() const; 890 891 /// getAsString - Retrieve the human-readable string for this name. 892 std::string getAsString() const; 893 894 /// printName - Print the human-readable name to a stream. 895 void printName(raw_ostream &OS, PrintingPolicy Policy) const; 896 897 /// getBeginLoc - Retrieve the location of the first token. getBeginLocDeclarationNameInfo898 SourceLocation getBeginLoc() const { return NameLoc; } 899 900 /// getSourceRange - The range of the declaration name. getSourceRangeDeclarationNameInfo901 SourceRange getSourceRange() const LLVM_READONLY { 902 return SourceRange(getBeginLoc(), getEndLoc()); 903 } 904 getEndLocDeclarationNameInfo905 SourceLocation getEndLoc() const LLVM_READONLY { 906 SourceLocation EndLoc = getEndLocPrivate(); 907 return EndLoc.isValid() ? EndLoc : getBeginLoc(); 908 } 909 910 private: 911 SourceLocation getEndLocPrivate() const; 912 }; 913 914 /// Insertion operator for partial diagnostics. This allows binding 915 /// DeclarationName's into a partial diagnostic with <<. 916 inline const StreamingDiagnostic &operator<<(const StreamingDiagnostic &PD, 917 DeclarationName N) { 918 PD.AddTaggedVal(N.getAsOpaqueInteger(), 919 DiagnosticsEngine::ak_declarationname); 920 return PD; 921 } 922 923 raw_ostream &operator<<(raw_ostream &OS, DeclarationNameInfo DNInfo); 924 925 } // namespace clang 926 927 namespace llvm { 928 929 /// Define DenseMapInfo so that DeclarationNames can be used as keys 930 /// in DenseMap and DenseSets. 931 template<> 932 struct DenseMapInfo<clang::DeclarationName> { 933 static inline clang::DeclarationName getEmptyKey() { 934 return clang::DeclarationName::getEmptyMarker(); 935 } 936 937 static inline clang::DeclarationName getTombstoneKey() { 938 return clang::DeclarationName::getTombstoneMarker(); 939 } 940 941 static unsigned getHashValue(clang::DeclarationName Name) { 942 return DenseMapInfo<void*>::getHashValue(Name.getAsOpaquePtr()); 943 } 944 945 static inline bool 946 isEqual(clang::DeclarationName LHS, clang::DeclarationName RHS) { 947 return LHS == RHS; 948 } 949 }; 950 951 template <> struct PointerLikeTypeTraits<clang::DeclarationName> { 952 static inline void *getAsVoidPointer(clang::DeclarationName P) { 953 return P.getAsOpaquePtr(); 954 } 955 static inline clang::DeclarationName getFromVoidPointer(void *P) { 956 return clang::DeclarationName::getFromOpaquePtr(P); 957 } 958 static constexpr int NumLowBitsAvailable = 0; 959 }; 960 961 } // namespace llvm 962 963 // The definition of AssumedTemplateStorage is factored out of TemplateName to 964 // resolve a cyclic dependency between it and DeclarationName (via Type). 965 namespace clang { 966 967 /// A structure for storing the information associated with a name that has 968 /// been assumed to be a template name (despite finding no TemplateDecls). 969 class AssumedTemplateStorage : public UncommonTemplateNameStorage { 970 friend class ASTContext; 971 972 AssumedTemplateStorage(DeclarationName Name) 973 : UncommonTemplateNameStorage(Assumed, 0, 0), Name(Name) {} 974 DeclarationName Name; 975 976 public: 977 /// Get the name of the template. 978 DeclarationName getDeclName() const { return Name; } 979 }; 980 981 } // namespace clang 982 983 #endif // LLVM_CLANG_AST_DECLARATIONNAME_H 984