blob: d65b2465677f14e7d5a1aeb2cd16b05b7980ac08 [file] [log] [blame]
Douglas Gregorfe1e1102009-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 Gregord6ff3322009-08-04 16:50:30 +000014#include "TreeTransform.h"
John McCallb53bbd42009-11-22 01:44:31 +000015#include "Lookup.h"
Douglas Gregor28ad4b52009-05-26 20:50:29 +000016#include "clang/AST/ASTConsumer.h"
Douglas Gregorfe1e1102009-02-27 19:31:52 +000017#include "clang/AST/ASTContext.h"
18#include "clang/AST/Expr.h"
Douglas Gregorfe1e1102009-02-27 19:31:52 +000019#include "clang/AST/DeclTemplate.h"
20#include "clang/Parse/DeclSpec.h"
21#include "clang/Basic/LangOptions.h"
22
23using namespace clang;
24
Douglas Gregor4ea568f2009-03-10 18:03:33 +000025//===----------------------------------------------------------------------===/
26// Template Instantiation Support
27//===----------------------------------------------------------------------===/
28
Douglas Gregor01afeef2009-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 Gregor36d7c5f2009-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 Gregora654dd82009-08-28 17:37:35 +000036MultiLevelTemplateArgumentList
Douglas Gregor36d7c5f2009-11-09 19:17:50 +000037Sema::getTemplateInstantiationArgs(NamedDecl *D,
38 const TemplateArgumentList *Innermost) {
Douglas Gregora654dd82009-08-28 17:37:35 +000039 // Accumulate the set of template argument lists in this structure.
40 MultiLevelTemplateArgumentList Result;
Mike Stump11289f42009-09-09 15:08:12 +000041
Douglas Gregor36d7c5f2009-11-09 19:17:50 +000042 if (Innermost)
43 Result.addOuterTemplateArguments(Innermost);
44
Douglas Gregora654dd82009-08-28 17:37:35 +000045 DeclContext *Ctx = dyn_cast<DeclContext>(D);
46 if (!Ctx)
47 Ctx = D->getDeclContext();
Mike Stump11289f42009-09-09 15:08:12 +000048
John McCall970d5302009-08-29 03:16:09 +000049 while (!Ctx->isFileContext()) {
Douglas Gregora654dd82009-08-28 17:37:35 +000050 // Add template arguments from a class template instantiation.
Mike Stump11289f42009-09-09 15:08:12 +000051 if (ClassTemplateSpecializationDecl *Spec
Douglas Gregora654dd82009-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 Stump11289f42009-09-09 15:08:12 +000056
Douglas Gregora654dd82009-08-28 17:37:35 +000057 Result.addOuterTemplateArguments(&Spec->getTemplateInstantiationArgs());
Douglas Gregorcf915552009-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 Stump11289f42009-09-09 15:08:12 +000064 }
Douglas Gregora654dd82009-08-28 17:37:35 +000065 // Add template arguments from a function template specialization.
John McCall970d5302009-08-29 03:16:09 +000066 else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Ctx)) {
Douglas Gregorcf915552009-10-13 16:30:37 +000067 if (Function->getTemplateSpecializationKind()
68 == TSK_ExplicitSpecialization)
69 break;
70
Douglas Gregora654dd82009-08-28 17:37:35 +000071 if (const TemplateArgumentList *TemplateArgs
Douglas Gregorcf915552009-10-13 16:30:37 +000072 = Function->getTemplateSpecializationArgs()) {
73 // Add the template arguments for this specialization.
Douglas Gregora654dd82009-08-28 17:37:35 +000074 Result.addOuterTemplateArguments(TemplateArgs);
John McCall970d5302009-08-29 03:16:09 +000075
Douglas Gregorcf915552009-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 McCall970d5302009-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 Gregora654dd82009-08-28 17:37:35 +000091 }
John McCall970d5302009-08-29 03:16:09 +000092
93 Ctx = Ctx->getParent();
Douglas Gregorb4850462009-05-14 23:26:13 +000094 }
Mike Stump11289f42009-09-09 15:08:12 +000095
Douglas Gregora654dd82009-08-28 17:37:35 +000096 return Result;
Douglas Gregorb4850462009-05-14 23:26:13 +000097}
98
Douglas Gregor84d49a22009-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 Gregorfcd5db32009-03-10 00:06:19 +0000116Sema::InstantiatingTemplate::
117InstantiatingTemplate(Sema &SemaRef, SourceLocation PointOfInstantiation,
Douglas Gregor85673582009-05-18 17:01:57 +0000118 Decl *Entity,
Douglas Gregorfcd5db32009-03-10 00:06:19 +0000119 SourceRange InstantiationRange)
120 : SemaRef(SemaRef) {
Douglas Gregor79cf6032009-03-10 20:44:00 +0000121
122 Invalid = CheckInstantiationDepth(PointOfInstantiation,
123 InstantiationRange);
124 if (!Invalid) {
Douglas Gregorfcd5db32009-03-10 00:06:19 +0000125 ActiveTemplateInstantiation Inst;
Douglas Gregor79cf6032009-03-10 20:44:00 +0000126 Inst.Kind = ActiveTemplateInstantiation::TemplateInstantiation;
Douglas Gregorfcd5db32009-03-10 00:06:19 +0000127 Inst.PointOfInstantiation = PointOfInstantiation;
Douglas Gregor79cf6032009-03-10 20:44:00 +0000128 Inst.Entity = reinterpret_cast<uintptr_t>(Entity);
Douglas Gregorc9220832009-03-12 18:36:18 +0000129 Inst.TemplateArgs = 0;
130 Inst.NumTemplateArgs = 0;
Douglas Gregor79cf6032009-03-10 20:44:00 +0000131 Inst.InstantiationRange = InstantiationRange;
132 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
Douglas Gregor79cf6032009-03-10 20:44:00 +0000133 }
134}
135
Mike Stump11289f42009-09-09 15:08:12 +0000136Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef,
Douglas Gregor79cf6032009-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 Stump11289f42009-09-09 15:08:12 +0000148 Inst.Kind
Douglas Gregor79cf6032009-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 Gregorfcd5db32009-03-10 00:06:19 +0000154 Inst.InstantiationRange = InstantiationRange;
155 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
Douglas Gregorfcd5db32009-03-10 00:06:19 +0000156 }
157}
158
Mike Stump11289f42009-09-09 15:08:12 +0000159Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef,
Douglas Gregor637d9982009-06-10 23:47:09 +0000160 SourceLocation PointOfInstantiation,
Douglas Gregorff6cbdf2009-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 Stump11289f42009-09-09 15:08:12 +0000167
Douglas Gregorff6cbdf2009-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 Gregor84d49a22009-11-11 21:54:23 +0000179
180 if (!Inst.isInstantiationRecord())
181 ++SemaRef.NonInstantiationEntries;
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000182 }
183}
184
Mike Stump11289f42009-09-09 15:08:12 +0000185Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef,
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000186 SourceLocation PointOfInstantiation,
Douglas Gregor637d9982009-06-10 23:47:09 +0000187 ClassTemplatePartialSpecializationDecl *PartialSpec,
188 const TemplateArgument *TemplateArgs,
189 unsigned NumTemplateArgs,
190 SourceRange InstantiationRange)
191 : SemaRef(SemaRef) {
192
Douglas Gregor84d49a22009-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 Gregor637d9982009-06-10 23:47:09 +0000206}
207
Mike Stump11289f42009-09-09 15:08:12 +0000208Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef,
Douglas Gregore62e6a02009-11-11 19:13:48 +0000209 SourceLocation PointOfInstantiation,
Anders Carlsson657bad42009-09-05 05:14:19 +0000210 ParmVarDecl *Param,
211 const TemplateArgument *TemplateArgs,
212 unsigned NumTemplateArgs,
213 SourceRange InstantiationRange)
214 : SemaRef(SemaRef) {
Mike Stump11289f42009-09-09 15:08:12 +0000215
Douglas Gregore62e6a02009-11-11 19:13:48 +0000216 Invalid = CheckInstantiationDepth(PointOfInstantiation, InstantiationRange);
Anders Carlsson657bad42009-09-05 05:14:19 +0000217
218 if (!Invalid) {
219 ActiveTemplateInstantiation Inst;
220 Inst.Kind
221 = ActiveTemplateInstantiation::DefaultFunctionArgumentInstantiation;
Douglas Gregore62e6a02009-11-11 19:13:48 +0000222 Inst.PointOfInstantiation = PointOfInstantiation;
Anders Carlsson657bad42009-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 Gregore62e6a02009-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 Gregor84d49a22009-11-11 21:54:23 +0000238 Invalid = false;
Douglas Gregore62e6a02009-11-11 19:13:48 +0000239
Douglas Gregor84d49a22009-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 Gregore62e6a02009-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 Gregor84d49a22009-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 Gregore62e6a02009-11-11 19:13:48 +0000271
Douglas Gregor84d49a22009-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 Carlsson657bad42009-09-05 05:14:19 +0000297}
298
Douglas Gregor85673582009-05-18 17:01:57 +0000299void Sema::InstantiatingTemplate::Clear() {
300 if (!Invalid) {
Douglas Gregor84d49a22009-11-11 21:54:23 +0000301 if (!SemaRef.ActiveTemplateInstantiations.back().isInstantiationRecord()) {
302 assert(SemaRef.NonInstantiationEntries > 0);
303 --SemaRef.NonInstantiationEntries;
304 }
305
Douglas Gregorfcd5db32009-03-10 00:06:19 +0000306 SemaRef.ActiveTemplateInstantiations.pop_back();
Douglas Gregor85673582009-05-18 17:01:57 +0000307 Invalid = true;
308 }
Douglas Gregorfcd5db32009-03-10 00:06:19 +0000309}
310
Douglas Gregor79cf6032009-03-10 20:44:00 +0000311bool Sema::InstantiatingTemplate::CheckInstantiationDepth(
312 SourceLocation PointOfInstantiation,
313 SourceRange InstantiationRange) {
Douglas Gregor84d49a22009-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 Gregor79cf6032009-03-10 20:44:00 +0000319 return false;
320
Mike Stump11289f42009-09-09 15:08:12 +0000321 SemaRef.Diag(PointOfInstantiation,
Douglas Gregor79cf6032009-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 Gregor4ea568f2009-03-10 18:03:33 +0000330/// \brief Prints the current instantiation stack through a series of
331/// notes.
332void Sema::PrintInstantiationStack() {
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000333 // FIXME: In all of these cases, we need to show the template arguments
Douglas Gregor4ea568f2009-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 Gregor79cf6032009-03-10 20:44:00 +0000339 switch (Active->Kind) {
340 case ActiveTemplateInstantiation::TemplateInstantiation: {
Douglas Gregor85673582009-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 Stump11289f42009-09-09 15:08:12 +0000346 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
Douglas Gregor85673582009-05-18 17:01:57 +0000347 DiagID)
348 << Context.getTypeDeclType(Record)
349 << Active->InstantiationRange;
Douglas Gregora6ef8f02009-07-24 20:34:43 +0000350 } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
Douglas Gregor4adbc6d2009-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 Stump11289f42009-09-09 15:08:12 +0000356 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
Douglas Gregor85673582009-05-18 17:01:57 +0000357 DiagID)
358 << Function
359 << Active->InstantiationRange;
Douglas Gregora6ef8f02009-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 Gregor85673582009-05-18 17:01:57 +0000365 }
Douglas Gregor79cf6032009-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 Gregordc572a32009-03-30 22:58:21 +0000372 = TemplateSpecializationType::PrintTemplateArgumentList(
Mike Stump11289f42009-09-09 15:08:12 +0000373 Active->TemplateArgs,
Douglas Gregor7de59662009-05-29 20:38:28 +0000374 Active->NumTemplateArgs,
375 Context.PrintingPolicy);
Douglas Gregor79cf6032009-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 Gregor637d9982009-06-10 23:47:09 +0000382
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000383 case ActiveTemplateInstantiation::ExplicitTemplateArgumentSubstitution: {
Mike Stump11289f42009-09-09 15:08:12 +0000384 FunctionTemplateDecl *FnTmpl
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000385 = cast<FunctionTemplateDecl>((Decl *)Active->Entity);
Douglas Gregor637d9982009-06-10 23:47:09 +0000386 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000387 diag::note_explicit_template_arg_substitution_here)
388 << FnTmpl << Active->InstantiationRange;
Douglas Gregor637d9982009-06-10 23:47:09 +0000389 break;
390 }
Mike Stump11289f42009-09-09 15:08:12 +0000391
Douglas Gregorff6cbdf2009-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 Gregor637d9982009-06-10 23:47:09 +0000408
Anders Carlsson657bad42009-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 Stump11289f42009-09-09 15:08:12 +0000412
Anders Carlsson657bad42009-09-05 05:14:19 +0000413 std::string TemplateArgsStr
414 = TemplateSpecializationType::PrintTemplateArgumentList(
Mike Stump11289f42009-09-09 15:08:12 +0000415 Active->TemplateArgs,
Anders Carlsson657bad42009-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 Carlssondc6d2c32009-09-05 05:38:54 +0000420 << (FD->getNameAsString() + TemplateArgsStr)
Anders Carlsson657bad42009-09-05 05:14:19 +0000421 << Active->InstantiationRange;
422 break;
423 }
Mike Stump11289f42009-09-09 15:08:12 +0000424
Douglas Gregore62e6a02009-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 Gregor84d49a22009-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 Gregor79cf6032009-03-10 20:44:00 +0000453 }
Douglas Gregor4ea568f2009-03-10 18:03:33 +0000454 }
455}
456
Douglas Gregor33834512009-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 Gregor84d49a22009-11-11 21:54:23 +0000463 ++Active)
464 {
Douglas Gregor33834512009-06-14 07:33:30 +0000465 switch(Active->Kind) {
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000466 case ActiveTemplateInstantiation::TemplateInstantiation:
Anders Carlsson657bad42009-09-05 05:14:19 +0000467 case ActiveTemplateInstantiation::DefaultFunctionArgumentInstantiation:
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000468 // This is a template instantiation, so there is no SFINAE.
469 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000470
Douglas Gregor33834512009-06-14 07:33:30 +0000471 case ActiveTemplateInstantiation::DefaultTemplateArgumentInstantiation:
Douglas Gregore62e6a02009-11-11 19:13:48 +0000472 case ActiveTemplateInstantiation::PriorTemplateArgumentSubstitution:
Douglas Gregor84d49a22009-11-11 21:54:23 +0000473 case ActiveTemplateInstantiation::DefaultTemplateArgumentChecking:
Douglas Gregore62e6a02009-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 Gregor33834512009-06-14 07:33:30 +0000477 break;
Mike Stump11289f42009-09-09 15:08:12 +0000478
Douglas Gregorff6cbdf2009-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 Gregor33834512009-06-14 07:33:30 +0000484 }
485 }
486
487 return false;
488}
489
Douglas Gregorfe1e1102009-02-27 19:31:52 +0000490//===----------------------------------------------------------------------===/
491// Template Instantiation for Types
492//===----------------------------------------------------------------------===/
Douglas Gregor17c0d7b2009-02-28 00:25:32 +0000493namespace {
Benjamin Kramer337e3a52009-11-28 19:45:26 +0000494 class TemplateInstantiator
Mike Stump11289f42009-09-09 15:08:12 +0000495 : public TreeTransform<TemplateInstantiator> {
Douglas Gregor01afeef2009-08-28 20:31:08 +0000496 const MultiLevelTemplateArgumentList &TemplateArgs;
Douglas Gregor17c0d7b2009-02-28 00:25:32 +0000497 SourceLocation Loc;
498 DeclarationName Entity;
Douglas Gregorfe1e1102009-02-27 19:31:52 +0000499
Douglas Gregor17c0d7b2009-02-28 00:25:32 +0000500 public:
Douglas Gregorebe10102009-08-20 07:17:43 +0000501 typedef TreeTransform<TemplateInstantiator> inherited;
Mike Stump11289f42009-09-09 15:08:12 +0000502
503 TemplateInstantiator(Sema &SemaRef,
Douglas Gregor01afeef2009-08-28 20:31:08 +0000504 const MultiLevelTemplateArgumentList &TemplateArgs,
Douglas Gregord6ff3322009-08-04 16:50:30 +0000505 SourceLocation Loc,
Mike Stump11289f42009-09-09 15:08:12 +0000506 DeclarationName Entity)
507 : inherited(SemaRef), TemplateArgs(TemplateArgs), Loc(Loc),
Douglas Gregorebe10102009-08-20 07:17:43 +0000508 Entity(Entity) { }
Douglas Gregor17c0d7b2009-02-28 00:25:32 +0000509
Mike Stump11289f42009-09-09 15:08:12 +0000510 /// \brief Determine whether the given type \p T has already been
Douglas Gregord6ff3322009-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 Gregorf61eca92009-05-13 18:28:20 +0000517 }
Mike Stump11289f42009-09-09 15:08:12 +0000518
Douglas Gregord6ff3322009-08-04 16:50:30 +0000519 /// \brief Returns the location of the entity being instantiated, if known.
520 SourceLocation getBaseLocation() { return Loc; }
Mike Stump11289f42009-09-09 15:08:12 +0000521
Douglas Gregord6ff3322009-08-04 16:50:30 +0000522 /// \brief Returns the name of the entity being instantiated, if any.
523 DeclarationName getBaseEntity() { return Entity; }
Mike Stump11289f42009-09-09 15:08:12 +0000524
Douglas Gregoref6ab412009-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 Gregord6ff3322009-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 Gregora16548e2009-08-11 05:31:07 +0000535
Mike Stump11289f42009-09-09 15:08:12 +0000536 /// \brief Transform the definition of the given declaration by
Douglas Gregorebe10102009-08-20 07:17:43 +0000537 /// instantiating it.
538 Decl *TransformDefinition(Decl *D);
Mike Stump11289f42009-09-09 15:08:12 +0000539
Douglas Gregora5cb6da2009-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 Gregorebe10102009-08-20 07:17:43 +0000544 /// \brief Rebuild the exception declaration and register the declaration
545 /// as an instantiated local.
Mike Stump11289f42009-09-09 15:08:12 +0000546 VarDecl *RebuildExceptionDecl(VarDecl *ExceptionDecl, QualType T,
John McCallbcd03502009-12-07 02:54:59 +0000547 TypeSourceInfo *Declarator,
Douglas Gregorebe10102009-08-20 07:17:43 +0000548 IdentifierInfo *Name,
549 SourceLocation Loc, SourceRange TypeRange);
Mike Stump11289f42009-09-09 15:08:12 +0000550
John McCall7f41d982009-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
John McCall47f29ea2009-12-08 09:21:05 +0000555 Sema::OwningExprResult TransformPredefinedExpr(PredefinedExpr *E);
556 Sema::OwningExprResult TransformDeclRefExpr(DeclRefExpr *E);
Mike Stump11289f42009-09-09 15:08:12 +0000557
John McCall47f29ea2009-12-08 09:21:05 +0000558 Sema::OwningExprResult TransformCXXDefaultArgExpr(CXXDefaultArgExpr *E);
Sebastian Redl14236c82009-11-08 13:56:19 +0000559
Mike Stump11289f42009-09-09 15:08:12 +0000560 /// \brief Transforms a template type parameter type by performing
Douglas Gregord6ff3322009-08-04 16:50:30 +0000561 /// substitution of the corresponding template type argument.
John McCall550e0c22009-10-21 00:40:46 +0000562 QualType TransformTemplateTypeParmType(TypeLocBuilder &TLB,
563 TemplateTypeParmTypeLoc TL);
Douglas Gregord6ff3322009-08-04 16:50:30 +0000564 };
Douglas Gregor04318252009-07-06 15:59:29 +0000565}
566
Douglas Gregord6ff3322009-08-04 16:50:30 +0000567Decl *TemplateInstantiator::TransformDecl(Decl *D) {
Douglas Gregor2b6ca462009-09-03 21:38:09 +0000568 if (!D)
569 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000570
Douglas Gregor2b6ca462009-09-03 21:38:09 +0000571 if (TemplateTemplateParmDecl *TTP = dyn_cast<TemplateTemplateParmDecl>(D)) {
Douglas Gregor01afeef2009-08-28 20:31:08 +0000572 if (TTP->getDepth() < TemplateArgs.getNumLevels()) {
Douglas Gregor9167f8b2009-11-11 01:00:40 +0000573 TemplateName Template
574 = TemplateArgs(TTP->getDepth(), TTP->getPosition()).getAsTemplate();
575 assert(!Template.isNull() && Template.getAsTemplateDecl() &&
Douglas Gregor01afeef2009-08-28 20:31:08 +0000576 "Wrong kind of template template argument");
Douglas Gregor9167f8b2009-11-11 01:00:40 +0000577 return Template.getAsTemplateDecl();
Douglas Gregor01afeef2009-08-28 20:31:08 +0000578 }
Mike Stump11289f42009-09-09 15:08:12 +0000579
580 // If the corresponding template argument is NULL or non-existent, it's
581 // because we are performing instantiation from explicitly-specified
Douglas Gregor01afeef2009-08-28 20:31:08 +0000582 // template arguments in a function template, but there were some
583 // arguments left unspecified.
Mike Stump11289f42009-09-09 15:08:12 +0000584 if (!TemplateArgs.hasTemplateArgument(TTP->getDepth(),
Douglas Gregor01afeef2009-08-28 20:31:08 +0000585 TTP->getPosition()))
586 return D;
Mike Stump11289f42009-09-09 15:08:12 +0000587
Douglas Gregor9167f8b2009-11-11 01:00:40 +0000588 // Fall through to find the instantiated declaration for this template
589 // template parameter.
Douglas Gregor71dc5092009-08-06 06:41:21 +0000590 }
Mike Stump11289f42009-09-09 15:08:12 +0000591
Douglas Gregor64621e62009-09-16 18:34:49 +0000592 return SemaRef.FindInstantiatedDecl(cast<NamedDecl>(D), TemplateArgs);
Douglas Gregord6ff3322009-08-04 16:50:30 +0000593}
594
Douglas Gregorebe10102009-08-20 07:17:43 +0000595Decl *TemplateInstantiator::TransformDefinition(Decl *D) {
John McCall76d824f2009-08-25 22:02:44 +0000596 Decl *Inst = getSema().SubstDecl(D, getSema().CurContext, TemplateArgs);
Douglas Gregorebe10102009-08-20 07:17:43 +0000597 if (!Inst)
598 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000599
Douglas Gregorebe10102009-08-20 07:17:43 +0000600 getSema().CurrentInstantiationScope->InstantiatedLocal(D, Inst);
601 return Inst;
602}
603
Douglas Gregora5cb6da2009-10-20 05:58:46 +0000604NamedDecl *
605TemplateInstantiator::TransformFirstQualifierInScope(NamedDecl *D,
606 SourceLocation Loc) {
607 // If the first part of the nested-name-specifier was a template type
608 // parameter, instantiate that type parameter down to a tag type.
609 if (TemplateTypeParmDecl *TTPD = dyn_cast_or_null<TemplateTypeParmDecl>(D)) {
610 const TemplateTypeParmType *TTP
611 = cast<TemplateTypeParmType>(getSema().Context.getTypeDeclType(TTPD));
612 if (TTP->getDepth() < TemplateArgs.getNumLevels()) {
613 QualType T = TemplateArgs(TTP->getDepth(), TTP->getIndex()).getAsType();
614 if (T.isNull())
615 return cast_or_null<NamedDecl>(TransformDecl(D));
616
617 if (const TagType *Tag = T->getAs<TagType>())
618 return Tag->getDecl();
619
620 // The resulting type is not a tag; complain.
621 getSema().Diag(Loc, diag::err_nested_name_spec_non_tag) << T;
622 return 0;
623 }
624 }
625
626 return cast_or_null<NamedDecl>(TransformDecl(D));
627}
628
Douglas Gregorebe10102009-08-20 07:17:43 +0000629VarDecl *
630TemplateInstantiator::RebuildExceptionDecl(VarDecl *ExceptionDecl,
Mike Stump11289f42009-09-09 15:08:12 +0000631 QualType T,
John McCallbcd03502009-12-07 02:54:59 +0000632 TypeSourceInfo *Declarator,
Douglas Gregorebe10102009-08-20 07:17:43 +0000633 IdentifierInfo *Name,
Mike Stump11289f42009-09-09 15:08:12 +0000634 SourceLocation Loc,
Douglas Gregorebe10102009-08-20 07:17:43 +0000635 SourceRange TypeRange) {
636 VarDecl *Var = inherited::RebuildExceptionDecl(ExceptionDecl, T, Declarator,
637 Name, Loc, TypeRange);
638 if (Var && !Var->isInvalidDecl())
639 getSema().CurrentInstantiationScope->InstantiatedLocal(ExceptionDecl, Var);
640 return Var;
641}
642
John McCall7f41d982009-09-11 04:59:25 +0000643QualType
644TemplateInstantiator::RebuildElaboratedType(QualType T,
645 ElaboratedType::TagKind Tag) {
646 if (const TagType *TT = T->getAs<TagType>()) {
647 TagDecl* TD = TT->getDecl();
648
649 // FIXME: this location is very wrong; we really need typelocs.
650 SourceLocation TagLocation = TD->getTagKeywordLoc();
651
652 // FIXME: type might be anonymous.
653 IdentifierInfo *Id = TD->getIdentifier();
654
655 // TODO: should we even warn on struct/class mismatches for this? Seems
656 // like it's likely to produce a lot of spurious errors.
657 if (!SemaRef.isAcceptableTagRedeclaration(TD, Tag, TagLocation, *Id)) {
658 SemaRef.Diag(TagLocation, diag::err_use_with_wrong_tag)
659 << Id
660 << CodeModificationHint::CreateReplacement(SourceRange(TagLocation),
661 TD->getKindName());
662 SemaRef.Diag(TD->getLocation(), diag::note_previous_use);
663 }
664 }
665
666 return TreeTransform<TemplateInstantiator>::RebuildElaboratedType(T, Tag);
667}
668
669Sema::OwningExprResult
John McCall47f29ea2009-12-08 09:21:05 +0000670TemplateInstantiator::TransformPredefinedExpr(PredefinedExpr *E) {
Anders Carlsson0b209a82009-09-11 01:22:35 +0000671 if (!E->isTypeDependent())
672 return SemaRef.Owned(E->Retain());
673
674 FunctionDecl *currentDecl = getSema().getCurFunctionDecl();
675 assert(currentDecl && "Must have current function declaration when "
676 "instantiating.");
677
678 PredefinedExpr::IdentType IT = E->getIdentType();
679
680 unsigned Length =
681 PredefinedExpr::ComputeName(getSema().Context, IT, currentDecl).length();
682
683 llvm::APInt LengthI(32, Length + 1);
John McCall8ccfcb52009-09-24 19:53:00 +0000684 QualType ResTy = getSema().Context.CharTy.withConst();
Anders Carlsson0b209a82009-09-11 01:22:35 +0000685 ResTy = getSema().Context.getConstantArrayType(ResTy, LengthI,
686 ArrayType::Normal, 0);
687 PredefinedExpr *PE =
688 new (getSema().Context) PredefinedExpr(E->getLocation(), ResTy, IT);
689 return getSema().Owned(PE);
690}
691
692Sema::OwningExprResult
John McCall47f29ea2009-12-08 09:21:05 +0000693TemplateInstantiator::TransformDeclRefExpr(DeclRefExpr *E) {
Douglas Gregora16548e2009-08-11 05:31:07 +0000694 // FIXME: Clean this up a bit
695 NamedDecl *D = E->getDecl();
696 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(D)) {
Douglas Gregor954de172009-10-31 17:21:17 +0000697 if (NTTP->getDepth() < TemplateArgs.getNumLevels()) {
Douglas Gregor954de172009-10-31 17:21:17 +0000698 // If the corresponding template argument is NULL or non-existent, it's
699 // because we are performing instantiation from explicitly-specified
700 // template arguments in a function template, but there were some
701 // arguments left unspecified.
702 if (!TemplateArgs.hasTemplateArgument(NTTP->getDepth(),
703 NTTP->getPosition()))
704 return SemaRef.Owned(E->Retain());
Mike Stump11289f42009-09-09 15:08:12 +0000705
Douglas Gregor954de172009-10-31 17:21:17 +0000706 const TemplateArgument &Arg = TemplateArgs(NTTP->getDepth(),
707 NTTP->getPosition());
Mike Stump11289f42009-09-09 15:08:12 +0000708
Douglas Gregor954de172009-10-31 17:21:17 +0000709 // The template argument itself might be an expression, in which
710 // case we just return that expression.
711 if (Arg.getKind() == TemplateArgument::Expression)
712 return SemaRef.Owned(Arg.getAsExpr()->Retain());
Mike Stump11289f42009-09-09 15:08:12 +0000713
Douglas Gregor954de172009-10-31 17:21:17 +0000714 if (Arg.getKind() == TemplateArgument::Declaration) {
715 ValueDecl *VD = cast<ValueDecl>(Arg.getAsDecl());
Mike Stump11289f42009-09-09 15:08:12 +0000716
Douglas Gregor954de172009-10-31 17:21:17 +0000717 VD = cast_or_null<ValueDecl>(
Douglas Gregorc95a1fa2009-11-04 07:01:15 +0000718 getSema().FindInstantiatedDecl(VD, TemplateArgs));
Douglas Gregor954de172009-10-31 17:21:17 +0000719 if (!VD)
720 return SemaRef.ExprError();
Mike Stump11289f42009-09-09 15:08:12 +0000721
Douglas Gregor4e948ce2009-11-12 17:40:13 +0000722 if (VD->getDeclContext()->isRecord()) {
723 // If the value is a class member, we might have a pointer-to-member.
724 // Determine whether the non-type template template parameter is of
725 // pointer-to-member type. If so, we need to build an appropriate
726 // expression for a pointer-to-member, since a "normal" DeclRefExpr
727 // would refer to the member itself.
728 if (NTTP->getType()->isMemberPointerType()) {
729 QualType ClassType
730 = SemaRef.Context.getTypeDeclType(
731 cast<RecordDecl>(VD->getDeclContext()));
732 NestedNameSpecifier *Qualifier
733 = NestedNameSpecifier::Create(SemaRef.Context, 0, false,
734 ClassType.getTypePtr());
735 CXXScopeSpec SS;
736 SS.setScopeRep(Qualifier);
737 OwningExprResult RefExpr
738 = SemaRef.BuildDeclRefExpr(VD,
739 VD->getType().getNonReferenceType(),
740 E->getLocation(),
Douglas Gregor4e948ce2009-11-12 17:40:13 +0000741 &SS);
742 if (RefExpr.isInvalid())
743 return SemaRef.ExprError();
744
745 return SemaRef.CreateBuiltinUnaryOp(E->getLocation(),
746 UnaryOperator::AddrOf,
747 move(RefExpr));
748 }
749 }
750
751 return SemaRef.BuildDeclRefExpr(VD, VD->getType().getNonReferenceType(),
Douglas Gregored6c7442009-11-23 11:41:28 +0000752 E->getLocation());
Douglas Gregor954de172009-10-31 17:21:17 +0000753 }
Mike Stump11289f42009-09-09 15:08:12 +0000754
Douglas Gregor954de172009-10-31 17:21:17 +0000755 assert(Arg.getKind() == TemplateArgument::Integral);
756 QualType T = Arg.getIntegralType();
757 if (T->isCharType() || T->isWideCharType())
758 return SemaRef.Owned(new (SemaRef.Context) CharacterLiteral(
759 Arg.getAsIntegral()->getZExtValue(),
760 T->isWideCharType(),
Mike Stump11289f42009-09-09 15:08:12 +0000761 T,
Douglas Gregor01afeef2009-08-28 20:31:08 +0000762 E->getSourceRange().getBegin()));
Douglas Gregor954de172009-10-31 17:21:17 +0000763 if (T->isBooleanType())
764 return SemaRef.Owned(new (SemaRef.Context) CXXBoolLiteralExpr(
765 Arg.getAsIntegral()->getBoolValue(),
766 T,
767 E->getSourceRange().getBegin()));
768
769 assert(Arg.getAsIntegral()->getBitWidth() == SemaRef.Context.getIntWidth(T));
770 return SemaRef.Owned(new (SemaRef.Context) IntegerLiteral(
771 *Arg.getAsIntegral(),
772 T,
773 E->getSourceRange().getBegin()));
774 }
775
776 // We have a non-type template parameter that isn't fully substituted;
777 // FindInstantiatedDecl will find it in the local instantiation scope.
Douglas Gregora16548e2009-08-11 05:31:07 +0000778 }
Mike Stump11289f42009-09-09 15:08:12 +0000779
John McCall47f29ea2009-12-08 09:21:05 +0000780 return TreeTransform<TemplateInstantiator>::TransformDeclRefExpr(E);
Douglas Gregora16548e2009-08-11 05:31:07 +0000781}
782
Sebastian Redl14236c82009-11-08 13:56:19 +0000783Sema::OwningExprResult TemplateInstantiator::TransformCXXDefaultArgExpr(
John McCall47f29ea2009-12-08 09:21:05 +0000784 CXXDefaultArgExpr *E) {
Sebastian Redl14236c82009-11-08 13:56:19 +0000785 assert(!cast<FunctionDecl>(E->getParam()->getDeclContext())->
786 getDescribedFunctionTemplate() &&
787 "Default arg expressions are never formed in dependent cases.");
Douglas Gregor033f6752009-12-23 23:03:06 +0000788 return SemaRef.BuildCXXDefaultArgExpr(E->getUsedLocation(),
789 cast<FunctionDecl>(E->getParam()->getDeclContext()),
790 E->getParam());
Sebastian Redl14236c82009-11-08 13:56:19 +0000791}
792
793
Mike Stump11289f42009-09-09 15:08:12 +0000794QualType
John McCall550e0c22009-10-21 00:40:46 +0000795TemplateInstantiator::TransformTemplateTypeParmType(TypeLocBuilder &TLB,
796 TemplateTypeParmTypeLoc TL) {
797 TemplateTypeParmType *T = TL.getTypePtr();
Douglas Gregor01afeef2009-08-28 20:31:08 +0000798 if (T->getDepth() < TemplateArgs.getNumLevels()) {
Douglas Gregorfe1e1102009-02-27 19:31:52 +0000799 // Replace the template type parameter with its corresponding
800 // template argument.
Mike Stump11289f42009-09-09 15:08:12 +0000801
802 // If the corresponding template argument is NULL or doesn't exist, it's
803 // because we are performing instantiation from explicitly-specified
804 // template arguments in a function template class, but there were some
Douglas Gregore3f1f352009-07-01 00:28:38 +0000805 // arguments left unspecified.
John McCall550e0c22009-10-21 00:40:46 +0000806 if (!TemplateArgs.hasTemplateArgument(T->getDepth(), T->getIndex())) {
807 TemplateTypeParmTypeLoc NewTL
808 = TLB.push<TemplateTypeParmTypeLoc>(TL.getType());
809 NewTL.setNameLoc(TL.getNameLoc());
810 return TL.getType();
811 }
Mike Stump11289f42009-09-09 15:08:12 +0000812
813 assert(TemplateArgs(T->getDepth(), T->getIndex()).getKind()
Douglas Gregor01afeef2009-08-28 20:31:08 +0000814 == TemplateArgument::Type &&
Douglas Gregorfe1e1102009-02-27 19:31:52 +0000815 "Template argument kind mismatch");
Douglas Gregor01afeef2009-08-28 20:31:08 +0000816
John McCallcebee162009-10-18 09:09:24 +0000817 QualType Replacement
818 = TemplateArgs(T->getDepth(), T->getIndex()).getAsType();
819
820 // TODO: only do this uniquing once, at the start of instantiation.
John McCall550e0c22009-10-21 00:40:46 +0000821 QualType Result
822 = getSema().Context.getSubstTemplateTypeParmType(T, Replacement);
823 SubstTemplateTypeParmTypeLoc NewTL
824 = TLB.push<SubstTemplateTypeParmTypeLoc>(Result);
825 NewTL.setNameLoc(TL.getNameLoc());
826 return Result;
Mike Stump11289f42009-09-09 15:08:12 +0000827 }
Douglas Gregorfe1e1102009-02-27 19:31:52 +0000828
829 // The template type parameter comes from an inner template (e.g.,
830 // the template parameter list of a member template inside the
831 // template we are instantiating). Create a new template type
832 // parameter with the template "level" reduced by one.
John McCall550e0c22009-10-21 00:40:46 +0000833 QualType Result
834 = getSema().Context.getTemplateTypeParmType(T->getDepth()
835 - TemplateArgs.getNumLevels(),
836 T->getIndex(),
837 T->isParameterPack(),
838 T->getName());
839 TemplateTypeParmTypeLoc NewTL = TLB.push<TemplateTypeParmTypeLoc>(Result);
840 NewTL.setNameLoc(TL.getNameLoc());
841 return Result;
Douglas Gregor17c0d7b2009-02-28 00:25:32 +0000842}
Douglas Gregorfe1e1102009-02-27 19:31:52 +0000843
John McCall76d824f2009-08-25 22:02:44 +0000844/// \brief Perform substitution on the type T with a given set of template
845/// arguments.
Douglas Gregorfe1e1102009-02-27 19:31:52 +0000846///
847/// This routine substitutes the given template arguments into the
848/// type T and produces the instantiated type.
849///
850/// \param T the type into which the template arguments will be
851/// substituted. If this type is not dependent, it will be returned
852/// immediately.
853///
854/// \param TemplateArgs the template arguments that will be
855/// substituted for the top-level template parameters within T.
856///
Douglas Gregorfe1e1102009-02-27 19:31:52 +0000857/// \param Loc the location in the source code where this substitution
858/// is being performed. It will typically be the location of the
859/// declarator (if we're instantiating the type of some declaration)
860/// or the location of the type in the source code (if, e.g., we're
861/// instantiating the type of a cast expression).
862///
863/// \param Entity the name of the entity associated with a declaration
864/// being instantiated (if any). May be empty to indicate that there
865/// is no such entity (if, e.g., this is a type that occurs as part of
866/// a cast expression) or that the entity has no name (e.g., an
867/// unnamed function parameter).
868///
869/// \returns If the instantiation succeeds, the instantiated
870/// type. Otherwise, produces diagnostics and returns a NULL type.
John McCallbcd03502009-12-07 02:54:59 +0000871TypeSourceInfo *Sema::SubstType(TypeSourceInfo *T,
John McCall609459e2009-10-21 00:58:09 +0000872 const MultiLevelTemplateArgumentList &Args,
873 SourceLocation Loc,
874 DeclarationName Entity) {
875 assert(!ActiveTemplateInstantiations.empty() &&
876 "Cannot perform an instantiation without some context on the "
877 "instantiation stack");
878
879 if (!T->getType()->isDependentType())
880 return T;
881
882 TemplateInstantiator Instantiator(*this, Args, Loc, Entity);
883 return Instantiator.TransformType(T);
884}
885
886/// Deprecated form of the above.
Mike Stump11289f42009-09-09 15:08:12 +0000887QualType Sema::SubstType(QualType T,
Douglas Gregor01afeef2009-08-28 20:31:08 +0000888 const MultiLevelTemplateArgumentList &TemplateArgs,
John McCall76d824f2009-08-25 22:02:44 +0000889 SourceLocation Loc, DeclarationName Entity) {
Douglas Gregor79cf6032009-03-10 20:44:00 +0000890 assert(!ActiveTemplateInstantiations.empty() &&
891 "Cannot perform an instantiation without some context on the "
892 "instantiation stack");
893
Douglas Gregorfe1e1102009-02-27 19:31:52 +0000894 // If T is not a dependent type, there is nothing to do.
895 if (!T->isDependentType())
896 return T;
897
Douglas Gregord6ff3322009-08-04 16:50:30 +0000898 TemplateInstantiator Instantiator(*this, TemplateArgs, Loc, Entity);
899 return Instantiator.TransformType(T);
Douglas Gregorfe1e1102009-02-27 19:31:52 +0000900}
Douglas Gregor463421d2009-03-03 04:44:36 +0000901
John McCall76d824f2009-08-25 22:02:44 +0000902/// \brief Perform substitution on the base class specifiers of the
903/// given class template specialization.
Douglas Gregor463421d2009-03-03 04:44:36 +0000904///
905/// Produces a diagnostic and returns true on error, returns false and
906/// attaches the instantiated base classes to the class template
907/// specialization if successful.
Mike Stump11289f42009-09-09 15:08:12 +0000908bool
John McCall76d824f2009-08-25 22:02:44 +0000909Sema::SubstBaseSpecifiers(CXXRecordDecl *Instantiation,
910 CXXRecordDecl *Pattern,
Douglas Gregor01afeef2009-08-28 20:31:08 +0000911 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor463421d2009-03-03 04:44:36 +0000912 bool Invalid = false;
Douglas Gregor6181ded2009-05-29 18:27:38 +0000913 llvm::SmallVector<CXXBaseSpecifier*, 4> InstantiatedBases;
Mike Stump11289f42009-09-09 15:08:12 +0000914 for (ClassTemplateSpecializationDecl::base_class_iterator
Douglas Gregor8ea8fd42009-03-25 21:17:03 +0000915 Base = Pattern->bases_begin(), BaseEnd = Pattern->bases_end();
Douglas Gregor2a72edd2009-03-10 18:52:44 +0000916 Base != BaseEnd; ++Base) {
Douglas Gregor463421d2009-03-03 04:44:36 +0000917 if (!Base->getType()->isDependentType()) {
Anders Carlssonae3c5cf2009-12-03 17:49:57 +0000918 const CXXRecordDecl *BaseDecl =
919 cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
920
921 // Make sure to set the attributes from the base.
922 SetClassDeclAttributesFromBase(Instantiation, BaseDecl,
923 Base->isVirtual());
924
Fariborz Jahanian5c14ec32009-07-22 17:41:53 +0000925 InstantiatedBases.push_back(new (Context) CXXBaseSpecifier(*Base));
Douglas Gregor463421d2009-03-03 04:44:36 +0000926 continue;
927 }
928
Mike Stump11289f42009-09-09 15:08:12 +0000929 QualType BaseType = SubstType(Base->getType(),
930 TemplateArgs,
John McCall76d824f2009-08-25 22:02:44 +0000931 Base->getSourceRange().getBegin(),
932 DeclarationName());
Douglas Gregor463421d2009-03-03 04:44:36 +0000933 if (BaseType.isNull()) {
934 Invalid = true;
935 continue;
936 }
937
938 if (CXXBaseSpecifier *InstantiatedBase
Douglas Gregor8ea8fd42009-03-25 21:17:03 +0000939 = CheckBaseSpecifier(Instantiation,
Douglas Gregor463421d2009-03-03 04:44:36 +0000940 Base->getSourceRange(),
941 Base->isVirtual(),
942 Base->getAccessSpecifierAsWritten(),
943 BaseType,
944 /*FIXME: Not totally accurate */
945 Base->getSourceRange().getBegin()))
946 InstantiatedBases.push_back(InstantiatedBase);
947 else
948 Invalid = true;
949 }
950
Douglas Gregor2a72edd2009-03-10 18:52:44 +0000951 if (!Invalid &&
Jay Foad7d0479f2009-05-21 09:52:38 +0000952 AttachBaseSpecifiers(Instantiation, InstantiatedBases.data(),
Douglas Gregor463421d2009-03-03 04:44:36 +0000953 InstantiatedBases.size()))
954 Invalid = true;
955
956 return Invalid;
957}
958
Douglas Gregor8ea8fd42009-03-25 21:17:03 +0000959/// \brief Instantiate the definition of a class from a given pattern.
960///
961/// \param PointOfInstantiation The point of instantiation within the
962/// source code.
963///
964/// \param Instantiation is the declaration whose definition is being
965/// instantiated. This will be either a class template specialization
966/// or a member class of a class template specialization.
967///
968/// \param Pattern is the pattern from which the instantiation
969/// occurs. This will be either the declaration of a class template or
970/// the declaration of a member class of a class template.
971///
972/// \param TemplateArgs The template arguments to be substituted into
973/// the pattern.
974///
Douglas Gregor34ec2ef2009-09-04 22:48:11 +0000975/// \param TSK the kind of implicit or explicit instantiation to perform.
Douglas Gregor8a2e6012009-08-24 15:23:48 +0000976///
977/// \param Complain whether to complain if the class cannot be instantiated due
978/// to the lack of a definition.
979///
Douglas Gregor8ea8fd42009-03-25 21:17:03 +0000980/// \returns true if an error occurred, false otherwise.
981bool
982Sema::InstantiateClass(SourceLocation PointOfInstantiation,
983 CXXRecordDecl *Instantiation, CXXRecordDecl *Pattern,
Douglas Gregor01afeef2009-08-28 20:31:08 +0000984 const MultiLevelTemplateArgumentList &TemplateArgs,
Douglas Gregor34ec2ef2009-09-04 22:48:11 +0000985 TemplateSpecializationKind TSK,
Douglas Gregor8a2e6012009-08-24 15:23:48 +0000986 bool Complain) {
Douglas Gregor8ea8fd42009-03-25 21:17:03 +0000987 bool Invalid = false;
John McCall87a44eb2009-08-20 01:44:21 +0000988
Mike Stump11289f42009-09-09 15:08:12 +0000989 CXXRecordDecl *PatternDef
Douglas Gregor8ea8fd42009-03-25 21:17:03 +0000990 = cast_or_null<CXXRecordDecl>(Pattern->getDefinition(Context));
991 if (!PatternDef) {
Douglas Gregor8a2e6012009-08-24 15:23:48 +0000992 if (!Complain) {
993 // Say nothing
994 } else if (Pattern == Instantiation->getInstantiatedFromMemberClass()) {
Douglas Gregor8ea8fd42009-03-25 21:17:03 +0000995 Diag(PointOfInstantiation,
996 diag::err_implicit_instantiate_member_undefined)
997 << Context.getTypeDeclType(Instantiation);
998 Diag(Pattern->getLocation(), diag::note_member_of_template_here);
999 } else {
Douglas Gregora1f49972009-05-13 00:25:59 +00001000 Diag(PointOfInstantiation, diag::err_template_instantiate_undefined)
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001001 << (TSK != TSK_ImplicitInstantiation)
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001002 << Context.getTypeDeclType(Instantiation);
1003 Diag(Pattern->getLocation(), diag::note_template_decl_here);
1004 }
1005 return true;
1006 }
1007 Pattern = PatternDef;
1008
Douglas Gregord6ba93d2009-10-15 15:54:05 +00001009 // \brief Record the point of instantiation.
1010 if (MemberSpecializationInfo *MSInfo
1011 = Instantiation->getMemberSpecializationInfo()) {
1012 MSInfo->setTemplateSpecializationKind(TSK);
1013 MSInfo->setPointOfInstantiation(PointOfInstantiation);
Douglas Gregoref6ab412009-10-27 06:26:26 +00001014 } else if (ClassTemplateSpecializationDecl *Spec
1015 = dyn_cast<ClassTemplateSpecializationDecl>(Instantiation)) {
1016 Spec->setTemplateSpecializationKind(TSK);
1017 Spec->setPointOfInstantiation(PointOfInstantiation);
Douglas Gregord6ba93d2009-10-15 15:54:05 +00001018 }
1019
Douglas Gregorf3430ae2009-03-25 21:23:52 +00001020 InstantiatingTemplate Inst(*this, PointOfInstantiation, Instantiation);
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001021 if (Inst)
1022 return true;
1023
1024 // Enter the scope of this instantiation. We don't use
1025 // PushDeclContext because we don't have a scope.
1026 DeclContext *PreviousContext = CurContext;
1027 CurContext = Instantiation;
1028
1029 // Start the definition of this instantiation.
1030 Instantiation->startDefinition();
1031
John McCall76d824f2009-08-25 22:02:44 +00001032 // Do substitution on the base class specifiers.
1033 if (SubstBaseSpecifiers(Instantiation, Pattern, TemplateArgs))
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001034 Invalid = true;
1035
Douglas Gregor6181ded2009-05-29 18:27:38 +00001036 llvm::SmallVector<DeclPtrTy, 4> Fields;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001037 for (RecordDecl::decl_iterator Member = Pattern->decls_begin(),
Mike Stump11289f42009-09-09 15:08:12 +00001038 MemberEnd = Pattern->decls_end();
Douglas Gregorbcced4e2009-04-09 21:40:53 +00001039 Member != MemberEnd; ++Member) {
John McCall76d824f2009-08-25 22:02:44 +00001040 Decl *NewMember = SubstDecl(*Member, Instantiation, TemplateArgs);
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001041 if (NewMember) {
Eli Friedmand0e8de22009-12-07 00:22:08 +00001042 if (FieldDecl *Field = dyn_cast<FieldDecl>(NewMember))
Chris Lattner83f095c2009-03-28 19:18:32 +00001043 Fields.push_back(DeclPtrTy::make(Field));
Eli Friedmand0e8de22009-12-07 00:22:08 +00001044 else if (NewMember->isInvalidDecl())
1045 Invalid = true;
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001046 } else {
1047 // FIXME: Eventually, a NULL return will mean that one of the
Mike Stump87c57ac2009-05-16 07:39:55 +00001048 // instantiations was a semantic disaster, and we'll want to set Invalid =
1049 // true. For now, we expect to skip some members that we can't yet handle.
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001050 }
1051 }
1052
1053 // Finish checking fields.
Chris Lattner83f095c2009-03-28 19:18:32 +00001054 ActOnFields(0, Instantiation->getLocation(), DeclPtrTy::make(Instantiation),
Jay Foad7d0479f2009-05-21 09:52:38 +00001055 Fields.data(), Fields.size(), SourceLocation(), SourceLocation(),
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001056 0);
Douglas Gregorc99f1552009-12-03 18:33:45 +00001057 CheckCompletedCXXClass(Instantiation);
Douglas Gregor3c74d412009-10-14 20:14:33 +00001058 if (Instantiation->isInvalidDecl())
1059 Invalid = true;
1060
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001061 // Exit the scope of this instantiation.
1062 CurContext = PreviousContext;
1063
Douglas Gregor28ad4b52009-05-26 20:50:29 +00001064 if (!Invalid)
1065 Consumer.HandleTagDeclDefinition(Instantiation);
1066
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001067 return Invalid;
1068}
1069
Mike Stump11289f42009-09-09 15:08:12 +00001070bool
Douglas Gregor463421d2009-03-03 04:44:36 +00001071Sema::InstantiateClassTemplateSpecialization(
Douglas Gregoref6ab412009-10-27 06:26:26 +00001072 SourceLocation PointOfInstantiation,
Douglas Gregor463421d2009-03-03 04:44:36 +00001073 ClassTemplateSpecializationDecl *ClassTemplateSpec,
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001074 TemplateSpecializationKind TSK,
Douglas Gregor8a2e6012009-08-24 15:23:48 +00001075 bool Complain) {
Douglas Gregor463421d2009-03-03 04:44:36 +00001076 // Perform the actual instantiation on the canonical declaration.
1077 ClassTemplateSpec = cast<ClassTemplateSpecializationDecl>(
Argyrios Kyrtzidis6b7e3762009-07-18 00:34:25 +00001078 ClassTemplateSpec->getCanonicalDecl());
Douglas Gregor463421d2009-03-03 04:44:36 +00001079
Douglas Gregor4aa04b12009-09-11 21:19:12 +00001080 // Check whether we have already instantiated or specialized this class
1081 // template specialization.
1082 if (ClassTemplateSpec->getSpecializationKind() != TSK_Undeclared) {
1083 if (ClassTemplateSpec->getSpecializationKind() ==
1084 TSK_ExplicitInstantiationDeclaration &&
1085 TSK == TSK_ExplicitInstantiationDefinition) {
1086 // An explicit instantiation definition follows an explicit instantiation
1087 // declaration (C++0x [temp.explicit]p10); go ahead and perform the
1088 // explicit instantiation.
1089 ClassTemplateSpec->setSpecializationKind(TSK);
Douglas Gregor4aa04b12009-09-11 21:19:12 +00001090 return false;
1091 }
1092
1093 // We can only instantiate something that hasn't already been
1094 // instantiated or specialized. Fail without any diagnostics: our
1095 // caller will provide an error message.
Douglas Gregor463421d2009-03-03 04:44:36 +00001096 return true;
Douglas Gregor4aa04b12009-09-11 21:19:12 +00001097 }
Douglas Gregor463421d2009-03-03 04:44:36 +00001098
Douglas Gregor00a511f2009-09-15 16:51:42 +00001099 if (ClassTemplateSpec->isInvalidDecl())
1100 return true;
1101
Douglas Gregor463421d2009-03-03 04:44:36 +00001102 ClassTemplateDecl *Template = ClassTemplateSpec->getSpecializedTemplate();
Douglas Gregor01afeef2009-08-28 20:31:08 +00001103 CXXRecordDecl *Pattern = 0;
Douglas Gregor2373c592009-05-31 09:31:02 +00001104
Douglas Gregor170bc422009-06-12 22:31:52 +00001105 // C++ [temp.class.spec.match]p1:
1106 // When a class template is used in a context that requires an
1107 // instantiation of the class, it is necessary to determine
1108 // whether the instantiation is to be generated using the primary
1109 // template or one of the partial specializations. This is done by
1110 // matching the template arguments of the class template
1111 // specialization with the template argument lists of the partial
1112 // specializations.
Douglas Gregorb7ae10f2009-06-05 00:53:49 +00001113 typedef std::pair<ClassTemplatePartialSpecializationDecl *,
1114 TemplateArgumentList *> MatchResult;
1115 llvm::SmallVector<MatchResult, 4> Matched;
Mike Stump11289f42009-09-09 15:08:12 +00001116 for (llvm::FoldingSet<ClassTemplatePartialSpecializationDecl>::iterator
Douglas Gregor2373c592009-05-31 09:31:02 +00001117 Partial = Template->getPartialSpecializations().begin(),
1118 PartialEnd = Template->getPartialSpecializations().end();
1119 Partial != PartialEnd;
1120 ++Partial) {
Douglas Gregor181aa4a2009-06-12 18:26:56 +00001121 TemplateDeductionInfo Info(Context);
1122 if (TemplateDeductionResult Result
Mike Stump11289f42009-09-09 15:08:12 +00001123 = DeduceTemplateArguments(&*Partial,
Douglas Gregor181aa4a2009-06-12 18:26:56 +00001124 ClassTemplateSpec->getTemplateArgs(),
1125 Info)) {
1126 // FIXME: Store the failed-deduction information for use in
1127 // diagnostics, later.
1128 (void)Result;
1129 } else {
1130 Matched.push_back(std::make_pair(&*Partial, Info.take()));
1131 }
Douglas Gregor2373c592009-05-31 09:31:02 +00001132 }
1133
Douglas Gregor21610382009-10-29 00:04:11 +00001134 if (Matched.size() >= 1) {
Douglas Gregorbe999392009-09-15 16:23:51 +00001135 llvm::SmallVector<MatchResult, 4>::iterator Best = Matched.begin();
Douglas Gregor21610382009-10-29 00:04:11 +00001136 if (Matched.size() == 1) {
1137 // -- If exactly one matching specialization is found, the
1138 // instantiation is generated from that specialization.
1139 // We don't need to do anything for this.
1140 } else {
1141 // -- If more than one matching specialization is found, the
1142 // partial order rules (14.5.4.2) are used to determine
1143 // whether one of the specializations is more specialized
1144 // than the others. If none of the specializations is more
1145 // specialized than all of the other matching
1146 // specializations, then the use of the class template is
1147 // ambiguous and the program is ill-formed.
1148 for (llvm::SmallVector<MatchResult, 4>::iterator P = Best + 1,
1149 PEnd = Matched.end();
1150 P != PEnd; ++P) {
1151 if (getMoreSpecializedPartialSpecialization(P->first, Best->first)
1152 == P->first)
1153 Best = P;
Douglas Gregorbe999392009-09-15 16:23:51 +00001154 }
Douglas Gregorbe999392009-09-15 16:23:51 +00001155
Douglas Gregor21610382009-10-29 00:04:11 +00001156 // Determine if the best partial specialization is more specialized than
1157 // the others.
1158 bool Ambiguous = false;
Douglas Gregorbe999392009-09-15 16:23:51 +00001159 for (llvm::SmallVector<MatchResult, 4>::iterator P = Matched.begin(),
1160 PEnd = Matched.end();
Douglas Gregor21610382009-10-29 00:04:11 +00001161 P != PEnd; ++P) {
1162 if (P != Best &&
1163 getMoreSpecializedPartialSpecialization(P->first, Best->first)
1164 != Best->first) {
1165 Ambiguous = true;
1166 break;
1167 }
1168 }
1169
1170 if (Ambiguous) {
1171 // Partial ordering did not produce a clear winner. Complain.
1172 ClassTemplateSpec->setInvalidDecl();
1173 Diag(PointOfInstantiation, diag::err_partial_spec_ordering_ambiguous)
1174 << ClassTemplateSpec;
1175
1176 // Print the matching partial specializations.
1177 for (llvm::SmallVector<MatchResult, 4>::iterator P = Matched.begin(),
1178 PEnd = Matched.end();
1179 P != PEnd; ++P)
1180 Diag(P->first->getLocation(), diag::note_partial_spec_match)
1181 << getTemplateArgumentBindingsText(P->first->getTemplateParameters(),
1182 *P->second);
Douglas Gregor01afeef2009-08-28 20:31:08 +00001183
Douglas Gregor21610382009-10-29 00:04:11 +00001184 return true;
1185 }
Douglas Gregorbe999392009-09-15 16:23:51 +00001186 }
1187
1188 // Instantiate using the best class template partial specialization.
Douglas Gregor21610382009-10-29 00:04:11 +00001189 ClassTemplatePartialSpecializationDecl *OrigPartialSpec = Best->first;
1190 while (OrigPartialSpec->getInstantiatedFromMember()) {
1191 // If we've found an explicit specialization of this class template,
1192 // stop here and use that as the pattern.
1193 if (OrigPartialSpec->isMemberSpecialization())
1194 break;
1195
1196 OrigPartialSpec = OrigPartialSpec->getInstantiatedFromMember();
1197 }
1198
1199 Pattern = OrigPartialSpec;
Douglas Gregorbe999392009-09-15 16:23:51 +00001200 ClassTemplateSpec->setInstantiationOf(Best->first, Best->second);
Douglas Gregor170bc422009-06-12 22:31:52 +00001201 } else {
1202 // -- If no matches are found, the instantiation is generated
1203 // from the primary template.
Douglas Gregor01afeef2009-08-28 20:31:08 +00001204 ClassTemplateDecl *OrigTemplate = Template;
Douglas Gregorcf915552009-10-13 16:30:37 +00001205 while (OrigTemplate->getInstantiatedFromMemberTemplate()) {
1206 // If we've found an explicit specialization of this class template,
1207 // stop here and use that as the pattern.
1208 if (OrigTemplate->isMemberSpecialization())
1209 break;
1210
Douglas Gregor01afeef2009-08-28 20:31:08 +00001211 OrigTemplate = OrigTemplate->getInstantiatedFromMemberTemplate();
Douglas Gregorcf915552009-10-13 16:30:37 +00001212 }
1213
Douglas Gregor01afeef2009-08-28 20:31:08 +00001214 Pattern = OrigTemplate->getTemplatedDecl();
Douglas Gregor2373c592009-05-31 09:31:02 +00001215 }
Douglas Gregor463421d2009-03-03 04:44:36 +00001216
Douglas Gregoref6ab412009-10-27 06:26:26 +00001217 bool Result = InstantiateClass(PointOfInstantiation, ClassTemplateSpec,
1218 Pattern,
1219 getTemplateInstantiationArgs(ClassTemplateSpec),
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001220 TSK,
Douglas Gregor8a2e6012009-08-24 15:23:48 +00001221 Complain);
Mike Stump11289f42009-09-09 15:08:12 +00001222
Douglas Gregorb7ae10f2009-06-05 00:53:49 +00001223 for (unsigned I = 0, N = Matched.size(); I != N; ++I) {
1224 // FIXME: Implement TemplateArgumentList::Destroy!
1225 // if (Matched[I].first != Pattern)
1226 // Matched[I].second->Destroy(Context);
1227 }
Mike Stump11289f42009-09-09 15:08:12 +00001228
Douglas Gregorb7ae10f2009-06-05 00:53:49 +00001229 return Result;
Douglas Gregor463421d2009-03-03 04:44:36 +00001230}
Douglas Gregor90a1a652009-03-19 17:26:29 +00001231
John McCall76d824f2009-08-25 22:02:44 +00001232/// \brief Instantiates the definitions of all of the member
1233/// of the given class, which is an instantiation of a class template
1234/// or a member class of a template.
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00001235void
1236Sema::InstantiateClassMembers(SourceLocation PointOfInstantiation,
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001237 CXXRecordDecl *Instantiation,
1238 const MultiLevelTemplateArgumentList &TemplateArgs,
1239 TemplateSpecializationKind TSK) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001240 for (DeclContext::decl_iterator D = Instantiation->decls_begin(),
1241 DEnd = Instantiation->decls_end();
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00001242 D != DEnd; ++D) {
Douglas Gregor1d957a32009-10-27 18:42:08 +00001243 bool SuppressNew = false;
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00001244 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(*D)) {
Douglas Gregor1d957a32009-10-27 18:42:08 +00001245 if (FunctionDecl *Pattern
1246 = Function->getInstantiatedFromMemberFunction()) {
1247 MemberSpecializationInfo *MSInfo
1248 = Function->getMemberSpecializationInfo();
1249 assert(MSInfo && "No member specialization information?");
1250 if (CheckSpecializationInstantiationRedecl(PointOfInstantiation, TSK,
1251 Function,
1252 MSInfo->getTemplateSpecializationKind(),
1253 MSInfo->getPointOfInstantiation(),
1254 SuppressNew) ||
1255 SuppressNew)
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001256 continue;
1257
Douglas Gregor1d957a32009-10-27 18:42:08 +00001258 if (Function->getBody())
1259 continue;
1260
1261 if (TSK == TSK_ExplicitInstantiationDefinition) {
1262 // C++0x [temp.explicit]p8:
1263 // An explicit instantiation definition that names a class template
1264 // specialization explicitly instantiates the class template
1265 // specialization and is only an explicit instantiation definition
1266 // of members whose definition is visible at the point of
1267 // instantiation.
1268 if (!Pattern->getBody())
1269 continue;
1270
1271 Function->setTemplateSpecializationKind(TSK, PointOfInstantiation);
1272
1273 InstantiateFunctionDefinition(PointOfInstantiation, Function);
1274 } else {
1275 Function->setTemplateSpecializationKind(TSK, PointOfInstantiation);
1276 }
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001277 }
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00001278 } else if (VarDecl *Var = dyn_cast<VarDecl>(*D)) {
Douglas Gregor86d142a2009-10-08 07:24:58 +00001279 if (Var->isStaticDataMember()) {
Douglas Gregor1d957a32009-10-27 18:42:08 +00001280 MemberSpecializationInfo *MSInfo = Var->getMemberSpecializationInfo();
1281 assert(MSInfo && "No member specialization information?");
1282 if (CheckSpecializationInstantiationRedecl(PointOfInstantiation, TSK,
1283 Var,
1284 MSInfo->getTemplateSpecializationKind(),
1285 MSInfo->getPointOfInstantiation(),
1286 SuppressNew) ||
1287 SuppressNew)
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001288 continue;
1289
Douglas Gregor1d957a32009-10-27 18:42:08 +00001290 if (TSK == TSK_ExplicitInstantiationDefinition) {
1291 // C++0x [temp.explicit]p8:
1292 // An explicit instantiation definition that names a class template
1293 // specialization explicitly instantiates the class template
1294 // specialization and is only an explicit instantiation definition
1295 // of members whose definition is visible at the point of
1296 // instantiation.
1297 if (!Var->getInstantiatedFromStaticDataMember()
1298 ->getOutOfLineDefinition())
1299 continue;
1300
1301 Var->setTemplateSpecializationKind(TSK, PointOfInstantiation);
Douglas Gregor86d142a2009-10-08 07:24:58 +00001302 InstantiateStaticDataMemberDefinition(PointOfInstantiation, Var);
Douglas Gregor1d957a32009-10-27 18:42:08 +00001303 } else {
1304 Var->setTemplateSpecializationKind(TSK, PointOfInstantiation);
1305 }
1306 }
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00001307 } else if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(*D)) {
Douglas Gregord801b062009-10-07 23:56:10 +00001308 if (Record->isInjectedClassName())
1309 continue;
1310
Douglas Gregor1d957a32009-10-27 18:42:08 +00001311 MemberSpecializationInfo *MSInfo = Record->getMemberSpecializationInfo();
1312 assert(MSInfo && "No member specialization information?");
1313 if (CheckSpecializationInstantiationRedecl(PointOfInstantiation, TSK,
1314 Record,
1315 MSInfo->getTemplateSpecializationKind(),
1316 MSInfo->getPointOfInstantiation(),
1317 SuppressNew) ||
1318 SuppressNew)
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001319 continue;
1320
Douglas Gregor1d957a32009-10-27 18:42:08 +00001321 CXXRecordDecl *Pattern = Record->getInstantiatedFromMemberClass();
1322 assert(Pattern && "Missing instantiated-from-template information");
1323
1324 if (!Record->getDefinition(Context)) {
1325 if (!Pattern->getDefinition(Context)) {
1326 // C++0x [temp.explicit]p8:
1327 // An explicit instantiation definition that names a class template
1328 // specialization explicitly instantiates the class template
1329 // specialization and is only an explicit instantiation definition
1330 // of members whose definition is visible at the point of
1331 // instantiation.
1332 if (TSK == TSK_ExplicitInstantiationDeclaration) {
1333 MSInfo->setTemplateSpecializationKind(TSK);
1334 MSInfo->setPointOfInstantiation(PointOfInstantiation);
1335 }
1336
1337 continue;
1338 }
1339
1340 InstantiateClass(PointOfInstantiation, Record, Pattern,
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001341 TemplateArgs,
1342 TSK);
Douglas Gregor1d957a32009-10-27 18:42:08 +00001343 }
Douglas Gregorc093c1d2009-10-08 01:19:17 +00001344
Douglas Gregor1d957a32009-10-27 18:42:08 +00001345 Pattern = cast_or_null<CXXRecordDecl>(Record->getDefinition(Context));
1346 if (Pattern)
1347 InstantiateClassMembers(PointOfInstantiation, Pattern, TemplateArgs,
1348 TSK);
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00001349 }
1350 }
1351}
1352
1353/// \brief Instantiate the definitions of all of the members of the
1354/// given class template specialization, which was named as part of an
1355/// explicit instantiation.
Mike Stump11289f42009-09-09 15:08:12 +00001356void
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001357Sema::InstantiateClassTemplateSpecializationMembers(
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00001358 SourceLocation PointOfInstantiation,
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001359 ClassTemplateSpecializationDecl *ClassTemplateSpec,
1360 TemplateSpecializationKind TSK) {
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00001361 // C++0x [temp.explicit]p7:
1362 // An explicit instantiation that names a class template
1363 // specialization is an explicit instantion of the same kind
1364 // (declaration or definition) of each of its members (not
1365 // including members inherited from base classes) that has not
1366 // been previously explicitly specialized in the translation unit
1367 // containing the explicit instantiation, except as described
1368 // below.
1369 InstantiateClassMembers(PointOfInstantiation, ClassTemplateSpec,
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001370 getTemplateInstantiationArgs(ClassTemplateSpec),
1371 TSK);
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00001372}
1373
Mike Stump11289f42009-09-09 15:08:12 +00001374Sema::OwningStmtResult
Douglas Gregor01afeef2009-08-28 20:31:08 +00001375Sema::SubstStmt(Stmt *S, const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregorebe10102009-08-20 07:17:43 +00001376 if (!S)
1377 return Owned(S);
1378
1379 TemplateInstantiator Instantiator(*this, TemplateArgs,
1380 SourceLocation(),
1381 DeclarationName());
1382 return Instantiator.TransformStmt(S);
1383}
1384
Mike Stump11289f42009-09-09 15:08:12 +00001385Sema::OwningExprResult
Douglas Gregor01afeef2009-08-28 20:31:08 +00001386Sema::SubstExpr(Expr *E, const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregora16548e2009-08-11 05:31:07 +00001387 if (!E)
1388 return Owned(E);
Mike Stump11289f42009-09-09 15:08:12 +00001389
Douglas Gregora16548e2009-08-11 05:31:07 +00001390 TemplateInstantiator Instantiator(*this, TemplateArgs,
1391 SourceLocation(),
1392 DeclarationName());
1393 return Instantiator.TransformExpr(E);
1394}
1395
John McCall76d824f2009-08-25 22:02:44 +00001396/// \brief Do template substitution on a nested-name-specifier.
Douglas Gregorf21eb492009-03-26 23:50:42 +00001397NestedNameSpecifier *
John McCall76d824f2009-08-25 22:02:44 +00001398Sema::SubstNestedNameSpecifier(NestedNameSpecifier *NNS,
Douglas Gregor01afeef2009-08-28 20:31:08 +00001399 SourceRange Range,
1400 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor1135c352009-08-06 05:28:30 +00001401 TemplateInstantiator Instantiator(*this, TemplateArgs, Range.getBegin(),
1402 DeclarationName());
1403 return Instantiator.TransformNestedNameSpecifier(NNS, Range);
Douglas Gregor90a1a652009-03-19 17:26:29 +00001404}
Douglas Gregoraa594892009-03-31 18:38:02 +00001405
1406TemplateName
John McCall76d824f2009-08-25 22:02:44 +00001407Sema::SubstTemplateName(TemplateName Name, SourceLocation Loc,
Douglas Gregor01afeef2009-08-28 20:31:08 +00001408 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor71dc5092009-08-06 06:41:21 +00001409 TemplateInstantiator Instantiator(*this, TemplateArgs, Loc,
1410 DeclarationName());
1411 return Instantiator.TransformTemplateName(Name);
Douglas Gregoraa594892009-03-31 18:38:02 +00001412}
Douglas Gregorc43620d2009-06-11 00:06:24 +00001413
John McCall0ad16662009-10-29 08:12:44 +00001414bool Sema::Subst(const TemplateArgumentLoc &Input, TemplateArgumentLoc &Output,
1415 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregore922c772009-08-04 22:27:00 +00001416 TemplateInstantiator Instantiator(*this, TemplateArgs, SourceLocation(),
1417 DeclarationName());
John McCall0ad16662009-10-29 08:12:44 +00001418
1419 return Instantiator.TransformTemplateArgument(Input, Output);
Douglas Gregorc43620d2009-06-11 00:06:24 +00001420}