blob: 50070fc663fa56c8399a6189f084b9b15cc91af8 [file] [log] [blame]
Douglas Gregorfe1e1102009-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 Gregord6ff3322009-08-04 16:50:30 +000014#include "TreeTransform.h"
Douglas Gregor28ad4b52009-05-26 20:50:29 +000015#include "clang/AST/ASTConsumer.h"
Douglas Gregorfe1e1102009-02-27 19:31:52 +000016#include "clang/AST/ASTContext.h"
17#include "clang/AST/Expr.h"
Douglas Gregorfe1e1102009-02-27 19:31:52 +000018#include "clang/AST/DeclTemplate.h"
19#include "clang/Parse/DeclSpec.h"
20#include "clang/Basic/LangOptions.h"
Douglas Gregor17c0d7b2009-02-28 00:25:32 +000021#include "llvm/Support/Compiler.h"
Douglas Gregorfe1e1102009-02-27 19:31:52 +000022
23using namespace clang;
24
Douglas Gregor4ea568f2009-03-10 18:03:33 +000025//===----------------------------------------------------------------------===/
26// Template Instantiation Support
27//===----------------------------------------------------------------------===/
28
Douglas Gregor01afeef2009-08-28 20:31:08 +000029/// \brief Retrieve the template argument list(s) that should be used to
30/// instantiate the definition of the given declaration.
Douglas Gregora654dd82009-08-28 17:37:35 +000031MultiLevelTemplateArgumentList
Douglas Gregorb4850462009-05-14 23:26:13 +000032Sema::getTemplateInstantiationArgs(NamedDecl *D) {
Douglas Gregora654dd82009-08-28 17:37:35 +000033 // Accumulate the set of template argument lists in this structure.
34 MultiLevelTemplateArgumentList Result;
Mike Stump11289f42009-09-09 15:08:12 +000035
Douglas Gregora654dd82009-08-28 17:37:35 +000036 DeclContext *Ctx = dyn_cast<DeclContext>(D);
37 if (!Ctx)
38 Ctx = D->getDeclContext();
Mike Stump11289f42009-09-09 15:08:12 +000039
John McCall970d5302009-08-29 03:16:09 +000040 while (!Ctx->isFileContext()) {
Douglas Gregora654dd82009-08-28 17:37:35 +000041 // Add template arguments from a class template instantiation.
Mike Stump11289f42009-09-09 15:08:12 +000042 if (ClassTemplateSpecializationDecl *Spec
Douglas Gregora654dd82009-08-28 17:37:35 +000043 = dyn_cast<ClassTemplateSpecializationDecl>(Ctx)) {
44 // We're done when we hit an explicit specialization.
45 if (Spec->getSpecializationKind() == TSK_ExplicitSpecialization)
46 break;
Mike Stump11289f42009-09-09 15:08:12 +000047
Douglas Gregora654dd82009-08-28 17:37:35 +000048 Result.addOuterTemplateArguments(&Spec->getTemplateInstantiationArgs());
Douglas Gregorcf915552009-10-13 16:30:37 +000049
50 // If this class template specialization was instantiated from a
51 // specialized member that is a class template, we're done.
52 assert(Spec->getSpecializedTemplate() && "No class template?");
53 if (Spec->getSpecializedTemplate()->isMemberSpecialization())
54 break;
Mike Stump11289f42009-09-09 15:08:12 +000055 }
Douglas Gregora654dd82009-08-28 17:37:35 +000056 // Add template arguments from a function template specialization.
John McCall970d5302009-08-29 03:16:09 +000057 else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Ctx)) {
Douglas Gregorcf915552009-10-13 16:30:37 +000058 if (Function->getTemplateSpecializationKind()
59 == TSK_ExplicitSpecialization)
60 break;
61
Douglas Gregora654dd82009-08-28 17:37:35 +000062 if (const TemplateArgumentList *TemplateArgs
Douglas Gregorcf915552009-10-13 16:30:37 +000063 = Function->getTemplateSpecializationArgs()) {
64 // Add the template arguments for this specialization.
Douglas Gregora654dd82009-08-28 17:37:35 +000065 Result.addOuterTemplateArguments(TemplateArgs);
John McCall970d5302009-08-29 03:16:09 +000066
Douglas Gregorcf915552009-10-13 16:30:37 +000067 // If this function was instantiated from a specialized member that is
68 // a function template, we're done.
69 assert(Function->getPrimaryTemplate() && "No function template?");
70 if (Function->getPrimaryTemplate()->isMemberSpecialization())
71 break;
72 }
73
John McCall970d5302009-08-29 03:16:09 +000074 // If this is a friend declaration and it declares an entity at
75 // namespace scope, take arguments from its lexical parent
76 // instead of its semantic parent.
77 if (Function->getFriendObjectKind() &&
78 Function->getDeclContext()->isFileContext()) {
79 Ctx = Function->getLexicalDeclContext();
80 continue;
81 }
Douglas Gregora654dd82009-08-28 17:37:35 +000082 }
John McCall970d5302009-08-29 03:16:09 +000083
84 Ctx = Ctx->getParent();
Douglas Gregorb4850462009-05-14 23:26:13 +000085 }
Mike Stump11289f42009-09-09 15:08:12 +000086
Douglas Gregora654dd82009-08-28 17:37:35 +000087 return Result;
Douglas Gregorb4850462009-05-14 23:26:13 +000088}
89
Douglas Gregorfcd5db32009-03-10 00:06:19 +000090Sema::InstantiatingTemplate::
91InstantiatingTemplate(Sema &SemaRef, SourceLocation PointOfInstantiation,
Douglas Gregor85673582009-05-18 17:01:57 +000092 Decl *Entity,
Douglas Gregorfcd5db32009-03-10 00:06:19 +000093 SourceRange InstantiationRange)
94 : SemaRef(SemaRef) {
Douglas Gregor79cf6032009-03-10 20:44:00 +000095
96 Invalid = CheckInstantiationDepth(PointOfInstantiation,
97 InstantiationRange);
98 if (!Invalid) {
Douglas Gregorfcd5db32009-03-10 00:06:19 +000099 ActiveTemplateInstantiation Inst;
Douglas Gregor79cf6032009-03-10 20:44:00 +0000100 Inst.Kind = ActiveTemplateInstantiation::TemplateInstantiation;
Douglas Gregorfcd5db32009-03-10 00:06:19 +0000101 Inst.PointOfInstantiation = PointOfInstantiation;
Douglas Gregor79cf6032009-03-10 20:44:00 +0000102 Inst.Entity = reinterpret_cast<uintptr_t>(Entity);
Douglas Gregorc9220832009-03-12 18:36:18 +0000103 Inst.TemplateArgs = 0;
104 Inst.NumTemplateArgs = 0;
Douglas Gregor79cf6032009-03-10 20:44:00 +0000105 Inst.InstantiationRange = InstantiationRange;
106 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
107 Invalid = false;
108 }
109}
110
Mike Stump11289f42009-09-09 15:08:12 +0000111Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef,
Douglas Gregor79cf6032009-03-10 20:44:00 +0000112 SourceLocation PointOfInstantiation,
113 TemplateDecl *Template,
114 const TemplateArgument *TemplateArgs,
115 unsigned NumTemplateArgs,
116 SourceRange InstantiationRange)
117 : SemaRef(SemaRef) {
118
119 Invalid = CheckInstantiationDepth(PointOfInstantiation,
120 InstantiationRange);
121 if (!Invalid) {
122 ActiveTemplateInstantiation Inst;
Mike Stump11289f42009-09-09 15:08:12 +0000123 Inst.Kind
Douglas Gregor79cf6032009-03-10 20:44:00 +0000124 = ActiveTemplateInstantiation::DefaultTemplateArgumentInstantiation;
125 Inst.PointOfInstantiation = PointOfInstantiation;
126 Inst.Entity = reinterpret_cast<uintptr_t>(Template);
127 Inst.TemplateArgs = TemplateArgs;
128 Inst.NumTemplateArgs = NumTemplateArgs;
Douglas Gregorfcd5db32009-03-10 00:06:19 +0000129 Inst.InstantiationRange = InstantiationRange;
130 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
131 Invalid = false;
132 }
133}
134
Mike Stump11289f42009-09-09 15:08:12 +0000135Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef,
Douglas Gregor637d9982009-06-10 23:47:09 +0000136 SourceLocation PointOfInstantiation,
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000137 FunctionTemplateDecl *FunctionTemplate,
138 const TemplateArgument *TemplateArgs,
139 unsigned NumTemplateArgs,
140 ActiveTemplateInstantiation::InstantiationKind Kind,
141 SourceRange InstantiationRange)
142: SemaRef(SemaRef) {
Mike Stump11289f42009-09-09 15:08:12 +0000143
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000144 Invalid = CheckInstantiationDepth(PointOfInstantiation,
145 InstantiationRange);
146 if (!Invalid) {
147 ActiveTemplateInstantiation Inst;
148 Inst.Kind = Kind;
149 Inst.PointOfInstantiation = PointOfInstantiation;
150 Inst.Entity = reinterpret_cast<uintptr_t>(FunctionTemplate);
151 Inst.TemplateArgs = TemplateArgs;
152 Inst.NumTemplateArgs = NumTemplateArgs;
153 Inst.InstantiationRange = InstantiationRange;
154 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
155 Invalid = false;
156 }
157}
158
Mike Stump11289f42009-09-09 15:08:12 +0000159Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef,
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000160 SourceLocation PointOfInstantiation,
Douglas Gregor637d9982009-06-10 23:47:09 +0000161 ClassTemplatePartialSpecializationDecl *PartialSpec,
162 const TemplateArgument *TemplateArgs,
163 unsigned NumTemplateArgs,
164 SourceRange InstantiationRange)
165 : SemaRef(SemaRef) {
166
167 Invalid = CheckInstantiationDepth(PointOfInstantiation,
168 InstantiationRange);
169 if (!Invalid) {
170 ActiveTemplateInstantiation Inst;
Mike Stump11289f42009-09-09 15:08:12 +0000171 Inst.Kind
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000172 = ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution;
Douglas Gregor637d9982009-06-10 23:47:09 +0000173 Inst.PointOfInstantiation = PointOfInstantiation;
174 Inst.Entity = reinterpret_cast<uintptr_t>(PartialSpec);
175 Inst.TemplateArgs = TemplateArgs;
176 Inst.NumTemplateArgs = NumTemplateArgs;
177 Inst.InstantiationRange = InstantiationRange;
178 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
179 Invalid = false;
180 }
181}
182
Mike Stump11289f42009-09-09 15:08:12 +0000183Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef,
Anders Carlsson657bad42009-09-05 05:14:19 +0000184 SourceLocation PointOfInstantation,
185 ParmVarDecl *Param,
186 const TemplateArgument *TemplateArgs,
187 unsigned NumTemplateArgs,
188 SourceRange InstantiationRange)
189 : SemaRef(SemaRef) {
Mike Stump11289f42009-09-09 15:08:12 +0000190
Anders Carlsson657bad42009-09-05 05:14:19 +0000191 Invalid = CheckInstantiationDepth(PointOfInstantation, InstantiationRange);
192
193 if (!Invalid) {
194 ActiveTemplateInstantiation Inst;
195 Inst.Kind
196 = ActiveTemplateInstantiation::DefaultFunctionArgumentInstantiation;
197 Inst.PointOfInstantiation = PointOfInstantation;
198 Inst.Entity = reinterpret_cast<uintptr_t>(Param);
199 Inst.TemplateArgs = TemplateArgs;
200 Inst.NumTemplateArgs = NumTemplateArgs;
201 Inst.InstantiationRange = InstantiationRange;
202 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
203 Invalid = false;
204 }
205}
206
Douglas Gregor85673582009-05-18 17:01:57 +0000207void Sema::InstantiatingTemplate::Clear() {
208 if (!Invalid) {
Douglas Gregorfcd5db32009-03-10 00:06:19 +0000209 SemaRef.ActiveTemplateInstantiations.pop_back();
Douglas Gregor85673582009-05-18 17:01:57 +0000210 Invalid = true;
211 }
Douglas Gregorfcd5db32009-03-10 00:06:19 +0000212}
213
Douglas Gregor79cf6032009-03-10 20:44:00 +0000214bool Sema::InstantiatingTemplate::CheckInstantiationDepth(
215 SourceLocation PointOfInstantiation,
216 SourceRange InstantiationRange) {
Mike Stump11289f42009-09-09 15:08:12 +0000217 if (SemaRef.ActiveTemplateInstantiations.size()
Douglas Gregor79cf6032009-03-10 20:44:00 +0000218 <= SemaRef.getLangOptions().InstantiationDepth)
219 return false;
220
Mike Stump11289f42009-09-09 15:08:12 +0000221 SemaRef.Diag(PointOfInstantiation,
Douglas Gregor79cf6032009-03-10 20:44:00 +0000222 diag::err_template_recursion_depth_exceeded)
223 << SemaRef.getLangOptions().InstantiationDepth
224 << InstantiationRange;
225 SemaRef.Diag(PointOfInstantiation, diag::note_template_recursion_depth)
226 << SemaRef.getLangOptions().InstantiationDepth;
227 return true;
228}
229
Douglas Gregor4ea568f2009-03-10 18:03:33 +0000230/// \brief Prints the current instantiation stack through a series of
231/// notes.
232void Sema::PrintInstantiationStack() {
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000233 // FIXME: In all of these cases, we need to show the template arguments
Douglas Gregor4ea568f2009-03-10 18:03:33 +0000234 for (llvm::SmallVector<ActiveTemplateInstantiation, 16>::reverse_iterator
235 Active = ActiveTemplateInstantiations.rbegin(),
236 ActiveEnd = ActiveTemplateInstantiations.rend();
237 Active != ActiveEnd;
238 ++Active) {
Douglas Gregor79cf6032009-03-10 20:44:00 +0000239 switch (Active->Kind) {
240 case ActiveTemplateInstantiation::TemplateInstantiation: {
Douglas Gregor85673582009-05-18 17:01:57 +0000241 Decl *D = reinterpret_cast<Decl *>(Active->Entity);
242 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) {
243 unsigned DiagID = diag::note_template_member_class_here;
244 if (isa<ClassTemplateSpecializationDecl>(Record))
245 DiagID = diag::note_template_class_instantiation_here;
Mike Stump11289f42009-09-09 15:08:12 +0000246 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
Douglas Gregor85673582009-05-18 17:01:57 +0000247 DiagID)
248 << Context.getTypeDeclType(Record)
249 << Active->InstantiationRange;
Douglas Gregora6ef8f02009-07-24 20:34:43 +0000250 } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
Douglas Gregor4adbc6d2009-06-26 00:10:03 +0000251 unsigned DiagID;
252 if (Function->getPrimaryTemplate())
253 DiagID = diag::note_function_template_spec_here;
254 else
255 DiagID = diag::note_template_member_function_here;
Mike Stump11289f42009-09-09 15:08:12 +0000256 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
Douglas Gregor85673582009-05-18 17:01:57 +0000257 DiagID)
258 << Function
259 << Active->InstantiationRange;
Douglas Gregora6ef8f02009-07-24 20:34:43 +0000260 } else {
261 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
262 diag::note_template_static_data_member_def_here)
263 << cast<VarDecl>(D)
264 << Active->InstantiationRange;
Douglas Gregor85673582009-05-18 17:01:57 +0000265 }
Douglas Gregor79cf6032009-03-10 20:44:00 +0000266 break;
267 }
268
269 case ActiveTemplateInstantiation::DefaultTemplateArgumentInstantiation: {
270 TemplateDecl *Template = cast<TemplateDecl>((Decl *)Active->Entity);
271 std::string TemplateArgsStr
Douglas Gregordc572a32009-03-30 22:58:21 +0000272 = TemplateSpecializationType::PrintTemplateArgumentList(
Mike Stump11289f42009-09-09 15:08:12 +0000273 Active->TemplateArgs,
Douglas Gregor7de59662009-05-29 20:38:28 +0000274 Active->NumTemplateArgs,
275 Context.PrintingPolicy);
Douglas Gregor79cf6032009-03-10 20:44:00 +0000276 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
277 diag::note_default_arg_instantiation_here)
278 << (Template->getNameAsString() + TemplateArgsStr)
279 << Active->InstantiationRange;
280 break;
281 }
Douglas Gregor637d9982009-06-10 23:47:09 +0000282
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000283 case ActiveTemplateInstantiation::ExplicitTemplateArgumentSubstitution: {
Mike Stump11289f42009-09-09 15:08:12 +0000284 FunctionTemplateDecl *FnTmpl
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000285 = cast<FunctionTemplateDecl>((Decl *)Active->Entity);
Douglas Gregor637d9982009-06-10 23:47:09 +0000286 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000287 diag::note_explicit_template_arg_substitution_here)
288 << FnTmpl << Active->InstantiationRange;
Douglas Gregor637d9982009-06-10 23:47:09 +0000289 break;
290 }
Mike Stump11289f42009-09-09 15:08:12 +0000291
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000292 case ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution:
293 if (ClassTemplatePartialSpecializationDecl *PartialSpec
294 = dyn_cast<ClassTemplatePartialSpecializationDecl>(
295 (Decl *)Active->Entity)) {
296 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
297 diag::note_partial_spec_deduct_instantiation_here)
298 << Context.getTypeDeclType(PartialSpec)
299 << Active->InstantiationRange;
300 } else {
301 FunctionTemplateDecl *FnTmpl
302 = cast<FunctionTemplateDecl>((Decl *)Active->Entity);
303 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
304 diag::note_function_template_deduction_instantiation_here)
305 << FnTmpl << Active->InstantiationRange;
306 }
307 break;
Douglas Gregor637d9982009-06-10 23:47:09 +0000308
Anders Carlsson657bad42009-09-05 05:14:19 +0000309 case ActiveTemplateInstantiation::DefaultFunctionArgumentInstantiation: {
310 ParmVarDecl *Param = cast<ParmVarDecl>((Decl *)Active->Entity);
311 FunctionDecl *FD = cast<FunctionDecl>(Param->getDeclContext());
Mike Stump11289f42009-09-09 15:08:12 +0000312
Anders Carlsson657bad42009-09-05 05:14:19 +0000313 std::string TemplateArgsStr
314 = TemplateSpecializationType::PrintTemplateArgumentList(
Mike Stump11289f42009-09-09 15:08:12 +0000315 Active->TemplateArgs,
Anders Carlsson657bad42009-09-05 05:14:19 +0000316 Active->NumTemplateArgs,
317 Context.PrintingPolicy);
318 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
319 diag::note_default_function_arg_instantiation_here)
Anders Carlssondc6d2c32009-09-05 05:38:54 +0000320 << (FD->getNameAsString() + TemplateArgsStr)
Anders Carlsson657bad42009-09-05 05:14:19 +0000321 << Active->InstantiationRange;
322 break;
323 }
Mike Stump11289f42009-09-09 15:08:12 +0000324
Douglas Gregor79cf6032009-03-10 20:44:00 +0000325 }
Douglas Gregor4ea568f2009-03-10 18:03:33 +0000326 }
327}
328
Douglas Gregor33834512009-06-14 07:33:30 +0000329bool Sema::isSFINAEContext() const {
330 using llvm::SmallVector;
331 for (SmallVector<ActiveTemplateInstantiation, 16>::const_reverse_iterator
332 Active = ActiveTemplateInstantiations.rbegin(),
333 ActiveEnd = ActiveTemplateInstantiations.rend();
334 Active != ActiveEnd;
335 ++Active) {
336
337 switch(Active->Kind) {
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000338 case ActiveTemplateInstantiation::TemplateInstantiation:
Anders Carlsson657bad42009-09-05 05:14:19 +0000339 case ActiveTemplateInstantiation::DefaultFunctionArgumentInstantiation:
340
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000341 // This is a template instantiation, so there is no SFINAE.
342 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000343
Douglas Gregor33834512009-06-14 07:33:30 +0000344 case ActiveTemplateInstantiation::DefaultTemplateArgumentInstantiation:
345 // A default template argument instantiation may or may not be a
346 // SFINAE context; look further up the stack.
347 break;
Mike Stump11289f42009-09-09 15:08:12 +0000348
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000349 case ActiveTemplateInstantiation::ExplicitTemplateArgumentSubstitution:
350 case ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution:
351 // We're either substitution explicitly-specified template arguments
352 // or deduced template arguments, so SFINAE applies.
353 return true;
Douglas Gregor33834512009-06-14 07:33:30 +0000354 }
355 }
356
357 return false;
358}
359
Douglas Gregorfe1e1102009-02-27 19:31:52 +0000360//===----------------------------------------------------------------------===/
361// Template Instantiation for Types
362//===----------------------------------------------------------------------===/
Douglas Gregor17c0d7b2009-02-28 00:25:32 +0000363namespace {
Mike Stump11289f42009-09-09 15:08:12 +0000364 class VISIBILITY_HIDDEN TemplateInstantiator
365 : public TreeTransform<TemplateInstantiator> {
Douglas Gregor01afeef2009-08-28 20:31:08 +0000366 const MultiLevelTemplateArgumentList &TemplateArgs;
Douglas Gregor17c0d7b2009-02-28 00:25:32 +0000367 SourceLocation Loc;
368 DeclarationName Entity;
Douglas Gregorfe1e1102009-02-27 19:31:52 +0000369
Douglas Gregor17c0d7b2009-02-28 00:25:32 +0000370 public:
Douglas Gregorebe10102009-08-20 07:17:43 +0000371 typedef TreeTransform<TemplateInstantiator> inherited;
Mike Stump11289f42009-09-09 15:08:12 +0000372
373 TemplateInstantiator(Sema &SemaRef,
Douglas Gregor01afeef2009-08-28 20:31:08 +0000374 const MultiLevelTemplateArgumentList &TemplateArgs,
Douglas Gregord6ff3322009-08-04 16:50:30 +0000375 SourceLocation Loc,
Mike Stump11289f42009-09-09 15:08:12 +0000376 DeclarationName Entity)
377 : inherited(SemaRef), TemplateArgs(TemplateArgs), Loc(Loc),
Douglas Gregorebe10102009-08-20 07:17:43 +0000378 Entity(Entity) { }
Douglas Gregor17c0d7b2009-02-28 00:25:32 +0000379
Mike Stump11289f42009-09-09 15:08:12 +0000380 /// \brief Determine whether the given type \p T has already been
Douglas Gregord6ff3322009-08-04 16:50:30 +0000381 /// transformed.
382 ///
383 /// For the purposes of template instantiation, a type has already been
384 /// transformed if it is NULL or if it is not dependent.
385 bool AlreadyTransformed(QualType T) {
386 return T.isNull() || !T->isDependentType();
Douglas Gregorf61eca92009-05-13 18:28:20 +0000387 }
Mike Stump11289f42009-09-09 15:08:12 +0000388
Douglas Gregord6ff3322009-08-04 16:50:30 +0000389 /// \brief Returns the location of the entity being instantiated, if known.
390 SourceLocation getBaseLocation() { return Loc; }
Mike Stump11289f42009-09-09 15:08:12 +0000391
Douglas Gregord6ff3322009-08-04 16:50:30 +0000392 /// \brief Returns the name of the entity being instantiated, if any.
393 DeclarationName getBaseEntity() { return Entity; }
Mike Stump11289f42009-09-09 15:08:12 +0000394
Douglas Gregoref6ab412009-10-27 06:26:26 +0000395 /// \brief Sets the "base" location and entity when that
396 /// information is known based on another transformation.
397 void setBase(SourceLocation Loc, DeclarationName Entity) {
398 this->Loc = Loc;
399 this->Entity = Entity;
400 }
401
Douglas Gregord6ff3322009-08-04 16:50:30 +0000402 /// \brief Transform the given declaration by instantiating a reference to
403 /// this declaration.
404 Decl *TransformDecl(Decl *D);
Douglas Gregora16548e2009-08-11 05:31:07 +0000405
Mike Stump11289f42009-09-09 15:08:12 +0000406 /// \brief Transform the definition of the given declaration by
Douglas Gregorebe10102009-08-20 07:17:43 +0000407 /// instantiating it.
408 Decl *TransformDefinition(Decl *D);
Mike Stump11289f42009-09-09 15:08:12 +0000409
Douglas Gregora5cb6da2009-10-20 05:58:46 +0000410 /// \bried Transform the first qualifier within a scope by instantiating the
411 /// declaration.
412 NamedDecl *TransformFirstQualifierInScope(NamedDecl *D, SourceLocation Loc);
413
Douglas Gregorebe10102009-08-20 07:17:43 +0000414 /// \brief Rebuild the exception declaration and register the declaration
415 /// as an instantiated local.
Mike Stump11289f42009-09-09 15:08:12 +0000416 VarDecl *RebuildExceptionDecl(VarDecl *ExceptionDecl, QualType T,
Douglas Gregorebe10102009-08-20 07:17:43 +0000417 DeclaratorInfo *Declarator,
418 IdentifierInfo *Name,
419 SourceLocation Loc, SourceRange TypeRange);
Mike Stump11289f42009-09-09 15:08:12 +0000420
John McCall7f41d982009-09-11 04:59:25 +0000421 /// \brief Check for tag mismatches when instantiating an
422 /// elaborated type.
423 QualType RebuildElaboratedType(QualType T, ElaboratedType::TagKind Tag);
424
Douglas Gregorc95a1fa2009-11-04 07:01:15 +0000425 Sema::OwningExprResult TransformPredefinedExpr(PredefinedExpr *E,
426 bool isAddressOfOperand);
427 Sema::OwningExprResult TransformDeclRefExpr(DeclRefExpr *E,
428 bool isAddressOfOperand);
Mike Stump11289f42009-09-09 15:08:12 +0000429
Sebastian Redl14236c82009-11-08 13:56:19 +0000430 Sema::OwningExprResult TransformCXXDefaultArgExpr(CXXDefaultArgExpr *E,
431 bool isAddressOfOperand);
432
Mike Stump11289f42009-09-09 15:08:12 +0000433 /// \brief Transforms a template type parameter type by performing
Douglas Gregord6ff3322009-08-04 16:50:30 +0000434 /// substitution of the corresponding template type argument.
John McCall550e0c22009-10-21 00:40:46 +0000435 QualType TransformTemplateTypeParmType(TypeLocBuilder &TLB,
436 TemplateTypeParmTypeLoc TL);
Douglas Gregord6ff3322009-08-04 16:50:30 +0000437 };
Douglas Gregor04318252009-07-06 15:59:29 +0000438}
439
Douglas Gregord6ff3322009-08-04 16:50:30 +0000440Decl *TemplateInstantiator::TransformDecl(Decl *D) {
Douglas Gregor2b6ca462009-09-03 21:38:09 +0000441 if (!D)
442 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000443
Douglas Gregor2b6ca462009-09-03 21:38:09 +0000444 if (TemplateTemplateParmDecl *TTP = dyn_cast<TemplateTemplateParmDecl>(D)) {
Douglas Gregor01afeef2009-08-28 20:31:08 +0000445 if (TTP->getDepth() < TemplateArgs.getNumLevels()) {
446 assert(TemplateArgs(TTP->getDepth(), TTP->getPosition()).getAsDecl() &&
447 "Wrong kind of template template argument");
Mike Stump11289f42009-09-09 15:08:12 +0000448 return cast<TemplateDecl>(TemplateArgs(TTP->getDepth(),
Douglas Gregor01afeef2009-08-28 20:31:08 +0000449 TTP->getPosition()).getAsDecl());
450 }
Mike Stump11289f42009-09-09 15:08:12 +0000451
452 // If the corresponding template argument is NULL or non-existent, it's
453 // because we are performing instantiation from explicitly-specified
Douglas Gregor01afeef2009-08-28 20:31:08 +0000454 // template arguments in a function template, but there were some
455 // arguments left unspecified.
Mike Stump11289f42009-09-09 15:08:12 +0000456 if (!TemplateArgs.hasTemplateArgument(TTP->getDepth(),
Douglas Gregor01afeef2009-08-28 20:31:08 +0000457 TTP->getPosition()))
458 return D;
Mike Stump11289f42009-09-09 15:08:12 +0000459
Douglas Gregor01afeef2009-08-28 20:31:08 +0000460 // FIXME: Implement depth reduction of template template parameters
Mike Stump11289f42009-09-09 15:08:12 +0000461 assert(false &&
Douglas Gregor01afeef2009-08-28 20:31:08 +0000462 "Reducing depth of template template parameters is not yet implemented");
Douglas Gregor71dc5092009-08-06 06:41:21 +0000463 }
Mike Stump11289f42009-09-09 15:08:12 +0000464
Douglas Gregor64621e62009-09-16 18:34:49 +0000465 return SemaRef.FindInstantiatedDecl(cast<NamedDecl>(D), TemplateArgs);
Douglas Gregord6ff3322009-08-04 16:50:30 +0000466}
467
Douglas Gregorebe10102009-08-20 07:17:43 +0000468Decl *TemplateInstantiator::TransformDefinition(Decl *D) {
John McCall76d824f2009-08-25 22:02:44 +0000469 Decl *Inst = getSema().SubstDecl(D, getSema().CurContext, TemplateArgs);
Douglas Gregorebe10102009-08-20 07:17:43 +0000470 if (!Inst)
471 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000472
Douglas Gregorebe10102009-08-20 07:17:43 +0000473 getSema().CurrentInstantiationScope->InstantiatedLocal(D, Inst);
474 return Inst;
475}
476
Douglas Gregora5cb6da2009-10-20 05:58:46 +0000477NamedDecl *
478TemplateInstantiator::TransformFirstQualifierInScope(NamedDecl *D,
479 SourceLocation Loc) {
480 // If the first part of the nested-name-specifier was a template type
481 // parameter, instantiate that type parameter down to a tag type.
482 if (TemplateTypeParmDecl *TTPD = dyn_cast_or_null<TemplateTypeParmDecl>(D)) {
483 const TemplateTypeParmType *TTP
484 = cast<TemplateTypeParmType>(getSema().Context.getTypeDeclType(TTPD));
485 if (TTP->getDepth() < TemplateArgs.getNumLevels()) {
486 QualType T = TemplateArgs(TTP->getDepth(), TTP->getIndex()).getAsType();
487 if (T.isNull())
488 return cast_or_null<NamedDecl>(TransformDecl(D));
489
490 if (const TagType *Tag = T->getAs<TagType>())
491 return Tag->getDecl();
492
493 // The resulting type is not a tag; complain.
494 getSema().Diag(Loc, diag::err_nested_name_spec_non_tag) << T;
495 return 0;
496 }
497 }
498
499 return cast_or_null<NamedDecl>(TransformDecl(D));
500}
501
Douglas Gregorebe10102009-08-20 07:17:43 +0000502VarDecl *
503TemplateInstantiator::RebuildExceptionDecl(VarDecl *ExceptionDecl,
Mike Stump11289f42009-09-09 15:08:12 +0000504 QualType T,
Douglas Gregorebe10102009-08-20 07:17:43 +0000505 DeclaratorInfo *Declarator,
506 IdentifierInfo *Name,
Mike Stump11289f42009-09-09 15:08:12 +0000507 SourceLocation Loc,
Douglas Gregorebe10102009-08-20 07:17:43 +0000508 SourceRange TypeRange) {
509 VarDecl *Var = inherited::RebuildExceptionDecl(ExceptionDecl, T, Declarator,
510 Name, Loc, TypeRange);
511 if (Var && !Var->isInvalidDecl())
512 getSema().CurrentInstantiationScope->InstantiatedLocal(ExceptionDecl, Var);
513 return Var;
514}
515
John McCall7f41d982009-09-11 04:59:25 +0000516QualType
517TemplateInstantiator::RebuildElaboratedType(QualType T,
518 ElaboratedType::TagKind Tag) {
519 if (const TagType *TT = T->getAs<TagType>()) {
520 TagDecl* TD = TT->getDecl();
521
522 // FIXME: this location is very wrong; we really need typelocs.
523 SourceLocation TagLocation = TD->getTagKeywordLoc();
524
525 // FIXME: type might be anonymous.
526 IdentifierInfo *Id = TD->getIdentifier();
527
528 // TODO: should we even warn on struct/class mismatches for this? Seems
529 // like it's likely to produce a lot of spurious errors.
530 if (!SemaRef.isAcceptableTagRedeclaration(TD, Tag, TagLocation, *Id)) {
531 SemaRef.Diag(TagLocation, diag::err_use_with_wrong_tag)
532 << Id
533 << CodeModificationHint::CreateReplacement(SourceRange(TagLocation),
534 TD->getKindName());
535 SemaRef.Diag(TD->getLocation(), diag::note_previous_use);
536 }
537 }
538
539 return TreeTransform<TemplateInstantiator>::RebuildElaboratedType(T, Tag);
540}
541
542Sema::OwningExprResult
Douglas Gregorc95a1fa2009-11-04 07:01:15 +0000543TemplateInstantiator::TransformPredefinedExpr(PredefinedExpr *E,
544 bool isAddressOfOperand) {
Anders Carlsson0b209a82009-09-11 01:22:35 +0000545 if (!E->isTypeDependent())
546 return SemaRef.Owned(E->Retain());
547
548 FunctionDecl *currentDecl = getSema().getCurFunctionDecl();
549 assert(currentDecl && "Must have current function declaration when "
550 "instantiating.");
551
552 PredefinedExpr::IdentType IT = E->getIdentType();
553
554 unsigned Length =
555 PredefinedExpr::ComputeName(getSema().Context, IT, currentDecl).length();
556
557 llvm::APInt LengthI(32, Length + 1);
John McCall8ccfcb52009-09-24 19:53:00 +0000558 QualType ResTy = getSema().Context.CharTy.withConst();
Anders Carlsson0b209a82009-09-11 01:22:35 +0000559 ResTy = getSema().Context.getConstantArrayType(ResTy, LengthI,
560 ArrayType::Normal, 0);
561 PredefinedExpr *PE =
562 new (getSema().Context) PredefinedExpr(E->getLocation(), ResTy, IT);
563 return getSema().Owned(PE);
564}
565
566Sema::OwningExprResult
Douglas Gregorc95a1fa2009-11-04 07:01:15 +0000567TemplateInstantiator::TransformDeclRefExpr(DeclRefExpr *E,
568 bool isAddressOfOperand) {
Douglas Gregora16548e2009-08-11 05:31:07 +0000569 // FIXME: Clean this up a bit
570 NamedDecl *D = E->getDecl();
571 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(D)) {
Douglas Gregor954de172009-10-31 17:21:17 +0000572 if (NTTP->getDepth() < TemplateArgs.getNumLevels()) {
573
574 // If the corresponding template argument is NULL or non-existent, it's
575 // because we are performing instantiation from explicitly-specified
576 // template arguments in a function template, but there were some
577 // arguments left unspecified.
578 if (!TemplateArgs.hasTemplateArgument(NTTP->getDepth(),
579 NTTP->getPosition()))
580 return SemaRef.Owned(E->Retain());
Mike Stump11289f42009-09-09 15:08:12 +0000581
Douglas Gregor954de172009-10-31 17:21:17 +0000582 const TemplateArgument &Arg = TemplateArgs(NTTP->getDepth(),
583 NTTP->getPosition());
Mike Stump11289f42009-09-09 15:08:12 +0000584
Douglas Gregor954de172009-10-31 17:21:17 +0000585 // The template argument itself might be an expression, in which
586 // case we just return that expression.
587 if (Arg.getKind() == TemplateArgument::Expression)
588 return SemaRef.Owned(Arg.getAsExpr()->Retain());
Mike Stump11289f42009-09-09 15:08:12 +0000589
Douglas Gregor954de172009-10-31 17:21:17 +0000590 if (Arg.getKind() == TemplateArgument::Declaration) {
591 ValueDecl *VD = cast<ValueDecl>(Arg.getAsDecl());
Mike Stump11289f42009-09-09 15:08:12 +0000592
Douglas Gregor954de172009-10-31 17:21:17 +0000593 VD = cast_or_null<ValueDecl>(
Douglas Gregorc95a1fa2009-11-04 07:01:15 +0000594 getSema().FindInstantiatedDecl(VD, TemplateArgs));
Douglas Gregor954de172009-10-31 17:21:17 +0000595 if (!VD)
596 return SemaRef.ExprError();
Mike Stump11289f42009-09-09 15:08:12 +0000597
Douglas Gregor954de172009-10-31 17:21:17 +0000598 return SemaRef.BuildDeclRefExpr(VD, VD->getType(), E->getLocation(),
599 /*FIXME:*/false, /*FIXME:*/false);
600 }
Mike Stump11289f42009-09-09 15:08:12 +0000601
Douglas Gregor954de172009-10-31 17:21:17 +0000602 assert(Arg.getKind() == TemplateArgument::Integral);
603 QualType T = Arg.getIntegralType();
604 if (T->isCharType() || T->isWideCharType())
605 return SemaRef.Owned(new (SemaRef.Context) CharacterLiteral(
606 Arg.getAsIntegral()->getZExtValue(),
607 T->isWideCharType(),
Mike Stump11289f42009-09-09 15:08:12 +0000608 T,
Douglas Gregor01afeef2009-08-28 20:31:08 +0000609 E->getSourceRange().getBegin()));
Douglas Gregor954de172009-10-31 17:21:17 +0000610 if (T->isBooleanType())
611 return SemaRef.Owned(new (SemaRef.Context) CXXBoolLiteralExpr(
612 Arg.getAsIntegral()->getBoolValue(),
613 T,
614 E->getSourceRange().getBegin()));
615
616 assert(Arg.getAsIntegral()->getBitWidth() == SemaRef.Context.getIntWidth(T));
617 return SemaRef.Owned(new (SemaRef.Context) IntegerLiteral(
618 *Arg.getAsIntegral(),
619 T,
620 E->getSourceRange().getBegin()));
621 }
622
623 // We have a non-type template parameter that isn't fully substituted;
624 // FindInstantiatedDecl will find it in the local instantiation scope.
Douglas Gregora16548e2009-08-11 05:31:07 +0000625 }
Mike Stump11289f42009-09-09 15:08:12 +0000626
Douglas Gregor64621e62009-09-16 18:34:49 +0000627 NamedDecl *InstD = SemaRef.FindInstantiatedDecl(D, TemplateArgs);
Douglas Gregora16548e2009-08-11 05:31:07 +0000628 if (!InstD)
629 return SemaRef.ExprError();
Mike Stump11289f42009-09-09 15:08:12 +0000630
Anders Carlsson4bb87ce2009-08-29 19:37:28 +0000631 // If we instantiated an UnresolvedUsingDecl and got back an UsingDecl,
Mike Stump11289f42009-09-09 15:08:12 +0000632 // we need to get the underlying decl.
Anders Carlsson4bb87ce2009-08-29 19:37:28 +0000633 // FIXME: Is this correct? Maybe FindInstantiatedDecl should do this?
634 InstD = InstD->getUnderlyingDecl();
Mike Stump11289f42009-09-09 15:08:12 +0000635
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000636 CXXScopeSpec SS;
637 NestedNameSpecifier *Qualifier = 0;
638 if (E->getQualifier()) {
639 Qualifier = TransformNestedNameSpecifier(E->getQualifier(),
640 E->getQualifierRange());
641 if (!Qualifier)
642 return SemaRef.ExprError();
643
644 SS.setScopeRep(Qualifier);
645 SS.setRange(E->getQualifierRange());
646 }
647
Mike Stump11289f42009-09-09 15:08:12 +0000648 return SemaRef.BuildDeclarationNameExpr(E->getLocation(), InstD,
Douglas Gregora16548e2009-08-11 05:31:07 +0000649 /*FIXME:*/false,
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000650 &SS,
Douglas Gregorc95a1fa2009-11-04 07:01:15 +0000651 isAddressOfOperand);
Douglas Gregora16548e2009-08-11 05:31:07 +0000652}
653
Sebastian Redl14236c82009-11-08 13:56:19 +0000654Sema::OwningExprResult TemplateInstantiator::TransformCXXDefaultArgExpr(
655 CXXDefaultArgExpr *E, bool isAddressOfOperand) {
656 assert(!cast<FunctionDecl>(E->getParam()->getDeclContext())->
657 getDescribedFunctionTemplate() &&
658 "Default arg expressions are never formed in dependent cases.");
659 return SemaRef.Owned(E->Retain());
660}
661
662
Mike Stump11289f42009-09-09 15:08:12 +0000663QualType
John McCall550e0c22009-10-21 00:40:46 +0000664TemplateInstantiator::TransformTemplateTypeParmType(TypeLocBuilder &TLB,
665 TemplateTypeParmTypeLoc TL) {
666 TemplateTypeParmType *T = TL.getTypePtr();
Douglas Gregor01afeef2009-08-28 20:31:08 +0000667 if (T->getDepth() < TemplateArgs.getNumLevels()) {
Douglas Gregorfe1e1102009-02-27 19:31:52 +0000668 // Replace the template type parameter with its corresponding
669 // template argument.
Mike Stump11289f42009-09-09 15:08:12 +0000670
671 // If the corresponding template argument is NULL or doesn't exist, it's
672 // because we are performing instantiation from explicitly-specified
673 // template arguments in a function template class, but there were some
Douglas Gregore3f1f352009-07-01 00:28:38 +0000674 // arguments left unspecified.
John McCall550e0c22009-10-21 00:40:46 +0000675 if (!TemplateArgs.hasTemplateArgument(T->getDepth(), T->getIndex())) {
676 TemplateTypeParmTypeLoc NewTL
677 = TLB.push<TemplateTypeParmTypeLoc>(TL.getType());
678 NewTL.setNameLoc(TL.getNameLoc());
679 return TL.getType();
680 }
Mike Stump11289f42009-09-09 15:08:12 +0000681
682 assert(TemplateArgs(T->getDepth(), T->getIndex()).getKind()
Douglas Gregor01afeef2009-08-28 20:31:08 +0000683 == TemplateArgument::Type &&
Douglas Gregorfe1e1102009-02-27 19:31:52 +0000684 "Template argument kind mismatch");
Douglas Gregor01afeef2009-08-28 20:31:08 +0000685
John McCallcebee162009-10-18 09:09:24 +0000686 QualType Replacement
687 = TemplateArgs(T->getDepth(), T->getIndex()).getAsType();
688
689 // TODO: only do this uniquing once, at the start of instantiation.
John McCall550e0c22009-10-21 00:40:46 +0000690 QualType Result
691 = getSema().Context.getSubstTemplateTypeParmType(T, Replacement);
692 SubstTemplateTypeParmTypeLoc NewTL
693 = TLB.push<SubstTemplateTypeParmTypeLoc>(Result);
694 NewTL.setNameLoc(TL.getNameLoc());
695 return Result;
Mike Stump11289f42009-09-09 15:08:12 +0000696 }
Douglas Gregorfe1e1102009-02-27 19:31:52 +0000697
698 // The template type parameter comes from an inner template (e.g.,
699 // the template parameter list of a member template inside the
700 // template we are instantiating). Create a new template type
701 // parameter with the template "level" reduced by one.
John McCall550e0c22009-10-21 00:40:46 +0000702 QualType Result
703 = getSema().Context.getTemplateTypeParmType(T->getDepth()
704 - TemplateArgs.getNumLevels(),
705 T->getIndex(),
706 T->isParameterPack(),
707 T->getName());
708 TemplateTypeParmTypeLoc NewTL = TLB.push<TemplateTypeParmTypeLoc>(Result);
709 NewTL.setNameLoc(TL.getNameLoc());
710 return Result;
Douglas Gregor17c0d7b2009-02-28 00:25:32 +0000711}
Douglas Gregorfe1e1102009-02-27 19:31:52 +0000712
John McCall76d824f2009-08-25 22:02:44 +0000713/// \brief Perform substitution on the type T with a given set of template
714/// arguments.
Douglas Gregorfe1e1102009-02-27 19:31:52 +0000715///
716/// This routine substitutes the given template arguments into the
717/// type T and produces the instantiated type.
718///
719/// \param T the type into which the template arguments will be
720/// substituted. If this type is not dependent, it will be returned
721/// immediately.
722///
723/// \param TemplateArgs the template arguments that will be
724/// substituted for the top-level template parameters within T.
725///
Douglas Gregorfe1e1102009-02-27 19:31:52 +0000726/// \param Loc the location in the source code where this substitution
727/// is being performed. It will typically be the location of the
728/// declarator (if we're instantiating the type of some declaration)
729/// or the location of the type in the source code (if, e.g., we're
730/// instantiating the type of a cast expression).
731///
732/// \param Entity the name of the entity associated with a declaration
733/// being instantiated (if any). May be empty to indicate that there
734/// is no such entity (if, e.g., this is a type that occurs as part of
735/// a cast expression) or that the entity has no name (e.g., an
736/// unnamed function parameter).
737///
738/// \returns If the instantiation succeeds, the instantiated
739/// type. Otherwise, produces diagnostics and returns a NULL type.
John McCall609459e2009-10-21 00:58:09 +0000740DeclaratorInfo *Sema::SubstType(DeclaratorInfo *T,
741 const MultiLevelTemplateArgumentList &Args,
742 SourceLocation Loc,
743 DeclarationName Entity) {
744 assert(!ActiveTemplateInstantiations.empty() &&
745 "Cannot perform an instantiation without some context on the "
746 "instantiation stack");
747
748 if (!T->getType()->isDependentType())
749 return T;
750
751 TemplateInstantiator Instantiator(*this, Args, Loc, Entity);
752 return Instantiator.TransformType(T);
753}
754
755/// Deprecated form of the above.
Mike Stump11289f42009-09-09 15:08:12 +0000756QualType Sema::SubstType(QualType T,
Douglas Gregor01afeef2009-08-28 20:31:08 +0000757 const MultiLevelTemplateArgumentList &TemplateArgs,
John McCall76d824f2009-08-25 22:02:44 +0000758 SourceLocation Loc, DeclarationName Entity) {
Douglas Gregor79cf6032009-03-10 20:44:00 +0000759 assert(!ActiveTemplateInstantiations.empty() &&
760 "Cannot perform an instantiation without some context on the "
761 "instantiation stack");
762
Douglas Gregorfe1e1102009-02-27 19:31:52 +0000763 // If T is not a dependent type, there is nothing to do.
764 if (!T->isDependentType())
765 return T;
766
Douglas Gregord6ff3322009-08-04 16:50:30 +0000767 TemplateInstantiator Instantiator(*this, TemplateArgs, Loc, Entity);
768 return Instantiator.TransformType(T);
Douglas Gregorfe1e1102009-02-27 19:31:52 +0000769}
Douglas Gregor463421d2009-03-03 04:44:36 +0000770
John McCall76d824f2009-08-25 22:02:44 +0000771/// \brief Perform substitution on the base class specifiers of the
772/// given class template specialization.
Douglas Gregor463421d2009-03-03 04:44:36 +0000773///
774/// Produces a diagnostic and returns true on error, returns false and
775/// attaches the instantiated base classes to the class template
776/// specialization if successful.
Mike Stump11289f42009-09-09 15:08:12 +0000777bool
John McCall76d824f2009-08-25 22:02:44 +0000778Sema::SubstBaseSpecifiers(CXXRecordDecl *Instantiation,
779 CXXRecordDecl *Pattern,
Douglas Gregor01afeef2009-08-28 20:31:08 +0000780 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor463421d2009-03-03 04:44:36 +0000781 bool Invalid = false;
Douglas Gregor6181ded2009-05-29 18:27:38 +0000782 llvm::SmallVector<CXXBaseSpecifier*, 4> InstantiatedBases;
Mike Stump11289f42009-09-09 15:08:12 +0000783 for (ClassTemplateSpecializationDecl::base_class_iterator
Douglas Gregor8ea8fd42009-03-25 21:17:03 +0000784 Base = Pattern->bases_begin(), BaseEnd = Pattern->bases_end();
Douglas Gregor2a72edd2009-03-10 18:52:44 +0000785 Base != BaseEnd; ++Base) {
Douglas Gregor463421d2009-03-03 04:44:36 +0000786 if (!Base->getType()->isDependentType()) {
Fariborz Jahanian5c14ec32009-07-22 17:41:53 +0000787 InstantiatedBases.push_back(new (Context) CXXBaseSpecifier(*Base));
Douglas Gregor463421d2009-03-03 04:44:36 +0000788 continue;
789 }
790
Mike Stump11289f42009-09-09 15:08:12 +0000791 QualType BaseType = SubstType(Base->getType(),
792 TemplateArgs,
John McCall76d824f2009-08-25 22:02:44 +0000793 Base->getSourceRange().getBegin(),
794 DeclarationName());
Douglas Gregor463421d2009-03-03 04:44:36 +0000795 if (BaseType.isNull()) {
796 Invalid = true;
797 continue;
798 }
799
800 if (CXXBaseSpecifier *InstantiatedBase
Douglas Gregor8ea8fd42009-03-25 21:17:03 +0000801 = CheckBaseSpecifier(Instantiation,
Douglas Gregor463421d2009-03-03 04:44:36 +0000802 Base->getSourceRange(),
803 Base->isVirtual(),
804 Base->getAccessSpecifierAsWritten(),
805 BaseType,
806 /*FIXME: Not totally accurate */
807 Base->getSourceRange().getBegin()))
808 InstantiatedBases.push_back(InstantiatedBase);
809 else
810 Invalid = true;
811 }
812
Douglas Gregor2a72edd2009-03-10 18:52:44 +0000813 if (!Invalid &&
Jay Foad7d0479f2009-05-21 09:52:38 +0000814 AttachBaseSpecifiers(Instantiation, InstantiatedBases.data(),
Douglas Gregor463421d2009-03-03 04:44:36 +0000815 InstantiatedBases.size()))
816 Invalid = true;
817
818 return Invalid;
819}
820
Douglas Gregor8ea8fd42009-03-25 21:17:03 +0000821/// \brief Instantiate the definition of a class from a given pattern.
822///
823/// \param PointOfInstantiation The point of instantiation within the
824/// source code.
825///
826/// \param Instantiation is the declaration whose definition is being
827/// instantiated. This will be either a class template specialization
828/// or a member class of a class template specialization.
829///
830/// \param Pattern is the pattern from which the instantiation
831/// occurs. This will be either the declaration of a class template or
832/// the declaration of a member class of a class template.
833///
834/// \param TemplateArgs The template arguments to be substituted into
835/// the pattern.
836///
Douglas Gregor34ec2ef2009-09-04 22:48:11 +0000837/// \param TSK the kind of implicit or explicit instantiation to perform.
Douglas Gregor8a2e6012009-08-24 15:23:48 +0000838///
839/// \param Complain whether to complain if the class cannot be instantiated due
840/// to the lack of a definition.
841///
Douglas Gregor8ea8fd42009-03-25 21:17:03 +0000842/// \returns true if an error occurred, false otherwise.
843bool
844Sema::InstantiateClass(SourceLocation PointOfInstantiation,
845 CXXRecordDecl *Instantiation, CXXRecordDecl *Pattern,
Douglas Gregor01afeef2009-08-28 20:31:08 +0000846 const MultiLevelTemplateArgumentList &TemplateArgs,
Douglas Gregor34ec2ef2009-09-04 22:48:11 +0000847 TemplateSpecializationKind TSK,
Douglas Gregor8a2e6012009-08-24 15:23:48 +0000848 bool Complain) {
Douglas Gregor8ea8fd42009-03-25 21:17:03 +0000849 bool Invalid = false;
John McCall87a44eb2009-08-20 01:44:21 +0000850
Mike Stump11289f42009-09-09 15:08:12 +0000851 CXXRecordDecl *PatternDef
Douglas Gregor8ea8fd42009-03-25 21:17:03 +0000852 = cast_or_null<CXXRecordDecl>(Pattern->getDefinition(Context));
853 if (!PatternDef) {
Douglas Gregor8a2e6012009-08-24 15:23:48 +0000854 if (!Complain) {
855 // Say nothing
856 } else if (Pattern == Instantiation->getInstantiatedFromMemberClass()) {
Douglas Gregor8ea8fd42009-03-25 21:17:03 +0000857 Diag(PointOfInstantiation,
858 diag::err_implicit_instantiate_member_undefined)
859 << Context.getTypeDeclType(Instantiation);
860 Diag(Pattern->getLocation(), diag::note_member_of_template_here);
861 } else {
Douglas Gregora1f49972009-05-13 00:25:59 +0000862 Diag(PointOfInstantiation, diag::err_template_instantiate_undefined)
Douglas Gregor34ec2ef2009-09-04 22:48:11 +0000863 << (TSK != TSK_ImplicitInstantiation)
Douglas Gregor8ea8fd42009-03-25 21:17:03 +0000864 << Context.getTypeDeclType(Instantiation);
865 Diag(Pattern->getLocation(), diag::note_template_decl_here);
866 }
867 return true;
868 }
869 Pattern = PatternDef;
870
Douglas Gregord6ba93d2009-10-15 15:54:05 +0000871 // \brief Record the point of instantiation.
872 if (MemberSpecializationInfo *MSInfo
873 = Instantiation->getMemberSpecializationInfo()) {
874 MSInfo->setTemplateSpecializationKind(TSK);
875 MSInfo->setPointOfInstantiation(PointOfInstantiation);
Douglas Gregoref6ab412009-10-27 06:26:26 +0000876 } else if (ClassTemplateSpecializationDecl *Spec
877 = dyn_cast<ClassTemplateSpecializationDecl>(Instantiation)) {
878 Spec->setTemplateSpecializationKind(TSK);
879 Spec->setPointOfInstantiation(PointOfInstantiation);
Douglas Gregord6ba93d2009-10-15 15:54:05 +0000880 }
881
Douglas Gregorf3430ae2009-03-25 21:23:52 +0000882 InstantiatingTemplate Inst(*this, PointOfInstantiation, Instantiation);
Douglas Gregor8ea8fd42009-03-25 21:17:03 +0000883 if (Inst)
884 return true;
885
886 // Enter the scope of this instantiation. We don't use
887 // PushDeclContext because we don't have a scope.
888 DeclContext *PreviousContext = CurContext;
889 CurContext = Instantiation;
890
891 // Start the definition of this instantiation.
892 Instantiation->startDefinition();
893
John McCall76d824f2009-08-25 22:02:44 +0000894 // Do substitution on the base class specifiers.
895 if (SubstBaseSpecifiers(Instantiation, Pattern, TemplateArgs))
Douglas Gregor8ea8fd42009-03-25 21:17:03 +0000896 Invalid = true;
897
Douglas Gregor6181ded2009-05-29 18:27:38 +0000898 llvm::SmallVector<DeclPtrTy, 4> Fields;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000899 for (RecordDecl::decl_iterator Member = Pattern->decls_begin(),
Mike Stump11289f42009-09-09 15:08:12 +0000900 MemberEnd = Pattern->decls_end();
Douglas Gregorbcced4e2009-04-09 21:40:53 +0000901 Member != MemberEnd; ++Member) {
John McCall76d824f2009-08-25 22:02:44 +0000902 Decl *NewMember = SubstDecl(*Member, Instantiation, TemplateArgs);
Douglas Gregor8ea8fd42009-03-25 21:17:03 +0000903 if (NewMember) {
904 if (NewMember->isInvalidDecl())
905 Invalid = true;
906 else if (FieldDecl *Field = dyn_cast<FieldDecl>(NewMember))
Chris Lattner83f095c2009-03-28 19:18:32 +0000907 Fields.push_back(DeclPtrTy::make(Field));
Anders Carlsson4bb87ce2009-08-29 19:37:28 +0000908 else if (UsingDecl *UD = dyn_cast<UsingDecl>(NewMember))
909 Instantiation->addDecl(UD);
Douglas Gregor8ea8fd42009-03-25 21:17:03 +0000910 } else {
911 // FIXME: Eventually, a NULL return will mean that one of the
Mike Stump87c57ac2009-05-16 07:39:55 +0000912 // instantiations was a semantic disaster, and we'll want to set Invalid =
913 // true. For now, we expect to skip some members that we can't yet handle.
Douglas Gregor8ea8fd42009-03-25 21:17:03 +0000914 }
915 }
916
917 // Finish checking fields.
Chris Lattner83f095c2009-03-28 19:18:32 +0000918 ActOnFields(0, Instantiation->getLocation(), DeclPtrTy::make(Instantiation),
Jay Foad7d0479f2009-05-21 09:52:38 +0000919 Fields.data(), Fields.size(), SourceLocation(), SourceLocation(),
Douglas Gregor8ea8fd42009-03-25 21:17:03 +0000920 0);
Douglas Gregor3c74d412009-10-14 20:14:33 +0000921 if (Instantiation->isInvalidDecl())
922 Invalid = true;
923
Douglas Gregor8ea8fd42009-03-25 21:17:03 +0000924 // Add any implicitly-declared members that we might need.
Douglas Gregor3c74d412009-10-14 20:14:33 +0000925 if (!Invalid)
926 AddImplicitlyDeclaredMembersToClass(Instantiation);
Douglas Gregor8ea8fd42009-03-25 21:17:03 +0000927
928 // Exit the scope of this instantiation.
929 CurContext = PreviousContext;
930
Douglas Gregor28ad4b52009-05-26 20:50:29 +0000931 if (!Invalid)
932 Consumer.HandleTagDeclDefinition(Instantiation);
933
Douglas Gregor8ea8fd42009-03-25 21:17:03 +0000934 return Invalid;
935}
936
Mike Stump11289f42009-09-09 15:08:12 +0000937bool
Douglas Gregor463421d2009-03-03 04:44:36 +0000938Sema::InstantiateClassTemplateSpecialization(
Douglas Gregoref6ab412009-10-27 06:26:26 +0000939 SourceLocation PointOfInstantiation,
Douglas Gregor463421d2009-03-03 04:44:36 +0000940 ClassTemplateSpecializationDecl *ClassTemplateSpec,
Douglas Gregor34ec2ef2009-09-04 22:48:11 +0000941 TemplateSpecializationKind TSK,
Douglas Gregor8a2e6012009-08-24 15:23:48 +0000942 bool Complain) {
Douglas Gregor463421d2009-03-03 04:44:36 +0000943 // Perform the actual instantiation on the canonical declaration.
944 ClassTemplateSpec = cast<ClassTemplateSpecializationDecl>(
Argyrios Kyrtzidis6b7e3762009-07-18 00:34:25 +0000945 ClassTemplateSpec->getCanonicalDecl());
Douglas Gregor463421d2009-03-03 04:44:36 +0000946
Douglas Gregor4aa04b12009-09-11 21:19:12 +0000947 // Check whether we have already instantiated or specialized this class
948 // template specialization.
949 if (ClassTemplateSpec->getSpecializationKind() != TSK_Undeclared) {
950 if (ClassTemplateSpec->getSpecializationKind() ==
951 TSK_ExplicitInstantiationDeclaration &&
952 TSK == TSK_ExplicitInstantiationDefinition) {
953 // An explicit instantiation definition follows an explicit instantiation
954 // declaration (C++0x [temp.explicit]p10); go ahead and perform the
955 // explicit instantiation.
956 ClassTemplateSpec->setSpecializationKind(TSK);
Douglas Gregor4aa04b12009-09-11 21:19:12 +0000957 return false;
958 }
959
960 // We can only instantiate something that hasn't already been
961 // instantiated or specialized. Fail without any diagnostics: our
962 // caller will provide an error message.
Douglas Gregor463421d2009-03-03 04:44:36 +0000963 return true;
Douglas Gregor4aa04b12009-09-11 21:19:12 +0000964 }
Douglas Gregor463421d2009-03-03 04:44:36 +0000965
Douglas Gregor00a511f2009-09-15 16:51:42 +0000966 if (ClassTemplateSpec->isInvalidDecl())
967 return true;
968
Douglas Gregor463421d2009-03-03 04:44:36 +0000969 ClassTemplateDecl *Template = ClassTemplateSpec->getSpecializedTemplate();
Douglas Gregor01afeef2009-08-28 20:31:08 +0000970 CXXRecordDecl *Pattern = 0;
Douglas Gregor2373c592009-05-31 09:31:02 +0000971
Douglas Gregor170bc422009-06-12 22:31:52 +0000972 // C++ [temp.class.spec.match]p1:
973 // When a class template is used in a context that requires an
974 // instantiation of the class, it is necessary to determine
975 // whether the instantiation is to be generated using the primary
976 // template or one of the partial specializations. This is done by
977 // matching the template arguments of the class template
978 // specialization with the template argument lists of the partial
979 // specializations.
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000980 typedef std::pair<ClassTemplatePartialSpecializationDecl *,
981 TemplateArgumentList *> MatchResult;
982 llvm::SmallVector<MatchResult, 4> Matched;
Mike Stump11289f42009-09-09 15:08:12 +0000983 for (llvm::FoldingSet<ClassTemplatePartialSpecializationDecl>::iterator
Douglas Gregor2373c592009-05-31 09:31:02 +0000984 Partial = Template->getPartialSpecializations().begin(),
985 PartialEnd = Template->getPartialSpecializations().end();
986 Partial != PartialEnd;
987 ++Partial) {
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000988 TemplateDeductionInfo Info(Context);
989 if (TemplateDeductionResult Result
Mike Stump11289f42009-09-09 15:08:12 +0000990 = DeduceTemplateArguments(&*Partial,
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000991 ClassTemplateSpec->getTemplateArgs(),
992 Info)) {
993 // FIXME: Store the failed-deduction information for use in
994 // diagnostics, later.
995 (void)Result;
996 } else {
997 Matched.push_back(std::make_pair(&*Partial, Info.take()));
998 }
Douglas Gregor2373c592009-05-31 09:31:02 +0000999 }
1000
Douglas Gregor21610382009-10-29 00:04:11 +00001001 if (Matched.size() >= 1) {
Douglas Gregorbe999392009-09-15 16:23:51 +00001002 llvm::SmallVector<MatchResult, 4>::iterator Best = Matched.begin();
Douglas Gregor21610382009-10-29 00:04:11 +00001003 if (Matched.size() == 1) {
1004 // -- If exactly one matching specialization is found, the
1005 // instantiation is generated from that specialization.
1006 // We don't need to do anything for this.
1007 } else {
1008 // -- If more than one matching specialization is found, the
1009 // partial order rules (14.5.4.2) are used to determine
1010 // whether one of the specializations is more specialized
1011 // than the others. If none of the specializations is more
1012 // specialized than all of the other matching
1013 // specializations, then the use of the class template is
1014 // ambiguous and the program is ill-formed.
1015 for (llvm::SmallVector<MatchResult, 4>::iterator P = Best + 1,
1016 PEnd = Matched.end();
1017 P != PEnd; ++P) {
1018 if (getMoreSpecializedPartialSpecialization(P->first, Best->first)
1019 == P->first)
1020 Best = P;
Douglas Gregorbe999392009-09-15 16:23:51 +00001021 }
Douglas Gregorbe999392009-09-15 16:23:51 +00001022
Douglas Gregor21610382009-10-29 00:04:11 +00001023 // Determine if the best partial specialization is more specialized than
1024 // the others.
1025 bool Ambiguous = false;
Douglas Gregorbe999392009-09-15 16:23:51 +00001026 for (llvm::SmallVector<MatchResult, 4>::iterator P = Matched.begin(),
1027 PEnd = Matched.end();
Douglas Gregor21610382009-10-29 00:04:11 +00001028 P != PEnd; ++P) {
1029 if (P != Best &&
1030 getMoreSpecializedPartialSpecialization(P->first, Best->first)
1031 != Best->first) {
1032 Ambiguous = true;
1033 break;
1034 }
1035 }
1036
1037 if (Ambiguous) {
1038 // Partial ordering did not produce a clear winner. Complain.
1039 ClassTemplateSpec->setInvalidDecl();
1040 Diag(PointOfInstantiation, diag::err_partial_spec_ordering_ambiguous)
1041 << ClassTemplateSpec;
1042
1043 // Print the matching partial specializations.
1044 for (llvm::SmallVector<MatchResult, 4>::iterator P = Matched.begin(),
1045 PEnd = Matched.end();
1046 P != PEnd; ++P)
1047 Diag(P->first->getLocation(), diag::note_partial_spec_match)
1048 << getTemplateArgumentBindingsText(P->first->getTemplateParameters(),
1049 *P->second);
Douglas Gregor01afeef2009-08-28 20:31:08 +00001050
Douglas Gregor21610382009-10-29 00:04:11 +00001051 return true;
1052 }
Douglas Gregorbe999392009-09-15 16:23:51 +00001053 }
1054
1055 // Instantiate using the best class template partial specialization.
Douglas Gregor21610382009-10-29 00:04:11 +00001056 ClassTemplatePartialSpecializationDecl *OrigPartialSpec = Best->first;
1057 while (OrigPartialSpec->getInstantiatedFromMember()) {
1058 // If we've found an explicit specialization of this class template,
1059 // stop here and use that as the pattern.
1060 if (OrigPartialSpec->isMemberSpecialization())
1061 break;
1062
1063 OrigPartialSpec = OrigPartialSpec->getInstantiatedFromMember();
1064 }
1065
1066 Pattern = OrigPartialSpec;
Douglas Gregorbe999392009-09-15 16:23:51 +00001067 ClassTemplateSpec->setInstantiationOf(Best->first, Best->second);
Douglas Gregor170bc422009-06-12 22:31:52 +00001068 } else {
1069 // -- If no matches are found, the instantiation is generated
1070 // from the primary template.
Douglas Gregor01afeef2009-08-28 20:31:08 +00001071 ClassTemplateDecl *OrigTemplate = Template;
Douglas Gregorcf915552009-10-13 16:30:37 +00001072 while (OrigTemplate->getInstantiatedFromMemberTemplate()) {
1073 // If we've found an explicit specialization of this class template,
1074 // stop here and use that as the pattern.
1075 if (OrigTemplate->isMemberSpecialization())
1076 break;
1077
Douglas Gregor01afeef2009-08-28 20:31:08 +00001078 OrigTemplate = OrigTemplate->getInstantiatedFromMemberTemplate();
Douglas Gregorcf915552009-10-13 16:30:37 +00001079 }
1080
Douglas Gregor01afeef2009-08-28 20:31:08 +00001081 Pattern = OrigTemplate->getTemplatedDecl();
Douglas Gregor2373c592009-05-31 09:31:02 +00001082 }
Douglas Gregor463421d2009-03-03 04:44:36 +00001083
Douglas Gregoref6ab412009-10-27 06:26:26 +00001084 bool Result = InstantiateClass(PointOfInstantiation, ClassTemplateSpec,
1085 Pattern,
1086 getTemplateInstantiationArgs(ClassTemplateSpec),
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001087 TSK,
Douglas Gregor8a2e6012009-08-24 15:23:48 +00001088 Complain);
Mike Stump11289f42009-09-09 15:08:12 +00001089
Douglas Gregorb7ae10f2009-06-05 00:53:49 +00001090 for (unsigned I = 0, N = Matched.size(); I != N; ++I) {
1091 // FIXME: Implement TemplateArgumentList::Destroy!
1092 // if (Matched[I].first != Pattern)
1093 // Matched[I].second->Destroy(Context);
1094 }
Mike Stump11289f42009-09-09 15:08:12 +00001095
Douglas Gregorb7ae10f2009-06-05 00:53:49 +00001096 return Result;
Douglas Gregor463421d2009-03-03 04:44:36 +00001097}
Douglas Gregor90a1a652009-03-19 17:26:29 +00001098
John McCall76d824f2009-08-25 22:02:44 +00001099/// \brief Instantiates the definitions of all of the member
1100/// of the given class, which is an instantiation of a class template
1101/// or a member class of a template.
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00001102void
1103Sema::InstantiateClassMembers(SourceLocation PointOfInstantiation,
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001104 CXXRecordDecl *Instantiation,
1105 const MultiLevelTemplateArgumentList &TemplateArgs,
1106 TemplateSpecializationKind TSK) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001107 for (DeclContext::decl_iterator D = Instantiation->decls_begin(),
1108 DEnd = Instantiation->decls_end();
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00001109 D != DEnd; ++D) {
Douglas Gregor1d957a32009-10-27 18:42:08 +00001110 bool SuppressNew = false;
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00001111 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(*D)) {
Douglas Gregor1d957a32009-10-27 18:42:08 +00001112 if (FunctionDecl *Pattern
1113 = Function->getInstantiatedFromMemberFunction()) {
1114 MemberSpecializationInfo *MSInfo
1115 = Function->getMemberSpecializationInfo();
1116 assert(MSInfo && "No member specialization information?");
1117 if (CheckSpecializationInstantiationRedecl(PointOfInstantiation, TSK,
1118 Function,
1119 MSInfo->getTemplateSpecializationKind(),
1120 MSInfo->getPointOfInstantiation(),
1121 SuppressNew) ||
1122 SuppressNew)
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001123 continue;
1124
Douglas Gregor1d957a32009-10-27 18:42:08 +00001125 if (Function->getBody())
1126 continue;
1127
1128 if (TSK == TSK_ExplicitInstantiationDefinition) {
1129 // C++0x [temp.explicit]p8:
1130 // An explicit instantiation definition that names a class template
1131 // specialization explicitly instantiates the class template
1132 // specialization and is only an explicit instantiation definition
1133 // of members whose definition is visible at the point of
1134 // instantiation.
1135 if (!Pattern->getBody())
1136 continue;
1137
1138 Function->setTemplateSpecializationKind(TSK, PointOfInstantiation);
1139
1140 InstantiateFunctionDefinition(PointOfInstantiation, Function);
1141 } else {
1142 Function->setTemplateSpecializationKind(TSK, PointOfInstantiation);
1143 }
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001144 }
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00001145 } else if (VarDecl *Var = dyn_cast<VarDecl>(*D)) {
Douglas Gregor86d142a2009-10-08 07:24:58 +00001146 if (Var->isStaticDataMember()) {
Douglas Gregor1d957a32009-10-27 18:42:08 +00001147 MemberSpecializationInfo *MSInfo = Var->getMemberSpecializationInfo();
1148 assert(MSInfo && "No member specialization information?");
1149 if (CheckSpecializationInstantiationRedecl(PointOfInstantiation, TSK,
1150 Var,
1151 MSInfo->getTemplateSpecializationKind(),
1152 MSInfo->getPointOfInstantiation(),
1153 SuppressNew) ||
1154 SuppressNew)
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001155 continue;
1156
Douglas Gregor1d957a32009-10-27 18:42:08 +00001157 if (TSK == TSK_ExplicitInstantiationDefinition) {
1158 // C++0x [temp.explicit]p8:
1159 // An explicit instantiation definition that names a class template
1160 // specialization explicitly instantiates the class template
1161 // specialization and is only an explicit instantiation definition
1162 // of members whose definition is visible at the point of
1163 // instantiation.
1164 if (!Var->getInstantiatedFromStaticDataMember()
1165 ->getOutOfLineDefinition())
1166 continue;
1167
1168 Var->setTemplateSpecializationKind(TSK, PointOfInstantiation);
Douglas Gregor86d142a2009-10-08 07:24:58 +00001169 InstantiateStaticDataMemberDefinition(PointOfInstantiation, Var);
Douglas Gregor1d957a32009-10-27 18:42:08 +00001170 } else {
1171 Var->setTemplateSpecializationKind(TSK, PointOfInstantiation);
1172 }
1173 }
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00001174 } else if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(*D)) {
Douglas Gregord801b062009-10-07 23:56:10 +00001175 if (Record->isInjectedClassName())
1176 continue;
1177
Douglas Gregor1d957a32009-10-27 18:42:08 +00001178 MemberSpecializationInfo *MSInfo = Record->getMemberSpecializationInfo();
1179 assert(MSInfo && "No member specialization information?");
1180 if (CheckSpecializationInstantiationRedecl(PointOfInstantiation, TSK,
1181 Record,
1182 MSInfo->getTemplateSpecializationKind(),
1183 MSInfo->getPointOfInstantiation(),
1184 SuppressNew) ||
1185 SuppressNew)
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001186 continue;
1187
Douglas Gregor1d957a32009-10-27 18:42:08 +00001188 CXXRecordDecl *Pattern = Record->getInstantiatedFromMemberClass();
1189 assert(Pattern && "Missing instantiated-from-template information");
1190
1191 if (!Record->getDefinition(Context)) {
1192 if (!Pattern->getDefinition(Context)) {
1193 // C++0x [temp.explicit]p8:
1194 // An explicit instantiation definition that names a class template
1195 // specialization explicitly instantiates the class template
1196 // specialization and is only an explicit instantiation definition
1197 // of members whose definition is visible at the point of
1198 // instantiation.
1199 if (TSK == TSK_ExplicitInstantiationDeclaration) {
1200 MSInfo->setTemplateSpecializationKind(TSK);
1201 MSInfo->setPointOfInstantiation(PointOfInstantiation);
1202 }
1203
1204 continue;
1205 }
1206
1207 InstantiateClass(PointOfInstantiation, Record, Pattern,
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001208 TemplateArgs,
1209 TSK);
Douglas Gregor1d957a32009-10-27 18:42:08 +00001210 }
Douglas Gregorc093c1d2009-10-08 01:19:17 +00001211
Douglas Gregor1d957a32009-10-27 18:42:08 +00001212 Pattern = cast_or_null<CXXRecordDecl>(Record->getDefinition(Context));
1213 if (Pattern)
1214 InstantiateClassMembers(PointOfInstantiation, Pattern, TemplateArgs,
1215 TSK);
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00001216 }
1217 }
1218}
1219
1220/// \brief Instantiate the definitions of all of the members of the
1221/// given class template specialization, which was named as part of an
1222/// explicit instantiation.
Mike Stump11289f42009-09-09 15:08:12 +00001223void
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001224Sema::InstantiateClassTemplateSpecializationMembers(
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00001225 SourceLocation PointOfInstantiation,
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001226 ClassTemplateSpecializationDecl *ClassTemplateSpec,
1227 TemplateSpecializationKind TSK) {
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00001228 // C++0x [temp.explicit]p7:
1229 // An explicit instantiation that names a class template
1230 // specialization is an explicit instantion of the same kind
1231 // (declaration or definition) of each of its members (not
1232 // including members inherited from base classes) that has not
1233 // been previously explicitly specialized in the translation unit
1234 // containing the explicit instantiation, except as described
1235 // below.
1236 InstantiateClassMembers(PointOfInstantiation, ClassTemplateSpec,
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001237 getTemplateInstantiationArgs(ClassTemplateSpec),
1238 TSK);
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00001239}
1240
Mike Stump11289f42009-09-09 15:08:12 +00001241Sema::OwningStmtResult
Douglas Gregor01afeef2009-08-28 20:31:08 +00001242Sema::SubstStmt(Stmt *S, const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregorebe10102009-08-20 07:17:43 +00001243 if (!S)
1244 return Owned(S);
1245
1246 TemplateInstantiator Instantiator(*this, TemplateArgs,
1247 SourceLocation(),
1248 DeclarationName());
1249 return Instantiator.TransformStmt(S);
1250}
1251
Mike Stump11289f42009-09-09 15:08:12 +00001252Sema::OwningExprResult
Douglas Gregor01afeef2009-08-28 20:31:08 +00001253Sema::SubstExpr(Expr *E, const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregora16548e2009-08-11 05:31:07 +00001254 if (!E)
1255 return Owned(E);
Mike Stump11289f42009-09-09 15:08:12 +00001256
Douglas Gregora16548e2009-08-11 05:31:07 +00001257 TemplateInstantiator Instantiator(*this, TemplateArgs,
1258 SourceLocation(),
1259 DeclarationName());
1260 return Instantiator.TransformExpr(E);
1261}
1262
John McCall76d824f2009-08-25 22:02:44 +00001263/// \brief Do template substitution on a nested-name-specifier.
Douglas Gregorf21eb492009-03-26 23:50:42 +00001264NestedNameSpecifier *
John McCall76d824f2009-08-25 22:02:44 +00001265Sema::SubstNestedNameSpecifier(NestedNameSpecifier *NNS,
Douglas Gregor01afeef2009-08-28 20:31:08 +00001266 SourceRange Range,
1267 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor1135c352009-08-06 05:28:30 +00001268 TemplateInstantiator Instantiator(*this, TemplateArgs, Range.getBegin(),
1269 DeclarationName());
1270 return Instantiator.TransformNestedNameSpecifier(NNS, Range);
Douglas Gregor90a1a652009-03-19 17:26:29 +00001271}
Douglas Gregoraa594892009-03-31 18:38:02 +00001272
1273TemplateName
John McCall76d824f2009-08-25 22:02:44 +00001274Sema::SubstTemplateName(TemplateName Name, SourceLocation Loc,
Douglas Gregor01afeef2009-08-28 20:31:08 +00001275 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor71dc5092009-08-06 06:41:21 +00001276 TemplateInstantiator Instantiator(*this, TemplateArgs, Loc,
1277 DeclarationName());
1278 return Instantiator.TransformTemplateName(Name);
Douglas Gregoraa594892009-03-31 18:38:02 +00001279}
Douglas Gregorc43620d2009-06-11 00:06:24 +00001280
John McCall0ad16662009-10-29 08:12:44 +00001281bool Sema::Subst(const TemplateArgumentLoc &Input, TemplateArgumentLoc &Output,
1282 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregore922c772009-08-04 22:27:00 +00001283 TemplateInstantiator Instantiator(*this, TemplateArgs, SourceLocation(),
1284 DeclarationName());
John McCall0ad16662009-10-29 08:12:44 +00001285
1286 return Instantiator.TransformTemplateArgument(Input, Output);
Douglas Gregorc43620d2009-06-11 00:06:24 +00001287}