blob: abc8e5fb5609dc2ef2ce07e20e5fc7758562caea [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
John McCallc4e70192009-09-11 04:59:25 +0000592 /// \brief Check for tag mismatches when instantiating an
593 /// elaborated type.
594 QualType RebuildElaboratedType(QualType T, ElaboratedType::TagKind Tag);
595
John McCall454feb92009-12-08 09:21:05 +0000596 Sema::OwningExprResult TransformPredefinedExpr(PredefinedExpr *E);
597 Sema::OwningExprResult TransformDeclRefExpr(DeclRefExpr *E);
John McCall454feb92009-12-08 09:21:05 +0000598 Sema::OwningExprResult TransformCXXDefaultArgExpr(CXXDefaultArgExpr *E);
John McCallb8fc0532010-02-06 08:42:39 +0000599 Sema::OwningExprResult TransformTemplateParmRefExpr(DeclRefExpr *E,
600 NonTypeTemplateParmDecl *D);
Sebastian Redla29e51b2009-11-08 13:56:19 +0000601
John McCall21ef0fa2010-03-11 09:03:00 +0000602 /// \brief Transforms a function proto type by performing
603 /// substitution in the function parameters, possibly adjusting
604 /// their types and marking default arguments as uninstantiated.
605 bool TransformFunctionTypeParams(FunctionProtoTypeLoc TL,
606 llvm::SmallVectorImpl<QualType> &PTypes,
607 llvm::SmallVectorImpl<ParmVarDecl*> &PVars);
608
609 ParmVarDecl *TransformFunctionTypeParam(ParmVarDecl *OldParm);
610
Mike Stump1eb44332009-09-09 15:08:12 +0000611 /// \brief Transforms a template type parameter type by performing
Douglas Gregor577f75a2009-08-04 16:50:30 +0000612 /// substitution of the corresponding template type argument.
John McCalla2becad2009-10-21 00:40:46 +0000613 QualType TransformTemplateTypeParmType(TypeLocBuilder &TLB,
Douglas Gregor124b8782010-02-16 19:09:40 +0000614 TemplateTypeParmTypeLoc TL,
615 QualType ObjectType);
Douglas Gregor577f75a2009-08-04 16:50:30 +0000616 };
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000617}
618
Douglas Gregor7c1e98f2010-03-01 15:56:25 +0000619Decl *TemplateInstantiator::TransformDecl(SourceLocation Loc, Decl *D) {
Douglas Gregorc68afe22009-09-03 21:38:09 +0000620 if (!D)
621 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000622
Douglas Gregorc68afe22009-09-03 21:38:09 +0000623 if (TemplateTemplateParmDecl *TTP = dyn_cast<TemplateTemplateParmDecl>(D)) {
Douglas Gregord6350ae2009-08-28 20:31:08 +0000624 if (TTP->getDepth() < TemplateArgs.getNumLevels()) {
Douglas Gregor6d3e6272010-02-05 19:54:12 +0000625 // If the corresponding template argument is NULL or non-existent, it's
626 // because we are performing instantiation from explicitly-specified
627 // template arguments in a function template, but there were some
628 // arguments left unspecified.
629 if (!TemplateArgs.hasTemplateArgument(TTP->getDepth(),
630 TTP->getPosition()))
631 return D;
632
Douglas Gregor788cd062009-11-11 01:00:40 +0000633 TemplateName Template
634 = TemplateArgs(TTP->getDepth(), TTP->getPosition()).getAsTemplate();
635 assert(!Template.isNull() && Template.getAsTemplateDecl() &&
Douglas Gregord6350ae2009-08-28 20:31:08 +0000636 "Wrong kind of template template argument");
Douglas Gregor788cd062009-11-11 01:00:40 +0000637 return Template.getAsTemplateDecl();
Douglas Gregord6350ae2009-08-28 20:31:08 +0000638 }
Mike Stump1eb44332009-09-09 15:08:12 +0000639
Douglas Gregor788cd062009-11-11 01:00:40 +0000640 // Fall through to find the instantiated declaration for this template
641 // template parameter.
Douglas Gregord1067e52009-08-06 06:41:21 +0000642 }
Mike Stump1eb44332009-09-09 15:08:12 +0000643
Douglas Gregor7c1e98f2010-03-01 15:56:25 +0000644 return SemaRef.FindInstantiatedDecl(Loc, cast<NamedDecl>(D), TemplateArgs);
Douglas Gregor577f75a2009-08-04 16:50:30 +0000645}
646
Douglas Gregoraac571c2010-03-01 17:25:41 +0000647Decl *TemplateInstantiator::TransformDefinition(SourceLocation Loc, Decl *D) {
John McCallce3ff2b2009-08-25 22:02:44 +0000648 Decl *Inst = getSema().SubstDecl(D, getSema().CurContext, TemplateArgs);
Douglas Gregor43959a92009-08-20 07:17:43 +0000649 if (!Inst)
650 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000651
Douglas Gregor43959a92009-08-20 07:17:43 +0000652 getSema().CurrentInstantiationScope->InstantiatedLocal(D, Inst);
653 return Inst;
654}
655
Douglas Gregor6cd21982009-10-20 05:58:46 +0000656NamedDecl *
657TemplateInstantiator::TransformFirstQualifierInScope(NamedDecl *D,
658 SourceLocation Loc) {
659 // If the first part of the nested-name-specifier was a template type
660 // parameter, instantiate that type parameter down to a tag type.
661 if (TemplateTypeParmDecl *TTPD = dyn_cast_or_null<TemplateTypeParmDecl>(D)) {
662 const TemplateTypeParmType *TTP
663 = cast<TemplateTypeParmType>(getSema().Context.getTypeDeclType(TTPD));
664 if (TTP->getDepth() < TemplateArgs.getNumLevels()) {
665 QualType T = TemplateArgs(TTP->getDepth(), TTP->getIndex()).getAsType();
666 if (T.isNull())
Douglas Gregor7c1e98f2010-03-01 15:56:25 +0000667 return cast_or_null<NamedDecl>(TransformDecl(Loc, D));
Douglas Gregor6cd21982009-10-20 05:58:46 +0000668
669 if (const TagType *Tag = T->getAs<TagType>())
670 return Tag->getDecl();
671
672 // The resulting type is not a tag; complain.
673 getSema().Diag(Loc, diag::err_nested_name_spec_non_tag) << T;
674 return 0;
675 }
676 }
677
Douglas Gregor7c1e98f2010-03-01 15:56:25 +0000678 return cast_or_null<NamedDecl>(TransformDecl(Loc, D));
Douglas Gregor6cd21982009-10-20 05:58:46 +0000679}
680
Douglas Gregor43959a92009-08-20 07:17:43 +0000681VarDecl *
682TemplateInstantiator::RebuildExceptionDecl(VarDecl *ExceptionDecl,
Mike Stump1eb44332009-09-09 15:08:12 +0000683 QualType T,
John McCalla93c9342009-12-07 02:54:59 +0000684 TypeSourceInfo *Declarator,
Douglas Gregor43959a92009-08-20 07:17:43 +0000685 IdentifierInfo *Name,
Mike Stump1eb44332009-09-09 15:08:12 +0000686 SourceLocation Loc,
Douglas Gregor43959a92009-08-20 07:17:43 +0000687 SourceRange TypeRange) {
688 VarDecl *Var = inherited::RebuildExceptionDecl(ExceptionDecl, T, Declarator,
689 Name, Loc, TypeRange);
690 if (Var && !Var->isInvalidDecl())
691 getSema().CurrentInstantiationScope->InstantiatedLocal(ExceptionDecl, Var);
692 return Var;
693}
694
John McCallc4e70192009-09-11 04:59:25 +0000695QualType
696TemplateInstantiator::RebuildElaboratedType(QualType T,
697 ElaboratedType::TagKind Tag) {
698 if (const TagType *TT = T->getAs<TagType>()) {
699 TagDecl* TD = TT->getDecl();
700
701 // FIXME: this location is very wrong; we really need typelocs.
702 SourceLocation TagLocation = TD->getTagKeywordLoc();
703
704 // FIXME: type might be anonymous.
705 IdentifierInfo *Id = TD->getIdentifier();
706
707 // TODO: should we even warn on struct/class mismatches for this? Seems
708 // like it's likely to produce a lot of spurious errors.
709 if (!SemaRef.isAcceptableTagRedeclaration(TD, Tag, TagLocation, *Id)) {
710 SemaRef.Diag(TagLocation, diag::err_use_with_wrong_tag)
711 << Id
Douglas Gregor849b2432010-03-31 17:46:05 +0000712 << FixItHint::CreateReplacement(SourceRange(TagLocation),
713 TD->getKindName());
John McCallc4e70192009-09-11 04:59:25 +0000714 SemaRef.Diag(TD->getLocation(), diag::note_previous_use);
715 }
716 }
717
718 return TreeTransform<TemplateInstantiator>::RebuildElaboratedType(T, Tag);
719}
720
721Sema::OwningExprResult
John McCall454feb92009-12-08 09:21:05 +0000722TemplateInstantiator::TransformPredefinedExpr(PredefinedExpr *E) {
Anders Carlsson773f3972009-09-11 01:22:35 +0000723 if (!E->isTypeDependent())
724 return SemaRef.Owned(E->Retain());
725
726 FunctionDecl *currentDecl = getSema().getCurFunctionDecl();
727 assert(currentDecl && "Must have current function declaration when "
728 "instantiating.");
729
730 PredefinedExpr::IdentType IT = E->getIdentType();
731
Anders Carlsson848fa642010-02-11 18:20:28 +0000732 unsigned Length = PredefinedExpr::ComputeName(IT, currentDecl).length();
Anders Carlsson773f3972009-09-11 01:22:35 +0000733
734 llvm::APInt LengthI(32, Length + 1);
John McCall0953e762009-09-24 19:53:00 +0000735 QualType ResTy = getSema().Context.CharTy.withConst();
Anders Carlsson773f3972009-09-11 01:22:35 +0000736 ResTy = getSema().Context.getConstantArrayType(ResTy, LengthI,
737 ArrayType::Normal, 0);
738 PredefinedExpr *PE =
739 new (getSema().Context) PredefinedExpr(E->getLocation(), ResTy, IT);
740 return getSema().Owned(PE);
741}
742
743Sema::OwningExprResult
John McCallb8fc0532010-02-06 08:42:39 +0000744TemplateInstantiator::TransformTemplateParmRefExpr(DeclRefExpr *E,
Douglas Gregordcee9802010-02-08 23:41:45 +0000745 NonTypeTemplateParmDecl *NTTP) {
John McCallb8fc0532010-02-06 08:42:39 +0000746 // If the corresponding template argument is NULL or non-existent, it's
747 // because we are performing instantiation from explicitly-specified
748 // template arguments in a function template, but there were some
749 // arguments left unspecified.
750 if (!TemplateArgs.hasTemplateArgument(NTTP->getDepth(),
751 NTTP->getPosition()))
752 return SemaRef.Owned(E->Retain());
Mike Stump1eb44332009-09-09 15:08:12 +0000753
John McCallb8fc0532010-02-06 08:42:39 +0000754 const TemplateArgument &Arg = TemplateArgs(NTTP->getDepth(),
755 NTTP->getPosition());
Mike Stump1eb44332009-09-09 15:08:12 +0000756
John McCallb8fc0532010-02-06 08:42:39 +0000757 // The template argument itself might be an expression, in which
758 // case we just return that expression.
759 if (Arg.getKind() == TemplateArgument::Expression)
760 return SemaRef.Owned(Arg.getAsExpr()->Retain());
Mike Stump1eb44332009-09-09 15:08:12 +0000761
John McCallb8fc0532010-02-06 08:42:39 +0000762 if (Arg.getKind() == TemplateArgument::Declaration) {
763 ValueDecl *VD = cast<ValueDecl>(Arg.getAsDecl());
Mike Stump1eb44332009-09-09 15:08:12 +0000764
John McCall645cf442010-02-06 10:23:53 +0000765 // Find the instantiation of the template argument. This is
766 // required for nested templates.
John McCallb8fc0532010-02-06 08:42:39 +0000767 VD = cast_or_null<ValueDecl>(
Douglas Gregor7c1e98f2010-03-01 15:56:25 +0000768 getSema().FindInstantiatedDecl(E->getLocation(),
769 VD, TemplateArgs));
John McCallb8fc0532010-02-06 08:42:39 +0000770 if (!VD)
771 return SemaRef.ExprError();
Mike Stump1eb44332009-09-09 15:08:12 +0000772
John McCall645cf442010-02-06 10:23:53 +0000773 // Derive the type we want the substituted decl to have. This had
774 // better be non-dependent, or these checks will have serious problems.
775 QualType TargetType = SemaRef.SubstType(NTTP->getType(), TemplateArgs,
Douglas Gregordcee9802010-02-08 23:41:45 +0000776 E->getLocation(),
777 DeclarationName());
John McCall645cf442010-02-06 10:23:53 +0000778 assert(!TargetType.isNull() && "type substitution failed for param type");
779 assert(!TargetType->isDependentType() && "param type still dependent");
Douglas Gregor02024a92010-03-28 02:42:43 +0000780 return SemaRef.BuildExpressionFromDeclTemplateArgument(Arg,
781 TargetType,
782 E->getLocation());
John McCallb8fc0532010-02-06 08:42:39 +0000783 }
784
Douglas Gregor02024a92010-03-28 02:42:43 +0000785 return SemaRef.BuildExpressionFromIntegralTemplateArgument(Arg,
786 E->getSourceRange().getBegin());
John McCallb8fc0532010-02-06 08:42:39 +0000787}
788
789
790Sema::OwningExprResult
791TemplateInstantiator::TransformDeclRefExpr(DeclRefExpr *E) {
792 NamedDecl *D = E->getDecl();
793 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(D)) {
794 if (NTTP->getDepth() < TemplateArgs.getNumLevels())
795 return TransformTemplateParmRefExpr(E, NTTP);
Douglas Gregor550d9b22009-10-31 17:21:17 +0000796
797 // We have a non-type template parameter that isn't fully substituted;
798 // FindInstantiatedDecl will find it in the local instantiation scope.
Douglas Gregorb98b1992009-08-11 05:31:07 +0000799 }
Mike Stump1eb44332009-09-09 15:08:12 +0000800
John McCall454feb92009-12-08 09:21:05 +0000801 return TreeTransform<TemplateInstantiator>::TransformDeclRefExpr(E);
Douglas Gregorb98b1992009-08-11 05:31:07 +0000802}
803
Sebastian Redla29e51b2009-11-08 13:56:19 +0000804Sema::OwningExprResult TemplateInstantiator::TransformCXXDefaultArgExpr(
John McCall454feb92009-12-08 09:21:05 +0000805 CXXDefaultArgExpr *E) {
Sebastian Redla29e51b2009-11-08 13:56:19 +0000806 assert(!cast<FunctionDecl>(E->getParam()->getDeclContext())->
807 getDescribedFunctionTemplate() &&
808 "Default arg expressions are never formed in dependent cases.");
Douglas Gregor036aed12009-12-23 23:03:06 +0000809 return SemaRef.BuildCXXDefaultArgExpr(E->getUsedLocation(),
810 cast<FunctionDecl>(E->getParam()->getDeclContext()),
811 E->getParam());
Sebastian Redla29e51b2009-11-08 13:56:19 +0000812}
813
814
John McCall21ef0fa2010-03-11 09:03:00 +0000815bool
816TemplateInstantiator::TransformFunctionTypeParams(FunctionProtoTypeLoc TL,
817 llvm::SmallVectorImpl<QualType> &PTypes,
818 llvm::SmallVectorImpl<ParmVarDecl*> &PVars) {
819 // Create a local instantiation scope for the parameters.
Douglas Gregor2b0749a42010-03-25 15:38:42 +0000820 // FIXME: When we implement the C++0x late-specified return type,
821 // we will need to move this scope out to the function type itself.
822 bool IsTemporaryScope = (SemaRef.CurrentInstantiationScope != 0);
823 Sema::LocalInstantiationScope Scope(SemaRef, IsTemporaryScope,
824 IsTemporaryScope);
John McCall21ef0fa2010-03-11 09:03:00 +0000825
826 if (TreeTransform<TemplateInstantiator>::
827 TransformFunctionTypeParams(TL, PTypes, PVars))
828 return true;
829
John McCall21ef0fa2010-03-11 09:03:00 +0000830 return false;
831}
832
833ParmVarDecl *
834TemplateInstantiator::TransformFunctionTypeParam(ParmVarDecl *OldParm) {
Douglas Gregorcb27b0f2010-04-12 07:48:19 +0000835 return SemaRef.SubstParmVarDecl(OldParm, TemplateArgs);
John McCall21ef0fa2010-03-11 09:03:00 +0000836}
837
Mike Stump1eb44332009-09-09 15:08:12 +0000838QualType
John McCalla2becad2009-10-21 00:40:46 +0000839TemplateInstantiator::TransformTemplateTypeParmType(TypeLocBuilder &TLB,
Douglas Gregor124b8782010-02-16 19:09:40 +0000840 TemplateTypeParmTypeLoc TL,
841 QualType ObjectType) {
John McCalla2becad2009-10-21 00:40:46 +0000842 TemplateTypeParmType *T = TL.getTypePtr();
Douglas Gregord6350ae2009-08-28 20:31:08 +0000843 if (T->getDepth() < TemplateArgs.getNumLevels()) {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000844 // Replace the template type parameter with its corresponding
845 // template argument.
Mike Stump1eb44332009-09-09 15:08:12 +0000846
847 // If the corresponding template argument is NULL or doesn't exist, it's
848 // because we are performing instantiation from explicitly-specified
849 // template arguments in a function template class, but there were some
Douglas Gregor16134c62009-07-01 00:28:38 +0000850 // arguments left unspecified.
John McCalla2becad2009-10-21 00:40:46 +0000851 if (!TemplateArgs.hasTemplateArgument(T->getDepth(), T->getIndex())) {
852 TemplateTypeParmTypeLoc NewTL
853 = TLB.push<TemplateTypeParmTypeLoc>(TL.getType());
854 NewTL.setNameLoc(TL.getNameLoc());
855 return TL.getType();
856 }
Mike Stump1eb44332009-09-09 15:08:12 +0000857
858 assert(TemplateArgs(T->getDepth(), T->getIndex()).getKind()
Douglas Gregord6350ae2009-08-28 20:31:08 +0000859 == TemplateArgument::Type &&
Douglas Gregor99ebf652009-02-27 19:31:52 +0000860 "Template argument kind mismatch");
Douglas Gregord6350ae2009-08-28 20:31:08 +0000861
John McCall49a832b2009-10-18 09:09:24 +0000862 QualType Replacement
863 = TemplateArgs(T->getDepth(), T->getIndex()).getAsType();
864
865 // TODO: only do this uniquing once, at the start of instantiation.
John McCalla2becad2009-10-21 00:40:46 +0000866 QualType Result
867 = getSema().Context.getSubstTemplateTypeParmType(T, Replacement);
868 SubstTemplateTypeParmTypeLoc NewTL
869 = TLB.push<SubstTemplateTypeParmTypeLoc>(Result);
870 NewTL.setNameLoc(TL.getNameLoc());
871 return Result;
Mike Stump1eb44332009-09-09 15:08:12 +0000872 }
Douglas Gregor99ebf652009-02-27 19:31:52 +0000873
874 // The template type parameter comes from an inner template (e.g.,
875 // the template parameter list of a member template inside the
876 // template we are instantiating). Create a new template type
877 // parameter with the template "level" reduced by one.
John McCalla2becad2009-10-21 00:40:46 +0000878 QualType Result
879 = getSema().Context.getTemplateTypeParmType(T->getDepth()
880 - TemplateArgs.getNumLevels(),
881 T->getIndex(),
882 T->isParameterPack(),
883 T->getName());
884 TemplateTypeParmTypeLoc NewTL = TLB.push<TemplateTypeParmTypeLoc>(Result);
885 NewTL.setNameLoc(TL.getNameLoc());
886 return Result;
Douglas Gregorcd281c32009-02-28 00:25:32 +0000887}
Douglas Gregor99ebf652009-02-27 19:31:52 +0000888
John McCallce3ff2b2009-08-25 22:02:44 +0000889/// \brief Perform substitution on the type T with a given set of template
890/// arguments.
Douglas Gregor99ebf652009-02-27 19:31:52 +0000891///
892/// This routine substitutes the given template arguments into the
893/// type T and produces the instantiated type.
894///
895/// \param T the type into which the template arguments will be
896/// substituted. If this type is not dependent, it will be returned
897/// immediately.
898///
899/// \param TemplateArgs the template arguments that will be
900/// substituted for the top-level template parameters within T.
901///
Douglas Gregor99ebf652009-02-27 19:31:52 +0000902/// \param Loc the location in the source code where this substitution
903/// is being performed. It will typically be the location of the
904/// declarator (if we're instantiating the type of some declaration)
905/// or the location of the type in the source code (if, e.g., we're
906/// instantiating the type of a cast expression).
907///
908/// \param Entity the name of the entity associated with a declaration
909/// being instantiated (if any). May be empty to indicate that there
910/// is no such entity (if, e.g., this is a type that occurs as part of
911/// a cast expression) or that the entity has no name (e.g., an
912/// unnamed function parameter).
913///
914/// \returns If the instantiation succeeds, the instantiated
915/// type. Otherwise, produces diagnostics and returns a NULL type.
John McCalla93c9342009-12-07 02:54:59 +0000916TypeSourceInfo *Sema::SubstType(TypeSourceInfo *T,
John McCallcd7ba1c2009-10-21 00:58:09 +0000917 const MultiLevelTemplateArgumentList &Args,
918 SourceLocation Loc,
919 DeclarationName Entity) {
920 assert(!ActiveTemplateInstantiations.empty() &&
921 "Cannot perform an instantiation without some context on the "
922 "instantiation stack");
923
924 if (!T->getType()->isDependentType())
925 return T;
926
927 TemplateInstantiator Instantiator(*this, Args, Loc, Entity);
928 return Instantiator.TransformType(T);
929}
930
931/// Deprecated form of the above.
Mike Stump1eb44332009-09-09 15:08:12 +0000932QualType Sema::SubstType(QualType T,
Douglas Gregord6350ae2009-08-28 20:31:08 +0000933 const MultiLevelTemplateArgumentList &TemplateArgs,
John McCallce3ff2b2009-08-25 22:02:44 +0000934 SourceLocation Loc, DeclarationName Entity) {
Douglas Gregordf667e72009-03-10 20:44:00 +0000935 assert(!ActiveTemplateInstantiations.empty() &&
936 "Cannot perform an instantiation without some context on the "
937 "instantiation stack");
938
Douglas Gregor99ebf652009-02-27 19:31:52 +0000939 // If T is not a dependent type, there is nothing to do.
940 if (!T->isDependentType())
941 return T;
942
Douglas Gregor577f75a2009-08-04 16:50:30 +0000943 TemplateInstantiator Instantiator(*this, TemplateArgs, Loc, Entity);
944 return Instantiator.TransformType(T);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000945}
Douglas Gregor2943aed2009-03-03 04:44:36 +0000946
John McCall6cd3b9f2010-04-09 17:38:44 +0000947static bool NeedsInstantiationAsFunctionType(TypeSourceInfo *T) {
948 if (T->getType()->isDependentType())
949 return true;
950
951 TypeLoc TL = T->getTypeLoc();
952 if (!isa<FunctionProtoTypeLoc>(TL))
953 return false;
954
955 FunctionProtoTypeLoc FP = cast<FunctionProtoTypeLoc>(TL);
956 for (unsigned I = 0, E = FP.getNumArgs(); I != E; ++I) {
957 ParmVarDecl *P = FP.getArg(I);
958
959 // TODO: currently we always rebuild expressions. When we
960 // properly get lazier about this, we should use the same
961 // logic to avoid rebuilding prototypes here.
962 if (P->hasInit())
963 return true;
964 }
965
966 return false;
967}
968
969/// A form of SubstType intended specifically for instantiating the
970/// type of a FunctionDecl. Its purpose is solely to force the
971/// instantiation of default-argument expressions.
972TypeSourceInfo *Sema::SubstFunctionDeclType(TypeSourceInfo *T,
973 const MultiLevelTemplateArgumentList &Args,
974 SourceLocation Loc,
975 DeclarationName Entity) {
976 assert(!ActiveTemplateInstantiations.empty() &&
977 "Cannot perform an instantiation without some context on the "
978 "instantiation stack");
979
980 if (!NeedsInstantiationAsFunctionType(T))
981 return T;
982
983 TemplateInstantiator Instantiator(*this, Args, Loc, Entity);
984
985 TypeLocBuilder TLB;
986
987 TypeLoc TL = T->getTypeLoc();
988 TLB.reserve(TL.getFullDataSize());
989
990 QualType Result = Instantiator.TransformType(TLB, TL, QualType());
991 if (Result.isNull())
992 return 0;
993
994 return TLB.getTypeSourceInfo(Context, Result);
995}
996
Douglas Gregorcb27b0f2010-04-12 07:48:19 +0000997ParmVarDecl *Sema::SubstParmVarDecl(ParmVarDecl *OldParm,
998 const MultiLevelTemplateArgumentList &TemplateArgs) {
999 TypeSourceInfo *OldDI = OldParm->getTypeSourceInfo();
1000 TypeSourceInfo *NewDI = SubstType(OldDI, TemplateArgs, OldParm->getLocation(),
1001 OldParm->getDeclName());
1002 if (!NewDI)
1003 return 0;
1004
1005 if (NewDI->getType()->isVoidType()) {
1006 Diag(OldParm->getLocation(), diag::err_param_with_void_type);
1007 return 0;
1008 }
1009
1010 ParmVarDecl *NewParm = CheckParameter(Context.getTranslationUnitDecl(),
1011 NewDI, NewDI->getType(),
1012 OldParm->getIdentifier(),
1013 OldParm->getLocation(),
Douglas Gregor16573fa2010-04-19 22:54:31 +00001014 OldParm->getStorageClass(),
1015 OldParm->getStorageClassAsWritten());
Douglas Gregorcb27b0f2010-04-12 07:48:19 +00001016 if (!NewParm)
1017 return 0;
1018
1019 // Mark the (new) default argument as uninstantiated (if any).
1020 if (OldParm->hasUninstantiatedDefaultArg()) {
1021 Expr *Arg = OldParm->getUninstantiatedDefaultArg();
1022 NewParm->setUninstantiatedDefaultArg(Arg);
1023 } else if (Expr *Arg = OldParm->getDefaultArg())
1024 NewParm->setUninstantiatedDefaultArg(Arg);
1025
1026 NewParm->setHasInheritedDefaultArg(OldParm->hasInheritedDefaultArg());
1027
1028 CurrentInstantiationScope->InstantiatedLocal(OldParm, NewParm);
1029 return NewParm;
1030}
1031
John McCallce3ff2b2009-08-25 22:02:44 +00001032/// \brief Perform substitution on the base class specifiers of the
1033/// given class template specialization.
Douglas Gregor2943aed2009-03-03 04:44:36 +00001034///
1035/// Produces a diagnostic and returns true on error, returns false and
1036/// attaches the instantiated base classes to the class template
1037/// specialization if successful.
Mike Stump1eb44332009-09-09 15:08:12 +00001038bool
John McCallce3ff2b2009-08-25 22:02:44 +00001039Sema::SubstBaseSpecifiers(CXXRecordDecl *Instantiation,
1040 CXXRecordDecl *Pattern,
Douglas Gregord6350ae2009-08-28 20:31:08 +00001041 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor2943aed2009-03-03 04:44:36 +00001042 bool Invalid = false;
Douglas Gregor0ca20ac2009-05-29 18:27:38 +00001043 llvm::SmallVector<CXXBaseSpecifier*, 4> InstantiatedBases;
Mike Stump1eb44332009-09-09 15:08:12 +00001044 for (ClassTemplateSpecializationDecl::base_class_iterator
Douglas Gregord475b8d2009-03-25 21:17:03 +00001045 Base = Pattern->bases_begin(), BaseEnd = Pattern->bases_end();
Douglas Gregor27b152f2009-03-10 18:52:44 +00001046 Base != BaseEnd; ++Base) {
Douglas Gregor2943aed2009-03-03 04:44:36 +00001047 if (!Base->getType()->isDependentType()) {
Anders Carlsson51f94042009-12-03 17:49:57 +00001048 const CXXRecordDecl *BaseDecl =
1049 cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
1050
1051 // Make sure to set the attributes from the base.
1052 SetClassDeclAttributesFromBase(Instantiation, BaseDecl,
1053 Base->isVirtual());
1054
Fariborz Jahanian71c6e712009-07-22 17:41:53 +00001055 InstantiatedBases.push_back(new (Context) CXXBaseSpecifier(*Base));
Douglas Gregor2943aed2009-03-03 04:44:36 +00001056 continue;
1057 }
1058
Mike Stump1eb44332009-09-09 15:08:12 +00001059 QualType BaseType = SubstType(Base->getType(),
1060 TemplateArgs,
John McCallce3ff2b2009-08-25 22:02:44 +00001061 Base->getSourceRange().getBegin(),
1062 DeclarationName());
Douglas Gregor2943aed2009-03-03 04:44:36 +00001063 if (BaseType.isNull()) {
1064 Invalid = true;
1065 continue;
1066 }
1067
1068 if (CXXBaseSpecifier *InstantiatedBase
Douglas Gregord475b8d2009-03-25 21:17:03 +00001069 = CheckBaseSpecifier(Instantiation,
Douglas Gregor2943aed2009-03-03 04:44:36 +00001070 Base->getSourceRange(),
1071 Base->isVirtual(),
1072 Base->getAccessSpecifierAsWritten(),
1073 BaseType,
1074 /*FIXME: Not totally accurate */
1075 Base->getSourceRange().getBegin()))
1076 InstantiatedBases.push_back(InstantiatedBase);
1077 else
1078 Invalid = true;
1079 }
1080
Douglas Gregor27b152f2009-03-10 18:52:44 +00001081 if (!Invalid &&
Jay Foadbeaaccd2009-05-21 09:52:38 +00001082 AttachBaseSpecifiers(Instantiation, InstantiatedBases.data(),
Douglas Gregor2943aed2009-03-03 04:44:36 +00001083 InstantiatedBases.size()))
1084 Invalid = true;
1085
1086 return Invalid;
1087}
1088
Douglas Gregord475b8d2009-03-25 21:17:03 +00001089/// \brief Instantiate the definition of a class from a given pattern.
1090///
1091/// \param PointOfInstantiation The point of instantiation within the
1092/// source code.
1093///
1094/// \param Instantiation is the declaration whose definition is being
1095/// instantiated. This will be either a class template specialization
1096/// or a member class of a class template specialization.
1097///
1098/// \param Pattern is the pattern from which the instantiation
1099/// occurs. This will be either the declaration of a class template or
1100/// the declaration of a member class of a class template.
1101///
1102/// \param TemplateArgs The template arguments to be substituted into
1103/// the pattern.
1104///
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001105/// \param TSK the kind of implicit or explicit instantiation to perform.
Douglas Gregor5842ba92009-08-24 15:23:48 +00001106///
1107/// \param Complain whether to complain if the class cannot be instantiated due
1108/// to the lack of a definition.
1109///
Douglas Gregord475b8d2009-03-25 21:17:03 +00001110/// \returns true if an error occurred, false otherwise.
1111bool
1112Sema::InstantiateClass(SourceLocation PointOfInstantiation,
1113 CXXRecordDecl *Instantiation, CXXRecordDecl *Pattern,
Douglas Gregord6350ae2009-08-28 20:31:08 +00001114 const MultiLevelTemplateArgumentList &TemplateArgs,
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001115 TemplateSpecializationKind TSK,
Douglas Gregor5842ba92009-08-24 15:23:48 +00001116 bool Complain) {
Douglas Gregord475b8d2009-03-25 21:17:03 +00001117 bool Invalid = false;
John McCalle29ba202009-08-20 01:44:21 +00001118
Mike Stump1eb44332009-09-09 15:08:12 +00001119 CXXRecordDecl *PatternDef
Douglas Gregor952b0172010-02-11 01:04:33 +00001120 = cast_or_null<CXXRecordDecl>(Pattern->getDefinition());
Douglas Gregord475b8d2009-03-25 21:17:03 +00001121 if (!PatternDef) {
Douglas Gregor5842ba92009-08-24 15:23:48 +00001122 if (!Complain) {
1123 // Say nothing
1124 } else if (Pattern == Instantiation->getInstantiatedFromMemberClass()) {
Douglas Gregord475b8d2009-03-25 21:17:03 +00001125 Diag(PointOfInstantiation,
1126 diag::err_implicit_instantiate_member_undefined)
1127 << Context.getTypeDeclType(Instantiation);
1128 Diag(Pattern->getLocation(), diag::note_member_of_template_here);
1129 } else {
Douglas Gregor93dfdb12009-05-13 00:25:59 +00001130 Diag(PointOfInstantiation, diag::err_template_instantiate_undefined)
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001131 << (TSK != TSK_ImplicitInstantiation)
Douglas Gregord475b8d2009-03-25 21:17:03 +00001132 << Context.getTypeDeclType(Instantiation);
1133 Diag(Pattern->getLocation(), diag::note_template_decl_here);
1134 }
1135 return true;
1136 }
1137 Pattern = PatternDef;
1138
Douglas Gregor454885e2009-10-15 15:54:05 +00001139 // \brief Record the point of instantiation.
1140 if (MemberSpecializationInfo *MSInfo
1141 = Instantiation->getMemberSpecializationInfo()) {
1142 MSInfo->setTemplateSpecializationKind(TSK);
1143 MSInfo->setPointOfInstantiation(PointOfInstantiation);
Douglas Gregor972e6ce2009-10-27 06:26:26 +00001144 } else if (ClassTemplateSpecializationDecl *Spec
1145 = dyn_cast<ClassTemplateSpecializationDecl>(Instantiation)) {
1146 Spec->setTemplateSpecializationKind(TSK);
1147 Spec->setPointOfInstantiation(PointOfInstantiation);
Douglas Gregor454885e2009-10-15 15:54:05 +00001148 }
1149
Douglas Gregord048bb72009-03-25 21:23:52 +00001150 InstantiatingTemplate Inst(*this, PointOfInstantiation, Instantiation);
Douglas Gregord475b8d2009-03-25 21:17:03 +00001151 if (Inst)
1152 return true;
1153
1154 // Enter the scope of this instantiation. We don't use
1155 // PushDeclContext because we don't have a scope.
1156 DeclContext *PreviousContext = CurContext;
1157 CurContext = Instantiation;
1158
Douglas Gregor05030bb2010-03-24 01:33:17 +00001159 // If this is an instantiation of a local class, merge this local
1160 // instantiation scope with the enclosing scope. Otherwise, every
1161 // instantiation of a class has its own local instantiation scope.
1162 bool MergeWithParentScope = !Instantiation->isDefinedOutsideFunctionOrMethod();
1163 Sema::LocalInstantiationScope Scope(*this, MergeWithParentScope);
1164
Douglas Gregord475b8d2009-03-25 21:17:03 +00001165 // Start the definition of this instantiation.
1166 Instantiation->startDefinition();
1167
John McCallce3ff2b2009-08-25 22:02:44 +00001168 // Do substitution on the base class specifiers.
1169 if (SubstBaseSpecifiers(Instantiation, Pattern, TemplateArgs))
Douglas Gregord475b8d2009-03-25 21:17:03 +00001170 Invalid = true;
1171
Douglas Gregor0ca20ac2009-05-29 18:27:38 +00001172 llvm::SmallVector<DeclPtrTy, 4> Fields;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001173 for (RecordDecl::decl_iterator Member = Pattern->decls_begin(),
Mike Stump1eb44332009-09-09 15:08:12 +00001174 MemberEnd = Pattern->decls_end();
Douglas Gregor6ab35242009-04-09 21:40:53 +00001175 Member != MemberEnd; ++Member) {
John McCallce3ff2b2009-08-25 22:02:44 +00001176 Decl *NewMember = SubstDecl(*Member, Instantiation, TemplateArgs);
Douglas Gregord475b8d2009-03-25 21:17:03 +00001177 if (NewMember) {
Eli Friedman721e77d2009-12-07 00:22:08 +00001178 if (FieldDecl *Field = dyn_cast<FieldDecl>(NewMember))
Chris Lattnerb28317a2009-03-28 19:18:32 +00001179 Fields.push_back(DeclPtrTy::make(Field));
Eli Friedman721e77d2009-12-07 00:22:08 +00001180 else if (NewMember->isInvalidDecl())
1181 Invalid = true;
Douglas Gregord475b8d2009-03-25 21:17:03 +00001182 } else {
1183 // FIXME: Eventually, a NULL return will mean that one of the
Mike Stump390b4cc2009-05-16 07:39:55 +00001184 // instantiations was a semantic disaster, and we'll want to set Invalid =
1185 // true. For now, we expect to skip some members that we can't yet handle.
Douglas Gregord475b8d2009-03-25 21:17:03 +00001186 }
1187 }
1188
1189 // Finish checking fields.
Chris Lattnerb28317a2009-03-28 19:18:32 +00001190 ActOnFields(0, Instantiation->getLocation(), DeclPtrTy::make(Instantiation),
Jay Foadbeaaccd2009-05-21 09:52:38 +00001191 Fields.data(), Fields.size(), SourceLocation(), SourceLocation(),
Douglas Gregord475b8d2009-03-25 21:17:03 +00001192 0);
Douglas Gregor6275e0c2010-04-12 17:09:20 +00001193 CheckCompletedCXXClass(/*Scope=*/0, Instantiation);
Douglas Gregor663b5a02009-10-14 20:14:33 +00001194 if (Instantiation->isInvalidDecl())
1195 Invalid = true;
1196
Douglas Gregord475b8d2009-03-25 21:17:03 +00001197 // Exit the scope of this instantiation.
1198 CurContext = PreviousContext;
1199
Douglas Gregor159ef1e2010-01-06 04:44:19 +00001200 // If this is a polymorphic C++ class without a key function, we'll
1201 // have to mark all of the virtual members to allow emission of a vtable
1202 // in this translation unit.
Chandler Carruth17e0f402010-02-15 22:12:26 +00001203 if (Instantiation->isDynamicClass() &&
1204 !Context.getKeyFunction(Instantiation)) {
1205 // Local classes need to have their methods instantiated immediately in
1206 // order to have the correct instantiation scope.
1207 if (Instantiation->isLocalClass()) {
1208 MarkVirtualMembersReferenced(PointOfInstantiation,
1209 Instantiation);
1210 } else {
Douglas Gregor159ef1e2010-01-06 04:44:19 +00001211 ClassesWithUnmarkedVirtualMembers.push_back(std::make_pair(Instantiation,
1212 PointOfInstantiation));
Chandler Carruth17e0f402010-02-15 22:12:26 +00001213 }
1214 }
Douglas Gregor159ef1e2010-01-06 04:44:19 +00001215
Douglas Gregoraba43bb2009-05-26 20:50:29 +00001216 if (!Invalid)
1217 Consumer.HandleTagDeclDefinition(Instantiation);
1218
Douglas Gregord475b8d2009-03-25 21:17:03 +00001219 return Invalid;
1220}
1221
Mike Stump1eb44332009-09-09 15:08:12 +00001222bool
Douglas Gregor2943aed2009-03-03 04:44:36 +00001223Sema::InstantiateClassTemplateSpecialization(
Douglas Gregor972e6ce2009-10-27 06:26:26 +00001224 SourceLocation PointOfInstantiation,
Douglas Gregor2943aed2009-03-03 04:44:36 +00001225 ClassTemplateSpecializationDecl *ClassTemplateSpec,
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001226 TemplateSpecializationKind TSK,
Douglas Gregor5842ba92009-08-24 15:23:48 +00001227 bool Complain) {
Douglas Gregor2943aed2009-03-03 04:44:36 +00001228 // Perform the actual instantiation on the canonical declaration.
1229 ClassTemplateSpec = cast<ClassTemplateSpecializationDecl>(
Argyrios Kyrtzidis97fbaa22009-07-18 00:34:25 +00001230 ClassTemplateSpec->getCanonicalDecl());
Douglas Gregor2943aed2009-03-03 04:44:36 +00001231
Douglas Gregor52604ab2009-09-11 21:19:12 +00001232 // Check whether we have already instantiated or specialized this class
1233 // template specialization.
1234 if (ClassTemplateSpec->getSpecializationKind() != TSK_Undeclared) {
1235 if (ClassTemplateSpec->getSpecializationKind() ==
1236 TSK_ExplicitInstantiationDeclaration &&
1237 TSK == TSK_ExplicitInstantiationDefinition) {
1238 // An explicit instantiation definition follows an explicit instantiation
1239 // declaration (C++0x [temp.explicit]p10); go ahead and perform the
1240 // explicit instantiation.
1241 ClassTemplateSpec->setSpecializationKind(TSK);
Douglas Gregor52604ab2009-09-11 21:19:12 +00001242 return false;
1243 }
1244
1245 // We can only instantiate something that hasn't already been
1246 // instantiated or specialized. Fail without any diagnostics: our
1247 // caller will provide an error message.
Douglas Gregor2943aed2009-03-03 04:44:36 +00001248 return true;
Douglas Gregor52604ab2009-09-11 21:19:12 +00001249 }
Douglas Gregor2943aed2009-03-03 04:44:36 +00001250
Douglas Gregor9eea08b2009-09-15 16:51:42 +00001251 if (ClassTemplateSpec->isInvalidDecl())
1252 return true;
1253
Douglas Gregor2943aed2009-03-03 04:44:36 +00001254 ClassTemplateDecl *Template = ClassTemplateSpec->getSpecializedTemplate();
Douglas Gregord6350ae2009-08-28 20:31:08 +00001255 CXXRecordDecl *Pattern = 0;
Douglas Gregorc8ab2562009-05-31 09:31:02 +00001256
Douglas Gregorc1efb3f2009-06-12 22:31:52 +00001257 // C++ [temp.class.spec.match]p1:
1258 // When a class template is used in a context that requires an
1259 // instantiation of the class, it is necessary to determine
1260 // whether the instantiation is to be generated using the primary
1261 // template or one of the partial specializations. This is done by
1262 // matching the template arguments of the class template
1263 // specialization with the template argument lists of the partial
1264 // specializations.
Douglas Gregor199d9912009-06-05 00:53:49 +00001265 typedef std::pair<ClassTemplatePartialSpecializationDecl *,
1266 TemplateArgumentList *> MatchResult;
1267 llvm::SmallVector<MatchResult, 4> Matched;
Mike Stump1eb44332009-09-09 15:08:12 +00001268 for (llvm::FoldingSet<ClassTemplatePartialSpecializationDecl>::iterator
Douglas Gregorc8ab2562009-05-31 09:31:02 +00001269 Partial = Template->getPartialSpecializations().begin(),
1270 PartialEnd = Template->getPartialSpecializations().end();
1271 Partial != PartialEnd;
1272 ++Partial) {
John McCall5769d612010-02-08 23:07:23 +00001273 TemplateDeductionInfo Info(Context, PointOfInstantiation);
Douglas Gregorf67875d2009-06-12 18:26:56 +00001274 if (TemplateDeductionResult Result
Mike Stump1eb44332009-09-09 15:08:12 +00001275 = DeduceTemplateArguments(&*Partial,
Douglas Gregorf67875d2009-06-12 18:26:56 +00001276 ClassTemplateSpec->getTemplateArgs(),
1277 Info)) {
1278 // FIXME: Store the failed-deduction information for use in
1279 // diagnostics, later.
1280 (void)Result;
1281 } else {
1282 Matched.push_back(std::make_pair(&*Partial, Info.take()));
1283 }
Douglas Gregorc8ab2562009-05-31 09:31:02 +00001284 }
1285
Douglas Gregored9c0f92009-10-29 00:04:11 +00001286 if (Matched.size() >= 1) {
Douglas Gregorbf4ea562009-09-15 16:23:51 +00001287 llvm::SmallVector<MatchResult, 4>::iterator Best = Matched.begin();
Douglas Gregored9c0f92009-10-29 00:04:11 +00001288 if (Matched.size() == 1) {
1289 // -- If exactly one matching specialization is found, the
1290 // instantiation is generated from that specialization.
1291 // We don't need to do anything for this.
1292 } else {
1293 // -- If more than one matching specialization is found, the
1294 // partial order rules (14.5.4.2) are used to determine
1295 // whether one of the specializations is more specialized
1296 // than the others. If none of the specializations is more
1297 // specialized than all of the other matching
1298 // specializations, then the use of the class template is
1299 // ambiguous and the program is ill-formed.
1300 for (llvm::SmallVector<MatchResult, 4>::iterator P = Best + 1,
1301 PEnd = Matched.end();
1302 P != PEnd; ++P) {
John McCall5769d612010-02-08 23:07:23 +00001303 if (getMoreSpecializedPartialSpecialization(P->first, Best->first,
1304 PointOfInstantiation)
Douglas Gregored9c0f92009-10-29 00:04:11 +00001305 == P->first)
1306 Best = P;
Douglas Gregorbf4ea562009-09-15 16:23:51 +00001307 }
Douglas Gregorbf4ea562009-09-15 16:23:51 +00001308
Douglas Gregored9c0f92009-10-29 00:04:11 +00001309 // Determine if the best partial specialization is more specialized than
1310 // the others.
1311 bool Ambiguous = false;
Douglas Gregorbf4ea562009-09-15 16:23:51 +00001312 for (llvm::SmallVector<MatchResult, 4>::iterator P = Matched.begin(),
1313 PEnd = Matched.end();
Douglas Gregored9c0f92009-10-29 00:04:11 +00001314 P != PEnd; ++P) {
1315 if (P != Best &&
John McCall5769d612010-02-08 23:07:23 +00001316 getMoreSpecializedPartialSpecialization(P->first, Best->first,
1317 PointOfInstantiation)
Douglas Gregored9c0f92009-10-29 00:04:11 +00001318 != Best->first) {
1319 Ambiguous = true;
1320 break;
1321 }
1322 }
1323
1324 if (Ambiguous) {
1325 // Partial ordering did not produce a clear winner. Complain.
1326 ClassTemplateSpec->setInvalidDecl();
1327 Diag(PointOfInstantiation, diag::err_partial_spec_ordering_ambiguous)
1328 << ClassTemplateSpec;
1329
1330 // Print the matching partial specializations.
1331 for (llvm::SmallVector<MatchResult, 4>::iterator P = Matched.begin(),
1332 PEnd = Matched.end();
1333 P != PEnd; ++P)
1334 Diag(P->first->getLocation(), diag::note_partial_spec_match)
1335 << getTemplateArgumentBindingsText(P->first->getTemplateParameters(),
1336 *P->second);
Douglas Gregord6350ae2009-08-28 20:31:08 +00001337
Douglas Gregored9c0f92009-10-29 00:04:11 +00001338 return true;
1339 }
Douglas Gregorbf4ea562009-09-15 16:23:51 +00001340 }
1341
1342 // Instantiate using the best class template partial specialization.
Douglas Gregored9c0f92009-10-29 00:04:11 +00001343 ClassTemplatePartialSpecializationDecl *OrigPartialSpec = Best->first;
1344 while (OrigPartialSpec->getInstantiatedFromMember()) {
1345 // If we've found an explicit specialization of this class template,
1346 // stop here and use that as the pattern.
1347 if (OrigPartialSpec->isMemberSpecialization())
1348 break;
1349
1350 OrigPartialSpec = OrigPartialSpec->getInstantiatedFromMember();
1351 }
1352
1353 Pattern = OrigPartialSpec;
Douglas Gregorbf4ea562009-09-15 16:23:51 +00001354 ClassTemplateSpec->setInstantiationOf(Best->first, Best->second);
Douglas Gregorc1efb3f2009-06-12 22:31:52 +00001355 } else {
1356 // -- If no matches are found, the instantiation is generated
1357 // from the primary template.
Douglas Gregord6350ae2009-08-28 20:31:08 +00001358 ClassTemplateDecl *OrigTemplate = Template;
Douglas Gregorfd056bc2009-10-13 16:30:37 +00001359 while (OrigTemplate->getInstantiatedFromMemberTemplate()) {
1360 // If we've found an explicit specialization of this class template,
1361 // stop here and use that as the pattern.
1362 if (OrigTemplate->isMemberSpecialization())
1363 break;
1364
Douglas Gregord6350ae2009-08-28 20:31:08 +00001365 OrigTemplate = OrigTemplate->getInstantiatedFromMemberTemplate();
Douglas Gregorfd056bc2009-10-13 16:30:37 +00001366 }
1367
Douglas Gregord6350ae2009-08-28 20:31:08 +00001368 Pattern = OrigTemplate->getTemplatedDecl();
Douglas Gregorc8ab2562009-05-31 09:31:02 +00001369 }
Douglas Gregor2943aed2009-03-03 04:44:36 +00001370
Douglas Gregor972e6ce2009-10-27 06:26:26 +00001371 bool Result = InstantiateClass(PointOfInstantiation, ClassTemplateSpec,
1372 Pattern,
1373 getTemplateInstantiationArgs(ClassTemplateSpec),
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001374 TSK,
Douglas Gregor5842ba92009-08-24 15:23:48 +00001375 Complain);
Mike Stump1eb44332009-09-09 15:08:12 +00001376
Douglas Gregor199d9912009-06-05 00:53:49 +00001377 for (unsigned I = 0, N = Matched.size(); I != N; ++I) {
1378 // FIXME: Implement TemplateArgumentList::Destroy!
1379 // if (Matched[I].first != Pattern)
1380 // Matched[I].second->Destroy(Context);
1381 }
Mike Stump1eb44332009-09-09 15:08:12 +00001382
Douglas Gregor199d9912009-06-05 00:53:49 +00001383 return Result;
Douglas Gregor2943aed2009-03-03 04:44:36 +00001384}
Douglas Gregor5953d8b2009-03-19 17:26:29 +00001385
John McCallce3ff2b2009-08-25 22:02:44 +00001386/// \brief Instantiates the definitions of all of the member
1387/// of the given class, which is an instantiation of a class template
1388/// or a member class of a template.
Douglas Gregora58861f2009-05-13 20:28:22 +00001389void
1390Sema::InstantiateClassMembers(SourceLocation PointOfInstantiation,
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001391 CXXRecordDecl *Instantiation,
1392 const MultiLevelTemplateArgumentList &TemplateArgs,
1393 TemplateSpecializationKind TSK) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001394 for (DeclContext::decl_iterator D = Instantiation->decls_begin(),
1395 DEnd = Instantiation->decls_end();
Douglas Gregora58861f2009-05-13 20:28:22 +00001396 D != DEnd; ++D) {
Douglas Gregor0d035142009-10-27 18:42:08 +00001397 bool SuppressNew = false;
Douglas Gregora58861f2009-05-13 20:28:22 +00001398 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(*D)) {
Douglas Gregor0d035142009-10-27 18:42:08 +00001399 if (FunctionDecl *Pattern
1400 = Function->getInstantiatedFromMemberFunction()) {
1401 MemberSpecializationInfo *MSInfo
1402 = Function->getMemberSpecializationInfo();
1403 assert(MSInfo && "No member specialization information?");
Douglas Gregorc42b6522010-04-09 21:02:29 +00001404 if (MSInfo->getTemplateSpecializationKind()
1405 == TSK_ExplicitSpecialization)
1406 continue;
1407
Douglas Gregor0d035142009-10-27 18:42:08 +00001408 if (CheckSpecializationInstantiationRedecl(PointOfInstantiation, TSK,
1409 Function,
1410 MSInfo->getTemplateSpecializationKind(),
1411 MSInfo->getPointOfInstantiation(),
1412 SuppressNew) ||
1413 SuppressNew)
Douglas Gregorf6b11852009-10-08 15:14:33 +00001414 continue;
1415
Douglas Gregor0d035142009-10-27 18:42:08 +00001416 if (Function->getBody())
1417 continue;
1418
1419 if (TSK == TSK_ExplicitInstantiationDefinition) {
1420 // C++0x [temp.explicit]p8:
1421 // An explicit instantiation definition that names a class template
1422 // specialization explicitly instantiates the class template
1423 // specialization and is only an explicit instantiation definition
1424 // of members whose definition is visible at the point of
1425 // instantiation.
1426 if (!Pattern->getBody())
1427 continue;
1428
1429 Function->setTemplateSpecializationKind(TSK, PointOfInstantiation);
1430
1431 InstantiateFunctionDefinition(PointOfInstantiation, Function);
1432 } else {
1433 Function->setTemplateSpecializationKind(TSK, PointOfInstantiation);
1434 }
Douglas Gregorf6b11852009-10-08 15:14:33 +00001435 }
Douglas Gregora58861f2009-05-13 20:28:22 +00001436 } else if (VarDecl *Var = dyn_cast<VarDecl>(*D)) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001437 if (Var->isStaticDataMember()) {
Douglas Gregor0d035142009-10-27 18:42:08 +00001438 MemberSpecializationInfo *MSInfo = Var->getMemberSpecializationInfo();
1439 assert(MSInfo && "No member specialization information?");
Douglas Gregorc42b6522010-04-09 21:02:29 +00001440 if (MSInfo->getTemplateSpecializationKind()
1441 == TSK_ExplicitSpecialization)
1442 continue;
1443
Douglas Gregor0d035142009-10-27 18:42:08 +00001444 if (CheckSpecializationInstantiationRedecl(PointOfInstantiation, TSK,
1445 Var,
1446 MSInfo->getTemplateSpecializationKind(),
1447 MSInfo->getPointOfInstantiation(),
1448 SuppressNew) ||
1449 SuppressNew)
Douglas Gregorf6b11852009-10-08 15:14:33 +00001450 continue;
1451
Douglas Gregor0d035142009-10-27 18:42:08 +00001452 if (TSK == TSK_ExplicitInstantiationDefinition) {
1453 // C++0x [temp.explicit]p8:
1454 // An explicit instantiation definition that names a class template
1455 // specialization explicitly instantiates the class template
1456 // specialization and is only an explicit instantiation definition
1457 // of members whose definition is visible at the point of
1458 // instantiation.
1459 if (!Var->getInstantiatedFromStaticDataMember()
1460 ->getOutOfLineDefinition())
1461 continue;
1462
1463 Var->setTemplateSpecializationKind(TSK, PointOfInstantiation);
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001464 InstantiateStaticDataMemberDefinition(PointOfInstantiation, Var);
Douglas Gregor0d035142009-10-27 18:42:08 +00001465 } else {
1466 Var->setTemplateSpecializationKind(TSK, PointOfInstantiation);
1467 }
1468 }
Douglas Gregora58861f2009-05-13 20:28:22 +00001469 } else if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(*D)) {
Douglas Gregora77eaa92010-04-18 18:11:38 +00001470 // Always skip the injected-class-name, along with any
1471 // redeclarations of nested classes, since both would cause us
1472 // to try to instantiate the members of a class twice.
1473 if (Record->isInjectedClassName() || Record->getPreviousDeclaration())
Douglas Gregor2db32322009-10-07 23:56:10 +00001474 continue;
1475
Douglas Gregor0d035142009-10-27 18:42:08 +00001476 MemberSpecializationInfo *MSInfo = Record->getMemberSpecializationInfo();
1477 assert(MSInfo && "No member specialization information?");
Douglas Gregorc42b6522010-04-09 21:02:29 +00001478
1479 if (MSInfo->getTemplateSpecializationKind()
1480 == TSK_ExplicitSpecialization)
1481 continue;
1482
Douglas Gregor0d035142009-10-27 18:42:08 +00001483 if (CheckSpecializationInstantiationRedecl(PointOfInstantiation, TSK,
1484 Record,
1485 MSInfo->getTemplateSpecializationKind(),
1486 MSInfo->getPointOfInstantiation(),
1487 SuppressNew) ||
1488 SuppressNew)
Douglas Gregorf6b11852009-10-08 15:14:33 +00001489 continue;
1490
Douglas Gregor0d035142009-10-27 18:42:08 +00001491 CXXRecordDecl *Pattern = Record->getInstantiatedFromMemberClass();
1492 assert(Pattern && "Missing instantiated-from-template information");
1493
Douglas Gregor952b0172010-02-11 01:04:33 +00001494 if (!Record->getDefinition()) {
1495 if (!Pattern->getDefinition()) {
Douglas Gregor0d035142009-10-27 18:42:08 +00001496 // C++0x [temp.explicit]p8:
1497 // An explicit instantiation definition that names a class template
1498 // specialization explicitly instantiates the class template
1499 // specialization and is only an explicit instantiation definition
1500 // of members whose definition is visible at the point of
1501 // instantiation.
1502 if (TSK == TSK_ExplicitInstantiationDeclaration) {
1503 MSInfo->setTemplateSpecializationKind(TSK);
1504 MSInfo->setPointOfInstantiation(PointOfInstantiation);
1505 }
1506
1507 continue;
1508 }
1509
1510 InstantiateClass(PointOfInstantiation, Record, Pattern,
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001511 TemplateArgs,
1512 TSK);
Douglas Gregor0d035142009-10-27 18:42:08 +00001513 }
Douglas Gregore9374d52009-10-08 01:19:17 +00001514
Douglas Gregor952b0172010-02-11 01:04:33 +00001515 Pattern = cast_or_null<CXXRecordDecl>(Record->getDefinition());
Douglas Gregor0d035142009-10-27 18:42:08 +00001516 if (Pattern)
1517 InstantiateClassMembers(PointOfInstantiation, Pattern, TemplateArgs,
1518 TSK);
Douglas Gregora58861f2009-05-13 20:28:22 +00001519 }
1520 }
1521}
1522
1523/// \brief Instantiate the definitions of all of the members of the
1524/// given class template specialization, which was named as part of an
1525/// explicit instantiation.
Mike Stump1eb44332009-09-09 15:08:12 +00001526void
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001527Sema::InstantiateClassTemplateSpecializationMembers(
Douglas Gregora58861f2009-05-13 20:28:22 +00001528 SourceLocation PointOfInstantiation,
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001529 ClassTemplateSpecializationDecl *ClassTemplateSpec,
1530 TemplateSpecializationKind TSK) {
Douglas Gregora58861f2009-05-13 20:28:22 +00001531 // C++0x [temp.explicit]p7:
1532 // An explicit instantiation that names a class template
1533 // specialization is an explicit instantion of the same kind
1534 // (declaration or definition) of each of its members (not
1535 // including members inherited from base classes) that has not
1536 // been previously explicitly specialized in the translation unit
1537 // containing the explicit instantiation, except as described
1538 // below.
1539 InstantiateClassMembers(PointOfInstantiation, ClassTemplateSpec,
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001540 getTemplateInstantiationArgs(ClassTemplateSpec),
1541 TSK);
Douglas Gregora58861f2009-05-13 20:28:22 +00001542}
1543
Mike Stump1eb44332009-09-09 15:08:12 +00001544Sema::OwningStmtResult
Douglas Gregord6350ae2009-08-28 20:31:08 +00001545Sema::SubstStmt(Stmt *S, const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor43959a92009-08-20 07:17:43 +00001546 if (!S)
1547 return Owned(S);
1548
1549 TemplateInstantiator Instantiator(*this, TemplateArgs,
1550 SourceLocation(),
1551 DeclarationName());
1552 return Instantiator.TransformStmt(S);
1553}
1554
Mike Stump1eb44332009-09-09 15:08:12 +00001555Sema::OwningExprResult
Douglas Gregord6350ae2009-08-28 20:31:08 +00001556Sema::SubstExpr(Expr *E, const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregorb98b1992009-08-11 05:31:07 +00001557 if (!E)
1558 return Owned(E);
Mike Stump1eb44332009-09-09 15:08:12 +00001559
Douglas Gregorb98b1992009-08-11 05:31:07 +00001560 TemplateInstantiator Instantiator(*this, TemplateArgs,
1561 SourceLocation(),
1562 DeclarationName());
1563 return Instantiator.TransformExpr(E);
1564}
1565
John McCallce3ff2b2009-08-25 22:02:44 +00001566/// \brief Do template substitution on a nested-name-specifier.
Douglas Gregorab452ba2009-03-26 23:50:42 +00001567NestedNameSpecifier *
John McCallce3ff2b2009-08-25 22:02:44 +00001568Sema::SubstNestedNameSpecifier(NestedNameSpecifier *NNS,
Douglas Gregord6350ae2009-08-28 20:31:08 +00001569 SourceRange Range,
1570 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregordcee1a12009-08-06 05:28:30 +00001571 TemplateInstantiator Instantiator(*this, TemplateArgs, Range.getBegin(),
1572 DeclarationName());
Douglas Gregoredc90502010-02-25 04:46:04 +00001573 return Instantiator.TransformNestedNameSpecifier(NNS, Range);
Douglas Gregor5953d8b2009-03-19 17:26:29 +00001574}
Douglas Gregorde650ae2009-03-31 18:38:02 +00001575
1576TemplateName
John McCallce3ff2b2009-08-25 22:02:44 +00001577Sema::SubstTemplateName(TemplateName Name, SourceLocation Loc,
Douglas Gregord6350ae2009-08-28 20:31:08 +00001578 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregord1067e52009-08-06 06:41:21 +00001579 TemplateInstantiator Instantiator(*this, TemplateArgs, Loc,
1580 DeclarationName());
1581 return Instantiator.TransformTemplateName(Name);
Douglas Gregorde650ae2009-03-31 18:38:02 +00001582}
Douglas Gregor91333002009-06-11 00:06:24 +00001583
John McCall833ca992009-10-29 08:12:44 +00001584bool Sema::Subst(const TemplateArgumentLoc &Input, TemplateArgumentLoc &Output,
1585 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor670444e2009-08-04 22:27:00 +00001586 TemplateInstantiator Instantiator(*this, TemplateArgs, SourceLocation(),
1587 DeclarationName());
John McCall833ca992009-10-29 08:12:44 +00001588
1589 return Instantiator.TransformTemplateArgument(Input, Output);
Douglas Gregor91333002009-06-11 00:06:24 +00001590}