IdentifierTable.h revision 98d86b98b3fd0bd9c546123b16fd9995509aaae1
15f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===--- IdentifierTable.h - Hash table for identifier lookup ---*- C++ -*-===//
25f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
35f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//                     The LLVM Compiler Infrastructure
45f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
50bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// This file is distributed under the University of Illinois Open Source
60bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// License. See LICENSE.TXT for details.
75f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
85f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
95f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
10c7229c338c21ef26b01ef3ecf9eec4fd373fa9ecChris Lattner// This file defines the IdentifierInfo, IdentifierTable, and Selector
11c7229c338c21ef26b01ef3ecf9eec4fd373fa9ecChris Lattner// interfaces.
125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
15c7229c338c21ef26b01ef3ecf9eec4fd373fa9ecChris Lattner#ifndef LLVM_CLANG_BASIC_IDENTIFIERTABLE_H
16c7229c338c21ef26b01ef3ecf9eec4fd373fa9ecChris Lattner#define LLVM_CLANG_BASIC_IDENTIFIERTABLE_H
175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
181cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor#include "clang/Basic/OperatorKinds.h"
195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "clang/Basic/TokenKinds.h"
20686775deca8b8685eb90801495880e3abdd844c2Chris Lattner#include "clang/Basic/LLVM.h"
215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "llvm/ADT/StringMap.h"
22700030ebddb987936d4fee14d9412821d96e4840Kovarththanan Rajaratnam#include "llvm/ADT/StringRef.h"
2368d331a78e655d97294e94fcfa63f92cc1f40578Steve Naroff#include "llvm/ADT/SmallString.h"
2472b1b15ee88aac0a63e2c1dc53fe22f5ab297b20Ted Kremenek#include "llvm/ADT/OwningPtr.h"
251734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor#include "llvm/Support/PointerLikeTypeTraits.h"
261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump#include <cassert>
27403ba3522d1b1c97ae5fad81c1a2c4b3a754e1c1Nick Lewycky#include <cctype>
28403ba3522d1b1c97ae5fad81c1a2c4b3a754e1c1Nick Lewycky#include <string>
295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
3085994260c41a54cab061a434ed378fc448333a4eChris Lattnernamespace llvm {
3185994260c41a54cab061a434ed378fc448333a4eChris Lattner  template <typename T> struct DenseMapInfo;
3285994260c41a54cab061a434ed378fc448333a4eChris Lattner}
3329238a0bf7cbf5b396efb451a0adb5fe4aa037caSteve Naroff
345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencernamespace clang {
35ea684e699ea84e61711e279f5fa7a1b9f3d46bc2Cedric Venet  class LangOptions;
367caeabd868d46cf4e68478c6e9136dae4e735d21Chris Lattner  class IdentifierInfo;
37ea9c26b3dbd74a1497f5609ae6e19a85f42b6073Ted Kremenek  class IdentifierTable;
387caeabd868d46cf4e68478c6e9136dae4e735d21Chris Lattner  class SourceLocation;
392e1cd4264d363ca869bf37ef160902f211d21b8cDouglas Gregor  class MultiKeywordSelector; // private class used by Selector
402e1cd4264d363ca869bf37ef160902f211d21b8cDouglas Gregor  class DeclarationName;      // AST class that stores declaration names
412e1cd4264d363ca869bf37ef160902f211d21b8cDouglas Gregor
427caeabd868d46cf4e68478c6e9136dae4e735d21Chris Lattner  /// IdentifierLocPair - A simple pair of identifier info and location.
437caeabd868d46cf4e68478c6e9136dae4e735d21Chris Lattner  typedef std::pair<IdentifierInfo*, SourceLocation> IdentifierLocPair;
441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// IdentifierInfo - One of these records is kept for each identifier that
475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// is lexed.  This contains information about whether the token was #define'd,
485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// is a language keyword, or if it is a front-end token of some sort (e.g. a
495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// variable or function name).  The preprocessor keeps this information in a
501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// set, and all tok::identifier tokens have a pointer to one of these.
515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerclass IdentifierInfo {
528e748ab52395328f2905855b295a22e33dc800b2Ted Kremenek  // Note: DON'T make TokenID a 'tok::TokenKind'; MSVC will treat it as a
535cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor  //       signed char and TokenKinds > 255 won't be handled correctly.
545cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor  unsigned TokenID            : 9; // Front-end token ID or tok::identifier.
555142af38ed0dd2f592cbfa00fa6e2e14dd6cc516Douglas Gregor  // Objective-C keyword ('protocol' in '@protocol') or builtin (__builtin_inf).
565142af38ed0dd2f592cbfa00fa6e2e14dd6cc516Douglas Gregor  // First NUM_OBJC_KEYWORDS values are for Objective-C, the remaining values
575142af38ed0dd2f592cbfa00fa6e2e14dd6cc516Douglas Gregor  // are for builtins.
58c81ca9852806e75972bbb246fa916152e8fba541Ted Kremenek  unsigned ObjCOrBuiltinID    :11;
594365a7e46822700357a272d839ee2656d9603d5aChris Lattner  bool HasMacro               : 1; // True if there is a #define for this.
605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  bool IsExtension            : 1; // True if identifier is a lang extension.
6198d86b98b3fd0bd9c546123b16fd9995509aaae1Richard Smith  bool IsCXX11CompatKeyword   : 1; // True if identifier is a keyword in C++11.
625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  bool IsPoisoned             : 1; // True if identifier is poisoned.
635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  bool IsCPPOperatorKeyword   : 1; // True if ident is a C++ operator keyword.
646a170eb3ea6d6319277becabef68eb1a26bf8766Chris Lattner  bool NeedsHandleIdentifier  : 1; // See "RecomputeNeedsHandleIdentifier".
653c7f4134603d04b44f997b43c0a9def270f25386Sebastian Redl  bool IsFromAST              : 1; // True if identfier first appeared in an AST
663c7f4134603d04b44f997b43c0a9def270f25386Sebastian Redl                                   // file and wasn't modified since.
67646395bbcaa849c94bc2a3246c71d809ca719f01Argyrios Kyrtzidis  bool RevertedTokenID        : 1; // True if RevertTokenIDToIdentifier was
68646395bbcaa849c94bc2a3246c71d809ca719f01Argyrios Kyrtzidis                                   // called.
695cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor  // 5 bits left in 32-bit word.
705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void *FETokenInfo;               // Managed by the language front-end.
71ea9c26b3dbd74a1497f5609ae6e19a85f42b6073Ted Kremenek  llvm::StringMapEntry<IdentifierInfo*> *Entry;
721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  IdentifierInfo(const IdentifierInfo&);  // NONCOPYABLE.
744365a7e46822700357a272d839ee2656d9603d5aChris Lattner  void operator=(const IdentifierInfo&);  // NONASSIGNABLE.
75ea9c26b3dbd74a1497f5609ae6e19a85f42b6073Ted Kremenek
761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  friend class IdentifierTable;
77295a2a617ac335f590e430ab7fcd98f8ce109251Douglas Gregor
785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerpublic:
79ea9c26b3dbd74a1497f5609ae6e19a85f42b6073Ted Kremenek  IdentifierInfo();
805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
8292e62b02226410bcad8584541b8f1ff4d35ebab9Chris Lattner  /// isStr - Return true if this is the identifier for the specified string.
8392e62b02226410bcad8584541b8f1ff4d35ebab9Chris Lattner  /// This is intended to be used for string literals only: II->isStr("foo").
8492e62b02226410bcad8584541b8f1ff4d35ebab9Chris Lattner  template <std::size_t StrLen>
8592e62b02226410bcad8584541b8f1ff4d35ebab9Chris Lattner  bool isStr(const char (&Str)[StrLen]) const {
867fe60650c1133ee74a3395cf1063690e274bb7acDaniel Dunbar    return getLength() == StrLen-1 && !memcmp(getNameStart(), Str, StrLen-1);
87845222ccd992282bf74b2fca53e7c3b84a81c098Chris Lattner  }
881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
897fe60650c1133ee74a3395cf1063690e274bb7acDaniel Dunbar  /// getNameStart - Return the beginning of the actual string for this
907fe60650c1133ee74a3395cf1063690e274bb7acDaniel Dunbar  /// identifier.  The returned string is properly null terminated.
915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  ///
927fe60650c1133ee74a3395cf1063690e274bb7acDaniel Dunbar  const char *getNameStart() const {
93ea9c26b3dbd74a1497f5609ae6e19a85f42b6073Ted Kremenek    if (Entry) return Entry->getKeyData();
947e3a004c6ed1fe87912203b9c5a113f8da89d261Ted Kremenek    // FIXME: This is gross. It would be best not to embed specific details
957e3a004c6ed1fe87912203b9c5a113f8da89d261Ted Kremenek    // of the PTH file format here.
961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // The 'this' pointer really points to a
97ea9c26b3dbd74a1497f5609ae6e19a85f42b6073Ted Kremenek    // std::pair<IdentifierInfo, const char*>, where internal pointer
98ea9c26b3dbd74a1497f5609ae6e19a85f42b6073Ted Kremenek    // points to the external string data.
9940844a8b5a89676fb61898d61ea4a7fa98eb9b6bGabor Greif    typedef std::pair<IdentifierInfo, const char*> actualtype;
10040844a8b5a89676fb61898d61ea4a7fa98eb9b6bGabor Greif    return ((const actualtype*) this)->second;
1011f945f632ae993bbb9f9e69d908f7526924c1a88Chris Lattner  }
1021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1031f945f632ae993bbb9f9e69d908f7526924c1a88Chris Lattner  /// getLength - Efficiently return the length of this identifier info.
1041f945f632ae993bbb9f9e69d908f7526924c1a88Chris Lattner  ///
1051f945f632ae993bbb9f9e69d908f7526924c1a88Chris Lattner  unsigned getLength() const {
106ea9c26b3dbd74a1497f5609ae6e19a85f42b6073Ted Kremenek    if (Entry) return Entry->getKeyLength();
1077e3a004c6ed1fe87912203b9c5a113f8da89d261Ted Kremenek    // FIXME: This is gross. It would be best not to embed specific details
1087e3a004c6ed1fe87912203b9c5a113f8da89d261Ted Kremenek    // of the PTH file format here.
1091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // The 'this' pointer really points to a
110ea9c26b3dbd74a1497f5609ae6e19a85f42b6073Ted Kremenek    // std::pair<IdentifierInfo, const char*>, where internal pointer
111ea9c26b3dbd74a1497f5609ae6e19a85f42b6073Ted Kremenek    // points to the external string data.
11240844a8b5a89676fb61898d61ea4a7fa98eb9b6bGabor Greif    typedef std::pair<IdentifierInfo, const char*> actualtype;
11340844a8b5a89676fb61898d61ea4a7fa98eb9b6bGabor Greif    const char* p = ((const actualtype*) this)->second - 2;
1147fe60650c1133ee74a3395cf1063690e274bb7acDaniel Dunbar    return (((unsigned) p[0]) | (((unsigned) p[1]) << 8)) - 1;
1157fe60650c1133ee74a3395cf1063690e274bb7acDaniel Dunbar  }
1167fe60650c1133ee74a3395cf1063690e274bb7acDaniel Dunbar
11701eb9b9683535d8a65c704ad2c545903409e2d36Daniel Dunbar  /// getName - Return the actual identifier string.
118686775deca8b8685eb90801495880e3abdd844c2Chris Lattner  StringRef getName() const {
119686775deca8b8685eb90801495880e3abdd844c2Chris Lattner    return StringRef(getNameStart(), getLength());
1205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
1211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1229c46de446d18f4a28446cb798d4131bd05515699Chris Lattner  /// hasMacroDefinition - Return true if this identifier is #defined to some
1239c46de446d18f4a28446cb798d4131bd05515699Chris Lattner  /// other value.
1249c46de446d18f4a28446cb798d4131bd05515699Chris Lattner  bool hasMacroDefinition() const {
1259c46de446d18f4a28446cb798d4131bd05515699Chris Lattner    return HasMacro;
1269c46de446d18f4a28446cb798d4131bd05515699Chris Lattner  }
1276a170eb3ea6d6319277becabef68eb1a26bf8766Chris Lattner  void setHasMacroDefinition(bool Val) {
1286a170eb3ea6d6319277becabef68eb1a26bf8766Chris Lattner    if (HasMacro == Val) return;
1291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1306a170eb3ea6d6319277becabef68eb1a26bf8766Chris Lattner    HasMacro = Val;
1316a170eb3ea6d6319277becabef68eb1a26bf8766Chris Lattner    if (Val)
1326a170eb3ea6d6319277becabef68eb1a26bf8766Chris Lattner      NeedsHandleIdentifier = 1;
1336a170eb3ea6d6319277becabef68eb1a26bf8766Chris Lattner    else
1346a170eb3ea6d6319277becabef68eb1a26bf8766Chris Lattner      RecomputeNeedsHandleIdentifier();
1353c7f4134603d04b44f997b43c0a9def270f25386Sebastian Redl    IsFromAST = false;
1366a170eb3ea6d6319277becabef68eb1a26bf8766Chris Lattner  }
1371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
138646395bbcaa849c94bc2a3246c71d809ca719f01Argyrios Kyrtzidis  /// getTokenID - If this is a source-language token (e.g. 'for'), this API
1395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// can be used to cause the lexer to map identifiers to source-language
1405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// tokens.
1418e748ab52395328f2905855b295a22e33dc800b2Ted Kremenek  tok::TokenKind getTokenID() const { return (tok::TokenKind)TokenID; }
142646395bbcaa849c94bc2a3246c71d809ca719f01Argyrios Kyrtzidis
143646395bbcaa849c94bc2a3246c71d809ca719f01Argyrios Kyrtzidis  /// \brief True if RevertTokenIDToIdentifier() was called.
144646395bbcaa849c94bc2a3246c71d809ca719f01Argyrios Kyrtzidis  bool hasRevertedTokenIDToIdentifier() const { return RevertedTokenID; }
145646395bbcaa849c94bc2a3246c71d809ca719f01Argyrios Kyrtzidis
146646395bbcaa849c94bc2a3246c71d809ca719f01Argyrios Kyrtzidis  /// \brief Revert TokenID to tok::identifier; used for GNU libstdc++ 4.2
147f573084db5807f6003282bdf53ca9d58bab1ddc4Argyrios Kyrtzidis  /// compatibility.
148f573084db5807f6003282bdf53ca9d58bab1ddc4Argyrios Kyrtzidis  ///
149f573084db5807f6003282bdf53ca9d58bab1ddc4Argyrios Kyrtzidis  /// TokenID is normally read-only but there are 2 instances where we revert it
150f573084db5807f6003282bdf53ca9d58bab1ddc4Argyrios Kyrtzidis  /// to tok::identifier for libstdc++ 4.2. Keep track of when this happens
1513c7f4134603d04b44f997b43c0a9def270f25386Sebastian Redl  /// using this method so we can inform serialization about it.
152646395bbcaa849c94bc2a3246c71d809ca719f01Argyrios Kyrtzidis  void RevertTokenIDToIdentifier() {
153646395bbcaa849c94bc2a3246c71d809ca719f01Argyrios Kyrtzidis    assert(TokenID != tok::identifier && "Already at tok::identifier");
154646395bbcaa849c94bc2a3246c71d809ca719f01Argyrios Kyrtzidis    TokenID = tok::identifier;
155646395bbcaa849c94bc2a3246c71d809ca719f01Argyrios Kyrtzidis    RevertedTokenID = true;
156646395bbcaa849c94bc2a3246c71d809ca719f01Argyrios Kyrtzidis  }
1571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// getPPKeywordID - Return the preprocessor keyword ID for this identifier.
1596d9a3e648d6bf6b347174152f191bd1377528f8cChris Lattner  /// For example, "define" will return tok::pp_define.
160387b98d37e08f7aa9ddf23e067e1d27e39ce16f3Chris Lattner  tok::PPKeywordKind getPPKeywordID() const;
1611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// getObjCKeywordID - Return the Objective-C keyword ID for the this
1635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// identifier.  For example, 'class' will return tok::objc_class if ObjC is
1645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// enabled.
165ca63fa00786e51c207c829f4182f11a6c6b552beTed Kremenek  tok::ObjCKeywordKind getObjCKeywordID() const {
1661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    if (ObjCOrBuiltinID < tok::NUM_OBJC_KEYWORDS)
1675142af38ed0dd2f592cbfa00fa6e2e14dd6cc516Douglas Gregor      return tok::ObjCKeywordKind(ObjCOrBuiltinID);
1685142af38ed0dd2f592cbfa00fa6e2e14dd6cc516Douglas Gregor    else
1695142af38ed0dd2f592cbfa00fa6e2e14dd6cc516Douglas Gregor      return tok::objc_not_keyword;
170ca63fa00786e51c207c829f4182f11a6c6b552beTed Kremenek  }
1715142af38ed0dd2f592cbfa00fa6e2e14dd6cc516Douglas Gregor  void setObjCKeywordID(tok::ObjCKeywordKind ID) { ObjCOrBuiltinID = ID; }
1723251ceb90b3fec68e86d6dcfa58836e20a7205c3Douglas Gregor
1735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// getBuiltinID - Return a value indicating whether this is a builtin
1745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// function.  0 is not-built-in.  1 is builtin-for-some-nonprimary-target.
1755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// 2+ are specific builtin functions.
1761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  unsigned getBuiltinID() const {
1775142af38ed0dd2f592cbfa00fa6e2e14dd6cc516Douglas Gregor    if (ObjCOrBuiltinID >= tok::NUM_OBJC_KEYWORDS)
1781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      return ObjCOrBuiltinID - tok::NUM_OBJC_KEYWORDS;
1795142af38ed0dd2f592cbfa00fa6e2e14dd6cc516Douglas Gregor    else
1805142af38ed0dd2f592cbfa00fa6e2e14dd6cc516Douglas Gregor      return 0;
1815142af38ed0dd2f592cbfa00fa6e2e14dd6cc516Douglas Gregor  }
1825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void setBuiltinID(unsigned ID) {
1835142af38ed0dd2f592cbfa00fa6e2e14dd6cc516Douglas Gregor    ObjCOrBuiltinID = ID + tok::NUM_OBJC_KEYWORDS;
1841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    assert(ObjCOrBuiltinID - unsigned(tok::NUM_OBJC_KEYWORDS) == ID
1855142af38ed0dd2f592cbfa00fa6e2e14dd6cc516Douglas Gregor           && "ID too large for field!");
1865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
1873251ceb90b3fec68e86d6dcfa58836e20a7205c3Douglas Gregor
1883251ceb90b3fec68e86d6dcfa58836e20a7205c3Douglas Gregor  unsigned getObjCOrBuiltinID() const { return ObjCOrBuiltinID; }
1893251ceb90b3fec68e86d6dcfa58836e20a7205c3Douglas Gregor  void setObjCOrBuiltinID(unsigned ID) { ObjCOrBuiltinID = ID; }
1903251ceb90b3fec68e86d6dcfa58836e20a7205c3Douglas Gregor
1915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// get/setExtension - Initialize information about whether or not this
1925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// language token is an extension.  This controls extension warnings, and is
1935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// only valid if a custom token ID is set.
1945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  bool isExtensionToken() const { return IsExtension; }
1956a170eb3ea6d6319277becabef68eb1a26bf8766Chris Lattner  void setIsExtensionToken(bool Val) {
1966a170eb3ea6d6319277becabef68eb1a26bf8766Chris Lattner    IsExtension = Val;
1976a170eb3ea6d6319277becabef68eb1a26bf8766Chris Lattner    if (Val)
1986a170eb3ea6d6319277becabef68eb1a26bf8766Chris Lattner      NeedsHandleIdentifier = 1;
1996a170eb3ea6d6319277becabef68eb1a26bf8766Chris Lattner    else
2006a170eb3ea6d6319277becabef68eb1a26bf8766Chris Lattner      RecomputeNeedsHandleIdentifier();
2016a170eb3ea6d6319277becabef68eb1a26bf8766Chris Lattner  }
2021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
20398d86b98b3fd0bd9c546123b16fd9995509aaae1Richard Smith  /// is/setIsCXX11CompatKeyword - Initialize information about whether or not
20498d86b98b3fd0bd9c546123b16fd9995509aaae1Richard Smith  /// this language token is a keyword in C++11. This controls compatibility
20598d86b98b3fd0bd9c546123b16fd9995509aaae1Richard Smith  /// warnings, and is only true when not parsing C++11. Once a compatibility
20698d86b98b3fd0bd9c546123b16fd9995509aaae1Richard Smith  /// problem has been diagnosed with this keyword, the flag will be cleared.
20798d86b98b3fd0bd9c546123b16fd9995509aaae1Richard Smith  bool isCXX11CompatKeyword() const { return IsCXX11CompatKeyword; }
20898d86b98b3fd0bd9c546123b16fd9995509aaae1Richard Smith  void setIsCXX11CompatKeyword(bool Val) {
20998d86b98b3fd0bd9c546123b16fd9995509aaae1Richard Smith    IsCXX11CompatKeyword = Val;
21098d86b98b3fd0bd9c546123b16fd9995509aaae1Richard Smith    if (Val)
21198d86b98b3fd0bd9c546123b16fd9995509aaae1Richard Smith      NeedsHandleIdentifier = 1;
21298d86b98b3fd0bd9c546123b16fd9995509aaae1Richard Smith    else
21398d86b98b3fd0bd9c546123b16fd9995509aaae1Richard Smith      RecomputeNeedsHandleIdentifier();
21498d86b98b3fd0bd9c546123b16fd9995509aaae1Richard Smith  }
21598d86b98b3fd0bd9c546123b16fd9995509aaae1Richard Smith
2165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// setIsPoisoned - Mark this identifier as poisoned.  After poisoning, the
2175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// Preprocessor will emit an error every time this token is used.
2186a170eb3ea6d6319277becabef68eb1a26bf8766Chris Lattner  void setIsPoisoned(bool Value = true) {
2196a170eb3ea6d6319277becabef68eb1a26bf8766Chris Lattner    IsPoisoned = Value;
2206a170eb3ea6d6319277becabef68eb1a26bf8766Chris Lattner    if (Value)
2216a170eb3ea6d6319277becabef68eb1a26bf8766Chris Lattner      NeedsHandleIdentifier = 1;
2226a170eb3ea6d6319277becabef68eb1a26bf8766Chris Lattner    else
2236a170eb3ea6d6319277becabef68eb1a26bf8766Chris Lattner      RecomputeNeedsHandleIdentifier();
2243c7f4134603d04b44f997b43c0a9def270f25386Sebastian Redl    IsFromAST = false;
2256a170eb3ea6d6319277becabef68eb1a26bf8766Chris Lattner  }
2261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// isPoisoned - Return true if this token has been poisoned.
2285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  bool isPoisoned() const { return IsPoisoned; }
2291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// isCPlusPlusOperatorKeyword/setIsCPlusPlusOperatorKeyword controls whether
2315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// this identifier is a C++ alternate representation of an operator.
2326a170eb3ea6d6319277becabef68eb1a26bf8766Chris Lattner  void setIsCPlusPlusOperatorKeyword(bool Val = true) {
2336a170eb3ea6d6319277becabef68eb1a26bf8766Chris Lattner    IsCPPOperatorKeyword = Val;
2346a170eb3ea6d6319277becabef68eb1a26bf8766Chris Lattner    if (Val)
2356a170eb3ea6d6319277becabef68eb1a26bf8766Chris Lattner      NeedsHandleIdentifier = 1;
2366a170eb3ea6d6319277becabef68eb1a26bf8766Chris Lattner    else
2376a170eb3ea6d6319277becabef68eb1a26bf8766Chris Lattner      RecomputeNeedsHandleIdentifier();
2386a170eb3ea6d6319277becabef68eb1a26bf8766Chris Lattner  }
2395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  bool isCPlusPlusOperatorKeyword() const { return IsCPPOperatorKeyword; }
2405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
2415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// getFETokenInfo/setFETokenInfo - The language front-end is allowed to
2425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// associate arbitrary metadata with this token.
2435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  template<typename T>
2445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  T *getFETokenInfo() const { return static_cast<T*>(FETokenInfo); }
2455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void setFETokenInfo(void *T) { FETokenInfo = T; }
2466a170eb3ea6d6319277becabef68eb1a26bf8766Chris Lattner
2476a170eb3ea6d6319277becabef68eb1a26bf8766Chris Lattner  /// isHandleIdentifierCase - Return true if the Preprocessor::HandleIdentifier
2486a170eb3ea6d6319277becabef68eb1a26bf8766Chris Lattner  /// must be called on a token of this identifier.  If this returns false, we
2496a170eb3ea6d6319277becabef68eb1a26bf8766Chris Lattner  /// know that HandleIdentifier will not affect the token.
2506a170eb3ea6d6319277becabef68eb1a26bf8766Chris Lattner  bool isHandleIdentifierCase() const { return NeedsHandleIdentifier; }
2511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2523c7f4134603d04b44f997b43c0a9def270f25386Sebastian Redl  /// isFromAST - Return true if the identifier in its current state was loaded
2533c7f4134603d04b44f997b43c0a9def270f25386Sebastian Redl  /// from an AST file.
2543c7f4134603d04b44f997b43c0a9def270f25386Sebastian Redl  bool isFromAST() const { return IsFromAST; }
255ffaab3e2bb13991bb3357e80f14bcae3745b2347Sebastian Redl
2563c7f4134603d04b44f997b43c0a9def270f25386Sebastian Redl  void setIsFromAST(bool FromAST = true) { IsFromAST = FromAST; }
257ffaab3e2bb13991bb3357e80f14bcae3745b2347Sebastian Redl
2586a170eb3ea6d6319277becabef68eb1a26bf8766Chris Lattnerprivate:
2596a170eb3ea6d6319277becabef68eb1a26bf8766Chris Lattner  /// RecomputeNeedsHandleIdentifier - The Preprocessor::HandleIdentifier does
2606a170eb3ea6d6319277becabef68eb1a26bf8766Chris Lattner  /// several special (but rare) things to identifiers of various sorts.  For
2616a170eb3ea6d6319277becabef68eb1a26bf8766Chris Lattner  /// example, it changes the "for" keyword token from tok::identifier to
2626a170eb3ea6d6319277becabef68eb1a26bf8766Chris Lattner  /// tok::for.
2636a170eb3ea6d6319277becabef68eb1a26bf8766Chris Lattner  ///
2646a170eb3ea6d6319277becabef68eb1a26bf8766Chris Lattner  /// This method is very tied to the definition of HandleIdentifier.  Any
2656a170eb3ea6d6319277becabef68eb1a26bf8766Chris Lattner  /// change to it should be reflected here.
2666a170eb3ea6d6319277becabef68eb1a26bf8766Chris Lattner  void RecomputeNeedsHandleIdentifier() {
2676a170eb3ea6d6319277becabef68eb1a26bf8766Chris Lattner    NeedsHandleIdentifier =
2686a170eb3ea6d6319277becabef68eb1a26bf8766Chris Lattner      (isPoisoned() | hasMacroDefinition() | isCPlusPlusOperatorKeyword() |
26998d86b98b3fd0bd9c546123b16fd9995509aaae1Richard Smith       isExtensionToken() | isCXX11CompatKeyword() ||
27098d86b98b3fd0bd9c546123b16fd9995509aaae1Richard Smith       (getTokenID() == tok::kw___import_module__));
2716a170eb3ea6d6319277becabef68eb1a26bf8766Chris Lattner  }
2725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer};
2735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
27428bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley/// \brief an RAII object for [un]poisoning an identifier
27528bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley/// within a certain scope. II is allowed to be null, in
27628bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley/// which case, objects of this type have no effect.
27728bbe4b8acc338476fe0825769b41fb32b423c72John Wiegleyclass PoisonIdentifierRAIIObject {
27828bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  IdentifierInfo *const II;
27928bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  const bool OldValue;
28028bbe4b8acc338476fe0825769b41fb32b423c72John Wiegleypublic:
28128bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  PoisonIdentifierRAIIObject(IdentifierInfo *II, bool NewValue)
28228bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley    : II(II), OldValue(II ? II->isPoisoned() : false) {
28328bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley    if(II)
28428bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley      II->setIsPoisoned(NewValue);
28528bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  }
28628bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley
28728bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  ~PoisonIdentifierRAIIObject() {
28828bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley    if(II)
28928bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley      II->setIsPoisoned(OldValue);
29028bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley  }
29128bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley};
29228bbe4b8acc338476fe0825769b41fb32b423c72John Wiegley
29395f4292cc526c629fead321c7fcfd4fe0f3bc66eDouglas Gregor/// \brief An iterator that walks over all of the known identifiers
29495f4292cc526c629fead321c7fcfd4fe0f3bc66eDouglas Gregor/// in the lookup table.
29595f4292cc526c629fead321c7fcfd4fe0f3bc66eDouglas Gregor///
29695f4292cc526c629fead321c7fcfd4fe0f3bc66eDouglas Gregor/// Since this iterator uses an abstract interface via virtual
29795f4292cc526c629fead321c7fcfd4fe0f3bc66eDouglas Gregor/// functions, it uses an object-oriented interface rather than the
29895f4292cc526c629fead321c7fcfd4fe0f3bc66eDouglas Gregor/// more standard C++ STL iterator interface. In this OO-style
29995f4292cc526c629fead321c7fcfd4fe0f3bc66eDouglas Gregor/// iteration, the single function \c Next() provides dereference,
30095f4292cc526c629fead321c7fcfd4fe0f3bc66eDouglas Gregor/// advance, and end-of-sequence checking in a single
30195f4292cc526c629fead321c7fcfd4fe0f3bc66eDouglas Gregor/// operation. Subclasses of this iterator type will provide the
30295f4292cc526c629fead321c7fcfd4fe0f3bc66eDouglas Gregor/// actual functionality.
30395f4292cc526c629fead321c7fcfd4fe0f3bc66eDouglas Gregorclass IdentifierIterator {
30495f4292cc526c629fead321c7fcfd4fe0f3bc66eDouglas Gregorprivate:
30595f4292cc526c629fead321c7fcfd4fe0f3bc66eDouglas Gregor  IdentifierIterator(const IdentifierIterator&); // Do not implement
30695f4292cc526c629fead321c7fcfd4fe0f3bc66eDouglas Gregor  IdentifierIterator &operator=(const IdentifierIterator&); // Do not implement
30795f4292cc526c629fead321c7fcfd4fe0f3bc66eDouglas Gregor
30895f4292cc526c629fead321c7fcfd4fe0f3bc66eDouglas Gregorprotected:
30995f4292cc526c629fead321c7fcfd4fe0f3bc66eDouglas Gregor  IdentifierIterator() { }
31095f4292cc526c629fead321c7fcfd4fe0f3bc66eDouglas Gregor
31195f4292cc526c629fead321c7fcfd4fe0f3bc66eDouglas Gregorpublic:
31295f4292cc526c629fead321c7fcfd4fe0f3bc66eDouglas Gregor  virtual ~IdentifierIterator();
31395f4292cc526c629fead321c7fcfd4fe0f3bc66eDouglas Gregor
31495f4292cc526c629fead321c7fcfd4fe0f3bc66eDouglas Gregor  /// \brief Retrieve the next string in the identifier table and
31595f4292cc526c629fead321c7fcfd4fe0f3bc66eDouglas Gregor  /// advances the iterator for the following string.
31695f4292cc526c629fead321c7fcfd4fe0f3bc66eDouglas Gregor  ///
31795f4292cc526c629fead321c7fcfd4fe0f3bc66eDouglas Gregor  /// \returns The next string in the identifier table. If there is
318686775deca8b8685eb90801495880e3abdd844c2Chris Lattner  /// no such string, returns an empty \c StringRef.
319686775deca8b8685eb90801495880e3abdd844c2Chris Lattner  virtual StringRef Next() = 0;
32095f4292cc526c629fead321c7fcfd4fe0f3bc66eDouglas Gregor};
32195f4292cc526c629fead321c7fcfd4fe0f3bc66eDouglas Gregor
32272b1b15ee88aac0a63e2c1dc53fe22f5ab297b20Ted Kremenek/// IdentifierInfoLookup - An abstract class used by IdentifierTable that
323668c1a4fdcc56bdd050256b1688e116fe84b72dbDouglas Gregor///  provides an interface for performing lookups from strings
32472b1b15ee88aac0a63e2c1dc53fe22f5ab297b20Ted Kremenek/// (const char *) to IdentiferInfo objects.
32572b1b15ee88aac0a63e2c1dc53fe22f5ab297b20Ted Kremenekclass IdentifierInfoLookup {
32672b1b15ee88aac0a63e2c1dc53fe22f5ab297b20Ted Kremenekpublic:
32772b1b15ee88aac0a63e2c1dc53fe22f5ab297b20Ted Kremenek  virtual ~IdentifierInfoLookup();
3281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
32972b1b15ee88aac0a63e2c1dc53fe22f5ab297b20Ted Kremenek  /// get - Return the identifier token info for the specified named identifier.
33072b1b15ee88aac0a63e2c1dc53fe22f5ab297b20Ted Kremenek  ///  Unlike the version in IdentifierTable, this returns a pointer instead
33172b1b15ee88aac0a63e2c1dc53fe22f5ab297b20Ted Kremenek  ///  of a reference.  If the pointer is NULL then the IdentifierInfo cannot
33272b1b15ee88aac0a63e2c1dc53fe22f5ab297b20Ted Kremenek  ///  be found.
333686775deca8b8685eb90801495880e3abdd844c2Chris Lattner  virtual IdentifierInfo* get(StringRef Name) = 0;
33495f4292cc526c629fead321c7fcfd4fe0f3bc66eDouglas Gregor
33595f4292cc526c629fead321c7fcfd4fe0f3bc66eDouglas Gregor  /// \brief Retrieve an iterator into the set of all identifiers
33695f4292cc526c629fead321c7fcfd4fe0f3bc66eDouglas Gregor  /// known to this identifier lookup source.
33795f4292cc526c629fead321c7fcfd4fe0f3bc66eDouglas Gregor  ///
33895f4292cc526c629fead321c7fcfd4fe0f3bc66eDouglas Gregor  /// This routine provides access to all of the identifiers known to
33995f4292cc526c629fead321c7fcfd4fe0f3bc66eDouglas Gregor  /// the identifier lookup, allowing access to the contents of the
34095f4292cc526c629fead321c7fcfd4fe0f3bc66eDouglas Gregor  /// identifiers without introducing the overhead of constructing
34195f4292cc526c629fead321c7fcfd4fe0f3bc66eDouglas Gregor  /// IdentifierInfo objects for each.
34295f4292cc526c629fead321c7fcfd4fe0f3bc66eDouglas Gregor  ///
34395f4292cc526c629fead321c7fcfd4fe0f3bc66eDouglas Gregor  /// \returns A new iterator into the set of known identifiers. The
34495f4292cc526c629fead321c7fcfd4fe0f3bc66eDouglas Gregor  /// caller is responsible for deleting this iterator.
34595f4292cc526c629fead321c7fcfd4fe0f3bc66eDouglas Gregor  virtual IdentifierIterator *getIdentifiers() const;
3461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump};
3478c5a760b82e73ed90b560090772db97e2ae27b09Douglas Gregor
3488c5a760b82e73ed90b560090772db97e2ae27b09Douglas Gregor/// \brief An abstract class used to resolve numerical identifier
3498c5a760b82e73ed90b560090772db97e2ae27b09Douglas Gregor/// references (meaningful only to some external source) into
3508c5a760b82e73ed90b560090772db97e2ae27b09Douglas Gregor/// IdentifierInfo pointers.
3518c5a760b82e73ed90b560090772db97e2ae27b09Douglas Gregorclass ExternalIdentifierLookup {
3528c5a760b82e73ed90b560090772db97e2ae27b09Douglas Gregorpublic:
3538c5a760b82e73ed90b560090772db97e2ae27b09Douglas Gregor  virtual ~ExternalIdentifierLookup();
3548c5a760b82e73ed90b560090772db97e2ae27b09Douglas Gregor
3558c5a760b82e73ed90b560090772db97e2ae27b09Douglas Gregor  /// \brief Return the identifier associated with the given ID number.
3568c5a760b82e73ed90b560090772db97e2ae27b09Douglas Gregor  ///
3578c5a760b82e73ed90b560090772db97e2ae27b09Douglas Gregor  /// The ID 0 is associated with the NULL identifier.
3588c5a760b82e73ed90b560090772db97e2ae27b09Douglas Gregor  virtual IdentifierInfo *GetIdentifier(unsigned ID) = 0;
3598c5a760b82e73ed90b560090772db97e2ae27b09Douglas Gregor};
3608c5a760b82e73ed90b560090772db97e2ae27b09Douglas Gregor
3615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// IdentifierTable - This table implements an efficient mapping from strings to
3625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// IdentifierInfo nodes.  It has no other purpose, but this is an
363fc8f0e14ad142ed811e90fbd9a30e419e301c717Chris Lattner/// extremely performance-critical piece of the code, as each occurrence of
3645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// every identifier goes through here when lexed.
3655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerclass IdentifierTable {
3665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Shark shows that using MallocAllocator is *much* slower than using this
3675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // BumpPtrAllocator!
368ea9c26b3dbd74a1497f5609ae6e19a85f42b6073Ted Kremenek  typedef llvm::StringMap<IdentifierInfo*, llvm::BumpPtrAllocator> HashTableTy;
3695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  HashTableTy HashTable;
3701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
37172b1b15ee88aac0a63e2c1dc53fe22f5ab297b20Ted Kremenek  IdentifierInfoLookup* ExternalLookup;
3721cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor
3735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerpublic:
3745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// IdentifierTable ctor - Create the identifier table, populating it with
3755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// info about the language keywords for the language specified by LangOpts.
37672b1b15ee88aac0a63e2c1dc53fe22f5ab297b20Ted Kremenek  IdentifierTable(const LangOptions &LangOpts,
37772b1b15ee88aac0a63e2c1dc53fe22f5ab297b20Ted Kremenek                  IdentifierInfoLookup* externalLookup = 0);
3781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
379668c1a4fdcc56bdd050256b1688e116fe84b72dbDouglas Gregor  /// \brief Set the external identifier lookup mechanism.
380668c1a4fdcc56bdd050256b1688e116fe84b72dbDouglas Gregor  void setExternalIdentifierLookup(IdentifierInfoLookup *IILookup) {
381668c1a4fdcc56bdd050256b1688e116fe84b72dbDouglas Gregor    ExternalLookup = IILookup;
382668c1a4fdcc56bdd050256b1688e116fe84b72dbDouglas Gregor  }
383668c1a4fdcc56bdd050256b1688e116fe84b72dbDouglas Gregor
38495f4292cc526c629fead321c7fcfd4fe0f3bc66eDouglas Gregor  /// \brief Retrieve the external identifier lookup object, if any.
38595f4292cc526c629fead321c7fcfd4fe0f3bc66eDouglas Gregor  IdentifierInfoLookup *getExternalIdentifierLookup() const {
38695f4292cc526c629fead321c7fcfd4fe0f3bc66eDouglas Gregor    return ExternalLookup;
38795f4292cc526c629fead321c7fcfd4fe0f3bc66eDouglas Gregor  }
38895f4292cc526c629fead321c7fcfd4fe0f3bc66eDouglas Gregor
38972b1b15ee88aac0a63e2c1dc53fe22f5ab297b20Ted Kremenek  llvm::BumpPtrAllocator& getAllocator() {
39072b1b15ee88aac0a63e2c1dc53fe22f5ab297b20Ted Kremenek    return HashTable.getAllocator();
39172b1b15ee88aac0a63e2c1dc53fe22f5ab297b20Ted Kremenek  }
3921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// get - Return the identifier token info for the specified named identifier.
3945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  ///
395686775deca8b8685eb90801495880e3abdd844c2Chris Lattner  IdentifierInfo &get(StringRef Name) {
396ea9c26b3dbd74a1497f5609ae6e19a85f42b6073Ted Kremenek    llvm::StringMapEntry<IdentifierInfo*> &Entry =
3976488292f5e204fed99bb43ab23b8342ddc03ce89Kovarththanan Rajaratnam      HashTable.GetOrCreateValue(Name);
3981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
399ea9c26b3dbd74a1497f5609ae6e19a85f42b6073Ted Kremenek    IdentifierInfo *II = Entry.getValue();
400d43b333be82438102ff4c459b1fb5dfb764e5f0dDaniel Dunbar    if (II) return *II;
4011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
402d43b333be82438102ff4c459b1fb5dfb764e5f0dDaniel Dunbar    // No entry; if we have an external lookup, look there first.
403d43b333be82438102ff4c459b1fb5dfb764e5f0dDaniel Dunbar    if (ExternalLookup) {
4046488292f5e204fed99bb43ab23b8342ddc03ce89Kovarththanan Rajaratnam      II = ExternalLookup->get(Name);
405d43b333be82438102ff4c459b1fb5dfb764e5f0dDaniel Dunbar      if (II) {
406d43b333be82438102ff4c459b1fb5dfb764e5f0dDaniel Dunbar        // Cache in the StringMap for subsequent lookups.
407d43b333be82438102ff4c459b1fb5dfb764e5f0dDaniel Dunbar        Entry.setValue(II);
408d43b333be82438102ff4c459b1fb5dfb764e5f0dDaniel Dunbar        return *II;
409ea9c26b3dbd74a1497f5609ae6e19a85f42b6073Ted Kremenek      }
410d42ffbd22fc7eb61321f6a88173ee424991f01c6Ted Kremenek    }
411ccb9bac3adb35a2dc78c1737e7b2dc6537a16393Daniel Dunbar
412d43b333be82438102ff4c459b1fb5dfb764e5f0dDaniel Dunbar    // Lookups failed, make a new IdentifierInfo.
413d43b333be82438102ff4c459b1fb5dfb764e5f0dDaniel Dunbar    void *Mem = getAllocator().Allocate<IdentifierInfo>();
414d43b333be82438102ff4c459b1fb5dfb764e5f0dDaniel Dunbar    II = new (Mem) IdentifierInfo();
415d43b333be82438102ff4c459b1fb5dfb764e5f0dDaniel Dunbar    Entry.setValue(II);
416d43b333be82438102ff4c459b1fb5dfb764e5f0dDaniel Dunbar
417d43b333be82438102ff4c459b1fb5dfb764e5f0dDaniel Dunbar    // Make sure getName() knows how to find the IdentifierInfo
418d43b333be82438102ff4c459b1fb5dfb764e5f0dDaniel Dunbar    // contents.
419d43b333be82438102ff4c459b1fb5dfb764e5f0dDaniel Dunbar    II->Entry = &Entry;
420d43b333be82438102ff4c459b1fb5dfb764e5f0dDaniel Dunbar
421ea9c26b3dbd74a1497f5609ae6e19a85f42b6073Ted Kremenek    return *II;
4225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
4231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
424686775deca8b8685eb90801495880e3abdd844c2Chris Lattner  IdentifierInfo &get(StringRef Name, tok::TokenKind TokenCode) {
425646395bbcaa849c94bc2a3246c71d809ca719f01Argyrios Kyrtzidis    IdentifierInfo &II = get(Name);
426646395bbcaa849c94bc2a3246c71d809ca719f01Argyrios Kyrtzidis    II.TokenID = TokenCode;
427d648d373d6c14dccadd3bef7b560f6a7296f949ePeter Collingbourne    assert(II.TokenID == (unsigned) TokenCode && "TokenCode too large");
428646395bbcaa849c94bc2a3246c71d809ca719f01Argyrios Kyrtzidis    return II;
429646395bbcaa849c94bc2a3246c71d809ca719f01Argyrios Kyrtzidis  }
430646395bbcaa849c94bc2a3246c71d809ca719f01Argyrios Kyrtzidis
431ffaab3e2bb13991bb3357e80f14bcae3745b2347Sebastian Redl  /// \brief Gets an IdentifierInfo for the given name without consulting
432ffaab3e2bb13991bb3357e80f14bcae3745b2347Sebastian Redl  ///        external sources.
4335f8e3302242cf94de2f8e46d10167f57fcf747c3Douglas Gregor  ///
434ffaab3e2bb13991bb3357e80f14bcae3745b2347Sebastian Redl  /// This is a version of get() meant for external sources that want to
435ffaab3e2bb13991bb3357e80f14bcae3745b2347Sebastian Redl  /// introduce or modify an identifier. If they called get(), they would
436ffaab3e2bb13991bb3357e80f14bcae3745b2347Sebastian Redl  /// likely end up in a recursion.
437686775deca8b8685eb90801495880e3abdd844c2Chris Lattner  IdentifierInfo &getOwn(StringRef Name) {
4385f8e3302242cf94de2f8e46d10167f57fcf747c3Douglas Gregor    llvm::StringMapEntry<IdentifierInfo*> &Entry =
43965aa6885818d4b4eea2e5a9d12085b2398148662Jay Foad      HashTable.GetOrCreateValue(Name);
4401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4415f8e3302242cf94de2f8e46d10167f57fcf747c3Douglas Gregor    IdentifierInfo *II = Entry.getValue();
442ffaab3e2bb13991bb3357e80f14bcae3745b2347Sebastian Redl    if (!II) {
4431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
444ffaab3e2bb13991bb3357e80f14bcae3745b2347Sebastian Redl      // Lookups failed, make a new IdentifierInfo.
445ffaab3e2bb13991bb3357e80f14bcae3745b2347Sebastian Redl      void *Mem = getAllocator().Allocate<IdentifierInfo>();
446ffaab3e2bb13991bb3357e80f14bcae3745b2347Sebastian Redl      II = new (Mem) IdentifierInfo();
447ffaab3e2bb13991bb3357e80f14bcae3745b2347Sebastian Redl      Entry.setValue(II);
4485f8e3302242cf94de2f8e46d10167f57fcf747c3Douglas Gregor
449ffaab3e2bb13991bb3357e80f14bcae3745b2347Sebastian Redl      // Make sure getName() knows how to find the IdentifierInfo
450ffaab3e2bb13991bb3357e80f14bcae3745b2347Sebastian Redl      // contents.
451ffaab3e2bb13991bb3357e80f14bcae3745b2347Sebastian Redl      II->Entry = &Entry;
452ffaab3e2bb13991bb3357e80f14bcae3745b2347Sebastian Redl    }
4535f8e3302242cf94de2f8e46d10167f57fcf747c3Douglas Gregor
4545f8e3302242cf94de2f8e46d10167f57fcf747c3Douglas Gregor    return *II;
4555f8e3302242cf94de2f8e46d10167f57fcf747c3Douglas Gregor  }
4565f8e3302242cf94de2f8e46d10167f57fcf747c3Douglas Gregor
4575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  typedef HashTableTy::const_iterator iterator;
4585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  typedef HashTableTy::const_iterator const_iterator;
4591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  iterator begin() const { return HashTable.begin(); }
4615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  iterator end() const   { return HashTable.end(); }
462c637e6b7afeebc6b4f751e4373715b6a8ea77272Ted Kremenek  unsigned size() const { return HashTable.size(); }
4631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// PrintStats - Print some statistics to stderr that indicate how well the
4655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// hashing is doing.
4665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void PrintStats() const;
4671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void AddKeywords(const LangOptions &LangOpts);
4695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer};
4705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
47185f3d76c0ecfdefcf83ea44a57b7a16119c8a045John McCall/// ObjCMethodFamily - A family of Objective-C methods.  These
47285f3d76c0ecfdefcf83ea44a57b7a16119c8a045John McCall/// families have no inherent meaning in the language, but are
47385f3d76c0ecfdefcf83ea44a57b7a16119c8a045John McCall/// nonetheless central enough in the existing implementations to
47485f3d76c0ecfdefcf83ea44a57b7a16119c8a045John McCall/// merit direct AST support.  While, in theory, arbitrary methods can
47585f3d76c0ecfdefcf83ea44a57b7a16119c8a045John McCall/// be considered to form families, we focus here on the methods
47685f3d76c0ecfdefcf83ea44a57b7a16119c8a045John McCall/// involving allocation and retain-count management, as these are the
47785f3d76c0ecfdefcf83ea44a57b7a16119c8a045John McCall/// most "core" and the most likely to be useful to diverse clients
47885f3d76c0ecfdefcf83ea44a57b7a16119c8a045John McCall/// without extra information.
47985f3d76c0ecfdefcf83ea44a57b7a16119c8a045John McCall///
48085f3d76c0ecfdefcf83ea44a57b7a16119c8a045John McCall/// Both selectors and actual method declarations may be classified
48185f3d76c0ecfdefcf83ea44a57b7a16119c8a045John McCall/// into families.  Method families may impose additional restrictions
48285f3d76c0ecfdefcf83ea44a57b7a16119c8a045John McCall/// beyond their selector name; for example, a method called '_init'
48385f3d76c0ecfdefcf83ea44a57b7a16119c8a045John McCall/// that returns void is not considered to be in the 'init' family
48485f3d76c0ecfdefcf83ea44a57b7a16119c8a045John McCall/// (but would be if it returned 'id').  It is also possible to
48585f3d76c0ecfdefcf83ea44a57b7a16119c8a045John McCall/// explicitly change or remove a method's family.  Therefore the
48685f3d76c0ecfdefcf83ea44a57b7a16119c8a045John McCall/// method's family should be considered the single source of truth.
48785f3d76c0ecfdefcf83ea44a57b7a16119c8a045John McCallenum ObjCMethodFamily {
48885f3d76c0ecfdefcf83ea44a57b7a16119c8a045John McCall  /// \brief No particular method family.
48985f3d76c0ecfdefcf83ea44a57b7a16119c8a045John McCall  OMF_None,
49085f3d76c0ecfdefcf83ea44a57b7a16119c8a045John McCall
49185f3d76c0ecfdefcf83ea44a57b7a16119c8a045John McCall  // Selectors in these families may have arbitrary arity, may be
49285f3d76c0ecfdefcf83ea44a57b7a16119c8a045John McCall  // written with arbitrary leading underscores, and may have
49385f3d76c0ecfdefcf83ea44a57b7a16119c8a045John McCall  // additional CamelCase "words" in their first selector chunk
49485f3d76c0ecfdefcf83ea44a57b7a16119c8a045John McCall  // following the family name.
49585f3d76c0ecfdefcf83ea44a57b7a16119c8a045John McCall  OMF_alloc,
49685f3d76c0ecfdefcf83ea44a57b7a16119c8a045John McCall  OMF_copy,
49785f3d76c0ecfdefcf83ea44a57b7a16119c8a045John McCall  OMF_init,
49885f3d76c0ecfdefcf83ea44a57b7a16119c8a045John McCall  OMF_mutableCopy,
49985f3d76c0ecfdefcf83ea44a57b7a16119c8a045John McCall  OMF_new,
50085f3d76c0ecfdefcf83ea44a57b7a16119c8a045John McCall
50185f3d76c0ecfdefcf83ea44a57b7a16119c8a045John McCall  // These families are singletons consisting only of the nullary
50285f3d76c0ecfdefcf83ea44a57b7a16119c8a045John McCall  // selector with the given name.
50385f3d76c0ecfdefcf83ea44a57b7a16119c8a045John McCall  OMF_autorelease,
50485f3d76c0ecfdefcf83ea44a57b7a16119c8a045John McCall  OMF_dealloc,
50580cb6e69d9e85231588ae604e4bc2bc9a07389afNico Weber  OMF_finalize,
50685f3d76c0ecfdefcf83ea44a57b7a16119c8a045John McCall  OMF_release,
50785f3d76c0ecfdefcf83ea44a57b7a16119c8a045John McCall  OMF_retain,
508926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor  OMF_retainCount,
5099670e179a67d868e171feac44fb8f9e2f108c5e8Fariborz Jahanian  OMF_self,
5109670e179a67d868e171feac44fb8f9e2f108c5e8Fariborz Jahanian
5119670e179a67d868e171feac44fb8f9e2f108c5e8Fariborz Jahanian  // performSelector families
5129670e179a67d868e171feac44fb8f9e2f108c5e8Fariborz Jahanian  OMF_performSelector
51385f3d76c0ecfdefcf83ea44a57b7a16119c8a045John McCall};
51485f3d76c0ecfdefcf83ea44a57b7a16119c8a045John McCall
51585f3d76c0ecfdefcf83ea44a57b7a16119c8a045John McCall/// Enough bits to store any enumerator in ObjCMethodFamily or
51685f3d76c0ecfdefcf83ea44a57b7a16119c8a045John McCall/// InvalidObjCMethodFamily.
51785f3d76c0ecfdefcf83ea44a57b7a16119c8a045John McCallenum { ObjCMethodFamilyBitWidth = 4 };
51885f3d76c0ecfdefcf83ea44a57b7a16119c8a045John McCall
51985f3d76c0ecfdefcf83ea44a57b7a16119c8a045John McCall/// An invalid value of ObjCMethodFamily.
52085f3d76c0ecfdefcf83ea44a57b7a16119c8a045John McCallenum { InvalidObjCMethodFamily = (1 << ObjCMethodFamilyBitWidth) - 1 };
52185f3d76c0ecfdefcf83ea44a57b7a16119c8a045John McCall
52229238a0bf7cbf5b396efb451a0adb5fe4aa037caSteve Naroff/// Selector - This smart pointer class efficiently represents Objective-C
52329238a0bf7cbf5b396efb451a0adb5fe4aa037caSteve Naroff/// method names. This class will either point to an IdentifierInfo or a
52429238a0bf7cbf5b396efb451a0adb5fe4aa037caSteve Naroff/// MultiKeywordSelector (which is private). This enables us to optimize
5251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// selectors that take no arguments and selectors that take 1 argument, which
52629238a0bf7cbf5b396efb451a0adb5fe4aa037caSteve Naroff/// accounts for 78% of all selectors in Cocoa.h.
527bcfb06ac6da1aa3c74ac1ef7a49c2807522366e7Steve Naroffclass Selector {
52840847cfb58acc3cac7d68727df9455ac45f2e118David Blaikie  friend class Diagnostic;
5291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
530bcfb06ac6da1aa3c74ac1ef7a49c2807522366e7Steve Naroff  enum IdentifierInfoFlag {
5310e2dfd3963465d238e791eb8996e4d49fa47b615Chris Lattner    // MultiKeywordSelector = 0.
532bcfb06ac6da1aa3c74ac1ef7a49c2807522366e7Steve Naroff    ZeroArg  = 0x1,
533bcfb06ac6da1aa3c74ac1ef7a49c2807522366e7Steve Naroff    OneArg   = 0x2,
534bcfb06ac6da1aa3c74ac1ef7a49c2807522366e7Steve Naroff    ArgFlags = ZeroArg|OneArg
535bcfb06ac6da1aa3c74ac1ef7a49c2807522366e7Steve Naroff  };
536bcfb06ac6da1aa3c74ac1ef7a49c2807522366e7Steve Naroff  uintptr_t InfoPtr; // a pointer to the MultiKeywordSelector or IdentifierInfo.
5371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
538bcfb06ac6da1aa3c74ac1ef7a49c2807522366e7Steve Naroff  Selector(IdentifierInfo *II, unsigned nArgs) {
539bcfb06ac6da1aa3c74ac1ef7a49c2807522366e7Steve Naroff    InfoPtr = reinterpret_cast<uintptr_t>(II);
5400e2dfd3963465d238e791eb8996e4d49fa47b615Chris Lattner    assert((InfoPtr & ArgFlags) == 0 &&"Insufficiently aligned IdentifierInfo");
5410e2dfd3963465d238e791eb8996e4d49fa47b615Chris Lattner    assert(nArgs < 2 && "nArgs not equal to 0/1");
5420e2dfd3963465d238e791eb8996e4d49fa47b615Chris Lattner    InfoPtr |= nArgs+1;
543bcfb06ac6da1aa3c74ac1ef7a49c2807522366e7Steve Naroff  }
544bcfb06ac6da1aa3c74ac1ef7a49c2807522366e7Steve Naroff  Selector(MultiKeywordSelector *SI) {
545bcfb06ac6da1aa3c74ac1ef7a49c2807522366e7Steve Naroff    InfoPtr = reinterpret_cast<uintptr_t>(SI);
5460e2dfd3963465d238e791eb8996e4d49fa47b615Chris Lattner    assert((InfoPtr & ArgFlags) == 0 &&"Insufficiently aligned IdentifierInfo");
547bcfb06ac6da1aa3c74ac1ef7a49c2807522366e7Steve Naroff  }
5481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
549bcfb06ac6da1aa3c74ac1ef7a49c2807522366e7Steve Naroff  IdentifierInfo *getAsIdentifierInfo() const {
5500e2dfd3963465d238e791eb8996e4d49fa47b615Chris Lattner    if (getIdentifierInfoFlag())
551bcfb06ac6da1aa3c74ac1ef7a49c2807522366e7Steve Naroff      return reinterpret_cast<IdentifierInfo *>(InfoPtr & ~ArgFlags);
552bcfb06ac6da1aa3c74ac1ef7a49c2807522366e7Steve Naroff    return 0;
553bcfb06ac6da1aa3c74ac1ef7a49c2807522366e7Steve Naroff  }
554bcfb06ac6da1aa3c74ac1ef7a49c2807522366e7Steve Naroff  unsigned getIdentifierInfoFlag() const {
555bcfb06ac6da1aa3c74ac1ef7a49c2807522366e7Steve Naroff    return InfoPtr & ArgFlags;
556bcfb06ac6da1aa3c74ac1ef7a49c2807522366e7Steve Naroff  }
5578af2c16571f3aade6d47ce81fa3857d01d375719Ted Kremenek
55885f3d76c0ecfdefcf83ea44a57b7a16119c8a045John McCall  static ObjCMethodFamily getMethodFamilyImpl(Selector sel);
55985f3d76c0ecfdefcf83ea44a57b7a16119c8a045John McCall
5608af2c16571f3aade6d47ce81fa3857d01d375719Ted Kremenekpublic:
5618af2c16571f3aade6d47ce81fa3857d01d375719Ted Kremenek  friend class SelectorTable; // only the SelectorTable can create these
5628af2c16571f3aade6d47ce81fa3857d01d375719Ted Kremenek  friend class DeclarationName; // and the AST's DeclarationName.
5638af2c16571f3aade6d47ce81fa3857d01d375719Ted Kremenek
5648af2c16571f3aade6d47ce81fa3857d01d375719Ted Kremenek  /// The default ctor should only be used when creating data structures that
5658af2c16571f3aade6d47ce81fa3857d01d375719Ted Kremenek  ///  will contain selectors.
5668af2c16571f3aade6d47ce81fa3857d01d375719Ted Kremenek  Selector() : InfoPtr(0) {}
56790cd1bb1baac2a0221f3642de0cbea3244b116e5Steve Naroff  Selector(uintptr_t V) : InfoPtr(V) {}
5688af2c16571f3aade6d47ce81fa3857d01d375719Ted Kremenek
569bcfb06ac6da1aa3c74ac1ef7a49c2807522366e7Steve Naroff  /// operator==/!= - Indicate whether the specified selectors are identical.
57097b7f26a92d87e514530a5b652460190ce48c974Ted Kremenek  bool operator==(Selector RHS) const {
571bcfb06ac6da1aa3c74ac1ef7a49c2807522366e7Steve Naroff    return InfoPtr == RHS.InfoPtr;
572bcfb06ac6da1aa3c74ac1ef7a49c2807522366e7Steve Naroff  }
57397b7f26a92d87e514530a5b652460190ce48c974Ted Kremenek  bool operator!=(Selector RHS) const {
574bcfb06ac6da1aa3c74ac1ef7a49c2807522366e7Steve Naroff    return InfoPtr != RHS.InfoPtr;
575bcfb06ac6da1aa3c74ac1ef7a49c2807522366e7Steve Naroff  }
576bcfb06ac6da1aa3c74ac1ef7a49c2807522366e7Steve Naroff  void *getAsOpaquePtr() const {
577bcfb06ac6da1aa3c74ac1ef7a49c2807522366e7Steve Naroff    return reinterpret_cast<void*>(InfoPtr);
578bcfb06ac6da1aa3c74ac1ef7a49c2807522366e7Steve Naroff  }
579405bad07391494d2eb025f8222c256c66b56e5f8Douglas Gregor
580405bad07391494d2eb025f8222c256c66b56e5f8Douglas Gregor  /// \brief Determine whether this is the empty selector.
581405bad07391494d2eb025f8222c256c66b56e5f8Douglas Gregor  bool isNull() const { return InfoPtr == 0; }
582405bad07391494d2eb025f8222c256c66b56e5f8Douglas Gregor
583bcfb06ac6da1aa3c74ac1ef7a49c2807522366e7Steve Naroff  // Predicates to identify the selector type.
5841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  bool isKeywordSelector() const {
5851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    return getIdentifierInfoFlag() != ZeroArg;
586bcfb06ac6da1aa3c74ac1ef7a49c2807522366e7Steve Naroff  }
5871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  bool isUnarySelector() const {
588bcfb06ac6da1aa3c74ac1ef7a49c2807522366e7Steve Naroff    return getIdentifierInfoFlag() == ZeroArg;
589bcfb06ac6da1aa3c74ac1ef7a49c2807522366e7Steve Naroff  }
5905b6b72f53ad164497cf62484b60cdbb4361f1978Steve Naroff  unsigned getNumArgs() const;
591813d8346529bf094eb2b249648906ba7fd226688Douglas Gregor
592813d8346529bf094eb2b249648906ba7fd226688Douglas Gregor
593813d8346529bf094eb2b249648906ba7fd226688Douglas Gregor  /// \brief Retrieve the identifier at a given position in the selector.
594813d8346529bf094eb2b249648906ba7fd226688Douglas Gregor  ///
595813d8346529bf094eb2b249648906ba7fd226688Douglas Gregor  /// Note that the identifier pointer returned may be NULL. Clients that only
596813d8346529bf094eb2b249648906ba7fd226688Douglas Gregor  /// care about the text of the identifier string, and not the specific,
597813d8346529bf094eb2b249648906ba7fd226688Douglas Gregor  /// uniqued identifier pointer, should use \c getNameForSlot(), which returns
598813d8346529bf094eb2b249648906ba7fd226688Douglas Gregor  /// an empty string when the identifier pointer would be NULL.
599813d8346529bf094eb2b249648906ba7fd226688Douglas Gregor  ///
600813d8346529bf094eb2b249648906ba7fd226688Douglas Gregor  /// \param argIndex The index for which we want to retrieve the identifier.
601813d8346529bf094eb2b249648906ba7fd226688Douglas Gregor  /// This index shall be less than \c getNumArgs() unless this is a keyword
602813d8346529bf094eb2b249648906ba7fd226688Douglas Gregor  /// selector, in which case 0 is the only permissible value.
603813d8346529bf094eb2b249648906ba7fd226688Douglas Gregor  ///
604813d8346529bf094eb2b249648906ba7fd226688Douglas Gregor  /// \returns the uniqued identifier for this slot, or NULL if this slot has
605813d8346529bf094eb2b249648906ba7fd226688Douglas Gregor  /// no corresponding identifier.
606f836e3fea2c77fdbb18170fb313ee0d45551320bChris Lattner  IdentifierInfo *getIdentifierInfoForSlot(unsigned argIndex) const;
607813d8346529bf094eb2b249648906ba7fd226688Douglas Gregor
608813d8346529bf094eb2b249648906ba7fd226688Douglas Gregor  /// \brief Retrieve the name at a given position in the selector.
609813d8346529bf094eb2b249648906ba7fd226688Douglas Gregor  ///
610813d8346529bf094eb2b249648906ba7fd226688Douglas Gregor  /// \param argIndex The index for which we want to retrieve the name.
611813d8346529bf094eb2b249648906ba7fd226688Douglas Gregor  /// This index shall be less than \c getNumArgs() unless this is a keyword
612813d8346529bf094eb2b249648906ba7fd226688Douglas Gregor  /// selector, in which case 0 is the only permissible value.
613813d8346529bf094eb2b249648906ba7fd226688Douglas Gregor  ///
614813d8346529bf094eb2b249648906ba7fd226688Douglas Gregor  /// \returns the name for this slot, which may be the empty string if no
615813d8346529bf094eb2b249648906ba7fd226688Douglas Gregor  /// name was supplied.
616686775deca8b8685eb90801495880e3abdd844c2Chris Lattner  StringRef getNameForSlot(unsigned argIndex) const;
617813d8346529bf094eb2b249648906ba7fd226688Douglas Gregor
618077bf5e2f48acfa9e7d69429b6e4ba86ea14896dChris Lattner  /// getAsString - Derive the full selector name (e.g. "foo:bar:") and return
619077bf5e2f48acfa9e7d69429b6e4ba86ea14896dChris Lattner  /// it as an std::string.
620077bf5e2f48acfa9e7d69429b6e4ba86ea14896dChris Lattner  std::string getAsString() const;
6211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
62285f3d76c0ecfdefcf83ea44a57b7a16119c8a045John McCall  /// getMethodFamily - Derive the conventional family of this method.
62385f3d76c0ecfdefcf83ea44a57b7a16119c8a045John McCall  ObjCMethodFamily getMethodFamily() const {
62485f3d76c0ecfdefcf83ea44a57b7a16119c8a045John McCall    return getMethodFamilyImpl(*this);
62585f3d76c0ecfdefcf83ea44a57b7a16119c8a045John McCall  }
62685f3d76c0ecfdefcf83ea44a57b7a16119c8a045John McCall
62785994260c41a54cab061a434ed378fc448333a4eChris Lattner  static Selector getEmptyMarker() {
62885994260c41a54cab061a434ed378fc448333a4eChris Lattner    return Selector(uintptr_t(-1));
62985994260c41a54cab061a434ed378fc448333a4eChris Lattner  }
63085994260c41a54cab061a434ed378fc448333a4eChris Lattner  static Selector getTombstoneMarker() {
63185994260c41a54cab061a434ed378fc448333a4eChris Lattner    return Selector(uintptr_t(-2));
63285994260c41a54cab061a434ed378fc448333a4eChris Lattner  }
633bcfb06ac6da1aa3c74ac1ef7a49c2807522366e7Steve Naroff};
634bcfb06ac6da1aa3c74ac1ef7a49c2807522366e7Steve Naroff
63529238a0bf7cbf5b396efb451a0adb5fe4aa037caSteve Naroff/// SelectorTable - This table allows us to fully hide how we implement
63629238a0bf7cbf5b396efb451a0adb5fe4aa037caSteve Naroff/// multi-keyword caching.
63729238a0bf7cbf5b396efb451a0adb5fe4aa037caSteve Naroffclass SelectorTable {
6385f7d2284c4b2f08d155732454002e68dc40c33efChris Lattner  void *Impl;  // Actually a SelectorTableImpl
63929238a0bf7cbf5b396efb451a0adb5fe4aa037caSteve Naroff  SelectorTable(const SelectorTable&); // DISABLED: DO NOT IMPLEMENT
64029238a0bf7cbf5b396efb451a0adb5fe4aa037caSteve Naroff  void operator=(const SelectorTable&); // DISABLED: DO NOT IMPLEMENT
64129238a0bf7cbf5b396efb451a0adb5fe4aa037caSteve Naroffpublic:
64229238a0bf7cbf5b396efb451a0adb5fe4aa037caSteve Naroff  SelectorTable();
64329238a0bf7cbf5b396efb451a0adb5fe4aa037caSteve Naroff  ~SelectorTable();
64429238a0bf7cbf5b396efb451a0adb5fe4aa037caSteve Naroff
645ff38491c18b060526d754765b952f4a497a89416Chris Lattner  /// getSelector - This can create any sort of selector.  NumArgs indicates
646ff38491c18b060526d754765b952f4a497a89416Chris Lattner  /// whether this is a no argument selector "foo", a single argument selector
647ff38491c18b060526d754765b952f4a497a89416Chris Lattner  /// "foo:" or multi-argument "foo:bar:".
648ff38491c18b060526d754765b952f4a497a89416Chris Lattner  Selector getSelector(unsigned NumArgs, IdentifierInfo **IIV);
6491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
650ff38491c18b060526d754765b952f4a497a89416Chris Lattner  Selector getUnarySelector(IdentifierInfo *ID) {
651ff38491c18b060526d754765b952f4a497a89416Chris Lattner    return Selector(ID, 1);
652ff38491c18b060526d754765b952f4a497a89416Chris Lattner  }
653ff38491c18b060526d754765b952f4a497a89416Chris Lattner  Selector getNullarySelector(IdentifierInfo *ID) {
654ff38491c18b060526d754765b952f4a497a89416Chris Lattner    return Selector(ID, 0);
655ff38491c18b060526d754765b952f4a497a89416Chris Lattner  }
65661f72cbd037e58f12cfe90cd442373f44092f030Steve Naroff
65797f55d6ffd548d1777d790c84b358030682f9de2Ted Kremenek  /// Return the total amount of memory allocated for managing selectors.
65897f55d6ffd548d1777d790c84b358030682f9de2Ted Kremenek  size_t getTotalMemory() const;
65997f55d6ffd548d1777d790c84b358030682f9de2Ted Kremenek
66061f72cbd037e58f12cfe90cd442373f44092f030Steve Naroff  /// constructSetterName - Return the setter name for the given
66161f72cbd037e58f12cfe90cd442373f44092f030Steve Naroff  /// identifier, i.e. "set" + Name where the initial character of Name
66261f72cbd037e58f12cfe90cd442373f44092f030Steve Naroff  /// has been capitalized.
663fdc92b7877535e6264fe43cfbdc8f01e9b224f81Steve Naroff  static Selector constructSetterName(IdentifierTable &Idents,
664fdc92b7877535e6264fe43cfbdc8f01e9b224f81Steve Naroff                                      SelectorTable &SelTable,
665fdc92b7877535e6264fe43cfbdc8f01e9b224f81Steve Naroff                                      const IdentifierInfo *Name) {
66661f72cbd037e58f12cfe90cd442373f44092f030Steve Naroff    llvm::SmallString<100> SelectorName;
66761f72cbd037e58f12cfe90cd442373f44092f030Steve Naroff    SelectorName = "set";
66801eb9b9683535d8a65c704ad2c545903409e2d36Daniel Dunbar    SelectorName += Name->getName();
66961f72cbd037e58f12cfe90cd442373f44092f030Steve Naroff    SelectorName[3] = toupper(SelectorName[3]);
6702781deb126663ca1bd412044a1e66577585987b3Kovarththanan Rajaratnam    IdentifierInfo *SetterName = &Idents.get(SelectorName);
671fdc92b7877535e6264fe43cfbdc8f01e9b224f81Steve Naroff    return SelTable.getUnarySelector(SetterName);
67261f72cbd037e58f12cfe90cd442373f44092f030Steve Naroff  }
67329238a0bf7cbf5b396efb451a0adb5fe4aa037caSteve Naroff};
67429238a0bf7cbf5b396efb451a0adb5fe4aa037caSteve Naroff
675e94ca9e4371c022329270436b3dd77adc4ddfa8fDouglas Gregor/// DeclarationNameExtra - Common base of the MultiKeywordSelector,
676e94ca9e4371c022329270436b3dd77adc4ddfa8fDouglas Gregor/// CXXSpecialName, and CXXOperatorIdName classes, all of which are
677e94ca9e4371c022329270436b3dd77adc4ddfa8fDouglas Gregor/// private classes that describe different kinds of names.
6782e1cd4264d363ca869bf37ef160902f211d21b8cDouglas Gregorclass DeclarationNameExtra {
6792e1cd4264d363ca869bf37ef160902f211d21b8cDouglas Gregorpublic:
6802e1cd4264d363ca869bf37ef160902f211d21b8cDouglas Gregor  /// ExtraKind - The kind of "extra" information stored in the
6812e1cd4264d363ca869bf37ef160902f211d21b8cDouglas Gregor  /// DeclarationName. See @c ExtraKindOrNumArgs for an explanation of
6822e1cd4264d363ca869bf37ef160902f211d21b8cDouglas Gregor  /// how these enumerator values are used.
6832e1cd4264d363ca869bf37ef160902f211d21b8cDouglas Gregor  enum ExtraKind {
6842e1cd4264d363ca869bf37ef160902f211d21b8cDouglas Gregor    CXXConstructor = 0,
6852e1cd4264d363ca869bf37ef160902f211d21b8cDouglas Gregor    CXXDestructor,
6862e1cd4264d363ca869bf37ef160902f211d21b8cDouglas Gregor    CXXConversionFunction,
687e94ca9e4371c022329270436b3dd77adc4ddfa8fDouglas Gregor#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
688e94ca9e4371c022329270436b3dd77adc4ddfa8fDouglas Gregor    CXXOperator##Name,
689e94ca9e4371c022329270436b3dd77adc4ddfa8fDouglas Gregor#include "clang/Basic/OperatorKinds.def"
6903e518bda00d710754ca077cf9be8dd821e16a854Sean Hunt    CXXLiteralOperator,
6912a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor    CXXUsingDirective,
6922e1cd4264d363ca869bf37ef160902f211d21b8cDouglas Gregor    NUM_EXTRA_KINDS
6932e1cd4264d363ca869bf37ef160902f211d21b8cDouglas Gregor  };
6945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
695e94ca9e4371c022329270436b3dd77adc4ddfa8fDouglas Gregor  /// ExtraKindOrNumArgs - Either the kind of C++ special name or
696e94ca9e4371c022329270436b3dd77adc4ddfa8fDouglas Gregor  /// operator-id (if the value is one of the CXX* enumerators of
697e94ca9e4371c022329270436b3dd77adc4ddfa8fDouglas Gregor  /// ExtraKind), in which case the DeclarationNameExtra is also a
6983e518bda00d710754ca077cf9be8dd821e16a854Sean Hunt  /// CXXSpecialName, (for CXXConstructor, CXXDestructor, or
6993e518bda00d710754ca077cf9be8dd821e16a854Sean Hunt  /// CXXConversionFunction) CXXOperatorIdName, or CXXLiteralOperatorName,
7003e518bda00d710754ca077cf9be8dd821e16a854Sean Hunt  /// it may be also name common to C++ using-directives (CXXUsingDirective),
7013e518bda00d710754ca077cf9be8dd821e16a854Sean Hunt  /// otherwise it is NUM_EXTRA_KINDS+NumArgs, where NumArgs is the number of
7022e1cd4264d363ca869bf37ef160902f211d21b8cDouglas Gregor  /// arguments in the Objective-C selector, in which case the
7032e1cd4264d363ca869bf37ef160902f211d21b8cDouglas Gregor  /// DeclarationNameExtra is also a MultiKeywordSelector.
7042e1cd4264d363ca869bf37ef160902f211d21b8cDouglas Gregor  unsigned ExtraKindOrNumArgs;
7052e1cd4264d363ca869bf37ef160902f211d21b8cDouglas Gregor};
7062e1cd4264d363ca869bf37ef160902f211d21b8cDouglas Gregor
7072e1cd4264d363ca869bf37ef160902f211d21b8cDouglas Gregor}  // end namespace clang
7084365a7e46822700357a272d839ee2656d9603d5aChris Lattner
7092e1cd4264d363ca869bf37ef160902f211d21b8cDouglas Gregornamespace llvm {
7104365a7e46822700357a272d839ee2656d9603d5aChris Lattner/// Define DenseMapInfo so that Selectors can be used as keys in DenseMap and
7114365a7e46822700357a272d839ee2656d9603d5aChris Lattner/// DenseSets.
71285994260c41a54cab061a434ed378fc448333a4eChris Lattnertemplate <>
71385994260c41a54cab061a434ed378fc448333a4eChris Lattnerstruct DenseMapInfo<clang::Selector> {
71485994260c41a54cab061a434ed378fc448333a4eChris Lattner  static inline clang::Selector getEmptyKey() {
71585994260c41a54cab061a434ed378fc448333a4eChris Lattner    return clang::Selector::getEmptyMarker();
71685994260c41a54cab061a434ed378fc448333a4eChris Lattner  }
71785994260c41a54cab061a434ed378fc448333a4eChris Lattner  static inline clang::Selector getTombstoneKey() {
7181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    return clang::Selector::getTombstoneMarker();
71985994260c41a54cab061a434ed378fc448333a4eChris Lattner  }
7201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
72185994260c41a54cab061a434ed378fc448333a4eChris Lattner  static unsigned getHashValue(clang::Selector S);
7221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
72385994260c41a54cab061a434ed378fc448333a4eChris Lattner  static bool isEqual(clang::Selector LHS, clang::Selector RHS) {
72485994260c41a54cab061a434ed378fc448333a4eChris Lattner    return LHS == RHS;
72585994260c41a54cab061a434ed378fc448333a4eChris Lattner  }
72685994260c41a54cab061a434ed378fc448333a4eChris Lattner};
727700030ebddb987936d4fee14d9412821d96e4840Kovarththanan Rajaratnam
72806159e878569e5f39bf0e8f11b84ac3ad0970597Chris Lattnertemplate <>
72906159e878569e5f39bf0e8f11b84ac3ad0970597Chris Lattnerstruct isPodLike<clang::Selector> { static const bool value = true; };
73006159e878569e5f39bf0e8f11b84ac3ad0970597Chris Lattner
731d36adf553cd71df96fe869204482e0f0d68c6bbfDouglas Gregortemplate<>
732d36adf553cd71df96fe869204482e0f0d68c6bbfDouglas Gregorclass PointerLikeTypeTraits<clang::Selector> {
733d36adf553cd71df96fe869204482e0f0d68c6bbfDouglas Gregorpublic:
734d36adf553cd71df96fe869204482e0f0d68c6bbfDouglas Gregor  static inline const void *getAsVoidPointer(clang::Selector P) {
735d36adf553cd71df96fe869204482e0f0d68c6bbfDouglas Gregor    return P.getAsOpaquePtr();
736d36adf553cd71df96fe869204482e0f0d68c6bbfDouglas Gregor  }
737d36adf553cd71df96fe869204482e0f0d68c6bbfDouglas Gregor  static inline clang::Selector getFromVoidPointer(const void *P) {
738d36adf553cd71df96fe869204482e0f0d68c6bbfDouglas Gregor    return clang::Selector(reinterpret_cast<uintptr_t>(P));
739d36adf553cd71df96fe869204482e0f0d68c6bbfDouglas Gregor  }
740d36adf553cd71df96fe869204482e0f0d68c6bbfDouglas Gregor  enum { NumLowBitsAvailable = 0 };
741d36adf553cd71df96fe869204482e0f0d68c6bbfDouglas Gregor};
74285994260c41a54cab061a434ed378fc448333a4eChris Lattner
7431734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor// Provide PointerLikeTypeTraits for IdentifierInfo pointers, which
7441734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor// are not guaranteed to be 8-byte aligned.
7451734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregortemplate<>
7461734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregorclass PointerLikeTypeTraits<clang::IdentifierInfo*> {
7471734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregorpublic:
7481734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor  static inline void *getAsVoidPointer(clang::IdentifierInfo* P) {
7491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    return P;
7501734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor  }
7511734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor  static inline clang::IdentifierInfo *getFromVoidPointer(void *P) {
7521734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor    return static_cast<clang::IdentifierInfo*>(P);
7531734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor  }
7541734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor  enum { NumLowBitsAvailable = 1 };
7551734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor};
7561734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor
7571734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregortemplate<>
7581734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregorclass PointerLikeTypeTraits<const clang::IdentifierInfo*> {
7591734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregorpublic:
7601734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor  static inline const void *getAsVoidPointer(const clang::IdentifierInfo* P) {
7611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    return P;
7621734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor  }
7631734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor  static inline const clang::IdentifierInfo *getFromVoidPointer(const void *P) {
7641734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor    return static_cast<const clang::IdentifierInfo*>(P);
7651734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor  }
7661734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor  enum { NumLowBitsAvailable = 1 };
7671734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor};
7681734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor
7692e1cd4264d363ca869bf37ef160902f211d21b8cDouglas Gregor}  // end namespace llvm
7705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#endif
771