blob: 51e17fe472e5a3cdff47ce606f36da9f09f11eeb [file] [log] [blame]
Douglas Gregor99ebf652009-02-27 19:31:52 +00001//===------- SemaTemplateInstantiate.cpp - C++ Template Instantiation ------===/
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//===----------------------------------------------------------------------===/
8//
9// This file implements C++ template instantiation.
10//
11//===----------------------------------------------------------------------===/
12
13#include "Sema.h"
Douglas Gregor577f75a2009-08-04 16:50:30 +000014#include "TreeTransform.h"
John McCall5b3f9132009-11-22 01:44:31 +000015#include "Lookup.h"
Douglas Gregoraba43bb2009-05-26 20:50:29 +000016#include "clang/AST/ASTConsumer.h"
Douglas Gregor99ebf652009-02-27 19:31:52 +000017#include "clang/AST/ASTContext.h"
18#include "clang/AST/Expr.h"
Douglas Gregor99ebf652009-02-27 19:31:52 +000019#include "clang/AST/DeclTemplate.h"
20#include "clang/Parse/DeclSpec.h"
21#include "clang/Basic/LangOptions.h"
22
23using namespace clang;
24
Douglas Gregoree1828a2009-03-10 18:03:33 +000025//===----------------------------------------------------------------------===/
26// Template Instantiation Support
27//===----------------------------------------------------------------------===/
28
Douglas Gregord6350ae2009-08-28 20:31:08 +000029/// \brief Retrieve the template argument list(s) that should be used to
30/// instantiate the definition of the given declaration.
Douglas Gregor0f8716b2009-11-09 19:17:50 +000031///
32/// \param D the declaration for which we are computing template instantiation
33/// arguments.
34///
35/// \param Innermost if non-NULL, the innermost template argument list.
Douglas Gregor525f96c2010-02-05 07:33:43 +000036///
37/// \param RelativeToPrimary true if we should get the template
38/// arguments relative to the primary template, even when we're
39/// dealing with a specialization. This is only relevant for function
40/// template specializations.
Douglas Gregord1102432009-08-28 17:37:35 +000041MultiLevelTemplateArgumentList
Douglas Gregor0f8716b2009-11-09 19:17:50 +000042Sema::getTemplateInstantiationArgs(NamedDecl *D,
Douglas Gregor525f96c2010-02-05 07:33:43 +000043 const TemplateArgumentList *Innermost,
44 bool RelativeToPrimary) {
Douglas Gregord1102432009-08-28 17:37:35 +000045 // Accumulate the set of template argument lists in this structure.
46 MultiLevelTemplateArgumentList Result;
Mike Stump1eb44332009-09-09 15:08:12 +000047
Douglas Gregor0f8716b2009-11-09 19:17:50 +000048 if (Innermost)
49 Result.addOuterTemplateArguments(Innermost);
50
Douglas Gregord1102432009-08-28 17:37:35 +000051 DeclContext *Ctx = dyn_cast<DeclContext>(D);
52 if (!Ctx)
53 Ctx = D->getDeclContext();
Mike Stump1eb44332009-09-09 15:08:12 +000054
John McCallf181d8a2009-08-29 03:16:09 +000055 while (!Ctx->isFileContext()) {
Douglas Gregord1102432009-08-28 17:37:35 +000056 // Add template arguments from a class template instantiation.
Mike Stump1eb44332009-09-09 15:08:12 +000057 if (ClassTemplateSpecializationDecl *Spec
Douglas Gregord1102432009-08-28 17:37:35 +000058 = dyn_cast<ClassTemplateSpecializationDecl>(Ctx)) {
59 // We're done when we hit an explicit specialization.
60 if (Spec->getSpecializationKind() == TSK_ExplicitSpecialization)
61 break;
Mike Stump1eb44332009-09-09 15:08:12 +000062
Douglas Gregord1102432009-08-28 17:37:35 +000063 Result.addOuterTemplateArguments(&Spec->getTemplateInstantiationArgs());
Douglas Gregorfd056bc2009-10-13 16:30:37 +000064
65 // If this class template specialization was instantiated from a
66 // specialized member that is a class template, we're done.
67 assert(Spec->getSpecializedTemplate() && "No class template?");
68 if (Spec->getSpecializedTemplate()->isMemberSpecialization())
69 break;
Mike Stump1eb44332009-09-09 15:08:12 +000070 }
Douglas Gregord1102432009-08-28 17:37:35 +000071 // Add template arguments from a function template specialization.
John McCallf181d8a2009-08-29 03:16:09 +000072 else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Ctx)) {
Douglas Gregor525f96c2010-02-05 07:33:43 +000073 if (!RelativeToPrimary &&
74 Function->getTemplateSpecializationKind()
75 == TSK_ExplicitSpecialization)
Douglas Gregorfd056bc2009-10-13 16:30:37 +000076 break;
77
Douglas Gregord1102432009-08-28 17:37:35 +000078 if (const TemplateArgumentList *TemplateArgs
Douglas Gregorfd056bc2009-10-13 16:30:37 +000079 = Function->getTemplateSpecializationArgs()) {
80 // Add the template arguments for this specialization.
Douglas Gregord1102432009-08-28 17:37:35 +000081 Result.addOuterTemplateArguments(TemplateArgs);
John McCallf181d8a2009-08-29 03:16:09 +000082
Douglas Gregorfd056bc2009-10-13 16:30:37 +000083 // If this function was instantiated from a specialized member that is
84 // a function template, we're done.
85 assert(Function->getPrimaryTemplate() && "No function template?");
86 if (Function->getPrimaryTemplate()->isMemberSpecialization())
87 break;
88 }
89
John McCallf181d8a2009-08-29 03:16:09 +000090 // If this is a friend declaration and it declares an entity at
91 // namespace scope, take arguments from its lexical parent
92 // instead of its semantic parent.
93 if (Function->getFriendObjectKind() &&
94 Function->getDeclContext()->isFileContext()) {
95 Ctx = Function->getLexicalDeclContext();
Douglas Gregor525f96c2010-02-05 07:33:43 +000096 RelativeToPrimary = false;
John McCallf181d8a2009-08-29 03:16:09 +000097 continue;
98 }
Douglas Gregord1102432009-08-28 17:37:35 +000099 }
John McCallf181d8a2009-08-29 03:16:09 +0000100
101 Ctx = Ctx->getParent();
Douglas Gregor525f96c2010-02-05 07:33:43 +0000102 RelativeToPrimary = false;
Douglas Gregor54dabfc2009-05-14 23:26:13 +0000103 }
Mike Stump1eb44332009-09-09 15:08:12 +0000104
Douglas Gregord1102432009-08-28 17:37:35 +0000105 return Result;
Douglas Gregor54dabfc2009-05-14 23:26:13 +0000106}
107
Douglas Gregorf35f8282009-11-11 21:54:23 +0000108bool Sema::ActiveTemplateInstantiation::isInstantiationRecord() const {
109 switch (Kind) {
110 case TemplateInstantiation:
111 case DefaultTemplateArgumentInstantiation:
112 case DefaultFunctionArgumentInstantiation:
113 return true;
114
115 case ExplicitTemplateArgumentSubstitution:
116 case DeducedTemplateArgumentSubstitution:
117 case PriorTemplateArgumentSubstitution:
118 case DefaultTemplateArgumentChecking:
119 return false;
120 }
121
122 return true;
123}
124
Douglas Gregor26dce442009-03-10 00:06:19 +0000125Sema::InstantiatingTemplate::
126InstantiatingTemplate(Sema &SemaRef, SourceLocation PointOfInstantiation,
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000127 Decl *Entity,
Douglas Gregor26dce442009-03-10 00:06:19 +0000128 SourceRange InstantiationRange)
129 : SemaRef(SemaRef) {
Douglas Gregordf667e72009-03-10 20:44:00 +0000130
131 Invalid = CheckInstantiationDepth(PointOfInstantiation,
132 InstantiationRange);
133 if (!Invalid) {
Douglas Gregor26dce442009-03-10 00:06:19 +0000134 ActiveTemplateInstantiation Inst;
Douglas Gregordf667e72009-03-10 20:44:00 +0000135 Inst.Kind = ActiveTemplateInstantiation::TemplateInstantiation;
Douglas Gregor26dce442009-03-10 00:06:19 +0000136 Inst.PointOfInstantiation = PointOfInstantiation;
Douglas Gregordf667e72009-03-10 20:44:00 +0000137 Inst.Entity = reinterpret_cast<uintptr_t>(Entity);
Douglas Gregor313a81d2009-03-12 18:36:18 +0000138 Inst.TemplateArgs = 0;
139 Inst.NumTemplateArgs = 0;
Douglas Gregordf667e72009-03-10 20:44:00 +0000140 Inst.InstantiationRange = InstantiationRange;
141 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
Douglas Gregordf667e72009-03-10 20:44:00 +0000142 }
143}
144
Mike Stump1eb44332009-09-09 15:08:12 +0000145Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef,
Douglas Gregordf667e72009-03-10 20:44:00 +0000146 SourceLocation PointOfInstantiation,
147 TemplateDecl *Template,
148 const TemplateArgument *TemplateArgs,
149 unsigned NumTemplateArgs,
150 SourceRange InstantiationRange)
151 : SemaRef(SemaRef) {
152
153 Invalid = CheckInstantiationDepth(PointOfInstantiation,
154 InstantiationRange);
155 if (!Invalid) {
156 ActiveTemplateInstantiation Inst;
Mike Stump1eb44332009-09-09 15:08:12 +0000157 Inst.Kind
Douglas Gregordf667e72009-03-10 20:44:00 +0000158 = ActiveTemplateInstantiation::DefaultTemplateArgumentInstantiation;
159 Inst.PointOfInstantiation = PointOfInstantiation;
160 Inst.Entity = reinterpret_cast<uintptr_t>(Template);
161 Inst.TemplateArgs = TemplateArgs;
162 Inst.NumTemplateArgs = NumTemplateArgs;
Douglas Gregor26dce442009-03-10 00:06:19 +0000163 Inst.InstantiationRange = InstantiationRange;
164 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
Douglas Gregor26dce442009-03-10 00:06:19 +0000165 }
166}
167
Mike Stump1eb44332009-09-09 15:08:12 +0000168Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef,
Douglas Gregor637a4092009-06-10 23:47:09 +0000169 SourceLocation PointOfInstantiation,
Douglas Gregorcca9e962009-07-01 22:01:06 +0000170 FunctionTemplateDecl *FunctionTemplate,
171 const TemplateArgument *TemplateArgs,
172 unsigned NumTemplateArgs,
173 ActiveTemplateInstantiation::InstantiationKind Kind,
174 SourceRange InstantiationRange)
175: SemaRef(SemaRef) {
Mike Stump1eb44332009-09-09 15:08:12 +0000176
Douglas Gregorcca9e962009-07-01 22:01:06 +0000177 Invalid = CheckInstantiationDepth(PointOfInstantiation,
178 InstantiationRange);
179 if (!Invalid) {
180 ActiveTemplateInstantiation Inst;
181 Inst.Kind = Kind;
182 Inst.PointOfInstantiation = PointOfInstantiation;
183 Inst.Entity = reinterpret_cast<uintptr_t>(FunctionTemplate);
184 Inst.TemplateArgs = TemplateArgs;
185 Inst.NumTemplateArgs = NumTemplateArgs;
186 Inst.InstantiationRange = InstantiationRange;
187 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
Douglas Gregorf35f8282009-11-11 21:54:23 +0000188
189 if (!Inst.isInstantiationRecord())
190 ++SemaRef.NonInstantiationEntries;
Douglas Gregorcca9e962009-07-01 22:01:06 +0000191 }
192}
193
Mike Stump1eb44332009-09-09 15:08:12 +0000194Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef,
Douglas Gregorcca9e962009-07-01 22:01:06 +0000195 SourceLocation PointOfInstantiation,
Douglas Gregor637a4092009-06-10 23:47:09 +0000196 ClassTemplatePartialSpecializationDecl *PartialSpec,
197 const TemplateArgument *TemplateArgs,
198 unsigned NumTemplateArgs,
199 SourceRange InstantiationRange)
200 : SemaRef(SemaRef) {
201
Douglas Gregorf35f8282009-11-11 21:54:23 +0000202 Invalid = false;
203
204 ActiveTemplateInstantiation Inst;
205 Inst.Kind = ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution;
206 Inst.PointOfInstantiation = PointOfInstantiation;
207 Inst.Entity = reinterpret_cast<uintptr_t>(PartialSpec);
208 Inst.TemplateArgs = TemplateArgs;
209 Inst.NumTemplateArgs = NumTemplateArgs;
210 Inst.InstantiationRange = InstantiationRange;
211 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
212
213 assert(!Inst.isInstantiationRecord());
214 ++SemaRef.NonInstantiationEntries;
Douglas Gregor637a4092009-06-10 23:47:09 +0000215}
216
Mike Stump1eb44332009-09-09 15:08:12 +0000217Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef,
Douglas Gregor9148c3f2009-11-11 19:13:48 +0000218 SourceLocation PointOfInstantiation,
Anders Carlsson25cae7f2009-09-05 05:14:19 +0000219 ParmVarDecl *Param,
220 const TemplateArgument *TemplateArgs,
221 unsigned NumTemplateArgs,
222 SourceRange InstantiationRange)
223 : SemaRef(SemaRef) {
Mike Stump1eb44332009-09-09 15:08:12 +0000224
Douglas Gregor9148c3f2009-11-11 19:13:48 +0000225 Invalid = CheckInstantiationDepth(PointOfInstantiation, InstantiationRange);
Anders Carlsson25cae7f2009-09-05 05:14:19 +0000226
227 if (!Invalid) {
228 ActiveTemplateInstantiation Inst;
229 Inst.Kind
230 = ActiveTemplateInstantiation::DefaultFunctionArgumentInstantiation;
Douglas Gregor9148c3f2009-11-11 19:13:48 +0000231 Inst.PointOfInstantiation = PointOfInstantiation;
Anders Carlsson25cae7f2009-09-05 05:14:19 +0000232 Inst.Entity = reinterpret_cast<uintptr_t>(Param);
233 Inst.TemplateArgs = TemplateArgs;
234 Inst.NumTemplateArgs = NumTemplateArgs;
235 Inst.InstantiationRange = InstantiationRange;
236 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
Douglas Gregor9148c3f2009-11-11 19:13:48 +0000237 }
238}
239
240Sema::InstantiatingTemplate::
241InstantiatingTemplate(Sema &SemaRef, SourceLocation PointOfInstantiation,
242 TemplateDecl *Template,
243 NonTypeTemplateParmDecl *Param,
244 const TemplateArgument *TemplateArgs,
245 unsigned NumTemplateArgs,
246 SourceRange InstantiationRange) : SemaRef(SemaRef) {
Douglas Gregorf35f8282009-11-11 21:54:23 +0000247 Invalid = false;
Douglas Gregor9148c3f2009-11-11 19:13:48 +0000248
Douglas Gregorf35f8282009-11-11 21:54:23 +0000249 ActiveTemplateInstantiation Inst;
250 Inst.Kind = ActiveTemplateInstantiation::PriorTemplateArgumentSubstitution;
251 Inst.PointOfInstantiation = PointOfInstantiation;
252 Inst.Template = Template;
253 Inst.Entity = reinterpret_cast<uintptr_t>(Param);
254 Inst.TemplateArgs = TemplateArgs;
255 Inst.NumTemplateArgs = NumTemplateArgs;
256 Inst.InstantiationRange = InstantiationRange;
257 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
258
259 assert(!Inst.isInstantiationRecord());
260 ++SemaRef.NonInstantiationEntries;
Douglas Gregor9148c3f2009-11-11 19:13:48 +0000261}
262
263Sema::InstantiatingTemplate::
264InstantiatingTemplate(Sema &SemaRef, SourceLocation PointOfInstantiation,
265 TemplateDecl *Template,
266 TemplateTemplateParmDecl *Param,
267 const TemplateArgument *TemplateArgs,
268 unsigned NumTemplateArgs,
269 SourceRange InstantiationRange) : SemaRef(SemaRef) {
Douglas Gregorf35f8282009-11-11 21:54:23 +0000270 Invalid = false;
271 ActiveTemplateInstantiation Inst;
272 Inst.Kind = ActiveTemplateInstantiation::PriorTemplateArgumentSubstitution;
273 Inst.PointOfInstantiation = PointOfInstantiation;
274 Inst.Template = Template;
275 Inst.Entity = reinterpret_cast<uintptr_t>(Param);
276 Inst.TemplateArgs = TemplateArgs;
277 Inst.NumTemplateArgs = NumTemplateArgs;
278 Inst.InstantiationRange = InstantiationRange;
279 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
Douglas Gregor9148c3f2009-11-11 19:13:48 +0000280
Douglas Gregorf35f8282009-11-11 21:54:23 +0000281 assert(!Inst.isInstantiationRecord());
282 ++SemaRef.NonInstantiationEntries;
283}
284
285Sema::InstantiatingTemplate::
286InstantiatingTemplate(Sema &SemaRef, SourceLocation PointOfInstantiation,
287 TemplateDecl *Template,
288 NamedDecl *Param,
289 const TemplateArgument *TemplateArgs,
290 unsigned NumTemplateArgs,
291 SourceRange InstantiationRange) : SemaRef(SemaRef) {
292 Invalid = false;
293
294 ActiveTemplateInstantiation Inst;
295 Inst.Kind = ActiveTemplateInstantiation::DefaultTemplateArgumentChecking;
296 Inst.PointOfInstantiation = PointOfInstantiation;
297 Inst.Template = Template;
298 Inst.Entity = reinterpret_cast<uintptr_t>(Param);
299 Inst.TemplateArgs = TemplateArgs;
300 Inst.NumTemplateArgs = NumTemplateArgs;
301 Inst.InstantiationRange = InstantiationRange;
302 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
303
304 assert(!Inst.isInstantiationRecord());
305 ++SemaRef.NonInstantiationEntries;
Anders Carlsson25cae7f2009-09-05 05:14:19 +0000306}
307
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000308void Sema::InstantiatingTemplate::Clear() {
309 if (!Invalid) {
Douglas Gregorf35f8282009-11-11 21:54:23 +0000310 if (!SemaRef.ActiveTemplateInstantiations.back().isInstantiationRecord()) {
311 assert(SemaRef.NonInstantiationEntries > 0);
312 --SemaRef.NonInstantiationEntries;
313 }
314
Douglas Gregor26dce442009-03-10 00:06:19 +0000315 SemaRef.ActiveTemplateInstantiations.pop_back();
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000316 Invalid = true;
317 }
Douglas Gregor26dce442009-03-10 00:06:19 +0000318}
319
Douglas Gregordf667e72009-03-10 20:44:00 +0000320bool Sema::InstantiatingTemplate::CheckInstantiationDepth(
321 SourceLocation PointOfInstantiation,
322 SourceRange InstantiationRange) {
Douglas Gregorf35f8282009-11-11 21:54:23 +0000323 assert(SemaRef.NonInstantiationEntries <=
324 SemaRef.ActiveTemplateInstantiations.size());
325 if ((SemaRef.ActiveTemplateInstantiations.size() -
326 SemaRef.NonInstantiationEntries)
327 <= SemaRef.getLangOptions().InstantiationDepth)
Douglas Gregordf667e72009-03-10 20:44:00 +0000328 return false;
329
Mike Stump1eb44332009-09-09 15:08:12 +0000330 SemaRef.Diag(PointOfInstantiation,
Douglas Gregordf667e72009-03-10 20:44:00 +0000331 diag::err_template_recursion_depth_exceeded)
332 << SemaRef.getLangOptions().InstantiationDepth
333 << InstantiationRange;
334 SemaRef.Diag(PointOfInstantiation, diag::note_template_recursion_depth)
335 << SemaRef.getLangOptions().InstantiationDepth;
336 return true;
337}
338
Douglas Gregoree1828a2009-03-10 18:03:33 +0000339/// \brief Prints the current instantiation stack through a series of
340/// notes.
341void Sema::PrintInstantiationStack() {
Douglas Gregorcca9e962009-07-01 22:01:06 +0000342 // FIXME: In all of these cases, we need to show the template arguments
Douglas Gregoree1828a2009-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 Gregordf667e72009-03-10 20:44:00 +0000348 switch (Active->Kind) {
349 case ActiveTemplateInstantiation::TemplateInstantiation: {
Douglas Gregorf3e7ce42009-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 Stump1eb44332009-09-09 15:08:12 +0000355 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000356 DiagID)
357 << Context.getTypeDeclType(Record)
358 << Active->InstantiationRange;
Douglas Gregor7caa6822009-07-24 20:34:43 +0000359 } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
Douglas Gregor1637be72009-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 Stump1eb44332009-09-09 15:08:12 +0000365 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000366 DiagID)
367 << Function
368 << Active->InstantiationRange;
Douglas Gregor7caa6822009-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 Gregorf3e7ce42009-05-18 17:01:57 +0000374 }
Douglas Gregordf667e72009-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 Gregor7532dc62009-03-30 22:58:21 +0000381 = TemplateSpecializationType::PrintTemplateArgumentList(
Mike Stump1eb44332009-09-09 15:08:12 +0000382 Active->TemplateArgs,
Douglas Gregord249e1d1f2009-05-29 20:38:28 +0000383 Active->NumTemplateArgs,
384 Context.PrintingPolicy);
Douglas Gregordf667e72009-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 Gregor637a4092009-06-10 23:47:09 +0000391
Douglas Gregorcca9e962009-07-01 22:01:06 +0000392 case ActiveTemplateInstantiation::ExplicitTemplateArgumentSubstitution: {
Mike Stump1eb44332009-09-09 15:08:12 +0000393 FunctionTemplateDecl *FnTmpl
Douglas Gregorcca9e962009-07-01 22:01:06 +0000394 = cast<FunctionTemplateDecl>((Decl *)Active->Entity);
Douglas Gregor637a4092009-06-10 23:47:09 +0000395 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
Douglas Gregorcca9e962009-07-01 22:01:06 +0000396 diag::note_explicit_template_arg_substitution_here)
397 << FnTmpl << Active->InstantiationRange;
Douglas Gregor637a4092009-06-10 23:47:09 +0000398 break;
399 }
Mike Stump1eb44332009-09-09 15:08:12 +0000400
Douglas Gregorcca9e962009-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 Gregor637a4092009-06-10 23:47:09 +0000417
Anders Carlsson25cae7f2009-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 Stump1eb44332009-09-09 15:08:12 +0000421
Anders Carlsson25cae7f2009-09-05 05:14:19 +0000422 std::string TemplateArgsStr
423 = TemplateSpecializationType::PrintTemplateArgumentList(
Mike Stump1eb44332009-09-09 15:08:12 +0000424 Active->TemplateArgs,
Anders Carlsson25cae7f2009-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 Carlsson6bc107b2009-09-05 05:38:54 +0000429 << (FD->getNameAsString() + TemplateArgsStr)
Anders Carlsson25cae7f2009-09-05 05:14:19 +0000430 << Active->InstantiationRange;
431 break;
432 }
Mike Stump1eb44332009-09-09 15:08:12 +0000433
Douglas Gregor9148c3f2009-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 Gregorf35f8282009-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 Gregordf667e72009-03-10 20:44:00 +0000462 }
Douglas Gregoree1828a2009-03-10 18:03:33 +0000463 }
464}
465
Douglas Gregor5e9f35c2009-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 Gregorf35f8282009-11-11 21:54:23 +0000472 ++Active)
473 {
Douglas Gregor5e9f35c2009-06-14 07:33:30 +0000474 switch(Active->Kind) {
Douglas Gregorcca9e962009-07-01 22:01:06 +0000475 case ActiveTemplateInstantiation::TemplateInstantiation:
Anders Carlsson25cae7f2009-09-05 05:14:19 +0000476 case ActiveTemplateInstantiation::DefaultFunctionArgumentInstantiation:
Douglas Gregorcca9e962009-07-01 22:01:06 +0000477 // This is a template instantiation, so there is no SFINAE.
478 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000479
Douglas Gregor5e9f35c2009-06-14 07:33:30 +0000480 case ActiveTemplateInstantiation::DefaultTemplateArgumentInstantiation:
Douglas Gregor9148c3f2009-11-11 19:13:48 +0000481 case ActiveTemplateInstantiation::PriorTemplateArgumentSubstitution:
Douglas Gregorf35f8282009-11-11 21:54:23 +0000482 case ActiveTemplateInstantiation::DefaultTemplateArgumentChecking:
Douglas Gregor9148c3f2009-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 Gregor5e9f35c2009-06-14 07:33:30 +0000486 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000487
Douglas Gregorcca9e962009-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 Gregor5e9f35c2009-06-14 07:33:30 +0000493 }
494 }
495
496 return false;
497}
498
Douglas Gregor99ebf652009-02-27 19:31:52 +0000499//===----------------------------------------------------------------------===/
500// Template Instantiation for Types
501//===----------------------------------------------------------------------===/
Douglas Gregorcd281c32009-02-28 00:25:32 +0000502namespace {
Benjamin Kramer85b45212009-11-28 19:45:26 +0000503 class TemplateInstantiator
Mike Stump1eb44332009-09-09 15:08:12 +0000504 : public TreeTransform<TemplateInstantiator> {
Douglas Gregord6350ae2009-08-28 20:31:08 +0000505 const MultiLevelTemplateArgumentList &TemplateArgs;
Douglas Gregorcd281c32009-02-28 00:25:32 +0000506 SourceLocation Loc;
507 DeclarationName Entity;
Douglas Gregor99ebf652009-02-27 19:31:52 +0000508
Douglas Gregorcd281c32009-02-28 00:25:32 +0000509 public:
Douglas Gregor43959a92009-08-20 07:17:43 +0000510 typedef TreeTransform<TemplateInstantiator> inherited;
Mike Stump1eb44332009-09-09 15:08:12 +0000511
512 TemplateInstantiator(Sema &SemaRef,
Douglas Gregord6350ae2009-08-28 20:31:08 +0000513 const MultiLevelTemplateArgumentList &TemplateArgs,
Douglas Gregor577f75a2009-08-04 16:50:30 +0000514 SourceLocation Loc,
Mike Stump1eb44332009-09-09 15:08:12 +0000515 DeclarationName Entity)
516 : inherited(SemaRef), TemplateArgs(TemplateArgs), Loc(Loc),
Douglas Gregor43959a92009-08-20 07:17:43 +0000517 Entity(Entity) { }
Douglas Gregorcd281c32009-02-28 00:25:32 +0000518
Mike Stump1eb44332009-09-09 15:08:12 +0000519 /// \brief Determine whether the given type \p T has already been
Douglas Gregor577f75a2009-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 Gregorff668032009-05-13 18:28:20 +0000526 }
Mike Stump1eb44332009-09-09 15:08:12 +0000527
Douglas Gregor577f75a2009-08-04 16:50:30 +0000528 /// \brief Returns the location of the entity being instantiated, if known.
529 SourceLocation getBaseLocation() { return Loc; }
Mike Stump1eb44332009-09-09 15:08:12 +0000530
Douglas Gregor577f75a2009-08-04 16:50:30 +0000531 /// \brief Returns the name of the entity being instantiated, if any.
532 DeclarationName getBaseEntity() { return Entity; }
Mike Stump1eb44332009-09-09 15:08:12 +0000533
Douglas Gregor972e6ce2009-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 Gregor577f75a2009-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 Gregorb98b1992009-08-11 05:31:07 +0000544
Mike Stump1eb44332009-09-09 15:08:12 +0000545 /// \brief Transform the definition of the given declaration by
Douglas Gregor43959a92009-08-20 07:17:43 +0000546 /// instantiating it.
547 Decl *TransformDefinition(Decl *D);
Mike Stump1eb44332009-09-09 15:08:12 +0000548
Douglas Gregor6cd21982009-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 Gregor43959a92009-08-20 07:17:43 +0000553 /// \brief Rebuild the exception declaration and register the declaration
554 /// as an instantiated local.
Mike Stump1eb44332009-09-09 15:08:12 +0000555 VarDecl *RebuildExceptionDecl(VarDecl *ExceptionDecl, QualType T,
John McCalla93c9342009-12-07 02:54:59 +0000556 TypeSourceInfo *Declarator,
Douglas Gregor43959a92009-08-20 07:17:43 +0000557 IdentifierInfo *Name,
558 SourceLocation Loc, SourceRange TypeRange);
Mike Stump1eb44332009-09-09 15:08:12 +0000559
John McCallc4e70192009-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 McCall454feb92009-12-08 09:21:05 +0000564 Sema::OwningExprResult TransformPredefinedExpr(PredefinedExpr *E);
565 Sema::OwningExprResult TransformDeclRefExpr(DeclRefExpr *E);
John McCall454feb92009-12-08 09:21:05 +0000566 Sema::OwningExprResult TransformCXXDefaultArgExpr(CXXDefaultArgExpr *E);
John McCallb8fc0532010-02-06 08:42:39 +0000567 Sema::OwningExprResult TransformTemplateParmRefExpr(DeclRefExpr *E,
568 NonTypeTemplateParmDecl *D);
Sebastian Redla29e51b2009-11-08 13:56:19 +0000569
Mike Stump1eb44332009-09-09 15:08:12 +0000570 /// \brief Transforms a template type parameter type by performing
Douglas Gregor577f75a2009-08-04 16:50:30 +0000571 /// substitution of the corresponding template type argument.
John McCalla2becad2009-10-21 00:40:46 +0000572 QualType TransformTemplateTypeParmType(TypeLocBuilder &TLB,
573 TemplateTypeParmTypeLoc TL);
Douglas Gregor577f75a2009-08-04 16:50:30 +0000574 };
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000575}
576
Douglas Gregor577f75a2009-08-04 16:50:30 +0000577Decl *TemplateInstantiator::TransformDecl(Decl *D) {
Douglas Gregorc68afe22009-09-03 21:38:09 +0000578 if (!D)
579 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000580
Douglas Gregorc68afe22009-09-03 21:38:09 +0000581 if (TemplateTemplateParmDecl *TTP = dyn_cast<TemplateTemplateParmDecl>(D)) {
Douglas Gregord6350ae2009-08-28 20:31:08 +0000582 if (TTP->getDepth() < TemplateArgs.getNumLevels()) {
Douglas Gregor6d3e6272010-02-05 19:54:12 +0000583 // If the corresponding template argument is NULL or non-existent, it's
584 // because we are performing instantiation from explicitly-specified
585 // template arguments in a function template, but there were some
586 // arguments left unspecified.
587 if (!TemplateArgs.hasTemplateArgument(TTP->getDepth(),
588 TTP->getPosition()))
589 return D;
590
Douglas Gregor788cd062009-11-11 01:00:40 +0000591 TemplateName Template
592 = TemplateArgs(TTP->getDepth(), TTP->getPosition()).getAsTemplate();
593 assert(!Template.isNull() && Template.getAsTemplateDecl() &&
Douglas Gregord6350ae2009-08-28 20:31:08 +0000594 "Wrong kind of template template argument");
Douglas Gregor788cd062009-11-11 01:00:40 +0000595 return Template.getAsTemplateDecl();
Douglas Gregord6350ae2009-08-28 20:31:08 +0000596 }
Mike Stump1eb44332009-09-09 15:08:12 +0000597
Douglas Gregor788cd062009-11-11 01:00:40 +0000598 // Fall through to find the instantiated declaration for this template
599 // template parameter.
Douglas Gregord1067e52009-08-06 06:41:21 +0000600 }
Mike Stump1eb44332009-09-09 15:08:12 +0000601
Douglas Gregore95b4092009-09-16 18:34:49 +0000602 return SemaRef.FindInstantiatedDecl(cast<NamedDecl>(D), TemplateArgs);
Douglas Gregor577f75a2009-08-04 16:50:30 +0000603}
604
Douglas Gregor43959a92009-08-20 07:17:43 +0000605Decl *TemplateInstantiator::TransformDefinition(Decl *D) {
John McCallce3ff2b2009-08-25 22:02:44 +0000606 Decl *Inst = getSema().SubstDecl(D, getSema().CurContext, TemplateArgs);
Douglas Gregor43959a92009-08-20 07:17:43 +0000607 if (!Inst)
608 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000609
Douglas Gregor43959a92009-08-20 07:17:43 +0000610 getSema().CurrentInstantiationScope->InstantiatedLocal(D, Inst);
611 return Inst;
612}
613
Douglas Gregor6cd21982009-10-20 05:58:46 +0000614NamedDecl *
615TemplateInstantiator::TransformFirstQualifierInScope(NamedDecl *D,
616 SourceLocation Loc) {
617 // If the first part of the nested-name-specifier was a template type
618 // parameter, instantiate that type parameter down to a tag type.
619 if (TemplateTypeParmDecl *TTPD = dyn_cast_or_null<TemplateTypeParmDecl>(D)) {
620 const TemplateTypeParmType *TTP
621 = cast<TemplateTypeParmType>(getSema().Context.getTypeDeclType(TTPD));
622 if (TTP->getDepth() < TemplateArgs.getNumLevels()) {
623 QualType T = TemplateArgs(TTP->getDepth(), TTP->getIndex()).getAsType();
624 if (T.isNull())
625 return cast_or_null<NamedDecl>(TransformDecl(D));
626
627 if (const TagType *Tag = T->getAs<TagType>())
628 return Tag->getDecl();
629
630 // The resulting type is not a tag; complain.
631 getSema().Diag(Loc, diag::err_nested_name_spec_non_tag) << T;
632 return 0;
633 }
634 }
635
636 return cast_or_null<NamedDecl>(TransformDecl(D));
637}
638
Douglas Gregor43959a92009-08-20 07:17:43 +0000639VarDecl *
640TemplateInstantiator::RebuildExceptionDecl(VarDecl *ExceptionDecl,
Mike Stump1eb44332009-09-09 15:08:12 +0000641 QualType T,
John McCalla93c9342009-12-07 02:54:59 +0000642 TypeSourceInfo *Declarator,
Douglas Gregor43959a92009-08-20 07:17:43 +0000643 IdentifierInfo *Name,
Mike Stump1eb44332009-09-09 15:08:12 +0000644 SourceLocation Loc,
Douglas Gregor43959a92009-08-20 07:17:43 +0000645 SourceRange TypeRange) {
646 VarDecl *Var = inherited::RebuildExceptionDecl(ExceptionDecl, T, Declarator,
647 Name, Loc, TypeRange);
648 if (Var && !Var->isInvalidDecl())
649 getSema().CurrentInstantiationScope->InstantiatedLocal(ExceptionDecl, Var);
650 return Var;
651}
652
John McCallc4e70192009-09-11 04:59:25 +0000653QualType
654TemplateInstantiator::RebuildElaboratedType(QualType T,
655 ElaboratedType::TagKind Tag) {
656 if (const TagType *TT = T->getAs<TagType>()) {
657 TagDecl* TD = TT->getDecl();
658
659 // FIXME: this location is very wrong; we really need typelocs.
660 SourceLocation TagLocation = TD->getTagKeywordLoc();
661
662 // FIXME: type might be anonymous.
663 IdentifierInfo *Id = TD->getIdentifier();
664
665 // TODO: should we even warn on struct/class mismatches for this? Seems
666 // like it's likely to produce a lot of spurious errors.
667 if (!SemaRef.isAcceptableTagRedeclaration(TD, Tag, TagLocation, *Id)) {
668 SemaRef.Diag(TagLocation, diag::err_use_with_wrong_tag)
669 << Id
670 << CodeModificationHint::CreateReplacement(SourceRange(TagLocation),
671 TD->getKindName());
672 SemaRef.Diag(TD->getLocation(), diag::note_previous_use);
673 }
674 }
675
676 return TreeTransform<TemplateInstantiator>::RebuildElaboratedType(T, Tag);
677}
678
679Sema::OwningExprResult
John McCall454feb92009-12-08 09:21:05 +0000680TemplateInstantiator::TransformPredefinedExpr(PredefinedExpr *E) {
Anders Carlsson773f3972009-09-11 01:22:35 +0000681 if (!E->isTypeDependent())
682 return SemaRef.Owned(E->Retain());
683
684 FunctionDecl *currentDecl = getSema().getCurFunctionDecl();
685 assert(currentDecl && "Must have current function declaration when "
686 "instantiating.");
687
688 PredefinedExpr::IdentType IT = E->getIdentType();
689
Anders Carlsson848fa642010-02-11 18:20:28 +0000690 unsigned Length = PredefinedExpr::ComputeName(IT, currentDecl).length();
Anders Carlsson773f3972009-09-11 01:22:35 +0000691
692 llvm::APInt LengthI(32, Length + 1);
John McCall0953e762009-09-24 19:53:00 +0000693 QualType ResTy = getSema().Context.CharTy.withConst();
Anders Carlsson773f3972009-09-11 01:22:35 +0000694 ResTy = getSema().Context.getConstantArrayType(ResTy, LengthI,
695 ArrayType::Normal, 0);
696 PredefinedExpr *PE =
697 new (getSema().Context) PredefinedExpr(E->getLocation(), ResTy, IT);
698 return getSema().Owned(PE);
699}
700
701Sema::OwningExprResult
John McCallb8fc0532010-02-06 08:42:39 +0000702TemplateInstantiator::TransformTemplateParmRefExpr(DeclRefExpr *E,
Douglas Gregordcee9802010-02-08 23:41:45 +0000703 NonTypeTemplateParmDecl *NTTP) {
John McCallb8fc0532010-02-06 08:42:39 +0000704 // If the corresponding template argument is NULL or non-existent, it's
705 // because we are performing instantiation from explicitly-specified
706 // template arguments in a function template, but there were some
707 // arguments left unspecified.
708 if (!TemplateArgs.hasTemplateArgument(NTTP->getDepth(),
709 NTTP->getPosition()))
710 return SemaRef.Owned(E->Retain());
Mike Stump1eb44332009-09-09 15:08:12 +0000711
John McCallb8fc0532010-02-06 08:42:39 +0000712 const TemplateArgument &Arg = TemplateArgs(NTTP->getDepth(),
713 NTTP->getPosition());
Mike Stump1eb44332009-09-09 15:08:12 +0000714
John McCallb8fc0532010-02-06 08:42:39 +0000715 // The template argument itself might be an expression, in which
716 // case we just return that expression.
717 if (Arg.getKind() == TemplateArgument::Expression)
718 return SemaRef.Owned(Arg.getAsExpr()->Retain());
Mike Stump1eb44332009-09-09 15:08:12 +0000719
John McCallb8fc0532010-02-06 08:42:39 +0000720 if (Arg.getKind() == TemplateArgument::Declaration) {
721 ValueDecl *VD = cast<ValueDecl>(Arg.getAsDecl());
Mike Stump1eb44332009-09-09 15:08:12 +0000722
John McCall645cf442010-02-06 10:23:53 +0000723 // Find the instantiation of the template argument. This is
724 // required for nested templates.
John McCallb8fc0532010-02-06 08:42:39 +0000725 VD = cast_or_null<ValueDecl>(
Douglas Gregorc86a6e92009-11-04 07:01:15 +0000726 getSema().FindInstantiatedDecl(VD, TemplateArgs));
John McCallb8fc0532010-02-06 08:42:39 +0000727 if (!VD)
728 return SemaRef.ExprError();
Mike Stump1eb44332009-09-09 15:08:12 +0000729
John McCall645cf442010-02-06 10:23:53 +0000730 // Derive the type we want the substituted decl to have. This had
731 // better be non-dependent, or these checks will have serious problems.
732 QualType TargetType = SemaRef.SubstType(NTTP->getType(), TemplateArgs,
Douglas Gregordcee9802010-02-08 23:41:45 +0000733 E->getLocation(),
734 DeclarationName());
John McCall645cf442010-02-06 10:23:53 +0000735 assert(!TargetType.isNull() && "type substitution failed for param type");
736 assert(!TargetType->isDependentType() && "param type still dependent");
737
John McCallb8fc0532010-02-06 08:42:39 +0000738 if (VD->getDeclContext()->isRecord() &&
739 (isa<CXXMethodDecl>(VD) || isa<FieldDecl>(VD))) {
740 // If the value is a class member, we might have a pointer-to-member.
741 // Determine whether the non-type template template parameter is of
742 // pointer-to-member type. If so, we need to build an appropriate
743 // expression for a pointer-to-member, since a "normal" DeclRefExpr
744 // would refer to the member itself.
John McCall645cf442010-02-06 10:23:53 +0000745 if (TargetType->isMemberPointerType()) {
John McCallb8fc0532010-02-06 08:42:39 +0000746 QualType ClassType
747 = SemaRef.Context.getTypeDeclType(
Douglas Gregor231edff2009-11-12 17:40:13 +0000748 cast<RecordDecl>(VD->getDeclContext()));
John McCallb8fc0532010-02-06 08:42:39 +0000749 NestedNameSpecifier *Qualifier
750 = NestedNameSpecifier::Create(SemaRef.Context, 0, false,
751 ClassType.getTypePtr());
752 CXXScopeSpec SS;
753 SS.setScopeRep(Qualifier);
754 OwningExprResult RefExpr
755 = SemaRef.BuildDeclRefExpr(VD,
756 VD->getType().getNonReferenceType(),
757 E->getLocation(),
758 &SS);
759 if (RefExpr.isInvalid())
760 return SemaRef.ExprError();
John McCall645cf442010-02-06 10:23:53 +0000761
762 RefExpr = SemaRef.CreateBuiltinUnaryOp(E->getLocation(),
763 UnaryOperator::AddrOf,
764 move(RefExpr));
765 assert(!RefExpr.isInvalid() &&
766 SemaRef.Context.hasSameType(((Expr*) RefExpr.get())->getType(),
767 TargetType));
768 return move(RefExpr);
John McCallb8fc0532010-02-06 08:42:39 +0000769 }
770 }
John McCall645cf442010-02-06 10:23:53 +0000771
Douglas Gregordcee9802010-02-08 23:41:45 +0000772 QualType T = VD->getType().getNonReferenceType();
773
John McCall645cf442010-02-06 10:23:53 +0000774 if (TargetType->isPointerType()) {
775 // C++03 [temp.arg.nontype]p5:
776 // - For a non-type template-parameter of type pointer to
777 // object, qualification conversions and the array-to-pointer
778 // conversion are applied.
779 // - For a non-type template-parameter of type pointer to
780 // function, only the function-to-pointer conversion is
781 // applied.
782
John McCallb8fc0532010-02-06 08:42:39 +0000783 OwningExprResult RefExpr
Douglas Gregordcee9802010-02-08 23:41:45 +0000784 = SemaRef.BuildDeclRefExpr(VD, T, E->getLocation());
John McCallb8fc0532010-02-06 08:42:39 +0000785 if (RefExpr.isInvalid())
786 return SemaRef.ExprError();
Chandler Carruth548028b2010-01-31 07:09:11 +0000787
John McCallb8fc0532010-02-06 08:42:39 +0000788 // Decay functions and arrays.
789 Expr *RefE = (Expr *)RefExpr.get();
790 SemaRef.DefaultFunctionArrayConversion(RefE);
791 if (RefE != RefExpr.get()) {
792 RefExpr.release();
793 RefExpr = SemaRef.Owned(RefE);
Douglas Gregor550d9b22009-10-31 17:21:17 +0000794 }
Mike Stump1eb44332009-09-09 15:08:12 +0000795
John McCall645cf442010-02-06 10:23:53 +0000796 // Qualification conversions.
John McCallb8fc0532010-02-06 08:42:39 +0000797 RefExpr.release();
John McCall645cf442010-02-06 10:23:53 +0000798 SemaRef.ImpCastExprToType(RefE, TargetType.getUnqualifiedType(),
John McCallb8fc0532010-02-06 08:42:39 +0000799 CastExpr::CK_NoOp);
800 return SemaRef.Owned(RefE);
801 }
802
Douglas Gregordcee9802010-02-08 23:41:45 +0000803 // If the non-type template parameter has reference type, qualify the
804 // resulting declaration reference with the extra qualifiers on the
805 // type that the reference refers to.
806 if (const ReferenceType *TargetRef = TargetType->getAs<ReferenceType>())
807 T = SemaRef.Context.getQualifiedType(T,
808 TargetRef->getPointeeType().getQualifiers());
809
810 return SemaRef.BuildDeclRefExpr(VD, T, E->getLocation());
John McCallb8fc0532010-02-06 08:42:39 +0000811 }
812
813 assert(Arg.getKind() == TemplateArgument::Integral);
814 QualType T = Arg.getIntegralType();
815 if (T->isCharType() || T->isWideCharType())
816 return SemaRef.Owned(new (SemaRef.Context) CharacterLiteral(
Douglas Gregor550d9b22009-10-31 17:21:17 +0000817 Arg.getAsIntegral()->getZExtValue(),
818 T->isWideCharType(),
Mike Stump1eb44332009-09-09 15:08:12 +0000819 T,
Douglas Gregord6350ae2009-08-28 20:31:08 +0000820 E->getSourceRange().getBegin()));
John McCallb8fc0532010-02-06 08:42:39 +0000821 if (T->isBooleanType())
822 return SemaRef.Owned(new (SemaRef.Context) CXXBoolLiteralExpr(
Douglas Gregor550d9b22009-10-31 17:21:17 +0000823 Arg.getAsIntegral()->getBoolValue(),
824 T,
825 E->getSourceRange().getBegin()));
826
John McCallb8fc0532010-02-06 08:42:39 +0000827 assert(Arg.getAsIntegral()->getBitWidth() == SemaRef.Context.getIntWidth(T));
828 return SemaRef.Owned(new (SemaRef.Context) IntegerLiteral(
Douglas Gregor550d9b22009-10-31 17:21:17 +0000829 *Arg.getAsIntegral(),
830 T,
831 E->getSourceRange().getBegin()));
John McCallb8fc0532010-02-06 08:42:39 +0000832}
833
834
835Sema::OwningExprResult
836TemplateInstantiator::TransformDeclRefExpr(DeclRefExpr *E) {
837 NamedDecl *D = E->getDecl();
838 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(D)) {
839 if (NTTP->getDepth() < TemplateArgs.getNumLevels())
840 return TransformTemplateParmRefExpr(E, NTTP);
Douglas Gregor550d9b22009-10-31 17:21:17 +0000841
842 // We have a non-type template parameter that isn't fully substituted;
843 // FindInstantiatedDecl will find it in the local instantiation scope.
Douglas Gregorb98b1992009-08-11 05:31:07 +0000844 }
Mike Stump1eb44332009-09-09 15:08:12 +0000845
John McCall454feb92009-12-08 09:21:05 +0000846 return TreeTransform<TemplateInstantiator>::TransformDeclRefExpr(E);
Douglas Gregorb98b1992009-08-11 05:31:07 +0000847}
848
Sebastian Redla29e51b2009-11-08 13:56:19 +0000849Sema::OwningExprResult TemplateInstantiator::TransformCXXDefaultArgExpr(
John McCall454feb92009-12-08 09:21:05 +0000850 CXXDefaultArgExpr *E) {
Sebastian Redla29e51b2009-11-08 13:56:19 +0000851 assert(!cast<FunctionDecl>(E->getParam()->getDeclContext())->
852 getDescribedFunctionTemplate() &&
853 "Default arg expressions are never formed in dependent cases.");
Douglas Gregor036aed12009-12-23 23:03:06 +0000854 return SemaRef.BuildCXXDefaultArgExpr(E->getUsedLocation(),
855 cast<FunctionDecl>(E->getParam()->getDeclContext()),
856 E->getParam());
Sebastian Redla29e51b2009-11-08 13:56:19 +0000857}
858
859
Mike Stump1eb44332009-09-09 15:08:12 +0000860QualType
John McCalla2becad2009-10-21 00:40:46 +0000861TemplateInstantiator::TransformTemplateTypeParmType(TypeLocBuilder &TLB,
862 TemplateTypeParmTypeLoc TL) {
863 TemplateTypeParmType *T = TL.getTypePtr();
Douglas Gregord6350ae2009-08-28 20:31:08 +0000864 if (T->getDepth() < TemplateArgs.getNumLevels()) {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000865 // Replace the template type parameter with its corresponding
866 // template argument.
Mike Stump1eb44332009-09-09 15:08:12 +0000867
868 // If the corresponding template argument is NULL or doesn't exist, it's
869 // because we are performing instantiation from explicitly-specified
870 // template arguments in a function template class, but there were some
Douglas Gregor16134c62009-07-01 00:28:38 +0000871 // arguments left unspecified.
John McCalla2becad2009-10-21 00:40:46 +0000872 if (!TemplateArgs.hasTemplateArgument(T->getDepth(), T->getIndex())) {
873 TemplateTypeParmTypeLoc NewTL
874 = TLB.push<TemplateTypeParmTypeLoc>(TL.getType());
875 NewTL.setNameLoc(TL.getNameLoc());
876 return TL.getType();
877 }
Mike Stump1eb44332009-09-09 15:08:12 +0000878
879 assert(TemplateArgs(T->getDepth(), T->getIndex()).getKind()
Douglas Gregord6350ae2009-08-28 20:31:08 +0000880 == TemplateArgument::Type &&
Douglas Gregor99ebf652009-02-27 19:31:52 +0000881 "Template argument kind mismatch");
Douglas Gregord6350ae2009-08-28 20:31:08 +0000882
John McCall49a832b2009-10-18 09:09:24 +0000883 QualType Replacement
884 = TemplateArgs(T->getDepth(), T->getIndex()).getAsType();
885
886 // TODO: only do this uniquing once, at the start of instantiation.
John McCalla2becad2009-10-21 00:40:46 +0000887 QualType Result
888 = getSema().Context.getSubstTemplateTypeParmType(T, Replacement);
889 SubstTemplateTypeParmTypeLoc NewTL
890 = TLB.push<SubstTemplateTypeParmTypeLoc>(Result);
891 NewTL.setNameLoc(TL.getNameLoc());
892 return Result;
Mike Stump1eb44332009-09-09 15:08:12 +0000893 }
Douglas Gregor99ebf652009-02-27 19:31:52 +0000894
895 // The template type parameter comes from an inner template (e.g.,
896 // the template parameter list of a member template inside the
897 // template we are instantiating). Create a new template type
898 // parameter with the template "level" reduced by one.
John McCalla2becad2009-10-21 00:40:46 +0000899 QualType Result
900 = getSema().Context.getTemplateTypeParmType(T->getDepth()
901 - TemplateArgs.getNumLevels(),
902 T->getIndex(),
903 T->isParameterPack(),
904 T->getName());
905 TemplateTypeParmTypeLoc NewTL = TLB.push<TemplateTypeParmTypeLoc>(Result);
906 NewTL.setNameLoc(TL.getNameLoc());
907 return Result;
Douglas Gregorcd281c32009-02-28 00:25:32 +0000908}
Douglas Gregor99ebf652009-02-27 19:31:52 +0000909
John McCallce3ff2b2009-08-25 22:02:44 +0000910/// \brief Perform substitution on the type T with a given set of template
911/// arguments.
Douglas Gregor99ebf652009-02-27 19:31:52 +0000912///
913/// This routine substitutes the given template arguments into the
914/// type T and produces the instantiated type.
915///
916/// \param T the type into which the template arguments will be
917/// substituted. If this type is not dependent, it will be returned
918/// immediately.
919///
920/// \param TemplateArgs the template arguments that will be
921/// substituted for the top-level template parameters within T.
922///
Douglas Gregor99ebf652009-02-27 19:31:52 +0000923/// \param Loc the location in the source code where this substitution
924/// is being performed. It will typically be the location of the
925/// declarator (if we're instantiating the type of some declaration)
926/// or the location of the type in the source code (if, e.g., we're
927/// instantiating the type of a cast expression).
928///
929/// \param Entity the name of the entity associated with a declaration
930/// being instantiated (if any). May be empty to indicate that there
931/// is no such entity (if, e.g., this is a type that occurs as part of
932/// a cast expression) or that the entity has no name (e.g., an
933/// unnamed function parameter).
934///
935/// \returns If the instantiation succeeds, the instantiated
936/// type. Otherwise, produces diagnostics and returns a NULL type.
John McCalla93c9342009-12-07 02:54:59 +0000937TypeSourceInfo *Sema::SubstType(TypeSourceInfo *T,
John McCallcd7ba1c2009-10-21 00:58:09 +0000938 const MultiLevelTemplateArgumentList &Args,
939 SourceLocation Loc,
940 DeclarationName Entity) {
941 assert(!ActiveTemplateInstantiations.empty() &&
942 "Cannot perform an instantiation without some context on the "
943 "instantiation stack");
944
945 if (!T->getType()->isDependentType())
946 return T;
947
948 TemplateInstantiator Instantiator(*this, Args, Loc, Entity);
949 return Instantiator.TransformType(T);
950}
951
952/// Deprecated form of the above.
Mike Stump1eb44332009-09-09 15:08:12 +0000953QualType Sema::SubstType(QualType T,
Douglas Gregord6350ae2009-08-28 20:31:08 +0000954 const MultiLevelTemplateArgumentList &TemplateArgs,
John McCallce3ff2b2009-08-25 22:02:44 +0000955 SourceLocation Loc, DeclarationName Entity) {
Douglas Gregordf667e72009-03-10 20:44:00 +0000956 assert(!ActiveTemplateInstantiations.empty() &&
957 "Cannot perform an instantiation without some context on the "
958 "instantiation stack");
959
Douglas Gregor99ebf652009-02-27 19:31:52 +0000960 // If T is not a dependent type, there is nothing to do.
961 if (!T->isDependentType())
962 return T;
963
Douglas Gregor577f75a2009-08-04 16:50:30 +0000964 TemplateInstantiator Instantiator(*this, TemplateArgs, Loc, Entity);
965 return Instantiator.TransformType(T);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000966}
Douglas Gregor2943aed2009-03-03 04:44:36 +0000967
John McCallce3ff2b2009-08-25 22:02:44 +0000968/// \brief Perform substitution on the base class specifiers of the
969/// given class template specialization.
Douglas Gregor2943aed2009-03-03 04:44:36 +0000970///
971/// Produces a diagnostic and returns true on error, returns false and
972/// attaches the instantiated base classes to the class template
973/// specialization if successful.
Mike Stump1eb44332009-09-09 15:08:12 +0000974bool
John McCallce3ff2b2009-08-25 22:02:44 +0000975Sema::SubstBaseSpecifiers(CXXRecordDecl *Instantiation,
976 CXXRecordDecl *Pattern,
Douglas Gregord6350ae2009-08-28 20:31:08 +0000977 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor2943aed2009-03-03 04:44:36 +0000978 bool Invalid = false;
Douglas Gregor0ca20ac2009-05-29 18:27:38 +0000979 llvm::SmallVector<CXXBaseSpecifier*, 4> InstantiatedBases;
Mike Stump1eb44332009-09-09 15:08:12 +0000980 for (ClassTemplateSpecializationDecl::base_class_iterator
Douglas Gregord475b8d2009-03-25 21:17:03 +0000981 Base = Pattern->bases_begin(), BaseEnd = Pattern->bases_end();
Douglas Gregor27b152f2009-03-10 18:52:44 +0000982 Base != BaseEnd; ++Base) {
Douglas Gregor2943aed2009-03-03 04:44:36 +0000983 if (!Base->getType()->isDependentType()) {
Anders Carlsson51f94042009-12-03 17:49:57 +0000984 const CXXRecordDecl *BaseDecl =
985 cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
986
987 // Make sure to set the attributes from the base.
988 SetClassDeclAttributesFromBase(Instantiation, BaseDecl,
989 Base->isVirtual());
990
Fariborz Jahanian71c6e712009-07-22 17:41:53 +0000991 InstantiatedBases.push_back(new (Context) CXXBaseSpecifier(*Base));
Douglas Gregor2943aed2009-03-03 04:44:36 +0000992 continue;
993 }
994
Mike Stump1eb44332009-09-09 15:08:12 +0000995 QualType BaseType = SubstType(Base->getType(),
996 TemplateArgs,
John McCallce3ff2b2009-08-25 22:02:44 +0000997 Base->getSourceRange().getBegin(),
998 DeclarationName());
Douglas Gregor2943aed2009-03-03 04:44:36 +0000999 if (BaseType.isNull()) {
1000 Invalid = true;
1001 continue;
1002 }
1003
1004 if (CXXBaseSpecifier *InstantiatedBase
Douglas Gregord475b8d2009-03-25 21:17:03 +00001005 = CheckBaseSpecifier(Instantiation,
Douglas Gregor2943aed2009-03-03 04:44:36 +00001006 Base->getSourceRange(),
1007 Base->isVirtual(),
1008 Base->getAccessSpecifierAsWritten(),
1009 BaseType,
1010 /*FIXME: Not totally accurate */
1011 Base->getSourceRange().getBegin()))
1012 InstantiatedBases.push_back(InstantiatedBase);
1013 else
1014 Invalid = true;
1015 }
1016
Douglas Gregor27b152f2009-03-10 18:52:44 +00001017 if (!Invalid &&
Jay Foadbeaaccd2009-05-21 09:52:38 +00001018 AttachBaseSpecifiers(Instantiation, InstantiatedBases.data(),
Douglas Gregor2943aed2009-03-03 04:44:36 +00001019 InstantiatedBases.size()))
1020 Invalid = true;
1021
1022 return Invalid;
1023}
1024
Douglas Gregord475b8d2009-03-25 21:17:03 +00001025/// \brief Instantiate the definition of a class from a given pattern.
1026///
1027/// \param PointOfInstantiation The point of instantiation within the
1028/// source code.
1029///
1030/// \param Instantiation is the declaration whose definition is being
1031/// instantiated. This will be either a class template specialization
1032/// or a member class of a class template specialization.
1033///
1034/// \param Pattern is the pattern from which the instantiation
1035/// occurs. This will be either the declaration of a class template or
1036/// the declaration of a member class of a class template.
1037///
1038/// \param TemplateArgs The template arguments to be substituted into
1039/// the pattern.
1040///
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001041/// \param TSK the kind of implicit or explicit instantiation to perform.
Douglas Gregor5842ba92009-08-24 15:23:48 +00001042///
1043/// \param Complain whether to complain if the class cannot be instantiated due
1044/// to the lack of a definition.
1045///
Douglas Gregord475b8d2009-03-25 21:17:03 +00001046/// \returns true if an error occurred, false otherwise.
1047bool
1048Sema::InstantiateClass(SourceLocation PointOfInstantiation,
1049 CXXRecordDecl *Instantiation, CXXRecordDecl *Pattern,
Douglas Gregord6350ae2009-08-28 20:31:08 +00001050 const MultiLevelTemplateArgumentList &TemplateArgs,
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001051 TemplateSpecializationKind TSK,
Douglas Gregor5842ba92009-08-24 15:23:48 +00001052 bool Complain) {
Douglas Gregord475b8d2009-03-25 21:17:03 +00001053 bool Invalid = false;
John McCalle29ba202009-08-20 01:44:21 +00001054
Mike Stump1eb44332009-09-09 15:08:12 +00001055 CXXRecordDecl *PatternDef
Douglas Gregor952b0172010-02-11 01:04:33 +00001056 = cast_or_null<CXXRecordDecl>(Pattern->getDefinition());
Douglas Gregord475b8d2009-03-25 21:17:03 +00001057 if (!PatternDef) {
Douglas Gregor5842ba92009-08-24 15:23:48 +00001058 if (!Complain) {
1059 // Say nothing
1060 } else if (Pattern == Instantiation->getInstantiatedFromMemberClass()) {
Douglas Gregord475b8d2009-03-25 21:17:03 +00001061 Diag(PointOfInstantiation,
1062 diag::err_implicit_instantiate_member_undefined)
1063 << Context.getTypeDeclType(Instantiation);
1064 Diag(Pattern->getLocation(), diag::note_member_of_template_here);
1065 } else {
Douglas Gregor93dfdb12009-05-13 00:25:59 +00001066 Diag(PointOfInstantiation, diag::err_template_instantiate_undefined)
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001067 << (TSK != TSK_ImplicitInstantiation)
Douglas Gregord475b8d2009-03-25 21:17:03 +00001068 << Context.getTypeDeclType(Instantiation);
1069 Diag(Pattern->getLocation(), diag::note_template_decl_here);
1070 }
1071 return true;
1072 }
1073 Pattern = PatternDef;
1074
Douglas Gregor454885e2009-10-15 15:54:05 +00001075 // \brief Record the point of instantiation.
1076 if (MemberSpecializationInfo *MSInfo
1077 = Instantiation->getMemberSpecializationInfo()) {
1078 MSInfo->setTemplateSpecializationKind(TSK);
1079 MSInfo->setPointOfInstantiation(PointOfInstantiation);
Douglas Gregor972e6ce2009-10-27 06:26:26 +00001080 } else if (ClassTemplateSpecializationDecl *Spec
1081 = dyn_cast<ClassTemplateSpecializationDecl>(Instantiation)) {
1082 Spec->setTemplateSpecializationKind(TSK);
1083 Spec->setPointOfInstantiation(PointOfInstantiation);
Douglas Gregor454885e2009-10-15 15:54:05 +00001084 }
1085
Douglas Gregord048bb72009-03-25 21:23:52 +00001086 InstantiatingTemplate Inst(*this, PointOfInstantiation, Instantiation);
Douglas Gregord475b8d2009-03-25 21:17:03 +00001087 if (Inst)
1088 return true;
1089
1090 // Enter the scope of this instantiation. We don't use
1091 // PushDeclContext because we don't have a scope.
1092 DeclContext *PreviousContext = CurContext;
1093 CurContext = Instantiation;
1094
1095 // Start the definition of this instantiation.
1096 Instantiation->startDefinition();
1097
John McCallce3ff2b2009-08-25 22:02:44 +00001098 // Do substitution on the base class specifiers.
1099 if (SubstBaseSpecifiers(Instantiation, Pattern, TemplateArgs))
Douglas Gregord475b8d2009-03-25 21:17:03 +00001100 Invalid = true;
1101
Douglas Gregor0ca20ac2009-05-29 18:27:38 +00001102 llvm::SmallVector<DeclPtrTy, 4> Fields;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001103 for (RecordDecl::decl_iterator Member = Pattern->decls_begin(),
Mike Stump1eb44332009-09-09 15:08:12 +00001104 MemberEnd = Pattern->decls_end();
Douglas Gregor6ab35242009-04-09 21:40:53 +00001105 Member != MemberEnd; ++Member) {
John McCallce3ff2b2009-08-25 22:02:44 +00001106 Decl *NewMember = SubstDecl(*Member, Instantiation, TemplateArgs);
Douglas Gregord475b8d2009-03-25 21:17:03 +00001107 if (NewMember) {
Eli Friedman721e77d2009-12-07 00:22:08 +00001108 if (FieldDecl *Field = dyn_cast<FieldDecl>(NewMember))
Chris Lattnerb28317a2009-03-28 19:18:32 +00001109 Fields.push_back(DeclPtrTy::make(Field));
Eli Friedman721e77d2009-12-07 00:22:08 +00001110 else if (NewMember->isInvalidDecl())
1111 Invalid = true;
Douglas Gregord475b8d2009-03-25 21:17:03 +00001112 } else {
1113 // FIXME: Eventually, a NULL return will mean that one of the
Mike Stump390b4cc2009-05-16 07:39:55 +00001114 // instantiations was a semantic disaster, and we'll want to set Invalid =
1115 // true. For now, we expect to skip some members that we can't yet handle.
Douglas Gregord475b8d2009-03-25 21:17:03 +00001116 }
1117 }
1118
1119 // Finish checking fields.
Chris Lattnerb28317a2009-03-28 19:18:32 +00001120 ActOnFields(0, Instantiation->getLocation(), DeclPtrTy::make(Instantiation),
Jay Foadbeaaccd2009-05-21 09:52:38 +00001121 Fields.data(), Fields.size(), SourceLocation(), SourceLocation(),
Douglas Gregord475b8d2009-03-25 21:17:03 +00001122 0);
Douglas Gregor1ab537b2009-12-03 18:33:45 +00001123 CheckCompletedCXXClass(Instantiation);
Douglas Gregor663b5a02009-10-14 20:14:33 +00001124 if (Instantiation->isInvalidDecl())
1125 Invalid = true;
1126
Douglas Gregord475b8d2009-03-25 21:17:03 +00001127 // Exit the scope of this instantiation.
1128 CurContext = PreviousContext;
1129
Douglas Gregor159ef1e2010-01-06 04:44:19 +00001130 // If this is a polymorphic C++ class without a key function, we'll
1131 // have to mark all of the virtual members to allow emission of a vtable
1132 // in this translation unit.
Chandler Carruth17e0f402010-02-15 22:12:26 +00001133 if (Instantiation->isDynamicClass() &&
1134 !Context.getKeyFunction(Instantiation)) {
1135 // Local classes need to have their methods instantiated immediately in
1136 // order to have the correct instantiation scope.
1137 if (Instantiation->isLocalClass()) {
1138 MarkVirtualMembersReferenced(PointOfInstantiation,
1139 Instantiation);
1140 } else {
Douglas Gregor159ef1e2010-01-06 04:44:19 +00001141 ClassesWithUnmarkedVirtualMembers.push_back(std::make_pair(Instantiation,
1142 PointOfInstantiation));
Chandler Carruth17e0f402010-02-15 22:12:26 +00001143 }
1144 }
Douglas Gregor159ef1e2010-01-06 04:44:19 +00001145
Douglas Gregoraba43bb2009-05-26 20:50:29 +00001146 if (!Invalid)
1147 Consumer.HandleTagDeclDefinition(Instantiation);
1148
Douglas Gregord475b8d2009-03-25 21:17:03 +00001149 return Invalid;
1150}
1151
Mike Stump1eb44332009-09-09 15:08:12 +00001152bool
Douglas Gregor2943aed2009-03-03 04:44:36 +00001153Sema::InstantiateClassTemplateSpecialization(
Douglas Gregor972e6ce2009-10-27 06:26:26 +00001154 SourceLocation PointOfInstantiation,
Douglas Gregor2943aed2009-03-03 04:44:36 +00001155 ClassTemplateSpecializationDecl *ClassTemplateSpec,
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001156 TemplateSpecializationKind TSK,
Douglas Gregor5842ba92009-08-24 15:23:48 +00001157 bool Complain) {
Douglas Gregor2943aed2009-03-03 04:44:36 +00001158 // Perform the actual instantiation on the canonical declaration.
1159 ClassTemplateSpec = cast<ClassTemplateSpecializationDecl>(
Argyrios Kyrtzidis97fbaa22009-07-18 00:34:25 +00001160 ClassTemplateSpec->getCanonicalDecl());
Douglas Gregor2943aed2009-03-03 04:44:36 +00001161
Douglas Gregor52604ab2009-09-11 21:19:12 +00001162 // Check whether we have already instantiated or specialized this class
1163 // template specialization.
1164 if (ClassTemplateSpec->getSpecializationKind() != TSK_Undeclared) {
1165 if (ClassTemplateSpec->getSpecializationKind() ==
1166 TSK_ExplicitInstantiationDeclaration &&
1167 TSK == TSK_ExplicitInstantiationDefinition) {
1168 // An explicit instantiation definition follows an explicit instantiation
1169 // declaration (C++0x [temp.explicit]p10); go ahead and perform the
1170 // explicit instantiation.
1171 ClassTemplateSpec->setSpecializationKind(TSK);
Douglas Gregor52604ab2009-09-11 21:19:12 +00001172 return false;
1173 }
1174
1175 // We can only instantiate something that hasn't already been
1176 // instantiated or specialized. Fail without any diagnostics: our
1177 // caller will provide an error message.
Douglas Gregor2943aed2009-03-03 04:44:36 +00001178 return true;
Douglas Gregor52604ab2009-09-11 21:19:12 +00001179 }
Douglas Gregor2943aed2009-03-03 04:44:36 +00001180
Douglas Gregor9eea08b2009-09-15 16:51:42 +00001181 if (ClassTemplateSpec->isInvalidDecl())
1182 return true;
1183
Douglas Gregor2943aed2009-03-03 04:44:36 +00001184 ClassTemplateDecl *Template = ClassTemplateSpec->getSpecializedTemplate();
Douglas Gregord6350ae2009-08-28 20:31:08 +00001185 CXXRecordDecl *Pattern = 0;
Douglas Gregorc8ab2562009-05-31 09:31:02 +00001186
Douglas Gregorc1efb3f2009-06-12 22:31:52 +00001187 // C++ [temp.class.spec.match]p1:
1188 // When a class template is used in a context that requires an
1189 // instantiation of the class, it is necessary to determine
1190 // whether the instantiation is to be generated using the primary
1191 // template or one of the partial specializations. This is done by
1192 // matching the template arguments of the class template
1193 // specialization with the template argument lists of the partial
1194 // specializations.
Douglas Gregor199d9912009-06-05 00:53:49 +00001195 typedef std::pair<ClassTemplatePartialSpecializationDecl *,
1196 TemplateArgumentList *> MatchResult;
1197 llvm::SmallVector<MatchResult, 4> Matched;
Mike Stump1eb44332009-09-09 15:08:12 +00001198 for (llvm::FoldingSet<ClassTemplatePartialSpecializationDecl>::iterator
Douglas Gregorc8ab2562009-05-31 09:31:02 +00001199 Partial = Template->getPartialSpecializations().begin(),
1200 PartialEnd = Template->getPartialSpecializations().end();
1201 Partial != PartialEnd;
1202 ++Partial) {
John McCall5769d612010-02-08 23:07:23 +00001203 TemplateDeductionInfo Info(Context, PointOfInstantiation);
Douglas Gregorf67875d2009-06-12 18:26:56 +00001204 if (TemplateDeductionResult Result
Mike Stump1eb44332009-09-09 15:08:12 +00001205 = DeduceTemplateArguments(&*Partial,
Douglas Gregorf67875d2009-06-12 18:26:56 +00001206 ClassTemplateSpec->getTemplateArgs(),
1207 Info)) {
1208 // FIXME: Store the failed-deduction information for use in
1209 // diagnostics, later.
1210 (void)Result;
1211 } else {
1212 Matched.push_back(std::make_pair(&*Partial, Info.take()));
1213 }
Douglas Gregorc8ab2562009-05-31 09:31:02 +00001214 }
1215
Douglas Gregored9c0f92009-10-29 00:04:11 +00001216 if (Matched.size() >= 1) {
Douglas Gregorbf4ea562009-09-15 16:23:51 +00001217 llvm::SmallVector<MatchResult, 4>::iterator Best = Matched.begin();
Douglas Gregored9c0f92009-10-29 00:04:11 +00001218 if (Matched.size() == 1) {
1219 // -- If exactly one matching specialization is found, the
1220 // instantiation is generated from that specialization.
1221 // We don't need to do anything for this.
1222 } else {
1223 // -- If more than one matching specialization is found, the
1224 // partial order rules (14.5.4.2) are used to determine
1225 // whether one of the specializations is more specialized
1226 // than the others. If none of the specializations is more
1227 // specialized than all of the other matching
1228 // specializations, then the use of the class template is
1229 // ambiguous and the program is ill-formed.
1230 for (llvm::SmallVector<MatchResult, 4>::iterator P = Best + 1,
1231 PEnd = Matched.end();
1232 P != PEnd; ++P) {
John McCall5769d612010-02-08 23:07:23 +00001233 if (getMoreSpecializedPartialSpecialization(P->first, Best->first,
1234 PointOfInstantiation)
Douglas Gregored9c0f92009-10-29 00:04:11 +00001235 == P->first)
1236 Best = P;
Douglas Gregorbf4ea562009-09-15 16:23:51 +00001237 }
Douglas Gregorbf4ea562009-09-15 16:23:51 +00001238
Douglas Gregored9c0f92009-10-29 00:04:11 +00001239 // Determine if the best partial specialization is more specialized than
1240 // the others.
1241 bool Ambiguous = false;
Douglas Gregorbf4ea562009-09-15 16:23:51 +00001242 for (llvm::SmallVector<MatchResult, 4>::iterator P = Matched.begin(),
1243 PEnd = Matched.end();
Douglas Gregored9c0f92009-10-29 00:04:11 +00001244 P != PEnd; ++P) {
1245 if (P != Best &&
John McCall5769d612010-02-08 23:07:23 +00001246 getMoreSpecializedPartialSpecialization(P->first, Best->first,
1247 PointOfInstantiation)
Douglas Gregored9c0f92009-10-29 00:04:11 +00001248 != Best->first) {
1249 Ambiguous = true;
1250 break;
1251 }
1252 }
1253
1254 if (Ambiguous) {
1255 // Partial ordering did not produce a clear winner. Complain.
1256 ClassTemplateSpec->setInvalidDecl();
1257 Diag(PointOfInstantiation, diag::err_partial_spec_ordering_ambiguous)
1258 << ClassTemplateSpec;
1259
1260 // Print the matching partial specializations.
1261 for (llvm::SmallVector<MatchResult, 4>::iterator P = Matched.begin(),
1262 PEnd = Matched.end();
1263 P != PEnd; ++P)
1264 Diag(P->first->getLocation(), diag::note_partial_spec_match)
1265 << getTemplateArgumentBindingsText(P->first->getTemplateParameters(),
1266 *P->second);
Douglas Gregord6350ae2009-08-28 20:31:08 +00001267
Douglas Gregored9c0f92009-10-29 00:04:11 +00001268 return true;
1269 }
Douglas Gregorbf4ea562009-09-15 16:23:51 +00001270 }
1271
1272 // Instantiate using the best class template partial specialization.
Douglas Gregored9c0f92009-10-29 00:04:11 +00001273 ClassTemplatePartialSpecializationDecl *OrigPartialSpec = Best->first;
1274 while (OrigPartialSpec->getInstantiatedFromMember()) {
1275 // If we've found an explicit specialization of this class template,
1276 // stop here and use that as the pattern.
1277 if (OrigPartialSpec->isMemberSpecialization())
1278 break;
1279
1280 OrigPartialSpec = OrigPartialSpec->getInstantiatedFromMember();
1281 }
1282
1283 Pattern = OrigPartialSpec;
Douglas Gregorbf4ea562009-09-15 16:23:51 +00001284 ClassTemplateSpec->setInstantiationOf(Best->first, Best->second);
Douglas Gregorc1efb3f2009-06-12 22:31:52 +00001285 } else {
1286 // -- If no matches are found, the instantiation is generated
1287 // from the primary template.
Douglas Gregord6350ae2009-08-28 20:31:08 +00001288 ClassTemplateDecl *OrigTemplate = Template;
Douglas Gregorfd056bc2009-10-13 16:30:37 +00001289 while (OrigTemplate->getInstantiatedFromMemberTemplate()) {
1290 // If we've found an explicit specialization of this class template,
1291 // stop here and use that as the pattern.
1292 if (OrigTemplate->isMemberSpecialization())
1293 break;
1294
Douglas Gregord6350ae2009-08-28 20:31:08 +00001295 OrigTemplate = OrigTemplate->getInstantiatedFromMemberTemplate();
Douglas Gregorfd056bc2009-10-13 16:30:37 +00001296 }
1297
Douglas Gregord6350ae2009-08-28 20:31:08 +00001298 Pattern = OrigTemplate->getTemplatedDecl();
Douglas Gregorc8ab2562009-05-31 09:31:02 +00001299 }
Douglas Gregor2943aed2009-03-03 04:44:36 +00001300
Douglas Gregor972e6ce2009-10-27 06:26:26 +00001301 bool Result = InstantiateClass(PointOfInstantiation, ClassTemplateSpec,
1302 Pattern,
1303 getTemplateInstantiationArgs(ClassTemplateSpec),
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001304 TSK,
Douglas Gregor5842ba92009-08-24 15:23:48 +00001305 Complain);
Mike Stump1eb44332009-09-09 15:08:12 +00001306
Douglas Gregor199d9912009-06-05 00:53:49 +00001307 for (unsigned I = 0, N = Matched.size(); I != N; ++I) {
1308 // FIXME: Implement TemplateArgumentList::Destroy!
1309 // if (Matched[I].first != Pattern)
1310 // Matched[I].second->Destroy(Context);
1311 }
Mike Stump1eb44332009-09-09 15:08:12 +00001312
Douglas Gregor199d9912009-06-05 00:53:49 +00001313 return Result;
Douglas Gregor2943aed2009-03-03 04:44:36 +00001314}
Douglas Gregor5953d8b2009-03-19 17:26:29 +00001315
John McCallce3ff2b2009-08-25 22:02:44 +00001316/// \brief Instantiates the definitions of all of the member
1317/// of the given class, which is an instantiation of a class template
1318/// or a member class of a template.
Douglas Gregora58861f2009-05-13 20:28:22 +00001319void
1320Sema::InstantiateClassMembers(SourceLocation PointOfInstantiation,
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001321 CXXRecordDecl *Instantiation,
1322 const MultiLevelTemplateArgumentList &TemplateArgs,
1323 TemplateSpecializationKind TSK) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001324 for (DeclContext::decl_iterator D = Instantiation->decls_begin(),
1325 DEnd = Instantiation->decls_end();
Douglas Gregora58861f2009-05-13 20:28:22 +00001326 D != DEnd; ++D) {
Douglas Gregor0d035142009-10-27 18:42:08 +00001327 bool SuppressNew = false;
Douglas Gregora58861f2009-05-13 20:28:22 +00001328 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(*D)) {
Douglas Gregor0d035142009-10-27 18:42:08 +00001329 if (FunctionDecl *Pattern
1330 = Function->getInstantiatedFromMemberFunction()) {
1331 MemberSpecializationInfo *MSInfo
1332 = Function->getMemberSpecializationInfo();
1333 assert(MSInfo && "No member specialization information?");
1334 if (CheckSpecializationInstantiationRedecl(PointOfInstantiation, TSK,
1335 Function,
1336 MSInfo->getTemplateSpecializationKind(),
1337 MSInfo->getPointOfInstantiation(),
1338 SuppressNew) ||
1339 SuppressNew)
Douglas Gregorf6b11852009-10-08 15:14:33 +00001340 continue;
1341
Douglas Gregor0d035142009-10-27 18:42:08 +00001342 if (Function->getBody())
1343 continue;
1344
1345 if (TSK == TSK_ExplicitInstantiationDefinition) {
1346 // C++0x [temp.explicit]p8:
1347 // An explicit instantiation definition that names a class template
1348 // specialization explicitly instantiates the class template
1349 // specialization and is only an explicit instantiation definition
1350 // of members whose definition is visible at the point of
1351 // instantiation.
1352 if (!Pattern->getBody())
1353 continue;
1354
1355 Function->setTemplateSpecializationKind(TSK, PointOfInstantiation);
1356
1357 InstantiateFunctionDefinition(PointOfInstantiation, Function);
1358 } else {
1359 Function->setTemplateSpecializationKind(TSK, PointOfInstantiation);
1360 }
Douglas Gregorf6b11852009-10-08 15:14:33 +00001361 }
Douglas Gregora58861f2009-05-13 20:28:22 +00001362 } else if (VarDecl *Var = dyn_cast<VarDecl>(*D)) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001363 if (Var->isStaticDataMember()) {
Douglas Gregor0d035142009-10-27 18:42:08 +00001364 MemberSpecializationInfo *MSInfo = Var->getMemberSpecializationInfo();
1365 assert(MSInfo && "No member specialization information?");
1366 if (CheckSpecializationInstantiationRedecl(PointOfInstantiation, TSK,
1367 Var,
1368 MSInfo->getTemplateSpecializationKind(),
1369 MSInfo->getPointOfInstantiation(),
1370 SuppressNew) ||
1371 SuppressNew)
Douglas Gregorf6b11852009-10-08 15:14:33 +00001372 continue;
1373
Douglas Gregor0d035142009-10-27 18:42:08 +00001374 if (TSK == TSK_ExplicitInstantiationDefinition) {
1375 // C++0x [temp.explicit]p8:
1376 // An explicit instantiation definition that names a class template
1377 // specialization explicitly instantiates the class template
1378 // specialization and is only an explicit instantiation definition
1379 // of members whose definition is visible at the point of
1380 // instantiation.
1381 if (!Var->getInstantiatedFromStaticDataMember()
1382 ->getOutOfLineDefinition())
1383 continue;
1384
1385 Var->setTemplateSpecializationKind(TSK, PointOfInstantiation);
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001386 InstantiateStaticDataMemberDefinition(PointOfInstantiation, Var);
Douglas Gregor0d035142009-10-27 18:42:08 +00001387 } else {
1388 Var->setTemplateSpecializationKind(TSK, PointOfInstantiation);
1389 }
1390 }
Douglas Gregora58861f2009-05-13 20:28:22 +00001391 } else if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(*D)) {
Douglas Gregor2db32322009-10-07 23:56:10 +00001392 if (Record->isInjectedClassName())
1393 continue;
1394
Douglas Gregor0d035142009-10-27 18:42:08 +00001395 MemberSpecializationInfo *MSInfo = Record->getMemberSpecializationInfo();
1396 assert(MSInfo && "No member specialization information?");
1397 if (CheckSpecializationInstantiationRedecl(PointOfInstantiation, TSK,
1398 Record,
1399 MSInfo->getTemplateSpecializationKind(),
1400 MSInfo->getPointOfInstantiation(),
1401 SuppressNew) ||
1402 SuppressNew)
Douglas Gregorf6b11852009-10-08 15:14:33 +00001403 continue;
1404
Douglas Gregor0d035142009-10-27 18:42:08 +00001405 CXXRecordDecl *Pattern = Record->getInstantiatedFromMemberClass();
1406 assert(Pattern && "Missing instantiated-from-template information");
1407
Douglas Gregor952b0172010-02-11 01:04:33 +00001408 if (!Record->getDefinition()) {
1409 if (!Pattern->getDefinition()) {
Douglas Gregor0d035142009-10-27 18:42:08 +00001410 // C++0x [temp.explicit]p8:
1411 // An explicit instantiation definition that names a class template
1412 // specialization explicitly instantiates the class template
1413 // specialization and is only an explicit instantiation definition
1414 // of members whose definition is visible at the point of
1415 // instantiation.
1416 if (TSK == TSK_ExplicitInstantiationDeclaration) {
1417 MSInfo->setTemplateSpecializationKind(TSK);
1418 MSInfo->setPointOfInstantiation(PointOfInstantiation);
1419 }
1420
1421 continue;
1422 }
1423
1424 InstantiateClass(PointOfInstantiation, Record, Pattern,
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001425 TemplateArgs,
1426 TSK);
Douglas Gregor0d035142009-10-27 18:42:08 +00001427 }
Douglas Gregore9374d52009-10-08 01:19:17 +00001428
Douglas Gregor952b0172010-02-11 01:04:33 +00001429 Pattern = cast_or_null<CXXRecordDecl>(Record->getDefinition());
Douglas Gregor0d035142009-10-27 18:42:08 +00001430 if (Pattern)
1431 InstantiateClassMembers(PointOfInstantiation, Pattern, TemplateArgs,
1432 TSK);
Douglas Gregora58861f2009-05-13 20:28:22 +00001433 }
1434 }
1435}
1436
1437/// \brief Instantiate the definitions of all of the members of the
1438/// given class template specialization, which was named as part of an
1439/// explicit instantiation.
Mike Stump1eb44332009-09-09 15:08:12 +00001440void
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001441Sema::InstantiateClassTemplateSpecializationMembers(
Douglas Gregora58861f2009-05-13 20:28:22 +00001442 SourceLocation PointOfInstantiation,
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001443 ClassTemplateSpecializationDecl *ClassTemplateSpec,
1444 TemplateSpecializationKind TSK) {
Douglas Gregora58861f2009-05-13 20:28:22 +00001445 // C++0x [temp.explicit]p7:
1446 // An explicit instantiation that names a class template
1447 // specialization is an explicit instantion of the same kind
1448 // (declaration or definition) of each of its members (not
1449 // including members inherited from base classes) that has not
1450 // been previously explicitly specialized in the translation unit
1451 // containing the explicit instantiation, except as described
1452 // below.
1453 InstantiateClassMembers(PointOfInstantiation, ClassTemplateSpec,
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001454 getTemplateInstantiationArgs(ClassTemplateSpec),
1455 TSK);
Douglas Gregora58861f2009-05-13 20:28:22 +00001456}
1457
Mike Stump1eb44332009-09-09 15:08:12 +00001458Sema::OwningStmtResult
Douglas Gregord6350ae2009-08-28 20:31:08 +00001459Sema::SubstStmt(Stmt *S, const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor43959a92009-08-20 07:17:43 +00001460 if (!S)
1461 return Owned(S);
1462
1463 TemplateInstantiator Instantiator(*this, TemplateArgs,
1464 SourceLocation(),
1465 DeclarationName());
1466 return Instantiator.TransformStmt(S);
1467}
1468
Mike Stump1eb44332009-09-09 15:08:12 +00001469Sema::OwningExprResult
Douglas Gregord6350ae2009-08-28 20:31:08 +00001470Sema::SubstExpr(Expr *E, const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregorb98b1992009-08-11 05:31:07 +00001471 if (!E)
1472 return Owned(E);
Mike Stump1eb44332009-09-09 15:08:12 +00001473
Douglas Gregorb98b1992009-08-11 05:31:07 +00001474 TemplateInstantiator Instantiator(*this, TemplateArgs,
1475 SourceLocation(),
1476 DeclarationName());
1477 return Instantiator.TransformExpr(E);
1478}
1479
John McCallce3ff2b2009-08-25 22:02:44 +00001480/// \brief Do template substitution on a nested-name-specifier.
Douglas Gregorab452ba2009-03-26 23:50:42 +00001481NestedNameSpecifier *
John McCallce3ff2b2009-08-25 22:02:44 +00001482Sema::SubstNestedNameSpecifier(NestedNameSpecifier *NNS,
Douglas Gregord6350ae2009-08-28 20:31:08 +00001483 SourceRange Range,
1484 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregordcee1a12009-08-06 05:28:30 +00001485 TemplateInstantiator Instantiator(*this, TemplateArgs, Range.getBegin(),
1486 DeclarationName());
1487 return Instantiator.TransformNestedNameSpecifier(NNS, Range);
Douglas Gregor5953d8b2009-03-19 17:26:29 +00001488}
Douglas Gregorde650ae2009-03-31 18:38:02 +00001489
1490TemplateName
John McCallce3ff2b2009-08-25 22:02:44 +00001491Sema::SubstTemplateName(TemplateName Name, SourceLocation Loc,
Douglas Gregord6350ae2009-08-28 20:31:08 +00001492 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregord1067e52009-08-06 06:41:21 +00001493 TemplateInstantiator Instantiator(*this, TemplateArgs, Loc,
1494 DeclarationName());
1495 return Instantiator.TransformTemplateName(Name);
Douglas Gregorde650ae2009-03-31 18:38:02 +00001496}
Douglas Gregor91333002009-06-11 00:06:24 +00001497
John McCall833ca992009-10-29 08:12:44 +00001498bool Sema::Subst(const TemplateArgumentLoc &Input, TemplateArgumentLoc &Output,
1499 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor670444e2009-08-04 22:27:00 +00001500 TemplateInstantiator Instantiator(*this, TemplateArgs, SourceLocation(),
1501 DeclarationName());
John McCall833ca992009-10-29 08:12:44 +00001502
1503 return Instantiator.TransformTemplateArgument(Input, Output);
Douglas Gregor91333002009-06-11 00:06:24 +00001504}