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