blob: 7dee0ad99bfb9113387ab22b729727963eadbd72 [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
13#include "Sema.h"
Douglas Gregor577f75a2009-08-04 16:50:30 +000014#include "TreeTransform.h"
John McCall5b3f9132009-11-22 01:44:31 +000015#include "Lookup.h"
Douglas Gregoraba43bb2009-05-26 20:50:29 +000016#include "clang/AST/ASTConsumer.h"
Douglas Gregor99ebf652009-02-27 19:31:52 +000017#include "clang/AST/ASTContext.h"
18#include "clang/AST/Expr.h"
Douglas Gregor99ebf652009-02-27 19:31:52 +000019#include "clang/AST/DeclTemplate.h"
20#include "clang/Parse/DeclSpec.h"
21#include "clang/Basic/LangOptions.h"
22
23using namespace clang;
24
Douglas Gregoree1828a2009-03-10 18:03:33 +000025//===----------------------------------------------------------------------===/
26// Template Instantiation Support
27//===----------------------------------------------------------------------===/
28
Douglas Gregord6350ae2009-08-28 20:31:08 +000029/// \brief Retrieve the template argument list(s) that should be used to
30/// instantiate the definition of the given declaration.
Douglas Gregor0f8716b2009-11-09 19:17:50 +000031///
32/// \param D the declaration for which we are computing template instantiation
33/// arguments.
34///
35/// \param Innermost if non-NULL, the innermost template argument list.
Douglas Gregor525f96c2010-02-05 07:33:43 +000036///
37/// \param RelativeToPrimary true if we should get the template
38/// arguments relative to the primary template, even when we're
39/// dealing with a specialization. This is only relevant for function
40/// template specializations.
Douglas Gregord1102432009-08-28 17:37:35 +000041MultiLevelTemplateArgumentList
Douglas Gregor0f8716b2009-11-09 19:17:50 +000042Sema::getTemplateInstantiationArgs(NamedDecl *D,
Douglas Gregor525f96c2010-02-05 07:33:43 +000043 const TemplateArgumentList *Innermost,
44 bool RelativeToPrimary) {
Douglas Gregord1102432009-08-28 17:37:35 +000045 // Accumulate the set of template argument lists in this structure.
46 MultiLevelTemplateArgumentList Result;
Mike Stump1eb44332009-09-09 15:08:12 +000047
Douglas Gregor0f8716b2009-11-09 19:17:50 +000048 if (Innermost)
49 Result.addOuterTemplateArguments(Innermost);
50
Douglas Gregord1102432009-08-28 17:37:35 +000051 DeclContext *Ctx = dyn_cast<DeclContext>(D);
52 if (!Ctx)
53 Ctx = D->getDeclContext();
Mike Stump1eb44332009-09-09 15:08:12 +000054
John McCallf181d8a2009-08-29 03:16:09 +000055 while (!Ctx->isFileContext()) {
Douglas Gregord1102432009-08-28 17:37:35 +000056 // Add template arguments from a class template instantiation.
Mike Stump1eb44332009-09-09 15:08:12 +000057 if (ClassTemplateSpecializationDecl *Spec
Douglas Gregord1102432009-08-28 17:37:35 +000058 = dyn_cast<ClassTemplateSpecializationDecl>(Ctx)) {
59 // We're done when we hit an explicit specialization.
60 if (Spec->getSpecializationKind() == TSK_ExplicitSpecialization)
61 break;
Mike Stump1eb44332009-09-09 15:08:12 +000062
Douglas Gregord1102432009-08-28 17:37:35 +000063 Result.addOuterTemplateArguments(&Spec->getTemplateInstantiationArgs());
Douglas Gregorfd056bc2009-10-13 16:30:37 +000064
65 // If this class template specialization was instantiated from a
66 // specialized member that is a class template, we're done.
67 assert(Spec->getSpecializedTemplate() && "No class template?");
68 if (Spec->getSpecializedTemplate()->isMemberSpecialization())
69 break;
Mike Stump1eb44332009-09-09 15:08:12 +000070 }
Douglas Gregord1102432009-08-28 17:37:35 +000071 // Add template arguments from a function template specialization.
John McCallf181d8a2009-08-29 03:16:09 +000072 else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Ctx)) {
Douglas Gregor525f96c2010-02-05 07:33:43 +000073 if (!RelativeToPrimary &&
74 Function->getTemplateSpecializationKind()
75 == TSK_ExplicitSpecialization)
Douglas Gregorfd056bc2009-10-13 16:30:37 +000076 break;
77
Douglas Gregord1102432009-08-28 17:37:35 +000078 if (const TemplateArgumentList *TemplateArgs
Douglas Gregorfd056bc2009-10-13 16:30:37 +000079 = Function->getTemplateSpecializationArgs()) {
80 // Add the template arguments for this specialization.
Douglas Gregord1102432009-08-28 17:37:35 +000081 Result.addOuterTemplateArguments(TemplateArgs);
John McCallf181d8a2009-08-29 03:16:09 +000082
Douglas Gregorfd056bc2009-10-13 16:30:37 +000083 // If this function was instantiated from a specialized member that is
84 // a function template, we're done.
85 assert(Function->getPrimaryTemplate() && "No function template?");
86 if (Function->getPrimaryTemplate()->isMemberSpecialization())
87 break;
88 }
89
John McCallf181d8a2009-08-29 03:16:09 +000090 // If this is a friend declaration and it declares an entity at
91 // namespace scope, take arguments from its lexical parent
92 // instead of its semantic parent.
93 if (Function->getFriendObjectKind() &&
94 Function->getDeclContext()->isFileContext()) {
95 Ctx = Function->getLexicalDeclContext();
Douglas Gregor525f96c2010-02-05 07:33:43 +000096 RelativeToPrimary = false;
John McCallf181d8a2009-08-29 03:16:09 +000097 continue;
98 }
Douglas Gregord1102432009-08-28 17:37:35 +000099 }
John McCallf181d8a2009-08-29 03:16:09 +0000100
101 Ctx = Ctx->getParent();
Douglas Gregor525f96c2010-02-05 07:33:43 +0000102 RelativeToPrimary = false;
Douglas Gregor54dabfc2009-05-14 23:26:13 +0000103 }
Mike Stump1eb44332009-09-09 15:08:12 +0000104
Douglas Gregord1102432009-08-28 17:37:35 +0000105 return Result;
Douglas Gregor54dabfc2009-05-14 23:26:13 +0000106}
107
Douglas Gregorf35f8282009-11-11 21:54:23 +0000108bool Sema::ActiveTemplateInstantiation::isInstantiationRecord() const {
109 switch (Kind) {
110 case TemplateInstantiation:
111 case DefaultTemplateArgumentInstantiation:
112 case DefaultFunctionArgumentInstantiation:
113 return true;
114
115 case ExplicitTemplateArgumentSubstitution:
116 case DeducedTemplateArgumentSubstitution:
117 case PriorTemplateArgumentSubstitution:
118 case DefaultTemplateArgumentChecking:
119 return false;
120 }
121
122 return true;
123}
124
Douglas Gregor26dce442009-03-10 00:06:19 +0000125Sema::InstantiatingTemplate::
126InstantiatingTemplate(Sema &SemaRef, SourceLocation PointOfInstantiation,
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000127 Decl *Entity,
Douglas Gregor26dce442009-03-10 00:06:19 +0000128 SourceRange InstantiationRange)
129 : SemaRef(SemaRef) {
Douglas Gregordf667e72009-03-10 20:44:00 +0000130
131 Invalid = CheckInstantiationDepth(PointOfInstantiation,
132 InstantiationRange);
133 if (!Invalid) {
Douglas Gregor26dce442009-03-10 00:06:19 +0000134 ActiveTemplateInstantiation Inst;
Douglas Gregordf667e72009-03-10 20:44:00 +0000135 Inst.Kind = ActiveTemplateInstantiation::TemplateInstantiation;
Douglas Gregor26dce442009-03-10 00:06:19 +0000136 Inst.PointOfInstantiation = PointOfInstantiation;
Douglas Gregordf667e72009-03-10 20:44:00 +0000137 Inst.Entity = reinterpret_cast<uintptr_t>(Entity);
Douglas Gregor313a81d2009-03-12 18:36:18 +0000138 Inst.TemplateArgs = 0;
139 Inst.NumTemplateArgs = 0;
Douglas Gregordf667e72009-03-10 20:44:00 +0000140 Inst.InstantiationRange = InstantiationRange;
141 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
Douglas Gregordf667e72009-03-10 20:44:00 +0000142 }
143}
144
Mike Stump1eb44332009-09-09 15:08:12 +0000145Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef,
Douglas Gregordf667e72009-03-10 20:44:00 +0000146 SourceLocation PointOfInstantiation,
147 TemplateDecl *Template,
148 const TemplateArgument *TemplateArgs,
149 unsigned NumTemplateArgs,
150 SourceRange InstantiationRange)
151 : SemaRef(SemaRef) {
152
153 Invalid = CheckInstantiationDepth(PointOfInstantiation,
154 InstantiationRange);
155 if (!Invalid) {
156 ActiveTemplateInstantiation Inst;
Mike Stump1eb44332009-09-09 15:08:12 +0000157 Inst.Kind
Douglas Gregordf667e72009-03-10 20:44:00 +0000158 = ActiveTemplateInstantiation::DefaultTemplateArgumentInstantiation;
159 Inst.PointOfInstantiation = PointOfInstantiation;
160 Inst.Entity = reinterpret_cast<uintptr_t>(Template);
161 Inst.TemplateArgs = TemplateArgs;
162 Inst.NumTemplateArgs = NumTemplateArgs;
Douglas Gregor26dce442009-03-10 00:06:19 +0000163 Inst.InstantiationRange = InstantiationRange;
164 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
Douglas Gregor26dce442009-03-10 00:06:19 +0000165 }
166}
167
Mike Stump1eb44332009-09-09 15:08:12 +0000168Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef,
Douglas Gregor637a4092009-06-10 23:47:09 +0000169 SourceLocation PointOfInstantiation,
Douglas Gregorcca9e962009-07-01 22:01:06 +0000170 FunctionTemplateDecl *FunctionTemplate,
171 const TemplateArgument *TemplateArgs,
172 unsigned NumTemplateArgs,
173 ActiveTemplateInstantiation::InstantiationKind Kind,
174 SourceRange InstantiationRange)
175: SemaRef(SemaRef) {
Mike Stump1eb44332009-09-09 15:08:12 +0000176
Douglas Gregorcca9e962009-07-01 22:01:06 +0000177 Invalid = CheckInstantiationDepth(PointOfInstantiation,
178 InstantiationRange);
179 if (!Invalid) {
180 ActiveTemplateInstantiation Inst;
181 Inst.Kind = Kind;
182 Inst.PointOfInstantiation = PointOfInstantiation;
183 Inst.Entity = reinterpret_cast<uintptr_t>(FunctionTemplate);
184 Inst.TemplateArgs = TemplateArgs;
185 Inst.NumTemplateArgs = NumTemplateArgs;
186 Inst.InstantiationRange = InstantiationRange;
187 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
Douglas Gregorf35f8282009-11-11 21:54:23 +0000188
189 if (!Inst.isInstantiationRecord())
190 ++SemaRef.NonInstantiationEntries;
Douglas Gregorcca9e962009-07-01 22:01:06 +0000191 }
192}
193
Mike Stump1eb44332009-09-09 15:08:12 +0000194Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef,
Douglas Gregorcca9e962009-07-01 22:01:06 +0000195 SourceLocation PointOfInstantiation,
Douglas Gregor637a4092009-06-10 23:47:09 +0000196 ClassTemplatePartialSpecializationDecl *PartialSpec,
197 const TemplateArgument *TemplateArgs,
198 unsigned NumTemplateArgs,
199 SourceRange InstantiationRange)
200 : SemaRef(SemaRef) {
201
Douglas Gregorf35f8282009-11-11 21:54:23 +0000202 Invalid = false;
203
204 ActiveTemplateInstantiation Inst;
205 Inst.Kind = ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution;
206 Inst.PointOfInstantiation = PointOfInstantiation;
207 Inst.Entity = reinterpret_cast<uintptr_t>(PartialSpec);
208 Inst.TemplateArgs = TemplateArgs;
209 Inst.NumTemplateArgs = NumTemplateArgs;
210 Inst.InstantiationRange = InstantiationRange;
211 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
212
213 assert(!Inst.isInstantiationRecord());
214 ++SemaRef.NonInstantiationEntries;
Douglas Gregor637a4092009-06-10 23:47:09 +0000215}
216
Mike Stump1eb44332009-09-09 15:08:12 +0000217Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef,
Douglas Gregor9148c3f2009-11-11 19:13:48 +0000218 SourceLocation PointOfInstantiation,
Anders Carlsson25cae7f2009-09-05 05:14:19 +0000219 ParmVarDecl *Param,
220 const TemplateArgument *TemplateArgs,
221 unsigned NumTemplateArgs,
222 SourceRange InstantiationRange)
223 : SemaRef(SemaRef) {
Mike Stump1eb44332009-09-09 15:08:12 +0000224
Douglas Gregor9148c3f2009-11-11 19:13:48 +0000225 Invalid = CheckInstantiationDepth(PointOfInstantiation, InstantiationRange);
Anders Carlsson25cae7f2009-09-05 05:14:19 +0000226
227 if (!Invalid) {
228 ActiveTemplateInstantiation Inst;
229 Inst.Kind
230 = ActiveTemplateInstantiation::DefaultFunctionArgumentInstantiation;
Douglas Gregor9148c3f2009-11-11 19:13:48 +0000231 Inst.PointOfInstantiation = PointOfInstantiation;
Anders Carlsson25cae7f2009-09-05 05:14:19 +0000232 Inst.Entity = reinterpret_cast<uintptr_t>(Param);
233 Inst.TemplateArgs = TemplateArgs;
234 Inst.NumTemplateArgs = NumTemplateArgs;
235 Inst.InstantiationRange = InstantiationRange;
236 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
Douglas Gregor9148c3f2009-11-11 19:13:48 +0000237 }
238}
239
240Sema::InstantiatingTemplate::
241InstantiatingTemplate(Sema &SemaRef, SourceLocation PointOfInstantiation,
242 TemplateDecl *Template,
243 NonTypeTemplateParmDecl *Param,
244 const TemplateArgument *TemplateArgs,
245 unsigned NumTemplateArgs,
246 SourceRange InstantiationRange) : SemaRef(SemaRef) {
Douglas Gregorf35f8282009-11-11 21:54:23 +0000247 Invalid = false;
Douglas Gregor9148c3f2009-11-11 19:13:48 +0000248
Douglas Gregorf35f8282009-11-11 21:54:23 +0000249 ActiveTemplateInstantiation Inst;
250 Inst.Kind = ActiveTemplateInstantiation::PriorTemplateArgumentSubstitution;
251 Inst.PointOfInstantiation = PointOfInstantiation;
252 Inst.Template = Template;
253 Inst.Entity = reinterpret_cast<uintptr_t>(Param);
254 Inst.TemplateArgs = TemplateArgs;
255 Inst.NumTemplateArgs = NumTemplateArgs;
256 Inst.InstantiationRange = InstantiationRange;
257 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
258
259 assert(!Inst.isInstantiationRecord());
260 ++SemaRef.NonInstantiationEntries;
Douglas Gregor9148c3f2009-11-11 19:13:48 +0000261}
262
263Sema::InstantiatingTemplate::
264InstantiatingTemplate(Sema &SemaRef, SourceLocation PointOfInstantiation,
265 TemplateDecl *Template,
266 TemplateTemplateParmDecl *Param,
267 const TemplateArgument *TemplateArgs,
268 unsigned NumTemplateArgs,
269 SourceRange InstantiationRange) : SemaRef(SemaRef) {
Douglas Gregorf35f8282009-11-11 21:54:23 +0000270 Invalid = false;
271 ActiveTemplateInstantiation Inst;
272 Inst.Kind = ActiveTemplateInstantiation::PriorTemplateArgumentSubstitution;
273 Inst.PointOfInstantiation = PointOfInstantiation;
274 Inst.Template = Template;
275 Inst.Entity = reinterpret_cast<uintptr_t>(Param);
276 Inst.TemplateArgs = TemplateArgs;
277 Inst.NumTemplateArgs = NumTemplateArgs;
278 Inst.InstantiationRange = InstantiationRange;
279 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
Douglas Gregor9148c3f2009-11-11 19:13:48 +0000280
Douglas Gregorf35f8282009-11-11 21:54:23 +0000281 assert(!Inst.isInstantiationRecord());
282 ++SemaRef.NonInstantiationEntries;
283}
284
285Sema::InstantiatingTemplate::
286InstantiatingTemplate(Sema &SemaRef, SourceLocation PointOfInstantiation,
287 TemplateDecl *Template,
288 NamedDecl *Param,
289 const TemplateArgument *TemplateArgs,
290 unsigned NumTemplateArgs,
291 SourceRange InstantiationRange) : SemaRef(SemaRef) {
292 Invalid = false;
293
294 ActiveTemplateInstantiation Inst;
295 Inst.Kind = ActiveTemplateInstantiation::DefaultTemplateArgumentChecking;
296 Inst.PointOfInstantiation = PointOfInstantiation;
297 Inst.Template = Template;
298 Inst.Entity = reinterpret_cast<uintptr_t>(Param);
299 Inst.TemplateArgs = TemplateArgs;
300 Inst.NumTemplateArgs = NumTemplateArgs;
301 Inst.InstantiationRange = InstantiationRange;
302 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
303
304 assert(!Inst.isInstantiationRecord());
305 ++SemaRef.NonInstantiationEntries;
Anders Carlsson25cae7f2009-09-05 05:14:19 +0000306}
307
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000308void Sema::InstantiatingTemplate::Clear() {
309 if (!Invalid) {
Douglas Gregorf35f8282009-11-11 21:54:23 +0000310 if (!SemaRef.ActiveTemplateInstantiations.back().isInstantiationRecord()) {
311 assert(SemaRef.NonInstantiationEntries > 0);
312 --SemaRef.NonInstantiationEntries;
313 }
314
Douglas Gregor26dce442009-03-10 00:06:19 +0000315 SemaRef.ActiveTemplateInstantiations.pop_back();
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000316 Invalid = true;
317 }
Douglas Gregor26dce442009-03-10 00:06:19 +0000318}
319
Douglas Gregordf667e72009-03-10 20:44:00 +0000320bool Sema::InstantiatingTemplate::CheckInstantiationDepth(
321 SourceLocation PointOfInstantiation,
322 SourceRange InstantiationRange) {
Douglas Gregorf35f8282009-11-11 21:54:23 +0000323 assert(SemaRef.NonInstantiationEntries <=
324 SemaRef.ActiveTemplateInstantiations.size());
325 if ((SemaRef.ActiveTemplateInstantiations.size() -
326 SemaRef.NonInstantiationEntries)
327 <= SemaRef.getLangOptions().InstantiationDepth)
Douglas Gregordf667e72009-03-10 20:44:00 +0000328 return false;
329
Mike Stump1eb44332009-09-09 15:08:12 +0000330 SemaRef.Diag(PointOfInstantiation,
Douglas Gregordf667e72009-03-10 20:44:00 +0000331 diag::err_template_recursion_depth_exceeded)
332 << SemaRef.getLangOptions().InstantiationDepth
333 << InstantiationRange;
334 SemaRef.Diag(PointOfInstantiation, diag::note_template_recursion_depth)
335 << SemaRef.getLangOptions().InstantiationDepth;
336 return true;
337}
338
Douglas Gregoree1828a2009-03-10 18:03:33 +0000339/// \brief Prints the current instantiation stack through a series of
340/// notes.
341void Sema::PrintInstantiationStack() {
Douglas Gregor575cf372010-04-20 07:18:24 +0000342 // Determine which template instantiations to skip, if any.
343 unsigned SkipStart = ActiveTemplateInstantiations.size(), SkipEnd = SkipStart;
344 unsigned Limit = Diags.getTemplateBacktraceLimit();
345 if (Limit && Limit < ActiveTemplateInstantiations.size()) {
346 SkipStart = Limit / 2 + Limit % 2;
347 SkipEnd = ActiveTemplateInstantiations.size() - Limit / 2;
348 }
349
Douglas Gregorcca9e962009-07-01 22:01:06 +0000350 // FIXME: In all of these cases, we need to show the template arguments
Douglas Gregor575cf372010-04-20 07:18:24 +0000351 unsigned InstantiationIdx = 0;
Douglas Gregoree1828a2009-03-10 18:03:33 +0000352 for (llvm::SmallVector<ActiveTemplateInstantiation, 16>::reverse_iterator
353 Active = ActiveTemplateInstantiations.rbegin(),
354 ActiveEnd = ActiveTemplateInstantiations.rend();
355 Active != ActiveEnd;
Douglas Gregor575cf372010-04-20 07:18:24 +0000356 ++Active, ++InstantiationIdx) {
357 // Skip this instantiation?
358 if (InstantiationIdx >= SkipStart && InstantiationIdx < SkipEnd) {
359 if (InstantiationIdx == SkipStart) {
360 // Note that we're skipping instantiations.
361 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
362 diag::note_instantiation_contexts_suppressed)
363 << unsigned(ActiveTemplateInstantiations.size() - Limit);
364 }
365 continue;
366 }
367
Douglas Gregordf667e72009-03-10 20:44:00 +0000368 switch (Active->Kind) {
369 case ActiveTemplateInstantiation::TemplateInstantiation: {
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000370 Decl *D = reinterpret_cast<Decl *>(Active->Entity);
371 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) {
372 unsigned DiagID = diag::note_template_member_class_here;
373 if (isa<ClassTemplateSpecializationDecl>(Record))
374 DiagID = diag::note_template_class_instantiation_here;
Mike Stump1eb44332009-09-09 15:08:12 +0000375 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000376 DiagID)
377 << Context.getTypeDeclType(Record)
378 << Active->InstantiationRange;
Douglas Gregor7caa6822009-07-24 20:34:43 +0000379 } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
Douglas Gregor1637be72009-06-26 00:10:03 +0000380 unsigned DiagID;
381 if (Function->getPrimaryTemplate())
382 DiagID = diag::note_function_template_spec_here;
383 else
384 DiagID = diag::note_template_member_function_here;
Mike Stump1eb44332009-09-09 15:08:12 +0000385 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000386 DiagID)
387 << Function
388 << Active->InstantiationRange;
Douglas Gregor7caa6822009-07-24 20:34:43 +0000389 } else {
390 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
391 diag::note_template_static_data_member_def_here)
392 << cast<VarDecl>(D)
393 << Active->InstantiationRange;
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000394 }
Douglas Gregordf667e72009-03-10 20:44:00 +0000395 break;
396 }
397
398 case ActiveTemplateInstantiation::DefaultTemplateArgumentInstantiation: {
399 TemplateDecl *Template = cast<TemplateDecl>((Decl *)Active->Entity);
400 std::string TemplateArgsStr
Douglas Gregor7532dc62009-03-30 22:58:21 +0000401 = TemplateSpecializationType::PrintTemplateArgumentList(
Mike Stump1eb44332009-09-09 15:08:12 +0000402 Active->TemplateArgs,
Douglas Gregord249e1d1f2009-05-29 20:38:28 +0000403 Active->NumTemplateArgs,
404 Context.PrintingPolicy);
Douglas Gregordf667e72009-03-10 20:44:00 +0000405 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
406 diag::note_default_arg_instantiation_here)
407 << (Template->getNameAsString() + TemplateArgsStr)
408 << Active->InstantiationRange;
409 break;
410 }
Douglas Gregor637a4092009-06-10 23:47:09 +0000411
Douglas Gregorcca9e962009-07-01 22:01:06 +0000412 case ActiveTemplateInstantiation::ExplicitTemplateArgumentSubstitution: {
Mike Stump1eb44332009-09-09 15:08:12 +0000413 FunctionTemplateDecl *FnTmpl
Douglas Gregorcca9e962009-07-01 22:01:06 +0000414 = cast<FunctionTemplateDecl>((Decl *)Active->Entity);
Douglas Gregor637a4092009-06-10 23:47:09 +0000415 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
Douglas Gregorcca9e962009-07-01 22:01:06 +0000416 diag::note_explicit_template_arg_substitution_here)
Douglas Gregor5e402912010-03-30 20:35:20 +0000417 << FnTmpl
418 << getTemplateArgumentBindingsText(FnTmpl->getTemplateParameters(),
419 Active->TemplateArgs,
420 Active->NumTemplateArgs)
421 << Active->InstantiationRange;
Douglas Gregor637a4092009-06-10 23:47:09 +0000422 break;
423 }
Mike Stump1eb44332009-09-09 15:08:12 +0000424
Douglas Gregorcca9e962009-07-01 22:01:06 +0000425 case ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution:
426 if (ClassTemplatePartialSpecializationDecl *PartialSpec
427 = dyn_cast<ClassTemplatePartialSpecializationDecl>(
428 (Decl *)Active->Entity)) {
429 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
430 diag::note_partial_spec_deduct_instantiation_here)
431 << Context.getTypeDeclType(PartialSpec)
Douglas Gregor5e402912010-03-30 20:35:20 +0000432 << getTemplateArgumentBindingsText(
433 PartialSpec->getTemplateParameters(),
434 Active->TemplateArgs,
435 Active->NumTemplateArgs)
Douglas Gregorcca9e962009-07-01 22:01:06 +0000436 << Active->InstantiationRange;
437 } else {
438 FunctionTemplateDecl *FnTmpl
439 = cast<FunctionTemplateDecl>((Decl *)Active->Entity);
440 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
441 diag::note_function_template_deduction_instantiation_here)
Douglas Gregor5e402912010-03-30 20:35:20 +0000442 << FnTmpl
443 << getTemplateArgumentBindingsText(FnTmpl->getTemplateParameters(),
444 Active->TemplateArgs,
445 Active->NumTemplateArgs)
446 << Active->InstantiationRange;
Douglas Gregorcca9e962009-07-01 22:01:06 +0000447 }
448 break;
Douglas Gregor637a4092009-06-10 23:47:09 +0000449
Anders Carlsson25cae7f2009-09-05 05:14:19 +0000450 case ActiveTemplateInstantiation::DefaultFunctionArgumentInstantiation: {
451 ParmVarDecl *Param = cast<ParmVarDecl>((Decl *)Active->Entity);
452 FunctionDecl *FD = cast<FunctionDecl>(Param->getDeclContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000453
Anders Carlsson25cae7f2009-09-05 05:14:19 +0000454 std::string TemplateArgsStr
455 = TemplateSpecializationType::PrintTemplateArgumentList(
Mike Stump1eb44332009-09-09 15:08:12 +0000456 Active->TemplateArgs,
Anders Carlsson25cae7f2009-09-05 05:14:19 +0000457 Active->NumTemplateArgs,
458 Context.PrintingPolicy);
459 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
460 diag::note_default_function_arg_instantiation_here)
Anders Carlsson6bc107b2009-09-05 05:38:54 +0000461 << (FD->getNameAsString() + TemplateArgsStr)
Anders Carlsson25cae7f2009-09-05 05:14:19 +0000462 << Active->InstantiationRange;
463 break;
464 }
Mike Stump1eb44332009-09-09 15:08:12 +0000465
Douglas Gregor9148c3f2009-11-11 19:13:48 +0000466 case ActiveTemplateInstantiation::PriorTemplateArgumentSubstitution: {
467 NamedDecl *Parm = cast<NamedDecl>((Decl *)Active->Entity);
468 std::string Name;
469 if (!Parm->getName().empty())
470 Name = std::string(" '") + Parm->getName().str() + "'";
471
472 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
473 diag::note_prior_template_arg_substitution)
474 << isa<TemplateTemplateParmDecl>(Parm)
475 << Name
476 << getTemplateArgumentBindingsText(
477 Active->Template->getTemplateParameters(),
478 Active->TemplateArgs,
479 Active->NumTemplateArgs)
480 << Active->InstantiationRange;
481 break;
482 }
Douglas Gregorf35f8282009-11-11 21:54:23 +0000483
484 case ActiveTemplateInstantiation::DefaultTemplateArgumentChecking: {
485 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
486 diag::note_template_default_arg_checking)
487 << getTemplateArgumentBindingsText(
488 Active->Template->getTemplateParameters(),
489 Active->TemplateArgs,
490 Active->NumTemplateArgs)
491 << Active->InstantiationRange;
492 break;
493 }
Douglas Gregordf667e72009-03-10 20:44:00 +0000494 }
Douglas Gregoree1828a2009-03-10 18:03:33 +0000495 }
496}
497
Douglas Gregor5e9f35c2009-06-14 07:33:30 +0000498bool Sema::isSFINAEContext() const {
499 using llvm::SmallVector;
500 for (SmallVector<ActiveTemplateInstantiation, 16>::const_reverse_iterator
501 Active = ActiveTemplateInstantiations.rbegin(),
502 ActiveEnd = ActiveTemplateInstantiations.rend();
503 Active != ActiveEnd;
Douglas Gregorf35f8282009-11-11 21:54:23 +0000504 ++Active)
505 {
Douglas Gregor5e9f35c2009-06-14 07:33:30 +0000506 switch(Active->Kind) {
Douglas Gregorcca9e962009-07-01 22:01:06 +0000507 case ActiveTemplateInstantiation::TemplateInstantiation:
Anders Carlsson25cae7f2009-09-05 05:14:19 +0000508 case ActiveTemplateInstantiation::DefaultFunctionArgumentInstantiation:
Douglas Gregorcca9e962009-07-01 22:01:06 +0000509 // This is a template instantiation, so there is no SFINAE.
510 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000511
Douglas Gregor5e9f35c2009-06-14 07:33:30 +0000512 case ActiveTemplateInstantiation::DefaultTemplateArgumentInstantiation:
Douglas Gregor9148c3f2009-11-11 19:13:48 +0000513 case ActiveTemplateInstantiation::PriorTemplateArgumentSubstitution:
Douglas Gregorf35f8282009-11-11 21:54:23 +0000514 case ActiveTemplateInstantiation::DefaultTemplateArgumentChecking:
Douglas Gregor9148c3f2009-11-11 19:13:48 +0000515 // A default template argument instantiation and substitution into
516 // template parameters with arguments for prior parameters may or may
517 // not be a SFINAE context; look further up the stack.
Douglas Gregor5e9f35c2009-06-14 07:33:30 +0000518 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000519
Douglas Gregorcca9e962009-07-01 22:01:06 +0000520 case ActiveTemplateInstantiation::ExplicitTemplateArgumentSubstitution:
521 case ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution:
522 // We're either substitution explicitly-specified template arguments
523 // or deduced template arguments, so SFINAE applies.
524 return true;
Douglas Gregor5e9f35c2009-06-14 07:33:30 +0000525 }
526 }
527
528 return false;
529}
530
Douglas Gregor99ebf652009-02-27 19:31:52 +0000531//===----------------------------------------------------------------------===/
532// Template Instantiation for Types
533//===----------------------------------------------------------------------===/
Douglas Gregorcd281c32009-02-28 00:25:32 +0000534namespace {
Benjamin Kramer85b45212009-11-28 19:45:26 +0000535 class TemplateInstantiator
Mike Stump1eb44332009-09-09 15:08:12 +0000536 : public TreeTransform<TemplateInstantiator> {
Douglas Gregord6350ae2009-08-28 20:31:08 +0000537 const MultiLevelTemplateArgumentList &TemplateArgs;
Douglas Gregorcd281c32009-02-28 00:25:32 +0000538 SourceLocation Loc;
539 DeclarationName Entity;
Douglas Gregor99ebf652009-02-27 19:31:52 +0000540
Douglas Gregorcd281c32009-02-28 00:25:32 +0000541 public:
Douglas Gregor43959a92009-08-20 07:17:43 +0000542 typedef TreeTransform<TemplateInstantiator> inherited;
Mike Stump1eb44332009-09-09 15:08:12 +0000543
544 TemplateInstantiator(Sema &SemaRef,
Douglas Gregord6350ae2009-08-28 20:31:08 +0000545 const MultiLevelTemplateArgumentList &TemplateArgs,
Douglas Gregor577f75a2009-08-04 16:50:30 +0000546 SourceLocation Loc,
Mike Stump1eb44332009-09-09 15:08:12 +0000547 DeclarationName Entity)
548 : inherited(SemaRef), TemplateArgs(TemplateArgs), Loc(Loc),
Douglas Gregor43959a92009-08-20 07:17:43 +0000549 Entity(Entity) { }
Douglas Gregorcd281c32009-02-28 00:25:32 +0000550
Mike Stump1eb44332009-09-09 15:08:12 +0000551 /// \brief Determine whether the given type \p T has already been
Douglas Gregor577f75a2009-08-04 16:50:30 +0000552 /// transformed.
553 ///
554 /// For the purposes of template instantiation, a type has already been
555 /// transformed if it is NULL or if it is not dependent.
556 bool AlreadyTransformed(QualType T) {
557 return T.isNull() || !T->isDependentType();
Douglas Gregorff668032009-05-13 18:28:20 +0000558 }
Mike Stump1eb44332009-09-09 15:08:12 +0000559
Douglas Gregor577f75a2009-08-04 16:50:30 +0000560 /// \brief Returns the location of the entity being instantiated, if known.
561 SourceLocation getBaseLocation() { return Loc; }
Mike Stump1eb44332009-09-09 15:08:12 +0000562
Douglas Gregor577f75a2009-08-04 16:50:30 +0000563 /// \brief Returns the name of the entity being instantiated, if any.
564 DeclarationName getBaseEntity() { return Entity; }
Mike Stump1eb44332009-09-09 15:08:12 +0000565
Douglas Gregor972e6ce2009-10-27 06:26:26 +0000566 /// \brief Sets the "base" location and entity when that
567 /// information is known based on another transformation.
568 void setBase(SourceLocation Loc, DeclarationName Entity) {
569 this->Loc = Loc;
570 this->Entity = Entity;
571 }
572
Douglas Gregor577f75a2009-08-04 16:50:30 +0000573 /// \brief Transform the given declaration by instantiating a reference to
574 /// this declaration.
Douglas Gregor7c1e98f2010-03-01 15:56:25 +0000575 Decl *TransformDecl(SourceLocation Loc, Decl *D);
Douglas Gregorb98b1992009-08-11 05:31:07 +0000576
Mike Stump1eb44332009-09-09 15:08:12 +0000577 /// \brief Transform the definition of the given declaration by
Douglas Gregor43959a92009-08-20 07:17:43 +0000578 /// instantiating it.
Douglas Gregoraac571c2010-03-01 17:25:41 +0000579 Decl *TransformDefinition(SourceLocation Loc, Decl *D);
Mike Stump1eb44332009-09-09 15:08:12 +0000580
Douglas Gregor6cd21982009-10-20 05:58:46 +0000581 /// \bried Transform the first qualifier within a scope by instantiating the
582 /// declaration.
583 NamedDecl *TransformFirstQualifierInScope(NamedDecl *D, SourceLocation Loc);
584
Douglas Gregor43959a92009-08-20 07:17:43 +0000585 /// \brief Rebuild the exception declaration and register the declaration
586 /// as an instantiated local.
Mike Stump1eb44332009-09-09 15:08:12 +0000587 VarDecl *RebuildExceptionDecl(VarDecl *ExceptionDecl, QualType T,
John McCalla93c9342009-12-07 02:54:59 +0000588 TypeSourceInfo *Declarator,
Douglas Gregor43959a92009-08-20 07:17:43 +0000589 IdentifierInfo *Name,
590 SourceLocation Loc, SourceRange TypeRange);
Mike Stump1eb44332009-09-09 15:08:12 +0000591
Douglas Gregorbe270a02010-04-26 17:57:08 +0000592 /// \brief Rebuild the Objective-C exception declaration and register the
593 /// declaration as an instantiated local.
594 VarDecl *RebuildObjCExceptionDecl(VarDecl *ExceptionDecl,
595 TypeSourceInfo *TSInfo, QualType T);
596
John McCallc4e70192009-09-11 04:59:25 +0000597 /// \brief Check for tag mismatches when instantiating an
598 /// elaborated type.
599 QualType RebuildElaboratedType(QualType T, ElaboratedType::TagKind Tag);
600
John McCall454feb92009-12-08 09:21:05 +0000601 Sema::OwningExprResult TransformPredefinedExpr(PredefinedExpr *E);
602 Sema::OwningExprResult TransformDeclRefExpr(DeclRefExpr *E);
John McCall454feb92009-12-08 09:21:05 +0000603 Sema::OwningExprResult TransformCXXDefaultArgExpr(CXXDefaultArgExpr *E);
John McCallb8fc0532010-02-06 08:42:39 +0000604 Sema::OwningExprResult TransformTemplateParmRefExpr(DeclRefExpr *E,
605 NonTypeTemplateParmDecl *D);
Sebastian Redla29e51b2009-11-08 13:56:19 +0000606
John McCall21ef0fa2010-03-11 09:03:00 +0000607 /// \brief Transforms a function proto type by performing
608 /// substitution in the function parameters, possibly adjusting
609 /// their types and marking default arguments as uninstantiated.
610 bool TransformFunctionTypeParams(FunctionProtoTypeLoc TL,
611 llvm::SmallVectorImpl<QualType> &PTypes,
612 llvm::SmallVectorImpl<ParmVarDecl*> &PVars);
613
614 ParmVarDecl *TransformFunctionTypeParam(ParmVarDecl *OldParm);
615
Mike Stump1eb44332009-09-09 15:08:12 +0000616 /// \brief Transforms a template type parameter type by performing
Douglas Gregor577f75a2009-08-04 16:50:30 +0000617 /// substitution of the corresponding template type argument.
John McCalla2becad2009-10-21 00:40:46 +0000618 QualType TransformTemplateTypeParmType(TypeLocBuilder &TLB,
Douglas Gregor124b8782010-02-16 19:09:40 +0000619 TemplateTypeParmTypeLoc TL,
620 QualType ObjectType);
Douglas Gregor577f75a2009-08-04 16:50:30 +0000621 };
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000622}
623
Douglas Gregor7c1e98f2010-03-01 15:56:25 +0000624Decl *TemplateInstantiator::TransformDecl(SourceLocation Loc, Decl *D) {
Douglas Gregorc68afe22009-09-03 21:38:09 +0000625 if (!D)
626 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000627
Douglas Gregorc68afe22009-09-03 21:38:09 +0000628 if (TemplateTemplateParmDecl *TTP = dyn_cast<TemplateTemplateParmDecl>(D)) {
Douglas Gregord6350ae2009-08-28 20:31:08 +0000629 if (TTP->getDepth() < TemplateArgs.getNumLevels()) {
Douglas Gregor6d3e6272010-02-05 19:54:12 +0000630 // If the corresponding template argument is NULL or non-existent, it's
631 // because we are performing instantiation from explicitly-specified
632 // template arguments in a function template, but there were some
633 // arguments left unspecified.
634 if (!TemplateArgs.hasTemplateArgument(TTP->getDepth(),
635 TTP->getPosition()))
636 return D;
637
Douglas Gregor788cd062009-11-11 01:00:40 +0000638 TemplateName Template
639 = TemplateArgs(TTP->getDepth(), TTP->getPosition()).getAsTemplate();
640 assert(!Template.isNull() && Template.getAsTemplateDecl() &&
Douglas Gregord6350ae2009-08-28 20:31:08 +0000641 "Wrong kind of template template argument");
Douglas Gregor788cd062009-11-11 01:00:40 +0000642 return Template.getAsTemplateDecl();
Douglas Gregord6350ae2009-08-28 20:31:08 +0000643 }
Mike Stump1eb44332009-09-09 15:08:12 +0000644
Douglas Gregor788cd062009-11-11 01:00:40 +0000645 // Fall through to find the instantiated declaration for this template
646 // template parameter.
Douglas Gregord1067e52009-08-06 06:41:21 +0000647 }
Mike Stump1eb44332009-09-09 15:08:12 +0000648
Douglas Gregor7c1e98f2010-03-01 15:56:25 +0000649 return SemaRef.FindInstantiatedDecl(Loc, cast<NamedDecl>(D), TemplateArgs);
Douglas Gregor577f75a2009-08-04 16:50:30 +0000650}
651
Douglas Gregoraac571c2010-03-01 17:25:41 +0000652Decl *TemplateInstantiator::TransformDefinition(SourceLocation Loc, Decl *D) {
John McCallce3ff2b2009-08-25 22:02:44 +0000653 Decl *Inst = getSema().SubstDecl(D, getSema().CurContext, TemplateArgs);
Douglas Gregor43959a92009-08-20 07:17:43 +0000654 if (!Inst)
655 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000656
Douglas Gregor43959a92009-08-20 07:17:43 +0000657 getSema().CurrentInstantiationScope->InstantiatedLocal(D, Inst);
658 return Inst;
659}
660
Douglas Gregor6cd21982009-10-20 05:58:46 +0000661NamedDecl *
662TemplateInstantiator::TransformFirstQualifierInScope(NamedDecl *D,
663 SourceLocation Loc) {
664 // If the first part of the nested-name-specifier was a template type
665 // parameter, instantiate that type parameter down to a tag type.
666 if (TemplateTypeParmDecl *TTPD = dyn_cast_or_null<TemplateTypeParmDecl>(D)) {
667 const TemplateTypeParmType *TTP
668 = cast<TemplateTypeParmType>(getSema().Context.getTypeDeclType(TTPD));
669 if (TTP->getDepth() < TemplateArgs.getNumLevels()) {
670 QualType T = TemplateArgs(TTP->getDepth(), TTP->getIndex()).getAsType();
671 if (T.isNull())
Douglas Gregor7c1e98f2010-03-01 15:56:25 +0000672 return cast_or_null<NamedDecl>(TransformDecl(Loc, D));
Douglas Gregor6cd21982009-10-20 05:58:46 +0000673
674 if (const TagType *Tag = T->getAs<TagType>())
675 return Tag->getDecl();
676
677 // The resulting type is not a tag; complain.
678 getSema().Diag(Loc, diag::err_nested_name_spec_non_tag) << T;
679 return 0;
680 }
681 }
682
Douglas Gregor7c1e98f2010-03-01 15:56:25 +0000683 return cast_or_null<NamedDecl>(TransformDecl(Loc, D));
Douglas Gregor6cd21982009-10-20 05:58:46 +0000684}
685
Douglas Gregor43959a92009-08-20 07:17:43 +0000686VarDecl *
687TemplateInstantiator::RebuildExceptionDecl(VarDecl *ExceptionDecl,
Mike Stump1eb44332009-09-09 15:08:12 +0000688 QualType T,
John McCalla93c9342009-12-07 02:54:59 +0000689 TypeSourceInfo *Declarator,
Douglas Gregor43959a92009-08-20 07:17:43 +0000690 IdentifierInfo *Name,
Mike Stump1eb44332009-09-09 15:08:12 +0000691 SourceLocation Loc,
Douglas Gregor43959a92009-08-20 07:17:43 +0000692 SourceRange TypeRange) {
693 VarDecl *Var = inherited::RebuildExceptionDecl(ExceptionDecl, T, Declarator,
694 Name, Loc, TypeRange);
Douglas Gregorbe270a02010-04-26 17:57:08 +0000695 if (Var)
696 getSema().CurrentInstantiationScope->InstantiatedLocal(ExceptionDecl, Var);
697 return Var;
698}
699
700VarDecl *TemplateInstantiator::RebuildObjCExceptionDecl(VarDecl *ExceptionDecl,
701 TypeSourceInfo *TSInfo,
702 QualType T) {
703 VarDecl *Var = inherited::RebuildObjCExceptionDecl(ExceptionDecl, TSInfo, T);
704 if (Var)
Douglas Gregor43959a92009-08-20 07:17:43 +0000705 getSema().CurrentInstantiationScope->InstantiatedLocal(ExceptionDecl, Var);
706 return Var;
707}
708
John McCallc4e70192009-09-11 04:59:25 +0000709QualType
710TemplateInstantiator::RebuildElaboratedType(QualType T,
711 ElaboratedType::TagKind Tag) {
712 if (const TagType *TT = T->getAs<TagType>()) {
713 TagDecl* TD = TT->getDecl();
714
715 // FIXME: this location is very wrong; we really need typelocs.
716 SourceLocation TagLocation = TD->getTagKeywordLoc();
717
718 // FIXME: type might be anonymous.
719 IdentifierInfo *Id = TD->getIdentifier();
720
721 // TODO: should we even warn on struct/class mismatches for this? Seems
722 // like it's likely to produce a lot of spurious errors.
723 if (!SemaRef.isAcceptableTagRedeclaration(TD, Tag, TagLocation, *Id)) {
724 SemaRef.Diag(TagLocation, diag::err_use_with_wrong_tag)
725 << Id
Douglas Gregor849b2432010-03-31 17:46:05 +0000726 << FixItHint::CreateReplacement(SourceRange(TagLocation),
727 TD->getKindName());
John McCallc4e70192009-09-11 04:59:25 +0000728 SemaRef.Diag(TD->getLocation(), diag::note_previous_use);
729 }
730 }
731
732 return TreeTransform<TemplateInstantiator>::RebuildElaboratedType(T, Tag);
733}
734
735Sema::OwningExprResult
John McCall454feb92009-12-08 09:21:05 +0000736TemplateInstantiator::TransformPredefinedExpr(PredefinedExpr *E) {
Anders Carlsson773f3972009-09-11 01:22:35 +0000737 if (!E->isTypeDependent())
738 return SemaRef.Owned(E->Retain());
739
740 FunctionDecl *currentDecl = getSema().getCurFunctionDecl();
741 assert(currentDecl && "Must have current function declaration when "
742 "instantiating.");
743
744 PredefinedExpr::IdentType IT = E->getIdentType();
745
Anders Carlsson848fa642010-02-11 18:20:28 +0000746 unsigned Length = PredefinedExpr::ComputeName(IT, currentDecl).length();
Anders Carlsson773f3972009-09-11 01:22:35 +0000747
748 llvm::APInt LengthI(32, Length + 1);
John McCall0953e762009-09-24 19:53:00 +0000749 QualType ResTy = getSema().Context.CharTy.withConst();
Anders Carlsson773f3972009-09-11 01:22:35 +0000750 ResTy = getSema().Context.getConstantArrayType(ResTy, LengthI,
751 ArrayType::Normal, 0);
752 PredefinedExpr *PE =
753 new (getSema().Context) PredefinedExpr(E->getLocation(), ResTy, IT);
754 return getSema().Owned(PE);
755}
756
757Sema::OwningExprResult
John McCallb8fc0532010-02-06 08:42:39 +0000758TemplateInstantiator::TransformTemplateParmRefExpr(DeclRefExpr *E,
Douglas Gregordcee9802010-02-08 23:41:45 +0000759 NonTypeTemplateParmDecl *NTTP) {
John McCallb8fc0532010-02-06 08:42:39 +0000760 // If the corresponding template argument is NULL or non-existent, it's
761 // because we are performing instantiation from explicitly-specified
762 // template arguments in a function template, but there were some
763 // arguments left unspecified.
764 if (!TemplateArgs.hasTemplateArgument(NTTP->getDepth(),
765 NTTP->getPosition()))
766 return SemaRef.Owned(E->Retain());
Mike Stump1eb44332009-09-09 15:08:12 +0000767
John McCallb8fc0532010-02-06 08:42:39 +0000768 const TemplateArgument &Arg = TemplateArgs(NTTP->getDepth(),
769 NTTP->getPosition());
Mike Stump1eb44332009-09-09 15:08:12 +0000770
John McCallb8fc0532010-02-06 08:42:39 +0000771 // The template argument itself might be an expression, in which
772 // case we just return that expression.
773 if (Arg.getKind() == TemplateArgument::Expression)
774 return SemaRef.Owned(Arg.getAsExpr()->Retain());
Mike Stump1eb44332009-09-09 15:08:12 +0000775
John McCallb8fc0532010-02-06 08:42:39 +0000776 if (Arg.getKind() == TemplateArgument::Declaration) {
777 ValueDecl *VD = cast<ValueDecl>(Arg.getAsDecl());
Mike Stump1eb44332009-09-09 15:08:12 +0000778
John McCall645cf442010-02-06 10:23:53 +0000779 // Find the instantiation of the template argument. This is
780 // required for nested templates.
John McCallb8fc0532010-02-06 08:42:39 +0000781 VD = cast_or_null<ValueDecl>(
Douglas Gregor7c1e98f2010-03-01 15:56:25 +0000782 getSema().FindInstantiatedDecl(E->getLocation(),
783 VD, TemplateArgs));
John McCallb8fc0532010-02-06 08:42:39 +0000784 if (!VD)
785 return SemaRef.ExprError();
Mike Stump1eb44332009-09-09 15:08:12 +0000786
John McCall645cf442010-02-06 10:23:53 +0000787 // Derive the type we want the substituted decl to have. This had
788 // better be non-dependent, or these checks will have serious problems.
789 QualType TargetType = SemaRef.SubstType(NTTP->getType(), TemplateArgs,
Douglas Gregordcee9802010-02-08 23:41:45 +0000790 E->getLocation(),
791 DeclarationName());
John McCall645cf442010-02-06 10:23:53 +0000792 assert(!TargetType.isNull() && "type substitution failed for param type");
793 assert(!TargetType->isDependentType() && "param type still dependent");
Douglas Gregor02024a92010-03-28 02:42:43 +0000794 return SemaRef.BuildExpressionFromDeclTemplateArgument(Arg,
795 TargetType,
796 E->getLocation());
John McCallb8fc0532010-02-06 08:42:39 +0000797 }
798
Douglas Gregor02024a92010-03-28 02:42:43 +0000799 return SemaRef.BuildExpressionFromIntegralTemplateArgument(Arg,
800 E->getSourceRange().getBegin());
John McCallb8fc0532010-02-06 08:42:39 +0000801}
802
803
804Sema::OwningExprResult
805TemplateInstantiator::TransformDeclRefExpr(DeclRefExpr *E) {
806 NamedDecl *D = E->getDecl();
807 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(D)) {
808 if (NTTP->getDepth() < TemplateArgs.getNumLevels())
809 return TransformTemplateParmRefExpr(E, NTTP);
Douglas Gregor550d9b22009-10-31 17:21:17 +0000810
811 // We have a non-type template parameter that isn't fully substituted;
812 // FindInstantiatedDecl will find it in the local instantiation scope.
Douglas Gregorb98b1992009-08-11 05:31:07 +0000813 }
Mike Stump1eb44332009-09-09 15:08:12 +0000814
John McCall454feb92009-12-08 09:21:05 +0000815 return TreeTransform<TemplateInstantiator>::TransformDeclRefExpr(E);
Douglas Gregorb98b1992009-08-11 05:31:07 +0000816}
817
Sebastian Redla29e51b2009-11-08 13:56:19 +0000818Sema::OwningExprResult TemplateInstantiator::TransformCXXDefaultArgExpr(
John McCall454feb92009-12-08 09:21:05 +0000819 CXXDefaultArgExpr *E) {
Sebastian Redla29e51b2009-11-08 13:56:19 +0000820 assert(!cast<FunctionDecl>(E->getParam()->getDeclContext())->
821 getDescribedFunctionTemplate() &&
822 "Default arg expressions are never formed in dependent cases.");
Douglas Gregor036aed12009-12-23 23:03:06 +0000823 return SemaRef.BuildCXXDefaultArgExpr(E->getUsedLocation(),
824 cast<FunctionDecl>(E->getParam()->getDeclContext()),
825 E->getParam());
Sebastian Redla29e51b2009-11-08 13:56:19 +0000826}
827
828
John McCall21ef0fa2010-03-11 09:03:00 +0000829bool
830TemplateInstantiator::TransformFunctionTypeParams(FunctionProtoTypeLoc TL,
831 llvm::SmallVectorImpl<QualType> &PTypes,
832 llvm::SmallVectorImpl<ParmVarDecl*> &PVars) {
833 // Create a local instantiation scope for the parameters.
Douglas Gregor2b0749a42010-03-25 15:38:42 +0000834 // FIXME: When we implement the C++0x late-specified return type,
835 // we will need to move this scope out to the function type itself.
836 bool IsTemporaryScope = (SemaRef.CurrentInstantiationScope != 0);
837 Sema::LocalInstantiationScope Scope(SemaRef, IsTemporaryScope,
838 IsTemporaryScope);
John McCall21ef0fa2010-03-11 09:03:00 +0000839
840 if (TreeTransform<TemplateInstantiator>::
841 TransformFunctionTypeParams(TL, PTypes, PVars))
842 return true;
843
John McCall21ef0fa2010-03-11 09:03:00 +0000844 return false;
845}
846
847ParmVarDecl *
848TemplateInstantiator::TransformFunctionTypeParam(ParmVarDecl *OldParm) {
Douglas Gregorcb27b0f2010-04-12 07:48:19 +0000849 return SemaRef.SubstParmVarDecl(OldParm, TemplateArgs);
John McCall21ef0fa2010-03-11 09:03:00 +0000850}
851
Mike Stump1eb44332009-09-09 15:08:12 +0000852QualType
John McCalla2becad2009-10-21 00:40:46 +0000853TemplateInstantiator::TransformTemplateTypeParmType(TypeLocBuilder &TLB,
Douglas Gregor124b8782010-02-16 19:09:40 +0000854 TemplateTypeParmTypeLoc TL,
855 QualType ObjectType) {
John McCalla2becad2009-10-21 00:40:46 +0000856 TemplateTypeParmType *T = TL.getTypePtr();
Douglas Gregord6350ae2009-08-28 20:31:08 +0000857 if (T->getDepth() < TemplateArgs.getNumLevels()) {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000858 // Replace the template type parameter with its corresponding
859 // template argument.
Mike Stump1eb44332009-09-09 15:08:12 +0000860
861 // If the corresponding template argument is NULL or doesn't exist, it's
862 // because we are performing instantiation from explicitly-specified
863 // template arguments in a function template class, but there were some
Douglas Gregor16134c62009-07-01 00:28:38 +0000864 // arguments left unspecified.
John McCalla2becad2009-10-21 00:40:46 +0000865 if (!TemplateArgs.hasTemplateArgument(T->getDepth(), T->getIndex())) {
866 TemplateTypeParmTypeLoc NewTL
867 = TLB.push<TemplateTypeParmTypeLoc>(TL.getType());
868 NewTL.setNameLoc(TL.getNameLoc());
869 return TL.getType();
870 }
Mike Stump1eb44332009-09-09 15:08:12 +0000871
872 assert(TemplateArgs(T->getDepth(), T->getIndex()).getKind()
Douglas Gregord6350ae2009-08-28 20:31:08 +0000873 == TemplateArgument::Type &&
Douglas Gregor99ebf652009-02-27 19:31:52 +0000874 "Template argument kind mismatch");
Douglas Gregord6350ae2009-08-28 20:31:08 +0000875
John McCall49a832b2009-10-18 09:09:24 +0000876 QualType Replacement
877 = TemplateArgs(T->getDepth(), T->getIndex()).getAsType();
878
879 // TODO: only do this uniquing once, at the start of instantiation.
John McCalla2becad2009-10-21 00:40:46 +0000880 QualType Result
881 = getSema().Context.getSubstTemplateTypeParmType(T, Replacement);
882 SubstTemplateTypeParmTypeLoc NewTL
883 = TLB.push<SubstTemplateTypeParmTypeLoc>(Result);
884 NewTL.setNameLoc(TL.getNameLoc());
885 return Result;
Mike Stump1eb44332009-09-09 15:08:12 +0000886 }
Douglas Gregor99ebf652009-02-27 19:31:52 +0000887
888 // The template type parameter comes from an inner template (e.g.,
889 // the template parameter list of a member template inside the
890 // template we are instantiating). Create a new template type
891 // parameter with the template "level" reduced by one.
John McCalla2becad2009-10-21 00:40:46 +0000892 QualType Result
893 = getSema().Context.getTemplateTypeParmType(T->getDepth()
894 - TemplateArgs.getNumLevels(),
895 T->getIndex(),
896 T->isParameterPack(),
897 T->getName());
898 TemplateTypeParmTypeLoc NewTL = TLB.push<TemplateTypeParmTypeLoc>(Result);
899 NewTL.setNameLoc(TL.getNameLoc());
900 return Result;
Douglas Gregorcd281c32009-02-28 00:25:32 +0000901}
Douglas Gregor99ebf652009-02-27 19:31:52 +0000902
John McCallce3ff2b2009-08-25 22:02:44 +0000903/// \brief Perform substitution on the type T with a given set of template
904/// arguments.
Douglas Gregor99ebf652009-02-27 19:31:52 +0000905///
906/// This routine substitutes the given template arguments into the
907/// type T and produces the instantiated type.
908///
909/// \param T the type into which the template arguments will be
910/// substituted. If this type is not dependent, it will be returned
911/// immediately.
912///
913/// \param TemplateArgs the template arguments that will be
914/// substituted for the top-level template parameters within T.
915///
Douglas Gregor99ebf652009-02-27 19:31:52 +0000916/// \param Loc the location in the source code where this substitution
917/// is being performed. It will typically be the location of the
918/// declarator (if we're instantiating the type of some declaration)
919/// or the location of the type in the source code (if, e.g., we're
920/// instantiating the type of a cast expression).
921///
922/// \param Entity the name of the entity associated with a declaration
923/// being instantiated (if any). May be empty to indicate that there
924/// is no such entity (if, e.g., this is a type that occurs as part of
925/// a cast expression) or that the entity has no name (e.g., an
926/// unnamed function parameter).
927///
928/// \returns If the instantiation succeeds, the instantiated
929/// type. Otherwise, produces diagnostics and returns a NULL type.
John McCalla93c9342009-12-07 02:54:59 +0000930TypeSourceInfo *Sema::SubstType(TypeSourceInfo *T,
John McCallcd7ba1c2009-10-21 00:58:09 +0000931 const MultiLevelTemplateArgumentList &Args,
932 SourceLocation Loc,
933 DeclarationName Entity) {
934 assert(!ActiveTemplateInstantiations.empty() &&
935 "Cannot perform an instantiation without some context on the "
936 "instantiation stack");
937
938 if (!T->getType()->isDependentType())
939 return T;
940
941 TemplateInstantiator Instantiator(*this, Args, Loc, Entity);
942 return Instantiator.TransformType(T);
943}
944
945/// Deprecated form of the above.
Mike Stump1eb44332009-09-09 15:08:12 +0000946QualType Sema::SubstType(QualType T,
Douglas Gregord6350ae2009-08-28 20:31:08 +0000947 const MultiLevelTemplateArgumentList &TemplateArgs,
John McCallce3ff2b2009-08-25 22:02:44 +0000948 SourceLocation Loc, DeclarationName Entity) {
Douglas Gregordf667e72009-03-10 20:44:00 +0000949 assert(!ActiveTemplateInstantiations.empty() &&
950 "Cannot perform an instantiation without some context on the "
951 "instantiation stack");
952
Douglas Gregor99ebf652009-02-27 19:31:52 +0000953 // If T is not a dependent type, there is nothing to do.
954 if (!T->isDependentType())
955 return T;
956
Douglas Gregor577f75a2009-08-04 16:50:30 +0000957 TemplateInstantiator Instantiator(*this, TemplateArgs, Loc, Entity);
958 return Instantiator.TransformType(T);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000959}
Douglas Gregor2943aed2009-03-03 04:44:36 +0000960
John McCall6cd3b9f2010-04-09 17:38:44 +0000961static bool NeedsInstantiationAsFunctionType(TypeSourceInfo *T) {
962 if (T->getType()->isDependentType())
963 return true;
964
965 TypeLoc TL = T->getTypeLoc();
966 if (!isa<FunctionProtoTypeLoc>(TL))
967 return false;
968
969 FunctionProtoTypeLoc FP = cast<FunctionProtoTypeLoc>(TL);
970 for (unsigned I = 0, E = FP.getNumArgs(); I != E; ++I) {
971 ParmVarDecl *P = FP.getArg(I);
972
973 // TODO: currently we always rebuild expressions. When we
974 // properly get lazier about this, we should use the same
975 // logic to avoid rebuilding prototypes here.
976 if (P->hasInit())
977 return true;
978 }
979
980 return false;
981}
982
983/// A form of SubstType intended specifically for instantiating the
984/// type of a FunctionDecl. Its purpose is solely to force the
985/// instantiation of default-argument expressions.
986TypeSourceInfo *Sema::SubstFunctionDeclType(TypeSourceInfo *T,
987 const MultiLevelTemplateArgumentList &Args,
988 SourceLocation Loc,
989 DeclarationName Entity) {
990 assert(!ActiveTemplateInstantiations.empty() &&
991 "Cannot perform an instantiation without some context on the "
992 "instantiation stack");
993
994 if (!NeedsInstantiationAsFunctionType(T))
995 return T;
996
997 TemplateInstantiator Instantiator(*this, Args, Loc, Entity);
998
999 TypeLocBuilder TLB;
1000
1001 TypeLoc TL = T->getTypeLoc();
1002 TLB.reserve(TL.getFullDataSize());
1003
1004 QualType Result = Instantiator.TransformType(TLB, TL, QualType());
1005 if (Result.isNull())
1006 return 0;
1007
1008 return TLB.getTypeSourceInfo(Context, Result);
1009}
1010
Douglas Gregorcb27b0f2010-04-12 07:48:19 +00001011ParmVarDecl *Sema::SubstParmVarDecl(ParmVarDecl *OldParm,
1012 const MultiLevelTemplateArgumentList &TemplateArgs) {
1013 TypeSourceInfo *OldDI = OldParm->getTypeSourceInfo();
1014 TypeSourceInfo *NewDI = SubstType(OldDI, TemplateArgs, OldParm->getLocation(),
1015 OldParm->getDeclName());
1016 if (!NewDI)
1017 return 0;
1018
1019 if (NewDI->getType()->isVoidType()) {
1020 Diag(OldParm->getLocation(), diag::err_param_with_void_type);
1021 return 0;
1022 }
1023
1024 ParmVarDecl *NewParm = CheckParameter(Context.getTranslationUnitDecl(),
1025 NewDI, NewDI->getType(),
1026 OldParm->getIdentifier(),
1027 OldParm->getLocation(),
Douglas Gregor16573fa2010-04-19 22:54:31 +00001028 OldParm->getStorageClass(),
1029 OldParm->getStorageClassAsWritten());
Douglas Gregorcb27b0f2010-04-12 07:48:19 +00001030 if (!NewParm)
1031 return 0;
1032
1033 // Mark the (new) default argument as uninstantiated (if any).
1034 if (OldParm->hasUninstantiatedDefaultArg()) {
1035 Expr *Arg = OldParm->getUninstantiatedDefaultArg();
1036 NewParm->setUninstantiatedDefaultArg(Arg);
1037 } else if (Expr *Arg = OldParm->getDefaultArg())
1038 NewParm->setUninstantiatedDefaultArg(Arg);
1039
1040 NewParm->setHasInheritedDefaultArg(OldParm->hasInheritedDefaultArg());
1041
1042 CurrentInstantiationScope->InstantiatedLocal(OldParm, NewParm);
1043 return NewParm;
1044}
1045
John McCallce3ff2b2009-08-25 22:02:44 +00001046/// \brief Perform substitution on the base class specifiers of the
1047/// given class template specialization.
Douglas Gregor2943aed2009-03-03 04:44:36 +00001048///
1049/// Produces a diagnostic and returns true on error, returns false and
1050/// attaches the instantiated base classes to the class template
1051/// specialization if successful.
Mike Stump1eb44332009-09-09 15:08:12 +00001052bool
John McCallce3ff2b2009-08-25 22:02:44 +00001053Sema::SubstBaseSpecifiers(CXXRecordDecl *Instantiation,
1054 CXXRecordDecl *Pattern,
Douglas Gregord6350ae2009-08-28 20:31:08 +00001055 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor2943aed2009-03-03 04:44:36 +00001056 bool Invalid = false;
Douglas Gregor0ca20ac2009-05-29 18:27:38 +00001057 llvm::SmallVector<CXXBaseSpecifier*, 4> InstantiatedBases;
Mike Stump1eb44332009-09-09 15:08:12 +00001058 for (ClassTemplateSpecializationDecl::base_class_iterator
Douglas Gregord475b8d2009-03-25 21:17:03 +00001059 Base = Pattern->bases_begin(), BaseEnd = Pattern->bases_end();
Douglas Gregor27b152f2009-03-10 18:52:44 +00001060 Base != BaseEnd; ++Base) {
Douglas Gregor2943aed2009-03-03 04:44:36 +00001061 if (!Base->getType()->isDependentType()) {
Anders Carlsson51f94042009-12-03 17:49:57 +00001062 const CXXRecordDecl *BaseDecl =
1063 cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
1064
1065 // Make sure to set the attributes from the base.
1066 SetClassDeclAttributesFromBase(Instantiation, BaseDecl,
1067 Base->isVirtual());
1068
Fariborz Jahanian71c6e712009-07-22 17:41:53 +00001069 InstantiatedBases.push_back(new (Context) CXXBaseSpecifier(*Base));
Douglas Gregor2943aed2009-03-03 04:44:36 +00001070 continue;
1071 }
1072
Mike Stump1eb44332009-09-09 15:08:12 +00001073 QualType BaseType = SubstType(Base->getType(),
1074 TemplateArgs,
John McCallce3ff2b2009-08-25 22:02:44 +00001075 Base->getSourceRange().getBegin(),
1076 DeclarationName());
Douglas Gregor2943aed2009-03-03 04:44:36 +00001077 if (BaseType.isNull()) {
1078 Invalid = true;
1079 continue;
1080 }
1081
1082 if (CXXBaseSpecifier *InstantiatedBase
Douglas Gregord475b8d2009-03-25 21:17:03 +00001083 = CheckBaseSpecifier(Instantiation,
Douglas Gregor2943aed2009-03-03 04:44:36 +00001084 Base->getSourceRange(),
1085 Base->isVirtual(),
1086 Base->getAccessSpecifierAsWritten(),
1087 BaseType,
1088 /*FIXME: Not totally accurate */
1089 Base->getSourceRange().getBegin()))
1090 InstantiatedBases.push_back(InstantiatedBase);
1091 else
1092 Invalid = true;
1093 }
1094
Douglas Gregor27b152f2009-03-10 18:52:44 +00001095 if (!Invalid &&
Jay Foadbeaaccd2009-05-21 09:52:38 +00001096 AttachBaseSpecifiers(Instantiation, InstantiatedBases.data(),
Douglas Gregor2943aed2009-03-03 04:44:36 +00001097 InstantiatedBases.size()))
1098 Invalid = true;
1099
1100 return Invalid;
1101}
1102
Douglas Gregord475b8d2009-03-25 21:17:03 +00001103/// \brief Instantiate the definition of a class from a given pattern.
1104///
1105/// \param PointOfInstantiation The point of instantiation within the
1106/// source code.
1107///
1108/// \param Instantiation is the declaration whose definition is being
1109/// instantiated. This will be either a class template specialization
1110/// or a member class of a class template specialization.
1111///
1112/// \param Pattern is the pattern from which the instantiation
1113/// occurs. This will be either the declaration of a class template or
1114/// the declaration of a member class of a class template.
1115///
1116/// \param TemplateArgs The template arguments to be substituted into
1117/// the pattern.
1118///
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001119/// \param TSK the kind of implicit or explicit instantiation to perform.
Douglas Gregor5842ba92009-08-24 15:23:48 +00001120///
1121/// \param Complain whether to complain if the class cannot be instantiated due
1122/// to the lack of a definition.
1123///
Douglas Gregord475b8d2009-03-25 21:17:03 +00001124/// \returns true if an error occurred, false otherwise.
1125bool
1126Sema::InstantiateClass(SourceLocation PointOfInstantiation,
1127 CXXRecordDecl *Instantiation, CXXRecordDecl *Pattern,
Douglas Gregord6350ae2009-08-28 20:31:08 +00001128 const MultiLevelTemplateArgumentList &TemplateArgs,
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001129 TemplateSpecializationKind TSK,
Douglas Gregor5842ba92009-08-24 15:23:48 +00001130 bool Complain) {
Douglas Gregord475b8d2009-03-25 21:17:03 +00001131 bool Invalid = false;
John McCalle29ba202009-08-20 01:44:21 +00001132
Mike Stump1eb44332009-09-09 15:08:12 +00001133 CXXRecordDecl *PatternDef
Douglas Gregor952b0172010-02-11 01:04:33 +00001134 = cast_or_null<CXXRecordDecl>(Pattern->getDefinition());
Douglas Gregord475b8d2009-03-25 21:17:03 +00001135 if (!PatternDef) {
Douglas Gregor5842ba92009-08-24 15:23:48 +00001136 if (!Complain) {
1137 // Say nothing
1138 } else if (Pattern == Instantiation->getInstantiatedFromMemberClass()) {
Douglas Gregord475b8d2009-03-25 21:17:03 +00001139 Diag(PointOfInstantiation,
1140 diag::err_implicit_instantiate_member_undefined)
1141 << Context.getTypeDeclType(Instantiation);
1142 Diag(Pattern->getLocation(), diag::note_member_of_template_here);
1143 } else {
Douglas Gregor93dfdb12009-05-13 00:25:59 +00001144 Diag(PointOfInstantiation, diag::err_template_instantiate_undefined)
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001145 << (TSK != TSK_ImplicitInstantiation)
Douglas Gregord475b8d2009-03-25 21:17:03 +00001146 << Context.getTypeDeclType(Instantiation);
1147 Diag(Pattern->getLocation(), diag::note_template_decl_here);
1148 }
1149 return true;
1150 }
1151 Pattern = PatternDef;
1152
Douglas Gregor454885e2009-10-15 15:54:05 +00001153 // \brief Record the point of instantiation.
1154 if (MemberSpecializationInfo *MSInfo
1155 = Instantiation->getMemberSpecializationInfo()) {
1156 MSInfo->setTemplateSpecializationKind(TSK);
1157 MSInfo->setPointOfInstantiation(PointOfInstantiation);
Douglas Gregor972e6ce2009-10-27 06:26:26 +00001158 } else if (ClassTemplateSpecializationDecl *Spec
1159 = dyn_cast<ClassTemplateSpecializationDecl>(Instantiation)) {
1160 Spec->setTemplateSpecializationKind(TSK);
1161 Spec->setPointOfInstantiation(PointOfInstantiation);
Douglas Gregor454885e2009-10-15 15:54:05 +00001162 }
1163
Douglas Gregord048bb72009-03-25 21:23:52 +00001164 InstantiatingTemplate Inst(*this, PointOfInstantiation, Instantiation);
Douglas Gregord475b8d2009-03-25 21:17:03 +00001165 if (Inst)
1166 return true;
1167
1168 // Enter the scope of this instantiation. We don't use
1169 // PushDeclContext because we don't have a scope.
John McCallf5813822010-04-29 00:35:03 +00001170 ContextRAII SavedContext(*this, Instantiation);
Douglas Gregord475b8d2009-03-25 21:17:03 +00001171
Douglas Gregor05030bb2010-03-24 01:33:17 +00001172 // If this is an instantiation of a local class, merge this local
1173 // instantiation scope with the enclosing scope. Otherwise, every
1174 // instantiation of a class has its own local instantiation scope.
1175 bool MergeWithParentScope = !Instantiation->isDefinedOutsideFunctionOrMethod();
1176 Sema::LocalInstantiationScope Scope(*this, MergeWithParentScope);
1177
Douglas Gregord475b8d2009-03-25 21:17:03 +00001178 // Start the definition of this instantiation.
1179 Instantiation->startDefinition();
1180
John McCallce3ff2b2009-08-25 22:02:44 +00001181 // Do substitution on the base class specifiers.
1182 if (SubstBaseSpecifiers(Instantiation, Pattern, TemplateArgs))
Douglas Gregord475b8d2009-03-25 21:17:03 +00001183 Invalid = true;
1184
Douglas Gregor0ca20ac2009-05-29 18:27:38 +00001185 llvm::SmallVector<DeclPtrTy, 4> Fields;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001186 for (RecordDecl::decl_iterator Member = Pattern->decls_begin(),
Mike Stump1eb44332009-09-09 15:08:12 +00001187 MemberEnd = Pattern->decls_end();
Douglas Gregor6ab35242009-04-09 21:40:53 +00001188 Member != MemberEnd; ++Member) {
John McCallce3ff2b2009-08-25 22:02:44 +00001189 Decl *NewMember = SubstDecl(*Member, Instantiation, TemplateArgs);
Douglas Gregord475b8d2009-03-25 21:17:03 +00001190 if (NewMember) {
Eli Friedman721e77d2009-12-07 00:22:08 +00001191 if (FieldDecl *Field = dyn_cast<FieldDecl>(NewMember))
Chris Lattnerb28317a2009-03-28 19:18:32 +00001192 Fields.push_back(DeclPtrTy::make(Field));
Eli Friedman721e77d2009-12-07 00:22:08 +00001193 else if (NewMember->isInvalidDecl())
1194 Invalid = true;
Douglas Gregord475b8d2009-03-25 21:17:03 +00001195 } else {
1196 // FIXME: Eventually, a NULL return will mean that one of the
Mike Stump390b4cc2009-05-16 07:39:55 +00001197 // instantiations was a semantic disaster, and we'll want to set Invalid =
1198 // true. For now, we expect to skip some members that we can't yet handle.
Douglas Gregord475b8d2009-03-25 21:17:03 +00001199 }
1200 }
1201
1202 // Finish checking fields.
Chris Lattnerb28317a2009-03-28 19:18:32 +00001203 ActOnFields(0, Instantiation->getLocation(), DeclPtrTy::make(Instantiation),
Jay Foadbeaaccd2009-05-21 09:52:38 +00001204 Fields.data(), Fields.size(), SourceLocation(), SourceLocation(),
Douglas Gregord475b8d2009-03-25 21:17:03 +00001205 0);
Douglas Gregor6275e0c2010-04-12 17:09:20 +00001206 CheckCompletedCXXClass(/*Scope=*/0, Instantiation);
Douglas Gregor663b5a02009-10-14 20:14:33 +00001207 if (Instantiation->isInvalidDecl())
1208 Invalid = true;
1209
Douglas Gregord475b8d2009-03-25 21:17:03 +00001210 // Exit the scope of this instantiation.
John McCallf5813822010-04-29 00:35:03 +00001211 SavedContext.pop();
Douglas Gregord475b8d2009-03-25 21:17:03 +00001212
Douglas Gregor159ef1e2010-01-06 04:44:19 +00001213 // If this is a polymorphic C++ class without a key function, we'll
1214 // have to mark all of the virtual members to allow emission of a vtable
1215 // in this translation unit.
Chandler Carruth17e0f402010-02-15 22:12:26 +00001216 if (Instantiation->isDynamicClass() &&
1217 !Context.getKeyFunction(Instantiation)) {
1218 // Local classes need to have their methods instantiated immediately in
1219 // order to have the correct instantiation scope.
1220 if (Instantiation->isLocalClass()) {
1221 MarkVirtualMembersReferenced(PointOfInstantiation,
1222 Instantiation);
1223 } else {
Douglas Gregor159ef1e2010-01-06 04:44:19 +00001224 ClassesWithUnmarkedVirtualMembers.push_back(std::make_pair(Instantiation,
1225 PointOfInstantiation));
Chandler Carruth17e0f402010-02-15 22:12:26 +00001226 }
1227 }
Douglas Gregor159ef1e2010-01-06 04:44:19 +00001228
Douglas Gregoraba43bb2009-05-26 20:50:29 +00001229 if (!Invalid)
1230 Consumer.HandleTagDeclDefinition(Instantiation);
1231
Douglas Gregord475b8d2009-03-25 21:17:03 +00001232 return Invalid;
1233}
1234
Mike Stump1eb44332009-09-09 15:08:12 +00001235bool
Douglas Gregor2943aed2009-03-03 04:44:36 +00001236Sema::InstantiateClassTemplateSpecialization(
Douglas Gregor972e6ce2009-10-27 06:26:26 +00001237 SourceLocation PointOfInstantiation,
Douglas Gregor2943aed2009-03-03 04:44:36 +00001238 ClassTemplateSpecializationDecl *ClassTemplateSpec,
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001239 TemplateSpecializationKind TSK,
Douglas Gregor5842ba92009-08-24 15:23:48 +00001240 bool Complain) {
Douglas Gregor2943aed2009-03-03 04:44:36 +00001241 // Perform the actual instantiation on the canonical declaration.
1242 ClassTemplateSpec = cast<ClassTemplateSpecializationDecl>(
Argyrios Kyrtzidis97fbaa22009-07-18 00:34:25 +00001243 ClassTemplateSpec->getCanonicalDecl());
Douglas Gregor2943aed2009-03-03 04:44:36 +00001244
Douglas Gregor52604ab2009-09-11 21:19:12 +00001245 // Check whether we have already instantiated or specialized this class
1246 // template specialization.
1247 if (ClassTemplateSpec->getSpecializationKind() != TSK_Undeclared) {
1248 if (ClassTemplateSpec->getSpecializationKind() ==
1249 TSK_ExplicitInstantiationDeclaration &&
1250 TSK == TSK_ExplicitInstantiationDefinition) {
1251 // An explicit instantiation definition follows an explicit instantiation
1252 // declaration (C++0x [temp.explicit]p10); go ahead and perform the
1253 // explicit instantiation.
1254 ClassTemplateSpec->setSpecializationKind(TSK);
Douglas Gregor52604ab2009-09-11 21:19:12 +00001255 return false;
1256 }
1257
1258 // We can only instantiate something that hasn't already been
1259 // instantiated or specialized. Fail without any diagnostics: our
1260 // caller will provide an error message.
Douglas Gregor2943aed2009-03-03 04:44:36 +00001261 return true;
Douglas Gregor52604ab2009-09-11 21:19:12 +00001262 }
Douglas Gregor2943aed2009-03-03 04:44:36 +00001263
Douglas Gregor9eea08b2009-09-15 16:51:42 +00001264 if (ClassTemplateSpec->isInvalidDecl())
1265 return true;
1266
Douglas Gregor2943aed2009-03-03 04:44:36 +00001267 ClassTemplateDecl *Template = ClassTemplateSpec->getSpecializedTemplate();
Douglas Gregord6350ae2009-08-28 20:31:08 +00001268 CXXRecordDecl *Pattern = 0;
Douglas Gregorc8ab2562009-05-31 09:31:02 +00001269
Douglas Gregorc1efb3f2009-06-12 22:31:52 +00001270 // C++ [temp.class.spec.match]p1:
1271 // When a class template is used in a context that requires an
1272 // instantiation of the class, it is necessary to determine
1273 // whether the instantiation is to be generated using the primary
1274 // template or one of the partial specializations. This is done by
1275 // matching the template arguments of the class template
1276 // specialization with the template argument lists of the partial
1277 // specializations.
Douglas Gregor199d9912009-06-05 00:53:49 +00001278 typedef std::pair<ClassTemplatePartialSpecializationDecl *,
1279 TemplateArgumentList *> MatchResult;
1280 llvm::SmallVector<MatchResult, 4> Matched;
Mike Stump1eb44332009-09-09 15:08:12 +00001281 for (llvm::FoldingSet<ClassTemplatePartialSpecializationDecl>::iterator
Douglas Gregorc8ab2562009-05-31 09:31:02 +00001282 Partial = Template->getPartialSpecializations().begin(),
1283 PartialEnd = Template->getPartialSpecializations().end();
1284 Partial != PartialEnd;
1285 ++Partial) {
John McCall5769d612010-02-08 23:07:23 +00001286 TemplateDeductionInfo Info(Context, PointOfInstantiation);
Douglas Gregorf67875d2009-06-12 18:26:56 +00001287 if (TemplateDeductionResult Result
Mike Stump1eb44332009-09-09 15:08:12 +00001288 = DeduceTemplateArguments(&*Partial,
Douglas Gregorf67875d2009-06-12 18:26:56 +00001289 ClassTemplateSpec->getTemplateArgs(),
1290 Info)) {
1291 // FIXME: Store the failed-deduction information for use in
1292 // diagnostics, later.
1293 (void)Result;
1294 } else {
1295 Matched.push_back(std::make_pair(&*Partial, Info.take()));
1296 }
Douglas Gregorc8ab2562009-05-31 09:31:02 +00001297 }
1298
Douglas Gregored9c0f92009-10-29 00:04:11 +00001299 if (Matched.size() >= 1) {
Douglas Gregorbf4ea562009-09-15 16:23:51 +00001300 llvm::SmallVector<MatchResult, 4>::iterator Best = Matched.begin();
Douglas Gregored9c0f92009-10-29 00:04:11 +00001301 if (Matched.size() == 1) {
1302 // -- If exactly one matching specialization is found, the
1303 // instantiation is generated from that specialization.
1304 // We don't need to do anything for this.
1305 } else {
1306 // -- If more than one matching specialization is found, the
1307 // partial order rules (14.5.4.2) are used to determine
1308 // whether one of the specializations is more specialized
1309 // than the others. If none of the specializations is more
1310 // specialized than all of the other matching
1311 // specializations, then the use of the class template is
1312 // ambiguous and the program is ill-formed.
1313 for (llvm::SmallVector<MatchResult, 4>::iterator P = Best + 1,
1314 PEnd = Matched.end();
1315 P != PEnd; ++P) {
John McCall5769d612010-02-08 23:07:23 +00001316 if (getMoreSpecializedPartialSpecialization(P->first, Best->first,
1317 PointOfInstantiation)
Douglas Gregored9c0f92009-10-29 00:04:11 +00001318 == P->first)
1319 Best = P;
Douglas Gregorbf4ea562009-09-15 16:23:51 +00001320 }
Douglas Gregorbf4ea562009-09-15 16:23:51 +00001321
Douglas Gregored9c0f92009-10-29 00:04:11 +00001322 // Determine if the best partial specialization is more specialized than
1323 // the others.
1324 bool Ambiguous = false;
Douglas Gregorbf4ea562009-09-15 16:23:51 +00001325 for (llvm::SmallVector<MatchResult, 4>::iterator P = Matched.begin(),
1326 PEnd = Matched.end();
Douglas Gregored9c0f92009-10-29 00:04:11 +00001327 P != PEnd; ++P) {
1328 if (P != Best &&
John McCall5769d612010-02-08 23:07:23 +00001329 getMoreSpecializedPartialSpecialization(P->first, Best->first,
1330 PointOfInstantiation)
Douglas Gregored9c0f92009-10-29 00:04:11 +00001331 != Best->first) {
1332 Ambiguous = true;
1333 break;
1334 }
1335 }
1336
1337 if (Ambiguous) {
1338 // Partial ordering did not produce a clear winner. Complain.
1339 ClassTemplateSpec->setInvalidDecl();
1340 Diag(PointOfInstantiation, diag::err_partial_spec_ordering_ambiguous)
1341 << ClassTemplateSpec;
1342
1343 // Print the matching partial specializations.
1344 for (llvm::SmallVector<MatchResult, 4>::iterator P = Matched.begin(),
1345 PEnd = Matched.end();
1346 P != PEnd; ++P)
1347 Diag(P->first->getLocation(), diag::note_partial_spec_match)
1348 << getTemplateArgumentBindingsText(P->first->getTemplateParameters(),
1349 *P->second);
Douglas Gregord6350ae2009-08-28 20:31:08 +00001350
Douglas Gregored9c0f92009-10-29 00:04:11 +00001351 return true;
1352 }
Douglas Gregorbf4ea562009-09-15 16:23:51 +00001353 }
1354
1355 // Instantiate using the best class template partial specialization.
Douglas Gregored9c0f92009-10-29 00:04:11 +00001356 ClassTemplatePartialSpecializationDecl *OrigPartialSpec = Best->first;
1357 while (OrigPartialSpec->getInstantiatedFromMember()) {
1358 // If we've found an explicit specialization of this class template,
1359 // stop here and use that as the pattern.
1360 if (OrigPartialSpec->isMemberSpecialization())
1361 break;
1362
1363 OrigPartialSpec = OrigPartialSpec->getInstantiatedFromMember();
1364 }
1365
1366 Pattern = OrigPartialSpec;
Douglas Gregorbf4ea562009-09-15 16:23:51 +00001367 ClassTemplateSpec->setInstantiationOf(Best->first, Best->second);
Douglas Gregorc1efb3f2009-06-12 22:31:52 +00001368 } else {
1369 // -- If no matches are found, the instantiation is generated
1370 // from the primary template.
Douglas Gregord6350ae2009-08-28 20:31:08 +00001371 ClassTemplateDecl *OrigTemplate = Template;
Douglas Gregorfd056bc2009-10-13 16:30:37 +00001372 while (OrigTemplate->getInstantiatedFromMemberTemplate()) {
1373 // If we've found an explicit specialization of this class template,
1374 // stop here and use that as the pattern.
1375 if (OrigTemplate->isMemberSpecialization())
1376 break;
1377
Douglas Gregord6350ae2009-08-28 20:31:08 +00001378 OrigTemplate = OrigTemplate->getInstantiatedFromMemberTemplate();
Douglas Gregorfd056bc2009-10-13 16:30:37 +00001379 }
1380
Douglas Gregord6350ae2009-08-28 20:31:08 +00001381 Pattern = OrigTemplate->getTemplatedDecl();
Douglas Gregorc8ab2562009-05-31 09:31:02 +00001382 }
Douglas Gregor2943aed2009-03-03 04:44:36 +00001383
Douglas Gregor972e6ce2009-10-27 06:26:26 +00001384 bool Result = InstantiateClass(PointOfInstantiation, ClassTemplateSpec,
1385 Pattern,
1386 getTemplateInstantiationArgs(ClassTemplateSpec),
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001387 TSK,
Douglas Gregor5842ba92009-08-24 15:23:48 +00001388 Complain);
Mike Stump1eb44332009-09-09 15:08:12 +00001389
Douglas Gregor199d9912009-06-05 00:53:49 +00001390 for (unsigned I = 0, N = Matched.size(); I != N; ++I) {
1391 // FIXME: Implement TemplateArgumentList::Destroy!
1392 // if (Matched[I].first != Pattern)
1393 // Matched[I].second->Destroy(Context);
1394 }
Mike Stump1eb44332009-09-09 15:08:12 +00001395
Douglas Gregor199d9912009-06-05 00:53:49 +00001396 return Result;
Douglas Gregor2943aed2009-03-03 04:44:36 +00001397}
Douglas Gregor5953d8b2009-03-19 17:26:29 +00001398
John McCallce3ff2b2009-08-25 22:02:44 +00001399/// \brief Instantiates the definitions of all of the member
1400/// of the given class, which is an instantiation of a class template
1401/// or a member class of a template.
Douglas Gregora58861f2009-05-13 20:28:22 +00001402void
1403Sema::InstantiateClassMembers(SourceLocation PointOfInstantiation,
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001404 CXXRecordDecl *Instantiation,
1405 const MultiLevelTemplateArgumentList &TemplateArgs,
1406 TemplateSpecializationKind TSK) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001407 for (DeclContext::decl_iterator D = Instantiation->decls_begin(),
1408 DEnd = Instantiation->decls_end();
Douglas Gregora58861f2009-05-13 20:28:22 +00001409 D != DEnd; ++D) {
Douglas Gregor0d035142009-10-27 18:42:08 +00001410 bool SuppressNew = false;
Douglas Gregora58861f2009-05-13 20:28:22 +00001411 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(*D)) {
Douglas Gregor0d035142009-10-27 18:42:08 +00001412 if (FunctionDecl *Pattern
1413 = Function->getInstantiatedFromMemberFunction()) {
1414 MemberSpecializationInfo *MSInfo
1415 = Function->getMemberSpecializationInfo();
1416 assert(MSInfo && "No member specialization information?");
Douglas Gregorc42b6522010-04-09 21:02:29 +00001417 if (MSInfo->getTemplateSpecializationKind()
1418 == TSK_ExplicitSpecialization)
1419 continue;
1420
Douglas Gregor0d035142009-10-27 18:42:08 +00001421 if (CheckSpecializationInstantiationRedecl(PointOfInstantiation, TSK,
1422 Function,
1423 MSInfo->getTemplateSpecializationKind(),
1424 MSInfo->getPointOfInstantiation(),
1425 SuppressNew) ||
1426 SuppressNew)
Douglas Gregorf6b11852009-10-08 15:14:33 +00001427 continue;
1428
Douglas Gregor0d035142009-10-27 18:42:08 +00001429 if (Function->getBody())
1430 continue;
1431
1432 if (TSK == TSK_ExplicitInstantiationDefinition) {
1433 // C++0x [temp.explicit]p8:
1434 // An explicit instantiation definition that names a class template
1435 // specialization explicitly instantiates the class template
1436 // specialization and is only an explicit instantiation definition
1437 // of members whose definition is visible at the point of
1438 // instantiation.
1439 if (!Pattern->getBody())
1440 continue;
1441
1442 Function->setTemplateSpecializationKind(TSK, PointOfInstantiation);
1443
1444 InstantiateFunctionDefinition(PointOfInstantiation, Function);
1445 } else {
1446 Function->setTemplateSpecializationKind(TSK, PointOfInstantiation);
1447 }
Douglas Gregorf6b11852009-10-08 15:14:33 +00001448 }
Douglas Gregora58861f2009-05-13 20:28:22 +00001449 } else if (VarDecl *Var = dyn_cast<VarDecl>(*D)) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001450 if (Var->isStaticDataMember()) {
Douglas Gregor0d035142009-10-27 18:42:08 +00001451 MemberSpecializationInfo *MSInfo = Var->getMemberSpecializationInfo();
1452 assert(MSInfo && "No member specialization information?");
Douglas Gregorc42b6522010-04-09 21:02:29 +00001453 if (MSInfo->getTemplateSpecializationKind()
1454 == TSK_ExplicitSpecialization)
1455 continue;
1456
Douglas Gregor0d035142009-10-27 18:42:08 +00001457 if (CheckSpecializationInstantiationRedecl(PointOfInstantiation, TSK,
1458 Var,
1459 MSInfo->getTemplateSpecializationKind(),
1460 MSInfo->getPointOfInstantiation(),
1461 SuppressNew) ||
1462 SuppressNew)
Douglas Gregorf6b11852009-10-08 15:14:33 +00001463 continue;
1464
Douglas Gregor0d035142009-10-27 18:42:08 +00001465 if (TSK == TSK_ExplicitInstantiationDefinition) {
1466 // C++0x [temp.explicit]p8:
1467 // An explicit instantiation definition that names a class template
1468 // specialization explicitly instantiates the class template
1469 // specialization and is only an explicit instantiation definition
1470 // of members whose definition is visible at the point of
1471 // instantiation.
1472 if (!Var->getInstantiatedFromStaticDataMember()
1473 ->getOutOfLineDefinition())
1474 continue;
1475
1476 Var->setTemplateSpecializationKind(TSK, PointOfInstantiation);
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001477 InstantiateStaticDataMemberDefinition(PointOfInstantiation, Var);
Douglas Gregor0d035142009-10-27 18:42:08 +00001478 } else {
1479 Var->setTemplateSpecializationKind(TSK, PointOfInstantiation);
1480 }
1481 }
Douglas Gregora58861f2009-05-13 20:28:22 +00001482 } else if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(*D)) {
Douglas Gregora77eaa92010-04-18 18:11:38 +00001483 // Always skip the injected-class-name, along with any
1484 // redeclarations of nested classes, since both would cause us
1485 // to try to instantiate the members of a class twice.
1486 if (Record->isInjectedClassName() || Record->getPreviousDeclaration())
Douglas Gregor2db32322009-10-07 23:56:10 +00001487 continue;
1488
Douglas Gregor0d035142009-10-27 18:42:08 +00001489 MemberSpecializationInfo *MSInfo = Record->getMemberSpecializationInfo();
1490 assert(MSInfo && "No member specialization information?");
Douglas Gregorc42b6522010-04-09 21:02:29 +00001491
1492 if (MSInfo->getTemplateSpecializationKind()
1493 == TSK_ExplicitSpecialization)
1494 continue;
1495
Douglas Gregor0d035142009-10-27 18:42:08 +00001496 if (CheckSpecializationInstantiationRedecl(PointOfInstantiation, TSK,
1497 Record,
1498 MSInfo->getTemplateSpecializationKind(),
1499 MSInfo->getPointOfInstantiation(),
1500 SuppressNew) ||
1501 SuppressNew)
Douglas Gregorf6b11852009-10-08 15:14:33 +00001502 continue;
1503
Douglas Gregor0d035142009-10-27 18:42:08 +00001504 CXXRecordDecl *Pattern = Record->getInstantiatedFromMemberClass();
1505 assert(Pattern && "Missing instantiated-from-template information");
1506
Douglas Gregor952b0172010-02-11 01:04:33 +00001507 if (!Record->getDefinition()) {
1508 if (!Pattern->getDefinition()) {
Douglas Gregor0d035142009-10-27 18:42:08 +00001509 // C++0x [temp.explicit]p8:
1510 // An explicit instantiation definition that names a class template
1511 // specialization explicitly instantiates the class template
1512 // specialization and is only an explicit instantiation definition
1513 // of members whose definition is visible at the point of
1514 // instantiation.
1515 if (TSK == TSK_ExplicitInstantiationDeclaration) {
1516 MSInfo->setTemplateSpecializationKind(TSK);
1517 MSInfo->setPointOfInstantiation(PointOfInstantiation);
1518 }
1519
1520 continue;
1521 }
1522
1523 InstantiateClass(PointOfInstantiation, Record, Pattern,
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001524 TemplateArgs,
1525 TSK);
Douglas Gregor0d035142009-10-27 18:42:08 +00001526 }
Douglas Gregore9374d52009-10-08 01:19:17 +00001527
Douglas Gregor952b0172010-02-11 01:04:33 +00001528 Pattern = cast_or_null<CXXRecordDecl>(Record->getDefinition());
Douglas Gregor0d035142009-10-27 18:42:08 +00001529 if (Pattern)
1530 InstantiateClassMembers(PointOfInstantiation, Pattern, TemplateArgs,
1531 TSK);
Douglas Gregora58861f2009-05-13 20:28:22 +00001532 }
1533 }
1534}
1535
1536/// \brief Instantiate the definitions of all of the members of the
1537/// given class template specialization, which was named as part of an
1538/// explicit instantiation.
Mike Stump1eb44332009-09-09 15:08:12 +00001539void
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001540Sema::InstantiateClassTemplateSpecializationMembers(
Douglas Gregora58861f2009-05-13 20:28:22 +00001541 SourceLocation PointOfInstantiation,
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001542 ClassTemplateSpecializationDecl *ClassTemplateSpec,
1543 TemplateSpecializationKind TSK) {
Douglas Gregora58861f2009-05-13 20:28:22 +00001544 // C++0x [temp.explicit]p7:
1545 // An explicit instantiation that names a class template
1546 // specialization is an explicit instantion of the same kind
1547 // (declaration or definition) of each of its members (not
1548 // including members inherited from base classes) that has not
1549 // been previously explicitly specialized in the translation unit
1550 // containing the explicit instantiation, except as described
1551 // below.
1552 InstantiateClassMembers(PointOfInstantiation, ClassTemplateSpec,
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001553 getTemplateInstantiationArgs(ClassTemplateSpec),
1554 TSK);
Douglas Gregora58861f2009-05-13 20:28:22 +00001555}
1556
Mike Stump1eb44332009-09-09 15:08:12 +00001557Sema::OwningStmtResult
Douglas Gregord6350ae2009-08-28 20:31:08 +00001558Sema::SubstStmt(Stmt *S, const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor43959a92009-08-20 07:17:43 +00001559 if (!S)
1560 return Owned(S);
1561
1562 TemplateInstantiator Instantiator(*this, TemplateArgs,
1563 SourceLocation(),
1564 DeclarationName());
1565 return Instantiator.TransformStmt(S);
1566}
1567
Mike Stump1eb44332009-09-09 15:08:12 +00001568Sema::OwningExprResult
Douglas Gregord6350ae2009-08-28 20:31:08 +00001569Sema::SubstExpr(Expr *E, const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregorb98b1992009-08-11 05:31:07 +00001570 if (!E)
1571 return Owned(E);
Mike Stump1eb44332009-09-09 15:08:12 +00001572
Douglas Gregorb98b1992009-08-11 05:31:07 +00001573 TemplateInstantiator Instantiator(*this, TemplateArgs,
1574 SourceLocation(),
1575 DeclarationName());
1576 return Instantiator.TransformExpr(E);
1577}
1578
John McCallce3ff2b2009-08-25 22:02:44 +00001579/// \brief Do template substitution on a nested-name-specifier.
Douglas Gregorab452ba2009-03-26 23:50:42 +00001580NestedNameSpecifier *
John McCallce3ff2b2009-08-25 22:02:44 +00001581Sema::SubstNestedNameSpecifier(NestedNameSpecifier *NNS,
Douglas Gregord6350ae2009-08-28 20:31:08 +00001582 SourceRange Range,
1583 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregordcee1a12009-08-06 05:28:30 +00001584 TemplateInstantiator Instantiator(*this, TemplateArgs, Range.getBegin(),
1585 DeclarationName());
Douglas Gregoredc90502010-02-25 04:46:04 +00001586 return Instantiator.TransformNestedNameSpecifier(NNS, Range);
Douglas Gregor5953d8b2009-03-19 17:26:29 +00001587}
Douglas Gregorde650ae2009-03-31 18:38:02 +00001588
1589TemplateName
John McCallce3ff2b2009-08-25 22:02:44 +00001590Sema::SubstTemplateName(TemplateName Name, SourceLocation Loc,
Douglas Gregord6350ae2009-08-28 20:31:08 +00001591 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregord1067e52009-08-06 06:41:21 +00001592 TemplateInstantiator Instantiator(*this, TemplateArgs, Loc,
1593 DeclarationName());
1594 return Instantiator.TransformTemplateName(Name);
Douglas Gregorde650ae2009-03-31 18:38:02 +00001595}
Douglas Gregor91333002009-06-11 00:06:24 +00001596
John McCall833ca992009-10-29 08:12:44 +00001597bool Sema::Subst(const TemplateArgumentLoc &Input, TemplateArgumentLoc &Output,
1598 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor670444e2009-08-04 22:27:00 +00001599 TemplateInstantiator Instantiator(*this, TemplateArgs, SourceLocation(),
1600 DeclarationName());
John McCall833ca992009-10-29 08:12:44 +00001601
1602 return Instantiator.TransformTemplateArgument(Input, Output);
Douglas Gregor91333002009-06-11 00:06:24 +00001603}