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