blob: 0c78c71206af6286d47be5fb3bebd814c83fbb7b [file] [log] [blame]
Douglas Gregor99ebf652009-02-27 19:31:52 +00001//===------- SemaTemplateInstantiate.cpp - C++ Template Instantiation ------===/
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//===----------------------------------------------------------------------===/
8//
9// This file implements C++ template instantiation.
10//
11//===----------------------------------------------------------------------===/
12
13#include "Sema.h"
Douglas Gregoraba43bb2009-05-26 20:50:29 +000014#include "clang/AST/ASTConsumer.h"
Douglas Gregor99ebf652009-02-27 19:31:52 +000015#include "clang/AST/ASTContext.h"
16#include "clang/AST/Expr.h"
Douglas Gregor99ebf652009-02-27 19:31:52 +000017#include "clang/AST/DeclTemplate.h"
18#include "clang/Parse/DeclSpec.h"
19#include "clang/Basic/LangOptions.h"
Douglas Gregorcd281c32009-02-28 00:25:32 +000020#include "llvm/Support/Compiler.h"
Douglas Gregor99ebf652009-02-27 19:31:52 +000021
22using namespace clang;
23
Douglas Gregoree1828a2009-03-10 18:03:33 +000024//===----------------------------------------------------------------------===/
25// Template Instantiation Support
26//===----------------------------------------------------------------------===/
27
Douglas Gregor54dabfc2009-05-14 23:26:13 +000028/// \brief Retrieve the template argument list that should be used to
29/// instantiate the given declaration.
30const TemplateArgumentList &
31Sema::getTemplateInstantiationArgs(NamedDecl *D) {
Douglas Gregor1637be72009-06-26 00:10:03 +000032 // Template arguments for a class template specialization.
Douglas Gregor54dabfc2009-05-14 23:26:13 +000033 if (ClassTemplateSpecializationDecl *Spec
34 = dyn_cast<ClassTemplateSpecializationDecl>(D))
35 return Spec->getTemplateArgs();
36
Douglas Gregor1637be72009-06-26 00:10:03 +000037 // Template arguments for a function template specialization.
38 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D))
39 if (const TemplateArgumentList *TemplateArgs
40 = Function->getTemplateSpecializationArgs())
41 return *TemplateArgs;
42
43 // Template arguments for a member of a class template specialization.
Douglas Gregor54dabfc2009-05-14 23:26:13 +000044 DeclContext *EnclosingTemplateCtx = D->getDeclContext();
45 while (!isa<ClassTemplateSpecializationDecl>(EnclosingTemplateCtx)) {
46 assert(!EnclosingTemplateCtx->isFileContext() &&
47 "Tried to get the instantiation arguments of a non-template");
48 EnclosingTemplateCtx = EnclosingTemplateCtx->getParent();
49 }
50
51 ClassTemplateSpecializationDecl *EnclosingTemplate
52 = cast<ClassTemplateSpecializationDecl>(EnclosingTemplateCtx);
53 return EnclosingTemplate->getTemplateArgs();
54}
55
Douglas Gregor26dce442009-03-10 00:06:19 +000056Sema::InstantiatingTemplate::
57InstantiatingTemplate(Sema &SemaRef, SourceLocation PointOfInstantiation,
Douglas Gregorf3e7ce42009-05-18 17:01:57 +000058 Decl *Entity,
Douglas Gregor26dce442009-03-10 00:06:19 +000059 SourceRange InstantiationRange)
60 : SemaRef(SemaRef) {
Douglas Gregordf667e72009-03-10 20:44:00 +000061
62 Invalid = CheckInstantiationDepth(PointOfInstantiation,
63 InstantiationRange);
64 if (!Invalid) {
Douglas Gregor26dce442009-03-10 00:06:19 +000065 ActiveTemplateInstantiation Inst;
Douglas Gregordf667e72009-03-10 20:44:00 +000066 Inst.Kind = ActiveTemplateInstantiation::TemplateInstantiation;
Douglas Gregor26dce442009-03-10 00:06:19 +000067 Inst.PointOfInstantiation = PointOfInstantiation;
Douglas Gregordf667e72009-03-10 20:44:00 +000068 Inst.Entity = reinterpret_cast<uintptr_t>(Entity);
Douglas Gregor313a81d2009-03-12 18:36:18 +000069 Inst.TemplateArgs = 0;
70 Inst.NumTemplateArgs = 0;
Douglas Gregordf667e72009-03-10 20:44:00 +000071 Inst.InstantiationRange = InstantiationRange;
72 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
73 Invalid = false;
74 }
75}
76
77Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef,
78 SourceLocation PointOfInstantiation,
79 TemplateDecl *Template,
80 const TemplateArgument *TemplateArgs,
81 unsigned NumTemplateArgs,
82 SourceRange InstantiationRange)
83 : SemaRef(SemaRef) {
84
85 Invalid = CheckInstantiationDepth(PointOfInstantiation,
86 InstantiationRange);
87 if (!Invalid) {
88 ActiveTemplateInstantiation Inst;
89 Inst.Kind
90 = ActiveTemplateInstantiation::DefaultTemplateArgumentInstantiation;
91 Inst.PointOfInstantiation = PointOfInstantiation;
92 Inst.Entity = reinterpret_cast<uintptr_t>(Template);
93 Inst.TemplateArgs = TemplateArgs;
94 Inst.NumTemplateArgs = NumTemplateArgs;
Douglas Gregor26dce442009-03-10 00:06:19 +000095 Inst.InstantiationRange = InstantiationRange;
96 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
97 Invalid = false;
98 }
99}
100
Douglas Gregor637a4092009-06-10 23:47:09 +0000101Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef,
102 SourceLocation PointOfInstantiation,
103 ClassTemplatePartialSpecializationDecl *PartialSpec,
104 const TemplateArgument *TemplateArgs,
105 unsigned NumTemplateArgs,
106 SourceRange InstantiationRange)
107 : SemaRef(SemaRef) {
108
109 Invalid = CheckInstantiationDepth(PointOfInstantiation,
110 InstantiationRange);
111 if (!Invalid) {
112 ActiveTemplateInstantiation Inst;
113 Inst.Kind
114 = ActiveTemplateInstantiation::PartialSpecDeductionInstantiation;
115 Inst.PointOfInstantiation = PointOfInstantiation;
116 Inst.Entity = reinterpret_cast<uintptr_t>(PartialSpec);
117 Inst.TemplateArgs = TemplateArgs;
118 Inst.NumTemplateArgs = NumTemplateArgs;
119 Inst.InstantiationRange = InstantiationRange;
120 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
121 Invalid = false;
122 }
123}
124
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000125void Sema::InstantiatingTemplate::Clear() {
126 if (!Invalid) {
Douglas Gregor26dce442009-03-10 00:06:19 +0000127 SemaRef.ActiveTemplateInstantiations.pop_back();
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000128 Invalid = true;
129 }
Douglas Gregor26dce442009-03-10 00:06:19 +0000130}
131
Douglas Gregordf667e72009-03-10 20:44:00 +0000132bool Sema::InstantiatingTemplate::CheckInstantiationDepth(
133 SourceLocation PointOfInstantiation,
134 SourceRange InstantiationRange) {
135 if (SemaRef.ActiveTemplateInstantiations.size()
136 <= SemaRef.getLangOptions().InstantiationDepth)
137 return false;
138
139 SemaRef.Diag(PointOfInstantiation,
140 diag::err_template_recursion_depth_exceeded)
141 << SemaRef.getLangOptions().InstantiationDepth
142 << InstantiationRange;
143 SemaRef.Diag(PointOfInstantiation, diag::note_template_recursion_depth)
144 << SemaRef.getLangOptions().InstantiationDepth;
145 return true;
146}
147
Douglas Gregoree1828a2009-03-10 18:03:33 +0000148/// \brief Prints the current instantiation stack through a series of
149/// notes.
150void Sema::PrintInstantiationStack() {
151 for (llvm::SmallVector<ActiveTemplateInstantiation, 16>::reverse_iterator
152 Active = ActiveTemplateInstantiations.rbegin(),
153 ActiveEnd = ActiveTemplateInstantiations.rend();
154 Active != ActiveEnd;
155 ++Active) {
Douglas Gregordf667e72009-03-10 20:44:00 +0000156 switch (Active->Kind) {
157 case ActiveTemplateInstantiation::TemplateInstantiation: {
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000158 Decl *D = reinterpret_cast<Decl *>(Active->Entity);
159 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) {
160 unsigned DiagID = diag::note_template_member_class_here;
161 if (isa<ClassTemplateSpecializationDecl>(Record))
162 DiagID = diag::note_template_class_instantiation_here;
163 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
164 DiagID)
165 << Context.getTypeDeclType(Record)
166 << Active->InstantiationRange;
167 } else {
168 FunctionDecl *Function = cast<FunctionDecl>(D);
Douglas Gregor1637be72009-06-26 00:10:03 +0000169 unsigned DiagID;
170 if (Function->getPrimaryTemplate())
171 DiagID = diag::note_function_template_spec_here;
172 else
173 DiagID = diag::note_template_member_function_here;
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000174 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
175 DiagID)
176 << Function
177 << Active->InstantiationRange;
178 }
Douglas Gregordf667e72009-03-10 20:44:00 +0000179 break;
180 }
181
182 case ActiveTemplateInstantiation::DefaultTemplateArgumentInstantiation: {
183 TemplateDecl *Template = cast<TemplateDecl>((Decl *)Active->Entity);
184 std::string TemplateArgsStr
Douglas Gregor7532dc62009-03-30 22:58:21 +0000185 = TemplateSpecializationType::PrintTemplateArgumentList(
Douglas Gregordf667e72009-03-10 20:44:00 +0000186 Active->TemplateArgs,
Douglas Gregord249e1d1f2009-05-29 20:38:28 +0000187 Active->NumTemplateArgs,
188 Context.PrintingPolicy);
Douglas Gregordf667e72009-03-10 20:44:00 +0000189 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
190 diag::note_default_arg_instantiation_here)
191 << (Template->getNameAsString() + TemplateArgsStr)
192 << Active->InstantiationRange;
193 break;
194 }
Douglas Gregor637a4092009-06-10 23:47:09 +0000195
196 case ActiveTemplateInstantiation::PartialSpecDeductionInstantiation: {
197 ClassTemplatePartialSpecializationDecl *PartialSpec
198 = cast<ClassTemplatePartialSpecializationDecl>((Decl *)Active->Entity);
Douglas Gregor637a4092009-06-10 23:47:09 +0000199 // FIXME: The active template instantiation's template arguments
200 // are interesting, too. We should add something like [with T =
201 // foo, U = bar, etc.] to the string.
202 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
203 diag::note_partial_spec_deduct_instantiation_here)
Douglas Gregor02cbbd22009-06-11 18:10:32 +0000204 << Context.getTypeDeclType(PartialSpec)
Douglas Gregor637a4092009-06-10 23:47:09 +0000205 << Active->InstantiationRange;
206 break;
207 }
208
Douglas Gregordf667e72009-03-10 20:44:00 +0000209 }
Douglas Gregoree1828a2009-03-10 18:03:33 +0000210 }
211}
212
Douglas Gregor5e9f35c2009-06-14 07:33:30 +0000213bool Sema::isSFINAEContext() const {
214 using llvm::SmallVector;
215 for (SmallVector<ActiveTemplateInstantiation, 16>::const_reverse_iterator
216 Active = ActiveTemplateInstantiations.rbegin(),
217 ActiveEnd = ActiveTemplateInstantiations.rend();
218 Active != ActiveEnd;
219 ++Active) {
220
221 switch(Active->Kind) {
222 case ActiveTemplateInstantiation::PartialSpecDeductionInstantiation:
223 // We're in a template argument deduction context, so SFINAE
224 // applies.
225 return true;
226
227 case ActiveTemplateInstantiation::DefaultTemplateArgumentInstantiation:
228 // A default template argument instantiation may or may not be a
229 // SFINAE context; look further up the stack.
230 break;
231
232 case ActiveTemplateInstantiation::TemplateInstantiation:
233 // This is a template instantiation, so there is no SFINAE.
234 return false;
235 }
236 }
237
238 return false;
239}
240
Douglas Gregor99ebf652009-02-27 19:31:52 +0000241//===----------------------------------------------------------------------===/
242// Template Instantiation for Types
243//===----------------------------------------------------------------------===/
Douglas Gregorcd281c32009-02-28 00:25:32 +0000244namespace {
245 class VISIBILITY_HIDDEN TemplateTypeInstantiator {
246 Sema &SemaRef;
Douglas Gregor7e063902009-05-11 23:53:27 +0000247 const TemplateArgumentList &TemplateArgs;
Douglas Gregorcd281c32009-02-28 00:25:32 +0000248 SourceLocation Loc;
249 DeclarationName Entity;
Douglas Gregor99ebf652009-02-27 19:31:52 +0000250
Douglas Gregorcd281c32009-02-28 00:25:32 +0000251 public:
252 TemplateTypeInstantiator(Sema &SemaRef,
Douglas Gregor7e063902009-05-11 23:53:27 +0000253 const TemplateArgumentList &TemplateArgs,
Douglas Gregorcd281c32009-02-28 00:25:32 +0000254 SourceLocation Loc,
255 DeclarationName Entity)
256 : SemaRef(SemaRef), TemplateArgs(TemplateArgs),
Douglas Gregor7e063902009-05-11 23:53:27 +0000257 Loc(Loc), Entity(Entity) { }
Douglas Gregorcd281c32009-02-28 00:25:32 +0000258
259 QualType operator()(QualType T) const { return Instantiate(T); }
260
261 QualType Instantiate(QualType T) const;
262
263 // Declare instantiate functions for each type.
264#define TYPE(Class, Base) \
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 Gregor99ebf652009-02-27 19:31:52 +0000275 // FIXME: Implement this
276 assert(false && "Cannot instantiate ExtQualType yet");
277 return QualType();
278}
279
Douglas Gregorcd281c32009-02-28 00:25:32 +0000280QualType
281TemplateTypeInstantiator::InstantiateBuiltinType(const BuiltinType *T,
282 unsigned Quals) const {
Douglas Gregor724651c2009-02-28 01:04:19 +0000283 assert(false && "Builtin types are not dependent and cannot be instantiated");
Douglas Gregorcd281c32009-02-28 00:25:32 +0000284 return QualType(T, Quals);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000285}
286
Douglas Gregorcd281c32009-02-28 00:25:32 +0000287QualType
288TemplateTypeInstantiator::
289InstantiateFixedWidthIntType(const FixedWidthIntType *T, unsigned Quals) const {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000290 // FIXME: Implement this
291 assert(false && "Cannot instantiate FixedWidthIntType yet");
292 return QualType();
293}
294
Douglas Gregorcd281c32009-02-28 00:25:32 +0000295QualType
296TemplateTypeInstantiator::InstantiateComplexType(const ComplexType *T,
297 unsigned Quals) const {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000298 // FIXME: Implement this
299 assert(false && "Cannot instantiate ComplexType yet");
300 return QualType();
301}
302
Douglas Gregorcd281c32009-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 Gregor99ebf652009-02-27 19:31:52 +0000311}
312
Douglas Gregorcd281c32009-02-28 00:25:32 +0000313QualType
314TemplateTypeInstantiator::InstantiateBlockPointerType(const BlockPointerType *T,
315 unsigned Quals) const {
Anders Carlsson859ba502009-06-12 16:23:10 +0000316 QualType PointeeType = Instantiate(T->getPointeeType());
317 if (PointeeType.isNull())
318 return QualType();
319
Anders Carlsson9a917e42009-06-12 22:56:54 +0000320 return SemaRef.BuildBlockPointerType(PointeeType, Quals, Loc, Entity);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000321}
322
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000323QualType
324TemplateTypeInstantiator::InstantiateLValueReferenceType(
325 const LValueReferenceType *T, unsigned Quals) const {
Douglas Gregorcd281c32009-02-28 00:25:32 +0000326 QualType ReferentType = Instantiate(T->getPointeeType());
327 if (ReferentType.isNull())
328 return QualType();
329
Sebastian Redl7c80bd62009-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 Gregor99ebf652009-02-27 19:31:52 +0000341}
342
Douglas Gregorcd281c32009-02-28 00:25:32 +0000343QualType
344TemplateTypeInstantiator::
345InstantiateMemberPointerType(const MemberPointerType *T,
346 unsigned Quals) const {
Douglas Gregor949bf692009-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 Gregor99ebf652009-02-27 19:31:52 +0000357}
358
Douglas Gregorcd281c32009-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 Gregorff668032009-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 Gregorcd281c32009-02-28 00:25:32 +0000390 return SemaRef.BuildArrayType(ElementType, T->getSizeModifier(),
391 &ArraySize, T->getIndexTypeQualifier(),
392 Loc, Entity);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000393}
394
Douglas Gregorcd281c32009-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 Gregor99ebf652009-02-27 19:31:52 +0000406}
407
Douglas Gregorcd281c32009-02-28 00:25:32 +0000408QualType
409TemplateTypeInstantiator::
410InstantiateVariableArrayType(const VariableArrayType *T,
411 unsigned Quals) const {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000412 // FIXME: Implement this
413 assert(false && "Cannot instantiate VariableArrayType yet");
414 return QualType();
415}
416
Douglas Gregorcd281c32009-02-28 00:25:32 +0000417QualType
418TemplateTypeInstantiator::
419InstantiateDependentSizedArrayType(const DependentSizedArrayType *T,
420 unsigned Quals) const {
Anders Carlsson76b1c842009-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 Gregorac7610d2009-06-22 20:57:11 +0000434 EnterExpressionEvaluationContext Unevaluated(SemaRef, Action::Unevaluated);
Anders Carlsson76b1c842009-03-15 20:12:13 +0000435 Sema::OwningExprResult InstantiatedArraySize =
Douglas Gregor7e063902009-05-11 23:53:27 +0000436 SemaRef.InstantiateExpr(ArraySize, TemplateArgs);
Anders Carlsson76b1c842009-03-15 20:12:13 +0000437 if (InstantiatedArraySize.isInvalid())
438 return QualType();
439
440 return SemaRef.BuildArrayType(ElementType, T->getSizeModifier(),
Anders Carlssone9146f22009-05-01 19:49:17 +0000441 InstantiatedArraySize.takeAs<Expr>(),
Anders Carlsson76b1c842009-03-15 20:12:13 +0000442 T->getIndexTypeQualifier(), Loc, Entity);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000443}
444
Douglas Gregorcd281c32009-02-28 00:25:32 +0000445QualType
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000446TemplateTypeInstantiator::
447InstantiateDependentSizedExtVectorType(const DependentSizedExtVectorType *T,
448 unsigned Quals) const {
449
Douglas Gregorf6ddb732009-06-18 18:45:36 +0000450 // Instantiate the element type if needed.
Douglas Gregor9cdda0c2009-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 Gregorac7610d2009-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 Gregorf6ddb732009-06-18 18:45:36 +0000462 // Instantiate the size expression.
463 const Expr *SizeExpr = T->getSizeExpr();
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000464 Sema::OwningExprResult InstantiatedArraySize =
Douglas Gregorf6ddb732009-06-18 18:45:36 +0000465 SemaRef.InstantiateExpr(const_cast<Expr *>(SizeExpr), TemplateArgs);
Douglas Gregor9cdda0c2009-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 Gregorcd281c32009-02-28 00:25:32 +0000476TemplateTypeInstantiator::InstantiateVectorType(const VectorType *T,
477 unsigned Quals) const {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000478 // FIXME: Implement this
479 assert(false && "Cannot instantiate VectorType yet");
480 return QualType();
481}
482
Douglas Gregorcd281c32009-02-28 00:25:32 +0000483QualType
484TemplateTypeInstantiator::InstantiateExtVectorType(const ExtVectorType *T,
485 unsigned Quals) const {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000486 // FIXME: Implement this
487 assert(false && "Cannot instantiate ExtVectorType yet");
488 return QualType();
489}
490
Douglas Gregorcd281c32009-02-28 00:25:32 +0000491QualType
492TemplateTypeInstantiator::
493InstantiateFunctionProtoType(const FunctionProtoType *T,
494 unsigned Quals) const {
Douglas Gregor724651c2009-02-28 01:04:19 +0000495 QualType ResultType = Instantiate(T->getResultType());
496 if (ResultType.isNull())
497 return ResultType;
498
Douglas Gregor0ca20ac2009-05-29 18:27:38 +0000499 llvm::SmallVector<QualType, 4> ParamTypes;
Douglas Gregor724651c2009-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 Gregor02cbbd22009-06-11 18:10:32 +0000510 return SemaRef.BuildFunctionType(ResultType, ParamTypes.data(),
Douglas Gregor724651c2009-02-28 01:04:19 +0000511 ParamTypes.size(),
512 T->isVariadic(), T->getTypeQuals(),
513 Loc, Entity);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000514}
515
Douglas Gregorcd281c32009-02-28 00:25:32 +0000516QualType
517TemplateTypeInstantiator::
518InstantiateFunctionNoProtoType(const FunctionNoProtoType *T,
519 unsigned Quals) const {
Douglas Gregor724651c2009-02-28 01:04:19 +0000520 assert(false && "Functions without prototypes cannot be dependent.");
Douglas Gregor99ebf652009-02-27 19:31:52 +0000521 return QualType();
522}
523
Douglas Gregorcd281c32009-02-28 00:25:32 +0000524QualType
525TemplateTypeInstantiator::InstantiateTypedefType(const TypedefType *T,
526 unsigned Quals) const {
Douglas Gregor815215d2009-05-27 05:35:12 +0000527 TypedefDecl *Typedef
Douglas Gregored961e72009-05-27 17:54:46 +0000528 = cast_or_null<TypedefDecl>(
529 SemaRef.InstantiateCurrentDeclRef(T->getDecl()));
Douglas Gregor815215d2009-05-27 05:35:12 +0000530 if (!Typedef)
531 return QualType();
532
533 return SemaRef.Context.getTypeDeclType(Typedef);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000534}
535
Douglas Gregorcd281c32009-02-28 00:25:32 +0000536QualType
537TemplateTypeInstantiator::InstantiateTypeOfExprType(const TypeOfExprType *T,
538 unsigned Quals) const {
Douglas Gregorac7610d2009-06-22 20:57:11 +0000539 // The expression in a typeof is not potentially evaluated.
540 EnterExpressionEvaluationContext Unevaluated(SemaRef, Action::Unevaluated);
541
Douglas Gregor5f8bd592009-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 Gregor99ebf652009-02-27 19:31:52 +0000548}
549
Douglas Gregorcd281c32009-02-28 00:25:32 +0000550QualType
551TemplateTypeInstantiator::InstantiateTypeOfType(const TypeOfType *T,
552 unsigned Quals) const {
Douglas Gregor5f8bd592009-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 Gregor99ebf652009-02-27 19:31:52 +0000558}
559
Anders Carlsson395b4752009-06-24 19:06:50 +0000560QualType
561TemplateTypeInstantiator::InstantiateDecltypeType(const DecltypeType *T,
562 unsigned Quals) const {
Anders Carlsson87471f52009-06-26 03:02:18 +0000563 // C++0x [dcl.type.simple]p4:
564 // The operand of the decltype specifier is an unevaluated operand.
565 EnterExpressionEvaluationContext Unevaluated(SemaRef,
566 Action::Unevaluated);
567
Anders Carlsson395b4752009-06-24 19:06:50 +0000568 Sema::OwningExprResult E
569 = SemaRef.InstantiateExpr(T->getUnderlyingExpr(), TemplateArgs);
570
571 if (E.isInvalid())
572 return QualType();
573
574 return SemaRef.Context.getDecltypeType(E.takeAs<Expr>());
575}
576
Douglas Gregorcd281c32009-02-28 00:25:32 +0000577QualType
578TemplateTypeInstantiator::InstantiateRecordType(const RecordType *T,
579 unsigned Quals) const {
Douglas Gregor815215d2009-05-27 05:35:12 +0000580 RecordDecl *Record
Douglas Gregored961e72009-05-27 17:54:46 +0000581 = cast_or_null<RecordDecl>(SemaRef.InstantiateCurrentDeclRef(T->getDecl()));
Douglas Gregor815215d2009-05-27 05:35:12 +0000582 if (!Record)
583 return QualType();
584
585 return SemaRef.Context.getTypeDeclType(Record);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000586}
587
Douglas Gregorcd281c32009-02-28 00:25:32 +0000588QualType
Douglas Gregorcd281c32009-02-28 00:25:32 +0000589TemplateTypeInstantiator::InstantiateEnumType(const EnumType *T,
590 unsigned Quals) const {
Douglas Gregor815215d2009-05-27 05:35:12 +0000591 EnumDecl *Enum
Douglas Gregored961e72009-05-27 17:54:46 +0000592 = cast_or_null<EnumDecl>(SemaRef.InstantiateCurrentDeclRef(T->getDecl()));
Douglas Gregor815215d2009-05-27 05:35:12 +0000593 if (!Enum)
594 return QualType();
595
596 return SemaRef.Context.getTypeDeclType(Enum);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000597}
598
Douglas Gregorcd281c32009-02-28 00:25:32 +0000599QualType
600TemplateTypeInstantiator::
601InstantiateTemplateTypeParmType(const TemplateTypeParmType *T,
602 unsigned Quals) const {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000603 if (T->getDepth() == 0) {
604 // Replace the template type parameter with its corresponding
605 // template argument.
Douglas Gregor99ebf652009-02-27 19:31:52 +0000606 assert(TemplateArgs[T->getIndex()].getKind() == TemplateArgument::Type &&
607 "Template argument kind mismatch");
608 QualType Result = TemplateArgs[T->getIndex()].getAsType();
Douglas Gregorcd281c32009-02-28 00:25:32 +0000609 if (Result.isNull() || !Quals)
Douglas Gregor99ebf652009-02-27 19:31:52 +0000610 return Result;
611
612 // C++ [dcl.ref]p1:
613 // [...] Cv-qualified references are ill-formed except when
614 // the cv-qualifiers are introduced through the use of a
615 // typedef (7.1.3) or of a template type argument (14.3), in
616 // which case the cv-qualifiers are ignored.
Douglas Gregorcd281c32009-02-28 00:25:32 +0000617 if (Quals && Result->isReferenceType())
618 Quals = 0;
Douglas Gregor99ebf652009-02-27 19:31:52 +0000619
Douglas Gregorcd281c32009-02-28 00:25:32 +0000620 return QualType(Result.getTypePtr(), Quals | Result.getCVRQualifiers());
Douglas Gregor99ebf652009-02-27 19:31:52 +0000621 }
622
623 // The template type parameter comes from an inner template (e.g.,
624 // the template parameter list of a member template inside the
625 // template we are instantiating). Create a new template type
626 // parameter with the template "level" reduced by one.
627 return SemaRef.Context.getTemplateTypeParmType(T->getDepth() - 1,
628 T->getIndex(),
Anders Carlsson76e4ce42009-06-16 00:30:48 +0000629 T->isParameterPack(),
Douglas Gregorcd281c32009-02-28 00:25:32 +0000630 T->getName())
631 .getQualifiedType(Quals);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000632}
633
Douglas Gregorcd281c32009-02-28 00:25:32 +0000634QualType
635TemplateTypeInstantiator::
Douglas Gregor7532dc62009-03-30 22:58:21 +0000636InstantiateTemplateSpecializationType(
637 const TemplateSpecializationType *T,
Douglas Gregorcd281c32009-02-28 00:25:32 +0000638 unsigned Quals) const {
Douglas Gregor0ca20ac2009-05-29 18:27:38 +0000639 llvm::SmallVector<TemplateArgument, 4> InstantiatedTemplateArgs;
Douglas Gregor40808ce2009-03-09 23:48:35 +0000640 InstantiatedTemplateArgs.reserve(T->getNumArgs());
Douglas Gregor7532dc62009-03-30 22:58:21 +0000641 for (TemplateSpecializationType::iterator Arg = T->begin(), ArgEnd = T->end();
Douglas Gregor40808ce2009-03-09 23:48:35 +0000642 Arg != ArgEnd; ++Arg) {
Douglas Gregor91333002009-06-11 00:06:24 +0000643 TemplateArgument InstArg = SemaRef.Instantiate(*Arg, TemplateArgs);
644 if (InstArg.isNull())
645 return QualType();
Douglas Gregor40808ce2009-03-09 23:48:35 +0000646
Douglas Gregor91333002009-06-11 00:06:24 +0000647 InstantiatedTemplateArgs.push_back(InstArg);
Douglas Gregor40808ce2009-03-09 23:48:35 +0000648 }
649
Mike Stump390b4cc2009-05-16 07:39:55 +0000650 // FIXME: We're missing the locations of the template name, '<', and '>'.
Douglas Gregorde650ae2009-03-31 18:38:02 +0000651
652 TemplateName Name = SemaRef.InstantiateTemplateName(T->getTemplateName(),
653 Loc,
Douglas Gregor7e063902009-05-11 23:53:27 +0000654 TemplateArgs);
Douglas Gregorde650ae2009-03-31 18:38:02 +0000655
656 return SemaRef.CheckTemplateIdType(Name, Loc, SourceLocation(),
Douglas Gregor02cbbd22009-06-11 18:10:32 +0000657 InstantiatedTemplateArgs.data(),
Douglas Gregor7532dc62009-03-30 22:58:21 +0000658 InstantiatedTemplateArgs.size(),
659 SourceLocation());
Douglas Gregor99ebf652009-02-27 19:31:52 +0000660}
661
Douglas Gregorcd281c32009-02-28 00:25:32 +0000662QualType
663TemplateTypeInstantiator::
Douglas Gregore4e5b052009-03-19 00:18:19 +0000664InstantiateQualifiedNameType(const QualifiedNameType *T,
665 unsigned Quals) const {
Douglas Gregord57959a2009-03-27 23:10:48 +0000666 // When we instantiated a qualified name type, there's no point in
667 // keeping the qualification around in the instantiated result. So,
668 // just instantiate the named type.
669 return (*this)(T->getNamedType());
670}
671
672QualType
673TemplateTypeInstantiator::
674InstantiateTypenameType(const TypenameType *T, unsigned Quals) const {
Douglas Gregor17343172009-04-01 00:28:59 +0000675 if (const TemplateSpecializationType *TemplateId = T->getTemplateId()) {
676 // When the typename type refers to a template-id, the template-id
677 // is dependent and has enough information to instantiate the
678 // result of the typename type. Since we don't care about keeping
679 // the spelling of the typename type in template instantiations,
680 // we just instantiate the template-id.
681 return InstantiateTemplateSpecializationType(TemplateId, Quals);
682 }
683
Douglas Gregord57959a2009-03-27 23:10:48 +0000684 NestedNameSpecifier *NNS
685 = SemaRef.InstantiateNestedNameSpecifier(T->getQualifier(),
686 SourceRange(Loc),
Douglas Gregor7e063902009-05-11 23:53:27 +0000687 TemplateArgs);
Douglas Gregord57959a2009-03-27 23:10:48 +0000688 if (!NNS)
689 return QualType();
690
Douglas Gregor17343172009-04-01 00:28:59 +0000691 return SemaRef.CheckTypenameType(NNS, *T->getIdentifier(), SourceRange(Loc));
Douglas Gregore4e5b052009-03-19 00:18:19 +0000692}
693
694QualType
695TemplateTypeInstantiator::
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000696InstantiateObjCObjectPointerType(const ObjCObjectPointerType *T,
697 unsigned Quals) const {
698 assert(false && "Objective-C types cannot be dependent");
699 return QualType();
700}
701
702QualType
703TemplateTypeInstantiator::
Douglas Gregorcd281c32009-02-28 00:25:32 +0000704InstantiateObjCInterfaceType(const ObjCInterfaceType *T,
705 unsigned Quals) const {
706 assert(false && "Objective-C types cannot be dependent");
Douglas Gregor99ebf652009-02-27 19:31:52 +0000707 return QualType();
708}
709
Douglas Gregorcd281c32009-02-28 00:25:32 +0000710QualType
711TemplateTypeInstantiator::
712InstantiateObjCQualifiedInterfaceType(const ObjCQualifiedInterfaceType *T,
713 unsigned Quals) const {
714 assert(false && "Objective-C types cannot be dependent");
Douglas Gregor99ebf652009-02-27 19:31:52 +0000715 return QualType();
716}
717
Douglas Gregorcd281c32009-02-28 00:25:32 +0000718/// \brief The actual implementation of Sema::InstantiateType().
719QualType TemplateTypeInstantiator::Instantiate(QualType T) const {
720 // If T is not a dependent type, there is nothing to do.
721 if (!T->isDependentType())
722 return T;
723
724 switch (T->getTypeClass()) {
725#define TYPE(Class, Base) \
726 case Type::Class: \
727 return Instantiate##Class##Type(cast<Class##Type>(T.getTypePtr()), \
728 T.getCVRQualifiers());
729#define ABSTRACT_TYPE(Class, Base)
730#include "clang/AST/TypeNodes.def"
731 }
732
733 assert(false && "Not all types have been decoded for instantiation");
734 return QualType();
735}
Douglas Gregor99ebf652009-02-27 19:31:52 +0000736
737/// \brief Instantiate the type T with a given set of template arguments.
738///
739/// This routine substitutes the given template arguments into the
740/// type T and produces the instantiated type.
741///
742/// \param T the type into which the template arguments will be
743/// substituted. If this type is not dependent, it will be returned
744/// immediately.
745///
746/// \param TemplateArgs the template arguments that will be
747/// substituted for the top-level template parameters within T.
748///
Douglas Gregor99ebf652009-02-27 19:31:52 +0000749/// \param Loc the location in the source code where this substitution
750/// is being performed. It will typically be the location of the
751/// declarator (if we're instantiating the type of some declaration)
752/// or the location of the type in the source code (if, e.g., we're
753/// instantiating the type of a cast expression).
754///
755/// \param Entity the name of the entity associated with a declaration
756/// being instantiated (if any). May be empty to indicate that there
757/// is no such entity (if, e.g., this is a type that occurs as part of
758/// a cast expression) or that the entity has no name (e.g., an
759/// unnamed function parameter).
760///
761/// \returns If the instantiation succeeds, the instantiated
762/// type. Otherwise, produces diagnostics and returns a NULL type.
763QualType Sema::InstantiateType(QualType T,
Douglas Gregor7e063902009-05-11 23:53:27 +0000764 const TemplateArgumentList &TemplateArgs,
Douglas Gregor99ebf652009-02-27 19:31:52 +0000765 SourceLocation Loc, DeclarationName Entity) {
Douglas Gregordf667e72009-03-10 20:44:00 +0000766 assert(!ActiveTemplateInstantiations.empty() &&
767 "Cannot perform an instantiation without some context on the "
768 "instantiation stack");
769
Douglas Gregor99ebf652009-02-27 19:31:52 +0000770 // If T is not a dependent type, there is nothing to do.
771 if (!T->isDependentType())
772 return T;
773
Douglas Gregor7e063902009-05-11 23:53:27 +0000774 TemplateTypeInstantiator Instantiator(*this, TemplateArgs, Loc, Entity);
Douglas Gregorcd281c32009-02-28 00:25:32 +0000775 return Instantiator(T);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000776}
Douglas Gregor2943aed2009-03-03 04:44:36 +0000777
778/// \brief Instantiate the base class specifiers of the given class
779/// template specialization.
780///
781/// Produces a diagnostic and returns true on error, returns false and
782/// attaches the instantiated base classes to the class template
783/// specialization if successful.
784bool
Douglas Gregord475b8d2009-03-25 21:17:03 +0000785Sema::InstantiateBaseSpecifiers(CXXRecordDecl *Instantiation,
786 CXXRecordDecl *Pattern,
Douglas Gregor7e063902009-05-11 23:53:27 +0000787 const TemplateArgumentList &TemplateArgs) {
Douglas Gregor2943aed2009-03-03 04:44:36 +0000788 bool Invalid = false;
Douglas Gregor0ca20ac2009-05-29 18:27:38 +0000789 llvm::SmallVector<CXXBaseSpecifier*, 4> InstantiatedBases;
Douglas Gregord475b8d2009-03-25 21:17:03 +0000790 for (ClassTemplateSpecializationDecl::base_class_iterator
791 Base = Pattern->bases_begin(), BaseEnd = Pattern->bases_end();
Douglas Gregor27b152f2009-03-10 18:52:44 +0000792 Base != BaseEnd; ++Base) {
Douglas Gregor2943aed2009-03-03 04:44:36 +0000793 if (!Base->getType()->isDependentType()) {
794 // FIXME: Allocate via ASTContext
795 InstantiatedBases.push_back(new CXXBaseSpecifier(*Base));
796 continue;
797 }
798
799 QualType BaseType = InstantiateType(Base->getType(),
Douglas Gregor7e063902009-05-11 23:53:27 +0000800 TemplateArgs,
Douglas Gregor2943aed2009-03-03 04:44:36 +0000801 Base->getSourceRange().getBegin(),
802 DeclarationName());
803 if (BaseType.isNull()) {
804 Invalid = true;
805 continue;
806 }
807
808 if (CXXBaseSpecifier *InstantiatedBase
Douglas Gregord475b8d2009-03-25 21:17:03 +0000809 = CheckBaseSpecifier(Instantiation,
Douglas Gregor2943aed2009-03-03 04:44:36 +0000810 Base->getSourceRange(),
811 Base->isVirtual(),
812 Base->getAccessSpecifierAsWritten(),
813 BaseType,
814 /*FIXME: Not totally accurate */
815 Base->getSourceRange().getBegin()))
816 InstantiatedBases.push_back(InstantiatedBase);
817 else
818 Invalid = true;
819 }
820
Douglas Gregor27b152f2009-03-10 18:52:44 +0000821 if (!Invalid &&
Jay Foadbeaaccd2009-05-21 09:52:38 +0000822 AttachBaseSpecifiers(Instantiation, InstantiatedBases.data(),
Douglas Gregor2943aed2009-03-03 04:44:36 +0000823 InstantiatedBases.size()))
824 Invalid = true;
825
826 return Invalid;
827}
828
Douglas Gregord475b8d2009-03-25 21:17:03 +0000829/// \brief Instantiate the definition of a class from a given pattern.
830///
831/// \param PointOfInstantiation The point of instantiation within the
832/// source code.
833///
834/// \param Instantiation is the declaration whose definition is being
835/// instantiated. This will be either a class template specialization
836/// or a member class of a class template specialization.
837///
838/// \param Pattern is the pattern from which the instantiation
839/// occurs. This will be either the declaration of a class template or
840/// the declaration of a member class of a class template.
841///
842/// \param TemplateArgs The template arguments to be substituted into
843/// the pattern.
844///
Douglas Gregord475b8d2009-03-25 21:17:03 +0000845/// \returns true if an error occurred, false otherwise.
846bool
847Sema::InstantiateClass(SourceLocation PointOfInstantiation,
848 CXXRecordDecl *Instantiation, CXXRecordDecl *Pattern,
Douglas Gregor93dfdb12009-05-13 00:25:59 +0000849 const TemplateArgumentList &TemplateArgs,
850 bool ExplicitInstantiation) {
Douglas Gregord475b8d2009-03-25 21:17:03 +0000851 bool Invalid = false;
852
853 CXXRecordDecl *PatternDef
854 = cast_or_null<CXXRecordDecl>(Pattern->getDefinition(Context));
855 if (!PatternDef) {
856 if (Pattern == Instantiation->getInstantiatedFromMemberClass()) {
857 Diag(PointOfInstantiation,
858 diag::err_implicit_instantiate_member_undefined)
859 << Context.getTypeDeclType(Instantiation);
860 Diag(Pattern->getLocation(), diag::note_member_of_template_here);
861 } else {
Douglas Gregor93dfdb12009-05-13 00:25:59 +0000862 Diag(PointOfInstantiation, diag::err_template_instantiate_undefined)
863 << ExplicitInstantiation
Douglas Gregord475b8d2009-03-25 21:17:03 +0000864 << Context.getTypeDeclType(Instantiation);
865 Diag(Pattern->getLocation(), diag::note_template_decl_here);
866 }
867 return true;
868 }
869 Pattern = PatternDef;
870
Douglas Gregord048bb72009-03-25 21:23:52 +0000871 InstantiatingTemplate Inst(*this, PointOfInstantiation, Instantiation);
Douglas Gregord475b8d2009-03-25 21:17:03 +0000872 if (Inst)
873 return true;
874
875 // Enter the scope of this instantiation. We don't use
876 // PushDeclContext because we don't have a scope.
877 DeclContext *PreviousContext = CurContext;
878 CurContext = Instantiation;
879
880 // Start the definition of this instantiation.
881 Instantiation->startDefinition();
882
883 // Instantiate the base class specifiers.
Douglas Gregor7e063902009-05-11 23:53:27 +0000884 if (InstantiateBaseSpecifiers(Instantiation, Pattern, TemplateArgs))
Douglas Gregord475b8d2009-03-25 21:17:03 +0000885 Invalid = true;
886
Douglas Gregor0ca20ac2009-05-29 18:27:38 +0000887 llvm::SmallVector<DeclPtrTy, 4> Fields;
Douglas Gregor6ab35242009-04-09 21:40:53 +0000888 for (RecordDecl::decl_iterator Member = Pattern->decls_begin(Context),
889 MemberEnd = Pattern->decls_end(Context);
890 Member != MemberEnd; ++Member) {
Douglas Gregor7e063902009-05-11 23:53:27 +0000891 Decl *NewMember = InstantiateDecl(*Member, Instantiation, TemplateArgs);
Douglas Gregord475b8d2009-03-25 21:17:03 +0000892 if (NewMember) {
893 if (NewMember->isInvalidDecl())
894 Invalid = true;
895 else if (FieldDecl *Field = dyn_cast<FieldDecl>(NewMember))
Chris Lattnerb28317a2009-03-28 19:18:32 +0000896 Fields.push_back(DeclPtrTy::make(Field));
Douglas Gregord475b8d2009-03-25 21:17:03 +0000897 } else {
898 // FIXME: Eventually, a NULL return will mean that one of the
Mike Stump390b4cc2009-05-16 07:39:55 +0000899 // instantiations was a semantic disaster, and we'll want to set Invalid =
900 // true. For now, we expect to skip some members that we can't yet handle.
Douglas Gregord475b8d2009-03-25 21:17:03 +0000901 }
902 }
903
904 // Finish checking fields.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000905 ActOnFields(0, Instantiation->getLocation(), DeclPtrTy::make(Instantiation),
Jay Foadbeaaccd2009-05-21 09:52:38 +0000906 Fields.data(), Fields.size(), SourceLocation(), SourceLocation(),
Douglas Gregord475b8d2009-03-25 21:17:03 +0000907 0);
908
909 // Add any implicitly-declared members that we might need.
910 AddImplicitlyDeclaredMembersToClass(Instantiation);
911
912 // Exit the scope of this instantiation.
913 CurContext = PreviousContext;
914
Douglas Gregoraba43bb2009-05-26 20:50:29 +0000915 if (!Invalid)
916 Consumer.HandleTagDeclDefinition(Instantiation);
917
Douglas Gregora58861f2009-05-13 20:28:22 +0000918 // If this is an explicit instantiation, instantiate our members, too.
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000919 if (!Invalid && ExplicitInstantiation) {
920 Inst.Clear();
Douglas Gregora58861f2009-05-13 20:28:22 +0000921 InstantiateClassMembers(PointOfInstantiation, Instantiation, TemplateArgs);
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000922 }
Douglas Gregora58861f2009-05-13 20:28:22 +0000923
Douglas Gregord475b8d2009-03-25 21:17:03 +0000924 return Invalid;
925}
926
Douglas Gregor2943aed2009-03-03 04:44:36 +0000927bool
928Sema::InstantiateClassTemplateSpecialization(
929 ClassTemplateSpecializationDecl *ClassTemplateSpec,
930 bool ExplicitInstantiation) {
931 // Perform the actual instantiation on the canonical declaration.
932 ClassTemplateSpec = cast<ClassTemplateSpecializationDecl>(
933 Context.getCanonicalDecl(ClassTemplateSpec));
934
935 // We can only instantiate something that hasn't already been
936 // instantiated or specialized. Fail without any diagnostics: our
937 // caller will provide an error message.
938 if (ClassTemplateSpec->getSpecializationKind() != TSK_Undeclared)
939 return true;
940
Douglas Gregor2943aed2009-03-03 04:44:36 +0000941 ClassTemplateDecl *Template = ClassTemplateSpec->getSpecializedTemplate();
Douglas Gregord475b8d2009-03-25 21:17:03 +0000942 CXXRecordDecl *Pattern = Template->getTemplatedDecl();
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000943 const TemplateArgumentList *TemplateArgs
944 = &ClassTemplateSpec->getTemplateArgs();
945
Douglas Gregorc1efb3f2009-06-12 22:31:52 +0000946 // C++ [temp.class.spec.match]p1:
947 // When a class template is used in a context that requires an
948 // instantiation of the class, it is necessary to determine
949 // whether the instantiation is to be generated using the primary
950 // template or one of the partial specializations. This is done by
951 // matching the template arguments of the class template
952 // specialization with the template argument lists of the partial
953 // specializations.
Douglas Gregor199d9912009-06-05 00:53:49 +0000954 typedef std::pair<ClassTemplatePartialSpecializationDecl *,
955 TemplateArgumentList *> MatchResult;
956 llvm::SmallVector<MatchResult, 4> Matched;
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000957 for (llvm::FoldingSet<ClassTemplatePartialSpecializationDecl>::iterator
958 Partial = Template->getPartialSpecializations().begin(),
959 PartialEnd = Template->getPartialSpecializations().end();
960 Partial != PartialEnd;
961 ++Partial) {
Douglas Gregorf67875d2009-06-12 18:26:56 +0000962 TemplateDeductionInfo Info(Context);
963 if (TemplateDeductionResult Result
Douglas Gregor199d9912009-06-05 00:53:49 +0000964 = DeduceTemplateArguments(&*Partial,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000965 ClassTemplateSpec->getTemplateArgs(),
966 Info)) {
967 // FIXME: Store the failed-deduction information for use in
968 // diagnostics, later.
969 (void)Result;
970 } else {
971 Matched.push_back(std::make_pair(&*Partial, Info.take()));
972 }
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000973 }
974
975 if (Matched.size() == 1) {
Douglas Gregorc1efb3f2009-06-12 22:31:52 +0000976 // -- If exactly one matching specialization is found, the
977 // instantiation is generated from that specialization.
Douglas Gregor199d9912009-06-05 00:53:49 +0000978 Pattern = Matched[0].first;
979 TemplateArgs = Matched[0].second;
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000980 } else if (Matched.size() > 1) {
Douglas Gregorc1efb3f2009-06-12 22:31:52 +0000981 // -- If more than one matching specialization is found, the
982 // partial order rules (14.5.4.2) are used to determine
983 // whether one of the specializations is more specialized
984 // than the others. If none of the specializations is more
985 // specialized than all of the other matching
986 // specializations, then the use of the class template is
987 // ambiguous and the program is ill-formed.
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000988 // FIXME: Implement partial ordering of class template partial
989 // specializations.
990 Diag(ClassTemplateSpec->getLocation(),
991 diag::unsup_template_partial_spec_ordering);
Douglas Gregorc1efb3f2009-06-12 22:31:52 +0000992 } else {
993 // -- If no matches are found, the instantiation is generated
994 // from the primary template.
995
996 // Since we initialized the pattern and template arguments from
997 // the primary template, there is nothing more we need to do here.
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000998 }
Douglas Gregor2943aed2009-03-03 04:44:36 +0000999
1000 // Note that this is an instantiation.
1001 ClassTemplateSpec->setSpecializationKind(
1002 ExplicitInstantiation? TSK_ExplicitInstantiation
1003 : TSK_ImplicitInstantiation);
1004
Douglas Gregor199d9912009-06-05 00:53:49 +00001005 bool Result = InstantiateClass(ClassTemplateSpec->getLocation(),
1006 ClassTemplateSpec, Pattern, *TemplateArgs,
1007 ExplicitInstantiation);
1008
1009 for (unsigned I = 0, N = Matched.size(); I != N; ++I) {
1010 // FIXME: Implement TemplateArgumentList::Destroy!
1011 // if (Matched[I].first != Pattern)
1012 // Matched[I].second->Destroy(Context);
1013 }
1014
1015 return Result;
Douglas Gregor2943aed2009-03-03 04:44:36 +00001016}
Douglas Gregor5953d8b2009-03-19 17:26:29 +00001017
Douglas Gregora58861f2009-05-13 20:28:22 +00001018/// \brief Instantiate the definitions of all of the member of the
1019/// given class, which is an instantiation of a class template or a
1020/// member class of a template.
1021void
1022Sema::InstantiateClassMembers(SourceLocation PointOfInstantiation,
1023 CXXRecordDecl *Instantiation,
1024 const TemplateArgumentList &TemplateArgs) {
1025 for (DeclContext::decl_iterator D = Instantiation->decls_begin(Context),
1026 DEnd = Instantiation->decls_end(Context);
1027 D != DEnd; ++D) {
1028 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(*D)) {
1029 if (!Function->getBody(Context))
Douglas Gregorf3e7ce42009-05-18 17:01:57 +00001030 InstantiateFunctionDefinition(PointOfInstantiation, Function);
Douglas Gregora58861f2009-05-13 20:28:22 +00001031 } else if (VarDecl *Var = dyn_cast<VarDecl>(*D)) {
1032 const VarDecl *Def = 0;
1033 if (!Var->getDefinition(Def))
1034 InstantiateVariableDefinition(Var);
1035 } else if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(*D)) {
1036 if (!Record->isInjectedClassName() && !Record->getDefinition(Context)) {
1037 assert(Record->getInstantiatedFromMemberClass() &&
1038 "Missing instantiated-from-template information");
Douglas Gregorf3e7ce42009-05-18 17:01:57 +00001039 InstantiateClass(PointOfInstantiation, Record,
Douglas Gregora58861f2009-05-13 20:28:22 +00001040 Record->getInstantiatedFromMemberClass(),
1041 TemplateArgs, true);
1042 }
1043 }
1044 }
1045}
1046
1047/// \brief Instantiate the definitions of all of the members of the
1048/// given class template specialization, which was named as part of an
1049/// explicit instantiation.
1050void Sema::InstantiateClassTemplateSpecializationMembers(
1051 SourceLocation PointOfInstantiation,
1052 ClassTemplateSpecializationDecl *ClassTemplateSpec) {
1053 // C++0x [temp.explicit]p7:
1054 // An explicit instantiation that names a class template
1055 // specialization is an explicit instantion of the same kind
1056 // (declaration or definition) of each of its members (not
1057 // including members inherited from base classes) that has not
1058 // been previously explicitly specialized in the translation unit
1059 // containing the explicit instantiation, except as described
1060 // below.
1061 InstantiateClassMembers(PointOfInstantiation, ClassTemplateSpec,
1062 ClassTemplateSpec->getTemplateArgs());
1063}
1064
Douglas Gregorab452ba2009-03-26 23:50:42 +00001065/// \brief Instantiate a nested-name-specifier.
1066NestedNameSpecifier *
1067Sema::InstantiateNestedNameSpecifier(NestedNameSpecifier *NNS,
1068 SourceRange Range,
Douglas Gregor7e063902009-05-11 23:53:27 +00001069 const TemplateArgumentList &TemplateArgs) {
Douglas Gregorab452ba2009-03-26 23:50:42 +00001070 // Instantiate the prefix of this nested name specifier.
1071 NestedNameSpecifier *Prefix = NNS->getPrefix();
1072 if (Prefix) {
Douglas Gregor7e063902009-05-11 23:53:27 +00001073 Prefix = InstantiateNestedNameSpecifier(Prefix, Range, TemplateArgs);
Douglas Gregorab452ba2009-03-26 23:50:42 +00001074 if (!Prefix)
1075 return 0;
Douglas Gregor5953d8b2009-03-19 17:26:29 +00001076 }
1077
Douglas Gregorab452ba2009-03-26 23:50:42 +00001078 switch (NNS->getKind()) {
Douglas Gregor17343172009-04-01 00:28:59 +00001079 case NestedNameSpecifier::Identifier: {
1080 assert(Prefix &&
1081 "Can't have an identifier nested-name-specifier with no prefix");
1082 CXXScopeSpec SS;
1083 // FIXME: The source location information is all wrong.
1084 SS.setRange(Range);
1085 SS.setScopeRep(Prefix);
1086 return static_cast<NestedNameSpecifier *>(
1087 ActOnCXXNestedNameSpecifier(0, SS,
1088 Range.getEnd(),
1089 Range.getEnd(),
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001090 *NNS->getAsIdentifier()));
Douglas Gregorab452ba2009-03-26 23:50:42 +00001091 break;
Douglas Gregor17343172009-04-01 00:28:59 +00001092 }
Douglas Gregorab452ba2009-03-26 23:50:42 +00001093
1094 case NestedNameSpecifier::Namespace:
1095 case NestedNameSpecifier::Global:
1096 return NNS;
1097
1098 case NestedNameSpecifier::TypeSpecWithTemplate:
1099 case NestedNameSpecifier::TypeSpec: {
1100 QualType T = QualType(NNS->getAsType(), 0);
1101 if (!T->isDependentType())
1102 return NNS;
1103
Douglas Gregor7e063902009-05-11 23:53:27 +00001104 T = InstantiateType(T, TemplateArgs, Range.getBegin(), DeclarationName());
Douglas Gregorab452ba2009-03-26 23:50:42 +00001105 if (T.isNull())
1106 return 0;
1107
Eli Friedman923f7532009-06-13 04:51:30 +00001108 if (T->isDependentType() || T->isRecordType() ||
Douglas Gregord57959a2009-03-27 23:10:48 +00001109 (getLangOptions().CPlusPlus0x && T->isEnumeralType())) {
Douglas Gregor17343172009-04-01 00:28:59 +00001110 assert(T.getCVRQualifiers() == 0 && "Can't get cv-qualifiers here");
Douglas Gregord57959a2009-03-27 23:10:48 +00001111 return NestedNameSpecifier::Create(Context, Prefix,
Douglas Gregorab452ba2009-03-26 23:50:42 +00001112 NNS->getKind() == NestedNameSpecifier::TypeSpecWithTemplate,
Douglas Gregord57959a2009-03-27 23:10:48 +00001113 T.getTypePtr());
1114 }
1115
1116 Diag(Range.getBegin(), diag::err_nested_name_spec_non_tag) << T;
1117 return 0;
Douglas Gregorab452ba2009-03-26 23:50:42 +00001118 }
1119 }
1120
Douglas Gregord57959a2009-03-27 23:10:48 +00001121 // Required to silence a GCC warning
Douglas Gregorab452ba2009-03-26 23:50:42 +00001122 return 0;
Douglas Gregor5953d8b2009-03-19 17:26:29 +00001123}
Douglas Gregorde650ae2009-03-31 18:38:02 +00001124
1125TemplateName
1126Sema::InstantiateTemplateName(TemplateName Name, SourceLocation Loc,
Douglas Gregor7e063902009-05-11 23:53:27 +00001127 const TemplateArgumentList &TemplateArgs) {
Douglas Gregorde650ae2009-03-31 18:38:02 +00001128 if (TemplateTemplateParmDecl *TTP
1129 = dyn_cast_or_null<TemplateTemplateParmDecl>(
1130 Name.getAsTemplateDecl())) {
1131 assert(TTP->getDepth() == 0 &&
1132 "Cannot reduce depth of a template template parameter");
Douglas Gregor9bde7732009-03-31 20:22:05 +00001133 assert(TemplateArgs[TTP->getPosition()].getAsDecl() &&
Douglas Gregorde650ae2009-03-31 18:38:02 +00001134 "Wrong kind of template template argument");
1135 ClassTemplateDecl *ClassTemplate
1136 = dyn_cast<ClassTemplateDecl>(
1137 TemplateArgs[TTP->getPosition()].getAsDecl());
Douglas Gregor9bde7732009-03-31 20:22:05 +00001138 assert(ClassTemplate && "Expected a class template");
Douglas Gregorde650ae2009-03-31 18:38:02 +00001139 if (QualifiedTemplateName *QTN = Name.getAsQualifiedTemplateName()) {
1140 NestedNameSpecifier *NNS
1141 = InstantiateNestedNameSpecifier(QTN->getQualifier(),
1142 /*FIXME=*/SourceRange(Loc),
Douglas Gregor7e063902009-05-11 23:53:27 +00001143 TemplateArgs);
Douglas Gregorde650ae2009-03-31 18:38:02 +00001144 if (NNS)
1145 return Context.getQualifiedTemplateName(NNS,
1146 QTN->hasTemplateKeyword(),
1147 ClassTemplate);
1148 }
1149
1150 return TemplateName(ClassTemplate);
1151 } else if (DependentTemplateName *DTN = Name.getAsDependentTemplateName()) {
1152 NestedNameSpecifier *NNS
1153 = InstantiateNestedNameSpecifier(DTN->getQualifier(),
1154 /*FIXME=*/SourceRange(Loc),
Douglas Gregor7e063902009-05-11 23:53:27 +00001155 TemplateArgs);
Douglas Gregorde650ae2009-03-31 18:38:02 +00001156
1157 if (!NNS) // FIXME: Not the best recovery strategy.
1158 return Name;
1159
1160 if (NNS->isDependent())
1161 return Context.getDependentTemplateName(NNS, DTN->getName());
1162
1163 // Somewhat redundant with ActOnDependentTemplateName.
1164 CXXScopeSpec SS;
1165 SS.setRange(SourceRange(Loc));
1166 SS.setScopeRep(NNS);
1167 TemplateTy Template;
1168 TemplateNameKind TNK = isTemplateName(*DTN->getName(), 0, Template, &SS);
1169 if (TNK == TNK_Non_template) {
1170 Diag(Loc, diag::err_template_kw_refers_to_non_template)
1171 << DTN->getName();
1172 return Name;
1173 } else if (TNK == TNK_Function_template) {
1174 Diag(Loc, diag::err_template_kw_refers_to_non_template)
1175 << DTN->getName();
1176 return Name;
1177 }
1178
1179 return Template.getAsVal<TemplateName>();
1180 }
1181
1182
1183
Mike Stump390b4cc2009-05-16 07:39:55 +00001184 // FIXME: Even if we're referring to a Decl that isn't a template template
1185 // parameter, we may need to instantiate the outer contexts of that
1186 // Decl. However, this won't be needed until we implement member templates.
Douglas Gregorde650ae2009-03-31 18:38:02 +00001187 return Name;
1188}
Douglas Gregor91333002009-06-11 00:06:24 +00001189
1190TemplateArgument Sema::Instantiate(TemplateArgument Arg,
1191 const TemplateArgumentList &TemplateArgs) {
1192 switch (Arg.getKind()) {
1193 case TemplateArgument::Null:
1194 assert(false && "Should never have a NULL template argument");
1195 break;
1196
1197 case TemplateArgument::Type: {
1198 QualType T = InstantiateType(Arg.getAsType(), TemplateArgs,
1199 Arg.getLocation(), DeclarationName());
1200 if (T.isNull())
1201 return TemplateArgument();
1202
1203 return TemplateArgument(Arg.getLocation(), T);
1204 }
1205
1206 case TemplateArgument::Declaration:
1207 // FIXME: Template instantiation for template template parameters.
1208 return Arg;
1209
1210 case TemplateArgument::Integral:
1211 return Arg;
1212
1213 case TemplateArgument::Expression: {
Douglas Gregorac7610d2009-06-22 20:57:11 +00001214 // Template argument expressions are not potentially evaluated.
1215 EnterExpressionEvaluationContext Unevaluated(*this, Action::Unevaluated);
1216
Douglas Gregor91333002009-06-11 00:06:24 +00001217 Sema::OwningExprResult E = InstantiateExpr(Arg.getAsExpr(), TemplateArgs);
1218 if (E.isInvalid())
1219 return TemplateArgument();
1220 return TemplateArgument(E.takeAs<Expr>());
1221 }
Anders Carlssond01b1da2009-06-15 17:04:53 +00001222
1223 case TemplateArgument::Pack:
1224 assert(0 && "FIXME: Implement!");
1225 break;
Douglas Gregor91333002009-06-11 00:06:24 +00001226 }
1227
1228 assert(false && "Unhandled template argument kind");
1229 return TemplateArgument();
1230}