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