clang  8.0.0svn
IdentifierTable.h
Go to the documentation of this file.
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 /// 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/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"
28 #include <cassert>
29 #include <cstddef>
30 #include <cstdint>
31 #include <cstring>
32 #include <string>
33 #include <utility>
34 
35 namespace clang {
36 
37 class IdentifierInfo;
38 class LangOptions;
39 class MultiKeywordSelector;
40 class SourceLocation;
41 
42 /// A simple pair of identifier info and location.
43 using IdentifierLocPair = std::pair<IdentifierInfo *, SourceLocation>;
44 
45 /// One of these records is kept for each identifier that
46 /// is lexed. This contains information about whether the token was \#define'd,
47 /// is a language keyword, or if it is a front-end token of some sort (e.g. a
48 /// variable or function name). The preprocessor keeps this information in a
49 /// set, and all tok::identifier tokens have a pointer to one of these.
51  friend class IdentifierTable;
52 
53  // Front-end token ID or tok::identifier.
54  unsigned TokenID : 9;
55 
56  // ObjC keyword ('protocol' in '@protocol') or builtin (__builtin_inf).
57  // First NUM_OBJC_KEYWORDS values are for Objective-C,
58  // the remaining values are for builtins.
59  unsigned ObjCOrBuiltinID : 13;
60 
61  // True if there is a #define for this.
62  unsigned HasMacro : 1;
63 
64  // True if there was a #define for this.
65  unsigned HadMacro : 1;
66 
67  // True if the identifier is a language extension.
68  unsigned IsExtension : 1;
69 
70  // True if the identifier is a keyword in a newer or proposed Standard.
71  unsigned IsFutureCompatKeyword : 1;
72 
73  // True if the identifier is poisoned.
74  unsigned IsPoisoned : 1;
75 
76  // True if the identifier is a C++ operator keyword.
77  unsigned IsCPPOperatorKeyword : 1;
78 
79  // Internal bit set by the member function RecomputeNeedsHandleIdentifier.
80  // See comment about RecomputeNeedsHandleIdentifier for more info.
81  unsigned NeedsHandleIdentifier : 1;
82 
83  // True if the identifier was loaded (at least partially) from an AST file.
84  unsigned IsFromAST : 1;
85 
86  // True if the identifier has changed from the definition
87  // loaded from an AST file.
88  unsigned ChangedAfterLoad : 1;
89 
90  // True if the identifier's frontend information has changed from the
91  // definition loaded from an AST file.
92  unsigned FEChangedAfterLoad : 1;
93 
94  // True if revertTokenIDToIdentifier was called.
95  unsigned RevertedTokenID : 1;
96 
97  // True if there may be additional information about
98  // this identifier stored externally.
99  unsigned OutOfDate : 1;
100 
101  // True if this is the 'import' contextual keyword.
102  unsigned IsModulesImport : 1;
103 
104  // 29 bits left in a 64-bit word.
105 
106  // Managed by the language front-end.
107  void *FETokenInfo = nullptr;
108 
109  llvm::StringMapEntry<IdentifierInfo *> *Entry = nullptr;
110 
111 public:
112  IdentifierInfo();
113  IdentifierInfo(const IdentifierInfo &) = delete;
114  IdentifierInfo &operator=(const IdentifierInfo &) = delete;
115 
116  /// Return true if this is the identifier for the specified string.
117  ///
118  /// This is intended to be used for string literals only: II->isStr("foo").
119  template <std::size_t StrLen>
120  bool isStr(const char (&Str)[StrLen]) const {
121  return getLength() == StrLen-1 &&
122  memcmp(getNameStart(), Str, StrLen-1) == 0;
123  }
124 
125  /// Return true if this is the identifier for the specified StringRef.
126  bool isStr(llvm::StringRef Str) const {
127  llvm::StringRef ThisStr(getNameStart(), getLength());
128  return ThisStr == Str;
129  }
130 
131  /// Return the beginning of the actual null-terminated string for this
132  /// identifier.
133  const char *getNameStart() const {
134  if (Entry) return Entry->getKeyData();
135  // FIXME: This is gross. It would be best not to embed specific details
136  // of the PTH file format here.
137  // The 'this' pointer really points to a
138  // std::pair<IdentifierInfo, const char*>, where internal pointer
139  // points to the external string data.
140  using actualtype = std::pair<IdentifierInfo, const char *>;
141 
142  return ((const actualtype*) this)->second;
143  }
144 
145  /// Efficiently return the length of this identifier info.
146  unsigned getLength() const {
147  if (Entry) return Entry->getKeyLength();
148  // FIXME: This is gross. It would be best not to embed specific details
149  // of the PTH file format here.
150  // The 'this' pointer really points to a
151  // std::pair<IdentifierInfo, const char*>, where internal pointer
152  // points to the external string data.
153  using actualtype = std::pair<IdentifierInfo, const char *>;
154 
155  const char* p = ((const actualtype*) this)->second - 2;
156  return (((unsigned) p[0]) | (((unsigned) p[1]) << 8)) - 1;
157  }
158 
159  /// Return the actual identifier string.
160  StringRef getName() const {
161  return StringRef(getNameStart(), getLength());
162  }
163 
164  /// Return true if this identifier is \#defined to some other value.
165  /// \note The current definition may be in a module and not currently visible.
166  bool hasMacroDefinition() const {
167  return HasMacro;
168  }
169  void setHasMacroDefinition(bool Val) {
170  if (HasMacro == Val) return;
171 
172  HasMacro = Val;
173  if (Val) {
174  NeedsHandleIdentifier = true;
175  HadMacro = true;
176  } else {
177  RecomputeNeedsHandleIdentifier();
178  }
179  }
180  /// Returns true if this identifier was \#defined to some value at any
181  /// moment. In this case there should be an entry for the identifier in the
182  /// macro history table in Preprocessor.
183  bool hadMacroDefinition() const {
184  return HadMacro;
185  }
186 
187  /// If this is a source-language token (e.g. 'for'), this API
188  /// can be used to cause the lexer to map identifiers to source-language
189  /// tokens.
190  tok::TokenKind getTokenID() const { return (tok::TokenKind)TokenID; }
191 
192  /// True if revertTokenIDToIdentifier() was called.
193  bool hasRevertedTokenIDToIdentifier() const { return RevertedTokenID; }
194 
195  /// Revert TokenID to tok::identifier; used for GNU libstdc++ 4.2
196  /// compatibility.
197  ///
198  /// TokenID is normally read-only but there are 2 instances where we revert it
199  /// to tok::identifier for libstdc++ 4.2. Keep track of when this happens
200  /// using this method so we can inform serialization about it.
202  assert(TokenID != tok::identifier && "Already at tok::identifier");
203  TokenID = tok::identifier;
204  RevertedTokenID = true;
205  }
207  assert(TokenID == tok::identifier && "Should be at tok::identifier");
208  TokenID = TK;
209  RevertedTokenID = false;
210  }
211 
212  /// Return the preprocessor keyword ID for this identifier.
213  ///
214  /// For example, "define" will return tok::pp_define.
216 
217  /// Return the Objective-C keyword ID for the this identifier.
218  ///
219  /// For example, 'class' will return tok::objc_class if ObjC is enabled.
221  if (ObjCOrBuiltinID < tok::NUM_OBJC_KEYWORDS)
222  return tok::ObjCKeywordKind(ObjCOrBuiltinID);
223  else
224  return tok::objc_not_keyword;
225  }
226  void setObjCKeywordID(tok::ObjCKeywordKind ID) { ObjCOrBuiltinID = ID; }
227 
228  /// True if setNotBuiltin() was called.
229  bool hasRevertedBuiltin() const {
230  return ObjCOrBuiltinID == tok::NUM_OBJC_KEYWORDS;
231  }
232 
233  /// Revert the identifier to a non-builtin identifier. We do this if
234  /// the name of a known builtin library function is used to declare that
235  /// function, but an unexpected type is specified.
236  void revertBuiltin() {
237  setBuiltinID(0);
238  }
239 
240  /// Return a value indicating whether this is a builtin function.
241  ///
242  /// 0 is not-built-in. 1+ are specific builtin functions.
243  unsigned getBuiltinID() const {
244  if (ObjCOrBuiltinID >= tok::NUM_OBJC_KEYWORDS)
245  return ObjCOrBuiltinID - tok::NUM_OBJC_KEYWORDS;
246  else
247  return 0;
248  }
249  void setBuiltinID(unsigned ID) {
250  ObjCOrBuiltinID = ID + tok::NUM_OBJC_KEYWORDS;
251  assert(ObjCOrBuiltinID - unsigned(tok::NUM_OBJC_KEYWORDS) == ID
252  && "ID too large for field!");
253  }
254 
255  unsigned getObjCOrBuiltinID() const { return ObjCOrBuiltinID; }
256  void setObjCOrBuiltinID(unsigned ID) { ObjCOrBuiltinID = ID; }
257 
258  /// get/setExtension - Initialize information about whether or not this
259  /// language token is an extension. This controls extension warnings, and is
260  /// only valid if a custom token ID is set.
261  bool isExtensionToken() const { return IsExtension; }
262  void setIsExtensionToken(bool Val) {
263  IsExtension = Val;
264  if (Val)
265  NeedsHandleIdentifier = true;
266  else
267  RecomputeNeedsHandleIdentifier();
268  }
269 
270  /// is/setIsFutureCompatKeyword - Initialize information about whether or not
271  /// this language token is a keyword in a newer or proposed Standard. This
272  /// controls compatibility warnings, and is only true when not parsing the
273  /// corresponding Standard. Once a compatibility problem has been diagnosed
274  /// with this keyword, the flag will be cleared.
275  bool isFutureCompatKeyword() const { return IsFutureCompatKeyword; }
276  void setIsFutureCompatKeyword(bool Val) {
277  IsFutureCompatKeyword = Val;
278  if (Val)
279  NeedsHandleIdentifier = true;
280  else
281  RecomputeNeedsHandleIdentifier();
282  }
283 
284  /// setIsPoisoned - Mark this identifier as poisoned. After poisoning, the
285  /// Preprocessor will emit an error every time this token is used.
286  void setIsPoisoned(bool Value = true) {
287  IsPoisoned = Value;
288  if (Value)
289  NeedsHandleIdentifier = true;
290  else
291  RecomputeNeedsHandleIdentifier();
292  }
293 
294  /// Return true if this token has been poisoned.
295  bool isPoisoned() const { return IsPoisoned; }
296 
297  /// isCPlusPlusOperatorKeyword/setIsCPlusPlusOperatorKeyword controls whether
298  /// this identifier is a C++ alternate representation of an operator.
299  void setIsCPlusPlusOperatorKeyword(bool Val = true) {
300  IsCPPOperatorKeyword = Val;
301  }
302  bool isCPlusPlusOperatorKeyword() const { return IsCPPOperatorKeyword; }
303 
304  /// Return true if this token is a keyword in the specified language.
305  bool isKeyword(const LangOptions &LangOpts) const;
306 
307  /// Return true if this token is a C++ keyword in the specified
308  /// language.
309  bool isCPlusPlusKeyword(const LangOptions &LangOpts) const;
310 
311  /// getFETokenInfo/setFETokenInfo - The language front-end is allowed to
312  /// associate arbitrary metadata with this token.
313  template<typename T>
314  T *getFETokenInfo() const { return static_cast<T*>(FETokenInfo); }
315  void setFETokenInfo(void *T) { FETokenInfo = T; }
316 
317  /// Return true if the Preprocessor::HandleIdentifier must be called
318  /// on a token of this identifier.
319  ///
320  /// If this returns false, we know that HandleIdentifier will not affect
321  /// the token.
322  bool isHandleIdentifierCase() const { return NeedsHandleIdentifier; }
323 
324  /// Return true if the identifier in its current state was loaded
325  /// from an AST file.
326  bool isFromAST() const { return IsFromAST; }
327 
328  void setIsFromAST() { IsFromAST = true; }
329 
330  /// Determine whether this identifier has changed since it was loaded
331  /// from an AST file.
333  return ChangedAfterLoad;
334  }
335 
336  /// Note that this identifier has changed since it was loaded from
337  /// an AST file.
339  ChangedAfterLoad = true;
340  }
341 
342  /// Determine whether the frontend token information for this
343  /// identifier has changed since it was loaded from an AST file.
345  return FEChangedAfterLoad;
346  }
347 
348  /// Note that the frontend token information for this identifier has
349  /// changed since it was loaded from an AST file.
351  FEChangedAfterLoad = true;
352  }
353 
354  /// Determine whether the information for this identifier is out of
355  /// date with respect to the external source.
356  bool isOutOfDate() const { return OutOfDate; }
357 
358  /// Set whether the information for this identifier is out of
359  /// date with respect to the external source.
360  void setOutOfDate(bool OOD) {
361  OutOfDate = OOD;
362  if (OOD)
363  NeedsHandleIdentifier = true;
364  else
365  RecomputeNeedsHandleIdentifier();
366  }
367 
368  /// Determine whether this is the contextual keyword \c import.
369  bool isModulesImport() const { return IsModulesImport; }
370 
371  /// Set whether this identifier is the contextual keyword \c import.
372  void setModulesImport(bool I) {
373  IsModulesImport = I;
374  if (I)
375  NeedsHandleIdentifier = true;
376  else
377  RecomputeNeedsHandleIdentifier();
378  }
379 
380  /// Return true if this identifier is an editor placeholder.
381  ///
382  /// Editor placeholders are produced by the code-completion engine and are
383  /// represented as characters between '<#' and '#>' in the source code. An
384  /// example of auto-completed call with a placeholder parameter is shown
385  /// below:
386  /// \code
387  /// function(<#int x#>);
388  /// \endcode
389  bool isEditorPlaceholder() const {
390  return getName().startswith("<#") && getName().endswith("#>");
391  }
392 
393  /// Provide less than operator for lexicographical sorting.
394  bool operator<(const IdentifierInfo &RHS) const {
395  return getName() < RHS.getName();
396  }
397 
398 private:
399  /// The Preprocessor::HandleIdentifier does several special (but rare)
400  /// things to identifiers of various sorts. For example, it changes the
401  /// \c for keyword token from tok::identifier to tok::for.
402  ///
403  /// This method is very tied to the definition of HandleIdentifier. Any
404  /// change to it should be reflected here.
405  void RecomputeNeedsHandleIdentifier() {
406  NeedsHandleIdentifier = isPoisoned() || hasMacroDefinition() ||
409  }
410 };
411 
412 /// An RAII object for [un]poisoning an identifier within a scope.
413 ///
414 /// \p II is allowed to be null, in which case objects of this type have
415 /// no effect.
417  IdentifierInfo *const II;
418  const bool OldValue;
419 
420 public:
422  : II(II), OldValue(II ? II->isPoisoned() : false) {
423  if(II)
424  II->setIsPoisoned(NewValue);
425  }
426 
428  if(II)
429  II->setIsPoisoned(OldValue);
430  }
431 };
432 
433 /// An iterator that walks over all of the known identifiers
434 /// in the lookup table.
435 ///
436 /// Since this iterator uses an abstract interface via virtual
437 /// functions, it uses an object-oriented interface rather than the
438 /// more standard C++ STL iterator interface. In this OO-style
439 /// iteration, the single function \c Next() provides dereference,
440 /// advance, and end-of-sequence checking in a single
441 /// operation. Subclasses of this iterator type will provide the
442 /// actual functionality.
444 protected:
445  IdentifierIterator() = default;
446 
447 public:
448  IdentifierIterator(const IdentifierIterator &) = delete;
450 
451  virtual ~IdentifierIterator();
452 
453  /// Retrieve the next string in the identifier table and
454  /// advances the iterator for the following string.
455  ///
456  /// \returns The next string in the identifier table. If there is
457  /// no such string, returns an empty \c StringRef.
458  virtual StringRef Next() = 0;
459 };
460 
461 /// Provides lookups to, and iteration over, IdentiferInfo objects.
463 public:
464  virtual ~IdentifierInfoLookup();
465 
466  /// Return the IdentifierInfo for the specified named identifier.
467  ///
468  /// Unlike the version in IdentifierTable, this returns a pointer instead
469  /// of a reference. If the pointer is null then the IdentifierInfo cannot
470  /// be found.
471  virtual IdentifierInfo* get(StringRef Name) = 0;
472 
473  /// Retrieve an iterator into the set of all identifiers
474  /// known to this identifier lookup source.
475  ///
476  /// This routine provides access to all of the identifiers known to
477  /// the identifier lookup, allowing access to the contents of the
478  /// identifiers without introducing the overhead of constructing
479  /// IdentifierInfo objects for each.
480  ///
481  /// \returns A new iterator into the set of known identifiers. The
482  /// caller is responsible for deleting this iterator.
483  virtual IdentifierIterator *getIdentifiers();
484 };
485 
486 /// Implements an efficient mapping from strings to IdentifierInfo nodes.
487 ///
488 /// This has no other purpose, but this is an extremely performance-critical
489 /// piece of the code, as each occurrence of every identifier goes through
490 /// here when lexed.
492  // Shark shows that using MallocAllocator is *much* slower than using this
493  // BumpPtrAllocator!
494  using HashTableTy = llvm::StringMap<IdentifierInfo *, llvm::BumpPtrAllocator>;
495  HashTableTy HashTable;
496 
497  IdentifierInfoLookup* ExternalLookup;
498 
499 public:
500  /// Create the identifier table.
501  explicit IdentifierTable(IdentifierInfoLookup *ExternalLookup = nullptr);
502 
503  /// Create the identifier table, populating it with info about the
504  /// language keywords for the language specified by \p LangOpts.
505  explicit IdentifierTable(const LangOptions &LangOpts,
506  IdentifierInfoLookup *ExternalLookup = nullptr);
507 
508  /// Set the external identifier lookup mechanism.
510  ExternalLookup = IILookup;
511  }
512 
513  /// Retrieve the external identifier lookup object, if any.
515  return ExternalLookup;
516  }
517 
518  llvm::BumpPtrAllocator& getAllocator() {
519  return HashTable.getAllocator();
520  }
521 
522  /// Return the identifier token info for the specified named
523  /// identifier.
524  IdentifierInfo &get(StringRef Name) {
525  auto &Entry = *HashTable.insert(std::make_pair(Name, nullptr)).first;
526 
527  IdentifierInfo *&II = Entry.second;
528  if (II) return *II;
529 
530  // No entry; if we have an external lookup, look there first.
531  if (ExternalLookup) {
532  II = ExternalLookup->get(Name);
533  if (II)
534  return *II;
535  }
536 
537  // Lookups failed, make a new IdentifierInfo.
538  void *Mem = getAllocator().Allocate<IdentifierInfo>();
539  II = new (Mem) IdentifierInfo();
540 
541  // Make sure getName() knows how to find the IdentifierInfo
542  // contents.
543  II->Entry = &Entry;
544 
545  return *II;
546  }
547 
548  IdentifierInfo &get(StringRef Name, tok::TokenKind TokenCode) {
549  IdentifierInfo &II = get(Name);
550  II.TokenID = TokenCode;
551  assert(II.TokenID == (unsigned) TokenCode && "TokenCode too large");
552  return II;
553  }
554 
555  /// Gets an IdentifierInfo for the given name without consulting
556  /// external sources.
557  ///
558  /// This is a version of get() meant for external sources that want to
559  /// introduce or modify an identifier. If they called get(), they would
560  /// likely end up in a recursion.
561  IdentifierInfo &getOwn(StringRef Name) {
562  auto &Entry = *HashTable.insert(std::make_pair(Name, nullptr)).first;
563 
564  IdentifierInfo *&II = Entry.second;
565  if (II)
566  return *II;
567 
568  // Lookups failed, make a new IdentifierInfo.
569  void *Mem = getAllocator().Allocate<IdentifierInfo>();
570  II = new (Mem) IdentifierInfo();
571 
572  // Make sure getName() knows how to find the IdentifierInfo
573  // contents.
574  II->Entry = &Entry;
575 
576  // If this is the 'import' contextual keyword, mark it as such.
577  if (Name.equals("import"))
578  II->setModulesImport(true);
579 
580  return *II;
581  }
582 
583  using iterator = HashTableTy::const_iterator;
584  using const_iterator = HashTableTy::const_iterator;
585 
586  iterator begin() const { return HashTable.begin(); }
587  iterator end() const { return HashTable.end(); }
588  unsigned size() const { return HashTable.size(); }
589 
590  /// Print some statistics to stderr that indicate how well the
591  /// hashing is doing.
592  void PrintStats() const;
593 
594  /// Populate the identifier table with info about the language keywords
595  /// for the language specified by \p LangOpts.
596  void AddKeywords(const LangOptions &LangOpts);
597 };
598 
599 /// A family of Objective-C methods.
600 ///
601 /// These families have no inherent meaning in the language, but are
602 /// nonetheless central enough in the existing implementations to
603 /// merit direct AST support. While, in theory, arbitrary methods can
604 /// be considered to form families, we focus here on the methods
605 /// involving allocation and retain-count management, as these are the
606 /// most "core" and the most likely to be useful to diverse clients
607 /// without extra information.
608 ///
609 /// Both selectors and actual method declarations may be classified
610 /// into families. Method families may impose additional restrictions
611 /// beyond their selector name; for example, a method called '_init'
612 /// that returns void is not considered to be in the 'init' family
613 /// (but would be if it returned 'id'). It is also possible to
614 /// explicitly change or remove a method's family. Therefore the
615 /// method's family should be considered the single source of truth.
617  /// No particular method family.
619 
620  // Selectors in these families may have arbitrary arity, may be
621  // written with arbitrary leading underscores, and may have
622  // additional CamelCase "words" in their first selector chunk
623  // following the family name.
629 
630  // These families are singletons consisting only of the nullary
631  // selector with the given name.
640 
641  // performSelector families
643 };
644 
645 /// Enough bits to store any enumerator in ObjCMethodFamily or
646 /// InvalidObjCMethodFamily.
648 
649 /// An invalid value of ObjCMethodFamily.
651 
652 /// A family of Objective-C methods.
653 ///
654 /// These are family of methods whose result type is initially 'id', but
655 /// but are candidate for the result type to be changed to 'instancetype'.
663 };
664 
669 };
670 
671 /// Smart pointer class that efficiently represents Objective-C method
672 /// names.
673 ///
674 /// This class will either point to an IdentifierInfo or a
675 /// MultiKeywordSelector (which is private). This enables us to optimize
676 /// selectors that take no arguments and selectors that take 1 argument, which
677 /// accounts for 78% of all selectors in Cocoa.h.
678 class Selector {
679  friend class Diagnostic;
680 
681  enum IdentifierInfoFlag {
682  // Empty selector = 0.
683  ZeroArg = 0x1,
684  OneArg = 0x2,
685  MultiArg = 0x3,
686  ArgFlags = ZeroArg|OneArg
687  };
688 
689  // a pointer to the MultiKeywordSelector or IdentifierInfo.
690  uintptr_t InfoPtr = 0;
691 
692  Selector(IdentifierInfo *II, unsigned nArgs) {
693  InfoPtr = reinterpret_cast<uintptr_t>(II);
694  assert((InfoPtr & ArgFlags) == 0 &&"Insufficiently aligned IdentifierInfo");
695  assert(nArgs < 2 && "nArgs not equal to 0/1");
696  InfoPtr |= nArgs+1;
697  }
698 
700  InfoPtr = reinterpret_cast<uintptr_t>(SI);
701  assert((InfoPtr & ArgFlags) == 0 &&"Insufficiently aligned IdentifierInfo");
702  InfoPtr |= MultiArg;
703  }
704 
705  IdentifierInfo *getAsIdentifierInfo() const {
706  if (getIdentifierInfoFlag() < MultiArg)
707  return reinterpret_cast<IdentifierInfo *>(InfoPtr & ~ArgFlags);
708  return nullptr;
709  }
710 
711  MultiKeywordSelector *getMultiKeywordSelector() const {
712  return reinterpret_cast<MultiKeywordSelector *>(InfoPtr & ~ArgFlags);
713  }
714 
715  unsigned getIdentifierInfoFlag() const {
716  return InfoPtr & ArgFlags;
717  }
718 
719  static ObjCMethodFamily getMethodFamilyImpl(Selector sel);
720 
721  static ObjCStringFormatFamily getStringFormatFamilyImpl(Selector sel);
722 
723 public:
724  friend class SelectorTable; // only the SelectorTable can create these
725  friend class DeclarationName; // and the AST's DeclarationName.
726 
727  /// The default ctor should only be used when creating data structures that
728  /// will contain selectors.
729  Selector() = default;
730  Selector(uintptr_t V) : InfoPtr(V) {}
731 
732  /// operator==/!= - Indicate whether the specified selectors are identical.
733  bool operator==(Selector RHS) const {
734  return InfoPtr == RHS.InfoPtr;
735  }
736  bool operator!=(Selector RHS) const {
737  return InfoPtr != RHS.InfoPtr;
738  }
739 
740  void *getAsOpaquePtr() const {
741  return reinterpret_cast<void*>(InfoPtr);
742  }
743 
744  /// Determine whether this is the empty selector.
745  bool isNull() const { return InfoPtr == 0; }
746 
747  // Predicates to identify the selector type.
748  bool isKeywordSelector() const {
749  return getIdentifierInfoFlag() != ZeroArg;
750  }
751 
752  bool isUnarySelector() const {
753  return getIdentifierInfoFlag() == ZeroArg;
754  }
755 
756  unsigned getNumArgs() const;
757 
758  /// Retrieve the identifier at a given position in the selector.
759  ///
760  /// Note that the identifier pointer returned may be NULL. Clients that only
761  /// care about the text of the identifier string, and not the specific,
762  /// uniqued identifier pointer, should use \c getNameForSlot(), which returns
763  /// an empty string when the identifier pointer would be NULL.
764  ///
765  /// \param argIndex The index for which we want to retrieve the identifier.
766  /// This index shall be less than \c getNumArgs() unless this is a keyword
767  /// selector, in which case 0 is the only permissible value.
768  ///
769  /// \returns the uniqued identifier for this slot, or NULL if this slot has
770  /// no corresponding identifier.
771  IdentifierInfo *getIdentifierInfoForSlot(unsigned argIndex) const;
772 
773  /// Retrieve the name at a given position in the selector.
774  ///
775  /// \param argIndex The index for which we want to retrieve the name.
776  /// This index shall be less than \c getNumArgs() unless this is a keyword
777  /// selector, in which case 0 is the only permissible value.
778  ///
779  /// \returns the name for this slot, which may be the empty string if no
780  /// name was supplied.
781  StringRef getNameForSlot(unsigned argIndex) const;
782 
783  /// Derive the full selector name (e.g. "foo:bar:") and return
784  /// it as an std::string.
785  std::string getAsString() const;
786 
787  /// Prints the full selector name (e.g. "foo:bar:").
788  void print(llvm::raw_ostream &OS) const;
789 
790  void dump() const;
791 
792  /// Derive the conventional family of this method.
794  return getMethodFamilyImpl(*this);
795  }
796 
798  return getStringFormatFamilyImpl(*this);
799  }
800 
802  return Selector(uintptr_t(-1));
803  }
804 
806  return Selector(uintptr_t(-2));
807  }
808 
809  static ObjCInstanceTypeFamily getInstTypeMethodFamily(Selector sel);
810 };
811 
812 /// This table allows us to fully hide how we implement
813 /// multi-keyword caching.
815  // Actually a SelectorTableImpl
816  void *Impl;
817 
818 public:
819  SelectorTable();
820  SelectorTable(const SelectorTable &) = delete;
821  SelectorTable &operator=(const SelectorTable &) = delete;
822  ~SelectorTable();
823 
824  /// Can create any sort of selector.
825  ///
826  /// \p NumArgs indicates whether this is a no argument selector "foo", a
827  /// single argument selector "foo:" or multi-argument "foo:bar:".
828  Selector getSelector(unsigned NumArgs, IdentifierInfo **IIV);
829 
831  return Selector(ID, 1);
832  }
833 
835  return Selector(ID, 0);
836  }
837 
838  /// Return the total amount of memory allocated for managing selectors.
839  size_t getTotalMemory() const;
840 
841  /// Return the default setter name for the given identifier.
842  ///
843  /// This is "set" + \p Name where the initial character of \p Name
844  /// has been capitalized.
845  static SmallString<64> constructSetterName(StringRef Name);
846 
847  /// Return the default setter selector for the given identifier.
848  ///
849  /// This is "set" + \p Name where the initial character of \p Name
850  /// has been capitalized.
851  static Selector constructSetterSelector(IdentifierTable &Idents,
852  SelectorTable &SelTable,
853  const IdentifierInfo *Name);
854 
855  /// Return the property name for the given setter selector.
856  static std::string getPropertyNameFromSetterSelector(Selector Sel);
857 };
858 
859 /// DeclarationNameExtra - Common base of the MultiKeywordSelector,
860 /// CXXSpecialName, and CXXOperatorIdName classes, all of which are
861 /// private classes that describe different kinds of names.
863 public:
864  /// ExtraKind - The kind of "extra" information stored in the
865  /// DeclarationName. See @c ExtraKindOrNumArgs for an explanation of
866  /// how these enumerator values are used.
867  enum ExtraKind {
868  CXXConstructor = 0,
871 #define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
872  CXXOperator##Name,
873 #include "clang/Basic/OperatorKinds.def"
877  NUM_EXTRA_KINDS
878  };
879 
880  /// ExtraKindOrNumArgs - Either the kind of C++ special name or
881  /// operator-id (if the value is one of the CXX* enumerators of
882  /// ExtraKind), in which case the DeclarationNameExtra is also a
883  /// CXXSpecialName, (for CXXConstructor, CXXDestructor, or
884  /// CXXConversionFunction) CXXOperatorIdName, or CXXLiteralOperatorName,
885  /// it may be also name common to C++ using-directives (CXXUsingDirective),
886  /// otherwise it is NUM_EXTRA_KINDS+NumArgs, where NumArgs is the number of
887  /// arguments in the Objective-C selector, in which case the
888  /// DeclarationNameExtra is also a MultiKeywordSelector.
890 };
891 
892 } // namespace clang
893 
894 namespace llvm {
895 
896 /// Define DenseMapInfo so that Selectors can be used as keys in DenseMap and
897 /// DenseSets.
898 template <>
899 struct DenseMapInfo<clang::Selector> {
902  }
903 
906  }
907 
908  static unsigned getHashValue(clang::Selector S);
909 
910  static bool isEqual(clang::Selector LHS, clang::Selector RHS) {
911  return LHS == RHS;
912  }
913 };
914 
915 template <>
916 struct isPodLike<clang::Selector> { static const bool value = true; };
917 
918 template<>
919 struct PointerLikeTypeTraits<clang::Selector> {
920  static const void *getAsVoidPointer(clang::Selector P) {
921  return P.getAsOpaquePtr();
922  }
923 
924  static clang::Selector getFromVoidPointer(const void *P) {
925  return clang::Selector(reinterpret_cast<uintptr_t>(P));
926  }
927 
928  enum { NumLowBitsAvailable = 0 };
929 };
930 
931 // Provide PointerLikeTypeTraits for IdentifierInfo pointers, which
932 // are not guaranteed to be 8-byte aligned.
933 template<>
936  return P;
937  }
938 
940  return static_cast<clang::IdentifierInfo*>(P);
941  }
942 
943  enum { NumLowBitsAvailable = 1 };
944 };
945 
946 template<>
948  static const void *getAsVoidPointer(const clang::IdentifierInfo* P) {
949  return P;
950  }
951 
952  static const clang::IdentifierInfo *getFromVoidPointer(const void *P) {
953  return static_cast<const clang::IdentifierInfo*>(P);
954  }
955 
956  enum { NumLowBitsAvailable = 1 };
957 };
958 
959 } // namespace llvm
960 
961 #endif // LLVM_CLANG_BASIC_IDENTIFIERTABLE_H
ObjCStringFormatFamily
Smart pointer class that efficiently represents Objective-C method names.
void * getAsOpaquePtr() const
void setChangedSinceDeserialization()
Note that this identifier has changed since it was loaded from an AST file.
static const clang::IdentifierInfo * getFromVoidPointer(const void *P)
void revertIdentifierToTokenID(tok::TokenKind TK)
DominatorTree GraphTraits specialization so the DominatorTree can be iterable by generic graph iterat...
Definition: Dominators.h:30
void setModulesImport(bool I)
Set whether this identifier is the contextual keyword import.
static clang::Selector getEmptyKey()
void setIsExtensionToken(bool Val)
StringRef P
bool isOutOfDate() const
Determine whether the information for this identifier is out of date with respect to the external sou...
friend class IdentifierTable
bool isFromAST() const
Return true if the identifier in its current state was loaded from an AST file.
static clang::Selector getFromVoidPointer(const void *P)
Selector getUnarySelector(IdentifierInfo *ID)
One of these records is kept for each identifier that is lexed.
This table allows us to fully hide how we implement multi-keyword caching.
bool isStr(const char(&Str)[StrLen]) const
Return true if this is the identifier for the specified string.
T * getFETokenInfo() const
getFETokenInfo/setFETokenInfo - The language front-end is allowed to associate arbitrary metadata wit...
HashTableTy::const_iterator const_iterator
void setHasMacroDefinition(bool Val)
ObjCMethodFamily
A family of Objective-C methods.
bool isCPlusPlusOperatorKeyword() const
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Definition: LangOptions.h:50
virtual IdentifierInfo * get(StringRef Name)=0
Return the IdentifierInfo for the specified named identifier.
bool isUnarySelector() const
void setIsFutureCompatKeyword(bool Val)
DeclarationNameExtra - Common base of the MultiKeywordSelector, CXXSpecialName, and CXXOperatorIdName...
Selector getNullarySelector(IdentifierInfo *ID)
tok::TokenKind getTokenID() const
If this is a source-language token (e.g.
static void dump(llvm::raw_ostream &OS, StringRef FunctionName, ArrayRef< CounterExpression > Expressions, ArrayRef< CounterMappingRegion > Regions)
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified...
IdentifierInfoLookup * getExternalIdentifierLookup() const
Retrieve the external identifier lookup object, if any.
unsigned size() const
iterator begin() const
bool hasRevertedBuiltin() const
True if setNotBuiltin() was called.
static clang::Selector getTombstoneKey()
bool isCPlusPlusKeyword(const LangOptions &LangOpts) const
Return true if this token is a C++ keyword in the specified language.
void revertBuiltin()
Revert the identifier to a non-builtin identifier.
unsigned getLength() const
Efficiently return the length of this identifier info.
bool operator!=(Selector RHS) const
static bool isEqual(clang::Selector LHS, clang::Selector RHS)
HashTableTy::const_iterator iterator
bool hasChangedSinceDeserialization() const
Determine whether this identifier has changed since it was loaded from an AST file.
unsigned ExtraKindOrNumArgs
ExtraKindOrNumArgs - Either the kind of C++ special name or operator-id (if the value is one of the C...
IdentifierInfo & getOwn(StringRef Name)
Gets an IdentifierInfo for the given name without consulting external sources.
Provides lookups to, and iteration over, IdentiferInfo objects.
void setIsCPlusPlusOperatorKeyword(bool Val=true)
isCPlusPlusOperatorKeyword/setIsCPlusPlusOperatorKeyword controls whether this identifier is a C++ al...
MultiKeywordSelector - One of these variable length records is kept for each selector containing more...
ObjCStringFormatFamily getStringFormatFamily() const
llvm::StringRef getAsString(SyncScope S)
Definition: SyncScope.h:51
Implements an efficient mapping from strings to IdentifierInfo nodes.
void setFETokenInfo(void *T)
tok::ObjCKeywordKind getObjCKeywordID() const
Return the Objective-C keyword ID for the this identifier.
PPKeywordKind
Provides a namespace for preprocessor keywords which start with a &#39;#&#39; at the beginning of the line...
Definition: TokenKinds.h:33
void setIsPoisoned(bool Value=true)
setIsPoisoned - Mark this identifier as poisoned.
bool hasFETokenInfoChangedSinceDeserialization() const
Determine whether the frontend token information for this identifier has changed since it was loaded ...
bool operator==(Selector RHS) const
operator==/!= - Indicate whether the specified selectors are identical.
ObjCInstanceTypeFamily
A family of Objective-C methods.
static const void * getAsVoidPointer(clang::Selector P)
An iterator that walks over all of the known identifiers in the lookup table.
bool isStr(llvm::StringRef Str) const
Return true if this is the identifier for the specified StringRef.
__UINTPTR_TYPE__ uintptr_t
An unsigned integer type with the property that any valid pointer to void can be converted to this ty...
Definition: opencl-c.h:82
ObjCKeywordKind
Provides a namespace for Objective-C keywords which start with an &#39;@&#39;.
Definition: TokenKinds.h:41
void setObjCKeywordID(tok::ObjCKeywordKind ID)
bool isKeyword(const LangOptions &LangOpts) const
Return true if this token is a keyword in the specified language.
unsigned getBuiltinID() const
Return a value indicating whether this is a builtin function.
static void * getAsVoidPointer(clang::IdentifierInfo *P)
ExtraKind
ExtraKind - The kind of "extra" information stored in the DeclarationName.
#define false
Definition: stdbool.h:33
PoisonIdentifierRAIIObject(IdentifierInfo *II, bool NewValue)
ObjCMethodFamily getMethodFamily() const
Derive the conventional family of this method.
IdentifierInfo & operator=(const IdentifierInfo &)=delete
void setFETokenInfoChangedSinceDeserialization()
Note that the frontend token information for this identifier has changed since it was loaded from an ...
iterator end() const
Selector(uintptr_t V)
static clang::IdentifierInfo * getFromVoidPointer(void *P)
llvm::BumpPtrAllocator & getAllocator()
const char * getNameStart() const
Return the beginning of the actual null-terminated string for this identifier.
bool isPoisoned() const
Return true if this token has been poisoned.
TokenKind
Provides a simple uniform namespace for tokens from all C languages.
Definition: TokenKinds.h:25
bool operator<(const IdentifierInfo &RHS) const
Provide less than operator for lexicographical sorting.
StringRef getName() const
Return the actual identifier string.
unsigned getObjCOrBuiltinID() const
bool hadMacroDefinition() const
Returns true if this identifier was #defined to some value at any moment.
Dataflow Directional Tag Classes.
std::pair< IdentifierInfo *, SourceLocation > IdentifierLocPair
A simple pair of identifier info and location.
bool isHandleIdentifierCase() const
Return true if the Preprocessor::HandleIdentifier must be called on a token of this identifier...
DeclarationName - The name of a declaration.
bool isExtensionToken() const
get/setExtension - Initialize information about whether or not this language token is an extension...
bool isKeywordSelector() const
tok::PPKeywordKind getPPKeywordID() const
Return the preprocessor keyword ID for this identifier.
static Selector getTombstoneMarker()
static Selector getEmptyMarker()
void setBuiltinID(unsigned ID)
Defines the clang::TokenKind enum and support functions.
No particular method family.
static const void * getAsVoidPointer(const clang::IdentifierInfo *P)
bool hasRevertedTokenIDToIdentifier() const
True if revertTokenIDToIdentifier() was called.
void setOutOfDate(bool OOD)
Set whether the information for this identifier is out of date with respect to the external source...
bool isNull() const
Determine whether this is the empty selector.
void revertTokenIDToIdentifier()
Revert TokenID to tok::identifier; used for GNU libstdc++ 4.2 compatibility.
A little helper class (which is basically a smart pointer that forwards info from DiagnosticsEngine) ...
Definition: Diagnostic.h:1315
bool isModulesImport() const
Determine whether this is the contextual keyword import.
bool isFutureCompatKeyword() const
is/setIsFutureCompatKeyword - Initialize information about whether or not this language token is a ke...
void setExternalIdentifierLookup(IdentifierInfoLookup *IILookup)
Set the external identifier lookup mechanism.
bool hasMacroDefinition() const
Return true if this identifier is #defined to some other value.
void setObjCOrBuiltinID(unsigned ID)
An RAII object for [un]poisoning an identifier within a scope.
bool isEditorPlaceholder() const
Return true if this identifier is an editor placeholder.