Coverage Report

Created: 2022-01-18 06:27

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/AST/DeclTemplate.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- DeclTemplate.cpp - Template Declaration AST Node Implementation ----===//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
//
9
// This file implements the C++ related Decl classes for templates.
10
//
11
//===----------------------------------------------------------------------===//
12
13
#include "clang/AST/DeclTemplate.h"
14
#include "clang/AST/ASTContext.h"
15
#include "clang/AST/ASTMutationListener.h"
16
#include "clang/AST/DeclCXX.h"
17
#include "clang/AST/DeclarationName.h"
18
#include "clang/AST/Expr.h"
19
#include "clang/AST/ExternalASTSource.h"
20
#include "clang/AST/TemplateBase.h"
21
#include "clang/AST/TemplateName.h"
22
#include "clang/AST/Type.h"
23
#include "clang/AST/TypeLoc.h"
24
#include "clang/Basic/Builtins.h"
25
#include "clang/Basic/LLVM.h"
26
#include "clang/Basic/SourceLocation.h"
27
#include "llvm/ADT/ArrayRef.h"
28
#include "llvm/ADT/FoldingSet.h"
29
#include "llvm/ADT/None.h"
30
#include "llvm/ADT/PointerUnion.h"
31
#include "llvm/ADT/SmallVector.h"
32
#include "llvm/Support/Casting.h"
33
#include "llvm/Support/ErrorHandling.h"
34
#include <algorithm>
35
#include <cassert>
36
#include <cstdint>
37
#include <memory>
38
#include <utility>
39
40
using namespace clang;
41
42
//===----------------------------------------------------------------------===//
43
// TemplateParameterList Implementation
44
//===----------------------------------------------------------------------===//
45
46
47
TemplateParameterList::TemplateParameterList(const ASTContext& C,
48
                                             SourceLocation TemplateLoc,
49
                                             SourceLocation LAngleLoc,
50
                                             ArrayRef<NamedDecl *> Params,
51
                                             SourceLocation RAngleLoc,
52
                                             Expr *RequiresClause)
53
    : TemplateLoc(TemplateLoc), LAngleLoc(LAngleLoc), RAngleLoc(RAngleLoc),
54
      NumParams(Params.size()), ContainsUnexpandedParameterPack(false),
55
      HasRequiresClause(RequiresClause != nullptr),
56
2.58M
      HasConstrainedParameters(false) {
57
7.24M
  for (unsigned Idx = 0; Idx < NumParams; 
++Idx4.66M
) {
58
4.66M
    NamedDecl *P = Params[Idx];
59
4.66M
    begin()[Idx] = P;
60
61
4.66M
    bool IsPack = P->isTemplateParameterPack();
62
4.66M
    if (const auto *NTTP = dyn_cast<NonTypeTemplateParmDecl>(P)) {
63
563k
      if (!IsPack && 
NTTP->getType()->containsUnexpandedParameterPack()521k
)
64
52
        ContainsUnexpandedParameterPack = true;
65
563k
      if (NTTP->hasPlaceholderTypeConstraint())
66
3
        HasConstrainedParameters = true;
67
4.09M
    } else if (const auto *TTP = dyn_cast<TemplateTemplateParmDecl>(P)) {
68
33.2k
      if (!IsPack &&
69
33.2k
          
TTP->getTemplateParameters()->containsUnexpandedParameterPack()33.1k
)
70
0
        ContainsUnexpandedParameterPack = true;
71
4.06M
    } else if (const auto *TTP = dyn_cast<TemplateTypeParmDecl>(P)) {
72
4.06M
      if (const TypeConstraint *TC = TTP->getTypeConstraint()) {
73
279
        if (TC->getImmediatelyDeclaredConstraint()
74
279
            ->containsUnexpandedParameterPack())
75
5
          ContainsUnexpandedParameterPack = true;
76
279
      }
77
4.06M
      if (TTP->hasTypeConstraint())
78
303
        HasConstrainedParameters = true;
79
4.06M
    } else {
80
0
      llvm_unreachable("unexpected template parameter type");
81
0
    }
82
    // FIXME: If a default argument contains an unexpanded parameter pack, the
83
    // template parameter list does too.
84
4.66M
  }
85
86
2.58M
  if (HasRequiresClause) {
87
335
    if (RequiresClause->containsUnexpandedParameterPack())
88
2
      ContainsUnexpandedParameterPack = true;
89
335
    *getTrailingObjects<Expr *>() = RequiresClause;
90
335
  }
91
2.58M
}
92
93
33.3k
bool TemplateParameterList::containsUnexpandedParameterPack() const {
94
33.3k
  if (ContainsUnexpandedParameterPack)
95
27
    return true;
96
33.2k
  if (!HasConstrainedParameters)
97
33.2k
    return false;
98
99
  // An implicit constrained parameter might have had a use of an unexpanded
100
  // pack added to it after the template parameter list was created. All
101
  // implicit parameters are at the end of the parameter list.
102
46
  for (const NamedDecl *Param : llvm::reverse(asArray())) {
103
46
    if (!Param->isImplicit())
104
46
      break;
105
106
0
    if (const auto *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) {
107
0
      const auto *TC = TTP->getTypeConstraint();
108
0
      if (TC && TC->getImmediatelyDeclaredConstraint()
109
0
                    ->containsUnexpandedParameterPack())
110
0
        return true;
111
0
    }
112
0
  }
113
114
46
  return false;
115
46
}
116
117
TemplateParameterList *
118
TemplateParameterList::Create(const ASTContext &C, SourceLocation TemplateLoc,
119
                              SourceLocation LAngleLoc,
120
                              ArrayRef<NamedDecl *> Params,
121
2.55M
                              SourceLocation RAngleLoc, Expr *RequiresClause) {
122
2.55M
  void *Mem = C.Allocate(totalSizeToAlloc<NamedDecl *, Expr *>(
123
2.55M
                             Params.size(), RequiresClause ? 
1u335
:
0u2.55M
),
124
2.55M
                         alignof(TemplateParameterList));
125
2.55M
  return new (Mem) TemplateParameterList(C, TemplateLoc, LAngleLoc, Params,
126
2.55M
                                         RAngleLoc, RequiresClause);
127
2.55M
}
128
129
4.59M
unsigned TemplateParameterList::getMinRequiredArguments() const {
130
4.59M
  unsigned NumRequiredArgs = 0;
131
8.46M
  for (const NamedDecl *P : asArray()) {
132
8.46M
    if (P->isTemplateParameterPack()) {
133
269k
      if (Optional<unsigned> Expansions = getExpandedPackSize(P)) {
134
0
        NumRequiredArgs += *Expansions;
135
0
        continue;
136
0
      }
137
269k
      break;
138
269k
    }
139
140
8.20M
    if (const auto *TTP = dyn_cast<TemplateTypeParmDecl>(P)) {
141
7.18M
      if (TTP->hasDefaultArgument())
142
744k
        break;
143
7.18M
    } else 
if (const auto *1.01M
NTTP1.01M
= dyn_cast<NonTypeTemplateParmDecl>(P)) {
144
1.00M
      if (NTTP->hasDefaultArgument())
145
298k
        break;
146
1.00M
    } else 
if (7.72k
cast<TemplateTemplateParmDecl>(P)->hasDefaultArgument()7.72k
)
147
246
      break;
148
149
7.15M
    ++NumRequiredArgs;
150
7.15M
  }
151
152
4.59M
  return NumRequiredArgs;
153
4.59M
}
154
155
3.09M
unsigned TemplateParameterList::getDepth() const {
156
3.09M
  if (size() == 0)
157
2.22k
    return 0;
158
159
3.09M
  const NamedDecl *FirstParm = getParam(0);
160
3.09M
  if (const auto *TTP = dyn_cast<TemplateTypeParmDecl>(FirstParm))
161
2.44M
    return TTP->getDepth();
162
647k
  else if (const auto *NTTP = dyn_cast<NonTypeTemplateParmDecl>(FirstParm))
163
631k
    return NTTP->getDepth();
164
15.9k
  else
165
15.9k
    return cast<TemplateTemplateParmDecl>(FirstParm)->getDepth();
166
3.09M
}
167
168
static bool AdoptTemplateParameterList(TemplateParameterList *Params,
169
1.48M
                                       DeclContext *Owner) {
170
1.48M
  bool Invalid = false;
171
2.70M
  for (NamedDecl *P : *Params) {
172
2.70M
    P->setDeclContext(Owner);
173
174
2.70M
    if (const auto *TTP = dyn_cast<TemplateTemplateParmDecl>(P))
175
27.4k
      if (AdoptTemplateParameterList(TTP->getTemplateParameters(), Owner))
176
0
        Invalid = true;
177
178
2.70M
    if (P->isInvalidDecl())
179
75
      Invalid = true;
180
2.70M
  }
181
1.48M
  return Invalid;
182
1.48M
}
183
184
void TemplateParameterList::
185
7.51M
getAssociatedConstraints(llvm::SmallVectorImpl<const Expr *> &AC) const {
186
7.51M
  if (HasConstrainedParameters)
187
295
    
for (const NamedDecl *Param : *this)250
{
188
295
      if (const auto *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) {
189
290
        if (const auto *TC = TTP->getTypeConstraint())
190
277
          AC.push_back(TC->getImmediatelyDeclaredConstraint());
191
290
      } else 
if (const auto *5
NTTP5
= dyn_cast<NonTypeTemplateParmDecl>(Param)) {
192
5
        if (const Expr *E = NTTP->getPlaceholderTypeConstraint())
193
5
          AC.push_back(E);
194
5
      }
195
295
    }
196
7.51M
  if (HasRequiresClause)
197
565
    AC.push_back(getRequiresClause());
198
7.51M
}
199
200
307
bool TemplateParameterList::hasAssociatedConstraints() const {
201
307
  return HasRequiresClause || 
HasConstrainedParameters283
;
202
307
}
203
204
bool TemplateParameterList::shouldIncludeTypeForArgument(
205
    const PrintingPolicy &Policy, const TemplateParameterList *TPL,
206
1.79M
    unsigned Idx) {
207
1.79M
  if (!TPL || 
Idx >= TPL->size()1.49M
||
Policy.AlwaysIncludeTypeForTemplateArgument1.49M
)
208
672k
    return true;
209
1.11M
  const NamedDecl *TemplParam = TPL->getParam(Idx);
210
1.11M
  if (const auto *ParamValueDecl =
211
1.11M
          dyn_cast<NonTypeTemplateParmDecl>(TemplParam))
212
124k
    if (ParamValueDecl->getType()->getContainedDeducedType())
213
431
      return true;
214
1.11M
  return false;
215
1.11M
}
216
217
namespace clang {
218
219
205
void *allocateDefaultArgStorageChain(const ASTContext &C) {
220
205
  return new (C) char[sizeof(void*) * 2];
221
205
}
222
223
} // namespace clang
224
225
//===----------------------------------------------------------------------===//
226
// TemplateDecl Implementation
227
//===----------------------------------------------------------------------===//
228
229
TemplateDecl::TemplateDecl(Kind DK, DeclContext *DC, SourceLocation L,
230
                           DeclarationName Name, TemplateParameterList *Params,
231
                           NamedDecl *Decl)
232
1.86M
    : NamedDecl(DK, DC, L, Name), TemplatedDecl(Decl), TemplateParams(Params) {}
233
234
0
void TemplateDecl::anchor() {}
235
236
void TemplateDecl::
237
7.31M
getAssociatedConstraints(llvm::SmallVectorImpl<const Expr *> &AC) const {
238
7.31M
  TemplateParams->getAssociatedConstraints(AC);
239
7.31M
  if (auto *FD = dyn_cast_or_null<FunctionDecl>(getTemplatedDecl()))
240
404k
    if (const Expr *TRC = FD->getTrailingRequiresClause())
241
144
      AC.push_back(TRC);
242
7.31M
}
243
244
280
bool TemplateDecl::hasAssociatedConstraints() const {
245
280
  if (TemplateParams->hasAssociatedConstraints())
246
7
    return true;
247
273
  if (auto *FD = dyn_cast_or_null<FunctionDecl>(getTemplatedDecl()))
248
0
    return FD->getTrailingRequiresClause();
249
273
  return false;
250
273
}
251
252
//===----------------------------------------------------------------------===//
253
// RedeclarableTemplateDecl Implementation
254
//===----------------------------------------------------------------------===//
255
256
0
void RedeclarableTemplateDecl::anchor() {}
257
258
19.9M
RedeclarableTemplateDecl::CommonBase *RedeclarableTemplateDecl::getCommonPtr() const {
259
19.9M
  if (Common)
260
18.8M
    return Common;
261
262
  // Walk the previous-declaration chain until we either find a declaration
263
  // with a common pointer or we run out of previous declarations.
264
1.07M
  SmallVector<const RedeclarableTemplateDecl *, 2> PrevDecls;
265
1.09M
  for (const RedeclarableTemplateDecl *Prev = getPreviousDecl(); Prev;
266
1.07M
       
Prev = Prev->getPreviousDecl()22.1k
) {
267
83.0k
    if (Prev->Common) {
268
60.9k
      Common = Prev->Common;
269
60.9k
      break;
270
60.9k
    }
271
272
22.1k
    PrevDecls.push_back(Prev);
273
22.1k
  }
274
275
  // If we never found a common pointer, allocate one now.
276
1.07M
  if (!Common) {
277
    // FIXME: If any of the declarations is from an AST file, we probably
278
    // need an update record to add the common data.
279
280
1.01M
    Common = newCommon(getASTContext());
281
1.01M
  }
282
283
  // Update any previous declarations we saw with the common pointer.
284
1.07M
  for (const RedeclarableTemplateDecl *Prev : PrevDecls)
285
22.1k
    Prev->Common = Common;
286
287
1.07M
  return Common;
288
19.9M
}
289
290
5.57M
void RedeclarableTemplateDecl::loadLazySpecializationsImpl() const {
291
  // Grab the most recent declaration to ensure we've loaded any lazy
292
  // redeclarations of this template.
293
5.57M
  CommonBase *CommonBasePtr = getMostRecentDecl()->getCommonPtr();
294
5.57M
  if (CommonBasePtr->LazySpecializations) {
295
51.5k
    ASTContext &Context = getASTContext();
296
51.5k
    uint32_t *Specs = CommonBasePtr->LazySpecializations;
297
51.5k
    CommonBasePtr->LazySpecializations = nullptr;
298
1.12M
    for (uint32_t I = 0, N = *Specs++; I != N; 
++I1.07M
)
299
1.07M
      (void)Context.getExternalSource()->GetExternalDecl(Specs[I]);
300
51.5k
  }
301
5.57M
}
302
303
template<class EntryType, typename... ProfileArguments>
304
typename RedeclarableTemplateDecl::SpecEntryTraits<EntryType>::DeclType *
305
RedeclarableTemplateDecl::findSpecializationImpl(
306
    llvm::FoldingSetVector<EntryType> &Specs, void *&InsertPos,
307
4.01M
    ProfileArguments&&... ProfileArgs) {
308
4.01M
  using SETraits = SpecEntryTraits<EntryType>;
309
310
4.01M
  llvm::FoldingSetNodeID ID;
311
4.01M
  EntryType::Profile(ID, std::forward<ProfileArguments>(ProfileArgs)...,
312
4.01M
                     getASTContext());
313
4.01M
  EntryType *Entry = Specs.FindNodeOrInsertPos(ID, InsertPos);
314
4.01M
  return Entry ? 
SETraits::getDecl(Entry)->getMostRecentDecl()1.60M
:
nullptr2.40M
;
315
4.01M
}
clang::RedeclarableTemplateDecl::SpecEntryTraits<clang::FunctionTemplateSpecializationInfo>::DeclType* clang::RedeclarableTemplateDecl::findSpecializationImpl<clang::FunctionTemplateSpecializationInfo, llvm::ArrayRef<clang::TemplateArgument>&>(llvm::FoldingSetVector<clang::FunctionTemplateSpecializationInfo, llvm::SmallVector<clang::FunctionTemplateSpecializationInfo*, 8u> >&, void*&, llvm::ArrayRef<clang::TemplateArgument>&)
Line
Count
Source
307
458k
    ProfileArguments&&... ProfileArgs) {
308
458k
  using SETraits = SpecEntryTraits<EntryType>;
309
310
458k
  llvm::FoldingSetNodeID ID;
311
458k
  EntryType::Profile(ID, std::forward<ProfileArguments>(ProfileArgs)...,
312
458k
                     getASTContext());
313
458k
  EntryType *Entry = Specs.FindNodeOrInsertPos(ID, InsertPos);
314
458k
  return Entry ? 
SETraits::getDecl(Entry)->getMostRecentDecl()179k
:
nullptr279k
;
315
458k
}
Unexecuted instantiation: clang::RedeclarableTemplateDecl::SpecEntryTraits<clang::FunctionTemplateSpecializationInfo>::DeclType* clang::RedeclarableTemplateDecl::findSpecializationImpl<clang::FunctionTemplateSpecializationInfo, llvm::ArrayRef<clang::TemplateArgument> >(llvm::FoldingSetVector<clang::FunctionTemplateSpecializationInfo, llvm::SmallVector<clang::FunctionTemplateSpecializationInfo*, 8u> >&, void*&, llvm::ArrayRef<clang::TemplateArgument>&&)
clang::RedeclarableTemplateDecl::SpecEntryTraits<clang::ClassTemplateSpecializationDecl>::DeclType* clang::RedeclarableTemplateDecl::findSpecializationImpl<clang::ClassTemplateSpecializationDecl, llvm::ArrayRef<clang::TemplateArgument>&>(llvm::FoldingSetVector<clang::ClassTemplateSpecializationDecl, llvm::SmallVector<clang::ClassTemplateSpecializationDecl*, 8u> >&, void*&, llvm::ArrayRef<clang::TemplateArgument>&)
Line
Count
Source
307
2.43M
    ProfileArguments&&... ProfileArgs) {
308
2.43M
  using SETraits = SpecEntryTraits<EntryType>;
309
310
2.43M
  llvm::FoldingSetNodeID ID;
311
2.43M
  EntryType::Profile(ID, std::forward<ProfileArguments>(ProfileArgs)...,
312
2.43M
                     getASTContext());
313
2.43M
  EntryType *Entry = Specs.FindNodeOrInsertPos(ID, InsertPos);
314
2.43M
  return Entry ? 
SETraits::getDecl(Entry)->getMostRecentDecl()1.42M
:
nullptr1.00M
;
315
2.43M
}
clang::RedeclarableTemplateDecl::SpecEntryTraits<clang::ClassTemplateSpecializationDecl>::DeclType* clang::RedeclarableTemplateDecl::findSpecializationImpl<clang::ClassTemplateSpecializationDecl, llvm::ArrayRef<clang::TemplateArgument> >(llvm::FoldingSetVector<clang::ClassTemplateSpecializationDecl, llvm::SmallVector<clang::ClassTemplateSpecializationDecl*, 8u> >&, void*&, llvm::ArrayRef<clang::TemplateArgument>&&)
Line
Count
Source
307
948k
    ProfileArguments&&... ProfileArgs) {
308
948k
  using SETraits = SpecEntryTraits<EntryType>;
309
310
948k
  llvm::FoldingSetNodeID ID;
311
948k
  EntryType::Profile(ID, std::forward<ProfileArguments>(ProfileArgs)...,
312
948k
                     getASTContext());
313
948k
  EntryType *Entry = Specs.FindNodeOrInsertPos(ID, InsertPos);
314
948k
  return Entry ? 
SETraits::getDecl(Entry)->getMostRecentDecl()0
: nullptr;
315
948k
}
clang::RedeclarableTemplateDecl::SpecEntryTraits<clang::ClassTemplatePartialSpecializationDecl>::DeclType* clang::RedeclarableTemplateDecl::findSpecializationImpl<clang::ClassTemplatePartialSpecializationDecl, llvm::ArrayRef<clang::TemplateArgument>&, clang::TemplateParameterList*&>(llvm::FoldingSetVector<clang::ClassTemplatePartialSpecializationDecl, llvm::SmallVector<clang::ClassTemplatePartialSpecializationDecl*, 8u> >&, void*&, llvm::ArrayRef<clang::TemplateArgument>&, clang::TemplateParameterList*&)
Line
Count
Source
307
161k
    ProfileArguments&&... ProfileArgs) {
308
161k
  using SETraits = SpecEntryTraits<EntryType>;
309
310
161k
  llvm::FoldingSetNodeID ID;
311
161k
  EntryType::Profile(ID, std::forward<ProfileArguments>(ProfileArgs)...,
312
161k
                     getASTContext());
313
161k
  EntryType *Entry = Specs.FindNodeOrInsertPos(ID, InsertPos);
314
161k
  return Entry ? 
SETraits::getDecl(Entry)->getMostRecentDecl()584
:
nullptr161k
;
315
161k
}
clang::RedeclarableTemplateDecl::SpecEntryTraits<clang::VarTemplateSpecializationDecl>::DeclType* clang::RedeclarableTemplateDecl::findSpecializationImpl<clang::VarTemplateSpecializationDecl, llvm::ArrayRef<clang::TemplateArgument>&>(llvm::FoldingSetVector<clang::VarTemplateSpecializationDecl, llvm::SmallVector<clang::VarTemplateSpecializationDecl*, 8u> >&, void*&, llvm::ArrayRef<clang::TemplateArgument>&)
Line
Count
Source
307
6.11k
    ProfileArguments&&... ProfileArgs) {
308
6.11k
  using SETraits = SpecEntryTraits<EntryType>;
309
310
6.11k
  llvm::FoldingSetNodeID ID;
311
6.11k
  EntryType::Profile(ID, std::forward<ProfileArguments>(ProfileArgs)...,
312
6.11k
                     getASTContext());
313
6.11k
  EntryType *Entry = Specs.FindNodeOrInsertPos(ID, InsertPos);
314
6.11k
  return Entry ? 
SETraits::getDecl(Entry)->getMostRecentDecl()892
:
nullptr5.21k
;
315
6.11k
}
clang::RedeclarableTemplateDecl::SpecEntryTraits<clang::VarTemplateSpecializationDecl>::DeclType* clang::RedeclarableTemplateDecl::findSpecializationImpl<clang::VarTemplateSpecializationDecl, llvm::ArrayRef<clang::TemplateArgument> >(llvm::FoldingSetVector<clang::VarTemplateSpecializationDecl, llvm::SmallVector<clang::VarTemplateSpecializationDecl*, 8u> >&, void*&, llvm::ArrayRef<clang::TemplateArgument>&&)
Line
Count
Source
307
2.84k
    ProfileArguments&&... ProfileArgs) {
308
2.84k
  using SETraits = SpecEntryTraits<EntryType>;
309
310
2.84k
  llvm::FoldingSetNodeID ID;
311
2.84k
  EntryType::Profile(ID, std::forward<ProfileArguments>(ProfileArgs)...,
312
2.84k
                     getASTContext());
313
2.84k
  EntryType *Entry = Specs.FindNodeOrInsertPos(ID, InsertPos);
314
2.84k
  return Entry ? 
SETraits::getDecl(Entry)->getMostRecentDecl()0
: nullptr;
315
2.84k
}
clang::RedeclarableTemplateDecl::SpecEntryTraits<clang::VarTemplatePartialSpecializationDecl>::DeclType* clang::RedeclarableTemplateDecl::findSpecializationImpl<clang::VarTemplatePartialSpecializationDecl, llvm::ArrayRef<clang::TemplateArgument>&, clang::TemplateParameterList*&>(llvm::FoldingSetVector<clang::VarTemplatePartialSpecializationDecl, llvm::SmallVector<clang::VarTemplatePartialSpecializationDecl*, 8u> >&, void*&, llvm::ArrayRef<clang::TemplateArgument>&, clang::TemplateParameterList*&)
Line
Count
Source
307
411
    ProfileArguments&&... ProfileArgs) {
308
411
  using SETraits = SpecEntryTraits<EntryType>;
309
310
411
  llvm::FoldingSetNodeID ID;
311
411
  EntryType::Profile(ID, std::forward<ProfileArguments>(ProfileArgs)...,
312
411
                     getASTContext());
313
411
  EntryType *Entry = Specs.FindNodeOrInsertPos(ID, InsertPos);
314
411
  return Entry ? 
SETraits::getDecl(Entry)->getMostRecentDecl()65
:
nullptr346
;
315
411
}
316
317
template<class Derived, class EntryType>
318
void RedeclarableTemplateDecl::addSpecializationImpl(
319
    llvm::FoldingSetVector<EntryType> &Specializations, EntryType *Entry,
320
1.19M
    void *InsertPos) {
321
1.19M
  using SETraits = SpecEntryTraits<EntryType>;
322
323
1.19M
  if (InsertPos) {
324
951k
#ifndef NDEBUG
325
951k
    void *CorrectInsertPos;
326
951k
    assert(!findSpecializationImpl(Specializations,
327
951k
                                   CorrectInsertPos,
328
951k
                                   SETraits::getTemplateArgs(Entry)) &&
329
951k
           InsertPos == CorrectInsertPos &&
330
951k
           "given incorrect InsertPos for specialization");
331
0
#endif
332
0
    Specializations.InsertNode(Entry, InsertPos);
333
951k
  } else {
334
239k
    EntryType *Existing = Specializations.GetOrInsertNode(Entry);
335
239k
    (void)Existing;
336
239k
    assert(SETraits::getDecl(Existing)->isCanonicalDecl() &&
337
239k
           "non-canonical specialization?");
338
239k
  }
339
340
1.19M
  if (ASTMutationListener *L = getASTMutationListener())
341
47.1k
    L->AddedCXXTemplateSpecialization(cast<Derived>(this),
342
47.1k
                                      SETraits::getDecl(Entry));
343
1.19M
}
void clang::RedeclarableTemplateDecl::addSpecializationImpl<clang::FunctionTemplateDecl, clang::FunctionTemplateSpecializationInfo>(llvm::FoldingSetVector<clang::FunctionTemplateSpecializationInfo, llvm::SmallVector<clang::FunctionTemplateSpecializationInfo*, 8u> >&, clang::FunctionTemplateSpecializationInfo*, void*)
Line
Count
Source
320
239k
    void *InsertPos) {
321
239k
  using SETraits = SpecEntryTraits<EntryType>;
322
323
239k
  if (InsertPos) {
324
0
#ifndef NDEBUG
325
0
    void *CorrectInsertPos;
326
0
    assert(!findSpecializationImpl(Specializations,
327
0
                                   CorrectInsertPos,
328
0
                                   SETraits::getTemplateArgs(Entry)) &&
329
0
           InsertPos == CorrectInsertPos &&
330
0
           "given incorrect InsertPos for specialization");
331
0
#endif
332
0
    Specializations.InsertNode(Entry, InsertPos);
333
239k
  } else {
334
239k
    EntryType *Existing = Specializations.GetOrInsertNode(Entry);
335
239k
    (void)Existing;
336
239k
    assert(SETraits::getDecl(Existing)->isCanonicalDecl() &&
337
239k
           "non-canonical specialization?");
338
239k
  }
339
340
239k
  if (ASTMutationListener *L = getASTMutationListener())
341
9.49k
    L->AddedCXXTemplateSpecialization(cast<Derived>(this),
342
9.49k
                                      SETraits::getDecl(Entry));
343
239k
}
void clang::RedeclarableTemplateDecl::addSpecializationImpl<clang::ClassTemplateDecl, clang::ClassTemplateSpecializationDecl>(llvm::FoldingSetVector<clang::ClassTemplateSpecializationDecl, llvm::SmallVector<clang::ClassTemplateSpecializationDecl*, 8u> >&, clang::ClassTemplateSpecializationDecl*, void*)
Line
Count
Source
320
948k
    void *InsertPos) {
321
948k
  using SETraits = SpecEntryTraits<EntryType>;
322
323
948k
  if (InsertPos) {
324
948k
#ifndef NDEBUG
325
948k
    void *CorrectInsertPos;
326
948k
    assert(!findSpecializationImpl(Specializations,
327
948k
                                   CorrectInsertPos,
328
948k
                                   SETraits::getTemplateArgs(Entry)) &&
329
948k
           InsertPos == CorrectInsertPos &&
330
948k
           "given incorrect InsertPos for specialization");
331
0
#endif
332
0
    Specializations.InsertNode(Entry, InsertPos);
333
948k
  } else {
334
0
    EntryType *Existing = Specializations.GetOrInsertNode(Entry);
335
0
    (void)Existing;
336
0
    assert(SETraits::getDecl(Existing)->isCanonicalDecl() &&
337
0
           "non-canonical specialization?");
338
0
  }
339
340
948k
  if (ASTMutationListener *L = getASTMutationListener())
341
37.5k
    L->AddedCXXTemplateSpecialization(cast<Derived>(this),
342
37.5k
                                      SETraits::getDecl(Entry));
343
948k
}
void clang::RedeclarableTemplateDecl::addSpecializationImpl<clang::VarTemplateDecl, clang::VarTemplateSpecializationDecl>(llvm::FoldingSetVector<clang::VarTemplateSpecializationDecl, llvm::SmallVector<clang::VarTemplateSpecializationDecl*, 8u> >&, clang::VarTemplateSpecializationDecl*, void*)
Line
Count
Source
320
2.84k
    void *InsertPos) {
321
2.84k
  using SETraits = SpecEntryTraits<EntryType>;
322
323
2.84k
  if (InsertPos) {
324
2.84k
#ifndef NDEBUG
325
2.84k
    void *CorrectInsertPos;
326
2.84k
    assert(!findSpecializationImpl(Specializations,
327
2.84k
                                   CorrectInsertPos,
328
2.84k
                                   SETraits::getTemplateArgs(Entry)) &&
329
2.84k
           InsertPos == CorrectInsertPos &&
330
2.84k
           "given incorrect InsertPos for specialization");
331
0
#endif
332
0
    Specializations.InsertNode(Entry, InsertPos);
333
2.84k
  } else {
334
0
    EntryType *Existing = Specializations.GetOrInsertNode(Entry);
335
0
    (void)Existing;
336
0
    assert(SETraits::getDecl(Existing)->isCanonicalDecl() &&
337
0
           "non-canonical specialization?");
338
0
  }
339
340
2.84k
  if (ASTMutationListener *L = getASTMutationListener())
341
113
    L->AddedCXXTemplateSpecialization(cast<Derived>(this),
342
113
                                      SETraits::getDecl(Entry));
343
2.84k
}
344
345
//===----------------------------------------------------------------------===//
346
// FunctionTemplateDecl Implementation
347
//===----------------------------------------------------------------------===//
348
349
FunctionTemplateDecl *
350
FunctionTemplateDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
351
                             DeclarationName Name,
352
884k
                             TemplateParameterList *Params, NamedDecl *Decl) {
353
884k
  bool Invalid = AdoptTemplateParameterList(Params, cast<DeclContext>(Decl));
354
884k
  auto *TD = new (C, DC) FunctionTemplateDecl(C, DC, L, Name, Params, Decl);
355
884k
  if (Invalid)
356
1
    TD->setInvalidDecl();
357
884k
  return TD;
358
884k
}
359
360
FunctionTemplateDecl *FunctionTemplateDecl::CreateDeserialized(ASTContext &C,
361
285k
                                                               unsigned ID) {
362
285k
  return new (C, ID) FunctionTemplateDecl(C, nullptr, SourceLocation(),
363
285k
                                          DeclarationName(), nullptr, nullptr);
364
285k
}
365
366
RedeclarableTemplateDecl::CommonBase *
367
935k
FunctionTemplateDecl::newCommon(ASTContext &C) const {
368
935k
  auto *CommonPtr = new (C) Common;
369
935k
  C.addDestruction(CommonPtr);
370
935k
  return CommonPtr;
371
935k
}
372
373
762k
void FunctionTemplateDecl::LoadLazySpecializations() const {
374
762k
  loadLazySpecializationsImpl();
375
762k
}
376
377
llvm::FoldingSetVector<FunctionTemplateSpecializationInfo> &
378
734k
FunctionTemplateDecl::getSpecializations() const {
379
734k
  LoadLazySpecializations();
380
734k
  return getCommonPtr()->Specializations;
381
734k
}
382
383
FunctionDecl *
384
FunctionTemplateDecl::findSpecialization(ArrayRef<TemplateArgument> Args,
385
458k
                                         void *&InsertPos) {
386
458k
  return findSpecializationImpl(getSpecializations(), InsertPos, Args);
387
458k
}
388
389
void FunctionTemplateDecl::addSpecialization(
390
239k
      FunctionTemplateSpecializationInfo *Info, void *InsertPos) {
391
239k
  addSpecializationImpl<FunctionTemplateDecl>(getSpecializations(), Info,
392
239k
                                              InsertPos);
393
239k
}
394
395
0
ArrayRef<TemplateArgument> FunctionTemplateDecl::getInjectedTemplateArgs() {
396
0
  TemplateParameterList *Params = getTemplateParameters();
397
0
  Common *CommonPtr = getCommonPtr();
398
0
  if (!CommonPtr->InjectedArgs) {
399
0
    auto &Context = getASTContext();
400
0
    SmallVector<TemplateArgument, 16> TemplateArgs;
401
0
    Context.getInjectedTemplateArgs(Params, TemplateArgs);
402
0
    CommonPtr->InjectedArgs =
403
0
        new (Context) TemplateArgument[TemplateArgs.size()];
404
0
    std::copy(TemplateArgs.begin(), TemplateArgs.end(),
405
0
              CommonPtr->InjectedArgs);
406
0
  }
407
408
0
  return llvm::makeArrayRef(CommonPtr->InjectedArgs, Params->size());
409
0
}
410
411
47.4k
void FunctionTemplateDecl::mergePrevDecl(FunctionTemplateDecl *Prev) {
412
47.4k
  using Base = RedeclarableTemplateDecl;
413
414
  // If we haven't created a common pointer yet, then it can just be created
415
  // with the usual method.
416
47.4k
  if (!Base::Common)
417
40.1k
    return;
418
419
7.32k
  Common *ThisCommon = static_cast<Common *>(Base::Common);
420
7.32k
  Common *PrevCommon = nullptr;
421
7.32k
  SmallVector<FunctionTemplateDecl *, 8> PreviousDecls;
422
7.34k
  for (; Prev; 
Prev = Prev->getPreviousDecl()18
) {
423
7.32k
    if (Prev->Base::Common) {
424
7.30k
      PrevCommon = static_cast<Common *>(Prev->Base::Common);
425
7.30k
      break;
426
7.30k
    }
427
18
    PreviousDecls.push_back(Prev);
428
18
  }
429
430
  // If the previous redecl chain hasn't created a common pointer yet, then just
431
  // use this common pointer.
432
7.32k
  if (!PrevCommon) {
433
18
    for (auto *D : PreviousDecls)
434
18
      D->Base::Common = ThisCommon;
435
18
    return;
436
18
  }
437
438
  // Ensure we don't leak any important state.
439
7.30k
  assert(ThisCommon->Specializations.size() == 0 &&
440
7.30k
         "Can't merge incompatible declarations!");
441
442
0
  Base::Common = PrevCommon;
443
7.30k
}
444
445
//===----------------------------------------------------------------------===//
446
// ClassTemplateDecl Implementation
447
//===----------------------------------------------------------------------===//
448
449
ClassTemplateDecl *ClassTemplateDecl::Create(ASTContext &C, DeclContext *DC,
450
                                             SourceLocation L,
451
                                             DeclarationName Name,
452
                                             TemplateParameterList *Params,
453
333k
                                             NamedDecl *Decl) {
454
333k
  bool Invalid = AdoptTemplateParameterList(Params, cast<DeclContext>(Decl));
455
333k
  auto *TD = new (C, DC) ClassTemplateDecl(C, DC, L, Name, Params, Decl);
456
333k
  if (Invalid)
457
73
    TD->setInvalidDecl();
458
333k
  return TD;
459
333k
}
460
461
ClassTemplateDecl *ClassTemplateDecl::CreateDeserialized(ASTContext &C,
462
226k
                                                         unsigned ID) {
463
226k
  return new (C, ID) ClassTemplateDecl(C, nullptr, SourceLocation(),
464
226k
                                       DeclarationName(), nullptr, nullptr);
465
226k
}
466
467
4.79M
void ClassTemplateDecl::LoadLazySpecializations() const {
468
4.79M
  loadLazySpecializationsImpl();
469
4.79M
}
470
471
llvm::FoldingSetVector<ClassTemplateSpecializationDecl> &
472
3.39M
ClassTemplateDecl::getSpecializations() const {
473
3.39M
  LoadLazySpecializations();
474
3.39M
  return getCommonPtr()->Specializations;
475
3.39M
}
476
477
llvm::FoldingSetVector<ClassTemplatePartialSpecializationDecl> &
478
1.19M
ClassTemplateDecl::getPartialSpecializations() const {
479
1.19M
  LoadLazySpecializations();
480
1.19M
  return getCommonPtr()->PartialSpecializations;
481
1.19M
}
482
483
RedeclarableTemplateDecl::CommonBase *
484
429k
ClassTemplateDecl::newCommon(ASTContext &C) const {
485
429k
  auto *CommonPtr = new (C) Common;
486
429k
  C.addDestruction(CommonPtr);
487
429k
  return CommonPtr;
488
429k
}
489
490
ClassTemplateSpecializationDecl *
491
ClassTemplateDecl::findSpecialization(ArrayRef<TemplateArgument> Args,
492
2.43M
                                      void *&InsertPos) {
493
2.43M
  return findSpecializationImpl(getSpecializations(), InsertPos, Args);
494
2.43M
}
495
496
void ClassTemplateDecl::AddSpecialization(ClassTemplateSpecializationDecl *D,
497
948k
                                          void *InsertPos) {
498
948k
  addSpecializationImpl<ClassTemplateDecl>(getSpecializations(), D, InsertPos);
499
948k
}
500
501
ClassTemplatePartialSpecializationDecl *
502
ClassTemplateDecl::findPartialSpecialization(
503
    ArrayRef<TemplateArgument> Args,
504
161k
    TemplateParameterList *TPL, void *&InsertPos) {
505
161k
  return findSpecializationImpl(getPartialSpecializations(), InsertPos, Args,
506
161k
                                TPL);
507
161k
}
508
509
static void ProfileTemplateParameterList(ASTContext &C,
510
209k
    llvm::FoldingSetNodeID &ID, const TemplateParameterList *TPL) {
511
209k
  const Expr *RC = TPL->getRequiresClause();
512
209k
  ID.AddBoolean(RC != nullptr);
513
209k
  if (RC)
514
34
    RC->Profile(ID, C, /*Canonical=*/true);
515
209k
  ID.AddInteger(TPL->size());
516
387k
  for (NamedDecl *D : *TPL) {
517
387k
    if (const auto *NTTP = dyn_cast<NonTypeTemplateParmDecl>(D)) {
518
58.9k
      ID.AddInteger(0);
519
58.9k
      ID.AddBoolean(NTTP->isParameterPack());
520
58.9k
      NTTP->getType().getCanonicalType().Profile(ID);
521
58.9k
      continue;
522
58.9k
    }
523
328k
    if (const auto *TTP = dyn_cast<TemplateTypeParmDecl>(D)) {
524
324k
      ID.AddInteger(1);
525
324k
      ID.AddBoolean(TTP->isParameterPack());
526
324k
      ID.AddBoolean(TTP->hasTypeConstraint());
527
324k
      if (const TypeConstraint *TC = TTP->getTypeConstraint())
528
9
        TC->getImmediatelyDeclaredConstraint()->Profile(ID, C,
529
9
                                                        /*Canonical=*/true);
530
324k
      continue;
531
324k
    }
532
3.94k
    const auto *TTP = cast<TemplateTemplateParmDecl>(D);
533
3.94k
    ID.AddInteger(2);
534
3.94k
    ID.AddBoolean(TTP->isParameterPack());
535
3.94k
    ProfileTemplateParameterList(C, ID, TTP->getTemplateParameters());
536
3.94k
  }
537
209k
}
538
539
void
540
ClassTemplatePartialSpecializationDecl::Profile(llvm::FoldingSetNodeID &ID,
541
    ArrayRef<TemplateArgument> TemplateArgs, TemplateParameterList *TPL,
542
205k
    ASTContext &Context) {
543
205k
  ID.AddInteger(TemplateArgs.size());
544
205k
  for (const TemplateArgument &TemplateArg : TemplateArgs)
545
447k
    TemplateArg.Profile(ID, Context);
546
205k
  ProfileTemplateParameterList(Context, ID, TPL);
547
205k
}
548
549
void ClassTemplateDecl::AddPartialSpecialization(
550
                                      ClassTemplatePartialSpecializationDecl *D,
551
160k
                                      void *InsertPos) {
552
160k
  if (InsertPos)
553
154k
    getPartialSpecializations().InsertNode(D, InsertPos);
554
5.55k
  else {
555
5.55k
    ClassTemplatePartialSpecializationDecl *Existing
556
5.55k
      = getPartialSpecializations().GetOrInsertNode(D);
557
5.55k
    (void)Existing;
558
5.55k
    assert(Existing->isCanonicalDecl() && "Non-canonical specialization?");
559
5.55k
  }
560
561
160k
  if (ASTMutationListener *L = getASTMutationListener())
562
4.81k
    L->AddedCXXTemplateSpecialization(this, D);
563
160k
}
564
565
void ClassTemplateDecl::getPartialSpecializations(
566
798k
    SmallVectorImpl<ClassTemplatePartialSpecializationDecl *> &PS) const {
567
798k
  llvm::FoldingSetVector<ClassTemplatePartialSpecializationDecl> &PartialSpecs
568
798k
    = getPartialSpecializations();
569
798k
  PS.clear();
570
798k
  PS.reserve(PartialSpecs.size());
571
798k
  for (ClassTemplatePartialSpecializationDecl &P : PartialSpecs)
572
533k
    PS.push_back(P.getMostRecentDecl());
573
798k
}
574
575
ClassTemplatePartialSpecializationDecl *
576
64.8k
ClassTemplateDecl::findPartialSpecialization(QualType T) {
577
64.8k
  ASTContext &Context = getASTContext();
578
64.8k
  for (ClassTemplatePartialSpecializationDecl &P :
579
96.3k
       getPartialSpecializations()) {
580
96.3k
    if (Context.hasSameType(P.getInjectedSpecializationType(), T))
581
63.4k
      return P.getMostRecentDecl();
582
96.3k
  }
583
584
1.41k
  return nullptr;
585
64.8k
}
586
587
ClassTemplatePartialSpecializationDecl *
588
ClassTemplateDecl::findPartialSpecInstantiatedFromMember(
589
5.54k
                                    ClassTemplatePartialSpecializationDecl *D) {
590
5.54k
  Decl *DCanon = D->getCanonicalDecl();
591
5.54k
  for (ClassTemplatePartialSpecializationDecl &P : getPartialSpecializations()) {
592
798
    if (P.getInstantiatedFromMember()->getCanonicalDecl() == DCanon)
593
0
      return P.getMostRecentDecl();
594
798
  }
595
596
5.54k
  return nullptr;
597
5.54k
}
598
599
QualType
600
1.10M
ClassTemplateDecl::getInjectedClassNameSpecialization() {
601
1.10M
  Common *CommonPtr = getCommonPtr();
602
1.10M
  if (!CommonPtr->InjectedClassNameType.isNull())
603
678k
    return CommonPtr->InjectedClassNameType;
604
605
  // C++0x [temp.dep.type]p2:
606
  //  The template argument list of a primary template is a template argument
607
  //  list in which the nth template argument has the value of the nth template
608
  //  parameter of the class template. If the nth template parameter is a
609
  //  template parameter pack (14.5.3), the nth template argument is a pack
610
  //  expansion (14.5.3) whose pattern is the name of the template parameter
611
  //  pack.
612
428k
  ASTContext &Context = getASTContext();
613
428k
  TemplateParameterList *Params = getTemplateParameters();
614
428k
  SmallVector<TemplateArgument, 16> TemplateArgs;
615
428k
  Context.getInjectedTemplateArgs(Params, TemplateArgs);
616
428k
  CommonPtr->InjectedClassNameType
617
428k
    = Context.getTemplateSpecializationType(TemplateName(this),
618
428k
                                            TemplateArgs);
619
428k
  return CommonPtr->InjectedClassNameType;
620
1.10M
}
621
622
//===----------------------------------------------------------------------===//
623
// TemplateTypeParm Allocation/Deallocation Method Implementations
624
//===----------------------------------------------------------------------===//
625
626
TemplateTypeParmDecl *
627
TemplateTypeParmDecl::Create(const ASTContext &C, DeclContext *DC,
628
                             SourceLocation KeyLoc, SourceLocation NameLoc,
629
                             unsigned D, unsigned P, IdentifierInfo *Id,
630
                             bool Typename, bool ParameterPack,
631
                             bool HasTypeConstraint,
632
2.83M
                             Optional<unsigned> NumExpanded) {
633
2.83M
  auto *TTPDecl =
634
2.83M
      new (C, DC,
635
2.83M
           additionalSizeToAlloc<TypeConstraint>(HasTypeConstraint ? 
1278
:
02.83M
))
636
2.83M
      TemplateTypeParmDecl(DC, KeyLoc, NameLoc, Id, Typename,
637
2.83M
                           HasTypeConstraint, NumExpanded);
638
2.83M
  QualType TTPType = C.getTemplateTypeParmType(D, P, ParameterPack, TTPDecl);
639
2.83M
  TTPDecl->setTypeForDecl(TTPType.getTypePtr());
640
2.83M
  return TTPDecl;
641
2.83M
}
642
643
TemplateTypeParmDecl *
644
0
TemplateTypeParmDecl::CreateDeserialized(const ASTContext &C, unsigned ID) {
645
0
  return new (C, ID) TemplateTypeParmDecl(nullptr, SourceLocation(),
646
0
                                          SourceLocation(), nullptr, false,
647
0
                                          false, None);
648
0
}
649
650
TemplateTypeParmDecl *
651
TemplateTypeParmDecl::CreateDeserialized(const ASTContext &C, unsigned ID,
652
1.12M
                                         bool HasTypeConstraint) {
653
1.12M
  return new (C, ID,
654
1.12M
              additionalSizeToAlloc<TypeConstraint>(HasTypeConstraint ? 
124
:
01.12M
))
655
1.12M
         TemplateTypeParmDecl(nullptr, SourceLocation(), SourceLocation(),
656
1.12M
                              nullptr, false, HasTypeConstraint, None);
657
1.12M
}
658
659
546k
SourceLocation TemplateTypeParmDecl::getDefaultArgumentLoc() const {
660
546k
  return hasDefaultArgument()
661
546k
             ? getDefaultArgumentInfo()->getTypeLoc().getBeginLoc()
662
546k
             : 
SourceLocation()0
;
663
546k
}
664
665
2.69k
SourceRange TemplateTypeParmDecl::getSourceRange() const {
666
2.69k
  if (hasDefaultArgument() && 
!defaultArgumentWasInherited()67
)
667
60
    return SourceRange(getBeginLoc(),
668
60
                       getDefaultArgumentInfo()->getTypeLoc().getEndLoc());
669
  // TypeDecl::getSourceRange returns a range containing name location, which is
670
  // wrong for unnamed template parameters. e.g:
671
  // it will return <[[typename>]] instead of <[[typename]]>
672
2.63k
  else if (getDeclName().isEmpty())
673
72
    return SourceRange(getBeginLoc());
674
2.56k
  return TypeDecl::getSourceRange();
675
2.69k
}
676
677
3.83M
unsigned TemplateTypeParmDecl::getDepth() const {
678
3.83M
  return getTypeForDecl()->castAs<TemplateTypeParmType>()->getDepth();
679
3.83M
}
680
681
1.08M
unsigned TemplateTypeParmDecl::getIndex() const {
682
1.08M
  return getTypeForDecl()->castAs<TemplateTypeParmType>()->getIndex();
683
1.08M
}
684
685
34.9M
bool TemplateTypeParmDecl::isParameterPack() const {
686
34.9M
  return getTypeForDecl()->castAs<TemplateTypeParmType>()->isParameterPack();
687
34.9M
}
688
689
void TemplateTypeParmDecl::setTypeConstraint(NestedNameSpecifierLoc NNS,
690
    DeclarationNameInfo NameInfo, NamedDecl *FoundDecl, ConceptDecl *CD,
691
    const ASTTemplateArgumentListInfo *ArgsAsWritten,
692
285
    Expr *ImmediatelyDeclaredConstraint) {
693
285
  assert(HasTypeConstraint &&
694
285
         "HasTypeConstraint=true must be passed at construction in order to "
695
285
         "call setTypeConstraint");
696
0
  assert(!TypeConstraintInitialized &&
697
285
         "TypeConstraint was already initialized!");
698
0
  new (getTrailingObjects<TypeConstraint>()) TypeConstraint(NNS, NameInfo,
699
285
      FoundDecl, CD, ArgsAsWritten, ImmediatelyDeclaredConstraint);
700
285
  TypeConstraintInitialized = true;
701
285
}
702
703
//===----------------------------------------------------------------------===//
704
// NonTypeTemplateParmDecl Method Implementations
705
//===----------------------------------------------------------------------===//
706
707
NonTypeTemplateParmDecl::NonTypeTemplateParmDecl(
708
    DeclContext *DC, SourceLocation StartLoc, SourceLocation IdLoc, unsigned D,
709
    unsigned P, IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo,
710
    ArrayRef<QualType> ExpandedTypes, ArrayRef<TypeSourceInfo *> ExpandedTInfos)
711
    : DeclaratorDecl(NonTypeTemplateParm, DC, IdLoc, Id, T, TInfo, StartLoc),
712
      TemplateParmPosition(D, P), ParameterPack(true),
713
54
      ExpandedParameterPack(true), NumExpandedTypes(ExpandedTypes.size()) {
714
54
  if (!ExpandedTypes.empty() && 
!ExpandedTInfos.empty()43
) {
715
43
    auto TypesAndInfos =
716
43
        getTrailingObjects<std::pair<QualType, TypeSourceInfo *>>();
717
135
    for (unsigned I = 0; I != NumExpandedTypes; 
++I92
) {
718
92
      new (&TypesAndInfos[I].first) QualType(ExpandedTypes[I]);
719
92
      TypesAndInfos[I].second = ExpandedTInfos[I];
720
92
    }
721
43
  }
722
54
}
723
724
NonTypeTemplateParmDecl *
725
NonTypeTemplateParmDecl::Create(const ASTContext &C, DeclContext *DC,
726
                                SourceLocation StartLoc, SourceLocation IdLoc,
727
                                unsigned D, unsigned P, IdentifierInfo *Id,
728
                                QualType T, bool ParameterPack,
729
383k
                                TypeSourceInfo *TInfo) {
730
383k
  AutoType *AT =
731
383k
      C.getLangOpts().CPlusPlus20 ? 
T->getContainedAutoType()996
:
nullptr382k
;
732
383k
  return new (C, DC,
733
383k
              additionalSizeToAlloc<std::pair<QualType, TypeSourceInfo *>,
734
383k
                                    Expr *>(0,
735
383k
                                            AT && 
AT->isConstrained()86
?
13
:
0383k
))
736
383k
      NonTypeTemplateParmDecl(DC, StartLoc, IdLoc, D, P, Id, T, ParameterPack,
737
383k
                              TInfo);
738
383k
}
739
740
NonTypeTemplateParmDecl *NonTypeTemplateParmDecl::Create(
741
    const ASTContext &C, DeclContext *DC, SourceLocation StartLoc,
742
    SourceLocation IdLoc, unsigned D, unsigned P, IdentifierInfo *Id,
743
    QualType T, TypeSourceInfo *TInfo, ArrayRef<QualType> ExpandedTypes,
744
50
    ArrayRef<TypeSourceInfo *> ExpandedTInfos) {
745
50
  AutoType *AT = TInfo->getType()->getContainedAutoType();
746
50
  return new (C, DC,
747
50
              additionalSizeToAlloc<std::pair<QualType, TypeSourceInfo *>,
748
50
                                    Expr *>(
749
50
                  ExpandedTypes.size(), AT && 
AT->isConstrained()0
?
10
: 0))
750
50
      NonTypeTemplateParmDecl(DC, StartLoc, IdLoc, D, P, Id, T, TInfo,
751
50
                              ExpandedTypes, ExpandedTInfos);
752
50
}
753
754
NonTypeTemplateParmDecl *
755
NonTypeTemplateParmDecl::CreateDeserialized(ASTContext &C, unsigned ID,
756
151k
                                            bool HasTypeConstraint) {
757
151k
  return new (C, ID, additionalSizeToAlloc<std::pair<QualType,
758
151k
                                                     TypeSourceInfo *>,
759
151k
                                           Expr *>(0,
760
151k
                                                   HasTypeConstraint ? 
10
: 0))
761
151k
          NonTypeTemplateParmDecl(nullptr, SourceLocation(), SourceLocation(),
762
151k
                                  0, 0, nullptr, QualType(), false, nullptr);
763
151k
}
764
765
NonTypeTemplateParmDecl *
766
NonTypeTemplateParmDecl::CreateDeserialized(ASTContext &C, unsigned ID,
767
                                            unsigned NumExpandedTypes,
768
4
                                            bool HasTypeConstraint) {
769
4
  auto *NTTP =
770
4
      new (C, ID, additionalSizeToAlloc<std::pair<QualType, TypeSourceInfo *>,
771
4
                                        Expr *>(
772
4
                      NumExpandedTypes, HasTypeConstraint ? 
10
: 0))
773
4
          NonTypeTemplateParmDecl(nullptr, SourceLocation(), SourceLocation(),
774
4
                                  0, 0, nullptr, QualType(), nullptr, None,
775
4
                                  None);
776
4
  NTTP->NumExpandedTypes = NumExpandedTypes;
777
4
  return NTTP;
778
4
}
779
780
390
SourceRange NonTypeTemplateParmDecl::getSourceRange() const {
781
390
  if (hasDefaultArgument() && 
!defaultArgumentWasInherited()21
)
782
19
    return SourceRange(getOuterLocStart(),
783
19
                       getDefaultArgument()->getSourceRange().getEnd());
784
371
  return DeclaratorDecl::getSourceRange();
785
390
}
786
787
65.3k
SourceLocation NonTypeTemplateParmDecl::getDefaultArgumentLoc() const {
788
65.3k
  return hasDefaultArgument()
789
65.3k
    ? getDefaultArgument()->getSourceRange().getBegin()
790
65.3k
    : 
SourceLocation()0
;
791
65.3k
}
792
793
//===----------------------------------------------------------------------===//
794
// TemplateTemplateParmDecl Method Implementations
795
//===----------------------------------------------------------------------===//
796
797
0
void TemplateTemplateParmDecl::anchor() {}
798
799
TemplateTemplateParmDecl::TemplateTemplateParmDecl(
800
    DeclContext *DC, SourceLocation L, unsigned D, unsigned P,
801
    IdentifierInfo *Id, TemplateParameterList *Params,
802
    ArrayRef<TemplateParameterList *> Expansions)
803
    : TemplateDecl(TemplateTemplateParm, DC, L, Id, Params),
804
      TemplateParmPosition(D, P), ParameterPack(true),
805
22
      ExpandedParameterPack(true), NumExpandedParams(Expansions.size()) {
806
22
  if (!Expansions.empty())
807
16
    std::uninitialized_copy(Expansions.begin(), Expansions.end(),
808
16
                            getTrailingObjects<TemplateParameterList *>());
809
22
}
810
811
TemplateTemplateParmDecl *
812
TemplateTemplateParmDecl::Create(const ASTContext &C, DeclContext *DC,
813
                                 SourceLocation L, unsigned D, unsigned P,
814
                                 bool ParameterPack, IdentifierInfo *Id,
815
35.6k
                                 TemplateParameterList *Params) {
816
35.6k
  return new (C, DC) TemplateTemplateParmDecl(DC, L, D, P, ParameterPack, Id,
817
35.6k
                                              Params);
818
35.6k
}
819
820
TemplateTemplateParmDecl *
821
TemplateTemplateParmDecl::Create(const ASTContext &C, DeclContext *DC,
822
                                 SourceLocation L, unsigned D, unsigned P,
823
                                 IdentifierInfo *Id,
824
                                 TemplateParameterList *Params,
825
18
                                 ArrayRef<TemplateParameterList *> Expansions) {
826
18
  return new (C, DC,
827
18
              additionalSizeToAlloc<TemplateParameterList *>(Expansions.size()))
828
18
      TemplateTemplateParmDecl(DC, L, D, P, Id, Params, Expansions);
829
18
}
830
831
TemplateTemplateParmDecl *
832
4.13k
TemplateTemplateParmDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
833
4.13k
  return new (C, ID) TemplateTemplateParmDecl(nullptr, SourceLocation(), 0, 0,
834
4.13k
                                              false, nullptr, nullptr);
835
4.13k
}
836
837
TemplateTemplateParmDecl *
838
TemplateTemplateParmDecl::CreateDeserialized(ASTContext &C, unsigned ID,
839
4
                                             unsigned NumExpansions) {
840
4
  auto *TTP =
841
4
      new (C, ID, additionalSizeToAlloc<TemplateParameterList *>(NumExpansions))
842
4
          TemplateTemplateParmDecl(nullptr, SourceLocation(), 0, 0, nullptr,
843
4
                                   nullptr, None);
844
4
  TTP->NumExpandedParams = NumExpansions;
845
4
  return TTP;
846
4
}
847
848
0
SourceLocation TemplateTemplateParmDecl::getDefaultArgumentLoc() const {
849
0
  return hasDefaultArgument() ? getDefaultArgument().getLocation()
850
0
                              : SourceLocation();
851
0
}
852
853
void TemplateTemplateParmDecl::setDefaultArgument(
854
19.3k
    const ASTContext &C, const TemplateArgumentLoc &DefArg) {
855
19.3k
  if (DefArg.getArgument().isNull())
856
0
    DefaultArgument.set(nullptr);
857
19.3k
  else
858
19.3k
    DefaultArgument.set(new (C) TemplateArgumentLoc(DefArg));
859
19.3k
}
860
861
//===----------------------------------------------------------------------===//
862
// TemplateArgumentList Implementation
863
//===----------------------------------------------------------------------===//
864
TemplateArgumentList::TemplateArgumentList(ArrayRef<TemplateArgument> Args)
865
    : Arguments(getTrailingObjects<TemplateArgument>()),
866
3.76M
      NumArguments(Args.size()) {
867
3.76M
  std::uninitialized_copy(Args.begin(), Args.end(),
868
3.76M
                          getTrailingObjects<TemplateArgument>());
869
3.76M
}
870
871
TemplateArgumentList *
872
TemplateArgumentList::CreateCopy(ASTContext &Context,
873
3.76M
                                 ArrayRef<TemplateArgument> Args) {
874
3.76M
  void *Mem = Context.Allocate(totalSizeToAlloc<TemplateArgument>(Args.size()));
875
3.76M
  return new (Mem) TemplateArgumentList(Args);
876
3.76M
}
877
878
FunctionTemplateSpecializationInfo *FunctionTemplateSpecializationInfo::Create(
879
    ASTContext &C, FunctionDecl *FD, FunctionTemplateDecl *Template,
880
    TemplateSpecializationKind TSK, const TemplateArgumentList *TemplateArgs,
881
    const TemplateArgumentListInfo *TemplateArgsAsWritten, SourceLocation POI,
882
377k
    MemberSpecializationInfo *MSInfo) {
883
377k
  const ASTTemplateArgumentListInfo *ArgsAsWritten = nullptr;
884
377k
  if (TemplateArgsAsWritten)
885
7.94k
    ArgsAsWritten = ASTTemplateArgumentListInfo::Create(C,
886
7.94k
                                                        *TemplateArgsAsWritten);
887
888
377k
  void *Mem =
889
377k
      C.Allocate(totalSizeToAlloc<MemberSpecializationInfo *>(MSInfo ? 
1114
:
0377k
));
890
377k
  return new (Mem) FunctionTemplateSpecializationInfo(
891
377k
      FD, Template, TSK, TemplateArgs, ArgsAsWritten, POI, MSInfo);
892
377k
}
893
894
//===----------------------------------------------------------------------===//
895
// ClassTemplateSpecializationDecl Implementation
896
//===----------------------------------------------------------------------===//
897
898
ClassTemplateSpecializationDecl::
899
ClassTemplateSpecializationDecl(ASTContext &Context, Kind DK, TagKind TK,
900
                                DeclContext *DC, SourceLocation StartLoc,
901
                                SourceLocation IdLoc,
902
                                ClassTemplateDecl *SpecializedTemplate,
903
                                ArrayRef<TemplateArgument> Args,
904
                                ClassTemplateSpecializationDecl *PrevDecl)
905
    : CXXRecordDecl(DK, TK, Context, DC, StartLoc, IdLoc,
906
                    SpecializedTemplate->getIdentifier(), PrevDecl),
907
    SpecializedTemplate(SpecializedTemplate),
908
    TemplateArgs(TemplateArgumentList::CreateCopy(Context, Args)),
909
1.11M
    SpecializationKind(TSK_Undeclared) {
910
1.11M
}
911
912
ClassTemplateSpecializationDecl::ClassTemplateSpecializationDecl(ASTContext &C,
913
                                                                 Kind DK)
914
    : CXXRecordDecl(DK, TTK_Struct, C, nullptr, SourceLocation(),
915
                    SourceLocation(), nullptr, nullptr),
916
1.03M
      SpecializationKind(TSK_Undeclared) {}
917
918
ClassTemplateSpecializationDecl *
919
ClassTemplateSpecializationDecl::Create(ASTContext &Context, TagKind TK,
920
                                        DeclContext *DC,
921
                                        SourceLocation StartLoc,
922
                                        SourceLocation IdLoc,
923
                                        ClassTemplateDecl *SpecializedTemplate,
924
                                        ArrayRef<TemplateArgument> Args,
925
953k
                                   ClassTemplateSpecializationDecl *PrevDecl) {
926
953k
  auto *Result =
927
953k
      new (Context, DC) ClassTemplateSpecializationDecl(
928
953k
          Context, ClassTemplateSpecialization, TK, DC, StartLoc, IdLoc,
929
953k
          SpecializedTemplate, Args, PrevDecl);
930
953k
  Result->setMayHaveOutOfDateDef(false);
931
932
953k
  Context.getTypeDeclType(Result, PrevDecl);
933
953k
  return Result;
934
953k
}
935
936
ClassTemplateSpecializationDecl *
937
ClassTemplateSpecializationDecl::CreateDeserialized(ASTContext &C,
938
1.00M
                                                    unsigned ID) {
939
1.00M
  auto *Result =
940
1.00M
    new (C, ID) ClassTemplateSpecializationDecl(C, ClassTemplateSpecialization);
941
1.00M
  Result->setMayHaveOutOfDateDef(false);
942
1.00M
  return Result;
943
1.00M
}
944
945
void ClassTemplateSpecializationDecl::getNameForDiagnostic(
946
72.5k
    raw_ostream &OS, const PrintingPolicy &Policy, bool Qualified) const {
947
72.5k
  NamedDecl::getNameForDiagnostic(OS, Policy, Qualified);
948
949
72.5k
  const auto *PS = dyn_cast<ClassTemplatePartialSpecializationDecl>(this);
950
72.5k
  if (const ASTTemplateArgumentListInfo *ArgsAsWritten =
951
72.5k
          PS ? PS->getTemplateArgsAsWritten() : nullptr) {
952
94
    printTemplateArgumentList(
953
94
        OS, ArgsAsWritten->arguments(), Policy,
954
94
        getSpecializedTemplate()->getTemplateParameters());
955
72.4k
  } else {
956
72.4k
    const TemplateArgumentList &TemplateArgs = getTemplateArgs();
957
72.4k
    printTemplateArgumentList(
958
72.4k
        OS, TemplateArgs.asArray(), Policy,
959
72.4k
        getSpecializedTemplate()->getTemplateParameters());
960
72.4k
  }
961
72.5k
}
962
963
ClassTemplateDecl *
964
13.7M
ClassTemplateSpecializationDecl::getSpecializedTemplate() const {
965
13.7M
  if (const auto *PartialSpec =
966
13.7M
          SpecializedTemplate.dyn_cast<SpecializedPartialSpecialization*>())
967
969k
    return PartialSpec->PartialSpecialization->getSpecializedTemplate();
968
12.8M
  return SpecializedTemplate.get<ClassTemplateDecl*>();
969
13.7M
}
970
971
SourceRange
972
436k
ClassTemplateSpecializationDecl::getSourceRange() const {
973
436k
  if (ExplicitInfo) {
974
421k
    SourceLocation Begin = getTemplateKeywordLoc();
975
421k
    if (Begin.isValid()) {
976
      // Here we have an explicit (partial) specialization or instantiation.
977
420k
      assert(getSpecializationKind() == TSK_ExplicitSpecialization ||
978
420k
             getSpecializationKind() == TSK_ExplicitInstantiationDeclaration ||
979
420k
             getSpecializationKind() == TSK_ExplicitInstantiationDefinition);
980
420k
      if (getExternLoc().isValid())
981
43
        Begin = getExternLoc();
982
420k
      SourceLocation End = getBraceRange().getEnd();
983
420k
      if (End.isInvalid())
984
370
        End = getTypeAsWritten()->getTypeLoc().getEndLoc();
985
420k
      return SourceRange(Begin, End);
986
420k
    }
987
    // An implicit instantiation of a class template partial specialization
988
    // uses ExplicitInfo to record the TypeAsWritten, but the source
989
    // locations should be retrieved from the instantiation pattern.
990
797
    using CTPSDecl = ClassTemplatePartialSpecializationDecl;
991
797
    auto *ctpsd = const_cast<CTPSDecl *>(cast<CTPSDecl>(this));
992
797
    CTPSDecl *inst_from = ctpsd->getInstantiatedFromMember();
993
797
    assert(inst_from != nullptr);
994
0
    return inst_from->getSourceRange();
995
421k
  }
996
15.3k
  else {
997
    // No explicit info available.
998
15.3k
    llvm::PointerUnion<ClassTemplateDecl *,
999
15.3k
                       ClassTemplatePartialSpecializationDecl *>
1000
15.3k
      inst_from = getInstantiatedFrom();
1001
15.3k
    if (inst_from.isNull())
1002
15.1k
      return getSpecializedTemplate()->getSourceRange();
1003
221
    if (const auto *ctd = inst_from.dyn_cast<ClassTemplateDecl *>())
1004
189
      return ctd->getSourceRange();
1005
32
    return inst_from.get<ClassTemplatePartialSpecializationDecl *>()
1006
32
      ->getSourceRange();
1007
221
  }
1008
436k
}
1009
1010
//===----------------------------------------------------------------------===//
1011
// ConceptDecl Implementation
1012
//===----------------------------------------------------------------------===//
1013
ConceptDecl *ConceptDecl::Create(ASTContext &C, DeclContext *DC,
1014
                                 SourceLocation L, DeclarationName Name,
1015
                                 TemplateParameterList *Params,
1016
236
                                 Expr *ConstraintExpr) {
1017
236
  bool Invalid = AdoptTemplateParameterList(Params, DC);
1018
236
  auto *TD = new (C, DC) ConceptDecl(DC, L, Name, Params, ConstraintExpr);
1019
236
  if (Invalid)
1020
0
    TD->setInvalidDecl();
1021
236
  return TD;
1022
236
}
1023
1024
ConceptDecl *ConceptDecl::CreateDeserialized(ASTContext &C,
1025
14
                                             unsigned ID) {
1026
14
  ConceptDecl *Result = new (C, ID) ConceptDecl(nullptr, SourceLocation(),
1027
14
                                                DeclarationName(),
1028
14
                                                nullptr, nullptr);
1029
1030
14
  return Result;
1031
14
}
1032
1033
//===----------------------------------------------------------------------===//
1034
// ClassTemplatePartialSpecializationDecl Implementation
1035
//===----------------------------------------------------------------------===//
1036
0
void ClassTemplatePartialSpecializationDecl::anchor() {}
1037
1038
ClassTemplatePartialSpecializationDecl::
1039
ClassTemplatePartialSpecializationDecl(ASTContext &Context, TagKind TK,
1040
                                       DeclContext *DC,
1041
                                       SourceLocation StartLoc,
1042
                                       SourceLocation IdLoc,
1043
                                       TemplateParameterList *Params,
1044
                                       ClassTemplateDecl *SpecializedTemplate,
1045
                                       ArrayRef<TemplateArgument> Args,
1046
                               const ASTTemplateArgumentListInfo *ArgInfos,
1047
                               ClassTemplatePartialSpecializationDecl *PrevDecl)
1048
    : ClassTemplateSpecializationDecl(Context,
1049
                                      ClassTemplatePartialSpecialization,
1050
                                      TK, DC, StartLoc, IdLoc,
1051
                                      SpecializedTemplate, Args, PrevDecl),
1052
      TemplateParams(Params), ArgsAsWritten(ArgInfos),
1053
160k
      InstantiatedFromMember(nullptr, false) {
1054
160k
  if (AdoptTemplateParameterList(Params, this))
1055
0
    setInvalidDecl();
1056
160k
}
1057
1058
ClassTemplatePartialSpecializationDecl *
1059
ClassTemplatePartialSpecializationDecl::
1060
Create(ASTContext &Context, TagKind TK,DeclContext *DC,
1061
       SourceLocation StartLoc, SourceLocation IdLoc,
1062
       TemplateParameterList *Params,
1063
       ClassTemplateDecl *SpecializedTemplate,
1064
       ArrayRef<TemplateArgument> Args,
1065
       const TemplateArgumentListInfo &ArgInfos,
1066
       QualType CanonInjectedType,
1067
160k
       ClassTemplatePartialSpecializationDecl *PrevDecl) {
1068
160k
  const ASTTemplateArgumentListInfo *ASTArgInfos =
1069
160k
    ASTTemplateArgumentListInfo::Create(Context, ArgInfos);
1070
1071
160k
  auto *Result = new (Context, DC)
1072
160k
      ClassTemplatePartialSpecializationDecl(Context, TK, DC, StartLoc, IdLoc,
1073
160k
                                             Params, SpecializedTemplate, Args,
1074
160k
                                             ASTArgInfos, PrevDecl);
1075
160k
  Result->setSpecializationKind(TSK_ExplicitSpecialization);
1076
160k
  Result->setMayHaveOutOfDateDef(false);
1077
1078
160k
  Context.getInjectedClassNameType(Result, CanonInjectedType);
1079
160k
  return Result;
1080
160k
}
1081
1082
ClassTemplatePartialSpecializationDecl *
1083
ClassTemplatePartialSpecializationDecl::CreateDeserialized(ASTContext &C,
1084
31.0k
                                                           unsigned ID) {
1085
31.0k
  auto *Result = new (C, ID) ClassTemplatePartialSpecializationDecl(C);
1086
31.0k
  Result->setMayHaveOutOfDateDef(false);
1087
31.0k
  return Result;
1088
31.0k
}
1089
1090
//===----------------------------------------------------------------------===//
1091
// FriendTemplateDecl Implementation
1092
//===----------------------------------------------------------------------===//
1093
1094
0
void FriendTemplateDecl::anchor() {}
1095
1096
FriendTemplateDecl *
1097
FriendTemplateDecl::Create(ASTContext &Context, DeclContext *DC,
1098
                           SourceLocation L,
1099
                           MutableArrayRef<TemplateParameterList *> Params,
1100
5
                           FriendUnion Friend, SourceLocation FLoc) {
1101
5
  return new (Context, DC) FriendTemplateDecl(DC, L, Params, Friend, FLoc);
1102
5
}
1103
1104
FriendTemplateDecl *FriendTemplateDecl::CreateDeserialized(ASTContext &C,
1105
0
                                                           unsigned ID) {
1106
0
  return new (C, ID) FriendTemplateDecl(EmptyShell());
1107
0
}
1108
1109
//===----------------------------------------------------------------------===//
1110
// TypeAliasTemplateDecl Implementation
1111
//===----------------------------------------------------------------------===//
1112
1113
TypeAliasTemplateDecl *
1114
TypeAliasTemplateDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
1115
                              DeclarationName Name,
1116
76.7k
                              TemplateParameterList *Params, NamedDecl *Decl) {
1117
76.7k
  bool Invalid = AdoptTemplateParameterList(Params, DC);
1118
76.7k
  auto *TD = new (C, DC) TypeAliasTemplateDecl(C, DC, L, Name, Params, Decl);
1119
76.7k
  if (Invalid)
1120
1
    TD->setInvalidDecl();
1121
76.7k
  return TD;
1122
76.7k
}
1123
1124
TypeAliasTemplateDecl *TypeAliasTemplateDecl::CreateDeserialized(ASTContext &C,
1125
13.2k
                                                                 unsigned ID) {
1126
13.2k
  return new (C, ID) TypeAliasTemplateDecl(C, nullptr, SourceLocation(),
1127
13.2k
                                           DeclarationName(), nullptr, nullptr);
1128
13.2k
}
1129
1130
RedeclarableTemplateDecl::CommonBase *
1131
41.2k
TypeAliasTemplateDecl::newCommon(ASTContext &C) const {
1132
41.2k
  auto *CommonPtr = new (C) Common;
1133
41.2k
  C.addDestruction(CommonPtr);
1134
41.2k
  return CommonPtr;
1135
41.2k
}
1136
1137
//===----------------------------------------------------------------------===//
1138
// ClassScopeFunctionSpecializationDecl Implementation
1139
//===----------------------------------------------------------------------===//
1140
1141
0
void ClassScopeFunctionSpecializationDecl::anchor() {}
1142
1143
ClassScopeFunctionSpecializationDecl *
1144
ClassScopeFunctionSpecializationDecl::CreateDeserialized(ASTContext &C,
1145
12
                                                         unsigned ID) {
1146
12
  return new (C, ID) ClassScopeFunctionSpecializationDecl(
1147
12
      nullptr, SourceLocation(), nullptr, nullptr);
1148
12
}
1149
1150
//===----------------------------------------------------------------------===//
1151
// VarTemplateDecl Implementation
1152
//===----------------------------------------------------------------------===//
1153
1154
0
VarTemplateDecl *VarTemplateDecl::getDefinition() {
1155
0
  VarTemplateDecl *CurD = this;
1156
0
  while (CurD) {
1157
0
    if (CurD->isThisDeclarationADefinition())
1158
0
      return CurD;
1159
0
    CurD = CurD->getPreviousDecl();
1160
0
  }
1161
0
  return nullptr;
1162
0
}
1163
1164
VarTemplateDecl *VarTemplateDecl::Create(ASTContext &C, DeclContext *DC,
1165
                                         SourceLocation L, DeclarationName Name,
1166
                                         TemplateParameterList *Params,
1167
3.18k
                                         VarDecl *Decl) {
1168
3.18k
  bool Invalid = AdoptTemplateParameterList(Params, DC);
1169
3.18k
  auto *TD = new (C, DC) VarTemplateDecl(C, DC, L, Name, Params, Decl);
1170
3.18k
  if (Invalid)
1171
0
    TD->setInvalidDecl();
1172
3.18k
  return TD;
1173
3.18k
}
1174
1175
VarTemplateDecl *VarTemplateDecl::CreateDeserialized(ASTContext &C,
1176
239
                                                     unsigned ID) {
1177
239
  return new (C, ID) VarTemplateDecl(C, nullptr, SourceLocation(),
1178
239
                                     DeclarationName(), nullptr, nullptr);
1179
239
}
1180
1181
14.1k
void VarTemplateDecl::LoadLazySpecializations() const {
1182
14.1k
  loadLazySpecializationsImpl();
1183
14.1k
}
1184
1185
llvm::FoldingSetVector<VarTemplateSpecializationDecl> &
1186
10.3k
VarTemplateDecl::getSpecializations() const {
1187
10.3k
  LoadLazySpecializations();
1188
10.3k
  return getCommonPtr()->Specializations;
1189
10.3k
}
1190
1191
llvm::FoldingSetVector<VarTemplatePartialSpecializationDecl> &
1192
3.48k
VarTemplateDecl::getPartialSpecializations() const {
1193
3.48k
  LoadLazySpecializations();
1194
3.48k
  return getCommonPtr()->PartialSpecializations;
1195
3.48k
}
1196
1197
RedeclarableTemplateDecl::CommonBase *
1198
2.14k
VarTemplateDecl::newCommon(ASTContext &C) const {
1199
2.14k
  auto *CommonPtr = new (C) Common;
1200
2.14k
  C.addDestruction(CommonPtr);
1201
2.14k
  return CommonPtr;
1202
2.14k
}
1203
1204
VarTemplateSpecializationDecl *
1205
VarTemplateDecl::findSpecialization(ArrayRef<TemplateArgument> Args,
1206
6.11k
                                    void *&InsertPos) {
1207
6.11k
  return findSpecializationImpl(getSpecializations(), InsertPos, Args);
1208
6.11k
}
1209
1210
void VarTemplateDecl::AddSpecialization(VarTemplateSpecializationDecl *D,
1211
2.84k
                                        void *InsertPos) {
1212
2.84k
  addSpecializationImpl<VarTemplateDecl>(getSpecializations(), D, InsertPos);
1213
2.84k
}
1214
1215
VarTemplatePartialSpecializationDecl *
1216
VarTemplateDecl::findPartialSpecialization(ArrayRef<TemplateArgument> Args,
1217
411
     TemplateParameterList *TPL, void *&InsertPos) {
1218
411
  return findSpecializationImpl(getPartialSpecializations(), InsertPos, Args,
1219
411
                                TPL);
1220
411
}
1221
1222
void
1223
VarTemplatePartialSpecializationDecl::Profile(llvm::FoldingSetNodeID &ID,
1224
    ArrayRef<TemplateArgument> TemplateArgs, TemplateParameterList *TPL,
1225
551
    ASTContext &Context) {
1226
551
  ID.AddInteger(TemplateArgs.size());
1227
551
  for (const TemplateArgument &TemplateArg : TemplateArgs)
1228
696
    TemplateArg.Profile(ID, Context);
1229
551
  ProfileTemplateParameterList(Context, ID, TPL);
1230
551
}
1231
1232
void VarTemplateDecl::AddPartialSpecialization(
1233
336
    VarTemplatePartialSpecializationDecl *D, void *InsertPos) {
1234
336
  if (InsertPos)
1235
287
    getPartialSpecializations().InsertNode(D, InsertPos);
1236
49
  else {
1237
49
    VarTemplatePartialSpecializationDecl *Existing =
1238
49
        getPartialSpecializations().GetOrInsertNode(D);
1239
49
    (void)Existing;
1240
49
    assert(Existing->isCanonicalDecl() && "Non-canonical specialization?");
1241
49
  }
1242
1243
336
  if (ASTMutationListener *L = getASTMutationListener())
1244
32
    L->AddedCXXTemplateSpecialization(this, D);
1245
336
}
1246
1247
void VarTemplateDecl::getPartialSpecializations(
1248
2.69k
    SmallVectorImpl<VarTemplatePartialSpecializationDecl *> &PS) const {
1249
2.69k
  llvm::FoldingSetVector<VarTemplatePartialSpecializationDecl> &PartialSpecs =
1250
2.69k
      getPartialSpecializations();
1251
2.69k
  PS.clear();
1252
2.69k
  PS.reserve(PartialSpecs.size());
1253
2.69k
  for (VarTemplatePartialSpecializationDecl &P : PartialSpecs)
1254
1.09k
    PS.push_back(P.getMostRecentDecl());
1255
2.69k
}
1256
1257
VarTemplatePartialSpecializationDecl *
1258
VarTemplateDecl::findPartialSpecInstantiatedFromMember(
1259
43
    VarTemplatePartialSpecializationDecl *D) {
1260
43
  Decl *DCanon = D->getCanonicalDecl();
1261
43
  for (VarTemplatePartialSpecializationDecl &P : getPartialSpecializations()) {
1262
0
    if (P.getInstantiatedFromMember()->getCanonicalDecl() == DCanon)
1263
0
      return P.getMostRecentDecl();
1264
0
  }
1265
1266
43
  return nullptr;
1267
43
}
1268
1269
//===----------------------------------------------------------------------===//
1270
// VarTemplateSpecializationDecl Implementation
1271
//===----------------------------------------------------------------------===//
1272
1273
VarTemplateSpecializationDecl::VarTemplateSpecializationDecl(
1274
    Kind DK, ASTContext &Context, DeclContext *DC, SourceLocation StartLoc,
1275
    SourceLocation IdLoc, VarTemplateDecl *SpecializedTemplate, QualType T,
1276
    TypeSourceInfo *TInfo, StorageClass S, ArrayRef<TemplateArgument> Args)
1277
    : VarDecl(DK, Context, DC, StartLoc, IdLoc,
1278
              SpecializedTemplate->getIdentifier(), T, TInfo, S),
1279
      SpecializedTemplate(SpecializedTemplate),
1280
      TemplateArgs(TemplateArgumentList::CreateCopy(Context, Args)),
1281
3.45k
      SpecializationKind(TSK_Undeclared), IsCompleteDefinition(false) {}
1282
1283
VarTemplateSpecializationDecl::VarTemplateSpecializationDecl(Kind DK,
1284
                                                             ASTContext &C)
1285
    : VarDecl(DK, C, nullptr, SourceLocation(), SourceLocation(), nullptr,
1286
              QualType(), nullptr, SC_None),
1287
190
      SpecializationKind(TSK_Undeclared), IsCompleteDefinition(false) {}
1288
1289
VarTemplateSpecializationDecl *VarTemplateSpecializationDecl::Create(
1290
    ASTContext &Context, DeclContext *DC, SourceLocation StartLoc,
1291
    SourceLocation IdLoc, VarTemplateDecl *SpecializedTemplate, QualType T,
1292
3.04k
    TypeSourceInfo *TInfo, StorageClass S, ArrayRef<TemplateArgument> Args) {
1293
3.04k
  return new (Context, DC) VarTemplateSpecializationDecl(
1294
3.04k
      VarTemplateSpecialization, Context, DC, StartLoc, IdLoc,
1295
3.04k
      SpecializedTemplate, T, TInfo, S, Args);
1296
3.04k
}
1297
1298
VarTemplateSpecializationDecl *
1299
159
VarTemplateSpecializationDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1300
159
  return new (C, ID)
1301
159
      VarTemplateSpecializationDecl(VarTemplateSpecialization, C);
1302
159
}
1303
1304
void VarTemplateSpecializationDecl::getNameForDiagnostic(
1305
174
    raw_ostream &OS, const PrintingPolicy &Policy, bool Qualified) const {
1306
174
  NamedDecl::getNameForDiagnostic(OS, Policy, Qualified);
1307
1308
174
  const auto *PS = dyn_cast<VarTemplatePartialSpecializationDecl>(this);
1309
174
  if (const ASTTemplateArgumentListInfo *ArgsAsWritten =
1310
174
          PS ? PS->getTemplateArgsAsWritten() : nullptr) {
1311
18
    printTemplateArgumentList(
1312
18
        OS, ArgsAsWritten->arguments(), Policy,
1313
18
        getSpecializedTemplate()->getTemplateParameters());
1314
156
  } else {
1315
156
    const TemplateArgumentList &TemplateArgs = getTemplateArgs();
1316
156
    printTemplateArgumentList(
1317
156
        OS, TemplateArgs.asArray(), Policy,
1318
156
        getSpecializedTemplate()->getTemplateParameters());
1319
156
  }
1320
174
}
1321
1322
11.5k
VarTemplateDecl *VarTemplateSpecializationDecl::getSpecializedTemplate() const {
1323
11.5k
  if (const auto *PartialSpec =
1324
11.5k
          SpecializedTemplate.dyn_cast<SpecializedPartialSpecialization *>())
1325
955
    return PartialSpec->PartialSpecialization->getSpecializedTemplate();
1326
10.5k
  return SpecializedTemplate.get<VarTemplateDecl *>();
1327
11.5k
}
1328
1329
void VarTemplateSpecializationDecl::setTemplateArgsInfo(
1330
3.05k
    const TemplateArgumentListInfo &ArgsInfo) {
1331
3.05k
  TemplateArgsInfo.setLAngleLoc(ArgsInfo.getLAngleLoc());
1332
3.05k
  TemplateArgsInfo.setRAngleLoc(ArgsInfo.getRAngleLoc());
1333
3.05k
  for (const TemplateArgumentLoc &Loc : ArgsInfo.arguments())
1334
4.37k
    TemplateArgsInfo.addArgument(Loc);
1335
3.05k
}
1336
1337
//===----------------------------------------------------------------------===//
1338
// VarTemplatePartialSpecializationDecl Implementation
1339
//===----------------------------------------------------------------------===//
1340
1341
0
void VarTemplatePartialSpecializationDecl::anchor() {}
1342
1343
VarTemplatePartialSpecializationDecl::VarTemplatePartialSpecializationDecl(
1344
    ASTContext &Context, DeclContext *DC, SourceLocation StartLoc,
1345
    SourceLocation IdLoc, TemplateParameterList *Params,
1346
    VarTemplateDecl *SpecializedTemplate, QualType T, TypeSourceInfo *TInfo,
1347
    StorageClass S, ArrayRef<TemplateArgument> Args,
1348
    const ASTTemplateArgumentListInfo *ArgInfos)
1349
    : VarTemplateSpecializationDecl(VarTemplatePartialSpecialization, Context,
1350
                                    DC, StartLoc, IdLoc, SpecializedTemplate, T,
1351
                                    TInfo, S, Args),
1352
      TemplateParams(Params), ArgsAsWritten(ArgInfos),
1353
411
      InstantiatedFromMember(nullptr, false) {
1354
411
  if (AdoptTemplateParameterList(Params, DC))
1355
0
    setInvalidDecl();
1356
411
}
1357
1358
VarTemplatePartialSpecializationDecl *
1359
VarTemplatePartialSpecializationDecl::Create(
1360
    ASTContext &Context, DeclContext *DC, SourceLocation StartLoc,
1361
    SourceLocation IdLoc, TemplateParameterList *Params,
1362
    VarTemplateDecl *SpecializedTemplate, QualType T, TypeSourceInfo *TInfo,
1363
    StorageClass S, ArrayRef<TemplateArgument> Args,
1364
411
    const TemplateArgumentListInfo &ArgInfos) {
1365
411
  const ASTTemplateArgumentListInfo *ASTArgInfos
1366
411
    = ASTTemplateArgumentListInfo::Create(Context, ArgInfos);
1367
1368
411
  auto *Result =
1369
411
      new (Context, DC) VarTemplatePartialSpecializationDecl(
1370
411
          Context, DC, StartLoc, IdLoc, Params, SpecializedTemplate, T, TInfo,
1371
411
          S, Args, ASTArgInfos);
1372
411
  Result->setSpecializationKind(TSK_ExplicitSpecialization);
1373
411
  return Result;
1374
411
}
1375
1376
VarTemplatePartialSpecializationDecl *
1377
VarTemplatePartialSpecializationDecl::CreateDeserialized(ASTContext &C,
1378
31
                                                         unsigned ID) {
1379
31
  return new (C, ID) VarTemplatePartialSpecializationDecl(C);
1380
31
}
1381
1382
static TemplateParameterList *
1383
603
createMakeIntegerSeqParameterList(const ASTContext &C, DeclContext *DC) {
1384
  // typename T
1385
603
  auto *T = TemplateTypeParmDecl::Create(
1386
603
      C, DC, SourceLocation(), SourceLocation(), /*Depth=*/1, /*Position=*/0,
1387
603
      /*Id=*/nullptr, /*Typename=*/true, /*ParameterPack=*/false,
1388
603
      /*HasTypeConstraint=*/false);
1389
603
  T->setImplicit(true);
1390
1391
  // T ...Ints
1392
603
  TypeSourceInfo *TI =
1393
603
      C.getTrivialTypeSourceInfo(QualType(T->getTypeForDecl(), 0));
1394
603
  auto *N = NonTypeTemplateParmDecl::Create(
1395
603
      C, DC, SourceLocation(), SourceLocation(), /*Depth=*/0, /*Position=*/1,
1396
603
      /*Id=*/nullptr, TI->getType(), /*ParameterPack=*/true, TI);
1397
603
  N->setImplicit(true);
1398
1399
  // <typename T, T ...Ints>
1400
603
  NamedDecl *P[2] = {T, N};
1401
603
  auto *TPL = TemplateParameterList::Create(
1402
603
      C, SourceLocation(), SourceLocation(), P, SourceLocation(), nullptr);
1403
1404
  // template <typename T, ...Ints> class IntSeq
1405
603
  auto *TemplateTemplateParm = TemplateTemplateParmDecl::Create(
1406
603
      C, DC, SourceLocation(), /*Depth=*/0, /*Position=*/0,
1407
603
      /*ParameterPack=*/false, /*Id=*/nullptr, TPL);
1408
603
  TemplateTemplateParm->setImplicit(true);
1409
1410
  // typename T
1411
603
  auto *TemplateTypeParm = TemplateTypeParmDecl::Create(
1412
603
      C, DC, SourceLocation(), SourceLocation(), /*Depth=*/0, /*Position=*/1,
1413
603
      /*Id=*/nullptr, /*Typename=*/true, /*ParameterPack=*/false,
1414
603
      /*HasTypeConstraint=*/false);
1415
603
  TemplateTypeParm->setImplicit(true);
1416
1417
  // T N
1418
603
  TypeSourceInfo *TInfo = C.getTrivialTypeSourceInfo(
1419
603
      QualType(TemplateTypeParm->getTypeForDecl(), 0));
1420
603
  auto *NonTypeTemplateParm = NonTypeTemplateParmDecl::Create(
1421
603
      C, DC, SourceLocation(), SourceLocation(), /*Depth=*/0, /*Position=*/2,
1422
603
      /*Id=*/nullptr, TInfo->getType(), /*ParameterPack=*/false, TInfo);
1423
603
  NamedDecl *Params[] = {TemplateTemplateParm, TemplateTypeParm,
1424
603
                         NonTypeTemplateParm};
1425
1426
  // template <template <typename T, T ...Ints> class IntSeq, typename T, T N>
1427
603
  return TemplateParameterList::Create(C, SourceLocation(), SourceLocation(),
1428
603
                                       Params, SourceLocation(), nullptr);
1429
603
}
1430
1431
static TemplateParameterList *
1432
476
createTypePackElementParameterList(const ASTContext &C, DeclContext *DC) {
1433
  // std::size_t Index
1434
476
  TypeSourceInfo *TInfo = C.getTrivialTypeSourceInfo(C.getSizeType());
1435
476
  auto *Index = NonTypeTemplateParmDecl::Create(
1436
476
      C, DC, SourceLocation(), SourceLocation(), /*Depth=*/0, /*Position=*/0,
1437
476
      /*Id=*/nullptr, TInfo->getType(), /*ParameterPack=*/false, TInfo);
1438
1439
  // typename ...T
1440
476
  auto *Ts = TemplateTypeParmDecl::Create(
1441
476
      C, DC, SourceLocation(), SourceLocation(), /*Depth=*/0, /*Position=*/1,
1442
476
      /*Id=*/nullptr, /*Typename=*/true, /*ParameterPack=*/true,
1443
476
      /*HasTypeConstraint=*/false);
1444
476
  Ts->setImplicit(true);
1445
1446
  // template <std::size_t Index, typename ...T>
1447
476
  NamedDecl *Params[] = {Index, Ts};
1448
476
  return TemplateParameterList::Create(C, SourceLocation(), SourceLocation(),
1449
476
                                       llvm::makeArrayRef(Params),
1450
476
                                       SourceLocation(), nullptr);
1451
476
}
1452
1453
static TemplateParameterList *createBuiltinTemplateParameterList(
1454
1.07k
    const ASTContext &C, DeclContext *DC, BuiltinTemplateKind BTK) {
1455
1.07k
  switch (BTK) {
1456
603
  case BTK__make_integer_seq:
1457
603
    return createMakeIntegerSeqParameterList(C, DC);
1458
476
  case BTK__type_pack_element:
1459
476
    return createTypePackElementParameterList(C, DC);
1460
1.07k
  }
1461
1462
0
  llvm_unreachable("unhandled BuiltinTemplateKind!");
1463
0
}
1464
1465
0
void BuiltinTemplateDecl::anchor() {}
1466
1467
BuiltinTemplateDecl::BuiltinTemplateDecl(const ASTContext &C, DeclContext *DC,
1468
                                         DeclarationName Name,
1469
                                         BuiltinTemplateKind BTK)
1470
    : TemplateDecl(BuiltinTemplate, DC, SourceLocation(), Name,
1471
                   createBuiltinTemplateParameterList(C, DC, BTK)),
1472
1.07k
      BTK(BTK) {}
1473
1474
33
void TypeConstraint::print(llvm::raw_ostream &OS, PrintingPolicy Policy) const {
1475
33
  if (NestedNameSpec)
1476
0
    NestedNameSpec.getNestedNameSpecifier()->print(OS, Policy);
1477
33
  ConceptName.printName(OS, Policy);
1478
33
  if (hasExplicitTemplateArgs()) {
1479
8
    OS << "<";
1480
    // FIXME: Find corresponding parameter for argument
1481
8
    for (auto &ArgLoc : ArgsAsWritten->arguments())
1482
8
      ArgLoc.getArgument().print(Policy, OS, /*IncludeType*/ false);
1483
8
    OS << ">";
1484
8
  }
1485
33
}
1486
1487
TemplateParamObjectDecl *TemplateParamObjectDecl::Create(const ASTContext &C,
1488
                                                         QualType T,
1489
190
                                                         const APValue &V) {
1490
190
  DeclContext *DC = C.getTranslationUnitDecl();
1491
190
  auto *TPOD = new (C, DC) TemplateParamObjectDecl(DC, T, V);
1492
190
  C.addDestruction(&TPOD->Value);
1493
190
  return TPOD;
1494
190
}
1495
1496
TemplateParamObjectDecl *
1497
2
TemplateParamObjectDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1498
2
  auto *TPOD = new (C, ID) TemplateParamObjectDecl(nullptr, QualType(), APValue());
1499
2
  C.addDestruction(&TPOD->Value);
1500
2
  return TPOD;
1501
2
}
1502
1503
1
void TemplateParamObjectDecl::printName(llvm::raw_ostream &OS) const {
1504
1
  OS << "<template param ";
1505
1
  printAsExpr(OS);
1506
1
  OS << ">";
1507
1
}
1508
1509
5
void TemplateParamObjectDecl::printAsExpr(llvm::raw_ostream &OS) const {
1510
5
  const ASTContext &Ctx = getASTContext();
1511
5
  getType().getUnqualifiedType().print(OS, Ctx.getPrintingPolicy());
1512
5
  printAsInit(OS);
1513
5
}
1514
1515
28
void TemplateParamObjectDecl::printAsInit(llvm::raw_ostream &OS) const {
1516
28
  const ASTContext &Ctx = getASTContext();
1517
28
  getValue().printPretty(OS, Ctx, getType());
1518
28
}