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&) LLVM_DELETED_FUNCTION;
     77   void operator=(const IdentifierInfo&) LLVM_DELETED_FUNCTION;
     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   /// getFETokenInfo/setFETokenInfo - The language front-end is allowed to
    253   /// associate arbitrary metadata with this token.
    254   template<typename T>
    255   T *getFETokenInfo() const { return static_cast<T*>(FETokenInfo); }
    256   void setFETokenInfo(void *T) { FETokenInfo = T; }
    257 
    258   /// \brief Return true if the Preprocessor::HandleIdentifier must be called
    259   /// on a token of this identifier.
    260   ///
    261   /// If this returns false, we know that HandleIdentifier will not affect
    262   /// the token.
    263   bool isHandleIdentifierCase() const { return NeedsHandleIdentifier; }
    264 
    265   /// \brief Return true if the identifier in its current state was loaded
    266   /// from an AST file.
    267   bool isFromAST() const { return IsFromAST; }
    268 
    269   void setIsFromAST() { IsFromAST = true; }
    270 
    271   /// \brief Determine whether this identifier has changed since it was loaded
    272   /// from an AST file.
    273   bool hasChangedSinceDeserialization() const {
    274     return ChangedAfterLoad;
    275   }
    276 
    277   /// \brief Note that this identifier has changed since it was loaded from
    278   /// an AST file.
    279   void setChangedSinceDeserialization() {
    280     ChangedAfterLoad = true;
    281   }
    282 
    283   /// \brief Determine whether the information for this identifier is out of
    284   /// date with respect to the external source.
    285   bool isOutOfDate() const { return OutOfDate; }
    286 
    287   /// \brief Set whether the information for this identifier is out of
    288   /// date with respect to the external source.
    289   void setOutOfDate(bool OOD) {
    290     OutOfDate = OOD;
    291     if (OOD)
    292       NeedsHandleIdentifier = true;
    293     else
    294       RecomputeNeedsHandleIdentifier();
    295   }
    296 
    297   /// \brief Determine whether this is the contextual keyword \c import.
    298   bool isModulesImport() const { return IsModulesImport; }
    299 
    300   /// \brief Set whether this identifier is the contextual keyword \c import.
    301   void setModulesImport(bool I) {
    302     IsModulesImport = I;
    303     if (I)
    304       NeedsHandleIdentifier = true;
    305     else
    306       RecomputeNeedsHandleIdentifier();
    307   }
    308 
    309 private:
    310   /// The Preprocessor::HandleIdentifier does several special (but rare)
    311   /// things to identifiers of various sorts.  For example, it changes the
    312   /// \c for keyword token from tok::identifier to tok::for.
    313   ///
    314   /// This method is very tied to the definition of HandleIdentifier.  Any
    315   /// change to it should be reflected here.
    316   void RecomputeNeedsHandleIdentifier() {
    317     NeedsHandleIdentifier =
    318       (isPoisoned() | hasMacroDefinition() | isCPlusPlusOperatorKeyword() |
    319        isExtensionToken() | isCXX11CompatKeyword() || isOutOfDate() ||
    320        isModulesImport());
    321   }
    322 };
    323 
    324 /// \brief An RAII object for [un]poisoning an identifier within a scope.
    325 ///
    326 /// \p II is allowed to be null, in which case objects of this type have
    327 /// no effect.
    328 class PoisonIdentifierRAIIObject {
    329   IdentifierInfo *const II;
    330   const bool OldValue;
    331 public:
    332   PoisonIdentifierRAIIObject(IdentifierInfo *II, bool NewValue)
    333     : II(II), OldValue(II ? II->isPoisoned() : false) {
    334     if(II)
    335       II->setIsPoisoned(NewValue);
    336   }
    337 
    338   ~PoisonIdentifierRAIIObject() {
    339     if(II)
    340       II->setIsPoisoned(OldValue);
    341   }
    342 };
    343 
    344 /// \brief An iterator that walks over all of the known identifiers
    345 /// in the lookup table.
    346 ///
    347 /// Since this iterator uses an abstract interface via virtual
    348 /// functions, it uses an object-oriented interface rather than the
    349 /// more standard C++ STL iterator interface. In this OO-style
    350 /// iteration, the single function \c Next() provides dereference,
    351 /// advance, and end-of-sequence checking in a single
    352 /// operation. Subclasses of this iterator type will provide the
    353 /// actual functionality.
    354 class IdentifierIterator {
    355 private:
    356   IdentifierIterator(const IdentifierIterator &) LLVM_DELETED_FUNCTION;
    357   void operator=(const IdentifierIterator &) LLVM_DELETED_FUNCTION;
    358 
    359 protected:
    360   IdentifierIterator() { }
    361 
    362 public:
    363   virtual ~IdentifierIterator();
    364 
    365   /// \brief Retrieve the next string in the identifier table and
    366   /// advances the iterator for the following string.
    367   ///
    368   /// \returns The next string in the identifier table. If there is
    369   /// no such string, returns an empty \c StringRef.
    370   virtual StringRef Next() = 0;
    371 };
    372 
    373 /// \brief Provides lookups to, and iteration over, IdentiferInfo objects.
    374 class IdentifierInfoLookup {
    375 public:
    376   virtual ~IdentifierInfoLookup();
    377 
    378   /// \brief Return the IdentifierInfo for the specified named identifier.
    379   ///
    380   /// Unlike the version in IdentifierTable, this returns a pointer instead
    381   /// of a reference.  If the pointer is null then the IdentifierInfo cannot
    382   /// be found.
    383   virtual IdentifierInfo* get(StringRef Name) = 0;
    384 
    385   /// \brief Retrieve an iterator into the set of all identifiers
    386   /// known to this identifier lookup source.
    387   ///
    388   /// This routine provides access to all of the identifiers known to
    389   /// the identifier lookup, allowing access to the contents of the
    390   /// identifiers without introducing the overhead of constructing
    391   /// IdentifierInfo objects for each.
    392   ///
    393   /// \returns A new iterator into the set of known identifiers. The
    394   /// caller is responsible for deleting this iterator.
    395   virtual IdentifierIterator *getIdentifiers();
    396 };
    397 
    398 /// \brief An abstract class used to resolve numerical identifier
    399 /// references (meaningful only to some external source) into
    400 /// IdentifierInfo pointers.
    401 class ExternalIdentifierLookup {
    402 public:
    403   virtual ~ExternalIdentifierLookup();
    404 
    405   /// \brief Return the identifier associated with the given ID number.
    406   ///
    407   /// The ID 0 is associated with the NULL identifier.
    408   virtual IdentifierInfo *GetIdentifier(unsigned ID) = 0;
    409 };
    410 
    411 /// \brief Implements an efficient mapping from strings to IdentifierInfo nodes.
    412 ///
    413 /// This has no other purpose, but this is an extremely performance-critical
    414 /// piece of the code, as each occurrence of every identifier goes through
    415 /// here when lexed.
    416 class IdentifierTable {
    417   // Shark shows that using MallocAllocator is *much* slower than using this
    418   // BumpPtrAllocator!
    419   typedef llvm::StringMap<IdentifierInfo*, llvm::BumpPtrAllocator> HashTableTy;
    420   HashTableTy HashTable;
    421 
    422   IdentifierInfoLookup* ExternalLookup;
    423 
    424 public:
    425   /// \brief Create the identifier table, populating it with info about the
    426   /// language keywords for the language specified by \p LangOpts.
    427   IdentifierTable(const LangOptions &LangOpts,
    428                   IdentifierInfoLookup* externalLookup = nullptr);
    429 
    430   /// \brief Set the external identifier lookup mechanism.
    431   void setExternalIdentifierLookup(IdentifierInfoLookup *IILookup) {
    432     ExternalLookup = IILookup;
    433   }
    434 
    435   /// \brief Retrieve the external identifier lookup object, if any.
    436   IdentifierInfoLookup *getExternalIdentifierLookup() const {
    437     return ExternalLookup;
    438   }
    439 
    440   llvm::BumpPtrAllocator& getAllocator() {
    441     return HashTable.getAllocator();
    442   }
    443 
    444   /// \brief Return the identifier token info for the specified named
    445   /// identifier.
    446   IdentifierInfo &get(StringRef Name) {
    447     llvm::StringMapEntry<IdentifierInfo*> &Entry =
    448       HashTable.GetOrCreateValue(Name);
    449 
    450     IdentifierInfo *II = Entry.getValue();
    451     if (II) return *II;
    452 
    453     // No entry; if we have an external lookup, look there first.
    454     if (ExternalLookup) {
    455       II = ExternalLookup->get(Name);
    456       if (II) {
    457         // Cache in the StringMap for subsequent lookups.
    458         Entry.setValue(II);
    459         return *II;
    460       }
    461     }
    462 
    463     // Lookups failed, make a new IdentifierInfo.
    464     void *Mem = getAllocator().Allocate<IdentifierInfo>();
    465     II = new (Mem) IdentifierInfo();
    466     Entry.setValue(II);
    467 
    468     // Make sure getName() knows how to find the IdentifierInfo
    469     // contents.
    470     II->Entry = &Entry;
    471 
    472     return *II;
    473   }
    474 
    475   IdentifierInfo &get(StringRef Name, tok::TokenKind TokenCode) {
    476     IdentifierInfo &II = get(Name);
    477     II.TokenID = TokenCode;
    478     assert(II.TokenID == (unsigned) TokenCode && "TokenCode too large");
    479     return II;
    480   }
    481 
    482   /// \brief Gets an IdentifierInfo for the given name without consulting
    483   ///        external sources.
    484   ///
    485   /// This is a version of get() meant for external sources that want to
    486   /// introduce or modify an identifier. If they called get(), they would
    487   /// likely end up in a recursion.
    488   IdentifierInfo &getOwn(StringRef Name) {
    489     llvm::StringMapEntry<IdentifierInfo*> &Entry =
    490       HashTable.GetOrCreateValue(Name);
    491 
    492     IdentifierInfo *II = Entry.getValue();
    493     if (!II) {
    494 
    495       // Lookups failed, make a new IdentifierInfo.
    496       void *Mem = getAllocator().Allocate<IdentifierInfo>();
    497       II = new (Mem) IdentifierInfo();
    498       Entry.setValue(II);
    499 
    500       // Make sure getName() knows how to find the IdentifierInfo
    501       // contents.
    502       II->Entry = &Entry;
    503 
    504       // If this is the 'import' contextual keyword, mark it as such.
    505       if (Name.equals("import"))
    506         II->setModulesImport(true);
    507     }
    508 
    509     return *II;
    510   }
    511 
    512   typedef HashTableTy::const_iterator iterator;
    513   typedef HashTableTy::const_iterator const_iterator;
    514 
    515   iterator begin() const { return HashTable.begin(); }
    516   iterator end() const   { return HashTable.end(); }
    517   unsigned size() const { return HashTable.size(); }
    518 
    519   /// \brief Print some statistics to stderr that indicate how well the
    520   /// hashing is doing.
    521   void PrintStats() const;
    522 
    523   void AddKeywords(const LangOptions &LangOpts);
    524 };
    525 
    526 /// \brief A family of Objective-C methods.
    527 ///
    528 /// These families have no inherent meaning in the language, but are
    529 /// nonetheless central enough in the existing implementations to
    530 /// merit direct AST support.  While, in theory, arbitrary methods can
    531 /// be considered to form families, we focus here on the methods
    532 /// involving allocation and retain-count management, as these are the
    533 /// most "core" and the most likely to be useful to diverse clients
    534 /// without extra information.
    535 ///
    536 /// Both selectors and actual method declarations may be classified
    537 /// into families.  Method families may impose additional restrictions
    538 /// beyond their selector name; for example, a method called '_init'
    539 /// that returns void is not considered to be in the 'init' family
    540 /// (but would be if it returned 'id').  It is also possible to
    541 /// explicitly change or remove a method's family.  Therefore the
    542 /// method's family should be considered the single source of truth.
    543 enum ObjCMethodFamily {
    544   /// \brief No particular method family.
    545   OMF_None,
    546 
    547   // Selectors in these families may have arbitrary arity, may be
    548   // written with arbitrary leading underscores, and may have
    549   // additional CamelCase "words" in their first selector chunk
    550   // following the family name.
    551   OMF_alloc,
    552   OMF_copy,
    553   OMF_init,
    554   OMF_mutableCopy,
    555   OMF_new,
    556 
    557   // These families are singletons consisting only of the nullary
    558   // selector with the given name.
    559   OMF_autorelease,
    560   OMF_dealloc,
    561   OMF_finalize,
    562   OMF_release,
    563   OMF_retain,
    564   OMF_retainCount,
    565   OMF_self,
    566 
    567   // performSelector families
    568   OMF_performSelector
    569 };
    570 
    571 /// Enough bits to store any enumerator in ObjCMethodFamily or
    572 /// InvalidObjCMethodFamily.
    573 enum { ObjCMethodFamilyBitWidth = 4 };
    574 
    575 /// \brief An invalid value of ObjCMethodFamily.
    576 enum { InvalidObjCMethodFamily = (1 << ObjCMethodFamilyBitWidth) - 1 };
    577 
    578 /// \brief A family of Objective-C methods.
    579 ///
    580 /// These are family of methods whose result type is initially 'id', but
    581 /// but are candidate for the result type to be changed to 'instancetype'.
    582 enum ObjCInstanceTypeFamily {
    583   OIT_None,
    584   OIT_Array,
    585   OIT_Dictionary,
    586   OIT_Singleton,
    587   OIT_Init,
    588   OIT_ReturnsSelf
    589 };
    590 
    591 /// \brief Smart pointer class that efficiently represents Objective-C method
    592 /// names.
    593 ///
    594 /// This class will either point to an IdentifierInfo or a
    595 /// MultiKeywordSelector (which is private). This enables us to optimize
    596 /// selectors that take no arguments and selectors that take 1 argument, which
    597 /// accounts for 78% of all selectors in Cocoa.h.
    598 class Selector {
    599   friend class Diagnostic;
    600 
    601   enum IdentifierInfoFlag {
    602     // Empty selector = 0.
    603     ZeroArg  = 0x1,
    604     OneArg   = 0x2,
    605     MultiArg = 0x3,
    606     ArgFlags = ZeroArg|OneArg
    607   };
    608   uintptr_t InfoPtr; // a pointer to the MultiKeywordSelector or IdentifierInfo.
    609 
    610   Selector(IdentifierInfo *II, unsigned nArgs) {
    611     InfoPtr = reinterpret_cast<uintptr_t>(II);
    612     assert((InfoPtr & ArgFlags) == 0 &&"Insufficiently aligned IdentifierInfo");
    613     assert(nArgs < 2 && "nArgs not equal to 0/1");
    614     InfoPtr |= nArgs+1;
    615   }
    616   Selector(MultiKeywordSelector *SI) {
    617     InfoPtr = reinterpret_cast<uintptr_t>(SI);
    618     assert((InfoPtr & ArgFlags) == 0 &&"Insufficiently aligned IdentifierInfo");
    619     InfoPtr |= MultiArg;
    620   }
    621 
    622   IdentifierInfo *getAsIdentifierInfo() const {
    623     if (getIdentifierInfoFlag() < MultiArg)
    624       return reinterpret_cast<IdentifierInfo *>(InfoPtr & ~ArgFlags);
    625     return nullptr;
    626   }
    627   MultiKeywordSelector *getMultiKeywordSelector() const {
    628     return reinterpret_cast<MultiKeywordSelector *>(InfoPtr & ~ArgFlags);
    629   }
    630 
    631   unsigned getIdentifierInfoFlag() const {
    632     return InfoPtr & ArgFlags;
    633   }
    634 
    635   static ObjCMethodFamily getMethodFamilyImpl(Selector sel);
    636 
    637 public:
    638   friend class SelectorTable; // only the SelectorTable can create these
    639   friend class DeclarationName; // and the AST's DeclarationName.
    640 
    641   /// The default ctor should only be used when creating data structures that
    642   ///  will contain selectors.
    643   Selector() : InfoPtr(0) {}
    644   Selector(uintptr_t V) : InfoPtr(V) {}
    645 
    646   /// operator==/!= - Indicate whether the specified selectors are identical.
    647   bool operator==(Selector RHS) const {
    648     return InfoPtr == RHS.InfoPtr;
    649   }
    650   bool operator!=(Selector RHS) const {
    651     return InfoPtr != RHS.InfoPtr;
    652   }
    653   void *getAsOpaquePtr() const {
    654     return reinterpret_cast<void*>(InfoPtr);
    655   }
    656 
    657   /// \brief Determine whether this is the empty selector.
    658   bool isNull() const { return InfoPtr == 0; }
    659 
    660   // Predicates to identify the selector type.
    661   bool isKeywordSelector() const {
    662     return getIdentifierInfoFlag() != ZeroArg;
    663   }
    664   bool isUnarySelector() const {
    665     return getIdentifierInfoFlag() == ZeroArg;
    666   }
    667   unsigned getNumArgs() const;
    668 
    669 
    670   /// \brief Retrieve the identifier at a given position in the selector.
    671   ///
    672   /// Note that the identifier pointer returned may be NULL. Clients that only
    673   /// care about the text of the identifier string, and not the specific,
    674   /// uniqued identifier pointer, should use \c getNameForSlot(), which returns
    675   /// an empty string when the identifier pointer would be NULL.
    676   ///
    677   /// \param argIndex The index for which we want to retrieve the identifier.
    678   /// This index shall be less than \c getNumArgs() unless this is a keyword
    679   /// selector, in which case 0 is the only permissible value.
    680   ///
    681   /// \returns the uniqued identifier for this slot, or NULL if this slot has
    682   /// no corresponding identifier.
    683   IdentifierInfo *getIdentifierInfoForSlot(unsigned argIndex) const;
    684 
    685   /// \brief Retrieve the name at a given position in the selector.
    686   ///
    687   /// \param argIndex The index for which we want to retrieve the name.
    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 name for this slot, which may be the empty string if no
    692   /// name was supplied.
    693   StringRef getNameForSlot(unsigned argIndex) const;
    694 
    695   /// \brief Derive the full selector name (e.g. "foo:bar:") and return
    696   /// it as an std::string.
    697   std::string getAsString() const;
    698 
    699   /// \brief Prints the full selector name (e.g. "foo:bar:").
    700   void print(llvm::raw_ostream &OS) const;
    701 
    702   /// \brief Derive the conventional family of this method.
    703   ObjCMethodFamily getMethodFamily() const {
    704     return getMethodFamilyImpl(*this);
    705   }
    706 
    707   static Selector getEmptyMarker() {
    708     return Selector(uintptr_t(-1));
    709   }
    710   static Selector getTombstoneMarker() {
    711     return Selector(uintptr_t(-2));
    712   }
    713 
    714   static ObjCInstanceTypeFamily getInstTypeMethodFamily(Selector sel);
    715 };
    716 
    717 /// \brief This table allows us to fully hide how we implement
    718 /// multi-keyword caching.
    719 class SelectorTable {
    720   void *Impl;  // Actually a SelectorTableImpl
    721   SelectorTable(const SelectorTable &) LLVM_DELETED_FUNCTION;
    722   void operator=(const SelectorTable &) LLVM_DELETED_FUNCTION;
    723 public:
    724   SelectorTable();
    725   ~SelectorTable();
    726 
    727   /// \brief Can create any sort of selector.
    728   ///
    729   /// \p NumArgs indicates whether this is a no argument selector "foo", a
    730   /// single argument selector "foo:" or multi-argument "foo:bar:".
    731   Selector getSelector(unsigned NumArgs, IdentifierInfo **IIV);
    732 
    733   Selector getUnarySelector(IdentifierInfo *ID) {
    734     return Selector(ID, 1);
    735   }
    736   Selector getNullarySelector(IdentifierInfo *ID) {
    737     return Selector(ID, 0);
    738   }
    739 
    740   /// \brief Return the total amount of memory allocated for managing selectors.
    741   size_t getTotalMemory() const;
    742 
    743   /// \brief Return the default setter name for the given identifier.
    744   ///
    745   /// This is "set" + \p Name where the initial character of \p Name
    746   /// has been capitalized.
    747   static SmallString<64> constructSetterName(StringRef Name);
    748 
    749   /// \brief Return the default setter selector for the given identifier.
    750   ///
    751   /// This is "set" + \p Name where the initial character of \p Name
    752   /// has been capitalized.
    753   static Selector constructSetterSelector(IdentifierTable &Idents,
    754                                           SelectorTable &SelTable,
    755                                           const IdentifierInfo *Name);
    756 };
    757 
    758 /// DeclarationNameExtra - Common base of the MultiKeywordSelector,
    759 /// CXXSpecialName, and CXXOperatorIdName classes, all of which are
    760 /// private classes that describe different kinds of names.
    761 class DeclarationNameExtra {
    762 public:
    763   /// ExtraKind - The kind of "extra" information stored in the
    764   /// DeclarationName. See @c ExtraKindOrNumArgs for an explanation of
    765   /// how these enumerator values are used.
    766   enum ExtraKind {
    767     CXXConstructor = 0,
    768     CXXDestructor,
    769     CXXConversionFunction,
    770 #define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
    771     CXXOperator##Name,
    772 #include "clang/Basic/OperatorKinds.def"
    773     CXXLiteralOperator,
    774     CXXUsingDirective,
    775     NUM_EXTRA_KINDS
    776   };
    777 
    778   /// ExtraKindOrNumArgs - Either the kind of C++ special name or
    779   /// operator-id (if the value is one of the CXX* enumerators of
    780   /// ExtraKind), in which case the DeclarationNameExtra is also a
    781   /// CXXSpecialName, (for CXXConstructor, CXXDestructor, or
    782   /// CXXConversionFunction) CXXOperatorIdName, or CXXLiteralOperatorName,
    783   /// it may be also name common to C++ using-directives (CXXUsingDirective),
    784   /// otherwise it is NUM_EXTRA_KINDS+NumArgs, where NumArgs is the number of
    785   /// arguments in the Objective-C selector, in which case the
    786   /// DeclarationNameExtra is also a MultiKeywordSelector.
    787   unsigned ExtraKindOrNumArgs;
    788 };
    789 
    790 }  // end namespace clang
    791 
    792 namespace llvm {
    793 /// Define DenseMapInfo so that Selectors can be used as keys in DenseMap and
    794 /// DenseSets.
    795 template <>
    796 struct DenseMapInfo<clang::Selector> {
    797   static inline clang::Selector getEmptyKey() {
    798     return clang::Selector::getEmptyMarker();
    799   }
    800   static inline clang::Selector getTombstoneKey() {
    801     return clang::Selector::getTombstoneMarker();
    802   }
    803 
    804   static unsigned getHashValue(clang::Selector S);
    805 
    806   static bool isEqual(clang::Selector LHS, clang::Selector RHS) {
    807     return LHS == RHS;
    808   }
    809 };
    810 
    811 template <>
    812 struct isPodLike<clang::Selector> { static const bool value = true; };
    813 
    814 template <typename T> class PointerLikeTypeTraits;
    815 
    816 template<>
    817 class PointerLikeTypeTraits<clang::Selector> {
    818 public:
    819   static inline const void *getAsVoidPointer(clang::Selector P) {
    820     return P.getAsOpaquePtr();
    821   }
    822   static inline clang::Selector getFromVoidPointer(const void *P) {
    823     return clang::Selector(reinterpret_cast<uintptr_t>(P));
    824   }
    825   enum { NumLowBitsAvailable = 0 };
    826 };
    827 
    828 // Provide PointerLikeTypeTraits for IdentifierInfo pointers, which
    829 // are not guaranteed to be 8-byte aligned.
    830 template<>
    831 class PointerLikeTypeTraits<clang::IdentifierInfo*> {
    832 public:
    833   static inline void *getAsVoidPointer(clang::IdentifierInfo* P) {
    834     return P;
    835   }
    836   static inline clang::IdentifierInfo *getFromVoidPointer(void *P) {
    837     return static_cast<clang::IdentifierInfo*>(P);
    838   }
    839   enum { NumLowBitsAvailable = 1 };
    840 };
    841 
    842 template<>
    843 class PointerLikeTypeTraits<const clang::IdentifierInfo*> {
    844 public:
    845   static inline const void *getAsVoidPointer(const clang::IdentifierInfo* P) {
    846     return P;
    847   }
    848   static inline const clang::IdentifierInfo *getFromVoidPointer(const void *P) {
    849     return static_cast<const clang::IdentifierInfo*>(P);
    850   }
    851   enum { NumLowBitsAvailable = 1 };
    852 };
    853 
    854 }  // end namespace llvm
    855 #endif
    856