blob: 4cb5a276e64fa5a07d30bda11543b404ebf8448b [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
40 for (; !Ctx->isFileContext(); Ctx = Ctx->getParent()) {
41 // 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());
49 continue;
50 }
51
52 // Add template arguments from a function template specialization.
53 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Ctx)) {
54 // FIXME: Check whether this is an explicit specialization.
55 if (const TemplateArgumentList *TemplateArgs
56 = Function->getTemplateSpecializationArgs())
57 Result.addOuterTemplateArguments(TemplateArgs);
58
59 continue;
60 }
Douglas Gregor5f62c5e2009-05-14 23:26:13 +000061 }
Douglas Gregor5b8beb32009-08-28 17:37:35 +000062
63 return Result;
Douglas Gregor5f62c5e2009-05-14 23:26:13 +000064}
65
Douglas Gregor375733c2009-03-10 00:06:19 +000066Sema::InstantiatingTemplate::
67InstantiatingTemplate(Sema &SemaRef, SourceLocation PointOfInstantiation,
Douglas Gregorb12249d2009-05-18 17:01:57 +000068 Decl *Entity,
Douglas Gregor375733c2009-03-10 00:06:19 +000069 SourceRange InstantiationRange)
70 : SemaRef(SemaRef) {
Douglas Gregor56d25a72009-03-10 20:44:00 +000071
72 Invalid = CheckInstantiationDepth(PointOfInstantiation,
73 InstantiationRange);
74 if (!Invalid) {
Douglas Gregor375733c2009-03-10 00:06:19 +000075 ActiveTemplateInstantiation Inst;
Douglas Gregor56d25a72009-03-10 20:44:00 +000076 Inst.Kind = ActiveTemplateInstantiation::TemplateInstantiation;
Douglas Gregor375733c2009-03-10 00:06:19 +000077 Inst.PointOfInstantiation = PointOfInstantiation;
Douglas Gregor56d25a72009-03-10 20:44:00 +000078 Inst.Entity = reinterpret_cast<uintptr_t>(Entity);
Douglas Gregor95ba1282009-03-12 18:36:18 +000079 Inst.TemplateArgs = 0;
80 Inst.NumTemplateArgs = 0;
Douglas Gregor56d25a72009-03-10 20:44:00 +000081 Inst.InstantiationRange = InstantiationRange;
82 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
83 Invalid = false;
84 }
85}
86
87Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef,
88 SourceLocation PointOfInstantiation,
89 TemplateDecl *Template,
90 const TemplateArgument *TemplateArgs,
91 unsigned NumTemplateArgs,
92 SourceRange InstantiationRange)
93 : SemaRef(SemaRef) {
94
95 Invalid = CheckInstantiationDepth(PointOfInstantiation,
96 InstantiationRange);
97 if (!Invalid) {
98 ActiveTemplateInstantiation Inst;
99 Inst.Kind
100 = ActiveTemplateInstantiation::DefaultTemplateArgumentInstantiation;
101 Inst.PointOfInstantiation = PointOfInstantiation;
102 Inst.Entity = reinterpret_cast<uintptr_t>(Template);
103 Inst.TemplateArgs = TemplateArgs;
104 Inst.NumTemplateArgs = NumTemplateArgs;
Douglas Gregor375733c2009-03-10 00:06:19 +0000105 Inst.InstantiationRange = InstantiationRange;
106 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
107 Invalid = false;
108 }
109}
110
Douglas Gregorc5e01af2009-06-10 23:47:09 +0000111Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef,
112 SourceLocation PointOfInstantiation,
Douglas Gregor4c8b2b32009-07-01 22:01:06 +0000113 FunctionTemplateDecl *FunctionTemplate,
114 const TemplateArgument *TemplateArgs,
115 unsigned NumTemplateArgs,
116 ActiveTemplateInstantiation::InstantiationKind Kind,
117 SourceRange InstantiationRange)
118: SemaRef(SemaRef) {
119
120 Invalid = CheckInstantiationDepth(PointOfInstantiation,
121 InstantiationRange);
122 if (!Invalid) {
123 ActiveTemplateInstantiation Inst;
124 Inst.Kind = Kind;
125 Inst.PointOfInstantiation = PointOfInstantiation;
126 Inst.Entity = reinterpret_cast<uintptr_t>(FunctionTemplate);
127 Inst.TemplateArgs = TemplateArgs;
128 Inst.NumTemplateArgs = NumTemplateArgs;
129 Inst.InstantiationRange = InstantiationRange;
130 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
131 Invalid = false;
132 }
133}
134
135Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef,
136 SourceLocation PointOfInstantiation,
Douglas Gregorc5e01af2009-06-10 23:47:09 +0000137 ClassTemplatePartialSpecializationDecl *PartialSpec,
138 const TemplateArgument *TemplateArgs,
139 unsigned NumTemplateArgs,
140 SourceRange InstantiationRange)
141 : SemaRef(SemaRef) {
142
143 Invalid = CheckInstantiationDepth(PointOfInstantiation,
144 InstantiationRange);
145 if (!Invalid) {
146 ActiveTemplateInstantiation Inst;
147 Inst.Kind
Douglas Gregor4c8b2b32009-07-01 22:01:06 +0000148 = ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution;
Douglas Gregorc5e01af2009-06-10 23:47:09 +0000149 Inst.PointOfInstantiation = PointOfInstantiation;
150 Inst.Entity = reinterpret_cast<uintptr_t>(PartialSpec);
151 Inst.TemplateArgs = TemplateArgs;
152 Inst.NumTemplateArgs = NumTemplateArgs;
153 Inst.InstantiationRange = InstantiationRange;
154 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
155 Invalid = false;
156 }
157}
158
Douglas Gregorb12249d2009-05-18 17:01:57 +0000159void Sema::InstantiatingTemplate::Clear() {
160 if (!Invalid) {
Douglas Gregor375733c2009-03-10 00:06:19 +0000161 SemaRef.ActiveTemplateInstantiations.pop_back();
Douglas Gregorb12249d2009-05-18 17:01:57 +0000162 Invalid = true;
163 }
Douglas Gregor375733c2009-03-10 00:06:19 +0000164}
165
Douglas Gregor56d25a72009-03-10 20:44:00 +0000166bool Sema::InstantiatingTemplate::CheckInstantiationDepth(
167 SourceLocation PointOfInstantiation,
168 SourceRange InstantiationRange) {
169 if (SemaRef.ActiveTemplateInstantiations.size()
170 <= SemaRef.getLangOptions().InstantiationDepth)
171 return false;
172
173 SemaRef.Diag(PointOfInstantiation,
174 diag::err_template_recursion_depth_exceeded)
175 << SemaRef.getLangOptions().InstantiationDepth
176 << InstantiationRange;
177 SemaRef.Diag(PointOfInstantiation, diag::note_template_recursion_depth)
178 << SemaRef.getLangOptions().InstantiationDepth;
179 return true;
180}
181
Douglas Gregorfee85d62009-03-10 18:03:33 +0000182/// \brief Prints the current instantiation stack through a series of
183/// notes.
184void Sema::PrintInstantiationStack() {
Douglas Gregor4c8b2b32009-07-01 22:01:06 +0000185 // FIXME: In all of these cases, we need to show the template arguments
Douglas Gregorfee85d62009-03-10 18:03:33 +0000186 for (llvm::SmallVector<ActiveTemplateInstantiation, 16>::reverse_iterator
187 Active = ActiveTemplateInstantiations.rbegin(),
188 ActiveEnd = ActiveTemplateInstantiations.rend();
189 Active != ActiveEnd;
190 ++Active) {
Douglas Gregor56d25a72009-03-10 20:44:00 +0000191 switch (Active->Kind) {
192 case ActiveTemplateInstantiation::TemplateInstantiation: {
Douglas Gregorb12249d2009-05-18 17:01:57 +0000193 Decl *D = reinterpret_cast<Decl *>(Active->Entity);
194 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) {
195 unsigned DiagID = diag::note_template_member_class_here;
196 if (isa<ClassTemplateSpecializationDecl>(Record))
197 DiagID = diag::note_template_class_instantiation_here;
198 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
199 DiagID)
200 << Context.getTypeDeclType(Record)
201 << Active->InstantiationRange;
Douglas Gregor181fe792009-07-24 20:34:43 +0000202 } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
Douglas Gregor6f5e0542009-06-26 00:10:03 +0000203 unsigned DiagID;
204 if (Function->getPrimaryTemplate())
205 DiagID = diag::note_function_template_spec_here;
206 else
207 DiagID = diag::note_template_member_function_here;
Douglas Gregorb12249d2009-05-18 17:01:57 +0000208 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
209 DiagID)
210 << Function
211 << Active->InstantiationRange;
Douglas Gregor181fe792009-07-24 20:34:43 +0000212 } else {
213 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
214 diag::note_template_static_data_member_def_here)
215 << cast<VarDecl>(D)
216 << Active->InstantiationRange;
Douglas Gregorb12249d2009-05-18 17:01:57 +0000217 }
Douglas Gregor56d25a72009-03-10 20:44:00 +0000218 break;
219 }
220
221 case ActiveTemplateInstantiation::DefaultTemplateArgumentInstantiation: {
222 TemplateDecl *Template = cast<TemplateDecl>((Decl *)Active->Entity);
223 std::string TemplateArgsStr
Douglas Gregordd13e842009-03-30 22:58:21 +0000224 = TemplateSpecializationType::PrintTemplateArgumentList(
Douglas Gregor4c8b2b32009-07-01 22:01:06 +0000225 Active->TemplateArgs,
Douglas Gregor3bf3bbc2009-05-29 20:38:28 +0000226 Active->NumTemplateArgs,
227 Context.PrintingPolicy);
Douglas Gregor56d25a72009-03-10 20:44:00 +0000228 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
229 diag::note_default_arg_instantiation_here)
230 << (Template->getNameAsString() + TemplateArgsStr)
231 << Active->InstantiationRange;
232 break;
233 }
Douglas Gregorc5e01af2009-06-10 23:47:09 +0000234
Douglas Gregor4c8b2b32009-07-01 22:01:06 +0000235 case ActiveTemplateInstantiation::ExplicitTemplateArgumentSubstitution: {
236 FunctionTemplateDecl *FnTmpl
237 = cast<FunctionTemplateDecl>((Decl *)Active->Entity);
Douglas Gregorc5e01af2009-06-10 23:47:09 +0000238 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
Douglas Gregor4c8b2b32009-07-01 22:01:06 +0000239 diag::note_explicit_template_arg_substitution_here)
240 << FnTmpl << Active->InstantiationRange;
Douglas Gregorc5e01af2009-06-10 23:47:09 +0000241 break;
242 }
Douglas Gregor4c8b2b32009-07-01 22:01:06 +0000243
244 case ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution:
245 if (ClassTemplatePartialSpecializationDecl *PartialSpec
246 = dyn_cast<ClassTemplatePartialSpecializationDecl>(
247 (Decl *)Active->Entity)) {
248 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
249 diag::note_partial_spec_deduct_instantiation_here)
250 << Context.getTypeDeclType(PartialSpec)
251 << Active->InstantiationRange;
252 } else {
253 FunctionTemplateDecl *FnTmpl
254 = cast<FunctionTemplateDecl>((Decl *)Active->Entity);
255 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
256 diag::note_function_template_deduction_instantiation_here)
257 << FnTmpl << Active->InstantiationRange;
258 }
259 break;
Douglas Gregorc5e01af2009-06-10 23:47:09 +0000260
Douglas Gregor56d25a72009-03-10 20:44:00 +0000261 }
Douglas Gregorfee85d62009-03-10 18:03:33 +0000262 }
263}
264
Douglas Gregor95d6c952009-06-14 07:33:30 +0000265bool Sema::isSFINAEContext() const {
266 using llvm::SmallVector;
267 for (SmallVector<ActiveTemplateInstantiation, 16>::const_reverse_iterator
268 Active = ActiveTemplateInstantiations.rbegin(),
269 ActiveEnd = ActiveTemplateInstantiations.rend();
270 Active != ActiveEnd;
271 ++Active) {
272
273 switch(Active->Kind) {
Douglas Gregor4c8b2b32009-07-01 22:01:06 +0000274 case ActiveTemplateInstantiation::TemplateInstantiation:
275 // This is a template instantiation, so there is no SFINAE.
276 return false;
277
Douglas Gregor95d6c952009-06-14 07:33:30 +0000278 case ActiveTemplateInstantiation::DefaultTemplateArgumentInstantiation:
279 // A default template argument instantiation may or may not be a
280 // SFINAE context; look further up the stack.
281 break;
Douglas Gregor4c8b2b32009-07-01 22:01:06 +0000282
283 case ActiveTemplateInstantiation::ExplicitTemplateArgumentSubstitution:
284 case ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution:
285 // We're either substitution explicitly-specified template arguments
286 // or deduced template arguments, so SFINAE applies.
287 return true;
Douglas Gregor95d6c952009-06-14 07:33:30 +0000288 }
289 }
290
291 return false;
292}
293
Douglas Gregor74296542009-02-27 19:31:52 +0000294//===----------------------------------------------------------------------===/
295// Template Instantiation for Types
296//===----------------------------------------------------------------------===/
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000297namespace {
Douglas Gregor841324a2009-08-04 16:50:30 +0000298 class VISIBILITY_HIDDEN TemplateInstantiator
299 : public TreeTransform<TemplateInstantiator>
300 {
Douglas Gregor8dbd0382009-08-28 20:31:08 +0000301 const MultiLevelTemplateArgumentList &TemplateArgs;
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000302 SourceLocation Loc;
303 DeclarationName Entity;
Douglas Gregor74296542009-02-27 19:31:52 +0000304
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000305 public:
Douglas Gregor23a44be2009-08-20 07:17:43 +0000306 typedef TreeTransform<TemplateInstantiator> inherited;
307
Douglas Gregor841324a2009-08-04 16:50:30 +0000308 TemplateInstantiator(Sema &SemaRef,
Douglas Gregor8dbd0382009-08-28 20:31:08 +0000309 const MultiLevelTemplateArgumentList &TemplateArgs,
Douglas Gregor841324a2009-08-04 16:50:30 +0000310 SourceLocation Loc,
311 DeclarationName Entity)
Douglas Gregor23a44be2009-08-20 07:17:43 +0000312 : inherited(SemaRef), TemplateArgs(TemplateArgs), Loc(Loc),
313 Entity(Entity) { }
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000314
Douglas Gregor841324a2009-08-04 16:50:30 +0000315 /// \brief Determine whether the given type \p T has already been
316 /// transformed.
317 ///
318 /// For the purposes of template instantiation, a type has already been
319 /// transformed if it is NULL or if it is not dependent.
320 bool AlreadyTransformed(QualType T) {
321 return T.isNull() || !T->isDependentType();
Douglas Gregor90177912009-05-13 18:28:20 +0000322 }
Douglas Gregor841324a2009-08-04 16:50:30 +0000323
324 /// \brief Returns the location of the entity being instantiated, if known.
325 SourceLocation getBaseLocation() { return Loc; }
326
327 /// \brief Returns the name of the entity being instantiated, if any.
328 DeclarationName getBaseEntity() { return Entity; }
329
Douglas Gregor841324a2009-08-04 16:50:30 +0000330 /// \brief Transform the given declaration by instantiating a reference to
331 /// this declaration.
332 Decl *TransformDecl(Decl *D);
Douglas Gregor9d879762009-08-11 05:31:07 +0000333
Douglas Gregor23a44be2009-08-20 07:17:43 +0000334 /// \brief Transform the definition of the given declaration by
335 /// instantiating it.
336 Decl *TransformDefinition(Decl *D);
337
338 /// \brief Rebuild the exception declaration and register the declaration
339 /// as an instantiated local.
340 VarDecl *RebuildExceptionDecl(VarDecl *ExceptionDecl, QualType T,
341 DeclaratorInfo *Declarator,
342 IdentifierInfo *Name,
343 SourceLocation Loc, SourceRange TypeRange);
Douglas Gregor9d879762009-08-11 05:31:07 +0000344
345 Sema::OwningExprResult TransformDeclRefExpr(DeclRefExpr *E);
346
Douglas Gregor841324a2009-08-04 16:50:30 +0000347 /// \brief Transforms a template type parameter type by performing
348 /// substitution of the corresponding template type argument.
349 QualType TransformTemplateTypeParmType(const TemplateTypeParmType *T);
350 };
Douglas Gregor1d381132009-07-06 15:59:29 +0000351}
352
Douglas Gregor841324a2009-08-04 16:50:30 +0000353Decl *TemplateInstantiator::TransformDecl(Decl *D) {
Douglas Gregor214d0462009-08-06 06:41:21 +0000354 if (TemplateTemplateParmDecl *TTP
355 = dyn_cast_or_null<TemplateTemplateParmDecl>(D)) {
Douglas Gregor8dbd0382009-08-28 20:31:08 +0000356 if (TTP->getDepth() < TemplateArgs.getNumLevels()) {
357 assert(TemplateArgs(TTP->getDepth(), TTP->getPosition()).getAsDecl() &&
358 "Wrong kind of template template argument");
359 return cast<TemplateDecl>(TemplateArgs(TTP->getDepth(),
360 TTP->getPosition()).getAsDecl());
361 }
362
363 // If the corresponding template argument is NULL or non-existent, it's
364 // because we are performing instantiation from explicitly-specified
365 // template arguments in a function template, but there were some
366 // arguments left unspecified.
367 if (!TemplateArgs.hasTemplateArgument(TTP->getDepth(),
368 TTP->getPosition()))
369 return D;
370
371 // FIXME: Implement depth reduction of template template parameters
372 assert(false &&
373 "Reducing depth of template template parameters is not yet implemented");
Douglas Gregor214d0462009-08-06 06:41:21 +0000374 }
375
John McCall0ba26ee2009-08-25 22:02:44 +0000376 return SemaRef.FindInstantiatedDecl(cast_or_null<NamedDecl>(D));
Douglas Gregor841324a2009-08-04 16:50:30 +0000377}
378
Douglas Gregor23a44be2009-08-20 07:17:43 +0000379Decl *TemplateInstantiator::TransformDefinition(Decl *D) {
John McCall0ba26ee2009-08-25 22:02:44 +0000380 Decl *Inst = getSema().SubstDecl(D, getSema().CurContext, TemplateArgs);
Douglas Gregor23a44be2009-08-20 07:17:43 +0000381 if (!Inst)
382 return 0;
383
384 getSema().CurrentInstantiationScope->InstantiatedLocal(D, Inst);
385 return Inst;
386}
387
388VarDecl *
389TemplateInstantiator::RebuildExceptionDecl(VarDecl *ExceptionDecl,
390 QualType T,
391 DeclaratorInfo *Declarator,
392 IdentifierInfo *Name,
393 SourceLocation Loc,
394 SourceRange TypeRange) {
395 VarDecl *Var = inherited::RebuildExceptionDecl(ExceptionDecl, T, Declarator,
396 Name, Loc, TypeRange);
397 if (Var && !Var->isInvalidDecl())
398 getSema().CurrentInstantiationScope->InstantiatedLocal(ExceptionDecl, Var);
399 return Var;
400}
401
Douglas Gregor9d879762009-08-11 05:31:07 +0000402Sema::OwningExprResult
403TemplateInstantiator::TransformDeclRefExpr(DeclRefExpr *E) {
404 // FIXME: Clean this up a bit
405 NamedDecl *D = E->getDecl();
406 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(D)) {
Douglas Gregor8dbd0382009-08-28 20:31:08 +0000407 if (NTTP->getDepth() >= TemplateArgs.getNumLevels()) {
408 assert(false && "Cannot reduce non-type template parameter depth yet");
409 return getSema().ExprError();
410 }
Douglas Gregor9d879762009-08-11 05:31:07 +0000411
412 // If the corresponding template argument is NULL or non-existent, it's
413 // because we are performing instantiation from explicitly-specified
414 // template arguments in a function template, but there were some
415 // arguments left unspecified.
Douglas Gregor8dbd0382009-08-28 20:31:08 +0000416 if (!TemplateArgs.hasTemplateArgument(NTTP->getDepth(),
417 NTTP->getPosition()))
418 return SemaRef.Owned(E->Retain());
Douglas Gregor9d879762009-08-11 05:31:07 +0000419
Douglas Gregor8dbd0382009-08-28 20:31:08 +0000420 const TemplateArgument &Arg = TemplateArgs(NTTP->getDepth(),
421 NTTP->getPosition());
Douglas Gregor9d879762009-08-11 05:31:07 +0000422
423 // The template argument itself might be an expression, in which
424 // case we just return that expression.
425 if (Arg.getKind() == TemplateArgument::Expression)
Douglas Gregor8dbd0382009-08-28 20:31:08 +0000426 return SemaRef.Owned(Arg.getAsExpr()->Retain());
Douglas Gregor9d879762009-08-11 05:31:07 +0000427
428 if (Arg.getKind() == TemplateArgument::Declaration) {
429 ValueDecl *VD = cast<ValueDecl>(Arg.getAsDecl());
430
Douglas Gregor8dbd0382009-08-28 20:31:08 +0000431 VD = cast_or_null<ValueDecl>(getSema().FindInstantiatedDecl(VD));
432 if (!VD)
433 return SemaRef.ExprError();
434
Douglas Gregor9d879762009-08-11 05:31:07 +0000435 return SemaRef.BuildDeclRefExpr(VD, VD->getType(), E->getLocation(),
Douglas Gregor8dbd0382009-08-28 20:31:08 +0000436 /*FIXME:*/false, /*FIXME:*/false);
Douglas Gregor9d879762009-08-11 05:31:07 +0000437 }
438
439 assert(Arg.getKind() == TemplateArgument::Integral);
440 QualType T = Arg.getIntegralType();
441 if (T->isCharType() || T->isWideCharType())
442 return SemaRef.Owned(new (SemaRef.Context) CharacterLiteral(
Douglas Gregor8dbd0382009-08-28 20:31:08 +0000443 Arg.getAsIntegral()->getZExtValue(),
444 T->isWideCharType(),
445 T,
446 E->getSourceRange().getBegin()));
Douglas Gregor9d879762009-08-11 05:31:07 +0000447 if (T->isBooleanType())
448 return SemaRef.Owned(new (SemaRef.Context) CXXBoolLiteralExpr(
Douglas Gregor8dbd0382009-08-28 20:31:08 +0000449 Arg.getAsIntegral()->getBoolValue(),
450 T,
451 E->getSourceRange().getBegin()));
Douglas Gregor9d879762009-08-11 05:31:07 +0000452
453 assert(Arg.getAsIntegral()->getBitWidth() == SemaRef.Context.getIntWidth(T));
454 return SemaRef.Owned(new (SemaRef.Context) IntegerLiteral(
Douglas Gregor8dbd0382009-08-28 20:31:08 +0000455 *Arg.getAsIntegral(),
456 T,
457 E->getSourceRange().getBegin()));
Douglas Gregor9d879762009-08-11 05:31:07 +0000458 }
459
460 if (OverloadedFunctionDecl *Ovl = dyn_cast<OverloadedFunctionDecl>(D)) {
461 // FIXME: instantiate each decl in the overload set
462 return SemaRef.Owned(new (SemaRef.Context) DeclRefExpr(Ovl,
Douglas Gregor8dbd0382009-08-28 20:31:08 +0000463 SemaRef.Context.OverloadTy,
464 E->getLocation(),
465 false, false));
Douglas Gregor9d879762009-08-11 05:31:07 +0000466 }
467
John McCall0ba26ee2009-08-25 22:02:44 +0000468 NamedDecl *InstD = SemaRef.FindInstantiatedDecl(D);
Douglas Gregor9d879762009-08-11 05:31:07 +0000469 if (!InstD)
470 return SemaRef.ExprError();
471
472 // FIXME: nested-name-specifier for QualifiedDeclRefExpr
473 return SemaRef.BuildDeclarationNameExpr(E->getLocation(), InstD,
474 /*FIXME:*/false,
475 /*FIXME:*/0,
476 /*FIXME:*/false);
477}
478
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000479QualType
Douglas Gregor841324a2009-08-04 16:50:30 +0000480TemplateInstantiator::TransformTemplateTypeParmType(
481 const TemplateTypeParmType *T) {
Douglas Gregor8dbd0382009-08-28 20:31:08 +0000482 if (T->getDepth() < TemplateArgs.getNumLevels()) {
Douglas Gregor74296542009-02-27 19:31:52 +0000483 // Replace the template type parameter with its corresponding
484 // template argument.
Douglas Gregorecd63b82009-07-01 00:28:38 +0000485
486 // If the corresponding template argument is NULL or doesn't exist, it's
487 // because we are performing instantiation from explicitly-specified
488 // template arguments in a function template class, but there were some
489 // arguments left unspecified.
Douglas Gregor8dbd0382009-08-28 20:31:08 +0000490 if (!TemplateArgs.hasTemplateArgument(T->getDepth(), T->getIndex()))
491 return QualType(T, 0);
492
493 assert(TemplateArgs(T->getDepth(), T->getIndex()).getKind()
494 == TemplateArgument::Type &&
Douglas Gregor74296542009-02-27 19:31:52 +0000495 "Template argument kind mismatch");
Douglas Gregor8dbd0382009-08-28 20:31:08 +0000496
497 return TemplateArgs(T->getDepth(), T->getIndex()).getAsType();
Douglas Gregor74296542009-02-27 19:31:52 +0000498 }
499
500 // The template type parameter comes from an inner template (e.g.,
501 // the template parameter list of a member template inside the
502 // template we are instantiating). Create a new template type
503 // parameter with the template "level" reduced by one.
Douglas Gregor8dbd0382009-08-28 20:31:08 +0000504 return getSema().Context.getTemplateTypeParmType(
505 T->getDepth() - TemplateArgs.getNumLevels(),
Douglas Gregor841324a2009-08-04 16:50:30 +0000506 T->getIndex(),
507 T->isParameterPack(),
508 T->getName());
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000509}
Douglas Gregor74296542009-02-27 19:31:52 +0000510
John McCall0ba26ee2009-08-25 22:02:44 +0000511/// \brief Perform substitution on the type T with a given set of template
512/// arguments.
Douglas Gregor74296542009-02-27 19:31:52 +0000513///
514/// This routine substitutes the given template arguments into the
515/// type T and produces the instantiated type.
516///
517/// \param T the type into which the template arguments will be
518/// substituted. If this type is not dependent, it will be returned
519/// immediately.
520///
521/// \param TemplateArgs the template arguments that will be
522/// substituted for the top-level template parameters within T.
523///
Douglas Gregor74296542009-02-27 19:31:52 +0000524/// \param Loc the location in the source code where this substitution
525/// is being performed. It will typically be the location of the
526/// declarator (if we're instantiating the type of some declaration)
527/// or the location of the type in the source code (if, e.g., we're
528/// instantiating the type of a cast expression).
529///
530/// \param Entity the name of the entity associated with a declaration
531/// being instantiated (if any). May be empty to indicate that there
532/// is no such entity (if, e.g., this is a type that occurs as part of
533/// a cast expression) or that the entity has no name (e.g., an
534/// unnamed function parameter).
535///
536/// \returns If the instantiation succeeds, the instantiated
537/// type. Otherwise, produces diagnostics and returns a NULL type.
John McCall0ba26ee2009-08-25 22:02:44 +0000538QualType Sema::SubstType(QualType T,
Douglas Gregor8dbd0382009-08-28 20:31:08 +0000539 const MultiLevelTemplateArgumentList &TemplateArgs,
John McCall0ba26ee2009-08-25 22:02:44 +0000540 SourceLocation Loc, DeclarationName Entity) {
Douglas Gregor56d25a72009-03-10 20:44:00 +0000541 assert(!ActiveTemplateInstantiations.empty() &&
542 "Cannot perform an instantiation without some context on the "
543 "instantiation stack");
544
Douglas Gregor74296542009-02-27 19:31:52 +0000545 // If T is not a dependent type, there is nothing to do.
546 if (!T->isDependentType())
547 return T;
548
Douglas Gregor841324a2009-08-04 16:50:30 +0000549 TemplateInstantiator Instantiator(*this, TemplateArgs, Loc, Entity);
550 return Instantiator.TransformType(T);
Douglas Gregor74296542009-02-27 19:31:52 +0000551}
Douglas Gregored3a3982009-03-03 04:44:36 +0000552
John McCall0ba26ee2009-08-25 22:02:44 +0000553/// \brief Perform substitution on the base class specifiers of the
554/// given class template specialization.
Douglas Gregored3a3982009-03-03 04:44:36 +0000555///
556/// Produces a diagnostic and returns true on error, returns false and
557/// attaches the instantiated base classes to the class template
558/// specialization if successful.
559bool
John McCall0ba26ee2009-08-25 22:02:44 +0000560Sema::SubstBaseSpecifiers(CXXRecordDecl *Instantiation,
561 CXXRecordDecl *Pattern,
Douglas Gregor8dbd0382009-08-28 20:31:08 +0000562 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregored3a3982009-03-03 04:44:36 +0000563 bool Invalid = false;
Douglas Gregor4ac20ef2009-05-29 18:27:38 +0000564 llvm::SmallVector<CXXBaseSpecifier*, 4> InstantiatedBases;
Douglas Gregorcc887972009-03-25 21:17:03 +0000565 for (ClassTemplateSpecializationDecl::base_class_iterator
566 Base = Pattern->bases_begin(), BaseEnd = Pattern->bases_end();
Douglas Gregord9572a12009-03-10 18:52:44 +0000567 Base != BaseEnd; ++Base) {
Douglas Gregored3a3982009-03-03 04:44:36 +0000568 if (!Base->getType()->isDependentType()) {
Fariborz Jahanian1373b6f2009-07-22 17:41:53 +0000569 InstantiatedBases.push_back(new (Context) CXXBaseSpecifier(*Base));
Douglas Gregored3a3982009-03-03 04:44:36 +0000570 continue;
571 }
572
John McCall0ba26ee2009-08-25 22:02:44 +0000573 QualType BaseType = SubstType(Base->getType(),
574 TemplateArgs,
575 Base->getSourceRange().getBegin(),
576 DeclarationName());
Douglas Gregored3a3982009-03-03 04:44:36 +0000577 if (BaseType.isNull()) {
578 Invalid = true;
579 continue;
580 }
581
582 if (CXXBaseSpecifier *InstantiatedBase
Douglas Gregorcc887972009-03-25 21:17:03 +0000583 = CheckBaseSpecifier(Instantiation,
Douglas Gregored3a3982009-03-03 04:44:36 +0000584 Base->getSourceRange(),
585 Base->isVirtual(),
586 Base->getAccessSpecifierAsWritten(),
587 BaseType,
588 /*FIXME: Not totally accurate */
589 Base->getSourceRange().getBegin()))
590 InstantiatedBases.push_back(InstantiatedBase);
591 else
592 Invalid = true;
593 }
594
Douglas Gregord9572a12009-03-10 18:52:44 +0000595 if (!Invalid &&
Jay Foad9e6bef42009-05-21 09:52:38 +0000596 AttachBaseSpecifiers(Instantiation, InstantiatedBases.data(),
Douglas Gregored3a3982009-03-03 04:44:36 +0000597 InstantiatedBases.size()))
598 Invalid = true;
599
600 return Invalid;
601}
602
Douglas Gregorcc887972009-03-25 21:17:03 +0000603/// \brief Instantiate the definition of a class from a given pattern.
604///
605/// \param PointOfInstantiation The point of instantiation within the
606/// source code.
607///
608/// \param Instantiation is the declaration whose definition is being
609/// instantiated. This will be either a class template specialization
610/// or a member class of a class template specialization.
611///
612/// \param Pattern is the pattern from which the instantiation
613/// occurs. This will be either the declaration of a class template or
614/// the declaration of a member class of a class template.
615///
616/// \param TemplateArgs The template arguments to be substituted into
617/// the pattern.
618///
Douglas Gregorb35c7992009-08-24 15:23:48 +0000619/// \param ExplicitInstantiation whether this is an explicit instantiation
620/// (otherwise, it is an implicit instantiation).
621///
622/// \param Complain whether to complain if the class cannot be instantiated due
623/// to the lack of a definition.
624///
Douglas Gregorcc887972009-03-25 21:17:03 +0000625/// \returns true if an error occurred, false otherwise.
626bool
627Sema::InstantiateClass(SourceLocation PointOfInstantiation,
628 CXXRecordDecl *Instantiation, CXXRecordDecl *Pattern,
Douglas Gregor8dbd0382009-08-28 20:31:08 +0000629 const MultiLevelTemplateArgumentList &TemplateArgs,
Douglas Gregorb35c7992009-08-24 15:23:48 +0000630 bool ExplicitInstantiation,
631 bool Complain) {
Douglas Gregorcc887972009-03-25 21:17:03 +0000632 bool Invalid = false;
John McCalld64b41e2009-08-20 01:44:21 +0000633
Douglas Gregorcc887972009-03-25 21:17:03 +0000634 CXXRecordDecl *PatternDef
635 = cast_or_null<CXXRecordDecl>(Pattern->getDefinition(Context));
636 if (!PatternDef) {
Douglas Gregorb35c7992009-08-24 15:23:48 +0000637 if (!Complain) {
638 // Say nothing
639 } else if (Pattern == Instantiation->getInstantiatedFromMemberClass()) {
Douglas Gregorcc887972009-03-25 21:17:03 +0000640 Diag(PointOfInstantiation,
641 diag::err_implicit_instantiate_member_undefined)
642 << Context.getTypeDeclType(Instantiation);
643 Diag(Pattern->getLocation(), diag::note_member_of_template_here);
644 } else {
Douglas Gregorfd79ac62009-05-13 00:25:59 +0000645 Diag(PointOfInstantiation, diag::err_template_instantiate_undefined)
646 << ExplicitInstantiation
Douglas Gregorcc887972009-03-25 21:17:03 +0000647 << Context.getTypeDeclType(Instantiation);
648 Diag(Pattern->getLocation(), diag::note_template_decl_here);
649 }
650 return true;
651 }
652 Pattern = PatternDef;
653
Douglas Gregor42c48522009-03-25 21:23:52 +0000654 InstantiatingTemplate Inst(*this, PointOfInstantiation, Instantiation);
Douglas Gregorcc887972009-03-25 21:17:03 +0000655 if (Inst)
656 return true;
657
658 // Enter the scope of this instantiation. We don't use
659 // PushDeclContext because we don't have a scope.
660 DeclContext *PreviousContext = CurContext;
661 CurContext = Instantiation;
662
663 // Start the definition of this instantiation.
664 Instantiation->startDefinition();
665
John McCall0ba26ee2009-08-25 22:02:44 +0000666 // Do substitution on the base class specifiers.
667 if (SubstBaseSpecifiers(Instantiation, Pattern, TemplateArgs))
Douglas Gregorcc887972009-03-25 21:17:03 +0000668 Invalid = true;
669
Douglas Gregor4ac20ef2009-05-29 18:27:38 +0000670 llvm::SmallVector<DeclPtrTy, 4> Fields;
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000671 for (RecordDecl::decl_iterator Member = Pattern->decls_begin(),
672 MemberEnd = Pattern->decls_end();
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000673 Member != MemberEnd; ++Member) {
John McCall0ba26ee2009-08-25 22:02:44 +0000674 Decl *NewMember = SubstDecl(*Member, Instantiation, TemplateArgs);
Douglas Gregorcc887972009-03-25 21:17:03 +0000675 if (NewMember) {
676 if (NewMember->isInvalidDecl())
677 Invalid = true;
678 else if (FieldDecl *Field = dyn_cast<FieldDecl>(NewMember))
Chris Lattner5261d0c2009-03-28 19:18:32 +0000679 Fields.push_back(DeclPtrTy::make(Field));
Douglas Gregorcc887972009-03-25 21:17:03 +0000680 } else {
681 // FIXME: Eventually, a NULL return will mean that one of the
Mike Stumpe127ae32009-05-16 07:39:55 +0000682 // instantiations was a semantic disaster, and we'll want to set Invalid =
683 // true. For now, we expect to skip some members that we can't yet handle.
Douglas Gregorcc887972009-03-25 21:17:03 +0000684 }
685 }
686
687 // Finish checking fields.
Chris Lattner5261d0c2009-03-28 19:18:32 +0000688 ActOnFields(0, Instantiation->getLocation(), DeclPtrTy::make(Instantiation),
Jay Foad9e6bef42009-05-21 09:52:38 +0000689 Fields.data(), Fields.size(), SourceLocation(), SourceLocation(),
Douglas Gregorcc887972009-03-25 21:17:03 +0000690 0);
691
692 // Add any implicitly-declared members that we might need.
693 AddImplicitlyDeclaredMembersToClass(Instantiation);
694
695 // Exit the scope of this instantiation.
696 CurContext = PreviousContext;
697
Douglas Gregordc18e892009-05-26 20:50:29 +0000698 if (!Invalid)
699 Consumer.HandleTagDeclDefinition(Instantiation);
700
Douglas Gregordf9f5d12009-05-13 20:28:22 +0000701 // If this is an explicit instantiation, instantiate our members, too.
Douglas Gregorb12249d2009-05-18 17:01:57 +0000702 if (!Invalid && ExplicitInstantiation) {
703 Inst.Clear();
Douglas Gregordf9f5d12009-05-13 20:28:22 +0000704 InstantiateClassMembers(PointOfInstantiation, Instantiation, TemplateArgs);
Douglas Gregorb12249d2009-05-18 17:01:57 +0000705 }
Douglas Gregordf9f5d12009-05-13 20:28:22 +0000706
Douglas Gregorcc887972009-03-25 21:17:03 +0000707 return Invalid;
708}
709
Douglas Gregored3a3982009-03-03 04:44:36 +0000710bool
711Sema::InstantiateClassTemplateSpecialization(
712 ClassTemplateSpecializationDecl *ClassTemplateSpec,
Douglas Gregorb35c7992009-08-24 15:23:48 +0000713 bool ExplicitInstantiation,
714 bool Complain) {
Douglas Gregored3a3982009-03-03 04:44:36 +0000715 // Perform the actual instantiation on the canonical declaration.
716 ClassTemplateSpec = cast<ClassTemplateSpecializationDecl>(
Argiris Kirtzidis17c7cab2009-07-18 00:34:25 +0000717 ClassTemplateSpec->getCanonicalDecl());
Douglas Gregored3a3982009-03-03 04:44:36 +0000718
719 // We can only instantiate something that hasn't already been
720 // instantiated or specialized. Fail without any diagnostics: our
721 // caller will provide an error message.
722 if (ClassTemplateSpec->getSpecializationKind() != TSK_Undeclared)
723 return true;
724
Douglas Gregored3a3982009-03-03 04:44:36 +0000725 ClassTemplateDecl *Template = ClassTemplateSpec->getSpecializedTemplate();
Douglas Gregor8dbd0382009-08-28 20:31:08 +0000726 CXXRecordDecl *Pattern = 0;
Douglas Gregor58944ac2009-05-31 09:31:02 +0000727
Douglas Gregor21530c52009-06-12 22:31:52 +0000728 // C++ [temp.class.spec.match]p1:
729 // When a class template is used in a context that requires an
730 // instantiation of the class, it is necessary to determine
731 // whether the instantiation is to be generated using the primary
732 // template or one of the partial specializations. This is done by
733 // matching the template arguments of the class template
734 // specialization with the template argument lists of the partial
735 // specializations.
Douglas Gregorab9f71a2009-06-05 00:53:49 +0000736 typedef std::pair<ClassTemplatePartialSpecializationDecl *,
737 TemplateArgumentList *> MatchResult;
738 llvm::SmallVector<MatchResult, 4> Matched;
Douglas Gregor58944ac2009-05-31 09:31:02 +0000739 for (llvm::FoldingSet<ClassTemplatePartialSpecializationDecl>::iterator
740 Partial = Template->getPartialSpecializations().begin(),
741 PartialEnd = Template->getPartialSpecializations().end();
742 Partial != PartialEnd;
743 ++Partial) {
Douglas Gregor623e2e02009-06-12 18:26:56 +0000744 TemplateDeductionInfo Info(Context);
745 if (TemplateDeductionResult Result
Douglas Gregorab9f71a2009-06-05 00:53:49 +0000746 = DeduceTemplateArguments(&*Partial,
Douglas Gregor623e2e02009-06-12 18:26:56 +0000747 ClassTemplateSpec->getTemplateArgs(),
748 Info)) {
749 // FIXME: Store the failed-deduction information for use in
750 // diagnostics, later.
751 (void)Result;
752 } else {
753 Matched.push_back(std::make_pair(&*Partial, Info.take()));
754 }
Douglas Gregor58944ac2009-05-31 09:31:02 +0000755 }
756
757 if (Matched.size() == 1) {
Douglas Gregor21530c52009-06-12 22:31:52 +0000758 // -- If exactly one matching specialization is found, the
759 // instantiation is generated from that specialization.
Douglas Gregorab9f71a2009-06-05 00:53:49 +0000760 Pattern = Matched[0].first;
Douglas Gregor7b0b83f2009-08-02 23:24:31 +0000761 ClassTemplateSpec->setInstantiationOf(Matched[0].first, Matched[0].second);
Douglas Gregor58944ac2009-05-31 09:31:02 +0000762 } else if (Matched.size() > 1) {
Douglas Gregor21530c52009-06-12 22:31:52 +0000763 // -- If more than one matching specialization is found, the
764 // partial order rules (14.5.4.2) are used to determine
765 // whether one of the specializations is more specialized
766 // than the others. If none of the specializations is more
767 // specialized than all of the other matching
768 // specializations, then the use of the class template is
769 // ambiguous and the program is ill-formed.
Douglas Gregor58944ac2009-05-31 09:31:02 +0000770 // FIXME: Implement partial ordering of class template partial
771 // specializations.
772 Diag(ClassTemplateSpec->getLocation(),
773 diag::unsup_template_partial_spec_ordering);
Douglas Gregor8dbd0382009-08-28 20:31:08 +0000774
775 // FIXME: Temporary hack to fall back to the primary template
776 ClassTemplateDecl *OrigTemplate = Template;
777 while (OrigTemplate->getInstantiatedFromMemberTemplate())
778 OrigTemplate = OrigTemplate->getInstantiatedFromMemberTemplate();
779
780 Pattern = OrigTemplate->getTemplatedDecl();
Douglas Gregor21530c52009-06-12 22:31:52 +0000781 } else {
782 // -- If no matches are found, the instantiation is generated
783 // from the primary template.
Douglas Gregor8dbd0382009-08-28 20:31:08 +0000784 ClassTemplateDecl *OrigTemplate = Template;
785 while (OrigTemplate->getInstantiatedFromMemberTemplate())
786 OrigTemplate = OrigTemplate->getInstantiatedFromMemberTemplate();
787
788 Pattern = OrigTemplate->getTemplatedDecl();
Douglas Gregor58944ac2009-05-31 09:31:02 +0000789 }
Douglas Gregored3a3982009-03-03 04:44:36 +0000790
Douglas Gregor8dbd0382009-08-28 20:31:08 +0000791 // Note that this is an instantiation.
Douglas Gregored3a3982009-03-03 04:44:36 +0000792 ClassTemplateSpec->setSpecializationKind(
793 ExplicitInstantiation? TSK_ExplicitInstantiation
794 : TSK_ImplicitInstantiation);
795
Douglas Gregorab9f71a2009-06-05 00:53:49 +0000796 bool Result = InstantiateClass(ClassTemplateSpec->getLocation(),
Douglas Gregor8dbd0382009-08-28 20:31:08 +0000797 ClassTemplateSpec, Pattern,
798 getTemplateInstantiationArgs(ClassTemplateSpec),
Douglas Gregorb35c7992009-08-24 15:23:48 +0000799 ExplicitInstantiation,
800 Complain);
Douglas Gregorab9f71a2009-06-05 00:53:49 +0000801
802 for (unsigned I = 0, N = Matched.size(); I != N; ++I) {
803 // FIXME: Implement TemplateArgumentList::Destroy!
804 // if (Matched[I].first != Pattern)
805 // Matched[I].second->Destroy(Context);
806 }
807
808 return Result;
Douglas Gregored3a3982009-03-03 04:44:36 +0000809}
Douglas Gregor47bde7c2009-03-19 17:26:29 +0000810
John McCall0ba26ee2009-08-25 22:02:44 +0000811/// \brief Instantiates the definitions of all of the member
812/// of the given class, which is an instantiation of a class template
813/// or a member class of a template.
Douglas Gregordf9f5d12009-05-13 20:28:22 +0000814void
815Sema::InstantiateClassMembers(SourceLocation PointOfInstantiation,
John McCall0ba26ee2009-08-25 22:02:44 +0000816 CXXRecordDecl *Instantiation,
Douglas Gregor8dbd0382009-08-28 20:31:08 +0000817 const MultiLevelTemplateArgumentList &TemplateArgs) {
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000818 for (DeclContext::decl_iterator D = Instantiation->decls_begin(),
819 DEnd = Instantiation->decls_end();
Douglas Gregordf9f5d12009-05-13 20:28:22 +0000820 D != DEnd; ++D) {
821 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(*D)) {
Argiris Kirtzidisccb9efe2009-06-30 02:35:26 +0000822 if (!Function->getBody())
Douglas Gregorb12249d2009-05-18 17:01:57 +0000823 InstantiateFunctionDefinition(PointOfInstantiation, Function);
Douglas Gregordf9f5d12009-05-13 20:28:22 +0000824 } else if (VarDecl *Var = dyn_cast<VarDecl>(*D)) {
Douglas Gregor181fe792009-07-24 20:34:43 +0000825 if (Var->isStaticDataMember())
826 InstantiateStaticDataMemberDefinition(PointOfInstantiation, Var);
Douglas Gregordf9f5d12009-05-13 20:28:22 +0000827 } else if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(*D)) {
828 if (!Record->isInjectedClassName() && !Record->getDefinition(Context)) {
829 assert(Record->getInstantiatedFromMemberClass() &&
830 "Missing instantiated-from-template information");
Douglas Gregorb12249d2009-05-18 17:01:57 +0000831 InstantiateClass(PointOfInstantiation, Record,
Douglas Gregordf9f5d12009-05-13 20:28:22 +0000832 Record->getInstantiatedFromMemberClass(),
833 TemplateArgs, true);
834 }
835 }
836 }
837}
838
839/// \brief Instantiate the definitions of all of the members of the
840/// given class template specialization, which was named as part of an
841/// explicit instantiation.
842void Sema::InstantiateClassTemplateSpecializationMembers(
843 SourceLocation PointOfInstantiation,
844 ClassTemplateSpecializationDecl *ClassTemplateSpec) {
845 // C++0x [temp.explicit]p7:
846 // An explicit instantiation that names a class template
847 // specialization is an explicit instantion of the same kind
848 // (declaration or definition) of each of its members (not
849 // including members inherited from base classes) that has not
850 // been previously explicitly specialized in the translation unit
851 // containing the explicit instantiation, except as described
852 // below.
853 InstantiateClassMembers(PointOfInstantiation, ClassTemplateSpec,
Douglas Gregor8dbd0382009-08-28 20:31:08 +0000854 getTemplateInstantiationArgs(ClassTemplateSpec));
Douglas Gregordf9f5d12009-05-13 20:28:22 +0000855}
856
Douglas Gregor23a44be2009-08-20 07:17:43 +0000857Sema::OwningStmtResult
Douglas Gregor8dbd0382009-08-28 20:31:08 +0000858Sema::SubstStmt(Stmt *S, const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor23a44be2009-08-20 07:17:43 +0000859 if (!S)
860 return Owned(S);
861
862 TemplateInstantiator Instantiator(*this, TemplateArgs,
863 SourceLocation(),
864 DeclarationName());
865 return Instantiator.TransformStmt(S);
866}
867
Douglas Gregor9d879762009-08-11 05:31:07 +0000868Sema::OwningExprResult
Douglas Gregor8dbd0382009-08-28 20:31:08 +0000869Sema::SubstExpr(Expr *E, const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor9d879762009-08-11 05:31:07 +0000870 if (!E)
871 return Owned(E);
872
873 TemplateInstantiator Instantiator(*this, TemplateArgs,
874 SourceLocation(),
875 DeclarationName());
876 return Instantiator.TransformExpr(E);
877}
878
John McCall0ba26ee2009-08-25 22:02:44 +0000879/// \brief Do template substitution on a nested-name-specifier.
Douglas Gregor1e589cc2009-03-26 23:50:42 +0000880NestedNameSpecifier *
John McCall0ba26ee2009-08-25 22:02:44 +0000881Sema::SubstNestedNameSpecifier(NestedNameSpecifier *NNS,
Douglas Gregor8dbd0382009-08-28 20:31:08 +0000882 SourceRange Range,
883 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor12431cb2009-08-06 05:28:30 +0000884 TemplateInstantiator Instantiator(*this, TemplateArgs, Range.getBegin(),
885 DeclarationName());
886 return Instantiator.TransformNestedNameSpecifier(NNS, Range);
Douglas Gregor47bde7c2009-03-19 17:26:29 +0000887}
Douglas Gregor15a92852009-03-31 18:38:02 +0000888
889TemplateName
John McCall0ba26ee2009-08-25 22:02:44 +0000890Sema::SubstTemplateName(TemplateName Name, SourceLocation Loc,
Douglas Gregor8dbd0382009-08-28 20:31:08 +0000891 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor214d0462009-08-06 06:41:21 +0000892 TemplateInstantiator Instantiator(*this, TemplateArgs, Loc,
893 DeclarationName());
894 return Instantiator.TransformTemplateName(Name);
Douglas Gregor15a92852009-03-31 18:38:02 +0000895}
Douglas Gregorb320b9e2009-06-11 00:06:24 +0000896
John McCall0ba26ee2009-08-25 22:02:44 +0000897TemplateArgument Sema::Subst(TemplateArgument Arg,
Douglas Gregor8dbd0382009-08-28 20:31:08 +0000898 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor2999faa2009-08-04 22:27:00 +0000899 TemplateInstantiator Instantiator(*this, TemplateArgs, SourceLocation(),
900 DeclarationName());
901 return Instantiator.TransformTemplateArgument(Arg);
Douglas Gregorb320b9e2009-06-11 00:06:24 +0000902}