blob: b48708305a36e206155b193f1804014d197fd95e [file] [log] [blame]
Douglas Gregor74296542009-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 Gregor841324a2009-08-04 16:50:30 +000014#include "TreeTransform.h"
Douglas Gregordc18e892009-05-26 20:50:29 +000015#include "clang/AST/ASTConsumer.h"
Douglas Gregor74296542009-02-27 19:31:52 +000016#include "clang/AST/ASTContext.h"
17#include "clang/AST/Expr.h"
Douglas Gregor74296542009-02-27 19:31:52 +000018#include "clang/AST/DeclTemplate.h"
19#include "clang/Parse/DeclSpec.h"
20#include "clang/Basic/LangOptions.h"
Douglas Gregorf57dcd02009-02-28 00:25:32 +000021#include "llvm/Support/Compiler.h"
Douglas Gregor74296542009-02-27 19:31:52 +000022
23using namespace clang;
24
Douglas Gregorfee85d62009-03-10 18:03:33 +000025//===----------------------------------------------------------------------===/
26// Template Instantiation Support
27//===----------------------------------------------------------------------===/
28
Douglas Gregor8dbd0382009-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 Gregor5b8beb32009-08-28 17:37:35 +000031MultiLevelTemplateArgumentList
Douglas Gregor5f62c5e2009-05-14 23:26:13 +000032Sema::getTemplateInstantiationArgs(NamedDecl *D) {
Douglas Gregor5b8beb32009-08-28 17:37:35 +000033 // Accumulate the set of template argument lists in this structure.
34 MultiLevelTemplateArgumentList Result;
35
36 DeclContext *Ctx = dyn_cast<DeclContext>(D);
37 if (!Ctx)
38 Ctx = D->getDeclContext();
39
John McCall178664c2009-08-29 03:16:09 +000040 while (!Ctx->isFileContext()) {
Douglas Gregor5b8beb32009-08-28 17:37:35 +000041 // Add template arguments from a class template instantiation.
42 if (ClassTemplateSpecializationDecl *Spec
43 = dyn_cast<ClassTemplateSpecializationDecl>(Ctx)) {
44 // We're done when we hit an explicit specialization.
45 if (Spec->getSpecializationKind() == TSK_ExplicitSpecialization)
46 break;
Douglas Gregor6f5e0542009-06-26 00:10:03 +000047
Douglas Gregor5b8beb32009-08-28 17:37:35 +000048 Result.addOuterTemplateArguments(&Spec->getTemplateInstantiationArgs());
Douglas Gregor5b8beb32009-08-28 17:37:35 +000049 }
50
51 // Add template arguments from a function template specialization.
John McCall178664c2009-08-29 03:16:09 +000052 else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Ctx)) {
Douglas Gregor5b8beb32009-08-28 17:37:35 +000053 // FIXME: Check whether this is an explicit specialization.
54 if (const TemplateArgumentList *TemplateArgs
55 = Function->getTemplateSpecializationArgs())
56 Result.addOuterTemplateArguments(TemplateArgs);
John McCall178664c2009-08-29 03:16:09 +000057
58 // If this is a friend declaration and it declares an entity at
59 // namespace scope, take arguments from its lexical parent
60 // instead of its semantic parent.
61 if (Function->getFriendObjectKind() &&
62 Function->getDeclContext()->isFileContext()) {
63 Ctx = Function->getLexicalDeclContext();
64 continue;
65 }
Douglas Gregor5b8beb32009-08-28 17:37:35 +000066 }
John McCall178664c2009-08-29 03:16:09 +000067
68 Ctx = Ctx->getParent();
Douglas Gregor5f62c5e2009-05-14 23:26:13 +000069 }
Douglas Gregor5b8beb32009-08-28 17:37:35 +000070
71 return Result;
Douglas Gregor5f62c5e2009-05-14 23:26:13 +000072}
73
Douglas Gregor375733c2009-03-10 00:06:19 +000074Sema::InstantiatingTemplate::
75InstantiatingTemplate(Sema &SemaRef, SourceLocation PointOfInstantiation,
Douglas Gregorb12249d2009-05-18 17:01:57 +000076 Decl *Entity,
Douglas Gregor375733c2009-03-10 00:06:19 +000077 SourceRange InstantiationRange)
78 : SemaRef(SemaRef) {
Douglas Gregor56d25a72009-03-10 20:44:00 +000079
80 Invalid = CheckInstantiationDepth(PointOfInstantiation,
81 InstantiationRange);
82 if (!Invalid) {
Douglas Gregor375733c2009-03-10 00:06:19 +000083 ActiveTemplateInstantiation Inst;
Douglas Gregor56d25a72009-03-10 20:44:00 +000084 Inst.Kind = ActiveTemplateInstantiation::TemplateInstantiation;
Douglas Gregor375733c2009-03-10 00:06:19 +000085 Inst.PointOfInstantiation = PointOfInstantiation;
Douglas Gregor56d25a72009-03-10 20:44:00 +000086 Inst.Entity = reinterpret_cast<uintptr_t>(Entity);
Douglas Gregor95ba1282009-03-12 18:36:18 +000087 Inst.TemplateArgs = 0;
88 Inst.NumTemplateArgs = 0;
Douglas Gregor56d25a72009-03-10 20:44:00 +000089 Inst.InstantiationRange = InstantiationRange;
90 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
91 Invalid = false;
92 }
93}
94
95Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef,
96 SourceLocation PointOfInstantiation,
97 TemplateDecl *Template,
98 const TemplateArgument *TemplateArgs,
99 unsigned NumTemplateArgs,
100 SourceRange InstantiationRange)
101 : SemaRef(SemaRef) {
102
103 Invalid = CheckInstantiationDepth(PointOfInstantiation,
104 InstantiationRange);
105 if (!Invalid) {
106 ActiveTemplateInstantiation Inst;
107 Inst.Kind
108 = ActiveTemplateInstantiation::DefaultTemplateArgumentInstantiation;
109 Inst.PointOfInstantiation = PointOfInstantiation;
110 Inst.Entity = reinterpret_cast<uintptr_t>(Template);
111 Inst.TemplateArgs = TemplateArgs;
112 Inst.NumTemplateArgs = NumTemplateArgs;
Douglas Gregor375733c2009-03-10 00:06:19 +0000113 Inst.InstantiationRange = InstantiationRange;
114 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
115 Invalid = false;
116 }
117}
118
Douglas Gregorc5e01af2009-06-10 23:47:09 +0000119Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef,
120 SourceLocation PointOfInstantiation,
Douglas Gregor4c8b2b32009-07-01 22:01:06 +0000121 FunctionTemplateDecl *FunctionTemplate,
122 const TemplateArgument *TemplateArgs,
123 unsigned NumTemplateArgs,
124 ActiveTemplateInstantiation::InstantiationKind Kind,
125 SourceRange InstantiationRange)
126: SemaRef(SemaRef) {
127
128 Invalid = CheckInstantiationDepth(PointOfInstantiation,
129 InstantiationRange);
130 if (!Invalid) {
131 ActiveTemplateInstantiation Inst;
132 Inst.Kind = Kind;
133 Inst.PointOfInstantiation = PointOfInstantiation;
134 Inst.Entity = reinterpret_cast<uintptr_t>(FunctionTemplate);
135 Inst.TemplateArgs = TemplateArgs;
136 Inst.NumTemplateArgs = NumTemplateArgs;
137 Inst.InstantiationRange = InstantiationRange;
138 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
139 Invalid = false;
140 }
141}
142
143Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef,
144 SourceLocation PointOfInstantiation,
Douglas Gregorc5e01af2009-06-10 23:47:09 +0000145 ClassTemplatePartialSpecializationDecl *PartialSpec,
146 const TemplateArgument *TemplateArgs,
147 unsigned NumTemplateArgs,
148 SourceRange InstantiationRange)
149 : SemaRef(SemaRef) {
150
151 Invalid = CheckInstantiationDepth(PointOfInstantiation,
152 InstantiationRange);
153 if (!Invalid) {
154 ActiveTemplateInstantiation Inst;
155 Inst.Kind
Douglas Gregor4c8b2b32009-07-01 22:01:06 +0000156 = ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution;
Douglas Gregorc5e01af2009-06-10 23:47:09 +0000157 Inst.PointOfInstantiation = PointOfInstantiation;
158 Inst.Entity = reinterpret_cast<uintptr_t>(PartialSpec);
159 Inst.TemplateArgs = TemplateArgs;
160 Inst.NumTemplateArgs = NumTemplateArgs;
161 Inst.InstantiationRange = InstantiationRange;
162 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
163 Invalid = false;
164 }
165}
166
Anders Carlsson21d18f22009-09-05 05:14:19 +0000167Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef,
168 SourceLocation PointOfInstantation,
169 ParmVarDecl *Param,
170 const TemplateArgument *TemplateArgs,
171 unsigned NumTemplateArgs,
172 SourceRange InstantiationRange)
173 : SemaRef(SemaRef) {
174
175 Invalid = CheckInstantiationDepth(PointOfInstantation, InstantiationRange);
176
177 if (!Invalid) {
178 ActiveTemplateInstantiation Inst;
179 Inst.Kind
180 = ActiveTemplateInstantiation::DefaultFunctionArgumentInstantiation;
181 Inst.PointOfInstantiation = PointOfInstantation;
182 Inst.Entity = reinterpret_cast<uintptr_t>(Param);
183 Inst.TemplateArgs = TemplateArgs;
184 Inst.NumTemplateArgs = NumTemplateArgs;
185 Inst.InstantiationRange = InstantiationRange;
186 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
187 Invalid = false;
188 }
189}
190
Douglas Gregorb12249d2009-05-18 17:01:57 +0000191void Sema::InstantiatingTemplate::Clear() {
192 if (!Invalid) {
Douglas Gregor375733c2009-03-10 00:06:19 +0000193 SemaRef.ActiveTemplateInstantiations.pop_back();
Douglas Gregorb12249d2009-05-18 17:01:57 +0000194 Invalid = true;
195 }
Douglas Gregor375733c2009-03-10 00:06:19 +0000196}
197
Douglas Gregor56d25a72009-03-10 20:44:00 +0000198bool Sema::InstantiatingTemplate::CheckInstantiationDepth(
199 SourceLocation PointOfInstantiation,
200 SourceRange InstantiationRange) {
201 if (SemaRef.ActiveTemplateInstantiations.size()
202 <= SemaRef.getLangOptions().InstantiationDepth)
203 return false;
204
205 SemaRef.Diag(PointOfInstantiation,
206 diag::err_template_recursion_depth_exceeded)
207 << SemaRef.getLangOptions().InstantiationDepth
208 << InstantiationRange;
209 SemaRef.Diag(PointOfInstantiation, diag::note_template_recursion_depth)
210 << SemaRef.getLangOptions().InstantiationDepth;
211 return true;
212}
213
Douglas Gregorfee85d62009-03-10 18:03:33 +0000214/// \brief Prints the current instantiation stack through a series of
215/// notes.
216void Sema::PrintInstantiationStack() {
Douglas Gregor4c8b2b32009-07-01 22:01:06 +0000217 // FIXME: In all of these cases, we need to show the template arguments
Douglas Gregorfee85d62009-03-10 18:03:33 +0000218 for (llvm::SmallVector<ActiveTemplateInstantiation, 16>::reverse_iterator
219 Active = ActiveTemplateInstantiations.rbegin(),
220 ActiveEnd = ActiveTemplateInstantiations.rend();
221 Active != ActiveEnd;
222 ++Active) {
Douglas Gregor56d25a72009-03-10 20:44:00 +0000223 switch (Active->Kind) {
224 case ActiveTemplateInstantiation::TemplateInstantiation: {
Douglas Gregorb12249d2009-05-18 17:01:57 +0000225 Decl *D = reinterpret_cast<Decl *>(Active->Entity);
226 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) {
227 unsigned DiagID = diag::note_template_member_class_here;
228 if (isa<ClassTemplateSpecializationDecl>(Record))
229 DiagID = diag::note_template_class_instantiation_here;
230 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
231 DiagID)
232 << Context.getTypeDeclType(Record)
233 << Active->InstantiationRange;
Douglas Gregor181fe792009-07-24 20:34:43 +0000234 } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
Douglas Gregor6f5e0542009-06-26 00:10:03 +0000235 unsigned DiagID;
236 if (Function->getPrimaryTemplate())
237 DiagID = diag::note_function_template_spec_here;
238 else
239 DiagID = diag::note_template_member_function_here;
Douglas Gregorb12249d2009-05-18 17:01:57 +0000240 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
241 DiagID)
242 << Function
243 << Active->InstantiationRange;
Douglas Gregor181fe792009-07-24 20:34:43 +0000244 } else {
245 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
246 diag::note_template_static_data_member_def_here)
247 << cast<VarDecl>(D)
248 << Active->InstantiationRange;
Douglas Gregorb12249d2009-05-18 17:01:57 +0000249 }
Douglas Gregor56d25a72009-03-10 20:44:00 +0000250 break;
251 }
252
253 case ActiveTemplateInstantiation::DefaultTemplateArgumentInstantiation: {
254 TemplateDecl *Template = cast<TemplateDecl>((Decl *)Active->Entity);
255 std::string TemplateArgsStr
Douglas Gregordd13e842009-03-30 22:58:21 +0000256 = TemplateSpecializationType::PrintTemplateArgumentList(
Douglas Gregor4c8b2b32009-07-01 22:01:06 +0000257 Active->TemplateArgs,
Douglas Gregor3bf3bbc2009-05-29 20:38:28 +0000258 Active->NumTemplateArgs,
259 Context.PrintingPolicy);
Douglas Gregor56d25a72009-03-10 20:44:00 +0000260 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
261 diag::note_default_arg_instantiation_here)
262 << (Template->getNameAsString() + TemplateArgsStr)
263 << Active->InstantiationRange;
264 break;
265 }
Douglas Gregorc5e01af2009-06-10 23:47:09 +0000266
Douglas Gregor4c8b2b32009-07-01 22:01:06 +0000267 case ActiveTemplateInstantiation::ExplicitTemplateArgumentSubstitution: {
268 FunctionTemplateDecl *FnTmpl
269 = cast<FunctionTemplateDecl>((Decl *)Active->Entity);
Douglas Gregorc5e01af2009-06-10 23:47:09 +0000270 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
Douglas Gregor4c8b2b32009-07-01 22:01:06 +0000271 diag::note_explicit_template_arg_substitution_here)
272 << FnTmpl << Active->InstantiationRange;
Douglas Gregorc5e01af2009-06-10 23:47:09 +0000273 break;
274 }
Douglas Gregor4c8b2b32009-07-01 22:01:06 +0000275
276 case ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution:
277 if (ClassTemplatePartialSpecializationDecl *PartialSpec
278 = dyn_cast<ClassTemplatePartialSpecializationDecl>(
279 (Decl *)Active->Entity)) {
280 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
281 diag::note_partial_spec_deduct_instantiation_here)
282 << Context.getTypeDeclType(PartialSpec)
283 << Active->InstantiationRange;
284 } else {
285 FunctionTemplateDecl *FnTmpl
286 = cast<FunctionTemplateDecl>((Decl *)Active->Entity);
287 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
288 diag::note_function_template_deduction_instantiation_here)
289 << FnTmpl << Active->InstantiationRange;
290 }
291 break;
Douglas Gregorc5e01af2009-06-10 23:47:09 +0000292
Anders Carlsson21d18f22009-09-05 05:14:19 +0000293 case ActiveTemplateInstantiation::DefaultFunctionArgumentInstantiation: {
294 ParmVarDecl *Param = cast<ParmVarDecl>((Decl *)Active->Entity);
295 FunctionDecl *FD = cast<FunctionDecl>(Param->getDeclContext());
296 TemplateDecl *Template = FD->getPrimaryTemplate();
297
298 std::string TemplateArgsStr
299 = TemplateSpecializationType::PrintTemplateArgumentList(
300 Active->TemplateArgs,
301 Active->NumTemplateArgs,
302 Context.PrintingPolicy);
303 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
304 diag::note_default_function_arg_instantiation_here)
305 << (Template->getNameAsString() + TemplateArgsStr)
306 << Active->InstantiationRange;
307 break;
308 }
309
Douglas Gregor56d25a72009-03-10 20:44:00 +0000310 }
Douglas Gregorfee85d62009-03-10 18:03:33 +0000311 }
312}
313
Douglas Gregor95d6c952009-06-14 07:33:30 +0000314bool Sema::isSFINAEContext() const {
315 using llvm::SmallVector;
316 for (SmallVector<ActiveTemplateInstantiation, 16>::const_reverse_iterator
317 Active = ActiveTemplateInstantiations.rbegin(),
318 ActiveEnd = ActiveTemplateInstantiations.rend();
319 Active != ActiveEnd;
320 ++Active) {
321
322 switch(Active->Kind) {
Douglas Gregor4c8b2b32009-07-01 22:01:06 +0000323 case ActiveTemplateInstantiation::TemplateInstantiation:
Anders Carlsson21d18f22009-09-05 05:14:19 +0000324 case ActiveTemplateInstantiation::DefaultFunctionArgumentInstantiation:
325
Douglas Gregor4c8b2b32009-07-01 22:01:06 +0000326 // This is a template instantiation, so there is no SFINAE.
327 return false;
328
Douglas Gregor95d6c952009-06-14 07:33:30 +0000329 case ActiveTemplateInstantiation::DefaultTemplateArgumentInstantiation:
330 // A default template argument instantiation may or may not be a
331 // SFINAE context; look further up the stack.
332 break;
Douglas Gregor4c8b2b32009-07-01 22:01:06 +0000333
334 case ActiveTemplateInstantiation::ExplicitTemplateArgumentSubstitution:
335 case ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution:
336 // We're either substitution explicitly-specified template arguments
337 // or deduced template arguments, so SFINAE applies.
338 return true;
Douglas Gregor95d6c952009-06-14 07:33:30 +0000339 }
340 }
341
342 return false;
343}
344
Douglas Gregor74296542009-02-27 19:31:52 +0000345//===----------------------------------------------------------------------===/
346// Template Instantiation for Types
347//===----------------------------------------------------------------------===/
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000348namespace {
Douglas Gregor841324a2009-08-04 16:50:30 +0000349 class VISIBILITY_HIDDEN TemplateInstantiator
350 : public TreeTransform<TemplateInstantiator>
351 {
Douglas Gregor8dbd0382009-08-28 20:31:08 +0000352 const MultiLevelTemplateArgumentList &TemplateArgs;
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000353 SourceLocation Loc;
354 DeclarationName Entity;
Douglas Gregor74296542009-02-27 19:31:52 +0000355
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000356 public:
Douglas Gregor23a44be2009-08-20 07:17:43 +0000357 typedef TreeTransform<TemplateInstantiator> inherited;
358
Douglas Gregor841324a2009-08-04 16:50:30 +0000359 TemplateInstantiator(Sema &SemaRef,
Douglas Gregor8dbd0382009-08-28 20:31:08 +0000360 const MultiLevelTemplateArgumentList &TemplateArgs,
Douglas Gregor841324a2009-08-04 16:50:30 +0000361 SourceLocation Loc,
362 DeclarationName Entity)
Douglas Gregor23a44be2009-08-20 07:17:43 +0000363 : inherited(SemaRef), TemplateArgs(TemplateArgs), Loc(Loc),
364 Entity(Entity) { }
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000365
Douglas Gregor841324a2009-08-04 16:50:30 +0000366 /// \brief Determine whether the given type \p T has already been
367 /// transformed.
368 ///
369 /// For the purposes of template instantiation, a type has already been
370 /// transformed if it is NULL or if it is not dependent.
371 bool AlreadyTransformed(QualType T) {
372 return T.isNull() || !T->isDependentType();
Douglas Gregor90177912009-05-13 18:28:20 +0000373 }
Douglas Gregor841324a2009-08-04 16:50:30 +0000374
375 /// \brief Returns the location of the entity being instantiated, if known.
376 SourceLocation getBaseLocation() { return Loc; }
377
378 /// \brief Returns the name of the entity being instantiated, if any.
379 DeclarationName getBaseEntity() { return Entity; }
380
Douglas Gregor841324a2009-08-04 16:50:30 +0000381 /// \brief Transform the given declaration by instantiating a reference to
382 /// this declaration.
383 Decl *TransformDecl(Decl *D);
Douglas Gregor9d879762009-08-11 05:31:07 +0000384
Douglas Gregor23a44be2009-08-20 07:17:43 +0000385 /// \brief Transform the definition of the given declaration by
386 /// instantiating it.
387 Decl *TransformDefinition(Decl *D);
388
389 /// \brief Rebuild the exception declaration and register the declaration
390 /// as an instantiated local.
391 VarDecl *RebuildExceptionDecl(VarDecl *ExceptionDecl, QualType T,
392 DeclaratorInfo *Declarator,
393 IdentifierInfo *Name,
394 SourceLocation Loc, SourceRange TypeRange);
Douglas Gregor9d879762009-08-11 05:31:07 +0000395
396 Sema::OwningExprResult TransformDeclRefExpr(DeclRefExpr *E);
397
Douglas Gregor841324a2009-08-04 16:50:30 +0000398 /// \brief Transforms a template type parameter type by performing
399 /// substitution of the corresponding template type argument.
400 QualType TransformTemplateTypeParmType(const TemplateTypeParmType *T);
401 };
Douglas Gregor1d381132009-07-06 15:59:29 +0000402}
403
Douglas Gregor841324a2009-08-04 16:50:30 +0000404Decl *TemplateInstantiator::TransformDecl(Decl *D) {
Douglas Gregorbc2fb7f2009-09-03 21:38:09 +0000405 if (!D)
406 return 0;
407
408 if (TemplateTemplateParmDecl *TTP = dyn_cast<TemplateTemplateParmDecl>(D)) {
Douglas Gregor8dbd0382009-08-28 20:31:08 +0000409 if (TTP->getDepth() < TemplateArgs.getNumLevels()) {
410 assert(TemplateArgs(TTP->getDepth(), TTP->getPosition()).getAsDecl() &&
411 "Wrong kind of template template argument");
412 return cast<TemplateDecl>(TemplateArgs(TTP->getDepth(),
413 TTP->getPosition()).getAsDecl());
414 }
415
416 // If the corresponding template argument is NULL or non-existent, it's
417 // because we are performing instantiation from explicitly-specified
418 // template arguments in a function template, but there were some
419 // arguments left unspecified.
420 if (!TemplateArgs.hasTemplateArgument(TTP->getDepth(),
421 TTP->getPosition()))
422 return D;
423
424 // FIXME: Implement depth reduction of template template parameters
425 assert(false &&
426 "Reducing depth of template template parameters is not yet implemented");
Douglas Gregor214d0462009-08-06 06:41:21 +0000427 }
428
Douglas Gregorbc2fb7f2009-09-03 21:38:09 +0000429 return SemaRef.FindInstantiatedDecl(cast<NamedDecl>(D));
Douglas Gregor841324a2009-08-04 16:50:30 +0000430}
431
Douglas Gregor23a44be2009-08-20 07:17:43 +0000432Decl *TemplateInstantiator::TransformDefinition(Decl *D) {
John McCall0ba26ee2009-08-25 22:02:44 +0000433 Decl *Inst = getSema().SubstDecl(D, getSema().CurContext, TemplateArgs);
Douglas Gregor23a44be2009-08-20 07:17:43 +0000434 if (!Inst)
435 return 0;
436
437 getSema().CurrentInstantiationScope->InstantiatedLocal(D, Inst);
438 return Inst;
439}
440
441VarDecl *
442TemplateInstantiator::RebuildExceptionDecl(VarDecl *ExceptionDecl,
443 QualType T,
444 DeclaratorInfo *Declarator,
445 IdentifierInfo *Name,
446 SourceLocation Loc,
447 SourceRange TypeRange) {
448 VarDecl *Var = inherited::RebuildExceptionDecl(ExceptionDecl, T, Declarator,
449 Name, Loc, TypeRange);
450 if (Var && !Var->isInvalidDecl())
451 getSema().CurrentInstantiationScope->InstantiatedLocal(ExceptionDecl, Var);
452 return Var;
453}
454
Douglas Gregor9d879762009-08-11 05:31:07 +0000455Sema::OwningExprResult
456TemplateInstantiator::TransformDeclRefExpr(DeclRefExpr *E) {
457 // FIXME: Clean this up a bit
458 NamedDecl *D = E->getDecl();
459 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(D)) {
Douglas Gregor8dbd0382009-08-28 20:31:08 +0000460 if (NTTP->getDepth() >= TemplateArgs.getNumLevels()) {
461 assert(false && "Cannot reduce non-type template parameter depth yet");
462 return getSema().ExprError();
463 }
Douglas Gregor9d879762009-08-11 05:31:07 +0000464
465 // If the corresponding template argument is NULL or non-existent, it's
466 // because we are performing instantiation from explicitly-specified
467 // template arguments in a function template, but there were some
468 // arguments left unspecified.
Douglas Gregor8dbd0382009-08-28 20:31:08 +0000469 if (!TemplateArgs.hasTemplateArgument(NTTP->getDepth(),
470 NTTP->getPosition()))
471 return SemaRef.Owned(E->Retain());
Douglas Gregor9d879762009-08-11 05:31:07 +0000472
Douglas Gregor8dbd0382009-08-28 20:31:08 +0000473 const TemplateArgument &Arg = TemplateArgs(NTTP->getDepth(),
474 NTTP->getPosition());
Douglas Gregor9d879762009-08-11 05:31:07 +0000475
476 // The template argument itself might be an expression, in which
477 // case we just return that expression.
478 if (Arg.getKind() == TemplateArgument::Expression)
Douglas Gregor8dbd0382009-08-28 20:31:08 +0000479 return SemaRef.Owned(Arg.getAsExpr()->Retain());
Douglas Gregor9d879762009-08-11 05:31:07 +0000480
481 if (Arg.getKind() == TemplateArgument::Declaration) {
482 ValueDecl *VD = cast<ValueDecl>(Arg.getAsDecl());
483
Douglas Gregor8dbd0382009-08-28 20:31:08 +0000484 VD = cast_or_null<ValueDecl>(getSema().FindInstantiatedDecl(VD));
485 if (!VD)
486 return SemaRef.ExprError();
487
Douglas Gregor9d879762009-08-11 05:31:07 +0000488 return SemaRef.BuildDeclRefExpr(VD, VD->getType(), E->getLocation(),
Douglas Gregor8dbd0382009-08-28 20:31:08 +0000489 /*FIXME:*/false, /*FIXME:*/false);
Douglas Gregor9d879762009-08-11 05:31:07 +0000490 }
491
492 assert(Arg.getKind() == TemplateArgument::Integral);
493 QualType T = Arg.getIntegralType();
494 if (T->isCharType() || T->isWideCharType())
495 return SemaRef.Owned(new (SemaRef.Context) CharacterLiteral(
Douglas Gregor8dbd0382009-08-28 20:31:08 +0000496 Arg.getAsIntegral()->getZExtValue(),
497 T->isWideCharType(),
498 T,
499 E->getSourceRange().getBegin()));
Douglas Gregor9d879762009-08-11 05:31:07 +0000500 if (T->isBooleanType())
501 return SemaRef.Owned(new (SemaRef.Context) CXXBoolLiteralExpr(
Douglas Gregor8dbd0382009-08-28 20:31:08 +0000502 Arg.getAsIntegral()->getBoolValue(),
503 T,
504 E->getSourceRange().getBegin()));
Douglas Gregor9d879762009-08-11 05:31:07 +0000505
506 assert(Arg.getAsIntegral()->getBitWidth() == SemaRef.Context.getIntWidth(T));
507 return SemaRef.Owned(new (SemaRef.Context) IntegerLiteral(
Douglas Gregor8dbd0382009-08-28 20:31:08 +0000508 *Arg.getAsIntegral(),
509 T,
510 E->getSourceRange().getBegin()));
Douglas Gregor9d879762009-08-11 05:31:07 +0000511 }
512
John McCall0ba26ee2009-08-25 22:02:44 +0000513 NamedDecl *InstD = SemaRef.FindInstantiatedDecl(D);
Douglas Gregor9d879762009-08-11 05:31:07 +0000514 if (!InstD)
515 return SemaRef.ExprError();
516
Anders Carlsson8197c482009-08-29 19:37:28 +0000517 // If we instantiated an UnresolvedUsingDecl and got back an UsingDecl,
518 // we need to get the underlying decl.
519 // FIXME: Is this correct? Maybe FindInstantiatedDecl should do this?
520 InstD = InstD->getUnderlyingDecl();
521
Douglas Gregor9d879762009-08-11 05:31:07 +0000522 // FIXME: nested-name-specifier for QualifiedDeclRefExpr
523 return SemaRef.BuildDeclarationNameExpr(E->getLocation(), InstD,
524 /*FIXME:*/false,
525 /*FIXME:*/0,
526 /*FIXME:*/false);
527}
528
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000529QualType
Douglas Gregor841324a2009-08-04 16:50:30 +0000530TemplateInstantiator::TransformTemplateTypeParmType(
531 const TemplateTypeParmType *T) {
Douglas Gregor8dbd0382009-08-28 20:31:08 +0000532 if (T->getDepth() < TemplateArgs.getNumLevels()) {
Douglas Gregor74296542009-02-27 19:31:52 +0000533 // Replace the template type parameter with its corresponding
534 // template argument.
Douglas Gregorecd63b82009-07-01 00:28:38 +0000535
536 // If the corresponding template argument is NULL or doesn't exist, it's
537 // because we are performing instantiation from explicitly-specified
538 // template arguments in a function template class, but there were some
539 // arguments left unspecified.
Douglas Gregor8dbd0382009-08-28 20:31:08 +0000540 if (!TemplateArgs.hasTemplateArgument(T->getDepth(), T->getIndex()))
541 return QualType(T, 0);
542
543 assert(TemplateArgs(T->getDepth(), T->getIndex()).getKind()
544 == TemplateArgument::Type &&
Douglas Gregor74296542009-02-27 19:31:52 +0000545 "Template argument kind mismatch");
Douglas Gregor8dbd0382009-08-28 20:31:08 +0000546
547 return TemplateArgs(T->getDepth(), T->getIndex()).getAsType();
Douglas Gregor74296542009-02-27 19:31:52 +0000548 }
549
550 // The template type parameter comes from an inner template (e.g.,
551 // the template parameter list of a member template inside the
552 // template we are instantiating). Create a new template type
553 // parameter with the template "level" reduced by one.
Douglas Gregor8dbd0382009-08-28 20:31:08 +0000554 return getSema().Context.getTemplateTypeParmType(
555 T->getDepth() - TemplateArgs.getNumLevels(),
Douglas Gregor841324a2009-08-04 16:50:30 +0000556 T->getIndex(),
557 T->isParameterPack(),
558 T->getName());
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000559}
Douglas Gregor74296542009-02-27 19:31:52 +0000560
John McCall0ba26ee2009-08-25 22:02:44 +0000561/// \brief Perform substitution on the type T with a given set of template
562/// arguments.
Douglas Gregor74296542009-02-27 19:31:52 +0000563///
564/// This routine substitutes the given template arguments into the
565/// type T and produces the instantiated type.
566///
567/// \param T the type into which the template arguments will be
568/// substituted. If this type is not dependent, it will be returned
569/// immediately.
570///
571/// \param TemplateArgs the template arguments that will be
572/// substituted for the top-level template parameters within T.
573///
Douglas Gregor74296542009-02-27 19:31:52 +0000574/// \param Loc the location in the source code where this substitution
575/// is being performed. It will typically be the location of the
576/// declarator (if we're instantiating the type of some declaration)
577/// or the location of the type in the source code (if, e.g., we're
578/// instantiating the type of a cast expression).
579///
580/// \param Entity the name of the entity associated with a declaration
581/// being instantiated (if any). May be empty to indicate that there
582/// is no such entity (if, e.g., this is a type that occurs as part of
583/// a cast expression) or that the entity has no name (e.g., an
584/// unnamed function parameter).
585///
586/// \returns If the instantiation succeeds, the instantiated
587/// type. Otherwise, produces diagnostics and returns a NULL type.
John McCall0ba26ee2009-08-25 22:02:44 +0000588QualType Sema::SubstType(QualType T,
Douglas Gregor8dbd0382009-08-28 20:31:08 +0000589 const MultiLevelTemplateArgumentList &TemplateArgs,
John McCall0ba26ee2009-08-25 22:02:44 +0000590 SourceLocation Loc, DeclarationName Entity) {
Douglas Gregor56d25a72009-03-10 20:44:00 +0000591 assert(!ActiveTemplateInstantiations.empty() &&
592 "Cannot perform an instantiation without some context on the "
593 "instantiation stack");
594
Douglas Gregor74296542009-02-27 19:31:52 +0000595 // If T is not a dependent type, there is nothing to do.
596 if (!T->isDependentType())
597 return T;
598
Douglas Gregor841324a2009-08-04 16:50:30 +0000599 TemplateInstantiator Instantiator(*this, TemplateArgs, Loc, Entity);
600 return Instantiator.TransformType(T);
Douglas Gregor74296542009-02-27 19:31:52 +0000601}
Douglas Gregored3a3982009-03-03 04:44:36 +0000602
John McCall0ba26ee2009-08-25 22:02:44 +0000603/// \brief Perform substitution on the base class specifiers of the
604/// given class template specialization.
Douglas Gregored3a3982009-03-03 04:44:36 +0000605///
606/// Produces a diagnostic and returns true on error, returns false and
607/// attaches the instantiated base classes to the class template
608/// specialization if successful.
609bool
John McCall0ba26ee2009-08-25 22:02:44 +0000610Sema::SubstBaseSpecifiers(CXXRecordDecl *Instantiation,
611 CXXRecordDecl *Pattern,
Douglas Gregor8dbd0382009-08-28 20:31:08 +0000612 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregored3a3982009-03-03 04:44:36 +0000613 bool Invalid = false;
Douglas Gregor4ac20ef2009-05-29 18:27:38 +0000614 llvm::SmallVector<CXXBaseSpecifier*, 4> InstantiatedBases;
Douglas Gregorcc887972009-03-25 21:17:03 +0000615 for (ClassTemplateSpecializationDecl::base_class_iterator
616 Base = Pattern->bases_begin(), BaseEnd = Pattern->bases_end();
Douglas Gregord9572a12009-03-10 18:52:44 +0000617 Base != BaseEnd; ++Base) {
Douglas Gregored3a3982009-03-03 04:44:36 +0000618 if (!Base->getType()->isDependentType()) {
Fariborz Jahanian1373b6f2009-07-22 17:41:53 +0000619 InstantiatedBases.push_back(new (Context) CXXBaseSpecifier(*Base));
Douglas Gregored3a3982009-03-03 04:44:36 +0000620 continue;
621 }
622
John McCall0ba26ee2009-08-25 22:02:44 +0000623 QualType BaseType = SubstType(Base->getType(),
624 TemplateArgs,
625 Base->getSourceRange().getBegin(),
626 DeclarationName());
Douglas Gregored3a3982009-03-03 04:44:36 +0000627 if (BaseType.isNull()) {
628 Invalid = true;
629 continue;
630 }
631
632 if (CXXBaseSpecifier *InstantiatedBase
Douglas Gregorcc887972009-03-25 21:17:03 +0000633 = CheckBaseSpecifier(Instantiation,
Douglas Gregored3a3982009-03-03 04:44:36 +0000634 Base->getSourceRange(),
635 Base->isVirtual(),
636 Base->getAccessSpecifierAsWritten(),
637 BaseType,
638 /*FIXME: Not totally accurate */
639 Base->getSourceRange().getBegin()))
640 InstantiatedBases.push_back(InstantiatedBase);
641 else
642 Invalid = true;
643 }
644
Douglas Gregord9572a12009-03-10 18:52:44 +0000645 if (!Invalid &&
Jay Foad9e6bef42009-05-21 09:52:38 +0000646 AttachBaseSpecifiers(Instantiation, InstantiatedBases.data(),
Douglas Gregored3a3982009-03-03 04:44:36 +0000647 InstantiatedBases.size()))
648 Invalid = true;
649
650 return Invalid;
651}
652
Douglas Gregorcc887972009-03-25 21:17:03 +0000653/// \brief Instantiate the definition of a class from a given pattern.
654///
655/// \param PointOfInstantiation The point of instantiation within the
656/// source code.
657///
658/// \param Instantiation is the declaration whose definition is being
659/// instantiated. This will be either a class template specialization
660/// or a member class of a class template specialization.
661///
662/// \param Pattern is the pattern from which the instantiation
663/// occurs. This will be either the declaration of a class template or
664/// the declaration of a member class of a class template.
665///
666/// \param TemplateArgs The template arguments to be substituted into
667/// the pattern.
668///
Douglas Gregor8e7e4602009-09-04 22:48:11 +0000669/// \param TSK the kind of implicit or explicit instantiation to perform.
Douglas Gregorb35c7992009-08-24 15:23:48 +0000670///
671/// \param Complain whether to complain if the class cannot be instantiated due
672/// to the lack of a definition.
673///
Douglas Gregorcc887972009-03-25 21:17:03 +0000674/// \returns true if an error occurred, false otherwise.
675bool
676Sema::InstantiateClass(SourceLocation PointOfInstantiation,
677 CXXRecordDecl *Instantiation, CXXRecordDecl *Pattern,
Douglas Gregor8dbd0382009-08-28 20:31:08 +0000678 const MultiLevelTemplateArgumentList &TemplateArgs,
Douglas Gregor8e7e4602009-09-04 22:48:11 +0000679 TemplateSpecializationKind TSK,
Douglas Gregorb35c7992009-08-24 15:23:48 +0000680 bool Complain) {
Douglas Gregorcc887972009-03-25 21:17:03 +0000681 bool Invalid = false;
John McCalld64b41e2009-08-20 01:44:21 +0000682
Douglas Gregorcc887972009-03-25 21:17:03 +0000683 CXXRecordDecl *PatternDef
684 = cast_or_null<CXXRecordDecl>(Pattern->getDefinition(Context));
685 if (!PatternDef) {
Douglas Gregorb35c7992009-08-24 15:23:48 +0000686 if (!Complain) {
687 // Say nothing
688 } else if (Pattern == Instantiation->getInstantiatedFromMemberClass()) {
Douglas Gregorcc887972009-03-25 21:17:03 +0000689 Diag(PointOfInstantiation,
690 diag::err_implicit_instantiate_member_undefined)
691 << Context.getTypeDeclType(Instantiation);
692 Diag(Pattern->getLocation(), diag::note_member_of_template_here);
693 } else {
Douglas Gregorfd79ac62009-05-13 00:25:59 +0000694 Diag(PointOfInstantiation, diag::err_template_instantiate_undefined)
Douglas Gregor8e7e4602009-09-04 22:48:11 +0000695 << (TSK != TSK_ImplicitInstantiation)
Douglas Gregorcc887972009-03-25 21:17:03 +0000696 << Context.getTypeDeclType(Instantiation);
697 Diag(Pattern->getLocation(), diag::note_template_decl_here);
698 }
699 return true;
700 }
701 Pattern = PatternDef;
702
Douglas Gregor42c48522009-03-25 21:23:52 +0000703 InstantiatingTemplate Inst(*this, PointOfInstantiation, Instantiation);
Douglas Gregorcc887972009-03-25 21:17:03 +0000704 if (Inst)
705 return true;
706
707 // Enter the scope of this instantiation. We don't use
708 // PushDeclContext because we don't have a scope.
709 DeclContext *PreviousContext = CurContext;
710 CurContext = Instantiation;
711
712 // Start the definition of this instantiation.
713 Instantiation->startDefinition();
714
John McCall0ba26ee2009-08-25 22:02:44 +0000715 // Do substitution on the base class specifiers.
716 if (SubstBaseSpecifiers(Instantiation, Pattern, TemplateArgs))
Douglas Gregorcc887972009-03-25 21:17:03 +0000717 Invalid = true;
718
Douglas Gregor4ac20ef2009-05-29 18:27:38 +0000719 llvm::SmallVector<DeclPtrTy, 4> Fields;
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000720 for (RecordDecl::decl_iterator Member = Pattern->decls_begin(),
721 MemberEnd = Pattern->decls_end();
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000722 Member != MemberEnd; ++Member) {
John McCall0ba26ee2009-08-25 22:02:44 +0000723 Decl *NewMember = SubstDecl(*Member, Instantiation, TemplateArgs);
Douglas Gregorcc887972009-03-25 21:17:03 +0000724 if (NewMember) {
725 if (NewMember->isInvalidDecl())
726 Invalid = true;
727 else if (FieldDecl *Field = dyn_cast<FieldDecl>(NewMember))
Chris Lattner5261d0c2009-03-28 19:18:32 +0000728 Fields.push_back(DeclPtrTy::make(Field));
Anders Carlsson8197c482009-08-29 19:37:28 +0000729 else if (UsingDecl *UD = dyn_cast<UsingDecl>(NewMember))
730 Instantiation->addDecl(UD);
Douglas Gregorcc887972009-03-25 21:17:03 +0000731 } else {
732 // FIXME: Eventually, a NULL return will mean that one of the
Mike Stumpe127ae32009-05-16 07:39:55 +0000733 // instantiations was a semantic disaster, and we'll want to set Invalid =
734 // true. For now, we expect to skip some members that we can't yet handle.
Douglas Gregorcc887972009-03-25 21:17:03 +0000735 }
736 }
737
738 // Finish checking fields.
Chris Lattner5261d0c2009-03-28 19:18:32 +0000739 ActOnFields(0, Instantiation->getLocation(), DeclPtrTy::make(Instantiation),
Jay Foad9e6bef42009-05-21 09:52:38 +0000740 Fields.data(), Fields.size(), SourceLocation(), SourceLocation(),
Douglas Gregorcc887972009-03-25 21:17:03 +0000741 0);
742
743 // Add any implicitly-declared members that we might need.
744 AddImplicitlyDeclaredMembersToClass(Instantiation);
745
746 // Exit the scope of this instantiation.
747 CurContext = PreviousContext;
748
Douglas Gregordc18e892009-05-26 20:50:29 +0000749 if (!Invalid)
750 Consumer.HandleTagDeclDefinition(Instantiation);
751
Douglas Gregordf9f5d12009-05-13 20:28:22 +0000752 // If this is an explicit instantiation, instantiate our members, too.
Douglas Gregor8e7e4602009-09-04 22:48:11 +0000753 if (!Invalid && TSK != TSK_ImplicitInstantiation) {
Douglas Gregorb12249d2009-05-18 17:01:57 +0000754 Inst.Clear();
Douglas Gregor8e7e4602009-09-04 22:48:11 +0000755 InstantiateClassMembers(PointOfInstantiation, Instantiation, TemplateArgs,
756 TSK);
Douglas Gregorb12249d2009-05-18 17:01:57 +0000757 }
Douglas Gregordf9f5d12009-05-13 20:28:22 +0000758
Douglas Gregorcc887972009-03-25 21:17:03 +0000759 return Invalid;
760}
761
Douglas Gregored3a3982009-03-03 04:44:36 +0000762bool
763Sema::InstantiateClassTemplateSpecialization(
764 ClassTemplateSpecializationDecl *ClassTemplateSpec,
Douglas Gregor8e7e4602009-09-04 22:48:11 +0000765 TemplateSpecializationKind TSK,
Douglas Gregorb35c7992009-08-24 15:23:48 +0000766 bool Complain) {
Douglas Gregored3a3982009-03-03 04:44:36 +0000767 // Perform the actual instantiation on the canonical declaration.
768 ClassTemplateSpec = cast<ClassTemplateSpecializationDecl>(
Argiris Kirtzidis17c7cab2009-07-18 00:34:25 +0000769 ClassTemplateSpec->getCanonicalDecl());
Douglas Gregored3a3982009-03-03 04:44:36 +0000770
771 // We can only instantiate something that hasn't already been
772 // instantiated or specialized. Fail without any diagnostics: our
773 // caller will provide an error message.
774 if (ClassTemplateSpec->getSpecializationKind() != TSK_Undeclared)
775 return true;
776
Douglas Gregored3a3982009-03-03 04:44:36 +0000777 ClassTemplateDecl *Template = ClassTemplateSpec->getSpecializedTemplate();
Douglas Gregor8dbd0382009-08-28 20:31:08 +0000778 CXXRecordDecl *Pattern = 0;
Douglas Gregor58944ac2009-05-31 09:31:02 +0000779
Douglas Gregor21530c52009-06-12 22:31:52 +0000780 // C++ [temp.class.spec.match]p1:
781 // When a class template is used in a context that requires an
782 // instantiation of the class, it is necessary to determine
783 // whether the instantiation is to be generated using the primary
784 // template or one of the partial specializations. This is done by
785 // matching the template arguments of the class template
786 // specialization with the template argument lists of the partial
787 // specializations.
Douglas Gregorab9f71a2009-06-05 00:53:49 +0000788 typedef std::pair<ClassTemplatePartialSpecializationDecl *,
789 TemplateArgumentList *> MatchResult;
790 llvm::SmallVector<MatchResult, 4> Matched;
Douglas Gregor58944ac2009-05-31 09:31:02 +0000791 for (llvm::FoldingSet<ClassTemplatePartialSpecializationDecl>::iterator
792 Partial = Template->getPartialSpecializations().begin(),
793 PartialEnd = Template->getPartialSpecializations().end();
794 Partial != PartialEnd;
795 ++Partial) {
Douglas Gregor623e2e02009-06-12 18:26:56 +0000796 TemplateDeductionInfo Info(Context);
797 if (TemplateDeductionResult Result
Douglas Gregorab9f71a2009-06-05 00:53:49 +0000798 = DeduceTemplateArguments(&*Partial,
Douglas Gregor623e2e02009-06-12 18:26:56 +0000799 ClassTemplateSpec->getTemplateArgs(),
800 Info)) {
801 // FIXME: Store the failed-deduction information for use in
802 // diagnostics, later.
803 (void)Result;
804 } else {
805 Matched.push_back(std::make_pair(&*Partial, Info.take()));
806 }
Douglas Gregor58944ac2009-05-31 09:31:02 +0000807 }
808
809 if (Matched.size() == 1) {
Douglas Gregor21530c52009-06-12 22:31:52 +0000810 // -- If exactly one matching specialization is found, the
811 // instantiation is generated from that specialization.
Douglas Gregorab9f71a2009-06-05 00:53:49 +0000812 Pattern = Matched[0].first;
Douglas Gregor7b0b83f2009-08-02 23:24:31 +0000813 ClassTemplateSpec->setInstantiationOf(Matched[0].first, Matched[0].second);
Douglas Gregor58944ac2009-05-31 09:31:02 +0000814 } else if (Matched.size() > 1) {
Douglas Gregor21530c52009-06-12 22:31:52 +0000815 // -- If more than one matching specialization is found, the
816 // partial order rules (14.5.4.2) are used to determine
817 // whether one of the specializations is more specialized
818 // than the others. If none of the specializations is more
819 // specialized than all of the other matching
820 // specializations, then the use of the class template is
821 // ambiguous and the program is ill-formed.
Douglas Gregor58944ac2009-05-31 09:31:02 +0000822 // FIXME: Implement partial ordering of class template partial
823 // specializations.
824 Diag(ClassTemplateSpec->getLocation(),
825 diag::unsup_template_partial_spec_ordering);
Douglas Gregor8dbd0382009-08-28 20:31:08 +0000826
827 // FIXME: Temporary hack to fall back to the primary template
828 ClassTemplateDecl *OrigTemplate = Template;
829 while (OrigTemplate->getInstantiatedFromMemberTemplate())
830 OrigTemplate = OrigTemplate->getInstantiatedFromMemberTemplate();
831
832 Pattern = OrigTemplate->getTemplatedDecl();
Douglas Gregor21530c52009-06-12 22:31:52 +0000833 } else {
834 // -- If no matches are found, the instantiation is generated
835 // from the primary template.
Douglas Gregor8dbd0382009-08-28 20:31:08 +0000836 ClassTemplateDecl *OrigTemplate = Template;
837 while (OrigTemplate->getInstantiatedFromMemberTemplate())
838 OrigTemplate = OrigTemplate->getInstantiatedFromMemberTemplate();
839
840 Pattern = OrigTemplate->getTemplatedDecl();
Douglas Gregor58944ac2009-05-31 09:31:02 +0000841 }
Douglas Gregored3a3982009-03-03 04:44:36 +0000842
Douglas Gregor8dbd0382009-08-28 20:31:08 +0000843 // Note that this is an instantiation.
Douglas Gregor8e7e4602009-09-04 22:48:11 +0000844 ClassTemplateSpec->setSpecializationKind(TSK);
Douglas Gregored3a3982009-03-03 04:44:36 +0000845
Douglas Gregorab9f71a2009-06-05 00:53:49 +0000846 bool Result = InstantiateClass(ClassTemplateSpec->getLocation(),
Douglas Gregor8dbd0382009-08-28 20:31:08 +0000847 ClassTemplateSpec, Pattern,
848 getTemplateInstantiationArgs(ClassTemplateSpec),
Douglas Gregor8e7e4602009-09-04 22:48:11 +0000849 TSK,
Douglas Gregorb35c7992009-08-24 15:23:48 +0000850 Complain);
Douglas Gregorab9f71a2009-06-05 00:53:49 +0000851
852 for (unsigned I = 0, N = Matched.size(); I != N; ++I) {
853 // FIXME: Implement TemplateArgumentList::Destroy!
854 // if (Matched[I].first != Pattern)
855 // Matched[I].second->Destroy(Context);
856 }
857
858 return Result;
Douglas Gregored3a3982009-03-03 04:44:36 +0000859}
Douglas Gregor47bde7c2009-03-19 17:26:29 +0000860
John McCall0ba26ee2009-08-25 22:02:44 +0000861/// \brief Instantiates the definitions of all of the member
862/// of the given class, which is an instantiation of a class template
863/// or a member class of a template.
Douglas Gregordf9f5d12009-05-13 20:28:22 +0000864void
865Sema::InstantiateClassMembers(SourceLocation PointOfInstantiation,
Douglas Gregor8e7e4602009-09-04 22:48:11 +0000866 CXXRecordDecl *Instantiation,
867 const MultiLevelTemplateArgumentList &TemplateArgs,
868 TemplateSpecializationKind TSK) {
869 // FIXME: extern templates
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000870 for (DeclContext::decl_iterator D = Instantiation->decls_begin(),
871 DEnd = Instantiation->decls_end();
Douglas Gregordf9f5d12009-05-13 20:28:22 +0000872 D != DEnd; ++D) {
873 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(*D)) {
Argiris Kirtzidisccb9efe2009-06-30 02:35:26 +0000874 if (!Function->getBody())
Douglas Gregorb12249d2009-05-18 17:01:57 +0000875 InstantiateFunctionDefinition(PointOfInstantiation, Function);
Douglas Gregordf9f5d12009-05-13 20:28:22 +0000876 } else if (VarDecl *Var = dyn_cast<VarDecl>(*D)) {
Douglas Gregor181fe792009-07-24 20:34:43 +0000877 if (Var->isStaticDataMember())
878 InstantiateStaticDataMemberDefinition(PointOfInstantiation, Var);
Douglas Gregordf9f5d12009-05-13 20:28:22 +0000879 } else if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(*D)) {
880 if (!Record->isInjectedClassName() && !Record->getDefinition(Context)) {
881 assert(Record->getInstantiatedFromMemberClass() &&
882 "Missing instantiated-from-template information");
Douglas Gregorb12249d2009-05-18 17:01:57 +0000883 InstantiateClass(PointOfInstantiation, Record,
Douglas Gregordf9f5d12009-05-13 20:28:22 +0000884 Record->getInstantiatedFromMemberClass(),
Douglas Gregor8e7e4602009-09-04 22:48:11 +0000885 TemplateArgs,
886 TSK);
Douglas Gregordf9f5d12009-05-13 20:28:22 +0000887 }
888 }
889 }
890}
891
892/// \brief Instantiate the definitions of all of the members of the
893/// given class template specialization, which was named as part of an
894/// explicit instantiation.
Douglas Gregor8e7e4602009-09-04 22:48:11 +0000895void
896Sema::InstantiateClassTemplateSpecializationMembers(
Douglas Gregordf9f5d12009-05-13 20:28:22 +0000897 SourceLocation PointOfInstantiation,
Douglas Gregor8e7e4602009-09-04 22:48:11 +0000898 ClassTemplateSpecializationDecl *ClassTemplateSpec,
899 TemplateSpecializationKind TSK) {
Douglas Gregordf9f5d12009-05-13 20:28:22 +0000900 // C++0x [temp.explicit]p7:
901 // An explicit instantiation that names a class template
902 // specialization is an explicit instantion of the same kind
903 // (declaration or definition) of each of its members (not
904 // including members inherited from base classes) that has not
905 // been previously explicitly specialized in the translation unit
906 // containing the explicit instantiation, except as described
907 // below.
908 InstantiateClassMembers(PointOfInstantiation, ClassTemplateSpec,
Douglas Gregor8e7e4602009-09-04 22:48:11 +0000909 getTemplateInstantiationArgs(ClassTemplateSpec),
910 TSK);
Douglas Gregordf9f5d12009-05-13 20:28:22 +0000911}
912
Douglas Gregor23a44be2009-08-20 07:17:43 +0000913Sema::OwningStmtResult
Douglas Gregor8dbd0382009-08-28 20:31:08 +0000914Sema::SubstStmt(Stmt *S, const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor23a44be2009-08-20 07:17:43 +0000915 if (!S)
916 return Owned(S);
917
918 TemplateInstantiator Instantiator(*this, TemplateArgs,
919 SourceLocation(),
920 DeclarationName());
921 return Instantiator.TransformStmt(S);
922}
923
Douglas Gregor9d879762009-08-11 05:31:07 +0000924Sema::OwningExprResult
Douglas Gregor8dbd0382009-08-28 20:31:08 +0000925Sema::SubstExpr(Expr *E, const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor9d879762009-08-11 05:31:07 +0000926 if (!E)
927 return Owned(E);
928
929 TemplateInstantiator Instantiator(*this, TemplateArgs,
930 SourceLocation(),
931 DeclarationName());
932 return Instantiator.TransformExpr(E);
933}
934
John McCall0ba26ee2009-08-25 22:02:44 +0000935/// \brief Do template substitution on a nested-name-specifier.
Douglas Gregor1e589cc2009-03-26 23:50:42 +0000936NestedNameSpecifier *
John McCall0ba26ee2009-08-25 22:02:44 +0000937Sema::SubstNestedNameSpecifier(NestedNameSpecifier *NNS,
Douglas Gregor8dbd0382009-08-28 20:31:08 +0000938 SourceRange Range,
939 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor12431cb2009-08-06 05:28:30 +0000940 TemplateInstantiator Instantiator(*this, TemplateArgs, Range.getBegin(),
941 DeclarationName());
942 return Instantiator.TransformNestedNameSpecifier(NNS, Range);
Douglas Gregor47bde7c2009-03-19 17:26:29 +0000943}
Douglas Gregor15a92852009-03-31 18:38:02 +0000944
945TemplateName
John McCall0ba26ee2009-08-25 22:02:44 +0000946Sema::SubstTemplateName(TemplateName Name, SourceLocation Loc,
Douglas Gregor8dbd0382009-08-28 20:31:08 +0000947 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor214d0462009-08-06 06:41:21 +0000948 TemplateInstantiator Instantiator(*this, TemplateArgs, Loc,
949 DeclarationName());
950 return Instantiator.TransformTemplateName(Name);
Douglas Gregor15a92852009-03-31 18:38:02 +0000951}
Douglas Gregorb320b9e2009-06-11 00:06:24 +0000952
John McCall0ba26ee2009-08-25 22:02:44 +0000953TemplateArgument Sema::Subst(TemplateArgument Arg,
Douglas Gregor8dbd0382009-08-28 20:31:08 +0000954 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor2999faa2009-08-04 22:27:00 +0000955 TemplateInstantiator Instantiator(*this, TemplateArgs, SourceLocation(),
956 DeclarationName());
957 return Instantiator.TransformTemplateArgument(Arg);
Douglas Gregorb320b9e2009-06-11 00:06:24 +0000958}