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