blob: 7416d1230178e0387e01bd857193a43aa00b1305 [file] [log] [blame]
Douglas Gregorfe1e1102009-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 McCall83024632010-08-25 22:03:47 +000013#include "clang/Sema/SemaInternal.h"
Douglas Gregord6ff3322009-08-04 16:50:30 +000014#include "TreeTransform.h"
John McCall8b0666c2010-08-20 18:27:03 +000015#include "clang/Sema/DeclSpec.h"
Richard Smith938f40b2011-06-11 17:19:42 +000016#include "clang/Sema/Initialization.h"
Douglas Gregorc3a6ade2010-08-12 20:07:10 +000017#include "clang/Sema/Lookup.h"
John McCallde6836a2010-08-24 07:21:54 +000018#include "clang/Sema/Template.h"
John McCall19c1bfd2010-08-25 05:32:35 +000019#include "clang/Sema/TemplateDeduction.h"
Douglas Gregor28ad4b52009-05-26 20:50:29 +000020#include "clang/AST/ASTConsumer.h"
Douglas Gregorfe1e1102009-02-27 19:31:52 +000021#include "clang/AST/ASTContext.h"
22#include "clang/AST/Expr.h"
Douglas Gregorfe1e1102009-02-27 19:31:52 +000023#include "clang/AST/DeclTemplate.h"
Douglas Gregorfe1e1102009-02-27 19:31:52 +000024#include "clang/Basic/LangOptions.h"
25
26using namespace clang;
John McCall19c1bfd2010-08-25 05:32:35 +000027using namespace sema;
Douglas Gregorfe1e1102009-02-27 19:31:52 +000028
Douglas Gregor4ea568f2009-03-10 18:03:33 +000029//===----------------------------------------------------------------------===/
30// Template Instantiation Support
31//===----------------------------------------------------------------------===/
32
Douglas Gregor01afeef2009-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 Gregor36d7c5f2009-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 Gregor8c702532010-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 Gregor1bd7a942010-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 Gregora654dd82009-08-28 17:37:35 +000050MultiLevelTemplateArgumentList
Douglas Gregor36d7c5f2009-11-09 19:17:50 +000051Sema::getTemplateInstantiationArgs(NamedDecl *D,
Douglas Gregor8c702532010-02-05 07:33:43 +000052 const TemplateArgumentList *Innermost,
Douglas Gregor1bd7a942010-05-03 23:29:10 +000053 bool RelativeToPrimary,
54 const FunctionDecl *Pattern) {
Douglas Gregora654dd82009-08-28 17:37:35 +000055 // Accumulate the set of template argument lists in this structure.
56 MultiLevelTemplateArgumentList Result;
Mike Stump11289f42009-09-09 15:08:12 +000057
Douglas Gregor36d7c5f2009-11-09 19:17:50 +000058 if (Innermost)
59 Result.addOuterTemplateArguments(Innermost);
60
Douglas Gregora654dd82009-08-28 17:37:35 +000061 DeclContext *Ctx = dyn_cast<DeclContext>(D);
Douglas Gregora51c9cc2011-05-22 00:21:10 +000062 if (!Ctx) {
Douglas Gregora654dd82009-08-28 17:37:35 +000063 Ctx = D->getDeclContext();
Douglas Gregora51c9cc2011-05-22 00:21:10 +000064
Douglas Gregor55462622011-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 Gregora51c9cc2011-05-22 00:21:10 +000079 }
80
John McCall970d5302009-08-29 03:16:09 +000081 while (!Ctx->isFileContext()) {
Douglas Gregora654dd82009-08-28 17:37:35 +000082 // Add template arguments from a class template instantiation.
Mike Stump11289f42009-09-09 15:08:12 +000083 if (ClassTemplateSpecializationDecl *Spec
Douglas Gregora654dd82009-08-28 17:37:35 +000084 = dyn_cast<ClassTemplateSpecializationDecl>(Ctx)) {
85 // We're done when we hit an explicit specialization.
Douglas Gregor9961ce92010-07-08 18:37:38 +000086 if (Spec->getSpecializationKind() == TSK_ExplicitSpecialization &&
87 !isa<ClassTemplatePartialSpecializationDecl>(Spec))
Douglas Gregora654dd82009-08-28 17:37:35 +000088 break;
Mike Stump11289f42009-09-09 15:08:12 +000089
Douglas Gregora654dd82009-08-28 17:37:35 +000090 Result.addOuterTemplateArguments(&Spec->getTemplateInstantiationArgs());
Douglas Gregorcf915552009-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 Stump11289f42009-09-09 15:08:12 +000097 }
Douglas Gregora654dd82009-08-28 17:37:35 +000098 // Add template arguments from a function template specialization.
John McCall970d5302009-08-29 03:16:09 +000099 else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Ctx)) {
Douglas Gregor8c702532010-02-05 07:33:43 +0000100 if (!RelativeToPrimary &&
Francois Pichet00c7e6c2011-08-14 03:52:19 +0000101 (Function->getTemplateSpecializationKind() ==
102 TSK_ExplicitSpecialization &&
103 !Function->getClassScopeSpecializationPattern()))
Douglas Gregorcf915552009-10-13 16:30:37 +0000104 break;
105
Douglas Gregora654dd82009-08-28 17:37:35 +0000106 if (const TemplateArgumentList *TemplateArgs
Douglas Gregorcf915552009-10-13 16:30:37 +0000107 = Function->getTemplateSpecializationArgs()) {
108 // Add the template arguments for this specialization.
Douglas Gregora654dd82009-08-28 17:37:35 +0000109 Result.addOuterTemplateArguments(TemplateArgs);
John McCall970d5302009-08-29 03:16:09 +0000110
Douglas Gregorcf915552009-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 Gregor43669f82011-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 Gregorcf915552009-10-13 16:30:37 +0000122 }
123
John McCall970d5302009-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 Gregor1bd7a942010-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 McCall970d5302009-08-29 03:16:09 +0000128 if (Function->getFriendObjectKind() &&
Douglas Gregor1bd7a942010-05-03 23:29:10 +0000129 Function->getDeclContext()->isFileContext() &&
130 (!Pattern || !Pattern->getLexicalDeclContext()->isFileContext())) {
John McCall970d5302009-08-29 03:16:09 +0000131 Ctx = Function->getLexicalDeclContext();
Douglas Gregor8c702532010-02-05 07:33:43 +0000132 RelativeToPrimary = false;
John McCall970d5302009-08-29 03:16:09 +0000133 continue;
134 }
Douglas Gregor9961ce92010-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 Gregora654dd82009-08-28 17:37:35 +0000144 }
John McCall970d5302009-08-29 03:16:09 +0000145
146 Ctx = Ctx->getParent();
Douglas Gregor8c702532010-02-05 07:33:43 +0000147 RelativeToPrimary = false;
Douglas Gregorb4850462009-05-14 23:26:13 +0000148 }
Mike Stump11289f42009-09-09 15:08:12 +0000149
Douglas Gregora654dd82009-08-28 17:37:35 +0000150 return Result;
Douglas Gregorb4850462009-05-14 23:26:13 +0000151}
152
Douglas Gregor84d49a22009-11-11 21:54:23 +0000153bool Sema::ActiveTemplateInstantiation::isInstantiationRecord() const {
154 switch (Kind) {
155 case TemplateInstantiation:
Richard Smithf623c962012-04-17 00:58:00 +0000156 case ExceptionSpecInstantiation:
Douglas Gregor84d49a22009-11-11 21:54:23 +0000157 case DefaultTemplateArgumentInstantiation:
158 case DefaultFunctionArgumentInstantiation:
Douglas Gregor84d49a22009-11-11 21:54:23 +0000159 case ExplicitTemplateArgumentSubstitution:
160 case DeducedTemplateArgumentSubstitution:
161 case PriorTemplateArgumentSubstitution:
Richard Smith8a874c92012-07-08 02:38:24 +0000162 return true;
163
Douglas Gregor84d49a22009-11-11 21:54:23 +0000164 case DefaultTemplateArgumentChecking:
165 return false;
166 }
David Blaikie8a40f702012-01-17 06:56:22 +0000167
168 llvm_unreachable("Invalid InstantiationKind!");
Douglas Gregor84d49a22009-11-11 21:54:23 +0000169}
170
Douglas Gregorfcd5db32009-03-10 00:06:19 +0000171Sema::InstantiatingTemplate::
172InstantiatingTemplate(Sema &SemaRef, SourceLocation PointOfInstantiation,
Douglas Gregor85673582009-05-18 17:01:57 +0000173 Decl *Entity,
Douglas Gregorfcd5db32009-03-10 00:06:19 +0000174 SourceRange InstantiationRange)
Douglas Gregoredb76852011-01-27 22:31:44 +0000175 : SemaRef(SemaRef),
176 SavedInNonInstantiationSFINAEContext(
177 SemaRef.InNonInstantiationSFINAEContext)
178{
Douglas Gregor79cf6032009-03-10 20:44:00 +0000179 Invalid = CheckInstantiationDepth(PointOfInstantiation,
180 InstantiationRange);
181 if (!Invalid) {
Douglas Gregorfcd5db32009-03-10 00:06:19 +0000182 ActiveTemplateInstantiation Inst;
Douglas Gregor79cf6032009-03-10 20:44:00 +0000183 Inst.Kind = ActiveTemplateInstantiation::TemplateInstantiation;
Douglas Gregorfcd5db32009-03-10 00:06:19 +0000184 Inst.PointOfInstantiation = PointOfInstantiation;
Douglas Gregor79cf6032009-03-10 20:44:00 +0000185 Inst.Entity = reinterpret_cast<uintptr_t>(Entity);
Douglas Gregorc9220832009-03-12 18:36:18 +0000186 Inst.TemplateArgs = 0;
187 Inst.NumTemplateArgs = 0;
Douglas Gregor79cf6032009-03-10 20:44:00 +0000188 Inst.InstantiationRange = InstantiationRange;
Douglas Gregoredb76852011-01-27 22:31:44 +0000189 SemaRef.InNonInstantiationSFINAEContext = false;
Douglas Gregor79cf6032009-03-10 20:44:00 +0000190 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
Douglas Gregor79cf6032009-03-10 20:44:00 +0000191 }
192}
193
Richard Smithf623c962012-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 Stump11289f42009-09-09 15:08:12 +0000217Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef,
Douglas Gregor79cf6032009-03-10 20:44:00 +0000218 SourceLocation PointOfInstantiation,
219 TemplateDecl *Template,
220 const TemplateArgument *TemplateArgs,
221 unsigned NumTemplateArgs,
222 SourceRange InstantiationRange)
Douglas Gregoredb76852011-01-27 22:31:44 +0000223 : SemaRef(SemaRef),
224 SavedInNonInstantiationSFINAEContext(
225 SemaRef.InNonInstantiationSFINAEContext)
226{
Douglas Gregor79cf6032009-03-10 20:44:00 +0000227 Invalid = CheckInstantiationDepth(PointOfInstantiation,
228 InstantiationRange);
229 if (!Invalid) {
230 ActiveTemplateInstantiation Inst;
Mike Stump11289f42009-09-09 15:08:12 +0000231 Inst.Kind
Douglas Gregor79cf6032009-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 Gregorfcd5db32009-03-10 00:06:19 +0000237 Inst.InstantiationRange = InstantiationRange;
Douglas Gregoredb76852011-01-27 22:31:44 +0000238 SemaRef.InNonInstantiationSFINAEContext = false;
Douglas Gregorfcd5db32009-03-10 00:06:19 +0000239 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
Douglas Gregorfcd5db32009-03-10 00:06:19 +0000240 }
241}
242
Mike Stump11289f42009-09-09 15:08:12 +0000243Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef,
Douglas Gregor637d9982009-06-10 23:47:09 +0000244 SourceLocation PointOfInstantiation,
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000245 FunctionTemplateDecl *FunctionTemplate,
246 const TemplateArgument *TemplateArgs,
247 unsigned NumTemplateArgs,
248 ActiveTemplateInstantiation::InstantiationKind Kind,
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +0000249 sema::TemplateDeductionInfo &DeductionInfo,
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000250 SourceRange InstantiationRange)
Douglas Gregoredb76852011-01-27 22:31:44 +0000251 : SemaRef(SemaRef),
252 SavedInNonInstantiationSFINAEContext(
253 SemaRef.InNonInstantiationSFINAEContext)
254{
Richard Smith8a874c92012-07-08 02:38:24 +0000255 Invalid = CheckInstantiationDepth(PointOfInstantiation, InstantiationRange);
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000256 if (!Invalid) {
257 ActiveTemplateInstantiation Inst;
258 Inst.Kind = Kind;
259 Inst.PointOfInstantiation = PointOfInstantiation;
260 Inst.Entity = reinterpret_cast<uintptr_t>(FunctionTemplate);
261 Inst.TemplateArgs = TemplateArgs;
262 Inst.NumTemplateArgs = NumTemplateArgs;
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +0000263 Inst.DeductionInfo = &DeductionInfo;
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000264 Inst.InstantiationRange = InstantiationRange;
Douglas Gregoredb76852011-01-27 22:31:44 +0000265 SemaRef.InNonInstantiationSFINAEContext = false;
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000266 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
Douglas Gregor84d49a22009-11-11 21:54:23 +0000267
268 if (!Inst.isInstantiationRecord())
269 ++SemaRef.NonInstantiationEntries;
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000270 }
271}
272
Mike Stump11289f42009-09-09 15:08:12 +0000273Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef,
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000274 SourceLocation PointOfInstantiation,
Douglas Gregor637d9982009-06-10 23:47:09 +0000275 ClassTemplatePartialSpecializationDecl *PartialSpec,
276 const TemplateArgument *TemplateArgs,
277 unsigned NumTemplateArgs,
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +0000278 sema::TemplateDeductionInfo &DeductionInfo,
Douglas Gregor637d9982009-06-10 23:47:09 +0000279 SourceRange InstantiationRange)
Douglas Gregoredb76852011-01-27 22:31:44 +0000280 : SemaRef(SemaRef),
281 SavedInNonInstantiationSFINAEContext(
282 SemaRef.InNonInstantiationSFINAEContext)
283{
Richard Smith8a874c92012-07-08 02:38:24 +0000284 Invalid = CheckInstantiationDepth(PointOfInstantiation, InstantiationRange);
285 if (!Invalid) {
286 ActiveTemplateInstantiation Inst;
287 Inst.Kind = ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution;
288 Inst.PointOfInstantiation = PointOfInstantiation;
289 Inst.Entity = reinterpret_cast<uintptr_t>(PartialSpec);
290 Inst.TemplateArgs = TemplateArgs;
291 Inst.NumTemplateArgs = NumTemplateArgs;
292 Inst.DeductionInfo = &DeductionInfo;
293 Inst.InstantiationRange = InstantiationRange;
294 SemaRef.InNonInstantiationSFINAEContext = false;
295 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
296 }
Douglas Gregor637d9982009-06-10 23:47:09 +0000297}
298
Mike Stump11289f42009-09-09 15:08:12 +0000299Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef,
Douglas Gregore62e6a02009-11-11 19:13:48 +0000300 SourceLocation PointOfInstantiation,
Anders Carlsson657bad42009-09-05 05:14:19 +0000301 ParmVarDecl *Param,
302 const TemplateArgument *TemplateArgs,
303 unsigned NumTemplateArgs,
304 SourceRange InstantiationRange)
Douglas Gregoredb76852011-01-27 22:31:44 +0000305 : SemaRef(SemaRef),
306 SavedInNonInstantiationSFINAEContext(
307 SemaRef.InNonInstantiationSFINAEContext)
308{
Douglas Gregore62e6a02009-11-11 19:13:48 +0000309 Invalid = CheckInstantiationDepth(PointOfInstantiation, InstantiationRange);
Anders Carlsson657bad42009-09-05 05:14:19 +0000310 if (!Invalid) {
311 ActiveTemplateInstantiation Inst;
312 Inst.Kind
313 = ActiveTemplateInstantiation::DefaultFunctionArgumentInstantiation;
Douglas Gregore62e6a02009-11-11 19:13:48 +0000314 Inst.PointOfInstantiation = PointOfInstantiation;
Anders Carlsson657bad42009-09-05 05:14:19 +0000315 Inst.Entity = reinterpret_cast<uintptr_t>(Param);
316 Inst.TemplateArgs = TemplateArgs;
317 Inst.NumTemplateArgs = NumTemplateArgs;
318 Inst.InstantiationRange = InstantiationRange;
Douglas Gregoredb76852011-01-27 22:31:44 +0000319 SemaRef.InNonInstantiationSFINAEContext = false;
Anders Carlsson657bad42009-09-05 05:14:19 +0000320 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
Douglas Gregore62e6a02009-11-11 19:13:48 +0000321 }
322}
323
324Sema::InstantiatingTemplate::
325InstantiatingTemplate(Sema &SemaRef, SourceLocation PointOfInstantiation,
Douglas Gregorca4686d2011-01-04 23:35:54 +0000326 NamedDecl *Template,
Douglas Gregore62e6a02009-11-11 19:13:48 +0000327 NonTypeTemplateParmDecl *Param,
328 const TemplateArgument *TemplateArgs,
329 unsigned NumTemplateArgs,
Douglas Gregoredb76852011-01-27 22:31:44 +0000330 SourceRange InstantiationRange)
331 : SemaRef(SemaRef),
332 SavedInNonInstantiationSFINAEContext(
333 SemaRef.InNonInstantiationSFINAEContext)
334{
Richard Smith8a874c92012-07-08 02:38:24 +0000335 Invalid = CheckInstantiationDepth(PointOfInstantiation, InstantiationRange);
336 if (!Invalid) {
337 ActiveTemplateInstantiation Inst;
338 Inst.Kind = ActiveTemplateInstantiation::PriorTemplateArgumentSubstitution;
339 Inst.PointOfInstantiation = PointOfInstantiation;
340 Inst.Template = Template;
341 Inst.Entity = reinterpret_cast<uintptr_t>(Param);
342 Inst.TemplateArgs = TemplateArgs;
343 Inst.NumTemplateArgs = NumTemplateArgs;
344 Inst.InstantiationRange = InstantiationRange;
345 SemaRef.InNonInstantiationSFINAEContext = false;
346 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
347 }
Douglas Gregore62e6a02009-11-11 19:13:48 +0000348}
349
350Sema::InstantiatingTemplate::
351InstantiatingTemplate(Sema &SemaRef, SourceLocation PointOfInstantiation,
Douglas Gregorca4686d2011-01-04 23:35:54 +0000352 NamedDecl *Template,
Douglas Gregore62e6a02009-11-11 19:13:48 +0000353 TemplateTemplateParmDecl *Param,
354 const TemplateArgument *TemplateArgs,
355 unsigned NumTemplateArgs,
Douglas Gregoredb76852011-01-27 22:31:44 +0000356 SourceRange InstantiationRange)
357 : SemaRef(SemaRef),
358 SavedInNonInstantiationSFINAEContext(
359 SemaRef.InNonInstantiationSFINAEContext)
360{
Richard Smith8a874c92012-07-08 02:38:24 +0000361 Invalid = CheckInstantiationDepth(PointOfInstantiation, InstantiationRange);
362 if (!Invalid) {
363 ActiveTemplateInstantiation Inst;
364 Inst.Kind = ActiveTemplateInstantiation::PriorTemplateArgumentSubstitution;
365 Inst.PointOfInstantiation = PointOfInstantiation;
366 Inst.Template = Template;
367 Inst.Entity = reinterpret_cast<uintptr_t>(Param);
368 Inst.TemplateArgs = TemplateArgs;
369 Inst.NumTemplateArgs = NumTemplateArgs;
370 Inst.InstantiationRange = InstantiationRange;
371 SemaRef.InNonInstantiationSFINAEContext = false;
372 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
373 }
Douglas Gregor84d49a22009-11-11 21:54:23 +0000374}
375
376Sema::InstantiatingTemplate::
377InstantiatingTemplate(Sema &SemaRef, SourceLocation PointOfInstantiation,
378 TemplateDecl *Template,
379 NamedDecl *Param,
380 const TemplateArgument *TemplateArgs,
381 unsigned NumTemplateArgs,
Douglas Gregoredb76852011-01-27 22:31:44 +0000382 SourceRange InstantiationRange)
383 : SemaRef(SemaRef),
384 SavedInNonInstantiationSFINAEContext(
385 SemaRef.InNonInstantiationSFINAEContext)
386{
Douglas Gregor84d49a22009-11-11 21:54:23 +0000387 Invalid = false;
388
389 ActiveTemplateInstantiation Inst;
390 Inst.Kind = ActiveTemplateInstantiation::DefaultTemplateArgumentChecking;
391 Inst.PointOfInstantiation = PointOfInstantiation;
392 Inst.Template = Template;
393 Inst.Entity = reinterpret_cast<uintptr_t>(Param);
394 Inst.TemplateArgs = TemplateArgs;
395 Inst.NumTemplateArgs = NumTemplateArgs;
396 Inst.InstantiationRange = InstantiationRange;
Douglas Gregoredb76852011-01-27 22:31:44 +0000397 SemaRef.InNonInstantiationSFINAEContext = false;
Douglas Gregor84d49a22009-11-11 21:54:23 +0000398 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
399
400 assert(!Inst.isInstantiationRecord());
401 ++SemaRef.NonInstantiationEntries;
Anders Carlsson657bad42009-09-05 05:14:19 +0000402}
403
Douglas Gregor85673582009-05-18 17:01:57 +0000404void Sema::InstantiatingTemplate::Clear() {
405 if (!Invalid) {
Douglas Gregor84d49a22009-11-11 21:54:23 +0000406 if (!SemaRef.ActiveTemplateInstantiations.back().isInstantiationRecord()) {
407 assert(SemaRef.NonInstantiationEntries > 0);
408 --SemaRef.NonInstantiationEntries;
409 }
Douglas Gregoredb76852011-01-27 22:31:44 +0000410 SemaRef.InNonInstantiationSFINAEContext
411 = SavedInNonInstantiationSFINAEContext;
Douglas Gregorfcd5db32009-03-10 00:06:19 +0000412 SemaRef.ActiveTemplateInstantiations.pop_back();
Douglas Gregor85673582009-05-18 17:01:57 +0000413 Invalid = true;
414 }
Douglas Gregorfcd5db32009-03-10 00:06:19 +0000415}
416
Douglas Gregor79cf6032009-03-10 20:44:00 +0000417bool Sema::InstantiatingTemplate::CheckInstantiationDepth(
418 SourceLocation PointOfInstantiation,
419 SourceRange InstantiationRange) {
Douglas Gregor84d49a22009-11-11 21:54:23 +0000420 assert(SemaRef.NonInstantiationEntries <=
421 SemaRef.ActiveTemplateInstantiations.size());
422 if ((SemaRef.ActiveTemplateInstantiations.size() -
423 SemaRef.NonInstantiationEntries)
David Blaikiebbafb8a2012-03-11 07:00:24 +0000424 <= SemaRef.getLangOpts().InstantiationDepth)
Douglas Gregor79cf6032009-03-10 20:44:00 +0000425 return false;
426
Mike Stump11289f42009-09-09 15:08:12 +0000427 SemaRef.Diag(PointOfInstantiation,
Douglas Gregor79cf6032009-03-10 20:44:00 +0000428 diag::err_template_recursion_depth_exceeded)
David Blaikiebbafb8a2012-03-11 07:00:24 +0000429 << SemaRef.getLangOpts().InstantiationDepth
Douglas Gregor79cf6032009-03-10 20:44:00 +0000430 << InstantiationRange;
431 SemaRef.Diag(PointOfInstantiation, diag::note_template_recursion_depth)
David Blaikiebbafb8a2012-03-11 07:00:24 +0000432 << SemaRef.getLangOpts().InstantiationDepth;
Douglas Gregor79cf6032009-03-10 20:44:00 +0000433 return true;
434}
435
Douglas Gregor4ea568f2009-03-10 18:03:33 +0000436/// \brief Prints the current instantiation stack through a series of
437/// notes.
438void Sema::PrintInstantiationStack() {
Douglas Gregorffed1cb2010-04-20 07:18:24 +0000439 // Determine which template instantiations to skip, if any.
440 unsigned SkipStart = ActiveTemplateInstantiations.size(), SkipEnd = SkipStart;
441 unsigned Limit = Diags.getTemplateBacktraceLimit();
442 if (Limit && Limit < ActiveTemplateInstantiations.size()) {
443 SkipStart = Limit / 2 + Limit % 2;
444 SkipEnd = ActiveTemplateInstantiations.size() - Limit / 2;
445 }
446
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000447 // FIXME: In all of these cases, we need to show the template arguments
Douglas Gregorffed1cb2010-04-20 07:18:24 +0000448 unsigned InstantiationIdx = 0;
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000449 for (SmallVector<ActiveTemplateInstantiation, 16>::reverse_iterator
Douglas Gregor4ea568f2009-03-10 18:03:33 +0000450 Active = ActiveTemplateInstantiations.rbegin(),
451 ActiveEnd = ActiveTemplateInstantiations.rend();
452 Active != ActiveEnd;
Douglas Gregorffed1cb2010-04-20 07:18:24 +0000453 ++Active, ++InstantiationIdx) {
454 // Skip this instantiation?
455 if (InstantiationIdx >= SkipStart && InstantiationIdx < SkipEnd) {
456 if (InstantiationIdx == SkipStart) {
457 // Note that we're skipping instantiations.
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000458 Diags.Report(Active->PointOfInstantiation,
Douglas Gregorffed1cb2010-04-20 07:18:24 +0000459 diag::note_instantiation_contexts_suppressed)
460 << unsigned(ActiveTemplateInstantiations.size() - Limit);
461 }
462 continue;
463 }
464
Douglas Gregor79cf6032009-03-10 20:44:00 +0000465 switch (Active->Kind) {
466 case ActiveTemplateInstantiation::TemplateInstantiation: {
Douglas Gregor85673582009-05-18 17:01:57 +0000467 Decl *D = reinterpret_cast<Decl *>(Active->Entity);
468 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) {
469 unsigned DiagID = diag::note_template_member_class_here;
470 if (isa<ClassTemplateSpecializationDecl>(Record))
471 DiagID = diag::note_template_class_instantiation_here;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000472 Diags.Report(Active->PointOfInstantiation, DiagID)
Douglas Gregor85673582009-05-18 17:01:57 +0000473 << Context.getTypeDeclType(Record)
474 << Active->InstantiationRange;
Douglas Gregora6ef8f02009-07-24 20:34:43 +0000475 } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
Douglas Gregor4adbc6d2009-06-26 00:10:03 +0000476 unsigned DiagID;
477 if (Function->getPrimaryTemplate())
478 DiagID = diag::note_function_template_spec_here;
479 else
480 DiagID = diag::note_template_member_function_here;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000481 Diags.Report(Active->PointOfInstantiation, DiagID)
Douglas Gregor85673582009-05-18 17:01:57 +0000482 << Function
483 << Active->InstantiationRange;
Richard Smith3f1b5d02011-05-05 21:57:07 +0000484 } else if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000485 Diags.Report(Active->PointOfInstantiation,
Douglas Gregora6ef8f02009-07-24 20:34:43 +0000486 diag::note_template_static_data_member_def_here)
Richard Smith3f1b5d02011-05-05 21:57:07 +0000487 << VD
488 << Active->InstantiationRange;
Richard Smith4b38ded2012-03-14 23:13:10 +0000489 } else if (EnumDecl *ED = dyn_cast<EnumDecl>(D)) {
490 Diags.Report(Active->PointOfInstantiation,
491 diag::note_template_enum_def_here)
492 << ED
493 << Active->InstantiationRange;
Richard Smith3f1b5d02011-05-05 21:57:07 +0000494 } else {
495 Diags.Report(Active->PointOfInstantiation,
496 diag::note_template_type_alias_instantiation_here)
497 << cast<TypeAliasTemplateDecl>(D)
Douglas Gregora6ef8f02009-07-24 20:34:43 +0000498 << Active->InstantiationRange;
Douglas Gregor85673582009-05-18 17:01:57 +0000499 }
Douglas Gregor79cf6032009-03-10 20:44:00 +0000500 break;
501 }
502
503 case ActiveTemplateInstantiation::DefaultTemplateArgumentInstantiation: {
504 TemplateDecl *Template = cast<TemplateDecl>((Decl *)Active->Entity);
505 std::string TemplateArgsStr
Douglas Gregordc572a32009-03-30 22:58:21 +0000506 = TemplateSpecializationType::PrintTemplateArgumentList(
Mike Stump11289f42009-09-09 15:08:12 +0000507 Active->TemplateArgs,
Douglas Gregor7de59662009-05-29 20:38:28 +0000508 Active->NumTemplateArgs,
Douglas Gregor75acd922011-09-27 23:30:47 +0000509 getPrintingPolicy());
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000510 Diags.Report(Active->PointOfInstantiation,
Douglas Gregor79cf6032009-03-10 20:44:00 +0000511 diag::note_default_arg_instantiation_here)
512 << (Template->getNameAsString() + TemplateArgsStr)
513 << Active->InstantiationRange;
514 break;
515 }
Douglas Gregor637d9982009-06-10 23:47:09 +0000516
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000517 case ActiveTemplateInstantiation::ExplicitTemplateArgumentSubstitution: {
Mike Stump11289f42009-09-09 15:08:12 +0000518 FunctionTemplateDecl *FnTmpl
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000519 = cast<FunctionTemplateDecl>((Decl *)Active->Entity);
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000520 Diags.Report(Active->PointOfInstantiation,
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000521 diag::note_explicit_template_arg_substitution_here)
Douglas Gregor607f1412010-03-30 20:35:20 +0000522 << FnTmpl
523 << getTemplateArgumentBindingsText(FnTmpl->getTemplateParameters(),
524 Active->TemplateArgs,
525 Active->NumTemplateArgs)
526 << Active->InstantiationRange;
Douglas Gregor637d9982009-06-10 23:47:09 +0000527 break;
528 }
Mike Stump11289f42009-09-09 15:08:12 +0000529
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000530 case ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution:
531 if (ClassTemplatePartialSpecializationDecl *PartialSpec
532 = dyn_cast<ClassTemplatePartialSpecializationDecl>(
533 (Decl *)Active->Entity)) {
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000534 Diags.Report(Active->PointOfInstantiation,
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000535 diag::note_partial_spec_deduct_instantiation_here)
536 << Context.getTypeDeclType(PartialSpec)
Douglas Gregor607f1412010-03-30 20:35:20 +0000537 << getTemplateArgumentBindingsText(
538 PartialSpec->getTemplateParameters(),
539 Active->TemplateArgs,
540 Active->NumTemplateArgs)
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000541 << Active->InstantiationRange;
542 } else {
543 FunctionTemplateDecl *FnTmpl
544 = cast<FunctionTemplateDecl>((Decl *)Active->Entity);
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000545 Diags.Report(Active->PointOfInstantiation,
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000546 diag::note_function_template_deduction_instantiation_here)
Douglas Gregor607f1412010-03-30 20:35:20 +0000547 << FnTmpl
548 << getTemplateArgumentBindingsText(FnTmpl->getTemplateParameters(),
549 Active->TemplateArgs,
550 Active->NumTemplateArgs)
551 << Active->InstantiationRange;
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000552 }
553 break;
Douglas Gregor637d9982009-06-10 23:47:09 +0000554
Anders Carlsson657bad42009-09-05 05:14:19 +0000555 case ActiveTemplateInstantiation::DefaultFunctionArgumentInstantiation: {
556 ParmVarDecl *Param = cast<ParmVarDecl>((Decl *)Active->Entity);
557 FunctionDecl *FD = cast<FunctionDecl>(Param->getDeclContext());
Mike Stump11289f42009-09-09 15:08:12 +0000558
Anders Carlsson657bad42009-09-05 05:14:19 +0000559 std::string TemplateArgsStr
560 = TemplateSpecializationType::PrintTemplateArgumentList(
Mike Stump11289f42009-09-09 15:08:12 +0000561 Active->TemplateArgs,
Anders Carlsson657bad42009-09-05 05:14:19 +0000562 Active->NumTemplateArgs,
Douglas Gregor75acd922011-09-27 23:30:47 +0000563 getPrintingPolicy());
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000564 Diags.Report(Active->PointOfInstantiation,
Anders Carlsson657bad42009-09-05 05:14:19 +0000565 diag::note_default_function_arg_instantiation_here)
Anders Carlssondc6d2c32009-09-05 05:38:54 +0000566 << (FD->getNameAsString() + TemplateArgsStr)
Anders Carlsson657bad42009-09-05 05:14:19 +0000567 << Active->InstantiationRange;
568 break;
569 }
Mike Stump11289f42009-09-09 15:08:12 +0000570
Douglas Gregore62e6a02009-11-11 19:13:48 +0000571 case ActiveTemplateInstantiation::PriorTemplateArgumentSubstitution: {
572 NamedDecl *Parm = cast<NamedDecl>((Decl *)Active->Entity);
573 std::string Name;
574 if (!Parm->getName().empty())
575 Name = std::string(" '") + Parm->getName().str() + "'";
Douglas Gregorca4686d2011-01-04 23:35:54 +0000576
577 TemplateParameterList *TemplateParams = 0;
578 if (TemplateDecl *Template = dyn_cast<TemplateDecl>(Active->Template))
579 TemplateParams = Template->getTemplateParameters();
580 else
581 TemplateParams =
582 cast<ClassTemplatePartialSpecializationDecl>(Active->Template)
583 ->getTemplateParameters();
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000584 Diags.Report(Active->PointOfInstantiation,
Douglas Gregore62e6a02009-11-11 19:13:48 +0000585 diag::note_prior_template_arg_substitution)
586 << isa<TemplateTemplateParmDecl>(Parm)
587 << Name
Douglas Gregorca4686d2011-01-04 23:35:54 +0000588 << getTemplateArgumentBindingsText(TemplateParams,
Douglas Gregore62e6a02009-11-11 19:13:48 +0000589 Active->TemplateArgs,
590 Active->NumTemplateArgs)
591 << Active->InstantiationRange;
592 break;
593 }
Douglas Gregor84d49a22009-11-11 21:54:23 +0000594
595 case ActiveTemplateInstantiation::DefaultTemplateArgumentChecking: {
Douglas Gregorca4686d2011-01-04 23:35:54 +0000596 TemplateParameterList *TemplateParams = 0;
597 if (TemplateDecl *Template = dyn_cast<TemplateDecl>(Active->Template))
598 TemplateParams = Template->getTemplateParameters();
599 else
600 TemplateParams =
601 cast<ClassTemplatePartialSpecializationDecl>(Active->Template)
602 ->getTemplateParameters();
603
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000604 Diags.Report(Active->PointOfInstantiation,
Douglas Gregor84d49a22009-11-11 21:54:23 +0000605 diag::note_template_default_arg_checking)
Douglas Gregorca4686d2011-01-04 23:35:54 +0000606 << getTemplateArgumentBindingsText(TemplateParams,
Douglas Gregor84d49a22009-11-11 21:54:23 +0000607 Active->TemplateArgs,
608 Active->NumTemplateArgs)
609 << Active->InstantiationRange;
610 break;
611 }
Richard Smithf623c962012-04-17 00:58:00 +0000612
613 case ActiveTemplateInstantiation::ExceptionSpecInstantiation:
614 Diags.Report(Active->PointOfInstantiation,
615 diag::note_template_exception_spec_instantiation_here)
616 << cast<FunctionDecl>((Decl *)Active->Entity)
617 << Active->InstantiationRange;
618 break;
Douglas Gregor79cf6032009-03-10 20:44:00 +0000619 }
Douglas Gregor4ea568f2009-03-10 18:03:33 +0000620 }
621}
622
Douglas Gregoredb76852011-01-27 22:31:44 +0000623llvm::Optional<TemplateDeductionInfo *> Sema::isSFINAEContext() const {
Douglas Gregoredb76852011-01-27 22:31:44 +0000624 if (InNonInstantiationSFINAEContext)
625 return llvm::Optional<TemplateDeductionInfo *>(0);
626
Douglas Gregor33834512009-06-14 07:33:30 +0000627 for (SmallVector<ActiveTemplateInstantiation, 16>::const_reverse_iterator
628 Active = ActiveTemplateInstantiations.rbegin(),
629 ActiveEnd = ActiveTemplateInstantiations.rend();
630 Active != ActiveEnd;
Douglas Gregor84d49a22009-11-11 21:54:23 +0000631 ++Active)
632 {
Douglas Gregor33834512009-06-14 07:33:30 +0000633 switch(Active->Kind) {
Douglas Gregoredb76852011-01-27 22:31:44 +0000634 case ActiveTemplateInstantiation::TemplateInstantiation:
Richard Smith72249ba2012-04-26 07:24:08 +0000635 // An instantiation of an alias template may or may not be a SFINAE
636 // context, depending on what else is on the stack.
637 if (isa<TypeAliasTemplateDecl>(reinterpret_cast<Decl *>(Active->Entity)))
638 break;
639 // Fall through.
640 case ActiveTemplateInstantiation::DefaultFunctionArgumentInstantiation:
Richard Smithf623c962012-04-17 00:58:00 +0000641 case ActiveTemplateInstantiation::ExceptionSpecInstantiation:
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000642 // This is a template instantiation, so there is no SFINAE.
Douglas Gregoredb76852011-01-27 22:31:44 +0000643 return llvm::Optional<TemplateDeductionInfo *>();
Mike Stump11289f42009-09-09 15:08:12 +0000644
Douglas Gregor33834512009-06-14 07:33:30 +0000645 case ActiveTemplateInstantiation::DefaultTemplateArgumentInstantiation:
Douglas Gregore62e6a02009-11-11 19:13:48 +0000646 case ActiveTemplateInstantiation::PriorTemplateArgumentSubstitution:
Douglas Gregor84d49a22009-11-11 21:54:23 +0000647 case ActiveTemplateInstantiation::DefaultTemplateArgumentChecking:
Douglas Gregore62e6a02009-11-11 19:13:48 +0000648 // A default template argument instantiation and substitution into
649 // template parameters with arguments for prior parameters may or may
650 // not be a SFINAE context; look further up the stack.
Douglas Gregor33834512009-06-14 07:33:30 +0000651 break;
Mike Stump11289f42009-09-09 15:08:12 +0000652
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000653 case ActiveTemplateInstantiation::ExplicitTemplateArgumentSubstitution:
654 case ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution:
655 // We're either substitution explicitly-specified template arguments
656 // or deduced template arguments, so SFINAE applies.
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +0000657 assert(Active->DeductionInfo && "Missing deduction info pointer");
658 return Active->DeductionInfo;
Douglas Gregor33834512009-06-14 07:33:30 +0000659 }
660 }
661
Douglas Gregoredb76852011-01-27 22:31:44 +0000662 return llvm::Optional<TemplateDeductionInfo *>();
Douglas Gregor33834512009-06-14 07:33:30 +0000663}
664
Douglas Gregora8bac7f2011-01-10 07:32:04 +0000665/// \brief Retrieve the depth and index of a parameter pack.
666static std::pair<unsigned, unsigned>
667getDepthAndIndex(NamedDecl *ND) {
668 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(ND))
669 return std::make_pair(TTP->getDepth(), TTP->getIndex());
670
671 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(ND))
672 return std::make_pair(NTTP->getDepth(), NTTP->getIndex());
673
674 TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(ND);
675 return std::make_pair(TTP->getDepth(), TTP->getIndex());
676}
677
Douglas Gregorfe1e1102009-02-27 19:31:52 +0000678//===----------------------------------------------------------------------===/
679// Template Instantiation for Types
680//===----------------------------------------------------------------------===/
Douglas Gregor17c0d7b2009-02-28 00:25:32 +0000681namespace {
Douglas Gregor14cf7522010-04-30 18:55:50 +0000682 class TemplateInstantiator : public TreeTransform<TemplateInstantiator> {
Douglas Gregor01afeef2009-08-28 20:31:08 +0000683 const MultiLevelTemplateArgumentList &TemplateArgs;
Douglas Gregor17c0d7b2009-02-28 00:25:32 +0000684 SourceLocation Loc;
685 DeclarationName Entity;
Douglas Gregorfe1e1102009-02-27 19:31:52 +0000686
Douglas Gregor17c0d7b2009-02-28 00:25:32 +0000687 public:
Douglas Gregorebe10102009-08-20 07:17:43 +0000688 typedef TreeTransform<TemplateInstantiator> inherited;
Mike Stump11289f42009-09-09 15:08:12 +0000689
690 TemplateInstantiator(Sema &SemaRef,
Douglas Gregor01afeef2009-08-28 20:31:08 +0000691 const MultiLevelTemplateArgumentList &TemplateArgs,
Douglas Gregord6ff3322009-08-04 16:50:30 +0000692 SourceLocation Loc,
Mike Stump11289f42009-09-09 15:08:12 +0000693 DeclarationName Entity)
694 : inherited(SemaRef), TemplateArgs(TemplateArgs), Loc(Loc),
Douglas Gregorebe10102009-08-20 07:17:43 +0000695 Entity(Entity) { }
Douglas Gregor17c0d7b2009-02-28 00:25:32 +0000696
Mike Stump11289f42009-09-09 15:08:12 +0000697 /// \brief Determine whether the given type \p T has already been
Douglas Gregord6ff3322009-08-04 16:50:30 +0000698 /// transformed.
699 ///
700 /// For the purposes of template instantiation, a type has already been
701 /// transformed if it is NULL or if it is not dependent.
Douglas Gregor5597ab42010-05-07 23:12:07 +0000702 bool AlreadyTransformed(QualType T);
Mike Stump11289f42009-09-09 15:08:12 +0000703
Douglas Gregord6ff3322009-08-04 16:50:30 +0000704 /// \brief Returns the location of the entity being instantiated, if known.
705 SourceLocation getBaseLocation() { return Loc; }
Mike Stump11289f42009-09-09 15:08:12 +0000706
Douglas Gregord6ff3322009-08-04 16:50:30 +0000707 /// \brief Returns the name of the entity being instantiated, if any.
708 DeclarationName getBaseEntity() { return Entity; }
Mike Stump11289f42009-09-09 15:08:12 +0000709
Douglas Gregoref6ab412009-10-27 06:26:26 +0000710 /// \brief Sets the "base" location and entity when that
711 /// information is known based on another transformation.
712 void setBase(SourceLocation Loc, DeclarationName Entity) {
713 this->Loc = Loc;
714 this->Entity = Entity;
715 }
Douglas Gregor840bd6c2010-12-20 22:05:00 +0000716
717 bool TryExpandParameterPacks(SourceLocation EllipsisLoc,
718 SourceRange PatternRange,
David Blaikieb9c168a2011-09-22 02:34:54 +0000719 llvm::ArrayRef<UnexpandedParameterPack> Unexpanded,
Douglas Gregor840bd6c2010-12-20 22:05:00 +0000720 bool &ShouldExpand,
Douglas Gregora8bac7f2011-01-10 07:32:04 +0000721 bool &RetainExpansion,
Douglas Gregor0dca5fd2011-01-14 17:04:44 +0000722 llvm::Optional<unsigned> &NumExpansions) {
Douglas Gregor76aca7b2010-12-21 00:52:54 +0000723 return getSema().CheckParameterPacksForExpansion(EllipsisLoc,
724 PatternRange, Unexpanded,
Douglas Gregor76aca7b2010-12-21 00:52:54 +0000725 TemplateArgs,
726 ShouldExpand,
Douglas Gregora8bac7f2011-01-10 07:32:04 +0000727 RetainExpansion,
Douglas Gregor76aca7b2010-12-21 00:52:54 +0000728 NumExpansions);
729 }
Douglas Gregor840bd6c2010-12-20 22:05:00 +0000730
Douglas Gregorf3010112011-01-07 16:43:16 +0000731 void ExpandingFunctionParameterPack(ParmVarDecl *Pack) {
732 SemaRef.CurrentInstantiationScope->MakeInstantiatedLocalArgPack(Pack);
733 }
734
Douglas Gregora8bac7f2011-01-10 07:32:04 +0000735 TemplateArgument ForgetPartiallySubstitutedPack() {
736 TemplateArgument Result;
737 if (NamedDecl *PartialPack
738 = SemaRef.CurrentInstantiationScope->getPartiallySubstitutedPack()){
739 MultiLevelTemplateArgumentList &TemplateArgs
740 = const_cast<MultiLevelTemplateArgumentList &>(this->TemplateArgs);
741 unsigned Depth, Index;
742 llvm::tie(Depth, Index) = getDepthAndIndex(PartialPack);
743 if (TemplateArgs.hasTemplateArgument(Depth, Index)) {
744 Result = TemplateArgs(Depth, Index);
745 TemplateArgs.setArgument(Depth, Index, TemplateArgument());
746 }
747 }
748
749 return Result;
750 }
751
752 void RememberPartiallySubstitutedPack(TemplateArgument Arg) {
753 if (Arg.isNull())
754 return;
755
756 if (NamedDecl *PartialPack
757 = SemaRef.CurrentInstantiationScope->getPartiallySubstitutedPack()){
758 MultiLevelTemplateArgumentList &TemplateArgs
759 = const_cast<MultiLevelTemplateArgumentList &>(this->TemplateArgs);
760 unsigned Depth, Index;
761 llvm::tie(Depth, Index) = getDepthAndIndex(PartialPack);
762 TemplateArgs.setArgument(Depth, Index, Arg);
763 }
764 }
765
Douglas Gregord6ff3322009-08-04 16:50:30 +0000766 /// \brief Transform the given declaration by instantiating a reference to
767 /// this declaration.
Douglas Gregora04f2ca2010-03-01 15:56:25 +0000768 Decl *TransformDecl(SourceLocation Loc, Decl *D);
Douglas Gregora16548e2009-08-11 05:31:07 +0000769
Douglas Gregor0c46b2b2012-02-13 22:00:16 +0000770 void transformAttrs(Decl *Old, Decl *New) {
771 SemaRef.InstantiateAttrs(TemplateArgs, Old, New);
772 }
773
774 void transformedLocalDecl(Decl *Old, Decl *New) {
775 SemaRef.CurrentInstantiationScope->InstantiatedLocal(Old, New);
776 }
777
Mike Stump11289f42009-09-09 15:08:12 +0000778 /// \brief Transform the definition of the given declaration by
Douglas Gregorebe10102009-08-20 07:17:43 +0000779 /// instantiating it.
Douglas Gregor25289362010-03-01 17:25:41 +0000780 Decl *TransformDefinition(SourceLocation Loc, Decl *D);
Mike Stump11289f42009-09-09 15:08:12 +0000781
Douglas Gregora5cb6da2009-10-20 05:58:46 +0000782 /// \bried Transform the first qualifier within a scope by instantiating the
783 /// declaration.
784 NamedDecl *TransformFirstQualifierInScope(NamedDecl *D, SourceLocation Loc);
785
Douglas Gregorebe10102009-08-20 07:17:43 +0000786 /// \brief Rebuild the exception declaration and register the declaration
787 /// as an instantiated local.
Douglas Gregor9f0e1aa2010-09-09 17:09:21 +0000788 VarDecl *RebuildExceptionDecl(VarDecl *ExceptionDecl,
John McCallbcd03502009-12-07 02:54:59 +0000789 TypeSourceInfo *Declarator,
Abramo Bagnaradff19302011-03-08 08:55:46 +0000790 SourceLocation StartLoc,
791 SourceLocation NameLoc,
792 IdentifierInfo *Name);
Mike Stump11289f42009-09-09 15:08:12 +0000793
Douglas Gregorf4e837f2010-04-26 17:57:08 +0000794 /// \brief Rebuild the Objective-C exception declaration and register the
795 /// declaration as an instantiated local.
796 VarDecl *RebuildObjCExceptionDecl(VarDecl *ExceptionDecl,
797 TypeSourceInfo *TSInfo, QualType T);
798
John McCall7f41d982009-09-11 04:59:25 +0000799 /// \brief Check for tag mismatches when instantiating an
800 /// elaborated type.
John McCall954b5de2010-11-04 19:04:38 +0000801 QualType RebuildElaboratedType(SourceLocation KeywordLoc,
802 ElaboratedTypeKeyword Keyword,
Douglas Gregor844cb502011-03-01 18:12:44 +0000803 NestedNameSpecifierLoc QualifierLoc,
804 QualType T);
John McCall7f41d982009-09-11 04:59:25 +0000805
Douglas Gregor9db53502011-03-02 18:07:45 +0000806 TemplateName TransformTemplateName(CXXScopeSpec &SS,
807 TemplateName Name,
808 SourceLocation NameLoc,
809 QualType ObjectType = QualType(),
810 NamedDecl *FirstQualifierInScope = 0);
811
John McCalldadc5752010-08-24 06:29:42 +0000812 ExprResult TransformPredefinedExpr(PredefinedExpr *E);
813 ExprResult TransformDeclRefExpr(DeclRefExpr *E);
814 ExprResult TransformCXXDefaultArgExpr(CXXDefaultArgExpr *E);
815 ExprResult TransformTemplateParmRefExpr(DeclRefExpr *E,
Douglas Gregoreb5a39d2010-12-24 00:15:10 +0000816 NonTypeTemplateParmDecl *D);
Douglas Gregorcdbc5392011-01-15 01:15:58 +0000817 ExprResult TransformSubstNonTypeTemplateParmPackExpr(
818 SubstNonTypeTemplateParmPackExpr *E);
819
Douglas Gregor14cf7522010-04-30 18:55:50 +0000820 QualType TransformFunctionProtoType(TypeLocBuilder &TLB,
John McCall31f82722010-11-12 08:19:04 +0000821 FunctionProtoTypeLoc TL);
Douglas Gregor3024f072012-04-16 07:05:22 +0000822 QualType TransformFunctionProtoType(TypeLocBuilder &TLB,
823 FunctionProtoTypeLoc TL,
824 CXXRecordDecl *ThisContext,
825 unsigned ThisTypeQuals);
826
Douglas Gregor715e4612011-01-14 22:40:04 +0000827 ParmVarDecl *TransformFunctionTypeParam(ParmVarDecl *OldParm,
John McCall8fb0d9d2011-05-01 22:35:37 +0000828 int indexAdjustment,
Douglas Gregor0dd22bc2012-01-25 16:15:54 +0000829 llvm::Optional<unsigned> NumExpansions,
830 bool ExpectParameterPack);
John McCall58f10c32010-03-11 09:03:00 +0000831
Mike Stump11289f42009-09-09 15:08:12 +0000832 /// \brief Transforms a template type parameter type by performing
Douglas Gregord6ff3322009-08-04 16:50:30 +0000833 /// substitution of the corresponding template type argument.
John McCall550e0c22009-10-21 00:40:46 +0000834 QualType TransformTemplateTypeParmType(TypeLocBuilder &TLB,
John McCall31f82722010-11-12 08:19:04 +0000835 TemplateTypeParmTypeLoc TL);
Nick Lewyckyc96c37f2010-07-06 19:51:49 +0000836
Douglas Gregorada4b792011-01-14 02:55:32 +0000837 /// \brief Transforms an already-substituted template type parameter pack
838 /// into either itself (if we aren't substituting into its pack expansion)
839 /// or the appropriate substituted argument.
840 QualType TransformSubstTemplateTypeParmPackType(TypeLocBuilder &TLB,
841 SubstTemplateTypeParmPackTypeLoc TL);
842
John McCalldadc5752010-08-24 06:29:42 +0000843 ExprResult TransformCallExpr(CallExpr *CE) {
Nick Lewyckyc96c37f2010-07-06 19:51:49 +0000844 getSema().CallsUndergoingInstantiation.push_back(CE);
John McCalldadc5752010-08-24 06:29:42 +0000845 ExprResult Result =
Nick Lewyckyc96c37f2010-07-06 19:51:49 +0000846 TreeTransform<TemplateInstantiator>::TransformCallExpr(CE);
847 getSema().CallsUndergoingInstantiation.pop_back();
848 return move(Result);
849 }
John McCall7c454bb2011-07-15 05:09:51 +0000850
851 private:
852 ExprResult transformNonTypeTemplateParmRef(NonTypeTemplateParmDecl *parm,
853 SourceLocation loc,
854 const TemplateArgument &arg);
Douglas Gregord6ff3322009-08-04 16:50:30 +0000855 };
Douglas Gregor04318252009-07-06 15:59:29 +0000856}
857
Douglas Gregor5597ab42010-05-07 23:12:07 +0000858bool TemplateInstantiator::AlreadyTransformed(QualType T) {
859 if (T.isNull())
860 return true;
861
Douglas Gregor678d76c2011-07-01 01:22:09 +0000862 if (T->isInstantiationDependentType() || T->isVariablyModifiedType())
Douglas Gregor5597ab42010-05-07 23:12:07 +0000863 return false;
864
865 getSema().MarkDeclarationsReferencedInType(Loc, T);
866 return true;
867}
868
Douglas Gregora04f2ca2010-03-01 15:56:25 +0000869Decl *TemplateInstantiator::TransformDecl(SourceLocation Loc, Decl *D) {
Douglas Gregor2b6ca462009-09-03 21:38:09 +0000870 if (!D)
871 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000872
Douglas Gregor2b6ca462009-09-03 21:38:09 +0000873 if (TemplateTemplateParmDecl *TTP = dyn_cast<TemplateTemplateParmDecl>(D)) {
Douglas Gregor01afeef2009-08-28 20:31:08 +0000874 if (TTP->getDepth() < TemplateArgs.getNumLevels()) {
Douglas Gregorb93971082010-02-05 19:54:12 +0000875 // If the corresponding template argument is NULL or non-existent, it's
876 // because we are performing instantiation from explicitly-specified
877 // template arguments in a function template, but there were some
878 // arguments left unspecified.
879 if (!TemplateArgs.hasTemplateArgument(TTP->getDepth(),
880 TTP->getPosition()))
881 return D;
882
Douglas Gregorf5500772011-01-05 15:48:55 +0000883 TemplateArgument Arg = TemplateArgs(TTP->getDepth(), TTP->getPosition());
884
885 if (TTP->isParameterPack()) {
886 assert(Arg.getKind() == TemplateArgument::Pack &&
887 "Missing argument pack");
888
Douglas Gregor5590be02011-01-15 06:45:20 +0000889 assert(getSema().ArgumentPackSubstitutionIndex >= 0);
Douglas Gregora8bac7f2011-01-10 07:32:04 +0000890 assert(getSema().ArgumentPackSubstitutionIndex < (int)Arg.pack_size());
Douglas Gregorf5500772011-01-05 15:48:55 +0000891 Arg = Arg.pack_begin()[getSema().ArgumentPackSubstitutionIndex];
892 }
893
894 TemplateName Template = Arg.getAsTemplate();
Douglas Gregor9167f8b2009-11-11 01:00:40 +0000895 assert(!Template.isNull() && Template.getAsTemplateDecl() &&
Douglas Gregor01afeef2009-08-28 20:31:08 +0000896 "Wrong kind of template template argument");
Douglas Gregor9167f8b2009-11-11 01:00:40 +0000897 return Template.getAsTemplateDecl();
Douglas Gregor01afeef2009-08-28 20:31:08 +0000898 }
Mike Stump11289f42009-09-09 15:08:12 +0000899
Douglas Gregor9167f8b2009-11-11 01:00:40 +0000900 // Fall through to find the instantiated declaration for this template
901 // template parameter.
Douglas Gregor71dc5092009-08-06 06:41:21 +0000902 }
Mike Stump11289f42009-09-09 15:08:12 +0000903
Douglas Gregora04f2ca2010-03-01 15:56:25 +0000904 return SemaRef.FindInstantiatedDecl(Loc, cast<NamedDecl>(D), TemplateArgs);
Douglas Gregord6ff3322009-08-04 16:50:30 +0000905}
906
Douglas Gregor25289362010-03-01 17:25:41 +0000907Decl *TemplateInstantiator::TransformDefinition(SourceLocation Loc, Decl *D) {
John McCall76d824f2009-08-25 22:02:44 +0000908 Decl *Inst = getSema().SubstDecl(D, getSema().CurContext, TemplateArgs);
Douglas Gregorebe10102009-08-20 07:17:43 +0000909 if (!Inst)
910 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000911
Douglas Gregorebe10102009-08-20 07:17:43 +0000912 getSema().CurrentInstantiationScope->InstantiatedLocal(D, Inst);
913 return Inst;
914}
915
Douglas Gregora5cb6da2009-10-20 05:58:46 +0000916NamedDecl *
917TemplateInstantiator::TransformFirstQualifierInScope(NamedDecl *D,
918 SourceLocation Loc) {
919 // If the first part of the nested-name-specifier was a template type
920 // parameter, instantiate that type parameter down to a tag type.
921 if (TemplateTypeParmDecl *TTPD = dyn_cast_or_null<TemplateTypeParmDecl>(D)) {
922 const TemplateTypeParmType *TTP
923 = cast<TemplateTypeParmType>(getSema().Context.getTypeDeclType(TTPD));
Douglas Gregor53c3f4e2010-12-20 22:48:17 +0000924
Douglas Gregora5cb6da2009-10-20 05:58:46 +0000925 if (TTP->getDepth() < TemplateArgs.getNumLevels()) {
Douglas Gregor53c3f4e2010-12-20 22:48:17 +0000926 // FIXME: This needs testing w/ member access expressions.
927 TemplateArgument Arg = TemplateArgs(TTP->getDepth(), TTP->getIndex());
928
929 if (TTP->isParameterPack()) {
930 assert(Arg.getKind() == TemplateArgument::Pack &&
931 "Missing argument pack");
932
Douglas Gregore1d60df2011-01-14 23:41:42 +0000933 if (getSema().ArgumentPackSubstitutionIndex == -1)
Douglas Gregor53c3f4e2010-12-20 22:48:17 +0000934 return 0;
Douglas Gregor53c3f4e2010-12-20 22:48:17 +0000935
Douglas Gregora8bac7f2011-01-10 07:32:04 +0000936 assert(getSema().ArgumentPackSubstitutionIndex < (int)Arg.pack_size());
Douglas Gregor53c3f4e2010-12-20 22:48:17 +0000937 Arg = Arg.pack_begin()[getSema().ArgumentPackSubstitutionIndex];
938 }
939
940 QualType T = Arg.getAsType();
Douglas Gregora5cb6da2009-10-20 05:58:46 +0000941 if (T.isNull())
Douglas Gregora04f2ca2010-03-01 15:56:25 +0000942 return cast_or_null<NamedDecl>(TransformDecl(Loc, D));
Douglas Gregora5cb6da2009-10-20 05:58:46 +0000943
944 if (const TagType *Tag = T->getAs<TagType>())
945 return Tag->getDecl();
946
947 // The resulting type is not a tag; complain.
948 getSema().Diag(Loc, diag::err_nested_name_spec_non_tag) << T;
949 return 0;
950 }
951 }
952
Douglas Gregora04f2ca2010-03-01 15:56:25 +0000953 return cast_or_null<NamedDecl>(TransformDecl(Loc, D));
Douglas Gregora5cb6da2009-10-20 05:58:46 +0000954}
955
Douglas Gregorebe10102009-08-20 07:17:43 +0000956VarDecl *
957TemplateInstantiator::RebuildExceptionDecl(VarDecl *ExceptionDecl,
John McCallbcd03502009-12-07 02:54:59 +0000958 TypeSourceInfo *Declarator,
Abramo Bagnaradff19302011-03-08 08:55:46 +0000959 SourceLocation StartLoc,
960 SourceLocation NameLoc,
961 IdentifierInfo *Name) {
Douglas Gregor9f0e1aa2010-09-09 17:09:21 +0000962 VarDecl *Var = inherited::RebuildExceptionDecl(ExceptionDecl, Declarator,
Abramo Bagnaradff19302011-03-08 08:55:46 +0000963 StartLoc, NameLoc, Name);
Douglas Gregorf4e837f2010-04-26 17:57:08 +0000964 if (Var)
965 getSema().CurrentInstantiationScope->InstantiatedLocal(ExceptionDecl, Var);
966 return Var;
967}
968
969VarDecl *TemplateInstantiator::RebuildObjCExceptionDecl(VarDecl *ExceptionDecl,
970 TypeSourceInfo *TSInfo,
971 QualType T) {
972 VarDecl *Var = inherited::RebuildObjCExceptionDecl(ExceptionDecl, TSInfo, T);
973 if (Var)
Douglas Gregorebe10102009-08-20 07:17:43 +0000974 getSema().CurrentInstantiationScope->InstantiatedLocal(ExceptionDecl, Var);
975 return Var;
976}
977
John McCall7f41d982009-09-11 04:59:25 +0000978QualType
John McCall954b5de2010-11-04 19:04:38 +0000979TemplateInstantiator::RebuildElaboratedType(SourceLocation KeywordLoc,
980 ElaboratedTypeKeyword Keyword,
Douglas Gregor844cb502011-03-01 18:12:44 +0000981 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara6150c882010-05-11 21:36:43 +0000982 QualType T) {
John McCall7f41d982009-09-11 04:59:25 +0000983 if (const TagType *TT = T->getAs<TagType>()) {
984 TagDecl* TD = TT->getDecl();
985
John McCall954b5de2010-11-04 19:04:38 +0000986 SourceLocation TagLocation = KeywordLoc;
John McCall7f41d982009-09-11 04:59:25 +0000987
988 // FIXME: type might be anonymous.
989 IdentifierInfo *Id = TD->getIdentifier();
990
991 // TODO: should we even warn on struct/class mismatches for this? Seems
992 // like it's likely to produce a lot of spurious errors.
Abramo Bagnara6150c882010-05-11 21:36:43 +0000993 if (Keyword != ETK_None && Keyword != ETK_Typename) {
994 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForKeyword(Keyword);
Richard Trieucaa33d32011-06-10 03:11:26 +0000995 if (!SemaRef.isAcceptableTagRedeclaration(TD, Kind, /*isDefinition*/false,
996 TagLocation, *Id)) {
Abramo Bagnara6150c882010-05-11 21:36:43 +0000997 SemaRef.Diag(TagLocation, diag::err_use_with_wrong_tag)
998 << Id
999 << FixItHint::CreateReplacement(SourceRange(TagLocation),
1000 TD->getKindName());
1001 SemaRef.Diag(TD->getLocation(), diag::note_previous_use);
1002 }
John McCall7f41d982009-09-11 04:59:25 +00001003 }
1004 }
1005
John McCall954b5de2010-11-04 19:04:38 +00001006 return TreeTransform<TemplateInstantiator>::RebuildElaboratedType(KeywordLoc,
1007 Keyword,
Douglas Gregor844cb502011-03-01 18:12:44 +00001008 QualifierLoc,
1009 T);
John McCall7f41d982009-09-11 04:59:25 +00001010}
1011
Douglas Gregor9db53502011-03-02 18:07:45 +00001012TemplateName TemplateInstantiator::TransformTemplateName(CXXScopeSpec &SS,
1013 TemplateName Name,
1014 SourceLocation NameLoc,
1015 QualType ObjectType,
1016 NamedDecl *FirstQualifierInScope) {
1017 if (TemplateTemplateParmDecl *TTP
1018 = dyn_cast_or_null<TemplateTemplateParmDecl>(Name.getAsTemplateDecl())) {
1019 if (TTP->getDepth() < TemplateArgs.getNumLevels()) {
1020 // If the corresponding template argument is NULL or non-existent, it's
1021 // because we are performing instantiation from explicitly-specified
1022 // template arguments in a function template, but there were some
1023 // arguments left unspecified.
1024 if (!TemplateArgs.hasTemplateArgument(TTP->getDepth(),
1025 TTP->getPosition()))
1026 return Name;
1027
1028 TemplateArgument Arg = TemplateArgs(TTP->getDepth(), TTP->getPosition());
1029
1030 if (TTP->isParameterPack()) {
1031 assert(Arg.getKind() == TemplateArgument::Pack &&
1032 "Missing argument pack");
1033
1034 if (getSema().ArgumentPackSubstitutionIndex == -1) {
1035 // We have the template argument pack to substitute, but we're not
1036 // actually expanding the enclosing pack expansion yet. So, just
1037 // keep the entire argument pack.
1038 return getSema().Context.getSubstTemplateTemplateParmPack(TTP, Arg);
1039 }
1040
1041 assert(getSema().ArgumentPackSubstitutionIndex < (int)Arg.pack_size());
1042 Arg = Arg.pack_begin()[getSema().ArgumentPackSubstitutionIndex];
1043 }
1044
1045 TemplateName Template = Arg.getAsTemplate();
Richard Smith3f1b5d02011-05-05 21:57:07 +00001046 assert(!Template.isNull() && "Null template template argument");
John McCalld9dfe3a2011-06-30 08:33:18 +00001047
Douglas Gregor9d9f8db2011-03-05 20:06:51 +00001048 // We don't ever want to substitute for a qualified template name, since
1049 // the qualifier is handled separately. So, look through the qualified
1050 // template name to its underlying declaration.
1051 if (QualifiedTemplateName *QTN = Template.getAsQualifiedTemplateName())
1052 Template = TemplateName(QTN->getTemplateDecl());
John McCalld9dfe3a2011-06-30 08:33:18 +00001053
1054 Template = getSema().Context.getSubstTemplateTemplateParm(TTP, Template);
Douglas Gregor9db53502011-03-02 18:07:45 +00001055 return Template;
1056 }
1057 }
1058
1059 if (SubstTemplateTemplateParmPackStorage *SubstPack
1060 = Name.getAsSubstTemplateTemplateParmPack()) {
1061 if (getSema().ArgumentPackSubstitutionIndex == -1)
1062 return Name;
1063
1064 const TemplateArgument &ArgPack = SubstPack->getArgumentPack();
1065 assert(getSema().ArgumentPackSubstitutionIndex < (int)ArgPack.pack_size() &&
1066 "Pack substitution index out-of-range");
1067 return ArgPack.pack_begin()[getSema().ArgumentPackSubstitutionIndex]
1068 .getAsTemplate();
1069 }
1070
1071 return inherited::TransformTemplateName(SS, Name, NameLoc, ObjectType,
1072 FirstQualifierInScope);
1073}
1074
John McCalldadc5752010-08-24 06:29:42 +00001075ExprResult
John McCall47f29ea2009-12-08 09:21:05 +00001076TemplateInstantiator::TransformPredefinedExpr(PredefinedExpr *E) {
Anders Carlsson0b209a82009-09-11 01:22:35 +00001077 if (!E->isTypeDependent())
John McCallc3007a22010-10-26 07:05:15 +00001078 return SemaRef.Owned(E);
Anders Carlsson0b209a82009-09-11 01:22:35 +00001079
1080 FunctionDecl *currentDecl = getSema().getCurFunctionDecl();
1081 assert(currentDecl && "Must have current function declaration when "
1082 "instantiating.");
1083
1084 PredefinedExpr::IdentType IT = E->getIdentType();
1085
Anders Carlsson5bd8d192010-02-11 18:20:28 +00001086 unsigned Length = PredefinedExpr::ComputeName(IT, currentDecl).length();
Anders Carlsson0b209a82009-09-11 01:22:35 +00001087
1088 llvm::APInt LengthI(32, Length + 1);
Nico Weber606cef42012-06-25 22:34:48 +00001089 QualType ResTy;
1090 if (IT == PredefinedExpr::LFunction)
1091 ResTy = getSema().Context.WCharTy.withConst();
1092 else
1093 ResTy = getSema().Context.CharTy.withConst();
Anders Carlsson0b209a82009-09-11 01:22:35 +00001094 ResTy = getSema().Context.getConstantArrayType(ResTy, LengthI,
1095 ArrayType::Normal, 0);
1096 PredefinedExpr *PE =
1097 new (getSema().Context) PredefinedExpr(E->getLocation(), ResTy, IT);
1098 return getSema().Owned(PE);
1099}
1100
John McCalldadc5752010-08-24 06:29:42 +00001101ExprResult
John McCall13481c52010-02-06 08:42:39 +00001102TemplateInstantiator::TransformTemplateParmRefExpr(DeclRefExpr *E,
Douglas Gregor6c379e22010-02-08 23:41:45 +00001103 NonTypeTemplateParmDecl *NTTP) {
John McCall13481c52010-02-06 08:42:39 +00001104 // If the corresponding template argument is NULL or non-existent, it's
1105 // because we are performing instantiation from explicitly-specified
1106 // template arguments in a function template, but there were some
1107 // arguments left unspecified.
1108 if (!TemplateArgs.hasTemplateArgument(NTTP->getDepth(),
1109 NTTP->getPosition()))
John McCallc3007a22010-10-26 07:05:15 +00001110 return SemaRef.Owned(E);
Mike Stump11289f42009-09-09 15:08:12 +00001111
Douglas Gregoreb5a39d2010-12-24 00:15:10 +00001112 TemplateArgument Arg = TemplateArgs(NTTP->getDepth(), NTTP->getPosition());
1113 if (NTTP->isParameterPack()) {
1114 assert(Arg.getKind() == TemplateArgument::Pack &&
1115 "Missing argument pack");
1116
1117 if (getSema().ArgumentPackSubstitutionIndex == -1) {
Douglas Gregorcdbc5392011-01-15 01:15:58 +00001118 // We have an argument pack, but we can't select a particular argument
1119 // out of it yet. Therefore, we'll build an expression to hold on to that
1120 // argument pack.
1121 QualType TargetType = SemaRef.SubstType(NTTP->getType(), TemplateArgs,
1122 E->getLocation(),
1123 NTTP->getDeclName());
1124 if (TargetType.isNull())
1125 return ExprError();
1126
1127 return new (SemaRef.Context) SubstNonTypeTemplateParmPackExpr(TargetType,
1128 NTTP,
1129 E->getLocation(),
1130 Arg);
Douglas Gregoreb5a39d2010-12-24 00:15:10 +00001131 }
1132
Douglas Gregora8bac7f2011-01-10 07:32:04 +00001133 assert(getSema().ArgumentPackSubstitutionIndex < (int)Arg.pack_size());
Douglas Gregoreb5a39d2010-12-24 00:15:10 +00001134 Arg = Arg.pack_begin()[getSema().ArgumentPackSubstitutionIndex];
1135 }
Mike Stump11289f42009-09-09 15:08:12 +00001136
John McCall7c454bb2011-07-15 05:09:51 +00001137 return transformNonTypeTemplateParmRef(NTTP, E->getLocation(), Arg);
1138}
1139
1140ExprResult TemplateInstantiator::transformNonTypeTemplateParmRef(
1141 NonTypeTemplateParmDecl *parm,
1142 SourceLocation loc,
1143 const TemplateArgument &arg) {
1144 ExprResult result;
1145 QualType type;
1146
John McCall13481c52010-02-06 08:42:39 +00001147 // The template argument itself might be an expression, in which
1148 // case we just return that expression.
John McCall7c454bb2011-07-15 05:09:51 +00001149 if (arg.getKind() == TemplateArgument::Expression) {
1150 Expr *argExpr = arg.getAsExpr();
1151 result = SemaRef.Owned(argExpr);
1152 type = argExpr->getType();
Mike Stump11289f42009-09-09 15:08:12 +00001153
John McCall7c454bb2011-07-15 05:09:51 +00001154 } else if (arg.getKind() == TemplateArgument::Declaration) {
Douglas Gregor31f55dc2012-04-06 22:40:38 +00001155 ValueDecl *VD;
1156 if (Decl *D = arg.getAsDecl()) {
1157 VD = cast<ValueDecl>(D);
Mike Stump11289f42009-09-09 15:08:12 +00001158
Douglas Gregor31f55dc2012-04-06 22:40:38 +00001159 // Find the instantiation of the template argument. This is
1160 // required for nested templates.
1161 VD = cast_or_null<ValueDecl>(
1162 getSema().FindInstantiatedDecl(loc, VD, TemplateArgs));
1163 if (!VD)
1164 return ExprError();
1165 } else {
1166 // Propagate NULL template argument.
1167 VD = 0;
1168 }
1169
John McCall15dda372010-02-06 10:23:53 +00001170 // Derive the type we want the substituted decl to have. This had
1171 // better be non-dependent, or these checks will have serious problems.
John McCall7c454bb2011-07-15 05:09:51 +00001172 if (parm->isExpandedParameterPack()) {
1173 type = parm->getExpansionType(SemaRef.ArgumentPackSubstitutionIndex);
1174 } else if (parm->isParameterPack() &&
1175 isa<PackExpansionType>(parm->getType())) {
1176 type = SemaRef.SubstType(
1177 cast<PackExpansionType>(parm->getType())->getPattern(),
1178 TemplateArgs, loc, parm->getDeclName());
1179 } else {
1180 type = SemaRef.SubstType(parm->getType(), TemplateArgs,
1181 loc, parm->getDeclName());
1182 }
1183 assert(!type.isNull() && "type substitution failed for param type");
1184 assert(!type->isDependentType() && "param type still dependent");
1185 result = SemaRef.BuildExpressionFromDeclTemplateArgument(arg, type, loc);
John McCall13481c52010-02-06 08:42:39 +00001186
John McCall7c454bb2011-07-15 05:09:51 +00001187 if (!result.isInvalid()) type = result.get()->getType();
1188 } else {
1189 result = SemaRef.BuildExpressionFromIntegralTemplateArgument(arg, loc);
1190
1191 // Note that this type can be different from the type of 'result',
1192 // e.g. if it's an enum type.
1193 type = arg.getIntegralType();
1194 }
1195 if (result.isInvalid()) return ExprError();
1196
1197 Expr *resultExpr = result.take();
1198 return SemaRef.Owned(new (SemaRef.Context)
1199 SubstNonTypeTemplateParmExpr(type,
1200 resultExpr->getValueKind(),
1201 loc, parm, resultExpr));
John McCall13481c52010-02-06 08:42:39 +00001202}
1203
Douglas Gregorcdbc5392011-01-15 01:15:58 +00001204ExprResult
1205TemplateInstantiator::TransformSubstNonTypeTemplateParmPackExpr(
1206 SubstNonTypeTemplateParmPackExpr *E) {
1207 if (getSema().ArgumentPackSubstitutionIndex == -1) {
1208 // We aren't expanding the parameter pack, so just return ourselves.
1209 return getSema().Owned(E);
1210 }
1211
Douglas Gregorcdbc5392011-01-15 01:15:58 +00001212 const TemplateArgument &ArgPack = E->getArgumentPack();
1213 unsigned Index = (unsigned)getSema().ArgumentPackSubstitutionIndex;
1214 assert(Index < ArgPack.pack_size() && "Substitution index out-of-range");
1215
1216 const TemplateArgument &Arg = ArgPack.pack_begin()[Index];
John McCall7c454bb2011-07-15 05:09:51 +00001217 return transformNonTypeTemplateParmRef(E->getParameterPack(),
1218 E->getParameterPackLocation(),
1219 Arg);
Douglas Gregorcdbc5392011-01-15 01:15:58 +00001220}
John McCall13481c52010-02-06 08:42:39 +00001221
John McCalldadc5752010-08-24 06:29:42 +00001222ExprResult
John McCall13481c52010-02-06 08:42:39 +00001223TemplateInstantiator::TransformDeclRefExpr(DeclRefExpr *E) {
1224 NamedDecl *D = E->getDecl();
1225 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(D)) {
1226 if (NTTP->getDepth() < TemplateArgs.getNumLevels())
1227 return TransformTemplateParmRefExpr(E, NTTP);
Douglas Gregor954de172009-10-31 17:21:17 +00001228
1229 // We have a non-type template parameter that isn't fully substituted;
1230 // FindInstantiatedDecl will find it in the local instantiation scope.
Douglas Gregora16548e2009-08-11 05:31:07 +00001231 }
Mike Stump11289f42009-09-09 15:08:12 +00001232
John McCall47f29ea2009-12-08 09:21:05 +00001233 return TreeTransform<TemplateInstantiator>::TransformDeclRefExpr(E);
Douglas Gregora16548e2009-08-11 05:31:07 +00001234}
1235
John McCalldadc5752010-08-24 06:29:42 +00001236ExprResult TemplateInstantiator::TransformCXXDefaultArgExpr(
John McCall47f29ea2009-12-08 09:21:05 +00001237 CXXDefaultArgExpr *E) {
Sebastian Redl14236c82009-11-08 13:56:19 +00001238 assert(!cast<FunctionDecl>(E->getParam()->getDeclContext())->
1239 getDescribedFunctionTemplate() &&
1240 "Default arg expressions are never formed in dependent cases.");
Douglas Gregor033f6752009-12-23 23:03:06 +00001241 return SemaRef.BuildCXXDefaultArgExpr(E->getUsedLocation(),
1242 cast<FunctionDecl>(E->getParam()->getDeclContext()),
1243 E->getParam());
Sebastian Redl14236c82009-11-08 13:56:19 +00001244}
1245
Douglas Gregor14cf7522010-04-30 18:55:50 +00001246QualType TemplateInstantiator::TransformFunctionProtoType(TypeLocBuilder &TLB,
John McCall31f82722010-11-12 08:19:04 +00001247 FunctionProtoTypeLoc TL) {
Douglas Gregor14cf7522010-04-30 18:55:50 +00001248 // We need a local instantiation scope for this function prototype.
John McCall19c1bfd2010-08-25 05:32:35 +00001249 LocalInstantiationScope Scope(SemaRef, /*CombineWithOuterScope=*/true);
John McCall31f82722010-11-12 08:19:04 +00001250 return inherited::TransformFunctionProtoType(TLB, TL);
John McCall58f10c32010-03-11 09:03:00 +00001251}
1252
Douglas Gregor3024f072012-04-16 07:05:22 +00001253QualType TemplateInstantiator::TransformFunctionProtoType(TypeLocBuilder &TLB,
1254 FunctionProtoTypeLoc TL,
1255 CXXRecordDecl *ThisContext,
1256 unsigned ThisTypeQuals) {
1257 // We need a local instantiation scope for this function prototype.
1258 LocalInstantiationScope Scope(SemaRef, /*CombineWithOuterScope=*/true);
1259 return inherited::TransformFunctionProtoType(TLB, TL, ThisContext,
1260 ThisTypeQuals);
1261}
1262
John McCall58f10c32010-03-11 09:03:00 +00001263ParmVarDecl *
Douglas Gregor715e4612011-01-14 22:40:04 +00001264TemplateInstantiator::TransformFunctionTypeParam(ParmVarDecl *OldParm,
John McCall8fb0d9d2011-05-01 22:35:37 +00001265 int indexAdjustment,
Douglas Gregor0dd22bc2012-01-25 16:15:54 +00001266 llvm::Optional<unsigned> NumExpansions,
1267 bool ExpectParameterPack) {
John McCall8fb0d9d2011-05-01 22:35:37 +00001268 return SemaRef.SubstParmVarDecl(OldParm, TemplateArgs, indexAdjustment,
Douglas Gregor0dd22bc2012-01-25 16:15:54 +00001269 NumExpansions, ExpectParameterPack);
John McCall58f10c32010-03-11 09:03:00 +00001270}
1271
Mike Stump11289f42009-09-09 15:08:12 +00001272QualType
John McCall550e0c22009-10-21 00:40:46 +00001273TemplateInstantiator::TransformTemplateTypeParmType(TypeLocBuilder &TLB,
John McCall31f82722010-11-12 08:19:04 +00001274 TemplateTypeParmTypeLoc TL) {
John McCall424cec92011-01-19 06:33:43 +00001275 const TemplateTypeParmType *T = TL.getTypePtr();
Douglas Gregor01afeef2009-08-28 20:31:08 +00001276 if (T->getDepth() < TemplateArgs.getNumLevels()) {
Douglas Gregorfe1e1102009-02-27 19:31:52 +00001277 // Replace the template type parameter with its corresponding
1278 // template argument.
Mike Stump11289f42009-09-09 15:08:12 +00001279
1280 // If the corresponding template argument is NULL or doesn't exist, it's
1281 // because we are performing instantiation from explicitly-specified
1282 // template arguments in a function template class, but there were some
Douglas Gregore3f1f352009-07-01 00:28:38 +00001283 // arguments left unspecified.
John McCall550e0c22009-10-21 00:40:46 +00001284 if (!TemplateArgs.hasTemplateArgument(T->getDepth(), T->getIndex())) {
1285 TemplateTypeParmTypeLoc NewTL
1286 = TLB.push<TemplateTypeParmTypeLoc>(TL.getType());
1287 NewTL.setNameLoc(TL.getNameLoc());
1288 return TL.getType();
1289 }
Mike Stump11289f42009-09-09 15:08:12 +00001290
Douglas Gregor840bd6c2010-12-20 22:05:00 +00001291 TemplateArgument Arg = TemplateArgs(T->getDepth(), T->getIndex());
1292
1293 if (T->isParameterPack()) {
1294 assert(Arg.getKind() == TemplateArgument::Pack &&
1295 "Missing argument pack");
1296
1297 if (getSema().ArgumentPackSubstitutionIndex == -1) {
Douglas Gregorada4b792011-01-14 02:55:32 +00001298 // We have the template argument pack, but we're not expanding the
1299 // enclosing pack expansion yet. Just save the template argument
1300 // pack for later substitution.
1301 QualType Result
1302 = getSema().Context.getSubstTemplateTypeParmPackType(T, Arg);
1303 SubstTemplateTypeParmPackTypeLoc NewTL
1304 = TLB.push<SubstTemplateTypeParmPackTypeLoc>(Result);
1305 NewTL.setNameLoc(TL.getNameLoc());
1306 return Result;
Douglas Gregor840bd6c2010-12-20 22:05:00 +00001307 }
1308
Douglas Gregora8bac7f2011-01-10 07:32:04 +00001309 assert(getSema().ArgumentPackSubstitutionIndex < (int)Arg.pack_size());
Douglas Gregor840bd6c2010-12-20 22:05:00 +00001310 Arg = Arg.pack_begin()[getSema().ArgumentPackSubstitutionIndex];
1311 }
1312
1313 assert(Arg.getKind() == TemplateArgument::Type &&
Douglas Gregorfe1e1102009-02-27 19:31:52 +00001314 "Template argument kind mismatch");
Douglas Gregor01afeef2009-08-28 20:31:08 +00001315
Douglas Gregor840bd6c2010-12-20 22:05:00 +00001316 QualType Replacement = Arg.getAsType();
John McCallcebee162009-10-18 09:09:24 +00001317
1318 // TODO: only do this uniquing once, at the start of instantiation.
John McCall550e0c22009-10-21 00:40:46 +00001319 QualType Result
1320 = getSema().Context.getSubstTemplateTypeParmType(T, Replacement);
1321 SubstTemplateTypeParmTypeLoc NewTL
1322 = TLB.push<SubstTemplateTypeParmTypeLoc>(Result);
1323 NewTL.setNameLoc(TL.getNameLoc());
1324 return Result;
Mike Stump11289f42009-09-09 15:08:12 +00001325 }
Douglas Gregorfe1e1102009-02-27 19:31:52 +00001326
1327 // The template type parameter comes from an inner template (e.g.,
1328 // the template parameter list of a member template inside the
1329 // template we are instantiating). Create a new template type
1330 // parameter with the template "level" reduced by one.
Chandler Carruth08836322011-05-01 00:51:33 +00001331 TemplateTypeParmDecl *NewTTPDecl = 0;
1332 if (TemplateTypeParmDecl *OldTTPDecl = T->getDecl())
1333 NewTTPDecl = cast_or_null<TemplateTypeParmDecl>(
1334 TransformDecl(TL.getNameLoc(), OldTTPDecl));
1335
John McCall550e0c22009-10-21 00:40:46 +00001336 QualType Result
1337 = getSema().Context.getTemplateTypeParmType(T->getDepth()
1338 - TemplateArgs.getNumLevels(),
1339 T->getIndex(),
1340 T->isParameterPack(),
Chandler Carruth08836322011-05-01 00:51:33 +00001341 NewTTPDecl);
John McCall550e0c22009-10-21 00:40:46 +00001342 TemplateTypeParmTypeLoc NewTL = TLB.push<TemplateTypeParmTypeLoc>(Result);
1343 NewTL.setNameLoc(TL.getNameLoc());
1344 return Result;
Douglas Gregor17c0d7b2009-02-28 00:25:32 +00001345}
Douglas Gregorfe1e1102009-02-27 19:31:52 +00001346
Douglas Gregorada4b792011-01-14 02:55:32 +00001347QualType
1348TemplateInstantiator::TransformSubstTemplateTypeParmPackType(
1349 TypeLocBuilder &TLB,
1350 SubstTemplateTypeParmPackTypeLoc TL) {
1351 if (getSema().ArgumentPackSubstitutionIndex == -1) {
1352 // We aren't expanding the parameter pack, so just return ourselves.
1353 SubstTemplateTypeParmPackTypeLoc NewTL
1354 = TLB.push<SubstTemplateTypeParmPackTypeLoc>(TL.getType());
1355 NewTL.setNameLoc(TL.getNameLoc());
1356 return TL.getType();
1357 }
1358
1359 const TemplateArgument &ArgPack = TL.getTypePtr()->getArgumentPack();
1360 unsigned Index = (unsigned)getSema().ArgumentPackSubstitutionIndex;
1361 assert(Index < ArgPack.pack_size() && "Substitution index out-of-range");
1362
1363 QualType Result = ArgPack.pack_begin()[Index].getAsType();
1364 Result = getSema().Context.getSubstTemplateTypeParmType(
1365 TL.getTypePtr()->getReplacedParameter(),
1366 Result);
1367 SubstTemplateTypeParmTypeLoc NewTL
1368 = TLB.push<SubstTemplateTypeParmTypeLoc>(Result);
1369 NewTL.setNameLoc(TL.getNameLoc());
1370 return Result;
1371}
1372
John McCall76d824f2009-08-25 22:02:44 +00001373/// \brief Perform substitution on the type T with a given set of template
1374/// arguments.
Douglas Gregorfe1e1102009-02-27 19:31:52 +00001375///
1376/// This routine substitutes the given template arguments into the
1377/// type T and produces the instantiated type.
1378///
1379/// \param T the type into which the template arguments will be
1380/// substituted. If this type is not dependent, it will be returned
1381/// immediately.
1382///
James Dennett634962f2012-06-14 21:40:34 +00001383/// \param Args the template arguments that will be
Douglas Gregorfe1e1102009-02-27 19:31:52 +00001384/// substituted for the top-level template parameters within T.
1385///
Douglas Gregorfe1e1102009-02-27 19:31:52 +00001386/// \param Loc the location in the source code where this substitution
1387/// is being performed. It will typically be the location of the
1388/// declarator (if we're instantiating the type of some declaration)
1389/// or the location of the type in the source code (if, e.g., we're
1390/// instantiating the type of a cast expression).
1391///
1392/// \param Entity the name of the entity associated with a declaration
1393/// being instantiated (if any). May be empty to indicate that there
1394/// is no such entity (if, e.g., this is a type that occurs as part of
1395/// a cast expression) or that the entity has no name (e.g., an
1396/// unnamed function parameter).
1397///
1398/// \returns If the instantiation succeeds, the instantiated
1399/// type. Otherwise, produces diagnostics and returns a NULL type.
John McCallbcd03502009-12-07 02:54:59 +00001400TypeSourceInfo *Sema::SubstType(TypeSourceInfo *T,
John McCall609459e2009-10-21 00:58:09 +00001401 const MultiLevelTemplateArgumentList &Args,
1402 SourceLocation Loc,
1403 DeclarationName Entity) {
1404 assert(!ActiveTemplateInstantiations.empty() &&
1405 "Cannot perform an instantiation without some context on the "
1406 "instantiation stack");
1407
Douglas Gregor678d76c2011-07-01 01:22:09 +00001408 if (!T->getType()->isInstantiationDependentType() &&
Douglas Gregor5a5073e2010-05-24 17:22:01 +00001409 !T->getType()->isVariablyModifiedType())
John McCall609459e2009-10-21 00:58:09 +00001410 return T;
1411
1412 TemplateInstantiator Instantiator(*this, Args, Loc, Entity);
1413 return Instantiator.TransformType(T);
1414}
1415
Douglas Gregor5499af42011-01-05 23:12:31 +00001416TypeSourceInfo *Sema::SubstType(TypeLoc TL,
1417 const MultiLevelTemplateArgumentList &Args,
1418 SourceLocation Loc,
1419 DeclarationName Entity) {
1420 assert(!ActiveTemplateInstantiations.empty() &&
1421 "Cannot perform an instantiation without some context on the "
1422 "instantiation stack");
1423
1424 if (TL.getType().isNull())
1425 return 0;
1426
Douglas Gregor678d76c2011-07-01 01:22:09 +00001427 if (!TL.getType()->isInstantiationDependentType() &&
Douglas Gregor5499af42011-01-05 23:12:31 +00001428 !TL.getType()->isVariablyModifiedType()) {
1429 // FIXME: Make a copy of the TypeLoc data here, so that we can
1430 // return a new TypeSourceInfo. Inefficient!
1431 TypeLocBuilder TLB;
1432 TLB.pushFullCopy(TL);
1433 return TLB.getTypeSourceInfo(Context, TL.getType());
1434 }
1435
1436 TemplateInstantiator Instantiator(*this, Args, Loc, Entity);
1437 TypeLocBuilder TLB;
1438 TLB.reserve(TL.getFullDataSize());
1439 QualType Result = Instantiator.TransformType(TLB, TL);
1440 if (Result.isNull())
1441 return 0;
1442
1443 return TLB.getTypeSourceInfo(Context, Result);
1444}
1445
John McCall609459e2009-10-21 00:58:09 +00001446/// Deprecated form of the above.
Mike Stump11289f42009-09-09 15:08:12 +00001447QualType Sema::SubstType(QualType T,
Douglas Gregor01afeef2009-08-28 20:31:08 +00001448 const MultiLevelTemplateArgumentList &TemplateArgs,
John McCall76d824f2009-08-25 22:02:44 +00001449 SourceLocation Loc, DeclarationName Entity) {
Douglas Gregor79cf6032009-03-10 20:44:00 +00001450 assert(!ActiveTemplateInstantiations.empty() &&
1451 "Cannot perform an instantiation without some context on the "
1452 "instantiation stack");
1453
Douglas Gregor5a5073e2010-05-24 17:22:01 +00001454 // If T is not a dependent type or a variably-modified type, there
1455 // is nothing to do.
Douglas Gregor678d76c2011-07-01 01:22:09 +00001456 if (!T->isInstantiationDependentType() && !T->isVariablyModifiedType())
Douglas Gregorfe1e1102009-02-27 19:31:52 +00001457 return T;
1458
Douglas Gregord6ff3322009-08-04 16:50:30 +00001459 TemplateInstantiator Instantiator(*this, TemplateArgs, Loc, Entity);
1460 return Instantiator.TransformType(T);
Douglas Gregorfe1e1102009-02-27 19:31:52 +00001461}
Douglas Gregor463421d2009-03-03 04:44:36 +00001462
John McCallb29f78f2010-04-09 17:38:44 +00001463static bool NeedsInstantiationAsFunctionType(TypeSourceInfo *T) {
Douglas Gregor678d76c2011-07-01 01:22:09 +00001464 if (T->getType()->isInstantiationDependentType() ||
1465 T->getType()->isVariablyModifiedType())
John McCallb29f78f2010-04-09 17:38:44 +00001466 return true;
1467
Abramo Bagnara6d810632010-12-14 22:11:44 +00001468 TypeLoc TL = T->getTypeLoc().IgnoreParens();
John McCallb29f78f2010-04-09 17:38:44 +00001469 if (!isa<FunctionProtoTypeLoc>(TL))
1470 return false;
1471
1472 FunctionProtoTypeLoc FP = cast<FunctionProtoTypeLoc>(TL);
1473 for (unsigned I = 0, E = FP.getNumArgs(); I != E; ++I) {
1474 ParmVarDecl *P = FP.getArg(I);
1475
Douglas Gregora7203e52011-05-09 20:45:16 +00001476 // The parameter's type as written might be dependent even if the
1477 // decayed type was not dependent.
1478 if (TypeSourceInfo *TSInfo = P->getTypeSourceInfo())
Douglas Gregor678d76c2011-07-01 01:22:09 +00001479 if (TSInfo->getType()->isInstantiationDependentType())
Douglas Gregora7203e52011-05-09 20:45:16 +00001480 return true;
1481
John McCallb29f78f2010-04-09 17:38:44 +00001482 // TODO: currently we always rebuild expressions. When we
1483 // properly get lazier about this, we should use the same
1484 // logic to avoid rebuilding prototypes here.
Douglas Gregor9cc278222011-01-05 21:14:17 +00001485 if (P->hasDefaultArg())
John McCallb29f78f2010-04-09 17:38:44 +00001486 return true;
1487 }
1488
1489 return false;
1490}
1491
1492/// A form of SubstType intended specifically for instantiating the
1493/// type of a FunctionDecl. Its purpose is solely to force the
1494/// instantiation of default-argument expressions.
1495TypeSourceInfo *Sema::SubstFunctionDeclType(TypeSourceInfo *T,
1496 const MultiLevelTemplateArgumentList &Args,
1497 SourceLocation Loc,
Douglas Gregor3024f072012-04-16 07:05:22 +00001498 DeclarationName Entity,
1499 CXXRecordDecl *ThisContext,
1500 unsigned ThisTypeQuals) {
John McCallb29f78f2010-04-09 17:38:44 +00001501 assert(!ActiveTemplateInstantiations.empty() &&
1502 "Cannot perform an instantiation without some context on the "
1503 "instantiation stack");
1504
1505 if (!NeedsInstantiationAsFunctionType(T))
1506 return T;
1507
1508 TemplateInstantiator Instantiator(*this, Args, Loc, Entity);
1509
1510 TypeLocBuilder TLB;
1511
1512 TypeLoc TL = T->getTypeLoc();
1513 TLB.reserve(TL.getFullDataSize());
1514
Douglas Gregor3024f072012-04-16 07:05:22 +00001515 QualType Result;
1516
1517 if (FunctionProtoTypeLoc *Proto = dyn_cast<FunctionProtoTypeLoc>(&TL)) {
1518 Result = Instantiator.TransformFunctionProtoType(TLB, *Proto, ThisContext,
1519 ThisTypeQuals);
1520 } else {
1521 Result = Instantiator.TransformType(TLB, TL);
1522 }
John McCallb29f78f2010-04-09 17:38:44 +00001523 if (Result.isNull())
1524 return 0;
1525
1526 return TLB.getTypeSourceInfo(Context, Result);
1527}
1528
Douglas Gregor940bca72010-04-12 07:48:19 +00001529ParmVarDecl *Sema::SubstParmVarDecl(ParmVarDecl *OldParm,
Douglas Gregor715e4612011-01-14 22:40:04 +00001530 const MultiLevelTemplateArgumentList &TemplateArgs,
John McCall8fb0d9d2011-05-01 22:35:37 +00001531 int indexAdjustment,
Douglas Gregor0dd22bc2012-01-25 16:15:54 +00001532 llvm::Optional<unsigned> NumExpansions,
1533 bool ExpectParameterPack) {
Douglas Gregor940bca72010-04-12 07:48:19 +00001534 TypeSourceInfo *OldDI = OldParm->getTypeSourceInfo();
Douglas Gregor5499af42011-01-05 23:12:31 +00001535 TypeSourceInfo *NewDI = 0;
1536
Douglas Gregor5499af42011-01-05 23:12:31 +00001537 TypeLoc OldTL = OldDI->getTypeLoc();
1538 if (isa<PackExpansionTypeLoc>(OldTL)) {
1539 PackExpansionTypeLoc ExpansionTL = cast<PackExpansionTypeLoc>(OldTL);
Douglas Gregor5499af42011-01-05 23:12:31 +00001540
1541 // We have a function parameter pack. Substitute into the pattern of the
1542 // expansion.
1543 NewDI = SubstType(ExpansionTL.getPatternLoc(), TemplateArgs,
1544 OldParm->getLocation(), OldParm->getDeclName());
1545 if (!NewDI)
1546 return 0;
1547
1548 if (NewDI->getType()->containsUnexpandedParameterPack()) {
1549 // We still have unexpanded parameter packs, which means that
1550 // our function parameter is still a function parameter pack.
1551 // Therefore, make its type a pack expansion type.
Douglas Gregor0dca5fd2011-01-14 17:04:44 +00001552 NewDI = CheckPackExpansion(NewDI, ExpansionTL.getEllipsisLoc(),
Douglas Gregor715e4612011-01-14 22:40:04 +00001553 NumExpansions);
Douglas Gregor0dd22bc2012-01-25 16:15:54 +00001554 } else if (ExpectParameterPack) {
1555 // We expected to get a parameter pack but didn't (because the type
1556 // itself is not a pack expansion type), so complain. This can occur when
1557 // the substitution goes through an alias template that "loses" the
1558 // pack expansion.
1559 Diag(OldParm->getLocation(),
1560 diag::err_function_parameter_pack_without_parameter_packs)
1561 << NewDI->getType();
1562 return 0;
1563 }
Douglas Gregor5499af42011-01-05 23:12:31 +00001564 } else {
1565 NewDI = SubstType(OldDI, TemplateArgs, OldParm->getLocation(),
1566 OldParm->getDeclName());
1567 }
1568
Douglas Gregor940bca72010-04-12 07:48:19 +00001569 if (!NewDI)
1570 return 0;
1571
1572 if (NewDI->getType()->isVoidType()) {
1573 Diag(OldParm->getLocation(), diag::err_param_with_void_type);
1574 return 0;
1575 }
1576
1577 ParmVarDecl *NewParm = CheckParameter(Context.getTranslationUnitDecl(),
Abramo Bagnaradff19302011-03-08 08:55:46 +00001578 OldParm->getInnerLocStart(),
Douglas Gregor940bca72010-04-12 07:48:19 +00001579 OldParm->getLocation(),
Abramo Bagnaradff19302011-03-08 08:55:46 +00001580 OldParm->getIdentifier(),
1581 NewDI->getType(), NewDI,
Douglas Gregorc4df4072010-04-19 22:54:31 +00001582 OldParm->getStorageClass(),
1583 OldParm->getStorageClassAsWritten());
Douglas Gregor940bca72010-04-12 07:48:19 +00001584 if (!NewParm)
1585 return 0;
Douglas Gregor6044d692010-05-19 17:02:24 +00001586
Douglas Gregor940bca72010-04-12 07:48:19 +00001587 // Mark the (new) default argument as uninstantiated (if any).
1588 if (OldParm->hasUninstantiatedDefaultArg()) {
1589 Expr *Arg = OldParm->getUninstantiatedDefaultArg();
1590 NewParm->setUninstantiatedDefaultArg(Arg);
Douglas Gregor758cb672010-10-12 18:23:32 +00001591 } else if (OldParm->hasUnparsedDefaultArg()) {
1592 NewParm->setUnparsedDefaultArg();
1593 UnparsedDefaultArgInstantiations[OldParm].push_back(NewParm);
David Blaikie7afed5e2012-05-01 06:05:57 +00001594 } else if (Expr *Arg = OldParm->getDefaultArg())
1595 // FIXME: if we non-lazily instantiated non-dependent default args for
1596 // non-dependent parameter types we could remove a bunch of duplicate
1597 // conversion warnings for such arguments.
1598 NewParm->setUninstantiatedDefaultArg(Arg);
Douglas Gregor940bca72010-04-12 07:48:19 +00001599
1600 NewParm->setHasInheritedDefaultArg(OldParm->hasInheritedDefaultArg());
Douglas Gregor0dd22bc2012-01-25 16:15:54 +00001601
Douglas Gregorf3010112011-01-07 16:43:16 +00001602 if (OldParm->isParameterPack() && !NewParm->isParameterPack()) {
Richard Smith928be492012-01-25 02:14:59 +00001603 // Add the new parameter to the instantiated parameter pack.
Douglas Gregorf3010112011-01-07 16:43:16 +00001604 CurrentInstantiationScope->InstantiatedLocalPackArg(OldParm, NewParm);
1605 } else {
1606 // Introduce an Old -> New mapping
Douglas Gregor5499af42011-01-05 23:12:31 +00001607 CurrentInstantiationScope->InstantiatedLocal(OldParm, NewParm);
Douglas Gregorf3010112011-01-07 16:43:16 +00001608 }
Douglas Gregor5499af42011-01-05 23:12:31 +00001609
Argyrios Kyrtzidis3816ed42010-07-19 10:14:41 +00001610 // FIXME: OldParm may come from a FunctionProtoType, in which case CurContext
1611 // can be anything, is this right ?
Fariborz Jahanian714447b2010-07-13 21:05:02 +00001612 NewParm->setDeclContext(CurContext);
John McCall8fb0d9d2011-05-01 22:35:37 +00001613
1614 NewParm->setScopeInfo(OldParm->getFunctionScopeDepth(),
1615 OldParm->getFunctionScopeIndex() + indexAdjustment);
Fariborz Jahaniana6c7efe2010-07-13 20:05:58 +00001616
Douglas Gregor940bca72010-04-12 07:48:19 +00001617 return NewParm;
1618}
1619
Douglas Gregordd472162011-01-07 00:20:55 +00001620/// \brief Substitute the given template arguments into the given set of
1621/// parameters, producing the set of parameter types that would be generated
1622/// from such a substitution.
1623bool Sema::SubstParmTypes(SourceLocation Loc,
1624 ParmVarDecl **Params, unsigned NumParams,
1625 const MultiLevelTemplateArgumentList &TemplateArgs,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001626 SmallVectorImpl<QualType> &ParamTypes,
1627 SmallVectorImpl<ParmVarDecl *> *OutParams) {
Douglas Gregordd472162011-01-07 00:20:55 +00001628 assert(!ActiveTemplateInstantiations.empty() &&
1629 "Cannot perform an instantiation without some context on the "
1630 "instantiation stack");
1631
1632 TemplateInstantiator Instantiator(*this, TemplateArgs, Loc,
1633 DeclarationName());
1634 return Instantiator.TransformFunctionTypeParams(Loc, Params, NumParams, 0,
Douglas Gregorf3010112011-01-07 16:43:16 +00001635 ParamTypes, OutParams);
Douglas Gregordd472162011-01-07 00:20:55 +00001636}
1637
John McCall76d824f2009-08-25 22:02:44 +00001638/// \brief Perform substitution on the base class specifiers of the
1639/// given class template specialization.
Douglas Gregor463421d2009-03-03 04:44:36 +00001640///
1641/// Produces a diagnostic and returns true on error, returns false and
1642/// attaches the instantiated base classes to the class template
1643/// specialization if successful.
Mike Stump11289f42009-09-09 15:08:12 +00001644bool
John McCall76d824f2009-08-25 22:02:44 +00001645Sema::SubstBaseSpecifiers(CXXRecordDecl *Instantiation,
1646 CXXRecordDecl *Pattern,
Douglas Gregor01afeef2009-08-28 20:31:08 +00001647 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor463421d2009-03-03 04:44:36 +00001648 bool Invalid = false;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001649 SmallVector<CXXBaseSpecifier*, 4> InstantiatedBases;
Mike Stump11289f42009-09-09 15:08:12 +00001650 for (ClassTemplateSpecializationDecl::base_class_iterator
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001651 Base = Pattern->bases_begin(), BaseEnd = Pattern->bases_end();
Douglas Gregor2a72edd2009-03-10 18:52:44 +00001652 Base != BaseEnd; ++Base) {
Douglas Gregor463421d2009-03-03 04:44:36 +00001653 if (!Base->getType()->isDependentType()) {
Fariborz Jahanian5c14ec32009-07-22 17:41:53 +00001654 InstantiatedBases.push_back(new (Context) CXXBaseSpecifier(*Base));
Douglas Gregor463421d2009-03-03 04:44:36 +00001655 continue;
1656 }
1657
Douglas Gregor752a5952011-01-03 22:36:02 +00001658 SourceLocation EllipsisLoc;
Douglas Gregorc52264e2011-03-02 02:04:06 +00001659 TypeSourceInfo *BaseTypeLoc;
Douglas Gregor752a5952011-01-03 22:36:02 +00001660 if (Base->isPackExpansion()) {
1661 // This is a pack expansion. See whether we should expand it now, or
1662 // wait until later.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001663 SmallVector<UnexpandedParameterPack, 2> Unexpanded;
Douglas Gregor752a5952011-01-03 22:36:02 +00001664 collectUnexpandedParameterPacks(Base->getTypeSourceInfo()->getTypeLoc(),
1665 Unexpanded);
1666 bool ShouldExpand = false;
Douglas Gregora8bac7f2011-01-10 07:32:04 +00001667 bool RetainExpansion = false;
Douglas Gregor0dca5fd2011-01-14 17:04:44 +00001668 llvm::Optional<unsigned> NumExpansions;
Douglas Gregor752a5952011-01-03 22:36:02 +00001669 if (CheckParameterPacksForExpansion(Base->getEllipsisLoc(),
1670 Base->getSourceRange(),
David Blaikieb9c168a2011-09-22 02:34:54 +00001671 Unexpanded,
Douglas Gregor752a5952011-01-03 22:36:02 +00001672 TemplateArgs, ShouldExpand,
Douglas Gregora8bac7f2011-01-10 07:32:04 +00001673 RetainExpansion,
Douglas Gregor752a5952011-01-03 22:36:02 +00001674 NumExpansions)) {
Douglas Gregor752a5952011-01-03 22:36:02 +00001675 Invalid = true;
Douglas Gregor44e7df62011-01-04 00:32:56 +00001676 continue;
Douglas Gregor752a5952011-01-03 22:36:02 +00001677 }
1678
1679 // If we should expand this pack expansion now, do so.
1680 if (ShouldExpand) {
Douglas Gregor0dca5fd2011-01-14 17:04:44 +00001681 for (unsigned I = 0; I != *NumExpansions; ++I) {
Douglas Gregor752a5952011-01-03 22:36:02 +00001682 Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(*this, I);
1683
1684 TypeSourceInfo *BaseTypeLoc = SubstType(Base->getTypeSourceInfo(),
1685 TemplateArgs,
1686 Base->getSourceRange().getBegin(),
1687 DeclarationName());
1688 if (!BaseTypeLoc) {
1689 Invalid = true;
1690 continue;
1691 }
1692
1693 if (CXXBaseSpecifier *InstantiatedBase
1694 = CheckBaseSpecifier(Instantiation,
1695 Base->getSourceRange(),
1696 Base->isVirtual(),
1697 Base->getAccessSpecifierAsWritten(),
1698 BaseTypeLoc,
1699 SourceLocation()))
1700 InstantiatedBases.push_back(InstantiatedBase);
1701 else
1702 Invalid = true;
1703 }
1704
1705 continue;
1706 }
1707
1708 // The resulting base specifier will (still) be a pack expansion.
1709 EllipsisLoc = Base->getEllipsisLoc();
Douglas Gregorc52264e2011-03-02 02:04:06 +00001710 Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(*this, -1);
1711 BaseTypeLoc = SubstType(Base->getTypeSourceInfo(),
1712 TemplateArgs,
1713 Base->getSourceRange().getBegin(),
1714 DeclarationName());
1715 } else {
1716 BaseTypeLoc = SubstType(Base->getTypeSourceInfo(),
1717 TemplateArgs,
1718 Base->getSourceRange().getBegin(),
1719 DeclarationName());
Douglas Gregor752a5952011-01-03 22:36:02 +00001720 }
1721
Nick Lewycky19b9f952010-07-26 16:56:01 +00001722 if (!BaseTypeLoc) {
Douglas Gregor463421d2009-03-03 04:44:36 +00001723 Invalid = true;
1724 continue;
1725 }
1726
1727 if (CXXBaseSpecifier *InstantiatedBase
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001728 = CheckBaseSpecifier(Instantiation,
Douglas Gregor463421d2009-03-03 04:44:36 +00001729 Base->getSourceRange(),
1730 Base->isVirtual(),
1731 Base->getAccessSpecifierAsWritten(),
Douglas Gregor752a5952011-01-03 22:36:02 +00001732 BaseTypeLoc,
1733 EllipsisLoc))
Douglas Gregor463421d2009-03-03 04:44:36 +00001734 InstantiatedBases.push_back(InstantiatedBase);
1735 else
1736 Invalid = true;
1737 }
1738
Douglas Gregor2a72edd2009-03-10 18:52:44 +00001739 if (!Invalid &&
Jay Foad7d0479f2009-05-21 09:52:38 +00001740 AttachBaseSpecifiers(Instantiation, InstantiatedBases.data(),
Douglas Gregor463421d2009-03-03 04:44:36 +00001741 InstantiatedBases.size()))
1742 Invalid = true;
1743
1744 return Invalid;
1745}
1746
DeLesley Hutchins30398dd2012-01-20 22:50:54 +00001747// Defined via #include from SemaTemplateInstantiateDecl.cpp
Benjamin Kramerbf8da9d2012-02-06 11:13:08 +00001748namespace clang {
1749 namespace sema {
1750 Attr *instantiateTemplateAttribute(const Attr *At, ASTContext &C, Sema &S,
1751 const MultiLevelTemplateArgumentList &TemplateArgs);
1752 }
1753}
DeLesley Hutchins30398dd2012-01-20 22:50:54 +00001754
Richard Smith4b38ded2012-03-14 23:13:10 +00001755/// Determine whether we would be unable to instantiate this template (because
1756/// it either has no definition, or is in the process of being instantiated).
1757static bool DiagnoseUninstantiableTemplate(Sema &S,
1758 SourceLocation PointOfInstantiation,
1759 TagDecl *Instantiation,
1760 bool InstantiatedFromMember,
1761 TagDecl *Pattern,
1762 TagDecl *PatternDef,
1763 TemplateSpecializationKind TSK,
1764 bool Complain = true) {
1765 if (PatternDef && !PatternDef->isBeingDefined())
1766 return false;
1767
1768 if (!Complain || (PatternDef && PatternDef->isInvalidDecl())) {
1769 // Say nothing
1770 } else if (PatternDef) {
1771 assert(PatternDef->isBeingDefined());
1772 S.Diag(PointOfInstantiation,
1773 diag::err_template_instantiate_within_definition)
1774 << (TSK != TSK_ImplicitInstantiation)
1775 << S.Context.getTypeDeclType(Instantiation);
1776 // Not much point in noting the template declaration here, since
1777 // we're lexically inside it.
1778 Instantiation->setInvalidDecl();
1779 } else if (InstantiatedFromMember) {
1780 S.Diag(PointOfInstantiation,
1781 diag::err_implicit_instantiate_member_undefined)
1782 << S.Context.getTypeDeclType(Instantiation);
1783 S.Diag(Pattern->getLocation(), diag::note_member_of_template_here);
1784 } else {
1785 S.Diag(PointOfInstantiation, diag::err_template_instantiate_undefined)
1786 << (TSK != TSK_ImplicitInstantiation)
1787 << S.Context.getTypeDeclType(Instantiation);
1788 S.Diag(Pattern->getLocation(), diag::note_template_decl_here);
1789 }
1790
1791 // In general, Instantiation isn't marked invalid to get more than one
1792 // error for multiple undefined instantiations. But the code that does
1793 // explicit declaration -> explicit definition conversion can't handle
1794 // invalid declarations, so mark as invalid in that case.
1795 if (TSK == TSK_ExplicitInstantiationDeclaration)
1796 Instantiation->setInvalidDecl();
1797 return true;
1798}
1799
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001800/// \brief Instantiate the definition of a class from a given pattern.
1801///
1802/// \param PointOfInstantiation The point of instantiation within the
1803/// source code.
1804///
1805/// \param Instantiation is the declaration whose definition is being
1806/// instantiated. This will be either a class template specialization
1807/// or a member class of a class template specialization.
1808///
1809/// \param Pattern is the pattern from which the instantiation
1810/// occurs. This will be either the declaration of a class template or
1811/// the declaration of a member class of a class template.
1812///
1813/// \param TemplateArgs The template arguments to be substituted into
1814/// the pattern.
1815///
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001816/// \param TSK the kind of implicit or explicit instantiation to perform.
Douglas Gregor8a2e6012009-08-24 15:23:48 +00001817///
1818/// \param Complain whether to complain if the class cannot be instantiated due
1819/// to the lack of a definition.
1820///
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001821/// \returns true if an error occurred, false otherwise.
1822bool
1823Sema::InstantiateClass(SourceLocation PointOfInstantiation,
1824 CXXRecordDecl *Instantiation, CXXRecordDecl *Pattern,
Douglas Gregor01afeef2009-08-28 20:31:08 +00001825 const MultiLevelTemplateArgumentList &TemplateArgs,
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001826 TemplateSpecializationKind TSK,
Douglas Gregor8a2e6012009-08-24 15:23:48 +00001827 bool Complain) {
Mike Stump11289f42009-09-09 15:08:12 +00001828 CXXRecordDecl *PatternDef
Douglas Gregor0a5a2212010-02-11 01:04:33 +00001829 = cast_or_null<CXXRecordDecl>(Pattern->getDefinition());
Richard Smith4b38ded2012-03-14 23:13:10 +00001830 if (DiagnoseUninstantiableTemplate(*this, PointOfInstantiation, Instantiation,
1831 Instantiation->getInstantiatedFromMemberClass(),
1832 Pattern, PatternDef, TSK, Complain))
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001833 return true;
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001834 Pattern = PatternDef;
1835
Douglas Gregord6ba93d2009-10-15 15:54:05 +00001836 // \brief Record the point of instantiation.
1837 if (MemberSpecializationInfo *MSInfo
1838 = Instantiation->getMemberSpecializationInfo()) {
1839 MSInfo->setTemplateSpecializationKind(TSK);
1840 MSInfo->setPointOfInstantiation(PointOfInstantiation);
Douglas Gregoref6ab412009-10-27 06:26:26 +00001841 } else if (ClassTemplateSpecializationDecl *Spec
Nico Weber3ffc4c92011-12-20 20:32:49 +00001842 = dyn_cast<ClassTemplateSpecializationDecl>(Instantiation)) {
Douglas Gregoref6ab412009-10-27 06:26:26 +00001843 Spec->setTemplateSpecializationKind(TSK);
1844 Spec->setPointOfInstantiation(PointOfInstantiation);
Douglas Gregord6ba93d2009-10-15 15:54:05 +00001845 }
1846
Douglas Gregorf3430ae2009-03-25 21:23:52 +00001847 InstantiatingTemplate Inst(*this, PointOfInstantiation, Instantiation);
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001848 if (Inst)
1849 return true;
1850
1851 // Enter the scope of this instantiation. We don't use
1852 // PushDeclContext because we don't have a scope.
John McCall80e58cd2010-04-29 00:35:03 +00001853 ContextRAII SavedContext(*this, Instantiation);
Douglas Gregor17158422010-05-12 17:27:19 +00001854 EnterExpressionEvaluationContext EvalContext(*this,
John McCallfaf5fb42010-08-26 23:41:50 +00001855 Sema::PotentiallyEvaluated);
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001856
Douglas Gregor51121572010-03-24 01:33:17 +00001857 // If this is an instantiation of a local class, merge this local
1858 // instantiation scope with the enclosing scope. Otherwise, every
1859 // instantiation of a class has its own local instantiation scope.
1860 bool MergeWithParentScope = !Instantiation->isDefinedOutsideFunctionOrMethod();
John McCall19c1bfd2010-08-25 05:32:35 +00001861 LocalInstantiationScope Scope(*this, MergeWithParentScope);
Douglas Gregor51121572010-03-24 01:33:17 +00001862
John McCall6602bb12010-08-01 02:01:53 +00001863 // Pull attributes from the pattern onto the instantiation.
1864 InstantiateAttrs(TemplateArgs, Pattern, Instantiation);
1865
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001866 // Start the definition of this instantiation.
1867 Instantiation->startDefinition();
Douglas Gregore9029562010-05-06 00:28:52 +00001868
1869 Instantiation->setTagKind(Pattern->getTagKind());
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001870
John McCall76d824f2009-08-25 22:02:44 +00001871 // Do substitution on the base class specifiers.
1872 if (SubstBaseSpecifiers(Instantiation, Pattern, TemplateArgs))
Douglas Gregorb9b8b812012-07-02 21:00:41 +00001873 Instantiation->setInvalidDecl();
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001874
Douglas Gregor869853e2010-11-10 19:44:59 +00001875 TemplateDeclInstantiator Instantiator(*this, Instantiation, TemplateArgs);
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001876 SmallVector<Decl*, 4> Fields;
1877 SmallVector<std::pair<FieldDecl*, FieldDecl*>, 4>
Richard Smith938f40b2011-06-11 17:19:42 +00001878 FieldsWithMemberInitializers;
DeLesley Hutchins30398dd2012-01-20 22:50:54 +00001879 // Delay instantiation of late parsed attributes.
1880 LateInstantiatedAttrVec LateAttrs;
1881 Instantiator.enableLateAttributeInstantiation(&LateAttrs);
1882
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001883 for (RecordDecl::decl_iterator Member = Pattern->decls_begin(),
Mike Stump11289f42009-09-09 15:08:12 +00001884 MemberEnd = Pattern->decls_end();
Douglas Gregorbcced4e2009-04-09 21:40:53 +00001885 Member != MemberEnd; ++Member) {
Argyrios Kyrtzidis9a94d9b2010-11-04 03:18:57 +00001886 // Don't instantiate members not belonging in this semantic context.
1887 // e.g. for:
1888 // @code
1889 // template <int i> class A {
1890 // class B *g;
1891 // };
1892 // @endcode
1893 // 'class B' has the template as lexical context but semantically it is
1894 // introduced in namespace scope.
1895 if ((*Member)->getDeclContext() != Pattern)
1896 continue;
1897
Douglas Gregor869853e2010-11-10 19:44:59 +00001898 if ((*Member)->isInvalidDecl()) {
Douglas Gregorb9b8b812012-07-02 21:00:41 +00001899 Instantiation->setInvalidDecl();
Douglas Gregor869853e2010-11-10 19:44:59 +00001900 continue;
1901 }
1902
1903 Decl *NewMember = Instantiator.Visit(*Member);
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001904 if (NewMember) {
Richard Smith938f40b2011-06-11 17:19:42 +00001905 if (FieldDecl *Field = dyn_cast<FieldDecl>(NewMember)) {
John McCall48871652010-08-21 09:40:31 +00001906 Fields.push_back(Field);
Richard Smith938f40b2011-06-11 17:19:42 +00001907 FieldDecl *OldField = cast<FieldDecl>(*Member);
1908 if (OldField->getInClassInitializer())
1909 FieldsWithMemberInitializers.push_back(std::make_pair(OldField,
1910 Field));
Richard Smith7d137e32012-03-23 03:33:32 +00001911 } else if (EnumDecl *Enum = dyn_cast<EnumDecl>(NewMember)) {
1912 // C++11 [temp.inst]p1: The implicit instantiation of a class template
1913 // specialization causes the implicit instantiation of the definitions
1914 // of unscoped member enumerations.
1915 // Record a point of instantiation for this implicit instantiation.
Richard Smithb66d7772012-03-23 23:09:08 +00001916 if (TSK == TSK_ImplicitInstantiation && !Enum->isScoped() &&
1917 Enum->isCompleteDefinition()) {
Richard Smith7d137e32012-03-23 03:33:32 +00001918 MemberSpecializationInfo *MSInfo =Enum->getMemberSpecializationInfo();
1919 assert(MSInfo && "no spec info for member enum specialization");
1920 MSInfo->setTemplateSpecializationKind(TSK_ImplicitInstantiation);
1921 MSInfo->setPointOfInstantiation(PointOfInstantiation);
1922 }
1923 }
1924
1925 if (NewMember->isInvalidDecl())
Douglas Gregorb9b8b812012-07-02 21:00:41 +00001926 Instantiation->setInvalidDecl();
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001927 } else {
1928 // FIXME: Eventually, a NULL return will mean that one of the
Douglas Gregorb9b8b812012-07-02 21:00:41 +00001929 // instantiations was a semantic disaster, and we'll want to mark the
1930 // declaration invalid.
1931 // For now, we expect to skip some members that we can't yet handle.
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001932 }
1933 }
1934
1935 // Finish checking fields.
David Blaikie751c5582011-09-22 02:58:26 +00001936 ActOnFields(0, Instantiation->getLocation(), Instantiation, Fields,
1937 SourceLocation(), SourceLocation(), 0);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001938 CheckCompletedCXXClass(Instantiation);
Richard Smith938f40b2011-06-11 17:19:42 +00001939
1940 // Attach any in-class member initializers now the class is complete.
Benjamin Kramer1d373c62012-05-17 12:01:52 +00001941 if (!FieldsWithMemberInitializers.empty()) {
Douglas Gregor3024f072012-04-16 07:05:22 +00001942 // C++11 [expr.prim.general]p4:
1943 // Otherwise, if a member-declarator declares a non-static data member
1944 // (9.2) of a class X, the expression this is a prvalue of type "pointer
1945 // to X" within the optional brace-or-equal-initializer. It shall not
1946 // appear elsewhere in the member-declarator.
1947 CXXThisScopeRAII ThisScope(*this, Instantiation, (unsigned)0);
1948
1949 for (unsigned I = 0, N = FieldsWithMemberInitializers.size(); I != N; ++I) {
1950 FieldDecl *OldField = FieldsWithMemberInitializers[I].first;
1951 FieldDecl *NewField = FieldsWithMemberInitializers[I].second;
1952 Expr *OldInit = OldField->getInClassInitializer();
Richard Smith938f40b2011-06-11 17:19:42 +00001953
Douglas Gregor3024f072012-04-16 07:05:22 +00001954 ExprResult NewInit = SubstInitializer(OldInit, TemplateArgs,
1955 /*CXXDirectInit=*/false);
1956 if (NewInit.isInvalid())
1957 NewField->setInvalidDecl();
1958 else {
1959 Expr *Init = NewInit.take();
1960 assert(Init && "no-argument initializer in class");
1961 assert(!isa<ParenListExpr>(Init) && "call-style init in class");
Richard Smith2b013182012-06-10 03:12:00 +00001962 ActOnCXXInClassMemberInitializer(NewField, Init->getLocStart(), Init);
Douglas Gregor3024f072012-04-16 07:05:22 +00001963 }
Richard Smithe3daab22011-07-20 00:12:52 +00001964 }
Richard Smith938f40b2011-06-11 17:19:42 +00001965 }
DeLesley Hutchins30398dd2012-01-20 22:50:54 +00001966 // Instantiate late parsed attributes, and attach them to their decls.
1967 // See Sema::InstantiateAttrs
1968 for (LateInstantiatedAttrVec::iterator I = LateAttrs.begin(),
1969 E = LateAttrs.end(); I != E; ++I) {
1970 assert(CurrentInstantiationScope == Instantiator.getStartingScope());
1971 CurrentInstantiationScope = I->Scope;
1972 Attr *NewAttr =
1973 instantiateTemplateAttribute(I->TmplAttr, Context, *this, TemplateArgs);
1974 I->NewDecl->addAttr(NewAttr);
1975 LocalInstantiationScope::deleteScopes(I->Scope,
1976 Instantiator.getStartingScope());
1977 }
1978 Instantiator.disableLateAttributeInstantiation();
1979 LateAttrs.clear();
1980
Richard Smith938f40b2011-06-11 17:19:42 +00001981 if (!FieldsWithMemberInitializers.empty())
1982 ActOnFinishDelayedMemberInitializers(Instantiation);
1983
Abramo Bagnara12dcbf32011-11-18 08:08:52 +00001984 if (TSK == TSK_ImplicitInstantiation) {
Argyrios Kyrtzidise3789482012-02-11 01:59:57 +00001985 Instantiation->setLocation(Pattern->getLocation());
Abramo Bagnara12dcbf32011-11-18 08:08:52 +00001986 Instantiation->setLocStart(Pattern->getInnerLocStart());
Abramo Bagnarafd3a4552011-10-03 20:34:03 +00001987 Instantiation->setRBraceLoc(Pattern->getRBraceLoc());
Abramo Bagnara12dcbf32011-11-18 08:08:52 +00001988 }
Abramo Bagnarafd3a4552011-10-03 20:34:03 +00001989
Douglas Gregorb9b8b812012-07-02 21:00:41 +00001990 if (!Instantiation->isInvalidDecl()) {
Douglas Gregor869853e2010-11-10 19:44:59 +00001991 // Instantiate any out-of-line class template partial
1992 // specializations now.
1993 for (TemplateDeclInstantiator::delayed_partial_spec_iterator
1994 P = Instantiator.delayed_partial_spec_begin(),
1995 PEnd = Instantiator.delayed_partial_spec_end();
1996 P != PEnd; ++P) {
1997 if (!Instantiator.InstantiateClassTemplatePartialSpecialization(
1998 P->first,
1999 P->second)) {
Douglas Gregorb9b8b812012-07-02 21:00:41 +00002000 Instantiation->setInvalidDecl();
Douglas Gregor869853e2010-11-10 19:44:59 +00002001 break;
2002 }
2003 }
2004 }
2005
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00002006 // Exit the scope of this instantiation.
John McCall80e58cd2010-04-29 00:35:03 +00002007 SavedContext.pop();
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00002008
Douglas Gregorb9b8b812012-07-02 21:00:41 +00002009 if (!Instantiation->isInvalidDecl()) {
Douglas Gregor28ad4b52009-05-26 20:50:29 +00002010 Consumer.HandleTagDeclDefinition(Instantiation);
2011
Douglas Gregor88d292c2010-05-13 16:44:06 +00002012 // Always emit the vtable for an explicit instantiation definition
2013 // of a polymorphic class template specialization.
2014 if (TSK == TSK_ExplicitInstantiationDefinition)
2015 MarkVTableUsed(PointOfInstantiation, Instantiation, true);
2016 }
2017
Douglas Gregorb9b8b812012-07-02 21:00:41 +00002018 return Instantiation->isInvalidDecl();
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00002019}
2020
Richard Smith4b38ded2012-03-14 23:13:10 +00002021/// \brief Instantiate the definition of an enum from a given pattern.
2022///
2023/// \param PointOfInstantiation The point of instantiation within the
2024/// source code.
2025/// \param Instantiation is the declaration whose definition is being
2026/// instantiated. This will be a member enumeration of a class
2027/// temploid specialization, or a local enumeration within a
2028/// function temploid specialization.
2029/// \param Pattern The templated declaration from which the instantiation
2030/// occurs.
2031/// \param TemplateArgs The template arguments to be substituted into
2032/// the pattern.
2033/// \param TSK The kind of implicit or explicit instantiation to perform.
2034///
2035/// \return \c true if an error occurred, \c false otherwise.
2036bool Sema::InstantiateEnum(SourceLocation PointOfInstantiation,
2037 EnumDecl *Instantiation, EnumDecl *Pattern,
2038 const MultiLevelTemplateArgumentList &TemplateArgs,
2039 TemplateSpecializationKind TSK) {
2040 EnumDecl *PatternDef = Pattern->getDefinition();
2041 if (DiagnoseUninstantiableTemplate(*this, PointOfInstantiation, Instantiation,
2042 Instantiation->getInstantiatedFromMemberEnum(),
2043 Pattern, PatternDef, TSK,/*Complain*/true))
2044 return true;
2045 Pattern = PatternDef;
2046
2047 // Record the point of instantiation.
2048 if (MemberSpecializationInfo *MSInfo
2049 = Instantiation->getMemberSpecializationInfo()) {
2050 MSInfo->setTemplateSpecializationKind(TSK);
2051 MSInfo->setPointOfInstantiation(PointOfInstantiation);
2052 }
2053
2054 InstantiatingTemplate Inst(*this, PointOfInstantiation, Instantiation);
2055 if (Inst)
2056 return true;
2057
2058 // Enter the scope of this instantiation. We don't use
2059 // PushDeclContext because we don't have a scope.
2060 ContextRAII SavedContext(*this, Instantiation);
2061 EnterExpressionEvaluationContext EvalContext(*this,
2062 Sema::PotentiallyEvaluated);
2063
2064 LocalInstantiationScope Scope(*this, /*MergeWithParentScope*/true);
2065
2066 // Pull attributes from the pattern onto the instantiation.
2067 InstantiateAttrs(TemplateArgs, Pattern, Instantiation);
2068
2069 TemplateDeclInstantiator Instantiator(*this, Instantiation, TemplateArgs);
2070 Instantiator.InstantiateEnumDefinition(Instantiation, Pattern);
2071
2072 // Exit the scope of this instantiation.
2073 SavedContext.pop();
2074
2075 return Instantiation->isInvalidDecl();
2076}
2077
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +00002078namespace {
2079 /// \brief A partial specialization whose template arguments have matched
2080 /// a given template-id.
2081 struct PartialSpecMatchResult {
2082 ClassTemplatePartialSpecializationDecl *Partial;
2083 TemplateArgumentList *Args;
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +00002084 };
2085}
2086
Mike Stump11289f42009-09-09 15:08:12 +00002087bool
Douglas Gregor463421d2009-03-03 04:44:36 +00002088Sema::InstantiateClassTemplateSpecialization(
Douglas Gregoref6ab412009-10-27 06:26:26 +00002089 SourceLocation PointOfInstantiation,
Douglas Gregor463421d2009-03-03 04:44:36 +00002090 ClassTemplateSpecializationDecl *ClassTemplateSpec,
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00002091 TemplateSpecializationKind TSK,
Douglas Gregor8a2e6012009-08-24 15:23:48 +00002092 bool Complain) {
Douglas Gregor463421d2009-03-03 04:44:36 +00002093 // Perform the actual instantiation on the canonical declaration.
2094 ClassTemplateSpec = cast<ClassTemplateSpecializationDecl>(
Argyrios Kyrtzidis6b7e3762009-07-18 00:34:25 +00002095 ClassTemplateSpec->getCanonicalDecl());
Douglas Gregor463421d2009-03-03 04:44:36 +00002096
Douglas Gregor4aa04b12009-09-11 21:19:12 +00002097 // Check whether we have already instantiated or specialized this class
2098 // template specialization.
2099 if (ClassTemplateSpec->getSpecializationKind() != TSK_Undeclared) {
2100 if (ClassTemplateSpec->getSpecializationKind() ==
2101 TSK_ExplicitInstantiationDeclaration &&
2102 TSK == TSK_ExplicitInstantiationDefinition) {
2103 // An explicit instantiation definition follows an explicit instantiation
2104 // declaration (C++0x [temp.explicit]p10); go ahead and perform the
2105 // explicit instantiation.
2106 ClassTemplateSpec->setSpecializationKind(TSK);
Douglas Gregor88d292c2010-05-13 16:44:06 +00002107
2108 // If this is an explicit instantiation definition, mark the
2109 // vtable as used.
Nico Weber3ffc4c92011-12-20 20:32:49 +00002110 if (TSK == TSK_ExplicitInstantiationDefinition &&
2111 !ClassTemplateSpec->isInvalidDecl())
Douglas Gregor88d292c2010-05-13 16:44:06 +00002112 MarkVTableUsed(PointOfInstantiation, ClassTemplateSpec, true);
2113
Douglas Gregor4aa04b12009-09-11 21:19:12 +00002114 return false;
2115 }
2116
2117 // We can only instantiate something that hasn't already been
2118 // instantiated or specialized. Fail without any diagnostics: our
2119 // caller will provide an error message.
Douglas Gregor463421d2009-03-03 04:44:36 +00002120 return true;
Douglas Gregor4aa04b12009-09-11 21:19:12 +00002121 }
Douglas Gregor463421d2009-03-03 04:44:36 +00002122
Douglas Gregor00a511f2009-09-15 16:51:42 +00002123 if (ClassTemplateSpec->isInvalidDecl())
2124 return true;
2125
Douglas Gregor463421d2009-03-03 04:44:36 +00002126 ClassTemplateDecl *Template = ClassTemplateSpec->getSpecializedTemplate();
Douglas Gregor01afeef2009-08-28 20:31:08 +00002127 CXXRecordDecl *Pattern = 0;
Douglas Gregor2373c592009-05-31 09:31:02 +00002128
Douglas Gregor170bc422009-06-12 22:31:52 +00002129 // C++ [temp.class.spec.match]p1:
2130 // When a class template is used in a context that requires an
2131 // instantiation of the class, it is necessary to determine
2132 // whether the instantiation is to be generated using the primary
2133 // template or one of the partial specializations. This is done by
2134 // matching the template arguments of the class template
2135 // specialization with the template argument lists of the partial
2136 // specializations.
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +00002137 typedef PartialSpecMatchResult MatchResult;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002138 SmallVector<MatchResult, 4> Matched;
2139 SmallVector<ClassTemplatePartialSpecializationDecl *, 4> PartialSpecs;
Douglas Gregor407e9612010-04-30 05:56:50 +00002140 Template->getPartialSpecializations(PartialSpecs);
2141 for (unsigned I = 0, N = PartialSpecs.size(); I != N; ++I) {
2142 ClassTemplatePartialSpecializationDecl *Partial = PartialSpecs[I];
John McCallbc077cf2010-02-08 23:07:23 +00002143 TemplateDeductionInfo Info(Context, PointOfInstantiation);
Douglas Gregor181aa4a2009-06-12 18:26:56 +00002144 if (TemplateDeductionResult Result
Douglas Gregor407e9612010-04-30 05:56:50 +00002145 = DeduceTemplateArguments(Partial,
Douglas Gregor181aa4a2009-06-12 18:26:56 +00002146 ClassTemplateSpec->getTemplateArgs(),
2147 Info)) {
2148 // FIXME: Store the failed-deduction information for use in
2149 // diagnostics, later.
2150 (void)Result;
2151 } else {
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +00002152 Matched.push_back(PartialSpecMatchResult());
2153 Matched.back().Partial = Partial;
2154 Matched.back().Args = Info.take();
Douglas Gregor181aa4a2009-06-12 18:26:56 +00002155 }
Douglas Gregor2373c592009-05-31 09:31:02 +00002156 }
2157
Douglas Gregor0231d8d2011-01-19 20:10:05 +00002158 // If we're dealing with a member template where the template parameters
2159 // have been instantiated, this provides the original template parameters
2160 // from which the member template's parameters were instantiated.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002161 SmallVector<const NamedDecl *, 4> InstantiatedTemplateParameters;
Douglas Gregor0231d8d2011-01-19 20:10:05 +00002162
Douglas Gregor21610382009-10-29 00:04:11 +00002163 if (Matched.size() >= 1) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002164 SmallVector<MatchResult, 4>::iterator Best = Matched.begin();
Douglas Gregor21610382009-10-29 00:04:11 +00002165 if (Matched.size() == 1) {
2166 // -- If exactly one matching specialization is found, the
2167 // instantiation is generated from that specialization.
2168 // We don't need to do anything for this.
2169 } else {
2170 // -- If more than one matching specialization is found, the
2171 // partial order rules (14.5.4.2) are used to determine
2172 // whether one of the specializations is more specialized
2173 // than the others. If none of the specializations is more
2174 // specialized than all of the other matching
2175 // specializations, then the use of the class template is
2176 // ambiguous and the program is ill-formed.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002177 for (SmallVector<MatchResult, 4>::iterator P = Best + 1,
Douglas Gregor21610382009-10-29 00:04:11 +00002178 PEnd = Matched.end();
2179 P != PEnd; ++P) {
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +00002180 if (getMoreSpecializedPartialSpecialization(P->Partial, Best->Partial,
John McCallbc077cf2010-02-08 23:07:23 +00002181 PointOfInstantiation)
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +00002182 == P->Partial)
Douglas Gregor21610382009-10-29 00:04:11 +00002183 Best = P;
Douglas Gregorbe999392009-09-15 16:23:51 +00002184 }
Douglas Gregorbe999392009-09-15 16:23:51 +00002185
Douglas Gregor21610382009-10-29 00:04:11 +00002186 // Determine if the best partial specialization is more specialized than
2187 // the others.
2188 bool Ambiguous = false;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002189 for (SmallVector<MatchResult, 4>::iterator P = Matched.begin(),
Douglas Gregorbe999392009-09-15 16:23:51 +00002190 PEnd = Matched.end();
Douglas Gregor21610382009-10-29 00:04:11 +00002191 P != PEnd; ++P) {
2192 if (P != Best &&
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +00002193 getMoreSpecializedPartialSpecialization(P->Partial, Best->Partial,
John McCallbc077cf2010-02-08 23:07:23 +00002194 PointOfInstantiation)
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +00002195 != Best->Partial) {
Douglas Gregor21610382009-10-29 00:04:11 +00002196 Ambiguous = true;
2197 break;
2198 }
2199 }
2200
2201 if (Ambiguous) {
2202 // Partial ordering did not produce a clear winner. Complain.
2203 ClassTemplateSpec->setInvalidDecl();
2204 Diag(PointOfInstantiation, diag::err_partial_spec_ordering_ambiguous)
2205 << ClassTemplateSpec;
2206
2207 // Print the matching partial specializations.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002208 for (SmallVector<MatchResult, 4>::iterator P = Matched.begin(),
Douglas Gregor21610382009-10-29 00:04:11 +00002209 PEnd = Matched.end();
2210 P != PEnd; ++P)
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +00002211 Diag(P->Partial->getLocation(), diag::note_partial_spec_match)
2212 << getTemplateArgumentBindingsText(
2213 P->Partial->getTemplateParameters(),
2214 *P->Args);
Douglas Gregor01afeef2009-08-28 20:31:08 +00002215
Douglas Gregor21610382009-10-29 00:04:11 +00002216 return true;
2217 }
Douglas Gregorbe999392009-09-15 16:23:51 +00002218 }
2219
2220 // Instantiate using the best class template partial specialization.
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +00002221 ClassTemplatePartialSpecializationDecl *OrigPartialSpec = Best->Partial;
Douglas Gregor21610382009-10-29 00:04:11 +00002222 while (OrigPartialSpec->getInstantiatedFromMember()) {
2223 // If we've found an explicit specialization of this class template,
2224 // stop here and use that as the pattern.
2225 if (OrigPartialSpec->isMemberSpecialization())
2226 break;
2227
2228 OrigPartialSpec = OrigPartialSpec->getInstantiatedFromMember();
2229 }
2230
2231 Pattern = OrigPartialSpec;
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +00002232 ClassTemplateSpec->setInstantiationOf(Best->Partial, Best->Args);
Douglas Gregor170bc422009-06-12 22:31:52 +00002233 } else {
2234 // -- If no matches are found, the instantiation is generated
2235 // from the primary template.
Douglas Gregor01afeef2009-08-28 20:31:08 +00002236 ClassTemplateDecl *OrigTemplate = Template;
Douglas Gregorcf915552009-10-13 16:30:37 +00002237 while (OrigTemplate->getInstantiatedFromMemberTemplate()) {
2238 // If we've found an explicit specialization of this class template,
2239 // stop here and use that as the pattern.
2240 if (OrigTemplate->isMemberSpecialization())
2241 break;
2242
Douglas Gregor01afeef2009-08-28 20:31:08 +00002243 OrigTemplate = OrigTemplate->getInstantiatedFromMemberTemplate();
Douglas Gregorcf915552009-10-13 16:30:37 +00002244 }
2245
Douglas Gregor01afeef2009-08-28 20:31:08 +00002246 Pattern = OrigTemplate->getTemplatedDecl();
Douglas Gregor2373c592009-05-31 09:31:02 +00002247 }
Douglas Gregor463421d2009-03-03 04:44:36 +00002248
Douglas Gregoref6ab412009-10-27 06:26:26 +00002249 bool Result = InstantiateClass(PointOfInstantiation, ClassTemplateSpec,
2250 Pattern,
2251 getTemplateInstantiationArgs(ClassTemplateSpec),
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00002252 TSK,
Douglas Gregor8a2e6012009-08-24 15:23:48 +00002253 Complain);
Mike Stump11289f42009-09-09 15:08:12 +00002254
Douglas Gregorb7ae10f2009-06-05 00:53:49 +00002255 return Result;
Douglas Gregor463421d2009-03-03 04:44:36 +00002256}
Douglas Gregor90a1a652009-03-19 17:26:29 +00002257
John McCall76d824f2009-08-25 22:02:44 +00002258/// \brief Instantiates the definitions of all of the member
2259/// of the given class, which is an instantiation of a class template
2260/// or a member class of a template.
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00002261void
2262Sema::InstantiateClassMembers(SourceLocation PointOfInstantiation,
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00002263 CXXRecordDecl *Instantiation,
2264 const MultiLevelTemplateArgumentList &TemplateArgs,
2265 TemplateSpecializationKind TSK) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002266 for (DeclContext::decl_iterator D = Instantiation->decls_begin(),
2267 DEnd = Instantiation->decls_end();
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00002268 D != DEnd; ++D) {
Douglas Gregor1d957a32009-10-27 18:42:08 +00002269 bool SuppressNew = false;
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00002270 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(*D)) {
Douglas Gregor1d957a32009-10-27 18:42:08 +00002271 if (FunctionDecl *Pattern
2272 = Function->getInstantiatedFromMemberFunction()) {
2273 MemberSpecializationInfo *MSInfo
2274 = Function->getMemberSpecializationInfo();
2275 assert(MSInfo && "No member specialization information?");
Douglas Gregor06aa50412010-04-09 21:02:29 +00002276 if (MSInfo->getTemplateSpecializationKind()
2277 == TSK_ExplicitSpecialization)
2278 continue;
2279
Douglas Gregor1d957a32009-10-27 18:42:08 +00002280 if (CheckSpecializationInstantiationRedecl(PointOfInstantiation, TSK,
2281 Function,
2282 MSInfo->getTemplateSpecializationKind(),
Nico Weberd75488d2010-09-27 21:02:09 +00002283 MSInfo->getPointOfInstantiation(),
Douglas Gregor1d957a32009-10-27 18:42:08 +00002284 SuppressNew) ||
2285 SuppressNew)
Douglas Gregorbbe8f462009-10-08 15:14:33 +00002286 continue;
2287
Alexis Hunt4a8ea102011-05-06 20:44:56 +00002288 if (Function->isDefined())
Douglas Gregor1d957a32009-10-27 18:42:08 +00002289 continue;
2290
2291 if (TSK == TSK_ExplicitInstantiationDefinition) {
2292 // C++0x [temp.explicit]p8:
2293 // An explicit instantiation definition that names a class template
2294 // specialization explicitly instantiates the class template
2295 // specialization and is only an explicit instantiation definition
2296 // of members whose definition is visible at the point of
2297 // instantiation.
Alexis Hunt4a8ea102011-05-06 20:44:56 +00002298 if (!Pattern->isDefined())
Douglas Gregor1d957a32009-10-27 18:42:08 +00002299 continue;
2300
2301 Function->setTemplateSpecializationKind(TSK, PointOfInstantiation);
2302
2303 InstantiateFunctionDefinition(PointOfInstantiation, Function);
2304 } else {
2305 Function->setTemplateSpecializationKind(TSK, PointOfInstantiation);
2306 }
Douglas Gregorbbe8f462009-10-08 15:14:33 +00002307 }
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00002308 } else if (VarDecl *Var = dyn_cast<VarDecl>(*D)) {
Douglas Gregor86d142a2009-10-08 07:24:58 +00002309 if (Var->isStaticDataMember()) {
Douglas Gregor1d957a32009-10-27 18:42:08 +00002310 MemberSpecializationInfo *MSInfo = Var->getMemberSpecializationInfo();
2311 assert(MSInfo && "No member specialization information?");
Douglas Gregor06aa50412010-04-09 21:02:29 +00002312 if (MSInfo->getTemplateSpecializationKind()
2313 == TSK_ExplicitSpecialization)
2314 continue;
2315
Douglas Gregor1d957a32009-10-27 18:42:08 +00002316 if (CheckSpecializationInstantiationRedecl(PointOfInstantiation, TSK,
2317 Var,
2318 MSInfo->getTemplateSpecializationKind(),
Nico Weberd75488d2010-09-27 21:02:09 +00002319 MSInfo->getPointOfInstantiation(),
Douglas Gregor1d957a32009-10-27 18:42:08 +00002320 SuppressNew) ||
2321 SuppressNew)
Douglas Gregorbbe8f462009-10-08 15:14:33 +00002322 continue;
2323
Douglas Gregor1d957a32009-10-27 18:42:08 +00002324 if (TSK == TSK_ExplicitInstantiationDefinition) {
2325 // C++0x [temp.explicit]p8:
2326 // An explicit instantiation definition that names a class template
2327 // specialization explicitly instantiates the class template
2328 // specialization and is only an explicit instantiation definition
2329 // of members whose definition is visible at the point of
2330 // instantiation.
2331 if (!Var->getInstantiatedFromStaticDataMember()
2332 ->getOutOfLineDefinition())
2333 continue;
2334
2335 Var->setTemplateSpecializationKind(TSK, PointOfInstantiation);
Douglas Gregor86d142a2009-10-08 07:24:58 +00002336 InstantiateStaticDataMemberDefinition(PointOfInstantiation, Var);
Douglas Gregor1d957a32009-10-27 18:42:08 +00002337 } else {
2338 Var->setTemplateSpecializationKind(TSK, PointOfInstantiation);
2339 }
2340 }
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00002341 } else if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(*D)) {
Douglas Gregor1da22252010-04-18 18:11:38 +00002342 // Always skip the injected-class-name, along with any
2343 // redeclarations of nested classes, since both would cause us
2344 // to try to instantiate the members of a class twice.
Douglas Gregorec9fd132012-01-14 16:38:05 +00002345 if (Record->isInjectedClassName() || Record->getPreviousDecl())
Douglas Gregord801b062009-10-07 23:56:10 +00002346 continue;
2347
Douglas Gregor1d957a32009-10-27 18:42:08 +00002348 MemberSpecializationInfo *MSInfo = Record->getMemberSpecializationInfo();
2349 assert(MSInfo && "No member specialization information?");
Douglas Gregor06aa50412010-04-09 21:02:29 +00002350
2351 if (MSInfo->getTemplateSpecializationKind()
2352 == TSK_ExplicitSpecialization)
2353 continue;
Nico Weberd75488d2010-09-27 21:02:09 +00002354
Douglas Gregor1d957a32009-10-27 18:42:08 +00002355 if (CheckSpecializationInstantiationRedecl(PointOfInstantiation, TSK,
2356 Record,
2357 MSInfo->getTemplateSpecializationKind(),
Nico Weberd75488d2010-09-27 21:02:09 +00002358 MSInfo->getPointOfInstantiation(),
Douglas Gregor1d957a32009-10-27 18:42:08 +00002359 SuppressNew) ||
2360 SuppressNew)
Douglas Gregorbbe8f462009-10-08 15:14:33 +00002361 continue;
2362
Douglas Gregor1d957a32009-10-27 18:42:08 +00002363 CXXRecordDecl *Pattern = Record->getInstantiatedFromMemberClass();
2364 assert(Pattern && "Missing instantiated-from-template information");
2365
Douglas Gregor0a5a2212010-02-11 01:04:33 +00002366 if (!Record->getDefinition()) {
2367 if (!Pattern->getDefinition()) {
Douglas Gregor1d957a32009-10-27 18:42:08 +00002368 // C++0x [temp.explicit]p8:
2369 // An explicit instantiation definition that names a class template
2370 // specialization explicitly instantiates the class template
2371 // specialization and is only an explicit instantiation definition
2372 // of members whose definition is visible at the point of
2373 // instantiation.
2374 if (TSK == TSK_ExplicitInstantiationDeclaration) {
2375 MSInfo->setTemplateSpecializationKind(TSK);
2376 MSInfo->setPointOfInstantiation(PointOfInstantiation);
2377 }
2378
2379 continue;
2380 }
2381
2382 InstantiateClass(PointOfInstantiation, Record, Pattern,
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00002383 TemplateArgs,
2384 TSK);
Nico Weberd75488d2010-09-27 21:02:09 +00002385 } else {
2386 if (TSK == TSK_ExplicitInstantiationDefinition &&
2387 Record->getTemplateSpecializationKind() ==
2388 TSK_ExplicitInstantiationDeclaration) {
2389 Record->setTemplateSpecializationKind(TSK);
2390 MarkVTableUsed(PointOfInstantiation, Record, true);
2391 }
Douglas Gregor1d957a32009-10-27 18:42:08 +00002392 }
Douglas Gregorc093c1d2009-10-08 01:19:17 +00002393
Douglas Gregor0a5a2212010-02-11 01:04:33 +00002394 Pattern = cast_or_null<CXXRecordDecl>(Record->getDefinition());
Douglas Gregor1d957a32009-10-27 18:42:08 +00002395 if (Pattern)
2396 InstantiateClassMembers(PointOfInstantiation, Pattern, TemplateArgs,
2397 TSK);
Richard Smith4b38ded2012-03-14 23:13:10 +00002398 } else if (EnumDecl *Enum = dyn_cast<EnumDecl>(*D)) {
2399 MemberSpecializationInfo *MSInfo = Enum->getMemberSpecializationInfo();
2400 assert(MSInfo && "No member specialization information?");
2401
2402 if (MSInfo->getTemplateSpecializationKind()
2403 == TSK_ExplicitSpecialization)
2404 continue;
2405
2406 if (CheckSpecializationInstantiationRedecl(
2407 PointOfInstantiation, TSK, Enum,
2408 MSInfo->getTemplateSpecializationKind(),
2409 MSInfo->getPointOfInstantiation(), SuppressNew) ||
2410 SuppressNew)
2411 continue;
2412
2413 if (Enum->getDefinition())
2414 continue;
2415
2416 EnumDecl *Pattern = Enum->getInstantiatedFromMemberEnum();
2417 assert(Pattern && "Missing instantiated-from-template information");
2418
2419 if (TSK == TSK_ExplicitInstantiationDefinition) {
2420 if (!Pattern->getDefinition())
2421 continue;
2422
2423 InstantiateEnum(PointOfInstantiation, Enum, Pattern, TemplateArgs, TSK);
2424 } else {
2425 MSInfo->setTemplateSpecializationKind(TSK);
2426 MSInfo->setPointOfInstantiation(PointOfInstantiation);
2427 }
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00002428 }
2429 }
2430}
2431
2432/// \brief Instantiate the definitions of all of the members of the
2433/// given class template specialization, which was named as part of an
2434/// explicit instantiation.
Mike Stump11289f42009-09-09 15:08:12 +00002435void
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00002436Sema::InstantiateClassTemplateSpecializationMembers(
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00002437 SourceLocation PointOfInstantiation,
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00002438 ClassTemplateSpecializationDecl *ClassTemplateSpec,
2439 TemplateSpecializationKind TSK) {
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00002440 // C++0x [temp.explicit]p7:
2441 // An explicit instantiation that names a class template
2442 // specialization is an explicit instantion of the same kind
2443 // (declaration or definition) of each of its members (not
2444 // including members inherited from base classes) that has not
2445 // been previously explicitly specialized in the translation unit
2446 // containing the explicit instantiation, except as described
2447 // below.
2448 InstantiateClassMembers(PointOfInstantiation, ClassTemplateSpec,
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00002449 getTemplateInstantiationArgs(ClassTemplateSpec),
2450 TSK);
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00002451}
2452
John McCalldadc5752010-08-24 06:29:42 +00002453StmtResult
Douglas Gregor01afeef2009-08-28 20:31:08 +00002454Sema::SubstStmt(Stmt *S, const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregorebe10102009-08-20 07:17:43 +00002455 if (!S)
2456 return Owned(S);
2457
2458 TemplateInstantiator Instantiator(*this, TemplateArgs,
2459 SourceLocation(),
2460 DeclarationName());
2461 return Instantiator.TransformStmt(S);
2462}
2463
John McCalldadc5752010-08-24 06:29:42 +00002464ExprResult
Douglas Gregor01afeef2009-08-28 20:31:08 +00002465Sema::SubstExpr(Expr *E, const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregora16548e2009-08-11 05:31:07 +00002466 if (!E)
2467 return Owned(E);
Mike Stump11289f42009-09-09 15:08:12 +00002468
Douglas Gregora16548e2009-08-11 05:31:07 +00002469 TemplateInstantiator Instantiator(*this, TemplateArgs,
2470 SourceLocation(),
2471 DeclarationName());
2472 return Instantiator.TransformExpr(E);
2473}
2474
Douglas Gregor2cd32a02011-01-07 19:35:17 +00002475bool Sema::SubstExprs(Expr **Exprs, unsigned NumExprs, bool IsCall,
2476 const MultiLevelTemplateArgumentList &TemplateArgs,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002477 SmallVectorImpl<Expr *> &Outputs) {
Douglas Gregor2cd32a02011-01-07 19:35:17 +00002478 if (NumExprs == 0)
2479 return false;
2480
2481 TemplateInstantiator Instantiator(*this, TemplateArgs,
2482 SourceLocation(),
2483 DeclarationName());
2484 return Instantiator.TransformExprs(Exprs, NumExprs, IsCall, Outputs);
2485}
2486
Douglas Gregor14454802011-02-25 02:25:35 +00002487NestedNameSpecifierLoc
2488Sema::SubstNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS,
2489 const MultiLevelTemplateArgumentList &TemplateArgs) {
2490 if (!NNS)
2491 return NestedNameSpecifierLoc();
2492
2493 TemplateInstantiator Instantiator(*this, TemplateArgs, NNS.getBeginLoc(),
2494 DeclarationName());
2495 return Instantiator.TransformNestedNameSpecifierLoc(NNS);
2496}
2497
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002498/// \brief Do template substitution on declaration name info.
2499DeclarationNameInfo
2500Sema::SubstDeclarationNameInfo(const DeclarationNameInfo &NameInfo,
2501 const MultiLevelTemplateArgumentList &TemplateArgs) {
2502 TemplateInstantiator Instantiator(*this, TemplateArgs, NameInfo.getLoc(),
2503 NameInfo.getName());
2504 return Instantiator.TransformDeclarationNameInfo(NameInfo);
2505}
2506
Douglas Gregoraa594892009-03-31 18:38:02 +00002507TemplateName
Douglas Gregordf846d12011-03-02 18:46:51 +00002508Sema::SubstTemplateName(NestedNameSpecifierLoc QualifierLoc,
2509 TemplateName Name, SourceLocation Loc,
Douglas Gregor01afeef2009-08-28 20:31:08 +00002510 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor71dc5092009-08-06 06:41:21 +00002511 TemplateInstantiator Instantiator(*this, TemplateArgs, Loc,
2512 DeclarationName());
Douglas Gregordf846d12011-03-02 18:46:51 +00002513 CXXScopeSpec SS;
2514 SS.Adopt(QualifierLoc);
2515 return Instantiator.TransformTemplateName(SS, Name, Loc);
Douglas Gregoraa594892009-03-31 18:38:02 +00002516}
Douglas Gregorc43620d2009-06-11 00:06:24 +00002517
Douglas Gregor0f3feb42010-12-22 21:19:48 +00002518bool Sema::Subst(const TemplateArgumentLoc *Args, unsigned NumArgs,
2519 TemplateArgumentListInfo &Result,
John McCall0ad16662009-10-29 08:12:44 +00002520 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregore922c772009-08-04 22:27:00 +00002521 TemplateInstantiator Instantiator(*this, TemplateArgs, SourceLocation(),
2522 DeclarationName());
Douglas Gregor0f3feb42010-12-22 21:19:48 +00002523
2524 return Instantiator.TransformTemplateArguments(Args, NumArgs, Result);
Douglas Gregorc43620d2009-06-11 00:06:24 +00002525}
Douglas Gregor14cf7522010-04-30 18:55:50 +00002526
Douglas Gregorf3010112011-01-07 16:43:16 +00002527llvm::PointerUnion<Decl *, LocalInstantiationScope::DeclArgumentPack *> *
2528LocalInstantiationScope::findInstantiationOf(const Decl *D) {
Chris Lattnercab02a62011-02-17 20:34:02 +00002529 for (LocalInstantiationScope *Current = this; Current;
Douglas Gregor14cf7522010-04-30 18:55:50 +00002530 Current = Current->Outer) {
Chris Lattnercab02a62011-02-17 20:34:02 +00002531
Douglas Gregor14cf7522010-04-30 18:55:50 +00002532 // Check if we found something within this scope.
Douglas Gregore9fc8dc2010-12-21 21:22:51 +00002533 const Decl *CheckD = D;
2534 do {
Douglas Gregorf3010112011-01-07 16:43:16 +00002535 LocalDeclsMap::iterator Found = Current->LocalDecls.find(CheckD);
Douglas Gregore9fc8dc2010-12-21 21:22:51 +00002536 if (Found != Current->LocalDecls.end())
Douglas Gregorf3010112011-01-07 16:43:16 +00002537 return &Found->second;
Douglas Gregore9fc8dc2010-12-21 21:22:51 +00002538
2539 // If this is a tag declaration, it's possible that we need to look for
2540 // a previous declaration.
2541 if (const TagDecl *Tag = dyn_cast<TagDecl>(CheckD))
Douglas Gregorec9fd132012-01-14 16:38:05 +00002542 CheckD = Tag->getPreviousDecl();
Douglas Gregore9fc8dc2010-12-21 21:22:51 +00002543 else
2544 CheckD = 0;
2545 } while (CheckD);
2546
Douglas Gregor14cf7522010-04-30 18:55:50 +00002547 // If we aren't combined with our outer scope, we're done.
2548 if (!Current->CombineWithOuterScope)
2549 break;
2550 }
Chris Lattnercab02a62011-02-17 20:34:02 +00002551
2552 // If we didn't find the decl, then we either have a sema bug, or we have a
2553 // forward reference to a label declaration. Return null to indicate that
2554 // we have an uninstantiated label.
2555 assert(isa<LabelDecl>(D) && "declaration not instantiated in this scope");
Douglas Gregor14cf7522010-04-30 18:55:50 +00002556 return 0;
2557}
2558
John McCall19c1bfd2010-08-25 05:32:35 +00002559void LocalInstantiationScope::InstantiatedLocal(const Decl *D, Decl *Inst) {
Douglas Gregorf3010112011-01-07 16:43:16 +00002560 llvm::PointerUnion<Decl *, DeclArgumentPack *> &Stored = LocalDecls[D];
Douglas Gregora8bac7f2011-01-10 07:32:04 +00002561 if (Stored.isNull())
2562 Stored = Inst;
2563 else if (Stored.is<Decl *>()) {
2564 assert(Stored.get<Decl *>() == Inst && "Already instantiated this local");
2565 Stored = Inst;
2566 } else
2567 LocalDecls[D].get<DeclArgumentPack *>()->push_back(Inst);
Douglas Gregor14cf7522010-04-30 18:55:50 +00002568}
Douglas Gregorf3010112011-01-07 16:43:16 +00002569
2570void LocalInstantiationScope::InstantiatedLocalPackArg(const Decl *D,
2571 Decl *Inst) {
2572 DeclArgumentPack *Pack = LocalDecls[D].get<DeclArgumentPack *>();
2573 Pack->push_back(Inst);
2574}
2575
2576void LocalInstantiationScope::MakeInstantiatedLocalArgPack(const Decl *D) {
2577 llvm::PointerUnion<Decl *, DeclArgumentPack *> &Stored = LocalDecls[D];
2578 assert(Stored.isNull() && "Already instantiated this local");
2579 DeclArgumentPack *Pack = new DeclArgumentPack;
2580 Stored = Pack;
2581 ArgumentPacks.push_back(Pack);
2582}
2583
Douglas Gregora8bac7f2011-01-10 07:32:04 +00002584void LocalInstantiationScope::SetPartiallySubstitutedPack(NamedDecl *Pack,
2585 const TemplateArgument *ExplicitArgs,
2586 unsigned NumExplicitArgs) {
2587 assert((!PartiallySubstitutedPack || PartiallySubstitutedPack == Pack) &&
2588 "Already have a partially-substituted pack");
2589 assert((!PartiallySubstitutedPack
2590 || NumArgsInPartiallySubstitutedPack == NumExplicitArgs) &&
2591 "Wrong number of arguments in partially-substituted pack");
2592 PartiallySubstitutedPack = Pack;
2593 ArgsInPartiallySubstitutedPack = ExplicitArgs;
2594 NumArgsInPartiallySubstitutedPack = NumExplicitArgs;
2595}
2596
2597NamedDecl *LocalInstantiationScope::getPartiallySubstitutedPack(
2598 const TemplateArgument **ExplicitArgs,
2599 unsigned *NumExplicitArgs) const {
2600 if (ExplicitArgs)
2601 *ExplicitArgs = 0;
2602 if (NumExplicitArgs)
2603 *NumExplicitArgs = 0;
2604
2605 for (const LocalInstantiationScope *Current = this; Current;
2606 Current = Current->Outer) {
2607 if (Current->PartiallySubstitutedPack) {
2608 if (ExplicitArgs)
2609 *ExplicitArgs = Current->ArgsInPartiallySubstitutedPack;
2610 if (NumExplicitArgs)
2611 *NumExplicitArgs = Current->NumArgsInPartiallySubstitutedPack;
2612
2613 return Current->PartiallySubstitutedPack;
2614 }
2615
2616 if (!Current->CombineWithOuterScope)
2617 break;
2618 }
2619
2620 return 0;
2621}