blob: 08019aaea43a893c464ac602773c452737f7b195 [file] [log] [blame]
Douglas Gregor74296542009-02-27 19:31:52 +00001//===------- SemaTemplateInstantiate.cpp - C++ Template Instantiation ------===/
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//===----------------------------------------------------------------------===/
8//
9// This file implements C++ template instantiation.
10//
11//===----------------------------------------------------------------------===/
12
13#include "Sema.h"
14#include "clang/AST/ASTContext.h"
15#include "clang/AST/Expr.h"
Douglas Gregor74296542009-02-27 19:31:52 +000016#include "clang/AST/DeclTemplate.h"
17#include "clang/Parse/DeclSpec.h"
18#include "clang/Basic/LangOptions.h"
Douglas Gregorf57dcd02009-02-28 00:25:32 +000019#include "llvm/Support/Compiler.h"
Douglas Gregor74296542009-02-27 19:31:52 +000020
21using namespace clang;
22
Douglas Gregorfee85d62009-03-10 18:03:33 +000023//===----------------------------------------------------------------------===/
24// Template Instantiation Support
25//===----------------------------------------------------------------------===/
26
Douglas Gregor5f62c5e2009-05-14 23:26:13 +000027/// \brief Retrieve the template argument list that should be used to
28/// instantiate the given declaration.
29const TemplateArgumentList &
30Sema::getTemplateInstantiationArgs(NamedDecl *D) {
31 if (ClassTemplateSpecializationDecl *Spec
32 = dyn_cast<ClassTemplateSpecializationDecl>(D))
33 return Spec->getTemplateArgs();
34
35 DeclContext *EnclosingTemplateCtx = D->getDeclContext();
36 while (!isa<ClassTemplateSpecializationDecl>(EnclosingTemplateCtx)) {
37 assert(!EnclosingTemplateCtx->isFileContext() &&
38 "Tried to get the instantiation arguments of a non-template");
39 EnclosingTemplateCtx = EnclosingTemplateCtx->getParent();
40 }
41
42 ClassTemplateSpecializationDecl *EnclosingTemplate
43 = cast<ClassTemplateSpecializationDecl>(EnclosingTemplateCtx);
44 return EnclosingTemplate->getTemplateArgs();
45}
46
Douglas Gregor375733c2009-03-10 00:06:19 +000047Sema::InstantiatingTemplate::
48InstantiatingTemplate(Sema &SemaRef, SourceLocation PointOfInstantiation,
Douglas Gregorb12249d2009-05-18 17:01:57 +000049 Decl *Entity,
Douglas Gregor375733c2009-03-10 00:06:19 +000050 SourceRange InstantiationRange)
51 : SemaRef(SemaRef) {
Douglas Gregor56d25a72009-03-10 20:44:00 +000052
53 Invalid = CheckInstantiationDepth(PointOfInstantiation,
54 InstantiationRange);
55 if (!Invalid) {
Douglas Gregor375733c2009-03-10 00:06:19 +000056 ActiveTemplateInstantiation Inst;
Douglas Gregor56d25a72009-03-10 20:44:00 +000057 Inst.Kind = ActiveTemplateInstantiation::TemplateInstantiation;
Douglas Gregor375733c2009-03-10 00:06:19 +000058 Inst.PointOfInstantiation = PointOfInstantiation;
Douglas Gregor56d25a72009-03-10 20:44:00 +000059 Inst.Entity = reinterpret_cast<uintptr_t>(Entity);
Douglas Gregor95ba1282009-03-12 18:36:18 +000060 Inst.TemplateArgs = 0;
61 Inst.NumTemplateArgs = 0;
Douglas Gregor56d25a72009-03-10 20:44:00 +000062 Inst.InstantiationRange = InstantiationRange;
63 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
64 Invalid = false;
65 }
66}
67
68Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef,
69 SourceLocation PointOfInstantiation,
70 TemplateDecl *Template,
71 const TemplateArgument *TemplateArgs,
72 unsigned NumTemplateArgs,
73 SourceRange InstantiationRange)
74 : SemaRef(SemaRef) {
75
76 Invalid = CheckInstantiationDepth(PointOfInstantiation,
77 InstantiationRange);
78 if (!Invalid) {
79 ActiveTemplateInstantiation Inst;
80 Inst.Kind
81 = ActiveTemplateInstantiation::DefaultTemplateArgumentInstantiation;
82 Inst.PointOfInstantiation = PointOfInstantiation;
83 Inst.Entity = reinterpret_cast<uintptr_t>(Template);
84 Inst.TemplateArgs = TemplateArgs;
85 Inst.NumTemplateArgs = NumTemplateArgs;
Douglas Gregor375733c2009-03-10 00:06:19 +000086 Inst.InstantiationRange = InstantiationRange;
87 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
88 Invalid = false;
89 }
90}
91
Douglas Gregorb12249d2009-05-18 17:01:57 +000092void Sema::InstantiatingTemplate::Clear() {
93 if (!Invalid) {
Douglas Gregor375733c2009-03-10 00:06:19 +000094 SemaRef.ActiveTemplateInstantiations.pop_back();
Douglas Gregorb12249d2009-05-18 17:01:57 +000095 Invalid = true;
96 }
Douglas Gregor375733c2009-03-10 00:06:19 +000097}
98
Douglas Gregor56d25a72009-03-10 20:44:00 +000099bool Sema::InstantiatingTemplate::CheckInstantiationDepth(
100 SourceLocation PointOfInstantiation,
101 SourceRange InstantiationRange) {
102 if (SemaRef.ActiveTemplateInstantiations.size()
103 <= SemaRef.getLangOptions().InstantiationDepth)
104 return false;
105
106 SemaRef.Diag(PointOfInstantiation,
107 diag::err_template_recursion_depth_exceeded)
108 << SemaRef.getLangOptions().InstantiationDepth
109 << InstantiationRange;
110 SemaRef.Diag(PointOfInstantiation, diag::note_template_recursion_depth)
111 << SemaRef.getLangOptions().InstantiationDepth;
112 return true;
113}
114
Douglas Gregorfee85d62009-03-10 18:03:33 +0000115/// \brief Prints the current instantiation stack through a series of
116/// notes.
117void Sema::PrintInstantiationStack() {
118 for (llvm::SmallVector<ActiveTemplateInstantiation, 16>::reverse_iterator
119 Active = ActiveTemplateInstantiations.rbegin(),
120 ActiveEnd = ActiveTemplateInstantiations.rend();
121 Active != ActiveEnd;
122 ++Active) {
Douglas Gregor56d25a72009-03-10 20:44:00 +0000123 switch (Active->Kind) {
124 case ActiveTemplateInstantiation::TemplateInstantiation: {
Douglas Gregorb12249d2009-05-18 17:01:57 +0000125 Decl *D = reinterpret_cast<Decl *>(Active->Entity);
126 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) {
127 unsigned DiagID = diag::note_template_member_class_here;
128 if (isa<ClassTemplateSpecializationDecl>(Record))
129 DiagID = diag::note_template_class_instantiation_here;
130 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
131 DiagID)
132 << Context.getTypeDeclType(Record)
133 << Active->InstantiationRange;
134 } else {
135 FunctionDecl *Function = cast<FunctionDecl>(D);
136 unsigned DiagID = diag::note_template_member_function_here;
137 // FIXME: check for a function template
138 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
139 DiagID)
140 << Function
141 << Active->InstantiationRange;
142 }
Douglas Gregor56d25a72009-03-10 20:44:00 +0000143 break;
144 }
145
146 case ActiveTemplateInstantiation::DefaultTemplateArgumentInstantiation: {
147 TemplateDecl *Template = cast<TemplateDecl>((Decl *)Active->Entity);
148 std::string TemplateArgsStr
Douglas Gregordd13e842009-03-30 22:58:21 +0000149 = TemplateSpecializationType::PrintTemplateArgumentList(
Douglas Gregor56d25a72009-03-10 20:44:00 +0000150 Active->TemplateArgs,
151 Active->NumTemplateArgs);
152 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
153 diag::note_default_arg_instantiation_here)
154 << (Template->getNameAsString() + TemplateArgsStr)
155 << Active->InstantiationRange;
156 break;
157 }
158 }
Douglas Gregorfee85d62009-03-10 18:03:33 +0000159 }
160}
161
Douglas Gregor74296542009-02-27 19:31:52 +0000162//===----------------------------------------------------------------------===/
163// Template Instantiation for Types
164//===----------------------------------------------------------------------===/
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000165namespace {
166 class VISIBILITY_HIDDEN TemplateTypeInstantiator {
167 Sema &SemaRef;
Douglas Gregorf9e7d3d2009-05-11 23:53:27 +0000168 const TemplateArgumentList &TemplateArgs;
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000169 SourceLocation Loc;
170 DeclarationName Entity;
Douglas Gregor74296542009-02-27 19:31:52 +0000171
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000172 public:
173 TemplateTypeInstantiator(Sema &SemaRef,
Douglas Gregorf9e7d3d2009-05-11 23:53:27 +0000174 const TemplateArgumentList &TemplateArgs,
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000175 SourceLocation Loc,
176 DeclarationName Entity)
177 : SemaRef(SemaRef), TemplateArgs(TemplateArgs),
Douglas Gregorf9e7d3d2009-05-11 23:53:27 +0000178 Loc(Loc), Entity(Entity) { }
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000179
180 QualType operator()(QualType T) const { return Instantiate(T); }
181
182 QualType Instantiate(QualType T) const;
183
184 // Declare instantiate functions for each type.
185#define TYPE(Class, Base) \
186 QualType Instantiate##Class##Type(const Class##Type *T, \
187 unsigned Quals) const;
188#define ABSTRACT_TYPE(Class, Base)
189#include "clang/AST/TypeNodes.def"
190 };
191}
192
193QualType
194TemplateTypeInstantiator::InstantiateExtQualType(const ExtQualType *T,
195 unsigned Quals) const {
Douglas Gregor74296542009-02-27 19:31:52 +0000196 // FIXME: Implement this
197 assert(false && "Cannot instantiate ExtQualType yet");
198 return QualType();
199}
200
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000201QualType
202TemplateTypeInstantiator::InstantiateBuiltinType(const BuiltinType *T,
203 unsigned Quals) const {
Douglas Gregorc0139ca2009-02-28 01:04:19 +0000204 assert(false && "Builtin types are not dependent and cannot be instantiated");
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000205 return QualType(T, Quals);
Douglas Gregor74296542009-02-27 19:31:52 +0000206}
207
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000208QualType
209TemplateTypeInstantiator::
210InstantiateFixedWidthIntType(const FixedWidthIntType *T, unsigned Quals) const {
Douglas Gregor74296542009-02-27 19:31:52 +0000211 // FIXME: Implement this
212 assert(false && "Cannot instantiate FixedWidthIntType yet");
213 return QualType();
214}
215
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000216QualType
217TemplateTypeInstantiator::InstantiateComplexType(const ComplexType *T,
218 unsigned Quals) const {
Douglas Gregor74296542009-02-27 19:31:52 +0000219 // FIXME: Implement this
220 assert(false && "Cannot instantiate ComplexType yet");
221 return QualType();
222}
223
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000224QualType
225TemplateTypeInstantiator::InstantiatePointerType(const PointerType *T,
226 unsigned Quals) const {
227 QualType PointeeType = Instantiate(T->getPointeeType());
228 if (PointeeType.isNull())
229 return QualType();
230
231 return SemaRef.BuildPointerType(PointeeType, Quals, Loc, Entity);
Douglas Gregor74296542009-02-27 19:31:52 +0000232}
233
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000234QualType
235TemplateTypeInstantiator::InstantiateBlockPointerType(const BlockPointerType *T,
236 unsigned Quals) const {
Douglas Gregor74296542009-02-27 19:31:52 +0000237 // FIXME: Implement this
238 assert(false && "Cannot instantiate BlockPointerType yet");
239 return QualType();
240}
241
Sebastian Redlce6fff02009-03-16 23:22:08 +0000242QualType
243TemplateTypeInstantiator::InstantiateLValueReferenceType(
244 const LValueReferenceType *T, unsigned Quals) const {
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000245 QualType ReferentType = Instantiate(T->getPointeeType());
246 if (ReferentType.isNull())
247 return QualType();
248
Sebastian Redlce6fff02009-03-16 23:22:08 +0000249 return SemaRef.BuildReferenceType(ReferentType, true, Quals, Loc, Entity);
250}
251
252QualType
253TemplateTypeInstantiator::InstantiateRValueReferenceType(
254 const RValueReferenceType *T, unsigned Quals) const {
255 QualType ReferentType = Instantiate(T->getPointeeType());
256 if (ReferentType.isNull())
257 return QualType();
258
259 return SemaRef.BuildReferenceType(ReferentType, false, Quals, Loc, Entity);
Douglas Gregor74296542009-02-27 19:31:52 +0000260}
261
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000262QualType
263TemplateTypeInstantiator::
264InstantiateMemberPointerType(const MemberPointerType *T,
265 unsigned Quals) const {
Douglas Gregor74296542009-02-27 19:31:52 +0000266 // FIXME: Implement this
267 assert(false && "Cannot instantiate MemberPointerType yet");
268 return QualType();
269}
270
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000271QualType
272TemplateTypeInstantiator::
273InstantiateConstantArrayType(const ConstantArrayType *T,
274 unsigned Quals) const {
275 QualType ElementType = Instantiate(T->getElementType());
276 if (ElementType.isNull())
277 return ElementType;
278
279 // Build a temporary integer literal to specify the size for
280 // BuildArrayType. Since we have already checked the size as part of
281 // creating the dependent array type in the first place, we know
Douglas Gregor90177912009-05-13 18:28:20 +0000282 // there aren't any errors. However, we do need to determine what
283 // C++ type to give the size expression.
284 llvm::APInt Size = T->getSize();
285 QualType Types[] = {
286 SemaRef.Context.UnsignedCharTy, SemaRef.Context.UnsignedShortTy,
287 SemaRef.Context.UnsignedIntTy, SemaRef.Context.UnsignedLongTy,
288 SemaRef.Context.UnsignedLongLongTy, SemaRef.Context.UnsignedInt128Ty
289 };
290 const unsigned NumTypes = sizeof(Types) / sizeof(QualType);
291 QualType SizeType;
292 for (unsigned I = 0; I != NumTypes; ++I)
293 if (Size.getBitWidth() == SemaRef.Context.getIntWidth(Types[I])) {
294 SizeType = Types[I];
295 break;
296 }
297
298 if (SizeType.isNull())
299 SizeType = SemaRef.Context.getFixedWidthIntType(Size.getBitWidth(), false);
300
301 IntegerLiteral ArraySize(Size, SizeType, Loc);
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000302 return SemaRef.BuildArrayType(ElementType, T->getSizeModifier(),
303 &ArraySize, T->getIndexTypeQualifier(),
304 Loc, Entity);
Douglas Gregor74296542009-02-27 19:31:52 +0000305}
306
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000307QualType
308TemplateTypeInstantiator::
309InstantiateIncompleteArrayType(const IncompleteArrayType *T,
310 unsigned Quals) const {
311 QualType ElementType = Instantiate(T->getElementType());
312 if (ElementType.isNull())
313 return ElementType;
314
315 return SemaRef.BuildArrayType(ElementType, T->getSizeModifier(),
316 0, T->getIndexTypeQualifier(),
317 Loc, Entity);
Douglas Gregor74296542009-02-27 19:31:52 +0000318}
319
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000320QualType
321TemplateTypeInstantiator::
322InstantiateVariableArrayType(const VariableArrayType *T,
323 unsigned Quals) const {
Douglas Gregor74296542009-02-27 19:31:52 +0000324 // FIXME: Implement this
325 assert(false && "Cannot instantiate VariableArrayType yet");
326 return QualType();
327}
328
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000329QualType
330TemplateTypeInstantiator::
331InstantiateDependentSizedArrayType(const DependentSizedArrayType *T,
332 unsigned Quals) const {
Anders Carlsson149f2782009-03-15 20:12:13 +0000333 Expr *ArraySize = T->getSizeExpr();
334 assert(ArraySize->isValueDependent() &&
335 "dependent sized array types must have value dependent size expr");
336
337 // Instantiate the element type if needed
338 QualType ElementType = T->getElementType();
339 if (ElementType->isDependentType()) {
340 ElementType = Instantiate(ElementType);
341 if (ElementType.isNull())
342 return QualType();
343 }
344
345 // Instantiate the size expression
346 Sema::OwningExprResult InstantiatedArraySize =
Douglas Gregorf9e7d3d2009-05-11 23:53:27 +0000347 SemaRef.InstantiateExpr(ArraySize, TemplateArgs);
Anders Carlsson149f2782009-03-15 20:12:13 +0000348 if (InstantiatedArraySize.isInvalid())
349 return QualType();
350
351 return SemaRef.BuildArrayType(ElementType, T->getSizeModifier(),
Anders Carlsson39ecdcf2009-05-01 19:49:17 +0000352 InstantiatedArraySize.takeAs<Expr>(),
Anders Carlsson149f2782009-03-15 20:12:13 +0000353 T->getIndexTypeQualifier(), Loc, Entity);
Douglas Gregor74296542009-02-27 19:31:52 +0000354}
355
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000356QualType
357TemplateTypeInstantiator::InstantiateVectorType(const VectorType *T,
358 unsigned Quals) const {
Douglas Gregor74296542009-02-27 19:31:52 +0000359 // FIXME: Implement this
360 assert(false && "Cannot instantiate VectorType yet");
361 return QualType();
362}
363
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000364QualType
365TemplateTypeInstantiator::InstantiateExtVectorType(const ExtVectorType *T,
366 unsigned Quals) const {
Douglas Gregor74296542009-02-27 19:31:52 +0000367 // FIXME: Implement this
368 assert(false && "Cannot instantiate ExtVectorType yet");
369 return QualType();
370}
371
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000372QualType
373TemplateTypeInstantiator::
374InstantiateFunctionProtoType(const FunctionProtoType *T,
375 unsigned Quals) const {
Douglas Gregorc0139ca2009-02-28 01:04:19 +0000376 QualType ResultType = Instantiate(T->getResultType());
377 if (ResultType.isNull())
378 return ResultType;
379
380 llvm::SmallVector<QualType, 16> ParamTypes;
381 for (FunctionProtoType::arg_type_iterator Param = T->arg_type_begin(),
382 ParamEnd = T->arg_type_end();
383 Param != ParamEnd; ++Param) {
384 QualType P = Instantiate(*Param);
385 if (P.isNull())
386 return P;
387
388 ParamTypes.push_back(P);
389 }
390
391 return SemaRef.BuildFunctionType(ResultType, &ParamTypes[0],
392 ParamTypes.size(),
393 T->isVariadic(), T->getTypeQuals(),
394 Loc, Entity);
Douglas Gregor74296542009-02-27 19:31:52 +0000395}
396
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000397QualType
398TemplateTypeInstantiator::
399InstantiateFunctionNoProtoType(const FunctionNoProtoType *T,
400 unsigned Quals) const {
Douglas Gregorc0139ca2009-02-28 01:04:19 +0000401 assert(false && "Functions without prototypes cannot be dependent.");
Douglas Gregor74296542009-02-27 19:31:52 +0000402 return QualType();
403}
404
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000405QualType
406TemplateTypeInstantiator::InstantiateTypedefType(const TypedefType *T,
407 unsigned Quals) const {
Douglas Gregor74296542009-02-27 19:31:52 +0000408 // FIXME: Implement this
409 assert(false && "Cannot instantiate TypedefType yet");
410 return QualType();
411}
412
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000413QualType
414TemplateTypeInstantiator::InstantiateTypeOfExprType(const TypeOfExprType *T,
415 unsigned Quals) const {
Douglas Gregor74296542009-02-27 19:31:52 +0000416 // FIXME: Implement this
417 assert(false && "Cannot instantiate TypeOfExprType yet");
418 return QualType();
419}
420
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000421QualType
422TemplateTypeInstantiator::InstantiateTypeOfType(const TypeOfType *T,
423 unsigned Quals) const {
Douglas Gregor74296542009-02-27 19:31:52 +0000424 // FIXME: Implement this
425 assert(false && "Cannot instantiate TypeOfType yet");
426 return QualType();
427}
428
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000429QualType
430TemplateTypeInstantiator::InstantiateRecordType(const RecordType *T,
431 unsigned Quals) const {
Douglas Gregor74296542009-02-27 19:31:52 +0000432 // FIXME: Implement this
433 assert(false && "Cannot instantiate RecordType yet");
434 return QualType();
435}
436
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000437QualType
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000438TemplateTypeInstantiator::InstantiateEnumType(const EnumType *T,
439 unsigned Quals) const {
Douglas Gregor74296542009-02-27 19:31:52 +0000440 // FIXME: Implement this
441 assert(false && "Cannot instantiate EnumType yet");
442 return QualType();
443}
444
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000445QualType
446TemplateTypeInstantiator::
447InstantiateTemplateTypeParmType(const TemplateTypeParmType *T,
448 unsigned Quals) const {
Douglas Gregor74296542009-02-27 19:31:52 +0000449 if (T->getDepth() == 0) {
450 // Replace the template type parameter with its corresponding
451 // template argument.
Douglas Gregor74296542009-02-27 19:31:52 +0000452 assert(TemplateArgs[T->getIndex()].getKind() == TemplateArgument::Type &&
453 "Template argument kind mismatch");
454 QualType Result = TemplateArgs[T->getIndex()].getAsType();
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000455 if (Result.isNull() || !Quals)
Douglas Gregor74296542009-02-27 19:31:52 +0000456 return Result;
457
458 // C++ [dcl.ref]p1:
459 // [...] Cv-qualified references are ill-formed except when
460 // the cv-qualifiers are introduced through the use of a
461 // typedef (7.1.3) or of a template type argument (14.3), in
462 // which case the cv-qualifiers are ignored.
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000463 if (Quals && Result->isReferenceType())
464 Quals = 0;
Douglas Gregor74296542009-02-27 19:31:52 +0000465
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000466 return QualType(Result.getTypePtr(), Quals | Result.getCVRQualifiers());
Douglas Gregor74296542009-02-27 19:31:52 +0000467 }
468
469 // The template type parameter comes from an inner template (e.g.,
470 // the template parameter list of a member template inside the
471 // template we are instantiating). Create a new template type
472 // parameter with the template "level" reduced by one.
473 return SemaRef.Context.getTemplateTypeParmType(T->getDepth() - 1,
474 T->getIndex(),
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000475 T->getName())
476 .getQualifiedType(Quals);
Douglas Gregor74296542009-02-27 19:31:52 +0000477}
478
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000479QualType
480TemplateTypeInstantiator::
Douglas Gregordd13e842009-03-30 22:58:21 +0000481InstantiateTemplateSpecializationType(
482 const TemplateSpecializationType *T,
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000483 unsigned Quals) const {
Douglas Gregorf9ff4b12009-03-09 23:48:35 +0000484 llvm::SmallVector<TemplateArgument, 16> InstantiatedTemplateArgs;
485 InstantiatedTemplateArgs.reserve(T->getNumArgs());
Douglas Gregordd13e842009-03-30 22:58:21 +0000486 for (TemplateSpecializationType::iterator Arg = T->begin(), ArgEnd = T->end();
Douglas Gregorf9ff4b12009-03-09 23:48:35 +0000487 Arg != ArgEnd; ++Arg) {
488 switch (Arg->getKind()) {
489 case TemplateArgument::Type: {
490 QualType T = SemaRef.InstantiateType(Arg->getAsType(),
Douglas Gregorf9e7d3d2009-05-11 23:53:27 +0000491 TemplateArgs,
Douglas Gregorf9ff4b12009-03-09 23:48:35 +0000492 Arg->getLocation(),
493 DeclarationName());
494 if (T.isNull())
495 return QualType();
496
497 InstantiatedTemplateArgs.push_back(
498 TemplateArgument(Arg->getLocation(), T));
499 break;
500 }
501
502 case TemplateArgument::Declaration:
503 case TemplateArgument::Integral:
504 InstantiatedTemplateArgs.push_back(*Arg);
505 break;
506
507 case TemplateArgument::Expression:
Douglas Gregor396f1142009-03-13 21:01:28 +0000508 Sema::OwningExprResult E
Douglas Gregorf9e7d3d2009-05-11 23:53:27 +0000509 = SemaRef.InstantiateExpr(Arg->getAsExpr(), TemplateArgs);
Douglas Gregor396f1142009-03-13 21:01:28 +0000510 if (E.isInvalid())
511 return QualType();
Anders Carlssonc154a722009-05-01 19:30:39 +0000512 InstantiatedTemplateArgs.push_back(E.takeAs<Expr>());
Douglas Gregorf9ff4b12009-03-09 23:48:35 +0000513 break;
514 }
515 }
516
Mike Stumpe127ae32009-05-16 07:39:55 +0000517 // FIXME: We're missing the locations of the template name, '<', and '>'.
Douglas Gregor15a92852009-03-31 18:38:02 +0000518
519 TemplateName Name = SemaRef.InstantiateTemplateName(T->getTemplateName(),
520 Loc,
Douglas Gregorf9e7d3d2009-05-11 23:53:27 +0000521 TemplateArgs);
Douglas Gregor15a92852009-03-31 18:38:02 +0000522
523 return SemaRef.CheckTemplateIdType(Name, Loc, SourceLocation(),
Douglas Gregordd13e842009-03-30 22:58:21 +0000524 &InstantiatedTemplateArgs[0],
525 InstantiatedTemplateArgs.size(),
526 SourceLocation());
Douglas Gregor74296542009-02-27 19:31:52 +0000527}
528
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000529QualType
530TemplateTypeInstantiator::
Douglas Gregor734b4ba2009-03-19 00:18:19 +0000531InstantiateQualifiedNameType(const QualifiedNameType *T,
532 unsigned Quals) const {
Douglas Gregord3022602009-03-27 23:10:48 +0000533 // When we instantiated a qualified name type, there's no point in
534 // keeping the qualification around in the instantiated result. So,
535 // just instantiate the named type.
536 return (*this)(T->getNamedType());
537}
538
539QualType
540TemplateTypeInstantiator::
541InstantiateTypenameType(const TypenameType *T, unsigned Quals) const {
Douglas Gregor77da5802009-04-01 00:28:59 +0000542 if (const TemplateSpecializationType *TemplateId = T->getTemplateId()) {
543 // When the typename type refers to a template-id, the template-id
544 // is dependent and has enough information to instantiate the
545 // result of the typename type. Since we don't care about keeping
546 // the spelling of the typename type in template instantiations,
547 // we just instantiate the template-id.
548 return InstantiateTemplateSpecializationType(TemplateId, Quals);
549 }
550
Douglas Gregord3022602009-03-27 23:10:48 +0000551 NestedNameSpecifier *NNS
552 = SemaRef.InstantiateNestedNameSpecifier(T->getQualifier(),
553 SourceRange(Loc),
Douglas Gregorf9e7d3d2009-05-11 23:53:27 +0000554 TemplateArgs);
Douglas Gregord3022602009-03-27 23:10:48 +0000555 if (!NNS)
556 return QualType();
557
Douglas Gregor77da5802009-04-01 00:28:59 +0000558 return SemaRef.CheckTypenameType(NNS, *T->getIdentifier(), SourceRange(Loc));
Douglas Gregor734b4ba2009-03-19 00:18:19 +0000559}
560
561QualType
562TemplateTypeInstantiator::
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000563InstantiateObjCInterfaceType(const ObjCInterfaceType *T,
564 unsigned Quals) const {
565 assert(false && "Objective-C types cannot be dependent");
Douglas Gregor74296542009-02-27 19:31:52 +0000566 return QualType();
567}
568
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000569QualType
570TemplateTypeInstantiator::
571InstantiateObjCQualifiedInterfaceType(const ObjCQualifiedInterfaceType *T,
572 unsigned Quals) const {
573 assert(false && "Objective-C types cannot be dependent");
Douglas Gregor74296542009-02-27 19:31:52 +0000574 return QualType();
575}
576
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000577QualType
578TemplateTypeInstantiator::
579InstantiateObjCQualifiedIdType(const ObjCQualifiedIdType *T,
580 unsigned Quals) const {
581 assert(false && "Objective-C types cannot be dependent");
Douglas Gregor74296542009-02-27 19:31:52 +0000582 return QualType();
583}
584
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000585/// \brief The actual implementation of Sema::InstantiateType().
586QualType TemplateTypeInstantiator::Instantiate(QualType T) const {
587 // If T is not a dependent type, there is nothing to do.
588 if (!T->isDependentType())
589 return T;
590
591 switch (T->getTypeClass()) {
592#define TYPE(Class, Base) \
593 case Type::Class: \
594 return Instantiate##Class##Type(cast<Class##Type>(T.getTypePtr()), \
595 T.getCVRQualifiers());
596#define ABSTRACT_TYPE(Class, Base)
597#include "clang/AST/TypeNodes.def"
598 }
599
600 assert(false && "Not all types have been decoded for instantiation");
601 return QualType();
602}
Douglas Gregor74296542009-02-27 19:31:52 +0000603
604/// \brief Instantiate the type T with a given set of template arguments.
605///
606/// This routine substitutes the given template arguments into the
607/// type T and produces the instantiated type.
608///
609/// \param T the type into which the template arguments will be
610/// substituted. If this type is not dependent, it will be returned
611/// immediately.
612///
613/// \param TemplateArgs the template arguments that will be
614/// substituted for the top-level template parameters within T.
615///
Douglas Gregor74296542009-02-27 19:31:52 +0000616/// \param Loc the location in the source code where this substitution
617/// is being performed. It will typically be the location of the
618/// declarator (if we're instantiating the type of some declaration)
619/// or the location of the type in the source code (if, e.g., we're
620/// instantiating the type of a cast expression).
621///
622/// \param Entity the name of the entity associated with a declaration
623/// being instantiated (if any). May be empty to indicate that there
624/// is no such entity (if, e.g., this is a type that occurs as part of
625/// a cast expression) or that the entity has no name (e.g., an
626/// unnamed function parameter).
627///
628/// \returns If the instantiation succeeds, the instantiated
629/// type. Otherwise, produces diagnostics and returns a NULL type.
630QualType Sema::InstantiateType(QualType T,
Douglas Gregorf9e7d3d2009-05-11 23:53:27 +0000631 const TemplateArgumentList &TemplateArgs,
Douglas Gregor74296542009-02-27 19:31:52 +0000632 SourceLocation Loc, DeclarationName Entity) {
Douglas Gregor56d25a72009-03-10 20:44:00 +0000633 assert(!ActiveTemplateInstantiations.empty() &&
634 "Cannot perform an instantiation without some context on the "
635 "instantiation stack");
636
Douglas Gregor74296542009-02-27 19:31:52 +0000637 // If T is not a dependent type, there is nothing to do.
638 if (!T->isDependentType())
639 return T;
640
Douglas Gregorf9e7d3d2009-05-11 23:53:27 +0000641 TemplateTypeInstantiator Instantiator(*this, TemplateArgs, Loc, Entity);
Douglas Gregorf57dcd02009-02-28 00:25:32 +0000642 return Instantiator(T);
Douglas Gregor74296542009-02-27 19:31:52 +0000643}
Douglas Gregored3a3982009-03-03 04:44:36 +0000644
645/// \brief Instantiate the base class specifiers of the given class
646/// template specialization.
647///
648/// Produces a diagnostic and returns true on error, returns false and
649/// attaches the instantiated base classes to the class template
650/// specialization if successful.
651bool
Douglas Gregorcc887972009-03-25 21:17:03 +0000652Sema::InstantiateBaseSpecifiers(CXXRecordDecl *Instantiation,
653 CXXRecordDecl *Pattern,
Douglas Gregorf9e7d3d2009-05-11 23:53:27 +0000654 const TemplateArgumentList &TemplateArgs) {
Douglas Gregored3a3982009-03-03 04:44:36 +0000655 bool Invalid = false;
656 llvm::SmallVector<CXXBaseSpecifier*, 8> InstantiatedBases;
Douglas Gregorcc887972009-03-25 21:17:03 +0000657 for (ClassTemplateSpecializationDecl::base_class_iterator
658 Base = Pattern->bases_begin(), BaseEnd = Pattern->bases_end();
Douglas Gregord9572a12009-03-10 18:52:44 +0000659 Base != BaseEnd; ++Base) {
Douglas Gregored3a3982009-03-03 04:44:36 +0000660 if (!Base->getType()->isDependentType()) {
661 // FIXME: Allocate via ASTContext
662 InstantiatedBases.push_back(new CXXBaseSpecifier(*Base));
663 continue;
664 }
665
666 QualType BaseType = InstantiateType(Base->getType(),
Douglas Gregorf9e7d3d2009-05-11 23:53:27 +0000667 TemplateArgs,
Douglas Gregored3a3982009-03-03 04:44:36 +0000668 Base->getSourceRange().getBegin(),
669 DeclarationName());
670 if (BaseType.isNull()) {
671 Invalid = true;
672 continue;
673 }
674
675 if (CXXBaseSpecifier *InstantiatedBase
Douglas Gregorcc887972009-03-25 21:17:03 +0000676 = CheckBaseSpecifier(Instantiation,
Douglas Gregored3a3982009-03-03 04:44:36 +0000677 Base->getSourceRange(),
678 Base->isVirtual(),
679 Base->getAccessSpecifierAsWritten(),
680 BaseType,
681 /*FIXME: Not totally accurate */
682 Base->getSourceRange().getBegin()))
683 InstantiatedBases.push_back(InstantiatedBase);
684 else
685 Invalid = true;
686 }
687
Douglas Gregord9572a12009-03-10 18:52:44 +0000688 if (!Invalid &&
Douglas Gregorcc887972009-03-25 21:17:03 +0000689 AttachBaseSpecifiers(Instantiation, &InstantiatedBases[0],
Douglas Gregored3a3982009-03-03 04:44:36 +0000690 InstantiatedBases.size()))
691 Invalid = true;
692
693 return Invalid;
694}
695
Douglas Gregorcc887972009-03-25 21:17:03 +0000696/// \brief Instantiate the definition of a class from a given pattern.
697///
698/// \param PointOfInstantiation The point of instantiation within the
699/// source code.
700///
701/// \param Instantiation is the declaration whose definition is being
702/// instantiated. This will be either a class template specialization
703/// or a member class of a class template specialization.
704///
705/// \param Pattern is the pattern from which the instantiation
706/// occurs. This will be either the declaration of a class template or
707/// the declaration of a member class of a class template.
708///
709/// \param TemplateArgs The template arguments to be substituted into
710/// the pattern.
711///
Douglas Gregorcc887972009-03-25 21:17:03 +0000712/// \returns true if an error occurred, false otherwise.
713bool
714Sema::InstantiateClass(SourceLocation PointOfInstantiation,
715 CXXRecordDecl *Instantiation, CXXRecordDecl *Pattern,
Douglas Gregorfd79ac62009-05-13 00:25:59 +0000716 const TemplateArgumentList &TemplateArgs,
717 bool ExplicitInstantiation) {
Douglas Gregorcc887972009-03-25 21:17:03 +0000718 bool Invalid = false;
719
720 CXXRecordDecl *PatternDef
721 = cast_or_null<CXXRecordDecl>(Pattern->getDefinition(Context));
722 if (!PatternDef) {
723 if (Pattern == Instantiation->getInstantiatedFromMemberClass()) {
724 Diag(PointOfInstantiation,
725 diag::err_implicit_instantiate_member_undefined)
726 << Context.getTypeDeclType(Instantiation);
727 Diag(Pattern->getLocation(), diag::note_member_of_template_here);
728 } else {
Douglas Gregorfd79ac62009-05-13 00:25:59 +0000729 Diag(PointOfInstantiation, diag::err_template_instantiate_undefined)
730 << ExplicitInstantiation
Douglas Gregorcc887972009-03-25 21:17:03 +0000731 << Context.getTypeDeclType(Instantiation);
732 Diag(Pattern->getLocation(), diag::note_template_decl_here);
733 }
734 return true;
735 }
736 Pattern = PatternDef;
737
Douglas Gregor42c48522009-03-25 21:23:52 +0000738 InstantiatingTemplate Inst(*this, PointOfInstantiation, Instantiation);
Douglas Gregorcc887972009-03-25 21:17:03 +0000739 if (Inst)
740 return true;
741
742 // Enter the scope of this instantiation. We don't use
743 // PushDeclContext because we don't have a scope.
744 DeclContext *PreviousContext = CurContext;
745 CurContext = Instantiation;
746
747 // Start the definition of this instantiation.
748 Instantiation->startDefinition();
749
750 // Instantiate the base class specifiers.
Douglas Gregorf9e7d3d2009-05-11 23:53:27 +0000751 if (InstantiateBaseSpecifiers(Instantiation, Pattern, TemplateArgs))
Douglas Gregorcc887972009-03-25 21:17:03 +0000752 Invalid = true;
753
Chris Lattner5261d0c2009-03-28 19:18:32 +0000754 llvm::SmallVector<DeclPtrTy, 32> Fields;
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000755 for (RecordDecl::decl_iterator Member = Pattern->decls_begin(Context),
756 MemberEnd = Pattern->decls_end(Context);
757 Member != MemberEnd; ++Member) {
Douglas Gregorf9e7d3d2009-05-11 23:53:27 +0000758 Decl *NewMember = InstantiateDecl(*Member, Instantiation, TemplateArgs);
Douglas Gregorcc887972009-03-25 21:17:03 +0000759 if (NewMember) {
760 if (NewMember->isInvalidDecl())
761 Invalid = true;
762 else if (FieldDecl *Field = dyn_cast<FieldDecl>(NewMember))
Chris Lattner5261d0c2009-03-28 19:18:32 +0000763 Fields.push_back(DeclPtrTy::make(Field));
Douglas Gregorcc887972009-03-25 21:17:03 +0000764 } else {
765 // FIXME: Eventually, a NULL return will mean that one of the
Mike Stumpe127ae32009-05-16 07:39:55 +0000766 // instantiations was a semantic disaster, and we'll want to set Invalid =
767 // true. For now, we expect to skip some members that we can't yet handle.
Douglas Gregorcc887972009-03-25 21:17:03 +0000768 }
769 }
770
771 // Finish checking fields.
Chris Lattner5261d0c2009-03-28 19:18:32 +0000772 ActOnFields(0, Instantiation->getLocation(), DeclPtrTy::make(Instantiation),
Douglas Gregorcc887972009-03-25 21:17:03 +0000773 &Fields[0], Fields.size(), SourceLocation(), SourceLocation(),
774 0);
775
776 // Add any implicitly-declared members that we might need.
777 AddImplicitlyDeclaredMembersToClass(Instantiation);
778
779 // Exit the scope of this instantiation.
780 CurContext = PreviousContext;
781
Douglas Gregordf9f5d12009-05-13 20:28:22 +0000782 // If this is an explicit instantiation, instantiate our members, too.
Douglas Gregorb12249d2009-05-18 17:01:57 +0000783 if (!Invalid && ExplicitInstantiation) {
784 Inst.Clear();
Douglas Gregordf9f5d12009-05-13 20:28:22 +0000785 InstantiateClassMembers(PointOfInstantiation, Instantiation, TemplateArgs);
Douglas Gregorb12249d2009-05-18 17:01:57 +0000786 }
Douglas Gregordf9f5d12009-05-13 20:28:22 +0000787
Douglas Gregorcc887972009-03-25 21:17:03 +0000788 return Invalid;
789}
790
Douglas Gregored3a3982009-03-03 04:44:36 +0000791bool
792Sema::InstantiateClassTemplateSpecialization(
793 ClassTemplateSpecializationDecl *ClassTemplateSpec,
794 bool ExplicitInstantiation) {
795 // Perform the actual instantiation on the canonical declaration.
796 ClassTemplateSpec = cast<ClassTemplateSpecializationDecl>(
797 Context.getCanonicalDecl(ClassTemplateSpec));
798
799 // We can only instantiate something that hasn't already been
800 // instantiated or specialized. Fail without any diagnostics: our
801 // caller will provide an error message.
802 if (ClassTemplateSpec->getSpecializationKind() != TSK_Undeclared)
803 return true;
804
Mike Stumpe127ae32009-05-16 07:39:55 +0000805 // FIXME: Push this class template instantiation onto the instantiation stack,
806 // checking for recursion that exceeds a certain depth.
Douglas Gregored3a3982009-03-03 04:44:36 +0000807
Mike Stumpe127ae32009-05-16 07:39:55 +0000808 // FIXME: Perform class template partial specialization to select the best
809 // template.
Douglas Gregored3a3982009-03-03 04:44:36 +0000810 ClassTemplateDecl *Template = ClassTemplateSpec->getSpecializedTemplate();
811
Douglas Gregorcc887972009-03-25 21:17:03 +0000812 CXXRecordDecl *Pattern = Template->getTemplatedDecl();
Douglas Gregored3a3982009-03-03 04:44:36 +0000813
814 // Note that this is an instantiation.
815 ClassTemplateSpec->setSpecializationKind(
816 ExplicitInstantiation? TSK_ExplicitInstantiation
817 : TSK_ImplicitInstantiation);
818
Douglas Gregorcc887972009-03-25 21:17:03 +0000819 return InstantiateClass(ClassTemplateSpec->getLocation(),
820 ClassTemplateSpec, Pattern,
Douglas Gregorfd79ac62009-05-13 00:25:59 +0000821 ClassTemplateSpec->getTemplateArgs(),
822 ExplicitInstantiation);
Douglas Gregored3a3982009-03-03 04:44:36 +0000823}
Douglas Gregor47bde7c2009-03-19 17:26:29 +0000824
Douglas Gregordf9f5d12009-05-13 20:28:22 +0000825/// \brief Instantiate the definitions of all of the member of the
826/// given class, which is an instantiation of a class template or a
827/// member class of a template.
828void
829Sema::InstantiateClassMembers(SourceLocation PointOfInstantiation,
830 CXXRecordDecl *Instantiation,
831 const TemplateArgumentList &TemplateArgs) {
832 for (DeclContext::decl_iterator D = Instantiation->decls_begin(Context),
833 DEnd = Instantiation->decls_end(Context);
834 D != DEnd; ++D) {
835 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(*D)) {
836 if (!Function->getBody(Context))
Douglas Gregorb12249d2009-05-18 17:01:57 +0000837 InstantiateFunctionDefinition(PointOfInstantiation, Function);
Douglas Gregordf9f5d12009-05-13 20:28:22 +0000838 } else if (VarDecl *Var = dyn_cast<VarDecl>(*D)) {
839 const VarDecl *Def = 0;
840 if (!Var->getDefinition(Def))
841 InstantiateVariableDefinition(Var);
842 } else if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(*D)) {
843 if (!Record->isInjectedClassName() && !Record->getDefinition(Context)) {
844 assert(Record->getInstantiatedFromMemberClass() &&
845 "Missing instantiated-from-template information");
Douglas Gregorb12249d2009-05-18 17:01:57 +0000846 InstantiateClass(PointOfInstantiation, Record,
Douglas Gregordf9f5d12009-05-13 20:28:22 +0000847 Record->getInstantiatedFromMemberClass(),
848 TemplateArgs, true);
849 }
850 }
851 }
852}
853
854/// \brief Instantiate the definitions of all of the members of the
855/// given class template specialization, which was named as part of an
856/// explicit instantiation.
857void Sema::InstantiateClassTemplateSpecializationMembers(
858 SourceLocation PointOfInstantiation,
859 ClassTemplateSpecializationDecl *ClassTemplateSpec) {
860 // C++0x [temp.explicit]p7:
861 // An explicit instantiation that names a class template
862 // specialization is an explicit instantion of the same kind
863 // (declaration or definition) of each of its members (not
864 // including members inherited from base classes) that has not
865 // been previously explicitly specialized in the translation unit
866 // containing the explicit instantiation, except as described
867 // below.
868 InstantiateClassMembers(PointOfInstantiation, ClassTemplateSpec,
869 ClassTemplateSpec->getTemplateArgs());
870}
871
Douglas Gregor1e589cc2009-03-26 23:50:42 +0000872/// \brief Instantiate a nested-name-specifier.
873NestedNameSpecifier *
874Sema::InstantiateNestedNameSpecifier(NestedNameSpecifier *NNS,
875 SourceRange Range,
Douglas Gregorf9e7d3d2009-05-11 23:53:27 +0000876 const TemplateArgumentList &TemplateArgs) {
Douglas Gregor1e589cc2009-03-26 23:50:42 +0000877 // Instantiate the prefix of this nested name specifier.
878 NestedNameSpecifier *Prefix = NNS->getPrefix();
879 if (Prefix) {
Douglas Gregorf9e7d3d2009-05-11 23:53:27 +0000880 Prefix = InstantiateNestedNameSpecifier(Prefix, Range, TemplateArgs);
Douglas Gregor1e589cc2009-03-26 23:50:42 +0000881 if (!Prefix)
882 return 0;
Douglas Gregor47bde7c2009-03-19 17:26:29 +0000883 }
884
Douglas Gregor1e589cc2009-03-26 23:50:42 +0000885 switch (NNS->getKind()) {
Douglas Gregor77da5802009-04-01 00:28:59 +0000886 case NestedNameSpecifier::Identifier: {
887 assert(Prefix &&
888 "Can't have an identifier nested-name-specifier with no prefix");
889 CXXScopeSpec SS;
890 // FIXME: The source location information is all wrong.
891 SS.setRange(Range);
892 SS.setScopeRep(Prefix);
893 return static_cast<NestedNameSpecifier *>(
894 ActOnCXXNestedNameSpecifier(0, SS,
895 Range.getEnd(),
896 Range.getEnd(),
Douglas Gregor96b6df92009-05-14 00:28:11 +0000897 *NNS->getAsIdentifier()));
Douglas Gregor1e589cc2009-03-26 23:50:42 +0000898 break;
Douglas Gregor77da5802009-04-01 00:28:59 +0000899 }
Douglas Gregor1e589cc2009-03-26 23:50:42 +0000900
901 case NestedNameSpecifier::Namespace:
902 case NestedNameSpecifier::Global:
903 return NNS;
904
905 case NestedNameSpecifier::TypeSpecWithTemplate:
906 case NestedNameSpecifier::TypeSpec: {
907 QualType T = QualType(NNS->getAsType(), 0);
908 if (!T->isDependentType())
909 return NNS;
910
Douglas Gregorf9e7d3d2009-05-11 23:53:27 +0000911 T = InstantiateType(T, TemplateArgs, Range.getBegin(), DeclarationName());
Douglas Gregor1e589cc2009-03-26 23:50:42 +0000912 if (T.isNull())
913 return 0;
914
Douglas Gregord3022602009-03-27 23:10:48 +0000915 if (T->isRecordType() ||
916 (getLangOptions().CPlusPlus0x && T->isEnumeralType())) {
Douglas Gregor77da5802009-04-01 00:28:59 +0000917 assert(T.getCVRQualifiers() == 0 && "Can't get cv-qualifiers here");
Douglas Gregord3022602009-03-27 23:10:48 +0000918 return NestedNameSpecifier::Create(Context, Prefix,
Douglas Gregor1e589cc2009-03-26 23:50:42 +0000919 NNS->getKind() == NestedNameSpecifier::TypeSpecWithTemplate,
Douglas Gregord3022602009-03-27 23:10:48 +0000920 T.getTypePtr());
921 }
922
923 Diag(Range.getBegin(), diag::err_nested_name_spec_non_tag) << T;
924 return 0;
Douglas Gregor1e589cc2009-03-26 23:50:42 +0000925 }
926 }
927
Douglas Gregord3022602009-03-27 23:10:48 +0000928 // Required to silence a GCC warning
Douglas Gregor1e589cc2009-03-26 23:50:42 +0000929 return 0;
Douglas Gregor47bde7c2009-03-19 17:26:29 +0000930}
Douglas Gregor15a92852009-03-31 18:38:02 +0000931
932TemplateName
933Sema::InstantiateTemplateName(TemplateName Name, SourceLocation Loc,
Douglas Gregorf9e7d3d2009-05-11 23:53:27 +0000934 const TemplateArgumentList &TemplateArgs) {
Douglas Gregor15a92852009-03-31 18:38:02 +0000935 if (TemplateTemplateParmDecl *TTP
936 = dyn_cast_or_null<TemplateTemplateParmDecl>(
937 Name.getAsTemplateDecl())) {
938 assert(TTP->getDepth() == 0 &&
939 "Cannot reduce depth of a template template parameter");
Douglas Gregoraed98042009-03-31 20:22:05 +0000940 assert(TemplateArgs[TTP->getPosition()].getAsDecl() &&
Douglas Gregor15a92852009-03-31 18:38:02 +0000941 "Wrong kind of template template argument");
942 ClassTemplateDecl *ClassTemplate
943 = dyn_cast<ClassTemplateDecl>(
944 TemplateArgs[TTP->getPosition()].getAsDecl());
Douglas Gregoraed98042009-03-31 20:22:05 +0000945 assert(ClassTemplate && "Expected a class template");
Douglas Gregor15a92852009-03-31 18:38:02 +0000946 if (QualifiedTemplateName *QTN = Name.getAsQualifiedTemplateName()) {
947 NestedNameSpecifier *NNS
948 = InstantiateNestedNameSpecifier(QTN->getQualifier(),
949 /*FIXME=*/SourceRange(Loc),
Douglas Gregorf9e7d3d2009-05-11 23:53:27 +0000950 TemplateArgs);
Douglas Gregor15a92852009-03-31 18:38:02 +0000951 if (NNS)
952 return Context.getQualifiedTemplateName(NNS,
953 QTN->hasTemplateKeyword(),
954 ClassTemplate);
955 }
956
957 return TemplateName(ClassTemplate);
958 } else if (DependentTemplateName *DTN = Name.getAsDependentTemplateName()) {
959 NestedNameSpecifier *NNS
960 = InstantiateNestedNameSpecifier(DTN->getQualifier(),
961 /*FIXME=*/SourceRange(Loc),
Douglas Gregorf9e7d3d2009-05-11 23:53:27 +0000962 TemplateArgs);
Douglas Gregor15a92852009-03-31 18:38:02 +0000963
964 if (!NNS) // FIXME: Not the best recovery strategy.
965 return Name;
966
967 if (NNS->isDependent())
968 return Context.getDependentTemplateName(NNS, DTN->getName());
969
970 // Somewhat redundant with ActOnDependentTemplateName.
971 CXXScopeSpec SS;
972 SS.setRange(SourceRange(Loc));
973 SS.setScopeRep(NNS);
974 TemplateTy Template;
975 TemplateNameKind TNK = isTemplateName(*DTN->getName(), 0, Template, &SS);
976 if (TNK == TNK_Non_template) {
977 Diag(Loc, diag::err_template_kw_refers_to_non_template)
978 << DTN->getName();
979 return Name;
980 } else if (TNK == TNK_Function_template) {
981 Diag(Loc, diag::err_template_kw_refers_to_non_template)
982 << DTN->getName();
983 return Name;
984 }
985
986 return Template.getAsVal<TemplateName>();
987 }
988
989
990
Mike Stumpe127ae32009-05-16 07:39:55 +0000991 // FIXME: Even if we're referring to a Decl that isn't a template template
992 // parameter, we may need to instantiate the outer contexts of that
993 // Decl. However, this won't be needed until we implement member templates.
Douglas Gregor15a92852009-03-31 18:38:02 +0000994 return Name;
995}