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 /// 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/DenseMapInfo.h"
22 #include "llvm/ADT/SmallString.h"
23 #include "llvm/ADT/StringMap.h"
24 #include "llvm/ADT/StringRef.h"
25 #include "llvm/Support/Allocator.h"
26 #include "llvm/Support/PointerLikeTypeTraits.h"
27 #include "llvm/Support/type_traits.h"
39 class MultiKeywordSelector;
42 /// A simple pair of identifier info and location.
43 using IdentifierLocPair = std::pair<IdentifierInfo *, SourceLocation>;
45 /// One of these records is kept for each identifier that
46 /// is lexed. This contains information about whether the token was \#define'd,
47 /// is a language keyword, or if it is a front-end token of some sort (e.g. a
48 /// variable or function name). The preprocessor keeps this information in a
49 /// set, and all tok::identifier tokens have a pointer to one of these.
50 class IdentifierInfo {
51 friend class IdentifierTable;
53 unsigned TokenID : 9; // Front-end token ID or tok::identifier.
54 // Objective-C keyword ('protocol' in '@protocol') or builtin (__builtin_inf).
55 // First NUM_OBJC_KEYWORDS values are for Objective-C, the remaining values
57 unsigned ObjCOrBuiltinID :13;
58 bool HasMacro : 1; // True if there is a #define for this.
59 bool HadMacro : 1; // True if there was a #define for this.
60 bool IsExtension : 1; // True if identifier is a lang extension.
61 bool IsFutureCompatKeyword : 1; // True if identifier is a keyword in a
62 // newer Standard or proposed Standard.
63 bool IsPoisoned : 1; // True if identifier is poisoned.
64 bool IsCPPOperatorKeyword : 1; // True if ident is a C++ operator keyword.
65 bool NeedsHandleIdentifier : 1; // See "RecomputeNeedsHandleIdentifier".
66 bool IsFromAST : 1; // True if identifier was loaded (at least
67 // partially) from an AST file.
68 bool ChangedAfterLoad : 1; // True if identifier has changed from the
69 // definition loaded from an AST file.
70 bool FEChangedAfterLoad : 1; // True if identifier's frontend information
71 // has changed from the definition loaded
73 bool RevertedTokenID : 1; // True if revertTokenIDToIdentifier was
75 bool OutOfDate : 1; // True if there may be additional
76 // information about this identifier
78 bool IsModulesImport : 1; // True if this is the 'import' contextual
80 // 29 bit left in 64-bit word.
82 // Managed by the language front-end.
83 void *FETokenInfo = nullptr;
85 llvm::StringMapEntry<IdentifierInfo *> *Entry = nullptr;
89 IdentifierInfo(const IdentifierInfo &) = delete;
90 IdentifierInfo &operator=(const IdentifierInfo &) = delete;
92 /// Return true if this is the identifier for the specified string.
94 /// This is intended to be used for string literals only: II->isStr("foo").
95 template <std::size_t StrLen>
96 bool isStr(const char (&Str)[StrLen]) const {
97 return getLength() == StrLen-1 &&
98 memcmp(getNameStart(), Str, StrLen-1) == 0;
101 /// Return true if this is the identifier for the specified StringRef.
102 bool isStr(llvm::StringRef Str) const {
103 llvm::StringRef ThisStr(getNameStart(), getLength());
104 return ThisStr == Str;
107 /// Return the beginning of the actual null-terminated string for this
109 const char *getNameStart() const {
110 if (Entry) return Entry->getKeyData();
111 // FIXME: This is gross. It would be best not to embed specific details
112 // of the PTH file format here.
113 // The 'this' pointer really points to a
114 // std::pair<IdentifierInfo, const char*>, where internal pointer
115 // points to the external string data.
116 using actualtype = std::pair<IdentifierInfo, const char *>;
118 return ((const actualtype*) this)->second;
121 /// Efficiently return the length of this identifier info.
122 unsigned getLength() const {
123 if (Entry) return Entry->getKeyLength();
124 // FIXME: This is gross. It would be best not to embed specific details
125 // of the PTH file format here.
126 // The 'this' pointer really points to a
127 // std::pair<IdentifierInfo, const char*>, where internal pointer
128 // points to the external string data.
129 using actualtype = std::pair<IdentifierInfo, const char *>;
131 const char* p = ((const actualtype*) this)->second - 2;
132 return (((unsigned) p[0]) | (((unsigned) p[1]) << 8)) - 1;
135 /// Return the actual identifier string.
136 StringRef getName() const {
137 return StringRef(getNameStart(), getLength());
140 /// Return true if this identifier is \#defined to some other value.
141 /// \note The current definition may be in a module and not currently visible.
142 bool hasMacroDefinition() const {
145 void setHasMacroDefinition(bool Val) {
146 if (HasMacro == Val) return;
150 NeedsHandleIdentifier = true;
153 RecomputeNeedsHandleIdentifier();
156 /// Returns true if this identifier was \#defined to some value at any
157 /// moment. In this case there should be an entry for the identifier in the
158 /// macro history table in Preprocessor.
159 bool hadMacroDefinition() const {
163 /// If this is a source-language token (e.g. 'for'), this API
164 /// can be used to cause the lexer to map identifiers to source-language
166 tok::TokenKind getTokenID() const { return (tok::TokenKind)TokenID; }
168 /// True if revertTokenIDToIdentifier() was called.
169 bool hasRevertedTokenIDToIdentifier() const { return RevertedTokenID; }
171 /// Revert TokenID to tok::identifier; used for GNU libstdc++ 4.2
174 /// TokenID is normally read-only but there are 2 instances where we revert it
175 /// to tok::identifier for libstdc++ 4.2. Keep track of when this happens
176 /// using this method so we can inform serialization about it.
177 void revertTokenIDToIdentifier() {
178 assert(TokenID != tok::identifier && "Already at tok::identifier");
179 TokenID = tok::identifier;
180 RevertedTokenID = true;
182 void revertIdentifierToTokenID(tok::TokenKind TK) {
183 assert(TokenID == tok::identifier && "Should be at tok::identifier");
185 RevertedTokenID = false;
188 /// Return the preprocessor keyword ID for this identifier.
190 /// For example, "define" will return tok::pp_define.
191 tok::PPKeywordKind getPPKeywordID() const;
193 /// Return the Objective-C keyword ID for the this identifier.
195 /// For example, 'class' will return tok::objc_class if ObjC is enabled.
196 tok::ObjCKeywordKind getObjCKeywordID() const {
197 if (ObjCOrBuiltinID < tok::NUM_OBJC_KEYWORDS)
198 return tok::ObjCKeywordKind(ObjCOrBuiltinID);
200 return tok::objc_not_keyword;
202 void setObjCKeywordID(tok::ObjCKeywordKind ID) { ObjCOrBuiltinID = ID; }
204 /// True if setNotBuiltin() was called.
205 bool hasRevertedBuiltin() const {
206 return ObjCOrBuiltinID == tok::NUM_OBJC_KEYWORDS;
209 /// Revert the identifier to a non-builtin identifier. We do this if
210 /// the name of a known builtin library function is used to declare that
211 /// function, but an unexpected type is specified.
212 void revertBuiltin() {
216 /// Return a value indicating whether this is a builtin function.
218 /// 0 is not-built-in. 1+ are specific builtin functions.
219 unsigned getBuiltinID() const {
220 if (ObjCOrBuiltinID >= tok::NUM_OBJC_KEYWORDS)
221 return ObjCOrBuiltinID - tok::NUM_OBJC_KEYWORDS;
225 void setBuiltinID(unsigned ID) {
226 ObjCOrBuiltinID = ID + tok::NUM_OBJC_KEYWORDS;
227 assert(ObjCOrBuiltinID - unsigned(tok::NUM_OBJC_KEYWORDS) == ID
228 && "ID too large for field!");
231 unsigned getObjCOrBuiltinID() const { return ObjCOrBuiltinID; }
232 void setObjCOrBuiltinID(unsigned ID) { ObjCOrBuiltinID = ID; }
234 /// get/setExtension - Initialize information about whether or not this
235 /// language token is an extension. This controls extension warnings, and is
236 /// only valid if a custom token ID is set.
237 bool isExtensionToken() const { return IsExtension; }
238 void setIsExtensionToken(bool Val) {
241 NeedsHandleIdentifier = true;
243 RecomputeNeedsHandleIdentifier();
246 /// is/setIsFutureCompatKeyword - Initialize information about whether or not
247 /// this language token is a keyword in a newer or proposed Standard. This
248 /// controls compatibility warnings, and is only true when not parsing the
249 /// corresponding Standard. Once a compatibility problem has been diagnosed
250 /// with this keyword, the flag will be cleared.
251 bool isFutureCompatKeyword() const { return IsFutureCompatKeyword; }
252 void setIsFutureCompatKeyword(bool Val) {
253 IsFutureCompatKeyword = Val;
255 NeedsHandleIdentifier = true;
257 RecomputeNeedsHandleIdentifier();
260 /// setIsPoisoned - Mark this identifier as poisoned. After poisoning, the
261 /// Preprocessor will emit an error every time this token is used.
262 void setIsPoisoned(bool Value = true) {
265 NeedsHandleIdentifier = true;
267 RecomputeNeedsHandleIdentifier();
270 /// Return true if this token has been poisoned.
271 bool isPoisoned() const { return IsPoisoned; }
273 /// isCPlusPlusOperatorKeyword/setIsCPlusPlusOperatorKeyword controls whether
274 /// this identifier is a C++ alternate representation of an operator.
275 void setIsCPlusPlusOperatorKeyword(bool Val = true) {
276 IsCPPOperatorKeyword = Val;
278 bool isCPlusPlusOperatorKeyword() const { return IsCPPOperatorKeyword; }
280 /// Return true if this token is a keyword in the specified language.
281 bool isKeyword(const LangOptions &LangOpts) const;
283 /// Return true if this token is a C++ keyword in the specified
285 bool isCPlusPlusKeyword(const LangOptions &LangOpts) const;
287 /// getFETokenInfo/setFETokenInfo - The language front-end is allowed to
288 /// associate arbitrary metadata with this token.
290 T *getFETokenInfo() const { return static_cast<T*>(FETokenInfo); }
291 void setFETokenInfo(void *T) { FETokenInfo = T; }
293 /// Return true if the Preprocessor::HandleIdentifier must be called
294 /// on a token of this identifier.
296 /// If this returns false, we know that HandleIdentifier will not affect
298 bool isHandleIdentifierCase() const { return NeedsHandleIdentifier; }
300 /// Return true if the identifier in its current state was loaded
301 /// from an AST file.
302 bool isFromAST() const { return IsFromAST; }
304 void setIsFromAST() { IsFromAST = true; }
306 /// Determine whether this identifier has changed since it was loaded
307 /// from an AST file.
308 bool hasChangedSinceDeserialization() const {
309 return ChangedAfterLoad;
312 /// Note that this identifier has changed since it was loaded from
314 void setChangedSinceDeserialization() {
315 ChangedAfterLoad = true;
318 /// Determine whether the frontend token information for this
319 /// identifier has changed since it was loaded from an AST file.
320 bool hasFETokenInfoChangedSinceDeserialization() const {
321 return FEChangedAfterLoad;
324 /// Note that the frontend token information for this identifier has
325 /// changed since it was loaded from an AST file.
326 void setFETokenInfoChangedSinceDeserialization() {
327 FEChangedAfterLoad = true;
330 /// Determine whether the information for this identifier is out of
331 /// date with respect to the external source.
332 bool isOutOfDate() const { return OutOfDate; }
334 /// Set whether the information for this identifier is out of
335 /// date with respect to the external source.
336 void setOutOfDate(bool OOD) {
339 NeedsHandleIdentifier = true;
341 RecomputeNeedsHandleIdentifier();
344 /// Determine whether this is the contextual keyword \c import.
345 bool isModulesImport() const { return IsModulesImport; }
347 /// Set whether this identifier is the contextual keyword \c import.
348 void setModulesImport(bool I) {
351 NeedsHandleIdentifier = true;
353 RecomputeNeedsHandleIdentifier();
356 /// Return true if this identifier is an editor placeholder.
358 /// Editor placeholders are produced by the code-completion engine and are
359 /// represented as characters between '<#' and '#>' in the source code. An
360 /// example of auto-completed call with a placeholder parameter is shown
363 /// function(<#int x#>);
365 bool isEditorPlaceholder() const {
366 return getName().startswith("<#") && getName().endswith("#>");
369 /// Provide less than operator for lexicographical sorting.
370 bool operator<(const IdentifierInfo &RHS) const {
371 return getName() < RHS.getName();
375 /// The Preprocessor::HandleIdentifier does several special (but rare)
376 /// things to identifiers of various sorts. For example, it changes the
377 /// \c for keyword token from tok::identifier to tok::for.
379 /// This method is very tied to the definition of HandleIdentifier. Any
380 /// change to it should be reflected here.
381 void RecomputeNeedsHandleIdentifier() {
382 NeedsHandleIdentifier = isPoisoned() || hasMacroDefinition() ||
383 isExtensionToken() || isFutureCompatKeyword() ||
384 isOutOfDate() || isModulesImport();
388 /// An RAII object for [un]poisoning an identifier within a scope.
390 /// \p II is allowed to be null, in which case objects of this type have
392 class PoisonIdentifierRAIIObject {
393 IdentifierInfo *const II;
397 PoisonIdentifierRAIIObject(IdentifierInfo *II, bool NewValue)
398 : II(II), OldValue(II ? II->isPoisoned() : false) {
400 II->setIsPoisoned(NewValue);
403 ~PoisonIdentifierRAIIObject() {
405 II->setIsPoisoned(OldValue);
409 /// An iterator that walks over all of the known identifiers
410 /// in the lookup table.
412 /// Since this iterator uses an abstract interface via virtual
413 /// functions, it uses an object-oriented interface rather than the
414 /// more standard C++ STL iterator interface. In this OO-style
415 /// iteration, the single function \c Next() provides dereference,
416 /// advance, and end-of-sequence checking in a single
417 /// operation. Subclasses of this iterator type will provide the
418 /// actual functionality.
419 class IdentifierIterator {
421 IdentifierIterator() = default;
424 IdentifierIterator(const IdentifierIterator &) = delete;
425 IdentifierIterator &operator=(const IdentifierIterator &) = delete;
427 virtual ~IdentifierIterator();
429 /// Retrieve the next string in the identifier table and
430 /// advances the iterator for the following string.
432 /// \returns The next string in the identifier table. If there is
433 /// no such string, returns an empty \c StringRef.
434 virtual StringRef Next() = 0;
437 /// Provides lookups to, and iteration over, IdentiferInfo objects.
438 class IdentifierInfoLookup {
440 virtual ~IdentifierInfoLookup();
442 /// Return the IdentifierInfo for the specified named identifier.
444 /// Unlike the version in IdentifierTable, this returns a pointer instead
445 /// of a reference. If the pointer is null then the IdentifierInfo cannot
447 virtual IdentifierInfo* get(StringRef Name) = 0;
449 /// Retrieve an iterator into the set of all identifiers
450 /// known to this identifier lookup source.
452 /// This routine provides access to all of the identifiers known to
453 /// the identifier lookup, allowing access to the contents of the
454 /// identifiers without introducing the overhead of constructing
455 /// IdentifierInfo objects for each.
457 /// \returns A new iterator into the set of known identifiers. The
458 /// caller is responsible for deleting this iterator.
459 virtual IdentifierIterator *getIdentifiers();
462 /// Implements an efficient mapping from strings to IdentifierInfo nodes.
464 /// This has no other purpose, but this is an extremely performance-critical
465 /// piece of the code, as each occurrence of every identifier goes through
467 class IdentifierTable {
468 // Shark shows that using MallocAllocator is *much* slower than using this
470 using HashTableTy = llvm::StringMap<IdentifierInfo *, llvm::BumpPtrAllocator>;
471 HashTableTy HashTable;
473 IdentifierInfoLookup* ExternalLookup;
476 /// Create the identifier table.
477 explicit IdentifierTable(IdentifierInfoLookup *ExternalLookup = nullptr);
479 /// Create the identifier table, populating it with info about the
480 /// language keywords for the language specified by \p LangOpts.
481 explicit IdentifierTable(const LangOptions &LangOpts,
482 IdentifierInfoLookup *ExternalLookup = nullptr);
484 /// Set the external identifier lookup mechanism.
485 void setExternalIdentifierLookup(IdentifierInfoLookup *IILookup) {
486 ExternalLookup = IILookup;
489 /// Retrieve the external identifier lookup object, if any.
490 IdentifierInfoLookup *getExternalIdentifierLookup() const {
491 return ExternalLookup;
494 llvm::BumpPtrAllocator& getAllocator() {
495 return HashTable.getAllocator();
498 /// Return the identifier token info for the specified named
500 IdentifierInfo &get(StringRef Name) {
501 auto &Entry = *HashTable.insert(std::make_pair(Name, nullptr)).first;
503 IdentifierInfo *&II = Entry.second;
506 // No entry; if we have an external lookup, look there first.
507 if (ExternalLookup) {
508 II = ExternalLookup->get(Name);
513 // Lookups failed, make a new IdentifierInfo.
514 void *Mem = getAllocator().Allocate<IdentifierInfo>();
515 II = new (Mem) IdentifierInfo();
517 // Make sure getName() knows how to find the IdentifierInfo
524 IdentifierInfo &get(StringRef Name, tok::TokenKind TokenCode) {
525 IdentifierInfo &II = get(Name);
526 II.TokenID = TokenCode;
527 assert(II.TokenID == (unsigned) TokenCode && "TokenCode too large");
531 /// Gets an IdentifierInfo for the given name without consulting
532 /// external sources.
534 /// This is a version of get() meant for external sources that want to
535 /// introduce or modify an identifier. If they called get(), they would
536 /// likely end up in a recursion.
537 IdentifierInfo &getOwn(StringRef Name) {
538 auto &Entry = *HashTable.insert(std::make_pair(Name, nullptr)).first;
540 IdentifierInfo *&II = Entry.second;
544 // Lookups failed, make a new IdentifierInfo.
545 void *Mem = getAllocator().Allocate<IdentifierInfo>();
546 II = new (Mem) IdentifierInfo();
548 // Make sure getName() knows how to find the IdentifierInfo
552 // If this is the 'import' contextual keyword, mark it as such.
553 if (Name.equals("import"))
554 II->setModulesImport(true);
559 using iterator = HashTableTy::const_iterator;
560 using const_iterator = HashTableTy::const_iterator;
562 iterator begin() const { return HashTable.begin(); }
563 iterator end() const { return HashTable.end(); }
564 unsigned size() const { return HashTable.size(); }
566 /// Print some statistics to stderr that indicate how well the
567 /// hashing is doing.
568 void PrintStats() const;
570 /// Populate the identifier table with info about the language keywords
571 /// for the language specified by \p LangOpts.
572 void AddKeywords(const LangOptions &LangOpts);
575 /// A family of Objective-C methods.
577 /// These families have no inherent meaning in the language, but are
578 /// nonetheless central enough in the existing implementations to
579 /// merit direct AST support. While, in theory, arbitrary methods can
580 /// be considered to form families, we focus here on the methods
581 /// involving allocation and retain-count management, as these are the
582 /// most "core" and the most likely to be useful to diverse clients
583 /// without extra information.
585 /// Both selectors and actual method declarations may be classified
586 /// into families. Method families may impose additional restrictions
587 /// beyond their selector name; for example, a method called '_init'
588 /// that returns void is not considered to be in the 'init' family
589 /// (but would be if it returned 'id'). It is also possible to
590 /// explicitly change or remove a method's family. Therefore the
591 /// method's family should be considered the single source of truth.
592 enum ObjCMethodFamily {
593 /// No particular method family.
596 // Selectors in these families may have arbitrary arity, may be
597 // written with arbitrary leading underscores, and may have
598 // additional CamelCase "words" in their first selector chunk
599 // following the family name.
606 // These families are singletons consisting only of the nullary
607 // selector with the given name.
617 // performSelector families
621 /// Enough bits to store any enumerator in ObjCMethodFamily or
622 /// InvalidObjCMethodFamily.
623 enum { ObjCMethodFamilyBitWidth = 4 };
625 /// An invalid value of ObjCMethodFamily.
626 enum { InvalidObjCMethodFamily = (1 << ObjCMethodFamilyBitWidth) - 1 };
628 /// A family of Objective-C methods.
630 /// These are family of methods whose result type is initially 'id', but
631 /// but are candidate for the result type to be changed to 'instancetype'.
632 enum ObjCInstanceTypeFamily {
641 enum ObjCStringFormatFamily {
647 /// Smart pointer class that efficiently represents Objective-C method
650 /// This class will either point to an IdentifierInfo or a
651 /// MultiKeywordSelector (which is private). This enables us to optimize
652 /// selectors that take no arguments and selectors that take 1 argument, which
653 /// accounts for 78% of all selectors in Cocoa.h.
655 friend class Diagnostic;
657 enum IdentifierInfoFlag {
658 // Empty selector = 0.
662 ArgFlags = ZeroArg|OneArg
665 // a pointer to the MultiKeywordSelector or IdentifierInfo.
666 uintptr_t InfoPtr = 0;
668 Selector(IdentifierInfo *II, unsigned nArgs) {
669 InfoPtr = reinterpret_cast<uintptr_t>(II);
670 assert((InfoPtr & ArgFlags) == 0 &&"Insufficiently aligned IdentifierInfo");
671 assert(nArgs < 2 && "nArgs not equal to 0/1");
675 Selector(MultiKeywordSelector *SI) {
676 InfoPtr = reinterpret_cast<uintptr_t>(SI);
677 assert((InfoPtr & ArgFlags) == 0 &&"Insufficiently aligned IdentifierInfo");
681 IdentifierInfo *getAsIdentifierInfo() const {
682 if (getIdentifierInfoFlag() < MultiArg)
683 return reinterpret_cast<IdentifierInfo *>(InfoPtr & ~ArgFlags);
687 MultiKeywordSelector *getMultiKeywordSelector() const {
688 return reinterpret_cast<MultiKeywordSelector *>(InfoPtr & ~ArgFlags);
691 unsigned getIdentifierInfoFlag() const {
692 return InfoPtr & ArgFlags;
695 static ObjCMethodFamily getMethodFamilyImpl(Selector sel);
697 static ObjCStringFormatFamily getStringFormatFamilyImpl(Selector sel);
700 friend class SelectorTable; // only the SelectorTable can create these
701 friend class DeclarationName; // and the AST's DeclarationName.
703 /// The default ctor should only be used when creating data structures that
704 /// will contain selectors.
705 Selector() = default;
706 Selector(uintptr_t V) : InfoPtr(V) {}
708 /// operator==/!= - Indicate whether the specified selectors are identical.
709 bool operator==(Selector RHS) const {
710 return InfoPtr == RHS.InfoPtr;
712 bool operator!=(Selector RHS) const {
713 return InfoPtr != RHS.InfoPtr;
716 void *getAsOpaquePtr() const {
717 return reinterpret_cast<void*>(InfoPtr);
720 /// Determine whether this is the empty selector.
721 bool isNull() const { return InfoPtr == 0; }
723 // Predicates to identify the selector type.
724 bool isKeywordSelector() const {
725 return getIdentifierInfoFlag() != ZeroArg;
728 bool isUnarySelector() const {
729 return getIdentifierInfoFlag() == ZeroArg;
732 unsigned getNumArgs() const;
734 /// Retrieve the identifier at a given position in the selector.
736 /// Note that the identifier pointer returned may be NULL. Clients that only
737 /// care about the text of the identifier string, and not the specific,
738 /// uniqued identifier pointer, should use \c getNameForSlot(), which returns
739 /// an empty string when the identifier pointer would be NULL.
741 /// \param argIndex The index for which we want to retrieve the identifier.
742 /// This index shall be less than \c getNumArgs() unless this is a keyword
743 /// selector, in which case 0 is the only permissible value.
745 /// \returns the uniqued identifier for this slot, or NULL if this slot has
746 /// no corresponding identifier.
747 IdentifierInfo *getIdentifierInfoForSlot(unsigned argIndex) const;
749 /// Retrieve the name at a given position in the selector.
751 /// \param argIndex The index for which we want to retrieve the name.
752 /// This index shall be less than \c getNumArgs() unless this is a keyword
753 /// selector, in which case 0 is the only permissible value.
755 /// \returns the name for this slot, which may be the empty string if no
756 /// name was supplied.
757 StringRef getNameForSlot(unsigned argIndex) const;
759 /// Derive the full selector name (e.g. "foo:bar:") and return
760 /// it as an std::string.
761 std::string getAsString() const;
763 /// Prints the full selector name (e.g. "foo:bar:").
764 void print(llvm::raw_ostream &OS) const;
768 /// Derive the conventional family of this method.
769 ObjCMethodFamily getMethodFamily() const {
770 return getMethodFamilyImpl(*this);
773 ObjCStringFormatFamily getStringFormatFamily() const {
774 return getStringFormatFamilyImpl(*this);
777 static Selector getEmptyMarker() {
778 return Selector(uintptr_t(-1));
781 static Selector getTombstoneMarker() {
782 return Selector(uintptr_t(-2));
785 static ObjCInstanceTypeFamily getInstTypeMethodFamily(Selector sel);
788 /// This table allows us to fully hide how we implement
789 /// multi-keyword caching.
790 class SelectorTable {
791 // Actually a SelectorTableImpl
796 SelectorTable(const SelectorTable &) = delete;
797 SelectorTable &operator=(const SelectorTable &) = delete;
800 /// Can create any sort of selector.
802 /// \p NumArgs indicates whether this is a no argument selector "foo", a
803 /// single argument selector "foo:" or multi-argument "foo:bar:".
804 Selector getSelector(unsigned NumArgs, IdentifierInfo **IIV);
806 Selector getUnarySelector(IdentifierInfo *ID) {
807 return Selector(ID, 1);
810 Selector getNullarySelector(IdentifierInfo *ID) {
811 return Selector(ID, 0);
814 /// Return the total amount of memory allocated for managing selectors.
815 size_t getTotalMemory() const;
817 /// Return the default setter name for the given identifier.
819 /// This is "set" + \p Name where the initial character of \p Name
820 /// has been capitalized.
821 static SmallString<64> constructSetterName(StringRef Name);
823 /// Return the default setter selector for the given identifier.
825 /// This is "set" + \p Name where the initial character of \p Name
826 /// has been capitalized.
827 static Selector constructSetterSelector(IdentifierTable &Idents,
828 SelectorTable &SelTable,
829 const IdentifierInfo *Name);
831 /// Return the property name for the given setter selector.
832 static std::string getPropertyNameFromSetterSelector(Selector Sel);
835 /// DeclarationNameExtra - Common base of the MultiKeywordSelector,
836 /// CXXSpecialName, and CXXOperatorIdName classes, all of which are
837 /// private classes that describe different kinds of names.
838 class DeclarationNameExtra {
840 /// ExtraKind - The kind of "extra" information stored in the
841 /// DeclarationName. See @c ExtraKindOrNumArgs for an explanation of
842 /// how these enumerator values are used.
846 CXXConversionFunction,
847 #define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
849 #include "clang/Basic/OperatorKinds.def"
856 /// ExtraKindOrNumArgs - Either the kind of C++ special name or
857 /// operator-id (if the value is one of the CXX* enumerators of
858 /// ExtraKind), in which case the DeclarationNameExtra is also a
859 /// CXXSpecialName, (for CXXConstructor, CXXDestructor, or
860 /// CXXConversionFunction) CXXOperatorIdName, or CXXLiteralOperatorName,
861 /// it may be also name common to C++ using-directives (CXXUsingDirective),
862 /// otherwise it is NUM_EXTRA_KINDS+NumArgs, where NumArgs is the number of
863 /// arguments in the Objective-C selector, in which case the
864 /// DeclarationNameExtra is also a MultiKeywordSelector.
865 unsigned ExtraKindOrNumArgs;
872 /// Define DenseMapInfo so that Selectors can be used as keys in DenseMap and
875 struct DenseMapInfo<clang::Selector> {
876 static clang::Selector getEmptyKey() {
877 return clang::Selector::getEmptyMarker();
880 static clang::Selector getTombstoneKey() {
881 return clang::Selector::getTombstoneMarker();
884 static unsigned getHashValue(clang::Selector S);
886 static bool isEqual(clang::Selector LHS, clang::Selector RHS) {
892 struct isPodLike<clang::Selector> { static const bool value = true; };
895 struct PointerLikeTypeTraits<clang::Selector> {
896 static const void *getAsVoidPointer(clang::Selector P) {
897 return P.getAsOpaquePtr();
900 static clang::Selector getFromVoidPointer(const void *P) {
901 return clang::Selector(reinterpret_cast<uintptr_t>(P));
904 enum { NumLowBitsAvailable = 0 };
907 // Provide PointerLikeTypeTraits for IdentifierInfo pointers, which
908 // are not guaranteed to be 8-byte aligned.
910 struct PointerLikeTypeTraits<clang::IdentifierInfo*> {
911 static void *getAsVoidPointer(clang::IdentifierInfo* P) {
915 static clang::IdentifierInfo *getFromVoidPointer(void *P) {
916 return static_cast<clang::IdentifierInfo*>(P);
919 enum { NumLowBitsAvailable = 1 };
923 struct PointerLikeTypeTraits<const clang::IdentifierInfo*> {
924 static const void *getAsVoidPointer(const clang::IdentifierInfo* P) {
928 static const clang::IdentifierInfo *getFromVoidPointer(const void *P) {
929 return static_cast<const clang::IdentifierInfo*>(P);
932 enum { NumLowBitsAvailable = 1 };
937 #endif // LLVM_CLANG_BASIC_IDENTIFIERTABLE_H