blob: f05323b511be2b88d0b6094f3c6e64cde41452bf [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,
103 ClassTemplatePartialSpecializationDecl *PartialSpec,
104 const TemplateArgument *TemplateArgs,
105 unsigned NumTemplateArgs,
106 SourceRange InstantiationRange)
107 : SemaRef(SemaRef) {
108
109 Invalid = CheckInstantiationDepth(PointOfInstantiation,
110 InstantiationRange);
111 if (!Invalid) {
112 ActiveTemplateInstantiation Inst;
113 Inst.Kind
114 = ActiveTemplateInstantiation::PartialSpecDeductionInstantiation;
115 Inst.PointOfInstantiation = PointOfInstantiation;
116 Inst.Entity = reinterpret_cast<uintptr_t>(PartialSpec);
117 Inst.TemplateArgs = TemplateArgs;
118 Inst.NumTemplateArgs = NumTemplateArgs;
119 Inst.InstantiationRange = InstantiationRange;
120 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
121 Invalid = false;
122 }
123}
124
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000125void Sema::InstantiatingTemplate::Clear() {
126 if (!Invalid) {
Douglas Gregor26dce442009-03-10 00:06:19 +0000127 SemaRef.ActiveTemplateInstantiations.pop_back();
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000128 Invalid = true;
129 }
Douglas Gregor26dce442009-03-10 00:06:19 +0000130}
131
Douglas Gregordf667e72009-03-10 20:44:00 +0000132bool Sema::InstantiatingTemplate::CheckInstantiationDepth(
133 SourceLocation PointOfInstantiation,
134 SourceRange InstantiationRange) {
135 if (SemaRef.ActiveTemplateInstantiations.size()
136 <= SemaRef.getLangOptions().InstantiationDepth)
137 return false;
138
139 SemaRef.Diag(PointOfInstantiation,
140 diag::err_template_recursion_depth_exceeded)
141 << SemaRef.getLangOptions().InstantiationDepth
142 << InstantiationRange;
143 SemaRef.Diag(PointOfInstantiation, diag::note_template_recursion_depth)
144 << SemaRef.getLangOptions().InstantiationDepth;
145 return true;
146}
147
Douglas Gregoree1828a2009-03-10 18:03:33 +0000148/// \brief Prints the current instantiation stack through a series of
149/// notes.
150void Sema::PrintInstantiationStack() {
151 for (llvm::SmallVector<ActiveTemplateInstantiation, 16>::reverse_iterator
152 Active = ActiveTemplateInstantiations.rbegin(),
153 ActiveEnd = ActiveTemplateInstantiations.rend();
154 Active != ActiveEnd;
155 ++Active) {
Douglas Gregordf667e72009-03-10 20:44:00 +0000156 switch (Active->Kind) {
157 case ActiveTemplateInstantiation::TemplateInstantiation: {
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000158 Decl *D = reinterpret_cast<Decl *>(Active->Entity);
159 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) {
160 unsigned DiagID = diag::note_template_member_class_here;
161 if (isa<ClassTemplateSpecializationDecl>(Record))
162 DiagID = diag::note_template_class_instantiation_here;
163 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
164 DiagID)
165 << Context.getTypeDeclType(Record)
166 << Active->InstantiationRange;
167 } else {
168 FunctionDecl *Function = cast<FunctionDecl>(D);
Douglas Gregor1637be72009-06-26 00:10:03 +0000169 unsigned DiagID;
170 if (Function->getPrimaryTemplate())
171 DiagID = diag::note_function_template_spec_here;
172 else
173 DiagID = diag::note_template_member_function_here;
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000174 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
175 DiagID)
176 << Function
177 << Active->InstantiationRange;
178 }
Douglas Gregordf667e72009-03-10 20:44:00 +0000179 break;
180 }
181
182 case ActiveTemplateInstantiation::DefaultTemplateArgumentInstantiation: {
183 TemplateDecl *Template = cast<TemplateDecl>((Decl *)Active->Entity);
184 std::string TemplateArgsStr
Douglas Gregor7532dc62009-03-30 22:58:21 +0000185 = TemplateSpecializationType::PrintTemplateArgumentList(
Douglas Gregordf667e72009-03-10 20:44:00 +0000186 Active->TemplateArgs,
Douglas Gregord249e1d1f2009-05-29 20:38:28 +0000187 Active->NumTemplateArgs,
188 Context.PrintingPolicy);
Douglas Gregordf667e72009-03-10 20:44:00 +0000189 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
190 diag::note_default_arg_instantiation_here)
191 << (Template->getNameAsString() + TemplateArgsStr)
192 << Active->InstantiationRange;
193 break;
194 }
Douglas Gregor637a4092009-06-10 23:47:09 +0000195
196 case ActiveTemplateInstantiation::PartialSpecDeductionInstantiation: {
197 ClassTemplatePartialSpecializationDecl *PartialSpec
198 = cast<ClassTemplatePartialSpecializationDecl>((Decl *)Active->Entity);
Douglas Gregor637a4092009-06-10 23:47:09 +0000199 // FIXME: The active template instantiation's template arguments
200 // are interesting, too. We should add something like [with T =
201 // foo, U = bar, etc.] to the string.
202 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
203 diag::note_partial_spec_deduct_instantiation_here)
Douglas Gregor02cbbd22009-06-11 18:10:32 +0000204 << Context.getTypeDeclType(PartialSpec)
Douglas Gregor637a4092009-06-10 23:47:09 +0000205 << Active->InstantiationRange;
206 break;
207 }
208
Douglas Gregordf667e72009-03-10 20:44:00 +0000209 }
Douglas Gregoree1828a2009-03-10 18:03:33 +0000210 }
211}
212
Douglas Gregor5e9f35c2009-06-14 07:33:30 +0000213bool Sema::isSFINAEContext() const {
214 using llvm::SmallVector;
215 for (SmallVector<ActiveTemplateInstantiation, 16>::const_reverse_iterator
216 Active = ActiveTemplateInstantiations.rbegin(),
217 ActiveEnd = ActiveTemplateInstantiations.rend();
218 Active != ActiveEnd;
219 ++Active) {
220
221 switch(Active->Kind) {
222 case ActiveTemplateInstantiation::PartialSpecDeductionInstantiation:
223 // We're in a template argument deduction context, so SFINAE
224 // applies.
225 return true;
226
227 case ActiveTemplateInstantiation::DefaultTemplateArgumentInstantiation:
228 // A default template argument instantiation may or may not be a
229 // SFINAE context; look further up the stack.
230 break;
231
232 case ActiveTemplateInstantiation::TemplateInstantiation:
233 // This is a template instantiation, so there is no SFINAE.
234 return false;
235 }
236 }
237
238 return false;
239}
240
Douglas Gregor99ebf652009-02-27 19:31:52 +0000241//===----------------------------------------------------------------------===/
242// Template Instantiation for Types
243//===----------------------------------------------------------------------===/
Douglas Gregorcd281c32009-02-28 00:25:32 +0000244namespace {
245 class VISIBILITY_HIDDEN TemplateTypeInstantiator {
246 Sema &SemaRef;
Douglas Gregor7e063902009-05-11 23:53:27 +0000247 const TemplateArgumentList &TemplateArgs;
Douglas Gregorcd281c32009-02-28 00:25:32 +0000248 SourceLocation Loc;
249 DeclarationName Entity;
Douglas Gregor99ebf652009-02-27 19:31:52 +0000250
Douglas Gregorcd281c32009-02-28 00:25:32 +0000251 public:
252 TemplateTypeInstantiator(Sema &SemaRef,
Douglas Gregor7e063902009-05-11 23:53:27 +0000253 const TemplateArgumentList &TemplateArgs,
Douglas Gregorcd281c32009-02-28 00:25:32 +0000254 SourceLocation Loc,
255 DeclarationName Entity)
256 : SemaRef(SemaRef), TemplateArgs(TemplateArgs),
Douglas Gregor7e063902009-05-11 23:53:27 +0000257 Loc(Loc), Entity(Entity) { }
Douglas Gregorcd281c32009-02-28 00:25:32 +0000258
259 QualType operator()(QualType T) const { return Instantiate(T); }
260
261 QualType Instantiate(QualType T) const;
262
263 // Declare instantiate functions for each type.
264#define TYPE(Class, Base) \
Douglas Gregor8a5cb112009-06-26 21:40:05 +0000265 QualType Instantiate##Class##Type(const Class##Type *T) const;
Douglas Gregorcd281c32009-02-28 00:25:32 +0000266#define ABSTRACT_TYPE(Class, Base)
267#include "clang/AST/TypeNodes.def"
268 };
269}
270
271QualType
Douglas Gregor8a5cb112009-06-26 21:40:05 +0000272TemplateTypeInstantiator::InstantiateExtQualType(const ExtQualType *T) const {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000273 // FIXME: Implement this
274 assert(false && "Cannot instantiate ExtQualType yet");
275 return QualType();
276}
277
Douglas Gregorcd281c32009-02-28 00:25:32 +0000278QualType
Douglas Gregor8a5cb112009-06-26 21:40:05 +0000279TemplateTypeInstantiator::InstantiateBuiltinType(const BuiltinType *T) const {
Douglas Gregor724651c2009-02-28 01:04:19 +0000280 assert(false && "Builtin types are not dependent and cannot be instantiated");
Douglas Gregor8a5cb112009-06-26 21:40:05 +0000281 return QualType(T, 0);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000282}
283
Douglas Gregorcd281c32009-02-28 00:25:32 +0000284QualType
285TemplateTypeInstantiator::
Douglas Gregor8a5cb112009-06-26 21:40:05 +0000286InstantiateFixedWidthIntType(const FixedWidthIntType *T) const {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000287 // FIXME: Implement this
288 assert(false && "Cannot instantiate FixedWidthIntType yet");
289 return QualType();
290}
291
Douglas Gregorcd281c32009-02-28 00:25:32 +0000292QualType
Douglas Gregor8a5cb112009-06-26 21:40:05 +0000293TemplateTypeInstantiator::InstantiateComplexType(const ComplexType *T) const {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000294 // FIXME: Implement this
295 assert(false && "Cannot instantiate ComplexType yet");
296 return QualType();
297}
298
Douglas Gregorcd281c32009-02-28 00:25:32 +0000299QualType
Douglas Gregor8a5cb112009-06-26 21:40:05 +0000300TemplateTypeInstantiator::InstantiatePointerType(const PointerType *T) const {
Douglas Gregorcd281c32009-02-28 00:25:32 +0000301 QualType PointeeType = Instantiate(T->getPointeeType());
302 if (PointeeType.isNull())
303 return QualType();
304
Douglas Gregor8a5cb112009-06-26 21:40:05 +0000305 return SemaRef.BuildPointerType(PointeeType, 0, Loc, Entity);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000306}
307
Douglas Gregorcd281c32009-02-28 00:25:32 +0000308QualType
Douglas Gregor8a5cb112009-06-26 21:40:05 +0000309TemplateTypeInstantiator::InstantiateBlockPointerType(
310 const BlockPointerType *T) const {
Anders Carlsson859ba502009-06-12 16:23:10 +0000311 QualType PointeeType = Instantiate(T->getPointeeType());
312 if (PointeeType.isNull())
313 return QualType();
314
Douglas Gregor8a5cb112009-06-26 21:40:05 +0000315 return SemaRef.BuildBlockPointerType(PointeeType, 0, Loc, Entity);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000316}
317
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000318QualType
319TemplateTypeInstantiator::InstantiateLValueReferenceType(
Douglas Gregor8a5cb112009-06-26 21:40:05 +0000320 const LValueReferenceType *T) const {
Douglas Gregorcd281c32009-02-28 00:25:32 +0000321 QualType ReferentType = Instantiate(T->getPointeeType());
322 if (ReferentType.isNull())
323 return QualType();
324
Douglas Gregor8a5cb112009-06-26 21:40:05 +0000325 return SemaRef.BuildReferenceType(ReferentType, true, 0, Loc, Entity);
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000326}
327
328QualType
329TemplateTypeInstantiator::InstantiateRValueReferenceType(
Douglas Gregor8a5cb112009-06-26 21:40:05 +0000330 const RValueReferenceType *T) const {
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000331 QualType ReferentType = Instantiate(T->getPointeeType());
332 if (ReferentType.isNull())
333 return QualType();
334
Douglas Gregor8a5cb112009-06-26 21:40:05 +0000335 return SemaRef.BuildReferenceType(ReferentType, false, 0, Loc, Entity);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000336}
337
Douglas Gregorcd281c32009-02-28 00:25:32 +0000338QualType
339TemplateTypeInstantiator::
Douglas Gregor8a5cb112009-06-26 21:40:05 +0000340InstantiateMemberPointerType(const MemberPointerType *T) const {
Douglas Gregor949bf692009-06-09 22:17:39 +0000341 QualType PointeeType = Instantiate(T->getPointeeType());
342 if (PointeeType.isNull())
343 return QualType();
344
345 QualType ClassType = Instantiate(QualType(T->getClass(), 0));
346 if (ClassType.isNull())
347 return QualType();
348
Douglas Gregor8a5cb112009-06-26 21:40:05 +0000349 return SemaRef.BuildMemberPointerType(PointeeType, ClassType, 0, Loc,
Douglas Gregor949bf692009-06-09 22:17:39 +0000350 Entity);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000351}
352
Douglas Gregorcd281c32009-02-28 00:25:32 +0000353QualType
354TemplateTypeInstantiator::
Douglas Gregor8a5cb112009-06-26 21:40:05 +0000355InstantiateConstantArrayType(const ConstantArrayType *T) const {
Douglas Gregorcd281c32009-02-28 00:25:32 +0000356 QualType ElementType = Instantiate(T->getElementType());
357 if (ElementType.isNull())
358 return ElementType;
359
360 // Build a temporary integer literal to specify the size for
361 // BuildArrayType. Since we have already checked the size as part of
362 // creating the dependent array type in the first place, we know
Douglas Gregorff668032009-05-13 18:28:20 +0000363 // there aren't any errors. However, we do need to determine what
364 // C++ type to give the size expression.
365 llvm::APInt Size = T->getSize();
366 QualType Types[] = {
367 SemaRef.Context.UnsignedCharTy, SemaRef.Context.UnsignedShortTy,
368 SemaRef.Context.UnsignedIntTy, SemaRef.Context.UnsignedLongTy,
369 SemaRef.Context.UnsignedLongLongTy, SemaRef.Context.UnsignedInt128Ty
370 };
371 const unsigned NumTypes = sizeof(Types) / sizeof(QualType);
372 QualType SizeType;
373 for (unsigned I = 0; I != NumTypes; ++I)
374 if (Size.getBitWidth() == SemaRef.Context.getIntWidth(Types[I])) {
375 SizeType = Types[I];
376 break;
377 }
378
379 if (SizeType.isNull())
380 SizeType = SemaRef.Context.getFixedWidthIntType(Size.getBitWidth(), false);
381
382 IntegerLiteral ArraySize(Size, SizeType, Loc);
Douglas Gregorcd281c32009-02-28 00:25:32 +0000383 return SemaRef.BuildArrayType(ElementType, T->getSizeModifier(),
384 &ArraySize, T->getIndexTypeQualifier(),
385 Loc, Entity);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000386}
387
Douglas Gregorcd281c32009-02-28 00:25:32 +0000388QualType
389TemplateTypeInstantiator::
Douglas Gregor8a5cb112009-06-26 21:40:05 +0000390InstantiateIncompleteArrayType(const IncompleteArrayType *T) const {
Douglas Gregorcd281c32009-02-28 00:25:32 +0000391 QualType ElementType = Instantiate(T->getElementType());
392 if (ElementType.isNull())
393 return ElementType;
394
395 return SemaRef.BuildArrayType(ElementType, T->getSizeModifier(),
396 0, T->getIndexTypeQualifier(),
397 Loc, Entity);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000398}
399
Douglas Gregorcd281c32009-02-28 00:25:32 +0000400QualType
401TemplateTypeInstantiator::
Douglas Gregor8a5cb112009-06-26 21:40:05 +0000402InstantiateVariableArrayType(const VariableArrayType *T) const {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000403 // FIXME: Implement this
404 assert(false && "Cannot instantiate VariableArrayType yet");
405 return QualType();
406}
407
Douglas Gregorcd281c32009-02-28 00:25:32 +0000408QualType
409TemplateTypeInstantiator::
Douglas Gregor8a5cb112009-06-26 21:40:05 +0000410InstantiateDependentSizedArrayType(const DependentSizedArrayType *T) const {
Anders Carlsson76b1c842009-03-15 20:12:13 +0000411 Expr *ArraySize = T->getSizeExpr();
412 assert(ArraySize->isValueDependent() &&
413 "dependent sized array types must have value dependent size expr");
414
415 // Instantiate the element type if needed
416 QualType ElementType = T->getElementType();
417 if (ElementType->isDependentType()) {
418 ElementType = Instantiate(ElementType);
419 if (ElementType.isNull())
420 return QualType();
421 }
422
423 // Instantiate the size expression
Douglas Gregorac7610d2009-06-22 20:57:11 +0000424 EnterExpressionEvaluationContext Unevaluated(SemaRef, Action::Unevaluated);
Anders Carlsson76b1c842009-03-15 20:12:13 +0000425 Sema::OwningExprResult InstantiatedArraySize =
Douglas Gregor7e063902009-05-11 23:53:27 +0000426 SemaRef.InstantiateExpr(ArraySize, TemplateArgs);
Anders Carlsson76b1c842009-03-15 20:12:13 +0000427 if (InstantiatedArraySize.isInvalid())
428 return QualType();
429
430 return SemaRef.BuildArrayType(ElementType, T->getSizeModifier(),
Anders Carlssone9146f22009-05-01 19:49:17 +0000431 InstantiatedArraySize.takeAs<Expr>(),
Anders Carlsson76b1c842009-03-15 20:12:13 +0000432 T->getIndexTypeQualifier(), Loc, Entity);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000433}
434
Douglas Gregorcd281c32009-02-28 00:25:32 +0000435QualType
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000436TemplateTypeInstantiator::
Douglas Gregor8a5cb112009-06-26 21:40:05 +0000437InstantiateDependentSizedExtVectorType(
438 const DependentSizedExtVectorType *T) const {
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000439
Douglas Gregorf6ddb732009-06-18 18:45:36 +0000440 // Instantiate the element type if needed.
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000441 QualType ElementType = T->getElementType();
442 if (ElementType->isDependentType()) {
443 ElementType = Instantiate(ElementType);
444 if (ElementType.isNull())
445 return QualType();
446 }
447
Douglas Gregorac7610d2009-06-22 20:57:11 +0000448 // The expression in a dependent-sized extended vector type is not
449 // potentially evaluated.
450 EnterExpressionEvaluationContext Unevaluated(SemaRef, Action::Unevaluated);
451
Douglas Gregorf6ddb732009-06-18 18:45:36 +0000452 // Instantiate the size expression.
453 const Expr *SizeExpr = T->getSizeExpr();
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000454 Sema::OwningExprResult InstantiatedArraySize =
Douglas Gregorf6ddb732009-06-18 18:45:36 +0000455 SemaRef.InstantiateExpr(const_cast<Expr *>(SizeExpr), TemplateArgs);
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000456 if (InstantiatedArraySize.isInvalid())
457 return QualType();
458
459 return SemaRef.BuildExtVectorType(ElementType,
460 SemaRef.Owned(
461 InstantiatedArraySize.takeAs<Expr>()),
462 T->getAttributeLoc());
463}
464
465QualType
Douglas Gregor8a5cb112009-06-26 21:40:05 +0000466TemplateTypeInstantiator::InstantiateVectorType(const VectorType *T) const {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000467 // FIXME: Implement this
468 assert(false && "Cannot instantiate VectorType yet");
469 return QualType();
470}
471
Douglas Gregorcd281c32009-02-28 00:25:32 +0000472QualType
Douglas Gregor8a5cb112009-06-26 21:40:05 +0000473TemplateTypeInstantiator::InstantiateExtVectorType(
474 const ExtVectorType *T) const {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000475 // FIXME: Implement this
476 assert(false && "Cannot instantiate ExtVectorType yet");
477 return QualType();
478}
479
Douglas Gregorcd281c32009-02-28 00:25:32 +0000480QualType
481TemplateTypeInstantiator::
Douglas Gregor8a5cb112009-06-26 21:40:05 +0000482InstantiateFunctionProtoType(const FunctionProtoType *T) const {
Douglas Gregor724651c2009-02-28 01:04:19 +0000483 QualType ResultType = Instantiate(T->getResultType());
484 if (ResultType.isNull())
485 return ResultType;
486
Douglas Gregor0ca20ac2009-05-29 18:27:38 +0000487 llvm::SmallVector<QualType, 4> ParamTypes;
Douglas Gregor724651c2009-02-28 01:04:19 +0000488 for (FunctionProtoType::arg_type_iterator Param = T->arg_type_begin(),
489 ParamEnd = T->arg_type_end();
490 Param != ParamEnd; ++Param) {
491 QualType P = Instantiate(*Param);
492 if (P.isNull())
493 return P;
494
495 ParamTypes.push_back(P);
496 }
497
Douglas Gregor02cbbd22009-06-11 18:10:32 +0000498 return SemaRef.BuildFunctionType(ResultType, ParamTypes.data(),
Douglas Gregor724651c2009-02-28 01:04:19 +0000499 ParamTypes.size(),
500 T->isVariadic(), T->getTypeQuals(),
501 Loc, Entity);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000502}
503
Douglas Gregorcd281c32009-02-28 00:25:32 +0000504QualType
505TemplateTypeInstantiator::
Douglas Gregor8a5cb112009-06-26 21:40:05 +0000506InstantiateFunctionNoProtoType(const FunctionNoProtoType *T) const {
Douglas Gregor724651c2009-02-28 01:04:19 +0000507 assert(false && "Functions without prototypes cannot be dependent.");
Douglas Gregor99ebf652009-02-27 19:31:52 +0000508 return QualType();
509}
510
Douglas Gregorcd281c32009-02-28 00:25:32 +0000511QualType
Douglas Gregor8a5cb112009-06-26 21:40:05 +0000512TemplateTypeInstantiator::InstantiateTypedefType(const TypedefType *T) const {
Douglas Gregor815215d2009-05-27 05:35:12 +0000513 TypedefDecl *Typedef
Douglas Gregored961e72009-05-27 17:54:46 +0000514 = cast_or_null<TypedefDecl>(
515 SemaRef.InstantiateCurrentDeclRef(T->getDecl()));
Douglas Gregor815215d2009-05-27 05:35:12 +0000516 if (!Typedef)
517 return QualType();
518
519 return SemaRef.Context.getTypeDeclType(Typedef);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000520}
521
Douglas Gregorcd281c32009-02-28 00:25:32 +0000522QualType
Douglas Gregor8a5cb112009-06-26 21:40:05 +0000523TemplateTypeInstantiator::InstantiateTypeOfExprType(
524 const TypeOfExprType *T) const {
Douglas Gregorac7610d2009-06-22 20:57:11 +0000525 // The expression in a typeof is not potentially evaluated.
526 EnterExpressionEvaluationContext Unevaluated(SemaRef, Action::Unevaluated);
527
Douglas Gregor5f8bd592009-05-26 22:09:24 +0000528 Sema::OwningExprResult E
529 = SemaRef.InstantiateExpr(T->getUnderlyingExpr(), TemplateArgs);
530 if (E.isInvalid())
531 return QualType();
532
Anders Carlssonaf017e62009-06-29 22:58:55 +0000533 return SemaRef.BuildTypeofExprType(E.takeAs<Expr>());
Douglas Gregor99ebf652009-02-27 19:31:52 +0000534}
535
Douglas Gregorcd281c32009-02-28 00:25:32 +0000536QualType
Douglas Gregor8a5cb112009-06-26 21:40:05 +0000537TemplateTypeInstantiator::InstantiateTypeOfType(const TypeOfType *T) const {
Douglas Gregor5f8bd592009-05-26 22:09:24 +0000538 QualType Underlying = Instantiate(T->getUnderlyingType());
539 if (Underlying.isNull())
540 return QualType();
541
542 return SemaRef.Context.getTypeOfType(Underlying);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000543}
544
Anders Carlsson395b4752009-06-24 19:06:50 +0000545QualType
Douglas Gregor8a5cb112009-06-26 21:40:05 +0000546TemplateTypeInstantiator::InstantiateDecltypeType(const DecltypeType *T) const {
Anders Carlsson87471f52009-06-26 03:02:18 +0000547 // C++0x [dcl.type.simple]p4:
548 // The operand of the decltype specifier is an unevaluated operand.
549 EnterExpressionEvaluationContext Unevaluated(SemaRef,
550 Action::Unevaluated);
551
Anders Carlsson395b4752009-06-24 19:06:50 +0000552 Sema::OwningExprResult E
553 = SemaRef.InstantiateExpr(T->getUnderlyingExpr(), TemplateArgs);
554
555 if (E.isInvalid())
556 return QualType();
557
Anders Carlssonaf017e62009-06-29 22:58:55 +0000558 return SemaRef.BuildDecltypeType(E.takeAs<Expr>());
Anders Carlsson395b4752009-06-24 19:06:50 +0000559}
560
Douglas Gregorcd281c32009-02-28 00:25:32 +0000561QualType
Douglas Gregor8a5cb112009-06-26 21:40:05 +0000562TemplateTypeInstantiator::InstantiateRecordType(const RecordType *T) const {
Douglas Gregor815215d2009-05-27 05:35:12 +0000563 RecordDecl *Record
Douglas Gregored961e72009-05-27 17:54:46 +0000564 = cast_or_null<RecordDecl>(SemaRef.InstantiateCurrentDeclRef(T->getDecl()));
Douglas Gregor815215d2009-05-27 05:35:12 +0000565 if (!Record)
566 return QualType();
567
568 return SemaRef.Context.getTypeDeclType(Record);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000569}
570
Douglas Gregorcd281c32009-02-28 00:25:32 +0000571QualType
Douglas Gregor8a5cb112009-06-26 21:40:05 +0000572TemplateTypeInstantiator::InstantiateEnumType(const EnumType *T) const {
Douglas Gregor815215d2009-05-27 05:35:12 +0000573 EnumDecl *Enum
Douglas Gregored961e72009-05-27 17:54:46 +0000574 = cast_or_null<EnumDecl>(SemaRef.InstantiateCurrentDeclRef(T->getDecl()));
Douglas Gregor815215d2009-05-27 05:35:12 +0000575 if (!Enum)
576 return QualType();
577
578 return SemaRef.Context.getTypeDeclType(Enum);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000579}
580
Douglas Gregorcd281c32009-02-28 00:25:32 +0000581QualType
582TemplateTypeInstantiator::
Douglas Gregor8a5cb112009-06-26 21:40:05 +0000583InstantiateTemplateTypeParmType(const TemplateTypeParmType *T) const {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000584 if (T->getDepth() == 0) {
585 // Replace the template type parameter with its corresponding
586 // template argument.
Douglas Gregor99ebf652009-02-27 19:31:52 +0000587 assert(TemplateArgs[T->getIndex()].getKind() == TemplateArgument::Type &&
588 "Template argument kind mismatch");
Douglas Gregor8a5cb112009-06-26 21:40:05 +0000589 return TemplateArgs[T->getIndex()].getAsType();
Douglas Gregor99ebf652009-02-27 19:31:52 +0000590 }
591
592 // The template type parameter comes from an inner template (e.g.,
593 // the template parameter list of a member template inside the
594 // template we are instantiating). Create a new template type
595 // parameter with the template "level" reduced by one.
596 return SemaRef.Context.getTemplateTypeParmType(T->getDepth() - 1,
597 T->getIndex(),
Anders Carlsson76e4ce42009-06-16 00:30:48 +0000598 T->isParameterPack(),
Douglas Gregor8a5cb112009-06-26 21:40:05 +0000599 T->getName());
Douglas Gregor99ebf652009-02-27 19:31:52 +0000600}
601
Douglas Gregorcd281c32009-02-28 00:25:32 +0000602QualType
603TemplateTypeInstantiator::
Douglas Gregor7532dc62009-03-30 22:58:21 +0000604InstantiateTemplateSpecializationType(
Douglas Gregor8a5cb112009-06-26 21:40:05 +0000605 const TemplateSpecializationType *T) const {
Douglas Gregor0ca20ac2009-05-29 18:27:38 +0000606 llvm::SmallVector<TemplateArgument, 4> InstantiatedTemplateArgs;
Douglas Gregor40808ce2009-03-09 23:48:35 +0000607 InstantiatedTemplateArgs.reserve(T->getNumArgs());
Douglas Gregor7532dc62009-03-30 22:58:21 +0000608 for (TemplateSpecializationType::iterator Arg = T->begin(), ArgEnd = T->end();
Douglas Gregor40808ce2009-03-09 23:48:35 +0000609 Arg != ArgEnd; ++Arg) {
Douglas Gregor91333002009-06-11 00:06:24 +0000610 TemplateArgument InstArg = SemaRef.Instantiate(*Arg, TemplateArgs);
611 if (InstArg.isNull())
612 return QualType();
Douglas Gregor40808ce2009-03-09 23:48:35 +0000613
Douglas Gregor91333002009-06-11 00:06:24 +0000614 InstantiatedTemplateArgs.push_back(InstArg);
Douglas Gregor40808ce2009-03-09 23:48:35 +0000615 }
616
Mike Stump390b4cc2009-05-16 07:39:55 +0000617 // FIXME: We're missing the locations of the template name, '<', and '>'.
Douglas Gregorde650ae2009-03-31 18:38:02 +0000618
619 TemplateName Name = SemaRef.InstantiateTemplateName(T->getTemplateName(),
620 Loc,
Douglas Gregor7e063902009-05-11 23:53:27 +0000621 TemplateArgs);
Douglas Gregorde650ae2009-03-31 18:38:02 +0000622
623 return SemaRef.CheckTemplateIdType(Name, Loc, SourceLocation(),
Douglas Gregor02cbbd22009-06-11 18:10:32 +0000624 InstantiatedTemplateArgs.data(),
Douglas Gregor7532dc62009-03-30 22:58:21 +0000625 InstantiatedTemplateArgs.size(),
626 SourceLocation());
Douglas Gregor99ebf652009-02-27 19:31:52 +0000627}
628
Douglas Gregorcd281c32009-02-28 00:25:32 +0000629QualType
630TemplateTypeInstantiator::
Douglas Gregor8a5cb112009-06-26 21:40:05 +0000631InstantiateQualifiedNameType(const QualifiedNameType *T) const {
Douglas Gregord57959a2009-03-27 23:10:48 +0000632 // When we instantiated a qualified name type, there's no point in
633 // keeping the qualification around in the instantiated result. So,
634 // just instantiate the named type.
635 return (*this)(T->getNamedType());
636}
637
638QualType
639TemplateTypeInstantiator::
Douglas Gregor8a5cb112009-06-26 21:40:05 +0000640InstantiateTypenameType(const TypenameType *T) const {
Douglas Gregor17343172009-04-01 00:28:59 +0000641 if (const TemplateSpecializationType *TemplateId = T->getTemplateId()) {
642 // When the typename type refers to a template-id, the template-id
643 // is dependent and has enough information to instantiate the
644 // result of the typename type. Since we don't care about keeping
645 // the spelling of the typename type in template instantiations,
646 // we just instantiate the template-id.
Douglas Gregor8a5cb112009-06-26 21:40:05 +0000647 return InstantiateTemplateSpecializationType(TemplateId);
Douglas Gregor17343172009-04-01 00:28:59 +0000648 }
649
Douglas Gregord57959a2009-03-27 23:10:48 +0000650 NestedNameSpecifier *NNS
651 = SemaRef.InstantiateNestedNameSpecifier(T->getQualifier(),
652 SourceRange(Loc),
Douglas Gregor7e063902009-05-11 23:53:27 +0000653 TemplateArgs);
Douglas Gregord57959a2009-03-27 23:10:48 +0000654 if (!NNS)
655 return QualType();
656
Douglas Gregor17343172009-04-01 00:28:59 +0000657 return SemaRef.CheckTypenameType(NNS, *T->getIdentifier(), SourceRange(Loc));
Douglas Gregore4e5b052009-03-19 00:18:19 +0000658}
659
660QualType
661TemplateTypeInstantiator::
Douglas Gregor8a5cb112009-06-26 21:40:05 +0000662InstantiateObjCObjectPointerType(const ObjCObjectPointerType *T) const {
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000663 assert(false && "Objective-C types cannot be dependent");
664 return QualType();
665}
666
667QualType
668TemplateTypeInstantiator::
Douglas Gregor8a5cb112009-06-26 21:40:05 +0000669InstantiateObjCInterfaceType(const ObjCInterfaceType *T) const {
Douglas Gregorcd281c32009-02-28 00:25:32 +0000670 assert(false && "Objective-C types cannot be dependent");
Douglas Gregor99ebf652009-02-27 19:31:52 +0000671 return QualType();
672}
673
Douglas Gregorcd281c32009-02-28 00:25:32 +0000674QualType
675TemplateTypeInstantiator::
Douglas Gregor8a5cb112009-06-26 21:40:05 +0000676InstantiateObjCQualifiedInterfaceType(
677 const ObjCQualifiedInterfaceType *T) const {
Douglas Gregorcd281c32009-02-28 00:25:32 +0000678 assert(false && "Objective-C types cannot be dependent");
Douglas Gregor99ebf652009-02-27 19:31:52 +0000679 return QualType();
680}
681
Douglas Gregorcd281c32009-02-28 00:25:32 +0000682/// \brief The actual implementation of Sema::InstantiateType().
683QualType TemplateTypeInstantiator::Instantiate(QualType T) const {
684 // If T is not a dependent type, there is nothing to do.
685 if (!T->isDependentType())
686 return T;
687
Douglas Gregor8a5cb112009-06-26 21:40:05 +0000688 QualType Result;
Douglas Gregorcd281c32009-02-28 00:25:32 +0000689 switch (T->getTypeClass()) {
690#define TYPE(Class, Base) \
691 case Type::Class: \
Douglas Gregor8a5cb112009-06-26 21:40:05 +0000692 Result = Instantiate##Class##Type(cast<Class##Type>(T.getTypePtr())); \
693 break;
Douglas Gregorcd281c32009-02-28 00:25:32 +0000694#define ABSTRACT_TYPE(Class, Base)
695#include "clang/AST/TypeNodes.def"
696 }
Douglas Gregor8a5cb112009-06-26 21:40:05 +0000697
698 // C++ [dcl.ref]p1:
699 // [...] Cv-qualified references are ill-formed except when
700 // the cv-qualifiers are introduced through the use of a
701 // typedef (7.1.3) or of a template type argument (14.3), in
702 // which case the cv-qualifiers are ignored.
703 //
704 // The same rule applies to function types.
705 if (!Result.isNull() && T.getCVRQualifiers() &&
706 !Result->isFunctionType() && !Result->isReferenceType())
707 Result = Result.getWithAdditionalQualifiers(T.getCVRQualifiers());
708 return Result;
Douglas Gregorcd281c32009-02-28 00:25:32 +0000709}
Douglas Gregor99ebf652009-02-27 19:31:52 +0000710
711/// \brief Instantiate the type T with a given set of template arguments.
712///
713/// This routine substitutes the given template arguments into the
714/// type T and produces the instantiated type.
715///
716/// \param T the type into which the template arguments will be
717/// substituted. If this type is not dependent, it will be returned
718/// immediately.
719///
720/// \param TemplateArgs the template arguments that will be
721/// substituted for the top-level template parameters within T.
722///
Douglas Gregor99ebf652009-02-27 19:31:52 +0000723/// \param Loc the location in the source code where this substitution
724/// is being performed. It will typically be the location of the
725/// declarator (if we're instantiating the type of some declaration)
726/// or the location of the type in the source code (if, e.g., we're
727/// instantiating the type of a cast expression).
728///
729/// \param Entity the name of the entity associated with a declaration
730/// being instantiated (if any). May be empty to indicate that there
731/// is no such entity (if, e.g., this is a type that occurs as part of
732/// a cast expression) or that the entity has no name (e.g., an
733/// unnamed function parameter).
734///
735/// \returns If the instantiation succeeds, the instantiated
736/// type. Otherwise, produces diagnostics and returns a NULL type.
737QualType Sema::InstantiateType(QualType T,
Douglas Gregor7e063902009-05-11 23:53:27 +0000738 const TemplateArgumentList &TemplateArgs,
Douglas Gregor99ebf652009-02-27 19:31:52 +0000739 SourceLocation Loc, DeclarationName Entity) {
Douglas Gregordf667e72009-03-10 20:44:00 +0000740 assert(!ActiveTemplateInstantiations.empty() &&
741 "Cannot perform an instantiation without some context on the "
742 "instantiation stack");
743
Douglas Gregor99ebf652009-02-27 19:31:52 +0000744 // If T is not a dependent type, there is nothing to do.
745 if (!T->isDependentType())
746 return T;
747
Douglas Gregor7e063902009-05-11 23:53:27 +0000748 TemplateTypeInstantiator Instantiator(*this, TemplateArgs, Loc, Entity);
Douglas Gregorcd281c32009-02-28 00:25:32 +0000749 return Instantiator(T);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000750}
Douglas Gregor2943aed2009-03-03 04:44:36 +0000751
752/// \brief Instantiate the base class specifiers of the given class
753/// template specialization.
754///
755/// Produces a diagnostic and returns true on error, returns false and
756/// attaches the instantiated base classes to the class template
757/// specialization if successful.
758bool
Douglas Gregord475b8d2009-03-25 21:17:03 +0000759Sema::InstantiateBaseSpecifiers(CXXRecordDecl *Instantiation,
760 CXXRecordDecl *Pattern,
Douglas Gregor7e063902009-05-11 23:53:27 +0000761 const TemplateArgumentList &TemplateArgs) {
Douglas Gregor2943aed2009-03-03 04:44:36 +0000762 bool Invalid = false;
Douglas Gregor0ca20ac2009-05-29 18:27:38 +0000763 llvm::SmallVector<CXXBaseSpecifier*, 4> InstantiatedBases;
Douglas Gregord475b8d2009-03-25 21:17:03 +0000764 for (ClassTemplateSpecializationDecl::base_class_iterator
765 Base = Pattern->bases_begin(), BaseEnd = Pattern->bases_end();
Douglas Gregor27b152f2009-03-10 18:52:44 +0000766 Base != BaseEnd; ++Base) {
Douglas Gregor2943aed2009-03-03 04:44:36 +0000767 if (!Base->getType()->isDependentType()) {
768 // FIXME: Allocate via ASTContext
769 InstantiatedBases.push_back(new CXXBaseSpecifier(*Base));
770 continue;
771 }
772
773 QualType BaseType = InstantiateType(Base->getType(),
Douglas Gregor7e063902009-05-11 23:53:27 +0000774 TemplateArgs,
Douglas Gregor2943aed2009-03-03 04:44:36 +0000775 Base->getSourceRange().getBegin(),
776 DeclarationName());
777 if (BaseType.isNull()) {
778 Invalid = true;
779 continue;
780 }
781
782 if (CXXBaseSpecifier *InstantiatedBase
Douglas Gregord475b8d2009-03-25 21:17:03 +0000783 = CheckBaseSpecifier(Instantiation,
Douglas Gregor2943aed2009-03-03 04:44:36 +0000784 Base->getSourceRange(),
785 Base->isVirtual(),
786 Base->getAccessSpecifierAsWritten(),
787 BaseType,
788 /*FIXME: Not totally accurate */
789 Base->getSourceRange().getBegin()))
790 InstantiatedBases.push_back(InstantiatedBase);
791 else
792 Invalid = true;
793 }
794
Douglas Gregor27b152f2009-03-10 18:52:44 +0000795 if (!Invalid &&
Jay Foadbeaaccd2009-05-21 09:52:38 +0000796 AttachBaseSpecifiers(Instantiation, InstantiatedBases.data(),
Douglas Gregor2943aed2009-03-03 04:44:36 +0000797 InstantiatedBases.size()))
798 Invalid = true;
799
800 return Invalid;
801}
802
Douglas Gregord475b8d2009-03-25 21:17:03 +0000803/// \brief Instantiate the definition of a class from a given pattern.
804///
805/// \param PointOfInstantiation The point of instantiation within the
806/// source code.
807///
808/// \param Instantiation is the declaration whose definition is being
809/// instantiated. This will be either a class template specialization
810/// or a member class of a class template specialization.
811///
812/// \param Pattern is the pattern from which the instantiation
813/// occurs. This will be either the declaration of a class template or
814/// the declaration of a member class of a class template.
815///
816/// \param TemplateArgs The template arguments to be substituted into
817/// the pattern.
818///
Douglas Gregord475b8d2009-03-25 21:17:03 +0000819/// \returns true if an error occurred, false otherwise.
820bool
821Sema::InstantiateClass(SourceLocation PointOfInstantiation,
822 CXXRecordDecl *Instantiation, CXXRecordDecl *Pattern,
Douglas Gregor93dfdb12009-05-13 00:25:59 +0000823 const TemplateArgumentList &TemplateArgs,
824 bool ExplicitInstantiation) {
Douglas Gregord475b8d2009-03-25 21:17:03 +0000825 bool Invalid = false;
826
827 CXXRecordDecl *PatternDef
828 = cast_or_null<CXXRecordDecl>(Pattern->getDefinition(Context));
829 if (!PatternDef) {
830 if (Pattern == Instantiation->getInstantiatedFromMemberClass()) {
831 Diag(PointOfInstantiation,
832 diag::err_implicit_instantiate_member_undefined)
833 << Context.getTypeDeclType(Instantiation);
834 Diag(Pattern->getLocation(), diag::note_member_of_template_here);
835 } else {
Douglas Gregor93dfdb12009-05-13 00:25:59 +0000836 Diag(PointOfInstantiation, diag::err_template_instantiate_undefined)
837 << ExplicitInstantiation
Douglas Gregord475b8d2009-03-25 21:17:03 +0000838 << Context.getTypeDeclType(Instantiation);
839 Diag(Pattern->getLocation(), diag::note_template_decl_here);
840 }
841 return true;
842 }
843 Pattern = PatternDef;
844
Douglas Gregord048bb72009-03-25 21:23:52 +0000845 InstantiatingTemplate Inst(*this, PointOfInstantiation, Instantiation);
Douglas Gregord475b8d2009-03-25 21:17:03 +0000846 if (Inst)
847 return true;
848
849 // Enter the scope of this instantiation. We don't use
850 // PushDeclContext because we don't have a scope.
851 DeclContext *PreviousContext = CurContext;
852 CurContext = Instantiation;
853
854 // Start the definition of this instantiation.
855 Instantiation->startDefinition();
856
857 // Instantiate the base class specifiers.
Douglas Gregor7e063902009-05-11 23:53:27 +0000858 if (InstantiateBaseSpecifiers(Instantiation, Pattern, TemplateArgs))
Douglas Gregord475b8d2009-03-25 21:17:03 +0000859 Invalid = true;
860
Douglas Gregor0ca20ac2009-05-29 18:27:38 +0000861 llvm::SmallVector<DeclPtrTy, 4> Fields;
Douglas Gregor6ab35242009-04-09 21:40:53 +0000862 for (RecordDecl::decl_iterator Member = Pattern->decls_begin(Context),
863 MemberEnd = Pattern->decls_end(Context);
864 Member != MemberEnd; ++Member) {
Douglas Gregor7e063902009-05-11 23:53:27 +0000865 Decl *NewMember = InstantiateDecl(*Member, Instantiation, TemplateArgs);
Douglas Gregord475b8d2009-03-25 21:17:03 +0000866 if (NewMember) {
867 if (NewMember->isInvalidDecl())
868 Invalid = true;
869 else if (FieldDecl *Field = dyn_cast<FieldDecl>(NewMember))
Chris Lattnerb28317a2009-03-28 19:18:32 +0000870 Fields.push_back(DeclPtrTy::make(Field));
Douglas Gregord475b8d2009-03-25 21:17:03 +0000871 } else {
872 // FIXME: Eventually, a NULL return will mean that one of the
Mike Stump390b4cc2009-05-16 07:39:55 +0000873 // instantiations was a semantic disaster, and we'll want to set Invalid =
874 // true. For now, we expect to skip some members that we can't yet handle.
Douglas Gregord475b8d2009-03-25 21:17:03 +0000875 }
876 }
877
878 // Finish checking fields.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000879 ActOnFields(0, Instantiation->getLocation(), DeclPtrTy::make(Instantiation),
Jay Foadbeaaccd2009-05-21 09:52:38 +0000880 Fields.data(), Fields.size(), SourceLocation(), SourceLocation(),
Douglas Gregord475b8d2009-03-25 21:17:03 +0000881 0);
882
883 // Add any implicitly-declared members that we might need.
884 AddImplicitlyDeclaredMembersToClass(Instantiation);
885
886 // Exit the scope of this instantiation.
887 CurContext = PreviousContext;
888
Douglas Gregoraba43bb2009-05-26 20:50:29 +0000889 if (!Invalid)
890 Consumer.HandleTagDeclDefinition(Instantiation);
891
Douglas Gregora58861f2009-05-13 20:28:22 +0000892 // If this is an explicit instantiation, instantiate our members, too.
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000893 if (!Invalid && ExplicitInstantiation) {
894 Inst.Clear();
Douglas Gregora58861f2009-05-13 20:28:22 +0000895 InstantiateClassMembers(PointOfInstantiation, Instantiation, TemplateArgs);
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000896 }
Douglas Gregora58861f2009-05-13 20:28:22 +0000897
Douglas Gregord475b8d2009-03-25 21:17:03 +0000898 return Invalid;
899}
900
Douglas Gregor2943aed2009-03-03 04:44:36 +0000901bool
902Sema::InstantiateClassTemplateSpecialization(
903 ClassTemplateSpecializationDecl *ClassTemplateSpec,
904 bool ExplicitInstantiation) {
905 // Perform the actual instantiation on the canonical declaration.
906 ClassTemplateSpec = cast<ClassTemplateSpecializationDecl>(
907 Context.getCanonicalDecl(ClassTemplateSpec));
908
909 // We can only instantiate something that hasn't already been
910 // instantiated or specialized. Fail without any diagnostics: our
911 // caller will provide an error message.
912 if (ClassTemplateSpec->getSpecializationKind() != TSK_Undeclared)
913 return true;
914
Douglas Gregor2943aed2009-03-03 04:44:36 +0000915 ClassTemplateDecl *Template = ClassTemplateSpec->getSpecializedTemplate();
Douglas Gregord475b8d2009-03-25 21:17:03 +0000916 CXXRecordDecl *Pattern = Template->getTemplatedDecl();
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000917 const TemplateArgumentList *TemplateArgs
918 = &ClassTemplateSpec->getTemplateArgs();
919
Douglas Gregorc1efb3f2009-06-12 22:31:52 +0000920 // C++ [temp.class.spec.match]p1:
921 // When a class template is used in a context that requires an
922 // instantiation of the class, it is necessary to determine
923 // whether the instantiation is to be generated using the primary
924 // template or one of the partial specializations. This is done by
925 // matching the template arguments of the class template
926 // specialization with the template argument lists of the partial
927 // specializations.
Douglas Gregor199d9912009-06-05 00:53:49 +0000928 typedef std::pair<ClassTemplatePartialSpecializationDecl *,
929 TemplateArgumentList *> MatchResult;
930 llvm::SmallVector<MatchResult, 4> Matched;
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000931 for (llvm::FoldingSet<ClassTemplatePartialSpecializationDecl>::iterator
932 Partial = Template->getPartialSpecializations().begin(),
933 PartialEnd = Template->getPartialSpecializations().end();
934 Partial != PartialEnd;
935 ++Partial) {
Douglas Gregorf67875d2009-06-12 18:26:56 +0000936 TemplateDeductionInfo Info(Context);
937 if (TemplateDeductionResult Result
Douglas Gregor199d9912009-06-05 00:53:49 +0000938 = DeduceTemplateArguments(&*Partial,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000939 ClassTemplateSpec->getTemplateArgs(),
940 Info)) {
941 // FIXME: Store the failed-deduction information for use in
942 // diagnostics, later.
943 (void)Result;
944 } else {
945 Matched.push_back(std::make_pair(&*Partial, Info.take()));
946 }
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000947 }
948
949 if (Matched.size() == 1) {
Douglas Gregorc1efb3f2009-06-12 22:31:52 +0000950 // -- If exactly one matching specialization is found, the
951 // instantiation is generated from that specialization.
Douglas Gregor199d9912009-06-05 00:53:49 +0000952 Pattern = Matched[0].first;
953 TemplateArgs = Matched[0].second;
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000954 } else if (Matched.size() > 1) {
Douglas Gregorc1efb3f2009-06-12 22:31:52 +0000955 // -- If more than one matching specialization is found, the
956 // partial order rules (14.5.4.2) are used to determine
957 // whether one of the specializations is more specialized
958 // than the others. If none of the specializations is more
959 // specialized than all of the other matching
960 // specializations, then the use of the class template is
961 // ambiguous and the program is ill-formed.
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000962 // FIXME: Implement partial ordering of class template partial
963 // specializations.
964 Diag(ClassTemplateSpec->getLocation(),
965 diag::unsup_template_partial_spec_ordering);
Douglas Gregorc1efb3f2009-06-12 22:31:52 +0000966 } else {
967 // -- If no matches are found, the instantiation is generated
968 // from the primary template.
969
970 // Since we initialized the pattern and template arguments from
971 // the primary template, there is nothing more we need to do here.
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000972 }
Douglas Gregor2943aed2009-03-03 04:44:36 +0000973
974 // Note that this is an instantiation.
975 ClassTemplateSpec->setSpecializationKind(
976 ExplicitInstantiation? TSK_ExplicitInstantiation
977 : TSK_ImplicitInstantiation);
978
Douglas Gregor199d9912009-06-05 00:53:49 +0000979 bool Result = InstantiateClass(ClassTemplateSpec->getLocation(),
980 ClassTemplateSpec, Pattern, *TemplateArgs,
981 ExplicitInstantiation);
982
983 for (unsigned I = 0, N = Matched.size(); I != N; ++I) {
984 // FIXME: Implement TemplateArgumentList::Destroy!
985 // if (Matched[I].first != Pattern)
986 // Matched[I].second->Destroy(Context);
987 }
988
989 return Result;
Douglas Gregor2943aed2009-03-03 04:44:36 +0000990}
Douglas Gregor5953d8b2009-03-19 17:26:29 +0000991
Douglas Gregora58861f2009-05-13 20:28:22 +0000992/// \brief Instantiate the definitions of all of the member of the
993/// given class, which is an instantiation of a class template or a
994/// member class of a template.
995void
996Sema::InstantiateClassMembers(SourceLocation PointOfInstantiation,
997 CXXRecordDecl *Instantiation,
998 const TemplateArgumentList &TemplateArgs) {
999 for (DeclContext::decl_iterator D = Instantiation->decls_begin(Context),
1000 DEnd = Instantiation->decls_end(Context);
1001 D != DEnd; ++D) {
1002 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(*D)) {
1003 if (!Function->getBody(Context))
Douglas Gregorf3e7ce42009-05-18 17:01:57 +00001004 InstantiateFunctionDefinition(PointOfInstantiation, Function);
Douglas Gregora58861f2009-05-13 20:28:22 +00001005 } else if (VarDecl *Var = dyn_cast<VarDecl>(*D)) {
1006 const VarDecl *Def = 0;
1007 if (!Var->getDefinition(Def))
1008 InstantiateVariableDefinition(Var);
1009 } else if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(*D)) {
1010 if (!Record->isInjectedClassName() && !Record->getDefinition(Context)) {
1011 assert(Record->getInstantiatedFromMemberClass() &&
1012 "Missing instantiated-from-template information");
Douglas Gregorf3e7ce42009-05-18 17:01:57 +00001013 InstantiateClass(PointOfInstantiation, Record,
Douglas Gregora58861f2009-05-13 20:28:22 +00001014 Record->getInstantiatedFromMemberClass(),
1015 TemplateArgs, true);
1016 }
1017 }
1018 }
1019}
1020
1021/// \brief Instantiate the definitions of all of the members of the
1022/// given class template specialization, which was named as part of an
1023/// explicit instantiation.
1024void Sema::InstantiateClassTemplateSpecializationMembers(
1025 SourceLocation PointOfInstantiation,
1026 ClassTemplateSpecializationDecl *ClassTemplateSpec) {
1027 // C++0x [temp.explicit]p7:
1028 // An explicit instantiation that names a class template
1029 // specialization is an explicit instantion of the same kind
1030 // (declaration or definition) of each of its members (not
1031 // including members inherited from base classes) that has not
1032 // been previously explicitly specialized in the translation unit
1033 // containing the explicit instantiation, except as described
1034 // below.
1035 InstantiateClassMembers(PointOfInstantiation, ClassTemplateSpec,
1036 ClassTemplateSpec->getTemplateArgs());
1037}
1038
Douglas Gregorab452ba2009-03-26 23:50:42 +00001039/// \brief Instantiate a nested-name-specifier.
1040NestedNameSpecifier *
1041Sema::InstantiateNestedNameSpecifier(NestedNameSpecifier *NNS,
1042 SourceRange Range,
Douglas Gregor7e063902009-05-11 23:53:27 +00001043 const TemplateArgumentList &TemplateArgs) {
Douglas Gregorab452ba2009-03-26 23:50:42 +00001044 // Instantiate the prefix of this nested name specifier.
1045 NestedNameSpecifier *Prefix = NNS->getPrefix();
1046 if (Prefix) {
Douglas Gregor7e063902009-05-11 23:53:27 +00001047 Prefix = InstantiateNestedNameSpecifier(Prefix, Range, TemplateArgs);
Douglas Gregorab452ba2009-03-26 23:50:42 +00001048 if (!Prefix)
1049 return 0;
Douglas Gregor5953d8b2009-03-19 17:26:29 +00001050 }
1051
Douglas Gregorab452ba2009-03-26 23:50:42 +00001052 switch (NNS->getKind()) {
Douglas Gregor17343172009-04-01 00:28:59 +00001053 case NestedNameSpecifier::Identifier: {
1054 assert(Prefix &&
1055 "Can't have an identifier nested-name-specifier with no prefix");
1056 CXXScopeSpec SS;
1057 // FIXME: The source location information is all wrong.
1058 SS.setRange(Range);
1059 SS.setScopeRep(Prefix);
1060 return static_cast<NestedNameSpecifier *>(
1061 ActOnCXXNestedNameSpecifier(0, SS,
1062 Range.getEnd(),
1063 Range.getEnd(),
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001064 *NNS->getAsIdentifier()));
Douglas Gregorab452ba2009-03-26 23:50:42 +00001065 break;
Douglas Gregor17343172009-04-01 00:28:59 +00001066 }
Douglas Gregorab452ba2009-03-26 23:50:42 +00001067
1068 case NestedNameSpecifier::Namespace:
1069 case NestedNameSpecifier::Global:
1070 return NNS;
1071
1072 case NestedNameSpecifier::TypeSpecWithTemplate:
1073 case NestedNameSpecifier::TypeSpec: {
1074 QualType T = QualType(NNS->getAsType(), 0);
1075 if (!T->isDependentType())
1076 return NNS;
1077
Douglas Gregor7e063902009-05-11 23:53:27 +00001078 T = InstantiateType(T, TemplateArgs, Range.getBegin(), DeclarationName());
Douglas Gregorab452ba2009-03-26 23:50:42 +00001079 if (T.isNull())
1080 return 0;
1081
Eli Friedman923f7532009-06-13 04:51:30 +00001082 if (T->isDependentType() || T->isRecordType() ||
Douglas Gregord57959a2009-03-27 23:10:48 +00001083 (getLangOptions().CPlusPlus0x && T->isEnumeralType())) {
Douglas Gregor17343172009-04-01 00:28:59 +00001084 assert(T.getCVRQualifiers() == 0 && "Can't get cv-qualifiers here");
Douglas Gregord57959a2009-03-27 23:10:48 +00001085 return NestedNameSpecifier::Create(Context, Prefix,
Douglas Gregorab452ba2009-03-26 23:50:42 +00001086 NNS->getKind() == NestedNameSpecifier::TypeSpecWithTemplate,
Douglas Gregord57959a2009-03-27 23:10:48 +00001087 T.getTypePtr());
1088 }
1089
1090 Diag(Range.getBegin(), diag::err_nested_name_spec_non_tag) << T;
1091 return 0;
Douglas Gregorab452ba2009-03-26 23:50:42 +00001092 }
1093 }
1094
Douglas Gregord57959a2009-03-27 23:10:48 +00001095 // Required to silence a GCC warning
Douglas Gregorab452ba2009-03-26 23:50:42 +00001096 return 0;
Douglas Gregor5953d8b2009-03-19 17:26:29 +00001097}
Douglas Gregorde650ae2009-03-31 18:38:02 +00001098
1099TemplateName
1100Sema::InstantiateTemplateName(TemplateName Name, SourceLocation Loc,
Douglas Gregor7e063902009-05-11 23:53:27 +00001101 const TemplateArgumentList &TemplateArgs) {
Douglas Gregorde650ae2009-03-31 18:38:02 +00001102 if (TemplateTemplateParmDecl *TTP
1103 = dyn_cast_or_null<TemplateTemplateParmDecl>(
1104 Name.getAsTemplateDecl())) {
1105 assert(TTP->getDepth() == 0 &&
1106 "Cannot reduce depth of a template template parameter");
Douglas Gregor9bde7732009-03-31 20:22:05 +00001107 assert(TemplateArgs[TTP->getPosition()].getAsDecl() &&
Douglas Gregorde650ae2009-03-31 18:38:02 +00001108 "Wrong kind of template template argument");
1109 ClassTemplateDecl *ClassTemplate
1110 = dyn_cast<ClassTemplateDecl>(
1111 TemplateArgs[TTP->getPosition()].getAsDecl());
Douglas Gregor9bde7732009-03-31 20:22:05 +00001112 assert(ClassTemplate && "Expected a class template");
Douglas Gregorde650ae2009-03-31 18:38:02 +00001113 if (QualifiedTemplateName *QTN = Name.getAsQualifiedTemplateName()) {
1114 NestedNameSpecifier *NNS
1115 = InstantiateNestedNameSpecifier(QTN->getQualifier(),
1116 /*FIXME=*/SourceRange(Loc),
Douglas Gregor7e063902009-05-11 23:53:27 +00001117 TemplateArgs);
Douglas Gregorde650ae2009-03-31 18:38:02 +00001118 if (NNS)
1119 return Context.getQualifiedTemplateName(NNS,
1120 QTN->hasTemplateKeyword(),
1121 ClassTemplate);
1122 }
1123
1124 return TemplateName(ClassTemplate);
1125 } else if (DependentTemplateName *DTN = Name.getAsDependentTemplateName()) {
1126 NestedNameSpecifier *NNS
1127 = InstantiateNestedNameSpecifier(DTN->getQualifier(),
1128 /*FIXME=*/SourceRange(Loc),
Douglas Gregor7e063902009-05-11 23:53:27 +00001129 TemplateArgs);
Douglas Gregorde650ae2009-03-31 18:38:02 +00001130
1131 if (!NNS) // FIXME: Not the best recovery strategy.
1132 return Name;
1133
1134 if (NNS->isDependent())
1135 return Context.getDependentTemplateName(NNS, DTN->getName());
1136
1137 // Somewhat redundant with ActOnDependentTemplateName.
1138 CXXScopeSpec SS;
1139 SS.setRange(SourceRange(Loc));
1140 SS.setScopeRep(NNS);
1141 TemplateTy Template;
1142 TemplateNameKind TNK = isTemplateName(*DTN->getName(), 0, Template, &SS);
1143 if (TNK == TNK_Non_template) {
1144 Diag(Loc, diag::err_template_kw_refers_to_non_template)
1145 << DTN->getName();
1146 return Name;
1147 } else if (TNK == TNK_Function_template) {
1148 Diag(Loc, diag::err_template_kw_refers_to_non_template)
1149 << DTN->getName();
1150 return Name;
1151 }
1152
1153 return Template.getAsVal<TemplateName>();
1154 }
1155
1156
1157
Mike Stump390b4cc2009-05-16 07:39:55 +00001158 // FIXME: Even if we're referring to a Decl that isn't a template template
1159 // parameter, we may need to instantiate the outer contexts of that
1160 // Decl. However, this won't be needed until we implement member templates.
Douglas Gregorde650ae2009-03-31 18:38:02 +00001161 return Name;
1162}
Douglas Gregor91333002009-06-11 00:06:24 +00001163
1164TemplateArgument Sema::Instantiate(TemplateArgument Arg,
1165 const TemplateArgumentList &TemplateArgs) {
1166 switch (Arg.getKind()) {
1167 case TemplateArgument::Null:
1168 assert(false && "Should never have a NULL template argument");
1169 break;
1170
1171 case TemplateArgument::Type: {
1172 QualType T = InstantiateType(Arg.getAsType(), TemplateArgs,
1173 Arg.getLocation(), DeclarationName());
1174 if (T.isNull())
1175 return TemplateArgument();
1176
1177 return TemplateArgument(Arg.getLocation(), T);
1178 }
1179
1180 case TemplateArgument::Declaration:
1181 // FIXME: Template instantiation for template template parameters.
1182 return Arg;
1183
1184 case TemplateArgument::Integral:
1185 return Arg;
1186
1187 case TemplateArgument::Expression: {
Douglas Gregorac7610d2009-06-22 20:57:11 +00001188 // Template argument expressions are not potentially evaluated.
1189 EnterExpressionEvaluationContext Unevaluated(*this, Action::Unevaluated);
1190
Douglas Gregor91333002009-06-11 00:06:24 +00001191 Sema::OwningExprResult E = InstantiateExpr(Arg.getAsExpr(), TemplateArgs);
1192 if (E.isInvalid())
1193 return TemplateArgument();
1194 return TemplateArgument(E.takeAs<Expr>());
1195 }
Anders Carlssond01b1da2009-06-15 17:04:53 +00001196
1197 case TemplateArgument::Pack:
1198 assert(0 && "FIXME: Implement!");
1199 break;
Douglas Gregor91333002009-06-11 00:06:24 +00001200 }
1201
1202 assert(false && "Unhandled template argument kind");
1203 return TemplateArgument();
1204}