Coverage Report

Created: 2021-08-24 07:12

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