IdentifierTable.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645
  1. //===--- IdentifierTable.cpp - Hash table for identifier lookup -----------===//
  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. // This file implements the IdentifierInfo, IdentifierVisitor, and
  11. // IdentifierTable interfaces.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #include "clang/Basic/CharInfo.h"
  15. #include "clang/Basic/IdentifierTable.h"
  16. #include "clang/Basic/LangOptions.h"
  17. #include "clang/Basic/OperatorKinds.h"
  18. #include "llvm/ADT/DenseMap.h"
  19. #include "llvm/ADT/FoldingSet.h"
  20. #include "llvm/ADT/SmallString.h"
  21. #include "llvm/Support/ErrorHandling.h"
  22. #include "llvm/Support/raw_ostream.h"
  23. #include <cstdio>
  24. using namespace clang;
  25. //===----------------------------------------------------------------------===//
  26. // IdentifierInfo Implementation
  27. //===----------------------------------------------------------------------===//
  28. IdentifierInfo::IdentifierInfo() {
  29. TokenID = tok::identifier;
  30. ObjCOrBuiltinID = 0;
  31. HasMacro = false;
  32. HadMacro = false;
  33. IsExtension = false;
  34. IsFutureCompatKeyword = false;
  35. IsPoisoned = false;
  36. IsCPPOperatorKeyword = false;
  37. NeedsHandleIdentifier = false;
  38. IsFromAST = false;
  39. ChangedAfterLoad = false;
  40. RevertedTokenID = false;
  41. OutOfDate = false;
  42. IsModulesImport = false;
  43. FETokenInfo = nullptr;
  44. Entry = nullptr;
  45. }
  46. //===----------------------------------------------------------------------===//
  47. // IdentifierTable Implementation
  48. //===----------------------------------------------------------------------===//
  49. IdentifierIterator::~IdentifierIterator() { }
  50. IdentifierInfoLookup::~IdentifierInfoLookup() {}
  51. namespace {
  52. /// \brief A simple identifier lookup iterator that represents an
  53. /// empty sequence of identifiers.
  54. class EmptyLookupIterator : public IdentifierIterator
  55. {
  56. public:
  57. StringRef Next() override { return StringRef(); }
  58. };
  59. }
  60. IdentifierIterator *IdentifierInfoLookup::getIdentifiers() {
  61. return new EmptyLookupIterator();
  62. }
  63. ExternalIdentifierLookup::~ExternalIdentifierLookup() {}
  64. IdentifierTable::IdentifierTable(const LangOptions &LangOpts,
  65. IdentifierInfoLookup* externalLookup)
  66. : HashTable(8192), // Start with space for 8K identifiers.
  67. ExternalLookup(externalLookup) {
  68. // Populate the identifier table with info about keywords for the current
  69. // language.
  70. AddKeywords(LangOpts);
  71. // Add the '_experimental_modules_import' contextual keyword.
  72. get("import").setModulesImport(true);
  73. }
  74. //===----------------------------------------------------------------------===//
  75. // Language Keyword Implementation
  76. //===----------------------------------------------------------------------===//
  77. // Constants for TokenKinds.def
  78. namespace {
  79. enum {
  80. KEYC99 = 0x1,
  81. KEYCXX = 0x2,
  82. KEYCXX11 = 0x4,
  83. KEYGNU = 0x8,
  84. KEYMS = 0x10,
  85. BOOLSUPPORT = 0x20,
  86. KEYALTIVEC = 0x40,
  87. KEYNOCXX = 0x80,
  88. KEYBORLAND = 0x100,
  89. KEYOPENCL = 0x200,
  90. KEYC11 = 0x400,
  91. KEYARC = 0x800,
  92. KEYNOMS18 = 0x01000,
  93. KEYNOOPENCL = 0x02000,
  94. WCHARSUPPORT = 0x04000,
  95. HALFSUPPORT = 0x08000,
  96. KEYALL = (0xffff & ~KEYNOMS18 &
  97. ~KEYNOOPENCL) // KEYNOMS18 and KEYNOOPENCL are used to exclude.
  98. };
  99. /// \brief How a keyword is treated in the selected standard.
  100. enum KeywordStatus {
  101. KS_Disabled, // Disabled
  102. KS_Extension, // Is an extension
  103. KS_Enabled, // Enabled
  104. KS_Future // Is a keyword in future standard
  105. };
  106. }
  107. /// \brief Translates flags as specified in TokenKinds.def into keyword status
  108. /// in the given language standard.
  109. static KeywordStatus getKeywordStatus(const LangOptions &LangOpts,
  110. unsigned Flags) {
  111. if (Flags == KEYALL) return KS_Enabled;
  112. if (LangOpts.CPlusPlus && (Flags & KEYCXX)) return KS_Enabled;
  113. if (LangOpts.CPlusPlus11 && (Flags & KEYCXX11)) return KS_Enabled;
  114. if (LangOpts.C99 && (Flags & KEYC99)) return KS_Enabled;
  115. if (LangOpts.GNUKeywords && (Flags & KEYGNU)) return KS_Extension;
  116. if (LangOpts.MicrosoftExt && (Flags & KEYMS)) return KS_Extension;
  117. if (LangOpts.Borland && (Flags & KEYBORLAND)) return KS_Extension;
  118. if (LangOpts.Bool && (Flags & BOOLSUPPORT)) return KS_Enabled;
  119. if (LangOpts.Half && (Flags & HALFSUPPORT)) return KS_Enabled;
  120. if (LangOpts.WChar && (Flags & WCHARSUPPORT)) return KS_Enabled;
  121. if (LangOpts.AltiVec && (Flags & KEYALTIVEC)) return KS_Enabled;
  122. if (LangOpts.OpenCL && (Flags & KEYOPENCL)) return KS_Enabled;
  123. if (!LangOpts.CPlusPlus && (Flags & KEYNOCXX)) return KS_Enabled;
  124. if (LangOpts.C11 && (Flags & KEYC11)) return KS_Enabled;
  125. // We treat bridge casts as objective-C keywords so we can warn on them
  126. // in non-arc mode.
  127. if (LangOpts.ObjC2 && (Flags & KEYARC)) return KS_Enabled;
  128. if (LangOpts.CPlusPlus && (Flags & KEYCXX11)) return KS_Future;
  129. return KS_Disabled;
  130. }
  131. /// AddKeyword - This method is used to associate a token ID with specific
  132. /// identifiers because they are language keywords. This causes the lexer to
  133. /// automatically map matching identifiers to specialized token codes.
  134. static void AddKeyword(StringRef Keyword,
  135. tok::TokenKind TokenCode, unsigned Flags,
  136. const LangOptions &LangOpts, IdentifierTable &Table) {
  137. KeywordStatus AddResult = getKeywordStatus(LangOpts, Flags);
  138. // Don't add this keyword under MSVCCompat.
  139. if (LangOpts.MSVCCompat && (Flags & KEYNOMS18) &&
  140. !LangOpts.isCompatibleWithMSVC(LangOptions::MSVC2015))
  141. return;
  142. // Don't add this keyword under OpenCL.
  143. if (LangOpts.OpenCL && (Flags & KEYNOOPENCL))
  144. return;
  145. // Don't add this keyword if disabled in this language.
  146. if (AddResult == KS_Disabled) return;
  147. IdentifierInfo &Info =
  148. Table.get(Keyword, AddResult == KS_Future ? tok::identifier : TokenCode);
  149. Info.setIsExtensionToken(AddResult == KS_Extension);
  150. Info.setIsFutureCompatKeyword(AddResult == KS_Future);
  151. }
  152. /// AddCXXOperatorKeyword - Register a C++ operator keyword alternative
  153. /// representations.
  154. static void AddCXXOperatorKeyword(StringRef Keyword,
  155. tok::TokenKind TokenCode,
  156. IdentifierTable &Table) {
  157. IdentifierInfo &Info = Table.get(Keyword, TokenCode);
  158. Info.setIsCPlusPlusOperatorKeyword();
  159. }
  160. /// AddObjCKeyword - Register an Objective-C \@keyword like "class" "selector"
  161. /// or "property".
  162. static void AddObjCKeyword(StringRef Name,
  163. tok::ObjCKeywordKind ObjCID,
  164. IdentifierTable &Table) {
  165. Table.get(Name).setObjCKeywordID(ObjCID);
  166. }
  167. /// AddKeywords - Add all keywords to the symbol table.
  168. ///
  169. void IdentifierTable::AddKeywords(const LangOptions &LangOpts) {
  170. // Add keywords and tokens for the current language.
  171. #define KEYWORD(NAME, FLAGS) \
  172. AddKeyword(StringRef(#NAME), tok::kw_ ## NAME, \
  173. FLAGS, LangOpts, *this);
  174. #define ALIAS(NAME, TOK, FLAGS) \
  175. AddKeyword(StringRef(NAME), tok::kw_ ## TOK, \
  176. FLAGS, LangOpts, *this);
  177. #define CXX_KEYWORD_OPERATOR(NAME, ALIAS) \
  178. if (LangOpts.CXXOperatorNames) \
  179. AddCXXOperatorKeyword(StringRef(#NAME), tok::ALIAS, *this);
  180. #define OBJC1_AT_KEYWORD(NAME) \
  181. if (LangOpts.ObjC1) \
  182. AddObjCKeyword(StringRef(#NAME), tok::objc_##NAME, *this);
  183. #define OBJC2_AT_KEYWORD(NAME) \
  184. if (LangOpts.ObjC2) \
  185. AddObjCKeyword(StringRef(#NAME), tok::objc_##NAME, *this);
  186. #define TESTING_KEYWORD(NAME, FLAGS)
  187. #include "clang/Basic/TokenKinds.def"
  188. if (LangOpts.ParseUnknownAnytype)
  189. AddKeyword("__unknown_anytype", tok::kw___unknown_anytype, KEYALL,
  190. LangOpts, *this);
  191. // FIXME: __declspec isn't really a CUDA extension, however it is required for
  192. // supporting cuda_builtin_vars.h, which uses __declspec(property). Once that
  193. // has been rewritten in terms of something more generic, remove this code.
  194. if (LangOpts.CUDA)
  195. AddKeyword("__declspec", tok::kw___declspec, KEYALL, LangOpts, *this);
  196. }
  197. /// \brief Checks if the specified token kind represents a keyword in the
  198. /// specified language.
  199. /// \returns Status of the keyword in the language.
  200. static KeywordStatus getTokenKwStatus(const LangOptions &LangOpts,
  201. tok::TokenKind K) {
  202. switch (K) {
  203. #define KEYWORD(NAME, FLAGS) \
  204. case tok::kw_##NAME: return getKeywordStatus(LangOpts, FLAGS);
  205. #include "clang/Basic/TokenKinds.def"
  206. default: return KS_Disabled;
  207. }
  208. }
  209. /// \brief Returns true if the identifier represents a keyword in the
  210. /// specified language.
  211. bool IdentifierInfo::isKeyword(const LangOptions &LangOpts) {
  212. switch (getTokenKwStatus(LangOpts, getTokenID())) {
  213. case KS_Enabled:
  214. case KS_Extension:
  215. return true;
  216. default:
  217. return false;
  218. }
  219. }
  220. tok::PPKeywordKind IdentifierInfo::getPPKeywordID() const {
  221. // We use a perfect hash function here involving the length of the keyword,
  222. // the first and third character. For preprocessor ID's there are no
  223. // collisions (if there were, the switch below would complain about duplicate
  224. // case values). Note that this depends on 'if' being null terminated.
  225. #define HASH(LEN, FIRST, THIRD) \
  226. (LEN << 5) + (((FIRST-'a') + (THIRD-'a')) & 31)
  227. #define CASE(LEN, FIRST, THIRD, NAME) \
  228. case HASH(LEN, FIRST, THIRD): \
  229. return memcmp(Name, #NAME, LEN) ? tok::pp_not_keyword : tok::pp_ ## NAME
  230. unsigned Len = getLength();
  231. if (Len < 2) return tok::pp_not_keyword;
  232. const char *Name = getNameStart();
  233. switch (HASH(Len, Name[0], Name[2])) {
  234. default: return tok::pp_not_keyword;
  235. CASE( 2, 'i', '\0', if);
  236. CASE( 4, 'e', 'i', elif);
  237. CASE( 4, 'e', 's', else);
  238. CASE( 4, 'l', 'n', line);
  239. CASE( 4, 's', 'c', sccs);
  240. CASE( 5, 'e', 'd', endif);
  241. CASE( 5, 'e', 'r', error);
  242. CASE( 5, 'i', 'e', ident);
  243. CASE( 5, 'i', 'd', ifdef);
  244. CASE( 5, 'u', 'd', undef);
  245. CASE( 6, 'a', 's', assert);
  246. CASE( 6, 'd', 'f', define);
  247. CASE( 6, 'i', 'n', ifndef);
  248. CASE( 6, 'i', 'p', import);
  249. CASE( 6, 'p', 'a', pragma);
  250. CASE( 7, 'd', 'f', defined);
  251. CASE( 7, 'i', 'c', include);
  252. CASE( 7, 'w', 'r', warning);
  253. CASE( 8, 'u', 'a', unassert);
  254. CASE(12, 'i', 'c', include_next);
  255. CASE(14, '_', 'p', __public_macro);
  256. CASE(15, '_', 'p', __private_macro);
  257. CASE(16, '_', 'i', __include_macros);
  258. #undef CASE
  259. #undef HASH
  260. }
  261. }
  262. //===----------------------------------------------------------------------===//
  263. // Stats Implementation
  264. //===----------------------------------------------------------------------===//
  265. /// PrintStats - Print statistics about how well the identifier table is doing
  266. /// at hashing identifiers.
  267. void IdentifierTable::PrintStats() const {
  268. unsigned NumBuckets = HashTable.getNumBuckets();
  269. unsigned NumIdentifiers = HashTable.getNumItems();
  270. unsigned NumEmptyBuckets = NumBuckets-NumIdentifiers;
  271. unsigned AverageIdentifierSize = 0;
  272. unsigned MaxIdentifierLength = 0;
  273. // TODO: Figure out maximum times an identifier had to probe for -stats.
  274. for (llvm::StringMap<IdentifierInfo*, llvm::BumpPtrAllocator>::const_iterator
  275. I = HashTable.begin(), E = HashTable.end(); I != E; ++I) {
  276. unsigned IdLen = I->getKeyLength();
  277. AverageIdentifierSize += IdLen;
  278. if (MaxIdentifierLength < IdLen)
  279. MaxIdentifierLength = IdLen;
  280. }
  281. fprintf(stderr, "\n*** Identifier Table Stats:\n");
  282. fprintf(stderr, "# Identifiers: %d\n", NumIdentifiers);
  283. fprintf(stderr, "# Empty Buckets: %d\n", NumEmptyBuckets);
  284. fprintf(stderr, "Hash density (#identifiers per bucket): %f\n",
  285. NumIdentifiers/(double)NumBuckets);
  286. fprintf(stderr, "Ave identifier length: %f\n",
  287. (AverageIdentifierSize/(double)NumIdentifiers));
  288. fprintf(stderr, "Max identifier length: %d\n", MaxIdentifierLength);
  289. // Compute statistics about the memory allocated for identifiers.
  290. HashTable.getAllocator().PrintStats();
  291. }
  292. //===----------------------------------------------------------------------===//
  293. // SelectorTable Implementation
  294. //===----------------------------------------------------------------------===//
  295. unsigned llvm::DenseMapInfo<clang::Selector>::getHashValue(clang::Selector S) {
  296. return DenseMapInfo<void*>::getHashValue(S.getAsOpaquePtr());
  297. }
  298. namespace clang {
  299. /// MultiKeywordSelector - One of these variable length records is kept for each
  300. /// selector containing more than one keyword. We use a folding set
  301. /// to unique aggregate names (keyword selectors in ObjC parlance). Access to
  302. /// this class is provided strictly through Selector.
  303. class MultiKeywordSelector
  304. : public DeclarationNameExtra, public llvm::FoldingSetNode {
  305. MultiKeywordSelector(unsigned nKeys) {
  306. ExtraKindOrNumArgs = NUM_EXTRA_KINDS + nKeys;
  307. }
  308. public:
  309. // Constructor for keyword selectors.
  310. MultiKeywordSelector(unsigned nKeys, IdentifierInfo **IIV) {
  311. assert((nKeys > 1) && "not a multi-keyword selector");
  312. ExtraKindOrNumArgs = NUM_EXTRA_KINDS + nKeys;
  313. // Fill in the trailing keyword array.
  314. IdentifierInfo **KeyInfo = reinterpret_cast<IdentifierInfo **>(this+1);
  315. for (unsigned i = 0; i != nKeys; ++i)
  316. KeyInfo[i] = IIV[i];
  317. }
  318. // getName - Derive the full selector name and return it.
  319. std::string getName() const;
  320. unsigned getNumArgs() const { return ExtraKindOrNumArgs - NUM_EXTRA_KINDS; }
  321. typedef IdentifierInfo *const *keyword_iterator;
  322. keyword_iterator keyword_begin() const {
  323. return reinterpret_cast<keyword_iterator>(this+1);
  324. }
  325. keyword_iterator keyword_end() const {
  326. return keyword_begin()+getNumArgs();
  327. }
  328. IdentifierInfo *getIdentifierInfoForSlot(unsigned i) const {
  329. assert(i < getNumArgs() && "getIdentifierInfoForSlot(): illegal index");
  330. return keyword_begin()[i];
  331. }
  332. static void Profile(llvm::FoldingSetNodeID &ID,
  333. keyword_iterator ArgTys, unsigned NumArgs) {
  334. ID.AddInteger(NumArgs);
  335. for (unsigned i = 0; i != NumArgs; ++i)
  336. ID.AddPointer(ArgTys[i]);
  337. }
  338. void Profile(llvm::FoldingSetNodeID &ID) {
  339. Profile(ID, keyword_begin(), getNumArgs());
  340. }
  341. };
  342. } // end namespace clang.
  343. unsigned Selector::getNumArgs() const {
  344. unsigned IIF = getIdentifierInfoFlag();
  345. if (IIF <= ZeroArg)
  346. return 0;
  347. if (IIF == OneArg)
  348. return 1;
  349. // We point to a MultiKeywordSelector.
  350. MultiKeywordSelector *SI = getMultiKeywordSelector();
  351. return SI->getNumArgs();
  352. }
  353. IdentifierInfo *Selector::getIdentifierInfoForSlot(unsigned argIndex) const {
  354. if (getIdentifierInfoFlag() < MultiArg) {
  355. assert(argIndex == 0 && "illegal keyword index");
  356. return getAsIdentifierInfo();
  357. }
  358. // We point to a MultiKeywordSelector.
  359. MultiKeywordSelector *SI = getMultiKeywordSelector();
  360. return SI->getIdentifierInfoForSlot(argIndex);
  361. }
  362. StringRef Selector::getNameForSlot(unsigned int argIndex) const {
  363. IdentifierInfo *II = getIdentifierInfoForSlot(argIndex);
  364. return II? II->getName() : StringRef();
  365. }
  366. std::string MultiKeywordSelector::getName() const {
  367. SmallString<256> Str;
  368. llvm::raw_svector_ostream OS(Str);
  369. for (keyword_iterator I = keyword_begin(), E = keyword_end(); I != E; ++I) {
  370. if (*I)
  371. OS << (*I)->getName();
  372. OS << ':';
  373. }
  374. return OS.str();
  375. }
  376. std::string Selector::getAsString() const {
  377. if (InfoPtr == 0)
  378. return "<null selector>";
  379. if (getIdentifierInfoFlag() < MultiArg) {
  380. IdentifierInfo *II = getAsIdentifierInfo();
  381. // If the number of arguments is 0 then II is guaranteed to not be null.
  382. if (getNumArgs() == 0)
  383. return II->getName();
  384. if (!II)
  385. return ":";
  386. return II->getName().str() + ":";
  387. }
  388. // We have a multiple keyword selector.
  389. return getMultiKeywordSelector()->getName();
  390. }
  391. void Selector::print(llvm::raw_ostream &OS) const {
  392. OS << getAsString();
  393. }
  394. /// Interpreting the given string using the normal CamelCase
  395. /// conventions, determine whether the given string starts with the
  396. /// given "word", which is assumed to end in a lowercase letter.
  397. static bool startsWithWord(StringRef name, StringRef word) {
  398. if (name.size() < word.size()) return false;
  399. return ((name.size() == word.size() || !isLowercase(name[word.size()])) &&
  400. name.startswith(word));
  401. }
  402. ObjCMethodFamily Selector::getMethodFamilyImpl(Selector sel) {
  403. IdentifierInfo *first = sel.getIdentifierInfoForSlot(0);
  404. if (!first) return OMF_None;
  405. StringRef name = first->getName();
  406. if (sel.isUnarySelector()) {
  407. if (name == "autorelease") return OMF_autorelease;
  408. if (name == "dealloc") return OMF_dealloc;
  409. if (name == "finalize") return OMF_finalize;
  410. if (name == "release") return OMF_release;
  411. if (name == "retain") return OMF_retain;
  412. if (name == "retainCount") return OMF_retainCount;
  413. if (name == "self") return OMF_self;
  414. if (name == "initialize") return OMF_initialize;
  415. }
  416. if (name == "performSelector") return OMF_performSelector;
  417. // The other method families may begin with a prefix of underscores.
  418. while (!name.empty() && name.front() == '_')
  419. name = name.substr(1);
  420. if (name.empty()) return OMF_None;
  421. switch (name.front()) {
  422. case 'a':
  423. if (startsWithWord(name, "alloc")) return OMF_alloc;
  424. break;
  425. case 'c':
  426. if (startsWithWord(name, "copy")) return OMF_copy;
  427. break;
  428. case 'i':
  429. if (startsWithWord(name, "init")) return OMF_init;
  430. break;
  431. case 'm':
  432. if (startsWithWord(name, "mutableCopy")) return OMF_mutableCopy;
  433. break;
  434. case 'n':
  435. if (startsWithWord(name, "new")) return OMF_new;
  436. break;
  437. default:
  438. break;
  439. }
  440. return OMF_None;
  441. }
  442. ObjCInstanceTypeFamily Selector::getInstTypeMethodFamily(Selector sel) {
  443. IdentifierInfo *first = sel.getIdentifierInfoForSlot(0);
  444. if (!first) return OIT_None;
  445. StringRef name = first->getName();
  446. if (name.empty()) return OIT_None;
  447. switch (name.front()) {
  448. case 'a':
  449. if (startsWithWord(name, "array")) return OIT_Array;
  450. break;
  451. case 'd':
  452. if (startsWithWord(name, "default")) return OIT_ReturnsSelf;
  453. if (startsWithWord(name, "dictionary")) return OIT_Dictionary;
  454. break;
  455. case 's':
  456. if (startsWithWord(name, "shared")) return OIT_ReturnsSelf;
  457. if (startsWithWord(name, "standard")) return OIT_Singleton;
  458. case 'i':
  459. if (startsWithWord(name, "init")) return OIT_Init;
  460. default:
  461. break;
  462. }
  463. return OIT_None;
  464. }
  465. ObjCStringFormatFamily Selector::getStringFormatFamilyImpl(Selector sel) {
  466. IdentifierInfo *first = sel.getIdentifierInfoForSlot(0);
  467. if (!first) return SFF_None;
  468. StringRef name = first->getName();
  469. switch (name.front()) {
  470. case 'a':
  471. if (name == "appendFormat") return SFF_NSString;
  472. break;
  473. case 'i':
  474. if (name == "initWithFormat") return SFF_NSString;
  475. break;
  476. case 'l':
  477. if (name == "localizedStringWithFormat") return SFF_NSString;
  478. break;
  479. case 's':
  480. if (name == "stringByAppendingFormat" ||
  481. name == "stringWithFormat") return SFF_NSString;
  482. break;
  483. }
  484. return SFF_None;
  485. }
  486. namespace {
  487. struct SelectorTableImpl {
  488. llvm::FoldingSet<MultiKeywordSelector> Table;
  489. llvm::BumpPtrAllocator Allocator;
  490. };
  491. } // end anonymous namespace.
  492. static SelectorTableImpl &getSelectorTableImpl(void *P) {
  493. return *static_cast<SelectorTableImpl*>(P);
  494. }
  495. SmallString<64>
  496. SelectorTable::constructSetterName(StringRef Name) {
  497. SmallString<64> SetterName("set");
  498. SetterName += Name;
  499. SetterName[3] = toUppercase(SetterName[3]);
  500. return SetterName;
  501. }
  502. Selector
  503. SelectorTable::constructSetterSelector(IdentifierTable &Idents,
  504. SelectorTable &SelTable,
  505. const IdentifierInfo *Name) {
  506. IdentifierInfo *SetterName =
  507. &Idents.get(constructSetterName(Name->getName()));
  508. return SelTable.getUnarySelector(SetterName);
  509. }
  510. size_t SelectorTable::getTotalMemory() const {
  511. SelectorTableImpl &SelTabImpl = getSelectorTableImpl(Impl);
  512. return SelTabImpl.Allocator.getTotalMemory();
  513. }
  514. Selector SelectorTable::getSelector(unsigned nKeys, IdentifierInfo **IIV) {
  515. if (nKeys < 2)
  516. return Selector(IIV[0], nKeys);
  517. SelectorTableImpl &SelTabImpl = getSelectorTableImpl(Impl);
  518. // Unique selector, to guarantee there is one per name.
  519. llvm::FoldingSetNodeID ID;
  520. MultiKeywordSelector::Profile(ID, IIV, nKeys);
  521. void *InsertPos = nullptr;
  522. if (MultiKeywordSelector *SI =
  523. SelTabImpl.Table.FindNodeOrInsertPos(ID, InsertPos))
  524. return Selector(SI);
  525. // MultiKeywordSelector objects are not allocated with new because they have a
  526. // variable size array (for parameter types) at the end of them.
  527. unsigned Size = sizeof(MultiKeywordSelector) + nKeys*sizeof(IdentifierInfo *);
  528. MultiKeywordSelector *SI =
  529. (MultiKeywordSelector*)SelTabImpl.Allocator.Allocate(Size,
  530. llvm::alignOf<MultiKeywordSelector>());
  531. new (SI) MultiKeywordSelector(nKeys, IIV);
  532. SelTabImpl.Table.InsertNode(SI, InsertPos);
  533. return Selector(SI);
  534. }
  535. SelectorTable::SelectorTable() {
  536. Impl = new SelectorTableImpl();
  537. }
  538. SelectorTable::~SelectorTable() {
  539. delete &getSelectorTableImpl(Impl);
  540. }
  541. const char *clang::getOperatorSpelling(OverloadedOperatorKind Operator) {
  542. switch (Operator) {
  543. case OO_None:
  544. case NUM_OVERLOADED_OPERATORS:
  545. return nullptr;
  546. #define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
  547. case OO_##Name: return Spelling;
  548. #include "clang/Basic/OperatorKinds.def"
  549. }
  550. llvm_unreachable("Invalid OverloadedOperatorKind!");
  551. }