blob: d009e2f81b7317a0e40e40738882c529aec5aec2 [file] [log] [blame]
Douglas Gregorfe1e1102009-02-27 19:31:52 +00001//===------- SemaTemplateInstantiate.cpp - C++ Template Instantiation ------===/
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//===----------------------------------------------------------------------===/
8//
9// This file implements C++ template instantiation.
10//
11//===----------------------------------------------------------------------===/
12
John McCall83024632010-08-25 22:03:47 +000013#include "clang/Sema/SemaInternal.h"
Douglas Gregord6ff3322009-08-04 16:50:30 +000014#include "TreeTransform.h"
John McCall8b0666c2010-08-20 18:27:03 +000015#include "clang/Sema/DeclSpec.h"
Richard Smith938f40b2011-06-11 17:19:42 +000016#include "clang/Sema/Initialization.h"
Douglas Gregorc3a6ade2010-08-12 20:07:10 +000017#include "clang/Sema/Lookup.h"
John McCallde6836a2010-08-24 07:21:54 +000018#include "clang/Sema/Template.h"
John McCall19c1bfd2010-08-25 05:32:35 +000019#include "clang/Sema/TemplateDeduction.h"
Douglas Gregor28ad4b52009-05-26 20:50:29 +000020#include "clang/AST/ASTConsumer.h"
Douglas Gregorfe1e1102009-02-27 19:31:52 +000021#include "clang/AST/ASTContext.h"
22#include "clang/AST/Expr.h"
Douglas Gregorfe1e1102009-02-27 19:31:52 +000023#include "clang/AST/DeclTemplate.h"
Douglas Gregorfe1e1102009-02-27 19:31:52 +000024#include "clang/Basic/LangOptions.h"
25
26using namespace clang;
John McCall19c1bfd2010-08-25 05:32:35 +000027using namespace sema;
Douglas Gregorfe1e1102009-02-27 19:31:52 +000028
Douglas Gregor4ea568f2009-03-10 18:03:33 +000029//===----------------------------------------------------------------------===/
30// Template Instantiation Support
31//===----------------------------------------------------------------------===/
32
Douglas Gregor01afeef2009-08-28 20:31:08 +000033/// \brief Retrieve the template argument list(s) that should be used to
34/// instantiate the definition of the given declaration.
Douglas Gregor36d7c5f2009-11-09 19:17:50 +000035///
36/// \param D the declaration for which we are computing template instantiation
37/// arguments.
38///
39/// \param Innermost if non-NULL, the innermost template argument list.
Douglas Gregor8c702532010-02-05 07:33:43 +000040///
41/// \param RelativeToPrimary true if we should get the template
42/// arguments relative to the primary template, even when we're
43/// dealing with a specialization. This is only relevant for function
44/// template specializations.
Douglas Gregor1bd7a942010-05-03 23:29:10 +000045///
46/// \param Pattern If non-NULL, indicates the pattern from which we will be
47/// instantiating the definition of the given declaration, \p D. This is
48/// used to determine the proper set of template instantiation arguments for
49/// friend function template specializations.
Douglas Gregora654dd82009-08-28 17:37:35 +000050MultiLevelTemplateArgumentList
Douglas Gregor36d7c5f2009-11-09 19:17:50 +000051Sema::getTemplateInstantiationArgs(NamedDecl *D,
Douglas Gregor8c702532010-02-05 07:33:43 +000052 const TemplateArgumentList *Innermost,
Douglas Gregor1bd7a942010-05-03 23:29:10 +000053 bool RelativeToPrimary,
54 const FunctionDecl *Pattern) {
Douglas Gregora654dd82009-08-28 17:37:35 +000055 // Accumulate the set of template argument lists in this structure.
56 MultiLevelTemplateArgumentList Result;
Mike Stump11289f42009-09-09 15:08:12 +000057
Douglas Gregor36d7c5f2009-11-09 19:17:50 +000058 if (Innermost)
59 Result.addOuterTemplateArguments(Innermost);
60
Douglas Gregora654dd82009-08-28 17:37:35 +000061 DeclContext *Ctx = dyn_cast<DeclContext>(D);
Douglas Gregora51c9cc2011-05-22 00:21:10 +000062 if (!Ctx) {
Douglas Gregora654dd82009-08-28 17:37:35 +000063 Ctx = D->getDeclContext();
Douglas Gregora51c9cc2011-05-22 00:21:10 +000064
Douglas Gregor55462622011-06-15 14:20:42 +000065 // If we have a template template parameter with translation unit context,
66 // then we're performing substitution into a default template argument of
67 // this template template parameter before we've constructed the template
68 // that will own this template template parameter. In this case, we
69 // use empty template parameter lists for all of the outer templates
70 // to avoid performing any substitutions.
71 if (Ctx->isTranslationUnit()) {
72 if (TemplateTemplateParmDecl *TTP
73 = dyn_cast<TemplateTemplateParmDecl>(D)) {
74 for (unsigned I = 0, N = TTP->getDepth() + 1; I != N; ++I)
75 Result.addOuterTemplateArguments(0, 0);
76 return Result;
77 }
78 }
Douglas Gregora51c9cc2011-05-22 00:21:10 +000079 }
80
John McCall970d5302009-08-29 03:16:09 +000081 while (!Ctx->isFileContext()) {
Douglas Gregora654dd82009-08-28 17:37:35 +000082 // Add template arguments from a class template instantiation.
Mike Stump11289f42009-09-09 15:08:12 +000083 if (ClassTemplateSpecializationDecl *Spec
Douglas Gregora654dd82009-08-28 17:37:35 +000084 = dyn_cast<ClassTemplateSpecializationDecl>(Ctx)) {
85 // We're done when we hit an explicit specialization.
Douglas Gregor9961ce92010-07-08 18:37:38 +000086 if (Spec->getSpecializationKind() == TSK_ExplicitSpecialization &&
87 !isa<ClassTemplatePartialSpecializationDecl>(Spec))
Douglas Gregora654dd82009-08-28 17:37:35 +000088 break;
Mike Stump11289f42009-09-09 15:08:12 +000089
Douglas Gregora654dd82009-08-28 17:37:35 +000090 Result.addOuterTemplateArguments(&Spec->getTemplateInstantiationArgs());
Douglas Gregorcf915552009-10-13 16:30:37 +000091
92 // If this class template specialization was instantiated from a
93 // specialized member that is a class template, we're done.
94 assert(Spec->getSpecializedTemplate() && "No class template?");
95 if (Spec->getSpecializedTemplate()->isMemberSpecialization())
96 break;
Mike Stump11289f42009-09-09 15:08:12 +000097 }
Douglas Gregora654dd82009-08-28 17:37:35 +000098 // Add template arguments from a function template specialization.
John McCall970d5302009-08-29 03:16:09 +000099 else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Ctx)) {
Douglas Gregor8c702532010-02-05 07:33:43 +0000100 if (!RelativeToPrimary &&
Francois Pichet00c7e6c2011-08-14 03:52:19 +0000101 (Function->getTemplateSpecializationKind() ==
102 TSK_ExplicitSpecialization &&
103 !Function->getClassScopeSpecializationPattern()))
Douglas Gregorcf915552009-10-13 16:30:37 +0000104 break;
105
Douglas Gregora654dd82009-08-28 17:37:35 +0000106 if (const TemplateArgumentList *TemplateArgs
Douglas Gregorcf915552009-10-13 16:30:37 +0000107 = Function->getTemplateSpecializationArgs()) {
108 // Add the template arguments for this specialization.
Douglas Gregora654dd82009-08-28 17:37:35 +0000109 Result.addOuterTemplateArguments(TemplateArgs);
John McCall970d5302009-08-29 03:16:09 +0000110
Douglas Gregorcf915552009-10-13 16:30:37 +0000111 // If this function was instantiated from a specialized member that is
112 // a function template, we're done.
113 assert(Function->getPrimaryTemplate() && "No function template?");
114 if (Function->getPrimaryTemplate()->isMemberSpecialization())
115 break;
Douglas Gregor43669f82011-03-05 17:54:25 +0000116 } else if (FunctionTemplateDecl *FunTmpl
117 = Function->getDescribedFunctionTemplate()) {
118 // Add the "injected" template arguments.
119 std::pair<const TemplateArgument *, unsigned>
120 Injected = FunTmpl->getInjectedTemplateArgs();
121 Result.addOuterTemplateArguments(Injected.first, Injected.second);
Douglas Gregorcf915552009-10-13 16:30:37 +0000122 }
123
John McCall970d5302009-08-29 03:16:09 +0000124 // If this is a friend declaration and it declares an entity at
125 // namespace scope, take arguments from its lexical parent
Douglas Gregor1bd7a942010-05-03 23:29:10 +0000126 // instead of its semantic parent, unless of course the pattern we're
127 // instantiating actually comes from the file's context!
John McCall970d5302009-08-29 03:16:09 +0000128 if (Function->getFriendObjectKind() &&
Douglas Gregor1bd7a942010-05-03 23:29:10 +0000129 Function->getDeclContext()->isFileContext() &&
130 (!Pattern || !Pattern->getLexicalDeclContext()->isFileContext())) {
John McCall970d5302009-08-29 03:16:09 +0000131 Ctx = Function->getLexicalDeclContext();
Douglas Gregor8c702532010-02-05 07:33:43 +0000132 RelativeToPrimary = false;
John McCall970d5302009-08-29 03:16:09 +0000133 continue;
134 }
Douglas Gregor9961ce92010-07-08 18:37:38 +0000135 } else if (CXXRecordDecl *Rec = dyn_cast<CXXRecordDecl>(Ctx)) {
136 if (ClassTemplateDecl *ClassTemplate = Rec->getDescribedClassTemplate()) {
137 QualType T = ClassTemplate->getInjectedClassNameSpecialization();
138 const TemplateSpecializationType *TST
139 = cast<TemplateSpecializationType>(Context.getCanonicalType(T));
140 Result.addOuterTemplateArguments(TST->getArgs(), TST->getNumArgs());
141 if (ClassTemplate->isMemberSpecialization())
142 break;
143 }
Douglas Gregora654dd82009-08-28 17:37:35 +0000144 }
John McCall970d5302009-08-29 03:16:09 +0000145
146 Ctx = Ctx->getParent();
Douglas Gregor8c702532010-02-05 07:33:43 +0000147 RelativeToPrimary = false;
Douglas Gregorb4850462009-05-14 23:26:13 +0000148 }
Mike Stump11289f42009-09-09 15:08:12 +0000149
Douglas Gregora654dd82009-08-28 17:37:35 +0000150 return Result;
Douglas Gregorb4850462009-05-14 23:26:13 +0000151}
152
Douglas Gregor84d49a22009-11-11 21:54:23 +0000153bool Sema::ActiveTemplateInstantiation::isInstantiationRecord() const {
154 switch (Kind) {
155 case TemplateInstantiation:
Richard Smithf623c962012-04-17 00:58:00 +0000156 case ExceptionSpecInstantiation:
Douglas Gregor84d49a22009-11-11 21:54:23 +0000157 case DefaultTemplateArgumentInstantiation:
158 case DefaultFunctionArgumentInstantiation:
Douglas Gregor84d49a22009-11-11 21:54:23 +0000159 case ExplicitTemplateArgumentSubstitution:
160 case DeducedTemplateArgumentSubstitution:
161 case PriorTemplateArgumentSubstitution:
Richard Smith8a874c92012-07-08 02:38:24 +0000162 return true;
163
Douglas Gregor84d49a22009-11-11 21:54:23 +0000164 case DefaultTemplateArgumentChecking:
165 return false;
166 }
David Blaikie8a40f702012-01-17 06:56:22 +0000167
168 llvm_unreachable("Invalid InstantiationKind!");
Douglas Gregor84d49a22009-11-11 21:54:23 +0000169}
170
Douglas Gregorfcd5db32009-03-10 00:06:19 +0000171Sema::InstantiatingTemplate::
172InstantiatingTemplate(Sema &SemaRef, SourceLocation PointOfInstantiation,
Douglas Gregor85673582009-05-18 17:01:57 +0000173 Decl *Entity,
Douglas Gregorfcd5db32009-03-10 00:06:19 +0000174 SourceRange InstantiationRange)
Douglas Gregoredb76852011-01-27 22:31:44 +0000175 : SemaRef(SemaRef),
176 SavedInNonInstantiationSFINAEContext(
177 SemaRef.InNonInstantiationSFINAEContext)
178{
Douglas Gregor79cf6032009-03-10 20:44:00 +0000179 Invalid = CheckInstantiationDepth(PointOfInstantiation,
180 InstantiationRange);
181 if (!Invalid) {
Douglas Gregorfcd5db32009-03-10 00:06:19 +0000182 ActiveTemplateInstantiation Inst;
Douglas Gregor79cf6032009-03-10 20:44:00 +0000183 Inst.Kind = ActiveTemplateInstantiation::TemplateInstantiation;
Douglas Gregorfcd5db32009-03-10 00:06:19 +0000184 Inst.PointOfInstantiation = PointOfInstantiation;
Douglas Gregor79cf6032009-03-10 20:44:00 +0000185 Inst.Entity = reinterpret_cast<uintptr_t>(Entity);
Douglas Gregorc9220832009-03-12 18:36:18 +0000186 Inst.TemplateArgs = 0;
187 Inst.NumTemplateArgs = 0;
Douglas Gregor79cf6032009-03-10 20:44:00 +0000188 Inst.InstantiationRange = InstantiationRange;
Douglas Gregoredb76852011-01-27 22:31:44 +0000189 SemaRef.InNonInstantiationSFINAEContext = false;
Douglas Gregor79cf6032009-03-10 20:44:00 +0000190 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
Douglas Gregor79cf6032009-03-10 20:44:00 +0000191 }
192}
193
Richard Smithf623c962012-04-17 00:58:00 +0000194Sema::InstantiatingTemplate::
195InstantiatingTemplate(Sema &SemaRef, SourceLocation PointOfInstantiation,
196 FunctionDecl *Entity, ExceptionSpecification,
197 SourceRange InstantiationRange)
198 : SemaRef(SemaRef),
199 SavedInNonInstantiationSFINAEContext(
200 SemaRef.InNonInstantiationSFINAEContext)
201{
202 Invalid = CheckInstantiationDepth(PointOfInstantiation,
203 InstantiationRange);
204 if (!Invalid) {
205 ActiveTemplateInstantiation Inst;
206 Inst.Kind = ActiveTemplateInstantiation::ExceptionSpecInstantiation;
207 Inst.PointOfInstantiation = PointOfInstantiation;
208 Inst.Entity = reinterpret_cast<uintptr_t>(Entity);
209 Inst.TemplateArgs = 0;
210 Inst.NumTemplateArgs = 0;
211 Inst.InstantiationRange = InstantiationRange;
212 SemaRef.InNonInstantiationSFINAEContext = false;
213 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
214 }
215}
216
Richard Smith80934652012-07-16 01:09:10 +0000217Sema::InstantiatingTemplate::
218InstantiatingTemplate(Sema &SemaRef, SourceLocation PointOfInstantiation,
219 TemplateDecl *Template,
220 ArrayRef<TemplateArgument> TemplateArgs,
221 SourceRange InstantiationRange)
Douglas Gregoredb76852011-01-27 22:31:44 +0000222 : SemaRef(SemaRef),
223 SavedInNonInstantiationSFINAEContext(
224 SemaRef.InNonInstantiationSFINAEContext)
225{
Douglas Gregor79cf6032009-03-10 20:44:00 +0000226 Invalid = CheckInstantiationDepth(PointOfInstantiation,
227 InstantiationRange);
228 if (!Invalid) {
229 ActiveTemplateInstantiation Inst;
Mike Stump11289f42009-09-09 15:08:12 +0000230 Inst.Kind
Douglas Gregor79cf6032009-03-10 20:44:00 +0000231 = ActiveTemplateInstantiation::DefaultTemplateArgumentInstantiation;
232 Inst.PointOfInstantiation = PointOfInstantiation;
233 Inst.Entity = reinterpret_cast<uintptr_t>(Template);
Richard Smith80934652012-07-16 01:09:10 +0000234 Inst.TemplateArgs = TemplateArgs.data();
235 Inst.NumTemplateArgs = TemplateArgs.size();
Douglas Gregorfcd5db32009-03-10 00:06:19 +0000236 Inst.InstantiationRange = InstantiationRange;
Douglas Gregoredb76852011-01-27 22:31:44 +0000237 SemaRef.InNonInstantiationSFINAEContext = false;
Douglas Gregorfcd5db32009-03-10 00:06:19 +0000238 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
Douglas Gregorfcd5db32009-03-10 00:06:19 +0000239 }
240}
241
Richard Smith80934652012-07-16 01:09:10 +0000242Sema::InstantiatingTemplate::
243InstantiatingTemplate(Sema &SemaRef, SourceLocation PointOfInstantiation,
244 FunctionTemplateDecl *FunctionTemplate,
245 ArrayRef<TemplateArgument> TemplateArgs,
246 ActiveTemplateInstantiation::InstantiationKind Kind,
247 sema::TemplateDeductionInfo &DeductionInfo,
248 SourceRange InstantiationRange)
Douglas Gregoredb76852011-01-27 22:31:44 +0000249 : SemaRef(SemaRef),
250 SavedInNonInstantiationSFINAEContext(
251 SemaRef.InNonInstantiationSFINAEContext)
252{
Richard Smith8a874c92012-07-08 02:38:24 +0000253 Invalid = CheckInstantiationDepth(PointOfInstantiation, InstantiationRange);
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000254 if (!Invalid) {
255 ActiveTemplateInstantiation Inst;
256 Inst.Kind = Kind;
257 Inst.PointOfInstantiation = PointOfInstantiation;
258 Inst.Entity = reinterpret_cast<uintptr_t>(FunctionTemplate);
Richard Smith80934652012-07-16 01:09:10 +0000259 Inst.TemplateArgs = TemplateArgs.data();
260 Inst.NumTemplateArgs = TemplateArgs.size();
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +0000261 Inst.DeductionInfo = &DeductionInfo;
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000262 Inst.InstantiationRange = InstantiationRange;
Douglas Gregoredb76852011-01-27 22:31:44 +0000263 SemaRef.InNonInstantiationSFINAEContext = false;
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000264 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
Douglas Gregor84d49a22009-11-11 21:54:23 +0000265
266 if (!Inst.isInstantiationRecord())
267 ++SemaRef.NonInstantiationEntries;
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000268 }
269}
270
Richard Smith80934652012-07-16 01:09:10 +0000271Sema::InstantiatingTemplate::
272InstantiatingTemplate(Sema &SemaRef, SourceLocation PointOfInstantiation,
273 ClassTemplatePartialSpecializationDecl *PartialSpec,
274 ArrayRef<TemplateArgument> TemplateArgs,
275 sema::TemplateDeductionInfo &DeductionInfo,
276 SourceRange InstantiationRange)
Douglas Gregoredb76852011-01-27 22:31:44 +0000277 : SemaRef(SemaRef),
278 SavedInNonInstantiationSFINAEContext(
279 SemaRef.InNonInstantiationSFINAEContext)
280{
Richard Smith8a874c92012-07-08 02:38:24 +0000281 Invalid = CheckInstantiationDepth(PointOfInstantiation, InstantiationRange);
282 if (!Invalid) {
283 ActiveTemplateInstantiation Inst;
284 Inst.Kind = ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution;
285 Inst.PointOfInstantiation = PointOfInstantiation;
286 Inst.Entity = reinterpret_cast<uintptr_t>(PartialSpec);
Richard Smith80934652012-07-16 01:09:10 +0000287 Inst.TemplateArgs = TemplateArgs.data();
288 Inst.NumTemplateArgs = TemplateArgs.size();
Richard Smith8a874c92012-07-08 02:38:24 +0000289 Inst.DeductionInfo = &DeductionInfo;
290 Inst.InstantiationRange = InstantiationRange;
291 SemaRef.InNonInstantiationSFINAEContext = false;
292 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
293 }
Douglas Gregor637d9982009-06-10 23:47:09 +0000294}
295
Richard Smith80934652012-07-16 01:09:10 +0000296Sema::InstantiatingTemplate::
297InstantiatingTemplate(Sema &SemaRef, SourceLocation PointOfInstantiation,
298 ParmVarDecl *Param,
299 ArrayRef<TemplateArgument> TemplateArgs,
300 SourceRange InstantiationRange)
Douglas Gregoredb76852011-01-27 22:31:44 +0000301 : SemaRef(SemaRef),
302 SavedInNonInstantiationSFINAEContext(
303 SemaRef.InNonInstantiationSFINAEContext)
304{
Douglas Gregore62e6a02009-11-11 19:13:48 +0000305 Invalid = CheckInstantiationDepth(PointOfInstantiation, InstantiationRange);
Anders Carlsson657bad42009-09-05 05:14:19 +0000306 if (!Invalid) {
307 ActiveTemplateInstantiation Inst;
308 Inst.Kind
309 = ActiveTemplateInstantiation::DefaultFunctionArgumentInstantiation;
Douglas Gregore62e6a02009-11-11 19:13:48 +0000310 Inst.PointOfInstantiation = PointOfInstantiation;
Anders Carlsson657bad42009-09-05 05:14:19 +0000311 Inst.Entity = reinterpret_cast<uintptr_t>(Param);
Richard Smith80934652012-07-16 01:09:10 +0000312 Inst.TemplateArgs = TemplateArgs.data();
313 Inst.NumTemplateArgs = TemplateArgs.size();
Anders Carlsson657bad42009-09-05 05:14:19 +0000314 Inst.InstantiationRange = InstantiationRange;
Douglas Gregoredb76852011-01-27 22:31:44 +0000315 SemaRef.InNonInstantiationSFINAEContext = false;
Anders Carlsson657bad42009-09-05 05:14:19 +0000316 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
Douglas Gregore62e6a02009-11-11 19:13:48 +0000317 }
318}
319
320Sema::InstantiatingTemplate::
321InstantiatingTemplate(Sema &SemaRef, SourceLocation PointOfInstantiation,
Richard Smith80934652012-07-16 01:09:10 +0000322 NamedDecl *Template, NonTypeTemplateParmDecl *Param,
323 ArrayRef<TemplateArgument> TemplateArgs,
324 SourceRange InstantiationRange)
Douglas Gregoredb76852011-01-27 22:31:44 +0000325 : SemaRef(SemaRef),
326 SavedInNonInstantiationSFINAEContext(
327 SemaRef.InNonInstantiationSFINAEContext)
328{
Richard Smith8a874c92012-07-08 02:38:24 +0000329 Invalid = CheckInstantiationDepth(PointOfInstantiation, InstantiationRange);
330 if (!Invalid) {
331 ActiveTemplateInstantiation Inst;
332 Inst.Kind = ActiveTemplateInstantiation::PriorTemplateArgumentSubstitution;
333 Inst.PointOfInstantiation = PointOfInstantiation;
334 Inst.Template = Template;
335 Inst.Entity = reinterpret_cast<uintptr_t>(Param);
Richard Smith80934652012-07-16 01:09:10 +0000336 Inst.TemplateArgs = TemplateArgs.data();
337 Inst.NumTemplateArgs = TemplateArgs.size();
Richard Smith8a874c92012-07-08 02:38:24 +0000338 Inst.InstantiationRange = InstantiationRange;
339 SemaRef.InNonInstantiationSFINAEContext = false;
340 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
341 }
Douglas Gregore62e6a02009-11-11 19:13:48 +0000342}
343
344Sema::InstantiatingTemplate::
345InstantiatingTemplate(Sema &SemaRef, SourceLocation PointOfInstantiation,
Richard Smith80934652012-07-16 01:09:10 +0000346 NamedDecl *Template, TemplateTemplateParmDecl *Param,
347 ArrayRef<TemplateArgument> TemplateArgs,
348 SourceRange InstantiationRange)
Douglas Gregoredb76852011-01-27 22:31:44 +0000349 : SemaRef(SemaRef),
350 SavedInNonInstantiationSFINAEContext(
351 SemaRef.InNonInstantiationSFINAEContext)
352{
Richard Smith8a874c92012-07-08 02:38:24 +0000353 Invalid = CheckInstantiationDepth(PointOfInstantiation, InstantiationRange);
354 if (!Invalid) {
355 ActiveTemplateInstantiation Inst;
356 Inst.Kind = ActiveTemplateInstantiation::PriorTemplateArgumentSubstitution;
357 Inst.PointOfInstantiation = PointOfInstantiation;
358 Inst.Template = Template;
359 Inst.Entity = reinterpret_cast<uintptr_t>(Param);
Richard Smith80934652012-07-16 01:09:10 +0000360 Inst.TemplateArgs = TemplateArgs.data();
361 Inst.NumTemplateArgs = TemplateArgs.size();
Richard Smith8a874c92012-07-08 02:38:24 +0000362 Inst.InstantiationRange = InstantiationRange;
363 SemaRef.InNonInstantiationSFINAEContext = false;
364 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
365 }
Douglas Gregor84d49a22009-11-11 21:54:23 +0000366}
367
368Sema::InstantiatingTemplate::
369InstantiatingTemplate(Sema &SemaRef, SourceLocation PointOfInstantiation,
Richard Smith80934652012-07-16 01:09:10 +0000370 TemplateDecl *Template, NamedDecl *Param,
371 ArrayRef<TemplateArgument> TemplateArgs,
372 SourceRange InstantiationRange)
Douglas Gregoredb76852011-01-27 22:31:44 +0000373 : SemaRef(SemaRef),
374 SavedInNonInstantiationSFINAEContext(
375 SemaRef.InNonInstantiationSFINAEContext)
376{
Douglas Gregor84d49a22009-11-11 21:54:23 +0000377 Invalid = false;
378
379 ActiveTemplateInstantiation Inst;
380 Inst.Kind = ActiveTemplateInstantiation::DefaultTemplateArgumentChecking;
381 Inst.PointOfInstantiation = PointOfInstantiation;
382 Inst.Template = Template;
383 Inst.Entity = reinterpret_cast<uintptr_t>(Param);
Richard Smith80934652012-07-16 01:09:10 +0000384 Inst.TemplateArgs = TemplateArgs.data();
385 Inst.NumTemplateArgs = TemplateArgs.size();
Douglas Gregor84d49a22009-11-11 21:54:23 +0000386 Inst.InstantiationRange = InstantiationRange;
Douglas Gregoredb76852011-01-27 22:31:44 +0000387 SemaRef.InNonInstantiationSFINAEContext = false;
Douglas Gregor84d49a22009-11-11 21:54:23 +0000388 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
389
390 assert(!Inst.isInstantiationRecord());
391 ++SemaRef.NonInstantiationEntries;
Anders Carlsson657bad42009-09-05 05:14:19 +0000392}
393
Douglas Gregor85673582009-05-18 17:01:57 +0000394void Sema::InstantiatingTemplate::Clear() {
395 if (!Invalid) {
Douglas Gregor84d49a22009-11-11 21:54:23 +0000396 if (!SemaRef.ActiveTemplateInstantiations.back().isInstantiationRecord()) {
397 assert(SemaRef.NonInstantiationEntries > 0);
398 --SemaRef.NonInstantiationEntries;
399 }
Douglas Gregoredb76852011-01-27 22:31:44 +0000400 SemaRef.InNonInstantiationSFINAEContext
401 = SavedInNonInstantiationSFINAEContext;
Douglas Gregorfcd5db32009-03-10 00:06:19 +0000402 SemaRef.ActiveTemplateInstantiations.pop_back();
Douglas Gregor85673582009-05-18 17:01:57 +0000403 Invalid = true;
404 }
Douglas Gregorfcd5db32009-03-10 00:06:19 +0000405}
406
Douglas Gregor79cf6032009-03-10 20:44:00 +0000407bool Sema::InstantiatingTemplate::CheckInstantiationDepth(
408 SourceLocation PointOfInstantiation,
409 SourceRange InstantiationRange) {
Douglas Gregor84d49a22009-11-11 21:54:23 +0000410 assert(SemaRef.NonInstantiationEntries <=
411 SemaRef.ActiveTemplateInstantiations.size());
412 if ((SemaRef.ActiveTemplateInstantiations.size() -
413 SemaRef.NonInstantiationEntries)
David Blaikiebbafb8a2012-03-11 07:00:24 +0000414 <= SemaRef.getLangOpts().InstantiationDepth)
Douglas Gregor79cf6032009-03-10 20:44:00 +0000415 return false;
416
Mike Stump11289f42009-09-09 15:08:12 +0000417 SemaRef.Diag(PointOfInstantiation,
Douglas Gregor79cf6032009-03-10 20:44:00 +0000418 diag::err_template_recursion_depth_exceeded)
David Blaikiebbafb8a2012-03-11 07:00:24 +0000419 << SemaRef.getLangOpts().InstantiationDepth
Douglas Gregor79cf6032009-03-10 20:44:00 +0000420 << InstantiationRange;
421 SemaRef.Diag(PointOfInstantiation, diag::note_template_recursion_depth)
David Blaikiebbafb8a2012-03-11 07:00:24 +0000422 << SemaRef.getLangOpts().InstantiationDepth;
Douglas Gregor79cf6032009-03-10 20:44:00 +0000423 return true;
424}
425
Douglas Gregor4ea568f2009-03-10 18:03:33 +0000426/// \brief Prints the current instantiation stack through a series of
427/// notes.
428void Sema::PrintInstantiationStack() {
Douglas Gregorffed1cb2010-04-20 07:18:24 +0000429 // Determine which template instantiations to skip, if any.
430 unsigned SkipStart = ActiveTemplateInstantiations.size(), SkipEnd = SkipStart;
431 unsigned Limit = Diags.getTemplateBacktraceLimit();
432 if (Limit && Limit < ActiveTemplateInstantiations.size()) {
433 SkipStart = Limit / 2 + Limit % 2;
434 SkipEnd = ActiveTemplateInstantiations.size() - Limit / 2;
435 }
436
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000437 // FIXME: In all of these cases, we need to show the template arguments
Douglas Gregorffed1cb2010-04-20 07:18:24 +0000438 unsigned InstantiationIdx = 0;
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000439 for (SmallVector<ActiveTemplateInstantiation, 16>::reverse_iterator
Douglas Gregor4ea568f2009-03-10 18:03:33 +0000440 Active = ActiveTemplateInstantiations.rbegin(),
441 ActiveEnd = ActiveTemplateInstantiations.rend();
442 Active != ActiveEnd;
Douglas Gregorffed1cb2010-04-20 07:18:24 +0000443 ++Active, ++InstantiationIdx) {
444 // Skip this instantiation?
445 if (InstantiationIdx >= SkipStart && InstantiationIdx < SkipEnd) {
446 if (InstantiationIdx == SkipStart) {
447 // Note that we're skipping instantiations.
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000448 Diags.Report(Active->PointOfInstantiation,
Douglas Gregorffed1cb2010-04-20 07:18:24 +0000449 diag::note_instantiation_contexts_suppressed)
450 << unsigned(ActiveTemplateInstantiations.size() - Limit);
451 }
452 continue;
453 }
454
Douglas Gregor79cf6032009-03-10 20:44:00 +0000455 switch (Active->Kind) {
456 case ActiveTemplateInstantiation::TemplateInstantiation: {
Douglas Gregor85673582009-05-18 17:01:57 +0000457 Decl *D = reinterpret_cast<Decl *>(Active->Entity);
458 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) {
459 unsigned DiagID = diag::note_template_member_class_here;
460 if (isa<ClassTemplateSpecializationDecl>(Record))
461 DiagID = diag::note_template_class_instantiation_here;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000462 Diags.Report(Active->PointOfInstantiation, DiagID)
Douglas Gregor85673582009-05-18 17:01:57 +0000463 << Context.getTypeDeclType(Record)
464 << Active->InstantiationRange;
Douglas Gregora6ef8f02009-07-24 20:34:43 +0000465 } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
Douglas Gregor4adbc6d2009-06-26 00:10:03 +0000466 unsigned DiagID;
467 if (Function->getPrimaryTemplate())
468 DiagID = diag::note_function_template_spec_here;
469 else
470 DiagID = diag::note_template_member_function_here;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000471 Diags.Report(Active->PointOfInstantiation, DiagID)
Douglas Gregor85673582009-05-18 17:01:57 +0000472 << Function
473 << Active->InstantiationRange;
Richard Smith3f1b5d02011-05-05 21:57:07 +0000474 } else if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000475 Diags.Report(Active->PointOfInstantiation,
Douglas Gregora6ef8f02009-07-24 20:34:43 +0000476 diag::note_template_static_data_member_def_here)
Richard Smith3f1b5d02011-05-05 21:57:07 +0000477 << VD
478 << Active->InstantiationRange;
Richard Smith4b38ded2012-03-14 23:13:10 +0000479 } else if (EnumDecl *ED = dyn_cast<EnumDecl>(D)) {
480 Diags.Report(Active->PointOfInstantiation,
481 diag::note_template_enum_def_here)
482 << ED
483 << Active->InstantiationRange;
Richard Smith3f1b5d02011-05-05 21:57:07 +0000484 } else {
485 Diags.Report(Active->PointOfInstantiation,
486 diag::note_template_type_alias_instantiation_here)
487 << cast<TypeAliasTemplateDecl>(D)
Douglas Gregora6ef8f02009-07-24 20:34:43 +0000488 << Active->InstantiationRange;
Douglas Gregor85673582009-05-18 17:01:57 +0000489 }
Douglas Gregor79cf6032009-03-10 20:44:00 +0000490 break;
491 }
492
493 case ActiveTemplateInstantiation::DefaultTemplateArgumentInstantiation: {
494 TemplateDecl *Template = cast<TemplateDecl>((Decl *)Active->Entity);
495 std::string TemplateArgsStr
Douglas Gregordc572a32009-03-30 22:58:21 +0000496 = TemplateSpecializationType::PrintTemplateArgumentList(
Mike Stump11289f42009-09-09 15:08:12 +0000497 Active->TemplateArgs,
Douglas Gregor7de59662009-05-29 20:38:28 +0000498 Active->NumTemplateArgs,
Douglas Gregor75acd922011-09-27 23:30:47 +0000499 getPrintingPolicy());
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000500 Diags.Report(Active->PointOfInstantiation,
Douglas Gregor79cf6032009-03-10 20:44:00 +0000501 diag::note_default_arg_instantiation_here)
502 << (Template->getNameAsString() + TemplateArgsStr)
503 << Active->InstantiationRange;
504 break;
505 }
Douglas Gregor637d9982009-06-10 23:47:09 +0000506
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000507 case ActiveTemplateInstantiation::ExplicitTemplateArgumentSubstitution: {
Mike Stump11289f42009-09-09 15:08:12 +0000508 FunctionTemplateDecl *FnTmpl
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000509 = cast<FunctionTemplateDecl>((Decl *)Active->Entity);
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000510 Diags.Report(Active->PointOfInstantiation,
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000511 diag::note_explicit_template_arg_substitution_here)
Douglas Gregor607f1412010-03-30 20:35:20 +0000512 << FnTmpl
513 << getTemplateArgumentBindingsText(FnTmpl->getTemplateParameters(),
514 Active->TemplateArgs,
515 Active->NumTemplateArgs)
516 << Active->InstantiationRange;
Douglas Gregor637d9982009-06-10 23:47:09 +0000517 break;
518 }
Mike Stump11289f42009-09-09 15:08:12 +0000519
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000520 case ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution:
521 if (ClassTemplatePartialSpecializationDecl *PartialSpec
522 = dyn_cast<ClassTemplatePartialSpecializationDecl>(
523 (Decl *)Active->Entity)) {
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000524 Diags.Report(Active->PointOfInstantiation,
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000525 diag::note_partial_spec_deduct_instantiation_here)
526 << Context.getTypeDeclType(PartialSpec)
Douglas Gregor607f1412010-03-30 20:35:20 +0000527 << getTemplateArgumentBindingsText(
528 PartialSpec->getTemplateParameters(),
529 Active->TemplateArgs,
530 Active->NumTemplateArgs)
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000531 << Active->InstantiationRange;
532 } else {
533 FunctionTemplateDecl *FnTmpl
534 = cast<FunctionTemplateDecl>((Decl *)Active->Entity);
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000535 Diags.Report(Active->PointOfInstantiation,
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000536 diag::note_function_template_deduction_instantiation_here)
Douglas Gregor607f1412010-03-30 20:35:20 +0000537 << FnTmpl
538 << getTemplateArgumentBindingsText(FnTmpl->getTemplateParameters(),
539 Active->TemplateArgs,
540 Active->NumTemplateArgs)
541 << Active->InstantiationRange;
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000542 }
543 break;
Douglas Gregor637d9982009-06-10 23:47:09 +0000544
Anders Carlsson657bad42009-09-05 05:14:19 +0000545 case ActiveTemplateInstantiation::DefaultFunctionArgumentInstantiation: {
546 ParmVarDecl *Param = cast<ParmVarDecl>((Decl *)Active->Entity);
547 FunctionDecl *FD = cast<FunctionDecl>(Param->getDeclContext());
Mike Stump11289f42009-09-09 15:08:12 +0000548
Anders Carlsson657bad42009-09-05 05:14:19 +0000549 std::string TemplateArgsStr
550 = TemplateSpecializationType::PrintTemplateArgumentList(
Mike Stump11289f42009-09-09 15:08:12 +0000551 Active->TemplateArgs,
Anders Carlsson657bad42009-09-05 05:14:19 +0000552 Active->NumTemplateArgs,
Douglas Gregor75acd922011-09-27 23:30:47 +0000553 getPrintingPolicy());
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000554 Diags.Report(Active->PointOfInstantiation,
Anders Carlsson657bad42009-09-05 05:14:19 +0000555 diag::note_default_function_arg_instantiation_here)
Anders Carlssondc6d2c32009-09-05 05:38:54 +0000556 << (FD->getNameAsString() + TemplateArgsStr)
Anders Carlsson657bad42009-09-05 05:14:19 +0000557 << Active->InstantiationRange;
558 break;
559 }
Mike Stump11289f42009-09-09 15:08:12 +0000560
Douglas Gregore62e6a02009-11-11 19:13:48 +0000561 case ActiveTemplateInstantiation::PriorTemplateArgumentSubstitution: {
562 NamedDecl *Parm = cast<NamedDecl>((Decl *)Active->Entity);
563 std::string Name;
564 if (!Parm->getName().empty())
565 Name = std::string(" '") + Parm->getName().str() + "'";
Douglas Gregorca4686d2011-01-04 23:35:54 +0000566
567 TemplateParameterList *TemplateParams = 0;
568 if (TemplateDecl *Template = dyn_cast<TemplateDecl>(Active->Template))
569 TemplateParams = Template->getTemplateParameters();
570 else
571 TemplateParams =
572 cast<ClassTemplatePartialSpecializationDecl>(Active->Template)
573 ->getTemplateParameters();
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000574 Diags.Report(Active->PointOfInstantiation,
Douglas Gregore62e6a02009-11-11 19:13:48 +0000575 diag::note_prior_template_arg_substitution)
576 << isa<TemplateTemplateParmDecl>(Parm)
577 << Name
Douglas Gregorca4686d2011-01-04 23:35:54 +0000578 << getTemplateArgumentBindingsText(TemplateParams,
Douglas Gregore62e6a02009-11-11 19:13:48 +0000579 Active->TemplateArgs,
580 Active->NumTemplateArgs)
581 << Active->InstantiationRange;
582 break;
583 }
Douglas Gregor84d49a22009-11-11 21:54:23 +0000584
585 case ActiveTemplateInstantiation::DefaultTemplateArgumentChecking: {
Douglas Gregorca4686d2011-01-04 23:35:54 +0000586 TemplateParameterList *TemplateParams = 0;
587 if (TemplateDecl *Template = dyn_cast<TemplateDecl>(Active->Template))
588 TemplateParams = Template->getTemplateParameters();
589 else
590 TemplateParams =
591 cast<ClassTemplatePartialSpecializationDecl>(Active->Template)
592 ->getTemplateParameters();
593
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000594 Diags.Report(Active->PointOfInstantiation,
Douglas Gregor84d49a22009-11-11 21:54:23 +0000595 diag::note_template_default_arg_checking)
Douglas Gregorca4686d2011-01-04 23:35:54 +0000596 << getTemplateArgumentBindingsText(TemplateParams,
Douglas Gregor84d49a22009-11-11 21:54:23 +0000597 Active->TemplateArgs,
598 Active->NumTemplateArgs)
599 << Active->InstantiationRange;
600 break;
601 }
Richard Smithf623c962012-04-17 00:58:00 +0000602
603 case ActiveTemplateInstantiation::ExceptionSpecInstantiation:
604 Diags.Report(Active->PointOfInstantiation,
605 diag::note_template_exception_spec_instantiation_here)
606 << cast<FunctionDecl>((Decl *)Active->Entity)
607 << Active->InstantiationRange;
608 break;
Douglas Gregor79cf6032009-03-10 20:44:00 +0000609 }
Douglas Gregor4ea568f2009-03-10 18:03:33 +0000610 }
611}
612
Douglas Gregoredb76852011-01-27 22:31:44 +0000613llvm::Optional<TemplateDeductionInfo *> Sema::isSFINAEContext() const {
Douglas Gregoredb76852011-01-27 22:31:44 +0000614 if (InNonInstantiationSFINAEContext)
615 return llvm::Optional<TemplateDeductionInfo *>(0);
616
Douglas Gregor33834512009-06-14 07:33:30 +0000617 for (SmallVector<ActiveTemplateInstantiation, 16>::const_reverse_iterator
618 Active = ActiveTemplateInstantiations.rbegin(),
619 ActiveEnd = ActiveTemplateInstantiations.rend();
620 Active != ActiveEnd;
Douglas Gregor84d49a22009-11-11 21:54:23 +0000621 ++Active)
622 {
Douglas Gregor33834512009-06-14 07:33:30 +0000623 switch(Active->Kind) {
Douglas Gregoredb76852011-01-27 22:31:44 +0000624 case ActiveTemplateInstantiation::TemplateInstantiation:
Richard Smith72249ba2012-04-26 07:24:08 +0000625 // An instantiation of an alias template may or may not be a SFINAE
626 // context, depending on what else is on the stack.
627 if (isa<TypeAliasTemplateDecl>(reinterpret_cast<Decl *>(Active->Entity)))
628 break;
629 // Fall through.
630 case ActiveTemplateInstantiation::DefaultFunctionArgumentInstantiation:
Richard Smithf623c962012-04-17 00:58:00 +0000631 case ActiveTemplateInstantiation::ExceptionSpecInstantiation:
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000632 // This is a template instantiation, so there is no SFINAE.
Douglas Gregoredb76852011-01-27 22:31:44 +0000633 return llvm::Optional<TemplateDeductionInfo *>();
Mike Stump11289f42009-09-09 15:08:12 +0000634
Douglas Gregor33834512009-06-14 07:33:30 +0000635 case ActiveTemplateInstantiation::DefaultTemplateArgumentInstantiation:
Douglas Gregore62e6a02009-11-11 19:13:48 +0000636 case ActiveTemplateInstantiation::PriorTemplateArgumentSubstitution:
Douglas Gregor84d49a22009-11-11 21:54:23 +0000637 case ActiveTemplateInstantiation::DefaultTemplateArgumentChecking:
Douglas Gregore62e6a02009-11-11 19:13:48 +0000638 // A default template argument instantiation and substitution into
639 // template parameters with arguments for prior parameters may or may
640 // not be a SFINAE context; look further up the stack.
Douglas Gregor33834512009-06-14 07:33:30 +0000641 break;
Mike Stump11289f42009-09-09 15:08:12 +0000642
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000643 case ActiveTemplateInstantiation::ExplicitTemplateArgumentSubstitution:
644 case ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution:
645 // We're either substitution explicitly-specified template arguments
646 // or deduced template arguments, so SFINAE applies.
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +0000647 assert(Active->DeductionInfo && "Missing deduction info pointer");
648 return Active->DeductionInfo;
Douglas Gregor33834512009-06-14 07:33:30 +0000649 }
650 }
651
Douglas Gregoredb76852011-01-27 22:31:44 +0000652 return llvm::Optional<TemplateDeductionInfo *>();
Douglas Gregor33834512009-06-14 07:33:30 +0000653}
654
Douglas Gregora8bac7f2011-01-10 07:32:04 +0000655/// \brief Retrieve the depth and index of a parameter pack.
656static std::pair<unsigned, unsigned>
657getDepthAndIndex(NamedDecl *ND) {
658 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(ND))
659 return std::make_pair(TTP->getDepth(), TTP->getIndex());
660
661 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(ND))
662 return std::make_pair(NTTP->getDepth(), NTTP->getIndex());
663
664 TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(ND);
665 return std::make_pair(TTP->getDepth(), TTP->getIndex());
666}
667
Douglas Gregorfe1e1102009-02-27 19:31:52 +0000668//===----------------------------------------------------------------------===/
669// Template Instantiation for Types
670//===----------------------------------------------------------------------===/
Douglas Gregor17c0d7b2009-02-28 00:25:32 +0000671namespace {
Douglas Gregor14cf7522010-04-30 18:55:50 +0000672 class TemplateInstantiator : public TreeTransform<TemplateInstantiator> {
Douglas Gregor01afeef2009-08-28 20:31:08 +0000673 const MultiLevelTemplateArgumentList &TemplateArgs;
Douglas Gregor17c0d7b2009-02-28 00:25:32 +0000674 SourceLocation Loc;
675 DeclarationName Entity;
Douglas Gregorfe1e1102009-02-27 19:31:52 +0000676
Douglas Gregor17c0d7b2009-02-28 00:25:32 +0000677 public:
Douglas Gregorebe10102009-08-20 07:17:43 +0000678 typedef TreeTransform<TemplateInstantiator> inherited;
Mike Stump11289f42009-09-09 15:08:12 +0000679
680 TemplateInstantiator(Sema &SemaRef,
Douglas Gregor01afeef2009-08-28 20:31:08 +0000681 const MultiLevelTemplateArgumentList &TemplateArgs,
Douglas Gregord6ff3322009-08-04 16:50:30 +0000682 SourceLocation Loc,
Mike Stump11289f42009-09-09 15:08:12 +0000683 DeclarationName Entity)
684 : inherited(SemaRef), TemplateArgs(TemplateArgs), Loc(Loc),
Douglas Gregorebe10102009-08-20 07:17:43 +0000685 Entity(Entity) { }
Douglas Gregor17c0d7b2009-02-28 00:25:32 +0000686
Mike Stump11289f42009-09-09 15:08:12 +0000687 /// \brief Determine whether the given type \p T has already been
Douglas Gregord6ff3322009-08-04 16:50:30 +0000688 /// transformed.
689 ///
690 /// For the purposes of template instantiation, a type has already been
691 /// transformed if it is NULL or if it is not dependent.
Douglas Gregor5597ab42010-05-07 23:12:07 +0000692 bool AlreadyTransformed(QualType T);
Mike Stump11289f42009-09-09 15:08:12 +0000693
Douglas Gregord6ff3322009-08-04 16:50:30 +0000694 /// \brief Returns the location of the entity being instantiated, if known.
695 SourceLocation getBaseLocation() { return Loc; }
Mike Stump11289f42009-09-09 15:08:12 +0000696
Douglas Gregord6ff3322009-08-04 16:50:30 +0000697 /// \brief Returns the name of the entity being instantiated, if any.
698 DeclarationName getBaseEntity() { return Entity; }
Mike Stump11289f42009-09-09 15:08:12 +0000699
Douglas Gregoref6ab412009-10-27 06:26:26 +0000700 /// \brief Sets the "base" location and entity when that
701 /// information is known based on another transformation.
702 void setBase(SourceLocation Loc, DeclarationName Entity) {
703 this->Loc = Loc;
704 this->Entity = Entity;
705 }
Douglas Gregor840bd6c2010-12-20 22:05:00 +0000706
707 bool TryExpandParameterPacks(SourceLocation EllipsisLoc,
708 SourceRange PatternRange,
David Blaikieb9c168a2011-09-22 02:34:54 +0000709 llvm::ArrayRef<UnexpandedParameterPack> Unexpanded,
Douglas Gregor840bd6c2010-12-20 22:05:00 +0000710 bool &ShouldExpand,
Douglas Gregora8bac7f2011-01-10 07:32:04 +0000711 bool &RetainExpansion,
Douglas Gregor0dca5fd2011-01-14 17:04:44 +0000712 llvm::Optional<unsigned> &NumExpansions) {
Douglas Gregor76aca7b2010-12-21 00:52:54 +0000713 return getSema().CheckParameterPacksForExpansion(EllipsisLoc,
714 PatternRange, Unexpanded,
Douglas Gregor76aca7b2010-12-21 00:52:54 +0000715 TemplateArgs,
716 ShouldExpand,
Douglas Gregora8bac7f2011-01-10 07:32:04 +0000717 RetainExpansion,
Douglas Gregor76aca7b2010-12-21 00:52:54 +0000718 NumExpansions);
719 }
Douglas Gregor840bd6c2010-12-20 22:05:00 +0000720
Douglas Gregorf3010112011-01-07 16:43:16 +0000721 void ExpandingFunctionParameterPack(ParmVarDecl *Pack) {
722 SemaRef.CurrentInstantiationScope->MakeInstantiatedLocalArgPack(Pack);
723 }
724
Douglas Gregora8bac7f2011-01-10 07:32:04 +0000725 TemplateArgument ForgetPartiallySubstitutedPack() {
726 TemplateArgument Result;
727 if (NamedDecl *PartialPack
728 = SemaRef.CurrentInstantiationScope->getPartiallySubstitutedPack()){
729 MultiLevelTemplateArgumentList &TemplateArgs
730 = const_cast<MultiLevelTemplateArgumentList &>(this->TemplateArgs);
731 unsigned Depth, Index;
732 llvm::tie(Depth, Index) = getDepthAndIndex(PartialPack);
733 if (TemplateArgs.hasTemplateArgument(Depth, Index)) {
734 Result = TemplateArgs(Depth, Index);
735 TemplateArgs.setArgument(Depth, Index, TemplateArgument());
736 }
737 }
738
739 return Result;
740 }
741
742 void RememberPartiallySubstitutedPack(TemplateArgument Arg) {
743 if (Arg.isNull())
744 return;
745
746 if (NamedDecl *PartialPack
747 = SemaRef.CurrentInstantiationScope->getPartiallySubstitutedPack()){
748 MultiLevelTemplateArgumentList &TemplateArgs
749 = const_cast<MultiLevelTemplateArgumentList &>(this->TemplateArgs);
750 unsigned Depth, Index;
751 llvm::tie(Depth, Index) = getDepthAndIndex(PartialPack);
752 TemplateArgs.setArgument(Depth, Index, Arg);
753 }
754 }
755
Douglas Gregord6ff3322009-08-04 16:50:30 +0000756 /// \brief Transform the given declaration by instantiating a reference to
757 /// this declaration.
Douglas Gregora04f2ca2010-03-01 15:56:25 +0000758 Decl *TransformDecl(SourceLocation Loc, Decl *D);
Douglas Gregora16548e2009-08-11 05:31:07 +0000759
Douglas Gregor0c46b2b2012-02-13 22:00:16 +0000760 void transformAttrs(Decl *Old, Decl *New) {
761 SemaRef.InstantiateAttrs(TemplateArgs, Old, New);
762 }
763
764 void transformedLocalDecl(Decl *Old, Decl *New) {
765 SemaRef.CurrentInstantiationScope->InstantiatedLocal(Old, New);
766 }
767
Mike Stump11289f42009-09-09 15:08:12 +0000768 /// \brief Transform the definition of the given declaration by
Douglas Gregorebe10102009-08-20 07:17:43 +0000769 /// instantiating it.
Douglas Gregor25289362010-03-01 17:25:41 +0000770 Decl *TransformDefinition(SourceLocation Loc, Decl *D);
Mike Stump11289f42009-09-09 15:08:12 +0000771
Douglas Gregora5cb6da2009-10-20 05:58:46 +0000772 /// \bried Transform the first qualifier within a scope by instantiating the
773 /// declaration.
774 NamedDecl *TransformFirstQualifierInScope(NamedDecl *D, SourceLocation Loc);
775
Douglas Gregorebe10102009-08-20 07:17:43 +0000776 /// \brief Rebuild the exception declaration and register the declaration
777 /// as an instantiated local.
Douglas Gregor9f0e1aa2010-09-09 17:09:21 +0000778 VarDecl *RebuildExceptionDecl(VarDecl *ExceptionDecl,
John McCallbcd03502009-12-07 02:54:59 +0000779 TypeSourceInfo *Declarator,
Abramo Bagnaradff19302011-03-08 08:55:46 +0000780 SourceLocation StartLoc,
781 SourceLocation NameLoc,
782 IdentifierInfo *Name);
Mike Stump11289f42009-09-09 15:08:12 +0000783
Douglas Gregorf4e837f2010-04-26 17:57:08 +0000784 /// \brief Rebuild the Objective-C exception declaration and register the
785 /// declaration as an instantiated local.
786 VarDecl *RebuildObjCExceptionDecl(VarDecl *ExceptionDecl,
787 TypeSourceInfo *TSInfo, QualType T);
788
John McCall7f41d982009-09-11 04:59:25 +0000789 /// \brief Check for tag mismatches when instantiating an
790 /// elaborated type.
John McCall954b5de2010-11-04 19:04:38 +0000791 QualType RebuildElaboratedType(SourceLocation KeywordLoc,
792 ElaboratedTypeKeyword Keyword,
Douglas Gregor844cb502011-03-01 18:12:44 +0000793 NestedNameSpecifierLoc QualifierLoc,
794 QualType T);
John McCall7f41d982009-09-11 04:59:25 +0000795
Douglas Gregor9db53502011-03-02 18:07:45 +0000796 TemplateName TransformTemplateName(CXXScopeSpec &SS,
797 TemplateName Name,
798 SourceLocation NameLoc,
799 QualType ObjectType = QualType(),
800 NamedDecl *FirstQualifierInScope = 0);
801
John McCalldadc5752010-08-24 06:29:42 +0000802 ExprResult TransformPredefinedExpr(PredefinedExpr *E);
803 ExprResult TransformDeclRefExpr(DeclRefExpr *E);
804 ExprResult TransformCXXDefaultArgExpr(CXXDefaultArgExpr *E);
805 ExprResult TransformTemplateParmRefExpr(DeclRefExpr *E,
Douglas Gregoreb5a39d2010-12-24 00:15:10 +0000806 NonTypeTemplateParmDecl *D);
Douglas Gregorcdbc5392011-01-15 01:15:58 +0000807 ExprResult TransformSubstNonTypeTemplateParmPackExpr(
808 SubstNonTypeTemplateParmPackExpr *E);
809
Douglas Gregor14cf7522010-04-30 18:55:50 +0000810 QualType TransformFunctionProtoType(TypeLocBuilder &TLB,
John McCall31f82722010-11-12 08:19:04 +0000811 FunctionProtoTypeLoc TL);
Douglas Gregor3024f072012-04-16 07:05:22 +0000812 QualType TransformFunctionProtoType(TypeLocBuilder &TLB,
813 FunctionProtoTypeLoc TL,
814 CXXRecordDecl *ThisContext,
815 unsigned ThisTypeQuals);
816
Douglas Gregor715e4612011-01-14 22:40:04 +0000817 ParmVarDecl *TransformFunctionTypeParam(ParmVarDecl *OldParm,
John McCall8fb0d9d2011-05-01 22:35:37 +0000818 int indexAdjustment,
Douglas Gregor0dd22bc2012-01-25 16:15:54 +0000819 llvm::Optional<unsigned> NumExpansions,
820 bool ExpectParameterPack);
John McCall58f10c32010-03-11 09:03:00 +0000821
Mike Stump11289f42009-09-09 15:08:12 +0000822 /// \brief Transforms a template type parameter type by performing
Douglas Gregord6ff3322009-08-04 16:50:30 +0000823 /// substitution of the corresponding template type argument.
John McCall550e0c22009-10-21 00:40:46 +0000824 QualType TransformTemplateTypeParmType(TypeLocBuilder &TLB,
John McCall31f82722010-11-12 08:19:04 +0000825 TemplateTypeParmTypeLoc TL);
Nick Lewyckyc96c37f2010-07-06 19:51:49 +0000826
Douglas Gregorada4b792011-01-14 02:55:32 +0000827 /// \brief Transforms an already-substituted template type parameter pack
828 /// into either itself (if we aren't substituting into its pack expansion)
829 /// or the appropriate substituted argument.
830 QualType TransformSubstTemplateTypeParmPackType(TypeLocBuilder &TLB,
831 SubstTemplateTypeParmPackTypeLoc TL);
832
John McCalldadc5752010-08-24 06:29:42 +0000833 ExprResult TransformCallExpr(CallExpr *CE) {
Nick Lewyckyc96c37f2010-07-06 19:51:49 +0000834 getSema().CallsUndergoingInstantiation.push_back(CE);
John McCalldadc5752010-08-24 06:29:42 +0000835 ExprResult Result =
Nick Lewyckyc96c37f2010-07-06 19:51:49 +0000836 TreeTransform<TemplateInstantiator>::TransformCallExpr(CE);
837 getSema().CallsUndergoingInstantiation.pop_back();
838 return move(Result);
839 }
John McCall7c454bb2011-07-15 05:09:51 +0000840
841 private:
842 ExprResult transformNonTypeTemplateParmRef(NonTypeTemplateParmDecl *parm,
843 SourceLocation loc,
Richard Smith34349002012-07-09 03:07:20 +0000844 TemplateArgument arg);
Douglas Gregord6ff3322009-08-04 16:50:30 +0000845 };
Douglas Gregor04318252009-07-06 15:59:29 +0000846}
847
Douglas Gregor5597ab42010-05-07 23:12:07 +0000848bool TemplateInstantiator::AlreadyTransformed(QualType T) {
849 if (T.isNull())
850 return true;
851
Douglas Gregor678d76c2011-07-01 01:22:09 +0000852 if (T->isInstantiationDependentType() || T->isVariablyModifiedType())
Douglas Gregor5597ab42010-05-07 23:12:07 +0000853 return false;
854
855 getSema().MarkDeclarationsReferencedInType(Loc, T);
856 return true;
857}
858
Douglas Gregora04f2ca2010-03-01 15:56:25 +0000859Decl *TemplateInstantiator::TransformDecl(SourceLocation Loc, Decl *D) {
Douglas Gregor2b6ca462009-09-03 21:38:09 +0000860 if (!D)
861 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000862
Douglas Gregor2b6ca462009-09-03 21:38:09 +0000863 if (TemplateTemplateParmDecl *TTP = dyn_cast<TemplateTemplateParmDecl>(D)) {
Douglas Gregor01afeef2009-08-28 20:31:08 +0000864 if (TTP->getDepth() < TemplateArgs.getNumLevels()) {
Douglas Gregorb93971082010-02-05 19:54:12 +0000865 // If the corresponding template argument is NULL or non-existent, it's
866 // because we are performing instantiation from explicitly-specified
867 // template arguments in a function template, but there were some
868 // arguments left unspecified.
869 if (!TemplateArgs.hasTemplateArgument(TTP->getDepth(),
870 TTP->getPosition()))
871 return D;
872
Douglas Gregorf5500772011-01-05 15:48:55 +0000873 TemplateArgument Arg = TemplateArgs(TTP->getDepth(), TTP->getPosition());
874
875 if (TTP->isParameterPack()) {
876 assert(Arg.getKind() == TemplateArgument::Pack &&
877 "Missing argument pack");
878
Douglas Gregor5590be02011-01-15 06:45:20 +0000879 assert(getSema().ArgumentPackSubstitutionIndex >= 0);
Douglas Gregora8bac7f2011-01-10 07:32:04 +0000880 assert(getSema().ArgumentPackSubstitutionIndex < (int)Arg.pack_size());
Douglas Gregorf5500772011-01-05 15:48:55 +0000881 Arg = Arg.pack_begin()[getSema().ArgumentPackSubstitutionIndex];
882 }
883
884 TemplateName Template = Arg.getAsTemplate();
Douglas Gregor9167f8b2009-11-11 01:00:40 +0000885 assert(!Template.isNull() && Template.getAsTemplateDecl() &&
Douglas Gregor01afeef2009-08-28 20:31:08 +0000886 "Wrong kind of template template argument");
Douglas Gregor9167f8b2009-11-11 01:00:40 +0000887 return Template.getAsTemplateDecl();
Douglas Gregor01afeef2009-08-28 20:31:08 +0000888 }
Mike Stump11289f42009-09-09 15:08:12 +0000889
Douglas Gregor9167f8b2009-11-11 01:00:40 +0000890 // Fall through to find the instantiated declaration for this template
891 // template parameter.
Douglas Gregor71dc5092009-08-06 06:41:21 +0000892 }
Mike Stump11289f42009-09-09 15:08:12 +0000893
Douglas Gregora04f2ca2010-03-01 15:56:25 +0000894 return SemaRef.FindInstantiatedDecl(Loc, cast<NamedDecl>(D), TemplateArgs);
Douglas Gregord6ff3322009-08-04 16:50:30 +0000895}
896
Douglas Gregor25289362010-03-01 17:25:41 +0000897Decl *TemplateInstantiator::TransformDefinition(SourceLocation Loc, Decl *D) {
John McCall76d824f2009-08-25 22:02:44 +0000898 Decl *Inst = getSema().SubstDecl(D, getSema().CurContext, TemplateArgs);
Douglas Gregorebe10102009-08-20 07:17:43 +0000899 if (!Inst)
900 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000901
Douglas Gregorebe10102009-08-20 07:17:43 +0000902 getSema().CurrentInstantiationScope->InstantiatedLocal(D, Inst);
903 return Inst;
904}
905
Douglas Gregora5cb6da2009-10-20 05:58:46 +0000906NamedDecl *
907TemplateInstantiator::TransformFirstQualifierInScope(NamedDecl *D,
908 SourceLocation Loc) {
909 // If the first part of the nested-name-specifier was a template type
910 // parameter, instantiate that type parameter down to a tag type.
911 if (TemplateTypeParmDecl *TTPD = dyn_cast_or_null<TemplateTypeParmDecl>(D)) {
912 const TemplateTypeParmType *TTP
913 = cast<TemplateTypeParmType>(getSema().Context.getTypeDeclType(TTPD));
Douglas Gregor53c3f4e2010-12-20 22:48:17 +0000914
Douglas Gregora5cb6da2009-10-20 05:58:46 +0000915 if (TTP->getDepth() < TemplateArgs.getNumLevels()) {
Douglas Gregor53c3f4e2010-12-20 22:48:17 +0000916 // FIXME: This needs testing w/ member access expressions.
917 TemplateArgument Arg = TemplateArgs(TTP->getDepth(), TTP->getIndex());
918
919 if (TTP->isParameterPack()) {
920 assert(Arg.getKind() == TemplateArgument::Pack &&
921 "Missing argument pack");
922
Douglas Gregore1d60df2011-01-14 23:41:42 +0000923 if (getSema().ArgumentPackSubstitutionIndex == -1)
Douglas Gregor53c3f4e2010-12-20 22:48:17 +0000924 return 0;
Douglas Gregor53c3f4e2010-12-20 22:48:17 +0000925
Douglas Gregora8bac7f2011-01-10 07:32:04 +0000926 assert(getSema().ArgumentPackSubstitutionIndex < (int)Arg.pack_size());
Douglas Gregor53c3f4e2010-12-20 22:48:17 +0000927 Arg = Arg.pack_begin()[getSema().ArgumentPackSubstitutionIndex];
928 }
929
930 QualType T = Arg.getAsType();
Douglas Gregora5cb6da2009-10-20 05:58:46 +0000931 if (T.isNull())
Douglas Gregora04f2ca2010-03-01 15:56:25 +0000932 return cast_or_null<NamedDecl>(TransformDecl(Loc, D));
Douglas Gregora5cb6da2009-10-20 05:58:46 +0000933
934 if (const TagType *Tag = T->getAs<TagType>())
935 return Tag->getDecl();
936
937 // The resulting type is not a tag; complain.
938 getSema().Diag(Loc, diag::err_nested_name_spec_non_tag) << T;
939 return 0;
940 }
941 }
942
Douglas Gregora04f2ca2010-03-01 15:56:25 +0000943 return cast_or_null<NamedDecl>(TransformDecl(Loc, D));
Douglas Gregora5cb6da2009-10-20 05:58:46 +0000944}
945
Douglas Gregorebe10102009-08-20 07:17:43 +0000946VarDecl *
947TemplateInstantiator::RebuildExceptionDecl(VarDecl *ExceptionDecl,
John McCallbcd03502009-12-07 02:54:59 +0000948 TypeSourceInfo *Declarator,
Abramo Bagnaradff19302011-03-08 08:55:46 +0000949 SourceLocation StartLoc,
950 SourceLocation NameLoc,
951 IdentifierInfo *Name) {
Douglas Gregor9f0e1aa2010-09-09 17:09:21 +0000952 VarDecl *Var = inherited::RebuildExceptionDecl(ExceptionDecl, Declarator,
Abramo Bagnaradff19302011-03-08 08:55:46 +0000953 StartLoc, NameLoc, Name);
Douglas Gregorf4e837f2010-04-26 17:57:08 +0000954 if (Var)
955 getSema().CurrentInstantiationScope->InstantiatedLocal(ExceptionDecl, Var);
956 return Var;
957}
958
959VarDecl *TemplateInstantiator::RebuildObjCExceptionDecl(VarDecl *ExceptionDecl,
960 TypeSourceInfo *TSInfo,
961 QualType T) {
962 VarDecl *Var = inherited::RebuildObjCExceptionDecl(ExceptionDecl, TSInfo, T);
963 if (Var)
Douglas Gregorebe10102009-08-20 07:17:43 +0000964 getSema().CurrentInstantiationScope->InstantiatedLocal(ExceptionDecl, Var);
965 return Var;
966}
967
John McCall7f41d982009-09-11 04:59:25 +0000968QualType
John McCall954b5de2010-11-04 19:04:38 +0000969TemplateInstantiator::RebuildElaboratedType(SourceLocation KeywordLoc,
970 ElaboratedTypeKeyword Keyword,
Douglas Gregor844cb502011-03-01 18:12:44 +0000971 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara6150c882010-05-11 21:36:43 +0000972 QualType T) {
John McCall7f41d982009-09-11 04:59:25 +0000973 if (const TagType *TT = T->getAs<TagType>()) {
974 TagDecl* TD = TT->getDecl();
975
John McCall954b5de2010-11-04 19:04:38 +0000976 SourceLocation TagLocation = KeywordLoc;
John McCall7f41d982009-09-11 04:59:25 +0000977
978 // FIXME: type might be anonymous.
979 IdentifierInfo *Id = TD->getIdentifier();
980
981 // TODO: should we even warn on struct/class mismatches for this? Seems
982 // like it's likely to produce a lot of spurious errors.
Abramo Bagnara6150c882010-05-11 21:36:43 +0000983 if (Keyword != ETK_None && Keyword != ETK_Typename) {
984 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForKeyword(Keyword);
Richard Trieucaa33d32011-06-10 03:11:26 +0000985 if (!SemaRef.isAcceptableTagRedeclaration(TD, Kind, /*isDefinition*/false,
986 TagLocation, *Id)) {
Abramo Bagnara6150c882010-05-11 21:36:43 +0000987 SemaRef.Diag(TagLocation, diag::err_use_with_wrong_tag)
988 << Id
989 << FixItHint::CreateReplacement(SourceRange(TagLocation),
990 TD->getKindName());
991 SemaRef.Diag(TD->getLocation(), diag::note_previous_use);
992 }
John McCall7f41d982009-09-11 04:59:25 +0000993 }
994 }
995
John McCall954b5de2010-11-04 19:04:38 +0000996 return TreeTransform<TemplateInstantiator>::RebuildElaboratedType(KeywordLoc,
997 Keyword,
Douglas Gregor844cb502011-03-01 18:12:44 +0000998 QualifierLoc,
999 T);
John McCall7f41d982009-09-11 04:59:25 +00001000}
1001
Douglas Gregor9db53502011-03-02 18:07:45 +00001002TemplateName TemplateInstantiator::TransformTemplateName(CXXScopeSpec &SS,
1003 TemplateName Name,
1004 SourceLocation NameLoc,
1005 QualType ObjectType,
1006 NamedDecl *FirstQualifierInScope) {
1007 if (TemplateTemplateParmDecl *TTP
1008 = dyn_cast_or_null<TemplateTemplateParmDecl>(Name.getAsTemplateDecl())) {
1009 if (TTP->getDepth() < TemplateArgs.getNumLevels()) {
1010 // If the corresponding template argument is NULL or non-existent, it's
1011 // because we are performing instantiation from explicitly-specified
1012 // template arguments in a function template, but there were some
1013 // arguments left unspecified.
1014 if (!TemplateArgs.hasTemplateArgument(TTP->getDepth(),
1015 TTP->getPosition()))
1016 return Name;
1017
1018 TemplateArgument Arg = TemplateArgs(TTP->getDepth(), TTP->getPosition());
1019
1020 if (TTP->isParameterPack()) {
1021 assert(Arg.getKind() == TemplateArgument::Pack &&
1022 "Missing argument pack");
1023
1024 if (getSema().ArgumentPackSubstitutionIndex == -1) {
1025 // We have the template argument pack to substitute, but we're not
1026 // actually expanding the enclosing pack expansion yet. So, just
1027 // keep the entire argument pack.
1028 return getSema().Context.getSubstTemplateTemplateParmPack(TTP, Arg);
1029 }
1030
1031 assert(getSema().ArgumentPackSubstitutionIndex < (int)Arg.pack_size());
1032 Arg = Arg.pack_begin()[getSema().ArgumentPackSubstitutionIndex];
1033 }
1034
1035 TemplateName Template = Arg.getAsTemplate();
Richard Smith3f1b5d02011-05-05 21:57:07 +00001036 assert(!Template.isNull() && "Null template template argument");
John McCalld9dfe3a2011-06-30 08:33:18 +00001037
Douglas Gregor9d9f8db2011-03-05 20:06:51 +00001038 // We don't ever want to substitute for a qualified template name, since
1039 // the qualifier is handled separately. So, look through the qualified
1040 // template name to its underlying declaration.
1041 if (QualifiedTemplateName *QTN = Template.getAsQualifiedTemplateName())
1042 Template = TemplateName(QTN->getTemplateDecl());
John McCalld9dfe3a2011-06-30 08:33:18 +00001043
1044 Template = getSema().Context.getSubstTemplateTemplateParm(TTP, Template);
Douglas Gregor9db53502011-03-02 18:07:45 +00001045 return Template;
1046 }
1047 }
1048
1049 if (SubstTemplateTemplateParmPackStorage *SubstPack
1050 = Name.getAsSubstTemplateTemplateParmPack()) {
1051 if (getSema().ArgumentPackSubstitutionIndex == -1)
1052 return Name;
1053
1054 const TemplateArgument &ArgPack = SubstPack->getArgumentPack();
1055 assert(getSema().ArgumentPackSubstitutionIndex < (int)ArgPack.pack_size() &&
1056 "Pack substitution index out-of-range");
1057 return ArgPack.pack_begin()[getSema().ArgumentPackSubstitutionIndex]
1058 .getAsTemplate();
1059 }
1060
1061 return inherited::TransformTemplateName(SS, Name, NameLoc, ObjectType,
1062 FirstQualifierInScope);
1063}
1064
John McCalldadc5752010-08-24 06:29:42 +00001065ExprResult
John McCall47f29ea2009-12-08 09:21:05 +00001066TemplateInstantiator::TransformPredefinedExpr(PredefinedExpr *E) {
Anders Carlsson0b209a82009-09-11 01:22:35 +00001067 if (!E->isTypeDependent())
John McCallc3007a22010-10-26 07:05:15 +00001068 return SemaRef.Owned(E);
Anders Carlsson0b209a82009-09-11 01:22:35 +00001069
1070 FunctionDecl *currentDecl = getSema().getCurFunctionDecl();
1071 assert(currentDecl && "Must have current function declaration when "
1072 "instantiating.");
1073
1074 PredefinedExpr::IdentType IT = E->getIdentType();
1075
Anders Carlsson5bd8d192010-02-11 18:20:28 +00001076 unsigned Length = PredefinedExpr::ComputeName(IT, currentDecl).length();
Anders Carlsson0b209a82009-09-11 01:22:35 +00001077
1078 llvm::APInt LengthI(32, Length + 1);
Nico Weber606cef42012-06-25 22:34:48 +00001079 QualType ResTy;
1080 if (IT == PredefinedExpr::LFunction)
1081 ResTy = getSema().Context.WCharTy.withConst();
1082 else
1083 ResTy = getSema().Context.CharTy.withConst();
Anders Carlsson0b209a82009-09-11 01:22:35 +00001084 ResTy = getSema().Context.getConstantArrayType(ResTy, LengthI,
1085 ArrayType::Normal, 0);
1086 PredefinedExpr *PE =
1087 new (getSema().Context) PredefinedExpr(E->getLocation(), ResTy, IT);
1088 return getSema().Owned(PE);
1089}
1090
John McCalldadc5752010-08-24 06:29:42 +00001091ExprResult
John McCall13481c52010-02-06 08:42:39 +00001092TemplateInstantiator::TransformTemplateParmRefExpr(DeclRefExpr *E,
Douglas Gregor6c379e22010-02-08 23:41:45 +00001093 NonTypeTemplateParmDecl *NTTP) {
John McCall13481c52010-02-06 08:42:39 +00001094 // If the corresponding template argument is NULL or non-existent, it's
1095 // because we are performing instantiation from explicitly-specified
1096 // template arguments in a function template, but there were some
1097 // arguments left unspecified.
1098 if (!TemplateArgs.hasTemplateArgument(NTTP->getDepth(),
1099 NTTP->getPosition()))
John McCallc3007a22010-10-26 07:05:15 +00001100 return SemaRef.Owned(E);
Mike Stump11289f42009-09-09 15:08:12 +00001101
Douglas Gregoreb5a39d2010-12-24 00:15:10 +00001102 TemplateArgument Arg = TemplateArgs(NTTP->getDepth(), NTTP->getPosition());
1103 if (NTTP->isParameterPack()) {
1104 assert(Arg.getKind() == TemplateArgument::Pack &&
1105 "Missing argument pack");
1106
1107 if (getSema().ArgumentPackSubstitutionIndex == -1) {
Douglas Gregorcdbc5392011-01-15 01:15:58 +00001108 // We have an argument pack, but we can't select a particular argument
1109 // out of it yet. Therefore, we'll build an expression to hold on to that
1110 // argument pack.
1111 QualType TargetType = SemaRef.SubstType(NTTP->getType(), TemplateArgs,
1112 E->getLocation(),
1113 NTTP->getDeclName());
1114 if (TargetType.isNull())
1115 return ExprError();
1116
1117 return new (SemaRef.Context) SubstNonTypeTemplateParmPackExpr(TargetType,
1118 NTTP,
1119 E->getLocation(),
1120 Arg);
Douglas Gregoreb5a39d2010-12-24 00:15:10 +00001121 }
1122
Douglas Gregora8bac7f2011-01-10 07:32:04 +00001123 assert(getSema().ArgumentPackSubstitutionIndex < (int)Arg.pack_size());
Douglas Gregoreb5a39d2010-12-24 00:15:10 +00001124 Arg = Arg.pack_begin()[getSema().ArgumentPackSubstitutionIndex];
1125 }
Mike Stump11289f42009-09-09 15:08:12 +00001126
John McCall7c454bb2011-07-15 05:09:51 +00001127 return transformNonTypeTemplateParmRef(NTTP, E->getLocation(), Arg);
1128}
1129
1130ExprResult TemplateInstantiator::transformNonTypeTemplateParmRef(
1131 NonTypeTemplateParmDecl *parm,
1132 SourceLocation loc,
Richard Smith34349002012-07-09 03:07:20 +00001133 TemplateArgument arg) {
John McCall7c454bb2011-07-15 05:09:51 +00001134 ExprResult result;
1135 QualType type;
1136
Richard Smith34349002012-07-09 03:07:20 +00001137 // If the argument is a pack expansion, the parameter must actually be a
1138 // parameter pack, and we should substitute the pattern itself, producing
1139 // an expression which contains an unexpanded parameter pack.
1140 if (arg.isPackExpansion()) {
1141 assert(parm->isParameterPack() && "pack expansion for non-pack");
1142 arg = arg.getPackExpansionPattern();
1143 }
1144
John McCall13481c52010-02-06 08:42:39 +00001145 // The template argument itself might be an expression, in which
1146 // case we just return that expression.
John McCall7c454bb2011-07-15 05:09:51 +00001147 if (arg.getKind() == TemplateArgument::Expression) {
1148 Expr *argExpr = arg.getAsExpr();
1149 result = SemaRef.Owned(argExpr);
1150 type = argExpr->getType();
Mike Stump11289f42009-09-09 15:08:12 +00001151
John McCall7c454bb2011-07-15 05:09:51 +00001152 } else if (arg.getKind() == TemplateArgument::Declaration) {
Douglas Gregor31f55dc2012-04-06 22:40:38 +00001153 ValueDecl *VD;
1154 if (Decl *D = arg.getAsDecl()) {
1155 VD = cast<ValueDecl>(D);
Mike Stump11289f42009-09-09 15:08:12 +00001156
Douglas Gregor31f55dc2012-04-06 22:40:38 +00001157 // Find the instantiation of the template argument. This is
1158 // required for nested templates.
1159 VD = cast_or_null<ValueDecl>(
1160 getSema().FindInstantiatedDecl(loc, VD, TemplateArgs));
1161 if (!VD)
1162 return ExprError();
1163 } else {
1164 // Propagate NULL template argument.
1165 VD = 0;
1166 }
1167
John McCall15dda372010-02-06 10:23:53 +00001168 // Derive the type we want the substituted decl to have. This had
1169 // better be non-dependent, or these checks will have serious problems.
John McCall7c454bb2011-07-15 05:09:51 +00001170 if (parm->isExpandedParameterPack()) {
1171 type = parm->getExpansionType(SemaRef.ArgumentPackSubstitutionIndex);
1172 } else if (parm->isParameterPack() &&
1173 isa<PackExpansionType>(parm->getType())) {
1174 type = SemaRef.SubstType(
1175 cast<PackExpansionType>(parm->getType())->getPattern(),
1176 TemplateArgs, loc, parm->getDeclName());
1177 } else {
1178 type = SemaRef.SubstType(parm->getType(), TemplateArgs,
1179 loc, parm->getDeclName());
1180 }
1181 assert(!type.isNull() && "type substitution failed for param type");
1182 assert(!type->isDependentType() && "param type still dependent");
1183 result = SemaRef.BuildExpressionFromDeclTemplateArgument(arg, type, loc);
John McCall13481c52010-02-06 08:42:39 +00001184
John McCall7c454bb2011-07-15 05:09:51 +00001185 if (!result.isInvalid()) type = result.get()->getType();
1186 } else {
1187 result = SemaRef.BuildExpressionFromIntegralTemplateArgument(arg, loc);
1188
1189 // Note that this type can be different from the type of 'result',
1190 // e.g. if it's an enum type.
1191 type = arg.getIntegralType();
1192 }
1193 if (result.isInvalid()) return ExprError();
1194
1195 Expr *resultExpr = result.take();
1196 return SemaRef.Owned(new (SemaRef.Context)
1197 SubstNonTypeTemplateParmExpr(type,
1198 resultExpr->getValueKind(),
1199 loc, parm, resultExpr));
John McCall13481c52010-02-06 08:42:39 +00001200}
1201
Douglas Gregorcdbc5392011-01-15 01:15:58 +00001202ExprResult
1203TemplateInstantiator::TransformSubstNonTypeTemplateParmPackExpr(
1204 SubstNonTypeTemplateParmPackExpr *E) {
1205 if (getSema().ArgumentPackSubstitutionIndex == -1) {
1206 // We aren't expanding the parameter pack, so just return ourselves.
1207 return getSema().Owned(E);
1208 }
1209
Douglas Gregorcdbc5392011-01-15 01:15:58 +00001210 const TemplateArgument &ArgPack = E->getArgumentPack();
1211 unsigned Index = (unsigned)getSema().ArgumentPackSubstitutionIndex;
1212 assert(Index < ArgPack.pack_size() && "Substitution index out-of-range");
1213
1214 const TemplateArgument &Arg = ArgPack.pack_begin()[Index];
John McCall7c454bb2011-07-15 05:09:51 +00001215 return transformNonTypeTemplateParmRef(E->getParameterPack(),
1216 E->getParameterPackLocation(),
1217 Arg);
Douglas Gregorcdbc5392011-01-15 01:15:58 +00001218}
John McCall13481c52010-02-06 08:42:39 +00001219
John McCalldadc5752010-08-24 06:29:42 +00001220ExprResult
John McCall13481c52010-02-06 08:42:39 +00001221TemplateInstantiator::TransformDeclRefExpr(DeclRefExpr *E) {
1222 NamedDecl *D = E->getDecl();
1223 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(D)) {
1224 if (NTTP->getDepth() < TemplateArgs.getNumLevels())
1225 return TransformTemplateParmRefExpr(E, NTTP);
Douglas Gregor954de172009-10-31 17:21:17 +00001226
1227 // We have a non-type template parameter that isn't fully substituted;
1228 // FindInstantiatedDecl will find it in the local instantiation scope.
Douglas Gregora16548e2009-08-11 05:31:07 +00001229 }
Mike Stump11289f42009-09-09 15:08:12 +00001230
John McCall47f29ea2009-12-08 09:21:05 +00001231 return TreeTransform<TemplateInstantiator>::TransformDeclRefExpr(E);
Douglas Gregora16548e2009-08-11 05:31:07 +00001232}
1233
John McCalldadc5752010-08-24 06:29:42 +00001234ExprResult TemplateInstantiator::TransformCXXDefaultArgExpr(
John McCall47f29ea2009-12-08 09:21:05 +00001235 CXXDefaultArgExpr *E) {
Sebastian Redl14236c82009-11-08 13:56:19 +00001236 assert(!cast<FunctionDecl>(E->getParam()->getDeclContext())->
1237 getDescribedFunctionTemplate() &&
1238 "Default arg expressions are never formed in dependent cases.");
Douglas Gregor033f6752009-12-23 23:03:06 +00001239 return SemaRef.BuildCXXDefaultArgExpr(E->getUsedLocation(),
1240 cast<FunctionDecl>(E->getParam()->getDeclContext()),
1241 E->getParam());
Sebastian Redl14236c82009-11-08 13:56:19 +00001242}
1243
Douglas Gregor14cf7522010-04-30 18:55:50 +00001244QualType TemplateInstantiator::TransformFunctionProtoType(TypeLocBuilder &TLB,
John McCall31f82722010-11-12 08:19:04 +00001245 FunctionProtoTypeLoc TL) {
Douglas Gregor14cf7522010-04-30 18:55:50 +00001246 // We need a local instantiation scope for this function prototype.
John McCall19c1bfd2010-08-25 05:32:35 +00001247 LocalInstantiationScope Scope(SemaRef, /*CombineWithOuterScope=*/true);
John McCall31f82722010-11-12 08:19:04 +00001248 return inherited::TransformFunctionProtoType(TLB, TL);
John McCall58f10c32010-03-11 09:03:00 +00001249}
1250
Douglas Gregor3024f072012-04-16 07:05:22 +00001251QualType TemplateInstantiator::TransformFunctionProtoType(TypeLocBuilder &TLB,
1252 FunctionProtoTypeLoc TL,
1253 CXXRecordDecl *ThisContext,
1254 unsigned ThisTypeQuals) {
1255 // We need a local instantiation scope for this function prototype.
1256 LocalInstantiationScope Scope(SemaRef, /*CombineWithOuterScope=*/true);
1257 return inherited::TransformFunctionProtoType(TLB, TL, ThisContext,
1258 ThisTypeQuals);
1259}
1260
John McCall58f10c32010-03-11 09:03:00 +00001261ParmVarDecl *
Douglas Gregor715e4612011-01-14 22:40:04 +00001262TemplateInstantiator::TransformFunctionTypeParam(ParmVarDecl *OldParm,
John McCall8fb0d9d2011-05-01 22:35:37 +00001263 int indexAdjustment,
Douglas Gregor0dd22bc2012-01-25 16:15:54 +00001264 llvm::Optional<unsigned> NumExpansions,
1265 bool ExpectParameterPack) {
John McCall8fb0d9d2011-05-01 22:35:37 +00001266 return SemaRef.SubstParmVarDecl(OldParm, TemplateArgs, indexAdjustment,
Douglas Gregor0dd22bc2012-01-25 16:15:54 +00001267 NumExpansions, ExpectParameterPack);
John McCall58f10c32010-03-11 09:03:00 +00001268}
1269
Mike Stump11289f42009-09-09 15:08:12 +00001270QualType
John McCall550e0c22009-10-21 00:40:46 +00001271TemplateInstantiator::TransformTemplateTypeParmType(TypeLocBuilder &TLB,
John McCall31f82722010-11-12 08:19:04 +00001272 TemplateTypeParmTypeLoc TL) {
John McCall424cec92011-01-19 06:33:43 +00001273 const TemplateTypeParmType *T = TL.getTypePtr();
Douglas Gregor01afeef2009-08-28 20:31:08 +00001274 if (T->getDepth() < TemplateArgs.getNumLevels()) {
Douglas Gregorfe1e1102009-02-27 19:31:52 +00001275 // Replace the template type parameter with its corresponding
1276 // template argument.
Mike Stump11289f42009-09-09 15:08:12 +00001277
1278 // If the corresponding template argument is NULL or doesn't exist, it's
1279 // because we are performing instantiation from explicitly-specified
1280 // template arguments in a function template class, but there were some
Douglas Gregore3f1f352009-07-01 00:28:38 +00001281 // arguments left unspecified.
John McCall550e0c22009-10-21 00:40:46 +00001282 if (!TemplateArgs.hasTemplateArgument(T->getDepth(), T->getIndex())) {
1283 TemplateTypeParmTypeLoc NewTL
1284 = TLB.push<TemplateTypeParmTypeLoc>(TL.getType());
1285 NewTL.setNameLoc(TL.getNameLoc());
1286 return TL.getType();
1287 }
Mike Stump11289f42009-09-09 15:08:12 +00001288
Douglas Gregor840bd6c2010-12-20 22:05:00 +00001289 TemplateArgument Arg = TemplateArgs(T->getDepth(), T->getIndex());
1290
1291 if (T->isParameterPack()) {
1292 assert(Arg.getKind() == TemplateArgument::Pack &&
1293 "Missing argument pack");
1294
1295 if (getSema().ArgumentPackSubstitutionIndex == -1) {
Douglas Gregorada4b792011-01-14 02:55:32 +00001296 // We have the template argument pack, but we're not expanding the
1297 // enclosing pack expansion yet. Just save the template argument
1298 // pack for later substitution.
1299 QualType Result
1300 = getSema().Context.getSubstTemplateTypeParmPackType(T, Arg);
1301 SubstTemplateTypeParmPackTypeLoc NewTL
1302 = TLB.push<SubstTemplateTypeParmPackTypeLoc>(Result);
1303 NewTL.setNameLoc(TL.getNameLoc());
1304 return Result;
Douglas Gregor840bd6c2010-12-20 22:05:00 +00001305 }
1306
Douglas Gregora8bac7f2011-01-10 07:32:04 +00001307 assert(getSema().ArgumentPackSubstitutionIndex < (int)Arg.pack_size());
Douglas Gregor840bd6c2010-12-20 22:05:00 +00001308 Arg = Arg.pack_begin()[getSema().ArgumentPackSubstitutionIndex];
1309 }
1310
1311 assert(Arg.getKind() == TemplateArgument::Type &&
Douglas Gregorfe1e1102009-02-27 19:31:52 +00001312 "Template argument kind mismatch");
Douglas Gregor01afeef2009-08-28 20:31:08 +00001313
Douglas Gregor840bd6c2010-12-20 22:05:00 +00001314 QualType Replacement = Arg.getAsType();
John McCallcebee162009-10-18 09:09:24 +00001315
1316 // TODO: only do this uniquing once, at the start of instantiation.
John McCall550e0c22009-10-21 00:40:46 +00001317 QualType Result
1318 = getSema().Context.getSubstTemplateTypeParmType(T, Replacement);
1319 SubstTemplateTypeParmTypeLoc NewTL
1320 = TLB.push<SubstTemplateTypeParmTypeLoc>(Result);
1321 NewTL.setNameLoc(TL.getNameLoc());
1322 return Result;
Mike Stump11289f42009-09-09 15:08:12 +00001323 }
Douglas Gregorfe1e1102009-02-27 19:31:52 +00001324
1325 // The template type parameter comes from an inner template (e.g.,
1326 // the template parameter list of a member template inside the
1327 // template we are instantiating). Create a new template type
1328 // parameter with the template "level" reduced by one.
Chandler Carruth08836322011-05-01 00:51:33 +00001329 TemplateTypeParmDecl *NewTTPDecl = 0;
1330 if (TemplateTypeParmDecl *OldTTPDecl = T->getDecl())
1331 NewTTPDecl = cast_or_null<TemplateTypeParmDecl>(
1332 TransformDecl(TL.getNameLoc(), OldTTPDecl));
1333
John McCall550e0c22009-10-21 00:40:46 +00001334 QualType Result
1335 = getSema().Context.getTemplateTypeParmType(T->getDepth()
1336 - TemplateArgs.getNumLevels(),
1337 T->getIndex(),
1338 T->isParameterPack(),
Chandler Carruth08836322011-05-01 00:51:33 +00001339 NewTTPDecl);
John McCall550e0c22009-10-21 00:40:46 +00001340 TemplateTypeParmTypeLoc NewTL = TLB.push<TemplateTypeParmTypeLoc>(Result);
1341 NewTL.setNameLoc(TL.getNameLoc());
1342 return Result;
Douglas Gregor17c0d7b2009-02-28 00:25:32 +00001343}
Douglas Gregorfe1e1102009-02-27 19:31:52 +00001344
Douglas Gregorada4b792011-01-14 02:55:32 +00001345QualType
1346TemplateInstantiator::TransformSubstTemplateTypeParmPackType(
1347 TypeLocBuilder &TLB,
1348 SubstTemplateTypeParmPackTypeLoc TL) {
1349 if (getSema().ArgumentPackSubstitutionIndex == -1) {
1350 // We aren't expanding the parameter pack, so just return ourselves.
1351 SubstTemplateTypeParmPackTypeLoc NewTL
1352 = TLB.push<SubstTemplateTypeParmPackTypeLoc>(TL.getType());
1353 NewTL.setNameLoc(TL.getNameLoc());
1354 return TL.getType();
1355 }
1356
1357 const TemplateArgument &ArgPack = TL.getTypePtr()->getArgumentPack();
1358 unsigned Index = (unsigned)getSema().ArgumentPackSubstitutionIndex;
1359 assert(Index < ArgPack.pack_size() && "Substitution index out-of-range");
1360
1361 QualType Result = ArgPack.pack_begin()[Index].getAsType();
1362 Result = getSema().Context.getSubstTemplateTypeParmType(
1363 TL.getTypePtr()->getReplacedParameter(),
1364 Result);
1365 SubstTemplateTypeParmTypeLoc NewTL
1366 = TLB.push<SubstTemplateTypeParmTypeLoc>(Result);
1367 NewTL.setNameLoc(TL.getNameLoc());
1368 return Result;
1369}
1370
John McCall76d824f2009-08-25 22:02:44 +00001371/// \brief Perform substitution on the type T with a given set of template
1372/// arguments.
Douglas Gregorfe1e1102009-02-27 19:31:52 +00001373///
1374/// This routine substitutes the given template arguments into the
1375/// type T and produces the instantiated type.
1376///
1377/// \param T the type into which the template arguments will be
1378/// substituted. If this type is not dependent, it will be returned
1379/// immediately.
1380///
James Dennett634962f2012-06-14 21:40:34 +00001381/// \param Args the template arguments that will be
Douglas Gregorfe1e1102009-02-27 19:31:52 +00001382/// substituted for the top-level template parameters within T.
1383///
Douglas Gregorfe1e1102009-02-27 19:31:52 +00001384/// \param Loc the location in the source code where this substitution
1385/// is being performed. It will typically be the location of the
1386/// declarator (if we're instantiating the type of some declaration)
1387/// or the location of the type in the source code (if, e.g., we're
1388/// instantiating the type of a cast expression).
1389///
1390/// \param Entity the name of the entity associated with a declaration
1391/// being instantiated (if any). May be empty to indicate that there
1392/// is no such entity (if, e.g., this is a type that occurs as part of
1393/// a cast expression) or that the entity has no name (e.g., an
1394/// unnamed function parameter).
1395///
1396/// \returns If the instantiation succeeds, the instantiated
1397/// type. Otherwise, produces diagnostics and returns a NULL type.
John McCallbcd03502009-12-07 02:54:59 +00001398TypeSourceInfo *Sema::SubstType(TypeSourceInfo *T,
John McCall609459e2009-10-21 00:58:09 +00001399 const MultiLevelTemplateArgumentList &Args,
1400 SourceLocation Loc,
1401 DeclarationName Entity) {
1402 assert(!ActiveTemplateInstantiations.empty() &&
1403 "Cannot perform an instantiation without some context on the "
1404 "instantiation stack");
1405
Douglas Gregor678d76c2011-07-01 01:22:09 +00001406 if (!T->getType()->isInstantiationDependentType() &&
Douglas Gregor5a5073e2010-05-24 17:22:01 +00001407 !T->getType()->isVariablyModifiedType())
John McCall609459e2009-10-21 00:58:09 +00001408 return T;
1409
1410 TemplateInstantiator Instantiator(*this, Args, Loc, Entity);
1411 return Instantiator.TransformType(T);
1412}
1413
Douglas Gregor5499af42011-01-05 23:12:31 +00001414TypeSourceInfo *Sema::SubstType(TypeLoc TL,
1415 const MultiLevelTemplateArgumentList &Args,
1416 SourceLocation Loc,
1417 DeclarationName Entity) {
1418 assert(!ActiveTemplateInstantiations.empty() &&
1419 "Cannot perform an instantiation without some context on the "
1420 "instantiation stack");
1421
1422 if (TL.getType().isNull())
1423 return 0;
1424
Douglas Gregor678d76c2011-07-01 01:22:09 +00001425 if (!TL.getType()->isInstantiationDependentType() &&
Douglas Gregor5499af42011-01-05 23:12:31 +00001426 !TL.getType()->isVariablyModifiedType()) {
1427 // FIXME: Make a copy of the TypeLoc data here, so that we can
1428 // return a new TypeSourceInfo. Inefficient!
1429 TypeLocBuilder TLB;
1430 TLB.pushFullCopy(TL);
1431 return TLB.getTypeSourceInfo(Context, TL.getType());
1432 }
1433
1434 TemplateInstantiator Instantiator(*this, Args, Loc, Entity);
1435 TypeLocBuilder TLB;
1436 TLB.reserve(TL.getFullDataSize());
1437 QualType Result = Instantiator.TransformType(TLB, TL);
1438 if (Result.isNull())
1439 return 0;
1440
1441 return TLB.getTypeSourceInfo(Context, Result);
1442}
1443
John McCall609459e2009-10-21 00:58:09 +00001444/// Deprecated form of the above.
Mike Stump11289f42009-09-09 15:08:12 +00001445QualType Sema::SubstType(QualType T,
Douglas Gregor01afeef2009-08-28 20:31:08 +00001446 const MultiLevelTemplateArgumentList &TemplateArgs,
John McCall76d824f2009-08-25 22:02:44 +00001447 SourceLocation Loc, DeclarationName Entity) {
Douglas Gregor79cf6032009-03-10 20:44:00 +00001448 assert(!ActiveTemplateInstantiations.empty() &&
1449 "Cannot perform an instantiation without some context on the "
1450 "instantiation stack");
1451
Douglas Gregor5a5073e2010-05-24 17:22:01 +00001452 // If T is not a dependent type or a variably-modified type, there
1453 // is nothing to do.
Douglas Gregor678d76c2011-07-01 01:22:09 +00001454 if (!T->isInstantiationDependentType() && !T->isVariablyModifiedType())
Douglas Gregorfe1e1102009-02-27 19:31:52 +00001455 return T;
1456
Douglas Gregord6ff3322009-08-04 16:50:30 +00001457 TemplateInstantiator Instantiator(*this, TemplateArgs, Loc, Entity);
1458 return Instantiator.TransformType(T);
Douglas Gregorfe1e1102009-02-27 19:31:52 +00001459}
Douglas Gregor463421d2009-03-03 04:44:36 +00001460
John McCallb29f78f2010-04-09 17:38:44 +00001461static bool NeedsInstantiationAsFunctionType(TypeSourceInfo *T) {
Douglas Gregor678d76c2011-07-01 01:22:09 +00001462 if (T->getType()->isInstantiationDependentType() ||
1463 T->getType()->isVariablyModifiedType())
John McCallb29f78f2010-04-09 17:38:44 +00001464 return true;
1465
Abramo Bagnara6d810632010-12-14 22:11:44 +00001466 TypeLoc TL = T->getTypeLoc().IgnoreParens();
John McCallb29f78f2010-04-09 17:38:44 +00001467 if (!isa<FunctionProtoTypeLoc>(TL))
1468 return false;
1469
1470 FunctionProtoTypeLoc FP = cast<FunctionProtoTypeLoc>(TL);
1471 for (unsigned I = 0, E = FP.getNumArgs(); I != E; ++I) {
1472 ParmVarDecl *P = FP.getArg(I);
1473
Douglas Gregora7203e52011-05-09 20:45:16 +00001474 // The parameter's type as written might be dependent even if the
1475 // decayed type was not dependent.
1476 if (TypeSourceInfo *TSInfo = P->getTypeSourceInfo())
Douglas Gregor678d76c2011-07-01 01:22:09 +00001477 if (TSInfo->getType()->isInstantiationDependentType())
Douglas Gregora7203e52011-05-09 20:45:16 +00001478 return true;
1479
John McCallb29f78f2010-04-09 17:38:44 +00001480 // TODO: currently we always rebuild expressions. When we
1481 // properly get lazier about this, we should use the same
1482 // logic to avoid rebuilding prototypes here.
Douglas Gregor9cc278222011-01-05 21:14:17 +00001483 if (P->hasDefaultArg())
John McCallb29f78f2010-04-09 17:38:44 +00001484 return true;
1485 }
1486
1487 return false;
1488}
1489
1490/// A form of SubstType intended specifically for instantiating the
1491/// type of a FunctionDecl. Its purpose is solely to force the
1492/// instantiation of default-argument expressions.
1493TypeSourceInfo *Sema::SubstFunctionDeclType(TypeSourceInfo *T,
1494 const MultiLevelTemplateArgumentList &Args,
1495 SourceLocation Loc,
Douglas Gregor3024f072012-04-16 07:05:22 +00001496 DeclarationName Entity,
1497 CXXRecordDecl *ThisContext,
1498 unsigned ThisTypeQuals) {
John McCallb29f78f2010-04-09 17:38:44 +00001499 assert(!ActiveTemplateInstantiations.empty() &&
1500 "Cannot perform an instantiation without some context on the "
1501 "instantiation stack");
1502
1503 if (!NeedsInstantiationAsFunctionType(T))
1504 return T;
1505
1506 TemplateInstantiator Instantiator(*this, Args, Loc, Entity);
1507
1508 TypeLocBuilder TLB;
1509
1510 TypeLoc TL = T->getTypeLoc();
1511 TLB.reserve(TL.getFullDataSize());
1512
Douglas Gregor3024f072012-04-16 07:05:22 +00001513 QualType Result;
1514
1515 if (FunctionProtoTypeLoc *Proto = dyn_cast<FunctionProtoTypeLoc>(&TL)) {
1516 Result = Instantiator.TransformFunctionProtoType(TLB, *Proto, ThisContext,
1517 ThisTypeQuals);
1518 } else {
1519 Result = Instantiator.TransformType(TLB, TL);
1520 }
John McCallb29f78f2010-04-09 17:38:44 +00001521 if (Result.isNull())
1522 return 0;
1523
1524 return TLB.getTypeSourceInfo(Context, Result);
1525}
1526
Douglas Gregor940bca72010-04-12 07:48:19 +00001527ParmVarDecl *Sema::SubstParmVarDecl(ParmVarDecl *OldParm,
Douglas Gregor715e4612011-01-14 22:40:04 +00001528 const MultiLevelTemplateArgumentList &TemplateArgs,
John McCall8fb0d9d2011-05-01 22:35:37 +00001529 int indexAdjustment,
Douglas Gregor0dd22bc2012-01-25 16:15:54 +00001530 llvm::Optional<unsigned> NumExpansions,
1531 bool ExpectParameterPack) {
Douglas Gregor940bca72010-04-12 07:48:19 +00001532 TypeSourceInfo *OldDI = OldParm->getTypeSourceInfo();
Douglas Gregor5499af42011-01-05 23:12:31 +00001533 TypeSourceInfo *NewDI = 0;
1534
Douglas Gregor5499af42011-01-05 23:12:31 +00001535 TypeLoc OldTL = OldDI->getTypeLoc();
1536 if (isa<PackExpansionTypeLoc>(OldTL)) {
1537 PackExpansionTypeLoc ExpansionTL = cast<PackExpansionTypeLoc>(OldTL);
Douglas Gregor5499af42011-01-05 23:12:31 +00001538
1539 // We have a function parameter pack. Substitute into the pattern of the
1540 // expansion.
1541 NewDI = SubstType(ExpansionTL.getPatternLoc(), TemplateArgs,
1542 OldParm->getLocation(), OldParm->getDeclName());
1543 if (!NewDI)
1544 return 0;
1545
1546 if (NewDI->getType()->containsUnexpandedParameterPack()) {
1547 // We still have unexpanded parameter packs, which means that
1548 // our function parameter is still a function parameter pack.
1549 // Therefore, make its type a pack expansion type.
Douglas Gregor0dca5fd2011-01-14 17:04:44 +00001550 NewDI = CheckPackExpansion(NewDI, ExpansionTL.getEllipsisLoc(),
Douglas Gregor715e4612011-01-14 22:40:04 +00001551 NumExpansions);
Douglas Gregor0dd22bc2012-01-25 16:15:54 +00001552 } else if (ExpectParameterPack) {
1553 // We expected to get a parameter pack but didn't (because the type
1554 // itself is not a pack expansion type), so complain. This can occur when
1555 // the substitution goes through an alias template that "loses" the
1556 // pack expansion.
1557 Diag(OldParm->getLocation(),
1558 diag::err_function_parameter_pack_without_parameter_packs)
1559 << NewDI->getType();
1560 return 0;
1561 }
Douglas Gregor5499af42011-01-05 23:12:31 +00001562 } else {
1563 NewDI = SubstType(OldDI, TemplateArgs, OldParm->getLocation(),
1564 OldParm->getDeclName());
1565 }
1566
Douglas Gregor940bca72010-04-12 07:48:19 +00001567 if (!NewDI)
1568 return 0;
1569
1570 if (NewDI->getType()->isVoidType()) {
1571 Diag(OldParm->getLocation(), diag::err_param_with_void_type);
1572 return 0;
1573 }
1574
1575 ParmVarDecl *NewParm = CheckParameter(Context.getTranslationUnitDecl(),
Abramo Bagnaradff19302011-03-08 08:55:46 +00001576 OldParm->getInnerLocStart(),
Douglas Gregor940bca72010-04-12 07:48:19 +00001577 OldParm->getLocation(),
Abramo Bagnaradff19302011-03-08 08:55:46 +00001578 OldParm->getIdentifier(),
1579 NewDI->getType(), NewDI,
Douglas Gregorc4df4072010-04-19 22:54:31 +00001580 OldParm->getStorageClass(),
1581 OldParm->getStorageClassAsWritten());
Douglas Gregor940bca72010-04-12 07:48:19 +00001582 if (!NewParm)
1583 return 0;
Douglas Gregor6044d692010-05-19 17:02:24 +00001584
Douglas Gregor940bca72010-04-12 07:48:19 +00001585 // Mark the (new) default argument as uninstantiated (if any).
1586 if (OldParm->hasUninstantiatedDefaultArg()) {
1587 Expr *Arg = OldParm->getUninstantiatedDefaultArg();
1588 NewParm->setUninstantiatedDefaultArg(Arg);
Douglas Gregor758cb672010-10-12 18:23:32 +00001589 } else if (OldParm->hasUnparsedDefaultArg()) {
1590 NewParm->setUnparsedDefaultArg();
1591 UnparsedDefaultArgInstantiations[OldParm].push_back(NewParm);
David Blaikie7afed5e2012-05-01 06:05:57 +00001592 } else if (Expr *Arg = OldParm->getDefaultArg())
1593 // FIXME: if we non-lazily instantiated non-dependent default args for
1594 // non-dependent parameter types we could remove a bunch of duplicate
1595 // conversion warnings for such arguments.
1596 NewParm->setUninstantiatedDefaultArg(Arg);
Douglas Gregor940bca72010-04-12 07:48:19 +00001597
1598 NewParm->setHasInheritedDefaultArg(OldParm->hasInheritedDefaultArg());
Douglas Gregor0dd22bc2012-01-25 16:15:54 +00001599
Douglas Gregorf3010112011-01-07 16:43:16 +00001600 if (OldParm->isParameterPack() && !NewParm->isParameterPack()) {
Richard Smith928be492012-01-25 02:14:59 +00001601 // Add the new parameter to the instantiated parameter pack.
Douglas Gregorf3010112011-01-07 16:43:16 +00001602 CurrentInstantiationScope->InstantiatedLocalPackArg(OldParm, NewParm);
1603 } else {
1604 // Introduce an Old -> New mapping
Douglas Gregor5499af42011-01-05 23:12:31 +00001605 CurrentInstantiationScope->InstantiatedLocal(OldParm, NewParm);
Douglas Gregorf3010112011-01-07 16:43:16 +00001606 }
Douglas Gregor5499af42011-01-05 23:12:31 +00001607
Argyrios Kyrtzidis3816ed42010-07-19 10:14:41 +00001608 // FIXME: OldParm may come from a FunctionProtoType, in which case CurContext
1609 // can be anything, is this right ?
Fariborz Jahanian714447b2010-07-13 21:05:02 +00001610 NewParm->setDeclContext(CurContext);
John McCall8fb0d9d2011-05-01 22:35:37 +00001611
1612 NewParm->setScopeInfo(OldParm->getFunctionScopeDepth(),
1613 OldParm->getFunctionScopeIndex() + indexAdjustment);
Fariborz Jahaniana6c7efe2010-07-13 20:05:58 +00001614
Douglas Gregor940bca72010-04-12 07:48:19 +00001615 return NewParm;
1616}
1617
Douglas Gregordd472162011-01-07 00:20:55 +00001618/// \brief Substitute the given template arguments into the given set of
1619/// parameters, producing the set of parameter types that would be generated
1620/// from such a substitution.
1621bool Sema::SubstParmTypes(SourceLocation Loc,
1622 ParmVarDecl **Params, unsigned NumParams,
1623 const MultiLevelTemplateArgumentList &TemplateArgs,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001624 SmallVectorImpl<QualType> &ParamTypes,
1625 SmallVectorImpl<ParmVarDecl *> *OutParams) {
Douglas Gregordd472162011-01-07 00:20:55 +00001626 assert(!ActiveTemplateInstantiations.empty() &&
1627 "Cannot perform an instantiation without some context on the "
1628 "instantiation stack");
1629
1630 TemplateInstantiator Instantiator(*this, TemplateArgs, Loc,
1631 DeclarationName());
1632 return Instantiator.TransformFunctionTypeParams(Loc, Params, NumParams, 0,
Douglas Gregorf3010112011-01-07 16:43:16 +00001633 ParamTypes, OutParams);
Douglas Gregordd472162011-01-07 00:20:55 +00001634}
1635
John McCall76d824f2009-08-25 22:02:44 +00001636/// \brief Perform substitution on the base class specifiers of the
1637/// given class template specialization.
Douglas Gregor463421d2009-03-03 04:44:36 +00001638///
1639/// Produces a diagnostic and returns true on error, returns false and
1640/// attaches the instantiated base classes to the class template
1641/// specialization if successful.
Mike Stump11289f42009-09-09 15:08:12 +00001642bool
John McCall76d824f2009-08-25 22:02:44 +00001643Sema::SubstBaseSpecifiers(CXXRecordDecl *Instantiation,
1644 CXXRecordDecl *Pattern,
Douglas Gregor01afeef2009-08-28 20:31:08 +00001645 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor463421d2009-03-03 04:44:36 +00001646 bool Invalid = false;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001647 SmallVector<CXXBaseSpecifier*, 4> InstantiatedBases;
Mike Stump11289f42009-09-09 15:08:12 +00001648 for (ClassTemplateSpecializationDecl::base_class_iterator
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001649 Base = Pattern->bases_begin(), BaseEnd = Pattern->bases_end();
Douglas Gregor2a72edd2009-03-10 18:52:44 +00001650 Base != BaseEnd; ++Base) {
Douglas Gregor463421d2009-03-03 04:44:36 +00001651 if (!Base->getType()->isDependentType()) {
Fariborz Jahanian5c14ec32009-07-22 17:41:53 +00001652 InstantiatedBases.push_back(new (Context) CXXBaseSpecifier(*Base));
Douglas Gregor463421d2009-03-03 04:44:36 +00001653 continue;
1654 }
1655
Douglas Gregor752a5952011-01-03 22:36:02 +00001656 SourceLocation EllipsisLoc;
Douglas Gregorc52264e2011-03-02 02:04:06 +00001657 TypeSourceInfo *BaseTypeLoc;
Douglas Gregor752a5952011-01-03 22:36:02 +00001658 if (Base->isPackExpansion()) {
1659 // This is a pack expansion. See whether we should expand it now, or
1660 // wait until later.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001661 SmallVector<UnexpandedParameterPack, 2> Unexpanded;
Douglas Gregor752a5952011-01-03 22:36:02 +00001662 collectUnexpandedParameterPacks(Base->getTypeSourceInfo()->getTypeLoc(),
1663 Unexpanded);
1664 bool ShouldExpand = false;
Douglas Gregora8bac7f2011-01-10 07:32:04 +00001665 bool RetainExpansion = false;
Douglas Gregor0dca5fd2011-01-14 17:04:44 +00001666 llvm::Optional<unsigned> NumExpansions;
Douglas Gregor752a5952011-01-03 22:36:02 +00001667 if (CheckParameterPacksForExpansion(Base->getEllipsisLoc(),
1668 Base->getSourceRange(),
David Blaikieb9c168a2011-09-22 02:34:54 +00001669 Unexpanded,
Douglas Gregor752a5952011-01-03 22:36:02 +00001670 TemplateArgs, ShouldExpand,
Douglas Gregora8bac7f2011-01-10 07:32:04 +00001671 RetainExpansion,
Douglas Gregor752a5952011-01-03 22:36:02 +00001672 NumExpansions)) {
Douglas Gregor752a5952011-01-03 22:36:02 +00001673 Invalid = true;
Douglas Gregor44e7df62011-01-04 00:32:56 +00001674 continue;
Douglas Gregor752a5952011-01-03 22:36:02 +00001675 }
1676
1677 // If we should expand this pack expansion now, do so.
1678 if (ShouldExpand) {
Douglas Gregor0dca5fd2011-01-14 17:04:44 +00001679 for (unsigned I = 0; I != *NumExpansions; ++I) {
Douglas Gregor752a5952011-01-03 22:36:02 +00001680 Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(*this, I);
1681
1682 TypeSourceInfo *BaseTypeLoc = SubstType(Base->getTypeSourceInfo(),
1683 TemplateArgs,
1684 Base->getSourceRange().getBegin(),
1685 DeclarationName());
1686 if (!BaseTypeLoc) {
1687 Invalid = true;
1688 continue;
1689 }
1690
1691 if (CXXBaseSpecifier *InstantiatedBase
1692 = CheckBaseSpecifier(Instantiation,
1693 Base->getSourceRange(),
1694 Base->isVirtual(),
1695 Base->getAccessSpecifierAsWritten(),
1696 BaseTypeLoc,
1697 SourceLocation()))
1698 InstantiatedBases.push_back(InstantiatedBase);
1699 else
1700 Invalid = true;
1701 }
1702
1703 continue;
1704 }
1705
1706 // The resulting base specifier will (still) be a pack expansion.
1707 EllipsisLoc = Base->getEllipsisLoc();
Douglas Gregorc52264e2011-03-02 02:04:06 +00001708 Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(*this, -1);
1709 BaseTypeLoc = SubstType(Base->getTypeSourceInfo(),
1710 TemplateArgs,
1711 Base->getSourceRange().getBegin(),
1712 DeclarationName());
1713 } else {
1714 BaseTypeLoc = SubstType(Base->getTypeSourceInfo(),
1715 TemplateArgs,
1716 Base->getSourceRange().getBegin(),
1717 DeclarationName());
Douglas Gregor752a5952011-01-03 22:36:02 +00001718 }
1719
Nick Lewycky19b9f952010-07-26 16:56:01 +00001720 if (!BaseTypeLoc) {
Douglas Gregor463421d2009-03-03 04:44:36 +00001721 Invalid = true;
1722 continue;
1723 }
1724
1725 if (CXXBaseSpecifier *InstantiatedBase
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001726 = CheckBaseSpecifier(Instantiation,
Douglas Gregor463421d2009-03-03 04:44:36 +00001727 Base->getSourceRange(),
1728 Base->isVirtual(),
1729 Base->getAccessSpecifierAsWritten(),
Douglas Gregor752a5952011-01-03 22:36:02 +00001730 BaseTypeLoc,
1731 EllipsisLoc))
Douglas Gregor463421d2009-03-03 04:44:36 +00001732 InstantiatedBases.push_back(InstantiatedBase);
1733 else
1734 Invalid = true;
1735 }
1736
Douglas Gregor2a72edd2009-03-10 18:52:44 +00001737 if (!Invalid &&
Jay Foad7d0479f2009-05-21 09:52:38 +00001738 AttachBaseSpecifiers(Instantiation, InstantiatedBases.data(),
Douglas Gregor463421d2009-03-03 04:44:36 +00001739 InstantiatedBases.size()))
1740 Invalid = true;
1741
1742 return Invalid;
1743}
1744
DeLesley Hutchins30398dd2012-01-20 22:50:54 +00001745// Defined via #include from SemaTemplateInstantiateDecl.cpp
Benjamin Kramerbf8da9d2012-02-06 11:13:08 +00001746namespace clang {
1747 namespace sema {
1748 Attr *instantiateTemplateAttribute(const Attr *At, ASTContext &C, Sema &S,
1749 const MultiLevelTemplateArgumentList &TemplateArgs);
1750 }
1751}
DeLesley Hutchins30398dd2012-01-20 22:50:54 +00001752
Richard Smith4b38ded2012-03-14 23:13:10 +00001753/// Determine whether we would be unable to instantiate this template (because
1754/// it either has no definition, or is in the process of being instantiated).
1755static bool DiagnoseUninstantiableTemplate(Sema &S,
1756 SourceLocation PointOfInstantiation,
1757 TagDecl *Instantiation,
1758 bool InstantiatedFromMember,
1759 TagDecl *Pattern,
1760 TagDecl *PatternDef,
1761 TemplateSpecializationKind TSK,
1762 bool Complain = true) {
1763 if (PatternDef && !PatternDef->isBeingDefined())
1764 return false;
1765
1766 if (!Complain || (PatternDef && PatternDef->isInvalidDecl())) {
1767 // Say nothing
1768 } else if (PatternDef) {
1769 assert(PatternDef->isBeingDefined());
1770 S.Diag(PointOfInstantiation,
1771 diag::err_template_instantiate_within_definition)
1772 << (TSK != TSK_ImplicitInstantiation)
1773 << S.Context.getTypeDeclType(Instantiation);
1774 // Not much point in noting the template declaration here, since
1775 // we're lexically inside it.
1776 Instantiation->setInvalidDecl();
1777 } else if (InstantiatedFromMember) {
1778 S.Diag(PointOfInstantiation,
1779 diag::err_implicit_instantiate_member_undefined)
1780 << S.Context.getTypeDeclType(Instantiation);
1781 S.Diag(Pattern->getLocation(), diag::note_member_of_template_here);
1782 } else {
1783 S.Diag(PointOfInstantiation, diag::err_template_instantiate_undefined)
1784 << (TSK != TSK_ImplicitInstantiation)
1785 << S.Context.getTypeDeclType(Instantiation);
1786 S.Diag(Pattern->getLocation(), diag::note_template_decl_here);
1787 }
1788
1789 // In general, Instantiation isn't marked invalid to get more than one
1790 // error for multiple undefined instantiations. But the code that does
1791 // explicit declaration -> explicit definition conversion can't handle
1792 // invalid declarations, so mark as invalid in that case.
1793 if (TSK == TSK_ExplicitInstantiationDeclaration)
1794 Instantiation->setInvalidDecl();
1795 return true;
1796}
1797
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001798/// \brief Instantiate the definition of a class from a given pattern.
1799///
1800/// \param PointOfInstantiation The point of instantiation within the
1801/// source code.
1802///
1803/// \param Instantiation is the declaration whose definition is being
1804/// instantiated. This will be either a class template specialization
1805/// or a member class of a class template specialization.
1806///
1807/// \param Pattern is the pattern from which the instantiation
1808/// occurs. This will be either the declaration of a class template or
1809/// the declaration of a member class of a class template.
1810///
1811/// \param TemplateArgs The template arguments to be substituted into
1812/// the pattern.
1813///
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001814/// \param TSK the kind of implicit or explicit instantiation to perform.
Douglas Gregor8a2e6012009-08-24 15:23:48 +00001815///
1816/// \param Complain whether to complain if the class cannot be instantiated due
1817/// to the lack of a definition.
1818///
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001819/// \returns true if an error occurred, false otherwise.
1820bool
1821Sema::InstantiateClass(SourceLocation PointOfInstantiation,
1822 CXXRecordDecl *Instantiation, CXXRecordDecl *Pattern,
Douglas Gregor01afeef2009-08-28 20:31:08 +00001823 const MultiLevelTemplateArgumentList &TemplateArgs,
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001824 TemplateSpecializationKind TSK,
Douglas Gregor8a2e6012009-08-24 15:23:48 +00001825 bool Complain) {
Mike Stump11289f42009-09-09 15:08:12 +00001826 CXXRecordDecl *PatternDef
Douglas Gregor0a5a2212010-02-11 01:04:33 +00001827 = cast_or_null<CXXRecordDecl>(Pattern->getDefinition());
Richard Smith4b38ded2012-03-14 23:13:10 +00001828 if (DiagnoseUninstantiableTemplate(*this, PointOfInstantiation, Instantiation,
1829 Instantiation->getInstantiatedFromMemberClass(),
1830 Pattern, PatternDef, TSK, Complain))
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001831 return true;
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001832 Pattern = PatternDef;
1833
Douglas Gregord6ba93d2009-10-15 15:54:05 +00001834 // \brief Record the point of instantiation.
1835 if (MemberSpecializationInfo *MSInfo
1836 = Instantiation->getMemberSpecializationInfo()) {
1837 MSInfo->setTemplateSpecializationKind(TSK);
1838 MSInfo->setPointOfInstantiation(PointOfInstantiation);
Douglas Gregoref6ab412009-10-27 06:26:26 +00001839 } else if (ClassTemplateSpecializationDecl *Spec
Nico Weber3ffc4c92011-12-20 20:32:49 +00001840 = dyn_cast<ClassTemplateSpecializationDecl>(Instantiation)) {
Douglas Gregoref6ab412009-10-27 06:26:26 +00001841 Spec->setTemplateSpecializationKind(TSK);
1842 Spec->setPointOfInstantiation(PointOfInstantiation);
Douglas Gregord6ba93d2009-10-15 15:54:05 +00001843 }
1844
Douglas Gregorf3430ae2009-03-25 21:23:52 +00001845 InstantiatingTemplate Inst(*this, PointOfInstantiation, Instantiation);
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001846 if (Inst)
1847 return true;
1848
1849 // Enter the scope of this instantiation. We don't use
1850 // PushDeclContext because we don't have a scope.
John McCall80e58cd2010-04-29 00:35:03 +00001851 ContextRAII SavedContext(*this, Instantiation);
Douglas Gregor17158422010-05-12 17:27:19 +00001852 EnterExpressionEvaluationContext EvalContext(*this,
John McCallfaf5fb42010-08-26 23:41:50 +00001853 Sema::PotentiallyEvaluated);
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001854
Douglas Gregor51121572010-03-24 01:33:17 +00001855 // If this is an instantiation of a local class, merge this local
1856 // instantiation scope with the enclosing scope. Otherwise, every
1857 // instantiation of a class has its own local instantiation scope.
1858 bool MergeWithParentScope = !Instantiation->isDefinedOutsideFunctionOrMethod();
John McCall19c1bfd2010-08-25 05:32:35 +00001859 LocalInstantiationScope Scope(*this, MergeWithParentScope);
Douglas Gregor51121572010-03-24 01:33:17 +00001860
John McCall6602bb12010-08-01 02:01:53 +00001861 // Pull attributes from the pattern onto the instantiation.
1862 InstantiateAttrs(TemplateArgs, Pattern, Instantiation);
1863
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001864 // Start the definition of this instantiation.
1865 Instantiation->startDefinition();
Douglas Gregore9029562010-05-06 00:28:52 +00001866
1867 Instantiation->setTagKind(Pattern->getTagKind());
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001868
John McCall76d824f2009-08-25 22:02:44 +00001869 // Do substitution on the base class specifiers.
1870 if (SubstBaseSpecifiers(Instantiation, Pattern, TemplateArgs))
Douglas Gregorb9b8b812012-07-02 21:00:41 +00001871 Instantiation->setInvalidDecl();
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001872
Douglas Gregor869853e2010-11-10 19:44:59 +00001873 TemplateDeclInstantiator Instantiator(*this, Instantiation, TemplateArgs);
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001874 SmallVector<Decl*, 4> Fields;
1875 SmallVector<std::pair<FieldDecl*, FieldDecl*>, 4>
Richard Smith938f40b2011-06-11 17:19:42 +00001876 FieldsWithMemberInitializers;
DeLesley Hutchins30398dd2012-01-20 22:50:54 +00001877 // Delay instantiation of late parsed attributes.
1878 LateInstantiatedAttrVec LateAttrs;
1879 Instantiator.enableLateAttributeInstantiation(&LateAttrs);
1880
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001881 for (RecordDecl::decl_iterator Member = Pattern->decls_begin(),
Mike Stump11289f42009-09-09 15:08:12 +00001882 MemberEnd = Pattern->decls_end();
Douglas Gregorbcced4e2009-04-09 21:40:53 +00001883 Member != MemberEnd; ++Member) {
Argyrios Kyrtzidis9a94d9b2010-11-04 03:18:57 +00001884 // Don't instantiate members not belonging in this semantic context.
1885 // e.g. for:
1886 // @code
1887 // template <int i> class A {
1888 // class B *g;
1889 // };
1890 // @endcode
1891 // 'class B' has the template as lexical context but semantically it is
1892 // introduced in namespace scope.
1893 if ((*Member)->getDeclContext() != Pattern)
1894 continue;
1895
Douglas Gregor869853e2010-11-10 19:44:59 +00001896 if ((*Member)->isInvalidDecl()) {
Richard Smithded9c2e2012-07-11 22:37:56 +00001897 Instantiation->setInvalidDecl();
Douglas Gregor869853e2010-11-10 19:44:59 +00001898 continue;
1899 }
1900
1901 Decl *NewMember = Instantiator.Visit(*Member);
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001902 if (NewMember) {
Richard Smith938f40b2011-06-11 17:19:42 +00001903 if (FieldDecl *Field = dyn_cast<FieldDecl>(NewMember)) {
John McCall48871652010-08-21 09:40:31 +00001904 Fields.push_back(Field);
Richard Smith938f40b2011-06-11 17:19:42 +00001905 FieldDecl *OldField = cast<FieldDecl>(*Member);
1906 if (OldField->getInClassInitializer())
1907 FieldsWithMemberInitializers.push_back(std::make_pair(OldField,
1908 Field));
Richard Smith7d137e32012-03-23 03:33:32 +00001909 } else if (EnumDecl *Enum = dyn_cast<EnumDecl>(NewMember)) {
1910 // C++11 [temp.inst]p1: The implicit instantiation of a class template
1911 // specialization causes the implicit instantiation of the definitions
1912 // of unscoped member enumerations.
1913 // Record a point of instantiation for this implicit instantiation.
Richard Smithb66d7772012-03-23 23:09:08 +00001914 if (TSK == TSK_ImplicitInstantiation && !Enum->isScoped() &&
1915 Enum->isCompleteDefinition()) {
Richard Smith7d137e32012-03-23 03:33:32 +00001916 MemberSpecializationInfo *MSInfo =Enum->getMemberSpecializationInfo();
1917 assert(MSInfo && "no spec info for member enum specialization");
1918 MSInfo->setTemplateSpecializationKind(TSK_ImplicitInstantiation);
1919 MSInfo->setPointOfInstantiation(PointOfInstantiation);
1920 }
Richard Smithded9c2e2012-07-11 22:37:56 +00001921 } else if (StaticAssertDecl *SA = dyn_cast<StaticAssertDecl>(NewMember)) {
1922 if (SA->isFailed()) {
1923 // A static_assert failed. Bail out; instantiating this
1924 // class is probably not meaningful.
1925 Instantiation->setInvalidDecl();
1926 break;
1927 }
Richard Smith7d137e32012-03-23 03:33:32 +00001928 }
1929
1930 if (NewMember->isInvalidDecl())
Douglas Gregorb9b8b812012-07-02 21:00:41 +00001931 Instantiation->setInvalidDecl();
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001932 } else {
1933 // FIXME: Eventually, a NULL return will mean that one of the
Douglas Gregorb9b8b812012-07-02 21:00:41 +00001934 // instantiations was a semantic disaster, and we'll want to mark the
1935 // declaration invalid.
1936 // For now, we expect to skip some members that we can't yet handle.
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001937 }
1938 }
1939
1940 // Finish checking fields.
David Blaikie751c5582011-09-22 02:58:26 +00001941 ActOnFields(0, Instantiation->getLocation(), Instantiation, Fields,
1942 SourceLocation(), SourceLocation(), 0);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001943 CheckCompletedCXXClass(Instantiation);
Richard Smith938f40b2011-06-11 17:19:42 +00001944
1945 // Attach any in-class member initializers now the class is complete.
Benjamin Kramer1d373c62012-05-17 12:01:52 +00001946 if (!FieldsWithMemberInitializers.empty()) {
Douglas Gregor3024f072012-04-16 07:05:22 +00001947 // C++11 [expr.prim.general]p4:
1948 // Otherwise, if a member-declarator declares a non-static data member
1949 // (9.2) of a class X, the expression this is a prvalue of type "pointer
1950 // to X" within the optional brace-or-equal-initializer. It shall not
1951 // appear elsewhere in the member-declarator.
1952 CXXThisScopeRAII ThisScope(*this, Instantiation, (unsigned)0);
1953
1954 for (unsigned I = 0, N = FieldsWithMemberInitializers.size(); I != N; ++I) {
1955 FieldDecl *OldField = FieldsWithMemberInitializers[I].first;
1956 FieldDecl *NewField = FieldsWithMemberInitializers[I].second;
1957 Expr *OldInit = OldField->getInClassInitializer();
Richard Smith938f40b2011-06-11 17:19:42 +00001958
Douglas Gregor3024f072012-04-16 07:05:22 +00001959 ExprResult NewInit = SubstInitializer(OldInit, TemplateArgs,
1960 /*CXXDirectInit=*/false);
1961 if (NewInit.isInvalid())
1962 NewField->setInvalidDecl();
1963 else {
1964 Expr *Init = NewInit.take();
1965 assert(Init && "no-argument initializer in class");
1966 assert(!isa<ParenListExpr>(Init) && "call-style init in class");
Richard Smith2b013182012-06-10 03:12:00 +00001967 ActOnCXXInClassMemberInitializer(NewField, Init->getLocStart(), Init);
Douglas Gregor3024f072012-04-16 07:05:22 +00001968 }
Richard Smithe3daab22011-07-20 00:12:52 +00001969 }
Richard Smith938f40b2011-06-11 17:19:42 +00001970 }
DeLesley Hutchins30398dd2012-01-20 22:50:54 +00001971 // Instantiate late parsed attributes, and attach them to their decls.
1972 // See Sema::InstantiateAttrs
1973 for (LateInstantiatedAttrVec::iterator I = LateAttrs.begin(),
1974 E = LateAttrs.end(); I != E; ++I) {
1975 assert(CurrentInstantiationScope == Instantiator.getStartingScope());
1976 CurrentInstantiationScope = I->Scope;
1977 Attr *NewAttr =
1978 instantiateTemplateAttribute(I->TmplAttr, Context, *this, TemplateArgs);
1979 I->NewDecl->addAttr(NewAttr);
1980 LocalInstantiationScope::deleteScopes(I->Scope,
1981 Instantiator.getStartingScope());
1982 }
1983 Instantiator.disableLateAttributeInstantiation();
1984 LateAttrs.clear();
1985
Richard Smith938f40b2011-06-11 17:19:42 +00001986 if (!FieldsWithMemberInitializers.empty())
1987 ActOnFinishDelayedMemberInitializers(Instantiation);
1988
Abramo Bagnara12dcbf32011-11-18 08:08:52 +00001989 if (TSK == TSK_ImplicitInstantiation) {
Argyrios Kyrtzidise3789482012-02-11 01:59:57 +00001990 Instantiation->setLocation(Pattern->getLocation());
Abramo Bagnara12dcbf32011-11-18 08:08:52 +00001991 Instantiation->setLocStart(Pattern->getInnerLocStart());
Abramo Bagnarafd3a4552011-10-03 20:34:03 +00001992 Instantiation->setRBraceLoc(Pattern->getRBraceLoc());
Abramo Bagnara12dcbf32011-11-18 08:08:52 +00001993 }
Abramo Bagnarafd3a4552011-10-03 20:34:03 +00001994
Douglas Gregorb9b8b812012-07-02 21:00:41 +00001995 if (!Instantiation->isInvalidDecl()) {
Douglas Gregor869853e2010-11-10 19:44:59 +00001996 // Instantiate any out-of-line class template partial
1997 // specializations now.
1998 for (TemplateDeclInstantiator::delayed_partial_spec_iterator
1999 P = Instantiator.delayed_partial_spec_begin(),
2000 PEnd = Instantiator.delayed_partial_spec_end();
2001 P != PEnd; ++P) {
2002 if (!Instantiator.InstantiateClassTemplatePartialSpecialization(
2003 P->first,
2004 P->second)) {
Douglas Gregorb9b8b812012-07-02 21:00:41 +00002005 Instantiation->setInvalidDecl();
Douglas Gregor869853e2010-11-10 19:44:59 +00002006 break;
2007 }
2008 }
2009 }
2010
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00002011 // Exit the scope of this instantiation.
John McCall80e58cd2010-04-29 00:35:03 +00002012 SavedContext.pop();
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00002013
Douglas Gregorb9b8b812012-07-02 21:00:41 +00002014 if (!Instantiation->isInvalidDecl()) {
Douglas Gregor28ad4b52009-05-26 20:50:29 +00002015 Consumer.HandleTagDeclDefinition(Instantiation);
2016
Douglas Gregor88d292c2010-05-13 16:44:06 +00002017 // Always emit the vtable for an explicit instantiation definition
2018 // of a polymorphic class template specialization.
2019 if (TSK == TSK_ExplicitInstantiationDefinition)
2020 MarkVTableUsed(PointOfInstantiation, Instantiation, true);
2021 }
2022
Douglas Gregorb9b8b812012-07-02 21:00:41 +00002023 return Instantiation->isInvalidDecl();
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00002024}
2025
Richard Smith4b38ded2012-03-14 23:13:10 +00002026/// \brief Instantiate the definition of an enum from a given pattern.
2027///
2028/// \param PointOfInstantiation The point of instantiation within the
2029/// source code.
2030/// \param Instantiation is the declaration whose definition is being
2031/// instantiated. This will be a member enumeration of a class
2032/// temploid specialization, or a local enumeration within a
2033/// function temploid specialization.
2034/// \param Pattern The templated declaration from which the instantiation
2035/// occurs.
2036/// \param TemplateArgs The template arguments to be substituted into
2037/// the pattern.
2038/// \param TSK The kind of implicit or explicit instantiation to perform.
2039///
2040/// \return \c true if an error occurred, \c false otherwise.
2041bool Sema::InstantiateEnum(SourceLocation PointOfInstantiation,
2042 EnumDecl *Instantiation, EnumDecl *Pattern,
2043 const MultiLevelTemplateArgumentList &TemplateArgs,
2044 TemplateSpecializationKind TSK) {
2045 EnumDecl *PatternDef = Pattern->getDefinition();
2046 if (DiagnoseUninstantiableTemplate(*this, PointOfInstantiation, Instantiation,
2047 Instantiation->getInstantiatedFromMemberEnum(),
2048 Pattern, PatternDef, TSK,/*Complain*/true))
2049 return true;
2050 Pattern = PatternDef;
2051
2052 // Record the point of instantiation.
2053 if (MemberSpecializationInfo *MSInfo
2054 = Instantiation->getMemberSpecializationInfo()) {
2055 MSInfo->setTemplateSpecializationKind(TSK);
2056 MSInfo->setPointOfInstantiation(PointOfInstantiation);
2057 }
2058
2059 InstantiatingTemplate Inst(*this, PointOfInstantiation, Instantiation);
2060 if (Inst)
2061 return true;
2062
2063 // Enter the scope of this instantiation. We don't use
2064 // PushDeclContext because we don't have a scope.
2065 ContextRAII SavedContext(*this, Instantiation);
2066 EnterExpressionEvaluationContext EvalContext(*this,
2067 Sema::PotentiallyEvaluated);
2068
2069 LocalInstantiationScope Scope(*this, /*MergeWithParentScope*/true);
2070
2071 // Pull attributes from the pattern onto the instantiation.
2072 InstantiateAttrs(TemplateArgs, Pattern, Instantiation);
2073
2074 TemplateDeclInstantiator Instantiator(*this, Instantiation, TemplateArgs);
2075 Instantiator.InstantiateEnumDefinition(Instantiation, Pattern);
2076
2077 // Exit the scope of this instantiation.
2078 SavedContext.pop();
2079
2080 return Instantiation->isInvalidDecl();
2081}
2082
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +00002083namespace {
2084 /// \brief A partial specialization whose template arguments have matched
2085 /// a given template-id.
2086 struct PartialSpecMatchResult {
2087 ClassTemplatePartialSpecializationDecl *Partial;
2088 TemplateArgumentList *Args;
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +00002089 };
2090}
2091
Mike Stump11289f42009-09-09 15:08:12 +00002092bool
Douglas Gregor463421d2009-03-03 04:44:36 +00002093Sema::InstantiateClassTemplateSpecialization(
Douglas Gregoref6ab412009-10-27 06:26:26 +00002094 SourceLocation PointOfInstantiation,
Douglas Gregor463421d2009-03-03 04:44:36 +00002095 ClassTemplateSpecializationDecl *ClassTemplateSpec,
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00002096 TemplateSpecializationKind TSK,
Douglas Gregor8a2e6012009-08-24 15:23:48 +00002097 bool Complain) {
Douglas Gregor463421d2009-03-03 04:44:36 +00002098 // Perform the actual instantiation on the canonical declaration.
2099 ClassTemplateSpec = cast<ClassTemplateSpecializationDecl>(
Argyrios Kyrtzidis6b7e3762009-07-18 00:34:25 +00002100 ClassTemplateSpec->getCanonicalDecl());
Douglas Gregor463421d2009-03-03 04:44:36 +00002101
Douglas Gregor4aa04b12009-09-11 21:19:12 +00002102 // Check whether we have already instantiated or specialized this class
2103 // template specialization.
2104 if (ClassTemplateSpec->getSpecializationKind() != TSK_Undeclared) {
2105 if (ClassTemplateSpec->getSpecializationKind() ==
2106 TSK_ExplicitInstantiationDeclaration &&
2107 TSK == TSK_ExplicitInstantiationDefinition) {
2108 // An explicit instantiation definition follows an explicit instantiation
2109 // declaration (C++0x [temp.explicit]p10); go ahead and perform the
2110 // explicit instantiation.
2111 ClassTemplateSpec->setSpecializationKind(TSK);
Douglas Gregor88d292c2010-05-13 16:44:06 +00002112
2113 // If this is an explicit instantiation definition, mark the
2114 // vtable as used.
Nico Weber3ffc4c92011-12-20 20:32:49 +00002115 if (TSK == TSK_ExplicitInstantiationDefinition &&
2116 !ClassTemplateSpec->isInvalidDecl())
Douglas Gregor88d292c2010-05-13 16:44:06 +00002117 MarkVTableUsed(PointOfInstantiation, ClassTemplateSpec, true);
2118
Douglas Gregor4aa04b12009-09-11 21:19:12 +00002119 return false;
2120 }
2121
2122 // We can only instantiate something that hasn't already been
2123 // instantiated or specialized. Fail without any diagnostics: our
2124 // caller will provide an error message.
Douglas Gregor463421d2009-03-03 04:44:36 +00002125 return true;
Douglas Gregor4aa04b12009-09-11 21:19:12 +00002126 }
Douglas Gregor463421d2009-03-03 04:44:36 +00002127
Douglas Gregor00a511f2009-09-15 16:51:42 +00002128 if (ClassTemplateSpec->isInvalidDecl())
2129 return true;
2130
Douglas Gregor463421d2009-03-03 04:44:36 +00002131 ClassTemplateDecl *Template = ClassTemplateSpec->getSpecializedTemplate();
Douglas Gregor01afeef2009-08-28 20:31:08 +00002132 CXXRecordDecl *Pattern = 0;
Douglas Gregor2373c592009-05-31 09:31:02 +00002133
Douglas Gregor170bc422009-06-12 22:31:52 +00002134 // C++ [temp.class.spec.match]p1:
2135 // When a class template is used in a context that requires an
2136 // instantiation of the class, it is necessary to determine
2137 // whether the instantiation is to be generated using the primary
2138 // template or one of the partial specializations. This is done by
2139 // matching the template arguments of the class template
2140 // specialization with the template argument lists of the partial
2141 // specializations.
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +00002142 typedef PartialSpecMatchResult MatchResult;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002143 SmallVector<MatchResult, 4> Matched;
2144 SmallVector<ClassTemplatePartialSpecializationDecl *, 4> PartialSpecs;
Douglas Gregor407e9612010-04-30 05:56:50 +00002145 Template->getPartialSpecializations(PartialSpecs);
2146 for (unsigned I = 0, N = PartialSpecs.size(); I != N; ++I) {
2147 ClassTemplatePartialSpecializationDecl *Partial = PartialSpecs[I];
John McCallbc077cf2010-02-08 23:07:23 +00002148 TemplateDeductionInfo Info(Context, PointOfInstantiation);
Douglas Gregor181aa4a2009-06-12 18:26:56 +00002149 if (TemplateDeductionResult Result
Douglas Gregor407e9612010-04-30 05:56:50 +00002150 = DeduceTemplateArguments(Partial,
Douglas Gregor181aa4a2009-06-12 18:26:56 +00002151 ClassTemplateSpec->getTemplateArgs(),
2152 Info)) {
2153 // FIXME: Store the failed-deduction information for use in
2154 // diagnostics, later.
2155 (void)Result;
2156 } else {
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +00002157 Matched.push_back(PartialSpecMatchResult());
2158 Matched.back().Partial = Partial;
2159 Matched.back().Args = Info.take();
Douglas Gregor181aa4a2009-06-12 18:26:56 +00002160 }
Douglas Gregor2373c592009-05-31 09:31:02 +00002161 }
2162
Douglas Gregor0231d8d2011-01-19 20:10:05 +00002163 // If we're dealing with a member template where the template parameters
2164 // have been instantiated, this provides the original template parameters
2165 // from which the member template's parameters were instantiated.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002166 SmallVector<const NamedDecl *, 4> InstantiatedTemplateParameters;
Douglas Gregor0231d8d2011-01-19 20:10:05 +00002167
Douglas Gregor21610382009-10-29 00:04:11 +00002168 if (Matched.size() >= 1) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002169 SmallVector<MatchResult, 4>::iterator Best = Matched.begin();
Douglas Gregor21610382009-10-29 00:04:11 +00002170 if (Matched.size() == 1) {
2171 // -- If exactly one matching specialization is found, the
2172 // instantiation is generated from that specialization.
2173 // We don't need to do anything for this.
2174 } else {
2175 // -- If more than one matching specialization is found, the
2176 // partial order rules (14.5.4.2) are used to determine
2177 // whether one of the specializations is more specialized
2178 // than the others. If none of the specializations is more
2179 // specialized than all of the other matching
2180 // specializations, then the use of the class template is
2181 // ambiguous and the program is ill-formed.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002182 for (SmallVector<MatchResult, 4>::iterator P = Best + 1,
Douglas Gregor21610382009-10-29 00:04:11 +00002183 PEnd = Matched.end();
2184 P != PEnd; ++P) {
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +00002185 if (getMoreSpecializedPartialSpecialization(P->Partial, Best->Partial,
John McCallbc077cf2010-02-08 23:07:23 +00002186 PointOfInstantiation)
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +00002187 == P->Partial)
Douglas Gregor21610382009-10-29 00:04:11 +00002188 Best = P;
Douglas Gregorbe999392009-09-15 16:23:51 +00002189 }
Douglas Gregorbe999392009-09-15 16:23:51 +00002190
Douglas Gregor21610382009-10-29 00:04:11 +00002191 // Determine if the best partial specialization is more specialized than
2192 // the others.
2193 bool Ambiguous = false;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002194 for (SmallVector<MatchResult, 4>::iterator P = Matched.begin(),
Douglas Gregorbe999392009-09-15 16:23:51 +00002195 PEnd = Matched.end();
Douglas Gregor21610382009-10-29 00:04:11 +00002196 P != PEnd; ++P) {
2197 if (P != Best &&
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +00002198 getMoreSpecializedPartialSpecialization(P->Partial, Best->Partial,
John McCallbc077cf2010-02-08 23:07:23 +00002199 PointOfInstantiation)
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +00002200 != Best->Partial) {
Douglas Gregor21610382009-10-29 00:04:11 +00002201 Ambiguous = true;
2202 break;
2203 }
2204 }
2205
2206 if (Ambiguous) {
2207 // Partial ordering did not produce a clear winner. Complain.
2208 ClassTemplateSpec->setInvalidDecl();
2209 Diag(PointOfInstantiation, diag::err_partial_spec_ordering_ambiguous)
2210 << ClassTemplateSpec;
2211
2212 // Print the matching partial specializations.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002213 for (SmallVector<MatchResult, 4>::iterator P = Matched.begin(),
Douglas Gregor21610382009-10-29 00:04:11 +00002214 PEnd = Matched.end();
2215 P != PEnd; ++P)
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +00002216 Diag(P->Partial->getLocation(), diag::note_partial_spec_match)
2217 << getTemplateArgumentBindingsText(
2218 P->Partial->getTemplateParameters(),
2219 *P->Args);
Douglas Gregor01afeef2009-08-28 20:31:08 +00002220
Douglas Gregor21610382009-10-29 00:04:11 +00002221 return true;
2222 }
Douglas Gregorbe999392009-09-15 16:23:51 +00002223 }
2224
2225 // Instantiate using the best class template partial specialization.
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +00002226 ClassTemplatePartialSpecializationDecl *OrigPartialSpec = Best->Partial;
Douglas Gregor21610382009-10-29 00:04:11 +00002227 while (OrigPartialSpec->getInstantiatedFromMember()) {
2228 // If we've found an explicit specialization of this class template,
2229 // stop here and use that as the pattern.
2230 if (OrigPartialSpec->isMemberSpecialization())
2231 break;
2232
2233 OrigPartialSpec = OrigPartialSpec->getInstantiatedFromMember();
2234 }
2235
2236 Pattern = OrigPartialSpec;
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +00002237 ClassTemplateSpec->setInstantiationOf(Best->Partial, Best->Args);
Douglas Gregor170bc422009-06-12 22:31:52 +00002238 } else {
2239 // -- If no matches are found, the instantiation is generated
2240 // from the primary template.
Douglas Gregor01afeef2009-08-28 20:31:08 +00002241 ClassTemplateDecl *OrigTemplate = Template;
Douglas Gregorcf915552009-10-13 16:30:37 +00002242 while (OrigTemplate->getInstantiatedFromMemberTemplate()) {
2243 // If we've found an explicit specialization of this class template,
2244 // stop here and use that as the pattern.
2245 if (OrigTemplate->isMemberSpecialization())
2246 break;
2247
Douglas Gregor01afeef2009-08-28 20:31:08 +00002248 OrigTemplate = OrigTemplate->getInstantiatedFromMemberTemplate();
Douglas Gregorcf915552009-10-13 16:30:37 +00002249 }
2250
Douglas Gregor01afeef2009-08-28 20:31:08 +00002251 Pattern = OrigTemplate->getTemplatedDecl();
Douglas Gregor2373c592009-05-31 09:31:02 +00002252 }
Douglas Gregor463421d2009-03-03 04:44:36 +00002253
Douglas Gregoref6ab412009-10-27 06:26:26 +00002254 bool Result = InstantiateClass(PointOfInstantiation, ClassTemplateSpec,
2255 Pattern,
2256 getTemplateInstantiationArgs(ClassTemplateSpec),
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00002257 TSK,
Douglas Gregor8a2e6012009-08-24 15:23:48 +00002258 Complain);
Mike Stump11289f42009-09-09 15:08:12 +00002259
Douglas Gregorb7ae10f2009-06-05 00:53:49 +00002260 return Result;
Douglas Gregor463421d2009-03-03 04:44:36 +00002261}
Douglas Gregor90a1a652009-03-19 17:26:29 +00002262
John McCall76d824f2009-08-25 22:02:44 +00002263/// \brief Instantiates the definitions of all of the member
2264/// of the given class, which is an instantiation of a class template
2265/// or a member class of a template.
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00002266void
2267Sema::InstantiateClassMembers(SourceLocation PointOfInstantiation,
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00002268 CXXRecordDecl *Instantiation,
2269 const MultiLevelTemplateArgumentList &TemplateArgs,
2270 TemplateSpecializationKind TSK) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002271 for (DeclContext::decl_iterator D = Instantiation->decls_begin(),
2272 DEnd = Instantiation->decls_end();
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00002273 D != DEnd; ++D) {
Douglas Gregor1d957a32009-10-27 18:42:08 +00002274 bool SuppressNew = false;
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00002275 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(*D)) {
Douglas Gregor1d957a32009-10-27 18:42:08 +00002276 if (FunctionDecl *Pattern
2277 = Function->getInstantiatedFromMemberFunction()) {
2278 MemberSpecializationInfo *MSInfo
2279 = Function->getMemberSpecializationInfo();
2280 assert(MSInfo && "No member specialization information?");
Douglas Gregor06aa50412010-04-09 21:02:29 +00002281 if (MSInfo->getTemplateSpecializationKind()
2282 == TSK_ExplicitSpecialization)
2283 continue;
2284
Douglas Gregor1d957a32009-10-27 18:42:08 +00002285 if (CheckSpecializationInstantiationRedecl(PointOfInstantiation, TSK,
2286 Function,
2287 MSInfo->getTemplateSpecializationKind(),
Nico Weberd75488d2010-09-27 21:02:09 +00002288 MSInfo->getPointOfInstantiation(),
Douglas Gregor1d957a32009-10-27 18:42:08 +00002289 SuppressNew) ||
2290 SuppressNew)
Douglas Gregorbbe8f462009-10-08 15:14:33 +00002291 continue;
2292
Alexis Hunt4a8ea102011-05-06 20:44:56 +00002293 if (Function->isDefined())
Douglas Gregor1d957a32009-10-27 18:42:08 +00002294 continue;
2295
2296 if (TSK == TSK_ExplicitInstantiationDefinition) {
2297 // C++0x [temp.explicit]p8:
2298 // An explicit instantiation definition that names a class template
2299 // specialization explicitly instantiates the class template
2300 // specialization and is only an explicit instantiation definition
2301 // of members whose definition is visible at the point of
2302 // instantiation.
Alexis Hunt4a8ea102011-05-06 20:44:56 +00002303 if (!Pattern->isDefined())
Douglas Gregor1d957a32009-10-27 18:42:08 +00002304 continue;
2305
2306 Function->setTemplateSpecializationKind(TSK, PointOfInstantiation);
2307
2308 InstantiateFunctionDefinition(PointOfInstantiation, Function);
2309 } else {
2310 Function->setTemplateSpecializationKind(TSK, PointOfInstantiation);
2311 }
Douglas Gregorbbe8f462009-10-08 15:14:33 +00002312 }
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00002313 } else if (VarDecl *Var = dyn_cast<VarDecl>(*D)) {
Douglas Gregor86d142a2009-10-08 07:24:58 +00002314 if (Var->isStaticDataMember()) {
Douglas Gregor1d957a32009-10-27 18:42:08 +00002315 MemberSpecializationInfo *MSInfo = Var->getMemberSpecializationInfo();
2316 assert(MSInfo && "No member specialization information?");
Douglas Gregor06aa50412010-04-09 21:02:29 +00002317 if (MSInfo->getTemplateSpecializationKind()
2318 == TSK_ExplicitSpecialization)
2319 continue;
2320
Douglas Gregor1d957a32009-10-27 18:42:08 +00002321 if (CheckSpecializationInstantiationRedecl(PointOfInstantiation, TSK,
2322 Var,
2323 MSInfo->getTemplateSpecializationKind(),
Nico Weberd75488d2010-09-27 21:02:09 +00002324 MSInfo->getPointOfInstantiation(),
Douglas Gregor1d957a32009-10-27 18:42:08 +00002325 SuppressNew) ||
2326 SuppressNew)
Douglas Gregorbbe8f462009-10-08 15:14:33 +00002327 continue;
2328
Douglas Gregor1d957a32009-10-27 18:42:08 +00002329 if (TSK == TSK_ExplicitInstantiationDefinition) {
2330 // C++0x [temp.explicit]p8:
2331 // An explicit instantiation definition that names a class template
2332 // specialization explicitly instantiates the class template
2333 // specialization and is only an explicit instantiation definition
2334 // of members whose definition is visible at the point of
2335 // instantiation.
2336 if (!Var->getInstantiatedFromStaticDataMember()
2337 ->getOutOfLineDefinition())
2338 continue;
2339
2340 Var->setTemplateSpecializationKind(TSK, PointOfInstantiation);
Douglas Gregor86d142a2009-10-08 07:24:58 +00002341 InstantiateStaticDataMemberDefinition(PointOfInstantiation, Var);
Douglas Gregor1d957a32009-10-27 18:42:08 +00002342 } else {
2343 Var->setTemplateSpecializationKind(TSK, PointOfInstantiation);
2344 }
2345 }
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00002346 } else if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(*D)) {
Douglas Gregor1da22252010-04-18 18:11:38 +00002347 // Always skip the injected-class-name, along with any
2348 // redeclarations of nested classes, since both would cause us
2349 // to try to instantiate the members of a class twice.
Douglas Gregorec9fd132012-01-14 16:38:05 +00002350 if (Record->isInjectedClassName() || Record->getPreviousDecl())
Douglas Gregord801b062009-10-07 23:56:10 +00002351 continue;
2352
Douglas Gregor1d957a32009-10-27 18:42:08 +00002353 MemberSpecializationInfo *MSInfo = Record->getMemberSpecializationInfo();
2354 assert(MSInfo && "No member specialization information?");
Douglas Gregor06aa50412010-04-09 21:02:29 +00002355
2356 if (MSInfo->getTemplateSpecializationKind()
2357 == TSK_ExplicitSpecialization)
2358 continue;
Nico Weberd75488d2010-09-27 21:02:09 +00002359
Douglas Gregor1d957a32009-10-27 18:42:08 +00002360 if (CheckSpecializationInstantiationRedecl(PointOfInstantiation, TSK,
2361 Record,
2362 MSInfo->getTemplateSpecializationKind(),
Nico Weberd75488d2010-09-27 21:02:09 +00002363 MSInfo->getPointOfInstantiation(),
Douglas Gregor1d957a32009-10-27 18:42:08 +00002364 SuppressNew) ||
2365 SuppressNew)
Douglas Gregorbbe8f462009-10-08 15:14:33 +00002366 continue;
2367
Douglas Gregor1d957a32009-10-27 18:42:08 +00002368 CXXRecordDecl *Pattern = Record->getInstantiatedFromMemberClass();
2369 assert(Pattern && "Missing instantiated-from-template information");
2370
Douglas Gregor0a5a2212010-02-11 01:04:33 +00002371 if (!Record->getDefinition()) {
2372 if (!Pattern->getDefinition()) {
Douglas Gregor1d957a32009-10-27 18:42:08 +00002373 // C++0x [temp.explicit]p8:
2374 // An explicit instantiation definition that names a class template
2375 // specialization explicitly instantiates the class template
2376 // specialization and is only an explicit instantiation definition
2377 // of members whose definition is visible at the point of
2378 // instantiation.
2379 if (TSK == TSK_ExplicitInstantiationDeclaration) {
2380 MSInfo->setTemplateSpecializationKind(TSK);
2381 MSInfo->setPointOfInstantiation(PointOfInstantiation);
2382 }
2383
2384 continue;
2385 }
2386
2387 InstantiateClass(PointOfInstantiation, Record, Pattern,
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00002388 TemplateArgs,
2389 TSK);
Nico Weberd75488d2010-09-27 21:02:09 +00002390 } else {
2391 if (TSK == TSK_ExplicitInstantiationDefinition &&
2392 Record->getTemplateSpecializationKind() ==
2393 TSK_ExplicitInstantiationDeclaration) {
2394 Record->setTemplateSpecializationKind(TSK);
2395 MarkVTableUsed(PointOfInstantiation, Record, true);
2396 }
Douglas Gregor1d957a32009-10-27 18:42:08 +00002397 }
Douglas Gregorc093c1d2009-10-08 01:19:17 +00002398
Douglas Gregor0a5a2212010-02-11 01:04:33 +00002399 Pattern = cast_or_null<CXXRecordDecl>(Record->getDefinition());
Douglas Gregor1d957a32009-10-27 18:42:08 +00002400 if (Pattern)
2401 InstantiateClassMembers(PointOfInstantiation, Pattern, TemplateArgs,
2402 TSK);
Richard Smith4b38ded2012-03-14 23:13:10 +00002403 } else if (EnumDecl *Enum = dyn_cast<EnumDecl>(*D)) {
2404 MemberSpecializationInfo *MSInfo = Enum->getMemberSpecializationInfo();
2405 assert(MSInfo && "No member specialization information?");
2406
2407 if (MSInfo->getTemplateSpecializationKind()
2408 == TSK_ExplicitSpecialization)
2409 continue;
2410
2411 if (CheckSpecializationInstantiationRedecl(
2412 PointOfInstantiation, TSK, Enum,
2413 MSInfo->getTemplateSpecializationKind(),
2414 MSInfo->getPointOfInstantiation(), SuppressNew) ||
2415 SuppressNew)
2416 continue;
2417
2418 if (Enum->getDefinition())
2419 continue;
2420
2421 EnumDecl *Pattern = Enum->getInstantiatedFromMemberEnum();
2422 assert(Pattern && "Missing instantiated-from-template information");
2423
2424 if (TSK == TSK_ExplicitInstantiationDefinition) {
2425 if (!Pattern->getDefinition())
2426 continue;
2427
2428 InstantiateEnum(PointOfInstantiation, Enum, Pattern, TemplateArgs, TSK);
2429 } else {
2430 MSInfo->setTemplateSpecializationKind(TSK);
2431 MSInfo->setPointOfInstantiation(PointOfInstantiation);
2432 }
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00002433 }
2434 }
2435}
2436
2437/// \brief Instantiate the definitions of all of the members of the
2438/// given class template specialization, which was named as part of an
2439/// explicit instantiation.
Mike Stump11289f42009-09-09 15:08:12 +00002440void
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00002441Sema::InstantiateClassTemplateSpecializationMembers(
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00002442 SourceLocation PointOfInstantiation,
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00002443 ClassTemplateSpecializationDecl *ClassTemplateSpec,
2444 TemplateSpecializationKind TSK) {
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00002445 // C++0x [temp.explicit]p7:
2446 // An explicit instantiation that names a class template
2447 // specialization is an explicit instantion of the same kind
2448 // (declaration or definition) of each of its members (not
2449 // including members inherited from base classes) that has not
2450 // been previously explicitly specialized in the translation unit
2451 // containing the explicit instantiation, except as described
2452 // below.
2453 InstantiateClassMembers(PointOfInstantiation, ClassTemplateSpec,
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00002454 getTemplateInstantiationArgs(ClassTemplateSpec),
2455 TSK);
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00002456}
2457
John McCalldadc5752010-08-24 06:29:42 +00002458StmtResult
Douglas Gregor01afeef2009-08-28 20:31:08 +00002459Sema::SubstStmt(Stmt *S, const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregorebe10102009-08-20 07:17:43 +00002460 if (!S)
2461 return Owned(S);
2462
2463 TemplateInstantiator Instantiator(*this, TemplateArgs,
2464 SourceLocation(),
2465 DeclarationName());
2466 return Instantiator.TransformStmt(S);
2467}
2468
John McCalldadc5752010-08-24 06:29:42 +00002469ExprResult
Douglas Gregor01afeef2009-08-28 20:31:08 +00002470Sema::SubstExpr(Expr *E, const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregora16548e2009-08-11 05:31:07 +00002471 if (!E)
2472 return Owned(E);
Mike Stump11289f42009-09-09 15:08:12 +00002473
Douglas Gregora16548e2009-08-11 05:31:07 +00002474 TemplateInstantiator Instantiator(*this, TemplateArgs,
2475 SourceLocation(),
2476 DeclarationName());
2477 return Instantiator.TransformExpr(E);
2478}
2479
Douglas Gregor2cd32a02011-01-07 19:35:17 +00002480bool Sema::SubstExprs(Expr **Exprs, unsigned NumExprs, bool IsCall,
2481 const MultiLevelTemplateArgumentList &TemplateArgs,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002482 SmallVectorImpl<Expr *> &Outputs) {
Douglas Gregor2cd32a02011-01-07 19:35:17 +00002483 if (NumExprs == 0)
2484 return false;
2485
2486 TemplateInstantiator Instantiator(*this, TemplateArgs,
2487 SourceLocation(),
2488 DeclarationName());
2489 return Instantiator.TransformExprs(Exprs, NumExprs, IsCall, Outputs);
2490}
2491
Douglas Gregor14454802011-02-25 02:25:35 +00002492NestedNameSpecifierLoc
2493Sema::SubstNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS,
2494 const MultiLevelTemplateArgumentList &TemplateArgs) {
2495 if (!NNS)
2496 return NestedNameSpecifierLoc();
2497
2498 TemplateInstantiator Instantiator(*this, TemplateArgs, NNS.getBeginLoc(),
2499 DeclarationName());
2500 return Instantiator.TransformNestedNameSpecifierLoc(NNS);
2501}
2502
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002503/// \brief Do template substitution on declaration name info.
2504DeclarationNameInfo
2505Sema::SubstDeclarationNameInfo(const DeclarationNameInfo &NameInfo,
2506 const MultiLevelTemplateArgumentList &TemplateArgs) {
2507 TemplateInstantiator Instantiator(*this, TemplateArgs, NameInfo.getLoc(),
2508 NameInfo.getName());
2509 return Instantiator.TransformDeclarationNameInfo(NameInfo);
2510}
2511
Douglas Gregoraa594892009-03-31 18:38:02 +00002512TemplateName
Douglas Gregordf846d12011-03-02 18:46:51 +00002513Sema::SubstTemplateName(NestedNameSpecifierLoc QualifierLoc,
2514 TemplateName Name, SourceLocation Loc,
Douglas Gregor01afeef2009-08-28 20:31:08 +00002515 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor71dc5092009-08-06 06:41:21 +00002516 TemplateInstantiator Instantiator(*this, TemplateArgs, Loc,
2517 DeclarationName());
Douglas Gregordf846d12011-03-02 18:46:51 +00002518 CXXScopeSpec SS;
2519 SS.Adopt(QualifierLoc);
2520 return Instantiator.TransformTemplateName(SS, Name, Loc);
Douglas Gregoraa594892009-03-31 18:38:02 +00002521}
Douglas Gregorc43620d2009-06-11 00:06:24 +00002522
Douglas Gregor0f3feb42010-12-22 21:19:48 +00002523bool Sema::Subst(const TemplateArgumentLoc *Args, unsigned NumArgs,
2524 TemplateArgumentListInfo &Result,
John McCall0ad16662009-10-29 08:12:44 +00002525 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregore922c772009-08-04 22:27:00 +00002526 TemplateInstantiator Instantiator(*this, TemplateArgs, SourceLocation(),
2527 DeclarationName());
Douglas Gregor0f3feb42010-12-22 21:19:48 +00002528
2529 return Instantiator.TransformTemplateArguments(Args, NumArgs, Result);
Douglas Gregorc43620d2009-06-11 00:06:24 +00002530}
Douglas Gregor14cf7522010-04-30 18:55:50 +00002531
Douglas Gregorf3010112011-01-07 16:43:16 +00002532llvm::PointerUnion<Decl *, LocalInstantiationScope::DeclArgumentPack *> *
2533LocalInstantiationScope::findInstantiationOf(const Decl *D) {
Chris Lattnercab02a62011-02-17 20:34:02 +00002534 for (LocalInstantiationScope *Current = this; Current;
Douglas Gregor14cf7522010-04-30 18:55:50 +00002535 Current = Current->Outer) {
Chris Lattnercab02a62011-02-17 20:34:02 +00002536
Douglas Gregor14cf7522010-04-30 18:55:50 +00002537 // Check if we found something within this scope.
Douglas Gregore9fc8dc2010-12-21 21:22:51 +00002538 const Decl *CheckD = D;
2539 do {
Douglas Gregorf3010112011-01-07 16:43:16 +00002540 LocalDeclsMap::iterator Found = Current->LocalDecls.find(CheckD);
Douglas Gregore9fc8dc2010-12-21 21:22:51 +00002541 if (Found != Current->LocalDecls.end())
Douglas Gregorf3010112011-01-07 16:43:16 +00002542 return &Found->second;
Douglas Gregore9fc8dc2010-12-21 21:22:51 +00002543
2544 // If this is a tag declaration, it's possible that we need to look for
2545 // a previous declaration.
2546 if (const TagDecl *Tag = dyn_cast<TagDecl>(CheckD))
Douglas Gregorec9fd132012-01-14 16:38:05 +00002547 CheckD = Tag->getPreviousDecl();
Douglas Gregore9fc8dc2010-12-21 21:22:51 +00002548 else
2549 CheckD = 0;
2550 } while (CheckD);
2551
Douglas Gregor14cf7522010-04-30 18:55:50 +00002552 // If we aren't combined with our outer scope, we're done.
2553 if (!Current->CombineWithOuterScope)
2554 break;
2555 }
Chris Lattnercab02a62011-02-17 20:34:02 +00002556
2557 // If we didn't find the decl, then we either have a sema bug, or we have a
2558 // forward reference to a label declaration. Return null to indicate that
2559 // we have an uninstantiated label.
2560 assert(isa<LabelDecl>(D) && "declaration not instantiated in this scope");
Douglas Gregor14cf7522010-04-30 18:55:50 +00002561 return 0;
2562}
2563
John McCall19c1bfd2010-08-25 05:32:35 +00002564void LocalInstantiationScope::InstantiatedLocal(const Decl *D, Decl *Inst) {
Douglas Gregorf3010112011-01-07 16:43:16 +00002565 llvm::PointerUnion<Decl *, DeclArgumentPack *> &Stored = LocalDecls[D];
Douglas Gregora8bac7f2011-01-10 07:32:04 +00002566 if (Stored.isNull())
2567 Stored = Inst;
2568 else if (Stored.is<Decl *>()) {
2569 assert(Stored.get<Decl *>() == Inst && "Already instantiated this local");
2570 Stored = Inst;
2571 } else
2572 LocalDecls[D].get<DeclArgumentPack *>()->push_back(Inst);
Douglas Gregor14cf7522010-04-30 18:55:50 +00002573}
Douglas Gregorf3010112011-01-07 16:43:16 +00002574
2575void LocalInstantiationScope::InstantiatedLocalPackArg(const Decl *D,
2576 Decl *Inst) {
2577 DeclArgumentPack *Pack = LocalDecls[D].get<DeclArgumentPack *>();
2578 Pack->push_back(Inst);
2579}
2580
2581void LocalInstantiationScope::MakeInstantiatedLocalArgPack(const Decl *D) {
2582 llvm::PointerUnion<Decl *, DeclArgumentPack *> &Stored = LocalDecls[D];
2583 assert(Stored.isNull() && "Already instantiated this local");
2584 DeclArgumentPack *Pack = new DeclArgumentPack;
2585 Stored = Pack;
2586 ArgumentPacks.push_back(Pack);
2587}
2588
Douglas Gregora8bac7f2011-01-10 07:32:04 +00002589void LocalInstantiationScope::SetPartiallySubstitutedPack(NamedDecl *Pack,
2590 const TemplateArgument *ExplicitArgs,
2591 unsigned NumExplicitArgs) {
2592 assert((!PartiallySubstitutedPack || PartiallySubstitutedPack == Pack) &&
2593 "Already have a partially-substituted pack");
2594 assert((!PartiallySubstitutedPack
2595 || NumArgsInPartiallySubstitutedPack == NumExplicitArgs) &&
2596 "Wrong number of arguments in partially-substituted pack");
2597 PartiallySubstitutedPack = Pack;
2598 ArgsInPartiallySubstitutedPack = ExplicitArgs;
2599 NumArgsInPartiallySubstitutedPack = NumExplicitArgs;
2600}
2601
2602NamedDecl *LocalInstantiationScope::getPartiallySubstitutedPack(
2603 const TemplateArgument **ExplicitArgs,
2604 unsigned *NumExplicitArgs) const {
2605 if (ExplicitArgs)
2606 *ExplicitArgs = 0;
2607 if (NumExplicitArgs)
2608 *NumExplicitArgs = 0;
2609
2610 for (const LocalInstantiationScope *Current = this; Current;
2611 Current = Current->Outer) {
2612 if (Current->PartiallySubstitutedPack) {
2613 if (ExplicitArgs)
2614 *ExplicitArgs = Current->ArgsInPartiallySubstitutedPack;
2615 if (NumExplicitArgs)
2616 *NumExplicitArgs = Current->NumArgsInPartiallySubstitutedPack;
2617
2618 return Current->PartiallySubstitutedPack;
2619 }
2620
2621 if (!Current->CombineWithOuterScope)
2622 break;
2623 }
2624
2625 return 0;
2626}