blob: 4d271a383099c06d7d1df619cfcb66813c02f2eb [file] [log] [blame]
Douglas Gregor74296542009-02-27 19:31:52 +00001//===------- SemaTemplateInstantiate.cpp - C++ Template Instantiation ------===/
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//===----------------------------------------------------------------------===/
8//
9// This file implements C++ template instantiation.
10//
11//===----------------------------------------------------------------------===/
12
13#include "Sema.h"
14#include "clang/AST/ASTContext.h"
15#include "clang/AST/Expr.h"
Douglas Gregor74296542009-02-27 19:31:52 +000016#include "clang/AST/DeclTemplate.h"
17#include "clang/Parse/DeclSpec.h"
18#include "clang/Basic/LangOptions.h"
Douglas Gregorf57dcd02009-02-28 00:25:32 +000019#include "llvm/Support/Compiler.h"
Douglas Gregor74296542009-02-27 19:31:52 +000020
21using namespace clang;
22
Douglas Gregorfee85d62009-03-10 18:03:33 +000023//===----------------------------------------------------------------------===/
24// Template Instantiation Support
25//===----------------------------------------------------------------------===/
26
Douglas Gregor375733c2009-03-10 00:06:19 +000027Sema::InstantiatingTemplate::
28InstantiatingTemplate(Sema &SemaRef, SourceLocation PointOfInstantiation,
29 ClassTemplateSpecializationDecl *Entity,
30 SourceRange InstantiationRange)
31 : SemaRef(SemaRef) {
Douglas Gregor56d25a72009-03-10 20:44:00 +000032
33 Invalid = CheckInstantiationDepth(PointOfInstantiation,
34 InstantiationRange);
35 if (!Invalid) {
Douglas Gregor375733c2009-03-10 00:06:19 +000036 ActiveTemplateInstantiation Inst;
Douglas Gregor56d25a72009-03-10 20:44:00 +000037 Inst.Kind = ActiveTemplateInstantiation::TemplateInstantiation;
Douglas Gregor375733c2009-03-10 00:06:19 +000038 Inst.PointOfInstantiation = PointOfInstantiation;
Douglas Gregor56d25a72009-03-10 20:44:00 +000039 Inst.Entity = reinterpret_cast<uintptr_t>(Entity);
Douglas Gregor95ba1282009-03-12 18:36:18 +000040 Inst.TemplateArgs = 0;
41 Inst.NumTemplateArgs = 0;
Douglas Gregor56d25a72009-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 Gregor375733c2009-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 Gregor56d25a72009-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 Gregorfee85d62009-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 Gregor56d25a72009-03-10 20:44:00 +0000101 switch (Active->Kind) {
102 case ActiveTemplateInstantiation::TemplateInstantiation: {
103 ClassTemplateSpecializationDecl *Spec
104 = cast<ClassTemplateSpecializationDecl>((Decl*)Active->Entity);
105 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
106 diag::note_template_class_instantiation_here)
107 << Context.getTypeDeclType(Spec)
108 << Active->InstantiationRange;
109 break;
110 }
111
112 case ActiveTemplateInstantiation::DefaultTemplateArgumentInstantiation: {
113 TemplateDecl *Template = cast<TemplateDecl>((Decl *)Active->Entity);
114 std::string TemplateArgsStr
115 = ClassTemplateSpecializationType::PrintTemplateArgumentList(
116 Active->TemplateArgs,
117 Active->NumTemplateArgs);
118 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
119 diag::note_default_arg_instantiation_here)
120 << (Template->getNameAsString() + TemplateArgsStr)
121 << Active->InstantiationRange;
122 break;
123 }
124 }
Douglas Gregorfee85d62009-03-10 18:03:33 +0000125 }
126}
127
Douglas Gregor74296542009-02-27 19:31:52 +0000128//===----------------------------------------------------------------------===/
129// Template Instantiation for Types
130//===----------------------------------------------------------------------===/
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000131namespace {
132 class VISIBILITY_HIDDEN TemplateTypeInstantiator {
133 Sema &SemaRef;
134 const TemplateArgument *TemplateArgs;
135 unsigned NumTemplateArgs;
136 SourceLocation Loc;
137 DeclarationName Entity;
Douglas Gregor74296542009-02-27 19:31:52 +0000138
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000139 public:
140 TemplateTypeInstantiator(Sema &SemaRef,
141 const TemplateArgument *TemplateArgs,
142 unsigned NumTemplateArgs,
143 SourceLocation Loc,
144 DeclarationName Entity)
145 : SemaRef(SemaRef), TemplateArgs(TemplateArgs),
146 NumTemplateArgs(NumTemplateArgs), Loc(Loc), Entity(Entity) { }
147
148 QualType operator()(QualType T) const { return Instantiate(T); }
149
150 QualType Instantiate(QualType T) const;
151
152 // Declare instantiate functions for each type.
153#define TYPE(Class, Base) \
154 QualType Instantiate##Class##Type(const Class##Type *T, \
155 unsigned Quals) const;
156#define ABSTRACT_TYPE(Class, Base)
157#include "clang/AST/TypeNodes.def"
158 };
159}
160
161QualType
162TemplateTypeInstantiator::InstantiateExtQualType(const ExtQualType *T,
163 unsigned Quals) const {
Douglas Gregor74296542009-02-27 19:31:52 +0000164 // FIXME: Implement this
165 assert(false && "Cannot instantiate ExtQualType yet");
166 return QualType();
167}
168
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000169QualType
170TemplateTypeInstantiator::InstantiateBuiltinType(const BuiltinType *T,
171 unsigned Quals) const {
Douglas Gregorc0139ca2009-02-28 01:04:19 +0000172 assert(false && "Builtin types are not dependent and cannot be instantiated");
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000173 return QualType(T, Quals);
Douglas Gregor74296542009-02-27 19:31:52 +0000174}
175
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000176QualType
177TemplateTypeInstantiator::
178InstantiateFixedWidthIntType(const FixedWidthIntType *T, unsigned Quals) const {
Douglas Gregor74296542009-02-27 19:31:52 +0000179 // FIXME: Implement this
180 assert(false && "Cannot instantiate FixedWidthIntType yet");
181 return QualType();
182}
183
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000184QualType
185TemplateTypeInstantiator::InstantiateComplexType(const ComplexType *T,
186 unsigned Quals) const {
Douglas Gregor74296542009-02-27 19:31:52 +0000187 // FIXME: Implement this
188 assert(false && "Cannot instantiate ComplexType yet");
189 return QualType();
190}
191
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000192QualType
193TemplateTypeInstantiator::InstantiatePointerType(const PointerType *T,
194 unsigned Quals) const {
195 QualType PointeeType = Instantiate(T->getPointeeType());
196 if (PointeeType.isNull())
197 return QualType();
198
199 return SemaRef.BuildPointerType(PointeeType, Quals, Loc, Entity);
Douglas Gregor74296542009-02-27 19:31:52 +0000200}
201
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000202QualType
203TemplateTypeInstantiator::InstantiateBlockPointerType(const BlockPointerType *T,
204 unsigned Quals) const {
Douglas Gregor74296542009-02-27 19:31:52 +0000205 // FIXME: Implement this
206 assert(false && "Cannot instantiate BlockPointerType yet");
207 return QualType();
208}
209
Sebastian Redlce6fff02009-03-16 23:22:08 +0000210QualType
211TemplateTypeInstantiator::InstantiateLValueReferenceType(
212 const LValueReferenceType *T, unsigned Quals) const {
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000213 QualType ReferentType = Instantiate(T->getPointeeType());
214 if (ReferentType.isNull())
215 return QualType();
216
Sebastian Redlce6fff02009-03-16 23:22:08 +0000217 return SemaRef.BuildReferenceType(ReferentType, true, Quals, Loc, Entity);
218}
219
220QualType
221TemplateTypeInstantiator::InstantiateRValueReferenceType(
222 const RValueReferenceType *T, unsigned Quals) const {
223 QualType ReferentType = Instantiate(T->getPointeeType());
224 if (ReferentType.isNull())
225 return QualType();
226
227 return SemaRef.BuildReferenceType(ReferentType, false, Quals, Loc, Entity);
Douglas Gregor74296542009-02-27 19:31:52 +0000228}
229
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000230QualType
231TemplateTypeInstantiator::
232InstantiateMemberPointerType(const MemberPointerType *T,
233 unsigned Quals) const {
Douglas Gregor74296542009-02-27 19:31:52 +0000234 // FIXME: Implement this
235 assert(false && "Cannot instantiate MemberPointerType yet");
236 return QualType();
237}
238
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000239QualType
240TemplateTypeInstantiator::
241InstantiateConstantArrayType(const ConstantArrayType *T,
242 unsigned Quals) const {
243 QualType ElementType = Instantiate(T->getElementType());
244 if (ElementType.isNull())
245 return ElementType;
246
247 // Build a temporary integer literal to specify the size for
248 // BuildArrayType. Since we have already checked the size as part of
249 // creating the dependent array type in the first place, we know
250 // there aren't any errors.
Douglas Gregor448fcd32009-03-09 20:07:22 +0000251 // FIXME: Is IntTy big enough? Maybe not, but LongLongTy causes
252 // problems that I have yet to investigate.
253 IntegerLiteral ArraySize(T->getSize(), SemaRef.Context.IntTy, Loc);
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000254 return SemaRef.BuildArrayType(ElementType, T->getSizeModifier(),
255 &ArraySize, T->getIndexTypeQualifier(),
256 Loc, Entity);
Douglas Gregor74296542009-02-27 19:31:52 +0000257}
258
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000259QualType
260TemplateTypeInstantiator::
261InstantiateIncompleteArrayType(const IncompleteArrayType *T,
262 unsigned Quals) const {
263 QualType ElementType = Instantiate(T->getElementType());
264 if (ElementType.isNull())
265 return ElementType;
266
267 return SemaRef.BuildArrayType(ElementType, T->getSizeModifier(),
268 0, T->getIndexTypeQualifier(),
269 Loc, Entity);
Douglas Gregor74296542009-02-27 19:31:52 +0000270}
271
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000272QualType
273TemplateTypeInstantiator::
274InstantiateVariableArrayType(const VariableArrayType *T,
275 unsigned Quals) const {
Douglas Gregor74296542009-02-27 19:31:52 +0000276 // FIXME: Implement this
277 assert(false && "Cannot instantiate VariableArrayType yet");
278 return QualType();
279}
280
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000281QualType
282TemplateTypeInstantiator::
283InstantiateDependentSizedArrayType(const DependentSizedArrayType *T,
284 unsigned Quals) const {
Anders Carlsson149f2782009-03-15 20:12:13 +0000285 Expr *ArraySize = T->getSizeExpr();
286 assert(ArraySize->isValueDependent() &&
287 "dependent sized array types must have value dependent size expr");
288
289 // Instantiate the element type if needed
290 QualType ElementType = T->getElementType();
291 if (ElementType->isDependentType()) {
292 ElementType = Instantiate(ElementType);
293 if (ElementType.isNull())
294 return QualType();
295 }
296
297 // Instantiate the size expression
298 Sema::OwningExprResult InstantiatedArraySize =
299 SemaRef.InstantiateExpr(ArraySize, TemplateArgs, NumTemplateArgs);
300 if (InstantiatedArraySize.isInvalid())
301 return QualType();
302
303 return SemaRef.BuildArrayType(ElementType, T->getSizeModifier(),
304 (Expr *)InstantiatedArraySize.release(),
305 T->getIndexTypeQualifier(), Loc, Entity);
Douglas Gregor74296542009-02-27 19:31:52 +0000306}
307
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000308QualType
309TemplateTypeInstantiator::InstantiateVectorType(const VectorType *T,
310 unsigned Quals) const {
Douglas Gregor74296542009-02-27 19:31:52 +0000311 // FIXME: Implement this
312 assert(false && "Cannot instantiate VectorType yet");
313 return QualType();
314}
315
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000316QualType
317TemplateTypeInstantiator::InstantiateExtVectorType(const ExtVectorType *T,
318 unsigned Quals) const {
Douglas Gregor74296542009-02-27 19:31:52 +0000319 // FIXME: Implement this
320 assert(false && "Cannot instantiate ExtVectorType yet");
321 return QualType();
322}
323
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000324QualType
325TemplateTypeInstantiator::
326InstantiateFunctionProtoType(const FunctionProtoType *T,
327 unsigned Quals) const {
Douglas Gregorc0139ca2009-02-28 01:04:19 +0000328 QualType ResultType = Instantiate(T->getResultType());
329 if (ResultType.isNull())
330 return ResultType;
331
332 llvm::SmallVector<QualType, 16> ParamTypes;
333 for (FunctionProtoType::arg_type_iterator Param = T->arg_type_begin(),
334 ParamEnd = T->arg_type_end();
335 Param != ParamEnd; ++Param) {
336 QualType P = Instantiate(*Param);
337 if (P.isNull())
338 return P;
339
340 ParamTypes.push_back(P);
341 }
342
343 return SemaRef.BuildFunctionType(ResultType, &ParamTypes[0],
344 ParamTypes.size(),
345 T->isVariadic(), T->getTypeQuals(),
346 Loc, Entity);
Douglas Gregor74296542009-02-27 19:31:52 +0000347}
348
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000349QualType
350TemplateTypeInstantiator::
351InstantiateFunctionNoProtoType(const FunctionNoProtoType *T,
352 unsigned Quals) const {
Douglas Gregorc0139ca2009-02-28 01:04:19 +0000353 assert(false && "Functions without prototypes cannot be dependent.");
Douglas Gregor74296542009-02-27 19:31:52 +0000354 return QualType();
355}
356
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000357QualType
358TemplateTypeInstantiator::InstantiateTypedefType(const TypedefType *T,
359 unsigned Quals) const {
Douglas Gregor74296542009-02-27 19:31:52 +0000360 // FIXME: Implement this
361 assert(false && "Cannot instantiate TypedefType yet");
362 return QualType();
363}
364
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000365QualType
366TemplateTypeInstantiator::InstantiateTypeOfExprType(const TypeOfExprType *T,
367 unsigned Quals) const {
Douglas Gregor74296542009-02-27 19:31:52 +0000368 // FIXME: Implement this
369 assert(false && "Cannot instantiate TypeOfExprType yet");
370 return QualType();
371}
372
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000373QualType
374TemplateTypeInstantiator::InstantiateTypeOfType(const TypeOfType *T,
375 unsigned Quals) const {
Douglas Gregor74296542009-02-27 19:31:52 +0000376 // FIXME: Implement this
377 assert(false && "Cannot instantiate TypeOfType yet");
378 return QualType();
379}
380
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000381QualType
382TemplateTypeInstantiator::InstantiateRecordType(const RecordType *T,
383 unsigned Quals) const {
Douglas Gregor74296542009-02-27 19:31:52 +0000384 // FIXME: Implement this
385 assert(false && "Cannot instantiate RecordType yet");
386 return QualType();
387}
388
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000389QualType
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000390TemplateTypeInstantiator::InstantiateEnumType(const EnumType *T,
391 unsigned Quals) const {
Douglas Gregor74296542009-02-27 19:31:52 +0000392 // FIXME: Implement this
393 assert(false && "Cannot instantiate EnumType yet");
394 return QualType();
395}
396
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000397QualType
398TemplateTypeInstantiator::
399InstantiateTemplateTypeParmType(const TemplateTypeParmType *T,
400 unsigned Quals) const {
Douglas Gregor74296542009-02-27 19:31:52 +0000401 if (T->getDepth() == 0) {
402 // Replace the template type parameter with its corresponding
403 // template argument.
404 assert(T->getIndex() < NumTemplateArgs && "Wrong # of template args");
405 assert(TemplateArgs[T->getIndex()].getKind() == TemplateArgument::Type &&
406 "Template argument kind mismatch");
407 QualType Result = TemplateArgs[T->getIndex()].getAsType();
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000408 if (Result.isNull() || !Quals)
Douglas Gregor74296542009-02-27 19:31:52 +0000409 return Result;
410
411 // C++ [dcl.ref]p1:
412 // [...] Cv-qualified references are ill-formed except when
413 // the cv-qualifiers are introduced through the use of a
414 // typedef (7.1.3) or of a template type argument (14.3), in
415 // which case the cv-qualifiers are ignored.
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000416 if (Quals && Result->isReferenceType())
417 Quals = 0;
Douglas Gregor74296542009-02-27 19:31:52 +0000418
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000419 return QualType(Result.getTypePtr(), Quals | Result.getCVRQualifiers());
Douglas Gregor74296542009-02-27 19:31:52 +0000420 }
421
422 // The template type parameter comes from an inner template (e.g.,
423 // the template parameter list of a member template inside the
424 // template we are instantiating). Create a new template type
425 // parameter with the template "level" reduced by one.
426 return SemaRef.Context.getTemplateTypeParmType(T->getDepth() - 1,
427 T->getIndex(),
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000428 T->getName())
429 .getQualifiedType(Quals);
Douglas Gregor74296542009-02-27 19:31:52 +0000430}
431
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000432QualType
433TemplateTypeInstantiator::
434InstantiateClassTemplateSpecializationType(
435 const ClassTemplateSpecializationType *T,
436 unsigned Quals) const {
Douglas Gregorf9ff4b12009-03-09 23:48:35 +0000437 llvm::SmallVector<TemplateArgument, 16> InstantiatedTemplateArgs;
438 InstantiatedTemplateArgs.reserve(T->getNumArgs());
439 for (ClassTemplateSpecializationType::iterator Arg = T->begin(),
440 ArgEnd = T->end();
441 Arg != ArgEnd; ++Arg) {
442 switch (Arg->getKind()) {
443 case TemplateArgument::Type: {
444 QualType T = SemaRef.InstantiateType(Arg->getAsType(),
445 TemplateArgs, NumTemplateArgs,
446 Arg->getLocation(),
447 DeclarationName());
448 if (T.isNull())
449 return QualType();
450
451 InstantiatedTemplateArgs.push_back(
452 TemplateArgument(Arg->getLocation(), T));
453 break;
454 }
455
456 case TemplateArgument::Declaration:
457 case TemplateArgument::Integral:
458 InstantiatedTemplateArgs.push_back(*Arg);
459 break;
460
461 case TemplateArgument::Expression:
Douglas Gregor396f1142009-03-13 21:01:28 +0000462 Sema::OwningExprResult E
463 = SemaRef.InstantiateExpr(Arg->getAsExpr(), TemplateArgs,
464 NumTemplateArgs);
465 if (E.isInvalid())
466 return QualType();
467 InstantiatedTemplateArgs.push_back((Expr *)E.release());
Douglas Gregorf9ff4b12009-03-09 23:48:35 +0000468 break;
469 }
470 }
471
472 // FIXME: We're missing the locations of the template name, '<', and
473 // '>'.
474 return SemaRef.CheckClassTemplateId(cast<ClassTemplateDecl>(T->getTemplate()),
475 Loc,
476 SourceLocation(),
477 &InstantiatedTemplateArgs[0],
478 InstantiatedTemplateArgs.size(),
479 SourceLocation());
Douglas Gregor74296542009-02-27 19:31:52 +0000480}
481
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000482QualType
483TemplateTypeInstantiator::
Douglas Gregor734b4ba2009-03-19 00:18:19 +0000484InstantiateQualifiedNameType(const QualifiedNameType *T,
485 unsigned Quals) const {
486 assert(false && "Cannot have dependent qualified name types (yet)");
487 return QualType();
488}
489
490QualType
491TemplateTypeInstantiator::
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000492InstantiateObjCInterfaceType(const ObjCInterfaceType *T,
493 unsigned Quals) const {
494 assert(false && "Objective-C types cannot be dependent");
Douglas Gregor74296542009-02-27 19:31:52 +0000495 return QualType();
496}
497
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000498QualType
499TemplateTypeInstantiator::
500InstantiateObjCQualifiedInterfaceType(const ObjCQualifiedInterfaceType *T,
501 unsigned Quals) const {
502 assert(false && "Objective-C types cannot be dependent");
Douglas Gregor74296542009-02-27 19:31:52 +0000503 return QualType();
504}
505
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000506QualType
507TemplateTypeInstantiator::
508InstantiateObjCQualifiedIdType(const ObjCQualifiedIdType *T,
509 unsigned Quals) const {
510 assert(false && "Objective-C types cannot be dependent");
Douglas Gregor74296542009-02-27 19:31:52 +0000511 return QualType();
512}
513
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000514QualType
515TemplateTypeInstantiator::
516InstantiateObjCQualifiedClassType(const ObjCQualifiedClassType *T,
517 unsigned Quals) const {
518 assert(false && "Objective-C types cannot be dependent");
Douglas Gregor74296542009-02-27 19:31:52 +0000519 return QualType();
520}
521
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000522/// \brief The actual implementation of Sema::InstantiateType().
523QualType TemplateTypeInstantiator::Instantiate(QualType T) const {
524 // If T is not a dependent type, there is nothing to do.
525 if (!T->isDependentType())
526 return T;
527
528 switch (T->getTypeClass()) {
529#define TYPE(Class, Base) \
530 case Type::Class: \
531 return Instantiate##Class##Type(cast<Class##Type>(T.getTypePtr()), \
532 T.getCVRQualifiers());
533#define ABSTRACT_TYPE(Class, Base)
534#include "clang/AST/TypeNodes.def"
535 }
536
537 assert(false && "Not all types have been decoded for instantiation");
538 return QualType();
539}
Douglas Gregor74296542009-02-27 19:31:52 +0000540
541/// \brief Instantiate the type T with a given set of template arguments.
542///
543/// This routine substitutes the given template arguments into the
544/// type T and produces the instantiated type.
545///
546/// \param T the type into which the template arguments will be
547/// substituted. If this type is not dependent, it will be returned
548/// immediately.
549///
550/// \param TemplateArgs the template arguments that will be
551/// substituted for the top-level template parameters within T.
552///
553/// \param NumTemplateArgs the number of template arguments provided
554/// by TemplateArgs.
555///
556/// \param Loc the location in the source code where this substitution
557/// is being performed. It will typically be the location of the
558/// declarator (if we're instantiating the type of some declaration)
559/// or the location of the type in the source code (if, e.g., we're
560/// instantiating the type of a cast expression).
561///
562/// \param Entity the name of the entity associated with a declaration
563/// being instantiated (if any). May be empty to indicate that there
564/// is no such entity (if, e.g., this is a type that occurs as part of
565/// a cast expression) or that the entity has no name (e.g., an
566/// unnamed function parameter).
567///
568/// \returns If the instantiation succeeds, the instantiated
569/// type. Otherwise, produces diagnostics and returns a NULL type.
570QualType Sema::InstantiateType(QualType T,
571 const TemplateArgument *TemplateArgs,
572 unsigned NumTemplateArgs,
573 SourceLocation Loc, DeclarationName Entity) {
Douglas Gregor56d25a72009-03-10 20:44:00 +0000574 assert(!ActiveTemplateInstantiations.empty() &&
575 "Cannot perform an instantiation without some context on the "
576 "instantiation stack");
577
Douglas Gregor74296542009-02-27 19:31:52 +0000578 // If T is not a dependent type, there is nothing to do.
579 if (!T->isDependentType())
580 return T;
581
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000582 TemplateTypeInstantiator Instantiator(*this, TemplateArgs, NumTemplateArgs,
583 Loc, Entity);
584 return Instantiator(T);
Douglas Gregor74296542009-02-27 19:31:52 +0000585}
Douglas Gregored3a3982009-03-03 04:44:36 +0000586
587/// \brief Instantiate the base class specifiers of the given class
588/// template specialization.
589///
590/// Produces a diagnostic and returns true on error, returns false and
591/// attaches the instantiated base classes to the class template
592/// specialization if successful.
593bool
594Sema::InstantiateBaseSpecifiers(
595 ClassTemplateSpecializationDecl *ClassTemplateSpec,
596 ClassTemplateDecl *ClassTemplate) {
597 bool Invalid = false;
598 llvm::SmallVector<CXXBaseSpecifier*, 8> InstantiatedBases;
599 for (ClassTemplateSpecializationDecl::base_class_iterator
600 Base = ClassTemplate->getTemplatedDecl()->bases_begin(),
601 BaseEnd = ClassTemplate->getTemplatedDecl()->bases_end();
Douglas Gregord9572a12009-03-10 18:52:44 +0000602 Base != BaseEnd; ++Base) {
Douglas Gregored3a3982009-03-03 04:44:36 +0000603 if (!Base->getType()->isDependentType()) {
604 // FIXME: Allocate via ASTContext
605 InstantiatedBases.push_back(new CXXBaseSpecifier(*Base));
606 continue;
607 }
608
609 QualType BaseType = InstantiateType(Base->getType(),
610 ClassTemplateSpec->getTemplateArgs(),
611 ClassTemplateSpec->getNumTemplateArgs(),
612 Base->getSourceRange().getBegin(),
613 DeclarationName());
614 if (BaseType.isNull()) {
615 Invalid = true;
616 continue;
617 }
618
619 if (CXXBaseSpecifier *InstantiatedBase
620 = CheckBaseSpecifier(ClassTemplateSpec,
621 Base->getSourceRange(),
622 Base->isVirtual(),
623 Base->getAccessSpecifierAsWritten(),
624 BaseType,
625 /*FIXME: Not totally accurate */
626 Base->getSourceRange().getBegin()))
627 InstantiatedBases.push_back(InstantiatedBase);
628 else
629 Invalid = true;
630 }
631
Douglas Gregord9572a12009-03-10 18:52:44 +0000632 if (!Invalid &&
633 AttachBaseSpecifiers(ClassTemplateSpec, &InstantiatedBases[0],
Douglas Gregored3a3982009-03-03 04:44:36 +0000634 InstantiatedBases.size()))
635 Invalid = true;
636
637 return Invalid;
638}
639
640bool
641Sema::InstantiateClassTemplateSpecialization(
642 ClassTemplateSpecializationDecl *ClassTemplateSpec,
643 bool ExplicitInstantiation) {
644 // Perform the actual instantiation on the canonical declaration.
645 ClassTemplateSpec = cast<ClassTemplateSpecializationDecl>(
646 Context.getCanonicalDecl(ClassTemplateSpec));
647
648 // We can only instantiate something that hasn't already been
649 // instantiated or specialized. Fail without any diagnostics: our
650 // caller will provide an error message.
651 if (ClassTemplateSpec->getSpecializationKind() != TSK_Undeclared)
652 return true;
653
654 // FIXME: Push this class template instantiation onto the
655 // instantiation stack, checking for recursion that exceeds a
656 // certain depth.
657
658 // FIXME: Perform class template partial specialization to select
659 // the best template.
660 ClassTemplateDecl *Template = ClassTemplateSpec->getSpecializedTemplate();
661
Douglas Gregor47bde7c2009-03-19 17:26:29 +0000662 RecordDecl *Pattern = cast_or_null<RecordDecl>(
663 Template->getTemplatedDecl()->getDefinition(Context));
664 if (!Pattern) {
Douglas Gregored3a3982009-03-03 04:44:36 +0000665 Diag(ClassTemplateSpec->getLocation(),
666 diag::err_template_implicit_instantiate_undefined)
667 << Context.getTypeDeclType(ClassTemplateSpec);
668 Diag(Template->getTemplatedDecl()->getLocation(),
669 diag::note_template_decl_here);
670 return true;
671 }
672
673 // Note that this is an instantiation.
674 ClassTemplateSpec->setSpecializationKind(
675 ExplicitInstantiation? TSK_ExplicitInstantiation
676 : TSK_ImplicitInstantiation);
677
678
679 bool Invalid = false;
680
Douglas Gregor375733c2009-03-10 00:06:19 +0000681 InstantiatingTemplate Inst(*this, ClassTemplateSpec->getLocation(),
682 ClassTemplateSpec);
683 if (Inst)
684 return true;
685
Douglas Gregored3a3982009-03-03 04:44:36 +0000686 // Enter the scope of this instantiation. We don't use
687 // PushDeclContext because we don't have a scope.
688 DeclContext *PreviousContext = CurContext;
689 CurContext = ClassTemplateSpec;
690
691 // Start the definition of this instantiation.
692 ClassTemplateSpec->startDefinition();
693
Douglas Gregored3a3982009-03-03 04:44:36 +0000694 // Instantiate the base class specifiers.
695 if (InstantiateBaseSpecifiers(ClassTemplateSpec, Template))
696 Invalid = true;
697
Douglas Gregor6e7c27c2009-03-11 16:48:53 +0000698 // FIXME: Create the injected-class-name for the
699 // instantiation. Should this be a typedef or something like it?
700
Douglas Gregor0e518af2009-03-11 18:59:21 +0000701 llvm::SmallVector<DeclTy *, 32> Fields;
Douglas Gregor6e7c27c2009-03-11 16:48:53 +0000702 for (RecordDecl::decl_iterator Member = Pattern->decls_begin(),
703 MemberEnd = Pattern->decls_end();
704 Member != MemberEnd; ++Member) {
Douglas Gregor1b39ef42009-03-17 21:15:40 +0000705 Decl *NewMember = InstantiateDecl(*Member, ClassTemplateSpec,
706 ClassTemplateSpec->getTemplateArgs(),
707 ClassTemplateSpec->getNumTemplateArgs());
708 if (NewMember) {
709 if (NewMember->isInvalidDecl())
Anders Carlssonc45057a2009-03-15 18:44:04 +0000710 Invalid = true;
Douglas Gregor1b39ef42009-03-17 21:15:40 +0000711 else if (FieldDecl *Field = dyn_cast<FieldDecl>(NewMember))
712 Fields.push_back(Field);
713 } else {
714 // FIXME: Eventually, a NULL return will mean that one of the
715 // instantiations was a semantic disaster, and we'll want to set
716 // Invalid = true. For now, we expect to skip some members that
717 // we can't yet handle.
Douglas Gregor6e7c27c2009-03-11 16:48:53 +0000718 }
719 }
720
Douglas Gregor0e518af2009-03-11 18:59:21 +0000721 // Finish checking fields.
722 ActOnFields(0, ClassTemplateSpec->getLocation(), ClassTemplateSpec,
723 &Fields[0], Fields.size(), SourceLocation(), SourceLocation(),
724 0);
725
Douglas Gregored3a3982009-03-03 04:44:36 +0000726 // Add any implicitly-declared members that we might need.
727 AddImplicitlyDeclaredMembersToClass(ClassTemplateSpec);
728
Douglas Gregored3a3982009-03-03 04:44:36 +0000729 // Exit the scope of this instantiation.
730 CurContext = PreviousContext;
731
732 return Invalid;
733}
Douglas Gregor47bde7c2009-03-19 17:26:29 +0000734
735/// \brief Instantiate a sequence of nested-name-specifiers into a
736/// scope specifier.
737CXXScopeSpec
738Sema::InstantiateScopeSpecifier(const NestedNameSpecifier *Components,
739 unsigned NumComponents,
740 SourceRange Range,
741 const TemplateArgument *TemplateArgs,
742 unsigned NumTemplateArgs) {
743 CXXScopeSpec SS;
744 for (unsigned Comp = 0; Comp < NumComponents; ++Comp) {
745 if (Type *T = Components[Comp].getAsType()) {
746 QualType NewT = InstantiateType(QualType(T, 0), TemplateArgs,
747 NumTemplateArgs, Range.getBegin(),
748 DeclarationName());
749 if (NewT.isNull())
750 return SS;
751 NestedNameSpecifier NNS(NewT.getTypePtr());
752 SS.addScopeRep(NNS.getAsOpaquePtr());
753 } else {
754 DeclContext *DC = Components[Comp].getAsDeclContext();
755 // FIXME: injected-class-name might be dependent, and therefore
756 // would need instantiation.
757 NestedNameSpecifier NNS(DC);
758 SS.addScopeRep(NNS.getAsOpaquePtr());
759 }
760 }
761
762 SS.setRange(Range);
763 return SS;
764}