blob: 31c048d74c1e0562a0e9af89f206c42e3922a6ff [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) {
32 if (ClassTemplateSpecializationDecl *Spec
33 = dyn_cast<ClassTemplateSpecializationDecl>(D))
34 return Spec->getTemplateArgs();
35
36 DeclContext *EnclosingTemplateCtx = D->getDeclContext();
37 while (!isa<ClassTemplateSpecializationDecl>(EnclosingTemplateCtx)) {
38 assert(!EnclosingTemplateCtx->isFileContext() &&
39 "Tried to get the instantiation arguments of a non-template");
40 EnclosingTemplateCtx = EnclosingTemplateCtx->getParent();
41 }
42
43 ClassTemplateSpecializationDecl *EnclosingTemplate
44 = cast<ClassTemplateSpecializationDecl>(EnclosingTemplateCtx);
45 return EnclosingTemplate->getTemplateArgs();
46}
47
Douglas Gregor26dce442009-03-10 00:06:19 +000048Sema::InstantiatingTemplate::
49InstantiatingTemplate(Sema &SemaRef, SourceLocation PointOfInstantiation,
Douglas Gregorf3e7ce42009-05-18 17:01:57 +000050 Decl *Entity,
Douglas Gregor26dce442009-03-10 00:06:19 +000051 SourceRange InstantiationRange)
52 : SemaRef(SemaRef) {
Douglas Gregordf667e72009-03-10 20:44:00 +000053
54 Invalid = CheckInstantiationDepth(PointOfInstantiation,
55 InstantiationRange);
56 if (!Invalid) {
Douglas Gregor26dce442009-03-10 00:06:19 +000057 ActiveTemplateInstantiation Inst;
Douglas Gregordf667e72009-03-10 20:44:00 +000058 Inst.Kind = ActiveTemplateInstantiation::TemplateInstantiation;
Douglas Gregor26dce442009-03-10 00:06:19 +000059 Inst.PointOfInstantiation = PointOfInstantiation;
Douglas Gregordf667e72009-03-10 20:44:00 +000060 Inst.Entity = reinterpret_cast<uintptr_t>(Entity);
Douglas Gregor313a81d2009-03-12 18:36:18 +000061 Inst.TemplateArgs = 0;
62 Inst.NumTemplateArgs = 0;
Douglas Gregordf667e72009-03-10 20:44:00 +000063 Inst.InstantiationRange = InstantiationRange;
64 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
65 Invalid = false;
66 }
67}
68
69Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef,
70 SourceLocation PointOfInstantiation,
71 TemplateDecl *Template,
72 const TemplateArgument *TemplateArgs,
73 unsigned NumTemplateArgs,
74 SourceRange InstantiationRange)
75 : SemaRef(SemaRef) {
76
77 Invalid = CheckInstantiationDepth(PointOfInstantiation,
78 InstantiationRange);
79 if (!Invalid) {
80 ActiveTemplateInstantiation Inst;
81 Inst.Kind
82 = ActiveTemplateInstantiation::DefaultTemplateArgumentInstantiation;
83 Inst.PointOfInstantiation = PointOfInstantiation;
84 Inst.Entity = reinterpret_cast<uintptr_t>(Template);
85 Inst.TemplateArgs = TemplateArgs;
86 Inst.NumTemplateArgs = NumTemplateArgs;
Douglas Gregor26dce442009-03-10 00:06:19 +000087 Inst.InstantiationRange = InstantiationRange;
88 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
89 Invalid = false;
90 }
91}
92
Douglas Gregor637a4092009-06-10 23:47:09 +000093Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef,
94 SourceLocation PointOfInstantiation,
95 ClassTemplatePartialSpecializationDecl *PartialSpec,
96 const TemplateArgument *TemplateArgs,
97 unsigned NumTemplateArgs,
98 SourceRange InstantiationRange)
99 : SemaRef(SemaRef) {
100
101 Invalid = CheckInstantiationDepth(PointOfInstantiation,
102 InstantiationRange);
103 if (!Invalid) {
104 ActiveTemplateInstantiation Inst;
105 Inst.Kind
106 = ActiveTemplateInstantiation::PartialSpecDeductionInstantiation;
107 Inst.PointOfInstantiation = PointOfInstantiation;
108 Inst.Entity = reinterpret_cast<uintptr_t>(PartialSpec);
109 Inst.TemplateArgs = TemplateArgs;
110 Inst.NumTemplateArgs = NumTemplateArgs;
111 Inst.InstantiationRange = InstantiationRange;
112 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
113 Invalid = false;
114 }
115}
116
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000117void Sema::InstantiatingTemplate::Clear() {
118 if (!Invalid) {
Douglas Gregor26dce442009-03-10 00:06:19 +0000119 SemaRef.ActiveTemplateInstantiations.pop_back();
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000120 Invalid = true;
121 }
Douglas Gregor26dce442009-03-10 00:06:19 +0000122}
123
Douglas Gregordf667e72009-03-10 20:44:00 +0000124bool Sema::InstantiatingTemplate::CheckInstantiationDepth(
125 SourceLocation PointOfInstantiation,
126 SourceRange InstantiationRange) {
127 if (SemaRef.ActiveTemplateInstantiations.size()
128 <= SemaRef.getLangOptions().InstantiationDepth)
129 return false;
130
131 SemaRef.Diag(PointOfInstantiation,
132 diag::err_template_recursion_depth_exceeded)
133 << SemaRef.getLangOptions().InstantiationDepth
134 << InstantiationRange;
135 SemaRef.Diag(PointOfInstantiation, diag::note_template_recursion_depth)
136 << SemaRef.getLangOptions().InstantiationDepth;
137 return true;
138}
139
Douglas Gregoree1828a2009-03-10 18:03:33 +0000140/// \brief Prints the current instantiation stack through a series of
141/// notes.
142void Sema::PrintInstantiationStack() {
143 for (llvm::SmallVector<ActiveTemplateInstantiation, 16>::reverse_iterator
144 Active = ActiveTemplateInstantiations.rbegin(),
145 ActiveEnd = ActiveTemplateInstantiations.rend();
146 Active != ActiveEnd;
147 ++Active) {
Douglas Gregordf667e72009-03-10 20:44:00 +0000148 switch (Active->Kind) {
149 case ActiveTemplateInstantiation::TemplateInstantiation: {
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000150 Decl *D = reinterpret_cast<Decl *>(Active->Entity);
151 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) {
152 unsigned DiagID = diag::note_template_member_class_here;
153 if (isa<ClassTemplateSpecializationDecl>(Record))
154 DiagID = diag::note_template_class_instantiation_here;
155 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
156 DiagID)
157 << Context.getTypeDeclType(Record)
158 << Active->InstantiationRange;
159 } else {
160 FunctionDecl *Function = cast<FunctionDecl>(D);
161 unsigned DiagID = diag::note_template_member_function_here;
162 // FIXME: check for a function template
163 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
164 DiagID)
165 << Function
166 << Active->InstantiationRange;
167 }
Douglas Gregordf667e72009-03-10 20:44:00 +0000168 break;
169 }
170
171 case ActiveTemplateInstantiation::DefaultTemplateArgumentInstantiation: {
172 TemplateDecl *Template = cast<TemplateDecl>((Decl *)Active->Entity);
173 std::string TemplateArgsStr
Douglas Gregor7532dc62009-03-30 22:58:21 +0000174 = TemplateSpecializationType::PrintTemplateArgumentList(
Douglas Gregordf667e72009-03-10 20:44:00 +0000175 Active->TemplateArgs,
Douglas Gregord249e1d1f2009-05-29 20:38:28 +0000176 Active->NumTemplateArgs,
177 Context.PrintingPolicy);
Douglas Gregordf667e72009-03-10 20:44:00 +0000178 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
179 diag::note_default_arg_instantiation_here)
180 << (Template->getNameAsString() + TemplateArgsStr)
181 << Active->InstantiationRange;
182 break;
183 }
Douglas Gregor637a4092009-06-10 23:47:09 +0000184
185 case ActiveTemplateInstantiation::PartialSpecDeductionInstantiation: {
186 ClassTemplatePartialSpecializationDecl *PartialSpec
187 = cast<ClassTemplatePartialSpecializationDecl>((Decl *)Active->Entity);
Douglas Gregor637a4092009-06-10 23:47:09 +0000188 // FIXME: The active template instantiation's template arguments
189 // are interesting, too. We should add something like [with T =
190 // foo, U = bar, etc.] to the string.
191 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
192 diag::note_partial_spec_deduct_instantiation_here)
Douglas Gregor02cbbd22009-06-11 18:10:32 +0000193 << Context.getTypeDeclType(PartialSpec)
Douglas Gregor637a4092009-06-10 23:47:09 +0000194 << Active->InstantiationRange;
195 break;
196 }
197
Douglas Gregordf667e72009-03-10 20:44:00 +0000198 }
Douglas Gregoree1828a2009-03-10 18:03:33 +0000199 }
200}
201
Douglas Gregor5e9f35c2009-06-14 07:33:30 +0000202bool Sema::isSFINAEContext() const {
203 using llvm::SmallVector;
204 for (SmallVector<ActiveTemplateInstantiation, 16>::const_reverse_iterator
205 Active = ActiveTemplateInstantiations.rbegin(),
206 ActiveEnd = ActiveTemplateInstantiations.rend();
207 Active != ActiveEnd;
208 ++Active) {
209
210 switch(Active->Kind) {
211 case ActiveTemplateInstantiation::PartialSpecDeductionInstantiation:
212 // We're in a template argument deduction context, so SFINAE
213 // applies.
214 return true;
215
216 case ActiveTemplateInstantiation::DefaultTemplateArgumentInstantiation:
217 // A default template argument instantiation may or may not be a
218 // SFINAE context; look further up the stack.
219 break;
220
221 case ActiveTemplateInstantiation::TemplateInstantiation:
222 // This is a template instantiation, so there is no SFINAE.
223 return false;
224 }
225 }
226
227 return false;
228}
229
Douglas Gregor99ebf652009-02-27 19:31:52 +0000230//===----------------------------------------------------------------------===/
231// Template Instantiation for Types
232//===----------------------------------------------------------------------===/
Douglas Gregorcd281c32009-02-28 00:25:32 +0000233namespace {
234 class VISIBILITY_HIDDEN TemplateTypeInstantiator {
235 Sema &SemaRef;
Douglas Gregor7e063902009-05-11 23:53:27 +0000236 const TemplateArgumentList &TemplateArgs;
Douglas Gregorcd281c32009-02-28 00:25:32 +0000237 SourceLocation Loc;
238 DeclarationName Entity;
Douglas Gregor99ebf652009-02-27 19:31:52 +0000239
Douglas Gregorcd281c32009-02-28 00:25:32 +0000240 public:
241 TemplateTypeInstantiator(Sema &SemaRef,
Douglas Gregor7e063902009-05-11 23:53:27 +0000242 const TemplateArgumentList &TemplateArgs,
Douglas Gregorcd281c32009-02-28 00:25:32 +0000243 SourceLocation Loc,
244 DeclarationName Entity)
245 : SemaRef(SemaRef), TemplateArgs(TemplateArgs),
Douglas Gregor7e063902009-05-11 23:53:27 +0000246 Loc(Loc), Entity(Entity) { }
Douglas Gregorcd281c32009-02-28 00:25:32 +0000247
248 QualType operator()(QualType T) const { return Instantiate(T); }
249
250 QualType Instantiate(QualType T) const;
251
252 // Declare instantiate functions for each type.
253#define TYPE(Class, Base) \
254 QualType Instantiate##Class##Type(const Class##Type *T, \
255 unsigned Quals) const;
256#define ABSTRACT_TYPE(Class, Base)
257#include "clang/AST/TypeNodes.def"
258 };
259}
260
261QualType
262TemplateTypeInstantiator::InstantiateExtQualType(const ExtQualType *T,
263 unsigned Quals) const {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000264 // FIXME: Implement this
265 assert(false && "Cannot instantiate ExtQualType yet");
266 return QualType();
267}
268
Douglas Gregorcd281c32009-02-28 00:25:32 +0000269QualType
270TemplateTypeInstantiator::InstantiateBuiltinType(const BuiltinType *T,
271 unsigned Quals) const {
Douglas Gregor724651c2009-02-28 01:04:19 +0000272 assert(false && "Builtin types are not dependent and cannot be instantiated");
Douglas Gregorcd281c32009-02-28 00:25:32 +0000273 return QualType(T, Quals);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000274}
275
Douglas Gregorcd281c32009-02-28 00:25:32 +0000276QualType
277TemplateTypeInstantiator::
278InstantiateFixedWidthIntType(const FixedWidthIntType *T, unsigned Quals) const {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000279 // FIXME: Implement this
280 assert(false && "Cannot instantiate FixedWidthIntType yet");
281 return QualType();
282}
283
Douglas Gregorcd281c32009-02-28 00:25:32 +0000284QualType
285TemplateTypeInstantiator::InstantiateComplexType(const ComplexType *T,
286 unsigned Quals) const {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000287 // FIXME: Implement this
288 assert(false && "Cannot instantiate ComplexType yet");
289 return QualType();
290}
291
Douglas Gregorcd281c32009-02-28 00:25:32 +0000292QualType
293TemplateTypeInstantiator::InstantiatePointerType(const PointerType *T,
294 unsigned Quals) const {
295 QualType PointeeType = Instantiate(T->getPointeeType());
296 if (PointeeType.isNull())
297 return QualType();
298
299 return SemaRef.BuildPointerType(PointeeType, Quals, Loc, Entity);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000300}
301
Douglas Gregorcd281c32009-02-28 00:25:32 +0000302QualType
303TemplateTypeInstantiator::InstantiateBlockPointerType(const BlockPointerType *T,
304 unsigned Quals) const {
Anders Carlsson859ba502009-06-12 16:23:10 +0000305 QualType PointeeType = Instantiate(T->getPointeeType());
306 if (PointeeType.isNull())
307 return QualType();
308
Anders Carlsson9a917e42009-06-12 22:56:54 +0000309 return SemaRef.BuildBlockPointerType(PointeeType, Quals, Loc, Entity);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000310}
311
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000312QualType
313TemplateTypeInstantiator::InstantiateLValueReferenceType(
314 const LValueReferenceType *T, unsigned Quals) const {
Douglas Gregorcd281c32009-02-28 00:25:32 +0000315 QualType ReferentType = Instantiate(T->getPointeeType());
316 if (ReferentType.isNull())
317 return QualType();
318
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000319 return SemaRef.BuildReferenceType(ReferentType, true, Quals, Loc, Entity);
320}
321
322QualType
323TemplateTypeInstantiator::InstantiateRValueReferenceType(
324 const RValueReferenceType *T, unsigned Quals) const {
325 QualType ReferentType = Instantiate(T->getPointeeType());
326 if (ReferentType.isNull())
327 return QualType();
328
329 return SemaRef.BuildReferenceType(ReferentType, false, Quals, Loc, Entity);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000330}
331
Douglas Gregorcd281c32009-02-28 00:25:32 +0000332QualType
333TemplateTypeInstantiator::
334InstantiateMemberPointerType(const MemberPointerType *T,
335 unsigned Quals) const {
Douglas Gregor949bf692009-06-09 22:17:39 +0000336 QualType PointeeType = Instantiate(T->getPointeeType());
337 if (PointeeType.isNull())
338 return QualType();
339
340 QualType ClassType = Instantiate(QualType(T->getClass(), 0));
341 if (ClassType.isNull())
342 return QualType();
343
344 return SemaRef.BuildMemberPointerType(PointeeType, ClassType, Quals, Loc,
345 Entity);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000346}
347
Douglas Gregorcd281c32009-02-28 00:25:32 +0000348QualType
349TemplateTypeInstantiator::
350InstantiateConstantArrayType(const ConstantArrayType *T,
351 unsigned Quals) const {
352 QualType ElementType = Instantiate(T->getElementType());
353 if (ElementType.isNull())
354 return ElementType;
355
356 // Build a temporary integer literal to specify the size for
357 // BuildArrayType. Since we have already checked the size as part of
358 // creating the dependent array type in the first place, we know
Douglas Gregorff668032009-05-13 18:28:20 +0000359 // there aren't any errors. However, we do need to determine what
360 // C++ type to give the size expression.
361 llvm::APInt Size = T->getSize();
362 QualType Types[] = {
363 SemaRef.Context.UnsignedCharTy, SemaRef.Context.UnsignedShortTy,
364 SemaRef.Context.UnsignedIntTy, SemaRef.Context.UnsignedLongTy,
365 SemaRef.Context.UnsignedLongLongTy, SemaRef.Context.UnsignedInt128Ty
366 };
367 const unsigned NumTypes = sizeof(Types) / sizeof(QualType);
368 QualType SizeType;
369 for (unsigned I = 0; I != NumTypes; ++I)
370 if (Size.getBitWidth() == SemaRef.Context.getIntWidth(Types[I])) {
371 SizeType = Types[I];
372 break;
373 }
374
375 if (SizeType.isNull())
376 SizeType = SemaRef.Context.getFixedWidthIntType(Size.getBitWidth(), false);
377
378 IntegerLiteral ArraySize(Size, SizeType, Loc);
Douglas Gregorcd281c32009-02-28 00:25:32 +0000379 return SemaRef.BuildArrayType(ElementType, T->getSizeModifier(),
380 &ArraySize, T->getIndexTypeQualifier(),
381 Loc, Entity);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000382}
383
Douglas Gregorcd281c32009-02-28 00:25:32 +0000384QualType
385TemplateTypeInstantiator::
386InstantiateIncompleteArrayType(const IncompleteArrayType *T,
387 unsigned Quals) const {
388 QualType ElementType = Instantiate(T->getElementType());
389 if (ElementType.isNull())
390 return ElementType;
391
392 return SemaRef.BuildArrayType(ElementType, T->getSizeModifier(),
393 0, T->getIndexTypeQualifier(),
394 Loc, Entity);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000395}
396
Douglas Gregorcd281c32009-02-28 00:25:32 +0000397QualType
398TemplateTypeInstantiator::
399InstantiateVariableArrayType(const VariableArrayType *T,
400 unsigned Quals) const {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000401 // FIXME: Implement this
402 assert(false && "Cannot instantiate VariableArrayType yet");
403 return QualType();
404}
405
Douglas Gregorcd281c32009-02-28 00:25:32 +0000406QualType
407TemplateTypeInstantiator::
408InstantiateDependentSizedArrayType(const DependentSizedArrayType *T,
409 unsigned Quals) const {
Anders Carlsson76b1c842009-03-15 20:12:13 +0000410 Expr *ArraySize = T->getSizeExpr();
411 assert(ArraySize->isValueDependent() &&
412 "dependent sized array types must have value dependent size expr");
413
414 // Instantiate the element type if needed
415 QualType ElementType = T->getElementType();
416 if (ElementType->isDependentType()) {
417 ElementType = Instantiate(ElementType);
418 if (ElementType.isNull())
419 return QualType();
420 }
421
422 // Instantiate the size expression
423 Sema::OwningExprResult InstantiatedArraySize =
Douglas Gregor7e063902009-05-11 23:53:27 +0000424 SemaRef.InstantiateExpr(ArraySize, TemplateArgs);
Anders Carlsson76b1c842009-03-15 20:12:13 +0000425 if (InstantiatedArraySize.isInvalid())
426 return QualType();
427
428 return SemaRef.BuildArrayType(ElementType, T->getSizeModifier(),
Anders Carlssone9146f22009-05-01 19:49:17 +0000429 InstantiatedArraySize.takeAs<Expr>(),
Anders Carlsson76b1c842009-03-15 20:12:13 +0000430 T->getIndexTypeQualifier(), Loc, Entity);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000431}
432
Douglas Gregorcd281c32009-02-28 00:25:32 +0000433QualType
434TemplateTypeInstantiator::InstantiateVectorType(const VectorType *T,
435 unsigned Quals) const {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000436 // FIXME: Implement this
437 assert(false && "Cannot instantiate VectorType yet");
438 return QualType();
439}
440
Douglas Gregorcd281c32009-02-28 00:25:32 +0000441QualType
442TemplateTypeInstantiator::InstantiateExtVectorType(const ExtVectorType *T,
443 unsigned Quals) const {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000444 // FIXME: Implement this
445 assert(false && "Cannot instantiate ExtVectorType yet");
446 return QualType();
447}
448
Douglas Gregorcd281c32009-02-28 00:25:32 +0000449QualType
450TemplateTypeInstantiator::
451InstantiateFunctionProtoType(const FunctionProtoType *T,
452 unsigned Quals) const {
Douglas Gregor724651c2009-02-28 01:04:19 +0000453 QualType ResultType = Instantiate(T->getResultType());
454 if (ResultType.isNull())
455 return ResultType;
456
Douglas Gregor0ca20ac2009-05-29 18:27:38 +0000457 llvm::SmallVector<QualType, 4> ParamTypes;
Douglas Gregor724651c2009-02-28 01:04:19 +0000458 for (FunctionProtoType::arg_type_iterator Param = T->arg_type_begin(),
459 ParamEnd = T->arg_type_end();
460 Param != ParamEnd; ++Param) {
461 QualType P = Instantiate(*Param);
462 if (P.isNull())
463 return P;
464
465 ParamTypes.push_back(P);
466 }
467
Douglas Gregor02cbbd22009-06-11 18:10:32 +0000468 return SemaRef.BuildFunctionType(ResultType, ParamTypes.data(),
Douglas Gregor724651c2009-02-28 01:04:19 +0000469 ParamTypes.size(),
470 T->isVariadic(), T->getTypeQuals(),
471 Loc, Entity);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000472}
473
Douglas Gregorcd281c32009-02-28 00:25:32 +0000474QualType
475TemplateTypeInstantiator::
476InstantiateFunctionNoProtoType(const FunctionNoProtoType *T,
477 unsigned Quals) const {
Douglas Gregor724651c2009-02-28 01:04:19 +0000478 assert(false && "Functions without prototypes cannot be dependent.");
Douglas Gregor99ebf652009-02-27 19:31:52 +0000479 return QualType();
480}
481
Douglas Gregorcd281c32009-02-28 00:25:32 +0000482QualType
483TemplateTypeInstantiator::InstantiateTypedefType(const TypedefType *T,
484 unsigned Quals) const {
Douglas Gregor815215d2009-05-27 05:35:12 +0000485 TypedefDecl *Typedef
Douglas Gregored961e72009-05-27 17:54:46 +0000486 = cast_or_null<TypedefDecl>(
487 SemaRef.InstantiateCurrentDeclRef(T->getDecl()));
Douglas Gregor815215d2009-05-27 05:35:12 +0000488 if (!Typedef)
489 return QualType();
490
491 return SemaRef.Context.getTypeDeclType(Typedef);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000492}
493
Douglas Gregorcd281c32009-02-28 00:25:32 +0000494QualType
495TemplateTypeInstantiator::InstantiateTypeOfExprType(const TypeOfExprType *T,
496 unsigned Quals) const {
Douglas Gregor5f8bd592009-05-26 22:09:24 +0000497 Sema::OwningExprResult E
498 = SemaRef.InstantiateExpr(T->getUnderlyingExpr(), TemplateArgs);
499 if (E.isInvalid())
500 return QualType();
501
502 return SemaRef.Context.getTypeOfExprType(E.takeAs<Expr>());
Douglas Gregor99ebf652009-02-27 19:31:52 +0000503}
504
Douglas Gregorcd281c32009-02-28 00:25:32 +0000505QualType
506TemplateTypeInstantiator::InstantiateTypeOfType(const TypeOfType *T,
507 unsigned Quals) const {
Douglas Gregor5f8bd592009-05-26 22:09:24 +0000508 QualType Underlying = Instantiate(T->getUnderlyingType());
509 if (Underlying.isNull())
510 return QualType();
511
512 return SemaRef.Context.getTypeOfType(Underlying);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000513}
514
Douglas Gregorcd281c32009-02-28 00:25:32 +0000515QualType
516TemplateTypeInstantiator::InstantiateRecordType(const RecordType *T,
517 unsigned Quals) const {
Douglas Gregor815215d2009-05-27 05:35:12 +0000518 RecordDecl *Record
Douglas Gregored961e72009-05-27 17:54:46 +0000519 = cast_or_null<RecordDecl>(SemaRef.InstantiateCurrentDeclRef(T->getDecl()));
Douglas Gregor815215d2009-05-27 05:35:12 +0000520 if (!Record)
521 return QualType();
522
523 return SemaRef.Context.getTypeDeclType(Record);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000524}
525
Douglas Gregorcd281c32009-02-28 00:25:32 +0000526QualType
Douglas Gregorcd281c32009-02-28 00:25:32 +0000527TemplateTypeInstantiator::InstantiateEnumType(const EnumType *T,
528 unsigned Quals) const {
Douglas Gregor815215d2009-05-27 05:35:12 +0000529 EnumDecl *Enum
Douglas Gregored961e72009-05-27 17:54:46 +0000530 = cast_or_null<EnumDecl>(SemaRef.InstantiateCurrentDeclRef(T->getDecl()));
Douglas Gregor815215d2009-05-27 05:35:12 +0000531 if (!Enum)
532 return QualType();
533
534 return SemaRef.Context.getTypeDeclType(Enum);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000535}
536
Douglas Gregorcd281c32009-02-28 00:25:32 +0000537QualType
538TemplateTypeInstantiator::
539InstantiateTemplateTypeParmType(const TemplateTypeParmType *T,
540 unsigned Quals) const {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000541 if (T->getDepth() == 0) {
542 // Replace the template type parameter with its corresponding
543 // template argument.
Douglas Gregor99ebf652009-02-27 19:31:52 +0000544 assert(TemplateArgs[T->getIndex()].getKind() == TemplateArgument::Type &&
545 "Template argument kind mismatch");
546 QualType Result = TemplateArgs[T->getIndex()].getAsType();
Douglas Gregorcd281c32009-02-28 00:25:32 +0000547 if (Result.isNull() || !Quals)
Douglas Gregor99ebf652009-02-27 19:31:52 +0000548 return Result;
549
550 // C++ [dcl.ref]p1:
551 // [...] Cv-qualified references are ill-formed except when
552 // the cv-qualifiers are introduced through the use of a
553 // typedef (7.1.3) or of a template type argument (14.3), in
554 // which case the cv-qualifiers are ignored.
Douglas Gregorcd281c32009-02-28 00:25:32 +0000555 if (Quals && Result->isReferenceType())
556 Quals = 0;
Douglas Gregor99ebf652009-02-27 19:31:52 +0000557
Douglas Gregorcd281c32009-02-28 00:25:32 +0000558 return QualType(Result.getTypePtr(), Quals | Result.getCVRQualifiers());
Douglas Gregor99ebf652009-02-27 19:31:52 +0000559 }
560
561 // The template type parameter comes from an inner template (e.g.,
562 // the template parameter list of a member template inside the
563 // template we are instantiating). Create a new template type
564 // parameter with the template "level" reduced by one.
565 return SemaRef.Context.getTemplateTypeParmType(T->getDepth() - 1,
566 T->getIndex(),
Anders Carlsson76e4ce42009-06-16 00:30:48 +0000567 T->isParameterPack(),
Douglas Gregorcd281c32009-02-28 00:25:32 +0000568 T->getName())
569 .getQualifiedType(Quals);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000570}
571
Douglas Gregorcd281c32009-02-28 00:25:32 +0000572QualType
573TemplateTypeInstantiator::
Douglas Gregor7532dc62009-03-30 22:58:21 +0000574InstantiateTemplateSpecializationType(
575 const TemplateSpecializationType *T,
Douglas Gregorcd281c32009-02-28 00:25:32 +0000576 unsigned Quals) const {
Douglas Gregor0ca20ac2009-05-29 18:27:38 +0000577 llvm::SmallVector<TemplateArgument, 4> InstantiatedTemplateArgs;
Douglas Gregor40808ce2009-03-09 23:48:35 +0000578 InstantiatedTemplateArgs.reserve(T->getNumArgs());
Douglas Gregor7532dc62009-03-30 22:58:21 +0000579 for (TemplateSpecializationType::iterator Arg = T->begin(), ArgEnd = T->end();
Douglas Gregor40808ce2009-03-09 23:48:35 +0000580 Arg != ArgEnd; ++Arg) {
Douglas Gregor91333002009-06-11 00:06:24 +0000581 TemplateArgument InstArg = SemaRef.Instantiate(*Arg, TemplateArgs);
582 if (InstArg.isNull())
583 return QualType();
Douglas Gregor40808ce2009-03-09 23:48:35 +0000584
Douglas Gregor91333002009-06-11 00:06:24 +0000585 InstantiatedTemplateArgs.push_back(InstArg);
Douglas Gregor40808ce2009-03-09 23:48:35 +0000586 }
587
Mike Stump390b4cc2009-05-16 07:39:55 +0000588 // FIXME: We're missing the locations of the template name, '<', and '>'.
Douglas Gregorde650ae2009-03-31 18:38:02 +0000589
590 TemplateName Name = SemaRef.InstantiateTemplateName(T->getTemplateName(),
591 Loc,
Douglas Gregor7e063902009-05-11 23:53:27 +0000592 TemplateArgs);
Douglas Gregorde650ae2009-03-31 18:38:02 +0000593
594 return SemaRef.CheckTemplateIdType(Name, Loc, SourceLocation(),
Douglas Gregor02cbbd22009-06-11 18:10:32 +0000595 InstantiatedTemplateArgs.data(),
Douglas Gregor7532dc62009-03-30 22:58:21 +0000596 InstantiatedTemplateArgs.size(),
597 SourceLocation());
Douglas Gregor99ebf652009-02-27 19:31:52 +0000598}
599
Douglas Gregorcd281c32009-02-28 00:25:32 +0000600QualType
601TemplateTypeInstantiator::
Douglas Gregore4e5b052009-03-19 00:18:19 +0000602InstantiateQualifiedNameType(const QualifiedNameType *T,
603 unsigned Quals) const {
Douglas Gregord57959a2009-03-27 23:10:48 +0000604 // When we instantiated a qualified name type, there's no point in
605 // keeping the qualification around in the instantiated result. So,
606 // just instantiate the named type.
607 return (*this)(T->getNamedType());
608}
609
610QualType
611TemplateTypeInstantiator::
612InstantiateTypenameType(const TypenameType *T, unsigned Quals) const {
Douglas Gregor17343172009-04-01 00:28:59 +0000613 if (const TemplateSpecializationType *TemplateId = T->getTemplateId()) {
614 // When the typename type refers to a template-id, the template-id
615 // is dependent and has enough information to instantiate the
616 // result of the typename type. Since we don't care about keeping
617 // the spelling of the typename type in template instantiations,
618 // we just instantiate the template-id.
619 return InstantiateTemplateSpecializationType(TemplateId, Quals);
620 }
621
Douglas Gregord57959a2009-03-27 23:10:48 +0000622 NestedNameSpecifier *NNS
623 = SemaRef.InstantiateNestedNameSpecifier(T->getQualifier(),
624 SourceRange(Loc),
Douglas Gregor7e063902009-05-11 23:53:27 +0000625 TemplateArgs);
Douglas Gregord57959a2009-03-27 23:10:48 +0000626 if (!NNS)
627 return QualType();
628
Douglas Gregor17343172009-04-01 00:28:59 +0000629 return SemaRef.CheckTypenameType(NNS, *T->getIdentifier(), SourceRange(Loc));
Douglas Gregore4e5b052009-03-19 00:18:19 +0000630}
631
632QualType
633TemplateTypeInstantiator::
Douglas Gregorcd281c32009-02-28 00:25:32 +0000634InstantiateObjCInterfaceType(const ObjCInterfaceType *T,
635 unsigned Quals) const {
636 assert(false && "Objective-C types cannot be dependent");
Douglas Gregor99ebf652009-02-27 19:31:52 +0000637 return QualType();
638}
639
Douglas Gregorcd281c32009-02-28 00:25:32 +0000640QualType
641TemplateTypeInstantiator::
642InstantiateObjCQualifiedInterfaceType(const ObjCQualifiedInterfaceType *T,
643 unsigned Quals) const {
644 assert(false && "Objective-C types cannot be dependent");
Douglas Gregor99ebf652009-02-27 19:31:52 +0000645 return QualType();
646}
647
Douglas Gregorcd281c32009-02-28 00:25:32 +0000648QualType
649TemplateTypeInstantiator::
650InstantiateObjCQualifiedIdType(const ObjCQualifiedIdType *T,
651 unsigned Quals) const {
652 assert(false && "Objective-C types cannot be dependent");
Douglas Gregor99ebf652009-02-27 19:31:52 +0000653 return QualType();
654}
655
Douglas Gregorcd281c32009-02-28 00:25:32 +0000656/// \brief The actual implementation of Sema::InstantiateType().
657QualType TemplateTypeInstantiator::Instantiate(QualType T) const {
658 // If T is not a dependent type, there is nothing to do.
659 if (!T->isDependentType())
660 return T;
661
662 switch (T->getTypeClass()) {
663#define TYPE(Class, Base) \
664 case Type::Class: \
665 return Instantiate##Class##Type(cast<Class##Type>(T.getTypePtr()), \
666 T.getCVRQualifiers());
667#define ABSTRACT_TYPE(Class, Base)
668#include "clang/AST/TypeNodes.def"
669 }
670
671 assert(false && "Not all types have been decoded for instantiation");
672 return QualType();
673}
Douglas Gregor99ebf652009-02-27 19:31:52 +0000674
675/// \brief Instantiate the type T with a given set of template arguments.
676///
677/// This routine substitutes the given template arguments into the
678/// type T and produces the instantiated type.
679///
680/// \param T the type into which the template arguments will be
681/// substituted. If this type is not dependent, it will be returned
682/// immediately.
683///
684/// \param TemplateArgs the template arguments that will be
685/// substituted for the top-level template parameters within T.
686///
Douglas Gregor99ebf652009-02-27 19:31:52 +0000687/// \param Loc the location in the source code where this substitution
688/// is being performed. It will typically be the location of the
689/// declarator (if we're instantiating the type of some declaration)
690/// or the location of the type in the source code (if, e.g., we're
691/// instantiating the type of a cast expression).
692///
693/// \param Entity the name of the entity associated with a declaration
694/// being instantiated (if any). May be empty to indicate that there
695/// is no such entity (if, e.g., this is a type that occurs as part of
696/// a cast expression) or that the entity has no name (e.g., an
697/// unnamed function parameter).
698///
699/// \returns If the instantiation succeeds, the instantiated
700/// type. Otherwise, produces diagnostics and returns a NULL type.
701QualType Sema::InstantiateType(QualType T,
Douglas Gregor7e063902009-05-11 23:53:27 +0000702 const TemplateArgumentList &TemplateArgs,
Douglas Gregor99ebf652009-02-27 19:31:52 +0000703 SourceLocation Loc, DeclarationName Entity) {
Douglas Gregordf667e72009-03-10 20:44:00 +0000704 assert(!ActiveTemplateInstantiations.empty() &&
705 "Cannot perform an instantiation without some context on the "
706 "instantiation stack");
707
Douglas Gregor99ebf652009-02-27 19:31:52 +0000708 // If T is not a dependent type, there is nothing to do.
709 if (!T->isDependentType())
710 return T;
711
Douglas Gregor7e063902009-05-11 23:53:27 +0000712 TemplateTypeInstantiator Instantiator(*this, TemplateArgs, Loc, Entity);
Douglas Gregorcd281c32009-02-28 00:25:32 +0000713 return Instantiator(T);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000714}
Douglas Gregor2943aed2009-03-03 04:44:36 +0000715
716/// \brief Instantiate the base class specifiers of the given class
717/// template specialization.
718///
719/// Produces a diagnostic and returns true on error, returns false and
720/// attaches the instantiated base classes to the class template
721/// specialization if successful.
722bool
Douglas Gregord475b8d2009-03-25 21:17:03 +0000723Sema::InstantiateBaseSpecifiers(CXXRecordDecl *Instantiation,
724 CXXRecordDecl *Pattern,
Douglas Gregor7e063902009-05-11 23:53:27 +0000725 const TemplateArgumentList &TemplateArgs) {
Douglas Gregor2943aed2009-03-03 04:44:36 +0000726 bool Invalid = false;
Douglas Gregor0ca20ac2009-05-29 18:27:38 +0000727 llvm::SmallVector<CXXBaseSpecifier*, 4> InstantiatedBases;
Douglas Gregord475b8d2009-03-25 21:17:03 +0000728 for (ClassTemplateSpecializationDecl::base_class_iterator
729 Base = Pattern->bases_begin(), BaseEnd = Pattern->bases_end();
Douglas Gregor27b152f2009-03-10 18:52:44 +0000730 Base != BaseEnd; ++Base) {
Douglas Gregor2943aed2009-03-03 04:44:36 +0000731 if (!Base->getType()->isDependentType()) {
732 // FIXME: Allocate via ASTContext
733 InstantiatedBases.push_back(new CXXBaseSpecifier(*Base));
734 continue;
735 }
736
737 QualType BaseType = InstantiateType(Base->getType(),
Douglas Gregor7e063902009-05-11 23:53:27 +0000738 TemplateArgs,
Douglas Gregor2943aed2009-03-03 04:44:36 +0000739 Base->getSourceRange().getBegin(),
740 DeclarationName());
741 if (BaseType.isNull()) {
742 Invalid = true;
743 continue;
744 }
745
746 if (CXXBaseSpecifier *InstantiatedBase
Douglas Gregord475b8d2009-03-25 21:17:03 +0000747 = CheckBaseSpecifier(Instantiation,
Douglas Gregor2943aed2009-03-03 04:44:36 +0000748 Base->getSourceRange(),
749 Base->isVirtual(),
750 Base->getAccessSpecifierAsWritten(),
751 BaseType,
752 /*FIXME: Not totally accurate */
753 Base->getSourceRange().getBegin()))
754 InstantiatedBases.push_back(InstantiatedBase);
755 else
756 Invalid = true;
757 }
758
Douglas Gregor27b152f2009-03-10 18:52:44 +0000759 if (!Invalid &&
Jay Foadbeaaccd2009-05-21 09:52:38 +0000760 AttachBaseSpecifiers(Instantiation, InstantiatedBases.data(),
Douglas Gregor2943aed2009-03-03 04:44:36 +0000761 InstantiatedBases.size()))
762 Invalid = true;
763
764 return Invalid;
765}
766
Douglas Gregord475b8d2009-03-25 21:17:03 +0000767/// \brief Instantiate the definition of a class from a given pattern.
768///
769/// \param PointOfInstantiation The point of instantiation within the
770/// source code.
771///
772/// \param Instantiation is the declaration whose definition is being
773/// instantiated. This will be either a class template specialization
774/// or a member class of a class template specialization.
775///
776/// \param Pattern is the pattern from which the instantiation
777/// occurs. This will be either the declaration of a class template or
778/// the declaration of a member class of a class template.
779///
780/// \param TemplateArgs The template arguments to be substituted into
781/// the pattern.
782///
Douglas Gregord475b8d2009-03-25 21:17:03 +0000783/// \returns true if an error occurred, false otherwise.
784bool
785Sema::InstantiateClass(SourceLocation PointOfInstantiation,
786 CXXRecordDecl *Instantiation, CXXRecordDecl *Pattern,
Douglas Gregor93dfdb12009-05-13 00:25:59 +0000787 const TemplateArgumentList &TemplateArgs,
788 bool ExplicitInstantiation) {
Douglas Gregord475b8d2009-03-25 21:17:03 +0000789 bool Invalid = false;
790
791 CXXRecordDecl *PatternDef
792 = cast_or_null<CXXRecordDecl>(Pattern->getDefinition(Context));
793 if (!PatternDef) {
794 if (Pattern == Instantiation->getInstantiatedFromMemberClass()) {
795 Diag(PointOfInstantiation,
796 diag::err_implicit_instantiate_member_undefined)
797 << Context.getTypeDeclType(Instantiation);
798 Diag(Pattern->getLocation(), diag::note_member_of_template_here);
799 } else {
Douglas Gregor93dfdb12009-05-13 00:25:59 +0000800 Diag(PointOfInstantiation, diag::err_template_instantiate_undefined)
801 << ExplicitInstantiation
Douglas Gregord475b8d2009-03-25 21:17:03 +0000802 << Context.getTypeDeclType(Instantiation);
803 Diag(Pattern->getLocation(), diag::note_template_decl_here);
804 }
805 return true;
806 }
807 Pattern = PatternDef;
808
Douglas Gregord048bb72009-03-25 21:23:52 +0000809 InstantiatingTemplate Inst(*this, PointOfInstantiation, Instantiation);
Douglas Gregord475b8d2009-03-25 21:17:03 +0000810 if (Inst)
811 return true;
812
813 // Enter the scope of this instantiation. We don't use
814 // PushDeclContext because we don't have a scope.
815 DeclContext *PreviousContext = CurContext;
816 CurContext = Instantiation;
817
818 // Start the definition of this instantiation.
819 Instantiation->startDefinition();
820
821 // Instantiate the base class specifiers.
Douglas Gregor7e063902009-05-11 23:53:27 +0000822 if (InstantiateBaseSpecifiers(Instantiation, Pattern, TemplateArgs))
Douglas Gregord475b8d2009-03-25 21:17:03 +0000823 Invalid = true;
824
Douglas Gregor0ca20ac2009-05-29 18:27:38 +0000825 llvm::SmallVector<DeclPtrTy, 4> Fields;
Douglas Gregor6ab35242009-04-09 21:40:53 +0000826 for (RecordDecl::decl_iterator Member = Pattern->decls_begin(Context),
827 MemberEnd = Pattern->decls_end(Context);
828 Member != MemberEnd; ++Member) {
Douglas Gregor7e063902009-05-11 23:53:27 +0000829 Decl *NewMember = InstantiateDecl(*Member, Instantiation, TemplateArgs);
Douglas Gregord475b8d2009-03-25 21:17:03 +0000830 if (NewMember) {
831 if (NewMember->isInvalidDecl())
832 Invalid = true;
833 else if (FieldDecl *Field = dyn_cast<FieldDecl>(NewMember))
Chris Lattnerb28317a2009-03-28 19:18:32 +0000834 Fields.push_back(DeclPtrTy::make(Field));
Douglas Gregord475b8d2009-03-25 21:17:03 +0000835 } else {
836 // FIXME: Eventually, a NULL return will mean that one of the
Mike Stump390b4cc2009-05-16 07:39:55 +0000837 // instantiations was a semantic disaster, and we'll want to set Invalid =
838 // true. For now, we expect to skip some members that we can't yet handle.
Douglas Gregord475b8d2009-03-25 21:17:03 +0000839 }
840 }
841
842 // Finish checking fields.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000843 ActOnFields(0, Instantiation->getLocation(), DeclPtrTy::make(Instantiation),
Jay Foadbeaaccd2009-05-21 09:52:38 +0000844 Fields.data(), Fields.size(), SourceLocation(), SourceLocation(),
Douglas Gregord475b8d2009-03-25 21:17:03 +0000845 0);
846
847 // Add any implicitly-declared members that we might need.
848 AddImplicitlyDeclaredMembersToClass(Instantiation);
849
850 // Exit the scope of this instantiation.
851 CurContext = PreviousContext;
852
Douglas Gregoraba43bb2009-05-26 20:50:29 +0000853 if (!Invalid)
854 Consumer.HandleTagDeclDefinition(Instantiation);
855
Douglas Gregora58861f2009-05-13 20:28:22 +0000856 // If this is an explicit instantiation, instantiate our members, too.
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000857 if (!Invalid && ExplicitInstantiation) {
858 Inst.Clear();
Douglas Gregora58861f2009-05-13 20:28:22 +0000859 InstantiateClassMembers(PointOfInstantiation, Instantiation, TemplateArgs);
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000860 }
Douglas Gregora58861f2009-05-13 20:28:22 +0000861
Douglas Gregord475b8d2009-03-25 21:17:03 +0000862 return Invalid;
863}
864
Douglas Gregor2943aed2009-03-03 04:44:36 +0000865bool
866Sema::InstantiateClassTemplateSpecialization(
867 ClassTemplateSpecializationDecl *ClassTemplateSpec,
868 bool ExplicitInstantiation) {
869 // Perform the actual instantiation on the canonical declaration.
870 ClassTemplateSpec = cast<ClassTemplateSpecializationDecl>(
871 Context.getCanonicalDecl(ClassTemplateSpec));
872
873 // We can only instantiate something that hasn't already been
874 // instantiated or specialized. Fail without any diagnostics: our
875 // caller will provide an error message.
876 if (ClassTemplateSpec->getSpecializationKind() != TSK_Undeclared)
877 return true;
878
Douglas Gregor2943aed2009-03-03 04:44:36 +0000879 ClassTemplateDecl *Template = ClassTemplateSpec->getSpecializedTemplate();
Douglas Gregord475b8d2009-03-25 21:17:03 +0000880 CXXRecordDecl *Pattern = Template->getTemplatedDecl();
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000881 const TemplateArgumentList *TemplateArgs
882 = &ClassTemplateSpec->getTemplateArgs();
883
Douglas Gregorc1efb3f2009-06-12 22:31:52 +0000884 // C++ [temp.class.spec.match]p1:
885 // When a class template is used in a context that requires an
886 // instantiation of the class, it is necessary to determine
887 // whether the instantiation is to be generated using the primary
888 // template or one of the partial specializations. This is done by
889 // matching the template arguments of the class template
890 // specialization with the template argument lists of the partial
891 // specializations.
Douglas Gregor199d9912009-06-05 00:53:49 +0000892 typedef std::pair<ClassTemplatePartialSpecializationDecl *,
893 TemplateArgumentList *> MatchResult;
894 llvm::SmallVector<MatchResult, 4> Matched;
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000895 for (llvm::FoldingSet<ClassTemplatePartialSpecializationDecl>::iterator
896 Partial = Template->getPartialSpecializations().begin(),
897 PartialEnd = Template->getPartialSpecializations().end();
898 Partial != PartialEnd;
899 ++Partial) {
Douglas Gregorf67875d2009-06-12 18:26:56 +0000900 TemplateDeductionInfo Info(Context);
901 if (TemplateDeductionResult Result
Douglas Gregor199d9912009-06-05 00:53:49 +0000902 = DeduceTemplateArguments(&*Partial,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000903 ClassTemplateSpec->getTemplateArgs(),
904 Info)) {
905 // FIXME: Store the failed-deduction information for use in
906 // diagnostics, later.
907 (void)Result;
908 } else {
909 Matched.push_back(std::make_pair(&*Partial, Info.take()));
910 }
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000911 }
912
913 if (Matched.size() == 1) {
Douglas Gregorc1efb3f2009-06-12 22:31:52 +0000914 // -- If exactly one matching specialization is found, the
915 // instantiation is generated from that specialization.
Douglas Gregor199d9912009-06-05 00:53:49 +0000916 Pattern = Matched[0].first;
917 TemplateArgs = Matched[0].second;
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000918 } else if (Matched.size() > 1) {
Douglas Gregorc1efb3f2009-06-12 22:31:52 +0000919 // -- If more than one matching specialization is found, the
920 // partial order rules (14.5.4.2) are used to determine
921 // whether one of the specializations is more specialized
922 // than the others. If none of the specializations is more
923 // specialized than all of the other matching
924 // specializations, then the use of the class template is
925 // ambiguous and the program is ill-formed.
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000926 // FIXME: Implement partial ordering of class template partial
927 // specializations.
928 Diag(ClassTemplateSpec->getLocation(),
929 diag::unsup_template_partial_spec_ordering);
Douglas Gregorc1efb3f2009-06-12 22:31:52 +0000930 } else {
931 // -- If no matches are found, the instantiation is generated
932 // from the primary template.
933
934 // Since we initialized the pattern and template arguments from
935 // the primary template, there is nothing more we need to do here.
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000936 }
Douglas Gregor2943aed2009-03-03 04:44:36 +0000937
938 // Note that this is an instantiation.
939 ClassTemplateSpec->setSpecializationKind(
940 ExplicitInstantiation? TSK_ExplicitInstantiation
941 : TSK_ImplicitInstantiation);
942
Douglas Gregor199d9912009-06-05 00:53:49 +0000943 bool Result = InstantiateClass(ClassTemplateSpec->getLocation(),
944 ClassTemplateSpec, Pattern, *TemplateArgs,
945 ExplicitInstantiation);
946
947 for (unsigned I = 0, N = Matched.size(); I != N; ++I) {
948 // FIXME: Implement TemplateArgumentList::Destroy!
949 // if (Matched[I].first != Pattern)
950 // Matched[I].second->Destroy(Context);
951 }
952
953 return Result;
Douglas Gregor2943aed2009-03-03 04:44:36 +0000954}
Douglas Gregor5953d8b2009-03-19 17:26:29 +0000955
Douglas Gregora58861f2009-05-13 20:28:22 +0000956/// \brief Instantiate the definitions of all of the member of the
957/// given class, which is an instantiation of a class template or a
958/// member class of a template.
959void
960Sema::InstantiateClassMembers(SourceLocation PointOfInstantiation,
961 CXXRecordDecl *Instantiation,
962 const TemplateArgumentList &TemplateArgs) {
963 for (DeclContext::decl_iterator D = Instantiation->decls_begin(Context),
964 DEnd = Instantiation->decls_end(Context);
965 D != DEnd; ++D) {
966 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(*D)) {
967 if (!Function->getBody(Context))
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000968 InstantiateFunctionDefinition(PointOfInstantiation, Function);
Douglas Gregora58861f2009-05-13 20:28:22 +0000969 } else if (VarDecl *Var = dyn_cast<VarDecl>(*D)) {
970 const VarDecl *Def = 0;
971 if (!Var->getDefinition(Def))
972 InstantiateVariableDefinition(Var);
973 } else if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(*D)) {
974 if (!Record->isInjectedClassName() && !Record->getDefinition(Context)) {
975 assert(Record->getInstantiatedFromMemberClass() &&
976 "Missing instantiated-from-template information");
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000977 InstantiateClass(PointOfInstantiation, Record,
Douglas Gregora58861f2009-05-13 20:28:22 +0000978 Record->getInstantiatedFromMemberClass(),
979 TemplateArgs, true);
980 }
981 }
982 }
983}
984
985/// \brief Instantiate the definitions of all of the members of the
986/// given class template specialization, which was named as part of an
987/// explicit instantiation.
988void Sema::InstantiateClassTemplateSpecializationMembers(
989 SourceLocation PointOfInstantiation,
990 ClassTemplateSpecializationDecl *ClassTemplateSpec) {
991 // C++0x [temp.explicit]p7:
992 // An explicit instantiation that names a class template
993 // specialization is an explicit instantion of the same kind
994 // (declaration or definition) of each of its members (not
995 // including members inherited from base classes) that has not
996 // been previously explicitly specialized in the translation unit
997 // containing the explicit instantiation, except as described
998 // below.
999 InstantiateClassMembers(PointOfInstantiation, ClassTemplateSpec,
1000 ClassTemplateSpec->getTemplateArgs());
1001}
1002
Douglas Gregorab452ba2009-03-26 23:50:42 +00001003/// \brief Instantiate a nested-name-specifier.
1004NestedNameSpecifier *
1005Sema::InstantiateNestedNameSpecifier(NestedNameSpecifier *NNS,
1006 SourceRange Range,
Douglas Gregor7e063902009-05-11 23:53:27 +00001007 const TemplateArgumentList &TemplateArgs) {
Douglas Gregorab452ba2009-03-26 23:50:42 +00001008 // Instantiate the prefix of this nested name specifier.
1009 NestedNameSpecifier *Prefix = NNS->getPrefix();
1010 if (Prefix) {
Douglas Gregor7e063902009-05-11 23:53:27 +00001011 Prefix = InstantiateNestedNameSpecifier(Prefix, Range, TemplateArgs);
Douglas Gregorab452ba2009-03-26 23:50:42 +00001012 if (!Prefix)
1013 return 0;
Douglas Gregor5953d8b2009-03-19 17:26:29 +00001014 }
1015
Douglas Gregorab452ba2009-03-26 23:50:42 +00001016 switch (NNS->getKind()) {
Douglas Gregor17343172009-04-01 00:28:59 +00001017 case NestedNameSpecifier::Identifier: {
1018 assert(Prefix &&
1019 "Can't have an identifier nested-name-specifier with no prefix");
1020 CXXScopeSpec SS;
1021 // FIXME: The source location information is all wrong.
1022 SS.setRange(Range);
1023 SS.setScopeRep(Prefix);
1024 return static_cast<NestedNameSpecifier *>(
1025 ActOnCXXNestedNameSpecifier(0, SS,
1026 Range.getEnd(),
1027 Range.getEnd(),
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001028 *NNS->getAsIdentifier()));
Douglas Gregorab452ba2009-03-26 23:50:42 +00001029 break;
Douglas Gregor17343172009-04-01 00:28:59 +00001030 }
Douglas Gregorab452ba2009-03-26 23:50:42 +00001031
1032 case NestedNameSpecifier::Namespace:
1033 case NestedNameSpecifier::Global:
1034 return NNS;
1035
1036 case NestedNameSpecifier::TypeSpecWithTemplate:
1037 case NestedNameSpecifier::TypeSpec: {
1038 QualType T = QualType(NNS->getAsType(), 0);
1039 if (!T->isDependentType())
1040 return NNS;
1041
Douglas Gregor7e063902009-05-11 23:53:27 +00001042 T = InstantiateType(T, TemplateArgs, Range.getBegin(), DeclarationName());
Douglas Gregorab452ba2009-03-26 23:50:42 +00001043 if (T.isNull())
1044 return 0;
1045
Eli Friedman923f7532009-06-13 04:51:30 +00001046 if (T->isDependentType() || T->isRecordType() ||
Douglas Gregord57959a2009-03-27 23:10:48 +00001047 (getLangOptions().CPlusPlus0x && T->isEnumeralType())) {
Douglas Gregor17343172009-04-01 00:28:59 +00001048 assert(T.getCVRQualifiers() == 0 && "Can't get cv-qualifiers here");
Douglas Gregord57959a2009-03-27 23:10:48 +00001049 return NestedNameSpecifier::Create(Context, Prefix,
Douglas Gregorab452ba2009-03-26 23:50:42 +00001050 NNS->getKind() == NestedNameSpecifier::TypeSpecWithTemplate,
Douglas Gregord57959a2009-03-27 23:10:48 +00001051 T.getTypePtr());
1052 }
1053
1054 Diag(Range.getBegin(), diag::err_nested_name_spec_non_tag) << T;
1055 return 0;
Douglas Gregorab452ba2009-03-26 23:50:42 +00001056 }
1057 }
1058
Douglas Gregord57959a2009-03-27 23:10:48 +00001059 // Required to silence a GCC warning
Douglas Gregorab452ba2009-03-26 23:50:42 +00001060 return 0;
Douglas Gregor5953d8b2009-03-19 17:26:29 +00001061}
Douglas Gregorde650ae2009-03-31 18:38:02 +00001062
1063TemplateName
1064Sema::InstantiateTemplateName(TemplateName Name, SourceLocation Loc,
Douglas Gregor7e063902009-05-11 23:53:27 +00001065 const TemplateArgumentList &TemplateArgs) {
Douglas Gregorde650ae2009-03-31 18:38:02 +00001066 if (TemplateTemplateParmDecl *TTP
1067 = dyn_cast_or_null<TemplateTemplateParmDecl>(
1068 Name.getAsTemplateDecl())) {
1069 assert(TTP->getDepth() == 0 &&
1070 "Cannot reduce depth of a template template parameter");
Douglas Gregor9bde7732009-03-31 20:22:05 +00001071 assert(TemplateArgs[TTP->getPosition()].getAsDecl() &&
Douglas Gregorde650ae2009-03-31 18:38:02 +00001072 "Wrong kind of template template argument");
1073 ClassTemplateDecl *ClassTemplate
1074 = dyn_cast<ClassTemplateDecl>(
1075 TemplateArgs[TTP->getPosition()].getAsDecl());
Douglas Gregor9bde7732009-03-31 20:22:05 +00001076 assert(ClassTemplate && "Expected a class template");
Douglas Gregorde650ae2009-03-31 18:38:02 +00001077 if (QualifiedTemplateName *QTN = Name.getAsQualifiedTemplateName()) {
1078 NestedNameSpecifier *NNS
1079 = InstantiateNestedNameSpecifier(QTN->getQualifier(),
1080 /*FIXME=*/SourceRange(Loc),
Douglas Gregor7e063902009-05-11 23:53:27 +00001081 TemplateArgs);
Douglas Gregorde650ae2009-03-31 18:38:02 +00001082 if (NNS)
1083 return Context.getQualifiedTemplateName(NNS,
1084 QTN->hasTemplateKeyword(),
1085 ClassTemplate);
1086 }
1087
1088 return TemplateName(ClassTemplate);
1089 } else if (DependentTemplateName *DTN = Name.getAsDependentTemplateName()) {
1090 NestedNameSpecifier *NNS
1091 = InstantiateNestedNameSpecifier(DTN->getQualifier(),
1092 /*FIXME=*/SourceRange(Loc),
Douglas Gregor7e063902009-05-11 23:53:27 +00001093 TemplateArgs);
Douglas Gregorde650ae2009-03-31 18:38:02 +00001094
1095 if (!NNS) // FIXME: Not the best recovery strategy.
1096 return Name;
1097
1098 if (NNS->isDependent())
1099 return Context.getDependentTemplateName(NNS, DTN->getName());
1100
1101 // Somewhat redundant with ActOnDependentTemplateName.
1102 CXXScopeSpec SS;
1103 SS.setRange(SourceRange(Loc));
1104 SS.setScopeRep(NNS);
1105 TemplateTy Template;
1106 TemplateNameKind TNK = isTemplateName(*DTN->getName(), 0, Template, &SS);
1107 if (TNK == TNK_Non_template) {
1108 Diag(Loc, diag::err_template_kw_refers_to_non_template)
1109 << DTN->getName();
1110 return Name;
1111 } else if (TNK == TNK_Function_template) {
1112 Diag(Loc, diag::err_template_kw_refers_to_non_template)
1113 << DTN->getName();
1114 return Name;
1115 }
1116
1117 return Template.getAsVal<TemplateName>();
1118 }
1119
1120
1121
Mike Stump390b4cc2009-05-16 07:39:55 +00001122 // FIXME: Even if we're referring to a Decl that isn't a template template
1123 // parameter, we may need to instantiate the outer contexts of that
1124 // Decl. However, this won't be needed until we implement member templates.
Douglas Gregorde650ae2009-03-31 18:38:02 +00001125 return Name;
1126}
Douglas Gregor91333002009-06-11 00:06:24 +00001127
1128TemplateArgument Sema::Instantiate(TemplateArgument Arg,
1129 const TemplateArgumentList &TemplateArgs) {
1130 switch (Arg.getKind()) {
1131 case TemplateArgument::Null:
1132 assert(false && "Should never have a NULL template argument");
1133 break;
1134
1135 case TemplateArgument::Type: {
1136 QualType T = InstantiateType(Arg.getAsType(), TemplateArgs,
1137 Arg.getLocation(), DeclarationName());
1138 if (T.isNull())
1139 return TemplateArgument();
1140
1141 return TemplateArgument(Arg.getLocation(), T);
1142 }
1143
1144 case TemplateArgument::Declaration:
1145 // FIXME: Template instantiation for template template parameters.
1146 return Arg;
1147
1148 case TemplateArgument::Integral:
1149 return Arg;
1150
1151 case TemplateArgument::Expression: {
1152 Sema::OwningExprResult E = InstantiateExpr(Arg.getAsExpr(), TemplateArgs);
1153 if (E.isInvalid())
1154 return TemplateArgument();
1155 return TemplateArgument(E.takeAs<Expr>());
1156 }
Anders Carlssond01b1da2009-06-15 17:04:53 +00001157
1158 case TemplateArgument::Pack:
1159 assert(0 && "FIXME: Implement!");
1160 break;
Douglas Gregor91333002009-06-11 00:06:24 +00001161 }
1162
1163 assert(false && "Unhandled template argument kind");
1164 return TemplateArgument();
1165}