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