blob: 82289e9416aba6594c9d319bd47d8e7dd732877f [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
Douglas Gregorc3a6ade2010-08-12 20:07:10 +000013#include "clang/Sema/Sema.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"
Douglas Gregor28ad4b52009-05-26 20:50:29 +000018#include "clang/AST/ASTConsumer.h"
Douglas Gregorfe1e1102009-02-27 19:31:52 +000019#include "clang/AST/ASTContext.h"
20#include "clang/AST/Expr.h"
Douglas Gregorfe1e1102009-02-27 19:31:52 +000021#include "clang/AST/DeclTemplate.h"
Douglas Gregorfe1e1102009-02-27 19:31:52 +000022#include "clang/Basic/LangOptions.h"
23
24using namespace clang;
25
Douglas Gregor4ea568f2009-03-10 18:03:33 +000026//===----------------------------------------------------------------------===/
27// Template Instantiation Support
28//===----------------------------------------------------------------------===/
29
Douglas Gregor01afeef2009-08-28 20:31:08 +000030/// \brief Retrieve the template argument list(s) that should be used to
31/// instantiate the definition of the given declaration.
Douglas Gregor36d7c5f2009-11-09 19:17:50 +000032///
33/// \param D the declaration for which we are computing template instantiation
34/// arguments.
35///
36/// \param Innermost if non-NULL, the innermost template argument list.
Douglas Gregor8c702532010-02-05 07:33:43 +000037///
38/// \param RelativeToPrimary true if we should get the template
39/// arguments relative to the primary template, even when we're
40/// dealing with a specialization. This is only relevant for function
41/// template specializations.
Douglas Gregor1bd7a942010-05-03 23:29:10 +000042///
43/// \param Pattern If non-NULL, indicates the pattern from which we will be
44/// instantiating the definition of the given declaration, \p D. This is
45/// used to determine the proper set of template instantiation arguments for
46/// friend function template specializations.
Douglas Gregora654dd82009-08-28 17:37:35 +000047MultiLevelTemplateArgumentList
Douglas Gregor36d7c5f2009-11-09 19:17:50 +000048Sema::getTemplateInstantiationArgs(NamedDecl *D,
Douglas Gregor8c702532010-02-05 07:33:43 +000049 const TemplateArgumentList *Innermost,
Douglas Gregor1bd7a942010-05-03 23:29:10 +000050 bool RelativeToPrimary,
51 const FunctionDecl *Pattern) {
Douglas Gregora654dd82009-08-28 17:37:35 +000052 // Accumulate the set of template argument lists in this structure.
53 MultiLevelTemplateArgumentList Result;
Mike Stump11289f42009-09-09 15:08:12 +000054
Douglas Gregor36d7c5f2009-11-09 19:17:50 +000055 if (Innermost)
56 Result.addOuterTemplateArguments(Innermost);
57
Douglas Gregora654dd82009-08-28 17:37:35 +000058 DeclContext *Ctx = dyn_cast<DeclContext>(D);
59 if (!Ctx)
60 Ctx = D->getDeclContext();
Mike Stump11289f42009-09-09 15:08:12 +000061
John McCall970d5302009-08-29 03:16:09 +000062 while (!Ctx->isFileContext()) {
Douglas Gregora654dd82009-08-28 17:37:35 +000063 // Add template arguments from a class template instantiation.
Mike Stump11289f42009-09-09 15:08:12 +000064 if (ClassTemplateSpecializationDecl *Spec
Douglas Gregora654dd82009-08-28 17:37:35 +000065 = dyn_cast<ClassTemplateSpecializationDecl>(Ctx)) {
66 // We're done when we hit an explicit specialization.
Douglas Gregor9961ce92010-07-08 18:37:38 +000067 if (Spec->getSpecializationKind() == TSK_ExplicitSpecialization &&
68 !isa<ClassTemplatePartialSpecializationDecl>(Spec))
Douglas Gregora654dd82009-08-28 17:37:35 +000069 break;
Mike Stump11289f42009-09-09 15:08:12 +000070
Douglas Gregora654dd82009-08-28 17:37:35 +000071 Result.addOuterTemplateArguments(&Spec->getTemplateInstantiationArgs());
Douglas Gregorcf915552009-10-13 16:30:37 +000072
73 // If this class template specialization was instantiated from a
74 // specialized member that is a class template, we're done.
75 assert(Spec->getSpecializedTemplate() && "No class template?");
76 if (Spec->getSpecializedTemplate()->isMemberSpecialization())
77 break;
Mike Stump11289f42009-09-09 15:08:12 +000078 }
Douglas Gregora654dd82009-08-28 17:37:35 +000079 // Add template arguments from a function template specialization.
John McCall970d5302009-08-29 03:16:09 +000080 else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Ctx)) {
Douglas Gregor8c702532010-02-05 07:33:43 +000081 if (!RelativeToPrimary &&
82 Function->getTemplateSpecializationKind()
83 == TSK_ExplicitSpecialization)
Douglas Gregorcf915552009-10-13 16:30:37 +000084 break;
85
Douglas Gregora654dd82009-08-28 17:37:35 +000086 if (const TemplateArgumentList *TemplateArgs
Douglas Gregorcf915552009-10-13 16:30:37 +000087 = Function->getTemplateSpecializationArgs()) {
88 // Add the template arguments for this specialization.
Douglas Gregora654dd82009-08-28 17:37:35 +000089 Result.addOuterTemplateArguments(TemplateArgs);
John McCall970d5302009-08-29 03:16:09 +000090
Douglas Gregorcf915552009-10-13 16:30:37 +000091 // If this function was instantiated from a specialized member that is
92 // a function template, we're done.
93 assert(Function->getPrimaryTemplate() && "No function template?");
94 if (Function->getPrimaryTemplate()->isMemberSpecialization())
95 break;
96 }
97
John McCall970d5302009-08-29 03:16:09 +000098 // If this is a friend declaration and it declares an entity at
99 // namespace scope, take arguments from its lexical parent
Douglas Gregor1bd7a942010-05-03 23:29:10 +0000100 // instead of its semantic parent, unless of course the pattern we're
101 // instantiating actually comes from the file's context!
John McCall970d5302009-08-29 03:16:09 +0000102 if (Function->getFriendObjectKind() &&
Douglas Gregor1bd7a942010-05-03 23:29:10 +0000103 Function->getDeclContext()->isFileContext() &&
104 (!Pattern || !Pattern->getLexicalDeclContext()->isFileContext())) {
John McCall970d5302009-08-29 03:16:09 +0000105 Ctx = Function->getLexicalDeclContext();
Douglas Gregor8c702532010-02-05 07:33:43 +0000106 RelativeToPrimary = false;
John McCall970d5302009-08-29 03:16:09 +0000107 continue;
108 }
Douglas Gregor9961ce92010-07-08 18:37:38 +0000109 } else if (CXXRecordDecl *Rec = dyn_cast<CXXRecordDecl>(Ctx)) {
110 if (ClassTemplateDecl *ClassTemplate = Rec->getDescribedClassTemplate()) {
111 QualType T = ClassTemplate->getInjectedClassNameSpecialization();
112 const TemplateSpecializationType *TST
113 = cast<TemplateSpecializationType>(Context.getCanonicalType(T));
114 Result.addOuterTemplateArguments(TST->getArgs(), TST->getNumArgs());
115 if (ClassTemplate->isMemberSpecialization())
116 break;
117 }
Douglas Gregora654dd82009-08-28 17:37:35 +0000118 }
John McCall970d5302009-08-29 03:16:09 +0000119
120 Ctx = Ctx->getParent();
Douglas Gregor8c702532010-02-05 07:33:43 +0000121 RelativeToPrimary = false;
Douglas Gregorb4850462009-05-14 23:26:13 +0000122 }
Mike Stump11289f42009-09-09 15:08:12 +0000123
Douglas Gregora654dd82009-08-28 17:37:35 +0000124 return Result;
Douglas Gregorb4850462009-05-14 23:26:13 +0000125}
126
Douglas Gregor84d49a22009-11-11 21:54:23 +0000127bool Sema::ActiveTemplateInstantiation::isInstantiationRecord() const {
128 switch (Kind) {
129 case TemplateInstantiation:
130 case DefaultTemplateArgumentInstantiation:
131 case DefaultFunctionArgumentInstantiation:
132 return true;
133
134 case ExplicitTemplateArgumentSubstitution:
135 case DeducedTemplateArgumentSubstitution:
136 case PriorTemplateArgumentSubstitution:
137 case DefaultTemplateArgumentChecking:
138 return false;
139 }
140
141 return true;
142}
143
Douglas Gregorfcd5db32009-03-10 00:06:19 +0000144Sema::InstantiatingTemplate::
145InstantiatingTemplate(Sema &SemaRef, SourceLocation PointOfInstantiation,
Douglas Gregor85673582009-05-18 17:01:57 +0000146 Decl *Entity,
Douglas Gregorfcd5db32009-03-10 00:06:19 +0000147 SourceRange InstantiationRange)
148 : SemaRef(SemaRef) {
Douglas Gregor79cf6032009-03-10 20:44:00 +0000149
150 Invalid = CheckInstantiationDepth(PointOfInstantiation,
151 InstantiationRange);
152 if (!Invalid) {
Douglas Gregorfcd5db32009-03-10 00:06:19 +0000153 ActiveTemplateInstantiation Inst;
Douglas Gregor79cf6032009-03-10 20:44:00 +0000154 Inst.Kind = ActiveTemplateInstantiation::TemplateInstantiation;
Douglas Gregorfcd5db32009-03-10 00:06:19 +0000155 Inst.PointOfInstantiation = PointOfInstantiation;
Douglas Gregor79cf6032009-03-10 20:44:00 +0000156 Inst.Entity = reinterpret_cast<uintptr_t>(Entity);
Douglas Gregorc9220832009-03-12 18:36:18 +0000157 Inst.TemplateArgs = 0;
158 Inst.NumTemplateArgs = 0;
Douglas Gregor79cf6032009-03-10 20:44:00 +0000159 Inst.InstantiationRange = InstantiationRange;
160 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
Douglas Gregor79cf6032009-03-10 20:44:00 +0000161 }
162}
163
Mike Stump11289f42009-09-09 15:08:12 +0000164Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef,
Douglas Gregor79cf6032009-03-10 20:44:00 +0000165 SourceLocation PointOfInstantiation,
166 TemplateDecl *Template,
167 const TemplateArgument *TemplateArgs,
168 unsigned NumTemplateArgs,
169 SourceRange InstantiationRange)
170 : SemaRef(SemaRef) {
171
172 Invalid = CheckInstantiationDepth(PointOfInstantiation,
173 InstantiationRange);
174 if (!Invalid) {
175 ActiveTemplateInstantiation Inst;
Mike Stump11289f42009-09-09 15:08:12 +0000176 Inst.Kind
Douglas Gregor79cf6032009-03-10 20:44:00 +0000177 = ActiveTemplateInstantiation::DefaultTemplateArgumentInstantiation;
178 Inst.PointOfInstantiation = PointOfInstantiation;
179 Inst.Entity = reinterpret_cast<uintptr_t>(Template);
180 Inst.TemplateArgs = TemplateArgs;
181 Inst.NumTemplateArgs = NumTemplateArgs;
Douglas Gregorfcd5db32009-03-10 00:06:19 +0000182 Inst.InstantiationRange = InstantiationRange;
183 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
Douglas Gregorfcd5db32009-03-10 00:06:19 +0000184 }
185}
186
Mike Stump11289f42009-09-09 15:08:12 +0000187Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef,
Douglas Gregor637d9982009-06-10 23:47:09 +0000188 SourceLocation PointOfInstantiation,
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000189 FunctionTemplateDecl *FunctionTemplate,
190 const TemplateArgument *TemplateArgs,
191 unsigned NumTemplateArgs,
192 ActiveTemplateInstantiation::InstantiationKind Kind,
193 SourceRange InstantiationRange)
194: SemaRef(SemaRef) {
Mike Stump11289f42009-09-09 15:08:12 +0000195
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000196 Invalid = CheckInstantiationDepth(PointOfInstantiation,
197 InstantiationRange);
198 if (!Invalid) {
199 ActiveTemplateInstantiation Inst;
200 Inst.Kind = Kind;
201 Inst.PointOfInstantiation = PointOfInstantiation;
202 Inst.Entity = reinterpret_cast<uintptr_t>(FunctionTemplate);
203 Inst.TemplateArgs = TemplateArgs;
204 Inst.NumTemplateArgs = NumTemplateArgs;
205 Inst.InstantiationRange = InstantiationRange;
206 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
Douglas Gregor84d49a22009-11-11 21:54:23 +0000207
208 if (!Inst.isInstantiationRecord())
209 ++SemaRef.NonInstantiationEntries;
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000210 }
211}
212
Mike Stump11289f42009-09-09 15:08:12 +0000213Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef,
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000214 SourceLocation PointOfInstantiation,
Douglas Gregor637d9982009-06-10 23:47:09 +0000215 ClassTemplatePartialSpecializationDecl *PartialSpec,
216 const TemplateArgument *TemplateArgs,
217 unsigned NumTemplateArgs,
218 SourceRange InstantiationRange)
219 : SemaRef(SemaRef) {
220
Douglas Gregor84d49a22009-11-11 21:54:23 +0000221 Invalid = false;
222
223 ActiveTemplateInstantiation Inst;
224 Inst.Kind = ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution;
225 Inst.PointOfInstantiation = PointOfInstantiation;
226 Inst.Entity = reinterpret_cast<uintptr_t>(PartialSpec);
227 Inst.TemplateArgs = TemplateArgs;
228 Inst.NumTemplateArgs = NumTemplateArgs;
229 Inst.InstantiationRange = InstantiationRange;
230 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
231
232 assert(!Inst.isInstantiationRecord());
233 ++SemaRef.NonInstantiationEntries;
Douglas Gregor637d9982009-06-10 23:47:09 +0000234}
235
Mike Stump11289f42009-09-09 15:08:12 +0000236Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef,
Douglas Gregore62e6a02009-11-11 19:13:48 +0000237 SourceLocation PointOfInstantiation,
Anders Carlsson657bad42009-09-05 05:14:19 +0000238 ParmVarDecl *Param,
239 const TemplateArgument *TemplateArgs,
240 unsigned NumTemplateArgs,
241 SourceRange InstantiationRange)
242 : SemaRef(SemaRef) {
Mike Stump11289f42009-09-09 15:08:12 +0000243
Douglas Gregore62e6a02009-11-11 19:13:48 +0000244 Invalid = CheckInstantiationDepth(PointOfInstantiation, InstantiationRange);
Anders Carlsson657bad42009-09-05 05:14:19 +0000245
246 if (!Invalid) {
247 ActiveTemplateInstantiation Inst;
248 Inst.Kind
249 = ActiveTemplateInstantiation::DefaultFunctionArgumentInstantiation;
Douglas Gregore62e6a02009-11-11 19:13:48 +0000250 Inst.PointOfInstantiation = PointOfInstantiation;
Anders Carlsson657bad42009-09-05 05:14:19 +0000251 Inst.Entity = reinterpret_cast<uintptr_t>(Param);
252 Inst.TemplateArgs = TemplateArgs;
253 Inst.NumTemplateArgs = NumTemplateArgs;
254 Inst.InstantiationRange = InstantiationRange;
255 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
Douglas Gregore62e6a02009-11-11 19:13:48 +0000256 }
257}
258
259Sema::InstantiatingTemplate::
260InstantiatingTemplate(Sema &SemaRef, SourceLocation PointOfInstantiation,
261 TemplateDecl *Template,
262 NonTypeTemplateParmDecl *Param,
263 const TemplateArgument *TemplateArgs,
264 unsigned NumTemplateArgs,
265 SourceRange InstantiationRange) : SemaRef(SemaRef) {
Douglas Gregor84d49a22009-11-11 21:54:23 +0000266 Invalid = false;
Douglas Gregore62e6a02009-11-11 19:13:48 +0000267
Douglas Gregor84d49a22009-11-11 21:54:23 +0000268 ActiveTemplateInstantiation Inst;
269 Inst.Kind = ActiveTemplateInstantiation::PriorTemplateArgumentSubstitution;
270 Inst.PointOfInstantiation = PointOfInstantiation;
271 Inst.Template = Template;
272 Inst.Entity = reinterpret_cast<uintptr_t>(Param);
273 Inst.TemplateArgs = TemplateArgs;
274 Inst.NumTemplateArgs = NumTemplateArgs;
275 Inst.InstantiationRange = InstantiationRange;
276 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
277
278 assert(!Inst.isInstantiationRecord());
279 ++SemaRef.NonInstantiationEntries;
Douglas Gregore62e6a02009-11-11 19:13:48 +0000280}
281
282Sema::InstantiatingTemplate::
283InstantiatingTemplate(Sema &SemaRef, SourceLocation PointOfInstantiation,
284 TemplateDecl *Template,
285 TemplateTemplateParmDecl *Param,
286 const TemplateArgument *TemplateArgs,
287 unsigned NumTemplateArgs,
288 SourceRange InstantiationRange) : SemaRef(SemaRef) {
Douglas Gregor84d49a22009-11-11 21:54:23 +0000289 Invalid = false;
290 ActiveTemplateInstantiation Inst;
291 Inst.Kind = ActiveTemplateInstantiation::PriorTemplateArgumentSubstitution;
292 Inst.PointOfInstantiation = PointOfInstantiation;
293 Inst.Template = Template;
294 Inst.Entity = reinterpret_cast<uintptr_t>(Param);
295 Inst.TemplateArgs = TemplateArgs;
296 Inst.NumTemplateArgs = NumTemplateArgs;
297 Inst.InstantiationRange = InstantiationRange;
298 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
Douglas Gregore62e6a02009-11-11 19:13:48 +0000299
Douglas Gregor84d49a22009-11-11 21:54:23 +0000300 assert(!Inst.isInstantiationRecord());
301 ++SemaRef.NonInstantiationEntries;
302}
303
304Sema::InstantiatingTemplate::
305InstantiatingTemplate(Sema &SemaRef, SourceLocation PointOfInstantiation,
306 TemplateDecl *Template,
307 NamedDecl *Param,
308 const TemplateArgument *TemplateArgs,
309 unsigned NumTemplateArgs,
310 SourceRange InstantiationRange) : SemaRef(SemaRef) {
311 Invalid = false;
312
313 ActiveTemplateInstantiation Inst;
314 Inst.Kind = ActiveTemplateInstantiation::DefaultTemplateArgumentChecking;
315 Inst.PointOfInstantiation = PointOfInstantiation;
316 Inst.Template = Template;
317 Inst.Entity = reinterpret_cast<uintptr_t>(Param);
318 Inst.TemplateArgs = TemplateArgs;
319 Inst.NumTemplateArgs = NumTemplateArgs;
320 Inst.InstantiationRange = InstantiationRange;
321 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
322
323 assert(!Inst.isInstantiationRecord());
324 ++SemaRef.NonInstantiationEntries;
Anders Carlsson657bad42009-09-05 05:14:19 +0000325}
326
Douglas Gregor85673582009-05-18 17:01:57 +0000327void Sema::InstantiatingTemplate::Clear() {
328 if (!Invalid) {
Douglas Gregor84d49a22009-11-11 21:54:23 +0000329 if (!SemaRef.ActiveTemplateInstantiations.back().isInstantiationRecord()) {
330 assert(SemaRef.NonInstantiationEntries > 0);
331 --SemaRef.NonInstantiationEntries;
332 }
333
Douglas Gregorfcd5db32009-03-10 00:06:19 +0000334 SemaRef.ActiveTemplateInstantiations.pop_back();
Douglas Gregor85673582009-05-18 17:01:57 +0000335 Invalid = true;
336 }
Douglas Gregorfcd5db32009-03-10 00:06:19 +0000337}
338
Douglas Gregor79cf6032009-03-10 20:44:00 +0000339bool Sema::InstantiatingTemplate::CheckInstantiationDepth(
340 SourceLocation PointOfInstantiation,
341 SourceRange InstantiationRange) {
Douglas Gregor84d49a22009-11-11 21:54:23 +0000342 assert(SemaRef.NonInstantiationEntries <=
343 SemaRef.ActiveTemplateInstantiations.size());
344 if ((SemaRef.ActiveTemplateInstantiations.size() -
345 SemaRef.NonInstantiationEntries)
346 <= SemaRef.getLangOptions().InstantiationDepth)
Douglas Gregor79cf6032009-03-10 20:44:00 +0000347 return false;
348
Mike Stump11289f42009-09-09 15:08:12 +0000349 SemaRef.Diag(PointOfInstantiation,
Douglas Gregor79cf6032009-03-10 20:44:00 +0000350 diag::err_template_recursion_depth_exceeded)
351 << SemaRef.getLangOptions().InstantiationDepth
352 << InstantiationRange;
353 SemaRef.Diag(PointOfInstantiation, diag::note_template_recursion_depth)
354 << SemaRef.getLangOptions().InstantiationDepth;
355 return true;
356}
357
Douglas Gregor4ea568f2009-03-10 18:03:33 +0000358/// \brief Prints the current instantiation stack through a series of
359/// notes.
360void Sema::PrintInstantiationStack() {
Douglas Gregorffed1cb2010-04-20 07:18:24 +0000361 // Determine which template instantiations to skip, if any.
362 unsigned SkipStart = ActiveTemplateInstantiations.size(), SkipEnd = SkipStart;
363 unsigned Limit = Diags.getTemplateBacktraceLimit();
364 if (Limit && Limit < ActiveTemplateInstantiations.size()) {
365 SkipStart = Limit / 2 + Limit % 2;
366 SkipEnd = ActiveTemplateInstantiations.size() - Limit / 2;
367 }
368
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000369 // FIXME: In all of these cases, we need to show the template arguments
Douglas Gregorffed1cb2010-04-20 07:18:24 +0000370 unsigned InstantiationIdx = 0;
Douglas Gregor4ea568f2009-03-10 18:03:33 +0000371 for (llvm::SmallVector<ActiveTemplateInstantiation, 16>::reverse_iterator
372 Active = ActiveTemplateInstantiations.rbegin(),
373 ActiveEnd = ActiveTemplateInstantiations.rend();
374 Active != ActiveEnd;
Douglas Gregorffed1cb2010-04-20 07:18:24 +0000375 ++Active, ++InstantiationIdx) {
376 // Skip this instantiation?
377 if (InstantiationIdx >= SkipStart && InstantiationIdx < SkipEnd) {
378 if (InstantiationIdx == SkipStart) {
379 // Note that we're skipping instantiations.
380 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
381 diag::note_instantiation_contexts_suppressed)
382 << unsigned(ActiveTemplateInstantiations.size() - Limit);
383 }
384 continue;
385 }
386
Douglas Gregor79cf6032009-03-10 20:44:00 +0000387 switch (Active->Kind) {
388 case ActiveTemplateInstantiation::TemplateInstantiation: {
Douglas Gregor85673582009-05-18 17:01:57 +0000389 Decl *D = reinterpret_cast<Decl *>(Active->Entity);
390 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) {
391 unsigned DiagID = diag::note_template_member_class_here;
392 if (isa<ClassTemplateSpecializationDecl>(Record))
393 DiagID = diag::note_template_class_instantiation_here;
Mike Stump11289f42009-09-09 15:08:12 +0000394 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
Douglas Gregor85673582009-05-18 17:01:57 +0000395 DiagID)
396 << Context.getTypeDeclType(Record)
397 << Active->InstantiationRange;
Douglas Gregora6ef8f02009-07-24 20:34:43 +0000398 } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
Douglas Gregor4adbc6d2009-06-26 00:10:03 +0000399 unsigned DiagID;
400 if (Function->getPrimaryTemplate())
401 DiagID = diag::note_function_template_spec_here;
402 else
403 DiagID = diag::note_template_member_function_here;
Mike Stump11289f42009-09-09 15:08:12 +0000404 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
Douglas Gregor85673582009-05-18 17:01:57 +0000405 DiagID)
406 << Function
407 << Active->InstantiationRange;
Douglas Gregora6ef8f02009-07-24 20:34:43 +0000408 } else {
409 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
410 diag::note_template_static_data_member_def_here)
411 << cast<VarDecl>(D)
412 << Active->InstantiationRange;
Douglas Gregor85673582009-05-18 17:01:57 +0000413 }
Douglas Gregor79cf6032009-03-10 20:44:00 +0000414 break;
415 }
416
417 case ActiveTemplateInstantiation::DefaultTemplateArgumentInstantiation: {
418 TemplateDecl *Template = cast<TemplateDecl>((Decl *)Active->Entity);
419 std::string TemplateArgsStr
Douglas Gregordc572a32009-03-30 22:58:21 +0000420 = TemplateSpecializationType::PrintTemplateArgumentList(
Mike Stump11289f42009-09-09 15:08:12 +0000421 Active->TemplateArgs,
Douglas Gregor7de59662009-05-29 20:38:28 +0000422 Active->NumTemplateArgs,
423 Context.PrintingPolicy);
Douglas Gregor79cf6032009-03-10 20:44:00 +0000424 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
425 diag::note_default_arg_instantiation_here)
426 << (Template->getNameAsString() + TemplateArgsStr)
427 << Active->InstantiationRange;
428 break;
429 }
Douglas Gregor637d9982009-06-10 23:47:09 +0000430
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000431 case ActiveTemplateInstantiation::ExplicitTemplateArgumentSubstitution: {
Mike Stump11289f42009-09-09 15:08:12 +0000432 FunctionTemplateDecl *FnTmpl
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000433 = cast<FunctionTemplateDecl>((Decl *)Active->Entity);
Douglas Gregor637d9982009-06-10 23:47:09 +0000434 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000435 diag::note_explicit_template_arg_substitution_here)
Douglas Gregor607f1412010-03-30 20:35:20 +0000436 << FnTmpl
437 << getTemplateArgumentBindingsText(FnTmpl->getTemplateParameters(),
438 Active->TemplateArgs,
439 Active->NumTemplateArgs)
440 << Active->InstantiationRange;
Douglas Gregor637d9982009-06-10 23:47:09 +0000441 break;
442 }
Mike Stump11289f42009-09-09 15:08:12 +0000443
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000444 case ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution:
445 if (ClassTemplatePartialSpecializationDecl *PartialSpec
446 = dyn_cast<ClassTemplatePartialSpecializationDecl>(
447 (Decl *)Active->Entity)) {
448 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
449 diag::note_partial_spec_deduct_instantiation_here)
450 << Context.getTypeDeclType(PartialSpec)
Douglas Gregor607f1412010-03-30 20:35:20 +0000451 << getTemplateArgumentBindingsText(
452 PartialSpec->getTemplateParameters(),
453 Active->TemplateArgs,
454 Active->NumTemplateArgs)
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000455 << Active->InstantiationRange;
456 } else {
457 FunctionTemplateDecl *FnTmpl
458 = cast<FunctionTemplateDecl>((Decl *)Active->Entity);
459 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
460 diag::note_function_template_deduction_instantiation_here)
Douglas Gregor607f1412010-03-30 20:35:20 +0000461 << FnTmpl
462 << getTemplateArgumentBindingsText(FnTmpl->getTemplateParameters(),
463 Active->TemplateArgs,
464 Active->NumTemplateArgs)
465 << Active->InstantiationRange;
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000466 }
467 break;
Douglas Gregor637d9982009-06-10 23:47:09 +0000468
Anders Carlsson657bad42009-09-05 05:14:19 +0000469 case ActiveTemplateInstantiation::DefaultFunctionArgumentInstantiation: {
470 ParmVarDecl *Param = cast<ParmVarDecl>((Decl *)Active->Entity);
471 FunctionDecl *FD = cast<FunctionDecl>(Param->getDeclContext());
Mike Stump11289f42009-09-09 15:08:12 +0000472
Anders Carlsson657bad42009-09-05 05:14:19 +0000473 std::string TemplateArgsStr
474 = TemplateSpecializationType::PrintTemplateArgumentList(
Mike Stump11289f42009-09-09 15:08:12 +0000475 Active->TemplateArgs,
Anders Carlsson657bad42009-09-05 05:14:19 +0000476 Active->NumTemplateArgs,
477 Context.PrintingPolicy);
478 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
479 diag::note_default_function_arg_instantiation_here)
Anders Carlssondc6d2c32009-09-05 05:38:54 +0000480 << (FD->getNameAsString() + TemplateArgsStr)
Anders Carlsson657bad42009-09-05 05:14:19 +0000481 << Active->InstantiationRange;
482 break;
483 }
Mike Stump11289f42009-09-09 15:08:12 +0000484
Douglas Gregore62e6a02009-11-11 19:13:48 +0000485 case ActiveTemplateInstantiation::PriorTemplateArgumentSubstitution: {
486 NamedDecl *Parm = cast<NamedDecl>((Decl *)Active->Entity);
487 std::string Name;
488 if (!Parm->getName().empty())
489 Name = std::string(" '") + Parm->getName().str() + "'";
490
491 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
492 diag::note_prior_template_arg_substitution)
493 << isa<TemplateTemplateParmDecl>(Parm)
494 << Name
495 << getTemplateArgumentBindingsText(
496 Active->Template->getTemplateParameters(),
497 Active->TemplateArgs,
498 Active->NumTemplateArgs)
499 << Active->InstantiationRange;
500 break;
501 }
Douglas Gregor84d49a22009-11-11 21:54:23 +0000502
503 case ActiveTemplateInstantiation::DefaultTemplateArgumentChecking: {
504 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
505 diag::note_template_default_arg_checking)
506 << getTemplateArgumentBindingsText(
507 Active->Template->getTemplateParameters(),
508 Active->TemplateArgs,
509 Active->NumTemplateArgs)
510 << Active->InstantiationRange;
511 break;
512 }
Douglas Gregor79cf6032009-03-10 20:44:00 +0000513 }
Douglas Gregor4ea568f2009-03-10 18:03:33 +0000514 }
515}
516
Douglas Gregor33834512009-06-14 07:33:30 +0000517bool Sema::isSFINAEContext() const {
518 using llvm::SmallVector;
519 for (SmallVector<ActiveTemplateInstantiation, 16>::const_reverse_iterator
520 Active = ActiveTemplateInstantiations.rbegin(),
521 ActiveEnd = ActiveTemplateInstantiations.rend();
522 Active != ActiveEnd;
Douglas Gregor84d49a22009-11-11 21:54:23 +0000523 ++Active)
524 {
Douglas Gregor33834512009-06-14 07:33:30 +0000525 switch(Active->Kind) {
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000526 case ActiveTemplateInstantiation::TemplateInstantiation:
Anders Carlsson657bad42009-09-05 05:14:19 +0000527 case ActiveTemplateInstantiation::DefaultFunctionArgumentInstantiation:
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000528 // This is a template instantiation, so there is no SFINAE.
529 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000530
Douglas Gregor33834512009-06-14 07:33:30 +0000531 case ActiveTemplateInstantiation::DefaultTemplateArgumentInstantiation:
Douglas Gregore62e6a02009-11-11 19:13:48 +0000532 case ActiveTemplateInstantiation::PriorTemplateArgumentSubstitution:
Douglas Gregor84d49a22009-11-11 21:54:23 +0000533 case ActiveTemplateInstantiation::DefaultTemplateArgumentChecking:
Douglas Gregore62e6a02009-11-11 19:13:48 +0000534 // A default template argument instantiation and substitution into
535 // template parameters with arguments for prior parameters may or may
536 // not be a SFINAE context; look further up the stack.
Douglas Gregor33834512009-06-14 07:33:30 +0000537 break;
Mike Stump11289f42009-09-09 15:08:12 +0000538
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000539 case ActiveTemplateInstantiation::ExplicitTemplateArgumentSubstitution:
540 case ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution:
541 // We're either substitution explicitly-specified template arguments
542 // or deduced template arguments, so SFINAE applies.
543 return true;
Douglas Gregor33834512009-06-14 07:33:30 +0000544 }
545 }
546
547 return false;
548}
549
Douglas Gregorfe1e1102009-02-27 19:31:52 +0000550//===----------------------------------------------------------------------===/
551// Template Instantiation for Types
552//===----------------------------------------------------------------------===/
Douglas Gregor17c0d7b2009-02-28 00:25:32 +0000553namespace {
Douglas Gregor14cf7522010-04-30 18:55:50 +0000554 class TemplateInstantiator : public TreeTransform<TemplateInstantiator> {
Douglas Gregor01afeef2009-08-28 20:31:08 +0000555 const MultiLevelTemplateArgumentList &TemplateArgs;
Douglas Gregor17c0d7b2009-02-28 00:25:32 +0000556 SourceLocation Loc;
557 DeclarationName Entity;
Douglas Gregorfe1e1102009-02-27 19:31:52 +0000558
Douglas Gregor17c0d7b2009-02-28 00:25:32 +0000559 public:
Douglas Gregorebe10102009-08-20 07:17:43 +0000560 typedef TreeTransform<TemplateInstantiator> inherited;
Mike Stump11289f42009-09-09 15:08:12 +0000561
562 TemplateInstantiator(Sema &SemaRef,
Douglas Gregor01afeef2009-08-28 20:31:08 +0000563 const MultiLevelTemplateArgumentList &TemplateArgs,
Douglas Gregord6ff3322009-08-04 16:50:30 +0000564 SourceLocation Loc,
Mike Stump11289f42009-09-09 15:08:12 +0000565 DeclarationName Entity)
566 : inherited(SemaRef), TemplateArgs(TemplateArgs), Loc(Loc),
Douglas Gregorebe10102009-08-20 07:17:43 +0000567 Entity(Entity) { }
Douglas Gregor17c0d7b2009-02-28 00:25:32 +0000568
Mike Stump11289f42009-09-09 15:08:12 +0000569 /// \brief Determine whether the given type \p T has already been
Douglas Gregord6ff3322009-08-04 16:50:30 +0000570 /// transformed.
571 ///
572 /// For the purposes of template instantiation, a type has already been
573 /// transformed if it is NULL or if it is not dependent.
Douglas Gregor5597ab42010-05-07 23:12:07 +0000574 bool AlreadyTransformed(QualType T);
Mike Stump11289f42009-09-09 15:08:12 +0000575
Douglas Gregord6ff3322009-08-04 16:50:30 +0000576 /// \brief Returns the location of the entity being instantiated, if known.
577 SourceLocation getBaseLocation() { return Loc; }
Mike Stump11289f42009-09-09 15:08:12 +0000578
Douglas Gregord6ff3322009-08-04 16:50:30 +0000579 /// \brief Returns the name of the entity being instantiated, if any.
580 DeclarationName getBaseEntity() { return Entity; }
Mike Stump11289f42009-09-09 15:08:12 +0000581
Douglas Gregoref6ab412009-10-27 06:26:26 +0000582 /// \brief Sets the "base" location and entity when that
583 /// information is known based on another transformation.
584 void setBase(SourceLocation Loc, DeclarationName Entity) {
585 this->Loc = Loc;
586 this->Entity = Entity;
587 }
588
Douglas Gregord6ff3322009-08-04 16:50:30 +0000589 /// \brief Transform the given declaration by instantiating a reference to
590 /// this declaration.
Douglas Gregora04f2ca2010-03-01 15:56:25 +0000591 Decl *TransformDecl(SourceLocation Loc, Decl *D);
Douglas Gregora16548e2009-08-11 05:31:07 +0000592
Mike Stump11289f42009-09-09 15:08:12 +0000593 /// \brief Transform the definition of the given declaration by
Douglas Gregorebe10102009-08-20 07:17:43 +0000594 /// instantiating it.
Douglas Gregor25289362010-03-01 17:25:41 +0000595 Decl *TransformDefinition(SourceLocation Loc, Decl *D);
Mike Stump11289f42009-09-09 15:08:12 +0000596
Douglas Gregora5cb6da2009-10-20 05:58:46 +0000597 /// \bried Transform the first qualifier within a scope by instantiating the
598 /// declaration.
599 NamedDecl *TransformFirstQualifierInScope(NamedDecl *D, SourceLocation Loc);
600
Douglas Gregorebe10102009-08-20 07:17:43 +0000601 /// \brief Rebuild the exception declaration and register the declaration
602 /// as an instantiated local.
Mike Stump11289f42009-09-09 15:08:12 +0000603 VarDecl *RebuildExceptionDecl(VarDecl *ExceptionDecl, QualType T,
John McCallbcd03502009-12-07 02:54:59 +0000604 TypeSourceInfo *Declarator,
Douglas Gregorebe10102009-08-20 07:17:43 +0000605 IdentifierInfo *Name,
606 SourceLocation Loc, SourceRange TypeRange);
Mike Stump11289f42009-09-09 15:08:12 +0000607
Douglas Gregorf4e837f2010-04-26 17:57:08 +0000608 /// \brief Rebuild the Objective-C exception declaration and register the
609 /// declaration as an instantiated local.
610 VarDecl *RebuildObjCExceptionDecl(VarDecl *ExceptionDecl,
611 TypeSourceInfo *TSInfo, QualType T);
612
John McCall7f41d982009-09-11 04:59:25 +0000613 /// \brief Check for tag mismatches when instantiating an
614 /// elaborated type.
Abramo Bagnara6150c882010-05-11 21:36:43 +0000615 QualType RebuildElaboratedType(ElaboratedTypeKeyword Keyword,
616 NestedNameSpecifier *NNS, QualType T);
John McCall7f41d982009-09-11 04:59:25 +0000617
John McCalldadc5752010-08-24 06:29:42 +0000618 ExprResult TransformPredefinedExpr(PredefinedExpr *E);
619 ExprResult TransformDeclRefExpr(DeclRefExpr *E);
620 ExprResult TransformCXXDefaultArgExpr(CXXDefaultArgExpr *E);
621 ExprResult TransformTemplateParmRefExpr(DeclRefExpr *E,
John McCall13481c52010-02-06 08:42:39 +0000622 NonTypeTemplateParmDecl *D);
Sebastian Redl14236c82009-11-08 13:56:19 +0000623
Douglas Gregor14cf7522010-04-30 18:55:50 +0000624 QualType TransformFunctionProtoType(TypeLocBuilder &TLB,
625 FunctionProtoTypeLoc TL,
626 QualType ObjectType);
John McCall58f10c32010-03-11 09:03:00 +0000627 ParmVarDecl *TransformFunctionTypeParam(ParmVarDecl *OldParm);
628
Mike Stump11289f42009-09-09 15:08:12 +0000629 /// \brief Transforms a template type parameter type by performing
Douglas Gregord6ff3322009-08-04 16:50:30 +0000630 /// substitution of the corresponding template type argument.
John McCall550e0c22009-10-21 00:40:46 +0000631 QualType TransformTemplateTypeParmType(TypeLocBuilder &TLB,
Douglas Gregorfe17d252010-02-16 19:09:40 +0000632 TemplateTypeParmTypeLoc TL,
633 QualType ObjectType);
Nick Lewyckyc96c37f2010-07-06 19:51:49 +0000634
John McCalldadc5752010-08-24 06:29:42 +0000635 ExprResult TransformCallExpr(CallExpr *CE) {
Nick Lewyckyc96c37f2010-07-06 19:51:49 +0000636 getSema().CallsUndergoingInstantiation.push_back(CE);
John McCalldadc5752010-08-24 06:29:42 +0000637 ExprResult Result =
Nick Lewyckyc96c37f2010-07-06 19:51:49 +0000638 TreeTransform<TemplateInstantiator>::TransformCallExpr(CE);
639 getSema().CallsUndergoingInstantiation.pop_back();
640 return move(Result);
641 }
Douglas Gregord6ff3322009-08-04 16:50:30 +0000642 };
Douglas Gregor04318252009-07-06 15:59:29 +0000643}
644
Douglas Gregor5597ab42010-05-07 23:12:07 +0000645bool TemplateInstantiator::AlreadyTransformed(QualType T) {
646 if (T.isNull())
647 return true;
648
Douglas Gregor5a5073e2010-05-24 17:22:01 +0000649 if (T->isDependentType() || T->isVariablyModifiedType())
Douglas Gregor5597ab42010-05-07 23:12:07 +0000650 return false;
651
652 getSema().MarkDeclarationsReferencedInType(Loc, T);
653 return true;
654}
655
Douglas Gregora04f2ca2010-03-01 15:56:25 +0000656Decl *TemplateInstantiator::TransformDecl(SourceLocation Loc, Decl *D) {
Douglas Gregor2b6ca462009-09-03 21:38:09 +0000657 if (!D)
658 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000659
Douglas Gregor2b6ca462009-09-03 21:38:09 +0000660 if (TemplateTemplateParmDecl *TTP = dyn_cast<TemplateTemplateParmDecl>(D)) {
Douglas Gregor01afeef2009-08-28 20:31:08 +0000661 if (TTP->getDepth() < TemplateArgs.getNumLevels()) {
Douglas Gregorb93971082010-02-05 19:54:12 +0000662 // If the corresponding template argument is NULL or non-existent, it's
663 // because we are performing instantiation from explicitly-specified
664 // template arguments in a function template, but there were some
665 // arguments left unspecified.
666 if (!TemplateArgs.hasTemplateArgument(TTP->getDepth(),
667 TTP->getPosition()))
668 return D;
669
Douglas Gregor9167f8b2009-11-11 01:00:40 +0000670 TemplateName Template
671 = TemplateArgs(TTP->getDepth(), TTP->getPosition()).getAsTemplate();
672 assert(!Template.isNull() && Template.getAsTemplateDecl() &&
Douglas Gregor01afeef2009-08-28 20:31:08 +0000673 "Wrong kind of template template argument");
Douglas Gregor9167f8b2009-11-11 01:00:40 +0000674 return Template.getAsTemplateDecl();
Douglas Gregor01afeef2009-08-28 20:31:08 +0000675 }
Mike Stump11289f42009-09-09 15:08:12 +0000676
Douglas Gregor9167f8b2009-11-11 01:00:40 +0000677 // Fall through to find the instantiated declaration for this template
678 // template parameter.
Douglas Gregor71dc5092009-08-06 06:41:21 +0000679 }
Mike Stump11289f42009-09-09 15:08:12 +0000680
Douglas Gregora04f2ca2010-03-01 15:56:25 +0000681 return SemaRef.FindInstantiatedDecl(Loc, cast<NamedDecl>(D), TemplateArgs);
Douglas Gregord6ff3322009-08-04 16:50:30 +0000682}
683
Douglas Gregor25289362010-03-01 17:25:41 +0000684Decl *TemplateInstantiator::TransformDefinition(SourceLocation Loc, Decl *D) {
John McCall76d824f2009-08-25 22:02:44 +0000685 Decl *Inst = getSema().SubstDecl(D, getSema().CurContext, TemplateArgs);
Douglas Gregorebe10102009-08-20 07:17:43 +0000686 if (!Inst)
687 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000688
Douglas Gregorebe10102009-08-20 07:17:43 +0000689 getSema().CurrentInstantiationScope->InstantiatedLocal(D, Inst);
690 return Inst;
691}
692
Douglas Gregora5cb6da2009-10-20 05:58:46 +0000693NamedDecl *
694TemplateInstantiator::TransformFirstQualifierInScope(NamedDecl *D,
695 SourceLocation Loc) {
696 // If the first part of the nested-name-specifier was a template type
697 // parameter, instantiate that type parameter down to a tag type.
698 if (TemplateTypeParmDecl *TTPD = dyn_cast_or_null<TemplateTypeParmDecl>(D)) {
699 const TemplateTypeParmType *TTP
700 = cast<TemplateTypeParmType>(getSema().Context.getTypeDeclType(TTPD));
701 if (TTP->getDepth() < TemplateArgs.getNumLevels()) {
702 QualType T = TemplateArgs(TTP->getDepth(), TTP->getIndex()).getAsType();
703 if (T.isNull())
Douglas Gregora04f2ca2010-03-01 15:56:25 +0000704 return cast_or_null<NamedDecl>(TransformDecl(Loc, D));
Douglas Gregora5cb6da2009-10-20 05:58:46 +0000705
706 if (const TagType *Tag = T->getAs<TagType>())
707 return Tag->getDecl();
708
709 // The resulting type is not a tag; complain.
710 getSema().Diag(Loc, diag::err_nested_name_spec_non_tag) << T;
711 return 0;
712 }
713 }
714
Douglas Gregora04f2ca2010-03-01 15:56:25 +0000715 return cast_or_null<NamedDecl>(TransformDecl(Loc, D));
Douglas Gregora5cb6da2009-10-20 05:58:46 +0000716}
717
Douglas Gregorebe10102009-08-20 07:17:43 +0000718VarDecl *
719TemplateInstantiator::RebuildExceptionDecl(VarDecl *ExceptionDecl,
Mike Stump11289f42009-09-09 15:08:12 +0000720 QualType T,
John McCallbcd03502009-12-07 02:54:59 +0000721 TypeSourceInfo *Declarator,
Douglas Gregorebe10102009-08-20 07:17:43 +0000722 IdentifierInfo *Name,
Mike Stump11289f42009-09-09 15:08:12 +0000723 SourceLocation Loc,
Douglas Gregorebe10102009-08-20 07:17:43 +0000724 SourceRange TypeRange) {
725 VarDecl *Var = inherited::RebuildExceptionDecl(ExceptionDecl, T, Declarator,
726 Name, Loc, TypeRange);
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)
822 return SemaRef.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.
869 Sema::LocalInstantiationScope Scope(SemaRef, /*CombineWithOuterScope=*/true);
870 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);
1065 } else if (Expr *Arg = OldParm->getDefaultArg())
1066 NewParm->setUninstantiatedDefaultArg(Arg);
1067
1068 NewParm->setHasInheritedDefaultArg(OldParm->hasInheritedDefaultArg());
1069
1070 CurrentInstantiationScope->InstantiatedLocal(OldParm, NewParm);
Argyrios Kyrtzidis3816ed42010-07-19 10:14:41 +00001071 // FIXME: OldParm may come from a FunctionProtoType, in which case CurContext
1072 // can be anything, is this right ?
Fariborz Jahanian714447b2010-07-13 21:05:02 +00001073 NewParm->setDeclContext(CurContext);
Fariborz Jahaniana6c7efe2010-07-13 20:05:58 +00001074
Douglas Gregor940bca72010-04-12 07:48:19 +00001075 return NewParm;
1076}
1077
John McCall76d824f2009-08-25 22:02:44 +00001078/// \brief Perform substitution on the base class specifiers of the
1079/// given class template specialization.
Douglas Gregor463421d2009-03-03 04:44:36 +00001080///
1081/// Produces a diagnostic and returns true on error, returns false and
1082/// attaches the instantiated base classes to the class template
1083/// specialization if successful.
Mike Stump11289f42009-09-09 15:08:12 +00001084bool
John McCall76d824f2009-08-25 22:02:44 +00001085Sema::SubstBaseSpecifiers(CXXRecordDecl *Instantiation,
1086 CXXRecordDecl *Pattern,
Douglas Gregor01afeef2009-08-28 20:31:08 +00001087 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor463421d2009-03-03 04:44:36 +00001088 bool Invalid = false;
Douglas Gregor6181ded2009-05-29 18:27:38 +00001089 llvm::SmallVector<CXXBaseSpecifier*, 4> InstantiatedBases;
Mike Stump11289f42009-09-09 15:08:12 +00001090 for (ClassTemplateSpecializationDecl::base_class_iterator
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001091 Base = Pattern->bases_begin(), BaseEnd = Pattern->bases_end();
Douglas Gregor2a72edd2009-03-10 18:52:44 +00001092 Base != BaseEnd; ++Base) {
Douglas Gregor463421d2009-03-03 04:44:36 +00001093 if (!Base->getType()->isDependentType()) {
Anders Carlssonae3c5cf2009-12-03 17:49:57 +00001094 const CXXRecordDecl *BaseDecl =
1095 cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
1096
1097 // Make sure to set the attributes from the base.
1098 SetClassDeclAttributesFromBase(Instantiation, BaseDecl,
1099 Base->isVirtual());
1100
Fariborz Jahanian5c14ec32009-07-22 17:41:53 +00001101 InstantiatedBases.push_back(new (Context) CXXBaseSpecifier(*Base));
Douglas Gregor463421d2009-03-03 04:44:36 +00001102 continue;
1103 }
1104
Nick Lewycky19b9f952010-07-26 16:56:01 +00001105 TypeSourceInfo *BaseTypeLoc = SubstType(Base->getTypeSourceInfo(),
1106 TemplateArgs,
1107 Base->getSourceRange().getBegin(),
1108 DeclarationName());
1109 if (!BaseTypeLoc) {
Douglas Gregor463421d2009-03-03 04:44:36 +00001110 Invalid = true;
1111 continue;
1112 }
1113
1114 if (CXXBaseSpecifier *InstantiatedBase
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001115 = CheckBaseSpecifier(Instantiation,
Douglas Gregor463421d2009-03-03 04:44:36 +00001116 Base->getSourceRange(),
1117 Base->isVirtual(),
1118 Base->getAccessSpecifierAsWritten(),
Nick Lewycky19b9f952010-07-26 16:56:01 +00001119 BaseTypeLoc))
Douglas Gregor463421d2009-03-03 04:44:36 +00001120 InstantiatedBases.push_back(InstantiatedBase);
1121 else
1122 Invalid = true;
1123 }
1124
Douglas Gregor2a72edd2009-03-10 18:52:44 +00001125 if (!Invalid &&
Jay Foad7d0479f2009-05-21 09:52:38 +00001126 AttachBaseSpecifiers(Instantiation, InstantiatedBases.data(),
Douglas Gregor463421d2009-03-03 04:44:36 +00001127 InstantiatedBases.size()))
1128 Invalid = true;
1129
1130 return Invalid;
1131}
1132
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001133/// \brief Instantiate the definition of a class from a given pattern.
1134///
1135/// \param PointOfInstantiation The point of instantiation within the
1136/// source code.
1137///
1138/// \param Instantiation is the declaration whose definition is being
1139/// instantiated. This will be either a class template specialization
1140/// or a member class of a class template specialization.
1141///
1142/// \param Pattern is the pattern from which the instantiation
1143/// occurs. This will be either the declaration of a class template or
1144/// the declaration of a member class of a class template.
1145///
1146/// \param TemplateArgs The template arguments to be substituted into
1147/// the pattern.
1148///
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001149/// \param TSK the kind of implicit or explicit instantiation to perform.
Douglas Gregor8a2e6012009-08-24 15:23:48 +00001150///
1151/// \param Complain whether to complain if the class cannot be instantiated due
1152/// to the lack of a definition.
1153///
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001154/// \returns true if an error occurred, false otherwise.
1155bool
1156Sema::InstantiateClass(SourceLocation PointOfInstantiation,
1157 CXXRecordDecl *Instantiation, CXXRecordDecl *Pattern,
Douglas Gregor01afeef2009-08-28 20:31:08 +00001158 const MultiLevelTemplateArgumentList &TemplateArgs,
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001159 TemplateSpecializationKind TSK,
Douglas Gregor8a2e6012009-08-24 15:23:48 +00001160 bool Complain) {
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001161 bool Invalid = false;
John McCall87a44eb2009-08-20 01:44:21 +00001162
Mike Stump11289f42009-09-09 15:08:12 +00001163 CXXRecordDecl *PatternDef
Douglas Gregor0a5a2212010-02-11 01:04:33 +00001164 = cast_or_null<CXXRecordDecl>(Pattern->getDefinition());
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001165 if (!PatternDef) {
Douglas Gregor8a2e6012009-08-24 15:23:48 +00001166 if (!Complain) {
1167 // Say nothing
1168 } else if (Pattern == Instantiation->getInstantiatedFromMemberClass()) {
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001169 Diag(PointOfInstantiation,
1170 diag::err_implicit_instantiate_member_undefined)
1171 << Context.getTypeDeclType(Instantiation);
1172 Diag(Pattern->getLocation(), diag::note_member_of_template_here);
1173 } else {
Douglas Gregora1f49972009-05-13 00:25:59 +00001174 Diag(PointOfInstantiation, diag::err_template_instantiate_undefined)
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001175 << (TSK != TSK_ImplicitInstantiation)
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001176 << Context.getTypeDeclType(Instantiation);
1177 Diag(Pattern->getLocation(), diag::note_template_decl_here);
1178 }
1179 return true;
1180 }
1181 Pattern = PatternDef;
1182
Douglas Gregord6ba93d2009-10-15 15:54:05 +00001183 // \brief Record the point of instantiation.
1184 if (MemberSpecializationInfo *MSInfo
1185 = Instantiation->getMemberSpecializationInfo()) {
1186 MSInfo->setTemplateSpecializationKind(TSK);
1187 MSInfo->setPointOfInstantiation(PointOfInstantiation);
Douglas Gregoref6ab412009-10-27 06:26:26 +00001188 } else if (ClassTemplateSpecializationDecl *Spec
1189 = dyn_cast<ClassTemplateSpecializationDecl>(Instantiation)) {
1190 Spec->setTemplateSpecializationKind(TSK);
1191 Spec->setPointOfInstantiation(PointOfInstantiation);
Douglas Gregord6ba93d2009-10-15 15:54:05 +00001192 }
1193
Douglas Gregorf3430ae2009-03-25 21:23:52 +00001194 InstantiatingTemplate Inst(*this, PointOfInstantiation, Instantiation);
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001195 if (Inst)
1196 return true;
1197
1198 // Enter the scope of this instantiation. We don't use
1199 // PushDeclContext because we don't have a scope.
John McCall80e58cd2010-04-29 00:35:03 +00001200 ContextRAII SavedContext(*this, Instantiation);
Douglas Gregor17158422010-05-12 17:27:19 +00001201 EnterExpressionEvaluationContext EvalContext(*this,
1202 Action::PotentiallyEvaluated);
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001203
Douglas Gregor51121572010-03-24 01:33:17 +00001204 // If this is an instantiation of a local class, merge this local
1205 // instantiation scope with the enclosing scope. Otherwise, every
1206 // instantiation of a class has its own local instantiation scope.
1207 bool MergeWithParentScope = !Instantiation->isDefinedOutsideFunctionOrMethod();
1208 Sema::LocalInstantiationScope Scope(*this, MergeWithParentScope);
1209
John McCall6602bb12010-08-01 02:01:53 +00001210 // Pull attributes from the pattern onto the instantiation.
1211 InstantiateAttrs(TemplateArgs, Pattern, Instantiation);
1212
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001213 // Start the definition of this instantiation.
1214 Instantiation->startDefinition();
Douglas Gregore9029562010-05-06 00:28:52 +00001215
1216 Instantiation->setTagKind(Pattern->getTagKind());
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001217
John McCall76d824f2009-08-25 22:02:44 +00001218 // Do substitution on the base class specifiers.
1219 if (SubstBaseSpecifiers(Instantiation, Pattern, TemplateArgs))
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001220 Invalid = true;
1221
John McCall48871652010-08-21 09:40:31 +00001222 llvm::SmallVector<Decl*, 4> Fields;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001223 for (RecordDecl::decl_iterator Member = Pattern->decls_begin(),
Mike Stump11289f42009-09-09 15:08:12 +00001224 MemberEnd = Pattern->decls_end();
Douglas Gregorbcced4e2009-04-09 21:40:53 +00001225 Member != MemberEnd; ++Member) {
John McCall76d824f2009-08-25 22:02:44 +00001226 Decl *NewMember = SubstDecl(*Member, Instantiation, TemplateArgs);
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001227 if (NewMember) {
Eli Friedmand0e8de22009-12-07 00:22:08 +00001228 if (FieldDecl *Field = dyn_cast<FieldDecl>(NewMember))
John McCall48871652010-08-21 09:40:31 +00001229 Fields.push_back(Field);
Eli Friedmand0e8de22009-12-07 00:22:08 +00001230 else if (NewMember->isInvalidDecl())
1231 Invalid = true;
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001232 } else {
1233 // FIXME: Eventually, a NULL return will mean that one of the
Mike Stump87c57ac2009-05-16 07:39:55 +00001234 // instantiations was a semantic disaster, and we'll want to set Invalid =
1235 // true. For now, we expect to skip some members that we can't yet handle.
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001236 }
1237 }
1238
1239 // Finish checking fields.
John McCall48871652010-08-21 09:40:31 +00001240 ActOnFields(0, Instantiation->getLocation(), Instantiation,
Jay Foad7d0479f2009-05-21 09:52:38 +00001241 Fields.data(), Fields.size(), SourceLocation(), SourceLocation(),
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001242 0);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001243 CheckCompletedCXXClass(Instantiation);
Douglas Gregor3c74d412009-10-14 20:14:33 +00001244 if (Instantiation->isInvalidDecl())
1245 Invalid = true;
1246
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001247 // Exit the scope of this instantiation.
John McCall80e58cd2010-04-29 00:35:03 +00001248 SavedContext.pop();
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001249
Douglas Gregor88d292c2010-05-13 16:44:06 +00001250 if (!Invalid) {
Douglas Gregor28ad4b52009-05-26 20:50:29 +00001251 Consumer.HandleTagDeclDefinition(Instantiation);
1252
Douglas Gregor88d292c2010-05-13 16:44:06 +00001253 // Always emit the vtable for an explicit instantiation definition
1254 // of a polymorphic class template specialization.
1255 if (TSK == TSK_ExplicitInstantiationDefinition)
1256 MarkVTableUsed(PointOfInstantiation, Instantiation, true);
1257 }
1258
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001259 return Invalid;
1260}
1261
Mike Stump11289f42009-09-09 15:08:12 +00001262bool
Douglas Gregor463421d2009-03-03 04:44:36 +00001263Sema::InstantiateClassTemplateSpecialization(
Douglas Gregoref6ab412009-10-27 06:26:26 +00001264 SourceLocation PointOfInstantiation,
Douglas Gregor463421d2009-03-03 04:44:36 +00001265 ClassTemplateSpecializationDecl *ClassTemplateSpec,
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001266 TemplateSpecializationKind TSK,
Douglas Gregor8a2e6012009-08-24 15:23:48 +00001267 bool Complain) {
Douglas Gregor463421d2009-03-03 04:44:36 +00001268 // Perform the actual instantiation on the canonical declaration.
1269 ClassTemplateSpec = cast<ClassTemplateSpecializationDecl>(
Argyrios Kyrtzidis6b7e3762009-07-18 00:34:25 +00001270 ClassTemplateSpec->getCanonicalDecl());
Douglas Gregor463421d2009-03-03 04:44:36 +00001271
Douglas Gregor4aa04b12009-09-11 21:19:12 +00001272 // Check whether we have already instantiated or specialized this class
1273 // template specialization.
1274 if (ClassTemplateSpec->getSpecializationKind() != TSK_Undeclared) {
1275 if (ClassTemplateSpec->getSpecializationKind() ==
1276 TSK_ExplicitInstantiationDeclaration &&
1277 TSK == TSK_ExplicitInstantiationDefinition) {
1278 // An explicit instantiation definition follows an explicit instantiation
1279 // declaration (C++0x [temp.explicit]p10); go ahead and perform the
1280 // explicit instantiation.
1281 ClassTemplateSpec->setSpecializationKind(TSK);
Douglas Gregor88d292c2010-05-13 16:44:06 +00001282
1283 // If this is an explicit instantiation definition, mark the
1284 // vtable as used.
1285 if (TSK == TSK_ExplicitInstantiationDefinition)
1286 MarkVTableUsed(PointOfInstantiation, ClassTemplateSpec, true);
1287
Douglas Gregor4aa04b12009-09-11 21:19:12 +00001288 return false;
1289 }
1290
1291 // We can only instantiate something that hasn't already been
1292 // instantiated or specialized. Fail without any diagnostics: our
1293 // caller will provide an error message.
Douglas Gregor463421d2009-03-03 04:44:36 +00001294 return true;
Douglas Gregor4aa04b12009-09-11 21:19:12 +00001295 }
Douglas Gregor463421d2009-03-03 04:44:36 +00001296
Douglas Gregor00a511f2009-09-15 16:51:42 +00001297 if (ClassTemplateSpec->isInvalidDecl())
1298 return true;
1299
Douglas Gregor463421d2009-03-03 04:44:36 +00001300 ClassTemplateDecl *Template = ClassTemplateSpec->getSpecializedTemplate();
Douglas Gregor01afeef2009-08-28 20:31:08 +00001301 CXXRecordDecl *Pattern = 0;
Douglas Gregor2373c592009-05-31 09:31:02 +00001302
Douglas Gregor170bc422009-06-12 22:31:52 +00001303 // C++ [temp.class.spec.match]p1:
1304 // When a class template is used in a context that requires an
1305 // instantiation of the class, it is necessary to determine
1306 // whether the instantiation is to be generated using the primary
1307 // template or one of the partial specializations. This is done by
1308 // matching the template arguments of the class template
1309 // specialization with the template argument lists of the partial
1310 // specializations.
Douglas Gregorb7ae10f2009-06-05 00:53:49 +00001311 typedef std::pair<ClassTemplatePartialSpecializationDecl *,
1312 TemplateArgumentList *> MatchResult;
1313 llvm::SmallVector<MatchResult, 4> Matched;
Douglas Gregor407e9612010-04-30 05:56:50 +00001314 llvm::SmallVector<ClassTemplatePartialSpecializationDecl *, 4> PartialSpecs;
1315 Template->getPartialSpecializations(PartialSpecs);
1316 for (unsigned I = 0, N = PartialSpecs.size(); I != N; ++I) {
1317 ClassTemplatePartialSpecializationDecl *Partial = PartialSpecs[I];
John McCallbc077cf2010-02-08 23:07:23 +00001318 TemplateDeductionInfo Info(Context, PointOfInstantiation);
Douglas Gregor181aa4a2009-06-12 18:26:56 +00001319 if (TemplateDeductionResult Result
Douglas Gregor407e9612010-04-30 05:56:50 +00001320 = DeduceTemplateArguments(Partial,
Douglas Gregor181aa4a2009-06-12 18:26:56 +00001321 ClassTemplateSpec->getTemplateArgs(),
1322 Info)) {
1323 // FIXME: Store the failed-deduction information for use in
1324 // diagnostics, later.
1325 (void)Result;
1326 } else {
Douglas Gregor407e9612010-04-30 05:56:50 +00001327 Matched.push_back(std::make_pair(Partial, Info.take()));
Douglas Gregor181aa4a2009-06-12 18:26:56 +00001328 }
Douglas Gregor2373c592009-05-31 09:31:02 +00001329 }
1330
Douglas Gregor21610382009-10-29 00:04:11 +00001331 if (Matched.size() >= 1) {
Douglas Gregorbe999392009-09-15 16:23:51 +00001332 llvm::SmallVector<MatchResult, 4>::iterator Best = Matched.begin();
Douglas Gregor21610382009-10-29 00:04:11 +00001333 if (Matched.size() == 1) {
1334 // -- If exactly one matching specialization is found, the
1335 // instantiation is generated from that specialization.
1336 // We don't need to do anything for this.
1337 } else {
1338 // -- If more than one matching specialization is found, the
1339 // partial order rules (14.5.4.2) are used to determine
1340 // whether one of the specializations is more specialized
1341 // than the others. If none of the specializations is more
1342 // specialized than all of the other matching
1343 // specializations, then the use of the class template is
1344 // ambiguous and the program is ill-formed.
1345 for (llvm::SmallVector<MatchResult, 4>::iterator P = Best + 1,
1346 PEnd = Matched.end();
1347 P != PEnd; ++P) {
John McCallbc077cf2010-02-08 23:07:23 +00001348 if (getMoreSpecializedPartialSpecialization(P->first, Best->first,
1349 PointOfInstantiation)
Douglas Gregor21610382009-10-29 00:04:11 +00001350 == P->first)
1351 Best = P;
Douglas Gregorbe999392009-09-15 16:23:51 +00001352 }
Douglas Gregorbe999392009-09-15 16:23:51 +00001353
Douglas Gregor21610382009-10-29 00:04:11 +00001354 // Determine if the best partial specialization is more specialized than
1355 // the others.
1356 bool Ambiguous = false;
Douglas Gregorbe999392009-09-15 16:23:51 +00001357 for (llvm::SmallVector<MatchResult, 4>::iterator P = Matched.begin(),
1358 PEnd = Matched.end();
Douglas Gregor21610382009-10-29 00:04:11 +00001359 P != PEnd; ++P) {
1360 if (P != Best &&
John McCallbc077cf2010-02-08 23:07:23 +00001361 getMoreSpecializedPartialSpecialization(P->first, Best->first,
1362 PointOfInstantiation)
Douglas Gregor21610382009-10-29 00:04:11 +00001363 != Best->first) {
1364 Ambiguous = true;
1365 break;
1366 }
1367 }
1368
1369 if (Ambiguous) {
1370 // Partial ordering did not produce a clear winner. Complain.
1371 ClassTemplateSpec->setInvalidDecl();
1372 Diag(PointOfInstantiation, diag::err_partial_spec_ordering_ambiguous)
1373 << ClassTemplateSpec;
1374
1375 // Print the matching partial specializations.
1376 for (llvm::SmallVector<MatchResult, 4>::iterator P = Matched.begin(),
1377 PEnd = Matched.end();
1378 P != PEnd; ++P)
1379 Diag(P->first->getLocation(), diag::note_partial_spec_match)
1380 << getTemplateArgumentBindingsText(P->first->getTemplateParameters(),
1381 *P->second);
Douglas Gregor01afeef2009-08-28 20:31:08 +00001382
Douglas Gregor21610382009-10-29 00:04:11 +00001383 return true;
1384 }
Douglas Gregorbe999392009-09-15 16:23:51 +00001385 }
1386
1387 // Instantiate using the best class template partial specialization.
Douglas Gregor21610382009-10-29 00:04:11 +00001388 ClassTemplatePartialSpecializationDecl *OrigPartialSpec = Best->first;
1389 while (OrigPartialSpec->getInstantiatedFromMember()) {
1390 // If we've found an explicit specialization of this class template,
1391 // stop here and use that as the pattern.
1392 if (OrigPartialSpec->isMemberSpecialization())
1393 break;
1394
1395 OrigPartialSpec = OrigPartialSpec->getInstantiatedFromMember();
1396 }
1397
1398 Pattern = OrigPartialSpec;
Douglas Gregorbe999392009-09-15 16:23:51 +00001399 ClassTemplateSpec->setInstantiationOf(Best->first, Best->second);
Douglas Gregor170bc422009-06-12 22:31:52 +00001400 } else {
1401 // -- If no matches are found, the instantiation is generated
1402 // from the primary template.
Douglas Gregor01afeef2009-08-28 20:31:08 +00001403 ClassTemplateDecl *OrigTemplate = Template;
Douglas Gregorcf915552009-10-13 16:30:37 +00001404 while (OrigTemplate->getInstantiatedFromMemberTemplate()) {
1405 // If we've found an explicit specialization of this class template,
1406 // stop here and use that as the pattern.
1407 if (OrigTemplate->isMemberSpecialization())
1408 break;
1409
Douglas Gregor01afeef2009-08-28 20:31:08 +00001410 OrigTemplate = OrigTemplate->getInstantiatedFromMemberTemplate();
Douglas Gregorcf915552009-10-13 16:30:37 +00001411 }
1412
Douglas Gregor01afeef2009-08-28 20:31:08 +00001413 Pattern = OrigTemplate->getTemplatedDecl();
Douglas Gregor2373c592009-05-31 09:31:02 +00001414 }
Douglas Gregor463421d2009-03-03 04:44:36 +00001415
Douglas Gregoref6ab412009-10-27 06:26:26 +00001416 bool Result = InstantiateClass(PointOfInstantiation, ClassTemplateSpec,
1417 Pattern,
1418 getTemplateInstantiationArgs(ClassTemplateSpec),
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001419 TSK,
Douglas Gregor8a2e6012009-08-24 15:23:48 +00001420 Complain);
Mike Stump11289f42009-09-09 15:08:12 +00001421
Douglas Gregorb7ae10f2009-06-05 00:53:49 +00001422 return Result;
Douglas Gregor463421d2009-03-03 04:44:36 +00001423}
Douglas Gregor90a1a652009-03-19 17:26:29 +00001424
John McCall76d824f2009-08-25 22:02:44 +00001425/// \brief Instantiates the definitions of all of the member
1426/// of the given class, which is an instantiation of a class template
1427/// or a member class of a template.
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00001428void
1429Sema::InstantiateClassMembers(SourceLocation PointOfInstantiation,
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001430 CXXRecordDecl *Instantiation,
1431 const MultiLevelTemplateArgumentList &TemplateArgs,
1432 TemplateSpecializationKind TSK) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001433 for (DeclContext::decl_iterator D = Instantiation->decls_begin(),
1434 DEnd = Instantiation->decls_end();
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00001435 D != DEnd; ++D) {
Douglas Gregor1d957a32009-10-27 18:42:08 +00001436 bool SuppressNew = false;
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00001437 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(*D)) {
Douglas Gregor1d957a32009-10-27 18:42:08 +00001438 if (FunctionDecl *Pattern
1439 = Function->getInstantiatedFromMemberFunction()) {
1440 MemberSpecializationInfo *MSInfo
1441 = Function->getMemberSpecializationInfo();
1442 assert(MSInfo && "No member specialization information?");
Douglas Gregor06aa50412010-04-09 21:02:29 +00001443 if (MSInfo->getTemplateSpecializationKind()
1444 == TSK_ExplicitSpecialization)
1445 continue;
1446
Douglas Gregor1d957a32009-10-27 18:42:08 +00001447 if (CheckSpecializationInstantiationRedecl(PointOfInstantiation, TSK,
1448 Function,
1449 MSInfo->getTemplateSpecializationKind(),
1450 MSInfo->getPointOfInstantiation(),
1451 SuppressNew) ||
1452 SuppressNew)
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001453 continue;
1454
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001455 if (Function->hasBody())
Douglas Gregor1d957a32009-10-27 18:42:08 +00001456 continue;
1457
1458 if (TSK == TSK_ExplicitInstantiationDefinition) {
1459 // C++0x [temp.explicit]p8:
1460 // An explicit instantiation definition that names a class template
1461 // specialization explicitly instantiates the class template
1462 // specialization and is only an explicit instantiation definition
1463 // of members whose definition is visible at the point of
1464 // instantiation.
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001465 if (!Pattern->hasBody())
Douglas Gregor1d957a32009-10-27 18:42:08 +00001466 continue;
1467
1468 Function->setTemplateSpecializationKind(TSK, PointOfInstantiation);
1469
1470 InstantiateFunctionDefinition(PointOfInstantiation, Function);
1471 } else {
1472 Function->setTemplateSpecializationKind(TSK, PointOfInstantiation);
1473 }
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001474 }
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00001475 } else if (VarDecl *Var = dyn_cast<VarDecl>(*D)) {
Douglas Gregor86d142a2009-10-08 07:24:58 +00001476 if (Var->isStaticDataMember()) {
Douglas Gregor1d957a32009-10-27 18:42:08 +00001477 MemberSpecializationInfo *MSInfo = Var->getMemberSpecializationInfo();
1478 assert(MSInfo && "No member specialization information?");
Douglas Gregor06aa50412010-04-09 21:02:29 +00001479 if (MSInfo->getTemplateSpecializationKind()
1480 == TSK_ExplicitSpecialization)
1481 continue;
1482
Douglas Gregor1d957a32009-10-27 18:42:08 +00001483 if (CheckSpecializationInstantiationRedecl(PointOfInstantiation, TSK,
1484 Var,
1485 MSInfo->getTemplateSpecializationKind(),
1486 MSInfo->getPointOfInstantiation(),
1487 SuppressNew) ||
1488 SuppressNew)
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001489 continue;
1490
Douglas Gregor1d957a32009-10-27 18:42:08 +00001491 if (TSK == TSK_ExplicitInstantiationDefinition) {
1492 // C++0x [temp.explicit]p8:
1493 // An explicit instantiation definition that names a class template
1494 // specialization explicitly instantiates the class template
1495 // specialization and is only an explicit instantiation definition
1496 // of members whose definition is visible at the point of
1497 // instantiation.
1498 if (!Var->getInstantiatedFromStaticDataMember()
1499 ->getOutOfLineDefinition())
1500 continue;
1501
1502 Var->setTemplateSpecializationKind(TSK, PointOfInstantiation);
Douglas Gregor86d142a2009-10-08 07:24:58 +00001503 InstantiateStaticDataMemberDefinition(PointOfInstantiation, Var);
Douglas Gregor1d957a32009-10-27 18:42:08 +00001504 } else {
1505 Var->setTemplateSpecializationKind(TSK, PointOfInstantiation);
1506 }
1507 }
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00001508 } else if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(*D)) {
Douglas Gregor1da22252010-04-18 18:11:38 +00001509 // Always skip the injected-class-name, along with any
1510 // redeclarations of nested classes, since both would cause us
1511 // to try to instantiate the members of a class twice.
1512 if (Record->isInjectedClassName() || Record->getPreviousDeclaration())
Douglas Gregord801b062009-10-07 23:56:10 +00001513 continue;
1514
Douglas Gregor1d957a32009-10-27 18:42:08 +00001515 MemberSpecializationInfo *MSInfo = Record->getMemberSpecializationInfo();
1516 assert(MSInfo && "No member specialization information?");
Douglas Gregor06aa50412010-04-09 21:02:29 +00001517
1518 if (MSInfo->getTemplateSpecializationKind()
1519 == TSK_ExplicitSpecialization)
1520 continue;
1521
Douglas Gregor1d957a32009-10-27 18:42:08 +00001522 if (CheckSpecializationInstantiationRedecl(PointOfInstantiation, TSK,
1523 Record,
1524 MSInfo->getTemplateSpecializationKind(),
1525 MSInfo->getPointOfInstantiation(),
1526 SuppressNew) ||
1527 SuppressNew)
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001528 continue;
1529
Douglas Gregor1d957a32009-10-27 18:42:08 +00001530 CXXRecordDecl *Pattern = Record->getInstantiatedFromMemberClass();
1531 assert(Pattern && "Missing instantiated-from-template information");
1532
Douglas Gregor0a5a2212010-02-11 01:04:33 +00001533 if (!Record->getDefinition()) {
1534 if (!Pattern->getDefinition()) {
Douglas Gregor1d957a32009-10-27 18:42:08 +00001535 // C++0x [temp.explicit]p8:
1536 // An explicit instantiation definition that names a class template
1537 // specialization explicitly instantiates the class template
1538 // specialization and is only an explicit instantiation definition
1539 // of members whose definition is visible at the point of
1540 // instantiation.
1541 if (TSK == TSK_ExplicitInstantiationDeclaration) {
1542 MSInfo->setTemplateSpecializationKind(TSK);
1543 MSInfo->setPointOfInstantiation(PointOfInstantiation);
1544 }
1545
1546 continue;
1547 }
1548
1549 InstantiateClass(PointOfInstantiation, Record, Pattern,
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001550 TemplateArgs,
1551 TSK);
Douglas Gregor1d957a32009-10-27 18:42:08 +00001552 }
Douglas Gregorc093c1d2009-10-08 01:19:17 +00001553
Douglas Gregor0a5a2212010-02-11 01:04:33 +00001554 Pattern = cast_or_null<CXXRecordDecl>(Record->getDefinition());
Douglas Gregor1d957a32009-10-27 18:42:08 +00001555 if (Pattern)
1556 InstantiateClassMembers(PointOfInstantiation, Pattern, TemplateArgs,
1557 TSK);
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00001558 }
1559 }
1560}
1561
1562/// \brief Instantiate the definitions of all of the members of the
1563/// given class template specialization, which was named as part of an
1564/// explicit instantiation.
Mike Stump11289f42009-09-09 15:08:12 +00001565void
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001566Sema::InstantiateClassTemplateSpecializationMembers(
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00001567 SourceLocation PointOfInstantiation,
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001568 ClassTemplateSpecializationDecl *ClassTemplateSpec,
1569 TemplateSpecializationKind TSK) {
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00001570 // C++0x [temp.explicit]p7:
1571 // An explicit instantiation that names a class template
1572 // specialization is an explicit instantion of the same kind
1573 // (declaration or definition) of each of its members (not
1574 // including members inherited from base classes) that has not
1575 // been previously explicitly specialized in the translation unit
1576 // containing the explicit instantiation, except as described
1577 // below.
1578 InstantiateClassMembers(PointOfInstantiation, ClassTemplateSpec,
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001579 getTemplateInstantiationArgs(ClassTemplateSpec),
1580 TSK);
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00001581}
1582
John McCalldadc5752010-08-24 06:29:42 +00001583StmtResult
Douglas Gregor01afeef2009-08-28 20:31:08 +00001584Sema::SubstStmt(Stmt *S, const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregorebe10102009-08-20 07:17:43 +00001585 if (!S)
1586 return Owned(S);
1587
1588 TemplateInstantiator Instantiator(*this, TemplateArgs,
1589 SourceLocation(),
1590 DeclarationName());
1591 return Instantiator.TransformStmt(S);
1592}
1593
John McCalldadc5752010-08-24 06:29:42 +00001594ExprResult
Douglas Gregor01afeef2009-08-28 20:31:08 +00001595Sema::SubstExpr(Expr *E, const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregora16548e2009-08-11 05:31:07 +00001596 if (!E)
1597 return Owned(E);
Mike Stump11289f42009-09-09 15:08:12 +00001598
Douglas Gregora16548e2009-08-11 05:31:07 +00001599 TemplateInstantiator Instantiator(*this, TemplateArgs,
1600 SourceLocation(),
1601 DeclarationName());
1602 return Instantiator.TransformExpr(E);
1603}
1604
John McCall76d824f2009-08-25 22:02:44 +00001605/// \brief Do template substitution on a nested-name-specifier.
Douglas Gregorf21eb492009-03-26 23:50:42 +00001606NestedNameSpecifier *
John McCall76d824f2009-08-25 22:02:44 +00001607Sema::SubstNestedNameSpecifier(NestedNameSpecifier *NNS,
Douglas Gregor01afeef2009-08-28 20:31:08 +00001608 SourceRange Range,
1609 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor1135c352009-08-06 05:28:30 +00001610 TemplateInstantiator Instantiator(*this, TemplateArgs, Range.getBegin(),
1611 DeclarationName());
Douglas Gregorcd3f49f2010-02-25 04:46:04 +00001612 return Instantiator.TransformNestedNameSpecifier(NNS, Range);
Douglas Gregor90a1a652009-03-19 17:26:29 +00001613}
Douglas Gregoraa594892009-03-31 18:38:02 +00001614
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001615/// \brief Do template substitution on declaration name info.
1616DeclarationNameInfo
1617Sema::SubstDeclarationNameInfo(const DeclarationNameInfo &NameInfo,
1618 const MultiLevelTemplateArgumentList &TemplateArgs) {
1619 TemplateInstantiator Instantiator(*this, TemplateArgs, NameInfo.getLoc(),
1620 NameInfo.getName());
1621 return Instantiator.TransformDeclarationNameInfo(NameInfo);
1622}
1623
Douglas Gregoraa594892009-03-31 18:38:02 +00001624TemplateName
John McCall76d824f2009-08-25 22:02:44 +00001625Sema::SubstTemplateName(TemplateName Name, SourceLocation Loc,
Douglas Gregor01afeef2009-08-28 20:31:08 +00001626 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor71dc5092009-08-06 06:41:21 +00001627 TemplateInstantiator Instantiator(*this, TemplateArgs, Loc,
1628 DeclarationName());
1629 return Instantiator.TransformTemplateName(Name);
Douglas Gregoraa594892009-03-31 18:38:02 +00001630}
Douglas Gregorc43620d2009-06-11 00:06:24 +00001631
John McCall0ad16662009-10-29 08:12:44 +00001632bool Sema::Subst(const TemplateArgumentLoc &Input, TemplateArgumentLoc &Output,
1633 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregore922c772009-08-04 22:27:00 +00001634 TemplateInstantiator Instantiator(*this, TemplateArgs, SourceLocation(),
1635 DeclarationName());
John McCall0ad16662009-10-29 08:12:44 +00001636
1637 return Instantiator.TransformTemplateArgument(Input, Output);
Douglas Gregorc43620d2009-06-11 00:06:24 +00001638}
Douglas Gregor14cf7522010-04-30 18:55:50 +00001639
1640Decl *Sema::LocalInstantiationScope::getInstantiationOf(const Decl *D) {
1641 for (LocalInstantiationScope *Current = this; Current;
1642 Current = Current->Outer) {
1643 // Check if we found something within this scope.
1644 llvm::DenseMap<const Decl *, Decl *>::iterator Found
1645 = Current->LocalDecls.find(D);
1646 if (Found != Current->LocalDecls.end())
1647 return Found->second;
1648
1649 // If we aren't combined with our outer scope, we're done.
1650 if (!Current->CombineWithOuterScope)
1651 break;
1652 }
1653
1654 assert(D->isInvalidDecl() &&
1655 "declaration was not instantiated in this scope!");
1656 return 0;
1657}
1658
1659void Sema::LocalInstantiationScope::InstantiatedLocal(const Decl *D,
1660 Decl *Inst) {
1661 Decl *&Stored = LocalDecls[D];
1662 assert((!Stored || Stored == Inst)&& "Already instantiated this local");
1663 Stored = Inst;
1664}