IdentifierTable.cpp 23 KB

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