blob: 1cc999201bb723c4fcaad6e038ed57fecde1a83d [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"
Douglas Gregoraba43bb2009-05-26 20:50:29 +000014#include "clang/AST/ASTConsumer.h"
Douglas Gregor99ebf652009-02-27 19:31:52 +000015#include "clang/AST/ASTContext.h"
16#include "clang/AST/Expr.h"
Douglas Gregor99ebf652009-02-27 19:31:52 +000017#include "clang/AST/DeclTemplate.h"
18#include "clang/Parse/DeclSpec.h"
19#include "clang/Basic/LangOptions.h"
Douglas Gregorcd281c32009-02-28 00:25:32 +000020#include "llvm/Support/Compiler.h"
Douglas Gregor99ebf652009-02-27 19:31:52 +000021
22using namespace clang;
23
Douglas Gregoree1828a2009-03-10 18:03:33 +000024//===----------------------------------------------------------------------===/
25// Template Instantiation Support
26//===----------------------------------------------------------------------===/
27
Douglas Gregor54dabfc2009-05-14 23:26:13 +000028/// \brief Retrieve the template argument list that should be used to
29/// instantiate the given declaration.
30const TemplateArgumentList &
31Sema::getTemplateInstantiationArgs(NamedDecl *D) {
32 if (ClassTemplateSpecializationDecl *Spec
33 = dyn_cast<ClassTemplateSpecializationDecl>(D))
34 return Spec->getTemplateArgs();
35
36 DeclContext *EnclosingTemplateCtx = D->getDeclContext();
37 while (!isa<ClassTemplateSpecializationDecl>(EnclosingTemplateCtx)) {
38 assert(!EnclosingTemplateCtx->isFileContext() &&
39 "Tried to get the instantiation arguments of a non-template");
40 EnclosingTemplateCtx = EnclosingTemplateCtx->getParent();
41 }
42
43 ClassTemplateSpecializationDecl *EnclosingTemplate
44 = cast<ClassTemplateSpecializationDecl>(EnclosingTemplateCtx);
45 return EnclosingTemplate->getTemplateArgs();
46}
47
Douglas Gregor26dce442009-03-10 00:06:19 +000048Sema::InstantiatingTemplate::
49InstantiatingTemplate(Sema &SemaRef, SourceLocation PointOfInstantiation,
Douglas Gregorf3e7ce42009-05-18 17:01:57 +000050 Decl *Entity,
Douglas Gregor26dce442009-03-10 00:06:19 +000051 SourceRange InstantiationRange)
52 : SemaRef(SemaRef) {
Douglas Gregordf667e72009-03-10 20:44:00 +000053
54 Invalid = CheckInstantiationDepth(PointOfInstantiation,
55 InstantiationRange);
56 if (!Invalid) {
Douglas Gregor26dce442009-03-10 00:06:19 +000057 ActiveTemplateInstantiation Inst;
Douglas Gregordf667e72009-03-10 20:44:00 +000058 Inst.Kind = ActiveTemplateInstantiation::TemplateInstantiation;
Douglas Gregor26dce442009-03-10 00:06:19 +000059 Inst.PointOfInstantiation = PointOfInstantiation;
Douglas Gregordf667e72009-03-10 20:44:00 +000060 Inst.Entity = reinterpret_cast<uintptr_t>(Entity);
Douglas Gregor313a81d2009-03-12 18:36:18 +000061 Inst.TemplateArgs = 0;
62 Inst.NumTemplateArgs = 0;
Douglas Gregordf667e72009-03-10 20:44:00 +000063 Inst.InstantiationRange = InstantiationRange;
64 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
65 Invalid = false;
66 }
67}
68
69Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef,
70 SourceLocation PointOfInstantiation,
71 TemplateDecl *Template,
72 const TemplateArgument *TemplateArgs,
73 unsigned NumTemplateArgs,
74 SourceRange InstantiationRange)
75 : SemaRef(SemaRef) {
76
77 Invalid = CheckInstantiationDepth(PointOfInstantiation,
78 InstantiationRange);
79 if (!Invalid) {
80 ActiveTemplateInstantiation Inst;
81 Inst.Kind
82 = ActiveTemplateInstantiation::DefaultTemplateArgumentInstantiation;
83 Inst.PointOfInstantiation = PointOfInstantiation;
84 Inst.Entity = reinterpret_cast<uintptr_t>(Template);
85 Inst.TemplateArgs = TemplateArgs;
86 Inst.NumTemplateArgs = NumTemplateArgs;
Douglas Gregor26dce442009-03-10 00:06:19 +000087 Inst.InstantiationRange = InstantiationRange;
88 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
89 Invalid = false;
90 }
91}
92
Douglas Gregorf3e7ce42009-05-18 17:01:57 +000093void Sema::InstantiatingTemplate::Clear() {
94 if (!Invalid) {
Douglas Gregor26dce442009-03-10 00:06:19 +000095 SemaRef.ActiveTemplateInstantiations.pop_back();
Douglas Gregorf3e7ce42009-05-18 17:01:57 +000096 Invalid = true;
97 }
Douglas Gregor26dce442009-03-10 00:06:19 +000098}
99
Douglas Gregordf667e72009-03-10 20:44:00 +0000100bool Sema::InstantiatingTemplate::CheckInstantiationDepth(
101 SourceLocation PointOfInstantiation,
102 SourceRange InstantiationRange) {
103 if (SemaRef.ActiveTemplateInstantiations.size()
104 <= SemaRef.getLangOptions().InstantiationDepth)
105 return false;
106
107 SemaRef.Diag(PointOfInstantiation,
108 diag::err_template_recursion_depth_exceeded)
109 << SemaRef.getLangOptions().InstantiationDepth
110 << InstantiationRange;
111 SemaRef.Diag(PointOfInstantiation, diag::note_template_recursion_depth)
112 << SemaRef.getLangOptions().InstantiationDepth;
113 return true;
114}
115
Douglas Gregoree1828a2009-03-10 18:03:33 +0000116/// \brief Prints the current instantiation stack through a series of
117/// notes.
118void Sema::PrintInstantiationStack() {
119 for (llvm::SmallVector<ActiveTemplateInstantiation, 16>::reverse_iterator
120 Active = ActiveTemplateInstantiations.rbegin(),
121 ActiveEnd = ActiveTemplateInstantiations.rend();
122 Active != ActiveEnd;
123 ++Active) {
Douglas Gregordf667e72009-03-10 20:44:00 +0000124 switch (Active->Kind) {
125 case ActiveTemplateInstantiation::TemplateInstantiation: {
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000126 Decl *D = reinterpret_cast<Decl *>(Active->Entity);
127 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) {
128 unsigned DiagID = diag::note_template_member_class_here;
129 if (isa<ClassTemplateSpecializationDecl>(Record))
130 DiagID = diag::note_template_class_instantiation_here;
131 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
132 DiagID)
133 << Context.getTypeDeclType(Record)
134 << Active->InstantiationRange;
135 } else {
136 FunctionDecl *Function = cast<FunctionDecl>(D);
137 unsigned DiagID = diag::note_template_member_function_here;
138 // FIXME: check for a function template
139 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
140 DiagID)
141 << Function
142 << Active->InstantiationRange;
143 }
Douglas Gregordf667e72009-03-10 20:44:00 +0000144 break;
145 }
146
147 case ActiveTemplateInstantiation::DefaultTemplateArgumentInstantiation: {
148 TemplateDecl *Template = cast<TemplateDecl>((Decl *)Active->Entity);
149 std::string TemplateArgsStr
Douglas Gregor7532dc62009-03-30 22:58:21 +0000150 = TemplateSpecializationType::PrintTemplateArgumentList(
Douglas Gregordf667e72009-03-10 20:44:00 +0000151 Active->TemplateArgs,
152 Active->NumTemplateArgs);
153 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
154 diag::note_default_arg_instantiation_here)
155 << (Template->getNameAsString() + TemplateArgsStr)
156 << Active->InstantiationRange;
157 break;
158 }
159 }
Douglas Gregoree1828a2009-03-10 18:03:33 +0000160 }
161}
162
Douglas Gregor99ebf652009-02-27 19:31:52 +0000163//===----------------------------------------------------------------------===/
164// Template Instantiation for Types
165//===----------------------------------------------------------------------===/
Douglas Gregorcd281c32009-02-28 00:25:32 +0000166namespace {
167 class VISIBILITY_HIDDEN TemplateTypeInstantiator {
168 Sema &SemaRef;
Douglas Gregor7e063902009-05-11 23:53:27 +0000169 const TemplateArgumentList &TemplateArgs;
Douglas Gregorcd281c32009-02-28 00:25:32 +0000170 SourceLocation Loc;
171 DeclarationName Entity;
Douglas Gregor99ebf652009-02-27 19:31:52 +0000172
Douglas Gregorcd281c32009-02-28 00:25:32 +0000173 public:
174 TemplateTypeInstantiator(Sema &SemaRef,
Douglas Gregor7e063902009-05-11 23:53:27 +0000175 const TemplateArgumentList &TemplateArgs,
Douglas Gregorcd281c32009-02-28 00:25:32 +0000176 SourceLocation Loc,
177 DeclarationName Entity)
178 : SemaRef(SemaRef), TemplateArgs(TemplateArgs),
Douglas Gregor7e063902009-05-11 23:53:27 +0000179 Loc(Loc), Entity(Entity) { }
Douglas Gregorcd281c32009-02-28 00:25:32 +0000180
181 QualType operator()(QualType T) const { return Instantiate(T); }
182
183 QualType Instantiate(QualType T) const;
184
185 // Declare instantiate functions for each type.
186#define TYPE(Class, Base) \
187 QualType Instantiate##Class##Type(const Class##Type *T, \
188 unsigned Quals) const;
189#define ABSTRACT_TYPE(Class, Base)
190#include "clang/AST/TypeNodes.def"
191 };
192}
193
194QualType
195TemplateTypeInstantiator::InstantiateExtQualType(const ExtQualType *T,
196 unsigned Quals) const {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000197 // FIXME: Implement this
198 assert(false && "Cannot instantiate ExtQualType yet");
199 return QualType();
200}
201
Douglas Gregorcd281c32009-02-28 00:25:32 +0000202QualType
203TemplateTypeInstantiator::InstantiateBuiltinType(const BuiltinType *T,
204 unsigned Quals) const {
Douglas Gregor724651c2009-02-28 01:04:19 +0000205 assert(false && "Builtin types are not dependent and cannot be instantiated");
Douglas Gregorcd281c32009-02-28 00:25:32 +0000206 return QualType(T, Quals);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000207}
208
Douglas Gregorcd281c32009-02-28 00:25:32 +0000209QualType
210TemplateTypeInstantiator::
211InstantiateFixedWidthIntType(const FixedWidthIntType *T, unsigned Quals) const {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000212 // FIXME: Implement this
213 assert(false && "Cannot instantiate FixedWidthIntType yet");
214 return QualType();
215}
216
Douglas Gregorcd281c32009-02-28 00:25:32 +0000217QualType
218TemplateTypeInstantiator::InstantiateComplexType(const ComplexType *T,
219 unsigned Quals) const {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000220 // FIXME: Implement this
221 assert(false && "Cannot instantiate ComplexType yet");
222 return QualType();
223}
224
Douglas Gregorcd281c32009-02-28 00:25:32 +0000225QualType
226TemplateTypeInstantiator::InstantiatePointerType(const PointerType *T,
227 unsigned Quals) const {
228 QualType PointeeType = Instantiate(T->getPointeeType());
229 if (PointeeType.isNull())
230 return QualType();
231
232 return SemaRef.BuildPointerType(PointeeType, Quals, Loc, Entity);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000233}
234
Douglas Gregorcd281c32009-02-28 00:25:32 +0000235QualType
236TemplateTypeInstantiator::InstantiateBlockPointerType(const BlockPointerType *T,
237 unsigned Quals) const {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000238 // FIXME: Implement this
239 assert(false && "Cannot instantiate BlockPointerType yet");
240 return QualType();
241}
242
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000243QualType
244TemplateTypeInstantiator::InstantiateLValueReferenceType(
245 const LValueReferenceType *T, unsigned Quals) const {
Douglas Gregorcd281c32009-02-28 00:25:32 +0000246 QualType ReferentType = Instantiate(T->getPointeeType());
247 if (ReferentType.isNull())
248 return QualType();
249
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000250 return SemaRef.BuildReferenceType(ReferentType, true, Quals, Loc, Entity);
251}
252
253QualType
254TemplateTypeInstantiator::InstantiateRValueReferenceType(
255 const RValueReferenceType *T, unsigned Quals) const {
256 QualType ReferentType = Instantiate(T->getPointeeType());
257 if (ReferentType.isNull())
258 return QualType();
259
260 return SemaRef.BuildReferenceType(ReferentType, false, Quals, Loc, Entity);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000261}
262
Douglas Gregorcd281c32009-02-28 00:25:32 +0000263QualType
264TemplateTypeInstantiator::
265InstantiateMemberPointerType(const MemberPointerType *T,
266 unsigned Quals) const {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000267 // FIXME: Implement this
268 assert(false && "Cannot instantiate MemberPointerType yet");
269 return QualType();
270}
271
Douglas Gregorcd281c32009-02-28 00:25:32 +0000272QualType
273TemplateTypeInstantiator::
274InstantiateConstantArrayType(const ConstantArrayType *T,
275 unsigned Quals) const {
276 QualType ElementType = Instantiate(T->getElementType());
277 if (ElementType.isNull())
278 return ElementType;
279
280 // Build a temporary integer literal to specify the size for
281 // BuildArrayType. Since we have already checked the size as part of
282 // creating the dependent array type in the first place, we know
Douglas Gregorff668032009-05-13 18:28:20 +0000283 // there aren't any errors. However, we do need to determine what
284 // C++ type to give the size expression.
285 llvm::APInt Size = T->getSize();
286 QualType Types[] = {
287 SemaRef.Context.UnsignedCharTy, SemaRef.Context.UnsignedShortTy,
288 SemaRef.Context.UnsignedIntTy, SemaRef.Context.UnsignedLongTy,
289 SemaRef.Context.UnsignedLongLongTy, SemaRef.Context.UnsignedInt128Ty
290 };
291 const unsigned NumTypes = sizeof(Types) / sizeof(QualType);
292 QualType SizeType;
293 for (unsigned I = 0; I != NumTypes; ++I)
294 if (Size.getBitWidth() == SemaRef.Context.getIntWidth(Types[I])) {
295 SizeType = Types[I];
296 break;
297 }
298
299 if (SizeType.isNull())
300 SizeType = SemaRef.Context.getFixedWidthIntType(Size.getBitWidth(), false);
301
302 IntegerLiteral ArraySize(Size, SizeType, Loc);
Douglas Gregorcd281c32009-02-28 00:25:32 +0000303 return SemaRef.BuildArrayType(ElementType, T->getSizeModifier(),
304 &ArraySize, T->getIndexTypeQualifier(),
305 Loc, Entity);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000306}
307
Douglas Gregorcd281c32009-02-28 00:25:32 +0000308QualType
309TemplateTypeInstantiator::
310InstantiateIncompleteArrayType(const IncompleteArrayType *T,
311 unsigned Quals) const {
312 QualType ElementType = Instantiate(T->getElementType());
313 if (ElementType.isNull())
314 return ElementType;
315
316 return SemaRef.BuildArrayType(ElementType, T->getSizeModifier(),
317 0, T->getIndexTypeQualifier(),
318 Loc, Entity);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000319}
320
Douglas Gregorcd281c32009-02-28 00:25:32 +0000321QualType
322TemplateTypeInstantiator::
323InstantiateVariableArrayType(const VariableArrayType *T,
324 unsigned Quals) const {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000325 // FIXME: Implement this
326 assert(false && "Cannot instantiate VariableArrayType yet");
327 return QualType();
328}
329
Douglas Gregorcd281c32009-02-28 00:25:32 +0000330QualType
331TemplateTypeInstantiator::
332InstantiateDependentSizedArrayType(const DependentSizedArrayType *T,
333 unsigned Quals) const {
Anders Carlsson76b1c842009-03-15 20:12:13 +0000334 Expr *ArraySize = T->getSizeExpr();
335 assert(ArraySize->isValueDependent() &&
336 "dependent sized array types must have value dependent size expr");
337
338 // Instantiate the element type if needed
339 QualType ElementType = T->getElementType();
340 if (ElementType->isDependentType()) {
341 ElementType = Instantiate(ElementType);
342 if (ElementType.isNull())
343 return QualType();
344 }
345
346 // Instantiate the size expression
347 Sema::OwningExprResult InstantiatedArraySize =
Douglas Gregor7e063902009-05-11 23:53:27 +0000348 SemaRef.InstantiateExpr(ArraySize, TemplateArgs);
Anders Carlsson76b1c842009-03-15 20:12:13 +0000349 if (InstantiatedArraySize.isInvalid())
350 return QualType();
351
352 return SemaRef.BuildArrayType(ElementType, T->getSizeModifier(),
Anders Carlssone9146f22009-05-01 19:49:17 +0000353 InstantiatedArraySize.takeAs<Expr>(),
Anders Carlsson76b1c842009-03-15 20:12:13 +0000354 T->getIndexTypeQualifier(), Loc, Entity);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000355}
356
Douglas Gregorcd281c32009-02-28 00:25:32 +0000357QualType
358TemplateTypeInstantiator::InstantiateVectorType(const VectorType *T,
359 unsigned Quals) const {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000360 // FIXME: Implement this
361 assert(false && "Cannot instantiate VectorType yet");
362 return QualType();
363}
364
Douglas Gregorcd281c32009-02-28 00:25:32 +0000365QualType
366TemplateTypeInstantiator::InstantiateExtVectorType(const ExtVectorType *T,
367 unsigned Quals) const {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000368 // FIXME: Implement this
369 assert(false && "Cannot instantiate ExtVectorType yet");
370 return QualType();
371}
372
Douglas Gregorcd281c32009-02-28 00:25:32 +0000373QualType
374TemplateTypeInstantiator::
375InstantiateFunctionProtoType(const FunctionProtoType *T,
376 unsigned Quals) const {
Douglas Gregor724651c2009-02-28 01:04:19 +0000377 QualType ResultType = Instantiate(T->getResultType());
378 if (ResultType.isNull())
379 return ResultType;
380
381 llvm::SmallVector<QualType, 16> ParamTypes;
382 for (FunctionProtoType::arg_type_iterator Param = T->arg_type_begin(),
383 ParamEnd = T->arg_type_end();
384 Param != ParamEnd; ++Param) {
385 QualType P = Instantiate(*Param);
386 if (P.isNull())
387 return P;
388
389 ParamTypes.push_back(P);
390 }
391
392 return SemaRef.BuildFunctionType(ResultType, &ParamTypes[0],
393 ParamTypes.size(),
394 T->isVariadic(), T->getTypeQuals(),
395 Loc, Entity);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000396}
397
Douglas Gregorcd281c32009-02-28 00:25:32 +0000398QualType
399TemplateTypeInstantiator::
400InstantiateFunctionNoProtoType(const FunctionNoProtoType *T,
401 unsigned Quals) const {
Douglas Gregor724651c2009-02-28 01:04:19 +0000402 assert(false && "Functions without prototypes cannot be dependent.");
Douglas Gregor99ebf652009-02-27 19:31:52 +0000403 return QualType();
404}
405
Douglas Gregorcd281c32009-02-28 00:25:32 +0000406QualType
407TemplateTypeInstantiator::InstantiateTypedefType(const TypedefType *T,
408 unsigned Quals) const {
Douglas Gregor815215d2009-05-27 05:35:12 +0000409 TypedefDecl *Typedef
410 = cast_or_null<TypedefDecl>(SemaRef.InstantiateDeclRef(T->getDecl(),
411 TemplateArgs));
412 if (!Typedef)
413 return QualType();
414
415 return SemaRef.Context.getTypeDeclType(Typedef);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000416}
417
Douglas Gregorcd281c32009-02-28 00:25:32 +0000418QualType
419TemplateTypeInstantiator::InstantiateTypeOfExprType(const TypeOfExprType *T,
420 unsigned Quals) const {
Douglas Gregor5f8bd592009-05-26 22:09:24 +0000421 Sema::OwningExprResult E
422 = SemaRef.InstantiateExpr(T->getUnderlyingExpr(), TemplateArgs);
423 if (E.isInvalid())
424 return QualType();
425
426 return SemaRef.Context.getTypeOfExprType(E.takeAs<Expr>());
Douglas Gregor99ebf652009-02-27 19:31:52 +0000427}
428
Douglas Gregorcd281c32009-02-28 00:25:32 +0000429QualType
430TemplateTypeInstantiator::InstantiateTypeOfType(const TypeOfType *T,
431 unsigned Quals) const {
Douglas Gregor5f8bd592009-05-26 22:09:24 +0000432 QualType Underlying = Instantiate(T->getUnderlyingType());
433 if (Underlying.isNull())
434 return QualType();
435
436 return SemaRef.Context.getTypeOfType(Underlying);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000437}
438
Douglas Gregorcd281c32009-02-28 00:25:32 +0000439QualType
440TemplateTypeInstantiator::InstantiateRecordType(const RecordType *T,
441 unsigned Quals) const {
Douglas Gregor815215d2009-05-27 05:35:12 +0000442 RecordDecl *Record
443 = cast_or_null<RecordDecl>(SemaRef.InstantiateDeclRef(T->getDecl(),
444 TemplateArgs));
445 if (!Record)
446 return QualType();
447
448 return SemaRef.Context.getTypeDeclType(Record);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000449}
450
Douglas Gregorcd281c32009-02-28 00:25:32 +0000451QualType
Douglas Gregorcd281c32009-02-28 00:25:32 +0000452TemplateTypeInstantiator::InstantiateEnumType(const EnumType *T,
453 unsigned Quals) const {
Douglas Gregor815215d2009-05-27 05:35:12 +0000454 EnumDecl *Enum
455 = cast_or_null<EnumDecl>(SemaRef.InstantiateDeclRef(T->getDecl(),
456 TemplateArgs));
457 if (!Enum)
458 return QualType();
459
460 return SemaRef.Context.getTypeDeclType(Enum);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000461}
462
Douglas Gregorcd281c32009-02-28 00:25:32 +0000463QualType
464TemplateTypeInstantiator::
465InstantiateTemplateTypeParmType(const TemplateTypeParmType *T,
466 unsigned Quals) const {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000467 if (T->getDepth() == 0) {
468 // Replace the template type parameter with its corresponding
469 // template argument.
Douglas Gregor99ebf652009-02-27 19:31:52 +0000470 assert(TemplateArgs[T->getIndex()].getKind() == TemplateArgument::Type &&
471 "Template argument kind mismatch");
472 QualType Result = TemplateArgs[T->getIndex()].getAsType();
Douglas Gregorcd281c32009-02-28 00:25:32 +0000473 if (Result.isNull() || !Quals)
Douglas Gregor99ebf652009-02-27 19:31:52 +0000474 return Result;
475
476 // C++ [dcl.ref]p1:
477 // [...] Cv-qualified references are ill-formed except when
478 // the cv-qualifiers are introduced through the use of a
479 // typedef (7.1.3) or of a template type argument (14.3), in
480 // which case the cv-qualifiers are ignored.
Douglas Gregorcd281c32009-02-28 00:25:32 +0000481 if (Quals && Result->isReferenceType())
482 Quals = 0;
Douglas Gregor99ebf652009-02-27 19:31:52 +0000483
Douglas Gregorcd281c32009-02-28 00:25:32 +0000484 return QualType(Result.getTypePtr(), Quals | Result.getCVRQualifiers());
Douglas Gregor99ebf652009-02-27 19:31:52 +0000485 }
486
487 // The template type parameter comes from an inner template (e.g.,
488 // the template parameter list of a member template inside the
489 // template we are instantiating). Create a new template type
490 // parameter with the template "level" reduced by one.
491 return SemaRef.Context.getTemplateTypeParmType(T->getDepth() - 1,
492 T->getIndex(),
Douglas Gregorcd281c32009-02-28 00:25:32 +0000493 T->getName())
494 .getQualifiedType(Quals);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000495}
496
Douglas Gregorcd281c32009-02-28 00:25:32 +0000497QualType
498TemplateTypeInstantiator::
Douglas Gregor7532dc62009-03-30 22:58:21 +0000499InstantiateTemplateSpecializationType(
500 const TemplateSpecializationType *T,
Douglas Gregorcd281c32009-02-28 00:25:32 +0000501 unsigned Quals) const {
Douglas Gregor40808ce2009-03-09 23:48:35 +0000502 llvm::SmallVector<TemplateArgument, 16> InstantiatedTemplateArgs;
503 InstantiatedTemplateArgs.reserve(T->getNumArgs());
Douglas Gregor7532dc62009-03-30 22:58:21 +0000504 for (TemplateSpecializationType::iterator Arg = T->begin(), ArgEnd = T->end();
Douglas Gregor40808ce2009-03-09 23:48:35 +0000505 Arg != ArgEnd; ++Arg) {
506 switch (Arg->getKind()) {
507 case TemplateArgument::Type: {
508 QualType T = SemaRef.InstantiateType(Arg->getAsType(),
Douglas Gregor7e063902009-05-11 23:53:27 +0000509 TemplateArgs,
Douglas Gregor40808ce2009-03-09 23:48:35 +0000510 Arg->getLocation(),
511 DeclarationName());
512 if (T.isNull())
513 return QualType();
514
515 InstantiatedTemplateArgs.push_back(
516 TemplateArgument(Arg->getLocation(), T));
517 break;
518 }
519
520 case TemplateArgument::Declaration:
521 case TemplateArgument::Integral:
522 InstantiatedTemplateArgs.push_back(*Arg);
523 break;
524
525 case TemplateArgument::Expression:
Douglas Gregorba498172009-03-13 21:01:28 +0000526 Sema::OwningExprResult E
Douglas Gregor7e063902009-05-11 23:53:27 +0000527 = SemaRef.InstantiateExpr(Arg->getAsExpr(), TemplateArgs);
Douglas Gregorba498172009-03-13 21:01:28 +0000528 if (E.isInvalid())
529 return QualType();
Anders Carlssonf1b1d592009-05-01 19:30:39 +0000530 InstantiatedTemplateArgs.push_back(E.takeAs<Expr>());
Douglas Gregor40808ce2009-03-09 23:48:35 +0000531 break;
532 }
533 }
534
Mike Stump390b4cc2009-05-16 07:39:55 +0000535 // FIXME: We're missing the locations of the template name, '<', and '>'.
Douglas Gregorde650ae2009-03-31 18:38:02 +0000536
537 TemplateName Name = SemaRef.InstantiateTemplateName(T->getTemplateName(),
538 Loc,
Douglas Gregor7e063902009-05-11 23:53:27 +0000539 TemplateArgs);
Douglas Gregorde650ae2009-03-31 18:38:02 +0000540
541 return SemaRef.CheckTemplateIdType(Name, Loc, SourceLocation(),
Douglas Gregor7532dc62009-03-30 22:58:21 +0000542 &InstantiatedTemplateArgs[0],
543 InstantiatedTemplateArgs.size(),
544 SourceLocation());
Douglas Gregor99ebf652009-02-27 19:31:52 +0000545}
546
Douglas Gregorcd281c32009-02-28 00:25:32 +0000547QualType
548TemplateTypeInstantiator::
Douglas Gregore4e5b052009-03-19 00:18:19 +0000549InstantiateQualifiedNameType(const QualifiedNameType *T,
550 unsigned Quals) const {
Douglas Gregord57959a2009-03-27 23:10:48 +0000551 // When we instantiated a qualified name type, there's no point in
552 // keeping the qualification around in the instantiated result. So,
553 // just instantiate the named type.
554 return (*this)(T->getNamedType());
555}
556
557QualType
558TemplateTypeInstantiator::
559InstantiateTypenameType(const TypenameType *T, unsigned Quals) const {
Douglas Gregor17343172009-04-01 00:28:59 +0000560 if (const TemplateSpecializationType *TemplateId = T->getTemplateId()) {
561 // When the typename type refers to a template-id, the template-id
562 // is dependent and has enough information to instantiate the
563 // result of the typename type. Since we don't care about keeping
564 // the spelling of the typename type in template instantiations,
565 // we just instantiate the template-id.
566 return InstantiateTemplateSpecializationType(TemplateId, Quals);
567 }
568
Douglas Gregord57959a2009-03-27 23:10:48 +0000569 NestedNameSpecifier *NNS
570 = SemaRef.InstantiateNestedNameSpecifier(T->getQualifier(),
571 SourceRange(Loc),
Douglas Gregor7e063902009-05-11 23:53:27 +0000572 TemplateArgs);
Douglas Gregord57959a2009-03-27 23:10:48 +0000573 if (!NNS)
574 return QualType();
575
Douglas Gregor17343172009-04-01 00:28:59 +0000576 return SemaRef.CheckTypenameType(NNS, *T->getIdentifier(), SourceRange(Loc));
Douglas Gregore4e5b052009-03-19 00:18:19 +0000577}
578
579QualType
580TemplateTypeInstantiator::
Douglas Gregorcd281c32009-02-28 00:25:32 +0000581InstantiateObjCInterfaceType(const ObjCInterfaceType *T,
582 unsigned Quals) const {
583 assert(false && "Objective-C types cannot be dependent");
Douglas Gregor99ebf652009-02-27 19:31:52 +0000584 return QualType();
585}
586
Douglas Gregorcd281c32009-02-28 00:25:32 +0000587QualType
588TemplateTypeInstantiator::
589InstantiateObjCQualifiedInterfaceType(const ObjCQualifiedInterfaceType *T,
590 unsigned Quals) const {
591 assert(false && "Objective-C types cannot be dependent");
Douglas Gregor99ebf652009-02-27 19:31:52 +0000592 return QualType();
593}
594
Douglas Gregorcd281c32009-02-28 00:25:32 +0000595QualType
596TemplateTypeInstantiator::
597InstantiateObjCQualifiedIdType(const ObjCQualifiedIdType *T,
598 unsigned Quals) const {
599 assert(false && "Objective-C types cannot be dependent");
Douglas Gregor99ebf652009-02-27 19:31:52 +0000600 return QualType();
601}
602
Douglas Gregorcd281c32009-02-28 00:25:32 +0000603/// \brief The actual implementation of Sema::InstantiateType().
604QualType TemplateTypeInstantiator::Instantiate(QualType T) const {
605 // If T is not a dependent type, there is nothing to do.
606 if (!T->isDependentType())
607 return T;
608
609 switch (T->getTypeClass()) {
610#define TYPE(Class, Base) \
611 case Type::Class: \
612 return Instantiate##Class##Type(cast<Class##Type>(T.getTypePtr()), \
613 T.getCVRQualifiers());
614#define ABSTRACT_TYPE(Class, Base)
615#include "clang/AST/TypeNodes.def"
616 }
617
618 assert(false && "Not all types have been decoded for instantiation");
619 return QualType();
620}
Douglas Gregor99ebf652009-02-27 19:31:52 +0000621
622/// \brief Instantiate the type T with a given set of template arguments.
623///
624/// This routine substitutes the given template arguments into the
625/// type T and produces the instantiated type.
626///
627/// \param T the type into which the template arguments will be
628/// substituted. If this type is not dependent, it will be returned
629/// immediately.
630///
631/// \param TemplateArgs the template arguments that will be
632/// substituted for the top-level template parameters within T.
633///
Douglas Gregor99ebf652009-02-27 19:31:52 +0000634/// \param Loc the location in the source code where this substitution
635/// is being performed. It will typically be the location of the
636/// declarator (if we're instantiating the type of some declaration)
637/// or the location of the type in the source code (if, e.g., we're
638/// instantiating the type of a cast expression).
639///
640/// \param Entity the name of the entity associated with a declaration
641/// being instantiated (if any). May be empty to indicate that there
642/// is no such entity (if, e.g., this is a type that occurs as part of
643/// a cast expression) or that the entity has no name (e.g., an
644/// unnamed function parameter).
645///
646/// \returns If the instantiation succeeds, the instantiated
647/// type. Otherwise, produces diagnostics and returns a NULL type.
648QualType Sema::InstantiateType(QualType T,
Douglas Gregor7e063902009-05-11 23:53:27 +0000649 const TemplateArgumentList &TemplateArgs,
Douglas Gregor99ebf652009-02-27 19:31:52 +0000650 SourceLocation Loc, DeclarationName Entity) {
Douglas Gregordf667e72009-03-10 20:44:00 +0000651 assert(!ActiveTemplateInstantiations.empty() &&
652 "Cannot perform an instantiation without some context on the "
653 "instantiation stack");
654
Douglas Gregor99ebf652009-02-27 19:31:52 +0000655 // If T is not a dependent type, there is nothing to do.
656 if (!T->isDependentType())
657 return T;
658
Douglas Gregor7e063902009-05-11 23:53:27 +0000659 TemplateTypeInstantiator Instantiator(*this, TemplateArgs, Loc, Entity);
Douglas Gregorcd281c32009-02-28 00:25:32 +0000660 return Instantiator(T);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000661}
Douglas Gregor2943aed2009-03-03 04:44:36 +0000662
663/// \brief Instantiate the base class specifiers of the given class
664/// template specialization.
665///
666/// Produces a diagnostic and returns true on error, returns false and
667/// attaches the instantiated base classes to the class template
668/// specialization if successful.
669bool
Douglas Gregord475b8d2009-03-25 21:17:03 +0000670Sema::InstantiateBaseSpecifiers(CXXRecordDecl *Instantiation,
671 CXXRecordDecl *Pattern,
Douglas Gregor7e063902009-05-11 23:53:27 +0000672 const TemplateArgumentList &TemplateArgs) {
Douglas Gregor2943aed2009-03-03 04:44:36 +0000673 bool Invalid = false;
674 llvm::SmallVector<CXXBaseSpecifier*, 8> InstantiatedBases;
Douglas Gregord475b8d2009-03-25 21:17:03 +0000675 for (ClassTemplateSpecializationDecl::base_class_iterator
676 Base = Pattern->bases_begin(), BaseEnd = Pattern->bases_end();
Douglas Gregor27b152f2009-03-10 18:52:44 +0000677 Base != BaseEnd; ++Base) {
Douglas Gregor2943aed2009-03-03 04:44:36 +0000678 if (!Base->getType()->isDependentType()) {
679 // FIXME: Allocate via ASTContext
680 InstantiatedBases.push_back(new CXXBaseSpecifier(*Base));
681 continue;
682 }
683
684 QualType BaseType = InstantiateType(Base->getType(),
Douglas Gregor7e063902009-05-11 23:53:27 +0000685 TemplateArgs,
Douglas Gregor2943aed2009-03-03 04:44:36 +0000686 Base->getSourceRange().getBegin(),
687 DeclarationName());
688 if (BaseType.isNull()) {
689 Invalid = true;
690 continue;
691 }
692
693 if (CXXBaseSpecifier *InstantiatedBase
Douglas Gregord475b8d2009-03-25 21:17:03 +0000694 = CheckBaseSpecifier(Instantiation,
Douglas Gregor2943aed2009-03-03 04:44:36 +0000695 Base->getSourceRange(),
696 Base->isVirtual(),
697 Base->getAccessSpecifierAsWritten(),
698 BaseType,
699 /*FIXME: Not totally accurate */
700 Base->getSourceRange().getBegin()))
701 InstantiatedBases.push_back(InstantiatedBase);
702 else
703 Invalid = true;
704 }
705
Douglas Gregor27b152f2009-03-10 18:52:44 +0000706 if (!Invalid &&
Jay Foadbeaaccd2009-05-21 09:52:38 +0000707 AttachBaseSpecifiers(Instantiation, InstantiatedBases.data(),
Douglas Gregor2943aed2009-03-03 04:44:36 +0000708 InstantiatedBases.size()))
709 Invalid = true;
710
711 return Invalid;
712}
713
Douglas Gregord475b8d2009-03-25 21:17:03 +0000714/// \brief Instantiate the definition of a class from a given pattern.
715///
716/// \param PointOfInstantiation The point of instantiation within the
717/// source code.
718///
719/// \param Instantiation is the declaration whose definition is being
720/// instantiated. This will be either a class template specialization
721/// or a member class of a class template specialization.
722///
723/// \param Pattern is the pattern from which the instantiation
724/// occurs. This will be either the declaration of a class template or
725/// the declaration of a member class of a class template.
726///
727/// \param TemplateArgs The template arguments to be substituted into
728/// the pattern.
729///
Douglas Gregord475b8d2009-03-25 21:17:03 +0000730/// \returns true if an error occurred, false otherwise.
731bool
732Sema::InstantiateClass(SourceLocation PointOfInstantiation,
733 CXXRecordDecl *Instantiation, CXXRecordDecl *Pattern,
Douglas Gregor93dfdb12009-05-13 00:25:59 +0000734 const TemplateArgumentList &TemplateArgs,
735 bool ExplicitInstantiation) {
Douglas Gregord475b8d2009-03-25 21:17:03 +0000736 bool Invalid = false;
737
738 CXXRecordDecl *PatternDef
739 = cast_or_null<CXXRecordDecl>(Pattern->getDefinition(Context));
740 if (!PatternDef) {
741 if (Pattern == Instantiation->getInstantiatedFromMemberClass()) {
742 Diag(PointOfInstantiation,
743 diag::err_implicit_instantiate_member_undefined)
744 << Context.getTypeDeclType(Instantiation);
745 Diag(Pattern->getLocation(), diag::note_member_of_template_here);
746 } else {
Douglas Gregor93dfdb12009-05-13 00:25:59 +0000747 Diag(PointOfInstantiation, diag::err_template_instantiate_undefined)
748 << ExplicitInstantiation
Douglas Gregord475b8d2009-03-25 21:17:03 +0000749 << Context.getTypeDeclType(Instantiation);
750 Diag(Pattern->getLocation(), diag::note_template_decl_here);
751 }
752 return true;
753 }
754 Pattern = PatternDef;
755
Douglas Gregord048bb72009-03-25 21:23:52 +0000756 InstantiatingTemplate Inst(*this, PointOfInstantiation, Instantiation);
Douglas Gregord475b8d2009-03-25 21:17:03 +0000757 if (Inst)
758 return true;
759
760 // Enter the scope of this instantiation. We don't use
761 // PushDeclContext because we don't have a scope.
762 DeclContext *PreviousContext = CurContext;
763 CurContext = Instantiation;
764
765 // Start the definition of this instantiation.
766 Instantiation->startDefinition();
767
768 // Instantiate the base class specifiers.
Douglas Gregor7e063902009-05-11 23:53:27 +0000769 if (InstantiateBaseSpecifiers(Instantiation, Pattern, TemplateArgs))
Douglas Gregord475b8d2009-03-25 21:17:03 +0000770 Invalid = true;
771
Chris Lattnerb28317a2009-03-28 19:18:32 +0000772 llvm::SmallVector<DeclPtrTy, 32> Fields;
Douglas Gregor6ab35242009-04-09 21:40:53 +0000773 for (RecordDecl::decl_iterator Member = Pattern->decls_begin(Context),
774 MemberEnd = Pattern->decls_end(Context);
775 Member != MemberEnd; ++Member) {
Douglas Gregor7e063902009-05-11 23:53:27 +0000776 Decl *NewMember = InstantiateDecl(*Member, Instantiation, TemplateArgs);
Douglas Gregord475b8d2009-03-25 21:17:03 +0000777 if (NewMember) {
778 if (NewMember->isInvalidDecl())
779 Invalid = true;
780 else if (FieldDecl *Field = dyn_cast<FieldDecl>(NewMember))
Chris Lattnerb28317a2009-03-28 19:18:32 +0000781 Fields.push_back(DeclPtrTy::make(Field));
Douglas Gregord475b8d2009-03-25 21:17:03 +0000782 } else {
783 // FIXME: Eventually, a NULL return will mean that one of the
Mike Stump390b4cc2009-05-16 07:39:55 +0000784 // instantiations was a semantic disaster, and we'll want to set Invalid =
785 // true. For now, we expect to skip some members that we can't yet handle.
Douglas Gregord475b8d2009-03-25 21:17:03 +0000786 }
787 }
788
789 // Finish checking fields.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000790 ActOnFields(0, Instantiation->getLocation(), DeclPtrTy::make(Instantiation),
Jay Foadbeaaccd2009-05-21 09:52:38 +0000791 Fields.data(), Fields.size(), SourceLocation(), SourceLocation(),
Douglas Gregord475b8d2009-03-25 21:17:03 +0000792 0);
793
794 // Add any implicitly-declared members that we might need.
795 AddImplicitlyDeclaredMembersToClass(Instantiation);
796
797 // Exit the scope of this instantiation.
798 CurContext = PreviousContext;
799
Douglas Gregoraba43bb2009-05-26 20:50:29 +0000800 if (!Invalid)
801 Consumer.HandleTagDeclDefinition(Instantiation);
802
Douglas Gregora58861f2009-05-13 20:28:22 +0000803 // If this is an explicit instantiation, instantiate our members, too.
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000804 if (!Invalid && ExplicitInstantiation) {
805 Inst.Clear();
Douglas Gregora58861f2009-05-13 20:28:22 +0000806 InstantiateClassMembers(PointOfInstantiation, Instantiation, TemplateArgs);
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000807 }
Douglas Gregora58861f2009-05-13 20:28:22 +0000808
Douglas Gregord475b8d2009-03-25 21:17:03 +0000809 return Invalid;
810}
811
Douglas Gregor2943aed2009-03-03 04:44:36 +0000812bool
813Sema::InstantiateClassTemplateSpecialization(
814 ClassTemplateSpecializationDecl *ClassTemplateSpec,
815 bool ExplicitInstantiation) {
816 // Perform the actual instantiation on the canonical declaration.
817 ClassTemplateSpec = cast<ClassTemplateSpecializationDecl>(
818 Context.getCanonicalDecl(ClassTemplateSpec));
819
820 // We can only instantiate something that hasn't already been
821 // instantiated or specialized. Fail without any diagnostics: our
822 // caller will provide an error message.
823 if (ClassTemplateSpec->getSpecializationKind() != TSK_Undeclared)
824 return true;
825
Mike Stump390b4cc2009-05-16 07:39:55 +0000826 // FIXME: Push this class template instantiation onto the instantiation stack,
827 // checking for recursion that exceeds a certain depth.
Douglas Gregor2943aed2009-03-03 04:44:36 +0000828
Mike Stump390b4cc2009-05-16 07:39:55 +0000829 // FIXME: Perform class template partial specialization to select the best
830 // template.
Douglas Gregor2943aed2009-03-03 04:44:36 +0000831 ClassTemplateDecl *Template = ClassTemplateSpec->getSpecializedTemplate();
832
Douglas Gregord475b8d2009-03-25 21:17:03 +0000833 CXXRecordDecl *Pattern = Template->getTemplatedDecl();
Douglas Gregor2943aed2009-03-03 04:44:36 +0000834
835 // Note that this is an instantiation.
836 ClassTemplateSpec->setSpecializationKind(
837 ExplicitInstantiation? TSK_ExplicitInstantiation
838 : TSK_ImplicitInstantiation);
839
Douglas Gregord475b8d2009-03-25 21:17:03 +0000840 return InstantiateClass(ClassTemplateSpec->getLocation(),
841 ClassTemplateSpec, Pattern,
Douglas Gregor93dfdb12009-05-13 00:25:59 +0000842 ClassTemplateSpec->getTemplateArgs(),
843 ExplicitInstantiation);
Douglas Gregor2943aed2009-03-03 04:44:36 +0000844}
Douglas Gregor5953d8b2009-03-19 17:26:29 +0000845
Douglas Gregora58861f2009-05-13 20:28:22 +0000846/// \brief Instantiate the definitions of all of the member of the
847/// given class, which is an instantiation of a class template or a
848/// member class of a template.
849void
850Sema::InstantiateClassMembers(SourceLocation PointOfInstantiation,
851 CXXRecordDecl *Instantiation,
852 const TemplateArgumentList &TemplateArgs) {
853 for (DeclContext::decl_iterator D = Instantiation->decls_begin(Context),
854 DEnd = Instantiation->decls_end(Context);
855 D != DEnd; ++D) {
856 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(*D)) {
857 if (!Function->getBody(Context))
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000858 InstantiateFunctionDefinition(PointOfInstantiation, Function);
Douglas Gregora58861f2009-05-13 20:28:22 +0000859 } else if (VarDecl *Var = dyn_cast<VarDecl>(*D)) {
860 const VarDecl *Def = 0;
861 if (!Var->getDefinition(Def))
862 InstantiateVariableDefinition(Var);
863 } else if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(*D)) {
864 if (!Record->isInjectedClassName() && !Record->getDefinition(Context)) {
865 assert(Record->getInstantiatedFromMemberClass() &&
866 "Missing instantiated-from-template information");
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000867 InstantiateClass(PointOfInstantiation, Record,
Douglas Gregora58861f2009-05-13 20:28:22 +0000868 Record->getInstantiatedFromMemberClass(),
869 TemplateArgs, true);
870 }
871 }
872 }
873}
874
875/// \brief Instantiate the definitions of all of the members of the
876/// given class template specialization, which was named as part of an
877/// explicit instantiation.
878void Sema::InstantiateClassTemplateSpecializationMembers(
879 SourceLocation PointOfInstantiation,
880 ClassTemplateSpecializationDecl *ClassTemplateSpec) {
881 // C++0x [temp.explicit]p7:
882 // An explicit instantiation that names a class template
883 // specialization is an explicit instantion of the same kind
884 // (declaration or definition) of each of its members (not
885 // including members inherited from base classes) that has not
886 // been previously explicitly specialized in the translation unit
887 // containing the explicit instantiation, except as described
888 // below.
889 InstantiateClassMembers(PointOfInstantiation, ClassTemplateSpec,
890 ClassTemplateSpec->getTemplateArgs());
891}
892
Douglas Gregorab452ba2009-03-26 23:50:42 +0000893/// \brief Instantiate a nested-name-specifier.
894NestedNameSpecifier *
895Sema::InstantiateNestedNameSpecifier(NestedNameSpecifier *NNS,
896 SourceRange Range,
Douglas Gregor7e063902009-05-11 23:53:27 +0000897 const TemplateArgumentList &TemplateArgs) {
Douglas Gregorab452ba2009-03-26 23:50:42 +0000898 // Instantiate the prefix of this nested name specifier.
899 NestedNameSpecifier *Prefix = NNS->getPrefix();
900 if (Prefix) {
Douglas Gregor7e063902009-05-11 23:53:27 +0000901 Prefix = InstantiateNestedNameSpecifier(Prefix, Range, TemplateArgs);
Douglas Gregorab452ba2009-03-26 23:50:42 +0000902 if (!Prefix)
903 return 0;
Douglas Gregor5953d8b2009-03-19 17:26:29 +0000904 }
905
Douglas Gregorab452ba2009-03-26 23:50:42 +0000906 switch (NNS->getKind()) {
Douglas Gregor17343172009-04-01 00:28:59 +0000907 case NestedNameSpecifier::Identifier: {
908 assert(Prefix &&
909 "Can't have an identifier nested-name-specifier with no prefix");
910 CXXScopeSpec SS;
911 // FIXME: The source location information is all wrong.
912 SS.setRange(Range);
913 SS.setScopeRep(Prefix);
914 return static_cast<NestedNameSpecifier *>(
915 ActOnCXXNestedNameSpecifier(0, SS,
916 Range.getEnd(),
917 Range.getEnd(),
Douglas Gregor3f5b61c2009-05-14 00:28:11 +0000918 *NNS->getAsIdentifier()));
Douglas Gregorab452ba2009-03-26 23:50:42 +0000919 break;
Douglas Gregor17343172009-04-01 00:28:59 +0000920 }
Douglas Gregorab452ba2009-03-26 23:50:42 +0000921
922 case NestedNameSpecifier::Namespace:
923 case NestedNameSpecifier::Global:
924 return NNS;
925
926 case NestedNameSpecifier::TypeSpecWithTemplate:
927 case NestedNameSpecifier::TypeSpec: {
928 QualType T = QualType(NNS->getAsType(), 0);
929 if (!T->isDependentType())
930 return NNS;
931
Douglas Gregor7e063902009-05-11 23:53:27 +0000932 T = InstantiateType(T, TemplateArgs, Range.getBegin(), DeclarationName());
Douglas Gregorab452ba2009-03-26 23:50:42 +0000933 if (T.isNull())
934 return 0;
935
Douglas Gregord57959a2009-03-27 23:10:48 +0000936 if (T->isRecordType() ||
937 (getLangOptions().CPlusPlus0x && T->isEnumeralType())) {
Douglas Gregor17343172009-04-01 00:28:59 +0000938 assert(T.getCVRQualifiers() == 0 && "Can't get cv-qualifiers here");
Douglas Gregord57959a2009-03-27 23:10:48 +0000939 return NestedNameSpecifier::Create(Context, Prefix,
Douglas Gregorab452ba2009-03-26 23:50:42 +0000940 NNS->getKind() == NestedNameSpecifier::TypeSpecWithTemplate,
Douglas Gregord57959a2009-03-27 23:10:48 +0000941 T.getTypePtr());
942 }
943
944 Diag(Range.getBegin(), diag::err_nested_name_spec_non_tag) << T;
945 return 0;
Douglas Gregorab452ba2009-03-26 23:50:42 +0000946 }
947 }
948
Douglas Gregord57959a2009-03-27 23:10:48 +0000949 // Required to silence a GCC warning
Douglas Gregorab452ba2009-03-26 23:50:42 +0000950 return 0;
Douglas Gregor5953d8b2009-03-19 17:26:29 +0000951}
Douglas Gregorde650ae2009-03-31 18:38:02 +0000952
953TemplateName
954Sema::InstantiateTemplateName(TemplateName Name, SourceLocation Loc,
Douglas Gregor7e063902009-05-11 23:53:27 +0000955 const TemplateArgumentList &TemplateArgs) {
Douglas Gregorde650ae2009-03-31 18:38:02 +0000956 if (TemplateTemplateParmDecl *TTP
957 = dyn_cast_or_null<TemplateTemplateParmDecl>(
958 Name.getAsTemplateDecl())) {
959 assert(TTP->getDepth() == 0 &&
960 "Cannot reduce depth of a template template parameter");
Douglas Gregor9bde7732009-03-31 20:22:05 +0000961 assert(TemplateArgs[TTP->getPosition()].getAsDecl() &&
Douglas Gregorde650ae2009-03-31 18:38:02 +0000962 "Wrong kind of template template argument");
963 ClassTemplateDecl *ClassTemplate
964 = dyn_cast<ClassTemplateDecl>(
965 TemplateArgs[TTP->getPosition()].getAsDecl());
Douglas Gregor9bde7732009-03-31 20:22:05 +0000966 assert(ClassTemplate && "Expected a class template");
Douglas Gregorde650ae2009-03-31 18:38:02 +0000967 if (QualifiedTemplateName *QTN = Name.getAsQualifiedTemplateName()) {
968 NestedNameSpecifier *NNS
969 = InstantiateNestedNameSpecifier(QTN->getQualifier(),
970 /*FIXME=*/SourceRange(Loc),
Douglas Gregor7e063902009-05-11 23:53:27 +0000971 TemplateArgs);
Douglas Gregorde650ae2009-03-31 18:38:02 +0000972 if (NNS)
973 return Context.getQualifiedTemplateName(NNS,
974 QTN->hasTemplateKeyword(),
975 ClassTemplate);
976 }
977
978 return TemplateName(ClassTemplate);
979 } else if (DependentTemplateName *DTN = Name.getAsDependentTemplateName()) {
980 NestedNameSpecifier *NNS
981 = InstantiateNestedNameSpecifier(DTN->getQualifier(),
982 /*FIXME=*/SourceRange(Loc),
Douglas Gregor7e063902009-05-11 23:53:27 +0000983 TemplateArgs);
Douglas Gregorde650ae2009-03-31 18:38:02 +0000984
985 if (!NNS) // FIXME: Not the best recovery strategy.
986 return Name;
987
988 if (NNS->isDependent())
989 return Context.getDependentTemplateName(NNS, DTN->getName());
990
991 // Somewhat redundant with ActOnDependentTemplateName.
992 CXXScopeSpec SS;
993 SS.setRange(SourceRange(Loc));
994 SS.setScopeRep(NNS);
995 TemplateTy Template;
996 TemplateNameKind TNK = isTemplateName(*DTN->getName(), 0, Template, &SS);
997 if (TNK == TNK_Non_template) {
998 Diag(Loc, diag::err_template_kw_refers_to_non_template)
999 << DTN->getName();
1000 return Name;
1001 } else if (TNK == TNK_Function_template) {
1002 Diag(Loc, diag::err_template_kw_refers_to_non_template)
1003 << DTN->getName();
1004 return Name;
1005 }
1006
1007 return Template.getAsVal<TemplateName>();
1008 }
1009
1010
1011
Mike Stump390b4cc2009-05-16 07:39:55 +00001012 // FIXME: Even if we're referring to a Decl that isn't a template template
1013 // parameter, we may need to instantiate the outer contexts of that
1014 // Decl. However, this won't be needed until we implement member templates.
Douglas Gregorde650ae2009-03-31 18:38:02 +00001015 return Name;
1016}