DeclarationName.h 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- DeclarationName.h - Representation of declaration names --*- 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. // This file declares the DeclarationName and DeclarationNameTable classes.
  15. //
  16. //===----------------------------------------------------------------------===//
  17. #ifndef LLVM_CLANG_AST_DECLARATIONNAME_H
  18. #define LLVM_CLANG_AST_DECLARATIONNAME_H
  19. #include "clang/AST/Type.h"
  20. #include "clang/Basic/Diagnostic.h"
  21. #include "clang/Basic/IdentifierTable.h"
  22. #include "clang/Basic/OperatorKinds.h"
  23. #include "clang/Basic/PartialDiagnostic.h"
  24. #include "clang/Basic/SourceLocation.h"
  25. #include "llvm/ADT/DenseMapInfo.h"
  26. #include "llvm/ADT/FoldingSet.h"
  27. #include "llvm/Support/Compiler.h"
  28. #include "llvm/Support/type_traits.h"
  29. #include <cassert>
  30. #include <cstdint>
  31. #include <cstring>
  32. #include <string>
  33. namespace clang {
  34. class ASTContext;
  35. template <typename> class CanQual;
  36. class DeclarationName;
  37. class DeclarationNameTable;
  38. struct PrintingPolicy;
  39. class TemplateDecl;
  40. class TypeSourceInfo;
  41. using CanQualType = CanQual<Type>;
  42. namespace detail {
  43. /// CXXSpecialNameExtra records the type associated with one of the "special"
  44. /// kinds of declaration names in C++, e.g., constructors, destructors, and
  45. /// conversion functions. Note that CXXSpecialName is used for C++ constructor,
  46. /// destructor and conversion functions, but the actual kind is not stored in
  47. /// CXXSpecialName. Instead we use three different FoldingSet<CXXSpecialName>
  48. /// in DeclarationNameTable.
  49. class alignas(IdentifierInfoAlignment) CXXSpecialNameExtra
  50. : public llvm::FoldingSetNode {
  51. friend class clang::DeclarationName;
  52. friend class clang::DeclarationNameTable;
  53. /// The type associated with this declaration name.
  54. QualType Type;
  55. /// Extra information associated with this declaration name that
  56. /// can be used by the front end. All bits are really needed
  57. /// so it is not possible to stash something in the low order bits.
  58. void *FETokenInfo;
  59. CXXSpecialNameExtra(QualType QT) : Type(QT), FETokenInfo(nullptr) {}
  60. public:
  61. void Profile(llvm::FoldingSetNodeID &ID) {
  62. ID.AddPointer(Type.getAsOpaquePtr());
  63. }
  64. };
  65. /// Contains extra information for the name of a C++ deduction guide.
  66. class alignas(IdentifierInfoAlignment) CXXDeductionGuideNameExtra
  67. : public detail::DeclarationNameExtra,
  68. public llvm::FoldingSetNode {
  69. friend class clang::DeclarationName;
  70. friend class clang::DeclarationNameTable;
  71. /// The template named by the deduction guide.
  72. TemplateDecl *Template;
  73. /// Extra information associated with this operator name that
  74. /// can be used by the front end. All bits are really needed
  75. /// so it is not possible to stash something in the low order bits.
  76. void *FETokenInfo;
  77. CXXDeductionGuideNameExtra(TemplateDecl *TD)
  78. : DeclarationNameExtra(CXXDeductionGuideName), Template(TD),
  79. FETokenInfo(nullptr) {}
  80. public:
  81. void Profile(llvm::FoldingSetNodeID &ID) { ID.AddPointer(Template); }
  82. };
  83. /// Contains extra information for the name of an overloaded operator
  84. /// in C++, such as "operator+. This do not includes literal or conversion
  85. /// operators. For literal operators see CXXLiteralOperatorIdName and for
  86. /// conversion operators see CXXSpecialNameExtra.
  87. class alignas(IdentifierInfoAlignment) CXXOperatorIdName {
  88. friend class clang::DeclarationName;
  89. friend class clang::DeclarationNameTable;
  90. /// The kind of this operator.
  91. OverloadedOperatorKind Kind = OO_None;
  92. /// Extra information associated with this operator name that
  93. /// can be used by the front end. All bits are really needed
  94. /// so it is not possible to stash something in the low order bits.
  95. void *FETokenInfo = nullptr;
  96. };
  97. /// Contains the actual identifier that makes up the
  98. /// name of a C++ literal operator.
  99. class alignas(IdentifierInfoAlignment) CXXLiteralOperatorIdName
  100. : public detail::DeclarationNameExtra,
  101. public llvm::FoldingSetNode {
  102. friend class clang::DeclarationName;
  103. friend class clang::DeclarationNameTable;
  104. IdentifierInfo *ID;
  105. /// Extra information associated with this operator name that
  106. /// can be used by the front end. All bits are really needed
  107. /// so it is not possible to stash something in the low order bits.
  108. void *FETokenInfo;
  109. CXXLiteralOperatorIdName(IdentifierInfo *II)
  110. : DeclarationNameExtra(CXXLiteralOperatorName), ID(II),
  111. FETokenInfo(nullptr) {}
  112. public:
  113. void Profile(llvm::FoldingSetNodeID &FSID) { FSID.AddPointer(ID); }
  114. };
  115. } // namespace detail
  116. /// The name of a declaration. In the common case, this just stores
  117. /// an IdentifierInfo pointer to a normal name. However, it also provides
  118. /// encodings for Objective-C selectors (optimizing zero- and one-argument
  119. /// selectors, which make up 78% percent of all selectors in Cocoa.h),
  120. /// special C++ names for constructors, destructors, and conversion functions,
  121. /// and C++ overloaded operators.
  122. class DeclarationName {
  123. friend class DeclarationNameTable;
  124. friend class NamedDecl;
  125. /// StoredNameKind represent the kind of name that is actually stored in the
  126. /// upper bits of the Ptr field. This is only used internally.
  127. ///
  128. /// NameKind, StoredNameKind, and DeclarationNameExtra::ExtraKind
  129. /// must satisfy the following properties. These properties enable
  130. /// efficient conversion between the various kinds.
  131. ///
  132. /// * The first seven enumerators of StoredNameKind must have the same
  133. /// numerical value as the first seven enumerators of NameKind.
  134. /// This enable efficient conversion between the two enumerations
  135. /// in the usual case.
  136. ///
  137. /// * The enumerations values of DeclarationNameExtra::ExtraKind must start
  138. /// at zero, and correspond to the numerical value of the first non-inline
  139. /// enumeration values of NameKind minus an offset. This makes conversion
  140. /// between DeclarationNameExtra::ExtraKind and NameKind possible with
  141. /// a single addition/substraction.
  142. ///
  143. /// * The enumeration values of Selector::IdentifierInfoFlag must correspond
  144. /// to the relevant enumeration values of StoredNameKind.
  145. /// More specifically:
  146. /// * ZeroArg == StoredObjCZeroArgSelector,
  147. /// * OneArg == StoredObjCOneArgSelector,
  148. /// * MultiArg == StoredDeclarationNameExtra
  149. ///
  150. /// * PtrMask must mask the low 3 bits of Ptr.
  151. enum StoredNameKind {
  152. StoredIdentifier = 0,
  153. StoredObjCZeroArgSelector = Selector::ZeroArg,
  154. StoredObjCOneArgSelector = Selector::OneArg,
  155. StoredCXXConstructorName = 3,
  156. StoredCXXDestructorName = 4,
  157. StoredCXXConversionFunctionName = 5,
  158. StoredCXXOperatorName = 6,
  159. StoredDeclarationNameExtra = Selector::MultiArg,
  160. PtrMask = 7,
  161. UncommonNameKindOffset = 8
  162. };
  163. static_assert(alignof(IdentifierInfo) >= 8 &&
  164. alignof(detail::DeclarationNameExtra) >= 8 &&
  165. alignof(detail::CXXSpecialNameExtra) >= 8 &&
  166. alignof(detail::CXXOperatorIdName) >= 8 &&
  167. alignof(detail::CXXDeductionGuideNameExtra) >= 8 &&
  168. alignof(detail::CXXLiteralOperatorIdName) >= 8,
  169. "The various classes that DeclarationName::Ptr can point to"
  170. " must be at least aligned to 8 bytes!");
  171. public:
  172. /// The kind of the name stored in this DeclarationName.
  173. /// The first 7 enumeration values are stored inline and correspond
  174. /// to frequently used kinds. The rest is stored in DeclarationNameExtra
  175. /// and correspond to infrequently used kinds.
  176. enum NameKind {
  177. Identifier = StoredIdentifier,
  178. ObjCZeroArgSelector = StoredObjCZeroArgSelector,
  179. ObjCOneArgSelector = StoredObjCOneArgSelector,
  180. CXXConstructorName = StoredCXXConstructorName,
  181. CXXDestructorName = StoredCXXDestructorName,
  182. CXXConversionFunctionName = StoredCXXConversionFunctionName,
  183. CXXOperatorName = StoredCXXOperatorName,
  184. CXXDeductionGuideName = UncommonNameKindOffset +
  185. detail::DeclarationNameExtra::CXXDeductionGuideName,
  186. CXXLiteralOperatorName =
  187. UncommonNameKindOffset +
  188. detail::DeclarationNameExtra::CXXLiteralOperatorName,
  189. CXXUsingDirective = UncommonNameKindOffset +
  190. detail::DeclarationNameExtra::CXXUsingDirective,
  191. ObjCMultiArgSelector = UncommonNameKindOffset +
  192. detail::DeclarationNameExtra::ObjCMultiArgSelector
  193. };
  194. private:
  195. /// The lowest three bits of Ptr are used to express what kind of name
  196. /// we're actually storing, using the values of StoredNameKind. Depending
  197. /// on the kind of name this is, the upper bits of Ptr may have one
  198. /// of several different meanings:
  199. ///
  200. /// StoredIdentifier - The name is a normal identifier, and Ptr is
  201. /// a normal IdentifierInfo pointer.
  202. ///
  203. /// StoredObjCZeroArgSelector - The name is an Objective-C
  204. /// selector with zero arguments, and Ptr is an IdentifierInfo
  205. /// pointer pointing to the selector name.
  206. ///
  207. /// StoredObjCOneArgSelector - The name is an Objective-C selector
  208. /// with one argument, and Ptr is an IdentifierInfo pointer
  209. /// pointing to the selector name.
  210. ///
  211. /// StoredCXXConstructorName - The name of a C++ constructor,
  212. /// Ptr points to a CXXSpecialNameExtra.
  213. ///
  214. /// StoredCXXDestructorName - The name of a C++ destructor,
  215. /// Ptr points to a CXXSpecialNameExtra.
  216. ///
  217. /// StoredCXXConversionFunctionName - The name of a C++ conversion function,
  218. /// Ptr points to a CXXSpecialNameExtra.
  219. ///
  220. /// StoredCXXOperatorName - The name of an overloaded C++ operator,
  221. /// Ptr points to a CXXOperatorIdName.
  222. ///
  223. /// StoredDeclarationNameExtra - Ptr is actually a pointer to a
  224. /// DeclarationNameExtra structure, whose first value will tell us
  225. /// whether this is an Objective-C selector, C++ deduction guide,
  226. /// C++ literal operator, or C++ using directive.
  227. uintptr_t Ptr = 0;
  228. StoredNameKind getStoredNameKind() const {
  229. return static_cast<StoredNameKind>(Ptr & PtrMask);
  230. }
  231. void *getPtr() const { return reinterpret_cast<void *>(Ptr & ~PtrMask); }
  232. void setPtrAndKind(const void *P, StoredNameKind Kind) {
  233. uintptr_t PAsInteger = reinterpret_cast<uintptr_t>(P);
  234. assert((Kind & ~PtrMask) == 0 &&
  235. "Invalid StoredNameKind in setPtrAndKind!");
  236. assert((PAsInteger & PtrMask) == 0 &&
  237. "Improperly aligned pointer in setPtrAndKind!");
  238. Ptr = PAsInteger | Kind;
  239. }
  240. /// Construct a declaration name from a DeclarationNameExtra.
  241. DeclarationName(detail::DeclarationNameExtra *Name) {
  242. setPtrAndKind(Name, StoredDeclarationNameExtra);
  243. }
  244. /// Construct a declaration name from a CXXSpecialNameExtra.
  245. DeclarationName(detail::CXXSpecialNameExtra *Name,
  246. StoredNameKind StoredKind) {
  247. assert((StoredKind == StoredCXXConstructorName ||
  248. StoredKind == StoredCXXDestructorName ||
  249. StoredKind == StoredCXXConversionFunctionName) &&
  250. "Invalid StoredNameKind when constructing a DeclarationName"
  251. " from a CXXSpecialNameExtra!");
  252. setPtrAndKind(Name, StoredKind);
  253. }
  254. /// Construct a DeclarationName from a CXXOperatorIdName.
  255. DeclarationName(detail::CXXOperatorIdName *Name) {
  256. setPtrAndKind(Name, StoredCXXOperatorName);
  257. }
  258. /// Assert that the stored pointer points to an IdentifierInfo and return it.
  259. IdentifierInfo *castAsIdentifierInfo() const {
  260. assert((getStoredNameKind() == StoredIdentifier) &&
  261. "DeclarationName does not store an IdentifierInfo!");
  262. return static_cast<IdentifierInfo *>(getPtr());
  263. }
  264. /// Assert that the stored pointer points to a DeclarationNameExtra
  265. /// and return it.
  266. detail::DeclarationNameExtra *castAsExtra() const {
  267. assert((getStoredNameKind() == StoredDeclarationNameExtra) &&
  268. "DeclarationName does not store an Extra structure!");
  269. return static_cast<detail::DeclarationNameExtra *>(getPtr());
  270. }
  271. /// Assert that the stored pointer points to a CXXSpecialNameExtra
  272. /// and return it.
  273. detail::CXXSpecialNameExtra *castAsCXXSpecialNameExtra() const {
  274. assert((getStoredNameKind() == StoredCXXConstructorName ||
  275. getStoredNameKind() == StoredCXXDestructorName ||
  276. getStoredNameKind() == StoredCXXConversionFunctionName) &&
  277. "DeclarationName does not store a CXXSpecialNameExtra!");
  278. return static_cast<detail::CXXSpecialNameExtra *>(getPtr());
  279. }
  280. /// Assert that the stored pointer points to a CXXOperatorIdName
  281. /// and return it.
  282. detail::CXXOperatorIdName *castAsCXXOperatorIdName() const {
  283. assert((getStoredNameKind() == StoredCXXOperatorName) &&
  284. "DeclarationName does not store a CXXOperatorIdName!");
  285. return static_cast<detail::CXXOperatorIdName *>(getPtr());
  286. }
  287. /// Assert that the stored pointer points to a CXXDeductionGuideNameExtra
  288. /// and return it.
  289. detail::CXXDeductionGuideNameExtra *castAsCXXDeductionGuideNameExtra() const {
  290. assert(getNameKind() == CXXDeductionGuideName &&
  291. "DeclarationName does not store a CXXDeductionGuideNameExtra!");
  292. return static_cast<detail::CXXDeductionGuideNameExtra *>(getPtr());
  293. }
  294. /// Assert that the stored pointer points to a CXXLiteralOperatorIdName
  295. /// and return it.
  296. detail::CXXLiteralOperatorIdName *castAsCXXLiteralOperatorIdName() const {
  297. assert(getNameKind() == CXXLiteralOperatorName &&
  298. "DeclarationName does not store a CXXLiteralOperatorIdName!");
  299. return static_cast<detail::CXXLiteralOperatorIdName *>(getPtr());
  300. }
  301. /// Get and set the FETokenInfo in the less common cases where the
  302. /// declaration name do not point to an identifier.
  303. void *getFETokenInfoSlow() const;
  304. void setFETokenInfoSlow(void *T);
  305. public:
  306. /// Construct an empty declaration name.
  307. DeclarationName() { setPtrAndKind(nullptr, StoredIdentifier); }
  308. /// Construct a declaration name from an IdentifierInfo *.
  309. DeclarationName(const IdentifierInfo *II) {
  310. setPtrAndKind(II, StoredIdentifier);
  311. }
  312. /// Construct a declaration name from an Objective-C selector.
  313. DeclarationName(Selector Sel) : Ptr(Sel.InfoPtr) {}
  314. /// Returns the name for all C++ using-directives.
  315. static DeclarationName getUsingDirectiveName() {
  316. // Single instance of DeclarationNameExtra for using-directive
  317. static detail::DeclarationNameExtra UDirExtra(
  318. detail::DeclarationNameExtra::CXXUsingDirective);
  319. return DeclarationName(&UDirExtra);
  320. }
  321. /// Evaluates true when this declaration name is non-empty.
  322. explicit operator bool() const {
  323. return getPtr() || (getStoredNameKind() != StoredIdentifier);
  324. }
  325. /// Evaluates true when this declaration name is empty.
  326. bool isEmpty() const { return !*this; }
  327. /// Predicate functions for querying what type of name this is.
  328. bool isIdentifier() const { return getStoredNameKind() == StoredIdentifier; }
  329. bool isObjCZeroArgSelector() const {
  330. return getStoredNameKind() == StoredObjCZeroArgSelector;
  331. }
  332. bool isObjCOneArgSelector() const {
  333. return getStoredNameKind() == StoredObjCOneArgSelector;
  334. }
  335. /// Determine what kind of name this is.
  336. NameKind getNameKind() const {
  337. // We rely on the fact that the first 7 NameKind and StoredNameKind
  338. // have the same numerical value. This makes the usual case efficient.
  339. StoredNameKind StoredKind = getStoredNameKind();
  340. if (StoredKind != StoredDeclarationNameExtra)
  341. return static_cast<NameKind>(StoredKind);
  342. // We have to consult DeclarationNameExtra. We rely on the fact that the
  343. // enumeration values of ExtraKind correspond to the enumeration values of
  344. // NameKind minus an offset of UncommonNameKindOffset.
  345. unsigned ExtraKind = castAsExtra()->getKind();
  346. return static_cast<NameKind>(UncommonNameKindOffset + ExtraKind);
  347. }
  348. /// Determines whether the name itself is dependent, e.g., because it
  349. /// involves a C++ type that is itself dependent.
  350. ///
  351. /// Note that this does not capture all of the notions of "dependent name",
  352. /// because an identifier can be a dependent name if it is used as the
  353. /// callee in a call expression with dependent arguments.
  354. bool isDependentName() const;
  355. /// Retrieve the human-readable string for this name.
  356. std::string getAsString() const;
  357. /// Retrieve the IdentifierInfo * stored in this declaration name,
  358. /// or null if this declaration name isn't a simple identifier.
  359. IdentifierInfo *getAsIdentifierInfo() const {
  360. if (isIdentifier())
  361. return castAsIdentifierInfo();
  362. return nullptr;
  363. }
  364. /// Get the representation of this declaration name as an opaque integer.
  365. uintptr_t getAsOpaqueInteger() const { return Ptr; }
  366. /// Get the representation of this declaration name as an opaque pointer.
  367. void *getAsOpaquePtr() const { return reinterpret_cast<void *>(Ptr); }
  368. /// Get a declaration name from an opaque pointer returned by getAsOpaquePtr.
  369. static DeclarationName getFromOpaquePtr(void *P) {
  370. DeclarationName N;
  371. N.Ptr = reinterpret_cast<uintptr_t>(P);
  372. return N;
  373. }
  374. /// Get a declaration name from an opaque integer
  375. /// returned by getAsOpaqueInteger.
  376. static DeclarationName getFromOpaqueInteger(uintptr_t P) {
  377. DeclarationName N;
  378. N.Ptr = P;
  379. return N;
  380. }
  381. /// If this name is one of the C++ names (of a constructor, destructor,
  382. /// or conversion function), return the type associated with that name.
  383. QualType getCXXNameType() const {
  384. if (getStoredNameKind() == StoredCXXConstructorName ||
  385. getStoredNameKind() == StoredCXXDestructorName ||
  386. getStoredNameKind() == StoredCXXConversionFunctionName) {
  387. assert(getPtr() && "getCXXNameType on a null DeclarationName!");
  388. return castAsCXXSpecialNameExtra()->Type;
  389. }
  390. return QualType();
  391. }
  392. /// If this name is the name of a C++ deduction guide, return the
  393. /// template associated with that name.
  394. TemplateDecl *getCXXDeductionGuideTemplate() const {
  395. if (getNameKind() == CXXDeductionGuideName) {
  396. assert(getPtr() &&
  397. "getCXXDeductionGuideTemplate on a null DeclarationName!");
  398. return castAsCXXDeductionGuideNameExtra()->Template;
  399. }
  400. return nullptr;
  401. }
  402. /// If this name is the name of an overloadable operator in C++
  403. /// (e.g., @c operator+), retrieve the kind of overloaded operator.
  404. OverloadedOperatorKind getCXXOverloadedOperator() const {
  405. if (getStoredNameKind() == StoredCXXOperatorName) {
  406. assert(getPtr() && "getCXXOverloadedOperator on a null DeclarationName!");
  407. return castAsCXXOperatorIdName()->Kind;
  408. }
  409. return OO_None;
  410. }
  411. /// If this name is the name of a literal operator,
  412. /// retrieve the identifier associated with it.
  413. IdentifierInfo *getCXXLiteralIdentifier() const {
  414. if (getNameKind() == CXXLiteralOperatorName) {
  415. assert(getPtr() && "getCXXLiteralIdentifier on a null DeclarationName!");
  416. return castAsCXXLiteralOperatorIdName()->ID;
  417. }
  418. return nullptr;
  419. }
  420. /// Get the Objective-C selector stored in this declaration name.
  421. Selector getObjCSelector() const {
  422. assert((getNameKind() == ObjCZeroArgSelector ||
  423. getNameKind() == ObjCOneArgSelector ||
  424. getNameKind() == ObjCMultiArgSelector || !getPtr()) &&
  425. "Not a selector!");
  426. return Selector(Ptr);
  427. }
  428. /// Get and set FETokenInfo. The language front-end is allowed to associate
  429. /// arbitrary metadata with some kinds of declaration names, including normal
  430. /// identifiers and C++ constructors, destructors, and conversion functions.
  431. void *getFETokenInfo() const {
  432. assert(getPtr() && "getFETokenInfo on an empty DeclarationName!");
  433. if (getStoredNameKind() == StoredIdentifier)
  434. return castAsIdentifierInfo()->getFETokenInfo();
  435. return getFETokenInfoSlow();
  436. }
  437. void setFETokenInfo(void *T) {
  438. assert(getPtr() && "setFETokenInfo on an empty DeclarationName!");
  439. if (getStoredNameKind() == StoredIdentifier)
  440. castAsIdentifierInfo()->setFETokenInfo(T);
  441. else
  442. setFETokenInfoSlow(T);
  443. }
  444. /// Determine whether the specified names are identical.
  445. friend bool operator==(DeclarationName LHS, DeclarationName RHS) {
  446. return LHS.Ptr == RHS.Ptr;
  447. }
  448. /// Determine whether the specified names are different.
  449. friend bool operator!=(DeclarationName LHS, DeclarationName RHS) {
  450. return LHS.Ptr != RHS.Ptr;
  451. }
  452. static DeclarationName getEmptyMarker() {
  453. DeclarationName Name;
  454. Name.Ptr = uintptr_t(-1);
  455. return Name;
  456. }
  457. static DeclarationName getTombstoneMarker() {
  458. DeclarationName Name;
  459. Name.Ptr = uintptr_t(-2);
  460. return Name;
  461. }
  462. static int compare(DeclarationName LHS, DeclarationName RHS);
  463. void print(raw_ostream &OS, const PrintingPolicy &Policy) const;
  464. void dump() const;
  465. };
  466. raw_ostream &operator<<(raw_ostream &OS, DeclarationName N);
  467. /// Ordering on two declaration names. If both names are identifiers,
  468. /// this provides a lexicographical ordering.
  469. inline bool operator<(DeclarationName LHS, DeclarationName RHS) {
  470. return DeclarationName::compare(LHS, RHS) < 0;
  471. }
  472. /// Ordering on two declaration names. If both names are identifiers,
  473. /// this provides a lexicographical ordering.
  474. inline bool operator>(DeclarationName LHS, DeclarationName RHS) {
  475. return DeclarationName::compare(LHS, RHS) > 0;
  476. }
  477. /// Ordering on two declaration names. If both names are identifiers,
  478. /// this provides a lexicographical ordering.
  479. inline bool operator<=(DeclarationName LHS, DeclarationName RHS) {
  480. return DeclarationName::compare(LHS, RHS) <= 0;
  481. }
  482. /// Ordering on two declaration names. If both names are identifiers,
  483. /// this provides a lexicographical ordering.
  484. inline bool operator>=(DeclarationName LHS, DeclarationName RHS) {
  485. return DeclarationName::compare(LHS, RHS) >= 0;
  486. }
  487. /// DeclarationNameTable is used to store and retrieve DeclarationName
  488. /// instances for the various kinds of declaration names, e.g., normal
  489. /// identifiers, C++ constructor names, etc. This class contains
  490. /// uniqued versions of each of the C++ special names, which can be
  491. /// retrieved using its member functions (e.g., getCXXConstructorName).
  492. class DeclarationNameTable {
  493. /// Used to allocate elements in the FoldingSets below.
  494. const ASTContext &Ctx;
  495. /// Manage the uniqued CXXSpecialNameExtra representing C++ constructors.
  496. /// getCXXConstructorName and getCXXSpecialName can be used to obtain
  497. /// a DeclarationName from the corresponding type of the constructor.
  498. llvm::FoldingSet<detail::CXXSpecialNameExtra> CXXConstructorNames;
  499. /// Manage the uniqued CXXSpecialNameExtra representing C++ destructors.
  500. /// getCXXDestructorName and getCXXSpecialName can be used to obtain
  501. /// a DeclarationName from the corresponding type of the destructor.
  502. llvm::FoldingSet<detail::CXXSpecialNameExtra> CXXDestructorNames;
  503. /// Manage the uniqued CXXSpecialNameExtra representing C++ conversion
  504. /// functions. getCXXConversionFunctionName and getCXXSpecialName can be
  505. /// used to obtain a DeclarationName from the corresponding type of the
  506. /// conversion function.
  507. llvm::FoldingSet<detail::CXXSpecialNameExtra> CXXConversionFunctionNames;
  508. /// Manage the uniqued CXXOperatorIdName, which contain extra information
  509. /// for the name of overloaded C++ operators. getCXXOperatorName
  510. /// can be used to obtain a DeclarationName from the operator kind.
  511. detail::CXXOperatorIdName CXXOperatorNames[NUM_OVERLOADED_OPERATORS];
  512. /// Manage the uniqued CXXLiteralOperatorIdName, which contain extra
  513. /// information for the name of C++ literal operators.
  514. /// getCXXLiteralOperatorName can be used to obtain a DeclarationName
  515. /// from the corresponding IdentifierInfo.
  516. llvm::FoldingSet<detail::CXXLiteralOperatorIdName> CXXLiteralOperatorNames;
  517. /// Manage the uniqued CXXDeductionGuideNameExtra, which contain
  518. /// extra information for the name of a C++ deduction guide.
  519. /// getCXXDeductionGuideName can be used to obtain a DeclarationName
  520. /// from the corresponding template declaration.
  521. llvm::FoldingSet<detail::CXXDeductionGuideNameExtra> CXXDeductionGuideNames;
  522. public:
  523. DeclarationNameTable(const ASTContext &C);
  524. DeclarationNameTable(const DeclarationNameTable &) = delete;
  525. DeclarationNameTable &operator=(const DeclarationNameTable &) = delete;
  526. DeclarationNameTable(DeclarationNameTable &&) = delete;
  527. DeclarationNameTable &operator=(DeclarationNameTable &&) = delete;
  528. ~DeclarationNameTable() = default;
  529. /// Create a declaration name that is a simple identifier.
  530. DeclarationName getIdentifier(const IdentifierInfo *ID) {
  531. return DeclarationName(ID);
  532. }
  533. /// Returns the name of a C++ constructor for the given Type.
  534. DeclarationName getCXXConstructorName(CanQualType Ty);
  535. /// Returns the name of a C++ destructor for the given Type.
  536. DeclarationName getCXXDestructorName(CanQualType Ty);
  537. /// Returns the name of a C++ deduction guide for the given template.
  538. DeclarationName getCXXDeductionGuideName(TemplateDecl *TD);
  539. /// Returns the name of a C++ conversion function for the given Type.
  540. DeclarationName getCXXConversionFunctionName(CanQualType Ty);
  541. /// Returns a declaration name for special kind of C++ name,
  542. /// e.g., for a constructor, destructor, or conversion function.
  543. /// Kind must be one of:
  544. /// * DeclarationName::CXXConstructorName,
  545. /// * DeclarationName::CXXDestructorName or
  546. /// * DeclarationName::CXXConversionFunctionName
  547. DeclarationName getCXXSpecialName(DeclarationName::NameKind Kind,
  548. CanQualType Ty);
  549. /// Get the name of the overloadable C++ operator corresponding to Op.
  550. DeclarationName getCXXOperatorName(OverloadedOperatorKind Op) {
  551. return DeclarationName(&CXXOperatorNames[Op]);
  552. }
  553. /// Get the name of the literal operator function with II as the identifier.
  554. DeclarationName getCXXLiteralOperatorName(IdentifierInfo *II);
  555. };
  556. /// DeclarationNameLoc - Additional source/type location info
  557. /// for a declaration name. Needs a DeclarationName in order
  558. /// to be interpreted correctly.
  559. class DeclarationNameLoc {
  560. // The source location for identifier stored elsewhere.
  561. // struct {} Identifier;
  562. // Type info for constructors, destructors and conversion functions.
  563. // Locations (if any) for the tilde (destructor) or operator keyword
  564. // (conversion) are stored elsewhere.
  565. struct NT {
  566. TypeSourceInfo *TInfo;
  567. };
  568. // The location (if any) of the operator keyword is stored elsewhere.
  569. struct CXXOpName {
  570. SourceLocation::UIntTy BeginOpNameLoc;
  571. SourceLocation::UIntTy EndOpNameLoc;
  572. };
  573. // The location (if any) of the operator keyword is stored elsewhere.
  574. struct CXXLitOpName {
  575. SourceLocation::UIntTy OpNameLoc;
  576. };
  577. // struct {} CXXUsingDirective;
  578. // struct {} ObjCZeroArgSelector;
  579. // struct {} ObjCOneArgSelector;
  580. // struct {} ObjCMultiArgSelector;
  581. union {
  582. struct NT NamedType;
  583. struct CXXOpName CXXOperatorName;
  584. struct CXXLitOpName CXXLiteralOperatorName;
  585. };
  586. void setNamedTypeLoc(TypeSourceInfo *TInfo) { NamedType.TInfo = TInfo; }
  587. void setCXXOperatorNameRange(SourceRange Range) {
  588. CXXOperatorName.BeginOpNameLoc = Range.getBegin().getRawEncoding();
  589. CXXOperatorName.EndOpNameLoc = Range.getEnd().getRawEncoding();
  590. }
  591. void setCXXLiteralOperatorNameLoc(SourceLocation Loc) {
  592. CXXLiteralOperatorName.OpNameLoc = Loc.getRawEncoding();
  593. }
  594. public:
  595. DeclarationNameLoc(DeclarationName Name);
  596. // FIXME: this should go away once all DNLocs are properly initialized.
  597. DeclarationNameLoc() { memset((void*) this, 0, sizeof(*this)); }
  598. /// Returns the source type info. Assumes that the object stores location
  599. /// information of a constructor, destructor or conversion operator.
  600. TypeSourceInfo *getNamedTypeInfo() const { return NamedType.TInfo; }
  601. /// Return the beginning location of the getCXXOperatorNameRange() range.
  602. SourceLocation getCXXOperatorNameBeginLoc() const {
  603. return SourceLocation::getFromRawEncoding(CXXOperatorName.BeginOpNameLoc);
  604. }
  605. /// Return the end location of the getCXXOperatorNameRange() range.
  606. SourceLocation getCXXOperatorNameEndLoc() const {
  607. return SourceLocation::getFromRawEncoding(CXXOperatorName.EndOpNameLoc);
  608. }
  609. /// Return the range of the operator name (without the operator keyword).
  610. /// Assumes that the object stores location information of a (non-literal)
  611. /// operator.
  612. SourceRange getCXXOperatorNameRange() const {
  613. return SourceRange(getCXXOperatorNameBeginLoc(),
  614. getCXXOperatorNameEndLoc());
  615. }
  616. /// Return the location of the literal operator name (without the operator
  617. /// keyword). Assumes that the object stores location information of a literal
  618. /// operator.
  619. SourceLocation getCXXLiteralOperatorNameLoc() const {
  620. return SourceLocation::getFromRawEncoding(CXXLiteralOperatorName.OpNameLoc);
  621. }
  622. /// Construct location information for a constructor, destructor or conversion
  623. /// operator.
  624. static DeclarationNameLoc makeNamedTypeLoc(TypeSourceInfo *TInfo) {
  625. DeclarationNameLoc DNL;
  626. DNL.setNamedTypeLoc(TInfo);
  627. return DNL;
  628. }
  629. /// Construct location information for a non-literal C++ operator.
  630. static DeclarationNameLoc makeCXXOperatorNameLoc(SourceLocation BeginLoc,
  631. SourceLocation EndLoc) {
  632. return makeCXXOperatorNameLoc(SourceRange(BeginLoc, EndLoc));
  633. }
  634. /// Construct location information for a non-literal C++ operator.
  635. static DeclarationNameLoc makeCXXOperatorNameLoc(SourceRange Range) {
  636. DeclarationNameLoc DNL;
  637. DNL.setCXXOperatorNameRange(Range);
  638. return DNL;
  639. }
  640. /// Construct location information for a literal C++ operator.
  641. static DeclarationNameLoc makeCXXLiteralOperatorNameLoc(SourceLocation Loc) {
  642. DeclarationNameLoc DNL;
  643. DNL.setCXXLiteralOperatorNameLoc(Loc);
  644. return DNL;
  645. }
  646. };
  647. /// DeclarationNameInfo - A collector data type for bundling together
  648. /// a DeclarationName and the correspnding source/type location info.
  649. struct DeclarationNameInfo {
  650. private:
  651. /// Name - The declaration name, also encoding name kind.
  652. DeclarationName Name;
  653. /// Loc - The main source location for the declaration name.
  654. SourceLocation NameLoc;
  655. /// Info - Further source/type location info for special kinds of names.
  656. DeclarationNameLoc LocInfo;
  657. public:
  658. // FIXME: remove it.
  659. DeclarationNameInfo() = default;
  660. DeclarationNameInfo(DeclarationName Name, SourceLocation NameLoc)
  661. : Name(Name), NameLoc(NameLoc), LocInfo(Name) {}
  662. DeclarationNameInfo(DeclarationName Name, SourceLocation NameLoc,
  663. DeclarationNameLoc LocInfo)
  664. : Name(Name), NameLoc(NameLoc), LocInfo(LocInfo) {}
  665. /// getName - Returns the embedded declaration name.
  666. DeclarationName getName() const { return Name; }
  667. /// setName - Sets the embedded declaration name.
  668. void setName(DeclarationName N) { Name = N; }
  669. /// getLoc - Returns the main location of the declaration name.
  670. SourceLocation getLoc() const { return NameLoc; }
  671. /// setLoc - Sets the main location of the declaration name.
  672. void setLoc(SourceLocation L) { NameLoc = L; }
  673. const DeclarationNameLoc &getInfo() const { return LocInfo; }
  674. void setInfo(const DeclarationNameLoc &Info) { LocInfo = Info; }
  675. /// getNamedTypeInfo - Returns the source type info associated to
  676. /// the name. Assumes it is a constructor, destructor or conversion.
  677. TypeSourceInfo *getNamedTypeInfo() const {
  678. if (Name.getNameKind() != DeclarationName::CXXConstructorName &&
  679. Name.getNameKind() != DeclarationName::CXXDestructorName &&
  680. Name.getNameKind() != DeclarationName::CXXConversionFunctionName)
  681. return nullptr;
  682. return LocInfo.getNamedTypeInfo();
  683. }
  684. /// setNamedTypeInfo - Sets the source type info associated to
  685. /// the name. Assumes it is a constructor, destructor or conversion.
  686. void setNamedTypeInfo(TypeSourceInfo *TInfo) {
  687. assert(Name.getNameKind() == DeclarationName::CXXConstructorName ||
  688. Name.getNameKind() == DeclarationName::CXXDestructorName ||
  689. Name.getNameKind() == DeclarationName::CXXConversionFunctionName);
  690. LocInfo = DeclarationNameLoc::makeNamedTypeLoc(TInfo);
  691. }
  692. /// getCXXOperatorNameRange - Gets the range of the operator name
  693. /// (without the operator keyword). Assumes it is a (non-literal) operator.
  694. SourceRange getCXXOperatorNameRange() const {
  695. if (Name.getNameKind() != DeclarationName::CXXOperatorName)
  696. return SourceRange();
  697. return LocInfo.getCXXOperatorNameRange();
  698. }
  699. /// setCXXOperatorNameRange - Sets the range of the operator name
  700. /// (without the operator keyword). Assumes it is a C++ operator.
  701. void setCXXOperatorNameRange(SourceRange R) {
  702. assert(Name.getNameKind() == DeclarationName::CXXOperatorName);
  703. LocInfo = DeclarationNameLoc::makeCXXOperatorNameLoc(R);
  704. }
  705. /// getCXXLiteralOperatorNameLoc - Returns the location of the literal
  706. /// operator name (not the operator keyword).
  707. /// Assumes it is a literal operator.
  708. SourceLocation getCXXLiteralOperatorNameLoc() const {
  709. if (Name.getNameKind() != DeclarationName::CXXLiteralOperatorName)
  710. return SourceLocation();
  711. return LocInfo.getCXXLiteralOperatorNameLoc();
  712. }
  713. /// setCXXLiteralOperatorNameLoc - Sets the location of the literal
  714. /// operator name (not the operator keyword).
  715. /// Assumes it is a literal operator.
  716. void setCXXLiteralOperatorNameLoc(SourceLocation Loc) {
  717. assert(Name.getNameKind() == DeclarationName::CXXLiteralOperatorName);
  718. LocInfo = DeclarationNameLoc::makeCXXLiteralOperatorNameLoc(Loc);
  719. }
  720. /// Determine whether this name involves a template parameter.
  721. bool isInstantiationDependent() const;
  722. /// Determine whether this name contains an unexpanded
  723. /// parameter pack.
  724. bool containsUnexpandedParameterPack() const;
  725. /// getAsString - Retrieve the human-readable string for this name.
  726. std::string getAsString() const;
  727. /// printName - Print the human-readable name to a stream.
  728. void printName(raw_ostream &OS, PrintingPolicy Policy) const;
  729. /// getBeginLoc - Retrieve the location of the first token.
  730. SourceLocation getBeginLoc() const { return NameLoc; }
  731. /// getSourceRange - The range of the declaration name.
  732. SourceRange getSourceRange() const LLVM_READONLY {
  733. return SourceRange(getBeginLoc(), getEndLoc());
  734. }
  735. SourceLocation getEndLoc() const LLVM_READONLY {
  736. SourceLocation EndLoc = getEndLocPrivate();
  737. return EndLoc.isValid() ? EndLoc : getBeginLoc();
  738. }
  739. private:
  740. SourceLocation getEndLocPrivate() const;
  741. };
  742. /// Insertion operator for partial diagnostics. This allows binding
  743. /// DeclarationName's into a partial diagnostic with <<.
  744. inline const StreamingDiagnostic &operator<<(const StreamingDiagnostic &PD,
  745. DeclarationName N) {
  746. PD.AddTaggedVal(N.getAsOpaqueInteger(),
  747. DiagnosticsEngine::ak_declarationname);
  748. return PD;
  749. }
  750. raw_ostream &operator<<(raw_ostream &OS, DeclarationNameInfo DNInfo);
  751. } // namespace clang
  752. namespace llvm {
  753. /// Define DenseMapInfo so that DeclarationNames can be used as keys
  754. /// in DenseMap and DenseSets.
  755. template<>
  756. struct DenseMapInfo<clang::DeclarationName> {
  757. static inline clang::DeclarationName getEmptyKey() {
  758. return clang::DeclarationName::getEmptyMarker();
  759. }
  760. static inline clang::DeclarationName getTombstoneKey() {
  761. return clang::DeclarationName::getTombstoneMarker();
  762. }
  763. static unsigned getHashValue(clang::DeclarationName Name) {
  764. return DenseMapInfo<void*>::getHashValue(Name.getAsOpaquePtr());
  765. }
  766. static inline bool
  767. isEqual(clang::DeclarationName LHS, clang::DeclarationName RHS) {
  768. return LHS == RHS;
  769. }
  770. };
  771. template <> struct PointerLikeTypeTraits<clang::DeclarationName> {
  772. static inline void *getAsVoidPointer(clang::DeclarationName P) {
  773. return P.getAsOpaquePtr();
  774. }
  775. static inline clang::DeclarationName getFromVoidPointer(void *P) {
  776. return clang::DeclarationName::getFromOpaquePtr(P);
  777. }
  778. static constexpr int NumLowBitsAvailable = 0;
  779. };
  780. } // namespace llvm
  781. // The definition of AssumedTemplateStorage is factored out of TemplateName to
  782. // resolve a cyclic dependency between it and DeclarationName (via Type).
  783. namespace clang {
  784. /// A structure for storing the information associated with a name that has
  785. /// been assumed to be a template name (despite finding no TemplateDecls).
  786. class AssumedTemplateStorage : public UncommonTemplateNameStorage {
  787. friend class ASTContext;
  788. AssumedTemplateStorage(DeclarationName Name)
  789. : UncommonTemplateNameStorage(Assumed, 0), Name(Name) {}
  790. DeclarationName Name;
  791. public:
  792. /// Get the name of the template.
  793. DeclarationName getDeclName() const { return Name; }
  794. };
  795. } // namespace clang
  796. #endif // LLVM_CLANG_AST_DECLARATIONNAME_H
  797. #ifdef __GNUC__
  798. #pragma GCC diagnostic pop
  799. #endif