blob: 502c151f4ef4a651e8fe04a68bf8aad5b4d3f192 [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);
John McCallba135432009-11-21 08:51:07 +0000560 Sema::OwningExprResult TransformUnresolvedLookupExpr(
561 UnresolvedLookupExpr *E,
562 bool isAddressOfOperand);
Mike Stump1eb44332009-09-09 15:08:12 +0000563
Sebastian Redla29e51b2009-11-08 13:56:19 +0000564 Sema::OwningExprResult TransformCXXDefaultArgExpr(CXXDefaultArgExpr *E,
565 bool isAddressOfOperand);
566
Mike Stump1eb44332009-09-09 15:08:12 +0000567 /// \brief Transforms a template type parameter type by performing
Douglas Gregor577f75a2009-08-04 16:50:30 +0000568 /// substitution of the corresponding template type argument.
John McCalla2becad2009-10-21 00:40:46 +0000569 QualType TransformTemplateTypeParmType(TypeLocBuilder &TLB,
570 TemplateTypeParmTypeLoc TL);
Douglas Gregor577f75a2009-08-04 16:50:30 +0000571 };
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000572}
573
Douglas Gregor577f75a2009-08-04 16:50:30 +0000574Decl *TemplateInstantiator::TransformDecl(Decl *D) {
Douglas Gregorc68afe22009-09-03 21:38:09 +0000575 if (!D)
576 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000577
Douglas Gregorc68afe22009-09-03 21:38:09 +0000578 if (TemplateTemplateParmDecl *TTP = dyn_cast<TemplateTemplateParmDecl>(D)) {
Douglas Gregord6350ae2009-08-28 20:31:08 +0000579 if (TTP->getDepth() < TemplateArgs.getNumLevels()) {
Douglas Gregor788cd062009-11-11 01:00:40 +0000580 TemplateName Template
581 = TemplateArgs(TTP->getDepth(), TTP->getPosition()).getAsTemplate();
582 assert(!Template.isNull() && Template.getAsTemplateDecl() &&
Douglas Gregord6350ae2009-08-28 20:31:08 +0000583 "Wrong kind of template template argument");
Douglas Gregor788cd062009-11-11 01:00:40 +0000584 return Template.getAsTemplateDecl();
Douglas Gregord6350ae2009-08-28 20:31:08 +0000585 }
Mike Stump1eb44332009-09-09 15:08:12 +0000586
587 // If the corresponding template argument is NULL or non-existent, it's
588 // because we are performing instantiation from explicitly-specified
Douglas Gregord6350ae2009-08-28 20:31:08 +0000589 // template arguments in a function template, but there were some
590 // arguments left unspecified.
Mike Stump1eb44332009-09-09 15:08:12 +0000591 if (!TemplateArgs.hasTemplateArgument(TTP->getDepth(),
Douglas Gregord6350ae2009-08-28 20:31:08 +0000592 TTP->getPosition()))
593 return D;
Mike Stump1eb44332009-09-09 15:08:12 +0000594
Douglas Gregor788cd062009-11-11 01:00:40 +0000595 // Fall through to find the instantiated declaration for this template
596 // template parameter.
Douglas Gregord1067e52009-08-06 06:41:21 +0000597 }
Mike Stump1eb44332009-09-09 15:08:12 +0000598
Douglas Gregore95b4092009-09-16 18:34:49 +0000599 return SemaRef.FindInstantiatedDecl(cast<NamedDecl>(D), TemplateArgs);
Douglas Gregor577f75a2009-08-04 16:50:30 +0000600}
601
Douglas Gregor43959a92009-08-20 07:17:43 +0000602Decl *TemplateInstantiator::TransformDefinition(Decl *D) {
John McCallce3ff2b2009-08-25 22:02:44 +0000603 Decl *Inst = getSema().SubstDecl(D, getSema().CurContext, TemplateArgs);
Douglas Gregor43959a92009-08-20 07:17:43 +0000604 if (!Inst)
605 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000606
Douglas Gregor43959a92009-08-20 07:17:43 +0000607 getSema().CurrentInstantiationScope->InstantiatedLocal(D, Inst);
608 return Inst;
609}
610
Douglas Gregor6cd21982009-10-20 05:58:46 +0000611NamedDecl *
612TemplateInstantiator::TransformFirstQualifierInScope(NamedDecl *D,
613 SourceLocation Loc) {
614 // If the first part of the nested-name-specifier was a template type
615 // parameter, instantiate that type parameter down to a tag type.
616 if (TemplateTypeParmDecl *TTPD = dyn_cast_or_null<TemplateTypeParmDecl>(D)) {
617 const TemplateTypeParmType *TTP
618 = cast<TemplateTypeParmType>(getSema().Context.getTypeDeclType(TTPD));
619 if (TTP->getDepth() < TemplateArgs.getNumLevels()) {
620 QualType T = TemplateArgs(TTP->getDepth(), TTP->getIndex()).getAsType();
621 if (T.isNull())
622 return cast_or_null<NamedDecl>(TransformDecl(D));
623
624 if (const TagType *Tag = T->getAs<TagType>())
625 return Tag->getDecl();
626
627 // The resulting type is not a tag; complain.
628 getSema().Diag(Loc, diag::err_nested_name_spec_non_tag) << T;
629 return 0;
630 }
631 }
632
633 return cast_or_null<NamedDecl>(TransformDecl(D));
634}
635
Douglas Gregor43959a92009-08-20 07:17:43 +0000636VarDecl *
637TemplateInstantiator::RebuildExceptionDecl(VarDecl *ExceptionDecl,
Mike Stump1eb44332009-09-09 15:08:12 +0000638 QualType T,
Douglas Gregor43959a92009-08-20 07:17:43 +0000639 DeclaratorInfo *Declarator,
640 IdentifierInfo *Name,
Mike Stump1eb44332009-09-09 15:08:12 +0000641 SourceLocation Loc,
Douglas Gregor43959a92009-08-20 07:17:43 +0000642 SourceRange TypeRange) {
643 VarDecl *Var = inherited::RebuildExceptionDecl(ExceptionDecl, T, Declarator,
644 Name, Loc, TypeRange);
645 if (Var && !Var->isInvalidDecl())
646 getSema().CurrentInstantiationScope->InstantiatedLocal(ExceptionDecl, Var);
647 return Var;
648}
649
John McCallc4e70192009-09-11 04:59:25 +0000650QualType
651TemplateInstantiator::RebuildElaboratedType(QualType T,
652 ElaboratedType::TagKind Tag) {
653 if (const TagType *TT = T->getAs<TagType>()) {
654 TagDecl* TD = TT->getDecl();
655
656 // FIXME: this location is very wrong; we really need typelocs.
657 SourceLocation TagLocation = TD->getTagKeywordLoc();
658
659 // FIXME: type might be anonymous.
660 IdentifierInfo *Id = TD->getIdentifier();
661
662 // TODO: should we even warn on struct/class mismatches for this? Seems
663 // like it's likely to produce a lot of spurious errors.
664 if (!SemaRef.isAcceptableTagRedeclaration(TD, Tag, TagLocation, *Id)) {
665 SemaRef.Diag(TagLocation, diag::err_use_with_wrong_tag)
666 << Id
667 << CodeModificationHint::CreateReplacement(SourceRange(TagLocation),
668 TD->getKindName());
669 SemaRef.Diag(TD->getLocation(), diag::note_previous_use);
670 }
671 }
672
673 return TreeTransform<TemplateInstantiator>::RebuildElaboratedType(T, Tag);
674}
675
676Sema::OwningExprResult
Douglas Gregorc86a6e92009-11-04 07:01:15 +0000677TemplateInstantiator::TransformPredefinedExpr(PredefinedExpr *E,
678 bool isAddressOfOperand) {
Anders Carlsson773f3972009-09-11 01:22:35 +0000679 if (!E->isTypeDependent())
680 return SemaRef.Owned(E->Retain());
681
682 FunctionDecl *currentDecl = getSema().getCurFunctionDecl();
683 assert(currentDecl && "Must have current function declaration when "
684 "instantiating.");
685
686 PredefinedExpr::IdentType IT = E->getIdentType();
687
688 unsigned Length =
689 PredefinedExpr::ComputeName(getSema().Context, IT, currentDecl).length();
690
691 llvm::APInt LengthI(32, Length + 1);
John McCall0953e762009-09-24 19:53:00 +0000692 QualType ResTy = getSema().Context.CharTy.withConst();
Anders Carlsson773f3972009-09-11 01:22:35 +0000693 ResTy = getSema().Context.getConstantArrayType(ResTy, LengthI,
694 ArrayType::Normal, 0);
695 PredefinedExpr *PE =
696 new (getSema().Context) PredefinedExpr(E->getLocation(), ResTy, IT);
697 return getSema().Owned(PE);
698}
699
700Sema::OwningExprResult
John McCallba135432009-11-21 08:51:07 +0000701TemplateInstantiator::TransformUnresolvedLookupExpr(UnresolvedLookupExpr *Old,
702 bool isAddressOfOperand) {
John McCall5b3f9132009-11-22 01:44:31 +0000703 LookupResult R(SemaRef, Old->getName(), Old->getNameLoc(),
704 Sema::LookupOrdinaryName);
John McCallba135432009-11-21 08:51:07 +0000705
John McCallba135432009-11-21 08:51:07 +0000706 for (UnresolvedLookupExpr::decls_iterator I = Old->decls_begin(),
707 E = Old->decls_end(); I != E; ++I) {
708 NamedDecl *InstD = SemaRef.FindInstantiatedDecl(*I, TemplateArgs);
709 if (!InstD)
710 return SemaRef.ExprError();
711
John McCall7453ed42009-11-22 00:44:51 +0000712 // The lookup values can never instantiate to a UsingDecl, because
713 // only UnresolvedUsingValueDecls do that, and those can never
714 // appear in UnresolvedLookupExprs (only UnresolvedMemberLookupExprs).
715 assert(!isa<UsingDecl>(InstD));
John McCallba135432009-11-21 08:51:07 +0000716
John McCall7453ed42009-11-22 00:44:51 +0000717 // Analogously.
718 assert(!isa<UnresolvedUsingValueDecl>(InstD->getUnderlyingDecl()));
John McCallba135432009-11-21 08:51:07 +0000719
John McCall5b3f9132009-11-22 01:44:31 +0000720 R.addDecl(InstD);
John McCallba135432009-11-21 08:51:07 +0000721 }
722
John McCall5b3f9132009-11-22 01:44:31 +0000723 R.resolveKind();
724
725 // This shouldn't be possible.
726 assert(!R.isAmbiguous());
727
John McCallba135432009-11-21 08:51:07 +0000728 CXXScopeSpec SS;
729 NestedNameSpecifier *Qualifier = 0;
730 if (Old->getQualifier()) {
731 Qualifier = TransformNestedNameSpecifier(Old->getQualifier(),
732 Old->getQualifierRange());
733 if (!Qualifier)
734 return SemaRef.ExprError();
735
736 SS.setScopeRep(Qualifier);
737 SS.setRange(Old->getQualifierRange());
738 }
739
John McCall5b3f9132009-11-22 01:44:31 +0000740 return SemaRef.BuildDeclarationNameExpr(&SS, R, Old->requiresADL());
John McCallba135432009-11-21 08:51:07 +0000741}
742
743Sema::OwningExprResult
Douglas Gregorc86a6e92009-11-04 07:01:15 +0000744TemplateInstantiator::TransformDeclRefExpr(DeclRefExpr *E,
745 bool isAddressOfOperand) {
Douglas Gregorb98b1992009-08-11 05:31:07 +0000746 // FIXME: Clean this up a bit
747 NamedDecl *D = E->getDecl();
748 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(D)) {
Douglas Gregor550d9b22009-10-31 17:21:17 +0000749 if (NTTP->getDepth() < TemplateArgs.getNumLevels()) {
Douglas Gregor550d9b22009-10-31 17:21:17 +0000750 // If the corresponding template argument is NULL or non-existent, it's
751 // because we are performing instantiation from explicitly-specified
752 // template arguments in a function template, but there were some
753 // arguments left unspecified.
754 if (!TemplateArgs.hasTemplateArgument(NTTP->getDepth(),
755 NTTP->getPosition()))
756 return SemaRef.Owned(E->Retain());
Mike Stump1eb44332009-09-09 15:08:12 +0000757
Douglas Gregor550d9b22009-10-31 17:21:17 +0000758 const TemplateArgument &Arg = TemplateArgs(NTTP->getDepth(),
759 NTTP->getPosition());
Mike Stump1eb44332009-09-09 15:08:12 +0000760
Douglas Gregor550d9b22009-10-31 17:21:17 +0000761 // The template argument itself might be an expression, in which
762 // case we just return that expression.
763 if (Arg.getKind() == TemplateArgument::Expression)
764 return SemaRef.Owned(Arg.getAsExpr()->Retain());
Mike Stump1eb44332009-09-09 15:08:12 +0000765
Douglas Gregor550d9b22009-10-31 17:21:17 +0000766 if (Arg.getKind() == TemplateArgument::Declaration) {
767 ValueDecl *VD = cast<ValueDecl>(Arg.getAsDecl());
Mike Stump1eb44332009-09-09 15:08:12 +0000768
Douglas Gregor550d9b22009-10-31 17:21:17 +0000769 VD = cast_or_null<ValueDecl>(
Douglas Gregorc86a6e92009-11-04 07:01:15 +0000770 getSema().FindInstantiatedDecl(VD, TemplateArgs));
Douglas Gregor550d9b22009-10-31 17:21:17 +0000771 if (!VD)
772 return SemaRef.ExprError();
Mike Stump1eb44332009-09-09 15:08:12 +0000773
Douglas Gregor231edff2009-11-12 17:40:13 +0000774 if (VD->getDeclContext()->isRecord()) {
775 // If the value is a class member, we might have a pointer-to-member.
776 // Determine whether the non-type template template parameter is of
777 // pointer-to-member type. If so, we need to build an appropriate
778 // expression for a pointer-to-member, since a "normal" DeclRefExpr
779 // would refer to the member itself.
780 if (NTTP->getType()->isMemberPointerType()) {
781 QualType ClassType
782 = SemaRef.Context.getTypeDeclType(
783 cast<RecordDecl>(VD->getDeclContext()));
784 NestedNameSpecifier *Qualifier
785 = NestedNameSpecifier::Create(SemaRef.Context, 0, false,
786 ClassType.getTypePtr());
787 CXXScopeSpec SS;
788 SS.setScopeRep(Qualifier);
789 OwningExprResult RefExpr
790 = SemaRef.BuildDeclRefExpr(VD,
791 VD->getType().getNonReferenceType(),
792 E->getLocation(),
Douglas Gregor231edff2009-11-12 17:40:13 +0000793 &SS);
794 if (RefExpr.isInvalid())
795 return SemaRef.ExprError();
796
797 return SemaRef.CreateBuiltinUnaryOp(E->getLocation(),
798 UnaryOperator::AddrOf,
799 move(RefExpr));
800 }
801 }
802
803 return SemaRef.BuildDeclRefExpr(VD, VD->getType().getNonReferenceType(),
Douglas Gregor0da76df2009-11-23 11:41:28 +0000804 E->getLocation());
Douglas Gregor550d9b22009-10-31 17:21:17 +0000805 }
Mike Stump1eb44332009-09-09 15:08:12 +0000806
Douglas Gregor550d9b22009-10-31 17:21:17 +0000807 assert(Arg.getKind() == TemplateArgument::Integral);
808 QualType T = Arg.getIntegralType();
809 if (T->isCharType() || T->isWideCharType())
810 return SemaRef.Owned(new (SemaRef.Context) CharacterLiteral(
811 Arg.getAsIntegral()->getZExtValue(),
812 T->isWideCharType(),
Mike Stump1eb44332009-09-09 15:08:12 +0000813 T,
Douglas Gregord6350ae2009-08-28 20:31:08 +0000814 E->getSourceRange().getBegin()));
Douglas Gregor550d9b22009-10-31 17:21:17 +0000815 if (T->isBooleanType())
816 return SemaRef.Owned(new (SemaRef.Context) CXXBoolLiteralExpr(
817 Arg.getAsIntegral()->getBoolValue(),
818 T,
819 E->getSourceRange().getBegin()));
820
821 assert(Arg.getAsIntegral()->getBitWidth() == SemaRef.Context.getIntWidth(T));
822 return SemaRef.Owned(new (SemaRef.Context) IntegerLiteral(
823 *Arg.getAsIntegral(),
824 T,
825 E->getSourceRange().getBegin()));
826 }
827
828 // We have a non-type template parameter that isn't fully substituted;
829 // FindInstantiatedDecl will find it in the local instantiation scope.
Douglas Gregorb98b1992009-08-11 05:31:07 +0000830 }
Mike Stump1eb44332009-09-09 15:08:12 +0000831
Douglas Gregore95b4092009-09-16 18:34:49 +0000832 NamedDecl *InstD = SemaRef.FindInstantiatedDecl(D, TemplateArgs);
Douglas Gregorb98b1992009-08-11 05:31:07 +0000833 if (!InstD)
834 return SemaRef.ExprError();
Mike Stump1eb44332009-09-09 15:08:12 +0000835
John McCallba135432009-11-21 08:51:07 +0000836 assert(!isa<UsingDecl>(InstD) && "decl ref instantiated to UsingDecl");
Mike Stump1eb44332009-09-09 15:08:12 +0000837
Douglas Gregora2813ce2009-10-23 18:54:35 +0000838 CXXScopeSpec SS;
839 NestedNameSpecifier *Qualifier = 0;
840 if (E->getQualifier()) {
841 Qualifier = TransformNestedNameSpecifier(E->getQualifier(),
842 E->getQualifierRange());
843 if (!Qualifier)
844 return SemaRef.ExprError();
845
846 SS.setScopeRep(Qualifier);
847 SS.setRange(E->getQualifierRange());
848 }
849
John McCallba135432009-11-21 08:51:07 +0000850 return SemaRef.BuildDeclarationNameExpr(&SS, E->getLocation(), InstD);
Douglas Gregorb98b1992009-08-11 05:31:07 +0000851}
852
Sebastian Redla29e51b2009-11-08 13:56:19 +0000853Sema::OwningExprResult TemplateInstantiator::TransformCXXDefaultArgExpr(
854 CXXDefaultArgExpr *E, bool isAddressOfOperand) {
855 assert(!cast<FunctionDecl>(E->getParam()->getDeclContext())->
856 getDescribedFunctionTemplate() &&
857 "Default arg expressions are never formed in dependent cases.");
858 return SemaRef.Owned(E->Retain());
859}
860
861
Mike Stump1eb44332009-09-09 15:08:12 +0000862QualType
John McCalla2becad2009-10-21 00:40:46 +0000863TemplateInstantiator::TransformTemplateTypeParmType(TypeLocBuilder &TLB,
864 TemplateTypeParmTypeLoc TL) {
865 TemplateTypeParmType *T = TL.getTypePtr();
Douglas Gregord6350ae2009-08-28 20:31:08 +0000866 if (T->getDepth() < TemplateArgs.getNumLevels()) {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000867 // Replace the template type parameter with its corresponding
868 // template argument.
Mike Stump1eb44332009-09-09 15:08:12 +0000869
870 // If the corresponding template argument is NULL or doesn't exist, it's
871 // because we are performing instantiation from explicitly-specified
872 // template arguments in a function template class, but there were some
Douglas Gregor16134c62009-07-01 00:28:38 +0000873 // arguments left unspecified.
John McCalla2becad2009-10-21 00:40:46 +0000874 if (!TemplateArgs.hasTemplateArgument(T->getDepth(), T->getIndex())) {
875 TemplateTypeParmTypeLoc NewTL
876 = TLB.push<TemplateTypeParmTypeLoc>(TL.getType());
877 NewTL.setNameLoc(TL.getNameLoc());
878 return TL.getType();
879 }
Mike Stump1eb44332009-09-09 15:08:12 +0000880
881 assert(TemplateArgs(T->getDepth(), T->getIndex()).getKind()
Douglas Gregord6350ae2009-08-28 20:31:08 +0000882 == TemplateArgument::Type &&
Douglas Gregor99ebf652009-02-27 19:31:52 +0000883 "Template argument kind mismatch");
Douglas Gregord6350ae2009-08-28 20:31:08 +0000884
John McCall49a832b2009-10-18 09:09:24 +0000885 QualType Replacement
886 = TemplateArgs(T->getDepth(), T->getIndex()).getAsType();
887
888 // TODO: only do this uniquing once, at the start of instantiation.
John McCalla2becad2009-10-21 00:40:46 +0000889 QualType Result
890 = getSema().Context.getSubstTemplateTypeParmType(T, Replacement);
891 SubstTemplateTypeParmTypeLoc NewTL
892 = TLB.push<SubstTemplateTypeParmTypeLoc>(Result);
893 NewTL.setNameLoc(TL.getNameLoc());
894 return Result;
Mike Stump1eb44332009-09-09 15:08:12 +0000895 }
Douglas Gregor99ebf652009-02-27 19:31:52 +0000896
897 // The template type parameter comes from an inner template (e.g.,
898 // the template parameter list of a member template inside the
899 // template we are instantiating). Create a new template type
900 // parameter with the template "level" reduced by one.
John McCalla2becad2009-10-21 00:40:46 +0000901 QualType Result
902 = getSema().Context.getTemplateTypeParmType(T->getDepth()
903 - TemplateArgs.getNumLevels(),
904 T->getIndex(),
905 T->isParameterPack(),
906 T->getName());
907 TemplateTypeParmTypeLoc NewTL = TLB.push<TemplateTypeParmTypeLoc>(Result);
908 NewTL.setNameLoc(TL.getNameLoc());
909 return Result;
Douglas Gregorcd281c32009-02-28 00:25:32 +0000910}
Douglas Gregor99ebf652009-02-27 19:31:52 +0000911
John McCallce3ff2b2009-08-25 22:02:44 +0000912/// \brief Perform substitution on the type T with a given set of template
913/// arguments.
Douglas Gregor99ebf652009-02-27 19:31:52 +0000914///
915/// This routine substitutes the given template arguments into the
916/// type T and produces the instantiated type.
917///
918/// \param T the type into which the template arguments will be
919/// substituted. If this type is not dependent, it will be returned
920/// immediately.
921///
922/// \param TemplateArgs the template arguments that will be
923/// substituted for the top-level template parameters within T.
924///
Douglas Gregor99ebf652009-02-27 19:31:52 +0000925/// \param Loc the location in the source code where this substitution
926/// is being performed. It will typically be the location of the
927/// declarator (if we're instantiating the type of some declaration)
928/// or the location of the type in the source code (if, e.g., we're
929/// instantiating the type of a cast expression).
930///
931/// \param Entity the name of the entity associated with a declaration
932/// being instantiated (if any). May be empty to indicate that there
933/// is no such entity (if, e.g., this is a type that occurs as part of
934/// a cast expression) or that the entity has no name (e.g., an
935/// unnamed function parameter).
936///
937/// \returns If the instantiation succeeds, the instantiated
938/// type. Otherwise, produces diagnostics and returns a NULL type.
John McCallcd7ba1c2009-10-21 00:58:09 +0000939DeclaratorInfo *Sema::SubstType(DeclaratorInfo *T,
940 const MultiLevelTemplateArgumentList &Args,
941 SourceLocation Loc,
942 DeclarationName Entity) {
943 assert(!ActiveTemplateInstantiations.empty() &&
944 "Cannot perform an instantiation without some context on the "
945 "instantiation stack");
946
947 if (!T->getType()->isDependentType())
948 return T;
949
950 TemplateInstantiator Instantiator(*this, Args, Loc, Entity);
951 return Instantiator.TransformType(T);
952}
953
954/// Deprecated form of the above.
Mike Stump1eb44332009-09-09 15:08:12 +0000955QualType Sema::SubstType(QualType T,
Douglas Gregord6350ae2009-08-28 20:31:08 +0000956 const MultiLevelTemplateArgumentList &TemplateArgs,
John McCallce3ff2b2009-08-25 22:02:44 +0000957 SourceLocation Loc, DeclarationName Entity) {
Douglas Gregordf667e72009-03-10 20:44:00 +0000958 assert(!ActiveTemplateInstantiations.empty() &&
959 "Cannot perform an instantiation without some context on the "
960 "instantiation stack");
961
Douglas Gregor99ebf652009-02-27 19:31:52 +0000962 // If T is not a dependent type, there is nothing to do.
963 if (!T->isDependentType())
964 return T;
965
Douglas Gregor577f75a2009-08-04 16:50:30 +0000966 TemplateInstantiator Instantiator(*this, TemplateArgs, Loc, Entity);
967 return Instantiator.TransformType(T);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000968}
Douglas Gregor2943aed2009-03-03 04:44:36 +0000969
John McCallce3ff2b2009-08-25 22:02:44 +0000970/// \brief Perform substitution on the base class specifiers of the
971/// given class template specialization.
Douglas Gregor2943aed2009-03-03 04:44:36 +0000972///
973/// Produces a diagnostic and returns true on error, returns false and
974/// attaches the instantiated base classes to the class template
975/// specialization if successful.
Mike Stump1eb44332009-09-09 15:08:12 +0000976bool
John McCallce3ff2b2009-08-25 22:02:44 +0000977Sema::SubstBaseSpecifiers(CXXRecordDecl *Instantiation,
978 CXXRecordDecl *Pattern,
Douglas Gregord6350ae2009-08-28 20:31:08 +0000979 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor2943aed2009-03-03 04:44:36 +0000980 bool Invalid = false;
Douglas Gregor0ca20ac2009-05-29 18:27:38 +0000981 llvm::SmallVector<CXXBaseSpecifier*, 4> InstantiatedBases;
Mike Stump1eb44332009-09-09 15:08:12 +0000982 for (ClassTemplateSpecializationDecl::base_class_iterator
Douglas Gregord475b8d2009-03-25 21:17:03 +0000983 Base = Pattern->bases_begin(), BaseEnd = Pattern->bases_end();
Douglas Gregor27b152f2009-03-10 18:52:44 +0000984 Base != BaseEnd; ++Base) {
Douglas Gregor2943aed2009-03-03 04:44:36 +0000985 if (!Base->getType()->isDependentType()) {
Fariborz Jahanian71c6e712009-07-22 17:41:53 +0000986 InstantiatedBases.push_back(new (Context) CXXBaseSpecifier(*Base));
Douglas Gregor2943aed2009-03-03 04:44:36 +0000987 continue;
988 }
989
Mike Stump1eb44332009-09-09 15:08:12 +0000990 QualType BaseType = SubstType(Base->getType(),
991 TemplateArgs,
John McCallce3ff2b2009-08-25 22:02:44 +0000992 Base->getSourceRange().getBegin(),
993 DeclarationName());
Douglas Gregor2943aed2009-03-03 04:44:36 +0000994 if (BaseType.isNull()) {
995 Invalid = true;
996 continue;
997 }
998
999 if (CXXBaseSpecifier *InstantiatedBase
Douglas Gregord475b8d2009-03-25 21:17:03 +00001000 = CheckBaseSpecifier(Instantiation,
Douglas Gregor2943aed2009-03-03 04:44:36 +00001001 Base->getSourceRange(),
1002 Base->isVirtual(),
1003 Base->getAccessSpecifierAsWritten(),
1004 BaseType,
1005 /*FIXME: Not totally accurate */
1006 Base->getSourceRange().getBegin()))
1007 InstantiatedBases.push_back(InstantiatedBase);
1008 else
1009 Invalid = true;
1010 }
1011
Douglas Gregor27b152f2009-03-10 18:52:44 +00001012 if (!Invalid &&
Jay Foadbeaaccd2009-05-21 09:52:38 +00001013 AttachBaseSpecifiers(Instantiation, InstantiatedBases.data(),
Douglas Gregor2943aed2009-03-03 04:44:36 +00001014 InstantiatedBases.size()))
1015 Invalid = true;
1016
1017 return Invalid;
1018}
1019
Douglas Gregord475b8d2009-03-25 21:17:03 +00001020/// \brief Instantiate the definition of a class from a given pattern.
1021///
1022/// \param PointOfInstantiation The point of instantiation within the
1023/// source code.
1024///
1025/// \param Instantiation is the declaration whose definition is being
1026/// instantiated. This will be either a class template specialization
1027/// or a member class of a class template specialization.
1028///
1029/// \param Pattern is the pattern from which the instantiation
1030/// occurs. This will be either the declaration of a class template or
1031/// the declaration of a member class of a class template.
1032///
1033/// \param TemplateArgs The template arguments to be substituted into
1034/// the pattern.
1035///
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001036/// \param TSK the kind of implicit or explicit instantiation to perform.
Douglas Gregor5842ba92009-08-24 15:23:48 +00001037///
1038/// \param Complain whether to complain if the class cannot be instantiated due
1039/// to the lack of a definition.
1040///
Douglas Gregord475b8d2009-03-25 21:17:03 +00001041/// \returns true if an error occurred, false otherwise.
1042bool
1043Sema::InstantiateClass(SourceLocation PointOfInstantiation,
1044 CXXRecordDecl *Instantiation, CXXRecordDecl *Pattern,
Douglas Gregord6350ae2009-08-28 20:31:08 +00001045 const MultiLevelTemplateArgumentList &TemplateArgs,
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001046 TemplateSpecializationKind TSK,
Douglas Gregor5842ba92009-08-24 15:23:48 +00001047 bool Complain) {
Douglas Gregord475b8d2009-03-25 21:17:03 +00001048 bool Invalid = false;
John McCalle29ba202009-08-20 01:44:21 +00001049
Mike Stump1eb44332009-09-09 15:08:12 +00001050 CXXRecordDecl *PatternDef
Douglas Gregord475b8d2009-03-25 21:17:03 +00001051 = cast_or_null<CXXRecordDecl>(Pattern->getDefinition(Context));
1052 if (!PatternDef) {
Douglas Gregor5842ba92009-08-24 15:23:48 +00001053 if (!Complain) {
1054 // Say nothing
1055 } else if (Pattern == Instantiation->getInstantiatedFromMemberClass()) {
Douglas Gregord475b8d2009-03-25 21:17:03 +00001056 Diag(PointOfInstantiation,
1057 diag::err_implicit_instantiate_member_undefined)
1058 << Context.getTypeDeclType(Instantiation);
1059 Diag(Pattern->getLocation(), diag::note_member_of_template_here);
1060 } else {
Douglas Gregor93dfdb12009-05-13 00:25:59 +00001061 Diag(PointOfInstantiation, diag::err_template_instantiate_undefined)
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001062 << (TSK != TSK_ImplicitInstantiation)
Douglas Gregord475b8d2009-03-25 21:17:03 +00001063 << Context.getTypeDeclType(Instantiation);
1064 Diag(Pattern->getLocation(), diag::note_template_decl_here);
1065 }
1066 return true;
1067 }
1068 Pattern = PatternDef;
1069
Douglas Gregor454885e2009-10-15 15:54:05 +00001070 // \brief Record the point of instantiation.
1071 if (MemberSpecializationInfo *MSInfo
1072 = Instantiation->getMemberSpecializationInfo()) {
1073 MSInfo->setTemplateSpecializationKind(TSK);
1074 MSInfo->setPointOfInstantiation(PointOfInstantiation);
Douglas Gregor972e6ce2009-10-27 06:26:26 +00001075 } else if (ClassTemplateSpecializationDecl *Spec
1076 = dyn_cast<ClassTemplateSpecializationDecl>(Instantiation)) {
1077 Spec->setTemplateSpecializationKind(TSK);
1078 Spec->setPointOfInstantiation(PointOfInstantiation);
Douglas Gregor454885e2009-10-15 15:54:05 +00001079 }
1080
Douglas Gregord048bb72009-03-25 21:23:52 +00001081 InstantiatingTemplate Inst(*this, PointOfInstantiation, Instantiation);
Douglas Gregord475b8d2009-03-25 21:17:03 +00001082 if (Inst)
1083 return true;
1084
1085 // Enter the scope of this instantiation. We don't use
1086 // PushDeclContext because we don't have a scope.
1087 DeclContext *PreviousContext = CurContext;
1088 CurContext = Instantiation;
1089
1090 // Start the definition of this instantiation.
1091 Instantiation->startDefinition();
1092
John McCallce3ff2b2009-08-25 22:02:44 +00001093 // Do substitution on the base class specifiers.
1094 if (SubstBaseSpecifiers(Instantiation, Pattern, TemplateArgs))
Douglas Gregord475b8d2009-03-25 21:17:03 +00001095 Invalid = true;
1096
Douglas Gregor0ca20ac2009-05-29 18:27:38 +00001097 llvm::SmallVector<DeclPtrTy, 4> Fields;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001098 for (RecordDecl::decl_iterator Member = Pattern->decls_begin(),
Mike Stump1eb44332009-09-09 15:08:12 +00001099 MemberEnd = Pattern->decls_end();
Douglas Gregor6ab35242009-04-09 21:40:53 +00001100 Member != MemberEnd; ++Member) {
John McCallce3ff2b2009-08-25 22:02:44 +00001101 Decl *NewMember = SubstDecl(*Member, Instantiation, TemplateArgs);
Douglas Gregord475b8d2009-03-25 21:17:03 +00001102 if (NewMember) {
Douglas Gregor9148c3f2009-11-11 19:13:48 +00001103 if (NewMember->isInvalidDecl()) {
Douglas Gregord475b8d2009-03-25 21:17:03 +00001104 Invalid = true;
Douglas Gregor9148c3f2009-11-11 19:13:48 +00001105 } else if (FieldDecl *Field = dyn_cast<FieldDecl>(NewMember))
Chris Lattnerb28317a2009-03-28 19:18:32 +00001106 Fields.push_back(DeclPtrTy::make(Field));
Anders Carlsson0d8df782009-08-29 19:37:28 +00001107 else if (UsingDecl *UD = dyn_cast<UsingDecl>(NewMember))
1108 Instantiation->addDecl(UD);
Douglas Gregord475b8d2009-03-25 21:17:03 +00001109 } else {
1110 // FIXME: Eventually, a NULL return will mean that one of the
Mike Stump390b4cc2009-05-16 07:39:55 +00001111 // instantiations was a semantic disaster, and we'll want to set Invalid =
1112 // true. For now, we expect to skip some members that we can't yet handle.
Douglas Gregord475b8d2009-03-25 21:17:03 +00001113 }
1114 }
1115
1116 // Finish checking fields.
Chris Lattnerb28317a2009-03-28 19:18:32 +00001117 ActOnFields(0, Instantiation->getLocation(), DeclPtrTy::make(Instantiation),
Jay Foadbeaaccd2009-05-21 09:52:38 +00001118 Fields.data(), Fields.size(), SourceLocation(), SourceLocation(),
Douglas Gregord475b8d2009-03-25 21:17:03 +00001119 0);
Douglas Gregor663b5a02009-10-14 20:14:33 +00001120 if (Instantiation->isInvalidDecl())
1121 Invalid = true;
1122
Douglas Gregord475b8d2009-03-25 21:17:03 +00001123 // Add any implicitly-declared members that we might need.
Douglas Gregor663b5a02009-10-14 20:14:33 +00001124 if (!Invalid)
1125 AddImplicitlyDeclaredMembersToClass(Instantiation);
Douglas Gregord475b8d2009-03-25 21:17:03 +00001126
1127 // Exit the scope of this instantiation.
1128 CurContext = PreviousContext;
1129
Douglas Gregoraba43bb2009-05-26 20:50:29 +00001130 if (!Invalid)
1131 Consumer.HandleTagDeclDefinition(Instantiation);
1132
Douglas Gregord475b8d2009-03-25 21:17:03 +00001133 return Invalid;
1134}
1135
Mike Stump1eb44332009-09-09 15:08:12 +00001136bool
Douglas Gregor2943aed2009-03-03 04:44:36 +00001137Sema::InstantiateClassTemplateSpecialization(
Douglas Gregor972e6ce2009-10-27 06:26:26 +00001138 SourceLocation PointOfInstantiation,
Douglas Gregor2943aed2009-03-03 04:44:36 +00001139 ClassTemplateSpecializationDecl *ClassTemplateSpec,
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001140 TemplateSpecializationKind TSK,
Douglas Gregor5842ba92009-08-24 15:23:48 +00001141 bool Complain) {
Douglas Gregor2943aed2009-03-03 04:44:36 +00001142 // Perform the actual instantiation on the canonical declaration.
1143 ClassTemplateSpec = cast<ClassTemplateSpecializationDecl>(
Argyrios Kyrtzidis97fbaa22009-07-18 00:34:25 +00001144 ClassTemplateSpec->getCanonicalDecl());
Douglas Gregor2943aed2009-03-03 04:44:36 +00001145
Douglas Gregor52604ab2009-09-11 21:19:12 +00001146 // Check whether we have already instantiated or specialized this class
1147 // template specialization.
1148 if (ClassTemplateSpec->getSpecializationKind() != TSK_Undeclared) {
1149 if (ClassTemplateSpec->getSpecializationKind() ==
1150 TSK_ExplicitInstantiationDeclaration &&
1151 TSK == TSK_ExplicitInstantiationDefinition) {
1152 // An explicit instantiation definition follows an explicit instantiation
1153 // declaration (C++0x [temp.explicit]p10); go ahead and perform the
1154 // explicit instantiation.
1155 ClassTemplateSpec->setSpecializationKind(TSK);
Douglas Gregor52604ab2009-09-11 21:19:12 +00001156 return false;
1157 }
1158
1159 // We can only instantiate something that hasn't already been
1160 // instantiated or specialized. Fail without any diagnostics: our
1161 // caller will provide an error message.
Douglas Gregor2943aed2009-03-03 04:44:36 +00001162 return true;
Douglas Gregor52604ab2009-09-11 21:19:12 +00001163 }
Douglas Gregor2943aed2009-03-03 04:44:36 +00001164
Douglas Gregor9eea08b2009-09-15 16:51:42 +00001165 if (ClassTemplateSpec->isInvalidDecl())
1166 return true;
1167
Douglas Gregor2943aed2009-03-03 04:44:36 +00001168 ClassTemplateDecl *Template = ClassTemplateSpec->getSpecializedTemplate();
Douglas Gregord6350ae2009-08-28 20:31:08 +00001169 CXXRecordDecl *Pattern = 0;
Douglas Gregorc8ab2562009-05-31 09:31:02 +00001170
Douglas Gregorc1efb3f2009-06-12 22:31:52 +00001171 // C++ [temp.class.spec.match]p1:
1172 // When a class template is used in a context that requires an
1173 // instantiation of the class, it is necessary to determine
1174 // whether the instantiation is to be generated using the primary
1175 // template or one of the partial specializations. This is done by
1176 // matching the template arguments of the class template
1177 // specialization with the template argument lists of the partial
1178 // specializations.
Douglas Gregor199d9912009-06-05 00:53:49 +00001179 typedef std::pair<ClassTemplatePartialSpecializationDecl *,
1180 TemplateArgumentList *> MatchResult;
1181 llvm::SmallVector<MatchResult, 4> Matched;
Mike Stump1eb44332009-09-09 15:08:12 +00001182 for (llvm::FoldingSet<ClassTemplatePartialSpecializationDecl>::iterator
Douglas Gregorc8ab2562009-05-31 09:31:02 +00001183 Partial = Template->getPartialSpecializations().begin(),
1184 PartialEnd = Template->getPartialSpecializations().end();
1185 Partial != PartialEnd;
1186 ++Partial) {
Douglas Gregorf67875d2009-06-12 18:26:56 +00001187 TemplateDeductionInfo Info(Context);
1188 if (TemplateDeductionResult Result
Mike Stump1eb44332009-09-09 15:08:12 +00001189 = DeduceTemplateArguments(&*Partial,
Douglas Gregorf67875d2009-06-12 18:26:56 +00001190 ClassTemplateSpec->getTemplateArgs(),
1191 Info)) {
1192 // FIXME: Store the failed-deduction information for use in
1193 // diagnostics, later.
1194 (void)Result;
1195 } else {
1196 Matched.push_back(std::make_pair(&*Partial, Info.take()));
1197 }
Douglas Gregorc8ab2562009-05-31 09:31:02 +00001198 }
1199
Douglas Gregored9c0f92009-10-29 00:04:11 +00001200 if (Matched.size() >= 1) {
Douglas Gregorbf4ea562009-09-15 16:23:51 +00001201 llvm::SmallVector<MatchResult, 4>::iterator Best = Matched.begin();
Douglas Gregored9c0f92009-10-29 00:04:11 +00001202 if (Matched.size() == 1) {
1203 // -- If exactly one matching specialization is found, the
1204 // instantiation is generated from that specialization.
1205 // We don't need to do anything for this.
1206 } else {
1207 // -- If more than one matching specialization is found, the
1208 // partial order rules (14.5.4.2) are used to determine
1209 // whether one of the specializations is more specialized
1210 // than the others. If none of the specializations is more
1211 // specialized than all of the other matching
1212 // specializations, then the use of the class template is
1213 // ambiguous and the program is ill-formed.
1214 for (llvm::SmallVector<MatchResult, 4>::iterator P = Best + 1,
1215 PEnd = Matched.end();
1216 P != PEnd; ++P) {
1217 if (getMoreSpecializedPartialSpecialization(P->first, Best->first)
1218 == P->first)
1219 Best = P;
Douglas Gregorbf4ea562009-09-15 16:23:51 +00001220 }
Douglas Gregorbf4ea562009-09-15 16:23:51 +00001221
Douglas Gregored9c0f92009-10-29 00:04:11 +00001222 // Determine if the best partial specialization is more specialized than
1223 // the others.
1224 bool Ambiguous = false;
Douglas Gregorbf4ea562009-09-15 16:23:51 +00001225 for (llvm::SmallVector<MatchResult, 4>::iterator P = Matched.begin(),
1226 PEnd = Matched.end();
Douglas Gregored9c0f92009-10-29 00:04:11 +00001227 P != PEnd; ++P) {
1228 if (P != Best &&
1229 getMoreSpecializedPartialSpecialization(P->first, Best->first)
1230 != Best->first) {
1231 Ambiguous = true;
1232 break;
1233 }
1234 }
1235
1236 if (Ambiguous) {
1237 // Partial ordering did not produce a clear winner. Complain.
1238 ClassTemplateSpec->setInvalidDecl();
1239 Diag(PointOfInstantiation, diag::err_partial_spec_ordering_ambiguous)
1240 << ClassTemplateSpec;
1241
1242 // Print the matching partial specializations.
1243 for (llvm::SmallVector<MatchResult, 4>::iterator P = Matched.begin(),
1244 PEnd = Matched.end();
1245 P != PEnd; ++P)
1246 Diag(P->first->getLocation(), diag::note_partial_spec_match)
1247 << getTemplateArgumentBindingsText(P->first->getTemplateParameters(),
1248 *P->second);
Douglas Gregord6350ae2009-08-28 20:31:08 +00001249
Douglas Gregored9c0f92009-10-29 00:04:11 +00001250 return true;
1251 }
Douglas Gregorbf4ea562009-09-15 16:23:51 +00001252 }
1253
1254 // Instantiate using the best class template partial specialization.
Douglas Gregored9c0f92009-10-29 00:04:11 +00001255 ClassTemplatePartialSpecializationDecl *OrigPartialSpec = Best->first;
1256 while (OrigPartialSpec->getInstantiatedFromMember()) {
1257 // If we've found an explicit specialization of this class template,
1258 // stop here and use that as the pattern.
1259 if (OrigPartialSpec->isMemberSpecialization())
1260 break;
1261
1262 OrigPartialSpec = OrigPartialSpec->getInstantiatedFromMember();
1263 }
1264
1265 Pattern = OrigPartialSpec;
Douglas Gregorbf4ea562009-09-15 16:23:51 +00001266 ClassTemplateSpec->setInstantiationOf(Best->first, Best->second);
Douglas Gregorc1efb3f2009-06-12 22:31:52 +00001267 } else {
1268 // -- If no matches are found, the instantiation is generated
1269 // from the primary template.
Douglas Gregord6350ae2009-08-28 20:31:08 +00001270 ClassTemplateDecl *OrigTemplate = Template;
Douglas Gregorfd056bc2009-10-13 16:30:37 +00001271 while (OrigTemplate->getInstantiatedFromMemberTemplate()) {
1272 // If we've found an explicit specialization of this class template,
1273 // stop here and use that as the pattern.
1274 if (OrigTemplate->isMemberSpecialization())
1275 break;
1276
Douglas Gregord6350ae2009-08-28 20:31:08 +00001277 OrigTemplate = OrigTemplate->getInstantiatedFromMemberTemplate();
Douglas Gregorfd056bc2009-10-13 16:30:37 +00001278 }
1279
Douglas Gregord6350ae2009-08-28 20:31:08 +00001280 Pattern = OrigTemplate->getTemplatedDecl();
Douglas Gregorc8ab2562009-05-31 09:31:02 +00001281 }
Douglas Gregor2943aed2009-03-03 04:44:36 +00001282
Douglas Gregor972e6ce2009-10-27 06:26:26 +00001283 bool Result = InstantiateClass(PointOfInstantiation, ClassTemplateSpec,
1284 Pattern,
1285 getTemplateInstantiationArgs(ClassTemplateSpec),
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001286 TSK,
Douglas Gregor5842ba92009-08-24 15:23:48 +00001287 Complain);
Mike Stump1eb44332009-09-09 15:08:12 +00001288
Douglas Gregor199d9912009-06-05 00:53:49 +00001289 for (unsigned I = 0, N = Matched.size(); I != N; ++I) {
1290 // FIXME: Implement TemplateArgumentList::Destroy!
1291 // if (Matched[I].first != Pattern)
1292 // Matched[I].second->Destroy(Context);
1293 }
Mike Stump1eb44332009-09-09 15:08:12 +00001294
Douglas Gregor199d9912009-06-05 00:53:49 +00001295 return Result;
Douglas Gregor2943aed2009-03-03 04:44:36 +00001296}
Douglas Gregor5953d8b2009-03-19 17:26:29 +00001297
John McCallce3ff2b2009-08-25 22:02:44 +00001298/// \brief Instantiates the definitions of all of the member
1299/// of the given class, which is an instantiation of a class template
1300/// or a member class of a template.
Douglas Gregora58861f2009-05-13 20:28:22 +00001301void
1302Sema::InstantiateClassMembers(SourceLocation PointOfInstantiation,
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001303 CXXRecordDecl *Instantiation,
1304 const MultiLevelTemplateArgumentList &TemplateArgs,
1305 TemplateSpecializationKind TSK) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001306 for (DeclContext::decl_iterator D = Instantiation->decls_begin(),
1307 DEnd = Instantiation->decls_end();
Douglas Gregora58861f2009-05-13 20:28:22 +00001308 D != DEnd; ++D) {
Douglas Gregor0d035142009-10-27 18:42:08 +00001309 bool SuppressNew = false;
Douglas Gregora58861f2009-05-13 20:28:22 +00001310 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(*D)) {
Douglas Gregor0d035142009-10-27 18:42:08 +00001311 if (FunctionDecl *Pattern
1312 = Function->getInstantiatedFromMemberFunction()) {
1313 MemberSpecializationInfo *MSInfo
1314 = Function->getMemberSpecializationInfo();
1315 assert(MSInfo && "No member specialization information?");
1316 if (CheckSpecializationInstantiationRedecl(PointOfInstantiation, TSK,
1317 Function,
1318 MSInfo->getTemplateSpecializationKind(),
1319 MSInfo->getPointOfInstantiation(),
1320 SuppressNew) ||
1321 SuppressNew)
Douglas Gregorf6b11852009-10-08 15:14:33 +00001322 continue;
1323
Douglas Gregor0d035142009-10-27 18:42:08 +00001324 if (Function->getBody())
1325 continue;
1326
1327 if (TSK == TSK_ExplicitInstantiationDefinition) {
1328 // C++0x [temp.explicit]p8:
1329 // An explicit instantiation definition that names a class template
1330 // specialization explicitly instantiates the class template
1331 // specialization and is only an explicit instantiation definition
1332 // of members whose definition is visible at the point of
1333 // instantiation.
1334 if (!Pattern->getBody())
1335 continue;
1336
1337 Function->setTemplateSpecializationKind(TSK, PointOfInstantiation);
1338
1339 InstantiateFunctionDefinition(PointOfInstantiation, Function);
1340 } else {
1341 Function->setTemplateSpecializationKind(TSK, PointOfInstantiation);
1342 }
Douglas Gregorf6b11852009-10-08 15:14:33 +00001343 }
Douglas Gregora58861f2009-05-13 20:28:22 +00001344 } else if (VarDecl *Var = dyn_cast<VarDecl>(*D)) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001345 if (Var->isStaticDataMember()) {
Douglas Gregor0d035142009-10-27 18:42:08 +00001346 MemberSpecializationInfo *MSInfo = Var->getMemberSpecializationInfo();
1347 assert(MSInfo && "No member specialization information?");
1348 if (CheckSpecializationInstantiationRedecl(PointOfInstantiation, TSK,
1349 Var,
1350 MSInfo->getTemplateSpecializationKind(),
1351 MSInfo->getPointOfInstantiation(),
1352 SuppressNew) ||
1353 SuppressNew)
Douglas Gregorf6b11852009-10-08 15:14:33 +00001354 continue;
1355
Douglas Gregor0d035142009-10-27 18:42:08 +00001356 if (TSK == TSK_ExplicitInstantiationDefinition) {
1357 // C++0x [temp.explicit]p8:
1358 // An explicit instantiation definition that names a class template
1359 // specialization explicitly instantiates the class template
1360 // specialization and is only an explicit instantiation definition
1361 // of members whose definition is visible at the point of
1362 // instantiation.
1363 if (!Var->getInstantiatedFromStaticDataMember()
1364 ->getOutOfLineDefinition())
1365 continue;
1366
1367 Var->setTemplateSpecializationKind(TSK, PointOfInstantiation);
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001368 InstantiateStaticDataMemberDefinition(PointOfInstantiation, Var);
Douglas Gregor0d035142009-10-27 18:42:08 +00001369 } else {
1370 Var->setTemplateSpecializationKind(TSK, PointOfInstantiation);
1371 }
1372 }
Douglas Gregora58861f2009-05-13 20:28:22 +00001373 } else if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(*D)) {
Douglas Gregor2db32322009-10-07 23:56:10 +00001374 if (Record->isInjectedClassName())
1375 continue;
1376
Douglas Gregor0d035142009-10-27 18:42:08 +00001377 MemberSpecializationInfo *MSInfo = Record->getMemberSpecializationInfo();
1378 assert(MSInfo && "No member specialization information?");
1379 if (CheckSpecializationInstantiationRedecl(PointOfInstantiation, TSK,
1380 Record,
1381 MSInfo->getTemplateSpecializationKind(),
1382 MSInfo->getPointOfInstantiation(),
1383 SuppressNew) ||
1384 SuppressNew)
Douglas Gregorf6b11852009-10-08 15:14:33 +00001385 continue;
1386
Douglas Gregor0d035142009-10-27 18:42:08 +00001387 CXXRecordDecl *Pattern = Record->getInstantiatedFromMemberClass();
1388 assert(Pattern && "Missing instantiated-from-template information");
1389
1390 if (!Record->getDefinition(Context)) {
1391 if (!Pattern->getDefinition(Context)) {
1392 // C++0x [temp.explicit]p8:
1393 // An explicit instantiation definition that names a class template
1394 // specialization explicitly instantiates the class template
1395 // specialization and is only an explicit instantiation definition
1396 // of members whose definition is visible at the point of
1397 // instantiation.
1398 if (TSK == TSK_ExplicitInstantiationDeclaration) {
1399 MSInfo->setTemplateSpecializationKind(TSK);
1400 MSInfo->setPointOfInstantiation(PointOfInstantiation);
1401 }
1402
1403 continue;
1404 }
1405
1406 InstantiateClass(PointOfInstantiation, Record, Pattern,
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001407 TemplateArgs,
1408 TSK);
Douglas Gregor0d035142009-10-27 18:42:08 +00001409 }
Douglas Gregore9374d52009-10-08 01:19:17 +00001410
Douglas Gregor0d035142009-10-27 18:42:08 +00001411 Pattern = cast_or_null<CXXRecordDecl>(Record->getDefinition(Context));
1412 if (Pattern)
1413 InstantiateClassMembers(PointOfInstantiation, Pattern, TemplateArgs,
1414 TSK);
Douglas Gregora58861f2009-05-13 20:28:22 +00001415 }
1416 }
1417}
1418
1419/// \brief Instantiate the definitions of all of the members of the
1420/// given class template specialization, which was named as part of an
1421/// explicit instantiation.
Mike Stump1eb44332009-09-09 15:08:12 +00001422void
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001423Sema::InstantiateClassTemplateSpecializationMembers(
Douglas Gregora58861f2009-05-13 20:28:22 +00001424 SourceLocation PointOfInstantiation,
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001425 ClassTemplateSpecializationDecl *ClassTemplateSpec,
1426 TemplateSpecializationKind TSK) {
Douglas Gregora58861f2009-05-13 20:28:22 +00001427 // C++0x [temp.explicit]p7:
1428 // An explicit instantiation that names a class template
1429 // specialization is an explicit instantion of the same kind
1430 // (declaration or definition) of each of its members (not
1431 // including members inherited from base classes) that has not
1432 // been previously explicitly specialized in the translation unit
1433 // containing the explicit instantiation, except as described
1434 // below.
1435 InstantiateClassMembers(PointOfInstantiation, ClassTemplateSpec,
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001436 getTemplateInstantiationArgs(ClassTemplateSpec),
1437 TSK);
Douglas Gregora58861f2009-05-13 20:28:22 +00001438}
1439
Mike Stump1eb44332009-09-09 15:08:12 +00001440Sema::OwningStmtResult
Douglas Gregord6350ae2009-08-28 20:31:08 +00001441Sema::SubstStmt(Stmt *S, const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor43959a92009-08-20 07:17:43 +00001442 if (!S)
1443 return Owned(S);
1444
1445 TemplateInstantiator Instantiator(*this, TemplateArgs,
1446 SourceLocation(),
1447 DeclarationName());
1448 return Instantiator.TransformStmt(S);
1449}
1450
Mike Stump1eb44332009-09-09 15:08:12 +00001451Sema::OwningExprResult
Douglas Gregord6350ae2009-08-28 20:31:08 +00001452Sema::SubstExpr(Expr *E, const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregorb98b1992009-08-11 05:31:07 +00001453 if (!E)
1454 return Owned(E);
Mike Stump1eb44332009-09-09 15:08:12 +00001455
Douglas Gregorb98b1992009-08-11 05:31:07 +00001456 TemplateInstantiator Instantiator(*this, TemplateArgs,
1457 SourceLocation(),
1458 DeclarationName());
1459 return Instantiator.TransformExpr(E);
1460}
1461
John McCallce3ff2b2009-08-25 22:02:44 +00001462/// \brief Do template substitution on a nested-name-specifier.
Douglas Gregorab452ba2009-03-26 23:50:42 +00001463NestedNameSpecifier *
John McCallce3ff2b2009-08-25 22:02:44 +00001464Sema::SubstNestedNameSpecifier(NestedNameSpecifier *NNS,
Douglas Gregord6350ae2009-08-28 20:31:08 +00001465 SourceRange Range,
1466 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregordcee1a12009-08-06 05:28:30 +00001467 TemplateInstantiator Instantiator(*this, TemplateArgs, Range.getBegin(),
1468 DeclarationName());
1469 return Instantiator.TransformNestedNameSpecifier(NNS, Range);
Douglas Gregor5953d8b2009-03-19 17:26:29 +00001470}
Douglas Gregorde650ae2009-03-31 18:38:02 +00001471
1472TemplateName
John McCallce3ff2b2009-08-25 22:02:44 +00001473Sema::SubstTemplateName(TemplateName Name, SourceLocation Loc,
Douglas Gregord6350ae2009-08-28 20:31:08 +00001474 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregord1067e52009-08-06 06:41:21 +00001475 TemplateInstantiator Instantiator(*this, TemplateArgs, Loc,
1476 DeclarationName());
1477 return Instantiator.TransformTemplateName(Name);
Douglas Gregorde650ae2009-03-31 18:38:02 +00001478}
Douglas Gregor91333002009-06-11 00:06:24 +00001479
John McCall833ca992009-10-29 08:12:44 +00001480bool Sema::Subst(const TemplateArgumentLoc &Input, TemplateArgumentLoc &Output,
1481 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor670444e2009-08-04 22:27:00 +00001482 TemplateInstantiator Instantiator(*this, TemplateArgs, SourceLocation(),
1483 DeclarationName());
John McCall833ca992009-10-29 08:12:44 +00001484
1485 return Instantiator.TransformTemplateArgument(Input, Output);
Douglas Gregor91333002009-06-11 00:06:24 +00001486}