blob: f58e744578a5b00bba83992b7e19b3f1f45793d3 [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;
98 }
99
John McCall970d5302009-08-29 03:16:09 +0000100 // If this is a friend declaration and it declares an entity at
101 // namespace scope, take arguments from its lexical parent
Douglas Gregor1bd7a942010-05-03 23:29:10 +0000102 // instead of its semantic parent, unless of course the pattern we're
103 // instantiating actually comes from the file's context!
John McCall970d5302009-08-29 03:16:09 +0000104 if (Function->getFriendObjectKind() &&
Douglas Gregor1bd7a942010-05-03 23:29:10 +0000105 Function->getDeclContext()->isFileContext() &&
106 (!Pattern || !Pattern->getLexicalDeclContext()->isFileContext())) {
John McCall970d5302009-08-29 03:16:09 +0000107 Ctx = Function->getLexicalDeclContext();
Douglas Gregor8c702532010-02-05 07:33:43 +0000108 RelativeToPrimary = false;
John McCall970d5302009-08-29 03:16:09 +0000109 continue;
110 }
Douglas Gregor9961ce92010-07-08 18:37:38 +0000111 } else if (CXXRecordDecl *Rec = dyn_cast<CXXRecordDecl>(Ctx)) {
112 if (ClassTemplateDecl *ClassTemplate = Rec->getDescribedClassTemplate()) {
113 QualType T = ClassTemplate->getInjectedClassNameSpecialization();
114 const TemplateSpecializationType *TST
115 = cast<TemplateSpecializationType>(Context.getCanonicalType(T));
116 Result.addOuterTemplateArguments(TST->getArgs(), TST->getNumArgs());
117 if (ClassTemplate->isMemberSpecialization())
118 break;
119 }
Douglas Gregora654dd82009-08-28 17:37:35 +0000120 }
John McCall970d5302009-08-29 03:16:09 +0000121
122 Ctx = Ctx->getParent();
Douglas Gregor8c702532010-02-05 07:33:43 +0000123 RelativeToPrimary = false;
Douglas Gregorb4850462009-05-14 23:26:13 +0000124 }
Mike Stump11289f42009-09-09 15:08:12 +0000125
Douglas Gregora654dd82009-08-28 17:37:35 +0000126 return Result;
Douglas Gregorb4850462009-05-14 23:26:13 +0000127}
128
Douglas Gregor84d49a22009-11-11 21:54:23 +0000129bool Sema::ActiveTemplateInstantiation::isInstantiationRecord() const {
130 switch (Kind) {
131 case TemplateInstantiation:
132 case DefaultTemplateArgumentInstantiation:
133 case DefaultFunctionArgumentInstantiation:
134 return true;
135
136 case ExplicitTemplateArgumentSubstitution:
137 case DeducedTemplateArgumentSubstitution:
138 case PriorTemplateArgumentSubstitution:
139 case DefaultTemplateArgumentChecking:
140 return false;
141 }
142
143 return true;
144}
145
Douglas Gregorfcd5db32009-03-10 00:06:19 +0000146Sema::InstantiatingTemplate::
147InstantiatingTemplate(Sema &SemaRef, SourceLocation PointOfInstantiation,
Douglas Gregor85673582009-05-18 17:01:57 +0000148 Decl *Entity,
Douglas Gregorfcd5db32009-03-10 00:06:19 +0000149 SourceRange InstantiationRange)
Douglas Gregoredb76852011-01-27 22:31:44 +0000150 : SemaRef(SemaRef),
151 SavedInNonInstantiationSFINAEContext(
152 SemaRef.InNonInstantiationSFINAEContext)
153{
Douglas Gregor79cf6032009-03-10 20:44:00 +0000154 Invalid = CheckInstantiationDepth(PointOfInstantiation,
155 InstantiationRange);
156 if (!Invalid) {
Douglas Gregorfcd5db32009-03-10 00:06:19 +0000157 ActiveTemplateInstantiation Inst;
Douglas Gregor79cf6032009-03-10 20:44:00 +0000158 Inst.Kind = ActiveTemplateInstantiation::TemplateInstantiation;
Douglas Gregorfcd5db32009-03-10 00:06:19 +0000159 Inst.PointOfInstantiation = PointOfInstantiation;
Douglas Gregor79cf6032009-03-10 20:44:00 +0000160 Inst.Entity = reinterpret_cast<uintptr_t>(Entity);
Douglas Gregorc9220832009-03-12 18:36:18 +0000161 Inst.TemplateArgs = 0;
162 Inst.NumTemplateArgs = 0;
Douglas Gregor79cf6032009-03-10 20:44:00 +0000163 Inst.InstantiationRange = InstantiationRange;
Douglas Gregoredb76852011-01-27 22:31:44 +0000164 SemaRef.InNonInstantiationSFINAEContext = false;
Douglas Gregor79cf6032009-03-10 20:44:00 +0000165 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
Douglas Gregor79cf6032009-03-10 20:44:00 +0000166 }
167}
168
Mike Stump11289f42009-09-09 15:08:12 +0000169Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef,
Douglas Gregor79cf6032009-03-10 20:44:00 +0000170 SourceLocation PointOfInstantiation,
171 TemplateDecl *Template,
172 const TemplateArgument *TemplateArgs,
173 unsigned NumTemplateArgs,
174 SourceRange InstantiationRange)
Douglas Gregoredb76852011-01-27 22:31:44 +0000175 : SemaRef(SemaRef),
176 SavedInNonInstantiationSFINAEContext(
177 SemaRef.InNonInstantiationSFINAEContext)
178{
Douglas Gregor79cf6032009-03-10 20:44:00 +0000179 Invalid = CheckInstantiationDepth(PointOfInstantiation,
180 InstantiationRange);
181 if (!Invalid) {
182 ActiveTemplateInstantiation Inst;
Mike Stump11289f42009-09-09 15:08:12 +0000183 Inst.Kind
Douglas Gregor79cf6032009-03-10 20:44:00 +0000184 = ActiveTemplateInstantiation::DefaultTemplateArgumentInstantiation;
185 Inst.PointOfInstantiation = PointOfInstantiation;
186 Inst.Entity = reinterpret_cast<uintptr_t>(Template);
187 Inst.TemplateArgs = TemplateArgs;
188 Inst.NumTemplateArgs = NumTemplateArgs;
Douglas Gregorfcd5db32009-03-10 00:06:19 +0000189 Inst.InstantiationRange = InstantiationRange;
Douglas Gregoredb76852011-01-27 22:31:44 +0000190 SemaRef.InNonInstantiationSFINAEContext = false;
Douglas Gregorfcd5db32009-03-10 00:06:19 +0000191 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
Douglas Gregorfcd5db32009-03-10 00:06:19 +0000192 }
193}
194
Mike Stump11289f42009-09-09 15:08:12 +0000195Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef,
Douglas Gregor637d9982009-06-10 23:47:09 +0000196 SourceLocation PointOfInstantiation,
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000197 FunctionTemplateDecl *FunctionTemplate,
198 const TemplateArgument *TemplateArgs,
199 unsigned NumTemplateArgs,
200 ActiveTemplateInstantiation::InstantiationKind Kind,
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +0000201 sema::TemplateDeductionInfo &DeductionInfo,
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000202 SourceRange InstantiationRange)
Douglas Gregoredb76852011-01-27 22:31:44 +0000203 : SemaRef(SemaRef),
204 SavedInNonInstantiationSFINAEContext(
205 SemaRef.InNonInstantiationSFINAEContext)
206{
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000207 Invalid = CheckInstantiationDepth(PointOfInstantiation,
208 InstantiationRange);
209 if (!Invalid) {
210 ActiveTemplateInstantiation Inst;
211 Inst.Kind = Kind;
212 Inst.PointOfInstantiation = PointOfInstantiation;
213 Inst.Entity = reinterpret_cast<uintptr_t>(FunctionTemplate);
214 Inst.TemplateArgs = TemplateArgs;
215 Inst.NumTemplateArgs = NumTemplateArgs;
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +0000216 Inst.DeductionInfo = &DeductionInfo;
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000217 Inst.InstantiationRange = InstantiationRange;
Douglas Gregoredb76852011-01-27 22:31:44 +0000218 SemaRef.InNonInstantiationSFINAEContext = false;
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000219 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
Douglas Gregor84d49a22009-11-11 21:54:23 +0000220
221 if (!Inst.isInstantiationRecord())
222 ++SemaRef.NonInstantiationEntries;
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000223 }
224}
225
Mike Stump11289f42009-09-09 15:08:12 +0000226Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef,
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000227 SourceLocation PointOfInstantiation,
Douglas Gregor637d9982009-06-10 23:47:09 +0000228 ClassTemplatePartialSpecializationDecl *PartialSpec,
229 const TemplateArgument *TemplateArgs,
230 unsigned NumTemplateArgs,
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +0000231 sema::TemplateDeductionInfo &DeductionInfo,
Douglas Gregor637d9982009-06-10 23:47:09 +0000232 SourceRange InstantiationRange)
Douglas Gregoredb76852011-01-27 22:31:44 +0000233 : SemaRef(SemaRef),
234 SavedInNonInstantiationSFINAEContext(
235 SemaRef.InNonInstantiationSFINAEContext)
236{
Douglas Gregor84d49a22009-11-11 21:54:23 +0000237 Invalid = false;
238
239 ActiveTemplateInstantiation Inst;
240 Inst.Kind = ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution;
241 Inst.PointOfInstantiation = PointOfInstantiation;
242 Inst.Entity = reinterpret_cast<uintptr_t>(PartialSpec);
243 Inst.TemplateArgs = TemplateArgs;
244 Inst.NumTemplateArgs = NumTemplateArgs;
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +0000245 Inst.DeductionInfo = &DeductionInfo;
Douglas Gregor84d49a22009-11-11 21:54:23 +0000246 Inst.InstantiationRange = InstantiationRange;
Douglas Gregoredb76852011-01-27 22:31:44 +0000247 SemaRef.InNonInstantiationSFINAEContext = false;
Douglas Gregor84d49a22009-11-11 21:54:23 +0000248 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
249
250 assert(!Inst.isInstantiationRecord());
251 ++SemaRef.NonInstantiationEntries;
Douglas Gregor637d9982009-06-10 23:47:09 +0000252}
253
Mike Stump11289f42009-09-09 15:08:12 +0000254Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef,
Douglas Gregore62e6a02009-11-11 19:13:48 +0000255 SourceLocation PointOfInstantiation,
Anders Carlsson657bad42009-09-05 05:14:19 +0000256 ParmVarDecl *Param,
257 const TemplateArgument *TemplateArgs,
258 unsigned NumTemplateArgs,
259 SourceRange InstantiationRange)
Douglas Gregoredb76852011-01-27 22:31:44 +0000260 : SemaRef(SemaRef),
261 SavedInNonInstantiationSFINAEContext(
262 SemaRef.InNonInstantiationSFINAEContext)
263{
Douglas Gregore62e6a02009-11-11 19:13:48 +0000264 Invalid = CheckInstantiationDepth(PointOfInstantiation, InstantiationRange);
Anders Carlsson657bad42009-09-05 05:14:19 +0000265
266 if (!Invalid) {
267 ActiveTemplateInstantiation Inst;
268 Inst.Kind
269 = ActiveTemplateInstantiation::DefaultFunctionArgumentInstantiation;
Douglas Gregore62e6a02009-11-11 19:13:48 +0000270 Inst.PointOfInstantiation = PointOfInstantiation;
Anders Carlsson657bad42009-09-05 05:14:19 +0000271 Inst.Entity = reinterpret_cast<uintptr_t>(Param);
272 Inst.TemplateArgs = TemplateArgs;
273 Inst.NumTemplateArgs = NumTemplateArgs;
274 Inst.InstantiationRange = InstantiationRange;
Douglas Gregoredb76852011-01-27 22:31:44 +0000275 SemaRef.InNonInstantiationSFINAEContext = false;
Anders Carlsson657bad42009-09-05 05:14:19 +0000276 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
Douglas Gregore62e6a02009-11-11 19:13:48 +0000277 }
278}
279
280Sema::InstantiatingTemplate::
281InstantiatingTemplate(Sema &SemaRef, SourceLocation PointOfInstantiation,
Douglas Gregorca4686d2011-01-04 23:35:54 +0000282 NamedDecl *Template,
Douglas Gregore62e6a02009-11-11 19:13:48 +0000283 NonTypeTemplateParmDecl *Param,
284 const TemplateArgument *TemplateArgs,
285 unsigned NumTemplateArgs,
Douglas Gregoredb76852011-01-27 22:31:44 +0000286 SourceRange InstantiationRange)
287 : SemaRef(SemaRef),
288 SavedInNonInstantiationSFINAEContext(
289 SemaRef.InNonInstantiationSFINAEContext)
290{
Douglas Gregor84d49a22009-11-11 21:54:23 +0000291 Invalid = false;
Douglas Gregore62e6a02009-11-11 19:13:48 +0000292
Douglas Gregor84d49a22009-11-11 21:54:23 +0000293 ActiveTemplateInstantiation Inst;
294 Inst.Kind = ActiveTemplateInstantiation::PriorTemplateArgumentSubstitution;
295 Inst.PointOfInstantiation = PointOfInstantiation;
296 Inst.Template = Template;
297 Inst.Entity = reinterpret_cast<uintptr_t>(Param);
298 Inst.TemplateArgs = TemplateArgs;
299 Inst.NumTemplateArgs = NumTemplateArgs;
300 Inst.InstantiationRange = InstantiationRange;
Douglas Gregoredb76852011-01-27 22:31:44 +0000301 SemaRef.InNonInstantiationSFINAEContext = false;
Douglas Gregor84d49a22009-11-11 21:54:23 +0000302 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
303
304 assert(!Inst.isInstantiationRecord());
305 ++SemaRef.NonInstantiationEntries;
Douglas Gregore62e6a02009-11-11 19:13:48 +0000306}
307
308Sema::InstantiatingTemplate::
309InstantiatingTemplate(Sema &SemaRef, SourceLocation PointOfInstantiation,
Douglas Gregorca4686d2011-01-04 23:35:54 +0000310 NamedDecl *Template,
Douglas Gregore62e6a02009-11-11 19:13:48 +0000311 TemplateTemplateParmDecl *Param,
312 const TemplateArgument *TemplateArgs,
313 unsigned NumTemplateArgs,
Douglas Gregoredb76852011-01-27 22:31:44 +0000314 SourceRange InstantiationRange)
315 : SemaRef(SemaRef),
316 SavedInNonInstantiationSFINAEContext(
317 SemaRef.InNonInstantiationSFINAEContext)
318{
Douglas Gregor84d49a22009-11-11 21:54:23 +0000319 Invalid = false;
320 ActiveTemplateInstantiation Inst;
321 Inst.Kind = ActiveTemplateInstantiation::PriorTemplateArgumentSubstitution;
322 Inst.PointOfInstantiation = PointOfInstantiation;
323 Inst.Template = Template;
324 Inst.Entity = reinterpret_cast<uintptr_t>(Param);
325 Inst.TemplateArgs = TemplateArgs;
326 Inst.NumTemplateArgs = NumTemplateArgs;
327 Inst.InstantiationRange = InstantiationRange;
Douglas Gregoredb76852011-01-27 22:31:44 +0000328 SemaRef.InNonInstantiationSFINAEContext = false;
Douglas Gregor84d49a22009-11-11 21:54:23 +0000329 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
Douglas Gregore62e6a02009-11-11 19:13:48 +0000330
Douglas Gregor84d49a22009-11-11 21:54:23 +0000331 assert(!Inst.isInstantiationRecord());
332 ++SemaRef.NonInstantiationEntries;
333}
334
335Sema::InstantiatingTemplate::
336InstantiatingTemplate(Sema &SemaRef, SourceLocation PointOfInstantiation,
337 TemplateDecl *Template,
338 NamedDecl *Param,
339 const TemplateArgument *TemplateArgs,
340 unsigned NumTemplateArgs,
Douglas Gregoredb76852011-01-27 22:31:44 +0000341 SourceRange InstantiationRange)
342 : SemaRef(SemaRef),
343 SavedInNonInstantiationSFINAEContext(
344 SemaRef.InNonInstantiationSFINAEContext)
345{
Douglas Gregor84d49a22009-11-11 21:54:23 +0000346 Invalid = false;
347
348 ActiveTemplateInstantiation Inst;
349 Inst.Kind = ActiveTemplateInstantiation::DefaultTemplateArgumentChecking;
350 Inst.PointOfInstantiation = PointOfInstantiation;
351 Inst.Template = Template;
352 Inst.Entity = reinterpret_cast<uintptr_t>(Param);
353 Inst.TemplateArgs = TemplateArgs;
354 Inst.NumTemplateArgs = NumTemplateArgs;
355 Inst.InstantiationRange = InstantiationRange;
Douglas Gregoredb76852011-01-27 22:31:44 +0000356 SemaRef.InNonInstantiationSFINAEContext = false;
Douglas Gregor84d49a22009-11-11 21:54:23 +0000357 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
358
359 assert(!Inst.isInstantiationRecord());
360 ++SemaRef.NonInstantiationEntries;
Anders Carlsson657bad42009-09-05 05:14:19 +0000361}
362
Douglas Gregor85673582009-05-18 17:01:57 +0000363void Sema::InstantiatingTemplate::Clear() {
364 if (!Invalid) {
Douglas Gregor84d49a22009-11-11 21:54:23 +0000365 if (!SemaRef.ActiveTemplateInstantiations.back().isInstantiationRecord()) {
366 assert(SemaRef.NonInstantiationEntries > 0);
367 --SemaRef.NonInstantiationEntries;
368 }
Douglas Gregoredb76852011-01-27 22:31:44 +0000369 SemaRef.InNonInstantiationSFINAEContext
370 = SavedInNonInstantiationSFINAEContext;
Douglas Gregorfcd5db32009-03-10 00:06:19 +0000371 SemaRef.ActiveTemplateInstantiations.pop_back();
Douglas Gregor85673582009-05-18 17:01:57 +0000372 Invalid = true;
373 }
Douglas Gregorfcd5db32009-03-10 00:06:19 +0000374}
375
Douglas Gregor79cf6032009-03-10 20:44:00 +0000376bool Sema::InstantiatingTemplate::CheckInstantiationDepth(
377 SourceLocation PointOfInstantiation,
378 SourceRange InstantiationRange) {
Douglas Gregor84d49a22009-11-11 21:54:23 +0000379 assert(SemaRef.NonInstantiationEntries <=
380 SemaRef.ActiveTemplateInstantiations.size());
381 if ((SemaRef.ActiveTemplateInstantiations.size() -
382 SemaRef.NonInstantiationEntries)
383 <= SemaRef.getLangOptions().InstantiationDepth)
Douglas Gregor79cf6032009-03-10 20:44:00 +0000384 return false;
385
Mike Stump11289f42009-09-09 15:08:12 +0000386 SemaRef.Diag(PointOfInstantiation,
Douglas Gregor79cf6032009-03-10 20:44:00 +0000387 diag::err_template_recursion_depth_exceeded)
388 << SemaRef.getLangOptions().InstantiationDepth
389 << InstantiationRange;
390 SemaRef.Diag(PointOfInstantiation, diag::note_template_recursion_depth)
391 << SemaRef.getLangOptions().InstantiationDepth;
392 return true;
393}
394
Douglas Gregor4ea568f2009-03-10 18:03:33 +0000395/// \brief Prints the current instantiation stack through a series of
396/// notes.
397void Sema::PrintInstantiationStack() {
Douglas Gregorffed1cb2010-04-20 07:18:24 +0000398 // Determine which template instantiations to skip, if any.
399 unsigned SkipStart = ActiveTemplateInstantiations.size(), SkipEnd = SkipStart;
400 unsigned Limit = Diags.getTemplateBacktraceLimit();
401 if (Limit && Limit < ActiveTemplateInstantiations.size()) {
402 SkipStart = Limit / 2 + Limit % 2;
403 SkipEnd = ActiveTemplateInstantiations.size() - Limit / 2;
404 }
405
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000406 // FIXME: In all of these cases, we need to show the template arguments
Douglas Gregorffed1cb2010-04-20 07:18:24 +0000407 unsigned InstantiationIdx = 0;
Douglas Gregor4ea568f2009-03-10 18:03:33 +0000408 for (llvm::SmallVector<ActiveTemplateInstantiation, 16>::reverse_iterator
409 Active = ActiveTemplateInstantiations.rbegin(),
410 ActiveEnd = ActiveTemplateInstantiations.rend();
411 Active != ActiveEnd;
Douglas Gregorffed1cb2010-04-20 07:18:24 +0000412 ++Active, ++InstantiationIdx) {
413 // Skip this instantiation?
414 if (InstantiationIdx >= SkipStart && InstantiationIdx < SkipEnd) {
415 if (InstantiationIdx == SkipStart) {
416 // Note that we're skipping instantiations.
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000417 Diags.Report(Active->PointOfInstantiation,
Douglas Gregorffed1cb2010-04-20 07:18:24 +0000418 diag::note_instantiation_contexts_suppressed)
419 << unsigned(ActiveTemplateInstantiations.size() - Limit);
420 }
421 continue;
422 }
423
Douglas Gregor79cf6032009-03-10 20:44:00 +0000424 switch (Active->Kind) {
425 case ActiveTemplateInstantiation::TemplateInstantiation: {
Douglas Gregor85673582009-05-18 17:01:57 +0000426 Decl *D = reinterpret_cast<Decl *>(Active->Entity);
427 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) {
428 unsigned DiagID = diag::note_template_member_class_here;
429 if (isa<ClassTemplateSpecializationDecl>(Record))
430 DiagID = diag::note_template_class_instantiation_here;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000431 Diags.Report(Active->PointOfInstantiation, DiagID)
Douglas Gregor85673582009-05-18 17:01:57 +0000432 << Context.getTypeDeclType(Record)
433 << Active->InstantiationRange;
Douglas Gregora6ef8f02009-07-24 20:34:43 +0000434 } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
Douglas Gregor4adbc6d2009-06-26 00:10:03 +0000435 unsigned DiagID;
436 if (Function->getPrimaryTemplate())
437 DiagID = diag::note_function_template_spec_here;
438 else
439 DiagID = diag::note_template_member_function_here;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000440 Diags.Report(Active->PointOfInstantiation, DiagID)
Douglas Gregor85673582009-05-18 17:01:57 +0000441 << Function
442 << Active->InstantiationRange;
Douglas Gregora6ef8f02009-07-24 20:34:43 +0000443 } else {
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000444 Diags.Report(Active->PointOfInstantiation,
Douglas Gregora6ef8f02009-07-24 20:34:43 +0000445 diag::note_template_static_data_member_def_here)
446 << cast<VarDecl>(D)
447 << Active->InstantiationRange;
Douglas Gregor85673582009-05-18 17:01:57 +0000448 }
Douglas Gregor79cf6032009-03-10 20:44:00 +0000449 break;
450 }
451
452 case ActiveTemplateInstantiation::DefaultTemplateArgumentInstantiation: {
453 TemplateDecl *Template = cast<TemplateDecl>((Decl *)Active->Entity);
454 std::string TemplateArgsStr
Douglas Gregordc572a32009-03-30 22:58:21 +0000455 = TemplateSpecializationType::PrintTemplateArgumentList(
Mike Stump11289f42009-09-09 15:08:12 +0000456 Active->TemplateArgs,
Douglas Gregor7de59662009-05-29 20:38:28 +0000457 Active->NumTemplateArgs,
458 Context.PrintingPolicy);
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000459 Diags.Report(Active->PointOfInstantiation,
Douglas Gregor79cf6032009-03-10 20:44:00 +0000460 diag::note_default_arg_instantiation_here)
461 << (Template->getNameAsString() + TemplateArgsStr)
462 << Active->InstantiationRange;
463 break;
464 }
Douglas Gregor637d9982009-06-10 23:47:09 +0000465
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000466 case ActiveTemplateInstantiation::ExplicitTemplateArgumentSubstitution: {
Mike Stump11289f42009-09-09 15:08:12 +0000467 FunctionTemplateDecl *FnTmpl
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000468 = cast<FunctionTemplateDecl>((Decl *)Active->Entity);
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000469 Diags.Report(Active->PointOfInstantiation,
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000470 diag::note_explicit_template_arg_substitution_here)
Douglas Gregor607f1412010-03-30 20:35:20 +0000471 << FnTmpl
472 << getTemplateArgumentBindingsText(FnTmpl->getTemplateParameters(),
473 Active->TemplateArgs,
474 Active->NumTemplateArgs)
475 << Active->InstantiationRange;
Douglas Gregor637d9982009-06-10 23:47:09 +0000476 break;
477 }
Mike Stump11289f42009-09-09 15:08:12 +0000478
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000479 case ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution:
480 if (ClassTemplatePartialSpecializationDecl *PartialSpec
481 = dyn_cast<ClassTemplatePartialSpecializationDecl>(
482 (Decl *)Active->Entity)) {
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000483 Diags.Report(Active->PointOfInstantiation,
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000484 diag::note_partial_spec_deduct_instantiation_here)
485 << Context.getTypeDeclType(PartialSpec)
Douglas Gregor607f1412010-03-30 20:35:20 +0000486 << getTemplateArgumentBindingsText(
487 PartialSpec->getTemplateParameters(),
488 Active->TemplateArgs,
489 Active->NumTemplateArgs)
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000490 << Active->InstantiationRange;
491 } else {
492 FunctionTemplateDecl *FnTmpl
493 = cast<FunctionTemplateDecl>((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_function_template_deduction_instantiation_here)
Douglas Gregor607f1412010-03-30 20:35:20 +0000496 << FnTmpl
497 << getTemplateArgumentBindingsText(FnTmpl->getTemplateParameters(),
498 Active->TemplateArgs,
499 Active->NumTemplateArgs)
500 << Active->InstantiationRange;
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000501 }
502 break;
Douglas Gregor637d9982009-06-10 23:47:09 +0000503
Anders Carlsson657bad42009-09-05 05:14:19 +0000504 case ActiveTemplateInstantiation::DefaultFunctionArgumentInstantiation: {
505 ParmVarDecl *Param = cast<ParmVarDecl>((Decl *)Active->Entity);
506 FunctionDecl *FD = cast<FunctionDecl>(Param->getDeclContext());
Mike Stump11289f42009-09-09 15:08:12 +0000507
Anders Carlsson657bad42009-09-05 05:14:19 +0000508 std::string TemplateArgsStr
509 = TemplateSpecializationType::PrintTemplateArgumentList(
Mike Stump11289f42009-09-09 15:08:12 +0000510 Active->TemplateArgs,
Anders Carlsson657bad42009-09-05 05:14:19 +0000511 Active->NumTemplateArgs,
512 Context.PrintingPolicy);
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000513 Diags.Report(Active->PointOfInstantiation,
Anders Carlsson657bad42009-09-05 05:14:19 +0000514 diag::note_default_function_arg_instantiation_here)
Anders Carlssondc6d2c32009-09-05 05:38:54 +0000515 << (FD->getNameAsString() + TemplateArgsStr)
Anders Carlsson657bad42009-09-05 05:14:19 +0000516 << Active->InstantiationRange;
517 break;
518 }
Mike Stump11289f42009-09-09 15:08:12 +0000519
Douglas Gregore62e6a02009-11-11 19:13:48 +0000520 case ActiveTemplateInstantiation::PriorTemplateArgumentSubstitution: {
521 NamedDecl *Parm = cast<NamedDecl>((Decl *)Active->Entity);
522 std::string Name;
523 if (!Parm->getName().empty())
524 Name = std::string(" '") + Parm->getName().str() + "'";
Douglas Gregorca4686d2011-01-04 23:35:54 +0000525
526 TemplateParameterList *TemplateParams = 0;
527 if (TemplateDecl *Template = dyn_cast<TemplateDecl>(Active->Template))
528 TemplateParams = Template->getTemplateParameters();
529 else
530 TemplateParams =
531 cast<ClassTemplatePartialSpecializationDecl>(Active->Template)
532 ->getTemplateParameters();
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000533 Diags.Report(Active->PointOfInstantiation,
Douglas Gregore62e6a02009-11-11 19:13:48 +0000534 diag::note_prior_template_arg_substitution)
535 << isa<TemplateTemplateParmDecl>(Parm)
536 << Name
Douglas Gregorca4686d2011-01-04 23:35:54 +0000537 << getTemplateArgumentBindingsText(TemplateParams,
Douglas Gregore62e6a02009-11-11 19:13:48 +0000538 Active->TemplateArgs,
539 Active->NumTemplateArgs)
540 << Active->InstantiationRange;
541 break;
542 }
Douglas Gregor84d49a22009-11-11 21:54:23 +0000543
544 case ActiveTemplateInstantiation::DefaultTemplateArgumentChecking: {
Douglas Gregorca4686d2011-01-04 23:35:54 +0000545 TemplateParameterList *TemplateParams = 0;
546 if (TemplateDecl *Template = dyn_cast<TemplateDecl>(Active->Template))
547 TemplateParams = Template->getTemplateParameters();
548 else
549 TemplateParams =
550 cast<ClassTemplatePartialSpecializationDecl>(Active->Template)
551 ->getTemplateParameters();
552
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000553 Diags.Report(Active->PointOfInstantiation,
Douglas Gregor84d49a22009-11-11 21:54:23 +0000554 diag::note_template_default_arg_checking)
Douglas Gregorca4686d2011-01-04 23:35:54 +0000555 << getTemplateArgumentBindingsText(TemplateParams,
Douglas Gregor84d49a22009-11-11 21:54:23 +0000556 Active->TemplateArgs,
557 Active->NumTemplateArgs)
558 << Active->InstantiationRange;
559 break;
560 }
Douglas Gregor79cf6032009-03-10 20:44:00 +0000561 }
Douglas Gregor4ea568f2009-03-10 18:03:33 +0000562 }
563}
564
Douglas Gregoredb76852011-01-27 22:31:44 +0000565llvm::Optional<TemplateDeductionInfo *> Sema::isSFINAEContext() const {
Douglas Gregor33834512009-06-14 07:33:30 +0000566 using llvm::SmallVector;
Douglas Gregoredb76852011-01-27 22:31:44 +0000567 if (InNonInstantiationSFINAEContext)
568 return llvm::Optional<TemplateDeductionInfo *>(0);
569
Douglas Gregor33834512009-06-14 07:33:30 +0000570 for (SmallVector<ActiveTemplateInstantiation, 16>::const_reverse_iterator
571 Active = ActiveTemplateInstantiations.rbegin(),
572 ActiveEnd = ActiveTemplateInstantiations.rend();
573 Active != ActiveEnd;
Douglas Gregor84d49a22009-11-11 21:54:23 +0000574 ++Active)
575 {
Douglas Gregor33834512009-06-14 07:33:30 +0000576 switch(Active->Kind) {
Anders Carlsson657bad42009-09-05 05:14:19 +0000577 case ActiveTemplateInstantiation::DefaultFunctionArgumentInstantiation:
Douglas Gregoredb76852011-01-27 22:31:44 +0000578 case ActiveTemplateInstantiation::TemplateInstantiation:
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000579 // This is a template instantiation, so there is no SFINAE.
Douglas Gregoredb76852011-01-27 22:31:44 +0000580 return llvm::Optional<TemplateDeductionInfo *>();
Mike Stump11289f42009-09-09 15:08:12 +0000581
Douglas Gregor33834512009-06-14 07:33:30 +0000582 case ActiveTemplateInstantiation::DefaultTemplateArgumentInstantiation:
Douglas Gregore62e6a02009-11-11 19:13:48 +0000583 case ActiveTemplateInstantiation::PriorTemplateArgumentSubstitution:
Douglas Gregor84d49a22009-11-11 21:54:23 +0000584 case ActiveTemplateInstantiation::DefaultTemplateArgumentChecking:
Douglas Gregore62e6a02009-11-11 19:13:48 +0000585 // A default template argument instantiation and substitution into
586 // template parameters with arguments for prior parameters may or may
587 // not be a SFINAE context; look further up the stack.
Douglas Gregor33834512009-06-14 07:33:30 +0000588 break;
Mike Stump11289f42009-09-09 15:08:12 +0000589
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000590 case ActiveTemplateInstantiation::ExplicitTemplateArgumentSubstitution:
591 case ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution:
592 // We're either substitution explicitly-specified template arguments
593 // or deduced template arguments, so SFINAE applies.
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +0000594 assert(Active->DeductionInfo && "Missing deduction info pointer");
595 return Active->DeductionInfo;
Douglas Gregor33834512009-06-14 07:33:30 +0000596 }
597 }
598
Douglas Gregoredb76852011-01-27 22:31:44 +0000599 return llvm::Optional<TemplateDeductionInfo *>();
Douglas Gregor33834512009-06-14 07:33:30 +0000600}
601
Douglas Gregora8bac7f2011-01-10 07:32:04 +0000602/// \brief Retrieve the depth and index of a parameter pack.
603static std::pair<unsigned, unsigned>
604getDepthAndIndex(NamedDecl *ND) {
605 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(ND))
606 return std::make_pair(TTP->getDepth(), TTP->getIndex());
607
608 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(ND))
609 return std::make_pair(NTTP->getDepth(), NTTP->getIndex());
610
611 TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(ND);
612 return std::make_pair(TTP->getDepth(), TTP->getIndex());
613}
614
Douglas Gregorfe1e1102009-02-27 19:31:52 +0000615//===----------------------------------------------------------------------===/
616// Template Instantiation for Types
617//===----------------------------------------------------------------------===/
Douglas Gregor17c0d7b2009-02-28 00:25:32 +0000618namespace {
Douglas Gregor14cf7522010-04-30 18:55:50 +0000619 class TemplateInstantiator : public TreeTransform<TemplateInstantiator> {
Douglas Gregor01afeef2009-08-28 20:31:08 +0000620 const MultiLevelTemplateArgumentList &TemplateArgs;
Douglas Gregor17c0d7b2009-02-28 00:25:32 +0000621 SourceLocation Loc;
622 DeclarationName Entity;
Douglas Gregorfe1e1102009-02-27 19:31:52 +0000623
Douglas Gregor17c0d7b2009-02-28 00:25:32 +0000624 public:
Douglas Gregorebe10102009-08-20 07:17:43 +0000625 typedef TreeTransform<TemplateInstantiator> inherited;
Mike Stump11289f42009-09-09 15:08:12 +0000626
627 TemplateInstantiator(Sema &SemaRef,
Douglas Gregor01afeef2009-08-28 20:31:08 +0000628 const MultiLevelTemplateArgumentList &TemplateArgs,
Douglas Gregord6ff3322009-08-04 16:50:30 +0000629 SourceLocation Loc,
Mike Stump11289f42009-09-09 15:08:12 +0000630 DeclarationName Entity)
631 : inherited(SemaRef), TemplateArgs(TemplateArgs), Loc(Loc),
Douglas Gregorebe10102009-08-20 07:17:43 +0000632 Entity(Entity) { }
Douglas Gregor17c0d7b2009-02-28 00:25:32 +0000633
Mike Stump11289f42009-09-09 15:08:12 +0000634 /// \brief Determine whether the given type \p T has already been
Douglas Gregord6ff3322009-08-04 16:50:30 +0000635 /// transformed.
636 ///
637 /// For the purposes of template instantiation, a type has already been
638 /// transformed if it is NULL or if it is not dependent.
Douglas Gregor5597ab42010-05-07 23:12:07 +0000639 bool AlreadyTransformed(QualType T);
Mike Stump11289f42009-09-09 15:08:12 +0000640
Douglas Gregord6ff3322009-08-04 16:50:30 +0000641 /// \brief Returns the location of the entity being instantiated, if known.
642 SourceLocation getBaseLocation() { return Loc; }
Mike Stump11289f42009-09-09 15:08:12 +0000643
Douglas Gregord6ff3322009-08-04 16:50:30 +0000644 /// \brief Returns the name of the entity being instantiated, if any.
645 DeclarationName getBaseEntity() { return Entity; }
Mike Stump11289f42009-09-09 15:08:12 +0000646
Douglas Gregoref6ab412009-10-27 06:26:26 +0000647 /// \brief Sets the "base" location and entity when that
648 /// information is known based on another transformation.
649 void setBase(SourceLocation Loc, DeclarationName Entity) {
650 this->Loc = Loc;
651 this->Entity = Entity;
652 }
Douglas Gregor840bd6c2010-12-20 22:05:00 +0000653
654 bool TryExpandParameterPacks(SourceLocation EllipsisLoc,
655 SourceRange PatternRange,
656 const UnexpandedParameterPack *Unexpanded,
657 unsigned NumUnexpanded,
658 bool &ShouldExpand,
Douglas Gregora8bac7f2011-01-10 07:32:04 +0000659 bool &RetainExpansion,
Douglas Gregor0dca5fd2011-01-14 17:04:44 +0000660 llvm::Optional<unsigned> &NumExpansions) {
Douglas Gregor76aca7b2010-12-21 00:52:54 +0000661 return getSema().CheckParameterPacksForExpansion(EllipsisLoc,
662 PatternRange, Unexpanded,
663 NumUnexpanded,
664 TemplateArgs,
665 ShouldExpand,
Douglas Gregora8bac7f2011-01-10 07:32:04 +0000666 RetainExpansion,
Douglas Gregor76aca7b2010-12-21 00:52:54 +0000667 NumExpansions);
668 }
Douglas Gregor840bd6c2010-12-20 22:05:00 +0000669
Douglas Gregorf3010112011-01-07 16:43:16 +0000670 void ExpandingFunctionParameterPack(ParmVarDecl *Pack) {
671 SemaRef.CurrentInstantiationScope->MakeInstantiatedLocalArgPack(Pack);
672 }
673
Douglas Gregora8bac7f2011-01-10 07:32:04 +0000674 TemplateArgument ForgetPartiallySubstitutedPack() {
675 TemplateArgument Result;
676 if (NamedDecl *PartialPack
677 = SemaRef.CurrentInstantiationScope->getPartiallySubstitutedPack()){
678 MultiLevelTemplateArgumentList &TemplateArgs
679 = const_cast<MultiLevelTemplateArgumentList &>(this->TemplateArgs);
680 unsigned Depth, Index;
681 llvm::tie(Depth, Index) = getDepthAndIndex(PartialPack);
682 if (TemplateArgs.hasTemplateArgument(Depth, Index)) {
683 Result = TemplateArgs(Depth, Index);
684 TemplateArgs.setArgument(Depth, Index, TemplateArgument());
685 }
686 }
687
688 return Result;
689 }
690
691 void RememberPartiallySubstitutedPack(TemplateArgument Arg) {
692 if (Arg.isNull())
693 return;
694
695 if (NamedDecl *PartialPack
696 = SemaRef.CurrentInstantiationScope->getPartiallySubstitutedPack()){
697 MultiLevelTemplateArgumentList &TemplateArgs
698 = const_cast<MultiLevelTemplateArgumentList &>(this->TemplateArgs);
699 unsigned Depth, Index;
700 llvm::tie(Depth, Index) = getDepthAndIndex(PartialPack);
701 TemplateArgs.setArgument(Depth, Index, Arg);
702 }
703 }
704
Douglas Gregord6ff3322009-08-04 16:50:30 +0000705 /// \brief Transform the given declaration by instantiating a reference to
706 /// this declaration.
Douglas Gregora04f2ca2010-03-01 15:56:25 +0000707 Decl *TransformDecl(SourceLocation Loc, Decl *D);
Douglas Gregora16548e2009-08-11 05:31:07 +0000708
Mike Stump11289f42009-09-09 15:08:12 +0000709 /// \brief Transform the definition of the given declaration by
Douglas Gregorebe10102009-08-20 07:17:43 +0000710 /// instantiating it.
Douglas Gregor25289362010-03-01 17:25:41 +0000711 Decl *TransformDefinition(SourceLocation Loc, Decl *D);
Mike Stump11289f42009-09-09 15:08:12 +0000712
Douglas Gregora5cb6da2009-10-20 05:58:46 +0000713 /// \bried Transform the first qualifier within a scope by instantiating the
714 /// declaration.
715 NamedDecl *TransformFirstQualifierInScope(NamedDecl *D, SourceLocation Loc);
716
Douglas Gregorebe10102009-08-20 07:17:43 +0000717 /// \brief Rebuild the exception declaration and register the declaration
718 /// as an instantiated local.
Douglas Gregor9f0e1aa2010-09-09 17:09:21 +0000719 VarDecl *RebuildExceptionDecl(VarDecl *ExceptionDecl,
John McCallbcd03502009-12-07 02:54:59 +0000720 TypeSourceInfo *Declarator,
Douglas Gregorebe10102009-08-20 07:17:43 +0000721 IdentifierInfo *Name,
Douglas Gregor9f0e1aa2010-09-09 17:09:21 +0000722 SourceLocation Loc);
Mike Stump11289f42009-09-09 15:08:12 +0000723
Douglas Gregorf4e837f2010-04-26 17:57:08 +0000724 /// \brief Rebuild the Objective-C exception declaration and register the
725 /// declaration as an instantiated local.
726 VarDecl *RebuildObjCExceptionDecl(VarDecl *ExceptionDecl,
727 TypeSourceInfo *TSInfo, QualType T);
728
John McCall7f41d982009-09-11 04:59:25 +0000729 /// \brief Check for tag mismatches when instantiating an
730 /// elaborated type.
John McCall954b5de2010-11-04 19:04:38 +0000731 QualType RebuildElaboratedType(SourceLocation KeywordLoc,
732 ElaboratedTypeKeyword Keyword,
Abramo Bagnara6150c882010-05-11 21:36:43 +0000733 NestedNameSpecifier *NNS, QualType T);
John McCall7f41d982009-09-11 04:59:25 +0000734
Douglas Gregor5590be02011-01-15 06:45:20 +0000735 TemplateName TransformTemplateName(TemplateName Name,
736 QualType ObjectType = QualType(),
737 NamedDecl *FirstQualifierInScope = 0);
738
John McCalldadc5752010-08-24 06:29:42 +0000739 ExprResult TransformPredefinedExpr(PredefinedExpr *E);
740 ExprResult TransformDeclRefExpr(DeclRefExpr *E);
741 ExprResult TransformCXXDefaultArgExpr(CXXDefaultArgExpr *E);
742 ExprResult TransformTemplateParmRefExpr(DeclRefExpr *E,
Douglas Gregoreb5a39d2010-12-24 00:15:10 +0000743 NonTypeTemplateParmDecl *D);
Douglas Gregorcdbc5392011-01-15 01:15:58 +0000744 ExprResult TransformSubstNonTypeTemplateParmPackExpr(
745 SubstNonTypeTemplateParmPackExpr *E);
746
Douglas Gregor14cf7522010-04-30 18:55:50 +0000747 QualType TransformFunctionProtoType(TypeLocBuilder &TLB,
John McCall31f82722010-11-12 08:19:04 +0000748 FunctionProtoTypeLoc TL);
Douglas Gregor715e4612011-01-14 22:40:04 +0000749 ParmVarDecl *TransformFunctionTypeParam(ParmVarDecl *OldParm,
750 llvm::Optional<unsigned> NumExpansions);
John McCall58f10c32010-03-11 09:03:00 +0000751
Mike Stump11289f42009-09-09 15:08:12 +0000752 /// \brief Transforms a template type parameter type by performing
Douglas Gregord6ff3322009-08-04 16:50:30 +0000753 /// substitution of the corresponding template type argument.
John McCall550e0c22009-10-21 00:40:46 +0000754 QualType TransformTemplateTypeParmType(TypeLocBuilder &TLB,
John McCall31f82722010-11-12 08:19:04 +0000755 TemplateTypeParmTypeLoc TL);
Nick Lewyckyc96c37f2010-07-06 19:51:49 +0000756
Douglas Gregorada4b792011-01-14 02:55:32 +0000757 /// \brief Transforms an already-substituted template type parameter pack
758 /// into either itself (if we aren't substituting into its pack expansion)
759 /// or the appropriate substituted argument.
760 QualType TransformSubstTemplateTypeParmPackType(TypeLocBuilder &TLB,
761 SubstTemplateTypeParmPackTypeLoc TL);
762
John McCalldadc5752010-08-24 06:29:42 +0000763 ExprResult TransformCallExpr(CallExpr *CE) {
Nick Lewyckyc96c37f2010-07-06 19:51:49 +0000764 getSema().CallsUndergoingInstantiation.push_back(CE);
John McCalldadc5752010-08-24 06:29:42 +0000765 ExprResult Result =
Nick Lewyckyc96c37f2010-07-06 19:51:49 +0000766 TreeTransform<TemplateInstantiator>::TransformCallExpr(CE);
767 getSema().CallsUndergoingInstantiation.pop_back();
768 return move(Result);
769 }
Douglas Gregord6ff3322009-08-04 16:50:30 +0000770 };
Douglas Gregor04318252009-07-06 15:59:29 +0000771}
772
Douglas Gregor5597ab42010-05-07 23:12:07 +0000773bool TemplateInstantiator::AlreadyTransformed(QualType T) {
774 if (T.isNull())
775 return true;
776
Douglas Gregor5a5073e2010-05-24 17:22:01 +0000777 if (T->isDependentType() || T->isVariablyModifiedType())
Douglas Gregor5597ab42010-05-07 23:12:07 +0000778 return false;
779
780 getSema().MarkDeclarationsReferencedInType(Loc, T);
781 return true;
782}
783
Douglas Gregora04f2ca2010-03-01 15:56:25 +0000784Decl *TemplateInstantiator::TransformDecl(SourceLocation Loc, Decl *D) {
Douglas Gregor2b6ca462009-09-03 21:38:09 +0000785 if (!D)
786 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000787
Douglas Gregor2b6ca462009-09-03 21:38:09 +0000788 if (TemplateTemplateParmDecl *TTP = dyn_cast<TemplateTemplateParmDecl>(D)) {
Douglas Gregor01afeef2009-08-28 20:31:08 +0000789 if (TTP->getDepth() < TemplateArgs.getNumLevels()) {
Douglas Gregorb93971082010-02-05 19:54:12 +0000790 // If the corresponding template argument is NULL or non-existent, it's
791 // because we are performing instantiation from explicitly-specified
792 // template arguments in a function template, but there were some
793 // arguments left unspecified.
794 if (!TemplateArgs.hasTemplateArgument(TTP->getDepth(),
795 TTP->getPosition()))
796 return D;
797
Douglas Gregorf5500772011-01-05 15:48:55 +0000798 TemplateArgument Arg = TemplateArgs(TTP->getDepth(), TTP->getPosition());
799
800 if (TTP->isParameterPack()) {
801 assert(Arg.getKind() == TemplateArgument::Pack &&
802 "Missing argument pack");
803
Douglas Gregor5590be02011-01-15 06:45:20 +0000804 assert(getSema().ArgumentPackSubstitutionIndex >= 0);
Douglas Gregora8bac7f2011-01-10 07:32:04 +0000805 assert(getSema().ArgumentPackSubstitutionIndex < (int)Arg.pack_size());
Douglas Gregorf5500772011-01-05 15:48:55 +0000806 Arg = Arg.pack_begin()[getSema().ArgumentPackSubstitutionIndex];
807 }
808
809 TemplateName Template = Arg.getAsTemplate();
Douglas Gregor9167f8b2009-11-11 01:00:40 +0000810 assert(!Template.isNull() && Template.getAsTemplateDecl() &&
Douglas Gregor01afeef2009-08-28 20:31:08 +0000811 "Wrong kind of template template argument");
Douglas Gregor9167f8b2009-11-11 01:00:40 +0000812 return Template.getAsTemplateDecl();
Douglas Gregor01afeef2009-08-28 20:31:08 +0000813 }
Mike Stump11289f42009-09-09 15:08:12 +0000814
Douglas Gregor9167f8b2009-11-11 01:00:40 +0000815 // Fall through to find the instantiated declaration for this template
816 // template parameter.
Douglas Gregor71dc5092009-08-06 06:41:21 +0000817 }
Mike Stump11289f42009-09-09 15:08:12 +0000818
Douglas Gregora04f2ca2010-03-01 15:56:25 +0000819 return SemaRef.FindInstantiatedDecl(Loc, cast<NamedDecl>(D), TemplateArgs);
Douglas Gregord6ff3322009-08-04 16:50:30 +0000820}
821
Douglas Gregor25289362010-03-01 17:25:41 +0000822Decl *TemplateInstantiator::TransformDefinition(SourceLocation Loc, Decl *D) {
John McCall76d824f2009-08-25 22:02:44 +0000823 Decl *Inst = getSema().SubstDecl(D, getSema().CurContext, TemplateArgs);
Douglas Gregorebe10102009-08-20 07:17:43 +0000824 if (!Inst)
825 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000826
Douglas Gregorebe10102009-08-20 07:17:43 +0000827 getSema().CurrentInstantiationScope->InstantiatedLocal(D, Inst);
828 return Inst;
829}
830
Douglas Gregora5cb6da2009-10-20 05:58:46 +0000831NamedDecl *
832TemplateInstantiator::TransformFirstQualifierInScope(NamedDecl *D,
833 SourceLocation Loc) {
834 // If the first part of the nested-name-specifier was a template type
835 // parameter, instantiate that type parameter down to a tag type.
836 if (TemplateTypeParmDecl *TTPD = dyn_cast_or_null<TemplateTypeParmDecl>(D)) {
837 const TemplateTypeParmType *TTP
838 = cast<TemplateTypeParmType>(getSema().Context.getTypeDeclType(TTPD));
Douglas Gregor53c3f4e2010-12-20 22:48:17 +0000839
Douglas Gregora5cb6da2009-10-20 05:58:46 +0000840 if (TTP->getDepth() < TemplateArgs.getNumLevels()) {
Douglas Gregor53c3f4e2010-12-20 22:48:17 +0000841 // FIXME: This needs testing w/ member access expressions.
842 TemplateArgument Arg = TemplateArgs(TTP->getDepth(), TTP->getIndex());
843
844 if (TTP->isParameterPack()) {
845 assert(Arg.getKind() == TemplateArgument::Pack &&
846 "Missing argument pack");
847
Douglas Gregore1d60df2011-01-14 23:41:42 +0000848 if (getSema().ArgumentPackSubstitutionIndex == -1)
Douglas Gregor53c3f4e2010-12-20 22:48:17 +0000849 return 0;
Douglas Gregor53c3f4e2010-12-20 22:48:17 +0000850
Douglas Gregora8bac7f2011-01-10 07:32:04 +0000851 assert(getSema().ArgumentPackSubstitutionIndex < (int)Arg.pack_size());
Douglas Gregor53c3f4e2010-12-20 22:48:17 +0000852 Arg = Arg.pack_begin()[getSema().ArgumentPackSubstitutionIndex];
853 }
854
855 QualType T = Arg.getAsType();
Douglas Gregora5cb6da2009-10-20 05:58:46 +0000856 if (T.isNull())
Douglas Gregora04f2ca2010-03-01 15:56:25 +0000857 return cast_or_null<NamedDecl>(TransformDecl(Loc, D));
Douglas Gregora5cb6da2009-10-20 05:58:46 +0000858
859 if (const TagType *Tag = T->getAs<TagType>())
860 return Tag->getDecl();
861
862 // The resulting type is not a tag; complain.
863 getSema().Diag(Loc, diag::err_nested_name_spec_non_tag) << T;
864 return 0;
865 }
866 }
867
Douglas Gregora04f2ca2010-03-01 15:56:25 +0000868 return cast_or_null<NamedDecl>(TransformDecl(Loc, D));
Douglas Gregora5cb6da2009-10-20 05:58:46 +0000869}
870
Douglas Gregorebe10102009-08-20 07:17:43 +0000871VarDecl *
872TemplateInstantiator::RebuildExceptionDecl(VarDecl *ExceptionDecl,
John McCallbcd03502009-12-07 02:54:59 +0000873 TypeSourceInfo *Declarator,
Douglas Gregorebe10102009-08-20 07:17:43 +0000874 IdentifierInfo *Name,
Douglas Gregor9f0e1aa2010-09-09 17:09:21 +0000875 SourceLocation Loc) {
876 VarDecl *Var = inherited::RebuildExceptionDecl(ExceptionDecl, Declarator,
877 Name, Loc);
Douglas Gregorf4e837f2010-04-26 17:57:08 +0000878 if (Var)
879 getSema().CurrentInstantiationScope->InstantiatedLocal(ExceptionDecl, Var);
880 return Var;
881}
882
883VarDecl *TemplateInstantiator::RebuildObjCExceptionDecl(VarDecl *ExceptionDecl,
884 TypeSourceInfo *TSInfo,
885 QualType T) {
886 VarDecl *Var = inherited::RebuildObjCExceptionDecl(ExceptionDecl, TSInfo, T);
887 if (Var)
Douglas Gregorebe10102009-08-20 07:17:43 +0000888 getSema().CurrentInstantiationScope->InstantiatedLocal(ExceptionDecl, Var);
889 return Var;
890}
891
John McCall7f41d982009-09-11 04:59:25 +0000892QualType
John McCall954b5de2010-11-04 19:04:38 +0000893TemplateInstantiator::RebuildElaboratedType(SourceLocation KeywordLoc,
894 ElaboratedTypeKeyword Keyword,
Abramo Bagnara6150c882010-05-11 21:36:43 +0000895 NestedNameSpecifier *NNS,
896 QualType T) {
John McCall7f41d982009-09-11 04:59:25 +0000897 if (const TagType *TT = T->getAs<TagType>()) {
898 TagDecl* TD = TT->getDecl();
899
John McCall954b5de2010-11-04 19:04:38 +0000900 SourceLocation TagLocation = KeywordLoc;
John McCall7f41d982009-09-11 04:59:25 +0000901
902 // FIXME: type might be anonymous.
903 IdentifierInfo *Id = TD->getIdentifier();
904
905 // TODO: should we even warn on struct/class mismatches for this? Seems
906 // like it's likely to produce a lot of spurious errors.
Abramo Bagnara6150c882010-05-11 21:36:43 +0000907 if (Keyword != ETK_None && Keyword != ETK_Typename) {
908 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForKeyword(Keyword);
909 if (!SemaRef.isAcceptableTagRedeclaration(TD, Kind, TagLocation, *Id)) {
910 SemaRef.Diag(TagLocation, diag::err_use_with_wrong_tag)
911 << Id
912 << FixItHint::CreateReplacement(SourceRange(TagLocation),
913 TD->getKindName());
914 SemaRef.Diag(TD->getLocation(), diag::note_previous_use);
915 }
John McCall7f41d982009-09-11 04:59:25 +0000916 }
917 }
918
John McCall954b5de2010-11-04 19:04:38 +0000919 return TreeTransform<TemplateInstantiator>::RebuildElaboratedType(KeywordLoc,
920 Keyword,
Abramo Bagnara6150c882010-05-11 21:36:43 +0000921 NNS, T);
John McCall7f41d982009-09-11 04:59:25 +0000922}
923
Douglas Gregor5590be02011-01-15 06:45:20 +0000924TemplateName TemplateInstantiator::TransformTemplateName(TemplateName Name,
925 QualType ObjectType,
926 NamedDecl *FirstQualifierInScope) {
927 if (TemplateTemplateParmDecl *TTP
928 = dyn_cast_or_null<TemplateTemplateParmDecl>(Name.getAsTemplateDecl())) {
929 if (TTP->getDepth() < TemplateArgs.getNumLevels()) {
930 // If the corresponding template argument is NULL or non-existent, it's
931 // because we are performing instantiation from explicitly-specified
932 // template arguments in a function template, but there were some
933 // arguments left unspecified.
934 if (!TemplateArgs.hasTemplateArgument(TTP->getDepth(),
935 TTP->getPosition()))
936 return Name;
937
938 TemplateArgument Arg = TemplateArgs(TTP->getDepth(), TTP->getPosition());
939
940 if (TTP->isParameterPack()) {
941 assert(Arg.getKind() == TemplateArgument::Pack &&
942 "Missing argument pack");
943
944 if (getSema().ArgumentPackSubstitutionIndex == -1) {
945 // We have the template argument pack to substitute, but we're not
946 // actually expanding the enclosing pack expansion yet. So, just
947 // keep the entire argument pack.
948 return getSema().Context.getSubstTemplateTemplateParmPack(TTP, Arg);
949 }
950
951 assert(getSema().ArgumentPackSubstitutionIndex < (int)Arg.pack_size());
952 Arg = Arg.pack_begin()[getSema().ArgumentPackSubstitutionIndex];
953 }
954
955 TemplateName Template = Arg.getAsTemplate();
956 assert(!Template.isNull() && Template.getAsTemplateDecl() &&
957 "Wrong kind of template template argument");
958 return Template;
959 }
960 }
961
962 if (SubstTemplateTemplateParmPackStorage *SubstPack
963 = Name.getAsSubstTemplateTemplateParmPack()) {
964 if (getSema().ArgumentPackSubstitutionIndex == -1)
965 return Name;
966
967 const TemplateArgument &ArgPack = SubstPack->getArgumentPack();
968 assert(getSema().ArgumentPackSubstitutionIndex < (int)ArgPack.pack_size() &&
969 "Pack substitution index out-of-range");
970 return ArgPack.pack_begin()[getSema().ArgumentPackSubstitutionIndex]
971 .getAsTemplate();
972 }
973
974 return inherited::TransformTemplateName(Name, ObjectType,
975 FirstQualifierInScope);
976}
977
John McCalldadc5752010-08-24 06:29:42 +0000978ExprResult
John McCall47f29ea2009-12-08 09:21:05 +0000979TemplateInstantiator::TransformPredefinedExpr(PredefinedExpr *E) {
Anders Carlsson0b209a82009-09-11 01:22:35 +0000980 if (!E->isTypeDependent())
John McCallc3007a22010-10-26 07:05:15 +0000981 return SemaRef.Owned(E);
Anders Carlsson0b209a82009-09-11 01:22:35 +0000982
983 FunctionDecl *currentDecl = getSema().getCurFunctionDecl();
984 assert(currentDecl && "Must have current function declaration when "
985 "instantiating.");
986
987 PredefinedExpr::IdentType IT = E->getIdentType();
988
Anders Carlsson5bd8d192010-02-11 18:20:28 +0000989 unsigned Length = PredefinedExpr::ComputeName(IT, currentDecl).length();
Anders Carlsson0b209a82009-09-11 01:22:35 +0000990
991 llvm::APInt LengthI(32, Length + 1);
John McCall8ccfcb52009-09-24 19:53:00 +0000992 QualType ResTy = getSema().Context.CharTy.withConst();
Anders Carlsson0b209a82009-09-11 01:22:35 +0000993 ResTy = getSema().Context.getConstantArrayType(ResTy, LengthI,
994 ArrayType::Normal, 0);
995 PredefinedExpr *PE =
996 new (getSema().Context) PredefinedExpr(E->getLocation(), ResTy, IT);
997 return getSema().Owned(PE);
998}
999
John McCalldadc5752010-08-24 06:29:42 +00001000ExprResult
John McCall13481c52010-02-06 08:42:39 +00001001TemplateInstantiator::TransformTemplateParmRefExpr(DeclRefExpr *E,
Douglas Gregor6c379e22010-02-08 23:41:45 +00001002 NonTypeTemplateParmDecl *NTTP) {
John McCall13481c52010-02-06 08:42:39 +00001003 // If the corresponding template argument is NULL or non-existent, it's
1004 // because we are performing instantiation from explicitly-specified
1005 // template arguments in a function template, but there were some
1006 // arguments left unspecified.
1007 if (!TemplateArgs.hasTemplateArgument(NTTP->getDepth(),
1008 NTTP->getPosition()))
John McCallc3007a22010-10-26 07:05:15 +00001009 return SemaRef.Owned(E);
Mike Stump11289f42009-09-09 15:08:12 +00001010
Douglas Gregoreb5a39d2010-12-24 00:15:10 +00001011 TemplateArgument Arg = TemplateArgs(NTTP->getDepth(), NTTP->getPosition());
1012 if (NTTP->isParameterPack()) {
1013 assert(Arg.getKind() == TemplateArgument::Pack &&
1014 "Missing argument pack");
1015
1016 if (getSema().ArgumentPackSubstitutionIndex == -1) {
Douglas Gregorcdbc5392011-01-15 01:15:58 +00001017 // We have an argument pack, but we can't select a particular argument
1018 // out of it yet. Therefore, we'll build an expression to hold on to that
1019 // argument pack.
1020 QualType TargetType = SemaRef.SubstType(NTTP->getType(), TemplateArgs,
1021 E->getLocation(),
1022 NTTP->getDeclName());
1023 if (TargetType.isNull())
1024 return ExprError();
1025
1026 return new (SemaRef.Context) SubstNonTypeTemplateParmPackExpr(TargetType,
1027 NTTP,
1028 E->getLocation(),
1029 Arg);
Douglas Gregoreb5a39d2010-12-24 00:15:10 +00001030 }
1031
Douglas Gregora8bac7f2011-01-10 07:32:04 +00001032 assert(getSema().ArgumentPackSubstitutionIndex < (int)Arg.pack_size());
Douglas Gregoreb5a39d2010-12-24 00:15:10 +00001033 Arg = Arg.pack_begin()[getSema().ArgumentPackSubstitutionIndex];
1034 }
Mike Stump11289f42009-09-09 15:08:12 +00001035
John McCall13481c52010-02-06 08:42:39 +00001036 // The template argument itself might be an expression, in which
1037 // case we just return that expression.
1038 if (Arg.getKind() == TemplateArgument::Expression)
John McCallc3007a22010-10-26 07:05:15 +00001039 return SemaRef.Owned(Arg.getAsExpr());
Mike Stump11289f42009-09-09 15:08:12 +00001040
John McCall13481c52010-02-06 08:42:39 +00001041 if (Arg.getKind() == TemplateArgument::Declaration) {
1042 ValueDecl *VD = cast<ValueDecl>(Arg.getAsDecl());
Mike Stump11289f42009-09-09 15:08:12 +00001043
John McCall15dda372010-02-06 10:23:53 +00001044 // Find the instantiation of the template argument. This is
1045 // required for nested templates.
John McCall13481c52010-02-06 08:42:39 +00001046 VD = cast_or_null<ValueDecl>(
Douglas Gregora04f2ca2010-03-01 15:56:25 +00001047 getSema().FindInstantiatedDecl(E->getLocation(),
1048 VD, TemplateArgs));
John McCall13481c52010-02-06 08:42:39 +00001049 if (!VD)
John McCallfaf5fb42010-08-26 23:41:50 +00001050 return ExprError();
Mike Stump11289f42009-09-09 15:08:12 +00001051
John McCall15dda372010-02-06 10:23:53 +00001052 // Derive the type we want the substituted decl to have. This had
1053 // better be non-dependent, or these checks will have serious problems.
Douglas Gregor0231d8d2011-01-19 20:10:05 +00001054 QualType TargetType;
1055 if (NTTP->isExpandedParameterPack())
1056 TargetType = NTTP->getExpansionType(
1057 getSema().ArgumentPackSubstitutionIndex);
1058 else if (NTTP->isParameterPack() &&
1059 isa<PackExpansionType>(NTTP->getType())) {
1060 TargetType = SemaRef.SubstType(
1061 cast<PackExpansionType>(NTTP->getType())->getPattern(),
1062 TemplateArgs, E->getLocation(),
1063 NTTP->getDeclName());
1064 } else
1065 TargetType = SemaRef.SubstType(NTTP->getType(), TemplateArgs,
1066 E->getLocation(), NTTP->getDeclName());
John McCall15dda372010-02-06 10:23:53 +00001067 assert(!TargetType.isNull() && "type substitution failed for param type");
1068 assert(!TargetType->isDependentType() && "param type still dependent");
Douglas Gregord5cb1dd2010-03-28 02:42:43 +00001069 return SemaRef.BuildExpressionFromDeclTemplateArgument(Arg,
1070 TargetType,
1071 E->getLocation());
John McCall13481c52010-02-06 08:42:39 +00001072 }
1073
Douglas Gregord5cb1dd2010-03-28 02:42:43 +00001074 return SemaRef.BuildExpressionFromIntegralTemplateArgument(Arg,
1075 E->getSourceRange().getBegin());
John McCall13481c52010-02-06 08:42:39 +00001076}
1077
Douglas Gregorcdbc5392011-01-15 01:15:58 +00001078ExprResult
1079TemplateInstantiator::TransformSubstNonTypeTemplateParmPackExpr(
1080 SubstNonTypeTemplateParmPackExpr *E) {
1081 if (getSema().ArgumentPackSubstitutionIndex == -1) {
1082 // We aren't expanding the parameter pack, so just return ourselves.
1083 return getSema().Owned(E);
1084 }
1085
Douglas Gregorcdbc5392011-01-15 01:15:58 +00001086 const TemplateArgument &ArgPack = E->getArgumentPack();
1087 unsigned Index = (unsigned)getSema().ArgumentPackSubstitutionIndex;
1088 assert(Index < ArgPack.pack_size() && "Substitution index out-of-range");
1089
1090 const TemplateArgument &Arg = ArgPack.pack_begin()[Index];
1091 if (Arg.getKind() == TemplateArgument::Expression)
1092 return SemaRef.Owned(Arg.getAsExpr());
1093
1094 if (Arg.getKind() == TemplateArgument::Declaration) {
1095 ValueDecl *VD = cast<ValueDecl>(Arg.getAsDecl());
1096
1097 // Find the instantiation of the template argument. This is
1098 // required for nested templates.
1099 VD = cast_or_null<ValueDecl>(
1100 getSema().FindInstantiatedDecl(E->getParameterPackLocation(),
1101 VD, TemplateArgs));
1102 if (!VD)
1103 return ExprError();
1104
Douglas Gregor0231d8d2011-01-19 20:10:05 +00001105 QualType T;
1106 NonTypeTemplateParmDecl *NTTP = E->getParameterPack();
1107 if (NTTP->isExpandedParameterPack())
1108 T = NTTP->getExpansionType(getSema().ArgumentPackSubstitutionIndex);
1109 else if (const PackExpansionType *Expansion
1110 = dyn_cast<PackExpansionType>(NTTP->getType()))
1111 T = SemaRef.SubstType(Expansion->getPattern(), TemplateArgs,
1112 E->getParameterPackLocation(), NTTP->getDeclName());
1113 else
1114 T = E->getType();
1115 return SemaRef.BuildExpressionFromDeclTemplateArgument(Arg, T,
Douglas Gregorcdbc5392011-01-15 01:15:58 +00001116 E->getParameterPackLocation());
1117 }
1118
1119 return SemaRef.BuildExpressionFromIntegralTemplateArgument(Arg,
1120 E->getParameterPackLocation());
1121}
John McCall13481c52010-02-06 08:42:39 +00001122
John McCalldadc5752010-08-24 06:29:42 +00001123ExprResult
John McCall13481c52010-02-06 08:42:39 +00001124TemplateInstantiator::TransformDeclRefExpr(DeclRefExpr *E) {
1125 NamedDecl *D = E->getDecl();
1126 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(D)) {
1127 if (NTTP->getDepth() < TemplateArgs.getNumLevels())
1128 return TransformTemplateParmRefExpr(E, NTTP);
Douglas Gregor954de172009-10-31 17:21:17 +00001129
1130 // We have a non-type template parameter that isn't fully substituted;
1131 // FindInstantiatedDecl will find it in the local instantiation scope.
Douglas Gregora16548e2009-08-11 05:31:07 +00001132 }
Mike Stump11289f42009-09-09 15:08:12 +00001133
John McCall47f29ea2009-12-08 09:21:05 +00001134 return TreeTransform<TemplateInstantiator>::TransformDeclRefExpr(E);
Douglas Gregora16548e2009-08-11 05:31:07 +00001135}
1136
John McCalldadc5752010-08-24 06:29:42 +00001137ExprResult TemplateInstantiator::TransformCXXDefaultArgExpr(
John McCall47f29ea2009-12-08 09:21:05 +00001138 CXXDefaultArgExpr *E) {
Sebastian Redl14236c82009-11-08 13:56:19 +00001139 assert(!cast<FunctionDecl>(E->getParam()->getDeclContext())->
1140 getDescribedFunctionTemplate() &&
1141 "Default arg expressions are never formed in dependent cases.");
Douglas Gregor033f6752009-12-23 23:03:06 +00001142 return SemaRef.BuildCXXDefaultArgExpr(E->getUsedLocation(),
1143 cast<FunctionDecl>(E->getParam()->getDeclContext()),
1144 E->getParam());
Sebastian Redl14236c82009-11-08 13:56:19 +00001145}
1146
Douglas Gregor14cf7522010-04-30 18:55:50 +00001147QualType TemplateInstantiator::TransformFunctionProtoType(TypeLocBuilder &TLB,
John McCall31f82722010-11-12 08:19:04 +00001148 FunctionProtoTypeLoc TL) {
Douglas Gregor14cf7522010-04-30 18:55:50 +00001149 // We need a local instantiation scope for this function prototype.
John McCall19c1bfd2010-08-25 05:32:35 +00001150 LocalInstantiationScope Scope(SemaRef, /*CombineWithOuterScope=*/true);
John McCall31f82722010-11-12 08:19:04 +00001151 return inherited::TransformFunctionProtoType(TLB, TL);
John McCall58f10c32010-03-11 09:03:00 +00001152}
1153
1154ParmVarDecl *
Douglas Gregor715e4612011-01-14 22:40:04 +00001155TemplateInstantiator::TransformFunctionTypeParam(ParmVarDecl *OldParm,
1156 llvm::Optional<unsigned> NumExpansions) {
1157 return SemaRef.SubstParmVarDecl(OldParm, TemplateArgs,
1158 NumExpansions);
John McCall58f10c32010-03-11 09:03:00 +00001159}
1160
Mike Stump11289f42009-09-09 15:08:12 +00001161QualType
John McCall550e0c22009-10-21 00:40:46 +00001162TemplateInstantiator::TransformTemplateTypeParmType(TypeLocBuilder &TLB,
John McCall31f82722010-11-12 08:19:04 +00001163 TemplateTypeParmTypeLoc TL) {
John McCall424cec92011-01-19 06:33:43 +00001164 const TemplateTypeParmType *T = TL.getTypePtr();
Douglas Gregor01afeef2009-08-28 20:31:08 +00001165 if (T->getDepth() < TemplateArgs.getNumLevels()) {
Douglas Gregorfe1e1102009-02-27 19:31:52 +00001166 // Replace the template type parameter with its corresponding
1167 // template argument.
Mike Stump11289f42009-09-09 15:08:12 +00001168
1169 // If the corresponding template argument is NULL or doesn't exist, it's
1170 // because we are performing instantiation from explicitly-specified
1171 // template arguments in a function template class, but there were some
Douglas Gregore3f1f352009-07-01 00:28:38 +00001172 // arguments left unspecified.
John McCall550e0c22009-10-21 00:40:46 +00001173 if (!TemplateArgs.hasTemplateArgument(T->getDepth(), T->getIndex())) {
1174 TemplateTypeParmTypeLoc NewTL
1175 = TLB.push<TemplateTypeParmTypeLoc>(TL.getType());
1176 NewTL.setNameLoc(TL.getNameLoc());
1177 return TL.getType();
1178 }
Mike Stump11289f42009-09-09 15:08:12 +00001179
Douglas Gregor840bd6c2010-12-20 22:05:00 +00001180 TemplateArgument Arg = TemplateArgs(T->getDepth(), T->getIndex());
1181
1182 if (T->isParameterPack()) {
1183 assert(Arg.getKind() == TemplateArgument::Pack &&
1184 "Missing argument pack");
1185
1186 if (getSema().ArgumentPackSubstitutionIndex == -1) {
Douglas Gregorada4b792011-01-14 02:55:32 +00001187 // We have the template argument pack, but we're not expanding the
1188 // enclosing pack expansion yet. Just save the template argument
1189 // pack for later substitution.
1190 QualType Result
1191 = getSema().Context.getSubstTemplateTypeParmPackType(T, Arg);
1192 SubstTemplateTypeParmPackTypeLoc NewTL
1193 = TLB.push<SubstTemplateTypeParmPackTypeLoc>(Result);
1194 NewTL.setNameLoc(TL.getNameLoc());
1195 return Result;
Douglas Gregor840bd6c2010-12-20 22:05:00 +00001196 }
1197
Douglas Gregora8bac7f2011-01-10 07:32:04 +00001198 assert(getSema().ArgumentPackSubstitutionIndex < (int)Arg.pack_size());
Douglas Gregor840bd6c2010-12-20 22:05:00 +00001199 Arg = Arg.pack_begin()[getSema().ArgumentPackSubstitutionIndex];
1200 }
1201
1202 assert(Arg.getKind() == TemplateArgument::Type &&
Douglas Gregorfe1e1102009-02-27 19:31:52 +00001203 "Template argument kind mismatch");
Douglas Gregor01afeef2009-08-28 20:31:08 +00001204
Douglas Gregor840bd6c2010-12-20 22:05:00 +00001205 QualType Replacement = Arg.getAsType();
John McCallcebee162009-10-18 09:09:24 +00001206
1207 // TODO: only do this uniquing once, at the start of instantiation.
John McCall550e0c22009-10-21 00:40:46 +00001208 QualType Result
1209 = getSema().Context.getSubstTemplateTypeParmType(T, Replacement);
1210 SubstTemplateTypeParmTypeLoc NewTL
1211 = TLB.push<SubstTemplateTypeParmTypeLoc>(Result);
1212 NewTL.setNameLoc(TL.getNameLoc());
1213 return Result;
Mike Stump11289f42009-09-09 15:08:12 +00001214 }
Douglas Gregorfe1e1102009-02-27 19:31:52 +00001215
1216 // The template type parameter comes from an inner template (e.g.,
1217 // the template parameter list of a member template inside the
1218 // template we are instantiating). Create a new template type
1219 // parameter with the template "level" reduced by one.
John McCall550e0c22009-10-21 00:40:46 +00001220 QualType Result
1221 = getSema().Context.getTemplateTypeParmType(T->getDepth()
1222 - TemplateArgs.getNumLevels(),
1223 T->getIndex(),
1224 T->isParameterPack(),
Douglas Gregor2ebcae12010-06-16 15:23:05 +00001225 T->getName());
John McCall550e0c22009-10-21 00:40:46 +00001226 TemplateTypeParmTypeLoc NewTL = TLB.push<TemplateTypeParmTypeLoc>(Result);
1227 NewTL.setNameLoc(TL.getNameLoc());
1228 return Result;
Douglas Gregor17c0d7b2009-02-28 00:25:32 +00001229}
Douglas Gregorfe1e1102009-02-27 19:31:52 +00001230
Douglas Gregorada4b792011-01-14 02:55:32 +00001231QualType
1232TemplateInstantiator::TransformSubstTemplateTypeParmPackType(
1233 TypeLocBuilder &TLB,
1234 SubstTemplateTypeParmPackTypeLoc TL) {
1235 if (getSema().ArgumentPackSubstitutionIndex == -1) {
1236 // We aren't expanding the parameter pack, so just return ourselves.
1237 SubstTemplateTypeParmPackTypeLoc NewTL
1238 = TLB.push<SubstTemplateTypeParmPackTypeLoc>(TL.getType());
1239 NewTL.setNameLoc(TL.getNameLoc());
1240 return TL.getType();
1241 }
1242
1243 const TemplateArgument &ArgPack = TL.getTypePtr()->getArgumentPack();
1244 unsigned Index = (unsigned)getSema().ArgumentPackSubstitutionIndex;
1245 assert(Index < ArgPack.pack_size() && "Substitution index out-of-range");
1246
1247 QualType Result = ArgPack.pack_begin()[Index].getAsType();
1248 Result = getSema().Context.getSubstTemplateTypeParmType(
1249 TL.getTypePtr()->getReplacedParameter(),
1250 Result);
1251 SubstTemplateTypeParmTypeLoc NewTL
1252 = TLB.push<SubstTemplateTypeParmTypeLoc>(Result);
1253 NewTL.setNameLoc(TL.getNameLoc());
1254 return Result;
1255}
1256
John McCall76d824f2009-08-25 22:02:44 +00001257/// \brief Perform substitution on the type T with a given set of template
1258/// arguments.
Douglas Gregorfe1e1102009-02-27 19:31:52 +00001259///
1260/// This routine substitutes the given template arguments into the
1261/// type T and produces the instantiated type.
1262///
1263/// \param T the type into which the template arguments will be
1264/// substituted. If this type is not dependent, it will be returned
1265/// immediately.
1266///
1267/// \param TemplateArgs the template arguments that will be
1268/// substituted for the top-level template parameters within T.
1269///
Douglas Gregorfe1e1102009-02-27 19:31:52 +00001270/// \param Loc the location in the source code where this substitution
1271/// is being performed. It will typically be the location of the
1272/// declarator (if we're instantiating the type of some declaration)
1273/// or the location of the type in the source code (if, e.g., we're
1274/// instantiating the type of a cast expression).
1275///
1276/// \param Entity the name of the entity associated with a declaration
1277/// being instantiated (if any). May be empty to indicate that there
1278/// is no such entity (if, e.g., this is a type that occurs as part of
1279/// a cast expression) or that the entity has no name (e.g., an
1280/// unnamed function parameter).
1281///
1282/// \returns If the instantiation succeeds, the instantiated
1283/// type. Otherwise, produces diagnostics and returns a NULL type.
John McCallbcd03502009-12-07 02:54:59 +00001284TypeSourceInfo *Sema::SubstType(TypeSourceInfo *T,
John McCall609459e2009-10-21 00:58:09 +00001285 const MultiLevelTemplateArgumentList &Args,
1286 SourceLocation Loc,
1287 DeclarationName Entity) {
1288 assert(!ActiveTemplateInstantiations.empty() &&
1289 "Cannot perform an instantiation without some context on the "
1290 "instantiation stack");
1291
Douglas Gregor5a5073e2010-05-24 17:22:01 +00001292 if (!T->getType()->isDependentType() &&
1293 !T->getType()->isVariablyModifiedType())
John McCall609459e2009-10-21 00:58:09 +00001294 return T;
1295
1296 TemplateInstantiator Instantiator(*this, Args, Loc, Entity);
1297 return Instantiator.TransformType(T);
1298}
1299
Douglas Gregor5499af42011-01-05 23:12:31 +00001300TypeSourceInfo *Sema::SubstType(TypeLoc TL,
1301 const MultiLevelTemplateArgumentList &Args,
1302 SourceLocation Loc,
1303 DeclarationName Entity) {
1304 assert(!ActiveTemplateInstantiations.empty() &&
1305 "Cannot perform an instantiation without some context on the "
1306 "instantiation stack");
1307
1308 if (TL.getType().isNull())
1309 return 0;
1310
1311 if (!TL.getType()->isDependentType() &&
1312 !TL.getType()->isVariablyModifiedType()) {
1313 // FIXME: Make a copy of the TypeLoc data here, so that we can
1314 // return a new TypeSourceInfo. Inefficient!
1315 TypeLocBuilder TLB;
1316 TLB.pushFullCopy(TL);
1317 return TLB.getTypeSourceInfo(Context, TL.getType());
1318 }
1319
1320 TemplateInstantiator Instantiator(*this, Args, Loc, Entity);
1321 TypeLocBuilder TLB;
1322 TLB.reserve(TL.getFullDataSize());
1323 QualType Result = Instantiator.TransformType(TLB, TL);
1324 if (Result.isNull())
1325 return 0;
1326
1327 return TLB.getTypeSourceInfo(Context, Result);
1328}
1329
John McCall609459e2009-10-21 00:58:09 +00001330/// Deprecated form of the above.
Mike Stump11289f42009-09-09 15:08:12 +00001331QualType Sema::SubstType(QualType T,
Douglas Gregor01afeef2009-08-28 20:31:08 +00001332 const MultiLevelTemplateArgumentList &TemplateArgs,
John McCall76d824f2009-08-25 22:02:44 +00001333 SourceLocation Loc, DeclarationName Entity) {
Douglas Gregor79cf6032009-03-10 20:44:00 +00001334 assert(!ActiveTemplateInstantiations.empty() &&
1335 "Cannot perform an instantiation without some context on the "
1336 "instantiation stack");
1337
Douglas Gregor5a5073e2010-05-24 17:22:01 +00001338 // If T is not a dependent type or a variably-modified type, there
1339 // is nothing to do.
1340 if (!T->isDependentType() && !T->isVariablyModifiedType())
Douglas Gregorfe1e1102009-02-27 19:31:52 +00001341 return T;
1342
Douglas Gregord6ff3322009-08-04 16:50:30 +00001343 TemplateInstantiator Instantiator(*this, TemplateArgs, Loc, Entity);
1344 return Instantiator.TransformType(T);
Douglas Gregorfe1e1102009-02-27 19:31:52 +00001345}
Douglas Gregor463421d2009-03-03 04:44:36 +00001346
John McCallb29f78f2010-04-09 17:38:44 +00001347static bool NeedsInstantiationAsFunctionType(TypeSourceInfo *T) {
Douglas Gregor5a5073e2010-05-24 17:22:01 +00001348 if (T->getType()->isDependentType() || T->getType()->isVariablyModifiedType())
John McCallb29f78f2010-04-09 17:38:44 +00001349 return true;
1350
Abramo Bagnara6d810632010-12-14 22:11:44 +00001351 TypeLoc TL = T->getTypeLoc().IgnoreParens();
John McCallb29f78f2010-04-09 17:38:44 +00001352 if (!isa<FunctionProtoTypeLoc>(TL))
1353 return false;
1354
1355 FunctionProtoTypeLoc FP = cast<FunctionProtoTypeLoc>(TL);
1356 for (unsigned I = 0, E = FP.getNumArgs(); I != E; ++I) {
1357 ParmVarDecl *P = FP.getArg(I);
1358
1359 // TODO: currently we always rebuild expressions. When we
1360 // properly get lazier about this, we should use the same
1361 // logic to avoid rebuilding prototypes here.
Douglas Gregor9cc278222011-01-05 21:14:17 +00001362 if (P->hasDefaultArg())
John McCallb29f78f2010-04-09 17:38:44 +00001363 return true;
1364 }
1365
1366 return false;
1367}
1368
1369/// A form of SubstType intended specifically for instantiating the
1370/// type of a FunctionDecl. Its purpose is solely to force the
1371/// instantiation of default-argument expressions.
1372TypeSourceInfo *Sema::SubstFunctionDeclType(TypeSourceInfo *T,
1373 const MultiLevelTemplateArgumentList &Args,
1374 SourceLocation Loc,
1375 DeclarationName Entity) {
1376 assert(!ActiveTemplateInstantiations.empty() &&
1377 "Cannot perform an instantiation without some context on the "
1378 "instantiation stack");
1379
1380 if (!NeedsInstantiationAsFunctionType(T))
1381 return T;
1382
1383 TemplateInstantiator Instantiator(*this, Args, Loc, Entity);
1384
1385 TypeLocBuilder TLB;
1386
1387 TypeLoc TL = T->getTypeLoc();
1388 TLB.reserve(TL.getFullDataSize());
1389
John McCall31f82722010-11-12 08:19:04 +00001390 QualType Result = Instantiator.TransformType(TLB, TL);
John McCallb29f78f2010-04-09 17:38:44 +00001391 if (Result.isNull())
1392 return 0;
1393
1394 return TLB.getTypeSourceInfo(Context, Result);
1395}
1396
Douglas Gregor940bca72010-04-12 07:48:19 +00001397ParmVarDecl *Sema::SubstParmVarDecl(ParmVarDecl *OldParm,
Douglas Gregor715e4612011-01-14 22:40:04 +00001398 const MultiLevelTemplateArgumentList &TemplateArgs,
1399 llvm::Optional<unsigned> NumExpansions) {
Douglas Gregor940bca72010-04-12 07:48:19 +00001400 TypeSourceInfo *OldDI = OldParm->getTypeSourceInfo();
Douglas Gregor5499af42011-01-05 23:12:31 +00001401 TypeSourceInfo *NewDI = 0;
1402
Douglas Gregor5499af42011-01-05 23:12:31 +00001403 TypeLoc OldTL = OldDI->getTypeLoc();
1404 if (isa<PackExpansionTypeLoc>(OldTL)) {
1405 PackExpansionTypeLoc ExpansionTL = cast<PackExpansionTypeLoc>(OldTL);
Douglas Gregor5499af42011-01-05 23:12:31 +00001406
1407 // We have a function parameter pack. Substitute into the pattern of the
1408 // expansion.
1409 NewDI = SubstType(ExpansionTL.getPatternLoc(), TemplateArgs,
1410 OldParm->getLocation(), OldParm->getDeclName());
1411 if (!NewDI)
1412 return 0;
1413
1414 if (NewDI->getType()->containsUnexpandedParameterPack()) {
1415 // We still have unexpanded parameter packs, which means that
1416 // our function parameter is still a function parameter pack.
1417 // Therefore, make its type a pack expansion type.
Douglas Gregor0dca5fd2011-01-14 17:04:44 +00001418 NewDI = CheckPackExpansion(NewDI, ExpansionTL.getEllipsisLoc(),
Douglas Gregor715e4612011-01-14 22:40:04 +00001419 NumExpansions);
Douglas Gregor5499af42011-01-05 23:12:31 +00001420 }
1421 } else {
1422 NewDI = SubstType(OldDI, TemplateArgs, OldParm->getLocation(),
1423 OldParm->getDeclName());
1424 }
1425
Douglas Gregor940bca72010-04-12 07:48:19 +00001426 if (!NewDI)
1427 return 0;
1428
1429 if (NewDI->getType()->isVoidType()) {
1430 Diag(OldParm->getLocation(), diag::err_param_with_void_type);
1431 return 0;
1432 }
1433
1434 ParmVarDecl *NewParm = CheckParameter(Context.getTranslationUnitDecl(),
1435 NewDI, NewDI->getType(),
1436 OldParm->getIdentifier(),
1437 OldParm->getLocation(),
Douglas Gregorc4df4072010-04-19 22:54:31 +00001438 OldParm->getStorageClass(),
1439 OldParm->getStorageClassAsWritten());
Douglas Gregor940bca72010-04-12 07:48:19 +00001440 if (!NewParm)
1441 return 0;
Douglas Gregor6044d692010-05-19 17:02:24 +00001442
Douglas Gregor940bca72010-04-12 07:48:19 +00001443 // Mark the (new) default argument as uninstantiated (if any).
1444 if (OldParm->hasUninstantiatedDefaultArg()) {
1445 Expr *Arg = OldParm->getUninstantiatedDefaultArg();
1446 NewParm->setUninstantiatedDefaultArg(Arg);
Douglas Gregor758cb672010-10-12 18:23:32 +00001447 } else if (OldParm->hasUnparsedDefaultArg()) {
1448 NewParm->setUnparsedDefaultArg();
1449 UnparsedDefaultArgInstantiations[OldParm].push_back(NewParm);
Douglas Gregor940bca72010-04-12 07:48:19 +00001450 } else if (Expr *Arg = OldParm->getDefaultArg())
1451 NewParm->setUninstantiatedDefaultArg(Arg);
1452
1453 NewParm->setHasInheritedDefaultArg(OldParm->hasInheritedDefaultArg());
1454
Douglas Gregor5499af42011-01-05 23:12:31 +00001455 // FIXME: When OldParm is a parameter pack and NewParm is not a parameter
1456 // pack, we actually have a set of instantiated locations. Maintain this set!
Douglas Gregorf3010112011-01-07 16:43:16 +00001457 if (OldParm->isParameterPack() && !NewParm->isParameterPack()) {
1458 // Add the new parameter to
1459 CurrentInstantiationScope->InstantiatedLocalPackArg(OldParm, NewParm);
1460 } else {
1461 // Introduce an Old -> New mapping
Douglas Gregor5499af42011-01-05 23:12:31 +00001462 CurrentInstantiationScope->InstantiatedLocal(OldParm, NewParm);
Douglas Gregorf3010112011-01-07 16:43:16 +00001463 }
Douglas Gregor5499af42011-01-05 23:12:31 +00001464
Argyrios Kyrtzidis3816ed42010-07-19 10:14:41 +00001465 // FIXME: OldParm may come from a FunctionProtoType, in which case CurContext
1466 // can be anything, is this right ?
Fariborz Jahanian714447b2010-07-13 21:05:02 +00001467 NewParm->setDeclContext(CurContext);
Fariborz Jahaniana6c7efe2010-07-13 20:05:58 +00001468
Douglas Gregor940bca72010-04-12 07:48:19 +00001469 return NewParm;
1470}
1471
Douglas Gregordd472162011-01-07 00:20:55 +00001472/// \brief Substitute the given template arguments into the given set of
1473/// parameters, producing the set of parameter types that would be generated
1474/// from such a substitution.
1475bool Sema::SubstParmTypes(SourceLocation Loc,
1476 ParmVarDecl **Params, unsigned NumParams,
1477 const MultiLevelTemplateArgumentList &TemplateArgs,
Douglas Gregorf3010112011-01-07 16:43:16 +00001478 llvm::SmallVectorImpl<QualType> &ParamTypes,
1479 llvm::SmallVectorImpl<ParmVarDecl *> *OutParams) {
Douglas Gregordd472162011-01-07 00:20:55 +00001480 assert(!ActiveTemplateInstantiations.empty() &&
1481 "Cannot perform an instantiation without some context on the "
1482 "instantiation stack");
1483
1484 TemplateInstantiator Instantiator(*this, TemplateArgs, Loc,
1485 DeclarationName());
1486 return Instantiator.TransformFunctionTypeParams(Loc, Params, NumParams, 0,
Douglas Gregorf3010112011-01-07 16:43:16 +00001487 ParamTypes, OutParams);
Douglas Gregordd472162011-01-07 00:20:55 +00001488}
1489
John McCall76d824f2009-08-25 22:02:44 +00001490/// \brief Perform substitution on the base class specifiers of the
1491/// given class template specialization.
Douglas Gregor463421d2009-03-03 04:44:36 +00001492///
1493/// Produces a diagnostic and returns true on error, returns false and
1494/// attaches the instantiated base classes to the class template
1495/// specialization if successful.
Mike Stump11289f42009-09-09 15:08:12 +00001496bool
John McCall76d824f2009-08-25 22:02:44 +00001497Sema::SubstBaseSpecifiers(CXXRecordDecl *Instantiation,
1498 CXXRecordDecl *Pattern,
Douglas Gregor01afeef2009-08-28 20:31:08 +00001499 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor463421d2009-03-03 04:44:36 +00001500 bool Invalid = false;
Douglas Gregor6181ded2009-05-29 18:27:38 +00001501 llvm::SmallVector<CXXBaseSpecifier*, 4> InstantiatedBases;
Mike Stump11289f42009-09-09 15:08:12 +00001502 for (ClassTemplateSpecializationDecl::base_class_iterator
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001503 Base = Pattern->bases_begin(), BaseEnd = Pattern->bases_end();
Douglas Gregor2a72edd2009-03-10 18:52:44 +00001504 Base != BaseEnd; ++Base) {
Douglas Gregor463421d2009-03-03 04:44:36 +00001505 if (!Base->getType()->isDependentType()) {
Fariborz Jahanian5c14ec32009-07-22 17:41:53 +00001506 InstantiatedBases.push_back(new (Context) CXXBaseSpecifier(*Base));
Douglas Gregor463421d2009-03-03 04:44:36 +00001507 continue;
1508 }
1509
Douglas Gregor752a5952011-01-03 22:36:02 +00001510 SourceLocation EllipsisLoc;
1511 if (Base->isPackExpansion()) {
1512 // This is a pack expansion. See whether we should expand it now, or
1513 // wait until later.
1514 llvm::SmallVector<UnexpandedParameterPack, 2> Unexpanded;
1515 collectUnexpandedParameterPacks(Base->getTypeSourceInfo()->getTypeLoc(),
1516 Unexpanded);
1517 bool ShouldExpand = false;
Douglas Gregora8bac7f2011-01-10 07:32:04 +00001518 bool RetainExpansion = false;
Douglas Gregor0dca5fd2011-01-14 17:04:44 +00001519 llvm::Optional<unsigned> NumExpansions;
Douglas Gregor752a5952011-01-03 22:36:02 +00001520 if (CheckParameterPacksForExpansion(Base->getEllipsisLoc(),
1521 Base->getSourceRange(),
1522 Unexpanded.data(), Unexpanded.size(),
1523 TemplateArgs, ShouldExpand,
Douglas Gregora8bac7f2011-01-10 07:32:04 +00001524 RetainExpansion,
Douglas Gregor752a5952011-01-03 22:36:02 +00001525 NumExpansions)) {
Douglas Gregor752a5952011-01-03 22:36:02 +00001526 Invalid = true;
Douglas Gregor44e7df62011-01-04 00:32:56 +00001527 continue;
Douglas Gregor752a5952011-01-03 22:36:02 +00001528 }
1529
1530 // If we should expand this pack expansion now, do so.
1531 if (ShouldExpand) {
Douglas Gregor0dca5fd2011-01-14 17:04:44 +00001532 for (unsigned I = 0; I != *NumExpansions; ++I) {
Douglas Gregor752a5952011-01-03 22:36:02 +00001533 Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(*this, I);
1534
1535 TypeSourceInfo *BaseTypeLoc = SubstType(Base->getTypeSourceInfo(),
1536 TemplateArgs,
1537 Base->getSourceRange().getBegin(),
1538 DeclarationName());
1539 if (!BaseTypeLoc) {
1540 Invalid = true;
1541 continue;
1542 }
1543
1544 if (CXXBaseSpecifier *InstantiatedBase
1545 = CheckBaseSpecifier(Instantiation,
1546 Base->getSourceRange(),
1547 Base->isVirtual(),
1548 Base->getAccessSpecifierAsWritten(),
1549 BaseTypeLoc,
1550 SourceLocation()))
1551 InstantiatedBases.push_back(InstantiatedBase);
1552 else
1553 Invalid = true;
1554 }
1555
1556 continue;
1557 }
1558
1559 // The resulting base specifier will (still) be a pack expansion.
1560 EllipsisLoc = Base->getEllipsisLoc();
1561 }
1562
1563 Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(*this, -1);
Nick Lewycky19b9f952010-07-26 16:56:01 +00001564 TypeSourceInfo *BaseTypeLoc = SubstType(Base->getTypeSourceInfo(),
1565 TemplateArgs,
1566 Base->getSourceRange().getBegin(),
1567 DeclarationName());
1568 if (!BaseTypeLoc) {
Douglas Gregor463421d2009-03-03 04:44:36 +00001569 Invalid = true;
1570 continue;
1571 }
1572
1573 if (CXXBaseSpecifier *InstantiatedBase
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001574 = CheckBaseSpecifier(Instantiation,
Douglas Gregor463421d2009-03-03 04:44:36 +00001575 Base->getSourceRange(),
1576 Base->isVirtual(),
1577 Base->getAccessSpecifierAsWritten(),
Douglas Gregor752a5952011-01-03 22:36:02 +00001578 BaseTypeLoc,
1579 EllipsisLoc))
Douglas Gregor463421d2009-03-03 04:44:36 +00001580 InstantiatedBases.push_back(InstantiatedBase);
1581 else
1582 Invalid = true;
1583 }
1584
Douglas Gregor2a72edd2009-03-10 18:52:44 +00001585 if (!Invalid &&
Jay Foad7d0479f2009-05-21 09:52:38 +00001586 AttachBaseSpecifiers(Instantiation, InstantiatedBases.data(),
Douglas Gregor463421d2009-03-03 04:44:36 +00001587 InstantiatedBases.size()))
1588 Invalid = true;
1589
1590 return Invalid;
1591}
1592
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001593/// \brief Instantiate the definition of a class from a given pattern.
1594///
1595/// \param PointOfInstantiation The point of instantiation within the
1596/// source code.
1597///
1598/// \param Instantiation is the declaration whose definition is being
1599/// instantiated. This will be either a class template specialization
1600/// or a member class of a class template specialization.
1601///
1602/// \param Pattern is the pattern from which the instantiation
1603/// occurs. This will be either the declaration of a class template or
1604/// the declaration of a member class of a class template.
1605///
1606/// \param TemplateArgs The template arguments to be substituted into
1607/// the pattern.
1608///
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001609/// \param TSK the kind of implicit or explicit instantiation to perform.
Douglas Gregor8a2e6012009-08-24 15:23:48 +00001610///
1611/// \param Complain whether to complain if the class cannot be instantiated due
1612/// to the lack of a definition.
1613///
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001614/// \returns true if an error occurred, false otherwise.
1615bool
1616Sema::InstantiateClass(SourceLocation PointOfInstantiation,
1617 CXXRecordDecl *Instantiation, CXXRecordDecl *Pattern,
Douglas Gregor01afeef2009-08-28 20:31:08 +00001618 const MultiLevelTemplateArgumentList &TemplateArgs,
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001619 TemplateSpecializationKind TSK,
Douglas Gregor8a2e6012009-08-24 15:23:48 +00001620 bool Complain) {
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001621 bool Invalid = false;
John McCall87a44eb2009-08-20 01:44:21 +00001622
Mike Stump11289f42009-09-09 15:08:12 +00001623 CXXRecordDecl *PatternDef
Douglas Gregor0a5a2212010-02-11 01:04:33 +00001624 = cast_or_null<CXXRecordDecl>(Pattern->getDefinition());
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001625 if (!PatternDef) {
Douglas Gregor8a2e6012009-08-24 15:23:48 +00001626 if (!Complain) {
1627 // Say nothing
1628 } else if (Pattern == Instantiation->getInstantiatedFromMemberClass()) {
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001629 Diag(PointOfInstantiation,
1630 diag::err_implicit_instantiate_member_undefined)
1631 << Context.getTypeDeclType(Instantiation);
1632 Diag(Pattern->getLocation(), diag::note_member_of_template_here);
1633 } else {
Douglas Gregora1f49972009-05-13 00:25:59 +00001634 Diag(PointOfInstantiation, diag::err_template_instantiate_undefined)
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001635 << (TSK != TSK_ImplicitInstantiation)
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001636 << Context.getTypeDeclType(Instantiation);
1637 Diag(Pattern->getLocation(), diag::note_template_decl_here);
1638 }
1639 return true;
1640 }
1641 Pattern = PatternDef;
1642
Douglas Gregord6ba93d2009-10-15 15:54:05 +00001643 // \brief Record the point of instantiation.
1644 if (MemberSpecializationInfo *MSInfo
1645 = Instantiation->getMemberSpecializationInfo()) {
1646 MSInfo->setTemplateSpecializationKind(TSK);
1647 MSInfo->setPointOfInstantiation(PointOfInstantiation);
Douglas Gregoref6ab412009-10-27 06:26:26 +00001648 } else if (ClassTemplateSpecializationDecl *Spec
1649 = dyn_cast<ClassTemplateSpecializationDecl>(Instantiation)) {
1650 Spec->setTemplateSpecializationKind(TSK);
1651 Spec->setPointOfInstantiation(PointOfInstantiation);
Douglas Gregord6ba93d2009-10-15 15:54:05 +00001652 }
1653
Douglas Gregorf3430ae2009-03-25 21:23:52 +00001654 InstantiatingTemplate Inst(*this, PointOfInstantiation, Instantiation);
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001655 if (Inst)
1656 return true;
1657
1658 // Enter the scope of this instantiation. We don't use
1659 // PushDeclContext because we don't have a scope.
John McCall80e58cd2010-04-29 00:35:03 +00001660 ContextRAII SavedContext(*this, Instantiation);
Douglas Gregor17158422010-05-12 17:27:19 +00001661 EnterExpressionEvaluationContext EvalContext(*this,
John McCallfaf5fb42010-08-26 23:41:50 +00001662 Sema::PotentiallyEvaluated);
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001663
Douglas Gregor51121572010-03-24 01:33:17 +00001664 // If this is an instantiation of a local class, merge this local
1665 // instantiation scope with the enclosing scope. Otherwise, every
1666 // instantiation of a class has its own local instantiation scope.
1667 bool MergeWithParentScope = !Instantiation->isDefinedOutsideFunctionOrMethod();
John McCall19c1bfd2010-08-25 05:32:35 +00001668 LocalInstantiationScope Scope(*this, MergeWithParentScope);
Douglas Gregor51121572010-03-24 01:33:17 +00001669
John McCall6602bb12010-08-01 02:01:53 +00001670 // Pull attributes from the pattern onto the instantiation.
1671 InstantiateAttrs(TemplateArgs, Pattern, Instantiation);
1672
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001673 // Start the definition of this instantiation.
1674 Instantiation->startDefinition();
Douglas Gregore9029562010-05-06 00:28:52 +00001675
1676 Instantiation->setTagKind(Pattern->getTagKind());
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001677
John McCall76d824f2009-08-25 22:02:44 +00001678 // Do substitution on the base class specifiers.
1679 if (SubstBaseSpecifiers(Instantiation, Pattern, TemplateArgs))
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001680 Invalid = true;
1681
Douglas Gregor869853e2010-11-10 19:44:59 +00001682 TemplateDeclInstantiator Instantiator(*this, Instantiation, TemplateArgs);
John McCall48871652010-08-21 09:40:31 +00001683 llvm::SmallVector<Decl*, 4> Fields;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001684 for (RecordDecl::decl_iterator Member = Pattern->decls_begin(),
Mike Stump11289f42009-09-09 15:08:12 +00001685 MemberEnd = Pattern->decls_end();
Douglas Gregorbcced4e2009-04-09 21:40:53 +00001686 Member != MemberEnd; ++Member) {
Argyrios Kyrtzidis9a94d9b2010-11-04 03:18:57 +00001687 // Don't instantiate members not belonging in this semantic context.
1688 // e.g. for:
1689 // @code
1690 // template <int i> class A {
1691 // class B *g;
1692 // };
1693 // @endcode
1694 // 'class B' has the template as lexical context but semantically it is
1695 // introduced in namespace scope.
1696 if ((*Member)->getDeclContext() != Pattern)
1697 continue;
1698
Douglas Gregor869853e2010-11-10 19:44:59 +00001699 if ((*Member)->isInvalidDecl()) {
1700 Invalid = true;
1701 continue;
1702 }
1703
1704 Decl *NewMember = Instantiator.Visit(*Member);
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001705 if (NewMember) {
Eli Friedmand0e8de22009-12-07 00:22:08 +00001706 if (FieldDecl *Field = dyn_cast<FieldDecl>(NewMember))
John McCall48871652010-08-21 09:40:31 +00001707 Fields.push_back(Field);
Eli Friedmand0e8de22009-12-07 00:22:08 +00001708 else if (NewMember->isInvalidDecl())
1709 Invalid = true;
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001710 } else {
1711 // FIXME: Eventually, a NULL return will mean that one of the
Mike Stump87c57ac2009-05-16 07:39:55 +00001712 // instantiations was a semantic disaster, and we'll want to set Invalid =
1713 // true. For now, we expect to skip some members that we can't yet handle.
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001714 }
1715 }
1716
1717 // Finish checking fields.
John McCall48871652010-08-21 09:40:31 +00001718 ActOnFields(0, Instantiation->getLocation(), Instantiation,
Jay Foad7d0479f2009-05-21 09:52:38 +00001719 Fields.data(), Fields.size(), SourceLocation(), SourceLocation(),
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001720 0);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001721 CheckCompletedCXXClass(Instantiation);
Douglas Gregor3c74d412009-10-14 20:14:33 +00001722 if (Instantiation->isInvalidDecl())
1723 Invalid = true;
Douglas Gregor869853e2010-11-10 19:44:59 +00001724 else {
1725 // Instantiate any out-of-line class template partial
1726 // specializations now.
1727 for (TemplateDeclInstantiator::delayed_partial_spec_iterator
1728 P = Instantiator.delayed_partial_spec_begin(),
1729 PEnd = Instantiator.delayed_partial_spec_end();
1730 P != PEnd; ++P) {
1731 if (!Instantiator.InstantiateClassTemplatePartialSpecialization(
1732 P->first,
1733 P->second)) {
1734 Invalid = true;
1735 break;
1736 }
1737 }
1738 }
1739
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001740 // Exit the scope of this instantiation.
John McCall80e58cd2010-04-29 00:35:03 +00001741 SavedContext.pop();
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001742
Douglas Gregor88d292c2010-05-13 16:44:06 +00001743 if (!Invalid) {
Douglas Gregor28ad4b52009-05-26 20:50:29 +00001744 Consumer.HandleTagDeclDefinition(Instantiation);
1745
Douglas Gregor88d292c2010-05-13 16:44:06 +00001746 // Always emit the vtable for an explicit instantiation definition
1747 // of a polymorphic class template specialization.
1748 if (TSK == TSK_ExplicitInstantiationDefinition)
1749 MarkVTableUsed(PointOfInstantiation, Instantiation, true);
1750 }
1751
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001752 return Invalid;
1753}
1754
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +00001755namespace {
1756 /// \brief A partial specialization whose template arguments have matched
1757 /// a given template-id.
1758 struct PartialSpecMatchResult {
1759 ClassTemplatePartialSpecializationDecl *Partial;
1760 TemplateArgumentList *Args;
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +00001761 };
1762}
1763
Mike Stump11289f42009-09-09 15:08:12 +00001764bool
Douglas Gregor463421d2009-03-03 04:44:36 +00001765Sema::InstantiateClassTemplateSpecialization(
Douglas Gregoref6ab412009-10-27 06:26:26 +00001766 SourceLocation PointOfInstantiation,
Douglas Gregor463421d2009-03-03 04:44:36 +00001767 ClassTemplateSpecializationDecl *ClassTemplateSpec,
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001768 TemplateSpecializationKind TSK,
Douglas Gregor8a2e6012009-08-24 15:23:48 +00001769 bool Complain) {
Douglas Gregor463421d2009-03-03 04:44:36 +00001770 // Perform the actual instantiation on the canonical declaration.
1771 ClassTemplateSpec = cast<ClassTemplateSpecializationDecl>(
Argyrios Kyrtzidis6b7e3762009-07-18 00:34:25 +00001772 ClassTemplateSpec->getCanonicalDecl());
Douglas Gregor463421d2009-03-03 04:44:36 +00001773
Douglas Gregor4aa04b12009-09-11 21:19:12 +00001774 // Check whether we have already instantiated or specialized this class
1775 // template specialization.
1776 if (ClassTemplateSpec->getSpecializationKind() != TSK_Undeclared) {
1777 if (ClassTemplateSpec->getSpecializationKind() ==
1778 TSK_ExplicitInstantiationDeclaration &&
1779 TSK == TSK_ExplicitInstantiationDefinition) {
1780 // An explicit instantiation definition follows an explicit instantiation
1781 // declaration (C++0x [temp.explicit]p10); go ahead and perform the
1782 // explicit instantiation.
1783 ClassTemplateSpec->setSpecializationKind(TSK);
Douglas Gregor88d292c2010-05-13 16:44:06 +00001784
1785 // If this is an explicit instantiation definition, mark the
1786 // vtable as used.
1787 if (TSK == TSK_ExplicitInstantiationDefinition)
1788 MarkVTableUsed(PointOfInstantiation, ClassTemplateSpec, true);
1789
Douglas Gregor4aa04b12009-09-11 21:19:12 +00001790 return false;
1791 }
1792
1793 // We can only instantiate something that hasn't already been
1794 // instantiated or specialized. Fail without any diagnostics: our
1795 // caller will provide an error message.
Douglas Gregor463421d2009-03-03 04:44:36 +00001796 return true;
Douglas Gregor4aa04b12009-09-11 21:19:12 +00001797 }
Douglas Gregor463421d2009-03-03 04:44:36 +00001798
Douglas Gregor00a511f2009-09-15 16:51:42 +00001799 if (ClassTemplateSpec->isInvalidDecl())
1800 return true;
1801
Douglas Gregor463421d2009-03-03 04:44:36 +00001802 ClassTemplateDecl *Template = ClassTemplateSpec->getSpecializedTemplate();
Douglas Gregor01afeef2009-08-28 20:31:08 +00001803 CXXRecordDecl *Pattern = 0;
Douglas Gregor2373c592009-05-31 09:31:02 +00001804
Douglas Gregor170bc422009-06-12 22:31:52 +00001805 // C++ [temp.class.spec.match]p1:
1806 // When a class template is used in a context that requires an
1807 // instantiation of the class, it is necessary to determine
1808 // whether the instantiation is to be generated using the primary
1809 // template or one of the partial specializations. This is done by
1810 // matching the template arguments of the class template
1811 // specialization with the template argument lists of the partial
1812 // specializations.
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +00001813 typedef PartialSpecMatchResult MatchResult;
Douglas Gregorb7ae10f2009-06-05 00:53:49 +00001814 llvm::SmallVector<MatchResult, 4> Matched;
Douglas Gregor407e9612010-04-30 05:56:50 +00001815 llvm::SmallVector<ClassTemplatePartialSpecializationDecl *, 4> PartialSpecs;
1816 Template->getPartialSpecializations(PartialSpecs);
1817 for (unsigned I = 0, N = PartialSpecs.size(); I != N; ++I) {
1818 ClassTemplatePartialSpecializationDecl *Partial = PartialSpecs[I];
John McCallbc077cf2010-02-08 23:07:23 +00001819 TemplateDeductionInfo Info(Context, PointOfInstantiation);
Douglas Gregor181aa4a2009-06-12 18:26:56 +00001820 if (TemplateDeductionResult Result
Douglas Gregor407e9612010-04-30 05:56:50 +00001821 = DeduceTemplateArguments(Partial,
Douglas Gregor181aa4a2009-06-12 18:26:56 +00001822 ClassTemplateSpec->getTemplateArgs(),
1823 Info)) {
1824 // FIXME: Store the failed-deduction information for use in
1825 // diagnostics, later.
1826 (void)Result;
1827 } else {
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +00001828 Matched.push_back(PartialSpecMatchResult());
1829 Matched.back().Partial = Partial;
1830 Matched.back().Args = Info.take();
Douglas Gregor181aa4a2009-06-12 18:26:56 +00001831 }
Douglas Gregor2373c592009-05-31 09:31:02 +00001832 }
1833
Douglas Gregor0231d8d2011-01-19 20:10:05 +00001834 // If we're dealing with a member template where the template parameters
1835 // have been instantiated, this provides the original template parameters
1836 // from which the member template's parameters were instantiated.
1837 llvm::SmallVector<const NamedDecl *, 4> InstantiatedTemplateParameters;
1838
Douglas Gregor21610382009-10-29 00:04:11 +00001839 if (Matched.size() >= 1) {
Douglas Gregorbe999392009-09-15 16:23:51 +00001840 llvm::SmallVector<MatchResult, 4>::iterator Best = Matched.begin();
Douglas Gregor21610382009-10-29 00:04:11 +00001841 if (Matched.size() == 1) {
1842 // -- If exactly one matching specialization is found, the
1843 // instantiation is generated from that specialization.
1844 // We don't need to do anything for this.
1845 } else {
1846 // -- If more than one matching specialization is found, the
1847 // partial order rules (14.5.4.2) are used to determine
1848 // whether one of the specializations is more specialized
1849 // than the others. If none of the specializations is more
1850 // specialized than all of the other matching
1851 // specializations, then the use of the class template is
1852 // ambiguous and the program is ill-formed.
1853 for (llvm::SmallVector<MatchResult, 4>::iterator P = Best + 1,
1854 PEnd = Matched.end();
1855 P != PEnd; ++P) {
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +00001856 if (getMoreSpecializedPartialSpecialization(P->Partial, Best->Partial,
John McCallbc077cf2010-02-08 23:07:23 +00001857 PointOfInstantiation)
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +00001858 == P->Partial)
Douglas Gregor21610382009-10-29 00:04:11 +00001859 Best = P;
Douglas Gregorbe999392009-09-15 16:23:51 +00001860 }
Douglas Gregorbe999392009-09-15 16:23:51 +00001861
Douglas Gregor21610382009-10-29 00:04:11 +00001862 // Determine if the best partial specialization is more specialized than
1863 // the others.
1864 bool Ambiguous = false;
Douglas Gregorbe999392009-09-15 16:23:51 +00001865 for (llvm::SmallVector<MatchResult, 4>::iterator P = Matched.begin(),
1866 PEnd = Matched.end();
Douglas Gregor21610382009-10-29 00:04:11 +00001867 P != PEnd; ++P) {
1868 if (P != Best &&
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +00001869 getMoreSpecializedPartialSpecialization(P->Partial, Best->Partial,
John McCallbc077cf2010-02-08 23:07:23 +00001870 PointOfInstantiation)
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +00001871 != Best->Partial) {
Douglas Gregor21610382009-10-29 00:04:11 +00001872 Ambiguous = true;
1873 break;
1874 }
1875 }
1876
1877 if (Ambiguous) {
1878 // Partial ordering did not produce a clear winner. Complain.
1879 ClassTemplateSpec->setInvalidDecl();
1880 Diag(PointOfInstantiation, diag::err_partial_spec_ordering_ambiguous)
1881 << ClassTemplateSpec;
1882
1883 // Print the matching partial specializations.
1884 for (llvm::SmallVector<MatchResult, 4>::iterator P = Matched.begin(),
1885 PEnd = Matched.end();
1886 P != PEnd; ++P)
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +00001887 Diag(P->Partial->getLocation(), diag::note_partial_spec_match)
1888 << getTemplateArgumentBindingsText(
1889 P->Partial->getTemplateParameters(),
1890 *P->Args);
Douglas Gregor01afeef2009-08-28 20:31:08 +00001891
Douglas Gregor21610382009-10-29 00:04:11 +00001892 return true;
1893 }
Douglas Gregorbe999392009-09-15 16:23:51 +00001894 }
1895
1896 // Instantiate using the best class template partial specialization.
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +00001897 ClassTemplatePartialSpecializationDecl *OrigPartialSpec = Best->Partial;
Douglas Gregor21610382009-10-29 00:04:11 +00001898 while (OrigPartialSpec->getInstantiatedFromMember()) {
1899 // If we've found an explicit specialization of this class template,
1900 // stop here and use that as the pattern.
1901 if (OrigPartialSpec->isMemberSpecialization())
1902 break;
1903
1904 OrigPartialSpec = OrigPartialSpec->getInstantiatedFromMember();
1905 }
1906
1907 Pattern = OrigPartialSpec;
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +00001908 ClassTemplateSpec->setInstantiationOf(Best->Partial, Best->Args);
Douglas Gregor170bc422009-06-12 22:31:52 +00001909 } else {
1910 // -- If no matches are found, the instantiation is generated
1911 // from the primary template.
Douglas Gregor01afeef2009-08-28 20:31:08 +00001912 ClassTemplateDecl *OrigTemplate = Template;
Douglas Gregorcf915552009-10-13 16:30:37 +00001913 while (OrigTemplate->getInstantiatedFromMemberTemplate()) {
1914 // If we've found an explicit specialization of this class template,
1915 // stop here and use that as the pattern.
1916 if (OrigTemplate->isMemberSpecialization())
1917 break;
1918
Douglas Gregor01afeef2009-08-28 20:31:08 +00001919 OrigTemplate = OrigTemplate->getInstantiatedFromMemberTemplate();
Douglas Gregorcf915552009-10-13 16:30:37 +00001920 }
1921
Douglas Gregor01afeef2009-08-28 20:31:08 +00001922 Pattern = OrigTemplate->getTemplatedDecl();
Douglas Gregor2373c592009-05-31 09:31:02 +00001923 }
Douglas Gregor463421d2009-03-03 04:44:36 +00001924
Douglas Gregoref6ab412009-10-27 06:26:26 +00001925 bool Result = InstantiateClass(PointOfInstantiation, ClassTemplateSpec,
1926 Pattern,
1927 getTemplateInstantiationArgs(ClassTemplateSpec),
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001928 TSK,
Douglas Gregor8a2e6012009-08-24 15:23:48 +00001929 Complain);
Mike Stump11289f42009-09-09 15:08:12 +00001930
Douglas Gregorb7ae10f2009-06-05 00:53:49 +00001931 return Result;
Douglas Gregor463421d2009-03-03 04:44:36 +00001932}
Douglas Gregor90a1a652009-03-19 17:26:29 +00001933
John McCall76d824f2009-08-25 22:02:44 +00001934/// \brief Instantiates the definitions of all of the member
1935/// of the given class, which is an instantiation of a class template
1936/// or a member class of a template.
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00001937void
1938Sema::InstantiateClassMembers(SourceLocation PointOfInstantiation,
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001939 CXXRecordDecl *Instantiation,
1940 const MultiLevelTemplateArgumentList &TemplateArgs,
1941 TemplateSpecializationKind TSK) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001942 for (DeclContext::decl_iterator D = Instantiation->decls_begin(),
1943 DEnd = Instantiation->decls_end();
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00001944 D != DEnd; ++D) {
Douglas Gregor1d957a32009-10-27 18:42:08 +00001945 bool SuppressNew = false;
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00001946 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(*D)) {
Douglas Gregor1d957a32009-10-27 18:42:08 +00001947 if (FunctionDecl *Pattern
1948 = Function->getInstantiatedFromMemberFunction()) {
1949 MemberSpecializationInfo *MSInfo
1950 = Function->getMemberSpecializationInfo();
1951 assert(MSInfo && "No member specialization information?");
Douglas Gregor06aa50412010-04-09 21:02:29 +00001952 if (MSInfo->getTemplateSpecializationKind()
1953 == TSK_ExplicitSpecialization)
1954 continue;
1955
Douglas Gregor1d957a32009-10-27 18:42:08 +00001956 if (CheckSpecializationInstantiationRedecl(PointOfInstantiation, TSK,
1957 Function,
1958 MSInfo->getTemplateSpecializationKind(),
Nico Weberd75488d2010-09-27 21:02:09 +00001959 MSInfo->getPointOfInstantiation(),
Douglas Gregor1d957a32009-10-27 18:42:08 +00001960 SuppressNew) ||
1961 SuppressNew)
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001962 continue;
1963
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001964 if (Function->hasBody())
Douglas Gregor1d957a32009-10-27 18:42:08 +00001965 continue;
1966
1967 if (TSK == TSK_ExplicitInstantiationDefinition) {
1968 // C++0x [temp.explicit]p8:
1969 // An explicit instantiation definition that names a class template
1970 // specialization explicitly instantiates the class template
1971 // specialization and is only an explicit instantiation definition
1972 // of members whose definition is visible at the point of
1973 // instantiation.
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001974 if (!Pattern->hasBody())
Douglas Gregor1d957a32009-10-27 18:42:08 +00001975 continue;
1976
1977 Function->setTemplateSpecializationKind(TSK, PointOfInstantiation);
1978
1979 InstantiateFunctionDefinition(PointOfInstantiation, Function);
1980 } else {
1981 Function->setTemplateSpecializationKind(TSK, PointOfInstantiation);
1982 }
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001983 }
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00001984 } else if (VarDecl *Var = dyn_cast<VarDecl>(*D)) {
Douglas Gregor86d142a2009-10-08 07:24:58 +00001985 if (Var->isStaticDataMember()) {
Douglas Gregor1d957a32009-10-27 18:42:08 +00001986 MemberSpecializationInfo *MSInfo = Var->getMemberSpecializationInfo();
1987 assert(MSInfo && "No member specialization information?");
Douglas Gregor06aa50412010-04-09 21:02:29 +00001988 if (MSInfo->getTemplateSpecializationKind()
1989 == TSK_ExplicitSpecialization)
1990 continue;
1991
Douglas Gregor1d957a32009-10-27 18:42:08 +00001992 if (CheckSpecializationInstantiationRedecl(PointOfInstantiation, TSK,
1993 Var,
1994 MSInfo->getTemplateSpecializationKind(),
Nico Weberd75488d2010-09-27 21:02:09 +00001995 MSInfo->getPointOfInstantiation(),
Douglas Gregor1d957a32009-10-27 18:42:08 +00001996 SuppressNew) ||
1997 SuppressNew)
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001998 continue;
1999
Douglas Gregor1d957a32009-10-27 18:42:08 +00002000 if (TSK == TSK_ExplicitInstantiationDefinition) {
2001 // C++0x [temp.explicit]p8:
2002 // An explicit instantiation definition that names a class template
2003 // specialization explicitly instantiates the class template
2004 // specialization and is only an explicit instantiation definition
2005 // of members whose definition is visible at the point of
2006 // instantiation.
2007 if (!Var->getInstantiatedFromStaticDataMember()
2008 ->getOutOfLineDefinition())
2009 continue;
2010
2011 Var->setTemplateSpecializationKind(TSK, PointOfInstantiation);
Douglas Gregor86d142a2009-10-08 07:24:58 +00002012 InstantiateStaticDataMemberDefinition(PointOfInstantiation, Var);
Douglas Gregor1d957a32009-10-27 18:42:08 +00002013 } else {
2014 Var->setTemplateSpecializationKind(TSK, PointOfInstantiation);
2015 }
2016 }
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00002017 } else if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(*D)) {
Douglas Gregor1da22252010-04-18 18:11:38 +00002018 // Always skip the injected-class-name, along with any
2019 // redeclarations of nested classes, since both would cause us
2020 // to try to instantiate the members of a class twice.
2021 if (Record->isInjectedClassName() || Record->getPreviousDeclaration())
Douglas Gregord801b062009-10-07 23:56:10 +00002022 continue;
2023
Douglas Gregor1d957a32009-10-27 18:42:08 +00002024 MemberSpecializationInfo *MSInfo = Record->getMemberSpecializationInfo();
2025 assert(MSInfo && "No member specialization information?");
Douglas Gregor06aa50412010-04-09 21:02:29 +00002026
2027 if (MSInfo->getTemplateSpecializationKind()
2028 == TSK_ExplicitSpecialization)
2029 continue;
Nico Weberd75488d2010-09-27 21:02:09 +00002030
Douglas Gregor1d957a32009-10-27 18:42:08 +00002031 if (CheckSpecializationInstantiationRedecl(PointOfInstantiation, TSK,
2032 Record,
2033 MSInfo->getTemplateSpecializationKind(),
Nico Weberd75488d2010-09-27 21:02:09 +00002034 MSInfo->getPointOfInstantiation(),
Douglas Gregor1d957a32009-10-27 18:42:08 +00002035 SuppressNew) ||
2036 SuppressNew)
Douglas Gregorbbe8f462009-10-08 15:14:33 +00002037 continue;
2038
Douglas Gregor1d957a32009-10-27 18:42:08 +00002039 CXXRecordDecl *Pattern = Record->getInstantiatedFromMemberClass();
2040 assert(Pattern && "Missing instantiated-from-template information");
2041
Douglas Gregor0a5a2212010-02-11 01:04:33 +00002042 if (!Record->getDefinition()) {
2043 if (!Pattern->getDefinition()) {
Douglas Gregor1d957a32009-10-27 18:42:08 +00002044 // C++0x [temp.explicit]p8:
2045 // An explicit instantiation definition that names a class template
2046 // specialization explicitly instantiates the class template
2047 // specialization and is only an explicit instantiation definition
2048 // of members whose definition is visible at the point of
2049 // instantiation.
2050 if (TSK == TSK_ExplicitInstantiationDeclaration) {
2051 MSInfo->setTemplateSpecializationKind(TSK);
2052 MSInfo->setPointOfInstantiation(PointOfInstantiation);
2053 }
2054
2055 continue;
2056 }
2057
2058 InstantiateClass(PointOfInstantiation, Record, Pattern,
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00002059 TemplateArgs,
2060 TSK);
Nico Weberd75488d2010-09-27 21:02:09 +00002061 } else {
2062 if (TSK == TSK_ExplicitInstantiationDefinition &&
2063 Record->getTemplateSpecializationKind() ==
2064 TSK_ExplicitInstantiationDeclaration) {
2065 Record->setTemplateSpecializationKind(TSK);
2066 MarkVTableUsed(PointOfInstantiation, Record, true);
2067 }
Douglas Gregor1d957a32009-10-27 18:42:08 +00002068 }
Douglas Gregorc093c1d2009-10-08 01:19:17 +00002069
Douglas Gregor0a5a2212010-02-11 01:04:33 +00002070 Pattern = cast_or_null<CXXRecordDecl>(Record->getDefinition());
Douglas Gregor1d957a32009-10-27 18:42:08 +00002071 if (Pattern)
2072 InstantiateClassMembers(PointOfInstantiation, Pattern, TemplateArgs,
2073 TSK);
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00002074 }
2075 }
2076}
2077
2078/// \brief Instantiate the definitions of all of the members of the
2079/// given class template specialization, which was named as part of an
2080/// explicit instantiation.
Mike Stump11289f42009-09-09 15:08:12 +00002081void
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00002082Sema::InstantiateClassTemplateSpecializationMembers(
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00002083 SourceLocation PointOfInstantiation,
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00002084 ClassTemplateSpecializationDecl *ClassTemplateSpec,
2085 TemplateSpecializationKind TSK) {
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00002086 // C++0x [temp.explicit]p7:
2087 // An explicit instantiation that names a class template
2088 // specialization is an explicit instantion of the same kind
2089 // (declaration or definition) of each of its members (not
2090 // including members inherited from base classes) that has not
2091 // been previously explicitly specialized in the translation unit
2092 // containing the explicit instantiation, except as described
2093 // below.
2094 InstantiateClassMembers(PointOfInstantiation, ClassTemplateSpec,
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00002095 getTemplateInstantiationArgs(ClassTemplateSpec),
2096 TSK);
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00002097}
2098
John McCalldadc5752010-08-24 06:29:42 +00002099StmtResult
Douglas Gregor01afeef2009-08-28 20:31:08 +00002100Sema::SubstStmt(Stmt *S, const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregorebe10102009-08-20 07:17:43 +00002101 if (!S)
2102 return Owned(S);
2103
2104 TemplateInstantiator Instantiator(*this, TemplateArgs,
2105 SourceLocation(),
2106 DeclarationName());
2107 return Instantiator.TransformStmt(S);
2108}
2109
John McCalldadc5752010-08-24 06:29:42 +00002110ExprResult
Douglas Gregor01afeef2009-08-28 20:31:08 +00002111Sema::SubstExpr(Expr *E, const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregora16548e2009-08-11 05:31:07 +00002112 if (!E)
2113 return Owned(E);
Mike Stump11289f42009-09-09 15:08:12 +00002114
Douglas Gregora16548e2009-08-11 05:31:07 +00002115 TemplateInstantiator Instantiator(*this, TemplateArgs,
2116 SourceLocation(),
2117 DeclarationName());
2118 return Instantiator.TransformExpr(E);
2119}
2120
Douglas Gregor2cd32a02011-01-07 19:35:17 +00002121bool Sema::SubstExprs(Expr **Exprs, unsigned NumExprs, bool IsCall,
2122 const MultiLevelTemplateArgumentList &TemplateArgs,
2123 llvm::SmallVectorImpl<Expr *> &Outputs) {
2124 if (NumExprs == 0)
2125 return false;
2126
2127 TemplateInstantiator Instantiator(*this, TemplateArgs,
2128 SourceLocation(),
2129 DeclarationName());
2130 return Instantiator.TransformExprs(Exprs, NumExprs, IsCall, Outputs);
2131}
2132
John McCall76d824f2009-08-25 22:02:44 +00002133/// \brief Do template substitution on a nested-name-specifier.
Douglas Gregorf21eb492009-03-26 23:50:42 +00002134NestedNameSpecifier *
John McCall76d824f2009-08-25 22:02:44 +00002135Sema::SubstNestedNameSpecifier(NestedNameSpecifier *NNS,
Douglas Gregor01afeef2009-08-28 20:31:08 +00002136 SourceRange Range,
2137 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor1135c352009-08-06 05:28:30 +00002138 TemplateInstantiator Instantiator(*this, TemplateArgs, Range.getBegin(),
2139 DeclarationName());
Douglas Gregorcd3f49f2010-02-25 04:46:04 +00002140 return Instantiator.TransformNestedNameSpecifier(NNS, Range);
Douglas Gregor90a1a652009-03-19 17:26:29 +00002141}
Douglas Gregoraa594892009-03-31 18:38:02 +00002142
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002143/// \brief Do template substitution on declaration name info.
2144DeclarationNameInfo
2145Sema::SubstDeclarationNameInfo(const DeclarationNameInfo &NameInfo,
2146 const MultiLevelTemplateArgumentList &TemplateArgs) {
2147 TemplateInstantiator Instantiator(*this, TemplateArgs, NameInfo.getLoc(),
2148 NameInfo.getName());
2149 return Instantiator.TransformDeclarationNameInfo(NameInfo);
2150}
2151
Douglas Gregoraa594892009-03-31 18:38:02 +00002152TemplateName
John McCall76d824f2009-08-25 22:02:44 +00002153Sema::SubstTemplateName(TemplateName Name, SourceLocation Loc,
Douglas Gregor01afeef2009-08-28 20:31:08 +00002154 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor71dc5092009-08-06 06:41:21 +00002155 TemplateInstantiator Instantiator(*this, TemplateArgs, Loc,
2156 DeclarationName());
2157 return Instantiator.TransformTemplateName(Name);
Douglas Gregoraa594892009-03-31 18:38:02 +00002158}
Douglas Gregorc43620d2009-06-11 00:06:24 +00002159
Douglas Gregor0f3feb42010-12-22 21:19:48 +00002160bool Sema::Subst(const TemplateArgumentLoc *Args, unsigned NumArgs,
2161 TemplateArgumentListInfo &Result,
John McCall0ad16662009-10-29 08:12:44 +00002162 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregore922c772009-08-04 22:27:00 +00002163 TemplateInstantiator Instantiator(*this, TemplateArgs, SourceLocation(),
2164 DeclarationName());
Douglas Gregor0f3feb42010-12-22 21:19:48 +00002165
2166 return Instantiator.TransformTemplateArguments(Args, NumArgs, Result);
Douglas Gregorc43620d2009-06-11 00:06:24 +00002167}
Douglas Gregor14cf7522010-04-30 18:55:50 +00002168
John McCall19c1bfd2010-08-25 05:32:35 +00002169Decl *LocalInstantiationScope::getInstantiationOf(const Decl *D) {
Douglas Gregorf3010112011-01-07 16:43:16 +00002170 llvm::PointerUnion<Decl *, DeclArgumentPack *> *Found= findInstantiationOf(D);
2171 if (!Found)
2172 return 0;
2173
2174 if (Found->is<Decl *>())
2175 return Found->get<Decl *>();
2176
2177 return (*Found->get<DeclArgumentPack *>())[
2178 SemaRef.ArgumentPackSubstitutionIndex];
2179}
2180
2181llvm::PointerUnion<Decl *, LocalInstantiationScope::DeclArgumentPack *> *
2182LocalInstantiationScope::findInstantiationOf(const Decl *D) {
Douglas Gregor14cf7522010-04-30 18:55:50 +00002183 for (LocalInstantiationScope *Current = this; Current;
2184 Current = Current->Outer) {
2185 // Check if we found something within this scope.
Douglas Gregore9fc8dc2010-12-21 21:22:51 +00002186 const Decl *CheckD = D;
2187 do {
Douglas Gregorf3010112011-01-07 16:43:16 +00002188 LocalDeclsMap::iterator Found = Current->LocalDecls.find(CheckD);
Douglas Gregore9fc8dc2010-12-21 21:22:51 +00002189 if (Found != Current->LocalDecls.end())
Douglas Gregorf3010112011-01-07 16:43:16 +00002190 return &Found->second;
Douglas Gregore9fc8dc2010-12-21 21:22:51 +00002191
2192 // If this is a tag declaration, it's possible that we need to look for
2193 // a previous declaration.
2194 if (const TagDecl *Tag = dyn_cast<TagDecl>(CheckD))
2195 CheckD = Tag->getPreviousDeclaration();
2196 else
2197 CheckD = 0;
2198 } while (CheckD);
2199
Douglas Gregor14cf7522010-04-30 18:55:50 +00002200 // If we aren't combined with our outer scope, we're done.
2201 if (!Current->CombineWithOuterScope)
2202 break;
2203 }
2204
2205 assert(D->isInvalidDecl() &&
2206 "declaration was not instantiated in this scope!");
2207 return 0;
2208}
2209
John McCall19c1bfd2010-08-25 05:32:35 +00002210void LocalInstantiationScope::InstantiatedLocal(const Decl *D, Decl *Inst) {
Douglas Gregorf3010112011-01-07 16:43:16 +00002211 llvm::PointerUnion<Decl *, DeclArgumentPack *> &Stored = LocalDecls[D];
Douglas Gregora8bac7f2011-01-10 07:32:04 +00002212 if (Stored.isNull())
2213 Stored = Inst;
2214 else if (Stored.is<Decl *>()) {
2215 assert(Stored.get<Decl *>() == Inst && "Already instantiated this local");
2216 Stored = Inst;
2217 } else
2218 LocalDecls[D].get<DeclArgumentPack *>()->push_back(Inst);
Douglas Gregor14cf7522010-04-30 18:55:50 +00002219}
Douglas Gregorf3010112011-01-07 16:43:16 +00002220
2221void LocalInstantiationScope::InstantiatedLocalPackArg(const Decl *D,
2222 Decl *Inst) {
2223 DeclArgumentPack *Pack = LocalDecls[D].get<DeclArgumentPack *>();
2224 Pack->push_back(Inst);
2225}
2226
2227void LocalInstantiationScope::MakeInstantiatedLocalArgPack(const Decl *D) {
2228 llvm::PointerUnion<Decl *, DeclArgumentPack *> &Stored = LocalDecls[D];
2229 assert(Stored.isNull() && "Already instantiated this local");
2230 DeclArgumentPack *Pack = new DeclArgumentPack;
2231 Stored = Pack;
2232 ArgumentPacks.push_back(Pack);
2233}
2234
Douglas Gregora8bac7f2011-01-10 07:32:04 +00002235void LocalInstantiationScope::SetPartiallySubstitutedPack(NamedDecl *Pack,
2236 const TemplateArgument *ExplicitArgs,
2237 unsigned NumExplicitArgs) {
2238 assert((!PartiallySubstitutedPack || PartiallySubstitutedPack == Pack) &&
2239 "Already have a partially-substituted pack");
2240 assert((!PartiallySubstitutedPack
2241 || NumArgsInPartiallySubstitutedPack == NumExplicitArgs) &&
2242 "Wrong number of arguments in partially-substituted pack");
2243 PartiallySubstitutedPack = Pack;
2244 ArgsInPartiallySubstitutedPack = ExplicitArgs;
2245 NumArgsInPartiallySubstitutedPack = NumExplicitArgs;
2246}
2247
2248NamedDecl *LocalInstantiationScope::getPartiallySubstitutedPack(
2249 const TemplateArgument **ExplicitArgs,
2250 unsigned *NumExplicitArgs) const {
2251 if (ExplicitArgs)
2252 *ExplicitArgs = 0;
2253 if (NumExplicitArgs)
2254 *NumExplicitArgs = 0;
2255
2256 for (const LocalInstantiationScope *Current = this; Current;
2257 Current = Current->Outer) {
2258 if (Current->PartiallySubstitutedPack) {
2259 if (ExplicitArgs)
2260 *ExplicitArgs = Current->ArgsInPartiallySubstitutedPack;
2261 if (NumExplicitArgs)
2262 *NumExplicitArgs = Current->NumArgsInPartiallySubstitutedPack;
2263
2264 return Current->PartiallySubstitutedPack;
2265 }
2266
2267 if (!Current->CombineWithOuterScope)
2268 break;
2269 }
2270
2271 return 0;
2272}