blob: 55e81aecbc76ad3cd770f0c32fcafdcc43ad3f20 [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
Douglas Gregorb12249d2009-05-18 17:01:57 +0000167void Sema::InstantiatingTemplate::Clear() {
168 if (!Invalid) {
Douglas Gregor375733c2009-03-10 00:06:19 +0000169 SemaRef.ActiveTemplateInstantiations.pop_back();
Douglas Gregorb12249d2009-05-18 17:01:57 +0000170 Invalid = true;
171 }
Douglas Gregor375733c2009-03-10 00:06:19 +0000172}
173
Douglas Gregor56d25a72009-03-10 20:44:00 +0000174bool Sema::InstantiatingTemplate::CheckInstantiationDepth(
175 SourceLocation PointOfInstantiation,
176 SourceRange InstantiationRange) {
177 if (SemaRef.ActiveTemplateInstantiations.size()
178 <= SemaRef.getLangOptions().InstantiationDepth)
179 return false;
180
181 SemaRef.Diag(PointOfInstantiation,
182 diag::err_template_recursion_depth_exceeded)
183 << SemaRef.getLangOptions().InstantiationDepth
184 << InstantiationRange;
185 SemaRef.Diag(PointOfInstantiation, diag::note_template_recursion_depth)
186 << SemaRef.getLangOptions().InstantiationDepth;
187 return true;
188}
189
Douglas Gregorfee85d62009-03-10 18:03:33 +0000190/// \brief Prints the current instantiation stack through a series of
191/// notes.
192void Sema::PrintInstantiationStack() {
Douglas Gregor4c8b2b32009-07-01 22:01:06 +0000193 // FIXME: In all of these cases, we need to show the template arguments
Douglas Gregorfee85d62009-03-10 18:03:33 +0000194 for (llvm::SmallVector<ActiveTemplateInstantiation, 16>::reverse_iterator
195 Active = ActiveTemplateInstantiations.rbegin(),
196 ActiveEnd = ActiveTemplateInstantiations.rend();
197 Active != ActiveEnd;
198 ++Active) {
Douglas Gregor56d25a72009-03-10 20:44:00 +0000199 switch (Active->Kind) {
200 case ActiveTemplateInstantiation::TemplateInstantiation: {
Douglas Gregorb12249d2009-05-18 17:01:57 +0000201 Decl *D = reinterpret_cast<Decl *>(Active->Entity);
202 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) {
203 unsigned DiagID = diag::note_template_member_class_here;
204 if (isa<ClassTemplateSpecializationDecl>(Record))
205 DiagID = diag::note_template_class_instantiation_here;
206 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
207 DiagID)
208 << Context.getTypeDeclType(Record)
209 << Active->InstantiationRange;
Douglas Gregor181fe792009-07-24 20:34:43 +0000210 } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
Douglas Gregor6f5e0542009-06-26 00:10:03 +0000211 unsigned DiagID;
212 if (Function->getPrimaryTemplate())
213 DiagID = diag::note_function_template_spec_here;
214 else
215 DiagID = diag::note_template_member_function_here;
Douglas Gregorb12249d2009-05-18 17:01:57 +0000216 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
217 DiagID)
218 << Function
219 << Active->InstantiationRange;
Douglas Gregor181fe792009-07-24 20:34:43 +0000220 } else {
221 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
222 diag::note_template_static_data_member_def_here)
223 << cast<VarDecl>(D)
224 << Active->InstantiationRange;
Douglas Gregorb12249d2009-05-18 17:01:57 +0000225 }
Douglas Gregor56d25a72009-03-10 20:44:00 +0000226 break;
227 }
228
229 case ActiveTemplateInstantiation::DefaultTemplateArgumentInstantiation: {
230 TemplateDecl *Template = cast<TemplateDecl>((Decl *)Active->Entity);
231 std::string TemplateArgsStr
Douglas Gregordd13e842009-03-30 22:58:21 +0000232 = TemplateSpecializationType::PrintTemplateArgumentList(
Douglas Gregor4c8b2b32009-07-01 22:01:06 +0000233 Active->TemplateArgs,
Douglas Gregor3bf3bbc2009-05-29 20:38:28 +0000234 Active->NumTemplateArgs,
235 Context.PrintingPolicy);
Douglas Gregor56d25a72009-03-10 20:44:00 +0000236 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
237 diag::note_default_arg_instantiation_here)
238 << (Template->getNameAsString() + TemplateArgsStr)
239 << Active->InstantiationRange;
240 break;
241 }
Douglas Gregorc5e01af2009-06-10 23:47:09 +0000242
Douglas Gregor4c8b2b32009-07-01 22:01:06 +0000243 case ActiveTemplateInstantiation::ExplicitTemplateArgumentSubstitution: {
244 FunctionTemplateDecl *FnTmpl
245 = cast<FunctionTemplateDecl>((Decl *)Active->Entity);
Douglas Gregorc5e01af2009-06-10 23:47:09 +0000246 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
Douglas Gregor4c8b2b32009-07-01 22:01:06 +0000247 diag::note_explicit_template_arg_substitution_here)
248 << FnTmpl << Active->InstantiationRange;
Douglas Gregorc5e01af2009-06-10 23:47:09 +0000249 break;
250 }
Douglas Gregor4c8b2b32009-07-01 22:01:06 +0000251
252 case ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution:
253 if (ClassTemplatePartialSpecializationDecl *PartialSpec
254 = dyn_cast<ClassTemplatePartialSpecializationDecl>(
255 (Decl *)Active->Entity)) {
256 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
257 diag::note_partial_spec_deduct_instantiation_here)
258 << Context.getTypeDeclType(PartialSpec)
259 << Active->InstantiationRange;
260 } else {
261 FunctionTemplateDecl *FnTmpl
262 = cast<FunctionTemplateDecl>((Decl *)Active->Entity);
263 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
264 diag::note_function_template_deduction_instantiation_here)
265 << FnTmpl << Active->InstantiationRange;
266 }
267 break;
Douglas Gregorc5e01af2009-06-10 23:47:09 +0000268
Douglas Gregor56d25a72009-03-10 20:44:00 +0000269 }
Douglas Gregorfee85d62009-03-10 18:03:33 +0000270 }
271}
272
Douglas Gregor95d6c952009-06-14 07:33:30 +0000273bool Sema::isSFINAEContext() const {
274 using llvm::SmallVector;
275 for (SmallVector<ActiveTemplateInstantiation, 16>::const_reverse_iterator
276 Active = ActiveTemplateInstantiations.rbegin(),
277 ActiveEnd = ActiveTemplateInstantiations.rend();
278 Active != ActiveEnd;
279 ++Active) {
280
281 switch(Active->Kind) {
Douglas Gregor4c8b2b32009-07-01 22:01:06 +0000282 case ActiveTemplateInstantiation::TemplateInstantiation:
283 // This is a template instantiation, so there is no SFINAE.
284 return false;
285
Douglas Gregor95d6c952009-06-14 07:33:30 +0000286 case ActiveTemplateInstantiation::DefaultTemplateArgumentInstantiation:
287 // A default template argument instantiation may or may not be a
288 // SFINAE context; look further up the stack.
289 break;
Douglas Gregor4c8b2b32009-07-01 22:01:06 +0000290
291 case ActiveTemplateInstantiation::ExplicitTemplateArgumentSubstitution:
292 case ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution:
293 // We're either substitution explicitly-specified template arguments
294 // or deduced template arguments, so SFINAE applies.
295 return true;
Douglas Gregor95d6c952009-06-14 07:33:30 +0000296 }
297 }
298
299 return false;
300}
301
Douglas Gregor74296542009-02-27 19:31:52 +0000302//===----------------------------------------------------------------------===/
303// Template Instantiation for Types
304//===----------------------------------------------------------------------===/
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000305namespace {
Douglas Gregor841324a2009-08-04 16:50:30 +0000306 class VISIBILITY_HIDDEN TemplateInstantiator
307 : public TreeTransform<TemplateInstantiator>
308 {
Douglas Gregor8dbd0382009-08-28 20:31:08 +0000309 const MultiLevelTemplateArgumentList &TemplateArgs;
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000310 SourceLocation Loc;
311 DeclarationName Entity;
Douglas Gregor74296542009-02-27 19:31:52 +0000312
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000313 public:
Douglas Gregor23a44be2009-08-20 07:17:43 +0000314 typedef TreeTransform<TemplateInstantiator> inherited;
315
Douglas Gregor841324a2009-08-04 16:50:30 +0000316 TemplateInstantiator(Sema &SemaRef,
Douglas Gregor8dbd0382009-08-28 20:31:08 +0000317 const MultiLevelTemplateArgumentList &TemplateArgs,
Douglas Gregor841324a2009-08-04 16:50:30 +0000318 SourceLocation Loc,
319 DeclarationName Entity)
Douglas Gregor23a44be2009-08-20 07:17:43 +0000320 : inherited(SemaRef), TemplateArgs(TemplateArgs), Loc(Loc),
321 Entity(Entity) { }
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000322
Douglas Gregor841324a2009-08-04 16:50:30 +0000323 /// \brief Determine whether the given type \p T has already been
324 /// transformed.
325 ///
326 /// For the purposes of template instantiation, a type has already been
327 /// transformed if it is NULL or if it is not dependent.
328 bool AlreadyTransformed(QualType T) {
329 return T.isNull() || !T->isDependentType();
Douglas Gregor90177912009-05-13 18:28:20 +0000330 }
Douglas Gregor841324a2009-08-04 16:50:30 +0000331
332 /// \brief Returns the location of the entity being instantiated, if known.
333 SourceLocation getBaseLocation() { return Loc; }
334
335 /// \brief Returns the name of the entity being instantiated, if any.
336 DeclarationName getBaseEntity() { return Entity; }
337
Douglas Gregor841324a2009-08-04 16:50:30 +0000338 /// \brief Transform the given declaration by instantiating a reference to
339 /// this declaration.
340 Decl *TransformDecl(Decl *D);
Douglas Gregor9d879762009-08-11 05:31:07 +0000341
Douglas Gregor23a44be2009-08-20 07:17:43 +0000342 /// \brief Transform the definition of the given declaration by
343 /// instantiating it.
344 Decl *TransformDefinition(Decl *D);
345
346 /// \brief Rebuild the exception declaration and register the declaration
347 /// as an instantiated local.
348 VarDecl *RebuildExceptionDecl(VarDecl *ExceptionDecl, QualType T,
349 DeclaratorInfo *Declarator,
350 IdentifierInfo *Name,
351 SourceLocation Loc, SourceRange TypeRange);
Douglas Gregor9d879762009-08-11 05:31:07 +0000352
353 Sema::OwningExprResult TransformDeclRefExpr(DeclRefExpr *E);
354
Douglas Gregor841324a2009-08-04 16:50:30 +0000355 /// \brief Transforms a template type parameter type by performing
356 /// substitution of the corresponding template type argument.
357 QualType TransformTemplateTypeParmType(const TemplateTypeParmType *T);
358 };
Douglas Gregor1d381132009-07-06 15:59:29 +0000359}
360
Douglas Gregor841324a2009-08-04 16:50:30 +0000361Decl *TemplateInstantiator::TransformDecl(Decl *D) {
Douglas Gregorbc2fb7f2009-09-03 21:38:09 +0000362 if (!D)
363 return 0;
364
365 if (TemplateTemplateParmDecl *TTP = dyn_cast<TemplateTemplateParmDecl>(D)) {
Douglas Gregor8dbd0382009-08-28 20:31:08 +0000366 if (TTP->getDepth() < TemplateArgs.getNumLevels()) {
367 assert(TemplateArgs(TTP->getDepth(), TTP->getPosition()).getAsDecl() &&
368 "Wrong kind of template template argument");
369 return cast<TemplateDecl>(TemplateArgs(TTP->getDepth(),
370 TTP->getPosition()).getAsDecl());
371 }
372
373 // If the corresponding template argument is NULL or non-existent, it's
374 // because we are performing instantiation from explicitly-specified
375 // template arguments in a function template, but there were some
376 // arguments left unspecified.
377 if (!TemplateArgs.hasTemplateArgument(TTP->getDepth(),
378 TTP->getPosition()))
379 return D;
380
381 // FIXME: Implement depth reduction of template template parameters
382 assert(false &&
383 "Reducing depth of template template parameters is not yet implemented");
Douglas Gregor214d0462009-08-06 06:41:21 +0000384 }
385
Douglas Gregorbc2fb7f2009-09-03 21:38:09 +0000386 return SemaRef.FindInstantiatedDecl(cast<NamedDecl>(D));
Douglas Gregor841324a2009-08-04 16:50:30 +0000387}
388
Douglas Gregor23a44be2009-08-20 07:17:43 +0000389Decl *TemplateInstantiator::TransformDefinition(Decl *D) {
John McCall0ba26ee2009-08-25 22:02:44 +0000390 Decl *Inst = getSema().SubstDecl(D, getSema().CurContext, TemplateArgs);
Douglas Gregor23a44be2009-08-20 07:17:43 +0000391 if (!Inst)
392 return 0;
393
394 getSema().CurrentInstantiationScope->InstantiatedLocal(D, Inst);
395 return Inst;
396}
397
398VarDecl *
399TemplateInstantiator::RebuildExceptionDecl(VarDecl *ExceptionDecl,
400 QualType T,
401 DeclaratorInfo *Declarator,
402 IdentifierInfo *Name,
403 SourceLocation Loc,
404 SourceRange TypeRange) {
405 VarDecl *Var = inherited::RebuildExceptionDecl(ExceptionDecl, T, Declarator,
406 Name, Loc, TypeRange);
407 if (Var && !Var->isInvalidDecl())
408 getSema().CurrentInstantiationScope->InstantiatedLocal(ExceptionDecl, Var);
409 return Var;
410}
411
Douglas Gregor9d879762009-08-11 05:31:07 +0000412Sema::OwningExprResult
413TemplateInstantiator::TransformDeclRefExpr(DeclRefExpr *E) {
414 // FIXME: Clean this up a bit
415 NamedDecl *D = E->getDecl();
416 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(D)) {
Douglas Gregor8dbd0382009-08-28 20:31:08 +0000417 if (NTTP->getDepth() >= TemplateArgs.getNumLevels()) {
418 assert(false && "Cannot reduce non-type template parameter depth yet");
419 return getSema().ExprError();
420 }
Douglas Gregor9d879762009-08-11 05:31:07 +0000421
422 // If the corresponding template argument is NULL or non-existent, it's
423 // because we are performing instantiation from explicitly-specified
424 // template arguments in a function template, but there were some
425 // arguments left unspecified.
Douglas Gregor8dbd0382009-08-28 20:31:08 +0000426 if (!TemplateArgs.hasTemplateArgument(NTTP->getDepth(),
427 NTTP->getPosition()))
428 return SemaRef.Owned(E->Retain());
Douglas Gregor9d879762009-08-11 05:31:07 +0000429
Douglas Gregor8dbd0382009-08-28 20:31:08 +0000430 const TemplateArgument &Arg = TemplateArgs(NTTP->getDepth(),
431 NTTP->getPosition());
Douglas Gregor9d879762009-08-11 05:31:07 +0000432
433 // The template argument itself might be an expression, in which
434 // case we just return that expression.
435 if (Arg.getKind() == TemplateArgument::Expression)
Douglas Gregor8dbd0382009-08-28 20:31:08 +0000436 return SemaRef.Owned(Arg.getAsExpr()->Retain());
Douglas Gregor9d879762009-08-11 05:31:07 +0000437
438 if (Arg.getKind() == TemplateArgument::Declaration) {
439 ValueDecl *VD = cast<ValueDecl>(Arg.getAsDecl());
440
Douglas Gregor8dbd0382009-08-28 20:31:08 +0000441 VD = cast_or_null<ValueDecl>(getSema().FindInstantiatedDecl(VD));
442 if (!VD)
443 return SemaRef.ExprError();
444
Douglas Gregor9d879762009-08-11 05:31:07 +0000445 return SemaRef.BuildDeclRefExpr(VD, VD->getType(), E->getLocation(),
Douglas Gregor8dbd0382009-08-28 20:31:08 +0000446 /*FIXME:*/false, /*FIXME:*/false);
Douglas Gregor9d879762009-08-11 05:31:07 +0000447 }
448
449 assert(Arg.getKind() == TemplateArgument::Integral);
450 QualType T = Arg.getIntegralType();
451 if (T->isCharType() || T->isWideCharType())
452 return SemaRef.Owned(new (SemaRef.Context) CharacterLiteral(
Douglas Gregor8dbd0382009-08-28 20:31:08 +0000453 Arg.getAsIntegral()->getZExtValue(),
454 T->isWideCharType(),
455 T,
456 E->getSourceRange().getBegin()));
Douglas Gregor9d879762009-08-11 05:31:07 +0000457 if (T->isBooleanType())
458 return SemaRef.Owned(new (SemaRef.Context) CXXBoolLiteralExpr(
Douglas Gregor8dbd0382009-08-28 20:31:08 +0000459 Arg.getAsIntegral()->getBoolValue(),
460 T,
461 E->getSourceRange().getBegin()));
Douglas Gregor9d879762009-08-11 05:31:07 +0000462
463 assert(Arg.getAsIntegral()->getBitWidth() == SemaRef.Context.getIntWidth(T));
464 return SemaRef.Owned(new (SemaRef.Context) IntegerLiteral(
Douglas Gregor8dbd0382009-08-28 20:31:08 +0000465 *Arg.getAsIntegral(),
466 T,
467 E->getSourceRange().getBegin()));
Douglas Gregor9d879762009-08-11 05:31:07 +0000468 }
469
John McCall0ba26ee2009-08-25 22:02:44 +0000470 NamedDecl *InstD = SemaRef.FindInstantiatedDecl(D);
Douglas Gregor9d879762009-08-11 05:31:07 +0000471 if (!InstD)
472 return SemaRef.ExprError();
473
Anders Carlsson8197c482009-08-29 19:37:28 +0000474 // If we instantiated an UnresolvedUsingDecl and got back an UsingDecl,
475 // we need to get the underlying decl.
476 // FIXME: Is this correct? Maybe FindInstantiatedDecl should do this?
477 InstD = InstD->getUnderlyingDecl();
478
Douglas Gregor9d879762009-08-11 05:31:07 +0000479 // FIXME: nested-name-specifier for QualifiedDeclRefExpr
480 return SemaRef.BuildDeclarationNameExpr(E->getLocation(), InstD,
481 /*FIXME:*/false,
482 /*FIXME:*/0,
483 /*FIXME:*/false);
484}
485
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000486QualType
Douglas Gregor841324a2009-08-04 16:50:30 +0000487TemplateInstantiator::TransformTemplateTypeParmType(
488 const TemplateTypeParmType *T) {
Douglas Gregor8dbd0382009-08-28 20:31:08 +0000489 if (T->getDepth() < TemplateArgs.getNumLevels()) {
Douglas Gregor74296542009-02-27 19:31:52 +0000490 // Replace the template type parameter with its corresponding
491 // template argument.
Douglas Gregorecd63b82009-07-01 00:28:38 +0000492
493 // If the corresponding template argument is NULL or doesn't exist, it's
494 // because we are performing instantiation from explicitly-specified
495 // template arguments in a function template class, but there were some
496 // arguments left unspecified.
Douglas Gregor8dbd0382009-08-28 20:31:08 +0000497 if (!TemplateArgs.hasTemplateArgument(T->getDepth(), T->getIndex()))
498 return QualType(T, 0);
499
500 assert(TemplateArgs(T->getDepth(), T->getIndex()).getKind()
501 == TemplateArgument::Type &&
Douglas Gregor74296542009-02-27 19:31:52 +0000502 "Template argument kind mismatch");
Douglas Gregor8dbd0382009-08-28 20:31:08 +0000503
504 return TemplateArgs(T->getDepth(), T->getIndex()).getAsType();
Douglas Gregor74296542009-02-27 19:31:52 +0000505 }
506
507 // The template type parameter comes from an inner template (e.g.,
508 // the template parameter list of a member template inside the
509 // template we are instantiating). Create a new template type
510 // parameter with the template "level" reduced by one.
Douglas Gregor8dbd0382009-08-28 20:31:08 +0000511 return getSema().Context.getTemplateTypeParmType(
512 T->getDepth() - TemplateArgs.getNumLevels(),
Douglas Gregor841324a2009-08-04 16:50:30 +0000513 T->getIndex(),
514 T->isParameterPack(),
515 T->getName());
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000516}
Douglas Gregor74296542009-02-27 19:31:52 +0000517
John McCall0ba26ee2009-08-25 22:02:44 +0000518/// \brief Perform substitution on the type T with a given set of template
519/// arguments.
Douglas Gregor74296542009-02-27 19:31:52 +0000520///
521/// This routine substitutes the given template arguments into the
522/// type T and produces the instantiated type.
523///
524/// \param T the type into which the template arguments will be
525/// substituted. If this type is not dependent, it will be returned
526/// immediately.
527///
528/// \param TemplateArgs the template arguments that will be
529/// substituted for the top-level template parameters within T.
530///
Douglas Gregor74296542009-02-27 19:31:52 +0000531/// \param Loc the location in the source code where this substitution
532/// is being performed. It will typically be the location of the
533/// declarator (if we're instantiating the type of some declaration)
534/// or the location of the type in the source code (if, e.g., we're
535/// instantiating the type of a cast expression).
536///
537/// \param Entity the name of the entity associated with a declaration
538/// being instantiated (if any). May be empty to indicate that there
539/// is no such entity (if, e.g., this is a type that occurs as part of
540/// a cast expression) or that the entity has no name (e.g., an
541/// unnamed function parameter).
542///
543/// \returns If the instantiation succeeds, the instantiated
544/// type. Otherwise, produces diagnostics and returns a NULL type.
John McCall0ba26ee2009-08-25 22:02:44 +0000545QualType Sema::SubstType(QualType T,
Douglas Gregor8dbd0382009-08-28 20:31:08 +0000546 const MultiLevelTemplateArgumentList &TemplateArgs,
John McCall0ba26ee2009-08-25 22:02:44 +0000547 SourceLocation Loc, DeclarationName Entity) {
Douglas Gregor56d25a72009-03-10 20:44:00 +0000548 assert(!ActiveTemplateInstantiations.empty() &&
549 "Cannot perform an instantiation without some context on the "
550 "instantiation stack");
551
Douglas Gregor74296542009-02-27 19:31:52 +0000552 // If T is not a dependent type, there is nothing to do.
553 if (!T->isDependentType())
554 return T;
555
Douglas Gregor841324a2009-08-04 16:50:30 +0000556 TemplateInstantiator Instantiator(*this, TemplateArgs, Loc, Entity);
557 return Instantiator.TransformType(T);
Douglas Gregor74296542009-02-27 19:31:52 +0000558}
Douglas Gregored3a3982009-03-03 04:44:36 +0000559
John McCall0ba26ee2009-08-25 22:02:44 +0000560/// \brief Perform substitution on the base class specifiers of the
561/// given class template specialization.
Douglas Gregored3a3982009-03-03 04:44:36 +0000562///
563/// Produces a diagnostic and returns true on error, returns false and
564/// attaches the instantiated base classes to the class template
565/// specialization if successful.
566bool
John McCall0ba26ee2009-08-25 22:02:44 +0000567Sema::SubstBaseSpecifiers(CXXRecordDecl *Instantiation,
568 CXXRecordDecl *Pattern,
Douglas Gregor8dbd0382009-08-28 20:31:08 +0000569 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregored3a3982009-03-03 04:44:36 +0000570 bool Invalid = false;
Douglas Gregor4ac20ef2009-05-29 18:27:38 +0000571 llvm::SmallVector<CXXBaseSpecifier*, 4> InstantiatedBases;
Douglas Gregorcc887972009-03-25 21:17:03 +0000572 for (ClassTemplateSpecializationDecl::base_class_iterator
573 Base = Pattern->bases_begin(), BaseEnd = Pattern->bases_end();
Douglas Gregord9572a12009-03-10 18:52:44 +0000574 Base != BaseEnd; ++Base) {
Douglas Gregored3a3982009-03-03 04:44:36 +0000575 if (!Base->getType()->isDependentType()) {
Fariborz Jahanian1373b6f2009-07-22 17:41:53 +0000576 InstantiatedBases.push_back(new (Context) CXXBaseSpecifier(*Base));
Douglas Gregored3a3982009-03-03 04:44:36 +0000577 continue;
578 }
579
John McCall0ba26ee2009-08-25 22:02:44 +0000580 QualType BaseType = SubstType(Base->getType(),
581 TemplateArgs,
582 Base->getSourceRange().getBegin(),
583 DeclarationName());
Douglas Gregored3a3982009-03-03 04:44:36 +0000584 if (BaseType.isNull()) {
585 Invalid = true;
586 continue;
587 }
588
589 if (CXXBaseSpecifier *InstantiatedBase
Douglas Gregorcc887972009-03-25 21:17:03 +0000590 = CheckBaseSpecifier(Instantiation,
Douglas Gregored3a3982009-03-03 04:44:36 +0000591 Base->getSourceRange(),
592 Base->isVirtual(),
593 Base->getAccessSpecifierAsWritten(),
594 BaseType,
595 /*FIXME: Not totally accurate */
596 Base->getSourceRange().getBegin()))
597 InstantiatedBases.push_back(InstantiatedBase);
598 else
599 Invalid = true;
600 }
601
Douglas Gregord9572a12009-03-10 18:52:44 +0000602 if (!Invalid &&
Jay Foad9e6bef42009-05-21 09:52:38 +0000603 AttachBaseSpecifiers(Instantiation, InstantiatedBases.data(),
Douglas Gregored3a3982009-03-03 04:44:36 +0000604 InstantiatedBases.size()))
605 Invalid = true;
606
607 return Invalid;
608}
609
Douglas Gregorcc887972009-03-25 21:17:03 +0000610/// \brief Instantiate the definition of a class from a given pattern.
611///
612/// \param PointOfInstantiation The point of instantiation within the
613/// source code.
614///
615/// \param Instantiation is the declaration whose definition is being
616/// instantiated. This will be either a class template specialization
617/// or a member class of a class template specialization.
618///
619/// \param Pattern is the pattern from which the instantiation
620/// occurs. This will be either the declaration of a class template or
621/// the declaration of a member class of a class template.
622///
623/// \param TemplateArgs The template arguments to be substituted into
624/// the pattern.
625///
Douglas Gregor8e7e4602009-09-04 22:48:11 +0000626/// \param TSK the kind of implicit or explicit instantiation to perform.
Douglas Gregorb35c7992009-08-24 15:23:48 +0000627///
628/// \param Complain whether to complain if the class cannot be instantiated due
629/// to the lack of a definition.
630///
Douglas Gregorcc887972009-03-25 21:17:03 +0000631/// \returns true if an error occurred, false otherwise.
632bool
633Sema::InstantiateClass(SourceLocation PointOfInstantiation,
634 CXXRecordDecl *Instantiation, CXXRecordDecl *Pattern,
Douglas Gregor8dbd0382009-08-28 20:31:08 +0000635 const MultiLevelTemplateArgumentList &TemplateArgs,
Douglas Gregor8e7e4602009-09-04 22:48:11 +0000636 TemplateSpecializationKind TSK,
Douglas Gregorb35c7992009-08-24 15:23:48 +0000637 bool Complain) {
Douglas Gregorcc887972009-03-25 21:17:03 +0000638 bool Invalid = false;
John McCalld64b41e2009-08-20 01:44:21 +0000639
Douglas Gregorcc887972009-03-25 21:17:03 +0000640 CXXRecordDecl *PatternDef
641 = cast_or_null<CXXRecordDecl>(Pattern->getDefinition(Context));
642 if (!PatternDef) {
Douglas Gregorb35c7992009-08-24 15:23:48 +0000643 if (!Complain) {
644 // Say nothing
645 } else if (Pattern == Instantiation->getInstantiatedFromMemberClass()) {
Douglas Gregorcc887972009-03-25 21:17:03 +0000646 Diag(PointOfInstantiation,
647 diag::err_implicit_instantiate_member_undefined)
648 << Context.getTypeDeclType(Instantiation);
649 Diag(Pattern->getLocation(), diag::note_member_of_template_here);
650 } else {
Douglas Gregorfd79ac62009-05-13 00:25:59 +0000651 Diag(PointOfInstantiation, diag::err_template_instantiate_undefined)
Douglas Gregor8e7e4602009-09-04 22:48:11 +0000652 << (TSK != TSK_ImplicitInstantiation)
Douglas Gregorcc887972009-03-25 21:17:03 +0000653 << Context.getTypeDeclType(Instantiation);
654 Diag(Pattern->getLocation(), diag::note_template_decl_here);
655 }
656 return true;
657 }
658 Pattern = PatternDef;
659
Douglas Gregor42c48522009-03-25 21:23:52 +0000660 InstantiatingTemplate Inst(*this, PointOfInstantiation, Instantiation);
Douglas Gregorcc887972009-03-25 21:17:03 +0000661 if (Inst)
662 return true;
663
664 // Enter the scope of this instantiation. We don't use
665 // PushDeclContext because we don't have a scope.
666 DeclContext *PreviousContext = CurContext;
667 CurContext = Instantiation;
668
669 // Start the definition of this instantiation.
670 Instantiation->startDefinition();
671
John McCall0ba26ee2009-08-25 22:02:44 +0000672 // Do substitution on the base class specifiers.
673 if (SubstBaseSpecifiers(Instantiation, Pattern, TemplateArgs))
Douglas Gregorcc887972009-03-25 21:17:03 +0000674 Invalid = true;
675
Douglas Gregor4ac20ef2009-05-29 18:27:38 +0000676 llvm::SmallVector<DeclPtrTy, 4> Fields;
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000677 for (RecordDecl::decl_iterator Member = Pattern->decls_begin(),
678 MemberEnd = Pattern->decls_end();
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000679 Member != MemberEnd; ++Member) {
John McCall0ba26ee2009-08-25 22:02:44 +0000680 Decl *NewMember = SubstDecl(*Member, Instantiation, TemplateArgs);
Douglas Gregorcc887972009-03-25 21:17:03 +0000681 if (NewMember) {
682 if (NewMember->isInvalidDecl())
683 Invalid = true;
684 else if (FieldDecl *Field = dyn_cast<FieldDecl>(NewMember))
Chris Lattner5261d0c2009-03-28 19:18:32 +0000685 Fields.push_back(DeclPtrTy::make(Field));
Anders Carlsson8197c482009-08-29 19:37:28 +0000686 else if (UsingDecl *UD = dyn_cast<UsingDecl>(NewMember))
687 Instantiation->addDecl(UD);
Douglas Gregorcc887972009-03-25 21:17:03 +0000688 } else {
689 // FIXME: Eventually, a NULL return will mean that one of the
Mike Stumpe127ae32009-05-16 07:39:55 +0000690 // instantiations was a semantic disaster, and we'll want to set Invalid =
691 // true. For now, we expect to skip some members that we can't yet handle.
Douglas Gregorcc887972009-03-25 21:17:03 +0000692 }
693 }
694
695 // Finish checking fields.
Chris Lattner5261d0c2009-03-28 19:18:32 +0000696 ActOnFields(0, Instantiation->getLocation(), DeclPtrTy::make(Instantiation),
Jay Foad9e6bef42009-05-21 09:52:38 +0000697 Fields.data(), Fields.size(), SourceLocation(), SourceLocation(),
Douglas Gregorcc887972009-03-25 21:17:03 +0000698 0);
699
700 // Add any implicitly-declared members that we might need.
701 AddImplicitlyDeclaredMembersToClass(Instantiation);
702
703 // Exit the scope of this instantiation.
704 CurContext = PreviousContext;
705
Douglas Gregordc18e892009-05-26 20:50:29 +0000706 if (!Invalid)
707 Consumer.HandleTagDeclDefinition(Instantiation);
708
Douglas Gregordf9f5d12009-05-13 20:28:22 +0000709 // If this is an explicit instantiation, instantiate our members, too.
Douglas Gregor8e7e4602009-09-04 22:48:11 +0000710 if (!Invalid && TSK != TSK_ImplicitInstantiation) {
Douglas Gregorb12249d2009-05-18 17:01:57 +0000711 Inst.Clear();
Douglas Gregor8e7e4602009-09-04 22:48:11 +0000712 InstantiateClassMembers(PointOfInstantiation, Instantiation, TemplateArgs,
713 TSK);
Douglas Gregorb12249d2009-05-18 17:01:57 +0000714 }
Douglas Gregordf9f5d12009-05-13 20:28:22 +0000715
Douglas Gregorcc887972009-03-25 21:17:03 +0000716 return Invalid;
717}
718
Douglas Gregored3a3982009-03-03 04:44:36 +0000719bool
720Sema::InstantiateClassTemplateSpecialization(
721 ClassTemplateSpecializationDecl *ClassTemplateSpec,
Douglas Gregor8e7e4602009-09-04 22:48:11 +0000722 TemplateSpecializationKind TSK,
Douglas Gregorb35c7992009-08-24 15:23:48 +0000723 bool Complain) {
Douglas Gregored3a3982009-03-03 04:44:36 +0000724 // Perform the actual instantiation on the canonical declaration.
725 ClassTemplateSpec = cast<ClassTemplateSpecializationDecl>(
Argiris Kirtzidis17c7cab2009-07-18 00:34:25 +0000726 ClassTemplateSpec->getCanonicalDecl());
Douglas Gregored3a3982009-03-03 04:44:36 +0000727
728 // We can only instantiate something that hasn't already been
729 // instantiated or specialized. Fail without any diagnostics: our
730 // caller will provide an error message.
731 if (ClassTemplateSpec->getSpecializationKind() != TSK_Undeclared)
732 return true;
733
Douglas Gregored3a3982009-03-03 04:44:36 +0000734 ClassTemplateDecl *Template = ClassTemplateSpec->getSpecializedTemplate();
Douglas Gregor8dbd0382009-08-28 20:31:08 +0000735 CXXRecordDecl *Pattern = 0;
Douglas Gregor58944ac2009-05-31 09:31:02 +0000736
Douglas Gregor21530c52009-06-12 22:31:52 +0000737 // C++ [temp.class.spec.match]p1:
738 // When a class template is used in a context that requires an
739 // instantiation of the class, it is necessary to determine
740 // whether the instantiation is to be generated using the primary
741 // template or one of the partial specializations. This is done by
742 // matching the template arguments of the class template
743 // specialization with the template argument lists of the partial
744 // specializations.
Douglas Gregorab9f71a2009-06-05 00:53:49 +0000745 typedef std::pair<ClassTemplatePartialSpecializationDecl *,
746 TemplateArgumentList *> MatchResult;
747 llvm::SmallVector<MatchResult, 4> Matched;
Douglas Gregor58944ac2009-05-31 09:31:02 +0000748 for (llvm::FoldingSet<ClassTemplatePartialSpecializationDecl>::iterator
749 Partial = Template->getPartialSpecializations().begin(),
750 PartialEnd = Template->getPartialSpecializations().end();
751 Partial != PartialEnd;
752 ++Partial) {
Douglas Gregor623e2e02009-06-12 18:26:56 +0000753 TemplateDeductionInfo Info(Context);
754 if (TemplateDeductionResult Result
Douglas Gregorab9f71a2009-06-05 00:53:49 +0000755 = DeduceTemplateArguments(&*Partial,
Douglas Gregor623e2e02009-06-12 18:26:56 +0000756 ClassTemplateSpec->getTemplateArgs(),
757 Info)) {
758 // FIXME: Store the failed-deduction information for use in
759 // diagnostics, later.
760 (void)Result;
761 } else {
762 Matched.push_back(std::make_pair(&*Partial, Info.take()));
763 }
Douglas Gregor58944ac2009-05-31 09:31:02 +0000764 }
765
766 if (Matched.size() == 1) {
Douglas Gregor21530c52009-06-12 22:31:52 +0000767 // -- If exactly one matching specialization is found, the
768 // instantiation is generated from that specialization.
Douglas Gregorab9f71a2009-06-05 00:53:49 +0000769 Pattern = Matched[0].first;
Douglas Gregor7b0b83f2009-08-02 23:24:31 +0000770 ClassTemplateSpec->setInstantiationOf(Matched[0].first, Matched[0].second);
Douglas Gregor58944ac2009-05-31 09:31:02 +0000771 } else if (Matched.size() > 1) {
Douglas Gregor21530c52009-06-12 22:31:52 +0000772 // -- If more than one matching specialization is found, the
773 // partial order rules (14.5.4.2) are used to determine
774 // whether one of the specializations is more specialized
775 // than the others. If none of the specializations is more
776 // specialized than all of the other matching
777 // specializations, then the use of the class template is
778 // ambiguous and the program is ill-formed.
Douglas Gregor58944ac2009-05-31 09:31:02 +0000779 // FIXME: Implement partial ordering of class template partial
780 // specializations.
781 Diag(ClassTemplateSpec->getLocation(),
782 diag::unsup_template_partial_spec_ordering);
Douglas Gregor8dbd0382009-08-28 20:31:08 +0000783
784 // FIXME: Temporary hack to fall back to the primary template
785 ClassTemplateDecl *OrigTemplate = Template;
786 while (OrigTemplate->getInstantiatedFromMemberTemplate())
787 OrigTemplate = OrigTemplate->getInstantiatedFromMemberTemplate();
788
789 Pattern = OrigTemplate->getTemplatedDecl();
Douglas Gregor21530c52009-06-12 22:31:52 +0000790 } else {
791 // -- If no matches are found, the instantiation is generated
792 // from the primary template.
Douglas Gregor8dbd0382009-08-28 20:31:08 +0000793 ClassTemplateDecl *OrigTemplate = Template;
794 while (OrigTemplate->getInstantiatedFromMemberTemplate())
795 OrigTemplate = OrigTemplate->getInstantiatedFromMemberTemplate();
796
797 Pattern = OrigTemplate->getTemplatedDecl();
Douglas Gregor58944ac2009-05-31 09:31:02 +0000798 }
Douglas Gregored3a3982009-03-03 04:44:36 +0000799
Douglas Gregor8dbd0382009-08-28 20:31:08 +0000800 // Note that this is an instantiation.
Douglas Gregor8e7e4602009-09-04 22:48:11 +0000801 ClassTemplateSpec->setSpecializationKind(TSK);
Douglas Gregored3a3982009-03-03 04:44:36 +0000802
Douglas Gregorab9f71a2009-06-05 00:53:49 +0000803 bool Result = InstantiateClass(ClassTemplateSpec->getLocation(),
Douglas Gregor8dbd0382009-08-28 20:31:08 +0000804 ClassTemplateSpec, Pattern,
805 getTemplateInstantiationArgs(ClassTemplateSpec),
Douglas Gregor8e7e4602009-09-04 22:48:11 +0000806 TSK,
Douglas Gregorb35c7992009-08-24 15:23:48 +0000807 Complain);
Douglas Gregorab9f71a2009-06-05 00:53:49 +0000808
809 for (unsigned I = 0, N = Matched.size(); I != N; ++I) {
810 // FIXME: Implement TemplateArgumentList::Destroy!
811 // if (Matched[I].first != Pattern)
812 // Matched[I].second->Destroy(Context);
813 }
814
815 return Result;
Douglas Gregored3a3982009-03-03 04:44:36 +0000816}
Douglas Gregor47bde7c2009-03-19 17:26:29 +0000817
John McCall0ba26ee2009-08-25 22:02:44 +0000818/// \brief Instantiates the definitions of all of the member
819/// of the given class, which is an instantiation of a class template
820/// or a member class of a template.
Douglas Gregordf9f5d12009-05-13 20:28:22 +0000821void
822Sema::InstantiateClassMembers(SourceLocation PointOfInstantiation,
Douglas Gregor8e7e4602009-09-04 22:48:11 +0000823 CXXRecordDecl *Instantiation,
824 const MultiLevelTemplateArgumentList &TemplateArgs,
825 TemplateSpecializationKind TSK) {
826 // FIXME: extern templates
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000827 for (DeclContext::decl_iterator D = Instantiation->decls_begin(),
828 DEnd = Instantiation->decls_end();
Douglas Gregordf9f5d12009-05-13 20:28:22 +0000829 D != DEnd; ++D) {
830 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(*D)) {
Argiris Kirtzidisccb9efe2009-06-30 02:35:26 +0000831 if (!Function->getBody())
Douglas Gregorb12249d2009-05-18 17:01:57 +0000832 InstantiateFunctionDefinition(PointOfInstantiation, Function);
Douglas Gregordf9f5d12009-05-13 20:28:22 +0000833 } else if (VarDecl *Var = dyn_cast<VarDecl>(*D)) {
Douglas Gregor181fe792009-07-24 20:34:43 +0000834 if (Var->isStaticDataMember())
835 InstantiateStaticDataMemberDefinition(PointOfInstantiation, Var);
Douglas Gregordf9f5d12009-05-13 20:28:22 +0000836 } else if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(*D)) {
837 if (!Record->isInjectedClassName() && !Record->getDefinition(Context)) {
838 assert(Record->getInstantiatedFromMemberClass() &&
839 "Missing instantiated-from-template information");
Douglas Gregorb12249d2009-05-18 17:01:57 +0000840 InstantiateClass(PointOfInstantiation, Record,
Douglas Gregordf9f5d12009-05-13 20:28:22 +0000841 Record->getInstantiatedFromMemberClass(),
Douglas Gregor8e7e4602009-09-04 22:48:11 +0000842 TemplateArgs,
843 TSK);
Douglas Gregordf9f5d12009-05-13 20:28:22 +0000844 }
845 }
846 }
847}
848
849/// \brief Instantiate the definitions of all of the members of the
850/// given class template specialization, which was named as part of an
851/// explicit instantiation.
Douglas Gregor8e7e4602009-09-04 22:48:11 +0000852void
853Sema::InstantiateClassTemplateSpecializationMembers(
Douglas Gregordf9f5d12009-05-13 20:28:22 +0000854 SourceLocation PointOfInstantiation,
Douglas Gregor8e7e4602009-09-04 22:48:11 +0000855 ClassTemplateSpecializationDecl *ClassTemplateSpec,
856 TemplateSpecializationKind TSK) {
Douglas Gregordf9f5d12009-05-13 20:28:22 +0000857 // C++0x [temp.explicit]p7:
858 // An explicit instantiation that names a class template
859 // specialization is an explicit instantion of the same kind
860 // (declaration or definition) of each of its members (not
861 // including members inherited from base classes) that has not
862 // been previously explicitly specialized in the translation unit
863 // containing the explicit instantiation, except as described
864 // below.
865 InstantiateClassMembers(PointOfInstantiation, ClassTemplateSpec,
Douglas Gregor8e7e4602009-09-04 22:48:11 +0000866 getTemplateInstantiationArgs(ClassTemplateSpec),
867 TSK);
Douglas Gregordf9f5d12009-05-13 20:28:22 +0000868}
869
Douglas Gregor23a44be2009-08-20 07:17:43 +0000870Sema::OwningStmtResult
Douglas Gregor8dbd0382009-08-28 20:31:08 +0000871Sema::SubstStmt(Stmt *S, const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor23a44be2009-08-20 07:17:43 +0000872 if (!S)
873 return Owned(S);
874
875 TemplateInstantiator Instantiator(*this, TemplateArgs,
876 SourceLocation(),
877 DeclarationName());
878 return Instantiator.TransformStmt(S);
879}
880
Douglas Gregor9d879762009-08-11 05:31:07 +0000881Sema::OwningExprResult
Douglas Gregor8dbd0382009-08-28 20:31:08 +0000882Sema::SubstExpr(Expr *E, const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor9d879762009-08-11 05:31:07 +0000883 if (!E)
884 return Owned(E);
885
886 TemplateInstantiator Instantiator(*this, TemplateArgs,
887 SourceLocation(),
888 DeclarationName());
889 return Instantiator.TransformExpr(E);
890}
891
John McCall0ba26ee2009-08-25 22:02:44 +0000892/// \brief Do template substitution on a nested-name-specifier.
Douglas Gregor1e589cc2009-03-26 23:50:42 +0000893NestedNameSpecifier *
John McCall0ba26ee2009-08-25 22:02:44 +0000894Sema::SubstNestedNameSpecifier(NestedNameSpecifier *NNS,
Douglas Gregor8dbd0382009-08-28 20:31:08 +0000895 SourceRange Range,
896 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor12431cb2009-08-06 05:28:30 +0000897 TemplateInstantiator Instantiator(*this, TemplateArgs, Range.getBegin(),
898 DeclarationName());
899 return Instantiator.TransformNestedNameSpecifier(NNS, Range);
Douglas Gregor47bde7c2009-03-19 17:26:29 +0000900}
Douglas Gregor15a92852009-03-31 18:38:02 +0000901
902TemplateName
John McCall0ba26ee2009-08-25 22:02:44 +0000903Sema::SubstTemplateName(TemplateName Name, SourceLocation Loc,
Douglas Gregor8dbd0382009-08-28 20:31:08 +0000904 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor214d0462009-08-06 06:41:21 +0000905 TemplateInstantiator Instantiator(*this, TemplateArgs, Loc,
906 DeclarationName());
907 return Instantiator.TransformTemplateName(Name);
Douglas Gregor15a92852009-03-31 18:38:02 +0000908}
Douglas Gregorb320b9e2009-06-11 00:06:24 +0000909
John McCall0ba26ee2009-08-25 22:02:44 +0000910TemplateArgument Sema::Subst(TemplateArgument Arg,
Douglas Gregor8dbd0382009-08-28 20:31:08 +0000911 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor2999faa2009-08-04 22:27:00 +0000912 TemplateInstantiator Instantiator(*this, TemplateArgs, SourceLocation(),
913 DeclarationName());
914 return Instantiator.TransformTemplateArgument(Arg);
Douglas Gregorb320b9e2009-06-11 00:06:24 +0000915}