1 //===--- IdentifierTable.h - Hash table for identifier lookup ---*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 /// \brief Defines the clang::IdentifierInfo, clang::IdentifierTable, and
12 /// clang::Selector interfaces.
14 //===----------------------------------------------------------------------===//
16 #ifndef LLVM_CLANG_BASIC_IDENTIFIERTABLE_H
17 #define LLVM_CLANG_BASIC_IDENTIFIERTABLE_H
19 #include "clang/Basic/LLVM.h"
20 #include "clang/Basic/TokenKinds.h"
21 #include "llvm/ADT/SmallString.h"
22 #include "llvm/ADT/StringMap.h"
23 #include "llvm/ADT/StringRef.h"
24 #include "llvm/Support/Allocator.h"
35 template <typename T> struct DenseMapInfo;
37 } // end namespace llvm
43 class IdentifierTable;
45 class MultiKeywordSelector; // private class used by Selector
46 class DeclarationName; // AST class that stores declaration names
48 /// \brief A simple pair of identifier info and location.
49 typedef std::pair<IdentifierInfo*, SourceLocation> IdentifierLocPair;
51 /// One of these records is kept for each identifier that
52 /// is lexed. This contains information about whether the token was \#define'd,
53 /// is a language keyword, or if it is a front-end token of some sort (e.g. a
54 /// variable or function name). The preprocessor keeps this information in a
55 /// set, and all tok::identifier tokens have a pointer to one of these.
56 class IdentifierInfo {
57 friend class IdentifierTable;
59 unsigned TokenID : 9; // Front-end token ID or tok::identifier.
60 // Objective-C keyword ('protocol' in '@protocol') or builtin (__builtin_inf).
61 // First NUM_OBJC_KEYWORDS values are for Objective-C, the remaining values
63 unsigned ObjCOrBuiltinID :13;
64 bool HasMacro : 1; // True if there is a #define for this.
65 bool HadMacro : 1; // True if there was a #define for this.
66 bool IsExtension : 1; // True if identifier is a lang extension.
67 bool IsFutureCompatKeyword : 1; // True if identifier is a keyword in a
68 // newer Standard or proposed Standard.
69 bool IsPoisoned : 1; // True if identifier is poisoned.
70 bool IsCPPOperatorKeyword : 1; // True if ident is a C++ operator keyword.
71 bool NeedsHandleIdentifier : 1; // See "RecomputeNeedsHandleIdentifier".
72 bool IsFromAST : 1; // True if identifier was loaded (at least
73 // partially) from an AST file.
74 bool ChangedAfterLoad : 1; // True if identifier has changed from the
75 // definition loaded from an AST file.
76 bool FEChangedAfterLoad : 1; // True if identifier's frontend information
77 // has changed from the definition loaded
79 bool RevertedTokenID : 1; // True if revertTokenIDToIdentifier was
81 bool OutOfDate : 1; // True if there may be additional
82 // information about this identifier
84 bool IsModulesImport : 1; // True if this is the 'import' contextual
86 // 29 bit left in 64-bit word.
88 void *FETokenInfo; // Managed by the language front-end.
89 llvm::StringMapEntry<IdentifierInfo*> *Entry;
93 IdentifierInfo(const IdentifierInfo &) = delete;
94 IdentifierInfo &operator=(const IdentifierInfo &) = delete;
96 /// \brief Return true if this is the identifier for the specified string.
98 /// This is intended to be used for string literals only: II->isStr("foo").
99 template <std::size_t StrLen>
100 bool isStr(const char (&Str)[StrLen]) const {
101 return getLength() == StrLen-1 &&
102 memcmp(getNameStart(), Str, StrLen-1) == 0;
105 /// \brief Return the beginning of the actual null-terminated string for this
108 const char *getNameStart() const {
109 if (Entry) return Entry->getKeyData();
110 // FIXME: This is gross. It would be best not to embed specific details
111 // of the PTH file format here.
112 // The 'this' pointer really points to a
113 // std::pair<IdentifierInfo, const char*>, where internal pointer
114 // points to the external string data.
115 typedef std::pair<IdentifierInfo, const char*> actualtype;
116 return ((const actualtype*) this)->second;
119 /// \brief Efficiently return the length of this identifier info.
121 unsigned getLength() const {
122 if (Entry) return Entry->getKeyLength();
123 // FIXME: This is gross. It would be best not to embed specific details
124 // of the PTH file format here.
125 // The 'this' pointer really points to a
126 // std::pair<IdentifierInfo, const char*>, where internal pointer
127 // points to the external string data.
128 typedef std::pair<IdentifierInfo, const char*> actualtype;
129 const char* p = ((const actualtype*) this)->second - 2;
130 return (((unsigned) p[0]) | (((unsigned) p[1]) << 8)) - 1;
133 /// \brief Return the actual identifier string.
134 StringRef getName() const {
135 return StringRef(getNameStart(), getLength());
138 /// \brief Return true if this identifier is \#defined to some other value.
139 /// \note The current definition may be in a module and not currently visible.
140 bool hasMacroDefinition() const {
143 void setHasMacroDefinition(bool Val) {
144 if (HasMacro == Val) return;
148 NeedsHandleIdentifier = true;
151 RecomputeNeedsHandleIdentifier();
154 /// \brief Returns true if this identifier was \#defined to some value at any
155 /// moment. In this case there should be an entry for the identifier in the
156 /// macro history table in Preprocessor.
157 bool hadMacroDefinition() const {
161 /// If this is a source-language token (e.g. 'for'), this API
162 /// can be used to cause the lexer to map identifiers to source-language
164 tok::TokenKind getTokenID() const { return (tok::TokenKind)TokenID; }
166 /// \brief True if revertTokenIDToIdentifier() was called.
167 bool hasRevertedTokenIDToIdentifier() const { return RevertedTokenID; }
169 /// \brief Revert TokenID to tok::identifier; used for GNU libstdc++ 4.2
172 /// TokenID is normally read-only but there are 2 instances where we revert it
173 /// to tok::identifier for libstdc++ 4.2. Keep track of when this happens
174 /// using this method so we can inform serialization about it.
175 void revertTokenIDToIdentifier() {
176 assert(TokenID != tok::identifier && "Already at tok::identifier");
177 TokenID = tok::identifier;
178 RevertedTokenID = true;
180 void revertIdentifierToTokenID(tok::TokenKind TK) {
181 assert(TokenID == tok::identifier && "Should be at tok::identifier");
183 RevertedTokenID = false;
186 /// \brief Return the preprocessor keyword ID for this identifier.
188 /// For example, "define" will return tok::pp_define.
189 tok::PPKeywordKind getPPKeywordID() const;
191 /// \brief Return the Objective-C keyword ID for the this identifier.
193 /// For example, 'class' will return tok::objc_class if ObjC is enabled.
194 tok::ObjCKeywordKind getObjCKeywordID() const {
195 if (ObjCOrBuiltinID < tok::NUM_OBJC_KEYWORDS)
196 return tok::ObjCKeywordKind(ObjCOrBuiltinID);
198 return tok::objc_not_keyword;
200 void setObjCKeywordID(tok::ObjCKeywordKind ID) { ObjCOrBuiltinID = ID; }
202 /// \brief True if setNotBuiltin() was called.
203 bool hasRevertedBuiltin() const {
204 return ObjCOrBuiltinID == tok::NUM_OBJC_KEYWORDS;
207 /// \brief Revert the identifier to a non-builtin identifier. We do this if
208 /// the name of a known builtin library function is used to declare that
209 /// function, but an unexpected type is specified.
210 void revertBuiltin() {
214 /// \brief Return a value indicating whether this is a builtin function.
216 /// 0 is not-built-in. 1+ are specific builtin functions.
217 unsigned getBuiltinID() const {
218 if (ObjCOrBuiltinID >= tok::NUM_OBJC_KEYWORDS)
219 return ObjCOrBuiltinID - tok::NUM_OBJC_KEYWORDS;
223 void setBuiltinID(unsigned ID) {
224 ObjCOrBuiltinID = ID + tok::NUM_OBJC_KEYWORDS;
225 assert(ObjCOrBuiltinID - unsigned(tok::NUM_OBJC_KEYWORDS) == ID
226 && "ID too large for field!");
229 unsigned getObjCOrBuiltinID() const { return ObjCOrBuiltinID; }
230 void setObjCOrBuiltinID(unsigned ID) { ObjCOrBuiltinID = ID; }
232 /// get/setExtension - Initialize information about whether or not this
233 /// language token is an extension. This controls extension warnings, and is
234 /// only valid if a custom token ID is set.
235 bool isExtensionToken() const { return IsExtension; }
236 void setIsExtensionToken(bool Val) {
239 NeedsHandleIdentifier = true;
241 RecomputeNeedsHandleIdentifier();
244 /// is/setIsFutureCompatKeyword - Initialize information about whether or not
245 /// this language token is a keyword in a newer or proposed Standard. This
246 /// controls compatibility warnings, and is only true when not parsing the
247 /// corresponding Standard. Once a compatibility problem has been diagnosed
248 /// with this keyword, the flag will be cleared.
249 bool isFutureCompatKeyword() const { return IsFutureCompatKeyword; }
250 void setIsFutureCompatKeyword(bool Val) {
251 IsFutureCompatKeyword = Val;
253 NeedsHandleIdentifier = true;
255 RecomputeNeedsHandleIdentifier();
258 /// setIsPoisoned - Mark this identifier as poisoned. After poisoning, the
259 /// Preprocessor will emit an error every time this token is used.
260 void setIsPoisoned(bool Value = true) {
263 NeedsHandleIdentifier = true;
265 RecomputeNeedsHandleIdentifier();
268 /// \brief Return true if this token has been poisoned.
269 bool isPoisoned() const { return IsPoisoned; }
271 /// isCPlusPlusOperatorKeyword/setIsCPlusPlusOperatorKeyword controls whether
272 /// this identifier is a C++ alternate representation of an operator.
273 void setIsCPlusPlusOperatorKeyword(bool Val = true) {
274 IsCPPOperatorKeyword = Val;
276 NeedsHandleIdentifier = true;
278 RecomputeNeedsHandleIdentifier();
280 bool isCPlusPlusOperatorKeyword() const { return IsCPPOperatorKeyword; }
282 /// \brief Return true if this token is a keyword in the specified language.
283 bool isKeyword(const LangOptions &LangOpts);
285 /// getFETokenInfo/setFETokenInfo - The language front-end is allowed to
286 /// associate arbitrary metadata with this token.
288 T *getFETokenInfo() const { return static_cast<T*>(FETokenInfo); }
289 void setFETokenInfo(void *T) { FETokenInfo = T; }
291 /// \brief Return true if the Preprocessor::HandleIdentifier must be called
292 /// on a token of this identifier.
294 /// If this returns false, we know that HandleIdentifier will not affect
296 bool isHandleIdentifierCase() const { return NeedsHandleIdentifier; }
298 /// \brief Return true if the identifier in its current state was loaded
299 /// from an AST file.
300 bool isFromAST() const { return IsFromAST; }
302 void setIsFromAST() { IsFromAST = true; }
304 /// \brief Determine whether this identifier has changed since it was loaded
305 /// from an AST file.
306 bool hasChangedSinceDeserialization() const {
307 return ChangedAfterLoad;
310 /// \brief Note that this identifier has changed since it was loaded from
312 void setChangedSinceDeserialization() {
313 ChangedAfterLoad = true;
316 /// \brief Determine whether the frontend token information for this
317 /// identifier has changed since it was loaded from an AST file.
318 bool hasFETokenInfoChangedSinceDeserialization() const {
319 return FEChangedAfterLoad;
322 /// \brief Note that the frontend token information for this identifier has
323 /// changed since it was loaded from an AST file.
324 void setFETokenInfoChangedSinceDeserialization() {
325 FEChangedAfterLoad = true;
328 /// \brief Determine whether the information for this identifier is out of
329 /// date with respect to the external source.
330 bool isOutOfDate() const { return OutOfDate; }
332 /// \brief Set whether the information for this identifier is out of
333 /// date with respect to the external source.
334 void setOutOfDate(bool OOD) {
337 NeedsHandleIdentifier = true;
339 RecomputeNeedsHandleIdentifier();
342 /// \brief Determine whether this is the contextual keyword \c import.
343 bool isModulesImport() const { return IsModulesImport; }
345 /// \brief Set whether this identifier is the contextual keyword \c import.
346 void setModulesImport(bool I) {
349 NeedsHandleIdentifier = true;
351 RecomputeNeedsHandleIdentifier();
354 /// \brief Provide less than operator for lexicographical sorting.
355 bool operator<(const IdentifierInfo &RHS) const {
356 return getName() < RHS.getName();
360 /// The Preprocessor::HandleIdentifier does several special (but rare)
361 /// things to identifiers of various sorts. For example, it changes the
362 /// \c for keyword token from tok::identifier to tok::for.
364 /// This method is very tied to the definition of HandleIdentifier. Any
365 /// change to it should be reflected here.
366 void RecomputeNeedsHandleIdentifier() {
367 NeedsHandleIdentifier =
368 (isPoisoned() | hasMacroDefinition() | isCPlusPlusOperatorKeyword() |
369 isExtensionToken() | isFutureCompatKeyword() || isOutOfDate() ||
374 /// \brief An RAII object for [un]poisoning an identifier within a scope.
376 /// \p II is allowed to be null, in which case objects of this type have
378 class PoisonIdentifierRAIIObject {
379 IdentifierInfo *const II;
383 PoisonIdentifierRAIIObject(IdentifierInfo *II, bool NewValue)
384 : II(II), OldValue(II ? II->isPoisoned() : false) {
386 II->setIsPoisoned(NewValue);
389 ~PoisonIdentifierRAIIObject() {
391 II->setIsPoisoned(OldValue);
395 /// \brief An iterator that walks over all of the known identifiers
396 /// in the lookup table.
398 /// Since this iterator uses an abstract interface via virtual
399 /// functions, it uses an object-oriented interface rather than the
400 /// more standard C++ STL iterator interface. In this OO-style
401 /// iteration, the single function \c Next() provides dereference,
402 /// advance, and end-of-sequence checking in a single
403 /// operation. Subclasses of this iterator type will provide the
404 /// actual functionality.
405 class IdentifierIterator {
407 IdentifierIterator() = default;
410 IdentifierIterator(const IdentifierIterator &) = delete;
411 IdentifierIterator &operator=(const IdentifierIterator &) = delete;
413 virtual ~IdentifierIterator();
415 /// \brief Retrieve the next string in the identifier table and
416 /// advances the iterator for the following string.
418 /// \returns The next string in the identifier table. If there is
419 /// no such string, returns an empty \c StringRef.
420 virtual StringRef Next() = 0;
423 /// \brief Provides lookups to, and iteration over, IdentiferInfo objects.
424 class IdentifierInfoLookup {
426 virtual ~IdentifierInfoLookup();
428 /// \brief Return the IdentifierInfo for the specified named identifier.
430 /// Unlike the version in IdentifierTable, this returns a pointer instead
431 /// of a reference. If the pointer is null then the IdentifierInfo cannot
433 virtual IdentifierInfo* get(StringRef Name) = 0;
435 /// \brief Retrieve an iterator into the set of all identifiers
436 /// known to this identifier lookup source.
438 /// This routine provides access to all of the identifiers known to
439 /// the identifier lookup, allowing access to the contents of the
440 /// identifiers without introducing the overhead of constructing
441 /// IdentifierInfo objects for each.
443 /// \returns A new iterator into the set of known identifiers. The
444 /// caller is responsible for deleting this iterator.
445 virtual IdentifierIterator *getIdentifiers();
448 /// \brief Implements an efficient mapping from strings to IdentifierInfo nodes.
450 /// This has no other purpose, but this is an extremely performance-critical
451 /// piece of the code, as each occurrence of every identifier goes through
453 class IdentifierTable {
454 // Shark shows that using MallocAllocator is *much* slower than using this
456 typedef llvm::StringMap<IdentifierInfo*, llvm::BumpPtrAllocator> HashTableTy;
457 HashTableTy HashTable;
459 IdentifierInfoLookup* ExternalLookup;
462 /// \brief Create the identifier table, populating it with info about the
463 /// language keywords for the language specified by \p LangOpts.
464 IdentifierTable(const LangOptions &LangOpts,
465 IdentifierInfoLookup* externalLookup = nullptr);
467 /// \brief Set the external identifier lookup mechanism.
468 void setExternalIdentifierLookup(IdentifierInfoLookup *IILookup) {
469 ExternalLookup = IILookup;
472 /// \brief Retrieve the external identifier lookup object, if any.
473 IdentifierInfoLookup *getExternalIdentifierLookup() const {
474 return ExternalLookup;
477 llvm::BumpPtrAllocator& getAllocator() {
478 return HashTable.getAllocator();
481 /// \brief Return the identifier token info for the specified named
483 IdentifierInfo &get(StringRef Name) {
484 auto &Entry = *HashTable.insert(std::make_pair(Name, nullptr)).first;
486 IdentifierInfo *&II = Entry.second;
489 // No entry; if we have an external lookup, look there first.
490 if (ExternalLookup) {
491 II = ExternalLookup->get(Name);
496 // Lookups failed, make a new IdentifierInfo.
497 void *Mem = getAllocator().Allocate<IdentifierInfo>();
498 II = new (Mem) IdentifierInfo();
500 // Make sure getName() knows how to find the IdentifierInfo
507 IdentifierInfo &get(StringRef Name, tok::TokenKind TokenCode) {
508 IdentifierInfo &II = get(Name);
509 II.TokenID = TokenCode;
510 assert(II.TokenID == (unsigned) TokenCode && "TokenCode too large");
514 /// \brief Gets an IdentifierInfo for the given name without consulting
515 /// external sources.
517 /// This is a version of get() meant for external sources that want to
518 /// introduce or modify an identifier. If they called get(), they would
519 /// likely end up in a recursion.
520 IdentifierInfo &getOwn(StringRef Name) {
521 auto &Entry = *HashTable.insert(std::make_pair(Name, nullptr)).first;
523 IdentifierInfo *&II = Entry.second;
527 // Lookups failed, make a new IdentifierInfo.
528 void *Mem = getAllocator().Allocate<IdentifierInfo>();
529 II = new (Mem) IdentifierInfo();
531 // Make sure getName() knows how to find the IdentifierInfo
535 // If this is the 'import' contextual keyword, mark it as such.
536 if (Name.equals("import"))
537 II->setModulesImport(true);
542 typedef HashTableTy::const_iterator iterator;
543 typedef HashTableTy::const_iterator const_iterator;
545 iterator begin() const { return HashTable.begin(); }
546 iterator end() const { return HashTable.end(); }
547 unsigned size() const { return HashTable.size(); }
549 /// \brief Print some statistics to stderr that indicate how well the
550 /// hashing is doing.
551 void PrintStats() const;
553 void AddKeywords(const LangOptions &LangOpts);
556 /// \brief A family of Objective-C methods.
558 /// These families have no inherent meaning in the language, but are
559 /// nonetheless central enough in the existing implementations to
560 /// merit direct AST support. While, in theory, arbitrary methods can
561 /// be considered to form families, we focus here on the methods
562 /// involving allocation and retain-count management, as these are the
563 /// most "core" and the most likely to be useful to diverse clients
564 /// without extra information.
566 /// Both selectors and actual method declarations may be classified
567 /// into families. Method families may impose additional restrictions
568 /// beyond their selector name; for example, a method called '_init'
569 /// that returns void is not considered to be in the 'init' family
570 /// (but would be if it returned 'id'). It is also possible to
571 /// explicitly change or remove a method's family. Therefore the
572 /// method's family should be considered the single source of truth.
573 enum ObjCMethodFamily {
574 /// \brief No particular method family.
577 // Selectors in these families may have arbitrary arity, may be
578 // written with arbitrary leading underscores, and may have
579 // additional CamelCase "words" in their first selector chunk
580 // following the family name.
587 // These families are singletons consisting only of the nullary
588 // selector with the given name.
598 // performSelector families
602 /// Enough bits to store any enumerator in ObjCMethodFamily or
603 /// InvalidObjCMethodFamily.
604 enum { ObjCMethodFamilyBitWidth = 4 };
606 /// \brief An invalid value of ObjCMethodFamily.
607 enum { InvalidObjCMethodFamily = (1 << ObjCMethodFamilyBitWidth) - 1 };
609 /// \brief A family of Objective-C methods.
611 /// These are family of methods whose result type is initially 'id', but
612 /// but are candidate for the result type to be changed to 'instancetype'.
613 enum ObjCInstanceTypeFamily {
622 enum ObjCStringFormatFamily {
628 /// \brief Smart pointer class that efficiently represents Objective-C method
631 /// This class will either point to an IdentifierInfo or a
632 /// MultiKeywordSelector (which is private). This enables us to optimize
633 /// selectors that take no arguments and selectors that take 1 argument, which
634 /// accounts for 78% of all selectors in Cocoa.h.
636 friend class Diagnostic;
638 enum IdentifierInfoFlag {
639 // Empty selector = 0.
643 ArgFlags = ZeroArg|OneArg
645 uintptr_t InfoPtr; // a pointer to the MultiKeywordSelector or IdentifierInfo.
647 Selector(IdentifierInfo *II, unsigned nArgs) {
648 InfoPtr = reinterpret_cast<uintptr_t>(II);
649 assert((InfoPtr & ArgFlags) == 0 &&"Insufficiently aligned IdentifierInfo");
650 assert(nArgs < 2 && "nArgs not equal to 0/1");
653 Selector(MultiKeywordSelector *SI) {
654 InfoPtr = reinterpret_cast<uintptr_t>(SI);
655 assert((InfoPtr & ArgFlags) == 0 &&"Insufficiently aligned IdentifierInfo");
659 IdentifierInfo *getAsIdentifierInfo() const {
660 if (getIdentifierInfoFlag() < MultiArg)
661 return reinterpret_cast<IdentifierInfo *>(InfoPtr & ~ArgFlags);
665 MultiKeywordSelector *getMultiKeywordSelector() const {
666 return reinterpret_cast<MultiKeywordSelector *>(InfoPtr & ~ArgFlags);
669 unsigned getIdentifierInfoFlag() const {
670 return InfoPtr & ArgFlags;
673 static ObjCMethodFamily getMethodFamilyImpl(Selector sel);
675 static ObjCStringFormatFamily getStringFormatFamilyImpl(Selector sel);
678 friend class SelectorTable; // only the SelectorTable can create these
679 friend class DeclarationName; // and the AST's DeclarationName.
681 /// The default ctor should only be used when creating data structures that
682 /// will contain selectors.
683 Selector() : InfoPtr(0) {}
684 Selector(uintptr_t V) : InfoPtr(V) {}
686 /// operator==/!= - Indicate whether the specified selectors are identical.
687 bool operator==(Selector RHS) const {
688 return InfoPtr == RHS.InfoPtr;
690 bool operator!=(Selector RHS) const {
691 return InfoPtr != RHS.InfoPtr;
694 void *getAsOpaquePtr() const {
695 return reinterpret_cast<void*>(InfoPtr);
698 /// \brief Determine whether this is the empty selector.
699 bool isNull() const { return InfoPtr == 0; }
701 // Predicates to identify the selector type.
702 bool isKeywordSelector() const {
703 return getIdentifierInfoFlag() != ZeroArg;
706 bool isUnarySelector() const {
707 return getIdentifierInfoFlag() == ZeroArg;
710 unsigned getNumArgs() const;
712 /// \brief Retrieve the identifier at a given position in the selector.
714 /// Note that the identifier pointer returned may be NULL. Clients that only
715 /// care about the text of the identifier string, and not the specific,
716 /// uniqued identifier pointer, should use \c getNameForSlot(), which returns
717 /// an empty string when the identifier pointer would be NULL.
719 /// \param argIndex The index for which we want to retrieve the identifier.
720 /// This index shall be less than \c getNumArgs() unless this is a keyword
721 /// selector, in which case 0 is the only permissible value.
723 /// \returns the uniqued identifier for this slot, or NULL if this slot has
724 /// no corresponding identifier.
725 IdentifierInfo *getIdentifierInfoForSlot(unsigned argIndex) const;
727 /// \brief Retrieve the name at a given position in the selector.
729 /// \param argIndex The index for which we want to retrieve the name.
730 /// This index shall be less than \c getNumArgs() unless this is a keyword
731 /// selector, in which case 0 is the only permissible value.
733 /// \returns the name for this slot, which may be the empty string if no
734 /// name was supplied.
735 StringRef getNameForSlot(unsigned argIndex) const;
737 /// \brief Derive the full selector name (e.g. "foo:bar:") and return
738 /// it as an std::string.
739 std::string getAsString() const;
741 /// \brief Prints the full selector name (e.g. "foo:bar:").
742 void print(llvm::raw_ostream &OS) const;
744 /// \brief Derive the conventional family of this method.
745 ObjCMethodFamily getMethodFamily() const {
746 return getMethodFamilyImpl(*this);
749 ObjCStringFormatFamily getStringFormatFamily() const {
750 return getStringFormatFamilyImpl(*this);
753 static Selector getEmptyMarker() {
754 return Selector(uintptr_t(-1));
757 static Selector getTombstoneMarker() {
758 return Selector(uintptr_t(-2));
761 static ObjCInstanceTypeFamily getInstTypeMethodFamily(Selector sel);
764 /// \brief This table allows us to fully hide how we implement
765 /// multi-keyword caching.
766 class SelectorTable {
767 void *Impl; // Actually a SelectorTableImpl
771 SelectorTable(const SelectorTable &) = delete;
772 SelectorTable &operator=(const SelectorTable &) = delete;
775 /// \brief Can create any sort of selector.
777 /// \p NumArgs indicates whether this is a no argument selector "foo", a
778 /// single argument selector "foo:" or multi-argument "foo:bar:".
779 Selector getSelector(unsigned NumArgs, IdentifierInfo **IIV);
781 Selector getUnarySelector(IdentifierInfo *ID) {
782 return Selector(ID, 1);
784 Selector getNullarySelector(IdentifierInfo *ID) {
785 return Selector(ID, 0);
788 /// \brief Return the total amount of memory allocated for managing selectors.
789 size_t getTotalMemory() const;
791 /// \brief Return the default setter name for the given identifier.
793 /// This is "set" + \p Name where the initial character of \p Name
794 /// has been capitalized.
795 static SmallString<64> constructSetterName(StringRef Name);
797 /// \brief Return the default setter selector for the given identifier.
799 /// This is "set" + \p Name where the initial character of \p Name
800 /// has been capitalized.
801 static Selector constructSetterSelector(IdentifierTable &Idents,
802 SelectorTable &SelTable,
803 const IdentifierInfo *Name);
806 /// DeclarationNameExtra - Common base of the MultiKeywordSelector,
807 /// CXXSpecialName, and CXXOperatorIdName classes, all of which are
808 /// private classes that describe different kinds of names.
809 class DeclarationNameExtra {
811 /// ExtraKind - The kind of "extra" information stored in the
812 /// DeclarationName. See @c ExtraKindOrNumArgs for an explanation of
813 /// how these enumerator values are used.
817 CXXConversionFunction,
818 #define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
820 #include "clang/Basic/OperatorKinds.def"
826 /// ExtraKindOrNumArgs - Either the kind of C++ special name or
827 /// operator-id (if the value is one of the CXX* enumerators of
828 /// ExtraKind), in which case the DeclarationNameExtra is also a
829 /// CXXSpecialName, (for CXXConstructor, CXXDestructor, or
830 /// CXXConversionFunction) CXXOperatorIdName, or CXXLiteralOperatorName,
831 /// it may be also name common to C++ using-directives (CXXUsingDirective),
832 /// otherwise it is NUM_EXTRA_KINDS+NumArgs, where NumArgs is the number of
833 /// arguments in the Objective-C selector, in which case the
834 /// DeclarationNameExtra is also a MultiKeywordSelector.
835 unsigned ExtraKindOrNumArgs;
838 } // end namespace clang
842 /// Define DenseMapInfo so that Selectors can be used as keys in DenseMap and
845 struct DenseMapInfo<clang::Selector> {
846 static inline clang::Selector getEmptyKey() {
847 return clang::Selector::getEmptyMarker();
850 static inline clang::Selector getTombstoneKey() {
851 return clang::Selector::getTombstoneMarker();
854 static unsigned getHashValue(clang::Selector S);
856 static bool isEqual(clang::Selector LHS, clang::Selector RHS) {
862 struct isPodLike<clang::Selector> { static const bool value = true; };
864 template <typename T> class PointerLikeTypeTraits;
867 class PointerLikeTypeTraits<clang::Selector> {
869 static inline const void *getAsVoidPointer(clang::Selector P) {
870 return P.getAsOpaquePtr();
873 static inline clang::Selector getFromVoidPointer(const void *P) {
874 return clang::Selector(reinterpret_cast<uintptr_t>(P));
877 enum { NumLowBitsAvailable = 0 };
880 // Provide PointerLikeTypeTraits for IdentifierInfo pointers, which
881 // are not guaranteed to be 8-byte aligned.
883 class PointerLikeTypeTraits<clang::IdentifierInfo*> {
885 static inline void *getAsVoidPointer(clang::IdentifierInfo* P) {
889 static inline clang::IdentifierInfo *getFromVoidPointer(void *P) {
890 return static_cast<clang::IdentifierInfo*>(P);
893 enum { NumLowBitsAvailable = 1 };
897 class PointerLikeTypeTraits<const clang::IdentifierInfo*> {
899 static inline const void *getAsVoidPointer(const clang::IdentifierInfo* P) {
903 static inline const clang::IdentifierInfo *getFromVoidPointer(const void *P) {
904 return static_cast<const clang::IdentifierInfo*>(P);
907 enum { NumLowBitsAvailable = 1 };
910 } // end namespace llvm
912 #endif // LLVM_CLANG_BASIC_IDENTIFIERTABLE_H