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