LCOV - code coverage report
Current view: top level - clang/Basic - IdentifierTable.h (source / functions) Hit Total Coverage
Test: clang.info Lines: 0 1 0.0 %
Date: 2016-01-31 12:01:00 Functions: 0 1 0.0 %

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

Generated by: LCOV version 1.11