blob: cb43f1c6a0d856fa9643dcee63a56a9b3efa7e6b [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) {
Douglas Gregor1637be72009-06-26 00:10:03 +000032 // Template arguments for a class template specialization.
Douglas Gregor54dabfc2009-05-14 23:26:13 +000033 if (ClassTemplateSpecializationDecl *Spec
34 = dyn_cast<ClassTemplateSpecializationDecl>(D))
35 return Spec->getTemplateArgs();
36
Douglas Gregor1637be72009-06-26 00:10:03 +000037 // Template arguments for a function template specialization.
38 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D))
39 if (const TemplateArgumentList *TemplateArgs
40 = Function->getTemplateSpecializationArgs())
41 return *TemplateArgs;
42
43 // Template arguments for a member of a class template specialization.
Douglas Gregor54dabfc2009-05-14 23:26:13 +000044 DeclContext *EnclosingTemplateCtx = D->getDeclContext();
45 while (!isa<ClassTemplateSpecializationDecl>(EnclosingTemplateCtx)) {
46 assert(!EnclosingTemplateCtx->isFileContext() &&
47 "Tried to get the instantiation arguments of a non-template");
48 EnclosingTemplateCtx = EnclosingTemplateCtx->getParent();
49 }
50
51 ClassTemplateSpecializationDecl *EnclosingTemplate
52 = cast<ClassTemplateSpecializationDecl>(EnclosingTemplateCtx);
53 return EnclosingTemplate->getTemplateArgs();
54}
55
Douglas Gregor26dce442009-03-10 00:06:19 +000056Sema::InstantiatingTemplate::
57InstantiatingTemplate(Sema &SemaRef, SourceLocation PointOfInstantiation,
Douglas Gregorf3e7ce42009-05-18 17:01:57 +000058 Decl *Entity,
Douglas Gregor26dce442009-03-10 00:06:19 +000059 SourceRange InstantiationRange)
60 : SemaRef(SemaRef) {
Douglas Gregordf667e72009-03-10 20:44:00 +000061
62 Invalid = CheckInstantiationDepth(PointOfInstantiation,
63 InstantiationRange);
64 if (!Invalid) {
Douglas Gregor26dce442009-03-10 00:06:19 +000065 ActiveTemplateInstantiation Inst;
Douglas Gregordf667e72009-03-10 20:44:00 +000066 Inst.Kind = ActiveTemplateInstantiation::TemplateInstantiation;
Douglas Gregor26dce442009-03-10 00:06:19 +000067 Inst.PointOfInstantiation = PointOfInstantiation;
Douglas Gregordf667e72009-03-10 20:44:00 +000068 Inst.Entity = reinterpret_cast<uintptr_t>(Entity);
Douglas Gregor313a81d2009-03-12 18:36:18 +000069 Inst.TemplateArgs = 0;
70 Inst.NumTemplateArgs = 0;
Douglas Gregordf667e72009-03-10 20:44:00 +000071 Inst.InstantiationRange = InstantiationRange;
72 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
73 Invalid = false;
74 }
75}
76
77Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef,
78 SourceLocation PointOfInstantiation,
79 TemplateDecl *Template,
80 const TemplateArgument *TemplateArgs,
81 unsigned NumTemplateArgs,
82 SourceRange InstantiationRange)
83 : SemaRef(SemaRef) {
84
85 Invalid = CheckInstantiationDepth(PointOfInstantiation,
86 InstantiationRange);
87 if (!Invalid) {
88 ActiveTemplateInstantiation Inst;
89 Inst.Kind
90 = ActiveTemplateInstantiation::DefaultTemplateArgumentInstantiation;
91 Inst.PointOfInstantiation = PointOfInstantiation;
92 Inst.Entity = reinterpret_cast<uintptr_t>(Template);
93 Inst.TemplateArgs = TemplateArgs;
94 Inst.NumTemplateArgs = NumTemplateArgs;
Douglas Gregor26dce442009-03-10 00:06:19 +000095 Inst.InstantiationRange = InstantiationRange;
96 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
97 Invalid = false;
98 }
99}
100
Douglas Gregor637a4092009-06-10 23:47:09 +0000101Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef,
102 SourceLocation PointOfInstantiation,
Douglas Gregorcca9e962009-07-01 22:01:06 +0000103 FunctionTemplateDecl *FunctionTemplate,
104 const TemplateArgument *TemplateArgs,
105 unsigned NumTemplateArgs,
106 ActiveTemplateInstantiation::InstantiationKind Kind,
107 SourceRange InstantiationRange)
108: SemaRef(SemaRef) {
109
110 Invalid = CheckInstantiationDepth(PointOfInstantiation,
111 InstantiationRange);
112 if (!Invalid) {
113 ActiveTemplateInstantiation Inst;
114 Inst.Kind = Kind;
115 Inst.PointOfInstantiation = PointOfInstantiation;
116 Inst.Entity = reinterpret_cast<uintptr_t>(FunctionTemplate);
117 Inst.TemplateArgs = TemplateArgs;
118 Inst.NumTemplateArgs = NumTemplateArgs;
119 Inst.InstantiationRange = InstantiationRange;
120 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
121 Invalid = false;
122 }
123}
124
125Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef,
126 SourceLocation PointOfInstantiation,
Douglas Gregor637a4092009-06-10 23:47:09 +0000127 ClassTemplatePartialSpecializationDecl *PartialSpec,
128 const TemplateArgument *TemplateArgs,
129 unsigned NumTemplateArgs,
130 SourceRange InstantiationRange)
131 : SemaRef(SemaRef) {
132
133 Invalid = CheckInstantiationDepth(PointOfInstantiation,
134 InstantiationRange);
135 if (!Invalid) {
136 ActiveTemplateInstantiation Inst;
137 Inst.Kind
Douglas Gregorcca9e962009-07-01 22:01:06 +0000138 = ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution;
Douglas Gregor637a4092009-06-10 23:47:09 +0000139 Inst.PointOfInstantiation = PointOfInstantiation;
140 Inst.Entity = reinterpret_cast<uintptr_t>(PartialSpec);
141 Inst.TemplateArgs = TemplateArgs;
142 Inst.NumTemplateArgs = NumTemplateArgs;
143 Inst.InstantiationRange = InstantiationRange;
144 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
145 Invalid = false;
146 }
147}
148
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000149void Sema::InstantiatingTemplate::Clear() {
150 if (!Invalid) {
Douglas Gregor26dce442009-03-10 00:06:19 +0000151 SemaRef.ActiveTemplateInstantiations.pop_back();
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000152 Invalid = true;
153 }
Douglas Gregor26dce442009-03-10 00:06:19 +0000154}
155
Douglas Gregordf667e72009-03-10 20:44:00 +0000156bool Sema::InstantiatingTemplate::CheckInstantiationDepth(
157 SourceLocation PointOfInstantiation,
158 SourceRange InstantiationRange) {
159 if (SemaRef.ActiveTemplateInstantiations.size()
160 <= SemaRef.getLangOptions().InstantiationDepth)
161 return false;
162
163 SemaRef.Diag(PointOfInstantiation,
164 diag::err_template_recursion_depth_exceeded)
165 << SemaRef.getLangOptions().InstantiationDepth
166 << InstantiationRange;
167 SemaRef.Diag(PointOfInstantiation, diag::note_template_recursion_depth)
168 << SemaRef.getLangOptions().InstantiationDepth;
169 return true;
170}
171
Douglas Gregoree1828a2009-03-10 18:03:33 +0000172/// \brief Prints the current instantiation stack through a series of
173/// notes.
174void Sema::PrintInstantiationStack() {
Douglas Gregorcca9e962009-07-01 22:01:06 +0000175 // FIXME: In all of these cases, we need to show the template arguments
Douglas Gregoree1828a2009-03-10 18:03:33 +0000176 for (llvm::SmallVector<ActiveTemplateInstantiation, 16>::reverse_iterator
177 Active = ActiveTemplateInstantiations.rbegin(),
178 ActiveEnd = ActiveTemplateInstantiations.rend();
179 Active != ActiveEnd;
180 ++Active) {
Douglas Gregordf667e72009-03-10 20:44:00 +0000181 switch (Active->Kind) {
182 case ActiveTemplateInstantiation::TemplateInstantiation: {
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000183 Decl *D = reinterpret_cast<Decl *>(Active->Entity);
184 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) {
185 unsigned DiagID = diag::note_template_member_class_here;
186 if (isa<ClassTemplateSpecializationDecl>(Record))
187 DiagID = diag::note_template_class_instantiation_here;
188 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
189 DiagID)
190 << Context.getTypeDeclType(Record)
191 << Active->InstantiationRange;
Douglas Gregor7caa6822009-07-24 20:34:43 +0000192 } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
Douglas Gregor1637be72009-06-26 00:10:03 +0000193 unsigned DiagID;
194 if (Function->getPrimaryTemplate())
195 DiagID = diag::note_function_template_spec_here;
196 else
197 DiagID = diag::note_template_member_function_here;
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000198 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
199 DiagID)
200 << Function
201 << Active->InstantiationRange;
Douglas Gregor7caa6822009-07-24 20:34:43 +0000202 } else {
203 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
204 diag::note_template_static_data_member_def_here)
205 << cast<VarDecl>(D)
206 << Active->InstantiationRange;
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000207 }
Douglas Gregordf667e72009-03-10 20:44:00 +0000208 break;
209 }
210
211 case ActiveTemplateInstantiation::DefaultTemplateArgumentInstantiation: {
212 TemplateDecl *Template = cast<TemplateDecl>((Decl *)Active->Entity);
213 std::string TemplateArgsStr
Douglas Gregor7532dc62009-03-30 22:58:21 +0000214 = TemplateSpecializationType::PrintTemplateArgumentList(
Douglas Gregorcca9e962009-07-01 22:01:06 +0000215 Active->TemplateArgs,
Douglas Gregord249e1d1f2009-05-29 20:38:28 +0000216 Active->NumTemplateArgs,
217 Context.PrintingPolicy);
Douglas Gregordf667e72009-03-10 20:44:00 +0000218 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
219 diag::note_default_arg_instantiation_here)
220 << (Template->getNameAsString() + TemplateArgsStr)
221 << Active->InstantiationRange;
222 break;
223 }
Douglas Gregor637a4092009-06-10 23:47:09 +0000224
Douglas Gregorcca9e962009-07-01 22:01:06 +0000225 case ActiveTemplateInstantiation::ExplicitTemplateArgumentSubstitution: {
226 FunctionTemplateDecl *FnTmpl
227 = cast<FunctionTemplateDecl>((Decl *)Active->Entity);
Douglas Gregor637a4092009-06-10 23:47:09 +0000228 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
Douglas Gregorcca9e962009-07-01 22:01:06 +0000229 diag::note_explicit_template_arg_substitution_here)
230 << FnTmpl << Active->InstantiationRange;
Douglas Gregor637a4092009-06-10 23:47:09 +0000231 break;
232 }
Douglas Gregorcca9e962009-07-01 22:01:06 +0000233
234 case ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution:
235 if (ClassTemplatePartialSpecializationDecl *PartialSpec
236 = dyn_cast<ClassTemplatePartialSpecializationDecl>(
237 (Decl *)Active->Entity)) {
238 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
239 diag::note_partial_spec_deduct_instantiation_here)
240 << Context.getTypeDeclType(PartialSpec)
241 << Active->InstantiationRange;
242 } else {
243 FunctionTemplateDecl *FnTmpl
244 = cast<FunctionTemplateDecl>((Decl *)Active->Entity);
245 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
246 diag::note_function_template_deduction_instantiation_here)
247 << FnTmpl << Active->InstantiationRange;
248 }
249 break;
Douglas Gregor637a4092009-06-10 23:47:09 +0000250
Douglas Gregordf667e72009-03-10 20:44:00 +0000251 }
Douglas Gregoree1828a2009-03-10 18:03:33 +0000252 }
253}
254
Douglas Gregor5e9f35c2009-06-14 07:33:30 +0000255bool Sema::isSFINAEContext() const {
256 using llvm::SmallVector;
257 for (SmallVector<ActiveTemplateInstantiation, 16>::const_reverse_iterator
258 Active = ActiveTemplateInstantiations.rbegin(),
259 ActiveEnd = ActiveTemplateInstantiations.rend();
260 Active != ActiveEnd;
261 ++Active) {
262
263 switch(Active->Kind) {
Douglas Gregorcca9e962009-07-01 22:01:06 +0000264 case ActiveTemplateInstantiation::TemplateInstantiation:
265 // This is a template instantiation, so there is no SFINAE.
266 return false;
267
Douglas Gregor5e9f35c2009-06-14 07:33:30 +0000268 case ActiveTemplateInstantiation::DefaultTemplateArgumentInstantiation:
269 // A default template argument instantiation may or may not be a
270 // SFINAE context; look further up the stack.
271 break;
Douglas Gregorcca9e962009-07-01 22:01:06 +0000272
273 case ActiveTemplateInstantiation::ExplicitTemplateArgumentSubstitution:
274 case ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution:
275 // We're either substitution explicitly-specified template arguments
276 // or deduced template arguments, so SFINAE applies.
277 return true;
Douglas Gregor5e9f35c2009-06-14 07:33:30 +0000278 }
279 }
280
281 return false;
282}
283
Douglas Gregor99ebf652009-02-27 19:31:52 +0000284//===----------------------------------------------------------------------===/
285// Template Instantiation for Types
286//===----------------------------------------------------------------------===/
Douglas Gregorcd281c32009-02-28 00:25:32 +0000287namespace {
288 class VISIBILITY_HIDDEN TemplateTypeInstantiator {
289 Sema &SemaRef;
Douglas Gregor7e063902009-05-11 23:53:27 +0000290 const TemplateArgumentList &TemplateArgs;
Douglas Gregorcd281c32009-02-28 00:25:32 +0000291 SourceLocation Loc;
292 DeclarationName Entity;
Douglas Gregor99ebf652009-02-27 19:31:52 +0000293
Douglas Gregorcd281c32009-02-28 00:25:32 +0000294 public:
295 TemplateTypeInstantiator(Sema &SemaRef,
Douglas Gregor7e063902009-05-11 23:53:27 +0000296 const TemplateArgumentList &TemplateArgs,
Douglas Gregorcd281c32009-02-28 00:25:32 +0000297 SourceLocation Loc,
298 DeclarationName Entity)
299 : SemaRef(SemaRef), TemplateArgs(TemplateArgs),
Douglas Gregor7e063902009-05-11 23:53:27 +0000300 Loc(Loc), Entity(Entity) { }
Douglas Gregorcd281c32009-02-28 00:25:32 +0000301
302 QualType operator()(QualType T) const { return Instantiate(T); }
303
304 QualType Instantiate(QualType T) const;
305
306 // Declare instantiate functions for each type.
307#define TYPE(Class, Base) \
Douglas Gregor8a5cb112009-06-26 21:40:05 +0000308 QualType Instantiate##Class##Type(const Class##Type *T) const;
Douglas Gregorcd281c32009-02-28 00:25:32 +0000309#define ABSTRACT_TYPE(Class, Base)
310#include "clang/AST/TypeNodes.def"
311 };
312}
313
314QualType
Douglas Gregor8a5cb112009-06-26 21:40:05 +0000315TemplateTypeInstantiator::InstantiateExtQualType(const ExtQualType *T) const {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000316 // FIXME: Implement this
317 assert(false && "Cannot instantiate ExtQualType yet");
318 return QualType();
319}
320
Douglas Gregorcd281c32009-02-28 00:25:32 +0000321QualType
Douglas Gregor8a5cb112009-06-26 21:40:05 +0000322TemplateTypeInstantiator::InstantiateBuiltinType(const BuiltinType *T) const {
Douglas Gregor724651c2009-02-28 01:04:19 +0000323 assert(false && "Builtin types are not dependent and cannot be instantiated");
Douglas Gregor8a5cb112009-06-26 21:40:05 +0000324 return QualType(T, 0);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000325}
326
Douglas Gregorcd281c32009-02-28 00:25:32 +0000327QualType
328TemplateTypeInstantiator::
Douglas Gregor8a5cb112009-06-26 21:40:05 +0000329InstantiateFixedWidthIntType(const FixedWidthIntType *T) const {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000330 // FIXME: Implement this
331 assert(false && "Cannot instantiate FixedWidthIntType yet");
332 return QualType();
333}
334
Douglas Gregorcd281c32009-02-28 00:25:32 +0000335QualType
Douglas Gregor8a5cb112009-06-26 21:40:05 +0000336TemplateTypeInstantiator::InstantiateComplexType(const ComplexType *T) const {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000337 // FIXME: Implement this
338 assert(false && "Cannot instantiate ComplexType yet");
339 return QualType();
340}
341
Douglas Gregorcd281c32009-02-28 00:25:32 +0000342QualType
Douglas Gregor8a5cb112009-06-26 21:40:05 +0000343TemplateTypeInstantiator::InstantiatePointerType(const PointerType *T) const {
Douglas Gregorcd281c32009-02-28 00:25:32 +0000344 QualType PointeeType = Instantiate(T->getPointeeType());
345 if (PointeeType.isNull())
346 return QualType();
347
Douglas Gregor8a5cb112009-06-26 21:40:05 +0000348 return SemaRef.BuildPointerType(PointeeType, 0, Loc, Entity);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000349}
350
Douglas Gregorcd281c32009-02-28 00:25:32 +0000351QualType
Douglas Gregor8a5cb112009-06-26 21:40:05 +0000352TemplateTypeInstantiator::InstantiateBlockPointerType(
353 const BlockPointerType *T) const {
Anders Carlsson859ba502009-06-12 16:23:10 +0000354 QualType PointeeType = Instantiate(T->getPointeeType());
355 if (PointeeType.isNull())
356 return QualType();
357
Douglas Gregor8a5cb112009-06-26 21:40:05 +0000358 return SemaRef.BuildBlockPointerType(PointeeType, 0, Loc, Entity);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000359}
360
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000361QualType
362TemplateTypeInstantiator::InstantiateLValueReferenceType(
Douglas Gregor8a5cb112009-06-26 21:40:05 +0000363 const LValueReferenceType *T) const {
Douglas Gregorcd281c32009-02-28 00:25:32 +0000364 QualType ReferentType = Instantiate(T->getPointeeType());
365 if (ReferentType.isNull())
366 return QualType();
367
Douglas Gregor8a5cb112009-06-26 21:40:05 +0000368 return SemaRef.BuildReferenceType(ReferentType, true, 0, Loc, Entity);
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000369}
370
371QualType
372TemplateTypeInstantiator::InstantiateRValueReferenceType(
Douglas Gregor8a5cb112009-06-26 21:40:05 +0000373 const RValueReferenceType *T) const {
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000374 QualType ReferentType = Instantiate(T->getPointeeType());
375 if (ReferentType.isNull())
376 return QualType();
377
Douglas Gregor8a5cb112009-06-26 21:40:05 +0000378 return SemaRef.BuildReferenceType(ReferentType, false, 0, Loc, Entity);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000379}
380
Douglas Gregorcd281c32009-02-28 00:25:32 +0000381QualType
382TemplateTypeInstantiator::
Douglas Gregor8a5cb112009-06-26 21:40:05 +0000383InstantiateMemberPointerType(const MemberPointerType *T) const {
Douglas Gregor949bf692009-06-09 22:17:39 +0000384 QualType PointeeType = Instantiate(T->getPointeeType());
385 if (PointeeType.isNull())
386 return QualType();
387
388 QualType ClassType = Instantiate(QualType(T->getClass(), 0));
389 if (ClassType.isNull())
390 return QualType();
391
Douglas Gregor8a5cb112009-06-26 21:40:05 +0000392 return SemaRef.BuildMemberPointerType(PointeeType, ClassType, 0, Loc,
Douglas Gregor949bf692009-06-09 22:17:39 +0000393 Entity);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000394}
395
Douglas Gregorcd281c32009-02-28 00:25:32 +0000396QualType
397TemplateTypeInstantiator::
Douglas Gregor8a5cb112009-06-26 21:40:05 +0000398InstantiateConstantArrayType(const ConstantArrayType *T) const {
Douglas Gregorcd281c32009-02-28 00:25:32 +0000399 QualType ElementType = Instantiate(T->getElementType());
400 if (ElementType.isNull())
401 return ElementType;
402
403 // Build a temporary integer literal to specify the size for
404 // BuildArrayType. Since we have already checked the size as part of
405 // creating the dependent array type in the first place, we know
Douglas Gregorff668032009-05-13 18:28:20 +0000406 // there aren't any errors. However, we do need to determine what
407 // C++ type to give the size expression.
408 llvm::APInt Size = T->getSize();
409 QualType Types[] = {
410 SemaRef.Context.UnsignedCharTy, SemaRef.Context.UnsignedShortTy,
411 SemaRef.Context.UnsignedIntTy, SemaRef.Context.UnsignedLongTy,
412 SemaRef.Context.UnsignedLongLongTy, SemaRef.Context.UnsignedInt128Ty
413 };
414 const unsigned NumTypes = sizeof(Types) / sizeof(QualType);
415 QualType SizeType;
416 for (unsigned I = 0; I != NumTypes; ++I)
417 if (Size.getBitWidth() == SemaRef.Context.getIntWidth(Types[I])) {
418 SizeType = Types[I];
419 break;
420 }
421
422 if (SizeType.isNull())
423 SizeType = SemaRef.Context.getFixedWidthIntType(Size.getBitWidth(), false);
424
425 IntegerLiteral ArraySize(Size, SizeType, Loc);
Douglas Gregorcd281c32009-02-28 00:25:32 +0000426 return SemaRef.BuildArrayType(ElementType, T->getSizeModifier(),
427 &ArraySize, T->getIndexTypeQualifier(),
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000428 SourceRange(), // FIXME: provide proper range?
429 Entity);
430}
431
432QualType
433TemplateTypeInstantiator::InstantiateConstantArrayWithExprType
434(const ConstantArrayWithExprType *T) const {
435 return InstantiateConstantArrayType(T);
436}
437
438QualType
439TemplateTypeInstantiator::InstantiateConstantArrayWithoutExprType
440(const ConstantArrayWithoutExprType *T) const {
441 return InstantiateConstantArrayType(T);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000442}
443
Douglas Gregorcd281c32009-02-28 00:25:32 +0000444QualType
445TemplateTypeInstantiator::
Douglas Gregor8a5cb112009-06-26 21:40:05 +0000446InstantiateIncompleteArrayType(const IncompleteArrayType *T) const {
Douglas Gregorcd281c32009-02-28 00:25:32 +0000447 QualType ElementType = Instantiate(T->getElementType());
448 if (ElementType.isNull())
449 return ElementType;
450
451 return SemaRef.BuildArrayType(ElementType, T->getSizeModifier(),
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000452 0, T->getIndexTypeQualifier(),
453 SourceRange(), // FIXME: provide proper range?
454 Entity);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000455}
456
Douglas Gregorcd281c32009-02-28 00:25:32 +0000457QualType
458TemplateTypeInstantiator::
Douglas Gregor8a5cb112009-06-26 21:40:05 +0000459InstantiateVariableArrayType(const VariableArrayType *T) const {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000460 // FIXME: Implement this
461 assert(false && "Cannot instantiate VariableArrayType yet");
462 return QualType();
463}
464
Douglas Gregorcd281c32009-02-28 00:25:32 +0000465QualType
466TemplateTypeInstantiator::
Douglas Gregor8a5cb112009-06-26 21:40:05 +0000467InstantiateDependentSizedArrayType(const DependentSizedArrayType *T) const {
Anders Carlsson76b1c842009-03-15 20:12:13 +0000468 Expr *ArraySize = T->getSizeExpr();
469 assert(ArraySize->isValueDependent() &&
470 "dependent sized array types must have value dependent size expr");
471
472 // Instantiate the element type if needed
473 QualType ElementType = T->getElementType();
474 if (ElementType->isDependentType()) {
475 ElementType = Instantiate(ElementType);
476 if (ElementType.isNull())
477 return QualType();
478 }
479
480 // Instantiate the size expression
Douglas Gregorac7610d2009-06-22 20:57:11 +0000481 EnterExpressionEvaluationContext Unevaluated(SemaRef, Action::Unevaluated);
Anders Carlsson76b1c842009-03-15 20:12:13 +0000482 Sema::OwningExprResult InstantiatedArraySize =
Douglas Gregor7e063902009-05-11 23:53:27 +0000483 SemaRef.InstantiateExpr(ArraySize, TemplateArgs);
Anders Carlsson76b1c842009-03-15 20:12:13 +0000484 if (InstantiatedArraySize.isInvalid())
485 return QualType();
486
487 return SemaRef.BuildArrayType(ElementType, T->getSizeModifier(),
Anders Carlssone9146f22009-05-01 19:49:17 +0000488 InstantiatedArraySize.takeAs<Expr>(),
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000489 T->getIndexTypeQualifier(),
490 SourceRange(), // FIXME: provide proper range?
491 Entity);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000492}
493
Douglas Gregorcd281c32009-02-28 00:25:32 +0000494QualType
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000495TemplateTypeInstantiator::
Douglas Gregor8a5cb112009-06-26 21:40:05 +0000496InstantiateDependentSizedExtVectorType(
497 const DependentSizedExtVectorType *T) const {
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000498
Douglas Gregorf6ddb732009-06-18 18:45:36 +0000499 // Instantiate the element type if needed.
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000500 QualType ElementType = T->getElementType();
501 if (ElementType->isDependentType()) {
502 ElementType = Instantiate(ElementType);
503 if (ElementType.isNull())
504 return QualType();
505 }
506
Douglas Gregorac7610d2009-06-22 20:57:11 +0000507 // The expression in a dependent-sized extended vector type is not
508 // potentially evaluated.
509 EnterExpressionEvaluationContext Unevaluated(SemaRef, Action::Unevaluated);
510
Douglas Gregorf6ddb732009-06-18 18:45:36 +0000511 // Instantiate the size expression.
512 const Expr *SizeExpr = T->getSizeExpr();
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000513 Sema::OwningExprResult InstantiatedArraySize =
Douglas Gregorf6ddb732009-06-18 18:45:36 +0000514 SemaRef.InstantiateExpr(const_cast<Expr *>(SizeExpr), TemplateArgs);
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000515 if (InstantiatedArraySize.isInvalid())
516 return QualType();
517
518 return SemaRef.BuildExtVectorType(ElementType,
519 SemaRef.Owned(
520 InstantiatedArraySize.takeAs<Expr>()),
521 T->getAttributeLoc());
522}
523
524QualType
Douglas Gregor8a5cb112009-06-26 21:40:05 +0000525TemplateTypeInstantiator::InstantiateVectorType(const VectorType *T) const {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000526 // FIXME: Implement this
527 assert(false && "Cannot instantiate VectorType yet");
528 return QualType();
529}
530
Douglas Gregorcd281c32009-02-28 00:25:32 +0000531QualType
Douglas Gregor8a5cb112009-06-26 21:40:05 +0000532TemplateTypeInstantiator::InstantiateExtVectorType(
533 const ExtVectorType *T) const {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000534 // FIXME: Implement this
535 assert(false && "Cannot instantiate ExtVectorType yet");
536 return QualType();
537}
538
Douglas Gregorcd281c32009-02-28 00:25:32 +0000539QualType
540TemplateTypeInstantiator::
Douglas Gregor8a5cb112009-06-26 21:40:05 +0000541InstantiateFunctionProtoType(const FunctionProtoType *T) const {
Douglas Gregor724651c2009-02-28 01:04:19 +0000542 QualType ResultType = Instantiate(T->getResultType());
543 if (ResultType.isNull())
544 return ResultType;
545
Douglas Gregor0ca20ac2009-05-29 18:27:38 +0000546 llvm::SmallVector<QualType, 4> ParamTypes;
Douglas Gregor724651c2009-02-28 01:04:19 +0000547 for (FunctionProtoType::arg_type_iterator Param = T->arg_type_begin(),
548 ParamEnd = T->arg_type_end();
549 Param != ParamEnd; ++Param) {
550 QualType P = Instantiate(*Param);
551 if (P.isNull())
552 return P;
553
554 ParamTypes.push_back(P);
555 }
556
Douglas Gregor02cbbd22009-06-11 18:10:32 +0000557 return SemaRef.BuildFunctionType(ResultType, ParamTypes.data(),
Douglas Gregor724651c2009-02-28 01:04:19 +0000558 ParamTypes.size(),
559 T->isVariadic(), T->getTypeQuals(),
560 Loc, Entity);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000561}
562
Douglas Gregorcd281c32009-02-28 00:25:32 +0000563QualType
564TemplateTypeInstantiator::
Douglas Gregor8a5cb112009-06-26 21:40:05 +0000565InstantiateFunctionNoProtoType(const FunctionNoProtoType *T) const {
Douglas Gregor724651c2009-02-28 01:04:19 +0000566 assert(false && "Functions without prototypes cannot be dependent.");
Douglas Gregor99ebf652009-02-27 19:31:52 +0000567 return QualType();
568}
569
Douglas Gregorcd281c32009-02-28 00:25:32 +0000570QualType
Douglas Gregor8a5cb112009-06-26 21:40:05 +0000571TemplateTypeInstantiator::InstantiateTypedefType(const TypedefType *T) const {
Douglas Gregor815215d2009-05-27 05:35:12 +0000572 TypedefDecl *Typedef
Douglas Gregored961e72009-05-27 17:54:46 +0000573 = cast_or_null<TypedefDecl>(
574 SemaRef.InstantiateCurrentDeclRef(T->getDecl()));
Douglas Gregor815215d2009-05-27 05:35:12 +0000575 if (!Typedef)
576 return QualType();
577
578 return SemaRef.Context.getTypeDeclType(Typedef);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000579}
580
Douglas Gregorcd281c32009-02-28 00:25:32 +0000581QualType
Douglas Gregor8a5cb112009-06-26 21:40:05 +0000582TemplateTypeInstantiator::InstantiateTypeOfExprType(
583 const TypeOfExprType *T) const {
Douglas Gregorac7610d2009-06-22 20:57:11 +0000584 // The expression in a typeof is not potentially evaluated.
585 EnterExpressionEvaluationContext Unevaluated(SemaRef, Action::Unevaluated);
586
Douglas Gregor5f8bd592009-05-26 22:09:24 +0000587 Sema::OwningExprResult E
588 = SemaRef.InstantiateExpr(T->getUnderlyingExpr(), TemplateArgs);
589 if (E.isInvalid())
590 return QualType();
591
Anders Carlssonaf017e62009-06-29 22:58:55 +0000592 return SemaRef.BuildTypeofExprType(E.takeAs<Expr>());
Douglas Gregor99ebf652009-02-27 19:31:52 +0000593}
594
Douglas Gregorcd281c32009-02-28 00:25:32 +0000595QualType
Douglas Gregor8a5cb112009-06-26 21:40:05 +0000596TemplateTypeInstantiator::InstantiateTypeOfType(const TypeOfType *T) const {
Douglas Gregor5f8bd592009-05-26 22:09:24 +0000597 QualType Underlying = Instantiate(T->getUnderlyingType());
598 if (Underlying.isNull())
599 return QualType();
600
601 return SemaRef.Context.getTypeOfType(Underlying);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000602}
603
Anders Carlsson395b4752009-06-24 19:06:50 +0000604QualType
Douglas Gregor8a5cb112009-06-26 21:40:05 +0000605TemplateTypeInstantiator::InstantiateDecltypeType(const DecltypeType *T) const {
Anders Carlsson87471f52009-06-26 03:02:18 +0000606 // C++0x [dcl.type.simple]p4:
607 // The operand of the decltype specifier is an unevaluated operand.
608 EnterExpressionEvaluationContext Unevaluated(SemaRef,
609 Action::Unevaluated);
610
Anders Carlsson395b4752009-06-24 19:06:50 +0000611 Sema::OwningExprResult E
612 = SemaRef.InstantiateExpr(T->getUnderlyingExpr(), TemplateArgs);
613
614 if (E.isInvalid())
615 return QualType();
616
Anders Carlssonaf017e62009-06-29 22:58:55 +0000617 return SemaRef.BuildDecltypeType(E.takeAs<Expr>());
Anders Carlsson395b4752009-06-24 19:06:50 +0000618}
619
Douglas Gregorcd281c32009-02-28 00:25:32 +0000620QualType
Douglas Gregor8a5cb112009-06-26 21:40:05 +0000621TemplateTypeInstantiator::InstantiateRecordType(const RecordType *T) const {
Douglas Gregor815215d2009-05-27 05:35:12 +0000622 RecordDecl *Record
Douglas Gregored961e72009-05-27 17:54:46 +0000623 = cast_or_null<RecordDecl>(SemaRef.InstantiateCurrentDeclRef(T->getDecl()));
Douglas Gregor815215d2009-05-27 05:35:12 +0000624 if (!Record)
625 return QualType();
626
627 return SemaRef.Context.getTypeDeclType(Record);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000628}
629
Douglas Gregorcd281c32009-02-28 00:25:32 +0000630QualType
Douglas Gregor8a5cb112009-06-26 21:40:05 +0000631TemplateTypeInstantiator::InstantiateEnumType(const EnumType *T) const {
Douglas Gregor815215d2009-05-27 05:35:12 +0000632 EnumDecl *Enum
Douglas Gregored961e72009-05-27 17:54:46 +0000633 = cast_or_null<EnumDecl>(SemaRef.InstantiateCurrentDeclRef(T->getDecl()));
Douglas Gregor815215d2009-05-27 05:35:12 +0000634 if (!Enum)
635 return QualType();
636
637 return SemaRef.Context.getTypeDeclType(Enum);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000638}
639
Douglas Gregorcd281c32009-02-28 00:25:32 +0000640QualType
641TemplateTypeInstantiator::
Douglas Gregor8a5cb112009-06-26 21:40:05 +0000642InstantiateTemplateTypeParmType(const TemplateTypeParmType *T) const {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000643 if (T->getDepth() == 0) {
644 // Replace the template type parameter with its corresponding
645 // template argument.
Douglas Gregor16134c62009-07-01 00:28:38 +0000646
647 // If the corresponding template argument is NULL or doesn't exist, it's
648 // because we are performing instantiation from explicitly-specified
649 // template arguments in a function template class, but there were some
650 // arguments left unspecified.
651 if (T->getIndex() >= TemplateArgs.size() ||
652 TemplateArgs[T->getIndex()].isNull())
653 return QualType(T, 0); // Would be nice to keep the original type here
654
Douglas Gregor99ebf652009-02-27 19:31:52 +0000655 assert(TemplateArgs[T->getIndex()].getKind() == TemplateArgument::Type &&
656 "Template argument kind mismatch");
Douglas Gregor8a5cb112009-06-26 21:40:05 +0000657 return TemplateArgs[T->getIndex()].getAsType();
Douglas Gregor99ebf652009-02-27 19:31:52 +0000658 }
659
660 // The template type parameter comes from an inner template (e.g.,
661 // the template parameter list of a member template inside the
662 // template we are instantiating). Create a new template type
663 // parameter with the template "level" reduced by one.
664 return SemaRef.Context.getTemplateTypeParmType(T->getDepth() - 1,
665 T->getIndex(),
Anders Carlsson76e4ce42009-06-16 00:30:48 +0000666 T->isParameterPack(),
Douglas Gregor8a5cb112009-06-26 21:40:05 +0000667 T->getName());
Douglas Gregor99ebf652009-02-27 19:31:52 +0000668}
669
Douglas Gregorcd281c32009-02-28 00:25:32 +0000670QualType
671TemplateTypeInstantiator::
Douglas Gregor7532dc62009-03-30 22:58:21 +0000672InstantiateTemplateSpecializationType(
Douglas Gregor8a5cb112009-06-26 21:40:05 +0000673 const TemplateSpecializationType *T) const {
Douglas Gregor0ca20ac2009-05-29 18:27:38 +0000674 llvm::SmallVector<TemplateArgument, 4> InstantiatedTemplateArgs;
Douglas Gregor40808ce2009-03-09 23:48:35 +0000675 InstantiatedTemplateArgs.reserve(T->getNumArgs());
Douglas Gregor7532dc62009-03-30 22:58:21 +0000676 for (TemplateSpecializationType::iterator Arg = T->begin(), ArgEnd = T->end();
Douglas Gregor40808ce2009-03-09 23:48:35 +0000677 Arg != ArgEnd; ++Arg) {
Douglas Gregor91333002009-06-11 00:06:24 +0000678 TemplateArgument InstArg = SemaRef.Instantiate(*Arg, TemplateArgs);
679 if (InstArg.isNull())
680 return QualType();
Douglas Gregor40808ce2009-03-09 23:48:35 +0000681
Douglas Gregor91333002009-06-11 00:06:24 +0000682 InstantiatedTemplateArgs.push_back(InstArg);
Douglas Gregor40808ce2009-03-09 23:48:35 +0000683 }
684
Mike Stump390b4cc2009-05-16 07:39:55 +0000685 // FIXME: We're missing the locations of the template name, '<', and '>'.
Douglas Gregorde650ae2009-03-31 18:38:02 +0000686
687 TemplateName Name = SemaRef.InstantiateTemplateName(T->getTemplateName(),
688 Loc,
Douglas Gregor7e063902009-05-11 23:53:27 +0000689 TemplateArgs);
Douglas Gregorde650ae2009-03-31 18:38:02 +0000690
691 return SemaRef.CheckTemplateIdType(Name, Loc, SourceLocation(),
Douglas Gregor02cbbd22009-06-11 18:10:32 +0000692 InstantiatedTemplateArgs.data(),
Douglas Gregor7532dc62009-03-30 22:58:21 +0000693 InstantiatedTemplateArgs.size(),
694 SourceLocation());
Douglas Gregor99ebf652009-02-27 19:31:52 +0000695}
696
Douglas Gregorcd281c32009-02-28 00:25:32 +0000697QualType
698TemplateTypeInstantiator::
Douglas Gregor8a5cb112009-06-26 21:40:05 +0000699InstantiateQualifiedNameType(const QualifiedNameType *T) const {
Douglas Gregord57959a2009-03-27 23:10:48 +0000700 // When we instantiated a qualified name type, there's no point in
701 // keeping the qualification around in the instantiated result. So,
702 // just instantiate the named type.
703 return (*this)(T->getNamedType());
704}
705
706QualType
707TemplateTypeInstantiator::
Douglas Gregor8a5cb112009-06-26 21:40:05 +0000708InstantiateTypenameType(const TypenameType *T) const {
Douglas Gregor17343172009-04-01 00:28:59 +0000709 if (const TemplateSpecializationType *TemplateId = T->getTemplateId()) {
710 // When the typename type refers to a template-id, the template-id
711 // is dependent and has enough information to instantiate the
712 // result of the typename type. Since we don't care about keeping
713 // the spelling of the typename type in template instantiations,
714 // we just instantiate the template-id.
Douglas Gregor8a5cb112009-06-26 21:40:05 +0000715 return InstantiateTemplateSpecializationType(TemplateId);
Douglas Gregor17343172009-04-01 00:28:59 +0000716 }
717
Douglas Gregord57959a2009-03-27 23:10:48 +0000718 NestedNameSpecifier *NNS
719 = SemaRef.InstantiateNestedNameSpecifier(T->getQualifier(),
720 SourceRange(Loc),
Douglas Gregor7e063902009-05-11 23:53:27 +0000721 TemplateArgs);
Douglas Gregord57959a2009-03-27 23:10:48 +0000722 if (!NNS)
723 return QualType();
724
Douglas Gregor17343172009-04-01 00:28:59 +0000725 return SemaRef.CheckTypenameType(NNS, *T->getIdentifier(), SourceRange(Loc));
Douglas Gregore4e5b052009-03-19 00:18:19 +0000726}
727
728QualType
729TemplateTypeInstantiator::
Douglas Gregor8a5cb112009-06-26 21:40:05 +0000730InstantiateObjCObjectPointerType(const ObjCObjectPointerType *T) const {
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000731 assert(false && "Objective-C types cannot be dependent");
732 return QualType();
733}
734
735QualType
736TemplateTypeInstantiator::
Douglas Gregor8a5cb112009-06-26 21:40:05 +0000737InstantiateObjCInterfaceType(const ObjCInterfaceType *T) const {
Douglas Gregorcd281c32009-02-28 00:25:32 +0000738 assert(false && "Objective-C types cannot be dependent");
Douglas Gregor99ebf652009-02-27 19:31:52 +0000739 return QualType();
740}
741
Douglas Gregorcd281c32009-02-28 00:25:32 +0000742/// \brief The actual implementation of Sema::InstantiateType().
743QualType TemplateTypeInstantiator::Instantiate(QualType T) const {
744 // If T is not a dependent type, there is nothing to do.
745 if (!T->isDependentType())
746 return T;
747
Douglas Gregor8a5cb112009-06-26 21:40:05 +0000748 QualType Result;
Douglas Gregorcd281c32009-02-28 00:25:32 +0000749 switch (T->getTypeClass()) {
750#define TYPE(Class, Base) \
751 case Type::Class: \
Douglas Gregor8a5cb112009-06-26 21:40:05 +0000752 Result = Instantiate##Class##Type(cast<Class##Type>(T.getTypePtr())); \
753 break;
Douglas Gregorcd281c32009-02-28 00:25:32 +0000754#define ABSTRACT_TYPE(Class, Base)
755#include "clang/AST/TypeNodes.def"
756 }
Douglas Gregor8a5cb112009-06-26 21:40:05 +0000757
758 // C++ [dcl.ref]p1:
759 // [...] Cv-qualified references are ill-formed except when
760 // the cv-qualifiers are introduced through the use of a
761 // typedef (7.1.3) or of a template type argument (14.3), in
762 // which case the cv-qualifiers are ignored.
763 //
764 // The same rule applies to function types.
Douglas Gregor9e9fae42009-07-22 20:02:25 +0000765 // FIXME: what about address-space and Objective-C GC qualifiers?
Douglas Gregor8a5cb112009-06-26 21:40:05 +0000766 if (!Result.isNull() && T.getCVRQualifiers() &&
767 !Result->isFunctionType() && !Result->isReferenceType())
768 Result = Result.getWithAdditionalQualifiers(T.getCVRQualifiers());
769 return Result;
Douglas Gregorcd281c32009-02-28 00:25:32 +0000770}
Douglas Gregor99ebf652009-02-27 19:31:52 +0000771
772/// \brief Instantiate the type T with a given set of template arguments.
773///
774/// This routine substitutes the given template arguments into the
775/// type T and produces the instantiated type.
776///
777/// \param T the type into which the template arguments will be
778/// substituted. If this type is not dependent, it will be returned
779/// immediately.
780///
781/// \param TemplateArgs the template arguments that will be
782/// substituted for the top-level template parameters within T.
783///
Douglas Gregor99ebf652009-02-27 19:31:52 +0000784/// \param Loc the location in the source code where this substitution
785/// is being performed. It will typically be the location of the
786/// declarator (if we're instantiating the type of some declaration)
787/// or the location of the type in the source code (if, e.g., we're
788/// instantiating the type of a cast expression).
789///
790/// \param Entity the name of the entity associated with a declaration
791/// being instantiated (if any). May be empty to indicate that there
792/// is no such entity (if, e.g., this is a type that occurs as part of
793/// a cast expression) or that the entity has no name (e.g., an
794/// unnamed function parameter).
795///
796/// \returns If the instantiation succeeds, the instantiated
797/// type. Otherwise, produces diagnostics and returns a NULL type.
798QualType Sema::InstantiateType(QualType T,
Douglas Gregor7e063902009-05-11 23:53:27 +0000799 const TemplateArgumentList &TemplateArgs,
Douglas Gregor99ebf652009-02-27 19:31:52 +0000800 SourceLocation Loc, DeclarationName Entity) {
Douglas Gregordf667e72009-03-10 20:44:00 +0000801 assert(!ActiveTemplateInstantiations.empty() &&
802 "Cannot perform an instantiation without some context on the "
803 "instantiation stack");
804
Douglas Gregor99ebf652009-02-27 19:31:52 +0000805 // If T is not a dependent type, there is nothing to do.
806 if (!T->isDependentType())
807 return T;
808
Douglas Gregor7e063902009-05-11 23:53:27 +0000809 TemplateTypeInstantiator Instantiator(*this, TemplateArgs, Loc, Entity);
Douglas Gregorcd281c32009-02-28 00:25:32 +0000810 return Instantiator(T);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000811}
Douglas Gregor2943aed2009-03-03 04:44:36 +0000812
813/// \brief Instantiate the base class specifiers of the given class
814/// template specialization.
815///
816/// Produces a diagnostic and returns true on error, returns false and
817/// attaches the instantiated base classes to the class template
818/// specialization if successful.
819bool
Douglas Gregord475b8d2009-03-25 21:17:03 +0000820Sema::InstantiateBaseSpecifiers(CXXRecordDecl *Instantiation,
821 CXXRecordDecl *Pattern,
Douglas Gregor7e063902009-05-11 23:53:27 +0000822 const TemplateArgumentList &TemplateArgs) {
Douglas Gregor2943aed2009-03-03 04:44:36 +0000823 bool Invalid = false;
Douglas Gregor0ca20ac2009-05-29 18:27:38 +0000824 llvm::SmallVector<CXXBaseSpecifier*, 4> InstantiatedBases;
Douglas Gregord475b8d2009-03-25 21:17:03 +0000825 for (ClassTemplateSpecializationDecl::base_class_iterator
826 Base = Pattern->bases_begin(), BaseEnd = Pattern->bases_end();
Douglas Gregor27b152f2009-03-10 18:52:44 +0000827 Base != BaseEnd; ++Base) {
Douglas Gregor2943aed2009-03-03 04:44:36 +0000828 if (!Base->getType()->isDependentType()) {
Fariborz Jahanian71c6e712009-07-22 17:41:53 +0000829 InstantiatedBases.push_back(new (Context) CXXBaseSpecifier(*Base));
Douglas Gregor2943aed2009-03-03 04:44:36 +0000830 continue;
831 }
832
833 QualType BaseType = InstantiateType(Base->getType(),
Douglas Gregor7e063902009-05-11 23:53:27 +0000834 TemplateArgs,
Douglas Gregor2943aed2009-03-03 04:44:36 +0000835 Base->getSourceRange().getBegin(),
836 DeclarationName());
837 if (BaseType.isNull()) {
838 Invalid = true;
839 continue;
840 }
841
842 if (CXXBaseSpecifier *InstantiatedBase
Douglas Gregord475b8d2009-03-25 21:17:03 +0000843 = CheckBaseSpecifier(Instantiation,
Douglas Gregor2943aed2009-03-03 04:44:36 +0000844 Base->getSourceRange(),
845 Base->isVirtual(),
846 Base->getAccessSpecifierAsWritten(),
847 BaseType,
848 /*FIXME: Not totally accurate */
849 Base->getSourceRange().getBegin()))
850 InstantiatedBases.push_back(InstantiatedBase);
851 else
852 Invalid = true;
853 }
854
Douglas Gregor27b152f2009-03-10 18:52:44 +0000855 if (!Invalid &&
Jay Foadbeaaccd2009-05-21 09:52:38 +0000856 AttachBaseSpecifiers(Instantiation, InstantiatedBases.data(),
Douglas Gregor2943aed2009-03-03 04:44:36 +0000857 InstantiatedBases.size()))
858 Invalid = true;
859
860 return Invalid;
861}
862
Douglas Gregord475b8d2009-03-25 21:17:03 +0000863/// \brief Instantiate the definition of a class from a given pattern.
864///
865/// \param PointOfInstantiation The point of instantiation within the
866/// source code.
867///
868/// \param Instantiation is the declaration whose definition is being
869/// instantiated. This will be either a class template specialization
870/// or a member class of a class template specialization.
871///
872/// \param Pattern is the pattern from which the instantiation
873/// occurs. This will be either the declaration of a class template or
874/// the declaration of a member class of a class template.
875///
876/// \param TemplateArgs The template arguments to be substituted into
877/// the pattern.
878///
Douglas Gregord475b8d2009-03-25 21:17:03 +0000879/// \returns true if an error occurred, false otherwise.
880bool
881Sema::InstantiateClass(SourceLocation PointOfInstantiation,
882 CXXRecordDecl *Instantiation, CXXRecordDecl *Pattern,
Douglas Gregor93dfdb12009-05-13 00:25:59 +0000883 const TemplateArgumentList &TemplateArgs,
884 bool ExplicitInstantiation) {
Douglas Gregord475b8d2009-03-25 21:17:03 +0000885 bool Invalid = false;
886
887 CXXRecordDecl *PatternDef
888 = cast_or_null<CXXRecordDecl>(Pattern->getDefinition(Context));
889 if (!PatternDef) {
890 if (Pattern == Instantiation->getInstantiatedFromMemberClass()) {
891 Diag(PointOfInstantiation,
892 diag::err_implicit_instantiate_member_undefined)
893 << Context.getTypeDeclType(Instantiation);
894 Diag(Pattern->getLocation(), diag::note_member_of_template_here);
895 } else {
Douglas Gregor93dfdb12009-05-13 00:25:59 +0000896 Diag(PointOfInstantiation, diag::err_template_instantiate_undefined)
897 << ExplicitInstantiation
Douglas Gregord475b8d2009-03-25 21:17:03 +0000898 << Context.getTypeDeclType(Instantiation);
899 Diag(Pattern->getLocation(), diag::note_template_decl_here);
900 }
901 return true;
902 }
903 Pattern = PatternDef;
904
Douglas Gregord048bb72009-03-25 21:23:52 +0000905 InstantiatingTemplate Inst(*this, PointOfInstantiation, Instantiation);
Douglas Gregord475b8d2009-03-25 21:17:03 +0000906 if (Inst)
907 return true;
908
909 // Enter the scope of this instantiation. We don't use
910 // PushDeclContext because we don't have a scope.
911 DeclContext *PreviousContext = CurContext;
912 CurContext = Instantiation;
913
914 // Start the definition of this instantiation.
915 Instantiation->startDefinition();
916
917 // Instantiate the base class specifiers.
Douglas Gregor7e063902009-05-11 23:53:27 +0000918 if (InstantiateBaseSpecifiers(Instantiation, Pattern, TemplateArgs))
Douglas Gregord475b8d2009-03-25 21:17:03 +0000919 Invalid = true;
920
Douglas Gregor0ca20ac2009-05-29 18:27:38 +0000921 llvm::SmallVector<DeclPtrTy, 4> Fields;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000922 for (RecordDecl::decl_iterator Member = Pattern->decls_begin(),
923 MemberEnd = Pattern->decls_end();
Douglas Gregor6ab35242009-04-09 21:40:53 +0000924 Member != MemberEnd; ++Member) {
Douglas Gregor7e063902009-05-11 23:53:27 +0000925 Decl *NewMember = InstantiateDecl(*Member, Instantiation, TemplateArgs);
Douglas Gregord475b8d2009-03-25 21:17:03 +0000926 if (NewMember) {
927 if (NewMember->isInvalidDecl())
928 Invalid = true;
929 else if (FieldDecl *Field = dyn_cast<FieldDecl>(NewMember))
Chris Lattnerb28317a2009-03-28 19:18:32 +0000930 Fields.push_back(DeclPtrTy::make(Field));
Douglas Gregord475b8d2009-03-25 21:17:03 +0000931 } else {
932 // FIXME: Eventually, a NULL return will mean that one of the
Mike Stump390b4cc2009-05-16 07:39:55 +0000933 // instantiations was a semantic disaster, and we'll want to set Invalid =
934 // true. For now, we expect to skip some members that we can't yet handle.
Douglas Gregord475b8d2009-03-25 21:17:03 +0000935 }
936 }
937
938 // Finish checking fields.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000939 ActOnFields(0, Instantiation->getLocation(), DeclPtrTy::make(Instantiation),
Jay Foadbeaaccd2009-05-21 09:52:38 +0000940 Fields.data(), Fields.size(), SourceLocation(), SourceLocation(),
Douglas Gregord475b8d2009-03-25 21:17:03 +0000941 0);
942
943 // Add any implicitly-declared members that we might need.
944 AddImplicitlyDeclaredMembersToClass(Instantiation);
945
946 // Exit the scope of this instantiation.
947 CurContext = PreviousContext;
948
Douglas Gregoraba43bb2009-05-26 20:50:29 +0000949 if (!Invalid)
950 Consumer.HandleTagDeclDefinition(Instantiation);
951
Douglas Gregora58861f2009-05-13 20:28:22 +0000952 // If this is an explicit instantiation, instantiate our members, too.
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000953 if (!Invalid && ExplicitInstantiation) {
954 Inst.Clear();
Douglas Gregora58861f2009-05-13 20:28:22 +0000955 InstantiateClassMembers(PointOfInstantiation, Instantiation, TemplateArgs);
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000956 }
Douglas Gregora58861f2009-05-13 20:28:22 +0000957
Douglas Gregord475b8d2009-03-25 21:17:03 +0000958 return Invalid;
959}
960
Douglas Gregor2943aed2009-03-03 04:44:36 +0000961bool
962Sema::InstantiateClassTemplateSpecialization(
963 ClassTemplateSpecializationDecl *ClassTemplateSpec,
964 bool ExplicitInstantiation) {
965 // Perform the actual instantiation on the canonical declaration.
966 ClassTemplateSpec = cast<ClassTemplateSpecializationDecl>(
Argyrios Kyrtzidis97fbaa22009-07-18 00:34:25 +0000967 ClassTemplateSpec->getCanonicalDecl());
Douglas Gregor2943aed2009-03-03 04:44:36 +0000968
969 // We can only instantiate something that hasn't already been
970 // instantiated or specialized. Fail without any diagnostics: our
971 // caller will provide an error message.
972 if (ClassTemplateSpec->getSpecializationKind() != TSK_Undeclared)
973 return true;
974
Douglas Gregor2943aed2009-03-03 04:44:36 +0000975 ClassTemplateDecl *Template = ClassTemplateSpec->getSpecializedTemplate();
Douglas Gregord475b8d2009-03-25 21:17:03 +0000976 CXXRecordDecl *Pattern = Template->getTemplatedDecl();
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000977 const TemplateArgumentList *TemplateArgs
978 = &ClassTemplateSpec->getTemplateArgs();
979
Douglas Gregorc1efb3f2009-06-12 22:31:52 +0000980 // C++ [temp.class.spec.match]p1:
981 // When a class template is used in a context that requires an
982 // instantiation of the class, it is necessary to determine
983 // whether the instantiation is to be generated using the primary
984 // template or one of the partial specializations. This is done by
985 // matching the template arguments of the class template
986 // specialization with the template argument lists of the partial
987 // specializations.
Douglas Gregor199d9912009-06-05 00:53:49 +0000988 typedef std::pair<ClassTemplatePartialSpecializationDecl *,
989 TemplateArgumentList *> MatchResult;
990 llvm::SmallVector<MatchResult, 4> Matched;
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000991 for (llvm::FoldingSet<ClassTemplatePartialSpecializationDecl>::iterator
992 Partial = Template->getPartialSpecializations().begin(),
993 PartialEnd = Template->getPartialSpecializations().end();
994 Partial != PartialEnd;
995 ++Partial) {
Douglas Gregorf67875d2009-06-12 18:26:56 +0000996 TemplateDeductionInfo Info(Context);
997 if (TemplateDeductionResult Result
Douglas Gregor199d9912009-06-05 00:53:49 +0000998 = DeduceTemplateArguments(&*Partial,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000999 ClassTemplateSpec->getTemplateArgs(),
1000 Info)) {
1001 // FIXME: Store the failed-deduction information for use in
1002 // diagnostics, later.
1003 (void)Result;
1004 } else {
1005 Matched.push_back(std::make_pair(&*Partial, Info.take()));
1006 }
Douglas Gregorc8ab2562009-05-31 09:31:02 +00001007 }
1008
1009 if (Matched.size() == 1) {
Douglas Gregorc1efb3f2009-06-12 22:31:52 +00001010 // -- If exactly one matching specialization is found, the
1011 // instantiation is generated from that specialization.
Douglas Gregor199d9912009-06-05 00:53:49 +00001012 Pattern = Matched[0].first;
1013 TemplateArgs = Matched[0].second;
Douglas Gregorc8ab2562009-05-31 09:31:02 +00001014 } else if (Matched.size() > 1) {
Douglas Gregorc1efb3f2009-06-12 22:31:52 +00001015 // -- If more than one matching specialization is found, the
1016 // partial order rules (14.5.4.2) are used to determine
1017 // whether one of the specializations is more specialized
1018 // than the others. If none of the specializations is more
1019 // specialized than all of the other matching
1020 // specializations, then the use of the class template is
1021 // ambiguous and the program is ill-formed.
Douglas Gregorc8ab2562009-05-31 09:31:02 +00001022 // FIXME: Implement partial ordering of class template partial
1023 // specializations.
1024 Diag(ClassTemplateSpec->getLocation(),
1025 diag::unsup_template_partial_spec_ordering);
Douglas Gregorc1efb3f2009-06-12 22:31:52 +00001026 } else {
1027 // -- If no matches are found, the instantiation is generated
1028 // from the primary template.
1029
1030 // Since we initialized the pattern and template arguments from
1031 // the primary template, there is nothing more we need to do here.
Douglas Gregorc8ab2562009-05-31 09:31:02 +00001032 }
Douglas Gregor2943aed2009-03-03 04:44:36 +00001033
1034 // Note that this is an instantiation.
1035 ClassTemplateSpec->setSpecializationKind(
1036 ExplicitInstantiation? TSK_ExplicitInstantiation
1037 : TSK_ImplicitInstantiation);
1038
Douglas Gregor199d9912009-06-05 00:53:49 +00001039 bool Result = InstantiateClass(ClassTemplateSpec->getLocation(),
1040 ClassTemplateSpec, Pattern, *TemplateArgs,
1041 ExplicitInstantiation);
1042
1043 for (unsigned I = 0, N = Matched.size(); I != N; ++I) {
1044 // FIXME: Implement TemplateArgumentList::Destroy!
1045 // if (Matched[I].first != Pattern)
1046 // Matched[I].second->Destroy(Context);
1047 }
1048
1049 return Result;
Douglas Gregor2943aed2009-03-03 04:44:36 +00001050}
Douglas Gregor5953d8b2009-03-19 17:26:29 +00001051
Douglas Gregora58861f2009-05-13 20:28:22 +00001052/// \brief Instantiate the definitions of all of the member of the
1053/// given class, which is an instantiation of a class template or a
1054/// member class of a template.
1055void
1056Sema::InstantiateClassMembers(SourceLocation PointOfInstantiation,
1057 CXXRecordDecl *Instantiation,
1058 const TemplateArgumentList &TemplateArgs) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001059 for (DeclContext::decl_iterator D = Instantiation->decls_begin(),
1060 DEnd = Instantiation->decls_end();
Douglas Gregora58861f2009-05-13 20:28:22 +00001061 D != DEnd; ++D) {
1062 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(*D)) {
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +00001063 if (!Function->getBody())
Douglas Gregorf3e7ce42009-05-18 17:01:57 +00001064 InstantiateFunctionDefinition(PointOfInstantiation, Function);
Douglas Gregora58861f2009-05-13 20:28:22 +00001065 } else if (VarDecl *Var = dyn_cast<VarDecl>(*D)) {
Douglas Gregor7caa6822009-07-24 20:34:43 +00001066 if (Var->isStaticDataMember())
1067 InstantiateStaticDataMemberDefinition(PointOfInstantiation, Var);
Douglas Gregora58861f2009-05-13 20:28:22 +00001068 } else if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(*D)) {
1069 if (!Record->isInjectedClassName() && !Record->getDefinition(Context)) {
1070 assert(Record->getInstantiatedFromMemberClass() &&
1071 "Missing instantiated-from-template information");
Douglas Gregorf3e7ce42009-05-18 17:01:57 +00001072 InstantiateClass(PointOfInstantiation, Record,
Douglas Gregora58861f2009-05-13 20:28:22 +00001073 Record->getInstantiatedFromMemberClass(),
1074 TemplateArgs, true);
1075 }
1076 }
1077 }
1078}
1079
1080/// \brief Instantiate the definitions of all of the members of the
1081/// given class template specialization, which was named as part of an
1082/// explicit instantiation.
1083void Sema::InstantiateClassTemplateSpecializationMembers(
1084 SourceLocation PointOfInstantiation,
1085 ClassTemplateSpecializationDecl *ClassTemplateSpec) {
1086 // C++0x [temp.explicit]p7:
1087 // An explicit instantiation that names a class template
1088 // specialization is an explicit instantion of the same kind
1089 // (declaration or definition) of each of its members (not
1090 // including members inherited from base classes) that has not
1091 // been previously explicitly specialized in the translation unit
1092 // containing the explicit instantiation, except as described
1093 // below.
1094 InstantiateClassMembers(PointOfInstantiation, ClassTemplateSpec,
1095 ClassTemplateSpec->getTemplateArgs());
1096}
1097
Douglas Gregorab452ba2009-03-26 23:50:42 +00001098/// \brief Instantiate a nested-name-specifier.
1099NestedNameSpecifier *
1100Sema::InstantiateNestedNameSpecifier(NestedNameSpecifier *NNS,
1101 SourceRange Range,
Douglas Gregor7e063902009-05-11 23:53:27 +00001102 const TemplateArgumentList &TemplateArgs) {
Douglas Gregorab452ba2009-03-26 23:50:42 +00001103 // Instantiate the prefix of this nested name specifier.
1104 NestedNameSpecifier *Prefix = NNS->getPrefix();
1105 if (Prefix) {
Douglas Gregor7e063902009-05-11 23:53:27 +00001106 Prefix = InstantiateNestedNameSpecifier(Prefix, Range, TemplateArgs);
Douglas Gregorab452ba2009-03-26 23:50:42 +00001107 if (!Prefix)
1108 return 0;
Douglas Gregor5953d8b2009-03-19 17:26:29 +00001109 }
1110
Douglas Gregorab452ba2009-03-26 23:50:42 +00001111 switch (NNS->getKind()) {
Douglas Gregor17343172009-04-01 00:28:59 +00001112 case NestedNameSpecifier::Identifier: {
1113 assert(Prefix &&
1114 "Can't have an identifier nested-name-specifier with no prefix");
1115 CXXScopeSpec SS;
1116 // FIXME: The source location information is all wrong.
1117 SS.setRange(Range);
1118 SS.setScopeRep(Prefix);
1119 return static_cast<NestedNameSpecifier *>(
1120 ActOnCXXNestedNameSpecifier(0, SS,
1121 Range.getEnd(),
1122 Range.getEnd(),
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00001123 *NNS->getAsIdentifier()));
Douglas Gregorab452ba2009-03-26 23:50:42 +00001124 break;
Douglas Gregor17343172009-04-01 00:28:59 +00001125 }
Douglas Gregorab452ba2009-03-26 23:50:42 +00001126
1127 case NestedNameSpecifier::Namespace:
1128 case NestedNameSpecifier::Global:
1129 return NNS;
1130
1131 case NestedNameSpecifier::TypeSpecWithTemplate:
1132 case NestedNameSpecifier::TypeSpec: {
1133 QualType T = QualType(NNS->getAsType(), 0);
1134 if (!T->isDependentType())
1135 return NNS;
1136
Douglas Gregor7e063902009-05-11 23:53:27 +00001137 T = InstantiateType(T, TemplateArgs, Range.getBegin(), DeclarationName());
Douglas Gregorab452ba2009-03-26 23:50:42 +00001138 if (T.isNull())
1139 return 0;
1140
Eli Friedman923f7532009-06-13 04:51:30 +00001141 if (T->isDependentType() || T->isRecordType() ||
Douglas Gregord57959a2009-03-27 23:10:48 +00001142 (getLangOptions().CPlusPlus0x && T->isEnumeralType())) {
Douglas Gregor17343172009-04-01 00:28:59 +00001143 assert(T.getCVRQualifiers() == 0 && "Can't get cv-qualifiers here");
Douglas Gregord57959a2009-03-27 23:10:48 +00001144 return NestedNameSpecifier::Create(Context, Prefix,
Douglas Gregorab452ba2009-03-26 23:50:42 +00001145 NNS->getKind() == NestedNameSpecifier::TypeSpecWithTemplate,
Douglas Gregord57959a2009-03-27 23:10:48 +00001146 T.getTypePtr());
1147 }
1148
1149 Diag(Range.getBegin(), diag::err_nested_name_spec_non_tag) << T;
1150 return 0;
Douglas Gregorab452ba2009-03-26 23:50:42 +00001151 }
1152 }
1153
Douglas Gregord57959a2009-03-27 23:10:48 +00001154 // Required to silence a GCC warning
Douglas Gregorab452ba2009-03-26 23:50:42 +00001155 return 0;
Douglas Gregor5953d8b2009-03-19 17:26:29 +00001156}
Douglas Gregorde650ae2009-03-31 18:38:02 +00001157
1158TemplateName
1159Sema::InstantiateTemplateName(TemplateName Name, SourceLocation Loc,
Douglas Gregor7e063902009-05-11 23:53:27 +00001160 const TemplateArgumentList &TemplateArgs) {
Douglas Gregorde650ae2009-03-31 18:38:02 +00001161 if (TemplateTemplateParmDecl *TTP
1162 = dyn_cast_or_null<TemplateTemplateParmDecl>(
1163 Name.getAsTemplateDecl())) {
1164 assert(TTP->getDepth() == 0 &&
1165 "Cannot reduce depth of a template template parameter");
Douglas Gregor9bde7732009-03-31 20:22:05 +00001166 assert(TemplateArgs[TTP->getPosition()].getAsDecl() &&
Douglas Gregorde650ae2009-03-31 18:38:02 +00001167 "Wrong kind of template template argument");
1168 ClassTemplateDecl *ClassTemplate
1169 = dyn_cast<ClassTemplateDecl>(
1170 TemplateArgs[TTP->getPosition()].getAsDecl());
Douglas Gregor9bde7732009-03-31 20:22:05 +00001171 assert(ClassTemplate && "Expected a class template");
Douglas Gregorde650ae2009-03-31 18:38:02 +00001172 if (QualifiedTemplateName *QTN = Name.getAsQualifiedTemplateName()) {
1173 NestedNameSpecifier *NNS
1174 = InstantiateNestedNameSpecifier(QTN->getQualifier(),
1175 /*FIXME=*/SourceRange(Loc),
Douglas Gregor7e063902009-05-11 23:53:27 +00001176 TemplateArgs);
Douglas Gregorde650ae2009-03-31 18:38:02 +00001177 if (NNS)
1178 return Context.getQualifiedTemplateName(NNS,
1179 QTN->hasTemplateKeyword(),
1180 ClassTemplate);
1181 }
1182
1183 return TemplateName(ClassTemplate);
1184 } else if (DependentTemplateName *DTN = Name.getAsDependentTemplateName()) {
1185 NestedNameSpecifier *NNS
1186 = InstantiateNestedNameSpecifier(DTN->getQualifier(),
1187 /*FIXME=*/SourceRange(Loc),
Douglas Gregor7e063902009-05-11 23:53:27 +00001188 TemplateArgs);
Douglas Gregorde650ae2009-03-31 18:38:02 +00001189
1190 if (!NNS) // FIXME: Not the best recovery strategy.
1191 return Name;
1192
1193 if (NNS->isDependent())
1194 return Context.getDependentTemplateName(NNS, DTN->getName());
1195
1196 // Somewhat redundant with ActOnDependentTemplateName.
1197 CXXScopeSpec SS;
1198 SS.setRange(SourceRange(Loc));
1199 SS.setScopeRep(NNS);
1200 TemplateTy Template;
1201 TemplateNameKind TNK = isTemplateName(*DTN->getName(), 0, Template, &SS);
1202 if (TNK == TNK_Non_template) {
1203 Diag(Loc, diag::err_template_kw_refers_to_non_template)
1204 << DTN->getName();
1205 return Name;
1206 } else if (TNK == TNK_Function_template) {
1207 Diag(Loc, diag::err_template_kw_refers_to_non_template)
1208 << DTN->getName();
1209 return Name;
1210 }
1211
1212 return Template.getAsVal<TemplateName>();
1213 }
1214
1215
1216
Mike Stump390b4cc2009-05-16 07:39:55 +00001217 // FIXME: Even if we're referring to a Decl that isn't a template template
1218 // parameter, we may need to instantiate the outer contexts of that
1219 // Decl. However, this won't be needed until we implement member templates.
Douglas Gregorde650ae2009-03-31 18:38:02 +00001220 return Name;
1221}
Douglas Gregor91333002009-06-11 00:06:24 +00001222
1223TemplateArgument Sema::Instantiate(TemplateArgument Arg,
1224 const TemplateArgumentList &TemplateArgs) {
1225 switch (Arg.getKind()) {
1226 case TemplateArgument::Null:
1227 assert(false && "Should never have a NULL template argument");
1228 break;
1229
1230 case TemplateArgument::Type: {
1231 QualType T = InstantiateType(Arg.getAsType(), TemplateArgs,
1232 Arg.getLocation(), DeclarationName());
1233 if (T.isNull())
1234 return TemplateArgument();
1235
1236 return TemplateArgument(Arg.getLocation(), T);
1237 }
1238
1239 case TemplateArgument::Declaration:
1240 // FIXME: Template instantiation for template template parameters.
1241 return Arg;
1242
1243 case TemplateArgument::Integral:
1244 return Arg;
1245
1246 case TemplateArgument::Expression: {
Douglas Gregorac7610d2009-06-22 20:57:11 +00001247 // Template argument expressions are not potentially evaluated.
1248 EnterExpressionEvaluationContext Unevaluated(*this, Action::Unevaluated);
1249
Douglas Gregor91333002009-06-11 00:06:24 +00001250 Sema::OwningExprResult E = InstantiateExpr(Arg.getAsExpr(), TemplateArgs);
1251 if (E.isInvalid())
1252 return TemplateArgument();
1253 return TemplateArgument(E.takeAs<Expr>());
1254 }
Anders Carlssond01b1da2009-06-15 17:04:53 +00001255
1256 case TemplateArgument::Pack:
1257 assert(0 && "FIXME: Implement!");
1258 break;
Douglas Gregor91333002009-06-11 00:06:24 +00001259 }
1260
1261 assert(false && "Unhandled template argument kind");
1262 return TemplateArgument();
1263}