blob: a75a8584d1f2e37dddc4e21ee247664b5a1c394b [file] [log] [blame]
Douglas Gregor99ebf652009-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 Gregoraba43bb2009-05-26 20:50:29 +000014#include "clang/AST/ASTConsumer.h"
Douglas Gregor99ebf652009-02-27 19:31:52 +000015#include "clang/AST/ASTContext.h"
16#include "clang/AST/Expr.h"
Douglas Gregor99ebf652009-02-27 19:31:52 +000017#include "clang/AST/DeclTemplate.h"
18#include "clang/Parse/DeclSpec.h"
19#include "clang/Basic/LangOptions.h"
Douglas Gregorcd281c32009-02-28 00:25:32 +000020#include "llvm/Support/Compiler.h"
Douglas Gregor99ebf652009-02-27 19:31:52 +000021
22using namespace clang;
23
Douglas Gregoree1828a2009-03-10 18:03:33 +000024//===----------------------------------------------------------------------===/
25// Template Instantiation Support
26//===----------------------------------------------------------------------===/
27
Douglas Gregor54dabfc2009-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 Gregor1637be72009-06-26 00:10:03 +000032 // Template arguments for a class template specialization.
Douglas Gregor54dabfc2009-05-14 23:26:13 +000033 if (ClassTemplateSpecializationDecl *Spec
34 = dyn_cast<ClassTemplateSpecializationDecl>(D))
35 return Spec->getTemplateArgs();
36
Douglas Gregor1637be72009-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 Gregor54dabfc2009-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 Gregor26dce442009-03-10 00:06:19 +000056Sema::InstantiatingTemplate::
57InstantiatingTemplate(Sema &SemaRef, SourceLocation PointOfInstantiation,
Douglas Gregorf3e7ce42009-05-18 17:01:57 +000058 Decl *Entity,
Douglas Gregor26dce442009-03-10 00:06:19 +000059 SourceRange InstantiationRange)
60 : SemaRef(SemaRef) {
Douglas Gregordf667e72009-03-10 20:44:00 +000061
62 Invalid = CheckInstantiationDepth(PointOfInstantiation,
63 InstantiationRange);
64 if (!Invalid) {
Douglas Gregor26dce442009-03-10 00:06:19 +000065 ActiveTemplateInstantiation Inst;
Douglas Gregordf667e72009-03-10 20:44:00 +000066 Inst.Kind = ActiveTemplateInstantiation::TemplateInstantiation;
Douglas Gregor26dce442009-03-10 00:06:19 +000067 Inst.PointOfInstantiation = PointOfInstantiation;
Douglas Gregordf667e72009-03-10 20:44:00 +000068 Inst.Entity = reinterpret_cast<uintptr_t>(Entity);
Douglas Gregor313a81d2009-03-12 18:36:18 +000069 Inst.TemplateArgs = 0;
70 Inst.NumTemplateArgs = 0;
Douglas Gregordf667e72009-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 Gregor26dce442009-03-10 00:06:19 +000095 Inst.InstantiationRange = InstantiationRange;
96 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
97 Invalid = false;
98 }
99}
100
Douglas Gregor637a4092009-06-10 23:47:09 +0000101Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef,
102 SourceLocation PointOfInstantiation,
Douglas Gregorcca9e962009-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 Gregor637a4092009-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 Gregorcca9e962009-07-01 22:01:06 +0000138 = ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution;
Douglas Gregor637a4092009-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 Gregorf3e7ce42009-05-18 17:01:57 +0000149void Sema::InstantiatingTemplate::Clear() {
150 if (!Invalid) {
Douglas Gregor26dce442009-03-10 00:06:19 +0000151 SemaRef.ActiveTemplateInstantiations.pop_back();
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000152 Invalid = true;
153 }
Douglas Gregor26dce442009-03-10 00:06:19 +0000154}
155
Douglas Gregordf667e72009-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 Gregoree1828a2009-03-10 18:03:33 +0000172/// \brief Prints the current instantiation stack through a series of
173/// notes.
174void Sema::PrintInstantiationStack() {
Douglas Gregorcca9e962009-07-01 22:01:06 +0000175 // FIXME: In all of these cases, we need to show the template arguments
Douglas Gregoree1828a2009-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 Gregordf667e72009-03-10 20:44:00 +0000181 switch (Active->Kind) {
182 case ActiveTemplateInstantiation::TemplateInstantiation: {
Douglas Gregorf3e7ce42009-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 Gregor1637be72009-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 Gregorf3e7ce42009-05-18 17:01:57 +0000199 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
200 DiagID)
201 << Function
202 << Active->InstantiationRange;
203 }
Douglas Gregordf667e72009-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 Gregor7532dc62009-03-30 22:58:21 +0000210 = TemplateSpecializationType::PrintTemplateArgumentList(
Douglas Gregorcca9e962009-07-01 22:01:06 +0000211 Active->TemplateArgs,
Douglas Gregord249e1d1f2009-05-29 20:38:28 +0000212 Active->NumTemplateArgs,
213 Context.PrintingPolicy);
Douglas Gregordf667e72009-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 Gregor637a4092009-06-10 23:47:09 +0000220
Douglas Gregorcca9e962009-07-01 22:01:06 +0000221 case ActiveTemplateInstantiation::ExplicitTemplateArgumentSubstitution: {
222 FunctionTemplateDecl *FnTmpl
223 = cast<FunctionTemplateDecl>((Decl *)Active->Entity);
Douglas Gregor637a4092009-06-10 23:47:09 +0000224 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
Douglas Gregorcca9e962009-07-01 22:01:06 +0000225 diag::note_explicit_template_arg_substitution_here)
226 << FnTmpl << Active->InstantiationRange;
Douglas Gregor637a4092009-06-10 23:47:09 +0000227 break;
228 }
Douglas Gregorcca9e962009-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 Gregor637a4092009-06-10 23:47:09 +0000246
Douglas Gregordf667e72009-03-10 20:44:00 +0000247 }
Douglas Gregoree1828a2009-03-10 18:03:33 +0000248 }
249}
250
Douglas Gregor5e9f35c2009-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 Gregorcca9e962009-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 Gregor5e9f35c2009-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 Gregorcca9e962009-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 Gregor5e9f35c2009-06-14 07:33:30 +0000274 }
275 }
276
277 return false;
278}
279
Douglas Gregor99ebf652009-02-27 19:31:52 +0000280//===----------------------------------------------------------------------===/
281// Template Instantiation for Types
282//===----------------------------------------------------------------------===/
Douglas Gregorcd281c32009-02-28 00:25:32 +0000283namespace {
284 class VISIBILITY_HIDDEN TemplateTypeInstantiator {
285 Sema &SemaRef;
Douglas Gregor7e063902009-05-11 23:53:27 +0000286 const TemplateArgumentList &TemplateArgs;
Douglas Gregorcd281c32009-02-28 00:25:32 +0000287 SourceLocation Loc;
288 DeclarationName Entity;
Douglas Gregor99ebf652009-02-27 19:31:52 +0000289
Douglas Gregorcd281c32009-02-28 00:25:32 +0000290 public:
291 TemplateTypeInstantiator(Sema &SemaRef,
Douglas Gregor7e063902009-05-11 23:53:27 +0000292 const TemplateArgumentList &TemplateArgs,
Douglas Gregorcd281c32009-02-28 00:25:32 +0000293 SourceLocation Loc,
294 DeclarationName Entity)
295 : SemaRef(SemaRef), TemplateArgs(TemplateArgs),
Douglas Gregor7e063902009-05-11 23:53:27 +0000296 Loc(Loc), Entity(Entity) { }
Douglas Gregorcd281c32009-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 Gregor8a5cb112009-06-26 21:40:05 +0000304 QualType Instantiate##Class##Type(const Class##Type *T) const;
Douglas Gregorcd281c32009-02-28 00:25:32 +0000305#define ABSTRACT_TYPE(Class, Base)
306#include "clang/AST/TypeNodes.def"
307 };
308}
309
310QualType
Douglas Gregor8a5cb112009-06-26 21:40:05 +0000311TemplateTypeInstantiator::InstantiateExtQualType(const ExtQualType *T) const {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000312 // FIXME: Implement this
313 assert(false && "Cannot instantiate ExtQualType yet");
314 return QualType();
315}
316
Douglas Gregorcd281c32009-02-28 00:25:32 +0000317QualType
Douglas Gregor8a5cb112009-06-26 21:40:05 +0000318TemplateTypeInstantiator::InstantiateBuiltinType(const BuiltinType *T) const {
Douglas Gregor724651c2009-02-28 01:04:19 +0000319 assert(false && "Builtin types are not dependent and cannot be instantiated");
Douglas Gregor8a5cb112009-06-26 21:40:05 +0000320 return QualType(T, 0);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000321}
322
Douglas Gregorcd281c32009-02-28 00:25:32 +0000323QualType
324TemplateTypeInstantiator::
Douglas Gregor8a5cb112009-06-26 21:40:05 +0000325InstantiateFixedWidthIntType(const FixedWidthIntType *T) const {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000326 // FIXME: Implement this
327 assert(false && "Cannot instantiate FixedWidthIntType yet");
328 return QualType();
329}
330
Douglas Gregorcd281c32009-02-28 00:25:32 +0000331QualType
Douglas Gregor8a5cb112009-06-26 21:40:05 +0000332TemplateTypeInstantiator::InstantiateComplexType(const ComplexType *T) const {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000333 // FIXME: Implement this
334 assert(false && "Cannot instantiate ComplexType yet");
335 return QualType();
336}
337
Douglas Gregorcd281c32009-02-28 00:25:32 +0000338QualType
Douglas Gregor8a5cb112009-06-26 21:40:05 +0000339TemplateTypeInstantiator::InstantiatePointerType(const PointerType *T) const {
Douglas Gregorcd281c32009-02-28 00:25:32 +0000340 QualType PointeeType = Instantiate(T->getPointeeType());
341 if (PointeeType.isNull())
342 return QualType();
343
Douglas Gregor8a5cb112009-06-26 21:40:05 +0000344 return SemaRef.BuildPointerType(PointeeType, 0, Loc, Entity);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000345}
346
Douglas Gregorcd281c32009-02-28 00:25:32 +0000347QualType
Douglas Gregor8a5cb112009-06-26 21:40:05 +0000348TemplateTypeInstantiator::InstantiateBlockPointerType(
349 const BlockPointerType *T) const {
Anders Carlsson859ba502009-06-12 16:23:10 +0000350 QualType PointeeType = Instantiate(T->getPointeeType());
351 if (PointeeType.isNull())
352 return QualType();
353
Douglas Gregor8a5cb112009-06-26 21:40:05 +0000354 return SemaRef.BuildBlockPointerType(PointeeType, 0, Loc, Entity);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000355}
356
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000357QualType
358TemplateTypeInstantiator::InstantiateLValueReferenceType(
Douglas Gregor8a5cb112009-06-26 21:40:05 +0000359 const LValueReferenceType *T) const {
Douglas Gregorcd281c32009-02-28 00:25:32 +0000360 QualType ReferentType = Instantiate(T->getPointeeType());
361 if (ReferentType.isNull())
362 return QualType();
363
Douglas Gregor8a5cb112009-06-26 21:40:05 +0000364 return SemaRef.BuildReferenceType(ReferentType, true, 0, Loc, Entity);
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000365}
366
367QualType
368TemplateTypeInstantiator::InstantiateRValueReferenceType(
Douglas Gregor8a5cb112009-06-26 21:40:05 +0000369 const RValueReferenceType *T) const {
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000370 QualType ReferentType = Instantiate(T->getPointeeType());
371 if (ReferentType.isNull())
372 return QualType();
373
Douglas Gregor8a5cb112009-06-26 21:40:05 +0000374 return SemaRef.BuildReferenceType(ReferentType, false, 0, Loc, Entity);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000375}
376
Douglas Gregorcd281c32009-02-28 00:25:32 +0000377QualType
378TemplateTypeInstantiator::
Douglas Gregor8a5cb112009-06-26 21:40:05 +0000379InstantiateMemberPointerType(const MemberPointerType *T) const {
Douglas Gregor949bf692009-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 Gregor8a5cb112009-06-26 21:40:05 +0000388 return SemaRef.BuildMemberPointerType(PointeeType, ClassType, 0, Loc,
Douglas Gregor949bf692009-06-09 22:17:39 +0000389 Entity);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000390}
391
Douglas Gregorcd281c32009-02-28 00:25:32 +0000392QualType
393TemplateTypeInstantiator::
Douglas Gregor8a5cb112009-06-26 21:40:05 +0000394InstantiateConstantArrayType(const ConstantArrayType *T) const {
Douglas Gregorcd281c32009-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 Gregorff668032009-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 Gregorcd281c32009-02-28 00:25:32 +0000422 return SemaRef.BuildArrayType(ElementType, T->getSizeModifier(),
423 &ArraySize, T->getIndexTypeQualifier(),
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000424 SourceRange(), // FIXME: provide proper range?
425 Entity);
426}
427
428QualType
429TemplateTypeInstantiator::InstantiateConstantArrayWithExprType
430(const ConstantArrayWithExprType *T) const {
431 return InstantiateConstantArrayType(T);
432}
433
434QualType
435TemplateTypeInstantiator::InstantiateConstantArrayWithoutExprType
436(const ConstantArrayWithoutExprType *T) const {
437 return InstantiateConstantArrayType(T);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000438}
439
Douglas Gregorcd281c32009-02-28 00:25:32 +0000440QualType
441TemplateTypeInstantiator::
Douglas Gregor8a5cb112009-06-26 21:40:05 +0000442InstantiateIncompleteArrayType(const IncompleteArrayType *T) const {
Douglas Gregorcd281c32009-02-28 00:25:32 +0000443 QualType ElementType = Instantiate(T->getElementType());
444 if (ElementType.isNull())
445 return ElementType;
446
447 return SemaRef.BuildArrayType(ElementType, T->getSizeModifier(),
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000448 0, T->getIndexTypeQualifier(),
449 SourceRange(), // FIXME: provide proper range?
450 Entity);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000451}
452
Douglas Gregorcd281c32009-02-28 00:25:32 +0000453QualType
454TemplateTypeInstantiator::
Douglas Gregor8a5cb112009-06-26 21:40:05 +0000455InstantiateVariableArrayType(const VariableArrayType *T) const {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000456 // FIXME: Implement this
457 assert(false && "Cannot instantiate VariableArrayType yet");
458 return QualType();
459}
460
Douglas Gregorcd281c32009-02-28 00:25:32 +0000461QualType
462TemplateTypeInstantiator::
Douglas Gregor8a5cb112009-06-26 21:40:05 +0000463InstantiateDependentSizedArrayType(const DependentSizedArrayType *T) const {
Anders Carlsson76b1c842009-03-15 20:12:13 +0000464 Expr *ArraySize = T->getSizeExpr();
465 assert(ArraySize->isValueDependent() &&
466 "dependent sized array types must have value dependent size expr");
467
468 // Instantiate the element type if needed
469 QualType ElementType = T->getElementType();
470 if (ElementType->isDependentType()) {
471 ElementType = Instantiate(ElementType);
472 if (ElementType.isNull())
473 return QualType();
474 }
475
476 // Instantiate the size expression
Douglas Gregorac7610d2009-06-22 20:57:11 +0000477 EnterExpressionEvaluationContext Unevaluated(SemaRef, Action::Unevaluated);
Anders Carlsson76b1c842009-03-15 20:12:13 +0000478 Sema::OwningExprResult InstantiatedArraySize =
Douglas Gregor7e063902009-05-11 23:53:27 +0000479 SemaRef.InstantiateExpr(ArraySize, TemplateArgs);
Anders Carlsson76b1c842009-03-15 20:12:13 +0000480 if (InstantiatedArraySize.isInvalid())
481 return QualType();
482
483 return SemaRef.BuildArrayType(ElementType, T->getSizeModifier(),
Anders Carlssone9146f22009-05-01 19:49:17 +0000484 InstantiatedArraySize.takeAs<Expr>(),
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000485 T->getIndexTypeQualifier(),
486 SourceRange(), // FIXME: provide proper range?
487 Entity);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000488}
489
Douglas Gregorcd281c32009-02-28 00:25:32 +0000490QualType
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000491TemplateTypeInstantiator::
Douglas Gregor8a5cb112009-06-26 21:40:05 +0000492InstantiateDependentSizedExtVectorType(
493 const DependentSizedExtVectorType *T) const {
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000494
Douglas Gregorf6ddb732009-06-18 18:45:36 +0000495 // Instantiate the element type if needed.
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000496 QualType ElementType = T->getElementType();
497 if (ElementType->isDependentType()) {
498 ElementType = Instantiate(ElementType);
499 if (ElementType.isNull())
500 return QualType();
501 }
502
Douglas Gregorac7610d2009-06-22 20:57:11 +0000503 // The expression in a dependent-sized extended vector type is not
504 // potentially evaluated.
505 EnterExpressionEvaluationContext Unevaluated(SemaRef, Action::Unevaluated);
506
Douglas Gregorf6ddb732009-06-18 18:45:36 +0000507 // Instantiate the size expression.
508 const Expr *SizeExpr = T->getSizeExpr();
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000509 Sema::OwningExprResult InstantiatedArraySize =
Douglas Gregorf6ddb732009-06-18 18:45:36 +0000510 SemaRef.InstantiateExpr(const_cast<Expr *>(SizeExpr), TemplateArgs);
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000511 if (InstantiatedArraySize.isInvalid())
512 return QualType();
513
514 return SemaRef.BuildExtVectorType(ElementType,
515 SemaRef.Owned(
516 InstantiatedArraySize.takeAs<Expr>()),
517 T->getAttributeLoc());
518}
519
520QualType
Douglas Gregor8a5cb112009-06-26 21:40:05 +0000521TemplateTypeInstantiator::InstantiateVectorType(const VectorType *T) const {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000522 // FIXME: Implement this
523 assert(false && "Cannot instantiate VectorType yet");
524 return QualType();
525}
526
Douglas Gregorcd281c32009-02-28 00:25:32 +0000527QualType
Douglas Gregor8a5cb112009-06-26 21:40:05 +0000528TemplateTypeInstantiator::InstantiateExtVectorType(
529 const ExtVectorType *T) const {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000530 // FIXME: Implement this
531 assert(false && "Cannot instantiate ExtVectorType yet");
532 return QualType();
533}
534
Douglas Gregorcd281c32009-02-28 00:25:32 +0000535QualType
536TemplateTypeInstantiator::
Douglas Gregor8a5cb112009-06-26 21:40:05 +0000537InstantiateFunctionProtoType(const FunctionProtoType *T) const {
Douglas Gregor724651c2009-02-28 01:04:19 +0000538 QualType ResultType = Instantiate(T->getResultType());
539 if (ResultType.isNull())
540 return ResultType;
541
Douglas Gregor0ca20ac2009-05-29 18:27:38 +0000542 llvm::SmallVector<QualType, 4> ParamTypes;
Douglas Gregor724651c2009-02-28 01:04:19 +0000543 for (FunctionProtoType::arg_type_iterator Param = T->arg_type_begin(),
544 ParamEnd = T->arg_type_end();
545 Param != ParamEnd; ++Param) {
546 QualType P = Instantiate(*Param);
547 if (P.isNull())
548 return P;
549
550 ParamTypes.push_back(P);
551 }
552
Douglas Gregor02cbbd22009-06-11 18:10:32 +0000553 return SemaRef.BuildFunctionType(ResultType, ParamTypes.data(),
Douglas Gregor724651c2009-02-28 01:04:19 +0000554 ParamTypes.size(),
555 T->isVariadic(), T->getTypeQuals(),
556 Loc, Entity);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000557}
558
Douglas Gregorcd281c32009-02-28 00:25:32 +0000559QualType
560TemplateTypeInstantiator::
Douglas Gregor8a5cb112009-06-26 21:40:05 +0000561InstantiateFunctionNoProtoType(const FunctionNoProtoType *T) const {
Douglas Gregor724651c2009-02-28 01:04:19 +0000562 assert(false && "Functions without prototypes cannot be dependent.");
Douglas Gregor99ebf652009-02-27 19:31:52 +0000563 return QualType();
564}
565
Douglas Gregorcd281c32009-02-28 00:25:32 +0000566QualType
Douglas Gregor8a5cb112009-06-26 21:40:05 +0000567TemplateTypeInstantiator::InstantiateTypedefType(const TypedefType *T) const {
Douglas Gregor815215d2009-05-27 05:35:12 +0000568 TypedefDecl *Typedef
Douglas Gregored961e72009-05-27 17:54:46 +0000569 = cast_or_null<TypedefDecl>(
570 SemaRef.InstantiateCurrentDeclRef(T->getDecl()));
Douglas Gregor815215d2009-05-27 05:35:12 +0000571 if (!Typedef)
572 return QualType();
573
574 return SemaRef.Context.getTypeDeclType(Typedef);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000575}
576
Douglas Gregorcd281c32009-02-28 00:25:32 +0000577QualType
Douglas Gregor8a5cb112009-06-26 21:40:05 +0000578TemplateTypeInstantiator::InstantiateTypeOfExprType(
579 const TypeOfExprType *T) const {
Douglas Gregorac7610d2009-06-22 20:57:11 +0000580 // The expression in a typeof is not potentially evaluated.
581 EnterExpressionEvaluationContext Unevaluated(SemaRef, Action::Unevaluated);
582
Douglas Gregor5f8bd592009-05-26 22:09:24 +0000583 Sema::OwningExprResult E
584 = SemaRef.InstantiateExpr(T->getUnderlyingExpr(), TemplateArgs);
585 if (E.isInvalid())
586 return QualType();
587
Anders Carlssonaf017e62009-06-29 22:58:55 +0000588 return SemaRef.BuildTypeofExprType(E.takeAs<Expr>());
Douglas Gregor99ebf652009-02-27 19:31:52 +0000589}
590
Douglas Gregorcd281c32009-02-28 00:25:32 +0000591QualType
Douglas Gregor8a5cb112009-06-26 21:40:05 +0000592TemplateTypeInstantiator::InstantiateTypeOfType(const TypeOfType *T) const {
Douglas Gregor5f8bd592009-05-26 22:09:24 +0000593 QualType Underlying = Instantiate(T->getUnderlyingType());
594 if (Underlying.isNull())
595 return QualType();
596
597 return SemaRef.Context.getTypeOfType(Underlying);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000598}
599
Anders Carlsson395b4752009-06-24 19:06:50 +0000600QualType
Douglas Gregor8a5cb112009-06-26 21:40:05 +0000601TemplateTypeInstantiator::InstantiateDecltypeType(const DecltypeType *T) const {
Anders Carlsson87471f52009-06-26 03:02:18 +0000602 // C++0x [dcl.type.simple]p4:
603 // The operand of the decltype specifier is an unevaluated operand.
604 EnterExpressionEvaluationContext Unevaluated(SemaRef,
605 Action::Unevaluated);
606
Anders Carlsson395b4752009-06-24 19:06:50 +0000607 Sema::OwningExprResult E
608 = SemaRef.InstantiateExpr(T->getUnderlyingExpr(), TemplateArgs);
609
610 if (E.isInvalid())
611 return QualType();
612
Anders Carlssonaf017e62009-06-29 22:58:55 +0000613 return SemaRef.BuildDecltypeType(E.takeAs<Expr>());
Anders Carlsson395b4752009-06-24 19:06:50 +0000614}
615
Douglas Gregorcd281c32009-02-28 00:25:32 +0000616QualType
Douglas Gregor8a5cb112009-06-26 21:40:05 +0000617TemplateTypeInstantiator::InstantiateRecordType(const RecordType *T) const {
Douglas Gregor815215d2009-05-27 05:35:12 +0000618 RecordDecl *Record
Douglas Gregored961e72009-05-27 17:54:46 +0000619 = cast_or_null<RecordDecl>(SemaRef.InstantiateCurrentDeclRef(T->getDecl()));
Douglas Gregor815215d2009-05-27 05:35:12 +0000620 if (!Record)
621 return QualType();
622
623 return SemaRef.Context.getTypeDeclType(Record);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000624}
625
Douglas Gregorcd281c32009-02-28 00:25:32 +0000626QualType
Douglas Gregor8a5cb112009-06-26 21:40:05 +0000627TemplateTypeInstantiator::InstantiateEnumType(const EnumType *T) const {
Douglas Gregor815215d2009-05-27 05:35:12 +0000628 EnumDecl *Enum
Douglas Gregored961e72009-05-27 17:54:46 +0000629 = cast_or_null<EnumDecl>(SemaRef.InstantiateCurrentDeclRef(T->getDecl()));
Douglas Gregor815215d2009-05-27 05:35:12 +0000630 if (!Enum)
631 return QualType();
632
633 return SemaRef.Context.getTypeDeclType(Enum);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000634}
635
Douglas Gregorcd281c32009-02-28 00:25:32 +0000636QualType
637TemplateTypeInstantiator::
Douglas Gregor8a5cb112009-06-26 21:40:05 +0000638InstantiateTemplateTypeParmType(const TemplateTypeParmType *T) const {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000639 if (T->getDepth() == 0) {
640 // Replace the template type parameter with its corresponding
641 // template argument.
Douglas Gregor16134c62009-07-01 00:28:38 +0000642
643 // If the corresponding template argument is NULL or doesn't exist, it's
644 // because we are performing instantiation from explicitly-specified
645 // template arguments in a function template class, but there were some
646 // arguments left unspecified.
647 if (T->getIndex() >= TemplateArgs.size() ||
648 TemplateArgs[T->getIndex()].isNull())
649 return QualType(T, 0); // Would be nice to keep the original type here
650
Douglas Gregor99ebf652009-02-27 19:31:52 +0000651 assert(TemplateArgs[T->getIndex()].getKind() == TemplateArgument::Type &&
652 "Template argument kind mismatch");
Douglas Gregor8a5cb112009-06-26 21:40:05 +0000653 return TemplateArgs[T->getIndex()].getAsType();
Douglas Gregor99ebf652009-02-27 19:31:52 +0000654 }
655
656 // The template type parameter comes from an inner template (e.g.,
657 // the template parameter list of a member template inside the
658 // template we are instantiating). Create a new template type
659 // parameter with the template "level" reduced by one.
660 return SemaRef.Context.getTemplateTypeParmType(T->getDepth() - 1,
661 T->getIndex(),
Anders Carlsson76e4ce42009-06-16 00:30:48 +0000662 T->isParameterPack(),
Douglas Gregor8a5cb112009-06-26 21:40:05 +0000663 T->getName());
Douglas Gregor99ebf652009-02-27 19:31:52 +0000664}
665
Douglas Gregorcd281c32009-02-28 00:25:32 +0000666QualType
667TemplateTypeInstantiator::
Douglas Gregor7532dc62009-03-30 22:58:21 +0000668InstantiateTemplateSpecializationType(
Douglas Gregor8a5cb112009-06-26 21:40:05 +0000669 const TemplateSpecializationType *T) const {
Douglas Gregor0ca20ac2009-05-29 18:27:38 +0000670 llvm::SmallVector<TemplateArgument, 4> InstantiatedTemplateArgs;
Douglas Gregor40808ce2009-03-09 23:48:35 +0000671 InstantiatedTemplateArgs.reserve(T->getNumArgs());
Douglas Gregor7532dc62009-03-30 22:58:21 +0000672 for (TemplateSpecializationType::iterator Arg = T->begin(), ArgEnd = T->end();
Douglas Gregor40808ce2009-03-09 23:48:35 +0000673 Arg != ArgEnd; ++Arg) {
Douglas Gregor91333002009-06-11 00:06:24 +0000674 TemplateArgument InstArg = SemaRef.Instantiate(*Arg, TemplateArgs);
675 if (InstArg.isNull())
676 return QualType();
Douglas Gregor40808ce2009-03-09 23:48:35 +0000677
Douglas Gregor91333002009-06-11 00:06:24 +0000678 InstantiatedTemplateArgs.push_back(InstArg);
Douglas Gregor40808ce2009-03-09 23:48:35 +0000679 }
680
Mike Stump390b4cc2009-05-16 07:39:55 +0000681 // FIXME: We're missing the locations of the template name, '<', and '>'.
Douglas Gregorde650ae2009-03-31 18:38:02 +0000682
683 TemplateName Name = SemaRef.InstantiateTemplateName(T->getTemplateName(),
684 Loc,
Douglas Gregor7e063902009-05-11 23:53:27 +0000685 TemplateArgs);
Douglas Gregorde650ae2009-03-31 18:38:02 +0000686
687 return SemaRef.CheckTemplateIdType(Name, Loc, SourceLocation(),
Douglas Gregor02cbbd22009-06-11 18:10:32 +0000688 InstantiatedTemplateArgs.data(),
Douglas Gregor7532dc62009-03-30 22:58:21 +0000689 InstantiatedTemplateArgs.size(),
690 SourceLocation());
Douglas Gregor99ebf652009-02-27 19:31:52 +0000691}
692
Douglas Gregorcd281c32009-02-28 00:25:32 +0000693QualType
694TemplateTypeInstantiator::
Douglas Gregor8a5cb112009-06-26 21:40:05 +0000695InstantiateQualifiedNameType(const QualifiedNameType *T) const {
Douglas Gregord57959a2009-03-27 23:10:48 +0000696 // When we instantiated a qualified name type, there's no point in
697 // keeping the qualification around in the instantiated result. So,
698 // just instantiate the named type.
699 return (*this)(T->getNamedType());
700}
701
702QualType
703TemplateTypeInstantiator::
Douglas Gregor8a5cb112009-06-26 21:40:05 +0000704InstantiateTypenameType(const TypenameType *T) const {
Douglas Gregor17343172009-04-01 00:28:59 +0000705 if (const TemplateSpecializationType *TemplateId = T->getTemplateId()) {
706 // When the typename type refers to a template-id, the template-id
707 // is dependent and has enough information to instantiate the
708 // result of the typename type. Since we don't care about keeping
709 // the spelling of the typename type in template instantiations,
710 // we just instantiate the template-id.
Douglas Gregor8a5cb112009-06-26 21:40:05 +0000711 return InstantiateTemplateSpecializationType(TemplateId);
Douglas Gregor17343172009-04-01 00:28:59 +0000712 }
713
Douglas Gregord57959a2009-03-27 23:10:48 +0000714 NestedNameSpecifier *NNS
715 = SemaRef.InstantiateNestedNameSpecifier(T->getQualifier(),
716 SourceRange(Loc),
Douglas Gregor7e063902009-05-11 23:53:27 +0000717 TemplateArgs);
Douglas Gregord57959a2009-03-27 23:10:48 +0000718 if (!NNS)
719 return QualType();
720
Douglas Gregor17343172009-04-01 00:28:59 +0000721 return SemaRef.CheckTypenameType(NNS, *T->getIdentifier(), SourceRange(Loc));
Douglas Gregore4e5b052009-03-19 00:18:19 +0000722}
723
724QualType
725TemplateTypeInstantiator::
Douglas Gregor8a5cb112009-06-26 21:40:05 +0000726InstantiateObjCObjectPointerType(const ObjCObjectPointerType *T) const {
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000727 assert(false && "Objective-C types cannot be dependent");
728 return QualType();
729}
730
731QualType
732TemplateTypeInstantiator::
Douglas Gregor8a5cb112009-06-26 21:40:05 +0000733InstantiateObjCInterfaceType(const ObjCInterfaceType *T) const {
Douglas Gregorcd281c32009-02-28 00:25:32 +0000734 assert(false && "Objective-C types cannot be dependent");
Douglas Gregor99ebf652009-02-27 19:31:52 +0000735 return QualType();
736}
737
Douglas Gregorcd281c32009-02-28 00:25:32 +0000738/// \brief The actual implementation of Sema::InstantiateType().
739QualType TemplateTypeInstantiator::Instantiate(QualType T) const {
740 // If T is not a dependent type, there is nothing to do.
741 if (!T->isDependentType())
742 return T;
743
Douglas Gregor8a5cb112009-06-26 21:40:05 +0000744 QualType Result;
Douglas Gregorcd281c32009-02-28 00:25:32 +0000745 switch (T->getTypeClass()) {
746#define TYPE(Class, Base) \
747 case Type::Class: \
Douglas Gregor8a5cb112009-06-26 21:40:05 +0000748 Result = Instantiate##Class##Type(cast<Class##Type>(T.getTypePtr())); \
749 break;
Douglas Gregorcd281c32009-02-28 00:25:32 +0000750#define ABSTRACT_TYPE(Class, Base)
751#include "clang/AST/TypeNodes.def"
752 }
Douglas Gregor8a5cb112009-06-26 21:40:05 +0000753
754 // C++ [dcl.ref]p1:
755 // [...] Cv-qualified references are ill-formed except when
756 // the cv-qualifiers are introduced through the use of a
757 // typedef (7.1.3) or of a template type argument (14.3), in
758 // which case the cv-qualifiers are ignored.
759 //
760 // The same rule applies to function types.
Douglas Gregor9e9fae42009-07-22 20:02:25 +0000761 // FIXME: what about address-space and Objective-C GC qualifiers?
Douglas Gregor8a5cb112009-06-26 21:40:05 +0000762 if (!Result.isNull() && T.getCVRQualifiers() &&
763 !Result->isFunctionType() && !Result->isReferenceType())
764 Result = Result.getWithAdditionalQualifiers(T.getCVRQualifiers());
765 return Result;
Douglas Gregorcd281c32009-02-28 00:25:32 +0000766}
Douglas Gregor99ebf652009-02-27 19:31:52 +0000767
768/// \brief Instantiate the type T with a given set of template arguments.
769///
770/// This routine substitutes the given template arguments into the
771/// type T and produces the instantiated type.
772///
773/// \param T the type into which the template arguments will be
774/// substituted. If this type is not dependent, it will be returned
775/// immediately.
776///
777/// \param TemplateArgs the template arguments that will be
778/// substituted for the top-level template parameters within T.
779///
Douglas Gregor99ebf652009-02-27 19:31:52 +0000780/// \param Loc the location in the source code where this substitution
781/// is being performed. It will typically be the location of the
782/// declarator (if we're instantiating the type of some declaration)
783/// or the location of the type in the source code (if, e.g., we're
784/// instantiating the type of a cast expression).
785///
786/// \param Entity the name of the entity associated with a declaration
787/// being instantiated (if any). May be empty to indicate that there
788/// is no such entity (if, e.g., this is a type that occurs as part of
789/// a cast expression) or that the entity has no name (e.g., an
790/// unnamed function parameter).
791///
792/// \returns If the instantiation succeeds, the instantiated
793/// type. Otherwise, produces diagnostics and returns a NULL type.
794QualType Sema::InstantiateType(QualType T,
Douglas Gregor7e063902009-05-11 23:53:27 +0000795 const TemplateArgumentList &TemplateArgs,
Douglas Gregor99ebf652009-02-27 19:31:52 +0000796 SourceLocation Loc, DeclarationName Entity) {
Douglas Gregordf667e72009-03-10 20:44:00 +0000797 assert(!ActiveTemplateInstantiations.empty() &&
798 "Cannot perform an instantiation without some context on the "
799 "instantiation stack");
800
Douglas Gregor99ebf652009-02-27 19:31:52 +0000801 // If T is not a dependent type, there is nothing to do.
802 if (!T->isDependentType())
803 return T;
804
Douglas Gregor7e063902009-05-11 23:53:27 +0000805 TemplateTypeInstantiator Instantiator(*this, TemplateArgs, Loc, Entity);
Douglas Gregorcd281c32009-02-28 00:25:32 +0000806 return Instantiator(T);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000807}
Douglas Gregor2943aed2009-03-03 04:44:36 +0000808
809/// \brief Instantiate the base class specifiers of the given class
810/// template specialization.
811///
812/// Produces a diagnostic and returns true on error, returns false and
813/// attaches the instantiated base classes to the class template
814/// specialization if successful.
815bool
Douglas Gregord475b8d2009-03-25 21:17:03 +0000816Sema::InstantiateBaseSpecifiers(CXXRecordDecl *Instantiation,
817 CXXRecordDecl *Pattern,
Douglas Gregor7e063902009-05-11 23:53:27 +0000818 const TemplateArgumentList &TemplateArgs) {
Douglas Gregor2943aed2009-03-03 04:44:36 +0000819 bool Invalid = false;
Douglas Gregor0ca20ac2009-05-29 18:27:38 +0000820 llvm::SmallVector<CXXBaseSpecifier*, 4> InstantiatedBases;
Douglas Gregord475b8d2009-03-25 21:17:03 +0000821 for (ClassTemplateSpecializationDecl::base_class_iterator
822 Base = Pattern->bases_begin(), BaseEnd = Pattern->bases_end();
Douglas Gregor27b152f2009-03-10 18:52:44 +0000823 Base != BaseEnd; ++Base) {
Douglas Gregor2943aed2009-03-03 04:44:36 +0000824 if (!Base->getType()->isDependentType()) {
Fariborz Jahanian71c6e712009-07-22 17:41:53 +0000825 InstantiatedBases.push_back(new (Context) CXXBaseSpecifier(*Base));
Douglas Gregor2943aed2009-03-03 04:44:36 +0000826 continue;
827 }
828
829 QualType BaseType = InstantiateType(Base->getType(),
Douglas Gregor7e063902009-05-11 23:53:27 +0000830 TemplateArgs,
Douglas Gregor2943aed2009-03-03 04:44:36 +0000831 Base->getSourceRange().getBegin(),
832 DeclarationName());
833 if (BaseType.isNull()) {
834 Invalid = true;
835 continue;
836 }
837
838 if (CXXBaseSpecifier *InstantiatedBase
Douglas Gregord475b8d2009-03-25 21:17:03 +0000839 = CheckBaseSpecifier(Instantiation,
Douglas Gregor2943aed2009-03-03 04:44:36 +0000840 Base->getSourceRange(),
841 Base->isVirtual(),
842 Base->getAccessSpecifierAsWritten(),
843 BaseType,
844 /*FIXME: Not totally accurate */
845 Base->getSourceRange().getBegin()))
846 InstantiatedBases.push_back(InstantiatedBase);
847 else
848 Invalid = true;
849 }
850
Douglas Gregor27b152f2009-03-10 18:52:44 +0000851 if (!Invalid &&
Jay Foadbeaaccd2009-05-21 09:52:38 +0000852 AttachBaseSpecifiers(Instantiation, InstantiatedBases.data(),
Douglas Gregor2943aed2009-03-03 04:44:36 +0000853 InstantiatedBases.size()))
854 Invalid = true;
855
856 return Invalid;
857}
858
Douglas Gregord475b8d2009-03-25 21:17:03 +0000859/// \brief Instantiate the definition of a class from a given pattern.
860///
861/// \param PointOfInstantiation The point of instantiation within the
862/// source code.
863///
864/// \param Instantiation is the declaration whose definition is being
865/// instantiated. This will be either a class template specialization
866/// or a member class of a class template specialization.
867///
868/// \param Pattern is the pattern from which the instantiation
869/// occurs. This will be either the declaration of a class template or
870/// the declaration of a member class of a class template.
871///
872/// \param TemplateArgs The template arguments to be substituted into
873/// the pattern.
874///
Douglas Gregord475b8d2009-03-25 21:17:03 +0000875/// \returns true if an error occurred, false otherwise.
876bool
877Sema::InstantiateClass(SourceLocation PointOfInstantiation,
878 CXXRecordDecl *Instantiation, CXXRecordDecl *Pattern,
Douglas Gregor93dfdb12009-05-13 00:25:59 +0000879 const TemplateArgumentList &TemplateArgs,
880 bool ExplicitInstantiation) {
Douglas Gregord475b8d2009-03-25 21:17:03 +0000881 bool Invalid = false;
882
883 CXXRecordDecl *PatternDef
884 = cast_or_null<CXXRecordDecl>(Pattern->getDefinition(Context));
885 if (!PatternDef) {
886 if (Pattern == Instantiation->getInstantiatedFromMemberClass()) {
887 Diag(PointOfInstantiation,
888 diag::err_implicit_instantiate_member_undefined)
889 << Context.getTypeDeclType(Instantiation);
890 Diag(Pattern->getLocation(), diag::note_member_of_template_here);
891 } else {
Douglas Gregor93dfdb12009-05-13 00:25:59 +0000892 Diag(PointOfInstantiation, diag::err_template_instantiate_undefined)
893 << ExplicitInstantiation
Douglas Gregord475b8d2009-03-25 21:17:03 +0000894 << Context.getTypeDeclType(Instantiation);
895 Diag(Pattern->getLocation(), diag::note_template_decl_here);
896 }
897 return true;
898 }
899 Pattern = PatternDef;
900
Douglas Gregord048bb72009-03-25 21:23:52 +0000901 InstantiatingTemplate Inst(*this, PointOfInstantiation, Instantiation);
Douglas Gregord475b8d2009-03-25 21:17:03 +0000902 if (Inst)
903 return true;
904
905 // Enter the scope of this instantiation. We don't use
906 // PushDeclContext because we don't have a scope.
907 DeclContext *PreviousContext = CurContext;
908 CurContext = Instantiation;
909
910 // Start the definition of this instantiation.
911 Instantiation->startDefinition();
912
913 // Instantiate the base class specifiers.
Douglas Gregor7e063902009-05-11 23:53:27 +0000914 if (InstantiateBaseSpecifiers(Instantiation, Pattern, TemplateArgs))
Douglas Gregord475b8d2009-03-25 21:17:03 +0000915 Invalid = true;
916
Douglas Gregor0ca20ac2009-05-29 18:27:38 +0000917 llvm::SmallVector<DeclPtrTy, 4> Fields;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000918 for (RecordDecl::decl_iterator Member = Pattern->decls_begin(),
919 MemberEnd = Pattern->decls_end();
Douglas Gregor6ab35242009-04-09 21:40:53 +0000920 Member != MemberEnd; ++Member) {
Douglas Gregor7e063902009-05-11 23:53:27 +0000921 Decl *NewMember = InstantiateDecl(*Member, Instantiation, TemplateArgs);
Douglas Gregord475b8d2009-03-25 21:17:03 +0000922 if (NewMember) {
923 if (NewMember->isInvalidDecl())
924 Invalid = true;
925 else if (FieldDecl *Field = dyn_cast<FieldDecl>(NewMember))
Chris Lattnerb28317a2009-03-28 19:18:32 +0000926 Fields.push_back(DeclPtrTy::make(Field));
Douglas Gregord475b8d2009-03-25 21:17:03 +0000927 } else {
928 // FIXME: Eventually, a NULL return will mean that one of the
Mike Stump390b4cc2009-05-16 07:39:55 +0000929 // instantiations was a semantic disaster, and we'll want to set Invalid =
930 // true. For now, we expect to skip some members that we can't yet handle.
Douglas Gregord475b8d2009-03-25 21:17:03 +0000931 }
932 }
933
934 // Finish checking fields.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000935 ActOnFields(0, Instantiation->getLocation(), DeclPtrTy::make(Instantiation),
Jay Foadbeaaccd2009-05-21 09:52:38 +0000936 Fields.data(), Fields.size(), SourceLocation(), SourceLocation(),
Douglas Gregord475b8d2009-03-25 21:17:03 +0000937 0);
938
939 // Add any implicitly-declared members that we might need.
940 AddImplicitlyDeclaredMembersToClass(Instantiation);
941
942 // Exit the scope of this instantiation.
943 CurContext = PreviousContext;
944
Douglas Gregoraba43bb2009-05-26 20:50:29 +0000945 if (!Invalid)
946 Consumer.HandleTagDeclDefinition(Instantiation);
947
Douglas Gregora58861f2009-05-13 20:28:22 +0000948 // If this is an explicit instantiation, instantiate our members, too.
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000949 if (!Invalid && ExplicitInstantiation) {
950 Inst.Clear();
Douglas Gregora58861f2009-05-13 20:28:22 +0000951 InstantiateClassMembers(PointOfInstantiation, Instantiation, TemplateArgs);
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000952 }
Douglas Gregora58861f2009-05-13 20:28:22 +0000953
Douglas Gregord475b8d2009-03-25 21:17:03 +0000954 return Invalid;
955}
956
Douglas Gregor2943aed2009-03-03 04:44:36 +0000957bool
958Sema::InstantiateClassTemplateSpecialization(
959 ClassTemplateSpecializationDecl *ClassTemplateSpec,
960 bool ExplicitInstantiation) {
961 // Perform the actual instantiation on the canonical declaration.
962 ClassTemplateSpec = cast<ClassTemplateSpecializationDecl>(
Argyrios Kyrtzidis97fbaa22009-07-18 00:34:25 +0000963 ClassTemplateSpec->getCanonicalDecl());
Douglas Gregor2943aed2009-03-03 04:44:36 +0000964
965 // We can only instantiate something that hasn't already been
966 // instantiated or specialized. Fail without any diagnostics: our
967 // caller will provide an error message.
968 if (ClassTemplateSpec->getSpecializationKind() != TSK_Undeclared)
969 return true;
970
Douglas Gregor2943aed2009-03-03 04:44:36 +0000971 ClassTemplateDecl *Template = ClassTemplateSpec->getSpecializedTemplate();
Douglas Gregord475b8d2009-03-25 21:17:03 +0000972 CXXRecordDecl *Pattern = Template->getTemplatedDecl();
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000973 const TemplateArgumentList *TemplateArgs
974 = &ClassTemplateSpec->getTemplateArgs();
975
Douglas Gregorc1efb3f2009-06-12 22:31:52 +0000976 // C++ [temp.class.spec.match]p1:
977 // When a class template is used in a context that requires an
978 // instantiation of the class, it is necessary to determine
979 // whether the instantiation is to be generated using the primary
980 // template or one of the partial specializations. This is done by
981 // matching the template arguments of the class template
982 // specialization with the template argument lists of the partial
983 // specializations.
Douglas Gregor199d9912009-06-05 00:53:49 +0000984 typedef std::pair<ClassTemplatePartialSpecializationDecl *,
985 TemplateArgumentList *> MatchResult;
986 llvm::SmallVector<MatchResult, 4> Matched;
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000987 for (llvm::FoldingSet<ClassTemplatePartialSpecializationDecl>::iterator
988 Partial = Template->getPartialSpecializations().begin(),
989 PartialEnd = Template->getPartialSpecializations().end();
990 Partial != PartialEnd;
991 ++Partial) {
Douglas Gregorf67875d2009-06-12 18:26:56 +0000992 TemplateDeductionInfo Info(Context);
993 if (TemplateDeductionResult Result
Douglas Gregor199d9912009-06-05 00:53:49 +0000994 = DeduceTemplateArguments(&*Partial,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000995 ClassTemplateSpec->getTemplateArgs(),
996 Info)) {
997 // FIXME: Store the failed-deduction information for use in
998 // diagnostics, later.
999 (void)Result;
1000 } else {
1001 Matched.push_back(std::make_pair(&*Partial, Info.take()));
1002 }
Douglas Gregorc8ab2562009-05-31 09:31:02 +00001003 }
1004
1005 if (Matched.size() == 1) {
Douglas Gregorc1efb3f2009-06-12 22:31:52 +00001006 // -- If exactly one matching specialization is found, the
1007 // instantiation is generated from that specialization.
Douglas Gregor199d9912009-06-05 00:53:49 +00001008 Pattern = Matched[0].first;
1009 TemplateArgs = Matched[0].second;
Douglas Gregorc8ab2562009-05-31 09:31:02 +00001010 } else if (Matched.size() > 1) {
Douglas Gregorc1efb3f2009-06-12 22:31:52 +00001011 // -- If more than one matching specialization is found, the
1012 // partial order rules (14.5.4.2) are used to determine
1013 // whether one of the specializations is more specialized
1014 // than the others. If none of the specializations is more
1015 // specialized than all of the other matching
1016 // specializations, then the use of the class template is
1017 // ambiguous and the program is ill-formed.
Douglas Gregorc8ab2562009-05-31 09:31:02 +00001018 // FIXME: Implement partial ordering of class template partial
1019 // specializations.
1020 Diag(ClassTemplateSpec->getLocation(),
1021 diag::unsup_template_partial_spec_ordering);
Douglas Gregorc1efb3f2009-06-12 22:31:52 +00001022 } else {
1023 // -- If no matches are found, the instantiation is generated
1024 // from the primary template.
1025
1026 // Since we initialized the pattern and template arguments from
1027 // the primary template, there is nothing more we need to do here.
Douglas Gregorc8ab2562009-05-31 09:31:02 +00001028 }
Douglas Gregor2943aed2009-03-03 04:44:36 +00001029
1030 // Note that this is an instantiation.
1031 ClassTemplateSpec->setSpecializationKind(
1032 ExplicitInstantiation? TSK_ExplicitInstantiation
1033 : TSK_ImplicitInstantiation);
1034
Douglas Gregor199d9912009-06-05 00:53:49 +00001035 bool Result = InstantiateClass(ClassTemplateSpec->getLocation(),
1036 ClassTemplateSpec, Pattern, *TemplateArgs,
1037 ExplicitInstantiation);
1038
1039 for (unsigned I = 0, N = Matched.size(); I != N; ++I) {
1040 // FIXME: Implement TemplateArgumentList::Destroy!
1041 // if (Matched[I].first != Pattern)
1042 // Matched[I].second->Destroy(Context);
1043 }
1044
1045 return Result;
Douglas Gregor2943aed2009-03-03 04:44:36 +00001046}
Douglas Gregor5953d8b2009-03-19 17:26:29 +00001047
Douglas Gregora58861f2009-05-13 20:28:22 +00001048/// \brief Instantiate the definitions of all of the member of the
1049/// given class, which is an instantiation of a class template or a
1050/// member class of a template.
1051void
1052Sema::InstantiateClassMembers(SourceLocation PointOfInstantiation,
1053 CXXRecordDecl *Instantiation,
1054 const TemplateArgumentList &TemplateArgs) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001055 for (DeclContext::decl_iterator D = Instantiation->decls_begin(),
1056 DEnd = Instantiation->decls_end();
Douglas Gregora58861f2009-05-13 20:28:22 +00001057 D != DEnd; ++D) {
1058 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(*D)) {
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +00001059 if (!Function->getBody())
Douglas Gregorf3e7ce42009-05-18 17:01:57 +00001060 InstantiateFunctionDefinition(PointOfInstantiation, Function);
Douglas Gregora58861f2009-05-13 20:28:22 +00001061 } else if (VarDecl *Var = dyn_cast<VarDecl>(*D)) {
1062 const VarDecl *Def = 0;
1063 if (!Var->getDefinition(Def))
1064 InstantiateVariableDefinition(Var);
1065 } else if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(*D)) {
1066 if (!Record->isInjectedClassName() && !Record->getDefinition(Context)) {
1067 assert(Record->getInstantiatedFromMemberClass() &&
1068 "Missing instantiated-from-template information");
Douglas Gregorf3e7ce42009-05-18 17:01:57 +00001069 InstantiateClass(PointOfInstantiation, Record,
Douglas Gregora58861f2009-05-13 20:28:22 +00001070 Record->getInstantiatedFromMemberClass(),
1071 TemplateArgs, true);
1072 }
1073 }
1074 }
1075}
1076
1077/// \brief Instantiate the definitions of all of the members of the
1078/// given class template specialization, which was named as part of an
1079/// explicit instantiation.
1080void Sema::InstantiateClassTemplateSpecializationMembers(
1081 SourceLocation PointOfInstantiation,
1082 ClassTemplateSpecializationDecl *ClassTemplateSpec) {
1083 // C++0x [temp.explicit]p7:
1084 // An explicit instantiation that names a class template
1085 // specialization is an explicit instantion of the same kind
1086 // (declaration or definition) of each of its members (not
1087 // including members inherited from base classes) that has not
1088 // been previously explicitly specialized in the translation unit
1089 // containing the explicit instantiation, except as described
1090 // below.
1091 InstantiateClassMembers(PointOfInstantiation, ClassTemplateSpec,
1092 ClassTemplateSpec->getTemplateArgs());
1093}
1094
Douglas Gregorab452ba2009-03-26 23:50:42 +00001095/// \brief Instantiate a nested-name-specifier.
1096NestedNameSpecifier *
1097Sema::InstantiateNestedNameSpecifier(NestedNameSpecifier *NNS,
1098 SourceRange Range,
Douglas Gregor7e063902009-05-11 23:53:27 +00001099 const TemplateArgumentList &TemplateArgs) {
Douglas Gregorab452ba2009-03-26 23:50:42 +00001100 // Instantiate the prefix of this nested name specifier.
1101 NestedNameSpecifier *Prefix = NNS->getPrefix();
1102 if (Prefix) {
Douglas Gregor7e063902009-05-11 23:53:27 +00001103 Prefix = InstantiateNestedNameSpecifier(Prefix, Range, TemplateArgs);
Douglas Gregorab452ba2009-03-26 23:50:42 +00001104 if (!Prefix)
1105 return 0;
Douglas Gregor5953d8b2009-03-19 17:26:29 +00001106 }
1107
Douglas Gregorab452ba2009-03-26 23:50:42 +00001108 switch (NNS->getKind()) {
Douglas Gregor17343172009-04-01 00:28:59 +00001109 case NestedNameSpecifier::Identifier: {
1110 assert(Prefix &&
1111 "Can't have an identifier nested-name-specifier with no prefix");
1112 CXXScopeSpec SS;
1113 // FIXME: The source location information is all wrong.
1114 SS.setRange(Range);
1115 SS.setScopeRep(Prefix);
1116 return static_cast<NestedNameSpecifier *>(
1117 ActOnCXXNestedNameSpecifier(0, SS,
1118 Range.getEnd(),
1119 Range.getEnd(),
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001120 *NNS->getAsIdentifier()));
Douglas Gregorab452ba2009-03-26 23:50:42 +00001121 break;
Douglas Gregor17343172009-04-01 00:28:59 +00001122 }
Douglas Gregorab452ba2009-03-26 23:50:42 +00001123
1124 case NestedNameSpecifier::Namespace:
1125 case NestedNameSpecifier::Global:
1126 return NNS;
1127
1128 case NestedNameSpecifier::TypeSpecWithTemplate:
1129 case NestedNameSpecifier::TypeSpec: {
1130 QualType T = QualType(NNS->getAsType(), 0);
1131 if (!T->isDependentType())
1132 return NNS;
1133
Douglas Gregor7e063902009-05-11 23:53:27 +00001134 T = InstantiateType(T, TemplateArgs, Range.getBegin(), DeclarationName());
Douglas Gregorab452ba2009-03-26 23:50:42 +00001135 if (T.isNull())
1136 return 0;
1137
Eli Friedman923f7532009-06-13 04:51:30 +00001138 if (T->isDependentType() || T->isRecordType() ||
Douglas Gregord57959a2009-03-27 23:10:48 +00001139 (getLangOptions().CPlusPlus0x && T->isEnumeralType())) {
Douglas Gregor17343172009-04-01 00:28:59 +00001140 assert(T.getCVRQualifiers() == 0 && "Can't get cv-qualifiers here");
Douglas Gregord57959a2009-03-27 23:10:48 +00001141 return NestedNameSpecifier::Create(Context, Prefix,
Douglas Gregorab452ba2009-03-26 23:50:42 +00001142 NNS->getKind() == NestedNameSpecifier::TypeSpecWithTemplate,
Douglas Gregord57959a2009-03-27 23:10:48 +00001143 T.getTypePtr());
1144 }
1145
1146 Diag(Range.getBegin(), diag::err_nested_name_spec_non_tag) << T;
1147 return 0;
Douglas Gregorab452ba2009-03-26 23:50:42 +00001148 }
1149 }
1150
Douglas Gregord57959a2009-03-27 23:10:48 +00001151 // Required to silence a GCC warning
Douglas Gregorab452ba2009-03-26 23:50:42 +00001152 return 0;
Douglas Gregor5953d8b2009-03-19 17:26:29 +00001153}
Douglas Gregorde650ae2009-03-31 18:38:02 +00001154
1155TemplateName
1156Sema::InstantiateTemplateName(TemplateName Name, SourceLocation Loc,
Douglas Gregor7e063902009-05-11 23:53:27 +00001157 const TemplateArgumentList &TemplateArgs) {
Douglas Gregorde650ae2009-03-31 18:38:02 +00001158 if (TemplateTemplateParmDecl *TTP
1159 = dyn_cast_or_null<TemplateTemplateParmDecl>(
1160 Name.getAsTemplateDecl())) {
1161 assert(TTP->getDepth() == 0 &&
1162 "Cannot reduce depth of a template template parameter");
Douglas Gregor9bde7732009-03-31 20:22:05 +00001163 assert(TemplateArgs[TTP->getPosition()].getAsDecl() &&
Douglas Gregorde650ae2009-03-31 18:38:02 +00001164 "Wrong kind of template template argument");
1165 ClassTemplateDecl *ClassTemplate
1166 = dyn_cast<ClassTemplateDecl>(
1167 TemplateArgs[TTP->getPosition()].getAsDecl());
Douglas Gregor9bde7732009-03-31 20:22:05 +00001168 assert(ClassTemplate && "Expected a class template");
Douglas Gregorde650ae2009-03-31 18:38:02 +00001169 if (QualifiedTemplateName *QTN = Name.getAsQualifiedTemplateName()) {
1170 NestedNameSpecifier *NNS
1171 = InstantiateNestedNameSpecifier(QTN->getQualifier(),
1172 /*FIXME=*/SourceRange(Loc),
Douglas Gregor7e063902009-05-11 23:53:27 +00001173 TemplateArgs);
Douglas Gregorde650ae2009-03-31 18:38:02 +00001174 if (NNS)
1175 return Context.getQualifiedTemplateName(NNS,
1176 QTN->hasTemplateKeyword(),
1177 ClassTemplate);
1178 }
1179
1180 return TemplateName(ClassTemplate);
1181 } else if (DependentTemplateName *DTN = Name.getAsDependentTemplateName()) {
1182 NestedNameSpecifier *NNS
1183 = InstantiateNestedNameSpecifier(DTN->getQualifier(),
1184 /*FIXME=*/SourceRange(Loc),
Douglas Gregor7e063902009-05-11 23:53:27 +00001185 TemplateArgs);
Douglas Gregorde650ae2009-03-31 18:38:02 +00001186
1187 if (!NNS) // FIXME: Not the best recovery strategy.
1188 return Name;
1189
1190 if (NNS->isDependent())
1191 return Context.getDependentTemplateName(NNS, DTN->getName());
1192
1193 // Somewhat redundant with ActOnDependentTemplateName.
1194 CXXScopeSpec SS;
1195 SS.setRange(SourceRange(Loc));
1196 SS.setScopeRep(NNS);
1197 TemplateTy Template;
1198 TemplateNameKind TNK = isTemplateName(*DTN->getName(), 0, Template, &SS);
1199 if (TNK == TNK_Non_template) {
1200 Diag(Loc, diag::err_template_kw_refers_to_non_template)
1201 << DTN->getName();
1202 return Name;
1203 } else if (TNK == TNK_Function_template) {
1204 Diag(Loc, diag::err_template_kw_refers_to_non_template)
1205 << DTN->getName();
1206 return Name;
1207 }
1208
1209 return Template.getAsVal<TemplateName>();
1210 }
1211
1212
1213
Mike Stump390b4cc2009-05-16 07:39:55 +00001214 // FIXME: Even if we're referring to a Decl that isn't a template template
1215 // parameter, we may need to instantiate the outer contexts of that
1216 // Decl. However, this won't be needed until we implement member templates.
Douglas Gregorde650ae2009-03-31 18:38:02 +00001217 return Name;
1218}
Douglas Gregor91333002009-06-11 00:06:24 +00001219
1220TemplateArgument Sema::Instantiate(TemplateArgument Arg,
1221 const TemplateArgumentList &TemplateArgs) {
1222 switch (Arg.getKind()) {
1223 case TemplateArgument::Null:
1224 assert(false && "Should never have a NULL template argument");
1225 break;
1226
1227 case TemplateArgument::Type: {
1228 QualType T = InstantiateType(Arg.getAsType(), TemplateArgs,
1229 Arg.getLocation(), DeclarationName());
1230 if (T.isNull())
1231 return TemplateArgument();
1232
1233 return TemplateArgument(Arg.getLocation(), T);
1234 }
1235
1236 case TemplateArgument::Declaration:
1237 // FIXME: Template instantiation for template template parameters.
1238 return Arg;
1239
1240 case TemplateArgument::Integral:
1241 return Arg;
1242
1243 case TemplateArgument::Expression: {
Douglas Gregorac7610d2009-06-22 20:57:11 +00001244 // Template argument expressions are not potentially evaluated.
1245 EnterExpressionEvaluationContext Unevaluated(*this, Action::Unevaluated);
1246
Douglas Gregor91333002009-06-11 00:06:24 +00001247 Sema::OwningExprResult E = InstantiateExpr(Arg.getAsExpr(), TemplateArgs);
1248 if (E.isInvalid())
1249 return TemplateArgument();
1250 return TemplateArgument(E.takeAs<Expr>());
1251 }
Anders Carlssond01b1da2009-06-15 17:04:53 +00001252
1253 case TemplateArgument::Pack:
1254 assert(0 && "FIXME: Implement!");
1255 break;
Douglas Gregor91333002009-06-11 00:06:24 +00001256 }
1257
1258 assert(false && "Unhandled template argument kind");
1259 return TemplateArgument();
1260}