blob: dd92218c6b7047c66a28541b7d544b02c1286e7c [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"
Douglas Gregorcd281c32009-02-28 00:25:32 +000022#include "llvm/Support/Compiler.h"
Douglas Gregor99ebf652009-02-27 19:31:52 +000023
24using namespace clang;
25
Douglas Gregoree1828a2009-03-10 18:03:33 +000026//===----------------------------------------------------------------------===/
27// Template Instantiation Support
28//===----------------------------------------------------------------------===/
29
Douglas Gregord6350ae2009-08-28 20:31:08 +000030/// \brief Retrieve the template argument list(s) that should be used to
31/// instantiate the definition of the given declaration.
Douglas Gregor0f8716b2009-11-09 19:17:50 +000032///
33/// \param D the declaration for which we are computing template instantiation
34/// arguments.
35///
36/// \param Innermost if non-NULL, the innermost template argument list.
Douglas Gregord1102432009-08-28 17:37:35 +000037MultiLevelTemplateArgumentList
Douglas Gregor0f8716b2009-11-09 19:17:50 +000038Sema::getTemplateInstantiationArgs(NamedDecl *D,
39 const TemplateArgumentList *Innermost) {
Douglas Gregord1102432009-08-28 17:37:35 +000040 // Accumulate the set of template argument lists in this structure.
41 MultiLevelTemplateArgumentList Result;
Mike Stump1eb44332009-09-09 15:08:12 +000042
Douglas Gregor0f8716b2009-11-09 19:17:50 +000043 if (Innermost)
44 Result.addOuterTemplateArguments(Innermost);
45
Douglas Gregord1102432009-08-28 17:37:35 +000046 DeclContext *Ctx = dyn_cast<DeclContext>(D);
47 if (!Ctx)
48 Ctx = D->getDeclContext();
Mike Stump1eb44332009-09-09 15:08:12 +000049
John McCallf181d8a2009-08-29 03:16:09 +000050 while (!Ctx->isFileContext()) {
Douglas Gregord1102432009-08-28 17:37:35 +000051 // Add template arguments from a class template instantiation.
Mike Stump1eb44332009-09-09 15:08:12 +000052 if (ClassTemplateSpecializationDecl *Spec
Douglas Gregord1102432009-08-28 17:37:35 +000053 = dyn_cast<ClassTemplateSpecializationDecl>(Ctx)) {
54 // We're done when we hit an explicit specialization.
55 if (Spec->getSpecializationKind() == TSK_ExplicitSpecialization)
56 break;
Mike Stump1eb44332009-09-09 15:08:12 +000057
Douglas Gregord1102432009-08-28 17:37:35 +000058 Result.addOuterTemplateArguments(&Spec->getTemplateInstantiationArgs());
Douglas Gregorfd056bc2009-10-13 16:30:37 +000059
60 // If this class template specialization was instantiated from a
61 // specialized member that is a class template, we're done.
62 assert(Spec->getSpecializedTemplate() && "No class template?");
63 if (Spec->getSpecializedTemplate()->isMemberSpecialization())
64 break;
Mike Stump1eb44332009-09-09 15:08:12 +000065 }
Douglas Gregord1102432009-08-28 17:37:35 +000066 // Add template arguments from a function template specialization.
John McCallf181d8a2009-08-29 03:16:09 +000067 else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Ctx)) {
Douglas Gregorfd056bc2009-10-13 16:30:37 +000068 if (Function->getTemplateSpecializationKind()
69 == TSK_ExplicitSpecialization)
70 break;
71
Douglas Gregord1102432009-08-28 17:37:35 +000072 if (const TemplateArgumentList *TemplateArgs
Douglas Gregorfd056bc2009-10-13 16:30:37 +000073 = Function->getTemplateSpecializationArgs()) {
74 // Add the template arguments for this specialization.
Douglas Gregord1102432009-08-28 17:37:35 +000075 Result.addOuterTemplateArguments(TemplateArgs);
John McCallf181d8a2009-08-29 03:16:09 +000076
Douglas Gregorfd056bc2009-10-13 16:30:37 +000077 // If this function was instantiated from a specialized member that is
78 // a function template, we're done.
79 assert(Function->getPrimaryTemplate() && "No function template?");
80 if (Function->getPrimaryTemplate()->isMemberSpecialization())
81 break;
82 }
83
John McCallf181d8a2009-08-29 03:16:09 +000084 // If this is a friend declaration and it declares an entity at
85 // namespace scope, take arguments from its lexical parent
86 // instead of its semantic parent.
87 if (Function->getFriendObjectKind() &&
88 Function->getDeclContext()->isFileContext()) {
89 Ctx = Function->getLexicalDeclContext();
90 continue;
91 }
Douglas Gregord1102432009-08-28 17:37:35 +000092 }
John McCallf181d8a2009-08-29 03:16:09 +000093
94 Ctx = Ctx->getParent();
Douglas Gregor54dabfc2009-05-14 23:26:13 +000095 }
Mike Stump1eb44332009-09-09 15:08:12 +000096
Douglas Gregord1102432009-08-28 17:37:35 +000097 return Result;
Douglas Gregor54dabfc2009-05-14 23:26:13 +000098}
99
Douglas Gregorf35f8282009-11-11 21:54:23 +0000100bool Sema::ActiveTemplateInstantiation::isInstantiationRecord() const {
101 switch (Kind) {
102 case TemplateInstantiation:
103 case DefaultTemplateArgumentInstantiation:
104 case DefaultFunctionArgumentInstantiation:
105 return true;
106
107 case ExplicitTemplateArgumentSubstitution:
108 case DeducedTemplateArgumentSubstitution:
109 case PriorTemplateArgumentSubstitution:
110 case DefaultTemplateArgumentChecking:
111 return false;
112 }
113
114 return true;
115}
116
Douglas Gregor26dce442009-03-10 00:06:19 +0000117Sema::InstantiatingTemplate::
118InstantiatingTemplate(Sema &SemaRef, SourceLocation PointOfInstantiation,
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000119 Decl *Entity,
Douglas Gregor26dce442009-03-10 00:06:19 +0000120 SourceRange InstantiationRange)
121 : SemaRef(SemaRef) {
Douglas Gregordf667e72009-03-10 20:44:00 +0000122
123 Invalid = CheckInstantiationDepth(PointOfInstantiation,
124 InstantiationRange);
125 if (!Invalid) {
Douglas Gregor26dce442009-03-10 00:06:19 +0000126 ActiveTemplateInstantiation Inst;
Douglas Gregordf667e72009-03-10 20:44:00 +0000127 Inst.Kind = ActiveTemplateInstantiation::TemplateInstantiation;
Douglas Gregor26dce442009-03-10 00:06:19 +0000128 Inst.PointOfInstantiation = PointOfInstantiation;
Douglas Gregordf667e72009-03-10 20:44:00 +0000129 Inst.Entity = reinterpret_cast<uintptr_t>(Entity);
Douglas Gregor313a81d2009-03-12 18:36:18 +0000130 Inst.TemplateArgs = 0;
131 Inst.NumTemplateArgs = 0;
Douglas Gregordf667e72009-03-10 20:44:00 +0000132 Inst.InstantiationRange = InstantiationRange;
133 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
Douglas Gregordf667e72009-03-10 20:44:00 +0000134 }
135}
136
Mike Stump1eb44332009-09-09 15:08:12 +0000137Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef,
Douglas Gregordf667e72009-03-10 20:44:00 +0000138 SourceLocation PointOfInstantiation,
139 TemplateDecl *Template,
140 const TemplateArgument *TemplateArgs,
141 unsigned NumTemplateArgs,
142 SourceRange InstantiationRange)
143 : SemaRef(SemaRef) {
144
145 Invalid = CheckInstantiationDepth(PointOfInstantiation,
146 InstantiationRange);
147 if (!Invalid) {
148 ActiveTemplateInstantiation Inst;
Mike Stump1eb44332009-09-09 15:08:12 +0000149 Inst.Kind
Douglas Gregordf667e72009-03-10 20:44:00 +0000150 = ActiveTemplateInstantiation::DefaultTemplateArgumentInstantiation;
151 Inst.PointOfInstantiation = PointOfInstantiation;
152 Inst.Entity = reinterpret_cast<uintptr_t>(Template);
153 Inst.TemplateArgs = TemplateArgs;
154 Inst.NumTemplateArgs = NumTemplateArgs;
Douglas Gregor26dce442009-03-10 00:06:19 +0000155 Inst.InstantiationRange = InstantiationRange;
156 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
Douglas Gregor26dce442009-03-10 00:06:19 +0000157 }
158}
159
Mike Stump1eb44332009-09-09 15:08:12 +0000160Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef,
Douglas Gregor637a4092009-06-10 23:47:09 +0000161 SourceLocation PointOfInstantiation,
Douglas Gregorcca9e962009-07-01 22:01:06 +0000162 FunctionTemplateDecl *FunctionTemplate,
163 const TemplateArgument *TemplateArgs,
164 unsigned NumTemplateArgs,
165 ActiveTemplateInstantiation::InstantiationKind Kind,
166 SourceRange InstantiationRange)
167: SemaRef(SemaRef) {
Mike Stump1eb44332009-09-09 15:08:12 +0000168
Douglas Gregorcca9e962009-07-01 22:01:06 +0000169 Invalid = CheckInstantiationDepth(PointOfInstantiation,
170 InstantiationRange);
171 if (!Invalid) {
172 ActiveTemplateInstantiation Inst;
173 Inst.Kind = Kind;
174 Inst.PointOfInstantiation = PointOfInstantiation;
175 Inst.Entity = reinterpret_cast<uintptr_t>(FunctionTemplate);
176 Inst.TemplateArgs = TemplateArgs;
177 Inst.NumTemplateArgs = NumTemplateArgs;
178 Inst.InstantiationRange = InstantiationRange;
179 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
Douglas Gregorf35f8282009-11-11 21:54:23 +0000180
181 if (!Inst.isInstantiationRecord())
182 ++SemaRef.NonInstantiationEntries;
Douglas Gregorcca9e962009-07-01 22:01:06 +0000183 }
184}
185
Mike Stump1eb44332009-09-09 15:08:12 +0000186Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef,
Douglas Gregorcca9e962009-07-01 22:01:06 +0000187 SourceLocation PointOfInstantiation,
Douglas Gregor637a4092009-06-10 23:47:09 +0000188 ClassTemplatePartialSpecializationDecl *PartialSpec,
189 const TemplateArgument *TemplateArgs,
190 unsigned NumTemplateArgs,
191 SourceRange InstantiationRange)
192 : SemaRef(SemaRef) {
193
Douglas Gregorf35f8282009-11-11 21:54:23 +0000194 Invalid = false;
195
196 ActiveTemplateInstantiation Inst;
197 Inst.Kind = ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution;
198 Inst.PointOfInstantiation = PointOfInstantiation;
199 Inst.Entity = reinterpret_cast<uintptr_t>(PartialSpec);
200 Inst.TemplateArgs = TemplateArgs;
201 Inst.NumTemplateArgs = NumTemplateArgs;
202 Inst.InstantiationRange = InstantiationRange;
203 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
204
205 assert(!Inst.isInstantiationRecord());
206 ++SemaRef.NonInstantiationEntries;
Douglas Gregor637a4092009-06-10 23:47:09 +0000207}
208
Mike Stump1eb44332009-09-09 15:08:12 +0000209Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef,
Douglas Gregor9148c3f2009-11-11 19:13:48 +0000210 SourceLocation PointOfInstantiation,
Anders Carlsson25cae7f2009-09-05 05:14:19 +0000211 ParmVarDecl *Param,
212 const TemplateArgument *TemplateArgs,
213 unsigned NumTemplateArgs,
214 SourceRange InstantiationRange)
215 : SemaRef(SemaRef) {
Mike Stump1eb44332009-09-09 15:08:12 +0000216
Douglas Gregor9148c3f2009-11-11 19:13:48 +0000217 Invalid = CheckInstantiationDepth(PointOfInstantiation, InstantiationRange);
Anders Carlsson25cae7f2009-09-05 05:14:19 +0000218
219 if (!Invalid) {
220 ActiveTemplateInstantiation Inst;
221 Inst.Kind
222 = ActiveTemplateInstantiation::DefaultFunctionArgumentInstantiation;
Douglas Gregor9148c3f2009-11-11 19:13:48 +0000223 Inst.PointOfInstantiation = PointOfInstantiation;
Anders Carlsson25cae7f2009-09-05 05:14:19 +0000224 Inst.Entity = reinterpret_cast<uintptr_t>(Param);
225 Inst.TemplateArgs = TemplateArgs;
226 Inst.NumTemplateArgs = NumTemplateArgs;
227 Inst.InstantiationRange = InstantiationRange;
228 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
Douglas Gregor9148c3f2009-11-11 19:13:48 +0000229 }
230}
231
232Sema::InstantiatingTemplate::
233InstantiatingTemplate(Sema &SemaRef, SourceLocation PointOfInstantiation,
234 TemplateDecl *Template,
235 NonTypeTemplateParmDecl *Param,
236 const TemplateArgument *TemplateArgs,
237 unsigned NumTemplateArgs,
238 SourceRange InstantiationRange) : SemaRef(SemaRef) {
Douglas Gregorf35f8282009-11-11 21:54:23 +0000239 Invalid = false;
Douglas Gregor9148c3f2009-11-11 19:13:48 +0000240
Douglas Gregorf35f8282009-11-11 21:54:23 +0000241 ActiveTemplateInstantiation Inst;
242 Inst.Kind = ActiveTemplateInstantiation::PriorTemplateArgumentSubstitution;
243 Inst.PointOfInstantiation = PointOfInstantiation;
244 Inst.Template = Template;
245 Inst.Entity = reinterpret_cast<uintptr_t>(Param);
246 Inst.TemplateArgs = TemplateArgs;
247 Inst.NumTemplateArgs = NumTemplateArgs;
248 Inst.InstantiationRange = InstantiationRange;
249 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
250
251 assert(!Inst.isInstantiationRecord());
252 ++SemaRef.NonInstantiationEntries;
Douglas Gregor9148c3f2009-11-11 19:13:48 +0000253}
254
255Sema::InstantiatingTemplate::
256InstantiatingTemplate(Sema &SemaRef, SourceLocation PointOfInstantiation,
257 TemplateDecl *Template,
258 TemplateTemplateParmDecl *Param,
259 const TemplateArgument *TemplateArgs,
260 unsigned NumTemplateArgs,
261 SourceRange InstantiationRange) : SemaRef(SemaRef) {
Douglas Gregorf35f8282009-11-11 21:54:23 +0000262 Invalid = false;
263 ActiveTemplateInstantiation Inst;
264 Inst.Kind = ActiveTemplateInstantiation::PriorTemplateArgumentSubstitution;
265 Inst.PointOfInstantiation = PointOfInstantiation;
266 Inst.Template = Template;
267 Inst.Entity = reinterpret_cast<uintptr_t>(Param);
268 Inst.TemplateArgs = TemplateArgs;
269 Inst.NumTemplateArgs = NumTemplateArgs;
270 Inst.InstantiationRange = InstantiationRange;
271 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
Douglas Gregor9148c3f2009-11-11 19:13:48 +0000272
Douglas Gregorf35f8282009-11-11 21:54:23 +0000273 assert(!Inst.isInstantiationRecord());
274 ++SemaRef.NonInstantiationEntries;
275}
276
277Sema::InstantiatingTemplate::
278InstantiatingTemplate(Sema &SemaRef, SourceLocation PointOfInstantiation,
279 TemplateDecl *Template,
280 NamedDecl *Param,
281 const TemplateArgument *TemplateArgs,
282 unsigned NumTemplateArgs,
283 SourceRange InstantiationRange) : SemaRef(SemaRef) {
284 Invalid = false;
285
286 ActiveTemplateInstantiation Inst;
287 Inst.Kind = ActiveTemplateInstantiation::DefaultTemplateArgumentChecking;
288 Inst.PointOfInstantiation = PointOfInstantiation;
289 Inst.Template = Template;
290 Inst.Entity = reinterpret_cast<uintptr_t>(Param);
291 Inst.TemplateArgs = TemplateArgs;
292 Inst.NumTemplateArgs = NumTemplateArgs;
293 Inst.InstantiationRange = InstantiationRange;
294 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
295
296 assert(!Inst.isInstantiationRecord());
297 ++SemaRef.NonInstantiationEntries;
Anders Carlsson25cae7f2009-09-05 05:14:19 +0000298}
299
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000300void Sema::InstantiatingTemplate::Clear() {
301 if (!Invalid) {
Douglas Gregorf35f8282009-11-11 21:54:23 +0000302 if (!SemaRef.ActiveTemplateInstantiations.back().isInstantiationRecord()) {
303 assert(SemaRef.NonInstantiationEntries > 0);
304 --SemaRef.NonInstantiationEntries;
305 }
306
Douglas Gregor26dce442009-03-10 00:06:19 +0000307 SemaRef.ActiveTemplateInstantiations.pop_back();
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000308 Invalid = true;
309 }
Douglas Gregor26dce442009-03-10 00:06:19 +0000310}
311
Douglas Gregordf667e72009-03-10 20:44:00 +0000312bool Sema::InstantiatingTemplate::CheckInstantiationDepth(
313 SourceLocation PointOfInstantiation,
314 SourceRange InstantiationRange) {
Douglas Gregorf35f8282009-11-11 21:54:23 +0000315 assert(SemaRef.NonInstantiationEntries <=
316 SemaRef.ActiveTemplateInstantiations.size());
317 if ((SemaRef.ActiveTemplateInstantiations.size() -
318 SemaRef.NonInstantiationEntries)
319 <= SemaRef.getLangOptions().InstantiationDepth)
Douglas Gregordf667e72009-03-10 20:44:00 +0000320 return false;
321
Mike Stump1eb44332009-09-09 15:08:12 +0000322 SemaRef.Diag(PointOfInstantiation,
Douglas Gregordf667e72009-03-10 20:44:00 +0000323 diag::err_template_recursion_depth_exceeded)
324 << SemaRef.getLangOptions().InstantiationDepth
325 << InstantiationRange;
326 SemaRef.Diag(PointOfInstantiation, diag::note_template_recursion_depth)
327 << SemaRef.getLangOptions().InstantiationDepth;
328 return true;
329}
330
Douglas Gregoree1828a2009-03-10 18:03:33 +0000331/// \brief Prints the current instantiation stack through a series of
332/// notes.
333void Sema::PrintInstantiationStack() {
Douglas Gregorcca9e962009-07-01 22:01:06 +0000334 // FIXME: In all of these cases, we need to show the template arguments
Douglas Gregoree1828a2009-03-10 18:03:33 +0000335 for (llvm::SmallVector<ActiveTemplateInstantiation, 16>::reverse_iterator
336 Active = ActiveTemplateInstantiations.rbegin(),
337 ActiveEnd = ActiveTemplateInstantiations.rend();
338 Active != ActiveEnd;
339 ++Active) {
Douglas Gregordf667e72009-03-10 20:44:00 +0000340 switch (Active->Kind) {
341 case ActiveTemplateInstantiation::TemplateInstantiation: {
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000342 Decl *D = reinterpret_cast<Decl *>(Active->Entity);
343 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) {
344 unsigned DiagID = diag::note_template_member_class_here;
345 if (isa<ClassTemplateSpecializationDecl>(Record))
346 DiagID = diag::note_template_class_instantiation_here;
Mike Stump1eb44332009-09-09 15:08:12 +0000347 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000348 DiagID)
349 << Context.getTypeDeclType(Record)
350 << Active->InstantiationRange;
Douglas Gregor7caa6822009-07-24 20:34:43 +0000351 } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
Douglas Gregor1637be72009-06-26 00:10:03 +0000352 unsigned DiagID;
353 if (Function->getPrimaryTemplate())
354 DiagID = diag::note_function_template_spec_here;
355 else
356 DiagID = diag::note_template_member_function_here;
Mike Stump1eb44332009-09-09 15:08:12 +0000357 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000358 DiagID)
359 << Function
360 << Active->InstantiationRange;
Douglas Gregor7caa6822009-07-24 20:34:43 +0000361 } else {
362 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
363 diag::note_template_static_data_member_def_here)
364 << cast<VarDecl>(D)
365 << Active->InstantiationRange;
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000366 }
Douglas Gregordf667e72009-03-10 20:44:00 +0000367 break;
368 }
369
370 case ActiveTemplateInstantiation::DefaultTemplateArgumentInstantiation: {
371 TemplateDecl *Template = cast<TemplateDecl>((Decl *)Active->Entity);
372 std::string TemplateArgsStr
Douglas Gregor7532dc62009-03-30 22:58:21 +0000373 = TemplateSpecializationType::PrintTemplateArgumentList(
Mike Stump1eb44332009-09-09 15:08:12 +0000374 Active->TemplateArgs,
Douglas Gregord249e1d1f2009-05-29 20:38:28 +0000375 Active->NumTemplateArgs,
376 Context.PrintingPolicy);
Douglas Gregordf667e72009-03-10 20:44:00 +0000377 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
378 diag::note_default_arg_instantiation_here)
379 << (Template->getNameAsString() + TemplateArgsStr)
380 << Active->InstantiationRange;
381 break;
382 }
Douglas Gregor637a4092009-06-10 23:47:09 +0000383
Douglas Gregorcca9e962009-07-01 22:01:06 +0000384 case ActiveTemplateInstantiation::ExplicitTemplateArgumentSubstitution: {
Mike Stump1eb44332009-09-09 15:08:12 +0000385 FunctionTemplateDecl *FnTmpl
Douglas Gregorcca9e962009-07-01 22:01:06 +0000386 = cast<FunctionTemplateDecl>((Decl *)Active->Entity);
Douglas Gregor637a4092009-06-10 23:47:09 +0000387 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
Douglas Gregorcca9e962009-07-01 22:01:06 +0000388 diag::note_explicit_template_arg_substitution_here)
389 << FnTmpl << Active->InstantiationRange;
Douglas Gregor637a4092009-06-10 23:47:09 +0000390 break;
391 }
Mike Stump1eb44332009-09-09 15:08:12 +0000392
Douglas Gregorcca9e962009-07-01 22:01:06 +0000393 case ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution:
394 if (ClassTemplatePartialSpecializationDecl *PartialSpec
395 = dyn_cast<ClassTemplatePartialSpecializationDecl>(
396 (Decl *)Active->Entity)) {
397 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
398 diag::note_partial_spec_deduct_instantiation_here)
399 << Context.getTypeDeclType(PartialSpec)
400 << Active->InstantiationRange;
401 } else {
402 FunctionTemplateDecl *FnTmpl
403 = cast<FunctionTemplateDecl>((Decl *)Active->Entity);
404 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
405 diag::note_function_template_deduction_instantiation_here)
406 << FnTmpl << Active->InstantiationRange;
407 }
408 break;
Douglas Gregor637a4092009-06-10 23:47:09 +0000409
Anders Carlsson25cae7f2009-09-05 05:14:19 +0000410 case ActiveTemplateInstantiation::DefaultFunctionArgumentInstantiation: {
411 ParmVarDecl *Param = cast<ParmVarDecl>((Decl *)Active->Entity);
412 FunctionDecl *FD = cast<FunctionDecl>(Param->getDeclContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000413
Anders Carlsson25cae7f2009-09-05 05:14:19 +0000414 std::string TemplateArgsStr
415 = TemplateSpecializationType::PrintTemplateArgumentList(
Mike Stump1eb44332009-09-09 15:08:12 +0000416 Active->TemplateArgs,
Anders Carlsson25cae7f2009-09-05 05:14:19 +0000417 Active->NumTemplateArgs,
418 Context.PrintingPolicy);
419 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
420 diag::note_default_function_arg_instantiation_here)
Anders Carlsson6bc107b2009-09-05 05:38:54 +0000421 << (FD->getNameAsString() + TemplateArgsStr)
Anders Carlsson25cae7f2009-09-05 05:14:19 +0000422 << Active->InstantiationRange;
423 break;
424 }
Mike Stump1eb44332009-09-09 15:08:12 +0000425
Douglas Gregor9148c3f2009-11-11 19:13:48 +0000426 case ActiveTemplateInstantiation::PriorTemplateArgumentSubstitution: {
427 NamedDecl *Parm = cast<NamedDecl>((Decl *)Active->Entity);
428 std::string Name;
429 if (!Parm->getName().empty())
430 Name = std::string(" '") + Parm->getName().str() + "'";
431
432 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
433 diag::note_prior_template_arg_substitution)
434 << isa<TemplateTemplateParmDecl>(Parm)
435 << Name
436 << getTemplateArgumentBindingsText(
437 Active->Template->getTemplateParameters(),
438 Active->TemplateArgs,
439 Active->NumTemplateArgs)
440 << Active->InstantiationRange;
441 break;
442 }
Douglas Gregorf35f8282009-11-11 21:54:23 +0000443
444 case ActiveTemplateInstantiation::DefaultTemplateArgumentChecking: {
445 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
446 diag::note_template_default_arg_checking)
447 << getTemplateArgumentBindingsText(
448 Active->Template->getTemplateParameters(),
449 Active->TemplateArgs,
450 Active->NumTemplateArgs)
451 << Active->InstantiationRange;
452 break;
453 }
Douglas Gregordf667e72009-03-10 20:44:00 +0000454 }
Douglas Gregoree1828a2009-03-10 18:03:33 +0000455 }
456}
457
Douglas Gregor5e9f35c2009-06-14 07:33:30 +0000458bool Sema::isSFINAEContext() const {
459 using llvm::SmallVector;
460 for (SmallVector<ActiveTemplateInstantiation, 16>::const_reverse_iterator
461 Active = ActiveTemplateInstantiations.rbegin(),
462 ActiveEnd = ActiveTemplateInstantiations.rend();
463 Active != ActiveEnd;
Douglas Gregorf35f8282009-11-11 21:54:23 +0000464 ++Active)
465 {
Douglas Gregor5e9f35c2009-06-14 07:33:30 +0000466 switch(Active->Kind) {
Douglas Gregorcca9e962009-07-01 22:01:06 +0000467 case ActiveTemplateInstantiation::TemplateInstantiation:
Anders Carlsson25cae7f2009-09-05 05:14:19 +0000468 case ActiveTemplateInstantiation::DefaultFunctionArgumentInstantiation:
Douglas Gregorcca9e962009-07-01 22:01:06 +0000469 // This is a template instantiation, so there is no SFINAE.
470 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000471
Douglas Gregor5e9f35c2009-06-14 07:33:30 +0000472 case ActiveTemplateInstantiation::DefaultTemplateArgumentInstantiation:
Douglas Gregor9148c3f2009-11-11 19:13:48 +0000473 case ActiveTemplateInstantiation::PriorTemplateArgumentSubstitution:
Douglas Gregorf35f8282009-11-11 21:54:23 +0000474 case ActiveTemplateInstantiation::DefaultTemplateArgumentChecking:
Douglas Gregor9148c3f2009-11-11 19:13:48 +0000475 // A default template argument instantiation and substitution into
476 // template parameters with arguments for prior parameters may or may
477 // not be a SFINAE context; look further up the stack.
Douglas Gregor5e9f35c2009-06-14 07:33:30 +0000478 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000479
Douglas Gregorcca9e962009-07-01 22:01:06 +0000480 case ActiveTemplateInstantiation::ExplicitTemplateArgumentSubstitution:
481 case ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution:
482 // We're either substitution explicitly-specified template arguments
483 // or deduced template arguments, so SFINAE applies.
484 return true;
Douglas Gregor5e9f35c2009-06-14 07:33:30 +0000485 }
486 }
487
488 return false;
489}
490
Douglas Gregor99ebf652009-02-27 19:31:52 +0000491//===----------------------------------------------------------------------===/
492// Template Instantiation for Types
493//===----------------------------------------------------------------------===/
Douglas Gregorcd281c32009-02-28 00:25:32 +0000494namespace {
Mike Stump1eb44332009-09-09 15:08:12 +0000495 class VISIBILITY_HIDDEN TemplateInstantiator
496 : public TreeTransform<TemplateInstantiator> {
Douglas Gregord6350ae2009-08-28 20:31:08 +0000497 const MultiLevelTemplateArgumentList &TemplateArgs;
Douglas Gregorcd281c32009-02-28 00:25:32 +0000498 SourceLocation Loc;
499 DeclarationName Entity;
Douglas Gregor99ebf652009-02-27 19:31:52 +0000500
Douglas Gregorcd281c32009-02-28 00:25:32 +0000501 public:
Douglas Gregor43959a92009-08-20 07:17:43 +0000502 typedef TreeTransform<TemplateInstantiator> inherited;
Mike Stump1eb44332009-09-09 15:08:12 +0000503
504 TemplateInstantiator(Sema &SemaRef,
Douglas Gregord6350ae2009-08-28 20:31:08 +0000505 const MultiLevelTemplateArgumentList &TemplateArgs,
Douglas Gregor577f75a2009-08-04 16:50:30 +0000506 SourceLocation Loc,
Mike Stump1eb44332009-09-09 15:08:12 +0000507 DeclarationName Entity)
508 : inherited(SemaRef), TemplateArgs(TemplateArgs), Loc(Loc),
Douglas Gregor43959a92009-08-20 07:17:43 +0000509 Entity(Entity) { }
Douglas Gregorcd281c32009-02-28 00:25:32 +0000510
Mike Stump1eb44332009-09-09 15:08:12 +0000511 /// \brief Determine whether the given type \p T has already been
Douglas Gregor577f75a2009-08-04 16:50:30 +0000512 /// transformed.
513 ///
514 /// For the purposes of template instantiation, a type has already been
515 /// transformed if it is NULL or if it is not dependent.
516 bool AlreadyTransformed(QualType T) {
517 return T.isNull() || !T->isDependentType();
Douglas Gregorff668032009-05-13 18:28:20 +0000518 }
Mike Stump1eb44332009-09-09 15:08:12 +0000519
Douglas Gregor577f75a2009-08-04 16:50:30 +0000520 /// \brief Returns the location of the entity being instantiated, if known.
521 SourceLocation getBaseLocation() { return Loc; }
Mike Stump1eb44332009-09-09 15:08:12 +0000522
Douglas Gregor577f75a2009-08-04 16:50:30 +0000523 /// \brief Returns the name of the entity being instantiated, if any.
524 DeclarationName getBaseEntity() { return Entity; }
Mike Stump1eb44332009-09-09 15:08:12 +0000525
Douglas Gregor972e6ce2009-10-27 06:26:26 +0000526 /// \brief Sets the "base" location and entity when that
527 /// information is known based on another transformation.
528 void setBase(SourceLocation Loc, DeclarationName Entity) {
529 this->Loc = Loc;
530 this->Entity = Entity;
531 }
532
Douglas Gregor577f75a2009-08-04 16:50:30 +0000533 /// \brief Transform the given declaration by instantiating a reference to
534 /// this declaration.
535 Decl *TransformDecl(Decl *D);
Douglas Gregorb98b1992009-08-11 05:31:07 +0000536
Mike Stump1eb44332009-09-09 15:08:12 +0000537 /// \brief Transform the definition of the given declaration by
Douglas Gregor43959a92009-08-20 07:17:43 +0000538 /// instantiating it.
539 Decl *TransformDefinition(Decl *D);
Mike Stump1eb44332009-09-09 15:08:12 +0000540
Douglas Gregor6cd21982009-10-20 05:58:46 +0000541 /// \bried Transform the first qualifier within a scope by instantiating the
542 /// declaration.
543 NamedDecl *TransformFirstQualifierInScope(NamedDecl *D, SourceLocation Loc);
544
Douglas Gregor43959a92009-08-20 07:17:43 +0000545 /// \brief Rebuild the exception declaration and register the declaration
546 /// as an instantiated local.
Mike Stump1eb44332009-09-09 15:08:12 +0000547 VarDecl *RebuildExceptionDecl(VarDecl *ExceptionDecl, QualType T,
Douglas Gregor43959a92009-08-20 07:17:43 +0000548 DeclaratorInfo *Declarator,
549 IdentifierInfo *Name,
550 SourceLocation Loc, SourceRange TypeRange);
Mike Stump1eb44332009-09-09 15:08:12 +0000551
John McCallc4e70192009-09-11 04:59:25 +0000552 /// \brief Check for tag mismatches when instantiating an
553 /// elaborated type.
554 QualType RebuildElaboratedType(QualType T, ElaboratedType::TagKind Tag);
555
Douglas Gregorc86a6e92009-11-04 07:01:15 +0000556 Sema::OwningExprResult TransformPredefinedExpr(PredefinedExpr *E,
557 bool isAddressOfOperand);
558 Sema::OwningExprResult TransformDeclRefExpr(DeclRefExpr *E,
559 bool isAddressOfOperand);
Mike Stump1eb44332009-09-09 15:08:12 +0000560
Sebastian Redla29e51b2009-11-08 13:56:19 +0000561 Sema::OwningExprResult TransformCXXDefaultArgExpr(CXXDefaultArgExpr *E,
562 bool isAddressOfOperand);
563
Mike Stump1eb44332009-09-09 15:08:12 +0000564 /// \brief Transforms a template type parameter type by performing
Douglas Gregor577f75a2009-08-04 16:50:30 +0000565 /// substitution of the corresponding template type argument.
John McCalla2becad2009-10-21 00:40:46 +0000566 QualType TransformTemplateTypeParmType(TypeLocBuilder &TLB,
567 TemplateTypeParmTypeLoc TL);
Douglas Gregor577f75a2009-08-04 16:50:30 +0000568 };
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000569}
570
Douglas Gregor577f75a2009-08-04 16:50:30 +0000571Decl *TemplateInstantiator::TransformDecl(Decl *D) {
Douglas Gregorc68afe22009-09-03 21:38:09 +0000572 if (!D)
573 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000574
Douglas Gregorc68afe22009-09-03 21:38:09 +0000575 if (TemplateTemplateParmDecl *TTP = dyn_cast<TemplateTemplateParmDecl>(D)) {
Douglas Gregord6350ae2009-08-28 20:31:08 +0000576 if (TTP->getDepth() < TemplateArgs.getNumLevels()) {
Douglas Gregor788cd062009-11-11 01:00:40 +0000577 TemplateName Template
578 = TemplateArgs(TTP->getDepth(), TTP->getPosition()).getAsTemplate();
579 assert(!Template.isNull() && Template.getAsTemplateDecl() &&
Douglas Gregord6350ae2009-08-28 20:31:08 +0000580 "Wrong kind of template template argument");
Douglas Gregor788cd062009-11-11 01:00:40 +0000581 return Template.getAsTemplateDecl();
Douglas Gregord6350ae2009-08-28 20:31:08 +0000582 }
Mike Stump1eb44332009-09-09 15:08:12 +0000583
584 // If the corresponding template argument is NULL or non-existent, it's
585 // because we are performing instantiation from explicitly-specified
Douglas Gregord6350ae2009-08-28 20:31:08 +0000586 // template arguments in a function template, but there were some
587 // arguments left unspecified.
Mike Stump1eb44332009-09-09 15:08:12 +0000588 if (!TemplateArgs.hasTemplateArgument(TTP->getDepth(),
Douglas Gregord6350ae2009-08-28 20:31:08 +0000589 TTP->getPosition()))
590 return D;
Mike Stump1eb44332009-09-09 15:08:12 +0000591
Douglas Gregor788cd062009-11-11 01:00:40 +0000592 // Fall through to find the instantiated declaration for this template
593 // template parameter.
Douglas Gregord1067e52009-08-06 06:41:21 +0000594 }
Mike Stump1eb44332009-09-09 15:08:12 +0000595
Douglas Gregore95b4092009-09-16 18:34:49 +0000596 return SemaRef.FindInstantiatedDecl(cast<NamedDecl>(D), TemplateArgs);
Douglas Gregor577f75a2009-08-04 16:50:30 +0000597}
598
Douglas Gregor43959a92009-08-20 07:17:43 +0000599Decl *TemplateInstantiator::TransformDefinition(Decl *D) {
John McCallce3ff2b2009-08-25 22:02:44 +0000600 Decl *Inst = getSema().SubstDecl(D, getSema().CurContext, TemplateArgs);
Douglas Gregor43959a92009-08-20 07:17:43 +0000601 if (!Inst)
602 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000603
Douglas Gregor43959a92009-08-20 07:17:43 +0000604 getSema().CurrentInstantiationScope->InstantiatedLocal(D, Inst);
605 return Inst;
606}
607
Douglas Gregor6cd21982009-10-20 05:58:46 +0000608NamedDecl *
609TemplateInstantiator::TransformFirstQualifierInScope(NamedDecl *D,
610 SourceLocation Loc) {
611 // If the first part of the nested-name-specifier was a template type
612 // parameter, instantiate that type parameter down to a tag type.
613 if (TemplateTypeParmDecl *TTPD = dyn_cast_or_null<TemplateTypeParmDecl>(D)) {
614 const TemplateTypeParmType *TTP
615 = cast<TemplateTypeParmType>(getSema().Context.getTypeDeclType(TTPD));
616 if (TTP->getDepth() < TemplateArgs.getNumLevels()) {
617 QualType T = TemplateArgs(TTP->getDepth(), TTP->getIndex()).getAsType();
618 if (T.isNull())
619 return cast_or_null<NamedDecl>(TransformDecl(D));
620
621 if (const TagType *Tag = T->getAs<TagType>())
622 return Tag->getDecl();
623
624 // The resulting type is not a tag; complain.
625 getSema().Diag(Loc, diag::err_nested_name_spec_non_tag) << T;
626 return 0;
627 }
628 }
629
630 return cast_or_null<NamedDecl>(TransformDecl(D));
631}
632
Douglas Gregor43959a92009-08-20 07:17:43 +0000633VarDecl *
634TemplateInstantiator::RebuildExceptionDecl(VarDecl *ExceptionDecl,
Mike Stump1eb44332009-09-09 15:08:12 +0000635 QualType T,
Douglas Gregor43959a92009-08-20 07:17:43 +0000636 DeclaratorInfo *Declarator,
637 IdentifierInfo *Name,
Mike Stump1eb44332009-09-09 15:08:12 +0000638 SourceLocation Loc,
Douglas Gregor43959a92009-08-20 07:17:43 +0000639 SourceRange TypeRange) {
640 VarDecl *Var = inherited::RebuildExceptionDecl(ExceptionDecl, T, Declarator,
641 Name, Loc, TypeRange);
642 if (Var && !Var->isInvalidDecl())
643 getSema().CurrentInstantiationScope->InstantiatedLocal(ExceptionDecl, Var);
644 return Var;
645}
646
John McCallc4e70192009-09-11 04:59:25 +0000647QualType
648TemplateInstantiator::RebuildElaboratedType(QualType T,
649 ElaboratedType::TagKind Tag) {
650 if (const TagType *TT = T->getAs<TagType>()) {
651 TagDecl* TD = TT->getDecl();
652
653 // FIXME: this location is very wrong; we really need typelocs.
654 SourceLocation TagLocation = TD->getTagKeywordLoc();
655
656 // FIXME: type might be anonymous.
657 IdentifierInfo *Id = TD->getIdentifier();
658
659 // TODO: should we even warn on struct/class mismatches for this? Seems
660 // like it's likely to produce a lot of spurious errors.
661 if (!SemaRef.isAcceptableTagRedeclaration(TD, Tag, TagLocation, *Id)) {
662 SemaRef.Diag(TagLocation, diag::err_use_with_wrong_tag)
663 << Id
664 << CodeModificationHint::CreateReplacement(SourceRange(TagLocation),
665 TD->getKindName());
666 SemaRef.Diag(TD->getLocation(), diag::note_previous_use);
667 }
668 }
669
670 return TreeTransform<TemplateInstantiator>::RebuildElaboratedType(T, Tag);
671}
672
673Sema::OwningExprResult
Douglas Gregorc86a6e92009-11-04 07:01:15 +0000674TemplateInstantiator::TransformPredefinedExpr(PredefinedExpr *E,
675 bool isAddressOfOperand) {
Anders Carlsson773f3972009-09-11 01:22:35 +0000676 if (!E->isTypeDependent())
677 return SemaRef.Owned(E->Retain());
678
679 FunctionDecl *currentDecl = getSema().getCurFunctionDecl();
680 assert(currentDecl && "Must have current function declaration when "
681 "instantiating.");
682
683 PredefinedExpr::IdentType IT = E->getIdentType();
684
685 unsigned Length =
686 PredefinedExpr::ComputeName(getSema().Context, IT, currentDecl).length();
687
688 llvm::APInt LengthI(32, Length + 1);
John McCall0953e762009-09-24 19:53:00 +0000689 QualType ResTy = getSema().Context.CharTy.withConst();
Anders Carlsson773f3972009-09-11 01:22:35 +0000690 ResTy = getSema().Context.getConstantArrayType(ResTy, LengthI,
691 ArrayType::Normal, 0);
692 PredefinedExpr *PE =
693 new (getSema().Context) PredefinedExpr(E->getLocation(), ResTy, IT);
694 return getSema().Owned(PE);
695}
696
697Sema::OwningExprResult
Douglas Gregorc86a6e92009-11-04 07:01:15 +0000698TemplateInstantiator::TransformDeclRefExpr(DeclRefExpr *E,
699 bool isAddressOfOperand) {
Douglas Gregorb98b1992009-08-11 05:31:07 +0000700 // FIXME: Clean this up a bit
701 NamedDecl *D = E->getDecl();
702 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(D)) {
Douglas Gregor550d9b22009-10-31 17:21:17 +0000703 if (NTTP->getDepth() < TemplateArgs.getNumLevels()) {
Douglas Gregor550d9b22009-10-31 17:21:17 +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
Douglas Gregor550d9b22009-10-31 17:21:17 +0000712 const TemplateArgument &Arg = TemplateArgs(NTTP->getDepth(),
713 NTTP->getPosition());
Mike Stump1eb44332009-09-09 15:08:12 +0000714
Douglas Gregor550d9b22009-10-31 17:21:17 +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
Douglas Gregor550d9b22009-10-31 17:21:17 +0000720 if (Arg.getKind() == TemplateArgument::Declaration) {
721 ValueDecl *VD = cast<ValueDecl>(Arg.getAsDecl());
Mike Stump1eb44332009-09-09 15:08:12 +0000722
Douglas Gregor550d9b22009-10-31 17:21:17 +0000723 VD = cast_or_null<ValueDecl>(
Douglas Gregorc86a6e92009-11-04 07:01:15 +0000724 getSema().FindInstantiatedDecl(VD, TemplateArgs));
Douglas Gregor550d9b22009-10-31 17:21:17 +0000725 if (!VD)
726 return SemaRef.ExprError();
Mike Stump1eb44332009-09-09 15:08:12 +0000727
Douglas Gregor231edff2009-11-12 17:40:13 +0000728 if (VD->getDeclContext()->isRecord()) {
729 // If the value is a class member, we might have a pointer-to-member.
730 // Determine whether the non-type template template parameter is of
731 // pointer-to-member type. If so, we need to build an appropriate
732 // expression for a pointer-to-member, since a "normal" DeclRefExpr
733 // would refer to the member itself.
734 if (NTTP->getType()->isMemberPointerType()) {
735 QualType ClassType
736 = SemaRef.Context.getTypeDeclType(
737 cast<RecordDecl>(VD->getDeclContext()));
738 NestedNameSpecifier *Qualifier
739 = NestedNameSpecifier::Create(SemaRef.Context, 0, false,
740 ClassType.getTypePtr());
741 CXXScopeSpec SS;
742 SS.setScopeRep(Qualifier);
743 OwningExprResult RefExpr
744 = SemaRef.BuildDeclRefExpr(VD,
745 VD->getType().getNonReferenceType(),
746 E->getLocation(),
Douglas Gregor231edff2009-11-12 17:40:13 +0000747 &SS);
748 if (RefExpr.isInvalid())
749 return SemaRef.ExprError();
750
751 return SemaRef.CreateBuiltinUnaryOp(E->getLocation(),
752 UnaryOperator::AddrOf,
753 move(RefExpr));
754 }
755 }
756
757 return SemaRef.BuildDeclRefExpr(VD, VD->getType().getNonReferenceType(),
Douglas Gregor0da76df2009-11-23 11:41:28 +0000758 E->getLocation());
Douglas Gregor550d9b22009-10-31 17:21:17 +0000759 }
Mike Stump1eb44332009-09-09 15:08:12 +0000760
Douglas Gregor550d9b22009-10-31 17:21:17 +0000761 assert(Arg.getKind() == TemplateArgument::Integral);
762 QualType T = Arg.getIntegralType();
763 if (T->isCharType() || T->isWideCharType())
764 return SemaRef.Owned(new (SemaRef.Context) CharacterLiteral(
765 Arg.getAsIntegral()->getZExtValue(),
766 T->isWideCharType(),
Mike Stump1eb44332009-09-09 15:08:12 +0000767 T,
Douglas Gregord6350ae2009-08-28 20:31:08 +0000768 E->getSourceRange().getBegin()));
Douglas Gregor550d9b22009-10-31 17:21:17 +0000769 if (T->isBooleanType())
770 return SemaRef.Owned(new (SemaRef.Context) CXXBoolLiteralExpr(
771 Arg.getAsIntegral()->getBoolValue(),
772 T,
773 E->getSourceRange().getBegin()));
774
775 assert(Arg.getAsIntegral()->getBitWidth() == SemaRef.Context.getIntWidth(T));
776 return SemaRef.Owned(new (SemaRef.Context) IntegerLiteral(
777 *Arg.getAsIntegral(),
778 T,
779 E->getSourceRange().getBegin()));
780 }
781
782 // We have a non-type template parameter that isn't fully substituted;
783 // FindInstantiatedDecl will find it in the local instantiation scope.
Douglas Gregorb98b1992009-08-11 05:31:07 +0000784 }
Mike Stump1eb44332009-09-09 15:08:12 +0000785
Douglas Gregore95b4092009-09-16 18:34:49 +0000786 NamedDecl *InstD = SemaRef.FindInstantiatedDecl(D, TemplateArgs);
Douglas Gregorb98b1992009-08-11 05:31:07 +0000787 if (!InstD)
788 return SemaRef.ExprError();
Mike Stump1eb44332009-09-09 15:08:12 +0000789
John McCallba135432009-11-21 08:51:07 +0000790 assert(!isa<UsingDecl>(InstD) && "decl ref instantiated to UsingDecl");
Mike Stump1eb44332009-09-09 15:08:12 +0000791
Douglas Gregora2813ce2009-10-23 18:54:35 +0000792 CXXScopeSpec SS;
793 NestedNameSpecifier *Qualifier = 0;
794 if (E->getQualifier()) {
795 Qualifier = TransformNestedNameSpecifier(E->getQualifier(),
796 E->getQualifierRange());
797 if (!Qualifier)
798 return SemaRef.ExprError();
799
800 SS.setScopeRep(Qualifier);
801 SS.setRange(E->getQualifierRange());
802 }
803
John McCallf7a1a742009-11-24 19:00:30 +0000804 return SemaRef.BuildDeclarationNameExpr(SS, E->getLocation(), InstD);
Douglas Gregorb98b1992009-08-11 05:31:07 +0000805}
806
Sebastian Redla29e51b2009-11-08 13:56:19 +0000807Sema::OwningExprResult TemplateInstantiator::TransformCXXDefaultArgExpr(
808 CXXDefaultArgExpr *E, bool isAddressOfOperand) {
809 assert(!cast<FunctionDecl>(E->getParam()->getDeclContext())->
810 getDescribedFunctionTemplate() &&
811 "Default arg expressions are never formed in dependent cases.");
812 return SemaRef.Owned(E->Retain());
813}
814
815
Mike Stump1eb44332009-09-09 15:08:12 +0000816QualType
John McCalla2becad2009-10-21 00:40:46 +0000817TemplateInstantiator::TransformTemplateTypeParmType(TypeLocBuilder &TLB,
818 TemplateTypeParmTypeLoc TL) {
819 TemplateTypeParmType *T = TL.getTypePtr();
Douglas Gregord6350ae2009-08-28 20:31:08 +0000820 if (T->getDepth() < TemplateArgs.getNumLevels()) {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000821 // Replace the template type parameter with its corresponding
822 // template argument.
Mike Stump1eb44332009-09-09 15:08:12 +0000823
824 // If the corresponding template argument is NULL or doesn't exist, it's
825 // because we are performing instantiation from explicitly-specified
826 // template arguments in a function template class, but there were some
Douglas Gregor16134c62009-07-01 00:28:38 +0000827 // arguments left unspecified.
John McCalla2becad2009-10-21 00:40:46 +0000828 if (!TemplateArgs.hasTemplateArgument(T->getDepth(), T->getIndex())) {
829 TemplateTypeParmTypeLoc NewTL
830 = TLB.push<TemplateTypeParmTypeLoc>(TL.getType());
831 NewTL.setNameLoc(TL.getNameLoc());
832 return TL.getType();
833 }
Mike Stump1eb44332009-09-09 15:08:12 +0000834
835 assert(TemplateArgs(T->getDepth(), T->getIndex()).getKind()
Douglas Gregord6350ae2009-08-28 20:31:08 +0000836 == TemplateArgument::Type &&
Douglas Gregor99ebf652009-02-27 19:31:52 +0000837 "Template argument kind mismatch");
Douglas Gregord6350ae2009-08-28 20:31:08 +0000838
John McCall49a832b2009-10-18 09:09:24 +0000839 QualType Replacement
840 = TemplateArgs(T->getDepth(), T->getIndex()).getAsType();
841
842 // TODO: only do this uniquing once, at the start of instantiation.
John McCalla2becad2009-10-21 00:40:46 +0000843 QualType Result
844 = getSema().Context.getSubstTemplateTypeParmType(T, Replacement);
845 SubstTemplateTypeParmTypeLoc NewTL
846 = TLB.push<SubstTemplateTypeParmTypeLoc>(Result);
847 NewTL.setNameLoc(TL.getNameLoc());
848 return Result;
Mike Stump1eb44332009-09-09 15:08:12 +0000849 }
Douglas Gregor99ebf652009-02-27 19:31:52 +0000850
851 // The template type parameter comes from an inner template (e.g.,
852 // the template parameter list of a member template inside the
853 // template we are instantiating). Create a new template type
854 // parameter with the template "level" reduced by one.
John McCalla2becad2009-10-21 00:40:46 +0000855 QualType Result
856 = getSema().Context.getTemplateTypeParmType(T->getDepth()
857 - TemplateArgs.getNumLevels(),
858 T->getIndex(),
859 T->isParameterPack(),
860 T->getName());
861 TemplateTypeParmTypeLoc NewTL = TLB.push<TemplateTypeParmTypeLoc>(Result);
862 NewTL.setNameLoc(TL.getNameLoc());
863 return Result;
Douglas Gregorcd281c32009-02-28 00:25:32 +0000864}
Douglas Gregor99ebf652009-02-27 19:31:52 +0000865
John McCallce3ff2b2009-08-25 22:02:44 +0000866/// \brief Perform substitution on the type T with a given set of template
867/// arguments.
Douglas Gregor99ebf652009-02-27 19:31:52 +0000868///
869/// This routine substitutes the given template arguments into the
870/// type T and produces the instantiated type.
871///
872/// \param T the type into which the template arguments will be
873/// substituted. If this type is not dependent, it will be returned
874/// immediately.
875///
876/// \param TemplateArgs the template arguments that will be
877/// substituted for the top-level template parameters within T.
878///
Douglas Gregor99ebf652009-02-27 19:31:52 +0000879/// \param Loc the location in the source code where this substitution
880/// is being performed. It will typically be the location of the
881/// declarator (if we're instantiating the type of some declaration)
882/// or the location of the type in the source code (if, e.g., we're
883/// instantiating the type of a cast expression).
884///
885/// \param Entity the name of the entity associated with a declaration
886/// being instantiated (if any). May be empty to indicate that there
887/// is no such entity (if, e.g., this is a type that occurs as part of
888/// a cast expression) or that the entity has no name (e.g., an
889/// unnamed function parameter).
890///
891/// \returns If the instantiation succeeds, the instantiated
892/// type. Otherwise, produces diagnostics and returns a NULL type.
John McCallcd7ba1c2009-10-21 00:58:09 +0000893DeclaratorInfo *Sema::SubstType(DeclaratorInfo *T,
894 const MultiLevelTemplateArgumentList &Args,
895 SourceLocation Loc,
896 DeclarationName Entity) {
897 assert(!ActiveTemplateInstantiations.empty() &&
898 "Cannot perform an instantiation without some context on the "
899 "instantiation stack");
900
901 if (!T->getType()->isDependentType())
902 return T;
903
904 TemplateInstantiator Instantiator(*this, Args, Loc, Entity);
905 return Instantiator.TransformType(T);
906}
907
908/// Deprecated form of the above.
Mike Stump1eb44332009-09-09 15:08:12 +0000909QualType Sema::SubstType(QualType T,
Douglas Gregord6350ae2009-08-28 20:31:08 +0000910 const MultiLevelTemplateArgumentList &TemplateArgs,
John McCallce3ff2b2009-08-25 22:02:44 +0000911 SourceLocation Loc, DeclarationName Entity) {
Douglas Gregordf667e72009-03-10 20:44:00 +0000912 assert(!ActiveTemplateInstantiations.empty() &&
913 "Cannot perform an instantiation without some context on the "
914 "instantiation stack");
915
Douglas Gregor99ebf652009-02-27 19:31:52 +0000916 // If T is not a dependent type, there is nothing to do.
917 if (!T->isDependentType())
918 return T;
919
Douglas Gregor577f75a2009-08-04 16:50:30 +0000920 TemplateInstantiator Instantiator(*this, TemplateArgs, Loc, Entity);
921 return Instantiator.TransformType(T);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000922}
Douglas Gregor2943aed2009-03-03 04:44:36 +0000923
John McCallce3ff2b2009-08-25 22:02:44 +0000924/// \brief Perform substitution on the base class specifiers of the
925/// given class template specialization.
Douglas Gregor2943aed2009-03-03 04:44:36 +0000926///
927/// Produces a diagnostic and returns true on error, returns false and
928/// attaches the instantiated base classes to the class template
929/// specialization if successful.
Mike Stump1eb44332009-09-09 15:08:12 +0000930bool
John McCallce3ff2b2009-08-25 22:02:44 +0000931Sema::SubstBaseSpecifiers(CXXRecordDecl *Instantiation,
932 CXXRecordDecl *Pattern,
Douglas Gregord6350ae2009-08-28 20:31:08 +0000933 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor2943aed2009-03-03 04:44:36 +0000934 bool Invalid = false;
Douglas Gregor0ca20ac2009-05-29 18:27:38 +0000935 llvm::SmallVector<CXXBaseSpecifier*, 4> InstantiatedBases;
Mike Stump1eb44332009-09-09 15:08:12 +0000936 for (ClassTemplateSpecializationDecl::base_class_iterator
Douglas Gregord475b8d2009-03-25 21:17:03 +0000937 Base = Pattern->bases_begin(), BaseEnd = Pattern->bases_end();
Douglas Gregor27b152f2009-03-10 18:52:44 +0000938 Base != BaseEnd; ++Base) {
Douglas Gregor2943aed2009-03-03 04:44:36 +0000939 if (!Base->getType()->isDependentType()) {
Fariborz Jahanian71c6e712009-07-22 17:41:53 +0000940 InstantiatedBases.push_back(new (Context) CXXBaseSpecifier(*Base));
Douglas Gregor2943aed2009-03-03 04:44:36 +0000941 continue;
942 }
943
Mike Stump1eb44332009-09-09 15:08:12 +0000944 QualType BaseType = SubstType(Base->getType(),
945 TemplateArgs,
John McCallce3ff2b2009-08-25 22:02:44 +0000946 Base->getSourceRange().getBegin(),
947 DeclarationName());
Douglas Gregor2943aed2009-03-03 04:44:36 +0000948 if (BaseType.isNull()) {
949 Invalid = true;
950 continue;
951 }
952
953 if (CXXBaseSpecifier *InstantiatedBase
Douglas Gregord475b8d2009-03-25 21:17:03 +0000954 = CheckBaseSpecifier(Instantiation,
Douglas Gregor2943aed2009-03-03 04:44:36 +0000955 Base->getSourceRange(),
956 Base->isVirtual(),
957 Base->getAccessSpecifierAsWritten(),
958 BaseType,
959 /*FIXME: Not totally accurate */
960 Base->getSourceRange().getBegin()))
961 InstantiatedBases.push_back(InstantiatedBase);
962 else
963 Invalid = true;
964 }
965
Douglas Gregor27b152f2009-03-10 18:52:44 +0000966 if (!Invalid &&
Jay Foadbeaaccd2009-05-21 09:52:38 +0000967 AttachBaseSpecifiers(Instantiation, InstantiatedBases.data(),
Douglas Gregor2943aed2009-03-03 04:44:36 +0000968 InstantiatedBases.size()))
969 Invalid = true;
970
971 return Invalid;
972}
973
Douglas Gregord475b8d2009-03-25 21:17:03 +0000974/// \brief Instantiate the definition of a class from a given pattern.
975///
976/// \param PointOfInstantiation The point of instantiation within the
977/// source code.
978///
979/// \param Instantiation is the declaration whose definition is being
980/// instantiated. This will be either a class template specialization
981/// or a member class of a class template specialization.
982///
983/// \param Pattern is the pattern from which the instantiation
984/// occurs. This will be either the declaration of a class template or
985/// the declaration of a member class of a class template.
986///
987/// \param TemplateArgs The template arguments to be substituted into
988/// the pattern.
989///
Douglas Gregord0e3daf2009-09-04 22:48:11 +0000990/// \param TSK the kind of implicit or explicit instantiation to perform.
Douglas Gregor5842ba92009-08-24 15:23:48 +0000991///
992/// \param Complain whether to complain if the class cannot be instantiated due
993/// to the lack of a definition.
994///
Douglas Gregord475b8d2009-03-25 21:17:03 +0000995/// \returns true if an error occurred, false otherwise.
996bool
997Sema::InstantiateClass(SourceLocation PointOfInstantiation,
998 CXXRecordDecl *Instantiation, CXXRecordDecl *Pattern,
Douglas Gregord6350ae2009-08-28 20:31:08 +0000999 const MultiLevelTemplateArgumentList &TemplateArgs,
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001000 TemplateSpecializationKind TSK,
Douglas Gregor5842ba92009-08-24 15:23:48 +00001001 bool Complain) {
Douglas Gregord475b8d2009-03-25 21:17:03 +00001002 bool Invalid = false;
John McCalle29ba202009-08-20 01:44:21 +00001003
Mike Stump1eb44332009-09-09 15:08:12 +00001004 CXXRecordDecl *PatternDef
Douglas Gregord475b8d2009-03-25 21:17:03 +00001005 = cast_or_null<CXXRecordDecl>(Pattern->getDefinition(Context));
1006 if (!PatternDef) {
Douglas Gregor5842ba92009-08-24 15:23:48 +00001007 if (!Complain) {
1008 // Say nothing
1009 } else if (Pattern == Instantiation->getInstantiatedFromMemberClass()) {
Douglas Gregord475b8d2009-03-25 21:17:03 +00001010 Diag(PointOfInstantiation,
1011 diag::err_implicit_instantiate_member_undefined)
1012 << Context.getTypeDeclType(Instantiation);
1013 Diag(Pattern->getLocation(), diag::note_member_of_template_here);
1014 } else {
Douglas Gregor93dfdb12009-05-13 00:25:59 +00001015 Diag(PointOfInstantiation, diag::err_template_instantiate_undefined)
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001016 << (TSK != TSK_ImplicitInstantiation)
Douglas Gregord475b8d2009-03-25 21:17:03 +00001017 << Context.getTypeDeclType(Instantiation);
1018 Diag(Pattern->getLocation(), diag::note_template_decl_here);
1019 }
1020 return true;
1021 }
1022 Pattern = PatternDef;
1023
Douglas Gregor454885e2009-10-15 15:54:05 +00001024 // \brief Record the point of instantiation.
1025 if (MemberSpecializationInfo *MSInfo
1026 = Instantiation->getMemberSpecializationInfo()) {
1027 MSInfo->setTemplateSpecializationKind(TSK);
1028 MSInfo->setPointOfInstantiation(PointOfInstantiation);
Douglas Gregor972e6ce2009-10-27 06:26:26 +00001029 } else if (ClassTemplateSpecializationDecl *Spec
1030 = dyn_cast<ClassTemplateSpecializationDecl>(Instantiation)) {
1031 Spec->setTemplateSpecializationKind(TSK);
1032 Spec->setPointOfInstantiation(PointOfInstantiation);
Douglas Gregor454885e2009-10-15 15:54:05 +00001033 }
1034
Douglas Gregord048bb72009-03-25 21:23:52 +00001035 InstantiatingTemplate Inst(*this, PointOfInstantiation, Instantiation);
Douglas Gregord475b8d2009-03-25 21:17:03 +00001036 if (Inst)
1037 return true;
1038
1039 // Enter the scope of this instantiation. We don't use
1040 // PushDeclContext because we don't have a scope.
1041 DeclContext *PreviousContext = CurContext;
1042 CurContext = Instantiation;
1043
1044 // Start the definition of this instantiation.
1045 Instantiation->startDefinition();
1046
John McCallce3ff2b2009-08-25 22:02:44 +00001047 // Do substitution on the base class specifiers.
1048 if (SubstBaseSpecifiers(Instantiation, Pattern, TemplateArgs))
Douglas Gregord475b8d2009-03-25 21:17:03 +00001049 Invalid = true;
1050
Douglas Gregor0ca20ac2009-05-29 18:27:38 +00001051 llvm::SmallVector<DeclPtrTy, 4> Fields;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001052 for (RecordDecl::decl_iterator Member = Pattern->decls_begin(),
Mike Stump1eb44332009-09-09 15:08:12 +00001053 MemberEnd = Pattern->decls_end();
Douglas Gregor6ab35242009-04-09 21:40:53 +00001054 Member != MemberEnd; ++Member) {
John McCallce3ff2b2009-08-25 22:02:44 +00001055 Decl *NewMember = SubstDecl(*Member, Instantiation, TemplateArgs);
Douglas Gregord475b8d2009-03-25 21:17:03 +00001056 if (NewMember) {
Douglas Gregor9148c3f2009-11-11 19:13:48 +00001057 if (NewMember->isInvalidDecl()) {
Douglas Gregord475b8d2009-03-25 21:17:03 +00001058 Invalid = true;
Douglas Gregor9148c3f2009-11-11 19:13:48 +00001059 } else if (FieldDecl *Field = dyn_cast<FieldDecl>(NewMember))
Chris Lattnerb28317a2009-03-28 19:18:32 +00001060 Fields.push_back(DeclPtrTy::make(Field));
Anders Carlsson0d8df782009-08-29 19:37:28 +00001061 else if (UsingDecl *UD = dyn_cast<UsingDecl>(NewMember))
1062 Instantiation->addDecl(UD);
Douglas Gregord475b8d2009-03-25 21:17:03 +00001063 } else {
1064 // FIXME: Eventually, a NULL return will mean that one of the
Mike Stump390b4cc2009-05-16 07:39:55 +00001065 // instantiations was a semantic disaster, and we'll want to set Invalid =
1066 // true. For now, we expect to skip some members that we can't yet handle.
Douglas Gregord475b8d2009-03-25 21:17:03 +00001067 }
1068 }
1069
1070 // Finish checking fields.
Chris Lattnerb28317a2009-03-28 19:18:32 +00001071 ActOnFields(0, Instantiation->getLocation(), DeclPtrTy::make(Instantiation),
Jay Foadbeaaccd2009-05-21 09:52:38 +00001072 Fields.data(), Fields.size(), SourceLocation(), SourceLocation(),
Douglas Gregord475b8d2009-03-25 21:17:03 +00001073 0);
Douglas Gregor663b5a02009-10-14 20:14:33 +00001074 if (Instantiation->isInvalidDecl())
1075 Invalid = true;
1076
Douglas Gregord475b8d2009-03-25 21:17:03 +00001077 // Add any implicitly-declared members that we might need.
Douglas Gregor663b5a02009-10-14 20:14:33 +00001078 if (!Invalid)
1079 AddImplicitlyDeclaredMembersToClass(Instantiation);
Douglas Gregord475b8d2009-03-25 21:17:03 +00001080
1081 // Exit the scope of this instantiation.
1082 CurContext = PreviousContext;
1083
Douglas Gregoraba43bb2009-05-26 20:50:29 +00001084 if (!Invalid)
1085 Consumer.HandleTagDeclDefinition(Instantiation);
1086
Douglas Gregord475b8d2009-03-25 21:17:03 +00001087 return Invalid;
1088}
1089
Mike Stump1eb44332009-09-09 15:08:12 +00001090bool
Douglas Gregor2943aed2009-03-03 04:44:36 +00001091Sema::InstantiateClassTemplateSpecialization(
Douglas Gregor972e6ce2009-10-27 06:26:26 +00001092 SourceLocation PointOfInstantiation,
Douglas Gregor2943aed2009-03-03 04:44:36 +00001093 ClassTemplateSpecializationDecl *ClassTemplateSpec,
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001094 TemplateSpecializationKind TSK,
Douglas Gregor5842ba92009-08-24 15:23:48 +00001095 bool Complain) {
Douglas Gregor2943aed2009-03-03 04:44:36 +00001096 // Perform the actual instantiation on the canonical declaration.
1097 ClassTemplateSpec = cast<ClassTemplateSpecializationDecl>(
Argyrios Kyrtzidis97fbaa22009-07-18 00:34:25 +00001098 ClassTemplateSpec->getCanonicalDecl());
Douglas Gregor2943aed2009-03-03 04:44:36 +00001099
Douglas Gregor52604ab2009-09-11 21:19:12 +00001100 // Check whether we have already instantiated or specialized this class
1101 // template specialization.
1102 if (ClassTemplateSpec->getSpecializationKind() != TSK_Undeclared) {
1103 if (ClassTemplateSpec->getSpecializationKind() ==
1104 TSK_ExplicitInstantiationDeclaration &&
1105 TSK == TSK_ExplicitInstantiationDefinition) {
1106 // An explicit instantiation definition follows an explicit instantiation
1107 // declaration (C++0x [temp.explicit]p10); go ahead and perform the
1108 // explicit instantiation.
1109 ClassTemplateSpec->setSpecializationKind(TSK);
Douglas Gregor52604ab2009-09-11 21:19:12 +00001110 return false;
1111 }
1112
1113 // We can only instantiate something that hasn't already been
1114 // instantiated or specialized. Fail without any diagnostics: our
1115 // caller will provide an error message.
Douglas Gregor2943aed2009-03-03 04:44:36 +00001116 return true;
Douglas Gregor52604ab2009-09-11 21:19:12 +00001117 }
Douglas Gregor2943aed2009-03-03 04:44:36 +00001118
Douglas Gregor9eea08b2009-09-15 16:51:42 +00001119 if (ClassTemplateSpec->isInvalidDecl())
1120 return true;
1121
Douglas Gregor2943aed2009-03-03 04:44:36 +00001122 ClassTemplateDecl *Template = ClassTemplateSpec->getSpecializedTemplate();
Douglas Gregord6350ae2009-08-28 20:31:08 +00001123 CXXRecordDecl *Pattern = 0;
Douglas Gregorc8ab2562009-05-31 09:31:02 +00001124
Douglas Gregorc1efb3f2009-06-12 22:31:52 +00001125 // C++ [temp.class.spec.match]p1:
1126 // When a class template is used in a context that requires an
1127 // instantiation of the class, it is necessary to determine
1128 // whether the instantiation is to be generated using the primary
1129 // template or one of the partial specializations. This is done by
1130 // matching the template arguments of the class template
1131 // specialization with the template argument lists of the partial
1132 // specializations.
Douglas Gregor199d9912009-06-05 00:53:49 +00001133 typedef std::pair<ClassTemplatePartialSpecializationDecl *,
1134 TemplateArgumentList *> MatchResult;
1135 llvm::SmallVector<MatchResult, 4> Matched;
Mike Stump1eb44332009-09-09 15:08:12 +00001136 for (llvm::FoldingSet<ClassTemplatePartialSpecializationDecl>::iterator
Douglas Gregorc8ab2562009-05-31 09:31:02 +00001137 Partial = Template->getPartialSpecializations().begin(),
1138 PartialEnd = Template->getPartialSpecializations().end();
1139 Partial != PartialEnd;
1140 ++Partial) {
Douglas Gregorf67875d2009-06-12 18:26:56 +00001141 TemplateDeductionInfo Info(Context);
1142 if (TemplateDeductionResult Result
Mike Stump1eb44332009-09-09 15:08:12 +00001143 = DeduceTemplateArguments(&*Partial,
Douglas Gregorf67875d2009-06-12 18:26:56 +00001144 ClassTemplateSpec->getTemplateArgs(),
1145 Info)) {
1146 // FIXME: Store the failed-deduction information for use in
1147 // diagnostics, later.
1148 (void)Result;
1149 } else {
1150 Matched.push_back(std::make_pair(&*Partial, Info.take()));
1151 }
Douglas Gregorc8ab2562009-05-31 09:31:02 +00001152 }
1153
Douglas Gregored9c0f92009-10-29 00:04:11 +00001154 if (Matched.size() >= 1) {
Douglas Gregorbf4ea562009-09-15 16:23:51 +00001155 llvm::SmallVector<MatchResult, 4>::iterator Best = Matched.begin();
Douglas Gregored9c0f92009-10-29 00:04:11 +00001156 if (Matched.size() == 1) {
1157 // -- If exactly one matching specialization is found, the
1158 // instantiation is generated from that specialization.
1159 // We don't need to do anything for this.
1160 } else {
1161 // -- If more than one matching specialization is found, the
1162 // partial order rules (14.5.4.2) are used to determine
1163 // whether one of the specializations is more specialized
1164 // than the others. If none of the specializations is more
1165 // specialized than all of the other matching
1166 // specializations, then the use of the class template is
1167 // ambiguous and the program is ill-formed.
1168 for (llvm::SmallVector<MatchResult, 4>::iterator P = Best + 1,
1169 PEnd = Matched.end();
1170 P != PEnd; ++P) {
1171 if (getMoreSpecializedPartialSpecialization(P->first, Best->first)
1172 == P->first)
1173 Best = P;
Douglas Gregorbf4ea562009-09-15 16:23:51 +00001174 }
Douglas Gregorbf4ea562009-09-15 16:23:51 +00001175
Douglas Gregored9c0f92009-10-29 00:04:11 +00001176 // Determine if the best partial specialization is more specialized than
1177 // the others.
1178 bool Ambiguous = false;
Douglas Gregorbf4ea562009-09-15 16:23:51 +00001179 for (llvm::SmallVector<MatchResult, 4>::iterator P = Matched.begin(),
1180 PEnd = Matched.end();
Douglas Gregored9c0f92009-10-29 00:04:11 +00001181 P != PEnd; ++P) {
1182 if (P != Best &&
1183 getMoreSpecializedPartialSpecialization(P->first, Best->first)
1184 != Best->first) {
1185 Ambiguous = true;
1186 break;
1187 }
1188 }
1189
1190 if (Ambiguous) {
1191 // Partial ordering did not produce a clear winner. Complain.
1192 ClassTemplateSpec->setInvalidDecl();
1193 Diag(PointOfInstantiation, diag::err_partial_spec_ordering_ambiguous)
1194 << ClassTemplateSpec;
1195
1196 // Print the matching partial specializations.
1197 for (llvm::SmallVector<MatchResult, 4>::iterator P = Matched.begin(),
1198 PEnd = Matched.end();
1199 P != PEnd; ++P)
1200 Diag(P->first->getLocation(), diag::note_partial_spec_match)
1201 << getTemplateArgumentBindingsText(P->first->getTemplateParameters(),
1202 *P->second);
Douglas Gregord6350ae2009-08-28 20:31:08 +00001203
Douglas Gregored9c0f92009-10-29 00:04:11 +00001204 return true;
1205 }
Douglas Gregorbf4ea562009-09-15 16:23:51 +00001206 }
1207
1208 // Instantiate using the best class template partial specialization.
Douglas Gregored9c0f92009-10-29 00:04:11 +00001209 ClassTemplatePartialSpecializationDecl *OrigPartialSpec = Best->first;
1210 while (OrigPartialSpec->getInstantiatedFromMember()) {
1211 // If we've found an explicit specialization of this class template,
1212 // stop here and use that as the pattern.
1213 if (OrigPartialSpec->isMemberSpecialization())
1214 break;
1215
1216 OrigPartialSpec = OrigPartialSpec->getInstantiatedFromMember();
1217 }
1218
1219 Pattern = OrigPartialSpec;
Douglas Gregorbf4ea562009-09-15 16:23:51 +00001220 ClassTemplateSpec->setInstantiationOf(Best->first, Best->second);
Douglas Gregorc1efb3f2009-06-12 22:31:52 +00001221 } else {
1222 // -- If no matches are found, the instantiation is generated
1223 // from the primary template.
Douglas Gregord6350ae2009-08-28 20:31:08 +00001224 ClassTemplateDecl *OrigTemplate = Template;
Douglas Gregorfd056bc2009-10-13 16:30:37 +00001225 while (OrigTemplate->getInstantiatedFromMemberTemplate()) {
1226 // If we've found an explicit specialization of this class template,
1227 // stop here and use that as the pattern.
1228 if (OrigTemplate->isMemberSpecialization())
1229 break;
1230
Douglas Gregord6350ae2009-08-28 20:31:08 +00001231 OrigTemplate = OrigTemplate->getInstantiatedFromMemberTemplate();
Douglas Gregorfd056bc2009-10-13 16:30:37 +00001232 }
1233
Douglas Gregord6350ae2009-08-28 20:31:08 +00001234 Pattern = OrigTemplate->getTemplatedDecl();
Douglas Gregorc8ab2562009-05-31 09:31:02 +00001235 }
Douglas Gregor2943aed2009-03-03 04:44:36 +00001236
Douglas Gregor972e6ce2009-10-27 06:26:26 +00001237 bool Result = InstantiateClass(PointOfInstantiation, ClassTemplateSpec,
1238 Pattern,
1239 getTemplateInstantiationArgs(ClassTemplateSpec),
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001240 TSK,
Douglas Gregor5842ba92009-08-24 15:23:48 +00001241 Complain);
Mike Stump1eb44332009-09-09 15:08:12 +00001242
Douglas Gregor199d9912009-06-05 00:53:49 +00001243 for (unsigned I = 0, N = Matched.size(); I != N; ++I) {
1244 // FIXME: Implement TemplateArgumentList::Destroy!
1245 // if (Matched[I].first != Pattern)
1246 // Matched[I].second->Destroy(Context);
1247 }
Mike Stump1eb44332009-09-09 15:08:12 +00001248
Douglas Gregor199d9912009-06-05 00:53:49 +00001249 return Result;
Douglas Gregor2943aed2009-03-03 04:44:36 +00001250}
Douglas Gregor5953d8b2009-03-19 17:26:29 +00001251
John McCallce3ff2b2009-08-25 22:02:44 +00001252/// \brief Instantiates the definitions of all of the member
1253/// of the given class, which is an instantiation of a class template
1254/// or a member class of a template.
Douglas Gregora58861f2009-05-13 20:28:22 +00001255void
1256Sema::InstantiateClassMembers(SourceLocation PointOfInstantiation,
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001257 CXXRecordDecl *Instantiation,
1258 const MultiLevelTemplateArgumentList &TemplateArgs,
1259 TemplateSpecializationKind TSK) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001260 for (DeclContext::decl_iterator D = Instantiation->decls_begin(),
1261 DEnd = Instantiation->decls_end();
Douglas Gregora58861f2009-05-13 20:28:22 +00001262 D != DEnd; ++D) {
Douglas Gregor0d035142009-10-27 18:42:08 +00001263 bool SuppressNew = false;
Douglas Gregora58861f2009-05-13 20:28:22 +00001264 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(*D)) {
Douglas Gregor0d035142009-10-27 18:42:08 +00001265 if (FunctionDecl *Pattern
1266 = Function->getInstantiatedFromMemberFunction()) {
1267 MemberSpecializationInfo *MSInfo
1268 = Function->getMemberSpecializationInfo();
1269 assert(MSInfo && "No member specialization information?");
1270 if (CheckSpecializationInstantiationRedecl(PointOfInstantiation, TSK,
1271 Function,
1272 MSInfo->getTemplateSpecializationKind(),
1273 MSInfo->getPointOfInstantiation(),
1274 SuppressNew) ||
1275 SuppressNew)
Douglas Gregorf6b11852009-10-08 15:14:33 +00001276 continue;
1277
Douglas Gregor0d035142009-10-27 18:42:08 +00001278 if (Function->getBody())
1279 continue;
1280
1281 if (TSK == TSK_ExplicitInstantiationDefinition) {
1282 // C++0x [temp.explicit]p8:
1283 // An explicit instantiation definition that names a class template
1284 // specialization explicitly instantiates the class template
1285 // specialization and is only an explicit instantiation definition
1286 // of members whose definition is visible at the point of
1287 // instantiation.
1288 if (!Pattern->getBody())
1289 continue;
1290
1291 Function->setTemplateSpecializationKind(TSK, PointOfInstantiation);
1292
1293 InstantiateFunctionDefinition(PointOfInstantiation, Function);
1294 } else {
1295 Function->setTemplateSpecializationKind(TSK, PointOfInstantiation);
1296 }
Douglas Gregorf6b11852009-10-08 15:14:33 +00001297 }
Douglas Gregora58861f2009-05-13 20:28:22 +00001298 } else if (VarDecl *Var = dyn_cast<VarDecl>(*D)) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001299 if (Var->isStaticDataMember()) {
Douglas Gregor0d035142009-10-27 18:42:08 +00001300 MemberSpecializationInfo *MSInfo = Var->getMemberSpecializationInfo();
1301 assert(MSInfo && "No member specialization information?");
1302 if (CheckSpecializationInstantiationRedecl(PointOfInstantiation, TSK,
1303 Var,
1304 MSInfo->getTemplateSpecializationKind(),
1305 MSInfo->getPointOfInstantiation(),
1306 SuppressNew) ||
1307 SuppressNew)
Douglas Gregorf6b11852009-10-08 15:14:33 +00001308 continue;
1309
Douglas Gregor0d035142009-10-27 18:42:08 +00001310 if (TSK == TSK_ExplicitInstantiationDefinition) {
1311 // C++0x [temp.explicit]p8:
1312 // An explicit instantiation definition that names a class template
1313 // specialization explicitly instantiates the class template
1314 // specialization and is only an explicit instantiation definition
1315 // of members whose definition is visible at the point of
1316 // instantiation.
1317 if (!Var->getInstantiatedFromStaticDataMember()
1318 ->getOutOfLineDefinition())
1319 continue;
1320
1321 Var->setTemplateSpecializationKind(TSK, PointOfInstantiation);
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001322 InstantiateStaticDataMemberDefinition(PointOfInstantiation, Var);
Douglas Gregor0d035142009-10-27 18:42:08 +00001323 } else {
1324 Var->setTemplateSpecializationKind(TSK, PointOfInstantiation);
1325 }
1326 }
Douglas Gregora58861f2009-05-13 20:28:22 +00001327 } else if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(*D)) {
Douglas Gregor2db32322009-10-07 23:56:10 +00001328 if (Record->isInjectedClassName())
1329 continue;
1330
Douglas Gregor0d035142009-10-27 18:42:08 +00001331 MemberSpecializationInfo *MSInfo = Record->getMemberSpecializationInfo();
1332 assert(MSInfo && "No member specialization information?");
1333 if (CheckSpecializationInstantiationRedecl(PointOfInstantiation, TSK,
1334 Record,
1335 MSInfo->getTemplateSpecializationKind(),
1336 MSInfo->getPointOfInstantiation(),
1337 SuppressNew) ||
1338 SuppressNew)
Douglas Gregorf6b11852009-10-08 15:14:33 +00001339 continue;
1340
Douglas Gregor0d035142009-10-27 18:42:08 +00001341 CXXRecordDecl *Pattern = Record->getInstantiatedFromMemberClass();
1342 assert(Pattern && "Missing instantiated-from-template information");
1343
1344 if (!Record->getDefinition(Context)) {
1345 if (!Pattern->getDefinition(Context)) {
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 (TSK == TSK_ExplicitInstantiationDeclaration) {
1353 MSInfo->setTemplateSpecializationKind(TSK);
1354 MSInfo->setPointOfInstantiation(PointOfInstantiation);
1355 }
1356
1357 continue;
1358 }
1359
1360 InstantiateClass(PointOfInstantiation, Record, Pattern,
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001361 TemplateArgs,
1362 TSK);
Douglas Gregor0d035142009-10-27 18:42:08 +00001363 }
Douglas Gregore9374d52009-10-08 01:19:17 +00001364
Douglas Gregor0d035142009-10-27 18:42:08 +00001365 Pattern = cast_or_null<CXXRecordDecl>(Record->getDefinition(Context));
1366 if (Pattern)
1367 InstantiateClassMembers(PointOfInstantiation, Pattern, TemplateArgs,
1368 TSK);
Douglas Gregora58861f2009-05-13 20:28:22 +00001369 }
1370 }
1371}
1372
1373/// \brief Instantiate the definitions of all of the members of the
1374/// given class template specialization, which was named as part of an
1375/// explicit instantiation.
Mike Stump1eb44332009-09-09 15:08:12 +00001376void
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001377Sema::InstantiateClassTemplateSpecializationMembers(
Douglas Gregora58861f2009-05-13 20:28:22 +00001378 SourceLocation PointOfInstantiation,
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001379 ClassTemplateSpecializationDecl *ClassTemplateSpec,
1380 TemplateSpecializationKind TSK) {
Douglas Gregora58861f2009-05-13 20:28:22 +00001381 // C++0x [temp.explicit]p7:
1382 // An explicit instantiation that names a class template
1383 // specialization is an explicit instantion of the same kind
1384 // (declaration or definition) of each of its members (not
1385 // including members inherited from base classes) that has not
1386 // been previously explicitly specialized in the translation unit
1387 // containing the explicit instantiation, except as described
1388 // below.
1389 InstantiateClassMembers(PointOfInstantiation, ClassTemplateSpec,
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001390 getTemplateInstantiationArgs(ClassTemplateSpec),
1391 TSK);
Douglas Gregora58861f2009-05-13 20:28:22 +00001392}
1393
Mike Stump1eb44332009-09-09 15:08:12 +00001394Sema::OwningStmtResult
Douglas Gregord6350ae2009-08-28 20:31:08 +00001395Sema::SubstStmt(Stmt *S, const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor43959a92009-08-20 07:17:43 +00001396 if (!S)
1397 return Owned(S);
1398
1399 TemplateInstantiator Instantiator(*this, TemplateArgs,
1400 SourceLocation(),
1401 DeclarationName());
1402 return Instantiator.TransformStmt(S);
1403}
1404
Mike Stump1eb44332009-09-09 15:08:12 +00001405Sema::OwningExprResult
Douglas Gregord6350ae2009-08-28 20:31:08 +00001406Sema::SubstExpr(Expr *E, const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregorb98b1992009-08-11 05:31:07 +00001407 if (!E)
1408 return Owned(E);
Mike Stump1eb44332009-09-09 15:08:12 +00001409
Douglas Gregorb98b1992009-08-11 05:31:07 +00001410 TemplateInstantiator Instantiator(*this, TemplateArgs,
1411 SourceLocation(),
1412 DeclarationName());
1413 return Instantiator.TransformExpr(E);
1414}
1415
John McCallce3ff2b2009-08-25 22:02:44 +00001416/// \brief Do template substitution on a nested-name-specifier.
Douglas Gregorab452ba2009-03-26 23:50:42 +00001417NestedNameSpecifier *
John McCallce3ff2b2009-08-25 22:02:44 +00001418Sema::SubstNestedNameSpecifier(NestedNameSpecifier *NNS,
Douglas Gregord6350ae2009-08-28 20:31:08 +00001419 SourceRange Range,
1420 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregordcee1a12009-08-06 05:28:30 +00001421 TemplateInstantiator Instantiator(*this, TemplateArgs, Range.getBegin(),
1422 DeclarationName());
1423 return Instantiator.TransformNestedNameSpecifier(NNS, Range);
Douglas Gregor5953d8b2009-03-19 17:26:29 +00001424}
Douglas Gregorde650ae2009-03-31 18:38:02 +00001425
1426TemplateName
John McCallce3ff2b2009-08-25 22:02:44 +00001427Sema::SubstTemplateName(TemplateName Name, SourceLocation Loc,
Douglas Gregord6350ae2009-08-28 20:31:08 +00001428 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregord1067e52009-08-06 06:41:21 +00001429 TemplateInstantiator Instantiator(*this, TemplateArgs, Loc,
1430 DeclarationName());
1431 return Instantiator.TransformTemplateName(Name);
Douglas Gregorde650ae2009-03-31 18:38:02 +00001432}
Douglas Gregor91333002009-06-11 00:06:24 +00001433
John McCall833ca992009-10-29 08:12:44 +00001434bool Sema::Subst(const TemplateArgumentLoc &Input, TemplateArgumentLoc &Output,
1435 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor670444e2009-08-04 22:27:00 +00001436 TemplateInstantiator Instantiator(*this, TemplateArgs, SourceLocation(),
1437 DeclarationName());
John McCall833ca992009-10-29 08:12:44 +00001438
1439 return Instantiator.TransformTemplateArgument(Input, Output);
Douglas Gregor91333002009-06-11 00:06:24 +00001440}