blob: ce8cbe0f434f04070f9ffc7206d12f3d7f02d3c2 [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
250 // there aren't any errors.
Douglas Gregor8d217212009-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 Gregorcd281c32009-02-28 00:25:32 +0000254 return SemaRef.BuildArrayType(ElementType, T->getSizeModifier(),
255 &ArraySize, T->getIndexTypeQualifier(),
256 Loc, Entity);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000257}
258
Douglas Gregorcd281c32009-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 Gregor99ebf652009-02-27 19:31:52 +0000270}
271
Douglas Gregorcd281c32009-02-28 00:25:32 +0000272QualType
273TemplateTypeInstantiator::
274InstantiateVariableArrayType(const VariableArrayType *T,
275 unsigned Quals) const {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000276 // FIXME: Implement this
277 assert(false && "Cannot instantiate VariableArrayType yet");
278 return QualType();
279}
280
Douglas Gregorcd281c32009-02-28 00:25:32 +0000281QualType
282TemplateTypeInstantiator::
283InstantiateDependentSizedArrayType(const DependentSizedArrayType *T,
284 unsigned Quals) const {
Anders Carlsson76b1c842009-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 =
Douglas Gregor7e063902009-05-11 23:53:27 +0000299 SemaRef.InstantiateExpr(ArraySize, TemplateArgs);
Anders Carlsson76b1c842009-03-15 20:12:13 +0000300 if (InstantiatedArraySize.isInvalid())
301 return QualType();
302
303 return SemaRef.BuildArrayType(ElementType, T->getSizeModifier(),
Anders Carlssone9146f22009-05-01 19:49:17 +0000304 InstantiatedArraySize.takeAs<Expr>(),
Anders Carlsson76b1c842009-03-15 20:12:13 +0000305 T->getIndexTypeQualifier(), Loc, Entity);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000306}
307
Douglas Gregorcd281c32009-02-28 00:25:32 +0000308QualType
309TemplateTypeInstantiator::InstantiateVectorType(const VectorType *T,
310 unsigned Quals) const {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000311 // FIXME: Implement this
312 assert(false && "Cannot instantiate VectorType yet");
313 return QualType();
314}
315
Douglas Gregorcd281c32009-02-28 00:25:32 +0000316QualType
317TemplateTypeInstantiator::InstantiateExtVectorType(const ExtVectorType *T,
318 unsigned Quals) const {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000319 // FIXME: Implement this
320 assert(false && "Cannot instantiate ExtVectorType yet");
321 return QualType();
322}
323
Douglas Gregorcd281c32009-02-28 00:25:32 +0000324QualType
325TemplateTypeInstantiator::
326InstantiateFunctionProtoType(const FunctionProtoType *T,
327 unsigned Quals) const {
Douglas Gregor724651c2009-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 Gregor99ebf652009-02-27 19:31:52 +0000347}
348
Douglas Gregorcd281c32009-02-28 00:25:32 +0000349QualType
350TemplateTypeInstantiator::
351InstantiateFunctionNoProtoType(const FunctionNoProtoType *T,
352 unsigned Quals) const {
Douglas Gregor724651c2009-02-28 01:04:19 +0000353 assert(false && "Functions without prototypes cannot be dependent.");
Douglas Gregor99ebf652009-02-27 19:31:52 +0000354 return QualType();
355}
356
Douglas Gregorcd281c32009-02-28 00:25:32 +0000357QualType
358TemplateTypeInstantiator::InstantiateTypedefType(const TypedefType *T,
359 unsigned Quals) const {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000360 // FIXME: Implement this
361 assert(false && "Cannot instantiate TypedefType yet");
362 return QualType();
363}
364
Douglas Gregorcd281c32009-02-28 00:25:32 +0000365QualType
366TemplateTypeInstantiator::InstantiateTypeOfExprType(const TypeOfExprType *T,
367 unsigned Quals) const {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000368 // FIXME: Implement this
369 assert(false && "Cannot instantiate TypeOfExprType yet");
370 return QualType();
371}
372
Douglas Gregorcd281c32009-02-28 00:25:32 +0000373QualType
374TemplateTypeInstantiator::InstantiateTypeOfType(const TypeOfType *T,
375 unsigned Quals) const {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000376 // FIXME: Implement this
377 assert(false && "Cannot instantiate TypeOfType yet");
378 return QualType();
379}
380
Douglas Gregorcd281c32009-02-28 00:25:32 +0000381QualType
382TemplateTypeInstantiator::InstantiateRecordType(const RecordType *T,
383 unsigned Quals) const {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000384 // FIXME: Implement this
385 assert(false && "Cannot instantiate RecordType yet");
386 return QualType();
387}
388
Douglas Gregorcd281c32009-02-28 00:25:32 +0000389QualType
Douglas Gregorcd281c32009-02-28 00:25:32 +0000390TemplateTypeInstantiator::InstantiateEnumType(const EnumType *T,
391 unsigned Quals) const {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000392 // FIXME: Implement this
393 assert(false && "Cannot instantiate EnumType yet");
394 return QualType();
395}
396
Douglas Gregorcd281c32009-02-28 00:25:32 +0000397QualType
398TemplateTypeInstantiator::
399InstantiateTemplateTypeParmType(const TemplateTypeParmType *T,
400 unsigned Quals) const {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000401 if (T->getDepth() == 0) {
402 // Replace the template type parameter with its corresponding
403 // template argument.
Douglas Gregor99ebf652009-02-27 19:31:52 +0000404 assert(TemplateArgs[T->getIndex()].getKind() == TemplateArgument::Type &&
405 "Template argument kind mismatch");
406 QualType Result = TemplateArgs[T->getIndex()].getAsType();
Douglas Gregorcd281c32009-02-28 00:25:32 +0000407 if (Result.isNull() || !Quals)
Douglas Gregor99ebf652009-02-27 19:31:52 +0000408 return Result;
409
410 // C++ [dcl.ref]p1:
411 // [...] Cv-qualified references are ill-formed except when
412 // the cv-qualifiers are introduced through the use of a
413 // typedef (7.1.3) or of a template type argument (14.3), in
414 // which case the cv-qualifiers are ignored.
Douglas Gregorcd281c32009-02-28 00:25:32 +0000415 if (Quals && Result->isReferenceType())
416 Quals = 0;
Douglas Gregor99ebf652009-02-27 19:31:52 +0000417
Douglas Gregorcd281c32009-02-28 00:25:32 +0000418 return QualType(Result.getTypePtr(), Quals | Result.getCVRQualifiers());
Douglas Gregor99ebf652009-02-27 19:31:52 +0000419 }
420
421 // The template type parameter comes from an inner template (e.g.,
422 // the template parameter list of a member template inside the
423 // template we are instantiating). Create a new template type
424 // parameter with the template "level" reduced by one.
425 return SemaRef.Context.getTemplateTypeParmType(T->getDepth() - 1,
426 T->getIndex(),
Douglas Gregorcd281c32009-02-28 00:25:32 +0000427 T->getName())
428 .getQualifiedType(Quals);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000429}
430
Douglas Gregorcd281c32009-02-28 00:25:32 +0000431QualType
432TemplateTypeInstantiator::
Douglas Gregor7532dc62009-03-30 22:58:21 +0000433InstantiateTemplateSpecializationType(
434 const TemplateSpecializationType *T,
Douglas Gregorcd281c32009-02-28 00:25:32 +0000435 unsigned Quals) const {
Douglas Gregor40808ce2009-03-09 23:48:35 +0000436 llvm::SmallVector<TemplateArgument, 16> InstantiatedTemplateArgs;
437 InstantiatedTemplateArgs.reserve(T->getNumArgs());
Douglas Gregor7532dc62009-03-30 22:58:21 +0000438 for (TemplateSpecializationType::iterator Arg = T->begin(), ArgEnd = T->end();
Douglas Gregor40808ce2009-03-09 23:48:35 +0000439 Arg != ArgEnd; ++Arg) {
440 switch (Arg->getKind()) {
441 case TemplateArgument::Type: {
442 QualType T = SemaRef.InstantiateType(Arg->getAsType(),
Douglas Gregor7e063902009-05-11 23:53:27 +0000443 TemplateArgs,
Douglas Gregor40808ce2009-03-09 23:48:35 +0000444 Arg->getLocation(),
445 DeclarationName());
446 if (T.isNull())
447 return QualType();
448
449 InstantiatedTemplateArgs.push_back(
450 TemplateArgument(Arg->getLocation(), T));
451 break;
452 }
453
454 case TemplateArgument::Declaration:
455 case TemplateArgument::Integral:
456 InstantiatedTemplateArgs.push_back(*Arg);
457 break;
458
459 case TemplateArgument::Expression:
Douglas Gregorba498172009-03-13 21:01:28 +0000460 Sema::OwningExprResult E
Douglas Gregor7e063902009-05-11 23:53:27 +0000461 = SemaRef.InstantiateExpr(Arg->getAsExpr(), TemplateArgs);
Douglas Gregorba498172009-03-13 21:01:28 +0000462 if (E.isInvalid())
463 return QualType();
Anders Carlssonf1b1d592009-05-01 19:30:39 +0000464 InstantiatedTemplateArgs.push_back(E.takeAs<Expr>());
Douglas Gregor40808ce2009-03-09 23:48:35 +0000465 break;
466 }
467 }
468
469 // FIXME: We're missing the locations of the template name, '<', and
470 // '>'.
Douglas Gregorde650ae2009-03-31 18:38:02 +0000471
472 TemplateName Name = SemaRef.InstantiateTemplateName(T->getTemplateName(),
473 Loc,
Douglas Gregor7e063902009-05-11 23:53:27 +0000474 TemplateArgs);
Douglas Gregorde650ae2009-03-31 18:38:02 +0000475
476 return SemaRef.CheckTemplateIdType(Name, Loc, SourceLocation(),
Douglas Gregor7532dc62009-03-30 22:58:21 +0000477 &InstantiatedTemplateArgs[0],
478 InstantiatedTemplateArgs.size(),
479 SourceLocation());
Douglas Gregor99ebf652009-02-27 19:31:52 +0000480}
481
Douglas Gregorcd281c32009-02-28 00:25:32 +0000482QualType
483TemplateTypeInstantiator::
Douglas Gregore4e5b052009-03-19 00:18:19 +0000484InstantiateQualifiedNameType(const QualifiedNameType *T,
485 unsigned Quals) const {
Douglas Gregord57959a2009-03-27 23:10:48 +0000486 // When we instantiated a qualified name type, there's no point in
487 // keeping the qualification around in the instantiated result. So,
488 // just instantiate the named type.
489 return (*this)(T->getNamedType());
490}
491
492QualType
493TemplateTypeInstantiator::
494InstantiateTypenameType(const TypenameType *T, unsigned Quals) const {
Douglas Gregor17343172009-04-01 00:28:59 +0000495 if (const TemplateSpecializationType *TemplateId = T->getTemplateId()) {
496 // When the typename type refers to a template-id, the template-id
497 // is dependent and has enough information to instantiate the
498 // result of the typename type. Since we don't care about keeping
499 // the spelling of the typename type in template instantiations,
500 // we just instantiate the template-id.
501 return InstantiateTemplateSpecializationType(TemplateId, Quals);
502 }
503
Douglas Gregord57959a2009-03-27 23:10:48 +0000504 NestedNameSpecifier *NNS
505 = SemaRef.InstantiateNestedNameSpecifier(T->getQualifier(),
506 SourceRange(Loc),
Douglas Gregor7e063902009-05-11 23:53:27 +0000507 TemplateArgs);
Douglas Gregord57959a2009-03-27 23:10:48 +0000508 if (!NNS)
509 return QualType();
510
Douglas Gregor17343172009-04-01 00:28:59 +0000511 return SemaRef.CheckTypenameType(NNS, *T->getIdentifier(), SourceRange(Loc));
Douglas Gregore4e5b052009-03-19 00:18:19 +0000512}
513
514QualType
515TemplateTypeInstantiator::
Douglas Gregorcd281c32009-02-28 00:25:32 +0000516InstantiateObjCInterfaceType(const ObjCInterfaceType *T,
517 unsigned Quals) const {
518 assert(false && "Objective-C types cannot be dependent");
Douglas Gregor99ebf652009-02-27 19:31:52 +0000519 return QualType();
520}
521
Douglas Gregorcd281c32009-02-28 00:25:32 +0000522QualType
523TemplateTypeInstantiator::
524InstantiateObjCQualifiedInterfaceType(const ObjCQualifiedInterfaceType *T,
525 unsigned Quals) const {
526 assert(false && "Objective-C types cannot be dependent");
Douglas Gregor99ebf652009-02-27 19:31:52 +0000527 return QualType();
528}
529
Douglas Gregorcd281c32009-02-28 00:25:32 +0000530QualType
531TemplateTypeInstantiator::
532InstantiateObjCQualifiedIdType(const ObjCQualifiedIdType *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 +0000538/// \brief The actual implementation of Sema::InstantiateType().
539QualType TemplateTypeInstantiator::Instantiate(QualType T) const {
540 // If T is not a dependent type, there is nothing to do.
541 if (!T->isDependentType())
542 return T;
543
544 switch (T->getTypeClass()) {
545#define TYPE(Class, Base) \
546 case Type::Class: \
547 return Instantiate##Class##Type(cast<Class##Type>(T.getTypePtr()), \
548 T.getCVRQualifiers());
549#define ABSTRACT_TYPE(Class, Base)
550#include "clang/AST/TypeNodes.def"
551 }
552
553 assert(false && "Not all types have been decoded for instantiation");
554 return QualType();
555}
Douglas Gregor99ebf652009-02-27 19:31:52 +0000556
557/// \brief Instantiate the type T with a given set of template arguments.
558///
559/// This routine substitutes the given template arguments into the
560/// type T and produces the instantiated type.
561///
562/// \param T the type into which the template arguments will be
563/// substituted. If this type is not dependent, it will be returned
564/// immediately.
565///
566/// \param TemplateArgs the template arguments that will be
567/// substituted for the top-level template parameters within T.
568///
Douglas Gregor99ebf652009-02-27 19:31:52 +0000569/// \param Loc the location in the source code where this substitution
570/// is being performed. It will typically be the location of the
571/// declarator (if we're instantiating the type of some declaration)
572/// or the location of the type in the source code (if, e.g., we're
573/// instantiating the type of a cast expression).
574///
575/// \param Entity the name of the entity associated with a declaration
576/// being instantiated (if any). May be empty to indicate that there
577/// is no such entity (if, e.g., this is a type that occurs as part of
578/// a cast expression) or that the entity has no name (e.g., an
579/// unnamed function parameter).
580///
581/// \returns If the instantiation succeeds, the instantiated
582/// type. Otherwise, produces diagnostics and returns a NULL type.
583QualType Sema::InstantiateType(QualType T,
Douglas Gregor7e063902009-05-11 23:53:27 +0000584 const TemplateArgumentList &TemplateArgs,
Douglas Gregor99ebf652009-02-27 19:31:52 +0000585 SourceLocation Loc, DeclarationName Entity) {
Douglas Gregordf667e72009-03-10 20:44:00 +0000586 assert(!ActiveTemplateInstantiations.empty() &&
587 "Cannot perform an instantiation without some context on the "
588 "instantiation stack");
589
Douglas Gregor99ebf652009-02-27 19:31:52 +0000590 // If T is not a dependent type, there is nothing to do.
591 if (!T->isDependentType())
592 return T;
593
Douglas Gregor7e063902009-05-11 23:53:27 +0000594 TemplateTypeInstantiator Instantiator(*this, TemplateArgs, Loc, Entity);
Douglas Gregorcd281c32009-02-28 00:25:32 +0000595 return Instantiator(T);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000596}
Douglas Gregor2943aed2009-03-03 04:44:36 +0000597
598/// \brief Instantiate the base class specifiers of the given class
599/// template specialization.
600///
601/// Produces a diagnostic and returns true on error, returns false and
602/// attaches the instantiated base classes to the class template
603/// specialization if successful.
604bool
Douglas Gregord475b8d2009-03-25 21:17:03 +0000605Sema::InstantiateBaseSpecifiers(CXXRecordDecl *Instantiation,
606 CXXRecordDecl *Pattern,
Douglas Gregor7e063902009-05-11 23:53:27 +0000607 const TemplateArgumentList &TemplateArgs) {
Douglas Gregor2943aed2009-03-03 04:44:36 +0000608 bool Invalid = false;
609 llvm::SmallVector<CXXBaseSpecifier*, 8> InstantiatedBases;
Douglas Gregord475b8d2009-03-25 21:17:03 +0000610 for (ClassTemplateSpecializationDecl::base_class_iterator
611 Base = Pattern->bases_begin(), BaseEnd = Pattern->bases_end();
Douglas Gregor27b152f2009-03-10 18:52:44 +0000612 Base != BaseEnd; ++Base) {
Douglas Gregor2943aed2009-03-03 04:44:36 +0000613 if (!Base->getType()->isDependentType()) {
614 // FIXME: Allocate via ASTContext
615 InstantiatedBases.push_back(new CXXBaseSpecifier(*Base));
616 continue;
617 }
618
619 QualType BaseType = InstantiateType(Base->getType(),
Douglas Gregor7e063902009-05-11 23:53:27 +0000620 TemplateArgs,
Douglas Gregor2943aed2009-03-03 04:44:36 +0000621 Base->getSourceRange().getBegin(),
622 DeclarationName());
623 if (BaseType.isNull()) {
624 Invalid = true;
625 continue;
626 }
627
628 if (CXXBaseSpecifier *InstantiatedBase
Douglas Gregord475b8d2009-03-25 21:17:03 +0000629 = CheckBaseSpecifier(Instantiation,
Douglas Gregor2943aed2009-03-03 04:44:36 +0000630 Base->getSourceRange(),
631 Base->isVirtual(),
632 Base->getAccessSpecifierAsWritten(),
633 BaseType,
634 /*FIXME: Not totally accurate */
635 Base->getSourceRange().getBegin()))
636 InstantiatedBases.push_back(InstantiatedBase);
637 else
638 Invalid = true;
639 }
640
Douglas Gregor27b152f2009-03-10 18:52:44 +0000641 if (!Invalid &&
Douglas Gregord475b8d2009-03-25 21:17:03 +0000642 AttachBaseSpecifiers(Instantiation, &InstantiatedBases[0],
Douglas Gregor2943aed2009-03-03 04:44:36 +0000643 InstantiatedBases.size()))
644 Invalid = true;
645
646 return Invalid;
647}
648
Douglas Gregord475b8d2009-03-25 21:17:03 +0000649/// \brief Instantiate the definition of a class from a given pattern.
650///
651/// \param PointOfInstantiation The point of instantiation within the
652/// source code.
653///
654/// \param Instantiation is the declaration whose definition is being
655/// instantiated. This will be either a class template specialization
656/// or a member class of a class template specialization.
657///
658/// \param Pattern is the pattern from which the instantiation
659/// occurs. This will be either the declaration of a class template or
660/// the declaration of a member class of a class template.
661///
662/// \param TemplateArgs The template arguments to be substituted into
663/// the pattern.
664///
Douglas Gregord475b8d2009-03-25 21:17:03 +0000665/// \returns true if an error occurred, false otherwise.
666bool
667Sema::InstantiateClass(SourceLocation PointOfInstantiation,
668 CXXRecordDecl *Instantiation, CXXRecordDecl *Pattern,
Douglas Gregor93dfdb12009-05-13 00:25:59 +0000669 const TemplateArgumentList &TemplateArgs,
670 bool ExplicitInstantiation) {
Douglas Gregord475b8d2009-03-25 21:17:03 +0000671 bool Invalid = false;
672
673 CXXRecordDecl *PatternDef
674 = cast_or_null<CXXRecordDecl>(Pattern->getDefinition(Context));
675 if (!PatternDef) {
676 if (Pattern == Instantiation->getInstantiatedFromMemberClass()) {
677 Diag(PointOfInstantiation,
678 diag::err_implicit_instantiate_member_undefined)
679 << Context.getTypeDeclType(Instantiation);
680 Diag(Pattern->getLocation(), diag::note_member_of_template_here);
681 } else {
Douglas Gregor93dfdb12009-05-13 00:25:59 +0000682 Diag(PointOfInstantiation, diag::err_template_instantiate_undefined)
683 << ExplicitInstantiation
Douglas Gregord475b8d2009-03-25 21:17:03 +0000684 << Context.getTypeDeclType(Instantiation);
685 Diag(Pattern->getLocation(), diag::note_template_decl_here);
686 }
687 return true;
688 }
689 Pattern = PatternDef;
690
Douglas Gregord048bb72009-03-25 21:23:52 +0000691 InstantiatingTemplate Inst(*this, PointOfInstantiation, Instantiation);
Douglas Gregord475b8d2009-03-25 21:17:03 +0000692 if (Inst)
693 return true;
694
695 // Enter the scope of this instantiation. We don't use
696 // PushDeclContext because we don't have a scope.
697 DeclContext *PreviousContext = CurContext;
698 CurContext = Instantiation;
699
700 // Start the definition of this instantiation.
701 Instantiation->startDefinition();
702
703 // Instantiate the base class specifiers.
Douglas Gregor7e063902009-05-11 23:53:27 +0000704 if (InstantiateBaseSpecifiers(Instantiation, Pattern, TemplateArgs))
Douglas Gregord475b8d2009-03-25 21:17:03 +0000705 Invalid = true;
706
Chris Lattnerb28317a2009-03-28 19:18:32 +0000707 llvm::SmallVector<DeclPtrTy, 32> Fields;
Douglas Gregor6ab35242009-04-09 21:40:53 +0000708 for (RecordDecl::decl_iterator Member = Pattern->decls_begin(Context),
709 MemberEnd = Pattern->decls_end(Context);
710 Member != MemberEnd; ++Member) {
Douglas Gregor7e063902009-05-11 23:53:27 +0000711 Decl *NewMember = InstantiateDecl(*Member, Instantiation, TemplateArgs);
Douglas Gregord475b8d2009-03-25 21:17:03 +0000712 if (NewMember) {
713 if (NewMember->isInvalidDecl())
714 Invalid = true;
715 else if (FieldDecl *Field = dyn_cast<FieldDecl>(NewMember))
Chris Lattnerb28317a2009-03-28 19:18:32 +0000716 Fields.push_back(DeclPtrTy::make(Field));
Douglas Gregord475b8d2009-03-25 21:17:03 +0000717 } else {
718 // FIXME: Eventually, a NULL return will mean that one of the
719 // instantiations was a semantic disaster, and we'll want to set
720 // Invalid = true. For now, we expect to skip some members that
721 // we can't yet handle.
722 }
723 }
724
725 // Finish checking fields.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000726 ActOnFields(0, Instantiation->getLocation(), DeclPtrTy::make(Instantiation),
Douglas Gregord475b8d2009-03-25 21:17:03 +0000727 &Fields[0], Fields.size(), SourceLocation(), SourceLocation(),
728 0);
729
730 // Add any implicitly-declared members that we might need.
731 AddImplicitlyDeclaredMembersToClass(Instantiation);
732
733 // Exit the scope of this instantiation.
734 CurContext = PreviousContext;
735
736 return Invalid;
737}
738
Douglas Gregor2943aed2009-03-03 04:44:36 +0000739bool
740Sema::InstantiateClassTemplateSpecialization(
741 ClassTemplateSpecializationDecl *ClassTemplateSpec,
742 bool ExplicitInstantiation) {
743 // Perform the actual instantiation on the canonical declaration.
744 ClassTemplateSpec = cast<ClassTemplateSpecializationDecl>(
745 Context.getCanonicalDecl(ClassTemplateSpec));
746
747 // We can only instantiate something that hasn't already been
748 // instantiated or specialized. Fail without any diagnostics: our
749 // caller will provide an error message.
750 if (ClassTemplateSpec->getSpecializationKind() != TSK_Undeclared)
751 return true;
752
753 // FIXME: Push this class template instantiation onto the
754 // instantiation stack, checking for recursion that exceeds a
755 // certain depth.
756
757 // FIXME: Perform class template partial specialization to select
758 // the best template.
759 ClassTemplateDecl *Template = ClassTemplateSpec->getSpecializedTemplate();
760
Douglas Gregord475b8d2009-03-25 21:17:03 +0000761 CXXRecordDecl *Pattern = Template->getTemplatedDecl();
Douglas Gregor2943aed2009-03-03 04:44:36 +0000762
763 // Note that this is an instantiation.
764 ClassTemplateSpec->setSpecializationKind(
765 ExplicitInstantiation? TSK_ExplicitInstantiation
766 : TSK_ImplicitInstantiation);
767
Douglas Gregord475b8d2009-03-25 21:17:03 +0000768 return InstantiateClass(ClassTemplateSpec->getLocation(),
769 ClassTemplateSpec, Pattern,
Douglas Gregor93dfdb12009-05-13 00:25:59 +0000770 ClassTemplateSpec->getTemplateArgs(),
771 ExplicitInstantiation);
Douglas Gregor2943aed2009-03-03 04:44:36 +0000772}
Douglas Gregor5953d8b2009-03-19 17:26:29 +0000773
Douglas Gregorab452ba2009-03-26 23:50:42 +0000774/// \brief Instantiate a nested-name-specifier.
775NestedNameSpecifier *
776Sema::InstantiateNestedNameSpecifier(NestedNameSpecifier *NNS,
777 SourceRange Range,
Douglas Gregor7e063902009-05-11 23:53:27 +0000778 const TemplateArgumentList &TemplateArgs) {
Douglas Gregorab452ba2009-03-26 23:50:42 +0000779 // Instantiate the prefix of this nested name specifier.
780 NestedNameSpecifier *Prefix = NNS->getPrefix();
781 if (Prefix) {
Douglas Gregor7e063902009-05-11 23:53:27 +0000782 Prefix = InstantiateNestedNameSpecifier(Prefix, Range, TemplateArgs);
Douglas Gregorab452ba2009-03-26 23:50:42 +0000783 if (!Prefix)
784 return 0;
Douglas Gregor5953d8b2009-03-19 17:26:29 +0000785 }
786
Douglas Gregorab452ba2009-03-26 23:50:42 +0000787 switch (NNS->getKind()) {
Douglas Gregor17343172009-04-01 00:28:59 +0000788 case NestedNameSpecifier::Identifier: {
789 assert(Prefix &&
790 "Can't have an identifier nested-name-specifier with no prefix");
791 CXXScopeSpec SS;
792 // FIXME: The source location information is all wrong.
793 SS.setRange(Range);
794 SS.setScopeRep(Prefix);
795 return static_cast<NestedNameSpecifier *>(
796 ActOnCXXNestedNameSpecifier(0, SS,
797 Range.getEnd(),
798 Range.getEnd(),
799 *NNS->getAsIdentifier()));
Douglas Gregorab452ba2009-03-26 23:50:42 +0000800 break;
Douglas Gregor17343172009-04-01 00:28:59 +0000801 }
Douglas Gregorab452ba2009-03-26 23:50:42 +0000802
803 case NestedNameSpecifier::Namespace:
804 case NestedNameSpecifier::Global:
805 return NNS;
806
807 case NestedNameSpecifier::TypeSpecWithTemplate:
808 case NestedNameSpecifier::TypeSpec: {
809 QualType T = QualType(NNS->getAsType(), 0);
810 if (!T->isDependentType())
811 return NNS;
812
Douglas Gregor7e063902009-05-11 23:53:27 +0000813 T = InstantiateType(T, TemplateArgs, Range.getBegin(), DeclarationName());
Douglas Gregorab452ba2009-03-26 23:50:42 +0000814 if (T.isNull())
815 return 0;
816
Douglas Gregord57959a2009-03-27 23:10:48 +0000817 if (T->isRecordType() ||
818 (getLangOptions().CPlusPlus0x && T->isEnumeralType())) {
Douglas Gregor17343172009-04-01 00:28:59 +0000819 assert(T.getCVRQualifiers() == 0 && "Can't get cv-qualifiers here");
Douglas Gregord57959a2009-03-27 23:10:48 +0000820 return NestedNameSpecifier::Create(Context, Prefix,
Douglas Gregorab452ba2009-03-26 23:50:42 +0000821 NNS->getKind() == NestedNameSpecifier::TypeSpecWithTemplate,
Douglas Gregord57959a2009-03-27 23:10:48 +0000822 T.getTypePtr());
823 }
824
825 Diag(Range.getBegin(), diag::err_nested_name_spec_non_tag) << T;
826 return 0;
Douglas Gregorab452ba2009-03-26 23:50:42 +0000827 }
828 }
829
Douglas Gregord57959a2009-03-27 23:10:48 +0000830 // Required to silence a GCC warning
Douglas Gregorab452ba2009-03-26 23:50:42 +0000831 return 0;
Douglas Gregor5953d8b2009-03-19 17:26:29 +0000832}
Douglas Gregorde650ae2009-03-31 18:38:02 +0000833
834TemplateName
835Sema::InstantiateTemplateName(TemplateName Name, SourceLocation Loc,
Douglas Gregor7e063902009-05-11 23:53:27 +0000836 const TemplateArgumentList &TemplateArgs) {
Douglas Gregorde650ae2009-03-31 18:38:02 +0000837 if (TemplateTemplateParmDecl *TTP
838 = dyn_cast_or_null<TemplateTemplateParmDecl>(
839 Name.getAsTemplateDecl())) {
840 assert(TTP->getDepth() == 0 &&
841 "Cannot reduce depth of a template template parameter");
Douglas Gregor9bde7732009-03-31 20:22:05 +0000842 assert(TemplateArgs[TTP->getPosition()].getAsDecl() &&
Douglas Gregorde650ae2009-03-31 18:38:02 +0000843 "Wrong kind of template template argument");
844 ClassTemplateDecl *ClassTemplate
845 = dyn_cast<ClassTemplateDecl>(
846 TemplateArgs[TTP->getPosition()].getAsDecl());
Douglas Gregor9bde7732009-03-31 20:22:05 +0000847 assert(ClassTemplate && "Expected a class template");
Douglas Gregorde650ae2009-03-31 18:38:02 +0000848 if (QualifiedTemplateName *QTN = Name.getAsQualifiedTemplateName()) {
849 NestedNameSpecifier *NNS
850 = InstantiateNestedNameSpecifier(QTN->getQualifier(),
851 /*FIXME=*/SourceRange(Loc),
Douglas Gregor7e063902009-05-11 23:53:27 +0000852 TemplateArgs);
Douglas Gregorde650ae2009-03-31 18:38:02 +0000853 if (NNS)
854 return Context.getQualifiedTemplateName(NNS,
855 QTN->hasTemplateKeyword(),
856 ClassTemplate);
857 }
858
859 return TemplateName(ClassTemplate);
860 } else if (DependentTemplateName *DTN = Name.getAsDependentTemplateName()) {
861 NestedNameSpecifier *NNS
862 = InstantiateNestedNameSpecifier(DTN->getQualifier(),
863 /*FIXME=*/SourceRange(Loc),
Douglas Gregor7e063902009-05-11 23:53:27 +0000864 TemplateArgs);
Douglas Gregorde650ae2009-03-31 18:38:02 +0000865
866 if (!NNS) // FIXME: Not the best recovery strategy.
867 return Name;
868
869 if (NNS->isDependent())
870 return Context.getDependentTemplateName(NNS, DTN->getName());
871
872 // Somewhat redundant with ActOnDependentTemplateName.
873 CXXScopeSpec SS;
874 SS.setRange(SourceRange(Loc));
875 SS.setScopeRep(NNS);
876 TemplateTy Template;
877 TemplateNameKind TNK = isTemplateName(*DTN->getName(), 0, Template, &SS);
878 if (TNK == TNK_Non_template) {
879 Diag(Loc, diag::err_template_kw_refers_to_non_template)
880 << DTN->getName();
881 return Name;
882 } else if (TNK == TNK_Function_template) {
883 Diag(Loc, diag::err_template_kw_refers_to_non_template)
884 << DTN->getName();
885 return Name;
886 }
887
888 return Template.getAsVal<TemplateName>();
889 }
890
891
892
893 // FIXME: Even if we're referring to a Decl that isn't a template
894 // template parameter, we may need to instantiate the outer contexts
895 // of that Decl. However, this won't be needed until we implement
896 // member templates.
897 return Name;
898}