blob: e727cdbc9e87db2d69f692ff0770c681d6676bd2 [file] [log] [blame]
Douglas Gregorfe1e1102009-02-27 19:31:52 +00001//===------- SemaTemplateInstantiate.cpp - C++ Template Instantiation ------===/
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//===----------------------------------------------------------------------===/
8//
9// This file implements C++ template instantiation.
10//
11//===----------------------------------------------------------------------===/
12
13#include "Sema.h"
Douglas Gregord6ff3322009-08-04 16:50:30 +000014#include "TreeTransform.h"
John McCallb53bbd42009-11-22 01:44:31 +000015#include "Lookup.h"
Douglas Gregor28ad4b52009-05-26 20:50:29 +000016#include "clang/AST/ASTConsumer.h"
Douglas Gregorfe1e1102009-02-27 19:31:52 +000017#include "clang/AST/ASTContext.h"
18#include "clang/AST/Expr.h"
Douglas Gregorfe1e1102009-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 Gregor4ea568f2009-03-10 18:03:33 +000025//===----------------------------------------------------------------------===/
26// Template Instantiation Support
27//===----------------------------------------------------------------------===/
28
Douglas Gregor01afeef2009-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 Gregor36d7c5f2009-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 Gregor8c702532010-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 Gregora654dd82009-08-28 17:37:35 +000041MultiLevelTemplateArgumentList
Douglas Gregor36d7c5f2009-11-09 19:17:50 +000042Sema::getTemplateInstantiationArgs(NamedDecl *D,
Douglas Gregor8c702532010-02-05 07:33:43 +000043 const TemplateArgumentList *Innermost,
44 bool RelativeToPrimary) {
Douglas Gregora654dd82009-08-28 17:37:35 +000045 // Accumulate the set of template argument lists in this structure.
46 MultiLevelTemplateArgumentList Result;
Mike Stump11289f42009-09-09 15:08:12 +000047
Douglas Gregor36d7c5f2009-11-09 19:17:50 +000048 if (Innermost)
49 Result.addOuterTemplateArguments(Innermost);
50
Douglas Gregora654dd82009-08-28 17:37:35 +000051 DeclContext *Ctx = dyn_cast<DeclContext>(D);
52 if (!Ctx)
53 Ctx = D->getDeclContext();
Mike Stump11289f42009-09-09 15:08:12 +000054
John McCall970d5302009-08-29 03:16:09 +000055 while (!Ctx->isFileContext()) {
Douglas Gregora654dd82009-08-28 17:37:35 +000056 // Add template arguments from a class template instantiation.
Mike Stump11289f42009-09-09 15:08:12 +000057 if (ClassTemplateSpecializationDecl *Spec
Douglas Gregora654dd82009-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 Stump11289f42009-09-09 15:08:12 +000062
Douglas Gregora654dd82009-08-28 17:37:35 +000063 Result.addOuterTemplateArguments(&Spec->getTemplateInstantiationArgs());
Douglas Gregorcf915552009-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 Stump11289f42009-09-09 15:08:12 +000070 }
Douglas Gregora654dd82009-08-28 17:37:35 +000071 // Add template arguments from a function template specialization.
John McCall970d5302009-08-29 03:16:09 +000072 else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Ctx)) {
Douglas Gregor8c702532010-02-05 07:33:43 +000073 if (!RelativeToPrimary &&
74 Function->getTemplateSpecializationKind()
75 == TSK_ExplicitSpecialization)
Douglas Gregorcf915552009-10-13 16:30:37 +000076 break;
77
Douglas Gregora654dd82009-08-28 17:37:35 +000078 if (const TemplateArgumentList *TemplateArgs
Douglas Gregorcf915552009-10-13 16:30:37 +000079 = Function->getTemplateSpecializationArgs()) {
80 // Add the template arguments for this specialization.
Douglas Gregora654dd82009-08-28 17:37:35 +000081 Result.addOuterTemplateArguments(TemplateArgs);
John McCall970d5302009-08-29 03:16:09 +000082
Douglas Gregorcf915552009-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 McCall970d5302009-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 Gregor8c702532010-02-05 07:33:43 +000096 RelativeToPrimary = false;
John McCall970d5302009-08-29 03:16:09 +000097 continue;
98 }
Douglas Gregora654dd82009-08-28 17:37:35 +000099 }
John McCall970d5302009-08-29 03:16:09 +0000100
101 Ctx = Ctx->getParent();
Douglas Gregor8c702532010-02-05 07:33:43 +0000102 RelativeToPrimary = false;
Douglas Gregorb4850462009-05-14 23:26:13 +0000103 }
Mike Stump11289f42009-09-09 15:08:12 +0000104
Douglas Gregora654dd82009-08-28 17:37:35 +0000105 return Result;
Douglas Gregorb4850462009-05-14 23:26:13 +0000106}
107
Douglas Gregor84d49a22009-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 Gregorfcd5db32009-03-10 00:06:19 +0000125Sema::InstantiatingTemplate::
126InstantiatingTemplate(Sema &SemaRef, SourceLocation PointOfInstantiation,
Douglas Gregor85673582009-05-18 17:01:57 +0000127 Decl *Entity,
Douglas Gregorfcd5db32009-03-10 00:06:19 +0000128 SourceRange InstantiationRange)
129 : SemaRef(SemaRef) {
Douglas Gregor79cf6032009-03-10 20:44:00 +0000130
131 Invalid = CheckInstantiationDepth(PointOfInstantiation,
132 InstantiationRange);
133 if (!Invalid) {
Douglas Gregorfcd5db32009-03-10 00:06:19 +0000134 ActiveTemplateInstantiation Inst;
Douglas Gregor79cf6032009-03-10 20:44:00 +0000135 Inst.Kind = ActiveTemplateInstantiation::TemplateInstantiation;
Douglas Gregorfcd5db32009-03-10 00:06:19 +0000136 Inst.PointOfInstantiation = PointOfInstantiation;
Douglas Gregor79cf6032009-03-10 20:44:00 +0000137 Inst.Entity = reinterpret_cast<uintptr_t>(Entity);
Douglas Gregorc9220832009-03-12 18:36:18 +0000138 Inst.TemplateArgs = 0;
139 Inst.NumTemplateArgs = 0;
Douglas Gregor79cf6032009-03-10 20:44:00 +0000140 Inst.InstantiationRange = InstantiationRange;
141 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
Douglas Gregor79cf6032009-03-10 20:44:00 +0000142 }
143}
144
Mike Stump11289f42009-09-09 15:08:12 +0000145Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef,
Douglas Gregor79cf6032009-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 Stump11289f42009-09-09 15:08:12 +0000157 Inst.Kind
Douglas Gregor79cf6032009-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 Gregorfcd5db32009-03-10 00:06:19 +0000163 Inst.InstantiationRange = InstantiationRange;
164 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
Douglas Gregorfcd5db32009-03-10 00:06:19 +0000165 }
166}
167
Mike Stump11289f42009-09-09 15:08:12 +0000168Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef,
Douglas Gregor637d9982009-06-10 23:47:09 +0000169 SourceLocation PointOfInstantiation,
Douglas Gregorff6cbdf2009-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 Stump11289f42009-09-09 15:08:12 +0000176
Douglas Gregorff6cbdf2009-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 Gregor84d49a22009-11-11 21:54:23 +0000188
189 if (!Inst.isInstantiationRecord())
190 ++SemaRef.NonInstantiationEntries;
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000191 }
192}
193
Mike Stump11289f42009-09-09 15:08:12 +0000194Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef,
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000195 SourceLocation PointOfInstantiation,
Douglas Gregor637d9982009-06-10 23:47:09 +0000196 ClassTemplatePartialSpecializationDecl *PartialSpec,
197 const TemplateArgument *TemplateArgs,
198 unsigned NumTemplateArgs,
199 SourceRange InstantiationRange)
200 : SemaRef(SemaRef) {
201
Douglas Gregor84d49a22009-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 Gregor637d9982009-06-10 23:47:09 +0000215}
216
Mike Stump11289f42009-09-09 15:08:12 +0000217Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef,
Douglas Gregore62e6a02009-11-11 19:13:48 +0000218 SourceLocation PointOfInstantiation,
Anders Carlsson657bad42009-09-05 05:14:19 +0000219 ParmVarDecl *Param,
220 const TemplateArgument *TemplateArgs,
221 unsigned NumTemplateArgs,
222 SourceRange InstantiationRange)
223 : SemaRef(SemaRef) {
Mike Stump11289f42009-09-09 15:08:12 +0000224
Douglas Gregore62e6a02009-11-11 19:13:48 +0000225 Invalid = CheckInstantiationDepth(PointOfInstantiation, InstantiationRange);
Anders Carlsson657bad42009-09-05 05:14:19 +0000226
227 if (!Invalid) {
228 ActiveTemplateInstantiation Inst;
229 Inst.Kind
230 = ActiveTemplateInstantiation::DefaultFunctionArgumentInstantiation;
Douglas Gregore62e6a02009-11-11 19:13:48 +0000231 Inst.PointOfInstantiation = PointOfInstantiation;
Anders Carlsson657bad42009-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 Gregore62e6a02009-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 Gregor84d49a22009-11-11 21:54:23 +0000247 Invalid = false;
Douglas Gregore62e6a02009-11-11 19:13:48 +0000248
Douglas Gregor84d49a22009-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 Gregore62e6a02009-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 Gregor84d49a22009-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 Gregore62e6a02009-11-11 19:13:48 +0000280
Douglas Gregor84d49a22009-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 Carlsson657bad42009-09-05 05:14:19 +0000306}
307
Douglas Gregor85673582009-05-18 17:01:57 +0000308void Sema::InstantiatingTemplate::Clear() {
309 if (!Invalid) {
Douglas Gregor84d49a22009-11-11 21:54:23 +0000310 if (!SemaRef.ActiveTemplateInstantiations.back().isInstantiationRecord()) {
311 assert(SemaRef.NonInstantiationEntries > 0);
312 --SemaRef.NonInstantiationEntries;
313 }
314
Douglas Gregorfcd5db32009-03-10 00:06:19 +0000315 SemaRef.ActiveTemplateInstantiations.pop_back();
Douglas Gregor85673582009-05-18 17:01:57 +0000316 Invalid = true;
317 }
Douglas Gregorfcd5db32009-03-10 00:06:19 +0000318}
319
Douglas Gregor79cf6032009-03-10 20:44:00 +0000320bool Sema::InstantiatingTemplate::CheckInstantiationDepth(
321 SourceLocation PointOfInstantiation,
322 SourceRange InstantiationRange) {
Douglas Gregor84d49a22009-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 Gregor79cf6032009-03-10 20:44:00 +0000328 return false;
329
Mike Stump11289f42009-09-09 15:08:12 +0000330 SemaRef.Diag(PointOfInstantiation,
Douglas Gregor79cf6032009-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 Gregor4ea568f2009-03-10 18:03:33 +0000339/// \brief Prints the current instantiation stack through a series of
340/// notes.
341void Sema::PrintInstantiationStack() {
Douglas Gregorffed1cb2010-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 Gregorff6cbdf2009-07-01 22:01:06 +0000350 // FIXME: In all of these cases, we need to show the template arguments
Douglas Gregorffed1cb2010-04-20 07:18:24 +0000351 unsigned InstantiationIdx = 0;
Douglas Gregor4ea568f2009-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 Gregorffed1cb2010-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 Gregor79cf6032009-03-10 20:44:00 +0000368 switch (Active->Kind) {
369 case ActiveTemplateInstantiation::TemplateInstantiation: {
Douglas Gregor85673582009-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 Stump11289f42009-09-09 15:08:12 +0000375 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
Douglas Gregor85673582009-05-18 17:01:57 +0000376 DiagID)
377 << Context.getTypeDeclType(Record)
378 << Active->InstantiationRange;
Douglas Gregora6ef8f02009-07-24 20:34:43 +0000379 } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
Douglas Gregor4adbc6d2009-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 Stump11289f42009-09-09 15:08:12 +0000385 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
Douglas Gregor85673582009-05-18 17:01:57 +0000386 DiagID)
387 << Function
388 << Active->InstantiationRange;
Douglas Gregora6ef8f02009-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 Gregor85673582009-05-18 17:01:57 +0000394 }
Douglas Gregor79cf6032009-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 Gregordc572a32009-03-30 22:58:21 +0000401 = TemplateSpecializationType::PrintTemplateArgumentList(
Mike Stump11289f42009-09-09 15:08:12 +0000402 Active->TemplateArgs,
Douglas Gregor7de59662009-05-29 20:38:28 +0000403 Active->NumTemplateArgs,
404 Context.PrintingPolicy);
Douglas Gregor79cf6032009-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 Gregor637d9982009-06-10 23:47:09 +0000411
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000412 case ActiveTemplateInstantiation::ExplicitTemplateArgumentSubstitution: {
Mike Stump11289f42009-09-09 15:08:12 +0000413 FunctionTemplateDecl *FnTmpl
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000414 = cast<FunctionTemplateDecl>((Decl *)Active->Entity);
Douglas Gregor637d9982009-06-10 23:47:09 +0000415 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000416 diag::note_explicit_template_arg_substitution_here)
Douglas Gregor607f1412010-03-30 20:35:20 +0000417 << FnTmpl
418 << getTemplateArgumentBindingsText(FnTmpl->getTemplateParameters(),
419 Active->TemplateArgs,
420 Active->NumTemplateArgs)
421 << Active->InstantiationRange;
Douglas Gregor637d9982009-06-10 23:47:09 +0000422 break;
423 }
Mike Stump11289f42009-09-09 15:08:12 +0000424
Douglas Gregorff6cbdf2009-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 Gregor607f1412010-03-30 20:35:20 +0000432 << getTemplateArgumentBindingsText(
433 PartialSpec->getTemplateParameters(),
434 Active->TemplateArgs,
435 Active->NumTemplateArgs)
Douglas Gregorff6cbdf2009-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 Gregor607f1412010-03-30 20:35:20 +0000442 << FnTmpl
443 << getTemplateArgumentBindingsText(FnTmpl->getTemplateParameters(),
444 Active->TemplateArgs,
445 Active->NumTemplateArgs)
446 << Active->InstantiationRange;
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000447 }
448 break;
Douglas Gregor637d9982009-06-10 23:47:09 +0000449
Anders Carlsson657bad42009-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 Stump11289f42009-09-09 15:08:12 +0000453
Anders Carlsson657bad42009-09-05 05:14:19 +0000454 std::string TemplateArgsStr
455 = TemplateSpecializationType::PrintTemplateArgumentList(
Mike Stump11289f42009-09-09 15:08:12 +0000456 Active->TemplateArgs,
Anders Carlsson657bad42009-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 Carlssondc6d2c32009-09-05 05:38:54 +0000461 << (FD->getNameAsString() + TemplateArgsStr)
Anders Carlsson657bad42009-09-05 05:14:19 +0000462 << Active->InstantiationRange;
463 break;
464 }
Mike Stump11289f42009-09-09 15:08:12 +0000465
Douglas Gregore62e6a02009-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 Gregor84d49a22009-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 Gregor79cf6032009-03-10 20:44:00 +0000494 }
Douglas Gregor4ea568f2009-03-10 18:03:33 +0000495 }
496}
497
Douglas Gregor33834512009-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 Gregor84d49a22009-11-11 21:54:23 +0000504 ++Active)
505 {
Douglas Gregor33834512009-06-14 07:33:30 +0000506 switch(Active->Kind) {
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000507 case ActiveTemplateInstantiation::TemplateInstantiation:
Anders Carlsson657bad42009-09-05 05:14:19 +0000508 case ActiveTemplateInstantiation::DefaultFunctionArgumentInstantiation:
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000509 // This is a template instantiation, so there is no SFINAE.
510 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000511
Douglas Gregor33834512009-06-14 07:33:30 +0000512 case ActiveTemplateInstantiation::DefaultTemplateArgumentInstantiation:
Douglas Gregore62e6a02009-11-11 19:13:48 +0000513 case ActiveTemplateInstantiation::PriorTemplateArgumentSubstitution:
Douglas Gregor84d49a22009-11-11 21:54:23 +0000514 case ActiveTemplateInstantiation::DefaultTemplateArgumentChecking:
Douglas Gregore62e6a02009-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 Gregor33834512009-06-14 07:33:30 +0000518 break;
Mike Stump11289f42009-09-09 15:08:12 +0000519
Douglas Gregorff6cbdf2009-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 Gregor33834512009-06-14 07:33:30 +0000525 }
526 }
527
528 return false;
529}
530
Douglas Gregorfe1e1102009-02-27 19:31:52 +0000531//===----------------------------------------------------------------------===/
532// Template Instantiation for Types
533//===----------------------------------------------------------------------===/
Douglas Gregor17c0d7b2009-02-28 00:25:32 +0000534namespace {
Douglas Gregor14cf7522010-04-30 18:55:50 +0000535 class TemplateInstantiator : public TreeTransform<TemplateInstantiator> {
Douglas Gregor01afeef2009-08-28 20:31:08 +0000536 const MultiLevelTemplateArgumentList &TemplateArgs;
Douglas Gregor17c0d7b2009-02-28 00:25:32 +0000537 SourceLocation Loc;
538 DeclarationName Entity;
Douglas Gregorfe1e1102009-02-27 19:31:52 +0000539
Douglas Gregor17c0d7b2009-02-28 00:25:32 +0000540 public:
Douglas Gregorebe10102009-08-20 07:17:43 +0000541 typedef TreeTransform<TemplateInstantiator> inherited;
Mike Stump11289f42009-09-09 15:08:12 +0000542
543 TemplateInstantiator(Sema &SemaRef,
Douglas Gregor01afeef2009-08-28 20:31:08 +0000544 const MultiLevelTemplateArgumentList &TemplateArgs,
Douglas Gregord6ff3322009-08-04 16:50:30 +0000545 SourceLocation Loc,
Mike Stump11289f42009-09-09 15:08:12 +0000546 DeclarationName Entity)
547 : inherited(SemaRef), TemplateArgs(TemplateArgs), Loc(Loc),
Douglas Gregorebe10102009-08-20 07:17:43 +0000548 Entity(Entity) { }
Douglas Gregor17c0d7b2009-02-28 00:25:32 +0000549
Mike Stump11289f42009-09-09 15:08:12 +0000550 /// \brief Determine whether the given type \p T has already been
Douglas Gregord6ff3322009-08-04 16:50:30 +0000551 /// transformed.
552 ///
553 /// For the purposes of template instantiation, a type has already been
554 /// transformed if it is NULL or if it is not dependent.
555 bool AlreadyTransformed(QualType T) {
556 return T.isNull() || !T->isDependentType();
Douglas Gregorf61eca92009-05-13 18:28:20 +0000557 }
Mike Stump11289f42009-09-09 15:08:12 +0000558
Douglas Gregord6ff3322009-08-04 16:50:30 +0000559 /// \brief Returns the location of the entity being instantiated, if known.
560 SourceLocation getBaseLocation() { return Loc; }
Mike Stump11289f42009-09-09 15:08:12 +0000561
Douglas Gregord6ff3322009-08-04 16:50:30 +0000562 /// \brief Returns the name of the entity being instantiated, if any.
563 DeclarationName getBaseEntity() { return Entity; }
Mike Stump11289f42009-09-09 15:08:12 +0000564
Douglas Gregoref6ab412009-10-27 06:26:26 +0000565 /// \brief Sets the "base" location and entity when that
566 /// information is known based on another transformation.
567 void setBase(SourceLocation Loc, DeclarationName Entity) {
568 this->Loc = Loc;
569 this->Entity = Entity;
570 }
571
Douglas Gregord6ff3322009-08-04 16:50:30 +0000572 /// \brief Transform the given declaration by instantiating a reference to
573 /// this declaration.
Douglas Gregora04f2ca2010-03-01 15:56:25 +0000574 Decl *TransformDecl(SourceLocation Loc, Decl *D);
Douglas Gregora16548e2009-08-11 05:31:07 +0000575
Mike Stump11289f42009-09-09 15:08:12 +0000576 /// \brief Transform the definition of the given declaration by
Douglas Gregorebe10102009-08-20 07:17:43 +0000577 /// instantiating it.
Douglas Gregor25289362010-03-01 17:25:41 +0000578 Decl *TransformDefinition(SourceLocation Loc, Decl *D);
Mike Stump11289f42009-09-09 15:08:12 +0000579
Douglas Gregora5cb6da2009-10-20 05:58:46 +0000580 /// \bried Transform the first qualifier within a scope by instantiating the
581 /// declaration.
582 NamedDecl *TransformFirstQualifierInScope(NamedDecl *D, SourceLocation Loc);
583
Douglas Gregorebe10102009-08-20 07:17:43 +0000584 /// \brief Rebuild the exception declaration and register the declaration
585 /// as an instantiated local.
Mike Stump11289f42009-09-09 15:08:12 +0000586 VarDecl *RebuildExceptionDecl(VarDecl *ExceptionDecl, QualType T,
John McCallbcd03502009-12-07 02:54:59 +0000587 TypeSourceInfo *Declarator,
Douglas Gregorebe10102009-08-20 07:17:43 +0000588 IdentifierInfo *Name,
589 SourceLocation Loc, SourceRange TypeRange);
Mike Stump11289f42009-09-09 15:08:12 +0000590
Douglas Gregorf4e837f2010-04-26 17:57:08 +0000591 /// \brief Rebuild the Objective-C exception declaration and register the
592 /// declaration as an instantiated local.
593 VarDecl *RebuildObjCExceptionDecl(VarDecl *ExceptionDecl,
594 TypeSourceInfo *TSInfo, QualType T);
595
John McCall7f41d982009-09-11 04:59:25 +0000596 /// \brief Check for tag mismatches when instantiating an
597 /// elaborated type.
598 QualType RebuildElaboratedType(QualType T, ElaboratedType::TagKind Tag);
599
John McCall47f29ea2009-12-08 09:21:05 +0000600 Sema::OwningExprResult TransformPredefinedExpr(PredefinedExpr *E);
601 Sema::OwningExprResult TransformDeclRefExpr(DeclRefExpr *E);
John McCall47f29ea2009-12-08 09:21:05 +0000602 Sema::OwningExprResult TransformCXXDefaultArgExpr(CXXDefaultArgExpr *E);
John McCall13481c52010-02-06 08:42:39 +0000603 Sema::OwningExprResult TransformTemplateParmRefExpr(DeclRefExpr *E,
604 NonTypeTemplateParmDecl *D);
Sebastian Redl14236c82009-11-08 13:56:19 +0000605
Douglas Gregor14cf7522010-04-30 18:55:50 +0000606 QualType TransformFunctionProtoType(TypeLocBuilder &TLB,
607 FunctionProtoTypeLoc TL,
608 QualType ObjectType);
John McCall58f10c32010-03-11 09:03:00 +0000609 ParmVarDecl *TransformFunctionTypeParam(ParmVarDecl *OldParm);
610
Mike Stump11289f42009-09-09 15:08:12 +0000611 /// \brief Transforms a template type parameter type by performing
Douglas Gregord6ff3322009-08-04 16:50:30 +0000612 /// substitution of the corresponding template type argument.
John McCall550e0c22009-10-21 00:40:46 +0000613 QualType TransformTemplateTypeParmType(TypeLocBuilder &TLB,
Douglas Gregorfe17d252010-02-16 19:09:40 +0000614 TemplateTypeParmTypeLoc TL,
615 QualType ObjectType);
Douglas Gregord6ff3322009-08-04 16:50:30 +0000616 };
Douglas Gregor04318252009-07-06 15:59:29 +0000617}
618
Douglas Gregora04f2ca2010-03-01 15:56:25 +0000619Decl *TemplateInstantiator::TransformDecl(SourceLocation Loc, Decl *D) {
Douglas Gregor2b6ca462009-09-03 21:38:09 +0000620 if (!D)
621 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000622
Douglas Gregor2b6ca462009-09-03 21:38:09 +0000623 if (TemplateTemplateParmDecl *TTP = dyn_cast<TemplateTemplateParmDecl>(D)) {
Douglas Gregor01afeef2009-08-28 20:31:08 +0000624 if (TTP->getDepth() < TemplateArgs.getNumLevels()) {
Douglas Gregorb93971082010-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 Gregor9167f8b2009-11-11 01:00:40 +0000633 TemplateName Template
634 = TemplateArgs(TTP->getDepth(), TTP->getPosition()).getAsTemplate();
635 assert(!Template.isNull() && Template.getAsTemplateDecl() &&
Douglas Gregor01afeef2009-08-28 20:31:08 +0000636 "Wrong kind of template template argument");
Douglas Gregor9167f8b2009-11-11 01:00:40 +0000637 return Template.getAsTemplateDecl();
Douglas Gregor01afeef2009-08-28 20:31:08 +0000638 }
Mike Stump11289f42009-09-09 15:08:12 +0000639
Douglas Gregor9167f8b2009-11-11 01:00:40 +0000640 // Fall through to find the instantiated declaration for this template
641 // template parameter.
Douglas Gregor71dc5092009-08-06 06:41:21 +0000642 }
Mike Stump11289f42009-09-09 15:08:12 +0000643
Douglas Gregora04f2ca2010-03-01 15:56:25 +0000644 return SemaRef.FindInstantiatedDecl(Loc, cast<NamedDecl>(D), TemplateArgs);
Douglas Gregord6ff3322009-08-04 16:50:30 +0000645}
646
Douglas Gregor25289362010-03-01 17:25:41 +0000647Decl *TemplateInstantiator::TransformDefinition(SourceLocation Loc, Decl *D) {
John McCall76d824f2009-08-25 22:02:44 +0000648 Decl *Inst = getSema().SubstDecl(D, getSema().CurContext, TemplateArgs);
Douglas Gregorebe10102009-08-20 07:17:43 +0000649 if (!Inst)
650 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000651
Douglas Gregorebe10102009-08-20 07:17:43 +0000652 getSema().CurrentInstantiationScope->InstantiatedLocal(D, Inst);
653 return Inst;
654}
655
Douglas Gregora5cb6da2009-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 Gregora04f2ca2010-03-01 15:56:25 +0000667 return cast_or_null<NamedDecl>(TransformDecl(Loc, D));
Douglas Gregora5cb6da2009-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 Gregora04f2ca2010-03-01 15:56:25 +0000678 return cast_or_null<NamedDecl>(TransformDecl(Loc, D));
Douglas Gregora5cb6da2009-10-20 05:58:46 +0000679}
680
Douglas Gregorebe10102009-08-20 07:17:43 +0000681VarDecl *
682TemplateInstantiator::RebuildExceptionDecl(VarDecl *ExceptionDecl,
Mike Stump11289f42009-09-09 15:08:12 +0000683 QualType T,
John McCallbcd03502009-12-07 02:54:59 +0000684 TypeSourceInfo *Declarator,
Douglas Gregorebe10102009-08-20 07:17:43 +0000685 IdentifierInfo *Name,
Mike Stump11289f42009-09-09 15:08:12 +0000686 SourceLocation Loc,
Douglas Gregorebe10102009-08-20 07:17:43 +0000687 SourceRange TypeRange) {
688 VarDecl *Var = inherited::RebuildExceptionDecl(ExceptionDecl, T, Declarator,
689 Name, Loc, TypeRange);
Douglas Gregorf4e837f2010-04-26 17:57:08 +0000690 if (Var)
691 getSema().CurrentInstantiationScope->InstantiatedLocal(ExceptionDecl, Var);
692 return Var;
693}
694
695VarDecl *TemplateInstantiator::RebuildObjCExceptionDecl(VarDecl *ExceptionDecl,
696 TypeSourceInfo *TSInfo,
697 QualType T) {
698 VarDecl *Var = inherited::RebuildObjCExceptionDecl(ExceptionDecl, TSInfo, T);
699 if (Var)
Douglas Gregorebe10102009-08-20 07:17:43 +0000700 getSema().CurrentInstantiationScope->InstantiatedLocal(ExceptionDecl, Var);
701 return Var;
702}
703
John McCall7f41d982009-09-11 04:59:25 +0000704QualType
705TemplateInstantiator::RebuildElaboratedType(QualType T,
706 ElaboratedType::TagKind Tag) {
707 if (const TagType *TT = T->getAs<TagType>()) {
708 TagDecl* TD = TT->getDecl();
709
710 // FIXME: this location is very wrong; we really need typelocs.
711 SourceLocation TagLocation = TD->getTagKeywordLoc();
712
713 // FIXME: type might be anonymous.
714 IdentifierInfo *Id = TD->getIdentifier();
715
716 // TODO: should we even warn on struct/class mismatches for this? Seems
717 // like it's likely to produce a lot of spurious errors.
718 if (!SemaRef.isAcceptableTagRedeclaration(TD, Tag, TagLocation, *Id)) {
719 SemaRef.Diag(TagLocation, diag::err_use_with_wrong_tag)
720 << Id
Douglas Gregora771f462010-03-31 17:46:05 +0000721 << FixItHint::CreateReplacement(SourceRange(TagLocation),
722 TD->getKindName());
John McCall7f41d982009-09-11 04:59:25 +0000723 SemaRef.Diag(TD->getLocation(), diag::note_previous_use);
724 }
725 }
726
727 return TreeTransform<TemplateInstantiator>::RebuildElaboratedType(T, Tag);
728}
729
730Sema::OwningExprResult
John McCall47f29ea2009-12-08 09:21:05 +0000731TemplateInstantiator::TransformPredefinedExpr(PredefinedExpr *E) {
Anders Carlsson0b209a82009-09-11 01:22:35 +0000732 if (!E->isTypeDependent())
733 return SemaRef.Owned(E->Retain());
734
735 FunctionDecl *currentDecl = getSema().getCurFunctionDecl();
736 assert(currentDecl && "Must have current function declaration when "
737 "instantiating.");
738
739 PredefinedExpr::IdentType IT = E->getIdentType();
740
Anders Carlsson5bd8d192010-02-11 18:20:28 +0000741 unsigned Length = PredefinedExpr::ComputeName(IT, currentDecl).length();
Anders Carlsson0b209a82009-09-11 01:22:35 +0000742
743 llvm::APInt LengthI(32, Length + 1);
John McCall8ccfcb52009-09-24 19:53:00 +0000744 QualType ResTy = getSema().Context.CharTy.withConst();
Anders Carlsson0b209a82009-09-11 01:22:35 +0000745 ResTy = getSema().Context.getConstantArrayType(ResTy, LengthI,
746 ArrayType::Normal, 0);
747 PredefinedExpr *PE =
748 new (getSema().Context) PredefinedExpr(E->getLocation(), ResTy, IT);
749 return getSema().Owned(PE);
750}
751
752Sema::OwningExprResult
John McCall13481c52010-02-06 08:42:39 +0000753TemplateInstantiator::TransformTemplateParmRefExpr(DeclRefExpr *E,
Douglas Gregor6c379e22010-02-08 23:41:45 +0000754 NonTypeTemplateParmDecl *NTTP) {
John McCall13481c52010-02-06 08:42:39 +0000755 // If the corresponding template argument is NULL or non-existent, it's
756 // because we are performing instantiation from explicitly-specified
757 // template arguments in a function template, but there were some
758 // arguments left unspecified.
759 if (!TemplateArgs.hasTemplateArgument(NTTP->getDepth(),
760 NTTP->getPosition()))
761 return SemaRef.Owned(E->Retain());
Mike Stump11289f42009-09-09 15:08:12 +0000762
John McCall13481c52010-02-06 08:42:39 +0000763 const TemplateArgument &Arg = TemplateArgs(NTTP->getDepth(),
764 NTTP->getPosition());
Mike Stump11289f42009-09-09 15:08:12 +0000765
John McCall13481c52010-02-06 08:42:39 +0000766 // The template argument itself might be an expression, in which
767 // case we just return that expression.
768 if (Arg.getKind() == TemplateArgument::Expression)
769 return SemaRef.Owned(Arg.getAsExpr()->Retain());
Mike Stump11289f42009-09-09 15:08:12 +0000770
John McCall13481c52010-02-06 08:42:39 +0000771 if (Arg.getKind() == TemplateArgument::Declaration) {
772 ValueDecl *VD = cast<ValueDecl>(Arg.getAsDecl());
Mike Stump11289f42009-09-09 15:08:12 +0000773
John McCall15dda372010-02-06 10:23:53 +0000774 // Find the instantiation of the template argument. This is
775 // required for nested templates.
John McCall13481c52010-02-06 08:42:39 +0000776 VD = cast_or_null<ValueDecl>(
Douglas Gregora04f2ca2010-03-01 15:56:25 +0000777 getSema().FindInstantiatedDecl(E->getLocation(),
778 VD, TemplateArgs));
John McCall13481c52010-02-06 08:42:39 +0000779 if (!VD)
780 return SemaRef.ExprError();
Mike Stump11289f42009-09-09 15:08:12 +0000781
John McCall15dda372010-02-06 10:23:53 +0000782 // Derive the type we want the substituted decl to have. This had
783 // better be non-dependent, or these checks will have serious problems.
784 QualType TargetType = SemaRef.SubstType(NTTP->getType(), TemplateArgs,
Douglas Gregor6c379e22010-02-08 23:41:45 +0000785 E->getLocation(),
786 DeclarationName());
John McCall15dda372010-02-06 10:23:53 +0000787 assert(!TargetType.isNull() && "type substitution failed for param type");
788 assert(!TargetType->isDependentType() && "param type still dependent");
Douglas Gregord5cb1dd2010-03-28 02:42:43 +0000789 return SemaRef.BuildExpressionFromDeclTemplateArgument(Arg,
790 TargetType,
791 E->getLocation());
John McCall13481c52010-02-06 08:42:39 +0000792 }
793
Douglas Gregord5cb1dd2010-03-28 02:42:43 +0000794 return SemaRef.BuildExpressionFromIntegralTemplateArgument(Arg,
795 E->getSourceRange().getBegin());
John McCall13481c52010-02-06 08:42:39 +0000796}
797
798
799Sema::OwningExprResult
800TemplateInstantiator::TransformDeclRefExpr(DeclRefExpr *E) {
801 NamedDecl *D = E->getDecl();
802 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(D)) {
803 if (NTTP->getDepth() < TemplateArgs.getNumLevels())
804 return TransformTemplateParmRefExpr(E, NTTP);
Douglas Gregor954de172009-10-31 17:21:17 +0000805
806 // We have a non-type template parameter that isn't fully substituted;
807 // FindInstantiatedDecl will find it in the local instantiation scope.
Douglas Gregora16548e2009-08-11 05:31:07 +0000808 }
Mike Stump11289f42009-09-09 15:08:12 +0000809
John McCall47f29ea2009-12-08 09:21:05 +0000810 return TreeTransform<TemplateInstantiator>::TransformDeclRefExpr(E);
Douglas Gregora16548e2009-08-11 05:31:07 +0000811}
812
Sebastian Redl14236c82009-11-08 13:56:19 +0000813Sema::OwningExprResult TemplateInstantiator::TransformCXXDefaultArgExpr(
John McCall47f29ea2009-12-08 09:21:05 +0000814 CXXDefaultArgExpr *E) {
Sebastian Redl14236c82009-11-08 13:56:19 +0000815 assert(!cast<FunctionDecl>(E->getParam()->getDeclContext())->
816 getDescribedFunctionTemplate() &&
817 "Default arg expressions are never formed in dependent cases.");
Douglas Gregor033f6752009-12-23 23:03:06 +0000818 return SemaRef.BuildCXXDefaultArgExpr(E->getUsedLocation(),
819 cast<FunctionDecl>(E->getParam()->getDeclContext()),
820 E->getParam());
Sebastian Redl14236c82009-11-08 13:56:19 +0000821}
822
Douglas Gregor14cf7522010-04-30 18:55:50 +0000823QualType TemplateInstantiator::TransformFunctionProtoType(TypeLocBuilder &TLB,
824 FunctionProtoTypeLoc TL,
825 QualType ObjectType) {
826 // We need a local instantiation scope for this function prototype.
827 Sema::LocalInstantiationScope Scope(SemaRef, /*CombineWithOuterScope=*/true);
828 return inherited::TransformFunctionProtoType(TLB, TL, ObjectType);
John McCall58f10c32010-03-11 09:03:00 +0000829}
830
831ParmVarDecl *
832TemplateInstantiator::TransformFunctionTypeParam(ParmVarDecl *OldParm) {
Douglas Gregor940bca72010-04-12 07:48:19 +0000833 return SemaRef.SubstParmVarDecl(OldParm, TemplateArgs);
John McCall58f10c32010-03-11 09:03:00 +0000834}
835
Mike Stump11289f42009-09-09 15:08:12 +0000836QualType
John McCall550e0c22009-10-21 00:40:46 +0000837TemplateInstantiator::TransformTemplateTypeParmType(TypeLocBuilder &TLB,
Douglas Gregorfe17d252010-02-16 19:09:40 +0000838 TemplateTypeParmTypeLoc TL,
839 QualType ObjectType) {
John McCall550e0c22009-10-21 00:40:46 +0000840 TemplateTypeParmType *T = TL.getTypePtr();
Douglas Gregor01afeef2009-08-28 20:31:08 +0000841 if (T->getDepth() < TemplateArgs.getNumLevels()) {
Douglas Gregorfe1e1102009-02-27 19:31:52 +0000842 // Replace the template type parameter with its corresponding
843 // template argument.
Mike Stump11289f42009-09-09 15:08:12 +0000844
845 // If the corresponding template argument is NULL or doesn't exist, it's
846 // because we are performing instantiation from explicitly-specified
847 // template arguments in a function template class, but there were some
Douglas Gregore3f1f352009-07-01 00:28:38 +0000848 // arguments left unspecified.
John McCall550e0c22009-10-21 00:40:46 +0000849 if (!TemplateArgs.hasTemplateArgument(T->getDepth(), T->getIndex())) {
850 TemplateTypeParmTypeLoc NewTL
851 = TLB.push<TemplateTypeParmTypeLoc>(TL.getType());
852 NewTL.setNameLoc(TL.getNameLoc());
853 return TL.getType();
854 }
Mike Stump11289f42009-09-09 15:08:12 +0000855
856 assert(TemplateArgs(T->getDepth(), T->getIndex()).getKind()
Douglas Gregor01afeef2009-08-28 20:31:08 +0000857 == TemplateArgument::Type &&
Douglas Gregorfe1e1102009-02-27 19:31:52 +0000858 "Template argument kind mismatch");
Douglas Gregor01afeef2009-08-28 20:31:08 +0000859
John McCallcebee162009-10-18 09:09:24 +0000860 QualType Replacement
861 = TemplateArgs(T->getDepth(), T->getIndex()).getAsType();
862
863 // TODO: only do this uniquing once, at the start of instantiation.
John McCall550e0c22009-10-21 00:40:46 +0000864 QualType Result
865 = getSema().Context.getSubstTemplateTypeParmType(T, Replacement);
866 SubstTemplateTypeParmTypeLoc NewTL
867 = TLB.push<SubstTemplateTypeParmTypeLoc>(Result);
868 NewTL.setNameLoc(TL.getNameLoc());
869 return Result;
Mike Stump11289f42009-09-09 15:08:12 +0000870 }
Douglas Gregorfe1e1102009-02-27 19:31:52 +0000871
872 // The template type parameter comes from an inner template (e.g.,
873 // the template parameter list of a member template inside the
874 // template we are instantiating). Create a new template type
875 // parameter with the template "level" reduced by one.
John McCall550e0c22009-10-21 00:40:46 +0000876 QualType Result
877 = getSema().Context.getTemplateTypeParmType(T->getDepth()
878 - TemplateArgs.getNumLevels(),
879 T->getIndex(),
880 T->isParameterPack(),
881 T->getName());
882 TemplateTypeParmTypeLoc NewTL = TLB.push<TemplateTypeParmTypeLoc>(Result);
883 NewTL.setNameLoc(TL.getNameLoc());
884 return Result;
Douglas Gregor17c0d7b2009-02-28 00:25:32 +0000885}
Douglas Gregorfe1e1102009-02-27 19:31:52 +0000886
John McCall76d824f2009-08-25 22:02:44 +0000887/// \brief Perform substitution on the type T with a given set of template
888/// arguments.
Douglas Gregorfe1e1102009-02-27 19:31:52 +0000889///
890/// This routine substitutes the given template arguments into the
891/// type T and produces the instantiated type.
892///
893/// \param T the type into which the template arguments will be
894/// substituted. If this type is not dependent, it will be returned
895/// immediately.
896///
897/// \param TemplateArgs the template arguments that will be
898/// substituted for the top-level template parameters within T.
899///
Douglas Gregorfe1e1102009-02-27 19:31:52 +0000900/// \param Loc the location in the source code where this substitution
901/// is being performed. It will typically be the location of the
902/// declarator (if we're instantiating the type of some declaration)
903/// or the location of the type in the source code (if, e.g., we're
904/// instantiating the type of a cast expression).
905///
906/// \param Entity the name of the entity associated with a declaration
907/// being instantiated (if any). May be empty to indicate that there
908/// is no such entity (if, e.g., this is a type that occurs as part of
909/// a cast expression) or that the entity has no name (e.g., an
910/// unnamed function parameter).
911///
912/// \returns If the instantiation succeeds, the instantiated
913/// type. Otherwise, produces diagnostics and returns a NULL type.
John McCallbcd03502009-12-07 02:54:59 +0000914TypeSourceInfo *Sema::SubstType(TypeSourceInfo *T,
John McCall609459e2009-10-21 00:58:09 +0000915 const MultiLevelTemplateArgumentList &Args,
916 SourceLocation Loc,
917 DeclarationName Entity) {
918 assert(!ActiveTemplateInstantiations.empty() &&
919 "Cannot perform an instantiation without some context on the "
920 "instantiation stack");
921
922 if (!T->getType()->isDependentType())
923 return T;
924
925 TemplateInstantiator Instantiator(*this, Args, Loc, Entity);
926 return Instantiator.TransformType(T);
927}
928
929/// Deprecated form of the above.
Mike Stump11289f42009-09-09 15:08:12 +0000930QualType Sema::SubstType(QualType T,
Douglas Gregor01afeef2009-08-28 20:31:08 +0000931 const MultiLevelTemplateArgumentList &TemplateArgs,
John McCall76d824f2009-08-25 22:02:44 +0000932 SourceLocation Loc, DeclarationName Entity) {
Douglas Gregor79cf6032009-03-10 20:44:00 +0000933 assert(!ActiveTemplateInstantiations.empty() &&
934 "Cannot perform an instantiation without some context on the "
935 "instantiation stack");
936
Douglas Gregorfe1e1102009-02-27 19:31:52 +0000937 // If T is not a dependent type, there is nothing to do.
938 if (!T->isDependentType())
939 return T;
940
Douglas Gregord6ff3322009-08-04 16:50:30 +0000941 TemplateInstantiator Instantiator(*this, TemplateArgs, Loc, Entity);
942 return Instantiator.TransformType(T);
Douglas Gregorfe1e1102009-02-27 19:31:52 +0000943}
Douglas Gregor463421d2009-03-03 04:44:36 +0000944
John McCallb29f78f2010-04-09 17:38:44 +0000945static bool NeedsInstantiationAsFunctionType(TypeSourceInfo *T) {
946 if (T->getType()->isDependentType())
947 return true;
948
949 TypeLoc TL = T->getTypeLoc();
950 if (!isa<FunctionProtoTypeLoc>(TL))
951 return false;
952
953 FunctionProtoTypeLoc FP = cast<FunctionProtoTypeLoc>(TL);
954 for (unsigned I = 0, E = FP.getNumArgs(); I != E; ++I) {
955 ParmVarDecl *P = FP.getArg(I);
956
957 // TODO: currently we always rebuild expressions. When we
958 // properly get lazier about this, we should use the same
959 // logic to avoid rebuilding prototypes here.
960 if (P->hasInit())
961 return true;
962 }
963
964 return false;
965}
966
967/// A form of SubstType intended specifically for instantiating the
968/// type of a FunctionDecl. Its purpose is solely to force the
969/// instantiation of default-argument expressions.
970TypeSourceInfo *Sema::SubstFunctionDeclType(TypeSourceInfo *T,
971 const MultiLevelTemplateArgumentList &Args,
972 SourceLocation Loc,
973 DeclarationName Entity) {
974 assert(!ActiveTemplateInstantiations.empty() &&
975 "Cannot perform an instantiation without some context on the "
976 "instantiation stack");
977
978 if (!NeedsInstantiationAsFunctionType(T))
979 return T;
980
981 TemplateInstantiator Instantiator(*this, Args, Loc, Entity);
982
983 TypeLocBuilder TLB;
984
985 TypeLoc TL = T->getTypeLoc();
986 TLB.reserve(TL.getFullDataSize());
987
988 QualType Result = Instantiator.TransformType(TLB, TL, QualType());
989 if (Result.isNull())
990 return 0;
991
992 return TLB.getTypeSourceInfo(Context, Result);
993}
994
Douglas Gregor940bca72010-04-12 07:48:19 +0000995ParmVarDecl *Sema::SubstParmVarDecl(ParmVarDecl *OldParm,
996 const MultiLevelTemplateArgumentList &TemplateArgs) {
997 TypeSourceInfo *OldDI = OldParm->getTypeSourceInfo();
998 TypeSourceInfo *NewDI = SubstType(OldDI, TemplateArgs, OldParm->getLocation(),
999 OldParm->getDeclName());
1000 if (!NewDI)
1001 return 0;
1002
1003 if (NewDI->getType()->isVoidType()) {
1004 Diag(OldParm->getLocation(), diag::err_param_with_void_type);
1005 return 0;
1006 }
1007
1008 ParmVarDecl *NewParm = CheckParameter(Context.getTranslationUnitDecl(),
1009 NewDI, NewDI->getType(),
1010 OldParm->getIdentifier(),
1011 OldParm->getLocation(),
Douglas Gregorc4df4072010-04-19 22:54:31 +00001012 OldParm->getStorageClass(),
1013 OldParm->getStorageClassAsWritten());
Douglas Gregor940bca72010-04-12 07:48:19 +00001014 if (!NewParm)
1015 return 0;
1016
1017 // Mark the (new) default argument as uninstantiated (if any).
1018 if (OldParm->hasUninstantiatedDefaultArg()) {
1019 Expr *Arg = OldParm->getUninstantiatedDefaultArg();
1020 NewParm->setUninstantiatedDefaultArg(Arg);
1021 } else if (Expr *Arg = OldParm->getDefaultArg())
1022 NewParm->setUninstantiatedDefaultArg(Arg);
1023
1024 NewParm->setHasInheritedDefaultArg(OldParm->hasInheritedDefaultArg());
1025
1026 CurrentInstantiationScope->InstantiatedLocal(OldParm, NewParm);
1027 return NewParm;
1028}
1029
John McCall76d824f2009-08-25 22:02:44 +00001030/// \brief Perform substitution on the base class specifiers of the
1031/// given class template specialization.
Douglas Gregor463421d2009-03-03 04:44:36 +00001032///
1033/// Produces a diagnostic and returns true on error, returns false and
1034/// attaches the instantiated base classes to the class template
1035/// specialization if successful.
Mike Stump11289f42009-09-09 15:08:12 +00001036bool
John McCall76d824f2009-08-25 22:02:44 +00001037Sema::SubstBaseSpecifiers(CXXRecordDecl *Instantiation,
1038 CXXRecordDecl *Pattern,
Douglas Gregor01afeef2009-08-28 20:31:08 +00001039 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor463421d2009-03-03 04:44:36 +00001040 bool Invalid = false;
Douglas Gregor6181ded2009-05-29 18:27:38 +00001041 llvm::SmallVector<CXXBaseSpecifier*, 4> InstantiatedBases;
Mike Stump11289f42009-09-09 15:08:12 +00001042 for (ClassTemplateSpecializationDecl::base_class_iterator
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001043 Base = Pattern->bases_begin(), BaseEnd = Pattern->bases_end();
Douglas Gregor2a72edd2009-03-10 18:52:44 +00001044 Base != BaseEnd; ++Base) {
Douglas Gregor463421d2009-03-03 04:44:36 +00001045 if (!Base->getType()->isDependentType()) {
Anders Carlssonae3c5cf2009-12-03 17:49:57 +00001046 const CXXRecordDecl *BaseDecl =
1047 cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
1048
1049 // Make sure to set the attributes from the base.
1050 SetClassDeclAttributesFromBase(Instantiation, BaseDecl,
1051 Base->isVirtual());
1052
Fariborz Jahanian5c14ec32009-07-22 17:41:53 +00001053 InstantiatedBases.push_back(new (Context) CXXBaseSpecifier(*Base));
Douglas Gregor463421d2009-03-03 04:44:36 +00001054 continue;
1055 }
1056
Mike Stump11289f42009-09-09 15:08:12 +00001057 QualType BaseType = SubstType(Base->getType(),
1058 TemplateArgs,
John McCall76d824f2009-08-25 22:02:44 +00001059 Base->getSourceRange().getBegin(),
1060 DeclarationName());
Douglas Gregor463421d2009-03-03 04:44:36 +00001061 if (BaseType.isNull()) {
1062 Invalid = true;
1063 continue;
1064 }
1065
1066 if (CXXBaseSpecifier *InstantiatedBase
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001067 = CheckBaseSpecifier(Instantiation,
Douglas Gregor463421d2009-03-03 04:44:36 +00001068 Base->getSourceRange(),
1069 Base->isVirtual(),
1070 Base->getAccessSpecifierAsWritten(),
1071 BaseType,
1072 /*FIXME: Not totally accurate */
1073 Base->getSourceRange().getBegin()))
1074 InstantiatedBases.push_back(InstantiatedBase);
1075 else
1076 Invalid = true;
1077 }
1078
Douglas Gregor2a72edd2009-03-10 18:52:44 +00001079 if (!Invalid &&
Jay Foad7d0479f2009-05-21 09:52:38 +00001080 AttachBaseSpecifiers(Instantiation, InstantiatedBases.data(),
Douglas Gregor463421d2009-03-03 04:44:36 +00001081 InstantiatedBases.size()))
1082 Invalid = true;
1083
1084 return Invalid;
1085}
1086
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001087/// \brief Instantiate the definition of a class from a given pattern.
1088///
1089/// \param PointOfInstantiation The point of instantiation within the
1090/// source code.
1091///
1092/// \param Instantiation is the declaration whose definition is being
1093/// instantiated. This will be either a class template specialization
1094/// or a member class of a class template specialization.
1095///
1096/// \param Pattern is the pattern from which the instantiation
1097/// occurs. This will be either the declaration of a class template or
1098/// the declaration of a member class of a class template.
1099///
1100/// \param TemplateArgs The template arguments to be substituted into
1101/// the pattern.
1102///
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001103/// \param TSK the kind of implicit or explicit instantiation to perform.
Douglas Gregor8a2e6012009-08-24 15:23:48 +00001104///
1105/// \param Complain whether to complain if the class cannot be instantiated due
1106/// to the lack of a definition.
1107///
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001108/// \returns true if an error occurred, false otherwise.
1109bool
1110Sema::InstantiateClass(SourceLocation PointOfInstantiation,
1111 CXXRecordDecl *Instantiation, CXXRecordDecl *Pattern,
Douglas Gregor01afeef2009-08-28 20:31:08 +00001112 const MultiLevelTemplateArgumentList &TemplateArgs,
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001113 TemplateSpecializationKind TSK,
Douglas Gregor8a2e6012009-08-24 15:23:48 +00001114 bool Complain) {
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001115 bool Invalid = false;
John McCall87a44eb2009-08-20 01:44:21 +00001116
Mike Stump11289f42009-09-09 15:08:12 +00001117 CXXRecordDecl *PatternDef
Douglas Gregor0a5a2212010-02-11 01:04:33 +00001118 = cast_or_null<CXXRecordDecl>(Pattern->getDefinition());
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001119 if (!PatternDef) {
Douglas Gregor8a2e6012009-08-24 15:23:48 +00001120 if (!Complain) {
1121 // Say nothing
1122 } else if (Pattern == Instantiation->getInstantiatedFromMemberClass()) {
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001123 Diag(PointOfInstantiation,
1124 diag::err_implicit_instantiate_member_undefined)
1125 << Context.getTypeDeclType(Instantiation);
1126 Diag(Pattern->getLocation(), diag::note_member_of_template_here);
1127 } else {
Douglas Gregora1f49972009-05-13 00:25:59 +00001128 Diag(PointOfInstantiation, diag::err_template_instantiate_undefined)
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001129 << (TSK != TSK_ImplicitInstantiation)
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001130 << Context.getTypeDeclType(Instantiation);
1131 Diag(Pattern->getLocation(), diag::note_template_decl_here);
1132 }
1133 return true;
1134 }
1135 Pattern = PatternDef;
1136
Douglas Gregord6ba93d2009-10-15 15:54:05 +00001137 // \brief Record the point of instantiation.
1138 if (MemberSpecializationInfo *MSInfo
1139 = Instantiation->getMemberSpecializationInfo()) {
1140 MSInfo->setTemplateSpecializationKind(TSK);
1141 MSInfo->setPointOfInstantiation(PointOfInstantiation);
Douglas Gregoref6ab412009-10-27 06:26:26 +00001142 } else if (ClassTemplateSpecializationDecl *Spec
1143 = dyn_cast<ClassTemplateSpecializationDecl>(Instantiation)) {
1144 Spec->setTemplateSpecializationKind(TSK);
1145 Spec->setPointOfInstantiation(PointOfInstantiation);
Douglas Gregord6ba93d2009-10-15 15:54:05 +00001146 }
1147
Douglas Gregorf3430ae2009-03-25 21:23:52 +00001148 InstantiatingTemplate Inst(*this, PointOfInstantiation, Instantiation);
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001149 if (Inst)
1150 return true;
1151
1152 // Enter the scope of this instantiation. We don't use
1153 // PushDeclContext because we don't have a scope.
John McCall80e58cd2010-04-29 00:35:03 +00001154 ContextRAII SavedContext(*this, Instantiation);
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001155
Douglas Gregor51121572010-03-24 01:33:17 +00001156 // If this is an instantiation of a local class, merge this local
1157 // instantiation scope with the enclosing scope. Otherwise, every
1158 // instantiation of a class has its own local instantiation scope.
1159 bool MergeWithParentScope = !Instantiation->isDefinedOutsideFunctionOrMethod();
1160 Sema::LocalInstantiationScope Scope(*this, MergeWithParentScope);
1161
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001162 // Start the definition of this instantiation.
1163 Instantiation->startDefinition();
1164
John McCall76d824f2009-08-25 22:02:44 +00001165 // Do substitution on the base class specifiers.
1166 if (SubstBaseSpecifiers(Instantiation, Pattern, TemplateArgs))
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001167 Invalid = true;
1168
Douglas Gregor6181ded2009-05-29 18:27:38 +00001169 llvm::SmallVector<DeclPtrTy, 4> Fields;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001170 for (RecordDecl::decl_iterator Member = Pattern->decls_begin(),
Mike Stump11289f42009-09-09 15:08:12 +00001171 MemberEnd = Pattern->decls_end();
Douglas Gregorbcced4e2009-04-09 21:40:53 +00001172 Member != MemberEnd; ++Member) {
John McCall76d824f2009-08-25 22:02:44 +00001173 Decl *NewMember = SubstDecl(*Member, Instantiation, TemplateArgs);
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001174 if (NewMember) {
Eli Friedmand0e8de22009-12-07 00:22:08 +00001175 if (FieldDecl *Field = dyn_cast<FieldDecl>(NewMember))
Chris Lattner83f095c2009-03-28 19:18:32 +00001176 Fields.push_back(DeclPtrTy::make(Field));
Eli Friedmand0e8de22009-12-07 00:22:08 +00001177 else if (NewMember->isInvalidDecl())
1178 Invalid = true;
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001179 } else {
1180 // FIXME: Eventually, a NULL return will mean that one of the
Mike Stump87c57ac2009-05-16 07:39:55 +00001181 // instantiations was a semantic disaster, and we'll want to set Invalid =
1182 // true. For now, we expect to skip some members that we can't yet handle.
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001183 }
1184 }
1185
1186 // Finish checking fields.
Chris Lattner83f095c2009-03-28 19:18:32 +00001187 ActOnFields(0, Instantiation->getLocation(), DeclPtrTy::make(Instantiation),
Jay Foad7d0479f2009-05-21 09:52:38 +00001188 Fields.data(), Fields.size(), SourceLocation(), SourceLocation(),
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001189 0);
Douglas Gregorb93b6062010-04-12 17:09:20 +00001190 CheckCompletedCXXClass(/*Scope=*/0, Instantiation);
Douglas Gregor3c74d412009-10-14 20:14:33 +00001191 if (Instantiation->isInvalidDecl())
1192 Invalid = true;
1193
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001194 // Exit the scope of this instantiation.
John McCall80e58cd2010-04-29 00:35:03 +00001195 SavedContext.pop();
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001196
Douglas Gregor0a0f04d2010-01-06 04:44:19 +00001197 // If this is a polymorphic C++ class without a key function, we'll
1198 // have to mark all of the virtual members to allow emission of a vtable
1199 // in this translation unit.
Chandler Carruth3e0c1402010-02-15 22:12:26 +00001200 if (Instantiation->isDynamicClass() &&
1201 !Context.getKeyFunction(Instantiation)) {
1202 // Local classes need to have their methods instantiated immediately in
1203 // order to have the correct instantiation scope.
1204 if (Instantiation->isLocalClass()) {
1205 MarkVirtualMembersReferenced(PointOfInstantiation,
1206 Instantiation);
1207 } else {
Douglas Gregor0a0f04d2010-01-06 04:44:19 +00001208 ClassesWithUnmarkedVirtualMembers.push_back(std::make_pair(Instantiation,
1209 PointOfInstantiation));
Chandler Carruth3e0c1402010-02-15 22:12:26 +00001210 }
1211 }
Douglas Gregor0a0f04d2010-01-06 04:44:19 +00001212
Douglas Gregor28ad4b52009-05-26 20:50:29 +00001213 if (!Invalid)
1214 Consumer.HandleTagDeclDefinition(Instantiation);
1215
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001216 return Invalid;
1217}
1218
Mike Stump11289f42009-09-09 15:08:12 +00001219bool
Douglas Gregor463421d2009-03-03 04:44:36 +00001220Sema::InstantiateClassTemplateSpecialization(
Douglas Gregoref6ab412009-10-27 06:26:26 +00001221 SourceLocation PointOfInstantiation,
Douglas Gregor463421d2009-03-03 04:44:36 +00001222 ClassTemplateSpecializationDecl *ClassTemplateSpec,
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001223 TemplateSpecializationKind TSK,
Douglas Gregor8a2e6012009-08-24 15:23:48 +00001224 bool Complain) {
Douglas Gregor463421d2009-03-03 04:44:36 +00001225 // Perform the actual instantiation on the canonical declaration.
1226 ClassTemplateSpec = cast<ClassTemplateSpecializationDecl>(
Argyrios Kyrtzidis6b7e3762009-07-18 00:34:25 +00001227 ClassTemplateSpec->getCanonicalDecl());
Douglas Gregor463421d2009-03-03 04:44:36 +00001228
Douglas Gregor4aa04b12009-09-11 21:19:12 +00001229 // Check whether we have already instantiated or specialized this class
1230 // template specialization.
1231 if (ClassTemplateSpec->getSpecializationKind() != TSK_Undeclared) {
1232 if (ClassTemplateSpec->getSpecializationKind() ==
1233 TSK_ExplicitInstantiationDeclaration &&
1234 TSK == TSK_ExplicitInstantiationDefinition) {
1235 // An explicit instantiation definition follows an explicit instantiation
1236 // declaration (C++0x [temp.explicit]p10); go ahead and perform the
1237 // explicit instantiation.
1238 ClassTemplateSpec->setSpecializationKind(TSK);
Douglas Gregor4aa04b12009-09-11 21:19:12 +00001239 return false;
1240 }
1241
1242 // We can only instantiate something that hasn't already been
1243 // instantiated or specialized. Fail without any diagnostics: our
1244 // caller will provide an error message.
Douglas Gregor463421d2009-03-03 04:44:36 +00001245 return true;
Douglas Gregor4aa04b12009-09-11 21:19:12 +00001246 }
Douglas Gregor463421d2009-03-03 04:44:36 +00001247
Douglas Gregor00a511f2009-09-15 16:51:42 +00001248 if (ClassTemplateSpec->isInvalidDecl())
1249 return true;
1250
Douglas Gregor463421d2009-03-03 04:44:36 +00001251 ClassTemplateDecl *Template = ClassTemplateSpec->getSpecializedTemplate();
Douglas Gregor01afeef2009-08-28 20:31:08 +00001252 CXXRecordDecl *Pattern = 0;
Douglas Gregor2373c592009-05-31 09:31:02 +00001253
Douglas Gregor170bc422009-06-12 22:31:52 +00001254 // C++ [temp.class.spec.match]p1:
1255 // When a class template is used in a context that requires an
1256 // instantiation of the class, it is necessary to determine
1257 // whether the instantiation is to be generated using the primary
1258 // template or one of the partial specializations. This is done by
1259 // matching the template arguments of the class template
1260 // specialization with the template argument lists of the partial
1261 // specializations.
Douglas Gregorb7ae10f2009-06-05 00:53:49 +00001262 typedef std::pair<ClassTemplatePartialSpecializationDecl *,
1263 TemplateArgumentList *> MatchResult;
1264 llvm::SmallVector<MatchResult, 4> Matched;
Douglas Gregor407e9612010-04-30 05:56:50 +00001265 llvm::SmallVector<ClassTemplatePartialSpecializationDecl *, 4> PartialSpecs;
1266 Template->getPartialSpecializations(PartialSpecs);
1267 for (unsigned I = 0, N = PartialSpecs.size(); I != N; ++I) {
1268 ClassTemplatePartialSpecializationDecl *Partial = PartialSpecs[I];
John McCallbc077cf2010-02-08 23:07:23 +00001269 TemplateDeductionInfo Info(Context, PointOfInstantiation);
Douglas Gregor181aa4a2009-06-12 18:26:56 +00001270 if (TemplateDeductionResult Result
Douglas Gregor407e9612010-04-30 05:56:50 +00001271 = DeduceTemplateArguments(Partial,
Douglas Gregor181aa4a2009-06-12 18:26:56 +00001272 ClassTemplateSpec->getTemplateArgs(),
1273 Info)) {
1274 // FIXME: Store the failed-deduction information for use in
1275 // diagnostics, later.
1276 (void)Result;
1277 } else {
Douglas Gregor407e9612010-04-30 05:56:50 +00001278 Matched.push_back(std::make_pair(Partial, Info.take()));
Douglas Gregor181aa4a2009-06-12 18:26:56 +00001279 }
Douglas Gregor2373c592009-05-31 09:31:02 +00001280 }
1281
Douglas Gregor21610382009-10-29 00:04:11 +00001282 if (Matched.size() >= 1) {
Douglas Gregorbe999392009-09-15 16:23:51 +00001283 llvm::SmallVector<MatchResult, 4>::iterator Best = Matched.begin();
Douglas Gregor21610382009-10-29 00:04:11 +00001284 if (Matched.size() == 1) {
1285 // -- If exactly one matching specialization is found, the
1286 // instantiation is generated from that specialization.
1287 // We don't need to do anything for this.
1288 } else {
1289 // -- If more than one matching specialization is found, the
1290 // partial order rules (14.5.4.2) are used to determine
1291 // whether one of the specializations is more specialized
1292 // than the others. If none of the specializations is more
1293 // specialized than all of the other matching
1294 // specializations, then the use of the class template is
1295 // ambiguous and the program is ill-formed.
1296 for (llvm::SmallVector<MatchResult, 4>::iterator P = Best + 1,
1297 PEnd = Matched.end();
1298 P != PEnd; ++P) {
John McCallbc077cf2010-02-08 23:07:23 +00001299 if (getMoreSpecializedPartialSpecialization(P->first, Best->first,
1300 PointOfInstantiation)
Douglas Gregor21610382009-10-29 00:04:11 +00001301 == P->first)
1302 Best = P;
Douglas Gregorbe999392009-09-15 16:23:51 +00001303 }
Douglas Gregorbe999392009-09-15 16:23:51 +00001304
Douglas Gregor21610382009-10-29 00:04:11 +00001305 // Determine if the best partial specialization is more specialized than
1306 // the others.
1307 bool Ambiguous = false;
Douglas Gregorbe999392009-09-15 16:23:51 +00001308 for (llvm::SmallVector<MatchResult, 4>::iterator P = Matched.begin(),
1309 PEnd = Matched.end();
Douglas Gregor21610382009-10-29 00:04:11 +00001310 P != PEnd; ++P) {
1311 if (P != Best &&
John McCallbc077cf2010-02-08 23:07:23 +00001312 getMoreSpecializedPartialSpecialization(P->first, Best->first,
1313 PointOfInstantiation)
Douglas Gregor21610382009-10-29 00:04:11 +00001314 != Best->first) {
1315 Ambiguous = true;
1316 break;
1317 }
1318 }
1319
1320 if (Ambiguous) {
1321 // Partial ordering did not produce a clear winner. Complain.
1322 ClassTemplateSpec->setInvalidDecl();
1323 Diag(PointOfInstantiation, diag::err_partial_spec_ordering_ambiguous)
1324 << ClassTemplateSpec;
1325
1326 // Print the matching partial specializations.
1327 for (llvm::SmallVector<MatchResult, 4>::iterator P = Matched.begin(),
1328 PEnd = Matched.end();
1329 P != PEnd; ++P)
1330 Diag(P->first->getLocation(), diag::note_partial_spec_match)
1331 << getTemplateArgumentBindingsText(P->first->getTemplateParameters(),
1332 *P->second);
Douglas Gregor01afeef2009-08-28 20:31:08 +00001333
Douglas Gregor21610382009-10-29 00:04:11 +00001334 return true;
1335 }
Douglas Gregorbe999392009-09-15 16:23:51 +00001336 }
1337
1338 // Instantiate using the best class template partial specialization.
Douglas Gregor21610382009-10-29 00:04:11 +00001339 ClassTemplatePartialSpecializationDecl *OrigPartialSpec = Best->first;
1340 while (OrigPartialSpec->getInstantiatedFromMember()) {
1341 // If we've found an explicit specialization of this class template,
1342 // stop here and use that as the pattern.
1343 if (OrigPartialSpec->isMemberSpecialization())
1344 break;
1345
1346 OrigPartialSpec = OrigPartialSpec->getInstantiatedFromMember();
1347 }
1348
1349 Pattern = OrigPartialSpec;
Douglas Gregorbe999392009-09-15 16:23:51 +00001350 ClassTemplateSpec->setInstantiationOf(Best->first, Best->second);
Douglas Gregor170bc422009-06-12 22:31:52 +00001351 } else {
1352 // -- If no matches are found, the instantiation is generated
1353 // from the primary template.
Douglas Gregor01afeef2009-08-28 20:31:08 +00001354 ClassTemplateDecl *OrigTemplate = Template;
Douglas Gregorcf915552009-10-13 16:30:37 +00001355 while (OrigTemplate->getInstantiatedFromMemberTemplate()) {
1356 // If we've found an explicit specialization of this class template,
1357 // stop here and use that as the pattern.
1358 if (OrigTemplate->isMemberSpecialization())
1359 break;
1360
Douglas Gregor01afeef2009-08-28 20:31:08 +00001361 OrigTemplate = OrigTemplate->getInstantiatedFromMemberTemplate();
Douglas Gregorcf915552009-10-13 16:30:37 +00001362 }
1363
Douglas Gregor01afeef2009-08-28 20:31:08 +00001364 Pattern = OrigTemplate->getTemplatedDecl();
Douglas Gregor2373c592009-05-31 09:31:02 +00001365 }
Douglas Gregor463421d2009-03-03 04:44:36 +00001366
Douglas Gregoref6ab412009-10-27 06:26:26 +00001367 bool Result = InstantiateClass(PointOfInstantiation, ClassTemplateSpec,
1368 Pattern,
1369 getTemplateInstantiationArgs(ClassTemplateSpec),
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001370 TSK,
Douglas Gregor8a2e6012009-08-24 15:23:48 +00001371 Complain);
Mike Stump11289f42009-09-09 15:08:12 +00001372
Douglas Gregorb7ae10f2009-06-05 00:53:49 +00001373 for (unsigned I = 0, N = Matched.size(); I != N; ++I) {
1374 // FIXME: Implement TemplateArgumentList::Destroy!
1375 // if (Matched[I].first != Pattern)
1376 // Matched[I].second->Destroy(Context);
1377 }
Mike Stump11289f42009-09-09 15:08:12 +00001378
Douglas Gregorb7ae10f2009-06-05 00:53:49 +00001379 return Result;
Douglas Gregor463421d2009-03-03 04:44:36 +00001380}
Douglas Gregor90a1a652009-03-19 17:26:29 +00001381
John McCall76d824f2009-08-25 22:02:44 +00001382/// \brief Instantiates the definitions of all of the member
1383/// of the given class, which is an instantiation of a class template
1384/// or a member class of a template.
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00001385void
1386Sema::InstantiateClassMembers(SourceLocation PointOfInstantiation,
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001387 CXXRecordDecl *Instantiation,
1388 const MultiLevelTemplateArgumentList &TemplateArgs,
1389 TemplateSpecializationKind TSK) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001390 for (DeclContext::decl_iterator D = Instantiation->decls_begin(),
1391 DEnd = Instantiation->decls_end();
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00001392 D != DEnd; ++D) {
Douglas Gregor1d957a32009-10-27 18:42:08 +00001393 bool SuppressNew = false;
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00001394 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(*D)) {
Douglas Gregor1d957a32009-10-27 18:42:08 +00001395 if (FunctionDecl *Pattern
1396 = Function->getInstantiatedFromMemberFunction()) {
1397 MemberSpecializationInfo *MSInfo
1398 = Function->getMemberSpecializationInfo();
1399 assert(MSInfo && "No member specialization information?");
Douglas Gregor06aa50412010-04-09 21:02:29 +00001400 if (MSInfo->getTemplateSpecializationKind()
1401 == TSK_ExplicitSpecialization)
1402 continue;
1403
Douglas Gregor1d957a32009-10-27 18:42:08 +00001404 if (CheckSpecializationInstantiationRedecl(PointOfInstantiation, TSK,
1405 Function,
1406 MSInfo->getTemplateSpecializationKind(),
1407 MSInfo->getPointOfInstantiation(),
1408 SuppressNew) ||
1409 SuppressNew)
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001410 continue;
1411
Douglas Gregor1d957a32009-10-27 18:42:08 +00001412 if (Function->getBody())
1413 continue;
1414
1415 if (TSK == TSK_ExplicitInstantiationDefinition) {
1416 // C++0x [temp.explicit]p8:
1417 // An explicit instantiation definition that names a class template
1418 // specialization explicitly instantiates the class template
1419 // specialization and is only an explicit instantiation definition
1420 // of members whose definition is visible at the point of
1421 // instantiation.
1422 if (!Pattern->getBody())
1423 continue;
1424
1425 Function->setTemplateSpecializationKind(TSK, PointOfInstantiation);
1426
1427 InstantiateFunctionDefinition(PointOfInstantiation, Function);
1428 } else {
1429 Function->setTemplateSpecializationKind(TSK, PointOfInstantiation);
1430 }
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001431 }
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00001432 } else if (VarDecl *Var = dyn_cast<VarDecl>(*D)) {
Douglas Gregor86d142a2009-10-08 07:24:58 +00001433 if (Var->isStaticDataMember()) {
Douglas Gregor1d957a32009-10-27 18:42:08 +00001434 MemberSpecializationInfo *MSInfo = Var->getMemberSpecializationInfo();
1435 assert(MSInfo && "No member specialization information?");
Douglas Gregor06aa50412010-04-09 21:02:29 +00001436 if (MSInfo->getTemplateSpecializationKind()
1437 == TSK_ExplicitSpecialization)
1438 continue;
1439
Douglas Gregor1d957a32009-10-27 18:42:08 +00001440 if (CheckSpecializationInstantiationRedecl(PointOfInstantiation, TSK,
1441 Var,
1442 MSInfo->getTemplateSpecializationKind(),
1443 MSInfo->getPointOfInstantiation(),
1444 SuppressNew) ||
1445 SuppressNew)
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001446 continue;
1447
Douglas Gregor1d957a32009-10-27 18:42:08 +00001448 if (TSK == TSK_ExplicitInstantiationDefinition) {
1449 // C++0x [temp.explicit]p8:
1450 // An explicit instantiation definition that names a class template
1451 // specialization explicitly instantiates the class template
1452 // specialization and is only an explicit instantiation definition
1453 // of members whose definition is visible at the point of
1454 // instantiation.
1455 if (!Var->getInstantiatedFromStaticDataMember()
1456 ->getOutOfLineDefinition())
1457 continue;
1458
1459 Var->setTemplateSpecializationKind(TSK, PointOfInstantiation);
Douglas Gregor86d142a2009-10-08 07:24:58 +00001460 InstantiateStaticDataMemberDefinition(PointOfInstantiation, Var);
Douglas Gregor1d957a32009-10-27 18:42:08 +00001461 } else {
1462 Var->setTemplateSpecializationKind(TSK, PointOfInstantiation);
1463 }
1464 }
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00001465 } else if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(*D)) {
Douglas Gregor1da22252010-04-18 18:11:38 +00001466 // Always skip the injected-class-name, along with any
1467 // redeclarations of nested classes, since both would cause us
1468 // to try to instantiate the members of a class twice.
1469 if (Record->isInjectedClassName() || Record->getPreviousDeclaration())
Douglas Gregord801b062009-10-07 23:56:10 +00001470 continue;
1471
Douglas Gregor1d957a32009-10-27 18:42:08 +00001472 MemberSpecializationInfo *MSInfo = Record->getMemberSpecializationInfo();
1473 assert(MSInfo && "No member specialization information?");
Douglas Gregor06aa50412010-04-09 21:02:29 +00001474
1475 if (MSInfo->getTemplateSpecializationKind()
1476 == TSK_ExplicitSpecialization)
1477 continue;
1478
Douglas Gregor1d957a32009-10-27 18:42:08 +00001479 if (CheckSpecializationInstantiationRedecl(PointOfInstantiation, TSK,
1480 Record,
1481 MSInfo->getTemplateSpecializationKind(),
1482 MSInfo->getPointOfInstantiation(),
1483 SuppressNew) ||
1484 SuppressNew)
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001485 continue;
1486
Douglas Gregor1d957a32009-10-27 18:42:08 +00001487 CXXRecordDecl *Pattern = Record->getInstantiatedFromMemberClass();
1488 assert(Pattern && "Missing instantiated-from-template information");
1489
Douglas Gregor0a5a2212010-02-11 01:04:33 +00001490 if (!Record->getDefinition()) {
1491 if (!Pattern->getDefinition()) {
Douglas Gregor1d957a32009-10-27 18:42:08 +00001492 // C++0x [temp.explicit]p8:
1493 // An explicit instantiation definition that names a class template
1494 // specialization explicitly instantiates the class template
1495 // specialization and is only an explicit instantiation definition
1496 // of members whose definition is visible at the point of
1497 // instantiation.
1498 if (TSK == TSK_ExplicitInstantiationDeclaration) {
1499 MSInfo->setTemplateSpecializationKind(TSK);
1500 MSInfo->setPointOfInstantiation(PointOfInstantiation);
1501 }
1502
1503 continue;
1504 }
1505
1506 InstantiateClass(PointOfInstantiation, Record, Pattern,
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001507 TemplateArgs,
1508 TSK);
Douglas Gregor1d957a32009-10-27 18:42:08 +00001509 }
Douglas Gregorc093c1d2009-10-08 01:19:17 +00001510
Douglas Gregor0a5a2212010-02-11 01:04:33 +00001511 Pattern = cast_or_null<CXXRecordDecl>(Record->getDefinition());
Douglas Gregor1d957a32009-10-27 18:42:08 +00001512 if (Pattern)
1513 InstantiateClassMembers(PointOfInstantiation, Pattern, TemplateArgs,
1514 TSK);
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00001515 }
1516 }
1517}
1518
1519/// \brief Instantiate the definitions of all of the members of the
1520/// given class template specialization, which was named as part of an
1521/// explicit instantiation.
Mike Stump11289f42009-09-09 15:08:12 +00001522void
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001523Sema::InstantiateClassTemplateSpecializationMembers(
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00001524 SourceLocation PointOfInstantiation,
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001525 ClassTemplateSpecializationDecl *ClassTemplateSpec,
1526 TemplateSpecializationKind TSK) {
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00001527 // C++0x [temp.explicit]p7:
1528 // An explicit instantiation that names a class template
1529 // specialization is an explicit instantion of the same kind
1530 // (declaration or definition) of each of its members (not
1531 // including members inherited from base classes) that has not
1532 // been previously explicitly specialized in the translation unit
1533 // containing the explicit instantiation, except as described
1534 // below.
1535 InstantiateClassMembers(PointOfInstantiation, ClassTemplateSpec,
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001536 getTemplateInstantiationArgs(ClassTemplateSpec),
1537 TSK);
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00001538}
1539
Mike Stump11289f42009-09-09 15:08:12 +00001540Sema::OwningStmtResult
Douglas Gregor01afeef2009-08-28 20:31:08 +00001541Sema::SubstStmt(Stmt *S, const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregorebe10102009-08-20 07:17:43 +00001542 if (!S)
1543 return Owned(S);
1544
1545 TemplateInstantiator Instantiator(*this, TemplateArgs,
1546 SourceLocation(),
1547 DeclarationName());
1548 return Instantiator.TransformStmt(S);
1549}
1550
Mike Stump11289f42009-09-09 15:08:12 +00001551Sema::OwningExprResult
Douglas Gregor01afeef2009-08-28 20:31:08 +00001552Sema::SubstExpr(Expr *E, const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregora16548e2009-08-11 05:31:07 +00001553 if (!E)
1554 return Owned(E);
Mike Stump11289f42009-09-09 15:08:12 +00001555
Douglas Gregora16548e2009-08-11 05:31:07 +00001556 TemplateInstantiator Instantiator(*this, TemplateArgs,
1557 SourceLocation(),
1558 DeclarationName());
1559 return Instantiator.TransformExpr(E);
1560}
1561
John McCall76d824f2009-08-25 22:02:44 +00001562/// \brief Do template substitution on a nested-name-specifier.
Douglas Gregorf21eb492009-03-26 23:50:42 +00001563NestedNameSpecifier *
John McCall76d824f2009-08-25 22:02:44 +00001564Sema::SubstNestedNameSpecifier(NestedNameSpecifier *NNS,
Douglas Gregor01afeef2009-08-28 20:31:08 +00001565 SourceRange Range,
1566 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor1135c352009-08-06 05:28:30 +00001567 TemplateInstantiator Instantiator(*this, TemplateArgs, Range.getBegin(),
1568 DeclarationName());
Douglas Gregorcd3f49f2010-02-25 04:46:04 +00001569 return Instantiator.TransformNestedNameSpecifier(NNS, Range);
Douglas Gregor90a1a652009-03-19 17:26:29 +00001570}
Douglas Gregoraa594892009-03-31 18:38:02 +00001571
1572TemplateName
John McCall76d824f2009-08-25 22:02:44 +00001573Sema::SubstTemplateName(TemplateName Name, SourceLocation Loc,
Douglas Gregor01afeef2009-08-28 20:31:08 +00001574 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor71dc5092009-08-06 06:41:21 +00001575 TemplateInstantiator Instantiator(*this, TemplateArgs, Loc,
1576 DeclarationName());
1577 return Instantiator.TransformTemplateName(Name);
Douglas Gregoraa594892009-03-31 18:38:02 +00001578}
Douglas Gregorc43620d2009-06-11 00:06:24 +00001579
John McCall0ad16662009-10-29 08:12:44 +00001580bool Sema::Subst(const TemplateArgumentLoc &Input, TemplateArgumentLoc &Output,
1581 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregore922c772009-08-04 22:27:00 +00001582 TemplateInstantiator Instantiator(*this, TemplateArgs, SourceLocation(),
1583 DeclarationName());
John McCall0ad16662009-10-29 08:12:44 +00001584
1585 return Instantiator.TransformTemplateArgument(Input, Output);
Douglas Gregorc43620d2009-06-11 00:06:24 +00001586}
Douglas Gregor14cf7522010-04-30 18:55:50 +00001587
1588Decl *Sema::LocalInstantiationScope::getInstantiationOf(const Decl *D) {
1589 for (LocalInstantiationScope *Current = this; Current;
1590 Current = Current->Outer) {
1591 // Check if we found something within this scope.
1592 llvm::DenseMap<const Decl *, Decl *>::iterator Found
1593 = Current->LocalDecls.find(D);
1594 if (Found != Current->LocalDecls.end())
1595 return Found->second;
1596
1597 // If we aren't combined with our outer scope, we're done.
1598 if (!Current->CombineWithOuterScope)
1599 break;
1600 }
1601
1602 assert(D->isInvalidDecl() &&
1603 "declaration was not instantiated in this scope!");
1604 return 0;
1605}
1606
1607void Sema::LocalInstantiationScope::InstantiatedLocal(const Decl *D,
1608 Decl *Inst) {
1609 Decl *&Stored = LocalDecls[D];
1610 assert((!Stored || Stored == Inst)&& "Already instantiated this local");
1611 Stored = Inst;
1612}