clang  8.0.0
DeclTemplate.h
Go to the documentation of this file.
1 //===- DeclTemplate.h - Classes for representing C++ templates --*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 /// \file
11 /// Defines the C++ template declaration subclasses.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #ifndef LLVM_CLANG_AST_DECLTEMPLATE_H
16 #define LLVM_CLANG_AST_DECLTEMPLATE_H
17 
18 #include "clang/AST/Decl.h"
19 #include "clang/AST/DeclBase.h"
20 #include "clang/AST/DeclCXX.h"
22 #include "clang/AST/Redeclarable.h"
23 #include "clang/AST/TemplateBase.h"
24 #include "clang/AST/Type.h"
25 #include "clang/Basic/LLVM.h"
27 #include "clang/Basic/Specifiers.h"
28 #include "llvm/ADT/ArrayRef.h"
29 #include "llvm/ADT/FoldingSet.h"
30 #include "llvm/ADT/PointerIntPair.h"
31 #include "llvm/ADT/PointerUnion.h"
32 #include "llvm/ADT/iterator.h"
33 #include "llvm/ADT/iterator_range.h"
34 #include "llvm/Support/Casting.h"
35 #include "llvm/Support/Compiler.h"
36 #include "llvm/Support/TrailingObjects.h"
37 #include <cassert>
38 #include <cstddef>
39 #include <cstdint>
40 #include <iterator>
41 #include <utility>
42 
43 namespace clang {
44 
45 enum BuiltinTemplateKind : int;
46 class ClassTemplateDecl;
47 class ClassTemplatePartialSpecializationDecl;
48 class Expr;
49 class FunctionTemplateDecl;
50 class IdentifierInfo;
51 class NonTypeTemplateParmDecl;
52 class TemplateDecl;
53 class TemplateTemplateParmDecl;
54 class TemplateTypeParmDecl;
55 class UnresolvedSetImpl;
56 class VarTemplateDecl;
57 class VarTemplatePartialSpecializationDecl;
58 
59 /// Stores a template parameter of any kind.
60 using TemplateParameter =
61  llvm::PointerUnion3<TemplateTypeParmDecl *, NonTypeTemplateParmDecl *,
63 
65 
66 /// Stores a list of template parameters for a TemplateDecl and its
67 /// derived classes.
69  : private llvm::TrailingObjects<TemplateParameterList, NamedDecl *,
70  Expr *> {
71  /// The location of the 'template' keyword.
72  SourceLocation TemplateLoc;
73 
74  /// The locations of the '<' and '>' angle brackets.
75  SourceLocation LAngleLoc, RAngleLoc;
76 
77  /// The number of template parameters in this template
78  /// parameter list.
79  unsigned NumParams : 30;
80 
81  /// Whether this template parameter list contains an unexpanded parameter
82  /// pack.
83  unsigned ContainsUnexpandedParameterPack : 1;
84 
85  /// Whether this template parameter list has an associated requires-clause
86  unsigned HasRequiresClause : 1;
87 
88 protected:
90  ArrayRef<NamedDecl *> Params, SourceLocation RAngleLoc,
91  Expr *RequiresClause);
92 
93  size_t numTrailingObjects(OverloadToken<NamedDecl *>) const {
94  return NumParams;
95  }
96 
97  size_t numTrailingObjects(OverloadToken<Expr *>) const {
98  return HasRequiresClause;
99  }
100 
101 public:
102  template <size_t N, bool HasRequiresClause>
105 
106  static TemplateParameterList *Create(const ASTContext &C,
107  SourceLocation TemplateLoc,
108  SourceLocation LAngleLoc,
109  ArrayRef<NamedDecl *> Params,
110  SourceLocation RAngleLoc,
111  Expr *RequiresClause);
112 
113  /// Iterates through the template parameters in this list.
114  using iterator = NamedDecl **;
115 
116  /// Iterates through the template parameters in this list.
117  using const_iterator = NamedDecl * const *;
118 
119  iterator begin() { return getTrailingObjects<NamedDecl *>(); }
120  const_iterator begin() const { return getTrailingObjects<NamedDecl *>(); }
121  iterator end() { return begin() + NumParams; }
122  const_iterator end() const { return begin() + NumParams; }
123 
124  unsigned size() const { return NumParams; }
125 
127  return llvm::makeArrayRef(begin(), end());
128  }
130  return llvm::makeArrayRef(begin(), size());
131  }
132 
133  NamedDecl* getParam(unsigned Idx) {
134  assert(Idx < size() && "Template parameter index out-of-range");
135  return begin()[Idx];
136  }
137  const NamedDecl* getParam(unsigned Idx) const {
138  assert(Idx < size() && "Template parameter index out-of-range");
139  return begin()[Idx];
140  }
141 
142  /// Returns the minimum number of arguments needed to form a
143  /// template specialization.
144  ///
145  /// This may be fewer than the number of template parameters, if some of
146  /// the parameters have default arguments or if there is a parameter pack.
147  unsigned getMinRequiredArguments() const;
148 
149  /// Get the depth of this template parameter list in the set of
150  /// template parameter lists.
151  ///
152  /// The first template parameter list in a declaration will have depth 0,
153  /// the second template parameter list will have depth 1, etc.
154  unsigned getDepth() const;
155 
156  /// Determine whether this template parameter list contains an
157  /// unexpanded parameter pack.
159  return ContainsUnexpandedParameterPack;
160  }
161 
162  /// The constraint-expression of the associated requires-clause.
164  return HasRequiresClause ? *getTrailingObjects<Expr *>() : nullptr;
165  }
166 
167  /// The constraint-expression of the associated requires-clause.
168  const Expr *getRequiresClause() const {
169  return HasRequiresClause ? *getTrailingObjects<Expr *>() : nullptr;
170  }
171 
172  SourceLocation getTemplateLoc() const { return TemplateLoc; }
173  SourceLocation getLAngleLoc() const { return LAngleLoc; }
174  SourceLocation getRAngleLoc() const { return RAngleLoc; }
175 
176  SourceRange getSourceRange() const LLVM_READONLY {
177  return SourceRange(TemplateLoc, RAngleLoc);
178  }
179 
180 public:
181  // FIXME: workaround for MSVC 2013; remove when no longer needed
182  using FixedSizeStorageOwner = TrailingObjects::FixedSizeStorageOwner;
183 };
184 
185 /// Stores a list of template parameters and the associated
186 /// requires-clause (if any) for a TemplateDecl and its derived classes.
187 /// Suitable for creating on the stack.
188 template <size_t N, bool HasRequiresClause>
191  typename TemplateParameterList::FixedSizeStorage<
192  NamedDecl *, Expr *>::with_counts<
193  N, HasRequiresClause ? 1u : 0u
194  >::type storage;
195 
196 public:
198  SourceLocation LAngleLoc,
199  ArrayRef<NamedDecl *> Params,
200  SourceLocation RAngleLoc,
201  Expr *RequiresClause)
203  (assert(N == Params.size()),
204  assert(HasRequiresClause == static_cast<bool>(RequiresClause)),
205  new (static_cast<void *>(&storage)) TemplateParameterList(
206  TemplateLoc, LAngleLoc, Params, RAngleLoc, RequiresClause))) {}
207 };
208 
209 /// A template argument list.
211  : private llvm::TrailingObjects<TemplateArgumentList, TemplateArgument> {
212  /// The template argument list.
213  const TemplateArgument *Arguments;
214 
215  /// The number of template arguments in this template
216  /// argument list.
217  unsigned NumArguments;
218 
219  // Constructs an instance with an internal Argument list, containing
220  // a copy of the Args array. (Called by CreateCopy)
222 
223 public:
225 
226  TemplateArgumentList(const TemplateArgumentList &) = delete;
227  TemplateArgumentList &operator=(const TemplateArgumentList &) = delete;
228 
229  /// Type used to indicate that the template argument list itself is a
230  /// stack object. It does not own its template arguments.
231  enum OnStackType { OnStack };
232 
233  /// Create a new template argument list that copies the given set of
234  /// template arguments.
235  static TemplateArgumentList *CreateCopy(ASTContext &Context,
237 
238  /// Construct a new, temporary template argument list on the stack.
239  ///
240  /// The template argument list does not own the template arguments
241  /// provided.
243  : Arguments(Args.data()), NumArguments(Args.size()) {}
244 
245  /// Produces a shallow copy of the given template argument list.
246  ///
247  /// This operation assumes that the input argument list outlives it.
248  /// This takes the list as a pointer to avoid looking like a copy
249  /// constructor, since this really really isn't safe to use that
250  /// way.
252  : Arguments(Other->data()), NumArguments(Other->size()) {}
253 
254  /// Retrieve the template argument at a given index.
255  const TemplateArgument &get(unsigned Idx) const {
256  assert(Idx < NumArguments && "Invalid template argument index");
257  return data()[Idx];
258  }
259 
260  /// Retrieve the template argument at a given index.
261  const TemplateArgument &operator[](unsigned Idx) const { return get(Idx); }
262 
263  /// Produce this as an array ref.
265  return llvm::makeArrayRef(data(), size());
266  }
267 
268  /// Retrieve the number of template arguments in this
269  /// template argument list.
270  unsigned size() const { return NumArguments; }
271 
272  /// Retrieve a pointer to the template argument list.
273  const TemplateArgument *data() const { return Arguments; }
274 };
275 
277 
278 /// Storage for a default argument. This is conceptually either empty, or an
279 /// argument value, or a pointer to a previous declaration that had a default
280 /// argument.
281 ///
282 /// However, this is complicated by modules: while we require all the default
283 /// arguments for a template to be equivalent, there may be more than one, and
284 /// we need to track all the originating parameters to determine if the default
285 /// argument is visible.
286 template<typename ParmDecl, typename ArgType>
288  /// Storage for both the value *and* another parameter from which we inherit
289  /// the default argument. This is used when multiple default arguments for a
290  /// parameter are merged together from different modules.
291  struct Chain {
292  ParmDecl *PrevDeclWithDefaultArg;
293  ArgType Value;
294  };
295  static_assert(sizeof(Chain) == sizeof(void *) * 2,
296  "non-pointer argument type?");
297 
298  llvm::PointerUnion3<ArgType, ParmDecl*, Chain*> ValueOrInherited;
299 
300  static ParmDecl *getParmOwningDefaultArg(ParmDecl *Parm) {
301  const DefaultArgStorage &Storage = Parm->getDefaultArgStorage();
302  if (auto *Prev = Storage.ValueOrInherited.template dyn_cast<ParmDecl *>())
303  Parm = Prev;
304  assert(!Parm->getDefaultArgStorage()
305  .ValueOrInherited.template is<ParmDecl *>() &&
306  "should only be one level of indirection");
307  return Parm;
308  }
309 
310 public:
311  DefaultArgStorage() : ValueOrInherited(ArgType()) {}
312 
313  /// Determine whether there is a default argument for this parameter.
314  bool isSet() const { return !ValueOrInherited.isNull(); }
315 
316  /// Determine whether the default argument for this parameter was inherited
317  /// from a previous declaration of the same entity.
318  bool isInherited() const { return ValueOrInherited.template is<ParmDecl*>(); }
319 
320  /// Get the default argument's value. This does not consider whether the
321  /// default argument is visible.
322  ArgType get() const {
323  const DefaultArgStorage *Storage = this;
324  if (const auto *Prev = ValueOrInherited.template dyn_cast<ParmDecl *>())
325  Storage = &Prev->getDefaultArgStorage();
326  if (const auto *C = Storage->ValueOrInherited.template dyn_cast<Chain *>())
327  return C->Value;
328  return Storage->ValueOrInherited.template get<ArgType>();
329  }
330 
331  /// Get the parameter from which we inherit the default argument, if any.
332  /// This is the parameter on which the default argument was actually written.
333  const ParmDecl *getInheritedFrom() const {
334  if (const auto *D = ValueOrInherited.template dyn_cast<ParmDecl *>())
335  return D;
336  if (const auto *C = ValueOrInherited.template dyn_cast<Chain *>())
337  return C->PrevDeclWithDefaultArg;
338  return nullptr;
339  }
340 
341  /// Set the default argument.
342  void set(ArgType Arg) {
343  assert(!isSet() && "default argument already set");
344  ValueOrInherited = Arg;
345  }
346 
347  /// Set that the default argument was inherited from another parameter.
348  void setInherited(const ASTContext &C, ParmDecl *InheritedFrom) {
349  assert(!isInherited() && "default argument already inherited");
350  InheritedFrom = getParmOwningDefaultArg(InheritedFrom);
351  if (!isSet())
352  ValueOrInherited = InheritedFrom;
353  else
354  ValueOrInherited = new (allocateDefaultArgStorageChain(C))
355  Chain{InheritedFrom, ValueOrInherited.template get<ArgType>()};
356  }
357 
358  /// Remove the default argument, even if it was inherited.
359  void clear() {
360  ValueOrInherited = ArgType();
361  }
362 };
363 
364 //===----------------------------------------------------------------------===//
365 // Kinds of Templates
366 //===----------------------------------------------------------------------===//
367 
368 /// Stores the template parameter list and associated constraints for
369 /// \c TemplateDecl objects that track associated constraints.
371  friend TemplateDecl;
372 
373 public:
374  ConstrainedTemplateDeclInfo() = default;
375 
377  return TemplateParams;
378  }
379 
380  Expr *getAssociatedConstraints() const { return AssociatedConstraints; }
381 
382 protected:
384  TemplateParams = TParams;
385  }
386 
387  void setAssociatedConstraints(Expr *AC) { AssociatedConstraints = AC; }
388 
389  TemplateParameterList *TemplateParams = nullptr;
390  Expr *AssociatedConstraints = nullptr;
391 };
392 
393 
394 /// The base class of all kinds of template declarations (e.g.,
395 /// class, function, etc.).
396 ///
397 /// The TemplateDecl class stores the list of template parameters and a
398 /// reference to the templated scoped declaration: the underlying AST node.
399 class TemplateDecl : public NamedDecl {
400  void anchor() override;
401 
402 protected:
403  // Construct a template decl with the given name and parameters.
404  // Used when there is no templated element (e.g., for tt-params).
407  TemplateParameterList *Params)
408  : NamedDecl(DK, DC, L, Name), TemplatedDecl(nullptr),
409  TemplateParams(CTDI) {
410  this->setTemplateParameters(Params);
411  }
412 
414  TemplateParameterList *Params)
415  : TemplateDecl(nullptr, DK, DC, L, Name, Params) {}
416 
417  // Construct a template decl with name, parameters, and templated element.
421  : NamedDecl(DK, DC, L, Name), TemplatedDecl(Decl),
422  TemplateParams(CTDI) {
423  this->setTemplateParameters(Params);
424  }
425 
428  : TemplateDecl(nullptr, DK, DC, L, Name, Params, Decl) {}
429 
430 public:
431  /// Get the list of template parameters
433  const auto *const CTDI =
434  TemplateParams.dyn_cast<ConstrainedTemplateDeclInfo *>();
435  return CTDI ? CTDI->getTemplateParameters()
436  : TemplateParams.get<TemplateParameterList *>();
437  }
438 
439  /// Get the constraint-expression from the associated requires-clause (if any)
440  const Expr *getRequiresClause() const {
441  const TemplateParameterList *const TP = getTemplateParameters();
442  return TP ? TP->getRequiresClause() : nullptr;
443  }
444 
446  const auto *const C = cast<TemplateDecl>(getCanonicalDecl());
447  const auto *const CTDI =
448  C->TemplateParams.dyn_cast<ConstrainedTemplateDeclInfo *>();
449  return CTDI ? CTDI->getAssociatedConstraints() : nullptr;
450  }
451 
452  /// Get the underlying, templated declaration.
453  NamedDecl *getTemplatedDecl() const { return TemplatedDecl; }
454 
455  // Implement isa/cast/dyncast/etc.
456  static bool classof(const Decl *D) { return classofKind(D->getKind()); }
457 
458  static bool classofKind(Kind K) {
459  return K >= firstTemplate && K <= lastTemplate;
460  }
461 
462  SourceRange getSourceRange() const override LLVM_READONLY {
463  return SourceRange(getTemplateParameters()->getTemplateLoc(),
464  TemplatedDecl->getSourceRange().getEnd());
465  }
466 
467 protected:
469 
470  /// The template parameter list and optional requires-clause
471  /// associated with this declaration; alternatively, a
472  /// \c ConstrainedTemplateDeclInfo if the associated constraints of the
473  /// template are being tracked by this particular declaration.
474  llvm::PointerUnion<TemplateParameterList *,
477 
478  void setTemplateParameters(TemplateParameterList *TParams) {
479  if (auto *const CTDI =
480  TemplateParams.dyn_cast<ConstrainedTemplateDeclInfo *>()) {
481  CTDI->setTemplateParameters(TParams);
482  } else {
483  TemplateParams = TParams;
484  }
485  }
486 
488  assert(isCanonicalDecl() &&
489  "Attaching associated constraints to non-canonical Decl");
490  TemplateParams.get<ConstrainedTemplateDeclInfo *>()
491  ->setAssociatedConstraints(AC);
492  }
493 
494 public:
495  /// Initialize the underlying templated declaration and
496  /// template parameters.
497  void init(NamedDecl *templatedDecl, TemplateParameterList* templateParams) {
498  assert(!TemplatedDecl && "TemplatedDecl already set!");
499  assert(!TemplateParams && "TemplateParams already set!");
500  TemplatedDecl = templatedDecl;
501  TemplateParams = templateParams;
502  }
503 };
504 
505 /// Provides information about a function template specialization,
506 /// which is a FunctionDecl that has been explicitly specialization or
507 /// instantiated from a function template.
508 class FunctionTemplateSpecializationInfo : public llvm::FoldingSetNode {
510  FunctionTemplateDecl *Template,
512  const TemplateArgumentList *TemplateArgs,
513  const ASTTemplateArgumentListInfo *TemplateArgsAsWritten,
514  SourceLocation POI)
515  : Function(FD), Template(Template, TSK - 1),
516  TemplateArguments(TemplateArgs),
517  TemplateArgumentsAsWritten(TemplateArgsAsWritten),
518  PointOfInstantiation(POI) {}
519 
520 public:
524  const TemplateArgumentList *TemplateArgs,
525  const TemplateArgumentListInfo *TemplateArgsAsWritten,
526  SourceLocation POI);
527 
528  /// The function template specialization that this structure
529  /// describes.
531 
532  /// The function template from which this function template
533  /// specialization was generated.
534  ///
535  /// The two bits contain the top 4 values of TemplateSpecializationKind.
536  llvm::PointerIntPair<FunctionTemplateDecl *, 2> Template;
537 
538  /// The template arguments used to produce the function template
539  /// specialization from the function template.
541 
542  /// The template arguments as written in the sources, if provided.
544 
545  /// The point at which this function template specialization was
546  /// first instantiated.
548 
549  /// Retrieve the template from which this function was specialized.
550  FunctionTemplateDecl *getTemplate() const { return Template.getPointer(); }
551 
552  /// Determine what kind of template specialization this is.
554  return (TemplateSpecializationKind)(Template.getInt() + 1);
555  }
556 
559  }
560 
561  /// True if this declaration is an explicit specialization,
562  /// explicit instantiation declaration, or explicit instantiation
563  /// definition.
567  }
568 
569  /// Set the template specialization kind.
571  assert(TSK != TSK_Undeclared &&
572  "Cannot encode TSK_Undeclared for a function template specialization");
573  Template.setInt(TSK - 1);
574  }
575 
576  /// Retrieve the first point of instantiation of this function
577  /// template specialization.
578  ///
579  /// The point of instantiation may be an invalid source location if this
580  /// function has yet to be instantiated.
582  return PointOfInstantiation;
583  }
584 
585  /// Set the (first) point of instantiation of this function template
586  /// specialization.
588  PointOfInstantiation = POI;
589  }
590 
591  void Profile(llvm::FoldingSetNodeID &ID) {
592  Profile(ID, TemplateArguments->asArray(),
593  Function->getASTContext());
594  }
595 
596  static void
597  Profile(llvm::FoldingSetNodeID &ID, ArrayRef<TemplateArgument> TemplateArgs,
598  ASTContext &Context) {
599  ID.AddInteger(TemplateArgs.size());
600  for (const TemplateArgument &TemplateArg : TemplateArgs)
601  TemplateArg.Profile(ID, Context);
602  }
603 };
604 
605 /// Provides information a specialization of a member of a class
606 /// template, which may be a member function, static data member,
607 /// member class or member enumeration.
609  // The member declaration from which this member was instantiated, and the
610  // manner in which the instantiation occurred (in the lower two bits).
611  llvm::PointerIntPair<NamedDecl *, 2> MemberAndTSK;
612 
613  // The point at which this member was first instantiated.
614  SourceLocation PointOfInstantiation;
615 
616 public:
617  explicit
620  : MemberAndTSK(IF, TSK - 1), PointOfInstantiation(POI) {
621  assert(TSK != TSK_Undeclared &&
622  "Cannot encode undeclared template specializations for members");
623  }
624 
625  /// Retrieve the member declaration from which this member was
626  /// instantiated.
627  NamedDecl *getInstantiatedFrom() const { return MemberAndTSK.getPointer(); }
628 
629  /// Determine what kind of template specialization this is.
631  return (TemplateSpecializationKind)(MemberAndTSK.getInt() + 1);
632  }
633 
636  }
637 
638  /// Set the template specialization kind.
640  assert(TSK != TSK_Undeclared &&
641  "Cannot encode undeclared template specializations for members");
642  MemberAndTSK.setInt(TSK - 1);
643  }
644 
645  /// Retrieve the first point of instantiation of this member.
646  /// If the point of instantiation is an invalid location, then this member
647  /// has not yet been instantiated.
649  return PointOfInstantiation;
650  }
651 
652  /// Set the first point of instantiation.
654  PointOfInstantiation = POI;
655  }
656 };
657 
658 /// Provides information about a dependent function-template
659 /// specialization declaration.
660 ///
661 /// Since explicit function template specialization and instantiation
662 /// declarations can only appear in namespace scope, and you can only
663 /// specialize a member of a fully-specialized class, the only way to
664 /// get one of these is in a friend declaration like the following:
665 ///
666 /// \code
667 /// template <class T> void foo(T);
668 /// template <class T> class A {
669 /// friend void foo<>(T);
670 /// };
671 /// \endcode
673  : private llvm::TrailingObjects<DependentFunctionTemplateSpecializationInfo,
674  TemplateArgumentLoc,
675  FunctionTemplateDecl *> {
676  /// The number of potential template candidates.
677  unsigned NumTemplates;
678 
679  /// The number of template arguments.
680  unsigned NumArgs;
681 
682  /// The locations of the left and right angle brackets.
683  SourceRange AngleLocs;
684 
685  size_t numTrailingObjects(OverloadToken<TemplateArgumentLoc>) const {
686  return NumArgs;
687  }
688  size_t numTrailingObjects(OverloadToken<FunctionTemplateDecl *>) const {
689  return NumTemplates;
690  }
691 
693  const UnresolvedSetImpl &Templates,
694  const TemplateArgumentListInfo &TemplateArgs);
695 
696 public:
698 
700  Create(ASTContext &Context, const UnresolvedSetImpl &Templates,
701  const TemplateArgumentListInfo &TemplateArgs);
702 
703  /// Returns the number of function templates that this might
704  /// be a specialization of.
705  unsigned getNumTemplates() const { return NumTemplates; }
706 
707  /// Returns the i'th template candidate.
708  FunctionTemplateDecl *getTemplate(unsigned I) const {
709  assert(I < getNumTemplates() && "template index out of range");
710  return getTrailingObjects<FunctionTemplateDecl *>()[I];
711  }
712 
713  /// Returns the explicit template arguments that were given.
715  return getTrailingObjects<TemplateArgumentLoc>();
716  }
717 
718  /// Returns the number of explicit template arguments that were given.
719  unsigned getNumTemplateArgs() const { return NumArgs; }
720 
721  /// Returns the nth template argument.
722  const TemplateArgumentLoc &getTemplateArg(unsigned I) const {
723  assert(I < getNumTemplateArgs() && "template arg index out of range");
724  return getTemplateArgs()[I];
725  }
726 
728  return AngleLocs.getBegin();
729  }
730 
732  return AngleLocs.getEnd();
733  }
734 };
735 
736 /// Declaration of a redeclarable template.
738  public Redeclarable<RedeclarableTemplateDecl>
739 {
741 
742  RedeclarableTemplateDecl *getNextRedeclarationImpl() override {
743  return getNextRedeclaration();
744  }
745 
746  RedeclarableTemplateDecl *getPreviousDeclImpl() override {
747  return getPreviousDecl();
748  }
749 
750  RedeclarableTemplateDecl *getMostRecentDeclImpl() override {
751  return getMostRecentDecl();
752  }
753 
754  void anchor() override;
755 protected:
756  template <typename EntryType> struct SpecEntryTraits {
757  using DeclType = EntryType;
758 
759  static DeclType *getDecl(EntryType *D) {
760  return D;
761  }
762 
764  return D->getTemplateArgs().asArray();
765  }
766  };
767 
768  template <typename EntryType, typename SETraits = SpecEntryTraits<EntryType>,
769  typename DeclType = typename SETraits::DeclType>
771  : llvm::iterator_adaptor_base<
772  SpecIterator<EntryType, SETraits, DeclType>,
773  typename llvm::FoldingSetVector<EntryType>::iterator,
774  typename std::iterator_traits<typename llvm::FoldingSetVector<
775  EntryType>::iterator>::iterator_category,
776  DeclType *, ptrdiff_t, DeclType *, DeclType *> {
777  SpecIterator() = default;
778  explicit SpecIterator(
779  typename llvm::FoldingSetVector<EntryType>::iterator SetIter)
780  : SpecIterator::iterator_adaptor_base(std::move(SetIter)) {}
781 
782  DeclType *operator*() const {
783  return SETraits::getDecl(&*this->I)->getMostRecentDecl();
784  }
785 
786  DeclType *operator->() const { return **this; }
787  };
788 
789  template <typename EntryType>
791  makeSpecIterator(llvm::FoldingSetVector<EntryType> &Specs, bool isEnd) {
792  return SpecIterator<EntryType>(isEnd ? Specs.end() : Specs.begin());
793  }
794 
795  void loadLazySpecializationsImpl() const;
796 
797  template <class EntryType> typename SpecEntryTraits<EntryType>::DeclType*
798  findSpecializationImpl(llvm::FoldingSetVector<EntryType> &Specs,
799  ArrayRef<TemplateArgument> Args, void *&InsertPos);
800 
801  template <class Derived, class EntryType>
802  void addSpecializationImpl(llvm::FoldingSetVector<EntryType> &Specs,
803  EntryType *Entry, void *InsertPos);
804 
805  struct CommonBase {
806  CommonBase() : InstantiatedFromMember(nullptr, false) {}
807 
808  /// The template from which this was most
809  /// directly instantiated (or null).
810  ///
811  /// The boolean value indicates whether this template
812  /// was explicitly specialized.
813  llvm::PointerIntPair<RedeclarableTemplateDecl*, 1, bool>
815 
816  /// If non-null, points to an array of specializations (including
817  /// partial specializations) known only by their external declaration IDs.
818  ///
819  /// The first value in the array is the number of specializations/partial
820  /// specializations that follow.
821  uint32_t *LazySpecializations = nullptr;
822  };
823 
824  /// Pointer to the common data shared by all declarations of this
825  /// template.
826  mutable CommonBase *Common = nullptr;
827 
828  /// Retrieves the "common" pointer shared by all (re-)declarations of
829  /// the same template. Calling this routine may implicitly allocate memory
830  /// for the common pointer.
831  CommonBase *getCommonPtr() const;
832 
833  virtual CommonBase *newCommon(ASTContext &C) const = 0;
834 
835  // Construct a template decl with name, parameters, and templated element.
839  NamedDecl *Decl)
840  : TemplateDecl(CTDI, DK, DC, L, Name, Params, Decl), redeclarable_base(C)
841  {}
842 
846  : RedeclarableTemplateDecl(nullptr, DK, C, DC, L, Name, Params, Decl) {}
847 
848 public:
849  friend class ASTDeclReader;
850  friend class ASTDeclWriter;
851  friend class ASTReader;
852  template <class decl_type> friend class RedeclarableTemplate;
853 
854  /// Retrieves the canonical declaration of this template.
856  return getFirstDecl();
857  }
859  return getFirstDecl();
860  }
861 
862  /// Determines whether this template was a specialization of a
863  /// member template.
864  ///
865  /// In the following example, the function template \c X<int>::f and the
866  /// member template \c X<int>::Inner are member specializations.
867  ///
868  /// \code
869  /// template<typename T>
870  /// struct X {
871  /// template<typename U> void f(T, U);
872  /// template<typename U> struct Inner;
873  /// };
874  ///
875  /// template<> template<typename T>
876  /// void X<int>::f(int, T);
877  /// template<> template<typename T>
878  /// struct X<int>::Inner { /* ... */ };
879  /// \endcode
880  bool isMemberSpecialization() const {
881  return getCommonPtr()->InstantiatedFromMember.getInt();
882  }
883 
884  /// Note that this member template is a specialization.
886  assert(getCommonPtr()->InstantiatedFromMember.getPointer() &&
887  "Only member templates can be member template specializations");
888  getCommonPtr()->InstantiatedFromMember.setInt(true);
889  }
890 
891  /// Retrieve the member template from which this template was
892  /// instantiated, or nullptr if this template was not instantiated from a
893  /// member template.
894  ///
895  /// A template is instantiated from a member template when the member
896  /// template itself is part of a class template (or member thereof). For
897  /// example, given
898  ///
899  /// \code
900  /// template<typename T>
901  /// struct X {
902  /// template<typename U> void f(T, U);
903  /// };
904  ///
905  /// void test(X<int> x) {
906  /// x.f(1, 'a');
907  /// };
908  /// \endcode
909  ///
910  /// \c X<int>::f is a FunctionTemplateDecl that describes the function
911  /// template
912  ///
913  /// \code
914  /// template<typename U> void X<int>::f(int, U);
915  /// \endcode
916  ///
917  /// which was itself created during the instantiation of \c X<int>. Calling
918  /// getInstantiatedFromMemberTemplate() on this FunctionTemplateDecl will
919  /// retrieve the FunctionTemplateDecl for the original template \c f within
920  /// the class template \c X<T>, i.e.,
921  ///
922  /// \code
923  /// template<typename T>
924  /// template<typename U>
925  /// void X<T>::f(T, U);
926  /// \endcode
928  return getCommonPtr()->InstantiatedFromMember.getPointer();
929  }
930 
932  assert(!getCommonPtr()->InstantiatedFromMember.getPointer());
933  getCommonPtr()->InstantiatedFromMember.setPointer(TD);
934  }
935 
937  using redecl_iterator = redeclarable_base::redecl_iterator;
938 
939  using redeclarable_base::redecls_begin;
940  using redeclarable_base::redecls_end;
941  using redeclarable_base::redecls;
942  using redeclarable_base::getPreviousDecl;
943  using redeclarable_base::getMostRecentDecl;
944  using redeclarable_base::isFirstDecl;
945 
946  // Implement isa/cast/dyncast/etc.
947  static bool classof(const Decl *D) { return classofKind(D->getKind()); }
948 
949  static bool classofKind(Kind K) {
950  return K >= firstRedeclarableTemplate && K <= lastRedeclarableTemplate;
951  }
952 };
953 
954 template <> struct RedeclarableTemplateDecl::
955 SpecEntryTraits<FunctionTemplateSpecializationInfo> {
957 
959  return I->Function;
960  }
961 
964  return I->TemplateArguments->asArray();
965  }
966 };
967 
968 /// Declaration of a template function.
970 protected:
971  friend class FunctionDecl;
972 
973  /// Data that is common to all of the declarations of a given
974  /// function template.
975  struct Common : CommonBase {
976  /// The function template specializations for this function
977  /// template, including explicit specializations and instantiations.
978  llvm::FoldingSetVector<FunctionTemplateSpecializationInfo> Specializations;
979 
980  /// The set of "injected" template arguments used within this
981  /// function template.
982  ///
983  /// This pointer refers to the template arguments (there are as
984  /// many template arguments as template parameaters) for the function
985  /// template, and is allocated lazily, since most function templates do not
986  /// require the use of this information.
987  TemplateArgument *InjectedArgs = nullptr;
988 
989  Common() = default;
990  };
991 
994  NamedDecl *Decl)
995  : RedeclarableTemplateDecl(FunctionTemplate, C, DC, L, Name, Params,
996  Decl) {}
997 
998  CommonBase *newCommon(ASTContext &C) const override;
999 
1001  return static_cast<Common *>(RedeclarableTemplateDecl::getCommonPtr());
1002  }
1003 
1004  /// Retrieve the set of function template specializations of this
1005  /// function template.
1006  llvm::FoldingSetVector<FunctionTemplateSpecializationInfo> &
1007  getSpecializations() const;
1008 
1009  /// Add a specialization of this function template.
1010  ///
1011  /// \param InsertPos Insert position in the FoldingSetVector, must have been
1012  /// retrieved by an earlier call to findSpecialization().
1013  void addSpecialization(FunctionTemplateSpecializationInfo* Info,
1014  void *InsertPos);
1015 
1016 public:
1017  friend class ASTDeclReader;
1018  friend class ASTDeclWriter;
1019 
1020  /// Load any lazily-loaded specializations from the external source.
1021  void LoadLazySpecializations() const;
1022 
1023  /// Get the underlying function declaration of the template.
1025  return static_cast<FunctionDecl *>(TemplatedDecl);
1026  }
1027 
1028  /// Returns whether this template declaration defines the primary
1029  /// pattern.
1031  return getTemplatedDecl()->isThisDeclarationADefinition();
1032  }
1033 
1034  /// Return the specialization with the provided arguments if it exists,
1035  /// otherwise return the insertion point.
1036  FunctionDecl *findSpecialization(ArrayRef<TemplateArgument> Args,
1037  void *&InsertPos);
1038 
1040  return cast<FunctionTemplateDecl>(
1042  }
1044  return cast<FunctionTemplateDecl>(
1046  }
1047 
1048  /// Retrieve the previous declaration of this function template, or
1049  /// nullptr if no such declaration exists.
1051  return cast_or_null<FunctionTemplateDecl>(
1052  static_cast<RedeclarableTemplateDecl *>(this)->getPreviousDecl());
1053  }
1055  return cast_or_null<FunctionTemplateDecl>(
1056  static_cast<const RedeclarableTemplateDecl *>(this)->getPreviousDecl());
1057  }
1058 
1060  return cast<FunctionTemplateDecl>(
1061  static_cast<RedeclarableTemplateDecl *>(this)
1062  ->getMostRecentDecl());
1063  }
1065  return const_cast<FunctionTemplateDecl*>(this)->getMostRecentDecl();
1066  }
1067 
1069  return cast_or_null<FunctionTemplateDecl>(
1071  }
1072 
1074  using spec_range = llvm::iterator_range<spec_iterator>;
1075 
1077  return spec_range(spec_begin(), spec_end());
1078  }
1079 
1081  return makeSpecIterator(getSpecializations(), false);
1082  }
1083 
1085  return makeSpecIterator(getSpecializations(), true);
1086  }
1087 
1088  /// Retrieve the "injected" template arguments that correspond to the
1089  /// template parameters of this function template.
1090  ///
1091  /// Although the C++ standard has no notion of the "injected" template
1092  /// arguments for a function template, the notion is convenient when
1093  /// we need to perform substitutions inside the definition of a function
1094  /// template.
1095  ArrayRef<TemplateArgument> getInjectedTemplateArgs();
1096 
1097  /// Merge \p Prev with our RedeclarableTemplateDecl::Common.
1098  void mergePrevDecl(FunctionTemplateDecl *Prev);
1099 
1100  /// Create a function template node.
1102  SourceLocation L,
1103  DeclarationName Name,
1104  TemplateParameterList *Params,
1105  NamedDecl *Decl);
1106 
1107  /// Create an empty function template node.
1108  static FunctionTemplateDecl *CreateDeserialized(ASTContext &C, unsigned ID);
1109 
1110  // Implement isa/cast/dyncast support
1111  static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1112  static bool classofKind(Kind K) { return K == FunctionTemplate; }
1113 };
1114 
1115 //===----------------------------------------------------------------------===//
1116 // Kinds of Template Parameters
1117 //===----------------------------------------------------------------------===//
1118 
1119 /// Defines the position of a template parameter within a template
1120 /// parameter list.
1121 ///
1122 /// Because template parameter can be listed
1123 /// sequentially for out-of-line template members, each template parameter is
1124 /// given a Depth - the nesting of template parameter scopes - and a Position -
1125 /// the occurrence within the parameter list.
1126 /// This class is inheritedly privately by different kinds of template
1127 /// parameters and is not part of the Decl hierarchy. Just a facility.
1129 protected:
1130  // FIXME: These probably don't need to be ints. int:5 for depth, int:8 for
1131  // position? Maybe?
1132  unsigned Depth;
1133  unsigned Position;
1134 
1135  TemplateParmPosition(unsigned D, unsigned P) : Depth(D), Position(P) {}
1136 
1137 public:
1138  TemplateParmPosition() = delete;
1139 
1140  /// Get the nesting depth of the template parameter.
1141  unsigned getDepth() const { return Depth; }
1142  void setDepth(unsigned D) { Depth = D; }
1143 
1144  /// Get the position of the template parameter within its parameter list.
1145  unsigned getPosition() const { return Position; }
1146  void setPosition(unsigned P) { Position = P; }
1147 
1148  /// Get the index of the template parameter within its parameter list.
1149  unsigned getIndex() const { return Position; }
1150 };
1151 
1152 /// Declaration of a template type parameter.
1153 ///
1154 /// For example, "T" in
1155 /// \code
1156 /// template<typename T> class vector;
1157 /// \endcode
1158 class TemplateTypeParmDecl : public TypeDecl {
1159  /// Sema creates these on the stack during auto type deduction.
1160  friend class Sema;
1161 
1162  /// Whether this template type parameter was declaration with
1163  /// the 'typename' keyword.
1164  ///
1165  /// If false, it was declared with the 'class' keyword.
1166  bool Typename : 1;
1167 
1168  /// The default template argument, if any.
1169  using DefArgStorage =
1171  DefArgStorage DefaultArgument;
1172 
1173  TemplateTypeParmDecl(DeclContext *DC, SourceLocation KeyLoc,
1175  bool Typename)
1176  : TypeDecl(TemplateTypeParm, DC, IdLoc, Id, KeyLoc), Typename(Typename) {}
1177 
1178 public:
1179  static TemplateTypeParmDecl *Create(const ASTContext &C, DeclContext *DC,
1180  SourceLocation KeyLoc,
1181  SourceLocation NameLoc,
1182  unsigned D, unsigned P,
1183  IdentifierInfo *Id, bool Typename,
1184  bool ParameterPack);
1185  static TemplateTypeParmDecl *CreateDeserialized(const ASTContext &C,
1186  unsigned ID);
1187 
1188  /// Whether this template type parameter was declared with
1189  /// the 'typename' keyword.
1190  ///
1191  /// If not, it was declared with the 'class' keyword.
1192  bool wasDeclaredWithTypename() const { return Typename; }
1193 
1194  const DefArgStorage &getDefaultArgStorage() const { return DefaultArgument; }
1195 
1196  /// Determine whether this template parameter has a default
1197  /// argument.
1198  bool hasDefaultArgument() const { return DefaultArgument.isSet(); }
1199 
1200  /// Retrieve the default argument, if any.
1202  return DefaultArgument.get()->getType();
1203  }
1204 
1205  /// Retrieves the default argument's source information, if any.
1207  return DefaultArgument.get();
1208  }
1209 
1210  /// Retrieves the location of the default argument declaration.
1211  SourceLocation getDefaultArgumentLoc() const;
1212 
1213  /// Determines whether the default argument was inherited
1214  /// from a previous declaration of this template.
1216  return DefaultArgument.isInherited();
1217  }
1218 
1219  /// Set the default argument for this template parameter.
1221  DefaultArgument.set(DefArg);
1222  }
1223 
1224  /// Set that this default argument was inherited from another
1225  /// parameter.
1227  TemplateTypeParmDecl *Prev) {
1228  DefaultArgument.setInherited(C, Prev);
1229  }
1230 
1231  /// Removes the default argument of this template parameter.
1233  DefaultArgument.clear();
1234  }
1235 
1236  /// Set whether this template type parameter was declared with
1237  /// the 'typename' or 'class' keyword.
1238  void setDeclaredWithTypename(bool withTypename) { Typename = withTypename; }
1239 
1240  /// Retrieve the depth of the template parameter.
1241  unsigned getDepth() const;
1242 
1243  /// Retrieve the index of the template parameter.
1244  unsigned getIndex() const;
1245 
1246  /// Returns whether this is a parameter pack.
1247  bool isParameterPack() const;
1248 
1249  SourceRange getSourceRange() const override LLVM_READONLY;
1250 
1251  // Implement isa/cast/dyncast/etc.
1252  static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1253  static bool classofKind(Kind K) { return K == TemplateTypeParm; }
1254 };
1255 
1256 /// NonTypeTemplateParmDecl - Declares a non-type template parameter,
1257 /// e.g., "Size" in
1258 /// @code
1259 /// template<int Size> class array { };
1260 /// @endcode
1261 class NonTypeTemplateParmDecl final
1262  : public DeclaratorDecl,
1263  protected TemplateParmPosition,
1264  private llvm::TrailingObjects<NonTypeTemplateParmDecl,
1265  std::pair<QualType, TypeSourceInfo *>> {
1266  friend class ASTDeclReader;
1267  friend TrailingObjects;
1268 
1269  /// The default template argument, if any, and whether or not
1270  /// it was inherited.
1272  DefArgStorage DefaultArgument;
1273 
1274  // FIXME: Collapse this into TemplateParamPosition; or, just move depth/index
1275  // down here to save memory.
1276 
1277  /// Whether this non-type template parameter is a parameter pack.
1278  bool ParameterPack;
1279 
1280  /// Whether this non-type template parameter is an "expanded"
1281  /// parameter pack, meaning that its type is a pack expansion and we
1282  /// already know the set of types that expansion expands to.
1283  bool ExpandedParameterPack = false;
1284 
1285  /// The number of types in an expanded parameter pack.
1286  unsigned NumExpandedTypes = 0;
1287 
1288  size_t numTrailingObjects(
1289  OverloadToken<std::pair<QualType, TypeSourceInfo *>>) const {
1290  return NumExpandedTypes;
1291  }
1292 
1293  NonTypeTemplateParmDecl(DeclContext *DC, SourceLocation StartLoc,
1294  SourceLocation IdLoc, unsigned D, unsigned P,
1296  bool ParameterPack, TypeSourceInfo *TInfo)
1297  : DeclaratorDecl(NonTypeTemplateParm, DC, IdLoc, Id, T, TInfo, StartLoc),
1298  TemplateParmPosition(D, P), ParameterPack(ParameterPack) {}
1299 
1300  NonTypeTemplateParmDecl(DeclContext *DC, SourceLocation StartLoc,
1301  SourceLocation IdLoc, unsigned D, unsigned P,
1303  TypeSourceInfo *TInfo,
1304  ArrayRef<QualType> ExpandedTypes,
1305  ArrayRef<TypeSourceInfo *> ExpandedTInfos);
1306 
1307 public:
1308  static NonTypeTemplateParmDecl *
1309  Create(const ASTContext &C, DeclContext *DC, SourceLocation StartLoc,
1310  SourceLocation IdLoc, unsigned D, unsigned P, IdentifierInfo *Id,
1311  QualType T, bool ParameterPack, TypeSourceInfo *TInfo);
1312 
1313  static NonTypeTemplateParmDecl *
1314  Create(const ASTContext &C, DeclContext *DC, SourceLocation StartLoc,
1315  SourceLocation IdLoc, unsigned D, unsigned P, IdentifierInfo *Id,
1316  QualType T, TypeSourceInfo *TInfo, ArrayRef<QualType> ExpandedTypes,
1317  ArrayRef<TypeSourceInfo *> ExpandedTInfos);
1318 
1319  static NonTypeTemplateParmDecl *CreateDeserialized(ASTContext &C,
1320  unsigned ID);
1321  static NonTypeTemplateParmDecl *CreateDeserialized(ASTContext &C,
1322  unsigned ID,
1323  unsigned NumExpandedTypes);
1324 
1330 
1331  SourceRange getSourceRange() const override LLVM_READONLY;
1332 
1333  const DefArgStorage &getDefaultArgStorage() const { return DefaultArgument; }
1334 
1335  /// Determine whether this template parameter has a default
1336  /// argument.
1337  bool hasDefaultArgument() const { return DefaultArgument.isSet(); }
1338 
1339  /// Retrieve the default argument, if any.
1340  Expr *getDefaultArgument() const { return DefaultArgument.get(); }
1341 
1342  /// Retrieve the location of the default argument, if any.
1343  SourceLocation getDefaultArgumentLoc() const;
1344 
1345  /// Determines whether the default argument was inherited
1346  /// from a previous declaration of this template.
1348  return DefaultArgument.isInherited();
1349  }
1350 
1351  /// Set the default argument for this template parameter, and
1352  /// whether that default argument was inherited from another
1353  /// declaration.
1354  void setDefaultArgument(Expr *DefArg) { DefaultArgument.set(DefArg); }
1356  NonTypeTemplateParmDecl *Parm) {
1357  DefaultArgument.setInherited(C, Parm);
1358  }
1359 
1360  /// Removes the default argument of this template parameter.
1361  void removeDefaultArgument() { DefaultArgument.clear(); }
1362 
1363  /// Whether this parameter is a non-type template parameter pack.
1364  ///
1365  /// If the parameter is a parameter pack, the type may be a
1366  /// \c PackExpansionType. In the following example, the \c Dims parameter
1367  /// is a parameter pack (whose type is 'unsigned').
1368  ///
1369  /// \code
1370  /// template<typename T, unsigned ...Dims> struct multi_array;
1371  /// \endcode
1372  bool isParameterPack() const { return ParameterPack; }
1373 
1374  /// Whether this parameter pack is a pack expansion.
1375  ///
1376  /// A non-type template parameter pack is a pack expansion if its type
1377  /// contains an unexpanded parameter pack. In this case, we will have
1378  /// built a PackExpansionType wrapping the type.
1379  bool isPackExpansion() const {
1380  return ParameterPack && getType()->getAs<PackExpansionType>();
1381  }
1382 
1383  /// Whether this parameter is a non-type template parameter pack
1384  /// that has a known list of different types at different positions.
1385  ///
1386  /// A parameter pack is an expanded parameter pack when the original
1387  /// parameter pack's type was itself a pack expansion, and that expansion
1388  /// has already been expanded. For example, given:
1389  ///
1390  /// \code
1391  /// template<typename ...Types>
1392  /// struct X {
1393  /// template<Types ...Values>
1394  /// struct Y { /* ... */ };
1395  /// };
1396  /// \endcode
1397  ///
1398  /// The parameter pack \c Values has a \c PackExpansionType as its type,
1399  /// which expands \c Types. When \c Types is supplied with template arguments
1400  /// by instantiating \c X, the instantiation of \c Values becomes an
1401  /// expanded parameter pack. For example, instantiating
1402  /// \c X<int, unsigned int> results in \c Values being an expanded parameter
1403  /// pack with expansion types \c int and \c unsigned int.
1404  ///
1405  /// The \c getExpansionType() and \c getExpansionTypeSourceInfo() functions
1406  /// return the expansion types.
1407  bool isExpandedParameterPack() const { return ExpandedParameterPack; }
1408 
1409  /// Retrieves the number of expansion types in an expanded parameter
1410  /// pack.
1411  unsigned getNumExpansionTypes() const {
1412  assert(ExpandedParameterPack && "Not an expansion parameter pack");
1413  return NumExpandedTypes;
1414  }
1415 
1416  /// Retrieve a particular expansion type within an expanded parameter
1417  /// pack.
1418  QualType getExpansionType(unsigned I) const {
1419  assert(I < NumExpandedTypes && "Out-of-range expansion type index");
1420  auto TypesAndInfos =
1421  getTrailingObjects<std::pair<QualType, TypeSourceInfo *>>();
1422  return TypesAndInfos[I].first;
1423  }
1424 
1425  /// Retrieve a particular expansion type source info within an
1426  /// expanded parameter pack.
1428  assert(I < NumExpandedTypes && "Out-of-range expansion type index");
1429  auto TypesAndInfos =
1430  getTrailingObjects<std::pair<QualType, TypeSourceInfo *>>();
1431  return TypesAndInfos[I].second;
1432  }
1433 
1434  // Implement isa/cast/dyncast/etc.
1435  static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1436  static bool classofKind(Kind K) { return K == NonTypeTemplateParm; }
1437 };
1438 
1439 /// TemplateTemplateParmDecl - Declares a template template parameter,
1440 /// e.g., "T" in
1441 /// @code
1442 /// template <template <typename> class T> class container { };
1443 /// @endcode
1444 /// A template template parameter is a TemplateDecl because it defines the
1445 /// name of a template and the template parameters allowable for substitution.
1447  : public TemplateDecl,
1448  protected TemplateParmPosition,
1449  private llvm::TrailingObjects<TemplateTemplateParmDecl,
1450  TemplateParameterList *> {
1451  /// The default template argument, if any.
1452  using DefArgStorage =
1454  DefArgStorage DefaultArgument;
1455 
1456  /// Whether this parameter is a parameter pack.
1457  bool ParameterPack;
1458 
1459  /// Whether this template template parameter is an "expanded"
1460  /// parameter pack, meaning that it is a pack expansion and we
1461  /// already know the set of template parameters that expansion expands to.
1462  bool ExpandedParameterPack = false;
1463 
1464  /// The number of parameters in an expanded parameter pack.
1465  unsigned NumExpandedParams = 0;
1466 
1468  unsigned D, unsigned P, bool ParameterPack,
1470  : TemplateDecl(TemplateTemplateParm, DC, L, Id, Params),
1471  TemplateParmPosition(D, P), ParameterPack(ParameterPack) {}
1472 
1474  unsigned D, unsigned P,
1477 
1478  void anchor() override;
1479 
1480 public:
1481  friend class ASTDeclReader;
1482  friend class ASTDeclWriter;
1484 
1485  static TemplateTemplateParmDecl *Create(const ASTContext &C, DeclContext *DC,
1486  SourceLocation L, unsigned D,
1487  unsigned P, bool ParameterPack,
1488  IdentifierInfo *Id,
1489  TemplateParameterList *Params);
1490  static TemplateTemplateParmDecl *Create(const ASTContext &C, DeclContext *DC,
1491  SourceLocation L, unsigned D,
1492  unsigned P,
1493  IdentifierInfo *Id,
1494  TemplateParameterList *Params,
1496 
1497  static TemplateTemplateParmDecl *CreateDeserialized(ASTContext &C,
1498  unsigned ID);
1499  static TemplateTemplateParmDecl *CreateDeserialized(ASTContext &C,
1500  unsigned ID,
1501  unsigned NumExpansions);
1502 
1508 
1509  /// Whether this template template parameter is a template
1510  /// parameter pack.
1511  ///
1512  /// \code
1513  /// template<template <class T> ...MetaFunctions> struct Apply;
1514  /// \endcode
1515  bool isParameterPack() const { return ParameterPack; }
1516 
1517  /// Whether this parameter pack is a pack expansion.
1518  ///
1519  /// A template template parameter pack is a pack expansion if its template
1520  /// parameter list contains an unexpanded parameter pack.
1521  bool isPackExpansion() const {
1522  return ParameterPack &&
1523  getTemplateParameters()->containsUnexpandedParameterPack();
1524  }
1525 
1526  /// Whether this parameter is a template template parameter pack that
1527  /// has a known list of different template parameter lists at different
1528  /// positions.
1529  ///
1530  /// A parameter pack is an expanded parameter pack when the original parameter
1531  /// pack's template parameter list was itself a pack expansion, and that
1532  /// expansion has already been expanded. For exampe, given:
1533  ///
1534  /// \code
1535  /// template<typename...Types> struct Outer {
1536  /// template<template<Types> class...Templates> struct Inner;
1537  /// };
1538  /// \endcode
1539  ///
1540  /// The parameter pack \c Templates is a pack expansion, which expands the
1541  /// pack \c Types. When \c Types is supplied with template arguments by
1542  /// instantiating \c Outer, the instantiation of \c Templates is an expanded
1543  /// parameter pack.
1544  bool isExpandedParameterPack() const { return ExpandedParameterPack; }
1545 
1546  /// Retrieves the number of expansion template parameters in
1547  /// an expanded parameter pack.
1549  assert(ExpandedParameterPack && "Not an expansion parameter pack");
1550  return NumExpandedParams;
1551  }
1552 
1553  /// Retrieve a particular expansion type within an expanded parameter
1554  /// pack.
1556  assert(I < NumExpandedParams && "Out-of-range expansion type index");
1557  return getTrailingObjects<TemplateParameterList *>()[I];
1558  }
1559 
1560  const DefArgStorage &getDefaultArgStorage() const { return DefaultArgument; }
1561 
1562  /// Determine whether this template parameter has a default
1563  /// argument.
1564  bool hasDefaultArgument() const { return DefaultArgument.isSet(); }
1565 
1566  /// Retrieve the default argument, if any.
1568  static const TemplateArgumentLoc None;
1569  return DefaultArgument.isSet() ? *DefaultArgument.get() : None;
1570  }
1571 
1572  /// Retrieve the location of the default argument, if any.
1573  SourceLocation getDefaultArgumentLoc() const;
1574 
1575  /// Determines whether the default argument was inherited
1576  /// from a previous declaration of this template.
1578  return DefaultArgument.isInherited();
1579  }
1580 
1581  /// Set the default argument for this template parameter, and
1582  /// whether that default argument was inherited from another
1583  /// declaration.
1584  void setDefaultArgument(const ASTContext &C,
1585  const TemplateArgumentLoc &DefArg);
1587  TemplateTemplateParmDecl *Prev) {
1588  DefaultArgument.setInherited(C, Prev);
1589  }
1590 
1591  /// Removes the default argument of this template parameter.
1592  void removeDefaultArgument() { DefaultArgument.clear(); }
1593 
1594  SourceRange getSourceRange() const override LLVM_READONLY {
1595  SourceLocation End = getLocation();
1596  if (hasDefaultArgument() && !defaultArgumentWasInherited())
1597  End = getDefaultArgument().getSourceRange().getEnd();
1598  return SourceRange(getTemplateParameters()->getTemplateLoc(), End);
1599  }
1600 
1601  // Implement isa/cast/dyncast/etc.
1602  static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1603  static bool classofKind(Kind K) { return K == TemplateTemplateParm; }
1604 };
1605 
1606 /// Represents the builtin template declaration which is used to
1607 /// implement __make_integer_seq and other builtin templates. It serves
1608 /// no real purpose beyond existing as a place to hold template parameters.
1610  BuiltinTemplateKind BTK;
1611 
1614 
1615  void anchor() override;
1616 
1617 public:
1618  // Implement isa/cast/dyncast support
1619  static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1620  static bool classofKind(Kind K) { return K == BuiltinTemplate; }
1621 
1623  DeclarationName Name,
1624  BuiltinTemplateKind BTK) {
1625  return new (C, DC) BuiltinTemplateDecl(C, DC, Name, BTK);
1626  }
1627 
1628  SourceRange getSourceRange() const override LLVM_READONLY {
1629  return {};
1630  }
1631 
1633 };
1634 
1635 /// Represents a class template specialization, which refers to
1636 /// a class template with a given set of template arguments.
1637 ///
1638 /// Class template specializations represent both explicit
1639 /// specialization of class templates, as in the example below, and
1640 /// implicit instantiations of class templates.
1641 ///
1642 /// \code
1643 /// template<typename T> class array;
1644 ///
1645 /// template<>
1646 /// class array<bool> { }; // class template specialization array<bool>
1647 /// \endcode
1649  : public CXXRecordDecl, public llvm::FoldingSetNode {
1650  /// Structure that stores information about a class template
1651  /// specialization that was instantiated from a class template partial
1652  /// specialization.
1653  struct SpecializedPartialSpecialization {
1654  /// The class template partial specialization from which this
1655  /// class template specialization was instantiated.
1656  ClassTemplatePartialSpecializationDecl *PartialSpecialization;
1657 
1658  /// The template argument list deduced for the class template
1659  /// partial specialization itself.
1660  const TemplateArgumentList *TemplateArgs;
1661  };
1662 
1663  /// The template that this specialization specializes
1664  llvm::PointerUnion<ClassTemplateDecl *, SpecializedPartialSpecialization *>
1665  SpecializedTemplate;
1666 
1667  /// Further info for explicit template specialization/instantiation.
1668  struct ExplicitSpecializationInfo {
1669  /// The type-as-written.
1670  TypeSourceInfo *TypeAsWritten = nullptr;
1671 
1672  /// The location of the extern keyword.
1673  SourceLocation ExternLoc;
1674 
1675  /// The location of the template keyword.
1676  SourceLocation TemplateKeywordLoc;
1677 
1678  ExplicitSpecializationInfo() = default;
1679  };
1680 
1681  /// Further info for explicit template specialization/instantiation.
1682  /// Does not apply to implicit specializations.
1683  ExplicitSpecializationInfo *ExplicitInfo = nullptr;
1684 
1685  /// The template arguments used to describe this specialization.
1686  const TemplateArgumentList *TemplateArgs;
1687 
1688  /// The point where this template was instantiated (if any)
1689  SourceLocation PointOfInstantiation;
1690 
1691  /// The kind of specialization this declaration refers to.
1692  /// Really a value of type TemplateSpecializationKind.
1693  unsigned SpecializationKind : 3;
1694 
1695 protected:
1697  DeclContext *DC, SourceLocation StartLoc,
1698  SourceLocation IdLoc,
1699  ClassTemplateDecl *SpecializedTemplate,
1702 
1704 
1705 public:
1706  friend class ASTDeclReader;
1707  friend class ASTDeclWriter;
1708 
1710  Create(ASTContext &Context, TagKind TK, DeclContext *DC,
1711  SourceLocation StartLoc, SourceLocation IdLoc,
1712  ClassTemplateDecl *SpecializedTemplate,
1716  CreateDeserialized(ASTContext &C, unsigned ID);
1717 
1718  void getNameForDiagnostic(raw_ostream &OS, const PrintingPolicy &Policy,
1719  bool Qualified) const override;
1720 
1721  // FIXME: This is broken. CXXRecordDecl::getMostRecentDecl() returns a
1722  // different "most recent" declaration from this function for the same
1723  // declaration, because we don't override getMostRecentDeclImpl(). But
1724  // it's not clear that we should override that, because the most recent
1725  // declaration as a CXXRecordDecl sometimes is the injected-class-name.
1727  return cast<ClassTemplateSpecializationDecl>(
1728  getMostRecentNonInjectedDecl());
1729  }
1730 
1731  /// Retrieve the template that this specialization specializes.
1732  ClassTemplateDecl *getSpecializedTemplate() const;
1733 
1734  /// Retrieve the template arguments of the class template
1735  /// specialization.
1737  return *TemplateArgs;
1738  }
1739 
1740  /// Determine the kind of specialization that this
1741  /// declaration represents.
1743  return static_cast<TemplateSpecializationKind>(SpecializationKind);
1744  }
1745 
1747  return getSpecializationKind() == TSK_ExplicitSpecialization;
1748  }
1749 
1750  /// True if this declaration is an explicit specialization,
1751  /// explicit instantiation declaration, or explicit instantiation
1752  /// definition.
1756  }
1757 
1759  SpecializationKind = TSK;
1760  }
1761 
1762  /// Get the point of instantiation (if any), or null if none.
1764  return PointOfInstantiation;
1765  }
1766 
1768  assert(Loc.isValid() && "point of instantiation must be valid!");
1769  PointOfInstantiation = Loc;
1770  }
1771 
1772  /// If this class template specialization is an instantiation of
1773  /// a template (rather than an explicit specialization), return the
1774  /// class template or class template partial specialization from which it
1775  /// was instantiated.
1776  llvm::PointerUnion<ClassTemplateDecl *,
1779  if (!isTemplateInstantiation(getSpecializationKind()))
1780  return llvm::PointerUnion<ClassTemplateDecl *,
1782 
1783  return getSpecializedTemplateOrPartial();
1784  }
1785 
1786  /// Retrieve the class template or class template partial
1787  /// specialization which was specialized by this.
1788  llvm::PointerUnion<ClassTemplateDecl *,
1791  if (const auto *PartialSpec =
1792  SpecializedTemplate.dyn_cast<SpecializedPartialSpecialization *>())
1793  return PartialSpec->PartialSpecialization;
1794 
1795  return SpecializedTemplate.get<ClassTemplateDecl*>();
1796  }
1797 
1798  /// Retrieve the set of template arguments that should be used
1799  /// to instantiate members of the class template or class template partial
1800  /// specialization from which this class template specialization was
1801  /// instantiated.
1802  ///
1803  /// \returns For a class template specialization instantiated from the primary
1804  /// template, this function will return the same template arguments as
1805  /// getTemplateArgs(). For a class template specialization instantiated from
1806  /// a class template partial specialization, this function will return the
1807  /// deduced template arguments for the class template partial specialization
1808  /// itself.
1810  if (const auto *PartialSpec =
1811  SpecializedTemplate.dyn_cast<SpecializedPartialSpecialization *>())
1812  return *PartialSpec->TemplateArgs;
1813 
1814  return getTemplateArgs();
1815  }
1816 
1817  /// Note that this class template specialization is actually an
1818  /// instantiation of the given class template partial specialization whose
1819  /// template arguments have been deduced.
1821  const TemplateArgumentList *TemplateArgs) {
1822  assert(!SpecializedTemplate.is<SpecializedPartialSpecialization*>() &&
1823  "Already set to a class template partial specialization!");
1824  auto *PS = new (getASTContext()) SpecializedPartialSpecialization();
1825  PS->PartialSpecialization = PartialSpec;
1826  PS->TemplateArgs = TemplateArgs;
1827  SpecializedTemplate = PS;
1828  }
1829 
1830  /// Note that this class template specialization is an instantiation
1831  /// of the given class template.
1832  void setInstantiationOf(ClassTemplateDecl *TemplDecl) {
1833  assert(!SpecializedTemplate.is<SpecializedPartialSpecialization*>() &&
1834  "Previously set to a class template partial specialization!");
1835  SpecializedTemplate = TemplDecl;
1836  }
1837 
1838  /// Sets the type of this specialization as it was written by
1839  /// the user. This will be a class template specialization type.
1841  if (!ExplicitInfo)
1842  ExplicitInfo = new (getASTContext()) ExplicitSpecializationInfo;
1843  ExplicitInfo->TypeAsWritten = T;
1844  }
1845 
1846  /// Gets the type of this specialization as it was written by
1847  /// the user, if it was so written.
1849  return ExplicitInfo ? ExplicitInfo->TypeAsWritten : nullptr;
1850  }
1851 
1852  /// Gets the location of the extern keyword, if present.
1854  return ExplicitInfo ? ExplicitInfo->ExternLoc : SourceLocation();
1855  }
1856 
1857  /// Sets the location of the extern keyword.
1859  if (!ExplicitInfo)
1860  ExplicitInfo = new (getASTContext()) ExplicitSpecializationInfo;
1861  ExplicitInfo->ExternLoc = Loc;
1862  }
1863 
1864  /// Sets the location of the template keyword.
1866  if (!ExplicitInfo)
1867  ExplicitInfo = new (getASTContext()) ExplicitSpecializationInfo;
1868  ExplicitInfo->TemplateKeywordLoc = Loc;
1869  }
1870 
1871  /// Gets the location of the template keyword, if present.
1873  return ExplicitInfo ? ExplicitInfo->TemplateKeywordLoc : SourceLocation();
1874  }
1875 
1876  SourceRange getSourceRange() const override LLVM_READONLY;
1877 
1878  void Profile(llvm::FoldingSetNodeID &ID) const {
1879  Profile(ID, TemplateArgs->asArray(), getASTContext());
1880  }
1881 
1882  static void
1883  Profile(llvm::FoldingSetNodeID &ID, ArrayRef<TemplateArgument> TemplateArgs,
1884  ASTContext &Context) {
1885  ID.AddInteger(TemplateArgs.size());
1886  for (const TemplateArgument &TemplateArg : TemplateArgs)
1887  TemplateArg.Profile(ID, Context);
1888  }
1889 
1890  static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1891 
1892  static bool classofKind(Kind K) {
1893  return K >= firstClassTemplateSpecialization &&
1894  K <= lastClassTemplateSpecialization;
1895  }
1896 };
1897 
1900  /// The list of template parameters
1901  TemplateParameterList* TemplateParams = nullptr;
1902 
1903  /// The source info for the template arguments as written.
1904  /// FIXME: redundant with TypeAsWritten?
1905  const ASTTemplateArgumentListInfo *ArgsAsWritten = nullptr;
1906 
1907  /// The class template partial specialization from which this
1908  /// class template partial specialization was instantiated.
1909  ///
1910  /// The boolean value will be true to indicate that this class template
1911  /// partial specialization was specialized at this level.
1912  llvm::PointerIntPair<ClassTemplatePartialSpecializationDecl *, 1, bool>
1913  InstantiatedFromMember;
1914 
1916  DeclContext *DC,
1917  SourceLocation StartLoc,
1918  SourceLocation IdLoc,
1919  TemplateParameterList *Params,
1920  ClassTemplateDecl *SpecializedTemplate,
1922  const ASTTemplateArgumentListInfo *ArgsAsWritten,
1924 
1926  : ClassTemplateSpecializationDecl(C, ClassTemplatePartialSpecialization),
1927  InstantiatedFromMember(nullptr, false) {}
1928 
1929  void anchor() override;
1930 
1931 public:
1932  friend class ASTDeclReader;
1933  friend class ASTDeclWriter;
1934 
1936  Create(ASTContext &Context, TagKind TK, DeclContext *DC,
1937  SourceLocation StartLoc, SourceLocation IdLoc,
1938  TemplateParameterList *Params,
1939  ClassTemplateDecl *SpecializedTemplate,
1941  const TemplateArgumentListInfo &ArgInfos,
1942  QualType CanonInjectedType,
1944 
1946  CreateDeserialized(ASTContext &C, unsigned ID);
1947 
1949  return cast<ClassTemplatePartialSpecializationDecl>(
1950  static_cast<ClassTemplateSpecializationDecl *>(
1951  this)->getMostRecentDecl());
1952  }
1953 
1954  /// Get the list of template parameters
1956  return TemplateParams;
1957  }
1958 
1959  /// Get the template arguments as written.
1961  return ArgsAsWritten;
1962  }
1963 
1964  /// Retrieve the member class template partial specialization from
1965  /// which this particular class template partial specialization was
1966  /// instantiated.
1967  ///
1968  /// \code
1969  /// template<typename T>
1970  /// struct Outer {
1971  /// template<typename U> struct Inner;
1972  /// template<typename U> struct Inner<U*> { }; // #1
1973  /// };
1974  ///
1975  /// Outer<float>::Inner<int*> ii;
1976  /// \endcode
1977  ///
1978  /// In this example, the instantiation of \c Outer<float>::Inner<int*> will
1979  /// end up instantiating the partial specialization
1980  /// \c Outer<float>::Inner<U*>, which itself was instantiated from the class
1981  /// template partial specialization \c Outer<T>::Inner<U*>. Given
1982  /// \c Outer<float>::Inner<U*>, this function would return
1983  /// \c Outer<T>::Inner<U*>.
1985  const auto *First =
1986  cast<ClassTemplatePartialSpecializationDecl>(getFirstDecl());
1987  return First->InstantiatedFromMember.getPointer();
1988  }
1991  return getInstantiatedFromMember();
1992  }
1993 
1996  auto *First = cast<ClassTemplatePartialSpecializationDecl>(getFirstDecl());
1997  First->InstantiatedFromMember.setPointer(PartialSpec);
1998  }
1999 
2000  /// Determines whether this class template partial specialization
2001  /// template was a specialization of a member partial specialization.
2002  ///
2003  /// In the following example, the member template partial specialization
2004  /// \c X<int>::Inner<T*> is a member specialization.
2005  ///
2006  /// \code
2007  /// template<typename T>
2008  /// struct X {
2009  /// template<typename U> struct Inner;
2010  /// template<typename U> struct Inner<U*>;
2011  /// };
2012  ///
2013  /// template<> template<typename T>
2014  /// struct X<int>::Inner<T*> { /* ... */ };
2015  /// \endcode
2017  const auto *First =
2018  cast<ClassTemplatePartialSpecializationDecl>(getFirstDecl());
2019  return First->InstantiatedFromMember.getInt();
2020  }
2021 
2022  /// Note that this member template is a specialization.
2024  auto *First = cast<ClassTemplatePartialSpecializationDecl>(getFirstDecl());
2025  assert(First->InstantiatedFromMember.getPointer() &&
2026  "Only member templates can be member template specializations");
2027  return First->InstantiatedFromMember.setInt(true);
2028  }
2029 
2030  /// Retrieves the injected specialization type for this partial
2031  /// specialization. This is not the same as the type-decl-type for
2032  /// this partial specialization, which is an InjectedClassNameType.
2034  assert(getTypeForDecl() && "partial specialization has no type set!");
2035  return cast<InjectedClassNameType>(getTypeForDecl())
2036  ->getInjectedSpecializationType();
2037  }
2038 
2039  // FIXME: Add Profile support!
2040 
2041  static bool classof(const Decl *D) { return classofKind(D->getKind()); }
2042 
2043  static bool classofKind(Kind K) {
2044  return K == ClassTemplatePartialSpecialization;
2045  }
2046 };
2047 
2048 /// Declaration of a class template.
2050 protected:
2051  /// Data that is common to all of the declarations of a given
2052  /// class template.
2053  struct Common : CommonBase {
2054  /// The class template specializations for this class
2055  /// template, including explicit specializations and instantiations.
2056  llvm::FoldingSetVector<ClassTemplateSpecializationDecl> Specializations;
2057 
2058  /// The class template partial specializations for this class
2059  /// template.
2060  llvm::FoldingSetVector<ClassTemplatePartialSpecializationDecl>
2062 
2063  /// The injected-class-name type for this class template.
2065 
2066  Common() = default;
2067  };
2068 
2069  /// Retrieve the set of specializations of this class template.
2070  llvm::FoldingSetVector<ClassTemplateSpecializationDecl> &
2071  getSpecializations() const;
2072 
2073  /// Retrieve the set of partial specializations of this class
2074  /// template.
2075  llvm::FoldingSetVector<ClassTemplatePartialSpecializationDecl> &
2076  getPartialSpecializations();
2077 
2081  : RedeclarableTemplateDecl(CTDI, ClassTemplate, C, DC, L, Name, Params,
2082  Decl) {}
2083 
2085  DeclarationName Name, TemplateParameterList *Params,
2086  NamedDecl *Decl)
2087  : ClassTemplateDecl(nullptr, C, DC, L, Name, Params, Decl) {}
2088 
2089  CommonBase *newCommon(ASTContext &C) const override;
2090 
2092  return static_cast<Common *>(RedeclarableTemplateDecl::getCommonPtr());
2093  }
2094 
2095 public:
2096  friend class ASTDeclReader;
2097  friend class ASTDeclWriter;
2098 
2099  /// Load any lazily-loaded specializations from the external source.
2100  void LoadLazySpecializations() const;
2101 
2102  /// Get the underlying class declarations of the template.
2104  return static_cast<CXXRecordDecl *>(TemplatedDecl);
2105  }
2106 
2107  /// Returns whether this template declaration defines the primary
2108  /// class pattern.
2110  return getTemplatedDecl()->isThisDeclarationADefinition();
2111  }
2112 
2113  // FIXME: remove default argument for AssociatedConstraints
2114  /// Create a class template node.
2116  SourceLocation L,
2117  DeclarationName Name,
2118  TemplateParameterList *Params,
2119  NamedDecl *Decl,
2120  Expr *AssociatedConstraints = nullptr);
2121 
2122  /// Create an empty class template node.
2123  static ClassTemplateDecl *CreateDeserialized(ASTContext &C, unsigned ID);
2124 
2125  /// Return the specialization with the provided arguments if it exists,
2126  /// otherwise return the insertion point.
2128  findSpecialization(ArrayRef<TemplateArgument> Args, void *&InsertPos);
2129 
2130  /// Insert the specified specialization knowing that it is not already
2131  /// in. InsertPos must be obtained from findSpecialization.
2132  void AddSpecialization(ClassTemplateSpecializationDecl *D, void *InsertPos);
2133 
2135  return cast<ClassTemplateDecl>(
2137  }
2139  return cast<ClassTemplateDecl>(
2141  }
2142 
2143  /// Retrieve the previous declaration of this class template, or
2144  /// nullptr if no such declaration exists.
2146  return cast_or_null<ClassTemplateDecl>(
2147  static_cast<RedeclarableTemplateDecl *>(this)->getPreviousDecl());
2148  }
2150  return cast_or_null<ClassTemplateDecl>(
2151  static_cast<const RedeclarableTemplateDecl *>(
2152  this)->getPreviousDecl());
2153  }
2154 
2156  return cast<ClassTemplateDecl>(
2157  static_cast<RedeclarableTemplateDecl *>(this)->getMostRecentDecl());
2158  }
2160  return const_cast<ClassTemplateDecl*>(this)->getMostRecentDecl();
2161  }
2162 
2164  return cast_or_null<ClassTemplateDecl>(
2166  }
2167 
2168  /// Return the partial specialization with the provided arguments if it
2169  /// exists, otherwise return the insertion point.
2171  findPartialSpecialization(ArrayRef<TemplateArgument> Args, void *&InsertPos);
2172 
2173  /// Insert the specified partial specialization knowing that it is not
2174  /// already in. InsertPos must be obtained from findPartialSpecialization.
2175  void AddPartialSpecialization(ClassTemplatePartialSpecializationDecl *D,
2176  void *InsertPos);
2177 
2178  /// Retrieve the partial specializations as an ordered list.
2179  void getPartialSpecializations(
2181 
2182  /// Find a class template partial specialization with the given
2183  /// type T.
2184  ///
2185  /// \param T a dependent type that names a specialization of this class
2186  /// template.
2187  ///
2188  /// \returns the class template partial specialization that exactly matches
2189  /// the type \p T, or nullptr if no such partial specialization exists.
2190  ClassTemplatePartialSpecializationDecl *findPartialSpecialization(QualType T);
2191 
2192  /// Find a class template partial specialization which was instantiated
2193  /// from the given member partial specialization.
2194  ///
2195  /// \param D a member class template partial specialization.
2196  ///
2197  /// \returns the class template partial specialization which was instantiated
2198  /// from the given member partial specialization, or nullptr if no such
2199  /// partial specialization exists.
2201  findPartialSpecInstantiatedFromMember(
2203 
2204  /// Retrieve the template specialization type of the
2205  /// injected-class-name for this class template.
2206  ///
2207  /// The injected-class-name for a class template \c X is \c
2208  /// X<template-args>, where \c template-args is formed from the
2209  /// template arguments that correspond to the template parameters of
2210  /// \c X. For example:
2211  ///
2212  /// \code
2213  /// template<typename T, int N>
2214  /// struct array {
2215  /// typedef array this_type; // "array" is equivalent to "array<T, N>"
2216  /// };
2217  /// \endcode
2218  QualType getInjectedClassNameSpecialization();
2219 
2221  using spec_range = llvm::iterator_range<spec_iterator>;
2222 
2224  return spec_range(spec_begin(), spec_end());
2225  }
2226 
2228  return makeSpecIterator(getSpecializations(), false);
2229  }
2230 
2232  return makeSpecIterator(getSpecializations(), true);
2233  }
2234 
2235  // Implement isa/cast/dyncast support
2236  static bool classof(const Decl *D) { return classofKind(D->getKind()); }
2237  static bool classofKind(Kind K) { return K == ClassTemplate; }
2238 };
2239 
2240 /// Declaration of a friend template.
2241 ///
2242 /// For example:
2243 /// \code
2244 /// template <typename T> class A {
2245 /// friend class MyVector<T>; // not a friend template
2246 /// template <typename U> friend class B; // not a friend template
2247 /// template <typename U> friend class Foo<T>::Nested; // friend template
2248 /// };
2249 /// \endcode
2250 ///
2251 /// \note This class is not currently in use. All of the above
2252 /// will yield a FriendDecl, not a FriendTemplateDecl.
2253 class FriendTemplateDecl : public Decl {
2254  virtual void anchor();
2255 
2256 public:
2257  using FriendUnion = llvm::PointerUnion<NamedDecl *,TypeSourceInfo *>;
2258 
2259 private:
2260  // The number of template parameters; always non-zero.
2261  unsigned NumParams = 0;
2262 
2263  // The parameter list.
2264  TemplateParameterList **Params = nullptr;
2265 
2266  // The declaration that's a friend of this class.
2267  FriendUnion Friend;
2268 
2269  // Location of the 'friend' specifier.
2270  SourceLocation FriendLoc;
2271 
2274  FriendUnion Friend, SourceLocation FriendLoc)
2275  : Decl(Decl::FriendTemplate, DC, Loc), NumParams(Params.size()),
2276  Params(Params.data()), Friend(Friend), FriendLoc(FriendLoc) {}
2277 
2278  FriendTemplateDecl(EmptyShell Empty) : Decl(Decl::FriendTemplate, Empty) {}
2279 
2280 public:
2281  friend class ASTDeclReader;
2282 
2283  static FriendTemplateDecl *
2284  Create(ASTContext &Context, DeclContext *DC, SourceLocation Loc,
2286  SourceLocation FriendLoc);
2287 
2288  static FriendTemplateDecl *CreateDeserialized(ASTContext &C, unsigned ID);
2289 
2290  /// If this friend declaration names a templated type (or
2291  /// a dependent member type of a templated type), return that
2292  /// type; otherwise return null.
2294  return Friend.dyn_cast<TypeSourceInfo*>();
2295  }
2296 
2297  /// If this friend declaration names a templated function (or
2298  /// a member function of a templated type), return that type;
2299  /// otherwise return null.
2301  return Friend.dyn_cast<NamedDecl*>();
2302  }
2303 
2304  /// Retrieves the location of the 'friend' keyword.
2306  return FriendLoc;
2307  }
2308 
2310  assert(i <= NumParams);
2311  return Params[i];
2312  }
2313 
2314  unsigned getNumTemplateParameters() const {
2315  return NumParams;
2316  }
2317 
2318  // Implement isa/cast/dyncast/etc.
2319  static bool classof(const Decl *D) { return classofKind(D->getKind()); }
2320  static bool classofKind(Kind K) { return K == Decl::FriendTemplate; }
2321 };
2322 
2323 /// Declaration of an alias template.
2324 ///
2325 /// For example:
2326 /// \code
2327 /// template <typename T> using V = std::map<T*, int, MyCompare<T>>;
2328 /// \endcode
2330 protected:
2332 
2334  DeclarationName Name, TemplateParameterList *Params,
2335  NamedDecl *Decl)
2336  : RedeclarableTemplateDecl(TypeAliasTemplate, C, DC, L, Name, Params,
2337  Decl) {}
2338 
2339  CommonBase *newCommon(ASTContext &C) const override;
2340 
2342  return static_cast<Common *>(RedeclarableTemplateDecl::getCommonPtr());
2343  }
2344 
2345 public:
2346  friend class ASTDeclReader;
2347  friend class ASTDeclWriter;
2348 
2349  /// Get the underlying function declaration of the template.
2351  return static_cast<TypeAliasDecl *>(TemplatedDecl);
2352  }
2353 
2354 
2356  return cast<TypeAliasTemplateDecl>(
2358  }
2360  return cast<TypeAliasTemplateDecl>(
2362  }
2363 
2364  /// Retrieve the previous declaration of this function template, or
2365  /// nullptr if no such declaration exists.
2367  return cast_or_null<TypeAliasTemplateDecl>(
2368  static_cast<RedeclarableTemplateDecl *>(this)->getPreviousDecl());
2369  }
2371  return cast_or_null<TypeAliasTemplateDecl>(
2372  static_cast<const RedeclarableTemplateDecl *>(
2373  this)->getPreviousDecl());
2374  }
2375 
2377  return cast_or_null<TypeAliasTemplateDecl>(
2379  }
2380 
2381  /// Create a function template node.
2383  SourceLocation L,
2384  DeclarationName Name,
2385  TemplateParameterList *Params,
2386  NamedDecl *Decl);
2387 
2388  /// Create an empty alias template node.
2389  static TypeAliasTemplateDecl *CreateDeserialized(ASTContext &C, unsigned ID);
2390 
2391  // Implement isa/cast/dyncast support
2392  static bool classof(const Decl *D) { return classofKind(D->getKind()); }
2393  static bool classofKind(Kind K) { return K == TypeAliasTemplate; }
2394 };
2395 
2396 /// Declaration of a function specialization at template class scope.
2397 ///
2398 /// This is a non-standard extension needed to support MSVC.
2399 ///
2400 /// For example:
2401 /// \code
2402 /// template <class T>
2403 /// class A {
2404 /// template <class U> void foo(U a) { }
2405 /// template<> void foo(int a) { }
2406 /// }
2407 /// \endcode
2408 ///
2409 /// "template<> foo(int a)" will be saved in Specialization as a normal
2410 /// CXXMethodDecl. Then during an instantiation of class A, it will be
2411 /// transformed into an actual function specialization.
2413  CXXMethodDecl *Specialization;
2414  bool HasExplicitTemplateArgs;
2415  TemplateArgumentListInfo TemplateArgs;
2416 
2418  CXXMethodDecl *FD, bool Args,
2419  TemplateArgumentListInfo TemplArgs)
2420  : Decl(Decl::ClassScopeFunctionSpecialization, DC, Loc),
2421  Specialization(FD), HasExplicitTemplateArgs(Args),
2422  TemplateArgs(std::move(TemplArgs)) {}
2423 
2425  : Decl(Decl::ClassScopeFunctionSpecialization, Empty) {}
2426 
2427  virtual void anchor();
2428 
2429 public:
2430  friend class ASTDeclReader;
2431  friend class ASTDeclWriter;
2432 
2433  CXXMethodDecl *getSpecialization() const { return Specialization; }
2434  bool hasExplicitTemplateArgs() const { return HasExplicitTemplateArgs; }
2435  const TemplateArgumentListInfo& templateArgs() const { return TemplateArgs; }
2436 
2438  DeclContext *DC,
2439  SourceLocation Loc,
2440  CXXMethodDecl *FD,
2441  bool HasExplicitTemplateArgs,
2442  TemplateArgumentListInfo TemplateArgs) {
2443  return new (C, DC) ClassScopeFunctionSpecializationDecl(
2444  DC, Loc, FD, HasExplicitTemplateArgs, std::move(TemplateArgs));
2445  }
2446 
2448  CreateDeserialized(ASTContext &Context, unsigned ID);
2449 
2450  // Implement isa/cast/dyncast/etc.
2451  static bool classof(const Decl *D) { return classofKind(D->getKind()); }
2452 
2453  static bool classofKind(Kind K) {
2454  return K == Decl::ClassScopeFunctionSpecialization;
2455  }
2456 };
2457 
2458 /// Implementation of inline functions that require the template declarations
2459 inline AnyFunctionDecl::AnyFunctionDecl(FunctionTemplateDecl *FTD)
2460  : Function(FTD) {}
2461 
2462 /// Represents a variable template specialization, which refers to
2463 /// a variable template with a given set of template arguments.
2464 ///
2465 /// Variable template specializations represent both explicit
2466 /// specializations of variable templates, as in the example below, and
2467 /// implicit instantiations of variable templates.
2468 ///
2469 /// \code
2470 /// template<typename T> constexpr T pi = T(3.1415926535897932385);
2471 ///
2472 /// template<>
2473 /// constexpr float pi<float>; // variable template specialization pi<float>
2474 /// \endcode
2476  public llvm::FoldingSetNode {
2477 
2478  /// Structure that stores information about a variable template
2479  /// specialization that was instantiated from a variable template partial
2480  /// specialization.
2481  struct SpecializedPartialSpecialization {
2482  /// The variable template partial specialization from which this
2483  /// variable template specialization was instantiated.
2484  VarTemplatePartialSpecializationDecl *PartialSpecialization;
2485 
2486  /// The template argument list deduced for the variable template
2487  /// partial specialization itself.
2488  const TemplateArgumentList *TemplateArgs;
2489  };
2490 
2491  /// The template that this specialization specializes.
2492  llvm::PointerUnion<VarTemplateDecl *, SpecializedPartialSpecialization *>
2493  SpecializedTemplate;
2494 
2495  /// Further info for explicit template specialization/instantiation.
2496  struct ExplicitSpecializationInfo {
2497  /// The type-as-written.
2498  TypeSourceInfo *TypeAsWritten = nullptr;
2499 
2500  /// The location of the extern keyword.
2501  SourceLocation ExternLoc;
2502 
2503  /// The location of the template keyword.
2504  SourceLocation TemplateKeywordLoc;
2505 
2506  ExplicitSpecializationInfo() = default;
2507  };
2508 
2509  /// Further info for explicit template specialization/instantiation.
2510  /// Does not apply to implicit specializations.
2511  ExplicitSpecializationInfo *ExplicitInfo = nullptr;
2512 
2513  /// The template arguments used to describe this specialization.
2514  const TemplateArgumentList *TemplateArgs;
2515  TemplateArgumentListInfo TemplateArgsInfo;
2516 
2517  /// The point where this template was instantiated (if any).
2518  SourceLocation PointOfInstantiation;
2519 
2520  /// The kind of specialization this declaration refers to.
2521  /// Really a value of type TemplateSpecializationKind.
2522  unsigned SpecializationKind : 3;
2523 
2524  /// Whether this declaration is a complete definition of the
2525  /// variable template specialization. We can't otherwise tell apart
2526  /// an instantiated declaration from an instantiated definition with
2527  /// no initializer.
2528  unsigned IsCompleteDefinition : 1;
2529 
2530 protected:
2532  SourceLocation StartLoc, SourceLocation IdLoc,
2533  VarTemplateDecl *SpecializedTemplate,
2534  QualType T, TypeSourceInfo *TInfo,
2535  StorageClass S,
2537 
2538  explicit VarTemplateSpecializationDecl(Kind DK, ASTContext &Context);
2539 
2540 public:
2541  friend class ASTDeclReader;
2542  friend class ASTDeclWriter;
2543  friend class VarDecl;
2544 
2546  Create(ASTContext &Context, DeclContext *DC, SourceLocation StartLoc,
2547  SourceLocation IdLoc, VarTemplateDecl *SpecializedTemplate, QualType T,
2548  TypeSourceInfo *TInfo, StorageClass S,
2550  static VarTemplateSpecializationDecl *CreateDeserialized(ASTContext &C,
2551  unsigned ID);
2552 
2553  void getNameForDiagnostic(raw_ostream &OS, const PrintingPolicy &Policy,
2554  bool Qualified) const override;
2555 
2557  VarDecl *Recent = static_cast<VarDecl *>(this)->getMostRecentDecl();
2558  return cast<VarTemplateSpecializationDecl>(Recent);
2559  }
2560 
2561  /// Retrieve the template that this specialization specializes.
2562  VarTemplateDecl *getSpecializedTemplate() const;
2563 
2564  /// Retrieve the template arguments of the variable template
2565  /// specialization.
2566  const TemplateArgumentList &getTemplateArgs() const { return *TemplateArgs; }
2567 
2568  // TODO: Always set this when creating the new specialization?
2569  void setTemplateArgsInfo(const TemplateArgumentListInfo &ArgsInfo);
2570 
2572  return TemplateArgsInfo;
2573  }
2574 
2575  /// Determine the kind of specialization that this
2576  /// declaration represents.
2578  return static_cast<TemplateSpecializationKind>(SpecializationKind);
2579  }
2580 
2582  return getSpecializationKind() == TSK_ExplicitSpecialization;
2583  }
2584 
2585  /// True if this declaration is an explicit specialization,
2586  /// explicit instantiation declaration, or explicit instantiation
2587  /// definition.
2591  }
2592 
2594  SpecializationKind = TSK;
2595  }
2596 
2597  /// Get the point of instantiation (if any), or null if none.
2599  return PointOfInstantiation;
2600  }
2601 
2603  assert(Loc.isValid() && "point of instantiation must be valid!");
2604  PointOfInstantiation = Loc;
2605  }
2606 
2607  void setCompleteDefinition() { IsCompleteDefinition = true; }
2608 
2609  /// If this variable template specialization is an instantiation of
2610  /// a template (rather than an explicit specialization), return the
2611  /// variable template or variable template partial specialization from which
2612  /// it was instantiated.
2613  llvm::PointerUnion<VarTemplateDecl *, VarTemplatePartialSpecializationDecl *>
2615  if (!isTemplateInstantiation(getSpecializationKind()))
2616  return llvm::PointerUnion<VarTemplateDecl *,
2618 
2619  return getSpecializedTemplateOrPartial();
2620  }
2621 
2622  /// Retrieve the variable template or variable template partial
2623  /// specialization which was specialized by this.
2624  llvm::PointerUnion<VarTemplateDecl *, VarTemplatePartialSpecializationDecl *>
2626  if (const auto *PartialSpec =
2627  SpecializedTemplate.dyn_cast<SpecializedPartialSpecialization *>())
2628  return PartialSpec->PartialSpecialization;
2629 
2630  return SpecializedTemplate.get<VarTemplateDecl *>();
2631  }
2632 
2633  /// Retrieve the set of template arguments that should be used
2634  /// to instantiate the initializer of the variable template or variable
2635  /// template partial specialization from which this variable template
2636  /// specialization was instantiated.
2637  ///
2638  /// \returns For a variable template specialization instantiated from the
2639  /// primary template, this function will return the same template arguments
2640  /// as getTemplateArgs(). For a variable template specialization instantiated
2641  /// from a variable template partial specialization, this function will the
2642  /// return deduced template arguments for the variable template partial
2643  /// specialization itself.
2645  if (const auto *PartialSpec =
2646  SpecializedTemplate.dyn_cast<SpecializedPartialSpecialization *>())
2647  return *PartialSpec->TemplateArgs;
2648 
2649  return getTemplateArgs();
2650  }
2651 
2652  /// Note that this variable template specialization is actually an
2653  /// instantiation of the given variable template partial specialization whose
2654  /// template arguments have been deduced.
2656  const TemplateArgumentList *TemplateArgs) {
2657  assert(!SpecializedTemplate.is<SpecializedPartialSpecialization *>() &&
2658  "Already set to a variable template partial specialization!");
2659  auto *PS = new (getASTContext()) SpecializedPartialSpecialization();
2660  PS->PartialSpecialization = PartialSpec;
2661  PS->TemplateArgs = TemplateArgs;
2662  SpecializedTemplate = PS;
2663  }
2664 
2665  /// Note that this variable template specialization is an instantiation
2666  /// of the given variable template.
2668  assert(!SpecializedTemplate.is<SpecializedPartialSpecialization *>() &&
2669  "Previously set to a variable template partial specialization!");
2670  SpecializedTemplate = TemplDecl;
2671  }
2672 
2673  /// Sets the type of this specialization as it was written by
2674  /// the user.
2676  if (!ExplicitInfo)
2677  ExplicitInfo = new (getASTContext()) ExplicitSpecializationInfo;
2678  ExplicitInfo->TypeAsWritten = T;
2679  }
2680 
2681  /// Gets the type of this specialization as it was written by
2682  /// the user, if it was so written.
2684  return ExplicitInfo ? ExplicitInfo->TypeAsWritten : nullptr;
2685  }
2686 
2687  /// Gets the location of the extern keyword, if present.
2689  return ExplicitInfo ? ExplicitInfo->ExternLoc : SourceLocation();
2690  }
2691 
2692  /// Sets the location of the extern keyword.
2694  if (!ExplicitInfo)
2695  ExplicitInfo = new (getASTContext()) ExplicitSpecializationInfo;
2696  ExplicitInfo->ExternLoc = Loc;
2697  }
2698 
2699  /// Sets the location of the template keyword.
2701  if (!ExplicitInfo)
2702  ExplicitInfo = new (getASTContext()) ExplicitSpecializationInfo;
2703  ExplicitInfo->TemplateKeywordLoc = Loc;
2704  }
2705 
2706  /// Gets the location of the template keyword, if present.
2708  return ExplicitInfo ? ExplicitInfo->TemplateKeywordLoc : SourceLocation();
2709  }
2710 
2711  void Profile(llvm::FoldingSetNodeID &ID) const {
2712  Profile(ID, TemplateArgs->asArray(), getASTContext());
2713  }
2714 
2715  static void Profile(llvm::FoldingSetNodeID &ID,
2716  ArrayRef<TemplateArgument> TemplateArgs,
2717  ASTContext &Context) {
2718  ID.AddInteger(TemplateArgs.size());
2719  for (const TemplateArgument &TemplateArg : TemplateArgs)
2720  TemplateArg.Profile(ID, Context);
2721  }
2722 
2723  static bool classof(const Decl *D) { return classofKind(D->getKind()); }
2724 
2725  static bool classofKind(Kind K) {
2726  return K >= firstVarTemplateSpecialization &&
2727  K <= lastVarTemplateSpecialization;
2728  }
2729 };
2730 
2733  /// The list of template parameters
2734  TemplateParameterList *TemplateParams = nullptr;
2735 
2736  /// The source info for the template arguments as written.
2737  /// FIXME: redundant with TypeAsWritten?
2738  const ASTTemplateArgumentListInfo *ArgsAsWritten = nullptr;
2739 
2740  /// The variable template partial specialization from which this
2741  /// variable template partial specialization was instantiated.
2742  ///
2743  /// The boolean value will be true to indicate that this variable template
2744  /// partial specialization was specialized at this level.
2745  llvm::PointerIntPair<VarTemplatePartialSpecializationDecl *, 1, bool>
2746  InstantiatedFromMember;
2747 
2749  ASTContext &Context, DeclContext *DC, SourceLocation StartLoc,
2750  SourceLocation IdLoc, TemplateParameterList *Params,
2751  VarTemplateDecl *SpecializedTemplate, QualType T, TypeSourceInfo *TInfo,
2753  const ASTTemplateArgumentListInfo *ArgInfos);
2754 
2756  : VarTemplateSpecializationDecl(VarTemplatePartialSpecialization,
2757  Context),
2758  InstantiatedFromMember(nullptr, false) {}
2759 
2760  void anchor() override;
2761 
2762 public:
2763  friend class ASTDeclReader;
2764  friend class ASTDeclWriter;
2765 
2767  Create(ASTContext &Context, DeclContext *DC, SourceLocation StartLoc,
2768  SourceLocation IdLoc, TemplateParameterList *Params,
2769  VarTemplateDecl *SpecializedTemplate, QualType T,
2771  const TemplateArgumentListInfo &ArgInfos);
2772 
2773  static VarTemplatePartialSpecializationDecl *CreateDeserialized(ASTContext &C,
2774  unsigned ID);
2775 
2777  return cast<VarTemplatePartialSpecializationDecl>(
2778  static_cast<VarTemplateSpecializationDecl *>(
2779  this)->getMostRecentDecl());
2780  }
2781 
2782  /// Get the list of template parameters
2784  return TemplateParams;
2785  }
2786 
2787  /// Get the template arguments as written.
2789  return ArgsAsWritten;
2790  }
2791 
2792  /// Retrieve the member variable template partial specialization from
2793  /// which this particular variable template partial specialization was
2794  /// instantiated.
2795  ///
2796  /// \code
2797  /// template<typename T>
2798  /// struct Outer {
2799  /// template<typename U> U Inner;
2800  /// template<typename U> U* Inner<U*> = (U*)(0); // #1
2801  /// };
2802  ///
2803  /// template int* Outer<float>::Inner<int*>;
2804  /// \endcode
2805  ///
2806  /// In this example, the instantiation of \c Outer<float>::Inner<int*> will
2807  /// end up instantiating the partial specialization
2808  /// \c Outer<float>::Inner<U*>, which itself was instantiated from the
2809  /// variable template partial specialization \c Outer<T>::Inner<U*>. Given
2810  /// \c Outer<float>::Inner<U*>, this function would return
2811  /// \c Outer<T>::Inner<U*>.
2813  const auto *First =
2814  cast<VarTemplatePartialSpecializationDecl>(getFirstDecl());
2815  return First->InstantiatedFromMember.getPointer();
2816  }
2817 
2818  void
2820  auto *First = cast<VarTemplatePartialSpecializationDecl>(getFirstDecl());
2821  First->InstantiatedFromMember.setPointer(PartialSpec);
2822  }
2823 
2824  /// Determines whether this variable template partial specialization
2825  /// was a specialization of a member partial specialization.
2826  ///
2827  /// In the following example, the member template partial specialization
2828  /// \c X<int>::Inner<T*> is a member specialization.
2829  ///
2830  /// \code
2831  /// template<typename T>
2832  /// struct X {
2833  /// template<typename U> U Inner;
2834  /// template<typename U> U* Inner<U*> = (U*)(0);
2835  /// };
2836  ///
2837  /// template<> template<typename T>
2838  /// U* X<int>::Inner<T*> = (T*)(0) + 1;
2839  /// \endcode
2841  const auto *First =
2842  cast<VarTemplatePartialSpecializationDecl>(getFirstDecl());
2843  return First->InstantiatedFromMember.getInt();
2844  }
2845 
2846  /// Note that this member template is a specialization.
2848  auto *First = cast<VarTemplatePartialSpecializationDecl>(getFirstDecl());
2849  assert(First->InstantiatedFromMember.getPointer() &&
2850  "Only member templates can be member template specializations");
2851  return First->InstantiatedFromMember.setInt(true);
2852  }
2853 
2854  static bool classof(const Decl *D) { return classofKind(D->getKind()); }
2855 
2856  static bool classofKind(Kind K) {
2857  return K == VarTemplatePartialSpecialization;
2858  }
2859 };
2860 
2861 /// Declaration of a variable template.
2863 protected:
2864  /// Data that is common to all of the declarations of a given
2865  /// variable template.
2866  struct Common : CommonBase {
2867  /// The variable template specializations for this variable
2868  /// template, including explicit specializations and instantiations.
2869  llvm::FoldingSetVector<VarTemplateSpecializationDecl> Specializations;
2870 
2871  /// The variable template partial specializations for this variable
2872  /// template.
2873  llvm::FoldingSetVector<VarTemplatePartialSpecializationDecl>
2875 
2876  Common() = default;
2877  };
2878 
2879  /// Retrieve the set of specializations of this variable template.
2880  llvm::FoldingSetVector<VarTemplateSpecializationDecl> &
2881  getSpecializations() const;
2882 
2883  /// Retrieve the set of partial specializations of this class
2884  /// template.
2885  llvm::FoldingSetVector<VarTemplatePartialSpecializationDecl> &
2886  getPartialSpecializations();
2887 
2889  DeclarationName Name, TemplateParameterList *Params,
2890  NamedDecl *Decl)
2891  : RedeclarableTemplateDecl(VarTemplate, C, DC, L, Name, Params, Decl) {}
2892 
2893  CommonBase *newCommon(ASTContext &C) const override;
2894 
2896  return static_cast<Common *>(RedeclarableTemplateDecl::getCommonPtr());
2897  }
2898 
2899 public:
2900  friend class ASTDeclReader;
2901  friend class ASTDeclWriter;
2902 
2903  /// Load any lazily-loaded specializations from the external source.
2904  void LoadLazySpecializations() const;
2905 
2906  /// Get the underlying variable declarations of the template.
2908  return static_cast<VarDecl *>(TemplatedDecl);
2909  }
2910 
2911  /// Returns whether this template declaration defines the primary
2912  /// variable pattern.
2914  return getTemplatedDecl()->isThisDeclarationADefinition();
2915  }
2916 
2918 
2919  /// Create a variable template node.
2920  static VarTemplateDecl *Create(ASTContext &C, DeclContext *DC,
2922  TemplateParameterList *Params,
2923  VarDecl *Decl);
2924 
2925  /// Create an empty variable template node.
2926  static VarTemplateDecl *CreateDeserialized(ASTContext &C, unsigned ID);
2927 
2928  /// Return the specialization with the provided arguments if it exists,
2929  /// otherwise return the insertion point.
2931  findSpecialization(ArrayRef<TemplateArgument> Args, void *&InsertPos);
2932 
2933  /// Insert the specified specialization knowing that it is not already
2934  /// in. InsertPos must be obtained from findSpecialization.
2935  void AddSpecialization(VarTemplateSpecializationDecl *D, void *InsertPos);
2936 
2938  return cast<VarTemplateDecl>(RedeclarableTemplateDecl::getCanonicalDecl());
2939  }
2941  return cast<VarTemplateDecl>(RedeclarableTemplateDecl::getCanonicalDecl());
2942  }
2943 
2944  /// Retrieve the previous declaration of this variable template, or
2945  /// nullptr if no such declaration exists.
2947  return cast_or_null<VarTemplateDecl>(
2948  static_cast<RedeclarableTemplateDecl *>(this)->getPreviousDecl());
2949  }
2951  return cast_or_null<VarTemplateDecl>(
2952  static_cast<const RedeclarableTemplateDecl *>(
2953  this)->getPreviousDecl());
2954  }
2955 
2957  return cast<VarTemplateDecl>(
2958  static_cast<RedeclarableTemplateDecl *>(this)->getMostRecentDecl());
2959  }
2961  return const_cast<VarTemplateDecl *>(this)->getMostRecentDecl();
2962  }
2963 
2965  return cast_or_null<VarTemplateDecl>(
2967  }
2968 
2969  /// Return the partial specialization with the provided arguments if it
2970  /// exists, otherwise return the insertion point.
2972  findPartialSpecialization(ArrayRef<TemplateArgument> Args, void *&InsertPos);
2973 
2974  /// Insert the specified partial specialization knowing that it is not
2975  /// already in. InsertPos must be obtained from findPartialSpecialization.
2976  void AddPartialSpecialization(VarTemplatePartialSpecializationDecl *D,
2977  void *InsertPos);
2978 
2979  /// Retrieve the partial specializations as an ordered list.
2980  void getPartialSpecializations(
2982 
2983  /// Find a variable template partial specialization which was
2984  /// instantiated
2985  /// from the given member partial specialization.
2986  ///
2987  /// \param D a member variable template partial specialization.
2988  ///
2989  /// \returns the variable template partial specialization which was
2990  /// instantiated
2991  /// from the given member partial specialization, or nullptr if no such
2992  /// partial specialization exists.
2993  VarTemplatePartialSpecializationDecl *findPartialSpecInstantiatedFromMember(
2995 
2997  using spec_range = llvm::iterator_range<spec_iterator>;
2998 
3000  return spec_range(spec_begin(), spec_end());
3001  }
3002 
3004  return makeSpecIterator(getSpecializations(), false);
3005  }
3006 
3008  return makeSpecIterator(getSpecializations(), true);
3009  }
3010 
3011  // Implement isa/cast/dyncast support
3012  static bool classof(const Decl *D) { return classofKind(D->getKind()); }
3013  static bool classofKind(Kind K) { return K == VarTemplate; }
3014 };
3015 
3017  if (auto *PD = P.dyn_cast<TemplateTypeParmDecl *>())
3018  return PD;
3019  if (auto *PD = P.dyn_cast<NonTypeTemplateParmDecl *>())
3020  return PD;
3021  return P.get<TemplateTemplateParmDecl *>();
3022 }
3023 
3025  auto *TD = dyn_cast<TemplateDecl>(D);
3026  return TD && (isa<ClassTemplateDecl>(TD) ||
3027  isa<ClassTemplatePartialSpecializationDecl>(TD) ||
3028  isa<TypeAliasTemplateDecl>(TD) ||
3029  isa<TemplateTemplateParmDecl>(TD))
3030  ? TD
3031  : nullptr;
3032 }
3033 
3034 } // namespace clang
3035 
3036 #endif // LLVM_CLANG_AST_DECLTEMPLATE_H
bool isPackExpansion() const
Whether this parameter pack is a pack expansion.
const internal::VariadicAllOfMatcher< Type > type
Matches Types in the clang AST.
const TemplateArgumentLoc & getTemplateArg(unsigned I) const
Returns the nth template argument.
Definition: DeclTemplate.h:722
void setInstantiationOf(ClassTemplateDecl *TemplDecl)
Note that this class template specialization is an instantiation of the given class template...
static const Decl * getCanonicalDecl(const Decl *D)
spec_iterator spec_begin() const
Represents a function declaration or definition.
Definition: Decl.h:1738
void setTemplateSpecializationKind(TemplateSpecializationKind TSK)
Set the template specialization kind.
Definition: DeclTemplate.h:570
FunctionTemplateDecl * getTemplate() const
Retrieve the template from which this function was specialized.
Definition: DeclTemplate.h:550
unsigned getNumTemplateArgs() const
Returns the number of explicit template arguments that were given.
Definition: DeclTemplate.h:719
RedeclarableTemplateDecl(ConstrainedTemplateDeclInfo *CTDI, Kind DK, ASTContext &C, DeclContext *DC, SourceLocation L, DeclarationName Name, TemplateParameterList *Params, NamedDecl *Decl)
Definition: DeclTemplate.h:836
SourceLocation getPointOfInstantiation() const
Retrieve the first point of instantiation of this function template specialization.
Definition: DeclTemplate.h:581
llvm::iterator_range< redecl_iterator > redecl_range
Definition: DeclBase.h:937
TemplateDecl(Kind DK, DeclContext *DC, SourceLocation L, DeclarationName Name, TemplateParameterList *Params)
Definition: DeclTemplate.h:413
bool isExplicitInstantiationOrSpecialization() const
True if this declaration is an explicit specialization, explicit instantiation declaration, or explicit instantiation definition.
A (possibly-)qualified type.
Definition: Type.h:638
FunctionDecl * Function
The function template specialization that this structure describes.
Definition: DeclTemplate.h:530
RedeclarableTemplateDecl(Kind DK, ASTContext &C, DeclContext *DC, SourceLocation L, DeclarationName Name, TemplateParameterList *Params, NamedDecl *Decl)
Definition: DeclTemplate.h:843
bool isThisDeclarationADefinition() const
Returns whether this template declaration defines the primary class pattern.
void setDefaultArgument(TypeSourceInfo *DefArg)
Set the default argument for this template parameter.
void setTemplateKeywordLoc(SourceLocation Loc)
Sets the location of the template keyword.
llvm::PointerUnion3< TemplateTypeParmDecl *, NonTypeTemplateParmDecl *, TemplateTemplateParmDecl * > TemplateParameter
Stores a template parameter of any kind.
Definition: DeclTemplate.h:62
static ClassTemplateDecl * getDefinition(ClassTemplateDecl *D)
VarTemplateSpecializationDecl * getMostRecentDecl()
ArrayRef< const NamedDecl * > asArray() const
Definition: DeclTemplate.h:129
bool isMemberSpecialization()
Determines whether this class template partial specialization template was a specialization of a memb...
VarDecl * getTemplatedDecl() const
Get the underlying variable declarations of the template.
FixedSizeTemplateParameterListStorage(SourceLocation TemplateLoc, SourceLocation LAngleLoc, ArrayRef< NamedDecl *> Params, SourceLocation RAngleLoc, Expr *RequiresClause)
Definition: DeclTemplate.h:197
C Language Family Type Representation.
static ClassScopeFunctionSpecializationDecl * Create(ASTContext &C, DeclContext *DC, SourceLocation Loc, CXXMethodDecl *FD, bool HasExplicitTemplateArgs, TemplateArgumentListInfo TemplateArgs)
spec_range specializations() const
unsigned size() const
Retrieve the number of template arguments in this template argument list.
Definition: DeclTemplate.h:270
unsigned getNumExpansionTypes() const
Retrieves the number of expansion types in an expanded parameter pack.
static TemplateSpecializationKind getTemplateSpecializationKind(Decl *D)
Determine what kind of template specialization the given declaration is.
spec_iterator spec_end() const
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:87
TypeAliasTemplateDecl * getPreviousDecl()
Retrieve the previous declaration of this function template, or nullptr if no such declaration exists...
ArrayRef< NamedDecl * > asArray()
Definition: DeclTemplate.h:126
void setInheritedDefaultArgument(const ASTContext &C, NonTypeTemplateParmDecl *Parm)
StringRef P
NamedDecl * getTemplatedDecl() const
Get the underlying, templated declaration.
Definition: DeclTemplate.h:453
VarTemplatePartialSpecializationDecl * getMostRecentDecl()
const TypeAliasTemplateDecl * getPreviousDecl() const
bool isExpandedParameterPack() const
Whether this parameter is a template template parameter pack that has a known list of different templ...
void removeDefaultArgument()
Removes the default argument of this template parameter.
Declaration of a variable template.
size_t numTrailingObjects(OverloadToken< Expr *>) const
Definition: DeclTemplate.h:97
void setSpecializationKind(TemplateSpecializationKind TSK)
NamedDecl * getParam(unsigned Idx)
Definition: DeclTemplate.h:133
static bool classofKind(Kind K)
A container of type source information.
Definition: Decl.h:87
TemplateParameterList * getTemplateParameters() const
Get the list of template parameters.
constexpr XRayInstrMask Function
Definition: XRayInstr.h:39
TypeSourceInfo * getDefaultArgumentInfo() const
Retrieves the default argument&#39;s source information, if any.
const Expr * getRequiresClause() const
Get the constraint-expression from the associated requires-clause (if any)
Definition: DeclTemplate.h:440
static bool classofKind(Kind K)
const ClassTemplateDecl * getCanonicalDecl() const
bool defaultArgumentWasInherited() const
Determines whether the default argument was inherited from a previous declaration of this template...
TemplateParameterList * getTemplateParameters() const
Get the list of template parameters.
bool isPackExpansion() const
Whether this parameter pack is a pack expansion.
unsigned getDepth() const
Get the nesting depth of the template parameter.
static void Profile(llvm::FoldingSetNodeID &ID, ArrayRef< TemplateArgument > TemplateArgs, ASTContext &Context)
TemplateParameterList * getExpansionTemplateParameters(unsigned I) const
Retrieve a particular expansion type within an expanded parameter pack.
Represents a variable declaration or definition.
Definition: Decl.h:813
Declaration of a redeclarable template.
Definition: DeclTemplate.h:737
TemplateParameterList * getTemplateParameters() const
Definition: DeclTemplate.h:376
NamedDecl *const * const_iterator
Iterates through the template parameters in this list.
Definition: DeclTemplate.h:117
bool hasDefaultArgument() const
Determine whether this template parameter has a default argument.
TemplateSpecializationKind getTemplateSpecializationKind() const
Determine what kind of template specialization this is.
Definition: DeclTemplate.h:553
SourceLocation getExternLoc() const
Gets the location of the extern keyword, if present.
llvm::PointerUnion< TemplateParameterList *, ConstrainedTemplateDeclInfo * > TemplateParams
The template parameter list and optional requires-clause associated with this declaration; alternativ...
Definition: DeclTemplate.h:476
static bool classofKind(Kind K)
const TemplateArgumentLoc * getTemplateArgs() const
Returns the explicit template arguments that were given.
Definition: DeclTemplate.h:714
Represents a variable template specialization, which refers to a variable template with a given set o...
SourceLocation getPointOfInstantiation() const
Get the point of instantiation (if any), or null if none.
SourceLocation getLAngleLoc() const
Definition: DeclTemplate.h:173
TemplateParmPosition(unsigned D, unsigned P)
Represents an explicit template argument list in C++, e.g., the "<int>" in "sort<int>".
Definition: TemplateBase.h:604
Stores a list of template parameters for a TemplateDecl and its derived classes.
Definition: DeclTemplate.h:68
Describes how types, statements, expressions, and declarations should be printed. ...
Definition: PrettyPrinter.h:38
TemplateSpecializationKind getTemplateSpecializationKind() const
Determine what kind of template specialization this is.
Definition: DeclTemplate.h:630
TypeSourceInfo * getTypeAsWritten() const
Gets the type of this specialization as it was written by the user, if it was so written.
SourceRange getSourceRange() const override LLVM_READONLY
Source range that this declaration covers.
Definition: DeclTemplate.h:462
Provides information about a dependent function-template specialization declaration.
Definition: DeclTemplate.h:672
Represents the builtin template declaration which is used to implement __make_integer_seq and other b...
static bool classof(const Decl *D)
const VarTemplateDecl * getCanonicalDecl() const
static bool classof(const Decl *D)
llvm::iterator_range< spec_iterator > spec_range
llvm::PointerUnion< VarTemplateDecl *, VarTemplatePartialSpecializationDecl * > getSpecializedTemplateOrPartial() const
Retrieve the variable template or variable template partial specialization which was specialized by t...
const TemplateArgumentList & getTemplateArgs() const
Retrieve the template arguments of the class template specialization.
bool isTemplateExplicitInstantiationOrSpecialization(TemplateSpecializationKind Kind)
True if this template specialization kind is an explicit specialization, explicit instantiation decla...
Definition: Specifiers.h:177
Provides common interface for the Decls that can be redeclared.
Definition: Redeclarable.h:85
TemplateArgumentList(const TemplateArgumentList *Other)
Produces a shallow copy of the given template argument list.
Definition: DeclTemplate.h:251
Represents a class template specialization, which refers to a class template with a given set of temp...
One of these records is kept for each identifier that is lexed.
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:155
TemplateSpecializationKind getSpecializationKind() const
Determine the kind of specialization that this declaration represents.
void setInstantiationOf(VarTemplateDecl *TemplDecl)
Note that this variable template specialization is an instantiation of the given variable template...
Defines the position of a template parameter within a template parameter list.
const DefArgStorage & getDefaultArgStorage() const
void * allocateDefaultArgStorageChain(const ASTContext &C)
Definition: Format.h:2072
ClassTemplateSpecializationDecl * getMostRecentDecl()
unsigned getPosition() const
Get the position of the template parameter within its parameter list.
static bool classofKind(Kind K)
Definition: DeclTemplate.h:458
void Profile(llvm::FoldingSetNodeID &ID)
Definition: DeclTemplate.h:591
Stores the template parameter list and associated constraints for TemplateDecl objects that track ass...
Definition: DeclTemplate.h:370
Declaration of a function specialization at template class scope.
SourceLocation getTemplateLoc() const
Definition: DeclTemplate.h:172
TypeSourceInfo * getFriendType() const
If this friend declaration names a templated type (or a dependent member type of a templated type)...
llvm::iterator_range< spec_iterator > spec_range
TypeAliasTemplateDecl(ASTContext &C, DeclContext *DC, SourceLocation L, DeclarationName Name, TemplateParameterList *Params, NamedDecl *Decl)
void setDefaultArgument(Expr *DefArg)
Set the default argument for this template parameter, and whether that default argument was inherited...
static bool classof(const Decl *D)
Provides information about a function template specialization, which is a FunctionDecl that has been ...
Definition: DeclTemplate.h:508
TypeAliasDecl * getTemplatedDecl() const
Get the underlying function declaration of the template.
const TemplateArgumentList * TemplateArguments
The template arguments used to produce the function template specialization from the function templat...
Definition: DeclTemplate.h:540
FunctionTemplateDecl * getCanonicalDecl() override
Retrieves the canonical declaration of this template.
Common * getCommonPtr() const
unsigned getMinRequiredArguments() const
Returns the minimum number of arguments needed to form a template specialization. ...
const RedeclarableTemplateDecl * getCanonicalDecl() const
Definition: DeclTemplate.h:858
SourceRange getSourceRange() const override LLVM_READONLY
Source range that this declaration covers.
void set(ArgType Arg)
Set the default argument.
Definition: DeclTemplate.h:342
bool isInherited() const
Determine whether the default argument for this parameter was inherited from a previous declaration o...
Definition: DeclTemplate.h:318
A convenient class for passing around template argument information.
Definition: TemplateBase.h:555
const TemplateArgumentList & getTemplateArgs() const
Retrieve the template arguments of the variable template specialization.
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified...
SourceLocation getTemplateKeywordLoc() const
Gets the location of the template keyword, if present.
static bool classof(const Decl *D)
Definition: DeclTemplate.h:456
bool isMemberSpecialization() const
Determines whether this template was a specialization of a member template.
Definition: DeclTemplate.h:880
static bool classofKind(Kind K)
static void Profile(llvm::FoldingSetNodeID &ID, ArrayRef< TemplateArgument > TemplateArgs, ASTContext &Context)
Definition: DeclTemplate.h:597
TemplateDecl * getAsTypeTemplateDecl(Decl *D)
Represents a declaration of a type.
Definition: Decl.h:2874
A set of unresolved declarations.
Definition: UnresolvedSet.h:61
RedeclarableTemplateDecl * getInstantiatedFromMemberTemplate() const
Retrieve the member template from which this template was instantiated, or nullptr if this template w...
Definition: DeclTemplate.h:927
void setInstantiatedFromMember(ClassTemplatePartialSpecializationDecl *PartialSpec)
void setInstantiationOf(ClassTemplatePartialSpecializationDecl *PartialSpec, const TemplateArgumentList *TemplateArgs)
Note that this class template specialization is actually an instantiation of the given class template...
const TemplateArgument & operator[](unsigned Idx) const
Retrieve the template argument at a given index.
Definition: DeclTemplate.h:261
void setSpecializationKind(TemplateSpecializationKind TSK)
spec_range specializations() const
ArgType get() const
Get the default argument&#39;s value.
Definition: DeclTemplate.h:322
ClassTemplatePartialSpecializationDecl * getInstantiatedFromMemberTemplate() const
TemplateParameterList * getTemplateParameterList(unsigned i) const
unsigned getIndex() const
Get the index of the template parameter within its parameter list.
static bool classofKind(Kind K)
bool hasDefaultArgument() const
Determine whether this template parameter has a default argument.
QualType getExpansionType(unsigned I) const
Retrieve a particular expansion type within an expanded parameter pack.
SourceLocation PointOfInstantiation
The point at which this function template specialization was first instantiated.
Definition: DeclTemplate.h:547
const TemplateArgument * data() const
Retrieve a pointer to the template argument list.
Definition: DeclTemplate.h:273
const TypeAliasTemplateDecl * getCanonicalDecl() const
CXXRecordDecl * getTemplatedDecl() const
Get the underlying class declarations of the template.
bool isParameterPack() const
Whether this template template parameter is a template parameter pack.
ClassTemplateDecl(ASTContext &C, DeclContext *DC, SourceLocation L, DeclarationName Name, TemplateParameterList *Params, NamedDecl *Decl)
bool isExpandedParameterPack() const
Whether this parameter is a non-type template parameter pack that has a known list of different types...
llvm::FoldingSetVector< FunctionTemplateSpecializationInfo > Specializations
The function template specializations for this function template, including explicit specializations ...
Definition: DeclTemplate.h:978
const VarTemplateDecl * getMostRecentDecl() const
Sema - This implements semantic analysis and AST building for C.
Definition: Sema.h:278
A placeholder type used to construct an empty shell of a decl-derived type that will be filled in lat...
Definition: DeclBase.h:103
Represents the declaration of a typedef-name via a C++11 alias-declaration.
Definition: Decl.h:3038
Represents a ValueDecl that came out of a declarator.
Definition: Decl.h:689
bool isParameterPack() const
Whether this parameter is a non-type template parameter pack.
void clear()
Remove the default argument, even if it was inherited.
Definition: DeclTemplate.h:359
llvm::FoldingSetVector< ClassTemplatePartialSpecializationDecl > PartialSpecializations
The class template partial specializations for this class template.
spec_range specializations() const
const NamedDecl * getParam(unsigned Idx) const
Definition: DeclTemplate.h:137
const FunctionTemplateDecl * getMostRecentDecl() const
TemplateParameterList * getTemplateParameters() const
Get the list of template parameters.
Definition: DeclTemplate.h:432
static bool classof(const Decl *D)
QualType getInjectedSpecializationType() const
Retrieves the injected specialization type for this partial specialization.
llvm::iterator_range< spec_iterator > spec_range
This represents one expression.
Definition: Expr.h:106
CommonBase * getCommonPtr() const
Retrieves the "common" pointer shared by all (re-)declarations of the same template.
void setAssociatedConstraints(Expr *AC)
Definition: DeclTemplate.h:487
SourceLocation End
const VarTemplateDecl * getPreviousDecl() const
llvm::FoldingSetVector< VarTemplatePartialSpecializationDecl > PartialSpecializations
The variable template partial specializations for this variable template.
Expr * getAssociatedConstraints() const
Definition: DeclTemplate.h:445
void setInherited(const ASTContext &C, ParmDecl *InheritedFrom)
Set that the default argument was inherited from another parameter.
Definition: DeclTemplate.h:348
int Id
Definition: ASTDiff.cpp:191
static bool classof(const Decl *D)
MemberSpecializationInfo(NamedDecl *IF, TemplateSpecializationKind TSK, SourceLocation POI=SourceLocation())
Definition: DeclTemplate.h:618
llvm::PointerIntPair< FunctionTemplateDecl *, 2 > Template
The function template from which this function template specialization was generated.
Definition: DeclTemplate.h:536
llvm::PointerUnion< NamedDecl *, TypeSourceInfo * > FriendUnion
static bool classof(const Decl *D)
#define bool
Definition: stdbool.h:31
spec_iterator spec_end() const
Data that is common to all of the declarations of a given variable template.
spec_iterator spec_end() const
void setMemberSpecialization()
Note that this member template is a specialization.
Definition: DeclTemplate.h:885
ArrayRef< TemplateArgument > asArray() const
Produce this as an array ref.
Definition: DeclTemplate.h:264
llvm::PointerUnion< ClassTemplateDecl *, ClassTemplatePartialSpecializationDecl * > getSpecializedTemplateOrPartial() const
Retrieve the class template or class template partial specialization which was specialized by this...
spec_iterator spec_begin() const
int Depth
Definition: ASTDiff.cpp:191
const ASTTemplateArgumentListInfo * getTemplateArgsAsWritten() const
Get the template arguments as written.
static bool classof(const Decl *D)
StorageClass
Storage classes.
Definition: Specifiers.h:206
Declaration of an alias template.
FunctionTemplateDecl * getTemplate(unsigned I) const
Returns the i&#39;th template candidate.
Definition: DeclTemplate.h:708
static ArrayRef< TemplateArgument > getTemplateArgs(EntryType *D)
Definition: DeclTemplate.h:763
void setPointOfInstantiation(SourceLocation Loc)
void setTypeAsWritten(TypeSourceInfo *T)
Sets the type of this specialization as it was written by the user.
SourceLocation getEnd() const
unsigned getNumExpansionTemplateParameters() const
Retrieves the number of expansion template parameters in an expanded parameter pack.
TemplateTemplateParmDecl - Declares a template template parameter, e.g., "T" in.
ClassTemplatePartialSpecializationDecl * getInstantiatedFromMember() const
Retrieve the member class template partial specialization from which this particular class template p...
BuiltinTemplateKind getBuiltinTemplateKind() const
Data that is common to all of the declarations of a given class template.
bool isTemplateInstantiation(TemplateSpecializationKind Kind)
Determine whether this template specialization kind refers to an instantiation of an entity (as oppos...
Definition: Specifiers.h:170
static bool classofKind(Kind K)
static TemplateParameterList * Create(const ASTContext &C, SourceLocation TemplateLoc, SourceLocation LAngleLoc, ArrayRef< NamedDecl *> Params, SourceLocation RAngleLoc, Expr *RequiresClause)
bool defaultArgumentWasInherited() const
Determines whether the default argument was inherited from a previous declaration of this template...
static DeclType * getDecl(EntryType *D)
Definition: DeclTemplate.h:759
ClassTemplateDecl * getInstantiatedFromMemberTemplate() const
TypeSourceInfo * getExpansionTypeSourceInfo(unsigned I) const
Retrieve a particular expansion type source info within an expanded parameter pack.
void setPointOfInstantiation(SourceLocation POI)
Set the (first) point of instantiation of this function template specialization.
Definition: DeclTemplate.h:587
NamedDecl * getInstantiatedFrom() const
Retrieve the member declaration from which this member was instantiated.
Definition: DeclTemplate.h:627
OnStackType
Type used to indicate that the template argument list itself is a stack object.
Definition: DeclTemplate.h:231
bool isExplicitInstantiationOrSpecialization() const
True if this declaration is an explicit specialization, explicit instantiation declaration, or explicit instantiation definition.
Definition: DeclTemplate.h:564
#define false
Definition: stdbool.h:33
Stores a list of template parameters and the associated requires-clause (if any) for a TemplateDecl a...
Definition: DeclTemplate.h:189
spec_iterator spec_begin() const
QualType InjectedClassNameType
The injected-class-name type for this class template.
static bool classofKind(Kind K)
const ASTTemplateArgumentListInfo * TemplateArgumentsAsWritten
The template arguments as written in the sources, if provided.
Definition: DeclTemplate.h:543
FunctionDecl * getTemplatedDecl() const
Get the underlying function declaration of the template.
Encodes a location in the source.
ClassTemplateDecl * getPreviousDecl()
Retrieve the previous declaration of this class template, or nullptr if no such declaration exists...
llvm::PointerUnion< ClassTemplateDecl *, ClassTemplatePartialSpecializationDecl * > getInstantiatedFrom() const
If this class template specialization is an instantiation of a template (rather than an explicit spec...
const FunctionTemplateDecl * getPreviousDecl() const
void setExternLoc(SourceLocation Loc)
Sets the location of the extern keyword.
bool wasDeclaredWithTypename() const
Whether this template type parameter was declared with the &#39;typename&#39; keyword.
const TemplateArgumentListInfo & templateArgs() const
ASTContext & getASTContext() const LLVM_READONLY
Definition: DeclBase.cpp:376
void setPosition(unsigned P)
const Expr * getRequiresClause() const
The constraint-expression of the associated requires-clause.
Definition: DeclTemplate.h:168
Represents a static or instance method of a struct/union/class.
Definition: DeclCXX.h:2041
Expr * getRequiresClause()
The constraint-expression of the associated requires-clause.
Definition: DeclTemplate.h:163
bool isMemberSpecialization()
Determines whether this variable template partial specialization was a specialization of a member par...
Data that is common to all of the declarations of a given function template.
Definition: DeclTemplate.h:975
void Profile(llvm::FoldingSetNodeID &ID) const
BuiltinTemplateKind
Kinds of BuiltinTemplateDecl.
Definition: Builtins.h:243
TemplateSpecializationKind getSpecializationKind() const
Determine the kind of specialization that this declaration represents.
unsigned getNumTemplates() const
Returns the number of function templates that this might be a specialization of.
Definition: DeclTemplate.h:705
static bool classof(const Decl *D)
Definition: DeclTemplate.h:947
Common * getCommonPtr() const
const TemplateArgumentListInfo & getTemplateArgsInfo() const
FunctionTemplateDecl(ASTContext &C, DeclContext *DC, SourceLocation L, DeclarationName Name, TemplateParameterList *Params, NamedDecl *Decl)
Definition: DeclTemplate.h:992
bool hasDefaultArgument() const
Determine whether this template parameter has a default argument.
void init(NamedDecl *templatedDecl, TemplateParameterList *templateParams)
Initialize the underlying templated declaration and template parameters.
Definition: DeclTemplate.h:497
void setPointOfInstantiation(SourceLocation Loc)
FunctionTemplateDecl * getPreviousDecl()
Retrieve the previous declaration of this function template, or nullptr if no such declaration exists...
ClassTemplateDecl * getMostRecentDecl()
This template specialization was formed from a template-id but has not yet been declared, defined, or instantiated.
Definition: Specifiers.h:149
FunctionTemplateDecl * getInstantiatedFromMemberTemplate() const
const ParmDecl * getInheritedFrom() const
Get the parameter from which we inherit the default argument, if any.
Definition: DeclTemplate.h:333
TypeSourceInfo * getTypeAsWritten() const
Gets the type of this specialization as it was written by the user, if it was so written.
SourceLocation getRAngleLoc() const
Definition: DeclTemplate.h:174
size_t numTrailingObjects(OverloadToken< NamedDecl *>) const
Definition: DeclTemplate.h:93
void setInstantiatedFromMember(VarTemplatePartialSpecializationDecl *PartialSpec)
static bool classofKind(Kind K)
SourceRange getSourceRange() const override LLVM_READONLY
Source range that this declaration covers.
TemplateDecl(ConstrainedTemplateDeclInfo *CTDI, Kind DK, DeclContext *DC, SourceLocation L, DeclarationName Name, TemplateParameterList *Params, NamedDecl *Decl)
Definition: DeclTemplate.h:418
const TemplateArgumentList & getTemplateInstantiationArgs() const
Retrieve the set of template arguments that should be used to instantiate the initializer of the vari...
static bool classofKind(Kind K)
Definition: DeclTemplate.h:949
llvm::PointerUnion< VarTemplateDecl *, VarTemplatePartialSpecializationDecl * > getInstantiatedFrom() const
If this variable template specialization is an instantiation of a template (rather than an explicit s...
void setDeclaredWithTypename(bool withTypename)
Set whether this template type parameter was declared with the &#39;typename&#39; or &#39;class&#39; keyword...
Represents a pack expansion of types.
Definition: Type.h:5355
NamedDecl * TemplatedDecl
Definition: DeclTemplate.h:468
void setTemplateSpecializationKind(TemplateSpecializationKind TSK)
Set the template specialization kind.
Definition: DeclTemplate.h:639
TemplateDecl(ConstrainedTemplateDeclInfo *CTDI, Kind DK, DeclContext *DC, SourceLocation L, DeclarationName Name, TemplateParameterList *Params)
Definition: DeclTemplate.h:405
Defines various enumerations that describe declaration and type specifiers.
Represents a template argument.
Definition: TemplateBase.h:51
SourceLocation getTemplateKeywordLoc() const
Gets the location of the template keyword, if present.
TagTypeKind
The kind of a tag type.
Definition: Type.h:5031
void setMemberSpecialization()
Note that this member template is a specialization.
const ClassTemplateDecl * getPreviousDecl() const
void removeDefaultArgument()
Removes the default argument of this template parameter.
Dataflow Directional Tag Classes.
bool isValid() const
Return true if this is a valid SourceLocation object.
bool containsUnexpandedParameterPack() const
Determine whether this template parameter list contains an unexpanded parameter pack.
Definition: DeclTemplate.h:158
DeclContext - This is used only as base class of specific decl types that can act as declaration cont...
Definition: DeclBase.h:1262
llvm::PointerIntPair< RedeclarableTemplateDecl *, 1, bool > InstantiatedFromMember
The template from which this was most directly instantiated (or null).
Definition: DeclTemplate.h:814
The base class of all kinds of template declarations (e.g., class, function, etc.).
Definition: DeclTemplate.h:399
static ArrayRef< TemplateArgument > getTemplateArgs(FunctionTemplateSpecializationInfo *I)
Definition: DeclTemplate.h:963
static BuiltinTemplateDecl * Create(const ASTContext &C, DeclContext *DC, DeclarationName Name, BuiltinTemplateKind BTK)
Reads an AST files chain containing the contents of a translation unit.
Definition: ASTReader.h:355
static bool classof(const Decl *D)
void setInheritedDefaultArgument(const ASTContext &C, TemplateTypeParmDecl *Prev)
Set that this default argument was inherited from another parameter.
bool isExplicitInstantiationOrSpecialization() const
True if this declaration is an explicit specialization, explicit instantiation declaration, or explicit instantiation definition.
unsigned getNumTemplateParameters() const
The name of a declaration.
Kind getKind() const
Definition: DeclBase.h:421
static void Profile(llvm::FoldingSetNodeID &ID, ArrayRef< TemplateArgument > TemplateArgs, ASTContext &Context)
void setTemplateParameters(TemplateParameterList *TParams)
Definition: DeclTemplate.h:478
llvm::iterator_range< redecl_iterator > redecl_range
Definition: Redeclarable.h:291
Storage for a default argument.
Definition: DeclTemplate.h:287
TemplateSpecializationKind
Describes the kind of template specialization that a particular template specialization declaration r...
Definition: Specifiers.h:146
Expr * getDefaultArgument() const
Retrieve the default argument, if any.
bool defaultArgumentWasInherited() const
Determines whether the default argument was inherited from a previous declaration of this template...
bool isThisDeclarationADefinition() const
Returns whether this template declaration defines the primary variable pattern.
ClassTemplatePartialSpecializationDecl * getMostRecentDecl()
unsigned getDepth() const
Get the depth of this template parameter list in the set of template parameter lists.
const DefArgStorage & getDefaultArgStorage() const
void setTemplateKeywordLoc(SourceLocation Loc)
Sets the location of the template keyword.
static bool classofKind(Kind K)
ClassTemplateDecl(ConstrainedTemplateDeclInfo *CTDI, ASTContext &C, DeclContext *DC, SourceLocation L, DeclarationName Name, TemplateParameterList *Params, NamedDecl *Decl)
Location wrapper for a TemplateArgument.
Definition: TemplateBase.h:450
const_iterator end() const
Definition: DeclTemplate.h:122
void setInstantiatedFromMemberTemplate(RedeclarableTemplateDecl *TD)
Definition: DeclTemplate.h:931
This template specialization was declared or defined by an explicit specialization (C++ [temp...
Definition: Specifiers.h:156
friend TrailingObjects
Definition: OpenMPClause.h:99
static DeclType * getDecl(FunctionTemplateSpecializationInfo *I)
Definition: DeclTemplate.h:958
VarTemplateDecl * getPreviousDecl()
Retrieve the previous declaration of this variable template, or nullptr if no such declaration exists...
RedeclarableTemplateDecl * getCanonicalDecl() override
Retrieves the canonical declaration of this template.
Definition: DeclTemplate.h:855
ClassTemplateDecl * getCanonicalDecl() override
Retrieves the canonical declaration of this template.
TypeAliasTemplateDecl * getCanonicalDecl() override
Retrieves the canonical declaration of this template.
static bool classof(const Decl *D)
const TemplateArgumentLoc & getDefaultArgument() const
Retrieve the default argument, if any.
const ClassTemplateDecl * getMostRecentDecl() const
void setInstantiationOf(VarTemplatePartialSpecializationDecl *PartialSpec, const TemplateArgumentList *TemplateArgs)
Note that this variable template specialization is actually an instantiation of the given variable te...
SpecIterator(typename llvm::FoldingSetVector< EntryType >::iterator SetIter)
Definition: DeclTemplate.h:778
TemplateDecl(Kind DK, DeclContext *DC, SourceLocation L, DeclarationName Name, TemplateParameterList *Params, NamedDecl *Decl)
Definition: DeclTemplate.h:426
A template argument list.
Definition: DeclTemplate.h:210
redeclarable_base::redecl_iterator redecl_iterator
Definition: DeclTemplate.h:937
Defines the C++ Decl subclasses, other than those for templates (found in DeclTemplate.h) and friends (in DeclFriend.h).
Defines the clang::SourceLocation class and associated facilities.
VarTemplateDecl * getMostRecentDecl()
static bool classof(const Decl *D)
Represents a C++ struct/union/class.
Definition: DeclCXX.h:300
TemplateParameterList(SourceLocation TemplateLoc, SourceLocation LAngleLoc, ArrayRef< NamedDecl *> Params, SourceLocation RAngleLoc, Expr *RequiresClause)
TypeAliasTemplateDecl * getInstantiatedFromMemberTemplate() const
Provides information a specialization of a member of a class template, which may be a member function...
Definition: DeclTemplate.h:608
static bool classof(const Decl *D)
VarTemplateDecl(ASTContext &C, DeclContext *DC, SourceLocation L, DeclarationName Name, TemplateParameterList *Params, NamedDecl *Decl)
SourceLocation getFriendLoc() const
Retrieves the location of the &#39;friend&#39; keyword.
Declaration of a class template.
VarTemplateDecl * getCanonicalDecl() override
Retrieves the canonical declaration of this template.
void removeDefaultArgument()
Removes the default argument of this template parameter.
static SpecIterator< EntryType > makeSpecIterator(llvm::FoldingSetVector< EntryType > &Specs, bool isEnd)
Definition: DeclTemplate.h:791
void setExternLoc(SourceLocation Loc)
Sets the location of the extern keyword.
const_iterator begin() const
Definition: DeclTemplate.h:120
VarTemplateDecl * getInstantiatedFromMemberTemplate() const
Kind
Lists the kind of concrete classes of Decl.
Definition: DeclBase.h:90
llvm::FoldingSetVector< VarTemplateSpecializationDecl > Specializations
The variable template specializations for this variable template, including explicit specializations ...
llvm::FoldingSetVector< ClassTemplateSpecializationDecl > Specializations
The class template specializations for this class template, including explicit specializations and in...
Common * getCommonPtr() const
SourceLocation getExternLoc() const
Gets the location of the extern keyword, if present.
bool isSet() const
Determine whether there is a default argument for this parameter.
Definition: DeclTemplate.h:314
void setMemberSpecialization()
Note that this member template is a specialization.
void setInheritedDefaultArgument(const ASTContext &C, TemplateTemplateParmDecl *Prev)
QualType getDefaultArgument() const
Retrieve the default argument, if any.
void Profile(llvm::FoldingSetNodeID &ID) const
void setTemplateParameters(TemplateParameterList *TParams)
Definition: DeclTemplate.h:383
const FunctionTemplateDecl * getCanonicalDecl() const
A trivial tuple used to represent a source range.
This represents a decl that may have a name.
Definition: Decl.h:249
NamedDecl * getAsNamedDecl(TemplateParameter P)
const TemplateArgumentList & getTemplateInstantiationArgs() const
Retrieve the set of template arguments that should be used to instantiate members of the class templa...
Declaration of a friend template.
VarTemplatePartialSpecializationDecl * getInstantiatedFromMember() const
Retrieve the member variable template partial specialization from which this particular variable temp...
SourceLocation getPointOfInstantiation() const
Retrieve the first point of instantiation of this member.
Definition: DeclTemplate.h:648
NamedDecl * getFriendDecl() const
If this friend declaration names a templated function (or a member function of a templated type)...
void setTypeAsWritten(TypeSourceInfo *T)
Sets the type of this specialization as it was written by the user.
SourceLocation getPointOfInstantiation() const
Get the point of instantiation (if any), or null if none.
void setPointOfInstantiation(SourceLocation POI)
Set the first point of instantiation.
Definition: DeclTemplate.h:653
SourceLocation getBegin() const
const ASTTemplateArgumentListInfo * getTemplateArgsAsWritten() const
Get the template arguments as written.
SourceRange getSourceRange() const LLVM_READONLY
Definition: DeclTemplate.h:176
TrailingObjects::FixedSizeStorageOwner FixedSizeStorageOwner
Definition: DeclTemplate.h:182
Declaration of a template function.
Definition: DeclTemplate.h:969
bool isThisDeclarationADefinition() const
Returns whether this template declaration defines the primary pattern.
static OMPLinearClause * Create(const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc, OpenMPLinearClauseKind Modifier, SourceLocation ModifierLoc, SourceLocation ColonLoc, SourceLocation EndLoc, ArrayRef< Expr *> VL, ArrayRef< Expr *> PL, ArrayRef< Expr *> IL, Expr *Step, Expr *CalcStep, Stmt *PreInit, Expr *PostUpdate)
Creates clause with a list of variables VL and a linear step Step.
FunctionTemplateDecl * getMostRecentDecl()
TemplateArgumentList(OnStackType, ArrayRef< TemplateArgument > Args)
Construct a new, temporary template argument list on the stack.
Definition: DeclTemplate.h:242