blob: 3e1e735c85b5f4654991b5907ce19c477c7d37ef [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;
Richard Smith3f1b5d02011-05-05 21:57:07 +0000449 } else if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
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)
Richard Smith3f1b5d02011-05-05 21:57:07 +0000452 << VD
453 << Active->InstantiationRange;
454 } else {
455 Diags.Report(Active->PointOfInstantiation,
456 diag::note_template_type_alias_instantiation_here)
457 << cast<TypeAliasTemplateDecl>(D)
Douglas Gregora6ef8f02009-07-24 20:34:43 +0000458 << Active->InstantiationRange;
Douglas Gregor85673582009-05-18 17:01:57 +0000459 }
Douglas Gregor79cf6032009-03-10 20:44:00 +0000460 break;
461 }
462
463 case ActiveTemplateInstantiation::DefaultTemplateArgumentInstantiation: {
464 TemplateDecl *Template = cast<TemplateDecl>((Decl *)Active->Entity);
465 std::string TemplateArgsStr
Douglas Gregordc572a32009-03-30 22:58:21 +0000466 = TemplateSpecializationType::PrintTemplateArgumentList(
Mike Stump11289f42009-09-09 15:08:12 +0000467 Active->TemplateArgs,
Douglas Gregor7de59662009-05-29 20:38:28 +0000468 Active->NumTemplateArgs,
469 Context.PrintingPolicy);
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000470 Diags.Report(Active->PointOfInstantiation,
Douglas Gregor79cf6032009-03-10 20:44:00 +0000471 diag::note_default_arg_instantiation_here)
472 << (Template->getNameAsString() + TemplateArgsStr)
473 << Active->InstantiationRange;
474 break;
475 }
Douglas Gregor637d9982009-06-10 23:47:09 +0000476
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000477 case ActiveTemplateInstantiation::ExplicitTemplateArgumentSubstitution: {
Mike Stump11289f42009-09-09 15:08:12 +0000478 FunctionTemplateDecl *FnTmpl
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000479 = cast<FunctionTemplateDecl>((Decl *)Active->Entity);
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000480 Diags.Report(Active->PointOfInstantiation,
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000481 diag::note_explicit_template_arg_substitution_here)
Douglas Gregor607f1412010-03-30 20:35:20 +0000482 << FnTmpl
483 << getTemplateArgumentBindingsText(FnTmpl->getTemplateParameters(),
484 Active->TemplateArgs,
485 Active->NumTemplateArgs)
486 << Active->InstantiationRange;
Douglas Gregor637d9982009-06-10 23:47:09 +0000487 break;
488 }
Mike Stump11289f42009-09-09 15:08:12 +0000489
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000490 case ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution:
491 if (ClassTemplatePartialSpecializationDecl *PartialSpec
492 = dyn_cast<ClassTemplatePartialSpecializationDecl>(
493 (Decl *)Active->Entity)) {
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000494 Diags.Report(Active->PointOfInstantiation,
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000495 diag::note_partial_spec_deduct_instantiation_here)
496 << Context.getTypeDeclType(PartialSpec)
Douglas Gregor607f1412010-03-30 20:35:20 +0000497 << getTemplateArgumentBindingsText(
498 PartialSpec->getTemplateParameters(),
499 Active->TemplateArgs,
500 Active->NumTemplateArgs)
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000501 << Active->InstantiationRange;
502 } else {
503 FunctionTemplateDecl *FnTmpl
504 = cast<FunctionTemplateDecl>((Decl *)Active->Entity);
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000505 Diags.Report(Active->PointOfInstantiation,
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000506 diag::note_function_template_deduction_instantiation_here)
Douglas Gregor607f1412010-03-30 20:35:20 +0000507 << FnTmpl
508 << getTemplateArgumentBindingsText(FnTmpl->getTemplateParameters(),
509 Active->TemplateArgs,
510 Active->NumTemplateArgs)
511 << Active->InstantiationRange;
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000512 }
513 break;
Douglas Gregor637d9982009-06-10 23:47:09 +0000514
Anders Carlsson657bad42009-09-05 05:14:19 +0000515 case ActiveTemplateInstantiation::DefaultFunctionArgumentInstantiation: {
516 ParmVarDecl *Param = cast<ParmVarDecl>((Decl *)Active->Entity);
517 FunctionDecl *FD = cast<FunctionDecl>(Param->getDeclContext());
Mike Stump11289f42009-09-09 15:08:12 +0000518
Anders Carlsson657bad42009-09-05 05:14:19 +0000519 std::string TemplateArgsStr
520 = TemplateSpecializationType::PrintTemplateArgumentList(
Mike Stump11289f42009-09-09 15:08:12 +0000521 Active->TemplateArgs,
Anders Carlsson657bad42009-09-05 05:14:19 +0000522 Active->NumTemplateArgs,
523 Context.PrintingPolicy);
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000524 Diags.Report(Active->PointOfInstantiation,
Anders Carlsson657bad42009-09-05 05:14:19 +0000525 diag::note_default_function_arg_instantiation_here)
Anders Carlssondc6d2c32009-09-05 05:38:54 +0000526 << (FD->getNameAsString() + TemplateArgsStr)
Anders Carlsson657bad42009-09-05 05:14:19 +0000527 << Active->InstantiationRange;
528 break;
529 }
Mike Stump11289f42009-09-09 15:08:12 +0000530
Douglas Gregore62e6a02009-11-11 19:13:48 +0000531 case ActiveTemplateInstantiation::PriorTemplateArgumentSubstitution: {
532 NamedDecl *Parm = cast<NamedDecl>((Decl *)Active->Entity);
533 std::string Name;
534 if (!Parm->getName().empty())
535 Name = std::string(" '") + Parm->getName().str() + "'";
Douglas Gregorca4686d2011-01-04 23:35:54 +0000536
537 TemplateParameterList *TemplateParams = 0;
538 if (TemplateDecl *Template = dyn_cast<TemplateDecl>(Active->Template))
539 TemplateParams = Template->getTemplateParameters();
540 else
541 TemplateParams =
542 cast<ClassTemplatePartialSpecializationDecl>(Active->Template)
543 ->getTemplateParameters();
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000544 Diags.Report(Active->PointOfInstantiation,
Douglas Gregore62e6a02009-11-11 19:13:48 +0000545 diag::note_prior_template_arg_substitution)
546 << isa<TemplateTemplateParmDecl>(Parm)
547 << Name
Douglas Gregorca4686d2011-01-04 23:35:54 +0000548 << getTemplateArgumentBindingsText(TemplateParams,
Douglas Gregore62e6a02009-11-11 19:13:48 +0000549 Active->TemplateArgs,
550 Active->NumTemplateArgs)
551 << Active->InstantiationRange;
552 break;
553 }
Douglas Gregor84d49a22009-11-11 21:54:23 +0000554
555 case ActiveTemplateInstantiation::DefaultTemplateArgumentChecking: {
Douglas Gregorca4686d2011-01-04 23:35:54 +0000556 TemplateParameterList *TemplateParams = 0;
557 if (TemplateDecl *Template = dyn_cast<TemplateDecl>(Active->Template))
558 TemplateParams = Template->getTemplateParameters();
559 else
560 TemplateParams =
561 cast<ClassTemplatePartialSpecializationDecl>(Active->Template)
562 ->getTemplateParameters();
563
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000564 Diags.Report(Active->PointOfInstantiation,
Douglas Gregor84d49a22009-11-11 21:54:23 +0000565 diag::note_template_default_arg_checking)
Douglas Gregorca4686d2011-01-04 23:35:54 +0000566 << getTemplateArgumentBindingsText(TemplateParams,
Douglas Gregor84d49a22009-11-11 21:54:23 +0000567 Active->TemplateArgs,
568 Active->NumTemplateArgs)
569 << Active->InstantiationRange;
570 break;
571 }
Douglas Gregor79cf6032009-03-10 20:44:00 +0000572 }
Douglas Gregor4ea568f2009-03-10 18:03:33 +0000573 }
574}
575
Douglas Gregoredb76852011-01-27 22:31:44 +0000576llvm::Optional<TemplateDeductionInfo *> Sema::isSFINAEContext() const {
Douglas Gregor33834512009-06-14 07:33:30 +0000577 using llvm::SmallVector;
Douglas Gregoredb76852011-01-27 22:31:44 +0000578 if (InNonInstantiationSFINAEContext)
579 return llvm::Optional<TemplateDeductionInfo *>(0);
580
Douglas Gregor33834512009-06-14 07:33:30 +0000581 for (SmallVector<ActiveTemplateInstantiation, 16>::const_reverse_iterator
582 Active = ActiveTemplateInstantiations.rbegin(),
583 ActiveEnd = ActiveTemplateInstantiations.rend();
584 Active != ActiveEnd;
Douglas Gregor84d49a22009-11-11 21:54:23 +0000585 ++Active)
586 {
Douglas Gregor33834512009-06-14 07:33:30 +0000587 switch(Active->Kind) {
Anders Carlsson657bad42009-09-05 05:14:19 +0000588 case ActiveTemplateInstantiation::DefaultFunctionArgumentInstantiation:
Douglas Gregoredb76852011-01-27 22:31:44 +0000589 case ActiveTemplateInstantiation::TemplateInstantiation:
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000590 // This is a template instantiation, so there is no SFINAE.
Douglas Gregoredb76852011-01-27 22:31:44 +0000591 return llvm::Optional<TemplateDeductionInfo *>();
Mike Stump11289f42009-09-09 15:08:12 +0000592
Douglas Gregor33834512009-06-14 07:33:30 +0000593 case ActiveTemplateInstantiation::DefaultTemplateArgumentInstantiation:
Douglas Gregore62e6a02009-11-11 19:13:48 +0000594 case ActiveTemplateInstantiation::PriorTemplateArgumentSubstitution:
Douglas Gregor84d49a22009-11-11 21:54:23 +0000595 case ActiveTemplateInstantiation::DefaultTemplateArgumentChecking:
Douglas Gregore62e6a02009-11-11 19:13:48 +0000596 // A default template argument instantiation and substitution into
597 // template parameters with arguments for prior parameters may or may
598 // not be a SFINAE context; look further up the stack.
Douglas Gregor33834512009-06-14 07:33:30 +0000599 break;
Mike Stump11289f42009-09-09 15:08:12 +0000600
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000601 case ActiveTemplateInstantiation::ExplicitTemplateArgumentSubstitution:
602 case ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution:
603 // We're either substitution explicitly-specified template arguments
604 // or deduced template arguments, so SFINAE applies.
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +0000605 assert(Active->DeductionInfo && "Missing deduction info pointer");
606 return Active->DeductionInfo;
Douglas Gregor33834512009-06-14 07:33:30 +0000607 }
608 }
609
Douglas Gregoredb76852011-01-27 22:31:44 +0000610 return llvm::Optional<TemplateDeductionInfo *>();
Douglas Gregor33834512009-06-14 07:33:30 +0000611}
612
Douglas Gregora8bac7f2011-01-10 07:32:04 +0000613/// \brief Retrieve the depth and index of a parameter pack.
614static std::pair<unsigned, unsigned>
615getDepthAndIndex(NamedDecl *ND) {
616 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(ND))
617 return std::make_pair(TTP->getDepth(), TTP->getIndex());
618
619 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(ND))
620 return std::make_pair(NTTP->getDepth(), NTTP->getIndex());
621
622 TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(ND);
623 return std::make_pair(TTP->getDepth(), TTP->getIndex());
624}
625
Douglas Gregorfe1e1102009-02-27 19:31:52 +0000626//===----------------------------------------------------------------------===/
627// Template Instantiation for Types
628//===----------------------------------------------------------------------===/
Douglas Gregor17c0d7b2009-02-28 00:25:32 +0000629namespace {
Douglas Gregor14cf7522010-04-30 18:55:50 +0000630 class TemplateInstantiator : public TreeTransform<TemplateInstantiator> {
Douglas Gregor01afeef2009-08-28 20:31:08 +0000631 const MultiLevelTemplateArgumentList &TemplateArgs;
Douglas Gregor17c0d7b2009-02-28 00:25:32 +0000632 SourceLocation Loc;
633 DeclarationName Entity;
Douglas Gregorfe1e1102009-02-27 19:31:52 +0000634
Douglas Gregor17c0d7b2009-02-28 00:25:32 +0000635 public:
Douglas Gregorebe10102009-08-20 07:17:43 +0000636 typedef TreeTransform<TemplateInstantiator> inherited;
Mike Stump11289f42009-09-09 15:08:12 +0000637
638 TemplateInstantiator(Sema &SemaRef,
Douglas Gregor01afeef2009-08-28 20:31:08 +0000639 const MultiLevelTemplateArgumentList &TemplateArgs,
Douglas Gregord6ff3322009-08-04 16:50:30 +0000640 SourceLocation Loc,
Mike Stump11289f42009-09-09 15:08:12 +0000641 DeclarationName Entity)
642 : inherited(SemaRef), TemplateArgs(TemplateArgs), Loc(Loc),
Douglas Gregorebe10102009-08-20 07:17:43 +0000643 Entity(Entity) { }
Douglas Gregor17c0d7b2009-02-28 00:25:32 +0000644
Mike Stump11289f42009-09-09 15:08:12 +0000645 /// \brief Determine whether the given type \p T has already been
Douglas Gregord6ff3322009-08-04 16:50:30 +0000646 /// transformed.
647 ///
648 /// For the purposes of template instantiation, a type has already been
649 /// transformed if it is NULL or if it is not dependent.
Douglas Gregor5597ab42010-05-07 23:12:07 +0000650 bool AlreadyTransformed(QualType T);
Mike Stump11289f42009-09-09 15:08:12 +0000651
Douglas Gregord6ff3322009-08-04 16:50:30 +0000652 /// \brief Returns the location of the entity being instantiated, if known.
653 SourceLocation getBaseLocation() { return Loc; }
Mike Stump11289f42009-09-09 15:08:12 +0000654
Douglas Gregord6ff3322009-08-04 16:50:30 +0000655 /// \brief Returns the name of the entity being instantiated, if any.
656 DeclarationName getBaseEntity() { return Entity; }
Mike Stump11289f42009-09-09 15:08:12 +0000657
Douglas Gregoref6ab412009-10-27 06:26:26 +0000658 /// \brief Sets the "base" location and entity when that
659 /// information is known based on another transformation.
660 void setBase(SourceLocation Loc, DeclarationName Entity) {
661 this->Loc = Loc;
662 this->Entity = Entity;
663 }
Douglas Gregor840bd6c2010-12-20 22:05:00 +0000664
665 bool TryExpandParameterPacks(SourceLocation EllipsisLoc,
666 SourceRange PatternRange,
667 const UnexpandedParameterPack *Unexpanded,
668 unsigned NumUnexpanded,
669 bool &ShouldExpand,
Douglas Gregora8bac7f2011-01-10 07:32:04 +0000670 bool &RetainExpansion,
Douglas Gregor0dca5fd2011-01-14 17:04:44 +0000671 llvm::Optional<unsigned> &NumExpansions) {
Douglas Gregor76aca7b2010-12-21 00:52:54 +0000672 return getSema().CheckParameterPacksForExpansion(EllipsisLoc,
673 PatternRange, Unexpanded,
674 NumUnexpanded,
675 TemplateArgs,
676 ShouldExpand,
Douglas Gregora8bac7f2011-01-10 07:32:04 +0000677 RetainExpansion,
Douglas Gregor76aca7b2010-12-21 00:52:54 +0000678 NumExpansions);
679 }
Douglas Gregor840bd6c2010-12-20 22:05:00 +0000680
Douglas Gregorf3010112011-01-07 16:43:16 +0000681 void ExpandingFunctionParameterPack(ParmVarDecl *Pack) {
682 SemaRef.CurrentInstantiationScope->MakeInstantiatedLocalArgPack(Pack);
683 }
684
Douglas Gregora8bac7f2011-01-10 07:32:04 +0000685 TemplateArgument ForgetPartiallySubstitutedPack() {
686 TemplateArgument Result;
687 if (NamedDecl *PartialPack
688 = SemaRef.CurrentInstantiationScope->getPartiallySubstitutedPack()){
689 MultiLevelTemplateArgumentList &TemplateArgs
690 = const_cast<MultiLevelTemplateArgumentList &>(this->TemplateArgs);
691 unsigned Depth, Index;
692 llvm::tie(Depth, Index) = getDepthAndIndex(PartialPack);
693 if (TemplateArgs.hasTemplateArgument(Depth, Index)) {
694 Result = TemplateArgs(Depth, Index);
695 TemplateArgs.setArgument(Depth, Index, TemplateArgument());
696 }
697 }
698
699 return Result;
700 }
701
702 void RememberPartiallySubstitutedPack(TemplateArgument Arg) {
703 if (Arg.isNull())
704 return;
705
706 if (NamedDecl *PartialPack
707 = SemaRef.CurrentInstantiationScope->getPartiallySubstitutedPack()){
708 MultiLevelTemplateArgumentList &TemplateArgs
709 = const_cast<MultiLevelTemplateArgumentList &>(this->TemplateArgs);
710 unsigned Depth, Index;
711 llvm::tie(Depth, Index) = getDepthAndIndex(PartialPack);
712 TemplateArgs.setArgument(Depth, Index, Arg);
713 }
714 }
715
Douglas Gregord6ff3322009-08-04 16:50:30 +0000716 /// \brief Transform the given declaration by instantiating a reference to
717 /// this declaration.
Douglas Gregora04f2ca2010-03-01 15:56:25 +0000718 Decl *TransformDecl(SourceLocation Loc, Decl *D);
Douglas Gregora16548e2009-08-11 05:31:07 +0000719
Mike Stump11289f42009-09-09 15:08:12 +0000720 /// \brief Transform the definition of the given declaration by
Douglas Gregorebe10102009-08-20 07:17:43 +0000721 /// instantiating it.
Douglas Gregor25289362010-03-01 17:25:41 +0000722 Decl *TransformDefinition(SourceLocation Loc, Decl *D);
Mike Stump11289f42009-09-09 15:08:12 +0000723
Douglas Gregora5cb6da2009-10-20 05:58:46 +0000724 /// \bried Transform the first qualifier within a scope by instantiating the
725 /// declaration.
726 NamedDecl *TransformFirstQualifierInScope(NamedDecl *D, SourceLocation Loc);
727
Douglas Gregorebe10102009-08-20 07:17:43 +0000728 /// \brief Rebuild the exception declaration and register the declaration
729 /// as an instantiated local.
Douglas Gregor9f0e1aa2010-09-09 17:09:21 +0000730 VarDecl *RebuildExceptionDecl(VarDecl *ExceptionDecl,
John McCallbcd03502009-12-07 02:54:59 +0000731 TypeSourceInfo *Declarator,
Abramo Bagnaradff19302011-03-08 08:55:46 +0000732 SourceLocation StartLoc,
733 SourceLocation NameLoc,
734 IdentifierInfo *Name);
Mike Stump11289f42009-09-09 15:08:12 +0000735
Douglas Gregorf4e837f2010-04-26 17:57:08 +0000736 /// \brief Rebuild the Objective-C exception declaration and register the
737 /// declaration as an instantiated local.
738 VarDecl *RebuildObjCExceptionDecl(VarDecl *ExceptionDecl,
739 TypeSourceInfo *TSInfo, QualType T);
740
John McCall7f41d982009-09-11 04:59:25 +0000741 /// \brief Check for tag mismatches when instantiating an
742 /// elaborated type.
John McCall954b5de2010-11-04 19:04:38 +0000743 QualType RebuildElaboratedType(SourceLocation KeywordLoc,
744 ElaboratedTypeKeyword Keyword,
Douglas Gregor844cb502011-03-01 18:12:44 +0000745 NestedNameSpecifierLoc QualifierLoc,
746 QualType T);
John McCall7f41d982009-09-11 04:59:25 +0000747
Douglas Gregor9db53502011-03-02 18:07:45 +0000748 TemplateName TransformTemplateName(CXXScopeSpec &SS,
749 TemplateName Name,
750 SourceLocation NameLoc,
751 QualType ObjectType = QualType(),
752 NamedDecl *FirstQualifierInScope = 0);
753
John McCalldadc5752010-08-24 06:29:42 +0000754 ExprResult TransformPredefinedExpr(PredefinedExpr *E);
755 ExprResult TransformDeclRefExpr(DeclRefExpr *E);
756 ExprResult TransformCXXDefaultArgExpr(CXXDefaultArgExpr *E);
757 ExprResult TransformTemplateParmRefExpr(DeclRefExpr *E,
Douglas Gregoreb5a39d2010-12-24 00:15:10 +0000758 NonTypeTemplateParmDecl *D);
Douglas Gregorcdbc5392011-01-15 01:15:58 +0000759 ExprResult TransformSubstNonTypeTemplateParmPackExpr(
760 SubstNonTypeTemplateParmPackExpr *E);
761
Douglas Gregor14cf7522010-04-30 18:55:50 +0000762 QualType TransformFunctionProtoType(TypeLocBuilder &TLB,
John McCall31f82722010-11-12 08:19:04 +0000763 FunctionProtoTypeLoc TL);
Douglas Gregor715e4612011-01-14 22:40:04 +0000764 ParmVarDecl *TransformFunctionTypeParam(ParmVarDecl *OldParm,
John McCall8fb0d9d2011-05-01 22:35:37 +0000765 int indexAdjustment,
Douglas Gregor715e4612011-01-14 22:40:04 +0000766 llvm::Optional<unsigned> NumExpansions);
John McCall58f10c32010-03-11 09:03:00 +0000767
Mike Stump11289f42009-09-09 15:08:12 +0000768 /// \brief Transforms a template type parameter type by performing
Douglas Gregord6ff3322009-08-04 16:50:30 +0000769 /// substitution of the corresponding template type argument.
John McCall550e0c22009-10-21 00:40:46 +0000770 QualType TransformTemplateTypeParmType(TypeLocBuilder &TLB,
John McCall31f82722010-11-12 08:19:04 +0000771 TemplateTypeParmTypeLoc TL);
Nick Lewyckyc96c37f2010-07-06 19:51:49 +0000772
Douglas Gregorada4b792011-01-14 02:55:32 +0000773 /// \brief Transforms an already-substituted template type parameter pack
774 /// into either itself (if we aren't substituting into its pack expansion)
775 /// or the appropriate substituted argument.
776 QualType TransformSubstTemplateTypeParmPackType(TypeLocBuilder &TLB,
777 SubstTemplateTypeParmPackTypeLoc TL);
778
John McCalldadc5752010-08-24 06:29:42 +0000779 ExprResult TransformCallExpr(CallExpr *CE) {
Nick Lewyckyc96c37f2010-07-06 19:51:49 +0000780 getSema().CallsUndergoingInstantiation.push_back(CE);
John McCalldadc5752010-08-24 06:29:42 +0000781 ExprResult Result =
Nick Lewyckyc96c37f2010-07-06 19:51:49 +0000782 TreeTransform<TemplateInstantiator>::TransformCallExpr(CE);
783 getSema().CallsUndergoingInstantiation.pop_back();
784 return move(Result);
785 }
Douglas Gregord6ff3322009-08-04 16:50:30 +0000786 };
Douglas Gregor04318252009-07-06 15:59:29 +0000787}
788
Douglas Gregor5597ab42010-05-07 23:12:07 +0000789bool TemplateInstantiator::AlreadyTransformed(QualType T) {
790 if (T.isNull())
791 return true;
792
Douglas Gregor5a5073e2010-05-24 17:22:01 +0000793 if (T->isDependentType() || T->isVariablyModifiedType())
Douglas Gregor5597ab42010-05-07 23:12:07 +0000794 return false;
795
796 getSema().MarkDeclarationsReferencedInType(Loc, T);
797 return true;
798}
799
Douglas Gregora04f2ca2010-03-01 15:56:25 +0000800Decl *TemplateInstantiator::TransformDecl(SourceLocation Loc, Decl *D) {
Douglas Gregor2b6ca462009-09-03 21:38:09 +0000801 if (!D)
802 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000803
Douglas Gregor2b6ca462009-09-03 21:38:09 +0000804 if (TemplateTemplateParmDecl *TTP = dyn_cast<TemplateTemplateParmDecl>(D)) {
Douglas Gregor01afeef2009-08-28 20:31:08 +0000805 if (TTP->getDepth() < TemplateArgs.getNumLevels()) {
Douglas Gregorb93971082010-02-05 19:54:12 +0000806 // If the corresponding template argument is NULL or non-existent, it's
807 // because we are performing instantiation from explicitly-specified
808 // template arguments in a function template, but there were some
809 // arguments left unspecified.
810 if (!TemplateArgs.hasTemplateArgument(TTP->getDepth(),
811 TTP->getPosition()))
812 return D;
813
Douglas Gregorf5500772011-01-05 15:48:55 +0000814 TemplateArgument Arg = TemplateArgs(TTP->getDepth(), TTP->getPosition());
815
816 if (TTP->isParameterPack()) {
817 assert(Arg.getKind() == TemplateArgument::Pack &&
818 "Missing argument pack");
819
Douglas Gregor5590be02011-01-15 06:45:20 +0000820 assert(getSema().ArgumentPackSubstitutionIndex >= 0);
Douglas Gregora8bac7f2011-01-10 07:32:04 +0000821 assert(getSema().ArgumentPackSubstitutionIndex < (int)Arg.pack_size());
Douglas Gregorf5500772011-01-05 15:48:55 +0000822 Arg = Arg.pack_begin()[getSema().ArgumentPackSubstitutionIndex];
823 }
824
825 TemplateName Template = Arg.getAsTemplate();
Douglas Gregor9167f8b2009-11-11 01:00:40 +0000826 assert(!Template.isNull() && Template.getAsTemplateDecl() &&
Douglas Gregor01afeef2009-08-28 20:31:08 +0000827 "Wrong kind of template template argument");
Douglas Gregor9167f8b2009-11-11 01:00:40 +0000828 return Template.getAsTemplateDecl();
Douglas Gregor01afeef2009-08-28 20:31:08 +0000829 }
Mike Stump11289f42009-09-09 15:08:12 +0000830
Douglas Gregor9167f8b2009-11-11 01:00:40 +0000831 // Fall through to find the instantiated declaration for this template
832 // template parameter.
Douglas Gregor71dc5092009-08-06 06:41:21 +0000833 }
Mike Stump11289f42009-09-09 15:08:12 +0000834
Douglas Gregora04f2ca2010-03-01 15:56:25 +0000835 return SemaRef.FindInstantiatedDecl(Loc, cast<NamedDecl>(D), TemplateArgs);
Douglas Gregord6ff3322009-08-04 16:50:30 +0000836}
837
Douglas Gregor25289362010-03-01 17:25:41 +0000838Decl *TemplateInstantiator::TransformDefinition(SourceLocation Loc, Decl *D) {
John McCall76d824f2009-08-25 22:02:44 +0000839 Decl *Inst = getSema().SubstDecl(D, getSema().CurContext, TemplateArgs);
Douglas Gregorebe10102009-08-20 07:17:43 +0000840 if (!Inst)
841 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000842
Douglas Gregorebe10102009-08-20 07:17:43 +0000843 getSema().CurrentInstantiationScope->InstantiatedLocal(D, Inst);
844 return Inst;
845}
846
Douglas Gregora5cb6da2009-10-20 05:58:46 +0000847NamedDecl *
848TemplateInstantiator::TransformFirstQualifierInScope(NamedDecl *D,
849 SourceLocation Loc) {
850 // If the first part of the nested-name-specifier was a template type
851 // parameter, instantiate that type parameter down to a tag type.
852 if (TemplateTypeParmDecl *TTPD = dyn_cast_or_null<TemplateTypeParmDecl>(D)) {
853 const TemplateTypeParmType *TTP
854 = cast<TemplateTypeParmType>(getSema().Context.getTypeDeclType(TTPD));
Douglas Gregor53c3f4e2010-12-20 22:48:17 +0000855
Douglas Gregora5cb6da2009-10-20 05:58:46 +0000856 if (TTP->getDepth() < TemplateArgs.getNumLevels()) {
Douglas Gregor53c3f4e2010-12-20 22:48:17 +0000857 // FIXME: This needs testing w/ member access expressions.
858 TemplateArgument Arg = TemplateArgs(TTP->getDepth(), TTP->getIndex());
859
860 if (TTP->isParameterPack()) {
861 assert(Arg.getKind() == TemplateArgument::Pack &&
862 "Missing argument pack");
863
Douglas Gregore1d60df2011-01-14 23:41:42 +0000864 if (getSema().ArgumentPackSubstitutionIndex == -1)
Douglas Gregor53c3f4e2010-12-20 22:48:17 +0000865 return 0;
Douglas Gregor53c3f4e2010-12-20 22:48:17 +0000866
Douglas Gregora8bac7f2011-01-10 07:32:04 +0000867 assert(getSema().ArgumentPackSubstitutionIndex < (int)Arg.pack_size());
Douglas Gregor53c3f4e2010-12-20 22:48:17 +0000868 Arg = Arg.pack_begin()[getSema().ArgumentPackSubstitutionIndex];
869 }
870
871 QualType T = Arg.getAsType();
Douglas Gregora5cb6da2009-10-20 05:58:46 +0000872 if (T.isNull())
Douglas Gregora04f2ca2010-03-01 15:56:25 +0000873 return cast_or_null<NamedDecl>(TransformDecl(Loc, D));
Douglas Gregora5cb6da2009-10-20 05:58:46 +0000874
875 if (const TagType *Tag = T->getAs<TagType>())
876 return Tag->getDecl();
877
878 // The resulting type is not a tag; complain.
879 getSema().Diag(Loc, diag::err_nested_name_spec_non_tag) << T;
880 return 0;
881 }
882 }
883
Douglas Gregora04f2ca2010-03-01 15:56:25 +0000884 return cast_or_null<NamedDecl>(TransformDecl(Loc, D));
Douglas Gregora5cb6da2009-10-20 05:58:46 +0000885}
886
Douglas Gregorebe10102009-08-20 07:17:43 +0000887VarDecl *
888TemplateInstantiator::RebuildExceptionDecl(VarDecl *ExceptionDecl,
John McCallbcd03502009-12-07 02:54:59 +0000889 TypeSourceInfo *Declarator,
Abramo Bagnaradff19302011-03-08 08:55:46 +0000890 SourceLocation StartLoc,
891 SourceLocation NameLoc,
892 IdentifierInfo *Name) {
Douglas Gregor9f0e1aa2010-09-09 17:09:21 +0000893 VarDecl *Var = inherited::RebuildExceptionDecl(ExceptionDecl, Declarator,
Abramo Bagnaradff19302011-03-08 08:55:46 +0000894 StartLoc, NameLoc, Name);
Douglas Gregorf4e837f2010-04-26 17:57:08 +0000895 if (Var)
896 getSema().CurrentInstantiationScope->InstantiatedLocal(ExceptionDecl, Var);
897 return Var;
898}
899
900VarDecl *TemplateInstantiator::RebuildObjCExceptionDecl(VarDecl *ExceptionDecl,
901 TypeSourceInfo *TSInfo,
902 QualType T) {
903 VarDecl *Var = inherited::RebuildObjCExceptionDecl(ExceptionDecl, TSInfo, T);
904 if (Var)
Douglas Gregorebe10102009-08-20 07:17:43 +0000905 getSema().CurrentInstantiationScope->InstantiatedLocal(ExceptionDecl, Var);
906 return Var;
907}
908
John McCall7f41d982009-09-11 04:59:25 +0000909QualType
John McCall954b5de2010-11-04 19:04:38 +0000910TemplateInstantiator::RebuildElaboratedType(SourceLocation KeywordLoc,
911 ElaboratedTypeKeyword Keyword,
Douglas Gregor844cb502011-03-01 18:12:44 +0000912 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara6150c882010-05-11 21:36:43 +0000913 QualType T) {
John McCall7f41d982009-09-11 04:59:25 +0000914 if (const TagType *TT = T->getAs<TagType>()) {
915 TagDecl* TD = TT->getDecl();
916
John McCall954b5de2010-11-04 19:04:38 +0000917 SourceLocation TagLocation = KeywordLoc;
John McCall7f41d982009-09-11 04:59:25 +0000918
919 // FIXME: type might be anonymous.
920 IdentifierInfo *Id = TD->getIdentifier();
921
922 // TODO: should we even warn on struct/class mismatches for this? Seems
923 // like it's likely to produce a lot of spurious errors.
Abramo Bagnara6150c882010-05-11 21:36:43 +0000924 if (Keyword != ETK_None && Keyword != ETK_Typename) {
925 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForKeyword(Keyword);
926 if (!SemaRef.isAcceptableTagRedeclaration(TD, Kind, TagLocation, *Id)) {
927 SemaRef.Diag(TagLocation, diag::err_use_with_wrong_tag)
928 << Id
929 << FixItHint::CreateReplacement(SourceRange(TagLocation),
930 TD->getKindName());
931 SemaRef.Diag(TD->getLocation(), diag::note_previous_use);
932 }
John McCall7f41d982009-09-11 04:59:25 +0000933 }
934 }
935
John McCall954b5de2010-11-04 19:04:38 +0000936 return TreeTransform<TemplateInstantiator>::RebuildElaboratedType(KeywordLoc,
937 Keyword,
Douglas Gregor844cb502011-03-01 18:12:44 +0000938 QualifierLoc,
939 T);
John McCall7f41d982009-09-11 04:59:25 +0000940}
941
Douglas Gregor9db53502011-03-02 18:07:45 +0000942TemplateName TemplateInstantiator::TransformTemplateName(CXXScopeSpec &SS,
943 TemplateName Name,
944 SourceLocation NameLoc,
945 QualType ObjectType,
946 NamedDecl *FirstQualifierInScope) {
947 if (TemplateTemplateParmDecl *TTP
948 = dyn_cast_or_null<TemplateTemplateParmDecl>(Name.getAsTemplateDecl())) {
949 if (TTP->getDepth() < TemplateArgs.getNumLevels()) {
950 // If the corresponding template argument is NULL or non-existent, it's
951 // because we are performing instantiation from explicitly-specified
952 // template arguments in a function template, but there were some
953 // arguments left unspecified.
954 if (!TemplateArgs.hasTemplateArgument(TTP->getDepth(),
955 TTP->getPosition()))
956 return Name;
957
958 TemplateArgument Arg = TemplateArgs(TTP->getDepth(), TTP->getPosition());
959
960 if (TTP->isParameterPack()) {
961 assert(Arg.getKind() == TemplateArgument::Pack &&
962 "Missing argument pack");
963
964 if (getSema().ArgumentPackSubstitutionIndex == -1) {
965 // We have the template argument pack to substitute, but we're not
966 // actually expanding the enclosing pack expansion yet. So, just
967 // keep the entire argument pack.
968 return getSema().Context.getSubstTemplateTemplateParmPack(TTP, Arg);
969 }
970
971 assert(getSema().ArgumentPackSubstitutionIndex < (int)Arg.pack_size());
972 Arg = Arg.pack_begin()[getSema().ArgumentPackSubstitutionIndex];
973 }
974
975 TemplateName Template = Arg.getAsTemplate();
Richard Smith3f1b5d02011-05-05 21:57:07 +0000976 assert(!Template.isNull() && "Null template template argument");
Douglas Gregor9d9f8db2011-03-05 20:06:51 +0000977
978 // We don't ever want to substitute for a qualified template name, since
979 // the qualifier is handled separately. So, look through the qualified
980 // template name to its underlying declaration.
981 if (QualifiedTemplateName *QTN = Template.getAsQualifiedTemplateName())
982 Template = TemplateName(QTN->getTemplateDecl());
983
Douglas Gregor9db53502011-03-02 18:07:45 +0000984 return Template;
985 }
986 }
987
988 if (SubstTemplateTemplateParmPackStorage *SubstPack
989 = Name.getAsSubstTemplateTemplateParmPack()) {
990 if (getSema().ArgumentPackSubstitutionIndex == -1)
991 return Name;
992
993 const TemplateArgument &ArgPack = SubstPack->getArgumentPack();
994 assert(getSema().ArgumentPackSubstitutionIndex < (int)ArgPack.pack_size() &&
995 "Pack substitution index out-of-range");
996 return ArgPack.pack_begin()[getSema().ArgumentPackSubstitutionIndex]
997 .getAsTemplate();
998 }
999
1000 return inherited::TransformTemplateName(SS, Name, NameLoc, ObjectType,
1001 FirstQualifierInScope);
1002}
1003
John McCalldadc5752010-08-24 06:29:42 +00001004ExprResult
John McCall47f29ea2009-12-08 09:21:05 +00001005TemplateInstantiator::TransformPredefinedExpr(PredefinedExpr *E) {
Anders Carlsson0b209a82009-09-11 01:22:35 +00001006 if (!E->isTypeDependent())
John McCallc3007a22010-10-26 07:05:15 +00001007 return SemaRef.Owned(E);
Anders Carlsson0b209a82009-09-11 01:22:35 +00001008
1009 FunctionDecl *currentDecl = getSema().getCurFunctionDecl();
1010 assert(currentDecl && "Must have current function declaration when "
1011 "instantiating.");
1012
1013 PredefinedExpr::IdentType IT = E->getIdentType();
1014
Anders Carlsson5bd8d192010-02-11 18:20:28 +00001015 unsigned Length = PredefinedExpr::ComputeName(IT, currentDecl).length();
Anders Carlsson0b209a82009-09-11 01:22:35 +00001016
1017 llvm::APInt LengthI(32, Length + 1);
John McCall8ccfcb52009-09-24 19:53:00 +00001018 QualType ResTy = getSema().Context.CharTy.withConst();
Anders Carlsson0b209a82009-09-11 01:22:35 +00001019 ResTy = getSema().Context.getConstantArrayType(ResTy, LengthI,
1020 ArrayType::Normal, 0);
1021 PredefinedExpr *PE =
1022 new (getSema().Context) PredefinedExpr(E->getLocation(), ResTy, IT);
1023 return getSema().Owned(PE);
1024}
1025
John McCalldadc5752010-08-24 06:29:42 +00001026ExprResult
John McCall13481c52010-02-06 08:42:39 +00001027TemplateInstantiator::TransformTemplateParmRefExpr(DeclRefExpr *E,
Douglas Gregor6c379e22010-02-08 23:41:45 +00001028 NonTypeTemplateParmDecl *NTTP) {
John McCall13481c52010-02-06 08:42:39 +00001029 // If the corresponding template argument is NULL or non-existent, it's
1030 // because we are performing instantiation from explicitly-specified
1031 // template arguments in a function template, but there were some
1032 // arguments left unspecified.
1033 if (!TemplateArgs.hasTemplateArgument(NTTP->getDepth(),
1034 NTTP->getPosition()))
John McCallc3007a22010-10-26 07:05:15 +00001035 return SemaRef.Owned(E);
Mike Stump11289f42009-09-09 15:08:12 +00001036
Douglas Gregoreb5a39d2010-12-24 00:15:10 +00001037 TemplateArgument Arg = TemplateArgs(NTTP->getDepth(), NTTP->getPosition());
1038 if (NTTP->isParameterPack()) {
1039 assert(Arg.getKind() == TemplateArgument::Pack &&
1040 "Missing argument pack");
1041
1042 if (getSema().ArgumentPackSubstitutionIndex == -1) {
Douglas Gregorcdbc5392011-01-15 01:15:58 +00001043 // We have an argument pack, but we can't select a particular argument
1044 // out of it yet. Therefore, we'll build an expression to hold on to that
1045 // argument pack.
1046 QualType TargetType = SemaRef.SubstType(NTTP->getType(), TemplateArgs,
1047 E->getLocation(),
1048 NTTP->getDeclName());
1049 if (TargetType.isNull())
1050 return ExprError();
1051
1052 return new (SemaRef.Context) SubstNonTypeTemplateParmPackExpr(TargetType,
1053 NTTP,
1054 E->getLocation(),
1055 Arg);
Douglas Gregoreb5a39d2010-12-24 00:15:10 +00001056 }
1057
Douglas Gregora8bac7f2011-01-10 07:32:04 +00001058 assert(getSema().ArgumentPackSubstitutionIndex < (int)Arg.pack_size());
Douglas Gregoreb5a39d2010-12-24 00:15:10 +00001059 Arg = Arg.pack_begin()[getSema().ArgumentPackSubstitutionIndex];
1060 }
Mike Stump11289f42009-09-09 15:08:12 +00001061
John McCall13481c52010-02-06 08:42:39 +00001062 // The template argument itself might be an expression, in which
1063 // case we just return that expression.
1064 if (Arg.getKind() == TemplateArgument::Expression)
John McCallc3007a22010-10-26 07:05:15 +00001065 return SemaRef.Owned(Arg.getAsExpr());
Mike Stump11289f42009-09-09 15:08:12 +00001066
John McCall13481c52010-02-06 08:42:39 +00001067 if (Arg.getKind() == TemplateArgument::Declaration) {
1068 ValueDecl *VD = cast<ValueDecl>(Arg.getAsDecl());
Mike Stump11289f42009-09-09 15:08:12 +00001069
John McCall15dda372010-02-06 10:23:53 +00001070 // Find the instantiation of the template argument. This is
1071 // required for nested templates.
John McCall13481c52010-02-06 08:42:39 +00001072 VD = cast_or_null<ValueDecl>(
Douglas Gregora04f2ca2010-03-01 15:56:25 +00001073 getSema().FindInstantiatedDecl(E->getLocation(),
1074 VD, TemplateArgs));
John McCall13481c52010-02-06 08:42:39 +00001075 if (!VD)
John McCallfaf5fb42010-08-26 23:41:50 +00001076 return ExprError();
Mike Stump11289f42009-09-09 15:08:12 +00001077
John McCall15dda372010-02-06 10:23:53 +00001078 // Derive the type we want the substituted decl to have. This had
1079 // better be non-dependent, or these checks will have serious problems.
Douglas Gregor0231d8d2011-01-19 20:10:05 +00001080 QualType TargetType;
1081 if (NTTP->isExpandedParameterPack())
1082 TargetType = NTTP->getExpansionType(
1083 getSema().ArgumentPackSubstitutionIndex);
1084 else if (NTTP->isParameterPack() &&
1085 isa<PackExpansionType>(NTTP->getType())) {
1086 TargetType = SemaRef.SubstType(
1087 cast<PackExpansionType>(NTTP->getType())->getPattern(),
1088 TemplateArgs, E->getLocation(),
1089 NTTP->getDeclName());
1090 } else
1091 TargetType = SemaRef.SubstType(NTTP->getType(), TemplateArgs,
1092 E->getLocation(), NTTP->getDeclName());
John McCall15dda372010-02-06 10:23:53 +00001093 assert(!TargetType.isNull() && "type substitution failed for param type");
1094 assert(!TargetType->isDependentType() && "param type still dependent");
Douglas Gregord5cb1dd2010-03-28 02:42:43 +00001095 return SemaRef.BuildExpressionFromDeclTemplateArgument(Arg,
1096 TargetType,
1097 E->getLocation());
John McCall13481c52010-02-06 08:42:39 +00001098 }
1099
Douglas Gregord5cb1dd2010-03-28 02:42:43 +00001100 return SemaRef.BuildExpressionFromIntegralTemplateArgument(Arg,
1101 E->getSourceRange().getBegin());
John McCall13481c52010-02-06 08:42:39 +00001102}
1103
Douglas Gregorcdbc5392011-01-15 01:15:58 +00001104ExprResult
1105TemplateInstantiator::TransformSubstNonTypeTemplateParmPackExpr(
1106 SubstNonTypeTemplateParmPackExpr *E) {
1107 if (getSema().ArgumentPackSubstitutionIndex == -1) {
1108 // We aren't expanding the parameter pack, so just return ourselves.
1109 return getSema().Owned(E);
1110 }
1111
Douglas Gregorcdbc5392011-01-15 01:15:58 +00001112 const TemplateArgument &ArgPack = E->getArgumentPack();
1113 unsigned Index = (unsigned)getSema().ArgumentPackSubstitutionIndex;
1114 assert(Index < ArgPack.pack_size() && "Substitution index out-of-range");
1115
1116 const TemplateArgument &Arg = ArgPack.pack_begin()[Index];
1117 if (Arg.getKind() == TemplateArgument::Expression)
1118 return SemaRef.Owned(Arg.getAsExpr());
1119
1120 if (Arg.getKind() == TemplateArgument::Declaration) {
1121 ValueDecl *VD = cast<ValueDecl>(Arg.getAsDecl());
1122
1123 // Find the instantiation of the template argument. This is
1124 // required for nested templates.
1125 VD = cast_or_null<ValueDecl>(
1126 getSema().FindInstantiatedDecl(E->getParameterPackLocation(),
1127 VD, TemplateArgs));
1128 if (!VD)
1129 return ExprError();
1130
Douglas Gregor0231d8d2011-01-19 20:10:05 +00001131 QualType T;
1132 NonTypeTemplateParmDecl *NTTP = E->getParameterPack();
1133 if (NTTP->isExpandedParameterPack())
1134 T = NTTP->getExpansionType(getSema().ArgumentPackSubstitutionIndex);
1135 else if (const PackExpansionType *Expansion
1136 = dyn_cast<PackExpansionType>(NTTP->getType()))
1137 T = SemaRef.SubstType(Expansion->getPattern(), TemplateArgs,
1138 E->getParameterPackLocation(), NTTP->getDeclName());
1139 else
1140 T = E->getType();
1141 return SemaRef.BuildExpressionFromDeclTemplateArgument(Arg, T,
Douglas Gregorcdbc5392011-01-15 01:15:58 +00001142 E->getParameterPackLocation());
1143 }
1144
1145 return SemaRef.BuildExpressionFromIntegralTemplateArgument(Arg,
1146 E->getParameterPackLocation());
1147}
John McCall13481c52010-02-06 08:42:39 +00001148
John McCalldadc5752010-08-24 06:29:42 +00001149ExprResult
John McCall13481c52010-02-06 08:42:39 +00001150TemplateInstantiator::TransformDeclRefExpr(DeclRefExpr *E) {
1151 NamedDecl *D = E->getDecl();
1152 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(D)) {
1153 if (NTTP->getDepth() < TemplateArgs.getNumLevels())
1154 return TransformTemplateParmRefExpr(E, NTTP);
Douglas Gregor954de172009-10-31 17:21:17 +00001155
1156 // We have a non-type template parameter that isn't fully substituted;
1157 // FindInstantiatedDecl will find it in the local instantiation scope.
Douglas Gregora16548e2009-08-11 05:31:07 +00001158 }
Mike Stump11289f42009-09-09 15:08:12 +00001159
John McCall47f29ea2009-12-08 09:21:05 +00001160 return TreeTransform<TemplateInstantiator>::TransformDeclRefExpr(E);
Douglas Gregora16548e2009-08-11 05:31:07 +00001161}
1162
John McCalldadc5752010-08-24 06:29:42 +00001163ExprResult TemplateInstantiator::TransformCXXDefaultArgExpr(
John McCall47f29ea2009-12-08 09:21:05 +00001164 CXXDefaultArgExpr *E) {
Sebastian Redl14236c82009-11-08 13:56:19 +00001165 assert(!cast<FunctionDecl>(E->getParam()->getDeclContext())->
1166 getDescribedFunctionTemplate() &&
1167 "Default arg expressions are never formed in dependent cases.");
Douglas Gregor033f6752009-12-23 23:03:06 +00001168 return SemaRef.BuildCXXDefaultArgExpr(E->getUsedLocation(),
1169 cast<FunctionDecl>(E->getParam()->getDeclContext()),
1170 E->getParam());
Sebastian Redl14236c82009-11-08 13:56:19 +00001171}
1172
Douglas Gregor14cf7522010-04-30 18:55:50 +00001173QualType TemplateInstantiator::TransformFunctionProtoType(TypeLocBuilder &TLB,
John McCall31f82722010-11-12 08:19:04 +00001174 FunctionProtoTypeLoc TL) {
Douglas Gregor14cf7522010-04-30 18:55:50 +00001175 // We need a local instantiation scope for this function prototype.
John McCall19c1bfd2010-08-25 05:32:35 +00001176 LocalInstantiationScope Scope(SemaRef, /*CombineWithOuterScope=*/true);
John McCall31f82722010-11-12 08:19:04 +00001177 return inherited::TransformFunctionProtoType(TLB, TL);
John McCall58f10c32010-03-11 09:03:00 +00001178}
1179
1180ParmVarDecl *
Douglas Gregor715e4612011-01-14 22:40:04 +00001181TemplateInstantiator::TransformFunctionTypeParam(ParmVarDecl *OldParm,
John McCall8fb0d9d2011-05-01 22:35:37 +00001182 int indexAdjustment,
Douglas Gregor715e4612011-01-14 22:40:04 +00001183 llvm::Optional<unsigned> NumExpansions) {
John McCall8fb0d9d2011-05-01 22:35:37 +00001184 return SemaRef.SubstParmVarDecl(OldParm, TemplateArgs, indexAdjustment,
Douglas Gregor715e4612011-01-14 22:40:04 +00001185 NumExpansions);
John McCall58f10c32010-03-11 09:03:00 +00001186}
1187
Mike Stump11289f42009-09-09 15:08:12 +00001188QualType
John McCall550e0c22009-10-21 00:40:46 +00001189TemplateInstantiator::TransformTemplateTypeParmType(TypeLocBuilder &TLB,
John McCall31f82722010-11-12 08:19:04 +00001190 TemplateTypeParmTypeLoc TL) {
John McCall424cec92011-01-19 06:33:43 +00001191 const TemplateTypeParmType *T = TL.getTypePtr();
Douglas Gregor01afeef2009-08-28 20:31:08 +00001192 if (T->getDepth() < TemplateArgs.getNumLevels()) {
Douglas Gregorfe1e1102009-02-27 19:31:52 +00001193 // Replace the template type parameter with its corresponding
1194 // template argument.
Mike Stump11289f42009-09-09 15:08:12 +00001195
1196 // If the corresponding template argument is NULL or doesn't exist, it's
1197 // because we are performing instantiation from explicitly-specified
1198 // template arguments in a function template class, but there were some
Douglas Gregore3f1f352009-07-01 00:28:38 +00001199 // arguments left unspecified.
John McCall550e0c22009-10-21 00:40:46 +00001200 if (!TemplateArgs.hasTemplateArgument(T->getDepth(), T->getIndex())) {
1201 TemplateTypeParmTypeLoc NewTL
1202 = TLB.push<TemplateTypeParmTypeLoc>(TL.getType());
1203 NewTL.setNameLoc(TL.getNameLoc());
1204 return TL.getType();
1205 }
Mike Stump11289f42009-09-09 15:08:12 +00001206
Douglas Gregor840bd6c2010-12-20 22:05:00 +00001207 TemplateArgument Arg = TemplateArgs(T->getDepth(), T->getIndex());
1208
1209 if (T->isParameterPack()) {
1210 assert(Arg.getKind() == TemplateArgument::Pack &&
1211 "Missing argument pack");
1212
1213 if (getSema().ArgumentPackSubstitutionIndex == -1) {
Douglas Gregorada4b792011-01-14 02:55:32 +00001214 // We have the template argument pack, but we're not expanding the
1215 // enclosing pack expansion yet. Just save the template argument
1216 // pack for later substitution.
1217 QualType Result
1218 = getSema().Context.getSubstTemplateTypeParmPackType(T, Arg);
1219 SubstTemplateTypeParmPackTypeLoc NewTL
1220 = TLB.push<SubstTemplateTypeParmPackTypeLoc>(Result);
1221 NewTL.setNameLoc(TL.getNameLoc());
1222 return Result;
Douglas Gregor840bd6c2010-12-20 22:05:00 +00001223 }
1224
Douglas Gregora8bac7f2011-01-10 07:32:04 +00001225 assert(getSema().ArgumentPackSubstitutionIndex < (int)Arg.pack_size());
Douglas Gregor840bd6c2010-12-20 22:05:00 +00001226 Arg = Arg.pack_begin()[getSema().ArgumentPackSubstitutionIndex];
1227 }
1228
1229 assert(Arg.getKind() == TemplateArgument::Type &&
Douglas Gregorfe1e1102009-02-27 19:31:52 +00001230 "Template argument kind mismatch");
Douglas Gregor01afeef2009-08-28 20:31:08 +00001231
Douglas Gregor840bd6c2010-12-20 22:05:00 +00001232 QualType Replacement = Arg.getAsType();
John McCallcebee162009-10-18 09:09:24 +00001233
1234 // TODO: only do this uniquing once, at the start of instantiation.
John McCall550e0c22009-10-21 00:40:46 +00001235 QualType Result
1236 = getSema().Context.getSubstTemplateTypeParmType(T, Replacement);
1237 SubstTemplateTypeParmTypeLoc NewTL
1238 = TLB.push<SubstTemplateTypeParmTypeLoc>(Result);
1239 NewTL.setNameLoc(TL.getNameLoc());
1240 return Result;
Mike Stump11289f42009-09-09 15:08:12 +00001241 }
Douglas Gregorfe1e1102009-02-27 19:31:52 +00001242
1243 // The template type parameter comes from an inner template (e.g.,
1244 // the template parameter list of a member template inside the
1245 // template we are instantiating). Create a new template type
1246 // parameter with the template "level" reduced by one.
Chandler Carruth08836322011-05-01 00:51:33 +00001247 TemplateTypeParmDecl *NewTTPDecl = 0;
1248 if (TemplateTypeParmDecl *OldTTPDecl = T->getDecl())
1249 NewTTPDecl = cast_or_null<TemplateTypeParmDecl>(
1250 TransformDecl(TL.getNameLoc(), OldTTPDecl));
1251
John McCall550e0c22009-10-21 00:40:46 +00001252 QualType Result
1253 = getSema().Context.getTemplateTypeParmType(T->getDepth()
1254 - TemplateArgs.getNumLevels(),
1255 T->getIndex(),
1256 T->isParameterPack(),
Chandler Carruth08836322011-05-01 00:51:33 +00001257 NewTTPDecl);
John McCall550e0c22009-10-21 00:40:46 +00001258 TemplateTypeParmTypeLoc NewTL = TLB.push<TemplateTypeParmTypeLoc>(Result);
1259 NewTL.setNameLoc(TL.getNameLoc());
1260 return Result;
Douglas Gregor17c0d7b2009-02-28 00:25:32 +00001261}
Douglas Gregorfe1e1102009-02-27 19:31:52 +00001262
Douglas Gregorada4b792011-01-14 02:55:32 +00001263QualType
1264TemplateInstantiator::TransformSubstTemplateTypeParmPackType(
1265 TypeLocBuilder &TLB,
1266 SubstTemplateTypeParmPackTypeLoc TL) {
1267 if (getSema().ArgumentPackSubstitutionIndex == -1) {
1268 // We aren't expanding the parameter pack, so just return ourselves.
1269 SubstTemplateTypeParmPackTypeLoc NewTL
1270 = TLB.push<SubstTemplateTypeParmPackTypeLoc>(TL.getType());
1271 NewTL.setNameLoc(TL.getNameLoc());
1272 return TL.getType();
1273 }
1274
1275 const TemplateArgument &ArgPack = TL.getTypePtr()->getArgumentPack();
1276 unsigned Index = (unsigned)getSema().ArgumentPackSubstitutionIndex;
1277 assert(Index < ArgPack.pack_size() && "Substitution index out-of-range");
1278
1279 QualType Result = ArgPack.pack_begin()[Index].getAsType();
1280 Result = getSema().Context.getSubstTemplateTypeParmType(
1281 TL.getTypePtr()->getReplacedParameter(),
1282 Result);
1283 SubstTemplateTypeParmTypeLoc NewTL
1284 = TLB.push<SubstTemplateTypeParmTypeLoc>(Result);
1285 NewTL.setNameLoc(TL.getNameLoc());
1286 return Result;
1287}
1288
John McCall76d824f2009-08-25 22:02:44 +00001289/// \brief Perform substitution on the type T with a given set of template
1290/// arguments.
Douglas Gregorfe1e1102009-02-27 19:31:52 +00001291///
1292/// This routine substitutes the given template arguments into the
1293/// type T and produces the instantiated type.
1294///
1295/// \param T the type into which the template arguments will be
1296/// substituted. If this type is not dependent, it will be returned
1297/// immediately.
1298///
1299/// \param TemplateArgs the template arguments that will be
1300/// substituted for the top-level template parameters within T.
1301///
Douglas Gregorfe1e1102009-02-27 19:31:52 +00001302/// \param Loc the location in the source code where this substitution
1303/// is being performed. It will typically be the location of the
1304/// declarator (if we're instantiating the type of some declaration)
1305/// or the location of the type in the source code (if, e.g., we're
1306/// instantiating the type of a cast expression).
1307///
1308/// \param Entity the name of the entity associated with a declaration
1309/// being instantiated (if any). May be empty to indicate that there
1310/// is no such entity (if, e.g., this is a type that occurs as part of
1311/// a cast expression) or that the entity has no name (e.g., an
1312/// unnamed function parameter).
1313///
1314/// \returns If the instantiation succeeds, the instantiated
1315/// type. Otherwise, produces diagnostics and returns a NULL type.
John McCallbcd03502009-12-07 02:54:59 +00001316TypeSourceInfo *Sema::SubstType(TypeSourceInfo *T,
John McCall609459e2009-10-21 00:58:09 +00001317 const MultiLevelTemplateArgumentList &Args,
1318 SourceLocation Loc,
1319 DeclarationName Entity) {
1320 assert(!ActiveTemplateInstantiations.empty() &&
1321 "Cannot perform an instantiation without some context on the "
1322 "instantiation stack");
1323
Douglas Gregor5a5073e2010-05-24 17:22:01 +00001324 if (!T->getType()->isDependentType() &&
1325 !T->getType()->isVariablyModifiedType())
John McCall609459e2009-10-21 00:58:09 +00001326 return T;
1327
1328 TemplateInstantiator Instantiator(*this, Args, Loc, Entity);
1329 return Instantiator.TransformType(T);
1330}
1331
Douglas Gregor5499af42011-01-05 23:12:31 +00001332TypeSourceInfo *Sema::SubstType(TypeLoc TL,
1333 const MultiLevelTemplateArgumentList &Args,
1334 SourceLocation Loc,
1335 DeclarationName Entity) {
1336 assert(!ActiveTemplateInstantiations.empty() &&
1337 "Cannot perform an instantiation without some context on the "
1338 "instantiation stack");
1339
1340 if (TL.getType().isNull())
1341 return 0;
1342
1343 if (!TL.getType()->isDependentType() &&
1344 !TL.getType()->isVariablyModifiedType()) {
1345 // FIXME: Make a copy of the TypeLoc data here, so that we can
1346 // return a new TypeSourceInfo. Inefficient!
1347 TypeLocBuilder TLB;
1348 TLB.pushFullCopy(TL);
1349 return TLB.getTypeSourceInfo(Context, TL.getType());
1350 }
1351
1352 TemplateInstantiator Instantiator(*this, Args, Loc, Entity);
1353 TypeLocBuilder TLB;
1354 TLB.reserve(TL.getFullDataSize());
1355 QualType Result = Instantiator.TransformType(TLB, TL);
1356 if (Result.isNull())
1357 return 0;
1358
1359 return TLB.getTypeSourceInfo(Context, Result);
1360}
1361
John McCall609459e2009-10-21 00:58:09 +00001362/// Deprecated form of the above.
Mike Stump11289f42009-09-09 15:08:12 +00001363QualType Sema::SubstType(QualType T,
Douglas Gregor01afeef2009-08-28 20:31:08 +00001364 const MultiLevelTemplateArgumentList &TemplateArgs,
John McCall76d824f2009-08-25 22:02:44 +00001365 SourceLocation Loc, DeclarationName Entity) {
Douglas Gregor79cf6032009-03-10 20:44:00 +00001366 assert(!ActiveTemplateInstantiations.empty() &&
1367 "Cannot perform an instantiation without some context on the "
1368 "instantiation stack");
1369
Douglas Gregor5a5073e2010-05-24 17:22:01 +00001370 // If T is not a dependent type or a variably-modified type, there
1371 // is nothing to do.
1372 if (!T->isDependentType() && !T->isVariablyModifiedType())
Douglas Gregorfe1e1102009-02-27 19:31:52 +00001373 return T;
1374
Douglas Gregord6ff3322009-08-04 16:50:30 +00001375 TemplateInstantiator Instantiator(*this, TemplateArgs, Loc, Entity);
1376 return Instantiator.TransformType(T);
Douglas Gregorfe1e1102009-02-27 19:31:52 +00001377}
Douglas Gregor463421d2009-03-03 04:44:36 +00001378
John McCallb29f78f2010-04-09 17:38:44 +00001379static bool NeedsInstantiationAsFunctionType(TypeSourceInfo *T) {
Douglas Gregor5a5073e2010-05-24 17:22:01 +00001380 if (T->getType()->isDependentType() || T->getType()->isVariablyModifiedType())
John McCallb29f78f2010-04-09 17:38:44 +00001381 return true;
1382
Abramo Bagnara6d810632010-12-14 22:11:44 +00001383 TypeLoc TL = T->getTypeLoc().IgnoreParens();
John McCallb29f78f2010-04-09 17:38:44 +00001384 if (!isa<FunctionProtoTypeLoc>(TL))
1385 return false;
1386
1387 FunctionProtoTypeLoc FP = cast<FunctionProtoTypeLoc>(TL);
1388 for (unsigned I = 0, E = FP.getNumArgs(); I != E; ++I) {
1389 ParmVarDecl *P = FP.getArg(I);
1390
1391 // TODO: currently we always rebuild expressions. When we
1392 // properly get lazier about this, we should use the same
1393 // logic to avoid rebuilding prototypes here.
Douglas Gregor9cc278222011-01-05 21:14:17 +00001394 if (P->hasDefaultArg())
John McCallb29f78f2010-04-09 17:38:44 +00001395 return true;
1396 }
1397
1398 return false;
1399}
1400
1401/// A form of SubstType intended specifically for instantiating the
1402/// type of a FunctionDecl. Its purpose is solely to force the
1403/// instantiation of default-argument expressions.
1404TypeSourceInfo *Sema::SubstFunctionDeclType(TypeSourceInfo *T,
1405 const MultiLevelTemplateArgumentList &Args,
1406 SourceLocation Loc,
1407 DeclarationName Entity) {
1408 assert(!ActiveTemplateInstantiations.empty() &&
1409 "Cannot perform an instantiation without some context on the "
1410 "instantiation stack");
1411
1412 if (!NeedsInstantiationAsFunctionType(T))
1413 return T;
1414
1415 TemplateInstantiator Instantiator(*this, Args, Loc, Entity);
1416
1417 TypeLocBuilder TLB;
1418
1419 TypeLoc TL = T->getTypeLoc();
1420 TLB.reserve(TL.getFullDataSize());
1421
John McCall31f82722010-11-12 08:19:04 +00001422 QualType Result = Instantiator.TransformType(TLB, TL);
John McCallb29f78f2010-04-09 17:38:44 +00001423 if (Result.isNull())
1424 return 0;
1425
1426 return TLB.getTypeSourceInfo(Context, Result);
1427}
1428
Douglas Gregor940bca72010-04-12 07:48:19 +00001429ParmVarDecl *Sema::SubstParmVarDecl(ParmVarDecl *OldParm,
Douglas Gregor715e4612011-01-14 22:40:04 +00001430 const MultiLevelTemplateArgumentList &TemplateArgs,
John McCall8fb0d9d2011-05-01 22:35:37 +00001431 int indexAdjustment,
Douglas Gregor715e4612011-01-14 22:40:04 +00001432 llvm::Optional<unsigned> NumExpansions) {
Douglas Gregor940bca72010-04-12 07:48:19 +00001433 TypeSourceInfo *OldDI = OldParm->getTypeSourceInfo();
Douglas Gregor5499af42011-01-05 23:12:31 +00001434 TypeSourceInfo *NewDI = 0;
1435
Douglas Gregor5499af42011-01-05 23:12:31 +00001436 TypeLoc OldTL = OldDI->getTypeLoc();
1437 if (isa<PackExpansionTypeLoc>(OldTL)) {
1438 PackExpansionTypeLoc ExpansionTL = cast<PackExpansionTypeLoc>(OldTL);
Douglas Gregor5499af42011-01-05 23:12:31 +00001439
1440 // We have a function parameter pack. Substitute into the pattern of the
1441 // expansion.
1442 NewDI = SubstType(ExpansionTL.getPatternLoc(), TemplateArgs,
1443 OldParm->getLocation(), OldParm->getDeclName());
1444 if (!NewDI)
1445 return 0;
1446
1447 if (NewDI->getType()->containsUnexpandedParameterPack()) {
1448 // We still have unexpanded parameter packs, which means that
1449 // our function parameter is still a function parameter pack.
1450 // Therefore, make its type a pack expansion type.
Douglas Gregor0dca5fd2011-01-14 17:04:44 +00001451 NewDI = CheckPackExpansion(NewDI, ExpansionTL.getEllipsisLoc(),
Douglas Gregor715e4612011-01-14 22:40:04 +00001452 NumExpansions);
Douglas Gregor5499af42011-01-05 23:12:31 +00001453 }
1454 } else {
1455 NewDI = SubstType(OldDI, TemplateArgs, OldParm->getLocation(),
1456 OldParm->getDeclName());
1457 }
1458
Douglas Gregor940bca72010-04-12 07:48:19 +00001459 if (!NewDI)
1460 return 0;
1461
1462 if (NewDI->getType()->isVoidType()) {
1463 Diag(OldParm->getLocation(), diag::err_param_with_void_type);
1464 return 0;
1465 }
1466
1467 ParmVarDecl *NewParm = CheckParameter(Context.getTranslationUnitDecl(),
Abramo Bagnaradff19302011-03-08 08:55:46 +00001468 OldParm->getInnerLocStart(),
Douglas Gregor940bca72010-04-12 07:48:19 +00001469 OldParm->getLocation(),
Abramo Bagnaradff19302011-03-08 08:55:46 +00001470 OldParm->getIdentifier(),
1471 NewDI->getType(), NewDI,
Douglas Gregorc4df4072010-04-19 22:54:31 +00001472 OldParm->getStorageClass(),
1473 OldParm->getStorageClassAsWritten());
Douglas Gregor940bca72010-04-12 07:48:19 +00001474 if (!NewParm)
1475 return 0;
Douglas Gregor6044d692010-05-19 17:02:24 +00001476
Douglas Gregor940bca72010-04-12 07:48:19 +00001477 // Mark the (new) default argument as uninstantiated (if any).
1478 if (OldParm->hasUninstantiatedDefaultArg()) {
1479 Expr *Arg = OldParm->getUninstantiatedDefaultArg();
1480 NewParm->setUninstantiatedDefaultArg(Arg);
Douglas Gregor758cb672010-10-12 18:23:32 +00001481 } else if (OldParm->hasUnparsedDefaultArg()) {
1482 NewParm->setUnparsedDefaultArg();
1483 UnparsedDefaultArgInstantiations[OldParm].push_back(NewParm);
Douglas Gregor940bca72010-04-12 07:48:19 +00001484 } else if (Expr *Arg = OldParm->getDefaultArg())
1485 NewParm->setUninstantiatedDefaultArg(Arg);
1486
1487 NewParm->setHasInheritedDefaultArg(OldParm->hasInheritedDefaultArg());
1488
Douglas Gregor5499af42011-01-05 23:12:31 +00001489 // FIXME: When OldParm is a parameter pack and NewParm is not a parameter
1490 // pack, we actually have a set of instantiated locations. Maintain this set!
Douglas Gregorf3010112011-01-07 16:43:16 +00001491 if (OldParm->isParameterPack() && !NewParm->isParameterPack()) {
1492 // Add the new parameter to
1493 CurrentInstantiationScope->InstantiatedLocalPackArg(OldParm, NewParm);
1494 } else {
1495 // Introduce an Old -> New mapping
Douglas Gregor5499af42011-01-05 23:12:31 +00001496 CurrentInstantiationScope->InstantiatedLocal(OldParm, NewParm);
Douglas Gregorf3010112011-01-07 16:43:16 +00001497 }
Douglas Gregor5499af42011-01-05 23:12:31 +00001498
Argyrios Kyrtzidis3816ed42010-07-19 10:14:41 +00001499 // FIXME: OldParm may come from a FunctionProtoType, in which case CurContext
1500 // can be anything, is this right ?
Fariborz Jahanian714447b2010-07-13 21:05:02 +00001501 NewParm->setDeclContext(CurContext);
John McCall8fb0d9d2011-05-01 22:35:37 +00001502
1503 NewParm->setScopeInfo(OldParm->getFunctionScopeDepth(),
1504 OldParm->getFunctionScopeIndex() + indexAdjustment);
Fariborz Jahaniana6c7efe2010-07-13 20:05:58 +00001505
Douglas Gregor940bca72010-04-12 07:48:19 +00001506 return NewParm;
1507}
1508
Douglas Gregordd472162011-01-07 00:20:55 +00001509/// \brief Substitute the given template arguments into the given set of
1510/// parameters, producing the set of parameter types that would be generated
1511/// from such a substitution.
1512bool Sema::SubstParmTypes(SourceLocation Loc,
1513 ParmVarDecl **Params, unsigned NumParams,
1514 const MultiLevelTemplateArgumentList &TemplateArgs,
Douglas Gregorf3010112011-01-07 16:43:16 +00001515 llvm::SmallVectorImpl<QualType> &ParamTypes,
1516 llvm::SmallVectorImpl<ParmVarDecl *> *OutParams) {
Douglas Gregordd472162011-01-07 00:20:55 +00001517 assert(!ActiveTemplateInstantiations.empty() &&
1518 "Cannot perform an instantiation without some context on the "
1519 "instantiation stack");
1520
1521 TemplateInstantiator Instantiator(*this, TemplateArgs, Loc,
1522 DeclarationName());
1523 return Instantiator.TransformFunctionTypeParams(Loc, Params, NumParams, 0,
Douglas Gregorf3010112011-01-07 16:43:16 +00001524 ParamTypes, OutParams);
Douglas Gregordd472162011-01-07 00:20:55 +00001525}
1526
John McCall76d824f2009-08-25 22:02:44 +00001527/// \brief Perform substitution on the base class specifiers of the
1528/// given class template specialization.
Douglas Gregor463421d2009-03-03 04:44:36 +00001529///
1530/// Produces a diagnostic and returns true on error, returns false and
1531/// attaches the instantiated base classes to the class template
1532/// specialization if successful.
Mike Stump11289f42009-09-09 15:08:12 +00001533bool
John McCall76d824f2009-08-25 22:02:44 +00001534Sema::SubstBaseSpecifiers(CXXRecordDecl *Instantiation,
1535 CXXRecordDecl *Pattern,
Douglas Gregor01afeef2009-08-28 20:31:08 +00001536 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor463421d2009-03-03 04:44:36 +00001537 bool Invalid = false;
Douglas Gregor6181ded2009-05-29 18:27:38 +00001538 llvm::SmallVector<CXXBaseSpecifier*, 4> InstantiatedBases;
Mike Stump11289f42009-09-09 15:08:12 +00001539 for (ClassTemplateSpecializationDecl::base_class_iterator
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001540 Base = Pattern->bases_begin(), BaseEnd = Pattern->bases_end();
Douglas Gregor2a72edd2009-03-10 18:52:44 +00001541 Base != BaseEnd; ++Base) {
Douglas Gregor463421d2009-03-03 04:44:36 +00001542 if (!Base->getType()->isDependentType()) {
Fariborz Jahanian5c14ec32009-07-22 17:41:53 +00001543 InstantiatedBases.push_back(new (Context) CXXBaseSpecifier(*Base));
Douglas Gregor463421d2009-03-03 04:44:36 +00001544 continue;
1545 }
1546
Douglas Gregor752a5952011-01-03 22:36:02 +00001547 SourceLocation EllipsisLoc;
Douglas Gregorc52264e2011-03-02 02:04:06 +00001548 TypeSourceInfo *BaseTypeLoc;
Douglas Gregor752a5952011-01-03 22:36:02 +00001549 if (Base->isPackExpansion()) {
1550 // This is a pack expansion. See whether we should expand it now, or
1551 // wait until later.
1552 llvm::SmallVector<UnexpandedParameterPack, 2> Unexpanded;
1553 collectUnexpandedParameterPacks(Base->getTypeSourceInfo()->getTypeLoc(),
1554 Unexpanded);
1555 bool ShouldExpand = false;
Douglas Gregora8bac7f2011-01-10 07:32:04 +00001556 bool RetainExpansion = false;
Douglas Gregor0dca5fd2011-01-14 17:04:44 +00001557 llvm::Optional<unsigned> NumExpansions;
Douglas Gregor752a5952011-01-03 22:36:02 +00001558 if (CheckParameterPacksForExpansion(Base->getEllipsisLoc(),
1559 Base->getSourceRange(),
1560 Unexpanded.data(), Unexpanded.size(),
1561 TemplateArgs, ShouldExpand,
Douglas Gregora8bac7f2011-01-10 07:32:04 +00001562 RetainExpansion,
Douglas Gregor752a5952011-01-03 22:36:02 +00001563 NumExpansions)) {
Douglas Gregor752a5952011-01-03 22:36:02 +00001564 Invalid = true;
Douglas Gregor44e7df62011-01-04 00:32:56 +00001565 continue;
Douglas Gregor752a5952011-01-03 22:36:02 +00001566 }
1567
1568 // If we should expand this pack expansion now, do so.
1569 if (ShouldExpand) {
Douglas Gregor0dca5fd2011-01-14 17:04:44 +00001570 for (unsigned I = 0; I != *NumExpansions; ++I) {
Douglas Gregor752a5952011-01-03 22:36:02 +00001571 Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(*this, I);
1572
1573 TypeSourceInfo *BaseTypeLoc = SubstType(Base->getTypeSourceInfo(),
1574 TemplateArgs,
1575 Base->getSourceRange().getBegin(),
1576 DeclarationName());
1577 if (!BaseTypeLoc) {
1578 Invalid = true;
1579 continue;
1580 }
1581
1582 if (CXXBaseSpecifier *InstantiatedBase
1583 = CheckBaseSpecifier(Instantiation,
1584 Base->getSourceRange(),
1585 Base->isVirtual(),
1586 Base->getAccessSpecifierAsWritten(),
1587 BaseTypeLoc,
1588 SourceLocation()))
1589 InstantiatedBases.push_back(InstantiatedBase);
1590 else
1591 Invalid = true;
1592 }
1593
1594 continue;
1595 }
1596
1597 // The resulting base specifier will (still) be a pack expansion.
1598 EllipsisLoc = Base->getEllipsisLoc();
Douglas Gregorc52264e2011-03-02 02:04:06 +00001599 Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(*this, -1);
1600 BaseTypeLoc = SubstType(Base->getTypeSourceInfo(),
1601 TemplateArgs,
1602 Base->getSourceRange().getBegin(),
1603 DeclarationName());
1604 } else {
1605 BaseTypeLoc = SubstType(Base->getTypeSourceInfo(),
1606 TemplateArgs,
1607 Base->getSourceRange().getBegin(),
1608 DeclarationName());
Douglas Gregor752a5952011-01-03 22:36:02 +00001609 }
1610
Nick Lewycky19b9f952010-07-26 16:56:01 +00001611 if (!BaseTypeLoc) {
Douglas Gregor463421d2009-03-03 04:44:36 +00001612 Invalid = true;
1613 continue;
1614 }
1615
1616 if (CXXBaseSpecifier *InstantiatedBase
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001617 = CheckBaseSpecifier(Instantiation,
Douglas Gregor463421d2009-03-03 04:44:36 +00001618 Base->getSourceRange(),
1619 Base->isVirtual(),
1620 Base->getAccessSpecifierAsWritten(),
Douglas Gregor752a5952011-01-03 22:36:02 +00001621 BaseTypeLoc,
1622 EllipsisLoc))
Douglas Gregor463421d2009-03-03 04:44:36 +00001623 InstantiatedBases.push_back(InstantiatedBase);
1624 else
1625 Invalid = true;
1626 }
1627
Douglas Gregor2a72edd2009-03-10 18:52:44 +00001628 if (!Invalid &&
Jay Foad7d0479f2009-05-21 09:52:38 +00001629 AttachBaseSpecifiers(Instantiation, InstantiatedBases.data(),
Douglas Gregor463421d2009-03-03 04:44:36 +00001630 InstantiatedBases.size()))
1631 Invalid = true;
1632
1633 return Invalid;
1634}
1635
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001636/// \brief Instantiate the definition of a class from a given pattern.
1637///
1638/// \param PointOfInstantiation The point of instantiation within the
1639/// source code.
1640///
1641/// \param Instantiation is the declaration whose definition is being
1642/// instantiated. This will be either a class template specialization
1643/// or a member class of a class template specialization.
1644///
1645/// \param Pattern is the pattern from which the instantiation
1646/// occurs. This will be either the declaration of a class template or
1647/// the declaration of a member class of a class template.
1648///
1649/// \param TemplateArgs The template arguments to be substituted into
1650/// the pattern.
1651///
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001652/// \param TSK the kind of implicit or explicit instantiation to perform.
Douglas Gregor8a2e6012009-08-24 15:23:48 +00001653///
1654/// \param Complain whether to complain if the class cannot be instantiated due
1655/// to the lack of a definition.
1656///
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001657/// \returns true if an error occurred, false otherwise.
1658bool
1659Sema::InstantiateClass(SourceLocation PointOfInstantiation,
1660 CXXRecordDecl *Instantiation, CXXRecordDecl *Pattern,
Douglas Gregor01afeef2009-08-28 20:31:08 +00001661 const MultiLevelTemplateArgumentList &TemplateArgs,
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001662 TemplateSpecializationKind TSK,
Douglas Gregor8a2e6012009-08-24 15:23:48 +00001663 bool Complain) {
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001664 bool Invalid = false;
John McCall87a44eb2009-08-20 01:44:21 +00001665
Mike Stump11289f42009-09-09 15:08:12 +00001666 CXXRecordDecl *PatternDef
Douglas Gregor0a5a2212010-02-11 01:04:33 +00001667 = cast_or_null<CXXRecordDecl>(Pattern->getDefinition());
John McCall54766662011-04-27 06:46:31 +00001668 if (!PatternDef || PatternDef->isBeingDefined()) {
1669 if (!Complain || (PatternDef && PatternDef->isInvalidDecl())) {
Douglas Gregor8a2e6012009-08-24 15:23:48 +00001670 // Say nothing
John McCall54766662011-04-27 06:46:31 +00001671 } else if (PatternDef) {
1672 assert(PatternDef->isBeingDefined());
1673 Diag(PointOfInstantiation,
1674 diag::err_template_instantiate_within_definition)
1675 << (TSK != TSK_ImplicitInstantiation)
1676 << Context.getTypeDeclType(Instantiation);
1677 // Not much point in noting the template declaration here, since
1678 // we're lexically inside it.
1679 Instantiation->setInvalidDecl();
Douglas Gregor8a2e6012009-08-24 15:23:48 +00001680 } else if (Pattern == Instantiation->getInstantiatedFromMemberClass()) {
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001681 Diag(PointOfInstantiation,
1682 diag::err_implicit_instantiate_member_undefined)
1683 << Context.getTypeDeclType(Instantiation);
1684 Diag(Pattern->getLocation(), diag::note_member_of_template_here);
1685 } else {
Douglas Gregora1f49972009-05-13 00:25:59 +00001686 Diag(PointOfInstantiation, diag::err_template_instantiate_undefined)
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001687 << (TSK != TSK_ImplicitInstantiation)
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001688 << Context.getTypeDeclType(Instantiation);
1689 Diag(Pattern->getLocation(), diag::note_template_decl_here);
1690 }
1691 return true;
1692 }
1693 Pattern = PatternDef;
1694
Douglas Gregord6ba93d2009-10-15 15:54:05 +00001695 // \brief Record the point of instantiation.
1696 if (MemberSpecializationInfo *MSInfo
1697 = Instantiation->getMemberSpecializationInfo()) {
1698 MSInfo->setTemplateSpecializationKind(TSK);
1699 MSInfo->setPointOfInstantiation(PointOfInstantiation);
Douglas Gregoref6ab412009-10-27 06:26:26 +00001700 } else if (ClassTemplateSpecializationDecl *Spec
1701 = dyn_cast<ClassTemplateSpecializationDecl>(Instantiation)) {
1702 Spec->setTemplateSpecializationKind(TSK);
1703 Spec->setPointOfInstantiation(PointOfInstantiation);
Douglas Gregord6ba93d2009-10-15 15:54:05 +00001704 }
1705
Douglas Gregorf3430ae2009-03-25 21:23:52 +00001706 InstantiatingTemplate Inst(*this, PointOfInstantiation, Instantiation);
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001707 if (Inst)
1708 return true;
1709
1710 // Enter the scope of this instantiation. We don't use
1711 // PushDeclContext because we don't have a scope.
John McCall80e58cd2010-04-29 00:35:03 +00001712 ContextRAII SavedContext(*this, Instantiation);
Douglas Gregor17158422010-05-12 17:27:19 +00001713 EnterExpressionEvaluationContext EvalContext(*this,
John McCallfaf5fb42010-08-26 23:41:50 +00001714 Sema::PotentiallyEvaluated);
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001715
Douglas Gregor51121572010-03-24 01:33:17 +00001716 // If this is an instantiation of a local class, merge this local
1717 // instantiation scope with the enclosing scope. Otherwise, every
1718 // instantiation of a class has its own local instantiation scope.
1719 bool MergeWithParentScope = !Instantiation->isDefinedOutsideFunctionOrMethod();
John McCall19c1bfd2010-08-25 05:32:35 +00001720 LocalInstantiationScope Scope(*this, MergeWithParentScope);
Douglas Gregor51121572010-03-24 01:33:17 +00001721
John McCall6602bb12010-08-01 02:01:53 +00001722 // Pull attributes from the pattern onto the instantiation.
1723 InstantiateAttrs(TemplateArgs, Pattern, Instantiation);
1724
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001725 // Start the definition of this instantiation.
1726 Instantiation->startDefinition();
Douglas Gregore9029562010-05-06 00:28:52 +00001727
1728 Instantiation->setTagKind(Pattern->getTagKind());
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001729
John McCall76d824f2009-08-25 22:02:44 +00001730 // Do substitution on the base class specifiers.
1731 if (SubstBaseSpecifiers(Instantiation, Pattern, TemplateArgs))
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001732 Invalid = true;
1733
Douglas Gregor869853e2010-11-10 19:44:59 +00001734 TemplateDeclInstantiator Instantiator(*this, Instantiation, TemplateArgs);
John McCall48871652010-08-21 09:40:31 +00001735 llvm::SmallVector<Decl*, 4> Fields;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001736 for (RecordDecl::decl_iterator Member = Pattern->decls_begin(),
Mike Stump11289f42009-09-09 15:08:12 +00001737 MemberEnd = Pattern->decls_end();
Douglas Gregorbcced4e2009-04-09 21:40:53 +00001738 Member != MemberEnd; ++Member) {
Argyrios Kyrtzidis9a94d9b2010-11-04 03:18:57 +00001739 // Don't instantiate members not belonging in this semantic context.
1740 // e.g. for:
1741 // @code
1742 // template <int i> class A {
1743 // class B *g;
1744 // };
1745 // @endcode
1746 // 'class B' has the template as lexical context but semantically it is
1747 // introduced in namespace scope.
1748 if ((*Member)->getDeclContext() != Pattern)
1749 continue;
1750
Douglas Gregor869853e2010-11-10 19:44:59 +00001751 if ((*Member)->isInvalidDecl()) {
1752 Invalid = true;
1753 continue;
1754 }
1755
1756 Decl *NewMember = Instantiator.Visit(*Member);
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001757 if (NewMember) {
Eli Friedmand0e8de22009-12-07 00:22:08 +00001758 if (FieldDecl *Field = dyn_cast<FieldDecl>(NewMember))
John McCall48871652010-08-21 09:40:31 +00001759 Fields.push_back(Field);
Eli Friedmand0e8de22009-12-07 00:22:08 +00001760 else if (NewMember->isInvalidDecl())
1761 Invalid = true;
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001762 } else {
1763 // FIXME: Eventually, a NULL return will mean that one of the
Mike Stump87c57ac2009-05-16 07:39:55 +00001764 // instantiations was a semantic disaster, and we'll want to set Invalid =
1765 // true. For now, we expect to skip some members that we can't yet handle.
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001766 }
1767 }
1768
1769 // Finish checking fields.
John McCall48871652010-08-21 09:40:31 +00001770 ActOnFields(0, Instantiation->getLocation(), Instantiation,
Jay Foad7d0479f2009-05-21 09:52:38 +00001771 Fields.data(), Fields.size(), SourceLocation(), SourceLocation(),
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001772 0);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001773 CheckCompletedCXXClass(Instantiation);
Douglas Gregor3c74d412009-10-14 20:14:33 +00001774 if (Instantiation->isInvalidDecl())
1775 Invalid = true;
Douglas Gregor869853e2010-11-10 19:44:59 +00001776 else {
1777 // Instantiate any out-of-line class template partial
1778 // specializations now.
1779 for (TemplateDeclInstantiator::delayed_partial_spec_iterator
1780 P = Instantiator.delayed_partial_spec_begin(),
1781 PEnd = Instantiator.delayed_partial_spec_end();
1782 P != PEnd; ++P) {
1783 if (!Instantiator.InstantiateClassTemplatePartialSpecialization(
1784 P->first,
1785 P->second)) {
1786 Invalid = true;
1787 break;
1788 }
1789 }
1790 }
1791
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001792 // Exit the scope of this instantiation.
John McCall80e58cd2010-04-29 00:35:03 +00001793 SavedContext.pop();
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001794
Douglas Gregor88d292c2010-05-13 16:44:06 +00001795 if (!Invalid) {
Douglas Gregor28ad4b52009-05-26 20:50:29 +00001796 Consumer.HandleTagDeclDefinition(Instantiation);
1797
Douglas Gregor88d292c2010-05-13 16:44:06 +00001798 // Always emit the vtable for an explicit instantiation definition
1799 // of a polymorphic class template specialization.
1800 if (TSK == TSK_ExplicitInstantiationDefinition)
1801 MarkVTableUsed(PointOfInstantiation, Instantiation, true);
1802 }
1803
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001804 return Invalid;
1805}
1806
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +00001807namespace {
1808 /// \brief A partial specialization whose template arguments have matched
1809 /// a given template-id.
1810 struct PartialSpecMatchResult {
1811 ClassTemplatePartialSpecializationDecl *Partial;
1812 TemplateArgumentList *Args;
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +00001813 };
1814}
1815
Mike Stump11289f42009-09-09 15:08:12 +00001816bool
Douglas Gregor463421d2009-03-03 04:44:36 +00001817Sema::InstantiateClassTemplateSpecialization(
Douglas Gregoref6ab412009-10-27 06:26:26 +00001818 SourceLocation PointOfInstantiation,
Douglas Gregor463421d2009-03-03 04:44:36 +00001819 ClassTemplateSpecializationDecl *ClassTemplateSpec,
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001820 TemplateSpecializationKind TSK,
Douglas Gregor8a2e6012009-08-24 15:23:48 +00001821 bool Complain) {
Douglas Gregor463421d2009-03-03 04:44:36 +00001822 // Perform the actual instantiation on the canonical declaration.
1823 ClassTemplateSpec = cast<ClassTemplateSpecializationDecl>(
Argyrios Kyrtzidis6b7e3762009-07-18 00:34:25 +00001824 ClassTemplateSpec->getCanonicalDecl());
Douglas Gregor463421d2009-03-03 04:44:36 +00001825
Douglas Gregor4aa04b12009-09-11 21:19:12 +00001826 // Check whether we have already instantiated or specialized this class
1827 // template specialization.
1828 if (ClassTemplateSpec->getSpecializationKind() != TSK_Undeclared) {
1829 if (ClassTemplateSpec->getSpecializationKind() ==
1830 TSK_ExplicitInstantiationDeclaration &&
1831 TSK == TSK_ExplicitInstantiationDefinition) {
1832 // An explicit instantiation definition follows an explicit instantiation
1833 // declaration (C++0x [temp.explicit]p10); go ahead and perform the
1834 // explicit instantiation.
1835 ClassTemplateSpec->setSpecializationKind(TSK);
Douglas Gregor88d292c2010-05-13 16:44:06 +00001836
1837 // If this is an explicit instantiation definition, mark the
1838 // vtable as used.
1839 if (TSK == TSK_ExplicitInstantiationDefinition)
1840 MarkVTableUsed(PointOfInstantiation, ClassTemplateSpec, true);
1841
Douglas Gregor4aa04b12009-09-11 21:19:12 +00001842 return false;
1843 }
1844
1845 // We can only instantiate something that hasn't already been
1846 // instantiated or specialized. Fail without any diagnostics: our
1847 // caller will provide an error message.
Douglas Gregor463421d2009-03-03 04:44:36 +00001848 return true;
Douglas Gregor4aa04b12009-09-11 21:19:12 +00001849 }
Douglas Gregor463421d2009-03-03 04:44:36 +00001850
Douglas Gregor00a511f2009-09-15 16:51:42 +00001851 if (ClassTemplateSpec->isInvalidDecl())
1852 return true;
1853
Douglas Gregor463421d2009-03-03 04:44:36 +00001854 ClassTemplateDecl *Template = ClassTemplateSpec->getSpecializedTemplate();
Douglas Gregor01afeef2009-08-28 20:31:08 +00001855 CXXRecordDecl *Pattern = 0;
Douglas Gregor2373c592009-05-31 09:31:02 +00001856
Douglas Gregor170bc422009-06-12 22:31:52 +00001857 // C++ [temp.class.spec.match]p1:
1858 // When a class template is used in a context that requires an
1859 // instantiation of the class, it is necessary to determine
1860 // whether the instantiation is to be generated using the primary
1861 // template or one of the partial specializations. This is done by
1862 // matching the template arguments of the class template
1863 // specialization with the template argument lists of the partial
1864 // specializations.
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +00001865 typedef PartialSpecMatchResult MatchResult;
Douglas Gregorb7ae10f2009-06-05 00:53:49 +00001866 llvm::SmallVector<MatchResult, 4> Matched;
Douglas Gregor407e9612010-04-30 05:56:50 +00001867 llvm::SmallVector<ClassTemplatePartialSpecializationDecl *, 4> PartialSpecs;
1868 Template->getPartialSpecializations(PartialSpecs);
1869 for (unsigned I = 0, N = PartialSpecs.size(); I != N; ++I) {
1870 ClassTemplatePartialSpecializationDecl *Partial = PartialSpecs[I];
John McCallbc077cf2010-02-08 23:07:23 +00001871 TemplateDeductionInfo Info(Context, PointOfInstantiation);
Douglas Gregor181aa4a2009-06-12 18:26:56 +00001872 if (TemplateDeductionResult Result
Douglas Gregor407e9612010-04-30 05:56:50 +00001873 = DeduceTemplateArguments(Partial,
Douglas Gregor181aa4a2009-06-12 18:26:56 +00001874 ClassTemplateSpec->getTemplateArgs(),
1875 Info)) {
1876 // FIXME: Store the failed-deduction information for use in
1877 // diagnostics, later.
1878 (void)Result;
1879 } else {
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +00001880 Matched.push_back(PartialSpecMatchResult());
1881 Matched.back().Partial = Partial;
1882 Matched.back().Args = Info.take();
Douglas Gregor181aa4a2009-06-12 18:26:56 +00001883 }
Douglas Gregor2373c592009-05-31 09:31:02 +00001884 }
1885
Douglas Gregor0231d8d2011-01-19 20:10:05 +00001886 // If we're dealing with a member template where the template parameters
1887 // have been instantiated, this provides the original template parameters
1888 // from which the member template's parameters were instantiated.
1889 llvm::SmallVector<const NamedDecl *, 4> InstantiatedTemplateParameters;
1890
Douglas Gregor21610382009-10-29 00:04:11 +00001891 if (Matched.size() >= 1) {
Douglas Gregorbe999392009-09-15 16:23:51 +00001892 llvm::SmallVector<MatchResult, 4>::iterator Best = Matched.begin();
Douglas Gregor21610382009-10-29 00:04:11 +00001893 if (Matched.size() == 1) {
1894 // -- If exactly one matching specialization is found, the
1895 // instantiation is generated from that specialization.
1896 // We don't need to do anything for this.
1897 } else {
1898 // -- If more than one matching specialization is found, the
1899 // partial order rules (14.5.4.2) are used to determine
1900 // whether one of the specializations is more specialized
1901 // than the others. If none of the specializations is more
1902 // specialized than all of the other matching
1903 // specializations, then the use of the class template is
1904 // ambiguous and the program is ill-formed.
1905 for (llvm::SmallVector<MatchResult, 4>::iterator P = Best + 1,
1906 PEnd = Matched.end();
1907 P != PEnd; ++P) {
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +00001908 if (getMoreSpecializedPartialSpecialization(P->Partial, Best->Partial,
John McCallbc077cf2010-02-08 23:07:23 +00001909 PointOfInstantiation)
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +00001910 == P->Partial)
Douglas Gregor21610382009-10-29 00:04:11 +00001911 Best = P;
Douglas Gregorbe999392009-09-15 16:23:51 +00001912 }
Douglas Gregorbe999392009-09-15 16:23:51 +00001913
Douglas Gregor21610382009-10-29 00:04:11 +00001914 // Determine if the best partial specialization is more specialized than
1915 // the others.
1916 bool Ambiguous = false;
Douglas Gregorbe999392009-09-15 16:23:51 +00001917 for (llvm::SmallVector<MatchResult, 4>::iterator P = Matched.begin(),
1918 PEnd = Matched.end();
Douglas Gregor21610382009-10-29 00:04:11 +00001919 P != PEnd; ++P) {
1920 if (P != Best &&
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +00001921 getMoreSpecializedPartialSpecialization(P->Partial, Best->Partial,
John McCallbc077cf2010-02-08 23:07:23 +00001922 PointOfInstantiation)
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +00001923 != Best->Partial) {
Douglas Gregor21610382009-10-29 00:04:11 +00001924 Ambiguous = true;
1925 break;
1926 }
1927 }
1928
1929 if (Ambiguous) {
1930 // Partial ordering did not produce a clear winner. Complain.
1931 ClassTemplateSpec->setInvalidDecl();
1932 Diag(PointOfInstantiation, diag::err_partial_spec_ordering_ambiguous)
1933 << ClassTemplateSpec;
1934
1935 // Print the matching partial specializations.
1936 for (llvm::SmallVector<MatchResult, 4>::iterator P = Matched.begin(),
1937 PEnd = Matched.end();
1938 P != PEnd; ++P)
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +00001939 Diag(P->Partial->getLocation(), diag::note_partial_spec_match)
1940 << getTemplateArgumentBindingsText(
1941 P->Partial->getTemplateParameters(),
1942 *P->Args);
Douglas Gregor01afeef2009-08-28 20:31:08 +00001943
Douglas Gregor21610382009-10-29 00:04:11 +00001944 return true;
1945 }
Douglas Gregorbe999392009-09-15 16:23:51 +00001946 }
1947
1948 // Instantiate using the best class template partial specialization.
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +00001949 ClassTemplatePartialSpecializationDecl *OrigPartialSpec = Best->Partial;
Douglas Gregor21610382009-10-29 00:04:11 +00001950 while (OrigPartialSpec->getInstantiatedFromMember()) {
1951 // If we've found an explicit specialization of this class template,
1952 // stop here and use that as the pattern.
1953 if (OrigPartialSpec->isMemberSpecialization())
1954 break;
1955
1956 OrigPartialSpec = OrigPartialSpec->getInstantiatedFromMember();
1957 }
1958
1959 Pattern = OrigPartialSpec;
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +00001960 ClassTemplateSpec->setInstantiationOf(Best->Partial, Best->Args);
Douglas Gregor170bc422009-06-12 22:31:52 +00001961 } else {
1962 // -- If no matches are found, the instantiation is generated
1963 // from the primary template.
Douglas Gregor01afeef2009-08-28 20:31:08 +00001964 ClassTemplateDecl *OrigTemplate = Template;
Douglas Gregorcf915552009-10-13 16:30:37 +00001965 while (OrigTemplate->getInstantiatedFromMemberTemplate()) {
1966 // If we've found an explicit specialization of this class template,
1967 // stop here and use that as the pattern.
1968 if (OrigTemplate->isMemberSpecialization())
1969 break;
1970
Douglas Gregor01afeef2009-08-28 20:31:08 +00001971 OrigTemplate = OrigTemplate->getInstantiatedFromMemberTemplate();
Douglas Gregorcf915552009-10-13 16:30:37 +00001972 }
1973
Douglas Gregor01afeef2009-08-28 20:31:08 +00001974 Pattern = OrigTemplate->getTemplatedDecl();
Douglas Gregor2373c592009-05-31 09:31:02 +00001975 }
Douglas Gregor463421d2009-03-03 04:44:36 +00001976
Douglas Gregoref6ab412009-10-27 06:26:26 +00001977 bool Result = InstantiateClass(PointOfInstantiation, ClassTemplateSpec,
1978 Pattern,
1979 getTemplateInstantiationArgs(ClassTemplateSpec),
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001980 TSK,
Douglas Gregor8a2e6012009-08-24 15:23:48 +00001981 Complain);
Mike Stump11289f42009-09-09 15:08:12 +00001982
Douglas Gregorb7ae10f2009-06-05 00:53:49 +00001983 return Result;
Douglas Gregor463421d2009-03-03 04:44:36 +00001984}
Douglas Gregor90a1a652009-03-19 17:26:29 +00001985
John McCall76d824f2009-08-25 22:02:44 +00001986/// \brief Instantiates the definitions of all of the member
1987/// of the given class, which is an instantiation of a class template
1988/// or a member class of a template.
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00001989void
1990Sema::InstantiateClassMembers(SourceLocation PointOfInstantiation,
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001991 CXXRecordDecl *Instantiation,
1992 const MultiLevelTemplateArgumentList &TemplateArgs,
1993 TemplateSpecializationKind TSK) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001994 for (DeclContext::decl_iterator D = Instantiation->decls_begin(),
1995 DEnd = Instantiation->decls_end();
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00001996 D != DEnd; ++D) {
Douglas Gregor1d957a32009-10-27 18:42:08 +00001997 bool SuppressNew = false;
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00001998 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(*D)) {
Douglas Gregor1d957a32009-10-27 18:42:08 +00001999 if (FunctionDecl *Pattern
2000 = Function->getInstantiatedFromMemberFunction()) {
2001 MemberSpecializationInfo *MSInfo
2002 = Function->getMemberSpecializationInfo();
2003 assert(MSInfo && "No member specialization information?");
Douglas Gregor06aa50412010-04-09 21:02:29 +00002004 if (MSInfo->getTemplateSpecializationKind()
2005 == TSK_ExplicitSpecialization)
2006 continue;
2007
Douglas Gregor1d957a32009-10-27 18:42:08 +00002008 if (CheckSpecializationInstantiationRedecl(PointOfInstantiation, TSK,
2009 Function,
2010 MSInfo->getTemplateSpecializationKind(),
Nico Weberd75488d2010-09-27 21:02:09 +00002011 MSInfo->getPointOfInstantiation(),
Douglas Gregor1d957a32009-10-27 18:42:08 +00002012 SuppressNew) ||
2013 SuppressNew)
Douglas Gregorbbe8f462009-10-08 15:14:33 +00002014 continue;
2015
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00002016 if (Function->hasBody())
Douglas Gregor1d957a32009-10-27 18:42:08 +00002017 continue;
2018
2019 if (TSK == TSK_ExplicitInstantiationDefinition) {
2020 // C++0x [temp.explicit]p8:
2021 // An explicit instantiation definition that names a class template
2022 // specialization explicitly instantiates the class template
2023 // specialization and is only an explicit instantiation definition
2024 // of members whose definition is visible at the point of
2025 // instantiation.
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00002026 if (!Pattern->hasBody())
Douglas Gregor1d957a32009-10-27 18:42:08 +00002027 continue;
2028
2029 Function->setTemplateSpecializationKind(TSK, PointOfInstantiation);
2030
2031 InstantiateFunctionDefinition(PointOfInstantiation, Function);
2032 } else {
2033 Function->setTemplateSpecializationKind(TSK, PointOfInstantiation);
2034 }
Douglas Gregorbbe8f462009-10-08 15:14:33 +00002035 }
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00002036 } else if (VarDecl *Var = dyn_cast<VarDecl>(*D)) {
Douglas Gregor86d142a2009-10-08 07:24:58 +00002037 if (Var->isStaticDataMember()) {
Douglas Gregor1d957a32009-10-27 18:42:08 +00002038 MemberSpecializationInfo *MSInfo = Var->getMemberSpecializationInfo();
2039 assert(MSInfo && "No member specialization information?");
Douglas Gregor06aa50412010-04-09 21:02:29 +00002040 if (MSInfo->getTemplateSpecializationKind()
2041 == TSK_ExplicitSpecialization)
2042 continue;
2043
Douglas Gregor1d957a32009-10-27 18:42:08 +00002044 if (CheckSpecializationInstantiationRedecl(PointOfInstantiation, TSK,
2045 Var,
2046 MSInfo->getTemplateSpecializationKind(),
Nico Weberd75488d2010-09-27 21:02:09 +00002047 MSInfo->getPointOfInstantiation(),
Douglas Gregor1d957a32009-10-27 18:42:08 +00002048 SuppressNew) ||
2049 SuppressNew)
Douglas Gregorbbe8f462009-10-08 15:14:33 +00002050 continue;
2051
Douglas Gregor1d957a32009-10-27 18:42:08 +00002052 if (TSK == TSK_ExplicitInstantiationDefinition) {
2053 // C++0x [temp.explicit]p8:
2054 // An explicit instantiation definition that names a class template
2055 // specialization explicitly instantiates the class template
2056 // specialization and is only an explicit instantiation definition
2057 // of members whose definition is visible at the point of
2058 // instantiation.
2059 if (!Var->getInstantiatedFromStaticDataMember()
2060 ->getOutOfLineDefinition())
2061 continue;
2062
2063 Var->setTemplateSpecializationKind(TSK, PointOfInstantiation);
Douglas Gregor86d142a2009-10-08 07:24:58 +00002064 InstantiateStaticDataMemberDefinition(PointOfInstantiation, Var);
Douglas Gregor1d957a32009-10-27 18:42:08 +00002065 } else {
2066 Var->setTemplateSpecializationKind(TSK, PointOfInstantiation);
2067 }
2068 }
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00002069 } else if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(*D)) {
Douglas Gregor1da22252010-04-18 18:11:38 +00002070 // Always skip the injected-class-name, along with any
2071 // redeclarations of nested classes, since both would cause us
2072 // to try to instantiate the members of a class twice.
2073 if (Record->isInjectedClassName() || Record->getPreviousDeclaration())
Douglas Gregord801b062009-10-07 23:56:10 +00002074 continue;
2075
Douglas Gregor1d957a32009-10-27 18:42:08 +00002076 MemberSpecializationInfo *MSInfo = Record->getMemberSpecializationInfo();
2077 assert(MSInfo && "No member specialization information?");
Douglas Gregor06aa50412010-04-09 21:02:29 +00002078
2079 if (MSInfo->getTemplateSpecializationKind()
2080 == TSK_ExplicitSpecialization)
2081 continue;
Nico Weberd75488d2010-09-27 21:02:09 +00002082
Douglas Gregor1d957a32009-10-27 18:42:08 +00002083 if (CheckSpecializationInstantiationRedecl(PointOfInstantiation, TSK,
2084 Record,
2085 MSInfo->getTemplateSpecializationKind(),
Nico Weberd75488d2010-09-27 21:02:09 +00002086 MSInfo->getPointOfInstantiation(),
Douglas Gregor1d957a32009-10-27 18:42:08 +00002087 SuppressNew) ||
2088 SuppressNew)
Douglas Gregorbbe8f462009-10-08 15:14:33 +00002089 continue;
2090
Douglas Gregor1d957a32009-10-27 18:42:08 +00002091 CXXRecordDecl *Pattern = Record->getInstantiatedFromMemberClass();
2092 assert(Pattern && "Missing instantiated-from-template information");
2093
Douglas Gregor0a5a2212010-02-11 01:04:33 +00002094 if (!Record->getDefinition()) {
2095 if (!Pattern->getDefinition()) {
Douglas Gregor1d957a32009-10-27 18:42:08 +00002096 // C++0x [temp.explicit]p8:
2097 // An explicit instantiation definition that names a class template
2098 // specialization explicitly instantiates the class template
2099 // specialization and is only an explicit instantiation definition
2100 // of members whose definition is visible at the point of
2101 // instantiation.
2102 if (TSK == TSK_ExplicitInstantiationDeclaration) {
2103 MSInfo->setTemplateSpecializationKind(TSK);
2104 MSInfo->setPointOfInstantiation(PointOfInstantiation);
2105 }
2106
2107 continue;
2108 }
2109
2110 InstantiateClass(PointOfInstantiation, Record, Pattern,
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00002111 TemplateArgs,
2112 TSK);
Nico Weberd75488d2010-09-27 21:02:09 +00002113 } else {
2114 if (TSK == TSK_ExplicitInstantiationDefinition &&
2115 Record->getTemplateSpecializationKind() ==
2116 TSK_ExplicitInstantiationDeclaration) {
2117 Record->setTemplateSpecializationKind(TSK);
2118 MarkVTableUsed(PointOfInstantiation, Record, true);
2119 }
Douglas Gregor1d957a32009-10-27 18:42:08 +00002120 }
Douglas Gregorc093c1d2009-10-08 01:19:17 +00002121
Douglas Gregor0a5a2212010-02-11 01:04:33 +00002122 Pattern = cast_or_null<CXXRecordDecl>(Record->getDefinition());
Douglas Gregor1d957a32009-10-27 18:42:08 +00002123 if (Pattern)
2124 InstantiateClassMembers(PointOfInstantiation, Pattern, TemplateArgs,
2125 TSK);
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00002126 }
2127 }
2128}
2129
2130/// \brief Instantiate the definitions of all of the members of the
2131/// given class template specialization, which was named as part of an
2132/// explicit instantiation.
Mike Stump11289f42009-09-09 15:08:12 +00002133void
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00002134Sema::InstantiateClassTemplateSpecializationMembers(
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00002135 SourceLocation PointOfInstantiation,
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00002136 ClassTemplateSpecializationDecl *ClassTemplateSpec,
2137 TemplateSpecializationKind TSK) {
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00002138 // C++0x [temp.explicit]p7:
2139 // An explicit instantiation that names a class template
2140 // specialization is an explicit instantion of the same kind
2141 // (declaration or definition) of each of its members (not
2142 // including members inherited from base classes) that has not
2143 // been previously explicitly specialized in the translation unit
2144 // containing the explicit instantiation, except as described
2145 // below.
2146 InstantiateClassMembers(PointOfInstantiation, ClassTemplateSpec,
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00002147 getTemplateInstantiationArgs(ClassTemplateSpec),
2148 TSK);
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00002149}
2150
John McCalldadc5752010-08-24 06:29:42 +00002151StmtResult
Douglas Gregor01afeef2009-08-28 20:31:08 +00002152Sema::SubstStmt(Stmt *S, const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregorebe10102009-08-20 07:17:43 +00002153 if (!S)
2154 return Owned(S);
2155
2156 TemplateInstantiator Instantiator(*this, TemplateArgs,
2157 SourceLocation(),
2158 DeclarationName());
2159 return Instantiator.TransformStmt(S);
2160}
2161
John McCalldadc5752010-08-24 06:29:42 +00002162ExprResult
Douglas Gregor01afeef2009-08-28 20:31:08 +00002163Sema::SubstExpr(Expr *E, const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregora16548e2009-08-11 05:31:07 +00002164 if (!E)
2165 return Owned(E);
Mike Stump11289f42009-09-09 15:08:12 +00002166
Douglas Gregora16548e2009-08-11 05:31:07 +00002167 TemplateInstantiator Instantiator(*this, TemplateArgs,
2168 SourceLocation(),
2169 DeclarationName());
2170 return Instantiator.TransformExpr(E);
2171}
2172
Douglas Gregor2cd32a02011-01-07 19:35:17 +00002173bool Sema::SubstExprs(Expr **Exprs, unsigned NumExprs, bool IsCall,
2174 const MultiLevelTemplateArgumentList &TemplateArgs,
2175 llvm::SmallVectorImpl<Expr *> &Outputs) {
2176 if (NumExprs == 0)
2177 return false;
2178
2179 TemplateInstantiator Instantiator(*this, TemplateArgs,
2180 SourceLocation(),
2181 DeclarationName());
2182 return Instantiator.TransformExprs(Exprs, NumExprs, IsCall, Outputs);
2183}
2184
Douglas Gregor14454802011-02-25 02:25:35 +00002185NestedNameSpecifierLoc
2186Sema::SubstNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS,
2187 const MultiLevelTemplateArgumentList &TemplateArgs) {
2188 if (!NNS)
2189 return NestedNameSpecifierLoc();
2190
2191 TemplateInstantiator Instantiator(*this, TemplateArgs, NNS.getBeginLoc(),
2192 DeclarationName());
2193 return Instantiator.TransformNestedNameSpecifierLoc(NNS);
2194}
2195
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002196/// \brief Do template substitution on declaration name info.
2197DeclarationNameInfo
2198Sema::SubstDeclarationNameInfo(const DeclarationNameInfo &NameInfo,
2199 const MultiLevelTemplateArgumentList &TemplateArgs) {
2200 TemplateInstantiator Instantiator(*this, TemplateArgs, NameInfo.getLoc(),
2201 NameInfo.getName());
2202 return Instantiator.TransformDeclarationNameInfo(NameInfo);
2203}
2204
Douglas Gregoraa594892009-03-31 18:38:02 +00002205TemplateName
Douglas Gregordf846d12011-03-02 18:46:51 +00002206Sema::SubstTemplateName(NestedNameSpecifierLoc QualifierLoc,
2207 TemplateName Name, SourceLocation Loc,
Douglas Gregor01afeef2009-08-28 20:31:08 +00002208 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor71dc5092009-08-06 06:41:21 +00002209 TemplateInstantiator Instantiator(*this, TemplateArgs, Loc,
2210 DeclarationName());
Douglas Gregordf846d12011-03-02 18:46:51 +00002211 CXXScopeSpec SS;
2212 SS.Adopt(QualifierLoc);
2213 return Instantiator.TransformTemplateName(SS, Name, Loc);
Douglas Gregoraa594892009-03-31 18:38:02 +00002214}
Douglas Gregorc43620d2009-06-11 00:06:24 +00002215
Douglas Gregor0f3feb42010-12-22 21:19:48 +00002216bool Sema::Subst(const TemplateArgumentLoc *Args, unsigned NumArgs,
2217 TemplateArgumentListInfo &Result,
John McCall0ad16662009-10-29 08:12:44 +00002218 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregore922c772009-08-04 22:27:00 +00002219 TemplateInstantiator Instantiator(*this, TemplateArgs, SourceLocation(),
2220 DeclarationName());
Douglas Gregor0f3feb42010-12-22 21:19:48 +00002221
2222 return Instantiator.TransformTemplateArguments(Args, NumArgs, Result);
Douglas Gregorc43620d2009-06-11 00:06:24 +00002223}
Douglas Gregor14cf7522010-04-30 18:55:50 +00002224
Douglas Gregorf3010112011-01-07 16:43:16 +00002225llvm::PointerUnion<Decl *, LocalInstantiationScope::DeclArgumentPack *> *
2226LocalInstantiationScope::findInstantiationOf(const Decl *D) {
Chris Lattnercab02a62011-02-17 20:34:02 +00002227 for (LocalInstantiationScope *Current = this; Current;
Douglas Gregor14cf7522010-04-30 18:55:50 +00002228 Current = Current->Outer) {
Chris Lattnercab02a62011-02-17 20:34:02 +00002229
Douglas Gregor14cf7522010-04-30 18:55:50 +00002230 // Check if we found something within this scope.
Douglas Gregore9fc8dc2010-12-21 21:22:51 +00002231 const Decl *CheckD = D;
2232 do {
Douglas Gregorf3010112011-01-07 16:43:16 +00002233 LocalDeclsMap::iterator Found = Current->LocalDecls.find(CheckD);
Douglas Gregore9fc8dc2010-12-21 21:22:51 +00002234 if (Found != Current->LocalDecls.end())
Douglas Gregorf3010112011-01-07 16:43:16 +00002235 return &Found->second;
Douglas Gregore9fc8dc2010-12-21 21:22:51 +00002236
2237 // If this is a tag declaration, it's possible that we need to look for
2238 // a previous declaration.
2239 if (const TagDecl *Tag = dyn_cast<TagDecl>(CheckD))
2240 CheckD = Tag->getPreviousDeclaration();
2241 else
2242 CheckD = 0;
2243 } while (CheckD);
2244
Douglas Gregor14cf7522010-04-30 18:55:50 +00002245 // If we aren't combined with our outer scope, we're done.
2246 if (!Current->CombineWithOuterScope)
2247 break;
2248 }
Chris Lattnercab02a62011-02-17 20:34:02 +00002249
2250 // If we didn't find the decl, then we either have a sema bug, or we have a
2251 // forward reference to a label declaration. Return null to indicate that
2252 // we have an uninstantiated label.
2253 assert(isa<LabelDecl>(D) && "declaration not instantiated in this scope");
Douglas Gregor14cf7522010-04-30 18:55:50 +00002254 return 0;
2255}
2256
John McCall19c1bfd2010-08-25 05:32:35 +00002257void LocalInstantiationScope::InstantiatedLocal(const Decl *D, Decl *Inst) {
Douglas Gregorf3010112011-01-07 16:43:16 +00002258 llvm::PointerUnion<Decl *, DeclArgumentPack *> &Stored = LocalDecls[D];
Douglas Gregora8bac7f2011-01-10 07:32:04 +00002259 if (Stored.isNull())
2260 Stored = Inst;
2261 else if (Stored.is<Decl *>()) {
2262 assert(Stored.get<Decl *>() == Inst && "Already instantiated this local");
2263 Stored = Inst;
2264 } else
2265 LocalDecls[D].get<DeclArgumentPack *>()->push_back(Inst);
Douglas Gregor14cf7522010-04-30 18:55:50 +00002266}
Douglas Gregorf3010112011-01-07 16:43:16 +00002267
2268void LocalInstantiationScope::InstantiatedLocalPackArg(const Decl *D,
2269 Decl *Inst) {
2270 DeclArgumentPack *Pack = LocalDecls[D].get<DeclArgumentPack *>();
2271 Pack->push_back(Inst);
2272}
2273
2274void LocalInstantiationScope::MakeInstantiatedLocalArgPack(const Decl *D) {
2275 llvm::PointerUnion<Decl *, DeclArgumentPack *> &Stored = LocalDecls[D];
2276 assert(Stored.isNull() && "Already instantiated this local");
2277 DeclArgumentPack *Pack = new DeclArgumentPack;
2278 Stored = Pack;
2279 ArgumentPacks.push_back(Pack);
2280}
2281
Douglas Gregora8bac7f2011-01-10 07:32:04 +00002282void LocalInstantiationScope::SetPartiallySubstitutedPack(NamedDecl *Pack,
2283 const TemplateArgument *ExplicitArgs,
2284 unsigned NumExplicitArgs) {
2285 assert((!PartiallySubstitutedPack || PartiallySubstitutedPack == Pack) &&
2286 "Already have a partially-substituted pack");
2287 assert((!PartiallySubstitutedPack
2288 || NumArgsInPartiallySubstitutedPack == NumExplicitArgs) &&
2289 "Wrong number of arguments in partially-substituted pack");
2290 PartiallySubstitutedPack = Pack;
2291 ArgsInPartiallySubstitutedPack = ExplicitArgs;
2292 NumArgsInPartiallySubstitutedPack = NumExplicitArgs;
2293}
2294
2295NamedDecl *LocalInstantiationScope::getPartiallySubstitutedPack(
2296 const TemplateArgument **ExplicitArgs,
2297 unsigned *NumExplicitArgs) const {
2298 if (ExplicitArgs)
2299 *ExplicitArgs = 0;
2300 if (NumExplicitArgs)
2301 *NumExplicitArgs = 0;
2302
2303 for (const LocalInstantiationScope *Current = this; Current;
2304 Current = Current->Outer) {
2305 if (Current->PartiallySubstitutedPack) {
2306 if (ExplicitArgs)
2307 *ExplicitArgs = Current->ArgsInPartiallySubstitutedPack;
2308 if (NumExplicitArgs)
2309 *NumExplicitArgs = Current->NumArgsInPartiallySubstitutedPack;
2310
2311 return Current->PartiallySubstitutedPack;
2312 }
2313
2314 if (!Current->CombineWithOuterScope)
2315 break;
2316 }
2317
2318 return 0;
2319}