Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/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
TemplateParameterList::TemplateParameterList(SourceLocation TemplateLoc,
47
                                             SourceLocation LAngleLoc,
48
                                             ArrayRef<NamedDecl *> Params,
49
                                             SourceLocation RAngleLoc,
50
                                             Expr *RequiresClause)
51
    : TemplateLoc(TemplateLoc), LAngleLoc(LAngleLoc), RAngleLoc(RAngleLoc),
52
      NumParams(Params.size()), ContainsUnexpandedParameterPack(false),
53
1.84M
      HasRequiresClause(static_cast<bool>(RequiresClause)) {
54
4.83M
  for (unsigned Idx = 0; Idx < NumParams; 
++Idx2.99M
) {
55
2.99M
    NamedDecl *P = Params[Idx];
56
2.99M
    begin()[Idx] = P;
57
2.99M
58
2.99M
    if (!P->isTemplateParameterPack()) {
59
2.77M
      if (const auto *NTTP = dyn_cast<NonTypeTemplateParmDecl>(P))
60
323k
        if (NTTP->getType()->containsUnexpandedParameterPack())
61
38
          ContainsUnexpandedParameterPack = true;
62
2.77M
63
2.77M
      if (const auto *TTP = dyn_cast<TemplateTemplateParmDecl>(P))
64
12.0k
        if (TTP->getTemplateParameters()->containsUnexpandedParameterPack())
65
0
          ContainsUnexpandedParameterPack = true;
66
2.77M
67
2.77M
      // FIXME: If a default argument contains an unexpanded parameter pack, the
68
2.77M
      // template parameter list does too.
69
2.77M
    }
70
2.99M
  }
71
1.84M
  if (RequiresClause) {
72
38
    *getTrailingObjects<Expr *>() = RequiresClause;
73
38
  }
74
1.84M
}
75
76
TemplateParameterList *
77
TemplateParameterList::Create(const ASTContext &C, SourceLocation TemplateLoc,
78
                              SourceLocation LAngleLoc,
79
                              ArrayRef<NamedDecl *> Params,
80
1.81M
                              SourceLocation RAngleLoc, Expr *RequiresClause) {
81
1.81M
  void *Mem = C.Allocate(totalSizeToAlloc<NamedDecl *, Expr *>(
82
1.81M
                             Params.size(), RequiresClause ? 
1u38
:
0u1.81M
),
83
1.81M
                         alignof(TemplateParameterList));
84
1.81M
  return new (Mem) TemplateParameterList(TemplateLoc, LAngleLoc, Params,
85
1.81M
                                         RAngleLoc, RequiresClause);
86
1.81M
}
87
88
6.22M
unsigned TemplateParameterList::getMinRequiredArguments() const {
89
6.22M
  unsigned NumRequiredArgs = 0;
90
12.3M
  for (const NamedDecl *P : asArray()) {
91
12.3M
    if (P->isTemplateParameterPack()) {
92
262k
      if (const auto *NTTP = dyn_cast<NonTypeTemplateParmDecl>(P))
93
12.6k
        if (NTTP->isExpandedParameterPack()) {
94
0
          NumRequiredArgs += NTTP->getNumExpansionTypes();
95
0
          continue;
96
0
        }
97
262k
98
262k
      break;
99
262k
    }
100
12.1M
101
12.1M
    if (const auto *TTP = dyn_cast<TemplateTypeParmDecl>(P)) {
102
10.1M
      if (TTP->hasDefaultArgument())
103
779k
        break;
104
1.92M
    } else if (const auto *NTTP = dyn_cast<NonTypeTemplateParmDecl>(P)) {
105
1.92M
      if (NTTP->hasDefaultArgument())
106
335k
        break;
107
3.22k
    } else if (cast<TemplateTemplateParmDecl>(P)->hasDefaultArgument())
108
230
      break;
109
11.0M
110
11.0M
    ++NumRequiredArgs;
111
11.0M
  }
112
6.22M
113
6.22M
  return NumRequiredArgs;
114
6.22M
}
115
116
2.34M
unsigned TemplateParameterList::getDepth() const {
117
2.34M
  if (size() == 0)
118
0
    return 0;
119
2.34M
120
2.34M
  const NamedDecl *FirstParm = getParam(0);
121
2.34M
  if (const auto *TTP = dyn_cast<TemplateTypeParmDecl>(FirstParm))
122
2.01M
    return TTP->getDepth();
123
326k
  else if (const auto *NTTP = dyn_cast<NonTypeTemplateParmDecl>(FirstParm))
124
320k
    return NTTP->getDepth();
125
5.95k
  else
126
5.95k
    return cast<TemplateTemplateParmDecl>(FirstParm)->getDepth();
127
2.34M
}
128
129
static void AdoptTemplateParameterList(TemplateParameterList *Params,
130
1.44M
                                       DeclContext *Owner) {
131
2.43M
  for (NamedDecl *P : *Params) {
132
2.43M
    P->setDeclContext(Owner);
133
2.43M
134
2.43M
    if (const auto *TTP = dyn_cast<TemplateTemplateParmDecl>(P))
135
10.7k
      AdoptTemplateParameterList(TTP->getTemplateParameters(), Owner);
136
2.43M
  }
137
1.44M
}
138
139
namespace clang {
140
141
205
void *allocateDefaultArgStorageChain(const ASTContext &C) {
142
205
  return new (C) char[sizeof(void*) * 2];
143
205
}
144
145
} // namespace clang
146
147
//===----------------------------------------------------------------------===//
148
// RedeclarableTemplateDecl Implementation
149
//===----------------------------------------------------------------------===//
150
151
0
void RedeclarableTemplateDecl::anchor() {}
152
153
19.7M
RedeclarableTemplateDecl::CommonBase *RedeclarableTemplateDecl::getCommonPtr() const {
154
19.7M
  if (Common)
155
19.0M
    return Common;
156
719k
157
719k
  // Walk the previous-declaration chain until we either find a declaration
158
719k
  // with a common pointer or we run out of previous declarations.
159
719k
  SmallVector<const RedeclarableTemplateDecl *, 2> PrevDecls;
160
731k
  for (const RedeclarableTemplateDecl *Prev = getPreviousDecl(); Prev;
161
719k
       
Prev = Prev->getPreviousDecl()11.5k
) {
162
46.2k
    if (Prev->Common) {
163
34.7k
      Common = Prev->Common;
164
34.7k
      break;
165
34.7k
    }
166
11.5k
167
11.5k
    PrevDecls.push_back(Prev);
168
11.5k
  }
169
719k
170
719k
  // If we never found a common pointer, allocate one now.
171
719k
  if (!Common) {
172
685k
    // FIXME: If any of the declarations is from an AST file, we probably
173
685k
    // need an update record to add the common data.
174
685k
175
685k
    Common = newCommon(getASTContext());
176
685k
  }
177
719k
178
719k
  // Update any previous declarations we saw with the common pointer.
179
719k
  for (const RedeclarableTemplateDecl *Prev : PrevDecls)
180
11.5k
    Prev->Common = Common;
181
719k
182
719k
  return Common;
183
719k
}
184
185
6.65M
void RedeclarableTemplateDecl::loadLazySpecializationsImpl() const {
186
6.65M
  // Grab the most recent declaration to ensure we've loaded any lazy
187
6.65M
  // redeclarations of this template.
188
6.65M
  CommonBase *CommonBasePtr = getMostRecentDecl()->getCommonPtr();
189
6.65M
  if (CommonBasePtr->LazySpecializations) {
190
2.25k
    ASTContext &Context = getASTContext();
191
2.25k
    uint32_t *Specs = CommonBasePtr->LazySpecializations;
192
2.25k
    CommonBasePtr->LazySpecializations = nullptr;
193
7.35k
    for (uint32_t I = 0, N = *Specs++; I != N; 
++I5.10k
)
194
5.10k
      (void)Context.getExternalSource()->GetExternalDecl(Specs[I]);
195
2.25k
  }
196
6.65M
}
197
198
template<class EntryType>
199
typename RedeclarableTemplateDecl::SpecEntryTraits<EntryType>::DeclType *
200
RedeclarableTemplateDecl::findSpecializationImpl(
201
    llvm::FoldingSetVector<EntryType> &Specs, ArrayRef<TemplateArgument> Args,
202
3.68M
    void *&InsertPos) {
203
3.68M
  using SETraits = SpecEntryTraits<EntryType>;
204
3.68M
205
3.68M
  llvm::FoldingSetNodeID ID;
206
3.68M
  EntryType::Profile(ID, Args, getASTContext());
207
3.68M
  EntryType *Entry = Specs.FindNodeOrInsertPos(ID, InsertPos);
208
3.68M
  return Entry ? 
SETraits::getDecl(Entry)->getMostRecentDecl()1.87M
:
nullptr1.80M
;
209
3.68M
}
clang::RedeclarableTemplateDecl::SpecEntryTraits<clang::FunctionTemplateSpecializationInfo>::DeclType* clang::RedeclarableTemplateDecl::findSpecializationImpl<clang::FunctionTemplateSpecializationInfo>(llvm::FoldingSetVector<clang::FunctionTemplateSpecializationInfo, llvm::SmallVector<clang::FunctionTemplateSpecializationInfo*, 8u> >&, llvm::ArrayRef<clang::TemplateArgument>, void*&)
Line
Count
Source
202
677k
    void *&InsertPos) {
203
677k
  using SETraits = SpecEntryTraits<EntryType>;
204
677k
205
677k
  llvm::FoldingSetNodeID ID;
206
677k
  EntryType::Profile(ID, Args, getASTContext());
207
677k
  EntryType *Entry = Specs.FindNodeOrInsertPos(ID, InsertPos);
208
677k
  return Entry ? 
SETraits::getDecl(Entry)->getMostRecentDecl()297k
:
nullptr380k
;
209
677k
}
clang::RedeclarableTemplateDecl::SpecEntryTraits<clang::ClassTemplateSpecializationDecl>::DeclType* clang::RedeclarableTemplateDecl::findSpecializationImpl<clang::ClassTemplateSpecializationDecl>(llvm::FoldingSetVector<clang::ClassTemplateSpecializationDecl, llvm::SmallVector<clang::ClassTemplateSpecializationDecl*, 8u> >&, llvm::ArrayRef<clang::TemplateArgument>, void*&)
Line
Count
Source
202
2.82M
    void *&InsertPos) {
203
2.82M
  using SETraits = SpecEntryTraits<EntryType>;
204
2.82M
205
2.82M
  llvm::FoldingSetNodeID ID;
206
2.82M
  EntryType::Profile(ID, Args, getASTContext());
207
2.82M
  EntryType *Entry = Specs.FindNodeOrInsertPos(ID, InsertPos);
208
2.82M
  return Entry ? 
SETraits::getDecl(Entry)->getMostRecentDecl()1.58M
:
nullptr1.24M
;
209
2.82M
}
clang::RedeclarableTemplateDecl::SpecEntryTraits<clang::ClassTemplatePartialSpecializationDecl>::DeclType* clang::RedeclarableTemplateDecl::findSpecializationImpl<clang::ClassTemplatePartialSpecializationDecl>(llvm::FoldingSetVector<clang::ClassTemplatePartialSpecializationDecl, llvm::SmallVector<clang::ClassTemplatePartialSpecializationDecl*, 8u> >&, llvm::ArrayRef<clang::TemplateArgument>, void*&)
Line
Count
Source
202
178k
    void *&InsertPos) {
203
178k
  using SETraits = SpecEntryTraits<EntryType>;
204
178k
205
178k
  llvm::FoldingSetNodeID ID;
206
178k
  EntryType::Profile(ID, Args, getASTContext());
207
178k
  EntryType *Entry = Specs.FindNodeOrInsertPos(ID, InsertPos);
208
178k
  return Entry ? 
SETraits::getDecl(Entry)->getMostRecentDecl()702
:
nullptr177k
;
209
178k
}
clang::RedeclarableTemplateDecl::SpecEntryTraits<clang::VarTemplateSpecializationDecl>::DeclType* clang::RedeclarableTemplateDecl::findSpecializationImpl<clang::VarTemplateSpecializationDecl>(llvm::FoldingSetVector<clang::VarTemplateSpecializationDecl, llvm::SmallVector<clang::VarTemplateSpecializationDecl*, 8u> >&, llvm::ArrayRef<clang::TemplateArgument>, void*&)
Line
Count
Source
202
1.79k
    void *&InsertPos) {
203
1.79k
  using SETraits = SpecEntryTraits<EntryType>;
204
1.79k
205
1.79k
  llvm::FoldingSetNodeID ID;
206
1.79k
  EntryType::Profile(ID, Args, getASTContext());
207
1.79k
  EntryType *Entry = Specs.FindNodeOrInsertPos(ID, InsertPos);
208
1.79k
  return Entry ? 
SETraits::getDecl(Entry)->getMostRecentDecl()486
:
nullptr1.31k
;
209
1.79k
}
clang::RedeclarableTemplateDecl::SpecEntryTraits<clang::VarTemplatePartialSpecializationDecl>::DeclType* clang::RedeclarableTemplateDecl::findSpecializationImpl<clang::VarTemplatePartialSpecializationDecl>(llvm::FoldingSetVector<clang::VarTemplatePartialSpecializationDecl, llvm::SmallVector<clang::VarTemplatePartialSpecializationDecl*, 8u> >&, llvm::ArrayRef<clang::TemplateArgument>, void*&)
Line
Count
Source
202
329
    void *&InsertPos) {
203
329
  using SETraits = SpecEntryTraits<EntryType>;
204
329
205
329
  llvm::FoldingSetNodeID ID;
206
329
  EntryType::Profile(ID, Args, getASTContext());
207
329
  EntryType *Entry = Specs.FindNodeOrInsertPos(ID, InsertPos);
208
329
  return Entry ? 
SETraits::getDecl(Entry)->getMostRecentDecl()62
:
nullptr267
;
209
329
}
210
211
template<class Derived, class EntryType>
212
void RedeclarableTemplateDecl::addSpecializationImpl(
213
    llvm::FoldingSetVector<EntryType> &Specializations, EntryType *Entry,
214
1.54M
    void *InsertPos) {
215
1.54M
  using SETraits = SpecEntryTraits<EntryType>;
216
1.54M
217
1.54M
  if (InsertPos) {
218
#ifndef NDEBUG
219
    void *CorrectInsertPos;
220
    assert(!findSpecializationImpl(Specializations,
221
                                   SETraits::getTemplateArgs(Entry),
222
                                   CorrectInsertPos) &&
223
           InsertPos == CorrectInsertPos &&
224
           "given incorrect InsertPos for specialization");
225
#endif
226
    Specializations.InsertNode(Entry, InsertPos);
227
1.24M
  } else {
228
303k
    EntryType *Existing = Specializations.GetOrInsertNode(Entry);
229
303k
    (void)Existing;
230
303k
    assert(SETraits::getDecl(Existing)->isCanonicalDecl() &&
231
303k
           "non-canonical specialization?");
232
303k
  }
233
1.54M
234
1.54M
  if (ASTMutationListener *L = getASTMutationListener())
235
4.64k
    L->AddedCXXTemplateSpecialization(cast<Derived>(this),
236
4.64k
                                      SETraits::getDecl(Entry));
237
1.54M
}
void clang::RedeclarableTemplateDecl::addSpecializationImpl<clang::FunctionTemplateDecl, clang::FunctionTemplateSpecializationInfo>(llvm::FoldingSetVector<clang::FunctionTemplateSpecializationInfo, llvm::SmallVector<clang::FunctionTemplateSpecializationInfo*, 8u> >&, clang::FunctionTemplateSpecializationInfo*, void*)
Line
Count
Source
214
303k
    void *InsertPos) {
215
303k
  using SETraits = SpecEntryTraits<EntryType>;
216
303k
217
303k
  if (InsertPos) {
218
#ifndef NDEBUG
219
    void *CorrectInsertPos;
220
    assert(!findSpecializationImpl(Specializations,
221
                                   SETraits::getTemplateArgs(Entry),
222
                                   CorrectInsertPos) &&
223
           InsertPos == CorrectInsertPos &&
224
           "given incorrect InsertPos for specialization");
225
#endif
226
    Specializations.InsertNode(Entry, InsertPos);
227
303k
  } else {
228
303k
    EntryType *Existing = Specializations.GetOrInsertNode(Entry);
229
303k
    (void)Existing;
230
303k
    assert(SETraits::getDecl(Existing)->isCanonicalDecl() &&
231
303k
           "non-canonical specialization?");
232
303k
  }
233
303k
234
303k
  if (ASTMutationListener *L = getASTMutationListener())
235
1.05k
    L->AddedCXXTemplateSpecialization(cast<Derived>(this),
236
1.05k
                                      SETraits::getDecl(Entry));
237
303k
}
void clang::RedeclarableTemplateDecl::addSpecializationImpl<clang::ClassTemplateDecl, clang::ClassTemplateSpecializationDecl>(llvm::FoldingSetVector<clang::ClassTemplateSpecializationDecl, llvm::SmallVector<clang::ClassTemplateSpecializationDecl*, 8u> >&, clang::ClassTemplateSpecializationDecl*, void*)
Line
Count
Source
214
1.24M
    void *InsertPos) {
215
1.24M
  using SETraits = SpecEntryTraits<EntryType>;
216
1.24M
217
1.24M
  if (InsertPos) {
218
#ifndef NDEBUG
219
    void *CorrectInsertPos;
220
    assert(!findSpecializationImpl(Specializations,
221
                                   SETraits::getTemplateArgs(Entry),
222
                                   CorrectInsertPos) &&
223
           InsertPos == CorrectInsertPos &&
224
           "given incorrect InsertPos for specialization");
225
#endif
226
    Specializations.InsertNode(Entry, InsertPos);
227
1.24M
  } else {
228
0
    EntryType *Existing = Specializations.GetOrInsertNode(Entry);
229
0
    (void)Existing;
230
0
    assert(SETraits::getDecl(Existing)->isCanonicalDecl() &&
231
0
           "non-canonical specialization?");
232
0
  }
233
1.24M
234
1.24M
  if (ASTMutationListener *L = getASTMutationListener())
235
3.49k
    L->AddedCXXTemplateSpecialization(cast<Derived>(this),
236
3.49k
                                      SETraits::getDecl(Entry));
237
1.24M
}
void clang::RedeclarableTemplateDecl::addSpecializationImpl<clang::VarTemplateDecl, clang::VarTemplateSpecializationDecl>(llvm::FoldingSetVector<clang::VarTemplateSpecializationDecl, llvm::SmallVector<clang::VarTemplateSpecializationDecl*, 8u> >&, clang::VarTemplateSpecializationDecl*, void*)
Line
Count
Source
214
1.30k
    void *InsertPos) {
215
1.30k
  using SETraits = SpecEntryTraits<EntryType>;
216
1.30k
217
1.30k
  if (InsertPos) {
218
#ifndef NDEBUG
219
    void *CorrectInsertPos;
220
    assert(!findSpecializationImpl(Specializations,
221
                                   SETraits::getTemplateArgs(Entry),
222
                                   CorrectInsertPos) &&
223
           InsertPos == CorrectInsertPos &&
224
           "given incorrect InsertPos for specialization");
225
#endif
226
    Specializations.InsertNode(Entry, InsertPos);
227
1.30k
  } else {
228
0
    EntryType *Existing = Specializations.GetOrInsertNode(Entry);
229
0
    (void)Existing;
230
0
    assert(SETraits::getDecl(Existing)->isCanonicalDecl() &&
231
0
           "non-canonical specialization?");
232
0
  }
233
1.30k
234
1.30k
  if (ASTMutationListener *L = getASTMutationListener())
235
90
    L->AddedCXXTemplateSpecialization(cast<Derived>(this),
236
90
                                      SETraits::getDecl(Entry));
237
1.30k
}
238
239
//===----------------------------------------------------------------------===//
240
// FunctionTemplateDecl Implementation
241
//===----------------------------------------------------------------------===//
242
243
FunctionTemplateDecl *FunctionTemplateDecl::Create(ASTContext &C,
244
                                                   DeclContext *DC,
245
                                                   SourceLocation L,
246
                                                   DeclarationName Name,
247
                                               TemplateParameterList *Params,
248
851k
                                                   NamedDecl *Decl) {
249
851k
  AdoptTemplateParameterList(Params, cast<DeclContext>(Decl));
250
851k
  return new (C, DC) FunctionTemplateDecl(C, DC, L, Name, Params, Decl);
251
851k
}
252
253
FunctionTemplateDecl *FunctionTemplateDecl::CreateDeserialized(ASTContext &C,
254
1.68k
                                                               unsigned ID) {
255
1.68k
  return new (C, ID) FunctionTemplateDecl(C, nullptr, SourceLocation(),
256
1.68k
                                          DeclarationName(), nullptr, nullptr);
257
1.68k
}
258
259
RedeclarableTemplateDecl::CommonBase *
260
360k
FunctionTemplateDecl::newCommon(ASTContext &C) const {
261
360k
  auto *CommonPtr = new (C) Common;
262
360k
  C.addDestruction(CommonPtr);
263
360k
  return CommonPtr;
264
360k
}
265
266
997k
void FunctionTemplateDecl::LoadLazySpecializations() const {
267
997k
  loadLazySpecializationsImpl();
268
997k
}
269
270
llvm::FoldingSetVector<FunctionTemplateSpecializationInfo> &
271
995k
FunctionTemplateDecl::getSpecializations() const {
272
995k
  LoadLazySpecializations();
273
995k
  return getCommonPtr()->Specializations;
274
995k
}
275
276
FunctionDecl *
277
FunctionTemplateDecl::findSpecialization(ArrayRef<TemplateArgument> Args,
278
677k
                                         void *&InsertPos) {
279
677k
  return findSpecializationImpl(getSpecializations(), Args, InsertPos);
280
677k
}
281
282
void FunctionTemplateDecl::addSpecialization(
283
303k
      FunctionTemplateSpecializationInfo *Info, void *InsertPos) {
284
303k
  addSpecializationImpl<FunctionTemplateDecl>(getSpecializations(), Info,
285
303k
                                              InsertPos);
286
303k
}
287
288
12
ArrayRef<TemplateArgument> FunctionTemplateDecl::getInjectedTemplateArgs() {
289
12
  TemplateParameterList *Params = getTemplateParameters();
290
12
  Common *CommonPtr = getCommonPtr();
291
12
  if (!CommonPtr->InjectedArgs) {
292
7
    auto &Context = getASTContext();
293
7
    SmallVector<TemplateArgument, 16> TemplateArgs;
294
7
    Context.getInjectedTemplateArgs(Params, TemplateArgs);
295
7
    CommonPtr->InjectedArgs =
296
7
        new (Context) TemplateArgument[TemplateArgs.size()];
297
7
    std::copy(TemplateArgs.begin(), TemplateArgs.end(),
298
7
              CommonPtr->InjectedArgs);
299
7
  }
300
12
301
12
  return llvm::makeArrayRef(CommonPtr->InjectedArgs, Params->size());
302
12
}
303
304
68.9k
void FunctionTemplateDecl::mergePrevDecl(FunctionTemplateDecl *Prev) {
305
68.9k
  using Base = RedeclarableTemplateDecl;
306
68.9k
307
68.9k
  // If we haven't created a common pointer yet, then it can just be created
308
68.9k
  // with the usual method.
309
68.9k
  if (!Base::Common)
310
68.9k
    return;
311
8
312
8
  Common *ThisCommon = static_cast<Common *>(Base::Common);
313
8
  Common *PrevCommon = nullptr;
314
8
  SmallVector<FunctionTemplateDecl *, 8> PreviousDecls;
315
13
  for (; Prev; 
Prev = Prev->getPreviousDecl()5
) {
316
8
    if (Prev->Base::Common) {
317
3
      PrevCommon = static_cast<Common *>(Prev->Base::Common);
318
3
      break;
319
3
    }
320
5
    PreviousDecls.push_back(Prev);
321
5
  }
322
8
323
8
  // If the previous redecl chain hasn't created a common pointer yet, then just
324
8
  // use this common pointer.
325
8
  if (!PrevCommon) {
326
5
    for (auto *D : PreviousDecls)
327
5
      D->Base::Common = ThisCommon;
328
5
    return;
329
5
  }
330
3
331
3
  // Ensure we don't leak any important state.
332
3
  assert(ThisCommon->Specializations.size() == 0 &&
333
3
         "Can't merge incompatible declarations!");
334
3
335
3
  Base::Common = PrevCommon;
336
3
}
337
338
//===----------------------------------------------------------------------===//
339
// ClassTemplateDecl Implementation
340
//===----------------------------------------------------------------------===//
341
342
ClassTemplateDecl *ClassTemplateDecl::Create(ASTContext &C,
343
                                             DeclContext *DC,
344
                                             SourceLocation L,
345
                                             DeclarationName Name,
346
                                             TemplateParameterList *Params,
347
                                             NamedDecl *Decl,
348
319k
                                             Expr *AssociatedConstraints) {
349
319k
  AdoptTemplateParameterList(Params, cast<DeclContext>(Decl));
350
319k
351
319k
  if (!AssociatedConstraints) {
352
319k
    return new (C, DC) ClassTemplateDecl(C, DC, L, Name, Params, Decl);
353
319k
  }
354
8
355
8
  auto *const CTDI = new (C) ConstrainedTemplateDeclInfo;
356
8
  auto *const New =
357
8
      new (C, DC) ClassTemplateDecl(CTDI, C, DC, L, Name, Params, Decl);
358
8
  New->setAssociatedConstraints(AssociatedConstraints);
359
8
  return New;
360
8
}
361
362
ClassTemplateDecl *ClassTemplateDecl::CreateDeserialized(ASTContext &C,
363
2.85k
                                                         unsigned ID) {
364
2.85k
  return new (C, ID) ClassTemplateDecl(C, nullptr, SourceLocation(),
365
2.85k
                                       DeclarationName(), nullptr, nullptr);
366
2.85k
}
367
368
5.65M
void ClassTemplateDecl::LoadLazySpecializations() const {
369
5.65M
  loadLazySpecializationsImpl();
370
5.65M
}
371
372
llvm::FoldingSetVector<ClassTemplateSpecializationDecl> &
373
4.07M
ClassTemplateDecl::getSpecializations() const {
374
4.07M
  LoadLazySpecializations();
375
4.07M
  return getCommonPtr()->Specializations;
376
4.07M
}
377
378
llvm::FoldingSetVector<ClassTemplatePartialSpecializationDecl> &
379
1.57M
ClassTemplateDecl::getPartialSpecializations() {
380
1.57M
  LoadLazySpecializations();
381
1.57M
  return getCommonPtr()->PartialSpecializations;
382
1.57M
}
383
384
RedeclarableTemplateDecl::CommonBase *
385
299k
ClassTemplateDecl::newCommon(ASTContext &C) const {
386
299k
  auto *CommonPtr = new (C) Common;
387
299k
  C.addDestruction(CommonPtr);
388
299k
  return CommonPtr;
389
299k
}
390
391
ClassTemplateSpecializationDecl *
392
ClassTemplateDecl::findSpecialization(ArrayRef<TemplateArgument> Args,
393
2.82M
                                      void *&InsertPos) {
394
2.82M
  return findSpecializationImpl(getSpecializations(), Args, InsertPos);
395
2.82M
}
396
397
void ClassTemplateDecl::AddSpecialization(ClassTemplateSpecializationDecl *D,
398
1.24M
                                          void *InsertPos) {
399
1.24M
  addSpecializationImpl<ClassTemplateDecl>(getSpecializations(), D, InsertPos);
400
1.24M
}
401
402
ClassTemplatePartialSpecializationDecl *
403
ClassTemplateDecl::findPartialSpecialization(ArrayRef<TemplateArgument> Args,
404
178k
                                             void *&InsertPos) {
405
178k
  return findSpecializationImpl(getPartialSpecializations(), Args, InsertPos);
406
178k
}
407
408
void ClassTemplateDecl::AddPartialSpecialization(
409
                                      ClassTemplatePartialSpecializationDecl *D,
410
177k
                                      void *InsertPos) {
411
177k
  if (InsertPos)
412
173k
    getPartialSpecializations().InsertNode(D, InsertPos);
413
4.20k
  else {
414
4.20k
    ClassTemplatePartialSpecializationDecl *Existing
415
4.20k
      = getPartialSpecializations().GetOrInsertNode(D);
416
4.20k
    (void)Existing;
417
4.20k
    assert(Existing->isCanonicalDecl() && "Non-canonical specialization?");
418
4.20k
  }
419
177k
420
177k
  if (ASTMutationListener *L = getASTMutationListener())
421
99
    L->AddedCXXTemplateSpecialization(this, D);
422
177k
}
423
424
void ClassTemplateDecl::getPartialSpecializations(
425
1.12M
          SmallVectorImpl<ClassTemplatePartialSpecializationDecl *> &PS) {
426
1.12M
  llvm::FoldingSetVector<ClassTemplatePartialSpecializationDecl> &PartialSpecs
427
1.12M
    = getPartialSpecializations();
428
1.12M
  PS.clear();
429
1.12M
  PS.reserve(PartialSpecs.size());
430
1.12M
  for (ClassTemplatePartialSpecializationDecl &P : PartialSpecs)
431
843k
    PS.push_back(P.getMostRecentDecl());
432
1.12M
}
433
434
ClassTemplatePartialSpecializationDecl *
435
85.8k
ClassTemplateDecl::findPartialSpecialization(QualType T) {
436
85.8k
  ASTContext &Context = getASTContext();
437
85.8k
  for (ClassTemplatePartialSpecializationDecl &P :
438
142k
       getPartialSpecializations()) {
439
142k
    if (Context.hasSameType(P.getInjectedSpecializationType(), T))
440
84.3k
      return P.getMostRecentDecl();
441
142k
  }
442
85.8k
443
85.8k
  
return nullptr1.51k
;
444
85.8k
}
445
446
ClassTemplatePartialSpecializationDecl *
447
ClassTemplateDecl::findPartialSpecInstantiatedFromMember(
448
4.20k
                                    ClassTemplatePartialSpecializationDecl *D) {
449
4.20k
  Decl *DCanon = D->getCanonicalDecl();
450
4.20k
  for (ClassTemplatePartialSpecializationDecl &P : getPartialSpecializations()) {
451
343
    if (P.getInstantiatedFromMember()->getCanonicalDecl() == DCanon)
452
0
      return P.getMostRecentDecl();
453
343
  }
454
4.20k
455
4.20k
  return nullptr;
456
4.20k
}
457
458
QualType
459
1.00M
ClassTemplateDecl::getInjectedClassNameSpecialization() {
460
1.00M
  Common *CommonPtr = getCommonPtr();
461
1.00M
  if (!CommonPtr->InjectedClassNameType.isNull())
462
703k
    return CommonPtr->InjectedClassNameType;
463
298k
464
298k
  // C++0x [temp.dep.type]p2:
465
298k
  //  The template argument list of a primary template is a template argument
466
298k
  //  list in which the nth template argument has the value of the nth template
467
298k
  //  parameter of the class template. If the nth template parameter is a
468
298k
  //  template parameter pack (14.5.3), the nth template argument is a pack
469
298k
  //  expansion (14.5.3) whose pattern is the name of the template parameter
470
298k
  //  pack.
471
298k
  ASTContext &Context = getASTContext();
472
298k
  TemplateParameterList *Params = getTemplateParameters();
473
298k
  SmallVector<TemplateArgument, 16> TemplateArgs;
474
298k
  Context.getInjectedTemplateArgs(Params, TemplateArgs);
475
298k
  CommonPtr->InjectedClassNameType
476
298k
    = Context.getTemplateSpecializationType(TemplateName(this),
477
298k
                                            TemplateArgs);
478
298k
  return CommonPtr->InjectedClassNameType;
479
298k
}
480
481
//===----------------------------------------------------------------------===//
482
// TemplateTypeParm Allocation/Deallocation Method Implementations
483
//===----------------------------------------------------------------------===//
484
485
TemplateTypeParmDecl *
486
TemplateTypeParmDecl::Create(const ASTContext &C, DeclContext *DC,
487
                             SourceLocation KeyLoc, SourceLocation NameLoc,
488
                             unsigned D, unsigned P, IdentifierInfo *Id,
489
2.62M
                             bool Typename, bool ParameterPack) {
490
2.62M
  auto *TTPDecl =
491
2.62M
      new (C, DC) TemplateTypeParmDecl(DC, KeyLoc, NameLoc, Id, Typename);
492
2.62M
  QualType TTPType = C.getTemplateTypeParmType(D, P, ParameterPack, TTPDecl);
493
2.62M
  TTPDecl->setTypeForDecl(TTPType.getTypePtr());
494
2.62M
  return TTPDecl;
495
2.62M
}
496
497
TemplateTypeParmDecl *
498
4.44k
TemplateTypeParmDecl::CreateDeserialized(const ASTContext &C, unsigned ID) {
499
4.44k
  return new (C, ID) TemplateTypeParmDecl(nullptr, SourceLocation(),
500
4.44k
                                          SourceLocation(), nullptr, false);
501
4.44k
}
502
503
371k
SourceLocation TemplateTypeParmDecl::getDefaultArgumentLoc() const {
504
371k
  return hasDefaultArgument()
505
371k
             ? getDefaultArgumentInfo()->getTypeLoc().getBeginLoc()
506
371k
             : 
SourceLocation()0
;
507
371k
}
508
509
1.99k
SourceRange TemplateTypeParmDecl::getSourceRange() const {
510
1.99k
  if (hasDefaultArgument() && 
!defaultArgumentWasInherited()53
)
511
52
    return SourceRange(getBeginLoc(),
512
52
                       getDefaultArgumentInfo()->getTypeLoc().getEndLoc());
513
1.94k
  else
514
1.94k
    return TypeDecl::getSourceRange();
515
1.99k
}
516
517
2.96M
unsigned TemplateTypeParmDecl::getDepth() const {
518
2.96M
  return getTypeForDecl()->getAs<TemplateTypeParmType>()->getDepth();
519
2.96M
}
520
521
722k
unsigned TemplateTypeParmDecl::getIndex() const {
522
722k
  return getTypeForDecl()->getAs<TemplateTypeParmType>()->getIndex();
523
722k
}
524
525
28.8M
bool TemplateTypeParmDecl::isParameterPack() const {
526
28.8M
  return getTypeForDecl()->getAs<TemplateTypeParmType>()->isParameterPack();
527
28.8M
}
528
529
//===----------------------------------------------------------------------===//
530
// NonTypeTemplateParmDecl Method Implementations
531
//===----------------------------------------------------------------------===//
532
533
NonTypeTemplateParmDecl::NonTypeTemplateParmDecl(
534
    DeclContext *DC, SourceLocation StartLoc, SourceLocation IdLoc, unsigned D,
535
    unsigned P, IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo,
536
    ArrayRef<QualType> ExpandedTypes, ArrayRef<TypeSourceInfo *> ExpandedTInfos)
537
    : DeclaratorDecl(NonTypeTemplateParm, DC, IdLoc, Id, T, TInfo, StartLoc),
538
      TemplateParmPosition(D, P), ParameterPack(true),
539
39
      ExpandedParameterPack(true), NumExpandedTypes(ExpandedTypes.size()) {
540
39
  if (!ExpandedTypes.empty() && 
!ExpandedTInfos.empty()31
) {
541
31
    auto TypesAndInfos =
542
31
        getTrailingObjects<std::pair<QualType, TypeSourceInfo *>>();
543
97
    for (unsigned I = 0; I != NumExpandedTypes; 
++I66
) {
544
66
      new (&TypesAndInfos[I].first) QualType(ExpandedTypes[I]);
545
66
      TypesAndInfos[I].second = ExpandedTInfos[I];
546
66
    }
547
31
  }
548
39
}
549
550
NonTypeTemplateParmDecl *
551
NonTypeTemplateParmDecl::Create(const ASTContext &C, DeclContext *DC,
552
                                SourceLocation StartLoc, SourceLocation IdLoc,
553
                                unsigned D, unsigned P, IdentifierInfo *Id,
554
                                QualType T, bool ParameterPack,
555
346k
                                TypeSourceInfo *TInfo) {
556
346k
  return new (C, DC) NonTypeTemplateParmDecl(DC, StartLoc, IdLoc, D, P, Id,
557
346k
                                             T, ParameterPack, TInfo);
558
346k
}
559
560
NonTypeTemplateParmDecl *NonTypeTemplateParmDecl::Create(
561
    const ASTContext &C, DeclContext *DC, SourceLocation StartLoc,
562
    SourceLocation IdLoc, unsigned D, unsigned P, IdentifierInfo *Id,
563
    QualType T, TypeSourceInfo *TInfo, ArrayRef<QualType> ExpandedTypes,
564
37
    ArrayRef<TypeSourceInfo *> ExpandedTInfos) {
565
37
  return new (C, DC,
566
37
              additionalSizeToAlloc<std::pair<QualType, TypeSourceInfo *>>(
567
37
                  ExpandedTypes.size()))
568
37
      NonTypeTemplateParmDecl(DC, StartLoc, IdLoc, D, P, Id, T, TInfo,
569
37
                              ExpandedTypes, ExpandedTInfos);
570
37
}
571
572
NonTypeTemplateParmDecl *
573
1.93k
NonTypeTemplateParmDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
574
1.93k
  return new (C, ID) NonTypeTemplateParmDecl(nullptr, SourceLocation(),
575
1.93k
                                             SourceLocation(), 0, 0, nullptr,
576
1.93k
                                             QualType(), false, nullptr);
577
1.93k
}
578
579
NonTypeTemplateParmDecl *
580
NonTypeTemplateParmDecl::CreateDeserialized(ASTContext &C, unsigned ID,
581
2
                                            unsigned NumExpandedTypes) {
582
2
  auto *NTTP =
583
2
      new (C, ID, additionalSizeToAlloc<std::pair<QualType, TypeSourceInfo *>>(
584
2
                      NumExpandedTypes))
585
2
          NonTypeTemplateParmDecl(nullptr, SourceLocation(), SourceLocation(),
586
2
                                  0, 0, nullptr, QualType(), nullptr, None,
587
2
                                  None);
588
2
  NTTP->NumExpandedTypes = NumExpandedTypes;
589
2
  return NTTP;
590
2
}
591
592
235
SourceRange NonTypeTemplateParmDecl::getSourceRange() const {
593
235
  if (hasDefaultArgument() && 
!defaultArgumentWasInherited()13
)
594
11
    return SourceRange(getOuterLocStart(),
595
11
                       getDefaultArgument()->getSourceRange().getEnd());
596
224
  return DeclaratorDecl::getSourceRange();
597
224
}
598
599
52.4k
SourceLocation NonTypeTemplateParmDecl::getDefaultArgumentLoc() const {
600
52.4k
  return hasDefaultArgument()
601
52.4k
    ? getDefaultArgument()->getSourceRange().getBegin()
602
52.4k
    : 
SourceLocation()0
;
603
52.4k
}
604
605
//===----------------------------------------------------------------------===//
606
// TemplateTemplateParmDecl Method Implementations
607
//===----------------------------------------------------------------------===//
608
609
0
void TemplateTemplateParmDecl::anchor() {}
610
611
TemplateTemplateParmDecl::TemplateTemplateParmDecl(
612
    DeclContext *DC, SourceLocation L, unsigned D, unsigned P,
613
    IdentifierInfo *Id, TemplateParameterList *Params,
614
    ArrayRef<TemplateParameterList *> Expansions)
615
    : TemplateDecl(TemplateTemplateParm, DC, L, Id, Params),
616
      TemplateParmPosition(D, P), ParameterPack(true),
617
15
      ExpandedParameterPack(true), NumExpandedParams(Expansions.size()) {
618
15
  if (!Expansions.empty())
619
13
    std::uninitialized_copy(Expansions.begin(), Expansions.end(),
620
13
                            getTrailingObjects<TemplateParameterList *>());
621
15
}
622
623
TemplateTemplateParmDecl *
624
TemplateTemplateParmDecl::Create(const ASTContext &C, DeclContext *DC,
625
                                 SourceLocation L, unsigned D, unsigned P,
626
                                 bool ParameterPack, IdentifierInfo *Id,
627
14.8k
                                 TemplateParameterList *Params) {
628
14.8k
  return new (C, DC) TemplateTemplateParmDecl(DC, L, D, P, ParameterPack, Id,
629
14.8k
                                              Params);
630
14.8k
}
631
632
TemplateTemplateParmDecl *
633
TemplateTemplateParmDecl::Create(const ASTContext &C, DeclContext *DC,
634
                                 SourceLocation L, unsigned D, unsigned P,
635
                                 IdentifierInfo *Id,
636
                                 TemplateParameterList *Params,
637
13
                                 ArrayRef<TemplateParameterList *> Expansions) {
638
13
  return new (C, DC,
639
13
              additionalSizeToAlloc<TemplateParameterList *>(Expansions.size()))
640
13
      TemplateTemplateParmDecl(DC, L, D, P, Id, Params, Expansions);
641
13
}
642
643
TemplateTemplateParmDecl *
644
321
TemplateTemplateParmDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
645
321
  return new (C, ID) TemplateTemplateParmDecl(nullptr, SourceLocation(), 0, 0,
646
321
                                              false, nullptr, nullptr);
647
321
}
648
649
TemplateTemplateParmDecl *
650
TemplateTemplateParmDecl::CreateDeserialized(ASTContext &C, unsigned ID,
651
2
                                             unsigned NumExpansions) {
652
2
  auto *TTP =
653
2
      new (C, ID, additionalSizeToAlloc<TemplateParameterList *>(NumExpansions))
654
2
          TemplateTemplateParmDecl(nullptr, SourceLocation(), 0, 0, nullptr,
655
2
                                   nullptr, None);
656
2
  TTP->NumExpandedParams = NumExpansions;
657
2
  return TTP;
658
2
}
659
660
0
SourceLocation TemplateTemplateParmDecl::getDefaultArgumentLoc() const {
661
0
  return hasDefaultArgument() ? getDefaultArgument().getLocation()
662
0
                              : SourceLocation();
663
0
}
664
665
void TemplateTemplateParmDecl::setDefaultArgument(
666
258
    const ASTContext &C, const TemplateArgumentLoc &DefArg) {
667
258
  if (DefArg.getArgument().isNull())
668
0
    DefaultArgument.set(nullptr);
669
258
  else
670
258
    DefaultArgument.set(new (C) TemplateArgumentLoc(DefArg));
671
258
}
672
673
//===----------------------------------------------------------------------===//
674
// TemplateArgumentList Implementation
675
//===----------------------------------------------------------------------===//
676
TemplateArgumentList::TemplateArgumentList(ArrayRef<TemplateArgument> Args)
677
    : Arguments(getTrailingObjects<TemplateArgument>()),
678
3.08M
      NumArguments(Args.size()) {
679
3.08M
  std::uninitialized_copy(Args.begin(), Args.end(),
680
3.08M
                          getTrailingObjects<TemplateArgument>());
681
3.08M
}
682
683
TemplateArgumentList *
684
TemplateArgumentList::CreateCopy(ASTContext &Context,
685
3.08M
                                 ArrayRef<TemplateArgument> Args) {
686
3.08M
  void *Mem = Context.Allocate(totalSizeToAlloc<TemplateArgument>(Args.size()));
687
3.08M
  return new (Mem) TemplateArgumentList(Args);
688
3.08M
}
689
690
FunctionTemplateSpecializationInfo *FunctionTemplateSpecializationInfo::Create(
691
    ASTContext &C, FunctionDecl *FD, FunctionTemplateDecl *Template,
692
    TemplateSpecializationKind TSK, const TemplateArgumentList *TemplateArgs,
693
    const TemplateArgumentListInfo *TemplateArgsAsWritten, SourceLocation POI,
694
304k
    MemberSpecializationInfo *MSInfo) {
695
304k
  const ASTTemplateArgumentListInfo *ArgsAsWritten = nullptr;
696
304k
  if (TemplateArgsAsWritten)
697
6.38k
    ArgsAsWritten = ASTTemplateArgumentListInfo::Create(C,
698
6.38k
                                                        *TemplateArgsAsWritten);
699
304k
700
304k
  void *Mem =
701
304k
      C.Allocate(totalSizeToAlloc<MemberSpecializationInfo *>(MSInfo ? 
196
:
0304k
));
702
304k
  return new (Mem) FunctionTemplateSpecializationInfo(
703
304k
      FD, Template, TSK, TemplateArgs, ArgsAsWritten, POI, MSInfo);
704
304k
}
705
706
//===----------------------------------------------------------------------===//
707
// TemplateDecl Implementation
708
//===----------------------------------------------------------------------===//
709
710
0
void TemplateDecl::anchor() {}
711
712
//===----------------------------------------------------------------------===//
713
// ClassTemplateSpecializationDecl Implementation
714
//===----------------------------------------------------------------------===//
715
716
ClassTemplateSpecializationDecl::
717
ClassTemplateSpecializationDecl(ASTContext &Context, Kind DK, TagKind TK,
718
                                DeclContext *DC, SourceLocation StartLoc,
719
                                SourceLocation IdLoc,
720
                                ClassTemplateDecl *SpecializedTemplate,
721
                                ArrayRef<TemplateArgument> Args,
722
                                ClassTemplateSpecializationDecl *PrevDecl)
723
    : CXXRecordDecl(DK, TK, Context, DC, StartLoc, IdLoc,
724
                    SpecializedTemplate->getIdentifier(), PrevDecl),
725
    SpecializedTemplate(SpecializedTemplate),
726
    TemplateArgs(TemplateArgumentList::CreateCopy(Context, Args)),
727
1.42M
    SpecializationKind(TSK_Undeclared) {
728
1.42M
}
729
730
ClassTemplateSpecializationDecl::ClassTemplateSpecializationDecl(ASTContext &C,
731
                                                                 Kind DK)
732
    : CXXRecordDecl(DK, TTK_Struct, C, nullptr, SourceLocation(),
733
                    SourceLocation(), nullptr, nullptr),
734
4.04k
      SpecializationKind(TSK_Undeclared) {}
735
736
ClassTemplateSpecializationDecl *
737
ClassTemplateSpecializationDecl::Create(ASTContext &Context, TagKind TK,
738
                                        DeclContext *DC,
739
                                        SourceLocation StartLoc,
740
                                        SourceLocation IdLoc,
741
                                        ClassTemplateDecl *SpecializedTemplate,
742
                                        ArrayRef<TemplateArgument> Args,
743
1.24M
                                   ClassTemplateSpecializationDecl *PrevDecl) {
744
1.24M
  auto *Result =
745
1.24M
      new (Context, DC) ClassTemplateSpecializationDecl(
746
1.24M
          Context, ClassTemplateSpecialization, TK, DC, StartLoc, IdLoc,
747
1.24M
          SpecializedTemplate, Args, PrevDecl);
748
1.24M
  Result->setMayHaveOutOfDateDef(false);
749
1.24M
750
1.24M
  Context.getTypeDeclType(Result, PrevDecl);
751
1.24M
  return Result;
752
1.24M
}
753
754
ClassTemplateSpecializationDecl *
755
ClassTemplateSpecializationDecl::CreateDeserialized(ASTContext &C,
756
3.89k
                                                    unsigned ID) {
757
3.89k
  auto *Result =
758
3.89k
    new (C, ID) ClassTemplateSpecializationDecl(C, ClassTemplateSpecialization);
759
3.89k
  Result->setMayHaveOutOfDateDef(false);
760
3.89k
  return Result;
761
3.89k
}
762
763
void ClassTemplateSpecializationDecl::getNameForDiagnostic(
764
1.41k
    raw_ostream &OS, const PrintingPolicy &Policy, bool Qualified) const {
765
1.41k
  NamedDecl::getNameForDiagnostic(OS, Policy, Qualified);
766
1.41k
767
1.41k
  const auto *PS = dyn_cast<ClassTemplatePartialSpecializationDecl>(this);
768
1.41k
  if (const ASTTemplateArgumentListInfo *ArgsAsWritten =
769
90
          PS ? PS->getTemplateArgsAsWritten() : nullptr) {
770
90
    printTemplateArgumentList(OS, ArgsAsWritten->arguments(), Policy);
771
1.32k
  } else {
772
1.32k
    const TemplateArgumentList &TemplateArgs = getTemplateArgs();
773
1.32k
    printTemplateArgumentList(OS, TemplateArgs.asArray(), Policy);
774
1.32k
  }
775
1.41k
}
776
777
ClassTemplateDecl *
778
13.9M
ClassTemplateSpecializationDecl::getSpecializedTemplate() const {
779
13.9M
  if (const auto *PartialSpec =
780
668k
          SpecializedTemplate.dyn_cast<SpecializedPartialSpecialization*>())
781
668k
    return PartialSpec->PartialSpecialization->getSpecializedTemplate();
782
13.2M
  return SpecializedTemplate.get<ClassTemplateDecl*>();
783
13.2M
}
784
785
SourceRange
786
461k
ClassTemplateSpecializationDecl::getSourceRange() const {
787
461k
  if (ExplicitInfo) {
788
454k
    SourceLocation Begin = getTemplateKeywordLoc();
789
454k
    if (Begin.isValid()) {
790
453k
      // Here we have an explicit (partial) specialization or instantiation.
791
453k
      assert(getSpecializationKind() == TSK_ExplicitSpecialization ||
792
453k
             getSpecializationKind() == TSK_ExplicitInstantiationDeclaration ||
793
453k
             getSpecializationKind() == TSK_ExplicitInstantiationDefinition);
794
453k
      if (getExternLoc().isValid())
795
18
        Begin = getExternLoc();
796
453k
      SourceLocation End = getBraceRange().getEnd();
797
453k
      if (End.isInvalid())
798
298
        End = getTypeAsWritten()->getTypeLoc().getEndLoc();
799
453k
      return SourceRange(Begin, End);
800
453k
    }
801
1.47k
    // An implicit instantiation of a class template partial specialization
802
1.47k
    // uses ExplicitInfo to record the TypeAsWritten, but the source
803
1.47k
    // locations should be retrieved from the instantiation pattern.
804
1.47k
    using CTPSDecl = ClassTemplatePartialSpecializationDecl;
805
1.47k
    auto *ctpsd = const_cast<CTPSDecl *>(cast<CTPSDecl>(this));
806
1.47k
    CTPSDecl *inst_from = ctpsd->getInstantiatedFromMember();
807
1.47k
    assert(inst_from != nullptr);
808
1.47k
    return inst_from->getSourceRange();
809
1.47k
  }
810
6.76k
  else {
811
6.76k
    // No explicit info available.
812
6.76k
    llvm::PointerUnion<ClassTemplateDecl *,
813
6.76k
                       ClassTemplatePartialSpecializationDecl *>
814
6.76k
      inst_from = getInstantiatedFrom();
815
6.76k
    if (inst_from.isNull())
816
6.60k
      return getSpecializedTemplate()->getSourceRange();
817
168
    if (const auto *ctd = inst_from.dyn_cast<ClassTemplateDecl *>())
818
138
      return ctd->getSourceRange();
819
30
    return inst_from.get<ClassTemplatePartialSpecializationDecl *>()
820
30
      ->getSourceRange();
821
30
  }
822
461k
}
823
824
//===----------------------------------------------------------------------===//
825
// ConceptDecl Implementation
826
//===----------------------------------------------------------------------===//
827
ConceptDecl *ConceptDecl::Create(ASTContext &C, DeclContext *DC,
828
                                 SourceLocation L, DeclarationName Name,
829
                                 TemplateParameterList *Params,
830
11
                                 Expr *ConstraintExpr) {
831
11
  AdoptTemplateParameterList(Params, DC);
832
11
  return new (C, DC) ConceptDecl(DC, L, Name, Params, ConstraintExpr);
833
11
}
834
835
ConceptDecl *ConceptDecl::CreateDeserialized(ASTContext &C,
836
0
                                             unsigned ID) {
837
0
  ConceptDecl *Result = new (C, ID) ConceptDecl(nullptr, SourceLocation(),
838
0
                                                DeclarationName(),
839
0
                                                nullptr, nullptr);
840
0
841
0
  return Result;
842
0
}
843
844
//===----------------------------------------------------------------------===//
845
// ClassTemplatePartialSpecializationDecl Implementation
846
//===----------------------------------------------------------------------===//
847
0
void ClassTemplatePartialSpecializationDecl::anchor() {}
848
849
ClassTemplatePartialSpecializationDecl::
850
ClassTemplatePartialSpecializationDecl(ASTContext &Context, TagKind TK,
851
                                       DeclContext *DC,
852
                                       SourceLocation StartLoc,
853
                                       SourceLocation IdLoc,
854
                                       TemplateParameterList *Params,
855
                                       ClassTemplateDecl *SpecializedTemplate,
856
                                       ArrayRef<TemplateArgument> Args,
857
                               const ASTTemplateArgumentListInfo *ArgInfos,
858
                               ClassTemplatePartialSpecializationDecl *PrevDecl)
859
    : ClassTemplateSpecializationDecl(Context,
860
                                      ClassTemplatePartialSpecialization,
861
                                      TK, DC, StartLoc, IdLoc,
862
                                      SpecializedTemplate, Args, PrevDecl),
863
      TemplateParams(Params), ArgsAsWritten(ArgInfos),
864
177k
      InstantiatedFromMember(nullptr, false) {
865
177k
  AdoptTemplateParameterList(Params, this);
866
177k
}
867
868
ClassTemplatePartialSpecializationDecl *
869
ClassTemplatePartialSpecializationDecl::
870
Create(ASTContext &Context, TagKind TK,DeclContext *DC,
871
       SourceLocation StartLoc, SourceLocation IdLoc,
872
       TemplateParameterList *Params,
873
       ClassTemplateDecl *SpecializedTemplate,
874
       ArrayRef<TemplateArgument> Args,
875
       const TemplateArgumentListInfo &ArgInfos,
876
       QualType CanonInjectedType,
877
177k
       ClassTemplatePartialSpecializationDecl *PrevDecl) {
878
177k
  const ASTTemplateArgumentListInfo *ASTArgInfos =
879
177k
    ASTTemplateArgumentListInfo::Create(Context, ArgInfos);
880
177k
881
177k
  auto *Result = new (Context, DC)
882
177k
      ClassTemplatePartialSpecializationDecl(Context, TK, DC, StartLoc, IdLoc,
883
177k
                                             Params, SpecializedTemplate, Args,
884
177k
                                             ASTArgInfos, PrevDecl);
885
177k
  Result->setSpecializationKind(TSK_ExplicitSpecialization);
886
177k
  Result->setMayHaveOutOfDateDef(false);
887
177k
888
177k
  Context.getInjectedClassNameType(Result, CanonInjectedType);
889
177k
  return Result;
890
177k
}
891
892
ClassTemplatePartialSpecializationDecl *
893
ClassTemplatePartialSpecializationDecl::CreateDeserialized(ASTContext &C,
894
151
                                                           unsigned ID) {
895
151
  auto *Result = new (C, ID) ClassTemplatePartialSpecializationDecl(C);
896
151
  Result->setMayHaveOutOfDateDef(false);
897
151
  return Result;
898
151
}
899
900
//===----------------------------------------------------------------------===//
901
// FriendTemplateDecl Implementation
902
//===----------------------------------------------------------------------===//
903
904
0
void FriendTemplateDecl::anchor() {}
905
906
FriendTemplateDecl *
907
FriendTemplateDecl::Create(ASTContext &Context, DeclContext *DC,
908
                           SourceLocation L,
909
                           MutableArrayRef<TemplateParameterList *> Params,
910
5
                           FriendUnion Friend, SourceLocation FLoc) {
911
5
  return new (Context, DC) FriendTemplateDecl(DC, L, Params, Friend, FLoc);
912
5
}
913
914
FriendTemplateDecl *FriendTemplateDecl::CreateDeserialized(ASTContext &C,
915
0
                                                           unsigned ID) {
916
0
  return new (C, ID) FriendTemplateDecl(EmptyShell());
917
0
}
918
919
//===----------------------------------------------------------------------===//
920
// TypeAliasTemplateDecl Implementation
921
//===----------------------------------------------------------------------===//
922
923
TypeAliasTemplateDecl *TypeAliasTemplateDecl::Create(ASTContext &C,
924
                                                     DeclContext *DC,
925
                                                     SourceLocation L,
926
                                                     DeclarationName Name,
927
                                                  TemplateParameterList *Params,
928
79.5k
                                                     NamedDecl *Decl) {
929
79.5k
  AdoptTemplateParameterList(Params, DC);
930
79.5k
  return new (C, DC) TypeAliasTemplateDecl(C, DC, L, Name, Params, Decl);
931
79.5k
}
932
933
TypeAliasTemplateDecl *TypeAliasTemplateDecl::CreateDeserialized(ASTContext &C,
934
77
                                                                 unsigned ID) {
935
77
  return new (C, ID) TypeAliasTemplateDecl(C, nullptr, SourceLocation(),
936
77
                                           DeclarationName(), nullptr, nullptr);
937
77
}
938
939
RedeclarableTemplateDecl::CommonBase *
940
28.5k
TypeAliasTemplateDecl::newCommon(ASTContext &C) const {
941
28.5k
  auto *CommonPtr = new (C) Common;
942
28.5k
  C.addDestruction(CommonPtr);
943
28.5k
  return CommonPtr;
944
28.5k
}
945
946
//===----------------------------------------------------------------------===//
947
// ClassScopeFunctionSpecializationDecl Implementation
948
//===----------------------------------------------------------------------===//
949
950
0
void ClassScopeFunctionSpecializationDecl::anchor() {}
951
952
ClassScopeFunctionSpecializationDecl *
953
ClassScopeFunctionSpecializationDecl::CreateDeserialized(ASTContext &C,
954
8
                                                         unsigned ID) {
955
8
  return new (C, ID) ClassScopeFunctionSpecializationDecl(
956
8
      nullptr, SourceLocation(), nullptr, nullptr);
957
8
}
958
959
//===----------------------------------------------------------------------===//
960
// VarTemplateDecl Implementation
961
//===----------------------------------------------------------------------===//
962
963
0
VarTemplateDecl *VarTemplateDecl::getDefinition() {
964
0
  VarTemplateDecl *CurD = this;
965
0
  while (CurD) {
966
0
    if (CurD->isThisDeclarationADefinition())
967
0
      return CurD;
968
0
    CurD = CurD->getPreviousDecl();
969
0
  }
970
0
  return nullptr;
971
0
}
972
973
VarTemplateDecl *VarTemplateDecl::Create(ASTContext &C, DeclContext *DC,
974
                                         SourceLocation L, DeclarationName Name,
975
                                         TemplateParameterList *Params,
976
1.46k
                                         VarDecl *Decl) {
977
1.46k
  AdoptTemplateParameterList(Params, DC);
978
1.46k
  return new (C, DC) VarTemplateDecl(C, DC, L, Name, Params, Decl);
979
1.46k
}
980
981
VarTemplateDecl *VarTemplateDecl::CreateDeserialized(ASTContext &C,
982
175
                                                     unsigned ID) {
983
175
  return new (C, ID) VarTemplateDecl(C, nullptr, SourceLocation(),
984
175
                                     DeclarationName(), nullptr, nullptr);
985
175
}
986
987
5.03k
void VarTemplateDecl::LoadLazySpecializations() const {
988
5.03k
  loadLazySpecializationsImpl();
989
5.03k
}
990
991
llvm::FoldingSetVector<VarTemplateSpecializationDecl> &
992
3.19k
VarTemplateDecl::getSpecializations() const {
993
3.19k
  LoadLazySpecializations();
994
3.19k
  return getCommonPtr()->Specializations;
995
3.19k
}
996
997
llvm::FoldingSetVector<VarTemplatePartialSpecializationDecl> &
998
1.70k
VarTemplateDecl::getPartialSpecializations() {
999
1.70k
  LoadLazySpecializations();
1000
1.70k
  return getCommonPtr()->PartialSpecializations;
1001
1.70k
}
1002
1003
RedeclarableTemplateDecl::CommonBase *
1004
1.10k
VarTemplateDecl::newCommon(ASTContext &C) const {
1005
1.10k
  auto *CommonPtr = new (C) Common;
1006
1.10k
  C.addDestruction(CommonPtr);
1007
1.10k
  return CommonPtr;
1008
1.10k
}
1009
1010
VarTemplateSpecializationDecl *
1011
VarTemplateDecl::findSpecialization(ArrayRef<TemplateArgument> Args,
1012
1.79k
                                    void *&InsertPos) {
1013
1.79k
  return findSpecializationImpl(getSpecializations(), Args, InsertPos);
1014
1.79k
}
1015
1016
void VarTemplateDecl::AddSpecialization(VarTemplateSpecializationDecl *D,
1017
1.30k
                                        void *InsertPos) {
1018
1.30k
  addSpecializationImpl<VarTemplateDecl>(getSpecializations(), D, InsertPos);
1019
1.30k
}
1020
1021
VarTemplatePartialSpecializationDecl *
1022
VarTemplateDecl::findPartialSpecialization(ArrayRef<TemplateArgument> Args,
1023
329
                                           void *&InsertPos) {
1024
329
  return findSpecializationImpl(getPartialSpecializations(), Args, InsertPos);
1025
329
}
1026
1027
void VarTemplateDecl::AddPartialSpecialization(
1028
257
    VarTemplatePartialSpecializationDecl *D, void *InsertPos) {
1029
257
  if (InsertPos)
1030
210
    getPartialSpecializations().InsertNode(D, InsertPos);
1031
47
  else {
1032
47
    VarTemplatePartialSpecializationDecl *Existing =
1033
47
        getPartialSpecializations().GetOrInsertNode(D);
1034
47
    (void)Existing;
1035
47
    assert(Existing->isCanonicalDecl() && "Non-canonical specialization?");
1036
47
  }
1037
257
1038
257
  if (ASTMutationListener *L = getASTMutationListener())
1039
24
    L->AddedCXXTemplateSpecialization(this, D);
1040
257
}
1041
1042
void VarTemplateDecl::getPartialSpecializations(
1043
1.07k
    SmallVectorImpl<VarTemplatePartialSpecializationDecl *> &PS) {
1044
1.07k
  llvm::FoldingSetVector<VarTemplatePartialSpecializationDecl> &PartialSpecs =
1045
1.07k
      getPartialSpecializations();
1046
1.07k
  PS.clear();
1047
1.07k
  PS.reserve(PartialSpecs.size());
1048
1.07k
  for (VarTemplatePartialSpecializationDecl &P : PartialSpecs)
1049
290
    PS.push_back(P.getMostRecentDecl());
1050
1.07k
}
1051
1052
VarTemplatePartialSpecializationDecl *
1053
VarTemplateDecl::findPartialSpecInstantiatedFromMember(
1054
41
    VarTemplatePartialSpecializationDecl *D) {
1055
41
  Decl *DCanon = D->getCanonicalDecl();
1056
41
  for (VarTemplatePartialSpecializationDecl &P : getPartialSpecializations()) {
1057
0
    if (P.getInstantiatedFromMember()->getCanonicalDecl() == DCanon)
1058
0
      return P.getMostRecentDecl();
1059
0
  }
1060
41
1061
41
  return nullptr;
1062
41
}
1063
1064
//===----------------------------------------------------------------------===//
1065
// VarTemplateSpecializationDecl Implementation
1066
//===----------------------------------------------------------------------===//
1067
1068
VarTemplateSpecializationDecl::VarTemplateSpecializationDecl(
1069
    Kind DK, ASTContext &Context, DeclContext *DC, SourceLocation StartLoc,
1070
    SourceLocation IdLoc, VarTemplateDecl *SpecializedTemplate, QualType T,
1071
    TypeSourceInfo *TInfo, StorageClass S, ArrayRef<TemplateArgument> Args)
1072
    : VarDecl(DK, Context, DC, StartLoc, IdLoc,
1073
              SpecializedTemplate->getIdentifier(), T, TInfo, S),
1074
      SpecializedTemplate(SpecializedTemplate),
1075
      TemplateArgs(TemplateArgumentList::CreateCopy(Context, Args)),
1076
1.80k
      SpecializationKind(TSK_Undeclared), IsCompleteDefinition(false) {}
1077
1078
VarTemplateSpecializationDecl::VarTemplateSpecializationDecl(Kind DK,
1079
                                                             ASTContext &C)
1080
    : VarDecl(DK, C, nullptr, SourceLocation(), SourceLocation(), nullptr,
1081
              QualType(), nullptr, SC_None),
1082
151
      SpecializationKind(TSK_Undeclared), IsCompleteDefinition(false) {}
1083
1084
VarTemplateSpecializationDecl *VarTemplateSpecializationDecl::Create(
1085
    ASTContext &Context, DeclContext *DC, SourceLocation StartLoc,
1086
    SourceLocation IdLoc, VarTemplateDecl *SpecializedTemplate, QualType T,
1087
1.48k
    TypeSourceInfo *TInfo, StorageClass S, ArrayRef<TemplateArgument> Args) {
1088
1.48k
  return new (Context, DC) VarTemplateSpecializationDecl(
1089
1.48k
      VarTemplateSpecialization, Context, DC, StartLoc, IdLoc,
1090
1.48k
      SpecializedTemplate, T, TInfo, S, Args);
1091
1.48k
}
1092
1093
VarTemplateSpecializationDecl *
1094
128
VarTemplateSpecializationDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1095
128
  return new (C, ID)
1096
128
      VarTemplateSpecializationDecl(VarTemplateSpecialization, C);
1097
128
}
1098
1099
void VarTemplateSpecializationDecl::getNameForDiagnostic(
1100
138
    raw_ostream &OS, const PrintingPolicy &Policy, bool Qualified) const {
1101
138
  NamedDecl::getNameForDiagnostic(OS, Policy, Qualified);
1102
138
1103
138
  const auto *PS = dyn_cast<VarTemplatePartialSpecializationDecl>(this);
1104
138
  if (const ASTTemplateArgumentListInfo *ArgsAsWritten =
1105
12
          PS ? PS->getTemplateArgsAsWritten() : nullptr) {
1106
12
    printTemplateArgumentList(OS, ArgsAsWritten->arguments(), Policy);
1107
126
  } else {
1108
126
    const TemplateArgumentList &TemplateArgs = getTemplateArgs();
1109
126
    printTemplateArgumentList(OS, TemplateArgs.asArray(), Policy);
1110
126
  }
1111
138
}
1112
1113
5.49k
VarTemplateDecl *VarTemplateSpecializationDecl::getSpecializedTemplate() const {
1114
5.49k
  if (const auto *PartialSpec =
1115
208
          SpecializedTemplate.dyn_cast<SpecializedPartialSpecialization *>())
1116
208
    return PartialSpec->PartialSpecialization->getSpecializedTemplate();
1117
5.28k
  return SpecializedTemplate.get<VarTemplateDecl *>();
1118
5.28k
}
1119
1120
void VarTemplateSpecializationDecl::setTemplateArgsInfo(
1121
1.48k
    const TemplateArgumentListInfo &ArgsInfo) {
1122
1.48k
  TemplateArgsInfo.setLAngleLoc(ArgsInfo.getLAngleLoc());
1123
1.48k
  TemplateArgsInfo.setRAngleLoc(ArgsInfo.getRAngleLoc());
1124
1.48k
  for (const TemplateArgumentLoc &Loc : ArgsInfo.arguments())
1125
1.66k
    TemplateArgsInfo.addArgument(Loc);
1126
1.48k
}
1127
1128
//===----------------------------------------------------------------------===//
1129
// VarTemplatePartialSpecializationDecl Implementation
1130
//===----------------------------------------------------------------------===//
1131
1132
0
void VarTemplatePartialSpecializationDecl::anchor() {}
1133
1134
VarTemplatePartialSpecializationDecl::VarTemplatePartialSpecializationDecl(
1135
    ASTContext &Context, DeclContext *DC, SourceLocation StartLoc,
1136
    SourceLocation IdLoc, TemplateParameterList *Params,
1137
    VarTemplateDecl *SpecializedTemplate, QualType T, TypeSourceInfo *TInfo,
1138
    StorageClass S, ArrayRef<TemplateArgument> Args,
1139
    const ASTTemplateArgumentListInfo *ArgInfos)
1140
    : VarTemplateSpecializationDecl(VarTemplatePartialSpecialization, Context,
1141
                                    DC, StartLoc, IdLoc, SpecializedTemplate, T,
1142
                                    TInfo, S, Args),
1143
      TemplateParams(Params), ArgsAsWritten(ArgInfos),
1144
321
      InstantiatedFromMember(nullptr, false) {
1145
321
  // TODO: The template parameters should be in DC by now. Verify.
1146
321
  // AdoptTemplateParameterList(Params, DC);
1147
321
}
1148
1149
VarTemplatePartialSpecializationDecl *
1150
VarTemplatePartialSpecializationDecl::Create(
1151
    ASTContext &Context, DeclContext *DC, SourceLocation StartLoc,
1152
    SourceLocation IdLoc, TemplateParameterList *Params,
1153
    VarTemplateDecl *SpecializedTemplate, QualType T, TypeSourceInfo *TInfo,
1154
    StorageClass S, ArrayRef<TemplateArgument> Args,
1155
321
    const TemplateArgumentListInfo &ArgInfos) {
1156
321
  const ASTTemplateArgumentListInfo *ASTArgInfos
1157
321
    = ASTTemplateArgumentListInfo::Create(Context, ArgInfos);
1158
321
1159
321
  auto *Result =
1160
321
      new (Context, DC) VarTemplatePartialSpecializationDecl(
1161
321
          Context, DC, StartLoc, IdLoc, Params, SpecializedTemplate, T, TInfo,
1162
321
          S, Args, ASTArgInfos);
1163
321
  Result->setSpecializationKind(TSK_ExplicitSpecialization);
1164
321
  return Result;
1165
321
}
1166
1167
VarTemplatePartialSpecializationDecl *
1168
VarTemplatePartialSpecializationDecl::CreateDeserialized(ASTContext &C,
1169
23
                                                         unsigned ID) {
1170
23
  return new (C, ID) VarTemplatePartialSpecializationDecl(C);
1171
23
}
1172
1173
static TemplateParameterList *
1174
305
createMakeIntegerSeqParameterList(const ASTContext &C, DeclContext *DC) {
1175
305
  // typename T
1176
305
  auto *T = TemplateTypeParmDecl::Create(
1177
305
      C, DC, SourceLocation(), SourceLocation(), /*Depth=*/1, /*Position=*/0,
1178
305
      /*Id=*/nullptr, /*Typename=*/true, /*ParameterPack=*/false);
1179
305
  T->setImplicit(true);
1180
305
1181
305
  // T ...Ints
1182
305
  TypeSourceInfo *TI =
1183
305
      C.getTrivialTypeSourceInfo(QualType(T->getTypeForDecl(), 0));
1184
305
  auto *N = NonTypeTemplateParmDecl::Create(
1185
305
      C, DC, SourceLocation(), SourceLocation(), /*Depth=*/0, /*Position=*/1,
1186
305
      /*Id=*/nullptr, TI->getType(), /*ParameterPack=*/true, TI);
1187
305
  N->setImplicit(true);
1188
305
1189
305
  // <typename T, T ...Ints>
1190
305
  NamedDecl *P[2] = {T, N};
1191
305
  auto *TPL = TemplateParameterList::Create(
1192
305
      C, SourceLocation(), SourceLocation(), P, SourceLocation(), nullptr);
1193
305
1194
305
  // template <typename T, ...Ints> class IntSeq
1195
305
  auto *TemplateTemplateParm = TemplateTemplateParmDecl::Create(
1196
305
      C, DC, SourceLocation(), /*Depth=*/0, /*Position=*/0,
1197
305
      /*ParameterPack=*/false, /*Id=*/nullptr, TPL);
1198
305
  TemplateTemplateParm->setImplicit(true);
1199
305
1200
305
  // typename T
1201
305
  auto *TemplateTypeParm = TemplateTypeParmDecl::Create(
1202
305
      C, DC, SourceLocation(), SourceLocation(), /*Depth=*/0, /*Position=*/1,
1203
305
      /*Id=*/nullptr, /*Typename=*/true, /*ParameterPack=*/false);
1204
305
  TemplateTypeParm->setImplicit(true);
1205
305
1206
305
  // T N
1207
305
  TypeSourceInfo *TInfo = C.getTrivialTypeSourceInfo(
1208
305
      QualType(TemplateTypeParm->getTypeForDecl(), 0));
1209
305
  auto *NonTypeTemplateParm = NonTypeTemplateParmDecl::Create(
1210
305
      C, DC, SourceLocation(), SourceLocation(), /*Depth=*/0, /*Position=*/2,
1211
305
      /*Id=*/nullptr, TInfo->getType(), /*ParameterPack=*/false, TInfo);
1212
305
  NamedDecl *Params[] = {TemplateTemplateParm, TemplateTypeParm,
1213
305
                         NonTypeTemplateParm};
1214
305
1215
305
  // template <template <typename T, T ...Ints> class IntSeq, typename T, T N>
1216
305
  return TemplateParameterList::Create(C, SourceLocation(), SourceLocation(),
1217
305
                                       Params, SourceLocation(), nullptr);
1218
305
}
1219
1220
static TemplateParameterList *
1221
303
createTypePackElementParameterList(const ASTContext &C, DeclContext *DC) {
1222
303
  // std::size_t Index
1223
303
  TypeSourceInfo *TInfo = C.getTrivialTypeSourceInfo(C.getSizeType());
1224
303
  auto *Index = NonTypeTemplateParmDecl::Create(
1225
303
      C, DC, SourceLocation(), SourceLocation(), /*Depth=*/0, /*Position=*/0,
1226
303
      /*Id=*/nullptr, TInfo->getType(), /*ParameterPack=*/false, TInfo);
1227
303
1228
303
  // typename ...T
1229
303
  auto *Ts = TemplateTypeParmDecl::Create(
1230
303
      C, DC, SourceLocation(), SourceLocation(), /*Depth=*/0, /*Position=*/1,
1231
303
      /*Id=*/nullptr, /*Typename=*/true, /*ParameterPack=*/true);
1232
303
  Ts->setImplicit(true);
1233
303
1234
303
  // template <std::size_t Index, typename ...T>
1235
303
  NamedDecl *Params[] = {Index, Ts};
1236
303
  return TemplateParameterList::Create(C, SourceLocation(), SourceLocation(),
1237
303
                                       llvm::makeArrayRef(Params),
1238
303
                                       SourceLocation(), nullptr);
1239
303
}
1240
1241
static TemplateParameterList *createBuiltinTemplateParameterList(
1242
608
    const ASTContext &C, DeclContext *DC, BuiltinTemplateKind BTK) {
1243
608
  switch (BTK) {
1244
608
  case BTK__make_integer_seq:
1245
305
    return createMakeIntegerSeqParameterList(C, DC);
1246
608
  case BTK__type_pack_element:
1247
303
    return createTypePackElementParameterList(C, DC);
1248
0
  }
1249
0
1250
0
  llvm_unreachable("unhandled BuiltinTemplateKind!");
1251
0
}
1252
1253
0
void BuiltinTemplateDecl::anchor() {}
1254
1255
BuiltinTemplateDecl::BuiltinTemplateDecl(const ASTContext &C, DeclContext *DC,
1256
                                         DeclarationName Name,
1257
                                         BuiltinTemplateKind BTK)
1258
    : TemplateDecl(BuiltinTemplate, DC, SourceLocation(), Name,
1259
                   createBuiltinTemplateParameterList(C, DC, BTK)),
1260
608
      BTK(BTK) {}