blob: dbdc5a8a7169fc9d06e959b82534e87b4a5072bd [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 {
Anders Carlsson76b1c842009-03-15 20:12:13 +0000286 Expr *ArraySize = T->getSizeExpr();
287 assert(ArraySize->isValueDependent() &&
288 "dependent sized array types must have value dependent size expr");
289
290 // Instantiate the element type if needed
291 QualType ElementType = T->getElementType();
292 if (ElementType->isDependentType()) {
293 ElementType = Instantiate(ElementType);
294 if (ElementType.isNull())
295 return QualType();
296 }
297
298 // Instantiate the size expression
299 Sema::OwningExprResult InstantiatedArraySize =
300 SemaRef.InstantiateExpr(ArraySize, TemplateArgs, NumTemplateArgs);
301 if (InstantiatedArraySize.isInvalid())
302 return QualType();
303
304 return SemaRef.BuildArrayType(ElementType, T->getSizeModifier(),
305 (Expr *)InstantiatedArraySize.release(),
306 T->getIndexTypeQualifier(), Loc, Entity);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000307}
308
Douglas Gregorcd281c32009-02-28 00:25:32 +0000309QualType
310TemplateTypeInstantiator::InstantiateVectorType(const VectorType *T,
311 unsigned Quals) const {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000312 // FIXME: Implement this
313 assert(false && "Cannot instantiate VectorType yet");
314 return QualType();
315}
316
Douglas Gregorcd281c32009-02-28 00:25:32 +0000317QualType
318TemplateTypeInstantiator::InstantiateExtVectorType(const ExtVectorType *T,
319 unsigned Quals) const {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000320 // FIXME: Implement this
321 assert(false && "Cannot instantiate ExtVectorType yet");
322 return QualType();
323}
324
Douglas Gregorcd281c32009-02-28 00:25:32 +0000325QualType
326TemplateTypeInstantiator::
327InstantiateFunctionProtoType(const FunctionProtoType *T,
328 unsigned Quals) const {
Douglas Gregor724651c2009-02-28 01:04:19 +0000329 QualType ResultType = Instantiate(T->getResultType());
330 if (ResultType.isNull())
331 return ResultType;
332
333 llvm::SmallVector<QualType, 16> ParamTypes;
334 for (FunctionProtoType::arg_type_iterator Param = T->arg_type_begin(),
335 ParamEnd = T->arg_type_end();
336 Param != ParamEnd; ++Param) {
337 QualType P = Instantiate(*Param);
338 if (P.isNull())
339 return P;
340
341 ParamTypes.push_back(P);
342 }
343
344 return SemaRef.BuildFunctionType(ResultType, &ParamTypes[0],
345 ParamTypes.size(),
346 T->isVariadic(), T->getTypeQuals(),
347 Loc, Entity);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000348}
349
Douglas Gregorcd281c32009-02-28 00:25:32 +0000350QualType
351TemplateTypeInstantiator::
352InstantiateFunctionNoProtoType(const FunctionNoProtoType *T,
353 unsigned Quals) const {
Douglas Gregor724651c2009-02-28 01:04:19 +0000354 assert(false && "Functions without prototypes cannot be dependent.");
Douglas Gregor99ebf652009-02-27 19:31:52 +0000355 return QualType();
356}
357
Douglas Gregorcd281c32009-02-28 00:25:32 +0000358QualType
359TemplateTypeInstantiator::InstantiateTypedefType(const TypedefType *T,
360 unsigned Quals) const {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000361 // FIXME: Implement this
362 assert(false && "Cannot instantiate TypedefType yet");
363 return QualType();
364}
365
Douglas Gregorcd281c32009-02-28 00:25:32 +0000366QualType
367TemplateTypeInstantiator::InstantiateTypeOfExprType(const TypeOfExprType *T,
368 unsigned Quals) const {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000369 // FIXME: Implement this
370 assert(false && "Cannot instantiate TypeOfExprType yet");
371 return QualType();
372}
373
Douglas Gregorcd281c32009-02-28 00:25:32 +0000374QualType
375TemplateTypeInstantiator::InstantiateTypeOfType(const TypeOfType *T,
376 unsigned Quals) const {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000377 // FIXME: Implement this
378 assert(false && "Cannot instantiate TypeOfType yet");
379 return QualType();
380}
381
Douglas Gregorcd281c32009-02-28 00:25:32 +0000382QualType
383TemplateTypeInstantiator::InstantiateRecordType(const RecordType *T,
384 unsigned Quals) const {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000385 // FIXME: Implement this
386 assert(false && "Cannot instantiate RecordType yet");
387 return QualType();
388}
389
Douglas Gregorcd281c32009-02-28 00:25:32 +0000390QualType
Douglas Gregorcd281c32009-02-28 00:25:32 +0000391TemplateTypeInstantiator::InstantiateEnumType(const EnumType *T,
392 unsigned Quals) const {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000393 // FIXME: Implement this
394 assert(false && "Cannot instantiate EnumType yet");
395 return QualType();
396}
397
Douglas Gregorcd281c32009-02-28 00:25:32 +0000398QualType
399TemplateTypeInstantiator::
400InstantiateTemplateTypeParmType(const TemplateTypeParmType *T,
401 unsigned Quals) const {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000402 if (T->getDepth() == 0) {
403 // Replace the template type parameter with its corresponding
404 // template argument.
405 assert(T->getIndex() < NumTemplateArgs && "Wrong # of template args");
406 assert(TemplateArgs[T->getIndex()].getKind() == TemplateArgument::Type &&
407 "Template argument kind mismatch");
408 QualType Result = TemplateArgs[T->getIndex()].getAsType();
Douglas Gregorcd281c32009-02-28 00:25:32 +0000409 if (Result.isNull() || !Quals)
Douglas Gregor99ebf652009-02-27 19:31:52 +0000410 return Result;
411
412 // C++ [dcl.ref]p1:
413 // [...] Cv-qualified references are ill-formed except when
414 // the cv-qualifiers are introduced through the use of a
415 // typedef (7.1.3) or of a template type argument (14.3), in
416 // which case the cv-qualifiers are ignored.
Douglas Gregorcd281c32009-02-28 00:25:32 +0000417 if (Quals && Result->isReferenceType())
418 Quals = 0;
Douglas Gregor99ebf652009-02-27 19:31:52 +0000419
Douglas Gregorcd281c32009-02-28 00:25:32 +0000420 return QualType(Result.getTypePtr(), Quals | Result.getCVRQualifiers());
Douglas Gregor99ebf652009-02-27 19:31:52 +0000421 }
422
423 // The template type parameter comes from an inner template (e.g.,
424 // the template parameter list of a member template inside the
425 // template we are instantiating). Create a new template type
426 // parameter with the template "level" reduced by one.
427 return SemaRef.Context.getTemplateTypeParmType(T->getDepth() - 1,
428 T->getIndex(),
Douglas Gregorcd281c32009-02-28 00:25:32 +0000429 T->getName())
430 .getQualifiedType(Quals);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000431}
432
Douglas Gregorcd281c32009-02-28 00:25:32 +0000433QualType
434TemplateTypeInstantiator::
435InstantiateClassTemplateSpecializationType(
436 const ClassTemplateSpecializationType *T,
437 unsigned Quals) const {
Douglas Gregor40808ce2009-03-09 23:48:35 +0000438 llvm::SmallVector<TemplateArgument, 16> InstantiatedTemplateArgs;
439 InstantiatedTemplateArgs.reserve(T->getNumArgs());
440 for (ClassTemplateSpecializationType::iterator Arg = T->begin(),
441 ArgEnd = T->end();
442 Arg != ArgEnd; ++Arg) {
443 switch (Arg->getKind()) {
444 case TemplateArgument::Type: {
445 QualType T = SemaRef.InstantiateType(Arg->getAsType(),
446 TemplateArgs, NumTemplateArgs,
447 Arg->getLocation(),
448 DeclarationName());
449 if (T.isNull())
450 return QualType();
451
452 InstantiatedTemplateArgs.push_back(
453 TemplateArgument(Arg->getLocation(), T));
454 break;
455 }
456
457 case TemplateArgument::Declaration:
458 case TemplateArgument::Integral:
459 InstantiatedTemplateArgs.push_back(*Arg);
460 break;
461
462 case TemplateArgument::Expression:
Douglas Gregorba498172009-03-13 21:01:28 +0000463 Sema::OwningExprResult E
464 = SemaRef.InstantiateExpr(Arg->getAsExpr(), TemplateArgs,
465 NumTemplateArgs);
466 if (E.isInvalid())
467 return QualType();
468 InstantiatedTemplateArgs.push_back((Expr *)E.release());
Douglas Gregor40808ce2009-03-09 23:48:35 +0000469 break;
470 }
471 }
472
473 // FIXME: We're missing the locations of the template name, '<', and
474 // '>'.
475 return SemaRef.CheckClassTemplateId(cast<ClassTemplateDecl>(T->getTemplate()),
476 Loc,
477 SourceLocation(),
478 &InstantiatedTemplateArgs[0],
479 InstantiatedTemplateArgs.size(),
480 SourceLocation());
Douglas Gregor99ebf652009-02-27 19:31:52 +0000481}
482
Douglas Gregorcd281c32009-02-28 00:25:32 +0000483QualType
484TemplateTypeInstantiator::
485InstantiateObjCInterfaceType(const ObjCInterfaceType *T,
486 unsigned Quals) const {
487 assert(false && "Objective-C types cannot be dependent");
Douglas Gregor99ebf652009-02-27 19:31:52 +0000488 return QualType();
489}
490
Douglas Gregorcd281c32009-02-28 00:25:32 +0000491QualType
492TemplateTypeInstantiator::
493InstantiateObjCQualifiedInterfaceType(const ObjCQualifiedInterfaceType *T,
494 unsigned Quals) const {
495 assert(false && "Objective-C types cannot be dependent");
Douglas Gregor99ebf652009-02-27 19:31:52 +0000496 return QualType();
497}
498
Douglas Gregorcd281c32009-02-28 00:25:32 +0000499QualType
500TemplateTypeInstantiator::
501InstantiateObjCQualifiedIdType(const ObjCQualifiedIdType *T,
502 unsigned Quals) const {
503 assert(false && "Objective-C types cannot be dependent");
Douglas Gregor99ebf652009-02-27 19:31:52 +0000504 return QualType();
505}
506
Douglas Gregorcd281c32009-02-28 00:25:32 +0000507QualType
508TemplateTypeInstantiator::
509InstantiateObjCQualifiedClassType(const ObjCQualifiedClassType *T,
510 unsigned Quals) const {
511 assert(false && "Objective-C types cannot be dependent");
Douglas Gregor99ebf652009-02-27 19:31:52 +0000512 return QualType();
513}
514
Douglas Gregorcd281c32009-02-28 00:25:32 +0000515/// \brief The actual implementation of Sema::InstantiateType().
516QualType TemplateTypeInstantiator::Instantiate(QualType T) const {
517 // If T is not a dependent type, there is nothing to do.
518 if (!T->isDependentType())
519 return T;
520
521 switch (T->getTypeClass()) {
522#define TYPE(Class, Base) \
523 case Type::Class: \
524 return Instantiate##Class##Type(cast<Class##Type>(T.getTypePtr()), \
525 T.getCVRQualifiers());
526#define ABSTRACT_TYPE(Class, Base)
527#include "clang/AST/TypeNodes.def"
528 }
529
530 assert(false && "Not all types have been decoded for instantiation");
531 return QualType();
532}
Douglas Gregor99ebf652009-02-27 19:31:52 +0000533
534/// \brief Instantiate the type T with a given set of template arguments.
535///
536/// This routine substitutes the given template arguments into the
537/// type T and produces the instantiated type.
538///
539/// \param T the type into which the template arguments will be
540/// substituted. If this type is not dependent, it will be returned
541/// immediately.
542///
543/// \param TemplateArgs the template arguments that will be
544/// substituted for the top-level template parameters within T.
545///
546/// \param NumTemplateArgs the number of template arguments provided
547/// by TemplateArgs.
548///
549/// \param Loc the location in the source code where this substitution
550/// is being performed. It will typically be the location of the
551/// declarator (if we're instantiating the type of some declaration)
552/// or the location of the type in the source code (if, e.g., we're
553/// instantiating the type of a cast expression).
554///
555/// \param Entity the name of the entity associated with a declaration
556/// being instantiated (if any). May be empty to indicate that there
557/// is no such entity (if, e.g., this is a type that occurs as part of
558/// a cast expression) or that the entity has no name (e.g., an
559/// unnamed function parameter).
560///
561/// \returns If the instantiation succeeds, the instantiated
562/// type. Otherwise, produces diagnostics and returns a NULL type.
563QualType Sema::InstantiateType(QualType T,
564 const TemplateArgument *TemplateArgs,
565 unsigned NumTemplateArgs,
566 SourceLocation Loc, DeclarationName Entity) {
Douglas Gregordf667e72009-03-10 20:44:00 +0000567 assert(!ActiveTemplateInstantiations.empty() &&
568 "Cannot perform an instantiation without some context on the "
569 "instantiation stack");
570
Douglas Gregor99ebf652009-02-27 19:31:52 +0000571 // If T is not a dependent type, there is nothing to do.
572 if (!T->isDependentType())
573 return T;
574
Douglas Gregorcd281c32009-02-28 00:25:32 +0000575 TemplateTypeInstantiator Instantiator(*this, TemplateArgs, NumTemplateArgs,
576 Loc, Entity);
577 return Instantiator(T);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000578}
Douglas Gregor2943aed2009-03-03 04:44:36 +0000579
Douglas Gregora0e500d2009-03-12 16:53:44 +0000580//===----------------------------------------------------------------------===/
581// Template Instantiation for Expressions
582//===----------------------------------------------------------------------===/
Douglas Gregor313a81d2009-03-12 18:36:18 +0000583namespace {
584 class VISIBILITY_HIDDEN TemplateExprInstantiator
585 : public StmtVisitor<TemplateExprInstantiator, Sema::OwningExprResult> {
586 Sema &SemaRef;
587 const TemplateArgument *TemplateArgs;
588 unsigned NumTemplateArgs;
589
590 public:
Douglas Gregorba498172009-03-13 21:01:28 +0000591 typedef Sema::OwningExprResult OwningExprResult;
592
Douglas Gregor313a81d2009-03-12 18:36:18 +0000593 TemplateExprInstantiator(Sema &SemaRef,
594 const TemplateArgument *TemplateArgs,
595 unsigned NumTemplateArgs)
596 : SemaRef(SemaRef), TemplateArgs(TemplateArgs),
597 NumTemplateArgs(NumTemplateArgs) { }
598
599 // FIXME: Once we get closer to completion, replace these
600 // manually-written declarations with automatically-generated ones
601 // from clang/AST/StmtNodes.def.
Douglas Gregorba498172009-03-13 21:01:28 +0000602 OwningExprResult VisitIntegerLiteral(IntegerLiteral *E);
603 OwningExprResult VisitDeclRefExpr(DeclRefExpr *E);
604 OwningExprResult VisitParenExpr(ParenExpr *E);
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000605 OwningExprResult VisitUnaryOperator(UnaryOperator *E);
Douglas Gregorba498172009-03-13 21:01:28 +0000606 OwningExprResult VisitBinaryOperator(BinaryOperator *E);
607 OwningExprResult VisitCXXOperatorCallExpr(CXXOperatorCallExpr *E);
608 OwningExprResult VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E);
609 OwningExprResult VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *E);
Douglas Gregor313a81d2009-03-12 18:36:18 +0000610
611 // Base case. I'm supposed to ignore this.
Anders Carlssona135fb42009-03-15 18:34:13 +0000612 Sema::OwningExprResult VisitStmt(Stmt *S) {
613 S->dump();
Douglas Gregordf032512009-03-12 22:46:12 +0000614 assert(false && "Cannot instantiate this kind of expression");
615 return SemaRef.ExprError();
616 }
Douglas Gregor313a81d2009-03-12 18:36:18 +0000617 };
618}
619
620Sema::OwningExprResult
621TemplateExprInstantiator::VisitIntegerLiteral(IntegerLiteral *E) {
Anders Carlssona135fb42009-03-15 18:34:13 +0000622 return SemaRef.Clone(E);
Douglas Gregor313a81d2009-03-12 18:36:18 +0000623}
624
625Sema::OwningExprResult
626TemplateExprInstantiator::VisitDeclRefExpr(DeclRefExpr *E) {
627 Decl *D = E->getDecl();
628 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(D)) {
629 assert(NTTP->getDepth() == 0 && "No nested templates yet");
Douglas Gregorc971f862009-03-12 22:20:26 +0000630 const TemplateArgument &Arg = TemplateArgs[NTTP->getPosition()];
Douglas Gregor313a81d2009-03-12 18:36:18 +0000631 return SemaRef.Owned(new (SemaRef.Context) IntegerLiteral(
Douglas Gregorc971f862009-03-12 22:20:26 +0000632 *Arg.getAsIntegral(),
633 Arg.getIntegralType(),
634 E->getSourceRange().getBegin()));
Douglas Gregor313a81d2009-03-12 18:36:18 +0000635 } else
636 assert(false && "Can't handle arbitrary declaration references");
637
638 return SemaRef.ExprError();
639}
640
641Sema::OwningExprResult
642TemplateExprInstantiator::VisitParenExpr(ParenExpr *E) {
643 Sema::OwningExprResult SubExpr
644 = SemaRef.InstantiateExpr(E->getSubExpr(), TemplateArgs, NumTemplateArgs);
645 if (SubExpr.isInvalid())
646 return SemaRef.ExprError();
647
648 return SemaRef.Owned(new (SemaRef.Context) ParenExpr(
649 E->getLParen(), E->getRParen(),
650 (Expr *)SubExpr.release()));
651}
652
Douglas Gregora0e500d2009-03-12 16:53:44 +0000653Sema::OwningExprResult
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000654TemplateExprInstantiator::VisitUnaryOperator(UnaryOperator *E) {
655 Sema::OwningExprResult Arg = Visit(E->getSubExpr());
656 if (Arg.isInvalid())
657 return SemaRef.ExprError();
658
659 return SemaRef.CreateBuiltinUnaryOp(E->getOperatorLoc(),
660 E->getOpcode(),
661 move(Arg));
662}
663
664Sema::OwningExprResult
Douglas Gregordf032512009-03-12 22:46:12 +0000665TemplateExprInstantiator::VisitBinaryOperator(BinaryOperator *E) {
666 Sema::OwningExprResult LHS = Visit(E->getLHS());
667 if (LHS.isInvalid())
668 return SemaRef.ExprError();
669
670 Sema::OwningExprResult RHS = Visit(E->getRHS());
671 if (RHS.isInvalid())
672 return SemaRef.ExprError();
673
674 Sema::OwningExprResult Result
675 = SemaRef.CreateBuiltinBinOp(E->getOperatorLoc(),
676 E->getOpcode(),
677 (Expr *)LHS.get(),
678 (Expr *)RHS.get());
679 if (Result.isInvalid())
680 return SemaRef.ExprError();
681
682 LHS.release();
683 RHS.release();
684 return move(Result);
685}
686
687Sema::OwningExprResult
Douglas Gregor3fd95ce2009-03-13 00:33:25 +0000688TemplateExprInstantiator::VisitCXXOperatorCallExpr(CXXOperatorCallExpr *E) {
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000689 Sema::OwningExprResult First = Visit(E->getArg(0));
690 if (First.isInvalid())
Douglas Gregor3fd95ce2009-03-13 00:33:25 +0000691 return SemaRef.ExprError();
692
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000693 Expr *Args[2] = { (Expr *)First.get(), 0 };
Douglas Gregor3fd95ce2009-03-13 00:33:25 +0000694
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000695 Sema::OwningExprResult Second(SemaRef);
696 if (E->getNumArgs() == 2) {
697 Second = Visit(E->getArg(1));
698
699 if (Second.isInvalid())
700 return SemaRef.ExprError();
701
702 Args[1] = (Expr *)Second.get();
703 }
Douglas Gregor063daf62009-03-13 18:40:31 +0000704
705 if (!E->isTypeDependent()) {
706 // Since our original expression was not type-dependent, we do not
707 // perform lookup again at instantiation time (C++ [temp.dep]p1).
708 // Instead, we just build the new overloaded operator call
709 // expression.
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000710 First.release();
711 Second.release();
Douglas Gregor063daf62009-03-13 18:40:31 +0000712 return SemaRef.Owned(new (SemaRef.Context) CXXOperatorCallExpr(
713 SemaRef.Context,
714 E->getOperator(),
715 E->getCallee(),
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000716 Args, E->getNumArgs(),
717 E->getType(),
Douglas Gregor063daf62009-03-13 18:40:31 +0000718 E->getOperatorLoc()));
719 }
720
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000721 bool isPostIncDec = E->getNumArgs() == 2 &&
722 (E->getOperator() == OO_PlusPlus || E->getOperator() == OO_MinusMinus);
723 if (E->getNumArgs() == 1 || isPostIncDec) {
724 if (!Args[0]->getType()->isOverloadableType()) {
725 // The argument is not of overloadable type, so try to create a
726 // built-in unary operation.
727 UnaryOperator::Opcode Opc
728 = UnaryOperator::getOverloadedOpcode(E->getOperator(), isPostIncDec);
729
730 return SemaRef.CreateBuiltinUnaryOp(E->getOperatorLoc(), Opc,
731 move(First));
732 }
733
734 // Fall through to perform overload resolution
735 } else {
736 assert(E->getNumArgs() == 2 && "Expected binary operation");
737
738 Sema::OwningExprResult Result(SemaRef);
739 if (!Args[0]->getType()->isOverloadableType() &&
740 !Args[1]->getType()->isOverloadableType()) {
741 // Neither of the arguments is an overloadable type, so try to
742 // create a built-in binary operation.
743 BinaryOperator::Opcode Opc =
744 BinaryOperator::getOverloadedOpcode(E->getOperator());
745 Result = SemaRef.CreateBuiltinBinOp(E->getOperatorLoc(), Opc,
746 Args[0], Args[1]);
747 if (Result.isInvalid())
748 return SemaRef.ExprError();
749
750 First.release();
751 Second.release();
752 return move(Result);
753 }
754
755 // Fall through to perform overload resolution.
756 }
757
758 // Compute the set of functions that were found at template
759 // definition time.
760 Sema::FunctionSet Functions;
761 DeclRefExpr *DRE = cast<DeclRefExpr>(E->getCallee());
762 OverloadedFunctionDecl *Overloads
763 = cast<OverloadedFunctionDecl>(DRE->getDecl());
764
765 // FIXME: Do we have to check
766 // IsAcceptableNonMemberOperatorCandidate for each of these?
767 for (OverloadedFunctionDecl::function_iterator
768 F = Overloads->function_begin(),
769 FEnd = Overloads->function_end();
770 F != FEnd; ++F)
771 Functions.insert(*F);
772
773 // Add any functions found via argument-dependent lookup.
774 DeclarationName OpName
775 = SemaRef.Context.DeclarationNames.getCXXOperatorName(E->getOperator());
776 SemaRef.ArgumentDependentLookup(OpName, Args, E->getNumArgs(), Functions);
777
778 // Create the overloaded operator invocation.
779 if (E->getNumArgs() == 1 || isPostIncDec) {
780 UnaryOperator::Opcode Opc
781 = UnaryOperator::getOverloadedOpcode(E->getOperator(), isPostIncDec);
782 return SemaRef.CreateOverloadedUnaryOp(E->getOperatorLoc(), Opc,
783 Functions, move(First));
784 }
785
786 // FIXME: This would be far less ugly if CreateOverloadedBinOp took
787 // in ExprArg arguments!
Douglas Gregor063daf62009-03-13 18:40:31 +0000788 BinaryOperator::Opcode Opc =
789 BinaryOperator::getOverloadedOpcode(E->getOperator());
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000790 OwningExprResult Result
791 = SemaRef.CreateOverloadedBinOp(E->getOperatorLoc(), Opc,
792 Functions, Args[0], Args[1]);
Douglas Gregor063daf62009-03-13 18:40:31 +0000793
Douglas Gregor3fd95ce2009-03-13 00:33:25 +0000794 if (Result.isInvalid())
795 return SemaRef.ExprError();
796
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000797 First.release();
798 Second.release();
Douglas Gregor3fd95ce2009-03-13 00:33:25 +0000799 return move(Result);
800}
801
802Sema::OwningExprResult
Douglas Gregorba498172009-03-13 21:01:28 +0000803TemplateExprInstantiator::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E) {
804 bool isSizeOf = E->isSizeOf();
805
806 if (E->isArgumentType()) {
807 QualType T = E->getArgumentType();
808 if (T->isDependentType()) {
809 T = SemaRef.InstantiateType(T, TemplateArgs, NumTemplateArgs,
810 /*FIXME*/E->getOperatorLoc(),
811 &SemaRef.PP.getIdentifierTable().get("sizeof"));
812 if (T.isNull())
813 return SemaRef.ExprError();
814 }
815
816 return SemaRef.CreateSizeOfAlignOfExpr(T, E->getOperatorLoc(), isSizeOf,
817 E->getSourceRange());
818 }
819
820 Sema::OwningExprResult Arg = Visit(E->getArgumentExpr());
821 if (Arg.isInvalid())
822 return SemaRef.ExprError();
823
824 Sema::OwningExprResult Result
825 = SemaRef.CreateSizeOfAlignOfExpr((Expr *)Arg.get(), E->getOperatorLoc(),
826 isSizeOf, E->getSourceRange());
827 if (Result.isInvalid())
828 return SemaRef.ExprError();
829
830 Arg.release();
831 return move(Result);
832}
833
834Sema::OwningExprResult
835TemplateExprInstantiator::VisitCXXTemporaryObjectExpr(
836 CXXTemporaryObjectExpr *E) {
837 QualType T = E->getType();
838 if (T->isDependentType()) {
839 T = SemaRef.InstantiateType(T, TemplateArgs, NumTemplateArgs,
840 E->getTypeBeginLoc(), DeclarationName());
841 if (T.isNull())
842 return SemaRef.ExprError();
843 }
844
845 llvm::SmallVector<Expr *, 16> Args;
846 Args.reserve(E->getNumArgs());
847 bool Invalid = false;
848 for (CXXTemporaryObjectExpr::arg_iterator Arg = E->arg_begin(),
849 ArgEnd = E->arg_end();
850 Arg != ArgEnd; ++Arg) {
851 OwningExprResult InstantiatedArg = Visit(*Arg);
852 if (InstantiatedArg.isInvalid()) {
853 Invalid = true;
854 break;
855 }
856
857 Args.push_back((Expr *)InstantiatedArg.release());
858 }
859
860 if (!Invalid) {
861 SourceLocation CommaLoc;
862 // FIXME: HACK!
863 if (Args.size() > 1)
864 CommaLoc
865 = SemaRef.PP.getLocForEndOfToken(Args[0]->getSourceRange().getEnd());
Sebastian Redlf53597f2009-03-15 17:47:39 +0000866 Sema::OwningExprResult Result(
867 SemaRef.ActOnCXXTypeConstructExpr(SourceRange(E->getTypeBeginLoc()
868 /*, FIXME*/),
869 T.getAsOpaquePtr(),
870 /*FIXME*/E->getTypeBeginLoc(),
871 Sema::MultiExprArg(SemaRef,
872 (void**)&Args[0],
873 Args.size()),
874 /*HACK*/&CommaLoc,
875 E->getSourceRange().getEnd()));
876 // At this point, Args no longer owns the arguments, no matter what.
877 return move(Result);
Douglas Gregorba498172009-03-13 21:01:28 +0000878 }
879
880 // Clean up the instantiated arguments.
881 // FIXME: Would rather do this with RAII.
882 for (unsigned Idx = 0; Idx < Args.size(); ++Idx)
883 SemaRef.DeleteExpr(Args[Idx]);
884
885 return SemaRef.ExprError();
886}
887
888Sema::OwningExprResult
Douglas Gregora0e500d2009-03-12 16:53:44 +0000889Sema::InstantiateExpr(Expr *E, const TemplateArgument *TemplateArgs,
890 unsigned NumTemplateArgs) {
Douglas Gregor313a81d2009-03-12 18:36:18 +0000891 TemplateExprInstantiator Instantiator(*this, TemplateArgs, NumTemplateArgs);
892 return Instantiator.Visit(E);
Douglas Gregora0e500d2009-03-12 16:53:44 +0000893}
894
Douglas Gregor2943aed2009-03-03 04:44:36 +0000895/// \brief Instantiate the base class specifiers of the given class
896/// template specialization.
897///
898/// Produces a diagnostic and returns true on error, returns false and
899/// attaches the instantiated base classes to the class template
900/// specialization if successful.
901bool
902Sema::InstantiateBaseSpecifiers(
903 ClassTemplateSpecializationDecl *ClassTemplateSpec,
904 ClassTemplateDecl *ClassTemplate) {
905 bool Invalid = false;
906 llvm::SmallVector<CXXBaseSpecifier*, 8> InstantiatedBases;
907 for (ClassTemplateSpecializationDecl::base_class_iterator
908 Base = ClassTemplate->getTemplatedDecl()->bases_begin(),
909 BaseEnd = ClassTemplate->getTemplatedDecl()->bases_end();
Douglas Gregor27b152f2009-03-10 18:52:44 +0000910 Base != BaseEnd; ++Base) {
Douglas Gregor2943aed2009-03-03 04:44:36 +0000911 if (!Base->getType()->isDependentType()) {
912 // FIXME: Allocate via ASTContext
913 InstantiatedBases.push_back(new CXXBaseSpecifier(*Base));
914 continue;
915 }
916
917 QualType BaseType = InstantiateType(Base->getType(),
918 ClassTemplateSpec->getTemplateArgs(),
919 ClassTemplateSpec->getNumTemplateArgs(),
920 Base->getSourceRange().getBegin(),
921 DeclarationName());
922 if (BaseType.isNull()) {
923 Invalid = true;
924 continue;
925 }
926
927 if (CXXBaseSpecifier *InstantiatedBase
928 = CheckBaseSpecifier(ClassTemplateSpec,
929 Base->getSourceRange(),
930 Base->isVirtual(),
931 Base->getAccessSpecifierAsWritten(),
932 BaseType,
933 /*FIXME: Not totally accurate */
934 Base->getSourceRange().getBegin()))
935 InstantiatedBases.push_back(InstantiatedBase);
936 else
937 Invalid = true;
938 }
939
Douglas Gregor27b152f2009-03-10 18:52:44 +0000940 if (!Invalid &&
941 AttachBaseSpecifiers(ClassTemplateSpec, &InstantiatedBases[0],
Douglas Gregor2943aed2009-03-03 04:44:36 +0000942 InstantiatedBases.size()))
943 Invalid = true;
944
945 return Invalid;
946}
947
948bool
949Sema::InstantiateClassTemplateSpecialization(
950 ClassTemplateSpecializationDecl *ClassTemplateSpec,
951 bool ExplicitInstantiation) {
952 // Perform the actual instantiation on the canonical declaration.
953 ClassTemplateSpec = cast<ClassTemplateSpecializationDecl>(
954 Context.getCanonicalDecl(ClassTemplateSpec));
955
956 // We can only instantiate something that hasn't already been
957 // instantiated or specialized. Fail without any diagnostics: our
958 // caller will provide an error message.
959 if (ClassTemplateSpec->getSpecializationKind() != TSK_Undeclared)
960 return true;
961
962 // FIXME: Push this class template instantiation onto the
963 // instantiation stack, checking for recursion that exceeds a
964 // certain depth.
965
966 // FIXME: Perform class template partial specialization to select
967 // the best template.
968 ClassTemplateDecl *Template = ClassTemplateSpec->getSpecializedTemplate();
969
970 if (!Template->getTemplatedDecl()->getDefinition(Context)) {
971 Diag(ClassTemplateSpec->getLocation(),
972 diag::err_template_implicit_instantiate_undefined)
973 << Context.getTypeDeclType(ClassTemplateSpec);
974 Diag(Template->getTemplatedDecl()->getLocation(),
975 diag::note_template_decl_here);
976 return true;
977 }
978
979 // Note that this is an instantiation.
980 ClassTemplateSpec->setSpecializationKind(
981 ExplicitInstantiation? TSK_ExplicitInstantiation
982 : TSK_ImplicitInstantiation);
983
984
985 bool Invalid = false;
986
Douglas Gregor26dce442009-03-10 00:06:19 +0000987 InstantiatingTemplate Inst(*this, ClassTemplateSpec->getLocation(),
988 ClassTemplateSpec);
989 if (Inst)
990 return true;
991
Douglas Gregor2943aed2009-03-03 04:44:36 +0000992 // Enter the scope of this instantiation. We don't use
993 // PushDeclContext because we don't have a scope.
994 DeclContext *PreviousContext = CurContext;
995 CurContext = ClassTemplateSpec;
996
997 // Start the definition of this instantiation.
998 ClassTemplateSpec->startDefinition();
999
Douglas Gregor2943aed2009-03-03 04:44:36 +00001000
1001 // Instantiate the base class specifiers.
1002 if (InstantiateBaseSpecifiers(ClassTemplateSpec, Template))
1003 Invalid = true;
1004
Douglas Gregor4fdf1fa2009-03-11 16:48:53 +00001005 // FIXME: Create the injected-class-name for the
1006 // instantiation. Should this be a typedef or something like it?
1007
1008 RecordDecl *Pattern = Template->getTemplatedDecl();
Douglas Gregor3cf538d2009-03-11 18:59:21 +00001009 llvm::SmallVector<DeclTy *, 32> Fields;
Douglas Gregor4fdf1fa2009-03-11 16:48:53 +00001010 for (RecordDecl::decl_iterator Member = Pattern->decls_begin(),
1011 MemberEnd = Pattern->decls_end();
1012 Member != MemberEnd; ++Member) {
1013 if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(*Member)) {
1014 // FIXME: Simplified instantiation of typedefs needs to be made
1015 // "real".
1016 QualType T = Typedef->getUnderlyingType();
1017 if (T->isDependentType()) {
1018 T = InstantiateType(T, ClassTemplateSpec->getTemplateArgs(),
1019 ClassTemplateSpec->getNumTemplateArgs(),
1020 Typedef->getLocation(),
1021 Typedef->getDeclName());
1022 if (T.isNull()) {
1023 Invalid = true;
1024 T = Context.IntTy;
1025 }
1026 }
1027
1028 // Create the new typedef
1029 TypedefDecl *New
1030 = TypedefDecl::Create(Context, ClassTemplateSpec,
1031 Typedef->getLocation(),
1032 Typedef->getIdentifier(),
1033 T);
1034 ClassTemplateSpec->addDecl(New);
Douglas Gregor3cf538d2009-03-11 18:59:21 +00001035 }
1036 else if (FieldDecl *Field = dyn_cast<FieldDecl>(*Member)) {
1037 // FIXME: Simplified instantiation of fields needs to be made
1038 // "real".
Douglas Gregora0e500d2009-03-12 16:53:44 +00001039 bool InvalidDecl = false;
Douglas Gregor3cf538d2009-03-11 18:59:21 +00001040 QualType T = Field->getType();
1041 if (T->isDependentType()) {
1042 T = InstantiateType(T, ClassTemplateSpec->getTemplateArgs(),
1043 ClassTemplateSpec->getNumTemplateArgs(),
1044 Field->getLocation(),
1045 Field->getDeclName());
1046 if (!T.isNull() && T->isFunctionType()) {
1047 // C++ [temp.arg.type]p3:
1048 // If a declaration acquires a function type through a type
1049 // dependent on a template-parameter and this causes a
1050 // declaration that does not use the syntactic form of a
1051 // function declarator to have function type, the program is
1052 // ill-formed.
1053 Diag(Field->getLocation(), diag::err_field_instantiates_to_function)
1054 << T;
1055 T = QualType();
Douglas Gregora0e500d2009-03-12 16:53:44 +00001056 InvalidDecl = true;
Douglas Gregor3cf538d2009-03-11 18:59:21 +00001057 }
1058 }
1059
Douglas Gregora0e500d2009-03-12 16:53:44 +00001060 Expr *BitWidth = Field->getBitWidth();
1061 if (InvalidDecl)
1062 BitWidth = 0;
Douglas Gregor3e287c22009-03-15 17:43:26 +00001063 else if (BitWidth) {
Douglas Gregora0e500d2009-03-12 16:53:44 +00001064 OwningExprResult InstantiatedBitWidth
1065 = InstantiateExpr(BitWidth,
1066 ClassTemplateSpec->getTemplateArgs(),
1067 ClassTemplateSpec->getNumTemplateArgs());
1068 if (InstantiatedBitWidth.isInvalid()) {
1069 Invalid = InvalidDecl = true;
1070 BitWidth = 0;
1071 } else
1072 BitWidth = (Expr *)InstantiatedBitWidth.release();
1073 }
1074
Douglas Gregor3cf538d2009-03-11 18:59:21 +00001075 FieldDecl *New = CheckFieldDecl(Field->getDeclName(), T,
1076 ClassTemplateSpec,
1077 Field->getLocation(),
1078 Field->isMutable(),
Douglas Gregora0e500d2009-03-12 16:53:44 +00001079 BitWidth,
Douglas Gregor4dd55f52009-03-11 20:50:30 +00001080 Field->getAccess(),
Douglas Gregor3cf538d2009-03-11 18:59:21 +00001081 0);
1082 if (New) {
1083 ClassTemplateSpec->addDecl(New);
1084 Fields.push_back(New);
1085
Douglas Gregora0e500d2009-03-12 16:53:44 +00001086 if (InvalidDecl)
1087 New->setInvalidDecl();
1088
Douglas Gregor3cf538d2009-03-11 18:59:21 +00001089 if (New->isInvalidDecl())
1090 Invalid = true;
1091 }
Anders Carlsson94b15fb2009-03-15 18:44:04 +00001092 } else if (StaticAssertDecl *SA = dyn_cast<StaticAssertDecl>(*Member)) {
1093 Expr *AssertExpr = SA->getAssertExpr();
1094
1095 OwningExprResult InstantiatedAssertExpr
1096 = InstantiateExpr(AssertExpr,
1097 ClassTemplateSpec->getTemplateArgs(),
1098 ClassTemplateSpec->getNumTemplateArgs());
1099 if (!InstantiatedAssertExpr.isInvalid()) {
1100 OwningExprResult Message = Clone(SA->getMessage());
1101
1102 Decl *New =
1103 (Decl *)ActOnStaticAssertDeclaration(SA->getLocation(),
1104 move(InstantiatedAssertExpr),
1105 move(Message));
1106 if (New->isInvalidDecl())
1107 Invalid = true;
1108
1109 } else
1110 Invalid = true;
Douglas Gregor4fdf1fa2009-03-11 16:48:53 +00001111 }
1112 }
1113
Douglas Gregor3cf538d2009-03-11 18:59:21 +00001114 // Finish checking fields.
1115 ActOnFields(0, ClassTemplateSpec->getLocation(), ClassTemplateSpec,
1116 &Fields[0], Fields.size(), SourceLocation(), SourceLocation(),
1117 0);
1118
Douglas Gregor2943aed2009-03-03 04:44:36 +00001119 // Add any implicitly-declared members that we might need.
1120 AddImplicitlyDeclaredMembersToClass(ClassTemplateSpec);
1121
Douglas Gregor2943aed2009-03-03 04:44:36 +00001122 // Exit the scope of this instantiation.
1123 CurContext = PreviousContext;
1124
1125 return Invalid;
1126}