Coverage Report

Created: 2020-02-15 09:57

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