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