blob: 866b9fc11da680ee006b1f968430690f87b6e17e [file] [log] [blame]
Douglas Gregor99ebf652009-02-27 19:31:52 +00001//===------- SemaTemplateInstantiate.cpp - C++ Template Instantiation ------===/
2//
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// This file implements C++ template instantiation.
10//
11//===----------------------------------------------------------------------===/
12
John McCall2d887082010-08-25 22:03:47 +000013#include "clang/Sema/SemaInternal.h"
Douglas Gregor577f75a2009-08-04 16:50:30 +000014#include "TreeTransform.h"
John McCall19510852010-08-20 18:27:03 +000015#include "clang/Sema/DeclSpec.h"
Richard Smith7a614d82011-06-11 17:19:42 +000016#include "clang/Sema/Initialization.h"
Douglas Gregore737f502010-08-12 20:07:10 +000017#include "clang/Sema/Lookup.h"
John McCall7cd088e2010-08-24 07:21:54 +000018#include "clang/Sema/Template.h"
John McCall2a7fb272010-08-25 05:32:35 +000019#include "clang/Sema/TemplateDeduction.h"
Douglas Gregoraba43bb2009-05-26 20:50:29 +000020#include "clang/AST/ASTConsumer.h"
Douglas Gregor99ebf652009-02-27 19:31:52 +000021#include "clang/AST/ASTContext.h"
22#include "clang/AST/Expr.h"
Douglas Gregor99ebf652009-02-27 19:31:52 +000023#include "clang/AST/DeclTemplate.h"
Douglas Gregor99ebf652009-02-27 19:31:52 +000024#include "clang/Basic/LangOptions.h"
25
26using namespace clang;
John McCall2a7fb272010-08-25 05:32:35 +000027using namespace sema;
Douglas Gregor99ebf652009-02-27 19:31:52 +000028
Douglas Gregoree1828a2009-03-10 18:03:33 +000029//===----------------------------------------------------------------------===/
30// Template Instantiation Support
31//===----------------------------------------------------------------------===/
32
Douglas Gregord6350ae2009-08-28 20:31:08 +000033/// \brief Retrieve the template argument list(s) that should be used to
34/// instantiate the definition of the given declaration.
Douglas Gregor0f8716b2009-11-09 19:17:50 +000035///
36/// \param D the declaration for which we are computing template instantiation
37/// arguments.
38///
39/// \param Innermost if non-NULL, the innermost template argument list.
Douglas Gregor525f96c2010-02-05 07:33:43 +000040///
41/// \param RelativeToPrimary true if we should get the template
42/// arguments relative to the primary template, even when we're
43/// dealing with a specialization. This is only relevant for function
44/// template specializations.
Douglas Gregore7089b02010-05-03 23:29:10 +000045///
46/// \param Pattern If non-NULL, indicates the pattern from which we will be
47/// instantiating the definition of the given declaration, \p D. This is
48/// used to determine the proper set of template instantiation arguments for
49/// friend function template specializations.
Douglas Gregord1102432009-08-28 17:37:35 +000050MultiLevelTemplateArgumentList
Douglas Gregor0f8716b2009-11-09 19:17:50 +000051Sema::getTemplateInstantiationArgs(NamedDecl *D,
Douglas Gregor525f96c2010-02-05 07:33:43 +000052 const TemplateArgumentList *Innermost,
Douglas Gregore7089b02010-05-03 23:29:10 +000053 bool RelativeToPrimary,
54 const FunctionDecl *Pattern) {
Douglas Gregord1102432009-08-28 17:37:35 +000055 // Accumulate the set of template argument lists in this structure.
56 MultiLevelTemplateArgumentList Result;
Mike Stump1eb44332009-09-09 15:08:12 +000057
Douglas Gregor0f8716b2009-11-09 19:17:50 +000058 if (Innermost)
59 Result.addOuterTemplateArguments(Innermost);
60
Douglas Gregord1102432009-08-28 17:37:35 +000061 DeclContext *Ctx = dyn_cast<DeclContext>(D);
Douglas Gregor93104c12011-05-22 00:21:10 +000062 if (!Ctx) {
Douglas Gregord1102432009-08-28 17:37:35 +000063 Ctx = D->getDeclContext();
Douglas Gregor93104c12011-05-22 00:21:10 +000064
Douglas Gregor383041d2011-06-15 14:20:42 +000065 // If we have a template template parameter with translation unit context,
66 // then we're performing substitution into a default template argument of
67 // this template template parameter before we've constructed the template
68 // that will own this template template parameter. In this case, we
69 // use empty template parameter lists for all of the outer templates
70 // to avoid performing any substitutions.
71 if (Ctx->isTranslationUnit()) {
72 if (TemplateTemplateParmDecl *TTP
73 = dyn_cast<TemplateTemplateParmDecl>(D)) {
74 for (unsigned I = 0, N = TTP->getDepth() + 1; I != N; ++I)
75 Result.addOuterTemplateArguments(0, 0);
76 return Result;
77 }
78 }
Douglas Gregor93104c12011-05-22 00:21:10 +000079 }
80
John McCallf181d8a2009-08-29 03:16:09 +000081 while (!Ctx->isFileContext()) {
Douglas Gregord1102432009-08-28 17:37:35 +000082 // Add template arguments from a class template instantiation.
Mike Stump1eb44332009-09-09 15:08:12 +000083 if (ClassTemplateSpecializationDecl *Spec
Douglas Gregord1102432009-08-28 17:37:35 +000084 = dyn_cast<ClassTemplateSpecializationDecl>(Ctx)) {
85 // We're done when we hit an explicit specialization.
Douglas Gregor24bae922010-07-08 18:37:38 +000086 if (Spec->getSpecializationKind() == TSK_ExplicitSpecialization &&
87 !isa<ClassTemplatePartialSpecializationDecl>(Spec))
Douglas Gregord1102432009-08-28 17:37:35 +000088 break;
Mike Stump1eb44332009-09-09 15:08:12 +000089
Douglas Gregord1102432009-08-28 17:37:35 +000090 Result.addOuterTemplateArguments(&Spec->getTemplateInstantiationArgs());
Douglas Gregorfd056bc2009-10-13 16:30:37 +000091
92 // If this class template specialization was instantiated from a
93 // specialized member that is a class template, we're done.
94 assert(Spec->getSpecializedTemplate() && "No class template?");
95 if (Spec->getSpecializedTemplate()->isMemberSpecialization())
96 break;
Mike Stump1eb44332009-09-09 15:08:12 +000097 }
Douglas Gregord1102432009-08-28 17:37:35 +000098 // Add template arguments from a function template specialization.
John McCallf181d8a2009-08-29 03:16:09 +000099 else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Ctx)) {
Douglas Gregor525f96c2010-02-05 07:33:43 +0000100 if (!RelativeToPrimary &&
Francois Pichetaf0f4d02011-08-14 03:52:19 +0000101 (Function->getTemplateSpecializationKind() ==
102 TSK_ExplicitSpecialization &&
103 !Function->getClassScopeSpecializationPattern()))
Douglas Gregorfd056bc2009-10-13 16:30:37 +0000104 break;
105
Douglas Gregord1102432009-08-28 17:37:35 +0000106 if (const TemplateArgumentList *TemplateArgs
Douglas Gregorfd056bc2009-10-13 16:30:37 +0000107 = Function->getTemplateSpecializationArgs()) {
108 // Add the template arguments for this specialization.
Douglas Gregord1102432009-08-28 17:37:35 +0000109 Result.addOuterTemplateArguments(TemplateArgs);
John McCallf181d8a2009-08-29 03:16:09 +0000110
Douglas Gregorfd056bc2009-10-13 16:30:37 +0000111 // If this function was instantiated from a specialized member that is
112 // a function template, we're done.
113 assert(Function->getPrimaryTemplate() && "No function template?");
114 if (Function->getPrimaryTemplate()->isMemberSpecialization())
115 break;
Douglas Gregorc494f772011-03-05 17:54:25 +0000116 } else if (FunctionTemplateDecl *FunTmpl
117 = Function->getDescribedFunctionTemplate()) {
118 // Add the "injected" template arguments.
119 std::pair<const TemplateArgument *, unsigned>
120 Injected = FunTmpl->getInjectedTemplateArgs();
121 Result.addOuterTemplateArguments(Injected.first, Injected.second);
Douglas Gregorfd056bc2009-10-13 16:30:37 +0000122 }
123
John McCallf181d8a2009-08-29 03:16:09 +0000124 // If this is a friend declaration and it declares an entity at
125 // namespace scope, take arguments from its lexical parent
Douglas Gregore7089b02010-05-03 23:29:10 +0000126 // instead of its semantic parent, unless of course the pattern we're
127 // instantiating actually comes from the file's context!
John McCallf181d8a2009-08-29 03:16:09 +0000128 if (Function->getFriendObjectKind() &&
Douglas Gregore7089b02010-05-03 23:29:10 +0000129 Function->getDeclContext()->isFileContext() &&
130 (!Pattern || !Pattern->getLexicalDeclContext()->isFileContext())) {
John McCallf181d8a2009-08-29 03:16:09 +0000131 Ctx = Function->getLexicalDeclContext();
Douglas Gregor525f96c2010-02-05 07:33:43 +0000132 RelativeToPrimary = false;
John McCallf181d8a2009-08-29 03:16:09 +0000133 continue;
134 }
Douglas Gregor24bae922010-07-08 18:37:38 +0000135 } else if (CXXRecordDecl *Rec = dyn_cast<CXXRecordDecl>(Ctx)) {
136 if (ClassTemplateDecl *ClassTemplate = Rec->getDescribedClassTemplate()) {
137 QualType T = ClassTemplate->getInjectedClassNameSpecialization();
138 const TemplateSpecializationType *TST
139 = cast<TemplateSpecializationType>(Context.getCanonicalType(T));
140 Result.addOuterTemplateArguments(TST->getArgs(), TST->getNumArgs());
141 if (ClassTemplate->isMemberSpecialization())
142 break;
143 }
Douglas Gregord1102432009-08-28 17:37:35 +0000144 }
John McCallf181d8a2009-08-29 03:16:09 +0000145
146 Ctx = Ctx->getParent();
Douglas Gregor525f96c2010-02-05 07:33:43 +0000147 RelativeToPrimary = false;
Douglas Gregor54dabfc2009-05-14 23:26:13 +0000148 }
Mike Stump1eb44332009-09-09 15:08:12 +0000149
Douglas Gregord1102432009-08-28 17:37:35 +0000150 return Result;
Douglas Gregor54dabfc2009-05-14 23:26:13 +0000151}
152
Douglas Gregorf35f8282009-11-11 21:54:23 +0000153bool Sema::ActiveTemplateInstantiation::isInstantiationRecord() const {
154 switch (Kind) {
155 case TemplateInstantiation:
Richard Smithe6975e92012-04-17 00:58:00 +0000156 case ExceptionSpecInstantiation:
Douglas Gregorf35f8282009-11-11 21:54:23 +0000157 case DefaultTemplateArgumentInstantiation:
158 case DefaultFunctionArgumentInstantiation:
159 return true;
160
161 case ExplicitTemplateArgumentSubstitution:
162 case DeducedTemplateArgumentSubstitution:
163 case PriorTemplateArgumentSubstitution:
164 case DefaultTemplateArgumentChecking:
165 return false;
166 }
David Blaikie7530c032012-01-17 06:56:22 +0000167
168 llvm_unreachable("Invalid InstantiationKind!");
Douglas Gregorf35f8282009-11-11 21:54:23 +0000169}
170
Douglas Gregor26dce442009-03-10 00:06:19 +0000171Sema::InstantiatingTemplate::
172InstantiatingTemplate(Sema &SemaRef, SourceLocation PointOfInstantiation,
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000173 Decl *Entity,
Douglas Gregor26dce442009-03-10 00:06:19 +0000174 SourceRange InstantiationRange)
Douglas Gregor1eee5dc2011-01-27 22:31:44 +0000175 : SemaRef(SemaRef),
176 SavedInNonInstantiationSFINAEContext(
177 SemaRef.InNonInstantiationSFINAEContext)
178{
Douglas Gregordf667e72009-03-10 20:44:00 +0000179 Invalid = CheckInstantiationDepth(PointOfInstantiation,
180 InstantiationRange);
181 if (!Invalid) {
Douglas Gregor26dce442009-03-10 00:06:19 +0000182 ActiveTemplateInstantiation Inst;
Douglas Gregordf667e72009-03-10 20:44:00 +0000183 Inst.Kind = ActiveTemplateInstantiation::TemplateInstantiation;
Douglas Gregor26dce442009-03-10 00:06:19 +0000184 Inst.PointOfInstantiation = PointOfInstantiation;
Douglas Gregordf667e72009-03-10 20:44:00 +0000185 Inst.Entity = reinterpret_cast<uintptr_t>(Entity);
Douglas Gregor313a81d2009-03-12 18:36:18 +0000186 Inst.TemplateArgs = 0;
187 Inst.NumTemplateArgs = 0;
Douglas Gregordf667e72009-03-10 20:44:00 +0000188 Inst.InstantiationRange = InstantiationRange;
Douglas Gregor1eee5dc2011-01-27 22:31:44 +0000189 SemaRef.InNonInstantiationSFINAEContext = false;
Douglas Gregordf667e72009-03-10 20:44:00 +0000190 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
Douglas Gregordf667e72009-03-10 20:44:00 +0000191 }
192}
193
Richard Smithe6975e92012-04-17 00:58:00 +0000194Sema::InstantiatingTemplate::
195InstantiatingTemplate(Sema &SemaRef, SourceLocation PointOfInstantiation,
196 FunctionDecl *Entity, ExceptionSpecification,
197 SourceRange InstantiationRange)
198 : SemaRef(SemaRef),
199 SavedInNonInstantiationSFINAEContext(
200 SemaRef.InNonInstantiationSFINAEContext)
201{
202 Invalid = CheckInstantiationDepth(PointOfInstantiation,
203 InstantiationRange);
204 if (!Invalid) {
205 ActiveTemplateInstantiation Inst;
206 Inst.Kind = ActiveTemplateInstantiation::ExceptionSpecInstantiation;
207 Inst.PointOfInstantiation = PointOfInstantiation;
208 Inst.Entity = reinterpret_cast<uintptr_t>(Entity);
209 Inst.TemplateArgs = 0;
210 Inst.NumTemplateArgs = 0;
211 Inst.InstantiationRange = InstantiationRange;
212 SemaRef.InNonInstantiationSFINAEContext = false;
213 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
214 }
215}
216
Mike Stump1eb44332009-09-09 15:08:12 +0000217Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef,
Douglas Gregordf667e72009-03-10 20:44:00 +0000218 SourceLocation PointOfInstantiation,
219 TemplateDecl *Template,
220 const TemplateArgument *TemplateArgs,
221 unsigned NumTemplateArgs,
222 SourceRange InstantiationRange)
Douglas Gregor1eee5dc2011-01-27 22:31:44 +0000223 : SemaRef(SemaRef),
224 SavedInNonInstantiationSFINAEContext(
225 SemaRef.InNonInstantiationSFINAEContext)
226{
Douglas Gregordf667e72009-03-10 20:44:00 +0000227 Invalid = CheckInstantiationDepth(PointOfInstantiation,
228 InstantiationRange);
229 if (!Invalid) {
230 ActiveTemplateInstantiation Inst;
Mike Stump1eb44332009-09-09 15:08:12 +0000231 Inst.Kind
Douglas Gregordf667e72009-03-10 20:44:00 +0000232 = ActiveTemplateInstantiation::DefaultTemplateArgumentInstantiation;
233 Inst.PointOfInstantiation = PointOfInstantiation;
234 Inst.Entity = reinterpret_cast<uintptr_t>(Template);
235 Inst.TemplateArgs = TemplateArgs;
236 Inst.NumTemplateArgs = NumTemplateArgs;
Douglas Gregor26dce442009-03-10 00:06:19 +0000237 Inst.InstantiationRange = InstantiationRange;
Douglas Gregor1eee5dc2011-01-27 22:31:44 +0000238 SemaRef.InNonInstantiationSFINAEContext = false;
Douglas Gregor26dce442009-03-10 00:06:19 +0000239 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
Douglas Gregor26dce442009-03-10 00:06:19 +0000240 }
241}
242
Mike Stump1eb44332009-09-09 15:08:12 +0000243Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef,
Douglas Gregor637a4092009-06-10 23:47:09 +0000244 SourceLocation PointOfInstantiation,
Douglas Gregorcca9e962009-07-01 22:01:06 +0000245 FunctionTemplateDecl *FunctionTemplate,
246 const TemplateArgument *TemplateArgs,
247 unsigned NumTemplateArgs,
248 ActiveTemplateInstantiation::InstantiationKind Kind,
Douglas Gregor9b623632010-10-12 23:32:35 +0000249 sema::TemplateDeductionInfo &DeductionInfo,
Douglas Gregorcca9e962009-07-01 22:01:06 +0000250 SourceRange InstantiationRange)
Douglas Gregor1eee5dc2011-01-27 22:31:44 +0000251 : SemaRef(SemaRef),
252 SavedInNonInstantiationSFINAEContext(
253 SemaRef.InNonInstantiationSFINAEContext)
254{
Douglas Gregorcca9e962009-07-01 22:01:06 +0000255 Invalid = CheckInstantiationDepth(PointOfInstantiation,
256 InstantiationRange);
257 if (!Invalid) {
258 ActiveTemplateInstantiation Inst;
259 Inst.Kind = Kind;
260 Inst.PointOfInstantiation = PointOfInstantiation;
261 Inst.Entity = reinterpret_cast<uintptr_t>(FunctionTemplate);
262 Inst.TemplateArgs = TemplateArgs;
263 Inst.NumTemplateArgs = NumTemplateArgs;
Douglas Gregor9b623632010-10-12 23:32:35 +0000264 Inst.DeductionInfo = &DeductionInfo;
Douglas Gregorcca9e962009-07-01 22:01:06 +0000265 Inst.InstantiationRange = InstantiationRange;
Douglas Gregor1eee5dc2011-01-27 22:31:44 +0000266 SemaRef.InNonInstantiationSFINAEContext = false;
Douglas Gregorcca9e962009-07-01 22:01:06 +0000267 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
Douglas Gregorf35f8282009-11-11 21:54:23 +0000268
269 if (!Inst.isInstantiationRecord())
270 ++SemaRef.NonInstantiationEntries;
Douglas Gregorcca9e962009-07-01 22:01:06 +0000271 }
272}
273
Mike Stump1eb44332009-09-09 15:08:12 +0000274Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef,
Douglas Gregorcca9e962009-07-01 22:01:06 +0000275 SourceLocation PointOfInstantiation,
Douglas Gregor637a4092009-06-10 23:47:09 +0000276 ClassTemplatePartialSpecializationDecl *PartialSpec,
277 const TemplateArgument *TemplateArgs,
278 unsigned NumTemplateArgs,
Douglas Gregor9b623632010-10-12 23:32:35 +0000279 sema::TemplateDeductionInfo &DeductionInfo,
Douglas Gregor637a4092009-06-10 23:47:09 +0000280 SourceRange InstantiationRange)
Douglas Gregor1eee5dc2011-01-27 22:31:44 +0000281 : SemaRef(SemaRef),
282 SavedInNonInstantiationSFINAEContext(
283 SemaRef.InNonInstantiationSFINAEContext)
284{
Douglas Gregorf35f8282009-11-11 21:54:23 +0000285 Invalid = false;
286
287 ActiveTemplateInstantiation Inst;
288 Inst.Kind = ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution;
289 Inst.PointOfInstantiation = PointOfInstantiation;
290 Inst.Entity = reinterpret_cast<uintptr_t>(PartialSpec);
291 Inst.TemplateArgs = TemplateArgs;
292 Inst.NumTemplateArgs = NumTemplateArgs;
Douglas Gregor9b623632010-10-12 23:32:35 +0000293 Inst.DeductionInfo = &DeductionInfo;
Douglas Gregorf35f8282009-11-11 21:54:23 +0000294 Inst.InstantiationRange = InstantiationRange;
Douglas Gregor1eee5dc2011-01-27 22:31:44 +0000295 SemaRef.InNonInstantiationSFINAEContext = false;
Douglas Gregorf35f8282009-11-11 21:54:23 +0000296 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
297
298 assert(!Inst.isInstantiationRecord());
299 ++SemaRef.NonInstantiationEntries;
Douglas Gregor637a4092009-06-10 23:47:09 +0000300}
301
Mike Stump1eb44332009-09-09 15:08:12 +0000302Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef,
Douglas Gregor9148c3f2009-11-11 19:13:48 +0000303 SourceLocation PointOfInstantiation,
Anders Carlsson25cae7f2009-09-05 05:14:19 +0000304 ParmVarDecl *Param,
305 const TemplateArgument *TemplateArgs,
306 unsigned NumTemplateArgs,
307 SourceRange InstantiationRange)
Douglas Gregor1eee5dc2011-01-27 22:31:44 +0000308 : SemaRef(SemaRef),
309 SavedInNonInstantiationSFINAEContext(
310 SemaRef.InNonInstantiationSFINAEContext)
311{
Douglas Gregor9148c3f2009-11-11 19:13:48 +0000312 Invalid = CheckInstantiationDepth(PointOfInstantiation, InstantiationRange);
Anders Carlsson25cae7f2009-09-05 05:14:19 +0000313
314 if (!Invalid) {
315 ActiveTemplateInstantiation Inst;
316 Inst.Kind
317 = ActiveTemplateInstantiation::DefaultFunctionArgumentInstantiation;
Douglas Gregor9148c3f2009-11-11 19:13:48 +0000318 Inst.PointOfInstantiation = PointOfInstantiation;
Anders Carlsson25cae7f2009-09-05 05:14:19 +0000319 Inst.Entity = reinterpret_cast<uintptr_t>(Param);
320 Inst.TemplateArgs = TemplateArgs;
321 Inst.NumTemplateArgs = NumTemplateArgs;
322 Inst.InstantiationRange = InstantiationRange;
Douglas Gregor1eee5dc2011-01-27 22:31:44 +0000323 SemaRef.InNonInstantiationSFINAEContext = false;
Anders Carlsson25cae7f2009-09-05 05:14:19 +0000324 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
Douglas Gregor9148c3f2009-11-11 19:13:48 +0000325 }
326}
327
328Sema::InstantiatingTemplate::
329InstantiatingTemplate(Sema &SemaRef, SourceLocation PointOfInstantiation,
Douglas Gregor54c53cc2011-01-04 23:35:54 +0000330 NamedDecl *Template,
Douglas Gregor9148c3f2009-11-11 19:13:48 +0000331 NonTypeTemplateParmDecl *Param,
332 const TemplateArgument *TemplateArgs,
333 unsigned NumTemplateArgs,
Douglas Gregor1eee5dc2011-01-27 22:31:44 +0000334 SourceRange InstantiationRange)
335 : SemaRef(SemaRef),
336 SavedInNonInstantiationSFINAEContext(
337 SemaRef.InNonInstantiationSFINAEContext)
338{
Douglas Gregorf35f8282009-11-11 21:54:23 +0000339 Invalid = false;
Douglas Gregor9148c3f2009-11-11 19:13:48 +0000340
Douglas Gregorf35f8282009-11-11 21:54:23 +0000341 ActiveTemplateInstantiation Inst;
342 Inst.Kind = ActiveTemplateInstantiation::PriorTemplateArgumentSubstitution;
343 Inst.PointOfInstantiation = PointOfInstantiation;
344 Inst.Template = Template;
345 Inst.Entity = reinterpret_cast<uintptr_t>(Param);
346 Inst.TemplateArgs = TemplateArgs;
347 Inst.NumTemplateArgs = NumTemplateArgs;
348 Inst.InstantiationRange = InstantiationRange;
Douglas Gregor1eee5dc2011-01-27 22:31:44 +0000349 SemaRef.InNonInstantiationSFINAEContext = false;
Douglas Gregorf35f8282009-11-11 21:54:23 +0000350 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
351
352 assert(!Inst.isInstantiationRecord());
353 ++SemaRef.NonInstantiationEntries;
Douglas Gregor9148c3f2009-11-11 19:13:48 +0000354}
355
356Sema::InstantiatingTemplate::
357InstantiatingTemplate(Sema &SemaRef, SourceLocation PointOfInstantiation,
Douglas Gregor54c53cc2011-01-04 23:35:54 +0000358 NamedDecl *Template,
Douglas Gregor9148c3f2009-11-11 19:13:48 +0000359 TemplateTemplateParmDecl *Param,
360 const TemplateArgument *TemplateArgs,
361 unsigned NumTemplateArgs,
Douglas Gregor1eee5dc2011-01-27 22:31:44 +0000362 SourceRange InstantiationRange)
363 : SemaRef(SemaRef),
364 SavedInNonInstantiationSFINAEContext(
365 SemaRef.InNonInstantiationSFINAEContext)
366{
Douglas Gregorf35f8282009-11-11 21:54:23 +0000367 Invalid = false;
368 ActiveTemplateInstantiation Inst;
369 Inst.Kind = ActiveTemplateInstantiation::PriorTemplateArgumentSubstitution;
370 Inst.PointOfInstantiation = PointOfInstantiation;
371 Inst.Template = Template;
372 Inst.Entity = reinterpret_cast<uintptr_t>(Param);
373 Inst.TemplateArgs = TemplateArgs;
374 Inst.NumTemplateArgs = NumTemplateArgs;
375 Inst.InstantiationRange = InstantiationRange;
Douglas Gregor1eee5dc2011-01-27 22:31:44 +0000376 SemaRef.InNonInstantiationSFINAEContext = false;
Douglas Gregorf35f8282009-11-11 21:54:23 +0000377 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
Douglas Gregor9148c3f2009-11-11 19:13:48 +0000378
Douglas Gregorf35f8282009-11-11 21:54:23 +0000379 assert(!Inst.isInstantiationRecord());
380 ++SemaRef.NonInstantiationEntries;
381}
382
383Sema::InstantiatingTemplate::
384InstantiatingTemplate(Sema &SemaRef, SourceLocation PointOfInstantiation,
385 TemplateDecl *Template,
386 NamedDecl *Param,
387 const TemplateArgument *TemplateArgs,
388 unsigned NumTemplateArgs,
Douglas Gregor1eee5dc2011-01-27 22:31:44 +0000389 SourceRange InstantiationRange)
390 : SemaRef(SemaRef),
391 SavedInNonInstantiationSFINAEContext(
392 SemaRef.InNonInstantiationSFINAEContext)
393{
Douglas Gregorf35f8282009-11-11 21:54:23 +0000394 Invalid = false;
395
396 ActiveTemplateInstantiation Inst;
397 Inst.Kind = ActiveTemplateInstantiation::DefaultTemplateArgumentChecking;
398 Inst.PointOfInstantiation = PointOfInstantiation;
399 Inst.Template = Template;
400 Inst.Entity = reinterpret_cast<uintptr_t>(Param);
401 Inst.TemplateArgs = TemplateArgs;
402 Inst.NumTemplateArgs = NumTemplateArgs;
403 Inst.InstantiationRange = InstantiationRange;
Douglas Gregor1eee5dc2011-01-27 22:31:44 +0000404 SemaRef.InNonInstantiationSFINAEContext = false;
Douglas Gregorf35f8282009-11-11 21:54:23 +0000405 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
406
407 assert(!Inst.isInstantiationRecord());
408 ++SemaRef.NonInstantiationEntries;
Anders Carlsson25cae7f2009-09-05 05:14:19 +0000409}
410
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000411void Sema::InstantiatingTemplate::Clear() {
412 if (!Invalid) {
Douglas Gregorf35f8282009-11-11 21:54:23 +0000413 if (!SemaRef.ActiveTemplateInstantiations.back().isInstantiationRecord()) {
414 assert(SemaRef.NonInstantiationEntries > 0);
415 --SemaRef.NonInstantiationEntries;
416 }
Douglas Gregor1eee5dc2011-01-27 22:31:44 +0000417 SemaRef.InNonInstantiationSFINAEContext
418 = SavedInNonInstantiationSFINAEContext;
Douglas Gregor26dce442009-03-10 00:06:19 +0000419 SemaRef.ActiveTemplateInstantiations.pop_back();
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000420 Invalid = true;
421 }
Douglas Gregor26dce442009-03-10 00:06:19 +0000422}
423
Douglas Gregordf667e72009-03-10 20:44:00 +0000424bool Sema::InstantiatingTemplate::CheckInstantiationDepth(
425 SourceLocation PointOfInstantiation,
426 SourceRange InstantiationRange) {
Douglas Gregorf35f8282009-11-11 21:54:23 +0000427 assert(SemaRef.NonInstantiationEntries <=
428 SemaRef.ActiveTemplateInstantiations.size());
429 if ((SemaRef.ActiveTemplateInstantiations.size() -
430 SemaRef.NonInstantiationEntries)
David Blaikie4e4d0842012-03-11 07:00:24 +0000431 <= SemaRef.getLangOpts().InstantiationDepth)
Douglas Gregordf667e72009-03-10 20:44:00 +0000432 return false;
433
Mike Stump1eb44332009-09-09 15:08:12 +0000434 SemaRef.Diag(PointOfInstantiation,
Douglas Gregordf667e72009-03-10 20:44:00 +0000435 diag::err_template_recursion_depth_exceeded)
David Blaikie4e4d0842012-03-11 07:00:24 +0000436 << SemaRef.getLangOpts().InstantiationDepth
Douglas Gregordf667e72009-03-10 20:44:00 +0000437 << InstantiationRange;
438 SemaRef.Diag(PointOfInstantiation, diag::note_template_recursion_depth)
David Blaikie4e4d0842012-03-11 07:00:24 +0000439 << SemaRef.getLangOpts().InstantiationDepth;
Douglas Gregordf667e72009-03-10 20:44:00 +0000440 return true;
441}
442
Douglas Gregoree1828a2009-03-10 18:03:33 +0000443/// \brief Prints the current instantiation stack through a series of
444/// notes.
445void Sema::PrintInstantiationStack() {
Douglas Gregor575cf372010-04-20 07:18:24 +0000446 // Determine which template instantiations to skip, if any.
447 unsigned SkipStart = ActiveTemplateInstantiations.size(), SkipEnd = SkipStart;
448 unsigned Limit = Diags.getTemplateBacktraceLimit();
449 if (Limit && Limit < ActiveTemplateInstantiations.size()) {
450 SkipStart = Limit / 2 + Limit % 2;
451 SkipEnd = ActiveTemplateInstantiations.size() - Limit / 2;
452 }
453
Douglas Gregorcca9e962009-07-01 22:01:06 +0000454 // FIXME: In all of these cases, we need to show the template arguments
Douglas Gregor575cf372010-04-20 07:18:24 +0000455 unsigned InstantiationIdx = 0;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000456 for (SmallVector<ActiveTemplateInstantiation, 16>::reverse_iterator
Douglas Gregoree1828a2009-03-10 18:03:33 +0000457 Active = ActiveTemplateInstantiations.rbegin(),
458 ActiveEnd = ActiveTemplateInstantiations.rend();
459 Active != ActiveEnd;
Douglas Gregor575cf372010-04-20 07:18:24 +0000460 ++Active, ++InstantiationIdx) {
461 // Skip this instantiation?
462 if (InstantiationIdx >= SkipStart && InstantiationIdx < SkipEnd) {
463 if (InstantiationIdx == SkipStart) {
464 // Note that we're skipping instantiations.
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000465 Diags.Report(Active->PointOfInstantiation,
Douglas Gregor575cf372010-04-20 07:18:24 +0000466 diag::note_instantiation_contexts_suppressed)
467 << unsigned(ActiveTemplateInstantiations.size() - Limit);
468 }
469 continue;
470 }
471
Douglas Gregordf667e72009-03-10 20:44:00 +0000472 switch (Active->Kind) {
473 case ActiveTemplateInstantiation::TemplateInstantiation: {
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000474 Decl *D = reinterpret_cast<Decl *>(Active->Entity);
475 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) {
476 unsigned DiagID = diag::note_template_member_class_here;
477 if (isa<ClassTemplateSpecializationDecl>(Record))
478 DiagID = diag::note_template_class_instantiation_here;
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000479 Diags.Report(Active->PointOfInstantiation, DiagID)
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000480 << Context.getTypeDeclType(Record)
481 << Active->InstantiationRange;
Douglas Gregor7caa6822009-07-24 20:34:43 +0000482 } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
Douglas Gregor1637be72009-06-26 00:10:03 +0000483 unsigned DiagID;
484 if (Function->getPrimaryTemplate())
485 DiagID = diag::note_function_template_spec_here;
486 else
487 DiagID = diag::note_template_member_function_here;
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000488 Diags.Report(Active->PointOfInstantiation, DiagID)
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000489 << Function
490 << Active->InstantiationRange;
Richard Smith3e4c6c42011-05-05 21:57:07 +0000491 } else if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000492 Diags.Report(Active->PointOfInstantiation,
Douglas Gregor7caa6822009-07-24 20:34:43 +0000493 diag::note_template_static_data_member_def_here)
Richard Smith3e4c6c42011-05-05 21:57:07 +0000494 << VD
495 << Active->InstantiationRange;
Richard Smithf1c66b42012-03-14 23:13:10 +0000496 } else if (EnumDecl *ED = dyn_cast<EnumDecl>(D)) {
497 Diags.Report(Active->PointOfInstantiation,
498 diag::note_template_enum_def_here)
499 << ED
500 << Active->InstantiationRange;
Richard Smith3e4c6c42011-05-05 21:57:07 +0000501 } else {
502 Diags.Report(Active->PointOfInstantiation,
503 diag::note_template_type_alias_instantiation_here)
504 << cast<TypeAliasTemplateDecl>(D)
Douglas Gregor7caa6822009-07-24 20:34:43 +0000505 << Active->InstantiationRange;
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000506 }
Douglas Gregordf667e72009-03-10 20:44:00 +0000507 break;
508 }
509
510 case ActiveTemplateInstantiation::DefaultTemplateArgumentInstantiation: {
511 TemplateDecl *Template = cast<TemplateDecl>((Decl *)Active->Entity);
512 std::string TemplateArgsStr
Douglas Gregor7532dc62009-03-30 22:58:21 +0000513 = TemplateSpecializationType::PrintTemplateArgumentList(
Mike Stump1eb44332009-09-09 15:08:12 +0000514 Active->TemplateArgs,
Douglas Gregord249e1d1f2009-05-29 20:38:28 +0000515 Active->NumTemplateArgs,
Douglas Gregor8987b232011-09-27 23:30:47 +0000516 getPrintingPolicy());
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000517 Diags.Report(Active->PointOfInstantiation,
Douglas Gregordf667e72009-03-10 20:44:00 +0000518 diag::note_default_arg_instantiation_here)
519 << (Template->getNameAsString() + TemplateArgsStr)
520 << Active->InstantiationRange;
521 break;
522 }
Douglas Gregor637a4092009-06-10 23:47:09 +0000523
Douglas Gregorcca9e962009-07-01 22:01:06 +0000524 case ActiveTemplateInstantiation::ExplicitTemplateArgumentSubstitution: {
Mike Stump1eb44332009-09-09 15:08:12 +0000525 FunctionTemplateDecl *FnTmpl
Douglas Gregorcca9e962009-07-01 22:01:06 +0000526 = cast<FunctionTemplateDecl>((Decl *)Active->Entity);
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000527 Diags.Report(Active->PointOfInstantiation,
Douglas Gregorcca9e962009-07-01 22:01:06 +0000528 diag::note_explicit_template_arg_substitution_here)
Douglas Gregor5e402912010-03-30 20:35:20 +0000529 << FnTmpl
530 << getTemplateArgumentBindingsText(FnTmpl->getTemplateParameters(),
531 Active->TemplateArgs,
532 Active->NumTemplateArgs)
533 << Active->InstantiationRange;
Douglas Gregor637a4092009-06-10 23:47:09 +0000534 break;
535 }
Mike Stump1eb44332009-09-09 15:08:12 +0000536
Douglas Gregorcca9e962009-07-01 22:01:06 +0000537 case ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution:
538 if (ClassTemplatePartialSpecializationDecl *PartialSpec
539 = dyn_cast<ClassTemplatePartialSpecializationDecl>(
540 (Decl *)Active->Entity)) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000541 Diags.Report(Active->PointOfInstantiation,
Douglas Gregorcca9e962009-07-01 22:01:06 +0000542 diag::note_partial_spec_deduct_instantiation_here)
543 << Context.getTypeDeclType(PartialSpec)
Douglas Gregor5e402912010-03-30 20:35:20 +0000544 << getTemplateArgumentBindingsText(
545 PartialSpec->getTemplateParameters(),
546 Active->TemplateArgs,
547 Active->NumTemplateArgs)
Douglas Gregorcca9e962009-07-01 22:01:06 +0000548 << Active->InstantiationRange;
549 } else {
550 FunctionTemplateDecl *FnTmpl
551 = cast<FunctionTemplateDecl>((Decl *)Active->Entity);
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000552 Diags.Report(Active->PointOfInstantiation,
Douglas Gregorcca9e962009-07-01 22:01:06 +0000553 diag::note_function_template_deduction_instantiation_here)
Douglas Gregor5e402912010-03-30 20:35:20 +0000554 << FnTmpl
555 << getTemplateArgumentBindingsText(FnTmpl->getTemplateParameters(),
556 Active->TemplateArgs,
557 Active->NumTemplateArgs)
558 << Active->InstantiationRange;
Douglas Gregorcca9e962009-07-01 22:01:06 +0000559 }
560 break;
Douglas Gregor637a4092009-06-10 23:47:09 +0000561
Anders Carlsson25cae7f2009-09-05 05:14:19 +0000562 case ActiveTemplateInstantiation::DefaultFunctionArgumentInstantiation: {
563 ParmVarDecl *Param = cast<ParmVarDecl>((Decl *)Active->Entity);
564 FunctionDecl *FD = cast<FunctionDecl>(Param->getDeclContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000565
Anders Carlsson25cae7f2009-09-05 05:14:19 +0000566 std::string TemplateArgsStr
567 = TemplateSpecializationType::PrintTemplateArgumentList(
Mike Stump1eb44332009-09-09 15:08:12 +0000568 Active->TemplateArgs,
Anders Carlsson25cae7f2009-09-05 05:14:19 +0000569 Active->NumTemplateArgs,
Douglas Gregor8987b232011-09-27 23:30:47 +0000570 getPrintingPolicy());
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000571 Diags.Report(Active->PointOfInstantiation,
Anders Carlsson25cae7f2009-09-05 05:14:19 +0000572 diag::note_default_function_arg_instantiation_here)
Anders Carlsson6bc107b2009-09-05 05:38:54 +0000573 << (FD->getNameAsString() + TemplateArgsStr)
Anders Carlsson25cae7f2009-09-05 05:14:19 +0000574 << Active->InstantiationRange;
575 break;
576 }
Mike Stump1eb44332009-09-09 15:08:12 +0000577
Douglas Gregor9148c3f2009-11-11 19:13:48 +0000578 case ActiveTemplateInstantiation::PriorTemplateArgumentSubstitution: {
579 NamedDecl *Parm = cast<NamedDecl>((Decl *)Active->Entity);
580 std::string Name;
581 if (!Parm->getName().empty())
582 Name = std::string(" '") + Parm->getName().str() + "'";
Douglas Gregor54c53cc2011-01-04 23:35:54 +0000583
584 TemplateParameterList *TemplateParams = 0;
585 if (TemplateDecl *Template = dyn_cast<TemplateDecl>(Active->Template))
586 TemplateParams = Template->getTemplateParameters();
587 else
588 TemplateParams =
589 cast<ClassTemplatePartialSpecializationDecl>(Active->Template)
590 ->getTemplateParameters();
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000591 Diags.Report(Active->PointOfInstantiation,
Douglas Gregor9148c3f2009-11-11 19:13:48 +0000592 diag::note_prior_template_arg_substitution)
593 << isa<TemplateTemplateParmDecl>(Parm)
594 << Name
Douglas Gregor54c53cc2011-01-04 23:35:54 +0000595 << getTemplateArgumentBindingsText(TemplateParams,
Douglas Gregor9148c3f2009-11-11 19:13:48 +0000596 Active->TemplateArgs,
597 Active->NumTemplateArgs)
598 << Active->InstantiationRange;
599 break;
600 }
Douglas Gregorf35f8282009-11-11 21:54:23 +0000601
602 case ActiveTemplateInstantiation::DefaultTemplateArgumentChecking: {
Douglas Gregor54c53cc2011-01-04 23:35:54 +0000603 TemplateParameterList *TemplateParams = 0;
604 if (TemplateDecl *Template = dyn_cast<TemplateDecl>(Active->Template))
605 TemplateParams = Template->getTemplateParameters();
606 else
607 TemplateParams =
608 cast<ClassTemplatePartialSpecializationDecl>(Active->Template)
609 ->getTemplateParameters();
610
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000611 Diags.Report(Active->PointOfInstantiation,
Douglas Gregorf35f8282009-11-11 21:54:23 +0000612 diag::note_template_default_arg_checking)
Douglas Gregor54c53cc2011-01-04 23:35:54 +0000613 << getTemplateArgumentBindingsText(TemplateParams,
Douglas Gregorf35f8282009-11-11 21:54:23 +0000614 Active->TemplateArgs,
615 Active->NumTemplateArgs)
616 << Active->InstantiationRange;
617 break;
618 }
Richard Smithe6975e92012-04-17 00:58:00 +0000619
620 case ActiveTemplateInstantiation::ExceptionSpecInstantiation:
621 Diags.Report(Active->PointOfInstantiation,
622 diag::note_template_exception_spec_instantiation_here)
623 << cast<FunctionDecl>((Decl *)Active->Entity)
624 << Active->InstantiationRange;
625 break;
Douglas Gregordf667e72009-03-10 20:44:00 +0000626 }
Douglas Gregoree1828a2009-03-10 18:03:33 +0000627 }
628}
629
Douglas Gregor1eee5dc2011-01-27 22:31:44 +0000630llvm::Optional<TemplateDeductionInfo *> Sema::isSFINAEContext() const {
Douglas Gregor1eee5dc2011-01-27 22:31:44 +0000631 if (InNonInstantiationSFINAEContext)
632 return llvm::Optional<TemplateDeductionInfo *>(0);
633
Douglas Gregor5e9f35c2009-06-14 07:33:30 +0000634 for (SmallVector<ActiveTemplateInstantiation, 16>::const_reverse_iterator
635 Active = ActiveTemplateInstantiations.rbegin(),
636 ActiveEnd = ActiveTemplateInstantiations.rend();
637 Active != ActiveEnd;
Douglas Gregorf35f8282009-11-11 21:54:23 +0000638 ++Active)
639 {
Douglas Gregor5e9f35c2009-06-14 07:33:30 +0000640 switch(Active->Kind) {
Douglas Gregor1eee5dc2011-01-27 22:31:44 +0000641 case ActiveTemplateInstantiation::TemplateInstantiation:
Richard Smitha43ea642012-04-26 07:24:08 +0000642 // An instantiation of an alias template may or may not be a SFINAE
643 // context, depending on what else is on the stack.
644 if (isa<TypeAliasTemplateDecl>(reinterpret_cast<Decl *>(Active->Entity)))
645 break;
646 // Fall through.
647 case ActiveTemplateInstantiation::DefaultFunctionArgumentInstantiation:
Richard Smithe6975e92012-04-17 00:58:00 +0000648 case ActiveTemplateInstantiation::ExceptionSpecInstantiation:
Douglas Gregorcca9e962009-07-01 22:01:06 +0000649 // This is a template instantiation, so there is no SFINAE.
Douglas Gregor1eee5dc2011-01-27 22:31:44 +0000650 return llvm::Optional<TemplateDeductionInfo *>();
Mike Stump1eb44332009-09-09 15:08:12 +0000651
Douglas Gregor5e9f35c2009-06-14 07:33:30 +0000652 case ActiveTemplateInstantiation::DefaultTemplateArgumentInstantiation:
Douglas Gregor9148c3f2009-11-11 19:13:48 +0000653 case ActiveTemplateInstantiation::PriorTemplateArgumentSubstitution:
Douglas Gregorf35f8282009-11-11 21:54:23 +0000654 case ActiveTemplateInstantiation::DefaultTemplateArgumentChecking:
Douglas Gregor9148c3f2009-11-11 19:13:48 +0000655 // A default template argument instantiation and substitution into
656 // template parameters with arguments for prior parameters may or may
657 // not be a SFINAE context; look further up the stack.
Douglas Gregor5e9f35c2009-06-14 07:33:30 +0000658 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000659
Douglas Gregorcca9e962009-07-01 22:01:06 +0000660 case ActiveTemplateInstantiation::ExplicitTemplateArgumentSubstitution:
661 case ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution:
662 // We're either substitution explicitly-specified template arguments
663 // or deduced template arguments, so SFINAE applies.
Douglas Gregor9b623632010-10-12 23:32:35 +0000664 assert(Active->DeductionInfo && "Missing deduction info pointer");
665 return Active->DeductionInfo;
Douglas Gregor5e9f35c2009-06-14 07:33:30 +0000666 }
667 }
668
Douglas Gregor1eee5dc2011-01-27 22:31:44 +0000669 return llvm::Optional<TemplateDeductionInfo *>();
Douglas Gregor5e9f35c2009-06-14 07:33:30 +0000670}
671
Douglas Gregord3731192011-01-10 07:32:04 +0000672/// \brief Retrieve the depth and index of a parameter pack.
673static std::pair<unsigned, unsigned>
674getDepthAndIndex(NamedDecl *ND) {
675 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(ND))
676 return std::make_pair(TTP->getDepth(), TTP->getIndex());
677
678 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(ND))
679 return std::make_pair(NTTP->getDepth(), NTTP->getIndex());
680
681 TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(ND);
682 return std::make_pair(TTP->getDepth(), TTP->getIndex());
683}
684
Douglas Gregor99ebf652009-02-27 19:31:52 +0000685//===----------------------------------------------------------------------===/
686// Template Instantiation for Types
687//===----------------------------------------------------------------------===/
Douglas Gregorcd281c32009-02-28 00:25:32 +0000688namespace {
Douglas Gregor895162d2010-04-30 18:55:50 +0000689 class TemplateInstantiator : public TreeTransform<TemplateInstantiator> {
Douglas Gregord6350ae2009-08-28 20:31:08 +0000690 const MultiLevelTemplateArgumentList &TemplateArgs;
Douglas Gregorcd281c32009-02-28 00:25:32 +0000691 SourceLocation Loc;
692 DeclarationName Entity;
Douglas Gregor99ebf652009-02-27 19:31:52 +0000693
Douglas Gregorcd281c32009-02-28 00:25:32 +0000694 public:
Douglas Gregor43959a92009-08-20 07:17:43 +0000695 typedef TreeTransform<TemplateInstantiator> inherited;
Mike Stump1eb44332009-09-09 15:08:12 +0000696
697 TemplateInstantiator(Sema &SemaRef,
Douglas Gregord6350ae2009-08-28 20:31:08 +0000698 const MultiLevelTemplateArgumentList &TemplateArgs,
Douglas Gregor577f75a2009-08-04 16:50:30 +0000699 SourceLocation Loc,
Mike Stump1eb44332009-09-09 15:08:12 +0000700 DeclarationName Entity)
701 : inherited(SemaRef), TemplateArgs(TemplateArgs), Loc(Loc),
Douglas Gregor43959a92009-08-20 07:17:43 +0000702 Entity(Entity) { }
Douglas Gregorcd281c32009-02-28 00:25:32 +0000703
Mike Stump1eb44332009-09-09 15:08:12 +0000704 /// \brief Determine whether the given type \p T has already been
Douglas Gregor577f75a2009-08-04 16:50:30 +0000705 /// transformed.
706 ///
707 /// For the purposes of template instantiation, a type has already been
708 /// transformed if it is NULL or if it is not dependent.
Douglas Gregorb4eeaff2010-05-07 23:12:07 +0000709 bool AlreadyTransformed(QualType T);
Mike Stump1eb44332009-09-09 15:08:12 +0000710
Douglas Gregor577f75a2009-08-04 16:50:30 +0000711 /// \brief Returns the location of the entity being instantiated, if known.
712 SourceLocation getBaseLocation() { return Loc; }
Mike Stump1eb44332009-09-09 15:08:12 +0000713
Douglas Gregor577f75a2009-08-04 16:50:30 +0000714 /// \brief Returns the name of the entity being instantiated, if any.
715 DeclarationName getBaseEntity() { return Entity; }
Mike Stump1eb44332009-09-09 15:08:12 +0000716
Douglas Gregor972e6ce2009-10-27 06:26:26 +0000717 /// \brief Sets the "base" location and entity when that
718 /// information is known based on another transformation.
719 void setBase(SourceLocation Loc, DeclarationName Entity) {
720 this->Loc = Loc;
721 this->Entity = Entity;
722 }
Douglas Gregor8491ffe2010-12-20 22:05:00 +0000723
724 bool TryExpandParameterPacks(SourceLocation EllipsisLoc,
725 SourceRange PatternRange,
David Blaikiea71f9d02011-09-22 02:34:54 +0000726 llvm::ArrayRef<UnexpandedParameterPack> Unexpanded,
Douglas Gregor8491ffe2010-12-20 22:05:00 +0000727 bool &ShouldExpand,
Douglas Gregord3731192011-01-10 07:32:04 +0000728 bool &RetainExpansion,
Douglas Gregorcded4f62011-01-14 17:04:44 +0000729 llvm::Optional<unsigned> &NumExpansions) {
Douglas Gregorb99268b2010-12-21 00:52:54 +0000730 return getSema().CheckParameterPacksForExpansion(EllipsisLoc,
731 PatternRange, Unexpanded,
Douglas Gregorb99268b2010-12-21 00:52:54 +0000732 TemplateArgs,
733 ShouldExpand,
Douglas Gregord3731192011-01-10 07:32:04 +0000734 RetainExpansion,
Douglas Gregorb99268b2010-12-21 00:52:54 +0000735 NumExpansions);
736 }
Douglas Gregor8491ffe2010-12-20 22:05:00 +0000737
Douglas Gregor12c9c002011-01-07 16:43:16 +0000738 void ExpandingFunctionParameterPack(ParmVarDecl *Pack) {
739 SemaRef.CurrentInstantiationScope->MakeInstantiatedLocalArgPack(Pack);
740 }
741
Douglas Gregord3731192011-01-10 07:32:04 +0000742 TemplateArgument ForgetPartiallySubstitutedPack() {
743 TemplateArgument Result;
744 if (NamedDecl *PartialPack
745 = SemaRef.CurrentInstantiationScope->getPartiallySubstitutedPack()){
746 MultiLevelTemplateArgumentList &TemplateArgs
747 = const_cast<MultiLevelTemplateArgumentList &>(this->TemplateArgs);
748 unsigned Depth, Index;
749 llvm::tie(Depth, Index) = getDepthAndIndex(PartialPack);
750 if (TemplateArgs.hasTemplateArgument(Depth, Index)) {
751 Result = TemplateArgs(Depth, Index);
752 TemplateArgs.setArgument(Depth, Index, TemplateArgument());
753 }
754 }
755
756 return Result;
757 }
758
759 void RememberPartiallySubstitutedPack(TemplateArgument Arg) {
760 if (Arg.isNull())
761 return;
762
763 if (NamedDecl *PartialPack
764 = SemaRef.CurrentInstantiationScope->getPartiallySubstitutedPack()){
765 MultiLevelTemplateArgumentList &TemplateArgs
766 = const_cast<MultiLevelTemplateArgumentList &>(this->TemplateArgs);
767 unsigned Depth, Index;
768 llvm::tie(Depth, Index) = getDepthAndIndex(PartialPack);
769 TemplateArgs.setArgument(Depth, Index, Arg);
770 }
771 }
772
Douglas Gregor577f75a2009-08-04 16:50:30 +0000773 /// \brief Transform the given declaration by instantiating a reference to
774 /// this declaration.
Douglas Gregor7c1e98f2010-03-01 15:56:25 +0000775 Decl *TransformDecl(SourceLocation Loc, Decl *D);
Douglas Gregorb98b1992009-08-11 05:31:07 +0000776
Douglas Gregordfca6f52012-02-13 22:00:16 +0000777 void transformAttrs(Decl *Old, Decl *New) {
778 SemaRef.InstantiateAttrs(TemplateArgs, Old, New);
779 }
780
781 void transformedLocalDecl(Decl *Old, Decl *New) {
782 SemaRef.CurrentInstantiationScope->InstantiatedLocal(Old, New);
783 }
784
Mike Stump1eb44332009-09-09 15:08:12 +0000785 /// \brief Transform the definition of the given declaration by
Douglas Gregor43959a92009-08-20 07:17:43 +0000786 /// instantiating it.
Douglas Gregoraac571c2010-03-01 17:25:41 +0000787 Decl *TransformDefinition(SourceLocation Loc, Decl *D);
Mike Stump1eb44332009-09-09 15:08:12 +0000788
Douglas Gregor6cd21982009-10-20 05:58:46 +0000789 /// \bried Transform the first qualifier within a scope by instantiating the
790 /// declaration.
791 NamedDecl *TransformFirstQualifierInScope(NamedDecl *D, SourceLocation Loc);
792
Douglas Gregor43959a92009-08-20 07:17:43 +0000793 /// \brief Rebuild the exception declaration and register the declaration
794 /// as an instantiated local.
Douglas Gregor83cb9422010-09-09 17:09:21 +0000795 VarDecl *RebuildExceptionDecl(VarDecl *ExceptionDecl,
John McCalla93c9342009-12-07 02:54:59 +0000796 TypeSourceInfo *Declarator,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +0000797 SourceLocation StartLoc,
798 SourceLocation NameLoc,
799 IdentifierInfo *Name);
Mike Stump1eb44332009-09-09 15:08:12 +0000800
Douglas Gregorbe270a02010-04-26 17:57:08 +0000801 /// \brief Rebuild the Objective-C exception declaration and register the
802 /// declaration as an instantiated local.
803 VarDecl *RebuildObjCExceptionDecl(VarDecl *ExceptionDecl,
804 TypeSourceInfo *TSInfo, QualType T);
805
John McCallc4e70192009-09-11 04:59:25 +0000806 /// \brief Check for tag mismatches when instantiating an
807 /// elaborated type.
John McCall21e413f2010-11-04 19:04:38 +0000808 QualType RebuildElaboratedType(SourceLocation KeywordLoc,
809 ElaboratedTypeKeyword Keyword,
Douglas Gregor9e876872011-03-01 18:12:44 +0000810 NestedNameSpecifierLoc QualifierLoc,
811 QualType T);
John McCallc4e70192009-09-11 04:59:25 +0000812
Douglas Gregorfd4ffeb2011-03-02 18:07:45 +0000813 TemplateName TransformTemplateName(CXXScopeSpec &SS,
814 TemplateName Name,
815 SourceLocation NameLoc,
816 QualType ObjectType = QualType(),
817 NamedDecl *FirstQualifierInScope = 0);
818
John McCall60d7b3a2010-08-24 06:29:42 +0000819 ExprResult TransformPredefinedExpr(PredefinedExpr *E);
820 ExprResult TransformDeclRefExpr(DeclRefExpr *E);
821 ExprResult TransformCXXDefaultArgExpr(CXXDefaultArgExpr *E);
822 ExprResult TransformTemplateParmRefExpr(DeclRefExpr *E,
Douglas Gregor56bc9832010-12-24 00:15:10 +0000823 NonTypeTemplateParmDecl *D);
Douglas Gregorc7793c72011-01-15 01:15:58 +0000824 ExprResult TransformSubstNonTypeTemplateParmPackExpr(
825 SubstNonTypeTemplateParmPackExpr *E);
826
Douglas Gregor895162d2010-04-30 18:55:50 +0000827 QualType TransformFunctionProtoType(TypeLocBuilder &TLB,
John McCall43fed0d2010-11-12 08:19:04 +0000828 FunctionProtoTypeLoc TL);
Douglas Gregorcefc3af2012-04-16 07:05:22 +0000829 QualType TransformFunctionProtoType(TypeLocBuilder &TLB,
830 FunctionProtoTypeLoc TL,
831 CXXRecordDecl *ThisContext,
832 unsigned ThisTypeQuals);
833
Douglas Gregor6a24bfd2011-01-14 22:40:04 +0000834 ParmVarDecl *TransformFunctionTypeParam(ParmVarDecl *OldParm,
John McCallfb44de92011-05-01 22:35:37 +0000835 int indexAdjustment,
Douglas Gregord1bb4ae2012-01-25 16:15:54 +0000836 llvm::Optional<unsigned> NumExpansions,
837 bool ExpectParameterPack);
John McCall21ef0fa2010-03-11 09:03:00 +0000838
Mike Stump1eb44332009-09-09 15:08:12 +0000839 /// \brief Transforms a template type parameter type by performing
Douglas Gregor577f75a2009-08-04 16:50:30 +0000840 /// substitution of the corresponding template type argument.
John McCalla2becad2009-10-21 00:40:46 +0000841 QualType TransformTemplateTypeParmType(TypeLocBuilder &TLB,
John McCall43fed0d2010-11-12 08:19:04 +0000842 TemplateTypeParmTypeLoc TL);
Nick Lewycky03d98c52010-07-06 19:51:49 +0000843
Douglas Gregorc3069d62011-01-14 02:55:32 +0000844 /// \brief Transforms an already-substituted template type parameter pack
845 /// into either itself (if we aren't substituting into its pack expansion)
846 /// or the appropriate substituted argument.
847 QualType TransformSubstTemplateTypeParmPackType(TypeLocBuilder &TLB,
848 SubstTemplateTypeParmPackTypeLoc TL);
849
John McCall60d7b3a2010-08-24 06:29:42 +0000850 ExprResult TransformCallExpr(CallExpr *CE) {
Nick Lewycky03d98c52010-07-06 19:51:49 +0000851 getSema().CallsUndergoingInstantiation.push_back(CE);
John McCall60d7b3a2010-08-24 06:29:42 +0000852 ExprResult Result =
Nick Lewycky03d98c52010-07-06 19:51:49 +0000853 TreeTransform<TemplateInstantiator>::TransformCallExpr(CE);
854 getSema().CallsUndergoingInstantiation.pop_back();
855 return move(Result);
856 }
John McCall91a57552011-07-15 05:09:51 +0000857
858 private:
859 ExprResult transformNonTypeTemplateParmRef(NonTypeTemplateParmDecl *parm,
860 SourceLocation loc,
861 const TemplateArgument &arg);
Douglas Gregor577f75a2009-08-04 16:50:30 +0000862 };
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000863}
864
Douglas Gregorb4eeaff2010-05-07 23:12:07 +0000865bool TemplateInstantiator::AlreadyTransformed(QualType T) {
866 if (T.isNull())
867 return true;
868
Douglas Gregor561f8122011-07-01 01:22:09 +0000869 if (T->isInstantiationDependentType() || T->isVariablyModifiedType())
Douglas Gregorb4eeaff2010-05-07 23:12:07 +0000870 return false;
871
872 getSema().MarkDeclarationsReferencedInType(Loc, T);
873 return true;
874}
875
Douglas Gregor7c1e98f2010-03-01 15:56:25 +0000876Decl *TemplateInstantiator::TransformDecl(SourceLocation Loc, Decl *D) {
Douglas Gregorc68afe22009-09-03 21:38:09 +0000877 if (!D)
878 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000879
Douglas Gregorc68afe22009-09-03 21:38:09 +0000880 if (TemplateTemplateParmDecl *TTP = dyn_cast<TemplateTemplateParmDecl>(D)) {
Douglas Gregord6350ae2009-08-28 20:31:08 +0000881 if (TTP->getDepth() < TemplateArgs.getNumLevels()) {
Douglas Gregor6d3e6272010-02-05 19:54:12 +0000882 // If the corresponding template argument is NULL or non-existent, it's
883 // because we are performing instantiation from explicitly-specified
884 // template arguments in a function template, but there were some
885 // arguments left unspecified.
886 if (!TemplateArgs.hasTemplateArgument(TTP->getDepth(),
887 TTP->getPosition()))
888 return D;
889
Douglas Gregor61c4d282011-01-05 15:48:55 +0000890 TemplateArgument Arg = TemplateArgs(TTP->getDepth(), TTP->getPosition());
891
892 if (TTP->isParameterPack()) {
893 assert(Arg.getKind() == TemplateArgument::Pack &&
894 "Missing argument pack");
895
Douglas Gregor1aee05d2011-01-15 06:45:20 +0000896 assert(getSema().ArgumentPackSubstitutionIndex >= 0);
Douglas Gregord3731192011-01-10 07:32:04 +0000897 assert(getSema().ArgumentPackSubstitutionIndex < (int)Arg.pack_size());
Douglas Gregor61c4d282011-01-05 15:48:55 +0000898 Arg = Arg.pack_begin()[getSema().ArgumentPackSubstitutionIndex];
899 }
900
901 TemplateName Template = Arg.getAsTemplate();
Douglas Gregor788cd062009-11-11 01:00:40 +0000902 assert(!Template.isNull() && Template.getAsTemplateDecl() &&
Douglas Gregord6350ae2009-08-28 20:31:08 +0000903 "Wrong kind of template template argument");
Douglas Gregor788cd062009-11-11 01:00:40 +0000904 return Template.getAsTemplateDecl();
Douglas Gregord6350ae2009-08-28 20:31:08 +0000905 }
Mike Stump1eb44332009-09-09 15:08:12 +0000906
Douglas Gregor788cd062009-11-11 01:00:40 +0000907 // Fall through to find the instantiated declaration for this template
908 // template parameter.
Douglas Gregord1067e52009-08-06 06:41:21 +0000909 }
Mike Stump1eb44332009-09-09 15:08:12 +0000910
Douglas Gregor7c1e98f2010-03-01 15:56:25 +0000911 return SemaRef.FindInstantiatedDecl(Loc, cast<NamedDecl>(D), TemplateArgs);
Douglas Gregor577f75a2009-08-04 16:50:30 +0000912}
913
Douglas Gregoraac571c2010-03-01 17:25:41 +0000914Decl *TemplateInstantiator::TransformDefinition(SourceLocation Loc, Decl *D) {
John McCallce3ff2b2009-08-25 22:02:44 +0000915 Decl *Inst = getSema().SubstDecl(D, getSema().CurContext, TemplateArgs);
Douglas Gregor43959a92009-08-20 07:17:43 +0000916 if (!Inst)
917 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000918
Douglas Gregor43959a92009-08-20 07:17:43 +0000919 getSema().CurrentInstantiationScope->InstantiatedLocal(D, Inst);
920 return Inst;
921}
922
Douglas Gregor6cd21982009-10-20 05:58:46 +0000923NamedDecl *
924TemplateInstantiator::TransformFirstQualifierInScope(NamedDecl *D,
925 SourceLocation Loc) {
926 // If the first part of the nested-name-specifier was a template type
927 // parameter, instantiate that type parameter down to a tag type.
928 if (TemplateTypeParmDecl *TTPD = dyn_cast_or_null<TemplateTypeParmDecl>(D)) {
929 const TemplateTypeParmType *TTP
930 = cast<TemplateTypeParmType>(getSema().Context.getTypeDeclType(TTPD));
Douglas Gregor984a58b2010-12-20 22:48:17 +0000931
Douglas Gregor6cd21982009-10-20 05:58:46 +0000932 if (TTP->getDepth() < TemplateArgs.getNumLevels()) {
Douglas Gregor984a58b2010-12-20 22:48:17 +0000933 // FIXME: This needs testing w/ member access expressions.
934 TemplateArgument Arg = TemplateArgs(TTP->getDepth(), TTP->getIndex());
935
936 if (TTP->isParameterPack()) {
937 assert(Arg.getKind() == TemplateArgument::Pack &&
938 "Missing argument pack");
939
Douglas Gregor2be29f42011-01-14 23:41:42 +0000940 if (getSema().ArgumentPackSubstitutionIndex == -1)
Douglas Gregor984a58b2010-12-20 22:48:17 +0000941 return 0;
Douglas Gregor984a58b2010-12-20 22:48:17 +0000942
Douglas Gregord3731192011-01-10 07:32:04 +0000943 assert(getSema().ArgumentPackSubstitutionIndex < (int)Arg.pack_size());
Douglas Gregor984a58b2010-12-20 22:48:17 +0000944 Arg = Arg.pack_begin()[getSema().ArgumentPackSubstitutionIndex];
945 }
946
947 QualType T = Arg.getAsType();
Douglas Gregor6cd21982009-10-20 05:58:46 +0000948 if (T.isNull())
Douglas Gregor7c1e98f2010-03-01 15:56:25 +0000949 return cast_or_null<NamedDecl>(TransformDecl(Loc, D));
Douglas Gregor6cd21982009-10-20 05:58:46 +0000950
951 if (const TagType *Tag = T->getAs<TagType>())
952 return Tag->getDecl();
953
954 // The resulting type is not a tag; complain.
955 getSema().Diag(Loc, diag::err_nested_name_spec_non_tag) << T;
956 return 0;
957 }
958 }
959
Douglas Gregor7c1e98f2010-03-01 15:56:25 +0000960 return cast_or_null<NamedDecl>(TransformDecl(Loc, D));
Douglas Gregor6cd21982009-10-20 05:58:46 +0000961}
962
Douglas Gregor43959a92009-08-20 07:17:43 +0000963VarDecl *
964TemplateInstantiator::RebuildExceptionDecl(VarDecl *ExceptionDecl,
John McCalla93c9342009-12-07 02:54:59 +0000965 TypeSourceInfo *Declarator,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +0000966 SourceLocation StartLoc,
967 SourceLocation NameLoc,
968 IdentifierInfo *Name) {
Douglas Gregor83cb9422010-09-09 17:09:21 +0000969 VarDecl *Var = inherited::RebuildExceptionDecl(ExceptionDecl, Declarator,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +0000970 StartLoc, NameLoc, Name);
Douglas Gregorbe270a02010-04-26 17:57:08 +0000971 if (Var)
972 getSema().CurrentInstantiationScope->InstantiatedLocal(ExceptionDecl, Var);
973 return Var;
974}
975
976VarDecl *TemplateInstantiator::RebuildObjCExceptionDecl(VarDecl *ExceptionDecl,
977 TypeSourceInfo *TSInfo,
978 QualType T) {
979 VarDecl *Var = inherited::RebuildObjCExceptionDecl(ExceptionDecl, TSInfo, T);
980 if (Var)
Douglas Gregor43959a92009-08-20 07:17:43 +0000981 getSema().CurrentInstantiationScope->InstantiatedLocal(ExceptionDecl, Var);
982 return Var;
983}
984
John McCallc4e70192009-09-11 04:59:25 +0000985QualType
John McCall21e413f2010-11-04 19:04:38 +0000986TemplateInstantiator::RebuildElaboratedType(SourceLocation KeywordLoc,
987 ElaboratedTypeKeyword Keyword,
Douglas Gregor9e876872011-03-01 18:12:44 +0000988 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000989 QualType T) {
John McCallc4e70192009-09-11 04:59:25 +0000990 if (const TagType *TT = T->getAs<TagType>()) {
991 TagDecl* TD = TT->getDecl();
992
John McCall21e413f2010-11-04 19:04:38 +0000993 SourceLocation TagLocation = KeywordLoc;
John McCallc4e70192009-09-11 04:59:25 +0000994
995 // FIXME: type might be anonymous.
996 IdentifierInfo *Id = TD->getIdentifier();
997
998 // TODO: should we even warn on struct/class mismatches for this? Seems
999 // like it's likely to produce a lot of spurious errors.
Abramo Bagnara465d41b2010-05-11 21:36:43 +00001000 if (Keyword != ETK_None && Keyword != ETK_Typename) {
1001 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForKeyword(Keyword);
Richard Trieubbf34c02011-06-10 03:11:26 +00001002 if (!SemaRef.isAcceptableTagRedeclaration(TD, Kind, /*isDefinition*/false,
1003 TagLocation, *Id)) {
Abramo Bagnara465d41b2010-05-11 21:36:43 +00001004 SemaRef.Diag(TagLocation, diag::err_use_with_wrong_tag)
1005 << Id
1006 << FixItHint::CreateReplacement(SourceRange(TagLocation),
1007 TD->getKindName());
1008 SemaRef.Diag(TD->getLocation(), diag::note_previous_use);
1009 }
John McCallc4e70192009-09-11 04:59:25 +00001010 }
1011 }
1012
John McCall21e413f2010-11-04 19:04:38 +00001013 return TreeTransform<TemplateInstantiator>::RebuildElaboratedType(KeywordLoc,
1014 Keyword,
Douglas Gregor9e876872011-03-01 18:12:44 +00001015 QualifierLoc,
1016 T);
John McCallc4e70192009-09-11 04:59:25 +00001017}
1018
Douglas Gregorfd4ffeb2011-03-02 18:07:45 +00001019TemplateName TemplateInstantiator::TransformTemplateName(CXXScopeSpec &SS,
1020 TemplateName Name,
1021 SourceLocation NameLoc,
1022 QualType ObjectType,
1023 NamedDecl *FirstQualifierInScope) {
1024 if (TemplateTemplateParmDecl *TTP
1025 = dyn_cast_or_null<TemplateTemplateParmDecl>(Name.getAsTemplateDecl())) {
1026 if (TTP->getDepth() < TemplateArgs.getNumLevels()) {
1027 // If the corresponding template argument is NULL or non-existent, it's
1028 // because we are performing instantiation from explicitly-specified
1029 // template arguments in a function template, but there were some
1030 // arguments left unspecified.
1031 if (!TemplateArgs.hasTemplateArgument(TTP->getDepth(),
1032 TTP->getPosition()))
1033 return Name;
1034
1035 TemplateArgument Arg = TemplateArgs(TTP->getDepth(), TTP->getPosition());
1036
1037 if (TTP->isParameterPack()) {
1038 assert(Arg.getKind() == TemplateArgument::Pack &&
1039 "Missing argument pack");
1040
1041 if (getSema().ArgumentPackSubstitutionIndex == -1) {
1042 // We have the template argument pack to substitute, but we're not
1043 // actually expanding the enclosing pack expansion yet. So, just
1044 // keep the entire argument pack.
1045 return getSema().Context.getSubstTemplateTemplateParmPack(TTP, Arg);
1046 }
1047
1048 assert(getSema().ArgumentPackSubstitutionIndex < (int)Arg.pack_size());
1049 Arg = Arg.pack_begin()[getSema().ArgumentPackSubstitutionIndex];
1050 }
1051
1052 TemplateName Template = Arg.getAsTemplate();
Richard Smith3e4c6c42011-05-05 21:57:07 +00001053 assert(!Template.isNull() && "Null template template argument");
John McCall14606042011-06-30 08:33:18 +00001054
Douglas Gregor58750382011-03-05 20:06:51 +00001055 // We don't ever want to substitute for a qualified template name, since
1056 // the qualifier is handled separately. So, look through the qualified
1057 // template name to its underlying declaration.
1058 if (QualifiedTemplateName *QTN = Template.getAsQualifiedTemplateName())
1059 Template = TemplateName(QTN->getTemplateDecl());
John McCall14606042011-06-30 08:33:18 +00001060
1061 Template = getSema().Context.getSubstTemplateTemplateParm(TTP, Template);
Douglas Gregorfd4ffeb2011-03-02 18:07:45 +00001062 return Template;
1063 }
1064 }
1065
1066 if (SubstTemplateTemplateParmPackStorage *SubstPack
1067 = Name.getAsSubstTemplateTemplateParmPack()) {
1068 if (getSema().ArgumentPackSubstitutionIndex == -1)
1069 return Name;
1070
1071 const TemplateArgument &ArgPack = SubstPack->getArgumentPack();
1072 assert(getSema().ArgumentPackSubstitutionIndex < (int)ArgPack.pack_size() &&
1073 "Pack substitution index out-of-range");
1074 return ArgPack.pack_begin()[getSema().ArgumentPackSubstitutionIndex]
1075 .getAsTemplate();
1076 }
1077
1078 return inherited::TransformTemplateName(SS, Name, NameLoc, ObjectType,
1079 FirstQualifierInScope);
1080}
1081
John McCall60d7b3a2010-08-24 06:29:42 +00001082ExprResult
John McCall454feb92009-12-08 09:21:05 +00001083TemplateInstantiator::TransformPredefinedExpr(PredefinedExpr *E) {
Anders Carlsson773f3972009-09-11 01:22:35 +00001084 if (!E->isTypeDependent())
John McCall3fa5cae2010-10-26 07:05:15 +00001085 return SemaRef.Owned(E);
Anders Carlsson773f3972009-09-11 01:22:35 +00001086
1087 FunctionDecl *currentDecl = getSema().getCurFunctionDecl();
1088 assert(currentDecl && "Must have current function declaration when "
1089 "instantiating.");
1090
1091 PredefinedExpr::IdentType IT = E->getIdentType();
1092
Anders Carlsson848fa642010-02-11 18:20:28 +00001093 unsigned Length = PredefinedExpr::ComputeName(IT, currentDecl).length();
Anders Carlsson773f3972009-09-11 01:22:35 +00001094
1095 llvm::APInt LengthI(32, Length + 1);
John McCall0953e762009-09-24 19:53:00 +00001096 QualType ResTy = getSema().Context.CharTy.withConst();
Anders Carlsson773f3972009-09-11 01:22:35 +00001097 ResTy = getSema().Context.getConstantArrayType(ResTy, LengthI,
1098 ArrayType::Normal, 0);
1099 PredefinedExpr *PE =
1100 new (getSema().Context) PredefinedExpr(E->getLocation(), ResTy, IT);
1101 return getSema().Owned(PE);
1102}
1103
John McCall60d7b3a2010-08-24 06:29:42 +00001104ExprResult
John McCallb8fc0532010-02-06 08:42:39 +00001105TemplateInstantiator::TransformTemplateParmRefExpr(DeclRefExpr *E,
Douglas Gregordcee9802010-02-08 23:41:45 +00001106 NonTypeTemplateParmDecl *NTTP) {
John McCallb8fc0532010-02-06 08:42:39 +00001107 // If the corresponding template argument is NULL or non-existent, it's
1108 // because we are performing instantiation from explicitly-specified
1109 // template arguments in a function template, but there were some
1110 // arguments left unspecified.
1111 if (!TemplateArgs.hasTemplateArgument(NTTP->getDepth(),
1112 NTTP->getPosition()))
John McCall3fa5cae2010-10-26 07:05:15 +00001113 return SemaRef.Owned(E);
Mike Stump1eb44332009-09-09 15:08:12 +00001114
Douglas Gregor56bc9832010-12-24 00:15:10 +00001115 TemplateArgument Arg = TemplateArgs(NTTP->getDepth(), NTTP->getPosition());
1116 if (NTTP->isParameterPack()) {
1117 assert(Arg.getKind() == TemplateArgument::Pack &&
1118 "Missing argument pack");
1119
1120 if (getSema().ArgumentPackSubstitutionIndex == -1) {
Douglas Gregorc7793c72011-01-15 01:15:58 +00001121 // We have an argument pack, but we can't select a particular argument
1122 // out of it yet. Therefore, we'll build an expression to hold on to that
1123 // argument pack.
1124 QualType TargetType = SemaRef.SubstType(NTTP->getType(), TemplateArgs,
1125 E->getLocation(),
1126 NTTP->getDeclName());
1127 if (TargetType.isNull())
1128 return ExprError();
1129
1130 return new (SemaRef.Context) SubstNonTypeTemplateParmPackExpr(TargetType,
1131 NTTP,
1132 E->getLocation(),
1133 Arg);
Douglas Gregor56bc9832010-12-24 00:15:10 +00001134 }
1135
Douglas Gregord3731192011-01-10 07:32:04 +00001136 assert(getSema().ArgumentPackSubstitutionIndex < (int)Arg.pack_size());
Douglas Gregor56bc9832010-12-24 00:15:10 +00001137 Arg = Arg.pack_begin()[getSema().ArgumentPackSubstitutionIndex];
1138 }
Mike Stump1eb44332009-09-09 15:08:12 +00001139
John McCall91a57552011-07-15 05:09:51 +00001140 return transformNonTypeTemplateParmRef(NTTP, E->getLocation(), Arg);
1141}
1142
1143ExprResult TemplateInstantiator::transformNonTypeTemplateParmRef(
1144 NonTypeTemplateParmDecl *parm,
1145 SourceLocation loc,
1146 const TemplateArgument &arg) {
1147 ExprResult result;
1148 QualType type;
1149
John McCallb8fc0532010-02-06 08:42:39 +00001150 // The template argument itself might be an expression, in which
1151 // case we just return that expression.
John McCall91a57552011-07-15 05:09:51 +00001152 if (arg.getKind() == TemplateArgument::Expression) {
1153 Expr *argExpr = arg.getAsExpr();
1154 result = SemaRef.Owned(argExpr);
1155 type = argExpr->getType();
Mike Stump1eb44332009-09-09 15:08:12 +00001156
John McCall91a57552011-07-15 05:09:51 +00001157 } else if (arg.getKind() == TemplateArgument::Declaration) {
Douglas Gregord2008e22012-04-06 22:40:38 +00001158 ValueDecl *VD;
1159 if (Decl *D = arg.getAsDecl()) {
1160 VD = cast<ValueDecl>(D);
Mike Stump1eb44332009-09-09 15:08:12 +00001161
Douglas Gregord2008e22012-04-06 22:40:38 +00001162 // Find the instantiation of the template argument. This is
1163 // required for nested templates.
1164 VD = cast_or_null<ValueDecl>(
1165 getSema().FindInstantiatedDecl(loc, VD, TemplateArgs));
1166 if (!VD)
1167 return ExprError();
1168 } else {
1169 // Propagate NULL template argument.
1170 VD = 0;
1171 }
1172
John McCall645cf442010-02-06 10:23:53 +00001173 // Derive the type we want the substituted decl to have. This had
1174 // better be non-dependent, or these checks will have serious problems.
John McCall91a57552011-07-15 05:09:51 +00001175 if (parm->isExpandedParameterPack()) {
1176 type = parm->getExpansionType(SemaRef.ArgumentPackSubstitutionIndex);
1177 } else if (parm->isParameterPack() &&
1178 isa<PackExpansionType>(parm->getType())) {
1179 type = SemaRef.SubstType(
1180 cast<PackExpansionType>(parm->getType())->getPattern(),
1181 TemplateArgs, loc, parm->getDeclName());
1182 } else {
1183 type = SemaRef.SubstType(parm->getType(), TemplateArgs,
1184 loc, parm->getDeclName());
1185 }
1186 assert(!type.isNull() && "type substitution failed for param type");
1187 assert(!type->isDependentType() && "param type still dependent");
1188 result = SemaRef.BuildExpressionFromDeclTemplateArgument(arg, type, loc);
John McCallb8fc0532010-02-06 08:42:39 +00001189
John McCall91a57552011-07-15 05:09:51 +00001190 if (!result.isInvalid()) type = result.get()->getType();
1191 } else {
1192 result = SemaRef.BuildExpressionFromIntegralTemplateArgument(arg, loc);
1193
1194 // Note that this type can be different from the type of 'result',
1195 // e.g. if it's an enum type.
1196 type = arg.getIntegralType();
1197 }
1198 if (result.isInvalid()) return ExprError();
1199
1200 Expr *resultExpr = result.take();
1201 return SemaRef.Owned(new (SemaRef.Context)
1202 SubstNonTypeTemplateParmExpr(type,
1203 resultExpr->getValueKind(),
1204 loc, parm, resultExpr));
John McCallb8fc0532010-02-06 08:42:39 +00001205}
1206
Douglas Gregorc7793c72011-01-15 01:15:58 +00001207ExprResult
1208TemplateInstantiator::TransformSubstNonTypeTemplateParmPackExpr(
1209 SubstNonTypeTemplateParmPackExpr *E) {
1210 if (getSema().ArgumentPackSubstitutionIndex == -1) {
1211 // We aren't expanding the parameter pack, so just return ourselves.
1212 return getSema().Owned(E);
1213 }
1214
Douglas Gregorc7793c72011-01-15 01:15:58 +00001215 const TemplateArgument &ArgPack = E->getArgumentPack();
1216 unsigned Index = (unsigned)getSema().ArgumentPackSubstitutionIndex;
1217 assert(Index < ArgPack.pack_size() && "Substitution index out-of-range");
1218
1219 const TemplateArgument &Arg = ArgPack.pack_begin()[Index];
John McCall91a57552011-07-15 05:09:51 +00001220 return transformNonTypeTemplateParmRef(E->getParameterPack(),
1221 E->getParameterPackLocation(),
1222 Arg);
Douglas Gregorc7793c72011-01-15 01:15:58 +00001223}
John McCallb8fc0532010-02-06 08:42:39 +00001224
John McCall60d7b3a2010-08-24 06:29:42 +00001225ExprResult
John McCallb8fc0532010-02-06 08:42:39 +00001226TemplateInstantiator::TransformDeclRefExpr(DeclRefExpr *E) {
1227 NamedDecl *D = E->getDecl();
1228 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(D)) {
1229 if (NTTP->getDepth() < TemplateArgs.getNumLevels())
1230 return TransformTemplateParmRefExpr(E, NTTP);
Douglas Gregor550d9b22009-10-31 17:21:17 +00001231
1232 // We have a non-type template parameter that isn't fully substituted;
1233 // FindInstantiatedDecl will find it in the local instantiation scope.
Douglas Gregorb98b1992009-08-11 05:31:07 +00001234 }
Mike Stump1eb44332009-09-09 15:08:12 +00001235
John McCall454feb92009-12-08 09:21:05 +00001236 return TreeTransform<TemplateInstantiator>::TransformDeclRefExpr(E);
Douglas Gregorb98b1992009-08-11 05:31:07 +00001237}
1238
John McCall60d7b3a2010-08-24 06:29:42 +00001239ExprResult TemplateInstantiator::TransformCXXDefaultArgExpr(
John McCall454feb92009-12-08 09:21:05 +00001240 CXXDefaultArgExpr *E) {
Sebastian Redla29e51b2009-11-08 13:56:19 +00001241 assert(!cast<FunctionDecl>(E->getParam()->getDeclContext())->
1242 getDescribedFunctionTemplate() &&
1243 "Default arg expressions are never formed in dependent cases.");
Douglas Gregor036aed12009-12-23 23:03:06 +00001244 return SemaRef.BuildCXXDefaultArgExpr(E->getUsedLocation(),
1245 cast<FunctionDecl>(E->getParam()->getDeclContext()),
1246 E->getParam());
Sebastian Redla29e51b2009-11-08 13:56:19 +00001247}
1248
Douglas Gregor895162d2010-04-30 18:55:50 +00001249QualType TemplateInstantiator::TransformFunctionProtoType(TypeLocBuilder &TLB,
John McCall43fed0d2010-11-12 08:19:04 +00001250 FunctionProtoTypeLoc TL) {
Douglas Gregor895162d2010-04-30 18:55:50 +00001251 // We need a local instantiation scope for this function prototype.
John McCall2a7fb272010-08-25 05:32:35 +00001252 LocalInstantiationScope Scope(SemaRef, /*CombineWithOuterScope=*/true);
John McCall43fed0d2010-11-12 08:19:04 +00001253 return inherited::TransformFunctionProtoType(TLB, TL);
John McCall21ef0fa2010-03-11 09:03:00 +00001254}
1255
Douglas Gregorcefc3af2012-04-16 07:05:22 +00001256QualType TemplateInstantiator::TransformFunctionProtoType(TypeLocBuilder &TLB,
1257 FunctionProtoTypeLoc TL,
1258 CXXRecordDecl *ThisContext,
1259 unsigned ThisTypeQuals) {
1260 // We need a local instantiation scope for this function prototype.
1261 LocalInstantiationScope Scope(SemaRef, /*CombineWithOuterScope=*/true);
1262 return inherited::TransformFunctionProtoType(TLB, TL, ThisContext,
1263 ThisTypeQuals);
1264}
1265
John McCall21ef0fa2010-03-11 09:03:00 +00001266ParmVarDecl *
Douglas Gregor6a24bfd2011-01-14 22:40:04 +00001267TemplateInstantiator::TransformFunctionTypeParam(ParmVarDecl *OldParm,
John McCallfb44de92011-05-01 22:35:37 +00001268 int indexAdjustment,
Douglas Gregord1bb4ae2012-01-25 16:15:54 +00001269 llvm::Optional<unsigned> NumExpansions,
1270 bool ExpectParameterPack) {
John McCallfb44de92011-05-01 22:35:37 +00001271 return SemaRef.SubstParmVarDecl(OldParm, TemplateArgs, indexAdjustment,
Douglas Gregord1bb4ae2012-01-25 16:15:54 +00001272 NumExpansions, ExpectParameterPack);
John McCall21ef0fa2010-03-11 09:03:00 +00001273}
1274
Mike Stump1eb44332009-09-09 15:08:12 +00001275QualType
John McCalla2becad2009-10-21 00:40:46 +00001276TemplateInstantiator::TransformTemplateTypeParmType(TypeLocBuilder &TLB,
John McCall43fed0d2010-11-12 08:19:04 +00001277 TemplateTypeParmTypeLoc TL) {
John McCallf4c73712011-01-19 06:33:43 +00001278 const TemplateTypeParmType *T = TL.getTypePtr();
Douglas Gregord6350ae2009-08-28 20:31:08 +00001279 if (T->getDepth() < TemplateArgs.getNumLevels()) {
Douglas Gregor99ebf652009-02-27 19:31:52 +00001280 // Replace the template type parameter with its corresponding
1281 // template argument.
Mike Stump1eb44332009-09-09 15:08:12 +00001282
1283 // If the corresponding template argument is NULL or doesn't exist, it's
1284 // because we are performing instantiation from explicitly-specified
1285 // template arguments in a function template class, but there were some
Douglas Gregor16134c62009-07-01 00:28:38 +00001286 // arguments left unspecified.
John McCalla2becad2009-10-21 00:40:46 +00001287 if (!TemplateArgs.hasTemplateArgument(T->getDepth(), T->getIndex())) {
1288 TemplateTypeParmTypeLoc NewTL
1289 = TLB.push<TemplateTypeParmTypeLoc>(TL.getType());
1290 NewTL.setNameLoc(TL.getNameLoc());
1291 return TL.getType();
1292 }
Mike Stump1eb44332009-09-09 15:08:12 +00001293
Douglas Gregor8491ffe2010-12-20 22:05:00 +00001294 TemplateArgument Arg = TemplateArgs(T->getDepth(), T->getIndex());
1295
1296 if (T->isParameterPack()) {
1297 assert(Arg.getKind() == TemplateArgument::Pack &&
1298 "Missing argument pack");
1299
1300 if (getSema().ArgumentPackSubstitutionIndex == -1) {
Douglas Gregorc3069d62011-01-14 02:55:32 +00001301 // We have the template argument pack, but we're not expanding the
1302 // enclosing pack expansion yet. Just save the template argument
1303 // pack for later substitution.
1304 QualType Result
1305 = getSema().Context.getSubstTemplateTypeParmPackType(T, Arg);
1306 SubstTemplateTypeParmPackTypeLoc NewTL
1307 = TLB.push<SubstTemplateTypeParmPackTypeLoc>(Result);
1308 NewTL.setNameLoc(TL.getNameLoc());
1309 return Result;
Douglas Gregor8491ffe2010-12-20 22:05:00 +00001310 }
1311
Douglas Gregord3731192011-01-10 07:32:04 +00001312 assert(getSema().ArgumentPackSubstitutionIndex < (int)Arg.pack_size());
Douglas Gregor8491ffe2010-12-20 22:05:00 +00001313 Arg = Arg.pack_begin()[getSema().ArgumentPackSubstitutionIndex];
1314 }
1315
1316 assert(Arg.getKind() == TemplateArgument::Type &&
Douglas Gregor99ebf652009-02-27 19:31:52 +00001317 "Template argument kind mismatch");
Douglas Gregord6350ae2009-08-28 20:31:08 +00001318
Douglas Gregor8491ffe2010-12-20 22:05:00 +00001319 QualType Replacement = Arg.getAsType();
John McCall49a832b2009-10-18 09:09:24 +00001320
1321 // TODO: only do this uniquing once, at the start of instantiation.
John McCalla2becad2009-10-21 00:40:46 +00001322 QualType Result
1323 = getSema().Context.getSubstTemplateTypeParmType(T, Replacement);
1324 SubstTemplateTypeParmTypeLoc NewTL
1325 = TLB.push<SubstTemplateTypeParmTypeLoc>(Result);
1326 NewTL.setNameLoc(TL.getNameLoc());
1327 return Result;
Mike Stump1eb44332009-09-09 15:08:12 +00001328 }
Douglas Gregor99ebf652009-02-27 19:31:52 +00001329
1330 // The template type parameter comes from an inner template (e.g.,
1331 // the template parameter list of a member template inside the
1332 // template we are instantiating). Create a new template type
1333 // parameter with the template "level" reduced by one.
Chandler Carruth4fb86f82011-05-01 00:51:33 +00001334 TemplateTypeParmDecl *NewTTPDecl = 0;
1335 if (TemplateTypeParmDecl *OldTTPDecl = T->getDecl())
1336 NewTTPDecl = cast_or_null<TemplateTypeParmDecl>(
1337 TransformDecl(TL.getNameLoc(), OldTTPDecl));
1338
John McCalla2becad2009-10-21 00:40:46 +00001339 QualType Result
1340 = getSema().Context.getTemplateTypeParmType(T->getDepth()
1341 - TemplateArgs.getNumLevels(),
1342 T->getIndex(),
1343 T->isParameterPack(),
Chandler Carruth4fb86f82011-05-01 00:51:33 +00001344 NewTTPDecl);
John McCalla2becad2009-10-21 00:40:46 +00001345 TemplateTypeParmTypeLoc NewTL = TLB.push<TemplateTypeParmTypeLoc>(Result);
1346 NewTL.setNameLoc(TL.getNameLoc());
1347 return Result;
Douglas Gregorcd281c32009-02-28 00:25:32 +00001348}
Douglas Gregor99ebf652009-02-27 19:31:52 +00001349
Douglas Gregorc3069d62011-01-14 02:55:32 +00001350QualType
1351TemplateInstantiator::TransformSubstTemplateTypeParmPackType(
1352 TypeLocBuilder &TLB,
1353 SubstTemplateTypeParmPackTypeLoc TL) {
1354 if (getSema().ArgumentPackSubstitutionIndex == -1) {
1355 // We aren't expanding the parameter pack, so just return ourselves.
1356 SubstTemplateTypeParmPackTypeLoc NewTL
1357 = TLB.push<SubstTemplateTypeParmPackTypeLoc>(TL.getType());
1358 NewTL.setNameLoc(TL.getNameLoc());
1359 return TL.getType();
1360 }
1361
1362 const TemplateArgument &ArgPack = TL.getTypePtr()->getArgumentPack();
1363 unsigned Index = (unsigned)getSema().ArgumentPackSubstitutionIndex;
1364 assert(Index < ArgPack.pack_size() && "Substitution index out-of-range");
1365
1366 QualType Result = ArgPack.pack_begin()[Index].getAsType();
1367 Result = getSema().Context.getSubstTemplateTypeParmType(
1368 TL.getTypePtr()->getReplacedParameter(),
1369 Result);
1370 SubstTemplateTypeParmTypeLoc NewTL
1371 = TLB.push<SubstTemplateTypeParmTypeLoc>(Result);
1372 NewTL.setNameLoc(TL.getNameLoc());
1373 return Result;
1374}
1375
John McCallce3ff2b2009-08-25 22:02:44 +00001376/// \brief Perform substitution on the type T with a given set of template
1377/// arguments.
Douglas Gregor99ebf652009-02-27 19:31:52 +00001378///
1379/// This routine substitutes the given template arguments into the
1380/// type T and produces the instantiated type.
1381///
1382/// \param T the type into which the template arguments will be
1383/// substituted. If this type is not dependent, it will be returned
1384/// immediately.
1385///
1386/// \param TemplateArgs the template arguments that will be
1387/// substituted for the top-level template parameters within T.
1388///
Douglas Gregor99ebf652009-02-27 19:31:52 +00001389/// \param Loc the location in the source code where this substitution
1390/// is being performed. It will typically be the location of the
1391/// declarator (if we're instantiating the type of some declaration)
1392/// or the location of the type in the source code (if, e.g., we're
1393/// instantiating the type of a cast expression).
1394///
1395/// \param Entity the name of the entity associated with a declaration
1396/// being instantiated (if any). May be empty to indicate that there
1397/// is no such entity (if, e.g., this is a type that occurs as part of
1398/// a cast expression) or that the entity has no name (e.g., an
1399/// unnamed function parameter).
1400///
1401/// \returns If the instantiation succeeds, the instantiated
1402/// type. Otherwise, produces diagnostics and returns a NULL type.
John McCalla93c9342009-12-07 02:54:59 +00001403TypeSourceInfo *Sema::SubstType(TypeSourceInfo *T,
John McCallcd7ba1c2009-10-21 00:58:09 +00001404 const MultiLevelTemplateArgumentList &Args,
1405 SourceLocation Loc,
1406 DeclarationName Entity) {
1407 assert(!ActiveTemplateInstantiations.empty() &&
1408 "Cannot perform an instantiation without some context on the "
1409 "instantiation stack");
1410
Douglas Gregor561f8122011-07-01 01:22:09 +00001411 if (!T->getType()->isInstantiationDependentType() &&
Douglas Gregor836adf62010-05-24 17:22:01 +00001412 !T->getType()->isVariablyModifiedType())
John McCallcd7ba1c2009-10-21 00:58:09 +00001413 return T;
1414
1415 TemplateInstantiator Instantiator(*this, Args, Loc, Entity);
1416 return Instantiator.TransformType(T);
1417}
1418
Douglas Gregor603cfb42011-01-05 23:12:31 +00001419TypeSourceInfo *Sema::SubstType(TypeLoc TL,
1420 const MultiLevelTemplateArgumentList &Args,
1421 SourceLocation Loc,
1422 DeclarationName Entity) {
1423 assert(!ActiveTemplateInstantiations.empty() &&
1424 "Cannot perform an instantiation without some context on the "
1425 "instantiation stack");
1426
1427 if (TL.getType().isNull())
1428 return 0;
1429
Douglas Gregor561f8122011-07-01 01:22:09 +00001430 if (!TL.getType()->isInstantiationDependentType() &&
Douglas Gregor603cfb42011-01-05 23:12:31 +00001431 !TL.getType()->isVariablyModifiedType()) {
1432 // FIXME: Make a copy of the TypeLoc data here, so that we can
1433 // return a new TypeSourceInfo. Inefficient!
1434 TypeLocBuilder TLB;
1435 TLB.pushFullCopy(TL);
1436 return TLB.getTypeSourceInfo(Context, TL.getType());
1437 }
1438
1439 TemplateInstantiator Instantiator(*this, Args, Loc, Entity);
1440 TypeLocBuilder TLB;
1441 TLB.reserve(TL.getFullDataSize());
1442 QualType Result = Instantiator.TransformType(TLB, TL);
1443 if (Result.isNull())
1444 return 0;
1445
1446 return TLB.getTypeSourceInfo(Context, Result);
1447}
1448
John McCallcd7ba1c2009-10-21 00:58:09 +00001449/// Deprecated form of the above.
Mike Stump1eb44332009-09-09 15:08:12 +00001450QualType Sema::SubstType(QualType T,
Douglas Gregord6350ae2009-08-28 20:31:08 +00001451 const MultiLevelTemplateArgumentList &TemplateArgs,
John McCallce3ff2b2009-08-25 22:02:44 +00001452 SourceLocation Loc, DeclarationName Entity) {
Douglas Gregordf667e72009-03-10 20:44:00 +00001453 assert(!ActiveTemplateInstantiations.empty() &&
1454 "Cannot perform an instantiation without some context on the "
1455 "instantiation stack");
1456
Douglas Gregor836adf62010-05-24 17:22:01 +00001457 // If T is not a dependent type or a variably-modified type, there
1458 // is nothing to do.
Douglas Gregor561f8122011-07-01 01:22:09 +00001459 if (!T->isInstantiationDependentType() && !T->isVariablyModifiedType())
Douglas Gregor99ebf652009-02-27 19:31:52 +00001460 return T;
1461
Douglas Gregor577f75a2009-08-04 16:50:30 +00001462 TemplateInstantiator Instantiator(*this, TemplateArgs, Loc, Entity);
1463 return Instantiator.TransformType(T);
Douglas Gregor99ebf652009-02-27 19:31:52 +00001464}
Douglas Gregor2943aed2009-03-03 04:44:36 +00001465
John McCall6cd3b9f2010-04-09 17:38:44 +00001466static bool NeedsInstantiationAsFunctionType(TypeSourceInfo *T) {
Douglas Gregor561f8122011-07-01 01:22:09 +00001467 if (T->getType()->isInstantiationDependentType() ||
1468 T->getType()->isVariablyModifiedType())
John McCall6cd3b9f2010-04-09 17:38:44 +00001469 return true;
1470
Abramo Bagnara723df242010-12-14 22:11:44 +00001471 TypeLoc TL = T->getTypeLoc().IgnoreParens();
John McCall6cd3b9f2010-04-09 17:38:44 +00001472 if (!isa<FunctionProtoTypeLoc>(TL))
1473 return false;
1474
1475 FunctionProtoTypeLoc FP = cast<FunctionProtoTypeLoc>(TL);
1476 for (unsigned I = 0, E = FP.getNumArgs(); I != E; ++I) {
1477 ParmVarDecl *P = FP.getArg(I);
1478
Douglas Gregorc056c172011-05-09 20:45:16 +00001479 // The parameter's type as written might be dependent even if the
1480 // decayed type was not dependent.
1481 if (TypeSourceInfo *TSInfo = P->getTypeSourceInfo())
Douglas Gregor561f8122011-07-01 01:22:09 +00001482 if (TSInfo->getType()->isInstantiationDependentType())
Douglas Gregorc056c172011-05-09 20:45:16 +00001483 return true;
1484
John McCall6cd3b9f2010-04-09 17:38:44 +00001485 // TODO: currently we always rebuild expressions. When we
1486 // properly get lazier about this, we should use the same
1487 // logic to avoid rebuilding prototypes here.
Douglas Gregor7b1cf302011-01-05 21:14:17 +00001488 if (P->hasDefaultArg())
John McCall6cd3b9f2010-04-09 17:38:44 +00001489 return true;
1490 }
1491
1492 return false;
1493}
1494
1495/// A form of SubstType intended specifically for instantiating the
1496/// type of a FunctionDecl. Its purpose is solely to force the
1497/// instantiation of default-argument expressions.
1498TypeSourceInfo *Sema::SubstFunctionDeclType(TypeSourceInfo *T,
1499 const MultiLevelTemplateArgumentList &Args,
1500 SourceLocation Loc,
Douglas Gregorcefc3af2012-04-16 07:05:22 +00001501 DeclarationName Entity,
1502 CXXRecordDecl *ThisContext,
1503 unsigned ThisTypeQuals) {
John McCall6cd3b9f2010-04-09 17:38:44 +00001504 assert(!ActiveTemplateInstantiations.empty() &&
1505 "Cannot perform an instantiation without some context on the "
1506 "instantiation stack");
1507
1508 if (!NeedsInstantiationAsFunctionType(T))
1509 return T;
1510
1511 TemplateInstantiator Instantiator(*this, Args, Loc, Entity);
1512
1513 TypeLocBuilder TLB;
1514
1515 TypeLoc TL = T->getTypeLoc();
1516 TLB.reserve(TL.getFullDataSize());
1517
Douglas Gregorcefc3af2012-04-16 07:05:22 +00001518 QualType Result;
1519
1520 if (FunctionProtoTypeLoc *Proto = dyn_cast<FunctionProtoTypeLoc>(&TL)) {
1521 Result = Instantiator.TransformFunctionProtoType(TLB, *Proto, ThisContext,
1522 ThisTypeQuals);
1523 } else {
1524 Result = Instantiator.TransformType(TLB, TL);
1525 }
John McCall6cd3b9f2010-04-09 17:38:44 +00001526 if (Result.isNull())
1527 return 0;
1528
1529 return TLB.getTypeSourceInfo(Context, Result);
1530}
1531
Douglas Gregorcb27b0f2010-04-12 07:48:19 +00001532ParmVarDecl *Sema::SubstParmVarDecl(ParmVarDecl *OldParm,
Douglas Gregor6a24bfd2011-01-14 22:40:04 +00001533 const MultiLevelTemplateArgumentList &TemplateArgs,
John McCallfb44de92011-05-01 22:35:37 +00001534 int indexAdjustment,
Douglas Gregord1bb4ae2012-01-25 16:15:54 +00001535 llvm::Optional<unsigned> NumExpansions,
1536 bool ExpectParameterPack) {
Douglas Gregorcb27b0f2010-04-12 07:48:19 +00001537 TypeSourceInfo *OldDI = OldParm->getTypeSourceInfo();
Douglas Gregor603cfb42011-01-05 23:12:31 +00001538 TypeSourceInfo *NewDI = 0;
1539
Douglas Gregor603cfb42011-01-05 23:12:31 +00001540 TypeLoc OldTL = OldDI->getTypeLoc();
1541 if (isa<PackExpansionTypeLoc>(OldTL)) {
1542 PackExpansionTypeLoc ExpansionTL = cast<PackExpansionTypeLoc>(OldTL);
Douglas Gregor603cfb42011-01-05 23:12:31 +00001543
1544 // We have a function parameter pack. Substitute into the pattern of the
1545 // expansion.
1546 NewDI = SubstType(ExpansionTL.getPatternLoc(), TemplateArgs,
1547 OldParm->getLocation(), OldParm->getDeclName());
1548 if (!NewDI)
1549 return 0;
1550
1551 if (NewDI->getType()->containsUnexpandedParameterPack()) {
1552 // We still have unexpanded parameter packs, which means that
1553 // our function parameter is still a function parameter pack.
1554 // Therefore, make its type a pack expansion type.
Douglas Gregorcded4f62011-01-14 17:04:44 +00001555 NewDI = CheckPackExpansion(NewDI, ExpansionTL.getEllipsisLoc(),
Douglas Gregor6a24bfd2011-01-14 22:40:04 +00001556 NumExpansions);
Douglas Gregord1bb4ae2012-01-25 16:15:54 +00001557 } else if (ExpectParameterPack) {
1558 // We expected to get a parameter pack but didn't (because the type
1559 // itself is not a pack expansion type), so complain. This can occur when
1560 // the substitution goes through an alias template that "loses" the
1561 // pack expansion.
1562 Diag(OldParm->getLocation(),
1563 diag::err_function_parameter_pack_without_parameter_packs)
1564 << NewDI->getType();
1565 return 0;
1566 }
Douglas Gregor603cfb42011-01-05 23:12:31 +00001567 } else {
1568 NewDI = SubstType(OldDI, TemplateArgs, OldParm->getLocation(),
1569 OldParm->getDeclName());
1570 }
1571
Douglas Gregorcb27b0f2010-04-12 07:48:19 +00001572 if (!NewDI)
1573 return 0;
1574
1575 if (NewDI->getType()->isVoidType()) {
1576 Diag(OldParm->getLocation(), diag::err_param_with_void_type);
1577 return 0;
1578 }
1579
1580 ParmVarDecl *NewParm = CheckParameter(Context.getTranslationUnitDecl(),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001581 OldParm->getInnerLocStart(),
Douglas Gregorcb27b0f2010-04-12 07:48:19 +00001582 OldParm->getLocation(),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001583 OldParm->getIdentifier(),
1584 NewDI->getType(), NewDI,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001585 OldParm->getStorageClass(),
1586 OldParm->getStorageClassAsWritten());
Douglas Gregorcb27b0f2010-04-12 07:48:19 +00001587 if (!NewParm)
1588 return 0;
Douglas Gregor4469e8a2010-05-19 17:02:24 +00001589
Douglas Gregorcb27b0f2010-04-12 07:48:19 +00001590 // Mark the (new) default argument as uninstantiated (if any).
1591 if (OldParm->hasUninstantiatedDefaultArg()) {
1592 Expr *Arg = OldParm->getUninstantiatedDefaultArg();
1593 NewParm->setUninstantiatedDefaultArg(Arg);
Douglas Gregor8cfb7a32010-10-12 18:23:32 +00001594 } else if (OldParm->hasUnparsedDefaultArg()) {
1595 NewParm->setUnparsedDefaultArg();
1596 UnparsedDefaultArgInstantiations[OldParm].push_back(NewParm);
David Blaikie57296722012-05-01 06:05:57 +00001597 } else if (Expr *Arg = OldParm->getDefaultArg())
1598 // FIXME: if we non-lazily instantiated non-dependent default args for
1599 // non-dependent parameter types we could remove a bunch of duplicate
1600 // conversion warnings for such arguments.
1601 NewParm->setUninstantiatedDefaultArg(Arg);
Douglas Gregorcb27b0f2010-04-12 07:48:19 +00001602
1603 NewParm->setHasInheritedDefaultArg(OldParm->hasInheritedDefaultArg());
Douglas Gregord1bb4ae2012-01-25 16:15:54 +00001604
Douglas Gregor12c9c002011-01-07 16:43:16 +00001605 if (OldParm->isParameterPack() && !NewParm->isParameterPack()) {
Richard Smithc0536c82012-01-25 02:14:59 +00001606 // Add the new parameter to the instantiated parameter pack.
Douglas Gregor12c9c002011-01-07 16:43:16 +00001607 CurrentInstantiationScope->InstantiatedLocalPackArg(OldParm, NewParm);
1608 } else {
1609 // Introduce an Old -> New mapping
Douglas Gregor603cfb42011-01-05 23:12:31 +00001610 CurrentInstantiationScope->InstantiatedLocal(OldParm, NewParm);
Douglas Gregor12c9c002011-01-07 16:43:16 +00001611 }
Douglas Gregor603cfb42011-01-05 23:12:31 +00001612
Argyrios Kyrtzidise3041be2010-07-19 10:14:41 +00001613 // FIXME: OldParm may come from a FunctionProtoType, in which case CurContext
1614 // can be anything, is this right ?
Fariborz Jahanian55a17c02010-07-13 21:05:02 +00001615 NewParm->setDeclContext(CurContext);
John McCallfb44de92011-05-01 22:35:37 +00001616
1617 NewParm->setScopeInfo(OldParm->getFunctionScopeDepth(),
1618 OldParm->getFunctionScopeIndex() + indexAdjustment);
Fariborz Jahaniane7ffbe22010-07-13 20:05:58 +00001619
Douglas Gregorcb27b0f2010-04-12 07:48:19 +00001620 return NewParm;
1621}
1622
Douglas Gregora009b592011-01-07 00:20:55 +00001623/// \brief Substitute the given template arguments into the given set of
1624/// parameters, producing the set of parameter types that would be generated
1625/// from such a substitution.
1626bool Sema::SubstParmTypes(SourceLocation Loc,
1627 ParmVarDecl **Params, unsigned NumParams,
1628 const MultiLevelTemplateArgumentList &TemplateArgs,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001629 SmallVectorImpl<QualType> &ParamTypes,
1630 SmallVectorImpl<ParmVarDecl *> *OutParams) {
Douglas Gregora009b592011-01-07 00:20:55 +00001631 assert(!ActiveTemplateInstantiations.empty() &&
1632 "Cannot perform an instantiation without some context on the "
1633 "instantiation stack");
1634
1635 TemplateInstantiator Instantiator(*this, TemplateArgs, Loc,
1636 DeclarationName());
1637 return Instantiator.TransformFunctionTypeParams(Loc, Params, NumParams, 0,
Douglas Gregor12c9c002011-01-07 16:43:16 +00001638 ParamTypes, OutParams);
Douglas Gregora009b592011-01-07 00:20:55 +00001639}
1640
John McCallce3ff2b2009-08-25 22:02:44 +00001641/// \brief Perform substitution on the base class specifiers of the
1642/// given class template specialization.
Douglas Gregor2943aed2009-03-03 04:44:36 +00001643///
1644/// Produces a diagnostic and returns true on error, returns false and
1645/// attaches the instantiated base classes to the class template
1646/// specialization if successful.
Mike Stump1eb44332009-09-09 15:08:12 +00001647bool
John McCallce3ff2b2009-08-25 22:02:44 +00001648Sema::SubstBaseSpecifiers(CXXRecordDecl *Instantiation,
1649 CXXRecordDecl *Pattern,
Douglas Gregord6350ae2009-08-28 20:31:08 +00001650 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor2943aed2009-03-03 04:44:36 +00001651 bool Invalid = false;
Chris Lattner5f9e2722011-07-23 10:55:15 +00001652 SmallVector<CXXBaseSpecifier*, 4> InstantiatedBases;
Mike Stump1eb44332009-09-09 15:08:12 +00001653 for (ClassTemplateSpecializationDecl::base_class_iterator
Douglas Gregord475b8d2009-03-25 21:17:03 +00001654 Base = Pattern->bases_begin(), BaseEnd = Pattern->bases_end();
Douglas Gregor27b152f2009-03-10 18:52:44 +00001655 Base != BaseEnd; ++Base) {
Douglas Gregor2943aed2009-03-03 04:44:36 +00001656 if (!Base->getType()->isDependentType()) {
Fariborz Jahanian71c6e712009-07-22 17:41:53 +00001657 InstantiatedBases.push_back(new (Context) CXXBaseSpecifier(*Base));
Douglas Gregor2943aed2009-03-03 04:44:36 +00001658 continue;
1659 }
1660
Douglas Gregorf90b27a2011-01-03 22:36:02 +00001661 SourceLocation EllipsisLoc;
Douglas Gregor406f98f2011-03-02 02:04:06 +00001662 TypeSourceInfo *BaseTypeLoc;
Douglas Gregorf90b27a2011-01-03 22:36:02 +00001663 if (Base->isPackExpansion()) {
1664 // This is a pack expansion. See whether we should expand it now, or
1665 // wait until later.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001666 SmallVector<UnexpandedParameterPack, 2> Unexpanded;
Douglas Gregorf90b27a2011-01-03 22:36:02 +00001667 collectUnexpandedParameterPacks(Base->getTypeSourceInfo()->getTypeLoc(),
1668 Unexpanded);
1669 bool ShouldExpand = false;
Douglas Gregord3731192011-01-10 07:32:04 +00001670 bool RetainExpansion = false;
Douglas Gregorcded4f62011-01-14 17:04:44 +00001671 llvm::Optional<unsigned> NumExpansions;
Douglas Gregorf90b27a2011-01-03 22:36:02 +00001672 if (CheckParameterPacksForExpansion(Base->getEllipsisLoc(),
1673 Base->getSourceRange(),
David Blaikiea71f9d02011-09-22 02:34:54 +00001674 Unexpanded,
Douglas Gregorf90b27a2011-01-03 22:36:02 +00001675 TemplateArgs, ShouldExpand,
Douglas Gregord3731192011-01-10 07:32:04 +00001676 RetainExpansion,
Douglas Gregorf90b27a2011-01-03 22:36:02 +00001677 NumExpansions)) {
Douglas Gregorf90b27a2011-01-03 22:36:02 +00001678 Invalid = true;
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00001679 continue;
Douglas Gregorf90b27a2011-01-03 22:36:02 +00001680 }
1681
1682 // If we should expand this pack expansion now, do so.
1683 if (ShouldExpand) {
Douglas Gregorcded4f62011-01-14 17:04:44 +00001684 for (unsigned I = 0; I != *NumExpansions; ++I) {
Douglas Gregorf90b27a2011-01-03 22:36:02 +00001685 Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(*this, I);
1686
1687 TypeSourceInfo *BaseTypeLoc = SubstType(Base->getTypeSourceInfo(),
1688 TemplateArgs,
1689 Base->getSourceRange().getBegin(),
1690 DeclarationName());
1691 if (!BaseTypeLoc) {
1692 Invalid = true;
1693 continue;
1694 }
1695
1696 if (CXXBaseSpecifier *InstantiatedBase
1697 = CheckBaseSpecifier(Instantiation,
1698 Base->getSourceRange(),
1699 Base->isVirtual(),
1700 Base->getAccessSpecifierAsWritten(),
1701 BaseTypeLoc,
1702 SourceLocation()))
1703 InstantiatedBases.push_back(InstantiatedBase);
1704 else
1705 Invalid = true;
1706 }
1707
1708 continue;
1709 }
1710
1711 // The resulting base specifier will (still) be a pack expansion.
1712 EllipsisLoc = Base->getEllipsisLoc();
Douglas Gregor406f98f2011-03-02 02:04:06 +00001713 Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(*this, -1);
1714 BaseTypeLoc = SubstType(Base->getTypeSourceInfo(),
1715 TemplateArgs,
1716 Base->getSourceRange().getBegin(),
1717 DeclarationName());
1718 } else {
1719 BaseTypeLoc = SubstType(Base->getTypeSourceInfo(),
1720 TemplateArgs,
1721 Base->getSourceRange().getBegin(),
1722 DeclarationName());
Douglas Gregorf90b27a2011-01-03 22:36:02 +00001723 }
1724
Nick Lewycky56062202010-07-26 16:56:01 +00001725 if (!BaseTypeLoc) {
Douglas Gregor2943aed2009-03-03 04:44:36 +00001726 Invalid = true;
1727 continue;
1728 }
1729
1730 if (CXXBaseSpecifier *InstantiatedBase
Douglas Gregord475b8d2009-03-25 21:17:03 +00001731 = CheckBaseSpecifier(Instantiation,
Douglas Gregor2943aed2009-03-03 04:44:36 +00001732 Base->getSourceRange(),
1733 Base->isVirtual(),
1734 Base->getAccessSpecifierAsWritten(),
Douglas Gregorf90b27a2011-01-03 22:36:02 +00001735 BaseTypeLoc,
1736 EllipsisLoc))
Douglas Gregor2943aed2009-03-03 04:44:36 +00001737 InstantiatedBases.push_back(InstantiatedBase);
1738 else
1739 Invalid = true;
1740 }
1741
Douglas Gregor27b152f2009-03-10 18:52:44 +00001742 if (!Invalid &&
Jay Foadbeaaccd2009-05-21 09:52:38 +00001743 AttachBaseSpecifiers(Instantiation, InstantiatedBases.data(),
Douglas Gregor2943aed2009-03-03 04:44:36 +00001744 InstantiatedBases.size()))
1745 Invalid = true;
1746
1747 return Invalid;
1748}
1749
DeLesley Hutchins23323e02012-01-20 22:50:54 +00001750// Defined via #include from SemaTemplateInstantiateDecl.cpp
Benjamin Kramer5bbc3852012-02-06 11:13:08 +00001751namespace clang {
1752 namespace sema {
1753 Attr *instantiateTemplateAttribute(const Attr *At, ASTContext &C, Sema &S,
1754 const MultiLevelTemplateArgumentList &TemplateArgs);
1755 }
1756}
DeLesley Hutchins23323e02012-01-20 22:50:54 +00001757
Richard Smithf1c66b42012-03-14 23:13:10 +00001758/// Determine whether we would be unable to instantiate this template (because
1759/// it either has no definition, or is in the process of being instantiated).
1760static bool DiagnoseUninstantiableTemplate(Sema &S,
1761 SourceLocation PointOfInstantiation,
1762 TagDecl *Instantiation,
1763 bool InstantiatedFromMember,
1764 TagDecl *Pattern,
1765 TagDecl *PatternDef,
1766 TemplateSpecializationKind TSK,
1767 bool Complain = true) {
1768 if (PatternDef && !PatternDef->isBeingDefined())
1769 return false;
1770
1771 if (!Complain || (PatternDef && PatternDef->isInvalidDecl())) {
1772 // Say nothing
1773 } else if (PatternDef) {
1774 assert(PatternDef->isBeingDefined());
1775 S.Diag(PointOfInstantiation,
1776 diag::err_template_instantiate_within_definition)
1777 << (TSK != TSK_ImplicitInstantiation)
1778 << S.Context.getTypeDeclType(Instantiation);
1779 // Not much point in noting the template declaration here, since
1780 // we're lexically inside it.
1781 Instantiation->setInvalidDecl();
1782 } else if (InstantiatedFromMember) {
1783 S.Diag(PointOfInstantiation,
1784 diag::err_implicit_instantiate_member_undefined)
1785 << S.Context.getTypeDeclType(Instantiation);
1786 S.Diag(Pattern->getLocation(), diag::note_member_of_template_here);
1787 } else {
1788 S.Diag(PointOfInstantiation, diag::err_template_instantiate_undefined)
1789 << (TSK != TSK_ImplicitInstantiation)
1790 << S.Context.getTypeDeclType(Instantiation);
1791 S.Diag(Pattern->getLocation(), diag::note_template_decl_here);
1792 }
1793
1794 // In general, Instantiation isn't marked invalid to get more than one
1795 // error for multiple undefined instantiations. But the code that does
1796 // explicit declaration -> explicit definition conversion can't handle
1797 // invalid declarations, so mark as invalid in that case.
1798 if (TSK == TSK_ExplicitInstantiationDeclaration)
1799 Instantiation->setInvalidDecl();
1800 return true;
1801}
1802
Douglas Gregord475b8d2009-03-25 21:17:03 +00001803/// \brief Instantiate the definition of a class from a given pattern.
1804///
1805/// \param PointOfInstantiation The point of instantiation within the
1806/// source code.
1807///
1808/// \param Instantiation is the declaration whose definition is being
1809/// instantiated. This will be either a class template specialization
1810/// or a member class of a class template specialization.
1811///
1812/// \param Pattern is the pattern from which the instantiation
1813/// occurs. This will be either the declaration of a class template or
1814/// the declaration of a member class of a class template.
1815///
1816/// \param TemplateArgs The template arguments to be substituted into
1817/// the pattern.
1818///
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001819/// \param TSK the kind of implicit or explicit instantiation to perform.
Douglas Gregor5842ba92009-08-24 15:23:48 +00001820///
1821/// \param Complain whether to complain if the class cannot be instantiated due
1822/// to the lack of a definition.
1823///
Douglas Gregord475b8d2009-03-25 21:17:03 +00001824/// \returns true if an error occurred, false otherwise.
1825bool
1826Sema::InstantiateClass(SourceLocation PointOfInstantiation,
1827 CXXRecordDecl *Instantiation, CXXRecordDecl *Pattern,
Douglas Gregord6350ae2009-08-28 20:31:08 +00001828 const MultiLevelTemplateArgumentList &TemplateArgs,
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001829 TemplateSpecializationKind TSK,
Douglas Gregor5842ba92009-08-24 15:23:48 +00001830 bool Complain) {
Douglas Gregord475b8d2009-03-25 21:17:03 +00001831 bool Invalid = false;
John McCalle29ba202009-08-20 01:44:21 +00001832
Mike Stump1eb44332009-09-09 15:08:12 +00001833 CXXRecordDecl *PatternDef
Douglas Gregor952b0172010-02-11 01:04:33 +00001834 = cast_or_null<CXXRecordDecl>(Pattern->getDefinition());
Richard Smithf1c66b42012-03-14 23:13:10 +00001835 if (DiagnoseUninstantiableTemplate(*this, PointOfInstantiation, Instantiation,
1836 Instantiation->getInstantiatedFromMemberClass(),
1837 Pattern, PatternDef, TSK, Complain))
Douglas Gregord475b8d2009-03-25 21:17:03 +00001838 return true;
Douglas Gregord475b8d2009-03-25 21:17:03 +00001839 Pattern = PatternDef;
1840
Douglas Gregor454885e2009-10-15 15:54:05 +00001841 // \brief Record the point of instantiation.
1842 if (MemberSpecializationInfo *MSInfo
1843 = Instantiation->getMemberSpecializationInfo()) {
1844 MSInfo->setTemplateSpecializationKind(TSK);
1845 MSInfo->setPointOfInstantiation(PointOfInstantiation);
Douglas Gregor972e6ce2009-10-27 06:26:26 +00001846 } else if (ClassTemplateSpecializationDecl *Spec
Nico Weberc7feca02011-12-20 20:32:49 +00001847 = dyn_cast<ClassTemplateSpecializationDecl>(Instantiation)) {
Douglas Gregor972e6ce2009-10-27 06:26:26 +00001848 Spec->setTemplateSpecializationKind(TSK);
1849 Spec->setPointOfInstantiation(PointOfInstantiation);
Douglas Gregor454885e2009-10-15 15:54:05 +00001850 }
1851
Douglas Gregord048bb72009-03-25 21:23:52 +00001852 InstantiatingTemplate Inst(*this, PointOfInstantiation, Instantiation);
Douglas Gregord475b8d2009-03-25 21:17:03 +00001853 if (Inst)
1854 return true;
1855
1856 // Enter the scope of this instantiation. We don't use
1857 // PushDeclContext because we don't have a scope.
John McCallf5813822010-04-29 00:35:03 +00001858 ContextRAII SavedContext(*this, Instantiation);
Douglas Gregor9679caf2010-05-12 17:27:19 +00001859 EnterExpressionEvaluationContext EvalContext(*this,
John McCallf312b1e2010-08-26 23:41:50 +00001860 Sema::PotentiallyEvaluated);
Douglas Gregord475b8d2009-03-25 21:17:03 +00001861
Douglas Gregor05030bb2010-03-24 01:33:17 +00001862 // If this is an instantiation of a local class, merge this local
1863 // instantiation scope with the enclosing scope. Otherwise, every
1864 // instantiation of a class has its own local instantiation scope.
1865 bool MergeWithParentScope = !Instantiation->isDefinedOutsideFunctionOrMethod();
John McCall2a7fb272010-08-25 05:32:35 +00001866 LocalInstantiationScope Scope(*this, MergeWithParentScope);
Douglas Gregor05030bb2010-03-24 01:33:17 +00001867
John McCall1d8d1cc2010-08-01 02:01:53 +00001868 // Pull attributes from the pattern onto the instantiation.
1869 InstantiateAttrs(TemplateArgs, Pattern, Instantiation);
1870
Douglas Gregord475b8d2009-03-25 21:17:03 +00001871 // Start the definition of this instantiation.
1872 Instantiation->startDefinition();
Douglas Gregor13c85772010-05-06 00:28:52 +00001873
1874 Instantiation->setTagKind(Pattern->getTagKind());
Douglas Gregord475b8d2009-03-25 21:17:03 +00001875
John McCallce3ff2b2009-08-25 22:02:44 +00001876 // Do substitution on the base class specifiers.
1877 if (SubstBaseSpecifiers(Instantiation, Pattern, TemplateArgs))
Douglas Gregord475b8d2009-03-25 21:17:03 +00001878 Invalid = true;
1879
Douglas Gregord65587f2010-11-10 19:44:59 +00001880 TemplateDeclInstantiator Instantiator(*this, Instantiation, TemplateArgs);
Chris Lattner5f9e2722011-07-23 10:55:15 +00001881 SmallVector<Decl*, 4> Fields;
1882 SmallVector<std::pair<FieldDecl*, FieldDecl*>, 4>
Richard Smith7a614d82011-06-11 17:19:42 +00001883 FieldsWithMemberInitializers;
DeLesley Hutchins23323e02012-01-20 22:50:54 +00001884 // Delay instantiation of late parsed attributes.
1885 LateInstantiatedAttrVec LateAttrs;
1886 Instantiator.enableLateAttributeInstantiation(&LateAttrs);
1887
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001888 for (RecordDecl::decl_iterator Member = Pattern->decls_begin(),
Mike Stump1eb44332009-09-09 15:08:12 +00001889 MemberEnd = Pattern->decls_end();
Douglas Gregor6ab35242009-04-09 21:40:53 +00001890 Member != MemberEnd; ++Member) {
Argyrios Kyrtzidisbb5e4312010-11-04 03:18:57 +00001891 // Don't instantiate members not belonging in this semantic context.
1892 // e.g. for:
1893 // @code
1894 // template <int i> class A {
1895 // class B *g;
1896 // };
1897 // @endcode
1898 // 'class B' has the template as lexical context but semantically it is
1899 // introduced in namespace scope.
1900 if ((*Member)->getDeclContext() != Pattern)
1901 continue;
1902
Douglas Gregord65587f2010-11-10 19:44:59 +00001903 if ((*Member)->isInvalidDecl()) {
1904 Invalid = true;
1905 continue;
1906 }
1907
1908 Decl *NewMember = Instantiator.Visit(*Member);
Douglas Gregord475b8d2009-03-25 21:17:03 +00001909 if (NewMember) {
Richard Smith7a614d82011-06-11 17:19:42 +00001910 if (FieldDecl *Field = dyn_cast<FieldDecl>(NewMember)) {
John McCalld226f652010-08-21 09:40:31 +00001911 Fields.push_back(Field);
Richard Smith7a614d82011-06-11 17:19:42 +00001912 FieldDecl *OldField = cast<FieldDecl>(*Member);
1913 if (OldField->getInClassInitializer())
1914 FieldsWithMemberInitializers.push_back(std::make_pair(OldField,
1915 Field));
Richard Smith1af83c42012-03-23 03:33:32 +00001916 } else if (EnumDecl *Enum = dyn_cast<EnumDecl>(NewMember)) {
1917 // C++11 [temp.inst]p1: The implicit instantiation of a class template
1918 // specialization causes the implicit instantiation of the definitions
1919 // of unscoped member enumerations.
1920 // Record a point of instantiation for this implicit instantiation.
Richard Smith3343fad2012-03-23 23:09:08 +00001921 if (TSK == TSK_ImplicitInstantiation && !Enum->isScoped() &&
1922 Enum->isCompleteDefinition()) {
Richard Smith1af83c42012-03-23 03:33:32 +00001923 MemberSpecializationInfo *MSInfo =Enum->getMemberSpecializationInfo();
1924 assert(MSInfo && "no spec info for member enum specialization");
1925 MSInfo->setTemplateSpecializationKind(TSK_ImplicitInstantiation);
1926 MSInfo->setPointOfInstantiation(PointOfInstantiation);
1927 }
1928 }
1929
1930 if (NewMember->isInvalidDecl())
Eli Friedman721e77d2009-12-07 00:22:08 +00001931 Invalid = true;
Douglas Gregord475b8d2009-03-25 21:17:03 +00001932 } else {
1933 // FIXME: Eventually, a NULL return will mean that one of the
Mike Stump390b4cc2009-05-16 07:39:55 +00001934 // instantiations was a semantic disaster, and we'll want to set Invalid =
1935 // true. For now, we expect to skip some members that we can't yet handle.
Douglas Gregord475b8d2009-03-25 21:17:03 +00001936 }
1937 }
1938
1939 // Finish checking fields.
David Blaikie77b6de02011-09-22 02:58:26 +00001940 ActOnFields(0, Instantiation->getLocation(), Instantiation, Fields,
1941 SourceLocation(), SourceLocation(), 0);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001942 CheckCompletedCXXClass(Instantiation);
Richard Smith7a614d82011-06-11 17:19:42 +00001943
1944 // Attach any in-class member initializers now the class is complete.
Benjamin Kramer268efba2012-05-17 12:01:52 +00001945 if (!FieldsWithMemberInitializers.empty()) {
Douglas Gregorcefc3af2012-04-16 07:05:22 +00001946 // C++11 [expr.prim.general]p4:
1947 // Otherwise, if a member-declarator declares a non-static data member
1948 // (9.2) of a class X, the expression this is a prvalue of type "pointer
1949 // to X" within the optional brace-or-equal-initializer. It shall not
1950 // appear elsewhere in the member-declarator.
1951 CXXThisScopeRAII ThisScope(*this, Instantiation, (unsigned)0);
1952
1953 for (unsigned I = 0, N = FieldsWithMemberInitializers.size(); I != N; ++I) {
1954 FieldDecl *OldField = FieldsWithMemberInitializers[I].first;
1955 FieldDecl *NewField = FieldsWithMemberInitializers[I].second;
1956 Expr *OldInit = OldField->getInClassInitializer();
Richard Smith7a614d82011-06-11 17:19:42 +00001957
Douglas Gregorcefc3af2012-04-16 07:05:22 +00001958 ExprResult NewInit = SubstInitializer(OldInit, TemplateArgs,
1959 /*CXXDirectInit=*/false);
1960 if (NewInit.isInvalid())
1961 NewField->setInvalidDecl();
1962 else {
1963 Expr *Init = NewInit.take();
1964 assert(Init && "no-argument initializer in class");
1965 assert(!isa<ParenListExpr>(Init) && "call-style init in class");
Richard Smithca523302012-06-10 03:12:00 +00001966 ActOnCXXInClassMemberInitializer(NewField, Init->getLocStart(), Init);
Douglas Gregorcefc3af2012-04-16 07:05:22 +00001967 }
Richard Smith0ff6f8f2011-07-20 00:12:52 +00001968 }
Richard Smith7a614d82011-06-11 17:19:42 +00001969 }
DeLesley Hutchins23323e02012-01-20 22:50:54 +00001970 // Instantiate late parsed attributes, and attach them to their decls.
1971 // See Sema::InstantiateAttrs
1972 for (LateInstantiatedAttrVec::iterator I = LateAttrs.begin(),
1973 E = LateAttrs.end(); I != E; ++I) {
1974 assert(CurrentInstantiationScope == Instantiator.getStartingScope());
1975 CurrentInstantiationScope = I->Scope;
1976 Attr *NewAttr =
1977 instantiateTemplateAttribute(I->TmplAttr, Context, *this, TemplateArgs);
1978 I->NewDecl->addAttr(NewAttr);
1979 LocalInstantiationScope::deleteScopes(I->Scope,
1980 Instantiator.getStartingScope());
1981 }
1982 Instantiator.disableLateAttributeInstantiation();
1983 LateAttrs.clear();
1984
Richard Smith7a614d82011-06-11 17:19:42 +00001985 if (!FieldsWithMemberInitializers.empty())
1986 ActOnFinishDelayedMemberInitializers(Instantiation);
1987
Abramo Bagnarae9946242011-11-18 08:08:52 +00001988 if (TSK == TSK_ImplicitInstantiation) {
Argyrios Kyrtzidis734bd6e2012-02-11 01:59:57 +00001989 Instantiation->setLocation(Pattern->getLocation());
Abramo Bagnarae9946242011-11-18 08:08:52 +00001990 Instantiation->setLocStart(Pattern->getInnerLocStart());
Abramo Bagnara09d82122011-10-03 20:34:03 +00001991 Instantiation->setRBraceLoc(Pattern->getRBraceLoc());
Abramo Bagnarae9946242011-11-18 08:08:52 +00001992 }
Abramo Bagnara09d82122011-10-03 20:34:03 +00001993
Douglas Gregor663b5a02009-10-14 20:14:33 +00001994 if (Instantiation->isInvalidDecl())
1995 Invalid = true;
Douglas Gregord65587f2010-11-10 19:44:59 +00001996 else {
1997 // Instantiate any out-of-line class template partial
1998 // specializations now.
1999 for (TemplateDeclInstantiator::delayed_partial_spec_iterator
2000 P = Instantiator.delayed_partial_spec_begin(),
2001 PEnd = Instantiator.delayed_partial_spec_end();
2002 P != PEnd; ++P) {
2003 if (!Instantiator.InstantiateClassTemplatePartialSpecialization(
2004 P->first,
2005 P->second)) {
2006 Invalid = true;
2007 break;
2008 }
2009 }
2010 }
2011
Douglas Gregord475b8d2009-03-25 21:17:03 +00002012 // Exit the scope of this instantiation.
John McCallf5813822010-04-29 00:35:03 +00002013 SavedContext.pop();
Douglas Gregord475b8d2009-03-25 21:17:03 +00002014
Douglas Gregor6fb745b2010-05-13 16:44:06 +00002015 if (!Invalid) {
Douglas Gregoraba43bb2009-05-26 20:50:29 +00002016 Consumer.HandleTagDeclDefinition(Instantiation);
2017
Douglas Gregor6fb745b2010-05-13 16:44:06 +00002018 // Always emit the vtable for an explicit instantiation definition
2019 // of a polymorphic class template specialization.
2020 if (TSK == TSK_ExplicitInstantiationDefinition)
2021 MarkVTableUsed(PointOfInstantiation, Instantiation, true);
2022 }
2023
Douglas Gregord475b8d2009-03-25 21:17:03 +00002024 return Invalid;
2025}
2026
Richard Smithf1c66b42012-03-14 23:13:10 +00002027/// \brief Instantiate the definition of an enum from a given pattern.
2028///
2029/// \param PointOfInstantiation The point of instantiation within the
2030/// source code.
2031/// \param Instantiation is the declaration whose definition is being
2032/// instantiated. This will be a member enumeration of a class
2033/// temploid specialization, or a local enumeration within a
2034/// function temploid specialization.
2035/// \param Pattern The templated declaration from which the instantiation
2036/// occurs.
2037/// \param TemplateArgs The template arguments to be substituted into
2038/// the pattern.
2039/// \param TSK The kind of implicit or explicit instantiation to perform.
2040///
2041/// \return \c true if an error occurred, \c false otherwise.
2042bool Sema::InstantiateEnum(SourceLocation PointOfInstantiation,
2043 EnumDecl *Instantiation, EnumDecl *Pattern,
2044 const MultiLevelTemplateArgumentList &TemplateArgs,
2045 TemplateSpecializationKind TSK) {
2046 EnumDecl *PatternDef = Pattern->getDefinition();
2047 if (DiagnoseUninstantiableTemplate(*this, PointOfInstantiation, Instantiation,
2048 Instantiation->getInstantiatedFromMemberEnum(),
2049 Pattern, PatternDef, TSK,/*Complain*/true))
2050 return true;
2051 Pattern = PatternDef;
2052
2053 // Record the point of instantiation.
2054 if (MemberSpecializationInfo *MSInfo
2055 = Instantiation->getMemberSpecializationInfo()) {
2056 MSInfo->setTemplateSpecializationKind(TSK);
2057 MSInfo->setPointOfInstantiation(PointOfInstantiation);
2058 }
2059
2060 InstantiatingTemplate Inst(*this, PointOfInstantiation, Instantiation);
2061 if (Inst)
2062 return true;
2063
2064 // Enter the scope of this instantiation. We don't use
2065 // PushDeclContext because we don't have a scope.
2066 ContextRAII SavedContext(*this, Instantiation);
2067 EnterExpressionEvaluationContext EvalContext(*this,
2068 Sema::PotentiallyEvaluated);
2069
2070 LocalInstantiationScope Scope(*this, /*MergeWithParentScope*/true);
2071
2072 // Pull attributes from the pattern onto the instantiation.
2073 InstantiateAttrs(TemplateArgs, Pattern, Instantiation);
2074
2075 TemplateDeclInstantiator Instantiator(*this, Instantiation, TemplateArgs);
2076 Instantiator.InstantiateEnumDefinition(Instantiation, Pattern);
2077
2078 // Exit the scope of this instantiation.
2079 SavedContext.pop();
2080
2081 return Instantiation->isInvalidDecl();
2082}
2083
Douglas Gregor9b623632010-10-12 23:32:35 +00002084namespace {
2085 /// \brief A partial specialization whose template arguments have matched
2086 /// a given template-id.
2087 struct PartialSpecMatchResult {
2088 ClassTemplatePartialSpecializationDecl *Partial;
2089 TemplateArgumentList *Args;
Douglas Gregor9b623632010-10-12 23:32:35 +00002090 };
2091}
2092
Mike Stump1eb44332009-09-09 15:08:12 +00002093bool
Douglas Gregor2943aed2009-03-03 04:44:36 +00002094Sema::InstantiateClassTemplateSpecialization(
Douglas Gregor972e6ce2009-10-27 06:26:26 +00002095 SourceLocation PointOfInstantiation,
Douglas Gregor2943aed2009-03-03 04:44:36 +00002096 ClassTemplateSpecializationDecl *ClassTemplateSpec,
Douglas Gregord0e3daf2009-09-04 22:48:11 +00002097 TemplateSpecializationKind TSK,
Douglas Gregor5842ba92009-08-24 15:23:48 +00002098 bool Complain) {
Douglas Gregor2943aed2009-03-03 04:44:36 +00002099 // Perform the actual instantiation on the canonical declaration.
2100 ClassTemplateSpec = cast<ClassTemplateSpecializationDecl>(
Argyrios Kyrtzidis97fbaa22009-07-18 00:34:25 +00002101 ClassTemplateSpec->getCanonicalDecl());
Douglas Gregor2943aed2009-03-03 04:44:36 +00002102
Douglas Gregor52604ab2009-09-11 21:19:12 +00002103 // Check whether we have already instantiated or specialized this class
2104 // template specialization.
2105 if (ClassTemplateSpec->getSpecializationKind() != TSK_Undeclared) {
2106 if (ClassTemplateSpec->getSpecializationKind() ==
2107 TSK_ExplicitInstantiationDeclaration &&
2108 TSK == TSK_ExplicitInstantiationDefinition) {
2109 // An explicit instantiation definition follows an explicit instantiation
2110 // declaration (C++0x [temp.explicit]p10); go ahead and perform the
2111 // explicit instantiation.
2112 ClassTemplateSpec->setSpecializationKind(TSK);
Douglas Gregor6fb745b2010-05-13 16:44:06 +00002113
2114 // If this is an explicit instantiation definition, mark the
2115 // vtable as used.
Nico Weberc7feca02011-12-20 20:32:49 +00002116 if (TSK == TSK_ExplicitInstantiationDefinition &&
2117 !ClassTemplateSpec->isInvalidDecl())
Douglas Gregor6fb745b2010-05-13 16:44:06 +00002118 MarkVTableUsed(PointOfInstantiation, ClassTemplateSpec, true);
2119
Douglas Gregor52604ab2009-09-11 21:19:12 +00002120 return false;
2121 }
2122
2123 // We can only instantiate something that hasn't already been
2124 // instantiated or specialized. Fail without any diagnostics: our
2125 // caller will provide an error message.
Douglas Gregor2943aed2009-03-03 04:44:36 +00002126 return true;
Douglas Gregor52604ab2009-09-11 21:19:12 +00002127 }
Douglas Gregor2943aed2009-03-03 04:44:36 +00002128
Douglas Gregor9eea08b2009-09-15 16:51:42 +00002129 if (ClassTemplateSpec->isInvalidDecl())
2130 return true;
2131
Douglas Gregor2943aed2009-03-03 04:44:36 +00002132 ClassTemplateDecl *Template = ClassTemplateSpec->getSpecializedTemplate();
Douglas Gregord6350ae2009-08-28 20:31:08 +00002133 CXXRecordDecl *Pattern = 0;
Douglas Gregorc8ab2562009-05-31 09:31:02 +00002134
Douglas Gregorc1efb3f2009-06-12 22:31:52 +00002135 // C++ [temp.class.spec.match]p1:
2136 // When a class template is used in a context that requires an
2137 // instantiation of the class, it is necessary to determine
2138 // whether the instantiation is to be generated using the primary
2139 // template or one of the partial specializations. This is done by
2140 // matching the template arguments of the class template
2141 // specialization with the template argument lists of the partial
2142 // specializations.
Douglas Gregor9b623632010-10-12 23:32:35 +00002143 typedef PartialSpecMatchResult MatchResult;
Chris Lattner5f9e2722011-07-23 10:55:15 +00002144 SmallVector<MatchResult, 4> Matched;
2145 SmallVector<ClassTemplatePartialSpecializationDecl *, 4> PartialSpecs;
Douglas Gregordc60c1e2010-04-30 05:56:50 +00002146 Template->getPartialSpecializations(PartialSpecs);
2147 for (unsigned I = 0, N = PartialSpecs.size(); I != N; ++I) {
2148 ClassTemplatePartialSpecializationDecl *Partial = PartialSpecs[I];
John McCall5769d612010-02-08 23:07:23 +00002149 TemplateDeductionInfo Info(Context, PointOfInstantiation);
Douglas Gregorf67875d2009-06-12 18:26:56 +00002150 if (TemplateDeductionResult Result
Douglas Gregordc60c1e2010-04-30 05:56:50 +00002151 = DeduceTemplateArguments(Partial,
Douglas Gregorf67875d2009-06-12 18:26:56 +00002152 ClassTemplateSpec->getTemplateArgs(),
2153 Info)) {
2154 // FIXME: Store the failed-deduction information for use in
2155 // diagnostics, later.
2156 (void)Result;
2157 } else {
Douglas Gregor9b623632010-10-12 23:32:35 +00002158 Matched.push_back(PartialSpecMatchResult());
2159 Matched.back().Partial = Partial;
2160 Matched.back().Args = Info.take();
Douglas Gregorf67875d2009-06-12 18:26:56 +00002161 }
Douglas Gregorc8ab2562009-05-31 09:31:02 +00002162 }
2163
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002164 // If we're dealing with a member template where the template parameters
2165 // have been instantiated, this provides the original template parameters
2166 // from which the member template's parameters were instantiated.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002167 SmallVector<const NamedDecl *, 4> InstantiatedTemplateParameters;
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002168
Douglas Gregored9c0f92009-10-29 00:04:11 +00002169 if (Matched.size() >= 1) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00002170 SmallVector<MatchResult, 4>::iterator Best = Matched.begin();
Douglas Gregored9c0f92009-10-29 00:04:11 +00002171 if (Matched.size() == 1) {
2172 // -- If exactly one matching specialization is found, the
2173 // instantiation is generated from that specialization.
2174 // We don't need to do anything for this.
2175 } else {
2176 // -- If more than one matching specialization is found, the
2177 // partial order rules (14.5.4.2) are used to determine
2178 // whether one of the specializations is more specialized
2179 // than the others. If none of the specializations is more
2180 // specialized than all of the other matching
2181 // specializations, then the use of the class template is
2182 // ambiguous and the program is ill-formed.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002183 for (SmallVector<MatchResult, 4>::iterator P = Best + 1,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002184 PEnd = Matched.end();
2185 P != PEnd; ++P) {
Douglas Gregor9b623632010-10-12 23:32:35 +00002186 if (getMoreSpecializedPartialSpecialization(P->Partial, Best->Partial,
John McCall5769d612010-02-08 23:07:23 +00002187 PointOfInstantiation)
Douglas Gregor9b623632010-10-12 23:32:35 +00002188 == P->Partial)
Douglas Gregored9c0f92009-10-29 00:04:11 +00002189 Best = P;
Douglas Gregorbf4ea562009-09-15 16:23:51 +00002190 }
Douglas Gregorbf4ea562009-09-15 16:23:51 +00002191
Douglas Gregored9c0f92009-10-29 00:04:11 +00002192 // Determine if the best partial specialization is more specialized than
2193 // the others.
2194 bool Ambiguous = false;
Chris Lattner5f9e2722011-07-23 10:55:15 +00002195 for (SmallVector<MatchResult, 4>::iterator P = Matched.begin(),
Douglas Gregorbf4ea562009-09-15 16:23:51 +00002196 PEnd = Matched.end();
Douglas Gregored9c0f92009-10-29 00:04:11 +00002197 P != PEnd; ++P) {
2198 if (P != Best &&
Douglas Gregor9b623632010-10-12 23:32:35 +00002199 getMoreSpecializedPartialSpecialization(P->Partial, Best->Partial,
John McCall5769d612010-02-08 23:07:23 +00002200 PointOfInstantiation)
Douglas Gregor9b623632010-10-12 23:32:35 +00002201 != Best->Partial) {
Douglas Gregored9c0f92009-10-29 00:04:11 +00002202 Ambiguous = true;
2203 break;
2204 }
2205 }
2206
2207 if (Ambiguous) {
2208 // Partial ordering did not produce a clear winner. Complain.
2209 ClassTemplateSpec->setInvalidDecl();
2210 Diag(PointOfInstantiation, diag::err_partial_spec_ordering_ambiguous)
2211 << ClassTemplateSpec;
2212
2213 // Print the matching partial specializations.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002214 for (SmallVector<MatchResult, 4>::iterator P = Matched.begin(),
Douglas Gregored9c0f92009-10-29 00:04:11 +00002215 PEnd = Matched.end();
2216 P != PEnd; ++P)
Douglas Gregor9b623632010-10-12 23:32:35 +00002217 Diag(P->Partial->getLocation(), diag::note_partial_spec_match)
2218 << getTemplateArgumentBindingsText(
2219 P->Partial->getTemplateParameters(),
2220 *P->Args);
Douglas Gregord6350ae2009-08-28 20:31:08 +00002221
Douglas Gregored9c0f92009-10-29 00:04:11 +00002222 return true;
2223 }
Douglas Gregorbf4ea562009-09-15 16:23:51 +00002224 }
2225
2226 // Instantiate using the best class template partial specialization.
Douglas Gregor9b623632010-10-12 23:32:35 +00002227 ClassTemplatePartialSpecializationDecl *OrigPartialSpec = Best->Partial;
Douglas Gregored9c0f92009-10-29 00:04:11 +00002228 while (OrigPartialSpec->getInstantiatedFromMember()) {
2229 // If we've found an explicit specialization of this class template,
2230 // stop here and use that as the pattern.
2231 if (OrigPartialSpec->isMemberSpecialization())
2232 break;
2233
2234 OrigPartialSpec = OrigPartialSpec->getInstantiatedFromMember();
2235 }
2236
2237 Pattern = OrigPartialSpec;
Douglas Gregor9b623632010-10-12 23:32:35 +00002238 ClassTemplateSpec->setInstantiationOf(Best->Partial, Best->Args);
Douglas Gregorc1efb3f2009-06-12 22:31:52 +00002239 } else {
2240 // -- If no matches are found, the instantiation is generated
2241 // from the primary template.
Douglas Gregord6350ae2009-08-28 20:31:08 +00002242 ClassTemplateDecl *OrigTemplate = Template;
Douglas Gregorfd056bc2009-10-13 16:30:37 +00002243 while (OrigTemplate->getInstantiatedFromMemberTemplate()) {
2244 // If we've found an explicit specialization of this class template,
2245 // stop here and use that as the pattern.
2246 if (OrigTemplate->isMemberSpecialization())
2247 break;
2248
Douglas Gregord6350ae2009-08-28 20:31:08 +00002249 OrigTemplate = OrigTemplate->getInstantiatedFromMemberTemplate();
Douglas Gregorfd056bc2009-10-13 16:30:37 +00002250 }
2251
Douglas Gregord6350ae2009-08-28 20:31:08 +00002252 Pattern = OrigTemplate->getTemplatedDecl();
Douglas Gregorc8ab2562009-05-31 09:31:02 +00002253 }
Douglas Gregor2943aed2009-03-03 04:44:36 +00002254
Douglas Gregor972e6ce2009-10-27 06:26:26 +00002255 bool Result = InstantiateClass(PointOfInstantiation, ClassTemplateSpec,
2256 Pattern,
2257 getTemplateInstantiationArgs(ClassTemplateSpec),
Douglas Gregord0e3daf2009-09-04 22:48:11 +00002258 TSK,
Douglas Gregor5842ba92009-08-24 15:23:48 +00002259 Complain);
Mike Stump1eb44332009-09-09 15:08:12 +00002260
Douglas Gregor199d9912009-06-05 00:53:49 +00002261 return Result;
Douglas Gregor2943aed2009-03-03 04:44:36 +00002262}
Douglas Gregor5953d8b2009-03-19 17:26:29 +00002263
John McCallce3ff2b2009-08-25 22:02:44 +00002264/// \brief Instantiates the definitions of all of the member
2265/// of the given class, which is an instantiation of a class template
2266/// or a member class of a template.
Douglas Gregora58861f2009-05-13 20:28:22 +00002267void
2268Sema::InstantiateClassMembers(SourceLocation PointOfInstantiation,
Douglas Gregord0e3daf2009-09-04 22:48:11 +00002269 CXXRecordDecl *Instantiation,
2270 const MultiLevelTemplateArgumentList &TemplateArgs,
2271 TemplateSpecializationKind TSK) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002272 for (DeclContext::decl_iterator D = Instantiation->decls_begin(),
2273 DEnd = Instantiation->decls_end();
Douglas Gregora58861f2009-05-13 20:28:22 +00002274 D != DEnd; ++D) {
Douglas Gregor0d035142009-10-27 18:42:08 +00002275 bool SuppressNew = false;
Douglas Gregora58861f2009-05-13 20:28:22 +00002276 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(*D)) {
Douglas Gregor0d035142009-10-27 18:42:08 +00002277 if (FunctionDecl *Pattern
2278 = Function->getInstantiatedFromMemberFunction()) {
2279 MemberSpecializationInfo *MSInfo
2280 = Function->getMemberSpecializationInfo();
2281 assert(MSInfo && "No member specialization information?");
Douglas Gregorc42b6522010-04-09 21:02:29 +00002282 if (MSInfo->getTemplateSpecializationKind()
2283 == TSK_ExplicitSpecialization)
2284 continue;
2285
Douglas Gregor0d035142009-10-27 18:42:08 +00002286 if (CheckSpecializationInstantiationRedecl(PointOfInstantiation, TSK,
2287 Function,
2288 MSInfo->getTemplateSpecializationKind(),
Nico Weberc956b6e2010-09-27 21:02:09 +00002289 MSInfo->getPointOfInstantiation(),
Douglas Gregor0d035142009-10-27 18:42:08 +00002290 SuppressNew) ||
2291 SuppressNew)
Douglas Gregorf6b11852009-10-08 15:14:33 +00002292 continue;
2293
Sean Hunt10620eb2011-05-06 20:44:56 +00002294 if (Function->isDefined())
Douglas Gregor0d035142009-10-27 18:42:08 +00002295 continue;
2296
2297 if (TSK == TSK_ExplicitInstantiationDefinition) {
2298 // C++0x [temp.explicit]p8:
2299 // An explicit instantiation definition that names a class template
2300 // specialization explicitly instantiates the class template
2301 // specialization and is only an explicit instantiation definition
2302 // of members whose definition is visible at the point of
2303 // instantiation.
Sean Hunt10620eb2011-05-06 20:44:56 +00002304 if (!Pattern->isDefined())
Douglas Gregor0d035142009-10-27 18:42:08 +00002305 continue;
2306
2307 Function->setTemplateSpecializationKind(TSK, PointOfInstantiation);
2308
2309 InstantiateFunctionDefinition(PointOfInstantiation, Function);
2310 } else {
2311 Function->setTemplateSpecializationKind(TSK, PointOfInstantiation);
2312 }
Douglas Gregorf6b11852009-10-08 15:14:33 +00002313 }
Douglas Gregora58861f2009-05-13 20:28:22 +00002314 } else if (VarDecl *Var = dyn_cast<VarDecl>(*D)) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00002315 if (Var->isStaticDataMember()) {
Douglas Gregor0d035142009-10-27 18:42:08 +00002316 MemberSpecializationInfo *MSInfo = Var->getMemberSpecializationInfo();
2317 assert(MSInfo && "No member specialization information?");
Douglas Gregorc42b6522010-04-09 21:02:29 +00002318 if (MSInfo->getTemplateSpecializationKind()
2319 == TSK_ExplicitSpecialization)
2320 continue;
2321
Douglas Gregor0d035142009-10-27 18:42:08 +00002322 if (CheckSpecializationInstantiationRedecl(PointOfInstantiation, TSK,
2323 Var,
2324 MSInfo->getTemplateSpecializationKind(),
Nico Weberc956b6e2010-09-27 21:02:09 +00002325 MSInfo->getPointOfInstantiation(),
Douglas Gregor0d035142009-10-27 18:42:08 +00002326 SuppressNew) ||
2327 SuppressNew)
Douglas Gregorf6b11852009-10-08 15:14:33 +00002328 continue;
2329
Douglas Gregor0d035142009-10-27 18:42:08 +00002330 if (TSK == TSK_ExplicitInstantiationDefinition) {
2331 // C++0x [temp.explicit]p8:
2332 // An explicit instantiation definition that names a class template
2333 // specialization explicitly instantiates the class template
2334 // specialization and is only an explicit instantiation definition
2335 // of members whose definition is visible at the point of
2336 // instantiation.
2337 if (!Var->getInstantiatedFromStaticDataMember()
2338 ->getOutOfLineDefinition())
2339 continue;
2340
2341 Var->setTemplateSpecializationKind(TSK, PointOfInstantiation);
Douglas Gregor251b4ff2009-10-08 07:24:58 +00002342 InstantiateStaticDataMemberDefinition(PointOfInstantiation, Var);
Douglas Gregor0d035142009-10-27 18:42:08 +00002343 } else {
2344 Var->setTemplateSpecializationKind(TSK, PointOfInstantiation);
2345 }
2346 }
Douglas Gregora58861f2009-05-13 20:28:22 +00002347 } else if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(*D)) {
Douglas Gregora77eaa92010-04-18 18:11:38 +00002348 // Always skip the injected-class-name, along with any
2349 // redeclarations of nested classes, since both would cause us
2350 // to try to instantiate the members of a class twice.
Douglas Gregoref96ee02012-01-14 16:38:05 +00002351 if (Record->isInjectedClassName() || Record->getPreviousDecl())
Douglas Gregor2db32322009-10-07 23:56:10 +00002352 continue;
2353
Douglas Gregor0d035142009-10-27 18:42:08 +00002354 MemberSpecializationInfo *MSInfo = Record->getMemberSpecializationInfo();
2355 assert(MSInfo && "No member specialization information?");
Douglas Gregorc42b6522010-04-09 21:02:29 +00002356
2357 if (MSInfo->getTemplateSpecializationKind()
2358 == TSK_ExplicitSpecialization)
2359 continue;
Nico Weberc956b6e2010-09-27 21:02:09 +00002360
Douglas Gregor0d035142009-10-27 18:42:08 +00002361 if (CheckSpecializationInstantiationRedecl(PointOfInstantiation, TSK,
2362 Record,
2363 MSInfo->getTemplateSpecializationKind(),
Nico Weberc956b6e2010-09-27 21:02:09 +00002364 MSInfo->getPointOfInstantiation(),
Douglas Gregor0d035142009-10-27 18:42:08 +00002365 SuppressNew) ||
2366 SuppressNew)
Douglas Gregorf6b11852009-10-08 15:14:33 +00002367 continue;
2368
Douglas Gregor0d035142009-10-27 18:42:08 +00002369 CXXRecordDecl *Pattern = Record->getInstantiatedFromMemberClass();
2370 assert(Pattern && "Missing instantiated-from-template information");
2371
Douglas Gregor952b0172010-02-11 01:04:33 +00002372 if (!Record->getDefinition()) {
2373 if (!Pattern->getDefinition()) {
Douglas Gregor0d035142009-10-27 18:42:08 +00002374 // C++0x [temp.explicit]p8:
2375 // An explicit instantiation definition that names a class template
2376 // specialization explicitly instantiates the class template
2377 // specialization and is only an explicit instantiation definition
2378 // of members whose definition is visible at the point of
2379 // instantiation.
2380 if (TSK == TSK_ExplicitInstantiationDeclaration) {
2381 MSInfo->setTemplateSpecializationKind(TSK);
2382 MSInfo->setPointOfInstantiation(PointOfInstantiation);
2383 }
2384
2385 continue;
2386 }
2387
2388 InstantiateClass(PointOfInstantiation, Record, Pattern,
Douglas Gregord0e3daf2009-09-04 22:48:11 +00002389 TemplateArgs,
2390 TSK);
Nico Weberc956b6e2010-09-27 21:02:09 +00002391 } else {
2392 if (TSK == TSK_ExplicitInstantiationDefinition &&
2393 Record->getTemplateSpecializationKind() ==
2394 TSK_ExplicitInstantiationDeclaration) {
2395 Record->setTemplateSpecializationKind(TSK);
2396 MarkVTableUsed(PointOfInstantiation, Record, true);
2397 }
Douglas Gregor0d035142009-10-27 18:42:08 +00002398 }
Douglas Gregore9374d52009-10-08 01:19:17 +00002399
Douglas Gregor952b0172010-02-11 01:04:33 +00002400 Pattern = cast_or_null<CXXRecordDecl>(Record->getDefinition());
Douglas Gregor0d035142009-10-27 18:42:08 +00002401 if (Pattern)
2402 InstantiateClassMembers(PointOfInstantiation, Pattern, TemplateArgs,
2403 TSK);
Richard Smithf1c66b42012-03-14 23:13:10 +00002404 } else if (EnumDecl *Enum = dyn_cast<EnumDecl>(*D)) {
2405 MemberSpecializationInfo *MSInfo = Enum->getMemberSpecializationInfo();
2406 assert(MSInfo && "No member specialization information?");
2407
2408 if (MSInfo->getTemplateSpecializationKind()
2409 == TSK_ExplicitSpecialization)
2410 continue;
2411
2412 if (CheckSpecializationInstantiationRedecl(
2413 PointOfInstantiation, TSK, Enum,
2414 MSInfo->getTemplateSpecializationKind(),
2415 MSInfo->getPointOfInstantiation(), SuppressNew) ||
2416 SuppressNew)
2417 continue;
2418
2419 if (Enum->getDefinition())
2420 continue;
2421
2422 EnumDecl *Pattern = Enum->getInstantiatedFromMemberEnum();
2423 assert(Pattern && "Missing instantiated-from-template information");
2424
2425 if (TSK == TSK_ExplicitInstantiationDefinition) {
2426 if (!Pattern->getDefinition())
2427 continue;
2428
2429 InstantiateEnum(PointOfInstantiation, Enum, Pattern, TemplateArgs, TSK);
2430 } else {
2431 MSInfo->setTemplateSpecializationKind(TSK);
2432 MSInfo->setPointOfInstantiation(PointOfInstantiation);
2433 }
Douglas Gregora58861f2009-05-13 20:28:22 +00002434 }
2435 }
2436}
2437
2438/// \brief Instantiate the definitions of all of the members of the
2439/// given class template specialization, which was named as part of an
2440/// explicit instantiation.
Mike Stump1eb44332009-09-09 15:08:12 +00002441void
Douglas Gregord0e3daf2009-09-04 22:48:11 +00002442Sema::InstantiateClassTemplateSpecializationMembers(
Douglas Gregora58861f2009-05-13 20:28:22 +00002443 SourceLocation PointOfInstantiation,
Douglas Gregord0e3daf2009-09-04 22:48:11 +00002444 ClassTemplateSpecializationDecl *ClassTemplateSpec,
2445 TemplateSpecializationKind TSK) {
Douglas Gregora58861f2009-05-13 20:28:22 +00002446 // C++0x [temp.explicit]p7:
2447 // An explicit instantiation that names a class template
2448 // specialization is an explicit instantion of the same kind
2449 // (declaration or definition) of each of its members (not
2450 // including members inherited from base classes) that has not
2451 // been previously explicitly specialized in the translation unit
2452 // containing the explicit instantiation, except as described
2453 // below.
2454 InstantiateClassMembers(PointOfInstantiation, ClassTemplateSpec,
Douglas Gregord0e3daf2009-09-04 22:48:11 +00002455 getTemplateInstantiationArgs(ClassTemplateSpec),
2456 TSK);
Douglas Gregora58861f2009-05-13 20:28:22 +00002457}
2458
John McCall60d7b3a2010-08-24 06:29:42 +00002459StmtResult
Douglas Gregord6350ae2009-08-28 20:31:08 +00002460Sema::SubstStmt(Stmt *S, const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor43959a92009-08-20 07:17:43 +00002461 if (!S)
2462 return Owned(S);
2463
2464 TemplateInstantiator Instantiator(*this, TemplateArgs,
2465 SourceLocation(),
2466 DeclarationName());
2467 return Instantiator.TransformStmt(S);
2468}
2469
John McCall60d7b3a2010-08-24 06:29:42 +00002470ExprResult
Douglas Gregord6350ae2009-08-28 20:31:08 +00002471Sema::SubstExpr(Expr *E, const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregorb98b1992009-08-11 05:31:07 +00002472 if (!E)
2473 return Owned(E);
Mike Stump1eb44332009-09-09 15:08:12 +00002474
Douglas Gregorb98b1992009-08-11 05:31:07 +00002475 TemplateInstantiator Instantiator(*this, TemplateArgs,
2476 SourceLocation(),
2477 DeclarationName());
2478 return Instantiator.TransformExpr(E);
2479}
2480
Douglas Gregor91fc73e2011-01-07 19:35:17 +00002481bool Sema::SubstExprs(Expr **Exprs, unsigned NumExprs, bool IsCall,
2482 const MultiLevelTemplateArgumentList &TemplateArgs,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002483 SmallVectorImpl<Expr *> &Outputs) {
Douglas Gregor91fc73e2011-01-07 19:35:17 +00002484 if (NumExprs == 0)
2485 return false;
2486
2487 TemplateInstantiator Instantiator(*this, TemplateArgs,
2488 SourceLocation(),
2489 DeclarationName());
2490 return Instantiator.TransformExprs(Exprs, NumExprs, IsCall, Outputs);
2491}
2492
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00002493NestedNameSpecifierLoc
2494Sema::SubstNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS,
2495 const MultiLevelTemplateArgumentList &TemplateArgs) {
2496 if (!NNS)
2497 return NestedNameSpecifierLoc();
2498
2499 TemplateInstantiator Instantiator(*this, TemplateArgs, NNS.getBeginLoc(),
2500 DeclarationName());
2501 return Instantiator.TransformNestedNameSpecifierLoc(NNS);
2502}
2503
Abramo Bagnara25777432010-08-11 22:01:17 +00002504/// \brief Do template substitution on declaration name info.
2505DeclarationNameInfo
2506Sema::SubstDeclarationNameInfo(const DeclarationNameInfo &NameInfo,
2507 const MultiLevelTemplateArgumentList &TemplateArgs) {
2508 TemplateInstantiator Instantiator(*this, TemplateArgs, NameInfo.getLoc(),
2509 NameInfo.getName());
2510 return Instantiator.TransformDeclarationNameInfo(NameInfo);
2511}
2512
Douglas Gregorde650ae2009-03-31 18:38:02 +00002513TemplateName
Douglas Gregor1d752d72011-03-02 18:46:51 +00002514Sema::SubstTemplateName(NestedNameSpecifierLoc QualifierLoc,
2515 TemplateName Name, SourceLocation Loc,
Douglas Gregord6350ae2009-08-28 20:31:08 +00002516 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregord1067e52009-08-06 06:41:21 +00002517 TemplateInstantiator Instantiator(*this, TemplateArgs, Loc,
2518 DeclarationName());
Douglas Gregor1d752d72011-03-02 18:46:51 +00002519 CXXScopeSpec SS;
2520 SS.Adopt(QualifierLoc);
2521 return Instantiator.TransformTemplateName(SS, Name, Loc);
Douglas Gregorde650ae2009-03-31 18:38:02 +00002522}
Douglas Gregor91333002009-06-11 00:06:24 +00002523
Douglas Gregore02e2622010-12-22 21:19:48 +00002524bool Sema::Subst(const TemplateArgumentLoc *Args, unsigned NumArgs,
2525 TemplateArgumentListInfo &Result,
John McCall833ca992009-10-29 08:12:44 +00002526 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor670444e2009-08-04 22:27:00 +00002527 TemplateInstantiator Instantiator(*this, TemplateArgs, SourceLocation(),
2528 DeclarationName());
Douglas Gregore02e2622010-12-22 21:19:48 +00002529
2530 return Instantiator.TransformTemplateArguments(Args, NumArgs, Result);
Douglas Gregor91333002009-06-11 00:06:24 +00002531}
Douglas Gregor895162d2010-04-30 18:55:50 +00002532
Douglas Gregor12c9c002011-01-07 16:43:16 +00002533llvm::PointerUnion<Decl *, LocalInstantiationScope::DeclArgumentPack *> *
2534LocalInstantiationScope::findInstantiationOf(const Decl *D) {
Chris Lattner57ad3782011-02-17 20:34:02 +00002535 for (LocalInstantiationScope *Current = this; Current;
Douglas Gregor895162d2010-04-30 18:55:50 +00002536 Current = Current->Outer) {
Chris Lattner57ad3782011-02-17 20:34:02 +00002537
Douglas Gregor895162d2010-04-30 18:55:50 +00002538 // Check if we found something within this scope.
Douglas Gregorebb1c562010-12-21 21:22:51 +00002539 const Decl *CheckD = D;
2540 do {
Douglas Gregor12c9c002011-01-07 16:43:16 +00002541 LocalDeclsMap::iterator Found = Current->LocalDecls.find(CheckD);
Douglas Gregorebb1c562010-12-21 21:22:51 +00002542 if (Found != Current->LocalDecls.end())
Douglas Gregor12c9c002011-01-07 16:43:16 +00002543 return &Found->second;
Douglas Gregorebb1c562010-12-21 21:22:51 +00002544
2545 // If this is a tag declaration, it's possible that we need to look for
2546 // a previous declaration.
2547 if (const TagDecl *Tag = dyn_cast<TagDecl>(CheckD))
Douglas Gregoref96ee02012-01-14 16:38:05 +00002548 CheckD = Tag->getPreviousDecl();
Douglas Gregorebb1c562010-12-21 21:22:51 +00002549 else
2550 CheckD = 0;
2551 } while (CheckD);
2552
Douglas Gregor895162d2010-04-30 18:55:50 +00002553 // If we aren't combined with our outer scope, we're done.
2554 if (!Current->CombineWithOuterScope)
2555 break;
2556 }
Chris Lattner57ad3782011-02-17 20:34:02 +00002557
2558 // If we didn't find the decl, then we either have a sema bug, or we have a
2559 // forward reference to a label declaration. Return null to indicate that
2560 // we have an uninstantiated label.
2561 assert(isa<LabelDecl>(D) && "declaration not instantiated in this scope");
Douglas Gregor895162d2010-04-30 18:55:50 +00002562 return 0;
2563}
2564
John McCall2a7fb272010-08-25 05:32:35 +00002565void LocalInstantiationScope::InstantiatedLocal(const Decl *D, Decl *Inst) {
Douglas Gregor12c9c002011-01-07 16:43:16 +00002566 llvm::PointerUnion<Decl *, DeclArgumentPack *> &Stored = LocalDecls[D];
Douglas Gregord3731192011-01-10 07:32:04 +00002567 if (Stored.isNull())
2568 Stored = Inst;
2569 else if (Stored.is<Decl *>()) {
2570 assert(Stored.get<Decl *>() == Inst && "Already instantiated this local");
2571 Stored = Inst;
2572 } else
2573 LocalDecls[D].get<DeclArgumentPack *>()->push_back(Inst);
Douglas Gregor895162d2010-04-30 18:55:50 +00002574}
Douglas Gregor12c9c002011-01-07 16:43:16 +00002575
2576void LocalInstantiationScope::InstantiatedLocalPackArg(const Decl *D,
2577 Decl *Inst) {
2578 DeclArgumentPack *Pack = LocalDecls[D].get<DeclArgumentPack *>();
2579 Pack->push_back(Inst);
2580}
2581
2582void LocalInstantiationScope::MakeInstantiatedLocalArgPack(const Decl *D) {
2583 llvm::PointerUnion<Decl *, DeclArgumentPack *> &Stored = LocalDecls[D];
2584 assert(Stored.isNull() && "Already instantiated this local");
2585 DeclArgumentPack *Pack = new DeclArgumentPack;
2586 Stored = Pack;
2587 ArgumentPacks.push_back(Pack);
2588}
2589
Douglas Gregord3731192011-01-10 07:32:04 +00002590void LocalInstantiationScope::SetPartiallySubstitutedPack(NamedDecl *Pack,
2591 const TemplateArgument *ExplicitArgs,
2592 unsigned NumExplicitArgs) {
2593 assert((!PartiallySubstitutedPack || PartiallySubstitutedPack == Pack) &&
2594 "Already have a partially-substituted pack");
2595 assert((!PartiallySubstitutedPack
2596 || NumArgsInPartiallySubstitutedPack == NumExplicitArgs) &&
2597 "Wrong number of arguments in partially-substituted pack");
2598 PartiallySubstitutedPack = Pack;
2599 ArgsInPartiallySubstitutedPack = ExplicitArgs;
2600 NumArgsInPartiallySubstitutedPack = NumExplicitArgs;
2601}
2602
2603NamedDecl *LocalInstantiationScope::getPartiallySubstitutedPack(
2604 const TemplateArgument **ExplicitArgs,
2605 unsigned *NumExplicitArgs) const {
2606 if (ExplicitArgs)
2607 *ExplicitArgs = 0;
2608 if (NumExplicitArgs)
2609 *NumExplicitArgs = 0;
2610
2611 for (const LocalInstantiationScope *Current = this; Current;
2612 Current = Current->Outer) {
2613 if (Current->PartiallySubstitutedPack) {
2614 if (ExplicitArgs)
2615 *ExplicitArgs = Current->ArgsInPartiallySubstitutedPack;
2616 if (NumExplicitArgs)
2617 *NumExplicitArgs = Current->NumArgsInPartiallySubstitutedPack;
2618
2619 return Current->PartiallySubstitutedPack;
2620 }
2621
2622 if (!Current->CombineWithOuterScope)
2623 break;
2624 }
2625
2626 return 0;
2627}