blob: 4d03e79c656e1f45d3bb4257a34a70803059ef98 [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 Gregor99ebf652009-02-27 19:31:52 +0000202//===----------------------------------------------------------------------===/
203// Template Instantiation for Types
204//===----------------------------------------------------------------------===/
Douglas Gregorcd281c32009-02-28 00:25:32 +0000205namespace {
206 class VISIBILITY_HIDDEN TemplateTypeInstantiator {
207 Sema &SemaRef;
Douglas Gregor7e063902009-05-11 23:53:27 +0000208 const TemplateArgumentList &TemplateArgs;
Douglas Gregorcd281c32009-02-28 00:25:32 +0000209 SourceLocation Loc;
210 DeclarationName Entity;
Douglas Gregor99ebf652009-02-27 19:31:52 +0000211
Douglas Gregorcd281c32009-02-28 00:25:32 +0000212 public:
213 TemplateTypeInstantiator(Sema &SemaRef,
Douglas Gregor7e063902009-05-11 23:53:27 +0000214 const TemplateArgumentList &TemplateArgs,
Douglas Gregorcd281c32009-02-28 00:25:32 +0000215 SourceLocation Loc,
216 DeclarationName Entity)
217 : SemaRef(SemaRef), TemplateArgs(TemplateArgs),
Douglas Gregor7e063902009-05-11 23:53:27 +0000218 Loc(Loc), Entity(Entity) { }
Douglas Gregorcd281c32009-02-28 00:25:32 +0000219
220 QualType operator()(QualType T) const { return Instantiate(T); }
221
222 QualType Instantiate(QualType T) const;
223
224 // Declare instantiate functions for each type.
225#define TYPE(Class, Base) \
226 QualType Instantiate##Class##Type(const Class##Type *T, \
227 unsigned Quals) const;
228#define ABSTRACT_TYPE(Class, Base)
229#include "clang/AST/TypeNodes.def"
230 };
231}
232
233QualType
234TemplateTypeInstantiator::InstantiateExtQualType(const ExtQualType *T,
235 unsigned Quals) const {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000236 // FIXME: Implement this
237 assert(false && "Cannot instantiate ExtQualType yet");
238 return QualType();
239}
240
Douglas Gregorcd281c32009-02-28 00:25:32 +0000241QualType
242TemplateTypeInstantiator::InstantiateBuiltinType(const BuiltinType *T,
243 unsigned Quals) const {
Douglas Gregor724651c2009-02-28 01:04:19 +0000244 assert(false && "Builtin types are not dependent and cannot be instantiated");
Douglas Gregorcd281c32009-02-28 00:25:32 +0000245 return QualType(T, Quals);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000246}
247
Douglas Gregorcd281c32009-02-28 00:25:32 +0000248QualType
249TemplateTypeInstantiator::
250InstantiateFixedWidthIntType(const FixedWidthIntType *T, unsigned Quals) const {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000251 // FIXME: Implement this
252 assert(false && "Cannot instantiate FixedWidthIntType yet");
253 return QualType();
254}
255
Douglas Gregorcd281c32009-02-28 00:25:32 +0000256QualType
257TemplateTypeInstantiator::InstantiateComplexType(const ComplexType *T,
258 unsigned Quals) const {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000259 // FIXME: Implement this
260 assert(false && "Cannot instantiate ComplexType yet");
261 return QualType();
262}
263
Douglas Gregorcd281c32009-02-28 00:25:32 +0000264QualType
265TemplateTypeInstantiator::InstantiatePointerType(const PointerType *T,
266 unsigned Quals) const {
267 QualType PointeeType = Instantiate(T->getPointeeType());
268 if (PointeeType.isNull())
269 return QualType();
270
271 return SemaRef.BuildPointerType(PointeeType, Quals, Loc, Entity);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000272}
273
Douglas Gregorcd281c32009-02-28 00:25:32 +0000274QualType
275TemplateTypeInstantiator::InstantiateBlockPointerType(const BlockPointerType *T,
276 unsigned Quals) const {
Anders Carlsson859ba502009-06-12 16:23:10 +0000277 QualType PointeeType = Instantiate(T->getPointeeType());
278 if (PointeeType.isNull())
279 return QualType();
280
Anders Carlsson9a917e42009-06-12 22:56:54 +0000281 return SemaRef.BuildBlockPointerType(PointeeType, Quals, Loc, Entity);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000282}
283
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000284QualType
285TemplateTypeInstantiator::InstantiateLValueReferenceType(
286 const LValueReferenceType *T, unsigned Quals) const {
Douglas Gregorcd281c32009-02-28 00:25:32 +0000287 QualType ReferentType = Instantiate(T->getPointeeType());
288 if (ReferentType.isNull())
289 return QualType();
290
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000291 return SemaRef.BuildReferenceType(ReferentType, true, Quals, Loc, Entity);
292}
293
294QualType
295TemplateTypeInstantiator::InstantiateRValueReferenceType(
296 const RValueReferenceType *T, unsigned Quals) const {
297 QualType ReferentType = Instantiate(T->getPointeeType());
298 if (ReferentType.isNull())
299 return QualType();
300
301 return SemaRef.BuildReferenceType(ReferentType, false, Quals, Loc, Entity);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000302}
303
Douglas Gregorcd281c32009-02-28 00:25:32 +0000304QualType
305TemplateTypeInstantiator::
306InstantiateMemberPointerType(const MemberPointerType *T,
307 unsigned Quals) const {
Douglas Gregor949bf692009-06-09 22:17:39 +0000308 QualType PointeeType = Instantiate(T->getPointeeType());
309 if (PointeeType.isNull())
310 return QualType();
311
312 QualType ClassType = Instantiate(QualType(T->getClass(), 0));
313 if (ClassType.isNull())
314 return QualType();
315
316 return SemaRef.BuildMemberPointerType(PointeeType, ClassType, Quals, Loc,
317 Entity);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000318}
319
Douglas Gregorcd281c32009-02-28 00:25:32 +0000320QualType
321TemplateTypeInstantiator::
322InstantiateConstantArrayType(const ConstantArrayType *T,
323 unsigned Quals) const {
324 QualType ElementType = Instantiate(T->getElementType());
325 if (ElementType.isNull())
326 return ElementType;
327
328 // Build a temporary integer literal to specify the size for
329 // BuildArrayType. Since we have already checked the size as part of
330 // creating the dependent array type in the first place, we know
Douglas Gregorff668032009-05-13 18:28:20 +0000331 // there aren't any errors. However, we do need to determine what
332 // C++ type to give the size expression.
333 llvm::APInt Size = T->getSize();
334 QualType Types[] = {
335 SemaRef.Context.UnsignedCharTy, SemaRef.Context.UnsignedShortTy,
336 SemaRef.Context.UnsignedIntTy, SemaRef.Context.UnsignedLongTy,
337 SemaRef.Context.UnsignedLongLongTy, SemaRef.Context.UnsignedInt128Ty
338 };
339 const unsigned NumTypes = sizeof(Types) / sizeof(QualType);
340 QualType SizeType;
341 for (unsigned I = 0; I != NumTypes; ++I)
342 if (Size.getBitWidth() == SemaRef.Context.getIntWidth(Types[I])) {
343 SizeType = Types[I];
344 break;
345 }
346
347 if (SizeType.isNull())
348 SizeType = SemaRef.Context.getFixedWidthIntType(Size.getBitWidth(), false);
349
350 IntegerLiteral ArraySize(Size, SizeType, Loc);
Douglas Gregorcd281c32009-02-28 00:25:32 +0000351 return SemaRef.BuildArrayType(ElementType, T->getSizeModifier(),
352 &ArraySize, T->getIndexTypeQualifier(),
353 Loc, Entity);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000354}
355
Douglas Gregorcd281c32009-02-28 00:25:32 +0000356QualType
357TemplateTypeInstantiator::
358InstantiateIncompleteArrayType(const IncompleteArrayType *T,
359 unsigned Quals) const {
360 QualType ElementType = Instantiate(T->getElementType());
361 if (ElementType.isNull())
362 return ElementType;
363
364 return SemaRef.BuildArrayType(ElementType, T->getSizeModifier(),
365 0, T->getIndexTypeQualifier(),
366 Loc, Entity);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000367}
368
Douglas Gregorcd281c32009-02-28 00:25:32 +0000369QualType
370TemplateTypeInstantiator::
371InstantiateVariableArrayType(const VariableArrayType *T,
372 unsigned Quals) const {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000373 // FIXME: Implement this
374 assert(false && "Cannot instantiate VariableArrayType yet");
375 return QualType();
376}
377
Douglas Gregorcd281c32009-02-28 00:25:32 +0000378QualType
379TemplateTypeInstantiator::
380InstantiateDependentSizedArrayType(const DependentSizedArrayType *T,
381 unsigned Quals) const {
Anders Carlsson76b1c842009-03-15 20:12:13 +0000382 Expr *ArraySize = T->getSizeExpr();
383 assert(ArraySize->isValueDependent() &&
384 "dependent sized array types must have value dependent size expr");
385
386 // Instantiate the element type if needed
387 QualType ElementType = T->getElementType();
388 if (ElementType->isDependentType()) {
389 ElementType = Instantiate(ElementType);
390 if (ElementType.isNull())
391 return QualType();
392 }
393
394 // Instantiate the size expression
395 Sema::OwningExprResult InstantiatedArraySize =
Douglas Gregor7e063902009-05-11 23:53:27 +0000396 SemaRef.InstantiateExpr(ArraySize, TemplateArgs);
Anders Carlsson76b1c842009-03-15 20:12:13 +0000397 if (InstantiatedArraySize.isInvalid())
398 return QualType();
399
400 return SemaRef.BuildArrayType(ElementType, T->getSizeModifier(),
Anders Carlssone9146f22009-05-01 19:49:17 +0000401 InstantiatedArraySize.takeAs<Expr>(),
Anders Carlsson76b1c842009-03-15 20:12:13 +0000402 T->getIndexTypeQualifier(), Loc, Entity);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000403}
404
Douglas Gregorcd281c32009-02-28 00:25:32 +0000405QualType
406TemplateTypeInstantiator::InstantiateVectorType(const VectorType *T,
407 unsigned Quals) const {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000408 // FIXME: Implement this
409 assert(false && "Cannot instantiate VectorType yet");
410 return QualType();
411}
412
Douglas Gregorcd281c32009-02-28 00:25:32 +0000413QualType
414TemplateTypeInstantiator::InstantiateExtVectorType(const ExtVectorType *T,
415 unsigned Quals) const {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000416 // FIXME: Implement this
417 assert(false && "Cannot instantiate ExtVectorType yet");
418 return QualType();
419}
420
Douglas Gregorcd281c32009-02-28 00:25:32 +0000421QualType
422TemplateTypeInstantiator::
423InstantiateFunctionProtoType(const FunctionProtoType *T,
424 unsigned Quals) const {
Douglas Gregor724651c2009-02-28 01:04:19 +0000425 QualType ResultType = Instantiate(T->getResultType());
426 if (ResultType.isNull())
427 return ResultType;
428
Douglas Gregor0ca20ac2009-05-29 18:27:38 +0000429 llvm::SmallVector<QualType, 4> ParamTypes;
Douglas Gregor724651c2009-02-28 01:04:19 +0000430 for (FunctionProtoType::arg_type_iterator Param = T->arg_type_begin(),
431 ParamEnd = T->arg_type_end();
432 Param != ParamEnd; ++Param) {
433 QualType P = Instantiate(*Param);
434 if (P.isNull())
435 return P;
436
437 ParamTypes.push_back(P);
438 }
439
Douglas Gregor02cbbd22009-06-11 18:10:32 +0000440 return SemaRef.BuildFunctionType(ResultType, ParamTypes.data(),
Douglas Gregor724651c2009-02-28 01:04:19 +0000441 ParamTypes.size(),
442 T->isVariadic(), T->getTypeQuals(),
443 Loc, Entity);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000444}
445
Douglas Gregorcd281c32009-02-28 00:25:32 +0000446QualType
447TemplateTypeInstantiator::
448InstantiateFunctionNoProtoType(const FunctionNoProtoType *T,
449 unsigned Quals) const {
Douglas Gregor724651c2009-02-28 01:04:19 +0000450 assert(false && "Functions without prototypes cannot be dependent.");
Douglas Gregor99ebf652009-02-27 19:31:52 +0000451 return QualType();
452}
453
Douglas Gregorcd281c32009-02-28 00:25:32 +0000454QualType
455TemplateTypeInstantiator::InstantiateTypedefType(const TypedefType *T,
456 unsigned Quals) const {
Douglas Gregor815215d2009-05-27 05:35:12 +0000457 TypedefDecl *Typedef
Douglas Gregored961e72009-05-27 17:54:46 +0000458 = cast_or_null<TypedefDecl>(
459 SemaRef.InstantiateCurrentDeclRef(T->getDecl()));
Douglas Gregor815215d2009-05-27 05:35:12 +0000460 if (!Typedef)
461 return QualType();
462
463 return SemaRef.Context.getTypeDeclType(Typedef);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000464}
465
Douglas Gregorcd281c32009-02-28 00:25:32 +0000466QualType
467TemplateTypeInstantiator::InstantiateTypeOfExprType(const TypeOfExprType *T,
468 unsigned Quals) const {
Douglas Gregor5f8bd592009-05-26 22:09:24 +0000469 Sema::OwningExprResult E
470 = SemaRef.InstantiateExpr(T->getUnderlyingExpr(), TemplateArgs);
471 if (E.isInvalid())
472 return QualType();
473
474 return SemaRef.Context.getTypeOfExprType(E.takeAs<Expr>());
Douglas Gregor99ebf652009-02-27 19:31:52 +0000475}
476
Douglas Gregorcd281c32009-02-28 00:25:32 +0000477QualType
478TemplateTypeInstantiator::InstantiateTypeOfType(const TypeOfType *T,
479 unsigned Quals) const {
Douglas Gregor5f8bd592009-05-26 22:09:24 +0000480 QualType Underlying = Instantiate(T->getUnderlyingType());
481 if (Underlying.isNull())
482 return QualType();
483
484 return SemaRef.Context.getTypeOfType(Underlying);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000485}
486
Douglas Gregorcd281c32009-02-28 00:25:32 +0000487QualType
488TemplateTypeInstantiator::InstantiateRecordType(const RecordType *T,
489 unsigned Quals) const {
Douglas Gregor815215d2009-05-27 05:35:12 +0000490 RecordDecl *Record
Douglas Gregored961e72009-05-27 17:54:46 +0000491 = cast_or_null<RecordDecl>(SemaRef.InstantiateCurrentDeclRef(T->getDecl()));
Douglas Gregor815215d2009-05-27 05:35:12 +0000492 if (!Record)
493 return QualType();
494
495 return SemaRef.Context.getTypeDeclType(Record);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000496}
497
Douglas Gregorcd281c32009-02-28 00:25:32 +0000498QualType
Douglas Gregorcd281c32009-02-28 00:25:32 +0000499TemplateTypeInstantiator::InstantiateEnumType(const EnumType *T,
500 unsigned Quals) const {
Douglas Gregor815215d2009-05-27 05:35:12 +0000501 EnumDecl *Enum
Douglas Gregored961e72009-05-27 17:54:46 +0000502 = cast_or_null<EnumDecl>(SemaRef.InstantiateCurrentDeclRef(T->getDecl()));
Douglas Gregor815215d2009-05-27 05:35:12 +0000503 if (!Enum)
504 return QualType();
505
506 return SemaRef.Context.getTypeDeclType(Enum);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000507}
508
Douglas Gregorcd281c32009-02-28 00:25:32 +0000509QualType
510TemplateTypeInstantiator::
511InstantiateTemplateTypeParmType(const TemplateTypeParmType *T,
512 unsigned Quals) const {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000513 if (T->getDepth() == 0) {
514 // Replace the template type parameter with its corresponding
515 // template argument.
Douglas Gregor99ebf652009-02-27 19:31:52 +0000516 assert(TemplateArgs[T->getIndex()].getKind() == TemplateArgument::Type &&
517 "Template argument kind mismatch");
518 QualType Result = TemplateArgs[T->getIndex()].getAsType();
Douglas Gregorcd281c32009-02-28 00:25:32 +0000519 if (Result.isNull() || !Quals)
Douglas Gregor99ebf652009-02-27 19:31:52 +0000520 return Result;
521
522 // C++ [dcl.ref]p1:
523 // [...] Cv-qualified references are ill-formed except when
524 // the cv-qualifiers are introduced through the use of a
525 // typedef (7.1.3) or of a template type argument (14.3), in
526 // which case the cv-qualifiers are ignored.
Douglas Gregorcd281c32009-02-28 00:25:32 +0000527 if (Quals && Result->isReferenceType())
528 Quals = 0;
Douglas Gregor99ebf652009-02-27 19:31:52 +0000529
Douglas Gregorcd281c32009-02-28 00:25:32 +0000530 return QualType(Result.getTypePtr(), Quals | Result.getCVRQualifiers());
Douglas Gregor99ebf652009-02-27 19:31:52 +0000531 }
532
533 // The template type parameter comes from an inner template (e.g.,
534 // the template parameter list of a member template inside the
535 // template we are instantiating). Create a new template type
536 // parameter with the template "level" reduced by one.
537 return SemaRef.Context.getTemplateTypeParmType(T->getDepth() - 1,
538 T->getIndex(),
Douglas Gregorcd281c32009-02-28 00:25:32 +0000539 T->getName())
540 .getQualifiedType(Quals);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000541}
542
Douglas Gregorcd281c32009-02-28 00:25:32 +0000543QualType
544TemplateTypeInstantiator::
Douglas Gregor7532dc62009-03-30 22:58:21 +0000545InstantiateTemplateSpecializationType(
546 const TemplateSpecializationType *T,
Douglas Gregorcd281c32009-02-28 00:25:32 +0000547 unsigned Quals) const {
Douglas Gregor0ca20ac2009-05-29 18:27:38 +0000548 llvm::SmallVector<TemplateArgument, 4> InstantiatedTemplateArgs;
Douglas Gregor40808ce2009-03-09 23:48:35 +0000549 InstantiatedTemplateArgs.reserve(T->getNumArgs());
Douglas Gregor7532dc62009-03-30 22:58:21 +0000550 for (TemplateSpecializationType::iterator Arg = T->begin(), ArgEnd = T->end();
Douglas Gregor40808ce2009-03-09 23:48:35 +0000551 Arg != ArgEnd; ++Arg) {
Douglas Gregor91333002009-06-11 00:06:24 +0000552 TemplateArgument InstArg = SemaRef.Instantiate(*Arg, TemplateArgs);
553 if (InstArg.isNull())
554 return QualType();
Douglas Gregor40808ce2009-03-09 23:48:35 +0000555
Douglas Gregor91333002009-06-11 00:06:24 +0000556 InstantiatedTemplateArgs.push_back(InstArg);
Douglas Gregor40808ce2009-03-09 23:48:35 +0000557 }
558
Mike Stump390b4cc2009-05-16 07:39:55 +0000559 // FIXME: We're missing the locations of the template name, '<', and '>'.
Douglas Gregorde650ae2009-03-31 18:38:02 +0000560
561 TemplateName Name = SemaRef.InstantiateTemplateName(T->getTemplateName(),
562 Loc,
Douglas Gregor7e063902009-05-11 23:53:27 +0000563 TemplateArgs);
Douglas Gregorde650ae2009-03-31 18:38:02 +0000564
565 return SemaRef.CheckTemplateIdType(Name, Loc, SourceLocation(),
Douglas Gregor02cbbd22009-06-11 18:10:32 +0000566 InstantiatedTemplateArgs.data(),
Douglas Gregor7532dc62009-03-30 22:58:21 +0000567 InstantiatedTemplateArgs.size(),
568 SourceLocation());
Douglas Gregor99ebf652009-02-27 19:31:52 +0000569}
570
Douglas Gregorcd281c32009-02-28 00:25:32 +0000571QualType
572TemplateTypeInstantiator::
Douglas Gregore4e5b052009-03-19 00:18:19 +0000573InstantiateQualifiedNameType(const QualifiedNameType *T,
574 unsigned Quals) const {
Douglas Gregord57959a2009-03-27 23:10:48 +0000575 // When we instantiated a qualified name type, there's no point in
576 // keeping the qualification around in the instantiated result. So,
577 // just instantiate the named type.
578 return (*this)(T->getNamedType());
579}
580
581QualType
582TemplateTypeInstantiator::
583InstantiateTypenameType(const TypenameType *T, unsigned Quals) const {
Douglas Gregor17343172009-04-01 00:28:59 +0000584 if (const TemplateSpecializationType *TemplateId = T->getTemplateId()) {
585 // When the typename type refers to a template-id, the template-id
586 // is dependent and has enough information to instantiate the
587 // result of the typename type. Since we don't care about keeping
588 // the spelling of the typename type in template instantiations,
589 // we just instantiate the template-id.
590 return InstantiateTemplateSpecializationType(TemplateId, Quals);
591 }
592
Douglas Gregord57959a2009-03-27 23:10:48 +0000593 NestedNameSpecifier *NNS
594 = SemaRef.InstantiateNestedNameSpecifier(T->getQualifier(),
595 SourceRange(Loc),
Douglas Gregor7e063902009-05-11 23:53:27 +0000596 TemplateArgs);
Douglas Gregord57959a2009-03-27 23:10:48 +0000597 if (!NNS)
598 return QualType();
599
Douglas Gregor17343172009-04-01 00:28:59 +0000600 return SemaRef.CheckTypenameType(NNS, *T->getIdentifier(), SourceRange(Loc));
Douglas Gregore4e5b052009-03-19 00:18:19 +0000601}
602
603QualType
604TemplateTypeInstantiator::
Douglas Gregorcd281c32009-02-28 00:25:32 +0000605InstantiateObjCInterfaceType(const ObjCInterfaceType *T,
606 unsigned Quals) const {
607 assert(false && "Objective-C types cannot be dependent");
Douglas Gregor99ebf652009-02-27 19:31:52 +0000608 return QualType();
609}
610
Douglas Gregorcd281c32009-02-28 00:25:32 +0000611QualType
612TemplateTypeInstantiator::
613InstantiateObjCQualifiedInterfaceType(const ObjCQualifiedInterfaceType *T,
614 unsigned Quals) const {
615 assert(false && "Objective-C types cannot be dependent");
Douglas Gregor99ebf652009-02-27 19:31:52 +0000616 return QualType();
617}
618
Douglas Gregorcd281c32009-02-28 00:25:32 +0000619QualType
620TemplateTypeInstantiator::
621InstantiateObjCQualifiedIdType(const ObjCQualifiedIdType *T,
622 unsigned Quals) const {
623 assert(false && "Objective-C types cannot be dependent");
Douglas Gregor99ebf652009-02-27 19:31:52 +0000624 return QualType();
625}
626
Douglas Gregorcd281c32009-02-28 00:25:32 +0000627/// \brief The actual implementation of Sema::InstantiateType().
628QualType TemplateTypeInstantiator::Instantiate(QualType T) const {
629 // If T is not a dependent type, there is nothing to do.
630 if (!T->isDependentType())
631 return T;
632
633 switch (T->getTypeClass()) {
634#define TYPE(Class, Base) \
635 case Type::Class: \
636 return Instantiate##Class##Type(cast<Class##Type>(T.getTypePtr()), \
637 T.getCVRQualifiers());
638#define ABSTRACT_TYPE(Class, Base)
639#include "clang/AST/TypeNodes.def"
640 }
641
642 assert(false && "Not all types have been decoded for instantiation");
643 return QualType();
644}
Douglas Gregor99ebf652009-02-27 19:31:52 +0000645
646/// \brief Instantiate the type T with a given set of template arguments.
647///
648/// This routine substitutes the given template arguments into the
649/// type T and produces the instantiated type.
650///
651/// \param T the type into which the template arguments will be
652/// substituted. If this type is not dependent, it will be returned
653/// immediately.
654///
655/// \param TemplateArgs the template arguments that will be
656/// substituted for the top-level template parameters within T.
657///
Douglas Gregor99ebf652009-02-27 19:31:52 +0000658/// \param Loc the location in the source code where this substitution
659/// is being performed. It will typically be the location of the
660/// declarator (if we're instantiating the type of some declaration)
661/// or the location of the type in the source code (if, e.g., we're
662/// instantiating the type of a cast expression).
663///
664/// \param Entity the name of the entity associated with a declaration
665/// being instantiated (if any). May be empty to indicate that there
666/// is no such entity (if, e.g., this is a type that occurs as part of
667/// a cast expression) or that the entity has no name (e.g., an
668/// unnamed function parameter).
669///
670/// \returns If the instantiation succeeds, the instantiated
671/// type. Otherwise, produces diagnostics and returns a NULL type.
672QualType Sema::InstantiateType(QualType T,
Douglas Gregor7e063902009-05-11 23:53:27 +0000673 const TemplateArgumentList &TemplateArgs,
Douglas Gregor99ebf652009-02-27 19:31:52 +0000674 SourceLocation Loc, DeclarationName Entity) {
Douglas Gregordf667e72009-03-10 20:44:00 +0000675 assert(!ActiveTemplateInstantiations.empty() &&
676 "Cannot perform an instantiation without some context on the "
677 "instantiation stack");
678
Douglas Gregor99ebf652009-02-27 19:31:52 +0000679 // If T is not a dependent type, there is nothing to do.
680 if (!T->isDependentType())
681 return T;
682
Douglas Gregor7e063902009-05-11 23:53:27 +0000683 TemplateTypeInstantiator Instantiator(*this, TemplateArgs, Loc, Entity);
Douglas Gregorcd281c32009-02-28 00:25:32 +0000684 return Instantiator(T);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000685}
Douglas Gregor2943aed2009-03-03 04:44:36 +0000686
687/// \brief Instantiate the base class specifiers of the given class
688/// template specialization.
689///
690/// Produces a diagnostic and returns true on error, returns false and
691/// attaches the instantiated base classes to the class template
692/// specialization if successful.
693bool
Douglas Gregord475b8d2009-03-25 21:17:03 +0000694Sema::InstantiateBaseSpecifiers(CXXRecordDecl *Instantiation,
695 CXXRecordDecl *Pattern,
Douglas Gregor7e063902009-05-11 23:53:27 +0000696 const TemplateArgumentList &TemplateArgs) {
Douglas Gregor2943aed2009-03-03 04:44:36 +0000697 bool Invalid = false;
Douglas Gregor0ca20ac2009-05-29 18:27:38 +0000698 llvm::SmallVector<CXXBaseSpecifier*, 4> InstantiatedBases;
Douglas Gregord475b8d2009-03-25 21:17:03 +0000699 for (ClassTemplateSpecializationDecl::base_class_iterator
700 Base = Pattern->bases_begin(), BaseEnd = Pattern->bases_end();
Douglas Gregor27b152f2009-03-10 18:52:44 +0000701 Base != BaseEnd; ++Base) {
Douglas Gregor2943aed2009-03-03 04:44:36 +0000702 if (!Base->getType()->isDependentType()) {
703 // FIXME: Allocate via ASTContext
704 InstantiatedBases.push_back(new CXXBaseSpecifier(*Base));
705 continue;
706 }
707
708 QualType BaseType = InstantiateType(Base->getType(),
Douglas Gregor7e063902009-05-11 23:53:27 +0000709 TemplateArgs,
Douglas Gregor2943aed2009-03-03 04:44:36 +0000710 Base->getSourceRange().getBegin(),
711 DeclarationName());
712 if (BaseType.isNull()) {
713 Invalid = true;
714 continue;
715 }
716
717 if (CXXBaseSpecifier *InstantiatedBase
Douglas Gregord475b8d2009-03-25 21:17:03 +0000718 = CheckBaseSpecifier(Instantiation,
Douglas Gregor2943aed2009-03-03 04:44:36 +0000719 Base->getSourceRange(),
720 Base->isVirtual(),
721 Base->getAccessSpecifierAsWritten(),
722 BaseType,
723 /*FIXME: Not totally accurate */
724 Base->getSourceRange().getBegin()))
725 InstantiatedBases.push_back(InstantiatedBase);
726 else
727 Invalid = true;
728 }
729
Douglas Gregor27b152f2009-03-10 18:52:44 +0000730 if (!Invalid &&
Jay Foadbeaaccd2009-05-21 09:52:38 +0000731 AttachBaseSpecifiers(Instantiation, InstantiatedBases.data(),
Douglas Gregor2943aed2009-03-03 04:44:36 +0000732 InstantiatedBases.size()))
733 Invalid = true;
734
735 return Invalid;
736}
737
Douglas Gregord475b8d2009-03-25 21:17:03 +0000738/// \brief Instantiate the definition of a class from a given pattern.
739///
740/// \param PointOfInstantiation The point of instantiation within the
741/// source code.
742///
743/// \param Instantiation is the declaration whose definition is being
744/// instantiated. This will be either a class template specialization
745/// or a member class of a class template specialization.
746///
747/// \param Pattern is the pattern from which the instantiation
748/// occurs. This will be either the declaration of a class template or
749/// the declaration of a member class of a class template.
750///
751/// \param TemplateArgs The template arguments to be substituted into
752/// the pattern.
753///
Douglas Gregord475b8d2009-03-25 21:17:03 +0000754/// \returns true if an error occurred, false otherwise.
755bool
756Sema::InstantiateClass(SourceLocation PointOfInstantiation,
757 CXXRecordDecl *Instantiation, CXXRecordDecl *Pattern,
Douglas Gregor93dfdb12009-05-13 00:25:59 +0000758 const TemplateArgumentList &TemplateArgs,
759 bool ExplicitInstantiation) {
Douglas Gregord475b8d2009-03-25 21:17:03 +0000760 bool Invalid = false;
761
762 CXXRecordDecl *PatternDef
763 = cast_or_null<CXXRecordDecl>(Pattern->getDefinition(Context));
764 if (!PatternDef) {
765 if (Pattern == Instantiation->getInstantiatedFromMemberClass()) {
766 Diag(PointOfInstantiation,
767 diag::err_implicit_instantiate_member_undefined)
768 << Context.getTypeDeclType(Instantiation);
769 Diag(Pattern->getLocation(), diag::note_member_of_template_here);
770 } else {
Douglas Gregor93dfdb12009-05-13 00:25:59 +0000771 Diag(PointOfInstantiation, diag::err_template_instantiate_undefined)
772 << ExplicitInstantiation
Douglas Gregord475b8d2009-03-25 21:17:03 +0000773 << Context.getTypeDeclType(Instantiation);
774 Diag(Pattern->getLocation(), diag::note_template_decl_here);
775 }
776 return true;
777 }
778 Pattern = PatternDef;
779
Douglas Gregord048bb72009-03-25 21:23:52 +0000780 InstantiatingTemplate Inst(*this, PointOfInstantiation, Instantiation);
Douglas Gregord475b8d2009-03-25 21:17:03 +0000781 if (Inst)
782 return true;
783
784 // Enter the scope of this instantiation. We don't use
785 // PushDeclContext because we don't have a scope.
786 DeclContext *PreviousContext = CurContext;
787 CurContext = Instantiation;
788
789 // Start the definition of this instantiation.
790 Instantiation->startDefinition();
791
792 // Instantiate the base class specifiers.
Douglas Gregor7e063902009-05-11 23:53:27 +0000793 if (InstantiateBaseSpecifiers(Instantiation, Pattern, TemplateArgs))
Douglas Gregord475b8d2009-03-25 21:17:03 +0000794 Invalid = true;
795
Douglas Gregor0ca20ac2009-05-29 18:27:38 +0000796 llvm::SmallVector<DeclPtrTy, 4> Fields;
Douglas Gregor6ab35242009-04-09 21:40:53 +0000797 for (RecordDecl::decl_iterator Member = Pattern->decls_begin(Context),
798 MemberEnd = Pattern->decls_end(Context);
799 Member != MemberEnd; ++Member) {
Douglas Gregor7e063902009-05-11 23:53:27 +0000800 Decl *NewMember = InstantiateDecl(*Member, Instantiation, TemplateArgs);
Douglas Gregord475b8d2009-03-25 21:17:03 +0000801 if (NewMember) {
802 if (NewMember->isInvalidDecl())
803 Invalid = true;
804 else if (FieldDecl *Field = dyn_cast<FieldDecl>(NewMember))
Chris Lattnerb28317a2009-03-28 19:18:32 +0000805 Fields.push_back(DeclPtrTy::make(Field));
Douglas Gregord475b8d2009-03-25 21:17:03 +0000806 } else {
807 // FIXME: Eventually, a NULL return will mean that one of the
Mike Stump390b4cc2009-05-16 07:39:55 +0000808 // instantiations was a semantic disaster, and we'll want to set Invalid =
809 // true. For now, we expect to skip some members that we can't yet handle.
Douglas Gregord475b8d2009-03-25 21:17:03 +0000810 }
811 }
812
813 // Finish checking fields.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000814 ActOnFields(0, Instantiation->getLocation(), DeclPtrTy::make(Instantiation),
Jay Foadbeaaccd2009-05-21 09:52:38 +0000815 Fields.data(), Fields.size(), SourceLocation(), SourceLocation(),
Douglas Gregord475b8d2009-03-25 21:17:03 +0000816 0);
817
818 // Add any implicitly-declared members that we might need.
819 AddImplicitlyDeclaredMembersToClass(Instantiation);
820
821 // Exit the scope of this instantiation.
822 CurContext = PreviousContext;
823
Douglas Gregoraba43bb2009-05-26 20:50:29 +0000824 if (!Invalid)
825 Consumer.HandleTagDeclDefinition(Instantiation);
826
Douglas Gregora58861f2009-05-13 20:28:22 +0000827 // If this is an explicit instantiation, instantiate our members, too.
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000828 if (!Invalid && ExplicitInstantiation) {
829 Inst.Clear();
Douglas Gregora58861f2009-05-13 20:28:22 +0000830 InstantiateClassMembers(PointOfInstantiation, Instantiation, TemplateArgs);
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000831 }
Douglas Gregora58861f2009-05-13 20:28:22 +0000832
Douglas Gregord475b8d2009-03-25 21:17:03 +0000833 return Invalid;
834}
835
Douglas Gregor2943aed2009-03-03 04:44:36 +0000836bool
837Sema::InstantiateClassTemplateSpecialization(
838 ClassTemplateSpecializationDecl *ClassTemplateSpec,
839 bool ExplicitInstantiation) {
840 // Perform the actual instantiation on the canonical declaration.
841 ClassTemplateSpec = cast<ClassTemplateSpecializationDecl>(
842 Context.getCanonicalDecl(ClassTemplateSpec));
843
844 // We can only instantiate something that hasn't already been
845 // instantiated or specialized. Fail without any diagnostics: our
846 // caller will provide an error message.
847 if (ClassTemplateSpec->getSpecializationKind() != TSK_Undeclared)
848 return true;
849
Douglas Gregor2943aed2009-03-03 04:44:36 +0000850 ClassTemplateDecl *Template = ClassTemplateSpec->getSpecializedTemplate();
Douglas Gregord475b8d2009-03-25 21:17:03 +0000851 CXXRecordDecl *Pattern = Template->getTemplatedDecl();
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000852 const TemplateArgumentList *TemplateArgs
853 = &ClassTemplateSpec->getTemplateArgs();
854
Douglas Gregorc1efb3f2009-06-12 22:31:52 +0000855 // C++ [temp.class.spec.match]p1:
856 // When a class template is used in a context that requires an
857 // instantiation of the class, it is necessary to determine
858 // whether the instantiation is to be generated using the primary
859 // template or one of the partial specializations. This is done by
860 // matching the template arguments of the class template
861 // specialization with the template argument lists of the partial
862 // specializations.
Douglas Gregor199d9912009-06-05 00:53:49 +0000863 typedef std::pair<ClassTemplatePartialSpecializationDecl *,
864 TemplateArgumentList *> MatchResult;
865 llvm::SmallVector<MatchResult, 4> Matched;
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000866 for (llvm::FoldingSet<ClassTemplatePartialSpecializationDecl>::iterator
867 Partial = Template->getPartialSpecializations().begin(),
868 PartialEnd = Template->getPartialSpecializations().end();
869 Partial != PartialEnd;
870 ++Partial) {
Douglas Gregorf67875d2009-06-12 18:26:56 +0000871 TemplateDeductionInfo Info(Context);
872 if (TemplateDeductionResult Result
Douglas Gregor199d9912009-06-05 00:53:49 +0000873 = DeduceTemplateArguments(&*Partial,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000874 ClassTemplateSpec->getTemplateArgs(),
875 Info)) {
876 // FIXME: Store the failed-deduction information for use in
877 // diagnostics, later.
878 (void)Result;
879 } else {
880 Matched.push_back(std::make_pair(&*Partial, Info.take()));
881 }
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000882 }
883
884 if (Matched.size() == 1) {
Douglas Gregorc1efb3f2009-06-12 22:31:52 +0000885 // -- If exactly one matching specialization is found, the
886 // instantiation is generated from that specialization.
Douglas Gregor199d9912009-06-05 00:53:49 +0000887 Pattern = Matched[0].first;
888 TemplateArgs = Matched[0].second;
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000889 } else if (Matched.size() > 1) {
Douglas Gregorc1efb3f2009-06-12 22:31:52 +0000890 // -- If more than one matching specialization is found, the
891 // partial order rules (14.5.4.2) are used to determine
892 // whether one of the specializations is more specialized
893 // than the others. If none of the specializations is more
894 // specialized than all of the other matching
895 // specializations, then the use of the class template is
896 // ambiguous and the program is ill-formed.
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000897 // FIXME: Implement partial ordering of class template partial
898 // specializations.
899 Diag(ClassTemplateSpec->getLocation(),
900 diag::unsup_template_partial_spec_ordering);
Douglas Gregorc1efb3f2009-06-12 22:31:52 +0000901 } else {
902 // -- If no matches are found, the instantiation is generated
903 // from the primary template.
904
905 // Since we initialized the pattern and template arguments from
906 // the primary template, there is nothing more we need to do here.
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000907 }
Douglas Gregor2943aed2009-03-03 04:44:36 +0000908
909 // Note that this is an instantiation.
910 ClassTemplateSpec->setSpecializationKind(
911 ExplicitInstantiation? TSK_ExplicitInstantiation
912 : TSK_ImplicitInstantiation);
913
Douglas Gregor199d9912009-06-05 00:53:49 +0000914 bool Result = InstantiateClass(ClassTemplateSpec->getLocation(),
915 ClassTemplateSpec, Pattern, *TemplateArgs,
916 ExplicitInstantiation);
917
918 for (unsigned I = 0, N = Matched.size(); I != N; ++I) {
919 // FIXME: Implement TemplateArgumentList::Destroy!
920 // if (Matched[I].first != Pattern)
921 // Matched[I].second->Destroy(Context);
922 }
923
924 return Result;
Douglas Gregor2943aed2009-03-03 04:44:36 +0000925}
Douglas Gregor5953d8b2009-03-19 17:26:29 +0000926
Douglas Gregora58861f2009-05-13 20:28:22 +0000927/// \brief Instantiate the definitions of all of the member of the
928/// given class, which is an instantiation of a class template or a
929/// member class of a template.
930void
931Sema::InstantiateClassMembers(SourceLocation PointOfInstantiation,
932 CXXRecordDecl *Instantiation,
933 const TemplateArgumentList &TemplateArgs) {
934 for (DeclContext::decl_iterator D = Instantiation->decls_begin(Context),
935 DEnd = Instantiation->decls_end(Context);
936 D != DEnd; ++D) {
937 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(*D)) {
938 if (!Function->getBody(Context))
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000939 InstantiateFunctionDefinition(PointOfInstantiation, Function);
Douglas Gregora58861f2009-05-13 20:28:22 +0000940 } else if (VarDecl *Var = dyn_cast<VarDecl>(*D)) {
941 const VarDecl *Def = 0;
942 if (!Var->getDefinition(Def))
943 InstantiateVariableDefinition(Var);
944 } else if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(*D)) {
945 if (!Record->isInjectedClassName() && !Record->getDefinition(Context)) {
946 assert(Record->getInstantiatedFromMemberClass() &&
947 "Missing instantiated-from-template information");
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000948 InstantiateClass(PointOfInstantiation, Record,
Douglas Gregora58861f2009-05-13 20:28:22 +0000949 Record->getInstantiatedFromMemberClass(),
950 TemplateArgs, true);
951 }
952 }
953 }
954}
955
956/// \brief Instantiate the definitions of all of the members of the
957/// given class template specialization, which was named as part of an
958/// explicit instantiation.
959void Sema::InstantiateClassTemplateSpecializationMembers(
960 SourceLocation PointOfInstantiation,
961 ClassTemplateSpecializationDecl *ClassTemplateSpec) {
962 // C++0x [temp.explicit]p7:
963 // An explicit instantiation that names a class template
964 // specialization is an explicit instantion of the same kind
965 // (declaration or definition) of each of its members (not
966 // including members inherited from base classes) that has not
967 // been previously explicitly specialized in the translation unit
968 // containing the explicit instantiation, except as described
969 // below.
970 InstantiateClassMembers(PointOfInstantiation, ClassTemplateSpec,
971 ClassTemplateSpec->getTemplateArgs());
972}
973
Douglas Gregorab452ba2009-03-26 23:50:42 +0000974/// \brief Instantiate a nested-name-specifier.
975NestedNameSpecifier *
976Sema::InstantiateNestedNameSpecifier(NestedNameSpecifier *NNS,
977 SourceRange Range,
Douglas Gregor7e063902009-05-11 23:53:27 +0000978 const TemplateArgumentList &TemplateArgs) {
Douglas Gregorab452ba2009-03-26 23:50:42 +0000979 // Instantiate the prefix of this nested name specifier.
980 NestedNameSpecifier *Prefix = NNS->getPrefix();
981 if (Prefix) {
Douglas Gregor7e063902009-05-11 23:53:27 +0000982 Prefix = InstantiateNestedNameSpecifier(Prefix, Range, TemplateArgs);
Douglas Gregorab452ba2009-03-26 23:50:42 +0000983 if (!Prefix)
984 return 0;
Douglas Gregor5953d8b2009-03-19 17:26:29 +0000985 }
986
Douglas Gregorab452ba2009-03-26 23:50:42 +0000987 switch (NNS->getKind()) {
Douglas Gregor17343172009-04-01 00:28:59 +0000988 case NestedNameSpecifier::Identifier: {
989 assert(Prefix &&
990 "Can't have an identifier nested-name-specifier with no prefix");
991 CXXScopeSpec SS;
992 // FIXME: The source location information is all wrong.
993 SS.setRange(Range);
994 SS.setScopeRep(Prefix);
995 return static_cast<NestedNameSpecifier *>(
996 ActOnCXXNestedNameSpecifier(0, SS,
997 Range.getEnd(),
998 Range.getEnd(),
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000999 *NNS->getAsIdentifier()));
Douglas Gregorab452ba2009-03-26 23:50:42 +00001000 break;
Douglas Gregor17343172009-04-01 00:28:59 +00001001 }
Douglas Gregorab452ba2009-03-26 23:50:42 +00001002
1003 case NestedNameSpecifier::Namespace:
1004 case NestedNameSpecifier::Global:
1005 return NNS;
1006
1007 case NestedNameSpecifier::TypeSpecWithTemplate:
1008 case NestedNameSpecifier::TypeSpec: {
1009 QualType T = QualType(NNS->getAsType(), 0);
1010 if (!T->isDependentType())
1011 return NNS;
1012
Douglas Gregor7e063902009-05-11 23:53:27 +00001013 T = InstantiateType(T, TemplateArgs, Range.getBegin(), DeclarationName());
Douglas Gregorab452ba2009-03-26 23:50:42 +00001014 if (T.isNull())
1015 return 0;
1016
Eli Friedman923f7532009-06-13 04:51:30 +00001017 if (T->isDependentType() || T->isRecordType() ||
Douglas Gregord57959a2009-03-27 23:10:48 +00001018 (getLangOptions().CPlusPlus0x && T->isEnumeralType())) {
Douglas Gregor17343172009-04-01 00:28:59 +00001019 assert(T.getCVRQualifiers() == 0 && "Can't get cv-qualifiers here");
Douglas Gregord57959a2009-03-27 23:10:48 +00001020 return NestedNameSpecifier::Create(Context, Prefix,
Douglas Gregorab452ba2009-03-26 23:50:42 +00001021 NNS->getKind() == NestedNameSpecifier::TypeSpecWithTemplate,
Douglas Gregord57959a2009-03-27 23:10:48 +00001022 T.getTypePtr());
1023 }
1024
1025 Diag(Range.getBegin(), diag::err_nested_name_spec_non_tag) << T;
1026 return 0;
Douglas Gregorab452ba2009-03-26 23:50:42 +00001027 }
1028 }
1029
Douglas Gregord57959a2009-03-27 23:10:48 +00001030 // Required to silence a GCC warning
Douglas Gregorab452ba2009-03-26 23:50:42 +00001031 return 0;
Douglas Gregor5953d8b2009-03-19 17:26:29 +00001032}
Douglas Gregorde650ae2009-03-31 18:38:02 +00001033
1034TemplateName
1035Sema::InstantiateTemplateName(TemplateName Name, SourceLocation Loc,
Douglas Gregor7e063902009-05-11 23:53:27 +00001036 const TemplateArgumentList &TemplateArgs) {
Douglas Gregorde650ae2009-03-31 18:38:02 +00001037 if (TemplateTemplateParmDecl *TTP
1038 = dyn_cast_or_null<TemplateTemplateParmDecl>(
1039 Name.getAsTemplateDecl())) {
1040 assert(TTP->getDepth() == 0 &&
1041 "Cannot reduce depth of a template template parameter");
Douglas Gregor9bde7732009-03-31 20:22:05 +00001042 assert(TemplateArgs[TTP->getPosition()].getAsDecl() &&
Douglas Gregorde650ae2009-03-31 18:38:02 +00001043 "Wrong kind of template template argument");
1044 ClassTemplateDecl *ClassTemplate
1045 = dyn_cast<ClassTemplateDecl>(
1046 TemplateArgs[TTP->getPosition()].getAsDecl());
Douglas Gregor9bde7732009-03-31 20:22:05 +00001047 assert(ClassTemplate && "Expected a class template");
Douglas Gregorde650ae2009-03-31 18:38:02 +00001048 if (QualifiedTemplateName *QTN = Name.getAsQualifiedTemplateName()) {
1049 NestedNameSpecifier *NNS
1050 = InstantiateNestedNameSpecifier(QTN->getQualifier(),
1051 /*FIXME=*/SourceRange(Loc),
Douglas Gregor7e063902009-05-11 23:53:27 +00001052 TemplateArgs);
Douglas Gregorde650ae2009-03-31 18:38:02 +00001053 if (NNS)
1054 return Context.getQualifiedTemplateName(NNS,
1055 QTN->hasTemplateKeyword(),
1056 ClassTemplate);
1057 }
1058
1059 return TemplateName(ClassTemplate);
1060 } else if (DependentTemplateName *DTN = Name.getAsDependentTemplateName()) {
1061 NestedNameSpecifier *NNS
1062 = InstantiateNestedNameSpecifier(DTN->getQualifier(),
1063 /*FIXME=*/SourceRange(Loc),
Douglas Gregor7e063902009-05-11 23:53:27 +00001064 TemplateArgs);
Douglas Gregorde650ae2009-03-31 18:38:02 +00001065
1066 if (!NNS) // FIXME: Not the best recovery strategy.
1067 return Name;
1068
1069 if (NNS->isDependent())
1070 return Context.getDependentTemplateName(NNS, DTN->getName());
1071
1072 // Somewhat redundant with ActOnDependentTemplateName.
1073 CXXScopeSpec SS;
1074 SS.setRange(SourceRange(Loc));
1075 SS.setScopeRep(NNS);
1076 TemplateTy Template;
1077 TemplateNameKind TNK = isTemplateName(*DTN->getName(), 0, Template, &SS);
1078 if (TNK == TNK_Non_template) {
1079 Diag(Loc, diag::err_template_kw_refers_to_non_template)
1080 << DTN->getName();
1081 return Name;
1082 } else if (TNK == TNK_Function_template) {
1083 Diag(Loc, diag::err_template_kw_refers_to_non_template)
1084 << DTN->getName();
1085 return Name;
1086 }
1087
1088 return Template.getAsVal<TemplateName>();
1089 }
1090
1091
1092
Mike Stump390b4cc2009-05-16 07:39:55 +00001093 // FIXME: Even if we're referring to a Decl that isn't a template template
1094 // parameter, we may need to instantiate the outer contexts of that
1095 // Decl. However, this won't be needed until we implement member templates.
Douglas Gregorde650ae2009-03-31 18:38:02 +00001096 return Name;
1097}
Douglas Gregor91333002009-06-11 00:06:24 +00001098
1099TemplateArgument Sema::Instantiate(TemplateArgument Arg,
1100 const TemplateArgumentList &TemplateArgs) {
1101 switch (Arg.getKind()) {
1102 case TemplateArgument::Null:
1103 assert(false && "Should never have a NULL template argument");
1104 break;
1105
1106 case TemplateArgument::Type: {
1107 QualType T = InstantiateType(Arg.getAsType(), TemplateArgs,
1108 Arg.getLocation(), DeclarationName());
1109 if (T.isNull())
1110 return TemplateArgument();
1111
1112 return TemplateArgument(Arg.getLocation(), T);
1113 }
1114
1115 case TemplateArgument::Declaration:
1116 // FIXME: Template instantiation for template template parameters.
1117 return Arg;
1118
1119 case TemplateArgument::Integral:
1120 return Arg;
1121
1122 case TemplateArgument::Expression: {
1123 Sema::OwningExprResult E = InstantiateExpr(Arg.getAsExpr(), TemplateArgs);
1124 if (E.isInvalid())
1125 return TemplateArgument();
1126 return TemplateArgument(E.takeAs<Expr>());
1127 }
1128 }
1129
1130 assert(false && "Unhandled template argument kind");
1131 return TemplateArgument();
1132}