clang  7.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 (const auto *Prev = ValueOrInherited.template dyn_cast<ParmDecl *>())
325  Storage = &Prev->getDefaultArgStorage();
326  if (const auto *C = Storage->ValueOrInherited.template dyn_cast<Chain *>())
327  return C->Value;
328  return Storage->ValueOrInherited.template get<ArgType>();
329  }
330 
331  /// Get the parameter from which we inherit the default argument, if any.
332  /// This is the parameter on which the default argument was actually written.
333  const ParmDecl *getInheritedFrom() const {
334  if (const auto *D = ValueOrInherited.template dyn_cast<ParmDecl *>())
335  return D;
336  if (const auto *C = ValueOrInherited.template dyn_cast<Chain *>())
337  return C->PrevDeclWithDefaultArg;
338  return nullptr;
339  }
340 
341  /// Set the default argument.
342  void set(ArgType Arg) {
343  assert(!isSet() && "default argument already set");
344  ValueOrInherited = Arg;
345  }
346 
347  /// Set that the default argument was inherited from another parameter.
348  void setInherited(const ASTContext &C, ParmDecl *InheritedFrom) {
349  assert(!isInherited() && "default argument already inherited");
350  InheritedFrom = getParmOwningDefaultArg(InheritedFrom);
351  if (!isSet())
352  ValueOrInherited = InheritedFrom;
353  else
354  ValueOrInherited = new (allocateDefaultArgStorageChain(C))
355  Chain{InheritedFrom, ValueOrInherited.template get<ArgType>()};
356  }
357 
358  /// Remove the default argument, even if it was inherited.
359  void clear() {
360  ValueOrInherited = ArgType();
361  }
362 };
363 
364 //===----------------------------------------------------------------------===//
365 // Kinds of Templates
366 //===----------------------------------------------------------------------===//
367 
368 /// \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 auto *const C = cast<TemplateDecl>(getCanonicalDecl());
447  const auto *const CTDI =
448  C->TemplateParams.dyn_cast<ConstrainedTemplateDeclInfo *>();
449  return CTDI ? CTDI->getAssociatedConstraints() : nullptr;
450  }
451 
452  /// Get the underlying, templated declaration.
453  NamedDecl *getTemplatedDecl() const { return TemplatedDecl; }
454 
455  // Implement isa/cast/dyncast/etc.
456  static bool classof(const Decl *D) { return classofKind(D->getKind()); }
457 
458  static bool classofKind(Kind K) {
459  return K >= firstTemplate && K <= lastTemplate;
460  }
461 
462  SourceRange getSourceRange() const override LLVM_READONLY {
463  return SourceRange(getTemplateParameters()->getTemplateLoc(),
464  TemplatedDecl->getSourceRange().getEnd());
465  }
466 
467 protected:
469 
470  /// \brief The template parameter list and optional requires-clause
471  /// associated with this declaration; alternatively, a
472  /// \c ConstrainedTemplateDeclInfo if the associated constraints of the
473  /// template are being tracked by this particular declaration.
474  llvm::PointerUnion<TemplateParameterList *,
477 
478  void setTemplateParameters(TemplateParameterList *TParams) {
479  if (auto *const CTDI =
480  TemplateParams.dyn_cast<ConstrainedTemplateDeclInfo *>()) {
481  CTDI->setTemplateParameters(TParams);
482  } else {
483  TemplateParams = TParams;
484  }
485  }
486 
488  assert(isCanonicalDecl() &&
489  "Attaching associated constraints to non-canonical Decl");
490  TemplateParams.get<ConstrainedTemplateDeclInfo *>()
491  ->setAssociatedConstraints(AC);
492  }
493 
494 public:
495  /// \brief Initialize the underlying templated declaration and
496  /// template parameters.
497  void init(NamedDecl *templatedDecl, TemplateParameterList* templateParams) {
498  assert(!TemplatedDecl && "TemplatedDecl already set!");
499  assert(!TemplateParams && "TemplateParams already set!");
500  TemplatedDecl = templatedDecl;
501  TemplateParams = templateParams;
502  }
503 };
504 
505 /// \brief Provides information about a function template specialization,
506 /// which is a FunctionDecl that has been explicitly specialization or
507 /// instantiated from a function template.
508 class FunctionTemplateSpecializationInfo : public llvm::FoldingSetNode {
510  FunctionTemplateDecl *Template,
512  const TemplateArgumentList *TemplateArgs,
513  const ASTTemplateArgumentListInfo *TemplateArgsAsWritten,
514  SourceLocation POI)
515  : Function(FD), Template(Template, TSK - 1),
516  TemplateArguments(TemplateArgs),
517  TemplateArgumentsAsWritten(TemplateArgsAsWritten),
518  PointOfInstantiation(POI) {}
519 
520 public:
524  const TemplateArgumentList *TemplateArgs,
525  const TemplateArgumentListInfo *TemplateArgsAsWritten,
526  SourceLocation POI);
527 
528  /// \brief The function template specialization that this structure
529  /// describes.
531 
532  /// \brief The function template from which this function template
533  /// specialization was generated.
534  ///
535  /// The two bits contain the top 4 values of TemplateSpecializationKind.
536  llvm::PointerIntPair<FunctionTemplateDecl *, 2> Template;
537 
538  /// \brief The template arguments used to produce the function template
539  /// specialization from the function template.
541 
542  /// \brief The template arguments as written in the sources, if provided.
544 
545  /// \brief The point at which this function template specialization was
546  /// first instantiated.
548 
549  /// \brief Retrieve the template from which this function was specialized.
550  FunctionTemplateDecl *getTemplate() const { return Template.getPointer(); }
551 
552  /// \brief Determine what kind of template specialization this is.
554  return (TemplateSpecializationKind)(Template.getInt() + 1);
555  }
556 
559  }
560 
561  /// \brief True if this declaration is an explicit specialization,
562  /// explicit instantiation declaration, or explicit instantiation
563  /// definition.
567  }
568 
569  /// \brief Set the template specialization kind.
571  assert(TSK != TSK_Undeclared &&
572  "Cannot encode TSK_Undeclared for a function template specialization");
573  Template.setInt(TSK - 1);
574  }
575 
576  /// \brief Retrieve the first point of instantiation of this function
577  /// template specialization.
578  ///
579  /// The point of instantiation may be an invalid source location if this
580  /// function has yet to be instantiated.
582  return PointOfInstantiation;
583  }
584 
585  /// \brief Set the (first) point of instantiation of this function template
586  /// specialization.
588  PointOfInstantiation = POI;
589  }
590 
591  void Profile(llvm::FoldingSetNodeID &ID) {
592  Profile(ID, TemplateArguments->asArray(),
593  Function->getASTContext());
594  }
595 
596  static void
597  Profile(llvm::FoldingSetNodeID &ID, ArrayRef<TemplateArgument> TemplateArgs,
598  ASTContext &Context) {
599  ID.AddInteger(TemplateArgs.size());
600  for (const TemplateArgument &TemplateArg : TemplateArgs)
601  TemplateArg.Profile(ID, Context);
602  }
603 };
604 
605 /// \brief Provides information a specialization of a member of a class
606 /// template, which may be a member function, static data member,
607 /// member class or member enumeration.
609  // The member declaration from which this member was instantiated, and the
610  // manner in which the instantiation occurred (in the lower two bits).
611  llvm::PointerIntPair<NamedDecl *, 2> MemberAndTSK;
612 
613  // The point at which this member was first instantiated.
614  SourceLocation PointOfInstantiation;
615 
616 public:
617  explicit
620  : MemberAndTSK(IF, TSK - 1), PointOfInstantiation(POI) {
621  assert(TSK != TSK_Undeclared &&
622  "Cannot encode undeclared template specializations for members");
623  }
624 
625  /// \brief Retrieve the member declaration from which this member was
626  /// instantiated.
627  NamedDecl *getInstantiatedFrom() const { return MemberAndTSK.getPointer(); }
628 
629  /// \brief Determine what kind of template specialization this is.
631  return (TemplateSpecializationKind)(MemberAndTSK.getInt() + 1);
632  }
633 
636  }
637 
638  /// \brief Set the template specialization kind.
640  assert(TSK != TSK_Undeclared &&
641  "Cannot encode undeclared template specializations for members");
642  MemberAndTSK.setInt(TSK - 1);
643  }
644 
645  /// \brief Retrieve the first point of instantiation of this member.
646  /// If the point of instantiation is an invalid location, then this member
647  /// has not yet been instantiated.
649  return PointOfInstantiation;
650  }
651 
652  /// \brief Set the first point of instantiation.
654  PointOfInstantiation = POI;
655  }
656 };
657 
658 /// \brief Provides information about a dependent function-template
659 /// specialization declaration.
660 ///
661 /// Since explicit function template specialization and instantiation
662 /// declarations can only appear in namespace scope, and you can only
663 /// specialize a member of a fully-specialized class, the only way to
664 /// get one of these is in a friend declaration like the following:
665 ///
666 /// \code
667 /// template <class T> void foo(T);
668 /// template <class T> class A {
669 /// friend void foo<>(T);
670 /// };
671 /// \endcode
673  : private llvm::TrailingObjects<DependentFunctionTemplateSpecializationInfo,
674  TemplateArgumentLoc,
675  FunctionTemplateDecl *> {
676  /// The number of potential template candidates.
677  unsigned NumTemplates;
678 
679  /// The number of template arguments.
680  unsigned NumArgs;
681 
682  /// The locations of the left and right angle brackets.
683  SourceRange AngleLocs;
684 
685  size_t numTrailingObjects(OverloadToken<TemplateArgumentLoc>) const {
686  return NumArgs;
687  }
688  size_t numTrailingObjects(OverloadToken<FunctionTemplateDecl *>) const {
689  return NumTemplates;
690  }
691 
693  const UnresolvedSetImpl &Templates,
694  const TemplateArgumentListInfo &TemplateArgs);
695 
696 public:
698 
700  Create(ASTContext &Context, const UnresolvedSetImpl &Templates,
701  const TemplateArgumentListInfo &TemplateArgs);
702 
703  /// \brief Returns the number of function templates that this might
704  /// be a specialization of.
705  unsigned getNumTemplates() const { return NumTemplates; }
706 
707  /// \brief Returns the i'th template candidate.
708  FunctionTemplateDecl *getTemplate(unsigned I) const {
709  assert(I < getNumTemplates() && "template index out of range");
710  return getTrailingObjects<FunctionTemplateDecl *>()[I];
711  }
712 
713  /// \brief Returns the explicit template arguments that were given.
715  return getTrailingObjects<TemplateArgumentLoc>();
716  }
717 
718  /// \brief Returns the number of explicit template arguments that were given.
719  unsigned getNumTemplateArgs() const { return NumArgs; }
720 
721  /// \brief Returns the nth template argument.
722  const TemplateArgumentLoc &getTemplateArg(unsigned I) const {
723  assert(I < getNumTemplateArgs() && "template arg index out of range");
724  return getTemplateArgs()[I];
725  }
726 
728  return AngleLocs.getBegin();
729  }
730 
732  return AngleLocs.getEnd();
733  }
734 };
735 
736 /// Declaration of a redeclarable template.
738  public Redeclarable<RedeclarableTemplateDecl>
739 {
741 
742  RedeclarableTemplateDecl *getNextRedeclarationImpl() override {
743  return getNextRedeclaration();
744  }
745 
746  RedeclarableTemplateDecl *getPreviousDeclImpl() override {
747  return getPreviousDecl();
748  }
749 
750  RedeclarableTemplateDecl *getMostRecentDeclImpl() override {
751  return getMostRecentDecl();
752  }
753 
754 protected:
755  template <typename EntryType> struct SpecEntryTraits {
756  using DeclType = EntryType;
757 
758  static DeclType *getDecl(EntryType *D) {
759  return D;
760  }
761 
763  return D->getTemplateArgs().asArray();
764  }
765  };
766 
767  template <typename EntryType, typename SETraits = SpecEntryTraits<EntryType>,
768  typename DeclType = typename SETraits::DeclType>
770  : llvm::iterator_adaptor_base<
771  SpecIterator<EntryType, SETraits, DeclType>,
772  typename llvm::FoldingSetVector<EntryType>::iterator,
773  typename std::iterator_traits<typename llvm::FoldingSetVector<
774  EntryType>::iterator>::iterator_category,
775  DeclType *, ptrdiff_t, DeclType *, DeclType *> {
776  SpecIterator() = default;
777  explicit SpecIterator(
778  typename llvm::FoldingSetVector<EntryType>::iterator SetIter)
779  : SpecIterator::iterator_adaptor_base(std::move(SetIter)) {}
780 
781  DeclType *operator*() const {
782  return SETraits::getDecl(&*this->I)->getMostRecentDecl();
783  }
784 
785  DeclType *operator->() const { return **this; }
786  };
787 
788  template <typename EntryType>
790  makeSpecIterator(llvm::FoldingSetVector<EntryType> &Specs, bool isEnd) {
791  return SpecIterator<EntryType>(isEnd ? Specs.end() : Specs.begin());
792  }
793 
794  void loadLazySpecializationsImpl() const;
795 
796  template <class EntryType> typename SpecEntryTraits<EntryType>::DeclType*
797  findSpecializationImpl(llvm::FoldingSetVector<EntryType> &Specs,
798  ArrayRef<TemplateArgument> Args, void *&InsertPos);
799 
800  template <class Derived, class EntryType>
801  void addSpecializationImpl(llvm::FoldingSetVector<EntryType> &Specs,
802  EntryType *Entry, void *InsertPos);
803 
804  struct CommonBase {
805  CommonBase() : InstantiatedFromMember(nullptr, false) {}
806 
807  /// \brief The template from which this was most
808  /// directly instantiated (or null).
809  ///
810  /// The boolean value indicates whether this template
811  /// was explicitly specialized.
812  llvm::PointerIntPair<RedeclarableTemplateDecl*, 1, bool>
814 
815  /// \brief If non-null, points to an array of specializations (including
816  /// partial specializations) known only by their external declaration IDs.
817  ///
818  /// The first value in the array is the number of specializations/partial
819  /// specializations that follow.
820  uint32_t *LazySpecializations = nullptr;
821  };
822 
823  /// \brief Pointer to the common data shared by all declarations of this
824  /// template.
825  mutable CommonBase *Common = nullptr;
826 
827  /// \brief Retrieves the "common" pointer shared by all (re-)declarations of
828  /// the same template. Calling this routine may implicitly allocate memory
829  /// for the common pointer.
830  CommonBase *getCommonPtr() const;
831 
832  virtual CommonBase *newCommon(ASTContext &C) const = 0;
833 
834  // Construct a template decl with name, parameters, and templated element.
838  NamedDecl *Decl)
839  : TemplateDecl(CTDI, DK, DC, L, Name, Params, Decl), redeclarable_base(C)
840  {}
841 
845  : RedeclarableTemplateDecl(nullptr, DK, C, DC, L, Name, Params, Decl) {}
846 
847 public:
848  friend class ASTDeclReader;
849  friend class ASTDeclWriter;
850  friend class ASTReader;
851  template <class decl_type> friend class RedeclarableTemplate;
852 
853  /// \brief Retrieves the canonical declaration of this template.
855  return getFirstDecl();
856  }
858  return getFirstDecl();
859  }
860 
861  /// \brief Determines whether this template was a specialization of a
862  /// member template.
863  ///
864  /// In the following example, the function template \c X<int>::f and the
865  /// member template \c X<int>::Inner are member specializations.
866  ///
867  /// \code
868  /// template<typename T>
869  /// struct X {
870  /// template<typename U> void f(T, U);
871  /// template<typename U> struct Inner;
872  /// };
873  ///
874  /// template<> template<typename T>
875  /// void X<int>::f(int, T);
876  /// template<> template<typename T>
877  /// struct X<int>::Inner { /* ... */ };
878  /// \endcode
879  bool isMemberSpecialization() const {
880  return getCommonPtr()->InstantiatedFromMember.getInt();
881  }
882 
883  /// \brief Note that this member template is a specialization.
885  assert(getCommonPtr()->InstantiatedFromMember.getPointer() &&
886  "Only member templates can be member template specializations");
887  getCommonPtr()->InstantiatedFromMember.setInt(true);
888  }
889 
890  /// \brief Retrieve the member template from which this template was
891  /// instantiated, or nullptr if this template was not instantiated from a
892  /// member template.
893  ///
894  /// A template is instantiated from a member template when the member
895  /// template itself is part of a class template (or member thereof). For
896  /// example, given
897  ///
898  /// \code
899  /// template<typename T>
900  /// struct X {
901  /// template<typename U> void f(T, U);
902  /// };
903  ///
904  /// void test(X<int> x) {
905  /// x.f(1, 'a');
906  /// };
907  /// \endcode
908  ///
909  /// \c X<int>::f is a FunctionTemplateDecl that describes the function
910  /// template
911  ///
912  /// \code
913  /// template<typename U> void X<int>::f(int, U);
914  /// \endcode
915  ///
916  /// which was itself created during the instantiation of \c X<int>. Calling
917  /// getInstantiatedFromMemberTemplate() on this FunctionTemplateDecl will
918  /// retrieve the FunctionTemplateDecl for the original template \c f within
919  /// the class template \c X<T>, i.e.,
920  ///
921  /// \code
922  /// template<typename T>
923  /// template<typename U>
924  /// void X<T>::f(T, U);
925  /// \endcode
927  return getCommonPtr()->InstantiatedFromMember.getPointer();
928  }
929 
931  assert(!getCommonPtr()->InstantiatedFromMember.getPointer());
932  getCommonPtr()->InstantiatedFromMember.setPointer(TD);
933  }
934 
936  using redecl_iterator = redeclarable_base::redecl_iterator;
937 
938  using redeclarable_base::redecls_begin;
939  using redeclarable_base::redecls_end;
940  using redeclarable_base::redecls;
941  using redeclarable_base::getPreviousDecl;
942  using redeclarable_base::getMostRecentDecl;
943  using redeclarable_base::isFirstDecl;
944 
945  // Implement isa/cast/dyncast/etc.
946  static bool classof(const Decl *D) { return classofKind(D->getKind()); }
947 
948  static bool classofKind(Kind K) {
949  return K >= firstRedeclarableTemplate && K <= lastRedeclarableTemplate;
950  }
951 };
952 
953 template <> struct RedeclarableTemplateDecl::
954 SpecEntryTraits<FunctionTemplateSpecializationInfo> {
956 
958  return I->Function;
959  }
960 
963  return I->TemplateArguments->asArray();
964  }
965 };
966 
967 /// Declaration of a template function.
969 protected:
970  friend class FunctionDecl;
971 
972  /// \brief Data that is common to all of the declarations of a given
973  /// function template.
974  struct Common : CommonBase {
975  /// \brief The function template specializations for this function
976  /// template, including explicit specializations and instantiations.
977  llvm::FoldingSetVector<FunctionTemplateSpecializationInfo> Specializations;
978 
979  /// \brief The set of "injected" template arguments used within this
980  /// function template.
981  ///
982  /// This pointer refers to the template arguments (there are as
983  /// many template arguments as template parameaters) for the function
984  /// template, and is allocated lazily, since most function templates do not
985  /// require the use of this information.
986  TemplateArgument *InjectedArgs = nullptr;
987 
988  Common() = default;
989  };
990 
993  NamedDecl *Decl)
994  : RedeclarableTemplateDecl(FunctionTemplate, C, DC, L, Name, Params,
995  Decl) {}
996 
997  CommonBase *newCommon(ASTContext &C) const override;
998 
999  Common *getCommonPtr() const {
1000  return static_cast<Common *>(RedeclarableTemplateDecl::getCommonPtr());
1001  }
1002 
1003  /// \brief Retrieve the set of function template specializations of this
1004  /// function template.
1005  llvm::FoldingSetVector<FunctionTemplateSpecializationInfo> &
1006  getSpecializations() const;
1007 
1008  /// \brief Add a specialization of this function template.
1009  ///
1010  /// \param InsertPos Insert position in the FoldingSetVector, must have been
1011  /// retrieved by an earlier call to findSpecialization().
1012  void addSpecialization(FunctionTemplateSpecializationInfo* Info,
1013  void *InsertPos);
1014 
1015 public:
1016  friend class ASTDeclReader;
1017  friend class ASTDeclWriter;
1018 
1019  /// \brief Load any lazily-loaded specializations from the external source.
1020  void LoadLazySpecializations() const;
1021 
1022  /// Get the underlying function declaration of the template.
1024  return static_cast<FunctionDecl *>(TemplatedDecl);
1025  }
1026 
1027  /// Returns whether this template declaration defines the primary
1028  /// pattern.
1030  return getTemplatedDecl()->isThisDeclarationADefinition();
1031  }
1032 
1033  /// \brief Return the specialization with the provided arguments if it exists,
1034  /// otherwise return the insertion point.
1035  FunctionDecl *findSpecialization(ArrayRef<TemplateArgument> Args,
1036  void *&InsertPos);
1037 
1039  return cast<FunctionTemplateDecl>(
1041  }
1043  return cast<FunctionTemplateDecl>(
1045  }
1046 
1047  /// \brief Retrieve the previous declaration of this function template, or
1048  /// nullptr if no such declaration exists.
1050  return cast_or_null<FunctionTemplateDecl>(
1051  static_cast<RedeclarableTemplateDecl *>(this)->getPreviousDecl());
1052  }
1054  return cast_or_null<FunctionTemplateDecl>(
1055  static_cast<const RedeclarableTemplateDecl *>(this)->getPreviousDecl());
1056  }
1057 
1059  return cast<FunctionTemplateDecl>(
1060  static_cast<RedeclarableTemplateDecl *>(this)
1061  ->getMostRecentDecl());
1062  }
1064  return const_cast<FunctionTemplateDecl*>(this)->getMostRecentDecl();
1065  }
1066 
1068  return cast_or_null<FunctionTemplateDecl>(
1070  }
1071 
1073  using spec_range = llvm::iterator_range<spec_iterator>;
1074 
1076  return spec_range(spec_begin(), spec_end());
1077  }
1078 
1080  return makeSpecIterator(getSpecializations(), false);
1081  }
1082 
1084  return makeSpecIterator(getSpecializations(), true);
1085  }
1086 
1087  /// \brief Retrieve the "injected" template arguments that correspond to the
1088  /// template parameters of this function template.
1089  ///
1090  /// Although the C++ standard has no notion of the "injected" template
1091  /// arguments for a function template, the notion is convenient when
1092  /// we need to perform substitutions inside the definition of a function
1093  /// template.
1094  ArrayRef<TemplateArgument> getInjectedTemplateArgs();
1095 
1096  /// \brief Create a function template node.
1098  SourceLocation L,
1099  DeclarationName Name,
1100  TemplateParameterList *Params,
1101  NamedDecl *Decl);
1102 
1103  /// \brief Create an empty function template node.
1104  static FunctionTemplateDecl *CreateDeserialized(ASTContext &C, unsigned ID);
1105 
1106  // Implement isa/cast/dyncast support
1107  static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1108  static bool classofKind(Kind K) { return K == FunctionTemplate; }
1109 };
1110 
1111 //===----------------------------------------------------------------------===//
1112 // Kinds of Template Parameters
1113 //===----------------------------------------------------------------------===//
1114 
1115 /// \brief Defines the position of a template parameter within a template
1116 /// parameter list.
1117 ///
1118 /// Because template parameter can be listed
1119 /// sequentially for out-of-line template members, each template parameter is
1120 /// given a Depth - the nesting of template parameter scopes - and a Position -
1121 /// the occurrence within the parameter list.
1122 /// This class is inheritedly privately by different kinds of template
1123 /// parameters and is not part of the Decl hierarchy. Just a facility.
1125 protected:
1126  // FIXME: These probably don't need to be ints. int:5 for depth, int:8 for
1127  // position? Maybe?
1128  unsigned Depth;
1129  unsigned Position;
1130 
1131  TemplateParmPosition(unsigned D, unsigned P) : Depth(D), Position(P) {}
1132 
1133 public:
1134  TemplateParmPosition() = delete;
1135 
1136  /// Get the nesting depth of the template parameter.
1137  unsigned getDepth() const { return Depth; }
1138  void setDepth(unsigned D) { Depth = D; }
1139 
1140  /// Get the position of the template parameter within its parameter list.
1141  unsigned getPosition() const { return Position; }
1142  void setPosition(unsigned P) { Position = P; }
1143 
1144  /// Get the index of the template parameter within its parameter list.
1145  unsigned getIndex() const { return Position; }
1146 };
1147 
1148 /// \brief Declaration of a template type parameter.
1149 ///
1150 /// For example, "T" in
1151 /// \code
1152 /// template<typename T> class vector;
1153 /// \endcode
1154 class TemplateTypeParmDecl : public TypeDecl {
1155  /// Sema creates these on the stack during auto type deduction.
1156  friend class Sema;
1157 
1158  /// \brief Whether this template type parameter was declaration with
1159  /// the 'typename' keyword.
1160  ///
1161  /// If false, it was declared with the 'class' keyword.
1162  bool Typename : 1;
1163 
1164  /// \brief The default template argument, if any.
1165  using DefArgStorage =
1167  DefArgStorage DefaultArgument;
1168 
1169  TemplateTypeParmDecl(DeclContext *DC, SourceLocation KeyLoc,
1171  bool Typename)
1172  : TypeDecl(TemplateTypeParm, DC, IdLoc, Id, KeyLoc), Typename(Typename) {}
1173 
1174 public:
1175  static TemplateTypeParmDecl *Create(const ASTContext &C, DeclContext *DC,
1176  SourceLocation KeyLoc,
1177  SourceLocation NameLoc,
1178  unsigned D, unsigned P,
1179  IdentifierInfo *Id, bool Typename,
1180  bool ParameterPack);
1181  static TemplateTypeParmDecl *CreateDeserialized(const ASTContext &C,
1182  unsigned ID);
1183 
1184  /// \brief Whether this template type parameter was declared with
1185  /// the 'typename' keyword.
1186  ///
1187  /// If not, it was declared with the 'class' keyword.
1188  bool wasDeclaredWithTypename() const { return Typename; }
1189 
1190  const DefArgStorage &getDefaultArgStorage() const { return DefaultArgument; }
1191 
1192  /// \brief Determine whether this template parameter has a default
1193  /// argument.
1194  bool hasDefaultArgument() const { return DefaultArgument.isSet(); }
1195 
1196  /// \brief Retrieve the default argument, if any.
1198  return DefaultArgument.get()->getType();
1199  }
1200 
1201  /// \brief Retrieves the default argument's source information, if any.
1203  return DefaultArgument.get();
1204  }
1205 
1206  /// \brief Retrieves the location of the default argument declaration.
1207  SourceLocation getDefaultArgumentLoc() const;
1208 
1209  /// \brief Determines whether the default argument was inherited
1210  /// from a previous declaration of this template.
1212  return DefaultArgument.isInherited();
1213  }
1214 
1215  /// \brief Set the default argument for this template parameter.
1217  DefaultArgument.set(DefArg);
1218  }
1219 
1220  /// \brief Set that this default argument was inherited from another
1221  /// parameter.
1223  TemplateTypeParmDecl *Prev) {
1224  DefaultArgument.setInherited(C, Prev);
1225  }
1226 
1227  /// \brief Removes the default argument of this template parameter.
1229  DefaultArgument.clear();
1230  }
1231 
1232  /// \brief Set whether this template type parameter was declared with
1233  /// the 'typename' or 'class' keyword.
1234  void setDeclaredWithTypename(bool withTypename) { Typename = withTypename; }
1235 
1236  /// \brief Retrieve the depth of the template parameter.
1237  unsigned getDepth() const;
1238 
1239  /// \brief Retrieve the index of the template parameter.
1240  unsigned getIndex() const;
1241 
1242  /// \brief Returns whether this is a parameter pack.
1243  bool isParameterPack() const;
1244 
1245  SourceRange getSourceRange() const override LLVM_READONLY;
1246 
1247  // Implement isa/cast/dyncast/etc.
1248  static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1249  static bool classofKind(Kind K) { return K == TemplateTypeParm; }
1250 };
1251 
1252 /// NonTypeTemplateParmDecl - Declares a non-type template parameter,
1253 /// e.g., "Size" in
1254 /// @code
1255 /// template<int Size> class array { };
1256 /// @endcode
1257 class NonTypeTemplateParmDecl final
1258  : public DeclaratorDecl,
1259  protected TemplateParmPosition,
1260  private llvm::TrailingObjects<NonTypeTemplateParmDecl,
1261  std::pair<QualType, TypeSourceInfo *>> {
1262  friend class ASTDeclReader;
1263  friend TrailingObjects;
1264 
1265  /// \brief The default template argument, if any, and whether or not
1266  /// it was inherited.
1268  DefArgStorage DefaultArgument;
1269 
1270  // FIXME: Collapse this into TemplateParamPosition; or, just move depth/index
1271  // down here to save memory.
1272 
1273  /// \brief Whether this non-type template parameter is a parameter pack.
1274  bool ParameterPack;
1275 
1276  /// \brief Whether this non-type template parameter is an "expanded"
1277  /// parameter pack, meaning that its type is a pack expansion and we
1278  /// already know the set of types that expansion expands to.
1279  bool ExpandedParameterPack = false;
1280 
1281  /// \brief The number of types in an expanded parameter pack.
1282  unsigned NumExpandedTypes = 0;
1283 
1284  size_t numTrailingObjects(
1285  OverloadToken<std::pair<QualType, TypeSourceInfo *>>) const {
1286  return NumExpandedTypes;
1287  }
1288 
1289  NonTypeTemplateParmDecl(DeclContext *DC, SourceLocation StartLoc,
1290  SourceLocation IdLoc, unsigned D, unsigned P,
1292  bool ParameterPack, TypeSourceInfo *TInfo)
1293  : DeclaratorDecl(NonTypeTemplateParm, DC, IdLoc, Id, T, TInfo, StartLoc),
1294  TemplateParmPosition(D, P), ParameterPack(ParameterPack) {}
1295 
1296  NonTypeTemplateParmDecl(DeclContext *DC, SourceLocation StartLoc,
1297  SourceLocation IdLoc, unsigned D, unsigned P,
1299  TypeSourceInfo *TInfo,
1300  ArrayRef<QualType> ExpandedTypes,
1301  ArrayRef<TypeSourceInfo *> ExpandedTInfos);
1302 
1303 public:
1304  static NonTypeTemplateParmDecl *
1305  Create(const ASTContext &C, DeclContext *DC, SourceLocation StartLoc,
1306  SourceLocation IdLoc, unsigned D, unsigned P, IdentifierInfo *Id,
1307  QualType T, bool ParameterPack, TypeSourceInfo *TInfo);
1308 
1309  static NonTypeTemplateParmDecl *
1310  Create(const ASTContext &C, DeclContext *DC, SourceLocation StartLoc,
1311  SourceLocation IdLoc, unsigned D, unsigned P, IdentifierInfo *Id,
1312  QualType T, TypeSourceInfo *TInfo, ArrayRef<QualType> ExpandedTypes,
1313  ArrayRef<TypeSourceInfo *> ExpandedTInfos);
1314 
1315  static NonTypeTemplateParmDecl *CreateDeserialized(ASTContext &C,
1316  unsigned ID);
1317  static NonTypeTemplateParmDecl *CreateDeserialized(ASTContext &C,
1318  unsigned ID,
1319  unsigned NumExpandedTypes);
1320 
1326 
1327  SourceRange getSourceRange() const override LLVM_READONLY;
1328 
1329  const DefArgStorage &getDefaultArgStorage() const { return DefaultArgument; }
1330 
1331  /// \brief Determine whether this template parameter has a default
1332  /// argument.
1333  bool hasDefaultArgument() const { return DefaultArgument.isSet(); }
1334 
1335  /// \brief Retrieve the default argument, if any.
1336  Expr *getDefaultArgument() const { return DefaultArgument.get(); }
1337 
1338  /// \brief Retrieve the location of the default argument, if any.
1339  SourceLocation getDefaultArgumentLoc() const;
1340 
1341  /// \brief Determines whether the default argument was inherited
1342  /// from a previous declaration of this template.
1344  return DefaultArgument.isInherited();
1345  }
1346 
1347  /// \brief Set the default argument for this template parameter, and
1348  /// whether that default argument was inherited from another
1349  /// declaration.
1350  void setDefaultArgument(Expr *DefArg) { DefaultArgument.set(DefArg); }
1352  NonTypeTemplateParmDecl *Parm) {
1353  DefaultArgument.setInherited(C, Parm);
1354  }
1355 
1356  /// \brief Removes the default argument of this template parameter.
1357  void removeDefaultArgument() { DefaultArgument.clear(); }
1358 
1359  /// \brief Whether this parameter is a non-type template parameter pack.
1360  ///
1361  /// If the parameter is a parameter pack, the type may be a
1362  /// \c PackExpansionType. In the following example, the \c Dims parameter
1363  /// is a parameter pack (whose type is 'unsigned').
1364  ///
1365  /// \code
1366  /// template<typename T, unsigned ...Dims> struct multi_array;
1367  /// \endcode
1368  bool isParameterPack() const { return ParameterPack; }
1369 
1370  /// \brief Whether this parameter pack is a pack expansion.
1371  ///
1372  /// A non-type template parameter pack is a pack expansion if its type
1373  /// contains an unexpanded parameter pack. In this case, we will have
1374  /// built a PackExpansionType wrapping the type.
1375  bool isPackExpansion() const {
1376  return ParameterPack && getType()->getAs<PackExpansionType>();
1377  }
1378 
1379  /// \brief Whether this parameter is a non-type template parameter pack
1380  /// that has a known list of different types at different positions.
1381  ///
1382  /// A parameter pack is an expanded parameter pack when the original
1383  /// parameter pack's type was itself a pack expansion, and that expansion
1384  /// has already been expanded. For example, given:
1385  ///
1386  /// \code
1387  /// template<typename ...Types>
1388  /// struct X {
1389  /// template<Types ...Values>
1390  /// struct Y { /* ... */ };
1391  /// };
1392  /// \endcode
1393  ///
1394  /// The parameter pack \c Values has a \c PackExpansionType as its type,
1395  /// which expands \c Types. When \c Types is supplied with template arguments
1396  /// by instantiating \c X, the instantiation of \c Values becomes an
1397  /// expanded parameter pack. For example, instantiating
1398  /// \c X<int, unsigned int> results in \c Values being an expanded parameter
1399  /// pack with expansion types \c int and \c unsigned int.
1400  ///
1401  /// The \c getExpansionType() and \c getExpansionTypeSourceInfo() functions
1402  /// return the expansion types.
1403  bool isExpandedParameterPack() const { return ExpandedParameterPack; }
1404 
1405  /// \brief Retrieves the number of expansion types in an expanded parameter
1406  /// pack.
1407  unsigned getNumExpansionTypes() const {
1408  assert(ExpandedParameterPack && "Not an expansion parameter pack");
1409  return NumExpandedTypes;
1410  }
1411 
1412  /// \brief Retrieve a particular expansion type within an expanded parameter
1413  /// pack.
1414  QualType getExpansionType(unsigned I) const {
1415  assert(I < NumExpandedTypes && "Out-of-range expansion type index");
1416  auto TypesAndInfos =
1417  getTrailingObjects<std::pair<QualType, TypeSourceInfo *>>();
1418  return TypesAndInfos[I].first;
1419  }
1420 
1421  /// \brief Retrieve a particular expansion type source info within an
1422  /// expanded parameter pack.
1424  assert(I < NumExpandedTypes && "Out-of-range expansion type index");
1425  auto TypesAndInfos =
1426  getTrailingObjects<std::pair<QualType, TypeSourceInfo *>>();
1427  return TypesAndInfos[I].second;
1428  }
1429 
1430  // Implement isa/cast/dyncast/etc.
1431  static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1432  static bool classofKind(Kind K) { return K == NonTypeTemplateParm; }
1433 };
1434 
1435 /// TemplateTemplateParmDecl - Declares a template template parameter,
1436 /// e.g., "T" in
1437 /// @code
1438 /// template <template <typename> class T> class container { };
1439 /// @endcode
1440 /// A template template parameter is a TemplateDecl because it defines the
1441 /// name of a template and the template parameters allowable for substitution.
1443  : public TemplateDecl,
1444  protected TemplateParmPosition,
1445  private llvm::TrailingObjects<TemplateTemplateParmDecl,
1446  TemplateParameterList *> {
1447  /// \brief The default template argument, if any.
1448  using DefArgStorage =
1450  DefArgStorage DefaultArgument;
1451 
1452  /// \brief Whether this parameter is a parameter pack.
1453  bool ParameterPack;
1454 
1455  /// \brief Whether this template template parameter is an "expanded"
1456  /// parameter pack, meaning that it is a pack expansion and we
1457  /// already know the set of template parameters that expansion expands to.
1458  bool ExpandedParameterPack = false;
1459 
1460  /// \brief The number of parameters in an expanded parameter pack.
1461  unsigned NumExpandedParams = 0;
1462 
1464  unsigned D, unsigned P, bool ParameterPack,
1466  : TemplateDecl(TemplateTemplateParm, DC, L, Id, Params),
1467  TemplateParmPosition(D, P), ParameterPack(ParameterPack) {}
1468 
1470  unsigned D, unsigned P,
1473 
1474  void anchor() override;
1475 
1476 public:
1477  friend class ASTDeclReader;
1478  friend class ASTDeclWriter;
1480 
1481  static TemplateTemplateParmDecl *Create(const ASTContext &C, DeclContext *DC,
1482  SourceLocation L, unsigned D,
1483  unsigned P, bool ParameterPack,
1484  IdentifierInfo *Id,
1485  TemplateParameterList *Params);
1486  static TemplateTemplateParmDecl *Create(const ASTContext &C, DeclContext *DC,
1487  SourceLocation L, unsigned D,
1488  unsigned P,
1489  IdentifierInfo *Id,
1490  TemplateParameterList *Params,
1492 
1493  static TemplateTemplateParmDecl *CreateDeserialized(ASTContext &C,
1494  unsigned ID);
1495  static TemplateTemplateParmDecl *CreateDeserialized(ASTContext &C,
1496  unsigned ID,
1497  unsigned NumExpansions);
1498 
1504 
1505  /// \brief Whether this template template parameter is a template
1506  /// parameter pack.
1507  ///
1508  /// \code
1509  /// template<template <class T> ...MetaFunctions> struct Apply;
1510  /// \endcode
1511  bool isParameterPack() const { return ParameterPack; }
1512 
1513  /// \brief Whether this parameter pack is a pack expansion.
1514  ///
1515  /// A template template parameter pack is a pack expansion if its template
1516  /// parameter list contains an unexpanded parameter pack.
1517  bool isPackExpansion() const {
1518  return ParameterPack &&
1519  getTemplateParameters()->containsUnexpandedParameterPack();
1520  }
1521 
1522  /// \brief Whether this parameter is a template template parameter pack that
1523  /// has a known list of different template parameter lists at different
1524  /// positions.
1525  ///
1526  /// A parameter pack is an expanded parameter pack when the original parameter
1527  /// pack's template parameter list was itself a pack expansion, and that
1528  /// expansion has already been expanded. For exampe, given:
1529  ///
1530  /// \code
1531  /// template<typename...Types> struct Outer {
1532  /// template<template<Types> class...Templates> struct Inner;
1533  /// };
1534  /// \endcode
1535  ///
1536  /// The parameter pack \c Templates is a pack expansion, which expands the
1537  /// pack \c Types. When \c Types is supplied with template arguments by
1538  /// instantiating \c Outer, the instantiation of \c Templates is an expanded
1539  /// parameter pack.
1540  bool isExpandedParameterPack() const { return ExpandedParameterPack; }
1541 
1542  /// \brief Retrieves the number of expansion template parameters in
1543  /// an expanded parameter pack.
1545  assert(ExpandedParameterPack && "Not an expansion parameter pack");
1546  return NumExpandedParams;
1547  }
1548 
1549  /// \brief Retrieve a particular expansion type within an expanded parameter
1550  /// pack.
1552  assert(I < NumExpandedParams && "Out-of-range expansion type index");
1553  return getTrailingObjects<TemplateParameterList *>()[I];
1554  }
1555 
1556  const DefArgStorage &getDefaultArgStorage() const { return DefaultArgument; }
1557 
1558  /// \brief Determine whether this template parameter has a default
1559  /// argument.
1560  bool hasDefaultArgument() const { return DefaultArgument.isSet(); }
1561 
1562  /// \brief Retrieve the default argument, if any.
1564  static const TemplateArgumentLoc None;
1565  return DefaultArgument.isSet() ? *DefaultArgument.get() : None;
1566  }
1567 
1568  /// \brief Retrieve the location of the default argument, if any.
1569  SourceLocation getDefaultArgumentLoc() const;
1570 
1571  /// \brief Determines whether the default argument was inherited
1572  /// from a previous declaration of this template.
1574  return DefaultArgument.isInherited();
1575  }
1576 
1577  /// \brief Set the default argument for this template parameter, and
1578  /// whether that default argument was inherited from another
1579  /// declaration.
1580  void setDefaultArgument(const ASTContext &C,
1581  const TemplateArgumentLoc &DefArg);
1583  TemplateTemplateParmDecl *Prev) {
1584  DefaultArgument.setInherited(C, Prev);
1585  }
1586 
1587  /// \brief Removes the default argument of this template parameter.
1588  void removeDefaultArgument() { DefaultArgument.clear(); }
1589 
1590  SourceRange getSourceRange() const override LLVM_READONLY {
1591  SourceLocation End = getLocation();
1592  if (hasDefaultArgument() && !defaultArgumentWasInherited())
1593  End = getDefaultArgument().getSourceRange().getEnd();
1594  return SourceRange(getTemplateParameters()->getTemplateLoc(), End);
1595  }
1596 
1597  // Implement isa/cast/dyncast/etc.
1598  static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1599  static bool classofKind(Kind K) { return K == TemplateTemplateParm; }
1600 };
1601 
1602 /// \brief Represents the builtin template declaration which is used to
1603 /// implement __make_integer_seq and other builtin templates. It serves
1604 /// no real purpose beyond existing as a place to hold template parameters.
1606  BuiltinTemplateKind BTK;
1607 
1610 
1611  void anchor() override;
1612 
1613 public:
1614  // Implement isa/cast/dyncast support
1615  static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1616  static bool classofKind(Kind K) { return K == BuiltinTemplate; }
1617 
1619  DeclarationName Name,
1620  BuiltinTemplateKind BTK) {
1621  return new (C, DC) BuiltinTemplateDecl(C, DC, Name, BTK);
1622  }
1623 
1624  SourceRange getSourceRange() const override LLVM_READONLY {
1625  return {};
1626  }
1627 
1629 };
1630 
1631 /// \brief Represents a class template specialization, which refers to
1632 /// a class template with a given set of template arguments.
1633 ///
1634 /// Class template specializations represent both explicit
1635 /// specialization of class templates, as in the example below, and
1636 /// implicit instantiations of class templates.
1637 ///
1638 /// \code
1639 /// template<typename T> class array;
1640 ///
1641 /// template<>
1642 /// class array<bool> { }; // class template specialization array<bool>
1643 /// \endcode
1645  : public CXXRecordDecl, public llvm::FoldingSetNode {
1646  /// \brief Structure that stores information about a class template
1647  /// specialization that was instantiated from a class template partial
1648  /// specialization.
1649  struct SpecializedPartialSpecialization {
1650  /// \brief The class template partial specialization from which this
1651  /// class template specialization was instantiated.
1652  ClassTemplatePartialSpecializationDecl *PartialSpecialization;
1653 
1654  /// \brief The template argument list deduced for the class template
1655  /// partial specialization itself.
1656  const TemplateArgumentList *TemplateArgs;
1657  };
1658 
1659  /// \brief The template that this specialization specializes
1660  llvm::PointerUnion<ClassTemplateDecl *, SpecializedPartialSpecialization *>
1661  SpecializedTemplate;
1662 
1663  /// \brief Further info for explicit template specialization/instantiation.
1664  struct ExplicitSpecializationInfo {
1665  /// \brief The type-as-written.
1666  TypeSourceInfo *TypeAsWritten = nullptr;
1667 
1668  /// \brief The location of the extern keyword.
1669  SourceLocation ExternLoc;
1670 
1671  /// \brief The location of the template keyword.
1672  SourceLocation TemplateKeywordLoc;
1673 
1674  ExplicitSpecializationInfo() = default;
1675  };
1676 
1677  /// \brief Further info for explicit template specialization/instantiation.
1678  /// Does not apply to implicit specializations.
1679  ExplicitSpecializationInfo *ExplicitInfo = nullptr;
1680 
1681  /// \brief The template arguments used to describe this specialization.
1682  const TemplateArgumentList *TemplateArgs;
1683 
1684  /// \brief The point where this template was instantiated (if any)
1685  SourceLocation PointOfInstantiation;
1686 
1687  /// \brief The kind of specialization this declaration refers to.
1688  /// Really a value of type TemplateSpecializationKind.
1689  unsigned SpecializationKind : 3;
1690 
1691 protected:
1693  DeclContext *DC, SourceLocation StartLoc,
1694  SourceLocation IdLoc,
1695  ClassTemplateDecl *SpecializedTemplate,
1698 
1700 
1701 public:
1702  friend class ASTDeclReader;
1703  friend class ASTDeclWriter;
1704 
1706  Create(ASTContext &Context, TagKind TK, DeclContext *DC,
1707  SourceLocation StartLoc, SourceLocation IdLoc,
1708  ClassTemplateDecl *SpecializedTemplate,
1712  CreateDeserialized(ASTContext &C, unsigned ID);
1713 
1714  void getNameForDiagnostic(raw_ostream &OS, const PrintingPolicy &Policy,
1715  bool Qualified) const override;
1716 
1717  // FIXME: This is broken. CXXRecordDecl::getMostRecentDecl() returns a
1718  // different "most recent" declaration from this function for the same
1719  // declaration, because we don't override getMostRecentDeclImpl(). But
1720  // it's not clear that we should override that, because the most recent
1721  // declaration as a CXXRecordDecl sometimes is the injected-class-name.
1723  CXXRecordDecl *Recent = static_cast<CXXRecordDecl *>(
1724  this)->getMostRecentDecl();
1725  while (!isa<ClassTemplateSpecializationDecl>(Recent)) {
1726  // FIXME: Does injected class name need to be in the redeclarations chain?
1727  assert(Recent->isInjectedClassName() && Recent->getPreviousDecl());
1728  Recent = Recent->getPreviousDecl();
1729  }
1730  return cast<ClassTemplateSpecializationDecl>(Recent);
1731  }
1732 
1733  /// \brief Retrieve the template that this specialization specializes.
1734  ClassTemplateDecl *getSpecializedTemplate() const;
1735 
1736  /// \brief Retrieve the template arguments of the class template
1737  /// specialization.
1739  return *TemplateArgs;
1740  }
1741 
1742  /// \brief Determine the kind of specialization that this
1743  /// declaration represents.
1745  return static_cast<TemplateSpecializationKind>(SpecializationKind);
1746  }
1747 
1749  return getSpecializationKind() == TSK_ExplicitSpecialization;
1750  }
1751 
1752  /// \brief True if this declaration is an explicit specialization,
1753  /// explicit instantiation declaration, or explicit instantiation
1754  /// definition.
1758  }
1759 
1761  SpecializationKind = TSK;
1762  }
1763 
1764  /// \brief Get the point of instantiation (if any), or null if none.
1766  return PointOfInstantiation;
1767  }
1768 
1770  assert(Loc.isValid() && "point of instantiation must be valid!");
1771  PointOfInstantiation = Loc;
1772  }
1773 
1774  /// \brief If this class template specialization is an instantiation of
1775  /// a template (rather than an explicit specialization), return the
1776  /// class template or class template partial specialization from which it
1777  /// was instantiated.
1778  llvm::PointerUnion<ClassTemplateDecl *,
1781  if (!isTemplateInstantiation(getSpecializationKind()))
1782  return llvm::PointerUnion<ClassTemplateDecl *,
1784 
1785  return getSpecializedTemplateOrPartial();
1786  }
1787 
1788  /// \brief Retrieve the class template or class template partial
1789  /// specialization which was specialized by this.
1790  llvm::PointerUnion<ClassTemplateDecl *,
1793  if (const auto *PartialSpec =
1794  SpecializedTemplate.dyn_cast<SpecializedPartialSpecialization *>())
1795  return PartialSpec->PartialSpecialization;
1796 
1797  return SpecializedTemplate.get<ClassTemplateDecl*>();
1798  }
1799 
1800  /// \brief Retrieve the set of template arguments that should be used
1801  /// to instantiate members of the class template or class template partial
1802  /// specialization from which this class template specialization was
1803  /// instantiated.
1804  ///
1805  /// \returns For a class template specialization instantiated from the primary
1806  /// template, this function will return the same template arguments as
1807  /// getTemplateArgs(). For a class template specialization instantiated from
1808  /// a class template partial specialization, this function will return the
1809  /// deduced template arguments for the class template partial specialization
1810  /// itself.
1812  if (const auto *PartialSpec =
1813  SpecializedTemplate.dyn_cast<SpecializedPartialSpecialization *>())
1814  return *PartialSpec->TemplateArgs;
1815 
1816  return getTemplateArgs();
1817  }
1818 
1819  /// \brief Note that this class template specialization is actually an
1820  /// instantiation of the given class template partial specialization whose
1821  /// template arguments have been deduced.
1823  const TemplateArgumentList *TemplateArgs) {
1824  assert(!SpecializedTemplate.is<SpecializedPartialSpecialization*>() &&
1825  "Already set to a class template partial specialization!");
1826  auto *PS = new (getASTContext()) SpecializedPartialSpecialization();
1827  PS->PartialSpecialization = PartialSpec;
1828  PS->TemplateArgs = TemplateArgs;
1829  SpecializedTemplate = PS;
1830  }
1831 
1832  /// \brief Note that this class template specialization is an instantiation
1833  /// of the given class template.
1834  void setInstantiationOf(ClassTemplateDecl *TemplDecl) {
1835  assert(!SpecializedTemplate.is<SpecializedPartialSpecialization*>() &&
1836  "Previously set to a class template partial specialization!");
1837  SpecializedTemplate = TemplDecl;
1838  }
1839 
1840  /// \brief Sets the type of this specialization as it was written by
1841  /// the user. This will be a class template specialization type.
1843  if (!ExplicitInfo)
1844  ExplicitInfo = new (getASTContext()) ExplicitSpecializationInfo;
1845  ExplicitInfo->TypeAsWritten = T;
1846  }
1847 
1848  /// \brief Gets the type of this specialization as it was written by
1849  /// the user, if it was so written.
1851  return ExplicitInfo ? ExplicitInfo->TypeAsWritten : nullptr;
1852  }
1853 
1854  /// \brief Gets the location of the extern keyword, if present.
1856  return ExplicitInfo ? ExplicitInfo->ExternLoc : SourceLocation();
1857  }
1858 
1859  /// \brief Sets the location of the extern keyword.
1861  if (!ExplicitInfo)
1862  ExplicitInfo = new (getASTContext()) ExplicitSpecializationInfo;
1863  ExplicitInfo->ExternLoc = Loc;
1864  }
1865 
1866  /// \brief Sets the location of the template keyword.
1868  if (!ExplicitInfo)
1869  ExplicitInfo = new (getASTContext()) ExplicitSpecializationInfo;
1870  ExplicitInfo->TemplateKeywordLoc = Loc;
1871  }
1872 
1873  /// \brief Gets the location of the template keyword, if present.
1875  return ExplicitInfo ? ExplicitInfo->TemplateKeywordLoc : SourceLocation();
1876  }
1877 
1878  SourceRange getSourceRange() const override LLVM_READONLY;
1879 
1880  void Profile(llvm::FoldingSetNodeID &ID) const {
1881  Profile(ID, TemplateArgs->asArray(), getASTContext());
1882  }
1883 
1884  static void
1885  Profile(llvm::FoldingSetNodeID &ID, ArrayRef<TemplateArgument> TemplateArgs,
1886  ASTContext &Context) {
1887  ID.AddInteger(TemplateArgs.size());
1888  for (const TemplateArgument &TemplateArg : TemplateArgs)
1889  TemplateArg.Profile(ID, Context);
1890  }
1891 
1892  static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1893 
1894  static bool classofKind(Kind K) {
1895  return K >= firstClassTemplateSpecialization &&
1896  K <= lastClassTemplateSpecialization;
1897  }
1898 };
1899 
1902  /// \brief The list of template parameters
1903  TemplateParameterList* TemplateParams = nullptr;
1904 
1905  /// \brief The source info for the template arguments as written.
1906  /// FIXME: redundant with TypeAsWritten?
1907  const ASTTemplateArgumentListInfo *ArgsAsWritten = nullptr;
1908 
1909  /// \brief The class template partial specialization from which this
1910  /// class template partial specialization was instantiated.
1911  ///
1912  /// The boolean value will be true to indicate that this class template
1913  /// partial specialization was specialized at this level.
1914  llvm::PointerIntPair<ClassTemplatePartialSpecializationDecl *, 1, bool>
1915  InstantiatedFromMember;
1916 
1918  DeclContext *DC,
1919  SourceLocation StartLoc,
1920  SourceLocation IdLoc,
1921  TemplateParameterList *Params,
1922  ClassTemplateDecl *SpecializedTemplate,
1924  const ASTTemplateArgumentListInfo *ArgsAsWritten,
1926 
1928  : ClassTemplateSpecializationDecl(C, ClassTemplatePartialSpecialization),
1929  InstantiatedFromMember(nullptr, false) {}
1930 
1931  void anchor() override;
1932 
1933 public:
1934  friend class ASTDeclReader;
1935  friend class ASTDeclWriter;
1936 
1938  Create(ASTContext &Context, TagKind TK, DeclContext *DC,
1939  SourceLocation StartLoc, SourceLocation IdLoc,
1940  TemplateParameterList *Params,
1941  ClassTemplateDecl *SpecializedTemplate,
1943  const TemplateArgumentListInfo &ArgInfos,
1944  QualType CanonInjectedType,
1946 
1948  CreateDeserialized(ASTContext &C, unsigned ID);
1949 
1951  return cast<ClassTemplatePartialSpecializationDecl>(
1952  static_cast<ClassTemplateSpecializationDecl *>(
1953  this)->getMostRecentDecl());
1954  }
1955 
1956  /// Get the list of template parameters
1958  return TemplateParams;
1959  }
1960 
1961  /// Get the template arguments as written.
1963  return ArgsAsWritten;
1964  }
1965 
1966  /// \brief Retrieve the member class template partial specialization from
1967  /// which this particular class template partial specialization was
1968  /// instantiated.
1969  ///
1970  /// \code
1971  /// template<typename T>
1972  /// struct Outer {
1973  /// template<typename U> struct Inner;
1974  /// template<typename U> struct Inner<U*> { }; // #1
1975  /// };
1976  ///
1977  /// Outer<float>::Inner<int*> ii;
1978  /// \endcode
1979  ///
1980  /// In this example, the instantiation of \c Outer<float>::Inner<int*> will
1981  /// end up instantiating the partial specialization
1982  /// \c Outer<float>::Inner<U*>, which itself was instantiated from the class
1983  /// template partial specialization \c Outer<T>::Inner<U*>. Given
1984  /// \c Outer<float>::Inner<U*>, this function would return
1985  /// \c Outer<T>::Inner<U*>.
1987  const auto *First =
1988  cast<ClassTemplatePartialSpecializationDecl>(getFirstDecl());
1989  return First->InstantiatedFromMember.getPointer();
1990  }
1993  return getInstantiatedFromMember();
1994  }
1995 
1998  auto *First = cast<ClassTemplatePartialSpecializationDecl>(getFirstDecl());
1999  First->InstantiatedFromMember.setPointer(PartialSpec);
2000  }
2001 
2002  /// \brief Determines whether this class template partial specialization
2003  /// template was a specialization of a member partial specialization.
2004  ///
2005  /// In the following example, the member template partial specialization
2006  /// \c X<int>::Inner<T*> is a member specialization.
2007  ///
2008  /// \code
2009  /// template<typename T>
2010  /// struct X {
2011  /// template<typename U> struct Inner;
2012  /// template<typename U> struct Inner<U*>;
2013  /// };
2014  ///
2015  /// template<> template<typename T>
2016  /// struct X<int>::Inner<T*> { /* ... */ };
2017  /// \endcode
2019  const auto *First =
2020  cast<ClassTemplatePartialSpecializationDecl>(getFirstDecl());
2021  return First->InstantiatedFromMember.getInt();
2022  }
2023 
2024  /// \brief Note that this member template is a specialization.
2026  auto *First = 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  Common() = default;
2069  };
2070 
2071  /// \brief Retrieve the set of specializations of this class template.
2072  llvm::FoldingSetVector<ClassTemplateSpecializationDecl> &
2073  getSpecializations() const;
2074 
2075  /// \brief Retrieve the set of partial specializations of this class
2076  /// template.
2077  llvm::FoldingSetVector<ClassTemplatePartialSpecializationDecl> &
2078  getPartialSpecializations();
2079 
2083  : RedeclarableTemplateDecl(CTDI, ClassTemplate, C, DC, L, Name, Params,
2084  Decl) {}
2085 
2087  DeclarationName Name, TemplateParameterList *Params,
2088  NamedDecl *Decl)
2089  : ClassTemplateDecl(nullptr, C, DC, L, Name, Params, Decl) {}
2090 
2091  CommonBase *newCommon(ASTContext &C) const override;
2092 
2094  return static_cast<Common *>(RedeclarableTemplateDecl::getCommonPtr());
2095  }
2096 
2097 public:
2098  friend class ASTDeclReader;
2099  friend class ASTDeclWriter;
2100 
2101  /// \brief Load any lazily-loaded specializations from the external source.
2102  void LoadLazySpecializations() const;
2103 
2104  /// \brief Get the underlying class declarations of the template.
2106  return static_cast<CXXRecordDecl *>(TemplatedDecl);
2107  }
2108 
2109  /// \brief Returns whether this template declaration defines the primary
2110  /// class pattern.
2112  return getTemplatedDecl()->isThisDeclarationADefinition();
2113  }
2114 
2115  // FIXME: remove default argument for AssociatedConstraints
2116  /// \brief Create a class template node.
2118  SourceLocation L,
2119  DeclarationName Name,
2120  TemplateParameterList *Params,
2121  NamedDecl *Decl,
2122  Expr *AssociatedConstraints = nullptr);
2123 
2124  /// \brief Create an empty class template node.
2125  static ClassTemplateDecl *CreateDeserialized(ASTContext &C, unsigned ID);
2126 
2127  /// \brief Return the specialization with the provided arguments if it exists,
2128  /// otherwise return the insertion point.
2130  findSpecialization(ArrayRef<TemplateArgument> Args, void *&InsertPos);
2131 
2132  /// \brief Insert the specified specialization knowing that it is not already
2133  /// in. InsertPos must be obtained from findSpecialization.
2134  void AddSpecialization(ClassTemplateSpecializationDecl *D, void *InsertPos);
2135 
2137  return cast<ClassTemplateDecl>(
2139  }
2141  return cast<ClassTemplateDecl>(
2143  }
2144 
2145  /// \brief Retrieve the previous declaration of this class template, or
2146  /// nullptr if no such declaration exists.
2148  return cast_or_null<ClassTemplateDecl>(
2149  static_cast<RedeclarableTemplateDecl *>(this)->getPreviousDecl());
2150  }
2152  return cast_or_null<ClassTemplateDecl>(
2153  static_cast<const RedeclarableTemplateDecl *>(
2154  this)->getPreviousDecl());
2155  }
2156 
2158  return cast<ClassTemplateDecl>(
2159  static_cast<RedeclarableTemplateDecl *>(this)->getMostRecentDecl());
2160  }
2162  return const_cast<ClassTemplateDecl*>(this)->getMostRecentDecl();
2163  }
2164 
2166  return cast_or_null<ClassTemplateDecl>(
2168  }
2169 
2170  /// \brief Return the partial specialization with the provided arguments if it
2171  /// exists, otherwise return the insertion point.
2173  findPartialSpecialization(ArrayRef<TemplateArgument> Args, void *&InsertPos);
2174 
2175  /// \brief Insert the specified partial specialization knowing that it is not
2176  /// already in. InsertPos must be obtained from findPartialSpecialization.
2177  void AddPartialSpecialization(ClassTemplatePartialSpecializationDecl *D,
2178  void *InsertPos);
2179 
2180  /// \brief Retrieve the partial specializations as an ordered list.
2181  void getPartialSpecializations(
2183 
2184  /// \brief Find a class template partial specialization with the given
2185  /// type T.
2186  ///
2187  /// \param T a dependent type that names a specialization of this class
2188  /// template.
2189  ///
2190  /// \returns the class template partial specialization that exactly matches
2191  /// the type \p T, or nullptr if no such partial specialization exists.
2192  ClassTemplatePartialSpecializationDecl *findPartialSpecialization(QualType T);
2193 
2194  /// \brief Find a class template partial specialization which was instantiated
2195  /// from the given member partial specialization.
2196  ///
2197  /// \param D a member class template partial specialization.
2198  ///
2199  /// \returns the class template partial specialization which was instantiated
2200  /// from the given member partial specialization, or nullptr if no such
2201  /// partial specialization exists.
2203  findPartialSpecInstantiatedFromMember(
2205 
2206  /// \brief Retrieve the template specialization type of the
2207  /// injected-class-name for this class template.
2208  ///
2209  /// The injected-class-name for a class template \c X is \c
2210  /// X<template-args>, where \c template-args is formed from the
2211  /// template arguments that correspond to the template parameters of
2212  /// \c X. For example:
2213  ///
2214  /// \code
2215  /// template<typename T, int N>
2216  /// struct array {
2217  /// typedef array this_type; // "array" is equivalent to "array<T, N>"
2218  /// };
2219  /// \endcode
2220  QualType getInjectedClassNameSpecialization();
2221 
2223  using spec_range = llvm::iterator_range<spec_iterator>;
2224 
2226  return spec_range(spec_begin(), spec_end());
2227  }
2228 
2230  return makeSpecIterator(getSpecializations(), false);
2231  }
2232 
2234  return makeSpecIterator(getSpecializations(), true);
2235  }
2236 
2237  // Implement isa/cast/dyncast support
2238  static bool classof(const Decl *D) { return classofKind(D->getKind()); }
2239  static bool classofKind(Kind K) { return K == ClassTemplate; }
2240 };
2241 
2242 /// \brief Declaration of a friend template.
2243 ///
2244 /// For example:
2245 /// \code
2246 /// template <typename T> class A {
2247 /// friend class MyVector<T>; // not a friend template
2248 /// template <typename U> friend class B; // not a friend template
2249 /// template <typename U> friend class Foo<T>::Nested; // friend template
2250 /// };
2251 /// \endcode
2252 ///
2253 /// \note This class is not currently in use. All of the above
2254 /// will yield a FriendDecl, not a FriendTemplateDecl.
2255 class FriendTemplateDecl : public Decl {
2256  virtual void anchor();
2257 
2258 public:
2259  using FriendUnion = llvm::PointerUnion<NamedDecl *,TypeSourceInfo *>;
2260 
2261 private:
2262  // The number of template parameters; always non-zero.
2263  unsigned NumParams = 0;
2264 
2265  // The parameter list.
2266  TemplateParameterList **Params = nullptr;
2267 
2268  // The declaration that's a friend of this class.
2269  FriendUnion Friend;
2270 
2271  // Location of the 'friend' specifier.
2272  SourceLocation FriendLoc;
2273 
2276  FriendUnion Friend, SourceLocation FriendLoc)
2277  : Decl(Decl::FriendTemplate, DC, Loc), NumParams(Params.size()),
2278  Params(Params.data()), Friend(Friend), FriendLoc(FriendLoc) {}
2279 
2280  FriendTemplateDecl(EmptyShell Empty) : Decl(Decl::FriendTemplate, Empty) {}
2281 
2282 public:
2283  friend class ASTDeclReader;
2284 
2285  static FriendTemplateDecl *
2286  Create(ASTContext &Context, DeclContext *DC, SourceLocation Loc,
2288  SourceLocation FriendLoc);
2289 
2290  static FriendTemplateDecl *CreateDeserialized(ASTContext &C, unsigned ID);
2291 
2292  /// If this friend declaration names a templated type (or
2293  /// a dependent member type of a templated type), return that
2294  /// type; otherwise return null.
2296  return Friend.dyn_cast<TypeSourceInfo*>();
2297  }
2298 
2299  /// If this friend declaration names a templated function (or
2300  /// a member function of a templated type), return that type;
2301  /// otherwise return null.
2303  return Friend.dyn_cast<NamedDecl*>();
2304  }
2305 
2306  /// \brief Retrieves the location of the 'friend' keyword.
2308  return FriendLoc;
2309  }
2310 
2312  assert(i <= NumParams);
2313  return Params[i];
2314  }
2315 
2316  unsigned getNumTemplateParameters() const {
2317  return NumParams;
2318  }
2319 
2320  // Implement isa/cast/dyncast/etc.
2321  static bool classof(const Decl *D) { return classofKind(D->getKind()); }
2322  static bool classofKind(Kind K) { return K == Decl::FriendTemplate; }
2323 };
2324 
2325 /// \brief Declaration of an alias template.
2326 ///
2327 /// For example:
2328 /// \code
2329 /// template <typename T> using V = std::map<T*, int, MyCompare<T>>;
2330 /// \endcode
2332 protected:
2334 
2336  DeclarationName Name, TemplateParameterList *Params,
2337  NamedDecl *Decl)
2338  : RedeclarableTemplateDecl(TypeAliasTemplate, C, DC, L, Name, Params,
2339  Decl) {}
2340 
2341  CommonBase *newCommon(ASTContext &C) const override;
2342 
2344  return static_cast<Common *>(RedeclarableTemplateDecl::getCommonPtr());
2345  }
2346 
2347 public:
2348  friend class ASTDeclReader;
2349  friend class ASTDeclWriter;
2350 
2351  /// Get the underlying function declaration of the template.
2353  return static_cast<TypeAliasDecl *>(TemplatedDecl);
2354  }
2355 
2356 
2358  return cast<TypeAliasTemplateDecl>(
2360  }
2362  return cast<TypeAliasTemplateDecl>(
2364  }
2365 
2366  /// \brief Retrieve the previous declaration of this function template, or
2367  /// nullptr if no such declaration exists.
2369  return cast_or_null<TypeAliasTemplateDecl>(
2370  static_cast<RedeclarableTemplateDecl *>(this)->getPreviousDecl());
2371  }
2373  return cast_or_null<TypeAliasTemplateDecl>(
2374  static_cast<const RedeclarableTemplateDecl *>(
2375  this)->getPreviousDecl());
2376  }
2377 
2379  return cast_or_null<TypeAliasTemplateDecl>(
2381  }
2382 
2383  /// \brief Create a function template node.
2385  SourceLocation L,
2386  DeclarationName Name,
2387  TemplateParameterList *Params,
2388  NamedDecl *Decl);
2389 
2390  /// \brief Create an empty alias template node.
2391  static TypeAliasTemplateDecl *CreateDeserialized(ASTContext &C, unsigned ID);
2392 
2393  // Implement isa/cast/dyncast support
2394  static bool classof(const Decl *D) { return classofKind(D->getKind()); }
2395  static bool classofKind(Kind K) { return K == TypeAliasTemplate; }
2396 };
2397 
2398 /// \brief Declaration of a function specialization at template class scope.
2399 ///
2400 /// This is a non-standard extension needed to support MSVC.
2401 ///
2402 /// For example:
2403 /// \code
2404 /// template <class T>
2405 /// class A {
2406 /// template <class U> void foo(U a) { }
2407 /// template<> void foo(int a) { }
2408 /// }
2409 /// \endcode
2410 ///
2411 /// "template<> foo(int a)" will be saved in Specialization as a normal
2412 /// CXXMethodDecl. Then during an instantiation of class A, it will be
2413 /// transformed into an actual function specialization.
2415  CXXMethodDecl *Specialization;
2416  bool HasExplicitTemplateArgs;
2417  TemplateArgumentListInfo TemplateArgs;
2418 
2420  CXXMethodDecl *FD, bool Args,
2421  TemplateArgumentListInfo TemplArgs)
2422  : Decl(Decl::ClassScopeFunctionSpecialization, DC, Loc),
2423  Specialization(FD), HasExplicitTemplateArgs(Args),
2424  TemplateArgs(std::move(TemplArgs)) {}
2425 
2427  : Decl(Decl::ClassScopeFunctionSpecialization, Empty) {}
2428 
2429  virtual void anchor();
2430 
2431 public:
2432  friend class ASTDeclReader;
2433  friend class ASTDeclWriter;
2434 
2435  CXXMethodDecl *getSpecialization() const { return Specialization; }
2436  bool hasExplicitTemplateArgs() const { return HasExplicitTemplateArgs; }
2437  const TemplateArgumentListInfo& templateArgs() const { return TemplateArgs; }
2438 
2440  DeclContext *DC,
2441  SourceLocation Loc,
2442  CXXMethodDecl *FD,
2443  bool HasExplicitTemplateArgs,
2444  TemplateArgumentListInfo TemplateArgs) {
2445  return new (C, DC) ClassScopeFunctionSpecializationDecl(
2446  DC, Loc, FD, HasExplicitTemplateArgs, std::move(TemplateArgs));
2447  }
2448 
2450  CreateDeserialized(ASTContext &Context, unsigned ID);
2451 
2452  // Implement isa/cast/dyncast/etc.
2453  static bool classof(const Decl *D) { return classofKind(D->getKind()); }
2454 
2455  static bool classofKind(Kind K) {
2456  return K == Decl::ClassScopeFunctionSpecialization;
2457  }
2458 };
2459 
2460 /// Implementation of inline functions that require the template declarations
2461 inline AnyFunctionDecl::AnyFunctionDecl(FunctionTemplateDecl *FTD)
2462  : Function(FTD) {}
2463 
2464 /// \brief Represents a variable template specialization, which refers to
2465 /// a variable template with a given set of template arguments.
2466 ///
2467 /// Variable template specializations represent both explicit
2468 /// specializations of variable templates, as in the example below, and
2469 /// implicit instantiations of variable templates.
2470 ///
2471 /// \code
2472 /// template<typename T> constexpr T pi = T(3.1415926535897932385);
2473 ///
2474 /// template<>
2475 /// constexpr float pi<float>; // variable template specialization pi<float>
2476 /// \endcode
2478  public llvm::FoldingSetNode {
2479 
2480  /// \brief Structure that stores information about a variable template
2481  /// specialization that was instantiated from a variable template partial
2482  /// specialization.
2483  struct SpecializedPartialSpecialization {
2484  /// \brief The variable template partial specialization from which this
2485  /// variable template specialization was instantiated.
2486  VarTemplatePartialSpecializationDecl *PartialSpecialization;
2487 
2488  /// \brief The template argument list deduced for the variable template
2489  /// partial specialization itself.
2490  const TemplateArgumentList *TemplateArgs;
2491  };
2492 
2493  /// \brief The template that this specialization specializes.
2494  llvm::PointerUnion<VarTemplateDecl *, SpecializedPartialSpecialization *>
2495  SpecializedTemplate;
2496 
2497  /// \brief Further info for explicit template specialization/instantiation.
2498  struct ExplicitSpecializationInfo {
2499  /// \brief The type-as-written.
2500  TypeSourceInfo *TypeAsWritten = nullptr;
2501 
2502  /// \brief The location of the extern keyword.
2503  SourceLocation ExternLoc;
2504 
2505  /// \brief The location of the template keyword.
2506  SourceLocation TemplateKeywordLoc;
2507 
2508  ExplicitSpecializationInfo() = default;
2509  };
2510 
2511  /// \brief Further info for explicit template specialization/instantiation.
2512  /// Does not apply to implicit specializations.
2513  ExplicitSpecializationInfo *ExplicitInfo = nullptr;
2514 
2515  /// \brief The template arguments used to describe this specialization.
2516  const TemplateArgumentList *TemplateArgs;
2517  TemplateArgumentListInfo TemplateArgsInfo;
2518 
2519  /// \brief The point where this template was instantiated (if any).
2520  SourceLocation PointOfInstantiation;
2521 
2522  /// \brief The kind of specialization this declaration refers to.
2523  /// Really a value of type TemplateSpecializationKind.
2524  unsigned SpecializationKind : 3;
2525 
2526  /// \brief Whether this declaration is a complete definition of the
2527  /// variable template specialization. We can't otherwise tell apart
2528  /// an instantiated declaration from an instantiated definition with
2529  /// no initializer.
2530  unsigned IsCompleteDefinition : 1;
2531 
2532 protected:
2534  SourceLocation StartLoc, SourceLocation IdLoc,
2535  VarTemplateDecl *SpecializedTemplate,
2536  QualType T, TypeSourceInfo *TInfo,
2537  StorageClass S,
2539 
2540  explicit VarTemplateSpecializationDecl(Kind DK, ASTContext &Context);
2541 
2542 public:
2543  friend class ASTDeclReader;
2544  friend class ASTDeclWriter;
2545  friend class VarDecl;
2546 
2548  Create(ASTContext &Context, DeclContext *DC, SourceLocation StartLoc,
2549  SourceLocation IdLoc, VarTemplateDecl *SpecializedTemplate, QualType T,
2550  TypeSourceInfo *TInfo, StorageClass S,
2552  static VarTemplateSpecializationDecl *CreateDeserialized(ASTContext &C,
2553  unsigned ID);
2554 
2555  void getNameForDiagnostic(raw_ostream &OS, const PrintingPolicy &Policy,
2556  bool Qualified) const override;
2557 
2559  VarDecl *Recent = static_cast<VarDecl *>(this)->getMostRecentDecl();
2560  return cast<VarTemplateSpecializationDecl>(Recent);
2561  }
2562 
2563  /// \brief Retrieve the template that this specialization specializes.
2564  VarTemplateDecl *getSpecializedTemplate() const;
2565 
2566  /// \brief Retrieve the template arguments of the variable template
2567  /// specialization.
2568  const TemplateArgumentList &getTemplateArgs() const { return *TemplateArgs; }
2569 
2570  // TODO: Always set this when creating the new specialization?
2571  void setTemplateArgsInfo(const TemplateArgumentListInfo &ArgsInfo);
2572 
2574  return TemplateArgsInfo;
2575  }
2576 
2577  /// \brief Determine the kind of specialization that this
2578  /// declaration represents.
2580  return static_cast<TemplateSpecializationKind>(SpecializationKind);
2581  }
2582 
2584  return getSpecializationKind() == TSK_ExplicitSpecialization;
2585  }
2586 
2587  /// \brief True if this declaration is an explicit specialization,
2588  /// explicit instantiation declaration, or explicit instantiation
2589  /// definition.
2593  }
2594 
2596  SpecializationKind = TSK;
2597  }
2598 
2599  /// \brief Get the point of instantiation (if any), or null if none.
2601  return PointOfInstantiation;
2602  }
2603 
2605  assert(Loc.isValid() && "point of instantiation must be valid!");
2606  PointOfInstantiation = Loc;
2607  }
2608 
2609  void setCompleteDefinition() { IsCompleteDefinition = true; }
2610 
2611  /// \brief If this variable template specialization is an instantiation of
2612  /// a template (rather than an explicit specialization), return the
2613  /// variable template or variable template partial specialization from which
2614  /// it was instantiated.
2615  llvm::PointerUnion<VarTemplateDecl *, VarTemplatePartialSpecializationDecl *>
2617  if (!isTemplateInstantiation(getSpecializationKind()))
2618  return llvm::PointerUnion<VarTemplateDecl *,
2620 
2621  return getSpecializedTemplateOrPartial();
2622  }
2623 
2624  /// \brief Retrieve the variable template or variable template partial
2625  /// specialization which was specialized by this.
2626  llvm::PointerUnion<VarTemplateDecl *, VarTemplatePartialSpecializationDecl *>
2628  if (const auto *PartialSpec =
2629  SpecializedTemplate.dyn_cast<SpecializedPartialSpecialization *>())
2630  return PartialSpec->PartialSpecialization;
2631 
2632  return SpecializedTemplate.get<VarTemplateDecl *>();
2633  }
2634 
2635  /// \brief Retrieve the set of template arguments that should be used
2636  /// to instantiate the initializer of the variable template or variable
2637  /// template partial specialization from which this variable template
2638  /// specialization was instantiated.
2639  ///
2640  /// \returns For a variable template specialization instantiated from the
2641  /// primary template, this function will return the same template arguments
2642  /// as getTemplateArgs(). For a variable template specialization instantiated
2643  /// from a variable template partial specialization, this function will the
2644  /// return deduced template arguments for the variable template partial
2645  /// specialization itself.
2647  if (const auto *PartialSpec =
2648  SpecializedTemplate.dyn_cast<SpecializedPartialSpecialization *>())
2649  return *PartialSpec->TemplateArgs;
2650 
2651  return getTemplateArgs();
2652  }
2653 
2654  /// \brief Note that this variable template specialization is actually an
2655  /// instantiation of the given variable template partial specialization whose
2656  /// template arguments have been deduced.
2658  const TemplateArgumentList *TemplateArgs) {
2659  assert(!SpecializedTemplate.is<SpecializedPartialSpecialization *>() &&
2660  "Already set to a variable template partial specialization!");
2661  auto *PS = new (getASTContext()) SpecializedPartialSpecialization();
2662  PS->PartialSpecialization = PartialSpec;
2663  PS->TemplateArgs = TemplateArgs;
2664  SpecializedTemplate = PS;
2665  }
2666 
2667  /// \brief Note that this variable template specialization is an instantiation
2668  /// of the given variable template.
2670  assert(!SpecializedTemplate.is<SpecializedPartialSpecialization *>() &&
2671  "Previously set to a variable template partial specialization!");
2672  SpecializedTemplate = TemplDecl;
2673  }
2674 
2675  /// \brief Sets the type of this specialization as it was written by
2676  /// the user.
2678  if (!ExplicitInfo)
2679  ExplicitInfo = new (getASTContext()) ExplicitSpecializationInfo;
2680  ExplicitInfo->TypeAsWritten = T;
2681  }
2682 
2683  /// \brief Gets the type of this specialization as it was written by
2684  /// the user, if it was so written.
2686  return ExplicitInfo ? ExplicitInfo->TypeAsWritten : nullptr;
2687  }
2688 
2689  /// \brief Gets the location of the extern keyword, if present.
2691  return ExplicitInfo ? ExplicitInfo->ExternLoc : SourceLocation();
2692  }
2693 
2694  /// \brief Sets the location of the extern keyword.
2696  if (!ExplicitInfo)
2697  ExplicitInfo = new (getASTContext()) ExplicitSpecializationInfo;
2698  ExplicitInfo->ExternLoc = Loc;
2699  }
2700 
2701  /// \brief Sets the location of the template keyword.
2703  if (!ExplicitInfo)
2704  ExplicitInfo = new (getASTContext()) ExplicitSpecializationInfo;
2705  ExplicitInfo->TemplateKeywordLoc = Loc;
2706  }
2707 
2708  /// \brief Gets the location of the template keyword, if present.
2710  return ExplicitInfo ? ExplicitInfo->TemplateKeywordLoc : SourceLocation();
2711  }
2712 
2713  void Profile(llvm::FoldingSetNodeID &ID) const {
2714  Profile(ID, TemplateArgs->asArray(), getASTContext());
2715  }
2716 
2717  static void Profile(llvm::FoldingSetNodeID &ID,
2718  ArrayRef<TemplateArgument> TemplateArgs,
2719  ASTContext &Context) {
2720  ID.AddInteger(TemplateArgs.size());
2721  for (const TemplateArgument &TemplateArg : TemplateArgs)
2722  TemplateArg.Profile(ID, Context);
2723  }
2724 
2725  static bool classof(const Decl *D) { return classofKind(D->getKind()); }
2726 
2727  static bool classofKind(Kind K) {
2728  return K >= firstVarTemplateSpecialization &&
2729  K <= lastVarTemplateSpecialization;
2730  }
2731 };
2732 
2735  /// \brief The list of template parameters
2736  TemplateParameterList *TemplateParams = nullptr;
2737 
2738  /// \brief The source info for the template arguments as written.
2739  /// FIXME: redundant with TypeAsWritten?
2740  const ASTTemplateArgumentListInfo *ArgsAsWritten = nullptr;
2741 
2742  /// \brief The variable template partial specialization from which this
2743  /// variable template partial specialization was instantiated.
2744  ///
2745  /// The boolean value will be true to indicate that this variable template
2746  /// partial specialization was specialized at this level.
2747  llvm::PointerIntPair<VarTemplatePartialSpecializationDecl *, 1, bool>
2748  InstantiatedFromMember;
2749 
2751  ASTContext &Context, DeclContext *DC, SourceLocation StartLoc,
2752  SourceLocation IdLoc, TemplateParameterList *Params,
2753  VarTemplateDecl *SpecializedTemplate, QualType T, TypeSourceInfo *TInfo,
2755  const ASTTemplateArgumentListInfo *ArgInfos);
2756 
2758  : VarTemplateSpecializationDecl(VarTemplatePartialSpecialization,
2759  Context),
2760  InstantiatedFromMember(nullptr, false) {}
2761 
2762  void anchor() override;
2763 
2764 public:
2765  friend class ASTDeclReader;
2766  friend class ASTDeclWriter;
2767 
2769  Create(ASTContext &Context, DeclContext *DC, SourceLocation StartLoc,
2770  SourceLocation IdLoc, TemplateParameterList *Params,
2771  VarTemplateDecl *SpecializedTemplate, QualType T,
2773  const TemplateArgumentListInfo &ArgInfos);
2774 
2775  static VarTemplatePartialSpecializationDecl *CreateDeserialized(ASTContext &C,
2776  unsigned ID);
2777 
2779  return cast<VarTemplatePartialSpecializationDecl>(
2780  static_cast<VarTemplateSpecializationDecl *>(
2781  this)->getMostRecentDecl());
2782  }
2783 
2784  /// Get the list of template parameters
2786  return TemplateParams;
2787  }
2788 
2789  /// Get the template arguments as written.
2791  return ArgsAsWritten;
2792  }
2793 
2794  /// \brief Retrieve the member variable template partial specialization from
2795  /// which this particular variable template partial specialization was
2796  /// instantiated.
2797  ///
2798  /// \code
2799  /// template<typename T>
2800  /// struct Outer {
2801  /// template<typename U> U Inner;
2802  /// template<typename U> U* Inner<U*> = (U*)(0); // #1
2803  /// };
2804  ///
2805  /// template int* Outer<float>::Inner<int*>;
2806  /// \endcode
2807  ///
2808  /// In this example, the instantiation of \c Outer<float>::Inner<int*> will
2809  /// end up instantiating the partial specialization
2810  /// \c Outer<float>::Inner<U*>, which itself was instantiated from the
2811  /// variable template partial specialization \c Outer<T>::Inner<U*>. Given
2812  /// \c Outer<float>::Inner<U*>, this function would return
2813  /// \c Outer<T>::Inner<U*>.
2815  const auto *First =
2816  cast<VarTemplatePartialSpecializationDecl>(getFirstDecl());
2817  return First->InstantiatedFromMember.getPointer();
2818  }
2819 
2820  void
2822  auto *First = cast<VarTemplatePartialSpecializationDecl>(getFirstDecl());
2823  First->InstantiatedFromMember.setPointer(PartialSpec);
2824  }
2825 
2826  /// \brief Determines whether this variable template partial specialization
2827  /// was a specialization of a member partial specialization.
2828  ///
2829  /// In the following example, the member template partial specialization
2830  /// \c X<int>::Inner<T*> is a member specialization.
2831  ///
2832  /// \code
2833  /// template<typename T>
2834  /// struct X {
2835  /// template<typename U> U Inner;
2836  /// template<typename U> U* Inner<U*> = (U*)(0);
2837  /// };
2838  ///
2839  /// template<> template<typename T>
2840  /// U* X<int>::Inner<T*> = (T*)(0) + 1;
2841  /// \endcode
2843  const auto *First =
2844  cast<VarTemplatePartialSpecializationDecl>(getFirstDecl());
2845  return First->InstantiatedFromMember.getInt();
2846  }
2847 
2848  /// \brief Note that this member template is a specialization.
2850  auto *First = cast<VarTemplatePartialSpecializationDecl>(getFirstDecl());
2851  assert(First->InstantiatedFromMember.getPointer() &&
2852  "Only member templates can be member template specializations");
2853  return First->InstantiatedFromMember.setInt(true);
2854  }
2855 
2856  static bool classof(const Decl *D) { return classofKind(D->getKind()); }
2857 
2858  static bool classofKind(Kind K) {
2859  return K == VarTemplatePartialSpecialization;
2860  }
2861 };
2862 
2863 /// Declaration of a variable template.
2865 protected:
2866  /// \brief Data that is common to all of the declarations of a given
2867  /// variable template.
2868  struct Common : CommonBase {
2869  /// \brief The variable template specializations for this variable
2870  /// template, including explicit specializations and instantiations.
2871  llvm::FoldingSetVector<VarTemplateSpecializationDecl> Specializations;
2872 
2873  /// \brief The variable template partial specializations for this variable
2874  /// template.
2875  llvm::FoldingSetVector<VarTemplatePartialSpecializationDecl>
2877 
2878  Common() = default;
2879  };
2880 
2881  /// \brief Retrieve the set of specializations of this variable template.
2882  llvm::FoldingSetVector<VarTemplateSpecializationDecl> &
2883  getSpecializations() const;
2884 
2885  /// \brief Retrieve the set of partial specializations of this class
2886  /// template.
2887  llvm::FoldingSetVector<VarTemplatePartialSpecializationDecl> &
2888  getPartialSpecializations();
2889 
2891  DeclarationName Name, TemplateParameterList *Params,
2892  NamedDecl *Decl)
2893  : RedeclarableTemplateDecl(VarTemplate, C, DC, L, Name, Params, Decl) {}
2894 
2895  CommonBase *newCommon(ASTContext &C) const override;
2896 
2898  return static_cast<Common *>(RedeclarableTemplateDecl::getCommonPtr());
2899  }
2900 
2901 public:
2902  friend class ASTDeclReader;
2903  friend class ASTDeclWriter;
2904 
2905  /// \brief Load any lazily-loaded specializations from the external source.
2906  void LoadLazySpecializations() const;
2907 
2908  /// \brief Get the underlying variable declarations of the template.
2910  return static_cast<VarDecl *>(TemplatedDecl);
2911  }
2912 
2913  /// \brief Returns whether this template declaration defines the primary
2914  /// variable pattern.
2916  return getTemplatedDecl()->isThisDeclarationADefinition();
2917  }
2918 
2920 
2921  /// \brief Create a variable template node.
2922  static VarTemplateDecl *Create(ASTContext &C, DeclContext *DC,
2924  TemplateParameterList *Params,
2925  VarDecl *Decl);
2926 
2927  /// \brief Create an empty variable template node.
2928  static VarTemplateDecl *CreateDeserialized(ASTContext &C, unsigned ID);
2929 
2930  /// \brief Return the specialization with the provided arguments if it exists,
2931  /// otherwise return the insertion point.
2933  findSpecialization(ArrayRef<TemplateArgument> Args, void *&InsertPos);
2934 
2935  /// \brief Insert the specified specialization knowing that it is not already
2936  /// in. InsertPos must be obtained from findSpecialization.
2937  void AddSpecialization(VarTemplateSpecializationDecl *D, void *InsertPos);
2938 
2940  return cast<VarTemplateDecl>(RedeclarableTemplateDecl::getCanonicalDecl());
2941  }
2943  return cast<VarTemplateDecl>(RedeclarableTemplateDecl::getCanonicalDecl());
2944  }
2945 
2946  /// \brief Retrieve the previous declaration of this variable template, or
2947  /// nullptr if no such declaration exists.
2949  return cast_or_null<VarTemplateDecl>(
2950  static_cast<RedeclarableTemplateDecl *>(this)->getPreviousDecl());
2951  }
2953  return cast_or_null<VarTemplateDecl>(
2954  static_cast<const RedeclarableTemplateDecl *>(
2955  this)->getPreviousDecl());
2956  }
2957 
2959  return cast<VarTemplateDecl>(
2960  static_cast<RedeclarableTemplateDecl *>(this)->getMostRecentDecl());
2961  }
2963  return const_cast<VarTemplateDecl *>(this)->getMostRecentDecl();
2964  }
2965 
2967  return cast_or_null<VarTemplateDecl>(
2969  }
2970 
2971  /// \brief Return the partial specialization with the provided arguments if it
2972  /// exists, otherwise return the insertion point.
2974  findPartialSpecialization(ArrayRef<TemplateArgument> Args, void *&InsertPos);
2975 
2976  /// \brief Insert the specified partial specialization knowing that it is not
2977  /// already in. InsertPos must be obtained from findPartialSpecialization.
2978  void AddPartialSpecialization(VarTemplatePartialSpecializationDecl *D,
2979  void *InsertPos);
2980 
2981  /// \brief Retrieve the partial specializations as an ordered list.
2982  void getPartialSpecializations(
2984 
2985  /// \brief Find a variable template partial specialization which was
2986  /// instantiated
2987  /// from the given member partial specialization.
2988  ///
2989  /// \param D a member variable template partial specialization.
2990  ///
2991  /// \returns the variable template partial specialization which was
2992  /// instantiated
2993  /// from the given member partial specialization, or nullptr if no such
2994  /// partial specialization exists.
2995  VarTemplatePartialSpecializationDecl *findPartialSpecInstantiatedFromMember(
2997 
2999  using spec_range = llvm::iterator_range<spec_iterator>;
3000 
3002  return spec_range(spec_begin(), spec_end());
3003  }
3004 
3006  return makeSpecIterator(getSpecializations(), false);
3007  }
3008 
3010  return makeSpecIterator(getSpecializations(), true);
3011  }
3012 
3013  // Implement isa/cast/dyncast support
3014  static bool classof(const Decl *D) { return classofKind(D->getKind()); }
3015  static bool classofKind(Kind K) { return K == VarTemplate; }
3016 };
3017 
3019  if (auto *PD = P.dyn_cast<TemplateTypeParmDecl *>())
3020  return PD;
3021  if (auto *PD = P.dyn_cast<NonTypeTemplateParmDecl *>())
3022  return PD;
3023  return P.get<TemplateTemplateParmDecl *>();
3024 }
3025 
3027  auto *TD = dyn_cast<TemplateDecl>(D);
3028  return TD && (isa<ClassTemplateDecl>(TD) ||
3029  isa<ClassTemplatePartialSpecializationDecl>(TD) ||
3030  isa<TypeAliasTemplateDecl>(TD) ||
3031  isa<TemplateTemplateParmDecl>(TD))
3032  ? TD
3033  : nullptr;
3034 }
3035 
3036 } // namespace clang
3037 
3038 #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:722
void setInstantiationOf(ClassTemplateDecl *TemplDecl)
Note that this class template specialization is an instantiation of the given class template...
static const Decl * getCanonicalDecl(const Decl *D)
spec_iterator spec_begin() const
Represents a function declaration or definition.
Definition: Decl.h:1709
void setTemplateSpecializationKind(TemplateSpecializationKind TSK)
Set the template specialization kind.
Definition: DeclTemplate.h:570
FunctionTemplateDecl * getTemplate() const
Retrieve the template from which this function was specialized.
Definition: DeclTemplate.h:550
unsigned getNumTemplateArgs() const
Returns the number of explicit template arguments that were given.
Definition: DeclTemplate.h:719
RedeclarableTemplateDecl(ConstrainedTemplateDeclInfo *CTDI, Kind DK, ASTContext &C, DeclContext *DC, SourceLocation L, DeclarationName Name, TemplateParameterList *Params, NamedDecl *Decl)
Definition: DeclTemplate.h:835
SourceLocation getPointOfInstantiation() const
Retrieve the first point of instantiation of this function template specialization.
Definition: DeclTemplate.h:581
llvm::iterator_range< redecl_iterator > redecl_range
Definition: DeclBase.h:936
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:530
RedeclarableTemplateDecl(Kind DK, ASTContext &C, DeclContext *DC, SourceLocation L, DeclarationName Name, TemplateParameterList *Params, NamedDecl *Decl)
Definition: DeclTemplate.h:842
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.
constexpr XRayInstrMask Function
Definition: XRayInstr.h:39
TypeSourceInfo * getDefaultArgumentInfo() const
Retrieves the default argument&#39;s source information, if any.
const Expr * getRequiresClause() const
Get the constraint-expression from the associated requires-clause (if any)
Definition: DeclTemplate.h:440
static bool classofKind(Kind K)
const ClassTemplateDecl * getCanonicalDecl() const
bool defaultArgumentWasInherited() const
Determines whether the default argument was inherited from a previous declaration of this template...
TemplateParameterList * getTemplateParameters() const
Get the list of template parameters.
bool isPackExpansion() const
Whether this parameter pack is a pack expansion.
unsigned getDepth() const
Get the nesting depth of the template parameter.
static void Profile(llvm::FoldingSetNodeID &ID, ArrayRef< TemplateArgument > TemplateArgs, ASTContext &Context)
TemplateParameterList * getExpansionTemplateParameters(unsigned I) const
Retrieve a particular expansion type within an expanded parameter pack.
Represents a variable declaration or definition.
Definition: Decl.h:812
Declaration of a redeclarable template.
Definition: DeclTemplate.h:737
TemplateParameterList * getTemplateParameters() const
Definition: DeclTemplate.h:376
NamedDecl *const * const_iterator
Iterates through the template parameters in this list.
Definition: DeclTemplate.h:117
bool hasDefaultArgument() const
Determine whether this template parameter has a default argument.
TemplateSpecializationKind getTemplateSpecializationKind() const
Determine what kind of template specialization this is.
Definition: DeclTemplate.h:553
SourceLocation getExternLoc() const
Gets the location of the extern keyword, if present.
llvm::PointerUnion< TemplateParameterList *, ConstrainedTemplateDeclInfo * > TemplateParams
The template parameter list and optional requires-clause associated with this declaration; alternativ...
Definition: DeclTemplate.h:476
static bool classofKind(Kind K)
const TemplateArgumentLoc * getTemplateArgs() const
Returns the explicit template arguments that were given.
Definition: DeclTemplate.h:714
Represents a variable template specialization, which refers to a variable template with a given set o...
SourceLocation getPointOfInstantiation() const
Get the point of instantiation (if any), or null if none.
SourceLocation getLAngleLoc() const
Definition: DeclTemplate.h:173
TemplateParmPosition(unsigned D, unsigned P)
Represents an explicit template argument list in C++, e.g., the "<int>" in "sort<int>".
Definition: TemplateBase.h: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:2510
TemplateSpecializationKind getTemplateSpecializationKind() const
Determine what kind of template specialization this is.
Definition: DeclTemplate.h:630
TypeSourceInfo * getTypeAsWritten() const
Gets the type of this specialization as it was written by the user, if it was so written.
SourceRange getSourceRange() const override LLVM_READONLY
Source range that this declaration covers.
Definition: DeclTemplate.h:462
Provides information about a dependent function-template specialization declaration.
Definition: DeclTemplate.h:672
Represents the builtin template declaration which is used to implement __make_integer_seq and other b...
static bool classof(const Decl *D)
const VarTemplateDecl * getCanonicalDecl() const
static bool classof(const Decl *D)
llvm::iterator_range< spec_iterator > spec_range
llvm::PointerUnion< VarTemplateDecl *, VarTemplatePartialSpecializationDecl * > getSpecializedTemplateOrPartial() const
Retrieve the variable template or variable template partial specialization which was specialized by t...
const TemplateArgumentList & getTemplateArgs() const
Retrieve the template arguments of the class template specialization.
bool isTemplateExplicitInstantiationOrSpecialization(TemplateSpecializationKind Kind)
True if this template specialization kind is an explicit specialization, explicit instantiation decla...
Definition: Specifiers.h: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:736
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:2041
ClassTemplateSpecializationDecl * getMostRecentDecl()
unsigned getPosition() const
Get the position of the template parameter within its parameter list.
static bool classofKind(Kind K)
Definition: DeclTemplate.h:458
void Profile(llvm::FoldingSetNodeID &ID)
Definition: DeclTemplate.h:591
Stores the template parameter list and associated constraints for TemplateDecl objects that track ass...
Definition: DeclTemplate.h:370
Declaration of a function specialization at template class scope.
SourceLocation getTemplateLoc() const
Definition: DeclTemplate.h:172
TypeSourceInfo * getFriendType() const
If this friend declaration names a templated type (or a dependent member type of a templated type)...
llvm::iterator_range< spec_iterator > spec_range
TypeAliasTemplateDecl(ASTContext &C, DeclContext *DC, SourceLocation L, DeclarationName Name, TemplateParameterList *Params, NamedDecl *Decl)
void setDefaultArgument(Expr *DefArg)
Set the default argument for this template parameter, and whether that default argument was inherited...
static bool classof(const Decl *D)
Provides information about a function template specialization, which is a FunctionDecl that has been ...
Definition: DeclTemplate.h:508
TypeAliasDecl * getTemplatedDecl() const
Get the underlying function declaration of the template.
const TemplateArgumentList * TemplateArguments
The template arguments used to produce the function template specialization from the function templat...
Definition: DeclTemplate.h:540
FunctionTemplateDecl * getCanonicalDecl() override
Retrieves the canonical declaration of this template.
Common * getCommonPtr() const
Definition: DeclTemplate.h:999
unsigned getMinRequiredArguments() const
Returns the minimum number of arguments needed to form a template specialization. ...
const RedeclarableTemplateDecl * getCanonicalDecl() const
Definition: DeclTemplate.h:857
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:879
static bool classofKind(Kind K)
static void Profile(llvm::FoldingSetNodeID &ID, ArrayRef< TemplateArgument > TemplateArgs, ASTContext &Context)
Definition: DeclTemplate.h:597
TemplateDecl * getAsTypeTemplateDecl(Decl *D)
Represents a declaration of a type.
Definition: Decl.h:2811
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:926
void setInstantiatedFromMember(ClassTemplatePartialSpecializationDecl *PartialSpec)
void setInstantiationOf(ClassTemplatePartialSpecializationDecl *PartialSpec, const TemplateArgumentList *TemplateArgs)
Note that this class template specialization is actually an instantiation of the given class template...
const TemplateArgument & operator[](unsigned Idx) const
Retrieve the template argument at a given index.
Definition: DeclTemplate.h:261
void setSpecializationKind(TemplateSpecializationKind TSK)
spec_range specializations() const
ArgType get() const
Get the default argument&#39;s value.
Definition: DeclTemplate.h:322
ClassTemplatePartialSpecializationDecl * getInstantiatedFromMemberTemplate() const
TemplateParameterList * getTemplateParameterList(unsigned i) const
unsigned getIndex() const
Get the index of the template parameter within its parameter list.
static bool classofKind(Kind K)
bool hasDefaultArgument() const
Determine whether this template parameter has a default argument.
QualType getExpansionType(unsigned I) const
Retrieve a particular expansion type within an expanded parameter pack.
SourceLocation PointOfInstantiation
The point at which this function template specialization was first instantiated.
Definition: DeclTemplate.h:547
const TemplateArgument * data() const
Retrieve a pointer to the template argument list.
Definition: DeclTemplate.h:273
const TypeAliasTemplateDecl * getCanonicalDecl() const
CXXRecordDecl * getTemplatedDecl() const
Get the underlying class declarations of the template.
bool isParameterPack() const
Whether this template template parameter is a template parameter pack.
ClassTemplateDecl(ASTContext &C, DeclContext *DC, SourceLocation L, DeclarationName Name, TemplateParameterList *Params, NamedDecl *Decl)
bool isExpandedParameterPack() const
Whether this parameter is a non-type template parameter pack that has a known list of different types...
llvm::FoldingSetVector< FunctionTemplateSpecializationInfo > Specializations
The function template specializations for this function template, including explicit specializations ...
Definition: DeclTemplate.h:977
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
Represents the declaration of a typedef-name via a C++11 alias-declaration.
Definition: Decl.h:2975
Represents a ValueDecl that came out of a declarator.
Definition: Decl.h:688
bool isParameterPack() const
Whether this parameter is a non-type template parameter pack.
void clear()
Remove the default argument, even if it was inherited.
Definition: DeclTemplate.h: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:487
SourceLocation End
const VarTemplateDecl * getPreviousDecl() const
llvm::FoldingSetVector< VarTemplatePartialSpecializationDecl > PartialSpecializations
The variable template partial specializations for this variable template.
Expr * getAssociatedConstraints() const
Definition: DeclTemplate.h:445
void setInherited(const ASTContext &C, ParmDecl *InheritedFrom)
Set that the default argument was inherited from another parameter.
Definition: DeclTemplate.h:348
int Id
Definition: ASTDiff.cpp:191
static bool classof(const Decl *D)
const FunctionProtoType * T
MemberSpecializationInfo(NamedDecl *IF, TemplateSpecializationKind TSK, SourceLocation POI=SourceLocation())
Definition: DeclTemplate.h:618
llvm::PointerIntPair< FunctionTemplateDecl *, 2 > Template
The function template from which this function template specialization was generated.
Definition: DeclTemplate.h:536
llvm::PointerUnion< NamedDecl *, TypeSourceInfo * > FriendUnion
static bool classof(const Decl *D)
#define bool
Definition: stdbool.h:31
spec_iterator spec_end() const
Data that is common to all of the declarations of a given variable template.
spec_iterator spec_end() const
void setMemberSpecialization()
Note that this member template is a specialization.
Definition: DeclTemplate.h:884
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:708
static ArrayRef< TemplateArgument > getTemplateArgs(EntryType *D)
Definition: DeclTemplate.h:762
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:758
ClassTemplateDecl * getInstantiatedFromMemberTemplate() const
TypeSourceInfo * getExpansionTypeSourceInfo(unsigned I) const
Retrieve a particular expansion type source info within an expanded parameter pack.
void setPointOfInstantiation(SourceLocation POI)
Set the (first) point of instantiation of this function template specialization.
Definition: DeclTemplate.h:587
bool isInjectedClassName() const
Determines whether this declaration represents the injected class name.
Definition: Decl.cpp:3982
NamedDecl * getInstantiatedFrom() const
Retrieve the member declaration from which this member was instantiated.
Definition: DeclTemplate.h:627
OnStackType
Type used to indicate that the template argument list itself is a stack object.
Definition: DeclTemplate.h:231
bool isExplicitInstantiationOrSpecialization() const
True if this declaration is an explicit specialization, explicit instantiation declaration, or explicit instantiation definition.
Definition: DeclTemplate.h:564
#define false
Definition: stdbool.h:33
Stores a list of template parameters and the associated requires-clause (if any) for a TemplateDecl a...
Definition: DeclTemplate.h:189
spec_iterator spec_begin() const
QualType InjectedClassNameType
The injected-class-name type for this class template.
static bool classofKind(Kind K)
const ASTTemplateArgumentListInfo * TemplateArgumentsAsWritten
The template arguments as written in the sources, if provided.
Definition: DeclTemplate.h:543
FunctionDecl * getTemplatedDecl() const
Get the underlying function declaration of the template.
Encodes a location in the source.
ClassTemplateDecl * getPreviousDecl()
Retrieve the previous declaration of this class template, or nullptr if no such declaration exists...
llvm::PointerUnion< ClassTemplateDecl *, ClassTemplatePartialSpecializationDecl * > getInstantiatedFrom() const
If this class template specialization is an instantiation of a template (rather than an explicit spec...
const FunctionTemplateDecl * getPreviousDecl() const
void setExternLoc(SourceLocation Loc)
Sets the location of the extern keyword.
bool wasDeclaredWithTypename() const
Whether this template type parameter was declared with the &#39;typename&#39; keyword.
const TemplateArgumentListInfo & templateArgs() const
ASTContext & getASTContext() const LLVM_READONLY
Definition: DeclBase.cpp:376
void setPosition(unsigned P)
const Expr * getRequiresClause() const
The constraint-expression of the associated requires-clause.
Definition: DeclTemplate.h:168
Represents a static or instance method of a struct/union/class.
Definition: DeclCXX.h:2016
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:974
void Profile(llvm::FoldingSetNodeID &ID) const
BuiltinTemplateKind
Kinds of BuiltinTemplateDecl.
Definition: Builtins.h:241
TemplateSpecializationKind getSpecializationKind() const
Determine the kind of specialization that this declaration represents.
unsigned getNumTemplates() const
Returns the number of function templates that this might be a specialization of.
Definition: DeclTemplate.h:705
static bool classof(const Decl *D)
Definition: DeclTemplate.h:946
Common * getCommonPtr() const
const TemplateArgumentListInfo & getTemplateArgsInfo() const
FunctionTemplateDecl(ASTContext &C, DeclContext *DC, SourceLocation L, DeclarationName Name, TemplateParameterList *Params, NamedDecl *Decl)
Definition: DeclTemplate.h:991
bool hasDefaultArgument() const
Determine whether this template parameter has a default argument.
void init(NamedDecl *templatedDecl, TemplateParameterList *templateParams)
Initialize the underlying templated declaration and template parameters.
Definition: DeclTemplate.h:497
void setPointOfInstantiation(SourceLocation Loc)
FunctionTemplateDecl * getPreviousDecl()
Retrieve the previous declaration of this function template, or nullptr if no such declaration exists...
ClassTemplateDecl * getMostRecentDecl()
This template specialization was formed from a template-id but has not yet been declared, defined, or instantiated.
Definition: Specifiers.h: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:948
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:5073
NamedDecl * TemplatedDecl
Definition: DeclTemplate.h:468
void setTemplateSpecializationKind(TemplateSpecializationKind TSK)
Set the template specialization kind.
Definition: DeclTemplate.h:639
TemplateDecl(ConstrainedTemplateDeclInfo *CTDI, Kind DK, DeclContext *DC, SourceLocation L, DeclarationName Name, TemplateParameterList *Params)
Definition: DeclTemplate.h:405
Defines various enumerations that describe declaration and type specifiers.
Represents a template argument.
Definition: TemplateBase.h:51
SourceLocation getTemplateKeywordLoc() const
Gets the location of the template keyword, if present.
TagTypeKind
The kind of a tag type.
Definition: Type.h:4765
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:1256
llvm::PointerIntPair< RedeclarableTemplateDecl *, 1, bool > InstantiatedFromMember
The template from which this was most directly instantiated (or null).
Definition: DeclTemplate.h:813
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:962
static BuiltinTemplateDecl * Create(const ASTContext &C, DeclContext *DC, DeclarationName Name, BuiltinTemplateKind BTK)
Reads an AST files chain containing the contents of a translation unit.
Definition: ASTReader.h:354
static bool classof(const Decl *D)
void setInheritedDefaultArgument(const ASTContext &C, TemplateTypeParmDecl *Prev)
Set that this default argument was inherited from another parameter.
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:478
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:930
This template specialization was declared or defined by an explicit specialization (C++ [temp...
Definition: Specifiers.h:153
friend TrailingObjects
Definition: OpenMPClause.h:89
static DeclType * getDecl(FunctionTemplateSpecializationInfo *I)
Definition: DeclTemplate.h:957
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:854
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:777
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:936
Defines the C++ Decl subclasses, other than those for templates (found in DeclTemplate.h) and friends (in DeclFriend.h).
Defines the clang::SourceLocation class and associated facilities.
VarTemplateDecl * getMostRecentDecl()
static bool classof(const Decl *D)
Represents a C++ struct/union/class.
Definition: DeclCXX.h:300
TemplateParameterList(SourceLocation TemplateLoc, SourceLocation LAngleLoc, ArrayRef< NamedDecl *> Params, SourceLocation RAngleLoc, Expr *RequiresClause)
TypeAliasTemplateDecl * getInstantiatedFromMemberTemplate() const
Provides information a specialization of a member of a class template, which may be a member function...
Definition: DeclTemplate.h:608
static bool classof(const Decl *D)
VarTemplateDecl(ASTContext &C, DeclContext *DC, SourceLocation L, DeclarationName Name, TemplateParameterList *Params, NamedDecl *Decl)
SourceLocation getFriendLoc() const
Retrieves the location of the &#39;friend&#39; keyword.
Declaration of a class template.
VarTemplateDecl * getCanonicalDecl() override
Retrieves the canonical declaration of this template.
void removeDefaultArgument()
Removes the default argument of this template parameter.
static SpecIterator< EntryType > makeSpecIterator(llvm::FoldingSetVector< EntryType > &Specs, bool isEnd)
Definition: DeclTemplate.h:790
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.
This represents a decl that may have a name.
Definition: Decl.h:248
NamedDecl * getAsNamedDecl(TemplateParameter P)
const TemplateArgumentList & getTemplateInstantiationArgs() const
Retrieve the set of template arguments that should be used to instantiate members of the class templa...
Declaration of a friend template.
VarTemplatePartialSpecializationDecl * getInstantiatedFromMember() const
Retrieve the member variable template partial specialization from which this particular variable temp...
SourceLocation getPointOfInstantiation() const
Retrieve the first point of instantiation of this member.
Definition: DeclTemplate.h:648
NamedDecl * getFriendDecl() const
If this friend declaration names a templated function (or a member function of a templated type)...
void setTypeAsWritten(TypeSourceInfo *T)
Sets the type of this specialization as it was written by the user.
SourceLocation getPointOfInstantiation() const
Get the point of instantiation (if any), or null if none.
void setPointOfInstantiation(SourceLocation POI)
Set the first point of instantiation.
Definition: DeclTemplate.h:653
SourceLocation getBegin() const
const ASTTemplateArgumentListInfo * getTemplateArgsAsWritten() const
Get the template arguments as written.
SourceRange getSourceRange() const LLVM_READONLY
Definition: DeclTemplate.h:176
TrailingObjects::FixedSizeStorageOwner FixedSizeStorageOwner
Definition: DeclTemplate.h:182
Declaration of a template function.
Definition: DeclTemplate.h:968
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