blob: 98b8ccd8d850d987f92d514bbcb3b5a5f35e36dc [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
John McCall83024632010-08-25 22:03:47 +000013#include "clang/Sema/SemaInternal.h"
Douglas Gregord6ff3322009-08-04 16:50:30 +000014#include "TreeTransform.h"
John McCall8b0666c2010-08-20 18:27:03 +000015#include "clang/Sema/DeclSpec.h"
Douglas Gregorc3a6ade2010-08-12 20:07:10 +000016#include "clang/Sema/Lookup.h"
John McCallde6836a2010-08-24 07:21:54 +000017#include "clang/Sema/Template.h"
John McCall19c1bfd2010-08-25 05:32:35 +000018#include "clang/Sema/TemplateDeduction.h"
Douglas Gregor28ad4b52009-05-26 20:50:29 +000019#include "clang/AST/ASTConsumer.h"
Douglas Gregorfe1e1102009-02-27 19:31:52 +000020#include "clang/AST/ASTContext.h"
21#include "clang/AST/Expr.h"
Douglas Gregorfe1e1102009-02-27 19:31:52 +000022#include "clang/AST/DeclTemplate.h"
Douglas Gregorfe1e1102009-02-27 19:31:52 +000023#include "clang/Basic/LangOptions.h"
24
25using namespace clang;
John McCall19c1bfd2010-08-25 05:32:35 +000026using namespace sema;
Douglas Gregorfe1e1102009-02-27 19:31:52 +000027
Douglas Gregor4ea568f2009-03-10 18:03:33 +000028//===----------------------------------------------------------------------===/
29// Template Instantiation Support
30//===----------------------------------------------------------------------===/
31
Douglas Gregor01afeef2009-08-28 20:31:08 +000032/// \brief Retrieve the template argument list(s) that should be used to
33/// instantiate the definition of the given declaration.
Douglas Gregor36d7c5f2009-11-09 19:17:50 +000034///
35/// \param D the declaration for which we are computing template instantiation
36/// arguments.
37///
38/// \param Innermost if non-NULL, the innermost template argument list.
Douglas Gregor8c702532010-02-05 07:33:43 +000039///
40/// \param RelativeToPrimary true if we should get the template
41/// arguments relative to the primary template, even when we're
42/// dealing with a specialization. This is only relevant for function
43/// template specializations.
Douglas Gregor1bd7a942010-05-03 23:29:10 +000044///
45/// \param Pattern If non-NULL, indicates the pattern from which we will be
46/// instantiating the definition of the given declaration, \p D. This is
47/// used to determine the proper set of template instantiation arguments for
48/// friend function template specializations.
Douglas Gregora654dd82009-08-28 17:37:35 +000049MultiLevelTemplateArgumentList
Douglas Gregor36d7c5f2009-11-09 19:17:50 +000050Sema::getTemplateInstantiationArgs(NamedDecl *D,
Douglas Gregor8c702532010-02-05 07:33:43 +000051 const TemplateArgumentList *Innermost,
Douglas Gregor1bd7a942010-05-03 23:29:10 +000052 bool RelativeToPrimary,
53 const FunctionDecl *Pattern) {
Douglas Gregora654dd82009-08-28 17:37:35 +000054 // Accumulate the set of template argument lists in this structure.
55 MultiLevelTemplateArgumentList Result;
Mike Stump11289f42009-09-09 15:08:12 +000056
Douglas Gregor36d7c5f2009-11-09 19:17:50 +000057 if (Innermost)
58 Result.addOuterTemplateArguments(Innermost);
59
Douglas Gregora654dd82009-08-28 17:37:35 +000060 DeclContext *Ctx = dyn_cast<DeclContext>(D);
61 if (!Ctx)
62 Ctx = D->getDeclContext();
Mike Stump11289f42009-09-09 15:08:12 +000063
John McCall970d5302009-08-29 03:16:09 +000064 while (!Ctx->isFileContext()) {
Douglas Gregora654dd82009-08-28 17:37:35 +000065 // Add template arguments from a class template instantiation.
Mike Stump11289f42009-09-09 15:08:12 +000066 if (ClassTemplateSpecializationDecl *Spec
Douglas Gregora654dd82009-08-28 17:37:35 +000067 = dyn_cast<ClassTemplateSpecializationDecl>(Ctx)) {
68 // We're done when we hit an explicit specialization.
Douglas Gregor9961ce92010-07-08 18:37:38 +000069 if (Spec->getSpecializationKind() == TSK_ExplicitSpecialization &&
70 !isa<ClassTemplatePartialSpecializationDecl>(Spec))
Douglas Gregora654dd82009-08-28 17:37:35 +000071 break;
Mike Stump11289f42009-09-09 15:08:12 +000072
Douglas Gregora654dd82009-08-28 17:37:35 +000073 Result.addOuterTemplateArguments(&Spec->getTemplateInstantiationArgs());
Douglas Gregorcf915552009-10-13 16:30:37 +000074
75 // If this class template specialization was instantiated from a
76 // specialized member that is a class template, we're done.
77 assert(Spec->getSpecializedTemplate() && "No class template?");
78 if (Spec->getSpecializedTemplate()->isMemberSpecialization())
79 break;
Mike Stump11289f42009-09-09 15:08:12 +000080 }
Douglas Gregora654dd82009-08-28 17:37:35 +000081 // Add template arguments from a function template specialization.
John McCall970d5302009-08-29 03:16:09 +000082 else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Ctx)) {
Douglas Gregor8c702532010-02-05 07:33:43 +000083 if (!RelativeToPrimary &&
84 Function->getTemplateSpecializationKind()
85 == TSK_ExplicitSpecialization)
Douglas Gregorcf915552009-10-13 16:30:37 +000086 break;
87
Douglas Gregora654dd82009-08-28 17:37:35 +000088 if (const TemplateArgumentList *TemplateArgs
Douglas Gregorcf915552009-10-13 16:30:37 +000089 = Function->getTemplateSpecializationArgs()) {
90 // Add the template arguments for this specialization.
Douglas Gregora654dd82009-08-28 17:37:35 +000091 Result.addOuterTemplateArguments(TemplateArgs);
John McCall970d5302009-08-29 03:16:09 +000092
Douglas Gregorcf915552009-10-13 16:30:37 +000093 // If this function was instantiated from a specialized member that is
94 // a function template, we're done.
95 assert(Function->getPrimaryTemplate() && "No function template?");
96 if (Function->getPrimaryTemplate()->isMemberSpecialization())
97 break;
98 }
99
John McCall970d5302009-08-29 03:16:09 +0000100 // If this is a friend declaration and it declares an entity at
101 // namespace scope, take arguments from its lexical parent
Douglas Gregor1bd7a942010-05-03 23:29:10 +0000102 // instead of its semantic parent, unless of course the pattern we're
103 // instantiating actually comes from the file's context!
John McCall970d5302009-08-29 03:16:09 +0000104 if (Function->getFriendObjectKind() &&
Douglas Gregor1bd7a942010-05-03 23:29:10 +0000105 Function->getDeclContext()->isFileContext() &&
106 (!Pattern || !Pattern->getLexicalDeclContext()->isFileContext())) {
John McCall970d5302009-08-29 03:16:09 +0000107 Ctx = Function->getLexicalDeclContext();
Douglas Gregor8c702532010-02-05 07:33:43 +0000108 RelativeToPrimary = false;
John McCall970d5302009-08-29 03:16:09 +0000109 continue;
110 }
Douglas Gregor9961ce92010-07-08 18:37:38 +0000111 } else if (CXXRecordDecl *Rec = dyn_cast<CXXRecordDecl>(Ctx)) {
112 if (ClassTemplateDecl *ClassTemplate = Rec->getDescribedClassTemplate()) {
113 QualType T = ClassTemplate->getInjectedClassNameSpecialization();
114 const TemplateSpecializationType *TST
115 = cast<TemplateSpecializationType>(Context.getCanonicalType(T));
116 Result.addOuterTemplateArguments(TST->getArgs(), TST->getNumArgs());
117 if (ClassTemplate->isMemberSpecialization())
118 break;
119 }
Douglas Gregora654dd82009-08-28 17:37:35 +0000120 }
John McCall970d5302009-08-29 03:16:09 +0000121
122 Ctx = Ctx->getParent();
Douglas Gregor8c702532010-02-05 07:33:43 +0000123 RelativeToPrimary = false;
Douglas Gregorb4850462009-05-14 23:26:13 +0000124 }
Mike Stump11289f42009-09-09 15:08:12 +0000125
Douglas Gregora654dd82009-08-28 17:37:35 +0000126 return Result;
Douglas Gregorb4850462009-05-14 23:26:13 +0000127}
128
Douglas Gregor84d49a22009-11-11 21:54:23 +0000129bool Sema::ActiveTemplateInstantiation::isInstantiationRecord() const {
130 switch (Kind) {
131 case TemplateInstantiation:
132 case DefaultTemplateArgumentInstantiation:
133 case DefaultFunctionArgumentInstantiation:
134 return true;
135
136 case ExplicitTemplateArgumentSubstitution:
137 case DeducedTemplateArgumentSubstitution:
138 case PriorTemplateArgumentSubstitution:
139 case DefaultTemplateArgumentChecking:
140 return false;
141 }
142
143 return true;
144}
145
Douglas Gregorfcd5db32009-03-10 00:06:19 +0000146Sema::InstantiatingTemplate::
147InstantiatingTemplate(Sema &SemaRef, SourceLocation PointOfInstantiation,
Douglas Gregor85673582009-05-18 17:01:57 +0000148 Decl *Entity,
Douglas Gregorfcd5db32009-03-10 00:06:19 +0000149 SourceRange InstantiationRange)
150 : SemaRef(SemaRef) {
Douglas Gregor79cf6032009-03-10 20:44:00 +0000151
152 Invalid = CheckInstantiationDepth(PointOfInstantiation,
153 InstantiationRange);
154 if (!Invalid) {
Douglas Gregorfcd5db32009-03-10 00:06:19 +0000155 ActiveTemplateInstantiation Inst;
Douglas Gregor79cf6032009-03-10 20:44:00 +0000156 Inst.Kind = ActiveTemplateInstantiation::TemplateInstantiation;
Douglas Gregorfcd5db32009-03-10 00:06:19 +0000157 Inst.PointOfInstantiation = PointOfInstantiation;
Douglas Gregor79cf6032009-03-10 20:44:00 +0000158 Inst.Entity = reinterpret_cast<uintptr_t>(Entity);
Douglas Gregorc9220832009-03-12 18:36:18 +0000159 Inst.TemplateArgs = 0;
160 Inst.NumTemplateArgs = 0;
Douglas Gregor79cf6032009-03-10 20:44:00 +0000161 Inst.InstantiationRange = InstantiationRange;
162 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
Douglas Gregor79cf6032009-03-10 20:44:00 +0000163 }
164}
165
Mike Stump11289f42009-09-09 15:08:12 +0000166Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef,
Douglas Gregor79cf6032009-03-10 20:44:00 +0000167 SourceLocation PointOfInstantiation,
168 TemplateDecl *Template,
169 const TemplateArgument *TemplateArgs,
170 unsigned NumTemplateArgs,
171 SourceRange InstantiationRange)
172 : SemaRef(SemaRef) {
173
174 Invalid = CheckInstantiationDepth(PointOfInstantiation,
175 InstantiationRange);
176 if (!Invalid) {
177 ActiveTemplateInstantiation Inst;
Mike Stump11289f42009-09-09 15:08:12 +0000178 Inst.Kind
Douglas Gregor79cf6032009-03-10 20:44:00 +0000179 = ActiveTemplateInstantiation::DefaultTemplateArgumentInstantiation;
180 Inst.PointOfInstantiation = PointOfInstantiation;
181 Inst.Entity = reinterpret_cast<uintptr_t>(Template);
182 Inst.TemplateArgs = TemplateArgs;
183 Inst.NumTemplateArgs = NumTemplateArgs;
Douglas Gregorfcd5db32009-03-10 00:06:19 +0000184 Inst.InstantiationRange = InstantiationRange;
185 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
Douglas Gregorfcd5db32009-03-10 00:06:19 +0000186 }
187}
188
Mike Stump11289f42009-09-09 15:08:12 +0000189Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef,
Douglas Gregor637d9982009-06-10 23:47:09 +0000190 SourceLocation PointOfInstantiation,
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000191 FunctionTemplateDecl *FunctionTemplate,
192 const TemplateArgument *TemplateArgs,
193 unsigned NumTemplateArgs,
194 ActiveTemplateInstantiation::InstantiationKind Kind,
195 SourceRange InstantiationRange)
196: SemaRef(SemaRef) {
Mike Stump11289f42009-09-09 15:08:12 +0000197
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000198 Invalid = CheckInstantiationDepth(PointOfInstantiation,
199 InstantiationRange);
200 if (!Invalid) {
201 ActiveTemplateInstantiation Inst;
202 Inst.Kind = Kind;
203 Inst.PointOfInstantiation = PointOfInstantiation;
204 Inst.Entity = reinterpret_cast<uintptr_t>(FunctionTemplate);
205 Inst.TemplateArgs = TemplateArgs;
206 Inst.NumTemplateArgs = NumTemplateArgs;
207 Inst.InstantiationRange = InstantiationRange;
208 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
Douglas Gregor84d49a22009-11-11 21:54:23 +0000209
210 if (!Inst.isInstantiationRecord())
211 ++SemaRef.NonInstantiationEntries;
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000212 }
213}
214
Mike Stump11289f42009-09-09 15:08:12 +0000215Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef,
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000216 SourceLocation PointOfInstantiation,
Douglas Gregor637d9982009-06-10 23:47:09 +0000217 ClassTemplatePartialSpecializationDecl *PartialSpec,
218 const TemplateArgument *TemplateArgs,
219 unsigned NumTemplateArgs,
220 SourceRange InstantiationRange)
221 : SemaRef(SemaRef) {
222
Douglas Gregor84d49a22009-11-11 21:54:23 +0000223 Invalid = false;
224
225 ActiveTemplateInstantiation Inst;
226 Inst.Kind = ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution;
227 Inst.PointOfInstantiation = PointOfInstantiation;
228 Inst.Entity = reinterpret_cast<uintptr_t>(PartialSpec);
229 Inst.TemplateArgs = TemplateArgs;
230 Inst.NumTemplateArgs = NumTemplateArgs;
231 Inst.InstantiationRange = InstantiationRange;
232 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
233
234 assert(!Inst.isInstantiationRecord());
235 ++SemaRef.NonInstantiationEntries;
Douglas Gregor637d9982009-06-10 23:47:09 +0000236}
237
Mike Stump11289f42009-09-09 15:08:12 +0000238Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef,
Douglas Gregore62e6a02009-11-11 19:13:48 +0000239 SourceLocation PointOfInstantiation,
Anders Carlsson657bad42009-09-05 05:14:19 +0000240 ParmVarDecl *Param,
241 const TemplateArgument *TemplateArgs,
242 unsigned NumTemplateArgs,
243 SourceRange InstantiationRange)
244 : SemaRef(SemaRef) {
Mike Stump11289f42009-09-09 15:08:12 +0000245
Douglas Gregore62e6a02009-11-11 19:13:48 +0000246 Invalid = CheckInstantiationDepth(PointOfInstantiation, InstantiationRange);
Anders Carlsson657bad42009-09-05 05:14:19 +0000247
248 if (!Invalid) {
249 ActiveTemplateInstantiation Inst;
250 Inst.Kind
251 = ActiveTemplateInstantiation::DefaultFunctionArgumentInstantiation;
Douglas Gregore62e6a02009-11-11 19:13:48 +0000252 Inst.PointOfInstantiation = PointOfInstantiation;
Anders Carlsson657bad42009-09-05 05:14:19 +0000253 Inst.Entity = reinterpret_cast<uintptr_t>(Param);
254 Inst.TemplateArgs = TemplateArgs;
255 Inst.NumTemplateArgs = NumTemplateArgs;
256 Inst.InstantiationRange = InstantiationRange;
257 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
Douglas Gregore62e6a02009-11-11 19:13:48 +0000258 }
259}
260
261Sema::InstantiatingTemplate::
262InstantiatingTemplate(Sema &SemaRef, SourceLocation PointOfInstantiation,
263 TemplateDecl *Template,
264 NonTypeTemplateParmDecl *Param,
265 const TemplateArgument *TemplateArgs,
266 unsigned NumTemplateArgs,
267 SourceRange InstantiationRange) : SemaRef(SemaRef) {
Douglas Gregor84d49a22009-11-11 21:54:23 +0000268 Invalid = false;
Douglas Gregore62e6a02009-11-11 19:13:48 +0000269
Douglas Gregor84d49a22009-11-11 21:54:23 +0000270 ActiveTemplateInstantiation Inst;
271 Inst.Kind = ActiveTemplateInstantiation::PriorTemplateArgumentSubstitution;
272 Inst.PointOfInstantiation = PointOfInstantiation;
273 Inst.Template = Template;
274 Inst.Entity = reinterpret_cast<uintptr_t>(Param);
275 Inst.TemplateArgs = TemplateArgs;
276 Inst.NumTemplateArgs = NumTemplateArgs;
277 Inst.InstantiationRange = InstantiationRange;
278 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
279
280 assert(!Inst.isInstantiationRecord());
281 ++SemaRef.NonInstantiationEntries;
Douglas Gregore62e6a02009-11-11 19:13:48 +0000282}
283
284Sema::InstantiatingTemplate::
285InstantiatingTemplate(Sema &SemaRef, SourceLocation PointOfInstantiation,
286 TemplateDecl *Template,
287 TemplateTemplateParmDecl *Param,
288 const TemplateArgument *TemplateArgs,
289 unsigned NumTemplateArgs,
290 SourceRange InstantiationRange) : SemaRef(SemaRef) {
Douglas Gregor84d49a22009-11-11 21:54:23 +0000291 Invalid = false;
292 ActiveTemplateInstantiation Inst;
293 Inst.Kind = ActiveTemplateInstantiation::PriorTemplateArgumentSubstitution;
294 Inst.PointOfInstantiation = PointOfInstantiation;
295 Inst.Template = Template;
296 Inst.Entity = reinterpret_cast<uintptr_t>(Param);
297 Inst.TemplateArgs = TemplateArgs;
298 Inst.NumTemplateArgs = NumTemplateArgs;
299 Inst.InstantiationRange = InstantiationRange;
300 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
Douglas Gregore62e6a02009-11-11 19:13:48 +0000301
Douglas Gregor84d49a22009-11-11 21:54:23 +0000302 assert(!Inst.isInstantiationRecord());
303 ++SemaRef.NonInstantiationEntries;
304}
305
306Sema::InstantiatingTemplate::
307InstantiatingTemplate(Sema &SemaRef, SourceLocation PointOfInstantiation,
308 TemplateDecl *Template,
309 NamedDecl *Param,
310 const TemplateArgument *TemplateArgs,
311 unsigned NumTemplateArgs,
312 SourceRange InstantiationRange) : SemaRef(SemaRef) {
313 Invalid = false;
314
315 ActiveTemplateInstantiation Inst;
316 Inst.Kind = ActiveTemplateInstantiation::DefaultTemplateArgumentChecking;
317 Inst.PointOfInstantiation = PointOfInstantiation;
318 Inst.Template = Template;
319 Inst.Entity = reinterpret_cast<uintptr_t>(Param);
320 Inst.TemplateArgs = TemplateArgs;
321 Inst.NumTemplateArgs = NumTemplateArgs;
322 Inst.InstantiationRange = InstantiationRange;
323 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
324
325 assert(!Inst.isInstantiationRecord());
326 ++SemaRef.NonInstantiationEntries;
Anders Carlsson657bad42009-09-05 05:14:19 +0000327}
328
Douglas Gregor85673582009-05-18 17:01:57 +0000329void Sema::InstantiatingTemplate::Clear() {
330 if (!Invalid) {
Douglas Gregor84d49a22009-11-11 21:54:23 +0000331 if (!SemaRef.ActiveTemplateInstantiations.back().isInstantiationRecord()) {
332 assert(SemaRef.NonInstantiationEntries > 0);
333 --SemaRef.NonInstantiationEntries;
334 }
335
Douglas Gregorfcd5db32009-03-10 00:06:19 +0000336 SemaRef.ActiveTemplateInstantiations.pop_back();
Douglas Gregor85673582009-05-18 17:01:57 +0000337 Invalid = true;
338 }
Douglas Gregorfcd5db32009-03-10 00:06:19 +0000339}
340
Douglas Gregor79cf6032009-03-10 20:44:00 +0000341bool Sema::InstantiatingTemplate::CheckInstantiationDepth(
342 SourceLocation PointOfInstantiation,
343 SourceRange InstantiationRange) {
Douglas Gregor84d49a22009-11-11 21:54:23 +0000344 assert(SemaRef.NonInstantiationEntries <=
345 SemaRef.ActiveTemplateInstantiations.size());
346 if ((SemaRef.ActiveTemplateInstantiations.size() -
347 SemaRef.NonInstantiationEntries)
348 <= SemaRef.getLangOptions().InstantiationDepth)
Douglas Gregor79cf6032009-03-10 20:44:00 +0000349 return false;
350
Mike Stump11289f42009-09-09 15:08:12 +0000351 SemaRef.Diag(PointOfInstantiation,
Douglas Gregor79cf6032009-03-10 20:44:00 +0000352 diag::err_template_recursion_depth_exceeded)
353 << SemaRef.getLangOptions().InstantiationDepth
354 << InstantiationRange;
355 SemaRef.Diag(PointOfInstantiation, diag::note_template_recursion_depth)
356 << SemaRef.getLangOptions().InstantiationDepth;
357 return true;
358}
359
Douglas Gregor4ea568f2009-03-10 18:03:33 +0000360/// \brief Prints the current instantiation stack through a series of
361/// notes.
362void Sema::PrintInstantiationStack() {
Douglas Gregorffed1cb2010-04-20 07:18:24 +0000363 // Determine which template instantiations to skip, if any.
364 unsigned SkipStart = ActiveTemplateInstantiations.size(), SkipEnd = SkipStart;
365 unsigned Limit = Diags.getTemplateBacktraceLimit();
366 if (Limit && Limit < ActiveTemplateInstantiations.size()) {
367 SkipStart = Limit / 2 + Limit % 2;
368 SkipEnd = ActiveTemplateInstantiations.size() - Limit / 2;
369 }
370
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000371 // FIXME: In all of these cases, we need to show the template arguments
Douglas Gregorffed1cb2010-04-20 07:18:24 +0000372 unsigned InstantiationIdx = 0;
Douglas Gregor4ea568f2009-03-10 18:03:33 +0000373 for (llvm::SmallVector<ActiveTemplateInstantiation, 16>::reverse_iterator
374 Active = ActiveTemplateInstantiations.rbegin(),
375 ActiveEnd = ActiveTemplateInstantiations.rend();
376 Active != ActiveEnd;
Douglas Gregorffed1cb2010-04-20 07:18:24 +0000377 ++Active, ++InstantiationIdx) {
378 // Skip this instantiation?
379 if (InstantiationIdx >= SkipStart && InstantiationIdx < SkipEnd) {
380 if (InstantiationIdx == SkipStart) {
381 // Note that we're skipping instantiations.
382 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
383 diag::note_instantiation_contexts_suppressed)
384 << unsigned(ActiveTemplateInstantiations.size() - Limit);
385 }
386 continue;
387 }
388
Douglas Gregor79cf6032009-03-10 20:44:00 +0000389 switch (Active->Kind) {
390 case ActiveTemplateInstantiation::TemplateInstantiation: {
Douglas Gregor85673582009-05-18 17:01:57 +0000391 Decl *D = reinterpret_cast<Decl *>(Active->Entity);
392 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) {
393 unsigned DiagID = diag::note_template_member_class_here;
394 if (isa<ClassTemplateSpecializationDecl>(Record))
395 DiagID = diag::note_template_class_instantiation_here;
Mike Stump11289f42009-09-09 15:08:12 +0000396 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
Douglas Gregor85673582009-05-18 17:01:57 +0000397 DiagID)
398 << Context.getTypeDeclType(Record)
399 << Active->InstantiationRange;
Douglas Gregora6ef8f02009-07-24 20:34:43 +0000400 } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
Douglas Gregor4adbc6d2009-06-26 00:10:03 +0000401 unsigned DiagID;
402 if (Function->getPrimaryTemplate())
403 DiagID = diag::note_function_template_spec_here;
404 else
405 DiagID = diag::note_template_member_function_here;
Mike Stump11289f42009-09-09 15:08:12 +0000406 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
Douglas Gregor85673582009-05-18 17:01:57 +0000407 DiagID)
408 << Function
409 << Active->InstantiationRange;
Douglas Gregora6ef8f02009-07-24 20:34:43 +0000410 } else {
411 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
412 diag::note_template_static_data_member_def_here)
413 << cast<VarDecl>(D)
414 << Active->InstantiationRange;
Douglas Gregor85673582009-05-18 17:01:57 +0000415 }
Douglas Gregor79cf6032009-03-10 20:44:00 +0000416 break;
417 }
418
419 case ActiveTemplateInstantiation::DefaultTemplateArgumentInstantiation: {
420 TemplateDecl *Template = cast<TemplateDecl>((Decl *)Active->Entity);
421 std::string TemplateArgsStr
Douglas Gregordc572a32009-03-30 22:58:21 +0000422 = TemplateSpecializationType::PrintTemplateArgumentList(
Mike Stump11289f42009-09-09 15:08:12 +0000423 Active->TemplateArgs,
Douglas Gregor7de59662009-05-29 20:38:28 +0000424 Active->NumTemplateArgs,
425 Context.PrintingPolicy);
Douglas Gregor79cf6032009-03-10 20:44:00 +0000426 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
427 diag::note_default_arg_instantiation_here)
428 << (Template->getNameAsString() + TemplateArgsStr)
429 << Active->InstantiationRange;
430 break;
431 }
Douglas Gregor637d9982009-06-10 23:47:09 +0000432
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000433 case ActiveTemplateInstantiation::ExplicitTemplateArgumentSubstitution: {
Mike Stump11289f42009-09-09 15:08:12 +0000434 FunctionTemplateDecl *FnTmpl
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000435 = cast<FunctionTemplateDecl>((Decl *)Active->Entity);
Douglas Gregor637d9982009-06-10 23:47:09 +0000436 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000437 diag::note_explicit_template_arg_substitution_here)
Douglas Gregor607f1412010-03-30 20:35:20 +0000438 << FnTmpl
439 << getTemplateArgumentBindingsText(FnTmpl->getTemplateParameters(),
440 Active->TemplateArgs,
441 Active->NumTemplateArgs)
442 << Active->InstantiationRange;
Douglas Gregor637d9982009-06-10 23:47:09 +0000443 break;
444 }
Mike Stump11289f42009-09-09 15:08:12 +0000445
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000446 case ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution:
447 if (ClassTemplatePartialSpecializationDecl *PartialSpec
448 = dyn_cast<ClassTemplatePartialSpecializationDecl>(
449 (Decl *)Active->Entity)) {
450 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
451 diag::note_partial_spec_deduct_instantiation_here)
452 << Context.getTypeDeclType(PartialSpec)
Douglas Gregor607f1412010-03-30 20:35:20 +0000453 << getTemplateArgumentBindingsText(
454 PartialSpec->getTemplateParameters(),
455 Active->TemplateArgs,
456 Active->NumTemplateArgs)
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000457 << Active->InstantiationRange;
458 } else {
459 FunctionTemplateDecl *FnTmpl
460 = cast<FunctionTemplateDecl>((Decl *)Active->Entity);
461 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
462 diag::note_function_template_deduction_instantiation_here)
Douglas Gregor607f1412010-03-30 20:35:20 +0000463 << FnTmpl
464 << getTemplateArgumentBindingsText(FnTmpl->getTemplateParameters(),
465 Active->TemplateArgs,
466 Active->NumTemplateArgs)
467 << Active->InstantiationRange;
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000468 }
469 break;
Douglas Gregor637d9982009-06-10 23:47:09 +0000470
Anders Carlsson657bad42009-09-05 05:14:19 +0000471 case ActiveTemplateInstantiation::DefaultFunctionArgumentInstantiation: {
472 ParmVarDecl *Param = cast<ParmVarDecl>((Decl *)Active->Entity);
473 FunctionDecl *FD = cast<FunctionDecl>(Param->getDeclContext());
Mike Stump11289f42009-09-09 15:08:12 +0000474
Anders Carlsson657bad42009-09-05 05:14:19 +0000475 std::string TemplateArgsStr
476 = TemplateSpecializationType::PrintTemplateArgumentList(
Mike Stump11289f42009-09-09 15:08:12 +0000477 Active->TemplateArgs,
Anders Carlsson657bad42009-09-05 05:14:19 +0000478 Active->NumTemplateArgs,
479 Context.PrintingPolicy);
480 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
481 diag::note_default_function_arg_instantiation_here)
Anders Carlssondc6d2c32009-09-05 05:38:54 +0000482 << (FD->getNameAsString() + TemplateArgsStr)
Anders Carlsson657bad42009-09-05 05:14:19 +0000483 << Active->InstantiationRange;
484 break;
485 }
Mike Stump11289f42009-09-09 15:08:12 +0000486
Douglas Gregore62e6a02009-11-11 19:13:48 +0000487 case ActiveTemplateInstantiation::PriorTemplateArgumentSubstitution: {
488 NamedDecl *Parm = cast<NamedDecl>((Decl *)Active->Entity);
489 std::string Name;
490 if (!Parm->getName().empty())
491 Name = std::string(" '") + Parm->getName().str() + "'";
492
493 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
494 diag::note_prior_template_arg_substitution)
495 << isa<TemplateTemplateParmDecl>(Parm)
496 << Name
497 << getTemplateArgumentBindingsText(
498 Active->Template->getTemplateParameters(),
499 Active->TemplateArgs,
500 Active->NumTemplateArgs)
501 << Active->InstantiationRange;
502 break;
503 }
Douglas Gregor84d49a22009-11-11 21:54:23 +0000504
505 case ActiveTemplateInstantiation::DefaultTemplateArgumentChecking: {
506 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
507 diag::note_template_default_arg_checking)
508 << getTemplateArgumentBindingsText(
509 Active->Template->getTemplateParameters(),
510 Active->TemplateArgs,
511 Active->NumTemplateArgs)
512 << Active->InstantiationRange;
513 break;
514 }
Douglas Gregor79cf6032009-03-10 20:44:00 +0000515 }
Douglas Gregor4ea568f2009-03-10 18:03:33 +0000516 }
517}
518
Douglas Gregor33834512009-06-14 07:33:30 +0000519bool Sema::isSFINAEContext() const {
520 using llvm::SmallVector;
521 for (SmallVector<ActiveTemplateInstantiation, 16>::const_reverse_iterator
522 Active = ActiveTemplateInstantiations.rbegin(),
523 ActiveEnd = ActiveTemplateInstantiations.rend();
524 Active != ActiveEnd;
Douglas Gregor84d49a22009-11-11 21:54:23 +0000525 ++Active)
526 {
Douglas Gregor33834512009-06-14 07:33:30 +0000527 switch(Active->Kind) {
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000528 case ActiveTemplateInstantiation::TemplateInstantiation:
Anders Carlsson657bad42009-09-05 05:14:19 +0000529 case ActiveTemplateInstantiation::DefaultFunctionArgumentInstantiation:
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000530 // This is a template instantiation, so there is no SFINAE.
531 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000532
Douglas Gregor33834512009-06-14 07:33:30 +0000533 case ActiveTemplateInstantiation::DefaultTemplateArgumentInstantiation:
Douglas Gregore62e6a02009-11-11 19:13:48 +0000534 case ActiveTemplateInstantiation::PriorTemplateArgumentSubstitution:
Douglas Gregor84d49a22009-11-11 21:54:23 +0000535 case ActiveTemplateInstantiation::DefaultTemplateArgumentChecking:
Douglas Gregore62e6a02009-11-11 19:13:48 +0000536 // A default template argument instantiation and substitution into
537 // template parameters with arguments for prior parameters may or may
538 // not be a SFINAE context; look further up the stack.
Douglas Gregor33834512009-06-14 07:33:30 +0000539 break;
Mike Stump11289f42009-09-09 15:08:12 +0000540
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000541 case ActiveTemplateInstantiation::ExplicitTemplateArgumentSubstitution:
542 case ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution:
543 // We're either substitution explicitly-specified template arguments
544 // or deduced template arguments, so SFINAE applies.
545 return true;
Douglas Gregor33834512009-06-14 07:33:30 +0000546 }
547 }
548
549 return false;
550}
551
Douglas Gregorfe1e1102009-02-27 19:31:52 +0000552//===----------------------------------------------------------------------===/
553// Template Instantiation for Types
554//===----------------------------------------------------------------------===/
Douglas Gregor17c0d7b2009-02-28 00:25:32 +0000555namespace {
Douglas Gregor14cf7522010-04-30 18:55:50 +0000556 class TemplateInstantiator : public TreeTransform<TemplateInstantiator> {
Douglas Gregor01afeef2009-08-28 20:31:08 +0000557 const MultiLevelTemplateArgumentList &TemplateArgs;
Douglas Gregor17c0d7b2009-02-28 00:25:32 +0000558 SourceLocation Loc;
559 DeclarationName Entity;
Douglas Gregorfe1e1102009-02-27 19:31:52 +0000560
Douglas Gregor17c0d7b2009-02-28 00:25:32 +0000561 public:
Douglas Gregorebe10102009-08-20 07:17:43 +0000562 typedef TreeTransform<TemplateInstantiator> inherited;
Mike Stump11289f42009-09-09 15:08:12 +0000563
564 TemplateInstantiator(Sema &SemaRef,
Douglas Gregor01afeef2009-08-28 20:31:08 +0000565 const MultiLevelTemplateArgumentList &TemplateArgs,
Douglas Gregord6ff3322009-08-04 16:50:30 +0000566 SourceLocation Loc,
Mike Stump11289f42009-09-09 15:08:12 +0000567 DeclarationName Entity)
568 : inherited(SemaRef), TemplateArgs(TemplateArgs), Loc(Loc),
Douglas Gregorebe10102009-08-20 07:17:43 +0000569 Entity(Entity) { }
Douglas Gregor17c0d7b2009-02-28 00:25:32 +0000570
Mike Stump11289f42009-09-09 15:08:12 +0000571 /// \brief Determine whether the given type \p T has already been
Douglas Gregord6ff3322009-08-04 16:50:30 +0000572 /// transformed.
573 ///
574 /// For the purposes of template instantiation, a type has already been
575 /// transformed if it is NULL or if it is not dependent.
Douglas Gregor5597ab42010-05-07 23:12:07 +0000576 bool AlreadyTransformed(QualType T);
Mike Stump11289f42009-09-09 15:08:12 +0000577
Douglas Gregord6ff3322009-08-04 16:50:30 +0000578 /// \brief Returns the location of the entity being instantiated, if known.
579 SourceLocation getBaseLocation() { return Loc; }
Mike Stump11289f42009-09-09 15:08:12 +0000580
Douglas Gregord6ff3322009-08-04 16:50:30 +0000581 /// \brief Returns the name of the entity being instantiated, if any.
582 DeclarationName getBaseEntity() { return Entity; }
Mike Stump11289f42009-09-09 15:08:12 +0000583
Douglas Gregoref6ab412009-10-27 06:26:26 +0000584 /// \brief Sets the "base" location and entity when that
585 /// information is known based on another transformation.
586 void setBase(SourceLocation Loc, DeclarationName Entity) {
587 this->Loc = Loc;
588 this->Entity = Entity;
589 }
590
Douglas Gregord6ff3322009-08-04 16:50:30 +0000591 /// \brief Transform the given declaration by instantiating a reference to
592 /// this declaration.
Douglas Gregora04f2ca2010-03-01 15:56:25 +0000593 Decl *TransformDecl(SourceLocation Loc, Decl *D);
Douglas Gregora16548e2009-08-11 05:31:07 +0000594
Mike Stump11289f42009-09-09 15:08:12 +0000595 /// \brief Transform the definition of the given declaration by
Douglas Gregorebe10102009-08-20 07:17:43 +0000596 /// instantiating it.
Douglas Gregor25289362010-03-01 17:25:41 +0000597 Decl *TransformDefinition(SourceLocation Loc, Decl *D);
Mike Stump11289f42009-09-09 15:08:12 +0000598
Douglas Gregora5cb6da2009-10-20 05:58:46 +0000599 /// \bried Transform the first qualifier within a scope by instantiating the
600 /// declaration.
601 NamedDecl *TransformFirstQualifierInScope(NamedDecl *D, SourceLocation Loc);
602
Douglas Gregorebe10102009-08-20 07:17:43 +0000603 /// \brief Rebuild the exception declaration and register the declaration
604 /// as an instantiated local.
Douglas Gregor9f0e1aa2010-09-09 17:09:21 +0000605 VarDecl *RebuildExceptionDecl(VarDecl *ExceptionDecl,
John McCallbcd03502009-12-07 02:54:59 +0000606 TypeSourceInfo *Declarator,
Douglas Gregorebe10102009-08-20 07:17:43 +0000607 IdentifierInfo *Name,
Douglas Gregor9f0e1aa2010-09-09 17:09:21 +0000608 SourceLocation Loc);
Mike Stump11289f42009-09-09 15:08:12 +0000609
Douglas Gregorf4e837f2010-04-26 17:57:08 +0000610 /// \brief Rebuild the Objective-C exception declaration and register the
611 /// declaration as an instantiated local.
612 VarDecl *RebuildObjCExceptionDecl(VarDecl *ExceptionDecl,
613 TypeSourceInfo *TSInfo, QualType T);
614
John McCall7f41d982009-09-11 04:59:25 +0000615 /// \brief Check for tag mismatches when instantiating an
616 /// elaborated type.
Abramo Bagnara6150c882010-05-11 21:36:43 +0000617 QualType RebuildElaboratedType(ElaboratedTypeKeyword Keyword,
618 NestedNameSpecifier *NNS, QualType T);
John McCall7f41d982009-09-11 04:59:25 +0000619
John McCalldadc5752010-08-24 06:29:42 +0000620 ExprResult TransformPredefinedExpr(PredefinedExpr *E);
621 ExprResult TransformDeclRefExpr(DeclRefExpr *E);
622 ExprResult TransformCXXDefaultArgExpr(CXXDefaultArgExpr *E);
623 ExprResult TransformTemplateParmRefExpr(DeclRefExpr *E,
John McCall13481c52010-02-06 08:42:39 +0000624 NonTypeTemplateParmDecl *D);
Sebastian Redl14236c82009-11-08 13:56:19 +0000625
Douglas Gregor14cf7522010-04-30 18:55:50 +0000626 QualType TransformFunctionProtoType(TypeLocBuilder &TLB,
627 FunctionProtoTypeLoc TL,
628 QualType ObjectType);
John McCall58f10c32010-03-11 09:03:00 +0000629 ParmVarDecl *TransformFunctionTypeParam(ParmVarDecl *OldParm);
630
Mike Stump11289f42009-09-09 15:08:12 +0000631 /// \brief Transforms a template type parameter type by performing
Douglas Gregord6ff3322009-08-04 16:50:30 +0000632 /// substitution of the corresponding template type argument.
John McCall550e0c22009-10-21 00:40:46 +0000633 QualType TransformTemplateTypeParmType(TypeLocBuilder &TLB,
Douglas Gregorfe17d252010-02-16 19:09:40 +0000634 TemplateTypeParmTypeLoc TL,
635 QualType ObjectType);
Nick Lewyckyc96c37f2010-07-06 19:51:49 +0000636
John McCalldadc5752010-08-24 06:29:42 +0000637 ExprResult TransformCallExpr(CallExpr *CE) {
Nick Lewyckyc96c37f2010-07-06 19:51:49 +0000638 getSema().CallsUndergoingInstantiation.push_back(CE);
John McCalldadc5752010-08-24 06:29:42 +0000639 ExprResult Result =
Nick Lewyckyc96c37f2010-07-06 19:51:49 +0000640 TreeTransform<TemplateInstantiator>::TransformCallExpr(CE);
641 getSema().CallsUndergoingInstantiation.pop_back();
642 return move(Result);
643 }
Douglas Gregord6ff3322009-08-04 16:50:30 +0000644 };
Douglas Gregor04318252009-07-06 15:59:29 +0000645}
646
Douglas Gregor5597ab42010-05-07 23:12:07 +0000647bool TemplateInstantiator::AlreadyTransformed(QualType T) {
648 if (T.isNull())
649 return true;
650
Douglas Gregor5a5073e2010-05-24 17:22:01 +0000651 if (T->isDependentType() || T->isVariablyModifiedType())
Douglas Gregor5597ab42010-05-07 23:12:07 +0000652 return false;
653
654 getSema().MarkDeclarationsReferencedInType(Loc, T);
655 return true;
656}
657
Douglas Gregora04f2ca2010-03-01 15:56:25 +0000658Decl *TemplateInstantiator::TransformDecl(SourceLocation Loc, Decl *D) {
Douglas Gregor2b6ca462009-09-03 21:38:09 +0000659 if (!D)
660 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000661
Douglas Gregor2b6ca462009-09-03 21:38:09 +0000662 if (TemplateTemplateParmDecl *TTP = dyn_cast<TemplateTemplateParmDecl>(D)) {
Douglas Gregor01afeef2009-08-28 20:31:08 +0000663 if (TTP->getDepth() < TemplateArgs.getNumLevels()) {
Douglas Gregorb93971082010-02-05 19:54:12 +0000664 // If the corresponding template argument is NULL or non-existent, it's
665 // because we are performing instantiation from explicitly-specified
666 // template arguments in a function template, but there were some
667 // arguments left unspecified.
668 if (!TemplateArgs.hasTemplateArgument(TTP->getDepth(),
669 TTP->getPosition()))
670 return D;
671
Douglas Gregor9167f8b2009-11-11 01:00:40 +0000672 TemplateName Template
673 = TemplateArgs(TTP->getDepth(), TTP->getPosition()).getAsTemplate();
674 assert(!Template.isNull() && Template.getAsTemplateDecl() &&
Douglas Gregor01afeef2009-08-28 20:31:08 +0000675 "Wrong kind of template template argument");
Douglas Gregor9167f8b2009-11-11 01:00:40 +0000676 return Template.getAsTemplateDecl();
Douglas Gregor01afeef2009-08-28 20:31:08 +0000677 }
Mike Stump11289f42009-09-09 15:08:12 +0000678
Douglas Gregor9167f8b2009-11-11 01:00:40 +0000679 // Fall through to find the instantiated declaration for this template
680 // template parameter.
Douglas Gregor71dc5092009-08-06 06:41:21 +0000681 }
Mike Stump11289f42009-09-09 15:08:12 +0000682
Douglas Gregora04f2ca2010-03-01 15:56:25 +0000683 return SemaRef.FindInstantiatedDecl(Loc, cast<NamedDecl>(D), TemplateArgs);
Douglas Gregord6ff3322009-08-04 16:50:30 +0000684}
685
Douglas Gregor25289362010-03-01 17:25:41 +0000686Decl *TemplateInstantiator::TransformDefinition(SourceLocation Loc, Decl *D) {
John McCall76d824f2009-08-25 22:02:44 +0000687 Decl *Inst = getSema().SubstDecl(D, getSema().CurContext, TemplateArgs);
Douglas Gregorebe10102009-08-20 07:17:43 +0000688 if (!Inst)
689 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000690
Douglas Gregorebe10102009-08-20 07:17:43 +0000691 getSema().CurrentInstantiationScope->InstantiatedLocal(D, Inst);
692 return Inst;
693}
694
Douglas Gregora5cb6da2009-10-20 05:58:46 +0000695NamedDecl *
696TemplateInstantiator::TransformFirstQualifierInScope(NamedDecl *D,
697 SourceLocation Loc) {
698 // If the first part of the nested-name-specifier was a template type
699 // parameter, instantiate that type parameter down to a tag type.
700 if (TemplateTypeParmDecl *TTPD = dyn_cast_or_null<TemplateTypeParmDecl>(D)) {
701 const TemplateTypeParmType *TTP
702 = cast<TemplateTypeParmType>(getSema().Context.getTypeDeclType(TTPD));
703 if (TTP->getDepth() < TemplateArgs.getNumLevels()) {
704 QualType T = TemplateArgs(TTP->getDepth(), TTP->getIndex()).getAsType();
705 if (T.isNull())
Douglas Gregora04f2ca2010-03-01 15:56:25 +0000706 return cast_or_null<NamedDecl>(TransformDecl(Loc, D));
Douglas Gregora5cb6da2009-10-20 05:58:46 +0000707
708 if (const TagType *Tag = T->getAs<TagType>())
709 return Tag->getDecl();
710
711 // The resulting type is not a tag; complain.
712 getSema().Diag(Loc, diag::err_nested_name_spec_non_tag) << T;
713 return 0;
714 }
715 }
716
Douglas Gregora04f2ca2010-03-01 15:56:25 +0000717 return cast_or_null<NamedDecl>(TransformDecl(Loc, D));
Douglas Gregora5cb6da2009-10-20 05:58:46 +0000718}
719
Douglas Gregorebe10102009-08-20 07:17:43 +0000720VarDecl *
721TemplateInstantiator::RebuildExceptionDecl(VarDecl *ExceptionDecl,
John McCallbcd03502009-12-07 02:54:59 +0000722 TypeSourceInfo *Declarator,
Douglas Gregorebe10102009-08-20 07:17:43 +0000723 IdentifierInfo *Name,
Douglas Gregor9f0e1aa2010-09-09 17:09:21 +0000724 SourceLocation Loc) {
725 VarDecl *Var = inherited::RebuildExceptionDecl(ExceptionDecl, Declarator,
726 Name, Loc);
Douglas Gregorf4e837f2010-04-26 17:57:08 +0000727 if (Var)
728 getSema().CurrentInstantiationScope->InstantiatedLocal(ExceptionDecl, Var);
729 return Var;
730}
731
732VarDecl *TemplateInstantiator::RebuildObjCExceptionDecl(VarDecl *ExceptionDecl,
733 TypeSourceInfo *TSInfo,
734 QualType T) {
735 VarDecl *Var = inherited::RebuildObjCExceptionDecl(ExceptionDecl, TSInfo, T);
736 if (Var)
Douglas Gregorebe10102009-08-20 07:17:43 +0000737 getSema().CurrentInstantiationScope->InstantiatedLocal(ExceptionDecl, Var);
738 return Var;
739}
740
John McCall7f41d982009-09-11 04:59:25 +0000741QualType
Abramo Bagnara6150c882010-05-11 21:36:43 +0000742TemplateInstantiator::RebuildElaboratedType(ElaboratedTypeKeyword Keyword,
743 NestedNameSpecifier *NNS,
744 QualType T) {
John McCall7f41d982009-09-11 04:59:25 +0000745 if (const TagType *TT = T->getAs<TagType>()) {
746 TagDecl* TD = TT->getDecl();
747
748 // FIXME: this location is very wrong; we really need typelocs.
749 SourceLocation TagLocation = TD->getTagKeywordLoc();
750
751 // FIXME: type might be anonymous.
752 IdentifierInfo *Id = TD->getIdentifier();
753
754 // TODO: should we even warn on struct/class mismatches for this? Seems
755 // like it's likely to produce a lot of spurious errors.
Abramo Bagnara6150c882010-05-11 21:36:43 +0000756 if (Keyword != ETK_None && Keyword != ETK_Typename) {
757 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForKeyword(Keyword);
758 if (!SemaRef.isAcceptableTagRedeclaration(TD, Kind, TagLocation, *Id)) {
759 SemaRef.Diag(TagLocation, diag::err_use_with_wrong_tag)
760 << Id
761 << FixItHint::CreateReplacement(SourceRange(TagLocation),
762 TD->getKindName());
763 SemaRef.Diag(TD->getLocation(), diag::note_previous_use);
764 }
John McCall7f41d982009-09-11 04:59:25 +0000765 }
766 }
767
Abramo Bagnara6150c882010-05-11 21:36:43 +0000768 return TreeTransform<TemplateInstantiator>::RebuildElaboratedType(Keyword,
769 NNS, T);
John McCall7f41d982009-09-11 04:59:25 +0000770}
771
John McCalldadc5752010-08-24 06:29:42 +0000772ExprResult
John McCall47f29ea2009-12-08 09:21:05 +0000773TemplateInstantiator::TransformPredefinedExpr(PredefinedExpr *E) {
Anders Carlsson0b209a82009-09-11 01:22:35 +0000774 if (!E->isTypeDependent())
775 return SemaRef.Owned(E->Retain());
776
777 FunctionDecl *currentDecl = getSema().getCurFunctionDecl();
778 assert(currentDecl && "Must have current function declaration when "
779 "instantiating.");
780
781 PredefinedExpr::IdentType IT = E->getIdentType();
782
Anders Carlsson5bd8d192010-02-11 18:20:28 +0000783 unsigned Length = PredefinedExpr::ComputeName(IT, currentDecl).length();
Anders Carlsson0b209a82009-09-11 01:22:35 +0000784
785 llvm::APInt LengthI(32, Length + 1);
John McCall8ccfcb52009-09-24 19:53:00 +0000786 QualType ResTy = getSema().Context.CharTy.withConst();
Anders Carlsson0b209a82009-09-11 01:22:35 +0000787 ResTy = getSema().Context.getConstantArrayType(ResTy, LengthI,
788 ArrayType::Normal, 0);
789 PredefinedExpr *PE =
790 new (getSema().Context) PredefinedExpr(E->getLocation(), ResTy, IT);
791 return getSema().Owned(PE);
792}
793
John McCalldadc5752010-08-24 06:29:42 +0000794ExprResult
John McCall13481c52010-02-06 08:42:39 +0000795TemplateInstantiator::TransformTemplateParmRefExpr(DeclRefExpr *E,
Douglas Gregor6c379e22010-02-08 23:41:45 +0000796 NonTypeTemplateParmDecl *NTTP) {
John McCall13481c52010-02-06 08:42:39 +0000797 // If the corresponding template argument is NULL or non-existent, it's
798 // because we are performing instantiation from explicitly-specified
799 // template arguments in a function template, but there were some
800 // arguments left unspecified.
801 if (!TemplateArgs.hasTemplateArgument(NTTP->getDepth(),
802 NTTP->getPosition()))
803 return SemaRef.Owned(E->Retain());
Mike Stump11289f42009-09-09 15:08:12 +0000804
John McCall13481c52010-02-06 08:42:39 +0000805 const TemplateArgument &Arg = TemplateArgs(NTTP->getDepth(),
806 NTTP->getPosition());
Mike Stump11289f42009-09-09 15:08:12 +0000807
John McCall13481c52010-02-06 08:42:39 +0000808 // The template argument itself might be an expression, in which
809 // case we just return that expression.
810 if (Arg.getKind() == TemplateArgument::Expression)
811 return SemaRef.Owned(Arg.getAsExpr()->Retain());
Mike Stump11289f42009-09-09 15:08:12 +0000812
John McCall13481c52010-02-06 08:42:39 +0000813 if (Arg.getKind() == TemplateArgument::Declaration) {
814 ValueDecl *VD = cast<ValueDecl>(Arg.getAsDecl());
Mike Stump11289f42009-09-09 15:08:12 +0000815
John McCall15dda372010-02-06 10:23:53 +0000816 // Find the instantiation of the template argument. This is
817 // required for nested templates.
John McCall13481c52010-02-06 08:42:39 +0000818 VD = cast_or_null<ValueDecl>(
Douglas Gregora04f2ca2010-03-01 15:56:25 +0000819 getSema().FindInstantiatedDecl(E->getLocation(),
820 VD, TemplateArgs));
John McCall13481c52010-02-06 08:42:39 +0000821 if (!VD)
John McCallfaf5fb42010-08-26 23:41:50 +0000822 return ExprError();
Mike Stump11289f42009-09-09 15:08:12 +0000823
John McCall15dda372010-02-06 10:23:53 +0000824 // Derive the type we want the substituted decl to have. This had
825 // better be non-dependent, or these checks will have serious problems.
826 QualType TargetType = SemaRef.SubstType(NTTP->getType(), TemplateArgs,
Douglas Gregor6c379e22010-02-08 23:41:45 +0000827 E->getLocation(),
828 DeclarationName());
John McCall15dda372010-02-06 10:23:53 +0000829 assert(!TargetType.isNull() && "type substitution failed for param type");
830 assert(!TargetType->isDependentType() && "param type still dependent");
Douglas Gregord5cb1dd2010-03-28 02:42:43 +0000831 return SemaRef.BuildExpressionFromDeclTemplateArgument(Arg,
832 TargetType,
833 E->getLocation());
John McCall13481c52010-02-06 08:42:39 +0000834 }
835
Douglas Gregord5cb1dd2010-03-28 02:42:43 +0000836 return SemaRef.BuildExpressionFromIntegralTemplateArgument(Arg,
837 E->getSourceRange().getBegin());
John McCall13481c52010-02-06 08:42:39 +0000838}
839
840
John McCalldadc5752010-08-24 06:29:42 +0000841ExprResult
John McCall13481c52010-02-06 08:42:39 +0000842TemplateInstantiator::TransformDeclRefExpr(DeclRefExpr *E) {
843 NamedDecl *D = E->getDecl();
844 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(D)) {
845 if (NTTP->getDepth() < TemplateArgs.getNumLevels())
846 return TransformTemplateParmRefExpr(E, NTTP);
Douglas Gregor954de172009-10-31 17:21:17 +0000847
848 // We have a non-type template parameter that isn't fully substituted;
849 // FindInstantiatedDecl will find it in the local instantiation scope.
Douglas Gregora16548e2009-08-11 05:31:07 +0000850 }
Mike Stump11289f42009-09-09 15:08:12 +0000851
John McCall47f29ea2009-12-08 09:21:05 +0000852 return TreeTransform<TemplateInstantiator>::TransformDeclRefExpr(E);
Douglas Gregora16548e2009-08-11 05:31:07 +0000853}
854
John McCalldadc5752010-08-24 06:29:42 +0000855ExprResult TemplateInstantiator::TransformCXXDefaultArgExpr(
John McCall47f29ea2009-12-08 09:21:05 +0000856 CXXDefaultArgExpr *E) {
Sebastian Redl14236c82009-11-08 13:56:19 +0000857 assert(!cast<FunctionDecl>(E->getParam()->getDeclContext())->
858 getDescribedFunctionTemplate() &&
859 "Default arg expressions are never formed in dependent cases.");
Douglas Gregor033f6752009-12-23 23:03:06 +0000860 return SemaRef.BuildCXXDefaultArgExpr(E->getUsedLocation(),
861 cast<FunctionDecl>(E->getParam()->getDeclContext()),
862 E->getParam());
Sebastian Redl14236c82009-11-08 13:56:19 +0000863}
864
Douglas Gregor14cf7522010-04-30 18:55:50 +0000865QualType TemplateInstantiator::TransformFunctionProtoType(TypeLocBuilder &TLB,
866 FunctionProtoTypeLoc TL,
867 QualType ObjectType) {
868 // We need a local instantiation scope for this function prototype.
John McCall19c1bfd2010-08-25 05:32:35 +0000869 LocalInstantiationScope Scope(SemaRef, /*CombineWithOuterScope=*/true);
Douglas Gregor14cf7522010-04-30 18:55:50 +0000870 return inherited::TransformFunctionProtoType(TLB, TL, ObjectType);
John McCall58f10c32010-03-11 09:03:00 +0000871}
872
873ParmVarDecl *
874TemplateInstantiator::TransformFunctionTypeParam(ParmVarDecl *OldParm) {
Douglas Gregor940bca72010-04-12 07:48:19 +0000875 return SemaRef.SubstParmVarDecl(OldParm, TemplateArgs);
John McCall58f10c32010-03-11 09:03:00 +0000876}
877
Mike Stump11289f42009-09-09 15:08:12 +0000878QualType
John McCall550e0c22009-10-21 00:40:46 +0000879TemplateInstantiator::TransformTemplateTypeParmType(TypeLocBuilder &TLB,
Douglas Gregorfe17d252010-02-16 19:09:40 +0000880 TemplateTypeParmTypeLoc TL,
881 QualType ObjectType) {
John McCall550e0c22009-10-21 00:40:46 +0000882 TemplateTypeParmType *T = TL.getTypePtr();
Douglas Gregor01afeef2009-08-28 20:31:08 +0000883 if (T->getDepth() < TemplateArgs.getNumLevels()) {
Douglas Gregorfe1e1102009-02-27 19:31:52 +0000884 // Replace the template type parameter with its corresponding
885 // template argument.
Mike Stump11289f42009-09-09 15:08:12 +0000886
887 // If the corresponding template argument is NULL or doesn't exist, it's
888 // because we are performing instantiation from explicitly-specified
889 // template arguments in a function template class, but there were some
Douglas Gregore3f1f352009-07-01 00:28:38 +0000890 // arguments left unspecified.
John McCall550e0c22009-10-21 00:40:46 +0000891 if (!TemplateArgs.hasTemplateArgument(T->getDepth(), T->getIndex())) {
892 TemplateTypeParmTypeLoc NewTL
893 = TLB.push<TemplateTypeParmTypeLoc>(TL.getType());
894 NewTL.setNameLoc(TL.getNameLoc());
895 return TL.getType();
896 }
Mike Stump11289f42009-09-09 15:08:12 +0000897
898 assert(TemplateArgs(T->getDepth(), T->getIndex()).getKind()
Douglas Gregor01afeef2009-08-28 20:31:08 +0000899 == TemplateArgument::Type &&
Douglas Gregorfe1e1102009-02-27 19:31:52 +0000900 "Template argument kind mismatch");
Douglas Gregor01afeef2009-08-28 20:31:08 +0000901
John McCallcebee162009-10-18 09:09:24 +0000902 QualType Replacement
903 = TemplateArgs(T->getDepth(), T->getIndex()).getAsType();
904
905 // TODO: only do this uniquing once, at the start of instantiation.
John McCall550e0c22009-10-21 00:40:46 +0000906 QualType Result
907 = getSema().Context.getSubstTemplateTypeParmType(T, Replacement);
908 SubstTemplateTypeParmTypeLoc NewTL
909 = TLB.push<SubstTemplateTypeParmTypeLoc>(Result);
910 NewTL.setNameLoc(TL.getNameLoc());
911 return Result;
Mike Stump11289f42009-09-09 15:08:12 +0000912 }
Douglas Gregorfe1e1102009-02-27 19:31:52 +0000913
914 // The template type parameter comes from an inner template (e.g.,
915 // the template parameter list of a member template inside the
916 // template we are instantiating). Create a new template type
917 // parameter with the template "level" reduced by one.
John McCall550e0c22009-10-21 00:40:46 +0000918 QualType Result
919 = getSema().Context.getTemplateTypeParmType(T->getDepth()
920 - TemplateArgs.getNumLevels(),
921 T->getIndex(),
922 T->isParameterPack(),
Douglas Gregor2ebcae12010-06-16 15:23:05 +0000923 T->getName());
John McCall550e0c22009-10-21 00:40:46 +0000924 TemplateTypeParmTypeLoc NewTL = TLB.push<TemplateTypeParmTypeLoc>(Result);
925 NewTL.setNameLoc(TL.getNameLoc());
926 return Result;
Douglas Gregor17c0d7b2009-02-28 00:25:32 +0000927}
Douglas Gregorfe1e1102009-02-27 19:31:52 +0000928
John McCall76d824f2009-08-25 22:02:44 +0000929/// \brief Perform substitution on the type T with a given set of template
930/// arguments.
Douglas Gregorfe1e1102009-02-27 19:31:52 +0000931///
932/// This routine substitutes the given template arguments into the
933/// type T and produces the instantiated type.
934///
935/// \param T the type into which the template arguments will be
936/// substituted. If this type is not dependent, it will be returned
937/// immediately.
938///
939/// \param TemplateArgs the template arguments that will be
940/// substituted for the top-level template parameters within T.
941///
Douglas Gregorfe1e1102009-02-27 19:31:52 +0000942/// \param Loc the location in the source code where this substitution
943/// is being performed. It will typically be the location of the
944/// declarator (if we're instantiating the type of some declaration)
945/// or the location of the type in the source code (if, e.g., we're
946/// instantiating the type of a cast expression).
947///
948/// \param Entity the name of the entity associated with a declaration
949/// being instantiated (if any). May be empty to indicate that there
950/// is no such entity (if, e.g., this is a type that occurs as part of
951/// a cast expression) or that the entity has no name (e.g., an
952/// unnamed function parameter).
953///
954/// \returns If the instantiation succeeds, the instantiated
955/// type. Otherwise, produces diagnostics and returns a NULL type.
John McCallbcd03502009-12-07 02:54:59 +0000956TypeSourceInfo *Sema::SubstType(TypeSourceInfo *T,
John McCall609459e2009-10-21 00:58:09 +0000957 const MultiLevelTemplateArgumentList &Args,
958 SourceLocation Loc,
959 DeclarationName Entity) {
960 assert(!ActiveTemplateInstantiations.empty() &&
961 "Cannot perform an instantiation without some context on the "
962 "instantiation stack");
963
Douglas Gregor5a5073e2010-05-24 17:22:01 +0000964 if (!T->getType()->isDependentType() &&
965 !T->getType()->isVariablyModifiedType())
John McCall609459e2009-10-21 00:58:09 +0000966 return T;
967
968 TemplateInstantiator Instantiator(*this, Args, Loc, Entity);
969 return Instantiator.TransformType(T);
970}
971
972/// Deprecated form of the above.
Mike Stump11289f42009-09-09 15:08:12 +0000973QualType Sema::SubstType(QualType T,
Douglas Gregor01afeef2009-08-28 20:31:08 +0000974 const MultiLevelTemplateArgumentList &TemplateArgs,
John McCall76d824f2009-08-25 22:02:44 +0000975 SourceLocation Loc, DeclarationName Entity) {
Douglas Gregor79cf6032009-03-10 20:44:00 +0000976 assert(!ActiveTemplateInstantiations.empty() &&
977 "Cannot perform an instantiation without some context on the "
978 "instantiation stack");
979
Douglas Gregor5a5073e2010-05-24 17:22:01 +0000980 // If T is not a dependent type or a variably-modified type, there
981 // is nothing to do.
982 if (!T->isDependentType() && !T->isVariablyModifiedType())
Douglas Gregorfe1e1102009-02-27 19:31:52 +0000983 return T;
984
Douglas Gregord6ff3322009-08-04 16:50:30 +0000985 TemplateInstantiator Instantiator(*this, TemplateArgs, Loc, Entity);
986 return Instantiator.TransformType(T);
Douglas Gregorfe1e1102009-02-27 19:31:52 +0000987}
Douglas Gregor463421d2009-03-03 04:44:36 +0000988
John McCallb29f78f2010-04-09 17:38:44 +0000989static bool NeedsInstantiationAsFunctionType(TypeSourceInfo *T) {
Douglas Gregor5a5073e2010-05-24 17:22:01 +0000990 if (T->getType()->isDependentType() || T->getType()->isVariablyModifiedType())
John McCallb29f78f2010-04-09 17:38:44 +0000991 return true;
992
993 TypeLoc TL = T->getTypeLoc();
994 if (!isa<FunctionProtoTypeLoc>(TL))
995 return false;
996
997 FunctionProtoTypeLoc FP = cast<FunctionProtoTypeLoc>(TL);
998 for (unsigned I = 0, E = FP.getNumArgs(); I != E; ++I) {
999 ParmVarDecl *P = FP.getArg(I);
1000
1001 // TODO: currently we always rebuild expressions. When we
1002 // properly get lazier about this, we should use the same
1003 // logic to avoid rebuilding prototypes here.
1004 if (P->hasInit())
1005 return true;
1006 }
1007
1008 return false;
1009}
1010
1011/// A form of SubstType intended specifically for instantiating the
1012/// type of a FunctionDecl. Its purpose is solely to force the
1013/// instantiation of default-argument expressions.
1014TypeSourceInfo *Sema::SubstFunctionDeclType(TypeSourceInfo *T,
1015 const MultiLevelTemplateArgumentList &Args,
1016 SourceLocation Loc,
1017 DeclarationName Entity) {
1018 assert(!ActiveTemplateInstantiations.empty() &&
1019 "Cannot perform an instantiation without some context on the "
1020 "instantiation stack");
1021
1022 if (!NeedsInstantiationAsFunctionType(T))
1023 return T;
1024
1025 TemplateInstantiator Instantiator(*this, Args, Loc, Entity);
1026
1027 TypeLocBuilder TLB;
1028
1029 TypeLoc TL = T->getTypeLoc();
1030 TLB.reserve(TL.getFullDataSize());
1031
1032 QualType Result = Instantiator.TransformType(TLB, TL, QualType());
1033 if (Result.isNull())
1034 return 0;
1035
1036 return TLB.getTypeSourceInfo(Context, Result);
1037}
1038
Douglas Gregor940bca72010-04-12 07:48:19 +00001039ParmVarDecl *Sema::SubstParmVarDecl(ParmVarDecl *OldParm,
1040 const MultiLevelTemplateArgumentList &TemplateArgs) {
1041 TypeSourceInfo *OldDI = OldParm->getTypeSourceInfo();
1042 TypeSourceInfo *NewDI = SubstType(OldDI, TemplateArgs, OldParm->getLocation(),
1043 OldParm->getDeclName());
1044 if (!NewDI)
1045 return 0;
1046
1047 if (NewDI->getType()->isVoidType()) {
1048 Diag(OldParm->getLocation(), diag::err_param_with_void_type);
1049 return 0;
1050 }
1051
1052 ParmVarDecl *NewParm = CheckParameter(Context.getTranslationUnitDecl(),
1053 NewDI, NewDI->getType(),
1054 OldParm->getIdentifier(),
1055 OldParm->getLocation(),
Douglas Gregorc4df4072010-04-19 22:54:31 +00001056 OldParm->getStorageClass(),
1057 OldParm->getStorageClassAsWritten());
Douglas Gregor940bca72010-04-12 07:48:19 +00001058 if (!NewParm)
1059 return 0;
Douglas Gregor6044d692010-05-19 17:02:24 +00001060
Douglas Gregor940bca72010-04-12 07:48:19 +00001061 // Mark the (new) default argument as uninstantiated (if any).
1062 if (OldParm->hasUninstantiatedDefaultArg()) {
1063 Expr *Arg = OldParm->getUninstantiatedDefaultArg();
1064 NewParm->setUninstantiatedDefaultArg(Arg);
Douglas Gregor758cb672010-10-12 18:23:32 +00001065 } else if (OldParm->hasUnparsedDefaultArg()) {
1066 NewParm->setUnparsedDefaultArg();
1067 UnparsedDefaultArgInstantiations[OldParm].push_back(NewParm);
Douglas Gregor940bca72010-04-12 07:48:19 +00001068 } else if (Expr *Arg = OldParm->getDefaultArg())
1069 NewParm->setUninstantiatedDefaultArg(Arg);
1070
1071 NewParm->setHasInheritedDefaultArg(OldParm->hasInheritedDefaultArg());
1072
1073 CurrentInstantiationScope->InstantiatedLocal(OldParm, NewParm);
Argyrios Kyrtzidis3816ed42010-07-19 10:14:41 +00001074 // FIXME: OldParm may come from a FunctionProtoType, in which case CurContext
1075 // can be anything, is this right ?
Fariborz Jahanian714447b2010-07-13 21:05:02 +00001076 NewParm->setDeclContext(CurContext);
Fariborz Jahaniana6c7efe2010-07-13 20:05:58 +00001077
Douglas Gregor940bca72010-04-12 07:48:19 +00001078 return NewParm;
1079}
1080
John McCall76d824f2009-08-25 22:02:44 +00001081/// \brief Perform substitution on the base class specifiers of the
1082/// given class template specialization.
Douglas Gregor463421d2009-03-03 04:44:36 +00001083///
1084/// Produces a diagnostic and returns true on error, returns false and
1085/// attaches the instantiated base classes to the class template
1086/// specialization if successful.
Mike Stump11289f42009-09-09 15:08:12 +00001087bool
John McCall76d824f2009-08-25 22:02:44 +00001088Sema::SubstBaseSpecifiers(CXXRecordDecl *Instantiation,
1089 CXXRecordDecl *Pattern,
Douglas Gregor01afeef2009-08-28 20:31:08 +00001090 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor463421d2009-03-03 04:44:36 +00001091 bool Invalid = false;
Douglas Gregor6181ded2009-05-29 18:27:38 +00001092 llvm::SmallVector<CXXBaseSpecifier*, 4> InstantiatedBases;
Mike Stump11289f42009-09-09 15:08:12 +00001093 for (ClassTemplateSpecializationDecl::base_class_iterator
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001094 Base = Pattern->bases_begin(), BaseEnd = Pattern->bases_end();
Douglas Gregor2a72edd2009-03-10 18:52:44 +00001095 Base != BaseEnd; ++Base) {
Douglas Gregor463421d2009-03-03 04:44:36 +00001096 if (!Base->getType()->isDependentType()) {
Fariborz Jahanian5c14ec32009-07-22 17:41:53 +00001097 InstantiatedBases.push_back(new (Context) CXXBaseSpecifier(*Base));
Douglas Gregor463421d2009-03-03 04:44:36 +00001098 continue;
1099 }
1100
Nick Lewycky19b9f952010-07-26 16:56:01 +00001101 TypeSourceInfo *BaseTypeLoc = SubstType(Base->getTypeSourceInfo(),
1102 TemplateArgs,
1103 Base->getSourceRange().getBegin(),
1104 DeclarationName());
1105 if (!BaseTypeLoc) {
Douglas Gregor463421d2009-03-03 04:44:36 +00001106 Invalid = true;
1107 continue;
1108 }
1109
1110 if (CXXBaseSpecifier *InstantiatedBase
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001111 = CheckBaseSpecifier(Instantiation,
Douglas Gregor463421d2009-03-03 04:44:36 +00001112 Base->getSourceRange(),
1113 Base->isVirtual(),
1114 Base->getAccessSpecifierAsWritten(),
Nick Lewycky19b9f952010-07-26 16:56:01 +00001115 BaseTypeLoc))
Douglas Gregor463421d2009-03-03 04:44:36 +00001116 InstantiatedBases.push_back(InstantiatedBase);
1117 else
1118 Invalid = true;
1119 }
1120
Douglas Gregor2a72edd2009-03-10 18:52:44 +00001121 if (!Invalid &&
Jay Foad7d0479f2009-05-21 09:52:38 +00001122 AttachBaseSpecifiers(Instantiation, InstantiatedBases.data(),
Douglas Gregor463421d2009-03-03 04:44:36 +00001123 InstantiatedBases.size()))
1124 Invalid = true;
1125
1126 return Invalid;
1127}
1128
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001129/// \brief Instantiate the definition of a class from a given pattern.
1130///
1131/// \param PointOfInstantiation The point of instantiation within the
1132/// source code.
1133///
1134/// \param Instantiation is the declaration whose definition is being
1135/// instantiated. This will be either a class template specialization
1136/// or a member class of a class template specialization.
1137///
1138/// \param Pattern is the pattern from which the instantiation
1139/// occurs. This will be either the declaration of a class template or
1140/// the declaration of a member class of a class template.
1141///
1142/// \param TemplateArgs The template arguments to be substituted into
1143/// the pattern.
1144///
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001145/// \param TSK the kind of implicit or explicit instantiation to perform.
Douglas Gregor8a2e6012009-08-24 15:23:48 +00001146///
1147/// \param Complain whether to complain if the class cannot be instantiated due
1148/// to the lack of a definition.
1149///
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001150/// \returns true if an error occurred, false otherwise.
1151bool
1152Sema::InstantiateClass(SourceLocation PointOfInstantiation,
1153 CXXRecordDecl *Instantiation, CXXRecordDecl *Pattern,
Douglas Gregor01afeef2009-08-28 20:31:08 +00001154 const MultiLevelTemplateArgumentList &TemplateArgs,
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001155 TemplateSpecializationKind TSK,
Douglas Gregor8a2e6012009-08-24 15:23:48 +00001156 bool Complain) {
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001157 bool Invalid = false;
John McCall87a44eb2009-08-20 01:44:21 +00001158
Mike Stump11289f42009-09-09 15:08:12 +00001159 CXXRecordDecl *PatternDef
Douglas Gregor0a5a2212010-02-11 01:04:33 +00001160 = cast_or_null<CXXRecordDecl>(Pattern->getDefinition());
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001161 if (!PatternDef) {
Douglas Gregor8a2e6012009-08-24 15:23:48 +00001162 if (!Complain) {
1163 // Say nothing
1164 } else if (Pattern == Instantiation->getInstantiatedFromMemberClass()) {
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001165 Diag(PointOfInstantiation,
1166 diag::err_implicit_instantiate_member_undefined)
1167 << Context.getTypeDeclType(Instantiation);
1168 Diag(Pattern->getLocation(), diag::note_member_of_template_here);
1169 } else {
Douglas Gregora1f49972009-05-13 00:25:59 +00001170 Diag(PointOfInstantiation, diag::err_template_instantiate_undefined)
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001171 << (TSK != TSK_ImplicitInstantiation)
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001172 << Context.getTypeDeclType(Instantiation);
1173 Diag(Pattern->getLocation(), diag::note_template_decl_here);
1174 }
1175 return true;
1176 }
1177 Pattern = PatternDef;
1178
Douglas Gregord6ba93d2009-10-15 15:54:05 +00001179 // \brief Record the point of instantiation.
1180 if (MemberSpecializationInfo *MSInfo
1181 = Instantiation->getMemberSpecializationInfo()) {
1182 MSInfo->setTemplateSpecializationKind(TSK);
1183 MSInfo->setPointOfInstantiation(PointOfInstantiation);
Douglas Gregoref6ab412009-10-27 06:26:26 +00001184 } else if (ClassTemplateSpecializationDecl *Spec
1185 = dyn_cast<ClassTemplateSpecializationDecl>(Instantiation)) {
1186 Spec->setTemplateSpecializationKind(TSK);
1187 Spec->setPointOfInstantiation(PointOfInstantiation);
Douglas Gregord6ba93d2009-10-15 15:54:05 +00001188 }
1189
Douglas Gregorf3430ae2009-03-25 21:23:52 +00001190 InstantiatingTemplate Inst(*this, PointOfInstantiation, Instantiation);
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001191 if (Inst)
1192 return true;
1193
1194 // Enter the scope of this instantiation. We don't use
1195 // PushDeclContext because we don't have a scope.
John McCall80e58cd2010-04-29 00:35:03 +00001196 ContextRAII SavedContext(*this, Instantiation);
Douglas Gregor17158422010-05-12 17:27:19 +00001197 EnterExpressionEvaluationContext EvalContext(*this,
John McCallfaf5fb42010-08-26 23:41:50 +00001198 Sema::PotentiallyEvaluated);
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001199
Douglas Gregor51121572010-03-24 01:33:17 +00001200 // If this is an instantiation of a local class, merge this local
1201 // instantiation scope with the enclosing scope. Otherwise, every
1202 // instantiation of a class has its own local instantiation scope.
1203 bool MergeWithParentScope = !Instantiation->isDefinedOutsideFunctionOrMethod();
John McCall19c1bfd2010-08-25 05:32:35 +00001204 LocalInstantiationScope Scope(*this, MergeWithParentScope);
Douglas Gregor51121572010-03-24 01:33:17 +00001205
John McCall6602bb12010-08-01 02:01:53 +00001206 // Pull attributes from the pattern onto the instantiation.
1207 InstantiateAttrs(TemplateArgs, Pattern, Instantiation);
1208
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001209 // Start the definition of this instantiation.
1210 Instantiation->startDefinition();
Douglas Gregore9029562010-05-06 00:28:52 +00001211
1212 Instantiation->setTagKind(Pattern->getTagKind());
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001213
John McCall76d824f2009-08-25 22:02:44 +00001214 // Do substitution on the base class specifiers.
1215 if (SubstBaseSpecifiers(Instantiation, Pattern, TemplateArgs))
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001216 Invalid = true;
1217
John McCall48871652010-08-21 09:40:31 +00001218 llvm::SmallVector<Decl*, 4> Fields;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001219 for (RecordDecl::decl_iterator Member = Pattern->decls_begin(),
Mike Stump11289f42009-09-09 15:08:12 +00001220 MemberEnd = Pattern->decls_end();
Douglas Gregorbcced4e2009-04-09 21:40:53 +00001221 Member != MemberEnd; ++Member) {
John McCall76d824f2009-08-25 22:02:44 +00001222 Decl *NewMember = SubstDecl(*Member, Instantiation, TemplateArgs);
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001223 if (NewMember) {
Eli Friedmand0e8de22009-12-07 00:22:08 +00001224 if (FieldDecl *Field = dyn_cast<FieldDecl>(NewMember))
John McCall48871652010-08-21 09:40:31 +00001225 Fields.push_back(Field);
Eli Friedmand0e8de22009-12-07 00:22:08 +00001226 else if (NewMember->isInvalidDecl())
1227 Invalid = true;
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001228 } else {
1229 // FIXME: Eventually, a NULL return will mean that one of the
Mike Stump87c57ac2009-05-16 07:39:55 +00001230 // instantiations was a semantic disaster, and we'll want to set Invalid =
1231 // true. For now, we expect to skip some members that we can't yet handle.
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001232 }
1233 }
1234
1235 // Finish checking fields.
John McCall48871652010-08-21 09:40:31 +00001236 ActOnFields(0, Instantiation->getLocation(), Instantiation,
Jay Foad7d0479f2009-05-21 09:52:38 +00001237 Fields.data(), Fields.size(), SourceLocation(), SourceLocation(),
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001238 0);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001239 CheckCompletedCXXClass(Instantiation);
Douglas Gregor3c74d412009-10-14 20:14:33 +00001240 if (Instantiation->isInvalidDecl())
1241 Invalid = true;
1242
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001243 // Exit the scope of this instantiation.
John McCall80e58cd2010-04-29 00:35:03 +00001244 SavedContext.pop();
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001245
Douglas Gregor88d292c2010-05-13 16:44:06 +00001246 if (!Invalid) {
Douglas Gregor28ad4b52009-05-26 20:50:29 +00001247 Consumer.HandleTagDeclDefinition(Instantiation);
1248
Douglas Gregor88d292c2010-05-13 16:44:06 +00001249 // Always emit the vtable for an explicit instantiation definition
1250 // of a polymorphic class template specialization.
1251 if (TSK == TSK_ExplicitInstantiationDefinition)
1252 MarkVTableUsed(PointOfInstantiation, Instantiation, true);
1253 }
1254
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001255 return Invalid;
1256}
1257
Mike Stump11289f42009-09-09 15:08:12 +00001258bool
Douglas Gregor463421d2009-03-03 04:44:36 +00001259Sema::InstantiateClassTemplateSpecialization(
Douglas Gregoref6ab412009-10-27 06:26:26 +00001260 SourceLocation PointOfInstantiation,
Douglas Gregor463421d2009-03-03 04:44:36 +00001261 ClassTemplateSpecializationDecl *ClassTemplateSpec,
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001262 TemplateSpecializationKind TSK,
Douglas Gregor8a2e6012009-08-24 15:23:48 +00001263 bool Complain) {
Douglas Gregor463421d2009-03-03 04:44:36 +00001264 // Perform the actual instantiation on the canonical declaration.
1265 ClassTemplateSpec = cast<ClassTemplateSpecializationDecl>(
Argyrios Kyrtzidis6b7e3762009-07-18 00:34:25 +00001266 ClassTemplateSpec->getCanonicalDecl());
Douglas Gregor463421d2009-03-03 04:44:36 +00001267
Douglas Gregor4aa04b12009-09-11 21:19:12 +00001268 // Check whether we have already instantiated or specialized this class
1269 // template specialization.
1270 if (ClassTemplateSpec->getSpecializationKind() != TSK_Undeclared) {
1271 if (ClassTemplateSpec->getSpecializationKind() ==
1272 TSK_ExplicitInstantiationDeclaration &&
1273 TSK == TSK_ExplicitInstantiationDefinition) {
1274 // An explicit instantiation definition follows an explicit instantiation
1275 // declaration (C++0x [temp.explicit]p10); go ahead and perform the
1276 // explicit instantiation.
1277 ClassTemplateSpec->setSpecializationKind(TSK);
Douglas Gregor88d292c2010-05-13 16:44:06 +00001278
1279 // If this is an explicit instantiation definition, mark the
1280 // vtable as used.
1281 if (TSK == TSK_ExplicitInstantiationDefinition)
1282 MarkVTableUsed(PointOfInstantiation, ClassTemplateSpec, true);
1283
Douglas Gregor4aa04b12009-09-11 21:19:12 +00001284 return false;
1285 }
1286
1287 // We can only instantiate something that hasn't already been
1288 // instantiated or specialized. Fail without any diagnostics: our
1289 // caller will provide an error message.
Douglas Gregor463421d2009-03-03 04:44:36 +00001290 return true;
Douglas Gregor4aa04b12009-09-11 21:19:12 +00001291 }
Douglas Gregor463421d2009-03-03 04:44:36 +00001292
Douglas Gregor00a511f2009-09-15 16:51:42 +00001293 if (ClassTemplateSpec->isInvalidDecl())
1294 return true;
1295
Douglas Gregor463421d2009-03-03 04:44:36 +00001296 ClassTemplateDecl *Template = ClassTemplateSpec->getSpecializedTemplate();
Douglas Gregor01afeef2009-08-28 20:31:08 +00001297 CXXRecordDecl *Pattern = 0;
Douglas Gregor2373c592009-05-31 09:31:02 +00001298
Douglas Gregor170bc422009-06-12 22:31:52 +00001299 // C++ [temp.class.spec.match]p1:
1300 // When a class template is used in a context that requires an
1301 // instantiation of the class, it is necessary to determine
1302 // whether the instantiation is to be generated using the primary
1303 // template or one of the partial specializations. This is done by
1304 // matching the template arguments of the class template
1305 // specialization with the template argument lists of the partial
1306 // specializations.
Douglas Gregorb7ae10f2009-06-05 00:53:49 +00001307 typedef std::pair<ClassTemplatePartialSpecializationDecl *,
1308 TemplateArgumentList *> MatchResult;
1309 llvm::SmallVector<MatchResult, 4> Matched;
Douglas Gregor407e9612010-04-30 05:56:50 +00001310 llvm::SmallVector<ClassTemplatePartialSpecializationDecl *, 4> PartialSpecs;
1311 Template->getPartialSpecializations(PartialSpecs);
1312 for (unsigned I = 0, N = PartialSpecs.size(); I != N; ++I) {
1313 ClassTemplatePartialSpecializationDecl *Partial = PartialSpecs[I];
John McCallbc077cf2010-02-08 23:07:23 +00001314 TemplateDeductionInfo Info(Context, PointOfInstantiation);
Douglas Gregor181aa4a2009-06-12 18:26:56 +00001315 if (TemplateDeductionResult Result
Douglas Gregor407e9612010-04-30 05:56:50 +00001316 = DeduceTemplateArguments(Partial,
Douglas Gregor181aa4a2009-06-12 18:26:56 +00001317 ClassTemplateSpec->getTemplateArgs(),
1318 Info)) {
1319 // FIXME: Store the failed-deduction information for use in
1320 // diagnostics, later.
1321 (void)Result;
1322 } else {
Douglas Gregor407e9612010-04-30 05:56:50 +00001323 Matched.push_back(std::make_pair(Partial, Info.take()));
Douglas Gregor181aa4a2009-06-12 18:26:56 +00001324 }
Douglas Gregor2373c592009-05-31 09:31:02 +00001325 }
1326
Douglas Gregor21610382009-10-29 00:04:11 +00001327 if (Matched.size() >= 1) {
Douglas Gregorbe999392009-09-15 16:23:51 +00001328 llvm::SmallVector<MatchResult, 4>::iterator Best = Matched.begin();
Douglas Gregor21610382009-10-29 00:04:11 +00001329 if (Matched.size() == 1) {
1330 // -- If exactly one matching specialization is found, the
1331 // instantiation is generated from that specialization.
1332 // We don't need to do anything for this.
1333 } else {
1334 // -- If more than one matching specialization is found, the
1335 // partial order rules (14.5.4.2) are used to determine
1336 // whether one of the specializations is more specialized
1337 // than the others. If none of the specializations is more
1338 // specialized than all of the other matching
1339 // specializations, then the use of the class template is
1340 // ambiguous and the program is ill-formed.
1341 for (llvm::SmallVector<MatchResult, 4>::iterator P = Best + 1,
1342 PEnd = Matched.end();
1343 P != PEnd; ++P) {
John McCallbc077cf2010-02-08 23:07:23 +00001344 if (getMoreSpecializedPartialSpecialization(P->first, Best->first,
1345 PointOfInstantiation)
Douglas Gregor21610382009-10-29 00:04:11 +00001346 == P->first)
1347 Best = P;
Douglas Gregorbe999392009-09-15 16:23:51 +00001348 }
Douglas Gregorbe999392009-09-15 16:23:51 +00001349
Douglas Gregor21610382009-10-29 00:04:11 +00001350 // Determine if the best partial specialization is more specialized than
1351 // the others.
1352 bool Ambiguous = false;
Douglas Gregorbe999392009-09-15 16:23:51 +00001353 for (llvm::SmallVector<MatchResult, 4>::iterator P = Matched.begin(),
1354 PEnd = Matched.end();
Douglas Gregor21610382009-10-29 00:04:11 +00001355 P != PEnd; ++P) {
1356 if (P != Best &&
John McCallbc077cf2010-02-08 23:07:23 +00001357 getMoreSpecializedPartialSpecialization(P->first, Best->first,
1358 PointOfInstantiation)
Douglas Gregor21610382009-10-29 00:04:11 +00001359 != Best->first) {
1360 Ambiguous = true;
1361 break;
1362 }
1363 }
1364
1365 if (Ambiguous) {
1366 // Partial ordering did not produce a clear winner. Complain.
1367 ClassTemplateSpec->setInvalidDecl();
1368 Diag(PointOfInstantiation, diag::err_partial_spec_ordering_ambiguous)
1369 << ClassTemplateSpec;
1370
1371 // Print the matching partial specializations.
1372 for (llvm::SmallVector<MatchResult, 4>::iterator P = Matched.begin(),
1373 PEnd = Matched.end();
1374 P != PEnd; ++P)
1375 Diag(P->first->getLocation(), diag::note_partial_spec_match)
1376 << getTemplateArgumentBindingsText(P->first->getTemplateParameters(),
1377 *P->second);
Douglas Gregor01afeef2009-08-28 20:31:08 +00001378
Douglas Gregor21610382009-10-29 00:04:11 +00001379 return true;
1380 }
Douglas Gregorbe999392009-09-15 16:23:51 +00001381 }
1382
1383 // Instantiate using the best class template partial specialization.
Douglas Gregor21610382009-10-29 00:04:11 +00001384 ClassTemplatePartialSpecializationDecl *OrigPartialSpec = Best->first;
1385 while (OrigPartialSpec->getInstantiatedFromMember()) {
1386 // If we've found an explicit specialization of this class template,
1387 // stop here and use that as the pattern.
1388 if (OrigPartialSpec->isMemberSpecialization())
1389 break;
1390
1391 OrigPartialSpec = OrigPartialSpec->getInstantiatedFromMember();
1392 }
1393
1394 Pattern = OrigPartialSpec;
Douglas Gregorbe999392009-09-15 16:23:51 +00001395 ClassTemplateSpec->setInstantiationOf(Best->first, Best->second);
Douglas Gregor170bc422009-06-12 22:31:52 +00001396 } else {
1397 // -- If no matches are found, the instantiation is generated
1398 // from the primary template.
Douglas Gregor01afeef2009-08-28 20:31:08 +00001399 ClassTemplateDecl *OrigTemplate = Template;
Douglas Gregorcf915552009-10-13 16:30:37 +00001400 while (OrigTemplate->getInstantiatedFromMemberTemplate()) {
1401 // If we've found an explicit specialization of this class template,
1402 // stop here and use that as the pattern.
1403 if (OrigTemplate->isMemberSpecialization())
1404 break;
1405
Douglas Gregor01afeef2009-08-28 20:31:08 +00001406 OrigTemplate = OrigTemplate->getInstantiatedFromMemberTemplate();
Douglas Gregorcf915552009-10-13 16:30:37 +00001407 }
1408
Douglas Gregor01afeef2009-08-28 20:31:08 +00001409 Pattern = OrigTemplate->getTemplatedDecl();
Douglas Gregor2373c592009-05-31 09:31:02 +00001410 }
Douglas Gregor463421d2009-03-03 04:44:36 +00001411
Douglas Gregoref6ab412009-10-27 06:26:26 +00001412 bool Result = InstantiateClass(PointOfInstantiation, ClassTemplateSpec,
1413 Pattern,
1414 getTemplateInstantiationArgs(ClassTemplateSpec),
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001415 TSK,
Douglas Gregor8a2e6012009-08-24 15:23:48 +00001416 Complain);
Mike Stump11289f42009-09-09 15:08:12 +00001417
Douglas Gregorb7ae10f2009-06-05 00:53:49 +00001418 return Result;
Douglas Gregor463421d2009-03-03 04:44:36 +00001419}
Douglas Gregor90a1a652009-03-19 17:26:29 +00001420
John McCall76d824f2009-08-25 22:02:44 +00001421/// \brief Instantiates the definitions of all of the member
1422/// of the given class, which is an instantiation of a class template
1423/// or a member class of a template.
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00001424void
1425Sema::InstantiateClassMembers(SourceLocation PointOfInstantiation,
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001426 CXXRecordDecl *Instantiation,
1427 const MultiLevelTemplateArgumentList &TemplateArgs,
1428 TemplateSpecializationKind TSK) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001429 for (DeclContext::decl_iterator D = Instantiation->decls_begin(),
1430 DEnd = Instantiation->decls_end();
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00001431 D != DEnd; ++D) {
Douglas Gregor1d957a32009-10-27 18:42:08 +00001432 bool SuppressNew = false;
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00001433 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(*D)) {
Douglas Gregor1d957a32009-10-27 18:42:08 +00001434 if (FunctionDecl *Pattern
1435 = Function->getInstantiatedFromMemberFunction()) {
1436 MemberSpecializationInfo *MSInfo
1437 = Function->getMemberSpecializationInfo();
1438 assert(MSInfo && "No member specialization information?");
Douglas Gregor06aa50412010-04-09 21:02:29 +00001439 if (MSInfo->getTemplateSpecializationKind()
1440 == TSK_ExplicitSpecialization)
1441 continue;
1442
Douglas Gregor1d957a32009-10-27 18:42:08 +00001443 if (CheckSpecializationInstantiationRedecl(PointOfInstantiation, TSK,
1444 Function,
1445 MSInfo->getTemplateSpecializationKind(),
Nico Weberd75488d2010-09-27 21:02:09 +00001446 MSInfo->getPointOfInstantiation(),
Douglas Gregor1d957a32009-10-27 18:42:08 +00001447 SuppressNew) ||
1448 SuppressNew)
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001449 continue;
1450
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001451 if (Function->hasBody())
Douglas Gregor1d957a32009-10-27 18:42:08 +00001452 continue;
1453
1454 if (TSK == TSK_ExplicitInstantiationDefinition) {
1455 // C++0x [temp.explicit]p8:
1456 // An explicit instantiation definition that names a class template
1457 // specialization explicitly instantiates the class template
1458 // specialization and is only an explicit instantiation definition
1459 // of members whose definition is visible at the point of
1460 // instantiation.
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001461 if (!Pattern->hasBody())
Douglas Gregor1d957a32009-10-27 18:42:08 +00001462 continue;
1463
1464 Function->setTemplateSpecializationKind(TSK, PointOfInstantiation);
1465
1466 InstantiateFunctionDefinition(PointOfInstantiation, Function);
1467 } else {
1468 Function->setTemplateSpecializationKind(TSK, PointOfInstantiation);
1469 }
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001470 }
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00001471 } else if (VarDecl *Var = dyn_cast<VarDecl>(*D)) {
Douglas Gregor86d142a2009-10-08 07:24:58 +00001472 if (Var->isStaticDataMember()) {
Douglas Gregor1d957a32009-10-27 18:42:08 +00001473 MemberSpecializationInfo *MSInfo = Var->getMemberSpecializationInfo();
1474 assert(MSInfo && "No member specialization information?");
Douglas Gregor06aa50412010-04-09 21:02:29 +00001475 if (MSInfo->getTemplateSpecializationKind()
1476 == TSK_ExplicitSpecialization)
1477 continue;
1478
Douglas Gregor1d957a32009-10-27 18:42:08 +00001479 if (CheckSpecializationInstantiationRedecl(PointOfInstantiation, TSK,
1480 Var,
1481 MSInfo->getTemplateSpecializationKind(),
Nico Weberd75488d2010-09-27 21:02:09 +00001482 MSInfo->getPointOfInstantiation(),
Douglas Gregor1d957a32009-10-27 18:42:08 +00001483 SuppressNew) ||
1484 SuppressNew)
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001485 continue;
1486
Douglas Gregor1d957a32009-10-27 18:42:08 +00001487 if (TSK == TSK_ExplicitInstantiationDefinition) {
1488 // C++0x [temp.explicit]p8:
1489 // An explicit instantiation definition that names a class template
1490 // specialization explicitly instantiates the class template
1491 // specialization and is only an explicit instantiation definition
1492 // of members whose definition is visible at the point of
1493 // instantiation.
1494 if (!Var->getInstantiatedFromStaticDataMember()
1495 ->getOutOfLineDefinition())
1496 continue;
1497
1498 Var->setTemplateSpecializationKind(TSK, PointOfInstantiation);
Douglas Gregor86d142a2009-10-08 07:24:58 +00001499 InstantiateStaticDataMemberDefinition(PointOfInstantiation, Var);
Douglas Gregor1d957a32009-10-27 18:42:08 +00001500 } else {
1501 Var->setTemplateSpecializationKind(TSK, PointOfInstantiation);
1502 }
1503 }
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00001504 } else if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(*D)) {
Douglas Gregor1da22252010-04-18 18:11:38 +00001505 // Always skip the injected-class-name, along with any
1506 // redeclarations of nested classes, since both would cause us
1507 // to try to instantiate the members of a class twice.
1508 if (Record->isInjectedClassName() || Record->getPreviousDeclaration())
Douglas Gregord801b062009-10-07 23:56:10 +00001509 continue;
1510
Douglas Gregor1d957a32009-10-27 18:42:08 +00001511 MemberSpecializationInfo *MSInfo = Record->getMemberSpecializationInfo();
1512 assert(MSInfo && "No member specialization information?");
Douglas Gregor06aa50412010-04-09 21:02:29 +00001513
1514 if (MSInfo->getTemplateSpecializationKind()
1515 == TSK_ExplicitSpecialization)
1516 continue;
Nico Weberd75488d2010-09-27 21:02:09 +00001517
Douglas Gregor1d957a32009-10-27 18:42:08 +00001518 if (CheckSpecializationInstantiationRedecl(PointOfInstantiation, TSK,
1519 Record,
1520 MSInfo->getTemplateSpecializationKind(),
Nico Weberd75488d2010-09-27 21:02:09 +00001521 MSInfo->getPointOfInstantiation(),
Douglas Gregor1d957a32009-10-27 18:42:08 +00001522 SuppressNew) ||
1523 SuppressNew)
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001524 continue;
1525
Douglas Gregor1d957a32009-10-27 18:42:08 +00001526 CXXRecordDecl *Pattern = Record->getInstantiatedFromMemberClass();
1527 assert(Pattern && "Missing instantiated-from-template information");
1528
Douglas Gregor0a5a2212010-02-11 01:04:33 +00001529 if (!Record->getDefinition()) {
1530 if (!Pattern->getDefinition()) {
Douglas Gregor1d957a32009-10-27 18:42:08 +00001531 // C++0x [temp.explicit]p8:
1532 // An explicit instantiation definition that names a class template
1533 // specialization explicitly instantiates the class template
1534 // specialization and is only an explicit instantiation definition
1535 // of members whose definition is visible at the point of
1536 // instantiation.
1537 if (TSK == TSK_ExplicitInstantiationDeclaration) {
1538 MSInfo->setTemplateSpecializationKind(TSK);
1539 MSInfo->setPointOfInstantiation(PointOfInstantiation);
1540 }
1541
1542 continue;
1543 }
1544
1545 InstantiateClass(PointOfInstantiation, Record, Pattern,
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001546 TemplateArgs,
1547 TSK);
Nico Weberd75488d2010-09-27 21:02:09 +00001548 } else {
1549 if (TSK == TSK_ExplicitInstantiationDefinition &&
1550 Record->getTemplateSpecializationKind() ==
1551 TSK_ExplicitInstantiationDeclaration) {
1552 Record->setTemplateSpecializationKind(TSK);
1553 MarkVTableUsed(PointOfInstantiation, Record, true);
1554 }
Douglas Gregor1d957a32009-10-27 18:42:08 +00001555 }
Douglas Gregorc093c1d2009-10-08 01:19:17 +00001556
Douglas Gregor0a5a2212010-02-11 01:04:33 +00001557 Pattern = cast_or_null<CXXRecordDecl>(Record->getDefinition());
Douglas Gregor1d957a32009-10-27 18:42:08 +00001558 if (Pattern)
1559 InstantiateClassMembers(PointOfInstantiation, Pattern, TemplateArgs,
1560 TSK);
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00001561 }
1562 }
1563}
1564
1565/// \brief Instantiate the definitions of all of the members of the
1566/// given class template specialization, which was named as part of an
1567/// explicit instantiation.
Mike Stump11289f42009-09-09 15:08:12 +00001568void
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001569Sema::InstantiateClassTemplateSpecializationMembers(
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00001570 SourceLocation PointOfInstantiation,
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001571 ClassTemplateSpecializationDecl *ClassTemplateSpec,
1572 TemplateSpecializationKind TSK) {
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00001573 // C++0x [temp.explicit]p7:
1574 // An explicit instantiation that names a class template
1575 // specialization is an explicit instantion of the same kind
1576 // (declaration or definition) of each of its members (not
1577 // including members inherited from base classes) that has not
1578 // been previously explicitly specialized in the translation unit
1579 // containing the explicit instantiation, except as described
1580 // below.
1581 InstantiateClassMembers(PointOfInstantiation, ClassTemplateSpec,
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001582 getTemplateInstantiationArgs(ClassTemplateSpec),
1583 TSK);
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00001584}
1585
John McCalldadc5752010-08-24 06:29:42 +00001586StmtResult
Douglas Gregor01afeef2009-08-28 20:31:08 +00001587Sema::SubstStmt(Stmt *S, const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregorebe10102009-08-20 07:17:43 +00001588 if (!S)
1589 return Owned(S);
1590
1591 TemplateInstantiator Instantiator(*this, TemplateArgs,
1592 SourceLocation(),
1593 DeclarationName());
1594 return Instantiator.TransformStmt(S);
1595}
1596
John McCalldadc5752010-08-24 06:29:42 +00001597ExprResult
Douglas Gregor01afeef2009-08-28 20:31:08 +00001598Sema::SubstExpr(Expr *E, const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregora16548e2009-08-11 05:31:07 +00001599 if (!E)
1600 return Owned(E);
Mike Stump11289f42009-09-09 15:08:12 +00001601
Douglas Gregora16548e2009-08-11 05:31:07 +00001602 TemplateInstantiator Instantiator(*this, TemplateArgs,
1603 SourceLocation(),
1604 DeclarationName());
1605 return Instantiator.TransformExpr(E);
1606}
1607
John McCall76d824f2009-08-25 22:02:44 +00001608/// \brief Do template substitution on a nested-name-specifier.
Douglas Gregorf21eb492009-03-26 23:50:42 +00001609NestedNameSpecifier *
John McCall76d824f2009-08-25 22:02:44 +00001610Sema::SubstNestedNameSpecifier(NestedNameSpecifier *NNS,
Douglas Gregor01afeef2009-08-28 20:31:08 +00001611 SourceRange Range,
1612 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor1135c352009-08-06 05:28:30 +00001613 TemplateInstantiator Instantiator(*this, TemplateArgs, Range.getBegin(),
1614 DeclarationName());
Douglas Gregorcd3f49f2010-02-25 04:46:04 +00001615 return Instantiator.TransformNestedNameSpecifier(NNS, Range);
Douglas Gregor90a1a652009-03-19 17:26:29 +00001616}
Douglas Gregoraa594892009-03-31 18:38:02 +00001617
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001618/// \brief Do template substitution on declaration name info.
1619DeclarationNameInfo
1620Sema::SubstDeclarationNameInfo(const DeclarationNameInfo &NameInfo,
1621 const MultiLevelTemplateArgumentList &TemplateArgs) {
1622 TemplateInstantiator Instantiator(*this, TemplateArgs, NameInfo.getLoc(),
1623 NameInfo.getName());
1624 return Instantiator.TransformDeclarationNameInfo(NameInfo);
1625}
1626
Douglas Gregoraa594892009-03-31 18:38:02 +00001627TemplateName
John McCall76d824f2009-08-25 22:02:44 +00001628Sema::SubstTemplateName(TemplateName Name, SourceLocation Loc,
Douglas Gregor01afeef2009-08-28 20:31:08 +00001629 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor71dc5092009-08-06 06:41:21 +00001630 TemplateInstantiator Instantiator(*this, TemplateArgs, Loc,
1631 DeclarationName());
1632 return Instantiator.TransformTemplateName(Name);
Douglas Gregoraa594892009-03-31 18:38:02 +00001633}
Douglas Gregorc43620d2009-06-11 00:06:24 +00001634
John McCall0ad16662009-10-29 08:12:44 +00001635bool Sema::Subst(const TemplateArgumentLoc &Input, TemplateArgumentLoc &Output,
1636 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregore922c772009-08-04 22:27:00 +00001637 TemplateInstantiator Instantiator(*this, TemplateArgs, SourceLocation(),
1638 DeclarationName());
John McCall0ad16662009-10-29 08:12:44 +00001639
1640 return Instantiator.TransformTemplateArgument(Input, Output);
Douglas Gregorc43620d2009-06-11 00:06:24 +00001641}
Douglas Gregor14cf7522010-04-30 18:55:50 +00001642
John McCall19c1bfd2010-08-25 05:32:35 +00001643Decl *LocalInstantiationScope::getInstantiationOf(const Decl *D) {
Douglas Gregor14cf7522010-04-30 18:55:50 +00001644 for (LocalInstantiationScope *Current = this; Current;
1645 Current = Current->Outer) {
1646 // Check if we found something within this scope.
1647 llvm::DenseMap<const Decl *, Decl *>::iterator Found
1648 = Current->LocalDecls.find(D);
1649 if (Found != Current->LocalDecls.end())
1650 return Found->second;
1651
1652 // If we aren't combined with our outer scope, we're done.
1653 if (!Current->CombineWithOuterScope)
1654 break;
1655 }
1656
1657 assert(D->isInvalidDecl() &&
1658 "declaration was not instantiated in this scope!");
1659 return 0;
1660}
1661
John McCall19c1bfd2010-08-25 05:32:35 +00001662void LocalInstantiationScope::InstantiatedLocal(const Decl *D, Decl *Inst) {
Douglas Gregor14cf7522010-04-30 18:55:50 +00001663 Decl *&Stored = LocalDecls[D];
1664 assert((!Stored || Stored == Inst)&& "Already instantiated this local");
1665 Stored = Inst;
1666}