blob: 4108e9fd6faeec578ddfe8def99dedae803d3ff2 [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"
Douglas Gregorc3a6ade2010-08-12 20:07:10 +000016#include "clang/Sema/Lookup.h"
John McCallde6836a2010-08-24 07:21:54 +000017#include "clang/Sema/Template.h"
John McCall19c1bfd2010-08-25 05:32:35 +000018#include "clang/Sema/TemplateDeduction.h"
Douglas Gregor28ad4b52009-05-26 20:50:29 +000019#include "clang/AST/ASTConsumer.h"
Douglas Gregorfe1e1102009-02-27 19:31:52 +000020#include "clang/AST/ASTContext.h"
21#include "clang/AST/Expr.h"
Douglas Gregorfe1e1102009-02-27 19:31:52 +000022#include "clang/AST/DeclTemplate.h"
Douglas Gregorfe1e1102009-02-27 19:31:52 +000023#include "clang/Basic/LangOptions.h"
24
25using namespace clang;
John McCall19c1bfd2010-08-25 05:32:35 +000026using namespace sema;
Douglas Gregorfe1e1102009-02-27 19:31:52 +000027
Douglas Gregor4ea568f2009-03-10 18:03:33 +000028//===----------------------------------------------------------------------===/
29// Template Instantiation Support
30//===----------------------------------------------------------------------===/
31
Douglas Gregor01afeef2009-08-28 20:31:08 +000032/// \brief Retrieve the template argument list(s) that should be used to
33/// instantiate the definition of the given declaration.
Douglas Gregor36d7c5f2009-11-09 19:17:50 +000034///
35/// \param D the declaration for which we are computing template instantiation
36/// arguments.
37///
38/// \param Innermost if non-NULL, the innermost template argument list.
Douglas Gregor8c702532010-02-05 07:33:43 +000039///
40/// \param RelativeToPrimary true if we should get the template
41/// arguments relative to the primary template, even when we're
42/// dealing with a specialization. This is only relevant for function
43/// template specializations.
Douglas Gregor1bd7a942010-05-03 23:29:10 +000044///
45/// \param Pattern If non-NULL, indicates the pattern from which we will be
46/// instantiating the definition of the given declaration, \p D. This is
47/// used to determine the proper set of template instantiation arguments for
48/// friend function template specializations.
Douglas Gregora654dd82009-08-28 17:37:35 +000049MultiLevelTemplateArgumentList
Douglas Gregor36d7c5f2009-11-09 19:17:50 +000050Sema::getTemplateInstantiationArgs(NamedDecl *D,
Douglas Gregor8c702532010-02-05 07:33:43 +000051 const TemplateArgumentList *Innermost,
Douglas Gregor1bd7a942010-05-03 23:29:10 +000052 bool RelativeToPrimary,
53 const FunctionDecl *Pattern) {
Douglas Gregora654dd82009-08-28 17:37:35 +000054 // Accumulate the set of template argument lists in this structure.
55 MultiLevelTemplateArgumentList Result;
Mike Stump11289f42009-09-09 15:08:12 +000056
Douglas Gregor36d7c5f2009-11-09 19:17:50 +000057 if (Innermost)
58 Result.addOuterTemplateArguments(Innermost);
59
Douglas Gregora654dd82009-08-28 17:37:35 +000060 DeclContext *Ctx = dyn_cast<DeclContext>(D);
61 if (!Ctx)
62 Ctx = D->getDeclContext();
Mike Stump11289f42009-09-09 15:08:12 +000063
John McCall970d5302009-08-29 03:16:09 +000064 while (!Ctx->isFileContext()) {
Douglas Gregora654dd82009-08-28 17:37:35 +000065 // Add template arguments from a class template instantiation.
Mike Stump11289f42009-09-09 15:08:12 +000066 if (ClassTemplateSpecializationDecl *Spec
Douglas Gregora654dd82009-08-28 17:37:35 +000067 = dyn_cast<ClassTemplateSpecializationDecl>(Ctx)) {
68 // We're done when we hit an explicit specialization.
Douglas Gregor9961ce92010-07-08 18:37:38 +000069 if (Spec->getSpecializationKind() == TSK_ExplicitSpecialization &&
70 !isa<ClassTemplatePartialSpecializationDecl>(Spec))
Douglas Gregora654dd82009-08-28 17:37:35 +000071 break;
Mike Stump11289f42009-09-09 15:08:12 +000072
Douglas Gregora654dd82009-08-28 17:37:35 +000073 Result.addOuterTemplateArguments(&Spec->getTemplateInstantiationArgs());
Douglas Gregorcf915552009-10-13 16:30:37 +000074
75 // If this class template specialization was instantiated from a
76 // specialized member that is a class template, we're done.
77 assert(Spec->getSpecializedTemplate() && "No class template?");
78 if (Spec->getSpecializedTemplate()->isMemberSpecialization())
79 break;
Mike Stump11289f42009-09-09 15:08:12 +000080 }
Douglas Gregora654dd82009-08-28 17:37:35 +000081 // Add template arguments from a function template specialization.
John McCall970d5302009-08-29 03:16:09 +000082 else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Ctx)) {
Douglas Gregor8c702532010-02-05 07:33:43 +000083 if (!RelativeToPrimary &&
84 Function->getTemplateSpecializationKind()
85 == TSK_ExplicitSpecialization)
Douglas Gregorcf915552009-10-13 16:30:37 +000086 break;
87
Douglas Gregora654dd82009-08-28 17:37:35 +000088 if (const TemplateArgumentList *TemplateArgs
Douglas Gregorcf915552009-10-13 16:30:37 +000089 = Function->getTemplateSpecializationArgs()) {
90 // Add the template arguments for this specialization.
Douglas Gregora654dd82009-08-28 17:37:35 +000091 Result.addOuterTemplateArguments(TemplateArgs);
John McCall970d5302009-08-29 03:16:09 +000092
Douglas Gregorcf915552009-10-13 16:30:37 +000093 // If this function was instantiated from a specialized member that is
94 // a function template, we're done.
95 assert(Function->getPrimaryTemplate() && "No function template?");
96 if (Function->getPrimaryTemplate()->isMemberSpecialization())
97 break;
Douglas Gregor43669f82011-03-05 17:54:25 +000098 } else if (FunctionTemplateDecl *FunTmpl
99 = Function->getDescribedFunctionTemplate()) {
100 // Add the "injected" template arguments.
101 std::pair<const TemplateArgument *, unsigned>
102 Injected = FunTmpl->getInjectedTemplateArgs();
103 Result.addOuterTemplateArguments(Injected.first, Injected.second);
Douglas Gregorcf915552009-10-13 16:30:37 +0000104 }
105
John McCall970d5302009-08-29 03:16:09 +0000106 // If this is a friend declaration and it declares an entity at
107 // namespace scope, take arguments from its lexical parent
Douglas Gregor1bd7a942010-05-03 23:29:10 +0000108 // instead of its semantic parent, unless of course the pattern we're
109 // instantiating actually comes from the file's context!
John McCall970d5302009-08-29 03:16:09 +0000110 if (Function->getFriendObjectKind() &&
Douglas Gregor1bd7a942010-05-03 23:29:10 +0000111 Function->getDeclContext()->isFileContext() &&
112 (!Pattern || !Pattern->getLexicalDeclContext()->isFileContext())) {
John McCall970d5302009-08-29 03:16:09 +0000113 Ctx = Function->getLexicalDeclContext();
Douglas Gregor8c702532010-02-05 07:33:43 +0000114 RelativeToPrimary = false;
John McCall970d5302009-08-29 03:16:09 +0000115 continue;
116 }
Douglas Gregor9961ce92010-07-08 18:37:38 +0000117 } else if (CXXRecordDecl *Rec = dyn_cast<CXXRecordDecl>(Ctx)) {
118 if (ClassTemplateDecl *ClassTemplate = Rec->getDescribedClassTemplate()) {
119 QualType T = ClassTemplate->getInjectedClassNameSpecialization();
120 const TemplateSpecializationType *TST
121 = cast<TemplateSpecializationType>(Context.getCanonicalType(T));
122 Result.addOuterTemplateArguments(TST->getArgs(), TST->getNumArgs());
123 if (ClassTemplate->isMemberSpecialization())
124 break;
125 }
Douglas Gregora654dd82009-08-28 17:37:35 +0000126 }
John McCall970d5302009-08-29 03:16:09 +0000127
128 Ctx = Ctx->getParent();
Douglas Gregor8c702532010-02-05 07:33:43 +0000129 RelativeToPrimary = false;
Douglas Gregorb4850462009-05-14 23:26:13 +0000130 }
Mike Stump11289f42009-09-09 15:08:12 +0000131
Douglas Gregora654dd82009-08-28 17:37:35 +0000132 return Result;
Douglas Gregorb4850462009-05-14 23:26:13 +0000133}
134
Douglas Gregor84d49a22009-11-11 21:54:23 +0000135bool Sema::ActiveTemplateInstantiation::isInstantiationRecord() const {
136 switch (Kind) {
137 case TemplateInstantiation:
138 case DefaultTemplateArgumentInstantiation:
139 case DefaultFunctionArgumentInstantiation:
140 return true;
141
142 case ExplicitTemplateArgumentSubstitution:
143 case DeducedTemplateArgumentSubstitution:
144 case PriorTemplateArgumentSubstitution:
145 case DefaultTemplateArgumentChecking:
146 return false;
147 }
148
149 return true;
150}
151
Douglas Gregorfcd5db32009-03-10 00:06:19 +0000152Sema::InstantiatingTemplate::
153InstantiatingTemplate(Sema &SemaRef, SourceLocation PointOfInstantiation,
Douglas Gregor85673582009-05-18 17:01:57 +0000154 Decl *Entity,
Douglas Gregorfcd5db32009-03-10 00:06:19 +0000155 SourceRange InstantiationRange)
Douglas Gregoredb76852011-01-27 22:31:44 +0000156 : SemaRef(SemaRef),
157 SavedInNonInstantiationSFINAEContext(
158 SemaRef.InNonInstantiationSFINAEContext)
159{
Douglas Gregor79cf6032009-03-10 20:44:00 +0000160 Invalid = CheckInstantiationDepth(PointOfInstantiation,
161 InstantiationRange);
162 if (!Invalid) {
Douglas Gregorfcd5db32009-03-10 00:06:19 +0000163 ActiveTemplateInstantiation Inst;
Douglas Gregor79cf6032009-03-10 20:44:00 +0000164 Inst.Kind = ActiveTemplateInstantiation::TemplateInstantiation;
Douglas Gregorfcd5db32009-03-10 00:06:19 +0000165 Inst.PointOfInstantiation = PointOfInstantiation;
Douglas Gregor79cf6032009-03-10 20:44:00 +0000166 Inst.Entity = reinterpret_cast<uintptr_t>(Entity);
Douglas Gregorc9220832009-03-12 18:36:18 +0000167 Inst.TemplateArgs = 0;
168 Inst.NumTemplateArgs = 0;
Douglas Gregor79cf6032009-03-10 20:44:00 +0000169 Inst.InstantiationRange = InstantiationRange;
Douglas Gregoredb76852011-01-27 22:31:44 +0000170 SemaRef.InNonInstantiationSFINAEContext = false;
Douglas Gregor79cf6032009-03-10 20:44:00 +0000171 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
Douglas Gregor79cf6032009-03-10 20:44:00 +0000172 }
173}
174
Mike Stump11289f42009-09-09 15:08:12 +0000175Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef,
Douglas Gregor79cf6032009-03-10 20:44:00 +0000176 SourceLocation PointOfInstantiation,
177 TemplateDecl *Template,
178 const TemplateArgument *TemplateArgs,
179 unsigned NumTemplateArgs,
180 SourceRange InstantiationRange)
Douglas Gregoredb76852011-01-27 22:31:44 +0000181 : SemaRef(SemaRef),
182 SavedInNonInstantiationSFINAEContext(
183 SemaRef.InNonInstantiationSFINAEContext)
184{
Douglas Gregor79cf6032009-03-10 20:44:00 +0000185 Invalid = CheckInstantiationDepth(PointOfInstantiation,
186 InstantiationRange);
187 if (!Invalid) {
188 ActiveTemplateInstantiation Inst;
Mike Stump11289f42009-09-09 15:08:12 +0000189 Inst.Kind
Douglas Gregor79cf6032009-03-10 20:44:00 +0000190 = ActiveTemplateInstantiation::DefaultTemplateArgumentInstantiation;
191 Inst.PointOfInstantiation = PointOfInstantiation;
192 Inst.Entity = reinterpret_cast<uintptr_t>(Template);
193 Inst.TemplateArgs = TemplateArgs;
194 Inst.NumTemplateArgs = NumTemplateArgs;
Douglas Gregorfcd5db32009-03-10 00:06:19 +0000195 Inst.InstantiationRange = InstantiationRange;
Douglas Gregoredb76852011-01-27 22:31:44 +0000196 SemaRef.InNonInstantiationSFINAEContext = false;
Douglas Gregorfcd5db32009-03-10 00:06:19 +0000197 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
Douglas Gregorfcd5db32009-03-10 00:06:19 +0000198 }
199}
200
Mike Stump11289f42009-09-09 15:08:12 +0000201Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef,
Douglas Gregor637d9982009-06-10 23:47:09 +0000202 SourceLocation PointOfInstantiation,
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000203 FunctionTemplateDecl *FunctionTemplate,
204 const TemplateArgument *TemplateArgs,
205 unsigned NumTemplateArgs,
206 ActiveTemplateInstantiation::InstantiationKind Kind,
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +0000207 sema::TemplateDeductionInfo &DeductionInfo,
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000208 SourceRange InstantiationRange)
Douglas Gregoredb76852011-01-27 22:31:44 +0000209 : SemaRef(SemaRef),
210 SavedInNonInstantiationSFINAEContext(
211 SemaRef.InNonInstantiationSFINAEContext)
212{
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000213 Invalid = CheckInstantiationDepth(PointOfInstantiation,
214 InstantiationRange);
215 if (!Invalid) {
216 ActiveTemplateInstantiation Inst;
217 Inst.Kind = Kind;
218 Inst.PointOfInstantiation = PointOfInstantiation;
219 Inst.Entity = reinterpret_cast<uintptr_t>(FunctionTemplate);
220 Inst.TemplateArgs = TemplateArgs;
221 Inst.NumTemplateArgs = NumTemplateArgs;
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +0000222 Inst.DeductionInfo = &DeductionInfo;
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000223 Inst.InstantiationRange = InstantiationRange;
Douglas Gregoredb76852011-01-27 22:31:44 +0000224 SemaRef.InNonInstantiationSFINAEContext = false;
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000225 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
Douglas Gregor84d49a22009-11-11 21:54:23 +0000226
227 if (!Inst.isInstantiationRecord())
228 ++SemaRef.NonInstantiationEntries;
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000229 }
230}
231
Mike Stump11289f42009-09-09 15:08:12 +0000232Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef,
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000233 SourceLocation PointOfInstantiation,
Douglas Gregor637d9982009-06-10 23:47:09 +0000234 ClassTemplatePartialSpecializationDecl *PartialSpec,
235 const TemplateArgument *TemplateArgs,
236 unsigned NumTemplateArgs,
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +0000237 sema::TemplateDeductionInfo &DeductionInfo,
Douglas Gregor637d9982009-06-10 23:47:09 +0000238 SourceRange InstantiationRange)
Douglas Gregoredb76852011-01-27 22:31:44 +0000239 : SemaRef(SemaRef),
240 SavedInNonInstantiationSFINAEContext(
241 SemaRef.InNonInstantiationSFINAEContext)
242{
Douglas Gregor84d49a22009-11-11 21:54:23 +0000243 Invalid = false;
244
245 ActiveTemplateInstantiation Inst;
246 Inst.Kind = ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution;
247 Inst.PointOfInstantiation = PointOfInstantiation;
248 Inst.Entity = reinterpret_cast<uintptr_t>(PartialSpec);
249 Inst.TemplateArgs = TemplateArgs;
250 Inst.NumTemplateArgs = NumTemplateArgs;
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +0000251 Inst.DeductionInfo = &DeductionInfo;
Douglas Gregor84d49a22009-11-11 21:54:23 +0000252 Inst.InstantiationRange = InstantiationRange;
Douglas Gregoredb76852011-01-27 22:31:44 +0000253 SemaRef.InNonInstantiationSFINAEContext = false;
Douglas Gregor84d49a22009-11-11 21:54:23 +0000254 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
255
256 assert(!Inst.isInstantiationRecord());
257 ++SemaRef.NonInstantiationEntries;
Douglas Gregor637d9982009-06-10 23:47:09 +0000258}
259
Mike Stump11289f42009-09-09 15:08:12 +0000260Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef,
Douglas Gregore62e6a02009-11-11 19:13:48 +0000261 SourceLocation PointOfInstantiation,
Anders Carlsson657bad42009-09-05 05:14:19 +0000262 ParmVarDecl *Param,
263 const TemplateArgument *TemplateArgs,
264 unsigned NumTemplateArgs,
265 SourceRange InstantiationRange)
Douglas Gregoredb76852011-01-27 22:31:44 +0000266 : SemaRef(SemaRef),
267 SavedInNonInstantiationSFINAEContext(
268 SemaRef.InNonInstantiationSFINAEContext)
269{
Douglas Gregore62e6a02009-11-11 19:13:48 +0000270 Invalid = CheckInstantiationDepth(PointOfInstantiation, InstantiationRange);
Anders Carlsson657bad42009-09-05 05:14:19 +0000271
272 if (!Invalid) {
273 ActiveTemplateInstantiation Inst;
274 Inst.Kind
275 = ActiveTemplateInstantiation::DefaultFunctionArgumentInstantiation;
Douglas Gregore62e6a02009-11-11 19:13:48 +0000276 Inst.PointOfInstantiation = PointOfInstantiation;
Anders Carlsson657bad42009-09-05 05:14:19 +0000277 Inst.Entity = reinterpret_cast<uintptr_t>(Param);
278 Inst.TemplateArgs = TemplateArgs;
279 Inst.NumTemplateArgs = NumTemplateArgs;
280 Inst.InstantiationRange = InstantiationRange;
Douglas Gregoredb76852011-01-27 22:31:44 +0000281 SemaRef.InNonInstantiationSFINAEContext = false;
Anders Carlsson657bad42009-09-05 05:14:19 +0000282 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
Douglas Gregore62e6a02009-11-11 19:13:48 +0000283 }
284}
285
286Sema::InstantiatingTemplate::
287InstantiatingTemplate(Sema &SemaRef, SourceLocation PointOfInstantiation,
Douglas Gregorca4686d2011-01-04 23:35:54 +0000288 NamedDecl *Template,
Douglas Gregore62e6a02009-11-11 19:13:48 +0000289 NonTypeTemplateParmDecl *Param,
290 const TemplateArgument *TemplateArgs,
291 unsigned NumTemplateArgs,
Douglas Gregoredb76852011-01-27 22:31:44 +0000292 SourceRange InstantiationRange)
293 : SemaRef(SemaRef),
294 SavedInNonInstantiationSFINAEContext(
295 SemaRef.InNonInstantiationSFINAEContext)
296{
Douglas Gregor84d49a22009-11-11 21:54:23 +0000297 Invalid = false;
Douglas Gregore62e6a02009-11-11 19:13:48 +0000298
Douglas Gregor84d49a22009-11-11 21:54:23 +0000299 ActiveTemplateInstantiation Inst;
300 Inst.Kind = ActiveTemplateInstantiation::PriorTemplateArgumentSubstitution;
301 Inst.PointOfInstantiation = PointOfInstantiation;
302 Inst.Template = Template;
303 Inst.Entity = reinterpret_cast<uintptr_t>(Param);
304 Inst.TemplateArgs = TemplateArgs;
305 Inst.NumTemplateArgs = NumTemplateArgs;
306 Inst.InstantiationRange = InstantiationRange;
Douglas Gregoredb76852011-01-27 22:31:44 +0000307 SemaRef.InNonInstantiationSFINAEContext = false;
Douglas Gregor84d49a22009-11-11 21:54:23 +0000308 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
309
310 assert(!Inst.isInstantiationRecord());
311 ++SemaRef.NonInstantiationEntries;
Douglas Gregore62e6a02009-11-11 19:13:48 +0000312}
313
314Sema::InstantiatingTemplate::
315InstantiatingTemplate(Sema &SemaRef, SourceLocation PointOfInstantiation,
Douglas Gregorca4686d2011-01-04 23:35:54 +0000316 NamedDecl *Template,
Douglas Gregore62e6a02009-11-11 19:13:48 +0000317 TemplateTemplateParmDecl *Param,
318 const TemplateArgument *TemplateArgs,
319 unsigned NumTemplateArgs,
Douglas Gregoredb76852011-01-27 22:31:44 +0000320 SourceRange InstantiationRange)
321 : SemaRef(SemaRef),
322 SavedInNonInstantiationSFINAEContext(
323 SemaRef.InNonInstantiationSFINAEContext)
324{
Douglas Gregor84d49a22009-11-11 21:54:23 +0000325 Invalid = false;
326 ActiveTemplateInstantiation Inst;
327 Inst.Kind = ActiveTemplateInstantiation::PriorTemplateArgumentSubstitution;
328 Inst.PointOfInstantiation = PointOfInstantiation;
329 Inst.Template = Template;
330 Inst.Entity = reinterpret_cast<uintptr_t>(Param);
331 Inst.TemplateArgs = TemplateArgs;
332 Inst.NumTemplateArgs = NumTemplateArgs;
333 Inst.InstantiationRange = InstantiationRange;
Douglas Gregoredb76852011-01-27 22:31:44 +0000334 SemaRef.InNonInstantiationSFINAEContext = false;
Douglas Gregor84d49a22009-11-11 21:54:23 +0000335 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
Douglas Gregore62e6a02009-11-11 19:13:48 +0000336
Douglas Gregor84d49a22009-11-11 21:54:23 +0000337 assert(!Inst.isInstantiationRecord());
338 ++SemaRef.NonInstantiationEntries;
339}
340
341Sema::InstantiatingTemplate::
342InstantiatingTemplate(Sema &SemaRef, SourceLocation PointOfInstantiation,
343 TemplateDecl *Template,
344 NamedDecl *Param,
345 const TemplateArgument *TemplateArgs,
346 unsigned NumTemplateArgs,
Douglas Gregoredb76852011-01-27 22:31:44 +0000347 SourceRange InstantiationRange)
348 : SemaRef(SemaRef),
349 SavedInNonInstantiationSFINAEContext(
350 SemaRef.InNonInstantiationSFINAEContext)
351{
Douglas Gregor84d49a22009-11-11 21:54:23 +0000352 Invalid = false;
353
354 ActiveTemplateInstantiation Inst;
355 Inst.Kind = ActiveTemplateInstantiation::DefaultTemplateArgumentChecking;
356 Inst.PointOfInstantiation = PointOfInstantiation;
357 Inst.Template = Template;
358 Inst.Entity = reinterpret_cast<uintptr_t>(Param);
359 Inst.TemplateArgs = TemplateArgs;
360 Inst.NumTemplateArgs = NumTemplateArgs;
361 Inst.InstantiationRange = InstantiationRange;
Douglas Gregoredb76852011-01-27 22:31:44 +0000362 SemaRef.InNonInstantiationSFINAEContext = false;
Douglas Gregor84d49a22009-11-11 21:54:23 +0000363 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
364
365 assert(!Inst.isInstantiationRecord());
366 ++SemaRef.NonInstantiationEntries;
Anders Carlsson657bad42009-09-05 05:14:19 +0000367}
368
Douglas Gregor85673582009-05-18 17:01:57 +0000369void Sema::InstantiatingTemplate::Clear() {
370 if (!Invalid) {
Douglas Gregor84d49a22009-11-11 21:54:23 +0000371 if (!SemaRef.ActiveTemplateInstantiations.back().isInstantiationRecord()) {
372 assert(SemaRef.NonInstantiationEntries > 0);
373 --SemaRef.NonInstantiationEntries;
374 }
Douglas Gregoredb76852011-01-27 22:31:44 +0000375 SemaRef.InNonInstantiationSFINAEContext
376 = SavedInNonInstantiationSFINAEContext;
Douglas Gregorfcd5db32009-03-10 00:06:19 +0000377 SemaRef.ActiveTemplateInstantiations.pop_back();
Douglas Gregor85673582009-05-18 17:01:57 +0000378 Invalid = true;
379 }
Douglas Gregorfcd5db32009-03-10 00:06:19 +0000380}
381
Douglas Gregor79cf6032009-03-10 20:44:00 +0000382bool Sema::InstantiatingTemplate::CheckInstantiationDepth(
383 SourceLocation PointOfInstantiation,
384 SourceRange InstantiationRange) {
Douglas Gregor84d49a22009-11-11 21:54:23 +0000385 assert(SemaRef.NonInstantiationEntries <=
386 SemaRef.ActiveTemplateInstantiations.size());
387 if ((SemaRef.ActiveTemplateInstantiations.size() -
388 SemaRef.NonInstantiationEntries)
389 <= SemaRef.getLangOptions().InstantiationDepth)
Douglas Gregor79cf6032009-03-10 20:44:00 +0000390 return false;
391
Mike Stump11289f42009-09-09 15:08:12 +0000392 SemaRef.Diag(PointOfInstantiation,
Douglas Gregor79cf6032009-03-10 20:44:00 +0000393 diag::err_template_recursion_depth_exceeded)
394 << SemaRef.getLangOptions().InstantiationDepth
395 << InstantiationRange;
396 SemaRef.Diag(PointOfInstantiation, diag::note_template_recursion_depth)
397 << SemaRef.getLangOptions().InstantiationDepth;
398 return true;
399}
400
Douglas Gregor4ea568f2009-03-10 18:03:33 +0000401/// \brief Prints the current instantiation stack through a series of
402/// notes.
403void Sema::PrintInstantiationStack() {
Douglas Gregorffed1cb2010-04-20 07:18:24 +0000404 // Determine which template instantiations to skip, if any.
405 unsigned SkipStart = ActiveTemplateInstantiations.size(), SkipEnd = SkipStart;
406 unsigned Limit = Diags.getTemplateBacktraceLimit();
407 if (Limit && Limit < ActiveTemplateInstantiations.size()) {
408 SkipStart = Limit / 2 + Limit % 2;
409 SkipEnd = ActiveTemplateInstantiations.size() - Limit / 2;
410 }
411
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000412 // FIXME: In all of these cases, we need to show the template arguments
Douglas Gregorffed1cb2010-04-20 07:18:24 +0000413 unsigned InstantiationIdx = 0;
Douglas Gregor4ea568f2009-03-10 18:03:33 +0000414 for (llvm::SmallVector<ActiveTemplateInstantiation, 16>::reverse_iterator
415 Active = ActiveTemplateInstantiations.rbegin(),
416 ActiveEnd = ActiveTemplateInstantiations.rend();
417 Active != ActiveEnd;
Douglas Gregorffed1cb2010-04-20 07:18:24 +0000418 ++Active, ++InstantiationIdx) {
419 // Skip this instantiation?
420 if (InstantiationIdx >= SkipStart && InstantiationIdx < SkipEnd) {
421 if (InstantiationIdx == SkipStart) {
422 // Note that we're skipping instantiations.
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000423 Diags.Report(Active->PointOfInstantiation,
Douglas Gregorffed1cb2010-04-20 07:18:24 +0000424 diag::note_instantiation_contexts_suppressed)
425 << unsigned(ActiveTemplateInstantiations.size() - Limit);
426 }
427 continue;
428 }
429
Douglas Gregor79cf6032009-03-10 20:44:00 +0000430 switch (Active->Kind) {
431 case ActiveTemplateInstantiation::TemplateInstantiation: {
Douglas Gregor85673582009-05-18 17:01:57 +0000432 Decl *D = reinterpret_cast<Decl *>(Active->Entity);
433 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) {
434 unsigned DiagID = diag::note_template_member_class_here;
435 if (isa<ClassTemplateSpecializationDecl>(Record))
436 DiagID = diag::note_template_class_instantiation_here;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000437 Diags.Report(Active->PointOfInstantiation, DiagID)
Douglas Gregor85673582009-05-18 17:01:57 +0000438 << Context.getTypeDeclType(Record)
439 << Active->InstantiationRange;
Douglas Gregora6ef8f02009-07-24 20:34:43 +0000440 } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
Douglas Gregor4adbc6d2009-06-26 00:10:03 +0000441 unsigned DiagID;
442 if (Function->getPrimaryTemplate())
443 DiagID = diag::note_function_template_spec_here;
444 else
445 DiagID = diag::note_template_member_function_here;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000446 Diags.Report(Active->PointOfInstantiation, DiagID)
Douglas Gregor85673582009-05-18 17:01:57 +0000447 << Function
448 << Active->InstantiationRange;
Douglas Gregora6ef8f02009-07-24 20:34:43 +0000449 } else {
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000450 Diags.Report(Active->PointOfInstantiation,
Douglas Gregora6ef8f02009-07-24 20:34:43 +0000451 diag::note_template_static_data_member_def_here)
452 << cast<VarDecl>(D)
453 << Active->InstantiationRange;
Douglas Gregor85673582009-05-18 17:01:57 +0000454 }
Douglas Gregor79cf6032009-03-10 20:44:00 +0000455 break;
456 }
457
458 case ActiveTemplateInstantiation::DefaultTemplateArgumentInstantiation: {
459 TemplateDecl *Template = cast<TemplateDecl>((Decl *)Active->Entity);
460 std::string TemplateArgsStr
Douglas Gregordc572a32009-03-30 22:58:21 +0000461 = TemplateSpecializationType::PrintTemplateArgumentList(
Mike Stump11289f42009-09-09 15:08:12 +0000462 Active->TemplateArgs,
Douglas Gregor7de59662009-05-29 20:38:28 +0000463 Active->NumTemplateArgs,
464 Context.PrintingPolicy);
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000465 Diags.Report(Active->PointOfInstantiation,
Douglas Gregor79cf6032009-03-10 20:44:00 +0000466 diag::note_default_arg_instantiation_here)
467 << (Template->getNameAsString() + TemplateArgsStr)
468 << Active->InstantiationRange;
469 break;
470 }
Douglas Gregor637d9982009-06-10 23:47:09 +0000471
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000472 case ActiveTemplateInstantiation::ExplicitTemplateArgumentSubstitution: {
Mike Stump11289f42009-09-09 15:08:12 +0000473 FunctionTemplateDecl *FnTmpl
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000474 = cast<FunctionTemplateDecl>((Decl *)Active->Entity);
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000475 Diags.Report(Active->PointOfInstantiation,
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000476 diag::note_explicit_template_arg_substitution_here)
Douglas Gregor607f1412010-03-30 20:35:20 +0000477 << FnTmpl
478 << getTemplateArgumentBindingsText(FnTmpl->getTemplateParameters(),
479 Active->TemplateArgs,
480 Active->NumTemplateArgs)
481 << Active->InstantiationRange;
Douglas Gregor637d9982009-06-10 23:47:09 +0000482 break;
483 }
Mike Stump11289f42009-09-09 15:08:12 +0000484
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000485 case ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution:
486 if (ClassTemplatePartialSpecializationDecl *PartialSpec
487 = dyn_cast<ClassTemplatePartialSpecializationDecl>(
488 (Decl *)Active->Entity)) {
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000489 Diags.Report(Active->PointOfInstantiation,
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000490 diag::note_partial_spec_deduct_instantiation_here)
491 << Context.getTypeDeclType(PartialSpec)
Douglas Gregor607f1412010-03-30 20:35:20 +0000492 << getTemplateArgumentBindingsText(
493 PartialSpec->getTemplateParameters(),
494 Active->TemplateArgs,
495 Active->NumTemplateArgs)
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000496 << Active->InstantiationRange;
497 } else {
498 FunctionTemplateDecl *FnTmpl
499 = cast<FunctionTemplateDecl>((Decl *)Active->Entity);
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000500 Diags.Report(Active->PointOfInstantiation,
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000501 diag::note_function_template_deduction_instantiation_here)
Douglas Gregor607f1412010-03-30 20:35:20 +0000502 << FnTmpl
503 << getTemplateArgumentBindingsText(FnTmpl->getTemplateParameters(),
504 Active->TemplateArgs,
505 Active->NumTemplateArgs)
506 << Active->InstantiationRange;
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000507 }
508 break;
Douglas Gregor637d9982009-06-10 23:47:09 +0000509
Anders Carlsson657bad42009-09-05 05:14:19 +0000510 case ActiveTemplateInstantiation::DefaultFunctionArgumentInstantiation: {
511 ParmVarDecl *Param = cast<ParmVarDecl>((Decl *)Active->Entity);
512 FunctionDecl *FD = cast<FunctionDecl>(Param->getDeclContext());
Mike Stump11289f42009-09-09 15:08:12 +0000513
Anders Carlsson657bad42009-09-05 05:14:19 +0000514 std::string TemplateArgsStr
515 = TemplateSpecializationType::PrintTemplateArgumentList(
Mike Stump11289f42009-09-09 15:08:12 +0000516 Active->TemplateArgs,
Anders Carlsson657bad42009-09-05 05:14:19 +0000517 Active->NumTemplateArgs,
518 Context.PrintingPolicy);
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000519 Diags.Report(Active->PointOfInstantiation,
Anders Carlsson657bad42009-09-05 05:14:19 +0000520 diag::note_default_function_arg_instantiation_here)
Anders Carlssondc6d2c32009-09-05 05:38:54 +0000521 << (FD->getNameAsString() + TemplateArgsStr)
Anders Carlsson657bad42009-09-05 05:14:19 +0000522 << Active->InstantiationRange;
523 break;
524 }
Mike Stump11289f42009-09-09 15:08:12 +0000525
Douglas Gregore62e6a02009-11-11 19:13:48 +0000526 case ActiveTemplateInstantiation::PriorTemplateArgumentSubstitution: {
527 NamedDecl *Parm = cast<NamedDecl>((Decl *)Active->Entity);
528 std::string Name;
529 if (!Parm->getName().empty())
530 Name = std::string(" '") + Parm->getName().str() + "'";
Douglas Gregorca4686d2011-01-04 23:35:54 +0000531
532 TemplateParameterList *TemplateParams = 0;
533 if (TemplateDecl *Template = dyn_cast<TemplateDecl>(Active->Template))
534 TemplateParams = Template->getTemplateParameters();
535 else
536 TemplateParams =
537 cast<ClassTemplatePartialSpecializationDecl>(Active->Template)
538 ->getTemplateParameters();
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000539 Diags.Report(Active->PointOfInstantiation,
Douglas Gregore62e6a02009-11-11 19:13:48 +0000540 diag::note_prior_template_arg_substitution)
541 << isa<TemplateTemplateParmDecl>(Parm)
542 << Name
Douglas Gregorca4686d2011-01-04 23:35:54 +0000543 << getTemplateArgumentBindingsText(TemplateParams,
Douglas Gregore62e6a02009-11-11 19:13:48 +0000544 Active->TemplateArgs,
545 Active->NumTemplateArgs)
546 << Active->InstantiationRange;
547 break;
548 }
Douglas Gregor84d49a22009-11-11 21:54:23 +0000549
550 case ActiveTemplateInstantiation::DefaultTemplateArgumentChecking: {
Douglas Gregorca4686d2011-01-04 23:35:54 +0000551 TemplateParameterList *TemplateParams = 0;
552 if (TemplateDecl *Template = dyn_cast<TemplateDecl>(Active->Template))
553 TemplateParams = Template->getTemplateParameters();
554 else
555 TemplateParams =
556 cast<ClassTemplatePartialSpecializationDecl>(Active->Template)
557 ->getTemplateParameters();
558
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000559 Diags.Report(Active->PointOfInstantiation,
Douglas Gregor84d49a22009-11-11 21:54:23 +0000560 diag::note_template_default_arg_checking)
Douglas Gregorca4686d2011-01-04 23:35:54 +0000561 << getTemplateArgumentBindingsText(TemplateParams,
Douglas Gregor84d49a22009-11-11 21:54:23 +0000562 Active->TemplateArgs,
563 Active->NumTemplateArgs)
564 << Active->InstantiationRange;
565 break;
566 }
Douglas Gregor79cf6032009-03-10 20:44:00 +0000567 }
Douglas Gregor4ea568f2009-03-10 18:03:33 +0000568 }
569}
570
Douglas Gregoredb76852011-01-27 22:31:44 +0000571llvm::Optional<TemplateDeductionInfo *> Sema::isSFINAEContext() const {
Douglas Gregor33834512009-06-14 07:33:30 +0000572 using llvm::SmallVector;
Douglas Gregoredb76852011-01-27 22:31:44 +0000573 if (InNonInstantiationSFINAEContext)
574 return llvm::Optional<TemplateDeductionInfo *>(0);
575
Douglas Gregor33834512009-06-14 07:33:30 +0000576 for (SmallVector<ActiveTemplateInstantiation, 16>::const_reverse_iterator
577 Active = ActiveTemplateInstantiations.rbegin(),
578 ActiveEnd = ActiveTemplateInstantiations.rend();
579 Active != ActiveEnd;
Douglas Gregor84d49a22009-11-11 21:54:23 +0000580 ++Active)
581 {
Douglas Gregor33834512009-06-14 07:33:30 +0000582 switch(Active->Kind) {
Anders Carlsson657bad42009-09-05 05:14:19 +0000583 case ActiveTemplateInstantiation::DefaultFunctionArgumentInstantiation:
Douglas Gregoredb76852011-01-27 22:31:44 +0000584 case ActiveTemplateInstantiation::TemplateInstantiation:
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000585 // This is a template instantiation, so there is no SFINAE.
Douglas Gregoredb76852011-01-27 22:31:44 +0000586 return llvm::Optional<TemplateDeductionInfo *>();
Mike Stump11289f42009-09-09 15:08:12 +0000587
Douglas Gregor33834512009-06-14 07:33:30 +0000588 case ActiveTemplateInstantiation::DefaultTemplateArgumentInstantiation:
Douglas Gregore62e6a02009-11-11 19:13:48 +0000589 case ActiveTemplateInstantiation::PriorTemplateArgumentSubstitution:
Douglas Gregor84d49a22009-11-11 21:54:23 +0000590 case ActiveTemplateInstantiation::DefaultTemplateArgumentChecking:
Douglas Gregore62e6a02009-11-11 19:13:48 +0000591 // A default template argument instantiation and substitution into
592 // template parameters with arguments for prior parameters may or may
593 // not be a SFINAE context; look further up the stack.
Douglas Gregor33834512009-06-14 07:33:30 +0000594 break;
Mike Stump11289f42009-09-09 15:08:12 +0000595
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000596 case ActiveTemplateInstantiation::ExplicitTemplateArgumentSubstitution:
597 case ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution:
598 // We're either substitution explicitly-specified template arguments
599 // or deduced template arguments, so SFINAE applies.
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +0000600 assert(Active->DeductionInfo && "Missing deduction info pointer");
601 return Active->DeductionInfo;
Douglas Gregor33834512009-06-14 07:33:30 +0000602 }
603 }
604
Douglas Gregoredb76852011-01-27 22:31:44 +0000605 return llvm::Optional<TemplateDeductionInfo *>();
Douglas Gregor33834512009-06-14 07:33:30 +0000606}
607
Douglas Gregora8bac7f2011-01-10 07:32:04 +0000608/// \brief Retrieve the depth and index of a parameter pack.
609static std::pair<unsigned, unsigned>
610getDepthAndIndex(NamedDecl *ND) {
611 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(ND))
612 return std::make_pair(TTP->getDepth(), TTP->getIndex());
613
614 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(ND))
615 return std::make_pair(NTTP->getDepth(), NTTP->getIndex());
616
617 TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(ND);
618 return std::make_pair(TTP->getDepth(), TTP->getIndex());
619}
620
Douglas Gregorfe1e1102009-02-27 19:31:52 +0000621//===----------------------------------------------------------------------===/
622// Template Instantiation for Types
623//===----------------------------------------------------------------------===/
Douglas Gregor17c0d7b2009-02-28 00:25:32 +0000624namespace {
Douglas Gregor14cf7522010-04-30 18:55:50 +0000625 class TemplateInstantiator : public TreeTransform<TemplateInstantiator> {
Douglas Gregor01afeef2009-08-28 20:31:08 +0000626 const MultiLevelTemplateArgumentList &TemplateArgs;
Douglas Gregor17c0d7b2009-02-28 00:25:32 +0000627 SourceLocation Loc;
628 DeclarationName Entity;
Douglas Gregorfe1e1102009-02-27 19:31:52 +0000629
Douglas Gregor17c0d7b2009-02-28 00:25:32 +0000630 public:
Douglas Gregorebe10102009-08-20 07:17:43 +0000631 typedef TreeTransform<TemplateInstantiator> inherited;
Mike Stump11289f42009-09-09 15:08:12 +0000632
633 TemplateInstantiator(Sema &SemaRef,
Douglas Gregor01afeef2009-08-28 20:31:08 +0000634 const MultiLevelTemplateArgumentList &TemplateArgs,
Douglas Gregord6ff3322009-08-04 16:50:30 +0000635 SourceLocation Loc,
Mike Stump11289f42009-09-09 15:08:12 +0000636 DeclarationName Entity)
637 : inherited(SemaRef), TemplateArgs(TemplateArgs), Loc(Loc),
Douglas Gregorebe10102009-08-20 07:17:43 +0000638 Entity(Entity) { }
Douglas Gregor17c0d7b2009-02-28 00:25:32 +0000639
Mike Stump11289f42009-09-09 15:08:12 +0000640 /// \brief Determine whether the given type \p T has already been
Douglas Gregord6ff3322009-08-04 16:50:30 +0000641 /// transformed.
642 ///
643 /// For the purposes of template instantiation, a type has already been
644 /// transformed if it is NULL or if it is not dependent.
Douglas Gregor5597ab42010-05-07 23:12:07 +0000645 bool AlreadyTransformed(QualType T);
Mike Stump11289f42009-09-09 15:08:12 +0000646
Douglas Gregord6ff3322009-08-04 16:50:30 +0000647 /// \brief Returns the location of the entity being instantiated, if known.
648 SourceLocation getBaseLocation() { return Loc; }
Mike Stump11289f42009-09-09 15:08:12 +0000649
Douglas Gregord6ff3322009-08-04 16:50:30 +0000650 /// \brief Returns the name of the entity being instantiated, if any.
651 DeclarationName getBaseEntity() { return Entity; }
Mike Stump11289f42009-09-09 15:08:12 +0000652
Douglas Gregoref6ab412009-10-27 06:26:26 +0000653 /// \brief Sets the "base" location and entity when that
654 /// information is known based on another transformation.
655 void setBase(SourceLocation Loc, DeclarationName Entity) {
656 this->Loc = Loc;
657 this->Entity = Entity;
658 }
Douglas Gregor840bd6c2010-12-20 22:05:00 +0000659
660 bool TryExpandParameterPacks(SourceLocation EllipsisLoc,
661 SourceRange PatternRange,
662 const UnexpandedParameterPack *Unexpanded,
663 unsigned NumUnexpanded,
664 bool &ShouldExpand,
Douglas Gregora8bac7f2011-01-10 07:32:04 +0000665 bool &RetainExpansion,
Douglas Gregor0dca5fd2011-01-14 17:04:44 +0000666 llvm::Optional<unsigned> &NumExpansions) {
Douglas Gregor76aca7b2010-12-21 00:52:54 +0000667 return getSema().CheckParameterPacksForExpansion(EllipsisLoc,
668 PatternRange, Unexpanded,
669 NumUnexpanded,
670 TemplateArgs,
671 ShouldExpand,
Douglas Gregora8bac7f2011-01-10 07:32:04 +0000672 RetainExpansion,
Douglas Gregor76aca7b2010-12-21 00:52:54 +0000673 NumExpansions);
674 }
Douglas Gregor840bd6c2010-12-20 22:05:00 +0000675
Douglas Gregorf3010112011-01-07 16:43:16 +0000676 void ExpandingFunctionParameterPack(ParmVarDecl *Pack) {
677 SemaRef.CurrentInstantiationScope->MakeInstantiatedLocalArgPack(Pack);
678 }
679
Douglas Gregora8bac7f2011-01-10 07:32:04 +0000680 TemplateArgument ForgetPartiallySubstitutedPack() {
681 TemplateArgument Result;
682 if (NamedDecl *PartialPack
683 = SemaRef.CurrentInstantiationScope->getPartiallySubstitutedPack()){
684 MultiLevelTemplateArgumentList &TemplateArgs
685 = const_cast<MultiLevelTemplateArgumentList &>(this->TemplateArgs);
686 unsigned Depth, Index;
687 llvm::tie(Depth, Index) = getDepthAndIndex(PartialPack);
688 if (TemplateArgs.hasTemplateArgument(Depth, Index)) {
689 Result = TemplateArgs(Depth, Index);
690 TemplateArgs.setArgument(Depth, Index, TemplateArgument());
691 }
692 }
693
694 return Result;
695 }
696
697 void RememberPartiallySubstitutedPack(TemplateArgument Arg) {
698 if (Arg.isNull())
699 return;
700
701 if (NamedDecl *PartialPack
702 = SemaRef.CurrentInstantiationScope->getPartiallySubstitutedPack()){
703 MultiLevelTemplateArgumentList &TemplateArgs
704 = const_cast<MultiLevelTemplateArgumentList &>(this->TemplateArgs);
705 unsigned Depth, Index;
706 llvm::tie(Depth, Index) = getDepthAndIndex(PartialPack);
707 TemplateArgs.setArgument(Depth, Index, Arg);
708 }
709 }
710
Douglas Gregord6ff3322009-08-04 16:50:30 +0000711 /// \brief Transform the given declaration by instantiating a reference to
712 /// this declaration.
Douglas Gregora04f2ca2010-03-01 15:56:25 +0000713 Decl *TransformDecl(SourceLocation Loc, Decl *D);
Douglas Gregora16548e2009-08-11 05:31:07 +0000714
Mike Stump11289f42009-09-09 15:08:12 +0000715 /// \brief Transform the definition of the given declaration by
Douglas Gregorebe10102009-08-20 07:17:43 +0000716 /// instantiating it.
Douglas Gregor25289362010-03-01 17:25:41 +0000717 Decl *TransformDefinition(SourceLocation Loc, Decl *D);
Mike Stump11289f42009-09-09 15:08:12 +0000718
Douglas Gregora5cb6da2009-10-20 05:58:46 +0000719 /// \bried Transform the first qualifier within a scope by instantiating the
720 /// declaration.
721 NamedDecl *TransformFirstQualifierInScope(NamedDecl *D, SourceLocation Loc);
722
Douglas Gregorebe10102009-08-20 07:17:43 +0000723 /// \brief Rebuild the exception declaration and register the declaration
724 /// as an instantiated local.
Douglas Gregor9f0e1aa2010-09-09 17:09:21 +0000725 VarDecl *RebuildExceptionDecl(VarDecl *ExceptionDecl,
John McCallbcd03502009-12-07 02:54:59 +0000726 TypeSourceInfo *Declarator,
Douglas Gregorebe10102009-08-20 07:17:43 +0000727 IdentifierInfo *Name,
Douglas Gregor9f0e1aa2010-09-09 17:09:21 +0000728 SourceLocation Loc);
Mike Stump11289f42009-09-09 15:08:12 +0000729
Douglas Gregorf4e837f2010-04-26 17:57:08 +0000730 /// \brief Rebuild the Objective-C exception declaration and register the
731 /// declaration as an instantiated local.
732 VarDecl *RebuildObjCExceptionDecl(VarDecl *ExceptionDecl,
733 TypeSourceInfo *TSInfo, QualType T);
734
John McCall7f41d982009-09-11 04:59:25 +0000735 /// \brief Check for tag mismatches when instantiating an
736 /// elaborated type.
John McCall954b5de2010-11-04 19:04:38 +0000737 QualType RebuildElaboratedType(SourceLocation KeywordLoc,
738 ElaboratedTypeKeyword Keyword,
Douglas Gregor844cb502011-03-01 18:12:44 +0000739 NestedNameSpecifierLoc QualifierLoc,
740 QualType T);
John McCall7f41d982009-09-11 04:59:25 +0000741
Douglas Gregor9db53502011-03-02 18:07:45 +0000742 TemplateName TransformTemplateName(CXXScopeSpec &SS,
743 TemplateName Name,
744 SourceLocation NameLoc,
745 QualType ObjectType = QualType(),
746 NamedDecl *FirstQualifierInScope = 0);
747
John McCalldadc5752010-08-24 06:29:42 +0000748 ExprResult TransformPredefinedExpr(PredefinedExpr *E);
749 ExprResult TransformDeclRefExpr(DeclRefExpr *E);
750 ExprResult TransformCXXDefaultArgExpr(CXXDefaultArgExpr *E);
751 ExprResult TransformTemplateParmRefExpr(DeclRefExpr *E,
Douglas Gregoreb5a39d2010-12-24 00:15:10 +0000752 NonTypeTemplateParmDecl *D);
Douglas Gregorcdbc5392011-01-15 01:15:58 +0000753 ExprResult TransformSubstNonTypeTemplateParmPackExpr(
754 SubstNonTypeTemplateParmPackExpr *E);
755
Douglas Gregor14cf7522010-04-30 18:55:50 +0000756 QualType TransformFunctionProtoType(TypeLocBuilder &TLB,
John McCall31f82722010-11-12 08:19:04 +0000757 FunctionProtoTypeLoc TL);
Douglas Gregor715e4612011-01-14 22:40:04 +0000758 ParmVarDecl *TransformFunctionTypeParam(ParmVarDecl *OldParm,
759 llvm::Optional<unsigned> NumExpansions);
John McCall58f10c32010-03-11 09:03:00 +0000760
Mike Stump11289f42009-09-09 15:08:12 +0000761 /// \brief Transforms a template type parameter type by performing
Douglas Gregord6ff3322009-08-04 16:50:30 +0000762 /// substitution of the corresponding template type argument.
John McCall550e0c22009-10-21 00:40:46 +0000763 QualType TransformTemplateTypeParmType(TypeLocBuilder &TLB,
John McCall31f82722010-11-12 08:19:04 +0000764 TemplateTypeParmTypeLoc TL);
Nick Lewyckyc96c37f2010-07-06 19:51:49 +0000765
Douglas Gregorada4b792011-01-14 02:55:32 +0000766 /// \brief Transforms an already-substituted template type parameter pack
767 /// into either itself (if we aren't substituting into its pack expansion)
768 /// or the appropriate substituted argument.
769 QualType TransformSubstTemplateTypeParmPackType(TypeLocBuilder &TLB,
770 SubstTemplateTypeParmPackTypeLoc TL);
771
John McCalldadc5752010-08-24 06:29:42 +0000772 ExprResult TransformCallExpr(CallExpr *CE) {
Nick Lewyckyc96c37f2010-07-06 19:51:49 +0000773 getSema().CallsUndergoingInstantiation.push_back(CE);
John McCalldadc5752010-08-24 06:29:42 +0000774 ExprResult Result =
Nick Lewyckyc96c37f2010-07-06 19:51:49 +0000775 TreeTransform<TemplateInstantiator>::TransformCallExpr(CE);
776 getSema().CallsUndergoingInstantiation.pop_back();
777 return move(Result);
778 }
Douglas Gregord6ff3322009-08-04 16:50:30 +0000779 };
Douglas Gregor04318252009-07-06 15:59:29 +0000780}
781
Douglas Gregor5597ab42010-05-07 23:12:07 +0000782bool TemplateInstantiator::AlreadyTransformed(QualType T) {
783 if (T.isNull())
784 return true;
785
Douglas Gregor5a5073e2010-05-24 17:22:01 +0000786 if (T->isDependentType() || T->isVariablyModifiedType())
Douglas Gregor5597ab42010-05-07 23:12:07 +0000787 return false;
788
789 getSema().MarkDeclarationsReferencedInType(Loc, T);
790 return true;
791}
792
Douglas Gregora04f2ca2010-03-01 15:56:25 +0000793Decl *TemplateInstantiator::TransformDecl(SourceLocation Loc, Decl *D) {
Douglas Gregor2b6ca462009-09-03 21:38:09 +0000794 if (!D)
795 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000796
Douglas Gregor2b6ca462009-09-03 21:38:09 +0000797 if (TemplateTemplateParmDecl *TTP = dyn_cast<TemplateTemplateParmDecl>(D)) {
Douglas Gregor01afeef2009-08-28 20:31:08 +0000798 if (TTP->getDepth() < TemplateArgs.getNumLevels()) {
Douglas Gregorb93971082010-02-05 19:54:12 +0000799 // If the corresponding template argument is NULL or non-existent, it's
800 // because we are performing instantiation from explicitly-specified
801 // template arguments in a function template, but there were some
802 // arguments left unspecified.
803 if (!TemplateArgs.hasTemplateArgument(TTP->getDepth(),
804 TTP->getPosition()))
805 return D;
806
Douglas Gregorf5500772011-01-05 15:48:55 +0000807 TemplateArgument Arg = TemplateArgs(TTP->getDepth(), TTP->getPosition());
808
809 if (TTP->isParameterPack()) {
810 assert(Arg.getKind() == TemplateArgument::Pack &&
811 "Missing argument pack");
812
Douglas Gregor5590be02011-01-15 06:45:20 +0000813 assert(getSema().ArgumentPackSubstitutionIndex >= 0);
Douglas Gregora8bac7f2011-01-10 07:32:04 +0000814 assert(getSema().ArgumentPackSubstitutionIndex < (int)Arg.pack_size());
Douglas Gregorf5500772011-01-05 15:48:55 +0000815 Arg = Arg.pack_begin()[getSema().ArgumentPackSubstitutionIndex];
816 }
817
818 TemplateName Template = Arg.getAsTemplate();
Douglas Gregor9167f8b2009-11-11 01:00:40 +0000819 assert(!Template.isNull() && Template.getAsTemplateDecl() &&
Douglas Gregor01afeef2009-08-28 20:31:08 +0000820 "Wrong kind of template template argument");
Douglas Gregor9167f8b2009-11-11 01:00:40 +0000821 return Template.getAsTemplateDecl();
Douglas Gregor01afeef2009-08-28 20:31:08 +0000822 }
Mike Stump11289f42009-09-09 15:08:12 +0000823
Douglas Gregor9167f8b2009-11-11 01:00:40 +0000824 // Fall through to find the instantiated declaration for this template
825 // template parameter.
Douglas Gregor71dc5092009-08-06 06:41:21 +0000826 }
Mike Stump11289f42009-09-09 15:08:12 +0000827
Douglas Gregora04f2ca2010-03-01 15:56:25 +0000828 return SemaRef.FindInstantiatedDecl(Loc, cast<NamedDecl>(D), TemplateArgs);
Douglas Gregord6ff3322009-08-04 16:50:30 +0000829}
830
Douglas Gregor25289362010-03-01 17:25:41 +0000831Decl *TemplateInstantiator::TransformDefinition(SourceLocation Loc, Decl *D) {
John McCall76d824f2009-08-25 22:02:44 +0000832 Decl *Inst = getSema().SubstDecl(D, getSema().CurContext, TemplateArgs);
Douglas Gregorebe10102009-08-20 07:17:43 +0000833 if (!Inst)
834 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000835
Douglas Gregorebe10102009-08-20 07:17:43 +0000836 getSema().CurrentInstantiationScope->InstantiatedLocal(D, Inst);
837 return Inst;
838}
839
Douglas Gregora5cb6da2009-10-20 05:58:46 +0000840NamedDecl *
841TemplateInstantiator::TransformFirstQualifierInScope(NamedDecl *D,
842 SourceLocation Loc) {
843 // If the first part of the nested-name-specifier was a template type
844 // parameter, instantiate that type parameter down to a tag type.
845 if (TemplateTypeParmDecl *TTPD = dyn_cast_or_null<TemplateTypeParmDecl>(D)) {
846 const TemplateTypeParmType *TTP
847 = cast<TemplateTypeParmType>(getSema().Context.getTypeDeclType(TTPD));
Douglas Gregor53c3f4e2010-12-20 22:48:17 +0000848
Douglas Gregora5cb6da2009-10-20 05:58:46 +0000849 if (TTP->getDepth() < TemplateArgs.getNumLevels()) {
Douglas Gregor53c3f4e2010-12-20 22:48:17 +0000850 // FIXME: This needs testing w/ member access expressions.
851 TemplateArgument Arg = TemplateArgs(TTP->getDepth(), TTP->getIndex());
852
853 if (TTP->isParameterPack()) {
854 assert(Arg.getKind() == TemplateArgument::Pack &&
855 "Missing argument pack");
856
Douglas Gregore1d60df2011-01-14 23:41:42 +0000857 if (getSema().ArgumentPackSubstitutionIndex == -1)
Douglas Gregor53c3f4e2010-12-20 22:48:17 +0000858 return 0;
Douglas Gregor53c3f4e2010-12-20 22:48:17 +0000859
Douglas Gregora8bac7f2011-01-10 07:32:04 +0000860 assert(getSema().ArgumentPackSubstitutionIndex < (int)Arg.pack_size());
Douglas Gregor53c3f4e2010-12-20 22:48:17 +0000861 Arg = Arg.pack_begin()[getSema().ArgumentPackSubstitutionIndex];
862 }
863
864 QualType T = Arg.getAsType();
Douglas Gregora5cb6da2009-10-20 05:58:46 +0000865 if (T.isNull())
Douglas Gregora04f2ca2010-03-01 15:56:25 +0000866 return cast_or_null<NamedDecl>(TransformDecl(Loc, D));
Douglas Gregora5cb6da2009-10-20 05:58:46 +0000867
868 if (const TagType *Tag = T->getAs<TagType>())
869 return Tag->getDecl();
870
871 // The resulting type is not a tag; complain.
872 getSema().Diag(Loc, diag::err_nested_name_spec_non_tag) << T;
873 return 0;
874 }
875 }
876
Douglas Gregora04f2ca2010-03-01 15:56:25 +0000877 return cast_or_null<NamedDecl>(TransformDecl(Loc, D));
Douglas Gregora5cb6da2009-10-20 05:58:46 +0000878}
879
Douglas Gregorebe10102009-08-20 07:17:43 +0000880VarDecl *
881TemplateInstantiator::RebuildExceptionDecl(VarDecl *ExceptionDecl,
John McCallbcd03502009-12-07 02:54:59 +0000882 TypeSourceInfo *Declarator,
Douglas Gregorebe10102009-08-20 07:17:43 +0000883 IdentifierInfo *Name,
Douglas Gregor9f0e1aa2010-09-09 17:09:21 +0000884 SourceLocation Loc) {
885 VarDecl *Var = inherited::RebuildExceptionDecl(ExceptionDecl, Declarator,
886 Name, Loc);
Douglas Gregorf4e837f2010-04-26 17:57:08 +0000887 if (Var)
888 getSema().CurrentInstantiationScope->InstantiatedLocal(ExceptionDecl, Var);
889 return Var;
890}
891
892VarDecl *TemplateInstantiator::RebuildObjCExceptionDecl(VarDecl *ExceptionDecl,
893 TypeSourceInfo *TSInfo,
894 QualType T) {
895 VarDecl *Var = inherited::RebuildObjCExceptionDecl(ExceptionDecl, TSInfo, T);
896 if (Var)
Douglas Gregorebe10102009-08-20 07:17:43 +0000897 getSema().CurrentInstantiationScope->InstantiatedLocal(ExceptionDecl, Var);
898 return Var;
899}
900
John McCall7f41d982009-09-11 04:59:25 +0000901QualType
John McCall954b5de2010-11-04 19:04:38 +0000902TemplateInstantiator::RebuildElaboratedType(SourceLocation KeywordLoc,
903 ElaboratedTypeKeyword Keyword,
Douglas Gregor844cb502011-03-01 18:12:44 +0000904 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara6150c882010-05-11 21:36:43 +0000905 QualType T) {
John McCall7f41d982009-09-11 04:59:25 +0000906 if (const TagType *TT = T->getAs<TagType>()) {
907 TagDecl* TD = TT->getDecl();
908
John McCall954b5de2010-11-04 19:04:38 +0000909 SourceLocation TagLocation = KeywordLoc;
John McCall7f41d982009-09-11 04:59:25 +0000910
911 // FIXME: type might be anonymous.
912 IdentifierInfo *Id = TD->getIdentifier();
913
914 // TODO: should we even warn on struct/class mismatches for this? Seems
915 // like it's likely to produce a lot of spurious errors.
Abramo Bagnara6150c882010-05-11 21:36:43 +0000916 if (Keyword != ETK_None && Keyword != ETK_Typename) {
917 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForKeyword(Keyword);
918 if (!SemaRef.isAcceptableTagRedeclaration(TD, Kind, TagLocation, *Id)) {
919 SemaRef.Diag(TagLocation, diag::err_use_with_wrong_tag)
920 << Id
921 << FixItHint::CreateReplacement(SourceRange(TagLocation),
922 TD->getKindName());
923 SemaRef.Diag(TD->getLocation(), diag::note_previous_use);
924 }
John McCall7f41d982009-09-11 04:59:25 +0000925 }
926 }
927
John McCall954b5de2010-11-04 19:04:38 +0000928 return TreeTransform<TemplateInstantiator>::RebuildElaboratedType(KeywordLoc,
929 Keyword,
Douglas Gregor844cb502011-03-01 18:12:44 +0000930 QualifierLoc,
931 T);
John McCall7f41d982009-09-11 04:59:25 +0000932}
933
Douglas Gregor9db53502011-03-02 18:07:45 +0000934TemplateName TemplateInstantiator::TransformTemplateName(CXXScopeSpec &SS,
935 TemplateName Name,
936 SourceLocation NameLoc,
937 QualType ObjectType,
938 NamedDecl *FirstQualifierInScope) {
939 if (TemplateTemplateParmDecl *TTP
940 = dyn_cast_or_null<TemplateTemplateParmDecl>(Name.getAsTemplateDecl())) {
941 if (TTP->getDepth() < TemplateArgs.getNumLevels()) {
942 // If the corresponding template argument is NULL or non-existent, it's
943 // because we are performing instantiation from explicitly-specified
944 // template arguments in a function template, but there were some
945 // arguments left unspecified.
946 if (!TemplateArgs.hasTemplateArgument(TTP->getDepth(),
947 TTP->getPosition()))
948 return Name;
949
950 TemplateArgument Arg = TemplateArgs(TTP->getDepth(), TTP->getPosition());
951
952 if (TTP->isParameterPack()) {
953 assert(Arg.getKind() == TemplateArgument::Pack &&
954 "Missing argument pack");
955
956 if (getSema().ArgumentPackSubstitutionIndex == -1) {
957 // We have the template argument pack to substitute, but we're not
958 // actually expanding the enclosing pack expansion yet. So, just
959 // keep the entire argument pack.
960 return getSema().Context.getSubstTemplateTemplateParmPack(TTP, Arg);
961 }
962
963 assert(getSema().ArgumentPackSubstitutionIndex < (int)Arg.pack_size());
964 Arg = Arg.pack_begin()[getSema().ArgumentPackSubstitutionIndex];
965 }
966
967 TemplateName Template = Arg.getAsTemplate();
968 assert(!Template.isNull() && Template.getAsTemplateDecl() &&
969 "Wrong kind of template template argument");
970 return Template;
971 }
972 }
973
974 if (SubstTemplateTemplateParmPackStorage *SubstPack
975 = Name.getAsSubstTemplateTemplateParmPack()) {
976 if (getSema().ArgumentPackSubstitutionIndex == -1)
977 return Name;
978
979 const TemplateArgument &ArgPack = SubstPack->getArgumentPack();
980 assert(getSema().ArgumentPackSubstitutionIndex < (int)ArgPack.pack_size() &&
981 "Pack substitution index out-of-range");
982 return ArgPack.pack_begin()[getSema().ArgumentPackSubstitutionIndex]
983 .getAsTemplate();
984 }
985
986 return inherited::TransformTemplateName(SS, Name, NameLoc, ObjectType,
987 FirstQualifierInScope);
988}
989
John McCalldadc5752010-08-24 06:29:42 +0000990ExprResult
John McCall47f29ea2009-12-08 09:21:05 +0000991TemplateInstantiator::TransformPredefinedExpr(PredefinedExpr *E) {
Anders Carlsson0b209a82009-09-11 01:22:35 +0000992 if (!E->isTypeDependent())
John McCallc3007a22010-10-26 07:05:15 +0000993 return SemaRef.Owned(E);
Anders Carlsson0b209a82009-09-11 01:22:35 +0000994
995 FunctionDecl *currentDecl = getSema().getCurFunctionDecl();
996 assert(currentDecl && "Must have current function declaration when "
997 "instantiating.");
998
999 PredefinedExpr::IdentType IT = E->getIdentType();
1000
Anders Carlsson5bd8d192010-02-11 18:20:28 +00001001 unsigned Length = PredefinedExpr::ComputeName(IT, currentDecl).length();
Anders Carlsson0b209a82009-09-11 01:22:35 +00001002
1003 llvm::APInt LengthI(32, Length + 1);
John McCall8ccfcb52009-09-24 19:53:00 +00001004 QualType ResTy = getSema().Context.CharTy.withConst();
Anders Carlsson0b209a82009-09-11 01:22:35 +00001005 ResTy = getSema().Context.getConstantArrayType(ResTy, LengthI,
1006 ArrayType::Normal, 0);
1007 PredefinedExpr *PE =
1008 new (getSema().Context) PredefinedExpr(E->getLocation(), ResTy, IT);
1009 return getSema().Owned(PE);
1010}
1011
John McCalldadc5752010-08-24 06:29:42 +00001012ExprResult
John McCall13481c52010-02-06 08:42:39 +00001013TemplateInstantiator::TransformTemplateParmRefExpr(DeclRefExpr *E,
Douglas Gregor6c379e22010-02-08 23:41:45 +00001014 NonTypeTemplateParmDecl *NTTP) {
John McCall13481c52010-02-06 08:42:39 +00001015 // If the corresponding template argument is NULL or non-existent, it's
1016 // because we are performing instantiation from explicitly-specified
1017 // template arguments in a function template, but there were some
1018 // arguments left unspecified.
1019 if (!TemplateArgs.hasTemplateArgument(NTTP->getDepth(),
1020 NTTP->getPosition()))
John McCallc3007a22010-10-26 07:05:15 +00001021 return SemaRef.Owned(E);
Mike Stump11289f42009-09-09 15:08:12 +00001022
Douglas Gregoreb5a39d2010-12-24 00:15:10 +00001023 TemplateArgument Arg = TemplateArgs(NTTP->getDepth(), NTTP->getPosition());
1024 if (NTTP->isParameterPack()) {
1025 assert(Arg.getKind() == TemplateArgument::Pack &&
1026 "Missing argument pack");
1027
1028 if (getSema().ArgumentPackSubstitutionIndex == -1) {
Douglas Gregorcdbc5392011-01-15 01:15:58 +00001029 // We have an argument pack, but we can't select a particular argument
1030 // out of it yet. Therefore, we'll build an expression to hold on to that
1031 // argument pack.
1032 QualType TargetType = SemaRef.SubstType(NTTP->getType(), TemplateArgs,
1033 E->getLocation(),
1034 NTTP->getDeclName());
1035 if (TargetType.isNull())
1036 return ExprError();
1037
1038 return new (SemaRef.Context) SubstNonTypeTemplateParmPackExpr(TargetType,
1039 NTTP,
1040 E->getLocation(),
1041 Arg);
Douglas Gregoreb5a39d2010-12-24 00:15:10 +00001042 }
1043
Douglas Gregora8bac7f2011-01-10 07:32:04 +00001044 assert(getSema().ArgumentPackSubstitutionIndex < (int)Arg.pack_size());
Douglas Gregoreb5a39d2010-12-24 00:15:10 +00001045 Arg = Arg.pack_begin()[getSema().ArgumentPackSubstitutionIndex];
1046 }
Mike Stump11289f42009-09-09 15:08:12 +00001047
John McCall13481c52010-02-06 08:42:39 +00001048 // The template argument itself might be an expression, in which
1049 // case we just return that expression.
1050 if (Arg.getKind() == TemplateArgument::Expression)
John McCallc3007a22010-10-26 07:05:15 +00001051 return SemaRef.Owned(Arg.getAsExpr());
Mike Stump11289f42009-09-09 15:08:12 +00001052
John McCall13481c52010-02-06 08:42:39 +00001053 if (Arg.getKind() == TemplateArgument::Declaration) {
1054 ValueDecl *VD = cast<ValueDecl>(Arg.getAsDecl());
Mike Stump11289f42009-09-09 15:08:12 +00001055
John McCall15dda372010-02-06 10:23:53 +00001056 // Find the instantiation of the template argument. This is
1057 // required for nested templates.
John McCall13481c52010-02-06 08:42:39 +00001058 VD = cast_or_null<ValueDecl>(
Douglas Gregora04f2ca2010-03-01 15:56:25 +00001059 getSema().FindInstantiatedDecl(E->getLocation(),
1060 VD, TemplateArgs));
John McCall13481c52010-02-06 08:42:39 +00001061 if (!VD)
John McCallfaf5fb42010-08-26 23:41:50 +00001062 return ExprError();
Mike Stump11289f42009-09-09 15:08:12 +00001063
John McCall15dda372010-02-06 10:23:53 +00001064 // Derive the type we want the substituted decl to have. This had
1065 // better be non-dependent, or these checks will have serious problems.
Douglas Gregor0231d8d2011-01-19 20:10:05 +00001066 QualType TargetType;
1067 if (NTTP->isExpandedParameterPack())
1068 TargetType = NTTP->getExpansionType(
1069 getSema().ArgumentPackSubstitutionIndex);
1070 else if (NTTP->isParameterPack() &&
1071 isa<PackExpansionType>(NTTP->getType())) {
1072 TargetType = SemaRef.SubstType(
1073 cast<PackExpansionType>(NTTP->getType())->getPattern(),
1074 TemplateArgs, E->getLocation(),
1075 NTTP->getDeclName());
1076 } else
1077 TargetType = SemaRef.SubstType(NTTP->getType(), TemplateArgs,
1078 E->getLocation(), NTTP->getDeclName());
John McCall15dda372010-02-06 10:23:53 +00001079 assert(!TargetType.isNull() && "type substitution failed for param type");
1080 assert(!TargetType->isDependentType() && "param type still dependent");
Douglas Gregord5cb1dd2010-03-28 02:42:43 +00001081 return SemaRef.BuildExpressionFromDeclTemplateArgument(Arg,
1082 TargetType,
1083 E->getLocation());
John McCall13481c52010-02-06 08:42:39 +00001084 }
1085
Douglas Gregord5cb1dd2010-03-28 02:42:43 +00001086 return SemaRef.BuildExpressionFromIntegralTemplateArgument(Arg,
1087 E->getSourceRange().getBegin());
John McCall13481c52010-02-06 08:42:39 +00001088}
1089
Douglas Gregorcdbc5392011-01-15 01:15:58 +00001090ExprResult
1091TemplateInstantiator::TransformSubstNonTypeTemplateParmPackExpr(
1092 SubstNonTypeTemplateParmPackExpr *E) {
1093 if (getSema().ArgumentPackSubstitutionIndex == -1) {
1094 // We aren't expanding the parameter pack, so just return ourselves.
1095 return getSema().Owned(E);
1096 }
1097
Douglas Gregorcdbc5392011-01-15 01:15:58 +00001098 const TemplateArgument &ArgPack = E->getArgumentPack();
1099 unsigned Index = (unsigned)getSema().ArgumentPackSubstitutionIndex;
1100 assert(Index < ArgPack.pack_size() && "Substitution index out-of-range");
1101
1102 const TemplateArgument &Arg = ArgPack.pack_begin()[Index];
1103 if (Arg.getKind() == TemplateArgument::Expression)
1104 return SemaRef.Owned(Arg.getAsExpr());
1105
1106 if (Arg.getKind() == TemplateArgument::Declaration) {
1107 ValueDecl *VD = cast<ValueDecl>(Arg.getAsDecl());
1108
1109 // Find the instantiation of the template argument. This is
1110 // required for nested templates.
1111 VD = cast_or_null<ValueDecl>(
1112 getSema().FindInstantiatedDecl(E->getParameterPackLocation(),
1113 VD, TemplateArgs));
1114 if (!VD)
1115 return ExprError();
1116
Douglas Gregor0231d8d2011-01-19 20:10:05 +00001117 QualType T;
1118 NonTypeTemplateParmDecl *NTTP = E->getParameterPack();
1119 if (NTTP->isExpandedParameterPack())
1120 T = NTTP->getExpansionType(getSema().ArgumentPackSubstitutionIndex);
1121 else if (const PackExpansionType *Expansion
1122 = dyn_cast<PackExpansionType>(NTTP->getType()))
1123 T = SemaRef.SubstType(Expansion->getPattern(), TemplateArgs,
1124 E->getParameterPackLocation(), NTTP->getDeclName());
1125 else
1126 T = E->getType();
1127 return SemaRef.BuildExpressionFromDeclTemplateArgument(Arg, T,
Douglas Gregorcdbc5392011-01-15 01:15:58 +00001128 E->getParameterPackLocation());
1129 }
1130
1131 return SemaRef.BuildExpressionFromIntegralTemplateArgument(Arg,
1132 E->getParameterPackLocation());
1133}
John McCall13481c52010-02-06 08:42:39 +00001134
John McCalldadc5752010-08-24 06:29:42 +00001135ExprResult
John McCall13481c52010-02-06 08:42:39 +00001136TemplateInstantiator::TransformDeclRefExpr(DeclRefExpr *E) {
1137 NamedDecl *D = E->getDecl();
1138 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(D)) {
1139 if (NTTP->getDepth() < TemplateArgs.getNumLevels())
1140 return TransformTemplateParmRefExpr(E, NTTP);
Douglas Gregor954de172009-10-31 17:21:17 +00001141
1142 // We have a non-type template parameter that isn't fully substituted;
1143 // FindInstantiatedDecl will find it in the local instantiation scope.
Douglas Gregora16548e2009-08-11 05:31:07 +00001144 }
Mike Stump11289f42009-09-09 15:08:12 +00001145
John McCall47f29ea2009-12-08 09:21:05 +00001146 return TreeTransform<TemplateInstantiator>::TransformDeclRefExpr(E);
Douglas Gregora16548e2009-08-11 05:31:07 +00001147}
1148
John McCalldadc5752010-08-24 06:29:42 +00001149ExprResult TemplateInstantiator::TransformCXXDefaultArgExpr(
John McCall47f29ea2009-12-08 09:21:05 +00001150 CXXDefaultArgExpr *E) {
Sebastian Redl14236c82009-11-08 13:56:19 +00001151 assert(!cast<FunctionDecl>(E->getParam()->getDeclContext())->
1152 getDescribedFunctionTemplate() &&
1153 "Default arg expressions are never formed in dependent cases.");
Douglas Gregor033f6752009-12-23 23:03:06 +00001154 return SemaRef.BuildCXXDefaultArgExpr(E->getUsedLocation(),
1155 cast<FunctionDecl>(E->getParam()->getDeclContext()),
1156 E->getParam());
Sebastian Redl14236c82009-11-08 13:56:19 +00001157}
1158
Douglas Gregor14cf7522010-04-30 18:55:50 +00001159QualType TemplateInstantiator::TransformFunctionProtoType(TypeLocBuilder &TLB,
John McCall31f82722010-11-12 08:19:04 +00001160 FunctionProtoTypeLoc TL) {
Douglas Gregor14cf7522010-04-30 18:55:50 +00001161 // We need a local instantiation scope for this function prototype.
John McCall19c1bfd2010-08-25 05:32:35 +00001162 LocalInstantiationScope Scope(SemaRef, /*CombineWithOuterScope=*/true);
John McCall31f82722010-11-12 08:19:04 +00001163 return inherited::TransformFunctionProtoType(TLB, TL);
John McCall58f10c32010-03-11 09:03:00 +00001164}
1165
1166ParmVarDecl *
Douglas Gregor715e4612011-01-14 22:40:04 +00001167TemplateInstantiator::TransformFunctionTypeParam(ParmVarDecl *OldParm,
1168 llvm::Optional<unsigned> NumExpansions) {
1169 return SemaRef.SubstParmVarDecl(OldParm, TemplateArgs,
1170 NumExpansions);
John McCall58f10c32010-03-11 09:03:00 +00001171}
1172
Mike Stump11289f42009-09-09 15:08:12 +00001173QualType
John McCall550e0c22009-10-21 00:40:46 +00001174TemplateInstantiator::TransformTemplateTypeParmType(TypeLocBuilder &TLB,
John McCall31f82722010-11-12 08:19:04 +00001175 TemplateTypeParmTypeLoc TL) {
John McCall424cec92011-01-19 06:33:43 +00001176 const TemplateTypeParmType *T = TL.getTypePtr();
Douglas Gregor01afeef2009-08-28 20:31:08 +00001177 if (T->getDepth() < TemplateArgs.getNumLevels()) {
Douglas Gregorfe1e1102009-02-27 19:31:52 +00001178 // Replace the template type parameter with its corresponding
1179 // template argument.
Mike Stump11289f42009-09-09 15:08:12 +00001180
1181 // If the corresponding template argument is NULL or doesn't exist, it's
1182 // because we are performing instantiation from explicitly-specified
1183 // template arguments in a function template class, but there were some
Douglas Gregore3f1f352009-07-01 00:28:38 +00001184 // arguments left unspecified.
John McCall550e0c22009-10-21 00:40:46 +00001185 if (!TemplateArgs.hasTemplateArgument(T->getDepth(), T->getIndex())) {
1186 TemplateTypeParmTypeLoc NewTL
1187 = TLB.push<TemplateTypeParmTypeLoc>(TL.getType());
1188 NewTL.setNameLoc(TL.getNameLoc());
1189 return TL.getType();
1190 }
Mike Stump11289f42009-09-09 15:08:12 +00001191
Douglas Gregor840bd6c2010-12-20 22:05:00 +00001192 TemplateArgument Arg = TemplateArgs(T->getDepth(), T->getIndex());
1193
1194 if (T->isParameterPack()) {
1195 assert(Arg.getKind() == TemplateArgument::Pack &&
1196 "Missing argument pack");
1197
1198 if (getSema().ArgumentPackSubstitutionIndex == -1) {
Douglas Gregorada4b792011-01-14 02:55:32 +00001199 // We have the template argument pack, but we're not expanding the
1200 // enclosing pack expansion yet. Just save the template argument
1201 // pack for later substitution.
1202 QualType Result
1203 = getSema().Context.getSubstTemplateTypeParmPackType(T, Arg);
1204 SubstTemplateTypeParmPackTypeLoc NewTL
1205 = TLB.push<SubstTemplateTypeParmPackTypeLoc>(Result);
1206 NewTL.setNameLoc(TL.getNameLoc());
1207 return Result;
Douglas Gregor840bd6c2010-12-20 22:05:00 +00001208 }
1209
Douglas Gregora8bac7f2011-01-10 07:32:04 +00001210 assert(getSema().ArgumentPackSubstitutionIndex < (int)Arg.pack_size());
Douglas Gregor840bd6c2010-12-20 22:05:00 +00001211 Arg = Arg.pack_begin()[getSema().ArgumentPackSubstitutionIndex];
1212 }
1213
1214 assert(Arg.getKind() == TemplateArgument::Type &&
Douglas Gregorfe1e1102009-02-27 19:31:52 +00001215 "Template argument kind mismatch");
Douglas Gregor01afeef2009-08-28 20:31:08 +00001216
Douglas Gregor840bd6c2010-12-20 22:05:00 +00001217 QualType Replacement = Arg.getAsType();
John McCallcebee162009-10-18 09:09:24 +00001218
1219 // TODO: only do this uniquing once, at the start of instantiation.
John McCall550e0c22009-10-21 00:40:46 +00001220 QualType Result
1221 = getSema().Context.getSubstTemplateTypeParmType(T, Replacement);
1222 SubstTemplateTypeParmTypeLoc NewTL
1223 = TLB.push<SubstTemplateTypeParmTypeLoc>(Result);
1224 NewTL.setNameLoc(TL.getNameLoc());
1225 return Result;
Mike Stump11289f42009-09-09 15:08:12 +00001226 }
Douglas Gregorfe1e1102009-02-27 19:31:52 +00001227
1228 // The template type parameter comes from an inner template (e.g.,
1229 // the template parameter list of a member template inside the
1230 // template we are instantiating). Create a new template type
1231 // parameter with the template "level" reduced by one.
John McCall550e0c22009-10-21 00:40:46 +00001232 QualType Result
1233 = getSema().Context.getTemplateTypeParmType(T->getDepth()
1234 - TemplateArgs.getNumLevels(),
1235 T->getIndex(),
1236 T->isParameterPack(),
Douglas Gregor2ebcae12010-06-16 15:23:05 +00001237 T->getName());
John McCall550e0c22009-10-21 00:40:46 +00001238 TemplateTypeParmTypeLoc NewTL = TLB.push<TemplateTypeParmTypeLoc>(Result);
1239 NewTL.setNameLoc(TL.getNameLoc());
1240 return Result;
Douglas Gregor17c0d7b2009-02-28 00:25:32 +00001241}
Douglas Gregorfe1e1102009-02-27 19:31:52 +00001242
Douglas Gregorada4b792011-01-14 02:55:32 +00001243QualType
1244TemplateInstantiator::TransformSubstTemplateTypeParmPackType(
1245 TypeLocBuilder &TLB,
1246 SubstTemplateTypeParmPackTypeLoc TL) {
1247 if (getSema().ArgumentPackSubstitutionIndex == -1) {
1248 // We aren't expanding the parameter pack, so just return ourselves.
1249 SubstTemplateTypeParmPackTypeLoc NewTL
1250 = TLB.push<SubstTemplateTypeParmPackTypeLoc>(TL.getType());
1251 NewTL.setNameLoc(TL.getNameLoc());
1252 return TL.getType();
1253 }
1254
1255 const TemplateArgument &ArgPack = TL.getTypePtr()->getArgumentPack();
1256 unsigned Index = (unsigned)getSema().ArgumentPackSubstitutionIndex;
1257 assert(Index < ArgPack.pack_size() && "Substitution index out-of-range");
1258
1259 QualType Result = ArgPack.pack_begin()[Index].getAsType();
1260 Result = getSema().Context.getSubstTemplateTypeParmType(
1261 TL.getTypePtr()->getReplacedParameter(),
1262 Result);
1263 SubstTemplateTypeParmTypeLoc NewTL
1264 = TLB.push<SubstTemplateTypeParmTypeLoc>(Result);
1265 NewTL.setNameLoc(TL.getNameLoc());
1266 return Result;
1267}
1268
John McCall76d824f2009-08-25 22:02:44 +00001269/// \brief Perform substitution on the type T with a given set of template
1270/// arguments.
Douglas Gregorfe1e1102009-02-27 19:31:52 +00001271///
1272/// This routine substitutes the given template arguments into the
1273/// type T and produces the instantiated type.
1274///
1275/// \param T the type into which the template arguments will be
1276/// substituted. If this type is not dependent, it will be returned
1277/// immediately.
1278///
1279/// \param TemplateArgs the template arguments that will be
1280/// substituted for the top-level template parameters within T.
1281///
Douglas Gregorfe1e1102009-02-27 19:31:52 +00001282/// \param Loc the location in the source code where this substitution
1283/// is being performed. It will typically be the location of the
1284/// declarator (if we're instantiating the type of some declaration)
1285/// or the location of the type in the source code (if, e.g., we're
1286/// instantiating the type of a cast expression).
1287///
1288/// \param Entity the name of the entity associated with a declaration
1289/// being instantiated (if any). May be empty to indicate that there
1290/// is no such entity (if, e.g., this is a type that occurs as part of
1291/// a cast expression) or that the entity has no name (e.g., an
1292/// unnamed function parameter).
1293///
1294/// \returns If the instantiation succeeds, the instantiated
1295/// type. Otherwise, produces diagnostics and returns a NULL type.
John McCallbcd03502009-12-07 02:54:59 +00001296TypeSourceInfo *Sema::SubstType(TypeSourceInfo *T,
John McCall609459e2009-10-21 00:58:09 +00001297 const MultiLevelTemplateArgumentList &Args,
1298 SourceLocation Loc,
1299 DeclarationName Entity) {
1300 assert(!ActiveTemplateInstantiations.empty() &&
1301 "Cannot perform an instantiation without some context on the "
1302 "instantiation stack");
1303
Douglas Gregor5a5073e2010-05-24 17:22:01 +00001304 if (!T->getType()->isDependentType() &&
1305 !T->getType()->isVariablyModifiedType())
John McCall609459e2009-10-21 00:58:09 +00001306 return T;
1307
1308 TemplateInstantiator Instantiator(*this, Args, Loc, Entity);
1309 return Instantiator.TransformType(T);
1310}
1311
Douglas Gregor5499af42011-01-05 23:12:31 +00001312TypeSourceInfo *Sema::SubstType(TypeLoc TL,
1313 const MultiLevelTemplateArgumentList &Args,
1314 SourceLocation Loc,
1315 DeclarationName Entity) {
1316 assert(!ActiveTemplateInstantiations.empty() &&
1317 "Cannot perform an instantiation without some context on the "
1318 "instantiation stack");
1319
1320 if (TL.getType().isNull())
1321 return 0;
1322
1323 if (!TL.getType()->isDependentType() &&
1324 !TL.getType()->isVariablyModifiedType()) {
1325 // FIXME: Make a copy of the TypeLoc data here, so that we can
1326 // return a new TypeSourceInfo. Inefficient!
1327 TypeLocBuilder TLB;
1328 TLB.pushFullCopy(TL);
1329 return TLB.getTypeSourceInfo(Context, TL.getType());
1330 }
1331
1332 TemplateInstantiator Instantiator(*this, Args, Loc, Entity);
1333 TypeLocBuilder TLB;
1334 TLB.reserve(TL.getFullDataSize());
1335 QualType Result = Instantiator.TransformType(TLB, TL);
1336 if (Result.isNull())
1337 return 0;
1338
1339 return TLB.getTypeSourceInfo(Context, Result);
1340}
1341
John McCall609459e2009-10-21 00:58:09 +00001342/// Deprecated form of the above.
Mike Stump11289f42009-09-09 15:08:12 +00001343QualType Sema::SubstType(QualType T,
Douglas Gregor01afeef2009-08-28 20:31:08 +00001344 const MultiLevelTemplateArgumentList &TemplateArgs,
John McCall76d824f2009-08-25 22:02:44 +00001345 SourceLocation Loc, DeclarationName Entity) {
Douglas Gregor79cf6032009-03-10 20:44:00 +00001346 assert(!ActiveTemplateInstantiations.empty() &&
1347 "Cannot perform an instantiation without some context on the "
1348 "instantiation stack");
1349
Douglas Gregor5a5073e2010-05-24 17:22:01 +00001350 // If T is not a dependent type or a variably-modified type, there
1351 // is nothing to do.
1352 if (!T->isDependentType() && !T->isVariablyModifiedType())
Douglas Gregorfe1e1102009-02-27 19:31:52 +00001353 return T;
1354
Douglas Gregord6ff3322009-08-04 16:50:30 +00001355 TemplateInstantiator Instantiator(*this, TemplateArgs, Loc, Entity);
1356 return Instantiator.TransformType(T);
Douglas Gregorfe1e1102009-02-27 19:31:52 +00001357}
Douglas Gregor463421d2009-03-03 04:44:36 +00001358
John McCallb29f78f2010-04-09 17:38:44 +00001359static bool NeedsInstantiationAsFunctionType(TypeSourceInfo *T) {
Douglas Gregor5a5073e2010-05-24 17:22:01 +00001360 if (T->getType()->isDependentType() || T->getType()->isVariablyModifiedType())
John McCallb29f78f2010-04-09 17:38:44 +00001361 return true;
1362
Abramo Bagnara6d810632010-12-14 22:11:44 +00001363 TypeLoc TL = T->getTypeLoc().IgnoreParens();
John McCallb29f78f2010-04-09 17:38:44 +00001364 if (!isa<FunctionProtoTypeLoc>(TL))
1365 return false;
1366
1367 FunctionProtoTypeLoc FP = cast<FunctionProtoTypeLoc>(TL);
1368 for (unsigned I = 0, E = FP.getNumArgs(); I != E; ++I) {
1369 ParmVarDecl *P = FP.getArg(I);
1370
1371 // TODO: currently we always rebuild expressions. When we
1372 // properly get lazier about this, we should use the same
1373 // logic to avoid rebuilding prototypes here.
Douglas Gregor9cc278222011-01-05 21:14:17 +00001374 if (P->hasDefaultArg())
John McCallb29f78f2010-04-09 17:38:44 +00001375 return true;
1376 }
1377
1378 return false;
1379}
1380
1381/// A form of SubstType intended specifically for instantiating the
1382/// type of a FunctionDecl. Its purpose is solely to force the
1383/// instantiation of default-argument expressions.
1384TypeSourceInfo *Sema::SubstFunctionDeclType(TypeSourceInfo *T,
1385 const MultiLevelTemplateArgumentList &Args,
1386 SourceLocation Loc,
1387 DeclarationName Entity) {
1388 assert(!ActiveTemplateInstantiations.empty() &&
1389 "Cannot perform an instantiation without some context on the "
1390 "instantiation stack");
1391
1392 if (!NeedsInstantiationAsFunctionType(T))
1393 return T;
1394
1395 TemplateInstantiator Instantiator(*this, Args, Loc, Entity);
1396
1397 TypeLocBuilder TLB;
1398
1399 TypeLoc TL = T->getTypeLoc();
1400 TLB.reserve(TL.getFullDataSize());
1401
John McCall31f82722010-11-12 08:19:04 +00001402 QualType Result = Instantiator.TransformType(TLB, TL);
John McCallb29f78f2010-04-09 17:38:44 +00001403 if (Result.isNull())
1404 return 0;
1405
1406 return TLB.getTypeSourceInfo(Context, Result);
1407}
1408
Douglas Gregor940bca72010-04-12 07:48:19 +00001409ParmVarDecl *Sema::SubstParmVarDecl(ParmVarDecl *OldParm,
Douglas Gregor715e4612011-01-14 22:40:04 +00001410 const MultiLevelTemplateArgumentList &TemplateArgs,
1411 llvm::Optional<unsigned> NumExpansions) {
Douglas Gregor940bca72010-04-12 07:48:19 +00001412 TypeSourceInfo *OldDI = OldParm->getTypeSourceInfo();
Douglas Gregor5499af42011-01-05 23:12:31 +00001413 TypeSourceInfo *NewDI = 0;
1414
Douglas Gregor5499af42011-01-05 23:12:31 +00001415 TypeLoc OldTL = OldDI->getTypeLoc();
1416 if (isa<PackExpansionTypeLoc>(OldTL)) {
1417 PackExpansionTypeLoc ExpansionTL = cast<PackExpansionTypeLoc>(OldTL);
Douglas Gregor5499af42011-01-05 23:12:31 +00001418
1419 // We have a function parameter pack. Substitute into the pattern of the
1420 // expansion.
1421 NewDI = SubstType(ExpansionTL.getPatternLoc(), TemplateArgs,
1422 OldParm->getLocation(), OldParm->getDeclName());
1423 if (!NewDI)
1424 return 0;
1425
1426 if (NewDI->getType()->containsUnexpandedParameterPack()) {
1427 // We still have unexpanded parameter packs, which means that
1428 // our function parameter is still a function parameter pack.
1429 // Therefore, make its type a pack expansion type.
Douglas Gregor0dca5fd2011-01-14 17:04:44 +00001430 NewDI = CheckPackExpansion(NewDI, ExpansionTL.getEllipsisLoc(),
Douglas Gregor715e4612011-01-14 22:40:04 +00001431 NumExpansions);
Douglas Gregor5499af42011-01-05 23:12:31 +00001432 }
1433 } else {
1434 NewDI = SubstType(OldDI, TemplateArgs, OldParm->getLocation(),
1435 OldParm->getDeclName());
1436 }
1437
Douglas Gregor940bca72010-04-12 07:48:19 +00001438 if (!NewDI)
1439 return 0;
1440
1441 if (NewDI->getType()->isVoidType()) {
1442 Diag(OldParm->getLocation(), diag::err_param_with_void_type);
1443 return 0;
1444 }
1445
1446 ParmVarDecl *NewParm = CheckParameter(Context.getTranslationUnitDecl(),
1447 NewDI, NewDI->getType(),
1448 OldParm->getIdentifier(),
1449 OldParm->getLocation(),
Douglas Gregorc4df4072010-04-19 22:54:31 +00001450 OldParm->getStorageClass(),
1451 OldParm->getStorageClassAsWritten());
Douglas Gregor940bca72010-04-12 07:48:19 +00001452 if (!NewParm)
1453 return 0;
Douglas Gregor6044d692010-05-19 17:02:24 +00001454
Douglas Gregor940bca72010-04-12 07:48:19 +00001455 // Mark the (new) default argument as uninstantiated (if any).
1456 if (OldParm->hasUninstantiatedDefaultArg()) {
1457 Expr *Arg = OldParm->getUninstantiatedDefaultArg();
1458 NewParm->setUninstantiatedDefaultArg(Arg);
Douglas Gregor758cb672010-10-12 18:23:32 +00001459 } else if (OldParm->hasUnparsedDefaultArg()) {
1460 NewParm->setUnparsedDefaultArg();
1461 UnparsedDefaultArgInstantiations[OldParm].push_back(NewParm);
Douglas Gregor940bca72010-04-12 07:48:19 +00001462 } else if (Expr *Arg = OldParm->getDefaultArg())
1463 NewParm->setUninstantiatedDefaultArg(Arg);
1464
1465 NewParm->setHasInheritedDefaultArg(OldParm->hasInheritedDefaultArg());
1466
Douglas Gregor5499af42011-01-05 23:12:31 +00001467 // FIXME: When OldParm is a parameter pack and NewParm is not a parameter
1468 // pack, we actually have a set of instantiated locations. Maintain this set!
Douglas Gregorf3010112011-01-07 16:43:16 +00001469 if (OldParm->isParameterPack() && !NewParm->isParameterPack()) {
1470 // Add the new parameter to
1471 CurrentInstantiationScope->InstantiatedLocalPackArg(OldParm, NewParm);
1472 } else {
1473 // Introduce an Old -> New mapping
Douglas Gregor5499af42011-01-05 23:12:31 +00001474 CurrentInstantiationScope->InstantiatedLocal(OldParm, NewParm);
Douglas Gregorf3010112011-01-07 16:43:16 +00001475 }
Douglas Gregor5499af42011-01-05 23:12:31 +00001476
Argyrios Kyrtzidis3816ed42010-07-19 10:14:41 +00001477 // FIXME: OldParm may come from a FunctionProtoType, in which case CurContext
1478 // can be anything, is this right ?
Fariborz Jahanian714447b2010-07-13 21:05:02 +00001479 NewParm->setDeclContext(CurContext);
Fariborz Jahaniana6c7efe2010-07-13 20:05:58 +00001480
Douglas Gregor940bca72010-04-12 07:48:19 +00001481 return NewParm;
1482}
1483
Douglas Gregordd472162011-01-07 00:20:55 +00001484/// \brief Substitute the given template arguments into the given set of
1485/// parameters, producing the set of parameter types that would be generated
1486/// from such a substitution.
1487bool Sema::SubstParmTypes(SourceLocation Loc,
1488 ParmVarDecl **Params, unsigned NumParams,
1489 const MultiLevelTemplateArgumentList &TemplateArgs,
Douglas Gregorf3010112011-01-07 16:43:16 +00001490 llvm::SmallVectorImpl<QualType> &ParamTypes,
1491 llvm::SmallVectorImpl<ParmVarDecl *> *OutParams) {
Douglas Gregordd472162011-01-07 00:20:55 +00001492 assert(!ActiveTemplateInstantiations.empty() &&
1493 "Cannot perform an instantiation without some context on the "
1494 "instantiation stack");
1495
1496 TemplateInstantiator Instantiator(*this, TemplateArgs, Loc,
1497 DeclarationName());
1498 return Instantiator.TransformFunctionTypeParams(Loc, Params, NumParams, 0,
Douglas Gregorf3010112011-01-07 16:43:16 +00001499 ParamTypes, OutParams);
Douglas Gregordd472162011-01-07 00:20:55 +00001500}
1501
John McCall76d824f2009-08-25 22:02:44 +00001502/// \brief Perform substitution on the base class specifiers of the
1503/// given class template specialization.
Douglas Gregor463421d2009-03-03 04:44:36 +00001504///
1505/// Produces a diagnostic and returns true on error, returns false and
1506/// attaches the instantiated base classes to the class template
1507/// specialization if successful.
Mike Stump11289f42009-09-09 15:08:12 +00001508bool
John McCall76d824f2009-08-25 22:02:44 +00001509Sema::SubstBaseSpecifiers(CXXRecordDecl *Instantiation,
1510 CXXRecordDecl *Pattern,
Douglas Gregor01afeef2009-08-28 20:31:08 +00001511 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor463421d2009-03-03 04:44:36 +00001512 bool Invalid = false;
Douglas Gregor6181ded2009-05-29 18:27:38 +00001513 llvm::SmallVector<CXXBaseSpecifier*, 4> InstantiatedBases;
Mike Stump11289f42009-09-09 15:08:12 +00001514 for (ClassTemplateSpecializationDecl::base_class_iterator
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001515 Base = Pattern->bases_begin(), BaseEnd = Pattern->bases_end();
Douglas Gregor2a72edd2009-03-10 18:52:44 +00001516 Base != BaseEnd; ++Base) {
Douglas Gregor463421d2009-03-03 04:44:36 +00001517 if (!Base->getType()->isDependentType()) {
Fariborz Jahanian5c14ec32009-07-22 17:41:53 +00001518 InstantiatedBases.push_back(new (Context) CXXBaseSpecifier(*Base));
Douglas Gregor463421d2009-03-03 04:44:36 +00001519 continue;
1520 }
1521
Douglas Gregor752a5952011-01-03 22:36:02 +00001522 SourceLocation EllipsisLoc;
Douglas Gregorc52264e2011-03-02 02:04:06 +00001523 TypeSourceInfo *BaseTypeLoc;
Douglas Gregor752a5952011-01-03 22:36:02 +00001524 if (Base->isPackExpansion()) {
1525 // This is a pack expansion. See whether we should expand it now, or
1526 // wait until later.
1527 llvm::SmallVector<UnexpandedParameterPack, 2> Unexpanded;
1528 collectUnexpandedParameterPacks(Base->getTypeSourceInfo()->getTypeLoc(),
1529 Unexpanded);
1530 bool ShouldExpand = false;
Douglas Gregora8bac7f2011-01-10 07:32:04 +00001531 bool RetainExpansion = false;
Douglas Gregor0dca5fd2011-01-14 17:04:44 +00001532 llvm::Optional<unsigned> NumExpansions;
Douglas Gregor752a5952011-01-03 22:36:02 +00001533 if (CheckParameterPacksForExpansion(Base->getEllipsisLoc(),
1534 Base->getSourceRange(),
1535 Unexpanded.data(), Unexpanded.size(),
1536 TemplateArgs, ShouldExpand,
Douglas Gregora8bac7f2011-01-10 07:32:04 +00001537 RetainExpansion,
Douglas Gregor752a5952011-01-03 22:36:02 +00001538 NumExpansions)) {
Douglas Gregor752a5952011-01-03 22:36:02 +00001539 Invalid = true;
Douglas Gregor44e7df62011-01-04 00:32:56 +00001540 continue;
Douglas Gregor752a5952011-01-03 22:36:02 +00001541 }
1542
1543 // If we should expand this pack expansion now, do so.
1544 if (ShouldExpand) {
Douglas Gregor0dca5fd2011-01-14 17:04:44 +00001545 for (unsigned I = 0; I != *NumExpansions; ++I) {
Douglas Gregor752a5952011-01-03 22:36:02 +00001546 Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(*this, I);
1547
1548 TypeSourceInfo *BaseTypeLoc = SubstType(Base->getTypeSourceInfo(),
1549 TemplateArgs,
1550 Base->getSourceRange().getBegin(),
1551 DeclarationName());
1552 if (!BaseTypeLoc) {
1553 Invalid = true;
1554 continue;
1555 }
1556
1557 if (CXXBaseSpecifier *InstantiatedBase
1558 = CheckBaseSpecifier(Instantiation,
1559 Base->getSourceRange(),
1560 Base->isVirtual(),
1561 Base->getAccessSpecifierAsWritten(),
1562 BaseTypeLoc,
1563 SourceLocation()))
1564 InstantiatedBases.push_back(InstantiatedBase);
1565 else
1566 Invalid = true;
1567 }
1568
1569 continue;
1570 }
1571
1572 // The resulting base specifier will (still) be a pack expansion.
1573 EllipsisLoc = Base->getEllipsisLoc();
Douglas Gregorc52264e2011-03-02 02:04:06 +00001574 Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(*this, -1);
1575 BaseTypeLoc = SubstType(Base->getTypeSourceInfo(),
1576 TemplateArgs,
1577 Base->getSourceRange().getBegin(),
1578 DeclarationName());
1579 } else {
1580 BaseTypeLoc = SubstType(Base->getTypeSourceInfo(),
1581 TemplateArgs,
1582 Base->getSourceRange().getBegin(),
1583 DeclarationName());
Douglas Gregor752a5952011-01-03 22:36:02 +00001584 }
1585
Nick Lewycky19b9f952010-07-26 16:56:01 +00001586 if (!BaseTypeLoc) {
Douglas Gregor463421d2009-03-03 04:44:36 +00001587 Invalid = true;
1588 continue;
1589 }
1590
1591 if (CXXBaseSpecifier *InstantiatedBase
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001592 = CheckBaseSpecifier(Instantiation,
Douglas Gregor463421d2009-03-03 04:44:36 +00001593 Base->getSourceRange(),
1594 Base->isVirtual(),
1595 Base->getAccessSpecifierAsWritten(),
Douglas Gregor752a5952011-01-03 22:36:02 +00001596 BaseTypeLoc,
1597 EllipsisLoc))
Douglas Gregor463421d2009-03-03 04:44:36 +00001598 InstantiatedBases.push_back(InstantiatedBase);
1599 else
1600 Invalid = true;
1601 }
1602
Douglas Gregor2a72edd2009-03-10 18:52:44 +00001603 if (!Invalid &&
Jay Foad7d0479f2009-05-21 09:52:38 +00001604 AttachBaseSpecifiers(Instantiation, InstantiatedBases.data(),
Douglas Gregor463421d2009-03-03 04:44:36 +00001605 InstantiatedBases.size()))
1606 Invalid = true;
1607
1608 return Invalid;
1609}
1610
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001611/// \brief Instantiate the definition of a class from a given pattern.
1612///
1613/// \param PointOfInstantiation The point of instantiation within the
1614/// source code.
1615///
1616/// \param Instantiation is the declaration whose definition is being
1617/// instantiated. This will be either a class template specialization
1618/// or a member class of a class template specialization.
1619///
1620/// \param Pattern is the pattern from which the instantiation
1621/// occurs. This will be either the declaration of a class template or
1622/// the declaration of a member class of a class template.
1623///
1624/// \param TemplateArgs The template arguments to be substituted into
1625/// the pattern.
1626///
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001627/// \param TSK the kind of implicit or explicit instantiation to perform.
Douglas Gregor8a2e6012009-08-24 15:23:48 +00001628///
1629/// \param Complain whether to complain if the class cannot be instantiated due
1630/// to the lack of a definition.
1631///
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001632/// \returns true if an error occurred, false otherwise.
1633bool
1634Sema::InstantiateClass(SourceLocation PointOfInstantiation,
1635 CXXRecordDecl *Instantiation, CXXRecordDecl *Pattern,
Douglas Gregor01afeef2009-08-28 20:31:08 +00001636 const MultiLevelTemplateArgumentList &TemplateArgs,
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001637 TemplateSpecializationKind TSK,
Douglas Gregor8a2e6012009-08-24 15:23:48 +00001638 bool Complain) {
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001639 bool Invalid = false;
John McCall87a44eb2009-08-20 01:44:21 +00001640
Mike Stump11289f42009-09-09 15:08:12 +00001641 CXXRecordDecl *PatternDef
Douglas Gregor0a5a2212010-02-11 01:04:33 +00001642 = cast_or_null<CXXRecordDecl>(Pattern->getDefinition());
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001643 if (!PatternDef) {
Douglas Gregor8a2e6012009-08-24 15:23:48 +00001644 if (!Complain) {
1645 // Say nothing
1646 } else if (Pattern == Instantiation->getInstantiatedFromMemberClass()) {
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001647 Diag(PointOfInstantiation,
1648 diag::err_implicit_instantiate_member_undefined)
1649 << Context.getTypeDeclType(Instantiation);
1650 Diag(Pattern->getLocation(), diag::note_member_of_template_here);
1651 } else {
Douglas Gregora1f49972009-05-13 00:25:59 +00001652 Diag(PointOfInstantiation, diag::err_template_instantiate_undefined)
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001653 << (TSK != TSK_ImplicitInstantiation)
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001654 << Context.getTypeDeclType(Instantiation);
1655 Diag(Pattern->getLocation(), diag::note_template_decl_here);
1656 }
1657 return true;
1658 }
1659 Pattern = PatternDef;
1660
Douglas Gregord6ba93d2009-10-15 15:54:05 +00001661 // \brief Record the point of instantiation.
1662 if (MemberSpecializationInfo *MSInfo
1663 = Instantiation->getMemberSpecializationInfo()) {
1664 MSInfo->setTemplateSpecializationKind(TSK);
1665 MSInfo->setPointOfInstantiation(PointOfInstantiation);
Douglas Gregoref6ab412009-10-27 06:26:26 +00001666 } else if (ClassTemplateSpecializationDecl *Spec
1667 = dyn_cast<ClassTemplateSpecializationDecl>(Instantiation)) {
1668 Spec->setTemplateSpecializationKind(TSK);
1669 Spec->setPointOfInstantiation(PointOfInstantiation);
Douglas Gregord6ba93d2009-10-15 15:54:05 +00001670 }
1671
Douglas Gregorf3430ae2009-03-25 21:23:52 +00001672 InstantiatingTemplate Inst(*this, PointOfInstantiation, Instantiation);
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001673 if (Inst)
1674 return true;
1675
1676 // Enter the scope of this instantiation. We don't use
1677 // PushDeclContext because we don't have a scope.
John McCall80e58cd2010-04-29 00:35:03 +00001678 ContextRAII SavedContext(*this, Instantiation);
Douglas Gregor17158422010-05-12 17:27:19 +00001679 EnterExpressionEvaluationContext EvalContext(*this,
John McCallfaf5fb42010-08-26 23:41:50 +00001680 Sema::PotentiallyEvaluated);
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001681
Douglas Gregor51121572010-03-24 01:33:17 +00001682 // If this is an instantiation of a local class, merge this local
1683 // instantiation scope with the enclosing scope. Otherwise, every
1684 // instantiation of a class has its own local instantiation scope.
1685 bool MergeWithParentScope = !Instantiation->isDefinedOutsideFunctionOrMethod();
John McCall19c1bfd2010-08-25 05:32:35 +00001686 LocalInstantiationScope Scope(*this, MergeWithParentScope);
Douglas Gregor51121572010-03-24 01:33:17 +00001687
John McCall6602bb12010-08-01 02:01:53 +00001688 // Pull attributes from the pattern onto the instantiation.
1689 InstantiateAttrs(TemplateArgs, Pattern, Instantiation);
1690
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001691 // Start the definition of this instantiation.
1692 Instantiation->startDefinition();
Douglas Gregore9029562010-05-06 00:28:52 +00001693
1694 Instantiation->setTagKind(Pattern->getTagKind());
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001695
John McCall76d824f2009-08-25 22:02:44 +00001696 // Do substitution on the base class specifiers.
1697 if (SubstBaseSpecifiers(Instantiation, Pattern, TemplateArgs))
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001698 Invalid = true;
1699
Douglas Gregor869853e2010-11-10 19:44:59 +00001700 TemplateDeclInstantiator Instantiator(*this, Instantiation, TemplateArgs);
John McCall48871652010-08-21 09:40:31 +00001701 llvm::SmallVector<Decl*, 4> Fields;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001702 for (RecordDecl::decl_iterator Member = Pattern->decls_begin(),
Mike Stump11289f42009-09-09 15:08:12 +00001703 MemberEnd = Pattern->decls_end();
Douglas Gregorbcced4e2009-04-09 21:40:53 +00001704 Member != MemberEnd; ++Member) {
Argyrios Kyrtzidis9a94d9b2010-11-04 03:18:57 +00001705 // Don't instantiate members not belonging in this semantic context.
1706 // e.g. for:
1707 // @code
1708 // template <int i> class A {
1709 // class B *g;
1710 // };
1711 // @endcode
1712 // 'class B' has the template as lexical context but semantically it is
1713 // introduced in namespace scope.
1714 if ((*Member)->getDeclContext() != Pattern)
1715 continue;
1716
Douglas Gregor869853e2010-11-10 19:44:59 +00001717 if ((*Member)->isInvalidDecl()) {
1718 Invalid = true;
1719 continue;
1720 }
1721
1722 Decl *NewMember = Instantiator.Visit(*Member);
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001723 if (NewMember) {
Eli Friedmand0e8de22009-12-07 00:22:08 +00001724 if (FieldDecl *Field = dyn_cast<FieldDecl>(NewMember))
John McCall48871652010-08-21 09:40:31 +00001725 Fields.push_back(Field);
Eli Friedmand0e8de22009-12-07 00:22:08 +00001726 else if (NewMember->isInvalidDecl())
1727 Invalid = true;
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001728 } else {
1729 // FIXME: Eventually, a NULL return will mean that one of the
Mike Stump87c57ac2009-05-16 07:39:55 +00001730 // instantiations was a semantic disaster, and we'll want to set Invalid =
1731 // true. For now, we expect to skip some members that we can't yet handle.
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001732 }
1733 }
1734
1735 // Finish checking fields.
John McCall48871652010-08-21 09:40:31 +00001736 ActOnFields(0, Instantiation->getLocation(), Instantiation,
Jay Foad7d0479f2009-05-21 09:52:38 +00001737 Fields.data(), Fields.size(), SourceLocation(), SourceLocation(),
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001738 0);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001739 CheckCompletedCXXClass(Instantiation);
Douglas Gregor3c74d412009-10-14 20:14:33 +00001740 if (Instantiation->isInvalidDecl())
1741 Invalid = true;
Douglas Gregor869853e2010-11-10 19:44:59 +00001742 else {
1743 // Instantiate any out-of-line class template partial
1744 // specializations now.
1745 for (TemplateDeclInstantiator::delayed_partial_spec_iterator
1746 P = Instantiator.delayed_partial_spec_begin(),
1747 PEnd = Instantiator.delayed_partial_spec_end();
1748 P != PEnd; ++P) {
1749 if (!Instantiator.InstantiateClassTemplatePartialSpecialization(
1750 P->first,
1751 P->second)) {
1752 Invalid = true;
1753 break;
1754 }
1755 }
1756 }
1757
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001758 // Exit the scope of this instantiation.
John McCall80e58cd2010-04-29 00:35:03 +00001759 SavedContext.pop();
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001760
Douglas Gregor88d292c2010-05-13 16:44:06 +00001761 if (!Invalid) {
Douglas Gregor28ad4b52009-05-26 20:50:29 +00001762 Consumer.HandleTagDeclDefinition(Instantiation);
1763
Douglas Gregor88d292c2010-05-13 16:44:06 +00001764 // Always emit the vtable for an explicit instantiation definition
1765 // of a polymorphic class template specialization.
1766 if (TSK == TSK_ExplicitInstantiationDefinition)
1767 MarkVTableUsed(PointOfInstantiation, Instantiation, true);
1768 }
1769
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001770 return Invalid;
1771}
1772
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +00001773namespace {
1774 /// \brief A partial specialization whose template arguments have matched
1775 /// a given template-id.
1776 struct PartialSpecMatchResult {
1777 ClassTemplatePartialSpecializationDecl *Partial;
1778 TemplateArgumentList *Args;
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +00001779 };
1780}
1781
Mike Stump11289f42009-09-09 15:08:12 +00001782bool
Douglas Gregor463421d2009-03-03 04:44:36 +00001783Sema::InstantiateClassTemplateSpecialization(
Douglas Gregoref6ab412009-10-27 06:26:26 +00001784 SourceLocation PointOfInstantiation,
Douglas Gregor463421d2009-03-03 04:44:36 +00001785 ClassTemplateSpecializationDecl *ClassTemplateSpec,
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001786 TemplateSpecializationKind TSK,
Douglas Gregor8a2e6012009-08-24 15:23:48 +00001787 bool Complain) {
Douglas Gregor463421d2009-03-03 04:44:36 +00001788 // Perform the actual instantiation on the canonical declaration.
1789 ClassTemplateSpec = cast<ClassTemplateSpecializationDecl>(
Argyrios Kyrtzidis6b7e3762009-07-18 00:34:25 +00001790 ClassTemplateSpec->getCanonicalDecl());
Douglas Gregor463421d2009-03-03 04:44:36 +00001791
Douglas Gregor4aa04b12009-09-11 21:19:12 +00001792 // Check whether we have already instantiated or specialized this class
1793 // template specialization.
1794 if (ClassTemplateSpec->getSpecializationKind() != TSK_Undeclared) {
1795 if (ClassTemplateSpec->getSpecializationKind() ==
1796 TSK_ExplicitInstantiationDeclaration &&
1797 TSK == TSK_ExplicitInstantiationDefinition) {
1798 // An explicit instantiation definition follows an explicit instantiation
1799 // declaration (C++0x [temp.explicit]p10); go ahead and perform the
1800 // explicit instantiation.
1801 ClassTemplateSpec->setSpecializationKind(TSK);
Douglas Gregor88d292c2010-05-13 16:44:06 +00001802
1803 // If this is an explicit instantiation definition, mark the
1804 // vtable as used.
1805 if (TSK == TSK_ExplicitInstantiationDefinition)
1806 MarkVTableUsed(PointOfInstantiation, ClassTemplateSpec, true);
1807
Douglas Gregor4aa04b12009-09-11 21:19:12 +00001808 return false;
1809 }
1810
1811 // We can only instantiate something that hasn't already been
1812 // instantiated or specialized. Fail without any diagnostics: our
1813 // caller will provide an error message.
Douglas Gregor463421d2009-03-03 04:44:36 +00001814 return true;
Douglas Gregor4aa04b12009-09-11 21:19:12 +00001815 }
Douglas Gregor463421d2009-03-03 04:44:36 +00001816
Douglas Gregor00a511f2009-09-15 16:51:42 +00001817 if (ClassTemplateSpec->isInvalidDecl())
1818 return true;
1819
Douglas Gregor463421d2009-03-03 04:44:36 +00001820 ClassTemplateDecl *Template = ClassTemplateSpec->getSpecializedTemplate();
Douglas Gregor01afeef2009-08-28 20:31:08 +00001821 CXXRecordDecl *Pattern = 0;
Douglas Gregor2373c592009-05-31 09:31:02 +00001822
Douglas Gregor170bc422009-06-12 22:31:52 +00001823 // C++ [temp.class.spec.match]p1:
1824 // When a class template is used in a context that requires an
1825 // instantiation of the class, it is necessary to determine
1826 // whether the instantiation is to be generated using the primary
1827 // template or one of the partial specializations. This is done by
1828 // matching the template arguments of the class template
1829 // specialization with the template argument lists of the partial
1830 // specializations.
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +00001831 typedef PartialSpecMatchResult MatchResult;
Douglas Gregorb7ae10f2009-06-05 00:53:49 +00001832 llvm::SmallVector<MatchResult, 4> Matched;
Douglas Gregor407e9612010-04-30 05:56:50 +00001833 llvm::SmallVector<ClassTemplatePartialSpecializationDecl *, 4> PartialSpecs;
1834 Template->getPartialSpecializations(PartialSpecs);
1835 for (unsigned I = 0, N = PartialSpecs.size(); I != N; ++I) {
1836 ClassTemplatePartialSpecializationDecl *Partial = PartialSpecs[I];
John McCallbc077cf2010-02-08 23:07:23 +00001837 TemplateDeductionInfo Info(Context, PointOfInstantiation);
Douglas Gregor181aa4a2009-06-12 18:26:56 +00001838 if (TemplateDeductionResult Result
Douglas Gregor407e9612010-04-30 05:56:50 +00001839 = DeduceTemplateArguments(Partial,
Douglas Gregor181aa4a2009-06-12 18:26:56 +00001840 ClassTemplateSpec->getTemplateArgs(),
1841 Info)) {
1842 // FIXME: Store the failed-deduction information for use in
1843 // diagnostics, later.
1844 (void)Result;
1845 } else {
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +00001846 Matched.push_back(PartialSpecMatchResult());
1847 Matched.back().Partial = Partial;
1848 Matched.back().Args = Info.take();
Douglas Gregor181aa4a2009-06-12 18:26:56 +00001849 }
Douglas Gregor2373c592009-05-31 09:31:02 +00001850 }
1851
Douglas Gregor0231d8d2011-01-19 20:10:05 +00001852 // If we're dealing with a member template where the template parameters
1853 // have been instantiated, this provides the original template parameters
1854 // from which the member template's parameters were instantiated.
1855 llvm::SmallVector<const NamedDecl *, 4> InstantiatedTemplateParameters;
1856
Douglas Gregor21610382009-10-29 00:04:11 +00001857 if (Matched.size() >= 1) {
Douglas Gregorbe999392009-09-15 16:23:51 +00001858 llvm::SmallVector<MatchResult, 4>::iterator Best = Matched.begin();
Douglas Gregor21610382009-10-29 00:04:11 +00001859 if (Matched.size() == 1) {
1860 // -- If exactly one matching specialization is found, the
1861 // instantiation is generated from that specialization.
1862 // We don't need to do anything for this.
1863 } else {
1864 // -- If more than one matching specialization is found, the
1865 // partial order rules (14.5.4.2) are used to determine
1866 // whether one of the specializations is more specialized
1867 // than the others. If none of the specializations is more
1868 // specialized than all of the other matching
1869 // specializations, then the use of the class template is
1870 // ambiguous and the program is ill-formed.
1871 for (llvm::SmallVector<MatchResult, 4>::iterator P = Best + 1,
1872 PEnd = Matched.end();
1873 P != PEnd; ++P) {
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +00001874 if (getMoreSpecializedPartialSpecialization(P->Partial, Best->Partial,
John McCallbc077cf2010-02-08 23:07:23 +00001875 PointOfInstantiation)
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +00001876 == P->Partial)
Douglas Gregor21610382009-10-29 00:04:11 +00001877 Best = P;
Douglas Gregorbe999392009-09-15 16:23:51 +00001878 }
Douglas Gregorbe999392009-09-15 16:23:51 +00001879
Douglas Gregor21610382009-10-29 00:04:11 +00001880 // Determine if the best partial specialization is more specialized than
1881 // the others.
1882 bool Ambiguous = false;
Douglas Gregorbe999392009-09-15 16:23:51 +00001883 for (llvm::SmallVector<MatchResult, 4>::iterator P = Matched.begin(),
1884 PEnd = Matched.end();
Douglas Gregor21610382009-10-29 00:04:11 +00001885 P != PEnd; ++P) {
1886 if (P != Best &&
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +00001887 getMoreSpecializedPartialSpecialization(P->Partial, Best->Partial,
John McCallbc077cf2010-02-08 23:07:23 +00001888 PointOfInstantiation)
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +00001889 != Best->Partial) {
Douglas Gregor21610382009-10-29 00:04:11 +00001890 Ambiguous = true;
1891 break;
1892 }
1893 }
1894
1895 if (Ambiguous) {
1896 // Partial ordering did not produce a clear winner. Complain.
1897 ClassTemplateSpec->setInvalidDecl();
1898 Diag(PointOfInstantiation, diag::err_partial_spec_ordering_ambiguous)
1899 << ClassTemplateSpec;
1900
1901 // Print the matching partial specializations.
1902 for (llvm::SmallVector<MatchResult, 4>::iterator P = Matched.begin(),
1903 PEnd = Matched.end();
1904 P != PEnd; ++P)
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +00001905 Diag(P->Partial->getLocation(), diag::note_partial_spec_match)
1906 << getTemplateArgumentBindingsText(
1907 P->Partial->getTemplateParameters(),
1908 *P->Args);
Douglas Gregor01afeef2009-08-28 20:31:08 +00001909
Douglas Gregor21610382009-10-29 00:04:11 +00001910 return true;
1911 }
Douglas Gregorbe999392009-09-15 16:23:51 +00001912 }
1913
1914 // Instantiate using the best class template partial specialization.
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +00001915 ClassTemplatePartialSpecializationDecl *OrigPartialSpec = Best->Partial;
Douglas Gregor21610382009-10-29 00:04:11 +00001916 while (OrigPartialSpec->getInstantiatedFromMember()) {
1917 // If we've found an explicit specialization of this class template,
1918 // stop here and use that as the pattern.
1919 if (OrigPartialSpec->isMemberSpecialization())
1920 break;
1921
1922 OrigPartialSpec = OrigPartialSpec->getInstantiatedFromMember();
1923 }
1924
1925 Pattern = OrigPartialSpec;
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +00001926 ClassTemplateSpec->setInstantiationOf(Best->Partial, Best->Args);
Douglas Gregor170bc422009-06-12 22:31:52 +00001927 } else {
1928 // -- If no matches are found, the instantiation is generated
1929 // from the primary template.
Douglas Gregor01afeef2009-08-28 20:31:08 +00001930 ClassTemplateDecl *OrigTemplate = Template;
Douglas Gregorcf915552009-10-13 16:30:37 +00001931 while (OrigTemplate->getInstantiatedFromMemberTemplate()) {
1932 // If we've found an explicit specialization of this class template,
1933 // stop here and use that as the pattern.
1934 if (OrigTemplate->isMemberSpecialization())
1935 break;
1936
Douglas Gregor01afeef2009-08-28 20:31:08 +00001937 OrigTemplate = OrigTemplate->getInstantiatedFromMemberTemplate();
Douglas Gregorcf915552009-10-13 16:30:37 +00001938 }
1939
Douglas Gregor01afeef2009-08-28 20:31:08 +00001940 Pattern = OrigTemplate->getTemplatedDecl();
Douglas Gregor2373c592009-05-31 09:31:02 +00001941 }
Douglas Gregor463421d2009-03-03 04:44:36 +00001942
Douglas Gregoref6ab412009-10-27 06:26:26 +00001943 bool Result = InstantiateClass(PointOfInstantiation, ClassTemplateSpec,
1944 Pattern,
1945 getTemplateInstantiationArgs(ClassTemplateSpec),
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001946 TSK,
Douglas Gregor8a2e6012009-08-24 15:23:48 +00001947 Complain);
Mike Stump11289f42009-09-09 15:08:12 +00001948
Douglas Gregorb7ae10f2009-06-05 00:53:49 +00001949 return Result;
Douglas Gregor463421d2009-03-03 04:44:36 +00001950}
Douglas Gregor90a1a652009-03-19 17:26:29 +00001951
John McCall76d824f2009-08-25 22:02:44 +00001952/// \brief Instantiates the definitions of all of the member
1953/// of the given class, which is an instantiation of a class template
1954/// or a member class of a template.
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00001955void
1956Sema::InstantiateClassMembers(SourceLocation PointOfInstantiation,
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001957 CXXRecordDecl *Instantiation,
1958 const MultiLevelTemplateArgumentList &TemplateArgs,
1959 TemplateSpecializationKind TSK) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001960 for (DeclContext::decl_iterator D = Instantiation->decls_begin(),
1961 DEnd = Instantiation->decls_end();
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00001962 D != DEnd; ++D) {
Douglas Gregor1d957a32009-10-27 18:42:08 +00001963 bool SuppressNew = false;
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00001964 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(*D)) {
Douglas Gregor1d957a32009-10-27 18:42:08 +00001965 if (FunctionDecl *Pattern
1966 = Function->getInstantiatedFromMemberFunction()) {
1967 MemberSpecializationInfo *MSInfo
1968 = Function->getMemberSpecializationInfo();
1969 assert(MSInfo && "No member specialization information?");
Douglas Gregor06aa50412010-04-09 21:02:29 +00001970 if (MSInfo->getTemplateSpecializationKind()
1971 == TSK_ExplicitSpecialization)
1972 continue;
1973
Douglas Gregor1d957a32009-10-27 18:42:08 +00001974 if (CheckSpecializationInstantiationRedecl(PointOfInstantiation, TSK,
1975 Function,
1976 MSInfo->getTemplateSpecializationKind(),
Nico Weberd75488d2010-09-27 21:02:09 +00001977 MSInfo->getPointOfInstantiation(),
Douglas Gregor1d957a32009-10-27 18:42:08 +00001978 SuppressNew) ||
1979 SuppressNew)
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001980 continue;
1981
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001982 if (Function->hasBody())
Douglas Gregor1d957a32009-10-27 18:42:08 +00001983 continue;
1984
1985 if (TSK == TSK_ExplicitInstantiationDefinition) {
1986 // C++0x [temp.explicit]p8:
1987 // An explicit instantiation definition that names a class template
1988 // specialization explicitly instantiates the class template
1989 // specialization and is only an explicit instantiation definition
1990 // of members whose definition is visible at the point of
1991 // instantiation.
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001992 if (!Pattern->hasBody())
Douglas Gregor1d957a32009-10-27 18:42:08 +00001993 continue;
1994
1995 Function->setTemplateSpecializationKind(TSK, PointOfInstantiation);
1996
1997 InstantiateFunctionDefinition(PointOfInstantiation, Function);
1998 } else {
1999 Function->setTemplateSpecializationKind(TSK, PointOfInstantiation);
2000 }
Douglas Gregorbbe8f462009-10-08 15:14:33 +00002001 }
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00002002 } else if (VarDecl *Var = dyn_cast<VarDecl>(*D)) {
Douglas Gregor86d142a2009-10-08 07:24:58 +00002003 if (Var->isStaticDataMember()) {
Douglas Gregor1d957a32009-10-27 18:42:08 +00002004 MemberSpecializationInfo *MSInfo = Var->getMemberSpecializationInfo();
2005 assert(MSInfo && "No member specialization information?");
Douglas Gregor06aa50412010-04-09 21:02:29 +00002006 if (MSInfo->getTemplateSpecializationKind()
2007 == TSK_ExplicitSpecialization)
2008 continue;
2009
Douglas Gregor1d957a32009-10-27 18:42:08 +00002010 if (CheckSpecializationInstantiationRedecl(PointOfInstantiation, TSK,
2011 Var,
2012 MSInfo->getTemplateSpecializationKind(),
Nico Weberd75488d2010-09-27 21:02:09 +00002013 MSInfo->getPointOfInstantiation(),
Douglas Gregor1d957a32009-10-27 18:42:08 +00002014 SuppressNew) ||
2015 SuppressNew)
Douglas Gregorbbe8f462009-10-08 15:14:33 +00002016 continue;
2017
Douglas Gregor1d957a32009-10-27 18:42:08 +00002018 if (TSK == TSK_ExplicitInstantiationDefinition) {
2019 // C++0x [temp.explicit]p8:
2020 // An explicit instantiation definition that names a class template
2021 // specialization explicitly instantiates the class template
2022 // specialization and is only an explicit instantiation definition
2023 // of members whose definition is visible at the point of
2024 // instantiation.
2025 if (!Var->getInstantiatedFromStaticDataMember()
2026 ->getOutOfLineDefinition())
2027 continue;
2028
2029 Var->setTemplateSpecializationKind(TSK, PointOfInstantiation);
Douglas Gregor86d142a2009-10-08 07:24:58 +00002030 InstantiateStaticDataMemberDefinition(PointOfInstantiation, Var);
Douglas Gregor1d957a32009-10-27 18:42:08 +00002031 } else {
2032 Var->setTemplateSpecializationKind(TSK, PointOfInstantiation);
2033 }
2034 }
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00002035 } else if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(*D)) {
Douglas Gregor1da22252010-04-18 18:11:38 +00002036 // Always skip the injected-class-name, along with any
2037 // redeclarations of nested classes, since both would cause us
2038 // to try to instantiate the members of a class twice.
2039 if (Record->isInjectedClassName() || Record->getPreviousDeclaration())
Douglas Gregord801b062009-10-07 23:56:10 +00002040 continue;
2041
Douglas Gregor1d957a32009-10-27 18:42:08 +00002042 MemberSpecializationInfo *MSInfo = Record->getMemberSpecializationInfo();
2043 assert(MSInfo && "No member specialization information?");
Douglas Gregor06aa50412010-04-09 21:02:29 +00002044
2045 if (MSInfo->getTemplateSpecializationKind()
2046 == TSK_ExplicitSpecialization)
2047 continue;
Nico Weberd75488d2010-09-27 21:02:09 +00002048
Douglas Gregor1d957a32009-10-27 18:42:08 +00002049 if (CheckSpecializationInstantiationRedecl(PointOfInstantiation, TSK,
2050 Record,
2051 MSInfo->getTemplateSpecializationKind(),
Nico Weberd75488d2010-09-27 21:02:09 +00002052 MSInfo->getPointOfInstantiation(),
Douglas Gregor1d957a32009-10-27 18:42:08 +00002053 SuppressNew) ||
2054 SuppressNew)
Douglas Gregorbbe8f462009-10-08 15:14:33 +00002055 continue;
2056
Douglas Gregor1d957a32009-10-27 18:42:08 +00002057 CXXRecordDecl *Pattern = Record->getInstantiatedFromMemberClass();
2058 assert(Pattern && "Missing instantiated-from-template information");
2059
Douglas Gregor0a5a2212010-02-11 01:04:33 +00002060 if (!Record->getDefinition()) {
2061 if (!Pattern->getDefinition()) {
Douglas Gregor1d957a32009-10-27 18:42:08 +00002062 // C++0x [temp.explicit]p8:
2063 // An explicit instantiation definition that names a class template
2064 // specialization explicitly instantiates the class template
2065 // specialization and is only an explicit instantiation definition
2066 // of members whose definition is visible at the point of
2067 // instantiation.
2068 if (TSK == TSK_ExplicitInstantiationDeclaration) {
2069 MSInfo->setTemplateSpecializationKind(TSK);
2070 MSInfo->setPointOfInstantiation(PointOfInstantiation);
2071 }
2072
2073 continue;
2074 }
2075
2076 InstantiateClass(PointOfInstantiation, Record, Pattern,
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00002077 TemplateArgs,
2078 TSK);
Nico Weberd75488d2010-09-27 21:02:09 +00002079 } else {
2080 if (TSK == TSK_ExplicitInstantiationDefinition &&
2081 Record->getTemplateSpecializationKind() ==
2082 TSK_ExplicitInstantiationDeclaration) {
2083 Record->setTemplateSpecializationKind(TSK);
2084 MarkVTableUsed(PointOfInstantiation, Record, true);
2085 }
Douglas Gregor1d957a32009-10-27 18:42:08 +00002086 }
Douglas Gregorc093c1d2009-10-08 01:19:17 +00002087
Douglas Gregor0a5a2212010-02-11 01:04:33 +00002088 Pattern = cast_or_null<CXXRecordDecl>(Record->getDefinition());
Douglas Gregor1d957a32009-10-27 18:42:08 +00002089 if (Pattern)
2090 InstantiateClassMembers(PointOfInstantiation, Pattern, TemplateArgs,
2091 TSK);
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00002092 }
2093 }
2094}
2095
2096/// \brief Instantiate the definitions of all of the members of the
2097/// given class template specialization, which was named as part of an
2098/// explicit instantiation.
Mike Stump11289f42009-09-09 15:08:12 +00002099void
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00002100Sema::InstantiateClassTemplateSpecializationMembers(
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00002101 SourceLocation PointOfInstantiation,
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00002102 ClassTemplateSpecializationDecl *ClassTemplateSpec,
2103 TemplateSpecializationKind TSK) {
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00002104 // C++0x [temp.explicit]p7:
2105 // An explicit instantiation that names a class template
2106 // specialization is an explicit instantion of the same kind
2107 // (declaration or definition) of each of its members (not
2108 // including members inherited from base classes) that has not
2109 // been previously explicitly specialized in the translation unit
2110 // containing the explicit instantiation, except as described
2111 // below.
2112 InstantiateClassMembers(PointOfInstantiation, ClassTemplateSpec,
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00002113 getTemplateInstantiationArgs(ClassTemplateSpec),
2114 TSK);
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00002115}
2116
John McCalldadc5752010-08-24 06:29:42 +00002117StmtResult
Douglas Gregor01afeef2009-08-28 20:31:08 +00002118Sema::SubstStmt(Stmt *S, const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregorebe10102009-08-20 07:17:43 +00002119 if (!S)
2120 return Owned(S);
2121
2122 TemplateInstantiator Instantiator(*this, TemplateArgs,
2123 SourceLocation(),
2124 DeclarationName());
2125 return Instantiator.TransformStmt(S);
2126}
2127
John McCalldadc5752010-08-24 06:29:42 +00002128ExprResult
Douglas Gregor01afeef2009-08-28 20:31:08 +00002129Sema::SubstExpr(Expr *E, const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregora16548e2009-08-11 05:31:07 +00002130 if (!E)
2131 return Owned(E);
Mike Stump11289f42009-09-09 15:08:12 +00002132
Douglas Gregora16548e2009-08-11 05:31:07 +00002133 TemplateInstantiator Instantiator(*this, TemplateArgs,
2134 SourceLocation(),
2135 DeclarationName());
2136 return Instantiator.TransformExpr(E);
2137}
2138
Douglas Gregor2cd32a02011-01-07 19:35:17 +00002139bool Sema::SubstExprs(Expr **Exprs, unsigned NumExprs, bool IsCall,
2140 const MultiLevelTemplateArgumentList &TemplateArgs,
2141 llvm::SmallVectorImpl<Expr *> &Outputs) {
2142 if (NumExprs == 0)
2143 return false;
2144
2145 TemplateInstantiator Instantiator(*this, TemplateArgs,
2146 SourceLocation(),
2147 DeclarationName());
2148 return Instantiator.TransformExprs(Exprs, NumExprs, IsCall, Outputs);
2149}
2150
Douglas Gregor14454802011-02-25 02:25:35 +00002151NestedNameSpecifierLoc
2152Sema::SubstNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS,
2153 const MultiLevelTemplateArgumentList &TemplateArgs) {
2154 if (!NNS)
2155 return NestedNameSpecifierLoc();
2156
2157 TemplateInstantiator Instantiator(*this, TemplateArgs, NNS.getBeginLoc(),
2158 DeclarationName());
2159 return Instantiator.TransformNestedNameSpecifierLoc(NNS);
2160}
2161
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002162/// \brief Do template substitution on declaration name info.
2163DeclarationNameInfo
2164Sema::SubstDeclarationNameInfo(const DeclarationNameInfo &NameInfo,
2165 const MultiLevelTemplateArgumentList &TemplateArgs) {
2166 TemplateInstantiator Instantiator(*this, TemplateArgs, NameInfo.getLoc(),
2167 NameInfo.getName());
2168 return Instantiator.TransformDeclarationNameInfo(NameInfo);
2169}
2170
Douglas Gregoraa594892009-03-31 18:38:02 +00002171TemplateName
Douglas Gregordf846d12011-03-02 18:46:51 +00002172Sema::SubstTemplateName(NestedNameSpecifierLoc QualifierLoc,
2173 TemplateName Name, SourceLocation Loc,
Douglas Gregor01afeef2009-08-28 20:31:08 +00002174 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor71dc5092009-08-06 06:41:21 +00002175 TemplateInstantiator Instantiator(*this, TemplateArgs, Loc,
2176 DeclarationName());
Douglas Gregordf846d12011-03-02 18:46:51 +00002177 CXXScopeSpec SS;
2178 SS.Adopt(QualifierLoc);
2179 return Instantiator.TransformTemplateName(SS, Name, Loc);
Douglas Gregoraa594892009-03-31 18:38:02 +00002180}
Douglas Gregorc43620d2009-06-11 00:06:24 +00002181
Douglas Gregor0f3feb42010-12-22 21:19:48 +00002182bool Sema::Subst(const TemplateArgumentLoc *Args, unsigned NumArgs,
2183 TemplateArgumentListInfo &Result,
John McCall0ad16662009-10-29 08:12:44 +00002184 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregore922c772009-08-04 22:27:00 +00002185 TemplateInstantiator Instantiator(*this, TemplateArgs, SourceLocation(),
2186 DeclarationName());
Douglas Gregor0f3feb42010-12-22 21:19:48 +00002187
2188 return Instantiator.TransformTemplateArguments(Args, NumArgs, Result);
Douglas Gregorc43620d2009-06-11 00:06:24 +00002189}
Douglas Gregor14cf7522010-04-30 18:55:50 +00002190
Douglas Gregorf3010112011-01-07 16:43:16 +00002191llvm::PointerUnion<Decl *, LocalInstantiationScope::DeclArgumentPack *> *
2192LocalInstantiationScope::findInstantiationOf(const Decl *D) {
Chris Lattnercab02a62011-02-17 20:34:02 +00002193 for (LocalInstantiationScope *Current = this; Current;
Douglas Gregor14cf7522010-04-30 18:55:50 +00002194 Current = Current->Outer) {
Chris Lattnercab02a62011-02-17 20:34:02 +00002195
Douglas Gregor14cf7522010-04-30 18:55:50 +00002196 // Check if we found something within this scope.
Douglas Gregore9fc8dc2010-12-21 21:22:51 +00002197 const Decl *CheckD = D;
2198 do {
Douglas Gregorf3010112011-01-07 16:43:16 +00002199 LocalDeclsMap::iterator Found = Current->LocalDecls.find(CheckD);
Douglas Gregore9fc8dc2010-12-21 21:22:51 +00002200 if (Found != Current->LocalDecls.end())
Douglas Gregorf3010112011-01-07 16:43:16 +00002201 return &Found->second;
Douglas Gregore9fc8dc2010-12-21 21:22:51 +00002202
2203 // If this is a tag declaration, it's possible that we need to look for
2204 // a previous declaration.
2205 if (const TagDecl *Tag = dyn_cast<TagDecl>(CheckD))
2206 CheckD = Tag->getPreviousDeclaration();
2207 else
2208 CheckD = 0;
2209 } while (CheckD);
2210
Douglas Gregor14cf7522010-04-30 18:55:50 +00002211 // If we aren't combined with our outer scope, we're done.
2212 if (!Current->CombineWithOuterScope)
2213 break;
2214 }
Chris Lattnercab02a62011-02-17 20:34:02 +00002215
2216 // If we didn't find the decl, then we either have a sema bug, or we have a
2217 // forward reference to a label declaration. Return null to indicate that
2218 // we have an uninstantiated label.
2219 assert(isa<LabelDecl>(D) && "declaration not instantiated in this scope");
Douglas Gregor14cf7522010-04-30 18:55:50 +00002220 return 0;
2221}
2222
John McCall19c1bfd2010-08-25 05:32:35 +00002223void LocalInstantiationScope::InstantiatedLocal(const Decl *D, Decl *Inst) {
Douglas Gregorf3010112011-01-07 16:43:16 +00002224 llvm::PointerUnion<Decl *, DeclArgumentPack *> &Stored = LocalDecls[D];
Douglas Gregora8bac7f2011-01-10 07:32:04 +00002225 if (Stored.isNull())
2226 Stored = Inst;
2227 else if (Stored.is<Decl *>()) {
2228 assert(Stored.get<Decl *>() == Inst && "Already instantiated this local");
2229 Stored = Inst;
2230 } else
2231 LocalDecls[D].get<DeclArgumentPack *>()->push_back(Inst);
Douglas Gregor14cf7522010-04-30 18:55:50 +00002232}
Douglas Gregorf3010112011-01-07 16:43:16 +00002233
2234void LocalInstantiationScope::InstantiatedLocalPackArg(const Decl *D,
2235 Decl *Inst) {
2236 DeclArgumentPack *Pack = LocalDecls[D].get<DeclArgumentPack *>();
2237 Pack->push_back(Inst);
2238}
2239
2240void LocalInstantiationScope::MakeInstantiatedLocalArgPack(const Decl *D) {
2241 llvm::PointerUnion<Decl *, DeclArgumentPack *> &Stored = LocalDecls[D];
2242 assert(Stored.isNull() && "Already instantiated this local");
2243 DeclArgumentPack *Pack = new DeclArgumentPack;
2244 Stored = Pack;
2245 ArgumentPacks.push_back(Pack);
2246}
2247
Douglas Gregora8bac7f2011-01-10 07:32:04 +00002248void LocalInstantiationScope::SetPartiallySubstitutedPack(NamedDecl *Pack,
2249 const TemplateArgument *ExplicitArgs,
2250 unsigned NumExplicitArgs) {
2251 assert((!PartiallySubstitutedPack || PartiallySubstitutedPack == Pack) &&
2252 "Already have a partially-substituted pack");
2253 assert((!PartiallySubstitutedPack
2254 || NumArgsInPartiallySubstitutedPack == NumExplicitArgs) &&
2255 "Wrong number of arguments in partially-substituted pack");
2256 PartiallySubstitutedPack = Pack;
2257 ArgsInPartiallySubstitutedPack = ExplicitArgs;
2258 NumArgsInPartiallySubstitutedPack = NumExplicitArgs;
2259}
2260
2261NamedDecl *LocalInstantiationScope::getPartiallySubstitutedPack(
2262 const TemplateArgument **ExplicitArgs,
2263 unsigned *NumExplicitArgs) const {
2264 if (ExplicitArgs)
2265 *ExplicitArgs = 0;
2266 if (NumExplicitArgs)
2267 *NumExplicitArgs = 0;
2268
2269 for (const LocalInstantiationScope *Current = this; Current;
2270 Current = Current->Outer) {
2271 if (Current->PartiallySubstitutedPack) {
2272 if (ExplicitArgs)
2273 *ExplicitArgs = Current->ArgsInPartiallySubstitutedPack;
2274 if (NumExplicitArgs)
2275 *NumExplicitArgs = Current->NumArgsInPartiallySubstitutedPack;
2276
2277 return Current->PartiallySubstitutedPack;
2278 }
2279
2280 if (!Current->CombineWithOuterScope)
2281 break;
2282 }
2283
2284 return 0;
2285}