TemplateName.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- TemplateName.h - C++ Template Name Representation --------*- 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 defines the TemplateName interface and subclasses.
  15. //
  16. //===----------------------------------------------------------------------===//
  17. #ifndef LLVM_CLANG_AST_TEMPLATENAME_H
  18. #define LLVM_CLANG_AST_TEMPLATENAME_H
  19. #include "clang/AST/DependenceFlags.h"
  20. #include "clang/AST/NestedNameSpecifier.h"
  21. #include "clang/Basic/LLVM.h"
  22. #include "llvm/ADT/FoldingSet.h"
  23. #include "llvm/ADT/PointerIntPair.h"
  24. #include "llvm/ADT/PointerUnion.h"
  25. #include "llvm/Support/PointerLikeTypeTraits.h"
  26. #include <cassert>
  27. namespace clang {
  28. class ASTContext;
  29. class DependentTemplateName;
  30. class IdentifierInfo;
  31. class NamedDecl;
  32. class NestedNameSpecifier;
  33. enum OverloadedOperatorKind : int;
  34. class OverloadedTemplateStorage;
  35. class AssumedTemplateStorage;
  36. struct PrintingPolicy;
  37. class QualifiedTemplateName;
  38. class SubstTemplateTemplateParmPackStorage;
  39. class SubstTemplateTemplateParmStorage;
  40. class TemplateArgument;
  41. class TemplateDecl;
  42. class TemplateTemplateParmDecl;
  43. /// Implementation class used to describe either a set of overloaded
  44. /// template names or an already-substituted template template parameter pack.
  45. class UncommonTemplateNameStorage {
  46. protected:
  47. enum Kind {
  48. Overloaded,
  49. Assumed, // defined in DeclarationName.h
  50. SubstTemplateTemplateParm,
  51. SubstTemplateTemplateParmPack
  52. };
  53. struct BitsTag {
  54. /// A Kind.
  55. unsigned Kind : 2;
  56. /// The number of stored templates or template arguments,
  57. /// depending on which subclass we have.
  58. unsigned Size : 30;
  59. };
  60. union {
  61. struct BitsTag Bits;
  62. void *PointerAlignment;
  63. };
  64. UncommonTemplateNameStorage(Kind kind, unsigned size) {
  65. Bits.Kind = kind;
  66. Bits.Size = size;
  67. }
  68. public:
  69. unsigned size() const { return Bits.Size; }
  70. OverloadedTemplateStorage *getAsOverloadedStorage() {
  71. return Bits.Kind == Overloaded
  72. ? reinterpret_cast<OverloadedTemplateStorage *>(this)
  73. : nullptr;
  74. }
  75. AssumedTemplateStorage *getAsAssumedTemplateName() {
  76. return Bits.Kind == Assumed
  77. ? reinterpret_cast<AssumedTemplateStorage *>(this)
  78. : nullptr;
  79. }
  80. SubstTemplateTemplateParmStorage *getAsSubstTemplateTemplateParm() {
  81. return Bits.Kind == SubstTemplateTemplateParm
  82. ? reinterpret_cast<SubstTemplateTemplateParmStorage *>(this)
  83. : nullptr;
  84. }
  85. SubstTemplateTemplateParmPackStorage *getAsSubstTemplateTemplateParmPack() {
  86. return Bits.Kind == SubstTemplateTemplateParmPack
  87. ? reinterpret_cast<SubstTemplateTemplateParmPackStorage *>(this)
  88. : nullptr;
  89. }
  90. };
  91. /// A structure for storing the information associated with an
  92. /// overloaded template name.
  93. class OverloadedTemplateStorage : public UncommonTemplateNameStorage {
  94. friend class ASTContext;
  95. OverloadedTemplateStorage(unsigned size)
  96. : UncommonTemplateNameStorage(Overloaded, size) {}
  97. NamedDecl **getStorage() {
  98. return reinterpret_cast<NamedDecl **>(this + 1);
  99. }
  100. NamedDecl * const *getStorage() const {
  101. return reinterpret_cast<NamedDecl *const *>(this + 1);
  102. }
  103. public:
  104. using iterator = NamedDecl *const *;
  105. iterator begin() const { return getStorage(); }
  106. iterator end() const { return getStorage() + size(); }
  107. llvm::ArrayRef<NamedDecl*> decls() const {
  108. return llvm::makeArrayRef(begin(), end());
  109. }
  110. };
  111. /// A structure for storing an already-substituted template template
  112. /// parameter pack.
  113. ///
  114. /// This kind of template names occurs when the parameter pack has been
  115. /// provided with a template template argument pack in a context where its
  116. /// enclosing pack expansion could not be fully expanded.
  117. class SubstTemplateTemplateParmPackStorage
  118. : public UncommonTemplateNameStorage, public llvm::FoldingSetNode
  119. {
  120. TemplateTemplateParmDecl *Parameter;
  121. const TemplateArgument *Arguments;
  122. public:
  123. SubstTemplateTemplateParmPackStorage(TemplateTemplateParmDecl *Parameter,
  124. unsigned Size,
  125. const TemplateArgument *Arguments)
  126. : UncommonTemplateNameStorage(SubstTemplateTemplateParmPack, Size),
  127. Parameter(Parameter), Arguments(Arguments) {}
  128. /// Retrieve the template template parameter pack being substituted.
  129. TemplateTemplateParmDecl *getParameterPack() const {
  130. return Parameter;
  131. }
  132. /// Retrieve the template template argument pack with which this
  133. /// parameter was substituted.
  134. TemplateArgument getArgumentPack() const;
  135. void Profile(llvm::FoldingSetNodeID &ID, ASTContext &Context);
  136. static void Profile(llvm::FoldingSetNodeID &ID,
  137. ASTContext &Context,
  138. TemplateTemplateParmDecl *Parameter,
  139. const TemplateArgument &ArgPack);
  140. };
  141. /// Represents a C++ template name within the type system.
  142. ///
  143. /// A C++ template name refers to a template within the C++ type
  144. /// system. In most cases, a template name is simply a reference to a
  145. /// class template, e.g.
  146. ///
  147. /// \code
  148. /// template<typename T> class X { };
  149. ///
  150. /// X<int> xi;
  151. /// \endcode
  152. ///
  153. /// Here, the 'X' in \c X<int> is a template name that refers to the
  154. /// declaration of the class template X, above. Template names can
  155. /// also refer to function templates, C++0x template aliases, etc.
  156. ///
  157. /// Some template names are dependent. For example, consider:
  158. ///
  159. /// \code
  160. /// template<typename MetaFun, typename T1, typename T2> struct apply2 {
  161. /// typedef typename MetaFun::template apply<T1, T2>::type type;
  162. /// };
  163. /// \endcode
  164. ///
  165. /// Here, "apply" is treated as a template name within the typename
  166. /// specifier in the typedef. "apply" is a nested template, and can
  167. /// only be understood in the context of
  168. class TemplateName {
  169. using StorageType =
  170. llvm::PointerUnion<TemplateDecl *, UncommonTemplateNameStorage *,
  171. QualifiedTemplateName *, DependentTemplateName *>;
  172. StorageType Storage;
  173. explicit TemplateName(void *Ptr);
  174. public:
  175. // Kind of name that is actually stored.
  176. enum NameKind {
  177. /// A single template declaration.
  178. Template,
  179. /// A set of overloaded template declarations.
  180. OverloadedTemplate,
  181. /// An unqualified-id that has been assumed to name a function template
  182. /// that will be found by ADL.
  183. AssumedTemplate,
  184. /// A qualified template name, where the qualification is kept
  185. /// to describe the source code as written.
  186. QualifiedTemplate,
  187. /// A dependent template name that has not been resolved to a
  188. /// template (or set of templates).
  189. DependentTemplate,
  190. /// A template template parameter that has been substituted
  191. /// for some other template name.
  192. SubstTemplateTemplateParm,
  193. /// A template template parameter pack that has been substituted for
  194. /// a template template argument pack, but has not yet been expanded into
  195. /// individual arguments.
  196. SubstTemplateTemplateParmPack
  197. };
  198. TemplateName() = default;
  199. explicit TemplateName(TemplateDecl *Template);
  200. explicit TemplateName(OverloadedTemplateStorage *Storage);
  201. explicit TemplateName(AssumedTemplateStorage *Storage);
  202. explicit TemplateName(SubstTemplateTemplateParmStorage *Storage);
  203. explicit TemplateName(SubstTemplateTemplateParmPackStorage *Storage);
  204. explicit TemplateName(QualifiedTemplateName *Qual);
  205. explicit TemplateName(DependentTemplateName *Dep);
  206. /// Determine whether this template name is NULL.
  207. bool isNull() const;
  208. // Get the kind of name that is actually stored.
  209. NameKind getKind() const;
  210. /// Retrieve the underlying template declaration that
  211. /// this template name refers to, if known.
  212. ///
  213. /// \returns The template declaration that this template name refers
  214. /// to, if any. If the template name does not refer to a specific
  215. /// declaration because it is a dependent name, or if it refers to a
  216. /// set of function templates, returns NULL.
  217. TemplateDecl *getAsTemplateDecl() const;
  218. /// Retrieve the underlying, overloaded function template
  219. /// declarations that this template name refers to, if known.
  220. ///
  221. /// \returns The set of overloaded function templates that this template
  222. /// name refers to, if known. If the template name does not refer to a
  223. /// specific set of function templates because it is a dependent name or
  224. /// refers to a single template, returns NULL.
  225. OverloadedTemplateStorage *getAsOverloadedTemplate() const;
  226. /// Retrieve information on a name that has been assumed to be a
  227. /// template-name in order to permit a call via ADL.
  228. AssumedTemplateStorage *getAsAssumedTemplateName() const;
  229. /// Retrieve the substituted template template parameter, if
  230. /// known.
  231. ///
  232. /// \returns The storage for the substituted template template parameter,
  233. /// if known. Otherwise, returns NULL.
  234. SubstTemplateTemplateParmStorage *getAsSubstTemplateTemplateParm() const;
  235. /// Retrieve the substituted template template parameter pack, if
  236. /// known.
  237. ///
  238. /// \returns The storage for the substituted template template parameter pack,
  239. /// if known. Otherwise, returns NULL.
  240. SubstTemplateTemplateParmPackStorage *
  241. getAsSubstTemplateTemplateParmPack() const;
  242. /// Retrieve the underlying qualified template name
  243. /// structure, if any.
  244. QualifiedTemplateName *getAsQualifiedTemplateName() const;
  245. /// Retrieve the underlying dependent template name
  246. /// structure, if any.
  247. DependentTemplateName *getAsDependentTemplateName() const;
  248. TemplateName getUnderlying() const;
  249. /// Get the template name to substitute when this template name is used as a
  250. /// template template argument. This refers to the most recent declaration of
  251. /// the template, including any default template arguments.
  252. TemplateName getNameToSubstitute() const;
  253. TemplateNameDependence getDependence() const;
  254. /// Determines whether this is a dependent template name.
  255. bool isDependent() const;
  256. /// Determines whether this is a template name that somehow
  257. /// depends on a template parameter.
  258. bool isInstantiationDependent() const;
  259. /// Determines whether this template name contains an
  260. /// unexpanded parameter pack (for C++0x variadic templates).
  261. bool containsUnexpandedParameterPack() const;
  262. enum class Qualified { None, AsWritten, Fully };
  263. /// Print the template name.
  264. ///
  265. /// \param OS the output stream to which the template name will be
  266. /// printed.
  267. ///
  268. /// \param Qual print the (Qualified::None) simple name,
  269. /// (Qualified::AsWritten) any written (possibly partial) qualifier, or
  270. /// (Qualified::Fully) the fully qualified name.
  271. void print(raw_ostream &OS, const PrintingPolicy &Policy,
  272. Qualified Qual = Qualified::AsWritten) const;
  273. /// Debugging aid that dumps the template name.
  274. void dump(raw_ostream &OS) const;
  275. /// Debugging aid that dumps the template name to standard
  276. /// error.
  277. void dump() const;
  278. void Profile(llvm::FoldingSetNodeID &ID) {
  279. ID.AddPointer(Storage.getOpaqueValue());
  280. }
  281. /// Retrieve the template name as a void pointer.
  282. void *getAsVoidPointer() const { return Storage.getOpaqueValue(); }
  283. /// Build a template name from a void pointer.
  284. static TemplateName getFromVoidPointer(void *Ptr) {
  285. return TemplateName(Ptr);
  286. }
  287. };
  288. /// Insertion operator for diagnostics. This allows sending TemplateName's
  289. /// into a diagnostic with <<.
  290. const StreamingDiagnostic &operator<<(const StreamingDiagnostic &DB,
  291. TemplateName N);
  292. /// A structure for storing the information associated with a
  293. /// substituted template template parameter.
  294. class SubstTemplateTemplateParmStorage
  295. : public UncommonTemplateNameStorage, public llvm::FoldingSetNode {
  296. friend class ASTContext;
  297. TemplateTemplateParmDecl *Parameter;
  298. TemplateName Replacement;
  299. SubstTemplateTemplateParmStorage(TemplateTemplateParmDecl *parameter,
  300. TemplateName replacement)
  301. : UncommonTemplateNameStorage(SubstTemplateTemplateParm, 0),
  302. Parameter(parameter), Replacement(replacement) {}
  303. public:
  304. TemplateTemplateParmDecl *getParameter() const { return Parameter; }
  305. TemplateName getReplacement() const { return Replacement; }
  306. void Profile(llvm::FoldingSetNodeID &ID);
  307. static void Profile(llvm::FoldingSetNodeID &ID,
  308. TemplateTemplateParmDecl *parameter,
  309. TemplateName replacement);
  310. };
  311. inline TemplateName TemplateName::getUnderlying() const {
  312. if (SubstTemplateTemplateParmStorage *subst
  313. = getAsSubstTemplateTemplateParm())
  314. return subst->getReplacement().getUnderlying();
  315. return *this;
  316. }
  317. /// Represents a template name that was expressed as a
  318. /// qualified name.
  319. ///
  320. /// This kind of template name refers to a template name that was
  321. /// preceded by a nested name specifier, e.g., \c std::vector. Here,
  322. /// the nested name specifier is "std::" and the template name is the
  323. /// declaration for "vector". The QualifiedTemplateName class is only
  324. /// used to provide "sugar" for template names that were expressed
  325. /// with a qualified name, and has no semantic meaning. In this
  326. /// manner, it is to TemplateName what ElaboratedType is to Type,
  327. /// providing extra syntactic sugar for downstream clients.
  328. class QualifiedTemplateName : public llvm::FoldingSetNode {
  329. friend class ASTContext;
  330. /// The nested name specifier that qualifies the template name.
  331. ///
  332. /// The bit is used to indicate whether the "template" keyword was
  333. /// present before the template name itself. Note that the
  334. /// "template" keyword is always redundant in this case (otherwise,
  335. /// the template name would be a dependent name and we would express
  336. /// this name with DependentTemplateName).
  337. llvm::PointerIntPair<NestedNameSpecifier *, 1> Qualifier;
  338. /// The template declaration or set of overloaded function templates
  339. /// that this qualified name refers to.
  340. TemplateDecl *Template;
  341. QualifiedTemplateName(NestedNameSpecifier *NNS, bool TemplateKeyword,
  342. TemplateDecl *Template)
  343. : Qualifier(NNS, TemplateKeyword? 1 : 0), Template(Template) {}
  344. public:
  345. /// Return the nested name specifier that qualifies this name.
  346. NestedNameSpecifier *getQualifier() const { return Qualifier.getPointer(); }
  347. /// Whether the template name was prefixed by the "template"
  348. /// keyword.
  349. bool hasTemplateKeyword() const { return Qualifier.getInt(); }
  350. /// The template declaration that this qualified name refers
  351. /// to.
  352. TemplateDecl *getDecl() const { return Template; }
  353. /// The template declaration to which this qualified name
  354. /// refers.
  355. TemplateDecl *getTemplateDecl() const { return Template; }
  356. void Profile(llvm::FoldingSetNodeID &ID) {
  357. Profile(ID, getQualifier(), hasTemplateKeyword(), getTemplateDecl());
  358. }
  359. static void Profile(llvm::FoldingSetNodeID &ID, NestedNameSpecifier *NNS,
  360. bool TemplateKeyword, TemplateDecl *Template) {
  361. ID.AddPointer(NNS);
  362. ID.AddBoolean(TemplateKeyword);
  363. ID.AddPointer(Template);
  364. }
  365. };
  366. /// Represents a dependent template name that cannot be
  367. /// resolved prior to template instantiation.
  368. ///
  369. /// This kind of template name refers to a dependent template name,
  370. /// including its nested name specifier (if any). For example,
  371. /// DependentTemplateName can refer to "MetaFun::template apply",
  372. /// where "MetaFun::" is the nested name specifier and "apply" is the
  373. /// template name referenced. The "template" keyword is implied.
  374. class DependentTemplateName : public llvm::FoldingSetNode {
  375. friend class ASTContext;
  376. /// The nested name specifier that qualifies the template
  377. /// name.
  378. ///
  379. /// The bit stored in this qualifier describes whether the \c Name field
  380. /// is interpreted as an IdentifierInfo pointer (when clear) or as an
  381. /// overloaded operator kind (when set).
  382. llvm::PointerIntPair<NestedNameSpecifier *, 1, bool> Qualifier;
  383. /// The dependent template name.
  384. union {
  385. /// The identifier template name.
  386. ///
  387. /// Only valid when the bit on \c Qualifier is clear.
  388. const IdentifierInfo *Identifier;
  389. /// The overloaded operator name.
  390. ///
  391. /// Only valid when the bit on \c Qualifier is set.
  392. OverloadedOperatorKind Operator;
  393. };
  394. /// The canonical template name to which this dependent
  395. /// template name refers.
  396. ///
  397. /// The canonical template name for a dependent template name is
  398. /// another dependent template name whose nested name specifier is
  399. /// canonical.
  400. TemplateName CanonicalTemplateName;
  401. DependentTemplateName(NestedNameSpecifier *Qualifier,
  402. const IdentifierInfo *Identifier)
  403. : Qualifier(Qualifier, false), Identifier(Identifier),
  404. CanonicalTemplateName(this) {}
  405. DependentTemplateName(NestedNameSpecifier *Qualifier,
  406. const IdentifierInfo *Identifier,
  407. TemplateName Canon)
  408. : Qualifier(Qualifier, false), Identifier(Identifier),
  409. CanonicalTemplateName(Canon) {}
  410. DependentTemplateName(NestedNameSpecifier *Qualifier,
  411. OverloadedOperatorKind Operator)
  412. : Qualifier(Qualifier, true), Operator(Operator),
  413. CanonicalTemplateName(this) {}
  414. DependentTemplateName(NestedNameSpecifier *Qualifier,
  415. OverloadedOperatorKind Operator,
  416. TemplateName Canon)
  417. : Qualifier(Qualifier, true), Operator(Operator),
  418. CanonicalTemplateName(Canon) {}
  419. public:
  420. /// Return the nested name specifier that qualifies this name.
  421. NestedNameSpecifier *getQualifier() const { return Qualifier.getPointer(); }
  422. /// Determine whether this template name refers to an identifier.
  423. bool isIdentifier() const { return !Qualifier.getInt(); }
  424. /// Returns the identifier to which this template name refers.
  425. const IdentifierInfo *getIdentifier() const {
  426. assert(isIdentifier() && "Template name isn't an identifier?");
  427. return Identifier;
  428. }
  429. /// Determine whether this template name refers to an overloaded
  430. /// operator.
  431. bool isOverloadedOperator() const { return Qualifier.getInt(); }
  432. /// Return the overloaded operator to which this template name refers.
  433. OverloadedOperatorKind getOperator() const {
  434. assert(isOverloadedOperator() &&
  435. "Template name isn't an overloaded operator?");
  436. return Operator;
  437. }
  438. void Profile(llvm::FoldingSetNodeID &ID) {
  439. if (isIdentifier())
  440. Profile(ID, getQualifier(), getIdentifier());
  441. else
  442. Profile(ID, getQualifier(), getOperator());
  443. }
  444. static void Profile(llvm::FoldingSetNodeID &ID, NestedNameSpecifier *NNS,
  445. const IdentifierInfo *Identifier) {
  446. ID.AddPointer(NNS);
  447. ID.AddBoolean(false);
  448. ID.AddPointer(Identifier);
  449. }
  450. static void Profile(llvm::FoldingSetNodeID &ID, NestedNameSpecifier *NNS,
  451. OverloadedOperatorKind Operator) {
  452. ID.AddPointer(NNS);
  453. ID.AddBoolean(true);
  454. ID.AddInteger(Operator);
  455. }
  456. };
  457. } // namespace clang.
  458. namespace llvm {
  459. /// The clang::TemplateName class is effectively a pointer.
  460. template<>
  461. struct PointerLikeTypeTraits<clang::TemplateName> {
  462. static inline void *getAsVoidPointer(clang::TemplateName TN) {
  463. return TN.getAsVoidPointer();
  464. }
  465. static inline clang::TemplateName getFromVoidPointer(void *Ptr) {
  466. return clang::TemplateName::getFromVoidPointer(Ptr);
  467. }
  468. // No bits are available!
  469. static constexpr int NumLowBitsAvailable = 0;
  470. };
  471. } // namespace llvm.
  472. #endif // LLVM_CLANG_AST_TEMPLATENAME_H
  473. #ifdef __GNUC__
  474. #pragma GCC diagnostic pop
  475. #endif