blob: c951b2573a32e9a63b6d6792aac944243d3d35c6 [file] [log] [blame]
Douglas Gregor99ebf652009-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 McCall2d887082010-08-25 22:03:47 +000013#include "clang/Sema/SemaInternal.h"
Douglas Gregor577f75a2009-08-04 16:50:30 +000014#include "TreeTransform.h"
John McCall19510852010-08-20 18:27:03 +000015#include "clang/Sema/DeclSpec.h"
Douglas Gregore737f502010-08-12 20:07:10 +000016#include "clang/Sema/Lookup.h"
John McCall7cd088e2010-08-24 07:21:54 +000017#include "clang/Sema/Template.h"
John McCall2a7fb272010-08-25 05:32:35 +000018#include "clang/Sema/TemplateDeduction.h"
Douglas Gregoraba43bb2009-05-26 20:50:29 +000019#include "clang/AST/ASTConsumer.h"
Douglas Gregor99ebf652009-02-27 19:31:52 +000020#include "clang/AST/ASTContext.h"
21#include "clang/AST/Expr.h"
Douglas Gregor99ebf652009-02-27 19:31:52 +000022#include "clang/AST/DeclTemplate.h"
Douglas Gregor99ebf652009-02-27 19:31:52 +000023#include "clang/Basic/LangOptions.h"
24
25using namespace clang;
John McCall2a7fb272010-08-25 05:32:35 +000026using namespace sema;
Douglas Gregor99ebf652009-02-27 19:31:52 +000027
Douglas Gregoree1828a2009-03-10 18:03:33 +000028//===----------------------------------------------------------------------===/
29// Template Instantiation Support
30//===----------------------------------------------------------------------===/
31
Douglas Gregord6350ae2009-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 Gregor0f8716b2009-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 Gregor525f96c2010-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 Gregore7089b02010-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 Gregord1102432009-08-28 17:37:35 +000049MultiLevelTemplateArgumentList
Douglas Gregor0f8716b2009-11-09 19:17:50 +000050Sema::getTemplateInstantiationArgs(NamedDecl *D,
Douglas Gregor525f96c2010-02-05 07:33:43 +000051 const TemplateArgumentList *Innermost,
Douglas Gregore7089b02010-05-03 23:29:10 +000052 bool RelativeToPrimary,
53 const FunctionDecl *Pattern) {
Douglas Gregord1102432009-08-28 17:37:35 +000054 // Accumulate the set of template argument lists in this structure.
55 MultiLevelTemplateArgumentList Result;
Mike Stump1eb44332009-09-09 15:08:12 +000056
Douglas Gregor0f8716b2009-11-09 19:17:50 +000057 if (Innermost)
58 Result.addOuterTemplateArguments(Innermost);
59
Douglas Gregord1102432009-08-28 17:37:35 +000060 DeclContext *Ctx = dyn_cast<DeclContext>(D);
61 if (!Ctx)
62 Ctx = D->getDeclContext();
Mike Stump1eb44332009-09-09 15:08:12 +000063
John McCallf181d8a2009-08-29 03:16:09 +000064 while (!Ctx->isFileContext()) {
Douglas Gregord1102432009-08-28 17:37:35 +000065 // Add template arguments from a class template instantiation.
Mike Stump1eb44332009-09-09 15:08:12 +000066 if (ClassTemplateSpecializationDecl *Spec
Douglas Gregord1102432009-08-28 17:37:35 +000067 = dyn_cast<ClassTemplateSpecializationDecl>(Ctx)) {
68 // We're done when we hit an explicit specialization.
Douglas Gregor24bae922010-07-08 18:37:38 +000069 if (Spec->getSpecializationKind() == TSK_ExplicitSpecialization &&
70 !isa<ClassTemplatePartialSpecializationDecl>(Spec))
Douglas Gregord1102432009-08-28 17:37:35 +000071 break;
Mike Stump1eb44332009-09-09 15:08:12 +000072
Douglas Gregord1102432009-08-28 17:37:35 +000073 Result.addOuterTemplateArguments(&Spec->getTemplateInstantiationArgs());
Douglas Gregorfd056bc2009-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 Stump1eb44332009-09-09 15:08:12 +000080 }
Douglas Gregord1102432009-08-28 17:37:35 +000081 // Add template arguments from a function template specialization.
John McCallf181d8a2009-08-29 03:16:09 +000082 else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Ctx)) {
Douglas Gregor525f96c2010-02-05 07:33:43 +000083 if (!RelativeToPrimary &&
84 Function->getTemplateSpecializationKind()
85 == TSK_ExplicitSpecialization)
Douglas Gregorfd056bc2009-10-13 16:30:37 +000086 break;
87
Douglas Gregord1102432009-08-28 17:37:35 +000088 if (const TemplateArgumentList *TemplateArgs
Douglas Gregorfd056bc2009-10-13 16:30:37 +000089 = Function->getTemplateSpecializationArgs()) {
90 // Add the template arguments for this specialization.
Douglas Gregord1102432009-08-28 17:37:35 +000091 Result.addOuterTemplateArguments(TemplateArgs);
John McCallf181d8a2009-08-29 03:16:09 +000092
Douglas Gregorfd056bc2009-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;
Douglas Gregorc494f772011-03-05 17:54:25 +000098 } else if (FunctionTemplateDecl *FunTmpl
99 = Function->getDescribedFunctionTemplate()) {
100 // Add the "injected" template arguments.
101 std::pair<const TemplateArgument *, unsigned>
102 Injected = FunTmpl->getInjectedTemplateArgs();
103 Result.addOuterTemplateArguments(Injected.first, Injected.second);
Douglas Gregorfd056bc2009-10-13 16:30:37 +0000104 }
105
John McCallf181d8a2009-08-29 03:16:09 +0000106 // If this is a friend declaration and it declares an entity at
107 // namespace scope, take arguments from its lexical parent
Douglas Gregore7089b02010-05-03 23:29:10 +0000108 // instead of its semantic parent, unless of course the pattern we're
109 // instantiating actually comes from the file's context!
John McCallf181d8a2009-08-29 03:16:09 +0000110 if (Function->getFriendObjectKind() &&
Douglas Gregore7089b02010-05-03 23:29:10 +0000111 Function->getDeclContext()->isFileContext() &&
112 (!Pattern || !Pattern->getLexicalDeclContext()->isFileContext())) {
John McCallf181d8a2009-08-29 03:16:09 +0000113 Ctx = Function->getLexicalDeclContext();
Douglas Gregor525f96c2010-02-05 07:33:43 +0000114 RelativeToPrimary = false;
John McCallf181d8a2009-08-29 03:16:09 +0000115 continue;
116 }
Douglas Gregor24bae922010-07-08 18:37:38 +0000117 } else if (CXXRecordDecl *Rec = dyn_cast<CXXRecordDecl>(Ctx)) {
118 if (ClassTemplateDecl *ClassTemplate = Rec->getDescribedClassTemplate()) {
119 QualType T = ClassTemplate->getInjectedClassNameSpecialization();
120 const TemplateSpecializationType *TST
121 = cast<TemplateSpecializationType>(Context.getCanonicalType(T));
122 Result.addOuterTemplateArguments(TST->getArgs(), TST->getNumArgs());
123 if (ClassTemplate->isMemberSpecialization())
124 break;
125 }
Douglas Gregord1102432009-08-28 17:37:35 +0000126 }
John McCallf181d8a2009-08-29 03:16:09 +0000127
128 Ctx = Ctx->getParent();
Douglas Gregor525f96c2010-02-05 07:33:43 +0000129 RelativeToPrimary = false;
Douglas Gregor54dabfc2009-05-14 23:26:13 +0000130 }
Mike Stump1eb44332009-09-09 15:08:12 +0000131
Douglas Gregord1102432009-08-28 17:37:35 +0000132 return Result;
Douglas Gregor54dabfc2009-05-14 23:26:13 +0000133}
134
Douglas Gregorf35f8282009-11-11 21:54:23 +0000135bool Sema::ActiveTemplateInstantiation::isInstantiationRecord() const {
136 switch (Kind) {
137 case TemplateInstantiation:
138 case DefaultTemplateArgumentInstantiation:
139 case DefaultFunctionArgumentInstantiation:
140 return true;
141
142 case ExplicitTemplateArgumentSubstitution:
143 case DeducedTemplateArgumentSubstitution:
144 case PriorTemplateArgumentSubstitution:
145 case DefaultTemplateArgumentChecking:
146 return false;
147 }
148
149 return true;
150}
151
Douglas Gregor26dce442009-03-10 00:06:19 +0000152Sema::InstantiatingTemplate::
153InstantiatingTemplate(Sema &SemaRef, SourceLocation PointOfInstantiation,
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000154 Decl *Entity,
Douglas Gregor26dce442009-03-10 00:06:19 +0000155 SourceRange InstantiationRange)
Douglas Gregor1eee5dc2011-01-27 22:31:44 +0000156 : SemaRef(SemaRef),
157 SavedInNonInstantiationSFINAEContext(
158 SemaRef.InNonInstantiationSFINAEContext)
159{
Douglas Gregordf667e72009-03-10 20:44:00 +0000160 Invalid = CheckInstantiationDepth(PointOfInstantiation,
161 InstantiationRange);
162 if (!Invalid) {
Douglas Gregor26dce442009-03-10 00:06:19 +0000163 ActiveTemplateInstantiation Inst;
Douglas Gregordf667e72009-03-10 20:44:00 +0000164 Inst.Kind = ActiveTemplateInstantiation::TemplateInstantiation;
Douglas Gregor26dce442009-03-10 00:06:19 +0000165 Inst.PointOfInstantiation = PointOfInstantiation;
Douglas Gregordf667e72009-03-10 20:44:00 +0000166 Inst.Entity = reinterpret_cast<uintptr_t>(Entity);
Douglas Gregor313a81d2009-03-12 18:36:18 +0000167 Inst.TemplateArgs = 0;
168 Inst.NumTemplateArgs = 0;
Douglas Gregordf667e72009-03-10 20:44:00 +0000169 Inst.InstantiationRange = InstantiationRange;
Douglas Gregor1eee5dc2011-01-27 22:31:44 +0000170 SemaRef.InNonInstantiationSFINAEContext = false;
Douglas Gregordf667e72009-03-10 20:44:00 +0000171 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
Douglas Gregordf667e72009-03-10 20:44:00 +0000172 }
173}
174
Mike Stump1eb44332009-09-09 15:08:12 +0000175Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef,
Douglas Gregordf667e72009-03-10 20:44:00 +0000176 SourceLocation PointOfInstantiation,
177 TemplateDecl *Template,
178 const TemplateArgument *TemplateArgs,
179 unsigned NumTemplateArgs,
180 SourceRange InstantiationRange)
Douglas Gregor1eee5dc2011-01-27 22:31:44 +0000181 : SemaRef(SemaRef),
182 SavedInNonInstantiationSFINAEContext(
183 SemaRef.InNonInstantiationSFINAEContext)
184{
Douglas Gregordf667e72009-03-10 20:44:00 +0000185 Invalid = CheckInstantiationDepth(PointOfInstantiation,
186 InstantiationRange);
187 if (!Invalid) {
188 ActiveTemplateInstantiation Inst;
Mike Stump1eb44332009-09-09 15:08:12 +0000189 Inst.Kind
Douglas Gregordf667e72009-03-10 20:44:00 +0000190 = ActiveTemplateInstantiation::DefaultTemplateArgumentInstantiation;
191 Inst.PointOfInstantiation = PointOfInstantiation;
192 Inst.Entity = reinterpret_cast<uintptr_t>(Template);
193 Inst.TemplateArgs = TemplateArgs;
194 Inst.NumTemplateArgs = NumTemplateArgs;
Douglas Gregor26dce442009-03-10 00:06:19 +0000195 Inst.InstantiationRange = InstantiationRange;
Douglas Gregor1eee5dc2011-01-27 22:31:44 +0000196 SemaRef.InNonInstantiationSFINAEContext = false;
Douglas Gregor26dce442009-03-10 00:06:19 +0000197 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
Douglas Gregor26dce442009-03-10 00:06:19 +0000198 }
199}
200
Mike Stump1eb44332009-09-09 15:08:12 +0000201Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef,
Douglas Gregor637a4092009-06-10 23:47:09 +0000202 SourceLocation PointOfInstantiation,
Douglas Gregorcca9e962009-07-01 22:01:06 +0000203 FunctionTemplateDecl *FunctionTemplate,
204 const TemplateArgument *TemplateArgs,
205 unsigned NumTemplateArgs,
206 ActiveTemplateInstantiation::InstantiationKind Kind,
Douglas Gregor9b623632010-10-12 23:32:35 +0000207 sema::TemplateDeductionInfo &DeductionInfo,
Douglas Gregorcca9e962009-07-01 22:01:06 +0000208 SourceRange InstantiationRange)
Douglas Gregor1eee5dc2011-01-27 22:31:44 +0000209 : SemaRef(SemaRef),
210 SavedInNonInstantiationSFINAEContext(
211 SemaRef.InNonInstantiationSFINAEContext)
212{
Douglas Gregorcca9e962009-07-01 22:01:06 +0000213 Invalid = CheckInstantiationDepth(PointOfInstantiation,
214 InstantiationRange);
215 if (!Invalid) {
216 ActiveTemplateInstantiation Inst;
217 Inst.Kind = Kind;
218 Inst.PointOfInstantiation = PointOfInstantiation;
219 Inst.Entity = reinterpret_cast<uintptr_t>(FunctionTemplate);
220 Inst.TemplateArgs = TemplateArgs;
221 Inst.NumTemplateArgs = NumTemplateArgs;
Douglas Gregor9b623632010-10-12 23:32:35 +0000222 Inst.DeductionInfo = &DeductionInfo;
Douglas Gregorcca9e962009-07-01 22:01:06 +0000223 Inst.InstantiationRange = InstantiationRange;
Douglas Gregor1eee5dc2011-01-27 22:31:44 +0000224 SemaRef.InNonInstantiationSFINAEContext = false;
Douglas Gregorcca9e962009-07-01 22:01:06 +0000225 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
Douglas Gregorf35f8282009-11-11 21:54:23 +0000226
227 if (!Inst.isInstantiationRecord())
228 ++SemaRef.NonInstantiationEntries;
Douglas Gregorcca9e962009-07-01 22:01:06 +0000229 }
230}
231
Mike Stump1eb44332009-09-09 15:08:12 +0000232Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef,
Douglas Gregorcca9e962009-07-01 22:01:06 +0000233 SourceLocation PointOfInstantiation,
Douglas Gregor637a4092009-06-10 23:47:09 +0000234 ClassTemplatePartialSpecializationDecl *PartialSpec,
235 const TemplateArgument *TemplateArgs,
236 unsigned NumTemplateArgs,
Douglas Gregor9b623632010-10-12 23:32:35 +0000237 sema::TemplateDeductionInfo &DeductionInfo,
Douglas Gregor637a4092009-06-10 23:47:09 +0000238 SourceRange InstantiationRange)
Douglas Gregor1eee5dc2011-01-27 22:31:44 +0000239 : SemaRef(SemaRef),
240 SavedInNonInstantiationSFINAEContext(
241 SemaRef.InNonInstantiationSFINAEContext)
242{
Douglas Gregorf35f8282009-11-11 21:54:23 +0000243 Invalid = false;
244
245 ActiveTemplateInstantiation Inst;
246 Inst.Kind = ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution;
247 Inst.PointOfInstantiation = PointOfInstantiation;
248 Inst.Entity = reinterpret_cast<uintptr_t>(PartialSpec);
249 Inst.TemplateArgs = TemplateArgs;
250 Inst.NumTemplateArgs = NumTemplateArgs;
Douglas Gregor9b623632010-10-12 23:32:35 +0000251 Inst.DeductionInfo = &DeductionInfo;
Douglas Gregorf35f8282009-11-11 21:54:23 +0000252 Inst.InstantiationRange = InstantiationRange;
Douglas Gregor1eee5dc2011-01-27 22:31:44 +0000253 SemaRef.InNonInstantiationSFINAEContext = false;
Douglas Gregorf35f8282009-11-11 21:54:23 +0000254 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
255
256 assert(!Inst.isInstantiationRecord());
257 ++SemaRef.NonInstantiationEntries;
Douglas Gregor637a4092009-06-10 23:47:09 +0000258}
259
Mike Stump1eb44332009-09-09 15:08:12 +0000260Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef,
Douglas Gregor9148c3f2009-11-11 19:13:48 +0000261 SourceLocation PointOfInstantiation,
Anders Carlsson25cae7f2009-09-05 05:14:19 +0000262 ParmVarDecl *Param,
263 const TemplateArgument *TemplateArgs,
264 unsigned NumTemplateArgs,
265 SourceRange InstantiationRange)
Douglas Gregor1eee5dc2011-01-27 22:31:44 +0000266 : SemaRef(SemaRef),
267 SavedInNonInstantiationSFINAEContext(
268 SemaRef.InNonInstantiationSFINAEContext)
269{
Douglas Gregor9148c3f2009-11-11 19:13:48 +0000270 Invalid = CheckInstantiationDepth(PointOfInstantiation, InstantiationRange);
Anders Carlsson25cae7f2009-09-05 05:14:19 +0000271
272 if (!Invalid) {
273 ActiveTemplateInstantiation Inst;
274 Inst.Kind
275 = ActiveTemplateInstantiation::DefaultFunctionArgumentInstantiation;
Douglas Gregor9148c3f2009-11-11 19:13:48 +0000276 Inst.PointOfInstantiation = PointOfInstantiation;
Anders Carlsson25cae7f2009-09-05 05:14:19 +0000277 Inst.Entity = reinterpret_cast<uintptr_t>(Param);
278 Inst.TemplateArgs = TemplateArgs;
279 Inst.NumTemplateArgs = NumTemplateArgs;
280 Inst.InstantiationRange = InstantiationRange;
Douglas Gregor1eee5dc2011-01-27 22:31:44 +0000281 SemaRef.InNonInstantiationSFINAEContext = false;
Anders Carlsson25cae7f2009-09-05 05:14:19 +0000282 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
Douglas Gregor9148c3f2009-11-11 19:13:48 +0000283 }
284}
285
286Sema::InstantiatingTemplate::
287InstantiatingTemplate(Sema &SemaRef, SourceLocation PointOfInstantiation,
Douglas Gregor54c53cc2011-01-04 23:35:54 +0000288 NamedDecl *Template,
Douglas Gregor9148c3f2009-11-11 19:13:48 +0000289 NonTypeTemplateParmDecl *Param,
290 const TemplateArgument *TemplateArgs,
291 unsigned NumTemplateArgs,
Douglas Gregor1eee5dc2011-01-27 22:31:44 +0000292 SourceRange InstantiationRange)
293 : SemaRef(SemaRef),
294 SavedInNonInstantiationSFINAEContext(
295 SemaRef.InNonInstantiationSFINAEContext)
296{
Douglas Gregorf35f8282009-11-11 21:54:23 +0000297 Invalid = false;
Douglas Gregor9148c3f2009-11-11 19:13:48 +0000298
Douglas Gregorf35f8282009-11-11 21:54:23 +0000299 ActiveTemplateInstantiation Inst;
300 Inst.Kind = ActiveTemplateInstantiation::PriorTemplateArgumentSubstitution;
301 Inst.PointOfInstantiation = PointOfInstantiation;
302 Inst.Template = Template;
303 Inst.Entity = reinterpret_cast<uintptr_t>(Param);
304 Inst.TemplateArgs = TemplateArgs;
305 Inst.NumTemplateArgs = NumTemplateArgs;
306 Inst.InstantiationRange = InstantiationRange;
Douglas Gregor1eee5dc2011-01-27 22:31:44 +0000307 SemaRef.InNonInstantiationSFINAEContext = false;
Douglas Gregorf35f8282009-11-11 21:54:23 +0000308 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
309
310 assert(!Inst.isInstantiationRecord());
311 ++SemaRef.NonInstantiationEntries;
Douglas Gregor9148c3f2009-11-11 19:13:48 +0000312}
313
314Sema::InstantiatingTemplate::
315InstantiatingTemplate(Sema &SemaRef, SourceLocation PointOfInstantiation,
Douglas Gregor54c53cc2011-01-04 23:35:54 +0000316 NamedDecl *Template,
Douglas Gregor9148c3f2009-11-11 19:13:48 +0000317 TemplateTemplateParmDecl *Param,
318 const TemplateArgument *TemplateArgs,
319 unsigned NumTemplateArgs,
Douglas Gregor1eee5dc2011-01-27 22:31:44 +0000320 SourceRange InstantiationRange)
321 : SemaRef(SemaRef),
322 SavedInNonInstantiationSFINAEContext(
323 SemaRef.InNonInstantiationSFINAEContext)
324{
Douglas Gregorf35f8282009-11-11 21:54:23 +0000325 Invalid = false;
326 ActiveTemplateInstantiation Inst;
327 Inst.Kind = ActiveTemplateInstantiation::PriorTemplateArgumentSubstitution;
328 Inst.PointOfInstantiation = PointOfInstantiation;
329 Inst.Template = Template;
330 Inst.Entity = reinterpret_cast<uintptr_t>(Param);
331 Inst.TemplateArgs = TemplateArgs;
332 Inst.NumTemplateArgs = NumTemplateArgs;
333 Inst.InstantiationRange = InstantiationRange;
Douglas Gregor1eee5dc2011-01-27 22:31:44 +0000334 SemaRef.InNonInstantiationSFINAEContext = false;
Douglas Gregorf35f8282009-11-11 21:54:23 +0000335 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
Douglas Gregor9148c3f2009-11-11 19:13:48 +0000336
Douglas Gregorf35f8282009-11-11 21:54:23 +0000337 assert(!Inst.isInstantiationRecord());
338 ++SemaRef.NonInstantiationEntries;
339}
340
341Sema::InstantiatingTemplate::
342InstantiatingTemplate(Sema &SemaRef, SourceLocation PointOfInstantiation,
343 TemplateDecl *Template,
344 NamedDecl *Param,
345 const TemplateArgument *TemplateArgs,
346 unsigned NumTemplateArgs,
Douglas Gregor1eee5dc2011-01-27 22:31:44 +0000347 SourceRange InstantiationRange)
348 : SemaRef(SemaRef),
349 SavedInNonInstantiationSFINAEContext(
350 SemaRef.InNonInstantiationSFINAEContext)
351{
Douglas Gregorf35f8282009-11-11 21:54:23 +0000352 Invalid = false;
353
354 ActiveTemplateInstantiation Inst;
355 Inst.Kind = ActiveTemplateInstantiation::DefaultTemplateArgumentChecking;
356 Inst.PointOfInstantiation = PointOfInstantiation;
357 Inst.Template = Template;
358 Inst.Entity = reinterpret_cast<uintptr_t>(Param);
359 Inst.TemplateArgs = TemplateArgs;
360 Inst.NumTemplateArgs = NumTemplateArgs;
361 Inst.InstantiationRange = InstantiationRange;
Douglas Gregor1eee5dc2011-01-27 22:31:44 +0000362 SemaRef.InNonInstantiationSFINAEContext = false;
Douglas Gregorf35f8282009-11-11 21:54:23 +0000363 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
364
365 assert(!Inst.isInstantiationRecord());
366 ++SemaRef.NonInstantiationEntries;
Anders Carlsson25cae7f2009-09-05 05:14:19 +0000367}
368
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000369void Sema::InstantiatingTemplate::Clear() {
370 if (!Invalid) {
Douglas Gregorf35f8282009-11-11 21:54:23 +0000371 if (!SemaRef.ActiveTemplateInstantiations.back().isInstantiationRecord()) {
372 assert(SemaRef.NonInstantiationEntries > 0);
373 --SemaRef.NonInstantiationEntries;
374 }
Douglas Gregor1eee5dc2011-01-27 22:31:44 +0000375 SemaRef.InNonInstantiationSFINAEContext
376 = SavedInNonInstantiationSFINAEContext;
Douglas Gregor26dce442009-03-10 00:06:19 +0000377 SemaRef.ActiveTemplateInstantiations.pop_back();
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000378 Invalid = true;
379 }
Douglas Gregor26dce442009-03-10 00:06:19 +0000380}
381
Douglas Gregordf667e72009-03-10 20:44:00 +0000382bool Sema::InstantiatingTemplate::CheckInstantiationDepth(
383 SourceLocation PointOfInstantiation,
384 SourceRange InstantiationRange) {
Douglas Gregorf35f8282009-11-11 21:54:23 +0000385 assert(SemaRef.NonInstantiationEntries <=
386 SemaRef.ActiveTemplateInstantiations.size());
387 if ((SemaRef.ActiveTemplateInstantiations.size() -
388 SemaRef.NonInstantiationEntries)
389 <= SemaRef.getLangOptions().InstantiationDepth)
Douglas Gregordf667e72009-03-10 20:44:00 +0000390 return false;
391
Mike Stump1eb44332009-09-09 15:08:12 +0000392 SemaRef.Diag(PointOfInstantiation,
Douglas Gregordf667e72009-03-10 20:44:00 +0000393 diag::err_template_recursion_depth_exceeded)
394 << SemaRef.getLangOptions().InstantiationDepth
395 << InstantiationRange;
396 SemaRef.Diag(PointOfInstantiation, diag::note_template_recursion_depth)
397 << SemaRef.getLangOptions().InstantiationDepth;
398 return true;
399}
400
Douglas Gregoree1828a2009-03-10 18:03:33 +0000401/// \brief Prints the current instantiation stack through a series of
402/// notes.
403void Sema::PrintInstantiationStack() {
Douglas Gregor575cf372010-04-20 07:18:24 +0000404 // Determine which template instantiations to skip, if any.
405 unsigned SkipStart = ActiveTemplateInstantiations.size(), SkipEnd = SkipStart;
406 unsigned Limit = Diags.getTemplateBacktraceLimit();
407 if (Limit && Limit < ActiveTemplateInstantiations.size()) {
408 SkipStart = Limit / 2 + Limit % 2;
409 SkipEnd = ActiveTemplateInstantiations.size() - Limit / 2;
410 }
411
Douglas Gregorcca9e962009-07-01 22:01:06 +0000412 // FIXME: In all of these cases, we need to show the template arguments
Douglas Gregor575cf372010-04-20 07:18:24 +0000413 unsigned InstantiationIdx = 0;
Douglas Gregoree1828a2009-03-10 18:03:33 +0000414 for (llvm::SmallVector<ActiveTemplateInstantiation, 16>::reverse_iterator
415 Active = ActiveTemplateInstantiations.rbegin(),
416 ActiveEnd = ActiveTemplateInstantiations.rend();
417 Active != ActiveEnd;
Douglas Gregor575cf372010-04-20 07:18:24 +0000418 ++Active, ++InstantiationIdx) {
419 // Skip this instantiation?
420 if (InstantiationIdx >= SkipStart && InstantiationIdx < SkipEnd) {
421 if (InstantiationIdx == SkipStart) {
422 // Note that we're skipping instantiations.
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000423 Diags.Report(Active->PointOfInstantiation,
Douglas Gregor575cf372010-04-20 07:18:24 +0000424 diag::note_instantiation_contexts_suppressed)
425 << unsigned(ActiveTemplateInstantiations.size() - Limit);
426 }
427 continue;
428 }
429
Douglas Gregordf667e72009-03-10 20:44:00 +0000430 switch (Active->Kind) {
431 case ActiveTemplateInstantiation::TemplateInstantiation: {
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000432 Decl *D = reinterpret_cast<Decl *>(Active->Entity);
433 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) {
434 unsigned DiagID = diag::note_template_member_class_here;
435 if (isa<ClassTemplateSpecializationDecl>(Record))
436 DiagID = diag::note_template_class_instantiation_here;
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000437 Diags.Report(Active->PointOfInstantiation, DiagID)
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000438 << Context.getTypeDeclType(Record)
439 << Active->InstantiationRange;
Douglas Gregor7caa6822009-07-24 20:34:43 +0000440 } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
Douglas Gregor1637be72009-06-26 00:10:03 +0000441 unsigned DiagID;
442 if (Function->getPrimaryTemplate())
443 DiagID = diag::note_function_template_spec_here;
444 else
445 DiagID = diag::note_template_member_function_here;
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000446 Diags.Report(Active->PointOfInstantiation, DiagID)
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000447 << Function
448 << Active->InstantiationRange;
Douglas Gregor7caa6822009-07-24 20:34:43 +0000449 } else {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000450 Diags.Report(Active->PointOfInstantiation,
Douglas Gregor7caa6822009-07-24 20:34:43 +0000451 diag::note_template_static_data_member_def_here)
452 << cast<VarDecl>(D)
453 << Active->InstantiationRange;
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000454 }
Douglas Gregordf667e72009-03-10 20:44:00 +0000455 break;
456 }
457
458 case ActiveTemplateInstantiation::DefaultTemplateArgumentInstantiation: {
459 TemplateDecl *Template = cast<TemplateDecl>((Decl *)Active->Entity);
460 std::string TemplateArgsStr
Douglas Gregor7532dc62009-03-30 22:58:21 +0000461 = TemplateSpecializationType::PrintTemplateArgumentList(
Mike Stump1eb44332009-09-09 15:08:12 +0000462 Active->TemplateArgs,
Douglas Gregord249e1d1f2009-05-29 20:38:28 +0000463 Active->NumTemplateArgs,
464 Context.PrintingPolicy);
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000465 Diags.Report(Active->PointOfInstantiation,
Douglas Gregordf667e72009-03-10 20:44:00 +0000466 diag::note_default_arg_instantiation_here)
467 << (Template->getNameAsString() + TemplateArgsStr)
468 << Active->InstantiationRange;
469 break;
470 }
Douglas Gregor637a4092009-06-10 23:47:09 +0000471
Douglas Gregorcca9e962009-07-01 22:01:06 +0000472 case ActiveTemplateInstantiation::ExplicitTemplateArgumentSubstitution: {
Mike Stump1eb44332009-09-09 15:08:12 +0000473 FunctionTemplateDecl *FnTmpl
Douglas Gregorcca9e962009-07-01 22:01:06 +0000474 = cast<FunctionTemplateDecl>((Decl *)Active->Entity);
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000475 Diags.Report(Active->PointOfInstantiation,
Douglas Gregorcca9e962009-07-01 22:01:06 +0000476 diag::note_explicit_template_arg_substitution_here)
Douglas Gregor5e402912010-03-30 20:35:20 +0000477 << FnTmpl
478 << getTemplateArgumentBindingsText(FnTmpl->getTemplateParameters(),
479 Active->TemplateArgs,
480 Active->NumTemplateArgs)
481 << Active->InstantiationRange;
Douglas Gregor637a4092009-06-10 23:47:09 +0000482 break;
483 }
Mike Stump1eb44332009-09-09 15:08:12 +0000484
Douglas Gregorcca9e962009-07-01 22:01:06 +0000485 case ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution:
486 if (ClassTemplatePartialSpecializationDecl *PartialSpec
487 = dyn_cast<ClassTemplatePartialSpecializationDecl>(
488 (Decl *)Active->Entity)) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000489 Diags.Report(Active->PointOfInstantiation,
Douglas Gregorcca9e962009-07-01 22:01:06 +0000490 diag::note_partial_spec_deduct_instantiation_here)
491 << Context.getTypeDeclType(PartialSpec)
Douglas Gregor5e402912010-03-30 20:35:20 +0000492 << getTemplateArgumentBindingsText(
493 PartialSpec->getTemplateParameters(),
494 Active->TemplateArgs,
495 Active->NumTemplateArgs)
Douglas Gregorcca9e962009-07-01 22:01:06 +0000496 << Active->InstantiationRange;
497 } else {
498 FunctionTemplateDecl *FnTmpl
499 = cast<FunctionTemplateDecl>((Decl *)Active->Entity);
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000500 Diags.Report(Active->PointOfInstantiation,
Douglas Gregorcca9e962009-07-01 22:01:06 +0000501 diag::note_function_template_deduction_instantiation_here)
Douglas Gregor5e402912010-03-30 20:35:20 +0000502 << FnTmpl
503 << getTemplateArgumentBindingsText(FnTmpl->getTemplateParameters(),
504 Active->TemplateArgs,
505 Active->NumTemplateArgs)
506 << Active->InstantiationRange;
Douglas Gregorcca9e962009-07-01 22:01:06 +0000507 }
508 break;
Douglas Gregor637a4092009-06-10 23:47:09 +0000509
Anders Carlsson25cae7f2009-09-05 05:14:19 +0000510 case ActiveTemplateInstantiation::DefaultFunctionArgumentInstantiation: {
511 ParmVarDecl *Param = cast<ParmVarDecl>((Decl *)Active->Entity);
512 FunctionDecl *FD = cast<FunctionDecl>(Param->getDeclContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000513
Anders Carlsson25cae7f2009-09-05 05:14:19 +0000514 std::string TemplateArgsStr
515 = TemplateSpecializationType::PrintTemplateArgumentList(
Mike Stump1eb44332009-09-09 15:08:12 +0000516 Active->TemplateArgs,
Anders Carlsson25cae7f2009-09-05 05:14:19 +0000517 Active->NumTemplateArgs,
518 Context.PrintingPolicy);
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000519 Diags.Report(Active->PointOfInstantiation,
Anders Carlsson25cae7f2009-09-05 05:14:19 +0000520 diag::note_default_function_arg_instantiation_here)
Anders Carlsson6bc107b2009-09-05 05:38:54 +0000521 << (FD->getNameAsString() + TemplateArgsStr)
Anders Carlsson25cae7f2009-09-05 05:14:19 +0000522 << Active->InstantiationRange;
523 break;
524 }
Mike Stump1eb44332009-09-09 15:08:12 +0000525
Douglas Gregor9148c3f2009-11-11 19:13:48 +0000526 case ActiveTemplateInstantiation::PriorTemplateArgumentSubstitution: {
527 NamedDecl *Parm = cast<NamedDecl>((Decl *)Active->Entity);
528 std::string Name;
529 if (!Parm->getName().empty())
530 Name = std::string(" '") + Parm->getName().str() + "'";
Douglas Gregor54c53cc2011-01-04 23:35:54 +0000531
532 TemplateParameterList *TemplateParams = 0;
533 if (TemplateDecl *Template = dyn_cast<TemplateDecl>(Active->Template))
534 TemplateParams = Template->getTemplateParameters();
535 else
536 TemplateParams =
537 cast<ClassTemplatePartialSpecializationDecl>(Active->Template)
538 ->getTemplateParameters();
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000539 Diags.Report(Active->PointOfInstantiation,
Douglas Gregor9148c3f2009-11-11 19:13:48 +0000540 diag::note_prior_template_arg_substitution)
541 << isa<TemplateTemplateParmDecl>(Parm)
542 << Name
Douglas Gregor54c53cc2011-01-04 23:35:54 +0000543 << getTemplateArgumentBindingsText(TemplateParams,
Douglas Gregor9148c3f2009-11-11 19:13:48 +0000544 Active->TemplateArgs,
545 Active->NumTemplateArgs)
546 << Active->InstantiationRange;
547 break;
548 }
Douglas Gregorf35f8282009-11-11 21:54:23 +0000549
550 case ActiveTemplateInstantiation::DefaultTemplateArgumentChecking: {
Douglas Gregor54c53cc2011-01-04 23:35:54 +0000551 TemplateParameterList *TemplateParams = 0;
552 if (TemplateDecl *Template = dyn_cast<TemplateDecl>(Active->Template))
553 TemplateParams = Template->getTemplateParameters();
554 else
555 TemplateParams =
556 cast<ClassTemplatePartialSpecializationDecl>(Active->Template)
557 ->getTemplateParameters();
558
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000559 Diags.Report(Active->PointOfInstantiation,
Douglas Gregorf35f8282009-11-11 21:54:23 +0000560 diag::note_template_default_arg_checking)
Douglas Gregor54c53cc2011-01-04 23:35:54 +0000561 << getTemplateArgumentBindingsText(TemplateParams,
Douglas Gregorf35f8282009-11-11 21:54:23 +0000562 Active->TemplateArgs,
563 Active->NumTemplateArgs)
564 << Active->InstantiationRange;
565 break;
566 }
Douglas Gregordf667e72009-03-10 20:44:00 +0000567 }
Douglas Gregoree1828a2009-03-10 18:03:33 +0000568 }
569}
570
Douglas Gregor1eee5dc2011-01-27 22:31:44 +0000571llvm::Optional<TemplateDeductionInfo *> Sema::isSFINAEContext() const {
Douglas Gregor5e9f35c2009-06-14 07:33:30 +0000572 using llvm::SmallVector;
Douglas Gregor1eee5dc2011-01-27 22:31:44 +0000573 if (InNonInstantiationSFINAEContext)
574 return llvm::Optional<TemplateDeductionInfo *>(0);
575
Douglas Gregor5e9f35c2009-06-14 07:33:30 +0000576 for (SmallVector<ActiveTemplateInstantiation, 16>::const_reverse_iterator
577 Active = ActiveTemplateInstantiations.rbegin(),
578 ActiveEnd = ActiveTemplateInstantiations.rend();
579 Active != ActiveEnd;
Douglas Gregorf35f8282009-11-11 21:54:23 +0000580 ++Active)
581 {
Douglas Gregor5e9f35c2009-06-14 07:33:30 +0000582 switch(Active->Kind) {
Anders Carlsson25cae7f2009-09-05 05:14:19 +0000583 case ActiveTemplateInstantiation::DefaultFunctionArgumentInstantiation:
Douglas Gregor1eee5dc2011-01-27 22:31:44 +0000584 case ActiveTemplateInstantiation::TemplateInstantiation:
Douglas Gregorcca9e962009-07-01 22:01:06 +0000585 // This is a template instantiation, so there is no SFINAE.
Douglas Gregor1eee5dc2011-01-27 22:31:44 +0000586 return llvm::Optional<TemplateDeductionInfo *>();
Mike Stump1eb44332009-09-09 15:08:12 +0000587
Douglas Gregor5e9f35c2009-06-14 07:33:30 +0000588 case ActiveTemplateInstantiation::DefaultTemplateArgumentInstantiation:
Douglas Gregor9148c3f2009-11-11 19:13:48 +0000589 case ActiveTemplateInstantiation::PriorTemplateArgumentSubstitution:
Douglas Gregorf35f8282009-11-11 21:54:23 +0000590 case ActiveTemplateInstantiation::DefaultTemplateArgumentChecking:
Douglas Gregor9148c3f2009-11-11 19:13:48 +0000591 // A default template argument instantiation and substitution into
592 // template parameters with arguments for prior parameters may or may
593 // not be a SFINAE context; look further up the stack.
Douglas Gregor5e9f35c2009-06-14 07:33:30 +0000594 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000595
Douglas Gregorcca9e962009-07-01 22:01:06 +0000596 case ActiveTemplateInstantiation::ExplicitTemplateArgumentSubstitution:
597 case ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution:
598 // We're either substitution explicitly-specified template arguments
599 // or deduced template arguments, so SFINAE applies.
Douglas Gregor9b623632010-10-12 23:32:35 +0000600 assert(Active->DeductionInfo && "Missing deduction info pointer");
601 return Active->DeductionInfo;
Douglas Gregor5e9f35c2009-06-14 07:33:30 +0000602 }
603 }
604
Douglas Gregor1eee5dc2011-01-27 22:31:44 +0000605 return llvm::Optional<TemplateDeductionInfo *>();
Douglas Gregor5e9f35c2009-06-14 07:33:30 +0000606}
607
Douglas Gregord3731192011-01-10 07:32:04 +0000608/// \brief Retrieve the depth and index of a parameter pack.
609static std::pair<unsigned, unsigned>
610getDepthAndIndex(NamedDecl *ND) {
611 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(ND))
612 return std::make_pair(TTP->getDepth(), TTP->getIndex());
613
614 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(ND))
615 return std::make_pair(NTTP->getDepth(), NTTP->getIndex());
616
617 TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(ND);
618 return std::make_pair(TTP->getDepth(), TTP->getIndex());
619}
620
Douglas Gregor99ebf652009-02-27 19:31:52 +0000621//===----------------------------------------------------------------------===/
622// Template Instantiation for Types
623//===----------------------------------------------------------------------===/
Douglas Gregorcd281c32009-02-28 00:25:32 +0000624namespace {
Douglas Gregor895162d2010-04-30 18:55:50 +0000625 class TemplateInstantiator : public TreeTransform<TemplateInstantiator> {
Douglas Gregord6350ae2009-08-28 20:31:08 +0000626 const MultiLevelTemplateArgumentList &TemplateArgs;
Douglas Gregorcd281c32009-02-28 00:25:32 +0000627 SourceLocation Loc;
628 DeclarationName Entity;
Douglas Gregor99ebf652009-02-27 19:31:52 +0000629
Douglas Gregorcd281c32009-02-28 00:25:32 +0000630 public:
Douglas Gregor43959a92009-08-20 07:17:43 +0000631 typedef TreeTransform<TemplateInstantiator> inherited;
Mike Stump1eb44332009-09-09 15:08:12 +0000632
633 TemplateInstantiator(Sema &SemaRef,
Douglas Gregord6350ae2009-08-28 20:31:08 +0000634 const MultiLevelTemplateArgumentList &TemplateArgs,
Douglas Gregor577f75a2009-08-04 16:50:30 +0000635 SourceLocation Loc,
Mike Stump1eb44332009-09-09 15:08:12 +0000636 DeclarationName Entity)
637 : inherited(SemaRef), TemplateArgs(TemplateArgs), Loc(Loc),
Douglas Gregor43959a92009-08-20 07:17:43 +0000638 Entity(Entity) { }
Douglas Gregorcd281c32009-02-28 00:25:32 +0000639
Mike Stump1eb44332009-09-09 15:08:12 +0000640 /// \brief Determine whether the given type \p T has already been
Douglas Gregor577f75a2009-08-04 16:50:30 +0000641 /// transformed.
642 ///
643 /// For the purposes of template instantiation, a type has already been
644 /// transformed if it is NULL or if it is not dependent.
Douglas Gregorb4eeaff2010-05-07 23:12:07 +0000645 bool AlreadyTransformed(QualType T);
Mike Stump1eb44332009-09-09 15:08:12 +0000646
Douglas Gregor577f75a2009-08-04 16:50:30 +0000647 /// \brief Returns the location of the entity being instantiated, if known.
648 SourceLocation getBaseLocation() { return Loc; }
Mike Stump1eb44332009-09-09 15:08:12 +0000649
Douglas Gregor577f75a2009-08-04 16:50:30 +0000650 /// \brief Returns the name of the entity being instantiated, if any.
651 DeclarationName getBaseEntity() { return Entity; }
Mike Stump1eb44332009-09-09 15:08:12 +0000652
Douglas Gregor972e6ce2009-10-27 06:26:26 +0000653 /// \brief Sets the "base" location and entity when that
654 /// information is known based on another transformation.
655 void setBase(SourceLocation Loc, DeclarationName Entity) {
656 this->Loc = Loc;
657 this->Entity = Entity;
658 }
Douglas Gregor8491ffe2010-12-20 22:05:00 +0000659
660 bool TryExpandParameterPacks(SourceLocation EllipsisLoc,
661 SourceRange PatternRange,
662 const UnexpandedParameterPack *Unexpanded,
663 unsigned NumUnexpanded,
664 bool &ShouldExpand,
Douglas Gregord3731192011-01-10 07:32:04 +0000665 bool &RetainExpansion,
Douglas Gregorcded4f62011-01-14 17:04:44 +0000666 llvm::Optional<unsigned> &NumExpansions) {
Douglas Gregorb99268b2010-12-21 00:52:54 +0000667 return getSema().CheckParameterPacksForExpansion(EllipsisLoc,
668 PatternRange, Unexpanded,
669 NumUnexpanded,
670 TemplateArgs,
671 ShouldExpand,
Douglas Gregord3731192011-01-10 07:32:04 +0000672 RetainExpansion,
Douglas Gregorb99268b2010-12-21 00:52:54 +0000673 NumExpansions);
674 }
Douglas Gregor8491ffe2010-12-20 22:05:00 +0000675
Douglas Gregor12c9c002011-01-07 16:43:16 +0000676 void ExpandingFunctionParameterPack(ParmVarDecl *Pack) {
677 SemaRef.CurrentInstantiationScope->MakeInstantiatedLocalArgPack(Pack);
678 }
679
Douglas Gregord3731192011-01-10 07:32:04 +0000680 TemplateArgument ForgetPartiallySubstitutedPack() {
681 TemplateArgument Result;
682 if (NamedDecl *PartialPack
683 = SemaRef.CurrentInstantiationScope->getPartiallySubstitutedPack()){
684 MultiLevelTemplateArgumentList &TemplateArgs
685 = const_cast<MultiLevelTemplateArgumentList &>(this->TemplateArgs);
686 unsigned Depth, Index;
687 llvm::tie(Depth, Index) = getDepthAndIndex(PartialPack);
688 if (TemplateArgs.hasTemplateArgument(Depth, Index)) {
689 Result = TemplateArgs(Depth, Index);
690 TemplateArgs.setArgument(Depth, Index, TemplateArgument());
691 }
692 }
693
694 return Result;
695 }
696
697 void RememberPartiallySubstitutedPack(TemplateArgument Arg) {
698 if (Arg.isNull())
699 return;
700
701 if (NamedDecl *PartialPack
702 = SemaRef.CurrentInstantiationScope->getPartiallySubstitutedPack()){
703 MultiLevelTemplateArgumentList &TemplateArgs
704 = const_cast<MultiLevelTemplateArgumentList &>(this->TemplateArgs);
705 unsigned Depth, Index;
706 llvm::tie(Depth, Index) = getDepthAndIndex(PartialPack);
707 TemplateArgs.setArgument(Depth, Index, Arg);
708 }
709 }
710
Douglas Gregor577f75a2009-08-04 16:50:30 +0000711 /// \brief Transform the given declaration by instantiating a reference to
712 /// this declaration.
Douglas Gregor7c1e98f2010-03-01 15:56:25 +0000713 Decl *TransformDecl(SourceLocation Loc, Decl *D);
Douglas Gregorb98b1992009-08-11 05:31:07 +0000714
Mike Stump1eb44332009-09-09 15:08:12 +0000715 /// \brief Transform the definition of the given declaration by
Douglas Gregor43959a92009-08-20 07:17:43 +0000716 /// instantiating it.
Douglas Gregoraac571c2010-03-01 17:25:41 +0000717 Decl *TransformDefinition(SourceLocation Loc, Decl *D);
Mike Stump1eb44332009-09-09 15:08:12 +0000718
Douglas Gregor6cd21982009-10-20 05:58:46 +0000719 /// \bried Transform the first qualifier within a scope by instantiating the
720 /// declaration.
721 NamedDecl *TransformFirstQualifierInScope(NamedDecl *D, SourceLocation Loc);
722
Douglas Gregor43959a92009-08-20 07:17:43 +0000723 /// \brief Rebuild the exception declaration and register the declaration
724 /// as an instantiated local.
Douglas Gregor83cb9422010-09-09 17:09:21 +0000725 VarDecl *RebuildExceptionDecl(VarDecl *ExceptionDecl,
John McCalla93c9342009-12-07 02:54:59 +0000726 TypeSourceInfo *Declarator,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +0000727 SourceLocation StartLoc,
728 SourceLocation NameLoc,
729 IdentifierInfo *Name);
Mike Stump1eb44332009-09-09 15:08:12 +0000730
Douglas Gregorbe270a02010-04-26 17:57:08 +0000731 /// \brief Rebuild the Objective-C exception declaration and register the
732 /// declaration as an instantiated local.
733 VarDecl *RebuildObjCExceptionDecl(VarDecl *ExceptionDecl,
734 TypeSourceInfo *TSInfo, QualType T);
735
John McCallc4e70192009-09-11 04:59:25 +0000736 /// \brief Check for tag mismatches when instantiating an
737 /// elaborated type.
John McCall21e413f2010-11-04 19:04:38 +0000738 QualType RebuildElaboratedType(SourceLocation KeywordLoc,
739 ElaboratedTypeKeyword Keyword,
Douglas Gregor9e876872011-03-01 18:12:44 +0000740 NestedNameSpecifierLoc QualifierLoc,
741 QualType T);
John McCallc4e70192009-09-11 04:59:25 +0000742
Douglas Gregorfd4ffeb2011-03-02 18:07:45 +0000743 TemplateName TransformTemplateName(CXXScopeSpec &SS,
744 TemplateName Name,
745 SourceLocation NameLoc,
746 QualType ObjectType = QualType(),
747 NamedDecl *FirstQualifierInScope = 0);
748
John McCall60d7b3a2010-08-24 06:29:42 +0000749 ExprResult TransformPredefinedExpr(PredefinedExpr *E);
750 ExprResult TransformDeclRefExpr(DeclRefExpr *E);
751 ExprResult TransformCXXDefaultArgExpr(CXXDefaultArgExpr *E);
752 ExprResult TransformTemplateParmRefExpr(DeclRefExpr *E,
Douglas Gregor56bc9832010-12-24 00:15:10 +0000753 NonTypeTemplateParmDecl *D);
Douglas Gregorc7793c72011-01-15 01:15:58 +0000754 ExprResult TransformSubstNonTypeTemplateParmPackExpr(
755 SubstNonTypeTemplateParmPackExpr *E);
756
Douglas Gregor895162d2010-04-30 18:55:50 +0000757 QualType TransformFunctionProtoType(TypeLocBuilder &TLB,
John McCall43fed0d2010-11-12 08:19:04 +0000758 FunctionProtoTypeLoc TL);
Douglas Gregor6a24bfd2011-01-14 22:40:04 +0000759 ParmVarDecl *TransformFunctionTypeParam(ParmVarDecl *OldParm,
760 llvm::Optional<unsigned> NumExpansions);
John McCall21ef0fa2010-03-11 09:03:00 +0000761
Mike Stump1eb44332009-09-09 15:08:12 +0000762 /// \brief Transforms a template type parameter type by performing
Douglas Gregor577f75a2009-08-04 16:50:30 +0000763 /// substitution of the corresponding template type argument.
John McCalla2becad2009-10-21 00:40:46 +0000764 QualType TransformTemplateTypeParmType(TypeLocBuilder &TLB,
John McCall43fed0d2010-11-12 08:19:04 +0000765 TemplateTypeParmTypeLoc TL);
Nick Lewycky03d98c52010-07-06 19:51:49 +0000766
Douglas Gregorc3069d62011-01-14 02:55:32 +0000767 /// \brief Transforms an already-substituted template type parameter pack
768 /// into either itself (if we aren't substituting into its pack expansion)
769 /// or the appropriate substituted argument.
770 QualType TransformSubstTemplateTypeParmPackType(TypeLocBuilder &TLB,
771 SubstTemplateTypeParmPackTypeLoc TL);
772
John McCall60d7b3a2010-08-24 06:29:42 +0000773 ExprResult TransformCallExpr(CallExpr *CE) {
Nick Lewycky03d98c52010-07-06 19:51:49 +0000774 getSema().CallsUndergoingInstantiation.push_back(CE);
John McCall60d7b3a2010-08-24 06:29:42 +0000775 ExprResult Result =
Nick Lewycky03d98c52010-07-06 19:51:49 +0000776 TreeTransform<TemplateInstantiator>::TransformCallExpr(CE);
777 getSema().CallsUndergoingInstantiation.pop_back();
778 return move(Result);
779 }
Douglas Gregor577f75a2009-08-04 16:50:30 +0000780 };
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000781}
782
Douglas Gregorb4eeaff2010-05-07 23:12:07 +0000783bool TemplateInstantiator::AlreadyTransformed(QualType T) {
784 if (T.isNull())
785 return true;
786
Douglas Gregor836adf62010-05-24 17:22:01 +0000787 if (T->isDependentType() || T->isVariablyModifiedType())
Douglas Gregorb4eeaff2010-05-07 23:12:07 +0000788 return false;
789
790 getSema().MarkDeclarationsReferencedInType(Loc, T);
791 return true;
792}
793
Douglas Gregor7c1e98f2010-03-01 15:56:25 +0000794Decl *TemplateInstantiator::TransformDecl(SourceLocation Loc, Decl *D) {
Douglas Gregorc68afe22009-09-03 21:38:09 +0000795 if (!D)
796 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000797
Douglas Gregorc68afe22009-09-03 21:38:09 +0000798 if (TemplateTemplateParmDecl *TTP = dyn_cast<TemplateTemplateParmDecl>(D)) {
Douglas Gregord6350ae2009-08-28 20:31:08 +0000799 if (TTP->getDepth() < TemplateArgs.getNumLevels()) {
Douglas Gregor6d3e6272010-02-05 19:54:12 +0000800 // If the corresponding template argument is NULL or non-existent, it's
801 // because we are performing instantiation from explicitly-specified
802 // template arguments in a function template, but there were some
803 // arguments left unspecified.
804 if (!TemplateArgs.hasTemplateArgument(TTP->getDepth(),
805 TTP->getPosition()))
806 return D;
807
Douglas Gregor61c4d282011-01-05 15:48:55 +0000808 TemplateArgument Arg = TemplateArgs(TTP->getDepth(), TTP->getPosition());
809
810 if (TTP->isParameterPack()) {
811 assert(Arg.getKind() == TemplateArgument::Pack &&
812 "Missing argument pack");
813
Douglas Gregor1aee05d2011-01-15 06:45:20 +0000814 assert(getSema().ArgumentPackSubstitutionIndex >= 0);
Douglas Gregord3731192011-01-10 07:32:04 +0000815 assert(getSema().ArgumentPackSubstitutionIndex < (int)Arg.pack_size());
Douglas Gregor61c4d282011-01-05 15:48:55 +0000816 Arg = Arg.pack_begin()[getSema().ArgumentPackSubstitutionIndex];
817 }
818
819 TemplateName Template = Arg.getAsTemplate();
Douglas Gregor788cd062009-11-11 01:00:40 +0000820 assert(!Template.isNull() && Template.getAsTemplateDecl() &&
Douglas Gregord6350ae2009-08-28 20:31:08 +0000821 "Wrong kind of template template argument");
Douglas Gregor788cd062009-11-11 01:00:40 +0000822 return Template.getAsTemplateDecl();
Douglas Gregord6350ae2009-08-28 20:31:08 +0000823 }
Mike Stump1eb44332009-09-09 15:08:12 +0000824
Douglas Gregor788cd062009-11-11 01:00:40 +0000825 // Fall through to find the instantiated declaration for this template
826 // template parameter.
Douglas Gregord1067e52009-08-06 06:41:21 +0000827 }
Mike Stump1eb44332009-09-09 15:08:12 +0000828
Douglas Gregor7c1e98f2010-03-01 15:56:25 +0000829 return SemaRef.FindInstantiatedDecl(Loc, cast<NamedDecl>(D), TemplateArgs);
Douglas Gregor577f75a2009-08-04 16:50:30 +0000830}
831
Douglas Gregoraac571c2010-03-01 17:25:41 +0000832Decl *TemplateInstantiator::TransformDefinition(SourceLocation Loc, Decl *D) {
John McCallce3ff2b2009-08-25 22:02:44 +0000833 Decl *Inst = getSema().SubstDecl(D, getSema().CurContext, TemplateArgs);
Douglas Gregor43959a92009-08-20 07:17:43 +0000834 if (!Inst)
835 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000836
Douglas Gregor43959a92009-08-20 07:17:43 +0000837 getSema().CurrentInstantiationScope->InstantiatedLocal(D, Inst);
838 return Inst;
839}
840
Douglas Gregor6cd21982009-10-20 05:58:46 +0000841NamedDecl *
842TemplateInstantiator::TransformFirstQualifierInScope(NamedDecl *D,
843 SourceLocation Loc) {
844 // If the first part of the nested-name-specifier was a template type
845 // parameter, instantiate that type parameter down to a tag type.
846 if (TemplateTypeParmDecl *TTPD = dyn_cast_or_null<TemplateTypeParmDecl>(D)) {
847 const TemplateTypeParmType *TTP
848 = cast<TemplateTypeParmType>(getSema().Context.getTypeDeclType(TTPD));
Douglas Gregor984a58b2010-12-20 22:48:17 +0000849
Douglas Gregor6cd21982009-10-20 05:58:46 +0000850 if (TTP->getDepth() < TemplateArgs.getNumLevels()) {
Douglas Gregor984a58b2010-12-20 22:48:17 +0000851 // FIXME: This needs testing w/ member access expressions.
852 TemplateArgument Arg = TemplateArgs(TTP->getDepth(), TTP->getIndex());
853
854 if (TTP->isParameterPack()) {
855 assert(Arg.getKind() == TemplateArgument::Pack &&
856 "Missing argument pack");
857
Douglas Gregor2be29f42011-01-14 23:41:42 +0000858 if (getSema().ArgumentPackSubstitutionIndex == -1)
Douglas Gregor984a58b2010-12-20 22:48:17 +0000859 return 0;
Douglas Gregor984a58b2010-12-20 22:48:17 +0000860
Douglas Gregord3731192011-01-10 07:32:04 +0000861 assert(getSema().ArgumentPackSubstitutionIndex < (int)Arg.pack_size());
Douglas Gregor984a58b2010-12-20 22:48:17 +0000862 Arg = Arg.pack_begin()[getSema().ArgumentPackSubstitutionIndex];
863 }
864
865 QualType T = Arg.getAsType();
Douglas Gregor6cd21982009-10-20 05:58:46 +0000866 if (T.isNull())
Douglas Gregor7c1e98f2010-03-01 15:56:25 +0000867 return cast_or_null<NamedDecl>(TransformDecl(Loc, D));
Douglas Gregor6cd21982009-10-20 05:58:46 +0000868
869 if (const TagType *Tag = T->getAs<TagType>())
870 return Tag->getDecl();
871
872 // The resulting type is not a tag; complain.
873 getSema().Diag(Loc, diag::err_nested_name_spec_non_tag) << T;
874 return 0;
875 }
876 }
877
Douglas Gregor7c1e98f2010-03-01 15:56:25 +0000878 return cast_or_null<NamedDecl>(TransformDecl(Loc, D));
Douglas Gregor6cd21982009-10-20 05:58:46 +0000879}
880
Douglas Gregor43959a92009-08-20 07:17:43 +0000881VarDecl *
882TemplateInstantiator::RebuildExceptionDecl(VarDecl *ExceptionDecl,
John McCalla93c9342009-12-07 02:54:59 +0000883 TypeSourceInfo *Declarator,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +0000884 SourceLocation StartLoc,
885 SourceLocation NameLoc,
886 IdentifierInfo *Name) {
Douglas Gregor83cb9422010-09-09 17:09:21 +0000887 VarDecl *Var = inherited::RebuildExceptionDecl(ExceptionDecl, Declarator,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +0000888 StartLoc, NameLoc, Name);
Douglas Gregorbe270a02010-04-26 17:57:08 +0000889 if (Var)
890 getSema().CurrentInstantiationScope->InstantiatedLocal(ExceptionDecl, Var);
891 return Var;
892}
893
894VarDecl *TemplateInstantiator::RebuildObjCExceptionDecl(VarDecl *ExceptionDecl,
895 TypeSourceInfo *TSInfo,
896 QualType T) {
897 VarDecl *Var = inherited::RebuildObjCExceptionDecl(ExceptionDecl, TSInfo, T);
898 if (Var)
Douglas Gregor43959a92009-08-20 07:17:43 +0000899 getSema().CurrentInstantiationScope->InstantiatedLocal(ExceptionDecl, Var);
900 return Var;
901}
902
John McCallc4e70192009-09-11 04:59:25 +0000903QualType
John McCall21e413f2010-11-04 19:04:38 +0000904TemplateInstantiator::RebuildElaboratedType(SourceLocation KeywordLoc,
905 ElaboratedTypeKeyword Keyword,
Douglas Gregor9e876872011-03-01 18:12:44 +0000906 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000907 QualType T) {
John McCallc4e70192009-09-11 04:59:25 +0000908 if (const TagType *TT = T->getAs<TagType>()) {
909 TagDecl* TD = TT->getDecl();
910
John McCall21e413f2010-11-04 19:04:38 +0000911 SourceLocation TagLocation = KeywordLoc;
John McCallc4e70192009-09-11 04:59:25 +0000912
913 // FIXME: type might be anonymous.
914 IdentifierInfo *Id = TD->getIdentifier();
915
916 // TODO: should we even warn on struct/class mismatches for this? Seems
917 // like it's likely to produce a lot of spurious errors.
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000918 if (Keyword != ETK_None && Keyword != ETK_Typename) {
919 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForKeyword(Keyword);
920 if (!SemaRef.isAcceptableTagRedeclaration(TD, Kind, TagLocation, *Id)) {
921 SemaRef.Diag(TagLocation, diag::err_use_with_wrong_tag)
922 << Id
923 << FixItHint::CreateReplacement(SourceRange(TagLocation),
924 TD->getKindName());
925 SemaRef.Diag(TD->getLocation(), diag::note_previous_use);
926 }
John McCallc4e70192009-09-11 04:59:25 +0000927 }
928 }
929
John McCall21e413f2010-11-04 19:04:38 +0000930 return TreeTransform<TemplateInstantiator>::RebuildElaboratedType(KeywordLoc,
931 Keyword,
Douglas Gregor9e876872011-03-01 18:12:44 +0000932 QualifierLoc,
933 T);
John McCallc4e70192009-09-11 04:59:25 +0000934}
935
Douglas Gregorfd4ffeb2011-03-02 18:07:45 +0000936TemplateName TemplateInstantiator::TransformTemplateName(CXXScopeSpec &SS,
937 TemplateName Name,
938 SourceLocation NameLoc,
939 QualType ObjectType,
940 NamedDecl *FirstQualifierInScope) {
941 if (TemplateTemplateParmDecl *TTP
942 = dyn_cast_or_null<TemplateTemplateParmDecl>(Name.getAsTemplateDecl())) {
943 if (TTP->getDepth() < TemplateArgs.getNumLevels()) {
944 // If the corresponding template argument is NULL or non-existent, it's
945 // because we are performing instantiation from explicitly-specified
946 // template arguments in a function template, but there were some
947 // arguments left unspecified.
948 if (!TemplateArgs.hasTemplateArgument(TTP->getDepth(),
949 TTP->getPosition()))
950 return Name;
951
952 TemplateArgument Arg = TemplateArgs(TTP->getDepth(), TTP->getPosition());
953
954 if (TTP->isParameterPack()) {
955 assert(Arg.getKind() == TemplateArgument::Pack &&
956 "Missing argument pack");
957
958 if (getSema().ArgumentPackSubstitutionIndex == -1) {
959 // We have the template argument pack to substitute, but we're not
960 // actually expanding the enclosing pack expansion yet. So, just
961 // keep the entire argument pack.
962 return getSema().Context.getSubstTemplateTemplateParmPack(TTP, Arg);
963 }
964
965 assert(getSema().ArgumentPackSubstitutionIndex < (int)Arg.pack_size());
966 Arg = Arg.pack_begin()[getSema().ArgumentPackSubstitutionIndex];
967 }
968
969 TemplateName Template = Arg.getAsTemplate();
970 assert(!Template.isNull() && Template.getAsTemplateDecl() &&
971 "Wrong kind of template template argument");
Douglas Gregor58750382011-03-05 20:06:51 +0000972
973 // We don't ever want to substitute for a qualified template name, since
974 // the qualifier is handled separately. So, look through the qualified
975 // template name to its underlying declaration.
976 if (QualifiedTemplateName *QTN = Template.getAsQualifiedTemplateName())
977 Template = TemplateName(QTN->getTemplateDecl());
978
Douglas Gregorfd4ffeb2011-03-02 18:07:45 +0000979 return Template;
980 }
981 }
982
983 if (SubstTemplateTemplateParmPackStorage *SubstPack
984 = Name.getAsSubstTemplateTemplateParmPack()) {
985 if (getSema().ArgumentPackSubstitutionIndex == -1)
986 return Name;
987
988 const TemplateArgument &ArgPack = SubstPack->getArgumentPack();
989 assert(getSema().ArgumentPackSubstitutionIndex < (int)ArgPack.pack_size() &&
990 "Pack substitution index out-of-range");
991 return ArgPack.pack_begin()[getSema().ArgumentPackSubstitutionIndex]
992 .getAsTemplate();
993 }
994
995 return inherited::TransformTemplateName(SS, Name, NameLoc, ObjectType,
996 FirstQualifierInScope);
997}
998
John McCall60d7b3a2010-08-24 06:29:42 +0000999ExprResult
John McCall454feb92009-12-08 09:21:05 +00001000TemplateInstantiator::TransformPredefinedExpr(PredefinedExpr *E) {
Anders Carlsson773f3972009-09-11 01:22:35 +00001001 if (!E->isTypeDependent())
John McCall3fa5cae2010-10-26 07:05:15 +00001002 return SemaRef.Owned(E);
Anders Carlsson773f3972009-09-11 01:22:35 +00001003
1004 FunctionDecl *currentDecl = getSema().getCurFunctionDecl();
1005 assert(currentDecl && "Must have current function declaration when "
1006 "instantiating.");
1007
1008 PredefinedExpr::IdentType IT = E->getIdentType();
1009
Anders Carlsson848fa642010-02-11 18:20:28 +00001010 unsigned Length = PredefinedExpr::ComputeName(IT, currentDecl).length();
Anders Carlsson773f3972009-09-11 01:22:35 +00001011
1012 llvm::APInt LengthI(32, Length + 1);
John McCall0953e762009-09-24 19:53:00 +00001013 QualType ResTy = getSema().Context.CharTy.withConst();
Anders Carlsson773f3972009-09-11 01:22:35 +00001014 ResTy = getSema().Context.getConstantArrayType(ResTy, LengthI,
1015 ArrayType::Normal, 0);
1016 PredefinedExpr *PE =
1017 new (getSema().Context) PredefinedExpr(E->getLocation(), ResTy, IT);
1018 return getSema().Owned(PE);
1019}
1020
John McCall60d7b3a2010-08-24 06:29:42 +00001021ExprResult
John McCallb8fc0532010-02-06 08:42:39 +00001022TemplateInstantiator::TransformTemplateParmRefExpr(DeclRefExpr *E,
Douglas Gregordcee9802010-02-08 23:41:45 +00001023 NonTypeTemplateParmDecl *NTTP) {
John McCallb8fc0532010-02-06 08:42:39 +00001024 // If the corresponding template argument is NULL or non-existent, it's
1025 // because we are performing instantiation from explicitly-specified
1026 // template arguments in a function template, but there were some
1027 // arguments left unspecified.
1028 if (!TemplateArgs.hasTemplateArgument(NTTP->getDepth(),
1029 NTTP->getPosition()))
John McCall3fa5cae2010-10-26 07:05:15 +00001030 return SemaRef.Owned(E);
Mike Stump1eb44332009-09-09 15:08:12 +00001031
Douglas Gregor56bc9832010-12-24 00:15:10 +00001032 TemplateArgument Arg = TemplateArgs(NTTP->getDepth(), NTTP->getPosition());
1033 if (NTTP->isParameterPack()) {
1034 assert(Arg.getKind() == TemplateArgument::Pack &&
1035 "Missing argument pack");
1036
1037 if (getSema().ArgumentPackSubstitutionIndex == -1) {
Douglas Gregorc7793c72011-01-15 01:15:58 +00001038 // We have an argument pack, but we can't select a particular argument
1039 // out of it yet. Therefore, we'll build an expression to hold on to that
1040 // argument pack.
1041 QualType TargetType = SemaRef.SubstType(NTTP->getType(), TemplateArgs,
1042 E->getLocation(),
1043 NTTP->getDeclName());
1044 if (TargetType.isNull())
1045 return ExprError();
1046
1047 return new (SemaRef.Context) SubstNonTypeTemplateParmPackExpr(TargetType,
1048 NTTP,
1049 E->getLocation(),
1050 Arg);
Douglas Gregor56bc9832010-12-24 00:15:10 +00001051 }
1052
Douglas Gregord3731192011-01-10 07:32:04 +00001053 assert(getSema().ArgumentPackSubstitutionIndex < (int)Arg.pack_size());
Douglas Gregor56bc9832010-12-24 00:15:10 +00001054 Arg = Arg.pack_begin()[getSema().ArgumentPackSubstitutionIndex];
1055 }
Mike Stump1eb44332009-09-09 15:08:12 +00001056
John McCallb8fc0532010-02-06 08:42:39 +00001057 // The template argument itself might be an expression, in which
1058 // case we just return that expression.
1059 if (Arg.getKind() == TemplateArgument::Expression)
John McCall3fa5cae2010-10-26 07:05:15 +00001060 return SemaRef.Owned(Arg.getAsExpr());
Mike Stump1eb44332009-09-09 15:08:12 +00001061
John McCallb8fc0532010-02-06 08:42:39 +00001062 if (Arg.getKind() == TemplateArgument::Declaration) {
1063 ValueDecl *VD = cast<ValueDecl>(Arg.getAsDecl());
Mike Stump1eb44332009-09-09 15:08:12 +00001064
John McCall645cf442010-02-06 10:23:53 +00001065 // Find the instantiation of the template argument. This is
1066 // required for nested templates.
John McCallb8fc0532010-02-06 08:42:39 +00001067 VD = cast_or_null<ValueDecl>(
Douglas Gregor7c1e98f2010-03-01 15:56:25 +00001068 getSema().FindInstantiatedDecl(E->getLocation(),
1069 VD, TemplateArgs));
John McCallb8fc0532010-02-06 08:42:39 +00001070 if (!VD)
John McCallf312b1e2010-08-26 23:41:50 +00001071 return ExprError();
Mike Stump1eb44332009-09-09 15:08:12 +00001072
John McCall645cf442010-02-06 10:23:53 +00001073 // Derive the type we want the substituted decl to have. This had
1074 // better be non-dependent, or these checks will have serious problems.
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001075 QualType TargetType;
1076 if (NTTP->isExpandedParameterPack())
1077 TargetType = NTTP->getExpansionType(
1078 getSema().ArgumentPackSubstitutionIndex);
1079 else if (NTTP->isParameterPack() &&
1080 isa<PackExpansionType>(NTTP->getType())) {
1081 TargetType = SemaRef.SubstType(
1082 cast<PackExpansionType>(NTTP->getType())->getPattern(),
1083 TemplateArgs, E->getLocation(),
1084 NTTP->getDeclName());
1085 } else
1086 TargetType = SemaRef.SubstType(NTTP->getType(), TemplateArgs,
1087 E->getLocation(), NTTP->getDeclName());
John McCall645cf442010-02-06 10:23:53 +00001088 assert(!TargetType.isNull() && "type substitution failed for param type");
1089 assert(!TargetType->isDependentType() && "param type still dependent");
Douglas Gregor02024a92010-03-28 02:42:43 +00001090 return SemaRef.BuildExpressionFromDeclTemplateArgument(Arg,
1091 TargetType,
1092 E->getLocation());
John McCallb8fc0532010-02-06 08:42:39 +00001093 }
1094
Douglas Gregor02024a92010-03-28 02:42:43 +00001095 return SemaRef.BuildExpressionFromIntegralTemplateArgument(Arg,
1096 E->getSourceRange().getBegin());
John McCallb8fc0532010-02-06 08:42:39 +00001097}
1098
Douglas Gregorc7793c72011-01-15 01:15:58 +00001099ExprResult
1100TemplateInstantiator::TransformSubstNonTypeTemplateParmPackExpr(
1101 SubstNonTypeTemplateParmPackExpr *E) {
1102 if (getSema().ArgumentPackSubstitutionIndex == -1) {
1103 // We aren't expanding the parameter pack, so just return ourselves.
1104 return getSema().Owned(E);
1105 }
1106
Douglas Gregorc7793c72011-01-15 01:15:58 +00001107 const TemplateArgument &ArgPack = E->getArgumentPack();
1108 unsigned Index = (unsigned)getSema().ArgumentPackSubstitutionIndex;
1109 assert(Index < ArgPack.pack_size() && "Substitution index out-of-range");
1110
1111 const TemplateArgument &Arg = ArgPack.pack_begin()[Index];
1112 if (Arg.getKind() == TemplateArgument::Expression)
1113 return SemaRef.Owned(Arg.getAsExpr());
1114
1115 if (Arg.getKind() == TemplateArgument::Declaration) {
1116 ValueDecl *VD = cast<ValueDecl>(Arg.getAsDecl());
1117
1118 // Find the instantiation of the template argument. This is
1119 // required for nested templates.
1120 VD = cast_or_null<ValueDecl>(
1121 getSema().FindInstantiatedDecl(E->getParameterPackLocation(),
1122 VD, TemplateArgs));
1123 if (!VD)
1124 return ExprError();
1125
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001126 QualType T;
1127 NonTypeTemplateParmDecl *NTTP = E->getParameterPack();
1128 if (NTTP->isExpandedParameterPack())
1129 T = NTTP->getExpansionType(getSema().ArgumentPackSubstitutionIndex);
1130 else if (const PackExpansionType *Expansion
1131 = dyn_cast<PackExpansionType>(NTTP->getType()))
1132 T = SemaRef.SubstType(Expansion->getPattern(), TemplateArgs,
1133 E->getParameterPackLocation(), NTTP->getDeclName());
1134 else
1135 T = E->getType();
1136 return SemaRef.BuildExpressionFromDeclTemplateArgument(Arg, T,
Douglas Gregorc7793c72011-01-15 01:15:58 +00001137 E->getParameterPackLocation());
1138 }
1139
1140 return SemaRef.BuildExpressionFromIntegralTemplateArgument(Arg,
1141 E->getParameterPackLocation());
1142}
John McCallb8fc0532010-02-06 08:42:39 +00001143
John McCall60d7b3a2010-08-24 06:29:42 +00001144ExprResult
John McCallb8fc0532010-02-06 08:42:39 +00001145TemplateInstantiator::TransformDeclRefExpr(DeclRefExpr *E) {
1146 NamedDecl *D = E->getDecl();
1147 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(D)) {
1148 if (NTTP->getDepth() < TemplateArgs.getNumLevels())
1149 return TransformTemplateParmRefExpr(E, NTTP);
Douglas Gregor550d9b22009-10-31 17:21:17 +00001150
1151 // We have a non-type template parameter that isn't fully substituted;
1152 // FindInstantiatedDecl will find it in the local instantiation scope.
Douglas Gregorb98b1992009-08-11 05:31:07 +00001153 }
Mike Stump1eb44332009-09-09 15:08:12 +00001154
John McCall454feb92009-12-08 09:21:05 +00001155 return TreeTransform<TemplateInstantiator>::TransformDeclRefExpr(E);
Douglas Gregorb98b1992009-08-11 05:31:07 +00001156}
1157
John McCall60d7b3a2010-08-24 06:29:42 +00001158ExprResult TemplateInstantiator::TransformCXXDefaultArgExpr(
John McCall454feb92009-12-08 09:21:05 +00001159 CXXDefaultArgExpr *E) {
Sebastian Redla29e51b2009-11-08 13:56:19 +00001160 assert(!cast<FunctionDecl>(E->getParam()->getDeclContext())->
1161 getDescribedFunctionTemplate() &&
1162 "Default arg expressions are never formed in dependent cases.");
Douglas Gregor036aed12009-12-23 23:03:06 +00001163 return SemaRef.BuildCXXDefaultArgExpr(E->getUsedLocation(),
1164 cast<FunctionDecl>(E->getParam()->getDeclContext()),
1165 E->getParam());
Sebastian Redla29e51b2009-11-08 13:56:19 +00001166}
1167
Douglas Gregor895162d2010-04-30 18:55:50 +00001168QualType TemplateInstantiator::TransformFunctionProtoType(TypeLocBuilder &TLB,
John McCall43fed0d2010-11-12 08:19:04 +00001169 FunctionProtoTypeLoc TL) {
Douglas Gregor895162d2010-04-30 18:55:50 +00001170 // We need a local instantiation scope for this function prototype.
John McCall2a7fb272010-08-25 05:32:35 +00001171 LocalInstantiationScope Scope(SemaRef, /*CombineWithOuterScope=*/true);
John McCall43fed0d2010-11-12 08:19:04 +00001172 return inherited::TransformFunctionProtoType(TLB, TL);
John McCall21ef0fa2010-03-11 09:03:00 +00001173}
1174
1175ParmVarDecl *
Douglas Gregor6a24bfd2011-01-14 22:40:04 +00001176TemplateInstantiator::TransformFunctionTypeParam(ParmVarDecl *OldParm,
1177 llvm::Optional<unsigned> NumExpansions) {
1178 return SemaRef.SubstParmVarDecl(OldParm, TemplateArgs,
1179 NumExpansions);
John McCall21ef0fa2010-03-11 09:03:00 +00001180}
1181
Mike Stump1eb44332009-09-09 15:08:12 +00001182QualType
John McCalla2becad2009-10-21 00:40:46 +00001183TemplateInstantiator::TransformTemplateTypeParmType(TypeLocBuilder &TLB,
John McCall43fed0d2010-11-12 08:19:04 +00001184 TemplateTypeParmTypeLoc TL) {
John McCallf4c73712011-01-19 06:33:43 +00001185 const TemplateTypeParmType *T = TL.getTypePtr();
Douglas Gregord6350ae2009-08-28 20:31:08 +00001186 if (T->getDepth() < TemplateArgs.getNumLevels()) {
Douglas Gregor99ebf652009-02-27 19:31:52 +00001187 // Replace the template type parameter with its corresponding
1188 // template argument.
Mike Stump1eb44332009-09-09 15:08:12 +00001189
1190 // If the corresponding template argument is NULL or doesn't exist, it's
1191 // because we are performing instantiation from explicitly-specified
1192 // template arguments in a function template class, but there were some
Douglas Gregor16134c62009-07-01 00:28:38 +00001193 // arguments left unspecified.
John McCalla2becad2009-10-21 00:40:46 +00001194 if (!TemplateArgs.hasTemplateArgument(T->getDepth(), T->getIndex())) {
1195 TemplateTypeParmTypeLoc NewTL
1196 = TLB.push<TemplateTypeParmTypeLoc>(TL.getType());
1197 NewTL.setNameLoc(TL.getNameLoc());
1198 return TL.getType();
1199 }
Mike Stump1eb44332009-09-09 15:08:12 +00001200
Douglas Gregor8491ffe2010-12-20 22:05:00 +00001201 TemplateArgument Arg = TemplateArgs(T->getDepth(), T->getIndex());
1202
1203 if (T->isParameterPack()) {
1204 assert(Arg.getKind() == TemplateArgument::Pack &&
1205 "Missing argument pack");
1206
1207 if (getSema().ArgumentPackSubstitutionIndex == -1) {
Douglas Gregorc3069d62011-01-14 02:55:32 +00001208 // We have the template argument pack, but we're not expanding the
1209 // enclosing pack expansion yet. Just save the template argument
1210 // pack for later substitution.
1211 QualType Result
1212 = getSema().Context.getSubstTemplateTypeParmPackType(T, Arg);
1213 SubstTemplateTypeParmPackTypeLoc NewTL
1214 = TLB.push<SubstTemplateTypeParmPackTypeLoc>(Result);
1215 NewTL.setNameLoc(TL.getNameLoc());
1216 return Result;
Douglas Gregor8491ffe2010-12-20 22:05:00 +00001217 }
1218
Douglas Gregord3731192011-01-10 07:32:04 +00001219 assert(getSema().ArgumentPackSubstitutionIndex < (int)Arg.pack_size());
Douglas Gregor8491ffe2010-12-20 22:05:00 +00001220 Arg = Arg.pack_begin()[getSema().ArgumentPackSubstitutionIndex];
1221 }
1222
1223 assert(Arg.getKind() == TemplateArgument::Type &&
Douglas Gregor99ebf652009-02-27 19:31:52 +00001224 "Template argument kind mismatch");
Douglas Gregord6350ae2009-08-28 20:31:08 +00001225
Douglas Gregor8491ffe2010-12-20 22:05:00 +00001226 QualType Replacement = Arg.getAsType();
John McCall49a832b2009-10-18 09:09:24 +00001227
1228 // TODO: only do this uniquing once, at the start of instantiation.
John McCalla2becad2009-10-21 00:40:46 +00001229 QualType Result
1230 = getSema().Context.getSubstTemplateTypeParmType(T, Replacement);
1231 SubstTemplateTypeParmTypeLoc NewTL
1232 = TLB.push<SubstTemplateTypeParmTypeLoc>(Result);
1233 NewTL.setNameLoc(TL.getNameLoc());
1234 return Result;
Mike Stump1eb44332009-09-09 15:08:12 +00001235 }
Douglas Gregor99ebf652009-02-27 19:31:52 +00001236
1237 // The template type parameter comes from an inner template (e.g.,
1238 // the template parameter list of a member template inside the
1239 // template we are instantiating). Create a new template type
1240 // parameter with the template "level" reduced by one.
John McCalla2becad2009-10-21 00:40:46 +00001241 QualType Result
1242 = getSema().Context.getTemplateTypeParmType(T->getDepth()
1243 - TemplateArgs.getNumLevels(),
1244 T->getIndex(),
1245 T->isParameterPack(),
Douglas Gregorefed5c82010-06-16 15:23:05 +00001246 T->getName());
John McCalla2becad2009-10-21 00:40:46 +00001247 TemplateTypeParmTypeLoc NewTL = TLB.push<TemplateTypeParmTypeLoc>(Result);
1248 NewTL.setNameLoc(TL.getNameLoc());
1249 return Result;
Douglas Gregorcd281c32009-02-28 00:25:32 +00001250}
Douglas Gregor99ebf652009-02-27 19:31:52 +00001251
Douglas Gregorc3069d62011-01-14 02:55:32 +00001252QualType
1253TemplateInstantiator::TransformSubstTemplateTypeParmPackType(
1254 TypeLocBuilder &TLB,
1255 SubstTemplateTypeParmPackTypeLoc TL) {
1256 if (getSema().ArgumentPackSubstitutionIndex == -1) {
1257 // We aren't expanding the parameter pack, so just return ourselves.
1258 SubstTemplateTypeParmPackTypeLoc NewTL
1259 = TLB.push<SubstTemplateTypeParmPackTypeLoc>(TL.getType());
1260 NewTL.setNameLoc(TL.getNameLoc());
1261 return TL.getType();
1262 }
1263
1264 const TemplateArgument &ArgPack = TL.getTypePtr()->getArgumentPack();
1265 unsigned Index = (unsigned)getSema().ArgumentPackSubstitutionIndex;
1266 assert(Index < ArgPack.pack_size() && "Substitution index out-of-range");
1267
1268 QualType Result = ArgPack.pack_begin()[Index].getAsType();
1269 Result = getSema().Context.getSubstTemplateTypeParmType(
1270 TL.getTypePtr()->getReplacedParameter(),
1271 Result);
1272 SubstTemplateTypeParmTypeLoc NewTL
1273 = TLB.push<SubstTemplateTypeParmTypeLoc>(Result);
1274 NewTL.setNameLoc(TL.getNameLoc());
1275 return Result;
1276}
1277
John McCallce3ff2b2009-08-25 22:02:44 +00001278/// \brief Perform substitution on the type T with a given set of template
1279/// arguments.
Douglas Gregor99ebf652009-02-27 19:31:52 +00001280///
1281/// This routine substitutes the given template arguments into the
1282/// type T and produces the instantiated type.
1283///
1284/// \param T the type into which the template arguments will be
1285/// substituted. If this type is not dependent, it will be returned
1286/// immediately.
1287///
1288/// \param TemplateArgs the template arguments that will be
1289/// substituted for the top-level template parameters within T.
1290///
Douglas Gregor99ebf652009-02-27 19:31:52 +00001291/// \param Loc the location in the source code where this substitution
1292/// is being performed. It will typically be the location of the
1293/// declarator (if we're instantiating the type of some declaration)
1294/// or the location of the type in the source code (if, e.g., we're
1295/// instantiating the type of a cast expression).
1296///
1297/// \param Entity the name of the entity associated with a declaration
1298/// being instantiated (if any). May be empty to indicate that there
1299/// is no such entity (if, e.g., this is a type that occurs as part of
1300/// a cast expression) or that the entity has no name (e.g., an
1301/// unnamed function parameter).
1302///
1303/// \returns If the instantiation succeeds, the instantiated
1304/// type. Otherwise, produces diagnostics and returns a NULL type.
John McCalla93c9342009-12-07 02:54:59 +00001305TypeSourceInfo *Sema::SubstType(TypeSourceInfo *T,
John McCallcd7ba1c2009-10-21 00:58:09 +00001306 const MultiLevelTemplateArgumentList &Args,
1307 SourceLocation Loc,
1308 DeclarationName Entity) {
1309 assert(!ActiveTemplateInstantiations.empty() &&
1310 "Cannot perform an instantiation without some context on the "
1311 "instantiation stack");
1312
Douglas Gregor836adf62010-05-24 17:22:01 +00001313 if (!T->getType()->isDependentType() &&
1314 !T->getType()->isVariablyModifiedType())
John McCallcd7ba1c2009-10-21 00:58:09 +00001315 return T;
1316
1317 TemplateInstantiator Instantiator(*this, Args, Loc, Entity);
1318 return Instantiator.TransformType(T);
1319}
1320
Douglas Gregor603cfb42011-01-05 23:12:31 +00001321TypeSourceInfo *Sema::SubstType(TypeLoc TL,
1322 const MultiLevelTemplateArgumentList &Args,
1323 SourceLocation Loc,
1324 DeclarationName Entity) {
1325 assert(!ActiveTemplateInstantiations.empty() &&
1326 "Cannot perform an instantiation without some context on the "
1327 "instantiation stack");
1328
1329 if (TL.getType().isNull())
1330 return 0;
1331
1332 if (!TL.getType()->isDependentType() &&
1333 !TL.getType()->isVariablyModifiedType()) {
1334 // FIXME: Make a copy of the TypeLoc data here, so that we can
1335 // return a new TypeSourceInfo. Inefficient!
1336 TypeLocBuilder TLB;
1337 TLB.pushFullCopy(TL);
1338 return TLB.getTypeSourceInfo(Context, TL.getType());
1339 }
1340
1341 TemplateInstantiator Instantiator(*this, Args, Loc, Entity);
1342 TypeLocBuilder TLB;
1343 TLB.reserve(TL.getFullDataSize());
1344 QualType Result = Instantiator.TransformType(TLB, TL);
1345 if (Result.isNull())
1346 return 0;
1347
1348 return TLB.getTypeSourceInfo(Context, Result);
1349}
1350
John McCallcd7ba1c2009-10-21 00:58:09 +00001351/// Deprecated form of the above.
Mike Stump1eb44332009-09-09 15:08:12 +00001352QualType Sema::SubstType(QualType T,
Douglas Gregord6350ae2009-08-28 20:31:08 +00001353 const MultiLevelTemplateArgumentList &TemplateArgs,
John McCallce3ff2b2009-08-25 22:02:44 +00001354 SourceLocation Loc, DeclarationName Entity) {
Douglas Gregordf667e72009-03-10 20:44:00 +00001355 assert(!ActiveTemplateInstantiations.empty() &&
1356 "Cannot perform an instantiation without some context on the "
1357 "instantiation stack");
1358
Douglas Gregor836adf62010-05-24 17:22:01 +00001359 // If T is not a dependent type or a variably-modified type, there
1360 // is nothing to do.
1361 if (!T->isDependentType() && !T->isVariablyModifiedType())
Douglas Gregor99ebf652009-02-27 19:31:52 +00001362 return T;
1363
Douglas Gregor577f75a2009-08-04 16:50:30 +00001364 TemplateInstantiator Instantiator(*this, TemplateArgs, Loc, Entity);
1365 return Instantiator.TransformType(T);
Douglas Gregor99ebf652009-02-27 19:31:52 +00001366}
Douglas Gregor2943aed2009-03-03 04:44:36 +00001367
John McCall6cd3b9f2010-04-09 17:38:44 +00001368static bool NeedsInstantiationAsFunctionType(TypeSourceInfo *T) {
Douglas Gregor836adf62010-05-24 17:22:01 +00001369 if (T->getType()->isDependentType() || T->getType()->isVariablyModifiedType())
John McCall6cd3b9f2010-04-09 17:38:44 +00001370 return true;
1371
Abramo Bagnara723df242010-12-14 22:11:44 +00001372 TypeLoc TL = T->getTypeLoc().IgnoreParens();
John McCall6cd3b9f2010-04-09 17:38:44 +00001373 if (!isa<FunctionProtoTypeLoc>(TL))
1374 return false;
1375
1376 FunctionProtoTypeLoc FP = cast<FunctionProtoTypeLoc>(TL);
1377 for (unsigned I = 0, E = FP.getNumArgs(); I != E; ++I) {
1378 ParmVarDecl *P = FP.getArg(I);
1379
1380 // TODO: currently we always rebuild expressions. When we
1381 // properly get lazier about this, we should use the same
1382 // logic to avoid rebuilding prototypes here.
Douglas Gregor7b1cf302011-01-05 21:14:17 +00001383 if (P->hasDefaultArg())
John McCall6cd3b9f2010-04-09 17:38:44 +00001384 return true;
1385 }
1386
1387 return false;
1388}
1389
1390/// A form of SubstType intended specifically for instantiating the
1391/// type of a FunctionDecl. Its purpose is solely to force the
1392/// instantiation of default-argument expressions.
1393TypeSourceInfo *Sema::SubstFunctionDeclType(TypeSourceInfo *T,
1394 const MultiLevelTemplateArgumentList &Args,
1395 SourceLocation Loc,
1396 DeclarationName Entity) {
1397 assert(!ActiveTemplateInstantiations.empty() &&
1398 "Cannot perform an instantiation without some context on the "
1399 "instantiation stack");
1400
1401 if (!NeedsInstantiationAsFunctionType(T))
1402 return T;
1403
1404 TemplateInstantiator Instantiator(*this, Args, Loc, Entity);
1405
1406 TypeLocBuilder TLB;
1407
1408 TypeLoc TL = T->getTypeLoc();
1409 TLB.reserve(TL.getFullDataSize());
1410
John McCall43fed0d2010-11-12 08:19:04 +00001411 QualType Result = Instantiator.TransformType(TLB, TL);
John McCall6cd3b9f2010-04-09 17:38:44 +00001412 if (Result.isNull())
1413 return 0;
1414
1415 return TLB.getTypeSourceInfo(Context, Result);
1416}
1417
Douglas Gregorcb27b0f2010-04-12 07:48:19 +00001418ParmVarDecl *Sema::SubstParmVarDecl(ParmVarDecl *OldParm,
Douglas Gregor6a24bfd2011-01-14 22:40:04 +00001419 const MultiLevelTemplateArgumentList &TemplateArgs,
1420 llvm::Optional<unsigned> NumExpansions) {
Douglas Gregorcb27b0f2010-04-12 07:48:19 +00001421 TypeSourceInfo *OldDI = OldParm->getTypeSourceInfo();
Douglas Gregor603cfb42011-01-05 23:12:31 +00001422 TypeSourceInfo *NewDI = 0;
1423
Douglas Gregor603cfb42011-01-05 23:12:31 +00001424 TypeLoc OldTL = OldDI->getTypeLoc();
1425 if (isa<PackExpansionTypeLoc>(OldTL)) {
1426 PackExpansionTypeLoc ExpansionTL = cast<PackExpansionTypeLoc>(OldTL);
Douglas Gregor603cfb42011-01-05 23:12:31 +00001427
1428 // We have a function parameter pack. Substitute into the pattern of the
1429 // expansion.
1430 NewDI = SubstType(ExpansionTL.getPatternLoc(), TemplateArgs,
1431 OldParm->getLocation(), OldParm->getDeclName());
1432 if (!NewDI)
1433 return 0;
1434
1435 if (NewDI->getType()->containsUnexpandedParameterPack()) {
1436 // We still have unexpanded parameter packs, which means that
1437 // our function parameter is still a function parameter pack.
1438 // Therefore, make its type a pack expansion type.
Douglas Gregorcded4f62011-01-14 17:04:44 +00001439 NewDI = CheckPackExpansion(NewDI, ExpansionTL.getEllipsisLoc(),
Douglas Gregor6a24bfd2011-01-14 22:40:04 +00001440 NumExpansions);
Douglas Gregor603cfb42011-01-05 23:12:31 +00001441 }
1442 } else {
1443 NewDI = SubstType(OldDI, TemplateArgs, OldParm->getLocation(),
1444 OldParm->getDeclName());
1445 }
1446
Douglas Gregorcb27b0f2010-04-12 07:48:19 +00001447 if (!NewDI)
1448 return 0;
1449
1450 if (NewDI->getType()->isVoidType()) {
1451 Diag(OldParm->getLocation(), diag::err_param_with_void_type);
1452 return 0;
1453 }
1454
1455 ParmVarDecl *NewParm = CheckParameter(Context.getTranslationUnitDecl(),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001456 OldParm->getInnerLocStart(),
Douglas Gregorcb27b0f2010-04-12 07:48:19 +00001457 OldParm->getLocation(),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001458 OldParm->getIdentifier(),
1459 NewDI->getType(), NewDI,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001460 OldParm->getStorageClass(),
1461 OldParm->getStorageClassAsWritten());
Douglas Gregorcb27b0f2010-04-12 07:48:19 +00001462 if (!NewParm)
1463 return 0;
Douglas Gregor4469e8a2010-05-19 17:02:24 +00001464
Douglas Gregorcb27b0f2010-04-12 07:48:19 +00001465 // Mark the (new) default argument as uninstantiated (if any).
1466 if (OldParm->hasUninstantiatedDefaultArg()) {
1467 Expr *Arg = OldParm->getUninstantiatedDefaultArg();
1468 NewParm->setUninstantiatedDefaultArg(Arg);
Douglas Gregor8cfb7a32010-10-12 18:23:32 +00001469 } else if (OldParm->hasUnparsedDefaultArg()) {
1470 NewParm->setUnparsedDefaultArg();
1471 UnparsedDefaultArgInstantiations[OldParm].push_back(NewParm);
Douglas Gregorcb27b0f2010-04-12 07:48:19 +00001472 } else if (Expr *Arg = OldParm->getDefaultArg())
1473 NewParm->setUninstantiatedDefaultArg(Arg);
1474
1475 NewParm->setHasInheritedDefaultArg(OldParm->hasInheritedDefaultArg());
1476
Douglas Gregor603cfb42011-01-05 23:12:31 +00001477 // FIXME: When OldParm is a parameter pack and NewParm is not a parameter
1478 // pack, we actually have a set of instantiated locations. Maintain this set!
Douglas Gregor12c9c002011-01-07 16:43:16 +00001479 if (OldParm->isParameterPack() && !NewParm->isParameterPack()) {
1480 // Add the new parameter to
1481 CurrentInstantiationScope->InstantiatedLocalPackArg(OldParm, NewParm);
1482 } else {
1483 // Introduce an Old -> New mapping
Douglas Gregor603cfb42011-01-05 23:12:31 +00001484 CurrentInstantiationScope->InstantiatedLocal(OldParm, NewParm);
Douglas Gregor12c9c002011-01-07 16:43:16 +00001485 }
Douglas Gregor603cfb42011-01-05 23:12:31 +00001486
Argyrios Kyrtzidise3041be2010-07-19 10:14:41 +00001487 // FIXME: OldParm may come from a FunctionProtoType, in which case CurContext
1488 // can be anything, is this right ?
Fariborz Jahanian55a17c02010-07-13 21:05:02 +00001489 NewParm->setDeclContext(CurContext);
Fariborz Jahaniane7ffbe22010-07-13 20:05:58 +00001490
Douglas Gregorcb27b0f2010-04-12 07:48:19 +00001491 return NewParm;
1492}
1493
Douglas Gregora009b592011-01-07 00:20:55 +00001494/// \brief Substitute the given template arguments into the given set of
1495/// parameters, producing the set of parameter types that would be generated
1496/// from such a substitution.
1497bool Sema::SubstParmTypes(SourceLocation Loc,
1498 ParmVarDecl **Params, unsigned NumParams,
1499 const MultiLevelTemplateArgumentList &TemplateArgs,
Douglas Gregor12c9c002011-01-07 16:43:16 +00001500 llvm::SmallVectorImpl<QualType> &ParamTypes,
1501 llvm::SmallVectorImpl<ParmVarDecl *> *OutParams) {
Douglas Gregora009b592011-01-07 00:20:55 +00001502 assert(!ActiveTemplateInstantiations.empty() &&
1503 "Cannot perform an instantiation without some context on the "
1504 "instantiation stack");
1505
1506 TemplateInstantiator Instantiator(*this, TemplateArgs, Loc,
1507 DeclarationName());
1508 return Instantiator.TransformFunctionTypeParams(Loc, Params, NumParams, 0,
Douglas Gregor12c9c002011-01-07 16:43:16 +00001509 ParamTypes, OutParams);
Douglas Gregora009b592011-01-07 00:20:55 +00001510}
1511
John McCallce3ff2b2009-08-25 22:02:44 +00001512/// \brief Perform substitution on the base class specifiers of the
1513/// given class template specialization.
Douglas Gregor2943aed2009-03-03 04:44:36 +00001514///
1515/// Produces a diagnostic and returns true on error, returns false and
1516/// attaches the instantiated base classes to the class template
1517/// specialization if successful.
Mike Stump1eb44332009-09-09 15:08:12 +00001518bool
John McCallce3ff2b2009-08-25 22:02:44 +00001519Sema::SubstBaseSpecifiers(CXXRecordDecl *Instantiation,
1520 CXXRecordDecl *Pattern,
Douglas Gregord6350ae2009-08-28 20:31:08 +00001521 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor2943aed2009-03-03 04:44:36 +00001522 bool Invalid = false;
Douglas Gregor0ca20ac2009-05-29 18:27:38 +00001523 llvm::SmallVector<CXXBaseSpecifier*, 4> InstantiatedBases;
Mike Stump1eb44332009-09-09 15:08:12 +00001524 for (ClassTemplateSpecializationDecl::base_class_iterator
Douglas Gregord475b8d2009-03-25 21:17:03 +00001525 Base = Pattern->bases_begin(), BaseEnd = Pattern->bases_end();
Douglas Gregor27b152f2009-03-10 18:52:44 +00001526 Base != BaseEnd; ++Base) {
Douglas Gregor2943aed2009-03-03 04:44:36 +00001527 if (!Base->getType()->isDependentType()) {
Fariborz Jahanian71c6e712009-07-22 17:41:53 +00001528 InstantiatedBases.push_back(new (Context) CXXBaseSpecifier(*Base));
Douglas Gregor2943aed2009-03-03 04:44:36 +00001529 continue;
1530 }
1531
Douglas Gregorf90b27a2011-01-03 22:36:02 +00001532 SourceLocation EllipsisLoc;
Douglas Gregor406f98f2011-03-02 02:04:06 +00001533 TypeSourceInfo *BaseTypeLoc;
Douglas Gregorf90b27a2011-01-03 22:36:02 +00001534 if (Base->isPackExpansion()) {
1535 // This is a pack expansion. See whether we should expand it now, or
1536 // wait until later.
1537 llvm::SmallVector<UnexpandedParameterPack, 2> Unexpanded;
1538 collectUnexpandedParameterPacks(Base->getTypeSourceInfo()->getTypeLoc(),
1539 Unexpanded);
1540 bool ShouldExpand = false;
Douglas Gregord3731192011-01-10 07:32:04 +00001541 bool RetainExpansion = false;
Douglas Gregorcded4f62011-01-14 17:04:44 +00001542 llvm::Optional<unsigned> NumExpansions;
Douglas Gregorf90b27a2011-01-03 22:36:02 +00001543 if (CheckParameterPacksForExpansion(Base->getEllipsisLoc(),
1544 Base->getSourceRange(),
1545 Unexpanded.data(), Unexpanded.size(),
1546 TemplateArgs, ShouldExpand,
Douglas Gregord3731192011-01-10 07:32:04 +00001547 RetainExpansion,
Douglas Gregorf90b27a2011-01-03 22:36:02 +00001548 NumExpansions)) {
Douglas Gregorf90b27a2011-01-03 22:36:02 +00001549 Invalid = true;
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00001550 continue;
Douglas Gregorf90b27a2011-01-03 22:36:02 +00001551 }
1552
1553 // If we should expand this pack expansion now, do so.
1554 if (ShouldExpand) {
Douglas Gregorcded4f62011-01-14 17:04:44 +00001555 for (unsigned I = 0; I != *NumExpansions; ++I) {
Douglas Gregorf90b27a2011-01-03 22:36:02 +00001556 Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(*this, I);
1557
1558 TypeSourceInfo *BaseTypeLoc = SubstType(Base->getTypeSourceInfo(),
1559 TemplateArgs,
1560 Base->getSourceRange().getBegin(),
1561 DeclarationName());
1562 if (!BaseTypeLoc) {
1563 Invalid = true;
1564 continue;
1565 }
1566
1567 if (CXXBaseSpecifier *InstantiatedBase
1568 = CheckBaseSpecifier(Instantiation,
1569 Base->getSourceRange(),
1570 Base->isVirtual(),
1571 Base->getAccessSpecifierAsWritten(),
1572 BaseTypeLoc,
1573 SourceLocation()))
1574 InstantiatedBases.push_back(InstantiatedBase);
1575 else
1576 Invalid = true;
1577 }
1578
1579 continue;
1580 }
1581
1582 // The resulting base specifier will (still) be a pack expansion.
1583 EllipsisLoc = Base->getEllipsisLoc();
Douglas Gregor406f98f2011-03-02 02:04:06 +00001584 Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(*this, -1);
1585 BaseTypeLoc = SubstType(Base->getTypeSourceInfo(),
1586 TemplateArgs,
1587 Base->getSourceRange().getBegin(),
1588 DeclarationName());
1589 } else {
1590 BaseTypeLoc = SubstType(Base->getTypeSourceInfo(),
1591 TemplateArgs,
1592 Base->getSourceRange().getBegin(),
1593 DeclarationName());
Douglas Gregorf90b27a2011-01-03 22:36:02 +00001594 }
1595
Nick Lewycky56062202010-07-26 16:56:01 +00001596 if (!BaseTypeLoc) {
Douglas Gregor2943aed2009-03-03 04:44:36 +00001597 Invalid = true;
1598 continue;
1599 }
1600
1601 if (CXXBaseSpecifier *InstantiatedBase
Douglas Gregord475b8d2009-03-25 21:17:03 +00001602 = CheckBaseSpecifier(Instantiation,
Douglas Gregor2943aed2009-03-03 04:44:36 +00001603 Base->getSourceRange(),
1604 Base->isVirtual(),
1605 Base->getAccessSpecifierAsWritten(),
Douglas Gregorf90b27a2011-01-03 22:36:02 +00001606 BaseTypeLoc,
1607 EllipsisLoc))
Douglas Gregor2943aed2009-03-03 04:44:36 +00001608 InstantiatedBases.push_back(InstantiatedBase);
1609 else
1610 Invalid = true;
1611 }
1612
Douglas Gregor27b152f2009-03-10 18:52:44 +00001613 if (!Invalid &&
Jay Foadbeaaccd2009-05-21 09:52:38 +00001614 AttachBaseSpecifiers(Instantiation, InstantiatedBases.data(),
Douglas Gregor2943aed2009-03-03 04:44:36 +00001615 InstantiatedBases.size()))
1616 Invalid = true;
1617
1618 return Invalid;
1619}
1620
Douglas Gregord475b8d2009-03-25 21:17:03 +00001621/// \brief Instantiate the definition of a class from a given pattern.
1622///
1623/// \param PointOfInstantiation The point of instantiation within the
1624/// source code.
1625///
1626/// \param Instantiation is the declaration whose definition is being
1627/// instantiated. This will be either a class template specialization
1628/// or a member class of a class template specialization.
1629///
1630/// \param Pattern is the pattern from which the instantiation
1631/// occurs. This will be either the declaration of a class template or
1632/// the declaration of a member class of a class template.
1633///
1634/// \param TemplateArgs The template arguments to be substituted into
1635/// the pattern.
1636///
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001637/// \param TSK the kind of implicit or explicit instantiation to perform.
Douglas Gregor5842ba92009-08-24 15:23:48 +00001638///
1639/// \param Complain whether to complain if the class cannot be instantiated due
1640/// to the lack of a definition.
1641///
Douglas Gregord475b8d2009-03-25 21:17:03 +00001642/// \returns true if an error occurred, false otherwise.
1643bool
1644Sema::InstantiateClass(SourceLocation PointOfInstantiation,
1645 CXXRecordDecl *Instantiation, CXXRecordDecl *Pattern,
Douglas Gregord6350ae2009-08-28 20:31:08 +00001646 const MultiLevelTemplateArgumentList &TemplateArgs,
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001647 TemplateSpecializationKind TSK,
Douglas Gregor5842ba92009-08-24 15:23:48 +00001648 bool Complain) {
Douglas Gregord475b8d2009-03-25 21:17:03 +00001649 bool Invalid = false;
John McCalle29ba202009-08-20 01:44:21 +00001650
Mike Stump1eb44332009-09-09 15:08:12 +00001651 CXXRecordDecl *PatternDef
Douglas Gregor952b0172010-02-11 01:04:33 +00001652 = cast_or_null<CXXRecordDecl>(Pattern->getDefinition());
Douglas Gregord475b8d2009-03-25 21:17:03 +00001653 if (!PatternDef) {
Douglas Gregor5842ba92009-08-24 15:23:48 +00001654 if (!Complain) {
1655 // Say nothing
1656 } else if (Pattern == Instantiation->getInstantiatedFromMemberClass()) {
Douglas Gregord475b8d2009-03-25 21:17:03 +00001657 Diag(PointOfInstantiation,
1658 diag::err_implicit_instantiate_member_undefined)
1659 << Context.getTypeDeclType(Instantiation);
1660 Diag(Pattern->getLocation(), diag::note_member_of_template_here);
1661 } else {
Douglas Gregor93dfdb12009-05-13 00:25:59 +00001662 Diag(PointOfInstantiation, diag::err_template_instantiate_undefined)
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001663 << (TSK != TSK_ImplicitInstantiation)
Douglas Gregord475b8d2009-03-25 21:17:03 +00001664 << Context.getTypeDeclType(Instantiation);
1665 Diag(Pattern->getLocation(), diag::note_template_decl_here);
1666 }
1667 return true;
1668 }
1669 Pattern = PatternDef;
1670
Douglas Gregor454885e2009-10-15 15:54:05 +00001671 // \brief Record the point of instantiation.
1672 if (MemberSpecializationInfo *MSInfo
1673 = Instantiation->getMemberSpecializationInfo()) {
1674 MSInfo->setTemplateSpecializationKind(TSK);
1675 MSInfo->setPointOfInstantiation(PointOfInstantiation);
Douglas Gregor972e6ce2009-10-27 06:26:26 +00001676 } else if (ClassTemplateSpecializationDecl *Spec
1677 = dyn_cast<ClassTemplateSpecializationDecl>(Instantiation)) {
1678 Spec->setTemplateSpecializationKind(TSK);
1679 Spec->setPointOfInstantiation(PointOfInstantiation);
Douglas Gregor454885e2009-10-15 15:54:05 +00001680 }
1681
Douglas Gregord048bb72009-03-25 21:23:52 +00001682 InstantiatingTemplate Inst(*this, PointOfInstantiation, Instantiation);
Douglas Gregord475b8d2009-03-25 21:17:03 +00001683 if (Inst)
1684 return true;
1685
1686 // Enter the scope of this instantiation. We don't use
1687 // PushDeclContext because we don't have a scope.
John McCallf5813822010-04-29 00:35:03 +00001688 ContextRAII SavedContext(*this, Instantiation);
Douglas Gregor9679caf2010-05-12 17:27:19 +00001689 EnterExpressionEvaluationContext EvalContext(*this,
John McCallf312b1e2010-08-26 23:41:50 +00001690 Sema::PotentiallyEvaluated);
Douglas Gregord475b8d2009-03-25 21:17:03 +00001691
Douglas Gregor05030bb2010-03-24 01:33:17 +00001692 // If this is an instantiation of a local class, merge this local
1693 // instantiation scope with the enclosing scope. Otherwise, every
1694 // instantiation of a class has its own local instantiation scope.
1695 bool MergeWithParentScope = !Instantiation->isDefinedOutsideFunctionOrMethod();
John McCall2a7fb272010-08-25 05:32:35 +00001696 LocalInstantiationScope Scope(*this, MergeWithParentScope);
Douglas Gregor05030bb2010-03-24 01:33:17 +00001697
John McCall1d8d1cc2010-08-01 02:01:53 +00001698 // Pull attributes from the pattern onto the instantiation.
1699 InstantiateAttrs(TemplateArgs, Pattern, Instantiation);
1700
Douglas Gregord475b8d2009-03-25 21:17:03 +00001701 // Start the definition of this instantiation.
1702 Instantiation->startDefinition();
Douglas Gregor13c85772010-05-06 00:28:52 +00001703
1704 Instantiation->setTagKind(Pattern->getTagKind());
Douglas Gregord475b8d2009-03-25 21:17:03 +00001705
John McCallce3ff2b2009-08-25 22:02:44 +00001706 // Do substitution on the base class specifiers.
1707 if (SubstBaseSpecifiers(Instantiation, Pattern, TemplateArgs))
Douglas Gregord475b8d2009-03-25 21:17:03 +00001708 Invalid = true;
1709
Douglas Gregord65587f2010-11-10 19:44:59 +00001710 TemplateDeclInstantiator Instantiator(*this, Instantiation, TemplateArgs);
John McCalld226f652010-08-21 09:40:31 +00001711 llvm::SmallVector<Decl*, 4> Fields;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001712 for (RecordDecl::decl_iterator Member = Pattern->decls_begin(),
Mike Stump1eb44332009-09-09 15:08:12 +00001713 MemberEnd = Pattern->decls_end();
Douglas Gregor6ab35242009-04-09 21:40:53 +00001714 Member != MemberEnd; ++Member) {
Argyrios Kyrtzidisbb5e4312010-11-04 03:18:57 +00001715 // Don't instantiate members not belonging in this semantic context.
1716 // e.g. for:
1717 // @code
1718 // template <int i> class A {
1719 // class B *g;
1720 // };
1721 // @endcode
1722 // 'class B' has the template as lexical context but semantically it is
1723 // introduced in namespace scope.
1724 if ((*Member)->getDeclContext() != Pattern)
1725 continue;
1726
Douglas Gregord65587f2010-11-10 19:44:59 +00001727 if ((*Member)->isInvalidDecl()) {
1728 Invalid = true;
1729 continue;
1730 }
1731
1732 Decl *NewMember = Instantiator.Visit(*Member);
Douglas Gregord475b8d2009-03-25 21:17:03 +00001733 if (NewMember) {
Eli Friedman721e77d2009-12-07 00:22:08 +00001734 if (FieldDecl *Field = dyn_cast<FieldDecl>(NewMember))
John McCalld226f652010-08-21 09:40:31 +00001735 Fields.push_back(Field);
Eli Friedman721e77d2009-12-07 00:22:08 +00001736 else if (NewMember->isInvalidDecl())
1737 Invalid = true;
Douglas Gregord475b8d2009-03-25 21:17:03 +00001738 } else {
1739 // FIXME: Eventually, a NULL return will mean that one of the
Mike Stump390b4cc2009-05-16 07:39:55 +00001740 // instantiations was a semantic disaster, and we'll want to set Invalid =
1741 // true. For now, we expect to skip some members that we can't yet handle.
Douglas Gregord475b8d2009-03-25 21:17:03 +00001742 }
1743 }
1744
1745 // Finish checking fields.
John McCalld226f652010-08-21 09:40:31 +00001746 ActOnFields(0, Instantiation->getLocation(), Instantiation,
Jay Foadbeaaccd2009-05-21 09:52:38 +00001747 Fields.data(), Fields.size(), SourceLocation(), SourceLocation(),
Douglas Gregord475b8d2009-03-25 21:17:03 +00001748 0);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001749 CheckCompletedCXXClass(Instantiation);
Douglas Gregor663b5a02009-10-14 20:14:33 +00001750 if (Instantiation->isInvalidDecl())
1751 Invalid = true;
Douglas Gregord65587f2010-11-10 19:44:59 +00001752 else {
1753 // Instantiate any out-of-line class template partial
1754 // specializations now.
1755 for (TemplateDeclInstantiator::delayed_partial_spec_iterator
1756 P = Instantiator.delayed_partial_spec_begin(),
1757 PEnd = Instantiator.delayed_partial_spec_end();
1758 P != PEnd; ++P) {
1759 if (!Instantiator.InstantiateClassTemplatePartialSpecialization(
1760 P->first,
1761 P->second)) {
1762 Invalid = true;
1763 break;
1764 }
1765 }
1766 }
1767
Douglas Gregord475b8d2009-03-25 21:17:03 +00001768 // Exit the scope of this instantiation.
John McCallf5813822010-04-29 00:35:03 +00001769 SavedContext.pop();
Douglas Gregord475b8d2009-03-25 21:17:03 +00001770
Douglas Gregor6fb745b2010-05-13 16:44:06 +00001771 if (!Invalid) {
Douglas Gregoraba43bb2009-05-26 20:50:29 +00001772 Consumer.HandleTagDeclDefinition(Instantiation);
1773
Douglas Gregor6fb745b2010-05-13 16:44:06 +00001774 // Always emit the vtable for an explicit instantiation definition
1775 // of a polymorphic class template specialization.
1776 if (TSK == TSK_ExplicitInstantiationDefinition)
1777 MarkVTableUsed(PointOfInstantiation, Instantiation, true);
1778 }
1779
Douglas Gregord475b8d2009-03-25 21:17:03 +00001780 return Invalid;
1781}
1782
Douglas Gregor9b623632010-10-12 23:32:35 +00001783namespace {
1784 /// \brief A partial specialization whose template arguments have matched
1785 /// a given template-id.
1786 struct PartialSpecMatchResult {
1787 ClassTemplatePartialSpecializationDecl *Partial;
1788 TemplateArgumentList *Args;
Douglas Gregor9b623632010-10-12 23:32:35 +00001789 };
1790}
1791
Mike Stump1eb44332009-09-09 15:08:12 +00001792bool
Douglas Gregor2943aed2009-03-03 04:44:36 +00001793Sema::InstantiateClassTemplateSpecialization(
Douglas Gregor972e6ce2009-10-27 06:26:26 +00001794 SourceLocation PointOfInstantiation,
Douglas Gregor2943aed2009-03-03 04:44:36 +00001795 ClassTemplateSpecializationDecl *ClassTemplateSpec,
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001796 TemplateSpecializationKind TSK,
Douglas Gregor5842ba92009-08-24 15:23:48 +00001797 bool Complain) {
Douglas Gregor2943aed2009-03-03 04:44:36 +00001798 // Perform the actual instantiation on the canonical declaration.
1799 ClassTemplateSpec = cast<ClassTemplateSpecializationDecl>(
Argyrios Kyrtzidis97fbaa22009-07-18 00:34:25 +00001800 ClassTemplateSpec->getCanonicalDecl());
Douglas Gregor2943aed2009-03-03 04:44:36 +00001801
Douglas Gregor52604ab2009-09-11 21:19:12 +00001802 // Check whether we have already instantiated or specialized this class
1803 // template specialization.
1804 if (ClassTemplateSpec->getSpecializationKind() != TSK_Undeclared) {
1805 if (ClassTemplateSpec->getSpecializationKind() ==
1806 TSK_ExplicitInstantiationDeclaration &&
1807 TSK == TSK_ExplicitInstantiationDefinition) {
1808 // An explicit instantiation definition follows an explicit instantiation
1809 // declaration (C++0x [temp.explicit]p10); go ahead and perform the
1810 // explicit instantiation.
1811 ClassTemplateSpec->setSpecializationKind(TSK);
Douglas Gregor6fb745b2010-05-13 16:44:06 +00001812
1813 // If this is an explicit instantiation definition, mark the
1814 // vtable as used.
1815 if (TSK == TSK_ExplicitInstantiationDefinition)
1816 MarkVTableUsed(PointOfInstantiation, ClassTemplateSpec, true);
1817
Douglas Gregor52604ab2009-09-11 21:19:12 +00001818 return false;
1819 }
1820
1821 // We can only instantiate something that hasn't already been
1822 // instantiated or specialized. Fail without any diagnostics: our
1823 // caller will provide an error message.
Douglas Gregor2943aed2009-03-03 04:44:36 +00001824 return true;
Douglas Gregor52604ab2009-09-11 21:19:12 +00001825 }
Douglas Gregor2943aed2009-03-03 04:44:36 +00001826
Douglas Gregor9eea08b2009-09-15 16:51:42 +00001827 if (ClassTemplateSpec->isInvalidDecl())
1828 return true;
1829
Douglas Gregor2943aed2009-03-03 04:44:36 +00001830 ClassTemplateDecl *Template = ClassTemplateSpec->getSpecializedTemplate();
Douglas Gregord6350ae2009-08-28 20:31:08 +00001831 CXXRecordDecl *Pattern = 0;
Douglas Gregorc8ab2562009-05-31 09:31:02 +00001832
Douglas Gregorc1efb3f2009-06-12 22:31:52 +00001833 // C++ [temp.class.spec.match]p1:
1834 // When a class template is used in a context that requires an
1835 // instantiation of the class, it is necessary to determine
1836 // whether the instantiation is to be generated using the primary
1837 // template or one of the partial specializations. This is done by
1838 // matching the template arguments of the class template
1839 // specialization with the template argument lists of the partial
1840 // specializations.
Douglas Gregor9b623632010-10-12 23:32:35 +00001841 typedef PartialSpecMatchResult MatchResult;
Douglas Gregor199d9912009-06-05 00:53:49 +00001842 llvm::SmallVector<MatchResult, 4> Matched;
Douglas Gregordc60c1e2010-04-30 05:56:50 +00001843 llvm::SmallVector<ClassTemplatePartialSpecializationDecl *, 4> PartialSpecs;
1844 Template->getPartialSpecializations(PartialSpecs);
1845 for (unsigned I = 0, N = PartialSpecs.size(); I != N; ++I) {
1846 ClassTemplatePartialSpecializationDecl *Partial = PartialSpecs[I];
John McCall5769d612010-02-08 23:07:23 +00001847 TemplateDeductionInfo Info(Context, PointOfInstantiation);
Douglas Gregorf67875d2009-06-12 18:26:56 +00001848 if (TemplateDeductionResult Result
Douglas Gregordc60c1e2010-04-30 05:56:50 +00001849 = DeduceTemplateArguments(Partial,
Douglas Gregorf67875d2009-06-12 18:26:56 +00001850 ClassTemplateSpec->getTemplateArgs(),
1851 Info)) {
1852 // FIXME: Store the failed-deduction information for use in
1853 // diagnostics, later.
1854 (void)Result;
1855 } else {
Douglas Gregor9b623632010-10-12 23:32:35 +00001856 Matched.push_back(PartialSpecMatchResult());
1857 Matched.back().Partial = Partial;
1858 Matched.back().Args = Info.take();
Douglas Gregorf67875d2009-06-12 18:26:56 +00001859 }
Douglas Gregorc8ab2562009-05-31 09:31:02 +00001860 }
1861
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001862 // If we're dealing with a member template where the template parameters
1863 // have been instantiated, this provides the original template parameters
1864 // from which the member template's parameters were instantiated.
1865 llvm::SmallVector<const NamedDecl *, 4> InstantiatedTemplateParameters;
1866
Douglas Gregored9c0f92009-10-29 00:04:11 +00001867 if (Matched.size() >= 1) {
Douglas Gregorbf4ea562009-09-15 16:23:51 +00001868 llvm::SmallVector<MatchResult, 4>::iterator Best = Matched.begin();
Douglas Gregored9c0f92009-10-29 00:04:11 +00001869 if (Matched.size() == 1) {
1870 // -- If exactly one matching specialization is found, the
1871 // instantiation is generated from that specialization.
1872 // We don't need to do anything for this.
1873 } else {
1874 // -- If more than one matching specialization is found, the
1875 // partial order rules (14.5.4.2) are used to determine
1876 // whether one of the specializations is more specialized
1877 // than the others. If none of the specializations is more
1878 // specialized than all of the other matching
1879 // specializations, then the use of the class template is
1880 // ambiguous and the program is ill-formed.
1881 for (llvm::SmallVector<MatchResult, 4>::iterator P = Best + 1,
1882 PEnd = Matched.end();
1883 P != PEnd; ++P) {
Douglas Gregor9b623632010-10-12 23:32:35 +00001884 if (getMoreSpecializedPartialSpecialization(P->Partial, Best->Partial,
John McCall5769d612010-02-08 23:07:23 +00001885 PointOfInstantiation)
Douglas Gregor9b623632010-10-12 23:32:35 +00001886 == P->Partial)
Douglas Gregored9c0f92009-10-29 00:04:11 +00001887 Best = P;
Douglas Gregorbf4ea562009-09-15 16:23:51 +00001888 }
Douglas Gregorbf4ea562009-09-15 16:23:51 +00001889
Douglas Gregored9c0f92009-10-29 00:04:11 +00001890 // Determine if the best partial specialization is more specialized than
1891 // the others.
1892 bool Ambiguous = false;
Douglas Gregorbf4ea562009-09-15 16:23:51 +00001893 for (llvm::SmallVector<MatchResult, 4>::iterator P = Matched.begin(),
1894 PEnd = Matched.end();
Douglas Gregored9c0f92009-10-29 00:04:11 +00001895 P != PEnd; ++P) {
1896 if (P != Best &&
Douglas Gregor9b623632010-10-12 23:32:35 +00001897 getMoreSpecializedPartialSpecialization(P->Partial, Best->Partial,
John McCall5769d612010-02-08 23:07:23 +00001898 PointOfInstantiation)
Douglas Gregor9b623632010-10-12 23:32:35 +00001899 != Best->Partial) {
Douglas Gregored9c0f92009-10-29 00:04:11 +00001900 Ambiguous = true;
1901 break;
1902 }
1903 }
1904
1905 if (Ambiguous) {
1906 // Partial ordering did not produce a clear winner. Complain.
1907 ClassTemplateSpec->setInvalidDecl();
1908 Diag(PointOfInstantiation, diag::err_partial_spec_ordering_ambiguous)
1909 << ClassTemplateSpec;
1910
1911 // Print the matching partial specializations.
1912 for (llvm::SmallVector<MatchResult, 4>::iterator P = Matched.begin(),
1913 PEnd = Matched.end();
1914 P != PEnd; ++P)
Douglas Gregor9b623632010-10-12 23:32:35 +00001915 Diag(P->Partial->getLocation(), diag::note_partial_spec_match)
1916 << getTemplateArgumentBindingsText(
1917 P->Partial->getTemplateParameters(),
1918 *P->Args);
Douglas Gregord6350ae2009-08-28 20:31:08 +00001919
Douglas Gregored9c0f92009-10-29 00:04:11 +00001920 return true;
1921 }
Douglas Gregorbf4ea562009-09-15 16:23:51 +00001922 }
1923
1924 // Instantiate using the best class template partial specialization.
Douglas Gregor9b623632010-10-12 23:32:35 +00001925 ClassTemplatePartialSpecializationDecl *OrigPartialSpec = Best->Partial;
Douglas Gregored9c0f92009-10-29 00:04:11 +00001926 while (OrigPartialSpec->getInstantiatedFromMember()) {
1927 // If we've found an explicit specialization of this class template,
1928 // stop here and use that as the pattern.
1929 if (OrigPartialSpec->isMemberSpecialization())
1930 break;
1931
1932 OrigPartialSpec = OrigPartialSpec->getInstantiatedFromMember();
1933 }
1934
1935 Pattern = OrigPartialSpec;
Douglas Gregor9b623632010-10-12 23:32:35 +00001936 ClassTemplateSpec->setInstantiationOf(Best->Partial, Best->Args);
Douglas Gregorc1efb3f2009-06-12 22:31:52 +00001937 } else {
1938 // -- If no matches are found, the instantiation is generated
1939 // from the primary template.
Douglas Gregord6350ae2009-08-28 20:31:08 +00001940 ClassTemplateDecl *OrigTemplate = Template;
Douglas Gregorfd056bc2009-10-13 16:30:37 +00001941 while (OrigTemplate->getInstantiatedFromMemberTemplate()) {
1942 // If we've found an explicit specialization of this class template,
1943 // stop here and use that as the pattern.
1944 if (OrigTemplate->isMemberSpecialization())
1945 break;
1946
Douglas Gregord6350ae2009-08-28 20:31:08 +00001947 OrigTemplate = OrigTemplate->getInstantiatedFromMemberTemplate();
Douglas Gregorfd056bc2009-10-13 16:30:37 +00001948 }
1949
Douglas Gregord6350ae2009-08-28 20:31:08 +00001950 Pattern = OrigTemplate->getTemplatedDecl();
Douglas Gregorc8ab2562009-05-31 09:31:02 +00001951 }
Douglas Gregor2943aed2009-03-03 04:44:36 +00001952
Douglas Gregor972e6ce2009-10-27 06:26:26 +00001953 bool Result = InstantiateClass(PointOfInstantiation, ClassTemplateSpec,
1954 Pattern,
1955 getTemplateInstantiationArgs(ClassTemplateSpec),
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001956 TSK,
Douglas Gregor5842ba92009-08-24 15:23:48 +00001957 Complain);
Mike Stump1eb44332009-09-09 15:08:12 +00001958
Douglas Gregor199d9912009-06-05 00:53:49 +00001959 return Result;
Douglas Gregor2943aed2009-03-03 04:44:36 +00001960}
Douglas Gregor5953d8b2009-03-19 17:26:29 +00001961
John McCallce3ff2b2009-08-25 22:02:44 +00001962/// \brief Instantiates the definitions of all of the member
1963/// of the given class, which is an instantiation of a class template
1964/// or a member class of a template.
Douglas Gregora58861f2009-05-13 20:28:22 +00001965void
1966Sema::InstantiateClassMembers(SourceLocation PointOfInstantiation,
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001967 CXXRecordDecl *Instantiation,
1968 const MultiLevelTemplateArgumentList &TemplateArgs,
1969 TemplateSpecializationKind TSK) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001970 for (DeclContext::decl_iterator D = Instantiation->decls_begin(),
1971 DEnd = Instantiation->decls_end();
Douglas Gregora58861f2009-05-13 20:28:22 +00001972 D != DEnd; ++D) {
Douglas Gregor0d035142009-10-27 18:42:08 +00001973 bool SuppressNew = false;
Douglas Gregora58861f2009-05-13 20:28:22 +00001974 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(*D)) {
Douglas Gregor0d035142009-10-27 18:42:08 +00001975 if (FunctionDecl *Pattern
1976 = Function->getInstantiatedFromMemberFunction()) {
1977 MemberSpecializationInfo *MSInfo
1978 = Function->getMemberSpecializationInfo();
1979 assert(MSInfo && "No member specialization information?");
Douglas Gregorc42b6522010-04-09 21:02:29 +00001980 if (MSInfo->getTemplateSpecializationKind()
1981 == TSK_ExplicitSpecialization)
1982 continue;
1983
Douglas Gregor0d035142009-10-27 18:42:08 +00001984 if (CheckSpecializationInstantiationRedecl(PointOfInstantiation, TSK,
1985 Function,
1986 MSInfo->getTemplateSpecializationKind(),
Nico Weberc956b6e2010-09-27 21:02:09 +00001987 MSInfo->getPointOfInstantiation(),
Douglas Gregor0d035142009-10-27 18:42:08 +00001988 SuppressNew) ||
1989 SuppressNew)
Douglas Gregorf6b11852009-10-08 15:14:33 +00001990 continue;
1991
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001992 if (Function->hasBody())
Douglas Gregor0d035142009-10-27 18:42:08 +00001993 continue;
1994
1995 if (TSK == TSK_ExplicitInstantiationDefinition) {
1996 // C++0x [temp.explicit]p8:
1997 // An explicit instantiation definition that names a class template
1998 // specialization explicitly instantiates the class template
1999 // specialization and is only an explicit instantiation definition
2000 // of members whose definition is visible at the point of
2001 // instantiation.
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00002002 if (!Pattern->hasBody())
Douglas Gregor0d035142009-10-27 18:42:08 +00002003 continue;
2004
2005 Function->setTemplateSpecializationKind(TSK, PointOfInstantiation);
2006
2007 InstantiateFunctionDefinition(PointOfInstantiation, Function);
2008 } else {
2009 Function->setTemplateSpecializationKind(TSK, PointOfInstantiation);
2010 }
Douglas Gregorf6b11852009-10-08 15:14:33 +00002011 }
Douglas Gregora58861f2009-05-13 20:28:22 +00002012 } else if (VarDecl *Var = dyn_cast<VarDecl>(*D)) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00002013 if (Var->isStaticDataMember()) {
Douglas Gregor0d035142009-10-27 18:42:08 +00002014 MemberSpecializationInfo *MSInfo = Var->getMemberSpecializationInfo();
2015 assert(MSInfo && "No member specialization information?");
Douglas Gregorc42b6522010-04-09 21:02:29 +00002016 if (MSInfo->getTemplateSpecializationKind()
2017 == TSK_ExplicitSpecialization)
2018 continue;
2019
Douglas Gregor0d035142009-10-27 18:42:08 +00002020 if (CheckSpecializationInstantiationRedecl(PointOfInstantiation, TSK,
2021 Var,
2022 MSInfo->getTemplateSpecializationKind(),
Nico Weberc956b6e2010-09-27 21:02:09 +00002023 MSInfo->getPointOfInstantiation(),
Douglas Gregor0d035142009-10-27 18:42:08 +00002024 SuppressNew) ||
2025 SuppressNew)
Douglas Gregorf6b11852009-10-08 15:14:33 +00002026 continue;
2027
Douglas Gregor0d035142009-10-27 18:42:08 +00002028 if (TSK == TSK_ExplicitInstantiationDefinition) {
2029 // C++0x [temp.explicit]p8:
2030 // An explicit instantiation definition that names a class template
2031 // specialization explicitly instantiates the class template
2032 // specialization and is only an explicit instantiation definition
2033 // of members whose definition is visible at the point of
2034 // instantiation.
2035 if (!Var->getInstantiatedFromStaticDataMember()
2036 ->getOutOfLineDefinition())
2037 continue;
2038
2039 Var->setTemplateSpecializationKind(TSK, PointOfInstantiation);
Douglas Gregor251b4ff2009-10-08 07:24:58 +00002040 InstantiateStaticDataMemberDefinition(PointOfInstantiation, Var);
Douglas Gregor0d035142009-10-27 18:42:08 +00002041 } else {
2042 Var->setTemplateSpecializationKind(TSK, PointOfInstantiation);
2043 }
2044 }
Douglas Gregora58861f2009-05-13 20:28:22 +00002045 } else if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(*D)) {
Douglas Gregora77eaa92010-04-18 18:11:38 +00002046 // Always skip the injected-class-name, along with any
2047 // redeclarations of nested classes, since both would cause us
2048 // to try to instantiate the members of a class twice.
2049 if (Record->isInjectedClassName() || Record->getPreviousDeclaration())
Douglas Gregor2db32322009-10-07 23:56:10 +00002050 continue;
2051
Douglas Gregor0d035142009-10-27 18:42:08 +00002052 MemberSpecializationInfo *MSInfo = Record->getMemberSpecializationInfo();
2053 assert(MSInfo && "No member specialization information?");
Douglas Gregorc42b6522010-04-09 21:02:29 +00002054
2055 if (MSInfo->getTemplateSpecializationKind()
2056 == TSK_ExplicitSpecialization)
2057 continue;
Nico Weberc956b6e2010-09-27 21:02:09 +00002058
Douglas Gregor0d035142009-10-27 18:42:08 +00002059 if (CheckSpecializationInstantiationRedecl(PointOfInstantiation, TSK,
2060 Record,
2061 MSInfo->getTemplateSpecializationKind(),
Nico Weberc956b6e2010-09-27 21:02:09 +00002062 MSInfo->getPointOfInstantiation(),
Douglas Gregor0d035142009-10-27 18:42:08 +00002063 SuppressNew) ||
2064 SuppressNew)
Douglas Gregorf6b11852009-10-08 15:14:33 +00002065 continue;
2066
Douglas Gregor0d035142009-10-27 18:42:08 +00002067 CXXRecordDecl *Pattern = Record->getInstantiatedFromMemberClass();
2068 assert(Pattern && "Missing instantiated-from-template information");
2069
Douglas Gregor952b0172010-02-11 01:04:33 +00002070 if (!Record->getDefinition()) {
2071 if (!Pattern->getDefinition()) {
Douglas Gregor0d035142009-10-27 18:42:08 +00002072 // C++0x [temp.explicit]p8:
2073 // An explicit instantiation definition that names a class template
2074 // specialization explicitly instantiates the class template
2075 // specialization and is only an explicit instantiation definition
2076 // of members whose definition is visible at the point of
2077 // instantiation.
2078 if (TSK == TSK_ExplicitInstantiationDeclaration) {
2079 MSInfo->setTemplateSpecializationKind(TSK);
2080 MSInfo->setPointOfInstantiation(PointOfInstantiation);
2081 }
2082
2083 continue;
2084 }
2085
2086 InstantiateClass(PointOfInstantiation, Record, Pattern,
Douglas Gregord0e3daf2009-09-04 22:48:11 +00002087 TemplateArgs,
2088 TSK);
Nico Weberc956b6e2010-09-27 21:02:09 +00002089 } else {
2090 if (TSK == TSK_ExplicitInstantiationDefinition &&
2091 Record->getTemplateSpecializationKind() ==
2092 TSK_ExplicitInstantiationDeclaration) {
2093 Record->setTemplateSpecializationKind(TSK);
2094 MarkVTableUsed(PointOfInstantiation, Record, true);
2095 }
Douglas Gregor0d035142009-10-27 18:42:08 +00002096 }
Douglas Gregore9374d52009-10-08 01:19:17 +00002097
Douglas Gregor952b0172010-02-11 01:04:33 +00002098 Pattern = cast_or_null<CXXRecordDecl>(Record->getDefinition());
Douglas Gregor0d035142009-10-27 18:42:08 +00002099 if (Pattern)
2100 InstantiateClassMembers(PointOfInstantiation, Pattern, TemplateArgs,
2101 TSK);
Douglas Gregora58861f2009-05-13 20:28:22 +00002102 }
2103 }
2104}
2105
2106/// \brief Instantiate the definitions of all of the members of the
2107/// given class template specialization, which was named as part of an
2108/// explicit instantiation.
Mike Stump1eb44332009-09-09 15:08:12 +00002109void
Douglas Gregord0e3daf2009-09-04 22:48:11 +00002110Sema::InstantiateClassTemplateSpecializationMembers(
Douglas Gregora58861f2009-05-13 20:28:22 +00002111 SourceLocation PointOfInstantiation,
Douglas Gregord0e3daf2009-09-04 22:48:11 +00002112 ClassTemplateSpecializationDecl *ClassTemplateSpec,
2113 TemplateSpecializationKind TSK) {
Douglas Gregora58861f2009-05-13 20:28:22 +00002114 // C++0x [temp.explicit]p7:
2115 // An explicit instantiation that names a class template
2116 // specialization is an explicit instantion of the same kind
2117 // (declaration or definition) of each of its members (not
2118 // including members inherited from base classes) that has not
2119 // been previously explicitly specialized in the translation unit
2120 // containing the explicit instantiation, except as described
2121 // below.
2122 InstantiateClassMembers(PointOfInstantiation, ClassTemplateSpec,
Douglas Gregord0e3daf2009-09-04 22:48:11 +00002123 getTemplateInstantiationArgs(ClassTemplateSpec),
2124 TSK);
Douglas Gregora58861f2009-05-13 20:28:22 +00002125}
2126
John McCall60d7b3a2010-08-24 06:29:42 +00002127StmtResult
Douglas Gregord6350ae2009-08-28 20:31:08 +00002128Sema::SubstStmt(Stmt *S, const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor43959a92009-08-20 07:17:43 +00002129 if (!S)
2130 return Owned(S);
2131
2132 TemplateInstantiator Instantiator(*this, TemplateArgs,
2133 SourceLocation(),
2134 DeclarationName());
2135 return Instantiator.TransformStmt(S);
2136}
2137
John McCall60d7b3a2010-08-24 06:29:42 +00002138ExprResult
Douglas Gregord6350ae2009-08-28 20:31:08 +00002139Sema::SubstExpr(Expr *E, const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregorb98b1992009-08-11 05:31:07 +00002140 if (!E)
2141 return Owned(E);
Mike Stump1eb44332009-09-09 15:08:12 +00002142
Douglas Gregorb98b1992009-08-11 05:31:07 +00002143 TemplateInstantiator Instantiator(*this, TemplateArgs,
2144 SourceLocation(),
2145 DeclarationName());
2146 return Instantiator.TransformExpr(E);
2147}
2148
Douglas Gregor91fc73e2011-01-07 19:35:17 +00002149bool Sema::SubstExprs(Expr **Exprs, unsigned NumExprs, bool IsCall,
2150 const MultiLevelTemplateArgumentList &TemplateArgs,
2151 llvm::SmallVectorImpl<Expr *> &Outputs) {
2152 if (NumExprs == 0)
2153 return false;
2154
2155 TemplateInstantiator Instantiator(*this, TemplateArgs,
2156 SourceLocation(),
2157 DeclarationName());
2158 return Instantiator.TransformExprs(Exprs, NumExprs, IsCall, Outputs);
2159}
2160
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00002161NestedNameSpecifierLoc
2162Sema::SubstNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS,
2163 const MultiLevelTemplateArgumentList &TemplateArgs) {
2164 if (!NNS)
2165 return NestedNameSpecifierLoc();
2166
2167 TemplateInstantiator Instantiator(*this, TemplateArgs, NNS.getBeginLoc(),
2168 DeclarationName());
2169 return Instantiator.TransformNestedNameSpecifierLoc(NNS);
2170}
2171
Abramo Bagnara25777432010-08-11 22:01:17 +00002172/// \brief Do template substitution on declaration name info.
2173DeclarationNameInfo
2174Sema::SubstDeclarationNameInfo(const DeclarationNameInfo &NameInfo,
2175 const MultiLevelTemplateArgumentList &TemplateArgs) {
2176 TemplateInstantiator Instantiator(*this, TemplateArgs, NameInfo.getLoc(),
2177 NameInfo.getName());
2178 return Instantiator.TransformDeclarationNameInfo(NameInfo);
2179}
2180
Douglas Gregorde650ae2009-03-31 18:38:02 +00002181TemplateName
Douglas Gregor1d752d72011-03-02 18:46:51 +00002182Sema::SubstTemplateName(NestedNameSpecifierLoc QualifierLoc,
2183 TemplateName Name, SourceLocation Loc,
Douglas Gregord6350ae2009-08-28 20:31:08 +00002184 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregord1067e52009-08-06 06:41:21 +00002185 TemplateInstantiator Instantiator(*this, TemplateArgs, Loc,
2186 DeclarationName());
Douglas Gregor1d752d72011-03-02 18:46:51 +00002187 CXXScopeSpec SS;
2188 SS.Adopt(QualifierLoc);
2189 return Instantiator.TransformTemplateName(SS, Name, Loc);
Douglas Gregorde650ae2009-03-31 18:38:02 +00002190}
Douglas Gregor91333002009-06-11 00:06:24 +00002191
Douglas Gregore02e2622010-12-22 21:19:48 +00002192bool Sema::Subst(const TemplateArgumentLoc *Args, unsigned NumArgs,
2193 TemplateArgumentListInfo &Result,
John McCall833ca992009-10-29 08:12:44 +00002194 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor670444e2009-08-04 22:27:00 +00002195 TemplateInstantiator Instantiator(*this, TemplateArgs, SourceLocation(),
2196 DeclarationName());
Douglas Gregore02e2622010-12-22 21:19:48 +00002197
2198 return Instantiator.TransformTemplateArguments(Args, NumArgs, Result);
Douglas Gregor91333002009-06-11 00:06:24 +00002199}
Douglas Gregor895162d2010-04-30 18:55:50 +00002200
Douglas Gregor12c9c002011-01-07 16:43:16 +00002201llvm::PointerUnion<Decl *, LocalInstantiationScope::DeclArgumentPack *> *
2202LocalInstantiationScope::findInstantiationOf(const Decl *D) {
Chris Lattner57ad3782011-02-17 20:34:02 +00002203 for (LocalInstantiationScope *Current = this; Current;
Douglas Gregor895162d2010-04-30 18:55:50 +00002204 Current = Current->Outer) {
Chris Lattner57ad3782011-02-17 20:34:02 +00002205
Douglas Gregor895162d2010-04-30 18:55:50 +00002206 // Check if we found something within this scope.
Douglas Gregorebb1c562010-12-21 21:22:51 +00002207 const Decl *CheckD = D;
2208 do {
Douglas Gregor12c9c002011-01-07 16:43:16 +00002209 LocalDeclsMap::iterator Found = Current->LocalDecls.find(CheckD);
Douglas Gregorebb1c562010-12-21 21:22:51 +00002210 if (Found != Current->LocalDecls.end())
Douglas Gregor12c9c002011-01-07 16:43:16 +00002211 return &Found->second;
Douglas Gregorebb1c562010-12-21 21:22:51 +00002212
2213 // If this is a tag declaration, it's possible that we need to look for
2214 // a previous declaration.
2215 if (const TagDecl *Tag = dyn_cast<TagDecl>(CheckD))
2216 CheckD = Tag->getPreviousDeclaration();
2217 else
2218 CheckD = 0;
2219 } while (CheckD);
2220
Douglas Gregor895162d2010-04-30 18:55:50 +00002221 // If we aren't combined with our outer scope, we're done.
2222 if (!Current->CombineWithOuterScope)
2223 break;
2224 }
Chris Lattner57ad3782011-02-17 20:34:02 +00002225
2226 // If we didn't find the decl, then we either have a sema bug, or we have a
2227 // forward reference to a label declaration. Return null to indicate that
2228 // we have an uninstantiated label.
2229 assert(isa<LabelDecl>(D) && "declaration not instantiated in this scope");
Douglas Gregor895162d2010-04-30 18:55:50 +00002230 return 0;
2231}
2232
John McCall2a7fb272010-08-25 05:32:35 +00002233void LocalInstantiationScope::InstantiatedLocal(const Decl *D, Decl *Inst) {
Douglas Gregor12c9c002011-01-07 16:43:16 +00002234 llvm::PointerUnion<Decl *, DeclArgumentPack *> &Stored = LocalDecls[D];
Douglas Gregord3731192011-01-10 07:32:04 +00002235 if (Stored.isNull())
2236 Stored = Inst;
2237 else if (Stored.is<Decl *>()) {
2238 assert(Stored.get<Decl *>() == Inst && "Already instantiated this local");
2239 Stored = Inst;
2240 } else
2241 LocalDecls[D].get<DeclArgumentPack *>()->push_back(Inst);
Douglas Gregor895162d2010-04-30 18:55:50 +00002242}
Douglas Gregor12c9c002011-01-07 16:43:16 +00002243
2244void LocalInstantiationScope::InstantiatedLocalPackArg(const Decl *D,
2245 Decl *Inst) {
2246 DeclArgumentPack *Pack = LocalDecls[D].get<DeclArgumentPack *>();
2247 Pack->push_back(Inst);
2248}
2249
2250void LocalInstantiationScope::MakeInstantiatedLocalArgPack(const Decl *D) {
2251 llvm::PointerUnion<Decl *, DeclArgumentPack *> &Stored = LocalDecls[D];
2252 assert(Stored.isNull() && "Already instantiated this local");
2253 DeclArgumentPack *Pack = new DeclArgumentPack;
2254 Stored = Pack;
2255 ArgumentPacks.push_back(Pack);
2256}
2257
Douglas Gregord3731192011-01-10 07:32:04 +00002258void LocalInstantiationScope::SetPartiallySubstitutedPack(NamedDecl *Pack,
2259 const TemplateArgument *ExplicitArgs,
2260 unsigned NumExplicitArgs) {
2261 assert((!PartiallySubstitutedPack || PartiallySubstitutedPack == Pack) &&
2262 "Already have a partially-substituted pack");
2263 assert((!PartiallySubstitutedPack
2264 || NumArgsInPartiallySubstitutedPack == NumExplicitArgs) &&
2265 "Wrong number of arguments in partially-substituted pack");
2266 PartiallySubstitutedPack = Pack;
2267 ArgsInPartiallySubstitutedPack = ExplicitArgs;
2268 NumArgsInPartiallySubstitutedPack = NumExplicitArgs;
2269}
2270
2271NamedDecl *LocalInstantiationScope::getPartiallySubstitutedPack(
2272 const TemplateArgument **ExplicitArgs,
2273 unsigned *NumExplicitArgs) const {
2274 if (ExplicitArgs)
2275 *ExplicitArgs = 0;
2276 if (NumExplicitArgs)
2277 *NumExplicitArgs = 0;
2278
2279 for (const LocalInstantiationScope *Current = this; Current;
2280 Current = Current->Outer) {
2281 if (Current->PartiallySubstitutedPack) {
2282 if (ExplicitArgs)
2283 *ExplicitArgs = Current->ArgsInPartiallySubstitutedPack;
2284 if (NumExplicitArgs)
2285 *NumExplicitArgs = Current->NumArgsInPartiallySubstitutedPack;
2286
2287 return Current->PartiallySubstitutedPack;
2288 }
2289
2290 if (!Current->CombineWithOuterScope)
2291 break;
2292 }
2293
2294 return 0;
2295}