blob: 24acb7a6e03d89d3eb89ac293c5a6839367438d1 [file] [log] [blame]
Douglas Gregorfe1e1102009-02-27 19:31:52 +00001//===------- SemaTemplateInstantiate.cpp - C++ Template Instantiation ------===/
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//===----------------------------------------------------------------------===/
8//
9// This file implements C++ template instantiation.
10//
11//===----------------------------------------------------------------------===/
12
John McCall83024632010-08-25 22:03:47 +000013#include "clang/Sema/SemaInternal.h"
Douglas Gregord6ff3322009-08-04 16:50:30 +000014#include "TreeTransform.h"
John McCall8b0666c2010-08-20 18:27:03 +000015#include "clang/Sema/DeclSpec.h"
Douglas Gregorc3a6ade2010-08-12 20:07:10 +000016#include "clang/Sema/Lookup.h"
John McCallde6836a2010-08-24 07:21:54 +000017#include "clang/Sema/Template.h"
John McCall19c1bfd2010-08-25 05:32:35 +000018#include "clang/Sema/TemplateDeduction.h"
Douglas Gregor28ad4b52009-05-26 20:50:29 +000019#include "clang/AST/ASTConsumer.h"
Douglas Gregorfe1e1102009-02-27 19:31:52 +000020#include "clang/AST/ASTContext.h"
21#include "clang/AST/Expr.h"
Douglas Gregorfe1e1102009-02-27 19:31:52 +000022#include "clang/AST/DeclTemplate.h"
Douglas Gregorfe1e1102009-02-27 19:31:52 +000023#include "clang/Basic/LangOptions.h"
24
25using namespace clang;
John McCall19c1bfd2010-08-25 05:32:35 +000026using namespace sema;
Douglas Gregorfe1e1102009-02-27 19:31:52 +000027
Douglas Gregor4ea568f2009-03-10 18:03:33 +000028//===----------------------------------------------------------------------===/
29// Template Instantiation Support
30//===----------------------------------------------------------------------===/
31
Douglas Gregor01afeef2009-08-28 20:31:08 +000032/// \brief Retrieve the template argument list(s) that should be used to
33/// instantiate the definition of the given declaration.
Douglas Gregor36d7c5f2009-11-09 19:17:50 +000034///
35/// \param D the declaration for which we are computing template instantiation
36/// arguments.
37///
38/// \param Innermost if non-NULL, the innermost template argument list.
Douglas Gregor8c702532010-02-05 07:33:43 +000039///
40/// \param RelativeToPrimary true if we should get the template
41/// arguments relative to the primary template, even when we're
42/// dealing with a specialization. This is only relevant for function
43/// template specializations.
Douglas Gregor1bd7a942010-05-03 23:29:10 +000044///
45/// \param Pattern If non-NULL, indicates the pattern from which we will be
46/// instantiating the definition of the given declaration, \p D. This is
47/// used to determine the proper set of template instantiation arguments for
48/// friend function template specializations.
Douglas Gregora654dd82009-08-28 17:37:35 +000049MultiLevelTemplateArgumentList
Douglas Gregor36d7c5f2009-11-09 19:17:50 +000050Sema::getTemplateInstantiationArgs(NamedDecl *D,
Douglas Gregor8c702532010-02-05 07:33:43 +000051 const TemplateArgumentList *Innermost,
Douglas Gregor1bd7a942010-05-03 23:29:10 +000052 bool RelativeToPrimary,
53 const FunctionDecl *Pattern) {
Douglas Gregora654dd82009-08-28 17:37:35 +000054 // Accumulate the set of template argument lists in this structure.
55 MultiLevelTemplateArgumentList Result;
Mike Stump11289f42009-09-09 15:08:12 +000056
Douglas Gregor36d7c5f2009-11-09 19:17:50 +000057 if (Innermost)
58 Result.addOuterTemplateArguments(Innermost);
59
Douglas Gregora654dd82009-08-28 17:37:35 +000060 DeclContext *Ctx = dyn_cast<DeclContext>(D);
61 if (!Ctx)
62 Ctx = D->getDeclContext();
Mike Stump11289f42009-09-09 15:08:12 +000063
John McCall970d5302009-08-29 03:16:09 +000064 while (!Ctx->isFileContext()) {
Douglas Gregora654dd82009-08-28 17:37:35 +000065 // Add template arguments from a class template instantiation.
Mike Stump11289f42009-09-09 15:08:12 +000066 if (ClassTemplateSpecializationDecl *Spec
Douglas Gregora654dd82009-08-28 17:37:35 +000067 = dyn_cast<ClassTemplateSpecializationDecl>(Ctx)) {
68 // We're done when we hit an explicit specialization.
Douglas Gregor9961ce92010-07-08 18:37:38 +000069 if (Spec->getSpecializationKind() == TSK_ExplicitSpecialization &&
70 !isa<ClassTemplatePartialSpecializationDecl>(Spec))
Douglas Gregora654dd82009-08-28 17:37:35 +000071 break;
Mike Stump11289f42009-09-09 15:08:12 +000072
Douglas Gregora654dd82009-08-28 17:37:35 +000073 Result.addOuterTemplateArguments(&Spec->getTemplateInstantiationArgs());
Douglas Gregorcf915552009-10-13 16:30:37 +000074
75 // If this class template specialization was instantiated from a
76 // specialized member that is a class template, we're done.
77 assert(Spec->getSpecializedTemplate() && "No class template?");
78 if (Spec->getSpecializedTemplate()->isMemberSpecialization())
79 break;
Mike Stump11289f42009-09-09 15:08:12 +000080 }
Douglas Gregora654dd82009-08-28 17:37:35 +000081 // Add template arguments from a function template specialization.
John McCall970d5302009-08-29 03:16:09 +000082 else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Ctx)) {
Douglas Gregor8c702532010-02-05 07:33:43 +000083 if (!RelativeToPrimary &&
84 Function->getTemplateSpecializationKind()
85 == TSK_ExplicitSpecialization)
Douglas Gregorcf915552009-10-13 16:30:37 +000086 break;
87
Douglas Gregora654dd82009-08-28 17:37:35 +000088 if (const TemplateArgumentList *TemplateArgs
Douglas Gregorcf915552009-10-13 16:30:37 +000089 = Function->getTemplateSpecializationArgs()) {
90 // Add the template arguments for this specialization.
Douglas Gregora654dd82009-08-28 17:37:35 +000091 Result.addOuterTemplateArguments(TemplateArgs);
John McCall970d5302009-08-29 03:16:09 +000092
Douglas Gregorcf915552009-10-13 16:30:37 +000093 // If this function was instantiated from a specialized member that is
94 // a function template, we're done.
95 assert(Function->getPrimaryTemplate() && "No function template?");
96 if (Function->getPrimaryTemplate()->isMemberSpecialization())
97 break;
98 }
99
John McCall970d5302009-08-29 03:16:09 +0000100 // If this is a friend declaration and it declares an entity at
101 // namespace scope, take arguments from its lexical parent
Douglas Gregor1bd7a942010-05-03 23:29:10 +0000102 // instead of its semantic parent, unless of course the pattern we're
103 // instantiating actually comes from the file's context!
John McCall970d5302009-08-29 03:16:09 +0000104 if (Function->getFriendObjectKind() &&
Douglas Gregor1bd7a942010-05-03 23:29:10 +0000105 Function->getDeclContext()->isFileContext() &&
106 (!Pattern || !Pattern->getLexicalDeclContext()->isFileContext())) {
John McCall970d5302009-08-29 03:16:09 +0000107 Ctx = Function->getLexicalDeclContext();
Douglas Gregor8c702532010-02-05 07:33:43 +0000108 RelativeToPrimary = false;
John McCall970d5302009-08-29 03:16:09 +0000109 continue;
110 }
Douglas Gregor9961ce92010-07-08 18:37:38 +0000111 } else if (CXXRecordDecl *Rec = dyn_cast<CXXRecordDecl>(Ctx)) {
112 if (ClassTemplateDecl *ClassTemplate = Rec->getDescribedClassTemplate()) {
113 QualType T = ClassTemplate->getInjectedClassNameSpecialization();
114 const TemplateSpecializationType *TST
115 = cast<TemplateSpecializationType>(Context.getCanonicalType(T));
116 Result.addOuterTemplateArguments(TST->getArgs(), TST->getNumArgs());
117 if (ClassTemplate->isMemberSpecialization())
118 break;
119 }
Douglas Gregora654dd82009-08-28 17:37:35 +0000120 }
John McCall970d5302009-08-29 03:16:09 +0000121
122 Ctx = Ctx->getParent();
Douglas Gregor8c702532010-02-05 07:33:43 +0000123 RelativeToPrimary = false;
Douglas Gregorb4850462009-05-14 23:26:13 +0000124 }
Mike Stump11289f42009-09-09 15:08:12 +0000125
Douglas Gregora654dd82009-08-28 17:37:35 +0000126 return Result;
Douglas Gregorb4850462009-05-14 23:26:13 +0000127}
128
Douglas Gregor84d49a22009-11-11 21:54:23 +0000129bool Sema::ActiveTemplateInstantiation::isInstantiationRecord() const {
130 switch (Kind) {
131 case TemplateInstantiation:
132 case DefaultTemplateArgumentInstantiation:
133 case DefaultFunctionArgumentInstantiation:
134 return true;
135
136 case ExplicitTemplateArgumentSubstitution:
137 case DeducedTemplateArgumentSubstitution:
138 case PriorTemplateArgumentSubstitution:
139 case DefaultTemplateArgumentChecking:
140 return false;
141 }
142
143 return true;
144}
145
Douglas Gregorfcd5db32009-03-10 00:06:19 +0000146Sema::InstantiatingTemplate::
147InstantiatingTemplate(Sema &SemaRef, SourceLocation PointOfInstantiation,
Douglas Gregor85673582009-05-18 17:01:57 +0000148 Decl *Entity,
Douglas Gregorfcd5db32009-03-10 00:06:19 +0000149 SourceRange InstantiationRange)
Douglas Gregoredb76852011-01-27 22:31:44 +0000150 : SemaRef(SemaRef),
151 SavedInNonInstantiationSFINAEContext(
152 SemaRef.InNonInstantiationSFINAEContext)
153{
Douglas Gregor79cf6032009-03-10 20:44:00 +0000154 Invalid = CheckInstantiationDepth(PointOfInstantiation,
155 InstantiationRange);
156 if (!Invalid) {
Douglas Gregorfcd5db32009-03-10 00:06:19 +0000157 ActiveTemplateInstantiation Inst;
Douglas Gregor79cf6032009-03-10 20:44:00 +0000158 Inst.Kind = ActiveTemplateInstantiation::TemplateInstantiation;
Douglas Gregorfcd5db32009-03-10 00:06:19 +0000159 Inst.PointOfInstantiation = PointOfInstantiation;
Douglas Gregor79cf6032009-03-10 20:44:00 +0000160 Inst.Entity = reinterpret_cast<uintptr_t>(Entity);
Douglas Gregorc9220832009-03-12 18:36:18 +0000161 Inst.TemplateArgs = 0;
162 Inst.NumTemplateArgs = 0;
Douglas Gregor79cf6032009-03-10 20:44:00 +0000163 Inst.InstantiationRange = InstantiationRange;
Douglas Gregoredb76852011-01-27 22:31:44 +0000164 SemaRef.InNonInstantiationSFINAEContext = false;
Douglas Gregor79cf6032009-03-10 20:44:00 +0000165 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
Douglas Gregor79cf6032009-03-10 20:44:00 +0000166 }
167}
168
Mike Stump11289f42009-09-09 15:08:12 +0000169Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef,
Douglas Gregor79cf6032009-03-10 20:44:00 +0000170 SourceLocation PointOfInstantiation,
171 TemplateDecl *Template,
172 const TemplateArgument *TemplateArgs,
173 unsigned NumTemplateArgs,
174 SourceRange InstantiationRange)
Douglas Gregoredb76852011-01-27 22:31:44 +0000175 : SemaRef(SemaRef),
176 SavedInNonInstantiationSFINAEContext(
177 SemaRef.InNonInstantiationSFINAEContext)
178{
Douglas Gregor79cf6032009-03-10 20:44:00 +0000179 Invalid = CheckInstantiationDepth(PointOfInstantiation,
180 InstantiationRange);
181 if (!Invalid) {
182 ActiveTemplateInstantiation Inst;
Mike Stump11289f42009-09-09 15:08:12 +0000183 Inst.Kind
Douglas Gregor79cf6032009-03-10 20:44:00 +0000184 = ActiveTemplateInstantiation::DefaultTemplateArgumentInstantiation;
185 Inst.PointOfInstantiation = PointOfInstantiation;
186 Inst.Entity = reinterpret_cast<uintptr_t>(Template);
187 Inst.TemplateArgs = TemplateArgs;
188 Inst.NumTemplateArgs = NumTemplateArgs;
Douglas Gregorfcd5db32009-03-10 00:06:19 +0000189 Inst.InstantiationRange = InstantiationRange;
Douglas Gregoredb76852011-01-27 22:31:44 +0000190 SemaRef.InNonInstantiationSFINAEContext = false;
Douglas Gregorfcd5db32009-03-10 00:06:19 +0000191 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
Douglas Gregorfcd5db32009-03-10 00:06:19 +0000192 }
193}
194
Mike Stump11289f42009-09-09 15:08:12 +0000195Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef,
Douglas Gregor637d9982009-06-10 23:47:09 +0000196 SourceLocation PointOfInstantiation,
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000197 FunctionTemplateDecl *FunctionTemplate,
198 const TemplateArgument *TemplateArgs,
199 unsigned NumTemplateArgs,
200 ActiveTemplateInstantiation::InstantiationKind Kind,
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +0000201 sema::TemplateDeductionInfo &DeductionInfo,
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000202 SourceRange InstantiationRange)
Douglas Gregoredb76852011-01-27 22:31:44 +0000203 : SemaRef(SemaRef),
204 SavedInNonInstantiationSFINAEContext(
205 SemaRef.InNonInstantiationSFINAEContext)
206{
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000207 Invalid = CheckInstantiationDepth(PointOfInstantiation,
208 InstantiationRange);
209 if (!Invalid) {
210 ActiveTemplateInstantiation Inst;
211 Inst.Kind = Kind;
212 Inst.PointOfInstantiation = PointOfInstantiation;
213 Inst.Entity = reinterpret_cast<uintptr_t>(FunctionTemplate);
214 Inst.TemplateArgs = TemplateArgs;
215 Inst.NumTemplateArgs = NumTemplateArgs;
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +0000216 Inst.DeductionInfo = &DeductionInfo;
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000217 Inst.InstantiationRange = InstantiationRange;
Douglas Gregoredb76852011-01-27 22:31:44 +0000218 SemaRef.InNonInstantiationSFINAEContext = false;
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000219 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
Douglas Gregor84d49a22009-11-11 21:54:23 +0000220
221 if (!Inst.isInstantiationRecord())
222 ++SemaRef.NonInstantiationEntries;
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000223 }
224}
225
Mike Stump11289f42009-09-09 15:08:12 +0000226Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef,
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000227 SourceLocation PointOfInstantiation,
Douglas Gregor637d9982009-06-10 23:47:09 +0000228 ClassTemplatePartialSpecializationDecl *PartialSpec,
229 const TemplateArgument *TemplateArgs,
230 unsigned NumTemplateArgs,
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +0000231 sema::TemplateDeductionInfo &DeductionInfo,
Douglas Gregor637d9982009-06-10 23:47:09 +0000232 SourceRange InstantiationRange)
Douglas Gregoredb76852011-01-27 22:31:44 +0000233 : SemaRef(SemaRef),
234 SavedInNonInstantiationSFINAEContext(
235 SemaRef.InNonInstantiationSFINAEContext)
236{
Douglas Gregor84d49a22009-11-11 21:54:23 +0000237 Invalid = false;
238
239 ActiveTemplateInstantiation Inst;
240 Inst.Kind = ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution;
241 Inst.PointOfInstantiation = PointOfInstantiation;
242 Inst.Entity = reinterpret_cast<uintptr_t>(PartialSpec);
243 Inst.TemplateArgs = TemplateArgs;
244 Inst.NumTemplateArgs = NumTemplateArgs;
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +0000245 Inst.DeductionInfo = &DeductionInfo;
Douglas Gregor84d49a22009-11-11 21:54:23 +0000246 Inst.InstantiationRange = InstantiationRange;
Douglas Gregoredb76852011-01-27 22:31:44 +0000247 SemaRef.InNonInstantiationSFINAEContext = false;
Douglas Gregor84d49a22009-11-11 21:54:23 +0000248 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
249
250 assert(!Inst.isInstantiationRecord());
251 ++SemaRef.NonInstantiationEntries;
Douglas Gregor637d9982009-06-10 23:47:09 +0000252}
253
Mike Stump11289f42009-09-09 15:08:12 +0000254Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef,
Douglas Gregore62e6a02009-11-11 19:13:48 +0000255 SourceLocation PointOfInstantiation,
Anders Carlsson657bad42009-09-05 05:14:19 +0000256 ParmVarDecl *Param,
257 const TemplateArgument *TemplateArgs,
258 unsigned NumTemplateArgs,
259 SourceRange InstantiationRange)
Douglas Gregoredb76852011-01-27 22:31:44 +0000260 : SemaRef(SemaRef),
261 SavedInNonInstantiationSFINAEContext(
262 SemaRef.InNonInstantiationSFINAEContext)
263{
Douglas Gregore62e6a02009-11-11 19:13:48 +0000264 Invalid = CheckInstantiationDepth(PointOfInstantiation, InstantiationRange);
Anders Carlsson657bad42009-09-05 05:14:19 +0000265
266 if (!Invalid) {
267 ActiveTemplateInstantiation Inst;
268 Inst.Kind
269 = ActiveTemplateInstantiation::DefaultFunctionArgumentInstantiation;
Douglas Gregore62e6a02009-11-11 19:13:48 +0000270 Inst.PointOfInstantiation = PointOfInstantiation;
Anders Carlsson657bad42009-09-05 05:14:19 +0000271 Inst.Entity = reinterpret_cast<uintptr_t>(Param);
272 Inst.TemplateArgs = TemplateArgs;
273 Inst.NumTemplateArgs = NumTemplateArgs;
274 Inst.InstantiationRange = InstantiationRange;
Douglas Gregoredb76852011-01-27 22:31:44 +0000275 SemaRef.InNonInstantiationSFINAEContext = false;
Anders Carlsson657bad42009-09-05 05:14:19 +0000276 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
Douglas Gregore62e6a02009-11-11 19:13:48 +0000277 }
278}
279
280Sema::InstantiatingTemplate::
281InstantiatingTemplate(Sema &SemaRef, SourceLocation PointOfInstantiation,
Douglas Gregorca4686d2011-01-04 23:35:54 +0000282 NamedDecl *Template,
Douglas Gregore62e6a02009-11-11 19:13:48 +0000283 NonTypeTemplateParmDecl *Param,
284 const TemplateArgument *TemplateArgs,
285 unsigned NumTemplateArgs,
Douglas Gregoredb76852011-01-27 22:31:44 +0000286 SourceRange InstantiationRange)
287 : SemaRef(SemaRef),
288 SavedInNonInstantiationSFINAEContext(
289 SemaRef.InNonInstantiationSFINAEContext)
290{
Douglas Gregor84d49a22009-11-11 21:54:23 +0000291 Invalid = false;
Douglas Gregore62e6a02009-11-11 19:13:48 +0000292
Douglas Gregor84d49a22009-11-11 21:54:23 +0000293 ActiveTemplateInstantiation Inst;
294 Inst.Kind = ActiveTemplateInstantiation::PriorTemplateArgumentSubstitution;
295 Inst.PointOfInstantiation = PointOfInstantiation;
296 Inst.Template = Template;
297 Inst.Entity = reinterpret_cast<uintptr_t>(Param);
298 Inst.TemplateArgs = TemplateArgs;
299 Inst.NumTemplateArgs = NumTemplateArgs;
300 Inst.InstantiationRange = InstantiationRange;
Douglas Gregoredb76852011-01-27 22:31:44 +0000301 SemaRef.InNonInstantiationSFINAEContext = false;
Douglas Gregor84d49a22009-11-11 21:54:23 +0000302 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
303
304 assert(!Inst.isInstantiationRecord());
305 ++SemaRef.NonInstantiationEntries;
Douglas Gregore62e6a02009-11-11 19:13:48 +0000306}
307
308Sema::InstantiatingTemplate::
309InstantiatingTemplate(Sema &SemaRef, SourceLocation PointOfInstantiation,
Douglas Gregorca4686d2011-01-04 23:35:54 +0000310 NamedDecl *Template,
Douglas Gregore62e6a02009-11-11 19:13:48 +0000311 TemplateTemplateParmDecl *Param,
312 const TemplateArgument *TemplateArgs,
313 unsigned NumTemplateArgs,
Douglas Gregoredb76852011-01-27 22:31:44 +0000314 SourceRange InstantiationRange)
315 : SemaRef(SemaRef),
316 SavedInNonInstantiationSFINAEContext(
317 SemaRef.InNonInstantiationSFINAEContext)
318{
Douglas Gregor84d49a22009-11-11 21:54:23 +0000319 Invalid = false;
320 ActiveTemplateInstantiation Inst;
321 Inst.Kind = ActiveTemplateInstantiation::PriorTemplateArgumentSubstitution;
322 Inst.PointOfInstantiation = PointOfInstantiation;
323 Inst.Template = Template;
324 Inst.Entity = reinterpret_cast<uintptr_t>(Param);
325 Inst.TemplateArgs = TemplateArgs;
326 Inst.NumTemplateArgs = NumTemplateArgs;
327 Inst.InstantiationRange = InstantiationRange;
Douglas Gregoredb76852011-01-27 22:31:44 +0000328 SemaRef.InNonInstantiationSFINAEContext = false;
Douglas Gregor84d49a22009-11-11 21:54:23 +0000329 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
Douglas Gregore62e6a02009-11-11 19:13:48 +0000330
Douglas Gregor84d49a22009-11-11 21:54:23 +0000331 assert(!Inst.isInstantiationRecord());
332 ++SemaRef.NonInstantiationEntries;
333}
334
335Sema::InstantiatingTemplate::
336InstantiatingTemplate(Sema &SemaRef, SourceLocation PointOfInstantiation,
337 TemplateDecl *Template,
338 NamedDecl *Param,
339 const TemplateArgument *TemplateArgs,
340 unsigned NumTemplateArgs,
Douglas Gregoredb76852011-01-27 22:31:44 +0000341 SourceRange InstantiationRange)
342 : SemaRef(SemaRef),
343 SavedInNonInstantiationSFINAEContext(
344 SemaRef.InNonInstantiationSFINAEContext)
345{
Douglas Gregor84d49a22009-11-11 21:54:23 +0000346 Invalid = false;
347
348 ActiveTemplateInstantiation Inst;
349 Inst.Kind = ActiveTemplateInstantiation::DefaultTemplateArgumentChecking;
350 Inst.PointOfInstantiation = PointOfInstantiation;
351 Inst.Template = Template;
352 Inst.Entity = reinterpret_cast<uintptr_t>(Param);
353 Inst.TemplateArgs = TemplateArgs;
354 Inst.NumTemplateArgs = NumTemplateArgs;
355 Inst.InstantiationRange = InstantiationRange;
Douglas Gregoredb76852011-01-27 22:31:44 +0000356 SemaRef.InNonInstantiationSFINAEContext = false;
Douglas Gregor84d49a22009-11-11 21:54:23 +0000357 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
358
359 assert(!Inst.isInstantiationRecord());
360 ++SemaRef.NonInstantiationEntries;
Anders Carlsson657bad42009-09-05 05:14:19 +0000361}
362
Douglas Gregor85673582009-05-18 17:01:57 +0000363void Sema::InstantiatingTemplate::Clear() {
364 if (!Invalid) {
Douglas Gregor84d49a22009-11-11 21:54:23 +0000365 if (!SemaRef.ActiveTemplateInstantiations.back().isInstantiationRecord()) {
366 assert(SemaRef.NonInstantiationEntries > 0);
367 --SemaRef.NonInstantiationEntries;
368 }
Douglas Gregoredb76852011-01-27 22:31:44 +0000369 SemaRef.InNonInstantiationSFINAEContext
370 = SavedInNonInstantiationSFINAEContext;
Douglas Gregorfcd5db32009-03-10 00:06:19 +0000371 SemaRef.ActiveTemplateInstantiations.pop_back();
Douglas Gregor85673582009-05-18 17:01:57 +0000372 Invalid = true;
373 }
Douglas Gregorfcd5db32009-03-10 00:06:19 +0000374}
375
Douglas Gregor79cf6032009-03-10 20:44:00 +0000376bool Sema::InstantiatingTemplate::CheckInstantiationDepth(
377 SourceLocation PointOfInstantiation,
378 SourceRange InstantiationRange) {
Douglas Gregor84d49a22009-11-11 21:54:23 +0000379 assert(SemaRef.NonInstantiationEntries <=
380 SemaRef.ActiveTemplateInstantiations.size());
381 if ((SemaRef.ActiveTemplateInstantiations.size() -
382 SemaRef.NonInstantiationEntries)
383 <= SemaRef.getLangOptions().InstantiationDepth)
Douglas Gregor79cf6032009-03-10 20:44:00 +0000384 return false;
385
Mike Stump11289f42009-09-09 15:08:12 +0000386 SemaRef.Diag(PointOfInstantiation,
Douglas Gregor79cf6032009-03-10 20:44:00 +0000387 diag::err_template_recursion_depth_exceeded)
388 << SemaRef.getLangOptions().InstantiationDepth
389 << InstantiationRange;
390 SemaRef.Diag(PointOfInstantiation, diag::note_template_recursion_depth)
391 << SemaRef.getLangOptions().InstantiationDepth;
392 return true;
393}
394
Douglas Gregor4ea568f2009-03-10 18:03:33 +0000395/// \brief Prints the current instantiation stack through a series of
396/// notes.
397void Sema::PrintInstantiationStack() {
Douglas Gregorffed1cb2010-04-20 07:18:24 +0000398 // Determine which template instantiations to skip, if any.
399 unsigned SkipStart = ActiveTemplateInstantiations.size(), SkipEnd = SkipStart;
400 unsigned Limit = Diags.getTemplateBacktraceLimit();
401 if (Limit && Limit < ActiveTemplateInstantiations.size()) {
402 SkipStart = Limit / 2 + Limit % 2;
403 SkipEnd = ActiveTemplateInstantiations.size() - Limit / 2;
404 }
405
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000406 // FIXME: In all of these cases, we need to show the template arguments
Douglas Gregorffed1cb2010-04-20 07:18:24 +0000407 unsigned InstantiationIdx = 0;
Douglas Gregor4ea568f2009-03-10 18:03:33 +0000408 for (llvm::SmallVector<ActiveTemplateInstantiation, 16>::reverse_iterator
409 Active = ActiveTemplateInstantiations.rbegin(),
410 ActiveEnd = ActiveTemplateInstantiations.rend();
411 Active != ActiveEnd;
Douglas Gregorffed1cb2010-04-20 07:18:24 +0000412 ++Active, ++InstantiationIdx) {
413 // Skip this instantiation?
414 if (InstantiationIdx >= SkipStart && InstantiationIdx < SkipEnd) {
415 if (InstantiationIdx == SkipStart) {
416 // Note that we're skipping instantiations.
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000417 Diags.Report(Active->PointOfInstantiation,
Douglas Gregorffed1cb2010-04-20 07:18:24 +0000418 diag::note_instantiation_contexts_suppressed)
419 << unsigned(ActiveTemplateInstantiations.size() - Limit);
420 }
421 continue;
422 }
423
Douglas Gregor79cf6032009-03-10 20:44:00 +0000424 switch (Active->Kind) {
425 case ActiveTemplateInstantiation::TemplateInstantiation: {
Douglas Gregor85673582009-05-18 17:01:57 +0000426 Decl *D = reinterpret_cast<Decl *>(Active->Entity);
427 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) {
428 unsigned DiagID = diag::note_template_member_class_here;
429 if (isa<ClassTemplateSpecializationDecl>(Record))
430 DiagID = diag::note_template_class_instantiation_here;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000431 Diags.Report(Active->PointOfInstantiation, DiagID)
Douglas Gregor85673582009-05-18 17:01:57 +0000432 << Context.getTypeDeclType(Record)
433 << Active->InstantiationRange;
Douglas Gregora6ef8f02009-07-24 20:34:43 +0000434 } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
Douglas Gregor4adbc6d2009-06-26 00:10:03 +0000435 unsigned DiagID;
436 if (Function->getPrimaryTemplate())
437 DiagID = diag::note_function_template_spec_here;
438 else
439 DiagID = diag::note_template_member_function_here;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000440 Diags.Report(Active->PointOfInstantiation, DiagID)
Douglas Gregor85673582009-05-18 17:01:57 +0000441 << Function
442 << Active->InstantiationRange;
Douglas Gregora6ef8f02009-07-24 20:34:43 +0000443 } else {
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000444 Diags.Report(Active->PointOfInstantiation,
Douglas Gregora6ef8f02009-07-24 20:34:43 +0000445 diag::note_template_static_data_member_def_here)
446 << cast<VarDecl>(D)
447 << Active->InstantiationRange;
Douglas Gregor85673582009-05-18 17:01:57 +0000448 }
Douglas Gregor79cf6032009-03-10 20:44:00 +0000449 break;
450 }
451
452 case ActiveTemplateInstantiation::DefaultTemplateArgumentInstantiation: {
453 TemplateDecl *Template = cast<TemplateDecl>((Decl *)Active->Entity);
454 std::string TemplateArgsStr
Douglas Gregordc572a32009-03-30 22:58:21 +0000455 = TemplateSpecializationType::PrintTemplateArgumentList(
Mike Stump11289f42009-09-09 15:08:12 +0000456 Active->TemplateArgs,
Douglas Gregor7de59662009-05-29 20:38:28 +0000457 Active->NumTemplateArgs,
458 Context.PrintingPolicy);
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000459 Diags.Report(Active->PointOfInstantiation,
Douglas Gregor79cf6032009-03-10 20:44:00 +0000460 diag::note_default_arg_instantiation_here)
461 << (Template->getNameAsString() + TemplateArgsStr)
462 << Active->InstantiationRange;
463 break;
464 }
Douglas Gregor637d9982009-06-10 23:47:09 +0000465
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000466 case ActiveTemplateInstantiation::ExplicitTemplateArgumentSubstitution: {
Mike Stump11289f42009-09-09 15:08:12 +0000467 FunctionTemplateDecl *FnTmpl
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000468 = cast<FunctionTemplateDecl>((Decl *)Active->Entity);
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000469 Diags.Report(Active->PointOfInstantiation,
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000470 diag::note_explicit_template_arg_substitution_here)
Douglas Gregor607f1412010-03-30 20:35:20 +0000471 << FnTmpl
472 << getTemplateArgumentBindingsText(FnTmpl->getTemplateParameters(),
473 Active->TemplateArgs,
474 Active->NumTemplateArgs)
475 << Active->InstantiationRange;
Douglas Gregor637d9982009-06-10 23:47:09 +0000476 break;
477 }
Mike Stump11289f42009-09-09 15:08:12 +0000478
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000479 case ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution:
480 if (ClassTemplatePartialSpecializationDecl *PartialSpec
481 = dyn_cast<ClassTemplatePartialSpecializationDecl>(
482 (Decl *)Active->Entity)) {
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000483 Diags.Report(Active->PointOfInstantiation,
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000484 diag::note_partial_spec_deduct_instantiation_here)
485 << Context.getTypeDeclType(PartialSpec)
Douglas Gregor607f1412010-03-30 20:35:20 +0000486 << getTemplateArgumentBindingsText(
487 PartialSpec->getTemplateParameters(),
488 Active->TemplateArgs,
489 Active->NumTemplateArgs)
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000490 << Active->InstantiationRange;
491 } else {
492 FunctionTemplateDecl *FnTmpl
493 = cast<FunctionTemplateDecl>((Decl *)Active->Entity);
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000494 Diags.Report(Active->PointOfInstantiation,
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000495 diag::note_function_template_deduction_instantiation_here)
Douglas Gregor607f1412010-03-30 20:35:20 +0000496 << FnTmpl
497 << getTemplateArgumentBindingsText(FnTmpl->getTemplateParameters(),
498 Active->TemplateArgs,
499 Active->NumTemplateArgs)
500 << Active->InstantiationRange;
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000501 }
502 break;
Douglas Gregor637d9982009-06-10 23:47:09 +0000503
Anders Carlsson657bad42009-09-05 05:14:19 +0000504 case ActiveTemplateInstantiation::DefaultFunctionArgumentInstantiation: {
505 ParmVarDecl *Param = cast<ParmVarDecl>((Decl *)Active->Entity);
506 FunctionDecl *FD = cast<FunctionDecl>(Param->getDeclContext());
Mike Stump11289f42009-09-09 15:08:12 +0000507
Anders Carlsson657bad42009-09-05 05:14:19 +0000508 std::string TemplateArgsStr
509 = TemplateSpecializationType::PrintTemplateArgumentList(
Mike Stump11289f42009-09-09 15:08:12 +0000510 Active->TemplateArgs,
Anders Carlsson657bad42009-09-05 05:14:19 +0000511 Active->NumTemplateArgs,
512 Context.PrintingPolicy);
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000513 Diags.Report(Active->PointOfInstantiation,
Anders Carlsson657bad42009-09-05 05:14:19 +0000514 diag::note_default_function_arg_instantiation_here)
Anders Carlssondc6d2c32009-09-05 05:38:54 +0000515 << (FD->getNameAsString() + TemplateArgsStr)
Anders Carlsson657bad42009-09-05 05:14:19 +0000516 << Active->InstantiationRange;
517 break;
518 }
Mike Stump11289f42009-09-09 15:08:12 +0000519
Douglas Gregore62e6a02009-11-11 19:13:48 +0000520 case ActiveTemplateInstantiation::PriorTemplateArgumentSubstitution: {
521 NamedDecl *Parm = cast<NamedDecl>((Decl *)Active->Entity);
522 std::string Name;
523 if (!Parm->getName().empty())
524 Name = std::string(" '") + Parm->getName().str() + "'";
Douglas Gregorca4686d2011-01-04 23:35:54 +0000525
526 TemplateParameterList *TemplateParams = 0;
527 if (TemplateDecl *Template = dyn_cast<TemplateDecl>(Active->Template))
528 TemplateParams = Template->getTemplateParameters();
529 else
530 TemplateParams =
531 cast<ClassTemplatePartialSpecializationDecl>(Active->Template)
532 ->getTemplateParameters();
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000533 Diags.Report(Active->PointOfInstantiation,
Douglas Gregore62e6a02009-11-11 19:13:48 +0000534 diag::note_prior_template_arg_substitution)
535 << isa<TemplateTemplateParmDecl>(Parm)
536 << Name
Douglas Gregorca4686d2011-01-04 23:35:54 +0000537 << getTemplateArgumentBindingsText(TemplateParams,
Douglas Gregore62e6a02009-11-11 19:13:48 +0000538 Active->TemplateArgs,
539 Active->NumTemplateArgs)
540 << Active->InstantiationRange;
541 break;
542 }
Douglas Gregor84d49a22009-11-11 21:54:23 +0000543
544 case ActiveTemplateInstantiation::DefaultTemplateArgumentChecking: {
Douglas Gregorca4686d2011-01-04 23:35:54 +0000545 TemplateParameterList *TemplateParams = 0;
546 if (TemplateDecl *Template = dyn_cast<TemplateDecl>(Active->Template))
547 TemplateParams = Template->getTemplateParameters();
548 else
549 TemplateParams =
550 cast<ClassTemplatePartialSpecializationDecl>(Active->Template)
551 ->getTemplateParameters();
552
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000553 Diags.Report(Active->PointOfInstantiation,
Douglas Gregor84d49a22009-11-11 21:54:23 +0000554 diag::note_template_default_arg_checking)
Douglas Gregorca4686d2011-01-04 23:35:54 +0000555 << getTemplateArgumentBindingsText(TemplateParams,
Douglas Gregor84d49a22009-11-11 21:54:23 +0000556 Active->TemplateArgs,
557 Active->NumTemplateArgs)
558 << Active->InstantiationRange;
559 break;
560 }
Douglas Gregor79cf6032009-03-10 20:44:00 +0000561 }
Douglas Gregor4ea568f2009-03-10 18:03:33 +0000562 }
563}
564
Douglas Gregoredb76852011-01-27 22:31:44 +0000565llvm::Optional<TemplateDeductionInfo *> Sema::isSFINAEContext() const {
Douglas Gregor33834512009-06-14 07:33:30 +0000566 using llvm::SmallVector;
Douglas Gregoredb76852011-01-27 22:31:44 +0000567 if (InNonInstantiationSFINAEContext)
568 return llvm::Optional<TemplateDeductionInfo *>(0);
569
Douglas Gregor33834512009-06-14 07:33:30 +0000570 for (SmallVector<ActiveTemplateInstantiation, 16>::const_reverse_iterator
571 Active = ActiveTemplateInstantiations.rbegin(),
572 ActiveEnd = ActiveTemplateInstantiations.rend();
573 Active != ActiveEnd;
Douglas Gregor84d49a22009-11-11 21:54:23 +0000574 ++Active)
575 {
Douglas Gregor33834512009-06-14 07:33:30 +0000576 switch(Active->Kind) {
Anders Carlsson657bad42009-09-05 05:14:19 +0000577 case ActiveTemplateInstantiation::DefaultFunctionArgumentInstantiation:
Douglas Gregoredb76852011-01-27 22:31:44 +0000578 case ActiveTemplateInstantiation::TemplateInstantiation:
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000579 // This is a template instantiation, so there is no SFINAE.
Douglas Gregoredb76852011-01-27 22:31:44 +0000580 return llvm::Optional<TemplateDeductionInfo *>();
Mike Stump11289f42009-09-09 15:08:12 +0000581
Douglas Gregor33834512009-06-14 07:33:30 +0000582 case ActiveTemplateInstantiation::DefaultTemplateArgumentInstantiation:
Douglas Gregore62e6a02009-11-11 19:13:48 +0000583 case ActiveTemplateInstantiation::PriorTemplateArgumentSubstitution:
Douglas Gregor84d49a22009-11-11 21:54:23 +0000584 case ActiveTemplateInstantiation::DefaultTemplateArgumentChecking:
Douglas Gregore62e6a02009-11-11 19:13:48 +0000585 // A default template argument instantiation and substitution into
586 // template parameters with arguments for prior parameters may or may
587 // not be a SFINAE context; look further up the stack.
Douglas Gregor33834512009-06-14 07:33:30 +0000588 break;
Mike Stump11289f42009-09-09 15:08:12 +0000589
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000590 case ActiveTemplateInstantiation::ExplicitTemplateArgumentSubstitution:
591 case ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution:
592 // We're either substitution explicitly-specified template arguments
593 // or deduced template arguments, so SFINAE applies.
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +0000594 assert(Active->DeductionInfo && "Missing deduction info pointer");
595 return Active->DeductionInfo;
Douglas Gregor33834512009-06-14 07:33:30 +0000596 }
597 }
598
Douglas Gregoredb76852011-01-27 22:31:44 +0000599 return llvm::Optional<TemplateDeductionInfo *>();
Douglas Gregor33834512009-06-14 07:33:30 +0000600}
601
Douglas Gregora8bac7f2011-01-10 07:32:04 +0000602/// \brief Retrieve the depth and index of a parameter pack.
603static std::pair<unsigned, unsigned>
604getDepthAndIndex(NamedDecl *ND) {
605 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(ND))
606 return std::make_pair(TTP->getDepth(), TTP->getIndex());
607
608 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(ND))
609 return std::make_pair(NTTP->getDepth(), NTTP->getIndex());
610
611 TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(ND);
612 return std::make_pair(TTP->getDepth(), TTP->getIndex());
613}
614
Douglas Gregorfe1e1102009-02-27 19:31:52 +0000615//===----------------------------------------------------------------------===/
616// Template Instantiation for Types
617//===----------------------------------------------------------------------===/
Douglas Gregor17c0d7b2009-02-28 00:25:32 +0000618namespace {
Douglas Gregor14cf7522010-04-30 18:55:50 +0000619 class TemplateInstantiator : public TreeTransform<TemplateInstantiator> {
Douglas Gregor01afeef2009-08-28 20:31:08 +0000620 const MultiLevelTemplateArgumentList &TemplateArgs;
Douglas Gregor17c0d7b2009-02-28 00:25:32 +0000621 SourceLocation Loc;
622 DeclarationName Entity;
Douglas Gregorfe1e1102009-02-27 19:31:52 +0000623
Douglas Gregor17c0d7b2009-02-28 00:25:32 +0000624 public:
Douglas Gregorebe10102009-08-20 07:17:43 +0000625 typedef TreeTransform<TemplateInstantiator> inherited;
Mike Stump11289f42009-09-09 15:08:12 +0000626
627 TemplateInstantiator(Sema &SemaRef,
Douglas Gregor01afeef2009-08-28 20:31:08 +0000628 const MultiLevelTemplateArgumentList &TemplateArgs,
Douglas Gregord6ff3322009-08-04 16:50:30 +0000629 SourceLocation Loc,
Mike Stump11289f42009-09-09 15:08:12 +0000630 DeclarationName Entity)
631 : inherited(SemaRef), TemplateArgs(TemplateArgs), Loc(Loc),
Douglas Gregorebe10102009-08-20 07:17:43 +0000632 Entity(Entity) { }
Douglas Gregor17c0d7b2009-02-28 00:25:32 +0000633
Mike Stump11289f42009-09-09 15:08:12 +0000634 /// \brief Determine whether the given type \p T has already been
Douglas Gregord6ff3322009-08-04 16:50:30 +0000635 /// transformed.
636 ///
637 /// For the purposes of template instantiation, a type has already been
638 /// transformed if it is NULL or if it is not dependent.
Douglas Gregor5597ab42010-05-07 23:12:07 +0000639 bool AlreadyTransformed(QualType T);
Mike Stump11289f42009-09-09 15:08:12 +0000640
Douglas Gregord6ff3322009-08-04 16:50:30 +0000641 /// \brief Returns the location of the entity being instantiated, if known.
642 SourceLocation getBaseLocation() { return Loc; }
Mike Stump11289f42009-09-09 15:08:12 +0000643
Douglas Gregord6ff3322009-08-04 16:50:30 +0000644 /// \brief Returns the name of the entity being instantiated, if any.
645 DeclarationName getBaseEntity() { return Entity; }
Mike Stump11289f42009-09-09 15:08:12 +0000646
Douglas Gregoref6ab412009-10-27 06:26:26 +0000647 /// \brief Sets the "base" location and entity when that
648 /// information is known based on another transformation.
649 void setBase(SourceLocation Loc, DeclarationName Entity) {
650 this->Loc = Loc;
651 this->Entity = Entity;
652 }
Douglas Gregor840bd6c2010-12-20 22:05:00 +0000653
654 bool TryExpandParameterPacks(SourceLocation EllipsisLoc,
655 SourceRange PatternRange,
656 const UnexpandedParameterPack *Unexpanded,
657 unsigned NumUnexpanded,
658 bool &ShouldExpand,
Douglas Gregora8bac7f2011-01-10 07:32:04 +0000659 bool &RetainExpansion,
Douglas Gregor0dca5fd2011-01-14 17:04:44 +0000660 llvm::Optional<unsigned> &NumExpansions) {
Douglas Gregor76aca7b2010-12-21 00:52:54 +0000661 return getSema().CheckParameterPacksForExpansion(EllipsisLoc,
662 PatternRange, Unexpanded,
663 NumUnexpanded,
664 TemplateArgs,
665 ShouldExpand,
Douglas Gregora8bac7f2011-01-10 07:32:04 +0000666 RetainExpansion,
Douglas Gregor76aca7b2010-12-21 00:52:54 +0000667 NumExpansions);
668 }
Douglas Gregor840bd6c2010-12-20 22:05:00 +0000669
Douglas Gregorf3010112011-01-07 16:43:16 +0000670 void ExpandingFunctionParameterPack(ParmVarDecl *Pack) {
671 SemaRef.CurrentInstantiationScope->MakeInstantiatedLocalArgPack(Pack);
672 }
673
Douglas Gregora8bac7f2011-01-10 07:32:04 +0000674 TemplateArgument ForgetPartiallySubstitutedPack() {
675 TemplateArgument Result;
676 if (NamedDecl *PartialPack
677 = SemaRef.CurrentInstantiationScope->getPartiallySubstitutedPack()){
678 MultiLevelTemplateArgumentList &TemplateArgs
679 = const_cast<MultiLevelTemplateArgumentList &>(this->TemplateArgs);
680 unsigned Depth, Index;
681 llvm::tie(Depth, Index) = getDepthAndIndex(PartialPack);
682 if (TemplateArgs.hasTemplateArgument(Depth, Index)) {
683 Result = TemplateArgs(Depth, Index);
684 TemplateArgs.setArgument(Depth, Index, TemplateArgument());
685 }
686 }
687
688 return Result;
689 }
690
691 void RememberPartiallySubstitutedPack(TemplateArgument Arg) {
692 if (Arg.isNull())
693 return;
694
695 if (NamedDecl *PartialPack
696 = SemaRef.CurrentInstantiationScope->getPartiallySubstitutedPack()){
697 MultiLevelTemplateArgumentList &TemplateArgs
698 = const_cast<MultiLevelTemplateArgumentList &>(this->TemplateArgs);
699 unsigned Depth, Index;
700 llvm::tie(Depth, Index) = getDepthAndIndex(PartialPack);
701 TemplateArgs.setArgument(Depth, Index, Arg);
702 }
703 }
704
Douglas Gregord6ff3322009-08-04 16:50:30 +0000705 /// \brief Transform the given declaration by instantiating a reference to
706 /// this declaration.
Douglas Gregora04f2ca2010-03-01 15:56:25 +0000707 Decl *TransformDecl(SourceLocation Loc, Decl *D);
Douglas Gregora16548e2009-08-11 05:31:07 +0000708
Mike Stump11289f42009-09-09 15:08:12 +0000709 /// \brief Transform the definition of the given declaration by
Douglas Gregorebe10102009-08-20 07:17:43 +0000710 /// instantiating it.
Douglas Gregor25289362010-03-01 17:25:41 +0000711 Decl *TransformDefinition(SourceLocation Loc, Decl *D);
Mike Stump11289f42009-09-09 15:08:12 +0000712
Douglas Gregora5cb6da2009-10-20 05:58:46 +0000713 /// \bried Transform the first qualifier within a scope by instantiating the
714 /// declaration.
715 NamedDecl *TransformFirstQualifierInScope(NamedDecl *D, SourceLocation Loc);
716
Douglas Gregorebe10102009-08-20 07:17:43 +0000717 /// \brief Rebuild the exception declaration and register the declaration
718 /// as an instantiated local.
Douglas Gregor9f0e1aa2010-09-09 17:09:21 +0000719 VarDecl *RebuildExceptionDecl(VarDecl *ExceptionDecl,
John McCallbcd03502009-12-07 02:54:59 +0000720 TypeSourceInfo *Declarator,
Douglas Gregorebe10102009-08-20 07:17:43 +0000721 IdentifierInfo *Name,
Douglas Gregor9f0e1aa2010-09-09 17:09:21 +0000722 SourceLocation Loc);
Mike Stump11289f42009-09-09 15:08:12 +0000723
Douglas Gregorf4e837f2010-04-26 17:57:08 +0000724 /// \brief Rebuild the Objective-C exception declaration and register the
725 /// declaration as an instantiated local.
726 VarDecl *RebuildObjCExceptionDecl(VarDecl *ExceptionDecl,
727 TypeSourceInfo *TSInfo, QualType T);
728
John McCall7f41d982009-09-11 04:59:25 +0000729 /// \brief Check for tag mismatches when instantiating an
730 /// elaborated type.
John McCall954b5de2010-11-04 19:04:38 +0000731 QualType RebuildElaboratedType(SourceLocation KeywordLoc,
732 ElaboratedTypeKeyword Keyword,
Douglas Gregor844cb502011-03-01 18:12:44 +0000733 NestedNameSpecifierLoc QualifierLoc,
734 QualType T);
John McCall7f41d982009-09-11 04:59:25 +0000735
Douglas Gregor9db53502011-03-02 18:07:45 +0000736 TemplateName TransformTemplateName(CXXScopeSpec &SS,
737 TemplateName Name,
738 SourceLocation NameLoc,
739 QualType ObjectType = QualType(),
740 NamedDecl *FirstQualifierInScope = 0);
741
John McCalldadc5752010-08-24 06:29:42 +0000742 ExprResult TransformPredefinedExpr(PredefinedExpr *E);
743 ExprResult TransformDeclRefExpr(DeclRefExpr *E);
744 ExprResult TransformCXXDefaultArgExpr(CXXDefaultArgExpr *E);
745 ExprResult TransformTemplateParmRefExpr(DeclRefExpr *E,
Douglas Gregoreb5a39d2010-12-24 00:15:10 +0000746 NonTypeTemplateParmDecl *D);
Douglas Gregorcdbc5392011-01-15 01:15:58 +0000747 ExprResult TransformSubstNonTypeTemplateParmPackExpr(
748 SubstNonTypeTemplateParmPackExpr *E);
749
Douglas Gregor14cf7522010-04-30 18:55:50 +0000750 QualType TransformFunctionProtoType(TypeLocBuilder &TLB,
John McCall31f82722010-11-12 08:19:04 +0000751 FunctionProtoTypeLoc TL);
Douglas Gregor715e4612011-01-14 22:40:04 +0000752 ParmVarDecl *TransformFunctionTypeParam(ParmVarDecl *OldParm,
753 llvm::Optional<unsigned> NumExpansions);
John McCall58f10c32010-03-11 09:03:00 +0000754
Mike Stump11289f42009-09-09 15:08:12 +0000755 /// \brief Transforms a template type parameter type by performing
Douglas Gregord6ff3322009-08-04 16:50:30 +0000756 /// substitution of the corresponding template type argument.
John McCall550e0c22009-10-21 00:40:46 +0000757 QualType TransformTemplateTypeParmType(TypeLocBuilder &TLB,
John McCall31f82722010-11-12 08:19:04 +0000758 TemplateTypeParmTypeLoc TL);
Nick Lewyckyc96c37f2010-07-06 19:51:49 +0000759
Douglas Gregorada4b792011-01-14 02:55:32 +0000760 /// \brief Transforms an already-substituted template type parameter pack
761 /// into either itself (if we aren't substituting into its pack expansion)
762 /// or the appropriate substituted argument.
763 QualType TransformSubstTemplateTypeParmPackType(TypeLocBuilder &TLB,
764 SubstTemplateTypeParmPackTypeLoc TL);
765
John McCalldadc5752010-08-24 06:29:42 +0000766 ExprResult TransformCallExpr(CallExpr *CE) {
Nick Lewyckyc96c37f2010-07-06 19:51:49 +0000767 getSema().CallsUndergoingInstantiation.push_back(CE);
John McCalldadc5752010-08-24 06:29:42 +0000768 ExprResult Result =
Nick Lewyckyc96c37f2010-07-06 19:51:49 +0000769 TreeTransform<TemplateInstantiator>::TransformCallExpr(CE);
770 getSema().CallsUndergoingInstantiation.pop_back();
771 return move(Result);
772 }
Douglas Gregord6ff3322009-08-04 16:50:30 +0000773 };
Douglas Gregor04318252009-07-06 15:59:29 +0000774}
775
Douglas Gregor5597ab42010-05-07 23:12:07 +0000776bool TemplateInstantiator::AlreadyTransformed(QualType T) {
777 if (T.isNull())
778 return true;
779
Douglas Gregor5a5073e2010-05-24 17:22:01 +0000780 if (T->isDependentType() || T->isVariablyModifiedType())
Douglas Gregor5597ab42010-05-07 23:12:07 +0000781 return false;
782
783 getSema().MarkDeclarationsReferencedInType(Loc, T);
784 return true;
785}
786
Douglas Gregora04f2ca2010-03-01 15:56:25 +0000787Decl *TemplateInstantiator::TransformDecl(SourceLocation Loc, Decl *D) {
Douglas Gregor2b6ca462009-09-03 21:38:09 +0000788 if (!D)
789 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000790
Douglas Gregor2b6ca462009-09-03 21:38:09 +0000791 if (TemplateTemplateParmDecl *TTP = dyn_cast<TemplateTemplateParmDecl>(D)) {
Douglas Gregor01afeef2009-08-28 20:31:08 +0000792 if (TTP->getDepth() < TemplateArgs.getNumLevels()) {
Douglas Gregorb93971082010-02-05 19:54:12 +0000793 // If the corresponding template argument is NULL or non-existent, it's
794 // because we are performing instantiation from explicitly-specified
795 // template arguments in a function template, but there were some
796 // arguments left unspecified.
797 if (!TemplateArgs.hasTemplateArgument(TTP->getDepth(),
798 TTP->getPosition()))
799 return D;
800
Douglas Gregorf5500772011-01-05 15:48:55 +0000801 TemplateArgument Arg = TemplateArgs(TTP->getDepth(), TTP->getPosition());
802
803 if (TTP->isParameterPack()) {
804 assert(Arg.getKind() == TemplateArgument::Pack &&
805 "Missing argument pack");
806
Douglas Gregor5590be02011-01-15 06:45:20 +0000807 assert(getSema().ArgumentPackSubstitutionIndex >= 0);
Douglas Gregora8bac7f2011-01-10 07:32:04 +0000808 assert(getSema().ArgumentPackSubstitutionIndex < (int)Arg.pack_size());
Douglas Gregorf5500772011-01-05 15:48:55 +0000809 Arg = Arg.pack_begin()[getSema().ArgumentPackSubstitutionIndex];
810 }
811
812 TemplateName Template = Arg.getAsTemplate();
Douglas Gregor9167f8b2009-11-11 01:00:40 +0000813 assert(!Template.isNull() && Template.getAsTemplateDecl() &&
Douglas Gregor01afeef2009-08-28 20:31:08 +0000814 "Wrong kind of template template argument");
Douglas Gregor9167f8b2009-11-11 01:00:40 +0000815 return Template.getAsTemplateDecl();
Douglas Gregor01afeef2009-08-28 20:31:08 +0000816 }
Mike Stump11289f42009-09-09 15:08:12 +0000817
Douglas Gregor9167f8b2009-11-11 01:00:40 +0000818 // Fall through to find the instantiated declaration for this template
819 // template parameter.
Douglas Gregor71dc5092009-08-06 06:41:21 +0000820 }
Mike Stump11289f42009-09-09 15:08:12 +0000821
Douglas Gregora04f2ca2010-03-01 15:56:25 +0000822 return SemaRef.FindInstantiatedDecl(Loc, cast<NamedDecl>(D), TemplateArgs);
Douglas Gregord6ff3322009-08-04 16:50:30 +0000823}
824
Douglas Gregor25289362010-03-01 17:25:41 +0000825Decl *TemplateInstantiator::TransformDefinition(SourceLocation Loc, Decl *D) {
John McCall76d824f2009-08-25 22:02:44 +0000826 Decl *Inst = getSema().SubstDecl(D, getSema().CurContext, TemplateArgs);
Douglas Gregorebe10102009-08-20 07:17:43 +0000827 if (!Inst)
828 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000829
Douglas Gregorebe10102009-08-20 07:17:43 +0000830 getSema().CurrentInstantiationScope->InstantiatedLocal(D, Inst);
831 return Inst;
832}
833
Douglas Gregora5cb6da2009-10-20 05:58:46 +0000834NamedDecl *
835TemplateInstantiator::TransformFirstQualifierInScope(NamedDecl *D,
836 SourceLocation Loc) {
837 // If the first part of the nested-name-specifier was a template type
838 // parameter, instantiate that type parameter down to a tag type.
839 if (TemplateTypeParmDecl *TTPD = dyn_cast_or_null<TemplateTypeParmDecl>(D)) {
840 const TemplateTypeParmType *TTP
841 = cast<TemplateTypeParmType>(getSema().Context.getTypeDeclType(TTPD));
Douglas Gregor53c3f4e2010-12-20 22:48:17 +0000842
Douglas Gregora5cb6da2009-10-20 05:58:46 +0000843 if (TTP->getDepth() < TemplateArgs.getNumLevels()) {
Douglas Gregor53c3f4e2010-12-20 22:48:17 +0000844 // FIXME: This needs testing w/ member access expressions.
845 TemplateArgument Arg = TemplateArgs(TTP->getDepth(), TTP->getIndex());
846
847 if (TTP->isParameterPack()) {
848 assert(Arg.getKind() == TemplateArgument::Pack &&
849 "Missing argument pack");
850
Douglas Gregore1d60df2011-01-14 23:41:42 +0000851 if (getSema().ArgumentPackSubstitutionIndex == -1)
Douglas Gregor53c3f4e2010-12-20 22:48:17 +0000852 return 0;
Douglas Gregor53c3f4e2010-12-20 22:48:17 +0000853
Douglas Gregora8bac7f2011-01-10 07:32:04 +0000854 assert(getSema().ArgumentPackSubstitutionIndex < (int)Arg.pack_size());
Douglas Gregor53c3f4e2010-12-20 22:48:17 +0000855 Arg = Arg.pack_begin()[getSema().ArgumentPackSubstitutionIndex];
856 }
857
858 QualType T = Arg.getAsType();
Douglas Gregora5cb6da2009-10-20 05:58:46 +0000859 if (T.isNull())
Douglas Gregora04f2ca2010-03-01 15:56:25 +0000860 return cast_or_null<NamedDecl>(TransformDecl(Loc, D));
Douglas Gregora5cb6da2009-10-20 05:58:46 +0000861
862 if (const TagType *Tag = T->getAs<TagType>())
863 return Tag->getDecl();
864
865 // The resulting type is not a tag; complain.
866 getSema().Diag(Loc, diag::err_nested_name_spec_non_tag) << T;
867 return 0;
868 }
869 }
870
Douglas Gregora04f2ca2010-03-01 15:56:25 +0000871 return cast_or_null<NamedDecl>(TransformDecl(Loc, D));
Douglas Gregora5cb6da2009-10-20 05:58:46 +0000872}
873
Douglas Gregorebe10102009-08-20 07:17:43 +0000874VarDecl *
875TemplateInstantiator::RebuildExceptionDecl(VarDecl *ExceptionDecl,
John McCallbcd03502009-12-07 02:54:59 +0000876 TypeSourceInfo *Declarator,
Douglas Gregorebe10102009-08-20 07:17:43 +0000877 IdentifierInfo *Name,
Douglas Gregor9f0e1aa2010-09-09 17:09:21 +0000878 SourceLocation Loc) {
879 VarDecl *Var = inherited::RebuildExceptionDecl(ExceptionDecl, Declarator,
880 Name, Loc);
Douglas Gregorf4e837f2010-04-26 17:57:08 +0000881 if (Var)
882 getSema().CurrentInstantiationScope->InstantiatedLocal(ExceptionDecl, Var);
883 return Var;
884}
885
886VarDecl *TemplateInstantiator::RebuildObjCExceptionDecl(VarDecl *ExceptionDecl,
887 TypeSourceInfo *TSInfo,
888 QualType T) {
889 VarDecl *Var = inherited::RebuildObjCExceptionDecl(ExceptionDecl, TSInfo, T);
890 if (Var)
Douglas Gregorebe10102009-08-20 07:17:43 +0000891 getSema().CurrentInstantiationScope->InstantiatedLocal(ExceptionDecl, Var);
892 return Var;
893}
894
John McCall7f41d982009-09-11 04:59:25 +0000895QualType
John McCall954b5de2010-11-04 19:04:38 +0000896TemplateInstantiator::RebuildElaboratedType(SourceLocation KeywordLoc,
897 ElaboratedTypeKeyword Keyword,
Douglas Gregor844cb502011-03-01 18:12:44 +0000898 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara6150c882010-05-11 21:36:43 +0000899 QualType T) {
John McCall7f41d982009-09-11 04:59:25 +0000900 if (const TagType *TT = T->getAs<TagType>()) {
901 TagDecl* TD = TT->getDecl();
902
John McCall954b5de2010-11-04 19:04:38 +0000903 SourceLocation TagLocation = KeywordLoc;
John McCall7f41d982009-09-11 04:59:25 +0000904
905 // FIXME: type might be anonymous.
906 IdentifierInfo *Id = TD->getIdentifier();
907
908 // TODO: should we even warn on struct/class mismatches for this? Seems
909 // like it's likely to produce a lot of spurious errors.
Abramo Bagnara6150c882010-05-11 21:36:43 +0000910 if (Keyword != ETK_None && Keyword != ETK_Typename) {
911 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForKeyword(Keyword);
912 if (!SemaRef.isAcceptableTagRedeclaration(TD, Kind, TagLocation, *Id)) {
913 SemaRef.Diag(TagLocation, diag::err_use_with_wrong_tag)
914 << Id
915 << FixItHint::CreateReplacement(SourceRange(TagLocation),
916 TD->getKindName());
917 SemaRef.Diag(TD->getLocation(), diag::note_previous_use);
918 }
John McCall7f41d982009-09-11 04:59:25 +0000919 }
920 }
921
John McCall954b5de2010-11-04 19:04:38 +0000922 return TreeTransform<TemplateInstantiator>::RebuildElaboratedType(KeywordLoc,
923 Keyword,
Douglas Gregor844cb502011-03-01 18:12:44 +0000924 QualifierLoc,
925 T);
John McCall7f41d982009-09-11 04:59:25 +0000926}
927
Douglas Gregor9db53502011-03-02 18:07:45 +0000928TemplateName TemplateInstantiator::TransformTemplateName(CXXScopeSpec &SS,
929 TemplateName Name,
930 SourceLocation NameLoc,
931 QualType ObjectType,
932 NamedDecl *FirstQualifierInScope) {
933 if (TemplateTemplateParmDecl *TTP
934 = dyn_cast_or_null<TemplateTemplateParmDecl>(Name.getAsTemplateDecl())) {
935 if (TTP->getDepth() < TemplateArgs.getNumLevels()) {
936 // If the corresponding template argument is NULL or non-existent, it's
937 // because we are performing instantiation from explicitly-specified
938 // template arguments in a function template, but there were some
939 // arguments left unspecified.
940 if (!TemplateArgs.hasTemplateArgument(TTP->getDepth(),
941 TTP->getPosition()))
942 return Name;
943
944 TemplateArgument Arg = TemplateArgs(TTP->getDepth(), TTP->getPosition());
945
946 if (TTP->isParameterPack()) {
947 assert(Arg.getKind() == TemplateArgument::Pack &&
948 "Missing argument pack");
949
950 if (getSema().ArgumentPackSubstitutionIndex == -1) {
951 // We have the template argument pack to substitute, but we're not
952 // actually expanding the enclosing pack expansion yet. So, just
953 // keep the entire argument pack.
954 return getSema().Context.getSubstTemplateTemplateParmPack(TTP, Arg);
955 }
956
957 assert(getSema().ArgumentPackSubstitutionIndex < (int)Arg.pack_size());
958 Arg = Arg.pack_begin()[getSema().ArgumentPackSubstitutionIndex];
959 }
960
961 TemplateName Template = Arg.getAsTemplate();
962 assert(!Template.isNull() && Template.getAsTemplateDecl() &&
963 "Wrong kind of template template argument");
964 return Template;
965 }
966 }
967
968 if (SubstTemplateTemplateParmPackStorage *SubstPack
969 = Name.getAsSubstTemplateTemplateParmPack()) {
970 if (getSema().ArgumentPackSubstitutionIndex == -1)
971 return Name;
972
973 const TemplateArgument &ArgPack = SubstPack->getArgumentPack();
974 assert(getSema().ArgumentPackSubstitutionIndex < (int)ArgPack.pack_size() &&
975 "Pack substitution index out-of-range");
976 return ArgPack.pack_begin()[getSema().ArgumentPackSubstitutionIndex]
977 .getAsTemplate();
978 }
979
980 return inherited::TransformTemplateName(SS, Name, NameLoc, ObjectType,
981 FirstQualifierInScope);
982}
983
John McCalldadc5752010-08-24 06:29:42 +0000984ExprResult
John McCall47f29ea2009-12-08 09:21:05 +0000985TemplateInstantiator::TransformPredefinedExpr(PredefinedExpr *E) {
Anders Carlsson0b209a82009-09-11 01:22:35 +0000986 if (!E->isTypeDependent())
John McCallc3007a22010-10-26 07:05:15 +0000987 return SemaRef.Owned(E);
Anders Carlsson0b209a82009-09-11 01:22:35 +0000988
989 FunctionDecl *currentDecl = getSema().getCurFunctionDecl();
990 assert(currentDecl && "Must have current function declaration when "
991 "instantiating.");
992
993 PredefinedExpr::IdentType IT = E->getIdentType();
994
Anders Carlsson5bd8d192010-02-11 18:20:28 +0000995 unsigned Length = PredefinedExpr::ComputeName(IT, currentDecl).length();
Anders Carlsson0b209a82009-09-11 01:22:35 +0000996
997 llvm::APInt LengthI(32, Length + 1);
John McCall8ccfcb52009-09-24 19:53:00 +0000998 QualType ResTy = getSema().Context.CharTy.withConst();
Anders Carlsson0b209a82009-09-11 01:22:35 +0000999 ResTy = getSema().Context.getConstantArrayType(ResTy, LengthI,
1000 ArrayType::Normal, 0);
1001 PredefinedExpr *PE =
1002 new (getSema().Context) PredefinedExpr(E->getLocation(), ResTy, IT);
1003 return getSema().Owned(PE);
1004}
1005
John McCalldadc5752010-08-24 06:29:42 +00001006ExprResult
John McCall13481c52010-02-06 08:42:39 +00001007TemplateInstantiator::TransformTemplateParmRefExpr(DeclRefExpr *E,
Douglas Gregor6c379e22010-02-08 23:41:45 +00001008 NonTypeTemplateParmDecl *NTTP) {
John McCall13481c52010-02-06 08:42:39 +00001009 // If the corresponding template argument is NULL or non-existent, it's
1010 // because we are performing instantiation from explicitly-specified
1011 // template arguments in a function template, but there were some
1012 // arguments left unspecified.
1013 if (!TemplateArgs.hasTemplateArgument(NTTP->getDepth(),
1014 NTTP->getPosition()))
John McCallc3007a22010-10-26 07:05:15 +00001015 return SemaRef.Owned(E);
Mike Stump11289f42009-09-09 15:08:12 +00001016
Douglas Gregoreb5a39d2010-12-24 00:15:10 +00001017 TemplateArgument Arg = TemplateArgs(NTTP->getDepth(), NTTP->getPosition());
1018 if (NTTP->isParameterPack()) {
1019 assert(Arg.getKind() == TemplateArgument::Pack &&
1020 "Missing argument pack");
1021
1022 if (getSema().ArgumentPackSubstitutionIndex == -1) {
Douglas Gregorcdbc5392011-01-15 01:15:58 +00001023 // We have an argument pack, but we can't select a particular argument
1024 // out of it yet. Therefore, we'll build an expression to hold on to that
1025 // argument pack.
1026 QualType TargetType = SemaRef.SubstType(NTTP->getType(), TemplateArgs,
1027 E->getLocation(),
1028 NTTP->getDeclName());
1029 if (TargetType.isNull())
1030 return ExprError();
1031
1032 return new (SemaRef.Context) SubstNonTypeTemplateParmPackExpr(TargetType,
1033 NTTP,
1034 E->getLocation(),
1035 Arg);
Douglas Gregoreb5a39d2010-12-24 00:15:10 +00001036 }
1037
Douglas Gregora8bac7f2011-01-10 07:32:04 +00001038 assert(getSema().ArgumentPackSubstitutionIndex < (int)Arg.pack_size());
Douglas Gregoreb5a39d2010-12-24 00:15:10 +00001039 Arg = Arg.pack_begin()[getSema().ArgumentPackSubstitutionIndex];
1040 }
Mike Stump11289f42009-09-09 15:08:12 +00001041
John McCall13481c52010-02-06 08:42:39 +00001042 // The template argument itself might be an expression, in which
1043 // case we just return that expression.
1044 if (Arg.getKind() == TemplateArgument::Expression)
John McCallc3007a22010-10-26 07:05:15 +00001045 return SemaRef.Owned(Arg.getAsExpr());
Mike Stump11289f42009-09-09 15:08:12 +00001046
John McCall13481c52010-02-06 08:42:39 +00001047 if (Arg.getKind() == TemplateArgument::Declaration) {
1048 ValueDecl *VD = cast<ValueDecl>(Arg.getAsDecl());
Mike Stump11289f42009-09-09 15:08:12 +00001049
John McCall15dda372010-02-06 10:23:53 +00001050 // Find the instantiation of the template argument. This is
1051 // required for nested templates.
John McCall13481c52010-02-06 08:42:39 +00001052 VD = cast_or_null<ValueDecl>(
Douglas Gregora04f2ca2010-03-01 15:56:25 +00001053 getSema().FindInstantiatedDecl(E->getLocation(),
1054 VD, TemplateArgs));
John McCall13481c52010-02-06 08:42:39 +00001055 if (!VD)
John McCallfaf5fb42010-08-26 23:41:50 +00001056 return ExprError();
Mike Stump11289f42009-09-09 15:08:12 +00001057
John McCall15dda372010-02-06 10:23:53 +00001058 // Derive the type we want the substituted decl to have. This had
1059 // better be non-dependent, or these checks will have serious problems.
Douglas Gregor0231d8d2011-01-19 20:10:05 +00001060 QualType TargetType;
1061 if (NTTP->isExpandedParameterPack())
1062 TargetType = NTTP->getExpansionType(
1063 getSema().ArgumentPackSubstitutionIndex);
1064 else if (NTTP->isParameterPack() &&
1065 isa<PackExpansionType>(NTTP->getType())) {
1066 TargetType = SemaRef.SubstType(
1067 cast<PackExpansionType>(NTTP->getType())->getPattern(),
1068 TemplateArgs, E->getLocation(),
1069 NTTP->getDeclName());
1070 } else
1071 TargetType = SemaRef.SubstType(NTTP->getType(), TemplateArgs,
1072 E->getLocation(), NTTP->getDeclName());
John McCall15dda372010-02-06 10:23:53 +00001073 assert(!TargetType.isNull() && "type substitution failed for param type");
1074 assert(!TargetType->isDependentType() && "param type still dependent");
Douglas Gregord5cb1dd2010-03-28 02:42:43 +00001075 return SemaRef.BuildExpressionFromDeclTemplateArgument(Arg,
1076 TargetType,
1077 E->getLocation());
John McCall13481c52010-02-06 08:42:39 +00001078 }
1079
Douglas Gregord5cb1dd2010-03-28 02:42:43 +00001080 return SemaRef.BuildExpressionFromIntegralTemplateArgument(Arg,
1081 E->getSourceRange().getBegin());
John McCall13481c52010-02-06 08:42:39 +00001082}
1083
Douglas Gregorcdbc5392011-01-15 01:15:58 +00001084ExprResult
1085TemplateInstantiator::TransformSubstNonTypeTemplateParmPackExpr(
1086 SubstNonTypeTemplateParmPackExpr *E) {
1087 if (getSema().ArgumentPackSubstitutionIndex == -1) {
1088 // We aren't expanding the parameter pack, so just return ourselves.
1089 return getSema().Owned(E);
1090 }
1091
Douglas Gregorcdbc5392011-01-15 01:15:58 +00001092 const TemplateArgument &ArgPack = E->getArgumentPack();
1093 unsigned Index = (unsigned)getSema().ArgumentPackSubstitutionIndex;
1094 assert(Index < ArgPack.pack_size() && "Substitution index out-of-range");
1095
1096 const TemplateArgument &Arg = ArgPack.pack_begin()[Index];
1097 if (Arg.getKind() == TemplateArgument::Expression)
1098 return SemaRef.Owned(Arg.getAsExpr());
1099
1100 if (Arg.getKind() == TemplateArgument::Declaration) {
1101 ValueDecl *VD = cast<ValueDecl>(Arg.getAsDecl());
1102
1103 // Find the instantiation of the template argument. This is
1104 // required for nested templates.
1105 VD = cast_or_null<ValueDecl>(
1106 getSema().FindInstantiatedDecl(E->getParameterPackLocation(),
1107 VD, TemplateArgs));
1108 if (!VD)
1109 return ExprError();
1110
Douglas Gregor0231d8d2011-01-19 20:10:05 +00001111 QualType T;
1112 NonTypeTemplateParmDecl *NTTP = E->getParameterPack();
1113 if (NTTP->isExpandedParameterPack())
1114 T = NTTP->getExpansionType(getSema().ArgumentPackSubstitutionIndex);
1115 else if (const PackExpansionType *Expansion
1116 = dyn_cast<PackExpansionType>(NTTP->getType()))
1117 T = SemaRef.SubstType(Expansion->getPattern(), TemplateArgs,
1118 E->getParameterPackLocation(), NTTP->getDeclName());
1119 else
1120 T = E->getType();
1121 return SemaRef.BuildExpressionFromDeclTemplateArgument(Arg, T,
Douglas Gregorcdbc5392011-01-15 01:15:58 +00001122 E->getParameterPackLocation());
1123 }
1124
1125 return SemaRef.BuildExpressionFromIntegralTemplateArgument(Arg,
1126 E->getParameterPackLocation());
1127}
John McCall13481c52010-02-06 08:42:39 +00001128
John McCalldadc5752010-08-24 06:29:42 +00001129ExprResult
John McCall13481c52010-02-06 08:42:39 +00001130TemplateInstantiator::TransformDeclRefExpr(DeclRefExpr *E) {
1131 NamedDecl *D = E->getDecl();
1132 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(D)) {
1133 if (NTTP->getDepth() < TemplateArgs.getNumLevels())
1134 return TransformTemplateParmRefExpr(E, NTTP);
Douglas Gregor954de172009-10-31 17:21:17 +00001135
1136 // We have a non-type template parameter that isn't fully substituted;
1137 // FindInstantiatedDecl will find it in the local instantiation scope.
Douglas Gregora16548e2009-08-11 05:31:07 +00001138 }
Mike Stump11289f42009-09-09 15:08:12 +00001139
John McCall47f29ea2009-12-08 09:21:05 +00001140 return TreeTransform<TemplateInstantiator>::TransformDeclRefExpr(E);
Douglas Gregora16548e2009-08-11 05:31:07 +00001141}
1142
John McCalldadc5752010-08-24 06:29:42 +00001143ExprResult TemplateInstantiator::TransformCXXDefaultArgExpr(
John McCall47f29ea2009-12-08 09:21:05 +00001144 CXXDefaultArgExpr *E) {
Sebastian Redl14236c82009-11-08 13:56:19 +00001145 assert(!cast<FunctionDecl>(E->getParam()->getDeclContext())->
1146 getDescribedFunctionTemplate() &&
1147 "Default arg expressions are never formed in dependent cases.");
Douglas Gregor033f6752009-12-23 23:03:06 +00001148 return SemaRef.BuildCXXDefaultArgExpr(E->getUsedLocation(),
1149 cast<FunctionDecl>(E->getParam()->getDeclContext()),
1150 E->getParam());
Sebastian Redl14236c82009-11-08 13:56:19 +00001151}
1152
Douglas Gregor14cf7522010-04-30 18:55:50 +00001153QualType TemplateInstantiator::TransformFunctionProtoType(TypeLocBuilder &TLB,
John McCall31f82722010-11-12 08:19:04 +00001154 FunctionProtoTypeLoc TL) {
Douglas Gregor14cf7522010-04-30 18:55:50 +00001155 // We need a local instantiation scope for this function prototype.
John McCall19c1bfd2010-08-25 05:32:35 +00001156 LocalInstantiationScope Scope(SemaRef, /*CombineWithOuterScope=*/true);
John McCall31f82722010-11-12 08:19:04 +00001157 return inherited::TransformFunctionProtoType(TLB, TL);
John McCall58f10c32010-03-11 09:03:00 +00001158}
1159
1160ParmVarDecl *
Douglas Gregor715e4612011-01-14 22:40:04 +00001161TemplateInstantiator::TransformFunctionTypeParam(ParmVarDecl *OldParm,
1162 llvm::Optional<unsigned> NumExpansions) {
1163 return SemaRef.SubstParmVarDecl(OldParm, TemplateArgs,
1164 NumExpansions);
John McCall58f10c32010-03-11 09:03:00 +00001165}
1166
Mike Stump11289f42009-09-09 15:08:12 +00001167QualType
John McCall550e0c22009-10-21 00:40:46 +00001168TemplateInstantiator::TransformTemplateTypeParmType(TypeLocBuilder &TLB,
John McCall31f82722010-11-12 08:19:04 +00001169 TemplateTypeParmTypeLoc TL) {
John McCall424cec92011-01-19 06:33:43 +00001170 const TemplateTypeParmType *T = TL.getTypePtr();
Douglas Gregor01afeef2009-08-28 20:31:08 +00001171 if (T->getDepth() < TemplateArgs.getNumLevels()) {
Douglas Gregorfe1e1102009-02-27 19:31:52 +00001172 // Replace the template type parameter with its corresponding
1173 // template argument.
Mike Stump11289f42009-09-09 15:08:12 +00001174
1175 // If the corresponding template argument is NULL or doesn't exist, it's
1176 // because we are performing instantiation from explicitly-specified
1177 // template arguments in a function template class, but there were some
Douglas Gregore3f1f352009-07-01 00:28:38 +00001178 // arguments left unspecified.
John McCall550e0c22009-10-21 00:40:46 +00001179 if (!TemplateArgs.hasTemplateArgument(T->getDepth(), T->getIndex())) {
1180 TemplateTypeParmTypeLoc NewTL
1181 = TLB.push<TemplateTypeParmTypeLoc>(TL.getType());
1182 NewTL.setNameLoc(TL.getNameLoc());
1183 return TL.getType();
1184 }
Mike Stump11289f42009-09-09 15:08:12 +00001185
Douglas Gregor840bd6c2010-12-20 22:05:00 +00001186 TemplateArgument Arg = TemplateArgs(T->getDepth(), T->getIndex());
1187
1188 if (T->isParameterPack()) {
1189 assert(Arg.getKind() == TemplateArgument::Pack &&
1190 "Missing argument pack");
1191
1192 if (getSema().ArgumentPackSubstitutionIndex == -1) {
Douglas Gregorada4b792011-01-14 02:55:32 +00001193 // We have the template argument pack, but we're not expanding the
1194 // enclosing pack expansion yet. Just save the template argument
1195 // pack for later substitution.
1196 QualType Result
1197 = getSema().Context.getSubstTemplateTypeParmPackType(T, Arg);
1198 SubstTemplateTypeParmPackTypeLoc NewTL
1199 = TLB.push<SubstTemplateTypeParmPackTypeLoc>(Result);
1200 NewTL.setNameLoc(TL.getNameLoc());
1201 return Result;
Douglas Gregor840bd6c2010-12-20 22:05:00 +00001202 }
1203
Douglas Gregora8bac7f2011-01-10 07:32:04 +00001204 assert(getSema().ArgumentPackSubstitutionIndex < (int)Arg.pack_size());
Douglas Gregor840bd6c2010-12-20 22:05:00 +00001205 Arg = Arg.pack_begin()[getSema().ArgumentPackSubstitutionIndex];
1206 }
1207
1208 assert(Arg.getKind() == TemplateArgument::Type &&
Douglas Gregorfe1e1102009-02-27 19:31:52 +00001209 "Template argument kind mismatch");
Douglas Gregor01afeef2009-08-28 20:31:08 +00001210
Douglas Gregor840bd6c2010-12-20 22:05:00 +00001211 QualType Replacement = Arg.getAsType();
John McCallcebee162009-10-18 09:09:24 +00001212
1213 // TODO: only do this uniquing once, at the start of instantiation.
John McCall550e0c22009-10-21 00:40:46 +00001214 QualType Result
1215 = getSema().Context.getSubstTemplateTypeParmType(T, Replacement);
1216 SubstTemplateTypeParmTypeLoc NewTL
1217 = TLB.push<SubstTemplateTypeParmTypeLoc>(Result);
1218 NewTL.setNameLoc(TL.getNameLoc());
1219 return Result;
Mike Stump11289f42009-09-09 15:08:12 +00001220 }
Douglas Gregorfe1e1102009-02-27 19:31:52 +00001221
1222 // The template type parameter comes from an inner template (e.g.,
1223 // the template parameter list of a member template inside the
1224 // template we are instantiating). Create a new template type
1225 // parameter with the template "level" reduced by one.
John McCall550e0c22009-10-21 00:40:46 +00001226 QualType Result
1227 = getSema().Context.getTemplateTypeParmType(T->getDepth()
1228 - TemplateArgs.getNumLevels(),
1229 T->getIndex(),
1230 T->isParameterPack(),
Douglas Gregor2ebcae12010-06-16 15:23:05 +00001231 T->getName());
John McCall550e0c22009-10-21 00:40:46 +00001232 TemplateTypeParmTypeLoc NewTL = TLB.push<TemplateTypeParmTypeLoc>(Result);
1233 NewTL.setNameLoc(TL.getNameLoc());
1234 return Result;
Douglas Gregor17c0d7b2009-02-28 00:25:32 +00001235}
Douglas Gregorfe1e1102009-02-27 19:31:52 +00001236
Douglas Gregorada4b792011-01-14 02:55:32 +00001237QualType
1238TemplateInstantiator::TransformSubstTemplateTypeParmPackType(
1239 TypeLocBuilder &TLB,
1240 SubstTemplateTypeParmPackTypeLoc TL) {
1241 if (getSema().ArgumentPackSubstitutionIndex == -1) {
1242 // We aren't expanding the parameter pack, so just return ourselves.
1243 SubstTemplateTypeParmPackTypeLoc NewTL
1244 = TLB.push<SubstTemplateTypeParmPackTypeLoc>(TL.getType());
1245 NewTL.setNameLoc(TL.getNameLoc());
1246 return TL.getType();
1247 }
1248
1249 const TemplateArgument &ArgPack = TL.getTypePtr()->getArgumentPack();
1250 unsigned Index = (unsigned)getSema().ArgumentPackSubstitutionIndex;
1251 assert(Index < ArgPack.pack_size() && "Substitution index out-of-range");
1252
1253 QualType Result = ArgPack.pack_begin()[Index].getAsType();
1254 Result = getSema().Context.getSubstTemplateTypeParmType(
1255 TL.getTypePtr()->getReplacedParameter(),
1256 Result);
1257 SubstTemplateTypeParmTypeLoc NewTL
1258 = TLB.push<SubstTemplateTypeParmTypeLoc>(Result);
1259 NewTL.setNameLoc(TL.getNameLoc());
1260 return Result;
1261}
1262
John McCall76d824f2009-08-25 22:02:44 +00001263/// \brief Perform substitution on the type T with a given set of template
1264/// arguments.
Douglas Gregorfe1e1102009-02-27 19:31:52 +00001265///
1266/// This routine substitutes the given template arguments into the
1267/// type T and produces the instantiated type.
1268///
1269/// \param T the type into which the template arguments will be
1270/// substituted. If this type is not dependent, it will be returned
1271/// immediately.
1272///
1273/// \param TemplateArgs the template arguments that will be
1274/// substituted for the top-level template parameters within T.
1275///
Douglas Gregorfe1e1102009-02-27 19:31:52 +00001276/// \param Loc the location in the source code where this substitution
1277/// is being performed. It will typically be the location of the
1278/// declarator (if we're instantiating the type of some declaration)
1279/// or the location of the type in the source code (if, e.g., we're
1280/// instantiating the type of a cast expression).
1281///
1282/// \param Entity the name of the entity associated with a declaration
1283/// being instantiated (if any). May be empty to indicate that there
1284/// is no such entity (if, e.g., this is a type that occurs as part of
1285/// a cast expression) or that the entity has no name (e.g., an
1286/// unnamed function parameter).
1287///
1288/// \returns If the instantiation succeeds, the instantiated
1289/// type. Otherwise, produces diagnostics and returns a NULL type.
John McCallbcd03502009-12-07 02:54:59 +00001290TypeSourceInfo *Sema::SubstType(TypeSourceInfo *T,
John McCall609459e2009-10-21 00:58:09 +00001291 const MultiLevelTemplateArgumentList &Args,
1292 SourceLocation Loc,
1293 DeclarationName Entity) {
1294 assert(!ActiveTemplateInstantiations.empty() &&
1295 "Cannot perform an instantiation without some context on the "
1296 "instantiation stack");
1297
Douglas Gregor5a5073e2010-05-24 17:22:01 +00001298 if (!T->getType()->isDependentType() &&
1299 !T->getType()->isVariablyModifiedType())
John McCall609459e2009-10-21 00:58:09 +00001300 return T;
1301
1302 TemplateInstantiator Instantiator(*this, Args, Loc, Entity);
1303 return Instantiator.TransformType(T);
1304}
1305
Douglas Gregor5499af42011-01-05 23:12:31 +00001306TypeSourceInfo *Sema::SubstType(TypeLoc TL,
1307 const MultiLevelTemplateArgumentList &Args,
1308 SourceLocation Loc,
1309 DeclarationName Entity) {
1310 assert(!ActiveTemplateInstantiations.empty() &&
1311 "Cannot perform an instantiation without some context on the "
1312 "instantiation stack");
1313
1314 if (TL.getType().isNull())
1315 return 0;
1316
1317 if (!TL.getType()->isDependentType() &&
1318 !TL.getType()->isVariablyModifiedType()) {
1319 // FIXME: Make a copy of the TypeLoc data here, so that we can
1320 // return a new TypeSourceInfo. Inefficient!
1321 TypeLocBuilder TLB;
1322 TLB.pushFullCopy(TL);
1323 return TLB.getTypeSourceInfo(Context, TL.getType());
1324 }
1325
1326 TemplateInstantiator Instantiator(*this, Args, Loc, Entity);
1327 TypeLocBuilder TLB;
1328 TLB.reserve(TL.getFullDataSize());
1329 QualType Result = Instantiator.TransformType(TLB, TL);
1330 if (Result.isNull())
1331 return 0;
1332
1333 return TLB.getTypeSourceInfo(Context, Result);
1334}
1335
John McCall609459e2009-10-21 00:58:09 +00001336/// Deprecated form of the above.
Mike Stump11289f42009-09-09 15:08:12 +00001337QualType Sema::SubstType(QualType T,
Douglas Gregor01afeef2009-08-28 20:31:08 +00001338 const MultiLevelTemplateArgumentList &TemplateArgs,
John McCall76d824f2009-08-25 22:02:44 +00001339 SourceLocation Loc, DeclarationName Entity) {
Douglas Gregor79cf6032009-03-10 20:44:00 +00001340 assert(!ActiveTemplateInstantiations.empty() &&
1341 "Cannot perform an instantiation without some context on the "
1342 "instantiation stack");
1343
Douglas Gregor5a5073e2010-05-24 17:22:01 +00001344 // If T is not a dependent type or a variably-modified type, there
1345 // is nothing to do.
1346 if (!T->isDependentType() && !T->isVariablyModifiedType())
Douglas Gregorfe1e1102009-02-27 19:31:52 +00001347 return T;
1348
Douglas Gregord6ff3322009-08-04 16:50:30 +00001349 TemplateInstantiator Instantiator(*this, TemplateArgs, Loc, Entity);
1350 return Instantiator.TransformType(T);
Douglas Gregorfe1e1102009-02-27 19:31:52 +00001351}
Douglas Gregor463421d2009-03-03 04:44:36 +00001352
John McCallb29f78f2010-04-09 17:38:44 +00001353static bool NeedsInstantiationAsFunctionType(TypeSourceInfo *T) {
Douglas Gregor5a5073e2010-05-24 17:22:01 +00001354 if (T->getType()->isDependentType() || T->getType()->isVariablyModifiedType())
John McCallb29f78f2010-04-09 17:38:44 +00001355 return true;
1356
Abramo Bagnara6d810632010-12-14 22:11:44 +00001357 TypeLoc TL = T->getTypeLoc().IgnoreParens();
John McCallb29f78f2010-04-09 17:38:44 +00001358 if (!isa<FunctionProtoTypeLoc>(TL))
1359 return false;
1360
1361 FunctionProtoTypeLoc FP = cast<FunctionProtoTypeLoc>(TL);
1362 for (unsigned I = 0, E = FP.getNumArgs(); I != E; ++I) {
1363 ParmVarDecl *P = FP.getArg(I);
1364
1365 // TODO: currently we always rebuild expressions. When we
1366 // properly get lazier about this, we should use the same
1367 // logic to avoid rebuilding prototypes here.
Douglas Gregor9cc278222011-01-05 21:14:17 +00001368 if (P->hasDefaultArg())
John McCallb29f78f2010-04-09 17:38:44 +00001369 return true;
1370 }
1371
1372 return false;
1373}
1374
1375/// A form of SubstType intended specifically for instantiating the
1376/// type of a FunctionDecl. Its purpose is solely to force the
1377/// instantiation of default-argument expressions.
1378TypeSourceInfo *Sema::SubstFunctionDeclType(TypeSourceInfo *T,
1379 const MultiLevelTemplateArgumentList &Args,
1380 SourceLocation Loc,
1381 DeclarationName Entity) {
1382 assert(!ActiveTemplateInstantiations.empty() &&
1383 "Cannot perform an instantiation without some context on the "
1384 "instantiation stack");
1385
1386 if (!NeedsInstantiationAsFunctionType(T))
1387 return T;
1388
1389 TemplateInstantiator Instantiator(*this, Args, Loc, Entity);
1390
1391 TypeLocBuilder TLB;
1392
1393 TypeLoc TL = T->getTypeLoc();
1394 TLB.reserve(TL.getFullDataSize());
1395
John McCall31f82722010-11-12 08:19:04 +00001396 QualType Result = Instantiator.TransformType(TLB, TL);
John McCallb29f78f2010-04-09 17:38:44 +00001397 if (Result.isNull())
1398 return 0;
1399
1400 return TLB.getTypeSourceInfo(Context, Result);
1401}
1402
Douglas Gregor940bca72010-04-12 07:48:19 +00001403ParmVarDecl *Sema::SubstParmVarDecl(ParmVarDecl *OldParm,
Douglas Gregor715e4612011-01-14 22:40:04 +00001404 const MultiLevelTemplateArgumentList &TemplateArgs,
1405 llvm::Optional<unsigned> NumExpansions) {
Douglas Gregor940bca72010-04-12 07:48:19 +00001406 TypeSourceInfo *OldDI = OldParm->getTypeSourceInfo();
Douglas Gregor5499af42011-01-05 23:12:31 +00001407 TypeSourceInfo *NewDI = 0;
1408
Douglas Gregor5499af42011-01-05 23:12:31 +00001409 TypeLoc OldTL = OldDI->getTypeLoc();
1410 if (isa<PackExpansionTypeLoc>(OldTL)) {
1411 PackExpansionTypeLoc ExpansionTL = cast<PackExpansionTypeLoc>(OldTL);
Douglas Gregor5499af42011-01-05 23:12:31 +00001412
1413 // We have a function parameter pack. Substitute into the pattern of the
1414 // expansion.
1415 NewDI = SubstType(ExpansionTL.getPatternLoc(), TemplateArgs,
1416 OldParm->getLocation(), OldParm->getDeclName());
1417 if (!NewDI)
1418 return 0;
1419
1420 if (NewDI->getType()->containsUnexpandedParameterPack()) {
1421 // We still have unexpanded parameter packs, which means that
1422 // our function parameter is still a function parameter pack.
1423 // Therefore, make its type a pack expansion type.
Douglas Gregor0dca5fd2011-01-14 17:04:44 +00001424 NewDI = CheckPackExpansion(NewDI, ExpansionTL.getEllipsisLoc(),
Douglas Gregor715e4612011-01-14 22:40:04 +00001425 NumExpansions);
Douglas Gregor5499af42011-01-05 23:12:31 +00001426 }
1427 } else {
1428 NewDI = SubstType(OldDI, TemplateArgs, OldParm->getLocation(),
1429 OldParm->getDeclName());
1430 }
1431
Douglas Gregor940bca72010-04-12 07:48:19 +00001432 if (!NewDI)
1433 return 0;
1434
1435 if (NewDI->getType()->isVoidType()) {
1436 Diag(OldParm->getLocation(), diag::err_param_with_void_type);
1437 return 0;
1438 }
1439
1440 ParmVarDecl *NewParm = CheckParameter(Context.getTranslationUnitDecl(),
1441 NewDI, NewDI->getType(),
1442 OldParm->getIdentifier(),
1443 OldParm->getLocation(),
Douglas Gregorc4df4072010-04-19 22:54:31 +00001444 OldParm->getStorageClass(),
1445 OldParm->getStorageClassAsWritten());
Douglas Gregor940bca72010-04-12 07:48:19 +00001446 if (!NewParm)
1447 return 0;
Douglas Gregor6044d692010-05-19 17:02:24 +00001448
Douglas Gregor940bca72010-04-12 07:48:19 +00001449 // Mark the (new) default argument as uninstantiated (if any).
1450 if (OldParm->hasUninstantiatedDefaultArg()) {
1451 Expr *Arg = OldParm->getUninstantiatedDefaultArg();
1452 NewParm->setUninstantiatedDefaultArg(Arg);
Douglas Gregor758cb672010-10-12 18:23:32 +00001453 } else if (OldParm->hasUnparsedDefaultArg()) {
1454 NewParm->setUnparsedDefaultArg();
1455 UnparsedDefaultArgInstantiations[OldParm].push_back(NewParm);
Douglas Gregor940bca72010-04-12 07:48:19 +00001456 } else if (Expr *Arg = OldParm->getDefaultArg())
1457 NewParm->setUninstantiatedDefaultArg(Arg);
1458
1459 NewParm->setHasInheritedDefaultArg(OldParm->hasInheritedDefaultArg());
1460
Douglas Gregor5499af42011-01-05 23:12:31 +00001461 // FIXME: When OldParm is a parameter pack and NewParm is not a parameter
1462 // pack, we actually have a set of instantiated locations. Maintain this set!
Douglas Gregorf3010112011-01-07 16:43:16 +00001463 if (OldParm->isParameterPack() && !NewParm->isParameterPack()) {
1464 // Add the new parameter to
1465 CurrentInstantiationScope->InstantiatedLocalPackArg(OldParm, NewParm);
1466 } else {
1467 // Introduce an Old -> New mapping
Douglas Gregor5499af42011-01-05 23:12:31 +00001468 CurrentInstantiationScope->InstantiatedLocal(OldParm, NewParm);
Douglas Gregorf3010112011-01-07 16:43:16 +00001469 }
Douglas Gregor5499af42011-01-05 23:12:31 +00001470
Argyrios Kyrtzidis3816ed42010-07-19 10:14:41 +00001471 // FIXME: OldParm may come from a FunctionProtoType, in which case CurContext
1472 // can be anything, is this right ?
Fariborz Jahanian714447b2010-07-13 21:05:02 +00001473 NewParm->setDeclContext(CurContext);
Fariborz Jahaniana6c7efe2010-07-13 20:05:58 +00001474
Douglas Gregor940bca72010-04-12 07:48:19 +00001475 return NewParm;
1476}
1477
Douglas Gregordd472162011-01-07 00:20:55 +00001478/// \brief Substitute the given template arguments into the given set of
1479/// parameters, producing the set of parameter types that would be generated
1480/// from such a substitution.
1481bool Sema::SubstParmTypes(SourceLocation Loc,
1482 ParmVarDecl **Params, unsigned NumParams,
1483 const MultiLevelTemplateArgumentList &TemplateArgs,
Douglas Gregorf3010112011-01-07 16:43:16 +00001484 llvm::SmallVectorImpl<QualType> &ParamTypes,
1485 llvm::SmallVectorImpl<ParmVarDecl *> *OutParams) {
Douglas Gregordd472162011-01-07 00:20:55 +00001486 assert(!ActiveTemplateInstantiations.empty() &&
1487 "Cannot perform an instantiation without some context on the "
1488 "instantiation stack");
1489
1490 TemplateInstantiator Instantiator(*this, TemplateArgs, Loc,
1491 DeclarationName());
1492 return Instantiator.TransformFunctionTypeParams(Loc, Params, NumParams, 0,
Douglas Gregorf3010112011-01-07 16:43:16 +00001493 ParamTypes, OutParams);
Douglas Gregordd472162011-01-07 00:20:55 +00001494}
1495
John McCall76d824f2009-08-25 22:02:44 +00001496/// \brief Perform substitution on the base class specifiers of the
1497/// given class template specialization.
Douglas Gregor463421d2009-03-03 04:44:36 +00001498///
1499/// Produces a diagnostic and returns true on error, returns false and
1500/// attaches the instantiated base classes to the class template
1501/// specialization if successful.
Mike Stump11289f42009-09-09 15:08:12 +00001502bool
John McCall76d824f2009-08-25 22:02:44 +00001503Sema::SubstBaseSpecifiers(CXXRecordDecl *Instantiation,
1504 CXXRecordDecl *Pattern,
Douglas Gregor01afeef2009-08-28 20:31:08 +00001505 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor463421d2009-03-03 04:44:36 +00001506 bool Invalid = false;
Douglas Gregor6181ded2009-05-29 18:27:38 +00001507 llvm::SmallVector<CXXBaseSpecifier*, 4> InstantiatedBases;
Mike Stump11289f42009-09-09 15:08:12 +00001508 for (ClassTemplateSpecializationDecl::base_class_iterator
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001509 Base = Pattern->bases_begin(), BaseEnd = Pattern->bases_end();
Douglas Gregor2a72edd2009-03-10 18:52:44 +00001510 Base != BaseEnd; ++Base) {
Douglas Gregor463421d2009-03-03 04:44:36 +00001511 if (!Base->getType()->isDependentType()) {
Fariborz Jahanian5c14ec32009-07-22 17:41:53 +00001512 InstantiatedBases.push_back(new (Context) CXXBaseSpecifier(*Base));
Douglas Gregor463421d2009-03-03 04:44:36 +00001513 continue;
1514 }
1515
Douglas Gregor752a5952011-01-03 22:36:02 +00001516 SourceLocation EllipsisLoc;
Douglas Gregorc52264e2011-03-02 02:04:06 +00001517 TypeSourceInfo *BaseTypeLoc;
Douglas Gregor752a5952011-01-03 22:36:02 +00001518 if (Base->isPackExpansion()) {
1519 // This is a pack expansion. See whether we should expand it now, or
1520 // wait until later.
1521 llvm::SmallVector<UnexpandedParameterPack, 2> Unexpanded;
1522 collectUnexpandedParameterPacks(Base->getTypeSourceInfo()->getTypeLoc(),
1523 Unexpanded);
1524 bool ShouldExpand = false;
Douglas Gregora8bac7f2011-01-10 07:32:04 +00001525 bool RetainExpansion = false;
Douglas Gregor0dca5fd2011-01-14 17:04:44 +00001526 llvm::Optional<unsigned> NumExpansions;
Douglas Gregor752a5952011-01-03 22:36:02 +00001527 if (CheckParameterPacksForExpansion(Base->getEllipsisLoc(),
1528 Base->getSourceRange(),
1529 Unexpanded.data(), Unexpanded.size(),
1530 TemplateArgs, ShouldExpand,
Douglas Gregora8bac7f2011-01-10 07:32:04 +00001531 RetainExpansion,
Douglas Gregor752a5952011-01-03 22:36:02 +00001532 NumExpansions)) {
Douglas Gregor752a5952011-01-03 22:36:02 +00001533 Invalid = true;
Douglas Gregor44e7df62011-01-04 00:32:56 +00001534 continue;
Douglas Gregor752a5952011-01-03 22:36:02 +00001535 }
1536
1537 // If we should expand this pack expansion now, do so.
1538 if (ShouldExpand) {
Douglas Gregor0dca5fd2011-01-14 17:04:44 +00001539 for (unsigned I = 0; I != *NumExpansions; ++I) {
Douglas Gregor752a5952011-01-03 22:36:02 +00001540 Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(*this, I);
1541
1542 TypeSourceInfo *BaseTypeLoc = SubstType(Base->getTypeSourceInfo(),
1543 TemplateArgs,
1544 Base->getSourceRange().getBegin(),
1545 DeclarationName());
1546 if (!BaseTypeLoc) {
1547 Invalid = true;
1548 continue;
1549 }
1550
1551 if (CXXBaseSpecifier *InstantiatedBase
1552 = CheckBaseSpecifier(Instantiation,
1553 Base->getSourceRange(),
1554 Base->isVirtual(),
1555 Base->getAccessSpecifierAsWritten(),
1556 BaseTypeLoc,
1557 SourceLocation()))
1558 InstantiatedBases.push_back(InstantiatedBase);
1559 else
1560 Invalid = true;
1561 }
1562
1563 continue;
1564 }
1565
1566 // The resulting base specifier will (still) be a pack expansion.
1567 EllipsisLoc = Base->getEllipsisLoc();
Douglas Gregorc52264e2011-03-02 02:04:06 +00001568 Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(*this, -1);
1569 BaseTypeLoc = SubstType(Base->getTypeSourceInfo(),
1570 TemplateArgs,
1571 Base->getSourceRange().getBegin(),
1572 DeclarationName());
1573 } else {
1574 BaseTypeLoc = SubstType(Base->getTypeSourceInfo(),
1575 TemplateArgs,
1576 Base->getSourceRange().getBegin(),
1577 DeclarationName());
Douglas Gregor752a5952011-01-03 22:36:02 +00001578 }
1579
Nick Lewycky19b9f952010-07-26 16:56:01 +00001580 if (!BaseTypeLoc) {
Douglas Gregor463421d2009-03-03 04:44:36 +00001581 Invalid = true;
1582 continue;
1583 }
1584
1585 if (CXXBaseSpecifier *InstantiatedBase
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001586 = CheckBaseSpecifier(Instantiation,
Douglas Gregor463421d2009-03-03 04:44:36 +00001587 Base->getSourceRange(),
1588 Base->isVirtual(),
1589 Base->getAccessSpecifierAsWritten(),
Douglas Gregor752a5952011-01-03 22:36:02 +00001590 BaseTypeLoc,
1591 EllipsisLoc))
Douglas Gregor463421d2009-03-03 04:44:36 +00001592 InstantiatedBases.push_back(InstantiatedBase);
1593 else
1594 Invalid = true;
1595 }
1596
Douglas Gregor2a72edd2009-03-10 18:52:44 +00001597 if (!Invalid &&
Jay Foad7d0479f2009-05-21 09:52:38 +00001598 AttachBaseSpecifiers(Instantiation, InstantiatedBases.data(),
Douglas Gregor463421d2009-03-03 04:44:36 +00001599 InstantiatedBases.size()))
1600 Invalid = true;
1601
1602 return Invalid;
1603}
1604
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001605/// \brief Instantiate the definition of a class from a given pattern.
1606///
1607/// \param PointOfInstantiation The point of instantiation within the
1608/// source code.
1609///
1610/// \param Instantiation is the declaration whose definition is being
1611/// instantiated. This will be either a class template specialization
1612/// or a member class of a class template specialization.
1613///
1614/// \param Pattern is the pattern from which the instantiation
1615/// occurs. This will be either the declaration of a class template or
1616/// the declaration of a member class of a class template.
1617///
1618/// \param TemplateArgs The template arguments to be substituted into
1619/// the pattern.
1620///
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001621/// \param TSK the kind of implicit or explicit instantiation to perform.
Douglas Gregor8a2e6012009-08-24 15:23:48 +00001622///
1623/// \param Complain whether to complain if the class cannot be instantiated due
1624/// to the lack of a definition.
1625///
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001626/// \returns true if an error occurred, false otherwise.
1627bool
1628Sema::InstantiateClass(SourceLocation PointOfInstantiation,
1629 CXXRecordDecl *Instantiation, CXXRecordDecl *Pattern,
Douglas Gregor01afeef2009-08-28 20:31:08 +00001630 const MultiLevelTemplateArgumentList &TemplateArgs,
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001631 TemplateSpecializationKind TSK,
Douglas Gregor8a2e6012009-08-24 15:23:48 +00001632 bool Complain) {
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001633 bool Invalid = false;
John McCall87a44eb2009-08-20 01:44:21 +00001634
Mike Stump11289f42009-09-09 15:08:12 +00001635 CXXRecordDecl *PatternDef
Douglas Gregor0a5a2212010-02-11 01:04:33 +00001636 = cast_or_null<CXXRecordDecl>(Pattern->getDefinition());
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001637 if (!PatternDef) {
Douglas Gregor8a2e6012009-08-24 15:23:48 +00001638 if (!Complain) {
1639 // Say nothing
1640 } else if (Pattern == Instantiation->getInstantiatedFromMemberClass()) {
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001641 Diag(PointOfInstantiation,
1642 diag::err_implicit_instantiate_member_undefined)
1643 << Context.getTypeDeclType(Instantiation);
1644 Diag(Pattern->getLocation(), diag::note_member_of_template_here);
1645 } else {
Douglas Gregora1f49972009-05-13 00:25:59 +00001646 Diag(PointOfInstantiation, diag::err_template_instantiate_undefined)
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001647 << (TSK != TSK_ImplicitInstantiation)
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001648 << Context.getTypeDeclType(Instantiation);
1649 Diag(Pattern->getLocation(), diag::note_template_decl_here);
1650 }
1651 return true;
1652 }
1653 Pattern = PatternDef;
1654
Douglas Gregord6ba93d2009-10-15 15:54:05 +00001655 // \brief Record the point of instantiation.
1656 if (MemberSpecializationInfo *MSInfo
1657 = Instantiation->getMemberSpecializationInfo()) {
1658 MSInfo->setTemplateSpecializationKind(TSK);
1659 MSInfo->setPointOfInstantiation(PointOfInstantiation);
Douglas Gregoref6ab412009-10-27 06:26:26 +00001660 } else if (ClassTemplateSpecializationDecl *Spec
1661 = dyn_cast<ClassTemplateSpecializationDecl>(Instantiation)) {
1662 Spec->setTemplateSpecializationKind(TSK);
1663 Spec->setPointOfInstantiation(PointOfInstantiation);
Douglas Gregord6ba93d2009-10-15 15:54:05 +00001664 }
1665
Douglas Gregorf3430ae2009-03-25 21:23:52 +00001666 InstantiatingTemplate Inst(*this, PointOfInstantiation, Instantiation);
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001667 if (Inst)
1668 return true;
1669
1670 // Enter the scope of this instantiation. We don't use
1671 // PushDeclContext because we don't have a scope.
John McCall80e58cd2010-04-29 00:35:03 +00001672 ContextRAII SavedContext(*this, Instantiation);
Douglas Gregor17158422010-05-12 17:27:19 +00001673 EnterExpressionEvaluationContext EvalContext(*this,
John McCallfaf5fb42010-08-26 23:41:50 +00001674 Sema::PotentiallyEvaluated);
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001675
Douglas Gregor51121572010-03-24 01:33:17 +00001676 // If this is an instantiation of a local class, merge this local
1677 // instantiation scope with the enclosing scope. Otherwise, every
1678 // instantiation of a class has its own local instantiation scope.
1679 bool MergeWithParentScope = !Instantiation->isDefinedOutsideFunctionOrMethod();
John McCall19c1bfd2010-08-25 05:32:35 +00001680 LocalInstantiationScope Scope(*this, MergeWithParentScope);
Douglas Gregor51121572010-03-24 01:33:17 +00001681
John McCall6602bb12010-08-01 02:01:53 +00001682 // Pull attributes from the pattern onto the instantiation.
1683 InstantiateAttrs(TemplateArgs, Pattern, Instantiation);
1684
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001685 // Start the definition of this instantiation.
1686 Instantiation->startDefinition();
Douglas Gregore9029562010-05-06 00:28:52 +00001687
1688 Instantiation->setTagKind(Pattern->getTagKind());
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001689
John McCall76d824f2009-08-25 22:02:44 +00001690 // Do substitution on the base class specifiers.
1691 if (SubstBaseSpecifiers(Instantiation, Pattern, TemplateArgs))
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001692 Invalid = true;
1693
Douglas Gregor869853e2010-11-10 19:44:59 +00001694 TemplateDeclInstantiator Instantiator(*this, Instantiation, TemplateArgs);
John McCall48871652010-08-21 09:40:31 +00001695 llvm::SmallVector<Decl*, 4> Fields;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001696 for (RecordDecl::decl_iterator Member = Pattern->decls_begin(),
Mike Stump11289f42009-09-09 15:08:12 +00001697 MemberEnd = Pattern->decls_end();
Douglas Gregorbcced4e2009-04-09 21:40:53 +00001698 Member != MemberEnd; ++Member) {
Argyrios Kyrtzidis9a94d9b2010-11-04 03:18:57 +00001699 // Don't instantiate members not belonging in this semantic context.
1700 // e.g. for:
1701 // @code
1702 // template <int i> class A {
1703 // class B *g;
1704 // };
1705 // @endcode
1706 // 'class B' has the template as lexical context but semantically it is
1707 // introduced in namespace scope.
1708 if ((*Member)->getDeclContext() != Pattern)
1709 continue;
1710
Douglas Gregor869853e2010-11-10 19:44:59 +00001711 if ((*Member)->isInvalidDecl()) {
1712 Invalid = true;
1713 continue;
1714 }
1715
1716 Decl *NewMember = Instantiator.Visit(*Member);
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001717 if (NewMember) {
Eli Friedmand0e8de22009-12-07 00:22:08 +00001718 if (FieldDecl *Field = dyn_cast<FieldDecl>(NewMember))
John McCall48871652010-08-21 09:40:31 +00001719 Fields.push_back(Field);
Eli Friedmand0e8de22009-12-07 00:22:08 +00001720 else if (NewMember->isInvalidDecl())
1721 Invalid = true;
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001722 } else {
1723 // FIXME: Eventually, a NULL return will mean that one of the
Mike Stump87c57ac2009-05-16 07:39:55 +00001724 // instantiations was a semantic disaster, and we'll want to set Invalid =
1725 // true. For now, we expect to skip some members that we can't yet handle.
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001726 }
1727 }
1728
1729 // Finish checking fields.
John McCall48871652010-08-21 09:40:31 +00001730 ActOnFields(0, Instantiation->getLocation(), Instantiation,
Jay Foad7d0479f2009-05-21 09:52:38 +00001731 Fields.data(), Fields.size(), SourceLocation(), SourceLocation(),
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001732 0);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001733 CheckCompletedCXXClass(Instantiation);
Douglas Gregor3c74d412009-10-14 20:14:33 +00001734 if (Instantiation->isInvalidDecl())
1735 Invalid = true;
Douglas Gregor869853e2010-11-10 19:44:59 +00001736 else {
1737 // Instantiate any out-of-line class template partial
1738 // specializations now.
1739 for (TemplateDeclInstantiator::delayed_partial_spec_iterator
1740 P = Instantiator.delayed_partial_spec_begin(),
1741 PEnd = Instantiator.delayed_partial_spec_end();
1742 P != PEnd; ++P) {
1743 if (!Instantiator.InstantiateClassTemplatePartialSpecialization(
1744 P->first,
1745 P->second)) {
1746 Invalid = true;
1747 break;
1748 }
1749 }
1750 }
1751
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001752 // Exit the scope of this instantiation.
John McCall80e58cd2010-04-29 00:35:03 +00001753 SavedContext.pop();
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001754
Douglas Gregor88d292c2010-05-13 16:44:06 +00001755 if (!Invalid) {
Douglas Gregor28ad4b52009-05-26 20:50:29 +00001756 Consumer.HandleTagDeclDefinition(Instantiation);
1757
Douglas Gregor88d292c2010-05-13 16:44:06 +00001758 // Always emit the vtable for an explicit instantiation definition
1759 // of a polymorphic class template specialization.
1760 if (TSK == TSK_ExplicitInstantiationDefinition)
1761 MarkVTableUsed(PointOfInstantiation, Instantiation, true);
1762 }
1763
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001764 return Invalid;
1765}
1766
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +00001767namespace {
1768 /// \brief A partial specialization whose template arguments have matched
1769 /// a given template-id.
1770 struct PartialSpecMatchResult {
1771 ClassTemplatePartialSpecializationDecl *Partial;
1772 TemplateArgumentList *Args;
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +00001773 };
1774}
1775
Mike Stump11289f42009-09-09 15:08:12 +00001776bool
Douglas Gregor463421d2009-03-03 04:44:36 +00001777Sema::InstantiateClassTemplateSpecialization(
Douglas Gregoref6ab412009-10-27 06:26:26 +00001778 SourceLocation PointOfInstantiation,
Douglas Gregor463421d2009-03-03 04:44:36 +00001779 ClassTemplateSpecializationDecl *ClassTemplateSpec,
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001780 TemplateSpecializationKind TSK,
Douglas Gregor8a2e6012009-08-24 15:23:48 +00001781 bool Complain) {
Douglas Gregor463421d2009-03-03 04:44:36 +00001782 // Perform the actual instantiation on the canonical declaration.
1783 ClassTemplateSpec = cast<ClassTemplateSpecializationDecl>(
Argyrios Kyrtzidis6b7e3762009-07-18 00:34:25 +00001784 ClassTemplateSpec->getCanonicalDecl());
Douglas Gregor463421d2009-03-03 04:44:36 +00001785
Douglas Gregor4aa04b12009-09-11 21:19:12 +00001786 // Check whether we have already instantiated or specialized this class
1787 // template specialization.
1788 if (ClassTemplateSpec->getSpecializationKind() != TSK_Undeclared) {
1789 if (ClassTemplateSpec->getSpecializationKind() ==
1790 TSK_ExplicitInstantiationDeclaration &&
1791 TSK == TSK_ExplicitInstantiationDefinition) {
1792 // An explicit instantiation definition follows an explicit instantiation
1793 // declaration (C++0x [temp.explicit]p10); go ahead and perform the
1794 // explicit instantiation.
1795 ClassTemplateSpec->setSpecializationKind(TSK);
Douglas Gregor88d292c2010-05-13 16:44:06 +00001796
1797 // If this is an explicit instantiation definition, mark the
1798 // vtable as used.
1799 if (TSK == TSK_ExplicitInstantiationDefinition)
1800 MarkVTableUsed(PointOfInstantiation, ClassTemplateSpec, true);
1801
Douglas Gregor4aa04b12009-09-11 21:19:12 +00001802 return false;
1803 }
1804
1805 // We can only instantiate something that hasn't already been
1806 // instantiated or specialized. Fail without any diagnostics: our
1807 // caller will provide an error message.
Douglas Gregor463421d2009-03-03 04:44:36 +00001808 return true;
Douglas Gregor4aa04b12009-09-11 21:19:12 +00001809 }
Douglas Gregor463421d2009-03-03 04:44:36 +00001810
Douglas Gregor00a511f2009-09-15 16:51:42 +00001811 if (ClassTemplateSpec->isInvalidDecl())
1812 return true;
1813
Douglas Gregor463421d2009-03-03 04:44:36 +00001814 ClassTemplateDecl *Template = ClassTemplateSpec->getSpecializedTemplate();
Douglas Gregor01afeef2009-08-28 20:31:08 +00001815 CXXRecordDecl *Pattern = 0;
Douglas Gregor2373c592009-05-31 09:31:02 +00001816
Douglas Gregor170bc422009-06-12 22:31:52 +00001817 // C++ [temp.class.spec.match]p1:
1818 // When a class template is used in a context that requires an
1819 // instantiation of the class, it is necessary to determine
1820 // whether the instantiation is to be generated using the primary
1821 // template or one of the partial specializations. This is done by
1822 // matching the template arguments of the class template
1823 // specialization with the template argument lists of the partial
1824 // specializations.
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +00001825 typedef PartialSpecMatchResult MatchResult;
Douglas Gregorb7ae10f2009-06-05 00:53:49 +00001826 llvm::SmallVector<MatchResult, 4> Matched;
Douglas Gregor407e9612010-04-30 05:56:50 +00001827 llvm::SmallVector<ClassTemplatePartialSpecializationDecl *, 4> PartialSpecs;
1828 Template->getPartialSpecializations(PartialSpecs);
1829 for (unsigned I = 0, N = PartialSpecs.size(); I != N; ++I) {
1830 ClassTemplatePartialSpecializationDecl *Partial = PartialSpecs[I];
John McCallbc077cf2010-02-08 23:07:23 +00001831 TemplateDeductionInfo Info(Context, PointOfInstantiation);
Douglas Gregor181aa4a2009-06-12 18:26:56 +00001832 if (TemplateDeductionResult Result
Douglas Gregor407e9612010-04-30 05:56:50 +00001833 = DeduceTemplateArguments(Partial,
Douglas Gregor181aa4a2009-06-12 18:26:56 +00001834 ClassTemplateSpec->getTemplateArgs(),
1835 Info)) {
1836 // FIXME: Store the failed-deduction information for use in
1837 // diagnostics, later.
1838 (void)Result;
1839 } else {
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +00001840 Matched.push_back(PartialSpecMatchResult());
1841 Matched.back().Partial = Partial;
1842 Matched.back().Args = Info.take();
Douglas Gregor181aa4a2009-06-12 18:26:56 +00001843 }
Douglas Gregor2373c592009-05-31 09:31:02 +00001844 }
1845
Douglas Gregor0231d8d2011-01-19 20:10:05 +00001846 // If we're dealing with a member template where the template parameters
1847 // have been instantiated, this provides the original template parameters
1848 // from which the member template's parameters were instantiated.
1849 llvm::SmallVector<const NamedDecl *, 4> InstantiatedTemplateParameters;
1850
Douglas Gregor21610382009-10-29 00:04:11 +00001851 if (Matched.size() >= 1) {
Douglas Gregorbe999392009-09-15 16:23:51 +00001852 llvm::SmallVector<MatchResult, 4>::iterator Best = Matched.begin();
Douglas Gregor21610382009-10-29 00:04:11 +00001853 if (Matched.size() == 1) {
1854 // -- If exactly one matching specialization is found, the
1855 // instantiation is generated from that specialization.
1856 // We don't need to do anything for this.
1857 } else {
1858 // -- If more than one matching specialization is found, the
1859 // partial order rules (14.5.4.2) are used to determine
1860 // whether one of the specializations is more specialized
1861 // than the others. If none of the specializations is more
1862 // specialized than all of the other matching
1863 // specializations, then the use of the class template is
1864 // ambiguous and the program is ill-formed.
1865 for (llvm::SmallVector<MatchResult, 4>::iterator P = Best + 1,
1866 PEnd = Matched.end();
1867 P != PEnd; ++P) {
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +00001868 if (getMoreSpecializedPartialSpecialization(P->Partial, Best->Partial,
John McCallbc077cf2010-02-08 23:07:23 +00001869 PointOfInstantiation)
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +00001870 == P->Partial)
Douglas Gregor21610382009-10-29 00:04:11 +00001871 Best = P;
Douglas Gregorbe999392009-09-15 16:23:51 +00001872 }
Douglas Gregorbe999392009-09-15 16:23:51 +00001873
Douglas Gregor21610382009-10-29 00:04:11 +00001874 // Determine if the best partial specialization is more specialized than
1875 // the others.
1876 bool Ambiguous = false;
Douglas Gregorbe999392009-09-15 16:23:51 +00001877 for (llvm::SmallVector<MatchResult, 4>::iterator P = Matched.begin(),
1878 PEnd = Matched.end();
Douglas Gregor21610382009-10-29 00:04:11 +00001879 P != PEnd; ++P) {
1880 if (P != Best &&
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +00001881 getMoreSpecializedPartialSpecialization(P->Partial, Best->Partial,
John McCallbc077cf2010-02-08 23:07:23 +00001882 PointOfInstantiation)
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +00001883 != Best->Partial) {
Douglas Gregor21610382009-10-29 00:04:11 +00001884 Ambiguous = true;
1885 break;
1886 }
1887 }
1888
1889 if (Ambiguous) {
1890 // Partial ordering did not produce a clear winner. Complain.
1891 ClassTemplateSpec->setInvalidDecl();
1892 Diag(PointOfInstantiation, diag::err_partial_spec_ordering_ambiguous)
1893 << ClassTemplateSpec;
1894
1895 // Print the matching partial specializations.
1896 for (llvm::SmallVector<MatchResult, 4>::iterator P = Matched.begin(),
1897 PEnd = Matched.end();
1898 P != PEnd; ++P)
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +00001899 Diag(P->Partial->getLocation(), diag::note_partial_spec_match)
1900 << getTemplateArgumentBindingsText(
1901 P->Partial->getTemplateParameters(),
1902 *P->Args);
Douglas Gregor01afeef2009-08-28 20:31:08 +00001903
Douglas Gregor21610382009-10-29 00:04:11 +00001904 return true;
1905 }
Douglas Gregorbe999392009-09-15 16:23:51 +00001906 }
1907
1908 // Instantiate using the best class template partial specialization.
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +00001909 ClassTemplatePartialSpecializationDecl *OrigPartialSpec = Best->Partial;
Douglas Gregor21610382009-10-29 00:04:11 +00001910 while (OrigPartialSpec->getInstantiatedFromMember()) {
1911 // If we've found an explicit specialization of this class template,
1912 // stop here and use that as the pattern.
1913 if (OrigPartialSpec->isMemberSpecialization())
1914 break;
1915
1916 OrigPartialSpec = OrigPartialSpec->getInstantiatedFromMember();
1917 }
1918
1919 Pattern = OrigPartialSpec;
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +00001920 ClassTemplateSpec->setInstantiationOf(Best->Partial, Best->Args);
Douglas Gregor170bc422009-06-12 22:31:52 +00001921 } else {
1922 // -- If no matches are found, the instantiation is generated
1923 // from the primary template.
Douglas Gregor01afeef2009-08-28 20:31:08 +00001924 ClassTemplateDecl *OrigTemplate = Template;
Douglas Gregorcf915552009-10-13 16:30:37 +00001925 while (OrigTemplate->getInstantiatedFromMemberTemplate()) {
1926 // If we've found an explicit specialization of this class template,
1927 // stop here and use that as the pattern.
1928 if (OrigTemplate->isMemberSpecialization())
1929 break;
1930
Douglas Gregor01afeef2009-08-28 20:31:08 +00001931 OrigTemplate = OrigTemplate->getInstantiatedFromMemberTemplate();
Douglas Gregorcf915552009-10-13 16:30:37 +00001932 }
1933
Douglas Gregor01afeef2009-08-28 20:31:08 +00001934 Pattern = OrigTemplate->getTemplatedDecl();
Douglas Gregor2373c592009-05-31 09:31:02 +00001935 }
Douglas Gregor463421d2009-03-03 04:44:36 +00001936
Douglas Gregoref6ab412009-10-27 06:26:26 +00001937 bool Result = InstantiateClass(PointOfInstantiation, ClassTemplateSpec,
1938 Pattern,
1939 getTemplateInstantiationArgs(ClassTemplateSpec),
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001940 TSK,
Douglas Gregor8a2e6012009-08-24 15:23:48 +00001941 Complain);
Mike Stump11289f42009-09-09 15:08:12 +00001942
Douglas Gregorb7ae10f2009-06-05 00:53:49 +00001943 return Result;
Douglas Gregor463421d2009-03-03 04:44:36 +00001944}
Douglas Gregor90a1a652009-03-19 17:26:29 +00001945
John McCall76d824f2009-08-25 22:02:44 +00001946/// \brief Instantiates the definitions of all of the member
1947/// of the given class, which is an instantiation of a class template
1948/// or a member class of a template.
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00001949void
1950Sema::InstantiateClassMembers(SourceLocation PointOfInstantiation,
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001951 CXXRecordDecl *Instantiation,
1952 const MultiLevelTemplateArgumentList &TemplateArgs,
1953 TemplateSpecializationKind TSK) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001954 for (DeclContext::decl_iterator D = Instantiation->decls_begin(),
1955 DEnd = Instantiation->decls_end();
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00001956 D != DEnd; ++D) {
Douglas Gregor1d957a32009-10-27 18:42:08 +00001957 bool SuppressNew = false;
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00001958 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(*D)) {
Douglas Gregor1d957a32009-10-27 18:42:08 +00001959 if (FunctionDecl *Pattern
1960 = Function->getInstantiatedFromMemberFunction()) {
1961 MemberSpecializationInfo *MSInfo
1962 = Function->getMemberSpecializationInfo();
1963 assert(MSInfo && "No member specialization information?");
Douglas Gregor06aa50412010-04-09 21:02:29 +00001964 if (MSInfo->getTemplateSpecializationKind()
1965 == TSK_ExplicitSpecialization)
1966 continue;
1967
Douglas Gregor1d957a32009-10-27 18:42:08 +00001968 if (CheckSpecializationInstantiationRedecl(PointOfInstantiation, TSK,
1969 Function,
1970 MSInfo->getTemplateSpecializationKind(),
Nico Weberd75488d2010-09-27 21:02:09 +00001971 MSInfo->getPointOfInstantiation(),
Douglas Gregor1d957a32009-10-27 18:42:08 +00001972 SuppressNew) ||
1973 SuppressNew)
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001974 continue;
1975
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001976 if (Function->hasBody())
Douglas Gregor1d957a32009-10-27 18:42:08 +00001977 continue;
1978
1979 if (TSK == TSK_ExplicitInstantiationDefinition) {
1980 // C++0x [temp.explicit]p8:
1981 // An explicit instantiation definition that names a class template
1982 // specialization explicitly instantiates the class template
1983 // specialization and is only an explicit instantiation definition
1984 // of members whose definition is visible at the point of
1985 // instantiation.
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001986 if (!Pattern->hasBody())
Douglas Gregor1d957a32009-10-27 18:42:08 +00001987 continue;
1988
1989 Function->setTemplateSpecializationKind(TSK, PointOfInstantiation);
1990
1991 InstantiateFunctionDefinition(PointOfInstantiation, Function);
1992 } else {
1993 Function->setTemplateSpecializationKind(TSK, PointOfInstantiation);
1994 }
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001995 }
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00001996 } else if (VarDecl *Var = dyn_cast<VarDecl>(*D)) {
Douglas Gregor86d142a2009-10-08 07:24:58 +00001997 if (Var->isStaticDataMember()) {
Douglas Gregor1d957a32009-10-27 18:42:08 +00001998 MemberSpecializationInfo *MSInfo = Var->getMemberSpecializationInfo();
1999 assert(MSInfo && "No member specialization information?");
Douglas Gregor06aa50412010-04-09 21:02:29 +00002000 if (MSInfo->getTemplateSpecializationKind()
2001 == TSK_ExplicitSpecialization)
2002 continue;
2003
Douglas Gregor1d957a32009-10-27 18:42:08 +00002004 if (CheckSpecializationInstantiationRedecl(PointOfInstantiation, TSK,
2005 Var,
2006 MSInfo->getTemplateSpecializationKind(),
Nico Weberd75488d2010-09-27 21:02:09 +00002007 MSInfo->getPointOfInstantiation(),
Douglas Gregor1d957a32009-10-27 18:42:08 +00002008 SuppressNew) ||
2009 SuppressNew)
Douglas Gregorbbe8f462009-10-08 15:14:33 +00002010 continue;
2011
Douglas Gregor1d957a32009-10-27 18:42:08 +00002012 if (TSK == TSK_ExplicitInstantiationDefinition) {
2013 // C++0x [temp.explicit]p8:
2014 // An explicit instantiation definition that names a class template
2015 // specialization explicitly instantiates the class template
2016 // specialization and is only an explicit instantiation definition
2017 // of members whose definition is visible at the point of
2018 // instantiation.
2019 if (!Var->getInstantiatedFromStaticDataMember()
2020 ->getOutOfLineDefinition())
2021 continue;
2022
2023 Var->setTemplateSpecializationKind(TSK, PointOfInstantiation);
Douglas Gregor86d142a2009-10-08 07:24:58 +00002024 InstantiateStaticDataMemberDefinition(PointOfInstantiation, Var);
Douglas Gregor1d957a32009-10-27 18:42:08 +00002025 } else {
2026 Var->setTemplateSpecializationKind(TSK, PointOfInstantiation);
2027 }
2028 }
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00002029 } else if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(*D)) {
Douglas Gregor1da22252010-04-18 18:11:38 +00002030 // Always skip the injected-class-name, along with any
2031 // redeclarations of nested classes, since both would cause us
2032 // to try to instantiate the members of a class twice.
2033 if (Record->isInjectedClassName() || Record->getPreviousDeclaration())
Douglas Gregord801b062009-10-07 23:56:10 +00002034 continue;
2035
Douglas Gregor1d957a32009-10-27 18:42:08 +00002036 MemberSpecializationInfo *MSInfo = Record->getMemberSpecializationInfo();
2037 assert(MSInfo && "No member specialization information?");
Douglas Gregor06aa50412010-04-09 21:02:29 +00002038
2039 if (MSInfo->getTemplateSpecializationKind()
2040 == TSK_ExplicitSpecialization)
2041 continue;
Nico Weberd75488d2010-09-27 21:02:09 +00002042
Douglas Gregor1d957a32009-10-27 18:42:08 +00002043 if (CheckSpecializationInstantiationRedecl(PointOfInstantiation, TSK,
2044 Record,
2045 MSInfo->getTemplateSpecializationKind(),
Nico Weberd75488d2010-09-27 21:02:09 +00002046 MSInfo->getPointOfInstantiation(),
Douglas Gregor1d957a32009-10-27 18:42:08 +00002047 SuppressNew) ||
2048 SuppressNew)
Douglas Gregorbbe8f462009-10-08 15:14:33 +00002049 continue;
2050
Douglas Gregor1d957a32009-10-27 18:42:08 +00002051 CXXRecordDecl *Pattern = Record->getInstantiatedFromMemberClass();
2052 assert(Pattern && "Missing instantiated-from-template information");
2053
Douglas Gregor0a5a2212010-02-11 01:04:33 +00002054 if (!Record->getDefinition()) {
2055 if (!Pattern->getDefinition()) {
Douglas Gregor1d957a32009-10-27 18:42:08 +00002056 // C++0x [temp.explicit]p8:
2057 // An explicit instantiation definition that names a class template
2058 // specialization explicitly instantiates the class template
2059 // specialization and is only an explicit instantiation definition
2060 // of members whose definition is visible at the point of
2061 // instantiation.
2062 if (TSK == TSK_ExplicitInstantiationDeclaration) {
2063 MSInfo->setTemplateSpecializationKind(TSK);
2064 MSInfo->setPointOfInstantiation(PointOfInstantiation);
2065 }
2066
2067 continue;
2068 }
2069
2070 InstantiateClass(PointOfInstantiation, Record, Pattern,
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00002071 TemplateArgs,
2072 TSK);
Nico Weberd75488d2010-09-27 21:02:09 +00002073 } else {
2074 if (TSK == TSK_ExplicitInstantiationDefinition &&
2075 Record->getTemplateSpecializationKind() ==
2076 TSK_ExplicitInstantiationDeclaration) {
2077 Record->setTemplateSpecializationKind(TSK);
2078 MarkVTableUsed(PointOfInstantiation, Record, true);
2079 }
Douglas Gregor1d957a32009-10-27 18:42:08 +00002080 }
Douglas Gregorc093c1d2009-10-08 01:19:17 +00002081
Douglas Gregor0a5a2212010-02-11 01:04:33 +00002082 Pattern = cast_or_null<CXXRecordDecl>(Record->getDefinition());
Douglas Gregor1d957a32009-10-27 18:42:08 +00002083 if (Pattern)
2084 InstantiateClassMembers(PointOfInstantiation, Pattern, TemplateArgs,
2085 TSK);
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00002086 }
2087 }
2088}
2089
2090/// \brief Instantiate the definitions of all of the members of the
2091/// given class template specialization, which was named as part of an
2092/// explicit instantiation.
Mike Stump11289f42009-09-09 15:08:12 +00002093void
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00002094Sema::InstantiateClassTemplateSpecializationMembers(
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00002095 SourceLocation PointOfInstantiation,
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00002096 ClassTemplateSpecializationDecl *ClassTemplateSpec,
2097 TemplateSpecializationKind TSK) {
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00002098 // C++0x [temp.explicit]p7:
2099 // An explicit instantiation that names a class template
2100 // specialization is an explicit instantion of the same kind
2101 // (declaration or definition) of each of its members (not
2102 // including members inherited from base classes) that has not
2103 // been previously explicitly specialized in the translation unit
2104 // containing the explicit instantiation, except as described
2105 // below.
2106 InstantiateClassMembers(PointOfInstantiation, ClassTemplateSpec,
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00002107 getTemplateInstantiationArgs(ClassTemplateSpec),
2108 TSK);
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00002109}
2110
John McCalldadc5752010-08-24 06:29:42 +00002111StmtResult
Douglas Gregor01afeef2009-08-28 20:31:08 +00002112Sema::SubstStmt(Stmt *S, const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregorebe10102009-08-20 07:17:43 +00002113 if (!S)
2114 return Owned(S);
2115
2116 TemplateInstantiator Instantiator(*this, TemplateArgs,
2117 SourceLocation(),
2118 DeclarationName());
2119 return Instantiator.TransformStmt(S);
2120}
2121
John McCalldadc5752010-08-24 06:29:42 +00002122ExprResult
Douglas Gregor01afeef2009-08-28 20:31:08 +00002123Sema::SubstExpr(Expr *E, const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregora16548e2009-08-11 05:31:07 +00002124 if (!E)
2125 return Owned(E);
Mike Stump11289f42009-09-09 15:08:12 +00002126
Douglas Gregora16548e2009-08-11 05:31:07 +00002127 TemplateInstantiator Instantiator(*this, TemplateArgs,
2128 SourceLocation(),
2129 DeclarationName());
2130 return Instantiator.TransformExpr(E);
2131}
2132
Douglas Gregor2cd32a02011-01-07 19:35:17 +00002133bool Sema::SubstExprs(Expr **Exprs, unsigned NumExprs, bool IsCall,
2134 const MultiLevelTemplateArgumentList &TemplateArgs,
2135 llvm::SmallVectorImpl<Expr *> &Outputs) {
2136 if (NumExprs == 0)
2137 return false;
2138
2139 TemplateInstantiator Instantiator(*this, TemplateArgs,
2140 SourceLocation(),
2141 DeclarationName());
2142 return Instantiator.TransformExprs(Exprs, NumExprs, IsCall, Outputs);
2143}
2144
John McCall76d824f2009-08-25 22:02:44 +00002145/// \brief Do template substitution on a nested-name-specifier.
Douglas Gregorf21eb492009-03-26 23:50:42 +00002146NestedNameSpecifier *
John McCall76d824f2009-08-25 22:02:44 +00002147Sema::SubstNestedNameSpecifier(NestedNameSpecifier *NNS,
Douglas Gregor01afeef2009-08-28 20:31:08 +00002148 SourceRange Range,
2149 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor1135c352009-08-06 05:28:30 +00002150 TemplateInstantiator Instantiator(*this, TemplateArgs, Range.getBegin(),
2151 DeclarationName());
Douglas Gregorcd3f49f2010-02-25 04:46:04 +00002152 return Instantiator.TransformNestedNameSpecifier(NNS, Range);
Douglas Gregor90a1a652009-03-19 17:26:29 +00002153}
Douglas Gregoraa594892009-03-31 18:38:02 +00002154
Douglas Gregor14454802011-02-25 02:25:35 +00002155NestedNameSpecifierLoc
2156Sema::SubstNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS,
2157 const MultiLevelTemplateArgumentList &TemplateArgs) {
2158 if (!NNS)
2159 return NestedNameSpecifierLoc();
2160
2161 TemplateInstantiator Instantiator(*this, TemplateArgs, NNS.getBeginLoc(),
2162 DeclarationName());
2163 return Instantiator.TransformNestedNameSpecifierLoc(NNS);
2164}
2165
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002166/// \brief Do template substitution on declaration name info.
2167DeclarationNameInfo
2168Sema::SubstDeclarationNameInfo(const DeclarationNameInfo &NameInfo,
2169 const MultiLevelTemplateArgumentList &TemplateArgs) {
2170 TemplateInstantiator Instantiator(*this, TemplateArgs, NameInfo.getLoc(),
2171 NameInfo.getName());
2172 return Instantiator.TransformDeclarationNameInfo(NameInfo);
2173}
2174
Douglas Gregoraa594892009-03-31 18:38:02 +00002175TemplateName
Douglas Gregordf846d12011-03-02 18:46:51 +00002176Sema::SubstTemplateName(NestedNameSpecifierLoc QualifierLoc,
2177 TemplateName Name, SourceLocation Loc,
Douglas Gregor01afeef2009-08-28 20:31:08 +00002178 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor71dc5092009-08-06 06:41:21 +00002179 TemplateInstantiator Instantiator(*this, TemplateArgs, Loc,
2180 DeclarationName());
Douglas Gregordf846d12011-03-02 18:46:51 +00002181 CXXScopeSpec SS;
2182 SS.Adopt(QualifierLoc);
2183 return Instantiator.TransformTemplateName(SS, Name, Loc);
Douglas Gregoraa594892009-03-31 18:38:02 +00002184}
Douglas Gregorc43620d2009-06-11 00:06:24 +00002185
Douglas Gregor0f3feb42010-12-22 21:19:48 +00002186bool Sema::Subst(const TemplateArgumentLoc *Args, unsigned NumArgs,
2187 TemplateArgumentListInfo &Result,
John McCall0ad16662009-10-29 08:12:44 +00002188 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregore922c772009-08-04 22:27:00 +00002189 TemplateInstantiator Instantiator(*this, TemplateArgs, SourceLocation(),
2190 DeclarationName());
Douglas Gregor0f3feb42010-12-22 21:19:48 +00002191
2192 return Instantiator.TransformTemplateArguments(Args, NumArgs, Result);
Douglas Gregorc43620d2009-06-11 00:06:24 +00002193}
Douglas Gregor14cf7522010-04-30 18:55:50 +00002194
Douglas Gregorf3010112011-01-07 16:43:16 +00002195llvm::PointerUnion<Decl *, LocalInstantiationScope::DeclArgumentPack *> *
2196LocalInstantiationScope::findInstantiationOf(const Decl *D) {
Chris Lattnercab02a62011-02-17 20:34:02 +00002197 for (LocalInstantiationScope *Current = this; Current;
Douglas Gregor14cf7522010-04-30 18:55:50 +00002198 Current = Current->Outer) {
Chris Lattnercab02a62011-02-17 20:34:02 +00002199
Douglas Gregor14cf7522010-04-30 18:55:50 +00002200 // Check if we found something within this scope.
Douglas Gregore9fc8dc2010-12-21 21:22:51 +00002201 const Decl *CheckD = D;
2202 do {
Douglas Gregorf3010112011-01-07 16:43:16 +00002203 LocalDeclsMap::iterator Found = Current->LocalDecls.find(CheckD);
Douglas Gregore9fc8dc2010-12-21 21:22:51 +00002204 if (Found != Current->LocalDecls.end())
Douglas Gregorf3010112011-01-07 16:43:16 +00002205 return &Found->second;
Douglas Gregore9fc8dc2010-12-21 21:22:51 +00002206
2207 // If this is a tag declaration, it's possible that we need to look for
2208 // a previous declaration.
2209 if (const TagDecl *Tag = dyn_cast<TagDecl>(CheckD))
2210 CheckD = Tag->getPreviousDeclaration();
2211 else
2212 CheckD = 0;
2213 } while (CheckD);
2214
Douglas Gregor14cf7522010-04-30 18:55:50 +00002215 // If we aren't combined with our outer scope, we're done.
2216 if (!Current->CombineWithOuterScope)
2217 break;
2218 }
Chris Lattnercab02a62011-02-17 20:34:02 +00002219
2220 // If we didn't find the decl, then we either have a sema bug, or we have a
2221 // forward reference to a label declaration. Return null to indicate that
2222 // we have an uninstantiated label.
2223 assert(isa<LabelDecl>(D) && "declaration not instantiated in this scope");
Douglas Gregor14cf7522010-04-30 18:55:50 +00002224 return 0;
2225}
2226
John McCall19c1bfd2010-08-25 05:32:35 +00002227void LocalInstantiationScope::InstantiatedLocal(const Decl *D, Decl *Inst) {
Douglas Gregorf3010112011-01-07 16:43:16 +00002228 llvm::PointerUnion<Decl *, DeclArgumentPack *> &Stored = LocalDecls[D];
Douglas Gregora8bac7f2011-01-10 07:32:04 +00002229 if (Stored.isNull())
2230 Stored = Inst;
2231 else if (Stored.is<Decl *>()) {
2232 assert(Stored.get<Decl *>() == Inst && "Already instantiated this local");
2233 Stored = Inst;
2234 } else
2235 LocalDecls[D].get<DeclArgumentPack *>()->push_back(Inst);
Douglas Gregor14cf7522010-04-30 18:55:50 +00002236}
Douglas Gregorf3010112011-01-07 16:43:16 +00002237
2238void LocalInstantiationScope::InstantiatedLocalPackArg(const Decl *D,
2239 Decl *Inst) {
2240 DeclArgumentPack *Pack = LocalDecls[D].get<DeclArgumentPack *>();
2241 Pack->push_back(Inst);
2242}
2243
2244void LocalInstantiationScope::MakeInstantiatedLocalArgPack(const Decl *D) {
2245 llvm::PointerUnion<Decl *, DeclArgumentPack *> &Stored = LocalDecls[D];
2246 assert(Stored.isNull() && "Already instantiated this local");
2247 DeclArgumentPack *Pack = new DeclArgumentPack;
2248 Stored = Pack;
2249 ArgumentPacks.push_back(Pack);
2250}
2251
Douglas Gregora8bac7f2011-01-10 07:32:04 +00002252void LocalInstantiationScope::SetPartiallySubstitutedPack(NamedDecl *Pack,
2253 const TemplateArgument *ExplicitArgs,
2254 unsigned NumExplicitArgs) {
2255 assert((!PartiallySubstitutedPack || PartiallySubstitutedPack == Pack) &&
2256 "Already have a partially-substituted pack");
2257 assert((!PartiallySubstitutedPack
2258 || NumArgsInPartiallySubstitutedPack == NumExplicitArgs) &&
2259 "Wrong number of arguments in partially-substituted pack");
2260 PartiallySubstitutedPack = Pack;
2261 ArgsInPartiallySubstitutedPack = ExplicitArgs;
2262 NumArgsInPartiallySubstitutedPack = NumExplicitArgs;
2263}
2264
2265NamedDecl *LocalInstantiationScope::getPartiallySubstitutedPack(
2266 const TemplateArgument **ExplicitArgs,
2267 unsigned *NumExplicitArgs) const {
2268 if (ExplicitArgs)
2269 *ExplicitArgs = 0;
2270 if (NumExplicitArgs)
2271 *NumExplicitArgs = 0;
2272
2273 for (const LocalInstantiationScope *Current = this; Current;
2274 Current = Current->Outer) {
2275 if (Current->PartiallySubstitutedPack) {
2276 if (ExplicitArgs)
2277 *ExplicitArgs = Current->ArgsInPartiallySubstitutedPack;
2278 if (NumExplicitArgs)
2279 *NumExplicitArgs = Current->NumArgsInPartiallySubstitutedPack;
2280
2281 return Current->PartiallySubstitutedPack;
2282 }
2283
2284 if (!Current->CombineWithOuterScope)
2285 break;
2286 }
2287
2288 return 0;
2289}