clang 24.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 bool RetainInnerDepths = false;
101
102 public:
103 /// Construct an empty set of template argument lists.
105
106 /// Construct a single-level template argument list.
107 MultiLevelTemplateArgumentList(Decl *D, ArgList Args, bool Final) {
108 addOuterTemplateArguments(D, Args, Final);
109 }
110
111 void setKind(TemplateSubstitutionKind K) { Kind = K; }
112
113 void setRetainInnerDepths() { RetainInnerDepths = true; }
114
115 bool retainInnerDepths() const { return RetainInnerDepths; }
116
117 /// Determine the kind of template substitution being performed.
118 TemplateSubstitutionKind getKind() const { return Kind; }
119
120 /// Determine whether we are rewriting template parameters rather than
121 /// substituting for them. If so, we should not leave references to the
122 /// original template parameters behind.
123 bool isRewrite() const {
125 }
126
127 /// Determine the number of levels in this template argument
128 /// list.
129 unsigned getNumLevels() const {
130 return TemplateArgumentLists.size() + NumRetainedOuterLevels;
131 }
132
133 /// Determine the number of substituted levels in this template
134 /// argument list.
135 unsigned getNumSubstitutedLevels() const {
136 return TemplateArgumentLists.size();
137 }
138
139 // Determine the number of substituted args at 'Depth'.
140 unsigned getNumSubsitutedArgs(unsigned Depth) const {
141 assert(NumRetainedOuterLevels <= Depth && Depth < getNumLevels());
142 return TemplateArgumentLists[getNumLevels() - Depth - 1].Args.size();
143 }
144
145 unsigned getNumRetainedOuterLevels() const {
146 return NumRetainedOuterLevels;
147 }
148
149 /// Determine how many of the \p OldDepth outermost template parameter
150 /// lists would be removed by substituting these arguments.
151 unsigned getNewDepth(unsigned OldDepth) const {
152 if (OldDepth < NumRetainedOuterLevels)
153 return OldDepth;
154 if (OldDepth < getNumLevels())
155 return NumRetainedOuterLevels;
156 return OldDepth - TemplateArgumentLists.size();
157 }
158
159 /// Retrieve the template argument at a given depth and index.
160 const TemplateArgument &operator()(unsigned Depth, unsigned Index) const {
161 assert(NumRetainedOuterLevels <= Depth && Depth < getNumLevels());
162 assert(Index <
163 TemplateArgumentLists[getNumLevels() - Depth - 1].Args.size());
164 return TemplateArgumentLists[getNumLevels() - Depth - 1].Args[Index];
165 }
166
167 /// A template-like entity which owns the whole pattern being substituted.
168 /// This will usually own a set of template parameters, or in some
169 /// cases might even be a template parameter itself.
170 std::pair<Decl *, bool> getAssociatedDecl(unsigned Depth) const {
171 assert(NumRetainedOuterLevels <= Depth && Depth < getNumLevels());
172 auto AD = TemplateArgumentLists[getNumLevels() - Depth - 1]
173 .AssociatedDeclAndFinal;
174 return {AD.getPointer(), AD.getInt()};
175 }
176
177 /// Determine whether there is a non-NULL template argument at the
178 /// given depth and index.
179 ///
180 /// There must exist a template argument list at the given depth.
181 bool hasTemplateArgument(unsigned Depth, unsigned Index) const {
182 assert(Depth < getNumLevels());
183
184 if (Depth < NumRetainedOuterLevels)
185 return false;
186
187 if (Index >=
188 TemplateArgumentLists[getNumLevels() - Depth - 1].Args.size())
189 return false;
190
191 return !(*this)(Depth, Index).isNull();
192 }
193
195 for (ArgumentListLevel ListLevel : TemplateArgumentLists)
196 for (const TemplateArgument &TA : ListLevel.Args)
197 // There might be null template arguments representing unused template
198 // parameter mappings in an MLTAL during concept checking.
199 if (!TA.isNull() && TA.isInstantiationDependent())
200 return true;
201 return false;
202 }
203
204 /// Clear out a specific template argument.
205 void setArgument(unsigned Depth, unsigned Index,
206 TemplateArgument Arg) {
207 assert(NumRetainedOuterLevels <= Depth && Depth < getNumLevels());
208 assert(Index <
209 TemplateArgumentLists[getNumLevels() - Depth - 1].Args.size());
210 const_cast<TemplateArgument &>(
211 TemplateArgumentLists[getNumLevels() - Depth - 1].Args[Index]) = Arg;
212 }
213
214 /// Add a new outmost level to the multi-level template argument
215 /// list.
216 /// A 'Final' substitution means that these Args don't need to be
217 /// resugared later.
218 void addOuterTemplateArguments(Decl *AssociatedDecl, ArgList Args,
219 bool Final) {
220 assert(!NumRetainedOuterLevels &&
221 "substituted args outside retained args?");
223 TemplateArgumentLists.push_back(
224 {{AssociatedDecl ? AssociatedDecl->getCanonicalDecl() : nullptr,
225 Final},
226 Args});
227 }
228
229 void addOuterTemplateArguments(ArgList Args) {
230 assert(!NumRetainedOuterLevels &&
231 "substituted args outside retained args?");
233 TemplateArgumentLists.push_back({{}, Args});
234 }
235
236 void addOuterTemplateArguments(std::nullopt_t) {
237 assert(!NumRetainedOuterLevels &&
238 "substituted args outside retained args?");
239 TemplateArgumentLists.push_back({});
240 }
241
242 /// Replaces the current 'innermost' level with the provided argument list.
243 /// This is useful for type deduction cases where we need to get the entire
244 /// list from the AST, but then add the deduced innermost list.
245 void replaceInnermostTemplateArguments(Decl *AssociatedDecl, ArgList Args,
246 bool Final = false) {
247 assert((!TemplateArgumentLists.empty() || NumRetainedOuterLevels) &&
248 "Replacing in an empty list?");
249
250 if (!TemplateArgumentLists.empty()) {
251 TemplateArgumentLists[0].Args = Args;
252 return;
253 }
254 --NumRetainedOuterLevels;
255 TemplateArgumentLists.push_back(
256 {{AssociatedDecl, /*Final=*/Final}, Args});
257 }
258
259 void replaceOutermostTemplateArguments(Decl *AssociatedDecl, ArgList Args) {
260 assert((!TemplateArgumentLists.empty()) && "Replacing in an empty list?");
261 TemplateArgumentLists.back().AssociatedDeclAndFinal.setPointer(
262 AssociatedDecl);
263 TemplateArgumentLists.back().Args = Args;
264 }
265
266 /// Add an outermost level that we are not substituting. We have no
267 /// arguments at this level, and do not remove it from the depth of inner
268 /// template parameters that we instantiate.
270 ++NumRetainedOuterLevels;
271 }
273 NumRetainedOuterLevels += Num;
274 }
275
276 /// Retrieve the innermost template argument list.
277 const ArgList &getInnermost() const {
278 return TemplateArgumentLists.front().Args;
279 }
280 /// Retrieve the outermost template argument list.
281 const ArgList &getOutermost() const {
282 return TemplateArgumentLists.back().Args;
283 }
284 ArgListsIterator begin() { return TemplateArgumentLists.begin(); }
285 ConstArgListsIterator begin() const {
286 return TemplateArgumentLists.begin();
287 }
288 ArgListsIterator end() { return TemplateArgumentLists.end(); }
289 ConstArgListsIterator end() const { return TemplateArgumentLists.end(); }
290
291 LLVM_DUMP_METHOD void dump() const {
292 LangOptions LO;
293 LO.CPlusPlus = true;
294 LO.Bool = true;
295 PrintingPolicy PP(LO);
296 llvm::errs() << "NumRetainedOuterLevels: " << NumRetainedOuterLevels
297 << "\n";
298 for (unsigned Depth = NumRetainedOuterLevels; Depth < getNumLevels();
299 ++Depth) {
300 llvm::errs() << Depth << ": ";
301 printTemplateArgumentList(
302 llvm::errs(),
303 TemplateArgumentLists[getNumLevels() - Depth - 1].Args, PP);
304 llvm::errs() << "\n";
305 }
306 }
307 };
308
309 /// The context in which partial ordering of function templates occurs.
310 enum TPOC {
311 /// Partial ordering of function templates for a function call.
313
314 /// Partial ordering of function templates for a call to a
315 /// conversion function.
317
318 /// Partial ordering of function templates in other contexts, e.g.,
319 /// taking the address of a function template or matching a function
320 /// template specialization to a function template.
322 };
323
324 // This is lame but unavoidable in a world without forward
325 // declarations of enums. The alternatives are to either pollute
326 // Sema.h (by including this file) or sacrifice type safety (by
327 // making Sema.h declare things as enums).
329 TPOC Value;
330
331 public:
332 TemplatePartialOrderingContext(TPOC Value) : Value(Value) {}
333
334 operator TPOC() const { return Value; }
335 };
336
337 /// Captures a template argument whose value has been deduced
338 /// via c++ template argument deduction.
340 /// For a non-type template argument, whether the value was
341 /// deduced from an array bound.
342 bool DeducedFromArrayBound = false;
343
344 public:
346
348 bool DeducedFromArrayBound = false)
349 : TemplateArgument(Arg), DeducedFromArrayBound(DeducedFromArrayBound) {}
350
351 /// Construct an integral non-type template argument that
352 /// has been deduced, possibly from an array bound.
354 const llvm::APSInt &Value,
355 QualType ValueType,
356 bool DeducedFromArrayBound)
357 : TemplateArgument(Ctx, Value, ValueType),
358 DeducedFromArrayBound(DeducedFromArrayBound) {}
359
360 /// For a non-type template argument, determine whether the
361 /// template argument was deduced from an array bound.
362 bool wasDeducedFromArrayBound() const { return DeducedFromArrayBound; }
363
364 /// Specify whether the given non-type template argument
365 /// was deduced from an array bound.
367 DeducedFromArrayBound = Deduced;
368 }
369 };
370
371 /// A stack-allocated class that identifies which local
372 /// variable declaration instantiations are present in this scope.
373 ///
374 /// A new instance of this class type will be created whenever we
375 /// instantiate a new function declaration, which will have its own
376 /// set of parameter declarations.
378 public:
379 /// A set of declarations.
381
382 private:
383 /// Reference to the semantic analysis that is performing
384 /// this template instantiation.
385 Sema &SemaRef;
386
387 using LocalDeclsMap =
388 llvm::SmallDenseMap<const Decl *,
389 llvm::PointerUnion<Decl *, DeclArgumentPack *>, 4>;
390
391 /// A mapping from local declarations that occur
392 /// within a template to their instantiations.
393 ///
394 /// This mapping is used during instantiation to keep track of,
395 /// e.g., function parameter and variable declarations. For example,
396 /// given:
397 ///
398 /// \code
399 /// template<typename T> T add(T x, T y) { return x + y; }
400 /// \endcode
401 ///
402 /// when we instantiate add<int>, we will introduce a mapping from
403 /// the ParmVarDecl for 'x' that occurs in the template to the
404 /// instantiated ParmVarDecl for 'x'.
405 ///
406 /// For a parameter pack, the local instantiation scope may contain a
407 /// set of instantiated parameters. This is stored as a DeclArgumentPack
408 /// pointer.
409 LocalDeclsMap LocalDecls;
410
411 /// The set of argument packs we've allocated.
413
414 /// The outer scope, which contains local variable
415 /// definitions from some other instantiation (that may not be
416 /// relevant to this particular scope).
418
419 /// Whether we have already exited this scope.
420 bool Exited = false;
421
422 /// Whether to combine this scope with the outer scope, such that
423 /// lookup will search our outer scope.
424 bool CombineWithOuterScope;
425
426 /// Whether this scope is being used to instantiate a lambda or block
427 /// expression, in which case it should be reused for instantiating the
428 /// lambda's FunctionProtoType.
429 bool InstantiatingLambdaOrBlock = false;
430
431 /// If non-NULL, the template parameter pack that has been
432 /// partially substituted per C++0x [temp.arg.explicit]p9.
433 NamedDecl *PartiallySubstitutedPack = nullptr;
434
435 /// If \c PartiallySubstitutedPack is non-null, the set of
436 /// explicitly-specified template arguments in that pack.
437 const TemplateArgument *ArgsInPartiallySubstitutedPack;
438
439 /// If \c PartiallySubstitutedPack, the number of
440 /// explicitly-specified template arguments in
441 /// ArgsInPartiallySubstitutedPack.
442 unsigned NumArgsInPartiallySubstitutedPack;
443
444 public:
445 LocalInstantiationScope(Sema &SemaRef, bool CombineWithOuterScope = false,
446 bool InstantiatingLambdaOrBlock = false)
447 : SemaRef(SemaRef), Outer(SemaRef.CurrentInstantiationScope),
448 CombineWithOuterScope(CombineWithOuterScope),
449 InstantiatingLambdaOrBlock(InstantiatingLambdaOrBlock) {
450 SemaRef.CurrentInstantiationScope = this;
451 }
452
456
460
461 const Sema &getSema() const { return SemaRef; }
462
463 /// Exit this local instantiation scope early.
464 void Exit() {
465 if (Exited)
466 return;
467
468 for (unsigned I = 0, N = ArgumentPacks.size(); I != N; ++I)
469 delete ArgumentPacks[I];
470
471 SemaRef.CurrentInstantiationScope = Outer;
472 Exited = true;
473 }
474
475 /// Clone this scope, and all outer scopes, down to the given
476 /// outermost scope.
478 if (this == Outermost) return this;
479
480 // Save the current scope from SemaRef since the LocalInstantiationScope
481 // will overwrite it on construction
482 LocalInstantiationScope *oldScope = SemaRef.CurrentInstantiationScope;
483
484 LocalInstantiationScope *newScope =
485 new LocalInstantiationScope(SemaRef, CombineWithOuterScope);
486
487 newScope->Outer = nullptr;
488 if (Outer)
489 newScope->Outer = Outer->cloneScopes(Outermost);
490
491 newScope->PartiallySubstitutedPack = PartiallySubstitutedPack;
492 newScope->ArgsInPartiallySubstitutedPack = ArgsInPartiallySubstitutedPack;
493 newScope->NumArgsInPartiallySubstitutedPack =
494 NumArgsInPartiallySubstitutedPack;
495
496 for (LocalDeclsMap::iterator I = LocalDecls.begin(), E = LocalDecls.end();
497 I != E; ++I) {
498 const Decl *D = I->first;
499 llvm::PointerUnion<Decl *, DeclArgumentPack *> &Stored =
500 newScope->LocalDecls[D];
501 if (auto *D2 = dyn_cast<Decl *>(I->second)) {
502 Stored = D2;
503 } else {
504 DeclArgumentPack *OldPack = cast<DeclArgumentPack *>(I->second);
505 DeclArgumentPack *NewPack = new DeclArgumentPack(*OldPack);
506 Stored = NewPack;
507 newScope->ArgumentPacks.push_back(NewPack);
508 }
509 }
510 // Restore the saved scope to SemaRef
511 SemaRef.CurrentInstantiationScope = oldScope;
512 return newScope;
513 }
514
515 /// deletes the given scope, and all outer scopes, down to the
516 /// given outermost scope.
518 LocalInstantiationScope *Outermost) {
519 while (Scope != Outermost) {
520 assert(Scope && "scope does not extend the outermost scope");
521 LocalInstantiationScope *Out = Scope->Outer;
522 delete Scope;
523 Scope = Out;
524 }
525 }
526
527 /// Find the instantiation of the declaration D within the current
528 /// instantiation scope.
529 ///
530 /// \param D The declaration whose instantiation we are searching for.
531 ///
532 /// \returns A pointer to the declaration or argument pack of declarations
533 /// to which the declaration \c D is instantiated, if found. Otherwise,
534 /// returns NULL.
535 llvm::PointerUnion<Decl *, DeclArgumentPack *> *
536 findInstantiationOf(const Decl *D);
537
538 /// Similar to \p findInstantiationOf(), but it wouldn't assert if the
539 /// instantiation was not found within the current instantiation scope. This
540 /// is helpful for on-demand declaration instantiation.
541 llvm::PointerUnion<Decl *, DeclArgumentPack *> *
543
544 void InstantiatedLocal(const Decl *D, Decl *Inst);
545 void InstantiatedLocalPackArg(const Decl *D, VarDecl *Inst);
546 void MakeInstantiatedLocalArgPack(const Decl *D);
547
548 /// Note that the given parameter pack has been partially substituted
549 /// via explicit specification of template arguments
550 /// (C++0x [temp.arg.explicit]p9).
551 ///
552 /// \param Pack The parameter pack, which will always be a template
553 /// parameter pack.
554 ///
555 /// \param ExplicitArgs The explicitly-specified template arguments provided
556 /// for this parameter pack.
557 ///
558 /// \param NumExplicitArgs The number of explicitly-specified template
559 /// arguments provided for this parameter pack.
561 const TemplateArgument *ExplicitArgs,
562 unsigned NumExplicitArgs);
563
564 /// Reset the partially-substituted pack when it is no longer of
565 /// interest.
567 assert(PartiallySubstitutedPack && "No partially-substituted pack");
568 PartiallySubstitutedPack = nullptr;
569 ArgsInPartiallySubstitutedPack = nullptr;
570 NumArgsInPartiallySubstitutedPack = 0;
571 }
572
573 /// Retrieve the partially-substitued template parameter pack.
574 ///
575 /// If there is no partially-substituted parameter pack, returns NULL.
576 NamedDecl *
577 getPartiallySubstitutedPack(const TemplateArgument **ExplicitArgs = nullptr,
578 unsigned *NumExplicitArgs = nullptr) const;
579
580 /// Determine whether D is a pack expansion created in this scope.
581 bool isLocalPackExpansion(const Decl *D);
582
583 /// Determine whether this scope is for instantiating a lambda or block.
584 bool isLambdaOrBlock() const { return InstantiatingLambdaOrBlock; }
585 };
586
588 : public DeclVisitor<TemplateDeclInstantiator, Decl *>
589 {
590 Sema &SemaRef;
592 DeclContext *Owner;
593 const MultiLevelTemplateArgumentList &TemplateArgs;
594 Sema::LateInstantiatedAttrVec* LateAttrs = nullptr;
595 LocalInstantiationScope *StartingScope = nullptr;
596 // Whether to evaluate the C++20 constraints or simply substitute into them.
597 bool EvaluateConstraints = true;
598
599 /// A list of out-of-line class template partial
600 /// specializations that will need to be instantiated after the
601 /// enclosing class's instantiation is complete.
602 SmallVector<std::pair<ClassTemplateDecl *,
604 1>
605 OutOfLinePartialSpecs;
606
607 /// A list of out-of-line variable template partial
608 /// specializations that will need to be instantiated after the
609 /// enclosing variable's instantiation is complete.
610 /// FIXME: Verify that this is needed.
612 std::pair<VarTemplateDecl *, VarTemplatePartialSpecializationDecl *>, 1>
613 OutOfLineVarPartialSpecs;
614
615 public:
617 const MultiLevelTemplateArgumentList &TemplateArgs)
618 : SemaRef(SemaRef), SubstIndex(SemaRef, SemaRef.ArgPackSubstIndex),
619 Owner(Owner), TemplateArgs(TemplateArgs) {}
620
622 EvaluateConstraints = B;
623 }
625 return EvaluateConstraints;
626 }
627
628// Define all the decl visitors using DeclNodes.inc
629#define DECL(DERIVED, BASE) \
630 Decl *Visit ## DERIVED ## Decl(DERIVED ## Decl *D);
631#define ABSTRACT_DECL(DECL)
632
633// Decls which never appear inside a class or function.
634#define OBJCCONTAINER(DERIVED, BASE)
635#define FILESCOPEASM(DERIVED, BASE)
636#define TOPLEVELSTMT(DERIVED, BASE)
637#define IMPORT(DERIVED, BASE)
638#define EXPORT(DERIVED, BASE)
639#define LINKAGESPEC(DERIVED, BASE)
640#define OBJCCOMPATIBLEALIAS(DERIVED, BASE)
641#define OBJCMETHOD(DERIVED, BASE)
642#define OBJCTYPEPARAM(DERIVED, BASE)
643#define OBJCIVAR(DERIVED, BASE)
644#define OBJCPROPERTY(DERIVED, BASE)
645#define OBJCPROPERTYIMPL(DERIVED, BASE)
646#define EMPTY(DERIVED, BASE)
647#define LIFETIMEEXTENDEDTEMPORARY(DERIVED, BASE)
648
649// Decls which never appear inside a template.
650#define OUTLINEDFUNCTION(DERIVED, BASE)
651
652// Decls which use special-case instantiation code.
653#define BLOCK(DERIVED, BASE)
654#define CAPTURED(DERIVED, BASE)
655#define IMPLICITPARAM(DERIVED, BASE)
656
657#include "clang/AST/DeclNodes.inc"
658
660
662 TypeSourceInfo *&TInfo,
663 DeclarationNameInfo &NameInfo);
664
665 // A few supplemental visitor functions.
667 TemplateParameterList *TemplateParams,
670 TemplateParameterList *TemplateParams,
672 Decl *VisitDecl(Decl *D);
673 Decl *VisitVarDecl(VarDecl *D, bool InstantiatingVarTemplate,
676 LookupResult *Lookup);
677
678 // Enable late instantiation of attributes. Late instantiated attributes
679 // will be stored in LA.
681 LateAttrs = LA;
682 StartingScope = SemaRef.CurrentInstantiationScope;
683 }
684
685 // Disable late instantiation of attributes.
687 LateAttrs = nullptr;
688 StartingScope = nullptr;
689 }
690
691 LocalInstantiationScope *getStartingScope() const { return StartingScope; }
692
695
698
699 /// Return an iterator to the beginning of the set of
700 /// "delayed" partial specializations, which must be passed to
701 /// InstantiateClassTemplatePartialSpecialization once the class
702 /// definition has been completed.
704 return OutOfLinePartialSpecs.begin();
705 }
706
708 return OutOfLineVarPartialSpecs.begin();
709 }
710
711 /// Return an iterator to the end of the set of
712 /// "delayed" partial specializations, which must be passed to
713 /// InstantiateClassTemplatePartialSpecialization once the class
714 /// definition has been completed.
716 return OutOfLinePartialSpecs.end();
717 }
718
720 return OutOfLineVarPartialSpecs.end();
721 }
722
723 // Helper functions for instantiating methods.
728
730
733
737
738 bool SubstQualifier(const DeclaratorDecl *OldDecl,
739 DeclaratorDecl *NewDecl);
740 bool SubstQualifier(const TagDecl *OldDecl,
741 TagDecl *NewDecl);
742
746 VarTemplateSpecializationDecl *PrevDecl = nullptr);
747
749
750 Decl *InstantiateTypedefNameDecl(TypedefNameDecl *D, bool IsTypeAlias);
754 ClassTemplateDecl *ClassTemplate,
761
762 private:
763 template<typename T>
764 Decl *instantiateUnresolvedUsingDecl(T *D,
765 bool InstantiatingPackElement = false);
766 };
767
768} // namespace clang
769
770#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:245
Represents a C++ declaration that introduces decls from somewhere else.
Definition DeclCXX.h:3534
A binding in a decomposition declaration.
Definition DeclCXX.h:4223
Represents a static or instance method of a struct/union/class.
Definition DeclCXX.h:2153
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:1467
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:996
Represents a ValueDecl that came out of a declarator.
Definition Decl.h:782
void setDeducedFromArrayBound(bool Deduced)
Specify whether the given non-type template argument was deduced from an array bound.
Definition Template.h:366
DeducedTemplateArgument(const TemplateArgument &Arg, bool DeducedFromArrayBound=false)
Definition Template.h:347
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:353
bool wasDeducedFromArrayBound() const
For a non-type template argument, determine whether the template argument was deduced from an array b...
Definition Template.h:362
Represents an enum.
Definition Decl.h:4192
FriendDecl - Represents the declaration of a friend entity, which can be a function,...
Definition DeclFriend.h:46
Represents a function declaration or definition.
Definition Decl.h:2082
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:377
LocalInstantiationScope(Sema &SemaRef, bool CombineWithOuterScope=false, bool InstantiatingLambdaOrBlock=false)
Definition Template.h:445
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:464
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:380
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:517
const Sema & getSema() const
Definition Template.h:461
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:566
bool isLambdaOrBlock() const
Determine whether this scope is for instantiating a lambda or block.
Definition Template.h:584
LocalInstantiationScope * cloneScopes(LocalInstantiationScope *Outermost)
Clone this scope, and all outer scopes, down to the given outermost scope.
Definition Template.h:477
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:181
ConstArgListsIterator end() const
Definition Template.h:289
const ArgList & getInnermost() const
Retrieve the innermost template argument list.
Definition Template.h:277
unsigned getNumSubsitutedArgs(unsigned Depth) const
Definition Template.h:140
std::pair< Decl *, bool > getAssociatedDecl(unsigned Depth) const
A template-like entity which owns the whole pattern being substituted.
Definition Template.h:170
void addOuterRetainedLevel()
Add an outermost level that we are not substituting.
Definition Template.h:269
ConstArgListsIterator begin() const
Definition Template.h:285
LLVM_DUMP_METHOD void dump() const
Definition Template.h:291
MultiLevelTemplateArgumentList(Decl *D, ArgList Args, bool Final)
Construct a single-level template argument list.
Definition Template.h:107
TemplateSubstitutionKind getKind() const
Determine the kind of template substitution being performed.
Definition Template.h:118
void addOuterTemplateArguments(Decl *AssociatedDecl, ArgList Args, bool Final)
Add a new outmost level to the multi-level template argument list.
Definition Template.h:218
unsigned getNumLevels() const
Determine the number of levels in this template argument list.
Definition Template.h:129
void setKind(TemplateSubstitutionKind K)
Definition Template.h:111
unsigned getNumSubstitutedLevels() const
Determine the number of substituted levels in this template argument list.
Definition Template.h:135
const TemplateArgument & operator()(unsigned Depth, unsigned Index) const
Retrieve the template argument at a given depth and index.
Definition Template.h:160
void replaceOutermostTemplateArguments(Decl *AssociatedDecl, ArgList Args)
Definition Template.h:259
const ArgList & getOutermost() const
Retrieve the outermost template argument list.
Definition Template.h:281
void addOuterRetainedLevels(unsigned Num)
Definition Template.h:272
MultiLevelTemplateArgumentList()=default
Construct an empty set of template argument lists.
unsigned getNumRetainedOuterLevels() const
Definition Template.h:145
unsigned getNewDepth(unsigned OldDepth) const
Determine how many of the OldDepth outermost template parameter lists would be removed by substitutin...
Definition Template.h:151
void setArgument(unsigned Depth, unsigned Index, TemplateArgument Arg)
Clear out a specific template argument.
Definition Template.h:205
void addOuterTemplateArguments(std::nullopt_t)
Definition Template.h:236
void replaceInnermostTemplateArguments(Decl *AssociatedDecl, ArgList Args, bool Final=false)
Replaces the current 'innermost' level with the provided argument list.
Definition Template.h:245
void addOuterTemplateArguments(ArgList Args)
Definition Template.h:229
bool isRewrite() const
Determine whether we are rewriting template parameters rather than substituting for them.
Definition Template.h:123
This represents a decl that may have a name.
Definition Decl.h:276
Represents a parameter to a function.
Definition Decl.h:1832
A (possibly-)qualified type.
Definition TypeBase.h:938
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:13924
Sema - This implements semantic analysis and AST building for C.
Definition Sema.h:862
SmallVector< LateInstantiatedAttribute, 1 > LateInstantiatedAttrVec
Definition Sema.h:14044
Represents the declaration of a struct/union/class/enum.
Definition Decl.h:3898
Represents a template argument.
bool isInstantiationDependent() const
Whether this template argument is dependent on a template parameter.
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:680
SmallVectorImpl< std::pair< ClassTemplateDecl *, ClassTemplatePartialSpecializationDecl * > >::iterator delayed_partial_spec_iterator
Definition Template.h:693
delayed_var_partial_spec_iterator delayed_var_partial_spec_end()
Definition Template.h:719
delayed_partial_spec_iterator delayed_partial_spec_begin()
Return an iterator to the beginning of the set of "delayed" partial specializations,...
Definition Template.h:703
bool SubstTemplateParameterLists(ArrayRef< TemplateParameterList * > TPLs, SmallVectorImpl< TemplateParameterList * > &InstTPLs)
void setEvaluateConstraints(bool B)
Definition Template.h:621
TemplateDeclInstantiator(Sema &SemaRef, DeclContext *Owner, const MultiLevelTemplateArgumentList &TemplateArgs)
Definition Template.h:616
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:691
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:696
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:715
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:707
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:8549
Base class for declarations which introduce a typedef-name.
Definition Decl.h:3743
Represents a variable declaration or definition.
Definition Decl.h:934
Declaration of a variable template.
Represents a variable template specialization, which refers to a variable template with a given set o...
Top level wrappers for InstallAPI frontend operations.
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
const FunctionProtoType * T
@ VarTemplate
The name was classified as a variable template name.
Definition Sema.h:578
@ Deduced
The normal deduced case.
Definition TypeBase.h:1818
TPOC
The context in which partial ordering of function templates occurs.
Definition Template.h:310
@ TPOC_Conversion
Partial ordering of function templates for a call to a conversion function.
Definition Template.h:316
@ TPOC_Other
Partial ordering of function templates in other contexts, e.g., taking the address of a function temp...
Definition Template.h:321
@ TPOC_Call
Partial ordering of function templates for a function call.
Definition Template.h:312
U cast(CodeGen::Address addr)
Definition Address.h:327
@ Enum
The "enum" keyword introduces the elaborated-type-specifier.
Definition TypeBase.h:6160
DeclarationNameInfo - A collector data type for bundling together a DeclarationName and the correspon...
Describes how types, statements, expressions, and declarations should be printed.