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