blob: 58fe59e321cf96bf992192ff9c71d004435ee744 [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"
Douglas Gregoraba43bb2009-05-26 20:50:29 +000015#include "clang/AST/ASTConsumer.h"
Douglas Gregor99ebf652009-02-27 19:31:52 +000016#include "clang/AST/ASTContext.h"
17#include "clang/AST/Expr.h"
Douglas Gregor99ebf652009-02-27 19:31:52 +000018#include "clang/AST/DeclTemplate.h"
19#include "clang/Parse/DeclSpec.h"
20#include "clang/Basic/LangOptions.h"
Douglas Gregorcd281c32009-02-28 00:25:32 +000021#include "llvm/Support/Compiler.h"
Douglas Gregor99ebf652009-02-27 19:31:52 +000022
23using namespace clang;
24
Douglas Gregoree1828a2009-03-10 18:03:33 +000025//===----------------------------------------------------------------------===/
26// Template Instantiation Support
27//===----------------------------------------------------------------------===/
28
Douglas Gregord6350ae2009-08-28 20:31:08 +000029/// \brief Retrieve the template argument list(s) that should be used to
30/// instantiate the definition of the given declaration.
Douglas Gregor0f8716b2009-11-09 19:17:50 +000031///
32/// \param D the declaration for which we are computing template instantiation
33/// arguments.
34///
35/// \param Innermost if non-NULL, the innermost template argument list.
Douglas Gregord1102432009-08-28 17:37:35 +000036MultiLevelTemplateArgumentList
Douglas Gregor0f8716b2009-11-09 19:17:50 +000037Sema::getTemplateInstantiationArgs(NamedDecl *D,
38 const TemplateArgumentList *Innermost) {
Douglas Gregord1102432009-08-28 17:37:35 +000039 // Accumulate the set of template argument lists in this structure.
40 MultiLevelTemplateArgumentList Result;
Mike Stump1eb44332009-09-09 15:08:12 +000041
Douglas Gregor0f8716b2009-11-09 19:17:50 +000042 if (Innermost)
43 Result.addOuterTemplateArguments(Innermost);
44
Douglas Gregord1102432009-08-28 17:37:35 +000045 DeclContext *Ctx = dyn_cast<DeclContext>(D);
46 if (!Ctx)
47 Ctx = D->getDeclContext();
Mike Stump1eb44332009-09-09 15:08:12 +000048
John McCallf181d8a2009-08-29 03:16:09 +000049 while (!Ctx->isFileContext()) {
Douglas Gregord1102432009-08-28 17:37:35 +000050 // Add template arguments from a class template instantiation.
Mike Stump1eb44332009-09-09 15:08:12 +000051 if (ClassTemplateSpecializationDecl *Spec
Douglas Gregord1102432009-08-28 17:37:35 +000052 = dyn_cast<ClassTemplateSpecializationDecl>(Ctx)) {
53 // We're done when we hit an explicit specialization.
54 if (Spec->getSpecializationKind() == TSK_ExplicitSpecialization)
55 break;
Mike Stump1eb44332009-09-09 15:08:12 +000056
Douglas Gregord1102432009-08-28 17:37:35 +000057 Result.addOuterTemplateArguments(&Spec->getTemplateInstantiationArgs());
Douglas Gregorfd056bc2009-10-13 16:30:37 +000058
59 // If this class template specialization was instantiated from a
60 // specialized member that is a class template, we're done.
61 assert(Spec->getSpecializedTemplate() && "No class template?");
62 if (Spec->getSpecializedTemplate()->isMemberSpecialization())
63 break;
Mike Stump1eb44332009-09-09 15:08:12 +000064 }
Douglas Gregord1102432009-08-28 17:37:35 +000065 // Add template arguments from a function template specialization.
John McCallf181d8a2009-08-29 03:16:09 +000066 else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Ctx)) {
Douglas Gregorfd056bc2009-10-13 16:30:37 +000067 if (Function->getTemplateSpecializationKind()
68 == TSK_ExplicitSpecialization)
69 break;
70
Douglas Gregord1102432009-08-28 17:37:35 +000071 if (const TemplateArgumentList *TemplateArgs
Douglas Gregorfd056bc2009-10-13 16:30:37 +000072 = Function->getTemplateSpecializationArgs()) {
73 // Add the template arguments for this specialization.
Douglas Gregord1102432009-08-28 17:37:35 +000074 Result.addOuterTemplateArguments(TemplateArgs);
John McCallf181d8a2009-08-29 03:16:09 +000075
Douglas Gregorfd056bc2009-10-13 16:30:37 +000076 // If this function was instantiated from a specialized member that is
77 // a function template, we're done.
78 assert(Function->getPrimaryTemplate() && "No function template?");
79 if (Function->getPrimaryTemplate()->isMemberSpecialization())
80 break;
81 }
82
John McCallf181d8a2009-08-29 03:16:09 +000083 // If this is a friend declaration and it declares an entity at
84 // namespace scope, take arguments from its lexical parent
85 // instead of its semantic parent.
86 if (Function->getFriendObjectKind() &&
87 Function->getDeclContext()->isFileContext()) {
88 Ctx = Function->getLexicalDeclContext();
89 continue;
90 }
Douglas Gregord1102432009-08-28 17:37:35 +000091 }
John McCallf181d8a2009-08-29 03:16:09 +000092
93 Ctx = Ctx->getParent();
Douglas Gregor54dabfc2009-05-14 23:26:13 +000094 }
Mike Stump1eb44332009-09-09 15:08:12 +000095
Douglas Gregord1102432009-08-28 17:37:35 +000096 return Result;
Douglas Gregor54dabfc2009-05-14 23:26:13 +000097}
98
Douglas Gregorf35f8282009-11-11 21:54:23 +000099bool Sema::ActiveTemplateInstantiation::isInstantiationRecord() const {
100 switch (Kind) {
101 case TemplateInstantiation:
102 case DefaultTemplateArgumentInstantiation:
103 case DefaultFunctionArgumentInstantiation:
104 return true;
105
106 case ExplicitTemplateArgumentSubstitution:
107 case DeducedTemplateArgumentSubstitution:
108 case PriorTemplateArgumentSubstitution:
109 case DefaultTemplateArgumentChecking:
110 return false;
111 }
112
113 return true;
114}
115
Douglas Gregor26dce442009-03-10 00:06:19 +0000116Sema::InstantiatingTemplate::
117InstantiatingTemplate(Sema &SemaRef, SourceLocation PointOfInstantiation,
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000118 Decl *Entity,
Douglas Gregor26dce442009-03-10 00:06:19 +0000119 SourceRange InstantiationRange)
120 : SemaRef(SemaRef) {
Douglas Gregordf667e72009-03-10 20:44:00 +0000121
122 Invalid = CheckInstantiationDepth(PointOfInstantiation,
123 InstantiationRange);
124 if (!Invalid) {
Douglas Gregor26dce442009-03-10 00:06:19 +0000125 ActiveTemplateInstantiation Inst;
Douglas Gregordf667e72009-03-10 20:44:00 +0000126 Inst.Kind = ActiveTemplateInstantiation::TemplateInstantiation;
Douglas Gregor26dce442009-03-10 00:06:19 +0000127 Inst.PointOfInstantiation = PointOfInstantiation;
Douglas Gregordf667e72009-03-10 20:44:00 +0000128 Inst.Entity = reinterpret_cast<uintptr_t>(Entity);
Douglas Gregor313a81d2009-03-12 18:36:18 +0000129 Inst.TemplateArgs = 0;
130 Inst.NumTemplateArgs = 0;
Douglas Gregordf667e72009-03-10 20:44:00 +0000131 Inst.InstantiationRange = InstantiationRange;
132 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
Douglas Gregordf667e72009-03-10 20:44:00 +0000133 }
134}
135
Mike Stump1eb44332009-09-09 15:08:12 +0000136Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef,
Douglas Gregordf667e72009-03-10 20:44:00 +0000137 SourceLocation PointOfInstantiation,
138 TemplateDecl *Template,
139 const TemplateArgument *TemplateArgs,
140 unsigned NumTemplateArgs,
141 SourceRange InstantiationRange)
142 : SemaRef(SemaRef) {
143
144 Invalid = CheckInstantiationDepth(PointOfInstantiation,
145 InstantiationRange);
146 if (!Invalid) {
147 ActiveTemplateInstantiation Inst;
Mike Stump1eb44332009-09-09 15:08:12 +0000148 Inst.Kind
Douglas Gregordf667e72009-03-10 20:44:00 +0000149 = ActiveTemplateInstantiation::DefaultTemplateArgumentInstantiation;
150 Inst.PointOfInstantiation = PointOfInstantiation;
151 Inst.Entity = reinterpret_cast<uintptr_t>(Template);
152 Inst.TemplateArgs = TemplateArgs;
153 Inst.NumTemplateArgs = NumTemplateArgs;
Douglas Gregor26dce442009-03-10 00:06:19 +0000154 Inst.InstantiationRange = InstantiationRange;
155 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
Douglas Gregor26dce442009-03-10 00:06:19 +0000156 }
157}
158
Mike Stump1eb44332009-09-09 15:08:12 +0000159Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef,
Douglas Gregor637a4092009-06-10 23:47:09 +0000160 SourceLocation PointOfInstantiation,
Douglas Gregorcca9e962009-07-01 22:01:06 +0000161 FunctionTemplateDecl *FunctionTemplate,
162 const TemplateArgument *TemplateArgs,
163 unsigned NumTemplateArgs,
164 ActiveTemplateInstantiation::InstantiationKind Kind,
165 SourceRange InstantiationRange)
166: SemaRef(SemaRef) {
Mike Stump1eb44332009-09-09 15:08:12 +0000167
Douglas Gregorcca9e962009-07-01 22:01:06 +0000168 Invalid = CheckInstantiationDepth(PointOfInstantiation,
169 InstantiationRange);
170 if (!Invalid) {
171 ActiveTemplateInstantiation Inst;
172 Inst.Kind = Kind;
173 Inst.PointOfInstantiation = PointOfInstantiation;
174 Inst.Entity = reinterpret_cast<uintptr_t>(FunctionTemplate);
175 Inst.TemplateArgs = TemplateArgs;
176 Inst.NumTemplateArgs = NumTemplateArgs;
177 Inst.InstantiationRange = InstantiationRange;
178 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
Douglas Gregorf35f8282009-11-11 21:54:23 +0000179
180 if (!Inst.isInstantiationRecord())
181 ++SemaRef.NonInstantiationEntries;
Douglas Gregorcca9e962009-07-01 22:01:06 +0000182 }
183}
184
Mike Stump1eb44332009-09-09 15:08:12 +0000185Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef,
Douglas Gregorcca9e962009-07-01 22:01:06 +0000186 SourceLocation PointOfInstantiation,
Douglas Gregor637a4092009-06-10 23:47:09 +0000187 ClassTemplatePartialSpecializationDecl *PartialSpec,
188 const TemplateArgument *TemplateArgs,
189 unsigned NumTemplateArgs,
190 SourceRange InstantiationRange)
191 : SemaRef(SemaRef) {
192
Douglas Gregorf35f8282009-11-11 21:54:23 +0000193 Invalid = false;
194
195 ActiveTemplateInstantiation Inst;
196 Inst.Kind = ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution;
197 Inst.PointOfInstantiation = PointOfInstantiation;
198 Inst.Entity = reinterpret_cast<uintptr_t>(PartialSpec);
199 Inst.TemplateArgs = TemplateArgs;
200 Inst.NumTemplateArgs = NumTemplateArgs;
201 Inst.InstantiationRange = InstantiationRange;
202 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
203
204 assert(!Inst.isInstantiationRecord());
205 ++SemaRef.NonInstantiationEntries;
Douglas Gregor637a4092009-06-10 23:47:09 +0000206}
207
Mike Stump1eb44332009-09-09 15:08:12 +0000208Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef,
Douglas Gregor9148c3f2009-11-11 19:13:48 +0000209 SourceLocation PointOfInstantiation,
Anders Carlsson25cae7f2009-09-05 05:14:19 +0000210 ParmVarDecl *Param,
211 const TemplateArgument *TemplateArgs,
212 unsigned NumTemplateArgs,
213 SourceRange InstantiationRange)
214 : SemaRef(SemaRef) {
Mike Stump1eb44332009-09-09 15:08:12 +0000215
Douglas Gregor9148c3f2009-11-11 19:13:48 +0000216 Invalid = CheckInstantiationDepth(PointOfInstantiation, InstantiationRange);
Anders Carlsson25cae7f2009-09-05 05:14:19 +0000217
218 if (!Invalid) {
219 ActiveTemplateInstantiation Inst;
220 Inst.Kind
221 = ActiveTemplateInstantiation::DefaultFunctionArgumentInstantiation;
Douglas Gregor9148c3f2009-11-11 19:13:48 +0000222 Inst.PointOfInstantiation = PointOfInstantiation;
Anders Carlsson25cae7f2009-09-05 05:14:19 +0000223 Inst.Entity = reinterpret_cast<uintptr_t>(Param);
224 Inst.TemplateArgs = TemplateArgs;
225 Inst.NumTemplateArgs = NumTemplateArgs;
226 Inst.InstantiationRange = InstantiationRange;
227 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
Douglas Gregor9148c3f2009-11-11 19:13:48 +0000228 }
229}
230
231Sema::InstantiatingTemplate::
232InstantiatingTemplate(Sema &SemaRef, SourceLocation PointOfInstantiation,
233 TemplateDecl *Template,
234 NonTypeTemplateParmDecl *Param,
235 const TemplateArgument *TemplateArgs,
236 unsigned NumTemplateArgs,
237 SourceRange InstantiationRange) : SemaRef(SemaRef) {
Douglas Gregorf35f8282009-11-11 21:54:23 +0000238 Invalid = false;
Douglas Gregor9148c3f2009-11-11 19:13:48 +0000239
Douglas Gregorf35f8282009-11-11 21:54:23 +0000240 ActiveTemplateInstantiation Inst;
241 Inst.Kind = ActiveTemplateInstantiation::PriorTemplateArgumentSubstitution;
242 Inst.PointOfInstantiation = PointOfInstantiation;
243 Inst.Template = Template;
244 Inst.Entity = reinterpret_cast<uintptr_t>(Param);
245 Inst.TemplateArgs = TemplateArgs;
246 Inst.NumTemplateArgs = NumTemplateArgs;
247 Inst.InstantiationRange = InstantiationRange;
248 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
249
250 assert(!Inst.isInstantiationRecord());
251 ++SemaRef.NonInstantiationEntries;
Douglas Gregor9148c3f2009-11-11 19:13:48 +0000252}
253
254Sema::InstantiatingTemplate::
255InstantiatingTemplate(Sema &SemaRef, SourceLocation PointOfInstantiation,
256 TemplateDecl *Template,
257 TemplateTemplateParmDecl *Param,
258 const TemplateArgument *TemplateArgs,
259 unsigned NumTemplateArgs,
260 SourceRange InstantiationRange) : SemaRef(SemaRef) {
Douglas Gregorf35f8282009-11-11 21:54:23 +0000261 Invalid = false;
262 ActiveTemplateInstantiation Inst;
263 Inst.Kind = ActiveTemplateInstantiation::PriorTemplateArgumentSubstitution;
264 Inst.PointOfInstantiation = PointOfInstantiation;
265 Inst.Template = Template;
266 Inst.Entity = reinterpret_cast<uintptr_t>(Param);
267 Inst.TemplateArgs = TemplateArgs;
268 Inst.NumTemplateArgs = NumTemplateArgs;
269 Inst.InstantiationRange = InstantiationRange;
270 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
Douglas Gregor9148c3f2009-11-11 19:13:48 +0000271
Douglas Gregorf35f8282009-11-11 21:54:23 +0000272 assert(!Inst.isInstantiationRecord());
273 ++SemaRef.NonInstantiationEntries;
274}
275
276Sema::InstantiatingTemplate::
277InstantiatingTemplate(Sema &SemaRef, SourceLocation PointOfInstantiation,
278 TemplateDecl *Template,
279 NamedDecl *Param,
280 const TemplateArgument *TemplateArgs,
281 unsigned NumTemplateArgs,
282 SourceRange InstantiationRange) : SemaRef(SemaRef) {
283 Invalid = false;
284
285 ActiveTemplateInstantiation Inst;
286 Inst.Kind = ActiveTemplateInstantiation::DefaultTemplateArgumentChecking;
287 Inst.PointOfInstantiation = PointOfInstantiation;
288 Inst.Template = Template;
289 Inst.Entity = reinterpret_cast<uintptr_t>(Param);
290 Inst.TemplateArgs = TemplateArgs;
291 Inst.NumTemplateArgs = NumTemplateArgs;
292 Inst.InstantiationRange = InstantiationRange;
293 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
294
295 assert(!Inst.isInstantiationRecord());
296 ++SemaRef.NonInstantiationEntries;
Anders Carlsson25cae7f2009-09-05 05:14:19 +0000297}
298
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000299void Sema::InstantiatingTemplate::Clear() {
300 if (!Invalid) {
Douglas Gregorf35f8282009-11-11 21:54:23 +0000301 if (!SemaRef.ActiveTemplateInstantiations.back().isInstantiationRecord()) {
302 assert(SemaRef.NonInstantiationEntries > 0);
303 --SemaRef.NonInstantiationEntries;
304 }
305
Douglas Gregor26dce442009-03-10 00:06:19 +0000306 SemaRef.ActiveTemplateInstantiations.pop_back();
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000307 Invalid = true;
308 }
Douglas Gregor26dce442009-03-10 00:06:19 +0000309}
310
Douglas Gregordf667e72009-03-10 20:44:00 +0000311bool Sema::InstantiatingTemplate::CheckInstantiationDepth(
312 SourceLocation PointOfInstantiation,
313 SourceRange InstantiationRange) {
Douglas Gregorf35f8282009-11-11 21:54:23 +0000314 assert(SemaRef.NonInstantiationEntries <=
315 SemaRef.ActiveTemplateInstantiations.size());
316 if ((SemaRef.ActiveTemplateInstantiations.size() -
317 SemaRef.NonInstantiationEntries)
318 <= SemaRef.getLangOptions().InstantiationDepth)
Douglas Gregordf667e72009-03-10 20:44:00 +0000319 return false;
320
Mike Stump1eb44332009-09-09 15:08:12 +0000321 SemaRef.Diag(PointOfInstantiation,
Douglas Gregordf667e72009-03-10 20:44:00 +0000322 diag::err_template_recursion_depth_exceeded)
323 << SemaRef.getLangOptions().InstantiationDepth
324 << InstantiationRange;
325 SemaRef.Diag(PointOfInstantiation, diag::note_template_recursion_depth)
326 << SemaRef.getLangOptions().InstantiationDepth;
327 return true;
328}
329
Douglas Gregoree1828a2009-03-10 18:03:33 +0000330/// \brief Prints the current instantiation stack through a series of
331/// notes.
332void Sema::PrintInstantiationStack() {
Douglas Gregorcca9e962009-07-01 22:01:06 +0000333 // FIXME: In all of these cases, we need to show the template arguments
Douglas Gregoree1828a2009-03-10 18:03:33 +0000334 for (llvm::SmallVector<ActiveTemplateInstantiation, 16>::reverse_iterator
335 Active = ActiveTemplateInstantiations.rbegin(),
336 ActiveEnd = ActiveTemplateInstantiations.rend();
337 Active != ActiveEnd;
338 ++Active) {
Douglas Gregordf667e72009-03-10 20:44:00 +0000339 switch (Active->Kind) {
340 case ActiveTemplateInstantiation::TemplateInstantiation: {
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000341 Decl *D = reinterpret_cast<Decl *>(Active->Entity);
342 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) {
343 unsigned DiagID = diag::note_template_member_class_here;
344 if (isa<ClassTemplateSpecializationDecl>(Record))
345 DiagID = diag::note_template_class_instantiation_here;
Mike Stump1eb44332009-09-09 15:08:12 +0000346 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000347 DiagID)
348 << Context.getTypeDeclType(Record)
349 << Active->InstantiationRange;
Douglas Gregor7caa6822009-07-24 20:34:43 +0000350 } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
Douglas Gregor1637be72009-06-26 00:10:03 +0000351 unsigned DiagID;
352 if (Function->getPrimaryTemplate())
353 DiagID = diag::note_function_template_spec_here;
354 else
355 DiagID = diag::note_template_member_function_here;
Mike Stump1eb44332009-09-09 15:08:12 +0000356 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000357 DiagID)
358 << Function
359 << Active->InstantiationRange;
Douglas Gregor7caa6822009-07-24 20:34:43 +0000360 } else {
361 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
362 diag::note_template_static_data_member_def_here)
363 << cast<VarDecl>(D)
364 << Active->InstantiationRange;
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000365 }
Douglas Gregordf667e72009-03-10 20:44:00 +0000366 break;
367 }
368
369 case ActiveTemplateInstantiation::DefaultTemplateArgumentInstantiation: {
370 TemplateDecl *Template = cast<TemplateDecl>((Decl *)Active->Entity);
371 std::string TemplateArgsStr
Douglas Gregor7532dc62009-03-30 22:58:21 +0000372 = TemplateSpecializationType::PrintTemplateArgumentList(
Mike Stump1eb44332009-09-09 15:08:12 +0000373 Active->TemplateArgs,
Douglas Gregord249e1d1f2009-05-29 20:38:28 +0000374 Active->NumTemplateArgs,
375 Context.PrintingPolicy);
Douglas Gregordf667e72009-03-10 20:44:00 +0000376 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
377 diag::note_default_arg_instantiation_here)
378 << (Template->getNameAsString() + TemplateArgsStr)
379 << Active->InstantiationRange;
380 break;
381 }
Douglas Gregor637a4092009-06-10 23:47:09 +0000382
Douglas Gregorcca9e962009-07-01 22:01:06 +0000383 case ActiveTemplateInstantiation::ExplicitTemplateArgumentSubstitution: {
Mike Stump1eb44332009-09-09 15:08:12 +0000384 FunctionTemplateDecl *FnTmpl
Douglas Gregorcca9e962009-07-01 22:01:06 +0000385 = cast<FunctionTemplateDecl>((Decl *)Active->Entity);
Douglas Gregor637a4092009-06-10 23:47:09 +0000386 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
Douglas Gregorcca9e962009-07-01 22:01:06 +0000387 diag::note_explicit_template_arg_substitution_here)
388 << FnTmpl << Active->InstantiationRange;
Douglas Gregor637a4092009-06-10 23:47:09 +0000389 break;
390 }
Mike Stump1eb44332009-09-09 15:08:12 +0000391
Douglas Gregorcca9e962009-07-01 22:01:06 +0000392 case ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution:
393 if (ClassTemplatePartialSpecializationDecl *PartialSpec
394 = dyn_cast<ClassTemplatePartialSpecializationDecl>(
395 (Decl *)Active->Entity)) {
396 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
397 diag::note_partial_spec_deduct_instantiation_here)
398 << Context.getTypeDeclType(PartialSpec)
399 << Active->InstantiationRange;
400 } else {
401 FunctionTemplateDecl *FnTmpl
402 = cast<FunctionTemplateDecl>((Decl *)Active->Entity);
403 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
404 diag::note_function_template_deduction_instantiation_here)
405 << FnTmpl << Active->InstantiationRange;
406 }
407 break;
Douglas Gregor637a4092009-06-10 23:47:09 +0000408
Anders Carlsson25cae7f2009-09-05 05:14:19 +0000409 case ActiveTemplateInstantiation::DefaultFunctionArgumentInstantiation: {
410 ParmVarDecl *Param = cast<ParmVarDecl>((Decl *)Active->Entity);
411 FunctionDecl *FD = cast<FunctionDecl>(Param->getDeclContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000412
Anders Carlsson25cae7f2009-09-05 05:14:19 +0000413 std::string TemplateArgsStr
414 = TemplateSpecializationType::PrintTemplateArgumentList(
Mike Stump1eb44332009-09-09 15:08:12 +0000415 Active->TemplateArgs,
Anders Carlsson25cae7f2009-09-05 05:14:19 +0000416 Active->NumTemplateArgs,
417 Context.PrintingPolicy);
418 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
419 diag::note_default_function_arg_instantiation_here)
Anders Carlsson6bc107b2009-09-05 05:38:54 +0000420 << (FD->getNameAsString() + TemplateArgsStr)
Anders Carlsson25cae7f2009-09-05 05:14:19 +0000421 << Active->InstantiationRange;
422 break;
423 }
Mike Stump1eb44332009-09-09 15:08:12 +0000424
Douglas Gregor9148c3f2009-11-11 19:13:48 +0000425 case ActiveTemplateInstantiation::PriorTemplateArgumentSubstitution: {
426 NamedDecl *Parm = cast<NamedDecl>((Decl *)Active->Entity);
427 std::string Name;
428 if (!Parm->getName().empty())
429 Name = std::string(" '") + Parm->getName().str() + "'";
430
431 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
432 diag::note_prior_template_arg_substitution)
433 << isa<TemplateTemplateParmDecl>(Parm)
434 << Name
435 << getTemplateArgumentBindingsText(
436 Active->Template->getTemplateParameters(),
437 Active->TemplateArgs,
438 Active->NumTemplateArgs)
439 << Active->InstantiationRange;
440 break;
441 }
Douglas Gregorf35f8282009-11-11 21:54:23 +0000442
443 case ActiveTemplateInstantiation::DefaultTemplateArgumentChecking: {
444 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
445 diag::note_template_default_arg_checking)
446 << getTemplateArgumentBindingsText(
447 Active->Template->getTemplateParameters(),
448 Active->TemplateArgs,
449 Active->NumTemplateArgs)
450 << Active->InstantiationRange;
451 break;
452 }
Douglas Gregordf667e72009-03-10 20:44:00 +0000453 }
Douglas Gregoree1828a2009-03-10 18:03:33 +0000454 }
455}
456
Douglas Gregor5e9f35c2009-06-14 07:33:30 +0000457bool Sema::isSFINAEContext() const {
458 using llvm::SmallVector;
459 for (SmallVector<ActiveTemplateInstantiation, 16>::const_reverse_iterator
460 Active = ActiveTemplateInstantiations.rbegin(),
461 ActiveEnd = ActiveTemplateInstantiations.rend();
462 Active != ActiveEnd;
Douglas Gregorf35f8282009-11-11 21:54:23 +0000463 ++Active)
464 {
Douglas Gregor5e9f35c2009-06-14 07:33:30 +0000465 switch(Active->Kind) {
Douglas Gregorcca9e962009-07-01 22:01:06 +0000466 case ActiveTemplateInstantiation::TemplateInstantiation:
Anders Carlsson25cae7f2009-09-05 05:14:19 +0000467 case ActiveTemplateInstantiation::DefaultFunctionArgumentInstantiation:
Douglas Gregorcca9e962009-07-01 22:01:06 +0000468 // This is a template instantiation, so there is no SFINAE.
469 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000470
Douglas Gregor5e9f35c2009-06-14 07:33:30 +0000471 case ActiveTemplateInstantiation::DefaultTemplateArgumentInstantiation:
Douglas Gregor9148c3f2009-11-11 19:13:48 +0000472 case ActiveTemplateInstantiation::PriorTemplateArgumentSubstitution:
Douglas Gregorf35f8282009-11-11 21:54:23 +0000473 case ActiveTemplateInstantiation::DefaultTemplateArgumentChecking:
Douglas Gregor9148c3f2009-11-11 19:13:48 +0000474 // A default template argument instantiation and substitution into
475 // template parameters with arguments for prior parameters may or may
476 // not be a SFINAE context; look further up the stack.
Douglas Gregor5e9f35c2009-06-14 07:33:30 +0000477 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000478
Douglas Gregorcca9e962009-07-01 22:01:06 +0000479 case ActiveTemplateInstantiation::ExplicitTemplateArgumentSubstitution:
480 case ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution:
481 // We're either substitution explicitly-specified template arguments
482 // or deduced template arguments, so SFINAE applies.
483 return true;
Douglas Gregor5e9f35c2009-06-14 07:33:30 +0000484 }
485 }
486
487 return false;
488}
489
Douglas Gregor99ebf652009-02-27 19:31:52 +0000490//===----------------------------------------------------------------------===/
491// Template Instantiation for Types
492//===----------------------------------------------------------------------===/
Douglas Gregorcd281c32009-02-28 00:25:32 +0000493namespace {
Mike Stump1eb44332009-09-09 15:08:12 +0000494 class VISIBILITY_HIDDEN TemplateInstantiator
495 : public TreeTransform<TemplateInstantiator> {
Douglas Gregord6350ae2009-08-28 20:31:08 +0000496 const MultiLevelTemplateArgumentList &TemplateArgs;
Douglas Gregorcd281c32009-02-28 00:25:32 +0000497 SourceLocation Loc;
498 DeclarationName Entity;
Douglas Gregor99ebf652009-02-27 19:31:52 +0000499
Douglas Gregorcd281c32009-02-28 00:25:32 +0000500 public:
Douglas Gregor43959a92009-08-20 07:17:43 +0000501 typedef TreeTransform<TemplateInstantiator> inherited;
Mike Stump1eb44332009-09-09 15:08:12 +0000502
503 TemplateInstantiator(Sema &SemaRef,
Douglas Gregord6350ae2009-08-28 20:31:08 +0000504 const MultiLevelTemplateArgumentList &TemplateArgs,
Douglas Gregor577f75a2009-08-04 16:50:30 +0000505 SourceLocation Loc,
Mike Stump1eb44332009-09-09 15:08:12 +0000506 DeclarationName Entity)
507 : inherited(SemaRef), TemplateArgs(TemplateArgs), Loc(Loc),
Douglas Gregor43959a92009-08-20 07:17:43 +0000508 Entity(Entity) { }
Douglas Gregorcd281c32009-02-28 00:25:32 +0000509
Mike Stump1eb44332009-09-09 15:08:12 +0000510 /// \brief Determine whether the given type \p T has already been
Douglas Gregor577f75a2009-08-04 16:50:30 +0000511 /// transformed.
512 ///
513 /// For the purposes of template instantiation, a type has already been
514 /// transformed if it is NULL or if it is not dependent.
515 bool AlreadyTransformed(QualType T) {
516 return T.isNull() || !T->isDependentType();
Douglas Gregorff668032009-05-13 18:28:20 +0000517 }
Mike Stump1eb44332009-09-09 15:08:12 +0000518
Douglas Gregor577f75a2009-08-04 16:50:30 +0000519 /// \brief Returns the location of the entity being instantiated, if known.
520 SourceLocation getBaseLocation() { return Loc; }
Mike Stump1eb44332009-09-09 15:08:12 +0000521
Douglas Gregor577f75a2009-08-04 16:50:30 +0000522 /// \brief Returns the name of the entity being instantiated, if any.
523 DeclarationName getBaseEntity() { return Entity; }
Mike Stump1eb44332009-09-09 15:08:12 +0000524
Douglas Gregor972e6ce2009-10-27 06:26:26 +0000525 /// \brief Sets the "base" location and entity when that
526 /// information is known based on another transformation.
527 void setBase(SourceLocation Loc, DeclarationName Entity) {
528 this->Loc = Loc;
529 this->Entity = Entity;
530 }
531
Douglas Gregor577f75a2009-08-04 16:50:30 +0000532 /// \brief Transform the given declaration by instantiating a reference to
533 /// this declaration.
534 Decl *TransformDecl(Decl *D);
Douglas Gregorb98b1992009-08-11 05:31:07 +0000535
Mike Stump1eb44332009-09-09 15:08:12 +0000536 /// \brief Transform the definition of the given declaration by
Douglas Gregor43959a92009-08-20 07:17:43 +0000537 /// instantiating it.
538 Decl *TransformDefinition(Decl *D);
Mike Stump1eb44332009-09-09 15:08:12 +0000539
Douglas Gregor6cd21982009-10-20 05:58:46 +0000540 /// \bried Transform the first qualifier within a scope by instantiating the
541 /// declaration.
542 NamedDecl *TransformFirstQualifierInScope(NamedDecl *D, SourceLocation Loc);
543
Douglas Gregor43959a92009-08-20 07:17:43 +0000544 /// \brief Rebuild the exception declaration and register the declaration
545 /// as an instantiated local.
Mike Stump1eb44332009-09-09 15:08:12 +0000546 VarDecl *RebuildExceptionDecl(VarDecl *ExceptionDecl, QualType T,
Douglas Gregor43959a92009-08-20 07:17:43 +0000547 DeclaratorInfo *Declarator,
548 IdentifierInfo *Name,
549 SourceLocation Loc, SourceRange TypeRange);
Mike Stump1eb44332009-09-09 15:08:12 +0000550
John McCallc4e70192009-09-11 04:59:25 +0000551 /// \brief Check for tag mismatches when instantiating an
552 /// elaborated type.
553 QualType RebuildElaboratedType(QualType T, ElaboratedType::TagKind Tag);
554
Douglas Gregorc86a6e92009-11-04 07:01:15 +0000555 Sema::OwningExprResult TransformPredefinedExpr(PredefinedExpr *E,
556 bool isAddressOfOperand);
557 Sema::OwningExprResult TransformDeclRefExpr(DeclRefExpr *E,
558 bool isAddressOfOperand);
Mike Stump1eb44332009-09-09 15:08:12 +0000559
Sebastian Redla29e51b2009-11-08 13:56:19 +0000560 Sema::OwningExprResult TransformCXXDefaultArgExpr(CXXDefaultArgExpr *E,
561 bool isAddressOfOperand);
562
Mike Stump1eb44332009-09-09 15:08:12 +0000563 /// \brief Transforms a template type parameter type by performing
Douglas Gregor577f75a2009-08-04 16:50:30 +0000564 /// substitution of the corresponding template type argument.
John McCalla2becad2009-10-21 00:40:46 +0000565 QualType TransformTemplateTypeParmType(TypeLocBuilder &TLB,
566 TemplateTypeParmTypeLoc TL);
Douglas Gregor577f75a2009-08-04 16:50:30 +0000567 };
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000568}
569
Douglas Gregor577f75a2009-08-04 16:50:30 +0000570Decl *TemplateInstantiator::TransformDecl(Decl *D) {
Douglas Gregorc68afe22009-09-03 21:38:09 +0000571 if (!D)
572 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000573
Douglas Gregorc68afe22009-09-03 21:38:09 +0000574 if (TemplateTemplateParmDecl *TTP = dyn_cast<TemplateTemplateParmDecl>(D)) {
Douglas Gregord6350ae2009-08-28 20:31:08 +0000575 if (TTP->getDepth() < TemplateArgs.getNumLevels()) {
Douglas Gregor788cd062009-11-11 01:00:40 +0000576 TemplateName Template
577 = TemplateArgs(TTP->getDepth(), TTP->getPosition()).getAsTemplate();
578 assert(!Template.isNull() && Template.getAsTemplateDecl() &&
Douglas Gregord6350ae2009-08-28 20:31:08 +0000579 "Wrong kind of template template argument");
Douglas Gregor788cd062009-11-11 01:00:40 +0000580 return Template.getAsTemplateDecl();
Douglas Gregord6350ae2009-08-28 20:31:08 +0000581 }
Mike Stump1eb44332009-09-09 15:08:12 +0000582
583 // If the corresponding template argument is NULL or non-existent, it's
584 // because we are performing instantiation from explicitly-specified
Douglas Gregord6350ae2009-08-28 20:31:08 +0000585 // template arguments in a function template, but there were some
586 // arguments left unspecified.
Mike Stump1eb44332009-09-09 15:08:12 +0000587 if (!TemplateArgs.hasTemplateArgument(TTP->getDepth(),
Douglas Gregord6350ae2009-08-28 20:31:08 +0000588 TTP->getPosition()))
589 return D;
Mike Stump1eb44332009-09-09 15:08:12 +0000590
Douglas Gregor788cd062009-11-11 01:00:40 +0000591 // Fall through to find the instantiated declaration for this template
592 // template parameter.
Douglas Gregord1067e52009-08-06 06:41:21 +0000593 }
Mike Stump1eb44332009-09-09 15:08:12 +0000594
Douglas Gregore95b4092009-09-16 18:34:49 +0000595 return SemaRef.FindInstantiatedDecl(cast<NamedDecl>(D), TemplateArgs);
Douglas Gregor577f75a2009-08-04 16:50:30 +0000596}
597
Douglas Gregor43959a92009-08-20 07:17:43 +0000598Decl *TemplateInstantiator::TransformDefinition(Decl *D) {
John McCallce3ff2b2009-08-25 22:02:44 +0000599 Decl *Inst = getSema().SubstDecl(D, getSema().CurContext, TemplateArgs);
Douglas Gregor43959a92009-08-20 07:17:43 +0000600 if (!Inst)
601 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000602
Douglas Gregor43959a92009-08-20 07:17:43 +0000603 getSema().CurrentInstantiationScope->InstantiatedLocal(D, Inst);
604 return Inst;
605}
606
Douglas Gregor6cd21982009-10-20 05:58:46 +0000607NamedDecl *
608TemplateInstantiator::TransformFirstQualifierInScope(NamedDecl *D,
609 SourceLocation Loc) {
610 // If the first part of the nested-name-specifier was a template type
611 // parameter, instantiate that type parameter down to a tag type.
612 if (TemplateTypeParmDecl *TTPD = dyn_cast_or_null<TemplateTypeParmDecl>(D)) {
613 const TemplateTypeParmType *TTP
614 = cast<TemplateTypeParmType>(getSema().Context.getTypeDeclType(TTPD));
615 if (TTP->getDepth() < TemplateArgs.getNumLevels()) {
616 QualType T = TemplateArgs(TTP->getDepth(), TTP->getIndex()).getAsType();
617 if (T.isNull())
618 return cast_or_null<NamedDecl>(TransformDecl(D));
619
620 if (const TagType *Tag = T->getAs<TagType>())
621 return Tag->getDecl();
622
623 // The resulting type is not a tag; complain.
624 getSema().Diag(Loc, diag::err_nested_name_spec_non_tag) << T;
625 return 0;
626 }
627 }
628
629 return cast_or_null<NamedDecl>(TransformDecl(D));
630}
631
Douglas Gregor43959a92009-08-20 07:17:43 +0000632VarDecl *
633TemplateInstantiator::RebuildExceptionDecl(VarDecl *ExceptionDecl,
Mike Stump1eb44332009-09-09 15:08:12 +0000634 QualType T,
Douglas Gregor43959a92009-08-20 07:17:43 +0000635 DeclaratorInfo *Declarator,
636 IdentifierInfo *Name,
Mike Stump1eb44332009-09-09 15:08:12 +0000637 SourceLocation Loc,
Douglas Gregor43959a92009-08-20 07:17:43 +0000638 SourceRange TypeRange) {
639 VarDecl *Var = inherited::RebuildExceptionDecl(ExceptionDecl, T, Declarator,
640 Name, Loc, TypeRange);
641 if (Var && !Var->isInvalidDecl())
642 getSema().CurrentInstantiationScope->InstantiatedLocal(ExceptionDecl, Var);
643 return Var;
644}
645
John McCallc4e70192009-09-11 04:59:25 +0000646QualType
647TemplateInstantiator::RebuildElaboratedType(QualType T,
648 ElaboratedType::TagKind Tag) {
649 if (const TagType *TT = T->getAs<TagType>()) {
650 TagDecl* TD = TT->getDecl();
651
652 // FIXME: this location is very wrong; we really need typelocs.
653 SourceLocation TagLocation = TD->getTagKeywordLoc();
654
655 // FIXME: type might be anonymous.
656 IdentifierInfo *Id = TD->getIdentifier();
657
658 // TODO: should we even warn on struct/class mismatches for this? Seems
659 // like it's likely to produce a lot of spurious errors.
660 if (!SemaRef.isAcceptableTagRedeclaration(TD, Tag, TagLocation, *Id)) {
661 SemaRef.Diag(TagLocation, diag::err_use_with_wrong_tag)
662 << Id
663 << CodeModificationHint::CreateReplacement(SourceRange(TagLocation),
664 TD->getKindName());
665 SemaRef.Diag(TD->getLocation(), diag::note_previous_use);
666 }
667 }
668
669 return TreeTransform<TemplateInstantiator>::RebuildElaboratedType(T, Tag);
670}
671
672Sema::OwningExprResult
Douglas Gregorc86a6e92009-11-04 07:01:15 +0000673TemplateInstantiator::TransformPredefinedExpr(PredefinedExpr *E,
674 bool isAddressOfOperand) {
Anders Carlsson773f3972009-09-11 01:22:35 +0000675 if (!E->isTypeDependent())
676 return SemaRef.Owned(E->Retain());
677
678 FunctionDecl *currentDecl = getSema().getCurFunctionDecl();
679 assert(currentDecl && "Must have current function declaration when "
680 "instantiating.");
681
682 PredefinedExpr::IdentType IT = E->getIdentType();
683
684 unsigned Length =
685 PredefinedExpr::ComputeName(getSema().Context, IT, currentDecl).length();
686
687 llvm::APInt LengthI(32, Length + 1);
John McCall0953e762009-09-24 19:53:00 +0000688 QualType ResTy = getSema().Context.CharTy.withConst();
Anders Carlsson773f3972009-09-11 01:22:35 +0000689 ResTy = getSema().Context.getConstantArrayType(ResTy, LengthI,
690 ArrayType::Normal, 0);
691 PredefinedExpr *PE =
692 new (getSema().Context) PredefinedExpr(E->getLocation(), ResTy, IT);
693 return getSema().Owned(PE);
694}
695
696Sema::OwningExprResult
Douglas Gregorc86a6e92009-11-04 07:01:15 +0000697TemplateInstantiator::TransformDeclRefExpr(DeclRefExpr *E,
698 bool isAddressOfOperand) {
Douglas Gregorb98b1992009-08-11 05:31:07 +0000699 // FIXME: Clean this up a bit
700 NamedDecl *D = E->getDecl();
701 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(D)) {
Douglas Gregor550d9b22009-10-31 17:21:17 +0000702 if (NTTP->getDepth() < TemplateArgs.getNumLevels()) {
Douglas Gregor550d9b22009-10-31 17:21:17 +0000703 // If the corresponding template argument is NULL or non-existent, it's
704 // because we are performing instantiation from explicitly-specified
705 // template arguments in a function template, but there were some
706 // arguments left unspecified.
707 if (!TemplateArgs.hasTemplateArgument(NTTP->getDepth(),
708 NTTP->getPosition()))
709 return SemaRef.Owned(E->Retain());
Mike Stump1eb44332009-09-09 15:08:12 +0000710
Douglas Gregor550d9b22009-10-31 17:21:17 +0000711 const TemplateArgument &Arg = TemplateArgs(NTTP->getDepth(),
712 NTTP->getPosition());
Mike Stump1eb44332009-09-09 15:08:12 +0000713
Douglas Gregor550d9b22009-10-31 17:21:17 +0000714 // The template argument itself might be an expression, in which
715 // case we just return that expression.
716 if (Arg.getKind() == TemplateArgument::Expression)
717 return SemaRef.Owned(Arg.getAsExpr()->Retain());
Mike Stump1eb44332009-09-09 15:08:12 +0000718
Douglas Gregor550d9b22009-10-31 17:21:17 +0000719 if (Arg.getKind() == TemplateArgument::Declaration) {
720 ValueDecl *VD = cast<ValueDecl>(Arg.getAsDecl());
Mike Stump1eb44332009-09-09 15:08:12 +0000721
Douglas Gregor550d9b22009-10-31 17:21:17 +0000722 VD = cast_or_null<ValueDecl>(
Douglas Gregorc86a6e92009-11-04 07:01:15 +0000723 getSema().FindInstantiatedDecl(VD, TemplateArgs));
Douglas Gregor550d9b22009-10-31 17:21:17 +0000724 if (!VD)
725 return SemaRef.ExprError();
Mike Stump1eb44332009-09-09 15:08:12 +0000726
Douglas Gregor231edff2009-11-12 17:40:13 +0000727 if (VD->getDeclContext()->isRecord()) {
728 // If the value is a class member, we might have a pointer-to-member.
729 // Determine whether the non-type template template parameter is of
730 // pointer-to-member type. If so, we need to build an appropriate
731 // expression for a pointer-to-member, since a "normal" DeclRefExpr
732 // would refer to the member itself.
733 if (NTTP->getType()->isMemberPointerType()) {
734 QualType ClassType
735 = SemaRef.Context.getTypeDeclType(
736 cast<RecordDecl>(VD->getDeclContext()));
737 NestedNameSpecifier *Qualifier
738 = NestedNameSpecifier::Create(SemaRef.Context, 0, false,
739 ClassType.getTypePtr());
740 CXXScopeSpec SS;
741 SS.setScopeRep(Qualifier);
742 OwningExprResult RefExpr
743 = SemaRef.BuildDeclRefExpr(VD,
744 VD->getType().getNonReferenceType(),
745 E->getLocation(),
746 /*FIXME:*/false, /*FIXME:*/false,
747 &SS);
748 if (RefExpr.isInvalid())
749 return SemaRef.ExprError();
750
751 return SemaRef.CreateBuiltinUnaryOp(E->getLocation(),
752 UnaryOperator::AddrOf,
753 move(RefExpr));
754 }
755 }
756
757 return SemaRef.BuildDeclRefExpr(VD, VD->getType().getNonReferenceType(),
758 E->getLocation(),
Douglas Gregor550d9b22009-10-31 17:21:17 +0000759 /*FIXME:*/false, /*FIXME:*/false);
760 }
Mike Stump1eb44332009-09-09 15:08:12 +0000761
Douglas Gregor550d9b22009-10-31 17:21:17 +0000762 assert(Arg.getKind() == TemplateArgument::Integral);
763 QualType T = Arg.getIntegralType();
764 if (T->isCharType() || T->isWideCharType())
765 return SemaRef.Owned(new (SemaRef.Context) CharacterLiteral(
766 Arg.getAsIntegral()->getZExtValue(),
767 T->isWideCharType(),
Mike Stump1eb44332009-09-09 15:08:12 +0000768 T,
Douglas Gregord6350ae2009-08-28 20:31:08 +0000769 E->getSourceRange().getBegin()));
Douglas Gregor550d9b22009-10-31 17:21:17 +0000770 if (T->isBooleanType())
771 return SemaRef.Owned(new (SemaRef.Context) CXXBoolLiteralExpr(
772 Arg.getAsIntegral()->getBoolValue(),
773 T,
774 E->getSourceRange().getBegin()));
775
776 assert(Arg.getAsIntegral()->getBitWidth() == SemaRef.Context.getIntWidth(T));
777 return SemaRef.Owned(new (SemaRef.Context) IntegerLiteral(
778 *Arg.getAsIntegral(),
779 T,
780 E->getSourceRange().getBegin()));
781 }
782
783 // We have a non-type template parameter that isn't fully substituted;
784 // FindInstantiatedDecl will find it in the local instantiation scope.
Douglas Gregorb98b1992009-08-11 05:31:07 +0000785 }
Mike Stump1eb44332009-09-09 15:08:12 +0000786
Douglas Gregore95b4092009-09-16 18:34:49 +0000787 NamedDecl *InstD = SemaRef.FindInstantiatedDecl(D, TemplateArgs);
Douglas Gregorb98b1992009-08-11 05:31:07 +0000788 if (!InstD)
789 return SemaRef.ExprError();
Mike Stump1eb44332009-09-09 15:08:12 +0000790
John McCall9488ea12009-11-17 05:59:44 +0000791 // Flatten using declarations into their shadow declarations.
792 if (isa<UsingDecl>(InstD)) {
793 UsingDecl *UD = cast<UsingDecl>(InstD);
794
795 bool HasNonFunction = false;
796
797 llvm::SmallVector<NamedDecl*, 8> Decls;
798 for (UsingDecl::shadow_iterator I = UD->shadow_begin(),
799 E = UD->shadow_end(); I != E; ++I) {
800 NamedDecl *TD = (*I)->getTargetDecl();
801 if (!TD->isFunctionOrFunctionTemplate())
802 HasNonFunction = true;
803
804 Decls.push_back(TD);
805 }
806
807 if (Decls.empty())
808 return SemaRef.ExprError();
809
810 if (Decls.size() == 1)
811 InstD = Decls[0];
812 else if (!HasNonFunction) {
813 OverloadedFunctionDecl *OFD
814 = OverloadedFunctionDecl::Create(SemaRef.Context,
815 UD->getDeclContext(),
816 UD->getDeclName());
817 for (llvm::SmallVectorImpl<NamedDecl*>::iterator I = Decls.begin(),
818 E = Decls.end(); I != E; ++I)
819 if (isa<FunctionDecl>(*I))
820 OFD->addOverload(cast<FunctionDecl>(*I));
821 else
822 OFD->addOverload(cast<FunctionTemplateDecl>(*I));
823
824 InstD = OFD;
825 } else {
826 // FIXME
827 assert(false && "using declaration resolved to mixed set");
828 return SemaRef.ExprError();
829 }
830 }
Mike Stump1eb44332009-09-09 15:08:12 +0000831
Douglas Gregora2813ce2009-10-23 18:54:35 +0000832 CXXScopeSpec SS;
833 NestedNameSpecifier *Qualifier = 0;
834 if (E->getQualifier()) {
835 Qualifier = TransformNestedNameSpecifier(E->getQualifier(),
836 E->getQualifierRange());
837 if (!Qualifier)
838 return SemaRef.ExprError();
839
840 SS.setScopeRep(Qualifier);
841 SS.setRange(E->getQualifierRange());
842 }
843
Mike Stump1eb44332009-09-09 15:08:12 +0000844 return SemaRef.BuildDeclarationNameExpr(E->getLocation(), InstD,
Douglas Gregorb98b1992009-08-11 05:31:07 +0000845 /*FIXME:*/false,
Douglas Gregora2813ce2009-10-23 18:54:35 +0000846 &SS,
Douglas Gregorc86a6e92009-11-04 07:01:15 +0000847 isAddressOfOperand);
Douglas Gregorb98b1992009-08-11 05:31:07 +0000848}
849
Sebastian Redla29e51b2009-11-08 13:56:19 +0000850Sema::OwningExprResult TemplateInstantiator::TransformCXXDefaultArgExpr(
851 CXXDefaultArgExpr *E, bool isAddressOfOperand) {
852 assert(!cast<FunctionDecl>(E->getParam()->getDeclContext())->
853 getDescribedFunctionTemplate() &&
854 "Default arg expressions are never formed in dependent cases.");
855 return SemaRef.Owned(E->Retain());
856}
857
858
Mike Stump1eb44332009-09-09 15:08:12 +0000859QualType
John McCalla2becad2009-10-21 00:40:46 +0000860TemplateInstantiator::TransformTemplateTypeParmType(TypeLocBuilder &TLB,
861 TemplateTypeParmTypeLoc TL) {
862 TemplateTypeParmType *T = TL.getTypePtr();
Douglas Gregord6350ae2009-08-28 20:31:08 +0000863 if (T->getDepth() < TemplateArgs.getNumLevels()) {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000864 // Replace the template type parameter with its corresponding
865 // template argument.
Mike Stump1eb44332009-09-09 15:08:12 +0000866
867 // If the corresponding template argument is NULL or doesn't exist, it's
868 // because we are performing instantiation from explicitly-specified
869 // template arguments in a function template class, but there were some
Douglas Gregor16134c62009-07-01 00:28:38 +0000870 // arguments left unspecified.
John McCalla2becad2009-10-21 00:40:46 +0000871 if (!TemplateArgs.hasTemplateArgument(T->getDepth(), T->getIndex())) {
872 TemplateTypeParmTypeLoc NewTL
873 = TLB.push<TemplateTypeParmTypeLoc>(TL.getType());
874 NewTL.setNameLoc(TL.getNameLoc());
875 return TL.getType();
876 }
Mike Stump1eb44332009-09-09 15:08:12 +0000877
878 assert(TemplateArgs(T->getDepth(), T->getIndex()).getKind()
Douglas Gregord6350ae2009-08-28 20:31:08 +0000879 == TemplateArgument::Type &&
Douglas Gregor99ebf652009-02-27 19:31:52 +0000880 "Template argument kind mismatch");
Douglas Gregord6350ae2009-08-28 20:31:08 +0000881
John McCall49a832b2009-10-18 09:09:24 +0000882 QualType Replacement
883 = TemplateArgs(T->getDepth(), T->getIndex()).getAsType();
884
885 // TODO: only do this uniquing once, at the start of instantiation.
John McCalla2becad2009-10-21 00:40:46 +0000886 QualType Result
887 = getSema().Context.getSubstTemplateTypeParmType(T, Replacement);
888 SubstTemplateTypeParmTypeLoc NewTL
889 = TLB.push<SubstTemplateTypeParmTypeLoc>(Result);
890 NewTL.setNameLoc(TL.getNameLoc());
891 return Result;
Mike Stump1eb44332009-09-09 15:08:12 +0000892 }
Douglas Gregor99ebf652009-02-27 19:31:52 +0000893
894 // The template type parameter comes from an inner template (e.g.,
895 // the template parameter list of a member template inside the
896 // template we are instantiating). Create a new template type
897 // parameter with the template "level" reduced by one.
John McCalla2becad2009-10-21 00:40:46 +0000898 QualType Result
899 = getSema().Context.getTemplateTypeParmType(T->getDepth()
900 - TemplateArgs.getNumLevels(),
901 T->getIndex(),
902 T->isParameterPack(),
903 T->getName());
904 TemplateTypeParmTypeLoc NewTL = TLB.push<TemplateTypeParmTypeLoc>(Result);
905 NewTL.setNameLoc(TL.getNameLoc());
906 return Result;
Douglas Gregorcd281c32009-02-28 00:25:32 +0000907}
Douglas Gregor99ebf652009-02-27 19:31:52 +0000908
John McCallce3ff2b2009-08-25 22:02:44 +0000909/// \brief Perform substitution on the type T with a given set of template
910/// arguments.
Douglas Gregor99ebf652009-02-27 19:31:52 +0000911///
912/// This routine substitutes the given template arguments into the
913/// type T and produces the instantiated type.
914///
915/// \param T the type into which the template arguments will be
916/// substituted. If this type is not dependent, it will be returned
917/// immediately.
918///
919/// \param TemplateArgs the template arguments that will be
920/// substituted for the top-level template parameters within T.
921///
Douglas Gregor99ebf652009-02-27 19:31:52 +0000922/// \param Loc the location in the source code where this substitution
923/// is being performed. It will typically be the location of the
924/// declarator (if we're instantiating the type of some declaration)
925/// or the location of the type in the source code (if, e.g., we're
926/// instantiating the type of a cast expression).
927///
928/// \param Entity the name of the entity associated with a declaration
929/// being instantiated (if any). May be empty to indicate that there
930/// is no such entity (if, e.g., this is a type that occurs as part of
931/// a cast expression) or that the entity has no name (e.g., an
932/// unnamed function parameter).
933///
934/// \returns If the instantiation succeeds, the instantiated
935/// type. Otherwise, produces diagnostics and returns a NULL type.
John McCallcd7ba1c2009-10-21 00:58:09 +0000936DeclaratorInfo *Sema::SubstType(DeclaratorInfo *T,
937 const MultiLevelTemplateArgumentList &Args,
938 SourceLocation Loc,
939 DeclarationName Entity) {
940 assert(!ActiveTemplateInstantiations.empty() &&
941 "Cannot perform an instantiation without some context on the "
942 "instantiation stack");
943
944 if (!T->getType()->isDependentType())
945 return T;
946
947 TemplateInstantiator Instantiator(*this, Args, Loc, Entity);
948 return Instantiator.TransformType(T);
949}
950
951/// Deprecated form of the above.
Mike Stump1eb44332009-09-09 15:08:12 +0000952QualType Sema::SubstType(QualType T,
Douglas Gregord6350ae2009-08-28 20:31:08 +0000953 const MultiLevelTemplateArgumentList &TemplateArgs,
John McCallce3ff2b2009-08-25 22:02:44 +0000954 SourceLocation Loc, DeclarationName Entity) {
Douglas Gregordf667e72009-03-10 20:44:00 +0000955 assert(!ActiveTemplateInstantiations.empty() &&
956 "Cannot perform an instantiation without some context on the "
957 "instantiation stack");
958
Douglas Gregor99ebf652009-02-27 19:31:52 +0000959 // If T is not a dependent type, there is nothing to do.
960 if (!T->isDependentType())
961 return T;
962
Douglas Gregor577f75a2009-08-04 16:50:30 +0000963 TemplateInstantiator Instantiator(*this, TemplateArgs, Loc, Entity);
964 return Instantiator.TransformType(T);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000965}
Douglas Gregor2943aed2009-03-03 04:44:36 +0000966
John McCallce3ff2b2009-08-25 22:02:44 +0000967/// \brief Perform substitution on the base class specifiers of the
968/// given class template specialization.
Douglas Gregor2943aed2009-03-03 04:44:36 +0000969///
970/// Produces a diagnostic and returns true on error, returns false and
971/// attaches the instantiated base classes to the class template
972/// specialization if successful.
Mike Stump1eb44332009-09-09 15:08:12 +0000973bool
John McCallce3ff2b2009-08-25 22:02:44 +0000974Sema::SubstBaseSpecifiers(CXXRecordDecl *Instantiation,
975 CXXRecordDecl *Pattern,
Douglas Gregord6350ae2009-08-28 20:31:08 +0000976 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor2943aed2009-03-03 04:44:36 +0000977 bool Invalid = false;
Douglas Gregor0ca20ac2009-05-29 18:27:38 +0000978 llvm::SmallVector<CXXBaseSpecifier*, 4> InstantiatedBases;
Mike Stump1eb44332009-09-09 15:08:12 +0000979 for (ClassTemplateSpecializationDecl::base_class_iterator
Douglas Gregord475b8d2009-03-25 21:17:03 +0000980 Base = Pattern->bases_begin(), BaseEnd = Pattern->bases_end();
Douglas Gregor27b152f2009-03-10 18:52:44 +0000981 Base != BaseEnd; ++Base) {
Douglas Gregor2943aed2009-03-03 04:44:36 +0000982 if (!Base->getType()->isDependentType()) {
Fariborz Jahanian71c6e712009-07-22 17:41:53 +0000983 InstantiatedBases.push_back(new (Context) CXXBaseSpecifier(*Base));
Douglas Gregor2943aed2009-03-03 04:44:36 +0000984 continue;
985 }
986
Mike Stump1eb44332009-09-09 15:08:12 +0000987 QualType BaseType = SubstType(Base->getType(),
988 TemplateArgs,
John McCallce3ff2b2009-08-25 22:02:44 +0000989 Base->getSourceRange().getBegin(),
990 DeclarationName());
Douglas Gregor2943aed2009-03-03 04:44:36 +0000991 if (BaseType.isNull()) {
992 Invalid = true;
993 continue;
994 }
995
996 if (CXXBaseSpecifier *InstantiatedBase
Douglas Gregord475b8d2009-03-25 21:17:03 +0000997 = CheckBaseSpecifier(Instantiation,
Douglas Gregor2943aed2009-03-03 04:44:36 +0000998 Base->getSourceRange(),
999 Base->isVirtual(),
1000 Base->getAccessSpecifierAsWritten(),
1001 BaseType,
1002 /*FIXME: Not totally accurate */
1003 Base->getSourceRange().getBegin()))
1004 InstantiatedBases.push_back(InstantiatedBase);
1005 else
1006 Invalid = true;
1007 }
1008
Douglas Gregor27b152f2009-03-10 18:52:44 +00001009 if (!Invalid &&
Jay Foadbeaaccd2009-05-21 09:52:38 +00001010 AttachBaseSpecifiers(Instantiation, InstantiatedBases.data(),
Douglas Gregor2943aed2009-03-03 04:44:36 +00001011 InstantiatedBases.size()))
1012 Invalid = true;
1013
1014 return Invalid;
1015}
1016
Douglas Gregord475b8d2009-03-25 21:17:03 +00001017/// \brief Instantiate the definition of a class from a given pattern.
1018///
1019/// \param PointOfInstantiation The point of instantiation within the
1020/// source code.
1021///
1022/// \param Instantiation is the declaration whose definition is being
1023/// instantiated. This will be either a class template specialization
1024/// or a member class of a class template specialization.
1025///
1026/// \param Pattern is the pattern from which the instantiation
1027/// occurs. This will be either the declaration of a class template or
1028/// the declaration of a member class of a class template.
1029///
1030/// \param TemplateArgs The template arguments to be substituted into
1031/// the pattern.
1032///
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001033/// \param TSK the kind of implicit or explicit instantiation to perform.
Douglas Gregor5842ba92009-08-24 15:23:48 +00001034///
1035/// \param Complain whether to complain if the class cannot be instantiated due
1036/// to the lack of a definition.
1037///
Douglas Gregord475b8d2009-03-25 21:17:03 +00001038/// \returns true if an error occurred, false otherwise.
1039bool
1040Sema::InstantiateClass(SourceLocation PointOfInstantiation,
1041 CXXRecordDecl *Instantiation, CXXRecordDecl *Pattern,
Douglas Gregord6350ae2009-08-28 20:31:08 +00001042 const MultiLevelTemplateArgumentList &TemplateArgs,
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001043 TemplateSpecializationKind TSK,
Douglas Gregor5842ba92009-08-24 15:23:48 +00001044 bool Complain) {
Douglas Gregord475b8d2009-03-25 21:17:03 +00001045 bool Invalid = false;
John McCalle29ba202009-08-20 01:44:21 +00001046
Mike Stump1eb44332009-09-09 15:08:12 +00001047 CXXRecordDecl *PatternDef
Douglas Gregord475b8d2009-03-25 21:17:03 +00001048 = cast_or_null<CXXRecordDecl>(Pattern->getDefinition(Context));
1049 if (!PatternDef) {
Douglas Gregor5842ba92009-08-24 15:23:48 +00001050 if (!Complain) {
1051 // Say nothing
1052 } else if (Pattern == Instantiation->getInstantiatedFromMemberClass()) {
Douglas Gregord475b8d2009-03-25 21:17:03 +00001053 Diag(PointOfInstantiation,
1054 diag::err_implicit_instantiate_member_undefined)
1055 << Context.getTypeDeclType(Instantiation);
1056 Diag(Pattern->getLocation(), diag::note_member_of_template_here);
1057 } else {
Douglas Gregor93dfdb12009-05-13 00:25:59 +00001058 Diag(PointOfInstantiation, diag::err_template_instantiate_undefined)
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001059 << (TSK != TSK_ImplicitInstantiation)
Douglas Gregord475b8d2009-03-25 21:17:03 +00001060 << Context.getTypeDeclType(Instantiation);
1061 Diag(Pattern->getLocation(), diag::note_template_decl_here);
1062 }
1063 return true;
1064 }
1065 Pattern = PatternDef;
1066
Douglas Gregor454885e2009-10-15 15:54:05 +00001067 // \brief Record the point of instantiation.
1068 if (MemberSpecializationInfo *MSInfo
1069 = Instantiation->getMemberSpecializationInfo()) {
1070 MSInfo->setTemplateSpecializationKind(TSK);
1071 MSInfo->setPointOfInstantiation(PointOfInstantiation);
Douglas Gregor972e6ce2009-10-27 06:26:26 +00001072 } else if (ClassTemplateSpecializationDecl *Spec
1073 = dyn_cast<ClassTemplateSpecializationDecl>(Instantiation)) {
1074 Spec->setTemplateSpecializationKind(TSK);
1075 Spec->setPointOfInstantiation(PointOfInstantiation);
Douglas Gregor454885e2009-10-15 15:54:05 +00001076 }
1077
Douglas Gregord048bb72009-03-25 21:23:52 +00001078 InstantiatingTemplate Inst(*this, PointOfInstantiation, Instantiation);
Douglas Gregord475b8d2009-03-25 21:17:03 +00001079 if (Inst)
1080 return true;
1081
1082 // Enter the scope of this instantiation. We don't use
1083 // PushDeclContext because we don't have a scope.
1084 DeclContext *PreviousContext = CurContext;
1085 CurContext = Instantiation;
1086
1087 // Start the definition of this instantiation.
1088 Instantiation->startDefinition();
1089
John McCallce3ff2b2009-08-25 22:02:44 +00001090 // Do substitution on the base class specifiers.
1091 if (SubstBaseSpecifiers(Instantiation, Pattern, TemplateArgs))
Douglas Gregord475b8d2009-03-25 21:17:03 +00001092 Invalid = true;
1093
Douglas Gregor0ca20ac2009-05-29 18:27:38 +00001094 llvm::SmallVector<DeclPtrTy, 4> Fields;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001095 for (RecordDecl::decl_iterator Member = Pattern->decls_begin(),
Mike Stump1eb44332009-09-09 15:08:12 +00001096 MemberEnd = Pattern->decls_end();
Douglas Gregor6ab35242009-04-09 21:40:53 +00001097 Member != MemberEnd; ++Member) {
John McCallce3ff2b2009-08-25 22:02:44 +00001098 Decl *NewMember = SubstDecl(*Member, Instantiation, TemplateArgs);
Douglas Gregord475b8d2009-03-25 21:17:03 +00001099 if (NewMember) {
Douglas Gregor9148c3f2009-11-11 19:13:48 +00001100 if (NewMember->isInvalidDecl()) {
Douglas Gregord475b8d2009-03-25 21:17:03 +00001101 Invalid = true;
Douglas Gregor9148c3f2009-11-11 19:13:48 +00001102 } else if (FieldDecl *Field = dyn_cast<FieldDecl>(NewMember))
Chris Lattnerb28317a2009-03-28 19:18:32 +00001103 Fields.push_back(DeclPtrTy::make(Field));
Anders Carlsson0d8df782009-08-29 19:37:28 +00001104 else if (UsingDecl *UD = dyn_cast<UsingDecl>(NewMember))
1105 Instantiation->addDecl(UD);
Douglas Gregord475b8d2009-03-25 21:17:03 +00001106 } else {
1107 // FIXME: Eventually, a NULL return will mean that one of the
Mike Stump390b4cc2009-05-16 07:39:55 +00001108 // instantiations was a semantic disaster, and we'll want to set Invalid =
1109 // true. For now, we expect to skip some members that we can't yet handle.
Douglas Gregord475b8d2009-03-25 21:17:03 +00001110 }
1111 }
1112
1113 // Finish checking fields.
Chris Lattnerb28317a2009-03-28 19:18:32 +00001114 ActOnFields(0, Instantiation->getLocation(), DeclPtrTy::make(Instantiation),
Jay Foadbeaaccd2009-05-21 09:52:38 +00001115 Fields.data(), Fields.size(), SourceLocation(), SourceLocation(),
Douglas Gregord475b8d2009-03-25 21:17:03 +00001116 0);
Douglas Gregor663b5a02009-10-14 20:14:33 +00001117 if (Instantiation->isInvalidDecl())
1118 Invalid = true;
1119
Douglas Gregord475b8d2009-03-25 21:17:03 +00001120 // Add any implicitly-declared members that we might need.
Douglas Gregor663b5a02009-10-14 20:14:33 +00001121 if (!Invalid)
1122 AddImplicitlyDeclaredMembersToClass(Instantiation);
Douglas Gregord475b8d2009-03-25 21:17:03 +00001123
1124 // Exit the scope of this instantiation.
1125 CurContext = PreviousContext;
1126
Douglas Gregoraba43bb2009-05-26 20:50:29 +00001127 if (!Invalid)
1128 Consumer.HandleTagDeclDefinition(Instantiation);
1129
Douglas Gregord475b8d2009-03-25 21:17:03 +00001130 return Invalid;
1131}
1132
Mike Stump1eb44332009-09-09 15:08:12 +00001133bool
Douglas Gregor2943aed2009-03-03 04:44:36 +00001134Sema::InstantiateClassTemplateSpecialization(
Douglas Gregor972e6ce2009-10-27 06:26:26 +00001135 SourceLocation PointOfInstantiation,
Douglas Gregor2943aed2009-03-03 04:44:36 +00001136 ClassTemplateSpecializationDecl *ClassTemplateSpec,
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001137 TemplateSpecializationKind TSK,
Douglas Gregor5842ba92009-08-24 15:23:48 +00001138 bool Complain) {
Douglas Gregor2943aed2009-03-03 04:44:36 +00001139 // Perform the actual instantiation on the canonical declaration.
1140 ClassTemplateSpec = cast<ClassTemplateSpecializationDecl>(
Argyrios Kyrtzidis97fbaa22009-07-18 00:34:25 +00001141 ClassTemplateSpec->getCanonicalDecl());
Douglas Gregor2943aed2009-03-03 04:44:36 +00001142
Douglas Gregor52604ab2009-09-11 21:19:12 +00001143 // Check whether we have already instantiated or specialized this class
1144 // template specialization.
1145 if (ClassTemplateSpec->getSpecializationKind() != TSK_Undeclared) {
1146 if (ClassTemplateSpec->getSpecializationKind() ==
1147 TSK_ExplicitInstantiationDeclaration &&
1148 TSK == TSK_ExplicitInstantiationDefinition) {
1149 // An explicit instantiation definition follows an explicit instantiation
1150 // declaration (C++0x [temp.explicit]p10); go ahead and perform the
1151 // explicit instantiation.
1152 ClassTemplateSpec->setSpecializationKind(TSK);
Douglas Gregor52604ab2009-09-11 21:19:12 +00001153 return false;
1154 }
1155
1156 // We can only instantiate something that hasn't already been
1157 // instantiated or specialized. Fail without any diagnostics: our
1158 // caller will provide an error message.
Douglas Gregor2943aed2009-03-03 04:44:36 +00001159 return true;
Douglas Gregor52604ab2009-09-11 21:19:12 +00001160 }
Douglas Gregor2943aed2009-03-03 04:44:36 +00001161
Douglas Gregor9eea08b2009-09-15 16:51:42 +00001162 if (ClassTemplateSpec->isInvalidDecl())
1163 return true;
1164
Douglas Gregor2943aed2009-03-03 04:44:36 +00001165 ClassTemplateDecl *Template = ClassTemplateSpec->getSpecializedTemplate();
Douglas Gregord6350ae2009-08-28 20:31:08 +00001166 CXXRecordDecl *Pattern = 0;
Douglas Gregorc8ab2562009-05-31 09:31:02 +00001167
Douglas Gregorc1efb3f2009-06-12 22:31:52 +00001168 // C++ [temp.class.spec.match]p1:
1169 // When a class template is used in a context that requires an
1170 // instantiation of the class, it is necessary to determine
1171 // whether the instantiation is to be generated using the primary
1172 // template or one of the partial specializations. This is done by
1173 // matching the template arguments of the class template
1174 // specialization with the template argument lists of the partial
1175 // specializations.
Douglas Gregor199d9912009-06-05 00:53:49 +00001176 typedef std::pair<ClassTemplatePartialSpecializationDecl *,
1177 TemplateArgumentList *> MatchResult;
1178 llvm::SmallVector<MatchResult, 4> Matched;
Mike Stump1eb44332009-09-09 15:08:12 +00001179 for (llvm::FoldingSet<ClassTemplatePartialSpecializationDecl>::iterator
Douglas Gregorc8ab2562009-05-31 09:31:02 +00001180 Partial = Template->getPartialSpecializations().begin(),
1181 PartialEnd = Template->getPartialSpecializations().end();
1182 Partial != PartialEnd;
1183 ++Partial) {
Douglas Gregorf67875d2009-06-12 18:26:56 +00001184 TemplateDeductionInfo Info(Context);
1185 if (TemplateDeductionResult Result
Mike Stump1eb44332009-09-09 15:08:12 +00001186 = DeduceTemplateArguments(&*Partial,
Douglas Gregorf67875d2009-06-12 18:26:56 +00001187 ClassTemplateSpec->getTemplateArgs(),
1188 Info)) {
1189 // FIXME: Store the failed-deduction information for use in
1190 // diagnostics, later.
1191 (void)Result;
1192 } else {
1193 Matched.push_back(std::make_pair(&*Partial, Info.take()));
1194 }
Douglas Gregorc8ab2562009-05-31 09:31:02 +00001195 }
1196
Douglas Gregored9c0f92009-10-29 00:04:11 +00001197 if (Matched.size() >= 1) {
Douglas Gregorbf4ea562009-09-15 16:23:51 +00001198 llvm::SmallVector<MatchResult, 4>::iterator Best = Matched.begin();
Douglas Gregored9c0f92009-10-29 00:04:11 +00001199 if (Matched.size() == 1) {
1200 // -- If exactly one matching specialization is found, the
1201 // instantiation is generated from that specialization.
1202 // We don't need to do anything for this.
1203 } else {
1204 // -- If more than one matching specialization is found, the
1205 // partial order rules (14.5.4.2) are used to determine
1206 // whether one of the specializations is more specialized
1207 // than the others. If none of the specializations is more
1208 // specialized than all of the other matching
1209 // specializations, then the use of the class template is
1210 // ambiguous and the program is ill-formed.
1211 for (llvm::SmallVector<MatchResult, 4>::iterator P = Best + 1,
1212 PEnd = Matched.end();
1213 P != PEnd; ++P) {
1214 if (getMoreSpecializedPartialSpecialization(P->first, Best->first)
1215 == P->first)
1216 Best = P;
Douglas Gregorbf4ea562009-09-15 16:23:51 +00001217 }
Douglas Gregorbf4ea562009-09-15 16:23:51 +00001218
Douglas Gregored9c0f92009-10-29 00:04:11 +00001219 // Determine if the best partial specialization is more specialized than
1220 // the others.
1221 bool Ambiguous = false;
Douglas Gregorbf4ea562009-09-15 16:23:51 +00001222 for (llvm::SmallVector<MatchResult, 4>::iterator P = Matched.begin(),
1223 PEnd = Matched.end();
Douglas Gregored9c0f92009-10-29 00:04:11 +00001224 P != PEnd; ++P) {
1225 if (P != Best &&
1226 getMoreSpecializedPartialSpecialization(P->first, Best->first)
1227 != Best->first) {
1228 Ambiguous = true;
1229 break;
1230 }
1231 }
1232
1233 if (Ambiguous) {
1234 // Partial ordering did not produce a clear winner. Complain.
1235 ClassTemplateSpec->setInvalidDecl();
1236 Diag(PointOfInstantiation, diag::err_partial_spec_ordering_ambiguous)
1237 << ClassTemplateSpec;
1238
1239 // Print the matching partial specializations.
1240 for (llvm::SmallVector<MatchResult, 4>::iterator P = Matched.begin(),
1241 PEnd = Matched.end();
1242 P != PEnd; ++P)
1243 Diag(P->first->getLocation(), diag::note_partial_spec_match)
1244 << getTemplateArgumentBindingsText(P->first->getTemplateParameters(),
1245 *P->second);
Douglas Gregord6350ae2009-08-28 20:31:08 +00001246
Douglas Gregored9c0f92009-10-29 00:04:11 +00001247 return true;
1248 }
Douglas Gregorbf4ea562009-09-15 16:23:51 +00001249 }
1250
1251 // Instantiate using the best class template partial specialization.
Douglas Gregored9c0f92009-10-29 00:04:11 +00001252 ClassTemplatePartialSpecializationDecl *OrigPartialSpec = Best->first;
1253 while (OrigPartialSpec->getInstantiatedFromMember()) {
1254 // If we've found an explicit specialization of this class template,
1255 // stop here and use that as the pattern.
1256 if (OrigPartialSpec->isMemberSpecialization())
1257 break;
1258
1259 OrigPartialSpec = OrigPartialSpec->getInstantiatedFromMember();
1260 }
1261
1262 Pattern = OrigPartialSpec;
Douglas Gregorbf4ea562009-09-15 16:23:51 +00001263 ClassTemplateSpec->setInstantiationOf(Best->first, Best->second);
Douglas Gregorc1efb3f2009-06-12 22:31:52 +00001264 } else {
1265 // -- If no matches are found, the instantiation is generated
1266 // from the primary template.
Douglas Gregord6350ae2009-08-28 20:31:08 +00001267 ClassTemplateDecl *OrigTemplate = Template;
Douglas Gregorfd056bc2009-10-13 16:30:37 +00001268 while (OrigTemplate->getInstantiatedFromMemberTemplate()) {
1269 // If we've found an explicit specialization of this class template,
1270 // stop here and use that as the pattern.
1271 if (OrigTemplate->isMemberSpecialization())
1272 break;
1273
Douglas Gregord6350ae2009-08-28 20:31:08 +00001274 OrigTemplate = OrigTemplate->getInstantiatedFromMemberTemplate();
Douglas Gregorfd056bc2009-10-13 16:30:37 +00001275 }
1276
Douglas Gregord6350ae2009-08-28 20:31:08 +00001277 Pattern = OrigTemplate->getTemplatedDecl();
Douglas Gregorc8ab2562009-05-31 09:31:02 +00001278 }
Douglas Gregor2943aed2009-03-03 04:44:36 +00001279
Douglas Gregor972e6ce2009-10-27 06:26:26 +00001280 bool Result = InstantiateClass(PointOfInstantiation, ClassTemplateSpec,
1281 Pattern,
1282 getTemplateInstantiationArgs(ClassTemplateSpec),
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001283 TSK,
Douglas Gregor5842ba92009-08-24 15:23:48 +00001284 Complain);
Mike Stump1eb44332009-09-09 15:08:12 +00001285
Douglas Gregor199d9912009-06-05 00:53:49 +00001286 for (unsigned I = 0, N = Matched.size(); I != N; ++I) {
1287 // FIXME: Implement TemplateArgumentList::Destroy!
1288 // if (Matched[I].first != Pattern)
1289 // Matched[I].second->Destroy(Context);
1290 }
Mike Stump1eb44332009-09-09 15:08:12 +00001291
Douglas Gregor199d9912009-06-05 00:53:49 +00001292 return Result;
Douglas Gregor2943aed2009-03-03 04:44:36 +00001293}
Douglas Gregor5953d8b2009-03-19 17:26:29 +00001294
John McCallce3ff2b2009-08-25 22:02:44 +00001295/// \brief Instantiates the definitions of all of the member
1296/// of the given class, which is an instantiation of a class template
1297/// or a member class of a template.
Douglas Gregora58861f2009-05-13 20:28:22 +00001298void
1299Sema::InstantiateClassMembers(SourceLocation PointOfInstantiation,
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001300 CXXRecordDecl *Instantiation,
1301 const MultiLevelTemplateArgumentList &TemplateArgs,
1302 TemplateSpecializationKind TSK) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001303 for (DeclContext::decl_iterator D = Instantiation->decls_begin(),
1304 DEnd = Instantiation->decls_end();
Douglas Gregora58861f2009-05-13 20:28:22 +00001305 D != DEnd; ++D) {
Douglas Gregor0d035142009-10-27 18:42:08 +00001306 bool SuppressNew = false;
Douglas Gregora58861f2009-05-13 20:28:22 +00001307 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(*D)) {
Douglas Gregor0d035142009-10-27 18:42:08 +00001308 if (FunctionDecl *Pattern
1309 = Function->getInstantiatedFromMemberFunction()) {
1310 MemberSpecializationInfo *MSInfo
1311 = Function->getMemberSpecializationInfo();
1312 assert(MSInfo && "No member specialization information?");
1313 if (CheckSpecializationInstantiationRedecl(PointOfInstantiation, TSK,
1314 Function,
1315 MSInfo->getTemplateSpecializationKind(),
1316 MSInfo->getPointOfInstantiation(),
1317 SuppressNew) ||
1318 SuppressNew)
Douglas Gregorf6b11852009-10-08 15:14:33 +00001319 continue;
1320
Douglas Gregor0d035142009-10-27 18:42:08 +00001321 if (Function->getBody())
1322 continue;
1323
1324 if (TSK == TSK_ExplicitInstantiationDefinition) {
1325 // C++0x [temp.explicit]p8:
1326 // An explicit instantiation definition that names a class template
1327 // specialization explicitly instantiates the class template
1328 // specialization and is only an explicit instantiation definition
1329 // of members whose definition is visible at the point of
1330 // instantiation.
1331 if (!Pattern->getBody())
1332 continue;
1333
1334 Function->setTemplateSpecializationKind(TSK, PointOfInstantiation);
1335
1336 InstantiateFunctionDefinition(PointOfInstantiation, Function);
1337 } else {
1338 Function->setTemplateSpecializationKind(TSK, PointOfInstantiation);
1339 }
Douglas Gregorf6b11852009-10-08 15:14:33 +00001340 }
Douglas Gregora58861f2009-05-13 20:28:22 +00001341 } else if (VarDecl *Var = dyn_cast<VarDecl>(*D)) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001342 if (Var->isStaticDataMember()) {
Douglas Gregor0d035142009-10-27 18:42:08 +00001343 MemberSpecializationInfo *MSInfo = Var->getMemberSpecializationInfo();
1344 assert(MSInfo && "No member specialization information?");
1345 if (CheckSpecializationInstantiationRedecl(PointOfInstantiation, TSK,
1346 Var,
1347 MSInfo->getTemplateSpecializationKind(),
1348 MSInfo->getPointOfInstantiation(),
1349 SuppressNew) ||
1350 SuppressNew)
Douglas Gregorf6b11852009-10-08 15:14:33 +00001351 continue;
1352
Douglas Gregor0d035142009-10-27 18:42:08 +00001353 if (TSK == TSK_ExplicitInstantiationDefinition) {
1354 // C++0x [temp.explicit]p8:
1355 // An explicit instantiation definition that names a class template
1356 // specialization explicitly instantiates the class template
1357 // specialization and is only an explicit instantiation definition
1358 // of members whose definition is visible at the point of
1359 // instantiation.
1360 if (!Var->getInstantiatedFromStaticDataMember()
1361 ->getOutOfLineDefinition())
1362 continue;
1363
1364 Var->setTemplateSpecializationKind(TSK, PointOfInstantiation);
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001365 InstantiateStaticDataMemberDefinition(PointOfInstantiation, Var);
Douglas Gregor0d035142009-10-27 18:42:08 +00001366 } else {
1367 Var->setTemplateSpecializationKind(TSK, PointOfInstantiation);
1368 }
1369 }
Douglas Gregora58861f2009-05-13 20:28:22 +00001370 } else if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(*D)) {
Douglas Gregor2db32322009-10-07 23:56:10 +00001371 if (Record->isInjectedClassName())
1372 continue;
1373
Douglas Gregor0d035142009-10-27 18:42:08 +00001374 MemberSpecializationInfo *MSInfo = Record->getMemberSpecializationInfo();
1375 assert(MSInfo && "No member specialization information?");
1376 if (CheckSpecializationInstantiationRedecl(PointOfInstantiation, TSK,
1377 Record,
1378 MSInfo->getTemplateSpecializationKind(),
1379 MSInfo->getPointOfInstantiation(),
1380 SuppressNew) ||
1381 SuppressNew)
Douglas Gregorf6b11852009-10-08 15:14:33 +00001382 continue;
1383
Douglas Gregor0d035142009-10-27 18:42:08 +00001384 CXXRecordDecl *Pattern = Record->getInstantiatedFromMemberClass();
1385 assert(Pattern && "Missing instantiated-from-template information");
1386
1387 if (!Record->getDefinition(Context)) {
1388 if (!Pattern->getDefinition(Context)) {
1389 // C++0x [temp.explicit]p8:
1390 // An explicit instantiation definition that names a class template
1391 // specialization explicitly instantiates the class template
1392 // specialization and is only an explicit instantiation definition
1393 // of members whose definition is visible at the point of
1394 // instantiation.
1395 if (TSK == TSK_ExplicitInstantiationDeclaration) {
1396 MSInfo->setTemplateSpecializationKind(TSK);
1397 MSInfo->setPointOfInstantiation(PointOfInstantiation);
1398 }
1399
1400 continue;
1401 }
1402
1403 InstantiateClass(PointOfInstantiation, Record, Pattern,
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001404 TemplateArgs,
1405 TSK);
Douglas Gregor0d035142009-10-27 18:42:08 +00001406 }
Douglas Gregore9374d52009-10-08 01:19:17 +00001407
Douglas Gregor0d035142009-10-27 18:42:08 +00001408 Pattern = cast_or_null<CXXRecordDecl>(Record->getDefinition(Context));
1409 if (Pattern)
1410 InstantiateClassMembers(PointOfInstantiation, Pattern, TemplateArgs,
1411 TSK);
Douglas Gregora58861f2009-05-13 20:28:22 +00001412 }
1413 }
1414}
1415
1416/// \brief Instantiate the definitions of all of the members of the
1417/// given class template specialization, which was named as part of an
1418/// explicit instantiation.
Mike Stump1eb44332009-09-09 15:08:12 +00001419void
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001420Sema::InstantiateClassTemplateSpecializationMembers(
Douglas Gregora58861f2009-05-13 20:28:22 +00001421 SourceLocation PointOfInstantiation,
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001422 ClassTemplateSpecializationDecl *ClassTemplateSpec,
1423 TemplateSpecializationKind TSK) {
Douglas Gregora58861f2009-05-13 20:28:22 +00001424 // C++0x [temp.explicit]p7:
1425 // An explicit instantiation that names a class template
1426 // specialization is an explicit instantion of the same kind
1427 // (declaration or definition) of each of its members (not
1428 // including members inherited from base classes) that has not
1429 // been previously explicitly specialized in the translation unit
1430 // containing the explicit instantiation, except as described
1431 // below.
1432 InstantiateClassMembers(PointOfInstantiation, ClassTemplateSpec,
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001433 getTemplateInstantiationArgs(ClassTemplateSpec),
1434 TSK);
Douglas Gregora58861f2009-05-13 20:28:22 +00001435}
1436
Mike Stump1eb44332009-09-09 15:08:12 +00001437Sema::OwningStmtResult
Douglas Gregord6350ae2009-08-28 20:31:08 +00001438Sema::SubstStmt(Stmt *S, const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor43959a92009-08-20 07:17:43 +00001439 if (!S)
1440 return Owned(S);
1441
1442 TemplateInstantiator Instantiator(*this, TemplateArgs,
1443 SourceLocation(),
1444 DeclarationName());
1445 return Instantiator.TransformStmt(S);
1446}
1447
Mike Stump1eb44332009-09-09 15:08:12 +00001448Sema::OwningExprResult
Douglas Gregord6350ae2009-08-28 20:31:08 +00001449Sema::SubstExpr(Expr *E, const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregorb98b1992009-08-11 05:31:07 +00001450 if (!E)
1451 return Owned(E);
Mike Stump1eb44332009-09-09 15:08:12 +00001452
Douglas Gregorb98b1992009-08-11 05:31:07 +00001453 TemplateInstantiator Instantiator(*this, TemplateArgs,
1454 SourceLocation(),
1455 DeclarationName());
1456 return Instantiator.TransformExpr(E);
1457}
1458
John McCallce3ff2b2009-08-25 22:02:44 +00001459/// \brief Do template substitution on a nested-name-specifier.
Douglas Gregorab452ba2009-03-26 23:50:42 +00001460NestedNameSpecifier *
John McCallce3ff2b2009-08-25 22:02:44 +00001461Sema::SubstNestedNameSpecifier(NestedNameSpecifier *NNS,
Douglas Gregord6350ae2009-08-28 20:31:08 +00001462 SourceRange Range,
1463 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregordcee1a12009-08-06 05:28:30 +00001464 TemplateInstantiator Instantiator(*this, TemplateArgs, Range.getBegin(),
1465 DeclarationName());
1466 return Instantiator.TransformNestedNameSpecifier(NNS, Range);
Douglas Gregor5953d8b2009-03-19 17:26:29 +00001467}
Douglas Gregorde650ae2009-03-31 18:38:02 +00001468
1469TemplateName
John McCallce3ff2b2009-08-25 22:02:44 +00001470Sema::SubstTemplateName(TemplateName Name, SourceLocation Loc,
Douglas Gregord6350ae2009-08-28 20:31:08 +00001471 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregord1067e52009-08-06 06:41:21 +00001472 TemplateInstantiator Instantiator(*this, TemplateArgs, Loc,
1473 DeclarationName());
1474 return Instantiator.TransformTemplateName(Name);
Douglas Gregorde650ae2009-03-31 18:38:02 +00001475}
Douglas Gregor91333002009-06-11 00:06:24 +00001476
John McCall833ca992009-10-29 08:12:44 +00001477bool Sema::Subst(const TemplateArgumentLoc &Input, TemplateArgumentLoc &Output,
1478 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor670444e2009-08-04 22:27:00 +00001479 TemplateInstantiator Instantiator(*this, TemplateArgs, SourceLocation(),
1480 DeclarationName());
John McCall833ca992009-10-29 08:12:44 +00001481
1482 return Instantiator.TransformTemplateArgument(Input, Output);
Douglas Gregor91333002009-06-11 00:06:24 +00001483}