clang 23.0.0git
Template.h
Go to the documentation of this file.
1//===- SemaTemplate.h - C++ Templates ---------------------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//===----------------------------------------------------------------------===//
7//
8// This file provides types used in the semantic analysis of C++ templates.
9//
10//===----------------------------------------------------------------------===//
11
12#ifndef LLVM_CLANG_SEMA_TEMPLATE_H
13#define LLVM_CLANG_SEMA_TEMPLATE_H
14
18#include "clang/AST/Type.h"
19#include "clang/Basic/LLVM.h"
20#include "clang/Sema/Sema.h"
21#include "llvm/ADT/ArrayRef.h"
22#include "llvm/ADT/DenseMap.h"
23#include "llvm/ADT/PointerUnion.h"
24#include "llvm/ADT/SmallVector.h"
25#include <cassert>
26#include <optional>
27#include <utility>
28
29namespace clang {
30
31class ASTContext;
32class BindingDecl;
33class CXXMethodDecl;
34class Decl;
35class DeclaratorDecl;
36class DeclContext;
37class EnumDecl;
38class FunctionDecl;
39class NamedDecl;
40class ParmVarDecl;
41class TagDecl;
42class TypedefNameDecl;
43class TypeSourceInfo;
44class VarDecl;
45
46/// The kind of template substitution being performed.
47enum class TemplateSubstitutionKind : char {
48 /// We are substituting template parameters for template arguments in order
49 /// to form a template 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).
55};
56
57 /// Data structure that captures multiple levels of template argument
58 /// lists for use in template instantiation.
59 ///
60 /// Multiple levels of template arguments occur when instantiating the
61 /// definitions of member templates. For example:
62 ///
63 /// \code
64 /// template<typename T>
65 /// struct X {
66 /// template<T Value>
67 /// struct Y {
68 /// void f();
69 /// };
70 /// };
71 /// \endcode
72 ///
73 /// When instantiating X<int>::Y<17>::f, the multi-level template argument
74 /// list will contain a template argument list (int) at depth 0 and a
75 /// template argument list (17) at depth 1.
77 /// The template argument list at a certain template depth
78
79 using ArgList = ArrayRef<TemplateArgument>;
80 struct ArgumentListLevel {
81 llvm::PointerIntPair<Decl *, 1, bool> AssociatedDeclAndFinal;
82 ArgList Args;
83 };
84 using ContainerType = SmallVector<ArgumentListLevel, 4>;
85
86 using ArgListsIterator = ContainerType::iterator;
87 using ConstArgListsIterator = ContainerType::const_iterator;
88
89 /// The template argument lists, stored from the innermost template
90 /// argument list (first) to the outermost template argument list (last).
91 ContainerType TemplateArgumentLists;
92
93 /// The number of outer levels of template arguments that are not
94 /// being substituted.
95 unsigned NumRetainedOuterLevels = 0;
96
97 /// The kind of substitution described by this argument list.
99
100 public:
101 /// Construct an empty set of template argument lists.
103
104 /// Construct a single-level template argument list.
105 MultiLevelTemplateArgumentList(Decl *D, ArgList Args, bool Final) {
106 addOuterTemplateArguments(D, Args, Final);
107 }
108
109 void setKind(TemplateSubstitutionKind K) { Kind = K; }
110
111 /// Determine the kind of template substitution being performed.
112 TemplateSubstitutionKind getKind() const { return Kind; }
113
114 /// Determine whether we are rewriting template parameters rather than
115 /// substituting for them. If so, we should not leave references to the
116 /// original template parameters behind.
117 bool isRewrite() const {
119 }
120
121 /// Determine the number of levels in this template argument
122 /// list.
123 unsigned getNumLevels() const {
124 return TemplateArgumentLists.size() + NumRetainedOuterLevels;
125 }
126
127 /// Determine the number of substituted levels in this template
128 /// argument list.
129 unsigned getNumSubstitutedLevels() const {
130 return TemplateArgumentLists.size();
131 }
132
133 // Determine the number of substituted args at 'Depth'.
134 unsigned getNumSubsitutedArgs(unsigned Depth) const {
135 assert(NumRetainedOuterLevels <= Depth && Depth < getNumLevels());
136 return TemplateArgumentLists[getNumLevels() - Depth - 1].Args.size();
137 }
138
139 unsigned getNumRetainedOuterLevels() const {
140 return NumRetainedOuterLevels;
141 }
142
143 /// Determine how many of the \p OldDepth outermost template parameter
144 /// lists would be removed by substituting these arguments.
145 unsigned getNewDepth(unsigned OldDepth) const {
146 if (OldDepth < NumRetainedOuterLevels)
147 return OldDepth;
148 if (OldDepth < getNumLevels())
149 return NumRetainedOuterLevels;
150 return OldDepth - TemplateArgumentLists.size();
151 }
152
153 /// Retrieve the template argument at a given depth and index.
154 const TemplateArgument &operator()(unsigned Depth, unsigned Index) const {
155 assert(NumRetainedOuterLevels <= Depth && Depth < getNumLevels());
156 assert(Index <
157 TemplateArgumentLists[getNumLevels() - Depth - 1].Args.size());
158 return TemplateArgumentLists[getNumLevels() - Depth - 1].Args[Index];
159 }
160
161 /// A template-like entity which owns the whole pattern being substituted.
162 /// This will usually own a set of template parameters, or in some
163 /// cases might even be a template parameter itself.
164 std::pair<Decl *, bool> getAssociatedDecl(unsigned Depth) const {
165 assert(NumRetainedOuterLevels <= Depth && Depth < getNumLevels());
166 auto AD = TemplateArgumentLists[getNumLevels() - Depth - 1]
167 .AssociatedDeclAndFinal;
168 return {AD.getPointer(), AD.getInt()};
169 }
170
171 /// Determine whether there is a non-NULL template argument at the
172 /// given depth and index.
173 ///
174 /// There must exist a template argument list at the given depth.
175 bool hasTemplateArgument(unsigned Depth, unsigned Index) const {
176 assert(Depth < getNumLevels());
177
178 if (Depth < NumRetainedOuterLevels)
179 return false;
180
181 if (Index >=
182 TemplateArgumentLists[getNumLevels() - Depth - 1].Args.size())
183 return false;
184
185 return !(*this)(Depth, Index).isNull();
186 }
187
189 for (ArgumentListLevel ListLevel : TemplateArgumentLists)
190 for (const TemplateArgument &TA : ListLevel.Args)
191 // There might be null template arguments representing unused template
192 // parameter mappings in an MLTAL during concept checking.
193 if (!TA.isNull() &&
194 C.getCanonicalTemplateArgument(TA).isInstantiationDependent())
195 return true;
196 return false;
197 }
198
199 /// Clear out a specific template argument.
200 void setArgument(unsigned Depth, unsigned Index,
201 TemplateArgument Arg) {
202 assert(NumRetainedOuterLevels <= Depth && Depth < getNumLevels());
203 assert(Index <
204 TemplateArgumentLists[getNumLevels() - Depth - 1].Args.size());
205 const_cast<TemplateArgument &>(
206 TemplateArgumentLists[getNumLevels() - Depth - 1].Args[Index]) = Arg;
207 }
208
209 /// Add a new outmost level to the multi-level template argument
210 /// list.
211 /// A 'Final' substitution means that these Args don't need to be
212 /// resugared later.
213 void addOuterTemplateArguments(Decl *AssociatedDecl, ArgList Args,
214 bool Final) {
215 assert(!NumRetainedOuterLevels &&
216 "substituted args outside retained args?");
218 TemplateArgumentLists.push_back(
219 {{AssociatedDecl ? AssociatedDecl->getCanonicalDecl() : nullptr,
220 Final},
221 Args});
222 }
223
224 void addOuterTemplateArguments(ArgList Args) {
225 assert(!NumRetainedOuterLevels &&
226 "substituted args outside retained args?");
228 TemplateArgumentLists.push_back({{}, Args});
229 }
230
231 void addOuterTemplateArguments(std::nullopt_t) {
232 assert(!NumRetainedOuterLevels &&
233 "substituted args outside retained args?");
234 TemplateArgumentLists.push_back({});
235 }
236
237 /// Replaces the current 'innermost' level with the provided argument list.
238 /// This is useful for type deduction cases where we need to get the entire
239 /// list from the AST, but then add the deduced innermost list.
240 void replaceInnermostTemplateArguments(Decl *AssociatedDecl, ArgList Args,
241 bool Final = false) {
242 assert((!TemplateArgumentLists.empty() || NumRetainedOuterLevels) &&
243 "Replacing in an empty list?");
244
245 if (!TemplateArgumentLists.empty()) {
246 TemplateArgumentLists[0].Args = Args;
247 return;
248 }
249 --NumRetainedOuterLevels;
250 TemplateArgumentLists.push_back(
251 {{AssociatedDecl, /*Final=*/Final}, Args});
252 }
253
254 void replaceOutermostTemplateArguments(Decl *AssociatedDecl, ArgList Args) {
255 assert((!TemplateArgumentLists.empty()) && "Replacing in an empty list?");
256 TemplateArgumentLists.back().AssociatedDeclAndFinal.setPointer(
257 AssociatedDecl);
258 TemplateArgumentLists.back().Args = Args;
259 }
260
261 /// Add an outermost level that we are not substituting. We have no
262 /// arguments at this level, and do not remove it from the depth of inner
263 /// template parameters that we instantiate.
265 ++NumRetainedOuterLevels;
266 }
268 NumRetainedOuterLevels += Num;
269 }
270
271 /// Retrieve the innermost template argument list.
272 const ArgList &getInnermost() const {
273 return TemplateArgumentLists.front().Args;
274 }
275 /// Retrieve the outermost template argument list.
276 const ArgList &getOutermost() const {
277 return TemplateArgumentLists.back().Args;
278 }
279 ArgListsIterator begin() { return TemplateArgumentLists.begin(); }
280 ConstArgListsIterator begin() const {
281 return TemplateArgumentLists.begin();
282 }
283 ArgListsIterator end() { return TemplateArgumentLists.end(); }
284 ConstArgListsIterator end() const { return TemplateArgumentLists.end(); }
285
286 LLVM_DUMP_METHOD void dump() const {
287 LangOptions LO;
288 LO.CPlusPlus = true;
289 LO.Bool = true;
290 PrintingPolicy PP(LO);
291 llvm::errs() << "NumRetainedOuterLevels: " << NumRetainedOuterLevels
292 << "\n";
293 for (unsigned Depth = NumRetainedOuterLevels; Depth < getNumLevels();
294 ++Depth) {
295 llvm::errs() << Depth << ": ";
296 printTemplateArgumentList(
297 llvm::errs(),
298 TemplateArgumentLists[getNumLevels() - Depth - 1].Args, PP);
299 llvm::errs() << "\n";
300 }
301 }
302 };
303
304 /// The context in which partial ordering of function templates occurs.
305 enum TPOC {
306 /// Partial ordering of function templates for a function call.
308
309 /// Partial ordering of function templates for a call to a
310 /// conversion function.
312
313 /// Partial ordering of function templates in other contexts, e.g.,
314 /// taking the address of a function template or matching a function
315 /// template specialization to a function template.
317 };
318
319 // This is lame but unavoidable in a world without forward
320 // declarations of enums. The alternatives are to either pollute
321 // Sema.h (by including this file) or sacrifice type safety (by
322 // making Sema.h declare things as enums).
324 TPOC Value;
325
326 public:
327 TemplatePartialOrderingContext(TPOC Value) : Value(Value) {}
328
329 operator TPOC() const { return Value; }
330 };
331
332 /// Captures a template argument whose value has been deduced
333 /// via c++ template argument deduction.
335 /// For a non-type template argument, whether the value was
336 /// deduced from an array bound.
337 bool DeducedFromArrayBound = false;
338
339 public:
341
343 bool DeducedFromArrayBound = false)
344 : TemplateArgument(Arg), DeducedFromArrayBound(DeducedFromArrayBound) {}
345
346 /// Construct an integral non-type template argument that
347 /// has been deduced, possibly from an array bound.
349 const llvm::APSInt &Value,
350 QualType ValueType,
351 bool DeducedFromArrayBound)
352 : TemplateArgument(Ctx, Value, ValueType),
353 DeducedFromArrayBound(DeducedFromArrayBound) {}
354
355 /// For a non-type template argument, determine whether the
356 /// template argument was deduced from an array bound.
357 bool wasDeducedFromArrayBound() const { return DeducedFromArrayBound; }
358
359 /// Specify whether the given non-type template argument
360 /// was deduced from an array bound.
361 void setDeducedFromArrayBound(bool Deduced) {
362 DeducedFromArrayBound = Deduced;
363 }
364 };
365
366 /// A stack-allocated class that identifies which local
367 /// variable declaration instantiations are present in this scope.
368 ///
369 /// A new instance of this class type will be created whenever we
370 /// instantiate a new function declaration, which will have its own
371 /// set of parameter declarations.
373 public:
374 /// A set of declarations.
376
377 private:
378 /// Reference to the semantic analysis that is performing
379 /// this template instantiation.
380 Sema &SemaRef;
381
382 using LocalDeclsMap =
383 llvm::SmallDenseMap<const Decl *,
384 llvm::PointerUnion<Decl *, DeclArgumentPack *>, 4>;
385
386 /// A mapping from local declarations that occur
387 /// within a template to their instantiations.
388 ///
389 /// This mapping is used during instantiation to keep track of,
390 /// e.g., function parameter and variable declarations. For example,
391 /// given:
392 ///
393 /// \code
394 /// template<typename T> T add(T x, T y) { return x + y; }
395 /// \endcode
396 ///
397 /// when we instantiate add<int>, we will introduce a mapping from
398 /// the ParmVarDecl for 'x' that occurs in the template to the
399 /// instantiated ParmVarDecl for 'x'.
400 ///
401 /// For a parameter pack, the local instantiation scope may contain a
402 /// set of instantiated parameters. This is stored as a DeclArgumentPack
403 /// pointer.
404 LocalDeclsMap LocalDecls;
405
406 /// The set of argument packs we've allocated.
408
409 /// The outer scope, which contains local variable
410 /// definitions from some other instantiation (that may not be
411 /// relevant to this particular scope).
413
414 /// Whether we have already exited this scope.
415 bool Exited = false;
416
417 /// Whether to combine this scope with the outer scope, such that
418 /// lookup will search our outer scope.
419 bool CombineWithOuterScope;
420
421 /// Whether this scope is being used to instantiate a lambda or block
422 /// expression, in which case it should be reused for instantiating the
423 /// lambda's FunctionProtoType.
424 bool InstantiatingLambdaOrBlock = false;
425
426 /// If non-NULL, the template parameter pack that has been
427 /// partially substituted per C++0x [temp.arg.explicit]p9.
428 NamedDecl *PartiallySubstitutedPack = nullptr;
429
430 /// If \c PartiallySubstitutedPack is non-null, the set of
431 /// explicitly-specified template arguments in that pack.
432 const TemplateArgument *ArgsInPartiallySubstitutedPack;
433
434 /// If \c PartiallySubstitutedPack, the number of
435 /// explicitly-specified template arguments in
436 /// ArgsInPartiallySubstitutedPack.
437 unsigned NumArgsInPartiallySubstitutedPack;
438
439 public:
440 LocalInstantiationScope(Sema &SemaRef, bool CombineWithOuterScope = false,
441 bool InstantiatingLambdaOrBlock = false)
442 : SemaRef(SemaRef), Outer(SemaRef.CurrentInstantiationScope),
443 CombineWithOuterScope(CombineWithOuterScope),
444 InstantiatingLambdaOrBlock(InstantiatingLambdaOrBlock) {
445 SemaRef.CurrentInstantiationScope = this;
446 }
447
451
455
456 const Sema &getSema() const { return SemaRef; }
457
458 /// Exit this local instantiation scope early.
459 void Exit() {
460 if (Exited)
461 return;
462
463 for (unsigned I = 0, N = ArgumentPacks.size(); I != N; ++I)
464 delete ArgumentPacks[I];
465
466 SemaRef.CurrentInstantiationScope = Outer;
467 Exited = true;
468 }
469
470 /// Clone this scope, and all outer scopes, down to the given
471 /// outermost scope.
473 if (this == Outermost) return this;
474
475 // Save the current scope from SemaRef since the LocalInstantiationScope
476 // will overwrite it on construction
477 LocalInstantiationScope *oldScope = SemaRef.CurrentInstantiationScope;
478
479 LocalInstantiationScope *newScope =
480 new LocalInstantiationScope(SemaRef, CombineWithOuterScope);
481
482 newScope->Outer = nullptr;
483 if (Outer)
484 newScope->Outer = Outer->cloneScopes(Outermost);
485
486 newScope->PartiallySubstitutedPack = PartiallySubstitutedPack;
487 newScope->ArgsInPartiallySubstitutedPack = ArgsInPartiallySubstitutedPack;
488 newScope->NumArgsInPartiallySubstitutedPack =
489 NumArgsInPartiallySubstitutedPack;
490
491 for (LocalDeclsMap::iterator I = LocalDecls.begin(), E = LocalDecls.end();
492 I != E; ++I) {
493 const Decl *D = I->first;
494 llvm::PointerUnion<Decl *, DeclArgumentPack *> &Stored =
495 newScope->LocalDecls[D];
496 if (auto *D2 = dyn_cast<Decl *>(I->second)) {
497 Stored = D2;
498 } else {
499 DeclArgumentPack *OldPack = cast<DeclArgumentPack *>(I->second);
500 DeclArgumentPack *NewPack = new DeclArgumentPack(*OldPack);
501 Stored = NewPack;
502 newScope->ArgumentPacks.push_back(NewPack);
503 }
504 }
505 // Restore the saved scope to SemaRef
506 SemaRef.CurrentInstantiationScope = oldScope;
507 return newScope;
508 }
509
510 /// deletes the given scope, and all outer scopes, down to the
511 /// given outermost scope.
513 LocalInstantiationScope *Outermost) {
514 while (Scope && Scope != Outermost) {
515 LocalInstantiationScope *Out = Scope->Outer;
516 delete Scope;
517 Scope = Out;
518 }
519 }
520
521 /// Find the instantiation of the declaration D within the current
522 /// instantiation scope.
523 ///
524 /// \param D The declaration whose instantiation we are searching for.
525 ///
526 /// \returns A pointer to the declaration or argument pack of declarations
527 /// to which the declaration \c D is instantiated, if found. Otherwise,
528 /// returns NULL.
529 llvm::PointerUnion<Decl *, DeclArgumentPack *> *
530 findInstantiationOf(const Decl *D);
531
532 /// Similar to \p findInstantiationOf(), but it wouldn't assert if the
533 /// instantiation was not found within the current instantiation scope. This
534 /// is helpful for on-demand declaration instantiation.
535 llvm::PointerUnion<Decl *, DeclArgumentPack *> *
537
538 void InstantiatedLocal(const Decl *D, Decl *Inst);
539 void InstantiatedLocalPackArg(const Decl *D, VarDecl *Inst);
540 void MakeInstantiatedLocalArgPack(const Decl *D);
541
542 /// Note that the given parameter pack has been partially substituted
543 /// via explicit specification of template arguments
544 /// (C++0x [temp.arg.explicit]p9).
545 ///
546 /// \param Pack The parameter pack, which will always be a template
547 /// parameter pack.
548 ///
549 /// \param ExplicitArgs The explicitly-specified template arguments provided
550 /// for this parameter pack.
551 ///
552 /// \param NumExplicitArgs The number of explicitly-specified template
553 /// arguments provided for this parameter pack.
555 const TemplateArgument *ExplicitArgs,
556 unsigned NumExplicitArgs);
557
558 /// Reset the partially-substituted pack when it is no longer of
559 /// interest.
561 assert(PartiallySubstitutedPack && "No partially-substituted pack");
562 PartiallySubstitutedPack = nullptr;
563 ArgsInPartiallySubstitutedPack = nullptr;
564 NumArgsInPartiallySubstitutedPack = 0;
565 }
566
567 /// Retrieve the partially-substitued template parameter pack.
568 ///
569 /// If there is no partially-substituted parameter pack, returns NULL.
570 NamedDecl *
571 getPartiallySubstitutedPack(const TemplateArgument **ExplicitArgs = nullptr,
572 unsigned *NumExplicitArgs = nullptr) const;
573
574 /// Determine whether D is a pack expansion created in this scope.
575 bool isLocalPackExpansion(const Decl *D);
576
577 /// Determine whether this scope is for instantiating a lambda or block.
578 bool isLambdaOrBlock() const { return InstantiatingLambdaOrBlock; }
579 };
580
582 : public DeclVisitor<TemplateDeclInstantiator, Decl *>
583 {
584 Sema &SemaRef;
586 DeclContext *Owner;
587 const MultiLevelTemplateArgumentList &TemplateArgs;
588 Sema::LateInstantiatedAttrVec* LateAttrs = nullptr;
589 LocalInstantiationScope *StartingScope = nullptr;
590 // Whether to evaluate the C++20 constraints or simply substitute into them.
591 bool EvaluateConstraints = true;
592
593 /// A list of out-of-line class template partial
594 /// specializations that will need to be instantiated after the
595 /// enclosing class's instantiation is complete.
596 SmallVector<std::pair<ClassTemplateDecl *,
598 1>
599 OutOfLinePartialSpecs;
600
601 /// A list of out-of-line variable template partial
602 /// specializations that will need to be instantiated after the
603 /// enclosing variable's instantiation is complete.
604 /// FIXME: Verify that this is needed.
606 std::pair<VarTemplateDecl *, VarTemplatePartialSpecializationDecl *>, 1>
607 OutOfLineVarPartialSpecs;
608
609 public:
611 const MultiLevelTemplateArgumentList &TemplateArgs)
612 : SemaRef(SemaRef), SubstIndex(SemaRef, SemaRef.ArgPackSubstIndex),
613 Owner(Owner), TemplateArgs(TemplateArgs) {}
614
616 EvaluateConstraints = B;
617 }
619 return EvaluateConstraints;
620 }
621
622// Define all the decl visitors using DeclNodes.inc
623#define DECL(DERIVED, BASE) \
624 Decl *Visit ## DERIVED ## Decl(DERIVED ## Decl *D);
625#define ABSTRACT_DECL(DECL)
626
627// Decls which never appear inside a class or function.
628#define OBJCCONTAINER(DERIVED, BASE)
629#define FILESCOPEASM(DERIVED, BASE)
630#define TOPLEVELSTMT(DERIVED, BASE)
631#define IMPORT(DERIVED, BASE)
632#define EXPORT(DERIVED, BASE)
633#define LINKAGESPEC(DERIVED, BASE)
634#define OBJCCOMPATIBLEALIAS(DERIVED, BASE)
635#define OBJCMETHOD(DERIVED, BASE)
636#define OBJCTYPEPARAM(DERIVED, BASE)
637#define OBJCIVAR(DERIVED, BASE)
638#define OBJCPROPERTY(DERIVED, BASE)
639#define OBJCPROPERTYIMPL(DERIVED, BASE)
640#define EMPTY(DERIVED, BASE)
641#define LIFETIMEEXTENDEDTEMPORARY(DERIVED, BASE)
642
643// Decls which never appear inside a template.
644#define OUTLINEDFUNCTION(DERIVED, BASE)
645
646// Decls which use special-case instantiation code.
647#define BLOCK(DERIVED, BASE)
648#define CAPTURED(DERIVED, BASE)
649#define IMPLICITPARAM(DERIVED, BASE)
650
651#include "clang/AST/DeclNodes.inc"
652
654
656 TypeSourceInfo *&TInfo,
657 DeclarationNameInfo &NameInfo);
658
659 // A few supplemental visitor functions.
661 TemplateParameterList *TemplateParams,
664 TemplateParameterList *TemplateParams,
666 Decl *VisitDecl(Decl *D);
667 Decl *VisitVarDecl(VarDecl *D, bool InstantiatingVarTemplate,
670 LookupResult *Lookup);
671
672 // Enable late instantiation of attributes. Late instantiated attributes
673 // will be stored in LA.
675 LateAttrs = LA;
676 StartingScope = SemaRef.CurrentInstantiationScope;
677 }
678
679 // Disable late instantiation of attributes.
681 LateAttrs = nullptr;
682 StartingScope = nullptr;
683 }
684
685 LocalInstantiationScope *getStartingScope() const { return StartingScope; }
686
689
692
693 /// Return an iterator to the beginning of the set of
694 /// "delayed" partial specializations, which must be passed to
695 /// InstantiateClassTemplatePartialSpecialization once the class
696 /// definition has been completed.
698 return OutOfLinePartialSpecs.begin();
699 }
700
702 return OutOfLineVarPartialSpecs.begin();
703 }
704
705 /// Return an iterator to the end of the set of
706 /// "delayed" partial specializations, which must be passed to
707 /// InstantiateClassTemplatePartialSpecialization once the class
708 /// definition has been completed.
710 return OutOfLinePartialSpecs.end();
711 }
712
714 return OutOfLineVarPartialSpecs.end();
715 }
716
717 // Helper functions for instantiating methods.
722
724
727
728 bool SubstQualifier(const DeclaratorDecl *OldDecl,
729 DeclaratorDecl *NewDecl);
730 bool SubstQualifier(const TagDecl *OldDecl,
731 TagDecl *NewDecl);
732
736 VarTemplateSpecializationDecl *PrevDecl = nullptr);
737
738 Decl *InstantiateTypedefNameDecl(TypedefNameDecl *D, bool IsTypeAlias);
742 ClassTemplateDecl *ClassTemplate,
749
750 private:
751 template<typename T>
752 Decl *instantiateUnresolvedUsingDecl(T *D,
753 bool InstantiatingPackElement = false);
754 };
755
756} // namespace clang
757
758#endif // LLVM_CLANG_SEMA_TEMPLATE_H
Defines the C++ template declaration subclasses.
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified.
llvm::SmallVector< std::pair< const MemRegion *, SVal >, 4 > Bindings
C Language Family Type Representation.
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition ASTContext.h:226
Represents a C++ declaration that introduces decls from somewhere else.
Definition DeclCXX.h:3499
A binding in a decomposition declaration.
Definition DeclCXX.h:4188
Represents a static or instance method of a struct/union/class.
Definition DeclCXX.h:2136
Declaration of a class template.
DeclContext - This is used only as base class of specific decl types that can act as declaration cont...
Definition DeclBase.h:1449
A simple visitor class that helps create declaration visitors.
Definition DeclVisitor.h:68
Decl - This represents one declaration (or definition), e.g.
Definition DeclBase.h:86
virtual Decl * getCanonicalDecl()
Retrieves the "canonical" declaration of the given declaration.
Definition DeclBase.h:978
Represents a ValueDecl that came out of a declarator.
Definition Decl.h:780
void setDeducedFromArrayBound(bool Deduced)
Specify whether the given non-type template argument was deduced from an array bound.
Definition Template.h:361
DeducedTemplateArgument(const TemplateArgument &Arg, bool DeducedFromArrayBound=false)
Definition Template.h:342
DeducedTemplateArgument(ASTContext &Ctx, const llvm::APSInt &Value, QualType ValueType, bool DeducedFromArrayBound)
Construct an integral non-type template argument that has been deduced, possibly from an array bound.
Definition Template.h:348
bool wasDeducedFromArrayBound() const
For a non-type template argument, determine whether the template argument was deduced from an array b...
Definition Template.h:357
Represents an enum.
Definition Decl.h:4013
Represents a function declaration or definition.
Definition Decl.h:2000
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
A stack-allocated class that identifies which local variable declaration instantiations are present i...
Definition Template.h:372
LocalInstantiationScope(Sema &SemaRef, bool CombineWithOuterScope=false, bool InstantiatingLambdaOrBlock=false)
Definition Template.h:440
LocalInstantiationScope & operator=(const LocalInstantiationScope &)=delete
LocalInstantiationScope(const LocalInstantiationScope &)=delete
void SetPartiallySubstitutedPack(NamedDecl *Pack, const TemplateArgument *ExplicitArgs, unsigned NumExplicitArgs)
Note that the given parameter pack has been partially substituted via explicit specification of templ...
NamedDecl * getPartiallySubstitutedPack(const TemplateArgument **ExplicitArgs=nullptr, unsigned *NumExplicitArgs=nullptr) const
Retrieve the partially-substitued template parameter pack.
void Exit()
Exit this local instantiation scope early.
Definition Template.h:459
bool isLocalPackExpansion(const Decl *D)
Determine whether D is a pack expansion created in this scope.
SmallVector< ValueDecl *, 4 > DeclArgumentPack
A set of declarations.
Definition Template.h:375
llvm::PointerUnion< Decl *, DeclArgumentPack * > * getInstantiationOfIfExists(const Decl *D)
Similar to findInstantiationOf(), but it wouldn't assert if the instantiation was not found within th...
static void deleteScopes(LocalInstantiationScope *Scope, LocalInstantiationScope *Outermost)
deletes the given scope, and all outer scopes, down to the given outermost scope.
Definition Template.h:512
const Sema & getSema() const
Definition Template.h:456
void InstantiatedLocal(const Decl *D, Decl *Inst)
void InstantiatedLocalPackArg(const Decl *D, VarDecl *Inst)
void ResetPartiallySubstitutedPack()
Reset the partially-substituted pack when it is no longer of interest.
Definition Template.h:560
bool isLambdaOrBlock() const
Determine whether this scope is for instantiating a lambda or block.
Definition Template.h:578
LocalInstantiationScope * cloneScopes(LocalInstantiationScope *Outermost)
Clone this scope, and all outer scopes, down to the given outermost scope.
Definition Template.h:472
llvm::PointerUnion< Decl *, DeclArgumentPack * > * findInstantiationOf(const Decl *D)
Find the instantiation of the declaration D within the current instantiation scope.
Represents the results of name lookup.
Definition Lookup.h:147
Data structure that captures multiple levels of template argument lists for use in template instantia...
Definition Template.h:76
bool hasTemplateArgument(unsigned Depth, unsigned Index) const
Determine whether there is a non-NULL template argument at the given depth and index.
Definition Template.h:175
ConstArgListsIterator end() const
Definition Template.h:284
const ArgList & getInnermost() const
Retrieve the innermost template argument list.
Definition Template.h:272
unsigned getNumSubsitutedArgs(unsigned Depth) const
Definition Template.h:134
std::pair< Decl *, bool > getAssociatedDecl(unsigned Depth) const
A template-like entity which owns the whole pattern being substituted.
Definition Template.h:164
void addOuterRetainedLevel()
Add an outermost level that we are not substituting.
Definition Template.h:264
ConstArgListsIterator begin() const
Definition Template.h:280
LLVM_DUMP_METHOD void dump() const
Definition Template.h:286
MultiLevelTemplateArgumentList(Decl *D, ArgList Args, bool Final)
Construct a single-level template argument list.
Definition Template.h:105
bool isAnyArgInstantiationDependent(const ASTContext &C) const
Definition Template.h:188
TemplateSubstitutionKind getKind() const
Determine the kind of template substitution being performed.
Definition Template.h:112
void addOuterTemplateArguments(Decl *AssociatedDecl, ArgList Args, bool Final)
Add a new outmost level to the multi-level template argument list.
Definition Template.h:213
unsigned getNumLevels() const
Determine the number of levels in this template argument list.
Definition Template.h:123
void setKind(TemplateSubstitutionKind K)
Definition Template.h:109
unsigned getNumSubstitutedLevels() const
Determine the number of substituted levels in this template argument list.
Definition Template.h:129
const TemplateArgument & operator()(unsigned Depth, unsigned Index) const
Retrieve the template argument at a given depth and index.
Definition Template.h:154
void replaceOutermostTemplateArguments(Decl *AssociatedDecl, ArgList Args)
Definition Template.h:254
const ArgList & getOutermost() const
Retrieve the outermost template argument list.
Definition Template.h:276
void addOuterRetainedLevels(unsigned Num)
Definition Template.h:267
MultiLevelTemplateArgumentList()=default
Construct an empty set of template argument lists.
unsigned getNumRetainedOuterLevels() const
Definition Template.h:139
unsigned getNewDepth(unsigned OldDepth) const
Determine how many of the OldDepth outermost template parameter lists would be removed by substitutin...
Definition Template.h:145
void setArgument(unsigned Depth, unsigned Index, TemplateArgument Arg)
Clear out a specific template argument.
Definition Template.h:200
void addOuterTemplateArguments(std::nullopt_t)
Definition Template.h:231
void replaceInnermostTemplateArguments(Decl *AssociatedDecl, ArgList Args, bool Final=false)
Replaces the current 'innermost' level with the provided argument list.
Definition Template.h:240
void addOuterTemplateArguments(ArgList Args)
Definition Template.h:224
bool isRewrite() const
Determine whether we are rewriting template parameters rather than substituting for them.
Definition Template.h:117
This represents a decl that may have a name.
Definition Decl.h:274
Represents a parameter to a function.
Definition Decl.h:1790
A (possibly-)qualified type.
Definition TypeBase.h:937
Scope - A scope is a transient data structure that is used while parsing the program.
Definition Scope.h:41
RAII object used to change the argument pack substitution index within a Sema object.
Definition Sema.h:13724
Sema - This implements semantic analysis and AST building for C.
Definition Sema.h:868
SmallVector< LateInstantiatedAttribute, 1 > LateInstantiatedAttrVec
Definition Sema.h:14216
Represents the declaration of a struct/union/class/enum.
Definition Decl.h:3717
Represents a template argument.
constexpr TemplateArgument()
Construct an empty, invalid template argument.
bool isNull() const
Determine whether this template argument has no value.
void enableLateAttributeInstantiation(Sema::LateInstantiatedAttrVec *LA)
Definition Template.h:674
SmallVectorImpl< std::pair< ClassTemplateDecl *, ClassTemplatePartialSpecializationDecl * > >::iterator delayed_partial_spec_iterator
Definition Template.h:687
delayed_var_partial_spec_iterator delayed_var_partial_spec_end()
Definition Template.h:713
delayed_partial_spec_iterator delayed_partial_spec_begin()
Return an iterator to the beginning of the set of "delayed" partial specializations,...
Definition Template.h:697
void setEvaluateConstraints(bool B)
Definition Template.h:615
TemplateDeclInstantiator(Sema &SemaRef, DeclContext *Owner, const MultiLevelTemplateArgumentList &TemplateArgs)
Definition Template.h:610
VarTemplateSpecializationDecl * VisitVarTemplateSpecializationDecl(VarTemplateDecl *VarTemplate, VarDecl *FromVar, ArrayRef< TemplateArgument > Converted, VarTemplateSpecializationDecl *PrevDecl=nullptr)
Decl * VisitVarDecl(VarDecl *D, bool InstantiatingVarTemplate, ArrayRef< BindingDecl * > *Bindings=nullptr)
bool InitMethodInstantiation(CXXMethodDecl *New, CXXMethodDecl *Tmpl)
Initializes common fields of an instantiated method declaration (New) from the corresponding fields o...
LocalInstantiationScope * getStartingScope() const
Definition Template.h:685
bool InitFunctionInstantiation(FunctionDecl *New, FunctionDecl *Tmpl)
Initializes the common fields of an instantiation function declaration (New) from the corresponding f...
VarTemplatePartialSpecializationDecl * InstantiateVarTemplatePartialSpecialization(VarTemplateDecl *VarTemplate, VarTemplatePartialSpecializationDecl *PartialSpec)
Instantiate the declaration of a variable template partial specialization.
void adjustForRewrite(RewriteKind RK, FunctionDecl *Orig, QualType &T, TypeSourceInfo *&TInfo, DeclarationNameInfo &NameInfo)
TypeSourceInfo * SubstFunctionType(FunctionDecl *D, SmallVectorImpl< ParmVarDecl * > &Params)
void InstantiateEnumDefinition(EnumDecl *Enum, EnumDecl *Pattern)
Decl * VisitFunctionDecl(FunctionDecl *D, TemplateParameterList *TemplateParams, RewriteKind RK=RewriteKind::None)
Normal class members are of more specific types and therefore don't make it here.
Decl * VisitCXXMethodDecl(CXXMethodDecl *D, TemplateParameterList *TemplateParams, RewriteKind RK=RewriteKind::None)
SmallVectorImpl< std::pair< VarTemplateDecl *, VarTemplatePartialSpecializationDecl * > >::iterator delayed_var_partial_spec_iterator
Definition Template.h:690
Decl * InstantiateTypeAliasTemplateDecl(TypeAliasTemplateDecl *D)
delayed_partial_spec_iterator delayed_partial_spec_end()
Return an iterator to the end of the set of "delayed" partial specializations, which must be passed t...
Definition Template.h:709
Decl * VisitBaseUsingDecls(BaseUsingDecl *D, BaseUsingDecl *Inst, LookupResult *Lookup)
bool SubstQualifier(const DeclaratorDecl *OldDecl, DeclaratorDecl *NewDecl)
TemplateParameterList * SubstTemplateParams(TemplateParameterList *List)
Instantiates a nested template parameter list in the current instantiation context.
Decl * InstantiateTypedefNameDecl(TypedefNameDecl *D, bool IsTypeAlias)
delayed_var_partial_spec_iterator delayed_var_partial_spec_begin()
Definition Template.h:701
ClassTemplatePartialSpecializationDecl * InstantiateClassTemplatePartialSpecialization(ClassTemplateDecl *ClassTemplate, ClassTemplatePartialSpecializationDecl *PartialSpec)
Instantiate the declaration of a class template partial specialization.
bool SubstDefaultedFunction(FunctionDecl *New, FunctionDecl *Tmpl)
Stores a list of template parameters for a TemplateDecl and its derived classes.
Declaration of an alias template.
A container of type source information.
Definition TypeBase.h:8359
Base class for declarations which introduce a typedef-name.
Definition Decl.h:3562
Represents a variable declaration or definition.
Definition Decl.h:926
Declaration of a variable template.
Represents a variable template specialization, which refers to a variable template with a given set o...
The JSON file list parser is used to communicate input to InstallAPI.
TemplateSubstitutionKind
The kind of template substitution being performed.
Definition Template.h:47
@ Rewrite
We are substituting template parameters for (typically) other template parameters in order to rewrite...
Definition Template.h:54
@ Specialization
We are substituting template parameters for template arguments in order to form a template specializa...
Definition Template.h:50
@ VarTemplate
The name was classified as a variable template name.
Definition Sema.h:585
TPOC
The context in which partial ordering of function templates occurs.
Definition Template.h:305
@ TPOC_Conversion
Partial ordering of function templates for a call to a conversion function.
Definition Template.h:311
@ TPOC_Other
Partial ordering of function templates in other contexts, e.g., taking the address of a function temp...
Definition Template.h:316
@ TPOC_Call
Partial ordering of function templates for a function call.
Definition Template.h:307
U cast(CodeGen::Address addr)
Definition Address.h:327
@ Enum
The "enum" keyword introduces the elaborated-type-specifier.
Definition TypeBase.h:5928
DeclarationNameInfo - A collector data type for bundling together a DeclarationName and the correspon...
Describes how types, statements, expressions, and declarations should be printed.