Coverage Report

Created: 2021-01-19 06:58

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