blob: 5c5701ac792dc0001eda79321b71ec79d9c64d57 [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 Gregorff6cbdf2009-07-01 22:01:06 +0000342 // FIXME: In all of these cases, we need to show the template arguments
Douglas Gregor4ea568f2009-03-10 18:03:33 +0000343 for (llvm::SmallVector<ActiveTemplateInstantiation, 16>::reverse_iterator
344 Active = ActiveTemplateInstantiations.rbegin(),
345 ActiveEnd = ActiveTemplateInstantiations.rend();
346 Active != ActiveEnd;
347 ++Active) {
Douglas Gregor79cf6032009-03-10 20:44:00 +0000348 switch (Active->Kind) {
349 case ActiveTemplateInstantiation::TemplateInstantiation: {
Douglas Gregor85673582009-05-18 17:01:57 +0000350 Decl *D = reinterpret_cast<Decl *>(Active->Entity);
351 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) {
352 unsigned DiagID = diag::note_template_member_class_here;
353 if (isa<ClassTemplateSpecializationDecl>(Record))
354 DiagID = diag::note_template_class_instantiation_here;
Mike Stump11289f42009-09-09 15:08:12 +0000355 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
Douglas Gregor85673582009-05-18 17:01:57 +0000356 DiagID)
357 << Context.getTypeDeclType(Record)
358 << Active->InstantiationRange;
Douglas Gregora6ef8f02009-07-24 20:34:43 +0000359 } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
Douglas Gregor4adbc6d2009-06-26 00:10:03 +0000360 unsigned DiagID;
361 if (Function->getPrimaryTemplate())
362 DiagID = diag::note_function_template_spec_here;
363 else
364 DiagID = diag::note_template_member_function_here;
Mike Stump11289f42009-09-09 15:08:12 +0000365 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
Douglas Gregor85673582009-05-18 17:01:57 +0000366 DiagID)
367 << Function
368 << Active->InstantiationRange;
Douglas Gregora6ef8f02009-07-24 20:34:43 +0000369 } else {
370 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
371 diag::note_template_static_data_member_def_here)
372 << cast<VarDecl>(D)
373 << Active->InstantiationRange;
Douglas Gregor85673582009-05-18 17:01:57 +0000374 }
Douglas Gregor79cf6032009-03-10 20:44:00 +0000375 break;
376 }
377
378 case ActiveTemplateInstantiation::DefaultTemplateArgumentInstantiation: {
379 TemplateDecl *Template = cast<TemplateDecl>((Decl *)Active->Entity);
380 std::string TemplateArgsStr
Douglas Gregordc572a32009-03-30 22:58:21 +0000381 = TemplateSpecializationType::PrintTemplateArgumentList(
Mike Stump11289f42009-09-09 15:08:12 +0000382 Active->TemplateArgs,
Douglas Gregor7de59662009-05-29 20:38:28 +0000383 Active->NumTemplateArgs,
384 Context.PrintingPolicy);
Douglas Gregor79cf6032009-03-10 20:44:00 +0000385 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
386 diag::note_default_arg_instantiation_here)
387 << (Template->getNameAsString() + TemplateArgsStr)
388 << Active->InstantiationRange;
389 break;
390 }
Douglas Gregor637d9982009-06-10 23:47:09 +0000391
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000392 case ActiveTemplateInstantiation::ExplicitTemplateArgumentSubstitution: {
Mike Stump11289f42009-09-09 15:08:12 +0000393 FunctionTemplateDecl *FnTmpl
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000394 = cast<FunctionTemplateDecl>((Decl *)Active->Entity);
Douglas Gregor637d9982009-06-10 23:47:09 +0000395 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000396 diag::note_explicit_template_arg_substitution_here)
397 << FnTmpl << Active->InstantiationRange;
Douglas Gregor637d9982009-06-10 23:47:09 +0000398 break;
399 }
Mike Stump11289f42009-09-09 15:08:12 +0000400
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000401 case ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution:
402 if (ClassTemplatePartialSpecializationDecl *PartialSpec
403 = dyn_cast<ClassTemplatePartialSpecializationDecl>(
404 (Decl *)Active->Entity)) {
405 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
406 diag::note_partial_spec_deduct_instantiation_here)
407 << Context.getTypeDeclType(PartialSpec)
408 << Active->InstantiationRange;
409 } else {
410 FunctionTemplateDecl *FnTmpl
411 = cast<FunctionTemplateDecl>((Decl *)Active->Entity);
412 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
413 diag::note_function_template_deduction_instantiation_here)
414 << FnTmpl << Active->InstantiationRange;
415 }
416 break;
Douglas Gregor637d9982009-06-10 23:47:09 +0000417
Anders Carlsson657bad42009-09-05 05:14:19 +0000418 case ActiveTemplateInstantiation::DefaultFunctionArgumentInstantiation: {
419 ParmVarDecl *Param = cast<ParmVarDecl>((Decl *)Active->Entity);
420 FunctionDecl *FD = cast<FunctionDecl>(Param->getDeclContext());
Mike Stump11289f42009-09-09 15:08:12 +0000421
Anders Carlsson657bad42009-09-05 05:14:19 +0000422 std::string TemplateArgsStr
423 = TemplateSpecializationType::PrintTemplateArgumentList(
Mike Stump11289f42009-09-09 15:08:12 +0000424 Active->TemplateArgs,
Anders Carlsson657bad42009-09-05 05:14:19 +0000425 Active->NumTemplateArgs,
426 Context.PrintingPolicy);
427 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
428 diag::note_default_function_arg_instantiation_here)
Anders Carlssondc6d2c32009-09-05 05:38:54 +0000429 << (FD->getNameAsString() + TemplateArgsStr)
Anders Carlsson657bad42009-09-05 05:14:19 +0000430 << Active->InstantiationRange;
431 break;
432 }
Mike Stump11289f42009-09-09 15:08:12 +0000433
Douglas Gregore62e6a02009-11-11 19:13:48 +0000434 case ActiveTemplateInstantiation::PriorTemplateArgumentSubstitution: {
435 NamedDecl *Parm = cast<NamedDecl>((Decl *)Active->Entity);
436 std::string Name;
437 if (!Parm->getName().empty())
438 Name = std::string(" '") + Parm->getName().str() + "'";
439
440 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
441 diag::note_prior_template_arg_substitution)
442 << isa<TemplateTemplateParmDecl>(Parm)
443 << Name
444 << getTemplateArgumentBindingsText(
445 Active->Template->getTemplateParameters(),
446 Active->TemplateArgs,
447 Active->NumTemplateArgs)
448 << Active->InstantiationRange;
449 break;
450 }
Douglas Gregor84d49a22009-11-11 21:54:23 +0000451
452 case ActiveTemplateInstantiation::DefaultTemplateArgumentChecking: {
453 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
454 diag::note_template_default_arg_checking)
455 << getTemplateArgumentBindingsText(
456 Active->Template->getTemplateParameters(),
457 Active->TemplateArgs,
458 Active->NumTemplateArgs)
459 << Active->InstantiationRange;
460 break;
461 }
Douglas Gregor79cf6032009-03-10 20:44:00 +0000462 }
Douglas Gregor4ea568f2009-03-10 18:03:33 +0000463 }
464}
465
Douglas Gregor33834512009-06-14 07:33:30 +0000466bool Sema::isSFINAEContext() const {
467 using llvm::SmallVector;
468 for (SmallVector<ActiveTemplateInstantiation, 16>::const_reverse_iterator
469 Active = ActiveTemplateInstantiations.rbegin(),
470 ActiveEnd = ActiveTemplateInstantiations.rend();
471 Active != ActiveEnd;
Douglas Gregor84d49a22009-11-11 21:54:23 +0000472 ++Active)
473 {
Douglas Gregor33834512009-06-14 07:33:30 +0000474 switch(Active->Kind) {
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000475 case ActiveTemplateInstantiation::TemplateInstantiation:
Anders Carlsson657bad42009-09-05 05:14:19 +0000476 case ActiveTemplateInstantiation::DefaultFunctionArgumentInstantiation:
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000477 // This is a template instantiation, so there is no SFINAE.
478 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000479
Douglas Gregor33834512009-06-14 07:33:30 +0000480 case ActiveTemplateInstantiation::DefaultTemplateArgumentInstantiation:
Douglas Gregore62e6a02009-11-11 19:13:48 +0000481 case ActiveTemplateInstantiation::PriorTemplateArgumentSubstitution:
Douglas Gregor84d49a22009-11-11 21:54:23 +0000482 case ActiveTemplateInstantiation::DefaultTemplateArgumentChecking:
Douglas Gregore62e6a02009-11-11 19:13:48 +0000483 // A default template argument instantiation and substitution into
484 // template parameters with arguments for prior parameters may or may
485 // not be a SFINAE context; look further up the stack.
Douglas Gregor33834512009-06-14 07:33:30 +0000486 break;
Mike Stump11289f42009-09-09 15:08:12 +0000487
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000488 case ActiveTemplateInstantiation::ExplicitTemplateArgumentSubstitution:
489 case ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution:
490 // We're either substitution explicitly-specified template arguments
491 // or deduced template arguments, so SFINAE applies.
492 return true;
Douglas Gregor33834512009-06-14 07:33:30 +0000493 }
494 }
495
496 return false;
497}
498
Douglas Gregorfe1e1102009-02-27 19:31:52 +0000499//===----------------------------------------------------------------------===/
500// Template Instantiation for Types
501//===----------------------------------------------------------------------===/
Douglas Gregor17c0d7b2009-02-28 00:25:32 +0000502namespace {
Benjamin Kramer337e3a52009-11-28 19:45:26 +0000503 class TemplateInstantiator
Mike Stump11289f42009-09-09 15:08:12 +0000504 : public TreeTransform<TemplateInstantiator> {
Douglas Gregor01afeef2009-08-28 20:31:08 +0000505 const MultiLevelTemplateArgumentList &TemplateArgs;
Douglas Gregor17c0d7b2009-02-28 00:25:32 +0000506 SourceLocation Loc;
507 DeclarationName Entity;
Douglas Gregorfe1e1102009-02-27 19:31:52 +0000508
Douglas Gregor17c0d7b2009-02-28 00:25:32 +0000509 public:
Douglas Gregorebe10102009-08-20 07:17:43 +0000510 typedef TreeTransform<TemplateInstantiator> inherited;
Mike Stump11289f42009-09-09 15:08:12 +0000511
512 TemplateInstantiator(Sema &SemaRef,
Douglas Gregor01afeef2009-08-28 20:31:08 +0000513 const MultiLevelTemplateArgumentList &TemplateArgs,
Douglas Gregord6ff3322009-08-04 16:50:30 +0000514 SourceLocation Loc,
Mike Stump11289f42009-09-09 15:08:12 +0000515 DeclarationName Entity)
516 : inherited(SemaRef), TemplateArgs(TemplateArgs), Loc(Loc),
Douglas Gregorebe10102009-08-20 07:17:43 +0000517 Entity(Entity) { }
Douglas Gregor17c0d7b2009-02-28 00:25:32 +0000518
Mike Stump11289f42009-09-09 15:08:12 +0000519 /// \brief Determine whether the given type \p T has already been
Douglas Gregord6ff3322009-08-04 16:50:30 +0000520 /// transformed.
521 ///
522 /// For the purposes of template instantiation, a type has already been
523 /// transformed if it is NULL or if it is not dependent.
524 bool AlreadyTransformed(QualType T) {
525 return T.isNull() || !T->isDependentType();
Douglas Gregorf61eca92009-05-13 18:28:20 +0000526 }
Mike Stump11289f42009-09-09 15:08:12 +0000527
Douglas Gregord6ff3322009-08-04 16:50:30 +0000528 /// \brief Returns the location of the entity being instantiated, if known.
529 SourceLocation getBaseLocation() { return Loc; }
Mike Stump11289f42009-09-09 15:08:12 +0000530
Douglas Gregord6ff3322009-08-04 16:50:30 +0000531 /// \brief Returns the name of the entity being instantiated, if any.
532 DeclarationName getBaseEntity() { return Entity; }
Mike Stump11289f42009-09-09 15:08:12 +0000533
Douglas Gregoref6ab412009-10-27 06:26:26 +0000534 /// \brief Sets the "base" location and entity when that
535 /// information is known based on another transformation.
536 void setBase(SourceLocation Loc, DeclarationName Entity) {
537 this->Loc = Loc;
538 this->Entity = Entity;
539 }
540
Douglas Gregord6ff3322009-08-04 16:50:30 +0000541 /// \brief Transform the given declaration by instantiating a reference to
542 /// this declaration.
543 Decl *TransformDecl(Decl *D);
Douglas Gregora16548e2009-08-11 05:31:07 +0000544
Mike Stump11289f42009-09-09 15:08:12 +0000545 /// \brief Transform the definition of the given declaration by
Douglas Gregorebe10102009-08-20 07:17:43 +0000546 /// instantiating it.
547 Decl *TransformDefinition(Decl *D);
Mike Stump11289f42009-09-09 15:08:12 +0000548
Douglas Gregora5cb6da2009-10-20 05:58:46 +0000549 /// \bried Transform the first qualifier within a scope by instantiating the
550 /// declaration.
551 NamedDecl *TransformFirstQualifierInScope(NamedDecl *D, SourceLocation Loc);
552
Douglas Gregorebe10102009-08-20 07:17:43 +0000553 /// \brief Rebuild the exception declaration and register the declaration
554 /// as an instantiated local.
Mike Stump11289f42009-09-09 15:08:12 +0000555 VarDecl *RebuildExceptionDecl(VarDecl *ExceptionDecl, QualType T,
John McCallbcd03502009-12-07 02:54:59 +0000556 TypeSourceInfo *Declarator,
Douglas Gregorebe10102009-08-20 07:17:43 +0000557 IdentifierInfo *Name,
558 SourceLocation Loc, SourceRange TypeRange);
Mike Stump11289f42009-09-09 15:08:12 +0000559
John McCall7f41d982009-09-11 04:59:25 +0000560 /// \brief Check for tag mismatches when instantiating an
561 /// elaborated type.
562 QualType RebuildElaboratedType(QualType T, ElaboratedType::TagKind Tag);
563
John McCall47f29ea2009-12-08 09:21:05 +0000564 Sema::OwningExprResult TransformPredefinedExpr(PredefinedExpr *E);
565 Sema::OwningExprResult TransformDeclRefExpr(DeclRefExpr *E);
John McCall47f29ea2009-12-08 09:21:05 +0000566 Sema::OwningExprResult TransformCXXDefaultArgExpr(CXXDefaultArgExpr *E);
Sebastian Redl14236c82009-11-08 13:56:19 +0000567
Mike Stump11289f42009-09-09 15:08:12 +0000568 /// \brief Transforms a template type parameter type by performing
Douglas Gregord6ff3322009-08-04 16:50:30 +0000569 /// substitution of the corresponding template type argument.
John McCall550e0c22009-10-21 00:40:46 +0000570 QualType TransformTemplateTypeParmType(TypeLocBuilder &TLB,
571 TemplateTypeParmTypeLoc TL);
Douglas Gregord6ff3322009-08-04 16:50:30 +0000572 };
Douglas Gregor04318252009-07-06 15:59:29 +0000573}
574
Douglas Gregord6ff3322009-08-04 16:50:30 +0000575Decl *TemplateInstantiator::TransformDecl(Decl *D) {
Douglas Gregor2b6ca462009-09-03 21:38:09 +0000576 if (!D)
577 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000578
Douglas Gregor2b6ca462009-09-03 21:38:09 +0000579 if (TemplateTemplateParmDecl *TTP = dyn_cast<TemplateTemplateParmDecl>(D)) {
Douglas Gregor01afeef2009-08-28 20:31:08 +0000580 if (TTP->getDepth() < TemplateArgs.getNumLevels()) {
Douglas Gregor9167f8b2009-11-11 01:00:40 +0000581 TemplateName Template
582 = TemplateArgs(TTP->getDepth(), TTP->getPosition()).getAsTemplate();
583 assert(!Template.isNull() && Template.getAsTemplateDecl() &&
Douglas Gregor01afeef2009-08-28 20:31:08 +0000584 "Wrong kind of template template argument");
Douglas Gregor9167f8b2009-11-11 01:00:40 +0000585 return Template.getAsTemplateDecl();
Douglas Gregor01afeef2009-08-28 20:31:08 +0000586 }
Mike Stump11289f42009-09-09 15:08:12 +0000587
588 // If the corresponding template argument is NULL or non-existent, it's
589 // because we are performing instantiation from explicitly-specified
Douglas Gregor01afeef2009-08-28 20:31:08 +0000590 // template arguments in a function template, but there were some
591 // arguments left unspecified.
Mike Stump11289f42009-09-09 15:08:12 +0000592 if (!TemplateArgs.hasTemplateArgument(TTP->getDepth(),
Douglas Gregor01afeef2009-08-28 20:31:08 +0000593 TTP->getPosition()))
594 return D;
Mike Stump11289f42009-09-09 15:08:12 +0000595
Douglas Gregor9167f8b2009-11-11 01:00:40 +0000596 // Fall through to find the instantiated declaration for this template
597 // template parameter.
Douglas Gregor71dc5092009-08-06 06:41:21 +0000598 }
Mike Stump11289f42009-09-09 15:08:12 +0000599
Douglas Gregor64621e62009-09-16 18:34:49 +0000600 return SemaRef.FindInstantiatedDecl(cast<NamedDecl>(D), TemplateArgs);
Douglas Gregord6ff3322009-08-04 16:50:30 +0000601}
602
Douglas Gregorebe10102009-08-20 07:17:43 +0000603Decl *TemplateInstantiator::TransformDefinition(Decl *D) {
John McCall76d824f2009-08-25 22:02:44 +0000604 Decl *Inst = getSema().SubstDecl(D, getSema().CurContext, TemplateArgs);
Douglas Gregorebe10102009-08-20 07:17:43 +0000605 if (!Inst)
606 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000607
Douglas Gregorebe10102009-08-20 07:17:43 +0000608 getSema().CurrentInstantiationScope->InstantiatedLocal(D, Inst);
609 return Inst;
610}
611
Douglas Gregora5cb6da2009-10-20 05:58:46 +0000612NamedDecl *
613TemplateInstantiator::TransformFirstQualifierInScope(NamedDecl *D,
614 SourceLocation Loc) {
615 // If the first part of the nested-name-specifier was a template type
616 // parameter, instantiate that type parameter down to a tag type.
617 if (TemplateTypeParmDecl *TTPD = dyn_cast_or_null<TemplateTypeParmDecl>(D)) {
618 const TemplateTypeParmType *TTP
619 = cast<TemplateTypeParmType>(getSema().Context.getTypeDeclType(TTPD));
620 if (TTP->getDepth() < TemplateArgs.getNumLevels()) {
621 QualType T = TemplateArgs(TTP->getDepth(), TTP->getIndex()).getAsType();
622 if (T.isNull())
623 return cast_or_null<NamedDecl>(TransformDecl(D));
624
625 if (const TagType *Tag = T->getAs<TagType>())
626 return Tag->getDecl();
627
628 // The resulting type is not a tag; complain.
629 getSema().Diag(Loc, diag::err_nested_name_spec_non_tag) << T;
630 return 0;
631 }
632 }
633
634 return cast_or_null<NamedDecl>(TransformDecl(D));
635}
636
Douglas Gregorebe10102009-08-20 07:17:43 +0000637VarDecl *
638TemplateInstantiator::RebuildExceptionDecl(VarDecl *ExceptionDecl,
Mike Stump11289f42009-09-09 15:08:12 +0000639 QualType T,
John McCallbcd03502009-12-07 02:54:59 +0000640 TypeSourceInfo *Declarator,
Douglas Gregorebe10102009-08-20 07:17:43 +0000641 IdentifierInfo *Name,
Mike Stump11289f42009-09-09 15:08:12 +0000642 SourceLocation Loc,
Douglas Gregorebe10102009-08-20 07:17:43 +0000643 SourceRange TypeRange) {
644 VarDecl *Var = inherited::RebuildExceptionDecl(ExceptionDecl, T, Declarator,
645 Name, Loc, TypeRange);
646 if (Var && !Var->isInvalidDecl())
647 getSema().CurrentInstantiationScope->InstantiatedLocal(ExceptionDecl, Var);
648 return Var;
649}
650
John McCall7f41d982009-09-11 04:59:25 +0000651QualType
652TemplateInstantiator::RebuildElaboratedType(QualType T,
653 ElaboratedType::TagKind Tag) {
654 if (const TagType *TT = T->getAs<TagType>()) {
655 TagDecl* TD = TT->getDecl();
656
657 // FIXME: this location is very wrong; we really need typelocs.
658 SourceLocation TagLocation = TD->getTagKeywordLoc();
659
660 // FIXME: type might be anonymous.
661 IdentifierInfo *Id = TD->getIdentifier();
662
663 // TODO: should we even warn on struct/class mismatches for this? Seems
664 // like it's likely to produce a lot of spurious errors.
665 if (!SemaRef.isAcceptableTagRedeclaration(TD, Tag, TagLocation, *Id)) {
666 SemaRef.Diag(TagLocation, diag::err_use_with_wrong_tag)
667 << Id
668 << CodeModificationHint::CreateReplacement(SourceRange(TagLocation),
669 TD->getKindName());
670 SemaRef.Diag(TD->getLocation(), diag::note_previous_use);
671 }
672 }
673
674 return TreeTransform<TemplateInstantiator>::RebuildElaboratedType(T, Tag);
675}
676
677Sema::OwningExprResult
John McCall47f29ea2009-12-08 09:21:05 +0000678TemplateInstantiator::TransformPredefinedExpr(PredefinedExpr *E) {
Anders Carlsson0b209a82009-09-11 01:22:35 +0000679 if (!E->isTypeDependent())
680 return SemaRef.Owned(E->Retain());
681
682 FunctionDecl *currentDecl = getSema().getCurFunctionDecl();
683 assert(currentDecl && "Must have current function declaration when "
684 "instantiating.");
685
686 PredefinedExpr::IdentType IT = E->getIdentType();
687
688 unsigned Length =
689 PredefinedExpr::ComputeName(getSema().Context, IT, currentDecl).length();
690
691 llvm::APInt LengthI(32, Length + 1);
John McCall8ccfcb52009-09-24 19:53:00 +0000692 QualType ResTy = getSema().Context.CharTy.withConst();
Anders Carlsson0b209a82009-09-11 01:22:35 +0000693 ResTy = getSema().Context.getConstantArrayType(ResTy, LengthI,
694 ArrayType::Normal, 0);
695 PredefinedExpr *PE =
696 new (getSema().Context) PredefinedExpr(E->getLocation(), ResTy, IT);
697 return getSema().Owned(PE);
698}
699
700Sema::OwningExprResult
John McCall47f29ea2009-12-08 09:21:05 +0000701TemplateInstantiator::TransformDeclRefExpr(DeclRefExpr *E) {
Douglas Gregora16548e2009-08-11 05:31:07 +0000702 // FIXME: Clean this up a bit
703 NamedDecl *D = E->getDecl();
704 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(D)) {
Douglas Gregor954de172009-10-31 17:21:17 +0000705 if (NTTP->getDepth() < TemplateArgs.getNumLevels()) {
Douglas Gregor954de172009-10-31 17:21:17 +0000706 // If the corresponding template argument is NULL or non-existent, it's
707 // because we are performing instantiation from explicitly-specified
708 // template arguments in a function template, but there were some
709 // arguments left unspecified.
710 if (!TemplateArgs.hasTemplateArgument(NTTP->getDepth(),
711 NTTP->getPosition()))
712 return SemaRef.Owned(E->Retain());
Mike Stump11289f42009-09-09 15:08:12 +0000713
Douglas Gregor954de172009-10-31 17:21:17 +0000714 const TemplateArgument &Arg = TemplateArgs(NTTP->getDepth(),
715 NTTP->getPosition());
Mike Stump11289f42009-09-09 15:08:12 +0000716
Douglas Gregor954de172009-10-31 17:21:17 +0000717 // The template argument itself might be an expression, in which
718 // case we just return that expression.
719 if (Arg.getKind() == TemplateArgument::Expression)
720 return SemaRef.Owned(Arg.getAsExpr()->Retain());
Mike Stump11289f42009-09-09 15:08:12 +0000721
Douglas Gregor954de172009-10-31 17:21:17 +0000722 if (Arg.getKind() == TemplateArgument::Declaration) {
723 ValueDecl *VD = cast<ValueDecl>(Arg.getAsDecl());
Mike Stump11289f42009-09-09 15:08:12 +0000724
Douglas Gregor954de172009-10-31 17:21:17 +0000725 VD = cast_or_null<ValueDecl>(
Douglas Gregorc95a1fa2009-11-04 07:01:15 +0000726 getSema().FindInstantiatedDecl(VD, TemplateArgs));
Douglas Gregor954de172009-10-31 17:21:17 +0000727 if (!VD)
728 return SemaRef.ExprError();
Mike Stump11289f42009-09-09 15:08:12 +0000729
Douglas Gregoreca8f5a2010-02-04 17:21:48 +0000730 if (VD->getDeclContext()->isRecord() &&
731 (isa<CXXMethodDecl>(VD) || isa<FieldDecl>(VD))) {
Douglas Gregor4e948ce2009-11-12 17:40:13 +0000732 // If the value is a class member, we might have a pointer-to-member.
733 // Determine whether the non-type template template parameter is of
734 // pointer-to-member type. If so, we need to build an appropriate
735 // expression for a pointer-to-member, since a "normal" DeclRefExpr
736 // would refer to the member itself.
737 if (NTTP->getType()->isMemberPointerType()) {
738 QualType ClassType
739 = SemaRef.Context.getTypeDeclType(
740 cast<RecordDecl>(VD->getDeclContext()));
741 NestedNameSpecifier *Qualifier
742 = NestedNameSpecifier::Create(SemaRef.Context, 0, false,
743 ClassType.getTypePtr());
744 CXXScopeSpec SS;
745 SS.setScopeRep(Qualifier);
746 OwningExprResult RefExpr
747 = SemaRef.BuildDeclRefExpr(VD,
748 VD->getType().getNonReferenceType(),
749 E->getLocation(),
Douglas Gregor4e948ce2009-11-12 17:40:13 +0000750 &SS);
751 if (RefExpr.isInvalid())
752 return SemaRef.ExprError();
753
754 return SemaRef.CreateBuiltinUnaryOp(E->getLocation(),
755 UnaryOperator::AddrOf,
756 move(RefExpr));
757 }
758 }
Douglas Gregoreca8f5a2010-02-04 17:21:48 +0000759 if (NTTP->getType()->isPointerType()) {
760 // If the template argument is expected to be a pointer
761 // type, we may have to decay array/pointer references, take
762 // the address of the argument, or perform cv-qualification
763 // adjustments to get the type of the rvalue right. Do so.
Chandler Carruth9b1fa252010-01-31 07:09:11 +0000764 OwningExprResult RefExpr
765 = SemaRef.BuildDeclRefExpr(VD, VD->getType().getNonReferenceType(),
766 E->getLocation());
767 if (RefExpr.isInvalid())
768 return SemaRef.ExprError();
769
Douglas Gregoreca8f5a2010-02-04 17:21:48 +0000770 // Decay functions and arrays.
771 Expr *RefE = (Expr *)RefExpr.get();
772 SemaRef.DefaultFunctionArrayConversion(RefE);
773 if (RefE != RefExpr.get()) {
774 RefExpr.release();
775 RefExpr = SemaRef.Owned(RefE);
776 }
777
778 // If the unqualified types are different and a a
779 // qualification conversion won't fix them types, we need to
780 // take the address. FIXME: Should we encode these steps in
781 // the template argument, then replay them here, like a
782 // miniature InitializationSequence?
783 if (!SemaRef.Context.hasSameUnqualifiedType(RefE->getType(),
784 NTTP->getType()) &&
785 !SemaRef.IsQualificationConversion(RefE->getType(),
786 NTTP->getType())) {
787 RefExpr = SemaRef.CreateBuiltinUnaryOp(E->getLocation(),
788 UnaryOperator::AddrOf,
789 move(RefExpr));
790 if (RefExpr.isInvalid())
791 return SemaRef.ExprError();
792
793 RefE = (Expr *)RefExpr.get();
794 assert(SemaRef.IsQualificationConversion(RefE->getType(),
795 NTTP->getType()));
796 }
797
798 // Strip top-level cv-qualifiers off the type.
799 RefExpr.release();
800 SemaRef.ImpCastExprToType(RefE,
801 NTTP->getType().getUnqualifiedType(),
802 CastExpr::CK_NoOp);
803 return SemaRef.Owned(RefE);
Chandler Carruth9b1fa252010-01-31 07:09:11 +0000804 }
Douglas Gregor4e948ce2009-11-12 17:40:13 +0000805
806 return SemaRef.BuildDeclRefExpr(VD, VD->getType().getNonReferenceType(),
Douglas Gregored6c7442009-11-23 11:41:28 +0000807 E->getLocation());
Douglas Gregor954de172009-10-31 17:21:17 +0000808 }
Mike Stump11289f42009-09-09 15:08:12 +0000809
Douglas Gregor954de172009-10-31 17:21:17 +0000810 assert(Arg.getKind() == TemplateArgument::Integral);
811 QualType T = Arg.getIntegralType();
812 if (T->isCharType() || T->isWideCharType())
813 return SemaRef.Owned(new (SemaRef.Context) CharacterLiteral(
814 Arg.getAsIntegral()->getZExtValue(),
815 T->isWideCharType(),
Mike Stump11289f42009-09-09 15:08:12 +0000816 T,
Douglas Gregor01afeef2009-08-28 20:31:08 +0000817 E->getSourceRange().getBegin()));
Douglas Gregor954de172009-10-31 17:21:17 +0000818 if (T->isBooleanType())
819 return SemaRef.Owned(new (SemaRef.Context) CXXBoolLiteralExpr(
820 Arg.getAsIntegral()->getBoolValue(),
821 T,
822 E->getSourceRange().getBegin()));
823
824 assert(Arg.getAsIntegral()->getBitWidth() == SemaRef.Context.getIntWidth(T));
825 return SemaRef.Owned(new (SemaRef.Context) IntegerLiteral(
826 *Arg.getAsIntegral(),
827 T,
828 E->getSourceRange().getBegin()));
829 }
830
831 // We have a non-type template parameter that isn't fully substituted;
832 // FindInstantiatedDecl will find it in the local instantiation scope.
Douglas Gregora16548e2009-08-11 05:31:07 +0000833 }
Mike Stump11289f42009-09-09 15:08:12 +0000834
John McCall47f29ea2009-12-08 09:21:05 +0000835 return TreeTransform<TemplateInstantiator>::TransformDeclRefExpr(E);
Douglas Gregora16548e2009-08-11 05:31:07 +0000836}
837
Sebastian Redl14236c82009-11-08 13:56:19 +0000838Sema::OwningExprResult TemplateInstantiator::TransformCXXDefaultArgExpr(
John McCall47f29ea2009-12-08 09:21:05 +0000839 CXXDefaultArgExpr *E) {
Sebastian Redl14236c82009-11-08 13:56:19 +0000840 assert(!cast<FunctionDecl>(E->getParam()->getDeclContext())->
841 getDescribedFunctionTemplate() &&
842 "Default arg expressions are never formed in dependent cases.");
Douglas Gregor033f6752009-12-23 23:03:06 +0000843 return SemaRef.BuildCXXDefaultArgExpr(E->getUsedLocation(),
844 cast<FunctionDecl>(E->getParam()->getDeclContext()),
845 E->getParam());
Sebastian Redl14236c82009-11-08 13:56:19 +0000846}
847
848
Mike Stump11289f42009-09-09 15:08:12 +0000849QualType
John McCall550e0c22009-10-21 00:40:46 +0000850TemplateInstantiator::TransformTemplateTypeParmType(TypeLocBuilder &TLB,
851 TemplateTypeParmTypeLoc TL) {
852 TemplateTypeParmType *T = TL.getTypePtr();
Douglas Gregor01afeef2009-08-28 20:31:08 +0000853 if (T->getDepth() < TemplateArgs.getNumLevels()) {
Douglas Gregorfe1e1102009-02-27 19:31:52 +0000854 // Replace the template type parameter with its corresponding
855 // template argument.
Mike Stump11289f42009-09-09 15:08:12 +0000856
857 // If the corresponding template argument is NULL or doesn't exist, it's
858 // because we are performing instantiation from explicitly-specified
859 // template arguments in a function template class, but there were some
Douglas Gregore3f1f352009-07-01 00:28:38 +0000860 // arguments left unspecified.
John McCall550e0c22009-10-21 00:40:46 +0000861 if (!TemplateArgs.hasTemplateArgument(T->getDepth(), T->getIndex())) {
862 TemplateTypeParmTypeLoc NewTL
863 = TLB.push<TemplateTypeParmTypeLoc>(TL.getType());
864 NewTL.setNameLoc(TL.getNameLoc());
865 return TL.getType();
866 }
Mike Stump11289f42009-09-09 15:08:12 +0000867
868 assert(TemplateArgs(T->getDepth(), T->getIndex()).getKind()
Douglas Gregor01afeef2009-08-28 20:31:08 +0000869 == TemplateArgument::Type &&
Douglas Gregorfe1e1102009-02-27 19:31:52 +0000870 "Template argument kind mismatch");
Douglas Gregor01afeef2009-08-28 20:31:08 +0000871
John McCallcebee162009-10-18 09:09:24 +0000872 QualType Replacement
873 = TemplateArgs(T->getDepth(), T->getIndex()).getAsType();
874
875 // TODO: only do this uniquing once, at the start of instantiation.
John McCall550e0c22009-10-21 00:40:46 +0000876 QualType Result
877 = getSema().Context.getSubstTemplateTypeParmType(T, Replacement);
878 SubstTemplateTypeParmTypeLoc NewTL
879 = TLB.push<SubstTemplateTypeParmTypeLoc>(Result);
880 NewTL.setNameLoc(TL.getNameLoc());
881 return Result;
Mike Stump11289f42009-09-09 15:08:12 +0000882 }
Douglas Gregorfe1e1102009-02-27 19:31:52 +0000883
884 // The template type parameter comes from an inner template (e.g.,
885 // the template parameter list of a member template inside the
886 // template we are instantiating). Create a new template type
887 // parameter with the template "level" reduced by one.
John McCall550e0c22009-10-21 00:40:46 +0000888 QualType Result
889 = getSema().Context.getTemplateTypeParmType(T->getDepth()
890 - TemplateArgs.getNumLevels(),
891 T->getIndex(),
892 T->isParameterPack(),
893 T->getName());
894 TemplateTypeParmTypeLoc NewTL = TLB.push<TemplateTypeParmTypeLoc>(Result);
895 NewTL.setNameLoc(TL.getNameLoc());
896 return Result;
Douglas Gregor17c0d7b2009-02-28 00:25:32 +0000897}
Douglas Gregorfe1e1102009-02-27 19:31:52 +0000898
John McCall76d824f2009-08-25 22:02:44 +0000899/// \brief Perform substitution on the type T with a given set of template
900/// arguments.
Douglas Gregorfe1e1102009-02-27 19:31:52 +0000901///
902/// This routine substitutes the given template arguments into the
903/// type T and produces the instantiated type.
904///
905/// \param T the type into which the template arguments will be
906/// substituted. If this type is not dependent, it will be returned
907/// immediately.
908///
909/// \param TemplateArgs the template arguments that will be
910/// substituted for the top-level template parameters within T.
911///
Douglas Gregorfe1e1102009-02-27 19:31:52 +0000912/// \param Loc the location in the source code where this substitution
913/// is being performed. It will typically be the location of the
914/// declarator (if we're instantiating the type of some declaration)
915/// or the location of the type in the source code (if, e.g., we're
916/// instantiating the type of a cast expression).
917///
918/// \param Entity the name of the entity associated with a declaration
919/// being instantiated (if any). May be empty to indicate that there
920/// is no such entity (if, e.g., this is a type that occurs as part of
921/// a cast expression) or that the entity has no name (e.g., an
922/// unnamed function parameter).
923///
924/// \returns If the instantiation succeeds, the instantiated
925/// type. Otherwise, produces diagnostics and returns a NULL type.
John McCallbcd03502009-12-07 02:54:59 +0000926TypeSourceInfo *Sema::SubstType(TypeSourceInfo *T,
John McCall609459e2009-10-21 00:58:09 +0000927 const MultiLevelTemplateArgumentList &Args,
928 SourceLocation Loc,
929 DeclarationName Entity) {
930 assert(!ActiveTemplateInstantiations.empty() &&
931 "Cannot perform an instantiation without some context on the "
932 "instantiation stack");
933
934 if (!T->getType()->isDependentType())
935 return T;
936
937 TemplateInstantiator Instantiator(*this, Args, Loc, Entity);
938 return Instantiator.TransformType(T);
939}
940
941/// Deprecated form of the above.
Mike Stump11289f42009-09-09 15:08:12 +0000942QualType Sema::SubstType(QualType T,
Douglas Gregor01afeef2009-08-28 20:31:08 +0000943 const MultiLevelTemplateArgumentList &TemplateArgs,
John McCall76d824f2009-08-25 22:02:44 +0000944 SourceLocation Loc, DeclarationName Entity) {
Douglas Gregor79cf6032009-03-10 20:44:00 +0000945 assert(!ActiveTemplateInstantiations.empty() &&
946 "Cannot perform an instantiation without some context on the "
947 "instantiation stack");
948
Douglas Gregorfe1e1102009-02-27 19:31:52 +0000949 // If T is not a dependent type, there is nothing to do.
950 if (!T->isDependentType())
951 return T;
952
Douglas Gregord6ff3322009-08-04 16:50:30 +0000953 TemplateInstantiator Instantiator(*this, TemplateArgs, Loc, Entity);
954 return Instantiator.TransformType(T);
Douglas Gregorfe1e1102009-02-27 19:31:52 +0000955}
Douglas Gregor463421d2009-03-03 04:44:36 +0000956
John McCall76d824f2009-08-25 22:02:44 +0000957/// \brief Perform substitution on the base class specifiers of the
958/// given class template specialization.
Douglas Gregor463421d2009-03-03 04:44:36 +0000959///
960/// Produces a diagnostic and returns true on error, returns false and
961/// attaches the instantiated base classes to the class template
962/// specialization if successful.
Mike Stump11289f42009-09-09 15:08:12 +0000963bool
John McCall76d824f2009-08-25 22:02:44 +0000964Sema::SubstBaseSpecifiers(CXXRecordDecl *Instantiation,
965 CXXRecordDecl *Pattern,
Douglas Gregor01afeef2009-08-28 20:31:08 +0000966 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor463421d2009-03-03 04:44:36 +0000967 bool Invalid = false;
Douglas Gregor6181ded2009-05-29 18:27:38 +0000968 llvm::SmallVector<CXXBaseSpecifier*, 4> InstantiatedBases;
Mike Stump11289f42009-09-09 15:08:12 +0000969 for (ClassTemplateSpecializationDecl::base_class_iterator
Douglas Gregor8ea8fd42009-03-25 21:17:03 +0000970 Base = Pattern->bases_begin(), BaseEnd = Pattern->bases_end();
Douglas Gregor2a72edd2009-03-10 18:52:44 +0000971 Base != BaseEnd; ++Base) {
Douglas Gregor463421d2009-03-03 04:44:36 +0000972 if (!Base->getType()->isDependentType()) {
Anders Carlssonae3c5cf2009-12-03 17:49:57 +0000973 const CXXRecordDecl *BaseDecl =
974 cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
975
976 // Make sure to set the attributes from the base.
977 SetClassDeclAttributesFromBase(Instantiation, BaseDecl,
978 Base->isVirtual());
979
Fariborz Jahanian5c14ec32009-07-22 17:41:53 +0000980 InstantiatedBases.push_back(new (Context) CXXBaseSpecifier(*Base));
Douglas Gregor463421d2009-03-03 04:44:36 +0000981 continue;
982 }
983
Mike Stump11289f42009-09-09 15:08:12 +0000984 QualType BaseType = SubstType(Base->getType(),
985 TemplateArgs,
John McCall76d824f2009-08-25 22:02:44 +0000986 Base->getSourceRange().getBegin(),
987 DeclarationName());
Douglas Gregor463421d2009-03-03 04:44:36 +0000988 if (BaseType.isNull()) {
989 Invalid = true;
990 continue;
991 }
992
993 if (CXXBaseSpecifier *InstantiatedBase
Douglas Gregor8ea8fd42009-03-25 21:17:03 +0000994 = CheckBaseSpecifier(Instantiation,
Douglas Gregor463421d2009-03-03 04:44:36 +0000995 Base->getSourceRange(),
996 Base->isVirtual(),
997 Base->getAccessSpecifierAsWritten(),
998 BaseType,
999 /*FIXME: Not totally accurate */
1000 Base->getSourceRange().getBegin()))
1001 InstantiatedBases.push_back(InstantiatedBase);
1002 else
1003 Invalid = true;
1004 }
1005
Douglas Gregor2a72edd2009-03-10 18:52:44 +00001006 if (!Invalid &&
Jay Foad7d0479f2009-05-21 09:52:38 +00001007 AttachBaseSpecifiers(Instantiation, InstantiatedBases.data(),
Douglas Gregor463421d2009-03-03 04:44:36 +00001008 InstantiatedBases.size()))
1009 Invalid = true;
1010
1011 return Invalid;
1012}
1013
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001014/// \brief Instantiate the definition of a class from a given pattern.
1015///
1016/// \param PointOfInstantiation The point of instantiation within the
1017/// source code.
1018///
1019/// \param Instantiation is the declaration whose definition is being
1020/// instantiated. This will be either a class template specialization
1021/// or a member class of a class template specialization.
1022///
1023/// \param Pattern is the pattern from which the instantiation
1024/// occurs. This will be either the declaration of a class template or
1025/// the declaration of a member class of a class template.
1026///
1027/// \param TemplateArgs The template arguments to be substituted into
1028/// the pattern.
1029///
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001030/// \param TSK the kind of implicit or explicit instantiation to perform.
Douglas Gregor8a2e6012009-08-24 15:23:48 +00001031///
1032/// \param Complain whether to complain if the class cannot be instantiated due
1033/// to the lack of a definition.
1034///
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001035/// \returns true if an error occurred, false otherwise.
1036bool
1037Sema::InstantiateClass(SourceLocation PointOfInstantiation,
1038 CXXRecordDecl *Instantiation, CXXRecordDecl *Pattern,
Douglas Gregor01afeef2009-08-28 20:31:08 +00001039 const MultiLevelTemplateArgumentList &TemplateArgs,
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001040 TemplateSpecializationKind TSK,
Douglas Gregor8a2e6012009-08-24 15:23:48 +00001041 bool Complain) {
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001042 bool Invalid = false;
John McCall87a44eb2009-08-20 01:44:21 +00001043
Mike Stump11289f42009-09-09 15:08:12 +00001044 CXXRecordDecl *PatternDef
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001045 = cast_or_null<CXXRecordDecl>(Pattern->getDefinition(Context));
1046 if (!PatternDef) {
Douglas Gregor8a2e6012009-08-24 15:23:48 +00001047 if (!Complain) {
1048 // Say nothing
1049 } else if (Pattern == Instantiation->getInstantiatedFromMemberClass()) {
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001050 Diag(PointOfInstantiation,
1051 diag::err_implicit_instantiate_member_undefined)
1052 << Context.getTypeDeclType(Instantiation);
1053 Diag(Pattern->getLocation(), diag::note_member_of_template_here);
1054 } else {
Douglas Gregora1f49972009-05-13 00:25:59 +00001055 Diag(PointOfInstantiation, diag::err_template_instantiate_undefined)
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001056 << (TSK != TSK_ImplicitInstantiation)
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001057 << Context.getTypeDeclType(Instantiation);
1058 Diag(Pattern->getLocation(), diag::note_template_decl_here);
1059 }
1060 return true;
1061 }
1062 Pattern = PatternDef;
1063
Douglas Gregord6ba93d2009-10-15 15:54:05 +00001064 // \brief Record the point of instantiation.
1065 if (MemberSpecializationInfo *MSInfo
1066 = Instantiation->getMemberSpecializationInfo()) {
1067 MSInfo->setTemplateSpecializationKind(TSK);
1068 MSInfo->setPointOfInstantiation(PointOfInstantiation);
Douglas Gregoref6ab412009-10-27 06:26:26 +00001069 } else if (ClassTemplateSpecializationDecl *Spec
1070 = dyn_cast<ClassTemplateSpecializationDecl>(Instantiation)) {
1071 Spec->setTemplateSpecializationKind(TSK);
1072 Spec->setPointOfInstantiation(PointOfInstantiation);
Douglas Gregord6ba93d2009-10-15 15:54:05 +00001073 }
1074
Douglas Gregorf3430ae2009-03-25 21:23:52 +00001075 InstantiatingTemplate Inst(*this, PointOfInstantiation, Instantiation);
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001076 if (Inst)
1077 return true;
1078
1079 // Enter the scope of this instantiation. We don't use
1080 // PushDeclContext because we don't have a scope.
1081 DeclContext *PreviousContext = CurContext;
1082 CurContext = Instantiation;
1083
1084 // Start the definition of this instantiation.
1085 Instantiation->startDefinition();
1086
John McCall76d824f2009-08-25 22:02:44 +00001087 // Do substitution on the base class specifiers.
1088 if (SubstBaseSpecifiers(Instantiation, Pattern, TemplateArgs))
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001089 Invalid = true;
1090
Douglas Gregor6181ded2009-05-29 18:27:38 +00001091 llvm::SmallVector<DeclPtrTy, 4> Fields;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001092 for (RecordDecl::decl_iterator Member = Pattern->decls_begin(),
Mike Stump11289f42009-09-09 15:08:12 +00001093 MemberEnd = Pattern->decls_end();
Douglas Gregorbcced4e2009-04-09 21:40:53 +00001094 Member != MemberEnd; ++Member) {
John McCall76d824f2009-08-25 22:02:44 +00001095 Decl *NewMember = SubstDecl(*Member, Instantiation, TemplateArgs);
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001096 if (NewMember) {
Eli Friedmand0e8de22009-12-07 00:22:08 +00001097 if (FieldDecl *Field = dyn_cast<FieldDecl>(NewMember))
Chris Lattner83f095c2009-03-28 19:18:32 +00001098 Fields.push_back(DeclPtrTy::make(Field));
Eli Friedmand0e8de22009-12-07 00:22:08 +00001099 else if (NewMember->isInvalidDecl())
1100 Invalid = true;
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001101 } else {
1102 // FIXME: Eventually, a NULL return will mean that one of the
Mike Stump87c57ac2009-05-16 07:39:55 +00001103 // instantiations was a semantic disaster, and we'll want to set Invalid =
1104 // true. For now, we expect to skip some members that we can't yet handle.
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001105 }
1106 }
1107
1108 // Finish checking fields.
Chris Lattner83f095c2009-03-28 19:18:32 +00001109 ActOnFields(0, Instantiation->getLocation(), DeclPtrTy::make(Instantiation),
Jay Foad7d0479f2009-05-21 09:52:38 +00001110 Fields.data(), Fields.size(), SourceLocation(), SourceLocation(),
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001111 0);
Douglas Gregorc99f1552009-12-03 18:33:45 +00001112 CheckCompletedCXXClass(Instantiation);
Douglas Gregor3c74d412009-10-14 20:14:33 +00001113 if (Instantiation->isInvalidDecl())
1114 Invalid = true;
1115
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001116 // Exit the scope of this instantiation.
1117 CurContext = PreviousContext;
1118
Douglas Gregor0a0f04d2010-01-06 04:44:19 +00001119 // If this is a polymorphic C++ class without a key function, we'll
1120 // have to mark all of the virtual members to allow emission of a vtable
1121 // in this translation unit.
1122 if (Instantiation->isDynamicClass() && !Context.getKeyFunction(Instantiation))
1123 ClassesWithUnmarkedVirtualMembers.push_back(std::make_pair(Instantiation,
1124 PointOfInstantiation));
1125
Douglas Gregor28ad4b52009-05-26 20:50:29 +00001126 if (!Invalid)
1127 Consumer.HandleTagDeclDefinition(Instantiation);
1128
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001129 return Invalid;
1130}
1131
Mike Stump11289f42009-09-09 15:08:12 +00001132bool
Douglas Gregor463421d2009-03-03 04:44:36 +00001133Sema::InstantiateClassTemplateSpecialization(
Douglas Gregoref6ab412009-10-27 06:26:26 +00001134 SourceLocation PointOfInstantiation,
Douglas Gregor463421d2009-03-03 04:44:36 +00001135 ClassTemplateSpecializationDecl *ClassTemplateSpec,
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001136 TemplateSpecializationKind TSK,
Douglas Gregor8a2e6012009-08-24 15:23:48 +00001137 bool Complain) {
Douglas Gregor463421d2009-03-03 04:44:36 +00001138 // Perform the actual instantiation on the canonical declaration.
1139 ClassTemplateSpec = cast<ClassTemplateSpecializationDecl>(
Argyrios Kyrtzidis6b7e3762009-07-18 00:34:25 +00001140 ClassTemplateSpec->getCanonicalDecl());
Douglas Gregor463421d2009-03-03 04:44:36 +00001141
Douglas Gregor4aa04b12009-09-11 21:19:12 +00001142 // Check whether we have already instantiated or specialized this class
1143 // template specialization.
1144 if (ClassTemplateSpec->getSpecializationKind() != TSK_Undeclared) {
1145 if (ClassTemplateSpec->getSpecializationKind() ==
1146 TSK_ExplicitInstantiationDeclaration &&
1147 TSK == TSK_ExplicitInstantiationDefinition) {
1148 // An explicit instantiation definition follows an explicit instantiation
1149 // declaration (C++0x [temp.explicit]p10); go ahead and perform the
1150 // explicit instantiation.
1151 ClassTemplateSpec->setSpecializationKind(TSK);
Douglas Gregor4aa04b12009-09-11 21:19:12 +00001152 return false;
1153 }
1154
1155 // We can only instantiate something that hasn't already been
1156 // instantiated or specialized. Fail without any diagnostics: our
1157 // caller will provide an error message.
Douglas Gregor463421d2009-03-03 04:44:36 +00001158 return true;
Douglas Gregor4aa04b12009-09-11 21:19:12 +00001159 }
Douglas Gregor463421d2009-03-03 04:44:36 +00001160
Douglas Gregor00a511f2009-09-15 16:51:42 +00001161 if (ClassTemplateSpec->isInvalidDecl())
1162 return true;
1163
Douglas Gregor463421d2009-03-03 04:44:36 +00001164 ClassTemplateDecl *Template = ClassTemplateSpec->getSpecializedTemplate();
Douglas Gregor01afeef2009-08-28 20:31:08 +00001165 CXXRecordDecl *Pattern = 0;
Douglas Gregor2373c592009-05-31 09:31:02 +00001166
Douglas Gregor170bc422009-06-12 22:31:52 +00001167 // C++ [temp.class.spec.match]p1:
1168 // When a class template is used in a context that requires an
1169 // instantiation of the class, it is necessary to determine
1170 // whether the instantiation is to be generated using the primary
1171 // template or one of the partial specializations. This is done by
1172 // matching the template arguments of the class template
1173 // specialization with the template argument lists of the partial
1174 // specializations.
Douglas Gregorb7ae10f2009-06-05 00:53:49 +00001175 typedef std::pair<ClassTemplatePartialSpecializationDecl *,
1176 TemplateArgumentList *> MatchResult;
1177 llvm::SmallVector<MatchResult, 4> Matched;
Mike Stump11289f42009-09-09 15:08:12 +00001178 for (llvm::FoldingSet<ClassTemplatePartialSpecializationDecl>::iterator
Douglas Gregor2373c592009-05-31 09:31:02 +00001179 Partial = Template->getPartialSpecializations().begin(),
1180 PartialEnd = Template->getPartialSpecializations().end();
1181 Partial != PartialEnd;
1182 ++Partial) {
Douglas Gregor181aa4a2009-06-12 18:26:56 +00001183 TemplateDeductionInfo Info(Context);
1184 if (TemplateDeductionResult Result
Mike Stump11289f42009-09-09 15:08:12 +00001185 = DeduceTemplateArguments(&*Partial,
Douglas Gregor181aa4a2009-06-12 18:26:56 +00001186 ClassTemplateSpec->getTemplateArgs(),
1187 Info)) {
1188 // FIXME: Store the failed-deduction information for use in
1189 // diagnostics, later.
1190 (void)Result;
1191 } else {
1192 Matched.push_back(std::make_pair(&*Partial, Info.take()));
1193 }
Douglas Gregor2373c592009-05-31 09:31:02 +00001194 }
1195
Douglas Gregor21610382009-10-29 00:04:11 +00001196 if (Matched.size() >= 1) {
Douglas Gregorbe999392009-09-15 16:23:51 +00001197 llvm::SmallVector<MatchResult, 4>::iterator Best = Matched.begin();
Douglas Gregor21610382009-10-29 00:04:11 +00001198 if (Matched.size() == 1) {
1199 // -- If exactly one matching specialization is found, the
1200 // instantiation is generated from that specialization.
1201 // We don't need to do anything for this.
1202 } else {
1203 // -- If more than one matching specialization is found, the
1204 // partial order rules (14.5.4.2) are used to determine
1205 // whether one of the specializations is more specialized
1206 // than the others. If none of the specializations is more
1207 // specialized than all of the other matching
1208 // specializations, then the use of the class template is
1209 // ambiguous and the program is ill-formed.
1210 for (llvm::SmallVector<MatchResult, 4>::iterator P = Best + 1,
1211 PEnd = Matched.end();
1212 P != PEnd; ++P) {
1213 if (getMoreSpecializedPartialSpecialization(P->first, Best->first)
1214 == P->first)
1215 Best = P;
Douglas Gregorbe999392009-09-15 16:23:51 +00001216 }
Douglas Gregorbe999392009-09-15 16:23:51 +00001217
Douglas Gregor21610382009-10-29 00:04:11 +00001218 // Determine if the best partial specialization is more specialized than
1219 // the others.
1220 bool Ambiguous = false;
Douglas Gregorbe999392009-09-15 16:23:51 +00001221 for (llvm::SmallVector<MatchResult, 4>::iterator P = Matched.begin(),
1222 PEnd = Matched.end();
Douglas Gregor21610382009-10-29 00:04:11 +00001223 P != PEnd; ++P) {
1224 if (P != Best &&
1225 getMoreSpecializedPartialSpecialization(P->first, Best->first)
1226 != Best->first) {
1227 Ambiguous = true;
1228 break;
1229 }
1230 }
1231
1232 if (Ambiguous) {
1233 // Partial ordering did not produce a clear winner. Complain.
1234 ClassTemplateSpec->setInvalidDecl();
1235 Diag(PointOfInstantiation, diag::err_partial_spec_ordering_ambiguous)
1236 << ClassTemplateSpec;
1237
1238 // Print the matching partial specializations.
1239 for (llvm::SmallVector<MatchResult, 4>::iterator P = Matched.begin(),
1240 PEnd = Matched.end();
1241 P != PEnd; ++P)
1242 Diag(P->first->getLocation(), diag::note_partial_spec_match)
1243 << getTemplateArgumentBindingsText(P->first->getTemplateParameters(),
1244 *P->second);
Douglas Gregor01afeef2009-08-28 20:31:08 +00001245
Douglas Gregor21610382009-10-29 00:04:11 +00001246 return true;
1247 }
Douglas Gregorbe999392009-09-15 16:23:51 +00001248 }
1249
1250 // Instantiate using the best class template partial specialization.
Douglas Gregor21610382009-10-29 00:04:11 +00001251 ClassTemplatePartialSpecializationDecl *OrigPartialSpec = Best->first;
1252 while (OrigPartialSpec->getInstantiatedFromMember()) {
1253 // If we've found an explicit specialization of this class template,
1254 // stop here and use that as the pattern.
1255 if (OrigPartialSpec->isMemberSpecialization())
1256 break;
1257
1258 OrigPartialSpec = OrigPartialSpec->getInstantiatedFromMember();
1259 }
1260
1261 Pattern = OrigPartialSpec;
Douglas Gregorbe999392009-09-15 16:23:51 +00001262 ClassTemplateSpec->setInstantiationOf(Best->first, Best->second);
Douglas Gregor170bc422009-06-12 22:31:52 +00001263 } else {
1264 // -- If no matches are found, the instantiation is generated
1265 // from the primary template.
Douglas Gregor01afeef2009-08-28 20:31:08 +00001266 ClassTemplateDecl *OrigTemplate = Template;
Douglas Gregorcf915552009-10-13 16:30:37 +00001267 while (OrigTemplate->getInstantiatedFromMemberTemplate()) {
1268 // If we've found an explicit specialization of this class template,
1269 // stop here and use that as the pattern.
1270 if (OrigTemplate->isMemberSpecialization())
1271 break;
1272
Douglas Gregor01afeef2009-08-28 20:31:08 +00001273 OrigTemplate = OrigTemplate->getInstantiatedFromMemberTemplate();
Douglas Gregorcf915552009-10-13 16:30:37 +00001274 }
1275
Douglas Gregor01afeef2009-08-28 20:31:08 +00001276 Pattern = OrigTemplate->getTemplatedDecl();
Douglas Gregor2373c592009-05-31 09:31:02 +00001277 }
Douglas Gregor463421d2009-03-03 04:44:36 +00001278
Douglas Gregoref6ab412009-10-27 06:26:26 +00001279 bool Result = InstantiateClass(PointOfInstantiation, ClassTemplateSpec,
1280 Pattern,
1281 getTemplateInstantiationArgs(ClassTemplateSpec),
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001282 TSK,
Douglas Gregor8a2e6012009-08-24 15:23:48 +00001283 Complain);
Mike Stump11289f42009-09-09 15:08:12 +00001284
Douglas Gregorb7ae10f2009-06-05 00:53:49 +00001285 for (unsigned I = 0, N = Matched.size(); I != N; ++I) {
1286 // FIXME: Implement TemplateArgumentList::Destroy!
1287 // if (Matched[I].first != Pattern)
1288 // Matched[I].second->Destroy(Context);
1289 }
Mike Stump11289f42009-09-09 15:08:12 +00001290
Douglas Gregorb7ae10f2009-06-05 00:53:49 +00001291 return Result;
Douglas Gregor463421d2009-03-03 04:44:36 +00001292}
Douglas Gregor90a1a652009-03-19 17:26:29 +00001293
John McCall76d824f2009-08-25 22:02:44 +00001294/// \brief Instantiates the definitions of all of the member
1295/// of the given class, which is an instantiation of a class template
1296/// or a member class of a template.
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00001297void
1298Sema::InstantiateClassMembers(SourceLocation PointOfInstantiation,
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001299 CXXRecordDecl *Instantiation,
1300 const MultiLevelTemplateArgumentList &TemplateArgs,
1301 TemplateSpecializationKind TSK) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001302 for (DeclContext::decl_iterator D = Instantiation->decls_begin(),
1303 DEnd = Instantiation->decls_end();
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00001304 D != DEnd; ++D) {
Douglas Gregor1d957a32009-10-27 18:42:08 +00001305 bool SuppressNew = false;
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00001306 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(*D)) {
Douglas Gregor1d957a32009-10-27 18:42:08 +00001307 if (FunctionDecl *Pattern
1308 = Function->getInstantiatedFromMemberFunction()) {
1309 MemberSpecializationInfo *MSInfo
1310 = Function->getMemberSpecializationInfo();
1311 assert(MSInfo && "No member specialization information?");
1312 if (CheckSpecializationInstantiationRedecl(PointOfInstantiation, TSK,
1313 Function,
1314 MSInfo->getTemplateSpecializationKind(),
1315 MSInfo->getPointOfInstantiation(),
1316 SuppressNew) ||
1317 SuppressNew)
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001318 continue;
1319
Douglas Gregor1d957a32009-10-27 18:42:08 +00001320 if (Function->getBody())
1321 continue;
1322
1323 if (TSK == TSK_ExplicitInstantiationDefinition) {
1324 // C++0x [temp.explicit]p8:
1325 // An explicit instantiation definition that names a class template
1326 // specialization explicitly instantiates the class template
1327 // specialization and is only an explicit instantiation definition
1328 // of members whose definition is visible at the point of
1329 // instantiation.
1330 if (!Pattern->getBody())
1331 continue;
1332
1333 Function->setTemplateSpecializationKind(TSK, PointOfInstantiation);
1334
1335 InstantiateFunctionDefinition(PointOfInstantiation, Function);
1336 } else {
1337 Function->setTemplateSpecializationKind(TSK, PointOfInstantiation);
1338 }
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001339 }
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00001340 } else if (VarDecl *Var = dyn_cast<VarDecl>(*D)) {
Douglas Gregor86d142a2009-10-08 07:24:58 +00001341 if (Var->isStaticDataMember()) {
Douglas Gregor1d957a32009-10-27 18:42:08 +00001342 MemberSpecializationInfo *MSInfo = Var->getMemberSpecializationInfo();
1343 assert(MSInfo && "No member specialization information?");
1344 if (CheckSpecializationInstantiationRedecl(PointOfInstantiation, TSK,
1345 Var,
1346 MSInfo->getTemplateSpecializationKind(),
1347 MSInfo->getPointOfInstantiation(),
1348 SuppressNew) ||
1349 SuppressNew)
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001350 continue;
1351
Douglas Gregor1d957a32009-10-27 18:42:08 +00001352 if (TSK == TSK_ExplicitInstantiationDefinition) {
1353 // C++0x [temp.explicit]p8:
1354 // An explicit instantiation definition that names a class template
1355 // specialization explicitly instantiates the class template
1356 // specialization and is only an explicit instantiation definition
1357 // of members whose definition is visible at the point of
1358 // instantiation.
1359 if (!Var->getInstantiatedFromStaticDataMember()
1360 ->getOutOfLineDefinition())
1361 continue;
1362
1363 Var->setTemplateSpecializationKind(TSK, PointOfInstantiation);
Douglas Gregor86d142a2009-10-08 07:24:58 +00001364 InstantiateStaticDataMemberDefinition(PointOfInstantiation, Var);
Douglas Gregor1d957a32009-10-27 18:42:08 +00001365 } else {
1366 Var->setTemplateSpecializationKind(TSK, PointOfInstantiation);
1367 }
1368 }
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00001369 } else if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(*D)) {
Douglas Gregord801b062009-10-07 23:56:10 +00001370 if (Record->isInjectedClassName())
1371 continue;
1372
Douglas Gregor1d957a32009-10-27 18:42:08 +00001373 MemberSpecializationInfo *MSInfo = Record->getMemberSpecializationInfo();
1374 assert(MSInfo && "No member specialization information?");
1375 if (CheckSpecializationInstantiationRedecl(PointOfInstantiation, TSK,
1376 Record,
1377 MSInfo->getTemplateSpecializationKind(),
1378 MSInfo->getPointOfInstantiation(),
1379 SuppressNew) ||
1380 SuppressNew)
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001381 continue;
1382
Douglas Gregor1d957a32009-10-27 18:42:08 +00001383 CXXRecordDecl *Pattern = Record->getInstantiatedFromMemberClass();
1384 assert(Pattern && "Missing instantiated-from-template information");
1385
1386 if (!Record->getDefinition(Context)) {
1387 if (!Pattern->getDefinition(Context)) {
1388 // C++0x [temp.explicit]p8:
1389 // An explicit instantiation definition that names a class template
1390 // specialization explicitly instantiates the class template
1391 // specialization and is only an explicit instantiation definition
1392 // of members whose definition is visible at the point of
1393 // instantiation.
1394 if (TSK == TSK_ExplicitInstantiationDeclaration) {
1395 MSInfo->setTemplateSpecializationKind(TSK);
1396 MSInfo->setPointOfInstantiation(PointOfInstantiation);
1397 }
1398
1399 continue;
1400 }
1401
1402 InstantiateClass(PointOfInstantiation, Record, Pattern,
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001403 TemplateArgs,
1404 TSK);
Douglas Gregor1d957a32009-10-27 18:42:08 +00001405 }
Douglas Gregorc093c1d2009-10-08 01:19:17 +00001406
Douglas Gregor1d957a32009-10-27 18:42:08 +00001407 Pattern = cast_or_null<CXXRecordDecl>(Record->getDefinition(Context));
1408 if (Pattern)
1409 InstantiateClassMembers(PointOfInstantiation, Pattern, TemplateArgs,
1410 TSK);
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00001411 }
1412 }
1413}
1414
1415/// \brief Instantiate the definitions of all of the members of the
1416/// given class template specialization, which was named as part of an
1417/// explicit instantiation.
Mike Stump11289f42009-09-09 15:08:12 +00001418void
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001419Sema::InstantiateClassTemplateSpecializationMembers(
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00001420 SourceLocation PointOfInstantiation,
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001421 ClassTemplateSpecializationDecl *ClassTemplateSpec,
1422 TemplateSpecializationKind TSK) {
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00001423 // C++0x [temp.explicit]p7:
1424 // An explicit instantiation that names a class template
1425 // specialization is an explicit instantion of the same kind
1426 // (declaration or definition) of each of its members (not
1427 // including members inherited from base classes) that has not
1428 // been previously explicitly specialized in the translation unit
1429 // containing the explicit instantiation, except as described
1430 // below.
1431 InstantiateClassMembers(PointOfInstantiation, ClassTemplateSpec,
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001432 getTemplateInstantiationArgs(ClassTemplateSpec),
1433 TSK);
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00001434}
1435
Mike Stump11289f42009-09-09 15:08:12 +00001436Sema::OwningStmtResult
Douglas Gregor01afeef2009-08-28 20:31:08 +00001437Sema::SubstStmt(Stmt *S, const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregorebe10102009-08-20 07:17:43 +00001438 if (!S)
1439 return Owned(S);
1440
1441 TemplateInstantiator Instantiator(*this, TemplateArgs,
1442 SourceLocation(),
1443 DeclarationName());
1444 return Instantiator.TransformStmt(S);
1445}
1446
Mike Stump11289f42009-09-09 15:08:12 +00001447Sema::OwningExprResult
Douglas Gregor01afeef2009-08-28 20:31:08 +00001448Sema::SubstExpr(Expr *E, const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregora16548e2009-08-11 05:31:07 +00001449 if (!E)
1450 return Owned(E);
Mike Stump11289f42009-09-09 15:08:12 +00001451
Douglas Gregora16548e2009-08-11 05:31:07 +00001452 TemplateInstantiator Instantiator(*this, TemplateArgs,
1453 SourceLocation(),
1454 DeclarationName());
1455 return Instantiator.TransformExpr(E);
1456}
1457
John McCall76d824f2009-08-25 22:02:44 +00001458/// \brief Do template substitution on a nested-name-specifier.
Douglas Gregorf21eb492009-03-26 23:50:42 +00001459NestedNameSpecifier *
John McCall76d824f2009-08-25 22:02:44 +00001460Sema::SubstNestedNameSpecifier(NestedNameSpecifier *NNS,
Douglas Gregor01afeef2009-08-28 20:31:08 +00001461 SourceRange Range,
1462 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor1135c352009-08-06 05:28:30 +00001463 TemplateInstantiator Instantiator(*this, TemplateArgs, Range.getBegin(),
1464 DeclarationName());
1465 return Instantiator.TransformNestedNameSpecifier(NNS, Range);
Douglas Gregor90a1a652009-03-19 17:26:29 +00001466}
Douglas Gregoraa594892009-03-31 18:38:02 +00001467
1468TemplateName
John McCall76d824f2009-08-25 22:02:44 +00001469Sema::SubstTemplateName(TemplateName Name, SourceLocation Loc,
Douglas Gregor01afeef2009-08-28 20:31:08 +00001470 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor71dc5092009-08-06 06:41:21 +00001471 TemplateInstantiator Instantiator(*this, TemplateArgs, Loc,
1472 DeclarationName());
1473 return Instantiator.TransformTemplateName(Name);
Douglas Gregoraa594892009-03-31 18:38:02 +00001474}
Douglas Gregorc43620d2009-06-11 00:06:24 +00001475
John McCall0ad16662009-10-29 08:12:44 +00001476bool Sema::Subst(const TemplateArgumentLoc &Input, TemplateArgumentLoc &Output,
1477 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregore922c772009-08-04 22:27:00 +00001478 TemplateInstantiator Instantiator(*this, TemplateArgs, SourceLocation(),
1479 DeclarationName());
John McCall0ad16662009-10-29 08:12:44 +00001480
1481 return Instantiator.TransformTemplateArgument(Input, Output);
Douglas Gregorc43620d2009-06-11 00:06:24 +00001482}