IdentifierTable.cpp 25 KB

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