blob: b330ae960be79324ff27b39221e242b0a64e78e1 [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 Gregor5f62c5e2009-05-14 23:26:13 +000029/// \brief Retrieve the template argument list that should be used to
30/// instantiate 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 Gregorf9e7d3d2009-05-11 23:53:27 +0000301 const TemplateArgumentList &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,
309 const TemplateArgumentList &TemplateArgs,
310 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)) {
356 // FIXME: Depth reduction
357 assert(TTP->getDepth() == 0 &&
358 "Cannot reduce depth of a template template parameter");
359 assert(TemplateArgs[TTP->getPosition()].getAsDecl() &&
360 "Wrong kind of template template argument");
361 TemplateDecl *Template
362 = dyn_cast<TemplateDecl>(TemplateArgs[TTP->getPosition()].getAsDecl());
363 assert(Template && "Expected a template");
364 return Template;
365 }
366
John McCall0ba26ee2009-08-25 22:02:44 +0000367 return SemaRef.FindInstantiatedDecl(cast_or_null<NamedDecl>(D));
Douglas Gregor841324a2009-08-04 16:50:30 +0000368}
369
Douglas Gregor23a44be2009-08-20 07:17:43 +0000370Decl *TemplateInstantiator::TransformDefinition(Decl *D) {
John McCall0ba26ee2009-08-25 22:02:44 +0000371 Decl *Inst = getSema().SubstDecl(D, getSema().CurContext, TemplateArgs);
Douglas Gregor23a44be2009-08-20 07:17:43 +0000372 if (!Inst)
373 return 0;
374
375 getSema().CurrentInstantiationScope->InstantiatedLocal(D, Inst);
376 return Inst;
377}
378
379VarDecl *
380TemplateInstantiator::RebuildExceptionDecl(VarDecl *ExceptionDecl,
381 QualType T,
382 DeclaratorInfo *Declarator,
383 IdentifierInfo *Name,
384 SourceLocation Loc,
385 SourceRange TypeRange) {
386 VarDecl *Var = inherited::RebuildExceptionDecl(ExceptionDecl, T, Declarator,
387 Name, Loc, TypeRange);
388 if (Var && !Var->isInvalidDecl())
389 getSema().CurrentInstantiationScope->InstantiatedLocal(ExceptionDecl, Var);
390 return Var;
391}
392
Douglas Gregor9d879762009-08-11 05:31:07 +0000393Sema::OwningExprResult
394TemplateInstantiator::TransformDeclRefExpr(DeclRefExpr *E) {
395 // FIXME: Clean this up a bit
396 NamedDecl *D = E->getDecl();
397 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(D)) {
398 assert(NTTP->getDepth() == 0 && "No nested templates yet");
399
400 // If the corresponding template argument is NULL or non-existent, it's
401 // because we are performing instantiation from explicitly-specified
402 // template arguments in a function template, but there were some
403 // arguments left unspecified.
404 if (NTTP->getPosition() >= TemplateArgs.size() ||
405 TemplateArgs[NTTP->getPosition()].isNull())
406 return SemaRef.Owned(E); // FIXME: Clone the expression!
407
408 const TemplateArgument &Arg = TemplateArgs[NTTP->getPosition()];
409
410 // The template argument itself might be an expression, in which
411 // case we just return that expression.
412 if (Arg.getKind() == TemplateArgument::Expression)
413 // FIXME: Clone the expression!
414 return SemaRef.Owned(Arg.getAsExpr());
415
416 if (Arg.getKind() == TemplateArgument::Declaration) {
417 ValueDecl *VD = cast<ValueDecl>(Arg.getAsDecl());
418
419 // FIXME: Can VD ever have a dependent type?
420 return SemaRef.BuildDeclRefExpr(VD, VD->getType(), E->getLocation(),
421 false, false);
422 }
423
424 assert(Arg.getKind() == TemplateArgument::Integral);
425 QualType T = Arg.getIntegralType();
426 if (T->isCharType() || T->isWideCharType())
427 return SemaRef.Owned(new (SemaRef.Context) CharacterLiteral(
428 Arg.getAsIntegral()->getZExtValue(),
429 T->isWideCharType(),
430 T,
431 E->getSourceRange().getBegin()));
432 if (T->isBooleanType())
433 return SemaRef.Owned(new (SemaRef.Context) CXXBoolLiteralExpr(
434 Arg.getAsIntegral()->getBoolValue(),
435 T,
436 E->getSourceRange().getBegin()));
437
438 assert(Arg.getAsIntegral()->getBitWidth() == SemaRef.Context.getIntWidth(T));
439 return SemaRef.Owned(new (SemaRef.Context) IntegerLiteral(
440 *Arg.getAsIntegral(),
441 T,
442 E->getSourceRange().getBegin()));
443 }
444
445 if (OverloadedFunctionDecl *Ovl = dyn_cast<OverloadedFunctionDecl>(D)) {
446 // FIXME: instantiate each decl in the overload set
447 return SemaRef.Owned(new (SemaRef.Context) DeclRefExpr(Ovl,
448 SemaRef.Context.OverloadTy,
449 E->getLocation(),
450 false, false));
451 }
452
John McCall0ba26ee2009-08-25 22:02:44 +0000453 NamedDecl *InstD = SemaRef.FindInstantiatedDecl(D);
Douglas Gregor9d879762009-08-11 05:31:07 +0000454 if (!InstD)
455 return SemaRef.ExprError();
456
457 // FIXME: nested-name-specifier for QualifiedDeclRefExpr
458 return SemaRef.BuildDeclarationNameExpr(E->getLocation(), InstD,
459 /*FIXME:*/false,
460 /*FIXME:*/0,
461 /*FIXME:*/false);
462}
463
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000464QualType
Douglas Gregor841324a2009-08-04 16:50:30 +0000465TemplateInstantiator::TransformTemplateTypeParmType(
466 const TemplateTypeParmType *T) {
Douglas Gregor74296542009-02-27 19:31:52 +0000467 if (T->getDepth() == 0) {
468 // Replace the template type parameter with its corresponding
469 // template argument.
Douglas Gregorecd63b82009-07-01 00:28:38 +0000470
Douglas Gregor841324a2009-08-04 16:50:30 +0000471 // FIXME: When dealing with member templates, we might end up with multiple
472 /// levels of template arguments that we're substituting into concurrently.
473
Douglas Gregorecd63b82009-07-01 00:28:38 +0000474 // If the corresponding template argument is NULL or doesn't exist, it's
475 // because we are performing instantiation from explicitly-specified
476 // template arguments in a function template class, but there were some
477 // arguments left unspecified.
478 if (T->getIndex() >= TemplateArgs.size() ||
479 TemplateArgs[T->getIndex()].isNull())
480 return QualType(T, 0); // Would be nice to keep the original type here
481
Douglas Gregor74296542009-02-27 19:31:52 +0000482 assert(TemplateArgs[T->getIndex()].getKind() == TemplateArgument::Type &&
483 "Template argument kind mismatch");
Douglas Gregor072ac382009-06-26 21:40:05 +0000484 return TemplateArgs[T->getIndex()].getAsType();
Douglas Gregor74296542009-02-27 19:31:52 +0000485 }
486
487 // The template type parameter comes from an inner template (e.g.,
488 // the template parameter list of a member template inside the
489 // template we are instantiating). Create a new template type
490 // parameter with the template "level" reduced by one.
Douglas Gregor841324a2009-08-04 16:50:30 +0000491 return getSema().Context.getTemplateTypeParmType(T->getDepth() - 1,
492 T->getIndex(),
493 T->isParameterPack(),
494 T->getName());
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000495}
Douglas Gregor74296542009-02-27 19:31:52 +0000496
John McCall0ba26ee2009-08-25 22:02:44 +0000497/// \brief Perform substitution on the type T with a given set of template
498/// arguments.
Douglas Gregor74296542009-02-27 19:31:52 +0000499///
500/// This routine substitutes the given template arguments into the
501/// type T and produces the instantiated type.
502///
503/// \param T the type into which the template arguments will be
504/// substituted. If this type is not dependent, it will be returned
505/// immediately.
506///
507/// \param TemplateArgs the template arguments that will be
508/// substituted for the top-level template parameters within T.
509///
Douglas Gregor74296542009-02-27 19:31:52 +0000510/// \param Loc the location in the source code where this substitution
511/// is being performed. It will typically be the location of the
512/// declarator (if we're instantiating the type of some declaration)
513/// or the location of the type in the source code (if, e.g., we're
514/// instantiating the type of a cast expression).
515///
516/// \param Entity the name of the entity associated with a declaration
517/// being instantiated (if any). May be empty to indicate that there
518/// is no such entity (if, e.g., this is a type that occurs as part of
519/// a cast expression) or that the entity has no name (e.g., an
520/// unnamed function parameter).
521///
522/// \returns If the instantiation succeeds, the instantiated
523/// type. Otherwise, produces diagnostics and returns a NULL type.
John McCall0ba26ee2009-08-25 22:02:44 +0000524QualType Sema::SubstType(QualType T,
525 const TemplateArgumentList &TemplateArgs,
526 SourceLocation Loc, DeclarationName Entity) {
Douglas Gregor56d25a72009-03-10 20:44:00 +0000527 assert(!ActiveTemplateInstantiations.empty() &&
528 "Cannot perform an instantiation without some context on the "
529 "instantiation stack");
530
Douglas Gregor74296542009-02-27 19:31:52 +0000531 // If T is not a dependent type, there is nothing to do.
532 if (!T->isDependentType())
533 return T;
534
Douglas Gregor841324a2009-08-04 16:50:30 +0000535 TemplateInstantiator Instantiator(*this, TemplateArgs, Loc, Entity);
536 return Instantiator.TransformType(T);
Douglas Gregor74296542009-02-27 19:31:52 +0000537}
Douglas Gregored3a3982009-03-03 04:44:36 +0000538
John McCall0ba26ee2009-08-25 22:02:44 +0000539/// \brief Perform substitution on the base class specifiers of the
540/// given class template specialization.
Douglas Gregored3a3982009-03-03 04:44:36 +0000541///
542/// Produces a diagnostic and returns true on error, returns false and
543/// attaches the instantiated base classes to the class template
544/// specialization if successful.
545bool
John McCall0ba26ee2009-08-25 22:02:44 +0000546Sema::SubstBaseSpecifiers(CXXRecordDecl *Instantiation,
547 CXXRecordDecl *Pattern,
548 const TemplateArgumentList &TemplateArgs) {
Douglas Gregored3a3982009-03-03 04:44:36 +0000549 bool Invalid = false;
Douglas Gregor4ac20ef2009-05-29 18:27:38 +0000550 llvm::SmallVector<CXXBaseSpecifier*, 4> InstantiatedBases;
Douglas Gregorcc887972009-03-25 21:17:03 +0000551 for (ClassTemplateSpecializationDecl::base_class_iterator
552 Base = Pattern->bases_begin(), BaseEnd = Pattern->bases_end();
Douglas Gregord9572a12009-03-10 18:52:44 +0000553 Base != BaseEnd; ++Base) {
Douglas Gregored3a3982009-03-03 04:44:36 +0000554 if (!Base->getType()->isDependentType()) {
Fariborz Jahanian1373b6f2009-07-22 17:41:53 +0000555 InstantiatedBases.push_back(new (Context) CXXBaseSpecifier(*Base));
Douglas Gregored3a3982009-03-03 04:44:36 +0000556 continue;
557 }
558
John McCall0ba26ee2009-08-25 22:02:44 +0000559 QualType BaseType = SubstType(Base->getType(),
560 TemplateArgs,
561 Base->getSourceRange().getBegin(),
562 DeclarationName());
Douglas Gregored3a3982009-03-03 04:44:36 +0000563 if (BaseType.isNull()) {
564 Invalid = true;
565 continue;
566 }
567
568 if (CXXBaseSpecifier *InstantiatedBase
Douglas Gregorcc887972009-03-25 21:17:03 +0000569 = CheckBaseSpecifier(Instantiation,
Douglas Gregored3a3982009-03-03 04:44:36 +0000570 Base->getSourceRange(),
571 Base->isVirtual(),
572 Base->getAccessSpecifierAsWritten(),
573 BaseType,
574 /*FIXME: Not totally accurate */
575 Base->getSourceRange().getBegin()))
576 InstantiatedBases.push_back(InstantiatedBase);
577 else
578 Invalid = true;
579 }
580
Douglas Gregord9572a12009-03-10 18:52:44 +0000581 if (!Invalid &&
Jay Foad9e6bef42009-05-21 09:52:38 +0000582 AttachBaseSpecifiers(Instantiation, InstantiatedBases.data(),
Douglas Gregored3a3982009-03-03 04:44:36 +0000583 InstantiatedBases.size()))
584 Invalid = true;
585
586 return Invalid;
587}
588
John McCalld64b41e2009-08-20 01:44:21 +0000589/// \brief Force a template's pattern class to be instantiated.
590///
591/// \returns true if an error occurred
592bool Sema::InstantiateTemplatePattern(SourceLocation PointOfInstantiation,
593 CXXRecordDecl *Pattern) {
594 if (Pattern->getDefinition(Context)) return false;
595
596 ClassTemplateDecl *PatternTemp = Pattern->getDescribedClassTemplate();
597 if (!PatternTemp) return false;
598
599 // Check whether this template is a lazy instantiation of a
600 // dependent member template, e.g. Inner<U> in
601 // Outer<int>::Inner<U>.
602 ClassTemplateDecl *PatternPatternTemp
603 = PatternTemp->getInstantiatedFromMemberTemplate();
604 if (!PatternPatternTemp) return false;
605
606 ClassTemplateSpecializationDecl *Spec = 0;
607 for (DeclContext *Parent = Pattern->getDeclContext();
608 Parent && !Spec; Parent = Parent->getParent())
609 Spec = dyn_cast<ClassTemplateSpecializationDecl>(Parent);
610 assert(Spec && "Not a member of a class template specialization?");
611
612 // TODO: the error message from a nested failure here is probably
613 // not ideal.
614 return InstantiateClass(PointOfInstantiation,
615 Pattern,
616 PatternPatternTemp->getTemplatedDecl(),
617 Spec->getTemplateArgs(),
618 /* ExplicitInstantiation = */ false);
619}
620
Douglas Gregorcc887972009-03-25 21:17:03 +0000621/// \brief Instantiate the definition of a class from a given pattern.
622///
623/// \param PointOfInstantiation The point of instantiation within the
624/// source code.
625///
626/// \param Instantiation is the declaration whose definition is being
627/// instantiated. This will be either a class template specialization
628/// or a member class of a class template specialization.
629///
630/// \param Pattern is the pattern from which the instantiation
631/// occurs. This will be either the declaration of a class template or
632/// the declaration of a member class of a class template.
633///
634/// \param TemplateArgs The template arguments to be substituted into
635/// the pattern.
636///
Douglas Gregorb35c7992009-08-24 15:23:48 +0000637/// \param ExplicitInstantiation whether this is an explicit instantiation
638/// (otherwise, it is an implicit instantiation).
639///
640/// \param Complain whether to complain if the class cannot be instantiated due
641/// to the lack of a definition.
642///
Douglas Gregorcc887972009-03-25 21:17:03 +0000643/// \returns true if an error occurred, false otherwise.
644bool
645Sema::InstantiateClass(SourceLocation PointOfInstantiation,
646 CXXRecordDecl *Instantiation, CXXRecordDecl *Pattern,
Douglas Gregorfd79ac62009-05-13 00:25:59 +0000647 const TemplateArgumentList &TemplateArgs,
Douglas Gregorb35c7992009-08-24 15:23:48 +0000648 bool ExplicitInstantiation,
649 bool Complain) {
Douglas Gregorcc887972009-03-25 21:17:03 +0000650 bool Invalid = false;
John McCalld64b41e2009-08-20 01:44:21 +0000651
652 // Lazily instantiate member templates here.
653 if (InstantiateTemplatePattern(PointOfInstantiation, Pattern))
654 return true;
Douglas Gregorcc887972009-03-25 21:17:03 +0000655
656 CXXRecordDecl *PatternDef
657 = cast_or_null<CXXRecordDecl>(Pattern->getDefinition(Context));
658 if (!PatternDef) {
Douglas Gregorb35c7992009-08-24 15:23:48 +0000659 if (!Complain) {
660 // Say nothing
661 } else if (Pattern == Instantiation->getInstantiatedFromMemberClass()) {
Douglas Gregorcc887972009-03-25 21:17:03 +0000662 Diag(PointOfInstantiation,
663 diag::err_implicit_instantiate_member_undefined)
664 << Context.getTypeDeclType(Instantiation);
665 Diag(Pattern->getLocation(), diag::note_member_of_template_here);
666 } else {
Douglas Gregorfd79ac62009-05-13 00:25:59 +0000667 Diag(PointOfInstantiation, diag::err_template_instantiate_undefined)
668 << ExplicitInstantiation
Douglas Gregorcc887972009-03-25 21:17:03 +0000669 << Context.getTypeDeclType(Instantiation);
670 Diag(Pattern->getLocation(), diag::note_template_decl_here);
671 }
672 return true;
673 }
674 Pattern = PatternDef;
675
Douglas Gregor42c48522009-03-25 21:23:52 +0000676 InstantiatingTemplate Inst(*this, PointOfInstantiation, Instantiation);
Douglas Gregorcc887972009-03-25 21:17:03 +0000677 if (Inst)
678 return true;
679
680 // Enter the scope of this instantiation. We don't use
681 // PushDeclContext because we don't have a scope.
682 DeclContext *PreviousContext = CurContext;
683 CurContext = Instantiation;
684
685 // Start the definition of this instantiation.
686 Instantiation->startDefinition();
687
John McCall0ba26ee2009-08-25 22:02:44 +0000688 // Do substitution on the base class specifiers.
689 if (SubstBaseSpecifiers(Instantiation, Pattern, TemplateArgs))
Douglas Gregorcc887972009-03-25 21:17:03 +0000690 Invalid = true;
691
Douglas Gregor4ac20ef2009-05-29 18:27:38 +0000692 llvm::SmallVector<DeclPtrTy, 4> Fields;
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000693 for (RecordDecl::decl_iterator Member = Pattern->decls_begin(),
694 MemberEnd = Pattern->decls_end();
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000695 Member != MemberEnd; ++Member) {
John McCall0ba26ee2009-08-25 22:02:44 +0000696 Decl *NewMember = SubstDecl(*Member, Instantiation, TemplateArgs);
Douglas Gregorcc887972009-03-25 21:17:03 +0000697 if (NewMember) {
698 if (NewMember->isInvalidDecl())
699 Invalid = true;
700 else if (FieldDecl *Field = dyn_cast<FieldDecl>(NewMember))
Chris Lattner5261d0c2009-03-28 19:18:32 +0000701 Fields.push_back(DeclPtrTy::make(Field));
Douglas Gregorcc887972009-03-25 21:17:03 +0000702 } else {
703 // FIXME: Eventually, a NULL return will mean that one of the
Mike Stumpe127ae32009-05-16 07:39:55 +0000704 // instantiations was a semantic disaster, and we'll want to set Invalid =
705 // true. For now, we expect to skip some members that we can't yet handle.
Douglas Gregorcc887972009-03-25 21:17:03 +0000706 }
707 }
708
709 // Finish checking fields.
Chris Lattner5261d0c2009-03-28 19:18:32 +0000710 ActOnFields(0, Instantiation->getLocation(), DeclPtrTy::make(Instantiation),
Jay Foad9e6bef42009-05-21 09:52:38 +0000711 Fields.data(), Fields.size(), SourceLocation(), SourceLocation(),
Douglas Gregorcc887972009-03-25 21:17:03 +0000712 0);
713
714 // Add any implicitly-declared members that we might need.
715 AddImplicitlyDeclaredMembersToClass(Instantiation);
716
717 // Exit the scope of this instantiation.
718 CurContext = PreviousContext;
719
Douglas Gregordc18e892009-05-26 20:50:29 +0000720 if (!Invalid)
721 Consumer.HandleTagDeclDefinition(Instantiation);
722
Douglas Gregordf9f5d12009-05-13 20:28:22 +0000723 // If this is an explicit instantiation, instantiate our members, too.
Douglas Gregorb12249d2009-05-18 17:01:57 +0000724 if (!Invalid && ExplicitInstantiation) {
725 Inst.Clear();
Douglas Gregordf9f5d12009-05-13 20:28:22 +0000726 InstantiateClassMembers(PointOfInstantiation, Instantiation, TemplateArgs);
Douglas Gregorb12249d2009-05-18 17:01:57 +0000727 }
Douglas Gregordf9f5d12009-05-13 20:28:22 +0000728
Douglas Gregorcc887972009-03-25 21:17:03 +0000729 return Invalid;
730}
731
Douglas Gregored3a3982009-03-03 04:44:36 +0000732bool
733Sema::InstantiateClassTemplateSpecialization(
734 ClassTemplateSpecializationDecl *ClassTemplateSpec,
Douglas Gregorb35c7992009-08-24 15:23:48 +0000735 bool ExplicitInstantiation,
736 bool Complain) {
Douglas Gregored3a3982009-03-03 04:44:36 +0000737 // Perform the actual instantiation on the canonical declaration.
738 ClassTemplateSpec = cast<ClassTemplateSpecializationDecl>(
Argiris Kirtzidis17c7cab2009-07-18 00:34:25 +0000739 ClassTemplateSpec->getCanonicalDecl());
Douglas Gregored3a3982009-03-03 04:44:36 +0000740
741 // We can only instantiate something that hasn't already been
742 // instantiated or specialized. Fail without any diagnostics: our
743 // caller will provide an error message.
744 if (ClassTemplateSpec->getSpecializationKind() != TSK_Undeclared)
745 return true;
746
Douglas Gregored3a3982009-03-03 04:44:36 +0000747 ClassTemplateDecl *Template = ClassTemplateSpec->getSpecializedTemplate();
Douglas Gregorcc887972009-03-25 21:17:03 +0000748 CXXRecordDecl *Pattern = Template->getTemplatedDecl();
Douglas Gregor58944ac2009-05-31 09:31:02 +0000749 const TemplateArgumentList *TemplateArgs
750 = &ClassTemplateSpec->getTemplateArgs();
751
Douglas Gregor21530c52009-06-12 22:31:52 +0000752 // C++ [temp.class.spec.match]p1:
753 // When a class template is used in a context that requires an
754 // instantiation of the class, it is necessary to determine
755 // whether the instantiation is to be generated using the primary
756 // template or one of the partial specializations. This is done by
757 // matching the template arguments of the class template
758 // specialization with the template argument lists of the partial
759 // specializations.
Douglas Gregorab9f71a2009-06-05 00:53:49 +0000760 typedef std::pair<ClassTemplatePartialSpecializationDecl *,
761 TemplateArgumentList *> MatchResult;
762 llvm::SmallVector<MatchResult, 4> Matched;
Douglas Gregor58944ac2009-05-31 09:31:02 +0000763 for (llvm::FoldingSet<ClassTemplatePartialSpecializationDecl>::iterator
764 Partial = Template->getPartialSpecializations().begin(),
765 PartialEnd = Template->getPartialSpecializations().end();
766 Partial != PartialEnd;
767 ++Partial) {
Douglas Gregor623e2e02009-06-12 18:26:56 +0000768 TemplateDeductionInfo Info(Context);
769 if (TemplateDeductionResult Result
Douglas Gregorab9f71a2009-06-05 00:53:49 +0000770 = DeduceTemplateArguments(&*Partial,
Douglas Gregor623e2e02009-06-12 18:26:56 +0000771 ClassTemplateSpec->getTemplateArgs(),
772 Info)) {
773 // FIXME: Store the failed-deduction information for use in
774 // diagnostics, later.
775 (void)Result;
776 } else {
777 Matched.push_back(std::make_pair(&*Partial, Info.take()));
778 }
Douglas Gregor58944ac2009-05-31 09:31:02 +0000779 }
780
781 if (Matched.size() == 1) {
Douglas Gregor21530c52009-06-12 22:31:52 +0000782 // -- If exactly one matching specialization is found, the
783 // instantiation is generated from that specialization.
Douglas Gregorab9f71a2009-06-05 00:53:49 +0000784 Pattern = Matched[0].first;
785 TemplateArgs = Matched[0].second;
Douglas Gregor7b0b83f2009-08-02 23:24:31 +0000786 ClassTemplateSpec->setInstantiationOf(Matched[0].first, Matched[0].second);
Douglas Gregor58944ac2009-05-31 09:31:02 +0000787 } else if (Matched.size() > 1) {
Douglas Gregor21530c52009-06-12 22:31:52 +0000788 // -- If more than one matching specialization is found, the
789 // partial order rules (14.5.4.2) are used to determine
790 // whether one of the specializations is more specialized
791 // than the others. If none of the specializations is more
792 // specialized than all of the other matching
793 // specializations, then the use of the class template is
794 // ambiguous and the program is ill-formed.
Douglas Gregor58944ac2009-05-31 09:31:02 +0000795 // FIXME: Implement partial ordering of class template partial
796 // specializations.
797 Diag(ClassTemplateSpec->getLocation(),
798 diag::unsup_template_partial_spec_ordering);
Douglas Gregor21530c52009-06-12 22:31:52 +0000799 } else {
800 // -- If no matches are found, the instantiation is generated
801 // from the primary template.
802
803 // Since we initialized the pattern and template arguments from
804 // the primary template, there is nothing more we need to do here.
Douglas Gregor58944ac2009-05-31 09:31:02 +0000805 }
Douglas Gregored3a3982009-03-03 04:44:36 +0000806
807 // Note that this is an instantiation.
808 ClassTemplateSpec->setSpecializationKind(
809 ExplicitInstantiation? TSK_ExplicitInstantiation
810 : TSK_ImplicitInstantiation);
811
Douglas Gregorab9f71a2009-06-05 00:53:49 +0000812 bool Result = InstantiateClass(ClassTemplateSpec->getLocation(),
813 ClassTemplateSpec, Pattern, *TemplateArgs,
Douglas Gregorb35c7992009-08-24 15:23:48 +0000814 ExplicitInstantiation,
815 Complain);
Douglas Gregorab9f71a2009-06-05 00:53:49 +0000816
817 for (unsigned I = 0, N = Matched.size(); I != N; ++I) {
818 // FIXME: Implement TemplateArgumentList::Destroy!
819 // if (Matched[I].first != Pattern)
820 // Matched[I].second->Destroy(Context);
821 }
822
823 return Result;
Douglas Gregored3a3982009-03-03 04:44:36 +0000824}
Douglas Gregor47bde7c2009-03-19 17:26:29 +0000825
John McCall0ba26ee2009-08-25 22:02:44 +0000826/// \brief Instantiates the definitions of all of the member
827/// of the given class, which is an instantiation of a class template
828/// or a member class of a template.
Douglas Gregordf9f5d12009-05-13 20:28:22 +0000829void
830Sema::InstantiateClassMembers(SourceLocation PointOfInstantiation,
John McCall0ba26ee2009-08-25 22:02:44 +0000831 CXXRecordDecl *Instantiation,
832 const TemplateArgumentList &TemplateArgs) {
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000833 for (DeclContext::decl_iterator D = Instantiation->decls_begin(),
834 DEnd = Instantiation->decls_end();
Douglas Gregordf9f5d12009-05-13 20:28:22 +0000835 D != DEnd; ++D) {
836 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(*D)) {
Argiris Kirtzidisccb9efe2009-06-30 02:35:26 +0000837 if (!Function->getBody())
Douglas Gregorb12249d2009-05-18 17:01:57 +0000838 InstantiateFunctionDefinition(PointOfInstantiation, Function);
Douglas Gregordf9f5d12009-05-13 20:28:22 +0000839 } else if (VarDecl *Var = dyn_cast<VarDecl>(*D)) {
Douglas Gregor181fe792009-07-24 20:34:43 +0000840 if (Var->isStaticDataMember())
841 InstantiateStaticDataMemberDefinition(PointOfInstantiation, Var);
Douglas Gregordf9f5d12009-05-13 20:28:22 +0000842 } else if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(*D)) {
843 if (!Record->isInjectedClassName() && !Record->getDefinition(Context)) {
844 assert(Record->getInstantiatedFromMemberClass() &&
845 "Missing instantiated-from-template information");
Douglas Gregorb12249d2009-05-18 17:01:57 +0000846 InstantiateClass(PointOfInstantiation, Record,
Douglas Gregordf9f5d12009-05-13 20:28:22 +0000847 Record->getInstantiatedFromMemberClass(),
848 TemplateArgs, true);
849 }
850 }
851 }
852}
853
854/// \brief Instantiate the definitions of all of the members of the
855/// given class template specialization, which was named as part of an
856/// explicit instantiation.
857void Sema::InstantiateClassTemplateSpecializationMembers(
858 SourceLocation PointOfInstantiation,
859 ClassTemplateSpecializationDecl *ClassTemplateSpec) {
860 // C++0x [temp.explicit]p7:
861 // An explicit instantiation that names a class template
862 // specialization is an explicit instantion of the same kind
863 // (declaration or definition) of each of its members (not
864 // including members inherited from base classes) that has not
865 // been previously explicitly specialized in the translation unit
866 // containing the explicit instantiation, except as described
867 // below.
868 InstantiateClassMembers(PointOfInstantiation, ClassTemplateSpec,
869 ClassTemplateSpec->getTemplateArgs());
870}
871
Douglas Gregor23a44be2009-08-20 07:17:43 +0000872Sema::OwningStmtResult
John McCall0ba26ee2009-08-25 22:02:44 +0000873Sema::SubstStmt(Stmt *S, const TemplateArgumentList &TemplateArgs) {
Douglas Gregor23a44be2009-08-20 07:17:43 +0000874 if (!S)
875 return Owned(S);
876
877 TemplateInstantiator Instantiator(*this, TemplateArgs,
878 SourceLocation(),
879 DeclarationName());
880 return Instantiator.TransformStmt(S);
881}
882
Douglas Gregor9d879762009-08-11 05:31:07 +0000883Sema::OwningExprResult
John McCall0ba26ee2009-08-25 22:02:44 +0000884Sema::SubstExpr(Expr *E, const TemplateArgumentList &TemplateArgs) {
Douglas Gregor9d879762009-08-11 05:31:07 +0000885 if (!E)
886 return Owned(E);
887
888 TemplateInstantiator Instantiator(*this, TemplateArgs,
889 SourceLocation(),
890 DeclarationName());
891 return Instantiator.TransformExpr(E);
892}
893
John McCall0ba26ee2009-08-25 22:02:44 +0000894/// \brief Do template substitution on a nested-name-specifier.
Douglas Gregor1e589cc2009-03-26 23:50:42 +0000895NestedNameSpecifier *
John McCall0ba26ee2009-08-25 22:02:44 +0000896Sema::SubstNestedNameSpecifier(NestedNameSpecifier *NNS,
Douglas Gregor1e589cc2009-03-26 23:50:42 +0000897 SourceRange Range,
Douglas Gregorf9e7d3d2009-05-11 23:53:27 +0000898 const TemplateArgumentList &TemplateArgs) {
Douglas Gregor12431cb2009-08-06 05:28:30 +0000899 TemplateInstantiator Instantiator(*this, TemplateArgs, Range.getBegin(),
900 DeclarationName());
901 return Instantiator.TransformNestedNameSpecifier(NNS, Range);
Douglas Gregor47bde7c2009-03-19 17:26:29 +0000902}
Douglas Gregor15a92852009-03-31 18:38:02 +0000903
904TemplateName
John McCall0ba26ee2009-08-25 22:02:44 +0000905Sema::SubstTemplateName(TemplateName Name, SourceLocation Loc,
906 const TemplateArgumentList &TemplateArgs) {
Douglas Gregor214d0462009-08-06 06:41:21 +0000907 TemplateInstantiator Instantiator(*this, TemplateArgs, Loc,
908 DeclarationName());
909 return Instantiator.TransformTemplateName(Name);
Douglas Gregor15a92852009-03-31 18:38:02 +0000910}
Douglas Gregorb320b9e2009-06-11 00:06:24 +0000911
John McCall0ba26ee2009-08-25 22:02:44 +0000912TemplateArgument Sema::Subst(TemplateArgument Arg,
913 const TemplateArgumentList &TemplateArgs) {
Douglas Gregor2999faa2009-08-04 22:27:00 +0000914 TemplateInstantiator Instantiator(*this, TemplateArgs, SourceLocation(),
915 DeclarationName());
916 return Instantiator.TransformTemplateArgument(Arg);
Douglas Gregorb320b9e2009-06-11 00:06:24 +0000917}