blob: c0f130da88ac804142eb99d17da1167ff0d397e6 [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,
Douglas Gregor844cb502011-03-01 18:12:44 +0000733 NestedNameSpecifierLoc QualifierLoc,
734 QualType T);
John McCall7f41d982009-09-11 04:59:25 +0000735
Douglas Gregor5590be02011-01-15 06:45:20 +0000736 TemplateName TransformTemplateName(TemplateName Name,
737 QualType ObjectType = QualType(),
738 NamedDecl *FirstQualifierInScope = 0);
739
John McCalldadc5752010-08-24 06:29:42 +0000740 ExprResult TransformPredefinedExpr(PredefinedExpr *E);
741 ExprResult TransformDeclRefExpr(DeclRefExpr *E);
742 ExprResult TransformCXXDefaultArgExpr(CXXDefaultArgExpr *E);
743 ExprResult TransformTemplateParmRefExpr(DeclRefExpr *E,
Douglas Gregoreb5a39d2010-12-24 00:15:10 +0000744 NonTypeTemplateParmDecl *D);
Douglas Gregorcdbc5392011-01-15 01:15:58 +0000745 ExprResult TransformSubstNonTypeTemplateParmPackExpr(
746 SubstNonTypeTemplateParmPackExpr *E);
747
Douglas Gregor14cf7522010-04-30 18:55:50 +0000748 QualType TransformFunctionProtoType(TypeLocBuilder &TLB,
John McCall31f82722010-11-12 08:19:04 +0000749 FunctionProtoTypeLoc TL);
Douglas Gregor715e4612011-01-14 22:40:04 +0000750 ParmVarDecl *TransformFunctionTypeParam(ParmVarDecl *OldParm,
751 llvm::Optional<unsigned> NumExpansions);
John McCall58f10c32010-03-11 09:03:00 +0000752
Mike Stump11289f42009-09-09 15:08:12 +0000753 /// \brief Transforms a template type parameter type by performing
Douglas Gregord6ff3322009-08-04 16:50:30 +0000754 /// substitution of the corresponding template type argument.
John McCall550e0c22009-10-21 00:40:46 +0000755 QualType TransformTemplateTypeParmType(TypeLocBuilder &TLB,
John McCall31f82722010-11-12 08:19:04 +0000756 TemplateTypeParmTypeLoc TL);
Nick Lewyckyc96c37f2010-07-06 19:51:49 +0000757
Douglas Gregorada4b792011-01-14 02:55:32 +0000758 /// \brief Transforms an already-substituted template type parameter pack
759 /// into either itself (if we aren't substituting into its pack expansion)
760 /// or the appropriate substituted argument.
761 QualType TransformSubstTemplateTypeParmPackType(TypeLocBuilder &TLB,
762 SubstTemplateTypeParmPackTypeLoc TL);
763
John McCalldadc5752010-08-24 06:29:42 +0000764 ExprResult TransformCallExpr(CallExpr *CE) {
Nick Lewyckyc96c37f2010-07-06 19:51:49 +0000765 getSema().CallsUndergoingInstantiation.push_back(CE);
John McCalldadc5752010-08-24 06:29:42 +0000766 ExprResult Result =
Nick Lewyckyc96c37f2010-07-06 19:51:49 +0000767 TreeTransform<TemplateInstantiator>::TransformCallExpr(CE);
768 getSema().CallsUndergoingInstantiation.pop_back();
769 return move(Result);
770 }
Douglas Gregord6ff3322009-08-04 16:50:30 +0000771 };
Douglas Gregor04318252009-07-06 15:59:29 +0000772}
773
Douglas Gregor5597ab42010-05-07 23:12:07 +0000774bool TemplateInstantiator::AlreadyTransformed(QualType T) {
775 if (T.isNull())
776 return true;
777
Douglas Gregor5a5073e2010-05-24 17:22:01 +0000778 if (T->isDependentType() || T->isVariablyModifiedType())
Douglas Gregor5597ab42010-05-07 23:12:07 +0000779 return false;
780
781 getSema().MarkDeclarationsReferencedInType(Loc, T);
782 return true;
783}
784
Douglas Gregora04f2ca2010-03-01 15:56:25 +0000785Decl *TemplateInstantiator::TransformDecl(SourceLocation Loc, Decl *D) {
Douglas Gregor2b6ca462009-09-03 21:38:09 +0000786 if (!D)
787 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000788
Douglas Gregor2b6ca462009-09-03 21:38:09 +0000789 if (TemplateTemplateParmDecl *TTP = dyn_cast<TemplateTemplateParmDecl>(D)) {
Douglas Gregor01afeef2009-08-28 20:31:08 +0000790 if (TTP->getDepth() < TemplateArgs.getNumLevels()) {
Douglas Gregorb93971082010-02-05 19:54:12 +0000791 // If the corresponding template argument is NULL or non-existent, it's
792 // because we are performing instantiation from explicitly-specified
793 // template arguments in a function template, but there were some
794 // arguments left unspecified.
795 if (!TemplateArgs.hasTemplateArgument(TTP->getDepth(),
796 TTP->getPosition()))
797 return D;
798
Douglas Gregorf5500772011-01-05 15:48:55 +0000799 TemplateArgument Arg = TemplateArgs(TTP->getDepth(), TTP->getPosition());
800
801 if (TTP->isParameterPack()) {
802 assert(Arg.getKind() == TemplateArgument::Pack &&
803 "Missing argument pack");
804
Douglas Gregor5590be02011-01-15 06:45:20 +0000805 assert(getSema().ArgumentPackSubstitutionIndex >= 0);
Douglas Gregora8bac7f2011-01-10 07:32:04 +0000806 assert(getSema().ArgumentPackSubstitutionIndex < (int)Arg.pack_size());
Douglas Gregorf5500772011-01-05 15:48:55 +0000807 Arg = Arg.pack_begin()[getSema().ArgumentPackSubstitutionIndex];
808 }
809
810 TemplateName Template = Arg.getAsTemplate();
Douglas Gregor9167f8b2009-11-11 01:00:40 +0000811 assert(!Template.isNull() && Template.getAsTemplateDecl() &&
Douglas Gregor01afeef2009-08-28 20:31:08 +0000812 "Wrong kind of template template argument");
Douglas Gregor9167f8b2009-11-11 01:00:40 +0000813 return Template.getAsTemplateDecl();
Douglas Gregor01afeef2009-08-28 20:31:08 +0000814 }
Mike Stump11289f42009-09-09 15:08:12 +0000815
Douglas Gregor9167f8b2009-11-11 01:00:40 +0000816 // Fall through to find the instantiated declaration for this template
817 // template parameter.
Douglas Gregor71dc5092009-08-06 06:41:21 +0000818 }
Mike Stump11289f42009-09-09 15:08:12 +0000819
Douglas Gregora04f2ca2010-03-01 15:56:25 +0000820 return SemaRef.FindInstantiatedDecl(Loc, cast<NamedDecl>(D), TemplateArgs);
Douglas Gregord6ff3322009-08-04 16:50:30 +0000821}
822
Douglas Gregor25289362010-03-01 17:25:41 +0000823Decl *TemplateInstantiator::TransformDefinition(SourceLocation Loc, Decl *D) {
John McCall76d824f2009-08-25 22:02:44 +0000824 Decl *Inst = getSema().SubstDecl(D, getSema().CurContext, TemplateArgs);
Douglas Gregorebe10102009-08-20 07:17:43 +0000825 if (!Inst)
826 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000827
Douglas Gregorebe10102009-08-20 07:17:43 +0000828 getSema().CurrentInstantiationScope->InstantiatedLocal(D, Inst);
829 return Inst;
830}
831
Douglas Gregora5cb6da2009-10-20 05:58:46 +0000832NamedDecl *
833TemplateInstantiator::TransformFirstQualifierInScope(NamedDecl *D,
834 SourceLocation Loc) {
835 // If the first part of the nested-name-specifier was a template type
836 // parameter, instantiate that type parameter down to a tag type.
837 if (TemplateTypeParmDecl *TTPD = dyn_cast_or_null<TemplateTypeParmDecl>(D)) {
838 const TemplateTypeParmType *TTP
839 = cast<TemplateTypeParmType>(getSema().Context.getTypeDeclType(TTPD));
Douglas Gregor53c3f4e2010-12-20 22:48:17 +0000840
Douglas Gregora5cb6da2009-10-20 05:58:46 +0000841 if (TTP->getDepth() < TemplateArgs.getNumLevels()) {
Douglas Gregor53c3f4e2010-12-20 22:48:17 +0000842 // FIXME: This needs testing w/ member access expressions.
843 TemplateArgument Arg = TemplateArgs(TTP->getDepth(), TTP->getIndex());
844
845 if (TTP->isParameterPack()) {
846 assert(Arg.getKind() == TemplateArgument::Pack &&
847 "Missing argument pack");
848
Douglas Gregore1d60df2011-01-14 23:41:42 +0000849 if (getSema().ArgumentPackSubstitutionIndex == -1)
Douglas Gregor53c3f4e2010-12-20 22:48:17 +0000850 return 0;
Douglas Gregor53c3f4e2010-12-20 22:48:17 +0000851
Douglas Gregora8bac7f2011-01-10 07:32:04 +0000852 assert(getSema().ArgumentPackSubstitutionIndex < (int)Arg.pack_size());
Douglas Gregor53c3f4e2010-12-20 22:48:17 +0000853 Arg = Arg.pack_begin()[getSema().ArgumentPackSubstitutionIndex];
854 }
855
856 QualType T = Arg.getAsType();
Douglas Gregora5cb6da2009-10-20 05:58:46 +0000857 if (T.isNull())
Douglas Gregora04f2ca2010-03-01 15:56:25 +0000858 return cast_or_null<NamedDecl>(TransformDecl(Loc, D));
Douglas Gregora5cb6da2009-10-20 05:58:46 +0000859
860 if (const TagType *Tag = T->getAs<TagType>())
861 return Tag->getDecl();
862
863 // The resulting type is not a tag; complain.
864 getSema().Diag(Loc, diag::err_nested_name_spec_non_tag) << T;
865 return 0;
866 }
867 }
868
Douglas Gregora04f2ca2010-03-01 15:56:25 +0000869 return cast_or_null<NamedDecl>(TransformDecl(Loc, D));
Douglas Gregora5cb6da2009-10-20 05:58:46 +0000870}
871
Douglas Gregorebe10102009-08-20 07:17:43 +0000872VarDecl *
873TemplateInstantiator::RebuildExceptionDecl(VarDecl *ExceptionDecl,
John McCallbcd03502009-12-07 02:54:59 +0000874 TypeSourceInfo *Declarator,
Douglas Gregorebe10102009-08-20 07:17:43 +0000875 IdentifierInfo *Name,
Douglas Gregor9f0e1aa2010-09-09 17:09:21 +0000876 SourceLocation Loc) {
877 VarDecl *Var = inherited::RebuildExceptionDecl(ExceptionDecl, Declarator,
878 Name, Loc);
Douglas Gregorf4e837f2010-04-26 17:57:08 +0000879 if (Var)
880 getSema().CurrentInstantiationScope->InstantiatedLocal(ExceptionDecl, Var);
881 return Var;
882}
883
884VarDecl *TemplateInstantiator::RebuildObjCExceptionDecl(VarDecl *ExceptionDecl,
885 TypeSourceInfo *TSInfo,
886 QualType T) {
887 VarDecl *Var = inherited::RebuildObjCExceptionDecl(ExceptionDecl, TSInfo, T);
888 if (Var)
Douglas Gregorebe10102009-08-20 07:17:43 +0000889 getSema().CurrentInstantiationScope->InstantiatedLocal(ExceptionDecl, Var);
890 return Var;
891}
892
John McCall7f41d982009-09-11 04:59:25 +0000893QualType
John McCall954b5de2010-11-04 19:04:38 +0000894TemplateInstantiator::RebuildElaboratedType(SourceLocation KeywordLoc,
895 ElaboratedTypeKeyword Keyword,
Douglas Gregor844cb502011-03-01 18:12:44 +0000896 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara6150c882010-05-11 21:36:43 +0000897 QualType T) {
John McCall7f41d982009-09-11 04:59:25 +0000898 if (const TagType *TT = T->getAs<TagType>()) {
899 TagDecl* TD = TT->getDecl();
900
John McCall954b5de2010-11-04 19:04:38 +0000901 SourceLocation TagLocation = KeywordLoc;
John McCall7f41d982009-09-11 04:59:25 +0000902
903 // FIXME: type might be anonymous.
904 IdentifierInfo *Id = TD->getIdentifier();
905
906 // TODO: should we even warn on struct/class mismatches for this? Seems
907 // like it's likely to produce a lot of spurious errors.
Abramo Bagnara6150c882010-05-11 21:36:43 +0000908 if (Keyword != ETK_None && Keyword != ETK_Typename) {
909 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForKeyword(Keyword);
910 if (!SemaRef.isAcceptableTagRedeclaration(TD, Kind, TagLocation, *Id)) {
911 SemaRef.Diag(TagLocation, diag::err_use_with_wrong_tag)
912 << Id
913 << FixItHint::CreateReplacement(SourceRange(TagLocation),
914 TD->getKindName());
915 SemaRef.Diag(TD->getLocation(), diag::note_previous_use);
916 }
John McCall7f41d982009-09-11 04:59:25 +0000917 }
918 }
919
John McCall954b5de2010-11-04 19:04:38 +0000920 return TreeTransform<TemplateInstantiator>::RebuildElaboratedType(KeywordLoc,
921 Keyword,
Douglas Gregor844cb502011-03-01 18:12:44 +0000922 QualifierLoc,
923 T);
John McCall7f41d982009-09-11 04:59:25 +0000924}
925
Douglas Gregor5590be02011-01-15 06:45:20 +0000926TemplateName TemplateInstantiator::TransformTemplateName(TemplateName Name,
927 QualType ObjectType,
928 NamedDecl *FirstQualifierInScope) {
929 if (TemplateTemplateParmDecl *TTP
930 = dyn_cast_or_null<TemplateTemplateParmDecl>(Name.getAsTemplateDecl())) {
931 if (TTP->getDepth() < TemplateArgs.getNumLevels()) {
932 // If the corresponding template argument is NULL or non-existent, it's
933 // because we are performing instantiation from explicitly-specified
934 // template arguments in a function template, but there were some
935 // arguments left unspecified.
936 if (!TemplateArgs.hasTemplateArgument(TTP->getDepth(),
937 TTP->getPosition()))
938 return Name;
939
940 TemplateArgument Arg = TemplateArgs(TTP->getDepth(), TTP->getPosition());
941
942 if (TTP->isParameterPack()) {
943 assert(Arg.getKind() == TemplateArgument::Pack &&
944 "Missing argument pack");
945
946 if (getSema().ArgumentPackSubstitutionIndex == -1) {
947 // We have the template argument pack to substitute, but we're not
948 // actually expanding the enclosing pack expansion yet. So, just
949 // keep the entire argument pack.
950 return getSema().Context.getSubstTemplateTemplateParmPack(TTP, Arg);
951 }
952
953 assert(getSema().ArgumentPackSubstitutionIndex < (int)Arg.pack_size());
954 Arg = Arg.pack_begin()[getSema().ArgumentPackSubstitutionIndex];
955 }
956
957 TemplateName Template = Arg.getAsTemplate();
958 assert(!Template.isNull() && Template.getAsTemplateDecl() &&
959 "Wrong kind of template template argument");
960 return Template;
961 }
962 }
963
964 if (SubstTemplateTemplateParmPackStorage *SubstPack
965 = Name.getAsSubstTemplateTemplateParmPack()) {
966 if (getSema().ArgumentPackSubstitutionIndex == -1)
967 return Name;
968
969 const TemplateArgument &ArgPack = SubstPack->getArgumentPack();
970 assert(getSema().ArgumentPackSubstitutionIndex < (int)ArgPack.pack_size() &&
971 "Pack substitution index out-of-range");
972 return ArgPack.pack_begin()[getSema().ArgumentPackSubstitutionIndex]
973 .getAsTemplate();
974 }
975
976 return inherited::TransformTemplateName(Name, ObjectType,
977 FirstQualifierInScope);
978}
979
John McCalldadc5752010-08-24 06:29:42 +0000980ExprResult
John McCall47f29ea2009-12-08 09:21:05 +0000981TemplateInstantiator::TransformPredefinedExpr(PredefinedExpr *E) {
Anders Carlsson0b209a82009-09-11 01:22:35 +0000982 if (!E->isTypeDependent())
John McCallc3007a22010-10-26 07:05:15 +0000983 return SemaRef.Owned(E);
Anders Carlsson0b209a82009-09-11 01:22:35 +0000984
985 FunctionDecl *currentDecl = getSema().getCurFunctionDecl();
986 assert(currentDecl && "Must have current function declaration when "
987 "instantiating.");
988
989 PredefinedExpr::IdentType IT = E->getIdentType();
990
Anders Carlsson5bd8d192010-02-11 18:20:28 +0000991 unsigned Length = PredefinedExpr::ComputeName(IT, currentDecl).length();
Anders Carlsson0b209a82009-09-11 01:22:35 +0000992
993 llvm::APInt LengthI(32, Length + 1);
John McCall8ccfcb52009-09-24 19:53:00 +0000994 QualType ResTy = getSema().Context.CharTy.withConst();
Anders Carlsson0b209a82009-09-11 01:22:35 +0000995 ResTy = getSema().Context.getConstantArrayType(ResTy, LengthI,
996 ArrayType::Normal, 0);
997 PredefinedExpr *PE =
998 new (getSema().Context) PredefinedExpr(E->getLocation(), ResTy, IT);
999 return getSema().Owned(PE);
1000}
1001
John McCalldadc5752010-08-24 06:29:42 +00001002ExprResult
John McCall13481c52010-02-06 08:42:39 +00001003TemplateInstantiator::TransformTemplateParmRefExpr(DeclRefExpr *E,
Douglas Gregor6c379e22010-02-08 23:41:45 +00001004 NonTypeTemplateParmDecl *NTTP) {
John McCall13481c52010-02-06 08:42:39 +00001005 // If the corresponding template argument is NULL or non-existent, it's
1006 // because we are performing instantiation from explicitly-specified
1007 // template arguments in a function template, but there were some
1008 // arguments left unspecified.
1009 if (!TemplateArgs.hasTemplateArgument(NTTP->getDepth(),
1010 NTTP->getPosition()))
John McCallc3007a22010-10-26 07:05:15 +00001011 return SemaRef.Owned(E);
Mike Stump11289f42009-09-09 15:08:12 +00001012
Douglas Gregoreb5a39d2010-12-24 00:15:10 +00001013 TemplateArgument Arg = TemplateArgs(NTTP->getDepth(), NTTP->getPosition());
1014 if (NTTP->isParameterPack()) {
1015 assert(Arg.getKind() == TemplateArgument::Pack &&
1016 "Missing argument pack");
1017
1018 if (getSema().ArgumentPackSubstitutionIndex == -1) {
Douglas Gregorcdbc5392011-01-15 01:15:58 +00001019 // We have an argument pack, but we can't select a particular argument
1020 // out of it yet. Therefore, we'll build an expression to hold on to that
1021 // argument pack.
1022 QualType TargetType = SemaRef.SubstType(NTTP->getType(), TemplateArgs,
1023 E->getLocation(),
1024 NTTP->getDeclName());
1025 if (TargetType.isNull())
1026 return ExprError();
1027
1028 return new (SemaRef.Context) SubstNonTypeTemplateParmPackExpr(TargetType,
1029 NTTP,
1030 E->getLocation(),
1031 Arg);
Douglas Gregoreb5a39d2010-12-24 00:15:10 +00001032 }
1033
Douglas Gregora8bac7f2011-01-10 07:32:04 +00001034 assert(getSema().ArgumentPackSubstitutionIndex < (int)Arg.pack_size());
Douglas Gregoreb5a39d2010-12-24 00:15:10 +00001035 Arg = Arg.pack_begin()[getSema().ArgumentPackSubstitutionIndex];
1036 }
Mike Stump11289f42009-09-09 15:08:12 +00001037
John McCall13481c52010-02-06 08:42:39 +00001038 // The template argument itself might be an expression, in which
1039 // case we just return that expression.
1040 if (Arg.getKind() == TemplateArgument::Expression)
John McCallc3007a22010-10-26 07:05:15 +00001041 return SemaRef.Owned(Arg.getAsExpr());
Mike Stump11289f42009-09-09 15:08:12 +00001042
John McCall13481c52010-02-06 08:42:39 +00001043 if (Arg.getKind() == TemplateArgument::Declaration) {
1044 ValueDecl *VD = cast<ValueDecl>(Arg.getAsDecl());
Mike Stump11289f42009-09-09 15:08:12 +00001045
John McCall15dda372010-02-06 10:23:53 +00001046 // Find the instantiation of the template argument. This is
1047 // required for nested templates.
John McCall13481c52010-02-06 08:42:39 +00001048 VD = cast_or_null<ValueDecl>(
Douglas Gregora04f2ca2010-03-01 15:56:25 +00001049 getSema().FindInstantiatedDecl(E->getLocation(),
1050 VD, TemplateArgs));
John McCall13481c52010-02-06 08:42:39 +00001051 if (!VD)
John McCallfaf5fb42010-08-26 23:41:50 +00001052 return ExprError();
Mike Stump11289f42009-09-09 15:08:12 +00001053
John McCall15dda372010-02-06 10:23:53 +00001054 // Derive the type we want the substituted decl to have. This had
1055 // better be non-dependent, or these checks will have serious problems.
Douglas Gregor0231d8d2011-01-19 20:10:05 +00001056 QualType TargetType;
1057 if (NTTP->isExpandedParameterPack())
1058 TargetType = NTTP->getExpansionType(
1059 getSema().ArgumentPackSubstitutionIndex);
1060 else if (NTTP->isParameterPack() &&
1061 isa<PackExpansionType>(NTTP->getType())) {
1062 TargetType = SemaRef.SubstType(
1063 cast<PackExpansionType>(NTTP->getType())->getPattern(),
1064 TemplateArgs, E->getLocation(),
1065 NTTP->getDeclName());
1066 } else
1067 TargetType = SemaRef.SubstType(NTTP->getType(), TemplateArgs,
1068 E->getLocation(), NTTP->getDeclName());
John McCall15dda372010-02-06 10:23:53 +00001069 assert(!TargetType.isNull() && "type substitution failed for param type");
1070 assert(!TargetType->isDependentType() && "param type still dependent");
Douglas Gregord5cb1dd2010-03-28 02:42:43 +00001071 return SemaRef.BuildExpressionFromDeclTemplateArgument(Arg,
1072 TargetType,
1073 E->getLocation());
John McCall13481c52010-02-06 08:42:39 +00001074 }
1075
Douglas Gregord5cb1dd2010-03-28 02:42:43 +00001076 return SemaRef.BuildExpressionFromIntegralTemplateArgument(Arg,
1077 E->getSourceRange().getBegin());
John McCall13481c52010-02-06 08:42:39 +00001078}
1079
Douglas Gregorcdbc5392011-01-15 01:15:58 +00001080ExprResult
1081TemplateInstantiator::TransformSubstNonTypeTemplateParmPackExpr(
1082 SubstNonTypeTemplateParmPackExpr *E) {
1083 if (getSema().ArgumentPackSubstitutionIndex == -1) {
1084 // We aren't expanding the parameter pack, so just return ourselves.
1085 return getSema().Owned(E);
1086 }
1087
Douglas Gregorcdbc5392011-01-15 01:15:58 +00001088 const TemplateArgument &ArgPack = E->getArgumentPack();
1089 unsigned Index = (unsigned)getSema().ArgumentPackSubstitutionIndex;
1090 assert(Index < ArgPack.pack_size() && "Substitution index out-of-range");
1091
1092 const TemplateArgument &Arg = ArgPack.pack_begin()[Index];
1093 if (Arg.getKind() == TemplateArgument::Expression)
1094 return SemaRef.Owned(Arg.getAsExpr());
1095
1096 if (Arg.getKind() == TemplateArgument::Declaration) {
1097 ValueDecl *VD = cast<ValueDecl>(Arg.getAsDecl());
1098
1099 // Find the instantiation of the template argument. This is
1100 // required for nested templates.
1101 VD = cast_or_null<ValueDecl>(
1102 getSema().FindInstantiatedDecl(E->getParameterPackLocation(),
1103 VD, TemplateArgs));
1104 if (!VD)
1105 return ExprError();
1106
Douglas Gregor0231d8d2011-01-19 20:10:05 +00001107 QualType T;
1108 NonTypeTemplateParmDecl *NTTP = E->getParameterPack();
1109 if (NTTP->isExpandedParameterPack())
1110 T = NTTP->getExpansionType(getSema().ArgumentPackSubstitutionIndex);
1111 else if (const PackExpansionType *Expansion
1112 = dyn_cast<PackExpansionType>(NTTP->getType()))
1113 T = SemaRef.SubstType(Expansion->getPattern(), TemplateArgs,
1114 E->getParameterPackLocation(), NTTP->getDeclName());
1115 else
1116 T = E->getType();
1117 return SemaRef.BuildExpressionFromDeclTemplateArgument(Arg, T,
Douglas Gregorcdbc5392011-01-15 01:15:58 +00001118 E->getParameterPackLocation());
1119 }
1120
1121 return SemaRef.BuildExpressionFromIntegralTemplateArgument(Arg,
1122 E->getParameterPackLocation());
1123}
John McCall13481c52010-02-06 08:42:39 +00001124
John McCalldadc5752010-08-24 06:29:42 +00001125ExprResult
John McCall13481c52010-02-06 08:42:39 +00001126TemplateInstantiator::TransformDeclRefExpr(DeclRefExpr *E) {
1127 NamedDecl *D = E->getDecl();
1128 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(D)) {
1129 if (NTTP->getDepth() < TemplateArgs.getNumLevels())
1130 return TransformTemplateParmRefExpr(E, NTTP);
Douglas Gregor954de172009-10-31 17:21:17 +00001131
1132 // We have a non-type template parameter that isn't fully substituted;
1133 // FindInstantiatedDecl will find it in the local instantiation scope.
Douglas Gregora16548e2009-08-11 05:31:07 +00001134 }
Mike Stump11289f42009-09-09 15:08:12 +00001135
John McCall47f29ea2009-12-08 09:21:05 +00001136 return TreeTransform<TemplateInstantiator>::TransformDeclRefExpr(E);
Douglas Gregora16548e2009-08-11 05:31:07 +00001137}
1138
John McCalldadc5752010-08-24 06:29:42 +00001139ExprResult TemplateInstantiator::TransformCXXDefaultArgExpr(
John McCall47f29ea2009-12-08 09:21:05 +00001140 CXXDefaultArgExpr *E) {
Sebastian Redl14236c82009-11-08 13:56:19 +00001141 assert(!cast<FunctionDecl>(E->getParam()->getDeclContext())->
1142 getDescribedFunctionTemplate() &&
1143 "Default arg expressions are never formed in dependent cases.");
Douglas Gregor033f6752009-12-23 23:03:06 +00001144 return SemaRef.BuildCXXDefaultArgExpr(E->getUsedLocation(),
1145 cast<FunctionDecl>(E->getParam()->getDeclContext()),
1146 E->getParam());
Sebastian Redl14236c82009-11-08 13:56:19 +00001147}
1148
Douglas Gregor14cf7522010-04-30 18:55:50 +00001149QualType TemplateInstantiator::TransformFunctionProtoType(TypeLocBuilder &TLB,
John McCall31f82722010-11-12 08:19:04 +00001150 FunctionProtoTypeLoc TL) {
Douglas Gregor14cf7522010-04-30 18:55:50 +00001151 // We need a local instantiation scope for this function prototype.
John McCall19c1bfd2010-08-25 05:32:35 +00001152 LocalInstantiationScope Scope(SemaRef, /*CombineWithOuterScope=*/true);
John McCall31f82722010-11-12 08:19:04 +00001153 return inherited::TransformFunctionProtoType(TLB, TL);
John McCall58f10c32010-03-11 09:03:00 +00001154}
1155
1156ParmVarDecl *
Douglas Gregor715e4612011-01-14 22:40:04 +00001157TemplateInstantiator::TransformFunctionTypeParam(ParmVarDecl *OldParm,
1158 llvm::Optional<unsigned> NumExpansions) {
1159 return SemaRef.SubstParmVarDecl(OldParm, TemplateArgs,
1160 NumExpansions);
John McCall58f10c32010-03-11 09:03:00 +00001161}
1162
Mike Stump11289f42009-09-09 15:08:12 +00001163QualType
John McCall550e0c22009-10-21 00:40:46 +00001164TemplateInstantiator::TransformTemplateTypeParmType(TypeLocBuilder &TLB,
John McCall31f82722010-11-12 08:19:04 +00001165 TemplateTypeParmTypeLoc TL) {
John McCall424cec92011-01-19 06:33:43 +00001166 const TemplateTypeParmType *T = TL.getTypePtr();
Douglas Gregor01afeef2009-08-28 20:31:08 +00001167 if (T->getDepth() < TemplateArgs.getNumLevels()) {
Douglas Gregorfe1e1102009-02-27 19:31:52 +00001168 // Replace the template type parameter with its corresponding
1169 // template argument.
Mike Stump11289f42009-09-09 15:08:12 +00001170
1171 // If the corresponding template argument is NULL or doesn't exist, it's
1172 // because we are performing instantiation from explicitly-specified
1173 // template arguments in a function template class, but there were some
Douglas Gregore3f1f352009-07-01 00:28:38 +00001174 // arguments left unspecified.
John McCall550e0c22009-10-21 00:40:46 +00001175 if (!TemplateArgs.hasTemplateArgument(T->getDepth(), T->getIndex())) {
1176 TemplateTypeParmTypeLoc NewTL
1177 = TLB.push<TemplateTypeParmTypeLoc>(TL.getType());
1178 NewTL.setNameLoc(TL.getNameLoc());
1179 return TL.getType();
1180 }
Mike Stump11289f42009-09-09 15:08:12 +00001181
Douglas Gregor840bd6c2010-12-20 22:05:00 +00001182 TemplateArgument Arg = TemplateArgs(T->getDepth(), T->getIndex());
1183
1184 if (T->isParameterPack()) {
1185 assert(Arg.getKind() == TemplateArgument::Pack &&
1186 "Missing argument pack");
1187
1188 if (getSema().ArgumentPackSubstitutionIndex == -1) {
Douglas Gregorada4b792011-01-14 02:55:32 +00001189 // We have the template argument pack, but we're not expanding the
1190 // enclosing pack expansion yet. Just save the template argument
1191 // pack for later substitution.
1192 QualType Result
1193 = getSema().Context.getSubstTemplateTypeParmPackType(T, Arg);
1194 SubstTemplateTypeParmPackTypeLoc NewTL
1195 = TLB.push<SubstTemplateTypeParmPackTypeLoc>(Result);
1196 NewTL.setNameLoc(TL.getNameLoc());
1197 return Result;
Douglas Gregor840bd6c2010-12-20 22:05:00 +00001198 }
1199
Douglas Gregora8bac7f2011-01-10 07:32:04 +00001200 assert(getSema().ArgumentPackSubstitutionIndex < (int)Arg.pack_size());
Douglas Gregor840bd6c2010-12-20 22:05:00 +00001201 Arg = Arg.pack_begin()[getSema().ArgumentPackSubstitutionIndex];
1202 }
1203
1204 assert(Arg.getKind() == TemplateArgument::Type &&
Douglas Gregorfe1e1102009-02-27 19:31:52 +00001205 "Template argument kind mismatch");
Douglas Gregor01afeef2009-08-28 20:31:08 +00001206
Douglas Gregor840bd6c2010-12-20 22:05:00 +00001207 QualType Replacement = Arg.getAsType();
John McCallcebee162009-10-18 09:09:24 +00001208
1209 // TODO: only do this uniquing once, at the start of instantiation.
John McCall550e0c22009-10-21 00:40:46 +00001210 QualType Result
1211 = getSema().Context.getSubstTemplateTypeParmType(T, Replacement);
1212 SubstTemplateTypeParmTypeLoc NewTL
1213 = TLB.push<SubstTemplateTypeParmTypeLoc>(Result);
1214 NewTL.setNameLoc(TL.getNameLoc());
1215 return Result;
Mike Stump11289f42009-09-09 15:08:12 +00001216 }
Douglas Gregorfe1e1102009-02-27 19:31:52 +00001217
1218 // The template type parameter comes from an inner template (e.g.,
1219 // the template parameter list of a member template inside the
1220 // template we are instantiating). Create a new template type
1221 // parameter with the template "level" reduced by one.
John McCall550e0c22009-10-21 00:40:46 +00001222 QualType Result
1223 = getSema().Context.getTemplateTypeParmType(T->getDepth()
1224 - TemplateArgs.getNumLevels(),
1225 T->getIndex(),
1226 T->isParameterPack(),
Douglas Gregor2ebcae12010-06-16 15:23:05 +00001227 T->getName());
John McCall550e0c22009-10-21 00:40:46 +00001228 TemplateTypeParmTypeLoc NewTL = TLB.push<TemplateTypeParmTypeLoc>(Result);
1229 NewTL.setNameLoc(TL.getNameLoc());
1230 return Result;
Douglas Gregor17c0d7b2009-02-28 00:25:32 +00001231}
Douglas Gregorfe1e1102009-02-27 19:31:52 +00001232
Douglas Gregorada4b792011-01-14 02:55:32 +00001233QualType
1234TemplateInstantiator::TransformSubstTemplateTypeParmPackType(
1235 TypeLocBuilder &TLB,
1236 SubstTemplateTypeParmPackTypeLoc TL) {
1237 if (getSema().ArgumentPackSubstitutionIndex == -1) {
1238 // We aren't expanding the parameter pack, so just return ourselves.
1239 SubstTemplateTypeParmPackTypeLoc NewTL
1240 = TLB.push<SubstTemplateTypeParmPackTypeLoc>(TL.getType());
1241 NewTL.setNameLoc(TL.getNameLoc());
1242 return TL.getType();
1243 }
1244
1245 const TemplateArgument &ArgPack = TL.getTypePtr()->getArgumentPack();
1246 unsigned Index = (unsigned)getSema().ArgumentPackSubstitutionIndex;
1247 assert(Index < ArgPack.pack_size() && "Substitution index out-of-range");
1248
1249 QualType Result = ArgPack.pack_begin()[Index].getAsType();
1250 Result = getSema().Context.getSubstTemplateTypeParmType(
1251 TL.getTypePtr()->getReplacedParameter(),
1252 Result);
1253 SubstTemplateTypeParmTypeLoc NewTL
1254 = TLB.push<SubstTemplateTypeParmTypeLoc>(Result);
1255 NewTL.setNameLoc(TL.getNameLoc());
1256 return Result;
1257}
1258
John McCall76d824f2009-08-25 22:02:44 +00001259/// \brief Perform substitution on the type T with a given set of template
1260/// arguments.
Douglas Gregorfe1e1102009-02-27 19:31:52 +00001261///
1262/// This routine substitutes the given template arguments into the
1263/// type T and produces the instantiated type.
1264///
1265/// \param T the type into which the template arguments will be
1266/// substituted. If this type is not dependent, it will be returned
1267/// immediately.
1268///
1269/// \param TemplateArgs the template arguments that will be
1270/// substituted for the top-level template parameters within T.
1271///
Douglas Gregorfe1e1102009-02-27 19:31:52 +00001272/// \param Loc the location in the source code where this substitution
1273/// is being performed. It will typically be the location of the
1274/// declarator (if we're instantiating the type of some declaration)
1275/// or the location of the type in the source code (if, e.g., we're
1276/// instantiating the type of a cast expression).
1277///
1278/// \param Entity the name of the entity associated with a declaration
1279/// being instantiated (if any). May be empty to indicate that there
1280/// is no such entity (if, e.g., this is a type that occurs as part of
1281/// a cast expression) or that the entity has no name (e.g., an
1282/// unnamed function parameter).
1283///
1284/// \returns If the instantiation succeeds, the instantiated
1285/// type. Otherwise, produces diagnostics and returns a NULL type.
John McCallbcd03502009-12-07 02:54:59 +00001286TypeSourceInfo *Sema::SubstType(TypeSourceInfo *T,
John McCall609459e2009-10-21 00:58:09 +00001287 const MultiLevelTemplateArgumentList &Args,
1288 SourceLocation Loc,
1289 DeclarationName Entity) {
1290 assert(!ActiveTemplateInstantiations.empty() &&
1291 "Cannot perform an instantiation without some context on the "
1292 "instantiation stack");
1293
Douglas Gregor5a5073e2010-05-24 17:22:01 +00001294 if (!T->getType()->isDependentType() &&
1295 !T->getType()->isVariablyModifiedType())
John McCall609459e2009-10-21 00:58:09 +00001296 return T;
1297
1298 TemplateInstantiator Instantiator(*this, Args, Loc, Entity);
1299 return Instantiator.TransformType(T);
1300}
1301
Douglas Gregor5499af42011-01-05 23:12:31 +00001302TypeSourceInfo *Sema::SubstType(TypeLoc TL,
1303 const MultiLevelTemplateArgumentList &Args,
1304 SourceLocation Loc,
1305 DeclarationName Entity) {
1306 assert(!ActiveTemplateInstantiations.empty() &&
1307 "Cannot perform an instantiation without some context on the "
1308 "instantiation stack");
1309
1310 if (TL.getType().isNull())
1311 return 0;
1312
1313 if (!TL.getType()->isDependentType() &&
1314 !TL.getType()->isVariablyModifiedType()) {
1315 // FIXME: Make a copy of the TypeLoc data here, so that we can
1316 // return a new TypeSourceInfo. Inefficient!
1317 TypeLocBuilder TLB;
1318 TLB.pushFullCopy(TL);
1319 return TLB.getTypeSourceInfo(Context, TL.getType());
1320 }
1321
1322 TemplateInstantiator Instantiator(*this, Args, Loc, Entity);
1323 TypeLocBuilder TLB;
1324 TLB.reserve(TL.getFullDataSize());
1325 QualType Result = Instantiator.TransformType(TLB, TL);
1326 if (Result.isNull())
1327 return 0;
1328
1329 return TLB.getTypeSourceInfo(Context, Result);
1330}
1331
John McCall609459e2009-10-21 00:58:09 +00001332/// Deprecated form of the above.
Mike Stump11289f42009-09-09 15:08:12 +00001333QualType Sema::SubstType(QualType T,
Douglas Gregor01afeef2009-08-28 20:31:08 +00001334 const MultiLevelTemplateArgumentList &TemplateArgs,
John McCall76d824f2009-08-25 22:02:44 +00001335 SourceLocation Loc, DeclarationName Entity) {
Douglas Gregor79cf6032009-03-10 20:44:00 +00001336 assert(!ActiveTemplateInstantiations.empty() &&
1337 "Cannot perform an instantiation without some context on the "
1338 "instantiation stack");
1339
Douglas Gregor5a5073e2010-05-24 17:22:01 +00001340 // If T is not a dependent type or a variably-modified type, there
1341 // is nothing to do.
1342 if (!T->isDependentType() && !T->isVariablyModifiedType())
Douglas Gregorfe1e1102009-02-27 19:31:52 +00001343 return T;
1344
Douglas Gregord6ff3322009-08-04 16:50:30 +00001345 TemplateInstantiator Instantiator(*this, TemplateArgs, Loc, Entity);
1346 return Instantiator.TransformType(T);
Douglas Gregorfe1e1102009-02-27 19:31:52 +00001347}
Douglas Gregor463421d2009-03-03 04:44:36 +00001348
John McCallb29f78f2010-04-09 17:38:44 +00001349static bool NeedsInstantiationAsFunctionType(TypeSourceInfo *T) {
Douglas Gregor5a5073e2010-05-24 17:22:01 +00001350 if (T->getType()->isDependentType() || T->getType()->isVariablyModifiedType())
John McCallb29f78f2010-04-09 17:38:44 +00001351 return true;
1352
Abramo Bagnara6d810632010-12-14 22:11:44 +00001353 TypeLoc TL = T->getTypeLoc().IgnoreParens();
John McCallb29f78f2010-04-09 17:38:44 +00001354 if (!isa<FunctionProtoTypeLoc>(TL))
1355 return false;
1356
1357 FunctionProtoTypeLoc FP = cast<FunctionProtoTypeLoc>(TL);
1358 for (unsigned I = 0, E = FP.getNumArgs(); I != E; ++I) {
1359 ParmVarDecl *P = FP.getArg(I);
1360
1361 // TODO: currently we always rebuild expressions. When we
1362 // properly get lazier about this, we should use the same
1363 // logic to avoid rebuilding prototypes here.
Douglas Gregor9cc278222011-01-05 21:14:17 +00001364 if (P->hasDefaultArg())
John McCallb29f78f2010-04-09 17:38:44 +00001365 return true;
1366 }
1367
1368 return false;
1369}
1370
1371/// A form of SubstType intended specifically for instantiating the
1372/// type of a FunctionDecl. Its purpose is solely to force the
1373/// instantiation of default-argument expressions.
1374TypeSourceInfo *Sema::SubstFunctionDeclType(TypeSourceInfo *T,
1375 const MultiLevelTemplateArgumentList &Args,
1376 SourceLocation Loc,
1377 DeclarationName Entity) {
1378 assert(!ActiveTemplateInstantiations.empty() &&
1379 "Cannot perform an instantiation without some context on the "
1380 "instantiation stack");
1381
1382 if (!NeedsInstantiationAsFunctionType(T))
1383 return T;
1384
1385 TemplateInstantiator Instantiator(*this, Args, Loc, Entity);
1386
1387 TypeLocBuilder TLB;
1388
1389 TypeLoc TL = T->getTypeLoc();
1390 TLB.reserve(TL.getFullDataSize());
1391
John McCall31f82722010-11-12 08:19:04 +00001392 QualType Result = Instantiator.TransformType(TLB, TL);
John McCallb29f78f2010-04-09 17:38:44 +00001393 if (Result.isNull())
1394 return 0;
1395
1396 return TLB.getTypeSourceInfo(Context, Result);
1397}
1398
Douglas Gregor940bca72010-04-12 07:48:19 +00001399ParmVarDecl *Sema::SubstParmVarDecl(ParmVarDecl *OldParm,
Douglas Gregor715e4612011-01-14 22:40:04 +00001400 const MultiLevelTemplateArgumentList &TemplateArgs,
1401 llvm::Optional<unsigned> NumExpansions) {
Douglas Gregor940bca72010-04-12 07:48:19 +00001402 TypeSourceInfo *OldDI = OldParm->getTypeSourceInfo();
Douglas Gregor5499af42011-01-05 23:12:31 +00001403 TypeSourceInfo *NewDI = 0;
1404
Douglas Gregor5499af42011-01-05 23:12:31 +00001405 TypeLoc OldTL = OldDI->getTypeLoc();
1406 if (isa<PackExpansionTypeLoc>(OldTL)) {
1407 PackExpansionTypeLoc ExpansionTL = cast<PackExpansionTypeLoc>(OldTL);
Douglas Gregor5499af42011-01-05 23:12:31 +00001408
1409 // We have a function parameter pack. Substitute into the pattern of the
1410 // expansion.
1411 NewDI = SubstType(ExpansionTL.getPatternLoc(), TemplateArgs,
1412 OldParm->getLocation(), OldParm->getDeclName());
1413 if (!NewDI)
1414 return 0;
1415
1416 if (NewDI->getType()->containsUnexpandedParameterPack()) {
1417 // We still have unexpanded parameter packs, which means that
1418 // our function parameter is still a function parameter pack.
1419 // Therefore, make its type a pack expansion type.
Douglas Gregor0dca5fd2011-01-14 17:04:44 +00001420 NewDI = CheckPackExpansion(NewDI, ExpansionTL.getEllipsisLoc(),
Douglas Gregor715e4612011-01-14 22:40:04 +00001421 NumExpansions);
Douglas Gregor5499af42011-01-05 23:12:31 +00001422 }
1423 } else {
1424 NewDI = SubstType(OldDI, TemplateArgs, OldParm->getLocation(),
1425 OldParm->getDeclName());
1426 }
1427
Douglas Gregor940bca72010-04-12 07:48:19 +00001428 if (!NewDI)
1429 return 0;
1430
1431 if (NewDI->getType()->isVoidType()) {
1432 Diag(OldParm->getLocation(), diag::err_param_with_void_type);
1433 return 0;
1434 }
1435
1436 ParmVarDecl *NewParm = CheckParameter(Context.getTranslationUnitDecl(),
1437 NewDI, NewDI->getType(),
1438 OldParm->getIdentifier(),
1439 OldParm->getLocation(),
Douglas Gregorc4df4072010-04-19 22:54:31 +00001440 OldParm->getStorageClass(),
1441 OldParm->getStorageClassAsWritten());
Douglas Gregor940bca72010-04-12 07:48:19 +00001442 if (!NewParm)
1443 return 0;
Douglas Gregor6044d692010-05-19 17:02:24 +00001444
Douglas Gregor940bca72010-04-12 07:48:19 +00001445 // Mark the (new) default argument as uninstantiated (if any).
1446 if (OldParm->hasUninstantiatedDefaultArg()) {
1447 Expr *Arg = OldParm->getUninstantiatedDefaultArg();
1448 NewParm->setUninstantiatedDefaultArg(Arg);
Douglas Gregor758cb672010-10-12 18:23:32 +00001449 } else if (OldParm->hasUnparsedDefaultArg()) {
1450 NewParm->setUnparsedDefaultArg();
1451 UnparsedDefaultArgInstantiations[OldParm].push_back(NewParm);
Douglas Gregor940bca72010-04-12 07:48:19 +00001452 } else if (Expr *Arg = OldParm->getDefaultArg())
1453 NewParm->setUninstantiatedDefaultArg(Arg);
1454
1455 NewParm->setHasInheritedDefaultArg(OldParm->hasInheritedDefaultArg());
1456
Douglas Gregor5499af42011-01-05 23:12:31 +00001457 // FIXME: When OldParm is a parameter pack and NewParm is not a parameter
1458 // pack, we actually have a set of instantiated locations. Maintain this set!
Douglas Gregorf3010112011-01-07 16:43:16 +00001459 if (OldParm->isParameterPack() && !NewParm->isParameterPack()) {
1460 // Add the new parameter to
1461 CurrentInstantiationScope->InstantiatedLocalPackArg(OldParm, NewParm);
1462 } else {
1463 // Introduce an Old -> New mapping
Douglas Gregor5499af42011-01-05 23:12:31 +00001464 CurrentInstantiationScope->InstantiatedLocal(OldParm, NewParm);
Douglas Gregorf3010112011-01-07 16:43:16 +00001465 }
Douglas Gregor5499af42011-01-05 23:12:31 +00001466
Argyrios Kyrtzidis3816ed42010-07-19 10:14:41 +00001467 // FIXME: OldParm may come from a FunctionProtoType, in which case CurContext
1468 // can be anything, is this right ?
Fariborz Jahanian714447b2010-07-13 21:05:02 +00001469 NewParm->setDeclContext(CurContext);
Fariborz Jahaniana6c7efe2010-07-13 20:05:58 +00001470
Douglas Gregor940bca72010-04-12 07:48:19 +00001471 return NewParm;
1472}
1473
Douglas Gregordd472162011-01-07 00:20:55 +00001474/// \brief Substitute the given template arguments into the given set of
1475/// parameters, producing the set of parameter types that would be generated
1476/// from such a substitution.
1477bool Sema::SubstParmTypes(SourceLocation Loc,
1478 ParmVarDecl **Params, unsigned NumParams,
1479 const MultiLevelTemplateArgumentList &TemplateArgs,
Douglas Gregorf3010112011-01-07 16:43:16 +00001480 llvm::SmallVectorImpl<QualType> &ParamTypes,
1481 llvm::SmallVectorImpl<ParmVarDecl *> *OutParams) {
Douglas Gregordd472162011-01-07 00:20:55 +00001482 assert(!ActiveTemplateInstantiations.empty() &&
1483 "Cannot perform an instantiation without some context on the "
1484 "instantiation stack");
1485
1486 TemplateInstantiator Instantiator(*this, TemplateArgs, Loc,
1487 DeclarationName());
1488 return Instantiator.TransformFunctionTypeParams(Loc, Params, NumParams, 0,
Douglas Gregorf3010112011-01-07 16:43:16 +00001489 ParamTypes, OutParams);
Douglas Gregordd472162011-01-07 00:20:55 +00001490}
1491
John McCall76d824f2009-08-25 22:02:44 +00001492/// \brief Perform substitution on the base class specifiers of the
1493/// given class template specialization.
Douglas Gregor463421d2009-03-03 04:44:36 +00001494///
1495/// Produces a diagnostic and returns true on error, returns false and
1496/// attaches the instantiated base classes to the class template
1497/// specialization if successful.
Mike Stump11289f42009-09-09 15:08:12 +00001498bool
John McCall76d824f2009-08-25 22:02:44 +00001499Sema::SubstBaseSpecifiers(CXXRecordDecl *Instantiation,
1500 CXXRecordDecl *Pattern,
Douglas Gregor01afeef2009-08-28 20:31:08 +00001501 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor463421d2009-03-03 04:44:36 +00001502 bool Invalid = false;
Douglas Gregor6181ded2009-05-29 18:27:38 +00001503 llvm::SmallVector<CXXBaseSpecifier*, 4> InstantiatedBases;
Mike Stump11289f42009-09-09 15:08:12 +00001504 for (ClassTemplateSpecializationDecl::base_class_iterator
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001505 Base = Pattern->bases_begin(), BaseEnd = Pattern->bases_end();
Douglas Gregor2a72edd2009-03-10 18:52:44 +00001506 Base != BaseEnd; ++Base) {
Douglas Gregor463421d2009-03-03 04:44:36 +00001507 if (!Base->getType()->isDependentType()) {
Fariborz Jahanian5c14ec32009-07-22 17:41:53 +00001508 InstantiatedBases.push_back(new (Context) CXXBaseSpecifier(*Base));
Douglas Gregor463421d2009-03-03 04:44:36 +00001509 continue;
1510 }
1511
Douglas Gregor752a5952011-01-03 22:36:02 +00001512 SourceLocation EllipsisLoc;
1513 if (Base->isPackExpansion()) {
1514 // This is a pack expansion. See whether we should expand it now, or
1515 // wait until later.
1516 llvm::SmallVector<UnexpandedParameterPack, 2> Unexpanded;
1517 collectUnexpandedParameterPacks(Base->getTypeSourceInfo()->getTypeLoc(),
1518 Unexpanded);
1519 bool ShouldExpand = false;
Douglas Gregora8bac7f2011-01-10 07:32:04 +00001520 bool RetainExpansion = false;
Douglas Gregor0dca5fd2011-01-14 17:04:44 +00001521 llvm::Optional<unsigned> NumExpansions;
Douglas Gregor752a5952011-01-03 22:36:02 +00001522 if (CheckParameterPacksForExpansion(Base->getEllipsisLoc(),
1523 Base->getSourceRange(),
1524 Unexpanded.data(), Unexpanded.size(),
1525 TemplateArgs, ShouldExpand,
Douglas Gregora8bac7f2011-01-10 07:32:04 +00001526 RetainExpansion,
Douglas Gregor752a5952011-01-03 22:36:02 +00001527 NumExpansions)) {
Douglas Gregor752a5952011-01-03 22:36:02 +00001528 Invalid = true;
Douglas Gregor44e7df62011-01-04 00:32:56 +00001529 continue;
Douglas Gregor752a5952011-01-03 22:36:02 +00001530 }
1531
1532 // If we should expand this pack expansion now, do so.
1533 if (ShouldExpand) {
Douglas Gregor0dca5fd2011-01-14 17:04:44 +00001534 for (unsigned I = 0; I != *NumExpansions; ++I) {
Douglas Gregor752a5952011-01-03 22:36:02 +00001535 Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(*this, I);
1536
1537 TypeSourceInfo *BaseTypeLoc = SubstType(Base->getTypeSourceInfo(),
1538 TemplateArgs,
1539 Base->getSourceRange().getBegin(),
1540 DeclarationName());
1541 if (!BaseTypeLoc) {
1542 Invalid = true;
1543 continue;
1544 }
1545
1546 if (CXXBaseSpecifier *InstantiatedBase
1547 = CheckBaseSpecifier(Instantiation,
1548 Base->getSourceRange(),
1549 Base->isVirtual(),
1550 Base->getAccessSpecifierAsWritten(),
1551 BaseTypeLoc,
1552 SourceLocation()))
1553 InstantiatedBases.push_back(InstantiatedBase);
1554 else
1555 Invalid = true;
1556 }
1557
1558 continue;
1559 }
1560
1561 // The resulting base specifier will (still) be a pack expansion.
1562 EllipsisLoc = Base->getEllipsisLoc();
1563 }
1564
1565 Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(*this, -1);
Nick Lewycky19b9f952010-07-26 16:56:01 +00001566 TypeSourceInfo *BaseTypeLoc = SubstType(Base->getTypeSourceInfo(),
1567 TemplateArgs,
1568 Base->getSourceRange().getBegin(),
1569 DeclarationName());
1570 if (!BaseTypeLoc) {
Douglas Gregor463421d2009-03-03 04:44:36 +00001571 Invalid = true;
1572 continue;
1573 }
1574
1575 if (CXXBaseSpecifier *InstantiatedBase
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001576 = CheckBaseSpecifier(Instantiation,
Douglas Gregor463421d2009-03-03 04:44:36 +00001577 Base->getSourceRange(),
1578 Base->isVirtual(),
1579 Base->getAccessSpecifierAsWritten(),
Douglas Gregor752a5952011-01-03 22:36:02 +00001580 BaseTypeLoc,
1581 EllipsisLoc))
Douglas Gregor463421d2009-03-03 04:44:36 +00001582 InstantiatedBases.push_back(InstantiatedBase);
1583 else
1584 Invalid = true;
1585 }
1586
Douglas Gregor2a72edd2009-03-10 18:52:44 +00001587 if (!Invalid &&
Jay Foad7d0479f2009-05-21 09:52:38 +00001588 AttachBaseSpecifiers(Instantiation, InstantiatedBases.data(),
Douglas Gregor463421d2009-03-03 04:44:36 +00001589 InstantiatedBases.size()))
1590 Invalid = true;
1591
1592 return Invalid;
1593}
1594
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001595/// \brief Instantiate the definition of a class from a given pattern.
1596///
1597/// \param PointOfInstantiation The point of instantiation within the
1598/// source code.
1599///
1600/// \param Instantiation is the declaration whose definition is being
1601/// instantiated. This will be either a class template specialization
1602/// or a member class of a class template specialization.
1603///
1604/// \param Pattern is the pattern from which the instantiation
1605/// occurs. This will be either the declaration of a class template or
1606/// the declaration of a member class of a class template.
1607///
1608/// \param TemplateArgs The template arguments to be substituted into
1609/// the pattern.
1610///
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001611/// \param TSK the kind of implicit or explicit instantiation to perform.
Douglas Gregor8a2e6012009-08-24 15:23:48 +00001612///
1613/// \param Complain whether to complain if the class cannot be instantiated due
1614/// to the lack of a definition.
1615///
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001616/// \returns true if an error occurred, false otherwise.
1617bool
1618Sema::InstantiateClass(SourceLocation PointOfInstantiation,
1619 CXXRecordDecl *Instantiation, CXXRecordDecl *Pattern,
Douglas Gregor01afeef2009-08-28 20:31:08 +00001620 const MultiLevelTemplateArgumentList &TemplateArgs,
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001621 TemplateSpecializationKind TSK,
Douglas Gregor8a2e6012009-08-24 15:23:48 +00001622 bool Complain) {
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001623 bool Invalid = false;
John McCall87a44eb2009-08-20 01:44:21 +00001624
Mike Stump11289f42009-09-09 15:08:12 +00001625 CXXRecordDecl *PatternDef
Douglas Gregor0a5a2212010-02-11 01:04:33 +00001626 = cast_or_null<CXXRecordDecl>(Pattern->getDefinition());
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001627 if (!PatternDef) {
Douglas Gregor8a2e6012009-08-24 15:23:48 +00001628 if (!Complain) {
1629 // Say nothing
1630 } else if (Pattern == Instantiation->getInstantiatedFromMemberClass()) {
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001631 Diag(PointOfInstantiation,
1632 diag::err_implicit_instantiate_member_undefined)
1633 << Context.getTypeDeclType(Instantiation);
1634 Diag(Pattern->getLocation(), diag::note_member_of_template_here);
1635 } else {
Douglas Gregora1f49972009-05-13 00:25:59 +00001636 Diag(PointOfInstantiation, diag::err_template_instantiate_undefined)
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001637 << (TSK != TSK_ImplicitInstantiation)
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001638 << Context.getTypeDeclType(Instantiation);
1639 Diag(Pattern->getLocation(), diag::note_template_decl_here);
1640 }
1641 return true;
1642 }
1643 Pattern = PatternDef;
1644
Douglas Gregord6ba93d2009-10-15 15:54:05 +00001645 // \brief Record the point of instantiation.
1646 if (MemberSpecializationInfo *MSInfo
1647 = Instantiation->getMemberSpecializationInfo()) {
1648 MSInfo->setTemplateSpecializationKind(TSK);
1649 MSInfo->setPointOfInstantiation(PointOfInstantiation);
Douglas Gregoref6ab412009-10-27 06:26:26 +00001650 } else if (ClassTemplateSpecializationDecl *Spec
1651 = dyn_cast<ClassTemplateSpecializationDecl>(Instantiation)) {
1652 Spec->setTemplateSpecializationKind(TSK);
1653 Spec->setPointOfInstantiation(PointOfInstantiation);
Douglas Gregord6ba93d2009-10-15 15:54:05 +00001654 }
1655
Douglas Gregorf3430ae2009-03-25 21:23:52 +00001656 InstantiatingTemplate Inst(*this, PointOfInstantiation, Instantiation);
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001657 if (Inst)
1658 return true;
1659
1660 // Enter the scope of this instantiation. We don't use
1661 // PushDeclContext because we don't have a scope.
John McCall80e58cd2010-04-29 00:35:03 +00001662 ContextRAII SavedContext(*this, Instantiation);
Douglas Gregor17158422010-05-12 17:27:19 +00001663 EnterExpressionEvaluationContext EvalContext(*this,
John McCallfaf5fb42010-08-26 23:41:50 +00001664 Sema::PotentiallyEvaluated);
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001665
Douglas Gregor51121572010-03-24 01:33:17 +00001666 // If this is an instantiation of a local class, merge this local
1667 // instantiation scope with the enclosing scope. Otherwise, every
1668 // instantiation of a class has its own local instantiation scope.
1669 bool MergeWithParentScope = !Instantiation->isDefinedOutsideFunctionOrMethod();
John McCall19c1bfd2010-08-25 05:32:35 +00001670 LocalInstantiationScope Scope(*this, MergeWithParentScope);
Douglas Gregor51121572010-03-24 01:33:17 +00001671
John McCall6602bb12010-08-01 02:01:53 +00001672 // Pull attributes from the pattern onto the instantiation.
1673 InstantiateAttrs(TemplateArgs, Pattern, Instantiation);
1674
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001675 // Start the definition of this instantiation.
1676 Instantiation->startDefinition();
Douglas Gregore9029562010-05-06 00:28:52 +00001677
1678 Instantiation->setTagKind(Pattern->getTagKind());
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001679
John McCall76d824f2009-08-25 22:02:44 +00001680 // Do substitution on the base class specifiers.
1681 if (SubstBaseSpecifiers(Instantiation, Pattern, TemplateArgs))
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001682 Invalid = true;
1683
Douglas Gregor869853e2010-11-10 19:44:59 +00001684 TemplateDeclInstantiator Instantiator(*this, Instantiation, TemplateArgs);
John McCall48871652010-08-21 09:40:31 +00001685 llvm::SmallVector<Decl*, 4> Fields;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001686 for (RecordDecl::decl_iterator Member = Pattern->decls_begin(),
Mike Stump11289f42009-09-09 15:08:12 +00001687 MemberEnd = Pattern->decls_end();
Douglas Gregorbcced4e2009-04-09 21:40:53 +00001688 Member != MemberEnd; ++Member) {
Argyrios Kyrtzidis9a94d9b2010-11-04 03:18:57 +00001689 // Don't instantiate members not belonging in this semantic context.
1690 // e.g. for:
1691 // @code
1692 // template <int i> class A {
1693 // class B *g;
1694 // };
1695 // @endcode
1696 // 'class B' has the template as lexical context but semantically it is
1697 // introduced in namespace scope.
1698 if ((*Member)->getDeclContext() != Pattern)
1699 continue;
1700
Douglas Gregor869853e2010-11-10 19:44:59 +00001701 if ((*Member)->isInvalidDecl()) {
1702 Invalid = true;
1703 continue;
1704 }
1705
1706 Decl *NewMember = Instantiator.Visit(*Member);
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001707 if (NewMember) {
Eli Friedmand0e8de22009-12-07 00:22:08 +00001708 if (FieldDecl *Field = dyn_cast<FieldDecl>(NewMember))
John McCall48871652010-08-21 09:40:31 +00001709 Fields.push_back(Field);
Eli Friedmand0e8de22009-12-07 00:22:08 +00001710 else if (NewMember->isInvalidDecl())
1711 Invalid = true;
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001712 } else {
1713 // FIXME: Eventually, a NULL return will mean that one of the
Mike Stump87c57ac2009-05-16 07:39:55 +00001714 // instantiations was a semantic disaster, and we'll want to set Invalid =
1715 // true. For now, we expect to skip some members that we can't yet handle.
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001716 }
1717 }
1718
1719 // Finish checking fields.
John McCall48871652010-08-21 09:40:31 +00001720 ActOnFields(0, Instantiation->getLocation(), Instantiation,
Jay Foad7d0479f2009-05-21 09:52:38 +00001721 Fields.data(), Fields.size(), SourceLocation(), SourceLocation(),
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001722 0);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001723 CheckCompletedCXXClass(Instantiation);
Douglas Gregor3c74d412009-10-14 20:14:33 +00001724 if (Instantiation->isInvalidDecl())
1725 Invalid = true;
Douglas Gregor869853e2010-11-10 19:44:59 +00001726 else {
1727 // Instantiate any out-of-line class template partial
1728 // specializations now.
1729 for (TemplateDeclInstantiator::delayed_partial_spec_iterator
1730 P = Instantiator.delayed_partial_spec_begin(),
1731 PEnd = Instantiator.delayed_partial_spec_end();
1732 P != PEnd; ++P) {
1733 if (!Instantiator.InstantiateClassTemplatePartialSpecialization(
1734 P->first,
1735 P->second)) {
1736 Invalid = true;
1737 break;
1738 }
1739 }
1740 }
1741
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001742 // Exit the scope of this instantiation.
John McCall80e58cd2010-04-29 00:35:03 +00001743 SavedContext.pop();
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001744
Douglas Gregor88d292c2010-05-13 16:44:06 +00001745 if (!Invalid) {
Douglas Gregor28ad4b52009-05-26 20:50:29 +00001746 Consumer.HandleTagDeclDefinition(Instantiation);
1747
Douglas Gregor88d292c2010-05-13 16:44:06 +00001748 // Always emit the vtable for an explicit instantiation definition
1749 // of a polymorphic class template specialization.
1750 if (TSK == TSK_ExplicitInstantiationDefinition)
1751 MarkVTableUsed(PointOfInstantiation, Instantiation, true);
1752 }
1753
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001754 return Invalid;
1755}
1756
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +00001757namespace {
1758 /// \brief A partial specialization whose template arguments have matched
1759 /// a given template-id.
1760 struct PartialSpecMatchResult {
1761 ClassTemplatePartialSpecializationDecl *Partial;
1762 TemplateArgumentList *Args;
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +00001763 };
1764}
1765
Mike Stump11289f42009-09-09 15:08:12 +00001766bool
Douglas Gregor463421d2009-03-03 04:44:36 +00001767Sema::InstantiateClassTemplateSpecialization(
Douglas Gregoref6ab412009-10-27 06:26:26 +00001768 SourceLocation PointOfInstantiation,
Douglas Gregor463421d2009-03-03 04:44:36 +00001769 ClassTemplateSpecializationDecl *ClassTemplateSpec,
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001770 TemplateSpecializationKind TSK,
Douglas Gregor8a2e6012009-08-24 15:23:48 +00001771 bool Complain) {
Douglas Gregor463421d2009-03-03 04:44:36 +00001772 // Perform the actual instantiation on the canonical declaration.
1773 ClassTemplateSpec = cast<ClassTemplateSpecializationDecl>(
Argyrios Kyrtzidis6b7e3762009-07-18 00:34:25 +00001774 ClassTemplateSpec->getCanonicalDecl());
Douglas Gregor463421d2009-03-03 04:44:36 +00001775
Douglas Gregor4aa04b12009-09-11 21:19:12 +00001776 // Check whether we have already instantiated or specialized this class
1777 // template specialization.
1778 if (ClassTemplateSpec->getSpecializationKind() != TSK_Undeclared) {
1779 if (ClassTemplateSpec->getSpecializationKind() ==
1780 TSK_ExplicitInstantiationDeclaration &&
1781 TSK == TSK_ExplicitInstantiationDefinition) {
1782 // An explicit instantiation definition follows an explicit instantiation
1783 // declaration (C++0x [temp.explicit]p10); go ahead and perform the
1784 // explicit instantiation.
1785 ClassTemplateSpec->setSpecializationKind(TSK);
Douglas Gregor88d292c2010-05-13 16:44:06 +00001786
1787 // If this is an explicit instantiation definition, mark the
1788 // vtable as used.
1789 if (TSK == TSK_ExplicitInstantiationDefinition)
1790 MarkVTableUsed(PointOfInstantiation, ClassTemplateSpec, true);
1791
Douglas Gregor4aa04b12009-09-11 21:19:12 +00001792 return false;
1793 }
1794
1795 // We can only instantiate something that hasn't already been
1796 // instantiated or specialized. Fail without any diagnostics: our
1797 // caller will provide an error message.
Douglas Gregor463421d2009-03-03 04:44:36 +00001798 return true;
Douglas Gregor4aa04b12009-09-11 21:19:12 +00001799 }
Douglas Gregor463421d2009-03-03 04:44:36 +00001800
Douglas Gregor00a511f2009-09-15 16:51:42 +00001801 if (ClassTemplateSpec->isInvalidDecl())
1802 return true;
1803
Douglas Gregor463421d2009-03-03 04:44:36 +00001804 ClassTemplateDecl *Template = ClassTemplateSpec->getSpecializedTemplate();
Douglas Gregor01afeef2009-08-28 20:31:08 +00001805 CXXRecordDecl *Pattern = 0;
Douglas Gregor2373c592009-05-31 09:31:02 +00001806
Douglas Gregor170bc422009-06-12 22:31:52 +00001807 // C++ [temp.class.spec.match]p1:
1808 // When a class template is used in a context that requires an
1809 // instantiation of the class, it is necessary to determine
1810 // whether the instantiation is to be generated using the primary
1811 // template or one of the partial specializations. This is done by
1812 // matching the template arguments of the class template
1813 // specialization with the template argument lists of the partial
1814 // specializations.
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +00001815 typedef PartialSpecMatchResult MatchResult;
Douglas Gregorb7ae10f2009-06-05 00:53:49 +00001816 llvm::SmallVector<MatchResult, 4> Matched;
Douglas Gregor407e9612010-04-30 05:56:50 +00001817 llvm::SmallVector<ClassTemplatePartialSpecializationDecl *, 4> PartialSpecs;
1818 Template->getPartialSpecializations(PartialSpecs);
1819 for (unsigned I = 0, N = PartialSpecs.size(); I != N; ++I) {
1820 ClassTemplatePartialSpecializationDecl *Partial = PartialSpecs[I];
John McCallbc077cf2010-02-08 23:07:23 +00001821 TemplateDeductionInfo Info(Context, PointOfInstantiation);
Douglas Gregor181aa4a2009-06-12 18:26:56 +00001822 if (TemplateDeductionResult Result
Douglas Gregor407e9612010-04-30 05:56:50 +00001823 = DeduceTemplateArguments(Partial,
Douglas Gregor181aa4a2009-06-12 18:26:56 +00001824 ClassTemplateSpec->getTemplateArgs(),
1825 Info)) {
1826 // FIXME: Store the failed-deduction information for use in
1827 // diagnostics, later.
1828 (void)Result;
1829 } else {
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +00001830 Matched.push_back(PartialSpecMatchResult());
1831 Matched.back().Partial = Partial;
1832 Matched.back().Args = Info.take();
Douglas Gregor181aa4a2009-06-12 18:26:56 +00001833 }
Douglas Gregor2373c592009-05-31 09:31:02 +00001834 }
1835
Douglas Gregor0231d8d2011-01-19 20:10:05 +00001836 // If we're dealing with a member template where the template parameters
1837 // have been instantiated, this provides the original template parameters
1838 // from which the member template's parameters were instantiated.
1839 llvm::SmallVector<const NamedDecl *, 4> InstantiatedTemplateParameters;
1840
Douglas Gregor21610382009-10-29 00:04:11 +00001841 if (Matched.size() >= 1) {
Douglas Gregorbe999392009-09-15 16:23:51 +00001842 llvm::SmallVector<MatchResult, 4>::iterator Best = Matched.begin();
Douglas Gregor21610382009-10-29 00:04:11 +00001843 if (Matched.size() == 1) {
1844 // -- If exactly one matching specialization is found, the
1845 // instantiation is generated from that specialization.
1846 // We don't need to do anything for this.
1847 } else {
1848 // -- If more than one matching specialization is found, the
1849 // partial order rules (14.5.4.2) are used to determine
1850 // whether one of the specializations is more specialized
1851 // than the others. If none of the specializations is more
1852 // specialized than all of the other matching
1853 // specializations, then the use of the class template is
1854 // ambiguous and the program is ill-formed.
1855 for (llvm::SmallVector<MatchResult, 4>::iterator P = Best + 1,
1856 PEnd = Matched.end();
1857 P != PEnd; ++P) {
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +00001858 if (getMoreSpecializedPartialSpecialization(P->Partial, Best->Partial,
John McCallbc077cf2010-02-08 23:07:23 +00001859 PointOfInstantiation)
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +00001860 == P->Partial)
Douglas Gregor21610382009-10-29 00:04:11 +00001861 Best = P;
Douglas Gregorbe999392009-09-15 16:23:51 +00001862 }
Douglas Gregorbe999392009-09-15 16:23:51 +00001863
Douglas Gregor21610382009-10-29 00:04:11 +00001864 // Determine if the best partial specialization is more specialized than
1865 // the others.
1866 bool Ambiguous = false;
Douglas Gregorbe999392009-09-15 16:23:51 +00001867 for (llvm::SmallVector<MatchResult, 4>::iterator P = Matched.begin(),
1868 PEnd = Matched.end();
Douglas Gregor21610382009-10-29 00:04:11 +00001869 P != PEnd; ++P) {
1870 if (P != Best &&
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +00001871 getMoreSpecializedPartialSpecialization(P->Partial, Best->Partial,
John McCallbc077cf2010-02-08 23:07:23 +00001872 PointOfInstantiation)
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +00001873 != Best->Partial) {
Douglas Gregor21610382009-10-29 00:04:11 +00001874 Ambiguous = true;
1875 break;
1876 }
1877 }
1878
1879 if (Ambiguous) {
1880 // Partial ordering did not produce a clear winner. Complain.
1881 ClassTemplateSpec->setInvalidDecl();
1882 Diag(PointOfInstantiation, diag::err_partial_spec_ordering_ambiguous)
1883 << ClassTemplateSpec;
1884
1885 // Print the matching partial specializations.
1886 for (llvm::SmallVector<MatchResult, 4>::iterator P = Matched.begin(),
1887 PEnd = Matched.end();
1888 P != PEnd; ++P)
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +00001889 Diag(P->Partial->getLocation(), diag::note_partial_spec_match)
1890 << getTemplateArgumentBindingsText(
1891 P->Partial->getTemplateParameters(),
1892 *P->Args);
Douglas Gregor01afeef2009-08-28 20:31:08 +00001893
Douglas Gregor21610382009-10-29 00:04:11 +00001894 return true;
1895 }
Douglas Gregorbe999392009-09-15 16:23:51 +00001896 }
1897
1898 // Instantiate using the best class template partial specialization.
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +00001899 ClassTemplatePartialSpecializationDecl *OrigPartialSpec = Best->Partial;
Douglas Gregor21610382009-10-29 00:04:11 +00001900 while (OrigPartialSpec->getInstantiatedFromMember()) {
1901 // If we've found an explicit specialization of this class template,
1902 // stop here and use that as the pattern.
1903 if (OrigPartialSpec->isMemberSpecialization())
1904 break;
1905
1906 OrigPartialSpec = OrigPartialSpec->getInstantiatedFromMember();
1907 }
1908
1909 Pattern = OrigPartialSpec;
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +00001910 ClassTemplateSpec->setInstantiationOf(Best->Partial, Best->Args);
Douglas Gregor170bc422009-06-12 22:31:52 +00001911 } else {
1912 // -- If no matches are found, the instantiation is generated
1913 // from the primary template.
Douglas Gregor01afeef2009-08-28 20:31:08 +00001914 ClassTemplateDecl *OrigTemplate = Template;
Douglas Gregorcf915552009-10-13 16:30:37 +00001915 while (OrigTemplate->getInstantiatedFromMemberTemplate()) {
1916 // If we've found an explicit specialization of this class template,
1917 // stop here and use that as the pattern.
1918 if (OrigTemplate->isMemberSpecialization())
1919 break;
1920
Douglas Gregor01afeef2009-08-28 20:31:08 +00001921 OrigTemplate = OrigTemplate->getInstantiatedFromMemberTemplate();
Douglas Gregorcf915552009-10-13 16:30:37 +00001922 }
1923
Douglas Gregor01afeef2009-08-28 20:31:08 +00001924 Pattern = OrigTemplate->getTemplatedDecl();
Douglas Gregor2373c592009-05-31 09:31:02 +00001925 }
Douglas Gregor463421d2009-03-03 04:44:36 +00001926
Douglas Gregoref6ab412009-10-27 06:26:26 +00001927 bool Result = InstantiateClass(PointOfInstantiation, ClassTemplateSpec,
1928 Pattern,
1929 getTemplateInstantiationArgs(ClassTemplateSpec),
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001930 TSK,
Douglas Gregor8a2e6012009-08-24 15:23:48 +00001931 Complain);
Mike Stump11289f42009-09-09 15:08:12 +00001932
Douglas Gregorb7ae10f2009-06-05 00:53:49 +00001933 return Result;
Douglas Gregor463421d2009-03-03 04:44:36 +00001934}
Douglas Gregor90a1a652009-03-19 17:26:29 +00001935
John McCall76d824f2009-08-25 22:02:44 +00001936/// \brief Instantiates the definitions of all of the member
1937/// of the given class, which is an instantiation of a class template
1938/// or a member class of a template.
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00001939void
1940Sema::InstantiateClassMembers(SourceLocation PointOfInstantiation,
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001941 CXXRecordDecl *Instantiation,
1942 const MultiLevelTemplateArgumentList &TemplateArgs,
1943 TemplateSpecializationKind TSK) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001944 for (DeclContext::decl_iterator D = Instantiation->decls_begin(),
1945 DEnd = Instantiation->decls_end();
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00001946 D != DEnd; ++D) {
Douglas Gregor1d957a32009-10-27 18:42:08 +00001947 bool SuppressNew = false;
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00001948 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(*D)) {
Douglas Gregor1d957a32009-10-27 18:42:08 +00001949 if (FunctionDecl *Pattern
1950 = Function->getInstantiatedFromMemberFunction()) {
1951 MemberSpecializationInfo *MSInfo
1952 = Function->getMemberSpecializationInfo();
1953 assert(MSInfo && "No member specialization information?");
Douglas Gregor06aa50412010-04-09 21:02:29 +00001954 if (MSInfo->getTemplateSpecializationKind()
1955 == TSK_ExplicitSpecialization)
1956 continue;
1957
Douglas Gregor1d957a32009-10-27 18:42:08 +00001958 if (CheckSpecializationInstantiationRedecl(PointOfInstantiation, TSK,
1959 Function,
1960 MSInfo->getTemplateSpecializationKind(),
Nico Weberd75488d2010-09-27 21:02:09 +00001961 MSInfo->getPointOfInstantiation(),
Douglas Gregor1d957a32009-10-27 18:42:08 +00001962 SuppressNew) ||
1963 SuppressNew)
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001964 continue;
1965
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001966 if (Function->hasBody())
Douglas Gregor1d957a32009-10-27 18:42:08 +00001967 continue;
1968
1969 if (TSK == TSK_ExplicitInstantiationDefinition) {
1970 // C++0x [temp.explicit]p8:
1971 // An explicit instantiation definition that names a class template
1972 // specialization explicitly instantiates the class template
1973 // specialization and is only an explicit instantiation definition
1974 // of members whose definition is visible at the point of
1975 // instantiation.
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001976 if (!Pattern->hasBody())
Douglas Gregor1d957a32009-10-27 18:42:08 +00001977 continue;
1978
1979 Function->setTemplateSpecializationKind(TSK, PointOfInstantiation);
1980
1981 InstantiateFunctionDefinition(PointOfInstantiation, Function);
1982 } else {
1983 Function->setTemplateSpecializationKind(TSK, PointOfInstantiation);
1984 }
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001985 }
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00001986 } else if (VarDecl *Var = dyn_cast<VarDecl>(*D)) {
Douglas Gregor86d142a2009-10-08 07:24:58 +00001987 if (Var->isStaticDataMember()) {
Douglas Gregor1d957a32009-10-27 18:42:08 +00001988 MemberSpecializationInfo *MSInfo = Var->getMemberSpecializationInfo();
1989 assert(MSInfo && "No member specialization information?");
Douglas Gregor06aa50412010-04-09 21:02:29 +00001990 if (MSInfo->getTemplateSpecializationKind()
1991 == TSK_ExplicitSpecialization)
1992 continue;
1993
Douglas Gregor1d957a32009-10-27 18:42:08 +00001994 if (CheckSpecializationInstantiationRedecl(PointOfInstantiation, TSK,
1995 Var,
1996 MSInfo->getTemplateSpecializationKind(),
Nico Weberd75488d2010-09-27 21:02:09 +00001997 MSInfo->getPointOfInstantiation(),
Douglas Gregor1d957a32009-10-27 18:42:08 +00001998 SuppressNew) ||
1999 SuppressNew)
Douglas Gregorbbe8f462009-10-08 15:14:33 +00002000 continue;
2001
Douglas Gregor1d957a32009-10-27 18:42:08 +00002002 if (TSK == TSK_ExplicitInstantiationDefinition) {
2003 // C++0x [temp.explicit]p8:
2004 // An explicit instantiation definition that names a class template
2005 // specialization explicitly instantiates the class template
2006 // specialization and is only an explicit instantiation definition
2007 // of members whose definition is visible at the point of
2008 // instantiation.
2009 if (!Var->getInstantiatedFromStaticDataMember()
2010 ->getOutOfLineDefinition())
2011 continue;
2012
2013 Var->setTemplateSpecializationKind(TSK, PointOfInstantiation);
Douglas Gregor86d142a2009-10-08 07:24:58 +00002014 InstantiateStaticDataMemberDefinition(PointOfInstantiation, Var);
Douglas Gregor1d957a32009-10-27 18:42:08 +00002015 } else {
2016 Var->setTemplateSpecializationKind(TSK, PointOfInstantiation);
2017 }
2018 }
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00002019 } else if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(*D)) {
Douglas Gregor1da22252010-04-18 18:11:38 +00002020 // Always skip the injected-class-name, along with any
2021 // redeclarations of nested classes, since both would cause us
2022 // to try to instantiate the members of a class twice.
2023 if (Record->isInjectedClassName() || Record->getPreviousDeclaration())
Douglas Gregord801b062009-10-07 23:56:10 +00002024 continue;
2025
Douglas Gregor1d957a32009-10-27 18:42:08 +00002026 MemberSpecializationInfo *MSInfo = Record->getMemberSpecializationInfo();
2027 assert(MSInfo && "No member specialization information?");
Douglas Gregor06aa50412010-04-09 21:02:29 +00002028
2029 if (MSInfo->getTemplateSpecializationKind()
2030 == TSK_ExplicitSpecialization)
2031 continue;
Nico Weberd75488d2010-09-27 21:02:09 +00002032
Douglas Gregor1d957a32009-10-27 18:42:08 +00002033 if (CheckSpecializationInstantiationRedecl(PointOfInstantiation, TSK,
2034 Record,
2035 MSInfo->getTemplateSpecializationKind(),
Nico Weberd75488d2010-09-27 21:02:09 +00002036 MSInfo->getPointOfInstantiation(),
Douglas Gregor1d957a32009-10-27 18:42:08 +00002037 SuppressNew) ||
2038 SuppressNew)
Douglas Gregorbbe8f462009-10-08 15:14:33 +00002039 continue;
2040
Douglas Gregor1d957a32009-10-27 18:42:08 +00002041 CXXRecordDecl *Pattern = Record->getInstantiatedFromMemberClass();
2042 assert(Pattern && "Missing instantiated-from-template information");
2043
Douglas Gregor0a5a2212010-02-11 01:04:33 +00002044 if (!Record->getDefinition()) {
2045 if (!Pattern->getDefinition()) {
Douglas Gregor1d957a32009-10-27 18:42:08 +00002046 // C++0x [temp.explicit]p8:
2047 // An explicit instantiation definition that names a class template
2048 // specialization explicitly instantiates the class template
2049 // specialization and is only an explicit instantiation definition
2050 // of members whose definition is visible at the point of
2051 // instantiation.
2052 if (TSK == TSK_ExplicitInstantiationDeclaration) {
2053 MSInfo->setTemplateSpecializationKind(TSK);
2054 MSInfo->setPointOfInstantiation(PointOfInstantiation);
2055 }
2056
2057 continue;
2058 }
2059
2060 InstantiateClass(PointOfInstantiation, Record, Pattern,
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00002061 TemplateArgs,
2062 TSK);
Nico Weberd75488d2010-09-27 21:02:09 +00002063 } else {
2064 if (TSK == TSK_ExplicitInstantiationDefinition &&
2065 Record->getTemplateSpecializationKind() ==
2066 TSK_ExplicitInstantiationDeclaration) {
2067 Record->setTemplateSpecializationKind(TSK);
2068 MarkVTableUsed(PointOfInstantiation, Record, true);
2069 }
Douglas Gregor1d957a32009-10-27 18:42:08 +00002070 }
Douglas Gregorc093c1d2009-10-08 01:19:17 +00002071
Douglas Gregor0a5a2212010-02-11 01:04:33 +00002072 Pattern = cast_or_null<CXXRecordDecl>(Record->getDefinition());
Douglas Gregor1d957a32009-10-27 18:42:08 +00002073 if (Pattern)
2074 InstantiateClassMembers(PointOfInstantiation, Pattern, TemplateArgs,
2075 TSK);
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00002076 }
2077 }
2078}
2079
2080/// \brief Instantiate the definitions of all of the members of the
2081/// given class template specialization, which was named as part of an
2082/// explicit instantiation.
Mike Stump11289f42009-09-09 15:08:12 +00002083void
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00002084Sema::InstantiateClassTemplateSpecializationMembers(
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00002085 SourceLocation PointOfInstantiation,
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00002086 ClassTemplateSpecializationDecl *ClassTemplateSpec,
2087 TemplateSpecializationKind TSK) {
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00002088 // C++0x [temp.explicit]p7:
2089 // An explicit instantiation that names a class template
2090 // specialization is an explicit instantion of the same kind
2091 // (declaration or definition) of each of its members (not
2092 // including members inherited from base classes) that has not
2093 // been previously explicitly specialized in the translation unit
2094 // containing the explicit instantiation, except as described
2095 // below.
2096 InstantiateClassMembers(PointOfInstantiation, ClassTemplateSpec,
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00002097 getTemplateInstantiationArgs(ClassTemplateSpec),
2098 TSK);
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00002099}
2100
John McCalldadc5752010-08-24 06:29:42 +00002101StmtResult
Douglas Gregor01afeef2009-08-28 20:31:08 +00002102Sema::SubstStmt(Stmt *S, const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregorebe10102009-08-20 07:17:43 +00002103 if (!S)
2104 return Owned(S);
2105
2106 TemplateInstantiator Instantiator(*this, TemplateArgs,
2107 SourceLocation(),
2108 DeclarationName());
2109 return Instantiator.TransformStmt(S);
2110}
2111
John McCalldadc5752010-08-24 06:29:42 +00002112ExprResult
Douglas Gregor01afeef2009-08-28 20:31:08 +00002113Sema::SubstExpr(Expr *E, const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregora16548e2009-08-11 05:31:07 +00002114 if (!E)
2115 return Owned(E);
Mike Stump11289f42009-09-09 15:08:12 +00002116
Douglas Gregora16548e2009-08-11 05:31:07 +00002117 TemplateInstantiator Instantiator(*this, TemplateArgs,
2118 SourceLocation(),
2119 DeclarationName());
2120 return Instantiator.TransformExpr(E);
2121}
2122
Douglas Gregor2cd32a02011-01-07 19:35:17 +00002123bool Sema::SubstExprs(Expr **Exprs, unsigned NumExprs, bool IsCall,
2124 const MultiLevelTemplateArgumentList &TemplateArgs,
2125 llvm::SmallVectorImpl<Expr *> &Outputs) {
2126 if (NumExprs == 0)
2127 return false;
2128
2129 TemplateInstantiator Instantiator(*this, TemplateArgs,
2130 SourceLocation(),
2131 DeclarationName());
2132 return Instantiator.TransformExprs(Exprs, NumExprs, IsCall, Outputs);
2133}
2134
John McCall76d824f2009-08-25 22:02:44 +00002135/// \brief Do template substitution on a nested-name-specifier.
Douglas Gregorf21eb492009-03-26 23:50:42 +00002136NestedNameSpecifier *
John McCall76d824f2009-08-25 22:02:44 +00002137Sema::SubstNestedNameSpecifier(NestedNameSpecifier *NNS,
Douglas Gregor01afeef2009-08-28 20:31:08 +00002138 SourceRange Range,
2139 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor1135c352009-08-06 05:28:30 +00002140 TemplateInstantiator Instantiator(*this, TemplateArgs, Range.getBegin(),
2141 DeclarationName());
Douglas Gregorcd3f49f2010-02-25 04:46:04 +00002142 return Instantiator.TransformNestedNameSpecifier(NNS, Range);
Douglas Gregor90a1a652009-03-19 17:26:29 +00002143}
Douglas Gregoraa594892009-03-31 18:38:02 +00002144
Douglas Gregor14454802011-02-25 02:25:35 +00002145NestedNameSpecifierLoc
2146Sema::SubstNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS,
2147 const MultiLevelTemplateArgumentList &TemplateArgs) {
2148 if (!NNS)
2149 return NestedNameSpecifierLoc();
2150
2151 TemplateInstantiator Instantiator(*this, TemplateArgs, NNS.getBeginLoc(),
2152 DeclarationName());
2153 return Instantiator.TransformNestedNameSpecifierLoc(NNS);
2154}
2155
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002156/// \brief Do template substitution on declaration name info.
2157DeclarationNameInfo
2158Sema::SubstDeclarationNameInfo(const DeclarationNameInfo &NameInfo,
2159 const MultiLevelTemplateArgumentList &TemplateArgs) {
2160 TemplateInstantiator Instantiator(*this, TemplateArgs, NameInfo.getLoc(),
2161 NameInfo.getName());
2162 return Instantiator.TransformDeclarationNameInfo(NameInfo);
2163}
2164
Douglas Gregoraa594892009-03-31 18:38:02 +00002165TemplateName
John McCall76d824f2009-08-25 22:02:44 +00002166Sema::SubstTemplateName(TemplateName Name, SourceLocation Loc,
Douglas Gregor01afeef2009-08-28 20:31:08 +00002167 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor71dc5092009-08-06 06:41:21 +00002168 TemplateInstantiator Instantiator(*this, TemplateArgs, Loc,
2169 DeclarationName());
2170 return Instantiator.TransformTemplateName(Name);
Douglas Gregoraa594892009-03-31 18:38:02 +00002171}
Douglas Gregorc43620d2009-06-11 00:06:24 +00002172
Douglas Gregor0f3feb42010-12-22 21:19:48 +00002173bool Sema::Subst(const TemplateArgumentLoc *Args, unsigned NumArgs,
2174 TemplateArgumentListInfo &Result,
John McCall0ad16662009-10-29 08:12:44 +00002175 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregore922c772009-08-04 22:27:00 +00002176 TemplateInstantiator Instantiator(*this, TemplateArgs, SourceLocation(),
2177 DeclarationName());
Douglas Gregor0f3feb42010-12-22 21:19:48 +00002178
2179 return Instantiator.TransformTemplateArguments(Args, NumArgs, Result);
Douglas Gregorc43620d2009-06-11 00:06:24 +00002180}
Douglas Gregor14cf7522010-04-30 18:55:50 +00002181
Douglas Gregorf3010112011-01-07 16:43:16 +00002182llvm::PointerUnion<Decl *, LocalInstantiationScope::DeclArgumentPack *> *
2183LocalInstantiationScope::findInstantiationOf(const Decl *D) {
Chris Lattnercab02a62011-02-17 20:34:02 +00002184 for (LocalInstantiationScope *Current = this; Current;
Douglas Gregor14cf7522010-04-30 18:55:50 +00002185 Current = Current->Outer) {
Chris Lattnercab02a62011-02-17 20:34:02 +00002186
Douglas Gregor14cf7522010-04-30 18:55:50 +00002187 // Check if we found something within this scope.
Douglas Gregore9fc8dc2010-12-21 21:22:51 +00002188 const Decl *CheckD = D;
2189 do {
Douglas Gregorf3010112011-01-07 16:43:16 +00002190 LocalDeclsMap::iterator Found = Current->LocalDecls.find(CheckD);
Douglas Gregore9fc8dc2010-12-21 21:22:51 +00002191 if (Found != Current->LocalDecls.end())
Douglas Gregorf3010112011-01-07 16:43:16 +00002192 return &Found->second;
Douglas Gregore9fc8dc2010-12-21 21:22:51 +00002193
2194 // If this is a tag declaration, it's possible that we need to look for
2195 // a previous declaration.
2196 if (const TagDecl *Tag = dyn_cast<TagDecl>(CheckD))
2197 CheckD = Tag->getPreviousDeclaration();
2198 else
2199 CheckD = 0;
2200 } while (CheckD);
2201
Douglas Gregor14cf7522010-04-30 18:55:50 +00002202 // If we aren't combined with our outer scope, we're done.
2203 if (!Current->CombineWithOuterScope)
2204 break;
2205 }
Chris Lattnercab02a62011-02-17 20:34:02 +00002206
2207 // If we didn't find the decl, then we either have a sema bug, or we have a
2208 // forward reference to a label declaration. Return null to indicate that
2209 // we have an uninstantiated label.
2210 assert(isa<LabelDecl>(D) && "declaration not instantiated in this scope");
Douglas Gregor14cf7522010-04-30 18:55:50 +00002211 return 0;
2212}
2213
John McCall19c1bfd2010-08-25 05:32:35 +00002214void LocalInstantiationScope::InstantiatedLocal(const Decl *D, Decl *Inst) {
Douglas Gregorf3010112011-01-07 16:43:16 +00002215 llvm::PointerUnion<Decl *, DeclArgumentPack *> &Stored = LocalDecls[D];
Douglas Gregora8bac7f2011-01-10 07:32:04 +00002216 if (Stored.isNull())
2217 Stored = Inst;
2218 else if (Stored.is<Decl *>()) {
2219 assert(Stored.get<Decl *>() == Inst && "Already instantiated this local");
2220 Stored = Inst;
2221 } else
2222 LocalDecls[D].get<DeclArgumentPack *>()->push_back(Inst);
Douglas Gregor14cf7522010-04-30 18:55:50 +00002223}
Douglas Gregorf3010112011-01-07 16:43:16 +00002224
2225void LocalInstantiationScope::InstantiatedLocalPackArg(const Decl *D,
2226 Decl *Inst) {
2227 DeclArgumentPack *Pack = LocalDecls[D].get<DeclArgumentPack *>();
2228 Pack->push_back(Inst);
2229}
2230
2231void LocalInstantiationScope::MakeInstantiatedLocalArgPack(const Decl *D) {
2232 llvm::PointerUnion<Decl *, DeclArgumentPack *> &Stored = LocalDecls[D];
2233 assert(Stored.isNull() && "Already instantiated this local");
2234 DeclArgumentPack *Pack = new DeclArgumentPack;
2235 Stored = Pack;
2236 ArgumentPacks.push_back(Pack);
2237}
2238
Douglas Gregora8bac7f2011-01-10 07:32:04 +00002239void LocalInstantiationScope::SetPartiallySubstitutedPack(NamedDecl *Pack,
2240 const TemplateArgument *ExplicitArgs,
2241 unsigned NumExplicitArgs) {
2242 assert((!PartiallySubstitutedPack || PartiallySubstitutedPack == Pack) &&
2243 "Already have a partially-substituted pack");
2244 assert((!PartiallySubstitutedPack
2245 || NumArgsInPartiallySubstitutedPack == NumExplicitArgs) &&
2246 "Wrong number of arguments in partially-substituted pack");
2247 PartiallySubstitutedPack = Pack;
2248 ArgsInPartiallySubstitutedPack = ExplicitArgs;
2249 NumArgsInPartiallySubstitutedPack = NumExplicitArgs;
2250}
2251
2252NamedDecl *LocalInstantiationScope::getPartiallySubstitutedPack(
2253 const TemplateArgument **ExplicitArgs,
2254 unsigned *NumExplicitArgs) const {
2255 if (ExplicitArgs)
2256 *ExplicitArgs = 0;
2257 if (NumExplicitArgs)
2258 *NumExplicitArgs = 0;
2259
2260 for (const LocalInstantiationScope *Current = this; Current;
2261 Current = Current->Outer) {
2262 if (Current->PartiallySubstitutedPack) {
2263 if (ExplicitArgs)
2264 *ExplicitArgs = Current->ArgsInPartiallySubstitutedPack;
2265 if (NumExplicitArgs)
2266 *NumExplicitArgs = Current->NumArgsInPartiallySubstitutedPack;
2267
2268 return Current->PartiallySubstitutedPack;
2269 }
2270
2271 if (!Current->CombineWithOuterScope)
2272 break;
2273 }
2274
2275 return 0;
2276}