Coverage Report

Created: 2018-09-21 05:35

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