blob: 4a12b077666ee50f8037d8e48bbaf066c5cbe586 [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"
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 Gregorf57dcd02009-02-28 00:25:32 +000020#include "llvm/Support/Compiler.h"
Douglas Gregor74296542009-02-27 19:31:52 +000021
22using namespace clang;
23
Douglas Gregorfee85d62009-03-10 18:03:33 +000024//===----------------------------------------------------------------------===/
25// Template Instantiation Support
26//===----------------------------------------------------------------------===/
27
Douglas Gregor375733c2009-03-10 00:06:19 +000028Sema::InstantiatingTemplate::
29InstantiatingTemplate(Sema &SemaRef, SourceLocation PointOfInstantiation,
30 ClassTemplateSpecializationDecl *Entity,
31 SourceRange InstantiationRange)
32 : SemaRef(SemaRef) {
33 if (SemaRef.ActiveTemplateInstantiations.size()
34 > SemaRef.getLangOptions().InstantiationDepth) {
35 SemaRef.Diag(PointOfInstantiation,
36 diag::err_template_recursion_depth_exceeded)
37 << SemaRef.getLangOptions().InstantiationDepth
38 << InstantiationRange;
39 SemaRef.Diag(PointOfInstantiation, diag::note_template_recursion_depth)
40 << SemaRef.getLangOptions().InstantiationDepth;
41 Invalid = true;
42 } else {
43 ActiveTemplateInstantiation Inst;
44 Inst.PointOfInstantiation = PointOfInstantiation;
45 Inst.Entity = Entity;
46 Inst.InstantiationRange = InstantiationRange;
47 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
48 Invalid = false;
49 }
50}
51
52Sema::InstantiatingTemplate::~InstantiatingTemplate() {
53 if (!Invalid)
54 SemaRef.ActiveTemplateInstantiations.pop_back();
55}
56
Douglas Gregorfee85d62009-03-10 18:03:33 +000057/// \brief Post-diagnostic hook for printing the instantiation stack.
58void Sema::PrintInstantiationStackHook(unsigned, void *Cookie) {
59 static_cast<Sema*>(Cookie)->PrintInstantiationStack();
60}
61
62/// \brief Prints the current instantiation stack through a series of
63/// notes.
64void Sema::PrintInstantiationStack() {
65 for (llvm::SmallVector<ActiveTemplateInstantiation, 16>::reverse_iterator
66 Active = ActiveTemplateInstantiations.rbegin(),
67 ActiveEnd = ActiveTemplateInstantiations.rend();
68 Active != ActiveEnd;
69 ++Active) {
70 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
71 diag::note_template_class_instantiation_here)
72 << Context.getTypeDeclType(Active->Entity)
73 << Active->InstantiationRange;
74 }
75}
76
Douglas Gregor74296542009-02-27 19:31:52 +000077//===----------------------------------------------------------------------===/
78// Template Instantiation for Types
79//===----------------------------------------------------------------------===/
Douglas Gregorf57dcd02009-02-28 00:25:32 +000080namespace {
81 class VISIBILITY_HIDDEN TemplateTypeInstantiator {
82 Sema &SemaRef;
83 const TemplateArgument *TemplateArgs;
84 unsigned NumTemplateArgs;
85 SourceLocation Loc;
86 DeclarationName Entity;
Douglas Gregor74296542009-02-27 19:31:52 +000087
Douglas Gregorf57dcd02009-02-28 00:25:32 +000088 public:
89 TemplateTypeInstantiator(Sema &SemaRef,
90 const TemplateArgument *TemplateArgs,
91 unsigned NumTemplateArgs,
92 SourceLocation Loc,
93 DeclarationName Entity)
94 : SemaRef(SemaRef), TemplateArgs(TemplateArgs),
95 NumTemplateArgs(NumTemplateArgs), Loc(Loc), Entity(Entity) { }
96
97 QualType operator()(QualType T) const { return Instantiate(T); }
98
99 QualType Instantiate(QualType T) const;
100
101 // Declare instantiate functions for each type.
102#define TYPE(Class, Base) \
103 QualType Instantiate##Class##Type(const Class##Type *T, \
104 unsigned Quals) const;
105#define ABSTRACT_TYPE(Class, Base)
106#include "clang/AST/TypeNodes.def"
107 };
108}
109
110QualType
111TemplateTypeInstantiator::InstantiateExtQualType(const ExtQualType *T,
112 unsigned Quals) const {
Douglas Gregor74296542009-02-27 19:31:52 +0000113 // FIXME: Implement this
114 assert(false && "Cannot instantiate ExtQualType yet");
115 return QualType();
116}
117
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000118QualType
119TemplateTypeInstantiator::InstantiateBuiltinType(const BuiltinType *T,
120 unsigned Quals) const {
Douglas Gregorc0139ca2009-02-28 01:04:19 +0000121 assert(false && "Builtin types are not dependent and cannot be instantiated");
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000122 return QualType(T, Quals);
Douglas Gregor74296542009-02-27 19:31:52 +0000123}
124
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000125QualType
126TemplateTypeInstantiator::
127InstantiateFixedWidthIntType(const FixedWidthIntType *T, unsigned Quals) const {
Douglas Gregor74296542009-02-27 19:31:52 +0000128 // FIXME: Implement this
129 assert(false && "Cannot instantiate FixedWidthIntType yet");
130 return QualType();
131}
132
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000133QualType
134TemplateTypeInstantiator::InstantiateComplexType(const ComplexType *T,
135 unsigned Quals) const {
Douglas Gregor74296542009-02-27 19:31:52 +0000136 // FIXME: Implement this
137 assert(false && "Cannot instantiate ComplexType yet");
138 return QualType();
139}
140
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000141QualType
142TemplateTypeInstantiator::InstantiatePointerType(const PointerType *T,
143 unsigned Quals) const {
144 QualType PointeeType = Instantiate(T->getPointeeType());
145 if (PointeeType.isNull())
146 return QualType();
147
148 return SemaRef.BuildPointerType(PointeeType, Quals, Loc, Entity);
Douglas Gregor74296542009-02-27 19:31:52 +0000149}
150
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000151QualType
152TemplateTypeInstantiator::InstantiateBlockPointerType(const BlockPointerType *T,
153 unsigned Quals) const {
Douglas Gregor74296542009-02-27 19:31:52 +0000154 // FIXME: Implement this
155 assert(false && "Cannot instantiate BlockPointerType yet");
156 return QualType();
157}
158
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000159QualType
160TemplateTypeInstantiator::InstantiateReferenceType(const ReferenceType *T,
161 unsigned Quals) const {
162 QualType ReferentType = Instantiate(T->getPointeeType());
163 if (ReferentType.isNull())
164 return QualType();
165
166 return SemaRef.BuildReferenceType(ReferentType, Quals, Loc, Entity);
Douglas Gregor74296542009-02-27 19:31:52 +0000167}
168
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000169QualType
170TemplateTypeInstantiator::
171InstantiateMemberPointerType(const MemberPointerType *T,
172 unsigned Quals) const {
Douglas Gregor74296542009-02-27 19:31:52 +0000173 // FIXME: Implement this
174 assert(false && "Cannot instantiate MemberPointerType yet");
175 return QualType();
176}
177
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000178QualType
179TemplateTypeInstantiator::
180InstantiateConstantArrayType(const ConstantArrayType *T,
181 unsigned Quals) const {
182 QualType ElementType = Instantiate(T->getElementType());
183 if (ElementType.isNull())
184 return ElementType;
185
186 // Build a temporary integer literal to specify the size for
187 // BuildArrayType. Since we have already checked the size as part of
188 // creating the dependent array type in the first place, we know
189 // there aren't any errors.
Douglas Gregor448fcd32009-03-09 20:07:22 +0000190 // FIXME: Is IntTy big enough? Maybe not, but LongLongTy causes
191 // problems that I have yet to investigate.
192 IntegerLiteral ArraySize(T->getSize(), SemaRef.Context.IntTy, Loc);
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000193 return SemaRef.BuildArrayType(ElementType, T->getSizeModifier(),
194 &ArraySize, T->getIndexTypeQualifier(),
195 Loc, Entity);
Douglas Gregor74296542009-02-27 19:31:52 +0000196}
197
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000198QualType
199TemplateTypeInstantiator::
200InstantiateIncompleteArrayType(const IncompleteArrayType *T,
201 unsigned Quals) const {
202 QualType ElementType = Instantiate(T->getElementType());
203 if (ElementType.isNull())
204 return ElementType;
205
206 return SemaRef.BuildArrayType(ElementType, T->getSizeModifier(),
207 0, T->getIndexTypeQualifier(),
208 Loc, Entity);
Douglas Gregor74296542009-02-27 19:31:52 +0000209}
210
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000211QualType
212TemplateTypeInstantiator::
213InstantiateVariableArrayType(const VariableArrayType *T,
214 unsigned Quals) const {
Douglas Gregor74296542009-02-27 19:31:52 +0000215 // FIXME: Implement this
216 assert(false && "Cannot instantiate VariableArrayType yet");
217 return QualType();
218}
219
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000220QualType
221TemplateTypeInstantiator::
222InstantiateDependentSizedArrayType(const DependentSizedArrayType *T,
223 unsigned Quals) const {
Douglas Gregor74296542009-02-27 19:31:52 +0000224 // FIXME: Implement this
225 assert(false && "Cannot instantiate DependentSizedArrayType yet");
226 return QualType();
227}
228
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000229QualType
230TemplateTypeInstantiator::InstantiateVectorType(const VectorType *T,
231 unsigned Quals) const {
Douglas Gregor74296542009-02-27 19:31:52 +0000232 // FIXME: Implement this
233 assert(false && "Cannot instantiate VectorType yet");
234 return QualType();
235}
236
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000237QualType
238TemplateTypeInstantiator::InstantiateExtVectorType(const ExtVectorType *T,
239 unsigned Quals) const {
Douglas Gregor74296542009-02-27 19:31:52 +0000240 // FIXME: Implement this
241 assert(false && "Cannot instantiate ExtVectorType yet");
242 return QualType();
243}
244
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000245QualType
246TemplateTypeInstantiator::
247InstantiateFunctionProtoType(const FunctionProtoType *T,
248 unsigned Quals) const {
Douglas Gregorc0139ca2009-02-28 01:04:19 +0000249 QualType ResultType = Instantiate(T->getResultType());
250 if (ResultType.isNull())
251 return ResultType;
252
253 llvm::SmallVector<QualType, 16> ParamTypes;
254 for (FunctionProtoType::arg_type_iterator Param = T->arg_type_begin(),
255 ParamEnd = T->arg_type_end();
256 Param != ParamEnd; ++Param) {
257 QualType P = Instantiate(*Param);
258 if (P.isNull())
259 return P;
260
261 ParamTypes.push_back(P);
262 }
263
264 return SemaRef.BuildFunctionType(ResultType, &ParamTypes[0],
265 ParamTypes.size(),
266 T->isVariadic(), T->getTypeQuals(),
267 Loc, Entity);
Douglas Gregor74296542009-02-27 19:31:52 +0000268}
269
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000270QualType
271TemplateTypeInstantiator::
272InstantiateFunctionNoProtoType(const FunctionNoProtoType *T,
273 unsigned Quals) const {
Douglas Gregorc0139ca2009-02-28 01:04:19 +0000274 assert(false && "Functions without prototypes cannot be dependent.");
Douglas Gregor74296542009-02-27 19:31:52 +0000275 return QualType();
276}
277
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000278QualType
279TemplateTypeInstantiator::InstantiateTypedefType(const TypedefType *T,
280 unsigned Quals) const {
Douglas Gregor74296542009-02-27 19:31:52 +0000281 // FIXME: Implement this
282 assert(false && "Cannot instantiate TypedefType yet");
283 return QualType();
284}
285
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000286QualType
287TemplateTypeInstantiator::InstantiateTypeOfExprType(const TypeOfExprType *T,
288 unsigned Quals) const {
Douglas Gregor74296542009-02-27 19:31:52 +0000289 // FIXME: Implement this
290 assert(false && "Cannot instantiate TypeOfExprType yet");
291 return QualType();
292}
293
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000294QualType
295TemplateTypeInstantiator::InstantiateTypeOfType(const TypeOfType *T,
296 unsigned Quals) const {
Douglas Gregor74296542009-02-27 19:31:52 +0000297 // FIXME: Implement this
298 assert(false && "Cannot instantiate TypeOfType yet");
299 return QualType();
300}
301
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000302QualType
303TemplateTypeInstantiator::InstantiateRecordType(const RecordType *T,
304 unsigned Quals) const {
Douglas Gregor74296542009-02-27 19:31:52 +0000305 // FIXME: Implement this
306 assert(false && "Cannot instantiate RecordType yet");
307 return QualType();
308}
309
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000310QualType
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000311TemplateTypeInstantiator::InstantiateEnumType(const EnumType *T,
312 unsigned Quals) const {
Douglas Gregor74296542009-02-27 19:31:52 +0000313 // FIXME: Implement this
314 assert(false && "Cannot instantiate EnumType yet");
315 return QualType();
316}
317
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000318QualType
319TemplateTypeInstantiator::
320InstantiateTemplateTypeParmType(const TemplateTypeParmType *T,
321 unsigned Quals) const {
Douglas Gregor74296542009-02-27 19:31:52 +0000322 if (T->getDepth() == 0) {
323 // Replace the template type parameter with its corresponding
324 // template argument.
325 assert(T->getIndex() < NumTemplateArgs && "Wrong # of template args");
326 assert(TemplateArgs[T->getIndex()].getKind() == TemplateArgument::Type &&
327 "Template argument kind mismatch");
328 QualType Result = TemplateArgs[T->getIndex()].getAsType();
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000329 if (Result.isNull() || !Quals)
Douglas Gregor74296542009-02-27 19:31:52 +0000330 return Result;
331
332 // C++ [dcl.ref]p1:
333 // [...] Cv-qualified references are ill-formed except when
334 // the cv-qualifiers are introduced through the use of a
335 // typedef (7.1.3) or of a template type argument (14.3), in
336 // which case the cv-qualifiers are ignored.
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000337 if (Quals && Result->isReferenceType())
338 Quals = 0;
Douglas Gregor74296542009-02-27 19:31:52 +0000339
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000340 return QualType(Result.getTypePtr(), Quals | Result.getCVRQualifiers());
Douglas Gregor74296542009-02-27 19:31:52 +0000341 }
342
343 // The template type parameter comes from an inner template (e.g.,
344 // the template parameter list of a member template inside the
345 // template we are instantiating). Create a new template type
346 // parameter with the template "level" reduced by one.
347 return SemaRef.Context.getTemplateTypeParmType(T->getDepth() - 1,
348 T->getIndex(),
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000349 T->getName())
350 .getQualifiedType(Quals);
Douglas Gregor74296542009-02-27 19:31:52 +0000351}
352
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000353QualType
354TemplateTypeInstantiator::
355InstantiateClassTemplateSpecializationType(
356 const ClassTemplateSpecializationType *T,
357 unsigned Quals) const {
Douglas Gregorf9ff4b12009-03-09 23:48:35 +0000358 llvm::SmallVector<TemplateArgument, 16> InstantiatedTemplateArgs;
359 InstantiatedTemplateArgs.reserve(T->getNumArgs());
360 for (ClassTemplateSpecializationType::iterator Arg = T->begin(),
361 ArgEnd = T->end();
362 Arg != ArgEnd; ++Arg) {
363 switch (Arg->getKind()) {
364 case TemplateArgument::Type: {
365 QualType T = SemaRef.InstantiateType(Arg->getAsType(),
366 TemplateArgs, NumTemplateArgs,
367 Arg->getLocation(),
368 DeclarationName());
369 if (T.isNull())
370 return QualType();
371
372 InstantiatedTemplateArgs.push_back(
373 TemplateArgument(Arg->getLocation(), T));
374 break;
375 }
376
377 case TemplateArgument::Declaration:
378 case TemplateArgument::Integral:
379 InstantiatedTemplateArgs.push_back(*Arg);
380 break;
381
382 case TemplateArgument::Expression:
383 assert(false && "Cannot instantiate expressions yet");
384 break;
385 }
386 }
387
388 // FIXME: We're missing the locations of the template name, '<', and
389 // '>'.
390 return SemaRef.CheckClassTemplateId(cast<ClassTemplateDecl>(T->getTemplate()),
391 Loc,
392 SourceLocation(),
393 &InstantiatedTemplateArgs[0],
394 InstantiatedTemplateArgs.size(),
395 SourceLocation());
Douglas Gregor74296542009-02-27 19:31:52 +0000396}
397
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000398QualType
399TemplateTypeInstantiator::
400InstantiateObjCInterfaceType(const ObjCInterfaceType *T,
401 unsigned Quals) const {
402 assert(false && "Objective-C types cannot be dependent");
Douglas Gregor74296542009-02-27 19:31:52 +0000403 return QualType();
404}
405
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000406QualType
407TemplateTypeInstantiator::
408InstantiateObjCQualifiedInterfaceType(const ObjCQualifiedInterfaceType *T,
409 unsigned Quals) const {
410 assert(false && "Objective-C types cannot be dependent");
Douglas Gregor74296542009-02-27 19:31:52 +0000411 return QualType();
412}
413
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000414QualType
415TemplateTypeInstantiator::
416InstantiateObjCQualifiedIdType(const ObjCQualifiedIdType *T,
417 unsigned Quals) const {
418 assert(false && "Objective-C types cannot be dependent");
Douglas Gregor74296542009-02-27 19:31:52 +0000419 return QualType();
420}
421
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000422QualType
423TemplateTypeInstantiator::
424InstantiateObjCQualifiedClassType(const ObjCQualifiedClassType *T,
425 unsigned Quals) const {
426 assert(false && "Objective-C types cannot be dependent");
Douglas Gregor74296542009-02-27 19:31:52 +0000427 return QualType();
428}
429
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000430/// \brief The actual implementation of Sema::InstantiateType().
431QualType TemplateTypeInstantiator::Instantiate(QualType T) const {
432 // If T is not a dependent type, there is nothing to do.
433 if (!T->isDependentType())
434 return T;
435
436 switch (T->getTypeClass()) {
437#define TYPE(Class, Base) \
438 case Type::Class: \
439 return Instantiate##Class##Type(cast<Class##Type>(T.getTypePtr()), \
440 T.getCVRQualifiers());
441#define ABSTRACT_TYPE(Class, Base)
442#include "clang/AST/TypeNodes.def"
443 }
444
445 assert(false && "Not all types have been decoded for instantiation");
446 return QualType();
447}
Douglas Gregor74296542009-02-27 19:31:52 +0000448
449/// \brief Instantiate the type T with a given set of template arguments.
450///
451/// This routine substitutes the given template arguments into the
452/// type T and produces the instantiated type.
453///
454/// \param T the type into which the template arguments will be
455/// substituted. If this type is not dependent, it will be returned
456/// immediately.
457///
458/// \param TemplateArgs the template arguments that will be
459/// substituted for the top-level template parameters within T.
460///
461/// \param NumTemplateArgs the number of template arguments provided
462/// by TemplateArgs.
463///
464/// \param Loc the location in the source code where this substitution
465/// is being performed. It will typically be the location of the
466/// declarator (if we're instantiating the type of some declaration)
467/// or the location of the type in the source code (if, e.g., we're
468/// instantiating the type of a cast expression).
469///
470/// \param Entity the name of the entity associated with a declaration
471/// being instantiated (if any). May be empty to indicate that there
472/// is no such entity (if, e.g., this is a type that occurs as part of
473/// a cast expression) or that the entity has no name (e.g., an
474/// unnamed function parameter).
475///
476/// \returns If the instantiation succeeds, the instantiated
477/// type. Otherwise, produces diagnostics and returns a NULL type.
478QualType Sema::InstantiateType(QualType T,
479 const TemplateArgument *TemplateArgs,
480 unsigned NumTemplateArgs,
481 SourceLocation Loc, DeclarationName Entity) {
482 // If T is not a dependent type, there is nothing to do.
483 if (!T->isDependentType())
484 return T;
485
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000486 TemplateTypeInstantiator Instantiator(*this, TemplateArgs, NumTemplateArgs,
487 Loc, Entity);
488 return Instantiator(T);
Douglas Gregor74296542009-02-27 19:31:52 +0000489}
Douglas Gregored3a3982009-03-03 04:44:36 +0000490
491/// \brief Instantiate the base class specifiers of the given class
492/// template specialization.
493///
494/// Produces a diagnostic and returns true on error, returns false and
495/// attaches the instantiated base classes to the class template
496/// specialization if successful.
497bool
498Sema::InstantiateBaseSpecifiers(
499 ClassTemplateSpecializationDecl *ClassTemplateSpec,
500 ClassTemplateDecl *ClassTemplate) {
501 bool Invalid = false;
502 llvm::SmallVector<CXXBaseSpecifier*, 8> InstantiatedBases;
503 for (ClassTemplateSpecializationDecl::base_class_iterator
504 Base = ClassTemplate->getTemplatedDecl()->bases_begin(),
505 BaseEnd = ClassTemplate->getTemplatedDecl()->bases_end();
506 Base != BaseEnd && !Invalid; ++Base) {
507 if (!Base->getType()->isDependentType()) {
508 // FIXME: Allocate via ASTContext
509 InstantiatedBases.push_back(new CXXBaseSpecifier(*Base));
510 continue;
511 }
512
513 QualType BaseType = InstantiateType(Base->getType(),
514 ClassTemplateSpec->getTemplateArgs(),
515 ClassTemplateSpec->getNumTemplateArgs(),
516 Base->getSourceRange().getBegin(),
517 DeclarationName());
518 if (BaseType.isNull()) {
519 Invalid = true;
520 continue;
521 }
522
523 if (CXXBaseSpecifier *InstantiatedBase
524 = CheckBaseSpecifier(ClassTemplateSpec,
525 Base->getSourceRange(),
526 Base->isVirtual(),
527 Base->getAccessSpecifierAsWritten(),
528 BaseType,
529 /*FIXME: Not totally accurate */
530 Base->getSourceRange().getBegin()))
531 InstantiatedBases.push_back(InstantiatedBase);
532 else
533 Invalid = true;
534 }
535
536 if (AttachBaseSpecifiers(ClassTemplateSpec, &InstantiatedBases[0],
537 InstantiatedBases.size()))
538 Invalid = true;
539
540 return Invalid;
541}
542
543bool
544Sema::InstantiateClassTemplateSpecialization(
545 ClassTemplateSpecializationDecl *ClassTemplateSpec,
546 bool ExplicitInstantiation) {
547 // Perform the actual instantiation on the canonical declaration.
548 ClassTemplateSpec = cast<ClassTemplateSpecializationDecl>(
549 Context.getCanonicalDecl(ClassTemplateSpec));
550
551 // We can only instantiate something that hasn't already been
552 // instantiated or specialized. Fail without any diagnostics: our
553 // caller will provide an error message.
554 if (ClassTemplateSpec->getSpecializationKind() != TSK_Undeclared)
555 return true;
556
557 // FIXME: Push this class template instantiation onto the
558 // instantiation stack, checking for recursion that exceeds a
559 // certain depth.
560
561 // FIXME: Perform class template partial specialization to select
562 // the best template.
563 ClassTemplateDecl *Template = ClassTemplateSpec->getSpecializedTemplate();
564
565 if (!Template->getTemplatedDecl()->getDefinition(Context)) {
566 Diag(ClassTemplateSpec->getLocation(),
567 diag::err_template_implicit_instantiate_undefined)
568 << Context.getTypeDeclType(ClassTemplateSpec);
569 Diag(Template->getTemplatedDecl()->getLocation(),
570 diag::note_template_decl_here);
571 return true;
572 }
573
574 // Note that this is an instantiation.
575 ClassTemplateSpec->setSpecializationKind(
576 ExplicitInstantiation? TSK_ExplicitInstantiation
577 : TSK_ImplicitInstantiation);
578
579
580 bool Invalid = false;
581
Douglas Gregor375733c2009-03-10 00:06:19 +0000582 InstantiatingTemplate Inst(*this, ClassTemplateSpec->getLocation(),
583 ClassTemplateSpec);
584 if (Inst)
585 return true;
586
Douglas Gregored3a3982009-03-03 04:44:36 +0000587 // Enter the scope of this instantiation. We don't use
588 // PushDeclContext because we don't have a scope.
589 DeclContext *PreviousContext = CurContext;
590 CurContext = ClassTemplateSpec;
591
592 // Start the definition of this instantiation.
593 ClassTemplateSpec->startDefinition();
594
595 // FIXME: Create the injected-class-name for the
596 // instantiation. Should this be a typedef or something like it?
597
598 // Instantiate the base class specifiers.
599 if (InstantiateBaseSpecifiers(ClassTemplateSpec, Template))
600 Invalid = true;
601
602 // FIXME: Instantiate all of the members.
603
604 // Add any implicitly-declared members that we might need.
605 AddImplicitlyDeclaredMembersToClass(ClassTemplateSpec);
606
607 // Finish the definition of this instantiation.
608 // FIXME: ActOnFields does more checking, which we'll eventually need.
609 ClassTemplateSpec->completeDefinition(Context);
610
611 // Exit the scope of this instantiation.
612 CurContext = PreviousContext;
613
614 return Invalid;
615}