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