blob: 97b58adb06a278bfe440c41bf778030d08d4d0da [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());
Anders Carlsson21d18f22009-09-05 05:14:19 +0000296
297 std::string TemplateArgsStr
298 = TemplateSpecializationType::PrintTemplateArgumentList(
299 Active->TemplateArgs,
300 Active->NumTemplateArgs,
301 Context.PrintingPolicy);
302 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
303 diag::note_default_function_arg_instantiation_here)
Anders Carlssonf608ead2009-09-05 05:38:54 +0000304 << (FD->getNameAsString() + TemplateArgsStr)
Anders Carlsson21d18f22009-09-05 05:14:19 +0000305 << Active->InstantiationRange;
306 break;
307 }
308
Douglas Gregor56d25a72009-03-10 20:44:00 +0000309 }
Douglas Gregorfee85d62009-03-10 18:03:33 +0000310 }
311}
312
Douglas Gregor95d6c952009-06-14 07:33:30 +0000313bool Sema::isSFINAEContext() const {
314 using llvm::SmallVector;
315 for (SmallVector<ActiveTemplateInstantiation, 16>::const_reverse_iterator
316 Active = ActiveTemplateInstantiations.rbegin(),
317 ActiveEnd = ActiveTemplateInstantiations.rend();
318 Active != ActiveEnd;
319 ++Active) {
320
321 switch(Active->Kind) {
Douglas Gregor4c8b2b32009-07-01 22:01:06 +0000322 case ActiveTemplateInstantiation::TemplateInstantiation:
Anders Carlsson21d18f22009-09-05 05:14:19 +0000323 case ActiveTemplateInstantiation::DefaultFunctionArgumentInstantiation:
324
Douglas Gregor4c8b2b32009-07-01 22:01:06 +0000325 // This is a template instantiation, so there is no SFINAE.
326 return false;
327
Douglas Gregor95d6c952009-06-14 07:33:30 +0000328 case ActiveTemplateInstantiation::DefaultTemplateArgumentInstantiation:
329 // A default template argument instantiation may or may not be a
330 // SFINAE context; look further up the stack.
331 break;
Douglas Gregor4c8b2b32009-07-01 22:01:06 +0000332
333 case ActiveTemplateInstantiation::ExplicitTemplateArgumentSubstitution:
334 case ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution:
335 // We're either substitution explicitly-specified template arguments
336 // or deduced template arguments, so SFINAE applies.
337 return true;
Douglas Gregor95d6c952009-06-14 07:33:30 +0000338 }
339 }
340
341 return false;
342}
343
Douglas Gregor74296542009-02-27 19:31:52 +0000344//===----------------------------------------------------------------------===/
345// Template Instantiation for Types
346//===----------------------------------------------------------------------===/
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000347namespace {
Douglas Gregor841324a2009-08-04 16:50:30 +0000348 class VISIBILITY_HIDDEN TemplateInstantiator
349 : public TreeTransform<TemplateInstantiator>
350 {
Douglas Gregor8dbd0382009-08-28 20:31:08 +0000351 const MultiLevelTemplateArgumentList &TemplateArgs;
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000352 SourceLocation Loc;
353 DeclarationName Entity;
Douglas Gregor74296542009-02-27 19:31:52 +0000354
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000355 public:
Douglas Gregor23a44be2009-08-20 07:17:43 +0000356 typedef TreeTransform<TemplateInstantiator> inherited;
357
Douglas Gregor841324a2009-08-04 16:50:30 +0000358 TemplateInstantiator(Sema &SemaRef,
Douglas Gregor8dbd0382009-08-28 20:31:08 +0000359 const MultiLevelTemplateArgumentList &TemplateArgs,
Douglas Gregor841324a2009-08-04 16:50:30 +0000360 SourceLocation Loc,
361 DeclarationName Entity)
Douglas Gregor23a44be2009-08-20 07:17:43 +0000362 : inherited(SemaRef), TemplateArgs(TemplateArgs), Loc(Loc),
363 Entity(Entity) { }
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000364
Douglas Gregor841324a2009-08-04 16:50:30 +0000365 /// \brief Determine whether the given type \p T has already been
366 /// transformed.
367 ///
368 /// For the purposes of template instantiation, a type has already been
369 /// transformed if it is NULL or if it is not dependent.
370 bool AlreadyTransformed(QualType T) {
371 return T.isNull() || !T->isDependentType();
Douglas Gregor90177912009-05-13 18:28:20 +0000372 }
Douglas Gregor841324a2009-08-04 16:50:30 +0000373
374 /// \brief Returns the location of the entity being instantiated, if known.
375 SourceLocation getBaseLocation() { return Loc; }
376
377 /// \brief Returns the name of the entity being instantiated, if any.
378 DeclarationName getBaseEntity() { return Entity; }
379
Douglas Gregor841324a2009-08-04 16:50:30 +0000380 /// \brief Transform the given declaration by instantiating a reference to
381 /// this declaration.
382 Decl *TransformDecl(Decl *D);
Douglas Gregor9d879762009-08-11 05:31:07 +0000383
Douglas Gregor23a44be2009-08-20 07:17:43 +0000384 /// \brief Transform the definition of the given declaration by
385 /// instantiating it.
386 Decl *TransformDefinition(Decl *D);
387
388 /// \brief Rebuild the exception declaration and register the declaration
389 /// as an instantiated local.
390 VarDecl *RebuildExceptionDecl(VarDecl *ExceptionDecl, QualType T,
391 DeclaratorInfo *Declarator,
392 IdentifierInfo *Name,
393 SourceLocation Loc, SourceRange TypeRange);
Douglas Gregor9d879762009-08-11 05:31:07 +0000394
395 Sema::OwningExprResult TransformDeclRefExpr(DeclRefExpr *E);
396
Douglas Gregor841324a2009-08-04 16:50:30 +0000397 /// \brief Transforms a template type parameter type by performing
398 /// substitution of the corresponding template type argument.
399 QualType TransformTemplateTypeParmType(const TemplateTypeParmType *T);
400 };
Douglas Gregor1d381132009-07-06 15:59:29 +0000401}
402
Douglas Gregor841324a2009-08-04 16:50:30 +0000403Decl *TemplateInstantiator::TransformDecl(Decl *D) {
Douglas Gregorbc2fb7f2009-09-03 21:38:09 +0000404 if (!D)
405 return 0;
406
407 if (TemplateTemplateParmDecl *TTP = dyn_cast<TemplateTemplateParmDecl>(D)) {
Douglas Gregor8dbd0382009-08-28 20:31:08 +0000408 if (TTP->getDepth() < TemplateArgs.getNumLevels()) {
409 assert(TemplateArgs(TTP->getDepth(), TTP->getPosition()).getAsDecl() &&
410 "Wrong kind of template template argument");
411 return cast<TemplateDecl>(TemplateArgs(TTP->getDepth(),
412 TTP->getPosition()).getAsDecl());
413 }
414
415 // If the corresponding template argument is NULL or non-existent, it's
416 // because we are performing instantiation from explicitly-specified
417 // template arguments in a function template, but there were some
418 // arguments left unspecified.
419 if (!TemplateArgs.hasTemplateArgument(TTP->getDepth(),
420 TTP->getPosition()))
421 return D;
422
423 // FIXME: Implement depth reduction of template template parameters
424 assert(false &&
425 "Reducing depth of template template parameters is not yet implemented");
Douglas Gregor214d0462009-08-06 06:41:21 +0000426 }
427
Douglas Gregorbc2fb7f2009-09-03 21:38:09 +0000428 return SemaRef.FindInstantiatedDecl(cast<NamedDecl>(D));
Douglas Gregor841324a2009-08-04 16:50:30 +0000429}
430
Douglas Gregor23a44be2009-08-20 07:17:43 +0000431Decl *TemplateInstantiator::TransformDefinition(Decl *D) {
John McCall0ba26ee2009-08-25 22:02:44 +0000432 Decl *Inst = getSema().SubstDecl(D, getSema().CurContext, TemplateArgs);
Douglas Gregor23a44be2009-08-20 07:17:43 +0000433 if (!Inst)
434 return 0;
435
436 getSema().CurrentInstantiationScope->InstantiatedLocal(D, Inst);
437 return Inst;
438}
439
440VarDecl *
441TemplateInstantiator::RebuildExceptionDecl(VarDecl *ExceptionDecl,
442 QualType T,
443 DeclaratorInfo *Declarator,
444 IdentifierInfo *Name,
445 SourceLocation Loc,
446 SourceRange TypeRange) {
447 VarDecl *Var = inherited::RebuildExceptionDecl(ExceptionDecl, T, Declarator,
448 Name, Loc, TypeRange);
449 if (Var && !Var->isInvalidDecl())
450 getSema().CurrentInstantiationScope->InstantiatedLocal(ExceptionDecl, Var);
451 return Var;
452}
453
Douglas Gregor9d879762009-08-11 05:31:07 +0000454Sema::OwningExprResult
455TemplateInstantiator::TransformDeclRefExpr(DeclRefExpr *E) {
456 // FIXME: Clean this up a bit
457 NamedDecl *D = E->getDecl();
458 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(D)) {
Douglas Gregor8dbd0382009-08-28 20:31:08 +0000459 if (NTTP->getDepth() >= TemplateArgs.getNumLevels()) {
460 assert(false && "Cannot reduce non-type template parameter depth yet");
461 return getSema().ExprError();
462 }
Douglas Gregor9d879762009-08-11 05:31:07 +0000463
464 // If the corresponding template argument is NULL or non-existent, it's
465 // because we are performing instantiation from explicitly-specified
466 // template arguments in a function template, but there were some
467 // arguments left unspecified.
Douglas Gregor8dbd0382009-08-28 20:31:08 +0000468 if (!TemplateArgs.hasTemplateArgument(NTTP->getDepth(),
469 NTTP->getPosition()))
470 return SemaRef.Owned(E->Retain());
Douglas Gregor9d879762009-08-11 05:31:07 +0000471
Douglas Gregor8dbd0382009-08-28 20:31:08 +0000472 const TemplateArgument &Arg = TemplateArgs(NTTP->getDepth(),
473 NTTP->getPosition());
Douglas Gregor9d879762009-08-11 05:31:07 +0000474
475 // The template argument itself might be an expression, in which
476 // case we just return that expression.
477 if (Arg.getKind() == TemplateArgument::Expression)
Douglas Gregor8dbd0382009-08-28 20:31:08 +0000478 return SemaRef.Owned(Arg.getAsExpr()->Retain());
Douglas Gregor9d879762009-08-11 05:31:07 +0000479
480 if (Arg.getKind() == TemplateArgument::Declaration) {
481 ValueDecl *VD = cast<ValueDecl>(Arg.getAsDecl());
482
Douglas Gregor8dbd0382009-08-28 20:31:08 +0000483 VD = cast_or_null<ValueDecl>(getSema().FindInstantiatedDecl(VD));
484 if (!VD)
485 return SemaRef.ExprError();
486
Douglas Gregor9d879762009-08-11 05:31:07 +0000487 return SemaRef.BuildDeclRefExpr(VD, VD->getType(), E->getLocation(),
Douglas Gregor8dbd0382009-08-28 20:31:08 +0000488 /*FIXME:*/false, /*FIXME:*/false);
Douglas Gregor9d879762009-08-11 05:31:07 +0000489 }
490
491 assert(Arg.getKind() == TemplateArgument::Integral);
492 QualType T = Arg.getIntegralType();
493 if (T->isCharType() || T->isWideCharType())
494 return SemaRef.Owned(new (SemaRef.Context) CharacterLiteral(
Douglas Gregor8dbd0382009-08-28 20:31:08 +0000495 Arg.getAsIntegral()->getZExtValue(),
496 T->isWideCharType(),
497 T,
498 E->getSourceRange().getBegin()));
Douglas Gregor9d879762009-08-11 05:31:07 +0000499 if (T->isBooleanType())
500 return SemaRef.Owned(new (SemaRef.Context) CXXBoolLiteralExpr(
Douglas Gregor8dbd0382009-08-28 20:31:08 +0000501 Arg.getAsIntegral()->getBoolValue(),
502 T,
503 E->getSourceRange().getBegin()));
Douglas Gregor9d879762009-08-11 05:31:07 +0000504
505 assert(Arg.getAsIntegral()->getBitWidth() == SemaRef.Context.getIntWidth(T));
506 return SemaRef.Owned(new (SemaRef.Context) IntegerLiteral(
Douglas Gregor8dbd0382009-08-28 20:31:08 +0000507 *Arg.getAsIntegral(),
508 T,
509 E->getSourceRange().getBegin()));
Douglas Gregor9d879762009-08-11 05:31:07 +0000510 }
511
John McCall0ba26ee2009-08-25 22:02:44 +0000512 NamedDecl *InstD = SemaRef.FindInstantiatedDecl(D);
Douglas Gregor9d879762009-08-11 05:31:07 +0000513 if (!InstD)
514 return SemaRef.ExprError();
515
Anders Carlsson8197c482009-08-29 19:37:28 +0000516 // If we instantiated an UnresolvedUsingDecl and got back an UsingDecl,
517 // we need to get the underlying decl.
518 // FIXME: Is this correct? Maybe FindInstantiatedDecl should do this?
519 InstD = InstD->getUnderlyingDecl();
520
Douglas Gregor9d879762009-08-11 05:31:07 +0000521 // FIXME: nested-name-specifier for QualifiedDeclRefExpr
522 return SemaRef.BuildDeclarationNameExpr(E->getLocation(), InstD,
523 /*FIXME:*/false,
524 /*FIXME:*/0,
525 /*FIXME:*/false);
526}
527
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000528QualType
Douglas Gregor841324a2009-08-04 16:50:30 +0000529TemplateInstantiator::TransformTemplateTypeParmType(
530 const TemplateTypeParmType *T) {
Douglas Gregor8dbd0382009-08-28 20:31:08 +0000531 if (T->getDepth() < TemplateArgs.getNumLevels()) {
Douglas Gregor74296542009-02-27 19:31:52 +0000532 // Replace the template type parameter with its corresponding
533 // template argument.
Douglas Gregorecd63b82009-07-01 00:28:38 +0000534
535 // If the corresponding template argument is NULL or doesn't exist, it's
536 // because we are performing instantiation from explicitly-specified
537 // template arguments in a function template class, but there were some
538 // arguments left unspecified.
Douglas Gregor8dbd0382009-08-28 20:31:08 +0000539 if (!TemplateArgs.hasTemplateArgument(T->getDepth(), T->getIndex()))
540 return QualType(T, 0);
541
542 assert(TemplateArgs(T->getDepth(), T->getIndex()).getKind()
543 == TemplateArgument::Type &&
Douglas Gregor74296542009-02-27 19:31:52 +0000544 "Template argument kind mismatch");
Douglas Gregor8dbd0382009-08-28 20:31:08 +0000545
546 return TemplateArgs(T->getDepth(), T->getIndex()).getAsType();
Douglas Gregor74296542009-02-27 19:31:52 +0000547 }
548
549 // The template type parameter comes from an inner template (e.g.,
550 // the template parameter list of a member template inside the
551 // template we are instantiating). Create a new template type
552 // parameter with the template "level" reduced by one.
Douglas Gregor8dbd0382009-08-28 20:31:08 +0000553 return getSema().Context.getTemplateTypeParmType(
554 T->getDepth() - TemplateArgs.getNumLevels(),
Douglas Gregor841324a2009-08-04 16:50:30 +0000555 T->getIndex(),
556 T->isParameterPack(),
557 T->getName());
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000558}
Douglas Gregor74296542009-02-27 19:31:52 +0000559
John McCall0ba26ee2009-08-25 22:02:44 +0000560/// \brief Perform substitution on the type T with a given set of template
561/// arguments.
Douglas Gregor74296542009-02-27 19:31:52 +0000562///
563/// This routine substitutes the given template arguments into the
564/// type T and produces the instantiated type.
565///
566/// \param T the type into which the template arguments will be
567/// substituted. If this type is not dependent, it will be returned
568/// immediately.
569///
570/// \param TemplateArgs the template arguments that will be
571/// substituted for the top-level template parameters within T.
572///
Douglas Gregor74296542009-02-27 19:31:52 +0000573/// \param Loc the location in the source code where this substitution
574/// is being performed. It will typically be the location of the
575/// declarator (if we're instantiating the type of some declaration)
576/// or the location of the type in the source code (if, e.g., we're
577/// instantiating the type of a cast expression).
578///
579/// \param Entity the name of the entity associated with a declaration
580/// being instantiated (if any). May be empty to indicate that there
581/// is no such entity (if, e.g., this is a type that occurs as part of
582/// a cast expression) or that the entity has no name (e.g., an
583/// unnamed function parameter).
584///
585/// \returns If the instantiation succeeds, the instantiated
586/// type. Otherwise, produces diagnostics and returns a NULL type.
John McCall0ba26ee2009-08-25 22:02:44 +0000587QualType Sema::SubstType(QualType T,
Douglas Gregor8dbd0382009-08-28 20:31:08 +0000588 const MultiLevelTemplateArgumentList &TemplateArgs,
John McCall0ba26ee2009-08-25 22:02:44 +0000589 SourceLocation Loc, DeclarationName Entity) {
Douglas Gregor56d25a72009-03-10 20:44:00 +0000590 assert(!ActiveTemplateInstantiations.empty() &&
591 "Cannot perform an instantiation without some context on the "
592 "instantiation stack");
593
Douglas Gregor74296542009-02-27 19:31:52 +0000594 // If T is not a dependent type, there is nothing to do.
595 if (!T->isDependentType())
596 return T;
597
Douglas Gregor841324a2009-08-04 16:50:30 +0000598 TemplateInstantiator Instantiator(*this, TemplateArgs, Loc, Entity);
599 return Instantiator.TransformType(T);
Douglas Gregor74296542009-02-27 19:31:52 +0000600}
Douglas Gregored3a3982009-03-03 04:44:36 +0000601
John McCall0ba26ee2009-08-25 22:02:44 +0000602/// \brief Perform substitution on the base class specifiers of the
603/// given class template specialization.
Douglas Gregored3a3982009-03-03 04:44:36 +0000604///
605/// Produces a diagnostic and returns true on error, returns false and
606/// attaches the instantiated base classes to the class template
607/// specialization if successful.
608bool
John McCall0ba26ee2009-08-25 22:02:44 +0000609Sema::SubstBaseSpecifiers(CXXRecordDecl *Instantiation,
610 CXXRecordDecl *Pattern,
Douglas Gregor8dbd0382009-08-28 20:31:08 +0000611 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregored3a3982009-03-03 04:44:36 +0000612 bool Invalid = false;
Douglas Gregor4ac20ef2009-05-29 18:27:38 +0000613 llvm::SmallVector<CXXBaseSpecifier*, 4> InstantiatedBases;
Douglas Gregorcc887972009-03-25 21:17:03 +0000614 for (ClassTemplateSpecializationDecl::base_class_iterator
615 Base = Pattern->bases_begin(), BaseEnd = Pattern->bases_end();
Douglas Gregord9572a12009-03-10 18:52:44 +0000616 Base != BaseEnd; ++Base) {
Douglas Gregored3a3982009-03-03 04:44:36 +0000617 if (!Base->getType()->isDependentType()) {
Fariborz Jahanian1373b6f2009-07-22 17:41:53 +0000618 InstantiatedBases.push_back(new (Context) CXXBaseSpecifier(*Base));
Douglas Gregored3a3982009-03-03 04:44:36 +0000619 continue;
620 }
621
John McCall0ba26ee2009-08-25 22:02:44 +0000622 QualType BaseType = SubstType(Base->getType(),
623 TemplateArgs,
624 Base->getSourceRange().getBegin(),
625 DeclarationName());
Douglas Gregored3a3982009-03-03 04:44:36 +0000626 if (BaseType.isNull()) {
627 Invalid = true;
628 continue;
629 }
630
631 if (CXXBaseSpecifier *InstantiatedBase
Douglas Gregorcc887972009-03-25 21:17:03 +0000632 = CheckBaseSpecifier(Instantiation,
Douglas Gregored3a3982009-03-03 04:44:36 +0000633 Base->getSourceRange(),
634 Base->isVirtual(),
635 Base->getAccessSpecifierAsWritten(),
636 BaseType,
637 /*FIXME: Not totally accurate */
638 Base->getSourceRange().getBegin()))
639 InstantiatedBases.push_back(InstantiatedBase);
640 else
641 Invalid = true;
642 }
643
Douglas Gregord9572a12009-03-10 18:52:44 +0000644 if (!Invalid &&
Jay Foad9e6bef42009-05-21 09:52:38 +0000645 AttachBaseSpecifiers(Instantiation, InstantiatedBases.data(),
Douglas Gregored3a3982009-03-03 04:44:36 +0000646 InstantiatedBases.size()))
647 Invalid = true;
648
649 return Invalid;
650}
651
Douglas Gregorcc887972009-03-25 21:17:03 +0000652/// \brief Instantiate the definition of a class from a given pattern.
653///
654/// \param PointOfInstantiation The point of instantiation within the
655/// source code.
656///
657/// \param Instantiation is the declaration whose definition is being
658/// instantiated. This will be either a class template specialization
659/// or a member class of a class template specialization.
660///
661/// \param Pattern is the pattern from which the instantiation
662/// occurs. This will be either the declaration of a class template or
663/// the declaration of a member class of a class template.
664///
665/// \param TemplateArgs The template arguments to be substituted into
666/// the pattern.
667///
Douglas Gregor8e7e4602009-09-04 22:48:11 +0000668/// \param TSK the kind of implicit or explicit instantiation to perform.
Douglas Gregorb35c7992009-08-24 15:23:48 +0000669///
670/// \param Complain whether to complain if the class cannot be instantiated due
671/// to the lack of a definition.
672///
Douglas Gregorcc887972009-03-25 21:17:03 +0000673/// \returns true if an error occurred, false otherwise.
674bool
675Sema::InstantiateClass(SourceLocation PointOfInstantiation,
676 CXXRecordDecl *Instantiation, CXXRecordDecl *Pattern,
Douglas Gregor8dbd0382009-08-28 20:31:08 +0000677 const MultiLevelTemplateArgumentList &TemplateArgs,
Douglas Gregor8e7e4602009-09-04 22:48:11 +0000678 TemplateSpecializationKind TSK,
Douglas Gregorb35c7992009-08-24 15:23:48 +0000679 bool Complain) {
Douglas Gregorcc887972009-03-25 21:17:03 +0000680 bool Invalid = false;
John McCalld64b41e2009-08-20 01:44:21 +0000681
Douglas Gregorcc887972009-03-25 21:17:03 +0000682 CXXRecordDecl *PatternDef
683 = cast_or_null<CXXRecordDecl>(Pattern->getDefinition(Context));
684 if (!PatternDef) {
Douglas Gregorb35c7992009-08-24 15:23:48 +0000685 if (!Complain) {
686 // Say nothing
687 } else if (Pattern == Instantiation->getInstantiatedFromMemberClass()) {
Douglas Gregorcc887972009-03-25 21:17:03 +0000688 Diag(PointOfInstantiation,
689 diag::err_implicit_instantiate_member_undefined)
690 << Context.getTypeDeclType(Instantiation);
691 Diag(Pattern->getLocation(), diag::note_member_of_template_here);
692 } else {
Douglas Gregorfd79ac62009-05-13 00:25:59 +0000693 Diag(PointOfInstantiation, diag::err_template_instantiate_undefined)
Douglas Gregor8e7e4602009-09-04 22:48:11 +0000694 << (TSK != TSK_ImplicitInstantiation)
Douglas Gregorcc887972009-03-25 21:17:03 +0000695 << Context.getTypeDeclType(Instantiation);
696 Diag(Pattern->getLocation(), diag::note_template_decl_here);
697 }
698 return true;
699 }
700 Pattern = PatternDef;
701
Douglas Gregor42c48522009-03-25 21:23:52 +0000702 InstantiatingTemplate Inst(*this, PointOfInstantiation, Instantiation);
Douglas Gregorcc887972009-03-25 21:17:03 +0000703 if (Inst)
704 return true;
705
706 // Enter the scope of this instantiation. We don't use
707 // PushDeclContext because we don't have a scope.
708 DeclContext *PreviousContext = CurContext;
709 CurContext = Instantiation;
710
711 // Start the definition of this instantiation.
712 Instantiation->startDefinition();
713
John McCall0ba26ee2009-08-25 22:02:44 +0000714 // Do substitution on the base class specifiers.
715 if (SubstBaseSpecifiers(Instantiation, Pattern, TemplateArgs))
Douglas Gregorcc887972009-03-25 21:17:03 +0000716 Invalid = true;
717
Douglas Gregor4ac20ef2009-05-29 18:27:38 +0000718 llvm::SmallVector<DeclPtrTy, 4> Fields;
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000719 for (RecordDecl::decl_iterator Member = Pattern->decls_begin(),
720 MemberEnd = Pattern->decls_end();
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000721 Member != MemberEnd; ++Member) {
John McCall0ba26ee2009-08-25 22:02:44 +0000722 Decl *NewMember = SubstDecl(*Member, Instantiation, TemplateArgs);
Douglas Gregorcc887972009-03-25 21:17:03 +0000723 if (NewMember) {
724 if (NewMember->isInvalidDecl())
725 Invalid = true;
726 else if (FieldDecl *Field = dyn_cast<FieldDecl>(NewMember))
Chris Lattner5261d0c2009-03-28 19:18:32 +0000727 Fields.push_back(DeclPtrTy::make(Field));
Anders Carlsson8197c482009-08-29 19:37:28 +0000728 else if (UsingDecl *UD = dyn_cast<UsingDecl>(NewMember))
729 Instantiation->addDecl(UD);
Douglas Gregorcc887972009-03-25 21:17:03 +0000730 } else {
731 // FIXME: Eventually, a NULL return will mean that one of the
Mike Stumpe127ae32009-05-16 07:39:55 +0000732 // instantiations was a semantic disaster, and we'll want to set Invalid =
733 // true. For now, we expect to skip some members that we can't yet handle.
Douglas Gregorcc887972009-03-25 21:17:03 +0000734 }
735 }
736
737 // Finish checking fields.
Chris Lattner5261d0c2009-03-28 19:18:32 +0000738 ActOnFields(0, Instantiation->getLocation(), DeclPtrTy::make(Instantiation),
Jay Foad9e6bef42009-05-21 09:52:38 +0000739 Fields.data(), Fields.size(), SourceLocation(), SourceLocation(),
Douglas Gregorcc887972009-03-25 21:17:03 +0000740 0);
741
742 // Add any implicitly-declared members that we might need.
743 AddImplicitlyDeclaredMembersToClass(Instantiation);
744
745 // Exit the scope of this instantiation.
746 CurContext = PreviousContext;
747
Douglas Gregordc18e892009-05-26 20:50:29 +0000748 if (!Invalid)
749 Consumer.HandleTagDeclDefinition(Instantiation);
750
Douglas Gregordf9f5d12009-05-13 20:28:22 +0000751 // If this is an explicit instantiation, instantiate our members, too.
Douglas Gregor8e7e4602009-09-04 22:48:11 +0000752 if (!Invalid && TSK != TSK_ImplicitInstantiation) {
Douglas Gregorb12249d2009-05-18 17:01:57 +0000753 Inst.Clear();
Douglas Gregor8e7e4602009-09-04 22:48:11 +0000754 InstantiateClassMembers(PointOfInstantiation, Instantiation, TemplateArgs,
755 TSK);
Douglas Gregorb12249d2009-05-18 17:01:57 +0000756 }
Douglas Gregordf9f5d12009-05-13 20:28:22 +0000757
Douglas Gregorcc887972009-03-25 21:17:03 +0000758 return Invalid;
759}
760
Douglas Gregored3a3982009-03-03 04:44:36 +0000761bool
762Sema::InstantiateClassTemplateSpecialization(
763 ClassTemplateSpecializationDecl *ClassTemplateSpec,
Douglas Gregor8e7e4602009-09-04 22:48:11 +0000764 TemplateSpecializationKind TSK,
Douglas Gregorb35c7992009-08-24 15:23:48 +0000765 bool Complain) {
Douglas Gregored3a3982009-03-03 04:44:36 +0000766 // Perform the actual instantiation on the canonical declaration.
767 ClassTemplateSpec = cast<ClassTemplateSpecializationDecl>(
Argiris Kirtzidis17c7cab2009-07-18 00:34:25 +0000768 ClassTemplateSpec->getCanonicalDecl());
Douglas Gregored3a3982009-03-03 04:44:36 +0000769
770 // We can only instantiate something that hasn't already been
771 // instantiated or specialized. Fail without any diagnostics: our
772 // caller will provide an error message.
773 if (ClassTemplateSpec->getSpecializationKind() != TSK_Undeclared)
774 return true;
775
Douglas Gregored3a3982009-03-03 04:44:36 +0000776 ClassTemplateDecl *Template = ClassTemplateSpec->getSpecializedTemplate();
Douglas Gregor8dbd0382009-08-28 20:31:08 +0000777 CXXRecordDecl *Pattern = 0;
Douglas Gregor58944ac2009-05-31 09:31:02 +0000778
Douglas Gregor21530c52009-06-12 22:31:52 +0000779 // C++ [temp.class.spec.match]p1:
780 // When a class template is used in a context that requires an
781 // instantiation of the class, it is necessary to determine
782 // whether the instantiation is to be generated using the primary
783 // template or one of the partial specializations. This is done by
784 // matching the template arguments of the class template
785 // specialization with the template argument lists of the partial
786 // specializations.
Douglas Gregorab9f71a2009-06-05 00:53:49 +0000787 typedef std::pair<ClassTemplatePartialSpecializationDecl *,
788 TemplateArgumentList *> MatchResult;
789 llvm::SmallVector<MatchResult, 4> Matched;
Douglas Gregor58944ac2009-05-31 09:31:02 +0000790 for (llvm::FoldingSet<ClassTemplatePartialSpecializationDecl>::iterator
791 Partial = Template->getPartialSpecializations().begin(),
792 PartialEnd = Template->getPartialSpecializations().end();
793 Partial != PartialEnd;
794 ++Partial) {
Douglas Gregor623e2e02009-06-12 18:26:56 +0000795 TemplateDeductionInfo Info(Context);
796 if (TemplateDeductionResult Result
Douglas Gregorab9f71a2009-06-05 00:53:49 +0000797 = DeduceTemplateArguments(&*Partial,
Douglas Gregor623e2e02009-06-12 18:26:56 +0000798 ClassTemplateSpec->getTemplateArgs(),
799 Info)) {
800 // FIXME: Store the failed-deduction information for use in
801 // diagnostics, later.
802 (void)Result;
803 } else {
804 Matched.push_back(std::make_pair(&*Partial, Info.take()));
805 }
Douglas Gregor58944ac2009-05-31 09:31:02 +0000806 }
807
808 if (Matched.size() == 1) {
Douglas Gregor21530c52009-06-12 22:31:52 +0000809 // -- If exactly one matching specialization is found, the
810 // instantiation is generated from that specialization.
Douglas Gregorab9f71a2009-06-05 00:53:49 +0000811 Pattern = Matched[0].first;
Douglas Gregor7b0b83f2009-08-02 23:24:31 +0000812 ClassTemplateSpec->setInstantiationOf(Matched[0].first, Matched[0].second);
Douglas Gregor58944ac2009-05-31 09:31:02 +0000813 } else if (Matched.size() > 1) {
Douglas Gregor21530c52009-06-12 22:31:52 +0000814 // -- If more than one matching specialization is found, the
815 // partial order rules (14.5.4.2) are used to determine
816 // whether one of the specializations is more specialized
817 // than the others. If none of the specializations is more
818 // specialized than all of the other matching
819 // specializations, then the use of the class template is
820 // ambiguous and the program is ill-formed.
Douglas Gregor58944ac2009-05-31 09:31:02 +0000821 // FIXME: Implement partial ordering of class template partial
822 // specializations.
823 Diag(ClassTemplateSpec->getLocation(),
824 diag::unsup_template_partial_spec_ordering);
Douglas Gregor8dbd0382009-08-28 20:31:08 +0000825
826 // FIXME: Temporary hack to fall back to the primary template
827 ClassTemplateDecl *OrigTemplate = Template;
828 while (OrigTemplate->getInstantiatedFromMemberTemplate())
829 OrigTemplate = OrigTemplate->getInstantiatedFromMemberTemplate();
830
831 Pattern = OrigTemplate->getTemplatedDecl();
Douglas Gregor21530c52009-06-12 22:31:52 +0000832 } else {
833 // -- If no matches are found, the instantiation is generated
834 // from the primary template.
Douglas Gregor8dbd0382009-08-28 20:31:08 +0000835 ClassTemplateDecl *OrigTemplate = Template;
836 while (OrigTemplate->getInstantiatedFromMemberTemplate())
837 OrigTemplate = OrigTemplate->getInstantiatedFromMemberTemplate();
838
839 Pattern = OrigTemplate->getTemplatedDecl();
Douglas Gregor58944ac2009-05-31 09:31:02 +0000840 }
Douglas Gregored3a3982009-03-03 04:44:36 +0000841
Douglas Gregor8dbd0382009-08-28 20:31:08 +0000842 // Note that this is an instantiation.
Douglas Gregor8e7e4602009-09-04 22:48:11 +0000843 ClassTemplateSpec->setSpecializationKind(TSK);
Douglas Gregored3a3982009-03-03 04:44:36 +0000844
Douglas Gregorab9f71a2009-06-05 00:53:49 +0000845 bool Result = InstantiateClass(ClassTemplateSpec->getLocation(),
Douglas Gregor8dbd0382009-08-28 20:31:08 +0000846 ClassTemplateSpec, Pattern,
847 getTemplateInstantiationArgs(ClassTemplateSpec),
Douglas Gregor8e7e4602009-09-04 22:48:11 +0000848 TSK,
Douglas Gregorb35c7992009-08-24 15:23:48 +0000849 Complain);
Douglas Gregorab9f71a2009-06-05 00:53:49 +0000850
851 for (unsigned I = 0, N = Matched.size(); I != N; ++I) {
852 // FIXME: Implement TemplateArgumentList::Destroy!
853 // if (Matched[I].first != Pattern)
854 // Matched[I].second->Destroy(Context);
855 }
856
857 return Result;
Douglas Gregored3a3982009-03-03 04:44:36 +0000858}
Douglas Gregor47bde7c2009-03-19 17:26:29 +0000859
John McCall0ba26ee2009-08-25 22:02:44 +0000860/// \brief Instantiates the definitions of all of the member
861/// of the given class, which is an instantiation of a class template
862/// or a member class of a template.
Douglas Gregordf9f5d12009-05-13 20:28:22 +0000863void
864Sema::InstantiateClassMembers(SourceLocation PointOfInstantiation,
Douglas Gregor8e7e4602009-09-04 22:48:11 +0000865 CXXRecordDecl *Instantiation,
866 const MultiLevelTemplateArgumentList &TemplateArgs,
867 TemplateSpecializationKind TSK) {
868 // FIXME: extern templates
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000869 for (DeclContext::decl_iterator D = Instantiation->decls_begin(),
870 DEnd = Instantiation->decls_end();
Douglas Gregordf9f5d12009-05-13 20:28:22 +0000871 D != DEnd; ++D) {
872 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(*D)) {
Argiris Kirtzidisccb9efe2009-06-30 02:35:26 +0000873 if (!Function->getBody())
Douglas Gregorb12249d2009-05-18 17:01:57 +0000874 InstantiateFunctionDefinition(PointOfInstantiation, Function);
Douglas Gregordf9f5d12009-05-13 20:28:22 +0000875 } else if (VarDecl *Var = dyn_cast<VarDecl>(*D)) {
Douglas Gregor181fe792009-07-24 20:34:43 +0000876 if (Var->isStaticDataMember())
877 InstantiateStaticDataMemberDefinition(PointOfInstantiation, Var);
Douglas Gregordf9f5d12009-05-13 20:28:22 +0000878 } else if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(*D)) {
879 if (!Record->isInjectedClassName() && !Record->getDefinition(Context)) {
880 assert(Record->getInstantiatedFromMemberClass() &&
881 "Missing instantiated-from-template information");
Douglas Gregorb12249d2009-05-18 17:01:57 +0000882 InstantiateClass(PointOfInstantiation, Record,
Douglas Gregordf9f5d12009-05-13 20:28:22 +0000883 Record->getInstantiatedFromMemberClass(),
Douglas Gregor8e7e4602009-09-04 22:48:11 +0000884 TemplateArgs,
885 TSK);
Douglas Gregordf9f5d12009-05-13 20:28:22 +0000886 }
887 }
888 }
889}
890
891/// \brief Instantiate the definitions of all of the members of the
892/// given class template specialization, which was named as part of an
893/// explicit instantiation.
Douglas Gregor8e7e4602009-09-04 22:48:11 +0000894void
895Sema::InstantiateClassTemplateSpecializationMembers(
Douglas Gregordf9f5d12009-05-13 20:28:22 +0000896 SourceLocation PointOfInstantiation,
Douglas Gregor8e7e4602009-09-04 22:48:11 +0000897 ClassTemplateSpecializationDecl *ClassTemplateSpec,
898 TemplateSpecializationKind TSK) {
Douglas Gregordf9f5d12009-05-13 20:28:22 +0000899 // C++0x [temp.explicit]p7:
900 // An explicit instantiation that names a class template
901 // specialization is an explicit instantion of the same kind
902 // (declaration or definition) of each of its members (not
903 // including members inherited from base classes) that has not
904 // been previously explicitly specialized in the translation unit
905 // containing the explicit instantiation, except as described
906 // below.
907 InstantiateClassMembers(PointOfInstantiation, ClassTemplateSpec,
Douglas Gregor8e7e4602009-09-04 22:48:11 +0000908 getTemplateInstantiationArgs(ClassTemplateSpec),
909 TSK);
Douglas Gregordf9f5d12009-05-13 20:28:22 +0000910}
911
Douglas Gregor23a44be2009-08-20 07:17:43 +0000912Sema::OwningStmtResult
Douglas Gregor8dbd0382009-08-28 20:31:08 +0000913Sema::SubstStmt(Stmt *S, const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor23a44be2009-08-20 07:17:43 +0000914 if (!S)
915 return Owned(S);
916
917 TemplateInstantiator Instantiator(*this, TemplateArgs,
918 SourceLocation(),
919 DeclarationName());
920 return Instantiator.TransformStmt(S);
921}
922
Douglas Gregor9d879762009-08-11 05:31:07 +0000923Sema::OwningExprResult
Douglas Gregor8dbd0382009-08-28 20:31:08 +0000924Sema::SubstExpr(Expr *E, const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor9d879762009-08-11 05:31:07 +0000925 if (!E)
926 return Owned(E);
927
928 TemplateInstantiator Instantiator(*this, TemplateArgs,
929 SourceLocation(),
930 DeclarationName());
931 return Instantiator.TransformExpr(E);
932}
933
John McCall0ba26ee2009-08-25 22:02:44 +0000934/// \brief Do template substitution on a nested-name-specifier.
Douglas Gregor1e589cc2009-03-26 23:50:42 +0000935NestedNameSpecifier *
John McCall0ba26ee2009-08-25 22:02:44 +0000936Sema::SubstNestedNameSpecifier(NestedNameSpecifier *NNS,
Douglas Gregor8dbd0382009-08-28 20:31:08 +0000937 SourceRange Range,
938 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor12431cb2009-08-06 05:28:30 +0000939 TemplateInstantiator Instantiator(*this, TemplateArgs, Range.getBegin(),
940 DeclarationName());
941 return Instantiator.TransformNestedNameSpecifier(NNS, Range);
Douglas Gregor47bde7c2009-03-19 17:26:29 +0000942}
Douglas Gregor15a92852009-03-31 18:38:02 +0000943
944TemplateName
John McCall0ba26ee2009-08-25 22:02:44 +0000945Sema::SubstTemplateName(TemplateName Name, SourceLocation Loc,
Douglas Gregor8dbd0382009-08-28 20:31:08 +0000946 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor214d0462009-08-06 06:41:21 +0000947 TemplateInstantiator Instantiator(*this, TemplateArgs, Loc,
948 DeclarationName());
949 return Instantiator.TransformTemplateName(Name);
Douglas Gregor15a92852009-03-31 18:38:02 +0000950}
Douglas Gregorb320b9e2009-06-11 00:06:24 +0000951
John McCall0ba26ee2009-08-25 22:02:44 +0000952TemplateArgument Sema::Subst(TemplateArgument Arg,
Douglas Gregor8dbd0382009-08-28 20:31:08 +0000953 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor2999faa2009-08-04 22:27:00 +0000954 TemplateInstantiator Instantiator(*this, TemplateArgs, SourceLocation(),
955 DeclarationName());
956 return Instantiator.TransformTemplateArgument(Arg);
Douglas Gregorb320b9e2009-06-11 00:06:24 +0000957}