blob: 25553e510fbb369b39ed7c9bbc83c237d4c2846c [file] [log] [blame]
Douglas Gregor99ebf652009-02-27 19:31:52 +00001//===------- SemaTemplateInstantiate.cpp - C++ Template Instantiation ------===/
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//===----------------------------------------------------------------------===/
8//
9// This file implements C++ template instantiation.
10//
11//===----------------------------------------------------------------------===/
12
13#include "Sema.h"
14#include "clang/AST/ASTContext.h"
15#include "clang/AST/Expr.h"
16#include "clang/AST/ExprCXX.h"
17#include "clang/AST/DeclTemplate.h"
Douglas Gregor313a81d2009-03-12 18:36:18 +000018#include "clang/AST/StmtVisitor.h"
Douglas Gregor99ebf652009-02-27 19:31:52 +000019#include "clang/Parse/DeclSpec.h"
Douglas Gregorba498172009-03-13 21:01:28 +000020#include "clang/Lex/Preprocessor.h" // for the identifier table
Douglas Gregor99ebf652009-02-27 19:31:52 +000021#include "clang/Basic/LangOptions.h"
Douglas Gregorcd281c32009-02-28 00:25:32 +000022#include "llvm/Support/Compiler.h"
Douglas Gregor99ebf652009-02-27 19:31:52 +000023
24using namespace clang;
25
Douglas Gregoree1828a2009-03-10 18:03:33 +000026//===----------------------------------------------------------------------===/
27// Template Instantiation Support
28//===----------------------------------------------------------------------===/
29
Douglas Gregor26dce442009-03-10 00:06:19 +000030Sema::InstantiatingTemplate::
31InstantiatingTemplate(Sema &SemaRef, SourceLocation PointOfInstantiation,
32 ClassTemplateSpecializationDecl *Entity,
33 SourceRange InstantiationRange)
34 : SemaRef(SemaRef) {
Douglas Gregordf667e72009-03-10 20:44:00 +000035
36 Invalid = CheckInstantiationDepth(PointOfInstantiation,
37 InstantiationRange);
38 if (!Invalid) {
Douglas Gregor26dce442009-03-10 00:06:19 +000039 ActiveTemplateInstantiation Inst;
Douglas Gregordf667e72009-03-10 20:44:00 +000040 Inst.Kind = ActiveTemplateInstantiation::TemplateInstantiation;
Douglas Gregor26dce442009-03-10 00:06:19 +000041 Inst.PointOfInstantiation = PointOfInstantiation;
Douglas Gregordf667e72009-03-10 20:44:00 +000042 Inst.Entity = reinterpret_cast<uintptr_t>(Entity);
Douglas Gregor313a81d2009-03-12 18:36:18 +000043 Inst.TemplateArgs = 0;
44 Inst.NumTemplateArgs = 0;
Douglas Gregordf667e72009-03-10 20:44:00 +000045 Inst.InstantiationRange = InstantiationRange;
46 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
47 Invalid = false;
48 }
49}
50
51Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef,
52 SourceLocation PointOfInstantiation,
53 TemplateDecl *Template,
54 const TemplateArgument *TemplateArgs,
55 unsigned NumTemplateArgs,
56 SourceRange InstantiationRange)
57 : SemaRef(SemaRef) {
58
59 Invalid = CheckInstantiationDepth(PointOfInstantiation,
60 InstantiationRange);
61 if (!Invalid) {
62 ActiveTemplateInstantiation Inst;
63 Inst.Kind
64 = ActiveTemplateInstantiation::DefaultTemplateArgumentInstantiation;
65 Inst.PointOfInstantiation = PointOfInstantiation;
66 Inst.Entity = reinterpret_cast<uintptr_t>(Template);
67 Inst.TemplateArgs = TemplateArgs;
68 Inst.NumTemplateArgs = NumTemplateArgs;
Douglas Gregor26dce442009-03-10 00:06:19 +000069 Inst.InstantiationRange = InstantiationRange;
70 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
71 Invalid = false;
72 }
73}
74
75Sema::InstantiatingTemplate::~InstantiatingTemplate() {
76 if (!Invalid)
77 SemaRef.ActiveTemplateInstantiations.pop_back();
78}
79
Douglas Gregordf667e72009-03-10 20:44:00 +000080bool Sema::InstantiatingTemplate::CheckInstantiationDepth(
81 SourceLocation PointOfInstantiation,
82 SourceRange InstantiationRange) {
83 if (SemaRef.ActiveTemplateInstantiations.size()
84 <= SemaRef.getLangOptions().InstantiationDepth)
85 return false;
86
87 SemaRef.Diag(PointOfInstantiation,
88 diag::err_template_recursion_depth_exceeded)
89 << SemaRef.getLangOptions().InstantiationDepth
90 << InstantiationRange;
91 SemaRef.Diag(PointOfInstantiation, diag::note_template_recursion_depth)
92 << SemaRef.getLangOptions().InstantiationDepth;
93 return true;
94}
95
Douglas Gregoree1828a2009-03-10 18:03:33 +000096/// \brief Post-diagnostic hook for printing the instantiation stack.
97void Sema::PrintInstantiationStackHook(unsigned, void *Cookie) {
Douglas Gregor27b152f2009-03-10 18:52:44 +000098 Sema &SemaRef = *static_cast<Sema*>(Cookie);
99 SemaRef.PrintInstantiationStack();
100 SemaRef.LastTemplateInstantiationErrorContext
Douglas Gregordf667e72009-03-10 20:44:00 +0000101 = SemaRef.ActiveTemplateInstantiations.back();
Douglas Gregoree1828a2009-03-10 18:03:33 +0000102}
103
104/// \brief Prints the current instantiation stack through a series of
105/// notes.
106void Sema::PrintInstantiationStack() {
107 for (llvm::SmallVector<ActiveTemplateInstantiation, 16>::reverse_iterator
108 Active = ActiveTemplateInstantiations.rbegin(),
109 ActiveEnd = ActiveTemplateInstantiations.rend();
110 Active != ActiveEnd;
111 ++Active) {
Douglas Gregordf667e72009-03-10 20:44:00 +0000112 switch (Active->Kind) {
113 case ActiveTemplateInstantiation::TemplateInstantiation: {
114 ClassTemplateSpecializationDecl *Spec
115 = cast<ClassTemplateSpecializationDecl>((Decl*)Active->Entity);
116 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
117 diag::note_template_class_instantiation_here)
118 << Context.getTypeDeclType(Spec)
119 << Active->InstantiationRange;
120 break;
121 }
122
123 case ActiveTemplateInstantiation::DefaultTemplateArgumentInstantiation: {
124 TemplateDecl *Template = cast<TemplateDecl>((Decl *)Active->Entity);
125 std::string TemplateArgsStr
126 = ClassTemplateSpecializationType::PrintTemplateArgumentList(
127 Active->TemplateArgs,
128 Active->NumTemplateArgs);
129 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
130 diag::note_default_arg_instantiation_here)
131 << (Template->getNameAsString() + TemplateArgsStr)
132 << Active->InstantiationRange;
133 break;
134 }
135 }
Douglas Gregoree1828a2009-03-10 18:03:33 +0000136 }
137}
138
Douglas Gregor99ebf652009-02-27 19:31:52 +0000139//===----------------------------------------------------------------------===/
140// Template Instantiation for Types
141//===----------------------------------------------------------------------===/
Douglas Gregorcd281c32009-02-28 00:25:32 +0000142namespace {
143 class VISIBILITY_HIDDEN TemplateTypeInstantiator {
144 Sema &SemaRef;
145 const TemplateArgument *TemplateArgs;
146 unsigned NumTemplateArgs;
147 SourceLocation Loc;
148 DeclarationName Entity;
Douglas Gregor99ebf652009-02-27 19:31:52 +0000149
Douglas Gregorcd281c32009-02-28 00:25:32 +0000150 public:
151 TemplateTypeInstantiator(Sema &SemaRef,
152 const TemplateArgument *TemplateArgs,
153 unsigned NumTemplateArgs,
154 SourceLocation Loc,
155 DeclarationName Entity)
156 : SemaRef(SemaRef), TemplateArgs(TemplateArgs),
157 NumTemplateArgs(NumTemplateArgs), Loc(Loc), Entity(Entity) { }
158
159 QualType operator()(QualType T) const { return Instantiate(T); }
160
161 QualType Instantiate(QualType T) const;
162
163 // Declare instantiate functions for each type.
164#define TYPE(Class, Base) \
165 QualType Instantiate##Class##Type(const Class##Type *T, \
166 unsigned Quals) const;
167#define ABSTRACT_TYPE(Class, Base)
168#include "clang/AST/TypeNodes.def"
169 };
170}
171
172QualType
173TemplateTypeInstantiator::InstantiateExtQualType(const ExtQualType *T,
174 unsigned Quals) const {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000175 // FIXME: Implement this
176 assert(false && "Cannot instantiate ExtQualType yet");
177 return QualType();
178}
179
Douglas Gregorcd281c32009-02-28 00:25:32 +0000180QualType
181TemplateTypeInstantiator::InstantiateBuiltinType(const BuiltinType *T,
182 unsigned Quals) const {
Douglas Gregor724651c2009-02-28 01:04:19 +0000183 assert(false && "Builtin types are not dependent and cannot be instantiated");
Douglas Gregorcd281c32009-02-28 00:25:32 +0000184 return QualType(T, Quals);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000185}
186
Douglas Gregorcd281c32009-02-28 00:25:32 +0000187QualType
188TemplateTypeInstantiator::
189InstantiateFixedWidthIntType(const FixedWidthIntType *T, unsigned Quals) const {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000190 // FIXME: Implement this
191 assert(false && "Cannot instantiate FixedWidthIntType yet");
192 return QualType();
193}
194
Douglas Gregorcd281c32009-02-28 00:25:32 +0000195QualType
196TemplateTypeInstantiator::InstantiateComplexType(const ComplexType *T,
197 unsigned Quals) const {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000198 // FIXME: Implement this
199 assert(false && "Cannot instantiate ComplexType yet");
200 return QualType();
201}
202
Douglas Gregorcd281c32009-02-28 00:25:32 +0000203QualType
204TemplateTypeInstantiator::InstantiatePointerType(const PointerType *T,
205 unsigned Quals) const {
206 QualType PointeeType = Instantiate(T->getPointeeType());
207 if (PointeeType.isNull())
208 return QualType();
209
210 return SemaRef.BuildPointerType(PointeeType, Quals, Loc, Entity);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000211}
212
Douglas Gregorcd281c32009-02-28 00:25:32 +0000213QualType
214TemplateTypeInstantiator::InstantiateBlockPointerType(const BlockPointerType *T,
215 unsigned Quals) const {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000216 // FIXME: Implement this
217 assert(false && "Cannot instantiate BlockPointerType yet");
218 return QualType();
219}
220
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000221QualType
222TemplateTypeInstantiator::InstantiateLValueReferenceType(
223 const LValueReferenceType *T, unsigned Quals) const {
Douglas Gregorcd281c32009-02-28 00:25:32 +0000224 QualType ReferentType = Instantiate(T->getPointeeType());
225 if (ReferentType.isNull())
226 return QualType();
227
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000228 return SemaRef.BuildReferenceType(ReferentType, true, Quals, Loc, Entity);
229}
230
231QualType
232TemplateTypeInstantiator::InstantiateRValueReferenceType(
233 const RValueReferenceType *T, unsigned Quals) const {
234 QualType ReferentType = Instantiate(T->getPointeeType());
235 if (ReferentType.isNull())
236 return QualType();
237
238 return SemaRef.BuildReferenceType(ReferentType, false, Quals, Loc, Entity);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000239}
240
Douglas Gregorcd281c32009-02-28 00:25:32 +0000241QualType
242TemplateTypeInstantiator::
243InstantiateMemberPointerType(const MemberPointerType *T,
244 unsigned Quals) const {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000245 // FIXME: Implement this
246 assert(false && "Cannot instantiate MemberPointerType yet");
247 return QualType();
248}
249
Douglas Gregorcd281c32009-02-28 00:25:32 +0000250QualType
251TemplateTypeInstantiator::
252InstantiateConstantArrayType(const ConstantArrayType *T,
253 unsigned Quals) const {
254 QualType ElementType = Instantiate(T->getElementType());
255 if (ElementType.isNull())
256 return ElementType;
257
258 // Build a temporary integer literal to specify the size for
259 // BuildArrayType. Since we have already checked the size as part of
260 // creating the dependent array type in the first place, we know
261 // there aren't any errors.
Douglas Gregor8d217212009-03-09 20:07:22 +0000262 // FIXME: Is IntTy big enough? Maybe not, but LongLongTy causes
263 // problems that I have yet to investigate.
264 IntegerLiteral ArraySize(T->getSize(), SemaRef.Context.IntTy, Loc);
Douglas Gregorcd281c32009-02-28 00:25:32 +0000265 return SemaRef.BuildArrayType(ElementType, T->getSizeModifier(),
266 &ArraySize, T->getIndexTypeQualifier(),
267 Loc, Entity);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000268}
269
Douglas Gregorcd281c32009-02-28 00:25:32 +0000270QualType
271TemplateTypeInstantiator::
272InstantiateIncompleteArrayType(const IncompleteArrayType *T,
273 unsigned Quals) const {
274 QualType ElementType = Instantiate(T->getElementType());
275 if (ElementType.isNull())
276 return ElementType;
277
278 return SemaRef.BuildArrayType(ElementType, T->getSizeModifier(),
279 0, T->getIndexTypeQualifier(),
280 Loc, Entity);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000281}
282
Douglas Gregorcd281c32009-02-28 00:25:32 +0000283QualType
284TemplateTypeInstantiator::
285InstantiateVariableArrayType(const VariableArrayType *T,
286 unsigned Quals) const {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000287 // FIXME: Implement this
288 assert(false && "Cannot instantiate VariableArrayType yet");
289 return QualType();
290}
291
Douglas Gregorcd281c32009-02-28 00:25:32 +0000292QualType
293TemplateTypeInstantiator::
294InstantiateDependentSizedArrayType(const DependentSizedArrayType *T,
295 unsigned Quals) const {
Anders Carlsson76b1c842009-03-15 20:12:13 +0000296 Expr *ArraySize = T->getSizeExpr();
297 assert(ArraySize->isValueDependent() &&
298 "dependent sized array types must have value dependent size expr");
299
300 // Instantiate the element type if needed
301 QualType ElementType = T->getElementType();
302 if (ElementType->isDependentType()) {
303 ElementType = Instantiate(ElementType);
304 if (ElementType.isNull())
305 return QualType();
306 }
307
308 // Instantiate the size expression
309 Sema::OwningExprResult InstantiatedArraySize =
310 SemaRef.InstantiateExpr(ArraySize, TemplateArgs, NumTemplateArgs);
311 if (InstantiatedArraySize.isInvalid())
312 return QualType();
313
314 return SemaRef.BuildArrayType(ElementType, T->getSizeModifier(),
315 (Expr *)InstantiatedArraySize.release(),
316 T->getIndexTypeQualifier(), Loc, Entity);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000317}
318
Douglas Gregorcd281c32009-02-28 00:25:32 +0000319QualType
320TemplateTypeInstantiator::InstantiateVectorType(const VectorType *T,
321 unsigned Quals) const {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000322 // FIXME: Implement this
323 assert(false && "Cannot instantiate VectorType yet");
324 return QualType();
325}
326
Douglas Gregorcd281c32009-02-28 00:25:32 +0000327QualType
328TemplateTypeInstantiator::InstantiateExtVectorType(const ExtVectorType *T,
329 unsigned Quals) const {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000330 // FIXME: Implement this
331 assert(false && "Cannot instantiate ExtVectorType yet");
332 return QualType();
333}
334
Douglas Gregorcd281c32009-02-28 00:25:32 +0000335QualType
336TemplateTypeInstantiator::
337InstantiateFunctionProtoType(const FunctionProtoType *T,
338 unsigned Quals) const {
Douglas Gregor724651c2009-02-28 01:04:19 +0000339 QualType ResultType = Instantiate(T->getResultType());
340 if (ResultType.isNull())
341 return ResultType;
342
343 llvm::SmallVector<QualType, 16> ParamTypes;
344 for (FunctionProtoType::arg_type_iterator Param = T->arg_type_begin(),
345 ParamEnd = T->arg_type_end();
346 Param != ParamEnd; ++Param) {
347 QualType P = Instantiate(*Param);
348 if (P.isNull())
349 return P;
350
351 ParamTypes.push_back(P);
352 }
353
354 return SemaRef.BuildFunctionType(ResultType, &ParamTypes[0],
355 ParamTypes.size(),
356 T->isVariadic(), T->getTypeQuals(),
357 Loc, Entity);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000358}
359
Douglas Gregorcd281c32009-02-28 00:25:32 +0000360QualType
361TemplateTypeInstantiator::
362InstantiateFunctionNoProtoType(const FunctionNoProtoType *T,
363 unsigned Quals) const {
Douglas Gregor724651c2009-02-28 01:04:19 +0000364 assert(false && "Functions without prototypes cannot be dependent.");
Douglas Gregor99ebf652009-02-27 19:31:52 +0000365 return QualType();
366}
367
Douglas Gregorcd281c32009-02-28 00:25:32 +0000368QualType
369TemplateTypeInstantiator::InstantiateTypedefType(const TypedefType *T,
370 unsigned Quals) const {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000371 // FIXME: Implement this
372 assert(false && "Cannot instantiate TypedefType yet");
373 return QualType();
374}
375
Douglas Gregorcd281c32009-02-28 00:25:32 +0000376QualType
377TemplateTypeInstantiator::InstantiateTypeOfExprType(const TypeOfExprType *T,
378 unsigned Quals) const {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000379 // FIXME: Implement this
380 assert(false && "Cannot instantiate TypeOfExprType yet");
381 return QualType();
382}
383
Douglas Gregorcd281c32009-02-28 00:25:32 +0000384QualType
385TemplateTypeInstantiator::InstantiateTypeOfType(const TypeOfType *T,
386 unsigned Quals) const {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000387 // FIXME: Implement this
388 assert(false && "Cannot instantiate TypeOfType yet");
389 return QualType();
390}
391
Douglas Gregorcd281c32009-02-28 00:25:32 +0000392QualType
393TemplateTypeInstantiator::InstantiateRecordType(const RecordType *T,
394 unsigned Quals) const {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000395 // FIXME: Implement this
396 assert(false && "Cannot instantiate RecordType yet");
397 return QualType();
398}
399
Douglas Gregorcd281c32009-02-28 00:25:32 +0000400QualType
Douglas Gregorcd281c32009-02-28 00:25:32 +0000401TemplateTypeInstantiator::InstantiateEnumType(const EnumType *T,
402 unsigned Quals) const {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000403 // FIXME: Implement this
404 assert(false && "Cannot instantiate EnumType yet");
405 return QualType();
406}
407
Douglas Gregorcd281c32009-02-28 00:25:32 +0000408QualType
409TemplateTypeInstantiator::
410InstantiateTemplateTypeParmType(const TemplateTypeParmType *T,
411 unsigned Quals) const {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000412 if (T->getDepth() == 0) {
413 // Replace the template type parameter with its corresponding
414 // template argument.
415 assert(T->getIndex() < NumTemplateArgs && "Wrong # of template args");
416 assert(TemplateArgs[T->getIndex()].getKind() == TemplateArgument::Type &&
417 "Template argument kind mismatch");
418 QualType Result = TemplateArgs[T->getIndex()].getAsType();
Douglas Gregorcd281c32009-02-28 00:25:32 +0000419 if (Result.isNull() || !Quals)
Douglas Gregor99ebf652009-02-27 19:31:52 +0000420 return Result;
421
422 // C++ [dcl.ref]p1:
423 // [...] Cv-qualified references are ill-formed except when
424 // the cv-qualifiers are introduced through the use of a
425 // typedef (7.1.3) or of a template type argument (14.3), in
426 // which case the cv-qualifiers are ignored.
Douglas Gregorcd281c32009-02-28 00:25:32 +0000427 if (Quals && Result->isReferenceType())
428 Quals = 0;
Douglas Gregor99ebf652009-02-27 19:31:52 +0000429
Douglas Gregorcd281c32009-02-28 00:25:32 +0000430 return QualType(Result.getTypePtr(), Quals | Result.getCVRQualifiers());
Douglas Gregor99ebf652009-02-27 19:31:52 +0000431 }
432
433 // The template type parameter comes from an inner template (e.g.,
434 // the template parameter list of a member template inside the
435 // template we are instantiating). Create a new template type
436 // parameter with the template "level" reduced by one.
437 return SemaRef.Context.getTemplateTypeParmType(T->getDepth() - 1,
438 T->getIndex(),
Douglas Gregorcd281c32009-02-28 00:25:32 +0000439 T->getName())
440 .getQualifiedType(Quals);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000441}
442
Douglas Gregorcd281c32009-02-28 00:25:32 +0000443QualType
444TemplateTypeInstantiator::
445InstantiateClassTemplateSpecializationType(
446 const ClassTemplateSpecializationType *T,
447 unsigned Quals) const {
Douglas Gregor40808ce2009-03-09 23:48:35 +0000448 llvm::SmallVector<TemplateArgument, 16> InstantiatedTemplateArgs;
449 InstantiatedTemplateArgs.reserve(T->getNumArgs());
450 for (ClassTemplateSpecializationType::iterator Arg = T->begin(),
451 ArgEnd = T->end();
452 Arg != ArgEnd; ++Arg) {
453 switch (Arg->getKind()) {
454 case TemplateArgument::Type: {
455 QualType T = SemaRef.InstantiateType(Arg->getAsType(),
456 TemplateArgs, NumTemplateArgs,
457 Arg->getLocation(),
458 DeclarationName());
459 if (T.isNull())
460 return QualType();
461
462 InstantiatedTemplateArgs.push_back(
463 TemplateArgument(Arg->getLocation(), T));
464 break;
465 }
466
467 case TemplateArgument::Declaration:
468 case TemplateArgument::Integral:
469 InstantiatedTemplateArgs.push_back(*Arg);
470 break;
471
472 case TemplateArgument::Expression:
Douglas Gregorba498172009-03-13 21:01:28 +0000473 Sema::OwningExprResult E
474 = SemaRef.InstantiateExpr(Arg->getAsExpr(), TemplateArgs,
475 NumTemplateArgs);
476 if (E.isInvalid())
477 return QualType();
478 InstantiatedTemplateArgs.push_back((Expr *)E.release());
Douglas Gregor40808ce2009-03-09 23:48:35 +0000479 break;
480 }
481 }
482
483 // FIXME: We're missing the locations of the template name, '<', and
484 // '>'.
485 return SemaRef.CheckClassTemplateId(cast<ClassTemplateDecl>(T->getTemplate()),
486 Loc,
487 SourceLocation(),
488 &InstantiatedTemplateArgs[0],
489 InstantiatedTemplateArgs.size(),
490 SourceLocation());
Douglas Gregor99ebf652009-02-27 19:31:52 +0000491}
492
Douglas Gregorcd281c32009-02-28 00:25:32 +0000493QualType
494TemplateTypeInstantiator::
495InstantiateObjCInterfaceType(const ObjCInterfaceType *T,
496 unsigned Quals) const {
497 assert(false && "Objective-C types cannot be dependent");
Douglas Gregor99ebf652009-02-27 19:31:52 +0000498 return QualType();
499}
500
Douglas Gregorcd281c32009-02-28 00:25:32 +0000501QualType
502TemplateTypeInstantiator::
503InstantiateObjCQualifiedInterfaceType(const ObjCQualifiedInterfaceType *T,
504 unsigned Quals) const {
505 assert(false && "Objective-C types cannot be dependent");
Douglas Gregor99ebf652009-02-27 19:31:52 +0000506 return QualType();
507}
508
Douglas Gregorcd281c32009-02-28 00:25:32 +0000509QualType
510TemplateTypeInstantiator::
511InstantiateObjCQualifiedIdType(const ObjCQualifiedIdType *T,
512 unsigned Quals) const {
513 assert(false && "Objective-C types cannot be dependent");
Douglas Gregor99ebf652009-02-27 19:31:52 +0000514 return QualType();
515}
516
Douglas Gregorcd281c32009-02-28 00:25:32 +0000517QualType
518TemplateTypeInstantiator::
519InstantiateObjCQualifiedClassType(const ObjCQualifiedClassType *T,
520 unsigned Quals) const {
521 assert(false && "Objective-C types cannot be dependent");
Douglas Gregor99ebf652009-02-27 19:31:52 +0000522 return QualType();
523}
524
Douglas Gregorcd281c32009-02-28 00:25:32 +0000525/// \brief The actual implementation of Sema::InstantiateType().
526QualType TemplateTypeInstantiator::Instantiate(QualType T) const {
527 // If T is not a dependent type, there is nothing to do.
528 if (!T->isDependentType())
529 return T;
530
531 switch (T->getTypeClass()) {
532#define TYPE(Class, Base) \
533 case Type::Class: \
534 return Instantiate##Class##Type(cast<Class##Type>(T.getTypePtr()), \
535 T.getCVRQualifiers());
536#define ABSTRACT_TYPE(Class, Base)
537#include "clang/AST/TypeNodes.def"
538 }
539
540 assert(false && "Not all types have been decoded for instantiation");
541 return QualType();
542}
Douglas Gregor99ebf652009-02-27 19:31:52 +0000543
544/// \brief Instantiate the type T with a given set of template arguments.
545///
546/// This routine substitutes the given template arguments into the
547/// type T and produces the instantiated type.
548///
549/// \param T the type into which the template arguments will be
550/// substituted. If this type is not dependent, it will be returned
551/// immediately.
552///
553/// \param TemplateArgs the template arguments that will be
554/// substituted for the top-level template parameters within T.
555///
556/// \param NumTemplateArgs the number of template arguments provided
557/// by TemplateArgs.
558///
559/// \param Loc the location in the source code where this substitution
560/// is being performed. It will typically be the location of the
561/// declarator (if we're instantiating the type of some declaration)
562/// or the location of the type in the source code (if, e.g., we're
563/// instantiating the type of a cast expression).
564///
565/// \param Entity the name of the entity associated with a declaration
566/// being instantiated (if any). May be empty to indicate that there
567/// is no such entity (if, e.g., this is a type that occurs as part of
568/// a cast expression) or that the entity has no name (e.g., an
569/// unnamed function parameter).
570///
571/// \returns If the instantiation succeeds, the instantiated
572/// type. Otherwise, produces diagnostics and returns a NULL type.
573QualType Sema::InstantiateType(QualType T,
574 const TemplateArgument *TemplateArgs,
575 unsigned NumTemplateArgs,
576 SourceLocation Loc, DeclarationName Entity) {
Douglas Gregordf667e72009-03-10 20:44:00 +0000577 assert(!ActiveTemplateInstantiations.empty() &&
578 "Cannot perform an instantiation without some context on the "
579 "instantiation stack");
580
Douglas Gregor99ebf652009-02-27 19:31:52 +0000581 // If T is not a dependent type, there is nothing to do.
582 if (!T->isDependentType())
583 return T;
584
Douglas Gregorcd281c32009-02-28 00:25:32 +0000585 TemplateTypeInstantiator Instantiator(*this, TemplateArgs, NumTemplateArgs,
586 Loc, Entity);
587 return Instantiator(T);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000588}
Douglas Gregor2943aed2009-03-03 04:44:36 +0000589
Douglas Gregora0e500d2009-03-12 16:53:44 +0000590//===----------------------------------------------------------------------===/
591// Template Instantiation for Expressions
592//===----------------------------------------------------------------------===/
Douglas Gregor313a81d2009-03-12 18:36:18 +0000593namespace {
594 class VISIBILITY_HIDDEN TemplateExprInstantiator
595 : public StmtVisitor<TemplateExprInstantiator, Sema::OwningExprResult> {
596 Sema &SemaRef;
597 const TemplateArgument *TemplateArgs;
598 unsigned NumTemplateArgs;
599
600 public:
Douglas Gregorba498172009-03-13 21:01:28 +0000601 typedef Sema::OwningExprResult OwningExprResult;
602
Douglas Gregor313a81d2009-03-12 18:36:18 +0000603 TemplateExprInstantiator(Sema &SemaRef,
604 const TemplateArgument *TemplateArgs,
605 unsigned NumTemplateArgs)
606 : SemaRef(SemaRef), TemplateArgs(TemplateArgs),
607 NumTemplateArgs(NumTemplateArgs) { }
608
609 // FIXME: Once we get closer to completion, replace these
610 // manually-written declarations with automatically-generated ones
611 // from clang/AST/StmtNodes.def.
Douglas Gregorba498172009-03-13 21:01:28 +0000612 OwningExprResult VisitIntegerLiteral(IntegerLiteral *E);
613 OwningExprResult VisitDeclRefExpr(DeclRefExpr *E);
614 OwningExprResult VisitParenExpr(ParenExpr *E);
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000615 OwningExprResult VisitUnaryOperator(UnaryOperator *E);
Douglas Gregorba498172009-03-13 21:01:28 +0000616 OwningExprResult VisitBinaryOperator(BinaryOperator *E);
617 OwningExprResult VisitCXXOperatorCallExpr(CXXOperatorCallExpr *E);
618 OwningExprResult VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E);
619 OwningExprResult VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *E);
Anders Carlsson0cde0a32009-03-17 00:28:02 +0000620 OwningExprResult VisitImplicitCastExpr(ImplicitCastExpr *E);
621
Douglas Gregor313a81d2009-03-12 18:36:18 +0000622 // Base case. I'm supposed to ignore this.
Anders Carlssona135fb42009-03-15 18:34:13 +0000623 Sema::OwningExprResult VisitStmt(Stmt *S) {
624 S->dump();
Douglas Gregordf032512009-03-12 22:46:12 +0000625 assert(false && "Cannot instantiate this kind of expression");
626 return SemaRef.ExprError();
627 }
Douglas Gregor313a81d2009-03-12 18:36:18 +0000628 };
629}
630
631Sema::OwningExprResult
632TemplateExprInstantiator::VisitIntegerLiteral(IntegerLiteral *E) {
Anders Carlssona135fb42009-03-15 18:34:13 +0000633 return SemaRef.Clone(E);
Douglas Gregor313a81d2009-03-12 18:36:18 +0000634}
635
636Sema::OwningExprResult
637TemplateExprInstantiator::VisitDeclRefExpr(DeclRefExpr *E) {
638 Decl *D = E->getDecl();
639 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(D)) {
640 assert(NTTP->getDepth() == 0 && "No nested templates yet");
Douglas Gregorc971f862009-03-12 22:20:26 +0000641 const TemplateArgument &Arg = TemplateArgs[NTTP->getPosition()];
Douglas Gregor1ac02dc2009-03-16 23:35:25 +0000642 QualType T = Arg.getIntegralType();
643 if (T->isCharType() || T->isWideCharType())
644 return SemaRef.Owned(new (SemaRef.Context) CharacterLiteral(
645 Arg.getAsIntegral()->getZExtValue(),
646 T->isWideCharType(),
647 T,
648 E->getSourceRange().getBegin()));
649 else if (T->isBooleanType())
650 return SemaRef.Owned(new (SemaRef.Context) CXXBoolLiteralExpr(
651 Arg.getAsIntegral()->getBoolValue(),
652 T,
653 E->getSourceRange().getBegin()));
654
Douglas Gregor313a81d2009-03-12 18:36:18 +0000655 return SemaRef.Owned(new (SemaRef.Context) IntegerLiteral(
Douglas Gregorc971f862009-03-12 22:20:26 +0000656 *Arg.getAsIntegral(),
Douglas Gregor1ac02dc2009-03-16 23:35:25 +0000657 T,
Douglas Gregorc971f862009-03-12 22:20:26 +0000658 E->getSourceRange().getBegin()));
Douglas Gregor313a81d2009-03-12 18:36:18 +0000659 } else
660 assert(false && "Can't handle arbitrary declaration references");
661
662 return SemaRef.ExprError();
663}
664
665Sema::OwningExprResult
666TemplateExprInstantiator::VisitParenExpr(ParenExpr *E) {
667 Sema::OwningExprResult SubExpr
668 = SemaRef.InstantiateExpr(E->getSubExpr(), TemplateArgs, NumTemplateArgs);
669 if (SubExpr.isInvalid())
670 return SemaRef.ExprError();
671
672 return SemaRef.Owned(new (SemaRef.Context) ParenExpr(
673 E->getLParen(), E->getRParen(),
674 (Expr *)SubExpr.release()));
675}
676
Douglas Gregora0e500d2009-03-12 16:53:44 +0000677Sema::OwningExprResult
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000678TemplateExprInstantiator::VisitUnaryOperator(UnaryOperator *E) {
679 Sema::OwningExprResult Arg = Visit(E->getSubExpr());
680 if (Arg.isInvalid())
681 return SemaRef.ExprError();
682
683 return SemaRef.CreateBuiltinUnaryOp(E->getOperatorLoc(),
684 E->getOpcode(),
685 move(Arg));
686}
687
688Sema::OwningExprResult
Douglas Gregordf032512009-03-12 22:46:12 +0000689TemplateExprInstantiator::VisitBinaryOperator(BinaryOperator *E) {
690 Sema::OwningExprResult LHS = Visit(E->getLHS());
691 if (LHS.isInvalid())
692 return SemaRef.ExprError();
693
694 Sema::OwningExprResult RHS = Visit(E->getRHS());
695 if (RHS.isInvalid())
696 return SemaRef.ExprError();
697
698 Sema::OwningExprResult Result
699 = SemaRef.CreateBuiltinBinOp(E->getOperatorLoc(),
700 E->getOpcode(),
701 (Expr *)LHS.get(),
702 (Expr *)RHS.get());
703 if (Result.isInvalid())
704 return SemaRef.ExprError();
705
706 LHS.release();
707 RHS.release();
708 return move(Result);
709}
710
711Sema::OwningExprResult
Douglas Gregor3fd95ce2009-03-13 00:33:25 +0000712TemplateExprInstantiator::VisitCXXOperatorCallExpr(CXXOperatorCallExpr *E) {
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000713 Sema::OwningExprResult First = Visit(E->getArg(0));
714 if (First.isInvalid())
Douglas Gregor3fd95ce2009-03-13 00:33:25 +0000715 return SemaRef.ExprError();
716
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000717 Expr *Args[2] = { (Expr *)First.get(), 0 };
Douglas Gregor3fd95ce2009-03-13 00:33:25 +0000718
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000719 Sema::OwningExprResult Second(SemaRef);
720 if (E->getNumArgs() == 2) {
721 Second = Visit(E->getArg(1));
722
723 if (Second.isInvalid())
724 return SemaRef.ExprError();
725
726 Args[1] = (Expr *)Second.get();
727 }
Douglas Gregor063daf62009-03-13 18:40:31 +0000728
729 if (!E->isTypeDependent()) {
730 // Since our original expression was not type-dependent, we do not
731 // perform lookup again at instantiation time (C++ [temp.dep]p1).
732 // Instead, we just build the new overloaded operator call
733 // expression.
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000734 First.release();
735 Second.release();
Douglas Gregor063daf62009-03-13 18:40:31 +0000736 return SemaRef.Owned(new (SemaRef.Context) CXXOperatorCallExpr(
737 SemaRef.Context,
738 E->getOperator(),
739 E->getCallee(),
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000740 Args, E->getNumArgs(),
741 E->getType(),
Douglas Gregor063daf62009-03-13 18:40:31 +0000742 E->getOperatorLoc()));
743 }
744
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000745 bool isPostIncDec = E->getNumArgs() == 2 &&
746 (E->getOperator() == OO_PlusPlus || E->getOperator() == OO_MinusMinus);
747 if (E->getNumArgs() == 1 || isPostIncDec) {
748 if (!Args[0]->getType()->isOverloadableType()) {
749 // The argument is not of overloadable type, so try to create a
750 // built-in unary operation.
751 UnaryOperator::Opcode Opc
752 = UnaryOperator::getOverloadedOpcode(E->getOperator(), isPostIncDec);
753
754 return SemaRef.CreateBuiltinUnaryOp(E->getOperatorLoc(), Opc,
755 move(First));
756 }
757
758 // Fall through to perform overload resolution
759 } else {
760 assert(E->getNumArgs() == 2 && "Expected binary operation");
761
762 Sema::OwningExprResult Result(SemaRef);
763 if (!Args[0]->getType()->isOverloadableType() &&
764 !Args[1]->getType()->isOverloadableType()) {
765 // Neither of the arguments is an overloadable type, so try to
766 // create a built-in binary operation.
767 BinaryOperator::Opcode Opc =
768 BinaryOperator::getOverloadedOpcode(E->getOperator());
769 Result = SemaRef.CreateBuiltinBinOp(E->getOperatorLoc(), Opc,
770 Args[0], Args[1]);
771 if (Result.isInvalid())
772 return SemaRef.ExprError();
773
774 First.release();
775 Second.release();
776 return move(Result);
777 }
778
779 // Fall through to perform overload resolution.
780 }
781
782 // Compute the set of functions that were found at template
783 // definition time.
784 Sema::FunctionSet Functions;
785 DeclRefExpr *DRE = cast<DeclRefExpr>(E->getCallee());
786 OverloadedFunctionDecl *Overloads
787 = cast<OverloadedFunctionDecl>(DRE->getDecl());
788
789 // FIXME: Do we have to check
790 // IsAcceptableNonMemberOperatorCandidate for each of these?
791 for (OverloadedFunctionDecl::function_iterator
792 F = Overloads->function_begin(),
793 FEnd = Overloads->function_end();
794 F != FEnd; ++F)
795 Functions.insert(*F);
796
797 // Add any functions found via argument-dependent lookup.
798 DeclarationName OpName
799 = SemaRef.Context.DeclarationNames.getCXXOperatorName(E->getOperator());
800 SemaRef.ArgumentDependentLookup(OpName, Args, E->getNumArgs(), Functions);
801
802 // Create the overloaded operator invocation.
803 if (E->getNumArgs() == 1 || isPostIncDec) {
804 UnaryOperator::Opcode Opc
805 = UnaryOperator::getOverloadedOpcode(E->getOperator(), isPostIncDec);
806 return SemaRef.CreateOverloadedUnaryOp(E->getOperatorLoc(), Opc,
807 Functions, move(First));
808 }
809
810 // FIXME: This would be far less ugly if CreateOverloadedBinOp took
811 // in ExprArg arguments!
Douglas Gregor063daf62009-03-13 18:40:31 +0000812 BinaryOperator::Opcode Opc =
813 BinaryOperator::getOverloadedOpcode(E->getOperator());
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000814 OwningExprResult Result
815 = SemaRef.CreateOverloadedBinOp(E->getOperatorLoc(), Opc,
816 Functions, Args[0], Args[1]);
Douglas Gregor063daf62009-03-13 18:40:31 +0000817
Douglas Gregor3fd95ce2009-03-13 00:33:25 +0000818 if (Result.isInvalid())
819 return SemaRef.ExprError();
820
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000821 First.release();
822 Second.release();
Douglas Gregor3fd95ce2009-03-13 00:33:25 +0000823 return move(Result);
824}
825
826Sema::OwningExprResult
Douglas Gregorba498172009-03-13 21:01:28 +0000827TemplateExprInstantiator::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E) {
828 bool isSizeOf = E->isSizeOf();
829
830 if (E->isArgumentType()) {
831 QualType T = E->getArgumentType();
832 if (T->isDependentType()) {
833 T = SemaRef.InstantiateType(T, TemplateArgs, NumTemplateArgs,
834 /*FIXME*/E->getOperatorLoc(),
835 &SemaRef.PP.getIdentifierTable().get("sizeof"));
836 if (T.isNull())
837 return SemaRef.ExprError();
838 }
839
840 return SemaRef.CreateSizeOfAlignOfExpr(T, E->getOperatorLoc(), isSizeOf,
841 E->getSourceRange());
842 }
843
844 Sema::OwningExprResult Arg = Visit(E->getArgumentExpr());
845 if (Arg.isInvalid())
846 return SemaRef.ExprError();
847
848 Sema::OwningExprResult Result
849 = SemaRef.CreateSizeOfAlignOfExpr((Expr *)Arg.get(), E->getOperatorLoc(),
850 isSizeOf, E->getSourceRange());
851 if (Result.isInvalid())
852 return SemaRef.ExprError();
853
854 Arg.release();
855 return move(Result);
856}
857
858Sema::OwningExprResult
859TemplateExprInstantiator::VisitCXXTemporaryObjectExpr(
860 CXXTemporaryObjectExpr *E) {
861 QualType T = E->getType();
862 if (T->isDependentType()) {
863 T = SemaRef.InstantiateType(T, TemplateArgs, NumTemplateArgs,
864 E->getTypeBeginLoc(), DeclarationName());
865 if (T.isNull())
866 return SemaRef.ExprError();
867 }
868
869 llvm::SmallVector<Expr *, 16> Args;
870 Args.reserve(E->getNumArgs());
871 bool Invalid = false;
872 for (CXXTemporaryObjectExpr::arg_iterator Arg = E->arg_begin(),
873 ArgEnd = E->arg_end();
874 Arg != ArgEnd; ++Arg) {
875 OwningExprResult InstantiatedArg = Visit(*Arg);
876 if (InstantiatedArg.isInvalid()) {
877 Invalid = true;
878 break;
879 }
880
881 Args.push_back((Expr *)InstantiatedArg.release());
882 }
883
884 if (!Invalid) {
885 SourceLocation CommaLoc;
886 // FIXME: HACK!
887 if (Args.size() > 1)
888 CommaLoc
889 = SemaRef.PP.getLocForEndOfToken(Args[0]->getSourceRange().getEnd());
Sebastian Redlf53597f2009-03-15 17:47:39 +0000890 Sema::OwningExprResult Result(
891 SemaRef.ActOnCXXTypeConstructExpr(SourceRange(E->getTypeBeginLoc()
892 /*, FIXME*/),
893 T.getAsOpaquePtr(),
894 /*FIXME*/E->getTypeBeginLoc(),
895 Sema::MultiExprArg(SemaRef,
896 (void**)&Args[0],
897 Args.size()),
898 /*HACK*/&CommaLoc,
899 E->getSourceRange().getEnd()));
900 // At this point, Args no longer owns the arguments, no matter what.
901 return move(Result);
Douglas Gregorba498172009-03-13 21:01:28 +0000902 }
903
904 // Clean up the instantiated arguments.
905 // FIXME: Would rather do this with RAII.
906 for (unsigned Idx = 0; Idx < Args.size(); ++Idx)
907 SemaRef.DeleteExpr(Args[Idx]);
908
909 return SemaRef.ExprError();
910}
911
Anders Carlsson0cde0a32009-03-17 00:28:02 +0000912Sema::OwningExprResult TemplateExprInstantiator::VisitImplicitCastExpr(
913 ImplicitCastExpr *E) {
914 assert(!E->isTypeDependent() && "Implicit casts must have known types");
915
916 Sema::OwningExprResult SubExpr = Visit(E->getSubExpr());
917 if (SubExpr.isInvalid())
918 return SemaRef.ExprError();
919
920 ImplicitCastExpr *ICE =
921 new (SemaRef.Context) ImplicitCastExpr(E->getType(),
922 (Expr *)SubExpr.release(),
923 E->isLvalueCast());
924 return SemaRef.Owned(ICE);
925}
926
Douglas Gregorba498172009-03-13 21:01:28 +0000927Sema::OwningExprResult
Douglas Gregora0e500d2009-03-12 16:53:44 +0000928Sema::InstantiateExpr(Expr *E, const TemplateArgument *TemplateArgs,
929 unsigned NumTemplateArgs) {
Douglas Gregor313a81d2009-03-12 18:36:18 +0000930 TemplateExprInstantiator Instantiator(*this, TemplateArgs, NumTemplateArgs);
931 return Instantiator.Visit(E);
Douglas Gregora0e500d2009-03-12 16:53:44 +0000932}
933
Douglas Gregor2943aed2009-03-03 04:44:36 +0000934/// \brief Instantiate the base class specifiers of the given class
935/// template specialization.
936///
937/// Produces a diagnostic and returns true on error, returns false and
938/// attaches the instantiated base classes to the class template
939/// specialization if successful.
940bool
941Sema::InstantiateBaseSpecifiers(
942 ClassTemplateSpecializationDecl *ClassTemplateSpec,
943 ClassTemplateDecl *ClassTemplate) {
944 bool Invalid = false;
945 llvm::SmallVector<CXXBaseSpecifier*, 8> InstantiatedBases;
946 for (ClassTemplateSpecializationDecl::base_class_iterator
947 Base = ClassTemplate->getTemplatedDecl()->bases_begin(),
948 BaseEnd = ClassTemplate->getTemplatedDecl()->bases_end();
Douglas Gregor27b152f2009-03-10 18:52:44 +0000949 Base != BaseEnd; ++Base) {
Douglas Gregor2943aed2009-03-03 04:44:36 +0000950 if (!Base->getType()->isDependentType()) {
951 // FIXME: Allocate via ASTContext
952 InstantiatedBases.push_back(new CXXBaseSpecifier(*Base));
953 continue;
954 }
955
956 QualType BaseType = InstantiateType(Base->getType(),
957 ClassTemplateSpec->getTemplateArgs(),
958 ClassTemplateSpec->getNumTemplateArgs(),
959 Base->getSourceRange().getBegin(),
960 DeclarationName());
961 if (BaseType.isNull()) {
962 Invalid = true;
963 continue;
964 }
965
966 if (CXXBaseSpecifier *InstantiatedBase
967 = CheckBaseSpecifier(ClassTemplateSpec,
968 Base->getSourceRange(),
969 Base->isVirtual(),
970 Base->getAccessSpecifierAsWritten(),
971 BaseType,
972 /*FIXME: Not totally accurate */
973 Base->getSourceRange().getBegin()))
974 InstantiatedBases.push_back(InstantiatedBase);
975 else
976 Invalid = true;
977 }
978
Douglas Gregor27b152f2009-03-10 18:52:44 +0000979 if (!Invalid &&
980 AttachBaseSpecifiers(ClassTemplateSpec, &InstantiatedBases[0],
Douglas Gregor2943aed2009-03-03 04:44:36 +0000981 InstantiatedBases.size()))
982 Invalid = true;
983
984 return Invalid;
985}
986
987bool
988Sema::InstantiateClassTemplateSpecialization(
989 ClassTemplateSpecializationDecl *ClassTemplateSpec,
990 bool ExplicitInstantiation) {
991 // Perform the actual instantiation on the canonical declaration.
992 ClassTemplateSpec = cast<ClassTemplateSpecializationDecl>(
993 Context.getCanonicalDecl(ClassTemplateSpec));
994
995 // We can only instantiate something that hasn't already been
996 // instantiated or specialized. Fail without any diagnostics: our
997 // caller will provide an error message.
998 if (ClassTemplateSpec->getSpecializationKind() != TSK_Undeclared)
999 return true;
1000
1001 // FIXME: Push this class template instantiation onto the
1002 // instantiation stack, checking for recursion that exceeds a
1003 // certain depth.
1004
1005 // FIXME: Perform class template partial specialization to select
1006 // the best template.
1007 ClassTemplateDecl *Template = ClassTemplateSpec->getSpecializedTemplate();
1008
1009 if (!Template->getTemplatedDecl()->getDefinition(Context)) {
1010 Diag(ClassTemplateSpec->getLocation(),
1011 diag::err_template_implicit_instantiate_undefined)
1012 << Context.getTypeDeclType(ClassTemplateSpec);
1013 Diag(Template->getTemplatedDecl()->getLocation(),
1014 diag::note_template_decl_here);
1015 return true;
1016 }
1017
1018 // Note that this is an instantiation.
1019 ClassTemplateSpec->setSpecializationKind(
1020 ExplicitInstantiation? TSK_ExplicitInstantiation
1021 : TSK_ImplicitInstantiation);
1022
1023
1024 bool Invalid = false;
1025
Douglas Gregor26dce442009-03-10 00:06:19 +00001026 InstantiatingTemplate Inst(*this, ClassTemplateSpec->getLocation(),
1027 ClassTemplateSpec);
1028 if (Inst)
1029 return true;
1030
Douglas Gregor2943aed2009-03-03 04:44:36 +00001031 // Enter the scope of this instantiation. We don't use
1032 // PushDeclContext because we don't have a scope.
1033 DeclContext *PreviousContext = CurContext;
1034 CurContext = ClassTemplateSpec;
1035
1036 // Start the definition of this instantiation.
1037 ClassTemplateSpec->startDefinition();
1038
Douglas Gregor2943aed2009-03-03 04:44:36 +00001039
1040 // Instantiate the base class specifiers.
1041 if (InstantiateBaseSpecifiers(ClassTemplateSpec, Template))
1042 Invalid = true;
1043
Douglas Gregor4fdf1fa2009-03-11 16:48:53 +00001044 // FIXME: Create the injected-class-name for the
1045 // instantiation. Should this be a typedef or something like it?
1046
1047 RecordDecl *Pattern = Template->getTemplatedDecl();
Douglas Gregor3cf538d2009-03-11 18:59:21 +00001048 llvm::SmallVector<DeclTy *, 32> Fields;
Douglas Gregor4fdf1fa2009-03-11 16:48:53 +00001049 for (RecordDecl::decl_iterator Member = Pattern->decls_begin(),
1050 MemberEnd = Pattern->decls_end();
1051 Member != MemberEnd; ++Member) {
1052 if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(*Member)) {
1053 // FIXME: Simplified instantiation of typedefs needs to be made
1054 // "real".
1055 QualType T = Typedef->getUnderlyingType();
1056 if (T->isDependentType()) {
1057 T = InstantiateType(T, ClassTemplateSpec->getTemplateArgs(),
1058 ClassTemplateSpec->getNumTemplateArgs(),
1059 Typedef->getLocation(),
1060 Typedef->getDeclName());
1061 if (T.isNull()) {
1062 Invalid = true;
1063 T = Context.IntTy;
1064 }
1065 }
1066
1067 // Create the new typedef
1068 TypedefDecl *New
1069 = TypedefDecl::Create(Context, ClassTemplateSpec,
1070 Typedef->getLocation(),
1071 Typedef->getIdentifier(),
1072 T);
1073 ClassTemplateSpec->addDecl(New);
Douglas Gregor3cf538d2009-03-11 18:59:21 +00001074 }
1075 else if (FieldDecl *Field = dyn_cast<FieldDecl>(*Member)) {
1076 // FIXME: Simplified instantiation of fields needs to be made
1077 // "real".
Douglas Gregora0e500d2009-03-12 16:53:44 +00001078 bool InvalidDecl = false;
Douglas Gregor3cf538d2009-03-11 18:59:21 +00001079 QualType T = Field->getType();
1080 if (T->isDependentType()) {
1081 T = InstantiateType(T, ClassTemplateSpec->getTemplateArgs(),
1082 ClassTemplateSpec->getNumTemplateArgs(),
1083 Field->getLocation(),
1084 Field->getDeclName());
1085 if (!T.isNull() && T->isFunctionType()) {
1086 // C++ [temp.arg.type]p3:
1087 // If a declaration acquires a function type through a type
1088 // dependent on a template-parameter and this causes a
1089 // declaration that does not use the syntactic form of a
1090 // function declarator to have function type, the program is
1091 // ill-formed.
1092 Diag(Field->getLocation(), diag::err_field_instantiates_to_function)
1093 << T;
1094 T = QualType();
Douglas Gregora0e500d2009-03-12 16:53:44 +00001095 InvalidDecl = true;
Douglas Gregor3cf538d2009-03-11 18:59:21 +00001096 }
1097 }
1098
Douglas Gregora0e500d2009-03-12 16:53:44 +00001099 Expr *BitWidth = Field->getBitWidth();
1100 if (InvalidDecl)
1101 BitWidth = 0;
Douglas Gregor3e287c22009-03-15 17:43:26 +00001102 else if (BitWidth) {
Douglas Gregora0e500d2009-03-12 16:53:44 +00001103 OwningExprResult InstantiatedBitWidth
1104 = InstantiateExpr(BitWidth,
1105 ClassTemplateSpec->getTemplateArgs(),
1106 ClassTemplateSpec->getNumTemplateArgs());
1107 if (InstantiatedBitWidth.isInvalid()) {
1108 Invalid = InvalidDecl = true;
1109 BitWidth = 0;
1110 } else
1111 BitWidth = (Expr *)InstantiatedBitWidth.release();
1112 }
1113
Douglas Gregor3cf538d2009-03-11 18:59:21 +00001114 FieldDecl *New = CheckFieldDecl(Field->getDeclName(), T,
1115 ClassTemplateSpec,
1116 Field->getLocation(),
1117 Field->isMutable(),
Douglas Gregora0e500d2009-03-12 16:53:44 +00001118 BitWidth,
Douglas Gregor4dd55f52009-03-11 20:50:30 +00001119 Field->getAccess(),
Douglas Gregor3cf538d2009-03-11 18:59:21 +00001120 0);
1121 if (New) {
1122 ClassTemplateSpec->addDecl(New);
1123 Fields.push_back(New);
1124
Douglas Gregora0e500d2009-03-12 16:53:44 +00001125 if (InvalidDecl)
1126 New->setInvalidDecl();
1127
Douglas Gregor3cf538d2009-03-11 18:59:21 +00001128 if (New->isInvalidDecl())
1129 Invalid = true;
1130 }
Anders Carlsson94b15fb2009-03-15 18:44:04 +00001131 } else if (StaticAssertDecl *SA = dyn_cast<StaticAssertDecl>(*Member)) {
1132 Expr *AssertExpr = SA->getAssertExpr();
1133
1134 OwningExprResult InstantiatedAssertExpr
1135 = InstantiateExpr(AssertExpr,
1136 ClassTemplateSpec->getTemplateArgs(),
1137 ClassTemplateSpec->getNumTemplateArgs());
1138 if (!InstantiatedAssertExpr.isInvalid()) {
1139 OwningExprResult Message = Clone(SA->getMessage());
1140
1141 Decl *New =
1142 (Decl *)ActOnStaticAssertDeclaration(SA->getLocation(),
1143 move(InstantiatedAssertExpr),
1144 move(Message));
1145 if (New->isInvalidDecl())
1146 Invalid = true;
1147
1148 } else
1149 Invalid = true;
Douglas Gregor4fdf1fa2009-03-11 16:48:53 +00001150 }
1151 }
1152
Douglas Gregor3cf538d2009-03-11 18:59:21 +00001153 // Finish checking fields.
1154 ActOnFields(0, ClassTemplateSpec->getLocation(), ClassTemplateSpec,
1155 &Fields[0], Fields.size(), SourceLocation(), SourceLocation(),
1156 0);
1157
Douglas Gregor2943aed2009-03-03 04:44:36 +00001158 // Add any implicitly-declared members that we might need.
1159 AddImplicitlyDeclaredMembersToClass(ClassTemplateSpec);
1160
Douglas Gregor2943aed2009-03-03 04:44:36 +00001161 // Exit the scope of this instantiation.
1162 CurContext = PreviousContext;
1163
1164 return Invalid;
1165}