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