IdentifierTable.cpp 22 KB

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