blob: 5800ab9265a1918b7767dc877309b47bf1d4dfd3 [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"
16#include "clang/AST/ExprCXX.h"
17#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 Gregor26dce442009-03-10 00:06:19 +000028Sema::InstantiatingTemplate::
29InstantiatingTemplate(Sema &SemaRef, SourceLocation PointOfInstantiation,
30 ClassTemplateSpecializationDecl *Entity,
31 SourceRange InstantiationRange)
32 : SemaRef(SemaRef) {
Douglas Gregordf667e72009-03-10 20:44:00 +000033
34 Invalid = CheckInstantiationDepth(PointOfInstantiation,
35 InstantiationRange);
36 if (!Invalid) {
Douglas Gregor26dce442009-03-10 00:06:19 +000037 ActiveTemplateInstantiation Inst;
Douglas Gregordf667e72009-03-10 20:44:00 +000038 Inst.Kind = ActiveTemplateInstantiation::TemplateInstantiation;
Douglas Gregor26dce442009-03-10 00:06:19 +000039 Inst.PointOfInstantiation = PointOfInstantiation;
Douglas Gregordf667e72009-03-10 20:44:00 +000040 Inst.Entity = reinterpret_cast<uintptr_t>(Entity);
41 Inst.InstantiationRange = InstantiationRange;
42 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
43 Invalid = false;
44 }
45}
46
47Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef,
48 SourceLocation PointOfInstantiation,
49 TemplateDecl *Template,
50 const TemplateArgument *TemplateArgs,
51 unsigned NumTemplateArgs,
52 SourceRange InstantiationRange)
53 : SemaRef(SemaRef) {
54
55 Invalid = CheckInstantiationDepth(PointOfInstantiation,
56 InstantiationRange);
57 if (!Invalid) {
58 ActiveTemplateInstantiation Inst;
59 Inst.Kind
60 = ActiveTemplateInstantiation::DefaultTemplateArgumentInstantiation;
61 Inst.PointOfInstantiation = PointOfInstantiation;
62 Inst.Entity = reinterpret_cast<uintptr_t>(Template);
63 Inst.TemplateArgs = TemplateArgs;
64 Inst.NumTemplateArgs = NumTemplateArgs;
Douglas Gregor26dce442009-03-10 00:06:19 +000065 Inst.InstantiationRange = InstantiationRange;
66 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
67 Invalid = false;
68 }
69}
70
71Sema::InstantiatingTemplate::~InstantiatingTemplate() {
72 if (!Invalid)
73 SemaRef.ActiveTemplateInstantiations.pop_back();
74}
75
Douglas Gregordf667e72009-03-10 20:44:00 +000076bool Sema::InstantiatingTemplate::CheckInstantiationDepth(
77 SourceLocation PointOfInstantiation,
78 SourceRange InstantiationRange) {
79 if (SemaRef.ActiveTemplateInstantiations.size()
80 <= SemaRef.getLangOptions().InstantiationDepth)
81 return false;
82
83 SemaRef.Diag(PointOfInstantiation,
84 diag::err_template_recursion_depth_exceeded)
85 << SemaRef.getLangOptions().InstantiationDepth
86 << InstantiationRange;
87 SemaRef.Diag(PointOfInstantiation, diag::note_template_recursion_depth)
88 << SemaRef.getLangOptions().InstantiationDepth;
89 return true;
90}
91
Douglas Gregoree1828a2009-03-10 18:03:33 +000092/// \brief Post-diagnostic hook for printing the instantiation stack.
93void Sema::PrintInstantiationStackHook(unsigned, void *Cookie) {
Douglas Gregor27b152f2009-03-10 18:52:44 +000094 Sema &SemaRef = *static_cast<Sema*>(Cookie);
95 SemaRef.PrintInstantiationStack();
96 SemaRef.LastTemplateInstantiationErrorContext
Douglas Gregordf667e72009-03-10 20:44:00 +000097 = SemaRef.ActiveTemplateInstantiations.back();
Douglas Gregoree1828a2009-03-10 18:03:33 +000098}
99
100/// \brief Prints the current instantiation stack through a series of
101/// notes.
102void Sema::PrintInstantiationStack() {
103 for (llvm::SmallVector<ActiveTemplateInstantiation, 16>::reverse_iterator
104 Active = ActiveTemplateInstantiations.rbegin(),
105 ActiveEnd = ActiveTemplateInstantiations.rend();
106 Active != ActiveEnd;
107 ++Active) {
Douglas Gregordf667e72009-03-10 20:44:00 +0000108 switch (Active->Kind) {
109 case ActiveTemplateInstantiation::TemplateInstantiation: {
110 ClassTemplateSpecializationDecl *Spec
111 = cast<ClassTemplateSpecializationDecl>((Decl*)Active->Entity);
112 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
113 diag::note_template_class_instantiation_here)
114 << Context.getTypeDeclType(Spec)
115 << Active->InstantiationRange;
116 break;
117 }
118
119 case ActiveTemplateInstantiation::DefaultTemplateArgumentInstantiation: {
120 TemplateDecl *Template = cast<TemplateDecl>((Decl *)Active->Entity);
121 std::string TemplateArgsStr
122 = ClassTemplateSpecializationType::PrintTemplateArgumentList(
123 Active->TemplateArgs,
124 Active->NumTemplateArgs);
125 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
126 diag::note_default_arg_instantiation_here)
127 << (Template->getNameAsString() + TemplateArgsStr)
128 << Active->InstantiationRange;
129 break;
130 }
131 }
Douglas Gregoree1828a2009-03-10 18:03:33 +0000132 }
133}
134
Douglas Gregor99ebf652009-02-27 19:31:52 +0000135//===----------------------------------------------------------------------===/
136// Template Instantiation for Types
137//===----------------------------------------------------------------------===/
Douglas Gregorcd281c32009-02-28 00:25:32 +0000138namespace {
139 class VISIBILITY_HIDDEN TemplateTypeInstantiator {
140 Sema &SemaRef;
141 const TemplateArgument *TemplateArgs;
142 unsigned NumTemplateArgs;
143 SourceLocation Loc;
144 DeclarationName Entity;
Douglas Gregor99ebf652009-02-27 19:31:52 +0000145
Douglas Gregorcd281c32009-02-28 00:25:32 +0000146 public:
147 TemplateTypeInstantiator(Sema &SemaRef,
148 const TemplateArgument *TemplateArgs,
149 unsigned NumTemplateArgs,
150 SourceLocation Loc,
151 DeclarationName Entity)
152 : SemaRef(SemaRef), TemplateArgs(TemplateArgs),
153 NumTemplateArgs(NumTemplateArgs), Loc(Loc), Entity(Entity) { }
154
155 QualType operator()(QualType T) const { return Instantiate(T); }
156
157 QualType Instantiate(QualType T) const;
158
159 // Declare instantiate functions for each type.
160#define TYPE(Class, Base) \
161 QualType Instantiate##Class##Type(const Class##Type *T, \
162 unsigned Quals) const;
163#define ABSTRACT_TYPE(Class, Base)
164#include "clang/AST/TypeNodes.def"
165 };
166}
167
168QualType
169TemplateTypeInstantiator::InstantiateExtQualType(const ExtQualType *T,
170 unsigned Quals) const {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000171 // FIXME: Implement this
172 assert(false && "Cannot instantiate ExtQualType yet");
173 return QualType();
174}
175
Douglas Gregorcd281c32009-02-28 00:25:32 +0000176QualType
177TemplateTypeInstantiator::InstantiateBuiltinType(const BuiltinType *T,
178 unsigned Quals) const {
Douglas Gregor724651c2009-02-28 01:04:19 +0000179 assert(false && "Builtin types are not dependent and cannot be instantiated");
Douglas Gregorcd281c32009-02-28 00:25:32 +0000180 return QualType(T, Quals);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000181}
182
Douglas Gregorcd281c32009-02-28 00:25:32 +0000183QualType
184TemplateTypeInstantiator::
185InstantiateFixedWidthIntType(const FixedWidthIntType *T, unsigned Quals) const {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000186 // FIXME: Implement this
187 assert(false && "Cannot instantiate FixedWidthIntType yet");
188 return QualType();
189}
190
Douglas Gregorcd281c32009-02-28 00:25:32 +0000191QualType
192TemplateTypeInstantiator::InstantiateComplexType(const ComplexType *T,
193 unsigned Quals) const {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000194 // FIXME: Implement this
195 assert(false && "Cannot instantiate ComplexType yet");
196 return QualType();
197}
198
Douglas Gregorcd281c32009-02-28 00:25:32 +0000199QualType
200TemplateTypeInstantiator::InstantiatePointerType(const PointerType *T,
201 unsigned Quals) const {
202 QualType PointeeType = Instantiate(T->getPointeeType());
203 if (PointeeType.isNull())
204 return QualType();
205
206 return SemaRef.BuildPointerType(PointeeType, Quals, Loc, Entity);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000207}
208
Douglas Gregorcd281c32009-02-28 00:25:32 +0000209QualType
210TemplateTypeInstantiator::InstantiateBlockPointerType(const BlockPointerType *T,
211 unsigned Quals) const {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000212 // FIXME: Implement this
213 assert(false && "Cannot instantiate BlockPointerType yet");
214 return QualType();
215}
216
Douglas Gregorcd281c32009-02-28 00:25:32 +0000217QualType
218TemplateTypeInstantiator::InstantiateReferenceType(const ReferenceType *T,
219 unsigned Quals) const {
220 QualType ReferentType = Instantiate(T->getPointeeType());
221 if (ReferentType.isNull())
222 return QualType();
223
224 return SemaRef.BuildReferenceType(ReferentType, Quals, Loc, Entity);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000225}
226
Douglas Gregorcd281c32009-02-28 00:25:32 +0000227QualType
228TemplateTypeInstantiator::
229InstantiateMemberPointerType(const MemberPointerType *T,
230 unsigned Quals) const {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000231 // FIXME: Implement this
232 assert(false && "Cannot instantiate MemberPointerType yet");
233 return QualType();
234}
235
Douglas Gregorcd281c32009-02-28 00:25:32 +0000236QualType
237TemplateTypeInstantiator::
238InstantiateConstantArrayType(const ConstantArrayType *T,
239 unsigned Quals) const {
240 QualType ElementType = Instantiate(T->getElementType());
241 if (ElementType.isNull())
242 return ElementType;
243
244 // Build a temporary integer literal to specify the size for
245 // BuildArrayType. Since we have already checked the size as part of
246 // creating the dependent array type in the first place, we know
247 // there aren't any errors.
Douglas Gregor8d217212009-03-09 20:07:22 +0000248 // FIXME: Is IntTy big enough? Maybe not, but LongLongTy causes
249 // problems that I have yet to investigate.
250 IntegerLiteral ArraySize(T->getSize(), SemaRef.Context.IntTy, Loc);
Douglas Gregorcd281c32009-02-28 00:25:32 +0000251 return SemaRef.BuildArrayType(ElementType, T->getSizeModifier(),
252 &ArraySize, T->getIndexTypeQualifier(),
253 Loc, Entity);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000254}
255
Douglas Gregorcd281c32009-02-28 00:25:32 +0000256QualType
257TemplateTypeInstantiator::
258InstantiateIncompleteArrayType(const IncompleteArrayType *T,
259 unsigned Quals) const {
260 QualType ElementType = Instantiate(T->getElementType());
261 if (ElementType.isNull())
262 return ElementType;
263
264 return SemaRef.BuildArrayType(ElementType, T->getSizeModifier(),
265 0, T->getIndexTypeQualifier(),
266 Loc, Entity);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000267}
268
Douglas Gregorcd281c32009-02-28 00:25:32 +0000269QualType
270TemplateTypeInstantiator::
271InstantiateVariableArrayType(const VariableArrayType *T,
272 unsigned Quals) const {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000273 // FIXME: Implement this
274 assert(false && "Cannot instantiate VariableArrayType yet");
275 return QualType();
276}
277
Douglas Gregorcd281c32009-02-28 00:25:32 +0000278QualType
279TemplateTypeInstantiator::
280InstantiateDependentSizedArrayType(const DependentSizedArrayType *T,
281 unsigned Quals) const {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000282 // FIXME: Implement this
283 assert(false && "Cannot instantiate DependentSizedArrayType yet");
284 return QualType();
285}
286
Douglas Gregorcd281c32009-02-28 00:25:32 +0000287QualType
288TemplateTypeInstantiator::InstantiateVectorType(const VectorType *T,
289 unsigned Quals) const {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000290 // FIXME: Implement this
291 assert(false && "Cannot instantiate VectorType yet");
292 return QualType();
293}
294
Douglas Gregorcd281c32009-02-28 00:25:32 +0000295QualType
296TemplateTypeInstantiator::InstantiateExtVectorType(const ExtVectorType *T,
297 unsigned Quals) const {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000298 // FIXME: Implement this
299 assert(false && "Cannot instantiate ExtVectorType yet");
300 return QualType();
301}
302
Douglas Gregorcd281c32009-02-28 00:25:32 +0000303QualType
304TemplateTypeInstantiator::
305InstantiateFunctionProtoType(const FunctionProtoType *T,
306 unsigned Quals) const {
Douglas Gregor724651c2009-02-28 01:04:19 +0000307 QualType ResultType = Instantiate(T->getResultType());
308 if (ResultType.isNull())
309 return ResultType;
310
311 llvm::SmallVector<QualType, 16> ParamTypes;
312 for (FunctionProtoType::arg_type_iterator Param = T->arg_type_begin(),
313 ParamEnd = T->arg_type_end();
314 Param != ParamEnd; ++Param) {
315 QualType P = Instantiate(*Param);
316 if (P.isNull())
317 return P;
318
319 ParamTypes.push_back(P);
320 }
321
322 return SemaRef.BuildFunctionType(ResultType, &ParamTypes[0],
323 ParamTypes.size(),
324 T->isVariadic(), T->getTypeQuals(),
325 Loc, Entity);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000326}
327
Douglas Gregorcd281c32009-02-28 00:25:32 +0000328QualType
329TemplateTypeInstantiator::
330InstantiateFunctionNoProtoType(const FunctionNoProtoType *T,
331 unsigned Quals) const {
Douglas Gregor724651c2009-02-28 01:04:19 +0000332 assert(false && "Functions without prototypes cannot be dependent.");
Douglas Gregor99ebf652009-02-27 19:31:52 +0000333 return QualType();
334}
335
Douglas Gregorcd281c32009-02-28 00:25:32 +0000336QualType
337TemplateTypeInstantiator::InstantiateTypedefType(const TypedefType *T,
338 unsigned Quals) const {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000339 // FIXME: Implement this
340 assert(false && "Cannot instantiate TypedefType yet");
341 return QualType();
342}
343
Douglas Gregorcd281c32009-02-28 00:25:32 +0000344QualType
345TemplateTypeInstantiator::InstantiateTypeOfExprType(const TypeOfExprType *T,
346 unsigned Quals) const {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000347 // FIXME: Implement this
348 assert(false && "Cannot instantiate TypeOfExprType yet");
349 return QualType();
350}
351
Douglas Gregorcd281c32009-02-28 00:25:32 +0000352QualType
353TemplateTypeInstantiator::InstantiateTypeOfType(const TypeOfType *T,
354 unsigned Quals) const {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000355 // FIXME: Implement this
356 assert(false && "Cannot instantiate TypeOfType yet");
357 return QualType();
358}
359
Douglas Gregorcd281c32009-02-28 00:25:32 +0000360QualType
361TemplateTypeInstantiator::InstantiateRecordType(const RecordType *T,
362 unsigned Quals) const {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000363 // FIXME: Implement this
364 assert(false && "Cannot instantiate RecordType yet");
365 return QualType();
366}
367
Douglas Gregorcd281c32009-02-28 00:25:32 +0000368QualType
Douglas Gregorcd281c32009-02-28 00:25:32 +0000369TemplateTypeInstantiator::InstantiateEnumType(const EnumType *T,
370 unsigned Quals) const {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000371 // FIXME: Implement this
372 assert(false && "Cannot instantiate EnumType yet");
373 return QualType();
374}
375
Douglas Gregorcd281c32009-02-28 00:25:32 +0000376QualType
377TemplateTypeInstantiator::
378InstantiateTemplateTypeParmType(const TemplateTypeParmType *T,
379 unsigned Quals) const {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000380 if (T->getDepth() == 0) {
381 // Replace the template type parameter with its corresponding
382 // template argument.
383 assert(T->getIndex() < NumTemplateArgs && "Wrong # of template args");
384 assert(TemplateArgs[T->getIndex()].getKind() == TemplateArgument::Type &&
385 "Template argument kind mismatch");
386 QualType Result = TemplateArgs[T->getIndex()].getAsType();
Douglas Gregorcd281c32009-02-28 00:25:32 +0000387 if (Result.isNull() || !Quals)
Douglas Gregor99ebf652009-02-27 19:31:52 +0000388 return Result;
389
390 // C++ [dcl.ref]p1:
391 // [...] Cv-qualified references are ill-formed except when
392 // the cv-qualifiers are introduced through the use of a
393 // typedef (7.1.3) or of a template type argument (14.3), in
394 // which case the cv-qualifiers are ignored.
Douglas Gregorcd281c32009-02-28 00:25:32 +0000395 if (Quals && Result->isReferenceType())
396 Quals = 0;
Douglas Gregor99ebf652009-02-27 19:31:52 +0000397
Douglas Gregorcd281c32009-02-28 00:25:32 +0000398 return QualType(Result.getTypePtr(), Quals | Result.getCVRQualifiers());
Douglas Gregor99ebf652009-02-27 19:31:52 +0000399 }
400
401 // The template type parameter comes from an inner template (e.g.,
402 // the template parameter list of a member template inside the
403 // template we are instantiating). Create a new template type
404 // parameter with the template "level" reduced by one.
405 return SemaRef.Context.getTemplateTypeParmType(T->getDepth() - 1,
406 T->getIndex(),
Douglas Gregorcd281c32009-02-28 00:25:32 +0000407 T->getName())
408 .getQualifiedType(Quals);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000409}
410
Douglas Gregorcd281c32009-02-28 00:25:32 +0000411QualType
412TemplateTypeInstantiator::
413InstantiateClassTemplateSpecializationType(
414 const ClassTemplateSpecializationType *T,
415 unsigned Quals) const {
Douglas Gregor40808ce2009-03-09 23:48:35 +0000416 llvm::SmallVector<TemplateArgument, 16> InstantiatedTemplateArgs;
417 InstantiatedTemplateArgs.reserve(T->getNumArgs());
418 for (ClassTemplateSpecializationType::iterator Arg = T->begin(),
419 ArgEnd = T->end();
420 Arg != ArgEnd; ++Arg) {
421 switch (Arg->getKind()) {
422 case TemplateArgument::Type: {
423 QualType T = SemaRef.InstantiateType(Arg->getAsType(),
424 TemplateArgs, NumTemplateArgs,
425 Arg->getLocation(),
426 DeclarationName());
427 if (T.isNull())
428 return QualType();
429
430 InstantiatedTemplateArgs.push_back(
431 TemplateArgument(Arg->getLocation(), T));
432 break;
433 }
434
435 case TemplateArgument::Declaration:
436 case TemplateArgument::Integral:
437 InstantiatedTemplateArgs.push_back(*Arg);
438 break;
439
440 case TemplateArgument::Expression:
441 assert(false && "Cannot instantiate expressions yet");
442 break;
443 }
444 }
445
446 // FIXME: We're missing the locations of the template name, '<', and
447 // '>'.
448 return SemaRef.CheckClassTemplateId(cast<ClassTemplateDecl>(T->getTemplate()),
449 Loc,
450 SourceLocation(),
451 &InstantiatedTemplateArgs[0],
452 InstantiatedTemplateArgs.size(),
453 SourceLocation());
Douglas Gregor99ebf652009-02-27 19:31:52 +0000454}
455
Douglas Gregorcd281c32009-02-28 00:25:32 +0000456QualType
457TemplateTypeInstantiator::
458InstantiateObjCInterfaceType(const ObjCInterfaceType *T,
459 unsigned Quals) const {
460 assert(false && "Objective-C types cannot be dependent");
Douglas Gregor99ebf652009-02-27 19:31:52 +0000461 return QualType();
462}
463
Douglas Gregorcd281c32009-02-28 00:25:32 +0000464QualType
465TemplateTypeInstantiator::
466InstantiateObjCQualifiedInterfaceType(const ObjCQualifiedInterfaceType *T,
467 unsigned Quals) const {
468 assert(false && "Objective-C types cannot be dependent");
Douglas Gregor99ebf652009-02-27 19:31:52 +0000469 return QualType();
470}
471
Douglas Gregorcd281c32009-02-28 00:25:32 +0000472QualType
473TemplateTypeInstantiator::
474InstantiateObjCQualifiedIdType(const ObjCQualifiedIdType *T,
475 unsigned Quals) const {
476 assert(false && "Objective-C types cannot be dependent");
Douglas Gregor99ebf652009-02-27 19:31:52 +0000477 return QualType();
478}
479
Douglas Gregorcd281c32009-02-28 00:25:32 +0000480QualType
481TemplateTypeInstantiator::
482InstantiateObjCQualifiedClassType(const ObjCQualifiedClassType *T,
483 unsigned Quals) const {
484 assert(false && "Objective-C types cannot be dependent");
Douglas Gregor99ebf652009-02-27 19:31:52 +0000485 return QualType();
486}
487
Douglas Gregorcd281c32009-02-28 00:25:32 +0000488/// \brief The actual implementation of Sema::InstantiateType().
489QualType TemplateTypeInstantiator::Instantiate(QualType T) const {
490 // If T is not a dependent type, there is nothing to do.
491 if (!T->isDependentType())
492 return T;
493
494 switch (T->getTypeClass()) {
495#define TYPE(Class, Base) \
496 case Type::Class: \
497 return Instantiate##Class##Type(cast<Class##Type>(T.getTypePtr()), \
498 T.getCVRQualifiers());
499#define ABSTRACT_TYPE(Class, Base)
500#include "clang/AST/TypeNodes.def"
501 }
502
503 assert(false && "Not all types have been decoded for instantiation");
504 return QualType();
505}
Douglas Gregor99ebf652009-02-27 19:31:52 +0000506
507/// \brief Instantiate the type T with a given set of template arguments.
508///
509/// This routine substitutes the given template arguments into the
510/// type T and produces the instantiated type.
511///
512/// \param T the type into which the template arguments will be
513/// substituted. If this type is not dependent, it will be returned
514/// immediately.
515///
516/// \param TemplateArgs the template arguments that will be
517/// substituted for the top-level template parameters within T.
518///
519/// \param NumTemplateArgs the number of template arguments provided
520/// by TemplateArgs.
521///
522/// \param Loc the location in the source code where this substitution
523/// is being performed. It will typically be the location of the
524/// declarator (if we're instantiating the type of some declaration)
525/// or the location of the type in the source code (if, e.g., we're
526/// instantiating the type of a cast expression).
527///
528/// \param Entity the name of the entity associated with a declaration
529/// being instantiated (if any). May be empty to indicate that there
530/// is no such entity (if, e.g., this is a type that occurs as part of
531/// a cast expression) or that the entity has no name (e.g., an
532/// unnamed function parameter).
533///
534/// \returns If the instantiation succeeds, the instantiated
535/// type. Otherwise, produces diagnostics and returns a NULL type.
536QualType Sema::InstantiateType(QualType T,
537 const TemplateArgument *TemplateArgs,
538 unsigned NumTemplateArgs,
539 SourceLocation Loc, DeclarationName Entity) {
Douglas Gregordf667e72009-03-10 20:44:00 +0000540 assert(!ActiveTemplateInstantiations.empty() &&
541 "Cannot perform an instantiation without some context on the "
542 "instantiation stack");
543
Douglas Gregor99ebf652009-02-27 19:31:52 +0000544 // If T is not a dependent type, there is nothing to do.
545 if (!T->isDependentType())
546 return T;
547
Douglas Gregorcd281c32009-02-28 00:25:32 +0000548 TemplateTypeInstantiator Instantiator(*this, TemplateArgs, NumTemplateArgs,
549 Loc, Entity);
550 return Instantiator(T);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000551}
Douglas Gregor2943aed2009-03-03 04:44:36 +0000552
553/// \brief Instantiate the base class specifiers of the given class
554/// template specialization.
555///
556/// Produces a diagnostic and returns true on error, returns false and
557/// attaches the instantiated base classes to the class template
558/// specialization if successful.
559bool
560Sema::InstantiateBaseSpecifiers(
561 ClassTemplateSpecializationDecl *ClassTemplateSpec,
562 ClassTemplateDecl *ClassTemplate) {
563 bool Invalid = false;
564 llvm::SmallVector<CXXBaseSpecifier*, 8> InstantiatedBases;
565 for (ClassTemplateSpecializationDecl::base_class_iterator
566 Base = ClassTemplate->getTemplatedDecl()->bases_begin(),
567 BaseEnd = ClassTemplate->getTemplatedDecl()->bases_end();
Douglas Gregor27b152f2009-03-10 18:52:44 +0000568 Base != BaseEnd; ++Base) {
Douglas Gregor2943aed2009-03-03 04:44:36 +0000569 if (!Base->getType()->isDependentType()) {
570 // FIXME: Allocate via ASTContext
571 InstantiatedBases.push_back(new CXXBaseSpecifier(*Base));
572 continue;
573 }
574
575 QualType BaseType = InstantiateType(Base->getType(),
576 ClassTemplateSpec->getTemplateArgs(),
577 ClassTemplateSpec->getNumTemplateArgs(),
578 Base->getSourceRange().getBegin(),
579 DeclarationName());
580 if (BaseType.isNull()) {
581 Invalid = true;
582 continue;
583 }
584
585 if (CXXBaseSpecifier *InstantiatedBase
586 = CheckBaseSpecifier(ClassTemplateSpec,
587 Base->getSourceRange(),
588 Base->isVirtual(),
589 Base->getAccessSpecifierAsWritten(),
590 BaseType,
591 /*FIXME: Not totally accurate */
592 Base->getSourceRange().getBegin()))
593 InstantiatedBases.push_back(InstantiatedBase);
594 else
595 Invalid = true;
596 }
597
Douglas Gregor27b152f2009-03-10 18:52:44 +0000598 if (!Invalid &&
599 AttachBaseSpecifiers(ClassTemplateSpec, &InstantiatedBases[0],
Douglas Gregor2943aed2009-03-03 04:44:36 +0000600 InstantiatedBases.size()))
601 Invalid = true;
602
603 return Invalid;
604}
605
606bool
607Sema::InstantiateClassTemplateSpecialization(
608 ClassTemplateSpecializationDecl *ClassTemplateSpec,
609 bool ExplicitInstantiation) {
610 // Perform the actual instantiation on the canonical declaration.
611 ClassTemplateSpec = cast<ClassTemplateSpecializationDecl>(
612 Context.getCanonicalDecl(ClassTemplateSpec));
613
614 // We can only instantiate something that hasn't already been
615 // instantiated or specialized. Fail without any diagnostics: our
616 // caller will provide an error message.
617 if (ClassTemplateSpec->getSpecializationKind() != TSK_Undeclared)
618 return true;
619
620 // FIXME: Push this class template instantiation onto the
621 // instantiation stack, checking for recursion that exceeds a
622 // certain depth.
623
624 // FIXME: Perform class template partial specialization to select
625 // the best template.
626 ClassTemplateDecl *Template = ClassTemplateSpec->getSpecializedTemplate();
627
628 if (!Template->getTemplatedDecl()->getDefinition(Context)) {
629 Diag(ClassTemplateSpec->getLocation(),
630 diag::err_template_implicit_instantiate_undefined)
631 << Context.getTypeDeclType(ClassTemplateSpec);
632 Diag(Template->getTemplatedDecl()->getLocation(),
633 diag::note_template_decl_here);
634 return true;
635 }
636
637 // Note that this is an instantiation.
638 ClassTemplateSpec->setSpecializationKind(
639 ExplicitInstantiation? TSK_ExplicitInstantiation
640 : TSK_ImplicitInstantiation);
641
642
643 bool Invalid = false;
644
Douglas Gregor26dce442009-03-10 00:06:19 +0000645 InstantiatingTemplate Inst(*this, ClassTemplateSpec->getLocation(),
646 ClassTemplateSpec);
647 if (Inst)
648 return true;
649
Douglas Gregor2943aed2009-03-03 04:44:36 +0000650 // Enter the scope of this instantiation. We don't use
651 // PushDeclContext because we don't have a scope.
652 DeclContext *PreviousContext = CurContext;
653 CurContext = ClassTemplateSpec;
654
655 // Start the definition of this instantiation.
656 ClassTemplateSpec->startDefinition();
657
Douglas Gregor2943aed2009-03-03 04:44:36 +0000658
659 // Instantiate the base class specifiers.
660 if (InstantiateBaseSpecifiers(ClassTemplateSpec, Template))
661 Invalid = true;
662
Douglas Gregor4fdf1fa2009-03-11 16:48:53 +0000663 // FIXME: Create the injected-class-name for the
664 // instantiation. Should this be a typedef or something like it?
665
666 RecordDecl *Pattern = Template->getTemplatedDecl();
Douglas Gregor3cf538d2009-03-11 18:59:21 +0000667 llvm::SmallVector<DeclTy *, 32> Fields;
Douglas Gregor4fdf1fa2009-03-11 16:48:53 +0000668 for (RecordDecl::decl_iterator Member = Pattern->decls_begin(),
669 MemberEnd = Pattern->decls_end();
670 Member != MemberEnd; ++Member) {
671 if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(*Member)) {
672 // FIXME: Simplified instantiation of typedefs needs to be made
673 // "real".
674 QualType T = Typedef->getUnderlyingType();
675 if (T->isDependentType()) {
676 T = InstantiateType(T, ClassTemplateSpec->getTemplateArgs(),
677 ClassTemplateSpec->getNumTemplateArgs(),
678 Typedef->getLocation(),
679 Typedef->getDeclName());
680 if (T.isNull()) {
681 Invalid = true;
682 T = Context.IntTy;
683 }
684 }
685
686 // Create the new typedef
687 TypedefDecl *New
688 = TypedefDecl::Create(Context, ClassTemplateSpec,
689 Typedef->getLocation(),
690 Typedef->getIdentifier(),
691 T);
692 ClassTemplateSpec->addDecl(New);
Douglas Gregor3cf538d2009-03-11 18:59:21 +0000693 }
694 else if (FieldDecl *Field = dyn_cast<FieldDecl>(*Member)) {
695 // FIXME: Simplified instantiation of fields needs to be made
696 // "real".
697 QualType T = Field->getType();
698 if (T->isDependentType()) {
699 T = InstantiateType(T, ClassTemplateSpec->getTemplateArgs(),
700 ClassTemplateSpec->getNumTemplateArgs(),
701 Field->getLocation(),
702 Field->getDeclName());
703 if (!T.isNull() && T->isFunctionType()) {
704 // C++ [temp.arg.type]p3:
705 // If a declaration acquires a function type through a type
706 // dependent on a template-parameter and this causes a
707 // declaration that does not use the syntactic form of a
708 // function declarator to have function type, the program is
709 // ill-formed.
710 Diag(Field->getLocation(), diag::err_field_instantiates_to_function)
711 << T;
712 T = QualType();
713 }
714 }
715
716 FieldDecl *New = CheckFieldDecl(Field->getDeclName(), T,
717 ClassTemplateSpec,
718 Field->getLocation(),
719 Field->isMutable(),
720 Field->getBitWidth(),
721 0);
722 if (New) {
723 ClassTemplateSpec->addDecl(New);
724 Fields.push_back(New);
725
726 if (New->isInvalidDecl())
727 Invalid = true;
728 }
Douglas Gregor4fdf1fa2009-03-11 16:48:53 +0000729 }
730 }
731
Douglas Gregor3cf538d2009-03-11 18:59:21 +0000732 // Finish checking fields.
733 ActOnFields(0, ClassTemplateSpec->getLocation(), ClassTemplateSpec,
734 &Fields[0], Fields.size(), SourceLocation(), SourceLocation(),
735 0);
736
Douglas Gregor2943aed2009-03-03 04:44:36 +0000737 // Add any implicitly-declared members that we might need.
738 AddImplicitlyDeclaredMembersToClass(ClassTemplateSpec);
739
Douglas Gregor2943aed2009-03-03 04:44:36 +0000740 // Exit the scope of this instantiation.
741 CurContext = PreviousContext;
742
743 return Invalid;
744}