blob: 0f677488c773d43d9da9b278f8ce17054cdf35ac [file] [log] [blame]
Douglas Gregor74296542009-02-27 19:31:52 +00001//===------- SemaTemplateInstantiate.cpp - C++ Template Instantiation ------===/
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//===----------------------------------------------------------------------===/
8//
9// This file implements C++ template instantiation.
10//
11//===----------------------------------------------------------------------===/
12
13#include "Sema.h"
Douglas Gregordc18e892009-05-26 20:50:29 +000014#include "clang/AST/ASTConsumer.h"
Douglas Gregor74296542009-02-27 19:31:52 +000015#include "clang/AST/ASTContext.h"
16#include "clang/AST/Expr.h"
Douglas Gregor74296542009-02-27 19:31:52 +000017#include "clang/AST/DeclTemplate.h"
18#include "clang/Parse/DeclSpec.h"
19#include "clang/Basic/LangOptions.h"
Douglas Gregorf57dcd02009-02-28 00:25:32 +000020#include "llvm/Support/Compiler.h"
Douglas Gregor74296542009-02-27 19:31:52 +000021
22using namespace clang;
23
Douglas Gregorfee85d62009-03-10 18:03:33 +000024//===----------------------------------------------------------------------===/
25// Template Instantiation Support
26//===----------------------------------------------------------------------===/
27
Douglas Gregor5f62c5e2009-05-14 23:26:13 +000028/// \brief Retrieve the template argument list that should be used to
29/// instantiate the given declaration.
30const TemplateArgumentList &
31Sema::getTemplateInstantiationArgs(NamedDecl *D) {
32 if (ClassTemplateSpecializationDecl *Spec
33 = dyn_cast<ClassTemplateSpecializationDecl>(D))
34 return Spec->getTemplateArgs();
35
36 DeclContext *EnclosingTemplateCtx = D->getDeclContext();
37 while (!isa<ClassTemplateSpecializationDecl>(EnclosingTemplateCtx)) {
38 assert(!EnclosingTemplateCtx->isFileContext() &&
39 "Tried to get the instantiation arguments of a non-template");
40 EnclosingTemplateCtx = EnclosingTemplateCtx->getParent();
41 }
42
43 ClassTemplateSpecializationDecl *EnclosingTemplate
44 = cast<ClassTemplateSpecializationDecl>(EnclosingTemplateCtx);
45 return EnclosingTemplate->getTemplateArgs();
46}
47
Douglas Gregor375733c2009-03-10 00:06:19 +000048Sema::InstantiatingTemplate::
49InstantiatingTemplate(Sema &SemaRef, SourceLocation PointOfInstantiation,
Douglas Gregorb12249d2009-05-18 17:01:57 +000050 Decl *Entity,
Douglas Gregor375733c2009-03-10 00:06:19 +000051 SourceRange InstantiationRange)
52 : SemaRef(SemaRef) {
Douglas Gregor56d25a72009-03-10 20:44:00 +000053
54 Invalid = CheckInstantiationDepth(PointOfInstantiation,
55 InstantiationRange);
56 if (!Invalid) {
Douglas Gregor375733c2009-03-10 00:06:19 +000057 ActiveTemplateInstantiation Inst;
Douglas Gregor56d25a72009-03-10 20:44:00 +000058 Inst.Kind = ActiveTemplateInstantiation::TemplateInstantiation;
Douglas Gregor375733c2009-03-10 00:06:19 +000059 Inst.PointOfInstantiation = PointOfInstantiation;
Douglas Gregor56d25a72009-03-10 20:44:00 +000060 Inst.Entity = reinterpret_cast<uintptr_t>(Entity);
Douglas Gregor95ba1282009-03-12 18:36:18 +000061 Inst.TemplateArgs = 0;
62 Inst.NumTemplateArgs = 0;
Douglas Gregor56d25a72009-03-10 20:44:00 +000063 Inst.InstantiationRange = InstantiationRange;
64 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
65 Invalid = false;
66 }
67}
68
69Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef,
70 SourceLocation PointOfInstantiation,
71 TemplateDecl *Template,
72 const TemplateArgument *TemplateArgs,
73 unsigned NumTemplateArgs,
74 SourceRange InstantiationRange)
75 : SemaRef(SemaRef) {
76
77 Invalid = CheckInstantiationDepth(PointOfInstantiation,
78 InstantiationRange);
79 if (!Invalid) {
80 ActiveTemplateInstantiation Inst;
81 Inst.Kind
82 = ActiveTemplateInstantiation::DefaultTemplateArgumentInstantiation;
83 Inst.PointOfInstantiation = PointOfInstantiation;
84 Inst.Entity = reinterpret_cast<uintptr_t>(Template);
85 Inst.TemplateArgs = TemplateArgs;
86 Inst.NumTemplateArgs = NumTemplateArgs;
Douglas Gregor375733c2009-03-10 00:06:19 +000087 Inst.InstantiationRange = InstantiationRange;
88 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
89 Invalid = false;
90 }
91}
92
Douglas Gregorc5e01af2009-06-10 23:47:09 +000093Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef,
94 SourceLocation PointOfInstantiation,
95 ClassTemplatePartialSpecializationDecl *PartialSpec,
96 const TemplateArgument *TemplateArgs,
97 unsigned NumTemplateArgs,
98 SourceRange InstantiationRange)
99 : SemaRef(SemaRef) {
100
101 Invalid = CheckInstantiationDepth(PointOfInstantiation,
102 InstantiationRange);
103 if (!Invalid) {
104 ActiveTemplateInstantiation Inst;
105 Inst.Kind
106 = ActiveTemplateInstantiation::PartialSpecDeductionInstantiation;
107 Inst.PointOfInstantiation = PointOfInstantiation;
108 Inst.Entity = reinterpret_cast<uintptr_t>(PartialSpec);
109 Inst.TemplateArgs = TemplateArgs;
110 Inst.NumTemplateArgs = NumTemplateArgs;
111 Inst.InstantiationRange = InstantiationRange;
112 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
113 Invalid = false;
114 }
115}
116
Douglas Gregorb12249d2009-05-18 17:01:57 +0000117void Sema::InstantiatingTemplate::Clear() {
118 if (!Invalid) {
Douglas Gregor375733c2009-03-10 00:06:19 +0000119 SemaRef.ActiveTemplateInstantiations.pop_back();
Douglas Gregorb12249d2009-05-18 17:01:57 +0000120 Invalid = true;
121 }
Douglas Gregor375733c2009-03-10 00:06:19 +0000122}
123
Douglas Gregor56d25a72009-03-10 20:44:00 +0000124bool Sema::InstantiatingTemplate::CheckInstantiationDepth(
125 SourceLocation PointOfInstantiation,
126 SourceRange InstantiationRange) {
127 if (SemaRef.ActiveTemplateInstantiations.size()
128 <= SemaRef.getLangOptions().InstantiationDepth)
129 return false;
130
131 SemaRef.Diag(PointOfInstantiation,
132 diag::err_template_recursion_depth_exceeded)
133 << SemaRef.getLangOptions().InstantiationDepth
134 << InstantiationRange;
135 SemaRef.Diag(PointOfInstantiation, diag::note_template_recursion_depth)
136 << SemaRef.getLangOptions().InstantiationDepth;
137 return true;
138}
139
Douglas Gregorfee85d62009-03-10 18:03:33 +0000140/// \brief Prints the current instantiation stack through a series of
141/// notes.
142void Sema::PrintInstantiationStack() {
143 for (llvm::SmallVector<ActiveTemplateInstantiation, 16>::reverse_iterator
144 Active = ActiveTemplateInstantiations.rbegin(),
145 ActiveEnd = ActiveTemplateInstantiations.rend();
146 Active != ActiveEnd;
147 ++Active) {
Douglas Gregor56d25a72009-03-10 20:44:00 +0000148 switch (Active->Kind) {
149 case ActiveTemplateInstantiation::TemplateInstantiation: {
Douglas Gregorb12249d2009-05-18 17:01:57 +0000150 Decl *D = reinterpret_cast<Decl *>(Active->Entity);
151 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) {
152 unsigned DiagID = diag::note_template_member_class_here;
153 if (isa<ClassTemplateSpecializationDecl>(Record))
154 DiagID = diag::note_template_class_instantiation_here;
155 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
156 DiagID)
157 << Context.getTypeDeclType(Record)
158 << Active->InstantiationRange;
159 } else {
160 FunctionDecl *Function = cast<FunctionDecl>(D);
161 unsigned DiagID = diag::note_template_member_function_here;
162 // FIXME: check for a function template
163 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
164 DiagID)
165 << Function
166 << Active->InstantiationRange;
167 }
Douglas Gregor56d25a72009-03-10 20:44:00 +0000168 break;
169 }
170
171 case ActiveTemplateInstantiation::DefaultTemplateArgumentInstantiation: {
172 TemplateDecl *Template = cast<TemplateDecl>((Decl *)Active->Entity);
173 std::string TemplateArgsStr
Douglas Gregordd13e842009-03-30 22:58:21 +0000174 = TemplateSpecializationType::PrintTemplateArgumentList(
Douglas Gregor56d25a72009-03-10 20:44:00 +0000175 Active->TemplateArgs,
Douglas Gregor3bf3bbc2009-05-29 20:38:28 +0000176 Active->NumTemplateArgs,
177 Context.PrintingPolicy);
Douglas Gregor56d25a72009-03-10 20:44:00 +0000178 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
179 diag::note_default_arg_instantiation_here)
180 << (Template->getNameAsString() + TemplateArgsStr)
181 << Active->InstantiationRange;
182 break;
183 }
Douglas Gregorc5e01af2009-06-10 23:47:09 +0000184
185 case ActiveTemplateInstantiation::PartialSpecDeductionInstantiation: {
186 ClassTemplatePartialSpecializationDecl *PartialSpec
187 = cast<ClassTemplatePartialSpecializationDecl>((Decl *)Active->Entity);
Douglas Gregorc5e01af2009-06-10 23:47:09 +0000188 // FIXME: The active template instantiation's template arguments
189 // are interesting, too. We should add something like [with T =
190 // foo, U = bar, etc.] to the string.
191 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
192 diag::note_partial_spec_deduct_instantiation_here)
Douglas Gregor8f378a92009-06-11 18:10:32 +0000193 << Context.getTypeDeclType(PartialSpec)
Douglas Gregorc5e01af2009-06-10 23:47:09 +0000194 << Active->InstantiationRange;
195 break;
196 }
197
Douglas Gregor56d25a72009-03-10 20:44:00 +0000198 }
Douglas Gregorfee85d62009-03-10 18:03:33 +0000199 }
200}
201
Douglas Gregor95d6c952009-06-14 07:33:30 +0000202bool Sema::isSFINAEContext() const {
203 using llvm::SmallVector;
204 for (SmallVector<ActiveTemplateInstantiation, 16>::const_reverse_iterator
205 Active = ActiveTemplateInstantiations.rbegin(),
206 ActiveEnd = ActiveTemplateInstantiations.rend();
207 Active != ActiveEnd;
208 ++Active) {
209
210 switch(Active->Kind) {
211 case ActiveTemplateInstantiation::PartialSpecDeductionInstantiation:
212 // We're in a template argument deduction context, so SFINAE
213 // applies.
214 return true;
215
216 case ActiveTemplateInstantiation::DefaultTemplateArgumentInstantiation:
217 // A default template argument instantiation may or may not be a
218 // SFINAE context; look further up the stack.
219 break;
220
221 case ActiveTemplateInstantiation::TemplateInstantiation:
222 // This is a template instantiation, so there is no SFINAE.
223 return false;
224 }
225 }
226
227 return false;
228}
229
Douglas Gregor74296542009-02-27 19:31:52 +0000230//===----------------------------------------------------------------------===/
231// Template Instantiation for Types
232//===----------------------------------------------------------------------===/
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000233namespace {
234 class VISIBILITY_HIDDEN TemplateTypeInstantiator {
235 Sema &SemaRef;
Douglas Gregorf9e7d3d2009-05-11 23:53:27 +0000236 const TemplateArgumentList &TemplateArgs;
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000237 SourceLocation Loc;
238 DeclarationName Entity;
Douglas Gregor74296542009-02-27 19:31:52 +0000239
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000240 public:
241 TemplateTypeInstantiator(Sema &SemaRef,
Douglas Gregorf9e7d3d2009-05-11 23:53:27 +0000242 const TemplateArgumentList &TemplateArgs,
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000243 SourceLocation Loc,
244 DeclarationName Entity)
245 : SemaRef(SemaRef), TemplateArgs(TemplateArgs),
Douglas Gregorf9e7d3d2009-05-11 23:53:27 +0000246 Loc(Loc), Entity(Entity) { }
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000247
248 QualType operator()(QualType T) const { return Instantiate(T); }
249
250 QualType Instantiate(QualType T) const;
251
252 // Declare instantiate functions for each type.
253#define TYPE(Class, Base) \
254 QualType Instantiate##Class##Type(const Class##Type *T, \
255 unsigned Quals) const;
256#define ABSTRACT_TYPE(Class, Base)
257#include "clang/AST/TypeNodes.def"
258 };
259}
260
261QualType
262TemplateTypeInstantiator::InstantiateExtQualType(const ExtQualType *T,
263 unsigned Quals) const {
Douglas Gregor74296542009-02-27 19:31:52 +0000264 // FIXME: Implement this
265 assert(false && "Cannot instantiate ExtQualType yet");
266 return QualType();
267}
268
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000269QualType
270TemplateTypeInstantiator::InstantiateBuiltinType(const BuiltinType *T,
271 unsigned Quals) const {
Douglas Gregorc0139ca2009-02-28 01:04:19 +0000272 assert(false && "Builtin types are not dependent and cannot be instantiated");
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000273 return QualType(T, Quals);
Douglas Gregor74296542009-02-27 19:31:52 +0000274}
275
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000276QualType
277TemplateTypeInstantiator::
278InstantiateFixedWidthIntType(const FixedWidthIntType *T, unsigned Quals) const {
Douglas Gregor74296542009-02-27 19:31:52 +0000279 // FIXME: Implement this
280 assert(false && "Cannot instantiate FixedWidthIntType yet");
281 return QualType();
282}
283
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000284QualType
285TemplateTypeInstantiator::InstantiateComplexType(const ComplexType *T,
286 unsigned Quals) const {
Douglas Gregor74296542009-02-27 19:31:52 +0000287 // FIXME: Implement this
288 assert(false && "Cannot instantiate ComplexType yet");
289 return QualType();
290}
291
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000292QualType
293TemplateTypeInstantiator::InstantiatePointerType(const PointerType *T,
294 unsigned Quals) const {
295 QualType PointeeType = Instantiate(T->getPointeeType());
296 if (PointeeType.isNull())
297 return QualType();
298
299 return SemaRef.BuildPointerType(PointeeType, Quals, Loc, Entity);
Douglas Gregor74296542009-02-27 19:31:52 +0000300}
301
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000302QualType
303TemplateTypeInstantiator::InstantiateBlockPointerType(const BlockPointerType *T,
304 unsigned Quals) const {
Anders Carlsson9d914d32009-06-12 16:23:10 +0000305 QualType PointeeType = Instantiate(T->getPointeeType());
306 if (PointeeType.isNull())
307 return QualType();
308
Anders Carlssondb52d342009-06-12 22:56:54 +0000309 return SemaRef.BuildBlockPointerType(PointeeType, Quals, Loc, Entity);
Douglas Gregor74296542009-02-27 19:31:52 +0000310}
311
Sebastian Redlce6fff02009-03-16 23:22:08 +0000312QualType
313TemplateTypeInstantiator::InstantiateLValueReferenceType(
314 const LValueReferenceType *T, unsigned Quals) const {
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000315 QualType ReferentType = Instantiate(T->getPointeeType());
316 if (ReferentType.isNull())
317 return QualType();
318
Sebastian Redlce6fff02009-03-16 23:22:08 +0000319 return SemaRef.BuildReferenceType(ReferentType, true, Quals, Loc, Entity);
320}
321
322QualType
323TemplateTypeInstantiator::InstantiateRValueReferenceType(
324 const RValueReferenceType *T, unsigned Quals) const {
325 QualType ReferentType = Instantiate(T->getPointeeType());
326 if (ReferentType.isNull())
327 return QualType();
328
329 return SemaRef.BuildReferenceType(ReferentType, false, Quals, Loc, Entity);
Douglas Gregor74296542009-02-27 19:31:52 +0000330}
331
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000332QualType
333TemplateTypeInstantiator::
334InstantiateMemberPointerType(const MemberPointerType *T,
335 unsigned Quals) const {
Douglas Gregora39e8302009-06-09 22:17:39 +0000336 QualType PointeeType = Instantiate(T->getPointeeType());
337 if (PointeeType.isNull())
338 return QualType();
339
340 QualType ClassType = Instantiate(QualType(T->getClass(), 0));
341 if (ClassType.isNull())
342 return QualType();
343
344 return SemaRef.BuildMemberPointerType(PointeeType, ClassType, Quals, Loc,
345 Entity);
Douglas Gregor74296542009-02-27 19:31:52 +0000346}
347
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000348QualType
349TemplateTypeInstantiator::
350InstantiateConstantArrayType(const ConstantArrayType *T,
351 unsigned Quals) const {
352 QualType ElementType = Instantiate(T->getElementType());
353 if (ElementType.isNull())
354 return ElementType;
355
356 // Build a temporary integer literal to specify the size for
357 // BuildArrayType. Since we have already checked the size as part of
358 // creating the dependent array type in the first place, we know
Douglas Gregor90177912009-05-13 18:28:20 +0000359 // there aren't any errors. However, we do need to determine what
360 // C++ type to give the size expression.
361 llvm::APInt Size = T->getSize();
362 QualType Types[] = {
363 SemaRef.Context.UnsignedCharTy, SemaRef.Context.UnsignedShortTy,
364 SemaRef.Context.UnsignedIntTy, SemaRef.Context.UnsignedLongTy,
365 SemaRef.Context.UnsignedLongLongTy, SemaRef.Context.UnsignedInt128Ty
366 };
367 const unsigned NumTypes = sizeof(Types) / sizeof(QualType);
368 QualType SizeType;
369 for (unsigned I = 0; I != NumTypes; ++I)
370 if (Size.getBitWidth() == SemaRef.Context.getIntWidth(Types[I])) {
371 SizeType = Types[I];
372 break;
373 }
374
375 if (SizeType.isNull())
376 SizeType = SemaRef.Context.getFixedWidthIntType(Size.getBitWidth(), false);
377
378 IntegerLiteral ArraySize(Size, SizeType, Loc);
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000379 return SemaRef.BuildArrayType(ElementType, T->getSizeModifier(),
380 &ArraySize, T->getIndexTypeQualifier(),
381 Loc, Entity);
Douglas Gregor74296542009-02-27 19:31:52 +0000382}
383
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000384QualType
385TemplateTypeInstantiator::
386InstantiateIncompleteArrayType(const IncompleteArrayType *T,
387 unsigned Quals) const {
388 QualType ElementType = Instantiate(T->getElementType());
389 if (ElementType.isNull())
390 return ElementType;
391
392 return SemaRef.BuildArrayType(ElementType, T->getSizeModifier(),
393 0, T->getIndexTypeQualifier(),
394 Loc, Entity);
Douglas Gregor74296542009-02-27 19:31:52 +0000395}
396
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000397QualType
398TemplateTypeInstantiator::
399InstantiateVariableArrayType(const VariableArrayType *T,
400 unsigned Quals) const {
Douglas Gregor74296542009-02-27 19:31:52 +0000401 // FIXME: Implement this
402 assert(false && "Cannot instantiate VariableArrayType yet");
403 return QualType();
404}
405
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000406QualType
407TemplateTypeInstantiator::
408InstantiateDependentSizedArrayType(const DependentSizedArrayType *T,
409 unsigned Quals) const {
Anders Carlsson149f2782009-03-15 20:12:13 +0000410 Expr *ArraySize = T->getSizeExpr();
411 assert(ArraySize->isValueDependent() &&
412 "dependent sized array types must have value dependent size expr");
413
414 // Instantiate the element type if needed
415 QualType ElementType = T->getElementType();
416 if (ElementType->isDependentType()) {
417 ElementType = Instantiate(ElementType);
418 if (ElementType.isNull())
419 return QualType();
420 }
421
422 // Instantiate the size expression
Douglas Gregora8b2fbf2009-06-22 20:57:11 +0000423 EnterExpressionEvaluationContext Unevaluated(SemaRef, Action::Unevaluated);
Anders Carlsson149f2782009-03-15 20:12:13 +0000424 Sema::OwningExprResult InstantiatedArraySize =
Douglas Gregorf9e7d3d2009-05-11 23:53:27 +0000425 SemaRef.InstantiateExpr(ArraySize, TemplateArgs);
Anders Carlsson149f2782009-03-15 20:12:13 +0000426 if (InstantiatedArraySize.isInvalid())
427 return QualType();
428
429 return SemaRef.BuildArrayType(ElementType, T->getSizeModifier(),
Anders Carlsson39ecdcf2009-05-01 19:49:17 +0000430 InstantiatedArraySize.takeAs<Expr>(),
Anders Carlsson149f2782009-03-15 20:12:13 +0000431 T->getIndexTypeQualifier(), Loc, Entity);
Douglas Gregor74296542009-02-27 19:31:52 +0000432}
433
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000434QualType
Douglas Gregor2a2e0402009-06-17 21:51:59 +0000435TemplateTypeInstantiator::
436InstantiateDependentSizedExtVectorType(const DependentSizedExtVectorType *T,
437 unsigned Quals) const {
438
Douglas Gregor68a7a6d2009-06-18 18:45:36 +0000439 // Instantiate the element type if needed.
Douglas Gregor2a2e0402009-06-17 21:51:59 +0000440 QualType ElementType = T->getElementType();
441 if (ElementType->isDependentType()) {
442 ElementType = Instantiate(ElementType);
443 if (ElementType.isNull())
444 return QualType();
445 }
446
Douglas Gregora8b2fbf2009-06-22 20:57:11 +0000447 // The expression in a dependent-sized extended vector type is not
448 // potentially evaluated.
449 EnterExpressionEvaluationContext Unevaluated(SemaRef, Action::Unevaluated);
450
Douglas Gregor68a7a6d2009-06-18 18:45:36 +0000451 // Instantiate the size expression.
452 const Expr *SizeExpr = T->getSizeExpr();
Douglas Gregor2a2e0402009-06-17 21:51:59 +0000453 Sema::OwningExprResult InstantiatedArraySize =
Douglas Gregor68a7a6d2009-06-18 18:45:36 +0000454 SemaRef.InstantiateExpr(const_cast<Expr *>(SizeExpr), TemplateArgs);
Douglas Gregor2a2e0402009-06-17 21:51:59 +0000455 if (InstantiatedArraySize.isInvalid())
456 return QualType();
457
458 return SemaRef.BuildExtVectorType(ElementType,
459 SemaRef.Owned(
460 InstantiatedArraySize.takeAs<Expr>()),
461 T->getAttributeLoc());
462}
463
464QualType
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000465TemplateTypeInstantiator::InstantiateVectorType(const VectorType *T,
466 unsigned Quals) const {
Douglas Gregor74296542009-02-27 19:31:52 +0000467 // FIXME: Implement this
468 assert(false && "Cannot instantiate VectorType yet");
469 return QualType();
470}
471
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000472QualType
473TemplateTypeInstantiator::InstantiateExtVectorType(const ExtVectorType *T,
474 unsigned Quals) const {
Douglas Gregor74296542009-02-27 19:31:52 +0000475 // FIXME: Implement this
476 assert(false && "Cannot instantiate ExtVectorType yet");
477 return QualType();
478}
479
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000480QualType
481TemplateTypeInstantiator::
482InstantiateFunctionProtoType(const FunctionProtoType *T,
483 unsigned Quals) const {
Douglas Gregorc0139ca2009-02-28 01:04:19 +0000484 QualType ResultType = Instantiate(T->getResultType());
485 if (ResultType.isNull())
486 return ResultType;
487
Douglas Gregor4ac20ef2009-05-29 18:27:38 +0000488 llvm::SmallVector<QualType, 4> ParamTypes;
Douglas Gregorc0139ca2009-02-28 01:04:19 +0000489 for (FunctionProtoType::arg_type_iterator Param = T->arg_type_begin(),
490 ParamEnd = T->arg_type_end();
491 Param != ParamEnd; ++Param) {
492 QualType P = Instantiate(*Param);
493 if (P.isNull())
494 return P;
495
496 ParamTypes.push_back(P);
497 }
498
Douglas Gregor8f378a92009-06-11 18:10:32 +0000499 return SemaRef.BuildFunctionType(ResultType, ParamTypes.data(),
Douglas Gregorc0139ca2009-02-28 01:04:19 +0000500 ParamTypes.size(),
501 T->isVariadic(), T->getTypeQuals(),
502 Loc, Entity);
Douglas Gregor74296542009-02-27 19:31:52 +0000503}
504
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000505QualType
506TemplateTypeInstantiator::
507InstantiateFunctionNoProtoType(const FunctionNoProtoType *T,
508 unsigned Quals) const {
Douglas Gregorc0139ca2009-02-28 01:04:19 +0000509 assert(false && "Functions without prototypes cannot be dependent.");
Douglas Gregor74296542009-02-27 19:31:52 +0000510 return QualType();
511}
512
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000513QualType
514TemplateTypeInstantiator::InstantiateTypedefType(const TypedefType *T,
515 unsigned Quals) const {
Douglas Gregorf97986d2009-05-27 05:35:12 +0000516 TypedefDecl *Typedef
Douglas Gregor9cbe6cd2009-05-27 17:54:46 +0000517 = cast_or_null<TypedefDecl>(
518 SemaRef.InstantiateCurrentDeclRef(T->getDecl()));
Douglas Gregorf97986d2009-05-27 05:35:12 +0000519 if (!Typedef)
520 return QualType();
521
522 return SemaRef.Context.getTypeDeclType(Typedef);
Douglas Gregor74296542009-02-27 19:31:52 +0000523}
524
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000525QualType
526TemplateTypeInstantiator::InstantiateTypeOfExprType(const TypeOfExprType *T,
527 unsigned Quals) const {
Douglas Gregora8b2fbf2009-06-22 20:57:11 +0000528 // The expression in a typeof is not potentially evaluated.
529 EnterExpressionEvaluationContext Unevaluated(SemaRef, Action::Unevaluated);
530
Douglas Gregord4f14af2009-05-26 22:09:24 +0000531 Sema::OwningExprResult E
532 = SemaRef.InstantiateExpr(T->getUnderlyingExpr(), TemplateArgs);
533 if (E.isInvalid())
534 return QualType();
535
536 return SemaRef.Context.getTypeOfExprType(E.takeAs<Expr>());
Douglas Gregor74296542009-02-27 19:31:52 +0000537}
538
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000539QualType
540TemplateTypeInstantiator::InstantiateTypeOfType(const TypeOfType *T,
541 unsigned Quals) const {
Douglas Gregord4f14af2009-05-26 22:09:24 +0000542 QualType Underlying = Instantiate(T->getUnderlyingType());
543 if (Underlying.isNull())
544 return QualType();
545
546 return SemaRef.Context.getTypeOfType(Underlying);
Douglas Gregor74296542009-02-27 19:31:52 +0000547}
548
Anders Carlsson93ab5332009-06-24 19:06:50 +0000549QualType
550TemplateTypeInstantiator::InstantiateDecltypeType(const DecltypeType *T,
551 unsigned Quals) const {
552 Sema::OwningExprResult E
553 = SemaRef.InstantiateExpr(T->getUnderlyingExpr(), TemplateArgs);
554
555 if (E.isInvalid())
556 return QualType();
557
558 return SemaRef.Context.getDecltypeType(E.takeAs<Expr>());
559}
560
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000561QualType
562TemplateTypeInstantiator::InstantiateRecordType(const RecordType *T,
563 unsigned Quals) const {
Douglas Gregorf97986d2009-05-27 05:35:12 +0000564 RecordDecl *Record
Douglas Gregor9cbe6cd2009-05-27 17:54:46 +0000565 = cast_or_null<RecordDecl>(SemaRef.InstantiateCurrentDeclRef(T->getDecl()));
Douglas Gregorf97986d2009-05-27 05:35:12 +0000566 if (!Record)
567 return QualType();
568
569 return SemaRef.Context.getTypeDeclType(Record);
Douglas Gregor74296542009-02-27 19:31:52 +0000570}
571
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000572QualType
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000573TemplateTypeInstantiator::InstantiateEnumType(const EnumType *T,
574 unsigned Quals) const {
Douglas Gregorf97986d2009-05-27 05:35:12 +0000575 EnumDecl *Enum
Douglas Gregor9cbe6cd2009-05-27 17:54:46 +0000576 = cast_or_null<EnumDecl>(SemaRef.InstantiateCurrentDeclRef(T->getDecl()));
Douglas Gregorf97986d2009-05-27 05:35:12 +0000577 if (!Enum)
578 return QualType();
579
580 return SemaRef.Context.getTypeDeclType(Enum);
Douglas Gregor74296542009-02-27 19:31:52 +0000581}
582
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000583QualType
584TemplateTypeInstantiator::
585InstantiateTemplateTypeParmType(const TemplateTypeParmType *T,
586 unsigned Quals) const {
Douglas Gregor74296542009-02-27 19:31:52 +0000587 if (T->getDepth() == 0) {
588 // Replace the template type parameter with its corresponding
589 // template argument.
Douglas Gregor74296542009-02-27 19:31:52 +0000590 assert(TemplateArgs[T->getIndex()].getKind() == TemplateArgument::Type &&
591 "Template argument kind mismatch");
592 QualType Result = TemplateArgs[T->getIndex()].getAsType();
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000593 if (Result.isNull() || !Quals)
Douglas Gregor74296542009-02-27 19:31:52 +0000594 return Result;
595
596 // C++ [dcl.ref]p1:
597 // [...] Cv-qualified references are ill-formed except when
598 // the cv-qualifiers are introduced through the use of a
599 // typedef (7.1.3) or of a template type argument (14.3), in
600 // which case the cv-qualifiers are ignored.
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000601 if (Quals && Result->isReferenceType())
602 Quals = 0;
Douglas Gregor74296542009-02-27 19:31:52 +0000603
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000604 return QualType(Result.getTypePtr(), Quals | Result.getCVRQualifiers());
Douglas Gregor74296542009-02-27 19:31:52 +0000605 }
606
607 // The template type parameter comes from an inner template (e.g.,
608 // the template parameter list of a member template inside the
609 // template we are instantiating). Create a new template type
610 // parameter with the template "level" reduced by one.
611 return SemaRef.Context.getTemplateTypeParmType(T->getDepth() - 1,
612 T->getIndex(),
Anders Carlsson4e3d3552009-06-16 00:30:48 +0000613 T->isParameterPack(),
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000614 T->getName())
615 .getQualifiedType(Quals);
Douglas Gregor74296542009-02-27 19:31:52 +0000616}
617
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000618QualType
619TemplateTypeInstantiator::
Douglas Gregordd13e842009-03-30 22:58:21 +0000620InstantiateTemplateSpecializationType(
621 const TemplateSpecializationType *T,
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000622 unsigned Quals) const {
Douglas Gregor4ac20ef2009-05-29 18:27:38 +0000623 llvm::SmallVector<TemplateArgument, 4> InstantiatedTemplateArgs;
Douglas Gregorf9ff4b12009-03-09 23:48:35 +0000624 InstantiatedTemplateArgs.reserve(T->getNumArgs());
Douglas Gregordd13e842009-03-30 22:58:21 +0000625 for (TemplateSpecializationType::iterator Arg = T->begin(), ArgEnd = T->end();
Douglas Gregorf9ff4b12009-03-09 23:48:35 +0000626 Arg != ArgEnd; ++Arg) {
Douglas Gregorb320b9e2009-06-11 00:06:24 +0000627 TemplateArgument InstArg = SemaRef.Instantiate(*Arg, TemplateArgs);
628 if (InstArg.isNull())
629 return QualType();
Douglas Gregorf9ff4b12009-03-09 23:48:35 +0000630
Douglas Gregorb320b9e2009-06-11 00:06:24 +0000631 InstantiatedTemplateArgs.push_back(InstArg);
Douglas Gregorf9ff4b12009-03-09 23:48:35 +0000632 }
633
Mike Stumpe127ae32009-05-16 07:39:55 +0000634 // FIXME: We're missing the locations of the template name, '<', and '>'.
Douglas Gregor15a92852009-03-31 18:38:02 +0000635
636 TemplateName Name = SemaRef.InstantiateTemplateName(T->getTemplateName(),
637 Loc,
Douglas Gregorf9e7d3d2009-05-11 23:53:27 +0000638 TemplateArgs);
Douglas Gregor15a92852009-03-31 18:38:02 +0000639
640 return SemaRef.CheckTemplateIdType(Name, Loc, SourceLocation(),
Douglas Gregor8f378a92009-06-11 18:10:32 +0000641 InstantiatedTemplateArgs.data(),
Douglas Gregordd13e842009-03-30 22:58:21 +0000642 InstantiatedTemplateArgs.size(),
643 SourceLocation());
Douglas Gregor74296542009-02-27 19:31:52 +0000644}
645
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000646QualType
647TemplateTypeInstantiator::
Douglas Gregor734b4ba2009-03-19 00:18:19 +0000648InstantiateQualifiedNameType(const QualifiedNameType *T,
649 unsigned Quals) const {
Douglas Gregord3022602009-03-27 23:10:48 +0000650 // When we instantiated a qualified name type, there's no point in
651 // keeping the qualification around in the instantiated result. So,
652 // just instantiate the named type.
653 return (*this)(T->getNamedType());
654}
655
656QualType
657TemplateTypeInstantiator::
658InstantiateTypenameType(const TypenameType *T, unsigned Quals) const {
Douglas Gregor77da5802009-04-01 00:28:59 +0000659 if (const TemplateSpecializationType *TemplateId = T->getTemplateId()) {
660 // When the typename type refers to a template-id, the template-id
661 // is dependent and has enough information to instantiate the
662 // result of the typename type. Since we don't care about keeping
663 // the spelling of the typename type in template instantiations,
664 // we just instantiate the template-id.
665 return InstantiateTemplateSpecializationType(TemplateId, Quals);
666 }
667
Douglas Gregord3022602009-03-27 23:10:48 +0000668 NestedNameSpecifier *NNS
669 = SemaRef.InstantiateNestedNameSpecifier(T->getQualifier(),
670 SourceRange(Loc),
Douglas Gregorf9e7d3d2009-05-11 23:53:27 +0000671 TemplateArgs);
Douglas Gregord3022602009-03-27 23:10:48 +0000672 if (!NNS)
673 return QualType();
674
Douglas Gregor77da5802009-04-01 00:28:59 +0000675 return SemaRef.CheckTypenameType(NNS, *T->getIdentifier(), SourceRange(Loc));
Douglas Gregor734b4ba2009-03-19 00:18:19 +0000676}
677
678QualType
679TemplateTypeInstantiator::
Steve Naroffc75c1a82009-06-17 22:40:22 +0000680InstantiateObjCObjectPointerType(const ObjCObjectPointerType *T,
681 unsigned Quals) const {
682 assert(false && "Objective-C types cannot be dependent");
683 return QualType();
684}
685
686QualType
687TemplateTypeInstantiator::
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000688InstantiateObjCInterfaceType(const ObjCInterfaceType *T,
689 unsigned Quals) const {
690 assert(false && "Objective-C types cannot be dependent");
Douglas Gregor74296542009-02-27 19:31:52 +0000691 return QualType();
692}
693
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000694QualType
695TemplateTypeInstantiator::
696InstantiateObjCQualifiedInterfaceType(const ObjCQualifiedInterfaceType *T,
697 unsigned Quals) const {
698 assert(false && "Objective-C types cannot be dependent");
Douglas Gregor74296542009-02-27 19:31:52 +0000699 return QualType();
700}
701
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000702/// \brief The actual implementation of Sema::InstantiateType().
703QualType TemplateTypeInstantiator::Instantiate(QualType T) const {
704 // If T is not a dependent type, there is nothing to do.
705 if (!T->isDependentType())
706 return T;
707
708 switch (T->getTypeClass()) {
709#define TYPE(Class, Base) \
710 case Type::Class: \
711 return Instantiate##Class##Type(cast<Class##Type>(T.getTypePtr()), \
712 T.getCVRQualifiers());
713#define ABSTRACT_TYPE(Class, Base)
714#include "clang/AST/TypeNodes.def"
715 }
716
717 assert(false && "Not all types have been decoded for instantiation");
718 return QualType();
719}
Douglas Gregor74296542009-02-27 19:31:52 +0000720
721/// \brief Instantiate the type T with a given set of template arguments.
722///
723/// This routine substitutes the given template arguments into the
724/// type T and produces the instantiated type.
725///
726/// \param T the type into which the template arguments will be
727/// substituted. If this type is not dependent, it will be returned
728/// immediately.
729///
730/// \param TemplateArgs the template arguments that will be
731/// substituted for the top-level template parameters within T.
732///
Douglas Gregor74296542009-02-27 19:31:52 +0000733/// \param Loc the location in the source code where this substitution
734/// is being performed. It will typically be the location of the
735/// declarator (if we're instantiating the type of some declaration)
736/// or the location of the type in the source code (if, e.g., we're
737/// instantiating the type of a cast expression).
738///
739/// \param Entity the name of the entity associated with a declaration
740/// being instantiated (if any). May be empty to indicate that there
741/// is no such entity (if, e.g., this is a type that occurs as part of
742/// a cast expression) or that the entity has no name (e.g., an
743/// unnamed function parameter).
744///
745/// \returns If the instantiation succeeds, the instantiated
746/// type. Otherwise, produces diagnostics and returns a NULL type.
747QualType Sema::InstantiateType(QualType T,
Douglas Gregorf9e7d3d2009-05-11 23:53:27 +0000748 const TemplateArgumentList &TemplateArgs,
Douglas Gregor74296542009-02-27 19:31:52 +0000749 SourceLocation Loc, DeclarationName Entity) {
Douglas Gregor56d25a72009-03-10 20:44:00 +0000750 assert(!ActiveTemplateInstantiations.empty() &&
751 "Cannot perform an instantiation without some context on the "
752 "instantiation stack");
753
Douglas Gregor74296542009-02-27 19:31:52 +0000754 // If T is not a dependent type, there is nothing to do.
755 if (!T->isDependentType())
756 return T;
757
Douglas Gregorf9e7d3d2009-05-11 23:53:27 +0000758 TemplateTypeInstantiator Instantiator(*this, TemplateArgs, Loc, Entity);
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000759 return Instantiator(T);
Douglas Gregor74296542009-02-27 19:31:52 +0000760}
Douglas Gregored3a3982009-03-03 04:44:36 +0000761
762/// \brief Instantiate the base class specifiers of the given class
763/// template specialization.
764///
765/// Produces a diagnostic and returns true on error, returns false and
766/// attaches the instantiated base classes to the class template
767/// specialization if successful.
768bool
Douglas Gregorcc887972009-03-25 21:17:03 +0000769Sema::InstantiateBaseSpecifiers(CXXRecordDecl *Instantiation,
770 CXXRecordDecl *Pattern,
Douglas Gregorf9e7d3d2009-05-11 23:53:27 +0000771 const TemplateArgumentList &TemplateArgs) {
Douglas Gregored3a3982009-03-03 04:44:36 +0000772 bool Invalid = false;
Douglas Gregor4ac20ef2009-05-29 18:27:38 +0000773 llvm::SmallVector<CXXBaseSpecifier*, 4> InstantiatedBases;
Douglas Gregorcc887972009-03-25 21:17:03 +0000774 for (ClassTemplateSpecializationDecl::base_class_iterator
775 Base = Pattern->bases_begin(), BaseEnd = Pattern->bases_end();
Douglas Gregord9572a12009-03-10 18:52:44 +0000776 Base != BaseEnd; ++Base) {
Douglas Gregored3a3982009-03-03 04:44:36 +0000777 if (!Base->getType()->isDependentType()) {
778 // FIXME: Allocate via ASTContext
779 InstantiatedBases.push_back(new CXXBaseSpecifier(*Base));
780 continue;
781 }
782
783 QualType BaseType = InstantiateType(Base->getType(),
Douglas Gregorf9e7d3d2009-05-11 23:53:27 +0000784 TemplateArgs,
Douglas Gregored3a3982009-03-03 04:44:36 +0000785 Base->getSourceRange().getBegin(),
786 DeclarationName());
787 if (BaseType.isNull()) {
788 Invalid = true;
789 continue;
790 }
791
792 if (CXXBaseSpecifier *InstantiatedBase
Douglas Gregorcc887972009-03-25 21:17:03 +0000793 = CheckBaseSpecifier(Instantiation,
Douglas Gregored3a3982009-03-03 04:44:36 +0000794 Base->getSourceRange(),
795 Base->isVirtual(),
796 Base->getAccessSpecifierAsWritten(),
797 BaseType,
798 /*FIXME: Not totally accurate */
799 Base->getSourceRange().getBegin()))
800 InstantiatedBases.push_back(InstantiatedBase);
801 else
802 Invalid = true;
803 }
804
Douglas Gregord9572a12009-03-10 18:52:44 +0000805 if (!Invalid &&
Jay Foad9e6bef42009-05-21 09:52:38 +0000806 AttachBaseSpecifiers(Instantiation, InstantiatedBases.data(),
Douglas Gregored3a3982009-03-03 04:44:36 +0000807 InstantiatedBases.size()))
808 Invalid = true;
809
810 return Invalid;
811}
812
Douglas Gregorcc887972009-03-25 21:17:03 +0000813/// \brief Instantiate the definition of a class from a given pattern.
814///
815/// \param PointOfInstantiation The point of instantiation within the
816/// source code.
817///
818/// \param Instantiation is the declaration whose definition is being
819/// instantiated. This will be either a class template specialization
820/// or a member class of a class template specialization.
821///
822/// \param Pattern is the pattern from which the instantiation
823/// occurs. This will be either the declaration of a class template or
824/// the declaration of a member class of a class template.
825///
826/// \param TemplateArgs The template arguments to be substituted into
827/// the pattern.
828///
Douglas Gregorcc887972009-03-25 21:17:03 +0000829/// \returns true if an error occurred, false otherwise.
830bool
831Sema::InstantiateClass(SourceLocation PointOfInstantiation,
832 CXXRecordDecl *Instantiation, CXXRecordDecl *Pattern,
Douglas Gregorfd79ac62009-05-13 00:25:59 +0000833 const TemplateArgumentList &TemplateArgs,
834 bool ExplicitInstantiation) {
Douglas Gregorcc887972009-03-25 21:17:03 +0000835 bool Invalid = false;
836
837 CXXRecordDecl *PatternDef
838 = cast_or_null<CXXRecordDecl>(Pattern->getDefinition(Context));
839 if (!PatternDef) {
840 if (Pattern == Instantiation->getInstantiatedFromMemberClass()) {
841 Diag(PointOfInstantiation,
842 diag::err_implicit_instantiate_member_undefined)
843 << Context.getTypeDeclType(Instantiation);
844 Diag(Pattern->getLocation(), diag::note_member_of_template_here);
845 } else {
Douglas Gregorfd79ac62009-05-13 00:25:59 +0000846 Diag(PointOfInstantiation, diag::err_template_instantiate_undefined)
847 << ExplicitInstantiation
Douglas Gregorcc887972009-03-25 21:17:03 +0000848 << Context.getTypeDeclType(Instantiation);
849 Diag(Pattern->getLocation(), diag::note_template_decl_here);
850 }
851 return true;
852 }
853 Pattern = PatternDef;
854
Douglas Gregor42c48522009-03-25 21:23:52 +0000855 InstantiatingTemplate Inst(*this, PointOfInstantiation, Instantiation);
Douglas Gregorcc887972009-03-25 21:17:03 +0000856 if (Inst)
857 return true;
858
859 // Enter the scope of this instantiation. We don't use
860 // PushDeclContext because we don't have a scope.
861 DeclContext *PreviousContext = CurContext;
862 CurContext = Instantiation;
863
864 // Start the definition of this instantiation.
865 Instantiation->startDefinition();
866
867 // Instantiate the base class specifiers.
Douglas Gregorf9e7d3d2009-05-11 23:53:27 +0000868 if (InstantiateBaseSpecifiers(Instantiation, Pattern, TemplateArgs))
Douglas Gregorcc887972009-03-25 21:17:03 +0000869 Invalid = true;
870
Douglas Gregor4ac20ef2009-05-29 18:27:38 +0000871 llvm::SmallVector<DeclPtrTy, 4> Fields;
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000872 for (RecordDecl::decl_iterator Member = Pattern->decls_begin(Context),
873 MemberEnd = Pattern->decls_end(Context);
874 Member != MemberEnd; ++Member) {
Douglas Gregorf9e7d3d2009-05-11 23:53:27 +0000875 Decl *NewMember = InstantiateDecl(*Member, Instantiation, TemplateArgs);
Douglas Gregorcc887972009-03-25 21:17:03 +0000876 if (NewMember) {
877 if (NewMember->isInvalidDecl())
878 Invalid = true;
879 else if (FieldDecl *Field = dyn_cast<FieldDecl>(NewMember))
Chris Lattner5261d0c2009-03-28 19:18:32 +0000880 Fields.push_back(DeclPtrTy::make(Field));
Douglas Gregorcc887972009-03-25 21:17:03 +0000881 } else {
882 // FIXME: Eventually, a NULL return will mean that one of the
Mike Stumpe127ae32009-05-16 07:39:55 +0000883 // instantiations was a semantic disaster, and we'll want to set Invalid =
884 // true. For now, we expect to skip some members that we can't yet handle.
Douglas Gregorcc887972009-03-25 21:17:03 +0000885 }
886 }
887
888 // Finish checking fields.
Chris Lattner5261d0c2009-03-28 19:18:32 +0000889 ActOnFields(0, Instantiation->getLocation(), DeclPtrTy::make(Instantiation),
Jay Foad9e6bef42009-05-21 09:52:38 +0000890 Fields.data(), Fields.size(), SourceLocation(), SourceLocation(),
Douglas Gregorcc887972009-03-25 21:17:03 +0000891 0);
892
893 // Add any implicitly-declared members that we might need.
894 AddImplicitlyDeclaredMembersToClass(Instantiation);
895
896 // Exit the scope of this instantiation.
897 CurContext = PreviousContext;
898
Douglas Gregordc18e892009-05-26 20:50:29 +0000899 if (!Invalid)
900 Consumer.HandleTagDeclDefinition(Instantiation);
901
Douglas Gregordf9f5d12009-05-13 20:28:22 +0000902 // If this is an explicit instantiation, instantiate our members, too.
Douglas Gregorb12249d2009-05-18 17:01:57 +0000903 if (!Invalid && ExplicitInstantiation) {
904 Inst.Clear();
Douglas Gregordf9f5d12009-05-13 20:28:22 +0000905 InstantiateClassMembers(PointOfInstantiation, Instantiation, TemplateArgs);
Douglas Gregorb12249d2009-05-18 17:01:57 +0000906 }
Douglas Gregordf9f5d12009-05-13 20:28:22 +0000907
Douglas Gregorcc887972009-03-25 21:17:03 +0000908 return Invalid;
909}
910
Douglas Gregored3a3982009-03-03 04:44:36 +0000911bool
912Sema::InstantiateClassTemplateSpecialization(
913 ClassTemplateSpecializationDecl *ClassTemplateSpec,
914 bool ExplicitInstantiation) {
915 // Perform the actual instantiation on the canonical declaration.
916 ClassTemplateSpec = cast<ClassTemplateSpecializationDecl>(
917 Context.getCanonicalDecl(ClassTemplateSpec));
918
919 // We can only instantiate something that hasn't already been
920 // instantiated or specialized. Fail without any diagnostics: our
921 // caller will provide an error message.
922 if (ClassTemplateSpec->getSpecializationKind() != TSK_Undeclared)
923 return true;
924
Douglas Gregored3a3982009-03-03 04:44:36 +0000925 ClassTemplateDecl *Template = ClassTemplateSpec->getSpecializedTemplate();
Douglas Gregorcc887972009-03-25 21:17:03 +0000926 CXXRecordDecl *Pattern = Template->getTemplatedDecl();
Douglas Gregor58944ac2009-05-31 09:31:02 +0000927 const TemplateArgumentList *TemplateArgs
928 = &ClassTemplateSpec->getTemplateArgs();
929
Douglas Gregor21530c52009-06-12 22:31:52 +0000930 // C++ [temp.class.spec.match]p1:
931 // When a class template is used in a context that requires an
932 // instantiation of the class, it is necessary to determine
933 // whether the instantiation is to be generated using the primary
934 // template or one of the partial specializations. This is done by
935 // matching the template arguments of the class template
936 // specialization with the template argument lists of the partial
937 // specializations.
Douglas Gregorab9f71a2009-06-05 00:53:49 +0000938 typedef std::pair<ClassTemplatePartialSpecializationDecl *,
939 TemplateArgumentList *> MatchResult;
940 llvm::SmallVector<MatchResult, 4> Matched;
Douglas Gregor58944ac2009-05-31 09:31:02 +0000941 for (llvm::FoldingSet<ClassTemplatePartialSpecializationDecl>::iterator
942 Partial = Template->getPartialSpecializations().begin(),
943 PartialEnd = Template->getPartialSpecializations().end();
944 Partial != PartialEnd;
945 ++Partial) {
Douglas Gregor623e2e02009-06-12 18:26:56 +0000946 TemplateDeductionInfo Info(Context);
947 if (TemplateDeductionResult Result
Douglas Gregorab9f71a2009-06-05 00:53:49 +0000948 = DeduceTemplateArguments(&*Partial,
Douglas Gregor623e2e02009-06-12 18:26:56 +0000949 ClassTemplateSpec->getTemplateArgs(),
950 Info)) {
951 // FIXME: Store the failed-deduction information for use in
952 // diagnostics, later.
953 (void)Result;
954 } else {
955 Matched.push_back(std::make_pair(&*Partial, Info.take()));
956 }
Douglas Gregor58944ac2009-05-31 09:31:02 +0000957 }
958
959 if (Matched.size() == 1) {
Douglas Gregor21530c52009-06-12 22:31:52 +0000960 // -- If exactly one matching specialization is found, the
961 // instantiation is generated from that specialization.
Douglas Gregorab9f71a2009-06-05 00:53:49 +0000962 Pattern = Matched[0].first;
963 TemplateArgs = Matched[0].second;
Douglas Gregor58944ac2009-05-31 09:31:02 +0000964 } else if (Matched.size() > 1) {
Douglas Gregor21530c52009-06-12 22:31:52 +0000965 // -- If more than one matching specialization is found, the
966 // partial order rules (14.5.4.2) are used to determine
967 // whether one of the specializations is more specialized
968 // than the others. If none of the specializations is more
969 // specialized than all of the other matching
970 // specializations, then the use of the class template is
971 // ambiguous and the program is ill-formed.
Douglas Gregor58944ac2009-05-31 09:31:02 +0000972 // FIXME: Implement partial ordering of class template partial
973 // specializations.
974 Diag(ClassTemplateSpec->getLocation(),
975 diag::unsup_template_partial_spec_ordering);
Douglas Gregor21530c52009-06-12 22:31:52 +0000976 } else {
977 // -- If no matches are found, the instantiation is generated
978 // from the primary template.
979
980 // Since we initialized the pattern and template arguments from
981 // the primary template, there is nothing more we need to do here.
Douglas Gregor58944ac2009-05-31 09:31:02 +0000982 }
Douglas Gregored3a3982009-03-03 04:44:36 +0000983
984 // Note that this is an instantiation.
985 ClassTemplateSpec->setSpecializationKind(
986 ExplicitInstantiation? TSK_ExplicitInstantiation
987 : TSK_ImplicitInstantiation);
988
Douglas Gregorab9f71a2009-06-05 00:53:49 +0000989 bool Result = InstantiateClass(ClassTemplateSpec->getLocation(),
990 ClassTemplateSpec, Pattern, *TemplateArgs,
991 ExplicitInstantiation);
992
993 for (unsigned I = 0, N = Matched.size(); I != N; ++I) {
994 // FIXME: Implement TemplateArgumentList::Destroy!
995 // if (Matched[I].first != Pattern)
996 // Matched[I].second->Destroy(Context);
997 }
998
999 return Result;
Douglas Gregored3a3982009-03-03 04:44:36 +00001000}
Douglas Gregor47bde7c2009-03-19 17:26:29 +00001001
Douglas Gregordf9f5d12009-05-13 20:28:22 +00001002/// \brief Instantiate the definitions of all of the member of the
1003/// given class, which is an instantiation of a class template or a
1004/// member class of a template.
1005void
1006Sema::InstantiateClassMembers(SourceLocation PointOfInstantiation,
1007 CXXRecordDecl *Instantiation,
1008 const TemplateArgumentList &TemplateArgs) {
1009 for (DeclContext::decl_iterator D = Instantiation->decls_begin(Context),
1010 DEnd = Instantiation->decls_end(Context);
1011 D != DEnd; ++D) {
1012 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(*D)) {
1013 if (!Function->getBody(Context))
Douglas Gregorb12249d2009-05-18 17:01:57 +00001014 InstantiateFunctionDefinition(PointOfInstantiation, Function);
Douglas Gregordf9f5d12009-05-13 20:28:22 +00001015 } else if (VarDecl *Var = dyn_cast<VarDecl>(*D)) {
1016 const VarDecl *Def = 0;
1017 if (!Var->getDefinition(Def))
1018 InstantiateVariableDefinition(Var);
1019 } else if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(*D)) {
1020 if (!Record->isInjectedClassName() && !Record->getDefinition(Context)) {
1021 assert(Record->getInstantiatedFromMemberClass() &&
1022 "Missing instantiated-from-template information");
Douglas Gregorb12249d2009-05-18 17:01:57 +00001023 InstantiateClass(PointOfInstantiation, Record,
Douglas Gregordf9f5d12009-05-13 20:28:22 +00001024 Record->getInstantiatedFromMemberClass(),
1025 TemplateArgs, true);
1026 }
1027 }
1028 }
1029}
1030
1031/// \brief Instantiate the definitions of all of the members of the
1032/// given class template specialization, which was named as part of an
1033/// explicit instantiation.
1034void Sema::InstantiateClassTemplateSpecializationMembers(
1035 SourceLocation PointOfInstantiation,
1036 ClassTemplateSpecializationDecl *ClassTemplateSpec) {
1037 // C++0x [temp.explicit]p7:
1038 // An explicit instantiation that names a class template
1039 // specialization is an explicit instantion of the same kind
1040 // (declaration or definition) of each of its members (not
1041 // including members inherited from base classes) that has not
1042 // been previously explicitly specialized in the translation unit
1043 // containing the explicit instantiation, except as described
1044 // below.
1045 InstantiateClassMembers(PointOfInstantiation, ClassTemplateSpec,
1046 ClassTemplateSpec->getTemplateArgs());
1047}
1048
Douglas Gregor1e589cc2009-03-26 23:50:42 +00001049/// \brief Instantiate a nested-name-specifier.
1050NestedNameSpecifier *
1051Sema::InstantiateNestedNameSpecifier(NestedNameSpecifier *NNS,
1052 SourceRange Range,
Douglas Gregorf9e7d3d2009-05-11 23:53:27 +00001053 const TemplateArgumentList &TemplateArgs) {
Douglas Gregor1e589cc2009-03-26 23:50:42 +00001054 // Instantiate the prefix of this nested name specifier.
1055 NestedNameSpecifier *Prefix = NNS->getPrefix();
1056 if (Prefix) {
Douglas Gregorf9e7d3d2009-05-11 23:53:27 +00001057 Prefix = InstantiateNestedNameSpecifier(Prefix, Range, TemplateArgs);
Douglas Gregor1e589cc2009-03-26 23:50:42 +00001058 if (!Prefix)
1059 return 0;
Douglas Gregor47bde7c2009-03-19 17:26:29 +00001060 }
1061
Douglas Gregor1e589cc2009-03-26 23:50:42 +00001062 switch (NNS->getKind()) {
Douglas Gregor77da5802009-04-01 00:28:59 +00001063 case NestedNameSpecifier::Identifier: {
1064 assert(Prefix &&
1065 "Can't have an identifier nested-name-specifier with no prefix");
1066 CXXScopeSpec SS;
1067 // FIXME: The source location information is all wrong.
1068 SS.setRange(Range);
1069 SS.setScopeRep(Prefix);
1070 return static_cast<NestedNameSpecifier *>(
1071 ActOnCXXNestedNameSpecifier(0, SS,
1072 Range.getEnd(),
1073 Range.getEnd(),
Douglas Gregor96b6df92009-05-14 00:28:11 +00001074 *NNS->getAsIdentifier()));
Douglas Gregor1e589cc2009-03-26 23:50:42 +00001075 break;
Douglas Gregor77da5802009-04-01 00:28:59 +00001076 }
Douglas Gregor1e589cc2009-03-26 23:50:42 +00001077
1078 case NestedNameSpecifier::Namespace:
1079 case NestedNameSpecifier::Global:
1080 return NNS;
1081
1082 case NestedNameSpecifier::TypeSpecWithTemplate:
1083 case NestedNameSpecifier::TypeSpec: {
1084 QualType T = QualType(NNS->getAsType(), 0);
1085 if (!T->isDependentType())
1086 return NNS;
1087
Douglas Gregorf9e7d3d2009-05-11 23:53:27 +00001088 T = InstantiateType(T, TemplateArgs, Range.getBegin(), DeclarationName());
Douglas Gregor1e589cc2009-03-26 23:50:42 +00001089 if (T.isNull())
1090 return 0;
1091
Eli Friedman1db6bab2009-06-13 04:51:30 +00001092 if (T->isDependentType() || T->isRecordType() ||
Douglas Gregord3022602009-03-27 23:10:48 +00001093 (getLangOptions().CPlusPlus0x && T->isEnumeralType())) {
Douglas Gregor77da5802009-04-01 00:28:59 +00001094 assert(T.getCVRQualifiers() == 0 && "Can't get cv-qualifiers here");
Douglas Gregord3022602009-03-27 23:10:48 +00001095 return NestedNameSpecifier::Create(Context, Prefix,
Douglas Gregor1e589cc2009-03-26 23:50:42 +00001096 NNS->getKind() == NestedNameSpecifier::TypeSpecWithTemplate,
Douglas Gregord3022602009-03-27 23:10:48 +00001097 T.getTypePtr());
1098 }
1099
1100 Diag(Range.getBegin(), diag::err_nested_name_spec_non_tag) << T;
1101 return 0;
Douglas Gregor1e589cc2009-03-26 23:50:42 +00001102 }
1103 }
1104
Douglas Gregord3022602009-03-27 23:10:48 +00001105 // Required to silence a GCC warning
Douglas Gregor1e589cc2009-03-26 23:50:42 +00001106 return 0;
Douglas Gregor47bde7c2009-03-19 17:26:29 +00001107}
Douglas Gregor15a92852009-03-31 18:38:02 +00001108
1109TemplateName
1110Sema::InstantiateTemplateName(TemplateName Name, SourceLocation Loc,
Douglas Gregorf9e7d3d2009-05-11 23:53:27 +00001111 const TemplateArgumentList &TemplateArgs) {
Douglas Gregor15a92852009-03-31 18:38:02 +00001112 if (TemplateTemplateParmDecl *TTP
1113 = dyn_cast_or_null<TemplateTemplateParmDecl>(
1114 Name.getAsTemplateDecl())) {
1115 assert(TTP->getDepth() == 0 &&
1116 "Cannot reduce depth of a template template parameter");
Douglas Gregoraed98042009-03-31 20:22:05 +00001117 assert(TemplateArgs[TTP->getPosition()].getAsDecl() &&
Douglas Gregor15a92852009-03-31 18:38:02 +00001118 "Wrong kind of template template argument");
1119 ClassTemplateDecl *ClassTemplate
1120 = dyn_cast<ClassTemplateDecl>(
1121 TemplateArgs[TTP->getPosition()].getAsDecl());
Douglas Gregoraed98042009-03-31 20:22:05 +00001122 assert(ClassTemplate && "Expected a class template");
Douglas Gregor15a92852009-03-31 18:38:02 +00001123 if (QualifiedTemplateName *QTN = Name.getAsQualifiedTemplateName()) {
1124 NestedNameSpecifier *NNS
1125 = InstantiateNestedNameSpecifier(QTN->getQualifier(),
1126 /*FIXME=*/SourceRange(Loc),
Douglas Gregorf9e7d3d2009-05-11 23:53:27 +00001127 TemplateArgs);
Douglas Gregor15a92852009-03-31 18:38:02 +00001128 if (NNS)
1129 return Context.getQualifiedTemplateName(NNS,
1130 QTN->hasTemplateKeyword(),
1131 ClassTemplate);
1132 }
1133
1134 return TemplateName(ClassTemplate);
1135 } else if (DependentTemplateName *DTN = Name.getAsDependentTemplateName()) {
1136 NestedNameSpecifier *NNS
1137 = InstantiateNestedNameSpecifier(DTN->getQualifier(),
1138 /*FIXME=*/SourceRange(Loc),
Douglas Gregorf9e7d3d2009-05-11 23:53:27 +00001139 TemplateArgs);
Douglas Gregor15a92852009-03-31 18:38:02 +00001140
1141 if (!NNS) // FIXME: Not the best recovery strategy.
1142 return Name;
1143
1144 if (NNS->isDependent())
1145 return Context.getDependentTemplateName(NNS, DTN->getName());
1146
1147 // Somewhat redundant with ActOnDependentTemplateName.
1148 CXXScopeSpec SS;
1149 SS.setRange(SourceRange(Loc));
1150 SS.setScopeRep(NNS);
1151 TemplateTy Template;
1152 TemplateNameKind TNK = isTemplateName(*DTN->getName(), 0, Template, &SS);
1153 if (TNK == TNK_Non_template) {
1154 Diag(Loc, diag::err_template_kw_refers_to_non_template)
1155 << DTN->getName();
1156 return Name;
1157 } else if (TNK == TNK_Function_template) {
1158 Diag(Loc, diag::err_template_kw_refers_to_non_template)
1159 << DTN->getName();
1160 return Name;
1161 }
1162
1163 return Template.getAsVal<TemplateName>();
1164 }
1165
1166
1167
Mike Stumpe127ae32009-05-16 07:39:55 +00001168 // FIXME: Even if we're referring to a Decl that isn't a template template
1169 // parameter, we may need to instantiate the outer contexts of that
1170 // Decl. However, this won't be needed until we implement member templates.
Douglas Gregor15a92852009-03-31 18:38:02 +00001171 return Name;
1172}
Douglas Gregorb320b9e2009-06-11 00:06:24 +00001173
1174TemplateArgument Sema::Instantiate(TemplateArgument Arg,
1175 const TemplateArgumentList &TemplateArgs) {
1176 switch (Arg.getKind()) {
1177 case TemplateArgument::Null:
1178 assert(false && "Should never have a NULL template argument");
1179 break;
1180
1181 case TemplateArgument::Type: {
1182 QualType T = InstantiateType(Arg.getAsType(), TemplateArgs,
1183 Arg.getLocation(), DeclarationName());
1184 if (T.isNull())
1185 return TemplateArgument();
1186
1187 return TemplateArgument(Arg.getLocation(), T);
1188 }
1189
1190 case TemplateArgument::Declaration:
1191 // FIXME: Template instantiation for template template parameters.
1192 return Arg;
1193
1194 case TemplateArgument::Integral:
1195 return Arg;
1196
1197 case TemplateArgument::Expression: {
Douglas Gregora8b2fbf2009-06-22 20:57:11 +00001198 // Template argument expressions are not potentially evaluated.
1199 EnterExpressionEvaluationContext Unevaluated(*this, Action::Unevaluated);
1200
Douglas Gregorb320b9e2009-06-11 00:06:24 +00001201 Sema::OwningExprResult E = InstantiateExpr(Arg.getAsExpr(), TemplateArgs);
1202 if (E.isInvalid())
1203 return TemplateArgument();
1204 return TemplateArgument(E.takeAs<Expr>());
1205 }
Anders Carlsson584b5062009-06-15 17:04:53 +00001206
1207 case TemplateArgument::Pack:
1208 assert(0 && "FIXME: Implement!");
1209 break;
Douglas Gregorb320b9e2009-06-11 00:06:24 +00001210 }
1211
1212 assert(false && "Unhandled template argument kind");
1213 return TemplateArgument();
1214}