blob: 5e4fde7af99c5b8d929666838aa7f9e151223d59 [file] [log] [blame]
Douglas Gregor74296542009-02-27 19:31:52 +00001//===------- SemaTemplateInstantiate.cpp - C++ Template Instantiation ------===/
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//===----------------------------------------------------------------------===/
8//
9// This file implements C++ template instantiation.
10//
11//===----------------------------------------------------------------------===/
12
13#include "Sema.h"
Douglas Gregordc18e892009-05-26 20:50:29 +000014#include "clang/AST/ASTConsumer.h"
Douglas Gregor74296542009-02-27 19:31:52 +000015#include "clang/AST/ASTContext.h"
16#include "clang/AST/Expr.h"
Douglas Gregor74296542009-02-27 19:31:52 +000017#include "clang/AST/DeclTemplate.h"
18#include "clang/Parse/DeclSpec.h"
19#include "clang/Basic/LangOptions.h"
Douglas Gregorf57dcd02009-02-28 00:25:32 +000020#include "llvm/Support/Compiler.h"
Douglas Gregor74296542009-02-27 19:31:52 +000021
22using namespace clang;
23
Douglas Gregorfee85d62009-03-10 18:03:33 +000024//===----------------------------------------------------------------------===/
25// Template Instantiation Support
26//===----------------------------------------------------------------------===/
27
Douglas Gregor5f62c5e2009-05-14 23:26:13 +000028/// \brief Retrieve the template argument list that should be used to
29/// instantiate the given declaration.
30const TemplateArgumentList &
31Sema::getTemplateInstantiationArgs(NamedDecl *D) {
Douglas Gregor6f5e0542009-06-26 00:10:03 +000032 // Template arguments for a class template specialization.
Douglas Gregor5f62c5e2009-05-14 23:26:13 +000033 if (ClassTemplateSpecializationDecl *Spec
34 = dyn_cast<ClassTemplateSpecializationDecl>(D))
35 return Spec->getTemplateArgs();
36
Douglas Gregor6f5e0542009-06-26 00:10:03 +000037 // Template arguments for a function template specialization.
38 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D))
39 if (const TemplateArgumentList *TemplateArgs
40 = Function->getTemplateSpecializationArgs())
41 return *TemplateArgs;
42
43 // Template arguments for a member of a class template specialization.
Douglas Gregor5f62c5e2009-05-14 23:26:13 +000044 DeclContext *EnclosingTemplateCtx = D->getDeclContext();
45 while (!isa<ClassTemplateSpecializationDecl>(EnclosingTemplateCtx)) {
46 assert(!EnclosingTemplateCtx->isFileContext() &&
47 "Tried to get the instantiation arguments of a non-template");
48 EnclosingTemplateCtx = EnclosingTemplateCtx->getParent();
49 }
50
51 ClassTemplateSpecializationDecl *EnclosingTemplate
52 = cast<ClassTemplateSpecializationDecl>(EnclosingTemplateCtx);
53 return EnclosingTemplate->getTemplateArgs();
54}
55
Douglas Gregor375733c2009-03-10 00:06:19 +000056Sema::InstantiatingTemplate::
57InstantiatingTemplate(Sema &SemaRef, SourceLocation PointOfInstantiation,
Douglas Gregorb12249d2009-05-18 17:01:57 +000058 Decl *Entity,
Douglas Gregor375733c2009-03-10 00:06:19 +000059 SourceRange InstantiationRange)
60 : SemaRef(SemaRef) {
Douglas Gregor56d25a72009-03-10 20:44:00 +000061
62 Invalid = CheckInstantiationDepth(PointOfInstantiation,
63 InstantiationRange);
64 if (!Invalid) {
Douglas Gregor375733c2009-03-10 00:06:19 +000065 ActiveTemplateInstantiation Inst;
Douglas Gregor56d25a72009-03-10 20:44:00 +000066 Inst.Kind = ActiveTemplateInstantiation::TemplateInstantiation;
Douglas Gregor375733c2009-03-10 00:06:19 +000067 Inst.PointOfInstantiation = PointOfInstantiation;
Douglas Gregor56d25a72009-03-10 20:44:00 +000068 Inst.Entity = reinterpret_cast<uintptr_t>(Entity);
Douglas Gregor95ba1282009-03-12 18:36:18 +000069 Inst.TemplateArgs = 0;
70 Inst.NumTemplateArgs = 0;
Douglas Gregor56d25a72009-03-10 20:44:00 +000071 Inst.InstantiationRange = InstantiationRange;
72 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
73 Invalid = false;
74 }
75}
76
77Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef,
78 SourceLocation PointOfInstantiation,
79 TemplateDecl *Template,
80 const TemplateArgument *TemplateArgs,
81 unsigned NumTemplateArgs,
82 SourceRange InstantiationRange)
83 : SemaRef(SemaRef) {
84
85 Invalid = CheckInstantiationDepth(PointOfInstantiation,
86 InstantiationRange);
87 if (!Invalid) {
88 ActiveTemplateInstantiation Inst;
89 Inst.Kind
90 = ActiveTemplateInstantiation::DefaultTemplateArgumentInstantiation;
91 Inst.PointOfInstantiation = PointOfInstantiation;
92 Inst.Entity = reinterpret_cast<uintptr_t>(Template);
93 Inst.TemplateArgs = TemplateArgs;
94 Inst.NumTemplateArgs = NumTemplateArgs;
Douglas Gregor375733c2009-03-10 00:06:19 +000095 Inst.InstantiationRange = InstantiationRange;
96 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
97 Invalid = false;
98 }
99}
100
Douglas Gregorc5e01af2009-06-10 23:47:09 +0000101Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef,
102 SourceLocation PointOfInstantiation,
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 Gregorb12249d2009-05-18 17:01:57 +0000125void Sema::InstantiatingTemplate::Clear() {
126 if (!Invalid) {
Douglas Gregor375733c2009-03-10 00:06:19 +0000127 SemaRef.ActiveTemplateInstantiations.pop_back();
Douglas Gregorb12249d2009-05-18 17:01:57 +0000128 Invalid = true;
129 }
Douglas Gregor375733c2009-03-10 00:06:19 +0000130}
131
Douglas Gregor56d25a72009-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 Gregorfee85d62009-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 Gregor56d25a72009-03-10 20:44:00 +0000156 switch (Active->Kind) {
157 case ActiveTemplateInstantiation::TemplateInstantiation: {
Douglas Gregorb12249d2009-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 Gregor6f5e0542009-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 Gregorb12249d2009-05-18 17:01:57 +0000174 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
175 DiagID)
176 << Function
177 << Active->InstantiationRange;
178 }
Douglas Gregor56d25a72009-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 Gregordd13e842009-03-30 22:58:21 +0000185 = TemplateSpecializationType::PrintTemplateArgumentList(
Douglas Gregor56d25a72009-03-10 20:44:00 +0000186 Active->TemplateArgs,
Douglas Gregor3bf3bbc2009-05-29 20:38:28 +0000187 Active->NumTemplateArgs,
188 Context.PrintingPolicy);
Douglas Gregor56d25a72009-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 Gregorc5e01af2009-06-10 23:47:09 +0000195
196 case ActiveTemplateInstantiation::PartialSpecDeductionInstantiation: {
197 ClassTemplatePartialSpecializationDecl *PartialSpec
198 = cast<ClassTemplatePartialSpecializationDecl>((Decl *)Active->Entity);
Douglas Gregorc5e01af2009-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 Gregor8f378a92009-06-11 18:10:32 +0000204 << Context.getTypeDeclType(PartialSpec)
Douglas Gregorc5e01af2009-06-10 23:47:09 +0000205 << Active->InstantiationRange;
206 break;
207 }
208
Douglas Gregor56d25a72009-03-10 20:44:00 +0000209 }
Douglas Gregorfee85d62009-03-10 18:03:33 +0000210 }
211}
212
Douglas Gregor95d6c952009-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 Gregor74296542009-02-27 19:31:52 +0000241//===----------------------------------------------------------------------===/
242// Template Instantiation for Types
243//===----------------------------------------------------------------------===/
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000244namespace {
245 class VISIBILITY_HIDDEN TemplateTypeInstantiator {
246 Sema &SemaRef;
Douglas Gregorf9e7d3d2009-05-11 23:53:27 +0000247 const TemplateArgumentList &TemplateArgs;
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000248 SourceLocation Loc;
249 DeclarationName Entity;
Douglas Gregor74296542009-02-27 19:31:52 +0000250
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000251 public:
252 TemplateTypeInstantiator(Sema &SemaRef,
Douglas Gregorf9e7d3d2009-05-11 23:53:27 +0000253 const TemplateArgumentList &TemplateArgs,
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000254 SourceLocation Loc,
255 DeclarationName Entity)
256 : SemaRef(SemaRef), TemplateArgs(TemplateArgs),
Douglas Gregorf9e7d3d2009-05-11 23:53:27 +0000257 Loc(Loc), Entity(Entity) { }
Douglas Gregorf57dcd02009-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) \
265 QualType Instantiate##Class##Type(const Class##Type *T, \
266 unsigned Quals) const;
267#define ABSTRACT_TYPE(Class, Base)
268#include "clang/AST/TypeNodes.def"
269 };
270}
271
272QualType
273TemplateTypeInstantiator::InstantiateExtQualType(const ExtQualType *T,
274 unsigned Quals) const {
Douglas Gregor74296542009-02-27 19:31:52 +0000275 // FIXME: Implement this
276 assert(false && "Cannot instantiate ExtQualType yet");
277 return QualType();
278}
279
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000280QualType
281TemplateTypeInstantiator::InstantiateBuiltinType(const BuiltinType *T,
282 unsigned Quals) const {
Douglas Gregorc0139ca2009-02-28 01:04:19 +0000283 assert(false && "Builtin types are not dependent and cannot be instantiated");
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000284 return QualType(T, Quals);
Douglas Gregor74296542009-02-27 19:31:52 +0000285}
286
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000287QualType
288TemplateTypeInstantiator::
289InstantiateFixedWidthIntType(const FixedWidthIntType *T, unsigned Quals) const {
Douglas Gregor74296542009-02-27 19:31:52 +0000290 // FIXME: Implement this
291 assert(false && "Cannot instantiate FixedWidthIntType yet");
292 return QualType();
293}
294
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000295QualType
296TemplateTypeInstantiator::InstantiateComplexType(const ComplexType *T,
297 unsigned Quals) const {
Douglas Gregor74296542009-02-27 19:31:52 +0000298 // FIXME: Implement this
299 assert(false && "Cannot instantiate ComplexType yet");
300 return QualType();
301}
302
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000303QualType
304TemplateTypeInstantiator::InstantiatePointerType(const PointerType *T,
305 unsigned Quals) const {
306 QualType PointeeType = Instantiate(T->getPointeeType());
307 if (PointeeType.isNull())
308 return QualType();
309
310 return SemaRef.BuildPointerType(PointeeType, Quals, Loc, Entity);
Douglas Gregor74296542009-02-27 19:31:52 +0000311}
312
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000313QualType
314TemplateTypeInstantiator::InstantiateBlockPointerType(const BlockPointerType *T,
315 unsigned Quals) const {
Anders Carlsson9d914d32009-06-12 16:23:10 +0000316 QualType PointeeType = Instantiate(T->getPointeeType());
317 if (PointeeType.isNull())
318 return QualType();
319
Anders Carlssondb52d342009-06-12 22:56:54 +0000320 return SemaRef.BuildBlockPointerType(PointeeType, Quals, Loc, Entity);
Douglas Gregor74296542009-02-27 19:31:52 +0000321}
322
Sebastian Redlce6fff02009-03-16 23:22:08 +0000323QualType
324TemplateTypeInstantiator::InstantiateLValueReferenceType(
325 const LValueReferenceType *T, unsigned Quals) const {
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000326 QualType ReferentType = Instantiate(T->getPointeeType());
327 if (ReferentType.isNull())
328 return QualType();
329
Sebastian Redlce6fff02009-03-16 23:22:08 +0000330 return SemaRef.BuildReferenceType(ReferentType, true, Quals, Loc, Entity);
331}
332
333QualType
334TemplateTypeInstantiator::InstantiateRValueReferenceType(
335 const RValueReferenceType *T, unsigned Quals) const {
336 QualType ReferentType = Instantiate(T->getPointeeType());
337 if (ReferentType.isNull())
338 return QualType();
339
340 return SemaRef.BuildReferenceType(ReferentType, false, Quals, Loc, Entity);
Douglas Gregor74296542009-02-27 19:31:52 +0000341}
342
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000343QualType
344TemplateTypeInstantiator::
345InstantiateMemberPointerType(const MemberPointerType *T,
346 unsigned Quals) const {
Douglas Gregora39e8302009-06-09 22:17:39 +0000347 QualType PointeeType = Instantiate(T->getPointeeType());
348 if (PointeeType.isNull())
349 return QualType();
350
351 QualType ClassType = Instantiate(QualType(T->getClass(), 0));
352 if (ClassType.isNull())
353 return QualType();
354
355 return SemaRef.BuildMemberPointerType(PointeeType, ClassType, Quals, Loc,
356 Entity);
Douglas Gregor74296542009-02-27 19:31:52 +0000357}
358
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000359QualType
360TemplateTypeInstantiator::
361InstantiateConstantArrayType(const ConstantArrayType *T,
362 unsigned Quals) const {
363 QualType ElementType = Instantiate(T->getElementType());
364 if (ElementType.isNull())
365 return ElementType;
366
367 // Build a temporary integer literal to specify the size for
368 // BuildArrayType. Since we have already checked the size as part of
369 // creating the dependent array type in the first place, we know
Douglas Gregor90177912009-05-13 18:28:20 +0000370 // there aren't any errors. However, we do need to determine what
371 // C++ type to give the size expression.
372 llvm::APInt Size = T->getSize();
373 QualType Types[] = {
374 SemaRef.Context.UnsignedCharTy, SemaRef.Context.UnsignedShortTy,
375 SemaRef.Context.UnsignedIntTy, SemaRef.Context.UnsignedLongTy,
376 SemaRef.Context.UnsignedLongLongTy, SemaRef.Context.UnsignedInt128Ty
377 };
378 const unsigned NumTypes = sizeof(Types) / sizeof(QualType);
379 QualType SizeType;
380 for (unsigned I = 0; I != NumTypes; ++I)
381 if (Size.getBitWidth() == SemaRef.Context.getIntWidth(Types[I])) {
382 SizeType = Types[I];
383 break;
384 }
385
386 if (SizeType.isNull())
387 SizeType = SemaRef.Context.getFixedWidthIntType(Size.getBitWidth(), false);
388
389 IntegerLiteral ArraySize(Size, SizeType, Loc);
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000390 return SemaRef.BuildArrayType(ElementType, T->getSizeModifier(),
391 &ArraySize, T->getIndexTypeQualifier(),
392 Loc, Entity);
Douglas Gregor74296542009-02-27 19:31:52 +0000393}
394
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000395QualType
396TemplateTypeInstantiator::
397InstantiateIncompleteArrayType(const IncompleteArrayType *T,
398 unsigned Quals) const {
399 QualType ElementType = Instantiate(T->getElementType());
400 if (ElementType.isNull())
401 return ElementType;
402
403 return SemaRef.BuildArrayType(ElementType, T->getSizeModifier(),
404 0, T->getIndexTypeQualifier(),
405 Loc, Entity);
Douglas Gregor74296542009-02-27 19:31:52 +0000406}
407
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000408QualType
409TemplateTypeInstantiator::
410InstantiateVariableArrayType(const VariableArrayType *T,
411 unsigned Quals) const {
Douglas Gregor74296542009-02-27 19:31:52 +0000412 // FIXME: Implement this
413 assert(false && "Cannot instantiate VariableArrayType yet");
414 return QualType();
415}
416
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000417QualType
418TemplateTypeInstantiator::
419InstantiateDependentSizedArrayType(const DependentSizedArrayType *T,
420 unsigned Quals) const {
Anders Carlsson149f2782009-03-15 20:12:13 +0000421 Expr *ArraySize = T->getSizeExpr();
422 assert(ArraySize->isValueDependent() &&
423 "dependent sized array types must have value dependent size expr");
424
425 // Instantiate the element type if needed
426 QualType ElementType = T->getElementType();
427 if (ElementType->isDependentType()) {
428 ElementType = Instantiate(ElementType);
429 if (ElementType.isNull())
430 return QualType();
431 }
432
433 // Instantiate the size expression
Douglas Gregora8b2fbf2009-06-22 20:57:11 +0000434 EnterExpressionEvaluationContext Unevaluated(SemaRef, Action::Unevaluated);
Anders Carlsson149f2782009-03-15 20:12:13 +0000435 Sema::OwningExprResult InstantiatedArraySize =
Douglas Gregorf9e7d3d2009-05-11 23:53:27 +0000436 SemaRef.InstantiateExpr(ArraySize, TemplateArgs);
Anders Carlsson149f2782009-03-15 20:12:13 +0000437 if (InstantiatedArraySize.isInvalid())
438 return QualType();
439
440 return SemaRef.BuildArrayType(ElementType, T->getSizeModifier(),
Anders Carlsson39ecdcf2009-05-01 19:49:17 +0000441 InstantiatedArraySize.takeAs<Expr>(),
Anders Carlsson149f2782009-03-15 20:12:13 +0000442 T->getIndexTypeQualifier(), Loc, Entity);
Douglas Gregor74296542009-02-27 19:31:52 +0000443}
444
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000445QualType
Douglas Gregor2a2e0402009-06-17 21:51:59 +0000446TemplateTypeInstantiator::
447InstantiateDependentSizedExtVectorType(const DependentSizedExtVectorType *T,
448 unsigned Quals) const {
449
Douglas Gregor68a7a6d2009-06-18 18:45:36 +0000450 // Instantiate the element type if needed.
Douglas Gregor2a2e0402009-06-17 21:51:59 +0000451 QualType ElementType = T->getElementType();
452 if (ElementType->isDependentType()) {
453 ElementType = Instantiate(ElementType);
454 if (ElementType.isNull())
455 return QualType();
456 }
457
Douglas Gregora8b2fbf2009-06-22 20:57:11 +0000458 // The expression in a dependent-sized extended vector type is not
459 // potentially evaluated.
460 EnterExpressionEvaluationContext Unevaluated(SemaRef, Action::Unevaluated);
461
Douglas Gregor68a7a6d2009-06-18 18:45:36 +0000462 // Instantiate the size expression.
463 const Expr *SizeExpr = T->getSizeExpr();
Douglas Gregor2a2e0402009-06-17 21:51:59 +0000464 Sema::OwningExprResult InstantiatedArraySize =
Douglas Gregor68a7a6d2009-06-18 18:45:36 +0000465 SemaRef.InstantiateExpr(const_cast<Expr *>(SizeExpr), TemplateArgs);
Douglas Gregor2a2e0402009-06-17 21:51:59 +0000466 if (InstantiatedArraySize.isInvalid())
467 return QualType();
468
469 return SemaRef.BuildExtVectorType(ElementType,
470 SemaRef.Owned(
471 InstantiatedArraySize.takeAs<Expr>()),
472 T->getAttributeLoc());
473}
474
475QualType
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000476TemplateTypeInstantiator::InstantiateVectorType(const VectorType *T,
477 unsigned Quals) const {
Douglas Gregor74296542009-02-27 19:31:52 +0000478 // FIXME: Implement this
479 assert(false && "Cannot instantiate VectorType yet");
480 return QualType();
481}
482
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000483QualType
484TemplateTypeInstantiator::InstantiateExtVectorType(const ExtVectorType *T,
485 unsigned Quals) const {
Douglas Gregor74296542009-02-27 19:31:52 +0000486 // FIXME: Implement this
487 assert(false && "Cannot instantiate ExtVectorType yet");
488 return QualType();
489}
490
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000491QualType
492TemplateTypeInstantiator::
493InstantiateFunctionProtoType(const FunctionProtoType *T,
494 unsigned Quals) const {
Douglas Gregorc0139ca2009-02-28 01:04:19 +0000495 QualType ResultType = Instantiate(T->getResultType());
496 if (ResultType.isNull())
497 return ResultType;
498
Douglas Gregor4ac20ef2009-05-29 18:27:38 +0000499 llvm::SmallVector<QualType, 4> ParamTypes;
Douglas Gregorc0139ca2009-02-28 01:04:19 +0000500 for (FunctionProtoType::arg_type_iterator Param = T->arg_type_begin(),
501 ParamEnd = T->arg_type_end();
502 Param != ParamEnd; ++Param) {
503 QualType P = Instantiate(*Param);
504 if (P.isNull())
505 return P;
506
507 ParamTypes.push_back(P);
508 }
509
Douglas Gregor8f378a92009-06-11 18:10:32 +0000510 return SemaRef.BuildFunctionType(ResultType, ParamTypes.data(),
Douglas Gregorc0139ca2009-02-28 01:04:19 +0000511 ParamTypes.size(),
512 T->isVariadic(), T->getTypeQuals(),
513 Loc, Entity);
Douglas Gregor74296542009-02-27 19:31:52 +0000514}
515
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000516QualType
517TemplateTypeInstantiator::
518InstantiateFunctionNoProtoType(const FunctionNoProtoType *T,
519 unsigned Quals) const {
Douglas Gregorc0139ca2009-02-28 01:04:19 +0000520 assert(false && "Functions without prototypes cannot be dependent.");
Douglas Gregor74296542009-02-27 19:31:52 +0000521 return QualType();
522}
523
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000524QualType
525TemplateTypeInstantiator::InstantiateTypedefType(const TypedefType *T,
526 unsigned Quals) const {
Douglas Gregorf97986d2009-05-27 05:35:12 +0000527 TypedefDecl *Typedef
Douglas Gregor9cbe6cd2009-05-27 17:54:46 +0000528 = cast_or_null<TypedefDecl>(
529 SemaRef.InstantiateCurrentDeclRef(T->getDecl()));
Douglas Gregorf97986d2009-05-27 05:35:12 +0000530 if (!Typedef)
531 return QualType();
532
533 return SemaRef.Context.getTypeDeclType(Typedef);
Douglas Gregor74296542009-02-27 19:31:52 +0000534}
535
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000536QualType
537TemplateTypeInstantiator::InstantiateTypeOfExprType(const TypeOfExprType *T,
538 unsigned Quals) const {
Douglas Gregora8b2fbf2009-06-22 20:57:11 +0000539 // The expression in a typeof is not potentially evaluated.
540 EnterExpressionEvaluationContext Unevaluated(SemaRef, Action::Unevaluated);
541
Douglas Gregord4f14af2009-05-26 22:09:24 +0000542 Sema::OwningExprResult E
543 = SemaRef.InstantiateExpr(T->getUnderlyingExpr(), TemplateArgs);
544 if (E.isInvalid())
545 return QualType();
546
547 return SemaRef.Context.getTypeOfExprType(E.takeAs<Expr>());
Douglas Gregor74296542009-02-27 19:31:52 +0000548}
549
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000550QualType
551TemplateTypeInstantiator::InstantiateTypeOfType(const TypeOfType *T,
552 unsigned Quals) const {
Douglas Gregord4f14af2009-05-26 22:09:24 +0000553 QualType Underlying = Instantiate(T->getUnderlyingType());
554 if (Underlying.isNull())
555 return QualType();
556
557 return SemaRef.Context.getTypeOfType(Underlying);
Douglas Gregor74296542009-02-27 19:31:52 +0000558}
559
Anders Carlsson93ab5332009-06-24 19:06:50 +0000560QualType
561TemplateTypeInstantiator::InstantiateDecltypeType(const DecltypeType *T,
562 unsigned Quals) const {
563 Sema::OwningExprResult E
564 = SemaRef.InstantiateExpr(T->getUnderlyingExpr(), TemplateArgs);
565
566 if (E.isInvalid())
567 return QualType();
568
569 return SemaRef.Context.getDecltypeType(E.takeAs<Expr>());
570}
571
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000572QualType
573TemplateTypeInstantiator::InstantiateRecordType(const RecordType *T,
574 unsigned Quals) const {
Douglas Gregorf97986d2009-05-27 05:35:12 +0000575 RecordDecl *Record
Douglas Gregor9cbe6cd2009-05-27 17:54:46 +0000576 = cast_or_null<RecordDecl>(SemaRef.InstantiateCurrentDeclRef(T->getDecl()));
Douglas Gregorf97986d2009-05-27 05:35:12 +0000577 if (!Record)
578 return QualType();
579
580 return SemaRef.Context.getTypeDeclType(Record);
Douglas Gregor74296542009-02-27 19:31:52 +0000581}
582
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000583QualType
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000584TemplateTypeInstantiator::InstantiateEnumType(const EnumType *T,
585 unsigned Quals) const {
Douglas Gregorf97986d2009-05-27 05:35:12 +0000586 EnumDecl *Enum
Douglas Gregor9cbe6cd2009-05-27 17:54:46 +0000587 = cast_or_null<EnumDecl>(SemaRef.InstantiateCurrentDeclRef(T->getDecl()));
Douglas Gregorf97986d2009-05-27 05:35:12 +0000588 if (!Enum)
589 return QualType();
590
591 return SemaRef.Context.getTypeDeclType(Enum);
Douglas Gregor74296542009-02-27 19:31:52 +0000592}
593
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000594QualType
595TemplateTypeInstantiator::
596InstantiateTemplateTypeParmType(const TemplateTypeParmType *T,
597 unsigned Quals) const {
Douglas Gregor74296542009-02-27 19:31:52 +0000598 if (T->getDepth() == 0) {
599 // Replace the template type parameter with its corresponding
600 // template argument.
Douglas Gregor74296542009-02-27 19:31:52 +0000601 assert(TemplateArgs[T->getIndex()].getKind() == TemplateArgument::Type &&
602 "Template argument kind mismatch");
603 QualType Result = TemplateArgs[T->getIndex()].getAsType();
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000604 if (Result.isNull() || !Quals)
Douglas Gregor74296542009-02-27 19:31:52 +0000605 return Result;
606
607 // C++ [dcl.ref]p1:
608 // [...] Cv-qualified references are ill-formed except when
609 // the cv-qualifiers are introduced through the use of a
610 // typedef (7.1.3) or of a template type argument (14.3), in
611 // which case the cv-qualifiers are ignored.
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000612 if (Quals && Result->isReferenceType())
613 Quals = 0;
Douglas Gregor74296542009-02-27 19:31:52 +0000614
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000615 return QualType(Result.getTypePtr(), Quals | Result.getCVRQualifiers());
Douglas Gregor74296542009-02-27 19:31:52 +0000616 }
617
618 // The template type parameter comes from an inner template (e.g.,
619 // the template parameter list of a member template inside the
620 // template we are instantiating). Create a new template type
621 // parameter with the template "level" reduced by one.
622 return SemaRef.Context.getTemplateTypeParmType(T->getDepth() - 1,
623 T->getIndex(),
Anders Carlsson4e3d3552009-06-16 00:30:48 +0000624 T->isParameterPack(),
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000625 T->getName())
626 .getQualifiedType(Quals);
Douglas Gregor74296542009-02-27 19:31:52 +0000627}
628
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000629QualType
630TemplateTypeInstantiator::
Douglas Gregordd13e842009-03-30 22:58:21 +0000631InstantiateTemplateSpecializationType(
632 const TemplateSpecializationType *T,
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000633 unsigned Quals) const {
Douglas Gregor4ac20ef2009-05-29 18:27:38 +0000634 llvm::SmallVector<TemplateArgument, 4> InstantiatedTemplateArgs;
Douglas Gregorf9ff4b12009-03-09 23:48:35 +0000635 InstantiatedTemplateArgs.reserve(T->getNumArgs());
Douglas Gregordd13e842009-03-30 22:58:21 +0000636 for (TemplateSpecializationType::iterator Arg = T->begin(), ArgEnd = T->end();
Douglas Gregorf9ff4b12009-03-09 23:48:35 +0000637 Arg != ArgEnd; ++Arg) {
Douglas Gregorb320b9e2009-06-11 00:06:24 +0000638 TemplateArgument InstArg = SemaRef.Instantiate(*Arg, TemplateArgs);
639 if (InstArg.isNull())
640 return QualType();
Douglas Gregorf9ff4b12009-03-09 23:48:35 +0000641
Douglas Gregorb320b9e2009-06-11 00:06:24 +0000642 InstantiatedTemplateArgs.push_back(InstArg);
Douglas Gregorf9ff4b12009-03-09 23:48:35 +0000643 }
644
Mike Stumpe127ae32009-05-16 07:39:55 +0000645 // FIXME: We're missing the locations of the template name, '<', and '>'.
Douglas Gregor15a92852009-03-31 18:38:02 +0000646
647 TemplateName Name = SemaRef.InstantiateTemplateName(T->getTemplateName(),
648 Loc,
Douglas Gregorf9e7d3d2009-05-11 23:53:27 +0000649 TemplateArgs);
Douglas Gregor15a92852009-03-31 18:38:02 +0000650
651 return SemaRef.CheckTemplateIdType(Name, Loc, SourceLocation(),
Douglas Gregor8f378a92009-06-11 18:10:32 +0000652 InstantiatedTemplateArgs.data(),
Douglas Gregordd13e842009-03-30 22:58:21 +0000653 InstantiatedTemplateArgs.size(),
654 SourceLocation());
Douglas Gregor74296542009-02-27 19:31:52 +0000655}
656
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000657QualType
658TemplateTypeInstantiator::
Douglas Gregor734b4ba2009-03-19 00:18:19 +0000659InstantiateQualifiedNameType(const QualifiedNameType *T,
660 unsigned Quals) const {
Douglas Gregord3022602009-03-27 23:10:48 +0000661 // When we instantiated a qualified name type, there's no point in
662 // keeping the qualification around in the instantiated result. So,
663 // just instantiate the named type.
664 return (*this)(T->getNamedType());
665}
666
667QualType
668TemplateTypeInstantiator::
669InstantiateTypenameType(const TypenameType *T, unsigned Quals) const {
Douglas Gregor77da5802009-04-01 00:28:59 +0000670 if (const TemplateSpecializationType *TemplateId = T->getTemplateId()) {
671 // When the typename type refers to a template-id, the template-id
672 // is dependent and has enough information to instantiate the
673 // result of the typename type. Since we don't care about keeping
674 // the spelling of the typename type in template instantiations,
675 // we just instantiate the template-id.
676 return InstantiateTemplateSpecializationType(TemplateId, Quals);
677 }
678
Douglas Gregord3022602009-03-27 23:10:48 +0000679 NestedNameSpecifier *NNS
680 = SemaRef.InstantiateNestedNameSpecifier(T->getQualifier(),
681 SourceRange(Loc),
Douglas Gregorf9e7d3d2009-05-11 23:53:27 +0000682 TemplateArgs);
Douglas Gregord3022602009-03-27 23:10:48 +0000683 if (!NNS)
684 return QualType();
685
Douglas Gregor77da5802009-04-01 00:28:59 +0000686 return SemaRef.CheckTypenameType(NNS, *T->getIdentifier(), SourceRange(Loc));
Douglas Gregor734b4ba2009-03-19 00:18:19 +0000687}
688
689QualType
690TemplateTypeInstantiator::
Steve Naroffc75c1a82009-06-17 22:40:22 +0000691InstantiateObjCObjectPointerType(const ObjCObjectPointerType *T,
692 unsigned Quals) const {
693 assert(false && "Objective-C types cannot be dependent");
694 return QualType();
695}
696
697QualType
698TemplateTypeInstantiator::
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000699InstantiateObjCInterfaceType(const ObjCInterfaceType *T,
700 unsigned Quals) const {
701 assert(false && "Objective-C types cannot be dependent");
Douglas Gregor74296542009-02-27 19:31:52 +0000702 return QualType();
703}
704
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000705QualType
706TemplateTypeInstantiator::
707InstantiateObjCQualifiedInterfaceType(const ObjCQualifiedInterfaceType *T,
708 unsigned Quals) const {
709 assert(false && "Objective-C types cannot be dependent");
Douglas Gregor74296542009-02-27 19:31:52 +0000710 return QualType();
711}
712
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000713/// \brief The actual implementation of Sema::InstantiateType().
714QualType TemplateTypeInstantiator::Instantiate(QualType T) const {
715 // If T is not a dependent type, there is nothing to do.
716 if (!T->isDependentType())
717 return T;
718
719 switch (T->getTypeClass()) {
720#define TYPE(Class, Base) \
721 case Type::Class: \
722 return Instantiate##Class##Type(cast<Class##Type>(T.getTypePtr()), \
723 T.getCVRQualifiers());
724#define ABSTRACT_TYPE(Class, Base)
725#include "clang/AST/TypeNodes.def"
726 }
727
728 assert(false && "Not all types have been decoded for instantiation");
729 return QualType();
730}
Douglas Gregor74296542009-02-27 19:31:52 +0000731
732/// \brief Instantiate the type T with a given set of template arguments.
733///
734/// This routine substitutes the given template arguments into the
735/// type T and produces the instantiated type.
736///
737/// \param T the type into which the template arguments will be
738/// substituted. If this type is not dependent, it will be returned
739/// immediately.
740///
741/// \param TemplateArgs the template arguments that will be
742/// substituted for the top-level template parameters within T.
743///
Douglas Gregor74296542009-02-27 19:31:52 +0000744/// \param Loc the location in the source code where this substitution
745/// is being performed. It will typically be the location of the
746/// declarator (if we're instantiating the type of some declaration)
747/// or the location of the type in the source code (if, e.g., we're
748/// instantiating the type of a cast expression).
749///
750/// \param Entity the name of the entity associated with a declaration
751/// being instantiated (if any). May be empty to indicate that there
752/// is no such entity (if, e.g., this is a type that occurs as part of
753/// a cast expression) or that the entity has no name (e.g., an
754/// unnamed function parameter).
755///
756/// \returns If the instantiation succeeds, the instantiated
757/// type. Otherwise, produces diagnostics and returns a NULL type.
758QualType Sema::InstantiateType(QualType T,
Douglas Gregorf9e7d3d2009-05-11 23:53:27 +0000759 const TemplateArgumentList &TemplateArgs,
Douglas Gregor74296542009-02-27 19:31:52 +0000760 SourceLocation Loc, DeclarationName Entity) {
Douglas Gregor56d25a72009-03-10 20:44:00 +0000761 assert(!ActiveTemplateInstantiations.empty() &&
762 "Cannot perform an instantiation without some context on the "
763 "instantiation stack");
764
Douglas Gregor74296542009-02-27 19:31:52 +0000765 // If T is not a dependent type, there is nothing to do.
766 if (!T->isDependentType())
767 return T;
768
Douglas Gregorf9e7d3d2009-05-11 23:53:27 +0000769 TemplateTypeInstantiator Instantiator(*this, TemplateArgs, Loc, Entity);
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000770 return Instantiator(T);
Douglas Gregor74296542009-02-27 19:31:52 +0000771}
Douglas Gregored3a3982009-03-03 04:44:36 +0000772
773/// \brief Instantiate the base class specifiers of the given class
774/// template specialization.
775///
776/// Produces a diagnostic and returns true on error, returns false and
777/// attaches the instantiated base classes to the class template
778/// specialization if successful.
779bool
Douglas Gregorcc887972009-03-25 21:17:03 +0000780Sema::InstantiateBaseSpecifiers(CXXRecordDecl *Instantiation,
781 CXXRecordDecl *Pattern,
Douglas Gregorf9e7d3d2009-05-11 23:53:27 +0000782 const TemplateArgumentList &TemplateArgs) {
Douglas Gregored3a3982009-03-03 04:44:36 +0000783 bool Invalid = false;
Douglas Gregor4ac20ef2009-05-29 18:27:38 +0000784 llvm::SmallVector<CXXBaseSpecifier*, 4> InstantiatedBases;
Douglas Gregorcc887972009-03-25 21:17:03 +0000785 for (ClassTemplateSpecializationDecl::base_class_iterator
786 Base = Pattern->bases_begin(), BaseEnd = Pattern->bases_end();
Douglas Gregord9572a12009-03-10 18:52:44 +0000787 Base != BaseEnd; ++Base) {
Douglas Gregored3a3982009-03-03 04:44:36 +0000788 if (!Base->getType()->isDependentType()) {
789 // FIXME: Allocate via ASTContext
790 InstantiatedBases.push_back(new CXXBaseSpecifier(*Base));
791 continue;
792 }
793
794 QualType BaseType = InstantiateType(Base->getType(),
Douglas Gregorf9e7d3d2009-05-11 23:53:27 +0000795 TemplateArgs,
Douglas Gregored3a3982009-03-03 04:44:36 +0000796 Base->getSourceRange().getBegin(),
797 DeclarationName());
798 if (BaseType.isNull()) {
799 Invalid = true;
800 continue;
801 }
802
803 if (CXXBaseSpecifier *InstantiatedBase
Douglas Gregorcc887972009-03-25 21:17:03 +0000804 = CheckBaseSpecifier(Instantiation,
Douglas Gregored3a3982009-03-03 04:44:36 +0000805 Base->getSourceRange(),
806 Base->isVirtual(),
807 Base->getAccessSpecifierAsWritten(),
808 BaseType,
809 /*FIXME: Not totally accurate */
810 Base->getSourceRange().getBegin()))
811 InstantiatedBases.push_back(InstantiatedBase);
812 else
813 Invalid = true;
814 }
815
Douglas Gregord9572a12009-03-10 18:52:44 +0000816 if (!Invalid &&
Jay Foad9e6bef42009-05-21 09:52:38 +0000817 AttachBaseSpecifiers(Instantiation, InstantiatedBases.data(),
Douglas Gregored3a3982009-03-03 04:44:36 +0000818 InstantiatedBases.size()))
819 Invalid = true;
820
821 return Invalid;
822}
823
Douglas Gregorcc887972009-03-25 21:17:03 +0000824/// \brief Instantiate the definition of a class from a given pattern.
825///
826/// \param PointOfInstantiation The point of instantiation within the
827/// source code.
828///
829/// \param Instantiation is the declaration whose definition is being
830/// instantiated. This will be either a class template specialization
831/// or a member class of a class template specialization.
832///
833/// \param Pattern is the pattern from which the instantiation
834/// occurs. This will be either the declaration of a class template or
835/// the declaration of a member class of a class template.
836///
837/// \param TemplateArgs The template arguments to be substituted into
838/// the pattern.
839///
Douglas Gregorcc887972009-03-25 21:17:03 +0000840/// \returns true if an error occurred, false otherwise.
841bool
842Sema::InstantiateClass(SourceLocation PointOfInstantiation,
843 CXXRecordDecl *Instantiation, CXXRecordDecl *Pattern,
Douglas Gregorfd79ac62009-05-13 00:25:59 +0000844 const TemplateArgumentList &TemplateArgs,
845 bool ExplicitInstantiation) {
Douglas Gregorcc887972009-03-25 21:17:03 +0000846 bool Invalid = false;
847
848 CXXRecordDecl *PatternDef
849 = cast_or_null<CXXRecordDecl>(Pattern->getDefinition(Context));
850 if (!PatternDef) {
851 if (Pattern == Instantiation->getInstantiatedFromMemberClass()) {
852 Diag(PointOfInstantiation,
853 diag::err_implicit_instantiate_member_undefined)
854 << Context.getTypeDeclType(Instantiation);
855 Diag(Pattern->getLocation(), diag::note_member_of_template_here);
856 } else {
Douglas Gregorfd79ac62009-05-13 00:25:59 +0000857 Diag(PointOfInstantiation, diag::err_template_instantiate_undefined)
858 << ExplicitInstantiation
Douglas Gregorcc887972009-03-25 21:17:03 +0000859 << Context.getTypeDeclType(Instantiation);
860 Diag(Pattern->getLocation(), diag::note_template_decl_here);
861 }
862 return true;
863 }
864 Pattern = PatternDef;
865
Douglas Gregor42c48522009-03-25 21:23:52 +0000866 InstantiatingTemplate Inst(*this, PointOfInstantiation, Instantiation);
Douglas Gregorcc887972009-03-25 21:17:03 +0000867 if (Inst)
868 return true;
869
870 // Enter the scope of this instantiation. We don't use
871 // PushDeclContext because we don't have a scope.
872 DeclContext *PreviousContext = CurContext;
873 CurContext = Instantiation;
874
875 // Start the definition of this instantiation.
876 Instantiation->startDefinition();
877
878 // Instantiate the base class specifiers.
Douglas Gregorf9e7d3d2009-05-11 23:53:27 +0000879 if (InstantiateBaseSpecifiers(Instantiation, Pattern, TemplateArgs))
Douglas Gregorcc887972009-03-25 21:17:03 +0000880 Invalid = true;
881
Douglas Gregor4ac20ef2009-05-29 18:27:38 +0000882 llvm::SmallVector<DeclPtrTy, 4> Fields;
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000883 for (RecordDecl::decl_iterator Member = Pattern->decls_begin(Context),
884 MemberEnd = Pattern->decls_end(Context);
885 Member != MemberEnd; ++Member) {
Douglas Gregorf9e7d3d2009-05-11 23:53:27 +0000886 Decl *NewMember = InstantiateDecl(*Member, Instantiation, TemplateArgs);
Douglas Gregorcc887972009-03-25 21:17:03 +0000887 if (NewMember) {
888 if (NewMember->isInvalidDecl())
889 Invalid = true;
890 else if (FieldDecl *Field = dyn_cast<FieldDecl>(NewMember))
Chris Lattner5261d0c2009-03-28 19:18:32 +0000891 Fields.push_back(DeclPtrTy::make(Field));
Douglas Gregorcc887972009-03-25 21:17:03 +0000892 } else {
893 // FIXME: Eventually, a NULL return will mean that one of the
Mike Stumpe127ae32009-05-16 07:39:55 +0000894 // instantiations was a semantic disaster, and we'll want to set Invalid =
895 // true. For now, we expect to skip some members that we can't yet handle.
Douglas Gregorcc887972009-03-25 21:17:03 +0000896 }
897 }
898
899 // Finish checking fields.
Chris Lattner5261d0c2009-03-28 19:18:32 +0000900 ActOnFields(0, Instantiation->getLocation(), DeclPtrTy::make(Instantiation),
Jay Foad9e6bef42009-05-21 09:52:38 +0000901 Fields.data(), Fields.size(), SourceLocation(), SourceLocation(),
Douglas Gregorcc887972009-03-25 21:17:03 +0000902 0);
903
904 // Add any implicitly-declared members that we might need.
905 AddImplicitlyDeclaredMembersToClass(Instantiation);
906
907 // Exit the scope of this instantiation.
908 CurContext = PreviousContext;
909
Douglas Gregordc18e892009-05-26 20:50:29 +0000910 if (!Invalid)
911 Consumer.HandleTagDeclDefinition(Instantiation);
912
Douglas Gregordf9f5d12009-05-13 20:28:22 +0000913 // If this is an explicit instantiation, instantiate our members, too.
Douglas Gregorb12249d2009-05-18 17:01:57 +0000914 if (!Invalid && ExplicitInstantiation) {
915 Inst.Clear();
Douglas Gregordf9f5d12009-05-13 20:28:22 +0000916 InstantiateClassMembers(PointOfInstantiation, Instantiation, TemplateArgs);
Douglas Gregorb12249d2009-05-18 17:01:57 +0000917 }
Douglas Gregordf9f5d12009-05-13 20:28:22 +0000918
Douglas Gregorcc887972009-03-25 21:17:03 +0000919 return Invalid;
920}
921
Douglas Gregored3a3982009-03-03 04:44:36 +0000922bool
923Sema::InstantiateClassTemplateSpecialization(
924 ClassTemplateSpecializationDecl *ClassTemplateSpec,
925 bool ExplicitInstantiation) {
926 // Perform the actual instantiation on the canonical declaration.
927 ClassTemplateSpec = cast<ClassTemplateSpecializationDecl>(
928 Context.getCanonicalDecl(ClassTemplateSpec));
929
930 // We can only instantiate something that hasn't already been
931 // instantiated or specialized. Fail without any diagnostics: our
932 // caller will provide an error message.
933 if (ClassTemplateSpec->getSpecializationKind() != TSK_Undeclared)
934 return true;
935
Douglas Gregored3a3982009-03-03 04:44:36 +0000936 ClassTemplateDecl *Template = ClassTemplateSpec->getSpecializedTemplate();
Douglas Gregorcc887972009-03-25 21:17:03 +0000937 CXXRecordDecl *Pattern = Template->getTemplatedDecl();
Douglas Gregor58944ac2009-05-31 09:31:02 +0000938 const TemplateArgumentList *TemplateArgs
939 = &ClassTemplateSpec->getTemplateArgs();
940
Douglas Gregor21530c52009-06-12 22:31:52 +0000941 // C++ [temp.class.spec.match]p1:
942 // When a class template is used in a context that requires an
943 // instantiation of the class, it is necessary to determine
944 // whether the instantiation is to be generated using the primary
945 // template or one of the partial specializations. This is done by
946 // matching the template arguments of the class template
947 // specialization with the template argument lists of the partial
948 // specializations.
Douglas Gregorab9f71a2009-06-05 00:53:49 +0000949 typedef std::pair<ClassTemplatePartialSpecializationDecl *,
950 TemplateArgumentList *> MatchResult;
951 llvm::SmallVector<MatchResult, 4> Matched;
Douglas Gregor58944ac2009-05-31 09:31:02 +0000952 for (llvm::FoldingSet<ClassTemplatePartialSpecializationDecl>::iterator
953 Partial = Template->getPartialSpecializations().begin(),
954 PartialEnd = Template->getPartialSpecializations().end();
955 Partial != PartialEnd;
956 ++Partial) {
Douglas Gregor623e2e02009-06-12 18:26:56 +0000957 TemplateDeductionInfo Info(Context);
958 if (TemplateDeductionResult Result
Douglas Gregorab9f71a2009-06-05 00:53:49 +0000959 = DeduceTemplateArguments(&*Partial,
Douglas Gregor623e2e02009-06-12 18:26:56 +0000960 ClassTemplateSpec->getTemplateArgs(),
961 Info)) {
962 // FIXME: Store the failed-deduction information for use in
963 // diagnostics, later.
964 (void)Result;
965 } else {
966 Matched.push_back(std::make_pair(&*Partial, Info.take()));
967 }
Douglas Gregor58944ac2009-05-31 09:31:02 +0000968 }
969
970 if (Matched.size() == 1) {
Douglas Gregor21530c52009-06-12 22:31:52 +0000971 // -- If exactly one matching specialization is found, the
972 // instantiation is generated from that specialization.
Douglas Gregorab9f71a2009-06-05 00:53:49 +0000973 Pattern = Matched[0].first;
974 TemplateArgs = Matched[0].second;
Douglas Gregor58944ac2009-05-31 09:31:02 +0000975 } else if (Matched.size() > 1) {
Douglas Gregor21530c52009-06-12 22:31:52 +0000976 // -- If more than one matching specialization is found, the
977 // partial order rules (14.5.4.2) are used to determine
978 // whether one of the specializations is more specialized
979 // than the others. If none of the specializations is more
980 // specialized than all of the other matching
981 // specializations, then the use of the class template is
982 // ambiguous and the program is ill-formed.
Douglas Gregor58944ac2009-05-31 09:31:02 +0000983 // FIXME: Implement partial ordering of class template partial
984 // specializations.
985 Diag(ClassTemplateSpec->getLocation(),
986 diag::unsup_template_partial_spec_ordering);
Douglas Gregor21530c52009-06-12 22:31:52 +0000987 } else {
988 // -- If no matches are found, the instantiation is generated
989 // from the primary template.
990
991 // Since we initialized the pattern and template arguments from
992 // the primary template, there is nothing more we need to do here.
Douglas Gregor58944ac2009-05-31 09:31:02 +0000993 }
Douglas Gregored3a3982009-03-03 04:44:36 +0000994
995 // Note that this is an instantiation.
996 ClassTemplateSpec->setSpecializationKind(
997 ExplicitInstantiation? TSK_ExplicitInstantiation
998 : TSK_ImplicitInstantiation);
999
Douglas Gregorab9f71a2009-06-05 00:53:49 +00001000 bool Result = InstantiateClass(ClassTemplateSpec->getLocation(),
1001 ClassTemplateSpec, Pattern, *TemplateArgs,
1002 ExplicitInstantiation);
1003
1004 for (unsigned I = 0, N = Matched.size(); I != N; ++I) {
1005 // FIXME: Implement TemplateArgumentList::Destroy!
1006 // if (Matched[I].first != Pattern)
1007 // Matched[I].second->Destroy(Context);
1008 }
1009
1010 return Result;
Douglas Gregored3a3982009-03-03 04:44:36 +00001011}
Douglas Gregor47bde7c2009-03-19 17:26:29 +00001012
Douglas Gregordf9f5d12009-05-13 20:28:22 +00001013/// \brief Instantiate the definitions of all of the member of the
1014/// given class, which is an instantiation of a class template or a
1015/// member class of a template.
1016void
1017Sema::InstantiateClassMembers(SourceLocation PointOfInstantiation,
1018 CXXRecordDecl *Instantiation,
1019 const TemplateArgumentList &TemplateArgs) {
1020 for (DeclContext::decl_iterator D = Instantiation->decls_begin(Context),
1021 DEnd = Instantiation->decls_end(Context);
1022 D != DEnd; ++D) {
1023 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(*D)) {
1024 if (!Function->getBody(Context))
Douglas Gregorb12249d2009-05-18 17:01:57 +00001025 InstantiateFunctionDefinition(PointOfInstantiation, Function);
Douglas Gregordf9f5d12009-05-13 20:28:22 +00001026 } else if (VarDecl *Var = dyn_cast<VarDecl>(*D)) {
1027 const VarDecl *Def = 0;
1028 if (!Var->getDefinition(Def))
1029 InstantiateVariableDefinition(Var);
1030 } else if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(*D)) {
1031 if (!Record->isInjectedClassName() && !Record->getDefinition(Context)) {
1032 assert(Record->getInstantiatedFromMemberClass() &&
1033 "Missing instantiated-from-template information");
Douglas Gregorb12249d2009-05-18 17:01:57 +00001034 InstantiateClass(PointOfInstantiation, Record,
Douglas Gregordf9f5d12009-05-13 20:28:22 +00001035 Record->getInstantiatedFromMemberClass(),
1036 TemplateArgs, true);
1037 }
1038 }
1039 }
1040}
1041
1042/// \brief Instantiate the definitions of all of the members of the
1043/// given class template specialization, which was named as part of an
1044/// explicit instantiation.
1045void Sema::InstantiateClassTemplateSpecializationMembers(
1046 SourceLocation PointOfInstantiation,
1047 ClassTemplateSpecializationDecl *ClassTemplateSpec) {
1048 // C++0x [temp.explicit]p7:
1049 // An explicit instantiation that names a class template
1050 // specialization is an explicit instantion of the same kind
1051 // (declaration or definition) of each of its members (not
1052 // including members inherited from base classes) that has not
1053 // been previously explicitly specialized in the translation unit
1054 // containing the explicit instantiation, except as described
1055 // below.
1056 InstantiateClassMembers(PointOfInstantiation, ClassTemplateSpec,
1057 ClassTemplateSpec->getTemplateArgs());
1058}
1059
Douglas Gregor1e589cc2009-03-26 23:50:42 +00001060/// \brief Instantiate a nested-name-specifier.
1061NestedNameSpecifier *
1062Sema::InstantiateNestedNameSpecifier(NestedNameSpecifier *NNS,
1063 SourceRange Range,
Douglas Gregorf9e7d3d2009-05-11 23:53:27 +00001064 const TemplateArgumentList &TemplateArgs) {
Douglas Gregor1e589cc2009-03-26 23:50:42 +00001065 // Instantiate the prefix of this nested name specifier.
1066 NestedNameSpecifier *Prefix = NNS->getPrefix();
1067 if (Prefix) {
Douglas Gregorf9e7d3d2009-05-11 23:53:27 +00001068 Prefix = InstantiateNestedNameSpecifier(Prefix, Range, TemplateArgs);
Douglas Gregor1e589cc2009-03-26 23:50:42 +00001069 if (!Prefix)
1070 return 0;
Douglas Gregor47bde7c2009-03-19 17:26:29 +00001071 }
1072
Douglas Gregor1e589cc2009-03-26 23:50:42 +00001073 switch (NNS->getKind()) {
Douglas Gregor77da5802009-04-01 00:28:59 +00001074 case NestedNameSpecifier::Identifier: {
1075 assert(Prefix &&
1076 "Can't have an identifier nested-name-specifier with no prefix");
1077 CXXScopeSpec SS;
1078 // FIXME: The source location information is all wrong.
1079 SS.setRange(Range);
1080 SS.setScopeRep(Prefix);
1081 return static_cast<NestedNameSpecifier *>(
1082 ActOnCXXNestedNameSpecifier(0, SS,
1083 Range.getEnd(),
1084 Range.getEnd(),
Douglas Gregor96b6df92009-05-14 00:28:11 +00001085 *NNS->getAsIdentifier()));
Douglas Gregor1e589cc2009-03-26 23:50:42 +00001086 break;
Douglas Gregor77da5802009-04-01 00:28:59 +00001087 }
Douglas Gregor1e589cc2009-03-26 23:50:42 +00001088
1089 case NestedNameSpecifier::Namespace:
1090 case NestedNameSpecifier::Global:
1091 return NNS;
1092
1093 case NestedNameSpecifier::TypeSpecWithTemplate:
1094 case NestedNameSpecifier::TypeSpec: {
1095 QualType T = QualType(NNS->getAsType(), 0);
1096 if (!T->isDependentType())
1097 return NNS;
1098
Douglas Gregorf9e7d3d2009-05-11 23:53:27 +00001099 T = InstantiateType(T, TemplateArgs, Range.getBegin(), DeclarationName());
Douglas Gregor1e589cc2009-03-26 23:50:42 +00001100 if (T.isNull())
1101 return 0;
1102
Eli Friedman1db6bab2009-06-13 04:51:30 +00001103 if (T->isDependentType() || T->isRecordType() ||
Douglas Gregord3022602009-03-27 23:10:48 +00001104 (getLangOptions().CPlusPlus0x && T->isEnumeralType())) {
Douglas Gregor77da5802009-04-01 00:28:59 +00001105 assert(T.getCVRQualifiers() == 0 && "Can't get cv-qualifiers here");
Douglas Gregord3022602009-03-27 23:10:48 +00001106 return NestedNameSpecifier::Create(Context, Prefix,
Douglas Gregor1e589cc2009-03-26 23:50:42 +00001107 NNS->getKind() == NestedNameSpecifier::TypeSpecWithTemplate,
Douglas Gregord3022602009-03-27 23:10:48 +00001108 T.getTypePtr());
1109 }
1110
1111 Diag(Range.getBegin(), diag::err_nested_name_spec_non_tag) << T;
1112 return 0;
Douglas Gregor1e589cc2009-03-26 23:50:42 +00001113 }
1114 }
1115
Douglas Gregord3022602009-03-27 23:10:48 +00001116 // Required to silence a GCC warning
Douglas Gregor1e589cc2009-03-26 23:50:42 +00001117 return 0;
Douglas Gregor47bde7c2009-03-19 17:26:29 +00001118}
Douglas Gregor15a92852009-03-31 18:38:02 +00001119
1120TemplateName
1121Sema::InstantiateTemplateName(TemplateName Name, SourceLocation Loc,
Douglas Gregorf9e7d3d2009-05-11 23:53:27 +00001122 const TemplateArgumentList &TemplateArgs) {
Douglas Gregor15a92852009-03-31 18:38:02 +00001123 if (TemplateTemplateParmDecl *TTP
1124 = dyn_cast_or_null<TemplateTemplateParmDecl>(
1125 Name.getAsTemplateDecl())) {
1126 assert(TTP->getDepth() == 0 &&
1127 "Cannot reduce depth of a template template parameter");
Douglas Gregoraed98042009-03-31 20:22:05 +00001128 assert(TemplateArgs[TTP->getPosition()].getAsDecl() &&
Douglas Gregor15a92852009-03-31 18:38:02 +00001129 "Wrong kind of template template argument");
1130 ClassTemplateDecl *ClassTemplate
1131 = dyn_cast<ClassTemplateDecl>(
1132 TemplateArgs[TTP->getPosition()].getAsDecl());
Douglas Gregoraed98042009-03-31 20:22:05 +00001133 assert(ClassTemplate && "Expected a class template");
Douglas Gregor15a92852009-03-31 18:38:02 +00001134 if (QualifiedTemplateName *QTN = Name.getAsQualifiedTemplateName()) {
1135 NestedNameSpecifier *NNS
1136 = InstantiateNestedNameSpecifier(QTN->getQualifier(),
1137 /*FIXME=*/SourceRange(Loc),
Douglas Gregorf9e7d3d2009-05-11 23:53:27 +00001138 TemplateArgs);
Douglas Gregor15a92852009-03-31 18:38:02 +00001139 if (NNS)
1140 return Context.getQualifiedTemplateName(NNS,
1141 QTN->hasTemplateKeyword(),
1142 ClassTemplate);
1143 }
1144
1145 return TemplateName(ClassTemplate);
1146 } else if (DependentTemplateName *DTN = Name.getAsDependentTemplateName()) {
1147 NestedNameSpecifier *NNS
1148 = InstantiateNestedNameSpecifier(DTN->getQualifier(),
1149 /*FIXME=*/SourceRange(Loc),
Douglas Gregorf9e7d3d2009-05-11 23:53:27 +00001150 TemplateArgs);
Douglas Gregor15a92852009-03-31 18:38:02 +00001151
1152 if (!NNS) // FIXME: Not the best recovery strategy.
1153 return Name;
1154
1155 if (NNS->isDependent())
1156 return Context.getDependentTemplateName(NNS, DTN->getName());
1157
1158 // Somewhat redundant with ActOnDependentTemplateName.
1159 CXXScopeSpec SS;
1160 SS.setRange(SourceRange(Loc));
1161 SS.setScopeRep(NNS);
1162 TemplateTy Template;
1163 TemplateNameKind TNK = isTemplateName(*DTN->getName(), 0, Template, &SS);
1164 if (TNK == TNK_Non_template) {
1165 Diag(Loc, diag::err_template_kw_refers_to_non_template)
1166 << DTN->getName();
1167 return Name;
1168 } else if (TNK == TNK_Function_template) {
1169 Diag(Loc, diag::err_template_kw_refers_to_non_template)
1170 << DTN->getName();
1171 return Name;
1172 }
1173
1174 return Template.getAsVal<TemplateName>();
1175 }
1176
1177
1178
Mike Stumpe127ae32009-05-16 07:39:55 +00001179 // FIXME: Even if we're referring to a Decl that isn't a template template
1180 // parameter, we may need to instantiate the outer contexts of that
1181 // Decl. However, this won't be needed until we implement member templates.
Douglas Gregor15a92852009-03-31 18:38:02 +00001182 return Name;
1183}
Douglas Gregorb320b9e2009-06-11 00:06:24 +00001184
1185TemplateArgument Sema::Instantiate(TemplateArgument Arg,
1186 const TemplateArgumentList &TemplateArgs) {
1187 switch (Arg.getKind()) {
1188 case TemplateArgument::Null:
1189 assert(false && "Should never have a NULL template argument");
1190 break;
1191
1192 case TemplateArgument::Type: {
1193 QualType T = InstantiateType(Arg.getAsType(), TemplateArgs,
1194 Arg.getLocation(), DeclarationName());
1195 if (T.isNull())
1196 return TemplateArgument();
1197
1198 return TemplateArgument(Arg.getLocation(), T);
1199 }
1200
1201 case TemplateArgument::Declaration:
1202 // FIXME: Template instantiation for template template parameters.
1203 return Arg;
1204
1205 case TemplateArgument::Integral:
1206 return Arg;
1207
1208 case TemplateArgument::Expression: {
Douglas Gregora8b2fbf2009-06-22 20:57:11 +00001209 // Template argument expressions are not potentially evaluated.
1210 EnterExpressionEvaluationContext Unevaluated(*this, Action::Unevaluated);
1211
Douglas Gregorb320b9e2009-06-11 00:06:24 +00001212 Sema::OwningExprResult E = InstantiateExpr(Arg.getAsExpr(), TemplateArgs);
1213 if (E.isInvalid())
1214 return TemplateArgument();
1215 return TemplateArgument(E.takeAs<Expr>());
1216 }
Anders Carlsson584b5062009-06-15 17:04:53 +00001217
1218 case TemplateArgument::Pack:
1219 assert(0 && "FIXME: Implement!");
1220 break;
Douglas Gregorb320b9e2009-06-11 00:06:24 +00001221 }
1222
1223 assert(false && "Unhandled template argument kind");
1224 return TemplateArgument();
1225}