blob: ac8f293a803199c92f457a4736cf23b0f9d5fe61 [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"
14#include "clang/AST/ASTContext.h"
15#include "clang/AST/Expr.h"
Douglas Gregor99ebf652009-02-27 19:31:52 +000016#include "clang/AST/DeclTemplate.h"
17#include "clang/Parse/DeclSpec.h"
18#include "clang/Basic/LangOptions.h"
Douglas Gregorcd281c32009-02-28 00:25:32 +000019#include "llvm/Support/Compiler.h"
Douglas Gregor99ebf652009-02-27 19:31:52 +000020
21using namespace clang;
22
Douglas Gregoree1828a2009-03-10 18:03:33 +000023//===----------------------------------------------------------------------===/
24// Template Instantiation Support
25//===----------------------------------------------------------------------===/
26
Douglas Gregor26dce442009-03-10 00:06:19 +000027Sema::InstantiatingTemplate::
28InstantiatingTemplate(Sema &SemaRef, SourceLocation PointOfInstantiation,
Douglas Gregord475b8d2009-03-25 21:17:03 +000029 CXXRecordDecl *Entity,
Douglas Gregor26dce442009-03-10 00:06:19 +000030 SourceRange InstantiationRange)
31 : SemaRef(SemaRef) {
Douglas Gregordf667e72009-03-10 20:44:00 +000032
33 Invalid = CheckInstantiationDepth(PointOfInstantiation,
34 InstantiationRange);
35 if (!Invalid) {
Douglas Gregor26dce442009-03-10 00:06:19 +000036 ActiveTemplateInstantiation Inst;
Douglas Gregordf667e72009-03-10 20:44:00 +000037 Inst.Kind = ActiveTemplateInstantiation::TemplateInstantiation;
Douglas Gregor26dce442009-03-10 00:06:19 +000038 Inst.PointOfInstantiation = PointOfInstantiation;
Douglas Gregordf667e72009-03-10 20:44:00 +000039 Inst.Entity = reinterpret_cast<uintptr_t>(Entity);
Douglas Gregor313a81d2009-03-12 18:36:18 +000040 Inst.TemplateArgs = 0;
41 Inst.NumTemplateArgs = 0;
Douglas Gregordf667e72009-03-10 20:44:00 +000042 Inst.InstantiationRange = InstantiationRange;
43 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
44 Invalid = false;
45 }
46}
47
48Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef,
49 SourceLocation PointOfInstantiation,
50 TemplateDecl *Template,
51 const TemplateArgument *TemplateArgs,
52 unsigned NumTemplateArgs,
53 SourceRange InstantiationRange)
54 : SemaRef(SemaRef) {
55
56 Invalid = CheckInstantiationDepth(PointOfInstantiation,
57 InstantiationRange);
58 if (!Invalid) {
59 ActiveTemplateInstantiation Inst;
60 Inst.Kind
61 = ActiveTemplateInstantiation::DefaultTemplateArgumentInstantiation;
62 Inst.PointOfInstantiation = PointOfInstantiation;
63 Inst.Entity = reinterpret_cast<uintptr_t>(Template);
64 Inst.TemplateArgs = TemplateArgs;
65 Inst.NumTemplateArgs = NumTemplateArgs;
Douglas Gregor26dce442009-03-10 00:06:19 +000066 Inst.InstantiationRange = InstantiationRange;
67 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
68 Invalid = false;
69 }
70}
71
72Sema::InstantiatingTemplate::~InstantiatingTemplate() {
73 if (!Invalid)
74 SemaRef.ActiveTemplateInstantiations.pop_back();
75}
76
Douglas Gregordf667e72009-03-10 20:44:00 +000077bool Sema::InstantiatingTemplate::CheckInstantiationDepth(
78 SourceLocation PointOfInstantiation,
79 SourceRange InstantiationRange) {
80 if (SemaRef.ActiveTemplateInstantiations.size()
81 <= SemaRef.getLangOptions().InstantiationDepth)
82 return false;
83
84 SemaRef.Diag(PointOfInstantiation,
85 diag::err_template_recursion_depth_exceeded)
86 << SemaRef.getLangOptions().InstantiationDepth
87 << InstantiationRange;
88 SemaRef.Diag(PointOfInstantiation, diag::note_template_recursion_depth)
89 << SemaRef.getLangOptions().InstantiationDepth;
90 return true;
91}
92
Douglas Gregoree1828a2009-03-10 18:03:33 +000093/// \brief Prints the current instantiation stack through a series of
94/// notes.
95void Sema::PrintInstantiationStack() {
96 for (llvm::SmallVector<ActiveTemplateInstantiation, 16>::reverse_iterator
97 Active = ActiveTemplateInstantiations.rbegin(),
98 ActiveEnd = ActiveTemplateInstantiations.rend();
99 Active != ActiveEnd;
100 ++Active) {
Douglas Gregordf667e72009-03-10 20:44:00 +0000101 switch (Active->Kind) {
102 case ActiveTemplateInstantiation::TemplateInstantiation: {
Douglas Gregord475b8d2009-03-25 21:17:03 +0000103 unsigned DiagID = diag::note_template_member_class_here;
104 CXXRecordDecl *Record = (CXXRecordDecl *)Active->Entity;
105 if (isa<ClassTemplateSpecializationDecl>(Record))
106 DiagID = diag::note_template_class_instantiation_here;
Douglas Gregordf667e72009-03-10 20:44:00 +0000107 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
Douglas Gregord475b8d2009-03-25 21:17:03 +0000108 DiagID)
109 << Context.getTypeDeclType(Record)
Douglas Gregordf667e72009-03-10 20:44:00 +0000110 << Active->InstantiationRange;
111 break;
112 }
113
114 case ActiveTemplateInstantiation::DefaultTemplateArgumentInstantiation: {
115 TemplateDecl *Template = cast<TemplateDecl>((Decl *)Active->Entity);
116 std::string TemplateArgsStr
Douglas Gregor7532dc62009-03-30 22:58:21 +0000117 = TemplateSpecializationType::PrintTemplateArgumentList(
Douglas Gregordf667e72009-03-10 20:44:00 +0000118 Active->TemplateArgs,
119 Active->NumTemplateArgs);
120 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
121 diag::note_default_arg_instantiation_here)
122 << (Template->getNameAsString() + TemplateArgsStr)
123 << Active->InstantiationRange;
124 break;
125 }
126 }
Douglas Gregoree1828a2009-03-10 18:03:33 +0000127 }
128}
129
Douglas Gregor99ebf652009-02-27 19:31:52 +0000130//===----------------------------------------------------------------------===/
131// Template Instantiation for Types
132//===----------------------------------------------------------------------===/
Douglas Gregorcd281c32009-02-28 00:25:32 +0000133namespace {
134 class VISIBILITY_HIDDEN TemplateTypeInstantiator {
135 Sema &SemaRef;
136 const TemplateArgument *TemplateArgs;
137 unsigned NumTemplateArgs;
138 SourceLocation Loc;
139 DeclarationName Entity;
Douglas Gregor99ebf652009-02-27 19:31:52 +0000140
Douglas Gregorcd281c32009-02-28 00:25:32 +0000141 public:
142 TemplateTypeInstantiator(Sema &SemaRef,
143 const TemplateArgument *TemplateArgs,
144 unsigned NumTemplateArgs,
145 SourceLocation Loc,
146 DeclarationName Entity)
147 : SemaRef(SemaRef), TemplateArgs(TemplateArgs),
148 NumTemplateArgs(NumTemplateArgs), Loc(Loc), Entity(Entity) { }
149
150 QualType operator()(QualType T) const { return Instantiate(T); }
151
152 QualType Instantiate(QualType T) const;
153
154 // Declare instantiate functions for each type.
155#define TYPE(Class, Base) \
156 QualType Instantiate##Class##Type(const Class##Type *T, \
157 unsigned Quals) const;
158#define ABSTRACT_TYPE(Class, Base)
159#include "clang/AST/TypeNodes.def"
160 };
161}
162
163QualType
164TemplateTypeInstantiator::InstantiateExtQualType(const ExtQualType *T,
165 unsigned Quals) const {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000166 // FIXME: Implement this
167 assert(false && "Cannot instantiate ExtQualType yet");
168 return QualType();
169}
170
Douglas Gregorcd281c32009-02-28 00:25:32 +0000171QualType
172TemplateTypeInstantiator::InstantiateBuiltinType(const BuiltinType *T,
173 unsigned Quals) const {
Douglas Gregor724651c2009-02-28 01:04:19 +0000174 assert(false && "Builtin types are not dependent and cannot be instantiated");
Douglas Gregorcd281c32009-02-28 00:25:32 +0000175 return QualType(T, Quals);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000176}
177
Douglas Gregorcd281c32009-02-28 00:25:32 +0000178QualType
179TemplateTypeInstantiator::
180InstantiateFixedWidthIntType(const FixedWidthIntType *T, unsigned Quals) const {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000181 // FIXME: Implement this
182 assert(false && "Cannot instantiate FixedWidthIntType yet");
183 return QualType();
184}
185
Douglas Gregorcd281c32009-02-28 00:25:32 +0000186QualType
187TemplateTypeInstantiator::InstantiateComplexType(const ComplexType *T,
188 unsigned Quals) const {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000189 // FIXME: Implement this
190 assert(false && "Cannot instantiate ComplexType yet");
191 return QualType();
192}
193
Douglas Gregorcd281c32009-02-28 00:25:32 +0000194QualType
195TemplateTypeInstantiator::InstantiatePointerType(const PointerType *T,
196 unsigned Quals) const {
197 QualType PointeeType = Instantiate(T->getPointeeType());
198 if (PointeeType.isNull())
199 return QualType();
200
201 return SemaRef.BuildPointerType(PointeeType, Quals, Loc, Entity);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000202}
203
Douglas Gregorcd281c32009-02-28 00:25:32 +0000204QualType
205TemplateTypeInstantiator::InstantiateBlockPointerType(const BlockPointerType *T,
206 unsigned Quals) const {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000207 // FIXME: Implement this
208 assert(false && "Cannot instantiate BlockPointerType yet");
209 return QualType();
210}
211
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000212QualType
213TemplateTypeInstantiator::InstantiateLValueReferenceType(
214 const LValueReferenceType *T, unsigned Quals) const {
Douglas Gregorcd281c32009-02-28 00:25:32 +0000215 QualType ReferentType = Instantiate(T->getPointeeType());
216 if (ReferentType.isNull())
217 return QualType();
218
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000219 return SemaRef.BuildReferenceType(ReferentType, true, Quals, Loc, Entity);
220}
221
222QualType
223TemplateTypeInstantiator::InstantiateRValueReferenceType(
224 const RValueReferenceType *T, unsigned Quals) const {
225 QualType ReferentType = Instantiate(T->getPointeeType());
226 if (ReferentType.isNull())
227 return QualType();
228
229 return SemaRef.BuildReferenceType(ReferentType, false, Quals, Loc, Entity);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000230}
231
Douglas Gregorcd281c32009-02-28 00:25:32 +0000232QualType
233TemplateTypeInstantiator::
234InstantiateMemberPointerType(const MemberPointerType *T,
235 unsigned Quals) const {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000236 // FIXME: Implement this
237 assert(false && "Cannot instantiate MemberPointerType yet");
238 return QualType();
239}
240
Douglas Gregorcd281c32009-02-28 00:25:32 +0000241QualType
242TemplateTypeInstantiator::
243InstantiateConstantArrayType(const ConstantArrayType *T,
244 unsigned Quals) const {
245 QualType ElementType = Instantiate(T->getElementType());
246 if (ElementType.isNull())
247 return ElementType;
248
249 // Build a temporary integer literal to specify the size for
250 // BuildArrayType. Since we have already checked the size as part of
251 // creating the dependent array type in the first place, we know
252 // there aren't any errors.
Douglas Gregor8d217212009-03-09 20:07:22 +0000253 // FIXME: Is IntTy big enough? Maybe not, but LongLongTy causes
254 // problems that I have yet to investigate.
255 IntegerLiteral ArraySize(T->getSize(), SemaRef.Context.IntTy, Loc);
Douglas Gregorcd281c32009-02-28 00:25:32 +0000256 return SemaRef.BuildArrayType(ElementType, T->getSizeModifier(),
257 &ArraySize, T->getIndexTypeQualifier(),
258 Loc, Entity);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000259}
260
Douglas Gregorcd281c32009-02-28 00:25:32 +0000261QualType
262TemplateTypeInstantiator::
263InstantiateIncompleteArrayType(const IncompleteArrayType *T,
264 unsigned Quals) const {
265 QualType ElementType = Instantiate(T->getElementType());
266 if (ElementType.isNull())
267 return ElementType;
268
269 return SemaRef.BuildArrayType(ElementType, T->getSizeModifier(),
270 0, T->getIndexTypeQualifier(),
271 Loc, Entity);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000272}
273
Douglas Gregorcd281c32009-02-28 00:25:32 +0000274QualType
275TemplateTypeInstantiator::
276InstantiateVariableArrayType(const VariableArrayType *T,
277 unsigned Quals) const {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000278 // FIXME: Implement this
279 assert(false && "Cannot instantiate VariableArrayType yet");
280 return QualType();
281}
282
Douglas Gregorcd281c32009-02-28 00:25:32 +0000283QualType
284TemplateTypeInstantiator::
285InstantiateDependentSizedArrayType(const DependentSizedArrayType *T,
286 unsigned Quals) const {
Anders Carlsson76b1c842009-03-15 20:12:13 +0000287 Expr *ArraySize = T->getSizeExpr();
288 assert(ArraySize->isValueDependent() &&
289 "dependent sized array types must have value dependent size expr");
290
291 // Instantiate the element type if needed
292 QualType ElementType = T->getElementType();
293 if (ElementType->isDependentType()) {
294 ElementType = Instantiate(ElementType);
295 if (ElementType.isNull())
296 return QualType();
297 }
298
299 // Instantiate the size expression
300 Sema::OwningExprResult InstantiatedArraySize =
301 SemaRef.InstantiateExpr(ArraySize, TemplateArgs, NumTemplateArgs);
302 if (InstantiatedArraySize.isInvalid())
303 return QualType();
304
305 return SemaRef.BuildArrayType(ElementType, T->getSizeModifier(),
Anders Carlssone9146f22009-05-01 19:49:17 +0000306 InstantiatedArraySize.takeAs<Expr>(),
Anders Carlsson76b1c842009-03-15 20:12:13 +0000307 T->getIndexTypeQualifier(), Loc, Entity);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000308}
309
Douglas Gregorcd281c32009-02-28 00:25:32 +0000310QualType
311TemplateTypeInstantiator::InstantiateVectorType(const VectorType *T,
312 unsigned Quals) const {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000313 // FIXME: Implement this
314 assert(false && "Cannot instantiate VectorType yet");
315 return QualType();
316}
317
Douglas Gregorcd281c32009-02-28 00:25:32 +0000318QualType
319TemplateTypeInstantiator::InstantiateExtVectorType(const ExtVectorType *T,
320 unsigned Quals) const {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000321 // FIXME: Implement this
322 assert(false && "Cannot instantiate ExtVectorType yet");
323 return QualType();
324}
325
Douglas Gregorcd281c32009-02-28 00:25:32 +0000326QualType
327TemplateTypeInstantiator::
328InstantiateFunctionProtoType(const FunctionProtoType *T,
329 unsigned Quals) const {
Douglas Gregor724651c2009-02-28 01:04:19 +0000330 QualType ResultType = Instantiate(T->getResultType());
331 if (ResultType.isNull())
332 return ResultType;
333
334 llvm::SmallVector<QualType, 16> ParamTypes;
335 for (FunctionProtoType::arg_type_iterator Param = T->arg_type_begin(),
336 ParamEnd = T->arg_type_end();
337 Param != ParamEnd; ++Param) {
338 QualType P = Instantiate(*Param);
339 if (P.isNull())
340 return P;
341
342 ParamTypes.push_back(P);
343 }
344
345 return SemaRef.BuildFunctionType(ResultType, &ParamTypes[0],
346 ParamTypes.size(),
347 T->isVariadic(), T->getTypeQuals(),
348 Loc, Entity);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000349}
350
Douglas Gregorcd281c32009-02-28 00:25:32 +0000351QualType
352TemplateTypeInstantiator::
353InstantiateFunctionNoProtoType(const FunctionNoProtoType *T,
354 unsigned Quals) const {
Douglas Gregor724651c2009-02-28 01:04:19 +0000355 assert(false && "Functions without prototypes cannot be dependent.");
Douglas Gregor99ebf652009-02-27 19:31:52 +0000356 return QualType();
357}
358
Douglas Gregorcd281c32009-02-28 00:25:32 +0000359QualType
360TemplateTypeInstantiator::InstantiateTypedefType(const TypedefType *T,
361 unsigned Quals) const {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000362 // FIXME: Implement this
363 assert(false && "Cannot instantiate TypedefType yet");
364 return QualType();
365}
366
Douglas Gregorcd281c32009-02-28 00:25:32 +0000367QualType
368TemplateTypeInstantiator::InstantiateTypeOfExprType(const TypeOfExprType *T,
369 unsigned Quals) const {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000370 // FIXME: Implement this
371 assert(false && "Cannot instantiate TypeOfExprType yet");
372 return QualType();
373}
374
Douglas Gregorcd281c32009-02-28 00:25:32 +0000375QualType
376TemplateTypeInstantiator::InstantiateTypeOfType(const TypeOfType *T,
377 unsigned Quals) const {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000378 // FIXME: Implement this
379 assert(false && "Cannot instantiate TypeOfType yet");
380 return QualType();
381}
382
Douglas Gregorcd281c32009-02-28 00:25:32 +0000383QualType
384TemplateTypeInstantiator::InstantiateRecordType(const RecordType *T,
385 unsigned Quals) const {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000386 // FIXME: Implement this
387 assert(false && "Cannot instantiate RecordType yet");
388 return QualType();
389}
390
Douglas Gregorcd281c32009-02-28 00:25:32 +0000391QualType
Douglas Gregorcd281c32009-02-28 00:25:32 +0000392TemplateTypeInstantiator::InstantiateEnumType(const EnumType *T,
393 unsigned Quals) const {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000394 // FIXME: Implement this
395 assert(false && "Cannot instantiate EnumType yet");
396 return QualType();
397}
398
Douglas Gregorcd281c32009-02-28 00:25:32 +0000399QualType
400TemplateTypeInstantiator::
401InstantiateTemplateTypeParmType(const TemplateTypeParmType *T,
402 unsigned Quals) const {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000403 if (T->getDepth() == 0) {
404 // Replace the template type parameter with its corresponding
405 // template argument.
406 assert(T->getIndex() < NumTemplateArgs && "Wrong # of template args");
407 assert(TemplateArgs[T->getIndex()].getKind() == TemplateArgument::Type &&
408 "Template argument kind mismatch");
409 QualType Result = TemplateArgs[T->getIndex()].getAsType();
Douglas Gregorcd281c32009-02-28 00:25:32 +0000410 if (Result.isNull() || !Quals)
Douglas Gregor99ebf652009-02-27 19:31:52 +0000411 return Result;
412
413 // C++ [dcl.ref]p1:
414 // [...] Cv-qualified references are ill-formed except when
415 // the cv-qualifiers are introduced through the use of a
416 // typedef (7.1.3) or of a template type argument (14.3), in
417 // which case the cv-qualifiers are ignored.
Douglas Gregorcd281c32009-02-28 00:25:32 +0000418 if (Quals && Result->isReferenceType())
419 Quals = 0;
Douglas Gregor99ebf652009-02-27 19:31:52 +0000420
Douglas Gregorcd281c32009-02-28 00:25:32 +0000421 return QualType(Result.getTypePtr(), Quals | Result.getCVRQualifiers());
Douglas Gregor99ebf652009-02-27 19:31:52 +0000422 }
423
424 // The template type parameter comes from an inner template (e.g.,
425 // the template parameter list of a member template inside the
426 // template we are instantiating). Create a new template type
427 // parameter with the template "level" reduced by one.
428 return SemaRef.Context.getTemplateTypeParmType(T->getDepth() - 1,
429 T->getIndex(),
Douglas Gregorcd281c32009-02-28 00:25:32 +0000430 T->getName())
431 .getQualifiedType(Quals);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000432}
433
Douglas Gregorcd281c32009-02-28 00:25:32 +0000434QualType
435TemplateTypeInstantiator::
Douglas Gregor7532dc62009-03-30 22:58:21 +0000436InstantiateTemplateSpecializationType(
437 const TemplateSpecializationType *T,
Douglas Gregorcd281c32009-02-28 00:25:32 +0000438 unsigned Quals) const {
Douglas Gregor40808ce2009-03-09 23:48:35 +0000439 llvm::SmallVector<TemplateArgument, 16> InstantiatedTemplateArgs;
440 InstantiatedTemplateArgs.reserve(T->getNumArgs());
Douglas Gregor7532dc62009-03-30 22:58:21 +0000441 for (TemplateSpecializationType::iterator Arg = T->begin(), ArgEnd = T->end();
Douglas Gregor40808ce2009-03-09 23:48:35 +0000442 Arg != ArgEnd; ++Arg) {
443 switch (Arg->getKind()) {
444 case TemplateArgument::Type: {
445 QualType T = SemaRef.InstantiateType(Arg->getAsType(),
446 TemplateArgs, NumTemplateArgs,
447 Arg->getLocation(),
448 DeclarationName());
449 if (T.isNull())
450 return QualType();
451
452 InstantiatedTemplateArgs.push_back(
453 TemplateArgument(Arg->getLocation(), T));
454 break;
455 }
456
457 case TemplateArgument::Declaration:
458 case TemplateArgument::Integral:
459 InstantiatedTemplateArgs.push_back(*Arg);
460 break;
461
462 case TemplateArgument::Expression:
Douglas Gregorba498172009-03-13 21:01:28 +0000463 Sema::OwningExprResult E
464 = SemaRef.InstantiateExpr(Arg->getAsExpr(), TemplateArgs,
465 NumTemplateArgs);
466 if (E.isInvalid())
467 return QualType();
Anders Carlssonf1b1d592009-05-01 19:30:39 +0000468 InstantiatedTemplateArgs.push_back(E.takeAs<Expr>());
Douglas Gregor40808ce2009-03-09 23:48:35 +0000469 break;
470 }
471 }
472
473 // FIXME: We're missing the locations of the template name, '<', and
474 // '>'.
Douglas Gregorde650ae2009-03-31 18:38:02 +0000475
476 TemplateName Name = SemaRef.InstantiateTemplateName(T->getTemplateName(),
477 Loc,
478 TemplateArgs,
479 NumTemplateArgs);
480
481 return SemaRef.CheckTemplateIdType(Name, Loc, SourceLocation(),
Douglas Gregor7532dc62009-03-30 22:58:21 +0000482 &InstantiatedTemplateArgs[0],
483 InstantiatedTemplateArgs.size(),
484 SourceLocation());
Douglas Gregor99ebf652009-02-27 19:31:52 +0000485}
486
Douglas Gregorcd281c32009-02-28 00:25:32 +0000487QualType
488TemplateTypeInstantiator::
Douglas Gregore4e5b052009-03-19 00:18:19 +0000489InstantiateQualifiedNameType(const QualifiedNameType *T,
490 unsigned Quals) const {
Douglas Gregord57959a2009-03-27 23:10:48 +0000491 // When we instantiated a qualified name type, there's no point in
492 // keeping the qualification around in the instantiated result. So,
493 // just instantiate the named type.
494 return (*this)(T->getNamedType());
495}
496
497QualType
498TemplateTypeInstantiator::
499InstantiateTypenameType(const TypenameType *T, unsigned Quals) const {
Douglas Gregor17343172009-04-01 00:28:59 +0000500 if (const TemplateSpecializationType *TemplateId = T->getTemplateId()) {
501 // When the typename type refers to a template-id, the template-id
502 // is dependent and has enough information to instantiate the
503 // result of the typename type. Since we don't care about keeping
504 // the spelling of the typename type in template instantiations,
505 // we just instantiate the template-id.
506 return InstantiateTemplateSpecializationType(TemplateId, Quals);
507 }
508
Douglas Gregord57959a2009-03-27 23:10:48 +0000509 NestedNameSpecifier *NNS
510 = SemaRef.InstantiateNestedNameSpecifier(T->getQualifier(),
511 SourceRange(Loc),
512 TemplateArgs, NumTemplateArgs);
513 if (!NNS)
514 return QualType();
515
Douglas Gregor17343172009-04-01 00:28:59 +0000516 return SemaRef.CheckTypenameType(NNS, *T->getIdentifier(), SourceRange(Loc));
Douglas Gregore4e5b052009-03-19 00:18:19 +0000517}
518
519QualType
520TemplateTypeInstantiator::
Douglas Gregorcd281c32009-02-28 00:25:32 +0000521InstantiateObjCInterfaceType(const ObjCInterfaceType *T,
522 unsigned Quals) const {
523 assert(false && "Objective-C types cannot be dependent");
Douglas Gregor99ebf652009-02-27 19:31:52 +0000524 return QualType();
525}
526
Douglas Gregorcd281c32009-02-28 00:25:32 +0000527QualType
528TemplateTypeInstantiator::
529InstantiateObjCQualifiedInterfaceType(const ObjCQualifiedInterfaceType *T,
530 unsigned Quals) const {
531 assert(false && "Objective-C types cannot be dependent");
Douglas Gregor99ebf652009-02-27 19:31:52 +0000532 return QualType();
533}
534
Douglas Gregorcd281c32009-02-28 00:25:32 +0000535QualType
536TemplateTypeInstantiator::
537InstantiateObjCQualifiedIdType(const ObjCQualifiedIdType *T,
538 unsigned Quals) const {
539 assert(false && "Objective-C types cannot be dependent");
Douglas Gregor99ebf652009-02-27 19:31:52 +0000540 return QualType();
541}
542
Douglas Gregorcd281c32009-02-28 00:25:32 +0000543/// \brief The actual implementation of Sema::InstantiateType().
544QualType TemplateTypeInstantiator::Instantiate(QualType T) const {
545 // If T is not a dependent type, there is nothing to do.
546 if (!T->isDependentType())
547 return T;
548
549 switch (T->getTypeClass()) {
550#define TYPE(Class, Base) \
551 case Type::Class: \
552 return Instantiate##Class##Type(cast<Class##Type>(T.getTypePtr()), \
553 T.getCVRQualifiers());
554#define ABSTRACT_TYPE(Class, Base)
555#include "clang/AST/TypeNodes.def"
556 }
557
558 assert(false && "Not all types have been decoded for instantiation");
559 return QualType();
560}
Douglas Gregor99ebf652009-02-27 19:31:52 +0000561
562/// \brief Instantiate the type T with a given set of template arguments.
563///
564/// This routine substitutes the given template arguments into the
565/// type T and produces the instantiated type.
566///
567/// \param T the type into which the template arguments will be
568/// substituted. If this type is not dependent, it will be returned
569/// immediately.
570///
571/// \param TemplateArgs the template arguments that will be
572/// substituted for the top-level template parameters within T.
573///
574/// \param NumTemplateArgs the number of template arguments provided
575/// by TemplateArgs.
576///
577/// \param Loc the location in the source code where this substitution
578/// is being performed. It will typically be the location of the
579/// declarator (if we're instantiating the type of some declaration)
580/// or the location of the type in the source code (if, e.g., we're
581/// instantiating the type of a cast expression).
582///
583/// \param Entity the name of the entity associated with a declaration
584/// being instantiated (if any). May be empty to indicate that there
585/// is no such entity (if, e.g., this is a type that occurs as part of
586/// a cast expression) or that the entity has no name (e.g., an
587/// unnamed function parameter).
588///
589/// \returns If the instantiation succeeds, the instantiated
590/// type. Otherwise, produces diagnostics and returns a NULL type.
591QualType Sema::InstantiateType(QualType T,
592 const TemplateArgument *TemplateArgs,
593 unsigned NumTemplateArgs,
594 SourceLocation Loc, DeclarationName Entity) {
Douglas Gregordf667e72009-03-10 20:44:00 +0000595 assert(!ActiveTemplateInstantiations.empty() &&
596 "Cannot perform an instantiation without some context on the "
597 "instantiation stack");
598
Douglas Gregor99ebf652009-02-27 19:31:52 +0000599 // If T is not a dependent type, there is nothing to do.
600 if (!T->isDependentType())
601 return T;
602
Douglas Gregorcd281c32009-02-28 00:25:32 +0000603 TemplateTypeInstantiator Instantiator(*this, TemplateArgs, NumTemplateArgs,
604 Loc, Entity);
605 return Instantiator(T);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000606}
Douglas Gregor2943aed2009-03-03 04:44:36 +0000607
608/// \brief Instantiate the base class specifiers of the given class
609/// template specialization.
610///
611/// Produces a diagnostic and returns true on error, returns false and
612/// attaches the instantiated base classes to the class template
613/// specialization if successful.
614bool
Douglas Gregord475b8d2009-03-25 21:17:03 +0000615Sema::InstantiateBaseSpecifiers(CXXRecordDecl *Instantiation,
616 CXXRecordDecl *Pattern,
617 const TemplateArgument *TemplateArgs,
618 unsigned NumTemplateArgs) {
Douglas Gregor2943aed2009-03-03 04:44:36 +0000619 bool Invalid = false;
620 llvm::SmallVector<CXXBaseSpecifier*, 8> InstantiatedBases;
Douglas Gregord475b8d2009-03-25 21:17:03 +0000621 for (ClassTemplateSpecializationDecl::base_class_iterator
622 Base = Pattern->bases_begin(), BaseEnd = Pattern->bases_end();
Douglas Gregor27b152f2009-03-10 18:52:44 +0000623 Base != BaseEnd; ++Base) {
Douglas Gregor2943aed2009-03-03 04:44:36 +0000624 if (!Base->getType()->isDependentType()) {
625 // FIXME: Allocate via ASTContext
626 InstantiatedBases.push_back(new CXXBaseSpecifier(*Base));
627 continue;
628 }
629
630 QualType BaseType = InstantiateType(Base->getType(),
Douglas Gregord475b8d2009-03-25 21:17:03 +0000631 TemplateArgs, NumTemplateArgs,
Douglas Gregor2943aed2009-03-03 04:44:36 +0000632 Base->getSourceRange().getBegin(),
633 DeclarationName());
634 if (BaseType.isNull()) {
635 Invalid = true;
636 continue;
637 }
638
639 if (CXXBaseSpecifier *InstantiatedBase
Douglas Gregord475b8d2009-03-25 21:17:03 +0000640 = CheckBaseSpecifier(Instantiation,
Douglas Gregor2943aed2009-03-03 04:44:36 +0000641 Base->getSourceRange(),
642 Base->isVirtual(),
643 Base->getAccessSpecifierAsWritten(),
644 BaseType,
645 /*FIXME: Not totally accurate */
646 Base->getSourceRange().getBegin()))
647 InstantiatedBases.push_back(InstantiatedBase);
648 else
649 Invalid = true;
650 }
651
Douglas Gregor27b152f2009-03-10 18:52:44 +0000652 if (!Invalid &&
Douglas Gregord475b8d2009-03-25 21:17:03 +0000653 AttachBaseSpecifiers(Instantiation, &InstantiatedBases[0],
Douglas Gregor2943aed2009-03-03 04:44:36 +0000654 InstantiatedBases.size()))
655 Invalid = true;
656
657 return Invalid;
658}
659
Douglas Gregord475b8d2009-03-25 21:17:03 +0000660/// \brief Instantiate the definition of a class from a given pattern.
661///
662/// \param PointOfInstantiation The point of instantiation within the
663/// source code.
664///
665/// \param Instantiation is the declaration whose definition is being
666/// instantiated. This will be either a class template specialization
667/// or a member class of a class template specialization.
668///
669/// \param Pattern is the pattern from which the instantiation
670/// occurs. This will be either the declaration of a class template or
671/// the declaration of a member class of a class template.
672///
673/// \param TemplateArgs The template arguments to be substituted into
674/// the pattern.
675///
676/// \param NumTemplateArgs The number of templates arguments in
677/// TemplateArgs.
678///
679/// \returns true if an error occurred, false otherwise.
680bool
681Sema::InstantiateClass(SourceLocation PointOfInstantiation,
682 CXXRecordDecl *Instantiation, CXXRecordDecl *Pattern,
683 const TemplateArgument *TemplateArgs,
684 unsigned NumTemplateArgs) {
685 bool Invalid = false;
686
687 CXXRecordDecl *PatternDef
688 = cast_or_null<CXXRecordDecl>(Pattern->getDefinition(Context));
689 if (!PatternDef) {
690 if (Pattern == Instantiation->getInstantiatedFromMemberClass()) {
691 Diag(PointOfInstantiation,
692 diag::err_implicit_instantiate_member_undefined)
693 << Context.getTypeDeclType(Instantiation);
694 Diag(Pattern->getLocation(), diag::note_member_of_template_here);
695 } else {
696 Diag(PointOfInstantiation,
697 diag::err_template_implicit_instantiate_undefined)
698 << Context.getTypeDeclType(Instantiation);
699 Diag(Pattern->getLocation(), diag::note_template_decl_here);
700 }
701 return true;
702 }
703 Pattern = PatternDef;
704
Douglas Gregord048bb72009-03-25 21:23:52 +0000705 InstantiatingTemplate Inst(*this, PointOfInstantiation, Instantiation);
Douglas Gregord475b8d2009-03-25 21:17:03 +0000706 if (Inst)
707 return true;
708
709 // Enter the scope of this instantiation. We don't use
710 // PushDeclContext because we don't have a scope.
711 DeclContext *PreviousContext = CurContext;
712 CurContext = Instantiation;
713
714 // Start the definition of this instantiation.
715 Instantiation->startDefinition();
716
717 // Instantiate the base class specifiers.
718 if (InstantiateBaseSpecifiers(Instantiation, Pattern, TemplateArgs,
719 NumTemplateArgs))
720 Invalid = true;
721
Chris Lattnerb28317a2009-03-28 19:18:32 +0000722 llvm::SmallVector<DeclPtrTy, 32> Fields;
Douglas Gregor6ab35242009-04-09 21:40:53 +0000723 for (RecordDecl::decl_iterator Member = Pattern->decls_begin(Context),
724 MemberEnd = Pattern->decls_end(Context);
725 Member != MemberEnd; ++Member) {
Douglas Gregord475b8d2009-03-25 21:17:03 +0000726 Decl *NewMember = InstantiateDecl(*Member, Instantiation,
727 TemplateArgs, NumTemplateArgs);
728 if (NewMember) {
729 if (NewMember->isInvalidDecl())
730 Invalid = true;
731 else if (FieldDecl *Field = dyn_cast<FieldDecl>(NewMember))
Chris Lattnerb28317a2009-03-28 19:18:32 +0000732 Fields.push_back(DeclPtrTy::make(Field));
Douglas Gregord475b8d2009-03-25 21:17:03 +0000733 } else {
734 // FIXME: Eventually, a NULL return will mean that one of the
735 // instantiations was a semantic disaster, and we'll want to set
736 // Invalid = true. For now, we expect to skip some members that
737 // we can't yet handle.
738 }
739 }
740
741 // Finish checking fields.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000742 ActOnFields(0, Instantiation->getLocation(), DeclPtrTy::make(Instantiation),
Douglas Gregord475b8d2009-03-25 21:17:03 +0000743 &Fields[0], Fields.size(), SourceLocation(), SourceLocation(),
744 0);
745
746 // Add any implicitly-declared members that we might need.
747 AddImplicitlyDeclaredMembersToClass(Instantiation);
748
749 // Exit the scope of this instantiation.
750 CurContext = PreviousContext;
751
752 return Invalid;
753}
754
Douglas Gregor2943aed2009-03-03 04:44:36 +0000755bool
756Sema::InstantiateClassTemplateSpecialization(
757 ClassTemplateSpecializationDecl *ClassTemplateSpec,
758 bool ExplicitInstantiation) {
759 // Perform the actual instantiation on the canonical declaration.
760 ClassTemplateSpec = cast<ClassTemplateSpecializationDecl>(
761 Context.getCanonicalDecl(ClassTemplateSpec));
762
763 // We can only instantiate something that hasn't already been
764 // instantiated or specialized. Fail without any diagnostics: our
765 // caller will provide an error message.
766 if (ClassTemplateSpec->getSpecializationKind() != TSK_Undeclared)
767 return true;
768
769 // FIXME: Push this class template instantiation onto the
770 // instantiation stack, checking for recursion that exceeds a
771 // certain depth.
772
773 // FIXME: Perform class template partial specialization to select
774 // the best template.
775 ClassTemplateDecl *Template = ClassTemplateSpec->getSpecializedTemplate();
776
Douglas Gregord475b8d2009-03-25 21:17:03 +0000777 CXXRecordDecl *Pattern = Template->getTemplatedDecl();
Douglas Gregor2943aed2009-03-03 04:44:36 +0000778
779 // Note that this is an instantiation.
780 ClassTemplateSpec->setSpecializationKind(
781 ExplicitInstantiation? TSK_ExplicitInstantiation
782 : TSK_ImplicitInstantiation);
783
Douglas Gregord475b8d2009-03-25 21:17:03 +0000784 return InstantiateClass(ClassTemplateSpec->getLocation(),
785 ClassTemplateSpec, Pattern,
786 ClassTemplateSpec->getTemplateArgs(),
787 ClassTemplateSpec->getNumTemplateArgs());
Douglas Gregor2943aed2009-03-03 04:44:36 +0000788}
Douglas Gregor5953d8b2009-03-19 17:26:29 +0000789
Douglas Gregorab452ba2009-03-26 23:50:42 +0000790/// \brief Instantiate a nested-name-specifier.
791NestedNameSpecifier *
792Sema::InstantiateNestedNameSpecifier(NestedNameSpecifier *NNS,
793 SourceRange Range,
794 const TemplateArgument *TemplateArgs,
795 unsigned NumTemplateArgs) {
796 // Instantiate the prefix of this nested name specifier.
797 NestedNameSpecifier *Prefix = NNS->getPrefix();
798 if (Prefix) {
799 Prefix = InstantiateNestedNameSpecifier(Prefix, Range, TemplateArgs,
800 NumTemplateArgs);
801 if (!Prefix)
802 return 0;
Douglas Gregor5953d8b2009-03-19 17:26:29 +0000803 }
804
Douglas Gregorab452ba2009-03-26 23:50:42 +0000805 switch (NNS->getKind()) {
Douglas Gregor17343172009-04-01 00:28:59 +0000806 case NestedNameSpecifier::Identifier: {
807 assert(Prefix &&
808 "Can't have an identifier nested-name-specifier with no prefix");
809 CXXScopeSpec SS;
810 // FIXME: The source location information is all wrong.
811 SS.setRange(Range);
812 SS.setScopeRep(Prefix);
813 return static_cast<NestedNameSpecifier *>(
814 ActOnCXXNestedNameSpecifier(0, SS,
815 Range.getEnd(),
816 Range.getEnd(),
817 *NNS->getAsIdentifier()));
Douglas Gregorab452ba2009-03-26 23:50:42 +0000818 break;
Douglas Gregor17343172009-04-01 00:28:59 +0000819 }
Douglas Gregorab452ba2009-03-26 23:50:42 +0000820
821 case NestedNameSpecifier::Namespace:
822 case NestedNameSpecifier::Global:
823 return NNS;
824
825 case NestedNameSpecifier::TypeSpecWithTemplate:
826 case NestedNameSpecifier::TypeSpec: {
827 QualType T = QualType(NNS->getAsType(), 0);
828 if (!T->isDependentType())
829 return NNS;
830
831 T = InstantiateType(T, TemplateArgs, NumTemplateArgs, Range.getBegin(),
832 DeclarationName());
833 if (T.isNull())
834 return 0;
835
Douglas Gregord57959a2009-03-27 23:10:48 +0000836 if (T->isRecordType() ||
837 (getLangOptions().CPlusPlus0x && T->isEnumeralType())) {
Douglas Gregor17343172009-04-01 00:28:59 +0000838 assert(T.getCVRQualifiers() == 0 && "Can't get cv-qualifiers here");
Douglas Gregord57959a2009-03-27 23:10:48 +0000839 return NestedNameSpecifier::Create(Context, Prefix,
Douglas Gregorab452ba2009-03-26 23:50:42 +0000840 NNS->getKind() == NestedNameSpecifier::TypeSpecWithTemplate,
Douglas Gregord57959a2009-03-27 23:10:48 +0000841 T.getTypePtr());
842 }
843
844 Diag(Range.getBegin(), diag::err_nested_name_spec_non_tag) << T;
845 return 0;
Douglas Gregorab452ba2009-03-26 23:50:42 +0000846 }
847 }
848
Douglas Gregord57959a2009-03-27 23:10:48 +0000849 // Required to silence a GCC warning
Douglas Gregorab452ba2009-03-26 23:50:42 +0000850 return 0;
Douglas Gregor5953d8b2009-03-19 17:26:29 +0000851}
Douglas Gregorde650ae2009-03-31 18:38:02 +0000852
853TemplateName
854Sema::InstantiateTemplateName(TemplateName Name, SourceLocation Loc,
855 const TemplateArgument *TemplateArgs,
856 unsigned NumTemplateArgs) {
857 if (TemplateTemplateParmDecl *TTP
858 = dyn_cast_or_null<TemplateTemplateParmDecl>(
859 Name.getAsTemplateDecl())) {
860 assert(TTP->getDepth() == 0 &&
861 "Cannot reduce depth of a template template parameter");
862 assert(TTP->getPosition() < NumTemplateArgs && "Wrong # of template args");
Douglas Gregor9bde7732009-03-31 20:22:05 +0000863 assert(TemplateArgs[TTP->getPosition()].getAsDecl() &&
Douglas Gregorde650ae2009-03-31 18:38:02 +0000864 "Wrong kind of template template argument");
865 ClassTemplateDecl *ClassTemplate
866 = dyn_cast<ClassTemplateDecl>(
867 TemplateArgs[TTP->getPosition()].getAsDecl());
Douglas Gregor9bde7732009-03-31 20:22:05 +0000868 assert(ClassTemplate && "Expected a class template");
Douglas Gregorde650ae2009-03-31 18:38:02 +0000869 if (QualifiedTemplateName *QTN = Name.getAsQualifiedTemplateName()) {
870 NestedNameSpecifier *NNS
871 = InstantiateNestedNameSpecifier(QTN->getQualifier(),
872 /*FIXME=*/SourceRange(Loc),
873 TemplateArgs, NumTemplateArgs);
874 if (NNS)
875 return Context.getQualifiedTemplateName(NNS,
876 QTN->hasTemplateKeyword(),
877 ClassTemplate);
878 }
879
880 return TemplateName(ClassTemplate);
881 } else if (DependentTemplateName *DTN = Name.getAsDependentTemplateName()) {
882 NestedNameSpecifier *NNS
883 = InstantiateNestedNameSpecifier(DTN->getQualifier(),
884 /*FIXME=*/SourceRange(Loc),
885 TemplateArgs, NumTemplateArgs);
886
887 if (!NNS) // FIXME: Not the best recovery strategy.
888 return Name;
889
890 if (NNS->isDependent())
891 return Context.getDependentTemplateName(NNS, DTN->getName());
892
893 // Somewhat redundant with ActOnDependentTemplateName.
894 CXXScopeSpec SS;
895 SS.setRange(SourceRange(Loc));
896 SS.setScopeRep(NNS);
897 TemplateTy Template;
898 TemplateNameKind TNK = isTemplateName(*DTN->getName(), 0, Template, &SS);
899 if (TNK == TNK_Non_template) {
900 Diag(Loc, diag::err_template_kw_refers_to_non_template)
901 << DTN->getName();
902 return Name;
903 } else if (TNK == TNK_Function_template) {
904 Diag(Loc, diag::err_template_kw_refers_to_non_template)
905 << DTN->getName();
906 return Name;
907 }
908
909 return Template.getAsVal<TemplateName>();
910 }
911
912
913
914 // FIXME: Even if we're referring to a Decl that isn't a template
915 // template parameter, we may need to instantiate the outer contexts
916 // of that Decl. However, this won't be needed until we implement
917 // member templates.
918 return Name;
919}