IdentifierTable.cpp 29 KB

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