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