blob: 4c3f100d74d0d4fc7f3eee2337700a6122791f7a [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
Douglas Gregorcd281c32009-02-28 00:25:32 +0000221QualType
222TemplateTypeInstantiator::InstantiateReferenceType(const ReferenceType *T,
223 unsigned Quals) const {
224 QualType ReferentType = Instantiate(T->getPointeeType());
225 if (ReferentType.isNull())
226 return QualType();
227
228 return SemaRef.BuildReferenceType(ReferentType, Quals, Loc, Entity);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000229}
230
Douglas Gregorcd281c32009-02-28 00:25:32 +0000231QualType
232TemplateTypeInstantiator::
233InstantiateMemberPointerType(const MemberPointerType *T,
234 unsigned Quals) const {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000235 // FIXME: Implement this
236 assert(false && "Cannot instantiate MemberPointerType yet");
237 return QualType();
238}
239
Douglas Gregorcd281c32009-02-28 00:25:32 +0000240QualType
241TemplateTypeInstantiator::
242InstantiateConstantArrayType(const ConstantArrayType *T,
243 unsigned Quals) const {
244 QualType ElementType = Instantiate(T->getElementType());
245 if (ElementType.isNull())
246 return ElementType;
247
248 // Build a temporary integer literal to specify the size for
249 // BuildArrayType. Since we have already checked the size as part of
250 // creating the dependent array type in the first place, we know
251 // there aren't any errors.
Douglas Gregor8d217212009-03-09 20:07:22 +0000252 // FIXME: Is IntTy big enough? Maybe not, but LongLongTy causes
253 // problems that I have yet to investigate.
254 IntegerLiteral ArraySize(T->getSize(), SemaRef.Context.IntTy, Loc);
Douglas Gregorcd281c32009-02-28 00:25:32 +0000255 return SemaRef.BuildArrayType(ElementType, T->getSizeModifier(),
256 &ArraySize, T->getIndexTypeQualifier(),
257 Loc, Entity);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000258}
259
Douglas Gregorcd281c32009-02-28 00:25:32 +0000260QualType
261TemplateTypeInstantiator::
262InstantiateIncompleteArrayType(const IncompleteArrayType *T,
263 unsigned Quals) const {
264 QualType ElementType = Instantiate(T->getElementType());
265 if (ElementType.isNull())
266 return ElementType;
267
268 return SemaRef.BuildArrayType(ElementType, T->getSizeModifier(),
269 0, T->getIndexTypeQualifier(),
270 Loc, Entity);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000271}
272
Douglas Gregorcd281c32009-02-28 00:25:32 +0000273QualType
274TemplateTypeInstantiator::
275InstantiateVariableArrayType(const VariableArrayType *T,
276 unsigned Quals) const {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000277 // FIXME: Implement this
278 assert(false && "Cannot instantiate VariableArrayType yet");
279 return QualType();
280}
281
Douglas Gregorcd281c32009-02-28 00:25:32 +0000282QualType
283TemplateTypeInstantiator::
284InstantiateDependentSizedArrayType(const DependentSizedArrayType *T,
285 unsigned Quals) const {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000286 // FIXME: Implement this
287 assert(false && "Cannot instantiate DependentSizedArrayType yet");
288 return QualType();
289}
290
Douglas Gregorcd281c32009-02-28 00:25:32 +0000291QualType
292TemplateTypeInstantiator::InstantiateVectorType(const VectorType *T,
293 unsigned Quals) const {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000294 // FIXME: Implement this
295 assert(false && "Cannot instantiate VectorType yet");
296 return QualType();
297}
298
Douglas Gregorcd281c32009-02-28 00:25:32 +0000299QualType
300TemplateTypeInstantiator::InstantiateExtVectorType(const ExtVectorType *T,
301 unsigned Quals) const {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000302 // FIXME: Implement this
303 assert(false && "Cannot instantiate ExtVectorType yet");
304 return QualType();
305}
306
Douglas Gregorcd281c32009-02-28 00:25:32 +0000307QualType
308TemplateTypeInstantiator::
309InstantiateFunctionProtoType(const FunctionProtoType *T,
310 unsigned Quals) const {
Douglas Gregor724651c2009-02-28 01:04:19 +0000311 QualType ResultType = Instantiate(T->getResultType());
312 if (ResultType.isNull())
313 return ResultType;
314
315 llvm::SmallVector<QualType, 16> ParamTypes;
316 for (FunctionProtoType::arg_type_iterator Param = T->arg_type_begin(),
317 ParamEnd = T->arg_type_end();
318 Param != ParamEnd; ++Param) {
319 QualType P = Instantiate(*Param);
320 if (P.isNull())
321 return P;
322
323 ParamTypes.push_back(P);
324 }
325
326 return SemaRef.BuildFunctionType(ResultType, &ParamTypes[0],
327 ParamTypes.size(),
328 T->isVariadic(), T->getTypeQuals(),
329 Loc, Entity);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000330}
331
Douglas Gregorcd281c32009-02-28 00:25:32 +0000332QualType
333TemplateTypeInstantiator::
334InstantiateFunctionNoProtoType(const FunctionNoProtoType *T,
335 unsigned Quals) const {
Douglas Gregor724651c2009-02-28 01:04:19 +0000336 assert(false && "Functions without prototypes cannot be dependent.");
Douglas Gregor99ebf652009-02-27 19:31:52 +0000337 return QualType();
338}
339
Douglas Gregorcd281c32009-02-28 00:25:32 +0000340QualType
341TemplateTypeInstantiator::InstantiateTypedefType(const TypedefType *T,
342 unsigned Quals) const {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000343 // FIXME: Implement this
344 assert(false && "Cannot instantiate TypedefType yet");
345 return QualType();
346}
347
Douglas Gregorcd281c32009-02-28 00:25:32 +0000348QualType
349TemplateTypeInstantiator::InstantiateTypeOfExprType(const TypeOfExprType *T,
350 unsigned Quals) const {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000351 // FIXME: Implement this
352 assert(false && "Cannot instantiate TypeOfExprType yet");
353 return QualType();
354}
355
Douglas Gregorcd281c32009-02-28 00:25:32 +0000356QualType
357TemplateTypeInstantiator::InstantiateTypeOfType(const TypeOfType *T,
358 unsigned Quals) const {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000359 // FIXME: Implement this
360 assert(false && "Cannot instantiate TypeOfType yet");
361 return QualType();
362}
363
Douglas Gregorcd281c32009-02-28 00:25:32 +0000364QualType
365TemplateTypeInstantiator::InstantiateRecordType(const RecordType *T,
366 unsigned Quals) const {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000367 // FIXME: Implement this
368 assert(false && "Cannot instantiate RecordType yet");
369 return QualType();
370}
371
Douglas Gregorcd281c32009-02-28 00:25:32 +0000372QualType
Douglas Gregorcd281c32009-02-28 00:25:32 +0000373TemplateTypeInstantiator::InstantiateEnumType(const EnumType *T,
374 unsigned Quals) const {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000375 // FIXME: Implement this
376 assert(false && "Cannot instantiate EnumType yet");
377 return QualType();
378}
379
Douglas Gregorcd281c32009-02-28 00:25:32 +0000380QualType
381TemplateTypeInstantiator::
382InstantiateTemplateTypeParmType(const TemplateTypeParmType *T,
383 unsigned Quals) const {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000384 if (T->getDepth() == 0) {
385 // Replace the template type parameter with its corresponding
386 // template argument.
387 assert(T->getIndex() < NumTemplateArgs && "Wrong # of template args");
388 assert(TemplateArgs[T->getIndex()].getKind() == TemplateArgument::Type &&
389 "Template argument kind mismatch");
390 QualType Result = TemplateArgs[T->getIndex()].getAsType();
Douglas Gregorcd281c32009-02-28 00:25:32 +0000391 if (Result.isNull() || !Quals)
Douglas Gregor99ebf652009-02-27 19:31:52 +0000392 return Result;
393
394 // C++ [dcl.ref]p1:
395 // [...] Cv-qualified references are ill-formed except when
396 // the cv-qualifiers are introduced through the use of a
397 // typedef (7.1.3) or of a template type argument (14.3), in
398 // which case the cv-qualifiers are ignored.
Douglas Gregorcd281c32009-02-28 00:25:32 +0000399 if (Quals && Result->isReferenceType())
400 Quals = 0;
Douglas Gregor99ebf652009-02-27 19:31:52 +0000401
Douglas Gregorcd281c32009-02-28 00:25:32 +0000402 return QualType(Result.getTypePtr(), Quals | Result.getCVRQualifiers());
Douglas Gregor99ebf652009-02-27 19:31:52 +0000403 }
404
405 // The template type parameter comes from an inner template (e.g.,
406 // the template parameter list of a member template inside the
407 // template we are instantiating). Create a new template type
408 // parameter with the template "level" reduced by one.
409 return SemaRef.Context.getTemplateTypeParmType(T->getDepth() - 1,
410 T->getIndex(),
Douglas Gregorcd281c32009-02-28 00:25:32 +0000411 T->getName())
412 .getQualifiedType(Quals);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000413}
414
Douglas Gregorcd281c32009-02-28 00:25:32 +0000415QualType
416TemplateTypeInstantiator::
417InstantiateClassTemplateSpecializationType(
418 const ClassTemplateSpecializationType *T,
419 unsigned Quals) const {
Douglas Gregor40808ce2009-03-09 23:48:35 +0000420 llvm::SmallVector<TemplateArgument, 16> InstantiatedTemplateArgs;
421 InstantiatedTemplateArgs.reserve(T->getNumArgs());
422 for (ClassTemplateSpecializationType::iterator Arg = T->begin(),
423 ArgEnd = T->end();
424 Arg != ArgEnd; ++Arg) {
425 switch (Arg->getKind()) {
426 case TemplateArgument::Type: {
427 QualType T = SemaRef.InstantiateType(Arg->getAsType(),
428 TemplateArgs, NumTemplateArgs,
429 Arg->getLocation(),
430 DeclarationName());
431 if (T.isNull())
432 return QualType();
433
434 InstantiatedTemplateArgs.push_back(
435 TemplateArgument(Arg->getLocation(), T));
436 break;
437 }
438
439 case TemplateArgument::Declaration:
440 case TemplateArgument::Integral:
441 InstantiatedTemplateArgs.push_back(*Arg);
442 break;
443
444 case TemplateArgument::Expression:
Douglas Gregorba498172009-03-13 21:01:28 +0000445 Sema::OwningExprResult E
446 = SemaRef.InstantiateExpr(Arg->getAsExpr(), TemplateArgs,
447 NumTemplateArgs);
448 if (E.isInvalid())
449 return QualType();
450 InstantiatedTemplateArgs.push_back((Expr *)E.release());
Douglas Gregor40808ce2009-03-09 23:48:35 +0000451 break;
452 }
453 }
454
455 // FIXME: We're missing the locations of the template name, '<', and
456 // '>'.
457 return SemaRef.CheckClassTemplateId(cast<ClassTemplateDecl>(T->getTemplate()),
458 Loc,
459 SourceLocation(),
460 &InstantiatedTemplateArgs[0],
461 InstantiatedTemplateArgs.size(),
462 SourceLocation());
Douglas Gregor99ebf652009-02-27 19:31:52 +0000463}
464
Douglas Gregorcd281c32009-02-28 00:25:32 +0000465QualType
466TemplateTypeInstantiator::
467InstantiateObjCInterfaceType(const ObjCInterfaceType *T,
468 unsigned Quals) const {
469 assert(false && "Objective-C types cannot be dependent");
Douglas Gregor99ebf652009-02-27 19:31:52 +0000470 return QualType();
471}
472
Douglas Gregorcd281c32009-02-28 00:25:32 +0000473QualType
474TemplateTypeInstantiator::
475InstantiateObjCQualifiedInterfaceType(const ObjCQualifiedInterfaceType *T,
476 unsigned Quals) const {
477 assert(false && "Objective-C types cannot be dependent");
Douglas Gregor99ebf652009-02-27 19:31:52 +0000478 return QualType();
479}
480
Douglas Gregorcd281c32009-02-28 00:25:32 +0000481QualType
482TemplateTypeInstantiator::
483InstantiateObjCQualifiedIdType(const ObjCQualifiedIdType *T,
484 unsigned Quals) const {
485 assert(false && "Objective-C types cannot be dependent");
Douglas Gregor99ebf652009-02-27 19:31:52 +0000486 return QualType();
487}
488
Douglas Gregorcd281c32009-02-28 00:25:32 +0000489QualType
490TemplateTypeInstantiator::
491InstantiateObjCQualifiedClassType(const ObjCQualifiedClassType *T,
492 unsigned Quals) const {
493 assert(false && "Objective-C types cannot be dependent");
Douglas Gregor99ebf652009-02-27 19:31:52 +0000494 return QualType();
495}
496
Douglas Gregorcd281c32009-02-28 00:25:32 +0000497/// \brief The actual implementation of Sema::InstantiateType().
498QualType TemplateTypeInstantiator::Instantiate(QualType T) const {
499 // If T is not a dependent type, there is nothing to do.
500 if (!T->isDependentType())
501 return T;
502
503 switch (T->getTypeClass()) {
504#define TYPE(Class, Base) \
505 case Type::Class: \
506 return Instantiate##Class##Type(cast<Class##Type>(T.getTypePtr()), \
507 T.getCVRQualifiers());
508#define ABSTRACT_TYPE(Class, Base)
509#include "clang/AST/TypeNodes.def"
510 }
511
512 assert(false && "Not all types have been decoded for instantiation");
513 return QualType();
514}
Douglas Gregor99ebf652009-02-27 19:31:52 +0000515
516/// \brief Instantiate the type T with a given set of template arguments.
517///
518/// This routine substitutes the given template arguments into the
519/// type T and produces the instantiated type.
520///
521/// \param T the type into which the template arguments will be
522/// substituted. If this type is not dependent, it will be returned
523/// immediately.
524///
525/// \param TemplateArgs the template arguments that will be
526/// substituted for the top-level template parameters within T.
527///
528/// \param NumTemplateArgs the number of template arguments provided
529/// by TemplateArgs.
530///
531/// \param Loc the location in the source code where this substitution
532/// is being performed. It will typically be the location of the
533/// declarator (if we're instantiating the type of some declaration)
534/// or the location of the type in the source code (if, e.g., we're
535/// instantiating the type of a cast expression).
536///
537/// \param Entity the name of the entity associated with a declaration
538/// being instantiated (if any). May be empty to indicate that there
539/// is no such entity (if, e.g., this is a type that occurs as part of
540/// a cast expression) or that the entity has no name (e.g., an
541/// unnamed function parameter).
542///
543/// \returns If the instantiation succeeds, the instantiated
544/// type. Otherwise, produces diagnostics and returns a NULL type.
545QualType Sema::InstantiateType(QualType T,
546 const TemplateArgument *TemplateArgs,
547 unsigned NumTemplateArgs,
548 SourceLocation Loc, DeclarationName Entity) {
Douglas Gregordf667e72009-03-10 20:44:00 +0000549 assert(!ActiveTemplateInstantiations.empty() &&
550 "Cannot perform an instantiation without some context on the "
551 "instantiation stack");
552
Douglas Gregor99ebf652009-02-27 19:31:52 +0000553 // If T is not a dependent type, there is nothing to do.
554 if (!T->isDependentType())
555 return T;
556
Douglas Gregorcd281c32009-02-28 00:25:32 +0000557 TemplateTypeInstantiator Instantiator(*this, TemplateArgs, NumTemplateArgs,
558 Loc, Entity);
559 return Instantiator(T);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000560}
Douglas Gregor2943aed2009-03-03 04:44:36 +0000561
Douglas Gregora0e500d2009-03-12 16:53:44 +0000562//===----------------------------------------------------------------------===/
563// Template Instantiation for Expressions
564//===----------------------------------------------------------------------===/
Douglas Gregor313a81d2009-03-12 18:36:18 +0000565namespace {
566 class VISIBILITY_HIDDEN TemplateExprInstantiator
567 : public StmtVisitor<TemplateExprInstantiator, Sema::OwningExprResult> {
568 Sema &SemaRef;
569 const TemplateArgument *TemplateArgs;
570 unsigned NumTemplateArgs;
571
572 public:
Douglas Gregorba498172009-03-13 21:01:28 +0000573 typedef Sema::OwningExprResult OwningExprResult;
574
Douglas Gregor313a81d2009-03-12 18:36:18 +0000575 TemplateExprInstantiator(Sema &SemaRef,
576 const TemplateArgument *TemplateArgs,
577 unsigned NumTemplateArgs)
578 : SemaRef(SemaRef), TemplateArgs(TemplateArgs),
579 NumTemplateArgs(NumTemplateArgs) { }
580
581 // FIXME: Once we get closer to completion, replace these
582 // manually-written declarations with automatically-generated ones
583 // from clang/AST/StmtNodes.def.
Douglas Gregorba498172009-03-13 21:01:28 +0000584 OwningExprResult VisitIntegerLiteral(IntegerLiteral *E);
585 OwningExprResult VisitDeclRefExpr(DeclRefExpr *E);
586 OwningExprResult VisitParenExpr(ParenExpr *E);
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000587 OwningExprResult VisitUnaryOperator(UnaryOperator *E);
Douglas Gregorba498172009-03-13 21:01:28 +0000588 OwningExprResult VisitBinaryOperator(BinaryOperator *E);
589 OwningExprResult VisitCXXOperatorCallExpr(CXXOperatorCallExpr *E);
590 OwningExprResult VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E);
591 OwningExprResult VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *E);
Douglas Gregor313a81d2009-03-12 18:36:18 +0000592
593 // Base case. I'm supposed to ignore this.
Douglas Gregordf032512009-03-12 22:46:12 +0000594 Sema::OwningExprResult VisitStmt(Stmt *) {
595 assert(false && "Cannot instantiate this kind of expression");
596 return SemaRef.ExprError();
597 }
Douglas Gregor313a81d2009-03-12 18:36:18 +0000598 };
599}
600
601Sema::OwningExprResult
602TemplateExprInstantiator::VisitIntegerLiteral(IntegerLiteral *E) {
603 // FIXME: Can't we just re-use the expression node?
604 return SemaRef.Owned(new (SemaRef.Context) IntegerLiteral(E->getValue(),
605 E->getType(),
606 E->getLocation()));
607}
608
609Sema::OwningExprResult
610TemplateExprInstantiator::VisitDeclRefExpr(DeclRefExpr *E) {
611 Decl *D = E->getDecl();
612 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(D)) {
613 assert(NTTP->getDepth() == 0 && "No nested templates yet");
Douglas Gregorc971f862009-03-12 22:20:26 +0000614 const TemplateArgument &Arg = TemplateArgs[NTTP->getPosition()];
Douglas Gregor313a81d2009-03-12 18:36:18 +0000615 return SemaRef.Owned(new (SemaRef.Context) IntegerLiteral(
Douglas Gregorc971f862009-03-12 22:20:26 +0000616 *Arg.getAsIntegral(),
617 Arg.getIntegralType(),
618 E->getSourceRange().getBegin()));
Douglas Gregor313a81d2009-03-12 18:36:18 +0000619 } else
620 assert(false && "Can't handle arbitrary declaration references");
621
622 return SemaRef.ExprError();
623}
624
625Sema::OwningExprResult
626TemplateExprInstantiator::VisitParenExpr(ParenExpr *E) {
627 Sema::OwningExprResult SubExpr
628 = SemaRef.InstantiateExpr(E->getSubExpr(), TemplateArgs, NumTemplateArgs);
629 if (SubExpr.isInvalid())
630 return SemaRef.ExprError();
631
632 return SemaRef.Owned(new (SemaRef.Context) ParenExpr(
633 E->getLParen(), E->getRParen(),
634 (Expr *)SubExpr.release()));
635}
636
Douglas Gregora0e500d2009-03-12 16:53:44 +0000637Sema::OwningExprResult
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000638TemplateExprInstantiator::VisitUnaryOperator(UnaryOperator *E) {
639 Sema::OwningExprResult Arg = Visit(E->getSubExpr());
640 if (Arg.isInvalid())
641 return SemaRef.ExprError();
642
643 return SemaRef.CreateBuiltinUnaryOp(E->getOperatorLoc(),
644 E->getOpcode(),
645 move(Arg));
646}
647
648Sema::OwningExprResult
Douglas Gregordf032512009-03-12 22:46:12 +0000649TemplateExprInstantiator::VisitBinaryOperator(BinaryOperator *E) {
650 Sema::OwningExprResult LHS = Visit(E->getLHS());
651 if (LHS.isInvalid())
652 return SemaRef.ExprError();
653
654 Sema::OwningExprResult RHS = Visit(E->getRHS());
655 if (RHS.isInvalid())
656 return SemaRef.ExprError();
657
658 Sema::OwningExprResult Result
659 = SemaRef.CreateBuiltinBinOp(E->getOperatorLoc(),
660 E->getOpcode(),
661 (Expr *)LHS.get(),
662 (Expr *)RHS.get());
663 if (Result.isInvalid())
664 return SemaRef.ExprError();
665
666 LHS.release();
667 RHS.release();
668 return move(Result);
669}
670
671Sema::OwningExprResult
Douglas Gregor3fd95ce2009-03-13 00:33:25 +0000672TemplateExprInstantiator::VisitCXXOperatorCallExpr(CXXOperatorCallExpr *E) {
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000673 Sema::OwningExprResult First = Visit(E->getArg(0));
674 if (First.isInvalid())
Douglas Gregor3fd95ce2009-03-13 00:33:25 +0000675 return SemaRef.ExprError();
676
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000677 Expr *Args[2] = { (Expr *)First.get(), 0 };
Douglas Gregor3fd95ce2009-03-13 00:33:25 +0000678
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000679 Sema::OwningExprResult Second(SemaRef);
680 if (E->getNumArgs() == 2) {
681 Second = Visit(E->getArg(1));
682
683 if (Second.isInvalid())
684 return SemaRef.ExprError();
685
686 Args[1] = (Expr *)Second.get();
687 }
Douglas Gregor063daf62009-03-13 18:40:31 +0000688
689 if (!E->isTypeDependent()) {
690 // Since our original expression was not type-dependent, we do not
691 // perform lookup again at instantiation time (C++ [temp.dep]p1).
692 // Instead, we just build the new overloaded operator call
693 // expression.
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000694 First.release();
695 Second.release();
Douglas Gregor063daf62009-03-13 18:40:31 +0000696 return SemaRef.Owned(new (SemaRef.Context) CXXOperatorCallExpr(
697 SemaRef.Context,
698 E->getOperator(),
699 E->getCallee(),
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000700 Args, E->getNumArgs(),
701 E->getType(),
Douglas Gregor063daf62009-03-13 18:40:31 +0000702 E->getOperatorLoc()));
703 }
704
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000705 bool isPostIncDec = E->getNumArgs() == 2 &&
706 (E->getOperator() == OO_PlusPlus || E->getOperator() == OO_MinusMinus);
707 if (E->getNumArgs() == 1 || isPostIncDec) {
708 if (!Args[0]->getType()->isOverloadableType()) {
709 // The argument is not of overloadable type, so try to create a
710 // built-in unary operation.
711 UnaryOperator::Opcode Opc
712 = UnaryOperator::getOverloadedOpcode(E->getOperator(), isPostIncDec);
713
714 return SemaRef.CreateBuiltinUnaryOp(E->getOperatorLoc(), Opc,
715 move(First));
716 }
717
718 // Fall through to perform overload resolution
719 } else {
720 assert(E->getNumArgs() == 2 && "Expected binary operation");
721
722 Sema::OwningExprResult Result(SemaRef);
723 if (!Args[0]->getType()->isOverloadableType() &&
724 !Args[1]->getType()->isOverloadableType()) {
725 // Neither of the arguments is an overloadable type, so try to
726 // create a built-in binary operation.
727 BinaryOperator::Opcode Opc =
728 BinaryOperator::getOverloadedOpcode(E->getOperator());
729 Result = SemaRef.CreateBuiltinBinOp(E->getOperatorLoc(), Opc,
730 Args[0], Args[1]);
731 if (Result.isInvalid())
732 return SemaRef.ExprError();
733
734 First.release();
735 Second.release();
736 return move(Result);
737 }
738
739 // Fall through to perform overload resolution.
740 }
741
742 // Compute the set of functions that were found at template
743 // definition time.
744 Sema::FunctionSet Functions;
745 DeclRefExpr *DRE = cast<DeclRefExpr>(E->getCallee());
746 OverloadedFunctionDecl *Overloads
747 = cast<OverloadedFunctionDecl>(DRE->getDecl());
748
749 // FIXME: Do we have to check
750 // IsAcceptableNonMemberOperatorCandidate for each of these?
751 for (OverloadedFunctionDecl::function_iterator
752 F = Overloads->function_begin(),
753 FEnd = Overloads->function_end();
754 F != FEnd; ++F)
755 Functions.insert(*F);
756
757 // Add any functions found via argument-dependent lookup.
758 DeclarationName OpName
759 = SemaRef.Context.DeclarationNames.getCXXOperatorName(E->getOperator());
760 SemaRef.ArgumentDependentLookup(OpName, Args, E->getNumArgs(), Functions);
761
762 // Create the overloaded operator invocation.
763 if (E->getNumArgs() == 1 || isPostIncDec) {
764 UnaryOperator::Opcode Opc
765 = UnaryOperator::getOverloadedOpcode(E->getOperator(), isPostIncDec);
766 return SemaRef.CreateOverloadedUnaryOp(E->getOperatorLoc(), Opc,
767 Functions, move(First));
768 }
769
770 // FIXME: This would be far less ugly if CreateOverloadedBinOp took
771 // in ExprArg arguments!
Douglas Gregor063daf62009-03-13 18:40:31 +0000772 BinaryOperator::Opcode Opc =
773 BinaryOperator::getOverloadedOpcode(E->getOperator());
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000774 OwningExprResult Result
775 = SemaRef.CreateOverloadedBinOp(E->getOperatorLoc(), Opc,
776 Functions, Args[0], Args[1]);
Douglas Gregor063daf62009-03-13 18:40:31 +0000777
Douglas Gregor3fd95ce2009-03-13 00:33:25 +0000778 if (Result.isInvalid())
779 return SemaRef.ExprError();
780
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000781 First.release();
782 Second.release();
Douglas Gregor3fd95ce2009-03-13 00:33:25 +0000783 return move(Result);
784}
785
786Sema::OwningExprResult
Douglas Gregorba498172009-03-13 21:01:28 +0000787TemplateExprInstantiator::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E) {
788 bool isSizeOf = E->isSizeOf();
789
790 if (E->isArgumentType()) {
791 QualType T = E->getArgumentType();
792 if (T->isDependentType()) {
793 T = SemaRef.InstantiateType(T, TemplateArgs, NumTemplateArgs,
794 /*FIXME*/E->getOperatorLoc(),
795 &SemaRef.PP.getIdentifierTable().get("sizeof"));
796 if (T.isNull())
797 return SemaRef.ExprError();
798 }
799
800 return SemaRef.CreateSizeOfAlignOfExpr(T, E->getOperatorLoc(), isSizeOf,
801 E->getSourceRange());
802 }
803
804 Sema::OwningExprResult Arg = Visit(E->getArgumentExpr());
805 if (Arg.isInvalid())
806 return SemaRef.ExprError();
807
808 Sema::OwningExprResult Result
809 = SemaRef.CreateSizeOfAlignOfExpr((Expr *)Arg.get(), E->getOperatorLoc(),
810 isSizeOf, E->getSourceRange());
811 if (Result.isInvalid())
812 return SemaRef.ExprError();
813
814 Arg.release();
815 return move(Result);
816}
817
818Sema::OwningExprResult
819TemplateExprInstantiator::VisitCXXTemporaryObjectExpr(
820 CXXTemporaryObjectExpr *E) {
821 QualType T = E->getType();
822 if (T->isDependentType()) {
823 T = SemaRef.InstantiateType(T, TemplateArgs, NumTemplateArgs,
824 E->getTypeBeginLoc(), DeclarationName());
825 if (T.isNull())
826 return SemaRef.ExprError();
827 }
828
829 llvm::SmallVector<Expr *, 16> Args;
830 Args.reserve(E->getNumArgs());
831 bool Invalid = false;
832 for (CXXTemporaryObjectExpr::arg_iterator Arg = E->arg_begin(),
833 ArgEnd = E->arg_end();
834 Arg != ArgEnd; ++Arg) {
835 OwningExprResult InstantiatedArg = Visit(*Arg);
836 if (InstantiatedArg.isInvalid()) {
837 Invalid = true;
838 break;
839 }
840
841 Args.push_back((Expr *)InstantiatedArg.release());
842 }
843
844 if (!Invalid) {
845 SourceLocation CommaLoc;
846 // FIXME: HACK!
847 if (Args.size() > 1)
848 CommaLoc
849 = SemaRef.PP.getLocForEndOfToken(Args[0]->getSourceRange().getEnd());
850 Sema::ExprResult Result
851 = SemaRef.ActOnCXXTypeConstructExpr(SourceRange(E->getTypeBeginLoc()
852 /*, FIXME*/),
853 T.getAsOpaquePtr(),
854 /*FIXME*/E->getTypeBeginLoc(),
855 (void**)&Args[0], Args.size(),
856 /*HACK*/&CommaLoc,
857 E->getSourceRange().getEnd());
858 if (!Result.isInvalid())
859 return SemaRef.Owned(Result);
860 }
861
862 // Clean up the instantiated arguments.
863 // FIXME: Would rather do this with RAII.
864 for (unsigned Idx = 0; Idx < Args.size(); ++Idx)
865 SemaRef.DeleteExpr(Args[Idx]);
866
867 return SemaRef.ExprError();
868}
869
870Sema::OwningExprResult
Douglas Gregora0e500d2009-03-12 16:53:44 +0000871Sema::InstantiateExpr(Expr *E, const TemplateArgument *TemplateArgs,
872 unsigned NumTemplateArgs) {
Douglas Gregor313a81d2009-03-12 18:36:18 +0000873 TemplateExprInstantiator Instantiator(*this, TemplateArgs, NumTemplateArgs);
874 return Instantiator.Visit(E);
Douglas Gregora0e500d2009-03-12 16:53:44 +0000875}
876
Douglas Gregor2943aed2009-03-03 04:44:36 +0000877/// \brief Instantiate the base class specifiers of the given class
878/// template specialization.
879///
880/// Produces a diagnostic and returns true on error, returns false and
881/// attaches the instantiated base classes to the class template
882/// specialization if successful.
883bool
884Sema::InstantiateBaseSpecifiers(
885 ClassTemplateSpecializationDecl *ClassTemplateSpec,
886 ClassTemplateDecl *ClassTemplate) {
887 bool Invalid = false;
888 llvm::SmallVector<CXXBaseSpecifier*, 8> InstantiatedBases;
889 for (ClassTemplateSpecializationDecl::base_class_iterator
890 Base = ClassTemplate->getTemplatedDecl()->bases_begin(),
891 BaseEnd = ClassTemplate->getTemplatedDecl()->bases_end();
Douglas Gregor27b152f2009-03-10 18:52:44 +0000892 Base != BaseEnd; ++Base) {
Douglas Gregor2943aed2009-03-03 04:44:36 +0000893 if (!Base->getType()->isDependentType()) {
894 // FIXME: Allocate via ASTContext
895 InstantiatedBases.push_back(new CXXBaseSpecifier(*Base));
896 continue;
897 }
898
899 QualType BaseType = InstantiateType(Base->getType(),
900 ClassTemplateSpec->getTemplateArgs(),
901 ClassTemplateSpec->getNumTemplateArgs(),
902 Base->getSourceRange().getBegin(),
903 DeclarationName());
904 if (BaseType.isNull()) {
905 Invalid = true;
906 continue;
907 }
908
909 if (CXXBaseSpecifier *InstantiatedBase
910 = CheckBaseSpecifier(ClassTemplateSpec,
911 Base->getSourceRange(),
912 Base->isVirtual(),
913 Base->getAccessSpecifierAsWritten(),
914 BaseType,
915 /*FIXME: Not totally accurate */
916 Base->getSourceRange().getBegin()))
917 InstantiatedBases.push_back(InstantiatedBase);
918 else
919 Invalid = true;
920 }
921
Douglas Gregor27b152f2009-03-10 18:52:44 +0000922 if (!Invalid &&
923 AttachBaseSpecifiers(ClassTemplateSpec, &InstantiatedBases[0],
Douglas Gregor2943aed2009-03-03 04:44:36 +0000924 InstantiatedBases.size()))
925 Invalid = true;
926
927 return Invalid;
928}
929
930bool
931Sema::InstantiateClassTemplateSpecialization(
932 ClassTemplateSpecializationDecl *ClassTemplateSpec,
933 bool ExplicitInstantiation) {
934 // Perform the actual instantiation on the canonical declaration.
935 ClassTemplateSpec = cast<ClassTemplateSpecializationDecl>(
936 Context.getCanonicalDecl(ClassTemplateSpec));
937
938 // We can only instantiate something that hasn't already been
939 // instantiated or specialized. Fail without any diagnostics: our
940 // caller will provide an error message.
941 if (ClassTemplateSpec->getSpecializationKind() != TSK_Undeclared)
942 return true;
943
944 // FIXME: Push this class template instantiation onto the
945 // instantiation stack, checking for recursion that exceeds a
946 // certain depth.
947
948 // FIXME: Perform class template partial specialization to select
949 // the best template.
950 ClassTemplateDecl *Template = ClassTemplateSpec->getSpecializedTemplate();
951
952 if (!Template->getTemplatedDecl()->getDefinition(Context)) {
953 Diag(ClassTemplateSpec->getLocation(),
954 diag::err_template_implicit_instantiate_undefined)
955 << Context.getTypeDeclType(ClassTemplateSpec);
956 Diag(Template->getTemplatedDecl()->getLocation(),
957 diag::note_template_decl_here);
958 return true;
959 }
960
961 // Note that this is an instantiation.
962 ClassTemplateSpec->setSpecializationKind(
963 ExplicitInstantiation? TSK_ExplicitInstantiation
964 : TSK_ImplicitInstantiation);
965
966
967 bool Invalid = false;
968
Douglas Gregor26dce442009-03-10 00:06:19 +0000969 InstantiatingTemplate Inst(*this, ClassTemplateSpec->getLocation(),
970 ClassTemplateSpec);
971 if (Inst)
972 return true;
973
Douglas Gregor2943aed2009-03-03 04:44:36 +0000974 // Enter the scope of this instantiation. We don't use
975 // PushDeclContext because we don't have a scope.
976 DeclContext *PreviousContext = CurContext;
977 CurContext = ClassTemplateSpec;
978
979 // Start the definition of this instantiation.
980 ClassTemplateSpec->startDefinition();
981
Douglas Gregor2943aed2009-03-03 04:44:36 +0000982
983 // Instantiate the base class specifiers.
984 if (InstantiateBaseSpecifiers(ClassTemplateSpec, Template))
985 Invalid = true;
986
Douglas Gregor4fdf1fa2009-03-11 16:48:53 +0000987 // FIXME: Create the injected-class-name for the
988 // instantiation. Should this be a typedef or something like it?
989
990 RecordDecl *Pattern = Template->getTemplatedDecl();
Douglas Gregor3cf538d2009-03-11 18:59:21 +0000991 llvm::SmallVector<DeclTy *, 32> Fields;
Douglas Gregor4fdf1fa2009-03-11 16:48:53 +0000992 for (RecordDecl::decl_iterator Member = Pattern->decls_begin(),
993 MemberEnd = Pattern->decls_end();
994 Member != MemberEnd; ++Member) {
995 if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(*Member)) {
996 // FIXME: Simplified instantiation of typedefs needs to be made
997 // "real".
998 QualType T = Typedef->getUnderlyingType();
999 if (T->isDependentType()) {
1000 T = InstantiateType(T, ClassTemplateSpec->getTemplateArgs(),
1001 ClassTemplateSpec->getNumTemplateArgs(),
1002 Typedef->getLocation(),
1003 Typedef->getDeclName());
1004 if (T.isNull()) {
1005 Invalid = true;
1006 T = Context.IntTy;
1007 }
1008 }
1009
1010 // Create the new typedef
1011 TypedefDecl *New
1012 = TypedefDecl::Create(Context, ClassTemplateSpec,
1013 Typedef->getLocation(),
1014 Typedef->getIdentifier(),
1015 T);
1016 ClassTemplateSpec->addDecl(New);
Douglas Gregor3cf538d2009-03-11 18:59:21 +00001017 }
1018 else if (FieldDecl *Field = dyn_cast<FieldDecl>(*Member)) {
1019 // FIXME: Simplified instantiation of fields needs to be made
1020 // "real".
Douglas Gregora0e500d2009-03-12 16:53:44 +00001021 bool InvalidDecl = false;
Douglas Gregor3cf538d2009-03-11 18:59:21 +00001022 QualType T = Field->getType();
1023 if (T->isDependentType()) {
1024 T = InstantiateType(T, ClassTemplateSpec->getTemplateArgs(),
1025 ClassTemplateSpec->getNumTemplateArgs(),
1026 Field->getLocation(),
1027 Field->getDeclName());
1028 if (!T.isNull() && T->isFunctionType()) {
1029 // C++ [temp.arg.type]p3:
1030 // If a declaration acquires a function type through a type
1031 // dependent on a template-parameter and this causes a
1032 // declaration that does not use the syntactic form of a
1033 // function declarator to have function type, the program is
1034 // ill-formed.
1035 Diag(Field->getLocation(), diag::err_field_instantiates_to_function)
1036 << T;
1037 T = QualType();
Douglas Gregora0e500d2009-03-12 16:53:44 +00001038 InvalidDecl = true;
Douglas Gregor3cf538d2009-03-11 18:59:21 +00001039 }
1040 }
1041
Douglas Gregora0e500d2009-03-12 16:53:44 +00001042 Expr *BitWidth = Field->getBitWidth();
1043 if (InvalidDecl)
1044 BitWidth = 0;
1045 if (BitWidth &&
1046 (BitWidth->isTypeDependent() || BitWidth->isValueDependent())) {
1047 OwningExprResult InstantiatedBitWidth
1048 = InstantiateExpr(BitWidth,
1049 ClassTemplateSpec->getTemplateArgs(),
1050 ClassTemplateSpec->getNumTemplateArgs());
1051 if (InstantiatedBitWidth.isInvalid()) {
1052 Invalid = InvalidDecl = true;
1053 BitWidth = 0;
1054 } else
1055 BitWidth = (Expr *)InstantiatedBitWidth.release();
1056 }
1057
Douglas Gregor3cf538d2009-03-11 18:59:21 +00001058 FieldDecl *New = CheckFieldDecl(Field->getDeclName(), T,
1059 ClassTemplateSpec,
1060 Field->getLocation(),
1061 Field->isMutable(),
Douglas Gregora0e500d2009-03-12 16:53:44 +00001062 BitWidth,
Douglas Gregor4dd55f52009-03-11 20:50:30 +00001063 Field->getAccess(),
Douglas Gregor3cf538d2009-03-11 18:59:21 +00001064 0);
1065 if (New) {
1066 ClassTemplateSpec->addDecl(New);
1067 Fields.push_back(New);
1068
Douglas Gregora0e500d2009-03-12 16:53:44 +00001069 if (InvalidDecl)
1070 New->setInvalidDecl();
1071
Douglas Gregor3cf538d2009-03-11 18:59:21 +00001072 if (New->isInvalidDecl())
1073 Invalid = true;
1074 }
Douglas Gregor4fdf1fa2009-03-11 16:48:53 +00001075 }
1076 }
1077
Douglas Gregor3cf538d2009-03-11 18:59:21 +00001078 // Finish checking fields.
1079 ActOnFields(0, ClassTemplateSpec->getLocation(), ClassTemplateSpec,
1080 &Fields[0], Fields.size(), SourceLocation(), SourceLocation(),
1081 0);
1082
Douglas Gregor2943aed2009-03-03 04:44:36 +00001083 // Add any implicitly-declared members that we might need.
1084 AddImplicitlyDeclaredMembersToClass(ClassTemplateSpec);
1085
Douglas Gregor2943aed2009-03-03 04:44:36 +00001086 // Exit the scope of this instantiation.
1087 CurContext = PreviousContext;
1088
1089 return Invalid;
1090}