blob: 3c5c239739b4fc63a9aa21074fca2d4ac92d67eb [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(),
793 /*FIXME:*/false, /*FIXME:*/false,
794 &SS);
795 if (RefExpr.isInvalid())
796 return SemaRef.ExprError();
797
798 return SemaRef.CreateBuiltinUnaryOp(E->getLocation(),
799 UnaryOperator::AddrOf,
800 move(RefExpr));
801 }
802 }
803
804 return SemaRef.BuildDeclRefExpr(VD, VD->getType().getNonReferenceType(),
805 E->getLocation(),
Douglas Gregor550d9b22009-10-31 17:21:17 +0000806 /*FIXME:*/false, /*FIXME:*/false);
807 }
Mike Stump1eb44332009-09-09 15:08:12 +0000808
Douglas Gregor550d9b22009-10-31 17:21:17 +0000809 assert(Arg.getKind() == TemplateArgument::Integral);
810 QualType T = Arg.getIntegralType();
811 if (T->isCharType() || T->isWideCharType())
812 return SemaRef.Owned(new (SemaRef.Context) CharacterLiteral(
813 Arg.getAsIntegral()->getZExtValue(),
814 T->isWideCharType(),
Mike Stump1eb44332009-09-09 15:08:12 +0000815 T,
Douglas Gregord6350ae2009-08-28 20:31:08 +0000816 E->getSourceRange().getBegin()));
Douglas Gregor550d9b22009-10-31 17:21:17 +0000817 if (T->isBooleanType())
818 return SemaRef.Owned(new (SemaRef.Context) CXXBoolLiteralExpr(
819 Arg.getAsIntegral()->getBoolValue(),
820 T,
821 E->getSourceRange().getBegin()));
822
823 assert(Arg.getAsIntegral()->getBitWidth() == SemaRef.Context.getIntWidth(T));
824 return SemaRef.Owned(new (SemaRef.Context) IntegerLiteral(
825 *Arg.getAsIntegral(),
826 T,
827 E->getSourceRange().getBegin()));
828 }
829
830 // We have a non-type template parameter that isn't fully substituted;
831 // FindInstantiatedDecl will find it in the local instantiation scope.
Douglas Gregorb98b1992009-08-11 05:31:07 +0000832 }
Mike Stump1eb44332009-09-09 15:08:12 +0000833
Douglas Gregore95b4092009-09-16 18:34:49 +0000834 NamedDecl *InstD = SemaRef.FindInstantiatedDecl(D, TemplateArgs);
Douglas Gregorb98b1992009-08-11 05:31:07 +0000835 if (!InstD)
836 return SemaRef.ExprError();
Mike Stump1eb44332009-09-09 15:08:12 +0000837
John McCallba135432009-11-21 08:51:07 +0000838 assert(!isa<UsingDecl>(InstD) && "decl ref instantiated to UsingDecl");
Mike Stump1eb44332009-09-09 15:08:12 +0000839
Douglas Gregora2813ce2009-10-23 18:54:35 +0000840 CXXScopeSpec SS;
841 NestedNameSpecifier *Qualifier = 0;
842 if (E->getQualifier()) {
843 Qualifier = TransformNestedNameSpecifier(E->getQualifier(),
844 E->getQualifierRange());
845 if (!Qualifier)
846 return SemaRef.ExprError();
847
848 SS.setScopeRep(Qualifier);
849 SS.setRange(E->getQualifierRange());
850 }
851
John McCallba135432009-11-21 08:51:07 +0000852 return SemaRef.BuildDeclarationNameExpr(&SS, E->getLocation(), InstD);
Douglas Gregorb98b1992009-08-11 05:31:07 +0000853}
854
Sebastian Redla29e51b2009-11-08 13:56:19 +0000855Sema::OwningExprResult TemplateInstantiator::TransformCXXDefaultArgExpr(
856 CXXDefaultArgExpr *E, bool isAddressOfOperand) {
857 assert(!cast<FunctionDecl>(E->getParam()->getDeclContext())->
858 getDescribedFunctionTemplate() &&
859 "Default arg expressions are never formed in dependent cases.");
860 return SemaRef.Owned(E->Retain());
861}
862
863
Mike Stump1eb44332009-09-09 15:08:12 +0000864QualType
John McCalla2becad2009-10-21 00:40:46 +0000865TemplateInstantiator::TransformTemplateTypeParmType(TypeLocBuilder &TLB,
866 TemplateTypeParmTypeLoc TL) {
867 TemplateTypeParmType *T = TL.getTypePtr();
Douglas Gregord6350ae2009-08-28 20:31:08 +0000868 if (T->getDepth() < TemplateArgs.getNumLevels()) {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000869 // Replace the template type parameter with its corresponding
870 // template argument.
Mike Stump1eb44332009-09-09 15:08:12 +0000871
872 // If the corresponding template argument is NULL or doesn't exist, it's
873 // because we are performing instantiation from explicitly-specified
874 // template arguments in a function template class, but there were some
Douglas Gregor16134c62009-07-01 00:28:38 +0000875 // arguments left unspecified.
John McCalla2becad2009-10-21 00:40:46 +0000876 if (!TemplateArgs.hasTemplateArgument(T->getDepth(), T->getIndex())) {
877 TemplateTypeParmTypeLoc NewTL
878 = TLB.push<TemplateTypeParmTypeLoc>(TL.getType());
879 NewTL.setNameLoc(TL.getNameLoc());
880 return TL.getType();
881 }
Mike Stump1eb44332009-09-09 15:08:12 +0000882
883 assert(TemplateArgs(T->getDepth(), T->getIndex()).getKind()
Douglas Gregord6350ae2009-08-28 20:31:08 +0000884 == TemplateArgument::Type &&
Douglas Gregor99ebf652009-02-27 19:31:52 +0000885 "Template argument kind mismatch");
Douglas Gregord6350ae2009-08-28 20:31:08 +0000886
John McCall49a832b2009-10-18 09:09:24 +0000887 QualType Replacement
888 = TemplateArgs(T->getDepth(), T->getIndex()).getAsType();
889
890 // TODO: only do this uniquing once, at the start of instantiation.
John McCalla2becad2009-10-21 00:40:46 +0000891 QualType Result
892 = getSema().Context.getSubstTemplateTypeParmType(T, Replacement);
893 SubstTemplateTypeParmTypeLoc NewTL
894 = TLB.push<SubstTemplateTypeParmTypeLoc>(Result);
895 NewTL.setNameLoc(TL.getNameLoc());
896 return Result;
Mike Stump1eb44332009-09-09 15:08:12 +0000897 }
Douglas Gregor99ebf652009-02-27 19:31:52 +0000898
899 // The template type parameter comes from an inner template (e.g.,
900 // the template parameter list of a member template inside the
901 // template we are instantiating). Create a new template type
902 // parameter with the template "level" reduced by one.
John McCalla2becad2009-10-21 00:40:46 +0000903 QualType Result
904 = getSema().Context.getTemplateTypeParmType(T->getDepth()
905 - TemplateArgs.getNumLevels(),
906 T->getIndex(),
907 T->isParameterPack(),
908 T->getName());
909 TemplateTypeParmTypeLoc NewTL = TLB.push<TemplateTypeParmTypeLoc>(Result);
910 NewTL.setNameLoc(TL.getNameLoc());
911 return Result;
Douglas Gregorcd281c32009-02-28 00:25:32 +0000912}
Douglas Gregor99ebf652009-02-27 19:31:52 +0000913
John McCallce3ff2b2009-08-25 22:02:44 +0000914/// \brief Perform substitution on the type T with a given set of template
915/// arguments.
Douglas Gregor99ebf652009-02-27 19:31:52 +0000916///
917/// This routine substitutes the given template arguments into the
918/// type T and produces the instantiated type.
919///
920/// \param T the type into which the template arguments will be
921/// substituted. If this type is not dependent, it will be returned
922/// immediately.
923///
924/// \param TemplateArgs the template arguments that will be
925/// substituted for the top-level template parameters within T.
926///
Douglas Gregor99ebf652009-02-27 19:31:52 +0000927/// \param Loc the location in the source code where this substitution
928/// is being performed. It will typically be the location of the
929/// declarator (if we're instantiating the type of some declaration)
930/// or the location of the type in the source code (if, e.g., we're
931/// instantiating the type of a cast expression).
932///
933/// \param Entity the name of the entity associated with a declaration
934/// being instantiated (if any). May be empty to indicate that there
935/// is no such entity (if, e.g., this is a type that occurs as part of
936/// a cast expression) or that the entity has no name (e.g., an
937/// unnamed function parameter).
938///
939/// \returns If the instantiation succeeds, the instantiated
940/// type. Otherwise, produces diagnostics and returns a NULL type.
John McCallcd7ba1c2009-10-21 00:58:09 +0000941DeclaratorInfo *Sema::SubstType(DeclaratorInfo *T,
942 const MultiLevelTemplateArgumentList &Args,
943 SourceLocation Loc,
944 DeclarationName Entity) {
945 assert(!ActiveTemplateInstantiations.empty() &&
946 "Cannot perform an instantiation without some context on the "
947 "instantiation stack");
948
949 if (!T->getType()->isDependentType())
950 return T;
951
952 TemplateInstantiator Instantiator(*this, Args, Loc, Entity);
953 return Instantiator.TransformType(T);
954}
955
956/// Deprecated form of the above.
Mike Stump1eb44332009-09-09 15:08:12 +0000957QualType Sema::SubstType(QualType T,
Douglas Gregord6350ae2009-08-28 20:31:08 +0000958 const MultiLevelTemplateArgumentList &TemplateArgs,
John McCallce3ff2b2009-08-25 22:02:44 +0000959 SourceLocation Loc, DeclarationName Entity) {
Douglas Gregordf667e72009-03-10 20:44:00 +0000960 assert(!ActiveTemplateInstantiations.empty() &&
961 "Cannot perform an instantiation without some context on the "
962 "instantiation stack");
963
Douglas Gregor99ebf652009-02-27 19:31:52 +0000964 // If T is not a dependent type, there is nothing to do.
965 if (!T->isDependentType())
966 return T;
967
Douglas Gregor577f75a2009-08-04 16:50:30 +0000968 TemplateInstantiator Instantiator(*this, TemplateArgs, Loc, Entity);
969 return Instantiator.TransformType(T);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000970}
Douglas Gregor2943aed2009-03-03 04:44:36 +0000971
John McCallce3ff2b2009-08-25 22:02:44 +0000972/// \brief Perform substitution on the base class specifiers of the
973/// given class template specialization.
Douglas Gregor2943aed2009-03-03 04:44:36 +0000974///
975/// Produces a diagnostic and returns true on error, returns false and
976/// attaches the instantiated base classes to the class template
977/// specialization if successful.
Mike Stump1eb44332009-09-09 15:08:12 +0000978bool
John McCallce3ff2b2009-08-25 22:02:44 +0000979Sema::SubstBaseSpecifiers(CXXRecordDecl *Instantiation,
980 CXXRecordDecl *Pattern,
Douglas Gregord6350ae2009-08-28 20:31:08 +0000981 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor2943aed2009-03-03 04:44:36 +0000982 bool Invalid = false;
Douglas Gregor0ca20ac2009-05-29 18:27:38 +0000983 llvm::SmallVector<CXXBaseSpecifier*, 4> InstantiatedBases;
Mike Stump1eb44332009-09-09 15:08:12 +0000984 for (ClassTemplateSpecializationDecl::base_class_iterator
Douglas Gregord475b8d2009-03-25 21:17:03 +0000985 Base = Pattern->bases_begin(), BaseEnd = Pattern->bases_end();
Douglas Gregor27b152f2009-03-10 18:52:44 +0000986 Base != BaseEnd; ++Base) {
Douglas Gregor2943aed2009-03-03 04:44:36 +0000987 if (!Base->getType()->isDependentType()) {
Fariborz Jahanian71c6e712009-07-22 17:41:53 +0000988 InstantiatedBases.push_back(new (Context) CXXBaseSpecifier(*Base));
Douglas Gregor2943aed2009-03-03 04:44:36 +0000989 continue;
990 }
991
Mike Stump1eb44332009-09-09 15:08:12 +0000992 QualType BaseType = SubstType(Base->getType(),
993 TemplateArgs,
John McCallce3ff2b2009-08-25 22:02:44 +0000994 Base->getSourceRange().getBegin(),
995 DeclarationName());
Douglas Gregor2943aed2009-03-03 04:44:36 +0000996 if (BaseType.isNull()) {
997 Invalid = true;
998 continue;
999 }
1000
1001 if (CXXBaseSpecifier *InstantiatedBase
Douglas Gregord475b8d2009-03-25 21:17:03 +00001002 = CheckBaseSpecifier(Instantiation,
Douglas Gregor2943aed2009-03-03 04:44:36 +00001003 Base->getSourceRange(),
1004 Base->isVirtual(),
1005 Base->getAccessSpecifierAsWritten(),
1006 BaseType,
1007 /*FIXME: Not totally accurate */
1008 Base->getSourceRange().getBegin()))
1009 InstantiatedBases.push_back(InstantiatedBase);
1010 else
1011 Invalid = true;
1012 }
1013
Douglas Gregor27b152f2009-03-10 18:52:44 +00001014 if (!Invalid &&
Jay Foadbeaaccd2009-05-21 09:52:38 +00001015 AttachBaseSpecifiers(Instantiation, InstantiatedBases.data(),
Douglas Gregor2943aed2009-03-03 04:44:36 +00001016 InstantiatedBases.size()))
1017 Invalid = true;
1018
1019 return Invalid;
1020}
1021
Douglas Gregord475b8d2009-03-25 21:17:03 +00001022/// \brief Instantiate the definition of a class from a given pattern.
1023///
1024/// \param PointOfInstantiation The point of instantiation within the
1025/// source code.
1026///
1027/// \param Instantiation is the declaration whose definition is being
1028/// instantiated. This will be either a class template specialization
1029/// or a member class of a class template specialization.
1030///
1031/// \param Pattern is the pattern from which the instantiation
1032/// occurs. This will be either the declaration of a class template or
1033/// the declaration of a member class of a class template.
1034///
1035/// \param TemplateArgs The template arguments to be substituted into
1036/// the pattern.
1037///
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001038/// \param TSK the kind of implicit or explicit instantiation to perform.
Douglas Gregor5842ba92009-08-24 15:23:48 +00001039///
1040/// \param Complain whether to complain if the class cannot be instantiated due
1041/// to the lack of a definition.
1042///
Douglas Gregord475b8d2009-03-25 21:17:03 +00001043/// \returns true if an error occurred, false otherwise.
1044bool
1045Sema::InstantiateClass(SourceLocation PointOfInstantiation,
1046 CXXRecordDecl *Instantiation, CXXRecordDecl *Pattern,
Douglas Gregord6350ae2009-08-28 20:31:08 +00001047 const MultiLevelTemplateArgumentList &TemplateArgs,
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001048 TemplateSpecializationKind TSK,
Douglas Gregor5842ba92009-08-24 15:23:48 +00001049 bool Complain) {
Douglas Gregord475b8d2009-03-25 21:17:03 +00001050 bool Invalid = false;
John McCalle29ba202009-08-20 01:44:21 +00001051
Mike Stump1eb44332009-09-09 15:08:12 +00001052 CXXRecordDecl *PatternDef
Douglas Gregord475b8d2009-03-25 21:17:03 +00001053 = cast_or_null<CXXRecordDecl>(Pattern->getDefinition(Context));
1054 if (!PatternDef) {
Douglas Gregor5842ba92009-08-24 15:23:48 +00001055 if (!Complain) {
1056 // Say nothing
1057 } else if (Pattern == Instantiation->getInstantiatedFromMemberClass()) {
Douglas Gregord475b8d2009-03-25 21:17:03 +00001058 Diag(PointOfInstantiation,
1059 diag::err_implicit_instantiate_member_undefined)
1060 << Context.getTypeDeclType(Instantiation);
1061 Diag(Pattern->getLocation(), diag::note_member_of_template_here);
1062 } else {
Douglas Gregor93dfdb12009-05-13 00:25:59 +00001063 Diag(PointOfInstantiation, diag::err_template_instantiate_undefined)
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001064 << (TSK != TSK_ImplicitInstantiation)
Douglas Gregord475b8d2009-03-25 21:17:03 +00001065 << Context.getTypeDeclType(Instantiation);
1066 Diag(Pattern->getLocation(), diag::note_template_decl_here);
1067 }
1068 return true;
1069 }
1070 Pattern = PatternDef;
1071
Douglas Gregor454885e2009-10-15 15:54:05 +00001072 // \brief Record the point of instantiation.
1073 if (MemberSpecializationInfo *MSInfo
1074 = Instantiation->getMemberSpecializationInfo()) {
1075 MSInfo->setTemplateSpecializationKind(TSK);
1076 MSInfo->setPointOfInstantiation(PointOfInstantiation);
Douglas Gregor972e6ce2009-10-27 06:26:26 +00001077 } else if (ClassTemplateSpecializationDecl *Spec
1078 = dyn_cast<ClassTemplateSpecializationDecl>(Instantiation)) {
1079 Spec->setTemplateSpecializationKind(TSK);
1080 Spec->setPointOfInstantiation(PointOfInstantiation);
Douglas Gregor454885e2009-10-15 15:54:05 +00001081 }
1082
Douglas Gregord048bb72009-03-25 21:23:52 +00001083 InstantiatingTemplate Inst(*this, PointOfInstantiation, Instantiation);
Douglas Gregord475b8d2009-03-25 21:17:03 +00001084 if (Inst)
1085 return true;
1086
1087 // Enter the scope of this instantiation. We don't use
1088 // PushDeclContext because we don't have a scope.
1089 DeclContext *PreviousContext = CurContext;
1090 CurContext = Instantiation;
1091
1092 // Start the definition of this instantiation.
1093 Instantiation->startDefinition();
1094
John McCallce3ff2b2009-08-25 22:02:44 +00001095 // Do substitution on the base class specifiers.
1096 if (SubstBaseSpecifiers(Instantiation, Pattern, TemplateArgs))
Douglas Gregord475b8d2009-03-25 21:17:03 +00001097 Invalid = true;
1098
Douglas Gregor0ca20ac2009-05-29 18:27:38 +00001099 llvm::SmallVector<DeclPtrTy, 4> Fields;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001100 for (RecordDecl::decl_iterator Member = Pattern->decls_begin(),
Mike Stump1eb44332009-09-09 15:08:12 +00001101 MemberEnd = Pattern->decls_end();
Douglas Gregor6ab35242009-04-09 21:40:53 +00001102 Member != MemberEnd; ++Member) {
John McCallce3ff2b2009-08-25 22:02:44 +00001103 Decl *NewMember = SubstDecl(*Member, Instantiation, TemplateArgs);
Douglas Gregord475b8d2009-03-25 21:17:03 +00001104 if (NewMember) {
Douglas Gregor9148c3f2009-11-11 19:13:48 +00001105 if (NewMember->isInvalidDecl()) {
Douglas Gregord475b8d2009-03-25 21:17:03 +00001106 Invalid = true;
Douglas Gregor9148c3f2009-11-11 19:13:48 +00001107 } else if (FieldDecl *Field = dyn_cast<FieldDecl>(NewMember))
Chris Lattnerb28317a2009-03-28 19:18:32 +00001108 Fields.push_back(DeclPtrTy::make(Field));
Anders Carlsson0d8df782009-08-29 19:37:28 +00001109 else if (UsingDecl *UD = dyn_cast<UsingDecl>(NewMember))
1110 Instantiation->addDecl(UD);
Douglas Gregord475b8d2009-03-25 21:17:03 +00001111 } else {
1112 // FIXME: Eventually, a NULL return will mean that one of the
Mike Stump390b4cc2009-05-16 07:39:55 +00001113 // instantiations was a semantic disaster, and we'll want to set Invalid =
1114 // true. For now, we expect to skip some members that we can't yet handle.
Douglas Gregord475b8d2009-03-25 21:17:03 +00001115 }
1116 }
1117
1118 // Finish checking fields.
Chris Lattnerb28317a2009-03-28 19:18:32 +00001119 ActOnFields(0, Instantiation->getLocation(), DeclPtrTy::make(Instantiation),
Jay Foadbeaaccd2009-05-21 09:52:38 +00001120 Fields.data(), Fields.size(), SourceLocation(), SourceLocation(),
Douglas Gregord475b8d2009-03-25 21:17:03 +00001121 0);
Douglas Gregor663b5a02009-10-14 20:14:33 +00001122 if (Instantiation->isInvalidDecl())
1123 Invalid = true;
1124
Douglas Gregord475b8d2009-03-25 21:17:03 +00001125 // Add any implicitly-declared members that we might need.
Douglas Gregor663b5a02009-10-14 20:14:33 +00001126 if (!Invalid)
1127 AddImplicitlyDeclaredMembersToClass(Instantiation);
Douglas Gregord475b8d2009-03-25 21:17:03 +00001128
1129 // Exit the scope of this instantiation.
1130 CurContext = PreviousContext;
1131
Douglas Gregoraba43bb2009-05-26 20:50:29 +00001132 if (!Invalid)
1133 Consumer.HandleTagDeclDefinition(Instantiation);
1134
Douglas Gregord475b8d2009-03-25 21:17:03 +00001135 return Invalid;
1136}
1137
Mike Stump1eb44332009-09-09 15:08:12 +00001138bool
Douglas Gregor2943aed2009-03-03 04:44:36 +00001139Sema::InstantiateClassTemplateSpecialization(
Douglas Gregor972e6ce2009-10-27 06:26:26 +00001140 SourceLocation PointOfInstantiation,
Douglas Gregor2943aed2009-03-03 04:44:36 +00001141 ClassTemplateSpecializationDecl *ClassTemplateSpec,
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001142 TemplateSpecializationKind TSK,
Douglas Gregor5842ba92009-08-24 15:23:48 +00001143 bool Complain) {
Douglas Gregor2943aed2009-03-03 04:44:36 +00001144 // Perform the actual instantiation on the canonical declaration.
1145 ClassTemplateSpec = cast<ClassTemplateSpecializationDecl>(
Argyrios Kyrtzidis97fbaa22009-07-18 00:34:25 +00001146 ClassTemplateSpec->getCanonicalDecl());
Douglas Gregor2943aed2009-03-03 04:44:36 +00001147
Douglas Gregor52604ab2009-09-11 21:19:12 +00001148 // Check whether we have already instantiated or specialized this class
1149 // template specialization.
1150 if (ClassTemplateSpec->getSpecializationKind() != TSK_Undeclared) {
1151 if (ClassTemplateSpec->getSpecializationKind() ==
1152 TSK_ExplicitInstantiationDeclaration &&
1153 TSK == TSK_ExplicitInstantiationDefinition) {
1154 // An explicit instantiation definition follows an explicit instantiation
1155 // declaration (C++0x [temp.explicit]p10); go ahead and perform the
1156 // explicit instantiation.
1157 ClassTemplateSpec->setSpecializationKind(TSK);
Douglas Gregor52604ab2009-09-11 21:19:12 +00001158 return false;
1159 }
1160
1161 // We can only instantiate something that hasn't already been
1162 // instantiated or specialized. Fail without any diagnostics: our
1163 // caller will provide an error message.
Douglas Gregor2943aed2009-03-03 04:44:36 +00001164 return true;
Douglas Gregor52604ab2009-09-11 21:19:12 +00001165 }
Douglas Gregor2943aed2009-03-03 04:44:36 +00001166
Douglas Gregor9eea08b2009-09-15 16:51:42 +00001167 if (ClassTemplateSpec->isInvalidDecl())
1168 return true;
1169
Douglas Gregor2943aed2009-03-03 04:44:36 +00001170 ClassTemplateDecl *Template = ClassTemplateSpec->getSpecializedTemplate();
Douglas Gregord6350ae2009-08-28 20:31:08 +00001171 CXXRecordDecl *Pattern = 0;
Douglas Gregorc8ab2562009-05-31 09:31:02 +00001172
Douglas Gregorc1efb3f2009-06-12 22:31:52 +00001173 // C++ [temp.class.spec.match]p1:
1174 // When a class template is used in a context that requires an
1175 // instantiation of the class, it is necessary to determine
1176 // whether the instantiation is to be generated using the primary
1177 // template or one of the partial specializations. This is done by
1178 // matching the template arguments of the class template
1179 // specialization with the template argument lists of the partial
1180 // specializations.
Douglas Gregor199d9912009-06-05 00:53:49 +00001181 typedef std::pair<ClassTemplatePartialSpecializationDecl *,
1182 TemplateArgumentList *> MatchResult;
1183 llvm::SmallVector<MatchResult, 4> Matched;
Mike Stump1eb44332009-09-09 15:08:12 +00001184 for (llvm::FoldingSet<ClassTemplatePartialSpecializationDecl>::iterator
Douglas Gregorc8ab2562009-05-31 09:31:02 +00001185 Partial = Template->getPartialSpecializations().begin(),
1186 PartialEnd = Template->getPartialSpecializations().end();
1187 Partial != PartialEnd;
1188 ++Partial) {
Douglas Gregorf67875d2009-06-12 18:26:56 +00001189 TemplateDeductionInfo Info(Context);
1190 if (TemplateDeductionResult Result
Mike Stump1eb44332009-09-09 15:08:12 +00001191 = DeduceTemplateArguments(&*Partial,
Douglas Gregorf67875d2009-06-12 18:26:56 +00001192 ClassTemplateSpec->getTemplateArgs(),
1193 Info)) {
1194 // FIXME: Store the failed-deduction information for use in
1195 // diagnostics, later.
1196 (void)Result;
1197 } else {
1198 Matched.push_back(std::make_pair(&*Partial, Info.take()));
1199 }
Douglas Gregorc8ab2562009-05-31 09:31:02 +00001200 }
1201
Douglas Gregored9c0f92009-10-29 00:04:11 +00001202 if (Matched.size() >= 1) {
Douglas Gregorbf4ea562009-09-15 16:23:51 +00001203 llvm::SmallVector<MatchResult, 4>::iterator Best = Matched.begin();
Douglas Gregored9c0f92009-10-29 00:04:11 +00001204 if (Matched.size() == 1) {
1205 // -- If exactly one matching specialization is found, the
1206 // instantiation is generated from that specialization.
1207 // We don't need to do anything for this.
1208 } else {
1209 // -- If more than one matching specialization is found, the
1210 // partial order rules (14.5.4.2) are used to determine
1211 // whether one of the specializations is more specialized
1212 // than the others. If none of the specializations is more
1213 // specialized than all of the other matching
1214 // specializations, then the use of the class template is
1215 // ambiguous and the program is ill-formed.
1216 for (llvm::SmallVector<MatchResult, 4>::iterator P = Best + 1,
1217 PEnd = Matched.end();
1218 P != PEnd; ++P) {
1219 if (getMoreSpecializedPartialSpecialization(P->first, Best->first)
1220 == P->first)
1221 Best = P;
Douglas Gregorbf4ea562009-09-15 16:23:51 +00001222 }
Douglas Gregorbf4ea562009-09-15 16:23:51 +00001223
Douglas Gregored9c0f92009-10-29 00:04:11 +00001224 // Determine if the best partial specialization is more specialized than
1225 // the others.
1226 bool Ambiguous = false;
Douglas Gregorbf4ea562009-09-15 16:23:51 +00001227 for (llvm::SmallVector<MatchResult, 4>::iterator P = Matched.begin(),
1228 PEnd = Matched.end();
Douglas Gregored9c0f92009-10-29 00:04:11 +00001229 P != PEnd; ++P) {
1230 if (P != Best &&
1231 getMoreSpecializedPartialSpecialization(P->first, Best->first)
1232 != Best->first) {
1233 Ambiguous = true;
1234 break;
1235 }
1236 }
1237
1238 if (Ambiguous) {
1239 // Partial ordering did not produce a clear winner. Complain.
1240 ClassTemplateSpec->setInvalidDecl();
1241 Diag(PointOfInstantiation, diag::err_partial_spec_ordering_ambiguous)
1242 << ClassTemplateSpec;
1243
1244 // Print the matching partial specializations.
1245 for (llvm::SmallVector<MatchResult, 4>::iterator P = Matched.begin(),
1246 PEnd = Matched.end();
1247 P != PEnd; ++P)
1248 Diag(P->first->getLocation(), diag::note_partial_spec_match)
1249 << getTemplateArgumentBindingsText(P->first->getTemplateParameters(),
1250 *P->second);
Douglas Gregord6350ae2009-08-28 20:31:08 +00001251
Douglas Gregored9c0f92009-10-29 00:04:11 +00001252 return true;
1253 }
Douglas Gregorbf4ea562009-09-15 16:23:51 +00001254 }
1255
1256 // Instantiate using the best class template partial specialization.
Douglas Gregored9c0f92009-10-29 00:04:11 +00001257 ClassTemplatePartialSpecializationDecl *OrigPartialSpec = Best->first;
1258 while (OrigPartialSpec->getInstantiatedFromMember()) {
1259 // If we've found an explicit specialization of this class template,
1260 // stop here and use that as the pattern.
1261 if (OrigPartialSpec->isMemberSpecialization())
1262 break;
1263
1264 OrigPartialSpec = OrigPartialSpec->getInstantiatedFromMember();
1265 }
1266
1267 Pattern = OrigPartialSpec;
Douglas Gregorbf4ea562009-09-15 16:23:51 +00001268 ClassTemplateSpec->setInstantiationOf(Best->first, Best->second);
Douglas Gregorc1efb3f2009-06-12 22:31:52 +00001269 } else {
1270 // -- If no matches are found, the instantiation is generated
1271 // from the primary template.
Douglas Gregord6350ae2009-08-28 20:31:08 +00001272 ClassTemplateDecl *OrigTemplate = Template;
Douglas Gregorfd056bc2009-10-13 16:30:37 +00001273 while (OrigTemplate->getInstantiatedFromMemberTemplate()) {
1274 // If we've found an explicit specialization of this class template,
1275 // stop here and use that as the pattern.
1276 if (OrigTemplate->isMemberSpecialization())
1277 break;
1278
Douglas Gregord6350ae2009-08-28 20:31:08 +00001279 OrigTemplate = OrigTemplate->getInstantiatedFromMemberTemplate();
Douglas Gregorfd056bc2009-10-13 16:30:37 +00001280 }
1281
Douglas Gregord6350ae2009-08-28 20:31:08 +00001282 Pattern = OrigTemplate->getTemplatedDecl();
Douglas Gregorc8ab2562009-05-31 09:31:02 +00001283 }
Douglas Gregor2943aed2009-03-03 04:44:36 +00001284
Douglas Gregor972e6ce2009-10-27 06:26:26 +00001285 bool Result = InstantiateClass(PointOfInstantiation, ClassTemplateSpec,
1286 Pattern,
1287 getTemplateInstantiationArgs(ClassTemplateSpec),
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001288 TSK,
Douglas Gregor5842ba92009-08-24 15:23:48 +00001289 Complain);
Mike Stump1eb44332009-09-09 15:08:12 +00001290
Douglas Gregor199d9912009-06-05 00:53:49 +00001291 for (unsigned I = 0, N = Matched.size(); I != N; ++I) {
1292 // FIXME: Implement TemplateArgumentList::Destroy!
1293 // if (Matched[I].first != Pattern)
1294 // Matched[I].second->Destroy(Context);
1295 }
Mike Stump1eb44332009-09-09 15:08:12 +00001296
Douglas Gregor199d9912009-06-05 00:53:49 +00001297 return Result;
Douglas Gregor2943aed2009-03-03 04:44:36 +00001298}
Douglas Gregor5953d8b2009-03-19 17:26:29 +00001299
John McCallce3ff2b2009-08-25 22:02:44 +00001300/// \brief Instantiates the definitions of all of the member
1301/// of the given class, which is an instantiation of a class template
1302/// or a member class of a template.
Douglas Gregora58861f2009-05-13 20:28:22 +00001303void
1304Sema::InstantiateClassMembers(SourceLocation PointOfInstantiation,
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001305 CXXRecordDecl *Instantiation,
1306 const MultiLevelTemplateArgumentList &TemplateArgs,
1307 TemplateSpecializationKind TSK) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001308 for (DeclContext::decl_iterator D = Instantiation->decls_begin(),
1309 DEnd = Instantiation->decls_end();
Douglas Gregora58861f2009-05-13 20:28:22 +00001310 D != DEnd; ++D) {
Douglas Gregor0d035142009-10-27 18:42:08 +00001311 bool SuppressNew = false;
Douglas Gregora58861f2009-05-13 20:28:22 +00001312 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(*D)) {
Douglas Gregor0d035142009-10-27 18:42:08 +00001313 if (FunctionDecl *Pattern
1314 = Function->getInstantiatedFromMemberFunction()) {
1315 MemberSpecializationInfo *MSInfo
1316 = Function->getMemberSpecializationInfo();
1317 assert(MSInfo && "No member specialization information?");
1318 if (CheckSpecializationInstantiationRedecl(PointOfInstantiation, TSK,
1319 Function,
1320 MSInfo->getTemplateSpecializationKind(),
1321 MSInfo->getPointOfInstantiation(),
1322 SuppressNew) ||
1323 SuppressNew)
Douglas Gregorf6b11852009-10-08 15:14:33 +00001324 continue;
1325
Douglas Gregor0d035142009-10-27 18:42:08 +00001326 if (Function->getBody())
1327 continue;
1328
1329 if (TSK == TSK_ExplicitInstantiationDefinition) {
1330 // C++0x [temp.explicit]p8:
1331 // An explicit instantiation definition that names a class template
1332 // specialization explicitly instantiates the class template
1333 // specialization and is only an explicit instantiation definition
1334 // of members whose definition is visible at the point of
1335 // instantiation.
1336 if (!Pattern->getBody())
1337 continue;
1338
1339 Function->setTemplateSpecializationKind(TSK, PointOfInstantiation);
1340
1341 InstantiateFunctionDefinition(PointOfInstantiation, Function);
1342 } else {
1343 Function->setTemplateSpecializationKind(TSK, PointOfInstantiation);
1344 }
Douglas Gregorf6b11852009-10-08 15:14:33 +00001345 }
Douglas Gregora58861f2009-05-13 20:28:22 +00001346 } else if (VarDecl *Var = dyn_cast<VarDecl>(*D)) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001347 if (Var->isStaticDataMember()) {
Douglas Gregor0d035142009-10-27 18:42:08 +00001348 MemberSpecializationInfo *MSInfo = Var->getMemberSpecializationInfo();
1349 assert(MSInfo && "No member specialization information?");
1350 if (CheckSpecializationInstantiationRedecl(PointOfInstantiation, TSK,
1351 Var,
1352 MSInfo->getTemplateSpecializationKind(),
1353 MSInfo->getPointOfInstantiation(),
1354 SuppressNew) ||
1355 SuppressNew)
Douglas Gregorf6b11852009-10-08 15:14:33 +00001356 continue;
1357
Douglas Gregor0d035142009-10-27 18:42:08 +00001358 if (TSK == TSK_ExplicitInstantiationDefinition) {
1359 // C++0x [temp.explicit]p8:
1360 // An explicit instantiation definition that names a class template
1361 // specialization explicitly instantiates the class template
1362 // specialization and is only an explicit instantiation definition
1363 // of members whose definition is visible at the point of
1364 // instantiation.
1365 if (!Var->getInstantiatedFromStaticDataMember()
1366 ->getOutOfLineDefinition())
1367 continue;
1368
1369 Var->setTemplateSpecializationKind(TSK, PointOfInstantiation);
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001370 InstantiateStaticDataMemberDefinition(PointOfInstantiation, Var);
Douglas Gregor0d035142009-10-27 18:42:08 +00001371 } else {
1372 Var->setTemplateSpecializationKind(TSK, PointOfInstantiation);
1373 }
1374 }
Douglas Gregora58861f2009-05-13 20:28:22 +00001375 } else if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(*D)) {
Douglas Gregor2db32322009-10-07 23:56:10 +00001376 if (Record->isInjectedClassName())
1377 continue;
1378
Douglas Gregor0d035142009-10-27 18:42:08 +00001379 MemberSpecializationInfo *MSInfo = Record->getMemberSpecializationInfo();
1380 assert(MSInfo && "No member specialization information?");
1381 if (CheckSpecializationInstantiationRedecl(PointOfInstantiation, TSK,
1382 Record,
1383 MSInfo->getTemplateSpecializationKind(),
1384 MSInfo->getPointOfInstantiation(),
1385 SuppressNew) ||
1386 SuppressNew)
Douglas Gregorf6b11852009-10-08 15:14:33 +00001387 continue;
1388
Douglas Gregor0d035142009-10-27 18:42:08 +00001389 CXXRecordDecl *Pattern = Record->getInstantiatedFromMemberClass();
1390 assert(Pattern && "Missing instantiated-from-template information");
1391
1392 if (!Record->getDefinition(Context)) {
1393 if (!Pattern->getDefinition(Context)) {
1394 // C++0x [temp.explicit]p8:
1395 // An explicit instantiation definition that names a class template
1396 // specialization explicitly instantiates the class template
1397 // specialization and is only an explicit instantiation definition
1398 // of members whose definition is visible at the point of
1399 // instantiation.
1400 if (TSK == TSK_ExplicitInstantiationDeclaration) {
1401 MSInfo->setTemplateSpecializationKind(TSK);
1402 MSInfo->setPointOfInstantiation(PointOfInstantiation);
1403 }
1404
1405 continue;
1406 }
1407
1408 InstantiateClass(PointOfInstantiation, Record, Pattern,
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001409 TemplateArgs,
1410 TSK);
Douglas Gregor0d035142009-10-27 18:42:08 +00001411 }
Douglas Gregore9374d52009-10-08 01:19:17 +00001412
Douglas Gregor0d035142009-10-27 18:42:08 +00001413 Pattern = cast_or_null<CXXRecordDecl>(Record->getDefinition(Context));
1414 if (Pattern)
1415 InstantiateClassMembers(PointOfInstantiation, Pattern, TemplateArgs,
1416 TSK);
Douglas Gregora58861f2009-05-13 20:28:22 +00001417 }
1418 }
1419}
1420
1421/// \brief Instantiate the definitions of all of the members of the
1422/// given class template specialization, which was named as part of an
1423/// explicit instantiation.
Mike Stump1eb44332009-09-09 15:08:12 +00001424void
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001425Sema::InstantiateClassTemplateSpecializationMembers(
Douglas Gregora58861f2009-05-13 20:28:22 +00001426 SourceLocation PointOfInstantiation,
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001427 ClassTemplateSpecializationDecl *ClassTemplateSpec,
1428 TemplateSpecializationKind TSK) {
Douglas Gregora58861f2009-05-13 20:28:22 +00001429 // C++0x [temp.explicit]p7:
1430 // An explicit instantiation that names a class template
1431 // specialization is an explicit instantion of the same kind
1432 // (declaration or definition) of each of its members (not
1433 // including members inherited from base classes) that has not
1434 // been previously explicitly specialized in the translation unit
1435 // containing the explicit instantiation, except as described
1436 // below.
1437 InstantiateClassMembers(PointOfInstantiation, ClassTemplateSpec,
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001438 getTemplateInstantiationArgs(ClassTemplateSpec),
1439 TSK);
Douglas Gregora58861f2009-05-13 20:28:22 +00001440}
1441
Mike Stump1eb44332009-09-09 15:08:12 +00001442Sema::OwningStmtResult
Douglas Gregord6350ae2009-08-28 20:31:08 +00001443Sema::SubstStmt(Stmt *S, const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor43959a92009-08-20 07:17:43 +00001444 if (!S)
1445 return Owned(S);
1446
1447 TemplateInstantiator Instantiator(*this, TemplateArgs,
1448 SourceLocation(),
1449 DeclarationName());
1450 return Instantiator.TransformStmt(S);
1451}
1452
Mike Stump1eb44332009-09-09 15:08:12 +00001453Sema::OwningExprResult
Douglas Gregord6350ae2009-08-28 20:31:08 +00001454Sema::SubstExpr(Expr *E, const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregorb98b1992009-08-11 05:31:07 +00001455 if (!E)
1456 return Owned(E);
Mike Stump1eb44332009-09-09 15:08:12 +00001457
Douglas Gregorb98b1992009-08-11 05:31:07 +00001458 TemplateInstantiator Instantiator(*this, TemplateArgs,
1459 SourceLocation(),
1460 DeclarationName());
1461 return Instantiator.TransformExpr(E);
1462}
1463
John McCallce3ff2b2009-08-25 22:02:44 +00001464/// \brief Do template substitution on a nested-name-specifier.
Douglas Gregorab452ba2009-03-26 23:50:42 +00001465NestedNameSpecifier *
John McCallce3ff2b2009-08-25 22:02:44 +00001466Sema::SubstNestedNameSpecifier(NestedNameSpecifier *NNS,
Douglas Gregord6350ae2009-08-28 20:31:08 +00001467 SourceRange Range,
1468 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregordcee1a12009-08-06 05:28:30 +00001469 TemplateInstantiator Instantiator(*this, TemplateArgs, Range.getBegin(),
1470 DeclarationName());
1471 return Instantiator.TransformNestedNameSpecifier(NNS, Range);
Douglas Gregor5953d8b2009-03-19 17:26:29 +00001472}
Douglas Gregorde650ae2009-03-31 18:38:02 +00001473
1474TemplateName
John McCallce3ff2b2009-08-25 22:02:44 +00001475Sema::SubstTemplateName(TemplateName Name, SourceLocation Loc,
Douglas Gregord6350ae2009-08-28 20:31:08 +00001476 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregord1067e52009-08-06 06:41:21 +00001477 TemplateInstantiator Instantiator(*this, TemplateArgs, Loc,
1478 DeclarationName());
1479 return Instantiator.TransformTemplateName(Name);
Douglas Gregorde650ae2009-03-31 18:38:02 +00001480}
Douglas Gregor91333002009-06-11 00:06:24 +00001481
John McCall833ca992009-10-29 08:12:44 +00001482bool Sema::Subst(const TemplateArgumentLoc &Input, TemplateArgumentLoc &Output,
1483 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor670444e2009-08-04 22:27:00 +00001484 TemplateInstantiator Instantiator(*this, TemplateArgs, SourceLocation(),
1485 DeclarationName());
John McCall833ca992009-10-29 08:12:44 +00001486
1487 return Instantiator.TransformTemplateArgument(Input, Output);
Douglas Gregor91333002009-06-11 00:06:24 +00001488}