1 //===--- IdentifierTable.h - Hash table for identifier lookup ---*- 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 /// \file 11 /// \brief Defines the clang::IdentifierInfo, clang::IdentifierTable, and 12 /// clang::Selector interfaces. 13 /// 14 //===----------------------------------------------------------------------===// 15 16 #ifndef LLVM_CLANG_BASIC_IDENTIFIERTABLE_H 17 #define LLVM_CLANG_BASIC_IDENTIFIERTABLE_H 18 19 #include "clang/Basic/LLVM.h" 20 #include "clang/Basic/OperatorKinds.h" 21 #include "clang/Basic/TokenKinds.h" 22 #include "llvm/ADT/SmallString.h" 23 #include "llvm/ADT/StringMap.h" 24 #include "llvm/ADT/StringRef.h" 25 #include "llvm/Support/PointerLikeTypeTraits.h" 26 #include <cassert> 27 #include <string> 28 29 namespace llvm { 30 template <typename T> struct DenseMapInfo; 31 } 32 33 namespace clang { 34 class LangOptions; 35 class IdentifierInfo; 36 class IdentifierTable; 37 class SourceLocation; 38 class MultiKeywordSelector; // private class used by Selector 39 class DeclarationName; // AST class that stores declaration names 40 41 /// \brief A simple pair of identifier info and location. 42 typedef std::pair<IdentifierInfo*, SourceLocation> IdentifierLocPair; 43 44 45 /// One of these records is kept for each identifier that 46 /// is lexed. This contains information about whether the token was \#define'd, 47 /// is a language keyword, or if it is a front-end token of some sort (e.g. a 48 /// variable or function name). The preprocessor keeps this information in a 49 /// set, and all tok::identifier tokens have a pointer to one of these. 50 class IdentifierInfo { 51 unsigned TokenID : 9; // Front-end token ID or tok::identifier. 52 // Objective-C keyword ('protocol' in '@protocol') or builtin (__builtin_inf). 53 // First NUM_OBJC_KEYWORDS values are for Objective-C, the remaining values 54 // are for builtins. 55 unsigned ObjCOrBuiltinID :11; 56 bool HasMacro : 1; // True if there is a #define for this. 57 bool HadMacro : 1; // True if there was a #define for this. 58 bool IsExtension : 1; // True if identifier is a lang extension. 59 bool IsCXX11CompatKeyword : 1; // True if identifier is a keyword in C++11. 60 bool IsPoisoned : 1; // True if identifier is poisoned. 61 bool IsCPPOperatorKeyword : 1; // True if ident is a C++ operator keyword. 62 bool NeedsHandleIdentifier : 1; // See "RecomputeNeedsHandleIdentifier". 63 bool IsFromAST : 1; // True if identifier was loaded (at least 64 // partially) from an AST file. 65 bool ChangedAfterLoad : 1; // True if identifier has changed from the 66 // definition loaded from an AST file. 67 bool RevertedTokenID : 1; // True if RevertTokenIDToIdentifier was 68 // called. 69 bool OutOfDate : 1; // True if there may be additional 70 // information about this identifier 71 // stored externally. 72 bool IsModulesImport : 1; // True if this is the 'import' contextual 73 // keyword. 74 // 32-bit word is filled. 75 76 void *FETokenInfo; // Managed by the language front-end. 77 llvm::StringMapEntry<IdentifierInfo*> *Entry; 78 79 IdentifierInfo(const IdentifierInfo&) LLVM_DELETED_FUNCTION; 80 void operator=(const IdentifierInfo&) LLVM_DELETED_FUNCTION; 81 82 friend class IdentifierTable; 83 84 public: 85 IdentifierInfo(); 86 87 88 /// \brief Return true if this is the identifier for the specified string. 89 /// 90 /// This is intended to be used for string literals only: II->isStr("foo"). 91 template <std::size_t StrLen> isStr(const char (& Str)[StrLen])92 bool isStr(const char (&Str)[StrLen]) const { 93 return getLength() == StrLen-1 && !memcmp(getNameStart(), Str, StrLen-1); 94 } 95 96 /// \brief Return the beginning of the actual null-terminated string for this 97 /// identifier. 98 /// getNameStart()99 const char *getNameStart() const { 100 if (Entry) return Entry->getKeyData(); 101 // FIXME: This is gross. It would be best not to embed specific details 102 // of the PTH file format here. 103 // The 'this' pointer really points to a 104 // std::pair<IdentifierInfo, const char*>, where internal pointer 105 // points to the external string data. 106 typedef std::pair<IdentifierInfo, const char*> actualtype; 107 return ((const actualtype*) this)->second; 108 } 109 110 /// \brief Efficiently return the length of this identifier info. 111 /// getLength()112 unsigned getLength() const { 113 if (Entry) return Entry->getKeyLength(); 114 // FIXME: This is gross. It would be best not to embed specific details 115 // of the PTH file format here. 116 // The 'this' pointer really points to a 117 // std::pair<IdentifierInfo, const char*>, where internal pointer 118 // points to the external string data. 119 typedef std::pair<IdentifierInfo, const char*> actualtype; 120 const char* p = ((const actualtype*) this)->second - 2; 121 return (((unsigned) p[0]) | (((unsigned) p[1]) << 8)) - 1; 122 } 123 124 /// \brief Return the actual identifier string. getName()125 StringRef getName() const { 126 return StringRef(getNameStart(), getLength()); 127 } 128 129 /// \brief Return true if this identifier is \#defined to some other value. hasMacroDefinition()130 bool hasMacroDefinition() const { 131 return HasMacro; 132 } setHasMacroDefinition(bool Val)133 void setHasMacroDefinition(bool Val) { 134 if (HasMacro == Val) return; 135 136 HasMacro = Val; 137 if (Val) { 138 NeedsHandleIdentifier = 1; 139 HadMacro = true; 140 } else { 141 RecomputeNeedsHandleIdentifier(); 142 } 143 } 144 /// \brief Returns true if this identifier was \#defined to some value at any 145 /// moment. In this case there should be an entry for the identifier in the 146 /// macro history table in Preprocessor. hadMacroDefinition()147 bool hadMacroDefinition() const { 148 return HadMacro; 149 } 150 151 /// If this is a source-language token (e.g. 'for'), this API 152 /// can be used to cause the lexer to map identifiers to source-language 153 /// tokens. getTokenID()154 tok::TokenKind getTokenID() const { return (tok::TokenKind)TokenID; } 155 156 /// \brief True if RevertTokenIDToIdentifier() was called. hasRevertedTokenIDToIdentifier()157 bool hasRevertedTokenIDToIdentifier() const { return RevertedTokenID; } 158 159 /// \brief Revert TokenID to tok::identifier; used for GNU libstdc++ 4.2 160 /// compatibility. 161 /// 162 /// TokenID is normally read-only but there are 2 instances where we revert it 163 /// to tok::identifier for libstdc++ 4.2. Keep track of when this happens 164 /// using this method so we can inform serialization about it. RevertTokenIDToIdentifier()165 void RevertTokenIDToIdentifier() { 166 assert(TokenID != tok::identifier && "Already at tok::identifier"); 167 TokenID = tok::identifier; 168 RevertedTokenID = true; 169 } 170 171 /// \brief Return the preprocessor keyword ID for this identifier. 172 /// 173 /// For example, "define" will return tok::pp_define. 174 tok::PPKeywordKind getPPKeywordID() const; 175 176 /// \brief Return the Objective-C keyword ID for the this identifier. 177 /// 178 /// For example, 'class' will return tok::objc_class if ObjC is enabled. getObjCKeywordID()179 tok::ObjCKeywordKind getObjCKeywordID() const { 180 if (ObjCOrBuiltinID < tok::NUM_OBJC_KEYWORDS) 181 return tok::ObjCKeywordKind(ObjCOrBuiltinID); 182 else 183 return tok::objc_not_keyword; 184 } setObjCKeywordID(tok::ObjCKeywordKind ID)185 void setObjCKeywordID(tok::ObjCKeywordKind ID) { ObjCOrBuiltinID = ID; } 186 187 /// \brief Return a value indicating whether this is a builtin function. 188 /// 189 /// 0 is not-built-in. 1 is builtin-for-some-nonprimary-target. 190 /// 2+ are specific builtin functions. getBuiltinID()191 unsigned getBuiltinID() const { 192 if (ObjCOrBuiltinID >= tok::NUM_OBJC_KEYWORDS) 193 return ObjCOrBuiltinID - tok::NUM_OBJC_KEYWORDS; 194 else 195 return 0; 196 } setBuiltinID(unsigned ID)197 void setBuiltinID(unsigned ID) { 198 ObjCOrBuiltinID = ID + tok::NUM_OBJC_KEYWORDS; 199 assert(ObjCOrBuiltinID - unsigned(tok::NUM_OBJC_KEYWORDS) == ID 200 && "ID too large for field!"); 201 } 202 getObjCOrBuiltinID()203 unsigned getObjCOrBuiltinID() const { return ObjCOrBuiltinID; } setObjCOrBuiltinID(unsigned ID)204 void setObjCOrBuiltinID(unsigned ID) { ObjCOrBuiltinID = ID; } 205 206 /// get/setExtension - Initialize information about whether or not this 207 /// language token is an extension. This controls extension warnings, and is 208 /// only valid if a custom token ID is set. isExtensionToken()209 bool isExtensionToken() const { return IsExtension; } setIsExtensionToken(bool Val)210 void setIsExtensionToken(bool Val) { 211 IsExtension = Val; 212 if (Val) 213 NeedsHandleIdentifier = 1; 214 else 215 RecomputeNeedsHandleIdentifier(); 216 } 217 218 /// is/setIsCXX11CompatKeyword - Initialize information about whether or not 219 /// this language token is a keyword in C++11. This controls compatibility 220 /// warnings, and is only true when not parsing C++11. Once a compatibility 221 /// problem has been diagnosed with this keyword, the flag will be cleared. isCXX11CompatKeyword()222 bool isCXX11CompatKeyword() const { return IsCXX11CompatKeyword; } setIsCXX11CompatKeyword(bool Val)223 void setIsCXX11CompatKeyword(bool Val) { 224 IsCXX11CompatKeyword = Val; 225 if (Val) 226 NeedsHandleIdentifier = 1; 227 else 228 RecomputeNeedsHandleIdentifier(); 229 } 230 231 /// setIsPoisoned - Mark this identifier as poisoned. After poisoning, the 232 /// Preprocessor will emit an error every time this token is used. 233 void setIsPoisoned(bool Value = true) { 234 IsPoisoned = Value; 235 if (Value) 236 NeedsHandleIdentifier = 1; 237 else 238 RecomputeNeedsHandleIdentifier(); 239 } 240 241 /// \brief Return true if this token has been poisoned. isPoisoned()242 bool isPoisoned() const { return IsPoisoned; } 243 244 /// isCPlusPlusOperatorKeyword/setIsCPlusPlusOperatorKeyword controls whether 245 /// this identifier is a C++ alternate representation of an operator. 246 void setIsCPlusPlusOperatorKeyword(bool Val = true) { 247 IsCPPOperatorKeyword = Val; 248 if (Val) 249 NeedsHandleIdentifier = 1; 250 else 251 RecomputeNeedsHandleIdentifier(); 252 } isCPlusPlusOperatorKeyword()253 bool isCPlusPlusOperatorKeyword() const { return IsCPPOperatorKeyword; } 254 255 /// getFETokenInfo/setFETokenInfo - The language front-end is allowed to 256 /// associate arbitrary metadata with this token. 257 template<typename T> getFETokenInfo()258 T *getFETokenInfo() const { return static_cast<T*>(FETokenInfo); } setFETokenInfo(void * T)259 void setFETokenInfo(void *T) { FETokenInfo = T; } 260 261 /// \brief Return true if the Preprocessor::HandleIdentifier must be called 262 /// on a token of this identifier. 263 /// 264 /// If this returns false, we know that HandleIdentifier will not affect 265 /// the token. isHandleIdentifierCase()266 bool isHandleIdentifierCase() const { return NeedsHandleIdentifier; } 267 268 /// \brief Return true if the identifier in its current state was loaded 269 /// from an AST file. isFromAST()270 bool isFromAST() const { return IsFromAST; } 271 setIsFromAST()272 void setIsFromAST() { IsFromAST = true; } 273 274 /// \brief Determine whether this identifier has changed since it was loaded 275 /// from an AST file. hasChangedSinceDeserialization()276 bool hasChangedSinceDeserialization() const { 277 return ChangedAfterLoad; 278 } 279 280 /// \brief Note that this identifier has changed since it was loaded from 281 /// an AST file. setChangedSinceDeserialization()282 void setChangedSinceDeserialization() { 283 ChangedAfterLoad = true; 284 } 285 286 /// \brief Determine whether the information for this identifier is out of 287 /// date with respect to the external source. isOutOfDate()288 bool isOutOfDate() const { return OutOfDate; } 289 290 /// \brief Set whether the information for this identifier is out of 291 /// date with respect to the external source. setOutOfDate(bool OOD)292 void setOutOfDate(bool OOD) { 293 OutOfDate = OOD; 294 if (OOD) 295 NeedsHandleIdentifier = true; 296 else 297 RecomputeNeedsHandleIdentifier(); 298 } 299 300 /// \brief Determine whether this is the contextual keyword \c import. isModulesImport()301 bool isModulesImport() const { return IsModulesImport; } 302 303 /// \brief Set whether this identifier is the contextual keyword \c import. setModulesImport(bool I)304 void setModulesImport(bool I) { 305 IsModulesImport = I; 306 if (I) 307 NeedsHandleIdentifier = true; 308 else 309 RecomputeNeedsHandleIdentifier(); 310 } 311 312 private: 313 /// The Preprocessor::HandleIdentifier does several special (but rare) 314 /// things to identifiers of various sorts. For example, it changes the 315 /// \c for keyword token from tok::identifier to tok::for. 316 /// 317 /// This method is very tied to the definition of HandleIdentifier. Any 318 /// change to it should be reflected here. RecomputeNeedsHandleIdentifier()319 void RecomputeNeedsHandleIdentifier() { 320 NeedsHandleIdentifier = 321 (isPoisoned() | hasMacroDefinition() | isCPlusPlusOperatorKeyword() | 322 isExtensionToken() | isCXX11CompatKeyword() || isOutOfDate() || 323 isModulesImport()); 324 } 325 }; 326 327 /// \brief An RAII object for [un]poisoning an identifier within a scope. 328 /// 329 /// \p II is allowed to be null, in which case objects of this type have 330 /// no effect. 331 class PoisonIdentifierRAIIObject { 332 IdentifierInfo *const II; 333 const bool OldValue; 334 public: PoisonIdentifierRAIIObject(IdentifierInfo * II,bool NewValue)335 PoisonIdentifierRAIIObject(IdentifierInfo *II, bool NewValue) 336 : II(II), OldValue(II ? II->isPoisoned() : false) { 337 if(II) 338 II->setIsPoisoned(NewValue); 339 } 340 ~PoisonIdentifierRAIIObject()341 ~PoisonIdentifierRAIIObject() { 342 if(II) 343 II->setIsPoisoned(OldValue); 344 } 345 }; 346 347 /// \brief An iterator that walks over all of the known identifiers 348 /// in the lookup table. 349 /// 350 /// Since this iterator uses an abstract interface via virtual 351 /// functions, it uses an object-oriented interface rather than the 352 /// more standard C++ STL iterator interface. In this OO-style 353 /// iteration, the single function \c Next() provides dereference, 354 /// advance, and end-of-sequence checking in a single 355 /// operation. Subclasses of this iterator type will provide the 356 /// actual functionality. 357 class IdentifierIterator { 358 private: 359 IdentifierIterator(const IdentifierIterator &) LLVM_DELETED_FUNCTION; 360 void operator=(const IdentifierIterator &) LLVM_DELETED_FUNCTION; 361 362 protected: IdentifierIterator()363 IdentifierIterator() { } 364 365 public: 366 virtual ~IdentifierIterator(); 367 368 /// \brief Retrieve the next string in the identifier table and 369 /// advances the iterator for the following string. 370 /// 371 /// \returns The next string in the identifier table. If there is 372 /// no such string, returns an empty \c StringRef. 373 virtual StringRef Next() = 0; 374 }; 375 376 /// \brief Provides lookups to, and iteration over, IdentiferInfo objects. 377 class IdentifierInfoLookup { 378 public: 379 virtual ~IdentifierInfoLookup(); 380 381 /// \brief Return the IdentifierInfo for the specified named identifier. 382 /// 383 /// Unlike the version in IdentifierTable, this returns a pointer instead 384 /// of a reference. If the pointer is null then the IdentifierInfo cannot 385 /// be found. 386 virtual IdentifierInfo* get(StringRef Name) = 0; 387 388 /// \brief Retrieve an iterator into the set of all identifiers 389 /// known to this identifier lookup source. 390 /// 391 /// This routine provides access to all of the identifiers known to 392 /// the identifier lookup, allowing access to the contents of the 393 /// identifiers without introducing the overhead of constructing 394 /// IdentifierInfo objects for each. 395 /// 396 /// \returns A new iterator into the set of known identifiers. The 397 /// caller is responsible for deleting this iterator. 398 virtual IdentifierIterator *getIdentifiers(); 399 }; 400 401 /// \brief An abstract class used to resolve numerical identifier 402 /// references (meaningful only to some external source) into 403 /// IdentifierInfo pointers. 404 class ExternalIdentifierLookup { 405 public: 406 virtual ~ExternalIdentifierLookup(); 407 408 /// \brief Return the identifier associated with the given ID number. 409 /// 410 /// The ID 0 is associated with the NULL identifier. 411 virtual IdentifierInfo *GetIdentifier(unsigned ID) = 0; 412 }; 413 414 /// \brief Implements an efficient mapping from strings to IdentifierInfo nodes. 415 /// 416 /// This has no other purpose, but this is an extremely performance-critical 417 /// piece of the code, as each occurrence of every identifier goes through 418 /// here when lexed. 419 class IdentifierTable { 420 // Shark shows that using MallocAllocator is *much* slower than using this 421 // BumpPtrAllocator! 422 typedef llvm::StringMap<IdentifierInfo*, llvm::BumpPtrAllocator> HashTableTy; 423 HashTableTy HashTable; 424 425 IdentifierInfoLookup* ExternalLookup; 426 427 public: 428 /// \brief Create the identifier table, populating it with info about the 429 /// language keywords for the language specified by \p LangOpts. 430 IdentifierTable(const LangOptions &LangOpts, 431 IdentifierInfoLookup* externalLookup = 0); 432 433 /// \brief Set the external identifier lookup mechanism. setExternalIdentifierLookup(IdentifierInfoLookup * IILookup)434 void setExternalIdentifierLookup(IdentifierInfoLookup *IILookup) { 435 ExternalLookup = IILookup; 436 } 437 438 /// \brief Retrieve the external identifier lookup object, if any. getExternalIdentifierLookup()439 IdentifierInfoLookup *getExternalIdentifierLookup() const { 440 return ExternalLookup; 441 } 442 getAllocator()443 llvm::BumpPtrAllocator& getAllocator() { 444 return HashTable.getAllocator(); 445 } 446 447 /// \brief Return the identifier token info for the specified named 448 /// identifier. get(StringRef Name)449 IdentifierInfo &get(StringRef Name) { 450 llvm::StringMapEntry<IdentifierInfo*> &Entry = 451 HashTable.GetOrCreateValue(Name); 452 453 IdentifierInfo *II = Entry.getValue(); 454 if (II) return *II; 455 456 // No entry; if we have an external lookup, look there first. 457 if (ExternalLookup) { 458 II = ExternalLookup->get(Name); 459 if (II) { 460 // Cache in the StringMap for subsequent lookups. 461 Entry.setValue(II); 462 return *II; 463 } 464 } 465 466 // Lookups failed, make a new IdentifierInfo. 467 void *Mem = getAllocator().Allocate<IdentifierInfo>(); 468 II = new (Mem) IdentifierInfo(); 469 Entry.setValue(II); 470 471 // Make sure getName() knows how to find the IdentifierInfo 472 // contents. 473 II->Entry = &Entry; 474 475 return *II; 476 } 477 get(StringRef Name,tok::TokenKind TokenCode)478 IdentifierInfo &get(StringRef Name, tok::TokenKind TokenCode) { 479 IdentifierInfo &II = get(Name); 480 II.TokenID = TokenCode; 481 assert(II.TokenID == (unsigned) TokenCode && "TokenCode too large"); 482 return II; 483 } 484 485 /// \brief Gets an IdentifierInfo for the given name without consulting 486 /// external sources. 487 /// 488 /// This is a version of get() meant for external sources that want to 489 /// introduce or modify an identifier. If they called get(), they would 490 /// likely end up in a recursion. getOwn(StringRef Name)491 IdentifierInfo &getOwn(StringRef Name) { 492 llvm::StringMapEntry<IdentifierInfo*> &Entry = 493 HashTable.GetOrCreateValue(Name); 494 495 IdentifierInfo *II = Entry.getValue(); 496 if (!II) { 497 498 // Lookups failed, make a new IdentifierInfo. 499 void *Mem = getAllocator().Allocate<IdentifierInfo>(); 500 II = new (Mem) IdentifierInfo(); 501 Entry.setValue(II); 502 503 // Make sure getName() knows how to find the IdentifierInfo 504 // contents. 505 II->Entry = &Entry; 506 507 // If this is the 'import' contextual keyword, mark it as such. 508 if (Name.equals("import")) 509 II->setModulesImport(true); 510 } 511 512 return *II; 513 } 514 515 typedef HashTableTy::const_iterator iterator; 516 typedef HashTableTy::const_iterator const_iterator; 517 begin()518 iterator begin() const { return HashTable.begin(); } end()519 iterator end() const { return HashTable.end(); } size()520 unsigned size() const { return HashTable.size(); } 521 522 /// \brief Print some statistics to stderr that indicate how well the 523 /// hashing is doing. 524 void PrintStats() const; 525 526 void AddKeywords(const LangOptions &LangOpts); 527 }; 528 529 /// \brief A family of Objective-C methods. 530 /// 531 /// These families have no inherent meaning in the language, but are 532 /// nonetheless central enough in the existing implementations to 533 /// merit direct AST support. While, in theory, arbitrary methods can 534 /// be considered to form families, we focus here on the methods 535 /// involving allocation and retain-count management, as these are the 536 /// most "core" and the most likely to be useful to diverse clients 537 /// without extra information. 538 /// 539 /// Both selectors and actual method declarations may be classified 540 /// into families. Method families may impose additional restrictions 541 /// beyond their selector name; for example, a method called '_init' 542 /// that returns void is not considered to be in the 'init' family 543 /// (but would be if it returned 'id'). It is also possible to 544 /// explicitly change or remove a method's family. Therefore the 545 /// method's family should be considered the single source of truth. 546 enum ObjCMethodFamily { 547 /// \brief No particular method family. 548 OMF_None, 549 550 // Selectors in these families may have arbitrary arity, may be 551 // written with arbitrary leading underscores, and may have 552 // additional CamelCase "words" in their first selector chunk 553 // following the family name. 554 OMF_alloc, 555 OMF_copy, 556 OMF_init, 557 OMF_mutableCopy, 558 OMF_new, 559 560 // These families are singletons consisting only of the nullary 561 // selector with the given name. 562 OMF_autorelease, 563 OMF_dealloc, 564 OMF_finalize, 565 OMF_release, 566 OMF_retain, 567 OMF_retainCount, 568 OMF_self, 569 570 // performSelector families 571 OMF_performSelector 572 }; 573 574 /// Enough bits to store any enumerator in ObjCMethodFamily or 575 /// InvalidObjCMethodFamily. 576 enum { ObjCMethodFamilyBitWidth = 4 }; 577 578 /// \brief An invalid value of ObjCMethodFamily. 579 enum { InvalidObjCMethodFamily = (1 << ObjCMethodFamilyBitWidth) - 1 }; 580 581 /// \brief A family of Objective-C methods. 582 /// 583 /// These are family of methods whose result type is initially 'id', but 584 /// but are candidate for the result type to be changed to 'instancetype'. 585 enum ObjCInstanceTypeFamily { 586 OIT_None, 587 OIT_Array, 588 OIT_Dictionary, 589 OIT_Singleton, 590 OIT_Init, 591 OIT_ReturnsSelf 592 }; 593 594 /// \brief Smart pointer class that efficiently represents Objective-C method 595 /// names. 596 /// 597 /// This class will either point to an IdentifierInfo or a 598 /// MultiKeywordSelector (which is private). This enables us to optimize 599 /// selectors that take no arguments and selectors that take 1 argument, which 600 /// accounts for 78% of all selectors in Cocoa.h. 601 class Selector { 602 friend class Diagnostic; 603 604 enum IdentifierInfoFlag { 605 // Empty selector = 0. 606 ZeroArg = 0x1, 607 OneArg = 0x2, 608 MultiArg = 0x3, 609 ArgFlags = ZeroArg|OneArg 610 }; 611 uintptr_t InfoPtr; // a pointer to the MultiKeywordSelector or IdentifierInfo. 612 Selector(IdentifierInfo * II,unsigned nArgs)613 Selector(IdentifierInfo *II, unsigned nArgs) { 614 InfoPtr = reinterpret_cast<uintptr_t>(II); 615 assert((InfoPtr & ArgFlags) == 0 &&"Insufficiently aligned IdentifierInfo"); 616 assert(nArgs < 2 && "nArgs not equal to 0/1"); 617 InfoPtr |= nArgs+1; 618 } Selector(MultiKeywordSelector * SI)619 Selector(MultiKeywordSelector *SI) { 620 InfoPtr = reinterpret_cast<uintptr_t>(SI); 621 assert((InfoPtr & ArgFlags) == 0 &&"Insufficiently aligned IdentifierInfo"); 622 InfoPtr |= MultiArg; 623 } 624 getAsIdentifierInfo()625 IdentifierInfo *getAsIdentifierInfo() const { 626 if (getIdentifierInfoFlag() < MultiArg) 627 return reinterpret_cast<IdentifierInfo *>(InfoPtr & ~ArgFlags); 628 return 0; 629 } getMultiKeywordSelector()630 MultiKeywordSelector *getMultiKeywordSelector() const { 631 return reinterpret_cast<MultiKeywordSelector *>(InfoPtr & ~ArgFlags); 632 } 633 getIdentifierInfoFlag()634 unsigned getIdentifierInfoFlag() const { 635 return InfoPtr & ArgFlags; 636 } 637 638 static ObjCMethodFamily getMethodFamilyImpl(Selector sel); 639 640 public: 641 friend class SelectorTable; // only the SelectorTable can create these 642 friend class DeclarationName; // and the AST's DeclarationName. 643 644 /// The default ctor should only be used when creating data structures that 645 /// will contain selectors. Selector()646 Selector() : InfoPtr(0) {} Selector(uintptr_t V)647 Selector(uintptr_t V) : InfoPtr(V) {} 648 649 /// operator==/!= - Indicate whether the specified selectors are identical. 650 bool operator==(Selector RHS) const { 651 return InfoPtr == RHS.InfoPtr; 652 } 653 bool operator!=(Selector RHS) const { 654 return InfoPtr != RHS.InfoPtr; 655 } getAsOpaquePtr()656 void *getAsOpaquePtr() const { 657 return reinterpret_cast<void*>(InfoPtr); 658 } 659 660 /// \brief Determine whether this is the empty selector. isNull()661 bool isNull() const { return InfoPtr == 0; } 662 663 // Predicates to identify the selector type. isKeywordSelector()664 bool isKeywordSelector() const { 665 return getIdentifierInfoFlag() != ZeroArg; 666 } isUnarySelector()667 bool isUnarySelector() const { 668 return getIdentifierInfoFlag() == ZeroArg; 669 } 670 unsigned getNumArgs() const; 671 672 673 /// \brief Retrieve the identifier at a given position in the selector. 674 /// 675 /// Note that the identifier pointer returned may be NULL. Clients that only 676 /// care about the text of the identifier string, and not the specific, 677 /// uniqued identifier pointer, should use \c getNameForSlot(), which returns 678 /// an empty string when the identifier pointer would be NULL. 679 /// 680 /// \param argIndex The index for which we want to retrieve the identifier. 681 /// This index shall be less than \c getNumArgs() unless this is a keyword 682 /// selector, in which case 0 is the only permissible value. 683 /// 684 /// \returns the uniqued identifier for this slot, or NULL if this slot has 685 /// no corresponding identifier. 686 IdentifierInfo *getIdentifierInfoForSlot(unsigned argIndex) const; 687 688 /// \brief Retrieve the name at a given position in the selector. 689 /// 690 /// \param argIndex The index for which we want to retrieve the name. 691 /// This index shall be less than \c getNumArgs() unless this is a keyword 692 /// selector, in which case 0 is the only permissible value. 693 /// 694 /// \returns the name for this slot, which may be the empty string if no 695 /// name was supplied. 696 StringRef getNameForSlot(unsigned argIndex) const; 697 698 /// \brief Derive the full selector name (e.g. "foo:bar:") and return 699 /// it as an std::string. 700 // FIXME: Add a print method that uses a raw_ostream. 701 std::string getAsString() const; 702 703 /// \brief Derive the conventional family of this method. getMethodFamily()704 ObjCMethodFamily getMethodFamily() const { 705 return getMethodFamilyImpl(*this); 706 } 707 getEmptyMarker()708 static Selector getEmptyMarker() { 709 return Selector(uintptr_t(-1)); 710 } getTombstoneMarker()711 static Selector getTombstoneMarker() { 712 return Selector(uintptr_t(-2)); 713 } 714 715 static ObjCInstanceTypeFamily getInstTypeMethodFamily(Selector sel); 716 }; 717 718 /// \brief This table allows us to fully hide how we implement 719 /// multi-keyword caching. 720 class SelectorTable { 721 void *Impl; // Actually a SelectorTableImpl 722 SelectorTable(const SelectorTable &) LLVM_DELETED_FUNCTION; 723 void operator=(const SelectorTable &) LLVM_DELETED_FUNCTION; 724 public: 725 SelectorTable(); 726 ~SelectorTable(); 727 728 /// \brief Can create any sort of selector. 729 /// 730 /// \p NumArgs indicates whether this is a no argument selector "foo", a 731 /// single argument selector "foo:" or multi-argument "foo:bar:". 732 Selector getSelector(unsigned NumArgs, IdentifierInfo **IIV); 733 getUnarySelector(IdentifierInfo * ID)734 Selector getUnarySelector(IdentifierInfo *ID) { 735 return Selector(ID, 1); 736 } getNullarySelector(IdentifierInfo * ID)737 Selector getNullarySelector(IdentifierInfo *ID) { 738 return Selector(ID, 0); 739 } 740 741 /// \brief Return the total amount of memory allocated for managing selectors. 742 size_t getTotalMemory() const; 743 744 /// \brief Return the default setter name for the given identifier. 745 /// 746 /// This is "set" + \p Name where the initial character of \p Name 747 /// has been capitalized. 748 static SmallString<64> constructSetterName(StringRef Name); 749 750 /// \brief Return the default setter selector for the given identifier. 751 /// 752 /// This is "set" + \p Name where the initial character of \p Name 753 /// has been capitalized. 754 static Selector constructSetterSelector(IdentifierTable &Idents, 755 SelectorTable &SelTable, 756 const IdentifierInfo *Name); 757 }; 758 759 /// DeclarationNameExtra - Common base of the MultiKeywordSelector, 760 /// CXXSpecialName, and CXXOperatorIdName classes, all of which are 761 /// private classes that describe different kinds of names. 762 class DeclarationNameExtra { 763 public: 764 /// ExtraKind - The kind of "extra" information stored in the 765 /// DeclarationName. See @c ExtraKindOrNumArgs for an explanation of 766 /// how these enumerator values are used. 767 enum ExtraKind { 768 CXXConstructor = 0, 769 CXXDestructor, 770 CXXConversionFunction, 771 #define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \ 772 CXXOperator##Name, 773 #include "clang/Basic/OperatorKinds.def" 774 CXXLiteralOperator, 775 CXXUsingDirective, 776 NUM_EXTRA_KINDS 777 }; 778 779 /// ExtraKindOrNumArgs - Either the kind of C++ special name or 780 /// operator-id (if the value is one of the CXX* enumerators of 781 /// ExtraKind), in which case the DeclarationNameExtra is also a 782 /// CXXSpecialName, (for CXXConstructor, CXXDestructor, or 783 /// CXXConversionFunction) CXXOperatorIdName, or CXXLiteralOperatorName, 784 /// it may be also name common to C++ using-directives (CXXUsingDirective), 785 /// otherwise it is NUM_EXTRA_KINDS+NumArgs, where NumArgs is the number of 786 /// arguments in the Objective-C selector, in which case the 787 /// DeclarationNameExtra is also a MultiKeywordSelector. 788 unsigned ExtraKindOrNumArgs; 789 }; 790 791 } // end namespace clang 792 793 namespace llvm { 794 /// Define DenseMapInfo so that Selectors can be used as keys in DenseMap and 795 /// DenseSets. 796 template <> 797 struct DenseMapInfo<clang::Selector> { 798 static inline clang::Selector getEmptyKey() { 799 return clang::Selector::getEmptyMarker(); 800 } 801 static inline clang::Selector getTombstoneKey() { 802 return clang::Selector::getTombstoneMarker(); 803 } 804 805 static unsigned getHashValue(clang::Selector S); 806 807 static bool isEqual(clang::Selector LHS, clang::Selector RHS) { 808 return LHS == RHS; 809 } 810 }; 811 812 template <> 813 struct isPodLike<clang::Selector> { static const bool value = true; }; 814 815 template<> 816 class PointerLikeTypeTraits<clang::Selector> { 817 public: 818 static inline const void *getAsVoidPointer(clang::Selector P) { 819 return P.getAsOpaquePtr(); 820 } 821 static inline clang::Selector getFromVoidPointer(const void *P) { 822 return clang::Selector(reinterpret_cast<uintptr_t>(P)); 823 } 824 enum { NumLowBitsAvailable = 0 }; 825 }; 826 827 // Provide PointerLikeTypeTraits for IdentifierInfo pointers, which 828 // are not guaranteed to be 8-byte aligned. 829 template<> 830 class PointerLikeTypeTraits<clang::IdentifierInfo*> { 831 public: 832 static inline void *getAsVoidPointer(clang::IdentifierInfo* P) { 833 return P; 834 } 835 static inline clang::IdentifierInfo *getFromVoidPointer(void *P) { 836 return static_cast<clang::IdentifierInfo*>(P); 837 } 838 enum { NumLowBitsAvailable = 1 }; 839 }; 840 841 template<> 842 class PointerLikeTypeTraits<const clang::IdentifierInfo*> { 843 public: 844 static inline const void *getAsVoidPointer(const clang::IdentifierInfo* P) { 845 return P; 846 } 847 static inline const clang::IdentifierInfo *getFromVoidPointer(const void *P) { 848 return static_cast<const clang::IdentifierInfo*>(P); 849 } 850 enum { NumLowBitsAvailable = 1 }; 851 }; 852 853 } // end namespace llvm 854 #endif 855