blob: 4642251d95ad57e7f74173f4546043bb27060029 [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.
31const TemplateArgumentList &
32Sema::getTemplateInstantiationArgs(NamedDecl *D) {
Douglas Gregor6f5e0542009-06-26 00:10:03 +000033 // Template arguments for a class template specialization.
Douglas Gregor5f62c5e2009-05-14 23:26:13 +000034 if (ClassTemplateSpecializationDecl *Spec
35 = dyn_cast<ClassTemplateSpecializationDecl>(D))
Douglas Gregor7b0b83f2009-08-02 23:24:31 +000036 return Spec->getTemplateInstantiationArgs();
Douglas Gregor5f62c5e2009-05-14 23:26:13 +000037
Douglas Gregor6f5e0542009-06-26 00:10:03 +000038 // Template arguments for a function template specialization.
39 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D))
40 if (const TemplateArgumentList *TemplateArgs
41 = Function->getTemplateSpecializationArgs())
42 return *TemplateArgs;
43
44 // Template arguments for a member of a class template specialization.
Douglas Gregor5f62c5e2009-05-14 23:26:13 +000045 DeclContext *EnclosingTemplateCtx = D->getDeclContext();
46 while (!isa<ClassTemplateSpecializationDecl>(EnclosingTemplateCtx)) {
47 assert(!EnclosingTemplateCtx->isFileContext() &&
48 "Tried to get the instantiation arguments of a non-template");
49 EnclosingTemplateCtx = EnclosingTemplateCtx->getParent();
50 }
51
52 ClassTemplateSpecializationDecl *EnclosingTemplate
53 = cast<ClassTemplateSpecializationDecl>(EnclosingTemplateCtx);
Douglas Gregor7b0b83f2009-08-02 23:24:31 +000054 return EnclosingTemplate->getTemplateInstantiationArgs();
Douglas Gregor5f62c5e2009-05-14 23:26:13 +000055}
56
Douglas Gregor375733c2009-03-10 00:06:19 +000057Sema::InstantiatingTemplate::
58InstantiatingTemplate(Sema &SemaRef, SourceLocation PointOfInstantiation,
Douglas Gregorb12249d2009-05-18 17:01:57 +000059 Decl *Entity,
Douglas Gregor375733c2009-03-10 00:06:19 +000060 SourceRange InstantiationRange)
61 : SemaRef(SemaRef) {
Douglas Gregor56d25a72009-03-10 20:44:00 +000062
63 Invalid = CheckInstantiationDepth(PointOfInstantiation,
64 InstantiationRange);
65 if (!Invalid) {
Douglas Gregor375733c2009-03-10 00:06:19 +000066 ActiveTemplateInstantiation Inst;
Douglas Gregor56d25a72009-03-10 20:44:00 +000067 Inst.Kind = ActiveTemplateInstantiation::TemplateInstantiation;
Douglas Gregor375733c2009-03-10 00:06:19 +000068 Inst.PointOfInstantiation = PointOfInstantiation;
Douglas Gregor56d25a72009-03-10 20:44:00 +000069 Inst.Entity = reinterpret_cast<uintptr_t>(Entity);
Douglas Gregor95ba1282009-03-12 18:36:18 +000070 Inst.TemplateArgs = 0;
71 Inst.NumTemplateArgs = 0;
Douglas Gregor56d25a72009-03-10 20:44:00 +000072 Inst.InstantiationRange = InstantiationRange;
73 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
74 Invalid = false;
75 }
76}
77
78Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef,
79 SourceLocation PointOfInstantiation,
80 TemplateDecl *Template,
81 const TemplateArgument *TemplateArgs,
82 unsigned NumTemplateArgs,
83 SourceRange InstantiationRange)
84 : SemaRef(SemaRef) {
85
86 Invalid = CheckInstantiationDepth(PointOfInstantiation,
87 InstantiationRange);
88 if (!Invalid) {
89 ActiveTemplateInstantiation Inst;
90 Inst.Kind
91 = ActiveTemplateInstantiation::DefaultTemplateArgumentInstantiation;
92 Inst.PointOfInstantiation = PointOfInstantiation;
93 Inst.Entity = reinterpret_cast<uintptr_t>(Template);
94 Inst.TemplateArgs = TemplateArgs;
95 Inst.NumTemplateArgs = NumTemplateArgs;
Douglas Gregor375733c2009-03-10 00:06:19 +000096 Inst.InstantiationRange = InstantiationRange;
97 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
98 Invalid = false;
99 }
100}
101
Douglas Gregorc5e01af2009-06-10 23:47:09 +0000102Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef,
103 SourceLocation PointOfInstantiation,
Douglas Gregor4c8b2b32009-07-01 22:01:06 +0000104 FunctionTemplateDecl *FunctionTemplate,
105 const TemplateArgument *TemplateArgs,
106 unsigned NumTemplateArgs,
107 ActiveTemplateInstantiation::InstantiationKind Kind,
108 SourceRange InstantiationRange)
109: SemaRef(SemaRef) {
110
111 Invalid = CheckInstantiationDepth(PointOfInstantiation,
112 InstantiationRange);
113 if (!Invalid) {
114 ActiveTemplateInstantiation Inst;
115 Inst.Kind = Kind;
116 Inst.PointOfInstantiation = PointOfInstantiation;
117 Inst.Entity = reinterpret_cast<uintptr_t>(FunctionTemplate);
118 Inst.TemplateArgs = TemplateArgs;
119 Inst.NumTemplateArgs = NumTemplateArgs;
120 Inst.InstantiationRange = InstantiationRange;
121 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
122 Invalid = false;
123 }
124}
125
126Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef,
127 SourceLocation PointOfInstantiation,
Douglas Gregorc5e01af2009-06-10 23:47:09 +0000128 ClassTemplatePartialSpecializationDecl *PartialSpec,
129 const TemplateArgument *TemplateArgs,
130 unsigned NumTemplateArgs,
131 SourceRange InstantiationRange)
132 : SemaRef(SemaRef) {
133
134 Invalid = CheckInstantiationDepth(PointOfInstantiation,
135 InstantiationRange);
136 if (!Invalid) {
137 ActiveTemplateInstantiation Inst;
138 Inst.Kind
Douglas Gregor4c8b2b32009-07-01 22:01:06 +0000139 = ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution;
Douglas Gregorc5e01af2009-06-10 23:47:09 +0000140 Inst.PointOfInstantiation = PointOfInstantiation;
141 Inst.Entity = reinterpret_cast<uintptr_t>(PartialSpec);
142 Inst.TemplateArgs = TemplateArgs;
143 Inst.NumTemplateArgs = NumTemplateArgs;
144 Inst.InstantiationRange = InstantiationRange;
145 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
146 Invalid = false;
147 }
148}
149
Douglas Gregorb12249d2009-05-18 17:01:57 +0000150void Sema::InstantiatingTemplate::Clear() {
151 if (!Invalid) {
Douglas Gregor375733c2009-03-10 00:06:19 +0000152 SemaRef.ActiveTemplateInstantiations.pop_back();
Douglas Gregorb12249d2009-05-18 17:01:57 +0000153 Invalid = true;
154 }
Douglas Gregor375733c2009-03-10 00:06:19 +0000155}
156
Douglas Gregor56d25a72009-03-10 20:44:00 +0000157bool Sema::InstantiatingTemplate::CheckInstantiationDepth(
158 SourceLocation PointOfInstantiation,
159 SourceRange InstantiationRange) {
160 if (SemaRef.ActiveTemplateInstantiations.size()
161 <= SemaRef.getLangOptions().InstantiationDepth)
162 return false;
163
164 SemaRef.Diag(PointOfInstantiation,
165 diag::err_template_recursion_depth_exceeded)
166 << SemaRef.getLangOptions().InstantiationDepth
167 << InstantiationRange;
168 SemaRef.Diag(PointOfInstantiation, diag::note_template_recursion_depth)
169 << SemaRef.getLangOptions().InstantiationDepth;
170 return true;
171}
172
Douglas Gregorfee85d62009-03-10 18:03:33 +0000173/// \brief Prints the current instantiation stack through a series of
174/// notes.
175void Sema::PrintInstantiationStack() {
Douglas Gregor4c8b2b32009-07-01 22:01:06 +0000176 // FIXME: In all of these cases, we need to show the template arguments
Douglas Gregorfee85d62009-03-10 18:03:33 +0000177 for (llvm::SmallVector<ActiveTemplateInstantiation, 16>::reverse_iterator
178 Active = ActiveTemplateInstantiations.rbegin(),
179 ActiveEnd = ActiveTemplateInstantiations.rend();
180 Active != ActiveEnd;
181 ++Active) {
Douglas Gregor56d25a72009-03-10 20:44:00 +0000182 switch (Active->Kind) {
183 case ActiveTemplateInstantiation::TemplateInstantiation: {
Douglas Gregorb12249d2009-05-18 17:01:57 +0000184 Decl *D = reinterpret_cast<Decl *>(Active->Entity);
185 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) {
186 unsigned DiagID = diag::note_template_member_class_here;
187 if (isa<ClassTemplateSpecializationDecl>(Record))
188 DiagID = diag::note_template_class_instantiation_here;
189 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
190 DiagID)
191 << Context.getTypeDeclType(Record)
192 << Active->InstantiationRange;
Douglas Gregor181fe792009-07-24 20:34:43 +0000193 } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
Douglas Gregor6f5e0542009-06-26 00:10:03 +0000194 unsigned DiagID;
195 if (Function->getPrimaryTemplate())
196 DiagID = diag::note_function_template_spec_here;
197 else
198 DiagID = diag::note_template_member_function_here;
Douglas Gregorb12249d2009-05-18 17:01:57 +0000199 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
200 DiagID)
201 << Function
202 << Active->InstantiationRange;
Douglas Gregor181fe792009-07-24 20:34:43 +0000203 } else {
204 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
205 diag::note_template_static_data_member_def_here)
206 << cast<VarDecl>(D)
207 << Active->InstantiationRange;
Douglas Gregorb12249d2009-05-18 17:01:57 +0000208 }
Douglas Gregor56d25a72009-03-10 20:44:00 +0000209 break;
210 }
211
212 case ActiveTemplateInstantiation::DefaultTemplateArgumentInstantiation: {
213 TemplateDecl *Template = cast<TemplateDecl>((Decl *)Active->Entity);
214 std::string TemplateArgsStr
Douglas Gregordd13e842009-03-30 22:58:21 +0000215 = TemplateSpecializationType::PrintTemplateArgumentList(
Douglas Gregor4c8b2b32009-07-01 22:01:06 +0000216 Active->TemplateArgs,
Douglas Gregor3bf3bbc2009-05-29 20:38:28 +0000217 Active->NumTemplateArgs,
218 Context.PrintingPolicy);
Douglas Gregor56d25a72009-03-10 20:44:00 +0000219 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
220 diag::note_default_arg_instantiation_here)
221 << (Template->getNameAsString() + TemplateArgsStr)
222 << Active->InstantiationRange;
223 break;
224 }
Douglas Gregorc5e01af2009-06-10 23:47:09 +0000225
Douglas Gregor4c8b2b32009-07-01 22:01:06 +0000226 case ActiveTemplateInstantiation::ExplicitTemplateArgumentSubstitution: {
227 FunctionTemplateDecl *FnTmpl
228 = cast<FunctionTemplateDecl>((Decl *)Active->Entity);
Douglas Gregorc5e01af2009-06-10 23:47:09 +0000229 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
Douglas Gregor4c8b2b32009-07-01 22:01:06 +0000230 diag::note_explicit_template_arg_substitution_here)
231 << FnTmpl << Active->InstantiationRange;
Douglas Gregorc5e01af2009-06-10 23:47:09 +0000232 break;
233 }
Douglas Gregor4c8b2b32009-07-01 22:01:06 +0000234
235 case ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution:
236 if (ClassTemplatePartialSpecializationDecl *PartialSpec
237 = dyn_cast<ClassTemplatePartialSpecializationDecl>(
238 (Decl *)Active->Entity)) {
239 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
240 diag::note_partial_spec_deduct_instantiation_here)
241 << Context.getTypeDeclType(PartialSpec)
242 << Active->InstantiationRange;
243 } else {
244 FunctionTemplateDecl *FnTmpl
245 = cast<FunctionTemplateDecl>((Decl *)Active->Entity);
246 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
247 diag::note_function_template_deduction_instantiation_here)
248 << FnTmpl << Active->InstantiationRange;
249 }
250 break;
Douglas Gregorc5e01af2009-06-10 23:47:09 +0000251
Douglas Gregor56d25a72009-03-10 20:44:00 +0000252 }
Douglas Gregorfee85d62009-03-10 18:03:33 +0000253 }
254}
255
Douglas Gregor95d6c952009-06-14 07:33:30 +0000256bool Sema::isSFINAEContext() const {
257 using llvm::SmallVector;
258 for (SmallVector<ActiveTemplateInstantiation, 16>::const_reverse_iterator
259 Active = ActiveTemplateInstantiations.rbegin(),
260 ActiveEnd = ActiveTemplateInstantiations.rend();
261 Active != ActiveEnd;
262 ++Active) {
263
264 switch(Active->Kind) {
Douglas Gregor4c8b2b32009-07-01 22:01:06 +0000265 case ActiveTemplateInstantiation::TemplateInstantiation:
266 // This is a template instantiation, so there is no SFINAE.
267 return false;
268
Douglas Gregor95d6c952009-06-14 07:33:30 +0000269 case ActiveTemplateInstantiation::DefaultTemplateArgumentInstantiation:
270 // A default template argument instantiation may or may not be a
271 // SFINAE context; look further up the stack.
272 break;
Douglas Gregor4c8b2b32009-07-01 22:01:06 +0000273
274 case ActiveTemplateInstantiation::ExplicitTemplateArgumentSubstitution:
275 case ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution:
276 // We're either substitution explicitly-specified template arguments
277 // or deduced template arguments, so SFINAE applies.
278 return true;
Douglas Gregor95d6c952009-06-14 07:33:30 +0000279 }
280 }
281
282 return false;
283}
284
Douglas Gregor74296542009-02-27 19:31:52 +0000285//===----------------------------------------------------------------------===/
286// Template Instantiation for Types
287//===----------------------------------------------------------------------===/
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000288namespace {
Douglas Gregor841324a2009-08-04 16:50:30 +0000289 class VISIBILITY_HIDDEN TemplateInstantiator
290 : public TreeTransform<TemplateInstantiator>
291 {
Douglas Gregorf9e7d3d2009-05-11 23:53:27 +0000292 const TemplateArgumentList &TemplateArgs;
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000293 SourceLocation Loc;
294 DeclarationName Entity;
Douglas Gregor74296542009-02-27 19:31:52 +0000295
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000296 public:
Douglas Gregor23a44be2009-08-20 07:17:43 +0000297 typedef TreeTransform<TemplateInstantiator> inherited;
298
Douglas Gregor841324a2009-08-04 16:50:30 +0000299 TemplateInstantiator(Sema &SemaRef,
300 const TemplateArgumentList &TemplateArgs,
301 SourceLocation Loc,
302 DeclarationName Entity)
Douglas Gregor23a44be2009-08-20 07:17:43 +0000303 : inherited(SemaRef), TemplateArgs(TemplateArgs), Loc(Loc),
304 Entity(Entity) { }
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000305
Douglas Gregor841324a2009-08-04 16:50:30 +0000306 /// \brief Determine whether the given type \p T has already been
307 /// transformed.
308 ///
309 /// For the purposes of template instantiation, a type has already been
310 /// transformed if it is NULL or if it is not dependent.
311 bool AlreadyTransformed(QualType T) {
312 return T.isNull() || !T->isDependentType();
Douglas Gregor90177912009-05-13 18:28:20 +0000313 }
Douglas Gregor841324a2009-08-04 16:50:30 +0000314
315 /// \brief Returns the location of the entity being instantiated, if known.
316 SourceLocation getBaseLocation() { return Loc; }
317
318 /// \brief Returns the name of the entity being instantiated, if any.
319 DeclarationName getBaseEntity() { return Entity; }
320
Douglas Gregor841324a2009-08-04 16:50:30 +0000321 /// \brief Transform the given declaration by instantiating a reference to
322 /// this declaration.
323 Decl *TransformDecl(Decl *D);
Douglas Gregor9d879762009-08-11 05:31:07 +0000324
Douglas Gregor23a44be2009-08-20 07:17:43 +0000325 /// \brief Transform the definition of the given declaration by
326 /// instantiating it.
327 Decl *TransformDefinition(Decl *D);
328
329 /// \brief Rebuild the exception declaration and register the declaration
330 /// as an instantiated local.
331 VarDecl *RebuildExceptionDecl(VarDecl *ExceptionDecl, QualType T,
332 DeclaratorInfo *Declarator,
333 IdentifierInfo *Name,
334 SourceLocation Loc, SourceRange TypeRange);
Douglas Gregor9d879762009-08-11 05:31:07 +0000335
336 Sema::OwningExprResult TransformDeclRefExpr(DeclRefExpr *E);
337
Douglas Gregor841324a2009-08-04 16:50:30 +0000338 /// \brief Transforms a template type parameter type by performing
339 /// substitution of the corresponding template type argument.
340 QualType TransformTemplateTypeParmType(const TemplateTypeParmType *T);
341 };
Douglas Gregor1d381132009-07-06 15:59:29 +0000342}
343
Douglas Gregor841324a2009-08-04 16:50:30 +0000344Decl *TemplateInstantiator::TransformDecl(Decl *D) {
Douglas Gregor214d0462009-08-06 06:41:21 +0000345 if (TemplateTemplateParmDecl *TTP
346 = dyn_cast_or_null<TemplateTemplateParmDecl>(D)) {
347 // FIXME: Depth reduction
348 assert(TTP->getDepth() == 0 &&
349 "Cannot reduce depth of a template template parameter");
350 assert(TemplateArgs[TTP->getPosition()].getAsDecl() &&
351 "Wrong kind of template template argument");
352 TemplateDecl *Template
353 = dyn_cast<TemplateDecl>(TemplateArgs[TTP->getPosition()].getAsDecl());
354 assert(Template && "Expected a template");
355 return Template;
356 }
357
John McCall0ba26ee2009-08-25 22:02:44 +0000358 return SemaRef.FindInstantiatedDecl(cast_or_null<NamedDecl>(D));
Douglas Gregor841324a2009-08-04 16:50:30 +0000359}
360
Douglas Gregor23a44be2009-08-20 07:17:43 +0000361Decl *TemplateInstantiator::TransformDefinition(Decl *D) {
John McCall0ba26ee2009-08-25 22:02:44 +0000362 Decl *Inst = getSema().SubstDecl(D, getSema().CurContext, TemplateArgs);
Douglas Gregor23a44be2009-08-20 07:17:43 +0000363 if (!Inst)
364 return 0;
365
366 getSema().CurrentInstantiationScope->InstantiatedLocal(D, Inst);
367 return Inst;
368}
369
370VarDecl *
371TemplateInstantiator::RebuildExceptionDecl(VarDecl *ExceptionDecl,
372 QualType T,
373 DeclaratorInfo *Declarator,
374 IdentifierInfo *Name,
375 SourceLocation Loc,
376 SourceRange TypeRange) {
377 VarDecl *Var = inherited::RebuildExceptionDecl(ExceptionDecl, T, Declarator,
378 Name, Loc, TypeRange);
379 if (Var && !Var->isInvalidDecl())
380 getSema().CurrentInstantiationScope->InstantiatedLocal(ExceptionDecl, Var);
381 return Var;
382}
383
Douglas Gregor9d879762009-08-11 05:31:07 +0000384Sema::OwningExprResult
385TemplateInstantiator::TransformDeclRefExpr(DeclRefExpr *E) {
386 // FIXME: Clean this up a bit
387 NamedDecl *D = E->getDecl();
388 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(D)) {
389 assert(NTTP->getDepth() == 0 && "No nested templates yet");
390
391 // If the corresponding template argument is NULL or non-existent, it's
392 // because we are performing instantiation from explicitly-specified
393 // template arguments in a function template, but there were some
394 // arguments left unspecified.
395 if (NTTP->getPosition() >= TemplateArgs.size() ||
396 TemplateArgs[NTTP->getPosition()].isNull())
397 return SemaRef.Owned(E); // FIXME: Clone the expression!
398
399 const TemplateArgument &Arg = TemplateArgs[NTTP->getPosition()];
400
401 // The template argument itself might be an expression, in which
402 // case we just return that expression.
403 if (Arg.getKind() == TemplateArgument::Expression)
404 // FIXME: Clone the expression!
405 return SemaRef.Owned(Arg.getAsExpr());
406
407 if (Arg.getKind() == TemplateArgument::Declaration) {
408 ValueDecl *VD = cast<ValueDecl>(Arg.getAsDecl());
409
410 // FIXME: Can VD ever have a dependent type?
411 return SemaRef.BuildDeclRefExpr(VD, VD->getType(), E->getLocation(),
412 false, false);
413 }
414
415 assert(Arg.getKind() == TemplateArgument::Integral);
416 QualType T = Arg.getIntegralType();
417 if (T->isCharType() || T->isWideCharType())
418 return SemaRef.Owned(new (SemaRef.Context) CharacterLiteral(
419 Arg.getAsIntegral()->getZExtValue(),
420 T->isWideCharType(),
421 T,
422 E->getSourceRange().getBegin()));
423 if (T->isBooleanType())
424 return SemaRef.Owned(new (SemaRef.Context) CXXBoolLiteralExpr(
425 Arg.getAsIntegral()->getBoolValue(),
426 T,
427 E->getSourceRange().getBegin()));
428
429 assert(Arg.getAsIntegral()->getBitWidth() == SemaRef.Context.getIntWidth(T));
430 return SemaRef.Owned(new (SemaRef.Context) IntegerLiteral(
431 *Arg.getAsIntegral(),
432 T,
433 E->getSourceRange().getBegin()));
434 }
435
436 if (OverloadedFunctionDecl *Ovl = dyn_cast<OverloadedFunctionDecl>(D)) {
437 // FIXME: instantiate each decl in the overload set
438 return SemaRef.Owned(new (SemaRef.Context) DeclRefExpr(Ovl,
439 SemaRef.Context.OverloadTy,
440 E->getLocation(),
441 false, false));
442 }
443
John McCall0ba26ee2009-08-25 22:02:44 +0000444 NamedDecl *InstD = SemaRef.FindInstantiatedDecl(D);
Douglas Gregor9d879762009-08-11 05:31:07 +0000445 if (!InstD)
446 return SemaRef.ExprError();
447
448 // FIXME: nested-name-specifier for QualifiedDeclRefExpr
449 return SemaRef.BuildDeclarationNameExpr(E->getLocation(), InstD,
450 /*FIXME:*/false,
451 /*FIXME:*/0,
452 /*FIXME:*/false);
453}
454
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000455QualType
Douglas Gregor841324a2009-08-04 16:50:30 +0000456TemplateInstantiator::TransformTemplateTypeParmType(
457 const TemplateTypeParmType *T) {
Douglas Gregor74296542009-02-27 19:31:52 +0000458 if (T->getDepth() == 0) {
459 // Replace the template type parameter with its corresponding
460 // template argument.
Douglas Gregorecd63b82009-07-01 00:28:38 +0000461
Douglas Gregor841324a2009-08-04 16:50:30 +0000462 // FIXME: When dealing with member templates, we might end up with multiple
463 /// levels of template arguments that we're substituting into concurrently.
464
Douglas Gregorecd63b82009-07-01 00:28:38 +0000465 // If the corresponding template argument is NULL or doesn't exist, it's
466 // because we are performing instantiation from explicitly-specified
467 // template arguments in a function template class, but there were some
468 // arguments left unspecified.
469 if (T->getIndex() >= TemplateArgs.size() ||
470 TemplateArgs[T->getIndex()].isNull())
471 return QualType(T, 0); // Would be nice to keep the original type here
472
Douglas Gregor74296542009-02-27 19:31:52 +0000473 assert(TemplateArgs[T->getIndex()].getKind() == TemplateArgument::Type &&
474 "Template argument kind mismatch");
Douglas Gregor072ac382009-06-26 21:40:05 +0000475 return TemplateArgs[T->getIndex()].getAsType();
Douglas Gregor74296542009-02-27 19:31:52 +0000476 }
477
478 // The template type parameter comes from an inner template (e.g.,
479 // the template parameter list of a member template inside the
480 // template we are instantiating). Create a new template type
481 // parameter with the template "level" reduced by one.
Douglas Gregor841324a2009-08-04 16:50:30 +0000482 return getSema().Context.getTemplateTypeParmType(T->getDepth() - 1,
483 T->getIndex(),
484 T->isParameterPack(),
485 T->getName());
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000486}
Douglas Gregor74296542009-02-27 19:31:52 +0000487
John McCall0ba26ee2009-08-25 22:02:44 +0000488/// \brief Perform substitution on the type T with a given set of template
489/// arguments.
Douglas Gregor74296542009-02-27 19:31:52 +0000490///
491/// This routine substitutes the given template arguments into the
492/// type T and produces the instantiated type.
493///
494/// \param T the type into which the template arguments will be
495/// substituted. If this type is not dependent, it will be returned
496/// immediately.
497///
498/// \param TemplateArgs the template arguments that will be
499/// substituted for the top-level template parameters within T.
500///
Douglas Gregor74296542009-02-27 19:31:52 +0000501/// \param Loc the location in the source code where this substitution
502/// is being performed. It will typically be the location of the
503/// declarator (if we're instantiating the type of some declaration)
504/// or the location of the type in the source code (if, e.g., we're
505/// instantiating the type of a cast expression).
506///
507/// \param Entity the name of the entity associated with a declaration
508/// being instantiated (if any). May be empty to indicate that there
509/// is no such entity (if, e.g., this is a type that occurs as part of
510/// a cast expression) or that the entity has no name (e.g., an
511/// unnamed function parameter).
512///
513/// \returns If the instantiation succeeds, the instantiated
514/// type. Otherwise, produces diagnostics and returns a NULL type.
John McCall0ba26ee2009-08-25 22:02:44 +0000515QualType Sema::SubstType(QualType T,
516 const TemplateArgumentList &TemplateArgs,
517 SourceLocation Loc, DeclarationName Entity) {
Douglas Gregor56d25a72009-03-10 20:44:00 +0000518 assert(!ActiveTemplateInstantiations.empty() &&
519 "Cannot perform an instantiation without some context on the "
520 "instantiation stack");
521
Douglas Gregor74296542009-02-27 19:31:52 +0000522 // If T is not a dependent type, there is nothing to do.
523 if (!T->isDependentType())
524 return T;
525
Douglas Gregor841324a2009-08-04 16:50:30 +0000526 TemplateInstantiator Instantiator(*this, TemplateArgs, Loc, Entity);
527 return Instantiator.TransformType(T);
Douglas Gregor74296542009-02-27 19:31:52 +0000528}
Douglas Gregored3a3982009-03-03 04:44:36 +0000529
John McCall0ba26ee2009-08-25 22:02:44 +0000530/// \brief Perform substitution on the base class specifiers of the
531/// given class template specialization.
Douglas Gregored3a3982009-03-03 04:44:36 +0000532///
533/// Produces a diagnostic and returns true on error, returns false and
534/// attaches the instantiated base classes to the class template
535/// specialization if successful.
536bool
John McCall0ba26ee2009-08-25 22:02:44 +0000537Sema::SubstBaseSpecifiers(CXXRecordDecl *Instantiation,
538 CXXRecordDecl *Pattern,
539 const TemplateArgumentList &TemplateArgs) {
Douglas Gregored3a3982009-03-03 04:44:36 +0000540 bool Invalid = false;
Douglas Gregor4ac20ef2009-05-29 18:27:38 +0000541 llvm::SmallVector<CXXBaseSpecifier*, 4> InstantiatedBases;
Douglas Gregorcc887972009-03-25 21:17:03 +0000542 for (ClassTemplateSpecializationDecl::base_class_iterator
543 Base = Pattern->bases_begin(), BaseEnd = Pattern->bases_end();
Douglas Gregord9572a12009-03-10 18:52:44 +0000544 Base != BaseEnd; ++Base) {
Douglas Gregored3a3982009-03-03 04:44:36 +0000545 if (!Base->getType()->isDependentType()) {
Fariborz Jahanian1373b6f2009-07-22 17:41:53 +0000546 InstantiatedBases.push_back(new (Context) CXXBaseSpecifier(*Base));
Douglas Gregored3a3982009-03-03 04:44:36 +0000547 continue;
548 }
549
John McCall0ba26ee2009-08-25 22:02:44 +0000550 QualType BaseType = SubstType(Base->getType(),
551 TemplateArgs,
552 Base->getSourceRange().getBegin(),
553 DeclarationName());
Douglas Gregored3a3982009-03-03 04:44:36 +0000554 if (BaseType.isNull()) {
555 Invalid = true;
556 continue;
557 }
558
559 if (CXXBaseSpecifier *InstantiatedBase
Douglas Gregorcc887972009-03-25 21:17:03 +0000560 = CheckBaseSpecifier(Instantiation,
Douglas Gregored3a3982009-03-03 04:44:36 +0000561 Base->getSourceRange(),
562 Base->isVirtual(),
563 Base->getAccessSpecifierAsWritten(),
564 BaseType,
565 /*FIXME: Not totally accurate */
566 Base->getSourceRange().getBegin()))
567 InstantiatedBases.push_back(InstantiatedBase);
568 else
569 Invalid = true;
570 }
571
Douglas Gregord9572a12009-03-10 18:52:44 +0000572 if (!Invalid &&
Jay Foad9e6bef42009-05-21 09:52:38 +0000573 AttachBaseSpecifiers(Instantiation, InstantiatedBases.data(),
Douglas Gregored3a3982009-03-03 04:44:36 +0000574 InstantiatedBases.size()))
575 Invalid = true;
576
577 return Invalid;
578}
579
John McCalld64b41e2009-08-20 01:44:21 +0000580/// \brief Force a template's pattern class to be instantiated.
581///
582/// \returns true if an error occurred
583bool Sema::InstantiateTemplatePattern(SourceLocation PointOfInstantiation,
584 CXXRecordDecl *Pattern) {
585 if (Pattern->getDefinition(Context)) return false;
586
587 ClassTemplateDecl *PatternTemp = Pattern->getDescribedClassTemplate();
588 if (!PatternTemp) return false;
589
590 // Check whether this template is a lazy instantiation of a
591 // dependent member template, e.g. Inner<U> in
592 // Outer<int>::Inner<U>.
593 ClassTemplateDecl *PatternPatternTemp
594 = PatternTemp->getInstantiatedFromMemberTemplate();
595 if (!PatternPatternTemp) return false;
596
597 ClassTemplateSpecializationDecl *Spec = 0;
598 for (DeclContext *Parent = Pattern->getDeclContext();
599 Parent && !Spec; Parent = Parent->getParent())
600 Spec = dyn_cast<ClassTemplateSpecializationDecl>(Parent);
601 assert(Spec && "Not a member of a class template specialization?");
602
603 // TODO: the error message from a nested failure here is probably
604 // not ideal.
605 return InstantiateClass(PointOfInstantiation,
606 Pattern,
607 PatternPatternTemp->getTemplatedDecl(),
608 Spec->getTemplateArgs(),
609 /* ExplicitInstantiation = */ false);
610}
611
Douglas Gregorcc887972009-03-25 21:17:03 +0000612/// \brief Instantiate the definition of a class from a given pattern.
613///
614/// \param PointOfInstantiation The point of instantiation within the
615/// source code.
616///
617/// \param Instantiation is the declaration whose definition is being
618/// instantiated. This will be either a class template specialization
619/// or a member class of a class template specialization.
620///
621/// \param Pattern is the pattern from which the instantiation
622/// occurs. This will be either the declaration of a class template or
623/// the declaration of a member class of a class template.
624///
625/// \param TemplateArgs The template arguments to be substituted into
626/// the pattern.
627///
Douglas Gregorb35c7992009-08-24 15:23:48 +0000628/// \param ExplicitInstantiation whether this is an explicit instantiation
629/// (otherwise, it is an implicit instantiation).
630///
631/// \param Complain whether to complain if the class cannot be instantiated due
632/// to the lack of a definition.
633///
Douglas Gregorcc887972009-03-25 21:17:03 +0000634/// \returns true if an error occurred, false otherwise.
635bool
636Sema::InstantiateClass(SourceLocation PointOfInstantiation,
637 CXXRecordDecl *Instantiation, CXXRecordDecl *Pattern,
Douglas Gregorfd79ac62009-05-13 00:25:59 +0000638 const TemplateArgumentList &TemplateArgs,
Douglas Gregorb35c7992009-08-24 15:23:48 +0000639 bool ExplicitInstantiation,
640 bool Complain) {
Douglas Gregorcc887972009-03-25 21:17:03 +0000641 bool Invalid = false;
John McCalld64b41e2009-08-20 01:44:21 +0000642
643 // Lazily instantiate member templates here.
644 if (InstantiateTemplatePattern(PointOfInstantiation, Pattern))
645 return true;
Douglas Gregorcc887972009-03-25 21:17:03 +0000646
647 CXXRecordDecl *PatternDef
648 = cast_or_null<CXXRecordDecl>(Pattern->getDefinition(Context));
649 if (!PatternDef) {
Douglas Gregorb35c7992009-08-24 15:23:48 +0000650 if (!Complain) {
651 // Say nothing
652 } else if (Pattern == Instantiation->getInstantiatedFromMemberClass()) {
Douglas Gregorcc887972009-03-25 21:17:03 +0000653 Diag(PointOfInstantiation,
654 diag::err_implicit_instantiate_member_undefined)
655 << Context.getTypeDeclType(Instantiation);
656 Diag(Pattern->getLocation(), diag::note_member_of_template_here);
657 } else {
Douglas Gregorfd79ac62009-05-13 00:25:59 +0000658 Diag(PointOfInstantiation, diag::err_template_instantiate_undefined)
659 << ExplicitInstantiation
Douglas Gregorcc887972009-03-25 21:17:03 +0000660 << Context.getTypeDeclType(Instantiation);
661 Diag(Pattern->getLocation(), diag::note_template_decl_here);
662 }
663 return true;
664 }
665 Pattern = PatternDef;
666
Douglas Gregor42c48522009-03-25 21:23:52 +0000667 InstantiatingTemplate Inst(*this, PointOfInstantiation, Instantiation);
Douglas Gregorcc887972009-03-25 21:17:03 +0000668 if (Inst)
669 return true;
670
671 // Enter the scope of this instantiation. We don't use
672 // PushDeclContext because we don't have a scope.
673 DeclContext *PreviousContext = CurContext;
674 CurContext = Instantiation;
675
676 // Start the definition of this instantiation.
677 Instantiation->startDefinition();
678
John McCall0ba26ee2009-08-25 22:02:44 +0000679 // Do substitution on the base class specifiers.
680 if (SubstBaseSpecifiers(Instantiation, Pattern, TemplateArgs))
Douglas Gregorcc887972009-03-25 21:17:03 +0000681 Invalid = true;
682
Douglas Gregor4ac20ef2009-05-29 18:27:38 +0000683 llvm::SmallVector<DeclPtrTy, 4> Fields;
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000684 for (RecordDecl::decl_iterator Member = Pattern->decls_begin(),
685 MemberEnd = Pattern->decls_end();
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000686 Member != MemberEnd; ++Member) {
John McCall0ba26ee2009-08-25 22:02:44 +0000687 Decl *NewMember = SubstDecl(*Member, Instantiation, TemplateArgs);
Douglas Gregorcc887972009-03-25 21:17:03 +0000688 if (NewMember) {
689 if (NewMember->isInvalidDecl())
690 Invalid = true;
691 else if (FieldDecl *Field = dyn_cast<FieldDecl>(NewMember))
Chris Lattner5261d0c2009-03-28 19:18:32 +0000692 Fields.push_back(DeclPtrTy::make(Field));
Douglas Gregorcc887972009-03-25 21:17:03 +0000693 } else {
694 // FIXME: Eventually, a NULL return will mean that one of the
Mike Stumpe127ae32009-05-16 07:39:55 +0000695 // instantiations was a semantic disaster, and we'll want to set Invalid =
696 // true. For now, we expect to skip some members that we can't yet handle.
Douglas Gregorcc887972009-03-25 21:17:03 +0000697 }
698 }
699
700 // Finish checking fields.
Chris Lattner5261d0c2009-03-28 19:18:32 +0000701 ActOnFields(0, Instantiation->getLocation(), DeclPtrTy::make(Instantiation),
Jay Foad9e6bef42009-05-21 09:52:38 +0000702 Fields.data(), Fields.size(), SourceLocation(), SourceLocation(),
Douglas Gregorcc887972009-03-25 21:17:03 +0000703 0);
704
705 // Add any implicitly-declared members that we might need.
706 AddImplicitlyDeclaredMembersToClass(Instantiation);
707
708 // Exit the scope of this instantiation.
709 CurContext = PreviousContext;
710
Douglas Gregordc18e892009-05-26 20:50:29 +0000711 if (!Invalid)
712 Consumer.HandleTagDeclDefinition(Instantiation);
713
Douglas Gregordf9f5d12009-05-13 20:28:22 +0000714 // If this is an explicit instantiation, instantiate our members, too.
Douglas Gregorb12249d2009-05-18 17:01:57 +0000715 if (!Invalid && ExplicitInstantiation) {
716 Inst.Clear();
Douglas Gregordf9f5d12009-05-13 20:28:22 +0000717 InstantiateClassMembers(PointOfInstantiation, Instantiation, TemplateArgs);
Douglas Gregorb12249d2009-05-18 17:01:57 +0000718 }
Douglas Gregordf9f5d12009-05-13 20:28:22 +0000719
Douglas Gregorcc887972009-03-25 21:17:03 +0000720 return Invalid;
721}
722
Douglas Gregored3a3982009-03-03 04:44:36 +0000723bool
724Sema::InstantiateClassTemplateSpecialization(
725 ClassTemplateSpecializationDecl *ClassTemplateSpec,
Douglas Gregorb35c7992009-08-24 15:23:48 +0000726 bool ExplicitInstantiation,
727 bool Complain) {
Douglas Gregored3a3982009-03-03 04:44:36 +0000728 // Perform the actual instantiation on the canonical declaration.
729 ClassTemplateSpec = cast<ClassTemplateSpecializationDecl>(
Argiris Kirtzidis17c7cab2009-07-18 00:34:25 +0000730 ClassTemplateSpec->getCanonicalDecl());
Douglas Gregored3a3982009-03-03 04:44:36 +0000731
732 // We can only instantiate something that hasn't already been
733 // instantiated or specialized. Fail without any diagnostics: our
734 // caller will provide an error message.
735 if (ClassTemplateSpec->getSpecializationKind() != TSK_Undeclared)
736 return true;
737
Douglas Gregored3a3982009-03-03 04:44:36 +0000738 ClassTemplateDecl *Template = ClassTemplateSpec->getSpecializedTemplate();
Douglas Gregorcc887972009-03-25 21:17:03 +0000739 CXXRecordDecl *Pattern = Template->getTemplatedDecl();
Douglas Gregor58944ac2009-05-31 09:31:02 +0000740 const TemplateArgumentList *TemplateArgs
741 = &ClassTemplateSpec->getTemplateArgs();
742
Douglas Gregor21530c52009-06-12 22:31:52 +0000743 // C++ [temp.class.spec.match]p1:
744 // When a class template is used in a context that requires an
745 // instantiation of the class, it is necessary to determine
746 // whether the instantiation is to be generated using the primary
747 // template or one of the partial specializations. This is done by
748 // matching the template arguments of the class template
749 // specialization with the template argument lists of the partial
750 // specializations.
Douglas Gregorab9f71a2009-06-05 00:53:49 +0000751 typedef std::pair<ClassTemplatePartialSpecializationDecl *,
752 TemplateArgumentList *> MatchResult;
753 llvm::SmallVector<MatchResult, 4> Matched;
Douglas Gregor58944ac2009-05-31 09:31:02 +0000754 for (llvm::FoldingSet<ClassTemplatePartialSpecializationDecl>::iterator
755 Partial = Template->getPartialSpecializations().begin(),
756 PartialEnd = Template->getPartialSpecializations().end();
757 Partial != PartialEnd;
758 ++Partial) {
Douglas Gregor623e2e02009-06-12 18:26:56 +0000759 TemplateDeductionInfo Info(Context);
760 if (TemplateDeductionResult Result
Douglas Gregorab9f71a2009-06-05 00:53:49 +0000761 = DeduceTemplateArguments(&*Partial,
Douglas Gregor623e2e02009-06-12 18:26:56 +0000762 ClassTemplateSpec->getTemplateArgs(),
763 Info)) {
764 // FIXME: Store the failed-deduction information for use in
765 // diagnostics, later.
766 (void)Result;
767 } else {
768 Matched.push_back(std::make_pair(&*Partial, Info.take()));
769 }
Douglas Gregor58944ac2009-05-31 09:31:02 +0000770 }
771
772 if (Matched.size() == 1) {
Douglas Gregor21530c52009-06-12 22:31:52 +0000773 // -- If exactly one matching specialization is found, the
774 // instantiation is generated from that specialization.
Douglas Gregorab9f71a2009-06-05 00:53:49 +0000775 Pattern = Matched[0].first;
776 TemplateArgs = Matched[0].second;
Douglas Gregor7b0b83f2009-08-02 23:24:31 +0000777 ClassTemplateSpec->setInstantiationOf(Matched[0].first, Matched[0].second);
Douglas Gregor58944ac2009-05-31 09:31:02 +0000778 } else if (Matched.size() > 1) {
Douglas Gregor21530c52009-06-12 22:31:52 +0000779 // -- If more than one matching specialization is found, the
780 // partial order rules (14.5.4.2) are used to determine
781 // whether one of the specializations is more specialized
782 // than the others. If none of the specializations is more
783 // specialized than all of the other matching
784 // specializations, then the use of the class template is
785 // ambiguous and the program is ill-formed.
Douglas Gregor58944ac2009-05-31 09:31:02 +0000786 // FIXME: Implement partial ordering of class template partial
787 // specializations.
788 Diag(ClassTemplateSpec->getLocation(),
789 diag::unsup_template_partial_spec_ordering);
Douglas Gregor21530c52009-06-12 22:31:52 +0000790 } else {
791 // -- If no matches are found, the instantiation is generated
792 // from the primary template.
793
794 // Since we initialized the pattern and template arguments from
795 // the primary template, there is nothing more we need to do here.
Douglas Gregor58944ac2009-05-31 09:31:02 +0000796 }
Douglas Gregored3a3982009-03-03 04:44:36 +0000797
798 // Note that this is an instantiation.
799 ClassTemplateSpec->setSpecializationKind(
800 ExplicitInstantiation? TSK_ExplicitInstantiation
801 : TSK_ImplicitInstantiation);
802
Douglas Gregorab9f71a2009-06-05 00:53:49 +0000803 bool Result = InstantiateClass(ClassTemplateSpec->getLocation(),
804 ClassTemplateSpec, Pattern, *TemplateArgs,
Douglas Gregorb35c7992009-08-24 15:23:48 +0000805 ExplicitInstantiation,
806 Complain);
Douglas Gregorab9f71a2009-06-05 00:53:49 +0000807
808 for (unsigned I = 0, N = Matched.size(); I != N; ++I) {
809 // FIXME: Implement TemplateArgumentList::Destroy!
810 // if (Matched[I].first != Pattern)
811 // Matched[I].second->Destroy(Context);
812 }
813
814 return Result;
Douglas Gregored3a3982009-03-03 04:44:36 +0000815}
Douglas Gregor47bde7c2009-03-19 17:26:29 +0000816
John McCall0ba26ee2009-08-25 22:02:44 +0000817/// \brief Instantiates the definitions of all of the member
818/// of the given class, which is an instantiation of a class template
819/// or a member class of a template.
Douglas Gregordf9f5d12009-05-13 20:28:22 +0000820void
821Sema::InstantiateClassMembers(SourceLocation PointOfInstantiation,
John McCall0ba26ee2009-08-25 22:02:44 +0000822 CXXRecordDecl *Instantiation,
823 const TemplateArgumentList &TemplateArgs) {
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000824 for (DeclContext::decl_iterator D = Instantiation->decls_begin(),
825 DEnd = Instantiation->decls_end();
Douglas Gregordf9f5d12009-05-13 20:28:22 +0000826 D != DEnd; ++D) {
827 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(*D)) {
Argiris Kirtzidisccb9efe2009-06-30 02:35:26 +0000828 if (!Function->getBody())
Douglas Gregorb12249d2009-05-18 17:01:57 +0000829 InstantiateFunctionDefinition(PointOfInstantiation, Function);
Douglas Gregordf9f5d12009-05-13 20:28:22 +0000830 } else if (VarDecl *Var = dyn_cast<VarDecl>(*D)) {
Douglas Gregor181fe792009-07-24 20:34:43 +0000831 if (Var->isStaticDataMember())
832 InstantiateStaticDataMemberDefinition(PointOfInstantiation, Var);
Douglas Gregordf9f5d12009-05-13 20:28:22 +0000833 } else if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(*D)) {
834 if (!Record->isInjectedClassName() && !Record->getDefinition(Context)) {
835 assert(Record->getInstantiatedFromMemberClass() &&
836 "Missing instantiated-from-template information");
Douglas Gregorb12249d2009-05-18 17:01:57 +0000837 InstantiateClass(PointOfInstantiation, Record,
Douglas Gregordf9f5d12009-05-13 20:28:22 +0000838 Record->getInstantiatedFromMemberClass(),
839 TemplateArgs, true);
840 }
841 }
842 }
843}
844
845/// \brief Instantiate the definitions of all of the members of the
846/// given class template specialization, which was named as part of an
847/// explicit instantiation.
848void Sema::InstantiateClassTemplateSpecializationMembers(
849 SourceLocation PointOfInstantiation,
850 ClassTemplateSpecializationDecl *ClassTemplateSpec) {
851 // C++0x [temp.explicit]p7:
852 // An explicit instantiation that names a class template
853 // specialization is an explicit instantion of the same kind
854 // (declaration or definition) of each of its members (not
855 // including members inherited from base classes) that has not
856 // been previously explicitly specialized in the translation unit
857 // containing the explicit instantiation, except as described
858 // below.
859 InstantiateClassMembers(PointOfInstantiation, ClassTemplateSpec,
860 ClassTemplateSpec->getTemplateArgs());
861}
862
Douglas Gregor23a44be2009-08-20 07:17:43 +0000863Sema::OwningStmtResult
John McCall0ba26ee2009-08-25 22:02:44 +0000864Sema::SubstStmt(Stmt *S, const TemplateArgumentList &TemplateArgs) {
Douglas Gregor23a44be2009-08-20 07:17:43 +0000865 if (!S)
866 return Owned(S);
867
868 TemplateInstantiator Instantiator(*this, TemplateArgs,
869 SourceLocation(),
870 DeclarationName());
871 return Instantiator.TransformStmt(S);
872}
873
Douglas Gregor9d879762009-08-11 05:31:07 +0000874Sema::OwningExprResult
John McCall0ba26ee2009-08-25 22:02:44 +0000875Sema::SubstExpr(Expr *E, const TemplateArgumentList &TemplateArgs) {
Douglas Gregor9d879762009-08-11 05:31:07 +0000876 if (!E)
877 return Owned(E);
878
879 TemplateInstantiator Instantiator(*this, TemplateArgs,
880 SourceLocation(),
881 DeclarationName());
882 return Instantiator.TransformExpr(E);
883}
884
John McCall0ba26ee2009-08-25 22:02:44 +0000885/// \brief Do template substitution on a nested-name-specifier.
Douglas Gregor1e589cc2009-03-26 23:50:42 +0000886NestedNameSpecifier *
John McCall0ba26ee2009-08-25 22:02:44 +0000887Sema::SubstNestedNameSpecifier(NestedNameSpecifier *NNS,
Douglas Gregor1e589cc2009-03-26 23:50:42 +0000888 SourceRange Range,
Douglas Gregorf9e7d3d2009-05-11 23:53:27 +0000889 const TemplateArgumentList &TemplateArgs) {
Douglas Gregor12431cb2009-08-06 05:28:30 +0000890 TemplateInstantiator Instantiator(*this, TemplateArgs, Range.getBegin(),
891 DeclarationName());
892 return Instantiator.TransformNestedNameSpecifier(NNS, Range);
Douglas Gregor47bde7c2009-03-19 17:26:29 +0000893}
Douglas Gregor15a92852009-03-31 18:38:02 +0000894
895TemplateName
John McCall0ba26ee2009-08-25 22:02:44 +0000896Sema::SubstTemplateName(TemplateName Name, SourceLocation Loc,
897 const TemplateArgumentList &TemplateArgs) {
Douglas Gregor214d0462009-08-06 06:41:21 +0000898 TemplateInstantiator Instantiator(*this, TemplateArgs, Loc,
899 DeclarationName());
900 return Instantiator.TransformTemplateName(Name);
Douglas Gregor15a92852009-03-31 18:38:02 +0000901}
Douglas Gregorb320b9e2009-06-11 00:06:24 +0000902
John McCall0ba26ee2009-08-25 22:02:44 +0000903TemplateArgument Sema::Subst(TemplateArgument Arg,
904 const TemplateArgumentList &TemplateArgs) {
Douglas Gregor2999faa2009-08-04 22:27:00 +0000905 TemplateInstantiator Instantiator(*this, TemplateArgs, SourceLocation(),
906 DeclarationName());
907 return Instantiator.TransformTemplateArgument(Arg);
Douglas Gregorb320b9e2009-06-11 00:06:24 +0000908}