blob: 6c2dc77b4cce0045297b2f097b0bb75bc4bb8cfb [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 Gregordc18e892009-05-26 20:50:29 +000014#include "clang/AST/ASTConsumer.h"
Douglas Gregor74296542009-02-27 19:31:52 +000015#include "clang/AST/ASTContext.h"
16#include "clang/AST/Expr.h"
Douglas Gregor74296542009-02-27 19:31:52 +000017#include "clang/AST/DeclTemplate.h"
18#include "clang/Parse/DeclSpec.h"
19#include "clang/Basic/LangOptions.h"
Douglas Gregorf57dcd02009-02-28 00:25:32 +000020#include "llvm/Support/Compiler.h"
Douglas Gregor74296542009-02-27 19:31:52 +000021
22using namespace clang;
23
Douglas Gregorfee85d62009-03-10 18:03:33 +000024//===----------------------------------------------------------------------===/
25// Template Instantiation Support
26//===----------------------------------------------------------------------===/
27
Douglas Gregor5f62c5e2009-05-14 23:26:13 +000028/// \brief Retrieve the template argument list that should be used to
29/// instantiate the given declaration.
30const TemplateArgumentList &
31Sema::getTemplateInstantiationArgs(NamedDecl *D) {
Douglas Gregor6f5e0542009-06-26 00:10:03 +000032 // Template arguments for a class template specialization.
Douglas Gregor5f62c5e2009-05-14 23:26:13 +000033 if (ClassTemplateSpecializationDecl *Spec
34 = dyn_cast<ClassTemplateSpecializationDecl>(D))
35 return Spec->getTemplateArgs();
36
Douglas Gregor6f5e0542009-06-26 00:10:03 +000037 // Template arguments for a function template specialization.
38 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D))
39 if (const TemplateArgumentList *TemplateArgs
40 = Function->getTemplateSpecializationArgs())
41 return *TemplateArgs;
42
43 // Template arguments for a member of a class template specialization.
Douglas Gregor5f62c5e2009-05-14 23:26:13 +000044 DeclContext *EnclosingTemplateCtx = D->getDeclContext();
45 while (!isa<ClassTemplateSpecializationDecl>(EnclosingTemplateCtx)) {
46 assert(!EnclosingTemplateCtx->isFileContext() &&
47 "Tried to get the instantiation arguments of a non-template");
48 EnclosingTemplateCtx = EnclosingTemplateCtx->getParent();
49 }
50
51 ClassTemplateSpecializationDecl *EnclosingTemplate
52 = cast<ClassTemplateSpecializationDecl>(EnclosingTemplateCtx);
53 return EnclosingTemplate->getTemplateArgs();
54}
55
Douglas Gregor375733c2009-03-10 00:06:19 +000056Sema::InstantiatingTemplate::
57InstantiatingTemplate(Sema &SemaRef, SourceLocation PointOfInstantiation,
Douglas Gregorb12249d2009-05-18 17:01:57 +000058 Decl *Entity,
Douglas Gregor375733c2009-03-10 00:06:19 +000059 SourceRange InstantiationRange)
60 : SemaRef(SemaRef) {
Douglas Gregor56d25a72009-03-10 20:44:00 +000061
62 Invalid = CheckInstantiationDepth(PointOfInstantiation,
63 InstantiationRange);
64 if (!Invalid) {
Douglas Gregor375733c2009-03-10 00:06:19 +000065 ActiveTemplateInstantiation Inst;
Douglas Gregor56d25a72009-03-10 20:44:00 +000066 Inst.Kind = ActiveTemplateInstantiation::TemplateInstantiation;
Douglas Gregor375733c2009-03-10 00:06:19 +000067 Inst.PointOfInstantiation = PointOfInstantiation;
Douglas Gregor56d25a72009-03-10 20:44:00 +000068 Inst.Entity = reinterpret_cast<uintptr_t>(Entity);
Douglas Gregor95ba1282009-03-12 18:36:18 +000069 Inst.TemplateArgs = 0;
70 Inst.NumTemplateArgs = 0;
Douglas Gregor56d25a72009-03-10 20:44:00 +000071 Inst.InstantiationRange = InstantiationRange;
72 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
73 Invalid = false;
74 }
75}
76
77Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef,
78 SourceLocation PointOfInstantiation,
79 TemplateDecl *Template,
80 const TemplateArgument *TemplateArgs,
81 unsigned NumTemplateArgs,
82 SourceRange InstantiationRange)
83 : SemaRef(SemaRef) {
84
85 Invalid = CheckInstantiationDepth(PointOfInstantiation,
86 InstantiationRange);
87 if (!Invalid) {
88 ActiveTemplateInstantiation Inst;
89 Inst.Kind
90 = ActiveTemplateInstantiation::DefaultTemplateArgumentInstantiation;
91 Inst.PointOfInstantiation = PointOfInstantiation;
92 Inst.Entity = reinterpret_cast<uintptr_t>(Template);
93 Inst.TemplateArgs = TemplateArgs;
94 Inst.NumTemplateArgs = NumTemplateArgs;
Douglas Gregor375733c2009-03-10 00:06:19 +000095 Inst.InstantiationRange = InstantiationRange;
96 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
97 Invalid = false;
98 }
99}
100
Douglas Gregorc5e01af2009-06-10 23:47:09 +0000101Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef,
102 SourceLocation PointOfInstantiation,
Douglas Gregor4c8b2b32009-07-01 22:01:06 +0000103 FunctionTemplateDecl *FunctionTemplate,
104 const TemplateArgument *TemplateArgs,
105 unsigned NumTemplateArgs,
106 ActiveTemplateInstantiation::InstantiationKind Kind,
107 SourceRange InstantiationRange)
108: SemaRef(SemaRef) {
109
110 Invalid = CheckInstantiationDepth(PointOfInstantiation,
111 InstantiationRange);
112 if (!Invalid) {
113 ActiveTemplateInstantiation Inst;
114 Inst.Kind = Kind;
115 Inst.PointOfInstantiation = PointOfInstantiation;
116 Inst.Entity = reinterpret_cast<uintptr_t>(FunctionTemplate);
117 Inst.TemplateArgs = TemplateArgs;
118 Inst.NumTemplateArgs = NumTemplateArgs;
119 Inst.InstantiationRange = InstantiationRange;
120 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
121 Invalid = false;
122 }
123}
124
125Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef,
126 SourceLocation PointOfInstantiation,
Douglas Gregorc5e01af2009-06-10 23:47:09 +0000127 ClassTemplatePartialSpecializationDecl *PartialSpec,
128 const TemplateArgument *TemplateArgs,
129 unsigned NumTemplateArgs,
130 SourceRange InstantiationRange)
131 : SemaRef(SemaRef) {
132
133 Invalid = CheckInstantiationDepth(PointOfInstantiation,
134 InstantiationRange);
135 if (!Invalid) {
136 ActiveTemplateInstantiation Inst;
137 Inst.Kind
Douglas Gregor4c8b2b32009-07-01 22:01:06 +0000138 = ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution;
Douglas Gregorc5e01af2009-06-10 23:47:09 +0000139 Inst.PointOfInstantiation = PointOfInstantiation;
140 Inst.Entity = reinterpret_cast<uintptr_t>(PartialSpec);
141 Inst.TemplateArgs = TemplateArgs;
142 Inst.NumTemplateArgs = NumTemplateArgs;
143 Inst.InstantiationRange = InstantiationRange;
144 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
145 Invalid = false;
146 }
147}
148
Douglas Gregorb12249d2009-05-18 17:01:57 +0000149void Sema::InstantiatingTemplate::Clear() {
150 if (!Invalid) {
Douglas Gregor375733c2009-03-10 00:06:19 +0000151 SemaRef.ActiveTemplateInstantiations.pop_back();
Douglas Gregorb12249d2009-05-18 17:01:57 +0000152 Invalid = true;
153 }
Douglas Gregor375733c2009-03-10 00:06:19 +0000154}
155
Douglas Gregor56d25a72009-03-10 20:44:00 +0000156bool Sema::InstantiatingTemplate::CheckInstantiationDepth(
157 SourceLocation PointOfInstantiation,
158 SourceRange InstantiationRange) {
159 if (SemaRef.ActiveTemplateInstantiations.size()
160 <= SemaRef.getLangOptions().InstantiationDepth)
161 return false;
162
163 SemaRef.Diag(PointOfInstantiation,
164 diag::err_template_recursion_depth_exceeded)
165 << SemaRef.getLangOptions().InstantiationDepth
166 << InstantiationRange;
167 SemaRef.Diag(PointOfInstantiation, diag::note_template_recursion_depth)
168 << SemaRef.getLangOptions().InstantiationDepth;
169 return true;
170}
171
Douglas Gregorfee85d62009-03-10 18:03:33 +0000172/// \brief Prints the current instantiation stack through a series of
173/// notes.
174void Sema::PrintInstantiationStack() {
Douglas Gregor4c8b2b32009-07-01 22:01:06 +0000175 // FIXME: In all of these cases, we need to show the template arguments
Douglas Gregorfee85d62009-03-10 18:03:33 +0000176 for (llvm::SmallVector<ActiveTemplateInstantiation, 16>::reverse_iterator
177 Active = ActiveTemplateInstantiations.rbegin(),
178 ActiveEnd = ActiveTemplateInstantiations.rend();
179 Active != ActiveEnd;
180 ++Active) {
Douglas Gregor56d25a72009-03-10 20:44:00 +0000181 switch (Active->Kind) {
182 case ActiveTemplateInstantiation::TemplateInstantiation: {
Douglas Gregorb12249d2009-05-18 17:01:57 +0000183 Decl *D = reinterpret_cast<Decl *>(Active->Entity);
184 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) {
185 unsigned DiagID = diag::note_template_member_class_here;
186 if (isa<ClassTemplateSpecializationDecl>(Record))
187 DiagID = diag::note_template_class_instantiation_here;
188 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
189 DiagID)
190 << Context.getTypeDeclType(Record)
191 << Active->InstantiationRange;
192 } else {
193 FunctionDecl *Function = 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;
203 }
Douglas Gregor56d25a72009-03-10 20:44:00 +0000204 break;
205 }
206
207 case ActiveTemplateInstantiation::DefaultTemplateArgumentInstantiation: {
208 TemplateDecl *Template = cast<TemplateDecl>((Decl *)Active->Entity);
209 std::string TemplateArgsStr
Douglas Gregordd13e842009-03-30 22:58:21 +0000210 = TemplateSpecializationType::PrintTemplateArgumentList(
Douglas Gregor4c8b2b32009-07-01 22:01:06 +0000211 Active->TemplateArgs,
Douglas Gregor3bf3bbc2009-05-29 20:38:28 +0000212 Active->NumTemplateArgs,
213 Context.PrintingPolicy);
Douglas Gregor56d25a72009-03-10 20:44:00 +0000214 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
215 diag::note_default_arg_instantiation_here)
216 << (Template->getNameAsString() + TemplateArgsStr)
217 << Active->InstantiationRange;
218 break;
219 }
Douglas Gregorc5e01af2009-06-10 23:47:09 +0000220
Douglas Gregor4c8b2b32009-07-01 22:01:06 +0000221 case ActiveTemplateInstantiation::ExplicitTemplateArgumentSubstitution: {
222 FunctionTemplateDecl *FnTmpl
223 = cast<FunctionTemplateDecl>((Decl *)Active->Entity);
Douglas Gregorc5e01af2009-06-10 23:47:09 +0000224 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
Douglas Gregor4c8b2b32009-07-01 22:01:06 +0000225 diag::note_explicit_template_arg_substitution_here)
226 << FnTmpl << Active->InstantiationRange;
Douglas Gregorc5e01af2009-06-10 23:47:09 +0000227 break;
228 }
Douglas Gregor4c8b2b32009-07-01 22:01:06 +0000229
230 case ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution:
231 if (ClassTemplatePartialSpecializationDecl *PartialSpec
232 = dyn_cast<ClassTemplatePartialSpecializationDecl>(
233 (Decl *)Active->Entity)) {
234 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
235 diag::note_partial_spec_deduct_instantiation_here)
236 << Context.getTypeDeclType(PartialSpec)
237 << Active->InstantiationRange;
238 } else {
239 FunctionTemplateDecl *FnTmpl
240 = cast<FunctionTemplateDecl>((Decl *)Active->Entity);
241 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
242 diag::note_function_template_deduction_instantiation_here)
243 << FnTmpl << Active->InstantiationRange;
244 }
245 break;
Douglas Gregorc5e01af2009-06-10 23:47:09 +0000246
Douglas Gregor56d25a72009-03-10 20:44:00 +0000247 }
Douglas Gregorfee85d62009-03-10 18:03:33 +0000248 }
249}
250
Douglas Gregor95d6c952009-06-14 07:33:30 +0000251bool Sema::isSFINAEContext() const {
252 using llvm::SmallVector;
253 for (SmallVector<ActiveTemplateInstantiation, 16>::const_reverse_iterator
254 Active = ActiveTemplateInstantiations.rbegin(),
255 ActiveEnd = ActiveTemplateInstantiations.rend();
256 Active != ActiveEnd;
257 ++Active) {
258
259 switch(Active->Kind) {
Douglas Gregor4c8b2b32009-07-01 22:01:06 +0000260 case ActiveTemplateInstantiation::TemplateInstantiation:
261 // This is a template instantiation, so there is no SFINAE.
262 return false;
263
Douglas Gregor95d6c952009-06-14 07:33:30 +0000264 case ActiveTemplateInstantiation::DefaultTemplateArgumentInstantiation:
265 // A default template argument instantiation may or may not be a
266 // SFINAE context; look further up the stack.
267 break;
Douglas Gregor4c8b2b32009-07-01 22:01:06 +0000268
269 case ActiveTemplateInstantiation::ExplicitTemplateArgumentSubstitution:
270 case ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution:
271 // We're either substitution explicitly-specified template arguments
272 // or deduced template arguments, so SFINAE applies.
273 return true;
Douglas Gregor95d6c952009-06-14 07:33:30 +0000274 }
275 }
276
277 return false;
278}
279
Douglas Gregor74296542009-02-27 19:31:52 +0000280//===----------------------------------------------------------------------===/
281// Template Instantiation for Types
282//===----------------------------------------------------------------------===/
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000283namespace {
284 class VISIBILITY_HIDDEN TemplateTypeInstantiator {
285 Sema &SemaRef;
Douglas Gregorf9e7d3d2009-05-11 23:53:27 +0000286 const TemplateArgumentList &TemplateArgs;
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000287 SourceLocation Loc;
288 DeclarationName Entity;
Douglas Gregor74296542009-02-27 19:31:52 +0000289
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000290 public:
291 TemplateTypeInstantiator(Sema &SemaRef,
Douglas Gregorf9e7d3d2009-05-11 23:53:27 +0000292 const TemplateArgumentList &TemplateArgs,
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000293 SourceLocation Loc,
294 DeclarationName Entity)
295 : SemaRef(SemaRef), TemplateArgs(TemplateArgs),
Douglas Gregorf9e7d3d2009-05-11 23:53:27 +0000296 Loc(Loc), Entity(Entity) { }
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000297
298 QualType operator()(QualType T) const { return Instantiate(T); }
299
300 QualType Instantiate(QualType T) const;
301
302 // Declare instantiate functions for each type.
303#define TYPE(Class, Base) \
Douglas Gregor072ac382009-06-26 21:40:05 +0000304 QualType Instantiate##Class##Type(const Class##Type *T) const;
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000305#define ABSTRACT_TYPE(Class, Base)
306#include "clang/AST/TypeNodes.def"
307 };
308}
309
310QualType
Douglas Gregor072ac382009-06-26 21:40:05 +0000311TemplateTypeInstantiator::InstantiateExtQualType(const ExtQualType *T) const {
Douglas Gregor74296542009-02-27 19:31:52 +0000312 // FIXME: Implement this
313 assert(false && "Cannot instantiate ExtQualType yet");
314 return QualType();
315}
316
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000317QualType
Douglas Gregor072ac382009-06-26 21:40:05 +0000318TemplateTypeInstantiator::InstantiateBuiltinType(const BuiltinType *T) const {
Douglas Gregorc0139ca2009-02-28 01:04:19 +0000319 assert(false && "Builtin types are not dependent and cannot be instantiated");
Douglas Gregor072ac382009-06-26 21:40:05 +0000320 return QualType(T, 0);
Douglas Gregor74296542009-02-27 19:31:52 +0000321}
322
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000323QualType
324TemplateTypeInstantiator::
Douglas Gregor072ac382009-06-26 21:40:05 +0000325InstantiateFixedWidthIntType(const FixedWidthIntType *T) const {
Douglas Gregor74296542009-02-27 19:31:52 +0000326 // FIXME: Implement this
327 assert(false && "Cannot instantiate FixedWidthIntType yet");
328 return QualType();
329}
330
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000331QualType
Douglas Gregor072ac382009-06-26 21:40:05 +0000332TemplateTypeInstantiator::InstantiateComplexType(const ComplexType *T) const {
Douglas Gregor74296542009-02-27 19:31:52 +0000333 // FIXME: Implement this
334 assert(false && "Cannot instantiate ComplexType yet");
335 return QualType();
336}
337
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000338QualType
Douglas Gregor072ac382009-06-26 21:40:05 +0000339TemplateTypeInstantiator::InstantiatePointerType(const PointerType *T) const {
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000340 QualType PointeeType = Instantiate(T->getPointeeType());
341 if (PointeeType.isNull())
342 return QualType();
343
Douglas Gregor072ac382009-06-26 21:40:05 +0000344 return SemaRef.BuildPointerType(PointeeType, 0, Loc, Entity);
Douglas Gregor74296542009-02-27 19:31:52 +0000345}
346
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000347QualType
Douglas Gregor072ac382009-06-26 21:40:05 +0000348TemplateTypeInstantiator::InstantiateBlockPointerType(
349 const BlockPointerType *T) const {
Anders Carlsson9d914d32009-06-12 16:23:10 +0000350 QualType PointeeType = Instantiate(T->getPointeeType());
351 if (PointeeType.isNull())
352 return QualType();
353
Douglas Gregor072ac382009-06-26 21:40:05 +0000354 return SemaRef.BuildBlockPointerType(PointeeType, 0, Loc, Entity);
Douglas Gregor74296542009-02-27 19:31:52 +0000355}
356
Sebastian Redlce6fff02009-03-16 23:22:08 +0000357QualType
358TemplateTypeInstantiator::InstantiateLValueReferenceType(
Douglas Gregor072ac382009-06-26 21:40:05 +0000359 const LValueReferenceType *T) const {
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000360 QualType ReferentType = Instantiate(T->getPointeeType());
361 if (ReferentType.isNull())
362 return QualType();
363
Douglas Gregor072ac382009-06-26 21:40:05 +0000364 return SemaRef.BuildReferenceType(ReferentType, true, 0, Loc, Entity);
Sebastian Redlce6fff02009-03-16 23:22:08 +0000365}
366
367QualType
368TemplateTypeInstantiator::InstantiateRValueReferenceType(
Douglas Gregor072ac382009-06-26 21:40:05 +0000369 const RValueReferenceType *T) const {
Sebastian Redlce6fff02009-03-16 23:22:08 +0000370 QualType ReferentType = Instantiate(T->getPointeeType());
371 if (ReferentType.isNull())
372 return QualType();
373
Douglas Gregor072ac382009-06-26 21:40:05 +0000374 return SemaRef.BuildReferenceType(ReferentType, false, 0, Loc, Entity);
Douglas Gregor74296542009-02-27 19:31:52 +0000375}
376
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000377QualType
378TemplateTypeInstantiator::
Douglas Gregor072ac382009-06-26 21:40:05 +0000379InstantiateMemberPointerType(const MemberPointerType *T) const {
Douglas Gregora39e8302009-06-09 22:17:39 +0000380 QualType PointeeType = Instantiate(T->getPointeeType());
381 if (PointeeType.isNull())
382 return QualType();
383
384 QualType ClassType = Instantiate(QualType(T->getClass(), 0));
385 if (ClassType.isNull())
386 return QualType();
387
Douglas Gregor072ac382009-06-26 21:40:05 +0000388 return SemaRef.BuildMemberPointerType(PointeeType, ClassType, 0, Loc,
Douglas Gregora39e8302009-06-09 22:17:39 +0000389 Entity);
Douglas Gregor74296542009-02-27 19:31:52 +0000390}
391
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000392QualType
393TemplateTypeInstantiator::
Douglas Gregor072ac382009-06-26 21:40:05 +0000394InstantiateConstantArrayType(const ConstantArrayType *T) const {
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000395 QualType ElementType = Instantiate(T->getElementType());
396 if (ElementType.isNull())
397 return ElementType;
398
399 // Build a temporary integer literal to specify the size for
400 // BuildArrayType. Since we have already checked the size as part of
401 // creating the dependent array type in the first place, we know
Douglas Gregor90177912009-05-13 18:28:20 +0000402 // there aren't any errors. However, we do need to determine what
403 // C++ type to give the size expression.
404 llvm::APInt Size = T->getSize();
405 QualType Types[] = {
406 SemaRef.Context.UnsignedCharTy, SemaRef.Context.UnsignedShortTy,
407 SemaRef.Context.UnsignedIntTy, SemaRef.Context.UnsignedLongTy,
408 SemaRef.Context.UnsignedLongLongTy, SemaRef.Context.UnsignedInt128Ty
409 };
410 const unsigned NumTypes = sizeof(Types) / sizeof(QualType);
411 QualType SizeType;
412 for (unsigned I = 0; I != NumTypes; ++I)
413 if (Size.getBitWidth() == SemaRef.Context.getIntWidth(Types[I])) {
414 SizeType = Types[I];
415 break;
416 }
417
418 if (SizeType.isNull())
419 SizeType = SemaRef.Context.getFixedWidthIntType(Size.getBitWidth(), false);
420
421 IntegerLiteral ArraySize(Size, SizeType, Loc);
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000422 return SemaRef.BuildArrayType(ElementType, T->getSizeModifier(),
423 &ArraySize, T->getIndexTypeQualifier(),
424 Loc, Entity);
Douglas Gregor74296542009-02-27 19:31:52 +0000425}
426
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000427QualType
428TemplateTypeInstantiator::
Douglas Gregor072ac382009-06-26 21:40:05 +0000429InstantiateIncompleteArrayType(const IncompleteArrayType *T) const {
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000430 QualType ElementType = Instantiate(T->getElementType());
431 if (ElementType.isNull())
432 return ElementType;
433
434 return SemaRef.BuildArrayType(ElementType, T->getSizeModifier(),
435 0, T->getIndexTypeQualifier(),
436 Loc, Entity);
Douglas Gregor74296542009-02-27 19:31:52 +0000437}
438
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000439QualType
440TemplateTypeInstantiator::
Douglas Gregor072ac382009-06-26 21:40:05 +0000441InstantiateVariableArrayType(const VariableArrayType *T) const {
Douglas Gregor74296542009-02-27 19:31:52 +0000442 // FIXME: Implement this
443 assert(false && "Cannot instantiate VariableArrayType yet");
444 return QualType();
445}
446
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000447QualType
448TemplateTypeInstantiator::
Douglas Gregor072ac382009-06-26 21:40:05 +0000449InstantiateDependentSizedArrayType(const DependentSizedArrayType *T) const {
Anders Carlsson149f2782009-03-15 20:12:13 +0000450 Expr *ArraySize = T->getSizeExpr();
451 assert(ArraySize->isValueDependent() &&
452 "dependent sized array types must have value dependent size expr");
453
454 // Instantiate the element type if needed
455 QualType ElementType = T->getElementType();
456 if (ElementType->isDependentType()) {
457 ElementType = Instantiate(ElementType);
458 if (ElementType.isNull())
459 return QualType();
460 }
461
462 // Instantiate the size expression
Douglas Gregora8b2fbf2009-06-22 20:57:11 +0000463 EnterExpressionEvaluationContext Unevaluated(SemaRef, Action::Unevaluated);
Anders Carlsson149f2782009-03-15 20:12:13 +0000464 Sema::OwningExprResult InstantiatedArraySize =
Douglas Gregorf9e7d3d2009-05-11 23:53:27 +0000465 SemaRef.InstantiateExpr(ArraySize, TemplateArgs);
Anders Carlsson149f2782009-03-15 20:12:13 +0000466 if (InstantiatedArraySize.isInvalid())
467 return QualType();
468
469 return SemaRef.BuildArrayType(ElementType, T->getSizeModifier(),
Anders Carlsson39ecdcf2009-05-01 19:49:17 +0000470 InstantiatedArraySize.takeAs<Expr>(),
Anders Carlsson149f2782009-03-15 20:12:13 +0000471 T->getIndexTypeQualifier(), Loc, Entity);
Douglas Gregor74296542009-02-27 19:31:52 +0000472}
473
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000474QualType
Douglas Gregor2a2e0402009-06-17 21:51:59 +0000475TemplateTypeInstantiator::
Douglas Gregor072ac382009-06-26 21:40:05 +0000476InstantiateDependentSizedExtVectorType(
477 const DependentSizedExtVectorType *T) const {
Douglas Gregor2a2e0402009-06-17 21:51:59 +0000478
Douglas Gregor68a7a6d2009-06-18 18:45:36 +0000479 // Instantiate the element type if needed.
Douglas Gregor2a2e0402009-06-17 21:51:59 +0000480 QualType ElementType = T->getElementType();
481 if (ElementType->isDependentType()) {
482 ElementType = Instantiate(ElementType);
483 if (ElementType.isNull())
484 return QualType();
485 }
486
Douglas Gregora8b2fbf2009-06-22 20:57:11 +0000487 // The expression in a dependent-sized extended vector type is not
488 // potentially evaluated.
489 EnterExpressionEvaluationContext Unevaluated(SemaRef, Action::Unevaluated);
490
Douglas Gregor68a7a6d2009-06-18 18:45:36 +0000491 // Instantiate the size expression.
492 const Expr *SizeExpr = T->getSizeExpr();
Douglas Gregor2a2e0402009-06-17 21:51:59 +0000493 Sema::OwningExprResult InstantiatedArraySize =
Douglas Gregor68a7a6d2009-06-18 18:45:36 +0000494 SemaRef.InstantiateExpr(const_cast<Expr *>(SizeExpr), TemplateArgs);
Douglas Gregor2a2e0402009-06-17 21:51:59 +0000495 if (InstantiatedArraySize.isInvalid())
496 return QualType();
497
498 return SemaRef.BuildExtVectorType(ElementType,
499 SemaRef.Owned(
500 InstantiatedArraySize.takeAs<Expr>()),
501 T->getAttributeLoc());
502}
503
504QualType
Douglas Gregor072ac382009-06-26 21:40:05 +0000505TemplateTypeInstantiator::InstantiateVectorType(const VectorType *T) const {
Douglas Gregor74296542009-02-27 19:31:52 +0000506 // FIXME: Implement this
507 assert(false && "Cannot instantiate VectorType yet");
508 return QualType();
509}
510
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000511QualType
Douglas Gregor072ac382009-06-26 21:40:05 +0000512TemplateTypeInstantiator::InstantiateExtVectorType(
513 const ExtVectorType *T) const {
Douglas Gregor74296542009-02-27 19:31:52 +0000514 // FIXME: Implement this
515 assert(false && "Cannot instantiate ExtVectorType yet");
516 return QualType();
517}
518
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000519QualType
520TemplateTypeInstantiator::
Douglas Gregor072ac382009-06-26 21:40:05 +0000521InstantiateFunctionProtoType(const FunctionProtoType *T) const {
Douglas Gregorc0139ca2009-02-28 01:04:19 +0000522 QualType ResultType = Instantiate(T->getResultType());
523 if (ResultType.isNull())
524 return ResultType;
525
Douglas Gregor4ac20ef2009-05-29 18:27:38 +0000526 llvm::SmallVector<QualType, 4> ParamTypes;
Douglas Gregorc0139ca2009-02-28 01:04:19 +0000527 for (FunctionProtoType::arg_type_iterator Param = T->arg_type_begin(),
528 ParamEnd = T->arg_type_end();
529 Param != ParamEnd; ++Param) {
530 QualType P = Instantiate(*Param);
531 if (P.isNull())
532 return P;
533
534 ParamTypes.push_back(P);
535 }
536
Douglas Gregor8f378a92009-06-11 18:10:32 +0000537 return SemaRef.BuildFunctionType(ResultType, ParamTypes.data(),
Douglas Gregorc0139ca2009-02-28 01:04:19 +0000538 ParamTypes.size(),
539 T->isVariadic(), T->getTypeQuals(),
540 Loc, Entity);
Douglas Gregor74296542009-02-27 19:31:52 +0000541}
542
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000543QualType
544TemplateTypeInstantiator::
Douglas Gregor072ac382009-06-26 21:40:05 +0000545InstantiateFunctionNoProtoType(const FunctionNoProtoType *T) const {
Douglas Gregorc0139ca2009-02-28 01:04:19 +0000546 assert(false && "Functions without prototypes cannot be dependent.");
Douglas Gregor74296542009-02-27 19:31:52 +0000547 return QualType();
548}
549
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000550QualType
Douglas Gregor072ac382009-06-26 21:40:05 +0000551TemplateTypeInstantiator::InstantiateTypedefType(const TypedefType *T) const {
Douglas Gregorf97986d2009-05-27 05:35:12 +0000552 TypedefDecl *Typedef
Douglas Gregor9cbe6cd2009-05-27 17:54:46 +0000553 = cast_or_null<TypedefDecl>(
554 SemaRef.InstantiateCurrentDeclRef(T->getDecl()));
Douglas Gregorf97986d2009-05-27 05:35:12 +0000555 if (!Typedef)
556 return QualType();
557
558 return SemaRef.Context.getTypeDeclType(Typedef);
Douglas Gregor74296542009-02-27 19:31:52 +0000559}
560
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000561QualType
Douglas Gregor072ac382009-06-26 21:40:05 +0000562TemplateTypeInstantiator::InstantiateTypeOfExprType(
563 const TypeOfExprType *T) const {
Douglas Gregora8b2fbf2009-06-22 20:57:11 +0000564 // The expression in a typeof is not potentially evaluated.
565 EnterExpressionEvaluationContext Unevaluated(SemaRef, Action::Unevaluated);
566
Douglas Gregord4f14af2009-05-26 22:09:24 +0000567 Sema::OwningExprResult E
568 = SemaRef.InstantiateExpr(T->getUnderlyingExpr(), TemplateArgs);
569 if (E.isInvalid())
570 return QualType();
571
Anders Carlsson0040e0c2009-06-29 22:58:55 +0000572 return SemaRef.BuildTypeofExprType(E.takeAs<Expr>());
Douglas Gregor74296542009-02-27 19:31:52 +0000573}
574
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000575QualType
Douglas Gregor072ac382009-06-26 21:40:05 +0000576TemplateTypeInstantiator::InstantiateTypeOfType(const TypeOfType *T) const {
Douglas Gregord4f14af2009-05-26 22:09:24 +0000577 QualType Underlying = Instantiate(T->getUnderlyingType());
578 if (Underlying.isNull())
579 return QualType();
580
581 return SemaRef.Context.getTypeOfType(Underlying);
Douglas Gregor74296542009-02-27 19:31:52 +0000582}
583
Anders Carlsson93ab5332009-06-24 19:06:50 +0000584QualType
Douglas Gregor072ac382009-06-26 21:40:05 +0000585TemplateTypeInstantiator::InstantiateDecltypeType(const DecltypeType *T) const {
Anders Carlsson13438132009-06-26 03:02:18 +0000586 // C++0x [dcl.type.simple]p4:
587 // The operand of the decltype specifier is an unevaluated operand.
588 EnterExpressionEvaluationContext Unevaluated(SemaRef,
589 Action::Unevaluated);
590
Anders Carlsson93ab5332009-06-24 19:06:50 +0000591 Sema::OwningExprResult E
592 = SemaRef.InstantiateExpr(T->getUnderlyingExpr(), TemplateArgs);
593
594 if (E.isInvalid())
595 return QualType();
596
Anders Carlsson0040e0c2009-06-29 22:58:55 +0000597 return SemaRef.BuildDecltypeType(E.takeAs<Expr>());
Anders Carlsson93ab5332009-06-24 19:06:50 +0000598}
599
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000600QualType
Douglas Gregor072ac382009-06-26 21:40:05 +0000601TemplateTypeInstantiator::InstantiateRecordType(const RecordType *T) const {
Douglas Gregorf97986d2009-05-27 05:35:12 +0000602 RecordDecl *Record
Douglas Gregor9cbe6cd2009-05-27 17:54:46 +0000603 = cast_or_null<RecordDecl>(SemaRef.InstantiateCurrentDeclRef(T->getDecl()));
Douglas Gregorf97986d2009-05-27 05:35:12 +0000604 if (!Record)
605 return QualType();
606
607 return SemaRef.Context.getTypeDeclType(Record);
Douglas Gregor74296542009-02-27 19:31:52 +0000608}
609
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000610QualType
Douglas Gregor072ac382009-06-26 21:40:05 +0000611TemplateTypeInstantiator::InstantiateEnumType(const EnumType *T) const {
Douglas Gregorf97986d2009-05-27 05:35:12 +0000612 EnumDecl *Enum
Douglas Gregor9cbe6cd2009-05-27 17:54:46 +0000613 = cast_or_null<EnumDecl>(SemaRef.InstantiateCurrentDeclRef(T->getDecl()));
Douglas Gregorf97986d2009-05-27 05:35:12 +0000614 if (!Enum)
615 return QualType();
616
617 return SemaRef.Context.getTypeDeclType(Enum);
Douglas Gregor74296542009-02-27 19:31:52 +0000618}
619
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000620QualType
621TemplateTypeInstantiator::
Douglas Gregor072ac382009-06-26 21:40:05 +0000622InstantiateTemplateTypeParmType(const TemplateTypeParmType *T) const {
Douglas Gregor74296542009-02-27 19:31:52 +0000623 if (T->getDepth() == 0) {
624 // Replace the template type parameter with its corresponding
625 // template argument.
Douglas Gregorecd63b82009-07-01 00:28:38 +0000626
627 // If the corresponding template argument is NULL or doesn't exist, it's
628 // because we are performing instantiation from explicitly-specified
629 // template arguments in a function template class, but there were some
630 // arguments left unspecified.
631 if (T->getIndex() >= TemplateArgs.size() ||
632 TemplateArgs[T->getIndex()].isNull())
633 return QualType(T, 0); // Would be nice to keep the original type here
634
Douglas Gregor74296542009-02-27 19:31:52 +0000635 assert(TemplateArgs[T->getIndex()].getKind() == TemplateArgument::Type &&
636 "Template argument kind mismatch");
Douglas Gregor072ac382009-06-26 21:40:05 +0000637 return TemplateArgs[T->getIndex()].getAsType();
Douglas Gregor74296542009-02-27 19:31:52 +0000638 }
639
640 // The template type parameter comes from an inner template (e.g.,
641 // the template parameter list of a member template inside the
642 // template we are instantiating). Create a new template type
643 // parameter with the template "level" reduced by one.
644 return SemaRef.Context.getTemplateTypeParmType(T->getDepth() - 1,
645 T->getIndex(),
Anders Carlsson4e3d3552009-06-16 00:30:48 +0000646 T->isParameterPack(),
Douglas Gregor072ac382009-06-26 21:40:05 +0000647 T->getName());
Douglas Gregor74296542009-02-27 19:31:52 +0000648}
649
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000650QualType
651TemplateTypeInstantiator::
Douglas Gregordd13e842009-03-30 22:58:21 +0000652InstantiateTemplateSpecializationType(
Douglas Gregor072ac382009-06-26 21:40:05 +0000653 const TemplateSpecializationType *T) const {
Douglas Gregor4ac20ef2009-05-29 18:27:38 +0000654 llvm::SmallVector<TemplateArgument, 4> InstantiatedTemplateArgs;
Douglas Gregorf9ff4b12009-03-09 23:48:35 +0000655 InstantiatedTemplateArgs.reserve(T->getNumArgs());
Douglas Gregordd13e842009-03-30 22:58:21 +0000656 for (TemplateSpecializationType::iterator Arg = T->begin(), ArgEnd = T->end();
Douglas Gregorf9ff4b12009-03-09 23:48:35 +0000657 Arg != ArgEnd; ++Arg) {
Douglas Gregorb320b9e2009-06-11 00:06:24 +0000658 TemplateArgument InstArg = SemaRef.Instantiate(*Arg, TemplateArgs);
659 if (InstArg.isNull())
660 return QualType();
Douglas Gregorf9ff4b12009-03-09 23:48:35 +0000661
Douglas Gregorb320b9e2009-06-11 00:06:24 +0000662 InstantiatedTemplateArgs.push_back(InstArg);
Douglas Gregorf9ff4b12009-03-09 23:48:35 +0000663 }
664
Mike Stumpe127ae32009-05-16 07:39:55 +0000665 // FIXME: We're missing the locations of the template name, '<', and '>'.
Douglas Gregor15a92852009-03-31 18:38:02 +0000666
667 TemplateName Name = SemaRef.InstantiateTemplateName(T->getTemplateName(),
668 Loc,
Douglas Gregorf9e7d3d2009-05-11 23:53:27 +0000669 TemplateArgs);
Douglas Gregor15a92852009-03-31 18:38:02 +0000670
671 return SemaRef.CheckTemplateIdType(Name, Loc, SourceLocation(),
Douglas Gregor8f378a92009-06-11 18:10:32 +0000672 InstantiatedTemplateArgs.data(),
Douglas Gregordd13e842009-03-30 22:58:21 +0000673 InstantiatedTemplateArgs.size(),
674 SourceLocation());
Douglas Gregor74296542009-02-27 19:31:52 +0000675}
676
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000677QualType
678TemplateTypeInstantiator::
Douglas Gregor072ac382009-06-26 21:40:05 +0000679InstantiateQualifiedNameType(const QualifiedNameType *T) const {
Douglas Gregord3022602009-03-27 23:10:48 +0000680 // When we instantiated a qualified name type, there's no point in
681 // keeping the qualification around in the instantiated result. So,
682 // just instantiate the named type.
683 return (*this)(T->getNamedType());
684}
685
686QualType
687TemplateTypeInstantiator::
Douglas Gregor072ac382009-06-26 21:40:05 +0000688InstantiateTypenameType(const TypenameType *T) const {
Douglas Gregor77da5802009-04-01 00:28:59 +0000689 if (const TemplateSpecializationType *TemplateId = T->getTemplateId()) {
690 // When the typename type refers to a template-id, the template-id
691 // is dependent and has enough information to instantiate the
692 // result of the typename type. Since we don't care about keeping
693 // the spelling of the typename type in template instantiations,
694 // we just instantiate the template-id.
Douglas Gregor072ac382009-06-26 21:40:05 +0000695 return InstantiateTemplateSpecializationType(TemplateId);
Douglas Gregor77da5802009-04-01 00:28:59 +0000696 }
697
Douglas Gregord3022602009-03-27 23:10:48 +0000698 NestedNameSpecifier *NNS
699 = SemaRef.InstantiateNestedNameSpecifier(T->getQualifier(),
700 SourceRange(Loc),
Douglas Gregorf9e7d3d2009-05-11 23:53:27 +0000701 TemplateArgs);
Douglas Gregord3022602009-03-27 23:10:48 +0000702 if (!NNS)
703 return QualType();
704
Douglas Gregor77da5802009-04-01 00:28:59 +0000705 return SemaRef.CheckTypenameType(NNS, *T->getIdentifier(), SourceRange(Loc));
Douglas Gregor734b4ba2009-03-19 00:18:19 +0000706}
707
708QualType
709TemplateTypeInstantiator::
Douglas Gregor072ac382009-06-26 21:40:05 +0000710InstantiateObjCObjectPointerType(const ObjCObjectPointerType *T) const {
Steve Naroffc75c1a82009-06-17 22:40:22 +0000711 assert(false && "Objective-C types cannot be dependent");
712 return QualType();
713}
714
715QualType
716TemplateTypeInstantiator::
Douglas Gregor072ac382009-06-26 21:40:05 +0000717InstantiateObjCInterfaceType(const ObjCInterfaceType *T) const {
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000718 assert(false && "Objective-C types cannot be dependent");
Douglas Gregor74296542009-02-27 19:31:52 +0000719 return QualType();
720}
721
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000722QualType
723TemplateTypeInstantiator::
Douglas Gregor072ac382009-06-26 21:40:05 +0000724InstantiateObjCQualifiedInterfaceType(
725 const ObjCQualifiedInterfaceType *T) const {
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000726 assert(false && "Objective-C types cannot be dependent");
Douglas Gregor74296542009-02-27 19:31:52 +0000727 return QualType();
728}
729
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000730/// \brief The actual implementation of Sema::InstantiateType().
731QualType TemplateTypeInstantiator::Instantiate(QualType T) const {
732 // If T is not a dependent type, there is nothing to do.
733 if (!T->isDependentType())
734 return T;
735
Douglas Gregor072ac382009-06-26 21:40:05 +0000736 QualType Result;
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000737 switch (T->getTypeClass()) {
738#define TYPE(Class, Base) \
739 case Type::Class: \
Douglas Gregor072ac382009-06-26 21:40:05 +0000740 Result = Instantiate##Class##Type(cast<Class##Type>(T.getTypePtr())); \
741 break;
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000742#define ABSTRACT_TYPE(Class, Base)
743#include "clang/AST/TypeNodes.def"
744 }
Douglas Gregor072ac382009-06-26 21:40:05 +0000745
746 // C++ [dcl.ref]p1:
747 // [...] Cv-qualified references are ill-formed except when
748 // the cv-qualifiers are introduced through the use of a
749 // typedef (7.1.3) or of a template type argument (14.3), in
750 // which case the cv-qualifiers are ignored.
751 //
752 // The same rule applies to function types.
753 if (!Result.isNull() && T.getCVRQualifiers() &&
754 !Result->isFunctionType() && !Result->isReferenceType())
755 Result = Result.getWithAdditionalQualifiers(T.getCVRQualifiers());
756 return Result;
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000757}
Douglas Gregor74296542009-02-27 19:31:52 +0000758
759/// \brief Instantiate the type T with a given set of template arguments.
760///
761/// This routine substitutes the given template arguments into the
762/// type T and produces the instantiated type.
763///
764/// \param T the type into which the template arguments will be
765/// substituted. If this type is not dependent, it will be returned
766/// immediately.
767///
768/// \param TemplateArgs the template arguments that will be
769/// substituted for the top-level template parameters within T.
770///
Douglas Gregor74296542009-02-27 19:31:52 +0000771/// \param Loc the location in the source code where this substitution
772/// is being performed. It will typically be the location of the
773/// declarator (if we're instantiating the type of some declaration)
774/// or the location of the type in the source code (if, e.g., we're
775/// instantiating the type of a cast expression).
776///
777/// \param Entity the name of the entity associated with a declaration
778/// being instantiated (if any). May be empty to indicate that there
779/// is no such entity (if, e.g., this is a type that occurs as part of
780/// a cast expression) or that the entity has no name (e.g., an
781/// unnamed function parameter).
782///
783/// \returns If the instantiation succeeds, the instantiated
784/// type. Otherwise, produces diagnostics and returns a NULL type.
785QualType Sema::InstantiateType(QualType T,
Douglas Gregorf9e7d3d2009-05-11 23:53:27 +0000786 const TemplateArgumentList &TemplateArgs,
Douglas Gregor74296542009-02-27 19:31:52 +0000787 SourceLocation Loc, DeclarationName Entity) {
Douglas Gregor56d25a72009-03-10 20:44:00 +0000788 assert(!ActiveTemplateInstantiations.empty() &&
789 "Cannot perform an instantiation without some context on the "
790 "instantiation stack");
791
Douglas Gregor74296542009-02-27 19:31:52 +0000792 // If T is not a dependent type, there is nothing to do.
793 if (!T->isDependentType())
794 return T;
795
Douglas Gregorf9e7d3d2009-05-11 23:53:27 +0000796 TemplateTypeInstantiator Instantiator(*this, TemplateArgs, Loc, Entity);
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000797 return Instantiator(T);
Douglas Gregor74296542009-02-27 19:31:52 +0000798}
Douglas Gregored3a3982009-03-03 04:44:36 +0000799
800/// \brief Instantiate the base class specifiers of the given class
801/// template specialization.
802///
803/// Produces a diagnostic and returns true on error, returns false and
804/// attaches the instantiated base classes to the class template
805/// specialization if successful.
806bool
Douglas Gregorcc887972009-03-25 21:17:03 +0000807Sema::InstantiateBaseSpecifiers(CXXRecordDecl *Instantiation,
808 CXXRecordDecl *Pattern,
Douglas Gregorf9e7d3d2009-05-11 23:53:27 +0000809 const TemplateArgumentList &TemplateArgs) {
Douglas Gregored3a3982009-03-03 04:44:36 +0000810 bool Invalid = false;
Douglas Gregor4ac20ef2009-05-29 18:27:38 +0000811 llvm::SmallVector<CXXBaseSpecifier*, 4> InstantiatedBases;
Douglas Gregorcc887972009-03-25 21:17:03 +0000812 for (ClassTemplateSpecializationDecl::base_class_iterator
813 Base = Pattern->bases_begin(), BaseEnd = Pattern->bases_end();
Douglas Gregord9572a12009-03-10 18:52:44 +0000814 Base != BaseEnd; ++Base) {
Douglas Gregored3a3982009-03-03 04:44:36 +0000815 if (!Base->getType()->isDependentType()) {
816 // FIXME: Allocate via ASTContext
817 InstantiatedBases.push_back(new CXXBaseSpecifier(*Base));
818 continue;
819 }
820
821 QualType BaseType = InstantiateType(Base->getType(),
Douglas Gregorf9e7d3d2009-05-11 23:53:27 +0000822 TemplateArgs,
Douglas Gregored3a3982009-03-03 04:44:36 +0000823 Base->getSourceRange().getBegin(),
824 DeclarationName());
825 if (BaseType.isNull()) {
826 Invalid = true;
827 continue;
828 }
829
830 if (CXXBaseSpecifier *InstantiatedBase
Douglas Gregorcc887972009-03-25 21:17:03 +0000831 = CheckBaseSpecifier(Instantiation,
Douglas Gregored3a3982009-03-03 04:44:36 +0000832 Base->getSourceRange(),
833 Base->isVirtual(),
834 Base->getAccessSpecifierAsWritten(),
835 BaseType,
836 /*FIXME: Not totally accurate */
837 Base->getSourceRange().getBegin()))
838 InstantiatedBases.push_back(InstantiatedBase);
839 else
840 Invalid = true;
841 }
842
Douglas Gregord9572a12009-03-10 18:52:44 +0000843 if (!Invalid &&
Jay Foad9e6bef42009-05-21 09:52:38 +0000844 AttachBaseSpecifiers(Instantiation, InstantiatedBases.data(),
Douglas Gregored3a3982009-03-03 04:44:36 +0000845 InstantiatedBases.size()))
846 Invalid = true;
847
848 return Invalid;
849}
850
Douglas Gregorcc887972009-03-25 21:17:03 +0000851/// \brief Instantiate the definition of a class from a given pattern.
852///
853/// \param PointOfInstantiation The point of instantiation within the
854/// source code.
855///
856/// \param Instantiation is the declaration whose definition is being
857/// instantiated. This will be either a class template specialization
858/// or a member class of a class template specialization.
859///
860/// \param Pattern is the pattern from which the instantiation
861/// occurs. This will be either the declaration of a class template or
862/// the declaration of a member class of a class template.
863///
864/// \param TemplateArgs The template arguments to be substituted into
865/// the pattern.
866///
Douglas Gregorcc887972009-03-25 21:17:03 +0000867/// \returns true if an error occurred, false otherwise.
868bool
869Sema::InstantiateClass(SourceLocation PointOfInstantiation,
870 CXXRecordDecl *Instantiation, CXXRecordDecl *Pattern,
Douglas Gregorfd79ac62009-05-13 00:25:59 +0000871 const TemplateArgumentList &TemplateArgs,
872 bool ExplicitInstantiation) {
Douglas Gregorcc887972009-03-25 21:17:03 +0000873 bool Invalid = false;
874
875 CXXRecordDecl *PatternDef
876 = cast_or_null<CXXRecordDecl>(Pattern->getDefinition(Context));
877 if (!PatternDef) {
878 if (Pattern == Instantiation->getInstantiatedFromMemberClass()) {
879 Diag(PointOfInstantiation,
880 diag::err_implicit_instantiate_member_undefined)
881 << Context.getTypeDeclType(Instantiation);
882 Diag(Pattern->getLocation(), diag::note_member_of_template_here);
883 } else {
Douglas Gregorfd79ac62009-05-13 00:25:59 +0000884 Diag(PointOfInstantiation, diag::err_template_instantiate_undefined)
885 << ExplicitInstantiation
Douglas Gregorcc887972009-03-25 21:17:03 +0000886 << Context.getTypeDeclType(Instantiation);
887 Diag(Pattern->getLocation(), diag::note_template_decl_here);
888 }
889 return true;
890 }
891 Pattern = PatternDef;
892
Douglas Gregor42c48522009-03-25 21:23:52 +0000893 InstantiatingTemplate Inst(*this, PointOfInstantiation, Instantiation);
Douglas Gregorcc887972009-03-25 21:17:03 +0000894 if (Inst)
895 return true;
896
897 // Enter the scope of this instantiation. We don't use
898 // PushDeclContext because we don't have a scope.
899 DeclContext *PreviousContext = CurContext;
900 CurContext = Instantiation;
901
902 // Start the definition of this instantiation.
903 Instantiation->startDefinition();
904
905 // Instantiate the base class specifiers.
Douglas Gregorf9e7d3d2009-05-11 23:53:27 +0000906 if (InstantiateBaseSpecifiers(Instantiation, Pattern, TemplateArgs))
Douglas Gregorcc887972009-03-25 21:17:03 +0000907 Invalid = true;
908
Douglas Gregor4ac20ef2009-05-29 18:27:38 +0000909 llvm::SmallVector<DeclPtrTy, 4> Fields;
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000910 for (RecordDecl::decl_iterator Member = Pattern->decls_begin(),
911 MemberEnd = Pattern->decls_end();
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000912 Member != MemberEnd; ++Member) {
Douglas Gregorf9e7d3d2009-05-11 23:53:27 +0000913 Decl *NewMember = InstantiateDecl(*Member, Instantiation, TemplateArgs);
Douglas Gregorcc887972009-03-25 21:17:03 +0000914 if (NewMember) {
915 if (NewMember->isInvalidDecl())
916 Invalid = true;
917 else if (FieldDecl *Field = dyn_cast<FieldDecl>(NewMember))
Chris Lattner5261d0c2009-03-28 19:18:32 +0000918 Fields.push_back(DeclPtrTy::make(Field));
Douglas Gregorcc887972009-03-25 21:17:03 +0000919 } else {
920 // FIXME: Eventually, a NULL return will mean that one of the
Mike Stumpe127ae32009-05-16 07:39:55 +0000921 // instantiations was a semantic disaster, and we'll want to set Invalid =
922 // true. For now, we expect to skip some members that we can't yet handle.
Douglas Gregorcc887972009-03-25 21:17:03 +0000923 }
924 }
925
926 // Finish checking fields.
Chris Lattner5261d0c2009-03-28 19:18:32 +0000927 ActOnFields(0, Instantiation->getLocation(), DeclPtrTy::make(Instantiation),
Jay Foad9e6bef42009-05-21 09:52:38 +0000928 Fields.data(), Fields.size(), SourceLocation(), SourceLocation(),
Douglas Gregorcc887972009-03-25 21:17:03 +0000929 0);
930
931 // Add any implicitly-declared members that we might need.
932 AddImplicitlyDeclaredMembersToClass(Instantiation);
933
934 // Exit the scope of this instantiation.
935 CurContext = PreviousContext;
936
Douglas Gregordc18e892009-05-26 20:50:29 +0000937 if (!Invalid)
938 Consumer.HandleTagDeclDefinition(Instantiation);
939
Douglas Gregordf9f5d12009-05-13 20:28:22 +0000940 // If this is an explicit instantiation, instantiate our members, too.
Douglas Gregorb12249d2009-05-18 17:01:57 +0000941 if (!Invalid && ExplicitInstantiation) {
942 Inst.Clear();
Douglas Gregordf9f5d12009-05-13 20:28:22 +0000943 InstantiateClassMembers(PointOfInstantiation, Instantiation, TemplateArgs);
Douglas Gregorb12249d2009-05-18 17:01:57 +0000944 }
Douglas Gregordf9f5d12009-05-13 20:28:22 +0000945
Douglas Gregorcc887972009-03-25 21:17:03 +0000946 return Invalid;
947}
948
Douglas Gregored3a3982009-03-03 04:44:36 +0000949bool
950Sema::InstantiateClassTemplateSpecialization(
951 ClassTemplateSpecializationDecl *ClassTemplateSpec,
952 bool ExplicitInstantiation) {
953 // Perform the actual instantiation on the canonical declaration.
954 ClassTemplateSpec = cast<ClassTemplateSpecializationDecl>(
955 Context.getCanonicalDecl(ClassTemplateSpec));
956
957 // We can only instantiate something that hasn't already been
958 // instantiated or specialized. Fail without any diagnostics: our
959 // caller will provide an error message.
960 if (ClassTemplateSpec->getSpecializationKind() != TSK_Undeclared)
961 return true;
962
Douglas Gregored3a3982009-03-03 04:44:36 +0000963 ClassTemplateDecl *Template = ClassTemplateSpec->getSpecializedTemplate();
Douglas Gregorcc887972009-03-25 21:17:03 +0000964 CXXRecordDecl *Pattern = Template->getTemplatedDecl();
Douglas Gregor58944ac2009-05-31 09:31:02 +0000965 const TemplateArgumentList *TemplateArgs
966 = &ClassTemplateSpec->getTemplateArgs();
967
Douglas Gregor21530c52009-06-12 22:31:52 +0000968 // C++ [temp.class.spec.match]p1:
969 // When a class template is used in a context that requires an
970 // instantiation of the class, it is necessary to determine
971 // whether the instantiation is to be generated using the primary
972 // template or one of the partial specializations. This is done by
973 // matching the template arguments of the class template
974 // specialization with the template argument lists of the partial
975 // specializations.
Douglas Gregorab9f71a2009-06-05 00:53:49 +0000976 typedef std::pair<ClassTemplatePartialSpecializationDecl *,
977 TemplateArgumentList *> MatchResult;
978 llvm::SmallVector<MatchResult, 4> Matched;
Douglas Gregor58944ac2009-05-31 09:31:02 +0000979 for (llvm::FoldingSet<ClassTemplatePartialSpecializationDecl>::iterator
980 Partial = Template->getPartialSpecializations().begin(),
981 PartialEnd = Template->getPartialSpecializations().end();
982 Partial != PartialEnd;
983 ++Partial) {
Douglas Gregor623e2e02009-06-12 18:26:56 +0000984 TemplateDeductionInfo Info(Context);
985 if (TemplateDeductionResult Result
Douglas Gregorab9f71a2009-06-05 00:53:49 +0000986 = DeduceTemplateArguments(&*Partial,
Douglas Gregor623e2e02009-06-12 18:26:56 +0000987 ClassTemplateSpec->getTemplateArgs(),
988 Info)) {
989 // FIXME: Store the failed-deduction information for use in
990 // diagnostics, later.
991 (void)Result;
992 } else {
993 Matched.push_back(std::make_pair(&*Partial, Info.take()));
994 }
Douglas Gregor58944ac2009-05-31 09:31:02 +0000995 }
996
997 if (Matched.size() == 1) {
Douglas Gregor21530c52009-06-12 22:31:52 +0000998 // -- If exactly one matching specialization is found, the
999 // instantiation is generated from that specialization.
Douglas Gregorab9f71a2009-06-05 00:53:49 +00001000 Pattern = Matched[0].first;
1001 TemplateArgs = Matched[0].second;
Douglas Gregor58944ac2009-05-31 09:31:02 +00001002 } else if (Matched.size() > 1) {
Douglas Gregor21530c52009-06-12 22:31:52 +00001003 // -- If more than one matching specialization is found, the
1004 // partial order rules (14.5.4.2) are used to determine
1005 // whether one of the specializations is more specialized
1006 // than the others. If none of the specializations is more
1007 // specialized than all of the other matching
1008 // specializations, then the use of the class template is
1009 // ambiguous and the program is ill-formed.
Douglas Gregor58944ac2009-05-31 09:31:02 +00001010 // FIXME: Implement partial ordering of class template partial
1011 // specializations.
1012 Diag(ClassTemplateSpec->getLocation(),
1013 diag::unsup_template_partial_spec_ordering);
Douglas Gregor21530c52009-06-12 22:31:52 +00001014 } else {
1015 // -- If no matches are found, the instantiation is generated
1016 // from the primary template.
1017
1018 // Since we initialized the pattern and template arguments from
1019 // the primary template, there is nothing more we need to do here.
Douglas Gregor58944ac2009-05-31 09:31:02 +00001020 }
Douglas Gregored3a3982009-03-03 04:44:36 +00001021
1022 // Note that this is an instantiation.
1023 ClassTemplateSpec->setSpecializationKind(
1024 ExplicitInstantiation? TSK_ExplicitInstantiation
1025 : TSK_ImplicitInstantiation);
1026
Douglas Gregorab9f71a2009-06-05 00:53:49 +00001027 bool Result = InstantiateClass(ClassTemplateSpec->getLocation(),
1028 ClassTemplateSpec, Pattern, *TemplateArgs,
1029 ExplicitInstantiation);
1030
1031 for (unsigned I = 0, N = Matched.size(); I != N; ++I) {
1032 // FIXME: Implement TemplateArgumentList::Destroy!
1033 // if (Matched[I].first != Pattern)
1034 // Matched[I].second->Destroy(Context);
1035 }
1036
1037 return Result;
Douglas Gregored3a3982009-03-03 04:44:36 +00001038}
Douglas Gregor47bde7c2009-03-19 17:26:29 +00001039
Douglas Gregordf9f5d12009-05-13 20:28:22 +00001040/// \brief Instantiate the definitions of all of the member of the
1041/// given class, which is an instantiation of a class template or a
1042/// member class of a template.
1043void
1044Sema::InstantiateClassMembers(SourceLocation PointOfInstantiation,
1045 CXXRecordDecl *Instantiation,
1046 const TemplateArgumentList &TemplateArgs) {
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +00001047 for (DeclContext::decl_iterator D = Instantiation->decls_begin(),
1048 DEnd = Instantiation->decls_end();
Douglas Gregordf9f5d12009-05-13 20:28:22 +00001049 D != DEnd; ++D) {
1050 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(*D)) {
Argiris Kirtzidisccb9efe2009-06-30 02:35:26 +00001051 if (!Function->getBody())
Douglas Gregorb12249d2009-05-18 17:01:57 +00001052 InstantiateFunctionDefinition(PointOfInstantiation, Function);
Douglas Gregordf9f5d12009-05-13 20:28:22 +00001053 } else if (VarDecl *Var = dyn_cast<VarDecl>(*D)) {
1054 const VarDecl *Def = 0;
1055 if (!Var->getDefinition(Def))
1056 InstantiateVariableDefinition(Var);
1057 } else if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(*D)) {
1058 if (!Record->isInjectedClassName() && !Record->getDefinition(Context)) {
1059 assert(Record->getInstantiatedFromMemberClass() &&
1060 "Missing instantiated-from-template information");
Douglas Gregorb12249d2009-05-18 17:01:57 +00001061 InstantiateClass(PointOfInstantiation, Record,
Douglas Gregordf9f5d12009-05-13 20:28:22 +00001062 Record->getInstantiatedFromMemberClass(),
1063 TemplateArgs, true);
1064 }
1065 }
1066 }
1067}
1068
1069/// \brief Instantiate the definitions of all of the members of the
1070/// given class template specialization, which was named as part of an
1071/// explicit instantiation.
1072void Sema::InstantiateClassTemplateSpecializationMembers(
1073 SourceLocation PointOfInstantiation,
1074 ClassTemplateSpecializationDecl *ClassTemplateSpec) {
1075 // C++0x [temp.explicit]p7:
1076 // An explicit instantiation that names a class template
1077 // specialization is an explicit instantion of the same kind
1078 // (declaration or definition) of each of its members (not
1079 // including members inherited from base classes) that has not
1080 // been previously explicitly specialized in the translation unit
1081 // containing the explicit instantiation, except as described
1082 // below.
1083 InstantiateClassMembers(PointOfInstantiation, ClassTemplateSpec,
1084 ClassTemplateSpec->getTemplateArgs());
1085}
1086
Douglas Gregor1e589cc2009-03-26 23:50:42 +00001087/// \brief Instantiate a nested-name-specifier.
1088NestedNameSpecifier *
1089Sema::InstantiateNestedNameSpecifier(NestedNameSpecifier *NNS,
1090 SourceRange Range,
Douglas Gregorf9e7d3d2009-05-11 23:53:27 +00001091 const TemplateArgumentList &TemplateArgs) {
Douglas Gregor1e589cc2009-03-26 23:50:42 +00001092 // Instantiate the prefix of this nested name specifier.
1093 NestedNameSpecifier *Prefix = NNS->getPrefix();
1094 if (Prefix) {
Douglas Gregorf9e7d3d2009-05-11 23:53:27 +00001095 Prefix = InstantiateNestedNameSpecifier(Prefix, Range, TemplateArgs);
Douglas Gregor1e589cc2009-03-26 23:50:42 +00001096 if (!Prefix)
1097 return 0;
Douglas Gregor47bde7c2009-03-19 17:26:29 +00001098 }
1099
Douglas Gregor1e589cc2009-03-26 23:50:42 +00001100 switch (NNS->getKind()) {
Douglas Gregor77da5802009-04-01 00:28:59 +00001101 case NestedNameSpecifier::Identifier: {
1102 assert(Prefix &&
1103 "Can't have an identifier nested-name-specifier with no prefix");
1104 CXXScopeSpec SS;
1105 // FIXME: The source location information is all wrong.
1106 SS.setRange(Range);
1107 SS.setScopeRep(Prefix);
1108 return static_cast<NestedNameSpecifier *>(
1109 ActOnCXXNestedNameSpecifier(0, SS,
1110 Range.getEnd(),
1111 Range.getEnd(),
Douglas Gregor96b6df92009-05-14 00:28:11 +00001112 *NNS->getAsIdentifier()));
Douglas Gregor1e589cc2009-03-26 23:50:42 +00001113 break;
Douglas Gregor77da5802009-04-01 00:28:59 +00001114 }
Douglas Gregor1e589cc2009-03-26 23:50:42 +00001115
1116 case NestedNameSpecifier::Namespace:
1117 case NestedNameSpecifier::Global:
1118 return NNS;
1119
1120 case NestedNameSpecifier::TypeSpecWithTemplate:
1121 case NestedNameSpecifier::TypeSpec: {
1122 QualType T = QualType(NNS->getAsType(), 0);
1123 if (!T->isDependentType())
1124 return NNS;
1125
Douglas Gregorf9e7d3d2009-05-11 23:53:27 +00001126 T = InstantiateType(T, TemplateArgs, Range.getBegin(), DeclarationName());
Douglas Gregor1e589cc2009-03-26 23:50:42 +00001127 if (T.isNull())
1128 return 0;
1129
Eli Friedman1db6bab2009-06-13 04:51:30 +00001130 if (T->isDependentType() || T->isRecordType() ||
Douglas Gregord3022602009-03-27 23:10:48 +00001131 (getLangOptions().CPlusPlus0x && T->isEnumeralType())) {
Douglas Gregor77da5802009-04-01 00:28:59 +00001132 assert(T.getCVRQualifiers() == 0 && "Can't get cv-qualifiers here");
Douglas Gregord3022602009-03-27 23:10:48 +00001133 return NestedNameSpecifier::Create(Context, Prefix,
Douglas Gregor1e589cc2009-03-26 23:50:42 +00001134 NNS->getKind() == NestedNameSpecifier::TypeSpecWithTemplate,
Douglas Gregord3022602009-03-27 23:10:48 +00001135 T.getTypePtr());
1136 }
1137
1138 Diag(Range.getBegin(), diag::err_nested_name_spec_non_tag) << T;
1139 return 0;
Douglas Gregor1e589cc2009-03-26 23:50:42 +00001140 }
1141 }
1142
Douglas Gregord3022602009-03-27 23:10:48 +00001143 // Required to silence a GCC warning
Douglas Gregor1e589cc2009-03-26 23:50:42 +00001144 return 0;
Douglas Gregor47bde7c2009-03-19 17:26:29 +00001145}
Douglas Gregor15a92852009-03-31 18:38:02 +00001146
1147TemplateName
1148Sema::InstantiateTemplateName(TemplateName Name, SourceLocation Loc,
Douglas Gregorf9e7d3d2009-05-11 23:53:27 +00001149 const TemplateArgumentList &TemplateArgs) {
Douglas Gregor15a92852009-03-31 18:38:02 +00001150 if (TemplateTemplateParmDecl *TTP
1151 = dyn_cast_or_null<TemplateTemplateParmDecl>(
1152 Name.getAsTemplateDecl())) {
1153 assert(TTP->getDepth() == 0 &&
1154 "Cannot reduce depth of a template template parameter");
Douglas Gregoraed98042009-03-31 20:22:05 +00001155 assert(TemplateArgs[TTP->getPosition()].getAsDecl() &&
Douglas Gregor15a92852009-03-31 18:38:02 +00001156 "Wrong kind of template template argument");
1157 ClassTemplateDecl *ClassTemplate
1158 = dyn_cast<ClassTemplateDecl>(
1159 TemplateArgs[TTP->getPosition()].getAsDecl());
Douglas Gregoraed98042009-03-31 20:22:05 +00001160 assert(ClassTemplate && "Expected a class template");
Douglas Gregor15a92852009-03-31 18:38:02 +00001161 if (QualifiedTemplateName *QTN = Name.getAsQualifiedTemplateName()) {
1162 NestedNameSpecifier *NNS
1163 = InstantiateNestedNameSpecifier(QTN->getQualifier(),
1164 /*FIXME=*/SourceRange(Loc),
Douglas Gregorf9e7d3d2009-05-11 23:53:27 +00001165 TemplateArgs);
Douglas Gregor15a92852009-03-31 18:38:02 +00001166 if (NNS)
1167 return Context.getQualifiedTemplateName(NNS,
1168 QTN->hasTemplateKeyword(),
1169 ClassTemplate);
1170 }
1171
1172 return TemplateName(ClassTemplate);
1173 } else if (DependentTemplateName *DTN = Name.getAsDependentTemplateName()) {
1174 NestedNameSpecifier *NNS
1175 = InstantiateNestedNameSpecifier(DTN->getQualifier(),
1176 /*FIXME=*/SourceRange(Loc),
Douglas Gregorf9e7d3d2009-05-11 23:53:27 +00001177 TemplateArgs);
Douglas Gregor15a92852009-03-31 18:38:02 +00001178
1179 if (!NNS) // FIXME: Not the best recovery strategy.
1180 return Name;
1181
1182 if (NNS->isDependent())
1183 return Context.getDependentTemplateName(NNS, DTN->getName());
1184
1185 // Somewhat redundant with ActOnDependentTemplateName.
1186 CXXScopeSpec SS;
1187 SS.setRange(SourceRange(Loc));
1188 SS.setScopeRep(NNS);
1189 TemplateTy Template;
1190 TemplateNameKind TNK = isTemplateName(*DTN->getName(), 0, Template, &SS);
1191 if (TNK == TNK_Non_template) {
1192 Diag(Loc, diag::err_template_kw_refers_to_non_template)
1193 << DTN->getName();
1194 return Name;
1195 } else if (TNK == TNK_Function_template) {
1196 Diag(Loc, diag::err_template_kw_refers_to_non_template)
1197 << DTN->getName();
1198 return Name;
1199 }
1200
1201 return Template.getAsVal<TemplateName>();
1202 }
1203
1204
1205
Mike Stumpe127ae32009-05-16 07:39:55 +00001206 // FIXME: Even if we're referring to a Decl that isn't a template template
1207 // parameter, we may need to instantiate the outer contexts of that
1208 // Decl. However, this won't be needed until we implement member templates.
Douglas Gregor15a92852009-03-31 18:38:02 +00001209 return Name;
1210}
Douglas Gregorb320b9e2009-06-11 00:06:24 +00001211
1212TemplateArgument Sema::Instantiate(TemplateArgument Arg,
1213 const TemplateArgumentList &TemplateArgs) {
1214 switch (Arg.getKind()) {
1215 case TemplateArgument::Null:
1216 assert(false && "Should never have a NULL template argument");
1217 break;
1218
1219 case TemplateArgument::Type: {
1220 QualType T = InstantiateType(Arg.getAsType(), TemplateArgs,
1221 Arg.getLocation(), DeclarationName());
1222 if (T.isNull())
1223 return TemplateArgument();
1224
1225 return TemplateArgument(Arg.getLocation(), T);
1226 }
1227
1228 case TemplateArgument::Declaration:
1229 // FIXME: Template instantiation for template template parameters.
1230 return Arg;
1231
1232 case TemplateArgument::Integral:
1233 return Arg;
1234
1235 case TemplateArgument::Expression: {
Douglas Gregora8b2fbf2009-06-22 20:57:11 +00001236 // Template argument expressions are not potentially evaluated.
1237 EnterExpressionEvaluationContext Unevaluated(*this, Action::Unevaluated);
1238
Douglas Gregorb320b9e2009-06-11 00:06:24 +00001239 Sema::OwningExprResult E = InstantiateExpr(Arg.getAsExpr(), TemplateArgs);
1240 if (E.isInvalid())
1241 return TemplateArgument();
1242 return TemplateArgument(E.takeAs<Expr>());
1243 }
Anders Carlsson584b5062009-06-15 17:04:53 +00001244
1245 case TemplateArgument::Pack:
1246 assert(0 && "FIXME: Implement!");
1247 break;
Douglas Gregorb320b9e2009-06-11 00:06:24 +00001248 }
1249
1250 assert(false && "Unhandled template argument kind");
1251 return TemplateArgument();
1252}