blob: a5411bdd059be75ebe2c57a2030226eed8a43cbb [file] [log] [blame]
Douglas Gregor74296542009-02-27 19:31:52 +00001//===------- SemaTemplateInstantiate.cpp - C++ Template Instantiation ------===/
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//===----------------------------------------------------------------------===/
8//
9// This file implements C++ template instantiation.
10//
11//===----------------------------------------------------------------------===/
12
13#include "Sema.h"
14#include "clang/AST/ASTContext.h"
15#include "clang/AST/Expr.h"
Douglas Gregor74296542009-02-27 19:31:52 +000016#include "clang/AST/DeclTemplate.h"
17#include "clang/Parse/DeclSpec.h"
18#include "clang/Basic/LangOptions.h"
Douglas Gregorf57dcd02009-02-28 00:25:32 +000019#include "llvm/Support/Compiler.h"
Douglas Gregor74296542009-02-27 19:31:52 +000020
21using namespace clang;
22
Douglas Gregorfee85d62009-03-10 18:03:33 +000023//===----------------------------------------------------------------------===/
24// Template Instantiation Support
25//===----------------------------------------------------------------------===/
26
Douglas Gregor5f62c5e2009-05-14 23:26:13 +000027/// \brief Retrieve the template argument list that should be used to
28/// instantiate the given declaration.
29const TemplateArgumentList &
30Sema::getTemplateInstantiationArgs(NamedDecl *D) {
31 if (ClassTemplateSpecializationDecl *Spec
32 = dyn_cast<ClassTemplateSpecializationDecl>(D))
33 return Spec->getTemplateArgs();
34
35 DeclContext *EnclosingTemplateCtx = D->getDeclContext();
36 while (!isa<ClassTemplateSpecializationDecl>(EnclosingTemplateCtx)) {
37 assert(!EnclosingTemplateCtx->isFileContext() &&
38 "Tried to get the instantiation arguments of a non-template");
39 EnclosingTemplateCtx = EnclosingTemplateCtx->getParent();
40 }
41
42 ClassTemplateSpecializationDecl *EnclosingTemplate
43 = cast<ClassTemplateSpecializationDecl>(EnclosingTemplateCtx);
44 return EnclosingTemplate->getTemplateArgs();
45}
46
Douglas Gregor375733c2009-03-10 00:06:19 +000047Sema::InstantiatingTemplate::
48InstantiatingTemplate(Sema &SemaRef, SourceLocation PointOfInstantiation,
Douglas Gregorcc887972009-03-25 21:17:03 +000049 CXXRecordDecl *Entity,
Douglas Gregor375733c2009-03-10 00:06:19 +000050 SourceRange InstantiationRange)
51 : SemaRef(SemaRef) {
Douglas Gregor56d25a72009-03-10 20:44:00 +000052
53 Invalid = CheckInstantiationDepth(PointOfInstantiation,
54 InstantiationRange);
55 if (!Invalid) {
Douglas Gregor375733c2009-03-10 00:06:19 +000056 ActiveTemplateInstantiation Inst;
Douglas Gregor56d25a72009-03-10 20:44:00 +000057 Inst.Kind = ActiveTemplateInstantiation::TemplateInstantiation;
Douglas Gregor375733c2009-03-10 00:06:19 +000058 Inst.PointOfInstantiation = PointOfInstantiation;
Douglas Gregor56d25a72009-03-10 20:44:00 +000059 Inst.Entity = reinterpret_cast<uintptr_t>(Entity);
Douglas Gregor95ba1282009-03-12 18:36:18 +000060 Inst.TemplateArgs = 0;
61 Inst.NumTemplateArgs = 0;
Douglas Gregor56d25a72009-03-10 20:44:00 +000062 Inst.InstantiationRange = InstantiationRange;
63 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
64 Invalid = false;
65 }
66}
67
68Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef,
69 SourceLocation PointOfInstantiation,
70 TemplateDecl *Template,
71 const TemplateArgument *TemplateArgs,
72 unsigned NumTemplateArgs,
73 SourceRange InstantiationRange)
74 : SemaRef(SemaRef) {
75
76 Invalid = CheckInstantiationDepth(PointOfInstantiation,
77 InstantiationRange);
78 if (!Invalid) {
79 ActiveTemplateInstantiation Inst;
80 Inst.Kind
81 = ActiveTemplateInstantiation::DefaultTemplateArgumentInstantiation;
82 Inst.PointOfInstantiation = PointOfInstantiation;
83 Inst.Entity = reinterpret_cast<uintptr_t>(Template);
84 Inst.TemplateArgs = TemplateArgs;
85 Inst.NumTemplateArgs = NumTemplateArgs;
Douglas Gregor375733c2009-03-10 00:06:19 +000086 Inst.InstantiationRange = InstantiationRange;
87 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
88 Invalid = false;
89 }
90}
91
92Sema::InstantiatingTemplate::~InstantiatingTemplate() {
93 if (!Invalid)
94 SemaRef.ActiveTemplateInstantiations.pop_back();
95}
96
Douglas Gregor56d25a72009-03-10 20:44:00 +000097bool Sema::InstantiatingTemplate::CheckInstantiationDepth(
98 SourceLocation PointOfInstantiation,
99 SourceRange InstantiationRange) {
100 if (SemaRef.ActiveTemplateInstantiations.size()
101 <= SemaRef.getLangOptions().InstantiationDepth)
102 return false;
103
104 SemaRef.Diag(PointOfInstantiation,
105 diag::err_template_recursion_depth_exceeded)
106 << SemaRef.getLangOptions().InstantiationDepth
107 << InstantiationRange;
108 SemaRef.Diag(PointOfInstantiation, diag::note_template_recursion_depth)
109 << SemaRef.getLangOptions().InstantiationDepth;
110 return true;
111}
112
Douglas Gregorfee85d62009-03-10 18:03:33 +0000113/// \brief Prints the current instantiation stack through a series of
114/// notes.
115void Sema::PrintInstantiationStack() {
116 for (llvm::SmallVector<ActiveTemplateInstantiation, 16>::reverse_iterator
117 Active = ActiveTemplateInstantiations.rbegin(),
118 ActiveEnd = ActiveTemplateInstantiations.rend();
119 Active != ActiveEnd;
120 ++Active) {
Douglas Gregor56d25a72009-03-10 20:44:00 +0000121 switch (Active->Kind) {
122 case ActiveTemplateInstantiation::TemplateInstantiation: {
Douglas Gregorcc887972009-03-25 21:17:03 +0000123 unsigned DiagID = diag::note_template_member_class_here;
124 CXXRecordDecl *Record = (CXXRecordDecl *)Active->Entity;
125 if (isa<ClassTemplateSpecializationDecl>(Record))
126 DiagID = diag::note_template_class_instantiation_here;
Douglas Gregor56d25a72009-03-10 20:44:00 +0000127 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
Douglas Gregorcc887972009-03-25 21:17:03 +0000128 DiagID)
129 << Context.getTypeDeclType(Record)
Douglas Gregor56d25a72009-03-10 20:44:00 +0000130 << Active->InstantiationRange;
131 break;
132 }
133
134 case ActiveTemplateInstantiation::DefaultTemplateArgumentInstantiation: {
135 TemplateDecl *Template = cast<TemplateDecl>((Decl *)Active->Entity);
136 std::string TemplateArgsStr
Douglas Gregordd13e842009-03-30 22:58:21 +0000137 = TemplateSpecializationType::PrintTemplateArgumentList(
Douglas Gregor56d25a72009-03-10 20:44:00 +0000138 Active->TemplateArgs,
139 Active->NumTemplateArgs);
140 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
141 diag::note_default_arg_instantiation_here)
142 << (Template->getNameAsString() + TemplateArgsStr)
143 << Active->InstantiationRange;
144 break;
145 }
146 }
Douglas Gregorfee85d62009-03-10 18:03:33 +0000147 }
148}
149
Douglas Gregor74296542009-02-27 19:31:52 +0000150//===----------------------------------------------------------------------===/
151// Template Instantiation for Types
152//===----------------------------------------------------------------------===/
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000153namespace {
154 class VISIBILITY_HIDDEN TemplateTypeInstantiator {
155 Sema &SemaRef;
Douglas Gregorf9e7d3d2009-05-11 23:53:27 +0000156 const TemplateArgumentList &TemplateArgs;
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000157 SourceLocation Loc;
158 DeclarationName Entity;
Douglas Gregor74296542009-02-27 19:31:52 +0000159
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000160 public:
161 TemplateTypeInstantiator(Sema &SemaRef,
Douglas Gregorf9e7d3d2009-05-11 23:53:27 +0000162 const TemplateArgumentList &TemplateArgs,
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000163 SourceLocation Loc,
164 DeclarationName Entity)
165 : SemaRef(SemaRef), TemplateArgs(TemplateArgs),
Douglas Gregorf9e7d3d2009-05-11 23:53:27 +0000166 Loc(Loc), Entity(Entity) { }
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000167
168 QualType operator()(QualType T) const { return Instantiate(T); }
169
170 QualType Instantiate(QualType T) const;
171
172 // Declare instantiate functions for each type.
173#define TYPE(Class, Base) \
174 QualType Instantiate##Class##Type(const Class##Type *T, \
175 unsigned Quals) const;
176#define ABSTRACT_TYPE(Class, Base)
177#include "clang/AST/TypeNodes.def"
178 };
179}
180
181QualType
182TemplateTypeInstantiator::InstantiateExtQualType(const ExtQualType *T,
183 unsigned Quals) const {
Douglas Gregor74296542009-02-27 19:31:52 +0000184 // FIXME: Implement this
185 assert(false && "Cannot instantiate ExtQualType yet");
186 return QualType();
187}
188
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000189QualType
190TemplateTypeInstantiator::InstantiateBuiltinType(const BuiltinType *T,
191 unsigned Quals) const {
Douglas Gregorc0139ca2009-02-28 01:04:19 +0000192 assert(false && "Builtin types are not dependent and cannot be instantiated");
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000193 return QualType(T, Quals);
Douglas Gregor74296542009-02-27 19:31:52 +0000194}
195
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000196QualType
197TemplateTypeInstantiator::
198InstantiateFixedWidthIntType(const FixedWidthIntType *T, unsigned Quals) const {
Douglas Gregor74296542009-02-27 19:31:52 +0000199 // FIXME: Implement this
200 assert(false && "Cannot instantiate FixedWidthIntType yet");
201 return QualType();
202}
203
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000204QualType
205TemplateTypeInstantiator::InstantiateComplexType(const ComplexType *T,
206 unsigned Quals) const {
Douglas Gregor74296542009-02-27 19:31:52 +0000207 // FIXME: Implement this
208 assert(false && "Cannot instantiate ComplexType yet");
209 return QualType();
210}
211
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000212QualType
213TemplateTypeInstantiator::InstantiatePointerType(const PointerType *T,
214 unsigned Quals) const {
215 QualType PointeeType = Instantiate(T->getPointeeType());
216 if (PointeeType.isNull())
217 return QualType();
218
219 return SemaRef.BuildPointerType(PointeeType, Quals, Loc, Entity);
Douglas Gregor74296542009-02-27 19:31:52 +0000220}
221
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000222QualType
223TemplateTypeInstantiator::InstantiateBlockPointerType(const BlockPointerType *T,
224 unsigned Quals) const {
Douglas Gregor74296542009-02-27 19:31:52 +0000225 // FIXME: Implement this
226 assert(false && "Cannot instantiate BlockPointerType yet");
227 return QualType();
228}
229
Sebastian Redlce6fff02009-03-16 23:22:08 +0000230QualType
231TemplateTypeInstantiator::InstantiateLValueReferenceType(
232 const LValueReferenceType *T, unsigned Quals) const {
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000233 QualType ReferentType = Instantiate(T->getPointeeType());
234 if (ReferentType.isNull())
235 return QualType();
236
Sebastian Redlce6fff02009-03-16 23:22:08 +0000237 return SemaRef.BuildReferenceType(ReferentType, true, Quals, Loc, Entity);
238}
239
240QualType
241TemplateTypeInstantiator::InstantiateRValueReferenceType(
242 const RValueReferenceType *T, unsigned Quals) const {
243 QualType ReferentType = Instantiate(T->getPointeeType());
244 if (ReferentType.isNull())
245 return QualType();
246
247 return SemaRef.BuildReferenceType(ReferentType, false, Quals, Loc, Entity);
Douglas Gregor74296542009-02-27 19:31:52 +0000248}
249
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000250QualType
251TemplateTypeInstantiator::
252InstantiateMemberPointerType(const MemberPointerType *T,
253 unsigned Quals) const {
Douglas Gregor74296542009-02-27 19:31:52 +0000254 // FIXME: Implement this
255 assert(false && "Cannot instantiate MemberPointerType yet");
256 return QualType();
257}
258
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000259QualType
260TemplateTypeInstantiator::
261InstantiateConstantArrayType(const ConstantArrayType *T,
262 unsigned Quals) const {
263 QualType ElementType = Instantiate(T->getElementType());
264 if (ElementType.isNull())
265 return ElementType;
266
267 // Build a temporary integer literal to specify the size for
268 // BuildArrayType. Since we have already checked the size as part of
269 // creating the dependent array type in the first place, we know
Douglas Gregor90177912009-05-13 18:28:20 +0000270 // there aren't any errors. However, we do need to determine what
271 // C++ type to give the size expression.
272 llvm::APInt Size = T->getSize();
273 QualType Types[] = {
274 SemaRef.Context.UnsignedCharTy, SemaRef.Context.UnsignedShortTy,
275 SemaRef.Context.UnsignedIntTy, SemaRef.Context.UnsignedLongTy,
276 SemaRef.Context.UnsignedLongLongTy, SemaRef.Context.UnsignedInt128Ty
277 };
278 const unsigned NumTypes = sizeof(Types) / sizeof(QualType);
279 QualType SizeType;
280 for (unsigned I = 0; I != NumTypes; ++I)
281 if (Size.getBitWidth() == SemaRef.Context.getIntWidth(Types[I])) {
282 SizeType = Types[I];
283 break;
284 }
285
286 if (SizeType.isNull())
287 SizeType = SemaRef.Context.getFixedWidthIntType(Size.getBitWidth(), false);
288
289 IntegerLiteral ArraySize(Size, SizeType, Loc);
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000290 return SemaRef.BuildArrayType(ElementType, T->getSizeModifier(),
291 &ArraySize, T->getIndexTypeQualifier(),
292 Loc, Entity);
Douglas Gregor74296542009-02-27 19:31:52 +0000293}
294
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000295QualType
296TemplateTypeInstantiator::
297InstantiateIncompleteArrayType(const IncompleteArrayType *T,
298 unsigned Quals) const {
299 QualType ElementType = Instantiate(T->getElementType());
300 if (ElementType.isNull())
301 return ElementType;
302
303 return SemaRef.BuildArrayType(ElementType, T->getSizeModifier(),
304 0, T->getIndexTypeQualifier(),
305 Loc, Entity);
Douglas Gregor74296542009-02-27 19:31:52 +0000306}
307
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000308QualType
309TemplateTypeInstantiator::
310InstantiateVariableArrayType(const VariableArrayType *T,
311 unsigned Quals) const {
Douglas Gregor74296542009-02-27 19:31:52 +0000312 // FIXME: Implement this
313 assert(false && "Cannot instantiate VariableArrayType yet");
314 return QualType();
315}
316
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000317QualType
318TemplateTypeInstantiator::
319InstantiateDependentSizedArrayType(const DependentSizedArrayType *T,
320 unsigned Quals) const {
Anders Carlsson149f2782009-03-15 20:12:13 +0000321 Expr *ArraySize = T->getSizeExpr();
322 assert(ArraySize->isValueDependent() &&
323 "dependent sized array types must have value dependent size expr");
324
325 // Instantiate the element type if needed
326 QualType ElementType = T->getElementType();
327 if (ElementType->isDependentType()) {
328 ElementType = Instantiate(ElementType);
329 if (ElementType.isNull())
330 return QualType();
331 }
332
333 // Instantiate the size expression
334 Sema::OwningExprResult InstantiatedArraySize =
Douglas Gregorf9e7d3d2009-05-11 23:53:27 +0000335 SemaRef.InstantiateExpr(ArraySize, TemplateArgs);
Anders Carlsson149f2782009-03-15 20:12:13 +0000336 if (InstantiatedArraySize.isInvalid())
337 return QualType();
338
339 return SemaRef.BuildArrayType(ElementType, T->getSizeModifier(),
Anders Carlsson39ecdcf2009-05-01 19:49:17 +0000340 InstantiatedArraySize.takeAs<Expr>(),
Anders Carlsson149f2782009-03-15 20:12:13 +0000341 T->getIndexTypeQualifier(), Loc, Entity);
Douglas Gregor74296542009-02-27 19:31:52 +0000342}
343
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000344QualType
345TemplateTypeInstantiator::InstantiateVectorType(const VectorType *T,
346 unsigned Quals) const {
Douglas Gregor74296542009-02-27 19:31:52 +0000347 // FIXME: Implement this
348 assert(false && "Cannot instantiate VectorType yet");
349 return QualType();
350}
351
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000352QualType
353TemplateTypeInstantiator::InstantiateExtVectorType(const ExtVectorType *T,
354 unsigned Quals) const {
Douglas Gregor74296542009-02-27 19:31:52 +0000355 // FIXME: Implement this
356 assert(false && "Cannot instantiate ExtVectorType yet");
357 return QualType();
358}
359
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000360QualType
361TemplateTypeInstantiator::
362InstantiateFunctionProtoType(const FunctionProtoType *T,
363 unsigned Quals) const {
Douglas Gregorc0139ca2009-02-28 01:04:19 +0000364 QualType ResultType = Instantiate(T->getResultType());
365 if (ResultType.isNull())
366 return ResultType;
367
368 llvm::SmallVector<QualType, 16> ParamTypes;
369 for (FunctionProtoType::arg_type_iterator Param = T->arg_type_begin(),
370 ParamEnd = T->arg_type_end();
371 Param != ParamEnd; ++Param) {
372 QualType P = Instantiate(*Param);
373 if (P.isNull())
374 return P;
375
376 ParamTypes.push_back(P);
377 }
378
379 return SemaRef.BuildFunctionType(ResultType, &ParamTypes[0],
380 ParamTypes.size(),
381 T->isVariadic(), T->getTypeQuals(),
382 Loc, Entity);
Douglas Gregor74296542009-02-27 19:31:52 +0000383}
384
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000385QualType
386TemplateTypeInstantiator::
387InstantiateFunctionNoProtoType(const FunctionNoProtoType *T,
388 unsigned Quals) const {
Douglas Gregorc0139ca2009-02-28 01:04:19 +0000389 assert(false && "Functions without prototypes cannot be dependent.");
Douglas Gregor74296542009-02-27 19:31:52 +0000390 return QualType();
391}
392
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000393QualType
394TemplateTypeInstantiator::InstantiateTypedefType(const TypedefType *T,
395 unsigned Quals) const {
Douglas Gregor74296542009-02-27 19:31:52 +0000396 // FIXME: Implement this
397 assert(false && "Cannot instantiate TypedefType yet");
398 return QualType();
399}
400
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000401QualType
402TemplateTypeInstantiator::InstantiateTypeOfExprType(const TypeOfExprType *T,
403 unsigned Quals) const {
Douglas Gregor74296542009-02-27 19:31:52 +0000404 // FIXME: Implement this
405 assert(false && "Cannot instantiate TypeOfExprType yet");
406 return QualType();
407}
408
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000409QualType
410TemplateTypeInstantiator::InstantiateTypeOfType(const TypeOfType *T,
411 unsigned Quals) const {
Douglas Gregor74296542009-02-27 19:31:52 +0000412 // FIXME: Implement this
413 assert(false && "Cannot instantiate TypeOfType yet");
414 return QualType();
415}
416
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000417QualType
418TemplateTypeInstantiator::InstantiateRecordType(const RecordType *T,
419 unsigned Quals) const {
Douglas Gregor74296542009-02-27 19:31:52 +0000420 // FIXME: Implement this
421 assert(false && "Cannot instantiate RecordType yet");
422 return QualType();
423}
424
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000425QualType
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000426TemplateTypeInstantiator::InstantiateEnumType(const EnumType *T,
427 unsigned Quals) const {
Douglas Gregor74296542009-02-27 19:31:52 +0000428 // FIXME: Implement this
429 assert(false && "Cannot instantiate EnumType yet");
430 return QualType();
431}
432
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000433QualType
434TemplateTypeInstantiator::
435InstantiateTemplateTypeParmType(const TemplateTypeParmType *T,
436 unsigned Quals) const {
Douglas Gregor74296542009-02-27 19:31:52 +0000437 if (T->getDepth() == 0) {
438 // Replace the template type parameter with its corresponding
439 // template argument.
Douglas Gregor74296542009-02-27 19:31:52 +0000440 assert(TemplateArgs[T->getIndex()].getKind() == TemplateArgument::Type &&
441 "Template argument kind mismatch");
442 QualType Result = TemplateArgs[T->getIndex()].getAsType();
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000443 if (Result.isNull() || !Quals)
Douglas Gregor74296542009-02-27 19:31:52 +0000444 return Result;
445
446 // C++ [dcl.ref]p1:
447 // [...] Cv-qualified references are ill-formed except when
448 // the cv-qualifiers are introduced through the use of a
449 // typedef (7.1.3) or of a template type argument (14.3), in
450 // which case the cv-qualifiers are ignored.
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000451 if (Quals && Result->isReferenceType())
452 Quals = 0;
Douglas Gregor74296542009-02-27 19:31:52 +0000453
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000454 return QualType(Result.getTypePtr(), Quals | Result.getCVRQualifiers());
Douglas Gregor74296542009-02-27 19:31:52 +0000455 }
456
457 // The template type parameter comes from an inner template (e.g.,
458 // the template parameter list of a member template inside the
459 // template we are instantiating). Create a new template type
460 // parameter with the template "level" reduced by one.
461 return SemaRef.Context.getTemplateTypeParmType(T->getDepth() - 1,
462 T->getIndex(),
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000463 T->getName())
464 .getQualifiedType(Quals);
Douglas Gregor74296542009-02-27 19:31:52 +0000465}
466
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000467QualType
468TemplateTypeInstantiator::
Douglas Gregordd13e842009-03-30 22:58:21 +0000469InstantiateTemplateSpecializationType(
470 const TemplateSpecializationType *T,
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000471 unsigned Quals) const {
Douglas Gregorf9ff4b12009-03-09 23:48:35 +0000472 llvm::SmallVector<TemplateArgument, 16> InstantiatedTemplateArgs;
473 InstantiatedTemplateArgs.reserve(T->getNumArgs());
Douglas Gregordd13e842009-03-30 22:58:21 +0000474 for (TemplateSpecializationType::iterator Arg = T->begin(), ArgEnd = T->end();
Douglas Gregorf9ff4b12009-03-09 23:48:35 +0000475 Arg != ArgEnd; ++Arg) {
476 switch (Arg->getKind()) {
477 case TemplateArgument::Type: {
478 QualType T = SemaRef.InstantiateType(Arg->getAsType(),
Douglas Gregorf9e7d3d2009-05-11 23:53:27 +0000479 TemplateArgs,
Douglas Gregorf9ff4b12009-03-09 23:48:35 +0000480 Arg->getLocation(),
481 DeclarationName());
482 if (T.isNull())
483 return QualType();
484
485 InstantiatedTemplateArgs.push_back(
486 TemplateArgument(Arg->getLocation(), T));
487 break;
488 }
489
490 case TemplateArgument::Declaration:
491 case TemplateArgument::Integral:
492 InstantiatedTemplateArgs.push_back(*Arg);
493 break;
494
495 case TemplateArgument::Expression:
Douglas Gregor396f1142009-03-13 21:01:28 +0000496 Sema::OwningExprResult E
Douglas Gregorf9e7d3d2009-05-11 23:53:27 +0000497 = SemaRef.InstantiateExpr(Arg->getAsExpr(), TemplateArgs);
Douglas Gregor396f1142009-03-13 21:01:28 +0000498 if (E.isInvalid())
499 return QualType();
Anders Carlssonc154a722009-05-01 19:30:39 +0000500 InstantiatedTemplateArgs.push_back(E.takeAs<Expr>());
Douglas Gregorf9ff4b12009-03-09 23:48:35 +0000501 break;
502 }
503 }
504
Mike Stumpe127ae32009-05-16 07:39:55 +0000505 // FIXME: We're missing the locations of the template name, '<', and '>'.
Douglas Gregor15a92852009-03-31 18:38:02 +0000506
507 TemplateName Name = SemaRef.InstantiateTemplateName(T->getTemplateName(),
508 Loc,
Douglas Gregorf9e7d3d2009-05-11 23:53:27 +0000509 TemplateArgs);
Douglas Gregor15a92852009-03-31 18:38:02 +0000510
511 return SemaRef.CheckTemplateIdType(Name, Loc, SourceLocation(),
Douglas Gregordd13e842009-03-30 22:58:21 +0000512 &InstantiatedTemplateArgs[0],
513 InstantiatedTemplateArgs.size(),
514 SourceLocation());
Douglas Gregor74296542009-02-27 19:31:52 +0000515}
516
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000517QualType
518TemplateTypeInstantiator::
Douglas Gregor734b4ba2009-03-19 00:18:19 +0000519InstantiateQualifiedNameType(const QualifiedNameType *T,
520 unsigned Quals) const {
Douglas Gregord3022602009-03-27 23:10:48 +0000521 // When we instantiated a qualified name type, there's no point in
522 // keeping the qualification around in the instantiated result. So,
523 // just instantiate the named type.
524 return (*this)(T->getNamedType());
525}
526
527QualType
528TemplateTypeInstantiator::
529InstantiateTypenameType(const TypenameType *T, unsigned Quals) const {
Douglas Gregor77da5802009-04-01 00:28:59 +0000530 if (const TemplateSpecializationType *TemplateId = T->getTemplateId()) {
531 // When the typename type refers to a template-id, the template-id
532 // is dependent and has enough information to instantiate the
533 // result of the typename type. Since we don't care about keeping
534 // the spelling of the typename type in template instantiations,
535 // we just instantiate the template-id.
536 return InstantiateTemplateSpecializationType(TemplateId, Quals);
537 }
538
Douglas Gregord3022602009-03-27 23:10:48 +0000539 NestedNameSpecifier *NNS
540 = SemaRef.InstantiateNestedNameSpecifier(T->getQualifier(),
541 SourceRange(Loc),
Douglas Gregorf9e7d3d2009-05-11 23:53:27 +0000542 TemplateArgs);
Douglas Gregord3022602009-03-27 23:10:48 +0000543 if (!NNS)
544 return QualType();
545
Douglas Gregor77da5802009-04-01 00:28:59 +0000546 return SemaRef.CheckTypenameType(NNS, *T->getIdentifier(), SourceRange(Loc));
Douglas Gregor734b4ba2009-03-19 00:18:19 +0000547}
548
549QualType
550TemplateTypeInstantiator::
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000551InstantiateObjCInterfaceType(const ObjCInterfaceType *T,
552 unsigned Quals) const {
553 assert(false && "Objective-C types cannot be dependent");
Douglas Gregor74296542009-02-27 19:31:52 +0000554 return QualType();
555}
556
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000557QualType
558TemplateTypeInstantiator::
559InstantiateObjCQualifiedInterfaceType(const ObjCQualifiedInterfaceType *T,
560 unsigned Quals) const {
561 assert(false && "Objective-C types cannot be dependent");
Douglas Gregor74296542009-02-27 19:31:52 +0000562 return QualType();
563}
564
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000565QualType
566TemplateTypeInstantiator::
567InstantiateObjCQualifiedIdType(const ObjCQualifiedIdType *T,
568 unsigned Quals) const {
569 assert(false && "Objective-C types cannot be dependent");
Douglas Gregor74296542009-02-27 19:31:52 +0000570 return QualType();
571}
572
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000573/// \brief The actual implementation of Sema::InstantiateType().
574QualType TemplateTypeInstantiator::Instantiate(QualType T) const {
575 // If T is not a dependent type, there is nothing to do.
576 if (!T->isDependentType())
577 return T;
578
579 switch (T->getTypeClass()) {
580#define TYPE(Class, Base) \
581 case Type::Class: \
582 return Instantiate##Class##Type(cast<Class##Type>(T.getTypePtr()), \
583 T.getCVRQualifiers());
584#define ABSTRACT_TYPE(Class, Base)
585#include "clang/AST/TypeNodes.def"
586 }
587
588 assert(false && "Not all types have been decoded for instantiation");
589 return QualType();
590}
Douglas Gregor74296542009-02-27 19:31:52 +0000591
592/// \brief Instantiate the type T with a given set of template arguments.
593///
594/// This routine substitutes the given template arguments into the
595/// type T and produces the instantiated type.
596///
597/// \param T the type into which the template arguments will be
598/// substituted. If this type is not dependent, it will be returned
599/// immediately.
600///
601/// \param TemplateArgs the template arguments that will be
602/// substituted for the top-level template parameters within T.
603///
Douglas Gregor74296542009-02-27 19:31:52 +0000604/// \param Loc the location in the source code where this substitution
605/// is being performed. It will typically be the location of the
606/// declarator (if we're instantiating the type of some declaration)
607/// or the location of the type in the source code (if, e.g., we're
608/// instantiating the type of a cast expression).
609///
610/// \param Entity the name of the entity associated with a declaration
611/// being instantiated (if any). May be empty to indicate that there
612/// is no such entity (if, e.g., this is a type that occurs as part of
613/// a cast expression) or that the entity has no name (e.g., an
614/// unnamed function parameter).
615///
616/// \returns If the instantiation succeeds, the instantiated
617/// type. Otherwise, produces diagnostics and returns a NULL type.
618QualType Sema::InstantiateType(QualType T,
Douglas Gregorf9e7d3d2009-05-11 23:53:27 +0000619 const TemplateArgumentList &TemplateArgs,
Douglas Gregor74296542009-02-27 19:31:52 +0000620 SourceLocation Loc, DeclarationName Entity) {
Douglas Gregor56d25a72009-03-10 20:44:00 +0000621 assert(!ActiveTemplateInstantiations.empty() &&
622 "Cannot perform an instantiation without some context on the "
623 "instantiation stack");
624
Douglas Gregor74296542009-02-27 19:31:52 +0000625 // If T is not a dependent type, there is nothing to do.
626 if (!T->isDependentType())
627 return T;
628
Douglas Gregorf9e7d3d2009-05-11 23:53:27 +0000629 TemplateTypeInstantiator Instantiator(*this, TemplateArgs, Loc, Entity);
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000630 return Instantiator(T);
Douglas Gregor74296542009-02-27 19:31:52 +0000631}
Douglas Gregored3a3982009-03-03 04:44:36 +0000632
633/// \brief Instantiate the base class specifiers of the given class
634/// template specialization.
635///
636/// Produces a diagnostic and returns true on error, returns false and
637/// attaches the instantiated base classes to the class template
638/// specialization if successful.
639bool
Douglas Gregorcc887972009-03-25 21:17:03 +0000640Sema::InstantiateBaseSpecifiers(CXXRecordDecl *Instantiation,
641 CXXRecordDecl *Pattern,
Douglas Gregorf9e7d3d2009-05-11 23:53:27 +0000642 const TemplateArgumentList &TemplateArgs) {
Douglas Gregored3a3982009-03-03 04:44:36 +0000643 bool Invalid = false;
644 llvm::SmallVector<CXXBaseSpecifier*, 8> InstantiatedBases;
Douglas Gregorcc887972009-03-25 21:17:03 +0000645 for (ClassTemplateSpecializationDecl::base_class_iterator
646 Base = Pattern->bases_begin(), BaseEnd = Pattern->bases_end();
Douglas Gregord9572a12009-03-10 18:52:44 +0000647 Base != BaseEnd; ++Base) {
Douglas Gregored3a3982009-03-03 04:44:36 +0000648 if (!Base->getType()->isDependentType()) {
649 // FIXME: Allocate via ASTContext
650 InstantiatedBases.push_back(new CXXBaseSpecifier(*Base));
651 continue;
652 }
653
654 QualType BaseType = InstantiateType(Base->getType(),
Douglas Gregorf9e7d3d2009-05-11 23:53:27 +0000655 TemplateArgs,
Douglas Gregored3a3982009-03-03 04:44:36 +0000656 Base->getSourceRange().getBegin(),
657 DeclarationName());
658 if (BaseType.isNull()) {
659 Invalid = true;
660 continue;
661 }
662
663 if (CXXBaseSpecifier *InstantiatedBase
Douglas Gregorcc887972009-03-25 21:17:03 +0000664 = CheckBaseSpecifier(Instantiation,
Douglas Gregored3a3982009-03-03 04:44:36 +0000665 Base->getSourceRange(),
666 Base->isVirtual(),
667 Base->getAccessSpecifierAsWritten(),
668 BaseType,
669 /*FIXME: Not totally accurate */
670 Base->getSourceRange().getBegin()))
671 InstantiatedBases.push_back(InstantiatedBase);
672 else
673 Invalid = true;
674 }
675
Douglas Gregord9572a12009-03-10 18:52:44 +0000676 if (!Invalid &&
Douglas Gregorcc887972009-03-25 21:17:03 +0000677 AttachBaseSpecifiers(Instantiation, &InstantiatedBases[0],
Douglas Gregored3a3982009-03-03 04:44:36 +0000678 InstantiatedBases.size()))
679 Invalid = true;
680
681 return Invalid;
682}
683
Douglas Gregorcc887972009-03-25 21:17:03 +0000684/// \brief Instantiate the definition of a class from a given pattern.
685///
686/// \param PointOfInstantiation The point of instantiation within the
687/// source code.
688///
689/// \param Instantiation is the declaration whose definition is being
690/// instantiated. This will be either a class template specialization
691/// or a member class of a class template specialization.
692///
693/// \param Pattern is the pattern from which the instantiation
694/// occurs. This will be either the declaration of a class template or
695/// the declaration of a member class of a class template.
696///
697/// \param TemplateArgs The template arguments to be substituted into
698/// the pattern.
699///
Douglas Gregorcc887972009-03-25 21:17:03 +0000700/// \returns true if an error occurred, false otherwise.
701bool
702Sema::InstantiateClass(SourceLocation PointOfInstantiation,
703 CXXRecordDecl *Instantiation, CXXRecordDecl *Pattern,
Douglas Gregorfd79ac62009-05-13 00:25:59 +0000704 const TemplateArgumentList &TemplateArgs,
705 bool ExplicitInstantiation) {
Douglas Gregorcc887972009-03-25 21:17:03 +0000706 bool Invalid = false;
707
708 CXXRecordDecl *PatternDef
709 = cast_or_null<CXXRecordDecl>(Pattern->getDefinition(Context));
710 if (!PatternDef) {
711 if (Pattern == Instantiation->getInstantiatedFromMemberClass()) {
712 Diag(PointOfInstantiation,
713 diag::err_implicit_instantiate_member_undefined)
714 << Context.getTypeDeclType(Instantiation);
715 Diag(Pattern->getLocation(), diag::note_member_of_template_here);
716 } else {
Douglas Gregorfd79ac62009-05-13 00:25:59 +0000717 Diag(PointOfInstantiation, diag::err_template_instantiate_undefined)
718 << ExplicitInstantiation
Douglas Gregorcc887972009-03-25 21:17:03 +0000719 << Context.getTypeDeclType(Instantiation);
720 Diag(Pattern->getLocation(), diag::note_template_decl_here);
721 }
722 return true;
723 }
724 Pattern = PatternDef;
725
Douglas Gregor42c48522009-03-25 21:23:52 +0000726 InstantiatingTemplate Inst(*this, PointOfInstantiation, Instantiation);
Douglas Gregorcc887972009-03-25 21:17:03 +0000727 if (Inst)
728 return true;
729
730 // Enter the scope of this instantiation. We don't use
731 // PushDeclContext because we don't have a scope.
732 DeclContext *PreviousContext = CurContext;
733 CurContext = Instantiation;
734
735 // Start the definition of this instantiation.
736 Instantiation->startDefinition();
737
738 // Instantiate the base class specifiers.
Douglas Gregorf9e7d3d2009-05-11 23:53:27 +0000739 if (InstantiateBaseSpecifiers(Instantiation, Pattern, TemplateArgs))
Douglas Gregorcc887972009-03-25 21:17:03 +0000740 Invalid = true;
741
Chris Lattner5261d0c2009-03-28 19:18:32 +0000742 llvm::SmallVector<DeclPtrTy, 32> Fields;
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000743 for (RecordDecl::decl_iterator Member = Pattern->decls_begin(Context),
744 MemberEnd = Pattern->decls_end(Context);
745 Member != MemberEnd; ++Member) {
Douglas Gregorf9e7d3d2009-05-11 23:53:27 +0000746 Decl *NewMember = InstantiateDecl(*Member, Instantiation, TemplateArgs);
Douglas Gregorcc887972009-03-25 21:17:03 +0000747 if (NewMember) {
748 if (NewMember->isInvalidDecl())
749 Invalid = true;
750 else if (FieldDecl *Field = dyn_cast<FieldDecl>(NewMember))
Chris Lattner5261d0c2009-03-28 19:18:32 +0000751 Fields.push_back(DeclPtrTy::make(Field));
Douglas Gregorcc887972009-03-25 21:17:03 +0000752 } else {
753 // FIXME: Eventually, a NULL return will mean that one of the
Mike Stumpe127ae32009-05-16 07:39:55 +0000754 // instantiations was a semantic disaster, and we'll want to set Invalid =
755 // true. For now, we expect to skip some members that we can't yet handle.
Douglas Gregorcc887972009-03-25 21:17:03 +0000756 }
757 }
758
759 // Finish checking fields.
Chris Lattner5261d0c2009-03-28 19:18:32 +0000760 ActOnFields(0, Instantiation->getLocation(), DeclPtrTy::make(Instantiation),
Douglas Gregorcc887972009-03-25 21:17:03 +0000761 &Fields[0], Fields.size(), SourceLocation(), SourceLocation(),
762 0);
763
764 // Add any implicitly-declared members that we might need.
765 AddImplicitlyDeclaredMembersToClass(Instantiation);
766
767 // Exit the scope of this instantiation.
768 CurContext = PreviousContext;
769
Douglas Gregordf9f5d12009-05-13 20:28:22 +0000770 // If this is an explicit instantiation, instantiate our members, too.
771 if (!Invalid && ExplicitInstantiation)
772 InstantiateClassMembers(PointOfInstantiation, Instantiation, TemplateArgs);
773
Douglas Gregorcc887972009-03-25 21:17:03 +0000774 return Invalid;
775}
776
Douglas Gregored3a3982009-03-03 04:44:36 +0000777bool
778Sema::InstantiateClassTemplateSpecialization(
779 ClassTemplateSpecializationDecl *ClassTemplateSpec,
780 bool ExplicitInstantiation) {
781 // Perform the actual instantiation on the canonical declaration.
782 ClassTemplateSpec = cast<ClassTemplateSpecializationDecl>(
783 Context.getCanonicalDecl(ClassTemplateSpec));
784
785 // We can only instantiate something that hasn't already been
786 // instantiated or specialized. Fail without any diagnostics: our
787 // caller will provide an error message.
788 if (ClassTemplateSpec->getSpecializationKind() != TSK_Undeclared)
789 return true;
790
Mike Stumpe127ae32009-05-16 07:39:55 +0000791 // FIXME: Push this class template instantiation onto the instantiation stack,
792 // checking for recursion that exceeds a certain depth.
Douglas Gregored3a3982009-03-03 04:44:36 +0000793
Mike Stumpe127ae32009-05-16 07:39:55 +0000794 // FIXME: Perform class template partial specialization to select the best
795 // template.
Douglas Gregored3a3982009-03-03 04:44:36 +0000796 ClassTemplateDecl *Template = ClassTemplateSpec->getSpecializedTemplate();
797
Douglas Gregorcc887972009-03-25 21:17:03 +0000798 CXXRecordDecl *Pattern = Template->getTemplatedDecl();
Douglas Gregored3a3982009-03-03 04:44:36 +0000799
800 // Note that this is an instantiation.
801 ClassTemplateSpec->setSpecializationKind(
802 ExplicitInstantiation? TSK_ExplicitInstantiation
803 : TSK_ImplicitInstantiation);
804
Douglas Gregorcc887972009-03-25 21:17:03 +0000805 return InstantiateClass(ClassTemplateSpec->getLocation(),
806 ClassTemplateSpec, Pattern,
Douglas Gregorfd79ac62009-05-13 00:25:59 +0000807 ClassTemplateSpec->getTemplateArgs(),
808 ExplicitInstantiation);
Douglas Gregored3a3982009-03-03 04:44:36 +0000809}
Douglas Gregor47bde7c2009-03-19 17:26:29 +0000810
Douglas Gregordf9f5d12009-05-13 20:28:22 +0000811/// \brief Instantiate the definitions of all of the member of the
812/// given class, which is an instantiation of a class template or a
813/// member class of a template.
814void
815Sema::InstantiateClassMembers(SourceLocation PointOfInstantiation,
816 CXXRecordDecl *Instantiation,
817 const TemplateArgumentList &TemplateArgs) {
818 for (DeclContext::decl_iterator D = Instantiation->decls_begin(Context),
819 DEnd = Instantiation->decls_end(Context);
820 D != DEnd; ++D) {
821 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(*D)) {
822 if (!Function->getBody(Context))
823 InstantiateFunctionDefinition(Function);
824 } else if (VarDecl *Var = dyn_cast<VarDecl>(*D)) {
825 const VarDecl *Def = 0;
826 if (!Var->getDefinition(Def))
827 InstantiateVariableDefinition(Var);
828 } else if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(*D)) {
829 if (!Record->isInjectedClassName() && !Record->getDefinition(Context)) {
830 assert(Record->getInstantiatedFromMemberClass() &&
831 "Missing instantiated-from-template information");
832 InstantiateClass(Record->getLocation(), Record,
833 Record->getInstantiatedFromMemberClass(),
834 TemplateArgs, true);
835 }
836 }
837 }
838}
839
840/// \brief Instantiate the definitions of all of the members of the
841/// given class template specialization, which was named as part of an
842/// explicit instantiation.
843void Sema::InstantiateClassTemplateSpecializationMembers(
844 SourceLocation PointOfInstantiation,
845 ClassTemplateSpecializationDecl *ClassTemplateSpec) {
846 // C++0x [temp.explicit]p7:
847 // An explicit instantiation that names a class template
848 // specialization is an explicit instantion of the same kind
849 // (declaration or definition) of each of its members (not
850 // including members inherited from base classes) that has not
851 // been previously explicitly specialized in the translation unit
852 // containing the explicit instantiation, except as described
853 // below.
854 InstantiateClassMembers(PointOfInstantiation, ClassTemplateSpec,
855 ClassTemplateSpec->getTemplateArgs());
856}
857
Douglas Gregor1e589cc2009-03-26 23:50:42 +0000858/// \brief Instantiate a nested-name-specifier.
859NestedNameSpecifier *
860Sema::InstantiateNestedNameSpecifier(NestedNameSpecifier *NNS,
861 SourceRange Range,
Douglas Gregorf9e7d3d2009-05-11 23:53:27 +0000862 const TemplateArgumentList &TemplateArgs) {
Douglas Gregor1e589cc2009-03-26 23:50:42 +0000863 // Instantiate the prefix of this nested name specifier.
864 NestedNameSpecifier *Prefix = NNS->getPrefix();
865 if (Prefix) {
Douglas Gregorf9e7d3d2009-05-11 23:53:27 +0000866 Prefix = InstantiateNestedNameSpecifier(Prefix, Range, TemplateArgs);
Douglas Gregor1e589cc2009-03-26 23:50:42 +0000867 if (!Prefix)
868 return 0;
Douglas Gregor47bde7c2009-03-19 17:26:29 +0000869 }
870
Douglas Gregor1e589cc2009-03-26 23:50:42 +0000871 switch (NNS->getKind()) {
Douglas Gregor77da5802009-04-01 00:28:59 +0000872 case NestedNameSpecifier::Identifier: {
873 assert(Prefix &&
874 "Can't have an identifier nested-name-specifier with no prefix");
875 CXXScopeSpec SS;
876 // FIXME: The source location information is all wrong.
877 SS.setRange(Range);
878 SS.setScopeRep(Prefix);
879 return static_cast<NestedNameSpecifier *>(
880 ActOnCXXNestedNameSpecifier(0, SS,
881 Range.getEnd(),
882 Range.getEnd(),
Douglas Gregor96b6df92009-05-14 00:28:11 +0000883 *NNS->getAsIdentifier()));
Douglas Gregor1e589cc2009-03-26 23:50:42 +0000884 break;
Douglas Gregor77da5802009-04-01 00:28:59 +0000885 }
Douglas Gregor1e589cc2009-03-26 23:50:42 +0000886
887 case NestedNameSpecifier::Namespace:
888 case NestedNameSpecifier::Global:
889 return NNS;
890
891 case NestedNameSpecifier::TypeSpecWithTemplate:
892 case NestedNameSpecifier::TypeSpec: {
893 QualType T = QualType(NNS->getAsType(), 0);
894 if (!T->isDependentType())
895 return NNS;
896
Douglas Gregorf9e7d3d2009-05-11 23:53:27 +0000897 T = InstantiateType(T, TemplateArgs, Range.getBegin(), DeclarationName());
Douglas Gregor1e589cc2009-03-26 23:50:42 +0000898 if (T.isNull())
899 return 0;
900
Douglas Gregord3022602009-03-27 23:10:48 +0000901 if (T->isRecordType() ||
902 (getLangOptions().CPlusPlus0x && T->isEnumeralType())) {
Douglas Gregor77da5802009-04-01 00:28:59 +0000903 assert(T.getCVRQualifiers() == 0 && "Can't get cv-qualifiers here");
Douglas Gregord3022602009-03-27 23:10:48 +0000904 return NestedNameSpecifier::Create(Context, Prefix,
Douglas Gregor1e589cc2009-03-26 23:50:42 +0000905 NNS->getKind() == NestedNameSpecifier::TypeSpecWithTemplate,
Douglas Gregord3022602009-03-27 23:10:48 +0000906 T.getTypePtr());
907 }
908
909 Diag(Range.getBegin(), diag::err_nested_name_spec_non_tag) << T;
910 return 0;
Douglas Gregor1e589cc2009-03-26 23:50:42 +0000911 }
912 }
913
Douglas Gregord3022602009-03-27 23:10:48 +0000914 // Required to silence a GCC warning
Douglas Gregor1e589cc2009-03-26 23:50:42 +0000915 return 0;
Douglas Gregor47bde7c2009-03-19 17:26:29 +0000916}
Douglas Gregor15a92852009-03-31 18:38:02 +0000917
918TemplateName
919Sema::InstantiateTemplateName(TemplateName Name, SourceLocation Loc,
Douglas Gregorf9e7d3d2009-05-11 23:53:27 +0000920 const TemplateArgumentList &TemplateArgs) {
Douglas Gregor15a92852009-03-31 18:38:02 +0000921 if (TemplateTemplateParmDecl *TTP
922 = dyn_cast_or_null<TemplateTemplateParmDecl>(
923 Name.getAsTemplateDecl())) {
924 assert(TTP->getDepth() == 0 &&
925 "Cannot reduce depth of a template template parameter");
Douglas Gregoraed98042009-03-31 20:22:05 +0000926 assert(TemplateArgs[TTP->getPosition()].getAsDecl() &&
Douglas Gregor15a92852009-03-31 18:38:02 +0000927 "Wrong kind of template template argument");
928 ClassTemplateDecl *ClassTemplate
929 = dyn_cast<ClassTemplateDecl>(
930 TemplateArgs[TTP->getPosition()].getAsDecl());
Douglas Gregoraed98042009-03-31 20:22:05 +0000931 assert(ClassTemplate && "Expected a class template");
Douglas Gregor15a92852009-03-31 18:38:02 +0000932 if (QualifiedTemplateName *QTN = Name.getAsQualifiedTemplateName()) {
933 NestedNameSpecifier *NNS
934 = InstantiateNestedNameSpecifier(QTN->getQualifier(),
935 /*FIXME=*/SourceRange(Loc),
Douglas Gregorf9e7d3d2009-05-11 23:53:27 +0000936 TemplateArgs);
Douglas Gregor15a92852009-03-31 18:38:02 +0000937 if (NNS)
938 return Context.getQualifiedTemplateName(NNS,
939 QTN->hasTemplateKeyword(),
940 ClassTemplate);
941 }
942
943 return TemplateName(ClassTemplate);
944 } else if (DependentTemplateName *DTN = Name.getAsDependentTemplateName()) {
945 NestedNameSpecifier *NNS
946 = InstantiateNestedNameSpecifier(DTN->getQualifier(),
947 /*FIXME=*/SourceRange(Loc),
Douglas Gregorf9e7d3d2009-05-11 23:53:27 +0000948 TemplateArgs);
Douglas Gregor15a92852009-03-31 18:38:02 +0000949
950 if (!NNS) // FIXME: Not the best recovery strategy.
951 return Name;
952
953 if (NNS->isDependent())
954 return Context.getDependentTemplateName(NNS, DTN->getName());
955
956 // Somewhat redundant with ActOnDependentTemplateName.
957 CXXScopeSpec SS;
958 SS.setRange(SourceRange(Loc));
959 SS.setScopeRep(NNS);
960 TemplateTy Template;
961 TemplateNameKind TNK = isTemplateName(*DTN->getName(), 0, Template, &SS);
962 if (TNK == TNK_Non_template) {
963 Diag(Loc, diag::err_template_kw_refers_to_non_template)
964 << DTN->getName();
965 return Name;
966 } else if (TNK == TNK_Function_template) {
967 Diag(Loc, diag::err_template_kw_refers_to_non_template)
968 << DTN->getName();
969 return Name;
970 }
971
972 return Template.getAsVal<TemplateName>();
973 }
974
975
976
Mike Stumpe127ae32009-05-16 07:39:55 +0000977 // FIXME: Even if we're referring to a Decl that isn't a template template
978 // parameter, we may need to instantiate the outer contexts of that
979 // Decl. However, this won't be needed until we implement member templates.
Douglas Gregor15a92852009-03-31 18:38:02 +0000980 return Name;
981}