blob: 907a7e24a912d04055d4eb7d06d757e20ba16661 [file] [log] [blame]
Douglas Gregorfe1e1102009-02-27 19:31:52 +00001//===------- SemaTemplateInstantiate.cpp - C++ Template Instantiation ------===/
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//===----------------------------------------------------------------------===/
8//
9// This file implements C++ template instantiation.
10//
11//===----------------------------------------------------------------------===/
12
John McCall83024632010-08-25 22:03:47 +000013#include "clang/Sema/SemaInternal.h"
Douglas Gregord6ff3322009-08-04 16:50:30 +000014#include "TreeTransform.h"
John McCall8b0666c2010-08-20 18:27:03 +000015#include "clang/Sema/DeclSpec.h"
Douglas Gregorc3a6ade2010-08-12 20:07:10 +000016#include "clang/Sema/Lookup.h"
John McCallde6836a2010-08-24 07:21:54 +000017#include "clang/Sema/Template.h"
John McCall19c1bfd2010-08-25 05:32:35 +000018#include "clang/Sema/TemplateDeduction.h"
Douglas Gregor28ad4b52009-05-26 20:50:29 +000019#include "clang/AST/ASTConsumer.h"
Douglas Gregorfe1e1102009-02-27 19:31:52 +000020#include "clang/AST/ASTContext.h"
21#include "clang/AST/Expr.h"
Douglas Gregorfe1e1102009-02-27 19:31:52 +000022#include "clang/AST/DeclTemplate.h"
Douglas Gregorfe1e1102009-02-27 19:31:52 +000023#include "clang/Basic/LangOptions.h"
24
25using namespace clang;
John McCall19c1bfd2010-08-25 05:32:35 +000026using namespace sema;
Douglas Gregorfe1e1102009-02-27 19:31:52 +000027
Douglas Gregor4ea568f2009-03-10 18:03:33 +000028//===----------------------------------------------------------------------===/
29// Template Instantiation Support
30//===----------------------------------------------------------------------===/
31
Douglas Gregor01afeef2009-08-28 20:31:08 +000032/// \brief Retrieve the template argument list(s) that should be used to
33/// instantiate the definition of the given declaration.
Douglas Gregor36d7c5f2009-11-09 19:17:50 +000034///
35/// \param D the declaration for which we are computing template instantiation
36/// arguments.
37///
38/// \param Innermost if non-NULL, the innermost template argument list.
Douglas Gregor8c702532010-02-05 07:33:43 +000039///
40/// \param RelativeToPrimary true if we should get the template
41/// arguments relative to the primary template, even when we're
42/// dealing with a specialization. This is only relevant for function
43/// template specializations.
Douglas Gregor1bd7a942010-05-03 23:29:10 +000044///
45/// \param Pattern If non-NULL, indicates the pattern from which we will be
46/// instantiating the definition of the given declaration, \p D. This is
47/// used to determine the proper set of template instantiation arguments for
48/// friend function template specializations.
Douglas Gregora654dd82009-08-28 17:37:35 +000049MultiLevelTemplateArgumentList
Douglas Gregor36d7c5f2009-11-09 19:17:50 +000050Sema::getTemplateInstantiationArgs(NamedDecl *D,
Douglas Gregor8c702532010-02-05 07:33:43 +000051 const TemplateArgumentList *Innermost,
Douglas Gregor1bd7a942010-05-03 23:29:10 +000052 bool RelativeToPrimary,
53 const FunctionDecl *Pattern) {
Douglas Gregora654dd82009-08-28 17:37:35 +000054 // Accumulate the set of template argument lists in this structure.
55 MultiLevelTemplateArgumentList Result;
Mike Stump11289f42009-09-09 15:08:12 +000056
Douglas Gregor36d7c5f2009-11-09 19:17:50 +000057 if (Innermost)
58 Result.addOuterTemplateArguments(Innermost);
59
Douglas Gregora654dd82009-08-28 17:37:35 +000060 DeclContext *Ctx = dyn_cast<DeclContext>(D);
Douglas Gregora51c9cc2011-05-22 00:21:10 +000061 if (!Ctx) {
Douglas Gregora654dd82009-08-28 17:37:35 +000062 Ctx = D->getDeclContext();
Douglas Gregora51c9cc2011-05-22 00:21:10 +000063
64 assert((!D->isTemplateParameter() || !Ctx->isTranslationUnit()) &&
65 "Template parameter doesn't have its context yet!");
66 }
67
John McCall970d5302009-08-29 03:16:09 +000068 while (!Ctx->isFileContext()) {
Douglas Gregora654dd82009-08-28 17:37:35 +000069 // Add template arguments from a class template instantiation.
Mike Stump11289f42009-09-09 15:08:12 +000070 if (ClassTemplateSpecializationDecl *Spec
Douglas Gregora654dd82009-08-28 17:37:35 +000071 = dyn_cast<ClassTemplateSpecializationDecl>(Ctx)) {
72 // We're done when we hit an explicit specialization.
Douglas Gregor9961ce92010-07-08 18:37:38 +000073 if (Spec->getSpecializationKind() == TSK_ExplicitSpecialization &&
74 !isa<ClassTemplatePartialSpecializationDecl>(Spec))
Douglas Gregora654dd82009-08-28 17:37:35 +000075 break;
Mike Stump11289f42009-09-09 15:08:12 +000076
Douglas Gregora654dd82009-08-28 17:37:35 +000077 Result.addOuterTemplateArguments(&Spec->getTemplateInstantiationArgs());
Douglas Gregorcf915552009-10-13 16:30:37 +000078
79 // If this class template specialization was instantiated from a
80 // specialized member that is a class template, we're done.
81 assert(Spec->getSpecializedTemplate() && "No class template?");
82 if (Spec->getSpecializedTemplate()->isMemberSpecialization())
83 break;
Mike Stump11289f42009-09-09 15:08:12 +000084 }
Douglas Gregora654dd82009-08-28 17:37:35 +000085 // Add template arguments from a function template specialization.
John McCall970d5302009-08-29 03:16:09 +000086 else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Ctx)) {
Douglas Gregor8c702532010-02-05 07:33:43 +000087 if (!RelativeToPrimary &&
88 Function->getTemplateSpecializationKind()
89 == TSK_ExplicitSpecialization)
Douglas Gregorcf915552009-10-13 16:30:37 +000090 break;
91
Douglas Gregora654dd82009-08-28 17:37:35 +000092 if (const TemplateArgumentList *TemplateArgs
Douglas Gregorcf915552009-10-13 16:30:37 +000093 = Function->getTemplateSpecializationArgs()) {
94 // Add the template arguments for this specialization.
Douglas Gregora654dd82009-08-28 17:37:35 +000095 Result.addOuterTemplateArguments(TemplateArgs);
John McCall970d5302009-08-29 03:16:09 +000096
Douglas Gregorcf915552009-10-13 16:30:37 +000097 // If this function was instantiated from a specialized member that is
98 // a function template, we're done.
99 assert(Function->getPrimaryTemplate() && "No function template?");
100 if (Function->getPrimaryTemplate()->isMemberSpecialization())
101 break;
Douglas Gregor43669f82011-03-05 17:54:25 +0000102 } else if (FunctionTemplateDecl *FunTmpl
103 = Function->getDescribedFunctionTemplate()) {
104 // Add the "injected" template arguments.
105 std::pair<const TemplateArgument *, unsigned>
106 Injected = FunTmpl->getInjectedTemplateArgs();
107 Result.addOuterTemplateArguments(Injected.first, Injected.second);
Douglas Gregorcf915552009-10-13 16:30:37 +0000108 }
109
John McCall970d5302009-08-29 03:16:09 +0000110 // If this is a friend declaration and it declares an entity at
111 // namespace scope, take arguments from its lexical parent
Douglas Gregor1bd7a942010-05-03 23:29:10 +0000112 // instead of its semantic parent, unless of course the pattern we're
113 // instantiating actually comes from the file's context!
John McCall970d5302009-08-29 03:16:09 +0000114 if (Function->getFriendObjectKind() &&
Douglas Gregor1bd7a942010-05-03 23:29:10 +0000115 Function->getDeclContext()->isFileContext() &&
116 (!Pattern || !Pattern->getLexicalDeclContext()->isFileContext())) {
John McCall970d5302009-08-29 03:16:09 +0000117 Ctx = Function->getLexicalDeclContext();
Douglas Gregor8c702532010-02-05 07:33:43 +0000118 RelativeToPrimary = false;
John McCall970d5302009-08-29 03:16:09 +0000119 continue;
120 }
Douglas Gregor9961ce92010-07-08 18:37:38 +0000121 } else if (CXXRecordDecl *Rec = dyn_cast<CXXRecordDecl>(Ctx)) {
122 if (ClassTemplateDecl *ClassTemplate = Rec->getDescribedClassTemplate()) {
123 QualType T = ClassTemplate->getInjectedClassNameSpecialization();
124 const TemplateSpecializationType *TST
125 = cast<TemplateSpecializationType>(Context.getCanonicalType(T));
126 Result.addOuterTemplateArguments(TST->getArgs(), TST->getNumArgs());
127 if (ClassTemplate->isMemberSpecialization())
128 break;
129 }
Douglas Gregora654dd82009-08-28 17:37:35 +0000130 }
John McCall970d5302009-08-29 03:16:09 +0000131
132 Ctx = Ctx->getParent();
Douglas Gregor8c702532010-02-05 07:33:43 +0000133 RelativeToPrimary = false;
Douglas Gregorb4850462009-05-14 23:26:13 +0000134 }
Mike Stump11289f42009-09-09 15:08:12 +0000135
Douglas Gregora654dd82009-08-28 17:37:35 +0000136 return Result;
Douglas Gregorb4850462009-05-14 23:26:13 +0000137}
138
Douglas Gregor84d49a22009-11-11 21:54:23 +0000139bool Sema::ActiveTemplateInstantiation::isInstantiationRecord() const {
140 switch (Kind) {
141 case TemplateInstantiation:
142 case DefaultTemplateArgumentInstantiation:
143 case DefaultFunctionArgumentInstantiation:
144 return true;
145
146 case ExplicitTemplateArgumentSubstitution:
147 case DeducedTemplateArgumentSubstitution:
148 case PriorTemplateArgumentSubstitution:
149 case DefaultTemplateArgumentChecking:
150 return false;
151 }
152
153 return true;
154}
155
Douglas Gregorfcd5db32009-03-10 00:06:19 +0000156Sema::InstantiatingTemplate::
157InstantiatingTemplate(Sema &SemaRef, SourceLocation PointOfInstantiation,
Douglas Gregor85673582009-05-18 17:01:57 +0000158 Decl *Entity,
Douglas Gregorfcd5db32009-03-10 00:06:19 +0000159 SourceRange InstantiationRange)
Douglas Gregoredb76852011-01-27 22:31:44 +0000160 : SemaRef(SemaRef),
161 SavedInNonInstantiationSFINAEContext(
162 SemaRef.InNonInstantiationSFINAEContext)
163{
Douglas Gregor79cf6032009-03-10 20:44:00 +0000164 Invalid = CheckInstantiationDepth(PointOfInstantiation,
165 InstantiationRange);
166 if (!Invalid) {
Douglas Gregorfcd5db32009-03-10 00:06:19 +0000167 ActiveTemplateInstantiation Inst;
Douglas Gregor79cf6032009-03-10 20:44:00 +0000168 Inst.Kind = ActiveTemplateInstantiation::TemplateInstantiation;
Douglas Gregorfcd5db32009-03-10 00:06:19 +0000169 Inst.PointOfInstantiation = PointOfInstantiation;
Douglas Gregor79cf6032009-03-10 20:44:00 +0000170 Inst.Entity = reinterpret_cast<uintptr_t>(Entity);
Douglas Gregorc9220832009-03-12 18:36:18 +0000171 Inst.TemplateArgs = 0;
172 Inst.NumTemplateArgs = 0;
Douglas Gregor79cf6032009-03-10 20:44:00 +0000173 Inst.InstantiationRange = InstantiationRange;
Douglas Gregoredb76852011-01-27 22:31:44 +0000174 SemaRef.InNonInstantiationSFINAEContext = false;
Douglas Gregor79cf6032009-03-10 20:44:00 +0000175 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
Douglas Gregor79cf6032009-03-10 20:44:00 +0000176 }
177}
178
Mike Stump11289f42009-09-09 15:08:12 +0000179Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef,
Douglas Gregor79cf6032009-03-10 20:44:00 +0000180 SourceLocation PointOfInstantiation,
181 TemplateDecl *Template,
182 const TemplateArgument *TemplateArgs,
183 unsigned NumTemplateArgs,
184 SourceRange InstantiationRange)
Douglas Gregoredb76852011-01-27 22:31:44 +0000185 : SemaRef(SemaRef),
186 SavedInNonInstantiationSFINAEContext(
187 SemaRef.InNonInstantiationSFINAEContext)
188{
Douglas Gregor79cf6032009-03-10 20:44:00 +0000189 Invalid = CheckInstantiationDepth(PointOfInstantiation,
190 InstantiationRange);
191 if (!Invalid) {
192 ActiveTemplateInstantiation Inst;
Mike Stump11289f42009-09-09 15:08:12 +0000193 Inst.Kind
Douglas Gregor79cf6032009-03-10 20:44:00 +0000194 = ActiveTemplateInstantiation::DefaultTemplateArgumentInstantiation;
195 Inst.PointOfInstantiation = PointOfInstantiation;
196 Inst.Entity = reinterpret_cast<uintptr_t>(Template);
197 Inst.TemplateArgs = TemplateArgs;
198 Inst.NumTemplateArgs = NumTemplateArgs;
Douglas Gregorfcd5db32009-03-10 00:06:19 +0000199 Inst.InstantiationRange = InstantiationRange;
Douglas Gregoredb76852011-01-27 22:31:44 +0000200 SemaRef.InNonInstantiationSFINAEContext = false;
Douglas Gregorfcd5db32009-03-10 00:06:19 +0000201 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
Douglas Gregorfcd5db32009-03-10 00:06:19 +0000202 }
203}
204
Mike Stump11289f42009-09-09 15:08:12 +0000205Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef,
Douglas Gregor637d9982009-06-10 23:47:09 +0000206 SourceLocation PointOfInstantiation,
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000207 FunctionTemplateDecl *FunctionTemplate,
208 const TemplateArgument *TemplateArgs,
209 unsigned NumTemplateArgs,
210 ActiveTemplateInstantiation::InstantiationKind Kind,
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +0000211 sema::TemplateDeductionInfo &DeductionInfo,
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000212 SourceRange InstantiationRange)
Douglas Gregoredb76852011-01-27 22:31:44 +0000213 : SemaRef(SemaRef),
214 SavedInNonInstantiationSFINAEContext(
215 SemaRef.InNonInstantiationSFINAEContext)
216{
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000217 Invalid = CheckInstantiationDepth(PointOfInstantiation,
218 InstantiationRange);
219 if (!Invalid) {
220 ActiveTemplateInstantiation Inst;
221 Inst.Kind = Kind;
222 Inst.PointOfInstantiation = PointOfInstantiation;
223 Inst.Entity = reinterpret_cast<uintptr_t>(FunctionTemplate);
224 Inst.TemplateArgs = TemplateArgs;
225 Inst.NumTemplateArgs = NumTemplateArgs;
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +0000226 Inst.DeductionInfo = &DeductionInfo;
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000227 Inst.InstantiationRange = InstantiationRange;
Douglas Gregoredb76852011-01-27 22:31:44 +0000228 SemaRef.InNonInstantiationSFINAEContext = false;
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000229 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
Douglas Gregor84d49a22009-11-11 21:54:23 +0000230
231 if (!Inst.isInstantiationRecord())
232 ++SemaRef.NonInstantiationEntries;
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000233 }
234}
235
Mike Stump11289f42009-09-09 15:08:12 +0000236Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef,
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000237 SourceLocation PointOfInstantiation,
Douglas Gregor637d9982009-06-10 23:47:09 +0000238 ClassTemplatePartialSpecializationDecl *PartialSpec,
239 const TemplateArgument *TemplateArgs,
240 unsigned NumTemplateArgs,
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +0000241 sema::TemplateDeductionInfo &DeductionInfo,
Douglas Gregor637d9982009-06-10 23:47:09 +0000242 SourceRange InstantiationRange)
Douglas Gregoredb76852011-01-27 22:31:44 +0000243 : SemaRef(SemaRef),
244 SavedInNonInstantiationSFINAEContext(
245 SemaRef.InNonInstantiationSFINAEContext)
246{
Douglas Gregor84d49a22009-11-11 21:54:23 +0000247 Invalid = false;
248
249 ActiveTemplateInstantiation Inst;
250 Inst.Kind = ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution;
251 Inst.PointOfInstantiation = PointOfInstantiation;
252 Inst.Entity = reinterpret_cast<uintptr_t>(PartialSpec);
253 Inst.TemplateArgs = TemplateArgs;
254 Inst.NumTemplateArgs = NumTemplateArgs;
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +0000255 Inst.DeductionInfo = &DeductionInfo;
Douglas Gregor84d49a22009-11-11 21:54:23 +0000256 Inst.InstantiationRange = InstantiationRange;
Douglas Gregoredb76852011-01-27 22:31:44 +0000257 SemaRef.InNonInstantiationSFINAEContext = false;
Douglas Gregor84d49a22009-11-11 21:54:23 +0000258 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
259
260 assert(!Inst.isInstantiationRecord());
261 ++SemaRef.NonInstantiationEntries;
Douglas Gregor637d9982009-06-10 23:47:09 +0000262}
263
Mike Stump11289f42009-09-09 15:08:12 +0000264Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef,
Douglas Gregore62e6a02009-11-11 19:13:48 +0000265 SourceLocation PointOfInstantiation,
Anders Carlsson657bad42009-09-05 05:14:19 +0000266 ParmVarDecl *Param,
267 const TemplateArgument *TemplateArgs,
268 unsigned NumTemplateArgs,
269 SourceRange InstantiationRange)
Douglas Gregoredb76852011-01-27 22:31:44 +0000270 : SemaRef(SemaRef),
271 SavedInNonInstantiationSFINAEContext(
272 SemaRef.InNonInstantiationSFINAEContext)
273{
Douglas Gregore62e6a02009-11-11 19:13:48 +0000274 Invalid = CheckInstantiationDepth(PointOfInstantiation, InstantiationRange);
Anders Carlsson657bad42009-09-05 05:14:19 +0000275
276 if (!Invalid) {
277 ActiveTemplateInstantiation Inst;
278 Inst.Kind
279 = ActiveTemplateInstantiation::DefaultFunctionArgumentInstantiation;
Douglas Gregore62e6a02009-11-11 19:13:48 +0000280 Inst.PointOfInstantiation = PointOfInstantiation;
Anders Carlsson657bad42009-09-05 05:14:19 +0000281 Inst.Entity = reinterpret_cast<uintptr_t>(Param);
282 Inst.TemplateArgs = TemplateArgs;
283 Inst.NumTemplateArgs = NumTemplateArgs;
284 Inst.InstantiationRange = InstantiationRange;
Douglas Gregoredb76852011-01-27 22:31:44 +0000285 SemaRef.InNonInstantiationSFINAEContext = false;
Anders Carlsson657bad42009-09-05 05:14:19 +0000286 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
Douglas Gregore62e6a02009-11-11 19:13:48 +0000287 }
288}
289
290Sema::InstantiatingTemplate::
291InstantiatingTemplate(Sema &SemaRef, SourceLocation PointOfInstantiation,
Douglas Gregorca4686d2011-01-04 23:35:54 +0000292 NamedDecl *Template,
Douglas Gregore62e6a02009-11-11 19:13:48 +0000293 NonTypeTemplateParmDecl *Param,
294 const TemplateArgument *TemplateArgs,
295 unsigned NumTemplateArgs,
Douglas Gregoredb76852011-01-27 22:31:44 +0000296 SourceRange InstantiationRange)
297 : SemaRef(SemaRef),
298 SavedInNonInstantiationSFINAEContext(
299 SemaRef.InNonInstantiationSFINAEContext)
300{
Douglas Gregor84d49a22009-11-11 21:54:23 +0000301 Invalid = false;
Douglas Gregore62e6a02009-11-11 19:13:48 +0000302
Douglas Gregor84d49a22009-11-11 21:54:23 +0000303 ActiveTemplateInstantiation Inst;
304 Inst.Kind = ActiveTemplateInstantiation::PriorTemplateArgumentSubstitution;
305 Inst.PointOfInstantiation = PointOfInstantiation;
306 Inst.Template = Template;
307 Inst.Entity = reinterpret_cast<uintptr_t>(Param);
308 Inst.TemplateArgs = TemplateArgs;
309 Inst.NumTemplateArgs = NumTemplateArgs;
310 Inst.InstantiationRange = InstantiationRange;
Douglas Gregoredb76852011-01-27 22:31:44 +0000311 SemaRef.InNonInstantiationSFINAEContext = false;
Douglas Gregor84d49a22009-11-11 21:54:23 +0000312 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
313
314 assert(!Inst.isInstantiationRecord());
315 ++SemaRef.NonInstantiationEntries;
Douglas Gregore62e6a02009-11-11 19:13:48 +0000316}
317
318Sema::InstantiatingTemplate::
319InstantiatingTemplate(Sema &SemaRef, SourceLocation PointOfInstantiation,
Douglas Gregorca4686d2011-01-04 23:35:54 +0000320 NamedDecl *Template,
Douglas Gregore62e6a02009-11-11 19:13:48 +0000321 TemplateTemplateParmDecl *Param,
322 const TemplateArgument *TemplateArgs,
323 unsigned NumTemplateArgs,
Douglas Gregoredb76852011-01-27 22:31:44 +0000324 SourceRange InstantiationRange)
325 : SemaRef(SemaRef),
326 SavedInNonInstantiationSFINAEContext(
327 SemaRef.InNonInstantiationSFINAEContext)
328{
Douglas Gregor84d49a22009-11-11 21:54:23 +0000329 Invalid = false;
330 ActiveTemplateInstantiation Inst;
331 Inst.Kind = ActiveTemplateInstantiation::PriorTemplateArgumentSubstitution;
332 Inst.PointOfInstantiation = PointOfInstantiation;
333 Inst.Template = Template;
334 Inst.Entity = reinterpret_cast<uintptr_t>(Param);
335 Inst.TemplateArgs = TemplateArgs;
336 Inst.NumTemplateArgs = NumTemplateArgs;
337 Inst.InstantiationRange = InstantiationRange;
Douglas Gregoredb76852011-01-27 22:31:44 +0000338 SemaRef.InNonInstantiationSFINAEContext = false;
Douglas Gregor84d49a22009-11-11 21:54:23 +0000339 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
Douglas Gregore62e6a02009-11-11 19:13:48 +0000340
Douglas Gregor84d49a22009-11-11 21:54:23 +0000341 assert(!Inst.isInstantiationRecord());
342 ++SemaRef.NonInstantiationEntries;
343}
344
345Sema::InstantiatingTemplate::
346InstantiatingTemplate(Sema &SemaRef, SourceLocation PointOfInstantiation,
347 TemplateDecl *Template,
348 NamedDecl *Param,
349 const TemplateArgument *TemplateArgs,
350 unsigned NumTemplateArgs,
Douglas Gregoredb76852011-01-27 22:31:44 +0000351 SourceRange InstantiationRange)
352 : SemaRef(SemaRef),
353 SavedInNonInstantiationSFINAEContext(
354 SemaRef.InNonInstantiationSFINAEContext)
355{
Douglas Gregor84d49a22009-11-11 21:54:23 +0000356 Invalid = false;
357
358 ActiveTemplateInstantiation Inst;
359 Inst.Kind = ActiveTemplateInstantiation::DefaultTemplateArgumentChecking;
360 Inst.PointOfInstantiation = PointOfInstantiation;
361 Inst.Template = Template;
362 Inst.Entity = reinterpret_cast<uintptr_t>(Param);
363 Inst.TemplateArgs = TemplateArgs;
364 Inst.NumTemplateArgs = NumTemplateArgs;
365 Inst.InstantiationRange = InstantiationRange;
Douglas Gregoredb76852011-01-27 22:31:44 +0000366 SemaRef.InNonInstantiationSFINAEContext = false;
Douglas Gregor84d49a22009-11-11 21:54:23 +0000367 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
368
369 assert(!Inst.isInstantiationRecord());
370 ++SemaRef.NonInstantiationEntries;
Anders Carlsson657bad42009-09-05 05:14:19 +0000371}
372
Douglas Gregor85673582009-05-18 17:01:57 +0000373void Sema::InstantiatingTemplate::Clear() {
374 if (!Invalid) {
Douglas Gregor84d49a22009-11-11 21:54:23 +0000375 if (!SemaRef.ActiveTemplateInstantiations.back().isInstantiationRecord()) {
376 assert(SemaRef.NonInstantiationEntries > 0);
377 --SemaRef.NonInstantiationEntries;
378 }
Douglas Gregoredb76852011-01-27 22:31:44 +0000379 SemaRef.InNonInstantiationSFINAEContext
380 = SavedInNonInstantiationSFINAEContext;
Douglas Gregorfcd5db32009-03-10 00:06:19 +0000381 SemaRef.ActiveTemplateInstantiations.pop_back();
Douglas Gregor85673582009-05-18 17:01:57 +0000382 Invalid = true;
383 }
Douglas Gregorfcd5db32009-03-10 00:06:19 +0000384}
385
Douglas Gregor79cf6032009-03-10 20:44:00 +0000386bool Sema::InstantiatingTemplate::CheckInstantiationDepth(
387 SourceLocation PointOfInstantiation,
388 SourceRange InstantiationRange) {
Douglas Gregor84d49a22009-11-11 21:54:23 +0000389 assert(SemaRef.NonInstantiationEntries <=
390 SemaRef.ActiveTemplateInstantiations.size());
391 if ((SemaRef.ActiveTemplateInstantiations.size() -
392 SemaRef.NonInstantiationEntries)
393 <= SemaRef.getLangOptions().InstantiationDepth)
Douglas Gregor79cf6032009-03-10 20:44:00 +0000394 return false;
395
Mike Stump11289f42009-09-09 15:08:12 +0000396 SemaRef.Diag(PointOfInstantiation,
Douglas Gregor79cf6032009-03-10 20:44:00 +0000397 diag::err_template_recursion_depth_exceeded)
398 << SemaRef.getLangOptions().InstantiationDepth
399 << InstantiationRange;
400 SemaRef.Diag(PointOfInstantiation, diag::note_template_recursion_depth)
401 << SemaRef.getLangOptions().InstantiationDepth;
402 return true;
403}
404
Douglas Gregor4ea568f2009-03-10 18:03:33 +0000405/// \brief Prints the current instantiation stack through a series of
406/// notes.
407void Sema::PrintInstantiationStack() {
Douglas Gregorffed1cb2010-04-20 07:18:24 +0000408 // Determine which template instantiations to skip, if any.
409 unsigned SkipStart = ActiveTemplateInstantiations.size(), SkipEnd = SkipStart;
410 unsigned Limit = Diags.getTemplateBacktraceLimit();
411 if (Limit && Limit < ActiveTemplateInstantiations.size()) {
412 SkipStart = Limit / 2 + Limit % 2;
413 SkipEnd = ActiveTemplateInstantiations.size() - Limit / 2;
414 }
415
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000416 // FIXME: In all of these cases, we need to show the template arguments
Douglas Gregorffed1cb2010-04-20 07:18:24 +0000417 unsigned InstantiationIdx = 0;
Douglas Gregor4ea568f2009-03-10 18:03:33 +0000418 for (llvm::SmallVector<ActiveTemplateInstantiation, 16>::reverse_iterator
419 Active = ActiveTemplateInstantiations.rbegin(),
420 ActiveEnd = ActiveTemplateInstantiations.rend();
421 Active != ActiveEnd;
Douglas Gregorffed1cb2010-04-20 07:18:24 +0000422 ++Active, ++InstantiationIdx) {
423 // Skip this instantiation?
424 if (InstantiationIdx >= SkipStart && InstantiationIdx < SkipEnd) {
425 if (InstantiationIdx == SkipStart) {
426 // Note that we're skipping instantiations.
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000427 Diags.Report(Active->PointOfInstantiation,
Douglas Gregorffed1cb2010-04-20 07:18:24 +0000428 diag::note_instantiation_contexts_suppressed)
429 << unsigned(ActiveTemplateInstantiations.size() - Limit);
430 }
431 continue;
432 }
433
Douglas Gregor79cf6032009-03-10 20:44:00 +0000434 switch (Active->Kind) {
435 case ActiveTemplateInstantiation::TemplateInstantiation: {
Douglas Gregor85673582009-05-18 17:01:57 +0000436 Decl *D = reinterpret_cast<Decl *>(Active->Entity);
437 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) {
438 unsigned DiagID = diag::note_template_member_class_here;
439 if (isa<ClassTemplateSpecializationDecl>(Record))
440 DiagID = diag::note_template_class_instantiation_here;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000441 Diags.Report(Active->PointOfInstantiation, DiagID)
Douglas Gregor85673582009-05-18 17:01:57 +0000442 << Context.getTypeDeclType(Record)
443 << Active->InstantiationRange;
Douglas Gregora6ef8f02009-07-24 20:34:43 +0000444 } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
Douglas Gregor4adbc6d2009-06-26 00:10:03 +0000445 unsigned DiagID;
446 if (Function->getPrimaryTemplate())
447 DiagID = diag::note_function_template_spec_here;
448 else
449 DiagID = diag::note_template_member_function_here;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000450 Diags.Report(Active->PointOfInstantiation, DiagID)
Douglas Gregor85673582009-05-18 17:01:57 +0000451 << Function
452 << Active->InstantiationRange;
Richard Smith3f1b5d02011-05-05 21:57:07 +0000453 } else if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000454 Diags.Report(Active->PointOfInstantiation,
Douglas Gregora6ef8f02009-07-24 20:34:43 +0000455 diag::note_template_static_data_member_def_here)
Richard Smith3f1b5d02011-05-05 21:57:07 +0000456 << VD
457 << Active->InstantiationRange;
458 } else {
459 Diags.Report(Active->PointOfInstantiation,
460 diag::note_template_type_alias_instantiation_here)
461 << cast<TypeAliasTemplateDecl>(D)
Douglas Gregora6ef8f02009-07-24 20:34:43 +0000462 << Active->InstantiationRange;
Douglas Gregor85673582009-05-18 17:01:57 +0000463 }
Douglas Gregor79cf6032009-03-10 20:44:00 +0000464 break;
465 }
466
467 case ActiveTemplateInstantiation::DefaultTemplateArgumentInstantiation: {
468 TemplateDecl *Template = cast<TemplateDecl>((Decl *)Active->Entity);
469 std::string TemplateArgsStr
Douglas Gregordc572a32009-03-30 22:58:21 +0000470 = TemplateSpecializationType::PrintTemplateArgumentList(
Mike Stump11289f42009-09-09 15:08:12 +0000471 Active->TemplateArgs,
Douglas Gregor7de59662009-05-29 20:38:28 +0000472 Active->NumTemplateArgs,
473 Context.PrintingPolicy);
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000474 Diags.Report(Active->PointOfInstantiation,
Douglas Gregor79cf6032009-03-10 20:44:00 +0000475 diag::note_default_arg_instantiation_here)
476 << (Template->getNameAsString() + TemplateArgsStr)
477 << Active->InstantiationRange;
478 break;
479 }
Douglas Gregor637d9982009-06-10 23:47:09 +0000480
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000481 case ActiveTemplateInstantiation::ExplicitTemplateArgumentSubstitution: {
Mike Stump11289f42009-09-09 15:08:12 +0000482 FunctionTemplateDecl *FnTmpl
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000483 = cast<FunctionTemplateDecl>((Decl *)Active->Entity);
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000484 Diags.Report(Active->PointOfInstantiation,
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000485 diag::note_explicit_template_arg_substitution_here)
Douglas Gregor607f1412010-03-30 20:35:20 +0000486 << FnTmpl
487 << getTemplateArgumentBindingsText(FnTmpl->getTemplateParameters(),
488 Active->TemplateArgs,
489 Active->NumTemplateArgs)
490 << Active->InstantiationRange;
Douglas Gregor637d9982009-06-10 23:47:09 +0000491 break;
492 }
Mike Stump11289f42009-09-09 15:08:12 +0000493
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000494 case ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution:
495 if (ClassTemplatePartialSpecializationDecl *PartialSpec
496 = dyn_cast<ClassTemplatePartialSpecializationDecl>(
497 (Decl *)Active->Entity)) {
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000498 Diags.Report(Active->PointOfInstantiation,
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000499 diag::note_partial_spec_deduct_instantiation_here)
500 << Context.getTypeDeclType(PartialSpec)
Douglas Gregor607f1412010-03-30 20:35:20 +0000501 << getTemplateArgumentBindingsText(
502 PartialSpec->getTemplateParameters(),
503 Active->TemplateArgs,
504 Active->NumTemplateArgs)
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000505 << Active->InstantiationRange;
506 } else {
507 FunctionTemplateDecl *FnTmpl
508 = cast<FunctionTemplateDecl>((Decl *)Active->Entity);
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000509 Diags.Report(Active->PointOfInstantiation,
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000510 diag::note_function_template_deduction_instantiation_here)
Douglas Gregor607f1412010-03-30 20:35:20 +0000511 << FnTmpl
512 << getTemplateArgumentBindingsText(FnTmpl->getTemplateParameters(),
513 Active->TemplateArgs,
514 Active->NumTemplateArgs)
515 << Active->InstantiationRange;
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000516 }
517 break;
Douglas Gregor637d9982009-06-10 23:47:09 +0000518
Anders Carlsson657bad42009-09-05 05:14:19 +0000519 case ActiveTemplateInstantiation::DefaultFunctionArgumentInstantiation: {
520 ParmVarDecl *Param = cast<ParmVarDecl>((Decl *)Active->Entity);
521 FunctionDecl *FD = cast<FunctionDecl>(Param->getDeclContext());
Mike Stump11289f42009-09-09 15:08:12 +0000522
Anders Carlsson657bad42009-09-05 05:14:19 +0000523 std::string TemplateArgsStr
524 = TemplateSpecializationType::PrintTemplateArgumentList(
Mike Stump11289f42009-09-09 15:08:12 +0000525 Active->TemplateArgs,
Anders Carlsson657bad42009-09-05 05:14:19 +0000526 Active->NumTemplateArgs,
527 Context.PrintingPolicy);
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000528 Diags.Report(Active->PointOfInstantiation,
Anders Carlsson657bad42009-09-05 05:14:19 +0000529 diag::note_default_function_arg_instantiation_here)
Anders Carlssondc6d2c32009-09-05 05:38:54 +0000530 << (FD->getNameAsString() + TemplateArgsStr)
Anders Carlsson657bad42009-09-05 05:14:19 +0000531 << Active->InstantiationRange;
532 break;
533 }
Mike Stump11289f42009-09-09 15:08:12 +0000534
Douglas Gregore62e6a02009-11-11 19:13:48 +0000535 case ActiveTemplateInstantiation::PriorTemplateArgumentSubstitution: {
536 NamedDecl *Parm = cast<NamedDecl>((Decl *)Active->Entity);
537 std::string Name;
538 if (!Parm->getName().empty())
539 Name = std::string(" '") + Parm->getName().str() + "'";
Douglas Gregorca4686d2011-01-04 23:35:54 +0000540
541 TemplateParameterList *TemplateParams = 0;
542 if (TemplateDecl *Template = dyn_cast<TemplateDecl>(Active->Template))
543 TemplateParams = Template->getTemplateParameters();
544 else
545 TemplateParams =
546 cast<ClassTemplatePartialSpecializationDecl>(Active->Template)
547 ->getTemplateParameters();
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000548 Diags.Report(Active->PointOfInstantiation,
Douglas Gregore62e6a02009-11-11 19:13:48 +0000549 diag::note_prior_template_arg_substitution)
550 << isa<TemplateTemplateParmDecl>(Parm)
551 << Name
Douglas Gregorca4686d2011-01-04 23:35:54 +0000552 << getTemplateArgumentBindingsText(TemplateParams,
Douglas Gregore62e6a02009-11-11 19:13:48 +0000553 Active->TemplateArgs,
554 Active->NumTemplateArgs)
555 << Active->InstantiationRange;
556 break;
557 }
Douglas Gregor84d49a22009-11-11 21:54:23 +0000558
559 case ActiveTemplateInstantiation::DefaultTemplateArgumentChecking: {
Douglas Gregorca4686d2011-01-04 23:35:54 +0000560 TemplateParameterList *TemplateParams = 0;
561 if (TemplateDecl *Template = dyn_cast<TemplateDecl>(Active->Template))
562 TemplateParams = Template->getTemplateParameters();
563 else
564 TemplateParams =
565 cast<ClassTemplatePartialSpecializationDecl>(Active->Template)
566 ->getTemplateParameters();
567
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000568 Diags.Report(Active->PointOfInstantiation,
Douglas Gregor84d49a22009-11-11 21:54:23 +0000569 diag::note_template_default_arg_checking)
Douglas Gregorca4686d2011-01-04 23:35:54 +0000570 << getTemplateArgumentBindingsText(TemplateParams,
Douglas Gregor84d49a22009-11-11 21:54:23 +0000571 Active->TemplateArgs,
572 Active->NumTemplateArgs)
573 << Active->InstantiationRange;
574 break;
575 }
Douglas Gregor79cf6032009-03-10 20:44:00 +0000576 }
Douglas Gregor4ea568f2009-03-10 18:03:33 +0000577 }
578}
579
Douglas Gregoredb76852011-01-27 22:31:44 +0000580llvm::Optional<TemplateDeductionInfo *> Sema::isSFINAEContext() const {
Douglas Gregor33834512009-06-14 07:33:30 +0000581 using llvm::SmallVector;
Douglas Gregoredb76852011-01-27 22:31:44 +0000582 if (InNonInstantiationSFINAEContext)
583 return llvm::Optional<TemplateDeductionInfo *>(0);
584
Douglas Gregor33834512009-06-14 07:33:30 +0000585 for (SmallVector<ActiveTemplateInstantiation, 16>::const_reverse_iterator
586 Active = ActiveTemplateInstantiations.rbegin(),
587 ActiveEnd = ActiveTemplateInstantiations.rend();
588 Active != ActiveEnd;
Douglas Gregor84d49a22009-11-11 21:54:23 +0000589 ++Active)
590 {
Douglas Gregor33834512009-06-14 07:33:30 +0000591 switch(Active->Kind) {
Anders Carlsson657bad42009-09-05 05:14:19 +0000592 case ActiveTemplateInstantiation::DefaultFunctionArgumentInstantiation:
Douglas Gregoredb76852011-01-27 22:31:44 +0000593 case ActiveTemplateInstantiation::TemplateInstantiation:
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000594 // This is a template instantiation, so there is no SFINAE.
Douglas Gregoredb76852011-01-27 22:31:44 +0000595 return llvm::Optional<TemplateDeductionInfo *>();
Mike Stump11289f42009-09-09 15:08:12 +0000596
Douglas Gregor33834512009-06-14 07:33:30 +0000597 case ActiveTemplateInstantiation::DefaultTemplateArgumentInstantiation:
Douglas Gregore62e6a02009-11-11 19:13:48 +0000598 case ActiveTemplateInstantiation::PriorTemplateArgumentSubstitution:
Douglas Gregor84d49a22009-11-11 21:54:23 +0000599 case ActiveTemplateInstantiation::DefaultTemplateArgumentChecking:
Douglas Gregore62e6a02009-11-11 19:13:48 +0000600 // A default template argument instantiation and substitution into
601 // template parameters with arguments for prior parameters may or may
602 // not be a SFINAE context; look further up the stack.
Douglas Gregor33834512009-06-14 07:33:30 +0000603 break;
Mike Stump11289f42009-09-09 15:08:12 +0000604
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000605 case ActiveTemplateInstantiation::ExplicitTemplateArgumentSubstitution:
606 case ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution:
607 // We're either substitution explicitly-specified template arguments
608 // or deduced template arguments, so SFINAE applies.
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +0000609 assert(Active->DeductionInfo && "Missing deduction info pointer");
610 return Active->DeductionInfo;
Douglas Gregor33834512009-06-14 07:33:30 +0000611 }
612 }
613
Douglas Gregoredb76852011-01-27 22:31:44 +0000614 return llvm::Optional<TemplateDeductionInfo *>();
Douglas Gregor33834512009-06-14 07:33:30 +0000615}
616
Douglas Gregora8bac7f2011-01-10 07:32:04 +0000617/// \brief Retrieve the depth and index of a parameter pack.
618static std::pair<unsigned, unsigned>
619getDepthAndIndex(NamedDecl *ND) {
620 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(ND))
621 return std::make_pair(TTP->getDepth(), TTP->getIndex());
622
623 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(ND))
624 return std::make_pair(NTTP->getDepth(), NTTP->getIndex());
625
626 TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(ND);
627 return std::make_pair(TTP->getDepth(), TTP->getIndex());
628}
629
Douglas Gregorfe1e1102009-02-27 19:31:52 +0000630//===----------------------------------------------------------------------===/
631// Template Instantiation for Types
632//===----------------------------------------------------------------------===/
Douglas Gregor17c0d7b2009-02-28 00:25:32 +0000633namespace {
Douglas Gregor14cf7522010-04-30 18:55:50 +0000634 class TemplateInstantiator : public TreeTransform<TemplateInstantiator> {
Douglas Gregor01afeef2009-08-28 20:31:08 +0000635 const MultiLevelTemplateArgumentList &TemplateArgs;
Douglas Gregor17c0d7b2009-02-28 00:25:32 +0000636 SourceLocation Loc;
637 DeclarationName Entity;
Douglas Gregorfe1e1102009-02-27 19:31:52 +0000638
Douglas Gregor17c0d7b2009-02-28 00:25:32 +0000639 public:
Douglas Gregorebe10102009-08-20 07:17:43 +0000640 typedef TreeTransform<TemplateInstantiator> inherited;
Mike Stump11289f42009-09-09 15:08:12 +0000641
642 TemplateInstantiator(Sema &SemaRef,
Douglas Gregor01afeef2009-08-28 20:31:08 +0000643 const MultiLevelTemplateArgumentList &TemplateArgs,
Douglas Gregord6ff3322009-08-04 16:50:30 +0000644 SourceLocation Loc,
Mike Stump11289f42009-09-09 15:08:12 +0000645 DeclarationName Entity)
646 : inherited(SemaRef), TemplateArgs(TemplateArgs), Loc(Loc),
Douglas Gregorebe10102009-08-20 07:17:43 +0000647 Entity(Entity) { }
Douglas Gregor17c0d7b2009-02-28 00:25:32 +0000648
Mike Stump11289f42009-09-09 15:08:12 +0000649 /// \brief Determine whether the given type \p T has already been
Douglas Gregord6ff3322009-08-04 16:50:30 +0000650 /// transformed.
651 ///
652 /// For the purposes of template instantiation, a type has already been
653 /// transformed if it is NULL or if it is not dependent.
Douglas Gregor5597ab42010-05-07 23:12:07 +0000654 bool AlreadyTransformed(QualType T);
Mike Stump11289f42009-09-09 15:08:12 +0000655
Douglas Gregord6ff3322009-08-04 16:50:30 +0000656 /// \brief Returns the location of the entity being instantiated, if known.
657 SourceLocation getBaseLocation() { return Loc; }
Mike Stump11289f42009-09-09 15:08:12 +0000658
Douglas Gregord6ff3322009-08-04 16:50:30 +0000659 /// \brief Returns the name of the entity being instantiated, if any.
660 DeclarationName getBaseEntity() { return Entity; }
Mike Stump11289f42009-09-09 15:08:12 +0000661
Douglas Gregoref6ab412009-10-27 06:26:26 +0000662 /// \brief Sets the "base" location and entity when that
663 /// information is known based on another transformation.
664 void setBase(SourceLocation Loc, DeclarationName Entity) {
665 this->Loc = Loc;
666 this->Entity = Entity;
667 }
Douglas Gregor840bd6c2010-12-20 22:05:00 +0000668
669 bool TryExpandParameterPacks(SourceLocation EllipsisLoc,
670 SourceRange PatternRange,
671 const UnexpandedParameterPack *Unexpanded,
672 unsigned NumUnexpanded,
673 bool &ShouldExpand,
Douglas Gregora8bac7f2011-01-10 07:32:04 +0000674 bool &RetainExpansion,
Douglas Gregor0dca5fd2011-01-14 17:04:44 +0000675 llvm::Optional<unsigned> &NumExpansions) {
Douglas Gregor76aca7b2010-12-21 00:52:54 +0000676 return getSema().CheckParameterPacksForExpansion(EllipsisLoc,
677 PatternRange, Unexpanded,
678 NumUnexpanded,
679 TemplateArgs,
680 ShouldExpand,
Douglas Gregora8bac7f2011-01-10 07:32:04 +0000681 RetainExpansion,
Douglas Gregor76aca7b2010-12-21 00:52:54 +0000682 NumExpansions);
683 }
Douglas Gregor840bd6c2010-12-20 22:05:00 +0000684
Douglas Gregorf3010112011-01-07 16:43:16 +0000685 void ExpandingFunctionParameterPack(ParmVarDecl *Pack) {
686 SemaRef.CurrentInstantiationScope->MakeInstantiatedLocalArgPack(Pack);
687 }
688
Douglas Gregora8bac7f2011-01-10 07:32:04 +0000689 TemplateArgument ForgetPartiallySubstitutedPack() {
690 TemplateArgument Result;
691 if (NamedDecl *PartialPack
692 = SemaRef.CurrentInstantiationScope->getPartiallySubstitutedPack()){
693 MultiLevelTemplateArgumentList &TemplateArgs
694 = const_cast<MultiLevelTemplateArgumentList &>(this->TemplateArgs);
695 unsigned Depth, Index;
696 llvm::tie(Depth, Index) = getDepthAndIndex(PartialPack);
697 if (TemplateArgs.hasTemplateArgument(Depth, Index)) {
698 Result = TemplateArgs(Depth, Index);
699 TemplateArgs.setArgument(Depth, Index, TemplateArgument());
700 }
701 }
702
703 return Result;
704 }
705
706 void RememberPartiallySubstitutedPack(TemplateArgument Arg) {
707 if (Arg.isNull())
708 return;
709
710 if (NamedDecl *PartialPack
711 = SemaRef.CurrentInstantiationScope->getPartiallySubstitutedPack()){
712 MultiLevelTemplateArgumentList &TemplateArgs
713 = const_cast<MultiLevelTemplateArgumentList &>(this->TemplateArgs);
714 unsigned Depth, Index;
715 llvm::tie(Depth, Index) = getDepthAndIndex(PartialPack);
716 TemplateArgs.setArgument(Depth, Index, Arg);
717 }
718 }
719
Douglas Gregord6ff3322009-08-04 16:50:30 +0000720 /// \brief Transform the given declaration by instantiating a reference to
721 /// this declaration.
Douglas Gregora04f2ca2010-03-01 15:56:25 +0000722 Decl *TransformDecl(SourceLocation Loc, Decl *D);
Douglas Gregora16548e2009-08-11 05:31:07 +0000723
Mike Stump11289f42009-09-09 15:08:12 +0000724 /// \brief Transform the definition of the given declaration by
Douglas Gregorebe10102009-08-20 07:17:43 +0000725 /// instantiating it.
Douglas Gregor25289362010-03-01 17:25:41 +0000726 Decl *TransformDefinition(SourceLocation Loc, Decl *D);
Mike Stump11289f42009-09-09 15:08:12 +0000727
Douglas Gregora5cb6da2009-10-20 05:58:46 +0000728 /// \bried Transform the first qualifier within a scope by instantiating the
729 /// declaration.
730 NamedDecl *TransformFirstQualifierInScope(NamedDecl *D, SourceLocation Loc);
731
Douglas Gregorebe10102009-08-20 07:17:43 +0000732 /// \brief Rebuild the exception declaration and register the declaration
733 /// as an instantiated local.
Douglas Gregor9f0e1aa2010-09-09 17:09:21 +0000734 VarDecl *RebuildExceptionDecl(VarDecl *ExceptionDecl,
John McCallbcd03502009-12-07 02:54:59 +0000735 TypeSourceInfo *Declarator,
Abramo Bagnaradff19302011-03-08 08:55:46 +0000736 SourceLocation StartLoc,
737 SourceLocation NameLoc,
738 IdentifierInfo *Name);
Mike Stump11289f42009-09-09 15:08:12 +0000739
Douglas Gregorf4e837f2010-04-26 17:57:08 +0000740 /// \brief Rebuild the Objective-C exception declaration and register the
741 /// declaration as an instantiated local.
742 VarDecl *RebuildObjCExceptionDecl(VarDecl *ExceptionDecl,
743 TypeSourceInfo *TSInfo, QualType T);
744
John McCall7f41d982009-09-11 04:59:25 +0000745 /// \brief Check for tag mismatches when instantiating an
746 /// elaborated type.
John McCall954b5de2010-11-04 19:04:38 +0000747 QualType RebuildElaboratedType(SourceLocation KeywordLoc,
748 ElaboratedTypeKeyword Keyword,
Douglas Gregor844cb502011-03-01 18:12:44 +0000749 NestedNameSpecifierLoc QualifierLoc,
750 QualType T);
John McCall7f41d982009-09-11 04:59:25 +0000751
Douglas Gregor9db53502011-03-02 18:07:45 +0000752 TemplateName TransformTemplateName(CXXScopeSpec &SS,
753 TemplateName Name,
754 SourceLocation NameLoc,
755 QualType ObjectType = QualType(),
756 NamedDecl *FirstQualifierInScope = 0);
757
John McCalldadc5752010-08-24 06:29:42 +0000758 ExprResult TransformPredefinedExpr(PredefinedExpr *E);
759 ExprResult TransformDeclRefExpr(DeclRefExpr *E);
760 ExprResult TransformCXXDefaultArgExpr(CXXDefaultArgExpr *E);
761 ExprResult TransformTemplateParmRefExpr(DeclRefExpr *E,
Douglas Gregoreb5a39d2010-12-24 00:15:10 +0000762 NonTypeTemplateParmDecl *D);
Douglas Gregorcdbc5392011-01-15 01:15:58 +0000763 ExprResult TransformSubstNonTypeTemplateParmPackExpr(
764 SubstNonTypeTemplateParmPackExpr *E);
765
Douglas Gregor14cf7522010-04-30 18:55:50 +0000766 QualType TransformFunctionProtoType(TypeLocBuilder &TLB,
John McCall31f82722010-11-12 08:19:04 +0000767 FunctionProtoTypeLoc TL);
Douglas Gregor715e4612011-01-14 22:40:04 +0000768 ParmVarDecl *TransformFunctionTypeParam(ParmVarDecl *OldParm,
John McCall8fb0d9d2011-05-01 22:35:37 +0000769 int indexAdjustment,
Douglas Gregor715e4612011-01-14 22:40:04 +0000770 llvm::Optional<unsigned> NumExpansions);
John McCall58f10c32010-03-11 09:03:00 +0000771
Mike Stump11289f42009-09-09 15:08:12 +0000772 /// \brief Transforms a template type parameter type by performing
Douglas Gregord6ff3322009-08-04 16:50:30 +0000773 /// substitution of the corresponding template type argument.
John McCall550e0c22009-10-21 00:40:46 +0000774 QualType TransformTemplateTypeParmType(TypeLocBuilder &TLB,
John McCall31f82722010-11-12 08:19:04 +0000775 TemplateTypeParmTypeLoc TL);
Nick Lewyckyc96c37f2010-07-06 19:51:49 +0000776
Douglas Gregorada4b792011-01-14 02:55:32 +0000777 /// \brief Transforms an already-substituted template type parameter pack
778 /// into either itself (if we aren't substituting into its pack expansion)
779 /// or the appropriate substituted argument.
780 QualType TransformSubstTemplateTypeParmPackType(TypeLocBuilder &TLB,
781 SubstTemplateTypeParmPackTypeLoc TL);
782
John McCalldadc5752010-08-24 06:29:42 +0000783 ExprResult TransformCallExpr(CallExpr *CE) {
Nick Lewyckyc96c37f2010-07-06 19:51:49 +0000784 getSema().CallsUndergoingInstantiation.push_back(CE);
John McCalldadc5752010-08-24 06:29:42 +0000785 ExprResult Result =
Nick Lewyckyc96c37f2010-07-06 19:51:49 +0000786 TreeTransform<TemplateInstantiator>::TransformCallExpr(CE);
787 getSema().CallsUndergoingInstantiation.pop_back();
788 return move(Result);
789 }
Douglas Gregord6ff3322009-08-04 16:50:30 +0000790 };
Douglas Gregor04318252009-07-06 15:59:29 +0000791}
792
Douglas Gregor5597ab42010-05-07 23:12:07 +0000793bool TemplateInstantiator::AlreadyTransformed(QualType T) {
794 if (T.isNull())
795 return true;
796
Douglas Gregor5a5073e2010-05-24 17:22:01 +0000797 if (T->isDependentType() || T->isVariablyModifiedType())
Douglas Gregor5597ab42010-05-07 23:12:07 +0000798 return false;
799
800 getSema().MarkDeclarationsReferencedInType(Loc, T);
801 return true;
802}
803
Douglas Gregora04f2ca2010-03-01 15:56:25 +0000804Decl *TemplateInstantiator::TransformDecl(SourceLocation Loc, Decl *D) {
Douglas Gregor2b6ca462009-09-03 21:38:09 +0000805 if (!D)
806 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000807
Douglas Gregor2b6ca462009-09-03 21:38:09 +0000808 if (TemplateTemplateParmDecl *TTP = dyn_cast<TemplateTemplateParmDecl>(D)) {
Douglas Gregor01afeef2009-08-28 20:31:08 +0000809 if (TTP->getDepth() < TemplateArgs.getNumLevels()) {
Douglas Gregorb93971082010-02-05 19:54:12 +0000810 // If the corresponding template argument is NULL or non-existent, it's
811 // because we are performing instantiation from explicitly-specified
812 // template arguments in a function template, but there were some
813 // arguments left unspecified.
814 if (!TemplateArgs.hasTemplateArgument(TTP->getDepth(),
815 TTP->getPosition()))
816 return D;
817
Douglas Gregorf5500772011-01-05 15:48:55 +0000818 TemplateArgument Arg = TemplateArgs(TTP->getDepth(), TTP->getPosition());
819
820 if (TTP->isParameterPack()) {
821 assert(Arg.getKind() == TemplateArgument::Pack &&
822 "Missing argument pack");
823
Douglas Gregor5590be02011-01-15 06:45:20 +0000824 assert(getSema().ArgumentPackSubstitutionIndex >= 0);
Douglas Gregora8bac7f2011-01-10 07:32:04 +0000825 assert(getSema().ArgumentPackSubstitutionIndex < (int)Arg.pack_size());
Douglas Gregorf5500772011-01-05 15:48:55 +0000826 Arg = Arg.pack_begin()[getSema().ArgumentPackSubstitutionIndex];
827 }
828
829 TemplateName Template = Arg.getAsTemplate();
Douglas Gregor9167f8b2009-11-11 01:00:40 +0000830 assert(!Template.isNull() && Template.getAsTemplateDecl() &&
Douglas Gregor01afeef2009-08-28 20:31:08 +0000831 "Wrong kind of template template argument");
Douglas Gregor9167f8b2009-11-11 01:00:40 +0000832 return Template.getAsTemplateDecl();
Douglas Gregor01afeef2009-08-28 20:31:08 +0000833 }
Mike Stump11289f42009-09-09 15:08:12 +0000834
Douglas Gregor9167f8b2009-11-11 01:00:40 +0000835 // Fall through to find the instantiated declaration for this template
836 // template parameter.
Douglas Gregor71dc5092009-08-06 06:41:21 +0000837 }
Mike Stump11289f42009-09-09 15:08:12 +0000838
Douglas Gregora04f2ca2010-03-01 15:56:25 +0000839 return SemaRef.FindInstantiatedDecl(Loc, cast<NamedDecl>(D), TemplateArgs);
Douglas Gregord6ff3322009-08-04 16:50:30 +0000840}
841
Douglas Gregor25289362010-03-01 17:25:41 +0000842Decl *TemplateInstantiator::TransformDefinition(SourceLocation Loc, Decl *D) {
John McCall76d824f2009-08-25 22:02:44 +0000843 Decl *Inst = getSema().SubstDecl(D, getSema().CurContext, TemplateArgs);
Douglas Gregorebe10102009-08-20 07:17:43 +0000844 if (!Inst)
845 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000846
Douglas Gregorebe10102009-08-20 07:17:43 +0000847 getSema().CurrentInstantiationScope->InstantiatedLocal(D, Inst);
848 return Inst;
849}
850
Douglas Gregora5cb6da2009-10-20 05:58:46 +0000851NamedDecl *
852TemplateInstantiator::TransformFirstQualifierInScope(NamedDecl *D,
853 SourceLocation Loc) {
854 // If the first part of the nested-name-specifier was a template type
855 // parameter, instantiate that type parameter down to a tag type.
856 if (TemplateTypeParmDecl *TTPD = dyn_cast_or_null<TemplateTypeParmDecl>(D)) {
857 const TemplateTypeParmType *TTP
858 = cast<TemplateTypeParmType>(getSema().Context.getTypeDeclType(TTPD));
Douglas Gregor53c3f4e2010-12-20 22:48:17 +0000859
Douglas Gregora5cb6da2009-10-20 05:58:46 +0000860 if (TTP->getDepth() < TemplateArgs.getNumLevels()) {
Douglas Gregor53c3f4e2010-12-20 22:48:17 +0000861 // FIXME: This needs testing w/ member access expressions.
862 TemplateArgument Arg = TemplateArgs(TTP->getDepth(), TTP->getIndex());
863
864 if (TTP->isParameterPack()) {
865 assert(Arg.getKind() == TemplateArgument::Pack &&
866 "Missing argument pack");
867
Douglas Gregore1d60df2011-01-14 23:41:42 +0000868 if (getSema().ArgumentPackSubstitutionIndex == -1)
Douglas Gregor53c3f4e2010-12-20 22:48:17 +0000869 return 0;
Douglas Gregor53c3f4e2010-12-20 22:48:17 +0000870
Douglas Gregora8bac7f2011-01-10 07:32:04 +0000871 assert(getSema().ArgumentPackSubstitutionIndex < (int)Arg.pack_size());
Douglas Gregor53c3f4e2010-12-20 22:48:17 +0000872 Arg = Arg.pack_begin()[getSema().ArgumentPackSubstitutionIndex];
873 }
874
875 QualType T = Arg.getAsType();
Douglas Gregora5cb6da2009-10-20 05:58:46 +0000876 if (T.isNull())
Douglas Gregora04f2ca2010-03-01 15:56:25 +0000877 return cast_or_null<NamedDecl>(TransformDecl(Loc, D));
Douglas Gregora5cb6da2009-10-20 05:58:46 +0000878
879 if (const TagType *Tag = T->getAs<TagType>())
880 return Tag->getDecl();
881
882 // The resulting type is not a tag; complain.
883 getSema().Diag(Loc, diag::err_nested_name_spec_non_tag) << T;
884 return 0;
885 }
886 }
887
Douglas Gregora04f2ca2010-03-01 15:56:25 +0000888 return cast_or_null<NamedDecl>(TransformDecl(Loc, D));
Douglas Gregora5cb6da2009-10-20 05:58:46 +0000889}
890
Douglas Gregorebe10102009-08-20 07:17:43 +0000891VarDecl *
892TemplateInstantiator::RebuildExceptionDecl(VarDecl *ExceptionDecl,
John McCallbcd03502009-12-07 02:54:59 +0000893 TypeSourceInfo *Declarator,
Abramo Bagnaradff19302011-03-08 08:55:46 +0000894 SourceLocation StartLoc,
895 SourceLocation NameLoc,
896 IdentifierInfo *Name) {
Douglas Gregor9f0e1aa2010-09-09 17:09:21 +0000897 VarDecl *Var = inherited::RebuildExceptionDecl(ExceptionDecl, Declarator,
Abramo Bagnaradff19302011-03-08 08:55:46 +0000898 StartLoc, NameLoc, Name);
Douglas Gregorf4e837f2010-04-26 17:57:08 +0000899 if (Var)
900 getSema().CurrentInstantiationScope->InstantiatedLocal(ExceptionDecl, Var);
901 return Var;
902}
903
904VarDecl *TemplateInstantiator::RebuildObjCExceptionDecl(VarDecl *ExceptionDecl,
905 TypeSourceInfo *TSInfo,
906 QualType T) {
907 VarDecl *Var = inherited::RebuildObjCExceptionDecl(ExceptionDecl, TSInfo, T);
908 if (Var)
Douglas Gregorebe10102009-08-20 07:17:43 +0000909 getSema().CurrentInstantiationScope->InstantiatedLocal(ExceptionDecl, Var);
910 return Var;
911}
912
John McCall7f41d982009-09-11 04:59:25 +0000913QualType
John McCall954b5de2010-11-04 19:04:38 +0000914TemplateInstantiator::RebuildElaboratedType(SourceLocation KeywordLoc,
915 ElaboratedTypeKeyword Keyword,
Douglas Gregor844cb502011-03-01 18:12:44 +0000916 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara6150c882010-05-11 21:36:43 +0000917 QualType T) {
John McCall7f41d982009-09-11 04:59:25 +0000918 if (const TagType *TT = T->getAs<TagType>()) {
919 TagDecl* TD = TT->getDecl();
920
John McCall954b5de2010-11-04 19:04:38 +0000921 SourceLocation TagLocation = KeywordLoc;
John McCall7f41d982009-09-11 04:59:25 +0000922
923 // FIXME: type might be anonymous.
924 IdentifierInfo *Id = TD->getIdentifier();
925
926 // TODO: should we even warn on struct/class mismatches for this? Seems
927 // like it's likely to produce a lot of spurious errors.
Abramo Bagnara6150c882010-05-11 21:36:43 +0000928 if (Keyword != ETK_None && Keyword != ETK_Typename) {
929 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForKeyword(Keyword);
930 if (!SemaRef.isAcceptableTagRedeclaration(TD, Kind, TagLocation, *Id)) {
931 SemaRef.Diag(TagLocation, diag::err_use_with_wrong_tag)
932 << Id
933 << FixItHint::CreateReplacement(SourceRange(TagLocation),
934 TD->getKindName());
935 SemaRef.Diag(TD->getLocation(), diag::note_previous_use);
936 }
John McCall7f41d982009-09-11 04:59:25 +0000937 }
938 }
939
John McCall954b5de2010-11-04 19:04:38 +0000940 return TreeTransform<TemplateInstantiator>::RebuildElaboratedType(KeywordLoc,
941 Keyword,
Douglas Gregor844cb502011-03-01 18:12:44 +0000942 QualifierLoc,
943 T);
John McCall7f41d982009-09-11 04:59:25 +0000944}
945
Douglas Gregor9db53502011-03-02 18:07:45 +0000946TemplateName TemplateInstantiator::TransformTemplateName(CXXScopeSpec &SS,
947 TemplateName Name,
948 SourceLocation NameLoc,
949 QualType ObjectType,
950 NamedDecl *FirstQualifierInScope) {
951 if (TemplateTemplateParmDecl *TTP
952 = dyn_cast_or_null<TemplateTemplateParmDecl>(Name.getAsTemplateDecl())) {
953 if (TTP->getDepth() < TemplateArgs.getNumLevels()) {
954 // If the corresponding template argument is NULL or non-existent, it's
955 // because we are performing instantiation from explicitly-specified
956 // template arguments in a function template, but there were some
957 // arguments left unspecified.
958 if (!TemplateArgs.hasTemplateArgument(TTP->getDepth(),
959 TTP->getPosition()))
960 return Name;
961
962 TemplateArgument Arg = TemplateArgs(TTP->getDepth(), TTP->getPosition());
963
964 if (TTP->isParameterPack()) {
965 assert(Arg.getKind() == TemplateArgument::Pack &&
966 "Missing argument pack");
967
968 if (getSema().ArgumentPackSubstitutionIndex == -1) {
969 // We have the template argument pack to substitute, but we're not
970 // actually expanding the enclosing pack expansion yet. So, just
971 // keep the entire argument pack.
972 return getSema().Context.getSubstTemplateTemplateParmPack(TTP, Arg);
973 }
974
975 assert(getSema().ArgumentPackSubstitutionIndex < (int)Arg.pack_size());
976 Arg = Arg.pack_begin()[getSema().ArgumentPackSubstitutionIndex];
977 }
978
979 TemplateName Template = Arg.getAsTemplate();
Richard Smith3f1b5d02011-05-05 21:57:07 +0000980 assert(!Template.isNull() && "Null template template argument");
Douglas Gregor9d9f8db2011-03-05 20:06:51 +0000981
982 // We don't ever want to substitute for a qualified template name, since
983 // the qualifier is handled separately. So, look through the qualified
984 // template name to its underlying declaration.
985 if (QualifiedTemplateName *QTN = Template.getAsQualifiedTemplateName())
986 Template = TemplateName(QTN->getTemplateDecl());
987
Douglas Gregor9db53502011-03-02 18:07:45 +0000988 return Template;
989 }
990 }
991
992 if (SubstTemplateTemplateParmPackStorage *SubstPack
993 = Name.getAsSubstTemplateTemplateParmPack()) {
994 if (getSema().ArgumentPackSubstitutionIndex == -1)
995 return Name;
996
997 const TemplateArgument &ArgPack = SubstPack->getArgumentPack();
998 assert(getSema().ArgumentPackSubstitutionIndex < (int)ArgPack.pack_size() &&
999 "Pack substitution index out-of-range");
1000 return ArgPack.pack_begin()[getSema().ArgumentPackSubstitutionIndex]
1001 .getAsTemplate();
1002 }
1003
1004 return inherited::TransformTemplateName(SS, Name, NameLoc, ObjectType,
1005 FirstQualifierInScope);
1006}
1007
John McCalldadc5752010-08-24 06:29:42 +00001008ExprResult
John McCall47f29ea2009-12-08 09:21:05 +00001009TemplateInstantiator::TransformPredefinedExpr(PredefinedExpr *E) {
Anders Carlsson0b209a82009-09-11 01:22:35 +00001010 if (!E->isTypeDependent())
John McCallc3007a22010-10-26 07:05:15 +00001011 return SemaRef.Owned(E);
Anders Carlsson0b209a82009-09-11 01:22:35 +00001012
1013 FunctionDecl *currentDecl = getSema().getCurFunctionDecl();
1014 assert(currentDecl && "Must have current function declaration when "
1015 "instantiating.");
1016
1017 PredefinedExpr::IdentType IT = E->getIdentType();
1018
Anders Carlsson5bd8d192010-02-11 18:20:28 +00001019 unsigned Length = PredefinedExpr::ComputeName(IT, currentDecl).length();
Anders Carlsson0b209a82009-09-11 01:22:35 +00001020
1021 llvm::APInt LengthI(32, Length + 1);
John McCall8ccfcb52009-09-24 19:53:00 +00001022 QualType ResTy = getSema().Context.CharTy.withConst();
Anders Carlsson0b209a82009-09-11 01:22:35 +00001023 ResTy = getSema().Context.getConstantArrayType(ResTy, LengthI,
1024 ArrayType::Normal, 0);
1025 PredefinedExpr *PE =
1026 new (getSema().Context) PredefinedExpr(E->getLocation(), ResTy, IT);
1027 return getSema().Owned(PE);
1028}
1029
John McCalldadc5752010-08-24 06:29:42 +00001030ExprResult
John McCall13481c52010-02-06 08:42:39 +00001031TemplateInstantiator::TransformTemplateParmRefExpr(DeclRefExpr *E,
Douglas Gregor6c379e22010-02-08 23:41:45 +00001032 NonTypeTemplateParmDecl *NTTP) {
John McCall13481c52010-02-06 08:42:39 +00001033 // If the corresponding template argument is NULL or non-existent, it's
1034 // because we are performing instantiation from explicitly-specified
1035 // template arguments in a function template, but there were some
1036 // arguments left unspecified.
1037 if (!TemplateArgs.hasTemplateArgument(NTTP->getDepth(),
1038 NTTP->getPosition()))
John McCallc3007a22010-10-26 07:05:15 +00001039 return SemaRef.Owned(E);
Mike Stump11289f42009-09-09 15:08:12 +00001040
Douglas Gregoreb5a39d2010-12-24 00:15:10 +00001041 TemplateArgument Arg = TemplateArgs(NTTP->getDepth(), NTTP->getPosition());
1042 if (NTTP->isParameterPack()) {
1043 assert(Arg.getKind() == TemplateArgument::Pack &&
1044 "Missing argument pack");
1045
1046 if (getSema().ArgumentPackSubstitutionIndex == -1) {
Douglas Gregorcdbc5392011-01-15 01:15:58 +00001047 // We have an argument pack, but we can't select a particular argument
1048 // out of it yet. Therefore, we'll build an expression to hold on to that
1049 // argument pack.
1050 QualType TargetType = SemaRef.SubstType(NTTP->getType(), TemplateArgs,
1051 E->getLocation(),
1052 NTTP->getDeclName());
1053 if (TargetType.isNull())
1054 return ExprError();
1055
1056 return new (SemaRef.Context) SubstNonTypeTemplateParmPackExpr(TargetType,
1057 NTTP,
1058 E->getLocation(),
1059 Arg);
Douglas Gregoreb5a39d2010-12-24 00:15:10 +00001060 }
1061
Douglas Gregora8bac7f2011-01-10 07:32:04 +00001062 assert(getSema().ArgumentPackSubstitutionIndex < (int)Arg.pack_size());
Douglas Gregoreb5a39d2010-12-24 00:15:10 +00001063 Arg = Arg.pack_begin()[getSema().ArgumentPackSubstitutionIndex];
1064 }
Mike Stump11289f42009-09-09 15:08:12 +00001065
John McCall13481c52010-02-06 08:42:39 +00001066 // The template argument itself might be an expression, in which
1067 // case we just return that expression.
1068 if (Arg.getKind() == TemplateArgument::Expression)
John McCallc3007a22010-10-26 07:05:15 +00001069 return SemaRef.Owned(Arg.getAsExpr());
Mike Stump11289f42009-09-09 15:08:12 +00001070
John McCall13481c52010-02-06 08:42:39 +00001071 if (Arg.getKind() == TemplateArgument::Declaration) {
1072 ValueDecl *VD = cast<ValueDecl>(Arg.getAsDecl());
Mike Stump11289f42009-09-09 15:08:12 +00001073
John McCall15dda372010-02-06 10:23:53 +00001074 // Find the instantiation of the template argument. This is
1075 // required for nested templates.
John McCall13481c52010-02-06 08:42:39 +00001076 VD = cast_or_null<ValueDecl>(
Douglas Gregora04f2ca2010-03-01 15:56:25 +00001077 getSema().FindInstantiatedDecl(E->getLocation(),
1078 VD, TemplateArgs));
John McCall13481c52010-02-06 08:42:39 +00001079 if (!VD)
John McCallfaf5fb42010-08-26 23:41:50 +00001080 return ExprError();
Mike Stump11289f42009-09-09 15:08:12 +00001081
John McCall15dda372010-02-06 10:23:53 +00001082 // Derive the type we want the substituted decl to have. This had
1083 // better be non-dependent, or these checks will have serious problems.
Douglas Gregor0231d8d2011-01-19 20:10:05 +00001084 QualType TargetType;
1085 if (NTTP->isExpandedParameterPack())
1086 TargetType = NTTP->getExpansionType(
1087 getSema().ArgumentPackSubstitutionIndex);
1088 else if (NTTP->isParameterPack() &&
1089 isa<PackExpansionType>(NTTP->getType())) {
1090 TargetType = SemaRef.SubstType(
1091 cast<PackExpansionType>(NTTP->getType())->getPattern(),
1092 TemplateArgs, E->getLocation(),
1093 NTTP->getDeclName());
1094 } else
1095 TargetType = SemaRef.SubstType(NTTP->getType(), TemplateArgs,
1096 E->getLocation(), NTTP->getDeclName());
John McCall15dda372010-02-06 10:23:53 +00001097 assert(!TargetType.isNull() && "type substitution failed for param type");
1098 assert(!TargetType->isDependentType() && "param type still dependent");
Douglas Gregord5cb1dd2010-03-28 02:42:43 +00001099 return SemaRef.BuildExpressionFromDeclTemplateArgument(Arg,
1100 TargetType,
1101 E->getLocation());
John McCall13481c52010-02-06 08:42:39 +00001102 }
1103
Douglas Gregord5cb1dd2010-03-28 02:42:43 +00001104 return SemaRef.BuildExpressionFromIntegralTemplateArgument(Arg,
1105 E->getSourceRange().getBegin());
John McCall13481c52010-02-06 08:42:39 +00001106}
1107
Douglas Gregorcdbc5392011-01-15 01:15:58 +00001108ExprResult
1109TemplateInstantiator::TransformSubstNonTypeTemplateParmPackExpr(
1110 SubstNonTypeTemplateParmPackExpr *E) {
1111 if (getSema().ArgumentPackSubstitutionIndex == -1) {
1112 // We aren't expanding the parameter pack, so just return ourselves.
1113 return getSema().Owned(E);
1114 }
1115
Douglas Gregorcdbc5392011-01-15 01:15:58 +00001116 const TemplateArgument &ArgPack = E->getArgumentPack();
1117 unsigned Index = (unsigned)getSema().ArgumentPackSubstitutionIndex;
1118 assert(Index < ArgPack.pack_size() && "Substitution index out-of-range");
1119
1120 const TemplateArgument &Arg = ArgPack.pack_begin()[Index];
1121 if (Arg.getKind() == TemplateArgument::Expression)
1122 return SemaRef.Owned(Arg.getAsExpr());
1123
1124 if (Arg.getKind() == TemplateArgument::Declaration) {
1125 ValueDecl *VD = cast<ValueDecl>(Arg.getAsDecl());
1126
1127 // Find the instantiation of the template argument. This is
1128 // required for nested templates.
1129 VD = cast_or_null<ValueDecl>(
1130 getSema().FindInstantiatedDecl(E->getParameterPackLocation(),
1131 VD, TemplateArgs));
1132 if (!VD)
1133 return ExprError();
1134
Douglas Gregor0231d8d2011-01-19 20:10:05 +00001135 QualType T;
1136 NonTypeTemplateParmDecl *NTTP = E->getParameterPack();
1137 if (NTTP->isExpandedParameterPack())
1138 T = NTTP->getExpansionType(getSema().ArgumentPackSubstitutionIndex);
1139 else if (const PackExpansionType *Expansion
1140 = dyn_cast<PackExpansionType>(NTTP->getType()))
1141 T = SemaRef.SubstType(Expansion->getPattern(), TemplateArgs,
1142 E->getParameterPackLocation(), NTTP->getDeclName());
1143 else
1144 T = E->getType();
1145 return SemaRef.BuildExpressionFromDeclTemplateArgument(Arg, T,
Douglas Gregorcdbc5392011-01-15 01:15:58 +00001146 E->getParameterPackLocation());
1147 }
1148
1149 return SemaRef.BuildExpressionFromIntegralTemplateArgument(Arg,
1150 E->getParameterPackLocation());
1151}
John McCall13481c52010-02-06 08:42:39 +00001152
John McCalldadc5752010-08-24 06:29:42 +00001153ExprResult
John McCall13481c52010-02-06 08:42:39 +00001154TemplateInstantiator::TransformDeclRefExpr(DeclRefExpr *E) {
1155 NamedDecl *D = E->getDecl();
1156 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(D)) {
1157 if (NTTP->getDepth() < TemplateArgs.getNumLevels())
1158 return TransformTemplateParmRefExpr(E, NTTP);
Douglas Gregor954de172009-10-31 17:21:17 +00001159
1160 // We have a non-type template parameter that isn't fully substituted;
1161 // FindInstantiatedDecl will find it in the local instantiation scope.
Douglas Gregora16548e2009-08-11 05:31:07 +00001162 }
Mike Stump11289f42009-09-09 15:08:12 +00001163
John McCall47f29ea2009-12-08 09:21:05 +00001164 return TreeTransform<TemplateInstantiator>::TransformDeclRefExpr(E);
Douglas Gregora16548e2009-08-11 05:31:07 +00001165}
1166
John McCalldadc5752010-08-24 06:29:42 +00001167ExprResult TemplateInstantiator::TransformCXXDefaultArgExpr(
John McCall47f29ea2009-12-08 09:21:05 +00001168 CXXDefaultArgExpr *E) {
Sebastian Redl14236c82009-11-08 13:56:19 +00001169 assert(!cast<FunctionDecl>(E->getParam()->getDeclContext())->
1170 getDescribedFunctionTemplate() &&
1171 "Default arg expressions are never formed in dependent cases.");
Douglas Gregor033f6752009-12-23 23:03:06 +00001172 return SemaRef.BuildCXXDefaultArgExpr(E->getUsedLocation(),
1173 cast<FunctionDecl>(E->getParam()->getDeclContext()),
1174 E->getParam());
Sebastian Redl14236c82009-11-08 13:56:19 +00001175}
1176
Douglas Gregor14cf7522010-04-30 18:55:50 +00001177QualType TemplateInstantiator::TransformFunctionProtoType(TypeLocBuilder &TLB,
John McCall31f82722010-11-12 08:19:04 +00001178 FunctionProtoTypeLoc TL) {
Douglas Gregor14cf7522010-04-30 18:55:50 +00001179 // We need a local instantiation scope for this function prototype.
John McCall19c1bfd2010-08-25 05:32:35 +00001180 LocalInstantiationScope Scope(SemaRef, /*CombineWithOuterScope=*/true);
John McCall31f82722010-11-12 08:19:04 +00001181 return inherited::TransformFunctionProtoType(TLB, TL);
John McCall58f10c32010-03-11 09:03:00 +00001182}
1183
1184ParmVarDecl *
Douglas Gregor715e4612011-01-14 22:40:04 +00001185TemplateInstantiator::TransformFunctionTypeParam(ParmVarDecl *OldParm,
John McCall8fb0d9d2011-05-01 22:35:37 +00001186 int indexAdjustment,
Douglas Gregor715e4612011-01-14 22:40:04 +00001187 llvm::Optional<unsigned> NumExpansions) {
John McCall8fb0d9d2011-05-01 22:35:37 +00001188 return SemaRef.SubstParmVarDecl(OldParm, TemplateArgs, indexAdjustment,
Douglas Gregor715e4612011-01-14 22:40:04 +00001189 NumExpansions);
John McCall58f10c32010-03-11 09:03:00 +00001190}
1191
Mike Stump11289f42009-09-09 15:08:12 +00001192QualType
John McCall550e0c22009-10-21 00:40:46 +00001193TemplateInstantiator::TransformTemplateTypeParmType(TypeLocBuilder &TLB,
John McCall31f82722010-11-12 08:19:04 +00001194 TemplateTypeParmTypeLoc TL) {
John McCall424cec92011-01-19 06:33:43 +00001195 const TemplateTypeParmType *T = TL.getTypePtr();
Douglas Gregor01afeef2009-08-28 20:31:08 +00001196 if (T->getDepth() < TemplateArgs.getNumLevels()) {
Douglas Gregorfe1e1102009-02-27 19:31:52 +00001197 // Replace the template type parameter with its corresponding
1198 // template argument.
Mike Stump11289f42009-09-09 15:08:12 +00001199
1200 // If the corresponding template argument is NULL or doesn't exist, it's
1201 // because we are performing instantiation from explicitly-specified
1202 // template arguments in a function template class, but there were some
Douglas Gregore3f1f352009-07-01 00:28:38 +00001203 // arguments left unspecified.
John McCall550e0c22009-10-21 00:40:46 +00001204 if (!TemplateArgs.hasTemplateArgument(T->getDepth(), T->getIndex())) {
1205 TemplateTypeParmTypeLoc NewTL
1206 = TLB.push<TemplateTypeParmTypeLoc>(TL.getType());
1207 NewTL.setNameLoc(TL.getNameLoc());
1208 return TL.getType();
1209 }
Mike Stump11289f42009-09-09 15:08:12 +00001210
Douglas Gregor840bd6c2010-12-20 22:05:00 +00001211 TemplateArgument Arg = TemplateArgs(T->getDepth(), T->getIndex());
1212
1213 if (T->isParameterPack()) {
1214 assert(Arg.getKind() == TemplateArgument::Pack &&
1215 "Missing argument pack");
1216
1217 if (getSema().ArgumentPackSubstitutionIndex == -1) {
Douglas Gregorada4b792011-01-14 02:55:32 +00001218 // We have the template argument pack, but we're not expanding the
1219 // enclosing pack expansion yet. Just save the template argument
1220 // pack for later substitution.
1221 QualType Result
1222 = getSema().Context.getSubstTemplateTypeParmPackType(T, Arg);
1223 SubstTemplateTypeParmPackTypeLoc NewTL
1224 = TLB.push<SubstTemplateTypeParmPackTypeLoc>(Result);
1225 NewTL.setNameLoc(TL.getNameLoc());
1226 return Result;
Douglas Gregor840bd6c2010-12-20 22:05:00 +00001227 }
1228
Douglas Gregora8bac7f2011-01-10 07:32:04 +00001229 assert(getSema().ArgumentPackSubstitutionIndex < (int)Arg.pack_size());
Douglas Gregor840bd6c2010-12-20 22:05:00 +00001230 Arg = Arg.pack_begin()[getSema().ArgumentPackSubstitutionIndex];
1231 }
1232
1233 assert(Arg.getKind() == TemplateArgument::Type &&
Douglas Gregorfe1e1102009-02-27 19:31:52 +00001234 "Template argument kind mismatch");
Douglas Gregor01afeef2009-08-28 20:31:08 +00001235
Douglas Gregor840bd6c2010-12-20 22:05:00 +00001236 QualType Replacement = Arg.getAsType();
John McCallcebee162009-10-18 09:09:24 +00001237
1238 // TODO: only do this uniquing once, at the start of instantiation.
John McCall550e0c22009-10-21 00:40:46 +00001239 QualType Result
1240 = getSema().Context.getSubstTemplateTypeParmType(T, Replacement);
1241 SubstTemplateTypeParmTypeLoc NewTL
1242 = TLB.push<SubstTemplateTypeParmTypeLoc>(Result);
1243 NewTL.setNameLoc(TL.getNameLoc());
1244 return Result;
Mike Stump11289f42009-09-09 15:08:12 +00001245 }
Douglas Gregorfe1e1102009-02-27 19:31:52 +00001246
1247 // The template type parameter comes from an inner template (e.g.,
1248 // the template parameter list of a member template inside the
1249 // template we are instantiating). Create a new template type
1250 // parameter with the template "level" reduced by one.
Chandler Carruth08836322011-05-01 00:51:33 +00001251 TemplateTypeParmDecl *NewTTPDecl = 0;
1252 if (TemplateTypeParmDecl *OldTTPDecl = T->getDecl())
1253 NewTTPDecl = cast_or_null<TemplateTypeParmDecl>(
1254 TransformDecl(TL.getNameLoc(), OldTTPDecl));
1255
John McCall550e0c22009-10-21 00:40:46 +00001256 QualType Result
1257 = getSema().Context.getTemplateTypeParmType(T->getDepth()
1258 - TemplateArgs.getNumLevels(),
1259 T->getIndex(),
1260 T->isParameterPack(),
Chandler Carruth08836322011-05-01 00:51:33 +00001261 NewTTPDecl);
John McCall550e0c22009-10-21 00:40:46 +00001262 TemplateTypeParmTypeLoc NewTL = TLB.push<TemplateTypeParmTypeLoc>(Result);
1263 NewTL.setNameLoc(TL.getNameLoc());
1264 return Result;
Douglas Gregor17c0d7b2009-02-28 00:25:32 +00001265}
Douglas Gregorfe1e1102009-02-27 19:31:52 +00001266
Douglas Gregorada4b792011-01-14 02:55:32 +00001267QualType
1268TemplateInstantiator::TransformSubstTemplateTypeParmPackType(
1269 TypeLocBuilder &TLB,
1270 SubstTemplateTypeParmPackTypeLoc TL) {
1271 if (getSema().ArgumentPackSubstitutionIndex == -1) {
1272 // We aren't expanding the parameter pack, so just return ourselves.
1273 SubstTemplateTypeParmPackTypeLoc NewTL
1274 = TLB.push<SubstTemplateTypeParmPackTypeLoc>(TL.getType());
1275 NewTL.setNameLoc(TL.getNameLoc());
1276 return TL.getType();
1277 }
1278
1279 const TemplateArgument &ArgPack = TL.getTypePtr()->getArgumentPack();
1280 unsigned Index = (unsigned)getSema().ArgumentPackSubstitutionIndex;
1281 assert(Index < ArgPack.pack_size() && "Substitution index out-of-range");
1282
1283 QualType Result = ArgPack.pack_begin()[Index].getAsType();
1284 Result = getSema().Context.getSubstTemplateTypeParmType(
1285 TL.getTypePtr()->getReplacedParameter(),
1286 Result);
1287 SubstTemplateTypeParmTypeLoc NewTL
1288 = TLB.push<SubstTemplateTypeParmTypeLoc>(Result);
1289 NewTL.setNameLoc(TL.getNameLoc());
1290 return Result;
1291}
1292
John McCall76d824f2009-08-25 22:02:44 +00001293/// \brief Perform substitution on the type T with a given set of template
1294/// arguments.
Douglas Gregorfe1e1102009-02-27 19:31:52 +00001295///
1296/// This routine substitutes the given template arguments into the
1297/// type T and produces the instantiated type.
1298///
1299/// \param T the type into which the template arguments will be
1300/// substituted. If this type is not dependent, it will be returned
1301/// immediately.
1302///
1303/// \param TemplateArgs the template arguments that will be
1304/// substituted for the top-level template parameters within T.
1305///
Douglas Gregorfe1e1102009-02-27 19:31:52 +00001306/// \param Loc the location in the source code where this substitution
1307/// is being performed. It will typically be the location of the
1308/// declarator (if we're instantiating the type of some declaration)
1309/// or the location of the type in the source code (if, e.g., we're
1310/// instantiating the type of a cast expression).
1311///
1312/// \param Entity the name of the entity associated with a declaration
1313/// being instantiated (if any). May be empty to indicate that there
1314/// is no such entity (if, e.g., this is a type that occurs as part of
1315/// a cast expression) or that the entity has no name (e.g., an
1316/// unnamed function parameter).
1317///
1318/// \returns If the instantiation succeeds, the instantiated
1319/// type. Otherwise, produces diagnostics and returns a NULL type.
John McCallbcd03502009-12-07 02:54:59 +00001320TypeSourceInfo *Sema::SubstType(TypeSourceInfo *T,
John McCall609459e2009-10-21 00:58:09 +00001321 const MultiLevelTemplateArgumentList &Args,
1322 SourceLocation Loc,
1323 DeclarationName Entity) {
1324 assert(!ActiveTemplateInstantiations.empty() &&
1325 "Cannot perform an instantiation without some context on the "
1326 "instantiation stack");
1327
Douglas Gregor5a5073e2010-05-24 17:22:01 +00001328 if (!T->getType()->isDependentType() &&
1329 !T->getType()->isVariablyModifiedType())
John McCall609459e2009-10-21 00:58:09 +00001330 return T;
1331
1332 TemplateInstantiator Instantiator(*this, Args, Loc, Entity);
1333 return Instantiator.TransformType(T);
1334}
1335
Douglas Gregor5499af42011-01-05 23:12:31 +00001336TypeSourceInfo *Sema::SubstType(TypeLoc TL,
1337 const MultiLevelTemplateArgumentList &Args,
1338 SourceLocation Loc,
1339 DeclarationName Entity) {
1340 assert(!ActiveTemplateInstantiations.empty() &&
1341 "Cannot perform an instantiation without some context on the "
1342 "instantiation stack");
1343
1344 if (TL.getType().isNull())
1345 return 0;
1346
1347 if (!TL.getType()->isDependentType() &&
1348 !TL.getType()->isVariablyModifiedType()) {
1349 // FIXME: Make a copy of the TypeLoc data here, so that we can
1350 // return a new TypeSourceInfo. Inefficient!
1351 TypeLocBuilder TLB;
1352 TLB.pushFullCopy(TL);
1353 return TLB.getTypeSourceInfo(Context, TL.getType());
1354 }
1355
1356 TemplateInstantiator Instantiator(*this, Args, Loc, Entity);
1357 TypeLocBuilder TLB;
1358 TLB.reserve(TL.getFullDataSize());
1359 QualType Result = Instantiator.TransformType(TLB, TL);
1360 if (Result.isNull())
1361 return 0;
1362
1363 return TLB.getTypeSourceInfo(Context, Result);
1364}
1365
John McCall609459e2009-10-21 00:58:09 +00001366/// Deprecated form of the above.
Mike Stump11289f42009-09-09 15:08:12 +00001367QualType Sema::SubstType(QualType T,
Douglas Gregor01afeef2009-08-28 20:31:08 +00001368 const MultiLevelTemplateArgumentList &TemplateArgs,
John McCall76d824f2009-08-25 22:02:44 +00001369 SourceLocation Loc, DeclarationName Entity) {
Douglas Gregor79cf6032009-03-10 20:44:00 +00001370 assert(!ActiveTemplateInstantiations.empty() &&
1371 "Cannot perform an instantiation without some context on the "
1372 "instantiation stack");
1373
Douglas Gregor5a5073e2010-05-24 17:22:01 +00001374 // If T is not a dependent type or a variably-modified type, there
1375 // is nothing to do.
1376 if (!T->isDependentType() && !T->isVariablyModifiedType())
Douglas Gregorfe1e1102009-02-27 19:31:52 +00001377 return T;
1378
Douglas Gregord6ff3322009-08-04 16:50:30 +00001379 TemplateInstantiator Instantiator(*this, TemplateArgs, Loc, Entity);
1380 return Instantiator.TransformType(T);
Douglas Gregorfe1e1102009-02-27 19:31:52 +00001381}
Douglas Gregor463421d2009-03-03 04:44:36 +00001382
John McCallb29f78f2010-04-09 17:38:44 +00001383static bool NeedsInstantiationAsFunctionType(TypeSourceInfo *T) {
Douglas Gregor5a5073e2010-05-24 17:22:01 +00001384 if (T->getType()->isDependentType() || T->getType()->isVariablyModifiedType())
John McCallb29f78f2010-04-09 17:38:44 +00001385 return true;
1386
Abramo Bagnara6d810632010-12-14 22:11:44 +00001387 TypeLoc TL = T->getTypeLoc().IgnoreParens();
John McCallb29f78f2010-04-09 17:38:44 +00001388 if (!isa<FunctionProtoTypeLoc>(TL))
1389 return false;
1390
1391 FunctionProtoTypeLoc FP = cast<FunctionProtoTypeLoc>(TL);
1392 for (unsigned I = 0, E = FP.getNumArgs(); I != E; ++I) {
1393 ParmVarDecl *P = FP.getArg(I);
1394
Douglas Gregora7203e52011-05-09 20:45:16 +00001395 // The parameter's type as written might be dependent even if the
1396 // decayed type was not dependent.
1397 if (TypeSourceInfo *TSInfo = P->getTypeSourceInfo())
1398 if (TSInfo->getType()->isDependentType())
1399 return true;
1400
John McCallb29f78f2010-04-09 17:38:44 +00001401 // TODO: currently we always rebuild expressions. When we
1402 // properly get lazier about this, we should use the same
1403 // logic to avoid rebuilding prototypes here.
Douglas Gregor9cc278222011-01-05 21:14:17 +00001404 if (P->hasDefaultArg())
John McCallb29f78f2010-04-09 17:38:44 +00001405 return true;
1406 }
1407
1408 return false;
1409}
1410
1411/// A form of SubstType intended specifically for instantiating the
1412/// type of a FunctionDecl. Its purpose is solely to force the
1413/// instantiation of default-argument expressions.
1414TypeSourceInfo *Sema::SubstFunctionDeclType(TypeSourceInfo *T,
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 (!NeedsInstantiationAsFunctionType(T))
1423 return T;
1424
1425 TemplateInstantiator Instantiator(*this, Args, Loc, Entity);
1426
1427 TypeLocBuilder TLB;
1428
1429 TypeLoc TL = T->getTypeLoc();
1430 TLB.reserve(TL.getFullDataSize());
1431
John McCall31f82722010-11-12 08:19:04 +00001432 QualType Result = Instantiator.TransformType(TLB, TL);
John McCallb29f78f2010-04-09 17:38:44 +00001433 if (Result.isNull())
1434 return 0;
1435
1436 return TLB.getTypeSourceInfo(Context, Result);
1437}
1438
Douglas Gregor940bca72010-04-12 07:48:19 +00001439ParmVarDecl *Sema::SubstParmVarDecl(ParmVarDecl *OldParm,
Douglas Gregor715e4612011-01-14 22:40:04 +00001440 const MultiLevelTemplateArgumentList &TemplateArgs,
John McCall8fb0d9d2011-05-01 22:35:37 +00001441 int indexAdjustment,
Douglas Gregor715e4612011-01-14 22:40:04 +00001442 llvm::Optional<unsigned> NumExpansions) {
Douglas Gregor940bca72010-04-12 07:48:19 +00001443 TypeSourceInfo *OldDI = OldParm->getTypeSourceInfo();
Douglas Gregor5499af42011-01-05 23:12:31 +00001444 TypeSourceInfo *NewDI = 0;
1445
Douglas Gregor5499af42011-01-05 23:12:31 +00001446 TypeLoc OldTL = OldDI->getTypeLoc();
1447 if (isa<PackExpansionTypeLoc>(OldTL)) {
1448 PackExpansionTypeLoc ExpansionTL = cast<PackExpansionTypeLoc>(OldTL);
Douglas Gregor5499af42011-01-05 23:12:31 +00001449
1450 // We have a function parameter pack. Substitute into the pattern of the
1451 // expansion.
1452 NewDI = SubstType(ExpansionTL.getPatternLoc(), TemplateArgs,
1453 OldParm->getLocation(), OldParm->getDeclName());
1454 if (!NewDI)
1455 return 0;
1456
1457 if (NewDI->getType()->containsUnexpandedParameterPack()) {
1458 // We still have unexpanded parameter packs, which means that
1459 // our function parameter is still a function parameter pack.
1460 // Therefore, make its type a pack expansion type.
Douglas Gregor0dca5fd2011-01-14 17:04:44 +00001461 NewDI = CheckPackExpansion(NewDI, ExpansionTL.getEllipsisLoc(),
Douglas Gregor715e4612011-01-14 22:40:04 +00001462 NumExpansions);
Douglas Gregor5499af42011-01-05 23:12:31 +00001463 }
1464 } else {
1465 NewDI = SubstType(OldDI, TemplateArgs, OldParm->getLocation(),
1466 OldParm->getDeclName());
1467 }
1468
Douglas Gregor940bca72010-04-12 07:48:19 +00001469 if (!NewDI)
1470 return 0;
1471
1472 if (NewDI->getType()->isVoidType()) {
1473 Diag(OldParm->getLocation(), diag::err_param_with_void_type);
1474 return 0;
1475 }
1476
1477 ParmVarDecl *NewParm = CheckParameter(Context.getTranslationUnitDecl(),
Abramo Bagnaradff19302011-03-08 08:55:46 +00001478 OldParm->getInnerLocStart(),
Douglas Gregor940bca72010-04-12 07:48:19 +00001479 OldParm->getLocation(),
Abramo Bagnaradff19302011-03-08 08:55:46 +00001480 OldParm->getIdentifier(),
1481 NewDI->getType(), NewDI,
Douglas Gregorc4df4072010-04-19 22:54:31 +00001482 OldParm->getStorageClass(),
1483 OldParm->getStorageClassAsWritten());
Douglas Gregor940bca72010-04-12 07:48:19 +00001484 if (!NewParm)
1485 return 0;
Douglas Gregor6044d692010-05-19 17:02:24 +00001486
Douglas Gregor940bca72010-04-12 07:48:19 +00001487 // Mark the (new) default argument as uninstantiated (if any).
1488 if (OldParm->hasUninstantiatedDefaultArg()) {
1489 Expr *Arg = OldParm->getUninstantiatedDefaultArg();
1490 NewParm->setUninstantiatedDefaultArg(Arg);
Douglas Gregor758cb672010-10-12 18:23:32 +00001491 } else if (OldParm->hasUnparsedDefaultArg()) {
1492 NewParm->setUnparsedDefaultArg();
1493 UnparsedDefaultArgInstantiations[OldParm].push_back(NewParm);
Douglas Gregor940bca72010-04-12 07:48:19 +00001494 } else if (Expr *Arg = OldParm->getDefaultArg())
1495 NewParm->setUninstantiatedDefaultArg(Arg);
1496
1497 NewParm->setHasInheritedDefaultArg(OldParm->hasInheritedDefaultArg());
1498
Douglas Gregor5499af42011-01-05 23:12:31 +00001499 // FIXME: When OldParm is a parameter pack and NewParm is not a parameter
1500 // pack, we actually have a set of instantiated locations. Maintain this set!
Douglas Gregorf3010112011-01-07 16:43:16 +00001501 if (OldParm->isParameterPack() && !NewParm->isParameterPack()) {
1502 // Add the new parameter to
1503 CurrentInstantiationScope->InstantiatedLocalPackArg(OldParm, NewParm);
1504 } else {
1505 // Introduce an Old -> New mapping
Douglas Gregor5499af42011-01-05 23:12:31 +00001506 CurrentInstantiationScope->InstantiatedLocal(OldParm, NewParm);
Douglas Gregorf3010112011-01-07 16:43:16 +00001507 }
Douglas Gregor5499af42011-01-05 23:12:31 +00001508
Argyrios Kyrtzidis3816ed42010-07-19 10:14:41 +00001509 // FIXME: OldParm may come from a FunctionProtoType, in which case CurContext
1510 // can be anything, is this right ?
Fariborz Jahanian714447b2010-07-13 21:05:02 +00001511 NewParm->setDeclContext(CurContext);
John McCall8fb0d9d2011-05-01 22:35:37 +00001512
1513 NewParm->setScopeInfo(OldParm->getFunctionScopeDepth(),
1514 OldParm->getFunctionScopeIndex() + indexAdjustment);
Fariborz Jahaniana6c7efe2010-07-13 20:05:58 +00001515
Douglas Gregor940bca72010-04-12 07:48:19 +00001516 return NewParm;
1517}
1518
Douglas Gregordd472162011-01-07 00:20:55 +00001519/// \brief Substitute the given template arguments into the given set of
1520/// parameters, producing the set of parameter types that would be generated
1521/// from such a substitution.
1522bool Sema::SubstParmTypes(SourceLocation Loc,
1523 ParmVarDecl **Params, unsigned NumParams,
1524 const MultiLevelTemplateArgumentList &TemplateArgs,
Douglas Gregorf3010112011-01-07 16:43:16 +00001525 llvm::SmallVectorImpl<QualType> &ParamTypes,
1526 llvm::SmallVectorImpl<ParmVarDecl *> *OutParams) {
Douglas Gregordd472162011-01-07 00:20:55 +00001527 assert(!ActiveTemplateInstantiations.empty() &&
1528 "Cannot perform an instantiation without some context on the "
1529 "instantiation stack");
1530
1531 TemplateInstantiator Instantiator(*this, TemplateArgs, Loc,
1532 DeclarationName());
1533 return Instantiator.TransformFunctionTypeParams(Loc, Params, NumParams, 0,
Douglas Gregorf3010112011-01-07 16:43:16 +00001534 ParamTypes, OutParams);
Douglas Gregordd472162011-01-07 00:20:55 +00001535}
1536
John McCall76d824f2009-08-25 22:02:44 +00001537/// \brief Perform substitution on the base class specifiers of the
1538/// given class template specialization.
Douglas Gregor463421d2009-03-03 04:44:36 +00001539///
1540/// Produces a diagnostic and returns true on error, returns false and
1541/// attaches the instantiated base classes to the class template
1542/// specialization if successful.
Mike Stump11289f42009-09-09 15:08:12 +00001543bool
John McCall76d824f2009-08-25 22:02:44 +00001544Sema::SubstBaseSpecifiers(CXXRecordDecl *Instantiation,
1545 CXXRecordDecl *Pattern,
Douglas Gregor01afeef2009-08-28 20:31:08 +00001546 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor463421d2009-03-03 04:44:36 +00001547 bool Invalid = false;
Douglas Gregor6181ded2009-05-29 18:27:38 +00001548 llvm::SmallVector<CXXBaseSpecifier*, 4> InstantiatedBases;
Mike Stump11289f42009-09-09 15:08:12 +00001549 for (ClassTemplateSpecializationDecl::base_class_iterator
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001550 Base = Pattern->bases_begin(), BaseEnd = Pattern->bases_end();
Douglas Gregor2a72edd2009-03-10 18:52:44 +00001551 Base != BaseEnd; ++Base) {
Douglas Gregor463421d2009-03-03 04:44:36 +00001552 if (!Base->getType()->isDependentType()) {
Fariborz Jahanian5c14ec32009-07-22 17:41:53 +00001553 InstantiatedBases.push_back(new (Context) CXXBaseSpecifier(*Base));
Douglas Gregor463421d2009-03-03 04:44:36 +00001554 continue;
1555 }
1556
Douglas Gregor752a5952011-01-03 22:36:02 +00001557 SourceLocation EllipsisLoc;
Douglas Gregorc52264e2011-03-02 02:04:06 +00001558 TypeSourceInfo *BaseTypeLoc;
Douglas Gregor752a5952011-01-03 22:36:02 +00001559 if (Base->isPackExpansion()) {
1560 // This is a pack expansion. See whether we should expand it now, or
1561 // wait until later.
1562 llvm::SmallVector<UnexpandedParameterPack, 2> Unexpanded;
1563 collectUnexpandedParameterPacks(Base->getTypeSourceInfo()->getTypeLoc(),
1564 Unexpanded);
1565 bool ShouldExpand = false;
Douglas Gregora8bac7f2011-01-10 07:32:04 +00001566 bool RetainExpansion = false;
Douglas Gregor0dca5fd2011-01-14 17:04:44 +00001567 llvm::Optional<unsigned> NumExpansions;
Douglas Gregor752a5952011-01-03 22:36:02 +00001568 if (CheckParameterPacksForExpansion(Base->getEllipsisLoc(),
1569 Base->getSourceRange(),
1570 Unexpanded.data(), Unexpanded.size(),
1571 TemplateArgs, ShouldExpand,
Douglas Gregora8bac7f2011-01-10 07:32:04 +00001572 RetainExpansion,
Douglas Gregor752a5952011-01-03 22:36:02 +00001573 NumExpansions)) {
Douglas Gregor752a5952011-01-03 22:36:02 +00001574 Invalid = true;
Douglas Gregor44e7df62011-01-04 00:32:56 +00001575 continue;
Douglas Gregor752a5952011-01-03 22:36:02 +00001576 }
1577
1578 // If we should expand this pack expansion now, do so.
1579 if (ShouldExpand) {
Douglas Gregor0dca5fd2011-01-14 17:04:44 +00001580 for (unsigned I = 0; I != *NumExpansions; ++I) {
Douglas Gregor752a5952011-01-03 22:36:02 +00001581 Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(*this, I);
1582
1583 TypeSourceInfo *BaseTypeLoc = SubstType(Base->getTypeSourceInfo(),
1584 TemplateArgs,
1585 Base->getSourceRange().getBegin(),
1586 DeclarationName());
1587 if (!BaseTypeLoc) {
1588 Invalid = true;
1589 continue;
1590 }
1591
1592 if (CXXBaseSpecifier *InstantiatedBase
1593 = CheckBaseSpecifier(Instantiation,
1594 Base->getSourceRange(),
1595 Base->isVirtual(),
1596 Base->getAccessSpecifierAsWritten(),
1597 BaseTypeLoc,
1598 SourceLocation()))
1599 InstantiatedBases.push_back(InstantiatedBase);
1600 else
1601 Invalid = true;
1602 }
1603
1604 continue;
1605 }
1606
1607 // The resulting base specifier will (still) be a pack expansion.
1608 EllipsisLoc = Base->getEllipsisLoc();
Douglas Gregorc52264e2011-03-02 02:04:06 +00001609 Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(*this, -1);
1610 BaseTypeLoc = SubstType(Base->getTypeSourceInfo(),
1611 TemplateArgs,
1612 Base->getSourceRange().getBegin(),
1613 DeclarationName());
1614 } else {
1615 BaseTypeLoc = SubstType(Base->getTypeSourceInfo(),
1616 TemplateArgs,
1617 Base->getSourceRange().getBegin(),
1618 DeclarationName());
Douglas Gregor752a5952011-01-03 22:36:02 +00001619 }
1620
Nick Lewycky19b9f952010-07-26 16:56:01 +00001621 if (!BaseTypeLoc) {
Douglas Gregor463421d2009-03-03 04:44:36 +00001622 Invalid = true;
1623 continue;
1624 }
1625
1626 if (CXXBaseSpecifier *InstantiatedBase
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001627 = CheckBaseSpecifier(Instantiation,
Douglas Gregor463421d2009-03-03 04:44:36 +00001628 Base->getSourceRange(),
1629 Base->isVirtual(),
1630 Base->getAccessSpecifierAsWritten(),
Douglas Gregor752a5952011-01-03 22:36:02 +00001631 BaseTypeLoc,
1632 EllipsisLoc))
Douglas Gregor463421d2009-03-03 04:44:36 +00001633 InstantiatedBases.push_back(InstantiatedBase);
1634 else
1635 Invalid = true;
1636 }
1637
Douglas Gregor2a72edd2009-03-10 18:52:44 +00001638 if (!Invalid &&
Jay Foad7d0479f2009-05-21 09:52:38 +00001639 AttachBaseSpecifiers(Instantiation, InstantiatedBases.data(),
Douglas Gregor463421d2009-03-03 04:44:36 +00001640 InstantiatedBases.size()))
1641 Invalid = true;
1642
1643 return Invalid;
1644}
1645
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001646/// \brief Instantiate the definition of a class from a given pattern.
1647///
1648/// \param PointOfInstantiation The point of instantiation within the
1649/// source code.
1650///
1651/// \param Instantiation is the declaration whose definition is being
1652/// instantiated. This will be either a class template specialization
1653/// or a member class of a class template specialization.
1654///
1655/// \param Pattern is the pattern from which the instantiation
1656/// occurs. This will be either the declaration of a class template or
1657/// the declaration of a member class of a class template.
1658///
1659/// \param TemplateArgs The template arguments to be substituted into
1660/// the pattern.
1661///
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001662/// \param TSK the kind of implicit or explicit instantiation to perform.
Douglas Gregor8a2e6012009-08-24 15:23:48 +00001663///
1664/// \param Complain whether to complain if the class cannot be instantiated due
1665/// to the lack of a definition.
1666///
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001667/// \returns true if an error occurred, false otherwise.
1668bool
1669Sema::InstantiateClass(SourceLocation PointOfInstantiation,
1670 CXXRecordDecl *Instantiation, CXXRecordDecl *Pattern,
Douglas Gregor01afeef2009-08-28 20:31:08 +00001671 const MultiLevelTemplateArgumentList &TemplateArgs,
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001672 TemplateSpecializationKind TSK,
Douglas Gregor8a2e6012009-08-24 15:23:48 +00001673 bool Complain) {
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001674 bool Invalid = false;
John McCall87a44eb2009-08-20 01:44:21 +00001675
Mike Stump11289f42009-09-09 15:08:12 +00001676 CXXRecordDecl *PatternDef
Douglas Gregor0a5a2212010-02-11 01:04:33 +00001677 = cast_or_null<CXXRecordDecl>(Pattern->getDefinition());
John McCall54766662011-04-27 06:46:31 +00001678 if (!PatternDef || PatternDef->isBeingDefined()) {
1679 if (!Complain || (PatternDef && PatternDef->isInvalidDecl())) {
Douglas Gregor8a2e6012009-08-24 15:23:48 +00001680 // Say nothing
John McCall54766662011-04-27 06:46:31 +00001681 } else if (PatternDef) {
1682 assert(PatternDef->isBeingDefined());
1683 Diag(PointOfInstantiation,
1684 diag::err_template_instantiate_within_definition)
1685 << (TSK != TSK_ImplicitInstantiation)
1686 << Context.getTypeDeclType(Instantiation);
1687 // Not much point in noting the template declaration here, since
1688 // we're lexically inside it.
1689 Instantiation->setInvalidDecl();
Douglas Gregor8a2e6012009-08-24 15:23:48 +00001690 } else if (Pattern == Instantiation->getInstantiatedFromMemberClass()) {
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001691 Diag(PointOfInstantiation,
1692 diag::err_implicit_instantiate_member_undefined)
1693 << Context.getTypeDeclType(Instantiation);
1694 Diag(Pattern->getLocation(), diag::note_member_of_template_here);
1695 } else {
Douglas Gregora1f49972009-05-13 00:25:59 +00001696 Diag(PointOfInstantiation, diag::err_template_instantiate_undefined)
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001697 << (TSK != TSK_ImplicitInstantiation)
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001698 << Context.getTypeDeclType(Instantiation);
1699 Diag(Pattern->getLocation(), diag::note_template_decl_here);
1700 }
1701 return true;
1702 }
1703 Pattern = PatternDef;
1704
Douglas Gregord6ba93d2009-10-15 15:54:05 +00001705 // \brief Record the point of instantiation.
1706 if (MemberSpecializationInfo *MSInfo
1707 = Instantiation->getMemberSpecializationInfo()) {
1708 MSInfo->setTemplateSpecializationKind(TSK);
1709 MSInfo->setPointOfInstantiation(PointOfInstantiation);
Douglas Gregoref6ab412009-10-27 06:26:26 +00001710 } else if (ClassTemplateSpecializationDecl *Spec
1711 = dyn_cast<ClassTemplateSpecializationDecl>(Instantiation)) {
1712 Spec->setTemplateSpecializationKind(TSK);
1713 Spec->setPointOfInstantiation(PointOfInstantiation);
Douglas Gregord6ba93d2009-10-15 15:54:05 +00001714 }
1715
Douglas Gregorf3430ae2009-03-25 21:23:52 +00001716 InstantiatingTemplate Inst(*this, PointOfInstantiation, Instantiation);
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001717 if (Inst)
1718 return true;
1719
1720 // Enter the scope of this instantiation. We don't use
1721 // PushDeclContext because we don't have a scope.
John McCall80e58cd2010-04-29 00:35:03 +00001722 ContextRAII SavedContext(*this, Instantiation);
Douglas Gregor17158422010-05-12 17:27:19 +00001723 EnterExpressionEvaluationContext EvalContext(*this,
John McCallfaf5fb42010-08-26 23:41:50 +00001724 Sema::PotentiallyEvaluated);
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001725
Douglas Gregor51121572010-03-24 01:33:17 +00001726 // If this is an instantiation of a local class, merge this local
1727 // instantiation scope with the enclosing scope. Otherwise, every
1728 // instantiation of a class has its own local instantiation scope.
1729 bool MergeWithParentScope = !Instantiation->isDefinedOutsideFunctionOrMethod();
John McCall19c1bfd2010-08-25 05:32:35 +00001730 LocalInstantiationScope Scope(*this, MergeWithParentScope);
Douglas Gregor51121572010-03-24 01:33:17 +00001731
John McCall6602bb12010-08-01 02:01:53 +00001732 // Pull attributes from the pattern onto the instantiation.
1733 InstantiateAttrs(TemplateArgs, Pattern, Instantiation);
1734
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001735 // Start the definition of this instantiation.
1736 Instantiation->startDefinition();
Douglas Gregore9029562010-05-06 00:28:52 +00001737
1738 Instantiation->setTagKind(Pattern->getTagKind());
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001739
John McCall76d824f2009-08-25 22:02:44 +00001740 // Do substitution on the base class specifiers.
1741 if (SubstBaseSpecifiers(Instantiation, Pattern, TemplateArgs))
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001742 Invalid = true;
1743
Douglas Gregor869853e2010-11-10 19:44:59 +00001744 TemplateDeclInstantiator Instantiator(*this, Instantiation, TemplateArgs);
John McCall48871652010-08-21 09:40:31 +00001745 llvm::SmallVector<Decl*, 4> Fields;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001746 for (RecordDecl::decl_iterator Member = Pattern->decls_begin(),
Mike Stump11289f42009-09-09 15:08:12 +00001747 MemberEnd = Pattern->decls_end();
Douglas Gregorbcced4e2009-04-09 21:40:53 +00001748 Member != MemberEnd; ++Member) {
Argyrios Kyrtzidis9a94d9b2010-11-04 03:18:57 +00001749 // Don't instantiate members not belonging in this semantic context.
1750 // e.g. for:
1751 // @code
1752 // template <int i> class A {
1753 // class B *g;
1754 // };
1755 // @endcode
1756 // 'class B' has the template as lexical context but semantically it is
1757 // introduced in namespace scope.
1758 if ((*Member)->getDeclContext() != Pattern)
1759 continue;
1760
Douglas Gregor869853e2010-11-10 19:44:59 +00001761 if ((*Member)->isInvalidDecl()) {
1762 Invalid = true;
1763 continue;
1764 }
1765
1766 Decl *NewMember = Instantiator.Visit(*Member);
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001767 if (NewMember) {
Eli Friedmand0e8de22009-12-07 00:22:08 +00001768 if (FieldDecl *Field = dyn_cast<FieldDecl>(NewMember))
John McCall48871652010-08-21 09:40:31 +00001769 Fields.push_back(Field);
Eli Friedmand0e8de22009-12-07 00:22:08 +00001770 else if (NewMember->isInvalidDecl())
1771 Invalid = true;
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001772 } else {
1773 // FIXME: Eventually, a NULL return will mean that one of the
Mike Stump87c57ac2009-05-16 07:39:55 +00001774 // instantiations was a semantic disaster, and we'll want to set Invalid =
1775 // true. For now, we expect to skip some members that we can't yet handle.
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001776 }
1777 }
1778
1779 // Finish checking fields.
John McCall48871652010-08-21 09:40:31 +00001780 ActOnFields(0, Instantiation->getLocation(), Instantiation,
Jay Foad7d0479f2009-05-21 09:52:38 +00001781 Fields.data(), Fields.size(), SourceLocation(), SourceLocation(),
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001782 0);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001783 CheckCompletedCXXClass(Instantiation);
Douglas Gregor3c74d412009-10-14 20:14:33 +00001784 if (Instantiation->isInvalidDecl())
1785 Invalid = true;
Douglas Gregor869853e2010-11-10 19:44:59 +00001786 else {
1787 // Instantiate any out-of-line class template partial
1788 // specializations now.
1789 for (TemplateDeclInstantiator::delayed_partial_spec_iterator
1790 P = Instantiator.delayed_partial_spec_begin(),
1791 PEnd = Instantiator.delayed_partial_spec_end();
1792 P != PEnd; ++P) {
1793 if (!Instantiator.InstantiateClassTemplatePartialSpecialization(
1794 P->first,
1795 P->second)) {
1796 Invalid = true;
1797 break;
1798 }
1799 }
1800 }
1801
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001802 // Exit the scope of this instantiation.
John McCall80e58cd2010-04-29 00:35:03 +00001803 SavedContext.pop();
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001804
Douglas Gregor88d292c2010-05-13 16:44:06 +00001805 if (!Invalid) {
Douglas Gregor28ad4b52009-05-26 20:50:29 +00001806 Consumer.HandleTagDeclDefinition(Instantiation);
1807
Douglas Gregor88d292c2010-05-13 16:44:06 +00001808 // Always emit the vtable for an explicit instantiation definition
1809 // of a polymorphic class template specialization.
1810 if (TSK == TSK_ExplicitInstantiationDefinition)
1811 MarkVTableUsed(PointOfInstantiation, Instantiation, true);
1812 }
1813
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001814 return Invalid;
1815}
1816
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +00001817namespace {
1818 /// \brief A partial specialization whose template arguments have matched
1819 /// a given template-id.
1820 struct PartialSpecMatchResult {
1821 ClassTemplatePartialSpecializationDecl *Partial;
1822 TemplateArgumentList *Args;
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +00001823 };
1824}
1825
Mike Stump11289f42009-09-09 15:08:12 +00001826bool
Douglas Gregor463421d2009-03-03 04:44:36 +00001827Sema::InstantiateClassTemplateSpecialization(
Douglas Gregoref6ab412009-10-27 06:26:26 +00001828 SourceLocation PointOfInstantiation,
Douglas Gregor463421d2009-03-03 04:44:36 +00001829 ClassTemplateSpecializationDecl *ClassTemplateSpec,
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001830 TemplateSpecializationKind TSK,
Douglas Gregor8a2e6012009-08-24 15:23:48 +00001831 bool Complain) {
Douglas Gregor463421d2009-03-03 04:44:36 +00001832 // Perform the actual instantiation on the canonical declaration.
1833 ClassTemplateSpec = cast<ClassTemplateSpecializationDecl>(
Argyrios Kyrtzidis6b7e3762009-07-18 00:34:25 +00001834 ClassTemplateSpec->getCanonicalDecl());
Douglas Gregor463421d2009-03-03 04:44:36 +00001835
Douglas Gregor4aa04b12009-09-11 21:19:12 +00001836 // Check whether we have already instantiated or specialized this class
1837 // template specialization.
1838 if (ClassTemplateSpec->getSpecializationKind() != TSK_Undeclared) {
1839 if (ClassTemplateSpec->getSpecializationKind() ==
1840 TSK_ExplicitInstantiationDeclaration &&
1841 TSK == TSK_ExplicitInstantiationDefinition) {
1842 // An explicit instantiation definition follows an explicit instantiation
1843 // declaration (C++0x [temp.explicit]p10); go ahead and perform the
1844 // explicit instantiation.
1845 ClassTemplateSpec->setSpecializationKind(TSK);
Douglas Gregor88d292c2010-05-13 16:44:06 +00001846
1847 // If this is an explicit instantiation definition, mark the
1848 // vtable as used.
1849 if (TSK == TSK_ExplicitInstantiationDefinition)
1850 MarkVTableUsed(PointOfInstantiation, ClassTemplateSpec, true);
1851
Douglas Gregor4aa04b12009-09-11 21:19:12 +00001852 return false;
1853 }
1854
1855 // We can only instantiate something that hasn't already been
1856 // instantiated or specialized. Fail without any diagnostics: our
1857 // caller will provide an error message.
Douglas Gregor463421d2009-03-03 04:44:36 +00001858 return true;
Douglas Gregor4aa04b12009-09-11 21:19:12 +00001859 }
Douglas Gregor463421d2009-03-03 04:44:36 +00001860
Douglas Gregor00a511f2009-09-15 16:51:42 +00001861 if (ClassTemplateSpec->isInvalidDecl())
1862 return true;
1863
Douglas Gregor463421d2009-03-03 04:44:36 +00001864 ClassTemplateDecl *Template = ClassTemplateSpec->getSpecializedTemplate();
Douglas Gregor01afeef2009-08-28 20:31:08 +00001865 CXXRecordDecl *Pattern = 0;
Douglas Gregor2373c592009-05-31 09:31:02 +00001866
Douglas Gregor170bc422009-06-12 22:31:52 +00001867 // C++ [temp.class.spec.match]p1:
1868 // When a class template is used in a context that requires an
1869 // instantiation of the class, it is necessary to determine
1870 // whether the instantiation is to be generated using the primary
1871 // template or one of the partial specializations. This is done by
1872 // matching the template arguments of the class template
1873 // specialization with the template argument lists of the partial
1874 // specializations.
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +00001875 typedef PartialSpecMatchResult MatchResult;
Douglas Gregorb7ae10f2009-06-05 00:53:49 +00001876 llvm::SmallVector<MatchResult, 4> Matched;
Douglas Gregor407e9612010-04-30 05:56:50 +00001877 llvm::SmallVector<ClassTemplatePartialSpecializationDecl *, 4> PartialSpecs;
1878 Template->getPartialSpecializations(PartialSpecs);
1879 for (unsigned I = 0, N = PartialSpecs.size(); I != N; ++I) {
1880 ClassTemplatePartialSpecializationDecl *Partial = PartialSpecs[I];
John McCallbc077cf2010-02-08 23:07:23 +00001881 TemplateDeductionInfo Info(Context, PointOfInstantiation);
Douglas Gregor181aa4a2009-06-12 18:26:56 +00001882 if (TemplateDeductionResult Result
Douglas Gregor407e9612010-04-30 05:56:50 +00001883 = DeduceTemplateArguments(Partial,
Douglas Gregor181aa4a2009-06-12 18:26:56 +00001884 ClassTemplateSpec->getTemplateArgs(),
1885 Info)) {
1886 // FIXME: Store the failed-deduction information for use in
1887 // diagnostics, later.
1888 (void)Result;
1889 } else {
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +00001890 Matched.push_back(PartialSpecMatchResult());
1891 Matched.back().Partial = Partial;
1892 Matched.back().Args = Info.take();
Douglas Gregor181aa4a2009-06-12 18:26:56 +00001893 }
Douglas Gregor2373c592009-05-31 09:31:02 +00001894 }
1895
Douglas Gregor0231d8d2011-01-19 20:10:05 +00001896 // If we're dealing with a member template where the template parameters
1897 // have been instantiated, this provides the original template parameters
1898 // from which the member template's parameters were instantiated.
1899 llvm::SmallVector<const NamedDecl *, 4> InstantiatedTemplateParameters;
1900
Douglas Gregor21610382009-10-29 00:04:11 +00001901 if (Matched.size() >= 1) {
Douglas Gregorbe999392009-09-15 16:23:51 +00001902 llvm::SmallVector<MatchResult, 4>::iterator Best = Matched.begin();
Douglas Gregor21610382009-10-29 00:04:11 +00001903 if (Matched.size() == 1) {
1904 // -- If exactly one matching specialization is found, the
1905 // instantiation is generated from that specialization.
1906 // We don't need to do anything for this.
1907 } else {
1908 // -- If more than one matching specialization is found, the
1909 // partial order rules (14.5.4.2) are used to determine
1910 // whether one of the specializations is more specialized
1911 // than the others. If none of the specializations is more
1912 // specialized than all of the other matching
1913 // specializations, then the use of the class template is
1914 // ambiguous and the program is ill-formed.
1915 for (llvm::SmallVector<MatchResult, 4>::iterator P = Best + 1,
1916 PEnd = Matched.end();
1917 P != PEnd; ++P) {
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +00001918 if (getMoreSpecializedPartialSpecialization(P->Partial, Best->Partial,
John McCallbc077cf2010-02-08 23:07:23 +00001919 PointOfInstantiation)
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +00001920 == P->Partial)
Douglas Gregor21610382009-10-29 00:04:11 +00001921 Best = P;
Douglas Gregorbe999392009-09-15 16:23:51 +00001922 }
Douglas Gregorbe999392009-09-15 16:23:51 +00001923
Douglas Gregor21610382009-10-29 00:04:11 +00001924 // Determine if the best partial specialization is more specialized than
1925 // the others.
1926 bool Ambiguous = false;
Douglas Gregorbe999392009-09-15 16:23:51 +00001927 for (llvm::SmallVector<MatchResult, 4>::iterator P = Matched.begin(),
1928 PEnd = Matched.end();
Douglas Gregor21610382009-10-29 00:04:11 +00001929 P != PEnd; ++P) {
1930 if (P != Best &&
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +00001931 getMoreSpecializedPartialSpecialization(P->Partial, Best->Partial,
John McCallbc077cf2010-02-08 23:07:23 +00001932 PointOfInstantiation)
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +00001933 != Best->Partial) {
Douglas Gregor21610382009-10-29 00:04:11 +00001934 Ambiguous = true;
1935 break;
1936 }
1937 }
1938
1939 if (Ambiguous) {
1940 // Partial ordering did not produce a clear winner. Complain.
1941 ClassTemplateSpec->setInvalidDecl();
1942 Diag(PointOfInstantiation, diag::err_partial_spec_ordering_ambiguous)
1943 << ClassTemplateSpec;
1944
1945 // Print the matching partial specializations.
1946 for (llvm::SmallVector<MatchResult, 4>::iterator P = Matched.begin(),
1947 PEnd = Matched.end();
1948 P != PEnd; ++P)
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +00001949 Diag(P->Partial->getLocation(), diag::note_partial_spec_match)
1950 << getTemplateArgumentBindingsText(
1951 P->Partial->getTemplateParameters(),
1952 *P->Args);
Douglas Gregor01afeef2009-08-28 20:31:08 +00001953
Douglas Gregor21610382009-10-29 00:04:11 +00001954 return true;
1955 }
Douglas Gregorbe999392009-09-15 16:23:51 +00001956 }
1957
1958 // Instantiate using the best class template partial specialization.
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +00001959 ClassTemplatePartialSpecializationDecl *OrigPartialSpec = Best->Partial;
Douglas Gregor21610382009-10-29 00:04:11 +00001960 while (OrigPartialSpec->getInstantiatedFromMember()) {
1961 // If we've found an explicit specialization of this class template,
1962 // stop here and use that as the pattern.
1963 if (OrigPartialSpec->isMemberSpecialization())
1964 break;
1965
1966 OrigPartialSpec = OrigPartialSpec->getInstantiatedFromMember();
1967 }
1968
1969 Pattern = OrigPartialSpec;
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +00001970 ClassTemplateSpec->setInstantiationOf(Best->Partial, Best->Args);
Douglas Gregor170bc422009-06-12 22:31:52 +00001971 } else {
1972 // -- If no matches are found, the instantiation is generated
1973 // from the primary template.
Douglas Gregor01afeef2009-08-28 20:31:08 +00001974 ClassTemplateDecl *OrigTemplate = Template;
Douglas Gregorcf915552009-10-13 16:30:37 +00001975 while (OrigTemplate->getInstantiatedFromMemberTemplate()) {
1976 // If we've found an explicit specialization of this class template,
1977 // stop here and use that as the pattern.
1978 if (OrigTemplate->isMemberSpecialization())
1979 break;
1980
Douglas Gregor01afeef2009-08-28 20:31:08 +00001981 OrigTemplate = OrigTemplate->getInstantiatedFromMemberTemplate();
Douglas Gregorcf915552009-10-13 16:30:37 +00001982 }
1983
Douglas Gregor01afeef2009-08-28 20:31:08 +00001984 Pattern = OrigTemplate->getTemplatedDecl();
Douglas Gregor2373c592009-05-31 09:31:02 +00001985 }
Douglas Gregor463421d2009-03-03 04:44:36 +00001986
Douglas Gregoref6ab412009-10-27 06:26:26 +00001987 bool Result = InstantiateClass(PointOfInstantiation, ClassTemplateSpec,
1988 Pattern,
1989 getTemplateInstantiationArgs(ClassTemplateSpec),
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001990 TSK,
Douglas Gregor8a2e6012009-08-24 15:23:48 +00001991 Complain);
Mike Stump11289f42009-09-09 15:08:12 +00001992
Douglas Gregorb7ae10f2009-06-05 00:53:49 +00001993 return Result;
Douglas Gregor463421d2009-03-03 04:44:36 +00001994}
Douglas Gregor90a1a652009-03-19 17:26:29 +00001995
John McCall76d824f2009-08-25 22:02:44 +00001996/// \brief Instantiates the definitions of all of the member
1997/// of the given class, which is an instantiation of a class template
1998/// or a member class of a template.
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00001999void
2000Sema::InstantiateClassMembers(SourceLocation PointOfInstantiation,
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00002001 CXXRecordDecl *Instantiation,
2002 const MultiLevelTemplateArgumentList &TemplateArgs,
2003 TemplateSpecializationKind TSK) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002004 for (DeclContext::decl_iterator D = Instantiation->decls_begin(),
2005 DEnd = Instantiation->decls_end();
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00002006 D != DEnd; ++D) {
Douglas Gregor1d957a32009-10-27 18:42:08 +00002007 bool SuppressNew = false;
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00002008 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(*D)) {
Douglas Gregor1d957a32009-10-27 18:42:08 +00002009 if (FunctionDecl *Pattern
2010 = Function->getInstantiatedFromMemberFunction()) {
2011 MemberSpecializationInfo *MSInfo
2012 = Function->getMemberSpecializationInfo();
2013 assert(MSInfo && "No member specialization information?");
Douglas Gregor06aa50412010-04-09 21:02:29 +00002014 if (MSInfo->getTemplateSpecializationKind()
2015 == TSK_ExplicitSpecialization)
2016 continue;
2017
Douglas Gregor1d957a32009-10-27 18:42:08 +00002018 if (CheckSpecializationInstantiationRedecl(PointOfInstantiation, TSK,
2019 Function,
2020 MSInfo->getTemplateSpecializationKind(),
Nico Weberd75488d2010-09-27 21:02:09 +00002021 MSInfo->getPointOfInstantiation(),
Douglas Gregor1d957a32009-10-27 18:42:08 +00002022 SuppressNew) ||
2023 SuppressNew)
Douglas Gregorbbe8f462009-10-08 15:14:33 +00002024 continue;
2025
Alexis Hunt4a8ea102011-05-06 20:44:56 +00002026 if (Function->isDefined())
Douglas Gregor1d957a32009-10-27 18:42:08 +00002027 continue;
2028
2029 if (TSK == TSK_ExplicitInstantiationDefinition) {
2030 // C++0x [temp.explicit]p8:
2031 // An explicit instantiation definition that names a class template
2032 // specialization explicitly instantiates the class template
2033 // specialization and is only an explicit instantiation definition
2034 // of members whose definition is visible at the point of
2035 // instantiation.
Alexis Hunt4a8ea102011-05-06 20:44:56 +00002036 if (!Pattern->isDefined())
Douglas Gregor1d957a32009-10-27 18:42:08 +00002037 continue;
2038
2039 Function->setTemplateSpecializationKind(TSK, PointOfInstantiation);
2040
2041 InstantiateFunctionDefinition(PointOfInstantiation, Function);
2042 } else {
2043 Function->setTemplateSpecializationKind(TSK, PointOfInstantiation);
2044 }
Douglas Gregorbbe8f462009-10-08 15:14:33 +00002045 }
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00002046 } else if (VarDecl *Var = dyn_cast<VarDecl>(*D)) {
Douglas Gregor86d142a2009-10-08 07:24:58 +00002047 if (Var->isStaticDataMember()) {
Douglas Gregor1d957a32009-10-27 18:42:08 +00002048 MemberSpecializationInfo *MSInfo = Var->getMemberSpecializationInfo();
2049 assert(MSInfo && "No member specialization information?");
Douglas Gregor06aa50412010-04-09 21:02:29 +00002050 if (MSInfo->getTemplateSpecializationKind()
2051 == TSK_ExplicitSpecialization)
2052 continue;
2053
Douglas Gregor1d957a32009-10-27 18:42:08 +00002054 if (CheckSpecializationInstantiationRedecl(PointOfInstantiation, TSK,
2055 Var,
2056 MSInfo->getTemplateSpecializationKind(),
Nico Weberd75488d2010-09-27 21:02:09 +00002057 MSInfo->getPointOfInstantiation(),
Douglas Gregor1d957a32009-10-27 18:42:08 +00002058 SuppressNew) ||
2059 SuppressNew)
Douglas Gregorbbe8f462009-10-08 15:14:33 +00002060 continue;
2061
Douglas Gregor1d957a32009-10-27 18:42:08 +00002062 if (TSK == TSK_ExplicitInstantiationDefinition) {
2063 // C++0x [temp.explicit]p8:
2064 // An explicit instantiation definition that names a class template
2065 // specialization explicitly instantiates the class template
2066 // specialization and is only an explicit instantiation definition
2067 // of members whose definition is visible at the point of
2068 // instantiation.
2069 if (!Var->getInstantiatedFromStaticDataMember()
2070 ->getOutOfLineDefinition())
2071 continue;
2072
2073 Var->setTemplateSpecializationKind(TSK, PointOfInstantiation);
Douglas Gregor86d142a2009-10-08 07:24:58 +00002074 InstantiateStaticDataMemberDefinition(PointOfInstantiation, Var);
Douglas Gregor1d957a32009-10-27 18:42:08 +00002075 } else {
2076 Var->setTemplateSpecializationKind(TSK, PointOfInstantiation);
2077 }
2078 }
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00002079 } else if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(*D)) {
Douglas Gregor1da22252010-04-18 18:11:38 +00002080 // Always skip the injected-class-name, along with any
2081 // redeclarations of nested classes, since both would cause us
2082 // to try to instantiate the members of a class twice.
2083 if (Record->isInjectedClassName() || Record->getPreviousDeclaration())
Douglas Gregord801b062009-10-07 23:56:10 +00002084 continue;
2085
Douglas Gregor1d957a32009-10-27 18:42:08 +00002086 MemberSpecializationInfo *MSInfo = Record->getMemberSpecializationInfo();
2087 assert(MSInfo && "No member specialization information?");
Douglas Gregor06aa50412010-04-09 21:02:29 +00002088
2089 if (MSInfo->getTemplateSpecializationKind()
2090 == TSK_ExplicitSpecialization)
2091 continue;
Nico Weberd75488d2010-09-27 21:02:09 +00002092
Douglas Gregor1d957a32009-10-27 18:42:08 +00002093 if (CheckSpecializationInstantiationRedecl(PointOfInstantiation, TSK,
2094 Record,
2095 MSInfo->getTemplateSpecializationKind(),
Nico Weberd75488d2010-09-27 21:02:09 +00002096 MSInfo->getPointOfInstantiation(),
Douglas Gregor1d957a32009-10-27 18:42:08 +00002097 SuppressNew) ||
2098 SuppressNew)
Douglas Gregorbbe8f462009-10-08 15:14:33 +00002099 continue;
2100
Douglas Gregor1d957a32009-10-27 18:42:08 +00002101 CXXRecordDecl *Pattern = Record->getInstantiatedFromMemberClass();
2102 assert(Pattern && "Missing instantiated-from-template information");
2103
Douglas Gregor0a5a2212010-02-11 01:04:33 +00002104 if (!Record->getDefinition()) {
2105 if (!Pattern->getDefinition()) {
Douglas Gregor1d957a32009-10-27 18:42:08 +00002106 // C++0x [temp.explicit]p8:
2107 // An explicit instantiation definition that names a class template
2108 // specialization explicitly instantiates the class template
2109 // specialization and is only an explicit instantiation definition
2110 // of members whose definition is visible at the point of
2111 // instantiation.
2112 if (TSK == TSK_ExplicitInstantiationDeclaration) {
2113 MSInfo->setTemplateSpecializationKind(TSK);
2114 MSInfo->setPointOfInstantiation(PointOfInstantiation);
2115 }
2116
2117 continue;
2118 }
2119
2120 InstantiateClass(PointOfInstantiation, Record, Pattern,
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00002121 TemplateArgs,
2122 TSK);
Nico Weberd75488d2010-09-27 21:02:09 +00002123 } else {
2124 if (TSK == TSK_ExplicitInstantiationDefinition &&
2125 Record->getTemplateSpecializationKind() ==
2126 TSK_ExplicitInstantiationDeclaration) {
2127 Record->setTemplateSpecializationKind(TSK);
2128 MarkVTableUsed(PointOfInstantiation, Record, true);
2129 }
Douglas Gregor1d957a32009-10-27 18:42:08 +00002130 }
Douglas Gregorc093c1d2009-10-08 01:19:17 +00002131
Douglas Gregor0a5a2212010-02-11 01:04:33 +00002132 Pattern = cast_or_null<CXXRecordDecl>(Record->getDefinition());
Douglas Gregor1d957a32009-10-27 18:42:08 +00002133 if (Pattern)
2134 InstantiateClassMembers(PointOfInstantiation, Pattern, TemplateArgs,
2135 TSK);
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00002136 }
2137 }
2138}
2139
2140/// \brief Instantiate the definitions of all of the members of the
2141/// given class template specialization, which was named as part of an
2142/// explicit instantiation.
Mike Stump11289f42009-09-09 15:08:12 +00002143void
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00002144Sema::InstantiateClassTemplateSpecializationMembers(
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00002145 SourceLocation PointOfInstantiation,
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00002146 ClassTemplateSpecializationDecl *ClassTemplateSpec,
2147 TemplateSpecializationKind TSK) {
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00002148 // C++0x [temp.explicit]p7:
2149 // An explicit instantiation that names a class template
2150 // specialization is an explicit instantion of the same kind
2151 // (declaration or definition) of each of its members (not
2152 // including members inherited from base classes) that has not
2153 // been previously explicitly specialized in the translation unit
2154 // containing the explicit instantiation, except as described
2155 // below.
2156 InstantiateClassMembers(PointOfInstantiation, ClassTemplateSpec,
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00002157 getTemplateInstantiationArgs(ClassTemplateSpec),
2158 TSK);
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00002159}
2160
John McCalldadc5752010-08-24 06:29:42 +00002161StmtResult
Douglas Gregor01afeef2009-08-28 20:31:08 +00002162Sema::SubstStmt(Stmt *S, const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregorebe10102009-08-20 07:17:43 +00002163 if (!S)
2164 return Owned(S);
2165
2166 TemplateInstantiator Instantiator(*this, TemplateArgs,
2167 SourceLocation(),
2168 DeclarationName());
2169 return Instantiator.TransformStmt(S);
2170}
2171
John McCalldadc5752010-08-24 06:29:42 +00002172ExprResult
Douglas Gregor01afeef2009-08-28 20:31:08 +00002173Sema::SubstExpr(Expr *E, const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregora16548e2009-08-11 05:31:07 +00002174 if (!E)
2175 return Owned(E);
Mike Stump11289f42009-09-09 15:08:12 +00002176
Douglas Gregora16548e2009-08-11 05:31:07 +00002177 TemplateInstantiator Instantiator(*this, TemplateArgs,
2178 SourceLocation(),
2179 DeclarationName());
2180 return Instantiator.TransformExpr(E);
2181}
2182
Douglas Gregor2cd32a02011-01-07 19:35:17 +00002183bool Sema::SubstExprs(Expr **Exprs, unsigned NumExprs, bool IsCall,
2184 const MultiLevelTemplateArgumentList &TemplateArgs,
2185 llvm::SmallVectorImpl<Expr *> &Outputs) {
2186 if (NumExprs == 0)
2187 return false;
2188
2189 TemplateInstantiator Instantiator(*this, TemplateArgs,
2190 SourceLocation(),
2191 DeclarationName());
2192 return Instantiator.TransformExprs(Exprs, NumExprs, IsCall, Outputs);
2193}
2194
Douglas Gregor14454802011-02-25 02:25:35 +00002195NestedNameSpecifierLoc
2196Sema::SubstNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS,
2197 const MultiLevelTemplateArgumentList &TemplateArgs) {
2198 if (!NNS)
2199 return NestedNameSpecifierLoc();
2200
2201 TemplateInstantiator Instantiator(*this, TemplateArgs, NNS.getBeginLoc(),
2202 DeclarationName());
2203 return Instantiator.TransformNestedNameSpecifierLoc(NNS);
2204}
2205
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002206/// \brief Do template substitution on declaration name info.
2207DeclarationNameInfo
2208Sema::SubstDeclarationNameInfo(const DeclarationNameInfo &NameInfo,
2209 const MultiLevelTemplateArgumentList &TemplateArgs) {
2210 TemplateInstantiator Instantiator(*this, TemplateArgs, NameInfo.getLoc(),
2211 NameInfo.getName());
2212 return Instantiator.TransformDeclarationNameInfo(NameInfo);
2213}
2214
Douglas Gregoraa594892009-03-31 18:38:02 +00002215TemplateName
Douglas Gregordf846d12011-03-02 18:46:51 +00002216Sema::SubstTemplateName(NestedNameSpecifierLoc QualifierLoc,
2217 TemplateName Name, SourceLocation Loc,
Douglas Gregor01afeef2009-08-28 20:31:08 +00002218 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor71dc5092009-08-06 06:41:21 +00002219 TemplateInstantiator Instantiator(*this, TemplateArgs, Loc,
2220 DeclarationName());
Douglas Gregordf846d12011-03-02 18:46:51 +00002221 CXXScopeSpec SS;
2222 SS.Adopt(QualifierLoc);
2223 return Instantiator.TransformTemplateName(SS, Name, Loc);
Douglas Gregoraa594892009-03-31 18:38:02 +00002224}
Douglas Gregorc43620d2009-06-11 00:06:24 +00002225
Douglas Gregor0f3feb42010-12-22 21:19:48 +00002226bool Sema::Subst(const TemplateArgumentLoc *Args, unsigned NumArgs,
2227 TemplateArgumentListInfo &Result,
John McCall0ad16662009-10-29 08:12:44 +00002228 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregore922c772009-08-04 22:27:00 +00002229 TemplateInstantiator Instantiator(*this, TemplateArgs, SourceLocation(),
2230 DeclarationName());
Douglas Gregor0f3feb42010-12-22 21:19:48 +00002231
2232 return Instantiator.TransformTemplateArguments(Args, NumArgs, Result);
Douglas Gregorc43620d2009-06-11 00:06:24 +00002233}
Douglas Gregor14cf7522010-04-30 18:55:50 +00002234
Douglas Gregorf3010112011-01-07 16:43:16 +00002235llvm::PointerUnion<Decl *, LocalInstantiationScope::DeclArgumentPack *> *
2236LocalInstantiationScope::findInstantiationOf(const Decl *D) {
Chris Lattnercab02a62011-02-17 20:34:02 +00002237 for (LocalInstantiationScope *Current = this; Current;
Douglas Gregor14cf7522010-04-30 18:55:50 +00002238 Current = Current->Outer) {
Chris Lattnercab02a62011-02-17 20:34:02 +00002239
Douglas Gregor14cf7522010-04-30 18:55:50 +00002240 // Check if we found something within this scope.
Douglas Gregore9fc8dc2010-12-21 21:22:51 +00002241 const Decl *CheckD = D;
2242 do {
Douglas Gregorf3010112011-01-07 16:43:16 +00002243 LocalDeclsMap::iterator Found = Current->LocalDecls.find(CheckD);
Douglas Gregore9fc8dc2010-12-21 21:22:51 +00002244 if (Found != Current->LocalDecls.end())
Douglas Gregorf3010112011-01-07 16:43:16 +00002245 return &Found->second;
Douglas Gregore9fc8dc2010-12-21 21:22:51 +00002246
2247 // If this is a tag declaration, it's possible that we need to look for
2248 // a previous declaration.
2249 if (const TagDecl *Tag = dyn_cast<TagDecl>(CheckD))
2250 CheckD = Tag->getPreviousDeclaration();
2251 else
2252 CheckD = 0;
2253 } while (CheckD);
2254
Douglas Gregor14cf7522010-04-30 18:55:50 +00002255 // If we aren't combined with our outer scope, we're done.
2256 if (!Current->CombineWithOuterScope)
2257 break;
2258 }
Chris Lattnercab02a62011-02-17 20:34:02 +00002259
2260 // If we didn't find the decl, then we either have a sema bug, or we have a
2261 // forward reference to a label declaration. Return null to indicate that
2262 // we have an uninstantiated label.
2263 assert(isa<LabelDecl>(D) && "declaration not instantiated in this scope");
Douglas Gregor14cf7522010-04-30 18:55:50 +00002264 return 0;
2265}
2266
John McCall19c1bfd2010-08-25 05:32:35 +00002267void LocalInstantiationScope::InstantiatedLocal(const Decl *D, Decl *Inst) {
Douglas Gregorf3010112011-01-07 16:43:16 +00002268 llvm::PointerUnion<Decl *, DeclArgumentPack *> &Stored = LocalDecls[D];
Douglas Gregora8bac7f2011-01-10 07:32:04 +00002269 if (Stored.isNull())
2270 Stored = Inst;
2271 else if (Stored.is<Decl *>()) {
2272 assert(Stored.get<Decl *>() == Inst && "Already instantiated this local");
2273 Stored = Inst;
2274 } else
2275 LocalDecls[D].get<DeclArgumentPack *>()->push_back(Inst);
Douglas Gregor14cf7522010-04-30 18:55:50 +00002276}
Douglas Gregorf3010112011-01-07 16:43:16 +00002277
2278void LocalInstantiationScope::InstantiatedLocalPackArg(const Decl *D,
2279 Decl *Inst) {
2280 DeclArgumentPack *Pack = LocalDecls[D].get<DeclArgumentPack *>();
2281 Pack->push_back(Inst);
2282}
2283
2284void LocalInstantiationScope::MakeInstantiatedLocalArgPack(const Decl *D) {
2285 llvm::PointerUnion<Decl *, DeclArgumentPack *> &Stored = LocalDecls[D];
2286 assert(Stored.isNull() && "Already instantiated this local");
2287 DeclArgumentPack *Pack = new DeclArgumentPack;
2288 Stored = Pack;
2289 ArgumentPacks.push_back(Pack);
2290}
2291
Douglas Gregora8bac7f2011-01-10 07:32:04 +00002292void LocalInstantiationScope::SetPartiallySubstitutedPack(NamedDecl *Pack,
2293 const TemplateArgument *ExplicitArgs,
2294 unsigned NumExplicitArgs) {
2295 assert((!PartiallySubstitutedPack || PartiallySubstitutedPack == Pack) &&
2296 "Already have a partially-substituted pack");
2297 assert((!PartiallySubstitutedPack
2298 || NumArgsInPartiallySubstitutedPack == NumExplicitArgs) &&
2299 "Wrong number of arguments in partially-substituted pack");
2300 PartiallySubstitutedPack = Pack;
2301 ArgsInPartiallySubstitutedPack = ExplicitArgs;
2302 NumArgsInPartiallySubstitutedPack = NumExplicitArgs;
2303}
2304
2305NamedDecl *LocalInstantiationScope::getPartiallySubstitutedPack(
2306 const TemplateArgument **ExplicitArgs,
2307 unsigned *NumExplicitArgs) const {
2308 if (ExplicitArgs)
2309 *ExplicitArgs = 0;
2310 if (NumExplicitArgs)
2311 *NumExplicitArgs = 0;
2312
2313 for (const LocalInstantiationScope *Current = this; Current;
2314 Current = Current->Outer) {
2315 if (Current->PartiallySubstitutedPack) {
2316 if (ExplicitArgs)
2317 *ExplicitArgs = Current->ArgsInPartiallySubstitutedPack;
2318 if (NumExplicitArgs)
2319 *NumExplicitArgs = Current->NumArgsInPartiallySubstitutedPack;
2320
2321 return Current->PartiallySubstitutedPack;
2322 }
2323
2324 if (!Current->CombineWithOuterScope)
2325 break;
2326 }
2327
2328 return 0;
2329}