blob: 3a2f6777556bc2ae259fe362dcc836585592c15b [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;
Douglas Gregor7e063902009-05-11 23:53:27 +0000136 const TemplateArgumentList &TemplateArgs;
Douglas Gregorcd281c32009-02-28 00:25:32 +0000137 SourceLocation Loc;
138 DeclarationName Entity;
Douglas Gregor99ebf652009-02-27 19:31:52 +0000139
Douglas Gregorcd281c32009-02-28 00:25:32 +0000140 public:
141 TemplateTypeInstantiator(Sema &SemaRef,
Douglas Gregor7e063902009-05-11 23:53:27 +0000142 const TemplateArgumentList &TemplateArgs,
Douglas Gregorcd281c32009-02-28 00:25:32 +0000143 SourceLocation Loc,
144 DeclarationName Entity)
145 : SemaRef(SemaRef), TemplateArgs(TemplateArgs),
Douglas Gregor7e063902009-05-11 23:53:27 +0000146 Loc(Loc), Entity(Entity) { }
Douglas Gregorcd281c32009-02-28 00:25:32 +0000147
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 Gregor99ebf652009-02-27 19:31:52 +0000164 // FIXME: Implement this
165 assert(false && "Cannot instantiate ExtQualType yet");
166 return QualType();
167}
168
Douglas Gregorcd281c32009-02-28 00:25:32 +0000169QualType
170TemplateTypeInstantiator::InstantiateBuiltinType(const BuiltinType *T,
171 unsigned Quals) const {
Douglas Gregor724651c2009-02-28 01:04:19 +0000172 assert(false && "Builtin types are not dependent and cannot be instantiated");
Douglas Gregorcd281c32009-02-28 00:25:32 +0000173 return QualType(T, Quals);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000174}
175
Douglas Gregorcd281c32009-02-28 00:25:32 +0000176QualType
177TemplateTypeInstantiator::
178InstantiateFixedWidthIntType(const FixedWidthIntType *T, unsigned Quals) const {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000179 // FIXME: Implement this
180 assert(false && "Cannot instantiate FixedWidthIntType yet");
181 return QualType();
182}
183
Douglas Gregorcd281c32009-02-28 00:25:32 +0000184QualType
185TemplateTypeInstantiator::InstantiateComplexType(const ComplexType *T,
186 unsigned Quals) const {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000187 // FIXME: Implement this
188 assert(false && "Cannot instantiate ComplexType yet");
189 return QualType();
190}
191
Douglas Gregorcd281c32009-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 Gregor99ebf652009-02-27 19:31:52 +0000200}
201
Douglas Gregorcd281c32009-02-28 00:25:32 +0000202QualType
203TemplateTypeInstantiator::InstantiateBlockPointerType(const BlockPointerType *T,
204 unsigned Quals) const {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000205 // FIXME: Implement this
206 assert(false && "Cannot instantiate BlockPointerType yet");
207 return QualType();
208}
209
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000210QualType
211TemplateTypeInstantiator::InstantiateLValueReferenceType(
212 const LValueReferenceType *T, unsigned Quals) const {
Douglas Gregorcd281c32009-02-28 00:25:32 +0000213 QualType ReferentType = Instantiate(T->getPointeeType());
214 if (ReferentType.isNull())
215 return QualType();
216
Sebastian Redl7c80bd62009-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 Gregor99ebf652009-02-27 19:31:52 +0000228}
229
Douglas Gregorcd281c32009-02-28 00:25:32 +0000230QualType
231TemplateTypeInstantiator::
232InstantiateMemberPointerType(const MemberPointerType *T,
233 unsigned Quals) const {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000234 // FIXME: Implement this
235 assert(false && "Cannot instantiate MemberPointerType yet");
236 return QualType();
237}
238
Douglas Gregorcd281c32009-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
Douglas Gregorff668032009-05-13 18:28:20 +0000250 // there aren't any errors. However, we do need to determine what
251 // C++ type to give the size expression.
252 llvm::APInt Size = T->getSize();
253 QualType Types[] = {
254 SemaRef.Context.UnsignedCharTy, SemaRef.Context.UnsignedShortTy,
255 SemaRef.Context.UnsignedIntTy, SemaRef.Context.UnsignedLongTy,
256 SemaRef.Context.UnsignedLongLongTy, SemaRef.Context.UnsignedInt128Ty
257 };
258 const unsigned NumTypes = sizeof(Types) / sizeof(QualType);
259 QualType SizeType;
260 for (unsigned I = 0; I != NumTypes; ++I)
261 if (Size.getBitWidth() == SemaRef.Context.getIntWidth(Types[I])) {
262 SizeType = Types[I];
263 break;
264 }
265
266 if (SizeType.isNull())
267 SizeType = SemaRef.Context.getFixedWidthIntType(Size.getBitWidth(), false);
268
269 IntegerLiteral ArraySize(Size, SizeType, Loc);
Douglas Gregorcd281c32009-02-28 00:25:32 +0000270 return SemaRef.BuildArrayType(ElementType, T->getSizeModifier(),
271 &ArraySize, T->getIndexTypeQualifier(),
272 Loc, Entity);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000273}
274
Douglas Gregorcd281c32009-02-28 00:25:32 +0000275QualType
276TemplateTypeInstantiator::
277InstantiateIncompleteArrayType(const IncompleteArrayType *T,
278 unsigned Quals) const {
279 QualType ElementType = Instantiate(T->getElementType());
280 if (ElementType.isNull())
281 return ElementType;
282
283 return SemaRef.BuildArrayType(ElementType, T->getSizeModifier(),
284 0, T->getIndexTypeQualifier(),
285 Loc, Entity);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000286}
287
Douglas Gregorcd281c32009-02-28 00:25:32 +0000288QualType
289TemplateTypeInstantiator::
290InstantiateVariableArrayType(const VariableArrayType *T,
291 unsigned Quals) const {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000292 // FIXME: Implement this
293 assert(false && "Cannot instantiate VariableArrayType yet");
294 return QualType();
295}
296
Douglas Gregorcd281c32009-02-28 00:25:32 +0000297QualType
298TemplateTypeInstantiator::
299InstantiateDependentSizedArrayType(const DependentSizedArrayType *T,
300 unsigned Quals) const {
Anders Carlsson76b1c842009-03-15 20:12:13 +0000301 Expr *ArraySize = T->getSizeExpr();
302 assert(ArraySize->isValueDependent() &&
303 "dependent sized array types must have value dependent size expr");
304
305 // Instantiate the element type if needed
306 QualType ElementType = T->getElementType();
307 if (ElementType->isDependentType()) {
308 ElementType = Instantiate(ElementType);
309 if (ElementType.isNull())
310 return QualType();
311 }
312
313 // Instantiate the size expression
314 Sema::OwningExprResult InstantiatedArraySize =
Douglas Gregor7e063902009-05-11 23:53:27 +0000315 SemaRef.InstantiateExpr(ArraySize, TemplateArgs);
Anders Carlsson76b1c842009-03-15 20:12:13 +0000316 if (InstantiatedArraySize.isInvalid())
317 return QualType();
318
319 return SemaRef.BuildArrayType(ElementType, T->getSizeModifier(),
Anders Carlssone9146f22009-05-01 19:49:17 +0000320 InstantiatedArraySize.takeAs<Expr>(),
Anders Carlsson76b1c842009-03-15 20:12:13 +0000321 T->getIndexTypeQualifier(), Loc, Entity);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000322}
323
Douglas Gregorcd281c32009-02-28 00:25:32 +0000324QualType
325TemplateTypeInstantiator::InstantiateVectorType(const VectorType *T,
326 unsigned Quals) const {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000327 // FIXME: Implement this
328 assert(false && "Cannot instantiate VectorType yet");
329 return QualType();
330}
331
Douglas Gregorcd281c32009-02-28 00:25:32 +0000332QualType
333TemplateTypeInstantiator::InstantiateExtVectorType(const ExtVectorType *T,
334 unsigned Quals) const {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000335 // FIXME: Implement this
336 assert(false && "Cannot instantiate ExtVectorType yet");
337 return QualType();
338}
339
Douglas Gregorcd281c32009-02-28 00:25:32 +0000340QualType
341TemplateTypeInstantiator::
342InstantiateFunctionProtoType(const FunctionProtoType *T,
343 unsigned Quals) const {
Douglas Gregor724651c2009-02-28 01:04:19 +0000344 QualType ResultType = Instantiate(T->getResultType());
345 if (ResultType.isNull())
346 return ResultType;
347
348 llvm::SmallVector<QualType, 16> ParamTypes;
349 for (FunctionProtoType::arg_type_iterator Param = T->arg_type_begin(),
350 ParamEnd = T->arg_type_end();
351 Param != ParamEnd; ++Param) {
352 QualType P = Instantiate(*Param);
353 if (P.isNull())
354 return P;
355
356 ParamTypes.push_back(P);
357 }
358
359 return SemaRef.BuildFunctionType(ResultType, &ParamTypes[0],
360 ParamTypes.size(),
361 T->isVariadic(), T->getTypeQuals(),
362 Loc, Entity);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000363}
364
Douglas Gregorcd281c32009-02-28 00:25:32 +0000365QualType
366TemplateTypeInstantiator::
367InstantiateFunctionNoProtoType(const FunctionNoProtoType *T,
368 unsigned Quals) const {
Douglas Gregor724651c2009-02-28 01:04:19 +0000369 assert(false && "Functions without prototypes cannot be dependent.");
Douglas Gregor99ebf652009-02-27 19:31:52 +0000370 return QualType();
371}
372
Douglas Gregorcd281c32009-02-28 00:25:32 +0000373QualType
374TemplateTypeInstantiator::InstantiateTypedefType(const TypedefType *T,
375 unsigned Quals) const {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000376 // FIXME: Implement this
377 assert(false && "Cannot instantiate TypedefType yet");
378 return QualType();
379}
380
Douglas Gregorcd281c32009-02-28 00:25:32 +0000381QualType
382TemplateTypeInstantiator::InstantiateTypeOfExprType(const TypeOfExprType *T,
383 unsigned Quals) const {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000384 // FIXME: Implement this
385 assert(false && "Cannot instantiate TypeOfExprType yet");
386 return QualType();
387}
388
Douglas Gregorcd281c32009-02-28 00:25:32 +0000389QualType
390TemplateTypeInstantiator::InstantiateTypeOfType(const TypeOfType *T,
391 unsigned Quals) const {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000392 // FIXME: Implement this
393 assert(false && "Cannot instantiate TypeOfType yet");
394 return QualType();
395}
396
Douglas Gregorcd281c32009-02-28 00:25:32 +0000397QualType
398TemplateTypeInstantiator::InstantiateRecordType(const RecordType *T,
399 unsigned Quals) const {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000400 // FIXME: Implement this
401 assert(false && "Cannot instantiate RecordType yet");
402 return QualType();
403}
404
Douglas Gregorcd281c32009-02-28 00:25:32 +0000405QualType
Douglas Gregorcd281c32009-02-28 00:25:32 +0000406TemplateTypeInstantiator::InstantiateEnumType(const EnumType *T,
407 unsigned Quals) const {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000408 // FIXME: Implement this
409 assert(false && "Cannot instantiate EnumType yet");
410 return QualType();
411}
412
Douglas Gregorcd281c32009-02-28 00:25:32 +0000413QualType
414TemplateTypeInstantiator::
415InstantiateTemplateTypeParmType(const TemplateTypeParmType *T,
416 unsigned Quals) const {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000417 if (T->getDepth() == 0) {
418 // Replace the template type parameter with its corresponding
419 // template argument.
Douglas Gregor99ebf652009-02-27 19:31:52 +0000420 assert(TemplateArgs[T->getIndex()].getKind() == TemplateArgument::Type &&
421 "Template argument kind mismatch");
422 QualType Result = TemplateArgs[T->getIndex()].getAsType();
Douglas Gregorcd281c32009-02-28 00:25:32 +0000423 if (Result.isNull() || !Quals)
Douglas Gregor99ebf652009-02-27 19:31:52 +0000424 return Result;
425
426 // C++ [dcl.ref]p1:
427 // [...] Cv-qualified references are ill-formed except when
428 // the cv-qualifiers are introduced through the use of a
429 // typedef (7.1.3) or of a template type argument (14.3), in
430 // which case the cv-qualifiers are ignored.
Douglas Gregorcd281c32009-02-28 00:25:32 +0000431 if (Quals && Result->isReferenceType())
432 Quals = 0;
Douglas Gregor99ebf652009-02-27 19:31:52 +0000433
Douglas Gregorcd281c32009-02-28 00:25:32 +0000434 return QualType(Result.getTypePtr(), Quals | Result.getCVRQualifiers());
Douglas Gregor99ebf652009-02-27 19:31:52 +0000435 }
436
437 // The template type parameter comes from an inner template (e.g.,
438 // the template parameter list of a member template inside the
439 // template we are instantiating). Create a new template type
440 // parameter with the template "level" reduced by one.
441 return SemaRef.Context.getTemplateTypeParmType(T->getDepth() - 1,
442 T->getIndex(),
Douglas Gregorcd281c32009-02-28 00:25:32 +0000443 T->getName())
444 .getQualifiedType(Quals);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000445}
446
Douglas Gregorcd281c32009-02-28 00:25:32 +0000447QualType
448TemplateTypeInstantiator::
Douglas Gregor7532dc62009-03-30 22:58:21 +0000449InstantiateTemplateSpecializationType(
450 const TemplateSpecializationType *T,
Douglas Gregorcd281c32009-02-28 00:25:32 +0000451 unsigned Quals) const {
Douglas Gregor40808ce2009-03-09 23:48:35 +0000452 llvm::SmallVector<TemplateArgument, 16> InstantiatedTemplateArgs;
453 InstantiatedTemplateArgs.reserve(T->getNumArgs());
Douglas Gregor7532dc62009-03-30 22:58:21 +0000454 for (TemplateSpecializationType::iterator Arg = T->begin(), ArgEnd = T->end();
Douglas Gregor40808ce2009-03-09 23:48:35 +0000455 Arg != ArgEnd; ++Arg) {
456 switch (Arg->getKind()) {
457 case TemplateArgument::Type: {
458 QualType T = SemaRef.InstantiateType(Arg->getAsType(),
Douglas Gregor7e063902009-05-11 23:53:27 +0000459 TemplateArgs,
Douglas Gregor40808ce2009-03-09 23:48:35 +0000460 Arg->getLocation(),
461 DeclarationName());
462 if (T.isNull())
463 return QualType();
464
465 InstantiatedTemplateArgs.push_back(
466 TemplateArgument(Arg->getLocation(), T));
467 break;
468 }
469
470 case TemplateArgument::Declaration:
471 case TemplateArgument::Integral:
472 InstantiatedTemplateArgs.push_back(*Arg);
473 break;
474
475 case TemplateArgument::Expression:
Douglas Gregorba498172009-03-13 21:01:28 +0000476 Sema::OwningExprResult E
Douglas Gregor7e063902009-05-11 23:53:27 +0000477 = SemaRef.InstantiateExpr(Arg->getAsExpr(), TemplateArgs);
Douglas Gregorba498172009-03-13 21:01:28 +0000478 if (E.isInvalid())
479 return QualType();
Anders Carlssonf1b1d592009-05-01 19:30:39 +0000480 InstantiatedTemplateArgs.push_back(E.takeAs<Expr>());
Douglas Gregor40808ce2009-03-09 23:48:35 +0000481 break;
482 }
483 }
484
485 // FIXME: We're missing the locations of the template name, '<', and
486 // '>'.
Douglas Gregorde650ae2009-03-31 18:38:02 +0000487
488 TemplateName Name = SemaRef.InstantiateTemplateName(T->getTemplateName(),
489 Loc,
Douglas Gregor7e063902009-05-11 23:53:27 +0000490 TemplateArgs);
Douglas Gregorde650ae2009-03-31 18:38:02 +0000491
492 return SemaRef.CheckTemplateIdType(Name, Loc, SourceLocation(),
Douglas Gregor7532dc62009-03-30 22:58:21 +0000493 &InstantiatedTemplateArgs[0],
494 InstantiatedTemplateArgs.size(),
495 SourceLocation());
Douglas Gregor99ebf652009-02-27 19:31:52 +0000496}
497
Douglas Gregorcd281c32009-02-28 00:25:32 +0000498QualType
499TemplateTypeInstantiator::
Douglas Gregore4e5b052009-03-19 00:18:19 +0000500InstantiateQualifiedNameType(const QualifiedNameType *T,
501 unsigned Quals) const {
Douglas Gregord57959a2009-03-27 23:10:48 +0000502 // When we instantiated a qualified name type, there's no point in
503 // keeping the qualification around in the instantiated result. So,
504 // just instantiate the named type.
505 return (*this)(T->getNamedType());
506}
507
508QualType
509TemplateTypeInstantiator::
510InstantiateTypenameType(const TypenameType *T, unsigned Quals) const {
Douglas Gregor17343172009-04-01 00:28:59 +0000511 if (const TemplateSpecializationType *TemplateId = T->getTemplateId()) {
512 // When the typename type refers to a template-id, the template-id
513 // is dependent and has enough information to instantiate the
514 // result of the typename type. Since we don't care about keeping
515 // the spelling of the typename type in template instantiations,
516 // we just instantiate the template-id.
517 return InstantiateTemplateSpecializationType(TemplateId, Quals);
518 }
519
Douglas Gregord57959a2009-03-27 23:10:48 +0000520 NestedNameSpecifier *NNS
521 = SemaRef.InstantiateNestedNameSpecifier(T->getQualifier(),
522 SourceRange(Loc),
Douglas Gregor7e063902009-05-11 23:53:27 +0000523 TemplateArgs);
Douglas Gregord57959a2009-03-27 23:10:48 +0000524 if (!NNS)
525 return QualType();
526
Douglas Gregor17343172009-04-01 00:28:59 +0000527 return SemaRef.CheckTypenameType(NNS, *T->getIdentifier(), SourceRange(Loc));
Douglas Gregore4e5b052009-03-19 00:18:19 +0000528}
529
530QualType
531TemplateTypeInstantiator::
Douglas Gregorcd281c32009-02-28 00:25:32 +0000532InstantiateObjCInterfaceType(const ObjCInterfaceType *T,
533 unsigned Quals) const {
534 assert(false && "Objective-C types cannot be dependent");
Douglas Gregor99ebf652009-02-27 19:31:52 +0000535 return QualType();
536}
537
Douglas Gregorcd281c32009-02-28 00:25:32 +0000538QualType
539TemplateTypeInstantiator::
540InstantiateObjCQualifiedInterfaceType(const ObjCQualifiedInterfaceType *T,
541 unsigned Quals) const {
542 assert(false && "Objective-C types cannot be dependent");
Douglas Gregor99ebf652009-02-27 19:31:52 +0000543 return QualType();
544}
545
Douglas Gregorcd281c32009-02-28 00:25:32 +0000546QualType
547TemplateTypeInstantiator::
548InstantiateObjCQualifiedIdType(const ObjCQualifiedIdType *T,
549 unsigned Quals) const {
550 assert(false && "Objective-C types cannot be dependent");
Douglas Gregor99ebf652009-02-27 19:31:52 +0000551 return QualType();
552}
553
Douglas Gregorcd281c32009-02-28 00:25:32 +0000554/// \brief The actual implementation of Sema::InstantiateType().
555QualType TemplateTypeInstantiator::Instantiate(QualType T) const {
556 // If T is not a dependent type, there is nothing to do.
557 if (!T->isDependentType())
558 return T;
559
560 switch (T->getTypeClass()) {
561#define TYPE(Class, Base) \
562 case Type::Class: \
563 return Instantiate##Class##Type(cast<Class##Type>(T.getTypePtr()), \
564 T.getCVRQualifiers());
565#define ABSTRACT_TYPE(Class, Base)
566#include "clang/AST/TypeNodes.def"
567 }
568
569 assert(false && "Not all types have been decoded for instantiation");
570 return QualType();
571}
Douglas Gregor99ebf652009-02-27 19:31:52 +0000572
573/// \brief Instantiate the type T with a given set of template arguments.
574///
575/// This routine substitutes the given template arguments into the
576/// type T and produces the instantiated type.
577///
578/// \param T the type into which the template arguments will be
579/// substituted. If this type is not dependent, it will be returned
580/// immediately.
581///
582/// \param TemplateArgs the template arguments that will be
583/// substituted for the top-level template parameters within T.
584///
Douglas Gregor99ebf652009-02-27 19:31:52 +0000585/// \param Loc the location in the source code where this substitution
586/// is being performed. It will typically be the location of the
587/// declarator (if we're instantiating the type of some declaration)
588/// or the location of the type in the source code (if, e.g., we're
589/// instantiating the type of a cast expression).
590///
591/// \param Entity the name of the entity associated with a declaration
592/// being instantiated (if any). May be empty to indicate that there
593/// is no such entity (if, e.g., this is a type that occurs as part of
594/// a cast expression) or that the entity has no name (e.g., an
595/// unnamed function parameter).
596///
597/// \returns If the instantiation succeeds, the instantiated
598/// type. Otherwise, produces diagnostics and returns a NULL type.
599QualType Sema::InstantiateType(QualType T,
Douglas Gregor7e063902009-05-11 23:53:27 +0000600 const TemplateArgumentList &TemplateArgs,
Douglas Gregor99ebf652009-02-27 19:31:52 +0000601 SourceLocation Loc, DeclarationName Entity) {
Douglas Gregordf667e72009-03-10 20:44:00 +0000602 assert(!ActiveTemplateInstantiations.empty() &&
603 "Cannot perform an instantiation without some context on the "
604 "instantiation stack");
605
Douglas Gregor99ebf652009-02-27 19:31:52 +0000606 // If T is not a dependent type, there is nothing to do.
607 if (!T->isDependentType())
608 return T;
609
Douglas Gregor7e063902009-05-11 23:53:27 +0000610 TemplateTypeInstantiator Instantiator(*this, TemplateArgs, Loc, Entity);
Douglas Gregorcd281c32009-02-28 00:25:32 +0000611 return Instantiator(T);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000612}
Douglas Gregor2943aed2009-03-03 04:44:36 +0000613
614/// \brief Instantiate the base class specifiers of the given class
615/// template specialization.
616///
617/// Produces a diagnostic and returns true on error, returns false and
618/// attaches the instantiated base classes to the class template
619/// specialization if successful.
620bool
Douglas Gregord475b8d2009-03-25 21:17:03 +0000621Sema::InstantiateBaseSpecifiers(CXXRecordDecl *Instantiation,
622 CXXRecordDecl *Pattern,
Douglas Gregor7e063902009-05-11 23:53:27 +0000623 const TemplateArgumentList &TemplateArgs) {
Douglas Gregor2943aed2009-03-03 04:44:36 +0000624 bool Invalid = false;
625 llvm::SmallVector<CXXBaseSpecifier*, 8> InstantiatedBases;
Douglas Gregord475b8d2009-03-25 21:17:03 +0000626 for (ClassTemplateSpecializationDecl::base_class_iterator
627 Base = Pattern->bases_begin(), BaseEnd = Pattern->bases_end();
Douglas Gregor27b152f2009-03-10 18:52:44 +0000628 Base != BaseEnd; ++Base) {
Douglas Gregor2943aed2009-03-03 04:44:36 +0000629 if (!Base->getType()->isDependentType()) {
630 // FIXME: Allocate via ASTContext
631 InstantiatedBases.push_back(new CXXBaseSpecifier(*Base));
632 continue;
633 }
634
635 QualType BaseType = InstantiateType(Base->getType(),
Douglas Gregor7e063902009-05-11 23:53:27 +0000636 TemplateArgs,
Douglas Gregor2943aed2009-03-03 04:44:36 +0000637 Base->getSourceRange().getBegin(),
638 DeclarationName());
639 if (BaseType.isNull()) {
640 Invalid = true;
641 continue;
642 }
643
644 if (CXXBaseSpecifier *InstantiatedBase
Douglas Gregord475b8d2009-03-25 21:17:03 +0000645 = CheckBaseSpecifier(Instantiation,
Douglas Gregor2943aed2009-03-03 04:44:36 +0000646 Base->getSourceRange(),
647 Base->isVirtual(),
648 Base->getAccessSpecifierAsWritten(),
649 BaseType,
650 /*FIXME: Not totally accurate */
651 Base->getSourceRange().getBegin()))
652 InstantiatedBases.push_back(InstantiatedBase);
653 else
654 Invalid = true;
655 }
656
Douglas Gregor27b152f2009-03-10 18:52:44 +0000657 if (!Invalid &&
Douglas Gregord475b8d2009-03-25 21:17:03 +0000658 AttachBaseSpecifiers(Instantiation, &InstantiatedBases[0],
Douglas Gregor2943aed2009-03-03 04:44:36 +0000659 InstantiatedBases.size()))
660 Invalid = true;
661
662 return Invalid;
663}
664
Douglas Gregord475b8d2009-03-25 21:17:03 +0000665/// \brief Instantiate the definition of a class from a given pattern.
666///
667/// \param PointOfInstantiation The point of instantiation within the
668/// source code.
669///
670/// \param Instantiation is the declaration whose definition is being
671/// instantiated. This will be either a class template specialization
672/// or a member class of a class template specialization.
673///
674/// \param Pattern is the pattern from which the instantiation
675/// occurs. This will be either the declaration of a class template or
676/// the declaration of a member class of a class template.
677///
678/// \param TemplateArgs The template arguments to be substituted into
679/// the pattern.
680///
Douglas Gregord475b8d2009-03-25 21:17:03 +0000681/// \returns true if an error occurred, false otherwise.
682bool
683Sema::InstantiateClass(SourceLocation PointOfInstantiation,
684 CXXRecordDecl *Instantiation, CXXRecordDecl *Pattern,
Douglas Gregor93dfdb12009-05-13 00:25:59 +0000685 const TemplateArgumentList &TemplateArgs,
686 bool ExplicitInstantiation) {
Douglas Gregord475b8d2009-03-25 21:17:03 +0000687 bool Invalid = false;
688
689 CXXRecordDecl *PatternDef
690 = cast_or_null<CXXRecordDecl>(Pattern->getDefinition(Context));
691 if (!PatternDef) {
692 if (Pattern == Instantiation->getInstantiatedFromMemberClass()) {
693 Diag(PointOfInstantiation,
694 diag::err_implicit_instantiate_member_undefined)
695 << Context.getTypeDeclType(Instantiation);
696 Diag(Pattern->getLocation(), diag::note_member_of_template_here);
697 } else {
Douglas Gregor93dfdb12009-05-13 00:25:59 +0000698 Diag(PointOfInstantiation, diag::err_template_instantiate_undefined)
699 << ExplicitInstantiation
Douglas Gregord475b8d2009-03-25 21:17:03 +0000700 << Context.getTypeDeclType(Instantiation);
701 Diag(Pattern->getLocation(), diag::note_template_decl_here);
702 }
703 return true;
704 }
705 Pattern = PatternDef;
706
Douglas Gregord048bb72009-03-25 21:23:52 +0000707 InstantiatingTemplate Inst(*this, PointOfInstantiation, Instantiation);
Douglas Gregord475b8d2009-03-25 21:17:03 +0000708 if (Inst)
709 return true;
710
711 // Enter the scope of this instantiation. We don't use
712 // PushDeclContext because we don't have a scope.
713 DeclContext *PreviousContext = CurContext;
714 CurContext = Instantiation;
715
716 // Start the definition of this instantiation.
717 Instantiation->startDefinition();
718
719 // Instantiate the base class specifiers.
Douglas Gregor7e063902009-05-11 23:53:27 +0000720 if (InstantiateBaseSpecifiers(Instantiation, Pattern, TemplateArgs))
Douglas Gregord475b8d2009-03-25 21:17:03 +0000721 Invalid = true;
722
Chris Lattnerb28317a2009-03-28 19:18:32 +0000723 llvm::SmallVector<DeclPtrTy, 32> Fields;
Douglas Gregor6ab35242009-04-09 21:40:53 +0000724 for (RecordDecl::decl_iterator Member = Pattern->decls_begin(Context),
725 MemberEnd = Pattern->decls_end(Context);
726 Member != MemberEnd; ++Member) {
Douglas Gregor7e063902009-05-11 23:53:27 +0000727 Decl *NewMember = InstantiateDecl(*Member, Instantiation, TemplateArgs);
Douglas Gregord475b8d2009-03-25 21:17:03 +0000728 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,
Douglas Gregor93dfdb12009-05-13 00:25:59 +0000786 ClassTemplateSpec->getTemplateArgs(),
787 ExplicitInstantiation);
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,
Douglas Gregor7e063902009-05-11 23:53:27 +0000794 const TemplateArgumentList &TemplateArgs) {
Douglas Gregorab452ba2009-03-26 23:50:42 +0000795 // Instantiate the prefix of this nested name specifier.
796 NestedNameSpecifier *Prefix = NNS->getPrefix();
797 if (Prefix) {
Douglas Gregor7e063902009-05-11 23:53:27 +0000798 Prefix = InstantiateNestedNameSpecifier(Prefix, Range, TemplateArgs);
Douglas Gregorab452ba2009-03-26 23:50:42 +0000799 if (!Prefix)
800 return 0;
Douglas Gregor5953d8b2009-03-19 17:26:29 +0000801 }
802
Douglas Gregorab452ba2009-03-26 23:50:42 +0000803 switch (NNS->getKind()) {
Douglas Gregor17343172009-04-01 00:28:59 +0000804 case NestedNameSpecifier::Identifier: {
805 assert(Prefix &&
806 "Can't have an identifier nested-name-specifier with no prefix");
807 CXXScopeSpec SS;
808 // FIXME: The source location information is all wrong.
809 SS.setRange(Range);
810 SS.setScopeRep(Prefix);
811 return static_cast<NestedNameSpecifier *>(
812 ActOnCXXNestedNameSpecifier(0, SS,
813 Range.getEnd(),
814 Range.getEnd(),
815 *NNS->getAsIdentifier()));
Douglas Gregorab452ba2009-03-26 23:50:42 +0000816 break;
Douglas Gregor17343172009-04-01 00:28:59 +0000817 }
Douglas Gregorab452ba2009-03-26 23:50:42 +0000818
819 case NestedNameSpecifier::Namespace:
820 case NestedNameSpecifier::Global:
821 return NNS;
822
823 case NestedNameSpecifier::TypeSpecWithTemplate:
824 case NestedNameSpecifier::TypeSpec: {
825 QualType T = QualType(NNS->getAsType(), 0);
826 if (!T->isDependentType())
827 return NNS;
828
Douglas Gregor7e063902009-05-11 23:53:27 +0000829 T = InstantiateType(T, TemplateArgs, Range.getBegin(), DeclarationName());
Douglas Gregorab452ba2009-03-26 23:50:42 +0000830 if (T.isNull())
831 return 0;
832
Douglas Gregord57959a2009-03-27 23:10:48 +0000833 if (T->isRecordType() ||
834 (getLangOptions().CPlusPlus0x && T->isEnumeralType())) {
Douglas Gregor17343172009-04-01 00:28:59 +0000835 assert(T.getCVRQualifiers() == 0 && "Can't get cv-qualifiers here");
Douglas Gregord57959a2009-03-27 23:10:48 +0000836 return NestedNameSpecifier::Create(Context, Prefix,
Douglas Gregorab452ba2009-03-26 23:50:42 +0000837 NNS->getKind() == NestedNameSpecifier::TypeSpecWithTemplate,
Douglas Gregord57959a2009-03-27 23:10:48 +0000838 T.getTypePtr());
839 }
840
841 Diag(Range.getBegin(), diag::err_nested_name_spec_non_tag) << T;
842 return 0;
Douglas Gregorab452ba2009-03-26 23:50:42 +0000843 }
844 }
845
Douglas Gregord57959a2009-03-27 23:10:48 +0000846 // Required to silence a GCC warning
Douglas Gregorab452ba2009-03-26 23:50:42 +0000847 return 0;
Douglas Gregor5953d8b2009-03-19 17:26:29 +0000848}
Douglas Gregorde650ae2009-03-31 18:38:02 +0000849
850TemplateName
851Sema::InstantiateTemplateName(TemplateName Name, SourceLocation Loc,
Douglas Gregor7e063902009-05-11 23:53:27 +0000852 const TemplateArgumentList &TemplateArgs) {
Douglas Gregorde650ae2009-03-31 18:38:02 +0000853 if (TemplateTemplateParmDecl *TTP
854 = dyn_cast_or_null<TemplateTemplateParmDecl>(
855 Name.getAsTemplateDecl())) {
856 assert(TTP->getDepth() == 0 &&
857 "Cannot reduce depth of a template template parameter");
Douglas Gregor9bde7732009-03-31 20:22:05 +0000858 assert(TemplateArgs[TTP->getPosition()].getAsDecl() &&
Douglas Gregorde650ae2009-03-31 18:38:02 +0000859 "Wrong kind of template template argument");
860 ClassTemplateDecl *ClassTemplate
861 = dyn_cast<ClassTemplateDecl>(
862 TemplateArgs[TTP->getPosition()].getAsDecl());
Douglas Gregor9bde7732009-03-31 20:22:05 +0000863 assert(ClassTemplate && "Expected a class template");
Douglas Gregorde650ae2009-03-31 18:38:02 +0000864 if (QualifiedTemplateName *QTN = Name.getAsQualifiedTemplateName()) {
865 NestedNameSpecifier *NNS
866 = InstantiateNestedNameSpecifier(QTN->getQualifier(),
867 /*FIXME=*/SourceRange(Loc),
Douglas Gregor7e063902009-05-11 23:53:27 +0000868 TemplateArgs);
Douglas Gregorde650ae2009-03-31 18:38:02 +0000869 if (NNS)
870 return Context.getQualifiedTemplateName(NNS,
871 QTN->hasTemplateKeyword(),
872 ClassTemplate);
873 }
874
875 return TemplateName(ClassTemplate);
876 } else if (DependentTemplateName *DTN = Name.getAsDependentTemplateName()) {
877 NestedNameSpecifier *NNS
878 = InstantiateNestedNameSpecifier(DTN->getQualifier(),
879 /*FIXME=*/SourceRange(Loc),
Douglas Gregor7e063902009-05-11 23:53:27 +0000880 TemplateArgs);
Douglas Gregorde650ae2009-03-31 18:38:02 +0000881
882 if (!NNS) // FIXME: Not the best recovery strategy.
883 return Name;
884
885 if (NNS->isDependent())
886 return Context.getDependentTemplateName(NNS, DTN->getName());
887
888 // Somewhat redundant with ActOnDependentTemplateName.
889 CXXScopeSpec SS;
890 SS.setRange(SourceRange(Loc));
891 SS.setScopeRep(NNS);
892 TemplateTy Template;
893 TemplateNameKind TNK = isTemplateName(*DTN->getName(), 0, Template, &SS);
894 if (TNK == TNK_Non_template) {
895 Diag(Loc, diag::err_template_kw_refers_to_non_template)
896 << DTN->getName();
897 return Name;
898 } else if (TNK == TNK_Function_template) {
899 Diag(Loc, diag::err_template_kw_refers_to_non_template)
900 << DTN->getName();
901 return Name;
902 }
903
904 return Template.getAsVal<TemplateName>();
905 }
906
907
908
909 // FIXME: Even if we're referring to a Decl that isn't a template
910 // template parameter, we may need to instantiate the outer contexts
911 // of that Decl. However, this won't be needed until we implement
912 // member templates.
913 return Name;
914}