Coverage Report

Created: 2019-07-24 05:18

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