Template.h 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- SemaTemplate.h - C++ Templates ---------------------------*- 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. // This file provides types used in the semantic analysis of C++ templates.
  14. //
  15. //===----------------------------------------------------------------------===//
  16. #ifndef LLVM_CLANG_SEMA_TEMPLATE_H
  17. #define LLVM_CLANG_SEMA_TEMPLATE_H
  18. #include "clang/AST/DeclTemplate.h"
  19. #include "clang/AST/DeclVisitor.h"
  20. #include "clang/AST/TemplateBase.h"
  21. #include "clang/AST/Type.h"
  22. #include "clang/Basic/LLVM.h"
  23. #include "clang/Sema/Sema.h"
  24. #include "llvm/ADT/ArrayRef.h"
  25. #include "llvm/ADT/DenseMap.h"
  26. #include "llvm/ADT/PointerUnion.h"
  27. #include "llvm/ADT/SmallVector.h"
  28. #include <cassert>
  29. #include <optional>
  30. #include <utility>
  31. namespace clang {
  32. class ASTContext;
  33. class BindingDecl;
  34. class CXXMethodDecl;
  35. class Decl;
  36. class DeclaratorDecl;
  37. class DeclContext;
  38. class EnumDecl;
  39. class FunctionDecl;
  40. class NamedDecl;
  41. class ParmVarDecl;
  42. class TagDecl;
  43. class TypedefNameDecl;
  44. class TypeSourceInfo;
  45. class VarDecl;
  46. /// The kind of template substitution being performed.
  47. enum class TemplateSubstitutionKind : char {
  48. /// We are substituting template parameters for template arguments in order
  49. /// to form a template specialization.
  50. Specialization,
  51. /// We are substituting template parameters for (typically) other template
  52. /// parameters in order to rewrite a declaration as a different declaration
  53. /// (for example, when forming a deduction guide from a constructor).
  54. Rewrite,
  55. };
  56. /// Data structure that captures multiple levels of template argument
  57. /// lists for use in template instantiation.
  58. ///
  59. /// Multiple levels of template arguments occur when instantiating the
  60. /// definitions of member templates. For example:
  61. ///
  62. /// \code
  63. /// template<typename T>
  64. /// struct X {
  65. /// template<T Value>
  66. /// struct Y {
  67. /// void f();
  68. /// };
  69. /// };
  70. /// \endcode
  71. ///
  72. /// When instantiating X<int>::Y<17>::f, the multi-level template argument
  73. /// list will contain a template argument list (int) at depth 0 and a
  74. /// template argument list (17) at depth 1.
  75. class MultiLevelTemplateArgumentList {
  76. /// The template argument list at a certain template depth
  77. using ArgList = ArrayRef<TemplateArgument>;
  78. struct ArgumentListLevel {
  79. llvm::PointerIntPair<Decl *, 1, bool> AssociatedDeclAndFinal;
  80. ArgList Args;
  81. };
  82. using ContainerType = SmallVector<ArgumentListLevel, 4>;
  83. using ArgListsIterator = ContainerType::iterator;
  84. using ConstArgListsIterator = ContainerType::const_iterator;
  85. /// The template argument lists, stored from the innermost template
  86. /// argument list (first) to the outermost template argument list (last).
  87. ContainerType TemplateArgumentLists;
  88. /// The number of outer levels of template arguments that are not
  89. /// being substituted.
  90. unsigned NumRetainedOuterLevels = 0;
  91. /// The kind of substitution described by this argument list.
  92. TemplateSubstitutionKind Kind = TemplateSubstitutionKind::Specialization;
  93. public:
  94. /// Construct an empty set of template argument lists.
  95. MultiLevelTemplateArgumentList() = default;
  96. /// Construct a single-level template argument list.
  97. MultiLevelTemplateArgumentList(Decl *D, ArgList Args, bool Final) {
  98. addOuterTemplateArguments(D, Args, Final);
  99. }
  100. void setKind(TemplateSubstitutionKind K) { Kind = K; }
  101. /// Determine the kind of template substitution being performed.
  102. TemplateSubstitutionKind getKind() const { return Kind; }
  103. /// Determine whether we are rewriting template parameters rather than
  104. /// substituting for them. If so, we should not leave references to the
  105. /// original template parameters behind.
  106. bool isRewrite() const {
  107. return Kind == TemplateSubstitutionKind::Rewrite;
  108. }
  109. /// Determine the number of levels in this template argument
  110. /// list.
  111. unsigned getNumLevels() const {
  112. return TemplateArgumentLists.size() + NumRetainedOuterLevels;
  113. }
  114. /// Determine the number of substituted levels in this template
  115. /// argument list.
  116. unsigned getNumSubstitutedLevels() const {
  117. return TemplateArgumentLists.size();
  118. }
  119. // Determine the number of substituted args at 'Depth'.
  120. unsigned getNumSubsitutedArgs(unsigned Depth) const {
  121. assert(NumRetainedOuterLevels <= Depth && Depth < getNumLevels());
  122. return TemplateArgumentLists[getNumLevels() - Depth - 1].Args.size();
  123. }
  124. unsigned getNumRetainedOuterLevels() const {
  125. return NumRetainedOuterLevels;
  126. }
  127. /// Determine how many of the \p OldDepth outermost template parameter
  128. /// lists would be removed by substituting these arguments.
  129. unsigned getNewDepth(unsigned OldDepth) const {
  130. if (OldDepth < NumRetainedOuterLevels)
  131. return OldDepth;
  132. if (OldDepth < getNumLevels())
  133. return NumRetainedOuterLevels;
  134. return OldDepth - TemplateArgumentLists.size();
  135. }
  136. /// Retrieve the template argument at a given depth and index.
  137. const TemplateArgument &operator()(unsigned Depth, unsigned Index) const {
  138. assert(NumRetainedOuterLevels <= Depth && Depth < getNumLevels());
  139. assert(Index <
  140. TemplateArgumentLists[getNumLevels() - Depth - 1].Args.size());
  141. return TemplateArgumentLists[getNumLevels() - Depth - 1].Args[Index];
  142. }
  143. /// A template-like entity which owns the whole pattern being substituted.
  144. /// This will usually own a set of template parameters, or in some
  145. /// cases might even be a template parameter itself.
  146. std::pair<Decl *, bool> getAssociatedDecl(unsigned Depth) const {
  147. assert(NumRetainedOuterLevels <= Depth && Depth < getNumLevels());
  148. auto AD = TemplateArgumentLists[getNumLevels() - Depth - 1]
  149. .AssociatedDeclAndFinal;
  150. return {AD.getPointer(), AD.getInt()};
  151. }
  152. /// Determine whether there is a non-NULL template argument at the
  153. /// given depth and index.
  154. ///
  155. /// There must exist a template argument list at the given depth.
  156. bool hasTemplateArgument(unsigned Depth, unsigned Index) const {
  157. assert(Depth < getNumLevels());
  158. if (Depth < NumRetainedOuterLevels)
  159. return false;
  160. if (Index >=
  161. TemplateArgumentLists[getNumLevels() - Depth - 1].Args.size())
  162. return false;
  163. return !(*this)(Depth, Index).isNull();
  164. }
  165. bool isAnyArgInstantiationDependent() const {
  166. for (ArgumentListLevel ListLevel : TemplateArgumentLists)
  167. for (const TemplateArgument &TA : ListLevel.Args)
  168. if (TA.isInstantiationDependent())
  169. return true;
  170. return false;
  171. }
  172. /// Clear out a specific template argument.
  173. void setArgument(unsigned Depth, unsigned Index,
  174. TemplateArgument Arg) {
  175. assert(NumRetainedOuterLevels <= Depth && Depth < getNumLevels());
  176. assert(Index <
  177. TemplateArgumentLists[getNumLevels() - Depth - 1].Args.size());
  178. const_cast<TemplateArgument &>(
  179. TemplateArgumentLists[getNumLevels() - Depth - 1].Args[Index]) = Arg;
  180. }
  181. /// Add a new outmost level to the multi-level template argument
  182. /// list.
  183. /// A 'Final' substitution means that Subst* nodes won't be built
  184. /// for the replacements.
  185. void addOuterTemplateArguments(Decl *AssociatedDecl, ArgList Args,
  186. bool Final) {
  187. assert(!NumRetainedOuterLevels &&
  188. "substituted args outside retained args?");
  189. assert(getKind() == TemplateSubstitutionKind::Specialization);
  190. TemplateArgumentLists.push_back(
  191. {{AssociatedDecl->getCanonicalDecl(), Final}, Args});
  192. }
  193. void addOuterTemplateArguments(ArgList Args) {
  194. assert(!NumRetainedOuterLevels &&
  195. "substituted args outside retained args?");
  196. assert(getKind() == TemplateSubstitutionKind::Rewrite);
  197. TemplateArgumentLists.push_back({{}, Args});
  198. }
  199. void addOuterTemplateArguments(std::nullopt_t) {
  200. assert(!NumRetainedOuterLevels &&
  201. "substituted args outside retained args?");
  202. TemplateArgumentLists.push_back({});
  203. }
  204. /// Replaces the current 'innermost' level with the provided argument list.
  205. /// This is useful for type deduction cases where we need to get the entire
  206. /// list from the AST, but then add the deduced innermost list.
  207. void replaceInnermostTemplateArguments(ArgList Args) {
  208. assert(TemplateArgumentLists.size() > 0 && "Replacing in an empty list?");
  209. TemplateArgumentLists[0].Args = Args;
  210. }
  211. /// Add an outermost level that we are not substituting. We have no
  212. /// arguments at this level, and do not remove it from the depth of inner
  213. /// template parameters that we instantiate.
  214. void addOuterRetainedLevel() {
  215. ++NumRetainedOuterLevels;
  216. }
  217. void addOuterRetainedLevels(unsigned Num) {
  218. NumRetainedOuterLevels += Num;
  219. }
  220. /// Retrieve the innermost template argument list.
  221. const ArgList &getInnermost() const {
  222. return TemplateArgumentLists.front().Args;
  223. }
  224. /// Retrieve the outermost template argument list.
  225. const ArgList &getOutermost() const {
  226. return TemplateArgumentLists.back().Args;
  227. }
  228. ArgListsIterator begin() { return TemplateArgumentLists.begin(); }
  229. ConstArgListsIterator begin() const {
  230. return TemplateArgumentLists.begin();
  231. }
  232. ArgListsIterator end() { return TemplateArgumentLists.end(); }
  233. ConstArgListsIterator end() const { return TemplateArgumentLists.end(); }
  234. };
  235. /// The context in which partial ordering of function templates occurs.
  236. enum TPOC {
  237. /// Partial ordering of function templates for a function call.
  238. TPOC_Call,
  239. /// Partial ordering of function templates for a call to a
  240. /// conversion function.
  241. TPOC_Conversion,
  242. /// Partial ordering of function templates in other contexts, e.g.,
  243. /// taking the address of a function template or matching a function
  244. /// template specialization to a function template.
  245. TPOC_Other
  246. };
  247. // This is lame but unavoidable in a world without forward
  248. // declarations of enums. The alternatives are to either pollute
  249. // Sema.h (by including this file) or sacrifice type safety (by
  250. // making Sema.h declare things as enums).
  251. class TemplatePartialOrderingContext {
  252. TPOC Value;
  253. public:
  254. TemplatePartialOrderingContext(TPOC Value) : Value(Value) {}
  255. operator TPOC() const { return Value; }
  256. };
  257. /// Captures a template argument whose value has been deduced
  258. /// via c++ template argument deduction.
  259. class DeducedTemplateArgument : public TemplateArgument {
  260. /// For a non-type template argument, whether the value was
  261. /// deduced from an array bound.
  262. bool DeducedFromArrayBound = false;
  263. public:
  264. DeducedTemplateArgument() = default;
  265. DeducedTemplateArgument(const TemplateArgument &Arg,
  266. bool DeducedFromArrayBound = false)
  267. : TemplateArgument(Arg), DeducedFromArrayBound(DeducedFromArrayBound) {}
  268. /// Construct an integral non-type template argument that
  269. /// has been deduced, possibly from an array bound.
  270. DeducedTemplateArgument(ASTContext &Ctx,
  271. const llvm::APSInt &Value,
  272. QualType ValueType,
  273. bool DeducedFromArrayBound)
  274. : TemplateArgument(Ctx, Value, ValueType),
  275. DeducedFromArrayBound(DeducedFromArrayBound) {}
  276. /// For a non-type template argument, determine whether the
  277. /// template argument was deduced from an array bound.
  278. bool wasDeducedFromArrayBound() const { return DeducedFromArrayBound; }
  279. /// Specify whether the given non-type template argument
  280. /// was deduced from an array bound.
  281. void setDeducedFromArrayBound(bool Deduced) {
  282. DeducedFromArrayBound = Deduced;
  283. }
  284. };
  285. /// A stack-allocated class that identifies which local
  286. /// variable declaration instantiations are present in this scope.
  287. ///
  288. /// A new instance of this class type will be created whenever we
  289. /// instantiate a new function declaration, which will have its own
  290. /// set of parameter declarations.
  291. class LocalInstantiationScope {
  292. public:
  293. /// A set of declarations.
  294. using DeclArgumentPack = SmallVector<VarDecl *, 4>;
  295. private:
  296. /// Reference to the semantic analysis that is performing
  297. /// this template instantiation.
  298. Sema &SemaRef;
  299. using LocalDeclsMap =
  300. llvm::SmallDenseMap<const Decl *,
  301. llvm::PointerUnion<Decl *, DeclArgumentPack *>, 4>;
  302. /// A mapping from local declarations that occur
  303. /// within a template to their instantiations.
  304. ///
  305. /// This mapping is used during instantiation to keep track of,
  306. /// e.g., function parameter and variable declarations. For example,
  307. /// given:
  308. ///
  309. /// \code
  310. /// template<typename T> T add(T x, T y) { return x + y; }
  311. /// \endcode
  312. ///
  313. /// when we instantiate add<int>, we will introduce a mapping from
  314. /// the ParmVarDecl for 'x' that occurs in the template to the
  315. /// instantiated ParmVarDecl for 'x'.
  316. ///
  317. /// For a parameter pack, the local instantiation scope may contain a
  318. /// set of instantiated parameters. This is stored as a DeclArgumentPack
  319. /// pointer.
  320. LocalDeclsMap LocalDecls;
  321. /// The set of argument packs we've allocated.
  322. SmallVector<DeclArgumentPack *, 1> ArgumentPacks;
  323. /// The outer scope, which contains local variable
  324. /// definitions from some other instantiation (that may not be
  325. /// relevant to this particular scope).
  326. LocalInstantiationScope *Outer;
  327. /// Whether we have already exited this scope.
  328. bool Exited = false;
  329. /// Whether to combine this scope with the outer scope, such that
  330. /// lookup will search our outer scope.
  331. bool CombineWithOuterScope;
  332. /// If non-NULL, the template parameter pack that has been
  333. /// partially substituted per C++0x [temp.arg.explicit]p9.
  334. NamedDecl *PartiallySubstitutedPack = nullptr;
  335. /// If \c PartiallySubstitutedPack is non-null, the set of
  336. /// explicitly-specified template arguments in that pack.
  337. const TemplateArgument *ArgsInPartiallySubstitutedPack;
  338. /// If \c PartiallySubstitutedPack, the number of
  339. /// explicitly-specified template arguments in
  340. /// ArgsInPartiallySubstitutedPack.
  341. unsigned NumArgsInPartiallySubstitutedPack;
  342. public:
  343. LocalInstantiationScope(Sema &SemaRef, bool CombineWithOuterScope = false)
  344. : SemaRef(SemaRef), Outer(SemaRef.CurrentInstantiationScope),
  345. CombineWithOuterScope(CombineWithOuterScope) {
  346. SemaRef.CurrentInstantiationScope = this;
  347. }
  348. LocalInstantiationScope(const LocalInstantiationScope &) = delete;
  349. LocalInstantiationScope &
  350. operator=(const LocalInstantiationScope &) = delete;
  351. ~LocalInstantiationScope() {
  352. Exit();
  353. }
  354. const Sema &getSema() const { return SemaRef; }
  355. /// Exit this local instantiation scope early.
  356. void Exit() {
  357. if (Exited)
  358. return;
  359. for (unsigned I = 0, N = ArgumentPacks.size(); I != N; ++I)
  360. delete ArgumentPacks[I];
  361. SemaRef.CurrentInstantiationScope = Outer;
  362. Exited = true;
  363. }
  364. /// Clone this scope, and all outer scopes, down to the given
  365. /// outermost scope.
  366. LocalInstantiationScope *cloneScopes(LocalInstantiationScope *Outermost) {
  367. if (this == Outermost) return this;
  368. // Save the current scope from SemaRef since the LocalInstantiationScope
  369. // will overwrite it on construction
  370. LocalInstantiationScope *oldScope = SemaRef.CurrentInstantiationScope;
  371. LocalInstantiationScope *newScope =
  372. new LocalInstantiationScope(SemaRef, CombineWithOuterScope);
  373. newScope->Outer = nullptr;
  374. if (Outer)
  375. newScope->Outer = Outer->cloneScopes(Outermost);
  376. newScope->PartiallySubstitutedPack = PartiallySubstitutedPack;
  377. newScope->ArgsInPartiallySubstitutedPack = ArgsInPartiallySubstitutedPack;
  378. newScope->NumArgsInPartiallySubstitutedPack =
  379. NumArgsInPartiallySubstitutedPack;
  380. for (LocalDeclsMap::iterator I = LocalDecls.begin(), E = LocalDecls.end();
  381. I != E; ++I) {
  382. const Decl *D = I->first;
  383. llvm::PointerUnion<Decl *, DeclArgumentPack *> &Stored =
  384. newScope->LocalDecls[D];
  385. if (I->second.is<Decl *>()) {
  386. Stored = I->second.get<Decl *>();
  387. } else {
  388. DeclArgumentPack *OldPack = I->second.get<DeclArgumentPack *>();
  389. DeclArgumentPack *NewPack = new DeclArgumentPack(*OldPack);
  390. Stored = NewPack;
  391. newScope->ArgumentPacks.push_back(NewPack);
  392. }
  393. }
  394. // Restore the saved scope to SemaRef
  395. SemaRef.CurrentInstantiationScope = oldScope;
  396. return newScope;
  397. }
  398. /// deletes the given scope, and all outer scopes, down to the
  399. /// given outermost scope.
  400. static void deleteScopes(LocalInstantiationScope *Scope,
  401. LocalInstantiationScope *Outermost) {
  402. while (Scope && Scope != Outermost) {
  403. LocalInstantiationScope *Out = Scope->Outer;
  404. delete Scope;
  405. Scope = Out;
  406. }
  407. }
  408. /// Find the instantiation of the declaration D within the current
  409. /// instantiation scope.
  410. ///
  411. /// \param D The declaration whose instantiation we are searching for.
  412. ///
  413. /// \returns A pointer to the declaration or argument pack of declarations
  414. /// to which the declaration \c D is instantiated, if found. Otherwise,
  415. /// returns NULL.
  416. llvm::PointerUnion<Decl *, DeclArgumentPack *> *
  417. findInstantiationOf(const Decl *D);
  418. void InstantiatedLocal(const Decl *D, Decl *Inst);
  419. void InstantiatedLocalPackArg(const Decl *D, VarDecl *Inst);
  420. void MakeInstantiatedLocalArgPack(const Decl *D);
  421. /// Note that the given parameter pack has been partially substituted
  422. /// via explicit specification of template arguments
  423. /// (C++0x [temp.arg.explicit]p9).
  424. ///
  425. /// \param Pack The parameter pack, which will always be a template
  426. /// parameter pack.
  427. ///
  428. /// \param ExplicitArgs The explicitly-specified template arguments provided
  429. /// for this parameter pack.
  430. ///
  431. /// \param NumExplicitArgs The number of explicitly-specified template
  432. /// arguments provided for this parameter pack.
  433. void SetPartiallySubstitutedPack(NamedDecl *Pack,
  434. const TemplateArgument *ExplicitArgs,
  435. unsigned NumExplicitArgs);
  436. /// Reset the partially-substituted pack when it is no longer of
  437. /// interest.
  438. void ResetPartiallySubstitutedPack() {
  439. assert(PartiallySubstitutedPack && "No partially-substituted pack");
  440. PartiallySubstitutedPack = nullptr;
  441. ArgsInPartiallySubstitutedPack = nullptr;
  442. NumArgsInPartiallySubstitutedPack = 0;
  443. }
  444. /// Retrieve the partially-substitued template parameter pack.
  445. ///
  446. /// If there is no partially-substituted parameter pack, returns NULL.
  447. NamedDecl *
  448. getPartiallySubstitutedPack(const TemplateArgument **ExplicitArgs = nullptr,
  449. unsigned *NumExplicitArgs = nullptr) const;
  450. /// Determine whether D is a pack expansion created in this scope.
  451. bool isLocalPackExpansion(const Decl *D);
  452. };
  453. class TemplateDeclInstantiator
  454. : public DeclVisitor<TemplateDeclInstantiator, Decl *>
  455. {
  456. Sema &SemaRef;
  457. Sema::ArgumentPackSubstitutionIndexRAII SubstIndex;
  458. DeclContext *Owner;
  459. const MultiLevelTemplateArgumentList &TemplateArgs;
  460. Sema::LateInstantiatedAttrVec* LateAttrs = nullptr;
  461. LocalInstantiationScope *StartingScope = nullptr;
  462. bool EvaluateConstraints = true;
  463. /// A list of out-of-line class template partial
  464. /// specializations that will need to be instantiated after the
  465. /// enclosing class's instantiation is complete.
  466. SmallVector<std::pair<ClassTemplateDecl *,
  467. ClassTemplatePartialSpecializationDecl *>, 4>
  468. OutOfLinePartialSpecs;
  469. /// A list of out-of-line variable template partial
  470. /// specializations that will need to be instantiated after the
  471. /// enclosing variable's instantiation is complete.
  472. /// FIXME: Verify that this is needed.
  473. SmallVector<
  474. std::pair<VarTemplateDecl *, VarTemplatePartialSpecializationDecl *>, 4>
  475. OutOfLineVarPartialSpecs;
  476. public:
  477. TemplateDeclInstantiator(Sema &SemaRef, DeclContext *Owner,
  478. const MultiLevelTemplateArgumentList &TemplateArgs)
  479. : SemaRef(SemaRef),
  480. SubstIndex(SemaRef, SemaRef.ArgumentPackSubstitutionIndex),
  481. Owner(Owner), TemplateArgs(TemplateArgs) {}
  482. void setEvaluateConstraints(bool B) {
  483. EvaluateConstraints = B;
  484. }
  485. bool getEvaluateConstraints() {
  486. return EvaluateConstraints;
  487. }
  488. // Define all the decl visitors using DeclNodes.inc
  489. #define DECL(DERIVED, BASE) \
  490. Decl *Visit ## DERIVED ## Decl(DERIVED ## Decl *D);
  491. #define ABSTRACT_DECL(DECL)
  492. // Decls which never appear inside a class or function.
  493. #define OBJCCONTAINER(DERIVED, BASE)
  494. #define FILESCOPEASM(DERIVED, BASE)
  495. #define TOPLEVELSTMT(DERIVED, BASE)
  496. #define IMPORT(DERIVED, BASE)
  497. #define EXPORT(DERIVED, BASE)
  498. #define LINKAGESPEC(DERIVED, BASE)
  499. #define OBJCCOMPATIBLEALIAS(DERIVED, BASE)
  500. #define OBJCMETHOD(DERIVED, BASE)
  501. #define OBJCTYPEPARAM(DERIVED, BASE)
  502. #define OBJCIVAR(DERIVED, BASE)
  503. #define OBJCPROPERTY(DERIVED, BASE)
  504. #define OBJCPROPERTYIMPL(DERIVED, BASE)
  505. #define EMPTY(DERIVED, BASE)
  506. #define LIFETIMEEXTENDEDTEMPORARY(DERIVED, BASE)
  507. // Decls which use special-case instantiation code.
  508. #define BLOCK(DERIVED, BASE)
  509. #define CAPTURED(DERIVED, BASE)
  510. #define IMPLICITPARAM(DERIVED, BASE)
  511. #include "clang/AST/DeclNodes.inc"
  512. enum class RewriteKind { None, RewriteSpaceshipAsEqualEqual };
  513. void adjustForRewrite(RewriteKind RK, FunctionDecl *Orig, QualType &T,
  514. TypeSourceInfo *&TInfo,
  515. DeclarationNameInfo &NameInfo);
  516. // A few supplemental visitor functions.
  517. Decl *VisitCXXMethodDecl(CXXMethodDecl *D,
  518. TemplateParameterList *TemplateParams,
  519. std::optional<const ASTTemplateArgumentListInfo *>
  520. ClassScopeSpecializationArgs = std::nullopt,
  521. RewriteKind RK = RewriteKind::None);
  522. Decl *VisitFunctionDecl(FunctionDecl *D,
  523. TemplateParameterList *TemplateParams,
  524. RewriteKind RK = RewriteKind::None);
  525. Decl *VisitDecl(Decl *D);
  526. Decl *VisitVarDecl(VarDecl *D, bool InstantiatingVarTemplate,
  527. ArrayRef<BindingDecl *> *Bindings = nullptr);
  528. Decl *VisitBaseUsingDecls(BaseUsingDecl *D, BaseUsingDecl *Inst,
  529. LookupResult *Lookup);
  530. // Enable late instantiation of attributes. Late instantiated attributes
  531. // will be stored in LA.
  532. void enableLateAttributeInstantiation(Sema::LateInstantiatedAttrVec *LA) {
  533. LateAttrs = LA;
  534. StartingScope = SemaRef.CurrentInstantiationScope;
  535. }
  536. // Disable late instantiation of attributes.
  537. void disableLateAttributeInstantiation() {
  538. LateAttrs = nullptr;
  539. StartingScope = nullptr;
  540. }
  541. LocalInstantiationScope *getStartingScope() const { return StartingScope; }
  542. using delayed_partial_spec_iterator = SmallVectorImpl<std::pair<
  543. ClassTemplateDecl *, ClassTemplatePartialSpecializationDecl *>>::iterator;
  544. using delayed_var_partial_spec_iterator = SmallVectorImpl<std::pair<
  545. VarTemplateDecl *, VarTemplatePartialSpecializationDecl *>>::iterator;
  546. /// Return an iterator to the beginning of the set of
  547. /// "delayed" partial specializations, which must be passed to
  548. /// InstantiateClassTemplatePartialSpecialization once the class
  549. /// definition has been completed.
  550. delayed_partial_spec_iterator delayed_partial_spec_begin() {
  551. return OutOfLinePartialSpecs.begin();
  552. }
  553. delayed_var_partial_spec_iterator delayed_var_partial_spec_begin() {
  554. return OutOfLineVarPartialSpecs.begin();
  555. }
  556. /// Return an iterator to the end of the set of
  557. /// "delayed" partial specializations, which must be passed to
  558. /// InstantiateClassTemplatePartialSpecialization once the class
  559. /// definition has been completed.
  560. delayed_partial_spec_iterator delayed_partial_spec_end() {
  561. return OutOfLinePartialSpecs.end();
  562. }
  563. delayed_var_partial_spec_iterator delayed_var_partial_spec_end() {
  564. return OutOfLineVarPartialSpecs.end();
  565. }
  566. // Helper functions for instantiating methods.
  567. TypeSourceInfo *SubstFunctionType(FunctionDecl *D,
  568. SmallVectorImpl<ParmVarDecl *> &Params);
  569. bool InitFunctionInstantiation(FunctionDecl *New, FunctionDecl *Tmpl);
  570. bool InitMethodInstantiation(CXXMethodDecl *New, CXXMethodDecl *Tmpl);
  571. bool SubstDefaultedFunction(FunctionDecl *New, FunctionDecl *Tmpl);
  572. TemplateParameterList *
  573. SubstTemplateParams(TemplateParameterList *List);
  574. bool SubstQualifier(const DeclaratorDecl *OldDecl,
  575. DeclaratorDecl *NewDecl);
  576. bool SubstQualifier(const TagDecl *OldDecl,
  577. TagDecl *NewDecl);
  578. Decl *VisitVarTemplateSpecializationDecl(
  579. VarTemplateDecl *VarTemplate, VarDecl *FromVar,
  580. const TemplateArgumentListInfo &TemplateArgsInfo,
  581. ArrayRef<TemplateArgument> Converted,
  582. VarTemplateSpecializationDecl *PrevDecl = nullptr);
  583. Decl *InstantiateTypedefNameDecl(TypedefNameDecl *D, bool IsTypeAlias);
  584. ClassTemplatePartialSpecializationDecl *
  585. InstantiateClassTemplatePartialSpecialization(
  586. ClassTemplateDecl *ClassTemplate,
  587. ClassTemplatePartialSpecializationDecl *PartialSpec);
  588. VarTemplatePartialSpecializationDecl *
  589. InstantiateVarTemplatePartialSpecialization(
  590. VarTemplateDecl *VarTemplate,
  591. VarTemplatePartialSpecializationDecl *PartialSpec);
  592. void InstantiateEnumDefinition(EnumDecl *Enum, EnumDecl *Pattern);
  593. private:
  594. template<typename T>
  595. Decl *instantiateUnresolvedUsingDecl(T *D,
  596. bool InstantiatingPackElement = false);
  597. };
  598. } // namespace clang
  599. #endif // LLVM_CLANG_SEMA_TEMPLATE_H
  600. #ifdef __GNUC__
  601. #pragma GCC diagnostic pop
  602. #endif