IdentifierTable.h 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- IdentifierTable.h - Hash table for identifier lookup -----*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. /// \file
  15. /// Defines the clang::IdentifierInfo, clang::IdentifierTable, and
  16. /// clang::Selector interfaces.
  17. //
  18. //===----------------------------------------------------------------------===//
  19. #ifndef LLVM_CLANG_BASIC_IDENTIFIERTABLE_H
  20. #define LLVM_CLANG_BASIC_IDENTIFIERTABLE_H
  21. #include "clang/Basic/LLVM.h"
  22. #include "clang/Basic/TokenKinds.h"
  23. #include "llvm/ADT/DenseMapInfo.h"
  24. #include "llvm/ADT/SmallString.h"
  25. #include "llvm/ADT/StringMap.h"
  26. #include "llvm/ADT/StringRef.h"
  27. #include "llvm/Support/Allocator.h"
  28. #include "llvm/Support/PointerLikeTypeTraits.h"
  29. #include "llvm/Support/type_traits.h"
  30. #include <cassert>
  31. #include <cstddef>
  32. #include <cstdint>
  33. #include <cstring>
  34. #include <string>
  35. #include <utility>
  36. namespace clang {
  37. class DeclarationName;
  38. class DeclarationNameTable;
  39. class IdentifierInfo;
  40. class LangOptions;
  41. class MultiKeywordSelector;
  42. class SourceLocation;
  43. enum class ReservedIdentifierStatus {
  44. NotReserved = 0,
  45. StartsWithUnderscoreAtGlobalScope,
  46. StartsWithUnderscoreAndIsExternC,
  47. StartsWithDoubleUnderscore,
  48. StartsWithUnderscoreFollowedByCapitalLetter,
  49. ContainsDoubleUnderscore,
  50. };
  51. /// Determine whether an identifier is reserved for use as a name at global
  52. /// scope. Such identifiers might be implementation-specific global functions
  53. /// or variables.
  54. inline bool isReservedAtGlobalScope(ReservedIdentifierStatus Status) {
  55. return Status != ReservedIdentifierStatus::NotReserved;
  56. }
  57. /// Determine whether an identifier is reserved in all contexts. Such
  58. /// identifiers might be implementation-specific keywords or macros, for
  59. /// example.
  60. inline bool isReservedInAllContexts(ReservedIdentifierStatus Status) {
  61. return Status != ReservedIdentifierStatus::NotReserved &&
  62. Status != ReservedIdentifierStatus::StartsWithUnderscoreAtGlobalScope &&
  63. Status != ReservedIdentifierStatus::StartsWithUnderscoreAndIsExternC;
  64. }
  65. /// A simple pair of identifier info and location.
  66. using IdentifierLocPair = std::pair<IdentifierInfo *, SourceLocation>;
  67. /// IdentifierInfo and other related classes are aligned to
  68. /// 8 bytes so that DeclarationName can use the lower 3 bits
  69. /// of a pointer to one of these classes.
  70. enum { IdentifierInfoAlignment = 8 };
  71. static constexpr int ObjCOrBuiltinIDBits = 16;
  72. /// One of these records is kept for each identifier that
  73. /// is lexed. This contains information about whether the token was \#define'd,
  74. /// is a language keyword, or if it is a front-end token of some sort (e.g. a
  75. /// variable or function name). The preprocessor keeps this information in a
  76. /// set, and all tok::identifier tokens have a pointer to one of these.
  77. /// It is aligned to 8 bytes because DeclarationName needs the lower 3 bits.
  78. class alignas(IdentifierInfoAlignment) IdentifierInfo {
  79. friend class IdentifierTable;
  80. // Front-end token ID or tok::identifier.
  81. unsigned TokenID : 9;
  82. // ObjC keyword ('protocol' in '@protocol') or builtin (__builtin_inf).
  83. // First NUM_OBJC_KEYWORDS values are for Objective-C,
  84. // the remaining values are for builtins.
  85. unsigned ObjCOrBuiltinID : ObjCOrBuiltinIDBits;
  86. // True if there is a #define for this.
  87. unsigned HasMacro : 1;
  88. // True if there was a #define for this.
  89. unsigned HadMacro : 1;
  90. // True if the identifier is a language extension.
  91. unsigned IsExtension : 1;
  92. // True if the identifier is a keyword in a newer or proposed Standard.
  93. unsigned IsFutureCompatKeyword : 1;
  94. // True if the identifier is poisoned.
  95. unsigned IsPoisoned : 1;
  96. // True if the identifier is a C++ operator keyword.
  97. unsigned IsCPPOperatorKeyword : 1;
  98. // Internal bit set by the member function RecomputeNeedsHandleIdentifier.
  99. // See comment about RecomputeNeedsHandleIdentifier for more info.
  100. unsigned NeedsHandleIdentifier : 1;
  101. // True if the identifier was loaded (at least partially) from an AST file.
  102. unsigned IsFromAST : 1;
  103. // True if the identifier has changed from the definition
  104. // loaded from an AST file.
  105. unsigned ChangedAfterLoad : 1;
  106. // True if the identifier's frontend information has changed from the
  107. // definition loaded from an AST file.
  108. unsigned FEChangedAfterLoad : 1;
  109. // True if revertTokenIDToIdentifier was called.
  110. unsigned RevertedTokenID : 1;
  111. // True if there may be additional information about
  112. // this identifier stored externally.
  113. unsigned OutOfDate : 1;
  114. // True if this is the 'import' contextual keyword.
  115. unsigned IsModulesImport : 1;
  116. // True if this is a mangled OpenMP variant name.
  117. unsigned IsMangledOpenMPVariantName : 1;
  118. // True if this is a deprecated macro.
  119. unsigned IsDeprecatedMacro : 1;
  120. // True if this macro is unsafe in headers.
  121. unsigned IsRestrictExpansion : 1;
  122. // True if this macro is final.
  123. unsigned IsFinal : 1;
  124. // 22 bits left in a 64-bit word.
  125. // Managed by the language front-end.
  126. void *FETokenInfo = nullptr;
  127. llvm::StringMapEntry<IdentifierInfo *> *Entry = nullptr;
  128. IdentifierInfo()
  129. : TokenID(tok::identifier), ObjCOrBuiltinID(0), HasMacro(false),
  130. HadMacro(false), IsExtension(false), IsFutureCompatKeyword(false),
  131. IsPoisoned(false), IsCPPOperatorKeyword(false),
  132. NeedsHandleIdentifier(false), IsFromAST(false), ChangedAfterLoad(false),
  133. FEChangedAfterLoad(false), RevertedTokenID(false), OutOfDate(false),
  134. IsModulesImport(false), IsMangledOpenMPVariantName(false),
  135. IsDeprecatedMacro(false), IsRestrictExpansion(false), IsFinal(false) {}
  136. public:
  137. IdentifierInfo(const IdentifierInfo &) = delete;
  138. IdentifierInfo &operator=(const IdentifierInfo &) = delete;
  139. IdentifierInfo(IdentifierInfo &&) = delete;
  140. IdentifierInfo &operator=(IdentifierInfo &&) = delete;
  141. /// Return true if this is the identifier for the specified string.
  142. ///
  143. /// This is intended to be used for string literals only: II->isStr("foo").
  144. template <std::size_t StrLen>
  145. bool isStr(const char (&Str)[StrLen]) const {
  146. return getLength() == StrLen-1 &&
  147. memcmp(getNameStart(), Str, StrLen-1) == 0;
  148. }
  149. /// Return true if this is the identifier for the specified StringRef.
  150. bool isStr(llvm::StringRef Str) const {
  151. llvm::StringRef ThisStr(getNameStart(), getLength());
  152. return ThisStr == Str;
  153. }
  154. /// Return the beginning of the actual null-terminated string for this
  155. /// identifier.
  156. const char *getNameStart() const { return Entry->getKeyData(); }
  157. /// Efficiently return the length of this identifier info.
  158. unsigned getLength() const { return Entry->getKeyLength(); }
  159. /// Return the actual identifier string.
  160. StringRef getName() const {
  161. return StringRef(getNameStart(), getLength());
  162. }
  163. /// Return true if this identifier is \#defined to some other value.
  164. /// \note The current definition may be in a module and not currently visible.
  165. bool hasMacroDefinition() const {
  166. return HasMacro;
  167. }
  168. void setHasMacroDefinition(bool Val) {
  169. if (HasMacro == Val) return;
  170. HasMacro = Val;
  171. if (Val) {
  172. NeedsHandleIdentifier = true;
  173. HadMacro = true;
  174. } else {
  175. // If this is a final macro, make the deprecation and header unsafe bits
  176. // stick around after the undefinition so they apply to any redefinitions.
  177. if (!IsFinal) {
  178. // Because calling the setters of these calls recomputes, just set them
  179. // manually to avoid recomputing a bunch of times.
  180. IsDeprecatedMacro = false;
  181. IsRestrictExpansion = false;
  182. }
  183. RecomputeNeedsHandleIdentifier();
  184. }
  185. }
  186. /// Returns true if this identifier was \#defined to some value at any
  187. /// moment. In this case there should be an entry for the identifier in the
  188. /// macro history table in Preprocessor.
  189. bool hadMacroDefinition() const {
  190. return HadMacro;
  191. }
  192. bool isDeprecatedMacro() const { return IsDeprecatedMacro; }
  193. void setIsDeprecatedMacro(bool Val) {
  194. if (IsDeprecatedMacro == Val)
  195. return;
  196. IsDeprecatedMacro = Val;
  197. if (Val)
  198. NeedsHandleIdentifier = true;
  199. else
  200. RecomputeNeedsHandleIdentifier();
  201. }
  202. bool isRestrictExpansion() const { return IsRestrictExpansion; }
  203. void setIsRestrictExpansion(bool Val) {
  204. if (IsRestrictExpansion == Val)
  205. return;
  206. IsRestrictExpansion = Val;
  207. if (Val)
  208. NeedsHandleIdentifier = true;
  209. else
  210. RecomputeNeedsHandleIdentifier();
  211. }
  212. bool isFinal() const { return IsFinal; }
  213. void setIsFinal(bool Val) { IsFinal = Val; }
  214. /// If this is a source-language token (e.g. 'for'), this API
  215. /// can be used to cause the lexer to map identifiers to source-language
  216. /// tokens.
  217. tok::TokenKind getTokenID() const { return (tok::TokenKind)TokenID; }
  218. /// True if revertTokenIDToIdentifier() was called.
  219. bool hasRevertedTokenIDToIdentifier() const { return RevertedTokenID; }
  220. /// Revert TokenID to tok::identifier; used for GNU libstdc++ 4.2
  221. /// compatibility.
  222. ///
  223. /// TokenID is normally read-only but there are 2 instances where we revert it
  224. /// to tok::identifier for libstdc++ 4.2. Keep track of when this happens
  225. /// using this method so we can inform serialization about it.
  226. void revertTokenIDToIdentifier() {
  227. assert(TokenID != tok::identifier && "Already at tok::identifier");
  228. TokenID = tok::identifier;
  229. RevertedTokenID = true;
  230. }
  231. void revertIdentifierToTokenID(tok::TokenKind TK) {
  232. assert(TokenID == tok::identifier && "Should be at tok::identifier");
  233. TokenID = TK;
  234. RevertedTokenID = false;
  235. }
  236. /// Return the preprocessor keyword ID for this identifier.
  237. ///
  238. /// For example, "define" will return tok::pp_define.
  239. tok::PPKeywordKind getPPKeywordID() const;
  240. /// Return the Objective-C keyword ID for the this identifier.
  241. ///
  242. /// For example, 'class' will return tok::objc_class if ObjC is enabled.
  243. tok::ObjCKeywordKind getObjCKeywordID() const {
  244. if (ObjCOrBuiltinID < tok::NUM_OBJC_KEYWORDS)
  245. return tok::ObjCKeywordKind(ObjCOrBuiltinID);
  246. else
  247. return tok::objc_not_keyword;
  248. }
  249. void setObjCKeywordID(tok::ObjCKeywordKind ID) { ObjCOrBuiltinID = ID; }
  250. /// Return a value indicating whether this is a builtin function.
  251. ///
  252. /// 0 is not-built-in. 1+ are specific builtin functions.
  253. unsigned getBuiltinID() const {
  254. if (ObjCOrBuiltinID >= tok::NUM_OBJC_KEYWORDS)
  255. return ObjCOrBuiltinID - tok::NUM_OBJC_KEYWORDS;
  256. else
  257. return 0;
  258. }
  259. void setBuiltinID(unsigned ID) {
  260. ObjCOrBuiltinID = ID + tok::NUM_OBJC_KEYWORDS;
  261. assert(ObjCOrBuiltinID - unsigned(tok::NUM_OBJC_KEYWORDS) == ID
  262. && "ID too large for field!");
  263. }
  264. unsigned getObjCOrBuiltinID() const { return ObjCOrBuiltinID; }
  265. void setObjCOrBuiltinID(unsigned ID) { ObjCOrBuiltinID = ID; }
  266. /// get/setExtension - Initialize information about whether or not this
  267. /// language token is an extension. This controls extension warnings, and is
  268. /// only valid if a custom token ID is set.
  269. bool isExtensionToken() const { return IsExtension; }
  270. void setIsExtensionToken(bool Val) {
  271. IsExtension = Val;
  272. if (Val)
  273. NeedsHandleIdentifier = true;
  274. else
  275. RecomputeNeedsHandleIdentifier();
  276. }
  277. /// is/setIsFutureCompatKeyword - Initialize information about whether or not
  278. /// this language token is a keyword in a newer or proposed Standard. This
  279. /// controls compatibility warnings, and is only true when not parsing the
  280. /// corresponding Standard. Once a compatibility problem has been diagnosed
  281. /// with this keyword, the flag will be cleared.
  282. bool isFutureCompatKeyword() const { return IsFutureCompatKeyword; }
  283. void setIsFutureCompatKeyword(bool Val) {
  284. IsFutureCompatKeyword = Val;
  285. if (Val)
  286. NeedsHandleIdentifier = true;
  287. else
  288. RecomputeNeedsHandleIdentifier();
  289. }
  290. /// setIsPoisoned - Mark this identifier as poisoned. After poisoning, the
  291. /// Preprocessor will emit an error every time this token is used.
  292. void setIsPoisoned(bool Value = true) {
  293. IsPoisoned = Value;
  294. if (Value)
  295. NeedsHandleIdentifier = true;
  296. else
  297. RecomputeNeedsHandleIdentifier();
  298. }
  299. /// Return true if this token has been poisoned.
  300. bool isPoisoned() const { return IsPoisoned; }
  301. /// isCPlusPlusOperatorKeyword/setIsCPlusPlusOperatorKeyword controls whether
  302. /// this identifier is a C++ alternate representation of an operator.
  303. void setIsCPlusPlusOperatorKeyword(bool Val = true) {
  304. IsCPPOperatorKeyword = Val;
  305. }
  306. bool isCPlusPlusOperatorKeyword() const { return IsCPPOperatorKeyword; }
  307. /// Return true if this token is a keyword in the specified language.
  308. bool isKeyword(const LangOptions &LangOpts) const;
  309. /// Return true if this token is a C++ keyword in the specified
  310. /// language.
  311. bool isCPlusPlusKeyword(const LangOptions &LangOpts) const;
  312. /// Get and set FETokenInfo. The language front-end is allowed to associate
  313. /// arbitrary metadata with this token.
  314. void *getFETokenInfo() const { return FETokenInfo; }
  315. void setFETokenInfo(void *T) { FETokenInfo = T; }
  316. /// Return true if the Preprocessor::HandleIdentifier must be called
  317. /// on a token of this identifier.
  318. ///
  319. /// If this returns false, we know that HandleIdentifier will not affect
  320. /// the token.
  321. bool isHandleIdentifierCase() const { return NeedsHandleIdentifier; }
  322. /// Return true if the identifier in its current state was loaded
  323. /// from an AST file.
  324. bool isFromAST() const { return IsFromAST; }
  325. void setIsFromAST() { IsFromAST = true; }
  326. /// Determine whether this identifier has changed since it was loaded
  327. /// from an AST file.
  328. bool hasChangedSinceDeserialization() const {
  329. return ChangedAfterLoad;
  330. }
  331. /// Note that this identifier has changed since it was loaded from
  332. /// an AST file.
  333. void setChangedSinceDeserialization() {
  334. ChangedAfterLoad = true;
  335. }
  336. /// Determine whether the frontend token information for this
  337. /// identifier has changed since it was loaded from an AST file.
  338. bool hasFETokenInfoChangedSinceDeserialization() const {
  339. return FEChangedAfterLoad;
  340. }
  341. /// Note that the frontend token information for this identifier has
  342. /// changed since it was loaded from an AST file.
  343. void setFETokenInfoChangedSinceDeserialization() {
  344. FEChangedAfterLoad = true;
  345. }
  346. /// Determine whether the information for this identifier is out of
  347. /// date with respect to the external source.
  348. bool isOutOfDate() const { return OutOfDate; }
  349. /// Set whether the information for this identifier is out of
  350. /// date with respect to the external source.
  351. void setOutOfDate(bool OOD) {
  352. OutOfDate = OOD;
  353. if (OOD)
  354. NeedsHandleIdentifier = true;
  355. else
  356. RecomputeNeedsHandleIdentifier();
  357. }
  358. /// Determine whether this is the contextual keyword \c import.
  359. bool isModulesImport() const { return IsModulesImport; }
  360. /// Set whether this identifier is the contextual keyword \c import.
  361. void setModulesImport(bool I) {
  362. IsModulesImport = I;
  363. if (I)
  364. NeedsHandleIdentifier = true;
  365. else
  366. RecomputeNeedsHandleIdentifier();
  367. }
  368. /// Determine whether this is the mangled name of an OpenMP variant.
  369. bool isMangledOpenMPVariantName() const { return IsMangledOpenMPVariantName; }
  370. /// Set whether this is the mangled name of an OpenMP variant.
  371. void setMangledOpenMPVariantName(bool I) { IsMangledOpenMPVariantName = I; }
  372. /// Return true if this identifier is an editor placeholder.
  373. ///
  374. /// Editor placeholders are produced by the code-completion engine and are
  375. /// represented as characters between '<#' and '#>' in the source code. An
  376. /// example of auto-completed call with a placeholder parameter is shown
  377. /// below:
  378. /// \code
  379. /// function(<#int x#>);
  380. /// \endcode
  381. bool isEditorPlaceholder() const {
  382. return getName().startswith("<#") && getName().endswith("#>");
  383. }
  384. /// Determine whether \p this is a name reserved for the implementation (C99
  385. /// 7.1.3, C++ [lib.global.names]).
  386. ReservedIdentifierStatus isReserved(const LangOptions &LangOpts) const;
  387. /// If the identifier is an "uglified" reserved name, return a cleaned form.
  388. /// e.g. _Foo => Foo. Otherwise, just returns the name.
  389. StringRef deuglifiedName() const;
  390. /// Provide less than operator for lexicographical sorting.
  391. bool operator<(const IdentifierInfo &RHS) const {
  392. return getName() < RHS.getName();
  393. }
  394. private:
  395. /// The Preprocessor::HandleIdentifier does several special (but rare)
  396. /// things to identifiers of various sorts. For example, it changes the
  397. /// \c for keyword token from tok::identifier to tok::for.
  398. ///
  399. /// This method is very tied to the definition of HandleIdentifier. Any
  400. /// change to it should be reflected here.
  401. void RecomputeNeedsHandleIdentifier() {
  402. NeedsHandleIdentifier = isPoisoned() || hasMacroDefinition() ||
  403. isExtensionToken() || isFutureCompatKeyword() ||
  404. isOutOfDate() || isModulesImport();
  405. }
  406. };
  407. /// An RAII object for [un]poisoning an identifier within a scope.
  408. ///
  409. /// \p II is allowed to be null, in which case objects of this type have
  410. /// no effect.
  411. class PoisonIdentifierRAIIObject {
  412. IdentifierInfo *const II;
  413. const bool OldValue;
  414. public:
  415. PoisonIdentifierRAIIObject(IdentifierInfo *II, bool NewValue)
  416. : II(II), OldValue(II ? II->isPoisoned() : false) {
  417. if(II)
  418. II->setIsPoisoned(NewValue);
  419. }
  420. ~PoisonIdentifierRAIIObject() {
  421. if(II)
  422. II->setIsPoisoned(OldValue);
  423. }
  424. };
  425. /// An iterator that walks over all of the known identifiers
  426. /// in the lookup table.
  427. ///
  428. /// Since this iterator uses an abstract interface via virtual
  429. /// functions, it uses an object-oriented interface rather than the
  430. /// more standard C++ STL iterator interface. In this OO-style
  431. /// iteration, the single function \c Next() provides dereference,
  432. /// advance, and end-of-sequence checking in a single
  433. /// operation. Subclasses of this iterator type will provide the
  434. /// actual functionality.
  435. class IdentifierIterator {
  436. protected:
  437. IdentifierIterator() = default;
  438. public:
  439. IdentifierIterator(const IdentifierIterator &) = delete;
  440. IdentifierIterator &operator=(const IdentifierIterator &) = delete;
  441. virtual ~IdentifierIterator();
  442. /// Retrieve the next string in the identifier table and
  443. /// advances the iterator for the following string.
  444. ///
  445. /// \returns The next string in the identifier table. If there is
  446. /// no such string, returns an empty \c StringRef.
  447. virtual StringRef Next() = 0;
  448. };
  449. /// Provides lookups to, and iteration over, IdentiferInfo objects.
  450. class IdentifierInfoLookup {
  451. public:
  452. virtual ~IdentifierInfoLookup();
  453. /// Return the IdentifierInfo for the specified named identifier.
  454. ///
  455. /// Unlike the version in IdentifierTable, this returns a pointer instead
  456. /// of a reference. If the pointer is null then the IdentifierInfo cannot
  457. /// be found.
  458. virtual IdentifierInfo* get(StringRef Name) = 0;
  459. /// Retrieve an iterator into the set of all identifiers
  460. /// known to this identifier lookup source.
  461. ///
  462. /// This routine provides access to all of the identifiers known to
  463. /// the identifier lookup, allowing access to the contents of the
  464. /// identifiers without introducing the overhead of constructing
  465. /// IdentifierInfo objects for each.
  466. ///
  467. /// \returns A new iterator into the set of known identifiers. The
  468. /// caller is responsible for deleting this iterator.
  469. virtual IdentifierIterator *getIdentifiers();
  470. };
  471. /// Implements an efficient mapping from strings to IdentifierInfo nodes.
  472. ///
  473. /// This has no other purpose, but this is an extremely performance-critical
  474. /// piece of the code, as each occurrence of every identifier goes through
  475. /// here when lexed.
  476. class IdentifierTable {
  477. // Shark shows that using MallocAllocator is *much* slower than using this
  478. // BumpPtrAllocator!
  479. using HashTableTy = llvm::StringMap<IdentifierInfo *, llvm::BumpPtrAllocator>;
  480. HashTableTy HashTable;
  481. IdentifierInfoLookup* ExternalLookup;
  482. public:
  483. /// Create the identifier table.
  484. explicit IdentifierTable(IdentifierInfoLookup *ExternalLookup = nullptr);
  485. /// Create the identifier table, populating it with info about the
  486. /// language keywords for the language specified by \p LangOpts.
  487. explicit IdentifierTable(const LangOptions &LangOpts,
  488. IdentifierInfoLookup *ExternalLookup = nullptr);
  489. /// Set the external identifier lookup mechanism.
  490. void setExternalIdentifierLookup(IdentifierInfoLookup *IILookup) {
  491. ExternalLookup = IILookup;
  492. }
  493. /// Retrieve the external identifier lookup object, if any.
  494. IdentifierInfoLookup *getExternalIdentifierLookup() const {
  495. return ExternalLookup;
  496. }
  497. llvm::BumpPtrAllocator& getAllocator() {
  498. return HashTable.getAllocator();
  499. }
  500. /// Return the identifier token info for the specified named
  501. /// identifier.
  502. IdentifierInfo &get(StringRef Name) {
  503. auto &Entry = *HashTable.insert(std::make_pair(Name, nullptr)).first;
  504. IdentifierInfo *&II = Entry.second;
  505. if (II) return *II;
  506. // No entry; if we have an external lookup, look there first.
  507. if (ExternalLookup) {
  508. II = ExternalLookup->get(Name);
  509. if (II)
  510. return *II;
  511. }
  512. // Lookups failed, make a new IdentifierInfo.
  513. void *Mem = getAllocator().Allocate<IdentifierInfo>();
  514. II = new (Mem) IdentifierInfo();
  515. // Make sure getName() knows how to find the IdentifierInfo
  516. // contents.
  517. II->Entry = &Entry;
  518. return *II;
  519. }
  520. IdentifierInfo &get(StringRef Name, tok::TokenKind TokenCode) {
  521. IdentifierInfo &II = get(Name);
  522. II.TokenID = TokenCode;
  523. assert(II.TokenID == (unsigned) TokenCode && "TokenCode too large");
  524. return II;
  525. }
  526. /// Gets an IdentifierInfo for the given name without consulting
  527. /// external sources.
  528. ///
  529. /// This is a version of get() meant for external sources that want to
  530. /// introduce or modify an identifier. If they called get(), they would
  531. /// likely end up in a recursion.
  532. IdentifierInfo &getOwn(StringRef Name) {
  533. auto &Entry = *HashTable.insert(std::make_pair(Name, nullptr)).first;
  534. IdentifierInfo *&II = Entry.second;
  535. if (II)
  536. return *II;
  537. // Lookups failed, make a new IdentifierInfo.
  538. void *Mem = getAllocator().Allocate<IdentifierInfo>();
  539. II = new (Mem) IdentifierInfo();
  540. // Make sure getName() knows how to find the IdentifierInfo
  541. // contents.
  542. II->Entry = &Entry;
  543. // If this is the 'import' contextual keyword, mark it as such.
  544. if (Name.equals("import"))
  545. II->setModulesImport(true);
  546. return *II;
  547. }
  548. using iterator = HashTableTy::const_iterator;
  549. using const_iterator = HashTableTy::const_iterator;
  550. iterator begin() const { return HashTable.begin(); }
  551. iterator end() const { return HashTable.end(); }
  552. unsigned size() const { return HashTable.size(); }
  553. iterator find(StringRef Name) const { return HashTable.find(Name); }
  554. /// Print some statistics to stderr that indicate how well the
  555. /// hashing is doing.
  556. void PrintStats() const;
  557. /// Populate the identifier table with info about the language keywords
  558. /// for the language specified by \p LangOpts.
  559. void AddKeywords(const LangOptions &LangOpts);
  560. };
  561. /// A family of Objective-C methods.
  562. ///
  563. /// These families have no inherent meaning in the language, but are
  564. /// nonetheless central enough in the existing implementations to
  565. /// merit direct AST support. While, in theory, arbitrary methods can
  566. /// be considered to form families, we focus here on the methods
  567. /// involving allocation and retain-count management, as these are the
  568. /// most "core" and the most likely to be useful to diverse clients
  569. /// without extra information.
  570. ///
  571. /// Both selectors and actual method declarations may be classified
  572. /// into families. Method families may impose additional restrictions
  573. /// beyond their selector name; for example, a method called '_init'
  574. /// that returns void is not considered to be in the 'init' family
  575. /// (but would be if it returned 'id'). It is also possible to
  576. /// explicitly change or remove a method's family. Therefore the
  577. /// method's family should be considered the single source of truth.
  578. enum ObjCMethodFamily {
  579. /// No particular method family.
  580. OMF_None,
  581. // Selectors in these families may have arbitrary arity, may be
  582. // written with arbitrary leading underscores, and may have
  583. // additional CamelCase "words" in their first selector chunk
  584. // following the family name.
  585. OMF_alloc,
  586. OMF_copy,
  587. OMF_init,
  588. OMF_mutableCopy,
  589. OMF_new,
  590. // These families are singletons consisting only of the nullary
  591. // selector with the given name.
  592. OMF_autorelease,
  593. OMF_dealloc,
  594. OMF_finalize,
  595. OMF_release,
  596. OMF_retain,
  597. OMF_retainCount,
  598. OMF_self,
  599. OMF_initialize,
  600. // performSelector families
  601. OMF_performSelector
  602. };
  603. /// Enough bits to store any enumerator in ObjCMethodFamily or
  604. /// InvalidObjCMethodFamily.
  605. enum { ObjCMethodFamilyBitWidth = 4 };
  606. /// An invalid value of ObjCMethodFamily.
  607. enum { InvalidObjCMethodFamily = (1 << ObjCMethodFamilyBitWidth) - 1 };
  608. /// A family of Objective-C methods.
  609. ///
  610. /// These are family of methods whose result type is initially 'id', but
  611. /// but are candidate for the result type to be changed to 'instancetype'.
  612. enum ObjCInstanceTypeFamily {
  613. OIT_None,
  614. OIT_Array,
  615. OIT_Dictionary,
  616. OIT_Singleton,
  617. OIT_Init,
  618. OIT_ReturnsSelf
  619. };
  620. enum ObjCStringFormatFamily {
  621. SFF_None,
  622. SFF_NSString,
  623. SFF_CFString
  624. };
  625. /// Smart pointer class that efficiently represents Objective-C method
  626. /// names.
  627. ///
  628. /// This class will either point to an IdentifierInfo or a
  629. /// MultiKeywordSelector (which is private). This enables us to optimize
  630. /// selectors that take no arguments and selectors that take 1 argument, which
  631. /// accounts for 78% of all selectors in Cocoa.h.
  632. class Selector {
  633. friend class Diagnostic;
  634. friend class SelectorTable; // only the SelectorTable can create these
  635. friend class DeclarationName; // and the AST's DeclarationName.
  636. enum IdentifierInfoFlag {
  637. // Empty selector = 0. Note that these enumeration values must
  638. // correspond to the enumeration values of DeclarationName::StoredNameKind
  639. ZeroArg = 0x01,
  640. OneArg = 0x02,
  641. MultiArg = 0x07,
  642. ArgFlags = 0x07
  643. };
  644. /// A pointer to the MultiKeywordSelector or IdentifierInfo. We use the low
  645. /// three bits of InfoPtr to store an IdentifierInfoFlag. Note that in any
  646. /// case IdentifierInfo and MultiKeywordSelector are already aligned to
  647. /// 8 bytes even on 32 bits archs because of DeclarationName.
  648. uintptr_t InfoPtr = 0;
  649. Selector(IdentifierInfo *II, unsigned nArgs) {
  650. InfoPtr = reinterpret_cast<uintptr_t>(II);
  651. assert((InfoPtr & ArgFlags) == 0 &&"Insufficiently aligned IdentifierInfo");
  652. assert(nArgs < 2 && "nArgs not equal to 0/1");
  653. InfoPtr |= nArgs+1;
  654. }
  655. Selector(MultiKeywordSelector *SI) {
  656. InfoPtr = reinterpret_cast<uintptr_t>(SI);
  657. assert((InfoPtr & ArgFlags) == 0 &&"Insufficiently aligned IdentifierInfo");
  658. InfoPtr |= MultiArg;
  659. }
  660. IdentifierInfo *getAsIdentifierInfo() const {
  661. if (getIdentifierInfoFlag() < MultiArg)
  662. return reinterpret_cast<IdentifierInfo *>(InfoPtr & ~ArgFlags);
  663. return nullptr;
  664. }
  665. MultiKeywordSelector *getMultiKeywordSelector() const {
  666. return reinterpret_cast<MultiKeywordSelector *>(InfoPtr & ~ArgFlags);
  667. }
  668. unsigned getIdentifierInfoFlag() const {
  669. return InfoPtr & ArgFlags;
  670. }
  671. static ObjCMethodFamily getMethodFamilyImpl(Selector sel);
  672. static ObjCStringFormatFamily getStringFormatFamilyImpl(Selector sel);
  673. public:
  674. /// The default ctor should only be used when creating data structures that
  675. /// will contain selectors.
  676. Selector() = default;
  677. explicit Selector(uintptr_t V) : InfoPtr(V) {}
  678. /// operator==/!= - Indicate whether the specified selectors are identical.
  679. bool operator==(Selector RHS) const {
  680. return InfoPtr == RHS.InfoPtr;
  681. }
  682. bool operator!=(Selector RHS) const {
  683. return InfoPtr != RHS.InfoPtr;
  684. }
  685. void *getAsOpaquePtr() const {
  686. return reinterpret_cast<void*>(InfoPtr);
  687. }
  688. /// Determine whether this is the empty selector.
  689. bool isNull() const { return InfoPtr == 0; }
  690. // Predicates to identify the selector type.
  691. bool isKeywordSelector() const {
  692. return getIdentifierInfoFlag() != ZeroArg;
  693. }
  694. bool isUnarySelector() const {
  695. return getIdentifierInfoFlag() == ZeroArg;
  696. }
  697. /// If this selector is the specific keyword selector described by Names.
  698. bool isKeywordSelector(ArrayRef<StringRef> Names) const;
  699. /// If this selector is the specific unary selector described by Name.
  700. bool isUnarySelector(StringRef Name) const;
  701. unsigned getNumArgs() const;
  702. /// Retrieve the identifier at a given position in the selector.
  703. ///
  704. /// Note that the identifier pointer returned may be NULL. Clients that only
  705. /// care about the text of the identifier string, and not the specific,
  706. /// uniqued identifier pointer, should use \c getNameForSlot(), which returns
  707. /// an empty string when the identifier pointer would be NULL.
  708. ///
  709. /// \param argIndex The index for which we want to retrieve the identifier.
  710. /// This index shall be less than \c getNumArgs() unless this is a keyword
  711. /// selector, in which case 0 is the only permissible value.
  712. ///
  713. /// \returns the uniqued identifier for this slot, or NULL if this slot has
  714. /// no corresponding identifier.
  715. IdentifierInfo *getIdentifierInfoForSlot(unsigned argIndex) const;
  716. /// Retrieve the name at a given position in the selector.
  717. ///
  718. /// \param argIndex The index for which we want to retrieve the name.
  719. /// This index shall be less than \c getNumArgs() unless this is a keyword
  720. /// selector, in which case 0 is the only permissible value.
  721. ///
  722. /// \returns the name for this slot, which may be the empty string if no
  723. /// name was supplied.
  724. StringRef getNameForSlot(unsigned argIndex) const;
  725. /// Derive the full selector name (e.g. "foo:bar:") and return
  726. /// it as an std::string.
  727. std::string getAsString() const;
  728. /// Prints the full selector name (e.g. "foo:bar:").
  729. void print(llvm::raw_ostream &OS) const;
  730. void dump() const;
  731. /// Derive the conventional family of this method.
  732. ObjCMethodFamily getMethodFamily() const {
  733. return getMethodFamilyImpl(*this);
  734. }
  735. ObjCStringFormatFamily getStringFormatFamily() const {
  736. return getStringFormatFamilyImpl(*this);
  737. }
  738. static Selector getEmptyMarker() {
  739. return Selector(uintptr_t(-1));
  740. }
  741. static Selector getTombstoneMarker() {
  742. return Selector(uintptr_t(-2));
  743. }
  744. static ObjCInstanceTypeFamily getInstTypeMethodFamily(Selector sel);
  745. };
  746. /// This table allows us to fully hide how we implement
  747. /// multi-keyword caching.
  748. class SelectorTable {
  749. // Actually a SelectorTableImpl
  750. void *Impl;
  751. public:
  752. SelectorTable();
  753. SelectorTable(const SelectorTable &) = delete;
  754. SelectorTable &operator=(const SelectorTable &) = delete;
  755. ~SelectorTable();
  756. /// Can create any sort of selector.
  757. ///
  758. /// \p NumArgs indicates whether this is a no argument selector "foo", a
  759. /// single argument selector "foo:" or multi-argument "foo:bar:".
  760. Selector getSelector(unsigned NumArgs, IdentifierInfo **IIV);
  761. Selector getUnarySelector(IdentifierInfo *ID) {
  762. return Selector(ID, 1);
  763. }
  764. Selector getNullarySelector(IdentifierInfo *ID) {
  765. return Selector(ID, 0);
  766. }
  767. /// Return the total amount of memory allocated for managing selectors.
  768. size_t getTotalMemory() const;
  769. /// Return the default setter name for the given identifier.
  770. ///
  771. /// This is "set" + \p Name where the initial character of \p Name
  772. /// has been capitalized.
  773. static SmallString<64> constructSetterName(StringRef Name);
  774. /// Return the default setter selector for the given identifier.
  775. ///
  776. /// This is "set" + \p Name where the initial character of \p Name
  777. /// has been capitalized.
  778. static Selector constructSetterSelector(IdentifierTable &Idents,
  779. SelectorTable &SelTable,
  780. const IdentifierInfo *Name);
  781. /// Return the property name for the given setter selector.
  782. static std::string getPropertyNameFromSetterSelector(Selector Sel);
  783. };
  784. namespace detail {
  785. /// DeclarationNameExtra is used as a base of various uncommon special names.
  786. /// This class is needed since DeclarationName has not enough space to store
  787. /// the kind of every possible names. Therefore the kind of common names is
  788. /// stored directly in DeclarationName, and the kind of uncommon names is
  789. /// stored in DeclarationNameExtra. It is aligned to 8 bytes because
  790. /// DeclarationName needs the lower 3 bits to store the kind of common names.
  791. /// DeclarationNameExtra is tightly coupled to DeclarationName and any change
  792. /// here is very likely to require changes in DeclarationName(Table).
  793. class alignas(IdentifierInfoAlignment) DeclarationNameExtra {
  794. friend class clang::DeclarationName;
  795. friend class clang::DeclarationNameTable;
  796. protected:
  797. /// The kind of "extra" information stored in the DeclarationName. See
  798. /// @c ExtraKindOrNumArgs for an explanation of how these enumerator values
  799. /// are used. Note that DeclarationName depends on the numerical values
  800. /// of the enumerators in this enum. See DeclarationName::StoredNameKind
  801. /// for more info.
  802. enum ExtraKind {
  803. CXXDeductionGuideName,
  804. CXXLiteralOperatorName,
  805. CXXUsingDirective,
  806. ObjCMultiArgSelector
  807. };
  808. /// ExtraKindOrNumArgs has one of the following meaning:
  809. /// * The kind of an uncommon C++ special name. This DeclarationNameExtra
  810. /// is in this case in fact either a CXXDeductionGuideNameExtra or
  811. /// a CXXLiteralOperatorIdName.
  812. ///
  813. /// * It may be also name common to C++ using-directives (CXXUsingDirective),
  814. ///
  815. /// * Otherwise it is ObjCMultiArgSelector+NumArgs, where NumArgs is
  816. /// the number of arguments in the Objective-C selector, in which
  817. /// case the DeclarationNameExtra is also a MultiKeywordSelector.
  818. unsigned ExtraKindOrNumArgs;
  819. DeclarationNameExtra(ExtraKind Kind) : ExtraKindOrNumArgs(Kind) {}
  820. DeclarationNameExtra(unsigned NumArgs)
  821. : ExtraKindOrNumArgs(ObjCMultiArgSelector + NumArgs) {}
  822. /// Return the corresponding ExtraKind.
  823. ExtraKind getKind() const {
  824. return static_cast<ExtraKind>(ExtraKindOrNumArgs >
  825. (unsigned)ObjCMultiArgSelector
  826. ? (unsigned)ObjCMultiArgSelector
  827. : ExtraKindOrNumArgs);
  828. }
  829. /// Return the number of arguments in an ObjC selector. Only valid when this
  830. /// is indeed an ObjCMultiArgSelector.
  831. unsigned getNumArgs() const {
  832. assert(ExtraKindOrNumArgs >= (unsigned)ObjCMultiArgSelector &&
  833. "getNumArgs called but this is not an ObjC selector!");
  834. return ExtraKindOrNumArgs - (unsigned)ObjCMultiArgSelector;
  835. }
  836. };
  837. } // namespace detail
  838. } // namespace clang
  839. namespace llvm {
  840. /// Define DenseMapInfo so that Selectors can be used as keys in DenseMap and
  841. /// DenseSets.
  842. template <>
  843. struct DenseMapInfo<clang::Selector> {
  844. static clang::Selector getEmptyKey() {
  845. return clang::Selector::getEmptyMarker();
  846. }
  847. static clang::Selector getTombstoneKey() {
  848. return clang::Selector::getTombstoneMarker();
  849. }
  850. static unsigned getHashValue(clang::Selector S);
  851. static bool isEqual(clang::Selector LHS, clang::Selector RHS) {
  852. return LHS == RHS;
  853. }
  854. };
  855. template<>
  856. struct PointerLikeTypeTraits<clang::Selector> {
  857. static const void *getAsVoidPointer(clang::Selector P) {
  858. return P.getAsOpaquePtr();
  859. }
  860. static clang::Selector getFromVoidPointer(const void *P) {
  861. return clang::Selector(reinterpret_cast<uintptr_t>(P));
  862. }
  863. static constexpr int NumLowBitsAvailable = 0;
  864. };
  865. // Provide PointerLikeTypeTraits for IdentifierInfo pointers, which
  866. // are not guaranteed to be 8-byte aligned.
  867. template<>
  868. struct PointerLikeTypeTraits<clang::IdentifierInfo*> {
  869. static void *getAsVoidPointer(clang::IdentifierInfo* P) {
  870. return P;
  871. }
  872. static clang::IdentifierInfo *getFromVoidPointer(void *P) {
  873. return static_cast<clang::IdentifierInfo*>(P);
  874. }
  875. static constexpr int NumLowBitsAvailable = 1;
  876. };
  877. template<>
  878. struct PointerLikeTypeTraits<const clang::IdentifierInfo*> {
  879. static const void *getAsVoidPointer(const clang::IdentifierInfo* P) {
  880. return P;
  881. }
  882. static const clang::IdentifierInfo *getFromVoidPointer(const void *P) {
  883. return static_cast<const clang::IdentifierInfo*>(P);
  884. }
  885. static constexpr int NumLowBitsAvailable = 1;
  886. };
  887. } // namespace llvm
  888. #endif // LLVM_CLANG_BASIC_IDENTIFIERTABLE_H
  889. #ifdef __GNUC__
  890. #pragma GCC diagnostic pop
  891. #endif