blob: 6374a92621a4dcc00dbc73832a35cf4ab3d21bf2 [file] [log] [blame]
Sebastian Redle2530ec2009-10-23 22:13:42 +00001//===--- DeclTemplate.cpp - Template Declaration AST Node Implementation --===//
Douglas Gregorded2d7b2009-02-04 19:02:06 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the C++ related Decl classes for templates.
11//
12//===----------------------------------------------------------------------===//
13
Douglas Gregorded2d7b2009-02-04 19:02:06 +000014#include "clang/AST/DeclTemplate.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000015#include "clang/AST/ASTContext.h"
16#include "clang/AST/ASTMutationListener.h"
17#include "clang/AST/DeclCXX.h"
Douglas Gregor8bf42052009-02-09 18:46:07 +000018#include "clang/AST/Expr.h"
Douglas Gregor85759112011-01-04 02:33:52 +000019#include "clang/AST/ExprCXX.h"
John McCall0ad16662009-10-29 08:12:44 +000020#include "clang/AST/TypeLoc.h"
Douglas Gregorded2d7b2009-02-04 19:02:06 +000021#include "clang/Basic/IdentifierTable.h"
22#include "llvm/ADT/STLExtras.h"
Douglas Gregor1ccc8412010-11-07 23:05:16 +000023#include <memory>
Douglas Gregorded2d7b2009-02-04 19:02:06 +000024using namespace clang;
25
26//===----------------------------------------------------------------------===//
27// TemplateParameterList Implementation
28//===----------------------------------------------------------------------===//
29
Douglas Gregorcd72ba92009-02-06 22:42:48 +000030TemplateParameterList::TemplateParameterList(SourceLocation TemplateLoc,
31 SourceLocation LAngleLoc,
Douglas Gregorbe999392009-09-15 16:23:51 +000032 NamedDecl **Params, unsigned NumParams,
Douglas Gregorcd72ba92009-02-06 22:42:48 +000033 SourceLocation RAngleLoc)
34 : TemplateLoc(TemplateLoc), LAngleLoc(LAngleLoc), RAngleLoc(RAngleLoc),
Richard Smith1fde8ec2012-09-07 02:06:42 +000035 NumParams(NumParams), ContainsUnexpandedParameterPack(false) {
36 assert(this->NumParams == NumParams && "Too many template parameters");
37 for (unsigned Idx = 0; Idx < NumParams; ++Idx) {
38 NamedDecl *P = Params[Idx];
39 begin()[Idx] = P;
40
41 if (!P->isTemplateParameterPack()) {
42 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(P))
43 if (NTTP->getType()->containsUnexpandedParameterPack())
44 ContainsUnexpandedParameterPack = true;
45
46 if (TemplateTemplateParmDecl *TTP = dyn_cast<TemplateTemplateParmDecl>(P))
47 if (TTP->getTemplateParameters()->containsUnexpandedParameterPack())
48 ContainsUnexpandedParameterPack = true;
49
50 // FIXME: If a default argument contains an unexpanded parameter pack, the
51 // template parameter list does too.
52 }
53 }
Douglas Gregorded2d7b2009-02-04 19:02:06 +000054}
55
56TemplateParameterList *
Jay Foad39c79802011-01-12 09:06:06 +000057TemplateParameterList::Create(const ASTContext &C, SourceLocation TemplateLoc,
Douglas Gregorbe999392009-09-15 16:23:51 +000058 SourceLocation LAngleLoc, NamedDecl **Params,
Douglas Gregorcd72ba92009-02-06 22:42:48 +000059 unsigned NumParams, SourceLocation RAngleLoc) {
Douglas Gregorbe999392009-09-15 16:23:51 +000060 unsigned Size = sizeof(TemplateParameterList)
61 + sizeof(NamedDecl *) * NumParams;
Richard Smith426f7852012-08-16 22:51:34 +000062 unsigned Align = std::max(llvm::alignOf<TemplateParameterList>(),
63 llvm::alignOf<NamedDecl*>());
Douglas Gregorded2d7b2009-02-04 19:02:06 +000064 void *Mem = C.Allocate(Size, Align);
Mike Stump11289f42009-09-09 15:08:12 +000065 return new (Mem) TemplateParameterList(TemplateLoc, LAngleLoc, Params,
Douglas Gregorcd72ba92009-02-06 22:42:48 +000066 NumParams, RAngleLoc);
Douglas Gregorded2d7b2009-02-04 19:02:06 +000067}
68
Douglas Gregorf8f86832009-02-11 18:16:40 +000069unsigned TemplateParameterList::getMinRequiredArguments() const {
Douglas Gregor0231d8d2011-01-19 20:10:05 +000070 unsigned NumRequiredArgs = 0;
71 for (iterator P = const_cast<TemplateParameterList *>(this)->begin(),
72 PEnd = const_cast<TemplateParameterList *>(this)->end();
73 P != PEnd; ++P) {
74 if ((*P)->isTemplateParameterPack()) {
75 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(*P))
76 if (NTTP->isExpandedParameterPack()) {
77 NumRequiredArgs += NTTP->getNumExpansionTypes();
78 continue;
79 }
80
Douglas Gregorf8f86832009-02-11 18:16:40 +000081 break;
Douglas Gregor0231d8d2011-01-19 20:10:05 +000082 }
83
84 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*P)) {
85 if (TTP->hasDefaultArgument())
86 break;
87 } else if (NonTypeTemplateParmDecl *NTTP
88 = dyn_cast<NonTypeTemplateParmDecl>(*P)) {
89 if (NTTP->hasDefaultArgument())
90 break;
91 } else if (cast<TemplateTemplateParmDecl>(*P)->hasDefaultArgument())
92 break;
93
94 ++NumRequiredArgs;
Douglas Gregorf8f86832009-02-11 18:16:40 +000095 }
Douglas Gregor0231d8d2011-01-19 20:10:05 +000096
Douglas Gregorf8f86832009-02-11 18:16:40 +000097 return NumRequiredArgs;
98}
99
Douglas Gregor21610382009-10-29 00:04:11 +0000100unsigned TemplateParameterList::getDepth() const {
101 if (size() == 0)
102 return 0;
103
104 const NamedDecl *FirstParm = getParam(0);
105 if (const TemplateTypeParmDecl *TTP
106 = dyn_cast<TemplateTypeParmDecl>(FirstParm))
107 return TTP->getDepth();
108 else if (const NonTypeTemplateParmDecl *NTTP
109 = dyn_cast<NonTypeTemplateParmDecl>(FirstParm))
110 return NTTP->getDepth();
111 else
112 return cast<TemplateTemplateParmDecl>(FirstParm)->getDepth();
113}
114
Douglas Gregor3c41bf72011-03-04 18:32:38 +0000115static void AdoptTemplateParameterList(TemplateParameterList *Params,
116 DeclContext *Owner) {
117 for (TemplateParameterList::iterator P = Params->begin(),
118 PEnd = Params->end();
119 P != PEnd; ++P) {
120 (*P)->setDeclContext(Owner);
121
122 if (TemplateTemplateParmDecl *TTP = dyn_cast<TemplateTemplateParmDecl>(*P))
123 AdoptTemplateParameterList(TTP->getTemplateParameters(), Owner);
124 }
125}
126
Douglas Gregorded2d7b2009-02-04 19:02:06 +0000127//===----------------------------------------------------------------------===//
Peter Collingbourne91b25b72010-07-29 16:11:51 +0000128// RedeclarableTemplateDecl Implementation
129//===----------------------------------------------------------------------===//
130
Dmitri Gribenkob53f37c2013-01-23 16:52:57 +0000131RedeclarableTemplateDecl::CommonBase *RedeclarableTemplateDecl::getCommonPtr() const {
Rafael Espindolacb444fe2013-10-19 02:28:17 +0000132 if (Common)
133 return Common;
134
135 // Walk the previous-declaration chain until we either find a declaration
136 // with a common pointer or we run out of previous declarations.
137 SmallVector<const RedeclarableTemplateDecl *, 2> PrevDecls;
138 for (const RedeclarableTemplateDecl *Prev = getPreviousDecl(); Prev;
139 Prev = Prev->getPreviousDecl()) {
140 if (Prev->Common) {
141 Common = Prev->Common;
142 break;
Douglas Gregor68444de2012-01-14 15:13:49 +0000143 }
Peter Collingbourne91b25b72010-07-29 16:11:51 +0000144
Rafael Espindolacb444fe2013-10-19 02:28:17 +0000145 PrevDecls.push_back(Prev);
Peter Collingbourne91b25b72010-07-29 16:11:51 +0000146 }
Peter Collingbourne91b25b72010-07-29 16:11:51 +0000147
Rafael Espindolacb444fe2013-10-19 02:28:17 +0000148 // If we never found a common pointer, allocate one now.
149 if (!Common) {
150 // FIXME: If any of the declarations is from an AST file, we probably
151 // need an update record to add the common data.
152
153 Common = newCommon(getASTContext());
154 }
155
156 // Update any previous declarations we saw with the common pointer.
157 for (unsigned I = 0, N = PrevDecls.size(); I != N; ++I)
158 PrevDecls[I]->Common = Common;
159
Douglas Gregor68444de2012-01-14 15:13:49 +0000160 return Common;
Peter Collingbourne029fd692010-07-29 16:12:09 +0000161}
162
Richard Smithe977e512015-02-24 01:23:23 +0000163template<class EntryType>
164typename RedeclarableTemplateDecl::SpecEntryTraits<EntryType>::DeclType *
Peter Collingbourneb498ed62010-07-30 17:09:04 +0000165RedeclarableTemplateDecl::findSpecializationImpl(
Richard Smithe977e512015-02-24 01:23:23 +0000166 llvm::FoldingSetVector<EntryType> &Specs, ArrayRef<TemplateArgument> Args,
167 void *&InsertPos) {
Peter Collingbourneb498ed62010-07-30 17:09:04 +0000168 typedef SpecEntryTraits<EntryType> SETraits;
169 llvm::FoldingSetNodeID ID;
Craig Topper7e0daca2014-06-26 04:58:53 +0000170 EntryType::Profile(ID,Args, getASTContext());
Peter Collingbourneb498ed62010-07-30 17:09:04 +0000171 EntryType *Entry = Specs.FindNodeOrInsertPos(ID, InsertPos);
Richard Smithe977e512015-02-24 01:23:23 +0000172 return Entry ? SETraits::getDecl(Entry)->getMostRecentDecl() : nullptr;
173}
174
175template<class Derived, class EntryType>
176void RedeclarableTemplateDecl::addSpecializationImpl(
177 llvm::FoldingSetVector<EntryType> &Specializations, EntryType *Entry,
178 void *InsertPos) {
179 typedef SpecEntryTraits<EntryType> SETraits;
180 if (InsertPos) {
181#ifndef NDEBUG
182 void *CorrectInsertPos;
183 assert(!findSpecializationImpl(Specializations,
184 SETraits::getTemplateArgs(Entry),
185 CorrectInsertPos) &&
186 InsertPos == CorrectInsertPos &&
187 "given incorrect InsertPos for specialization");
188#endif
189 Specializations.InsertNode(Entry, InsertPos);
190 } else {
191 EntryType *Existing = Specializations.GetOrInsertNode(Entry);
192 (void)Existing;
193 assert(SETraits::getDecl(Existing)->isCanonicalDecl() &&
194 "non-canonical specialization?");
195 }
196
197 if (ASTMutationListener *L = getASTMutationListener())
198 L->AddedCXXTemplateSpecialization(cast<Derived>(this),
199 SETraits::getDecl(Entry));
Peter Collingbourneb498ed62010-07-30 17:09:04 +0000200}
201
Douglas Gregor43669f82011-03-05 17:54:25 +0000202/// \brief Generate the injected template arguments for the given template
203/// parameter list, e.g., for the injected-class-name of a class template.
204static void GenerateInjectedTemplateArgs(ASTContext &Context,
205 TemplateParameterList *Params,
206 TemplateArgument *Args) {
207 for (TemplateParameterList::iterator Param = Params->begin(),
208 ParamEnd = Params->end();
209 Param != ParamEnd; ++Param) {
210 TemplateArgument Arg;
211 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*Param)) {
212 QualType ArgType = Context.getTypeDeclType(TTP);
213 if (TTP->isParameterPack())
David Blaikie7a30dc52013-02-21 01:47:18 +0000214 ArgType = Context.getPackExpansionType(ArgType, None);
David Blaikie05785d12013-02-20 22:23:23 +0000215
Douglas Gregor43669f82011-03-05 17:54:25 +0000216 Arg = TemplateArgument(ArgType);
217 } else if (NonTypeTemplateParmDecl *NTTP =
218 dyn_cast<NonTypeTemplateParmDecl>(*Param)) {
John McCall113bee02012-03-10 09:33:50 +0000219 Expr *E = new (Context) DeclRefExpr(NTTP, /*enclosing*/ false,
Douglas Gregor43669f82011-03-05 17:54:25 +0000220 NTTP->getType().getNonLValueExprType(Context),
221 Expr::getValueKindForType(NTTP->getType()),
222 NTTP->getLocation());
223
224 if (NTTP->isParameterPack())
David Blaikie7a30dc52013-02-21 01:47:18 +0000225 E = new (Context) PackExpansionExpr(Context.DependentTy, E,
226 NTTP->getLocation(), None);
Douglas Gregor43669f82011-03-05 17:54:25 +0000227 Arg = TemplateArgument(E);
228 } else {
229 TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(*Param);
230 if (TTP->isParameterPack())
David Blaikie05785d12013-02-20 22:23:23 +0000231 Arg = TemplateArgument(TemplateName(TTP), Optional<unsigned>());
Douglas Gregor43669f82011-03-05 17:54:25 +0000232 else
233 Arg = TemplateArgument(TemplateName(TTP));
234 }
235
236 if ((*Param)->isTemplateParameterPack())
237 Arg = TemplateArgument::CreatePackCopy(Context, &Arg, 1);
238
239 *Args++ = Arg;
240 }
241}
242
Peter Collingbourne91b25b72010-07-29 16:11:51 +0000243//===----------------------------------------------------------------------===//
Douglas Gregorded2d7b2009-02-04 19:02:06 +0000244// FunctionTemplateDecl Implementation
245//===----------------------------------------------------------------------===//
246
Douglas Gregor1a809332010-05-23 18:26:36 +0000247void FunctionTemplateDecl::DeallocateCommon(void *Ptr) {
248 static_cast<Common *>(Ptr)->~Common();
249}
250
Douglas Gregorded2d7b2009-02-04 19:02:06 +0000251FunctionTemplateDecl *FunctionTemplateDecl::Create(ASTContext &C,
252 DeclContext *DC,
253 SourceLocation L,
254 DeclarationName Name,
Douglas Gregor8f5d4422009-06-29 20:59:39 +0000255 TemplateParameterList *Params,
Douglas Gregorded2d7b2009-02-04 19:02:06 +0000256 NamedDecl *Decl) {
Douglas Gregor3c41bf72011-03-04 18:32:38 +0000257 AdoptTemplateParameterList(Params, cast<DeclContext>(Decl));
Richard Smith053f6c62014-05-16 23:01:30 +0000258 return new (C, DC) FunctionTemplateDecl(C, DC, L, Name, Params, Decl);
Douglas Gregorded2d7b2009-02-04 19:02:06 +0000259}
260
Douglas Gregor72172e92012-01-05 21:55:30 +0000261FunctionTemplateDecl *FunctionTemplateDecl::CreateDeserialized(ASTContext &C,
262 unsigned ID) {
Richard Smith053f6c62014-05-16 23:01:30 +0000263 return new (C, ID) FunctionTemplateDecl(C, nullptr, SourceLocation(),
Craig Topper36250ad2014-05-12 05:36:57 +0000264 DeclarationName(), nullptr, nullptr);
Douglas Gregorfd7c2252011-03-04 17:52:15 +0000265}
266
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +0000267RedeclarableTemplateDecl::CommonBase *
Dmitri Gribenkob53f37c2013-01-23 16:52:57 +0000268FunctionTemplateDecl::newCommon(ASTContext &C) const {
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +0000269 Common *CommonPtr = new (C) Common;
270 C.AddDeallocation(DeallocateCommon, CommonPtr);
Peter Collingbourne91b25b72010-07-29 16:11:51 +0000271 return CommonPtr;
272}
273
Richard Smithfeb3e1a2013-06-28 04:37:53 +0000274void FunctionTemplateDecl::LoadLazySpecializations() const {
Richard Smithe3536dd2015-02-24 02:44:23 +0000275 // Grab the most recent declaration to ensure we've loaded any lazy
276 // redeclarations of this template.
277 //
278 // FIXME: Avoid walking the entire redeclaration chain here.
279 Common *CommonPtr = getMostRecentDecl()->getCommonPtr();
Richard Smithfeb3e1a2013-06-28 04:37:53 +0000280 if (CommonPtr->LazySpecializations) {
281 ASTContext &Context = getASTContext();
282 uint32_t *Specs = CommonPtr->LazySpecializations;
Craig Topper36250ad2014-05-12 05:36:57 +0000283 CommonPtr->LazySpecializations = nullptr;
Richard Smithfeb3e1a2013-06-28 04:37:53 +0000284 for (uint32_t I = 0, N = *Specs++; I != N; ++I)
285 (void)Context.getExternalSource()->GetExternalDecl(Specs[I]);
286 }
287}
288
289llvm::FoldingSetVector<FunctionTemplateSpecializationInfo> &
290FunctionTemplateDecl::getSpecializations() const {
291 LoadLazySpecializations();
292 return getCommonPtr()->Specializations;
293}
294
Argyrios Kyrtzidisdde57902010-07-20 13:59:58 +0000295FunctionDecl *
Craig Topper7e0daca2014-06-26 04:58:53 +0000296FunctionTemplateDecl::findSpecialization(ArrayRef<TemplateArgument> Args,
297 void *&InsertPos) {
298 return findSpecializationImpl(getSpecializations(), Args, InsertPos);
Argyrios Kyrtzidisdde57902010-07-20 13:59:58 +0000299}
300
Sebastian Redl9ab988f2011-04-14 14:07:59 +0000301void FunctionTemplateDecl::addSpecialization(
302 FunctionTemplateSpecializationInfo *Info, void *InsertPos) {
Richard Smithe977e512015-02-24 01:23:23 +0000303 addSpecializationImpl<FunctionTemplateDecl>(getSpecializations(), Info,
304 InsertPos);
Sebastian Redl9ab988f2011-04-14 14:07:59 +0000305}
306
Richard Smith841d8b22013-05-17 03:04:50 +0000307ArrayRef<TemplateArgument> FunctionTemplateDecl::getInjectedTemplateArgs() {
Douglas Gregor43669f82011-03-05 17:54:25 +0000308 TemplateParameterList *Params = getTemplateParameters();
309 Common *CommonPtr = getCommonPtr();
310 if (!CommonPtr->InjectedArgs) {
311 CommonPtr->InjectedArgs
Richard Smith841d8b22013-05-17 03:04:50 +0000312 = new (getASTContext()) TemplateArgument[Params->size()];
313 GenerateInjectedTemplateArgs(getASTContext(), Params,
Douglas Gregor43669f82011-03-05 17:54:25 +0000314 CommonPtr->InjectedArgs);
315 }
Richard Smith841d8b22013-05-17 03:04:50 +0000316
317 return llvm::makeArrayRef(CommonPtr->InjectedArgs, Params->size());
Douglas Gregor43669f82011-03-05 17:54:25 +0000318}
319
Douglas Gregorded2d7b2009-02-04 19:02:06 +0000320//===----------------------------------------------------------------------===//
321// ClassTemplateDecl Implementation
322//===----------------------------------------------------------------------===//
323
Douglas Gregor1a809332010-05-23 18:26:36 +0000324void ClassTemplateDecl::DeallocateCommon(void *Ptr) {
325 static_cast<Common *>(Ptr)->~Common();
326}
327
Douglas Gregorded2d7b2009-02-04 19:02:06 +0000328ClassTemplateDecl *ClassTemplateDecl::Create(ASTContext &C,
329 DeclContext *DC,
330 SourceLocation L,
331 DeclarationName Name,
332 TemplateParameterList *Params,
Douglas Gregor90a1a652009-03-19 17:26:29 +0000333 NamedDecl *Decl,
334 ClassTemplateDecl *PrevDecl) {
Douglas Gregor3c41bf72011-03-04 18:32:38 +0000335 AdoptTemplateParameterList(Params, cast<DeclContext>(Decl));
Richard Smith053f6c62014-05-16 23:01:30 +0000336 ClassTemplateDecl *New = new (C, DC) ClassTemplateDecl(C, DC, L, Name,
337 Params, Decl);
Rafael Espindola8db352d2013-10-17 15:37:26 +0000338 New->setPreviousDecl(PrevDecl);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +0000339 return New;
Douglas Gregor90a1a652009-03-19 17:26:29 +0000340}
341
Richard Smith053f6c62014-05-16 23:01:30 +0000342ClassTemplateDecl *ClassTemplateDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +0000343 unsigned ID) {
Richard Smith053f6c62014-05-16 23:01:30 +0000344 return new (C, ID) ClassTemplateDecl(C, nullptr, SourceLocation(),
345 DeclarationName(), nullptr, nullptr);
Douglas Gregorfd7c2252011-03-04 17:52:15 +0000346}
347
Dmitri Gribenko81f25752013-02-14 13:20:36 +0000348void ClassTemplateDecl::LoadLazySpecializations() const {
Richard Smithe3536dd2015-02-24 02:44:23 +0000349 // Grab the most recent declaration to ensure we've loaded any lazy
350 // redeclarations of this template.
351 //
352 // FIXME: Avoid walking the entire redeclaration chain here.
353 Common *CommonPtr = getMostRecentDecl()->getCommonPtr();
Douglas Gregor7e8c4e02010-10-27 22:21:36 +0000354 if (CommonPtr->LazySpecializations) {
355 ASTContext &Context = getASTContext();
356 uint32_t *Specs = CommonPtr->LazySpecializations;
Craig Topper36250ad2014-05-12 05:36:57 +0000357 CommonPtr->LazySpecializations = nullptr;
Douglas Gregor7e8c4e02010-10-27 22:21:36 +0000358 for (uint32_t I = 0, N = *Specs++; I != N; ++I)
359 (void)Context.getExternalSource()->GetExternalDecl(Specs[I]);
360 }
361}
362
Chandler Carruthb41171b2012-05-03 23:49:05 +0000363llvm::FoldingSetVector<ClassTemplateSpecializationDecl> &
Dmitri Gribenko81f25752013-02-14 13:20:36 +0000364ClassTemplateDecl::getSpecializations() const {
Douglas Gregor7e8c4e02010-10-27 22:21:36 +0000365 LoadLazySpecializations();
366 return getCommonPtr()->Specializations;
367}
368
Chandler Carruthb41171b2012-05-03 23:49:05 +0000369llvm::FoldingSetVector<ClassTemplatePartialSpecializationDecl> &
Douglas Gregor7e8c4e02010-10-27 22:21:36 +0000370ClassTemplateDecl::getPartialSpecializations() {
371 LoadLazySpecializations();
372 return getCommonPtr()->PartialSpecializations;
373}
374
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +0000375RedeclarableTemplateDecl::CommonBase *
Dmitri Gribenkob53f37c2013-01-23 16:52:57 +0000376ClassTemplateDecl::newCommon(ASTContext &C) const {
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +0000377 Common *CommonPtr = new (C) Common;
378 C.AddDeallocation(DeallocateCommon, CommonPtr);
Peter Collingbourne91b25b72010-07-29 16:11:51 +0000379 return CommonPtr;
380}
381
Argyrios Kyrtzidis47470f22010-07-20 13:59:28 +0000382ClassTemplateSpecializationDecl *
Craig Topper7e0daca2014-06-26 04:58:53 +0000383ClassTemplateDecl::findSpecialization(ArrayRef<TemplateArgument> Args,
384 void *&InsertPos) {
385 return findSpecializationImpl(getSpecializations(), Args, InsertPos);
Argyrios Kyrtzidis47470f22010-07-20 13:59:28 +0000386}
387
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +0000388void ClassTemplateDecl::AddSpecialization(ClassTemplateSpecializationDecl *D,
389 void *InsertPos) {
Richard Smithe977e512015-02-24 01:23:23 +0000390 addSpecializationImpl<ClassTemplateDecl>(getSpecializations(), D, InsertPos);
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +0000391}
392
Argyrios Kyrtzidis47470f22010-07-20 13:59:28 +0000393ClassTemplatePartialSpecializationDecl *
Craig Topper7e0daca2014-06-26 04:58:53 +0000394ClassTemplateDecl::findPartialSpecialization(ArrayRef<TemplateArgument> Args,
Argyrios Kyrtzidis47470f22010-07-20 13:59:28 +0000395 void *&InsertPos) {
Craig Topper7e0daca2014-06-26 04:58:53 +0000396 return findSpecializationImpl(getPartialSpecializations(), Args, InsertPos);
Argyrios Kyrtzidis47470f22010-07-20 13:59:28 +0000397}
398
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +0000399void ClassTemplateDecl::AddPartialSpecialization(
400 ClassTemplatePartialSpecializationDecl *D,
401 void *InsertPos) {
Douglas Gregorce9978f2012-03-28 14:34:23 +0000402 if (InsertPos)
403 getPartialSpecializations().InsertNode(D, InsertPos);
404 else {
405 ClassTemplatePartialSpecializationDecl *Existing
406 = getPartialSpecializations().GetOrInsertNode(D);
407 (void)Existing;
408 assert(Existing->isCanonicalDecl() && "Non-canonical specialization?");
409 }
410
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +0000411 if (ASTMutationListener *L = getASTMutationListener())
412 L->AddedCXXTemplateSpecialization(this, D);
413}
414
Douglas Gregor407e9612010-04-30 05:56:50 +0000415void ClassTemplateDecl::getPartialSpecializations(
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000416 SmallVectorImpl<ClassTemplatePartialSpecializationDecl *> &PS) {
Chandler Carruthb41171b2012-05-03 23:49:05 +0000417 llvm::FoldingSetVector<ClassTemplatePartialSpecializationDecl> &PartialSpecs
Argyrios Kyrtzidisa35c8e42010-06-21 10:57:41 +0000418 = getPartialSpecializations();
Douglas Gregor407e9612010-04-30 05:56:50 +0000419 PS.clear();
Richard Smithb2f61b42013-08-22 23:27:37 +0000420 PS.reserve(PartialSpecs.size());
Chandler Carruthb41171b2012-05-03 23:49:05 +0000421 for (llvm::FoldingSetVector<ClassTemplatePartialSpecializationDecl>::iterator
Douglas Gregor407e9612010-04-30 05:56:50 +0000422 P = PartialSpecs.begin(), PEnd = PartialSpecs.end();
Richard Smithb2f61b42013-08-22 23:27:37 +0000423 P != PEnd; ++P)
424 PS.push_back(P->getMostRecentDecl());
Douglas Gregor407e9612010-04-30 05:56:50 +0000425}
426
Douglas Gregor15301382009-07-30 17:40:51 +0000427ClassTemplatePartialSpecializationDecl *
428ClassTemplateDecl::findPartialSpecialization(QualType T) {
429 ASTContext &Context = getASTContext();
Chandler Carruthb41171b2012-05-03 23:49:05 +0000430 using llvm::FoldingSetVector;
431 typedef FoldingSetVector<ClassTemplatePartialSpecializationDecl>::iterator
Douglas Gregor15301382009-07-30 17:40:51 +0000432 partial_spec_iterator;
433 for (partial_spec_iterator P = getPartialSpecializations().begin(),
434 PEnd = getPartialSpecializations().end();
435 P != PEnd; ++P) {
John McCall2408e322010-04-27 00:57:59 +0000436 if (Context.hasSameType(P->getInjectedSpecializationType(), T))
Douglas Gregorec9fd132012-01-14 16:38:05 +0000437 return P->getMostRecentDecl();
Argyrios Kyrtzidis47470f22010-07-20 13:59:28 +0000438 }
439
Craig Topper36250ad2014-05-12 05:36:57 +0000440 return nullptr;
Argyrios Kyrtzidis47470f22010-07-20 13:59:28 +0000441}
442
443ClassTemplatePartialSpecializationDecl *
444ClassTemplateDecl::findPartialSpecInstantiatedFromMember(
445 ClassTemplatePartialSpecializationDecl *D) {
446 Decl *DCanon = D->getCanonicalDecl();
Chandler Carruthb41171b2012-05-03 23:49:05 +0000447 for (llvm::FoldingSetVector<ClassTemplatePartialSpecializationDecl>::iterator
Argyrios Kyrtzidis47470f22010-07-20 13:59:28 +0000448 P = getPartialSpecializations().begin(),
449 PEnd = getPartialSpecializations().end();
450 P != PEnd; ++P) {
451 if (P->getInstantiatedFromMember()->getCanonicalDecl() == DCanon)
Douglas Gregorec9fd132012-01-14 16:38:05 +0000452 return P->getMostRecentDecl();
Douglas Gregor15301382009-07-30 17:40:51 +0000453 }
Mike Stump11289f42009-09-09 15:08:12 +0000454
Craig Topper36250ad2014-05-12 05:36:57 +0000455 return nullptr;
Douglas Gregor15301382009-07-30 17:40:51 +0000456}
457
John McCalle78aac42010-03-10 03:28:59 +0000458QualType
Douglas Gregor9961ce92010-07-08 18:37:38 +0000459ClassTemplateDecl::getInjectedClassNameSpecialization() {
Argyrios Kyrtzidisa35c8e42010-06-21 10:57:41 +0000460 Common *CommonPtr = getCommonPtr();
Douglas Gregore362cea2009-05-10 22:57:19 +0000461 if (!CommonPtr->InjectedClassNameType.isNull())
462 return CommonPtr->InjectedClassNameType;
463
Douglas Gregor8092e802010-12-23 16:00:30 +0000464 // C++0x [temp.dep.type]p2:
465 // The template argument list of a primary template is a template argument
466 // list in which the nth template argument has the value of the nth template
467 // parameter of the class template. If the nth template parameter is a
468 // template parameter pack (14.5.3), the nth template argument is a pack
469 // expansion (14.5.3) whose pattern is the name of the template parameter
470 // pack.
Douglas Gregor9961ce92010-07-08 18:37:38 +0000471 ASTContext &Context = getASTContext();
Douglas Gregore362cea2009-05-10 22:57:19 +0000472 TemplateParameterList *Params = getTemplateParameters();
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000473 SmallVector<TemplateArgument, 16> TemplateArgs;
Douglas Gregor43669f82011-03-05 17:54:25 +0000474 TemplateArgs.resize(Params->size());
475 GenerateInjectedTemplateArgs(getASTContext(), Params, TemplateArgs.data());
Douglas Gregore362cea2009-05-10 22:57:19 +0000476 CommonPtr->InjectedClassNameType
Douglas Gregora8e02e72009-07-28 23:00:59 +0000477 = Context.getTemplateSpecializationType(TemplateName(this),
Douglas Gregore362cea2009-05-10 22:57:19 +0000478 &TemplateArgs[0],
Douglas Gregora8e02e72009-07-28 23:00:59 +0000479 TemplateArgs.size());
Douglas Gregore362cea2009-05-10 22:57:19 +0000480 return CommonPtr->InjectedClassNameType;
481}
482
Douglas Gregorded2d7b2009-02-04 19:02:06 +0000483//===----------------------------------------------------------------------===//
484// TemplateTypeParm Allocation/Deallocation Method Implementations
485//===----------------------------------------------------------------------===//
486
487TemplateTypeParmDecl *
Jay Foad39c79802011-01-12 09:06:06 +0000488TemplateTypeParmDecl::Create(const ASTContext &C, DeclContext *DC,
Abramo Bagnarab3185b02011-03-06 15:48:19 +0000489 SourceLocation KeyLoc, SourceLocation NameLoc,
490 unsigned D, unsigned P, IdentifierInfo *Id,
491 bool Typename, bool ParameterPack) {
Chandler Carruth08836322011-05-01 00:51:33 +0000492 TemplateTypeParmDecl *TTPDecl =
Richard Smithf7981722013-11-22 09:01:48 +0000493 new (C, DC) TemplateTypeParmDecl(DC, KeyLoc, NameLoc, Id, Typename);
Chandler Carruth08836322011-05-01 00:51:33 +0000494 QualType TTPType = C.getTemplateTypeParmType(D, P, ParameterPack, TTPDecl);
Richard Smith5b21db82014-04-23 18:20:42 +0000495 TTPDecl->setTypeForDecl(TTPType.getTypePtr());
Chandler Carruth08836322011-05-01 00:51:33 +0000496 return TTPDecl;
Douglas Gregorded2d7b2009-02-04 19:02:06 +0000497}
498
Argyrios Kyrtzidis39f0e302010-07-02 11:54:55 +0000499TemplateTypeParmDecl *
Douglas Gregor72172e92012-01-05 21:55:30 +0000500TemplateTypeParmDecl::CreateDeserialized(const ASTContext &C, unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +0000501 return new (C, ID) TemplateTypeParmDecl(nullptr, SourceLocation(),
502 SourceLocation(), nullptr, false);
Argyrios Kyrtzidis39f0e302010-07-02 11:54:55 +0000503}
504
John McCall0ad16662009-10-29 08:12:44 +0000505SourceLocation TemplateTypeParmDecl::getDefaultArgumentLoc() const {
Abramo Bagnara23485e02011-03-04 12:42:03 +0000506 return hasDefaultArgument()
507 ? DefaultArgument->getTypeLoc().getBeginLoc()
508 : SourceLocation();
509}
510
511SourceRange TemplateTypeParmDecl::getSourceRange() const {
512 if (hasDefaultArgument() && !defaultArgumentWasInherited())
Abramo Bagnarab3185b02011-03-06 15:48:19 +0000513 return SourceRange(getLocStart(),
Abramo Bagnara23485e02011-03-04 12:42:03 +0000514 DefaultArgument->getTypeLoc().getEndLoc());
515 else
Abramo Bagnarab3185b02011-03-06 15:48:19 +0000516 return TypeDecl::getSourceRange();
John McCall0ad16662009-10-29 08:12:44 +0000517}
518
Douglas Gregor21610382009-10-29 00:04:11 +0000519unsigned TemplateTypeParmDecl::getDepth() const {
Richard Smith5b21db82014-04-23 18:20:42 +0000520 return getTypeForDecl()->getAs<TemplateTypeParmType>()->getDepth();
Douglas Gregor21610382009-10-29 00:04:11 +0000521}
522
523unsigned TemplateTypeParmDecl::getIndex() const {
Richard Smith5b21db82014-04-23 18:20:42 +0000524 return getTypeForDecl()->getAs<TemplateTypeParmType>()->getIndex();
Douglas Gregor21610382009-10-29 00:04:11 +0000525}
526
Chandler Carruth08836322011-05-01 00:51:33 +0000527bool TemplateTypeParmDecl::isParameterPack() const {
Richard Smith5b21db82014-04-23 18:20:42 +0000528 return getTypeForDecl()->getAs<TemplateTypeParmType>()->isParameterPack();
Chandler Carruth08836322011-05-01 00:51:33 +0000529}
530
Douglas Gregorded2d7b2009-02-04 19:02:06 +0000531//===----------------------------------------------------------------------===//
532// NonTypeTemplateParmDecl Method Implementations
533//===----------------------------------------------------------------------===//
534
Douglas Gregor0231d8d2011-01-19 20:10:05 +0000535NonTypeTemplateParmDecl::NonTypeTemplateParmDecl(DeclContext *DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +0000536 SourceLocation StartLoc,
537 SourceLocation IdLoc,
538 unsigned D, unsigned P,
539 IdentifierInfo *Id,
Douglas Gregor0231d8d2011-01-19 20:10:05 +0000540 QualType T,
541 TypeSourceInfo *TInfo,
542 const QualType *ExpandedTypes,
543 unsigned NumExpandedTypes,
544 TypeSourceInfo **ExpandedTInfos)
Abramo Bagnaradff19302011-03-08 08:55:46 +0000545 : DeclaratorDecl(NonTypeTemplateParm, DC, IdLoc, Id, T, TInfo, StartLoc),
Craig Topper36250ad2014-05-12 05:36:57 +0000546 TemplateParmPosition(D, P), DefaultArgumentAndInherited(nullptr, false),
Douglas Gregor0231d8d2011-01-19 20:10:05 +0000547 ParameterPack(true), ExpandedParameterPack(true),
548 NumExpandedTypes(NumExpandedTypes)
549{
550 if (ExpandedTypes && ExpandedTInfos) {
551 void **TypesAndInfos = reinterpret_cast<void **>(this + 1);
552 for (unsigned I = 0; I != NumExpandedTypes; ++I) {
553 TypesAndInfos[2*I] = ExpandedTypes[I].getAsOpaquePtr();
554 TypesAndInfos[2*I + 1] = ExpandedTInfos[I];
555 }
556 }
557}
558
Douglas Gregorded2d7b2009-02-04 19:02:06 +0000559NonTypeTemplateParmDecl *
Jay Foad39c79802011-01-12 09:06:06 +0000560NonTypeTemplateParmDecl::Create(const ASTContext &C, DeclContext *DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +0000561 SourceLocation StartLoc, SourceLocation IdLoc,
562 unsigned D, unsigned P, IdentifierInfo *Id,
563 QualType T, bool ParameterPack,
564 TypeSourceInfo *TInfo) {
Richard Smithf7981722013-11-22 09:01:48 +0000565 return new (C, DC) NonTypeTemplateParmDecl(DC, StartLoc, IdLoc, D, P, Id,
566 T, ParameterPack, TInfo);
Douglas Gregorded2d7b2009-02-04 19:02:06 +0000567}
568
Douglas Gregor0231d8d2011-01-19 20:10:05 +0000569NonTypeTemplateParmDecl *
570NonTypeTemplateParmDecl::Create(const ASTContext &C, DeclContext *DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +0000571 SourceLocation StartLoc, SourceLocation IdLoc,
572 unsigned D, unsigned P,
Douglas Gregor0231d8d2011-01-19 20:10:05 +0000573 IdentifierInfo *Id, QualType T,
574 TypeSourceInfo *TInfo,
575 const QualType *ExpandedTypes,
576 unsigned NumExpandedTypes,
577 TypeSourceInfo **ExpandedTInfos) {
Richard Smithf7981722013-11-22 09:01:48 +0000578 unsigned Extra = NumExpandedTypes * 2 * sizeof(void*);
579 return new (C, DC, Extra) NonTypeTemplateParmDecl(
580 DC, StartLoc, IdLoc, D, P, Id, T, TInfo,
581 ExpandedTypes, NumExpandedTypes, ExpandedTInfos);
Douglas Gregor0231d8d2011-01-19 20:10:05 +0000582}
583
Douglas Gregor72172e92012-01-05 21:55:30 +0000584NonTypeTemplateParmDecl *
585NonTypeTemplateParmDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +0000586 return new (C, ID) NonTypeTemplateParmDecl(nullptr, SourceLocation(),
587 SourceLocation(), 0, 0, nullptr,
588 QualType(), false, nullptr);
Douglas Gregor72172e92012-01-05 21:55:30 +0000589}
590
591NonTypeTemplateParmDecl *
592NonTypeTemplateParmDecl::CreateDeserialized(ASTContext &C, unsigned ID,
593 unsigned NumExpandedTypes) {
Richard Smithf7981722013-11-22 09:01:48 +0000594 unsigned Extra = NumExpandedTypes * 2 * sizeof(void*);
595 return new (C, ID, Extra) NonTypeTemplateParmDecl(
Craig Topper36250ad2014-05-12 05:36:57 +0000596 nullptr, SourceLocation(), SourceLocation(), 0, 0, nullptr, QualType(),
597 nullptr, nullptr, NumExpandedTypes, nullptr);
Douglas Gregor72172e92012-01-05 21:55:30 +0000598}
599
John McCallf4cd4f92011-02-09 01:13:10 +0000600SourceRange NonTypeTemplateParmDecl::getSourceRange() const {
Abramo Bagnarae15d5532011-03-04 11:03:48 +0000601 if (hasDefaultArgument() && !defaultArgumentWasInherited())
Abramo Bagnaraea947882011-03-08 16:41:52 +0000602 return SourceRange(getOuterLocStart(),
603 getDefaultArgument()->getSourceRange().getEnd());
604 return DeclaratorDecl::getSourceRange();
John McCallf4cd4f92011-02-09 01:13:10 +0000605}
606
Douglas Gregordba32632009-02-10 19:49:53 +0000607SourceLocation NonTypeTemplateParmDecl::getDefaultArgumentLoc() const {
Abramo Bagnara656e3002010-06-09 09:26:05 +0000608 return hasDefaultArgument()
609 ? getDefaultArgument()->getSourceRange().getBegin()
610 : SourceLocation();
Douglas Gregordba32632009-02-10 19:49:53 +0000611}
612
Douglas Gregorded2d7b2009-02-04 19:02:06 +0000613//===----------------------------------------------------------------------===//
614// TemplateTemplateParmDecl Method Implementations
615//===----------------------------------------------------------------------===//
616
David Blaikie68e081d2011-12-20 02:48:34 +0000617void TemplateTemplateParmDecl::anchor() { }
618
Richard Smith1fde8ec2012-09-07 02:06:42 +0000619TemplateTemplateParmDecl::TemplateTemplateParmDecl(
620 DeclContext *DC, SourceLocation L, unsigned D, unsigned P,
621 IdentifierInfo *Id, TemplateParameterList *Params,
622 unsigned NumExpansions, TemplateParameterList * const *Expansions)
623 : TemplateDecl(TemplateTemplateParm, DC, L, Id, Params),
624 TemplateParmPosition(D, P), DefaultArgument(),
625 DefaultArgumentWasInherited(false), ParameterPack(true),
626 ExpandedParameterPack(true), NumExpandedParams(NumExpansions) {
627 if (Expansions)
628 std::memcpy(reinterpret_cast<void*>(this + 1), Expansions,
629 sizeof(TemplateParameterList*) * NumExpandedParams);
630}
631
Douglas Gregorded2d7b2009-02-04 19:02:06 +0000632TemplateTemplateParmDecl *
Jay Foad39c79802011-01-12 09:06:06 +0000633TemplateTemplateParmDecl::Create(const ASTContext &C, DeclContext *DC,
Douglas Gregorded2d7b2009-02-04 19:02:06 +0000634 SourceLocation L, unsigned D, unsigned P,
Douglas Gregorf5500772011-01-05 15:48:55 +0000635 bool ParameterPack, IdentifierInfo *Id,
Douglas Gregorded2d7b2009-02-04 19:02:06 +0000636 TemplateParameterList *Params) {
Richard Smithf7981722013-11-22 09:01:48 +0000637 return new (C, DC) TemplateTemplateParmDecl(DC, L, D, P, ParameterPack, Id,
638 Params);
Douglas Gregorded2d7b2009-02-04 19:02:06 +0000639}
640
Douglas Gregor72172e92012-01-05 21:55:30 +0000641TemplateTemplateParmDecl *
Richard Smith1fde8ec2012-09-07 02:06:42 +0000642TemplateTemplateParmDecl::Create(const ASTContext &C, DeclContext *DC,
643 SourceLocation L, unsigned D, unsigned P,
644 IdentifierInfo *Id,
645 TemplateParameterList *Params,
Dmitri Gribenkof8579502013-01-12 19:30:44 +0000646 ArrayRef<TemplateParameterList *> Expansions) {
Richard Smithf7981722013-11-22 09:01:48 +0000647 return new (C, DC, sizeof(TemplateParameterList*) * Expansions.size())
648 TemplateTemplateParmDecl(DC, L, D, P, Id, Params,
649 Expansions.size(), Expansions.data());
Richard Smith1fde8ec2012-09-07 02:06:42 +0000650}
651
652TemplateTemplateParmDecl *
Douglas Gregor72172e92012-01-05 21:55:30 +0000653TemplateTemplateParmDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +0000654 return new (C, ID) TemplateTemplateParmDecl(nullptr, SourceLocation(), 0, 0,
655 false, nullptr, nullptr);
Douglas Gregor72172e92012-01-05 21:55:30 +0000656}
657
Richard Smith1fde8ec2012-09-07 02:06:42 +0000658TemplateTemplateParmDecl *
659TemplateTemplateParmDecl::CreateDeserialized(ASTContext &C, unsigned ID,
660 unsigned NumExpansions) {
Richard Smithf7981722013-11-22 09:01:48 +0000661 return new (C, ID, sizeof(TemplateParameterList*) * NumExpansions)
Craig Topper36250ad2014-05-12 05:36:57 +0000662 TemplateTemplateParmDecl(nullptr, SourceLocation(), 0, 0, nullptr,
663 nullptr, NumExpansions, nullptr);
Richard Smith1fde8ec2012-09-07 02:06:42 +0000664}
665
Douglas Gregor264ec4f2009-02-17 01:05:43 +0000666//===----------------------------------------------------------------------===//
Douglas Gregord002c7b2009-05-11 23:53:27 +0000667// TemplateArgumentList Implementation
668//===----------------------------------------------------------------------===//
Douglas Gregor1ccc8412010-11-07 23:05:16 +0000669TemplateArgumentList *
670TemplateArgumentList::CreateCopy(ASTContext &Context,
671 const TemplateArgument *Args,
672 unsigned NumArgs) {
673 std::size_t Size = sizeof(TemplateArgumentList)
674 + NumArgs * sizeof(TemplateArgument);
675 void *Mem = Context.Allocate(Size);
676 TemplateArgument *StoredArgs
677 = reinterpret_cast<TemplateArgument *>(
678 static_cast<TemplateArgumentList *>(Mem) + 1);
679 std::uninitialized_copy(Args, Args + NumArgs, StoredArgs);
680 return new (Mem) TemplateArgumentList(StoredArgs, NumArgs, true);
Argyrios Kyrtzidisfe6ba882010-06-23 13:48:23 +0000681}
682
Argyrios Kyrtzidise9a24432011-09-22 20:07:09 +0000683FunctionTemplateSpecializationInfo *
684FunctionTemplateSpecializationInfo::Create(ASTContext &C, FunctionDecl *FD,
685 FunctionTemplateDecl *Template,
686 TemplateSpecializationKind TSK,
687 const TemplateArgumentList *TemplateArgs,
688 const TemplateArgumentListInfo *TemplateArgsAsWritten,
689 SourceLocation POI) {
Craig Topper36250ad2014-05-12 05:36:57 +0000690 const ASTTemplateArgumentListInfo *ArgsAsWritten = nullptr;
Argyrios Kyrtzidise9a24432011-09-22 20:07:09 +0000691 if (TemplateArgsAsWritten)
692 ArgsAsWritten = ASTTemplateArgumentListInfo::Create(C,
693 *TemplateArgsAsWritten);
694
695 return new (C) FunctionTemplateSpecializationInfo(FD, Template, TSK,
696 TemplateArgs,
697 ArgsAsWritten,
698 POI);
699}
700
Douglas Gregord002c7b2009-05-11 23:53:27 +0000701//===----------------------------------------------------------------------===//
David Blaikie68e081d2011-12-20 02:48:34 +0000702// TemplateDecl Implementation
703//===----------------------------------------------------------------------===//
704
705void TemplateDecl::anchor() { }
706
707//===----------------------------------------------------------------------===//
Douglas Gregor264ec4f2009-02-17 01:05:43 +0000708// ClassTemplateSpecializationDecl Implementation
709//===----------------------------------------------------------------------===//
710ClassTemplateSpecializationDecl::
Douglas Gregore9029562010-05-06 00:28:52 +0000711ClassTemplateSpecializationDecl(ASTContext &Context, Kind DK, TagKind TK,
Abramo Bagnara29c2d462011-03-09 14:09:51 +0000712 DeclContext *DC, SourceLocation StartLoc,
713 SourceLocation IdLoc,
Douglas Gregor264ec4f2009-02-17 01:05:43 +0000714 ClassTemplateDecl *SpecializedTemplate,
Douglas Gregor1ccc8412010-11-07 23:05:16 +0000715 const TemplateArgument *Args,
716 unsigned NumArgs,
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +0000717 ClassTemplateSpecializationDecl *PrevDecl)
Richard Smith053f6c62014-05-16 23:01:30 +0000718 : CXXRecordDecl(DK, TK, Context, DC, StartLoc, IdLoc,
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +0000719 SpecializedTemplate->getIdentifier(),
720 PrevDecl),
Douglas Gregor264ec4f2009-02-17 01:05:43 +0000721 SpecializedTemplate(SpecializedTemplate),
Craig Topper36250ad2014-05-12 05:36:57 +0000722 ExplicitInfo(nullptr),
Douglas Gregor1ccc8412010-11-07 23:05:16 +0000723 TemplateArgs(TemplateArgumentList::CreateCopy(Context, Args, NumArgs)),
Douglas Gregord002c7b2009-05-11 23:53:27 +0000724 SpecializationKind(TSK_Undeclared) {
Douglas Gregor264ec4f2009-02-17 01:05:43 +0000725}
Mike Stump11289f42009-09-09 15:08:12 +0000726
Richard Smith053f6c62014-05-16 23:01:30 +0000727ClassTemplateSpecializationDecl::ClassTemplateSpecializationDecl(ASTContext &C,
728 Kind DK)
729 : CXXRecordDecl(DK, TTK_Struct, C, nullptr, SourceLocation(),
730 SourceLocation(), nullptr, nullptr),
731 ExplicitInfo(nullptr), SpecializationKind(TSK_Undeclared) {}
Argyrios Kyrtzidisfe6ba882010-06-23 13:48:23 +0000732
Douglas Gregor264ec4f2009-02-17 01:05:43 +0000733ClassTemplateSpecializationDecl *
Douglas Gregore9029562010-05-06 00:28:52 +0000734ClassTemplateSpecializationDecl::Create(ASTContext &Context, TagKind TK,
Abramo Bagnara29c2d462011-03-09 14:09:51 +0000735 DeclContext *DC,
736 SourceLocation StartLoc,
737 SourceLocation IdLoc,
Douglas Gregor264ec4f2009-02-17 01:05:43 +0000738 ClassTemplateDecl *SpecializedTemplate,
Douglas Gregor1ccc8412010-11-07 23:05:16 +0000739 const TemplateArgument *Args,
740 unsigned NumArgs,
Douglas Gregor67a65642009-02-17 23:15:12 +0000741 ClassTemplateSpecializationDecl *PrevDecl) {
Richard Smithf7981722013-11-22 09:01:48 +0000742 ClassTemplateSpecializationDecl *Result =
743 new (Context, DC) ClassTemplateSpecializationDecl(
744 Context, ClassTemplateSpecialization, TK, DC, StartLoc, IdLoc,
745 SpecializedTemplate, Args, NumArgs, PrevDecl);
Douglas Gregor7dab26b2013-02-09 01:35:03 +0000746 Result->MayHaveOutOfDateDef = false;
747
Douglas Gregor67a65642009-02-17 23:15:12 +0000748 Context.getTypeDeclType(Result, PrevDecl);
749 return Result;
Douglas Gregor264ec4f2009-02-17 01:05:43 +0000750}
Douglas Gregor2373c592009-05-31 09:31:02 +0000751
Argyrios Kyrtzidisfe6ba882010-06-23 13:48:23 +0000752ClassTemplateSpecializationDecl *
Richard Smithf7981722013-11-22 09:01:48 +0000753ClassTemplateSpecializationDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +0000754 unsigned ID) {
Douglas Gregor7dab26b2013-02-09 01:35:03 +0000755 ClassTemplateSpecializationDecl *Result =
Richard Smith053f6c62014-05-16 23:01:30 +0000756 new (C, ID) ClassTemplateSpecializationDecl(C, ClassTemplateSpecialization);
Douglas Gregor7dab26b2013-02-09 01:35:03 +0000757 Result->MayHaveOutOfDateDef = false;
758 return Result;
Argyrios Kyrtzidisfe6ba882010-06-23 13:48:23 +0000759}
760
Benjamin Kramer9170e912013-02-22 15:46:01 +0000761void ClassTemplateSpecializationDecl::getNameForDiagnostic(
762 raw_ostream &OS, const PrintingPolicy &Policy, bool Qualified) const {
763 NamedDecl::getNameForDiagnostic(OS, Policy, Qualified);
Douglas Gregorb11aad82011-02-19 18:51:44 +0000764
765 const TemplateArgumentList &TemplateArgs = getTemplateArgs();
Benjamin Kramer9170e912013-02-22 15:46:01 +0000766 TemplateSpecializationType::PrintTemplateArgumentList(
767 OS, TemplateArgs.data(), TemplateArgs.size(), Policy);
Douglas Gregorb11aad82011-02-19 18:51:44 +0000768}
769
Douglas Gregor9dc8bd32009-08-02 23:24:31 +0000770ClassTemplateDecl *
Mike Stump11289f42009-09-09 15:08:12 +0000771ClassTemplateSpecializationDecl::getSpecializedTemplate() const {
772 if (SpecializedPartialSpecialization *PartialSpec
Douglas Gregor9dc8bd32009-08-02 23:24:31 +0000773 = SpecializedTemplate.dyn_cast<SpecializedPartialSpecialization*>())
774 return PartialSpec->PartialSpecialization->getSpecializedTemplate();
775 return SpecializedTemplate.get<ClassTemplateDecl*>();
776}
777
Abramo Bagnaraa0935262011-03-04 14:20:30 +0000778SourceRange
779ClassTemplateSpecializationDecl::getSourceRange() const {
Abramo Bagnarafd3a4552011-10-03 20:34:03 +0000780 if (ExplicitInfo) {
Abramo Bagnarac76dcbd2012-10-15 21:06:42 +0000781 SourceLocation Begin = getTemplateKeywordLoc();
782 if (Begin.isValid()) {
783 // Here we have an explicit (partial) specialization or instantiation.
784 assert(getSpecializationKind() == TSK_ExplicitSpecialization ||
785 getSpecializationKind() == TSK_ExplicitInstantiationDeclaration ||
786 getSpecializationKind() == TSK_ExplicitInstantiationDefinition);
787 if (getExternLoc().isValid())
788 Begin = getExternLoc();
789 SourceLocation End = getRBraceLoc();
790 if (End.isInvalid())
791 End = getTypeAsWritten()->getTypeLoc().getEndLoc();
792 return SourceRange(Begin, End);
793 }
794 // An implicit instantiation of a class template partial specialization
795 // uses ExplicitInfo to record the TypeAsWritten, but the source
796 // locations should be retrieved from the instantiation pattern.
797 typedef ClassTemplatePartialSpecializationDecl CTPSDecl;
798 CTPSDecl *ctpsd = const_cast<CTPSDecl*>(cast<CTPSDecl>(this));
799 CTPSDecl *inst_from = ctpsd->getInstantiatedFromMember();
Craig Topper36250ad2014-05-12 05:36:57 +0000800 assert(inst_from != nullptr);
Abramo Bagnarac76dcbd2012-10-15 21:06:42 +0000801 return inst_from->getSourceRange();
Abramo Bagnarafd3a4552011-10-03 20:34:03 +0000802 }
803 else {
804 // No explicit info available.
805 llvm::PointerUnion<ClassTemplateDecl *,
806 ClassTemplatePartialSpecializationDecl *>
807 inst_from = getInstantiatedFrom();
808 if (inst_from.isNull())
809 return getSpecializedTemplate()->getSourceRange();
810 if (ClassTemplateDecl *ctd = inst_from.dyn_cast<ClassTemplateDecl*>())
811 return ctd->getSourceRange();
812 return inst_from.get<ClassTemplatePartialSpecializationDecl*>()
813 ->getSourceRange();
814 }
Abramo Bagnaraa0935262011-03-04 14:20:30 +0000815}
816
Douglas Gregor2373c592009-05-31 09:31:02 +0000817//===----------------------------------------------------------------------===//
818// ClassTemplatePartialSpecializationDecl Implementation
819//===----------------------------------------------------------------------===//
David Blaikie68e081d2011-12-20 02:48:34 +0000820void ClassTemplatePartialSpecializationDecl::anchor() { }
821
Douglas Gregorfd7c2252011-03-04 17:52:15 +0000822ClassTemplatePartialSpecializationDecl::
823ClassTemplatePartialSpecializationDecl(ASTContext &Context, TagKind TK,
Abramo Bagnara29c2d462011-03-09 14:09:51 +0000824 DeclContext *DC,
825 SourceLocation StartLoc,
826 SourceLocation IdLoc,
Douglas Gregorfd7c2252011-03-04 17:52:15 +0000827 TemplateParameterList *Params,
828 ClassTemplateDecl *SpecializedTemplate,
829 const TemplateArgument *Args,
830 unsigned NumArgs,
Enea Zaffanella6dbe1872013-08-10 07:24:53 +0000831 const ASTTemplateArgumentListInfo *ArgInfos,
Richard Smithb2f61b42013-08-22 23:27:37 +0000832 ClassTemplatePartialSpecializationDecl *PrevDecl)
Douglas Gregorfd7c2252011-03-04 17:52:15 +0000833 : ClassTemplateSpecializationDecl(Context,
834 ClassTemplatePartialSpecialization,
Abramo Bagnara29c2d462011-03-09 14:09:51 +0000835 TK, DC, StartLoc, IdLoc,
836 SpecializedTemplate,
Douglas Gregorfd7c2252011-03-04 17:52:15 +0000837 Args, NumArgs, PrevDecl),
838 TemplateParams(Params), ArgsAsWritten(ArgInfos),
Craig Topper36250ad2014-05-12 05:36:57 +0000839 InstantiatedFromMember(nullptr, false)
Enea Zaffanella6dbe1872013-08-10 07:24:53 +0000840{
Douglas Gregor3c41bf72011-03-04 18:32:38 +0000841 AdoptTemplateParameterList(Params, this);
Douglas Gregorfd7c2252011-03-04 17:52:15 +0000842}
843
Douglas Gregor2373c592009-05-31 09:31:02 +0000844ClassTemplatePartialSpecializationDecl *
845ClassTemplatePartialSpecializationDecl::
Abramo Bagnara29c2d462011-03-09 14:09:51 +0000846Create(ASTContext &Context, TagKind TK,DeclContext *DC,
847 SourceLocation StartLoc, SourceLocation IdLoc,
Douglas Gregor2373c592009-05-31 09:31:02 +0000848 TemplateParameterList *Params,
849 ClassTemplateDecl *SpecializedTemplate,
Douglas Gregor1ccc8412010-11-07 23:05:16 +0000850 const TemplateArgument *Args,
851 unsigned NumArgs,
John McCall6b51f282009-11-23 01:53:49 +0000852 const TemplateArgumentListInfo &ArgInfos,
John McCalle78aac42010-03-10 03:28:59 +0000853 QualType CanonInjectedType,
Richard Smithb2f61b42013-08-22 23:27:37 +0000854 ClassTemplatePartialSpecializationDecl *PrevDecl) {
Enea Zaffanella6dbe1872013-08-10 07:24:53 +0000855 const ASTTemplateArgumentListInfo *ASTArgInfos =
856 ASTTemplateArgumentListInfo::Create(Context, ArgInfos);
John McCall0ad16662009-10-29 08:12:44 +0000857
Richard Smithf7981722013-11-22 09:01:48 +0000858 ClassTemplatePartialSpecializationDecl *Result = new (Context, DC)
859 ClassTemplatePartialSpecializationDecl(Context, TK, DC, StartLoc, IdLoc,
860 Params, SpecializedTemplate, Args,
861 NumArgs, ASTArgInfos, PrevDecl);
Douglas Gregor2373c592009-05-31 09:31:02 +0000862 Result->setSpecializationKind(TSK_ExplicitSpecialization);
Douglas Gregor7dab26b2013-02-09 01:35:03 +0000863 Result->MayHaveOutOfDateDef = false;
John McCalle78aac42010-03-10 03:28:59 +0000864
865 Context.getInjectedClassNameType(Result, CanonInjectedType);
Douglas Gregor2373c592009-05-31 09:31:02 +0000866 return Result;
867}
John McCall11083da2009-09-16 22:47:08 +0000868
Argyrios Kyrtzidisfe6ba882010-06-23 13:48:23 +0000869ClassTemplatePartialSpecializationDecl *
Douglas Gregor72172e92012-01-05 21:55:30 +0000870ClassTemplatePartialSpecializationDecl::CreateDeserialized(ASTContext &C,
871 unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +0000872 ClassTemplatePartialSpecializationDecl *Result =
Richard Smith053f6c62014-05-16 23:01:30 +0000873 new (C, ID) ClassTemplatePartialSpecializationDecl(C);
Douglas Gregor7dab26b2013-02-09 01:35:03 +0000874 Result->MayHaveOutOfDateDef = false;
875 return Result;
Argyrios Kyrtzidisfe6ba882010-06-23 13:48:23 +0000876}
877
John McCall11083da2009-09-16 22:47:08 +0000878//===----------------------------------------------------------------------===//
879// FriendTemplateDecl Implementation
880//===----------------------------------------------------------------------===//
881
David Blaikie68e081d2011-12-20 02:48:34 +0000882void FriendTemplateDecl::anchor() { }
883
John McCall11083da2009-09-16 22:47:08 +0000884FriendTemplateDecl *FriendTemplateDecl::Create(ASTContext &Context,
885 DeclContext *DC,
886 SourceLocation L,
887 unsigned NParams,
888 TemplateParameterList **Params,
889 FriendUnion Friend,
890 SourceLocation FLoc) {
Richard Smithf7981722013-11-22 09:01:48 +0000891 return new (Context, DC) FriendTemplateDecl(DC, L, NParams, Params,
892 Friend, FLoc);
John McCall11083da2009-09-16 22:47:08 +0000893}
Argyrios Kyrtzidis165b5812010-07-22 16:04:10 +0000894
Douglas Gregor72172e92012-01-05 21:55:30 +0000895FriendTemplateDecl *FriendTemplateDecl::CreateDeserialized(ASTContext &C,
896 unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +0000897 return new (C, ID) FriendTemplateDecl(EmptyShell());
Argyrios Kyrtzidis165b5812010-07-22 16:04:10 +0000898}
Richard Smith3f1b5d02011-05-05 21:57:07 +0000899
900//===----------------------------------------------------------------------===//
901// TypeAliasTemplateDecl Implementation
902//===----------------------------------------------------------------------===//
903
904TypeAliasTemplateDecl *TypeAliasTemplateDecl::Create(ASTContext &C,
905 DeclContext *DC,
906 SourceLocation L,
907 DeclarationName Name,
908 TemplateParameterList *Params,
909 NamedDecl *Decl) {
910 AdoptTemplateParameterList(Params, DC);
Richard Smith053f6c62014-05-16 23:01:30 +0000911 return new (C, DC) TypeAliasTemplateDecl(C, DC, L, Name, Params, Decl);
Richard Smith3f1b5d02011-05-05 21:57:07 +0000912}
913
Douglas Gregor72172e92012-01-05 21:55:30 +0000914TypeAliasTemplateDecl *TypeAliasTemplateDecl::CreateDeserialized(ASTContext &C,
915 unsigned ID) {
Richard Smith053f6c62014-05-16 23:01:30 +0000916 return new (C, ID) TypeAliasTemplateDecl(C, nullptr, SourceLocation(),
Craig Topper36250ad2014-05-12 05:36:57 +0000917 DeclarationName(), nullptr, nullptr);
Richard Smith3f1b5d02011-05-05 21:57:07 +0000918}
919
920void TypeAliasTemplateDecl::DeallocateCommon(void *Ptr) {
921 static_cast<Common *>(Ptr)->~Common();
922}
923RedeclarableTemplateDecl::CommonBase *
Dmitri Gribenkob53f37c2013-01-23 16:52:57 +0000924TypeAliasTemplateDecl::newCommon(ASTContext &C) const {
Richard Smith3f1b5d02011-05-05 21:57:07 +0000925 Common *CommonPtr = new (C) Common;
926 C.AddDeallocation(DeallocateCommon, CommonPtr);
927 return CommonPtr;
928}
929
David Blaikie68e081d2011-12-20 02:48:34 +0000930//===----------------------------------------------------------------------===//
931// ClassScopeFunctionSpecializationDecl Implementation
932//===----------------------------------------------------------------------===//
933
934void ClassScopeFunctionSpecializationDecl::anchor() { }
Douglas Gregor72172e92012-01-05 21:55:30 +0000935
936ClassScopeFunctionSpecializationDecl *
937ClassScopeFunctionSpecializationDecl::CreateDeserialized(ASTContext &C,
938 unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +0000939 return new (C, ID) ClassScopeFunctionSpecializationDecl(
Craig Topper36250ad2014-05-12 05:36:57 +0000940 nullptr, SourceLocation(), nullptr, false, TemplateArgumentListInfo());
Douglas Gregor72172e92012-01-05 21:55:30 +0000941}
Larisse Voufo39a1e502013-08-06 01:03:05 +0000942
943//===----------------------------------------------------------------------===//
944// VarTemplateDecl Implementation
945//===----------------------------------------------------------------------===//
946
947void VarTemplateDecl::DeallocateCommon(void *Ptr) {
948 static_cast<Common *>(Ptr)->~Common();
949}
950
Larisse Voufoa11bd8a2013-08-13 02:02:26 +0000951VarTemplateDecl *VarTemplateDecl::getDefinition() {
952 VarTemplateDecl *CurD = this;
953 while (CurD) {
954 if (CurD->isThisDeclarationADefinition())
955 return CurD;
956 CurD = CurD->getPreviousDecl();
957 }
Craig Topper36250ad2014-05-12 05:36:57 +0000958 return nullptr;
Larisse Voufoa11bd8a2013-08-13 02:02:26 +0000959}
960
Larisse Voufo39a1e502013-08-06 01:03:05 +0000961VarTemplateDecl *VarTemplateDecl::Create(ASTContext &C, DeclContext *DC,
962 SourceLocation L, DeclarationName Name,
963 TemplateParameterList *Params,
Richard Smithbeef3452014-01-16 23:39:20 +0000964 VarDecl *Decl) {
Richard Smith053f6c62014-05-16 23:01:30 +0000965 return new (C, DC) VarTemplateDecl(C, DC, L, Name, Params, Decl);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000966}
967
968VarTemplateDecl *VarTemplateDecl::CreateDeserialized(ASTContext &C,
969 unsigned ID) {
Richard Smith053f6c62014-05-16 23:01:30 +0000970 return new (C, ID) VarTemplateDecl(C, nullptr, SourceLocation(),
971 DeclarationName(), nullptr, nullptr);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000972}
973
Alp Tokerf6a24ce2013-12-05 16:25:25 +0000974// TODO: Unify across class, function and variable templates?
Larisse Voufo30616382013-08-23 22:21:36 +0000975// May require moving this and Common to RedeclarableTemplateDecl.
Larisse Voufo39a1e502013-08-06 01:03:05 +0000976void VarTemplateDecl::LoadLazySpecializations() const {
Richard Smithe3536dd2015-02-24 02:44:23 +0000977 // Grab the most recent declaration to ensure we've loaded any lazy
978 // redeclarations of this template.
979 //
980 // FIXME: Avoid walking the entire redeclaration chain here.
981 Common *CommonPtr = getMostRecentDecl()->getCommonPtr();
Larisse Voufo39a1e502013-08-06 01:03:05 +0000982 if (CommonPtr->LazySpecializations) {
983 ASTContext &Context = getASTContext();
984 uint32_t *Specs = CommonPtr->LazySpecializations;
Craig Topper36250ad2014-05-12 05:36:57 +0000985 CommonPtr->LazySpecializations = nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +0000986 for (uint32_t I = 0, N = *Specs++; I != N; ++I)
987 (void)Context.getExternalSource()->GetExternalDecl(Specs[I]);
988 }
989}
990
991llvm::FoldingSetVector<VarTemplateSpecializationDecl> &
992VarTemplateDecl::getSpecializations() const {
993 LoadLazySpecializations();
994 return getCommonPtr()->Specializations;
995}
996
997llvm::FoldingSetVector<VarTemplatePartialSpecializationDecl> &
998VarTemplateDecl::getPartialSpecializations() {
999 LoadLazySpecializations();
1000 return getCommonPtr()->PartialSpecializations;
1001}
1002
1003RedeclarableTemplateDecl::CommonBase *
1004VarTemplateDecl::newCommon(ASTContext &C) const {
1005 Common *CommonPtr = new (C) Common;
1006 C.AddDeallocation(DeallocateCommon, CommonPtr);
1007 return CommonPtr;
1008}
1009
1010VarTemplateSpecializationDecl *
Craig Topper7e0daca2014-06-26 04:58:53 +00001011VarTemplateDecl::findSpecialization(ArrayRef<TemplateArgument> Args,
1012 void *&InsertPos) {
1013 return findSpecializationImpl(getSpecializations(), Args, InsertPos);
Larisse Voufo39a1e502013-08-06 01:03:05 +00001014}
1015
1016void VarTemplateDecl::AddSpecialization(VarTemplateSpecializationDecl *D,
1017 void *InsertPos) {
Richard Smithe977e512015-02-24 01:23:23 +00001018 addSpecializationImpl<VarTemplateDecl>(getSpecializations(), D, InsertPos);
Larisse Voufo39a1e502013-08-06 01:03:05 +00001019}
1020
1021VarTemplatePartialSpecializationDecl *
Craig Topper7e0daca2014-06-26 04:58:53 +00001022VarTemplateDecl::findPartialSpecialization(ArrayRef<TemplateArgument> Args,
1023 void *&InsertPos) {
1024 return findSpecializationImpl(getPartialSpecializations(), Args, InsertPos);
Larisse Voufo39a1e502013-08-06 01:03:05 +00001025}
1026
1027void VarTemplateDecl::AddPartialSpecialization(
1028 VarTemplatePartialSpecializationDecl *D, void *InsertPos) {
1029 if (InsertPos)
1030 getPartialSpecializations().InsertNode(D, InsertPos);
1031 else {
1032 VarTemplatePartialSpecializationDecl *Existing =
1033 getPartialSpecializations().GetOrInsertNode(D);
1034 (void)Existing;
1035 assert(Existing->isCanonicalDecl() && "Non-canonical specialization?");
1036 }
1037
1038 if (ASTMutationListener *L = getASTMutationListener())
1039 L->AddedCXXTemplateSpecialization(this, D);
1040}
1041
1042void VarTemplateDecl::getPartialSpecializations(
1043 SmallVectorImpl<VarTemplatePartialSpecializationDecl *> &PS) {
1044 llvm::FoldingSetVector<VarTemplatePartialSpecializationDecl> &PartialSpecs =
1045 getPartialSpecializations();
1046 PS.clear();
Richard Smithb2f61b42013-08-22 23:27:37 +00001047 PS.reserve(PartialSpecs.size());
Larisse Voufo39a1e502013-08-06 01:03:05 +00001048 for (llvm::FoldingSetVector<VarTemplatePartialSpecializationDecl>::iterator
1049 P = PartialSpecs.begin(),
1050 PEnd = PartialSpecs.end();
Richard Smithb2f61b42013-08-22 23:27:37 +00001051 P != PEnd; ++P)
1052 PS.push_back(P->getMostRecentDecl());
Larisse Voufo39a1e502013-08-06 01:03:05 +00001053}
1054
1055VarTemplatePartialSpecializationDecl *
1056VarTemplateDecl::findPartialSpecInstantiatedFromMember(
1057 VarTemplatePartialSpecializationDecl *D) {
1058 Decl *DCanon = D->getCanonicalDecl();
1059 for (llvm::FoldingSetVector<VarTemplatePartialSpecializationDecl>::iterator
1060 P = getPartialSpecializations().begin(),
1061 PEnd = getPartialSpecializations().end();
1062 P != PEnd; ++P) {
1063 if (P->getInstantiatedFromMember()->getCanonicalDecl() == DCanon)
1064 return P->getMostRecentDecl();
1065 }
1066
Craig Topper36250ad2014-05-12 05:36:57 +00001067 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00001068}
1069
1070//===----------------------------------------------------------------------===//
1071// VarTemplateSpecializationDecl Implementation
1072//===----------------------------------------------------------------------===//
1073VarTemplateSpecializationDecl::VarTemplateSpecializationDecl(
Richard Smith053f6c62014-05-16 23:01:30 +00001074 Kind DK, ASTContext &Context, DeclContext *DC, SourceLocation StartLoc,
Larisse Voufo39a1e502013-08-06 01:03:05 +00001075 SourceLocation IdLoc, VarTemplateDecl *SpecializedTemplate, QualType T,
1076 TypeSourceInfo *TInfo, StorageClass S, const TemplateArgument *Args,
1077 unsigned NumArgs)
Richard Smith053f6c62014-05-16 23:01:30 +00001078 : VarDecl(DK, Context, DC, StartLoc, IdLoc,
1079 SpecializedTemplate->getIdentifier(), T, TInfo, S),
Craig Topper36250ad2014-05-12 05:36:57 +00001080 SpecializedTemplate(SpecializedTemplate), ExplicitInfo(nullptr),
Larisse Voufo39a1e502013-08-06 01:03:05 +00001081 TemplateArgs(TemplateArgumentList::CreateCopy(Context, Args, NumArgs)),
1082 SpecializationKind(TSK_Undeclared) {}
1083
Richard Smith053f6c62014-05-16 23:01:30 +00001084VarTemplateSpecializationDecl::VarTemplateSpecializationDecl(Kind DK,
1085 ASTContext &C)
1086 : VarDecl(DK, C, nullptr, SourceLocation(), SourceLocation(), nullptr,
Craig Topper36250ad2014-05-12 05:36:57 +00001087 QualType(), nullptr, SC_None),
1088 ExplicitInfo(nullptr), SpecializationKind(TSK_Undeclared) {}
Larisse Voufo39a1e502013-08-06 01:03:05 +00001089
1090VarTemplateSpecializationDecl *VarTemplateSpecializationDecl::Create(
1091 ASTContext &Context, DeclContext *DC, SourceLocation StartLoc,
1092 SourceLocation IdLoc, VarTemplateDecl *SpecializedTemplate, QualType T,
1093 TypeSourceInfo *TInfo, StorageClass S, const TemplateArgument *Args,
1094 unsigned NumArgs) {
Richard Smithf7981722013-11-22 09:01:48 +00001095 return new (Context, DC) VarTemplateSpecializationDecl(
Richard Smith053f6c62014-05-16 23:01:30 +00001096 VarTemplateSpecialization, Context, DC, StartLoc, IdLoc,
Richard Smithf7981722013-11-22 09:01:48 +00001097 SpecializedTemplate, T, TInfo, S, Args, NumArgs);
Larisse Voufo39a1e502013-08-06 01:03:05 +00001098}
1099
1100VarTemplateSpecializationDecl *
1101VarTemplateSpecializationDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smith053f6c62014-05-16 23:01:30 +00001102 return new (C, ID)
1103 VarTemplateSpecializationDecl(VarTemplateSpecialization, C);
Larisse Voufo39a1e502013-08-06 01:03:05 +00001104}
1105
1106void VarTemplateSpecializationDecl::getNameForDiagnostic(
1107 raw_ostream &OS, const PrintingPolicy &Policy, bool Qualified) const {
1108 NamedDecl::getNameForDiagnostic(OS, Policy, Qualified);
1109
1110 const TemplateArgumentList &TemplateArgs = getTemplateArgs();
1111 TemplateSpecializationType::PrintTemplateArgumentList(
1112 OS, TemplateArgs.data(), TemplateArgs.size(), Policy);
1113}
1114
1115VarTemplateDecl *VarTemplateSpecializationDecl::getSpecializedTemplate() const {
1116 if (SpecializedPartialSpecialization *PartialSpec =
1117 SpecializedTemplate.dyn_cast<SpecializedPartialSpecialization *>())
1118 return PartialSpec->PartialSpecialization->getSpecializedTemplate();
1119 return SpecializedTemplate.get<VarTemplateDecl *>();
1120}
1121
1122void VarTemplateSpecializationDecl::setTemplateArgsInfo(
1123 const TemplateArgumentListInfo &ArgsInfo) {
1124 unsigned N = ArgsInfo.size();
1125 TemplateArgsInfo.setLAngleLoc(ArgsInfo.getLAngleLoc());
1126 TemplateArgsInfo.setRAngleLoc(ArgsInfo.getRAngleLoc());
1127 for (unsigned I = 0; I != N; ++I)
1128 TemplateArgsInfo.addArgument(ArgsInfo[I]);
1129}
1130
1131//===----------------------------------------------------------------------===//
1132// VarTemplatePartialSpecializationDecl Implementation
1133//===----------------------------------------------------------------------===//
1134void VarTemplatePartialSpecializationDecl::anchor() {}
1135
1136VarTemplatePartialSpecializationDecl::VarTemplatePartialSpecializationDecl(
1137 ASTContext &Context, DeclContext *DC, SourceLocation StartLoc,
1138 SourceLocation IdLoc, TemplateParameterList *Params,
1139 VarTemplateDecl *SpecializedTemplate, QualType T, TypeSourceInfo *TInfo,
1140 StorageClass S, const TemplateArgument *Args, unsigned NumArgs,
Richard Smithb2f61b42013-08-22 23:27:37 +00001141 const ASTTemplateArgumentListInfo *ArgInfos)
Richard Smith053f6c62014-05-16 23:01:30 +00001142 : VarTemplateSpecializationDecl(VarTemplatePartialSpecialization, Context,
Larisse Voufo39a1e502013-08-06 01:03:05 +00001143 DC, StartLoc, IdLoc, SpecializedTemplate, T,
1144 TInfo, S, Args, NumArgs),
1145 TemplateParams(Params), ArgsAsWritten(ArgInfos),
Craig Topper36250ad2014-05-12 05:36:57 +00001146 InstantiatedFromMember(nullptr, false) {
Larisse Voufo39a1e502013-08-06 01:03:05 +00001147 // TODO: The template parameters should be in DC by now. Verify.
1148 // AdoptTemplateParameterList(Params, DC);
1149}
1150
1151VarTemplatePartialSpecializationDecl *
1152VarTemplatePartialSpecializationDecl::Create(
1153 ASTContext &Context, DeclContext *DC, SourceLocation StartLoc,
1154 SourceLocation IdLoc, TemplateParameterList *Params,
1155 VarTemplateDecl *SpecializedTemplate, QualType T, TypeSourceInfo *TInfo,
1156 StorageClass S, const TemplateArgument *Args, unsigned NumArgs,
Richard Smithb2f61b42013-08-22 23:27:37 +00001157 const TemplateArgumentListInfo &ArgInfos) {
Enea Zaffanella6dbe1872013-08-10 07:24:53 +00001158 const ASTTemplateArgumentListInfo *ASTArgInfos
1159 = ASTTemplateArgumentListInfo::Create(Context, ArgInfos);
Larisse Voufo39a1e502013-08-06 01:03:05 +00001160
1161 VarTemplatePartialSpecializationDecl *Result =
Richard Smithf7981722013-11-22 09:01:48 +00001162 new (Context, DC) VarTemplatePartialSpecializationDecl(
Larisse Voufo39a1e502013-08-06 01:03:05 +00001163 Context, DC, StartLoc, IdLoc, Params, SpecializedTemplate, T, TInfo,
Richard Smithb2f61b42013-08-22 23:27:37 +00001164 S, Args, NumArgs, ASTArgInfos);
Larisse Voufo39a1e502013-08-06 01:03:05 +00001165 Result->setSpecializationKind(TSK_ExplicitSpecialization);
1166 return Result;
1167}
1168
1169VarTemplatePartialSpecializationDecl *
1170VarTemplatePartialSpecializationDecl::CreateDeserialized(ASTContext &C,
1171 unsigned ID) {
Richard Smith053f6c62014-05-16 23:01:30 +00001172 return new (C, ID) VarTemplatePartialSpecializationDecl(C);
Larisse Voufo39a1e502013-08-06 01:03:05 +00001173}