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