blob: 5f181fb5e1cedf64640b00ff8a42b56880d9924a [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;
98 }
99
John McCallf181d8a2009-08-29 03:16:09 +0000100 // If this is a friend declaration and it declares an entity at
101 // namespace scope, take arguments from its lexical parent
Douglas Gregore7089b02010-05-03 23:29:10 +0000102 // instead of its semantic parent, unless of course the pattern we're
103 // instantiating actually comes from the file's context!
John McCallf181d8a2009-08-29 03:16:09 +0000104 if (Function->getFriendObjectKind() &&
Douglas Gregore7089b02010-05-03 23:29:10 +0000105 Function->getDeclContext()->isFileContext() &&
106 (!Pattern || !Pattern->getLexicalDeclContext()->isFileContext())) {
John McCallf181d8a2009-08-29 03:16:09 +0000107 Ctx = Function->getLexicalDeclContext();
Douglas Gregor525f96c2010-02-05 07:33:43 +0000108 RelativeToPrimary = false;
John McCallf181d8a2009-08-29 03:16:09 +0000109 continue;
110 }
Douglas Gregor24bae922010-07-08 18:37:38 +0000111 } else if (CXXRecordDecl *Rec = dyn_cast<CXXRecordDecl>(Ctx)) {
112 if (ClassTemplateDecl *ClassTemplate = Rec->getDescribedClassTemplate()) {
113 QualType T = ClassTemplate->getInjectedClassNameSpecialization();
114 const TemplateSpecializationType *TST
115 = cast<TemplateSpecializationType>(Context.getCanonicalType(T));
116 Result.addOuterTemplateArguments(TST->getArgs(), TST->getNumArgs());
117 if (ClassTemplate->isMemberSpecialization())
118 break;
119 }
Douglas Gregord1102432009-08-28 17:37:35 +0000120 }
John McCallf181d8a2009-08-29 03:16:09 +0000121
122 Ctx = Ctx->getParent();
Douglas Gregor525f96c2010-02-05 07:33:43 +0000123 RelativeToPrimary = false;
Douglas Gregor54dabfc2009-05-14 23:26:13 +0000124 }
Mike Stump1eb44332009-09-09 15:08:12 +0000125
Douglas Gregord1102432009-08-28 17:37:35 +0000126 return Result;
Douglas Gregor54dabfc2009-05-14 23:26:13 +0000127}
128
Douglas Gregorf35f8282009-11-11 21:54:23 +0000129bool Sema::ActiveTemplateInstantiation::isInstantiationRecord() const {
130 switch (Kind) {
131 case TemplateInstantiation:
132 case DefaultTemplateArgumentInstantiation:
133 case DefaultFunctionArgumentInstantiation:
134 return true;
135
136 case ExplicitTemplateArgumentSubstitution:
137 case DeducedTemplateArgumentSubstitution:
138 case PriorTemplateArgumentSubstitution:
139 case DefaultTemplateArgumentChecking:
140 return false;
141 }
142
143 return true;
144}
145
Douglas Gregor26dce442009-03-10 00:06:19 +0000146Sema::InstantiatingTemplate::
147InstantiatingTemplate(Sema &SemaRef, SourceLocation PointOfInstantiation,
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000148 Decl *Entity,
Douglas Gregor26dce442009-03-10 00:06:19 +0000149 SourceRange InstantiationRange)
150 : SemaRef(SemaRef) {
Douglas Gregordf667e72009-03-10 20:44:00 +0000151 Invalid = CheckInstantiationDepth(PointOfInstantiation,
152 InstantiationRange);
153 if (!Invalid) {
Douglas Gregor26dce442009-03-10 00:06:19 +0000154 ActiveTemplateInstantiation Inst;
Douglas Gregordf667e72009-03-10 20:44:00 +0000155 Inst.Kind = ActiveTemplateInstantiation::TemplateInstantiation;
Douglas Gregor26dce442009-03-10 00:06:19 +0000156 Inst.PointOfInstantiation = PointOfInstantiation;
Douglas Gregordf667e72009-03-10 20:44:00 +0000157 Inst.Entity = reinterpret_cast<uintptr_t>(Entity);
Douglas Gregor313a81d2009-03-12 18:36:18 +0000158 Inst.TemplateArgs = 0;
159 Inst.NumTemplateArgs = 0;
Douglas Gregordf667e72009-03-10 20:44:00 +0000160 Inst.InstantiationRange = InstantiationRange;
161 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
Douglas Gregordf667e72009-03-10 20:44:00 +0000162 }
163}
164
Mike Stump1eb44332009-09-09 15:08:12 +0000165Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef,
Douglas Gregordf667e72009-03-10 20:44:00 +0000166 SourceLocation PointOfInstantiation,
167 TemplateDecl *Template,
168 const TemplateArgument *TemplateArgs,
169 unsigned NumTemplateArgs,
170 SourceRange InstantiationRange)
171 : SemaRef(SemaRef) {
172
173 Invalid = CheckInstantiationDepth(PointOfInstantiation,
174 InstantiationRange);
175 if (!Invalid) {
176 ActiveTemplateInstantiation Inst;
Mike Stump1eb44332009-09-09 15:08:12 +0000177 Inst.Kind
Douglas Gregordf667e72009-03-10 20:44:00 +0000178 = ActiveTemplateInstantiation::DefaultTemplateArgumentInstantiation;
179 Inst.PointOfInstantiation = PointOfInstantiation;
180 Inst.Entity = reinterpret_cast<uintptr_t>(Template);
181 Inst.TemplateArgs = TemplateArgs;
182 Inst.NumTemplateArgs = NumTemplateArgs;
Douglas Gregor26dce442009-03-10 00:06:19 +0000183 Inst.InstantiationRange = InstantiationRange;
184 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
Douglas Gregor26dce442009-03-10 00:06:19 +0000185 }
186}
187
Mike Stump1eb44332009-09-09 15:08:12 +0000188Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef,
Douglas Gregor637a4092009-06-10 23:47:09 +0000189 SourceLocation PointOfInstantiation,
Douglas Gregorcca9e962009-07-01 22:01:06 +0000190 FunctionTemplateDecl *FunctionTemplate,
191 const TemplateArgument *TemplateArgs,
192 unsigned NumTemplateArgs,
193 ActiveTemplateInstantiation::InstantiationKind Kind,
Douglas Gregor9b623632010-10-12 23:32:35 +0000194 sema::TemplateDeductionInfo &DeductionInfo,
Douglas Gregorcca9e962009-07-01 22:01:06 +0000195 SourceRange InstantiationRange)
Nick Lewycky7663f392010-11-20 01:29:55 +0000196 : SemaRef(SemaRef) {
Mike Stump1eb44332009-09-09 15:08:12 +0000197
Douglas Gregorcca9e962009-07-01 22:01:06 +0000198 Invalid = CheckInstantiationDepth(PointOfInstantiation,
199 InstantiationRange);
200 if (!Invalid) {
201 ActiveTemplateInstantiation Inst;
202 Inst.Kind = Kind;
203 Inst.PointOfInstantiation = PointOfInstantiation;
204 Inst.Entity = reinterpret_cast<uintptr_t>(FunctionTemplate);
205 Inst.TemplateArgs = TemplateArgs;
206 Inst.NumTemplateArgs = NumTemplateArgs;
Douglas Gregor9b623632010-10-12 23:32:35 +0000207 Inst.DeductionInfo = &DeductionInfo;
Douglas Gregorcca9e962009-07-01 22:01:06 +0000208 Inst.InstantiationRange = InstantiationRange;
209 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
Douglas Gregorf35f8282009-11-11 21:54:23 +0000210
211 if (!Inst.isInstantiationRecord())
212 ++SemaRef.NonInstantiationEntries;
Douglas Gregorcca9e962009-07-01 22:01:06 +0000213 }
214}
215
Mike Stump1eb44332009-09-09 15:08:12 +0000216Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef,
Douglas Gregorcca9e962009-07-01 22:01:06 +0000217 SourceLocation PointOfInstantiation,
Douglas Gregor637a4092009-06-10 23:47:09 +0000218 ClassTemplatePartialSpecializationDecl *PartialSpec,
219 const TemplateArgument *TemplateArgs,
220 unsigned NumTemplateArgs,
Douglas Gregor9b623632010-10-12 23:32:35 +0000221 sema::TemplateDeductionInfo &DeductionInfo,
Douglas Gregor637a4092009-06-10 23:47:09 +0000222 SourceRange InstantiationRange)
223 : SemaRef(SemaRef) {
224
Douglas Gregorf35f8282009-11-11 21:54:23 +0000225 Invalid = false;
226
227 ActiveTemplateInstantiation Inst;
228 Inst.Kind = ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution;
229 Inst.PointOfInstantiation = PointOfInstantiation;
230 Inst.Entity = reinterpret_cast<uintptr_t>(PartialSpec);
231 Inst.TemplateArgs = TemplateArgs;
232 Inst.NumTemplateArgs = NumTemplateArgs;
Douglas Gregor9b623632010-10-12 23:32:35 +0000233 Inst.DeductionInfo = &DeductionInfo;
Douglas Gregorf35f8282009-11-11 21:54:23 +0000234 Inst.InstantiationRange = InstantiationRange;
235 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
236
237 assert(!Inst.isInstantiationRecord());
238 ++SemaRef.NonInstantiationEntries;
Douglas Gregor637a4092009-06-10 23:47:09 +0000239}
240
Mike Stump1eb44332009-09-09 15:08:12 +0000241Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef,
Douglas Gregor9148c3f2009-11-11 19:13:48 +0000242 SourceLocation PointOfInstantiation,
Anders Carlsson25cae7f2009-09-05 05:14:19 +0000243 ParmVarDecl *Param,
244 const TemplateArgument *TemplateArgs,
245 unsigned NumTemplateArgs,
246 SourceRange InstantiationRange)
247 : SemaRef(SemaRef) {
Mike Stump1eb44332009-09-09 15:08:12 +0000248
Douglas Gregor9148c3f2009-11-11 19:13:48 +0000249 Invalid = CheckInstantiationDepth(PointOfInstantiation, InstantiationRange);
Anders Carlsson25cae7f2009-09-05 05:14:19 +0000250
251 if (!Invalid) {
252 ActiveTemplateInstantiation Inst;
253 Inst.Kind
254 = ActiveTemplateInstantiation::DefaultFunctionArgumentInstantiation;
Douglas Gregor9148c3f2009-11-11 19:13:48 +0000255 Inst.PointOfInstantiation = PointOfInstantiation;
Anders Carlsson25cae7f2009-09-05 05:14:19 +0000256 Inst.Entity = reinterpret_cast<uintptr_t>(Param);
257 Inst.TemplateArgs = TemplateArgs;
258 Inst.NumTemplateArgs = NumTemplateArgs;
259 Inst.InstantiationRange = InstantiationRange;
260 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
Douglas Gregor9148c3f2009-11-11 19:13:48 +0000261 }
262}
263
264Sema::InstantiatingTemplate::
265InstantiatingTemplate(Sema &SemaRef, SourceLocation PointOfInstantiation,
Douglas Gregor54c53cc2011-01-04 23:35:54 +0000266 NamedDecl *Template,
Douglas Gregor9148c3f2009-11-11 19:13:48 +0000267 NonTypeTemplateParmDecl *Param,
268 const TemplateArgument *TemplateArgs,
269 unsigned NumTemplateArgs,
270 SourceRange InstantiationRange) : SemaRef(SemaRef) {
Douglas Gregorf35f8282009-11-11 21:54:23 +0000271 Invalid = false;
Douglas Gregor9148c3f2009-11-11 19:13:48 +0000272
Douglas Gregorf35f8282009-11-11 21:54:23 +0000273 ActiveTemplateInstantiation Inst;
274 Inst.Kind = ActiveTemplateInstantiation::PriorTemplateArgumentSubstitution;
275 Inst.PointOfInstantiation = PointOfInstantiation;
276 Inst.Template = Template;
277 Inst.Entity = reinterpret_cast<uintptr_t>(Param);
278 Inst.TemplateArgs = TemplateArgs;
279 Inst.NumTemplateArgs = NumTemplateArgs;
280 Inst.InstantiationRange = InstantiationRange;
281 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
282
283 assert(!Inst.isInstantiationRecord());
284 ++SemaRef.NonInstantiationEntries;
Douglas Gregor9148c3f2009-11-11 19:13:48 +0000285}
286
287Sema::InstantiatingTemplate::
288InstantiatingTemplate(Sema &SemaRef, SourceLocation PointOfInstantiation,
Douglas Gregor54c53cc2011-01-04 23:35:54 +0000289 NamedDecl *Template,
Douglas Gregor9148c3f2009-11-11 19:13:48 +0000290 TemplateTemplateParmDecl *Param,
291 const TemplateArgument *TemplateArgs,
292 unsigned NumTemplateArgs,
293 SourceRange InstantiationRange) : SemaRef(SemaRef) {
Douglas Gregorf35f8282009-11-11 21:54:23 +0000294 Invalid = false;
295 ActiveTemplateInstantiation Inst;
296 Inst.Kind = ActiveTemplateInstantiation::PriorTemplateArgumentSubstitution;
297 Inst.PointOfInstantiation = PointOfInstantiation;
298 Inst.Template = Template;
299 Inst.Entity = reinterpret_cast<uintptr_t>(Param);
300 Inst.TemplateArgs = TemplateArgs;
301 Inst.NumTemplateArgs = NumTemplateArgs;
302 Inst.InstantiationRange = InstantiationRange;
303 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
Douglas Gregor9148c3f2009-11-11 19:13:48 +0000304
Douglas Gregorf35f8282009-11-11 21:54:23 +0000305 assert(!Inst.isInstantiationRecord());
306 ++SemaRef.NonInstantiationEntries;
307}
308
309Sema::InstantiatingTemplate::
310InstantiatingTemplate(Sema &SemaRef, SourceLocation PointOfInstantiation,
311 TemplateDecl *Template,
312 NamedDecl *Param,
313 const TemplateArgument *TemplateArgs,
314 unsigned NumTemplateArgs,
315 SourceRange InstantiationRange) : SemaRef(SemaRef) {
316 Invalid = false;
317
318 ActiveTemplateInstantiation Inst;
319 Inst.Kind = ActiveTemplateInstantiation::DefaultTemplateArgumentChecking;
320 Inst.PointOfInstantiation = PointOfInstantiation;
321 Inst.Template = Template;
322 Inst.Entity = reinterpret_cast<uintptr_t>(Param);
323 Inst.TemplateArgs = TemplateArgs;
324 Inst.NumTemplateArgs = NumTemplateArgs;
325 Inst.InstantiationRange = InstantiationRange;
326 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
327
328 assert(!Inst.isInstantiationRecord());
329 ++SemaRef.NonInstantiationEntries;
Anders Carlsson25cae7f2009-09-05 05:14:19 +0000330}
331
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000332void Sema::InstantiatingTemplate::Clear() {
333 if (!Invalid) {
Douglas Gregorf35f8282009-11-11 21:54:23 +0000334 if (!SemaRef.ActiveTemplateInstantiations.back().isInstantiationRecord()) {
335 assert(SemaRef.NonInstantiationEntries > 0);
336 --SemaRef.NonInstantiationEntries;
337 }
338
Douglas Gregor26dce442009-03-10 00:06:19 +0000339 SemaRef.ActiveTemplateInstantiations.pop_back();
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000340 Invalid = true;
341 }
Douglas Gregor26dce442009-03-10 00:06:19 +0000342}
343
Douglas Gregordf667e72009-03-10 20:44:00 +0000344bool Sema::InstantiatingTemplate::CheckInstantiationDepth(
345 SourceLocation PointOfInstantiation,
346 SourceRange InstantiationRange) {
Douglas Gregorf35f8282009-11-11 21:54:23 +0000347 assert(SemaRef.NonInstantiationEntries <=
348 SemaRef.ActiveTemplateInstantiations.size());
349 if ((SemaRef.ActiveTemplateInstantiations.size() -
350 SemaRef.NonInstantiationEntries)
351 <= SemaRef.getLangOptions().InstantiationDepth)
Douglas Gregordf667e72009-03-10 20:44:00 +0000352 return false;
353
Mike Stump1eb44332009-09-09 15:08:12 +0000354 SemaRef.Diag(PointOfInstantiation,
Douglas Gregordf667e72009-03-10 20:44:00 +0000355 diag::err_template_recursion_depth_exceeded)
356 << SemaRef.getLangOptions().InstantiationDepth
357 << InstantiationRange;
358 SemaRef.Diag(PointOfInstantiation, diag::note_template_recursion_depth)
359 << SemaRef.getLangOptions().InstantiationDepth;
360 return true;
361}
362
Douglas Gregoree1828a2009-03-10 18:03:33 +0000363/// \brief Prints the current instantiation stack through a series of
364/// notes.
365void Sema::PrintInstantiationStack() {
Douglas Gregor575cf372010-04-20 07:18:24 +0000366 // Determine which template instantiations to skip, if any.
367 unsigned SkipStart = ActiveTemplateInstantiations.size(), SkipEnd = SkipStart;
368 unsigned Limit = Diags.getTemplateBacktraceLimit();
369 if (Limit && Limit < ActiveTemplateInstantiations.size()) {
370 SkipStart = Limit / 2 + Limit % 2;
371 SkipEnd = ActiveTemplateInstantiations.size() - Limit / 2;
372 }
373
Douglas Gregorcca9e962009-07-01 22:01:06 +0000374 // FIXME: In all of these cases, we need to show the template arguments
Douglas Gregor575cf372010-04-20 07:18:24 +0000375 unsigned InstantiationIdx = 0;
Douglas Gregoree1828a2009-03-10 18:03:33 +0000376 for (llvm::SmallVector<ActiveTemplateInstantiation, 16>::reverse_iterator
377 Active = ActiveTemplateInstantiations.rbegin(),
378 ActiveEnd = ActiveTemplateInstantiations.rend();
379 Active != ActiveEnd;
Douglas Gregor575cf372010-04-20 07:18:24 +0000380 ++Active, ++InstantiationIdx) {
381 // Skip this instantiation?
382 if (InstantiationIdx >= SkipStart && InstantiationIdx < SkipEnd) {
383 if (InstantiationIdx == SkipStart) {
384 // Note that we're skipping instantiations.
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000385 Diags.Report(Active->PointOfInstantiation,
Douglas Gregor575cf372010-04-20 07:18:24 +0000386 diag::note_instantiation_contexts_suppressed)
387 << unsigned(ActiveTemplateInstantiations.size() - Limit);
388 }
389 continue;
390 }
391
Douglas Gregordf667e72009-03-10 20:44:00 +0000392 switch (Active->Kind) {
393 case ActiveTemplateInstantiation::TemplateInstantiation: {
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000394 Decl *D = reinterpret_cast<Decl *>(Active->Entity);
395 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) {
396 unsigned DiagID = diag::note_template_member_class_here;
397 if (isa<ClassTemplateSpecializationDecl>(Record))
398 DiagID = diag::note_template_class_instantiation_here;
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000399 Diags.Report(Active->PointOfInstantiation, DiagID)
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000400 << Context.getTypeDeclType(Record)
401 << Active->InstantiationRange;
Douglas Gregor7caa6822009-07-24 20:34:43 +0000402 } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
Douglas Gregor1637be72009-06-26 00:10:03 +0000403 unsigned DiagID;
404 if (Function->getPrimaryTemplate())
405 DiagID = diag::note_function_template_spec_here;
406 else
407 DiagID = diag::note_template_member_function_here;
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000408 Diags.Report(Active->PointOfInstantiation, DiagID)
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000409 << Function
410 << Active->InstantiationRange;
Douglas Gregor7caa6822009-07-24 20:34:43 +0000411 } else {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000412 Diags.Report(Active->PointOfInstantiation,
Douglas Gregor7caa6822009-07-24 20:34:43 +0000413 diag::note_template_static_data_member_def_here)
414 << cast<VarDecl>(D)
415 << Active->InstantiationRange;
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000416 }
Douglas Gregordf667e72009-03-10 20:44:00 +0000417 break;
418 }
419
420 case ActiveTemplateInstantiation::DefaultTemplateArgumentInstantiation: {
421 TemplateDecl *Template = cast<TemplateDecl>((Decl *)Active->Entity);
422 std::string TemplateArgsStr
Douglas Gregor7532dc62009-03-30 22:58:21 +0000423 = TemplateSpecializationType::PrintTemplateArgumentList(
Mike Stump1eb44332009-09-09 15:08:12 +0000424 Active->TemplateArgs,
Douglas Gregord249e1d1f2009-05-29 20:38:28 +0000425 Active->NumTemplateArgs,
426 Context.PrintingPolicy);
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000427 Diags.Report(Active->PointOfInstantiation,
Douglas Gregordf667e72009-03-10 20:44:00 +0000428 diag::note_default_arg_instantiation_here)
429 << (Template->getNameAsString() + TemplateArgsStr)
430 << Active->InstantiationRange;
431 break;
432 }
Douglas Gregor637a4092009-06-10 23:47:09 +0000433
Douglas Gregorcca9e962009-07-01 22:01:06 +0000434 case ActiveTemplateInstantiation::ExplicitTemplateArgumentSubstitution: {
Mike Stump1eb44332009-09-09 15:08:12 +0000435 FunctionTemplateDecl *FnTmpl
Douglas Gregorcca9e962009-07-01 22:01:06 +0000436 = cast<FunctionTemplateDecl>((Decl *)Active->Entity);
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000437 Diags.Report(Active->PointOfInstantiation,
Douglas Gregorcca9e962009-07-01 22:01:06 +0000438 diag::note_explicit_template_arg_substitution_here)
Douglas Gregor5e402912010-03-30 20:35:20 +0000439 << FnTmpl
440 << getTemplateArgumentBindingsText(FnTmpl->getTemplateParameters(),
441 Active->TemplateArgs,
442 Active->NumTemplateArgs)
443 << Active->InstantiationRange;
Douglas Gregor637a4092009-06-10 23:47:09 +0000444 break;
445 }
Mike Stump1eb44332009-09-09 15:08:12 +0000446
Douglas Gregorcca9e962009-07-01 22:01:06 +0000447 case ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution:
448 if (ClassTemplatePartialSpecializationDecl *PartialSpec
449 = dyn_cast<ClassTemplatePartialSpecializationDecl>(
450 (Decl *)Active->Entity)) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000451 Diags.Report(Active->PointOfInstantiation,
Douglas Gregorcca9e962009-07-01 22:01:06 +0000452 diag::note_partial_spec_deduct_instantiation_here)
453 << Context.getTypeDeclType(PartialSpec)
Douglas Gregor5e402912010-03-30 20:35:20 +0000454 << getTemplateArgumentBindingsText(
455 PartialSpec->getTemplateParameters(),
456 Active->TemplateArgs,
457 Active->NumTemplateArgs)
Douglas Gregorcca9e962009-07-01 22:01:06 +0000458 << Active->InstantiationRange;
459 } else {
460 FunctionTemplateDecl *FnTmpl
461 = cast<FunctionTemplateDecl>((Decl *)Active->Entity);
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000462 Diags.Report(Active->PointOfInstantiation,
Douglas Gregorcca9e962009-07-01 22:01:06 +0000463 diag::note_function_template_deduction_instantiation_here)
Douglas Gregor5e402912010-03-30 20:35:20 +0000464 << FnTmpl
465 << getTemplateArgumentBindingsText(FnTmpl->getTemplateParameters(),
466 Active->TemplateArgs,
467 Active->NumTemplateArgs)
468 << Active->InstantiationRange;
Douglas Gregorcca9e962009-07-01 22:01:06 +0000469 }
470 break;
Douglas Gregor637a4092009-06-10 23:47:09 +0000471
Anders Carlsson25cae7f2009-09-05 05:14:19 +0000472 case ActiveTemplateInstantiation::DefaultFunctionArgumentInstantiation: {
473 ParmVarDecl *Param = cast<ParmVarDecl>((Decl *)Active->Entity);
474 FunctionDecl *FD = cast<FunctionDecl>(Param->getDeclContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000475
Anders Carlsson25cae7f2009-09-05 05:14:19 +0000476 std::string TemplateArgsStr
477 = TemplateSpecializationType::PrintTemplateArgumentList(
Mike Stump1eb44332009-09-09 15:08:12 +0000478 Active->TemplateArgs,
Anders Carlsson25cae7f2009-09-05 05:14:19 +0000479 Active->NumTemplateArgs,
480 Context.PrintingPolicy);
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000481 Diags.Report(Active->PointOfInstantiation,
Anders Carlsson25cae7f2009-09-05 05:14:19 +0000482 diag::note_default_function_arg_instantiation_here)
Anders Carlsson6bc107b2009-09-05 05:38:54 +0000483 << (FD->getNameAsString() + TemplateArgsStr)
Anders Carlsson25cae7f2009-09-05 05:14:19 +0000484 << Active->InstantiationRange;
485 break;
486 }
Mike Stump1eb44332009-09-09 15:08:12 +0000487
Douglas Gregor9148c3f2009-11-11 19:13:48 +0000488 case ActiveTemplateInstantiation::PriorTemplateArgumentSubstitution: {
489 NamedDecl *Parm = cast<NamedDecl>((Decl *)Active->Entity);
490 std::string Name;
491 if (!Parm->getName().empty())
492 Name = std::string(" '") + Parm->getName().str() + "'";
Douglas Gregor54c53cc2011-01-04 23:35:54 +0000493
494 TemplateParameterList *TemplateParams = 0;
495 if (TemplateDecl *Template = dyn_cast<TemplateDecl>(Active->Template))
496 TemplateParams = Template->getTemplateParameters();
497 else
498 TemplateParams =
499 cast<ClassTemplatePartialSpecializationDecl>(Active->Template)
500 ->getTemplateParameters();
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000501 Diags.Report(Active->PointOfInstantiation,
Douglas Gregor9148c3f2009-11-11 19:13:48 +0000502 diag::note_prior_template_arg_substitution)
503 << isa<TemplateTemplateParmDecl>(Parm)
504 << Name
Douglas Gregor54c53cc2011-01-04 23:35:54 +0000505 << getTemplateArgumentBindingsText(TemplateParams,
Douglas Gregor9148c3f2009-11-11 19:13:48 +0000506 Active->TemplateArgs,
507 Active->NumTemplateArgs)
508 << Active->InstantiationRange;
509 break;
510 }
Douglas Gregorf35f8282009-11-11 21:54:23 +0000511
512 case ActiveTemplateInstantiation::DefaultTemplateArgumentChecking: {
Douglas Gregor54c53cc2011-01-04 23:35:54 +0000513 TemplateParameterList *TemplateParams = 0;
514 if (TemplateDecl *Template = dyn_cast<TemplateDecl>(Active->Template))
515 TemplateParams = Template->getTemplateParameters();
516 else
517 TemplateParams =
518 cast<ClassTemplatePartialSpecializationDecl>(Active->Template)
519 ->getTemplateParameters();
520
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000521 Diags.Report(Active->PointOfInstantiation,
Douglas Gregorf35f8282009-11-11 21:54:23 +0000522 diag::note_template_default_arg_checking)
Douglas Gregor54c53cc2011-01-04 23:35:54 +0000523 << getTemplateArgumentBindingsText(TemplateParams,
Douglas Gregorf35f8282009-11-11 21:54:23 +0000524 Active->TemplateArgs,
525 Active->NumTemplateArgs)
526 << Active->InstantiationRange;
527 break;
528 }
Douglas Gregordf667e72009-03-10 20:44:00 +0000529 }
Douglas Gregoree1828a2009-03-10 18:03:33 +0000530 }
531}
532
Douglas Gregor9b623632010-10-12 23:32:35 +0000533TemplateDeductionInfo *Sema::isSFINAEContext() const {
Douglas Gregor5e9f35c2009-06-14 07:33:30 +0000534 using llvm::SmallVector;
535 for (SmallVector<ActiveTemplateInstantiation, 16>::const_reverse_iterator
536 Active = ActiveTemplateInstantiations.rbegin(),
537 ActiveEnd = ActiveTemplateInstantiations.rend();
538 Active != ActiveEnd;
Douglas Gregorf35f8282009-11-11 21:54:23 +0000539 ++Active)
540 {
Douglas Gregor5e9f35c2009-06-14 07:33:30 +0000541 switch(Active->Kind) {
Douglas Gregorcca9e962009-07-01 22:01:06 +0000542 case ActiveTemplateInstantiation::TemplateInstantiation:
Anders Carlsson25cae7f2009-09-05 05:14:19 +0000543 case ActiveTemplateInstantiation::DefaultFunctionArgumentInstantiation:
Douglas Gregorcca9e962009-07-01 22:01:06 +0000544 // This is a template instantiation, so there is no SFINAE.
Douglas Gregor9b623632010-10-12 23:32:35 +0000545 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000546
Douglas Gregor5e9f35c2009-06-14 07:33:30 +0000547 case ActiveTemplateInstantiation::DefaultTemplateArgumentInstantiation:
Douglas Gregor9148c3f2009-11-11 19:13:48 +0000548 case ActiveTemplateInstantiation::PriorTemplateArgumentSubstitution:
Douglas Gregorf35f8282009-11-11 21:54:23 +0000549 case ActiveTemplateInstantiation::DefaultTemplateArgumentChecking:
Douglas Gregor9148c3f2009-11-11 19:13:48 +0000550 // A default template argument instantiation and substitution into
551 // template parameters with arguments for prior parameters may or may
552 // not be a SFINAE context; look further up the stack.
Douglas Gregor5e9f35c2009-06-14 07:33:30 +0000553 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000554
Douglas Gregorcca9e962009-07-01 22:01:06 +0000555 case ActiveTemplateInstantiation::ExplicitTemplateArgumentSubstitution:
556 case ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution:
557 // We're either substitution explicitly-specified template arguments
558 // or deduced template arguments, so SFINAE applies.
Douglas Gregor9b623632010-10-12 23:32:35 +0000559 assert(Active->DeductionInfo && "Missing deduction info pointer");
560 return Active->DeductionInfo;
Douglas Gregor5e9f35c2009-06-14 07:33:30 +0000561 }
562 }
563
Douglas Gregor9b623632010-10-12 23:32:35 +0000564 return 0;
Douglas Gregor5e9f35c2009-06-14 07:33:30 +0000565}
566
Douglas Gregord3731192011-01-10 07:32:04 +0000567/// \brief Retrieve the depth and index of a parameter pack.
568static std::pair<unsigned, unsigned>
569getDepthAndIndex(NamedDecl *ND) {
570 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(ND))
571 return std::make_pair(TTP->getDepth(), TTP->getIndex());
572
573 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(ND))
574 return std::make_pair(NTTP->getDepth(), NTTP->getIndex());
575
576 TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(ND);
577 return std::make_pair(TTP->getDepth(), TTP->getIndex());
578}
579
Douglas Gregor99ebf652009-02-27 19:31:52 +0000580//===----------------------------------------------------------------------===/
581// Template Instantiation for Types
582//===----------------------------------------------------------------------===/
Douglas Gregorcd281c32009-02-28 00:25:32 +0000583namespace {
Douglas Gregor895162d2010-04-30 18:55:50 +0000584 class TemplateInstantiator : public TreeTransform<TemplateInstantiator> {
Douglas Gregord6350ae2009-08-28 20:31:08 +0000585 const MultiLevelTemplateArgumentList &TemplateArgs;
Douglas Gregorcd281c32009-02-28 00:25:32 +0000586 SourceLocation Loc;
587 DeclarationName Entity;
Douglas Gregor99ebf652009-02-27 19:31:52 +0000588
Douglas Gregorcd281c32009-02-28 00:25:32 +0000589 public:
Douglas Gregor43959a92009-08-20 07:17:43 +0000590 typedef TreeTransform<TemplateInstantiator> inherited;
Mike Stump1eb44332009-09-09 15:08:12 +0000591
592 TemplateInstantiator(Sema &SemaRef,
Douglas Gregord6350ae2009-08-28 20:31:08 +0000593 const MultiLevelTemplateArgumentList &TemplateArgs,
Douglas Gregor577f75a2009-08-04 16:50:30 +0000594 SourceLocation Loc,
Mike Stump1eb44332009-09-09 15:08:12 +0000595 DeclarationName Entity)
596 : inherited(SemaRef), TemplateArgs(TemplateArgs), Loc(Loc),
Douglas Gregor43959a92009-08-20 07:17:43 +0000597 Entity(Entity) { }
Douglas Gregorcd281c32009-02-28 00:25:32 +0000598
Mike Stump1eb44332009-09-09 15:08:12 +0000599 /// \brief Determine whether the given type \p T has already been
Douglas Gregor577f75a2009-08-04 16:50:30 +0000600 /// transformed.
601 ///
602 /// For the purposes of template instantiation, a type has already been
603 /// transformed if it is NULL or if it is not dependent.
Douglas Gregorb4eeaff2010-05-07 23:12:07 +0000604 bool AlreadyTransformed(QualType T);
Mike Stump1eb44332009-09-09 15:08:12 +0000605
Douglas Gregor577f75a2009-08-04 16:50:30 +0000606 /// \brief Returns the location of the entity being instantiated, if known.
607 SourceLocation getBaseLocation() { return Loc; }
Mike Stump1eb44332009-09-09 15:08:12 +0000608
Douglas Gregor577f75a2009-08-04 16:50:30 +0000609 /// \brief Returns the name of the entity being instantiated, if any.
610 DeclarationName getBaseEntity() { return Entity; }
Mike Stump1eb44332009-09-09 15:08:12 +0000611
Douglas Gregor972e6ce2009-10-27 06:26:26 +0000612 /// \brief Sets the "base" location and entity when that
613 /// information is known based on another transformation.
614 void setBase(SourceLocation Loc, DeclarationName Entity) {
615 this->Loc = Loc;
616 this->Entity = Entity;
617 }
Douglas Gregor8491ffe2010-12-20 22:05:00 +0000618
619 bool TryExpandParameterPacks(SourceLocation EllipsisLoc,
620 SourceRange PatternRange,
621 const UnexpandedParameterPack *Unexpanded,
622 unsigned NumUnexpanded,
623 bool &ShouldExpand,
Douglas Gregord3731192011-01-10 07:32:04 +0000624 bool &RetainExpansion,
Douglas Gregorb99268b2010-12-21 00:52:54 +0000625 unsigned &NumExpansions) {
626 return getSema().CheckParameterPacksForExpansion(EllipsisLoc,
627 PatternRange, Unexpanded,
628 NumUnexpanded,
629 TemplateArgs,
630 ShouldExpand,
Douglas Gregord3731192011-01-10 07:32:04 +0000631 RetainExpansion,
Douglas Gregorb99268b2010-12-21 00:52:54 +0000632 NumExpansions);
633 }
Douglas Gregor8491ffe2010-12-20 22:05:00 +0000634
Douglas Gregor12c9c002011-01-07 16:43:16 +0000635 void ExpandingFunctionParameterPack(ParmVarDecl *Pack) {
636 SemaRef.CurrentInstantiationScope->MakeInstantiatedLocalArgPack(Pack);
637 }
638
Douglas Gregord3731192011-01-10 07:32:04 +0000639 TemplateArgument ForgetPartiallySubstitutedPack() {
640 TemplateArgument Result;
641 if (NamedDecl *PartialPack
642 = SemaRef.CurrentInstantiationScope->getPartiallySubstitutedPack()){
643 MultiLevelTemplateArgumentList &TemplateArgs
644 = const_cast<MultiLevelTemplateArgumentList &>(this->TemplateArgs);
645 unsigned Depth, Index;
646 llvm::tie(Depth, Index) = getDepthAndIndex(PartialPack);
647 if (TemplateArgs.hasTemplateArgument(Depth, Index)) {
648 Result = TemplateArgs(Depth, Index);
649 TemplateArgs.setArgument(Depth, Index, TemplateArgument());
650 }
651 }
652
653 return Result;
654 }
655
656 void RememberPartiallySubstitutedPack(TemplateArgument Arg) {
657 if (Arg.isNull())
658 return;
659
660 if (NamedDecl *PartialPack
661 = SemaRef.CurrentInstantiationScope->getPartiallySubstitutedPack()){
662 MultiLevelTemplateArgumentList &TemplateArgs
663 = const_cast<MultiLevelTemplateArgumentList &>(this->TemplateArgs);
664 unsigned Depth, Index;
665 llvm::tie(Depth, Index) = getDepthAndIndex(PartialPack);
666 TemplateArgs.setArgument(Depth, Index, Arg);
667 }
668 }
669
Douglas Gregor577f75a2009-08-04 16:50:30 +0000670 /// \brief Transform the given declaration by instantiating a reference to
671 /// this declaration.
Douglas Gregor7c1e98f2010-03-01 15:56:25 +0000672 Decl *TransformDecl(SourceLocation Loc, Decl *D);
Douglas Gregorb98b1992009-08-11 05:31:07 +0000673
Mike Stump1eb44332009-09-09 15:08:12 +0000674 /// \brief Transform the definition of the given declaration by
Douglas Gregor43959a92009-08-20 07:17:43 +0000675 /// instantiating it.
Douglas Gregoraac571c2010-03-01 17:25:41 +0000676 Decl *TransformDefinition(SourceLocation Loc, Decl *D);
Mike Stump1eb44332009-09-09 15:08:12 +0000677
Douglas Gregor6cd21982009-10-20 05:58:46 +0000678 /// \bried Transform the first qualifier within a scope by instantiating the
679 /// declaration.
680 NamedDecl *TransformFirstQualifierInScope(NamedDecl *D, SourceLocation Loc);
681
Douglas Gregor43959a92009-08-20 07:17:43 +0000682 /// \brief Rebuild the exception declaration and register the declaration
683 /// as an instantiated local.
Douglas Gregor83cb9422010-09-09 17:09:21 +0000684 VarDecl *RebuildExceptionDecl(VarDecl *ExceptionDecl,
John McCalla93c9342009-12-07 02:54:59 +0000685 TypeSourceInfo *Declarator,
Douglas Gregor43959a92009-08-20 07:17:43 +0000686 IdentifierInfo *Name,
Douglas Gregor83cb9422010-09-09 17:09:21 +0000687 SourceLocation Loc);
Mike Stump1eb44332009-09-09 15:08:12 +0000688
Douglas Gregorbe270a02010-04-26 17:57:08 +0000689 /// \brief Rebuild the Objective-C exception declaration and register the
690 /// declaration as an instantiated local.
691 VarDecl *RebuildObjCExceptionDecl(VarDecl *ExceptionDecl,
692 TypeSourceInfo *TSInfo, QualType T);
693
John McCallc4e70192009-09-11 04:59:25 +0000694 /// \brief Check for tag mismatches when instantiating an
695 /// elaborated type.
John McCall21e413f2010-11-04 19:04:38 +0000696 QualType RebuildElaboratedType(SourceLocation KeywordLoc,
697 ElaboratedTypeKeyword Keyword,
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000698 NestedNameSpecifier *NNS, QualType T);
John McCallc4e70192009-09-11 04:59:25 +0000699
John McCall60d7b3a2010-08-24 06:29:42 +0000700 ExprResult TransformPredefinedExpr(PredefinedExpr *E);
701 ExprResult TransformDeclRefExpr(DeclRefExpr *E);
702 ExprResult TransformCXXDefaultArgExpr(CXXDefaultArgExpr *E);
703 ExprResult TransformTemplateParmRefExpr(DeclRefExpr *E,
Douglas Gregor56bc9832010-12-24 00:15:10 +0000704 NonTypeTemplateParmDecl *D);
Sebastian Redla29e51b2009-11-08 13:56:19 +0000705
Douglas Gregor895162d2010-04-30 18:55:50 +0000706 QualType TransformFunctionProtoType(TypeLocBuilder &TLB,
John McCall43fed0d2010-11-12 08:19:04 +0000707 FunctionProtoTypeLoc TL);
John McCall21ef0fa2010-03-11 09:03:00 +0000708 ParmVarDecl *TransformFunctionTypeParam(ParmVarDecl *OldParm);
709
Mike Stump1eb44332009-09-09 15:08:12 +0000710 /// \brief Transforms a template type parameter type by performing
Douglas Gregor577f75a2009-08-04 16:50:30 +0000711 /// substitution of the corresponding template type argument.
John McCalla2becad2009-10-21 00:40:46 +0000712 QualType TransformTemplateTypeParmType(TypeLocBuilder &TLB,
John McCall43fed0d2010-11-12 08:19:04 +0000713 TemplateTypeParmTypeLoc TL);
Nick Lewycky03d98c52010-07-06 19:51:49 +0000714
John McCall60d7b3a2010-08-24 06:29:42 +0000715 ExprResult TransformCallExpr(CallExpr *CE) {
Nick Lewycky03d98c52010-07-06 19:51:49 +0000716 getSema().CallsUndergoingInstantiation.push_back(CE);
John McCall60d7b3a2010-08-24 06:29:42 +0000717 ExprResult Result =
Nick Lewycky03d98c52010-07-06 19:51:49 +0000718 TreeTransform<TemplateInstantiator>::TransformCallExpr(CE);
719 getSema().CallsUndergoingInstantiation.pop_back();
720 return move(Result);
721 }
Douglas Gregor577f75a2009-08-04 16:50:30 +0000722 };
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000723}
724
Douglas Gregorb4eeaff2010-05-07 23:12:07 +0000725bool TemplateInstantiator::AlreadyTransformed(QualType T) {
726 if (T.isNull())
727 return true;
728
Douglas Gregor836adf62010-05-24 17:22:01 +0000729 if (T->isDependentType() || T->isVariablyModifiedType())
Douglas Gregorb4eeaff2010-05-07 23:12:07 +0000730 return false;
731
732 getSema().MarkDeclarationsReferencedInType(Loc, T);
733 return true;
734}
735
Douglas Gregor7c1e98f2010-03-01 15:56:25 +0000736Decl *TemplateInstantiator::TransformDecl(SourceLocation Loc, Decl *D) {
Douglas Gregorc68afe22009-09-03 21:38:09 +0000737 if (!D)
738 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000739
Douglas Gregorc68afe22009-09-03 21:38:09 +0000740 if (TemplateTemplateParmDecl *TTP = dyn_cast<TemplateTemplateParmDecl>(D)) {
Douglas Gregord6350ae2009-08-28 20:31:08 +0000741 if (TTP->getDepth() < TemplateArgs.getNumLevels()) {
Douglas Gregor6d3e6272010-02-05 19:54:12 +0000742 // If the corresponding template argument is NULL or non-existent, it's
743 // because we are performing instantiation from explicitly-specified
744 // template arguments in a function template, but there were some
745 // arguments left unspecified.
746 if (!TemplateArgs.hasTemplateArgument(TTP->getDepth(),
747 TTP->getPosition()))
748 return D;
749
Douglas Gregor61c4d282011-01-05 15:48:55 +0000750 TemplateArgument Arg = TemplateArgs(TTP->getDepth(), TTP->getPosition());
751
752 if (TTP->isParameterPack()) {
753 assert(Arg.getKind() == TemplateArgument::Pack &&
754 "Missing argument pack");
755
756 if (getSema().ArgumentPackSubstitutionIndex == -1) {
757 // FIXME: Variadic templates fun case.
758 getSema().Diag(Loc, diag::err_pack_expansion_mismatch_unsupported);
759 return 0;
760 }
761
Douglas Gregord3731192011-01-10 07:32:04 +0000762 assert(getSema().ArgumentPackSubstitutionIndex < (int)Arg.pack_size());
Douglas Gregor61c4d282011-01-05 15:48:55 +0000763 Arg = Arg.pack_begin()[getSema().ArgumentPackSubstitutionIndex];
764 }
765
766 TemplateName Template = Arg.getAsTemplate();
Douglas Gregor788cd062009-11-11 01:00:40 +0000767 assert(!Template.isNull() && Template.getAsTemplateDecl() &&
Douglas Gregord6350ae2009-08-28 20:31:08 +0000768 "Wrong kind of template template argument");
Douglas Gregor788cd062009-11-11 01:00:40 +0000769 return Template.getAsTemplateDecl();
Douglas Gregord6350ae2009-08-28 20:31:08 +0000770 }
Mike Stump1eb44332009-09-09 15:08:12 +0000771
Douglas Gregor788cd062009-11-11 01:00:40 +0000772 // Fall through to find the instantiated declaration for this template
773 // template parameter.
Douglas Gregord1067e52009-08-06 06:41:21 +0000774 }
Mike Stump1eb44332009-09-09 15:08:12 +0000775
Douglas Gregor7c1e98f2010-03-01 15:56:25 +0000776 return SemaRef.FindInstantiatedDecl(Loc, cast<NamedDecl>(D), TemplateArgs);
Douglas Gregor577f75a2009-08-04 16:50:30 +0000777}
778
Douglas Gregoraac571c2010-03-01 17:25:41 +0000779Decl *TemplateInstantiator::TransformDefinition(SourceLocation Loc, Decl *D) {
John McCallce3ff2b2009-08-25 22:02:44 +0000780 Decl *Inst = getSema().SubstDecl(D, getSema().CurContext, TemplateArgs);
Douglas Gregor43959a92009-08-20 07:17:43 +0000781 if (!Inst)
782 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000783
Douglas Gregor43959a92009-08-20 07:17:43 +0000784 getSema().CurrentInstantiationScope->InstantiatedLocal(D, Inst);
785 return Inst;
786}
787
Douglas Gregor6cd21982009-10-20 05:58:46 +0000788NamedDecl *
789TemplateInstantiator::TransformFirstQualifierInScope(NamedDecl *D,
790 SourceLocation Loc) {
791 // If the first part of the nested-name-specifier was a template type
792 // parameter, instantiate that type parameter down to a tag type.
793 if (TemplateTypeParmDecl *TTPD = dyn_cast_or_null<TemplateTypeParmDecl>(D)) {
794 const TemplateTypeParmType *TTP
795 = cast<TemplateTypeParmType>(getSema().Context.getTypeDeclType(TTPD));
Douglas Gregor984a58b2010-12-20 22:48:17 +0000796
Douglas Gregor6cd21982009-10-20 05:58:46 +0000797 if (TTP->getDepth() < TemplateArgs.getNumLevels()) {
Douglas Gregor984a58b2010-12-20 22:48:17 +0000798 // FIXME: This needs testing w/ member access expressions.
799 TemplateArgument Arg = TemplateArgs(TTP->getDepth(), TTP->getIndex());
800
801 if (TTP->isParameterPack()) {
802 assert(Arg.getKind() == TemplateArgument::Pack &&
803 "Missing argument pack");
804
805 if (getSema().ArgumentPackSubstitutionIndex == -1) {
806 // FIXME: Variadic templates fun case.
807 getSema().Diag(Loc, diag::err_pack_expansion_mismatch_unsupported);
808 return 0;
809 }
810
Douglas Gregord3731192011-01-10 07:32:04 +0000811 assert(getSema().ArgumentPackSubstitutionIndex < (int)Arg.pack_size());
Douglas Gregor984a58b2010-12-20 22:48:17 +0000812 Arg = Arg.pack_begin()[getSema().ArgumentPackSubstitutionIndex];
813 }
814
815 QualType T = Arg.getAsType();
Douglas Gregor6cd21982009-10-20 05:58:46 +0000816 if (T.isNull())
Douglas Gregor7c1e98f2010-03-01 15:56:25 +0000817 return cast_or_null<NamedDecl>(TransformDecl(Loc, D));
Douglas Gregor6cd21982009-10-20 05:58:46 +0000818
819 if (const TagType *Tag = T->getAs<TagType>())
820 return Tag->getDecl();
821
822 // The resulting type is not a tag; complain.
823 getSema().Diag(Loc, diag::err_nested_name_spec_non_tag) << T;
824 return 0;
825 }
826 }
827
Douglas Gregor7c1e98f2010-03-01 15:56:25 +0000828 return cast_or_null<NamedDecl>(TransformDecl(Loc, D));
Douglas Gregor6cd21982009-10-20 05:58:46 +0000829}
830
Douglas Gregor43959a92009-08-20 07:17:43 +0000831VarDecl *
832TemplateInstantiator::RebuildExceptionDecl(VarDecl *ExceptionDecl,
John McCalla93c9342009-12-07 02:54:59 +0000833 TypeSourceInfo *Declarator,
Douglas Gregor43959a92009-08-20 07:17:43 +0000834 IdentifierInfo *Name,
Douglas Gregor83cb9422010-09-09 17:09:21 +0000835 SourceLocation Loc) {
836 VarDecl *Var = inherited::RebuildExceptionDecl(ExceptionDecl, Declarator,
837 Name, Loc);
Douglas Gregorbe270a02010-04-26 17:57:08 +0000838 if (Var)
839 getSema().CurrentInstantiationScope->InstantiatedLocal(ExceptionDecl, Var);
840 return Var;
841}
842
843VarDecl *TemplateInstantiator::RebuildObjCExceptionDecl(VarDecl *ExceptionDecl,
844 TypeSourceInfo *TSInfo,
845 QualType T) {
846 VarDecl *Var = inherited::RebuildObjCExceptionDecl(ExceptionDecl, TSInfo, T);
847 if (Var)
Douglas Gregor43959a92009-08-20 07:17:43 +0000848 getSema().CurrentInstantiationScope->InstantiatedLocal(ExceptionDecl, Var);
849 return Var;
850}
851
John McCallc4e70192009-09-11 04:59:25 +0000852QualType
John McCall21e413f2010-11-04 19:04:38 +0000853TemplateInstantiator::RebuildElaboratedType(SourceLocation KeywordLoc,
854 ElaboratedTypeKeyword Keyword,
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000855 NestedNameSpecifier *NNS,
856 QualType T) {
John McCallc4e70192009-09-11 04:59:25 +0000857 if (const TagType *TT = T->getAs<TagType>()) {
858 TagDecl* TD = TT->getDecl();
859
John McCall21e413f2010-11-04 19:04:38 +0000860 SourceLocation TagLocation = KeywordLoc;
John McCallc4e70192009-09-11 04:59:25 +0000861
862 // FIXME: type might be anonymous.
863 IdentifierInfo *Id = TD->getIdentifier();
864
865 // TODO: should we even warn on struct/class mismatches for this? Seems
866 // like it's likely to produce a lot of spurious errors.
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000867 if (Keyword != ETK_None && Keyword != ETK_Typename) {
868 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForKeyword(Keyword);
869 if (!SemaRef.isAcceptableTagRedeclaration(TD, Kind, TagLocation, *Id)) {
870 SemaRef.Diag(TagLocation, diag::err_use_with_wrong_tag)
871 << Id
872 << FixItHint::CreateReplacement(SourceRange(TagLocation),
873 TD->getKindName());
874 SemaRef.Diag(TD->getLocation(), diag::note_previous_use);
875 }
John McCallc4e70192009-09-11 04:59:25 +0000876 }
877 }
878
John McCall21e413f2010-11-04 19:04:38 +0000879 return TreeTransform<TemplateInstantiator>::RebuildElaboratedType(KeywordLoc,
880 Keyword,
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000881 NNS, T);
John McCallc4e70192009-09-11 04:59:25 +0000882}
883
John McCall60d7b3a2010-08-24 06:29:42 +0000884ExprResult
John McCall454feb92009-12-08 09:21:05 +0000885TemplateInstantiator::TransformPredefinedExpr(PredefinedExpr *E) {
Anders Carlsson773f3972009-09-11 01:22:35 +0000886 if (!E->isTypeDependent())
John McCall3fa5cae2010-10-26 07:05:15 +0000887 return SemaRef.Owned(E);
Anders Carlsson773f3972009-09-11 01:22:35 +0000888
889 FunctionDecl *currentDecl = getSema().getCurFunctionDecl();
890 assert(currentDecl && "Must have current function declaration when "
891 "instantiating.");
892
893 PredefinedExpr::IdentType IT = E->getIdentType();
894
Anders Carlsson848fa642010-02-11 18:20:28 +0000895 unsigned Length = PredefinedExpr::ComputeName(IT, currentDecl).length();
Anders Carlsson773f3972009-09-11 01:22:35 +0000896
897 llvm::APInt LengthI(32, Length + 1);
John McCall0953e762009-09-24 19:53:00 +0000898 QualType ResTy = getSema().Context.CharTy.withConst();
Anders Carlsson773f3972009-09-11 01:22:35 +0000899 ResTy = getSema().Context.getConstantArrayType(ResTy, LengthI,
900 ArrayType::Normal, 0);
901 PredefinedExpr *PE =
902 new (getSema().Context) PredefinedExpr(E->getLocation(), ResTy, IT);
903 return getSema().Owned(PE);
904}
905
John McCall60d7b3a2010-08-24 06:29:42 +0000906ExprResult
John McCallb8fc0532010-02-06 08:42:39 +0000907TemplateInstantiator::TransformTemplateParmRefExpr(DeclRefExpr *E,
Douglas Gregordcee9802010-02-08 23:41:45 +0000908 NonTypeTemplateParmDecl *NTTP) {
John McCallb8fc0532010-02-06 08:42:39 +0000909 // If the corresponding template argument is NULL or non-existent, it's
910 // because we are performing instantiation from explicitly-specified
911 // template arguments in a function template, but there were some
912 // arguments left unspecified.
913 if (!TemplateArgs.hasTemplateArgument(NTTP->getDepth(),
914 NTTP->getPosition()))
John McCall3fa5cae2010-10-26 07:05:15 +0000915 return SemaRef.Owned(E);
Mike Stump1eb44332009-09-09 15:08:12 +0000916
Douglas Gregor56bc9832010-12-24 00:15:10 +0000917 TemplateArgument Arg = TemplateArgs(NTTP->getDepth(), NTTP->getPosition());
918 if (NTTP->isParameterPack()) {
919 assert(Arg.getKind() == TemplateArgument::Pack &&
920 "Missing argument pack");
921
922 if (getSema().ArgumentPackSubstitutionIndex == -1) {
923 // FIXME: Variadic templates fun case.
924 getSema().Diag(Loc, diag::err_pack_expansion_mismatch_unsupported);
925 return ExprError();
926 }
927
Douglas Gregord3731192011-01-10 07:32:04 +0000928 assert(getSema().ArgumentPackSubstitutionIndex < (int)Arg.pack_size());
Douglas Gregor56bc9832010-12-24 00:15:10 +0000929 Arg = Arg.pack_begin()[getSema().ArgumentPackSubstitutionIndex];
930 }
Mike Stump1eb44332009-09-09 15:08:12 +0000931
John McCallb8fc0532010-02-06 08:42:39 +0000932 // The template argument itself might be an expression, in which
933 // case we just return that expression.
934 if (Arg.getKind() == TemplateArgument::Expression)
John McCall3fa5cae2010-10-26 07:05:15 +0000935 return SemaRef.Owned(Arg.getAsExpr());
Mike Stump1eb44332009-09-09 15:08:12 +0000936
John McCallb8fc0532010-02-06 08:42:39 +0000937 if (Arg.getKind() == TemplateArgument::Declaration) {
938 ValueDecl *VD = cast<ValueDecl>(Arg.getAsDecl());
Mike Stump1eb44332009-09-09 15:08:12 +0000939
John McCall645cf442010-02-06 10:23:53 +0000940 // Find the instantiation of the template argument. This is
941 // required for nested templates.
John McCallb8fc0532010-02-06 08:42:39 +0000942 VD = cast_or_null<ValueDecl>(
Douglas Gregor7c1e98f2010-03-01 15:56:25 +0000943 getSema().FindInstantiatedDecl(E->getLocation(),
944 VD, TemplateArgs));
John McCallb8fc0532010-02-06 08:42:39 +0000945 if (!VD)
John McCallf312b1e2010-08-26 23:41:50 +0000946 return ExprError();
Mike Stump1eb44332009-09-09 15:08:12 +0000947
John McCall645cf442010-02-06 10:23:53 +0000948 // Derive the type we want the substituted decl to have. This had
949 // better be non-dependent, or these checks will have serious problems.
950 QualType TargetType = SemaRef.SubstType(NTTP->getType(), TemplateArgs,
Douglas Gregordcee9802010-02-08 23:41:45 +0000951 E->getLocation(),
952 DeclarationName());
John McCall645cf442010-02-06 10:23:53 +0000953 assert(!TargetType.isNull() && "type substitution failed for param type");
954 assert(!TargetType->isDependentType() && "param type still dependent");
Douglas Gregor02024a92010-03-28 02:42:43 +0000955 return SemaRef.BuildExpressionFromDeclTemplateArgument(Arg,
956 TargetType,
957 E->getLocation());
John McCallb8fc0532010-02-06 08:42:39 +0000958 }
959
Douglas Gregor02024a92010-03-28 02:42:43 +0000960 return SemaRef.BuildExpressionFromIntegralTemplateArgument(Arg,
961 E->getSourceRange().getBegin());
John McCallb8fc0532010-02-06 08:42:39 +0000962}
963
964
John McCall60d7b3a2010-08-24 06:29:42 +0000965ExprResult
John McCallb8fc0532010-02-06 08:42:39 +0000966TemplateInstantiator::TransformDeclRefExpr(DeclRefExpr *E) {
967 NamedDecl *D = E->getDecl();
968 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(D)) {
969 if (NTTP->getDepth() < TemplateArgs.getNumLevels())
970 return TransformTemplateParmRefExpr(E, NTTP);
Douglas Gregor550d9b22009-10-31 17:21:17 +0000971
972 // We have a non-type template parameter that isn't fully substituted;
973 // FindInstantiatedDecl will find it in the local instantiation scope.
Douglas Gregorb98b1992009-08-11 05:31:07 +0000974 }
Mike Stump1eb44332009-09-09 15:08:12 +0000975
John McCall454feb92009-12-08 09:21:05 +0000976 return TreeTransform<TemplateInstantiator>::TransformDeclRefExpr(E);
Douglas Gregorb98b1992009-08-11 05:31:07 +0000977}
978
John McCall60d7b3a2010-08-24 06:29:42 +0000979ExprResult TemplateInstantiator::TransformCXXDefaultArgExpr(
John McCall454feb92009-12-08 09:21:05 +0000980 CXXDefaultArgExpr *E) {
Sebastian Redla29e51b2009-11-08 13:56:19 +0000981 assert(!cast<FunctionDecl>(E->getParam()->getDeclContext())->
982 getDescribedFunctionTemplate() &&
983 "Default arg expressions are never formed in dependent cases.");
Douglas Gregor036aed12009-12-23 23:03:06 +0000984 return SemaRef.BuildCXXDefaultArgExpr(E->getUsedLocation(),
985 cast<FunctionDecl>(E->getParam()->getDeclContext()),
986 E->getParam());
Sebastian Redla29e51b2009-11-08 13:56:19 +0000987}
988
Douglas Gregor895162d2010-04-30 18:55:50 +0000989QualType TemplateInstantiator::TransformFunctionProtoType(TypeLocBuilder &TLB,
John McCall43fed0d2010-11-12 08:19:04 +0000990 FunctionProtoTypeLoc TL) {
Douglas Gregor895162d2010-04-30 18:55:50 +0000991 // We need a local instantiation scope for this function prototype.
John McCall2a7fb272010-08-25 05:32:35 +0000992 LocalInstantiationScope Scope(SemaRef, /*CombineWithOuterScope=*/true);
John McCall43fed0d2010-11-12 08:19:04 +0000993 return inherited::TransformFunctionProtoType(TLB, TL);
John McCall21ef0fa2010-03-11 09:03:00 +0000994}
995
996ParmVarDecl *
997TemplateInstantiator::TransformFunctionTypeParam(ParmVarDecl *OldParm) {
Douglas Gregorcb27b0f2010-04-12 07:48:19 +0000998 return SemaRef.SubstParmVarDecl(OldParm, TemplateArgs);
John McCall21ef0fa2010-03-11 09:03:00 +0000999}
1000
Mike Stump1eb44332009-09-09 15:08:12 +00001001QualType
John McCalla2becad2009-10-21 00:40:46 +00001002TemplateInstantiator::TransformTemplateTypeParmType(TypeLocBuilder &TLB,
John McCall43fed0d2010-11-12 08:19:04 +00001003 TemplateTypeParmTypeLoc TL) {
John McCalla2becad2009-10-21 00:40:46 +00001004 TemplateTypeParmType *T = TL.getTypePtr();
Douglas Gregord6350ae2009-08-28 20:31:08 +00001005 if (T->getDepth() < TemplateArgs.getNumLevels()) {
Douglas Gregor99ebf652009-02-27 19:31:52 +00001006 // Replace the template type parameter with its corresponding
1007 // template argument.
Mike Stump1eb44332009-09-09 15:08:12 +00001008
1009 // If the corresponding template argument is NULL or doesn't exist, it's
1010 // because we are performing instantiation from explicitly-specified
1011 // template arguments in a function template class, but there were some
Douglas Gregor16134c62009-07-01 00:28:38 +00001012 // arguments left unspecified.
John McCalla2becad2009-10-21 00:40:46 +00001013 if (!TemplateArgs.hasTemplateArgument(T->getDepth(), T->getIndex())) {
1014 TemplateTypeParmTypeLoc NewTL
1015 = TLB.push<TemplateTypeParmTypeLoc>(TL.getType());
1016 NewTL.setNameLoc(TL.getNameLoc());
1017 return TL.getType();
1018 }
Mike Stump1eb44332009-09-09 15:08:12 +00001019
Douglas Gregor8491ffe2010-12-20 22:05:00 +00001020 TemplateArgument Arg = TemplateArgs(T->getDepth(), T->getIndex());
1021
1022 if (T->isParameterPack()) {
1023 assert(Arg.getKind() == TemplateArgument::Pack &&
1024 "Missing argument pack");
1025
1026 if (getSema().ArgumentPackSubstitutionIndex == -1) {
1027 // FIXME: Variadic templates fun case.
1028 getSema().Diag(TL.getSourceRange().getBegin(),
1029 diag::err_pack_expansion_mismatch_unsupported);
1030 return QualType();
1031 }
1032
Douglas Gregord3731192011-01-10 07:32:04 +00001033 assert(getSema().ArgumentPackSubstitutionIndex < (int)Arg.pack_size());
Douglas Gregor8491ffe2010-12-20 22:05:00 +00001034 Arg = Arg.pack_begin()[getSema().ArgumentPackSubstitutionIndex];
1035 }
1036
1037 assert(Arg.getKind() == TemplateArgument::Type &&
Douglas Gregor99ebf652009-02-27 19:31:52 +00001038 "Template argument kind mismatch");
Douglas Gregord6350ae2009-08-28 20:31:08 +00001039
Douglas Gregor8491ffe2010-12-20 22:05:00 +00001040 QualType Replacement = Arg.getAsType();
John McCall49a832b2009-10-18 09:09:24 +00001041
1042 // TODO: only do this uniquing once, at the start of instantiation.
John McCalla2becad2009-10-21 00:40:46 +00001043 QualType Result
1044 = getSema().Context.getSubstTemplateTypeParmType(T, Replacement);
1045 SubstTemplateTypeParmTypeLoc NewTL
1046 = TLB.push<SubstTemplateTypeParmTypeLoc>(Result);
1047 NewTL.setNameLoc(TL.getNameLoc());
1048 return Result;
Mike Stump1eb44332009-09-09 15:08:12 +00001049 }
Douglas Gregor99ebf652009-02-27 19:31:52 +00001050
1051 // The template type parameter comes from an inner template (e.g.,
1052 // the template parameter list of a member template inside the
1053 // template we are instantiating). Create a new template type
1054 // parameter with the template "level" reduced by one.
John McCalla2becad2009-10-21 00:40:46 +00001055 QualType Result
1056 = getSema().Context.getTemplateTypeParmType(T->getDepth()
1057 - TemplateArgs.getNumLevels(),
1058 T->getIndex(),
1059 T->isParameterPack(),
Douglas Gregorefed5c82010-06-16 15:23:05 +00001060 T->getName());
John McCalla2becad2009-10-21 00:40:46 +00001061 TemplateTypeParmTypeLoc NewTL = TLB.push<TemplateTypeParmTypeLoc>(Result);
1062 NewTL.setNameLoc(TL.getNameLoc());
1063 return Result;
Douglas Gregorcd281c32009-02-28 00:25:32 +00001064}
Douglas Gregor99ebf652009-02-27 19:31:52 +00001065
John McCallce3ff2b2009-08-25 22:02:44 +00001066/// \brief Perform substitution on the type T with a given set of template
1067/// arguments.
Douglas Gregor99ebf652009-02-27 19:31:52 +00001068///
1069/// This routine substitutes the given template arguments into the
1070/// type T and produces the instantiated type.
1071///
1072/// \param T the type into which the template arguments will be
1073/// substituted. If this type is not dependent, it will be returned
1074/// immediately.
1075///
1076/// \param TemplateArgs the template arguments that will be
1077/// substituted for the top-level template parameters within T.
1078///
Douglas Gregor99ebf652009-02-27 19:31:52 +00001079/// \param Loc the location in the source code where this substitution
1080/// is being performed. It will typically be the location of the
1081/// declarator (if we're instantiating the type of some declaration)
1082/// or the location of the type in the source code (if, e.g., we're
1083/// instantiating the type of a cast expression).
1084///
1085/// \param Entity the name of the entity associated with a declaration
1086/// being instantiated (if any). May be empty to indicate that there
1087/// is no such entity (if, e.g., this is a type that occurs as part of
1088/// a cast expression) or that the entity has no name (e.g., an
1089/// unnamed function parameter).
1090///
1091/// \returns If the instantiation succeeds, the instantiated
1092/// type. Otherwise, produces diagnostics and returns a NULL type.
John McCalla93c9342009-12-07 02:54:59 +00001093TypeSourceInfo *Sema::SubstType(TypeSourceInfo *T,
John McCallcd7ba1c2009-10-21 00:58:09 +00001094 const MultiLevelTemplateArgumentList &Args,
1095 SourceLocation Loc,
1096 DeclarationName Entity) {
1097 assert(!ActiveTemplateInstantiations.empty() &&
1098 "Cannot perform an instantiation without some context on the "
1099 "instantiation stack");
1100
Douglas Gregor836adf62010-05-24 17:22:01 +00001101 if (!T->getType()->isDependentType() &&
1102 !T->getType()->isVariablyModifiedType())
John McCallcd7ba1c2009-10-21 00:58:09 +00001103 return T;
1104
1105 TemplateInstantiator Instantiator(*this, Args, Loc, Entity);
1106 return Instantiator.TransformType(T);
1107}
1108
Douglas Gregor603cfb42011-01-05 23:12:31 +00001109TypeSourceInfo *Sema::SubstType(TypeLoc TL,
1110 const MultiLevelTemplateArgumentList &Args,
1111 SourceLocation Loc,
1112 DeclarationName Entity) {
1113 assert(!ActiveTemplateInstantiations.empty() &&
1114 "Cannot perform an instantiation without some context on the "
1115 "instantiation stack");
1116
1117 if (TL.getType().isNull())
1118 return 0;
1119
1120 if (!TL.getType()->isDependentType() &&
1121 !TL.getType()->isVariablyModifiedType()) {
1122 // FIXME: Make a copy of the TypeLoc data here, so that we can
1123 // return a new TypeSourceInfo. Inefficient!
1124 TypeLocBuilder TLB;
1125 TLB.pushFullCopy(TL);
1126 return TLB.getTypeSourceInfo(Context, TL.getType());
1127 }
1128
1129 TemplateInstantiator Instantiator(*this, Args, Loc, Entity);
1130 TypeLocBuilder TLB;
1131 TLB.reserve(TL.getFullDataSize());
1132 QualType Result = Instantiator.TransformType(TLB, TL);
1133 if (Result.isNull())
1134 return 0;
1135
1136 return TLB.getTypeSourceInfo(Context, Result);
1137}
1138
John McCallcd7ba1c2009-10-21 00:58:09 +00001139/// Deprecated form of the above.
Mike Stump1eb44332009-09-09 15:08:12 +00001140QualType Sema::SubstType(QualType T,
Douglas Gregord6350ae2009-08-28 20:31:08 +00001141 const MultiLevelTemplateArgumentList &TemplateArgs,
John McCallce3ff2b2009-08-25 22:02:44 +00001142 SourceLocation Loc, DeclarationName Entity) {
Douglas Gregordf667e72009-03-10 20:44:00 +00001143 assert(!ActiveTemplateInstantiations.empty() &&
1144 "Cannot perform an instantiation without some context on the "
1145 "instantiation stack");
1146
Douglas Gregor836adf62010-05-24 17:22:01 +00001147 // If T is not a dependent type or a variably-modified type, there
1148 // is nothing to do.
1149 if (!T->isDependentType() && !T->isVariablyModifiedType())
Douglas Gregor99ebf652009-02-27 19:31:52 +00001150 return T;
1151
Douglas Gregor577f75a2009-08-04 16:50:30 +00001152 TemplateInstantiator Instantiator(*this, TemplateArgs, Loc, Entity);
1153 return Instantiator.TransformType(T);
Douglas Gregor99ebf652009-02-27 19:31:52 +00001154}
Douglas Gregor2943aed2009-03-03 04:44:36 +00001155
John McCall6cd3b9f2010-04-09 17:38:44 +00001156static bool NeedsInstantiationAsFunctionType(TypeSourceInfo *T) {
Douglas Gregor836adf62010-05-24 17:22:01 +00001157 if (T->getType()->isDependentType() || T->getType()->isVariablyModifiedType())
John McCall6cd3b9f2010-04-09 17:38:44 +00001158 return true;
1159
Abramo Bagnara723df242010-12-14 22:11:44 +00001160 TypeLoc TL = T->getTypeLoc().IgnoreParens();
John McCall6cd3b9f2010-04-09 17:38:44 +00001161 if (!isa<FunctionProtoTypeLoc>(TL))
1162 return false;
1163
1164 FunctionProtoTypeLoc FP = cast<FunctionProtoTypeLoc>(TL);
1165 for (unsigned I = 0, E = FP.getNumArgs(); I != E; ++I) {
1166 ParmVarDecl *P = FP.getArg(I);
1167
1168 // TODO: currently we always rebuild expressions. When we
1169 // properly get lazier about this, we should use the same
1170 // logic to avoid rebuilding prototypes here.
Douglas Gregor7b1cf302011-01-05 21:14:17 +00001171 if (P->hasDefaultArg())
John McCall6cd3b9f2010-04-09 17:38:44 +00001172 return true;
1173 }
1174
1175 return false;
1176}
1177
1178/// A form of SubstType intended specifically for instantiating the
1179/// type of a FunctionDecl. Its purpose is solely to force the
1180/// instantiation of default-argument expressions.
1181TypeSourceInfo *Sema::SubstFunctionDeclType(TypeSourceInfo *T,
1182 const MultiLevelTemplateArgumentList &Args,
1183 SourceLocation Loc,
1184 DeclarationName Entity) {
1185 assert(!ActiveTemplateInstantiations.empty() &&
1186 "Cannot perform an instantiation without some context on the "
1187 "instantiation stack");
1188
1189 if (!NeedsInstantiationAsFunctionType(T))
1190 return T;
1191
1192 TemplateInstantiator Instantiator(*this, Args, Loc, Entity);
1193
1194 TypeLocBuilder TLB;
1195
1196 TypeLoc TL = T->getTypeLoc();
1197 TLB.reserve(TL.getFullDataSize());
1198
John McCall43fed0d2010-11-12 08:19:04 +00001199 QualType Result = Instantiator.TransformType(TLB, TL);
John McCall6cd3b9f2010-04-09 17:38:44 +00001200 if (Result.isNull())
1201 return 0;
1202
1203 return TLB.getTypeSourceInfo(Context, Result);
1204}
1205
Douglas Gregorcb27b0f2010-04-12 07:48:19 +00001206ParmVarDecl *Sema::SubstParmVarDecl(ParmVarDecl *OldParm,
1207 const MultiLevelTemplateArgumentList &TemplateArgs) {
1208 TypeSourceInfo *OldDI = OldParm->getTypeSourceInfo();
Douglas Gregor603cfb42011-01-05 23:12:31 +00001209 TypeSourceInfo *NewDI = 0;
1210
Douglas Gregor603cfb42011-01-05 23:12:31 +00001211 TypeLoc OldTL = OldDI->getTypeLoc();
1212 if (isa<PackExpansionTypeLoc>(OldTL)) {
1213 PackExpansionTypeLoc ExpansionTL = cast<PackExpansionTypeLoc>(OldTL);
Douglas Gregor603cfb42011-01-05 23:12:31 +00001214
1215 // We have a function parameter pack. Substitute into the pattern of the
1216 // expansion.
1217 NewDI = SubstType(ExpansionTL.getPatternLoc(), TemplateArgs,
1218 OldParm->getLocation(), OldParm->getDeclName());
1219 if (!NewDI)
1220 return 0;
1221
1222 if (NewDI->getType()->containsUnexpandedParameterPack()) {
1223 // We still have unexpanded parameter packs, which means that
1224 // our function parameter is still a function parameter pack.
1225 // Therefore, make its type a pack expansion type.
1226 NewDI = CheckPackExpansion(NewDI, ExpansionTL.getEllipsisLoc());
Douglas Gregor603cfb42011-01-05 23:12:31 +00001227 }
1228 } else {
1229 NewDI = SubstType(OldDI, TemplateArgs, OldParm->getLocation(),
1230 OldParm->getDeclName());
1231 }
1232
Douglas Gregorcb27b0f2010-04-12 07:48:19 +00001233 if (!NewDI)
1234 return 0;
1235
1236 if (NewDI->getType()->isVoidType()) {
1237 Diag(OldParm->getLocation(), diag::err_param_with_void_type);
1238 return 0;
1239 }
1240
1241 ParmVarDecl *NewParm = CheckParameter(Context.getTranslationUnitDecl(),
1242 NewDI, NewDI->getType(),
1243 OldParm->getIdentifier(),
1244 OldParm->getLocation(),
Douglas Gregor16573fa2010-04-19 22:54:31 +00001245 OldParm->getStorageClass(),
1246 OldParm->getStorageClassAsWritten());
Douglas Gregorcb27b0f2010-04-12 07:48:19 +00001247 if (!NewParm)
1248 return 0;
Douglas Gregor4469e8a2010-05-19 17:02:24 +00001249
Douglas Gregorcb27b0f2010-04-12 07:48:19 +00001250 // Mark the (new) default argument as uninstantiated (if any).
1251 if (OldParm->hasUninstantiatedDefaultArg()) {
1252 Expr *Arg = OldParm->getUninstantiatedDefaultArg();
1253 NewParm->setUninstantiatedDefaultArg(Arg);
Douglas Gregor8cfb7a32010-10-12 18:23:32 +00001254 } else if (OldParm->hasUnparsedDefaultArg()) {
1255 NewParm->setUnparsedDefaultArg();
1256 UnparsedDefaultArgInstantiations[OldParm].push_back(NewParm);
Douglas Gregorcb27b0f2010-04-12 07:48:19 +00001257 } else if (Expr *Arg = OldParm->getDefaultArg())
1258 NewParm->setUninstantiatedDefaultArg(Arg);
1259
1260 NewParm->setHasInheritedDefaultArg(OldParm->hasInheritedDefaultArg());
1261
Douglas Gregor603cfb42011-01-05 23:12:31 +00001262 // FIXME: When OldParm is a parameter pack and NewParm is not a parameter
1263 // pack, we actually have a set of instantiated locations. Maintain this set!
Douglas Gregor12c9c002011-01-07 16:43:16 +00001264 if (OldParm->isParameterPack() && !NewParm->isParameterPack()) {
1265 // Add the new parameter to
1266 CurrentInstantiationScope->InstantiatedLocalPackArg(OldParm, NewParm);
1267 } else {
1268 // Introduce an Old -> New mapping
Douglas Gregor603cfb42011-01-05 23:12:31 +00001269 CurrentInstantiationScope->InstantiatedLocal(OldParm, NewParm);
Douglas Gregor12c9c002011-01-07 16:43:16 +00001270 }
Douglas Gregor603cfb42011-01-05 23:12:31 +00001271
Argyrios Kyrtzidise3041be2010-07-19 10:14:41 +00001272 // FIXME: OldParm may come from a FunctionProtoType, in which case CurContext
1273 // can be anything, is this right ?
Fariborz Jahanian55a17c02010-07-13 21:05:02 +00001274 NewParm->setDeclContext(CurContext);
Fariborz Jahaniane7ffbe22010-07-13 20:05:58 +00001275
Douglas Gregorcb27b0f2010-04-12 07:48:19 +00001276 return NewParm;
1277}
1278
Douglas Gregora009b592011-01-07 00:20:55 +00001279/// \brief Substitute the given template arguments into the given set of
1280/// parameters, producing the set of parameter types that would be generated
1281/// from such a substitution.
1282bool Sema::SubstParmTypes(SourceLocation Loc,
1283 ParmVarDecl **Params, unsigned NumParams,
1284 const MultiLevelTemplateArgumentList &TemplateArgs,
Douglas Gregor12c9c002011-01-07 16:43:16 +00001285 llvm::SmallVectorImpl<QualType> &ParamTypes,
1286 llvm::SmallVectorImpl<ParmVarDecl *> *OutParams) {
Douglas Gregora009b592011-01-07 00:20:55 +00001287 assert(!ActiveTemplateInstantiations.empty() &&
1288 "Cannot perform an instantiation without some context on the "
1289 "instantiation stack");
1290
1291 TemplateInstantiator Instantiator(*this, TemplateArgs, Loc,
1292 DeclarationName());
1293 return Instantiator.TransformFunctionTypeParams(Loc, Params, NumParams, 0,
Douglas Gregor12c9c002011-01-07 16:43:16 +00001294 ParamTypes, OutParams);
Douglas Gregora009b592011-01-07 00:20:55 +00001295}
1296
John McCallce3ff2b2009-08-25 22:02:44 +00001297/// \brief Perform substitution on the base class specifiers of the
1298/// given class template specialization.
Douglas Gregor2943aed2009-03-03 04:44:36 +00001299///
1300/// Produces a diagnostic and returns true on error, returns false and
1301/// attaches the instantiated base classes to the class template
1302/// specialization if successful.
Mike Stump1eb44332009-09-09 15:08:12 +00001303bool
John McCallce3ff2b2009-08-25 22:02:44 +00001304Sema::SubstBaseSpecifiers(CXXRecordDecl *Instantiation,
1305 CXXRecordDecl *Pattern,
Douglas Gregord6350ae2009-08-28 20:31:08 +00001306 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor2943aed2009-03-03 04:44:36 +00001307 bool Invalid = false;
Douglas Gregor0ca20ac2009-05-29 18:27:38 +00001308 llvm::SmallVector<CXXBaseSpecifier*, 4> InstantiatedBases;
Mike Stump1eb44332009-09-09 15:08:12 +00001309 for (ClassTemplateSpecializationDecl::base_class_iterator
Douglas Gregord475b8d2009-03-25 21:17:03 +00001310 Base = Pattern->bases_begin(), BaseEnd = Pattern->bases_end();
Douglas Gregor27b152f2009-03-10 18:52:44 +00001311 Base != BaseEnd; ++Base) {
Douglas Gregor2943aed2009-03-03 04:44:36 +00001312 if (!Base->getType()->isDependentType()) {
Fariborz Jahanian71c6e712009-07-22 17:41:53 +00001313 InstantiatedBases.push_back(new (Context) CXXBaseSpecifier(*Base));
Douglas Gregor2943aed2009-03-03 04:44:36 +00001314 continue;
1315 }
1316
Douglas Gregorf90b27a2011-01-03 22:36:02 +00001317 SourceLocation EllipsisLoc;
1318 if (Base->isPackExpansion()) {
1319 // This is a pack expansion. See whether we should expand it now, or
1320 // wait until later.
1321 llvm::SmallVector<UnexpandedParameterPack, 2> Unexpanded;
1322 collectUnexpandedParameterPacks(Base->getTypeSourceInfo()->getTypeLoc(),
1323 Unexpanded);
1324 bool ShouldExpand = false;
Douglas Gregord3731192011-01-10 07:32:04 +00001325 bool RetainExpansion = false;
Douglas Gregorf90b27a2011-01-03 22:36:02 +00001326 unsigned NumExpansions = 0;
1327 if (CheckParameterPacksForExpansion(Base->getEllipsisLoc(),
1328 Base->getSourceRange(),
1329 Unexpanded.data(), Unexpanded.size(),
1330 TemplateArgs, ShouldExpand,
Douglas Gregord3731192011-01-10 07:32:04 +00001331 RetainExpansion,
Douglas Gregorf90b27a2011-01-03 22:36:02 +00001332 NumExpansions)) {
Douglas Gregorf90b27a2011-01-03 22:36:02 +00001333 Invalid = true;
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00001334 continue;
Douglas Gregorf90b27a2011-01-03 22:36:02 +00001335 }
1336
1337 // If we should expand this pack expansion now, do so.
1338 if (ShouldExpand) {
1339 for (unsigned I = 0; I != NumExpansions; ++I) {
1340 Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(*this, I);
1341
1342 TypeSourceInfo *BaseTypeLoc = SubstType(Base->getTypeSourceInfo(),
1343 TemplateArgs,
1344 Base->getSourceRange().getBegin(),
1345 DeclarationName());
1346 if (!BaseTypeLoc) {
1347 Invalid = true;
1348 continue;
1349 }
1350
1351 if (CXXBaseSpecifier *InstantiatedBase
1352 = CheckBaseSpecifier(Instantiation,
1353 Base->getSourceRange(),
1354 Base->isVirtual(),
1355 Base->getAccessSpecifierAsWritten(),
1356 BaseTypeLoc,
1357 SourceLocation()))
1358 InstantiatedBases.push_back(InstantiatedBase);
1359 else
1360 Invalid = true;
1361 }
1362
1363 continue;
1364 }
1365
1366 // The resulting base specifier will (still) be a pack expansion.
1367 EllipsisLoc = Base->getEllipsisLoc();
1368 }
1369
1370 Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(*this, -1);
Nick Lewycky56062202010-07-26 16:56:01 +00001371 TypeSourceInfo *BaseTypeLoc = SubstType(Base->getTypeSourceInfo(),
1372 TemplateArgs,
1373 Base->getSourceRange().getBegin(),
1374 DeclarationName());
1375 if (!BaseTypeLoc) {
Douglas Gregor2943aed2009-03-03 04:44:36 +00001376 Invalid = true;
1377 continue;
1378 }
1379
1380 if (CXXBaseSpecifier *InstantiatedBase
Douglas Gregord475b8d2009-03-25 21:17:03 +00001381 = CheckBaseSpecifier(Instantiation,
Douglas Gregor2943aed2009-03-03 04:44:36 +00001382 Base->getSourceRange(),
1383 Base->isVirtual(),
1384 Base->getAccessSpecifierAsWritten(),
Douglas Gregorf90b27a2011-01-03 22:36:02 +00001385 BaseTypeLoc,
1386 EllipsisLoc))
Douglas Gregor2943aed2009-03-03 04:44:36 +00001387 InstantiatedBases.push_back(InstantiatedBase);
1388 else
1389 Invalid = true;
1390 }
1391
Douglas Gregor27b152f2009-03-10 18:52:44 +00001392 if (!Invalid &&
Jay Foadbeaaccd2009-05-21 09:52:38 +00001393 AttachBaseSpecifiers(Instantiation, InstantiatedBases.data(),
Douglas Gregor2943aed2009-03-03 04:44:36 +00001394 InstantiatedBases.size()))
1395 Invalid = true;
1396
1397 return Invalid;
1398}
1399
Douglas Gregord475b8d2009-03-25 21:17:03 +00001400/// \brief Instantiate the definition of a class from a given pattern.
1401///
1402/// \param PointOfInstantiation The point of instantiation within the
1403/// source code.
1404///
1405/// \param Instantiation is the declaration whose definition is being
1406/// instantiated. This will be either a class template specialization
1407/// or a member class of a class template specialization.
1408///
1409/// \param Pattern is the pattern from which the instantiation
1410/// occurs. This will be either the declaration of a class template or
1411/// the declaration of a member class of a class template.
1412///
1413/// \param TemplateArgs The template arguments to be substituted into
1414/// the pattern.
1415///
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001416/// \param TSK the kind of implicit or explicit instantiation to perform.
Douglas Gregor5842ba92009-08-24 15:23:48 +00001417///
1418/// \param Complain whether to complain if the class cannot be instantiated due
1419/// to the lack of a definition.
1420///
Douglas Gregord475b8d2009-03-25 21:17:03 +00001421/// \returns true if an error occurred, false otherwise.
1422bool
1423Sema::InstantiateClass(SourceLocation PointOfInstantiation,
1424 CXXRecordDecl *Instantiation, CXXRecordDecl *Pattern,
Douglas Gregord6350ae2009-08-28 20:31:08 +00001425 const MultiLevelTemplateArgumentList &TemplateArgs,
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001426 TemplateSpecializationKind TSK,
Douglas Gregor5842ba92009-08-24 15:23:48 +00001427 bool Complain) {
Douglas Gregord475b8d2009-03-25 21:17:03 +00001428 bool Invalid = false;
John McCalle29ba202009-08-20 01:44:21 +00001429
Mike Stump1eb44332009-09-09 15:08:12 +00001430 CXXRecordDecl *PatternDef
Douglas Gregor952b0172010-02-11 01:04:33 +00001431 = cast_or_null<CXXRecordDecl>(Pattern->getDefinition());
Douglas Gregord475b8d2009-03-25 21:17:03 +00001432 if (!PatternDef) {
Douglas Gregor5842ba92009-08-24 15:23:48 +00001433 if (!Complain) {
1434 // Say nothing
1435 } else if (Pattern == Instantiation->getInstantiatedFromMemberClass()) {
Douglas Gregord475b8d2009-03-25 21:17:03 +00001436 Diag(PointOfInstantiation,
1437 diag::err_implicit_instantiate_member_undefined)
1438 << Context.getTypeDeclType(Instantiation);
1439 Diag(Pattern->getLocation(), diag::note_member_of_template_here);
1440 } else {
Douglas Gregor93dfdb12009-05-13 00:25:59 +00001441 Diag(PointOfInstantiation, diag::err_template_instantiate_undefined)
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001442 << (TSK != TSK_ImplicitInstantiation)
Douglas Gregord475b8d2009-03-25 21:17:03 +00001443 << Context.getTypeDeclType(Instantiation);
1444 Diag(Pattern->getLocation(), diag::note_template_decl_here);
1445 }
1446 return true;
1447 }
1448 Pattern = PatternDef;
1449
Douglas Gregor454885e2009-10-15 15:54:05 +00001450 // \brief Record the point of instantiation.
1451 if (MemberSpecializationInfo *MSInfo
1452 = Instantiation->getMemberSpecializationInfo()) {
1453 MSInfo->setTemplateSpecializationKind(TSK);
1454 MSInfo->setPointOfInstantiation(PointOfInstantiation);
Douglas Gregor972e6ce2009-10-27 06:26:26 +00001455 } else if (ClassTemplateSpecializationDecl *Spec
1456 = dyn_cast<ClassTemplateSpecializationDecl>(Instantiation)) {
1457 Spec->setTemplateSpecializationKind(TSK);
1458 Spec->setPointOfInstantiation(PointOfInstantiation);
Douglas Gregor454885e2009-10-15 15:54:05 +00001459 }
1460
Douglas Gregord048bb72009-03-25 21:23:52 +00001461 InstantiatingTemplate Inst(*this, PointOfInstantiation, Instantiation);
Douglas Gregord475b8d2009-03-25 21:17:03 +00001462 if (Inst)
1463 return true;
1464
1465 // Enter the scope of this instantiation. We don't use
1466 // PushDeclContext because we don't have a scope.
John McCallf5813822010-04-29 00:35:03 +00001467 ContextRAII SavedContext(*this, Instantiation);
Douglas Gregor9679caf2010-05-12 17:27:19 +00001468 EnterExpressionEvaluationContext EvalContext(*this,
John McCallf312b1e2010-08-26 23:41:50 +00001469 Sema::PotentiallyEvaluated);
Douglas Gregord475b8d2009-03-25 21:17:03 +00001470
Douglas Gregor05030bb2010-03-24 01:33:17 +00001471 // If this is an instantiation of a local class, merge this local
1472 // instantiation scope with the enclosing scope. Otherwise, every
1473 // instantiation of a class has its own local instantiation scope.
1474 bool MergeWithParentScope = !Instantiation->isDefinedOutsideFunctionOrMethod();
John McCall2a7fb272010-08-25 05:32:35 +00001475 LocalInstantiationScope Scope(*this, MergeWithParentScope);
Douglas Gregor05030bb2010-03-24 01:33:17 +00001476
John McCall1d8d1cc2010-08-01 02:01:53 +00001477 // Pull attributes from the pattern onto the instantiation.
1478 InstantiateAttrs(TemplateArgs, Pattern, Instantiation);
1479
Douglas Gregord475b8d2009-03-25 21:17:03 +00001480 // Start the definition of this instantiation.
1481 Instantiation->startDefinition();
Douglas Gregor13c85772010-05-06 00:28:52 +00001482
1483 Instantiation->setTagKind(Pattern->getTagKind());
Douglas Gregord475b8d2009-03-25 21:17:03 +00001484
John McCallce3ff2b2009-08-25 22:02:44 +00001485 // Do substitution on the base class specifiers.
1486 if (SubstBaseSpecifiers(Instantiation, Pattern, TemplateArgs))
Douglas Gregord475b8d2009-03-25 21:17:03 +00001487 Invalid = true;
1488
Douglas Gregord65587f2010-11-10 19:44:59 +00001489 TemplateDeclInstantiator Instantiator(*this, Instantiation, TemplateArgs);
John McCalld226f652010-08-21 09:40:31 +00001490 llvm::SmallVector<Decl*, 4> Fields;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001491 for (RecordDecl::decl_iterator Member = Pattern->decls_begin(),
Mike Stump1eb44332009-09-09 15:08:12 +00001492 MemberEnd = Pattern->decls_end();
Douglas Gregor6ab35242009-04-09 21:40:53 +00001493 Member != MemberEnd; ++Member) {
Argyrios Kyrtzidisbb5e4312010-11-04 03:18:57 +00001494 // Don't instantiate members not belonging in this semantic context.
1495 // e.g. for:
1496 // @code
1497 // template <int i> class A {
1498 // class B *g;
1499 // };
1500 // @endcode
1501 // 'class B' has the template as lexical context but semantically it is
1502 // introduced in namespace scope.
1503 if ((*Member)->getDeclContext() != Pattern)
1504 continue;
1505
Douglas Gregord65587f2010-11-10 19:44:59 +00001506 if ((*Member)->isInvalidDecl()) {
1507 Invalid = true;
1508 continue;
1509 }
1510
1511 Decl *NewMember = Instantiator.Visit(*Member);
Douglas Gregord475b8d2009-03-25 21:17:03 +00001512 if (NewMember) {
Eli Friedman721e77d2009-12-07 00:22:08 +00001513 if (FieldDecl *Field = dyn_cast<FieldDecl>(NewMember))
John McCalld226f652010-08-21 09:40:31 +00001514 Fields.push_back(Field);
Eli Friedman721e77d2009-12-07 00:22:08 +00001515 else if (NewMember->isInvalidDecl())
1516 Invalid = true;
Douglas Gregord475b8d2009-03-25 21:17:03 +00001517 } else {
1518 // FIXME: Eventually, a NULL return will mean that one of the
Mike Stump390b4cc2009-05-16 07:39:55 +00001519 // instantiations was a semantic disaster, and we'll want to set Invalid =
1520 // true. For now, we expect to skip some members that we can't yet handle.
Douglas Gregord475b8d2009-03-25 21:17:03 +00001521 }
1522 }
1523
1524 // Finish checking fields.
John McCalld226f652010-08-21 09:40:31 +00001525 ActOnFields(0, Instantiation->getLocation(), Instantiation,
Jay Foadbeaaccd2009-05-21 09:52:38 +00001526 Fields.data(), Fields.size(), SourceLocation(), SourceLocation(),
Douglas Gregord475b8d2009-03-25 21:17:03 +00001527 0);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001528 CheckCompletedCXXClass(Instantiation);
Douglas Gregor663b5a02009-10-14 20:14:33 +00001529 if (Instantiation->isInvalidDecl())
1530 Invalid = true;
Douglas Gregord65587f2010-11-10 19:44:59 +00001531 else {
1532 // Instantiate any out-of-line class template partial
1533 // specializations now.
1534 for (TemplateDeclInstantiator::delayed_partial_spec_iterator
1535 P = Instantiator.delayed_partial_spec_begin(),
1536 PEnd = Instantiator.delayed_partial_spec_end();
1537 P != PEnd; ++P) {
1538 if (!Instantiator.InstantiateClassTemplatePartialSpecialization(
1539 P->first,
1540 P->second)) {
1541 Invalid = true;
1542 break;
1543 }
1544 }
1545 }
1546
Douglas Gregord475b8d2009-03-25 21:17:03 +00001547 // Exit the scope of this instantiation.
John McCallf5813822010-04-29 00:35:03 +00001548 SavedContext.pop();
Douglas Gregord475b8d2009-03-25 21:17:03 +00001549
Douglas Gregor6fb745b2010-05-13 16:44:06 +00001550 if (!Invalid) {
Douglas Gregoraba43bb2009-05-26 20:50:29 +00001551 Consumer.HandleTagDeclDefinition(Instantiation);
1552
Douglas Gregor6fb745b2010-05-13 16:44:06 +00001553 // Always emit the vtable for an explicit instantiation definition
1554 // of a polymorphic class template specialization.
1555 if (TSK == TSK_ExplicitInstantiationDefinition)
1556 MarkVTableUsed(PointOfInstantiation, Instantiation, true);
1557 }
1558
Douglas Gregord475b8d2009-03-25 21:17:03 +00001559 return Invalid;
1560}
1561
Douglas Gregor9b623632010-10-12 23:32:35 +00001562namespace {
1563 /// \brief A partial specialization whose template arguments have matched
1564 /// a given template-id.
1565 struct PartialSpecMatchResult {
1566 ClassTemplatePartialSpecializationDecl *Partial;
1567 TemplateArgumentList *Args;
Douglas Gregor9b623632010-10-12 23:32:35 +00001568 };
1569}
1570
Mike Stump1eb44332009-09-09 15:08:12 +00001571bool
Douglas Gregor2943aed2009-03-03 04:44:36 +00001572Sema::InstantiateClassTemplateSpecialization(
Douglas Gregor972e6ce2009-10-27 06:26:26 +00001573 SourceLocation PointOfInstantiation,
Douglas Gregor2943aed2009-03-03 04:44:36 +00001574 ClassTemplateSpecializationDecl *ClassTemplateSpec,
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001575 TemplateSpecializationKind TSK,
Douglas Gregor5842ba92009-08-24 15:23:48 +00001576 bool Complain) {
Douglas Gregor2943aed2009-03-03 04:44:36 +00001577 // Perform the actual instantiation on the canonical declaration.
1578 ClassTemplateSpec = cast<ClassTemplateSpecializationDecl>(
Argyrios Kyrtzidis97fbaa22009-07-18 00:34:25 +00001579 ClassTemplateSpec->getCanonicalDecl());
Douglas Gregor2943aed2009-03-03 04:44:36 +00001580
Douglas Gregor52604ab2009-09-11 21:19:12 +00001581 // Check whether we have already instantiated or specialized this class
1582 // template specialization.
1583 if (ClassTemplateSpec->getSpecializationKind() != TSK_Undeclared) {
1584 if (ClassTemplateSpec->getSpecializationKind() ==
1585 TSK_ExplicitInstantiationDeclaration &&
1586 TSK == TSK_ExplicitInstantiationDefinition) {
1587 // An explicit instantiation definition follows an explicit instantiation
1588 // declaration (C++0x [temp.explicit]p10); go ahead and perform the
1589 // explicit instantiation.
1590 ClassTemplateSpec->setSpecializationKind(TSK);
Douglas Gregor6fb745b2010-05-13 16:44:06 +00001591
1592 // If this is an explicit instantiation definition, mark the
1593 // vtable as used.
1594 if (TSK == TSK_ExplicitInstantiationDefinition)
1595 MarkVTableUsed(PointOfInstantiation, ClassTemplateSpec, true);
1596
Douglas Gregor52604ab2009-09-11 21:19:12 +00001597 return false;
1598 }
1599
1600 // We can only instantiate something that hasn't already been
1601 // instantiated or specialized. Fail without any diagnostics: our
1602 // caller will provide an error message.
Douglas Gregor2943aed2009-03-03 04:44:36 +00001603 return true;
Douglas Gregor52604ab2009-09-11 21:19:12 +00001604 }
Douglas Gregor2943aed2009-03-03 04:44:36 +00001605
Douglas Gregor9eea08b2009-09-15 16:51:42 +00001606 if (ClassTemplateSpec->isInvalidDecl())
1607 return true;
1608
Douglas Gregor2943aed2009-03-03 04:44:36 +00001609 ClassTemplateDecl *Template = ClassTemplateSpec->getSpecializedTemplate();
Douglas Gregord6350ae2009-08-28 20:31:08 +00001610 CXXRecordDecl *Pattern = 0;
Douglas Gregorc8ab2562009-05-31 09:31:02 +00001611
Douglas Gregorc1efb3f2009-06-12 22:31:52 +00001612 // C++ [temp.class.spec.match]p1:
1613 // When a class template is used in a context that requires an
1614 // instantiation of the class, it is necessary to determine
1615 // whether the instantiation is to be generated using the primary
1616 // template or one of the partial specializations. This is done by
1617 // matching the template arguments of the class template
1618 // specialization with the template argument lists of the partial
1619 // specializations.
Douglas Gregor9b623632010-10-12 23:32:35 +00001620 typedef PartialSpecMatchResult MatchResult;
Douglas Gregor199d9912009-06-05 00:53:49 +00001621 llvm::SmallVector<MatchResult, 4> Matched;
Douglas Gregordc60c1e2010-04-30 05:56:50 +00001622 llvm::SmallVector<ClassTemplatePartialSpecializationDecl *, 4> PartialSpecs;
1623 Template->getPartialSpecializations(PartialSpecs);
1624 for (unsigned I = 0, N = PartialSpecs.size(); I != N; ++I) {
1625 ClassTemplatePartialSpecializationDecl *Partial = PartialSpecs[I];
John McCall5769d612010-02-08 23:07:23 +00001626 TemplateDeductionInfo Info(Context, PointOfInstantiation);
Douglas Gregorf67875d2009-06-12 18:26:56 +00001627 if (TemplateDeductionResult Result
Douglas Gregordc60c1e2010-04-30 05:56:50 +00001628 = DeduceTemplateArguments(Partial,
Douglas Gregorf67875d2009-06-12 18:26:56 +00001629 ClassTemplateSpec->getTemplateArgs(),
1630 Info)) {
1631 // FIXME: Store the failed-deduction information for use in
1632 // diagnostics, later.
1633 (void)Result;
1634 } else {
Douglas Gregor9b623632010-10-12 23:32:35 +00001635 Matched.push_back(PartialSpecMatchResult());
1636 Matched.back().Partial = Partial;
1637 Matched.back().Args = Info.take();
Douglas Gregorf67875d2009-06-12 18:26:56 +00001638 }
Douglas Gregorc8ab2562009-05-31 09:31:02 +00001639 }
1640
Douglas Gregored9c0f92009-10-29 00:04:11 +00001641 if (Matched.size() >= 1) {
Douglas Gregorbf4ea562009-09-15 16:23:51 +00001642 llvm::SmallVector<MatchResult, 4>::iterator Best = Matched.begin();
Douglas Gregored9c0f92009-10-29 00:04:11 +00001643 if (Matched.size() == 1) {
1644 // -- If exactly one matching specialization is found, the
1645 // instantiation is generated from that specialization.
1646 // We don't need to do anything for this.
1647 } else {
1648 // -- If more than one matching specialization is found, the
1649 // partial order rules (14.5.4.2) are used to determine
1650 // whether one of the specializations is more specialized
1651 // than the others. If none of the specializations is more
1652 // specialized than all of the other matching
1653 // specializations, then the use of the class template is
1654 // ambiguous and the program is ill-formed.
1655 for (llvm::SmallVector<MatchResult, 4>::iterator P = Best + 1,
1656 PEnd = Matched.end();
1657 P != PEnd; ++P) {
Douglas Gregor9b623632010-10-12 23:32:35 +00001658 if (getMoreSpecializedPartialSpecialization(P->Partial, Best->Partial,
John McCall5769d612010-02-08 23:07:23 +00001659 PointOfInstantiation)
Douglas Gregor9b623632010-10-12 23:32:35 +00001660 == P->Partial)
Douglas Gregored9c0f92009-10-29 00:04:11 +00001661 Best = P;
Douglas Gregorbf4ea562009-09-15 16:23:51 +00001662 }
Douglas Gregorbf4ea562009-09-15 16:23:51 +00001663
Douglas Gregored9c0f92009-10-29 00:04:11 +00001664 // Determine if the best partial specialization is more specialized than
1665 // the others.
1666 bool Ambiguous = false;
Douglas Gregorbf4ea562009-09-15 16:23:51 +00001667 for (llvm::SmallVector<MatchResult, 4>::iterator P = Matched.begin(),
1668 PEnd = Matched.end();
Douglas Gregored9c0f92009-10-29 00:04:11 +00001669 P != PEnd; ++P) {
1670 if (P != Best &&
Douglas Gregor9b623632010-10-12 23:32:35 +00001671 getMoreSpecializedPartialSpecialization(P->Partial, Best->Partial,
John McCall5769d612010-02-08 23:07:23 +00001672 PointOfInstantiation)
Douglas Gregor9b623632010-10-12 23:32:35 +00001673 != Best->Partial) {
Douglas Gregored9c0f92009-10-29 00:04:11 +00001674 Ambiguous = true;
1675 break;
1676 }
1677 }
1678
1679 if (Ambiguous) {
1680 // Partial ordering did not produce a clear winner. Complain.
1681 ClassTemplateSpec->setInvalidDecl();
1682 Diag(PointOfInstantiation, diag::err_partial_spec_ordering_ambiguous)
1683 << ClassTemplateSpec;
1684
1685 // Print the matching partial specializations.
1686 for (llvm::SmallVector<MatchResult, 4>::iterator P = Matched.begin(),
1687 PEnd = Matched.end();
1688 P != PEnd; ++P)
Douglas Gregor9b623632010-10-12 23:32:35 +00001689 Diag(P->Partial->getLocation(), diag::note_partial_spec_match)
1690 << getTemplateArgumentBindingsText(
1691 P->Partial->getTemplateParameters(),
1692 *P->Args);
Douglas Gregord6350ae2009-08-28 20:31:08 +00001693
Douglas Gregored9c0f92009-10-29 00:04:11 +00001694 return true;
1695 }
Douglas Gregorbf4ea562009-09-15 16:23:51 +00001696 }
1697
1698 // Instantiate using the best class template partial specialization.
Douglas Gregor9b623632010-10-12 23:32:35 +00001699 ClassTemplatePartialSpecializationDecl *OrigPartialSpec = Best->Partial;
Douglas Gregored9c0f92009-10-29 00:04:11 +00001700 while (OrigPartialSpec->getInstantiatedFromMember()) {
1701 // If we've found an explicit specialization of this class template,
1702 // stop here and use that as the pattern.
1703 if (OrigPartialSpec->isMemberSpecialization())
1704 break;
1705
1706 OrigPartialSpec = OrigPartialSpec->getInstantiatedFromMember();
1707 }
1708
1709 Pattern = OrigPartialSpec;
Douglas Gregor9b623632010-10-12 23:32:35 +00001710 ClassTemplateSpec->setInstantiationOf(Best->Partial, Best->Args);
Douglas Gregorc1efb3f2009-06-12 22:31:52 +00001711 } else {
1712 // -- If no matches are found, the instantiation is generated
1713 // from the primary template.
Douglas Gregord6350ae2009-08-28 20:31:08 +00001714 ClassTemplateDecl *OrigTemplate = Template;
Douglas Gregorfd056bc2009-10-13 16:30:37 +00001715 while (OrigTemplate->getInstantiatedFromMemberTemplate()) {
1716 // If we've found an explicit specialization of this class template,
1717 // stop here and use that as the pattern.
1718 if (OrigTemplate->isMemberSpecialization())
1719 break;
1720
Douglas Gregord6350ae2009-08-28 20:31:08 +00001721 OrigTemplate = OrigTemplate->getInstantiatedFromMemberTemplate();
Douglas Gregorfd056bc2009-10-13 16:30:37 +00001722 }
1723
Douglas Gregord6350ae2009-08-28 20:31:08 +00001724 Pattern = OrigTemplate->getTemplatedDecl();
Douglas Gregorc8ab2562009-05-31 09:31:02 +00001725 }
Douglas Gregor2943aed2009-03-03 04:44:36 +00001726
Douglas Gregor972e6ce2009-10-27 06:26:26 +00001727 bool Result = InstantiateClass(PointOfInstantiation, ClassTemplateSpec,
1728 Pattern,
1729 getTemplateInstantiationArgs(ClassTemplateSpec),
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001730 TSK,
Douglas Gregor5842ba92009-08-24 15:23:48 +00001731 Complain);
Mike Stump1eb44332009-09-09 15:08:12 +00001732
Douglas Gregor199d9912009-06-05 00:53:49 +00001733 return Result;
Douglas Gregor2943aed2009-03-03 04:44:36 +00001734}
Douglas Gregor5953d8b2009-03-19 17:26:29 +00001735
John McCallce3ff2b2009-08-25 22:02:44 +00001736/// \brief Instantiates the definitions of all of the member
1737/// of the given class, which is an instantiation of a class template
1738/// or a member class of a template.
Douglas Gregora58861f2009-05-13 20:28:22 +00001739void
1740Sema::InstantiateClassMembers(SourceLocation PointOfInstantiation,
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001741 CXXRecordDecl *Instantiation,
1742 const MultiLevelTemplateArgumentList &TemplateArgs,
1743 TemplateSpecializationKind TSK) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001744 for (DeclContext::decl_iterator D = Instantiation->decls_begin(),
1745 DEnd = Instantiation->decls_end();
Douglas Gregora58861f2009-05-13 20:28:22 +00001746 D != DEnd; ++D) {
Douglas Gregor0d035142009-10-27 18:42:08 +00001747 bool SuppressNew = false;
Douglas Gregora58861f2009-05-13 20:28:22 +00001748 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(*D)) {
Douglas Gregor0d035142009-10-27 18:42:08 +00001749 if (FunctionDecl *Pattern
1750 = Function->getInstantiatedFromMemberFunction()) {
1751 MemberSpecializationInfo *MSInfo
1752 = Function->getMemberSpecializationInfo();
1753 assert(MSInfo && "No member specialization information?");
Douglas Gregorc42b6522010-04-09 21:02:29 +00001754 if (MSInfo->getTemplateSpecializationKind()
1755 == TSK_ExplicitSpecialization)
1756 continue;
1757
Douglas Gregor0d035142009-10-27 18:42:08 +00001758 if (CheckSpecializationInstantiationRedecl(PointOfInstantiation, TSK,
1759 Function,
1760 MSInfo->getTemplateSpecializationKind(),
Nico Weberc956b6e2010-09-27 21:02:09 +00001761 MSInfo->getPointOfInstantiation(),
Douglas Gregor0d035142009-10-27 18:42:08 +00001762 SuppressNew) ||
1763 SuppressNew)
Douglas Gregorf6b11852009-10-08 15:14:33 +00001764 continue;
1765
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001766 if (Function->hasBody())
Douglas Gregor0d035142009-10-27 18:42:08 +00001767 continue;
1768
1769 if (TSK == TSK_ExplicitInstantiationDefinition) {
1770 // C++0x [temp.explicit]p8:
1771 // An explicit instantiation definition that names a class template
1772 // specialization explicitly instantiates the class template
1773 // specialization and is only an explicit instantiation definition
1774 // of members whose definition is visible at the point of
1775 // instantiation.
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001776 if (!Pattern->hasBody())
Douglas Gregor0d035142009-10-27 18:42:08 +00001777 continue;
1778
1779 Function->setTemplateSpecializationKind(TSK, PointOfInstantiation);
1780
1781 InstantiateFunctionDefinition(PointOfInstantiation, Function);
1782 } else {
1783 Function->setTemplateSpecializationKind(TSK, PointOfInstantiation);
1784 }
Douglas Gregorf6b11852009-10-08 15:14:33 +00001785 }
Douglas Gregora58861f2009-05-13 20:28:22 +00001786 } else if (VarDecl *Var = dyn_cast<VarDecl>(*D)) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001787 if (Var->isStaticDataMember()) {
Douglas Gregor0d035142009-10-27 18:42:08 +00001788 MemberSpecializationInfo *MSInfo = Var->getMemberSpecializationInfo();
1789 assert(MSInfo && "No member specialization information?");
Douglas Gregorc42b6522010-04-09 21:02:29 +00001790 if (MSInfo->getTemplateSpecializationKind()
1791 == TSK_ExplicitSpecialization)
1792 continue;
1793
Douglas Gregor0d035142009-10-27 18:42:08 +00001794 if (CheckSpecializationInstantiationRedecl(PointOfInstantiation, TSK,
1795 Var,
1796 MSInfo->getTemplateSpecializationKind(),
Nico Weberc956b6e2010-09-27 21:02:09 +00001797 MSInfo->getPointOfInstantiation(),
Douglas Gregor0d035142009-10-27 18:42:08 +00001798 SuppressNew) ||
1799 SuppressNew)
Douglas Gregorf6b11852009-10-08 15:14:33 +00001800 continue;
1801
Douglas Gregor0d035142009-10-27 18:42:08 +00001802 if (TSK == TSK_ExplicitInstantiationDefinition) {
1803 // C++0x [temp.explicit]p8:
1804 // An explicit instantiation definition that names a class template
1805 // specialization explicitly instantiates the class template
1806 // specialization and is only an explicit instantiation definition
1807 // of members whose definition is visible at the point of
1808 // instantiation.
1809 if (!Var->getInstantiatedFromStaticDataMember()
1810 ->getOutOfLineDefinition())
1811 continue;
1812
1813 Var->setTemplateSpecializationKind(TSK, PointOfInstantiation);
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001814 InstantiateStaticDataMemberDefinition(PointOfInstantiation, Var);
Douglas Gregor0d035142009-10-27 18:42:08 +00001815 } else {
1816 Var->setTemplateSpecializationKind(TSK, PointOfInstantiation);
1817 }
1818 }
Douglas Gregora58861f2009-05-13 20:28:22 +00001819 } else if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(*D)) {
Douglas Gregora77eaa92010-04-18 18:11:38 +00001820 // Always skip the injected-class-name, along with any
1821 // redeclarations of nested classes, since both would cause us
1822 // to try to instantiate the members of a class twice.
1823 if (Record->isInjectedClassName() || Record->getPreviousDeclaration())
Douglas Gregor2db32322009-10-07 23:56:10 +00001824 continue;
1825
Douglas Gregor0d035142009-10-27 18:42:08 +00001826 MemberSpecializationInfo *MSInfo = Record->getMemberSpecializationInfo();
1827 assert(MSInfo && "No member specialization information?");
Douglas Gregorc42b6522010-04-09 21:02:29 +00001828
1829 if (MSInfo->getTemplateSpecializationKind()
1830 == TSK_ExplicitSpecialization)
1831 continue;
Nico Weberc956b6e2010-09-27 21:02:09 +00001832
Douglas Gregor0d035142009-10-27 18:42:08 +00001833 if (CheckSpecializationInstantiationRedecl(PointOfInstantiation, TSK,
1834 Record,
1835 MSInfo->getTemplateSpecializationKind(),
Nico Weberc956b6e2010-09-27 21:02:09 +00001836 MSInfo->getPointOfInstantiation(),
Douglas Gregor0d035142009-10-27 18:42:08 +00001837 SuppressNew) ||
1838 SuppressNew)
Douglas Gregorf6b11852009-10-08 15:14:33 +00001839 continue;
1840
Douglas Gregor0d035142009-10-27 18:42:08 +00001841 CXXRecordDecl *Pattern = Record->getInstantiatedFromMemberClass();
1842 assert(Pattern && "Missing instantiated-from-template information");
1843
Douglas Gregor952b0172010-02-11 01:04:33 +00001844 if (!Record->getDefinition()) {
1845 if (!Pattern->getDefinition()) {
Douglas Gregor0d035142009-10-27 18:42:08 +00001846 // C++0x [temp.explicit]p8:
1847 // An explicit instantiation definition that names a class template
1848 // specialization explicitly instantiates the class template
1849 // specialization and is only an explicit instantiation definition
1850 // of members whose definition is visible at the point of
1851 // instantiation.
1852 if (TSK == TSK_ExplicitInstantiationDeclaration) {
1853 MSInfo->setTemplateSpecializationKind(TSK);
1854 MSInfo->setPointOfInstantiation(PointOfInstantiation);
1855 }
1856
1857 continue;
1858 }
1859
1860 InstantiateClass(PointOfInstantiation, Record, Pattern,
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001861 TemplateArgs,
1862 TSK);
Nico Weberc956b6e2010-09-27 21:02:09 +00001863 } else {
1864 if (TSK == TSK_ExplicitInstantiationDefinition &&
1865 Record->getTemplateSpecializationKind() ==
1866 TSK_ExplicitInstantiationDeclaration) {
1867 Record->setTemplateSpecializationKind(TSK);
1868 MarkVTableUsed(PointOfInstantiation, Record, true);
1869 }
Douglas Gregor0d035142009-10-27 18:42:08 +00001870 }
Douglas Gregore9374d52009-10-08 01:19:17 +00001871
Douglas Gregor952b0172010-02-11 01:04:33 +00001872 Pattern = cast_or_null<CXXRecordDecl>(Record->getDefinition());
Douglas Gregor0d035142009-10-27 18:42:08 +00001873 if (Pattern)
1874 InstantiateClassMembers(PointOfInstantiation, Pattern, TemplateArgs,
1875 TSK);
Douglas Gregora58861f2009-05-13 20:28:22 +00001876 }
1877 }
1878}
1879
1880/// \brief Instantiate the definitions of all of the members of the
1881/// given class template specialization, which was named as part of an
1882/// explicit instantiation.
Mike Stump1eb44332009-09-09 15:08:12 +00001883void
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001884Sema::InstantiateClassTemplateSpecializationMembers(
Douglas Gregora58861f2009-05-13 20:28:22 +00001885 SourceLocation PointOfInstantiation,
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001886 ClassTemplateSpecializationDecl *ClassTemplateSpec,
1887 TemplateSpecializationKind TSK) {
Douglas Gregora58861f2009-05-13 20:28:22 +00001888 // C++0x [temp.explicit]p7:
1889 // An explicit instantiation that names a class template
1890 // specialization is an explicit instantion of the same kind
1891 // (declaration or definition) of each of its members (not
1892 // including members inherited from base classes) that has not
1893 // been previously explicitly specialized in the translation unit
1894 // containing the explicit instantiation, except as described
1895 // below.
1896 InstantiateClassMembers(PointOfInstantiation, ClassTemplateSpec,
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001897 getTemplateInstantiationArgs(ClassTemplateSpec),
1898 TSK);
Douglas Gregora58861f2009-05-13 20:28:22 +00001899}
1900
John McCall60d7b3a2010-08-24 06:29:42 +00001901StmtResult
Douglas Gregord6350ae2009-08-28 20:31:08 +00001902Sema::SubstStmt(Stmt *S, const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor43959a92009-08-20 07:17:43 +00001903 if (!S)
1904 return Owned(S);
1905
1906 TemplateInstantiator Instantiator(*this, TemplateArgs,
1907 SourceLocation(),
1908 DeclarationName());
1909 return Instantiator.TransformStmt(S);
1910}
1911
John McCall60d7b3a2010-08-24 06:29:42 +00001912ExprResult
Douglas Gregord6350ae2009-08-28 20:31:08 +00001913Sema::SubstExpr(Expr *E, const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregorb98b1992009-08-11 05:31:07 +00001914 if (!E)
1915 return Owned(E);
Mike Stump1eb44332009-09-09 15:08:12 +00001916
Douglas Gregorb98b1992009-08-11 05:31:07 +00001917 TemplateInstantiator Instantiator(*this, TemplateArgs,
1918 SourceLocation(),
1919 DeclarationName());
1920 return Instantiator.TransformExpr(E);
1921}
1922
Douglas Gregor91fc73e2011-01-07 19:35:17 +00001923bool Sema::SubstExprs(Expr **Exprs, unsigned NumExprs, bool IsCall,
1924 const MultiLevelTemplateArgumentList &TemplateArgs,
1925 llvm::SmallVectorImpl<Expr *> &Outputs) {
1926 if (NumExprs == 0)
1927 return false;
1928
1929 TemplateInstantiator Instantiator(*this, TemplateArgs,
1930 SourceLocation(),
1931 DeclarationName());
1932 return Instantiator.TransformExprs(Exprs, NumExprs, IsCall, Outputs);
1933}
1934
John McCallce3ff2b2009-08-25 22:02:44 +00001935/// \brief Do template substitution on a nested-name-specifier.
Douglas Gregorab452ba2009-03-26 23:50:42 +00001936NestedNameSpecifier *
John McCallce3ff2b2009-08-25 22:02:44 +00001937Sema::SubstNestedNameSpecifier(NestedNameSpecifier *NNS,
Douglas Gregord6350ae2009-08-28 20:31:08 +00001938 SourceRange Range,
1939 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregordcee1a12009-08-06 05:28:30 +00001940 TemplateInstantiator Instantiator(*this, TemplateArgs, Range.getBegin(),
1941 DeclarationName());
Douglas Gregoredc90502010-02-25 04:46:04 +00001942 return Instantiator.TransformNestedNameSpecifier(NNS, Range);
Douglas Gregor5953d8b2009-03-19 17:26:29 +00001943}
Douglas Gregorde650ae2009-03-31 18:38:02 +00001944
Abramo Bagnara25777432010-08-11 22:01:17 +00001945/// \brief Do template substitution on declaration name info.
1946DeclarationNameInfo
1947Sema::SubstDeclarationNameInfo(const DeclarationNameInfo &NameInfo,
1948 const MultiLevelTemplateArgumentList &TemplateArgs) {
1949 TemplateInstantiator Instantiator(*this, TemplateArgs, NameInfo.getLoc(),
1950 NameInfo.getName());
1951 return Instantiator.TransformDeclarationNameInfo(NameInfo);
1952}
1953
Douglas Gregorde650ae2009-03-31 18:38:02 +00001954TemplateName
John McCallce3ff2b2009-08-25 22:02:44 +00001955Sema::SubstTemplateName(TemplateName Name, SourceLocation Loc,
Douglas Gregord6350ae2009-08-28 20:31:08 +00001956 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregord1067e52009-08-06 06:41:21 +00001957 TemplateInstantiator Instantiator(*this, TemplateArgs, Loc,
1958 DeclarationName());
1959 return Instantiator.TransformTemplateName(Name);
Douglas Gregorde650ae2009-03-31 18:38:02 +00001960}
Douglas Gregor91333002009-06-11 00:06:24 +00001961
Douglas Gregore02e2622010-12-22 21:19:48 +00001962bool Sema::Subst(const TemplateArgumentLoc *Args, unsigned NumArgs,
1963 TemplateArgumentListInfo &Result,
John McCall833ca992009-10-29 08:12:44 +00001964 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor670444e2009-08-04 22:27:00 +00001965 TemplateInstantiator Instantiator(*this, TemplateArgs, SourceLocation(),
1966 DeclarationName());
Douglas Gregore02e2622010-12-22 21:19:48 +00001967
1968 return Instantiator.TransformTemplateArguments(Args, NumArgs, Result);
Douglas Gregor91333002009-06-11 00:06:24 +00001969}
Douglas Gregor895162d2010-04-30 18:55:50 +00001970
John McCall2a7fb272010-08-25 05:32:35 +00001971Decl *LocalInstantiationScope::getInstantiationOf(const Decl *D) {
Douglas Gregor12c9c002011-01-07 16:43:16 +00001972 llvm::PointerUnion<Decl *, DeclArgumentPack *> *Found= findInstantiationOf(D);
1973 if (!Found)
1974 return 0;
1975
1976 if (Found->is<Decl *>())
1977 return Found->get<Decl *>();
1978
1979 return (*Found->get<DeclArgumentPack *>())[
1980 SemaRef.ArgumentPackSubstitutionIndex];
1981}
1982
1983llvm::PointerUnion<Decl *, LocalInstantiationScope::DeclArgumentPack *> *
1984LocalInstantiationScope::findInstantiationOf(const Decl *D) {
Douglas Gregor895162d2010-04-30 18:55:50 +00001985 for (LocalInstantiationScope *Current = this; Current;
1986 Current = Current->Outer) {
1987 // Check if we found something within this scope.
Douglas Gregorebb1c562010-12-21 21:22:51 +00001988 const Decl *CheckD = D;
1989 do {
Douglas Gregor12c9c002011-01-07 16:43:16 +00001990 LocalDeclsMap::iterator Found = Current->LocalDecls.find(CheckD);
Douglas Gregorebb1c562010-12-21 21:22:51 +00001991 if (Found != Current->LocalDecls.end())
Douglas Gregor12c9c002011-01-07 16:43:16 +00001992 return &Found->second;
Douglas Gregorebb1c562010-12-21 21:22:51 +00001993
1994 // If this is a tag declaration, it's possible that we need to look for
1995 // a previous declaration.
1996 if (const TagDecl *Tag = dyn_cast<TagDecl>(CheckD))
1997 CheckD = Tag->getPreviousDeclaration();
1998 else
1999 CheckD = 0;
2000 } while (CheckD);
2001
Douglas Gregor895162d2010-04-30 18:55:50 +00002002 // If we aren't combined with our outer scope, we're done.
2003 if (!Current->CombineWithOuterScope)
2004 break;
2005 }
2006
2007 assert(D->isInvalidDecl() &&
2008 "declaration was not instantiated in this scope!");
2009 return 0;
2010}
2011
John McCall2a7fb272010-08-25 05:32:35 +00002012void LocalInstantiationScope::InstantiatedLocal(const Decl *D, Decl *Inst) {
Douglas Gregor12c9c002011-01-07 16:43:16 +00002013 llvm::PointerUnion<Decl *, DeclArgumentPack *> &Stored = LocalDecls[D];
Douglas Gregord3731192011-01-10 07:32:04 +00002014 if (Stored.isNull())
2015 Stored = Inst;
2016 else if (Stored.is<Decl *>()) {
2017 assert(Stored.get<Decl *>() == Inst && "Already instantiated this local");
2018 Stored = Inst;
2019 } else
2020 LocalDecls[D].get<DeclArgumentPack *>()->push_back(Inst);
Douglas Gregor895162d2010-04-30 18:55:50 +00002021}
Douglas Gregor12c9c002011-01-07 16:43:16 +00002022
2023void LocalInstantiationScope::InstantiatedLocalPackArg(const Decl *D,
2024 Decl *Inst) {
2025 DeclArgumentPack *Pack = LocalDecls[D].get<DeclArgumentPack *>();
2026 Pack->push_back(Inst);
2027}
2028
2029void LocalInstantiationScope::MakeInstantiatedLocalArgPack(const Decl *D) {
2030 llvm::PointerUnion<Decl *, DeclArgumentPack *> &Stored = LocalDecls[D];
2031 assert(Stored.isNull() && "Already instantiated this local");
2032 DeclArgumentPack *Pack = new DeclArgumentPack;
2033 Stored = Pack;
2034 ArgumentPacks.push_back(Pack);
2035}
2036
Douglas Gregord3731192011-01-10 07:32:04 +00002037void LocalInstantiationScope::SetPartiallySubstitutedPack(NamedDecl *Pack,
2038 const TemplateArgument *ExplicitArgs,
2039 unsigned NumExplicitArgs) {
2040 assert((!PartiallySubstitutedPack || PartiallySubstitutedPack == Pack) &&
2041 "Already have a partially-substituted pack");
2042 assert((!PartiallySubstitutedPack
2043 || NumArgsInPartiallySubstitutedPack == NumExplicitArgs) &&
2044 "Wrong number of arguments in partially-substituted pack");
2045 PartiallySubstitutedPack = Pack;
2046 ArgsInPartiallySubstitutedPack = ExplicitArgs;
2047 NumArgsInPartiallySubstitutedPack = NumExplicitArgs;
2048}
2049
2050NamedDecl *LocalInstantiationScope::getPartiallySubstitutedPack(
2051 const TemplateArgument **ExplicitArgs,
2052 unsigned *NumExplicitArgs) const {
2053 if (ExplicitArgs)
2054 *ExplicitArgs = 0;
2055 if (NumExplicitArgs)
2056 *NumExplicitArgs = 0;
2057
2058 for (const LocalInstantiationScope *Current = this; Current;
2059 Current = Current->Outer) {
2060 if (Current->PartiallySubstitutedPack) {
2061 if (ExplicitArgs)
2062 *ExplicitArgs = Current->ArgsInPartiallySubstitutedPack;
2063 if (NumExplicitArgs)
2064 *NumExplicitArgs = Current->NumArgsInPartiallySubstitutedPack;
2065
2066 return Current->PartiallySubstitutedPack;
2067 }
2068
2069 if (!Current->CombineWithOuterScope)
2070 break;
2071 }
2072
2073 return 0;
2074}