Coverage Report

Created: 2022-07-16 07:03

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