blob: 16500d4207a3d33556afad9dda48e57a6c1739ce [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 Invalid = CheckInstantiationDepth(PointOfInstantiation,
152 InstantiationRange);
153 if (!Invalid) {
Douglas Gregorfcd5db32009-03-10 00:06:19 +0000154 ActiveTemplateInstantiation Inst;
Douglas Gregor79cf6032009-03-10 20:44:00 +0000155 Inst.Kind = ActiveTemplateInstantiation::TemplateInstantiation;
Douglas Gregorfcd5db32009-03-10 00:06:19 +0000156 Inst.PointOfInstantiation = PointOfInstantiation;
Douglas Gregor79cf6032009-03-10 20:44:00 +0000157 Inst.Entity = reinterpret_cast<uintptr_t>(Entity);
Douglas Gregorc9220832009-03-12 18:36:18 +0000158 Inst.TemplateArgs = 0;
159 Inst.NumTemplateArgs = 0;
Douglas Gregor79cf6032009-03-10 20:44:00 +0000160 Inst.InstantiationRange = InstantiationRange;
161 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
Douglas Gregor79cf6032009-03-10 20:44:00 +0000162 }
163}
164
Mike Stump11289f42009-09-09 15:08:12 +0000165Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef,
Douglas Gregor79cf6032009-03-10 20:44:00 +0000166 SourceLocation PointOfInstantiation,
167 TemplateDecl *Template,
168 const TemplateArgument *TemplateArgs,
169 unsigned NumTemplateArgs,
170 SourceRange InstantiationRange)
171 : SemaRef(SemaRef) {
172
173 Invalid = CheckInstantiationDepth(PointOfInstantiation,
174 InstantiationRange);
175 if (!Invalid) {
176 ActiveTemplateInstantiation Inst;
Mike Stump11289f42009-09-09 15:08:12 +0000177 Inst.Kind
Douglas Gregor79cf6032009-03-10 20:44:00 +0000178 = ActiveTemplateInstantiation::DefaultTemplateArgumentInstantiation;
179 Inst.PointOfInstantiation = PointOfInstantiation;
180 Inst.Entity = reinterpret_cast<uintptr_t>(Template);
181 Inst.TemplateArgs = TemplateArgs;
182 Inst.NumTemplateArgs = NumTemplateArgs;
Douglas Gregorfcd5db32009-03-10 00:06:19 +0000183 Inst.InstantiationRange = InstantiationRange;
184 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
Douglas Gregorfcd5db32009-03-10 00:06:19 +0000185 }
186}
187
Mike Stump11289f42009-09-09 15:08:12 +0000188Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef,
Douglas Gregor637d9982009-06-10 23:47:09 +0000189 SourceLocation PointOfInstantiation,
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000190 FunctionTemplateDecl *FunctionTemplate,
191 const TemplateArgument *TemplateArgs,
192 unsigned NumTemplateArgs,
193 ActiveTemplateInstantiation::InstantiationKind Kind,
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +0000194 sema::TemplateDeductionInfo &DeductionInfo,
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000195 SourceRange InstantiationRange)
Nick Lewycky9331ed82010-11-20 01:29:55 +0000196 : 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;
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +0000207 Inst.DeductionInfo = &DeductionInfo;
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000208 Inst.InstantiationRange = InstantiationRange;
209 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
Douglas Gregor84d49a22009-11-11 21:54:23 +0000210
211 if (!Inst.isInstantiationRecord())
212 ++SemaRef.NonInstantiationEntries;
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000213 }
214}
215
Mike Stump11289f42009-09-09 15:08:12 +0000216Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef,
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000217 SourceLocation PointOfInstantiation,
Douglas Gregor637d9982009-06-10 23:47:09 +0000218 ClassTemplatePartialSpecializationDecl *PartialSpec,
219 const TemplateArgument *TemplateArgs,
220 unsigned NumTemplateArgs,
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +0000221 sema::TemplateDeductionInfo &DeductionInfo,
Douglas Gregor637d9982009-06-10 23:47:09 +0000222 SourceRange InstantiationRange)
223 : SemaRef(SemaRef) {
224
Douglas Gregor84d49a22009-11-11 21:54:23 +0000225 Invalid = false;
226
227 ActiveTemplateInstantiation Inst;
228 Inst.Kind = ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution;
229 Inst.PointOfInstantiation = PointOfInstantiation;
230 Inst.Entity = reinterpret_cast<uintptr_t>(PartialSpec);
231 Inst.TemplateArgs = TemplateArgs;
232 Inst.NumTemplateArgs = NumTemplateArgs;
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +0000233 Inst.DeductionInfo = &DeductionInfo;
Douglas Gregor84d49a22009-11-11 21:54:23 +0000234 Inst.InstantiationRange = InstantiationRange;
235 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
236
237 assert(!Inst.isInstantiationRecord());
238 ++SemaRef.NonInstantiationEntries;
Douglas Gregor637d9982009-06-10 23:47:09 +0000239}
240
Mike Stump11289f42009-09-09 15:08:12 +0000241Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef,
Douglas Gregore62e6a02009-11-11 19:13:48 +0000242 SourceLocation PointOfInstantiation,
Anders Carlsson657bad42009-09-05 05:14:19 +0000243 ParmVarDecl *Param,
244 const TemplateArgument *TemplateArgs,
245 unsigned NumTemplateArgs,
246 SourceRange InstantiationRange)
247 : SemaRef(SemaRef) {
Mike Stump11289f42009-09-09 15:08:12 +0000248
Douglas Gregore62e6a02009-11-11 19:13:48 +0000249 Invalid = CheckInstantiationDepth(PointOfInstantiation, InstantiationRange);
Anders Carlsson657bad42009-09-05 05:14:19 +0000250
251 if (!Invalid) {
252 ActiveTemplateInstantiation Inst;
253 Inst.Kind
254 = ActiveTemplateInstantiation::DefaultFunctionArgumentInstantiation;
Douglas Gregore62e6a02009-11-11 19:13:48 +0000255 Inst.PointOfInstantiation = PointOfInstantiation;
Anders Carlsson657bad42009-09-05 05:14:19 +0000256 Inst.Entity = reinterpret_cast<uintptr_t>(Param);
257 Inst.TemplateArgs = TemplateArgs;
258 Inst.NumTemplateArgs = NumTemplateArgs;
259 Inst.InstantiationRange = InstantiationRange;
260 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
Douglas Gregore62e6a02009-11-11 19:13:48 +0000261 }
262}
263
264Sema::InstantiatingTemplate::
265InstantiatingTemplate(Sema &SemaRef, SourceLocation PointOfInstantiation,
266 TemplateDecl *Template,
267 NonTypeTemplateParmDecl *Param,
268 const TemplateArgument *TemplateArgs,
269 unsigned NumTemplateArgs,
270 SourceRange InstantiationRange) : SemaRef(SemaRef) {
Douglas Gregor84d49a22009-11-11 21:54:23 +0000271 Invalid = false;
Douglas Gregore62e6a02009-11-11 19:13:48 +0000272
Douglas Gregor84d49a22009-11-11 21:54:23 +0000273 ActiveTemplateInstantiation Inst;
274 Inst.Kind = ActiveTemplateInstantiation::PriorTemplateArgumentSubstitution;
275 Inst.PointOfInstantiation = PointOfInstantiation;
276 Inst.Template = Template;
277 Inst.Entity = reinterpret_cast<uintptr_t>(Param);
278 Inst.TemplateArgs = TemplateArgs;
279 Inst.NumTemplateArgs = NumTemplateArgs;
280 Inst.InstantiationRange = InstantiationRange;
281 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
282
283 assert(!Inst.isInstantiationRecord());
284 ++SemaRef.NonInstantiationEntries;
Douglas Gregore62e6a02009-11-11 19:13:48 +0000285}
286
287Sema::InstantiatingTemplate::
288InstantiatingTemplate(Sema &SemaRef, SourceLocation PointOfInstantiation,
289 TemplateDecl *Template,
290 TemplateTemplateParmDecl *Param,
291 const TemplateArgument *TemplateArgs,
292 unsigned NumTemplateArgs,
293 SourceRange InstantiationRange) : SemaRef(SemaRef) {
Douglas Gregor84d49a22009-11-11 21:54:23 +0000294 Invalid = false;
295 ActiveTemplateInstantiation Inst;
296 Inst.Kind = ActiveTemplateInstantiation::PriorTemplateArgumentSubstitution;
297 Inst.PointOfInstantiation = PointOfInstantiation;
298 Inst.Template = Template;
299 Inst.Entity = reinterpret_cast<uintptr_t>(Param);
300 Inst.TemplateArgs = TemplateArgs;
301 Inst.NumTemplateArgs = NumTemplateArgs;
302 Inst.InstantiationRange = InstantiationRange;
303 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
Douglas Gregore62e6a02009-11-11 19:13:48 +0000304
Douglas Gregor84d49a22009-11-11 21:54:23 +0000305 assert(!Inst.isInstantiationRecord());
306 ++SemaRef.NonInstantiationEntries;
307}
308
309Sema::InstantiatingTemplate::
310InstantiatingTemplate(Sema &SemaRef, SourceLocation PointOfInstantiation,
311 TemplateDecl *Template,
312 NamedDecl *Param,
313 const TemplateArgument *TemplateArgs,
314 unsigned NumTemplateArgs,
315 SourceRange InstantiationRange) : SemaRef(SemaRef) {
316 Invalid = false;
317
318 ActiveTemplateInstantiation Inst;
319 Inst.Kind = ActiveTemplateInstantiation::DefaultTemplateArgumentChecking;
320 Inst.PointOfInstantiation = PointOfInstantiation;
321 Inst.Template = Template;
322 Inst.Entity = reinterpret_cast<uintptr_t>(Param);
323 Inst.TemplateArgs = TemplateArgs;
324 Inst.NumTemplateArgs = NumTemplateArgs;
325 Inst.InstantiationRange = InstantiationRange;
326 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
327
328 assert(!Inst.isInstantiationRecord());
329 ++SemaRef.NonInstantiationEntries;
Anders Carlsson657bad42009-09-05 05:14:19 +0000330}
331
Douglas Gregor85673582009-05-18 17:01:57 +0000332void Sema::InstantiatingTemplate::Clear() {
333 if (!Invalid) {
Douglas Gregor84d49a22009-11-11 21:54:23 +0000334 if (!SemaRef.ActiveTemplateInstantiations.back().isInstantiationRecord()) {
335 assert(SemaRef.NonInstantiationEntries > 0);
336 --SemaRef.NonInstantiationEntries;
337 }
338
Douglas Gregorfcd5db32009-03-10 00:06:19 +0000339 SemaRef.ActiveTemplateInstantiations.pop_back();
Douglas Gregor85673582009-05-18 17:01:57 +0000340 Invalid = true;
341 }
Douglas Gregorfcd5db32009-03-10 00:06:19 +0000342}
343
Douglas Gregor79cf6032009-03-10 20:44:00 +0000344bool Sema::InstantiatingTemplate::CheckInstantiationDepth(
345 SourceLocation PointOfInstantiation,
346 SourceRange InstantiationRange) {
Douglas Gregor84d49a22009-11-11 21:54:23 +0000347 assert(SemaRef.NonInstantiationEntries <=
348 SemaRef.ActiveTemplateInstantiations.size());
349 if ((SemaRef.ActiveTemplateInstantiations.size() -
350 SemaRef.NonInstantiationEntries)
351 <= SemaRef.getLangOptions().InstantiationDepth)
Douglas Gregor79cf6032009-03-10 20:44:00 +0000352 return false;
353
Mike Stump11289f42009-09-09 15:08:12 +0000354 SemaRef.Diag(PointOfInstantiation,
Douglas Gregor79cf6032009-03-10 20:44:00 +0000355 diag::err_template_recursion_depth_exceeded)
356 << SemaRef.getLangOptions().InstantiationDepth
357 << InstantiationRange;
358 SemaRef.Diag(PointOfInstantiation, diag::note_template_recursion_depth)
359 << SemaRef.getLangOptions().InstantiationDepth;
360 return true;
361}
362
Douglas Gregor4ea568f2009-03-10 18:03:33 +0000363/// \brief Prints the current instantiation stack through a series of
364/// notes.
365void Sema::PrintInstantiationStack() {
Douglas Gregorffed1cb2010-04-20 07:18:24 +0000366 // Determine which template instantiations to skip, if any.
367 unsigned SkipStart = ActiveTemplateInstantiations.size(), SkipEnd = SkipStart;
368 unsigned Limit = Diags.getTemplateBacktraceLimit();
369 if (Limit && Limit < ActiveTemplateInstantiations.size()) {
370 SkipStart = Limit / 2 + Limit % 2;
371 SkipEnd = ActiveTemplateInstantiations.size() - Limit / 2;
372 }
373
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000374 // FIXME: In all of these cases, we need to show the template arguments
Douglas Gregorffed1cb2010-04-20 07:18:24 +0000375 unsigned InstantiationIdx = 0;
Douglas Gregor4ea568f2009-03-10 18:03:33 +0000376 for (llvm::SmallVector<ActiveTemplateInstantiation, 16>::reverse_iterator
377 Active = ActiveTemplateInstantiations.rbegin(),
378 ActiveEnd = ActiveTemplateInstantiations.rend();
379 Active != ActiveEnd;
Douglas Gregorffed1cb2010-04-20 07:18:24 +0000380 ++Active, ++InstantiationIdx) {
381 // Skip this instantiation?
382 if (InstantiationIdx >= SkipStart && InstantiationIdx < SkipEnd) {
383 if (InstantiationIdx == SkipStart) {
384 // Note that we're skipping instantiations.
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000385 Diags.Report(Active->PointOfInstantiation,
Douglas Gregorffed1cb2010-04-20 07:18:24 +0000386 diag::note_instantiation_contexts_suppressed)
387 << unsigned(ActiveTemplateInstantiations.size() - Limit);
388 }
389 continue;
390 }
391
Douglas Gregor79cf6032009-03-10 20:44:00 +0000392 switch (Active->Kind) {
393 case ActiveTemplateInstantiation::TemplateInstantiation: {
Douglas Gregor85673582009-05-18 17:01:57 +0000394 Decl *D = reinterpret_cast<Decl *>(Active->Entity);
395 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) {
396 unsigned DiagID = diag::note_template_member_class_here;
397 if (isa<ClassTemplateSpecializationDecl>(Record))
398 DiagID = diag::note_template_class_instantiation_here;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000399 Diags.Report(Active->PointOfInstantiation, DiagID)
Douglas Gregor85673582009-05-18 17:01:57 +0000400 << Context.getTypeDeclType(Record)
401 << Active->InstantiationRange;
Douglas Gregora6ef8f02009-07-24 20:34:43 +0000402 } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
Douglas Gregor4adbc6d2009-06-26 00:10:03 +0000403 unsigned DiagID;
404 if (Function->getPrimaryTemplate())
405 DiagID = diag::note_function_template_spec_here;
406 else
407 DiagID = diag::note_template_member_function_here;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000408 Diags.Report(Active->PointOfInstantiation, DiagID)
Douglas Gregor85673582009-05-18 17:01:57 +0000409 << Function
410 << Active->InstantiationRange;
Douglas Gregora6ef8f02009-07-24 20:34:43 +0000411 } else {
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000412 Diags.Report(Active->PointOfInstantiation,
Douglas Gregora6ef8f02009-07-24 20:34:43 +0000413 diag::note_template_static_data_member_def_here)
414 << cast<VarDecl>(D)
415 << Active->InstantiationRange;
Douglas Gregor85673582009-05-18 17:01:57 +0000416 }
Douglas Gregor79cf6032009-03-10 20:44:00 +0000417 break;
418 }
419
420 case ActiveTemplateInstantiation::DefaultTemplateArgumentInstantiation: {
421 TemplateDecl *Template = cast<TemplateDecl>((Decl *)Active->Entity);
422 std::string TemplateArgsStr
Douglas Gregordc572a32009-03-30 22:58:21 +0000423 = TemplateSpecializationType::PrintTemplateArgumentList(
Mike Stump11289f42009-09-09 15:08:12 +0000424 Active->TemplateArgs,
Douglas Gregor7de59662009-05-29 20:38:28 +0000425 Active->NumTemplateArgs,
426 Context.PrintingPolicy);
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000427 Diags.Report(Active->PointOfInstantiation,
Douglas Gregor79cf6032009-03-10 20:44:00 +0000428 diag::note_default_arg_instantiation_here)
429 << (Template->getNameAsString() + TemplateArgsStr)
430 << Active->InstantiationRange;
431 break;
432 }
Douglas Gregor637d9982009-06-10 23:47:09 +0000433
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000434 case ActiveTemplateInstantiation::ExplicitTemplateArgumentSubstitution: {
Mike Stump11289f42009-09-09 15:08:12 +0000435 FunctionTemplateDecl *FnTmpl
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000436 = cast<FunctionTemplateDecl>((Decl *)Active->Entity);
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000437 Diags.Report(Active->PointOfInstantiation,
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000438 diag::note_explicit_template_arg_substitution_here)
Douglas Gregor607f1412010-03-30 20:35:20 +0000439 << FnTmpl
440 << getTemplateArgumentBindingsText(FnTmpl->getTemplateParameters(),
441 Active->TemplateArgs,
442 Active->NumTemplateArgs)
443 << Active->InstantiationRange;
Douglas Gregor637d9982009-06-10 23:47:09 +0000444 break;
445 }
Mike Stump11289f42009-09-09 15:08:12 +0000446
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000447 case ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution:
448 if (ClassTemplatePartialSpecializationDecl *PartialSpec
449 = dyn_cast<ClassTemplatePartialSpecializationDecl>(
450 (Decl *)Active->Entity)) {
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000451 Diags.Report(Active->PointOfInstantiation,
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000452 diag::note_partial_spec_deduct_instantiation_here)
453 << Context.getTypeDeclType(PartialSpec)
Douglas Gregor607f1412010-03-30 20:35:20 +0000454 << getTemplateArgumentBindingsText(
455 PartialSpec->getTemplateParameters(),
456 Active->TemplateArgs,
457 Active->NumTemplateArgs)
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000458 << Active->InstantiationRange;
459 } else {
460 FunctionTemplateDecl *FnTmpl
461 = cast<FunctionTemplateDecl>((Decl *)Active->Entity);
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000462 Diags.Report(Active->PointOfInstantiation,
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000463 diag::note_function_template_deduction_instantiation_here)
Douglas Gregor607f1412010-03-30 20:35:20 +0000464 << FnTmpl
465 << getTemplateArgumentBindingsText(FnTmpl->getTemplateParameters(),
466 Active->TemplateArgs,
467 Active->NumTemplateArgs)
468 << Active->InstantiationRange;
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000469 }
470 break;
Douglas Gregor637d9982009-06-10 23:47:09 +0000471
Anders Carlsson657bad42009-09-05 05:14:19 +0000472 case ActiveTemplateInstantiation::DefaultFunctionArgumentInstantiation: {
473 ParmVarDecl *Param = cast<ParmVarDecl>((Decl *)Active->Entity);
474 FunctionDecl *FD = cast<FunctionDecl>(Param->getDeclContext());
Mike Stump11289f42009-09-09 15:08:12 +0000475
Anders Carlsson657bad42009-09-05 05:14:19 +0000476 std::string TemplateArgsStr
477 = TemplateSpecializationType::PrintTemplateArgumentList(
Mike Stump11289f42009-09-09 15:08:12 +0000478 Active->TemplateArgs,
Anders Carlsson657bad42009-09-05 05:14:19 +0000479 Active->NumTemplateArgs,
480 Context.PrintingPolicy);
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000481 Diags.Report(Active->PointOfInstantiation,
Anders Carlsson657bad42009-09-05 05:14:19 +0000482 diag::note_default_function_arg_instantiation_here)
Anders Carlssondc6d2c32009-09-05 05:38:54 +0000483 << (FD->getNameAsString() + TemplateArgsStr)
Anders Carlsson657bad42009-09-05 05:14:19 +0000484 << Active->InstantiationRange;
485 break;
486 }
Mike Stump11289f42009-09-09 15:08:12 +0000487
Douglas Gregore62e6a02009-11-11 19:13:48 +0000488 case ActiveTemplateInstantiation::PriorTemplateArgumentSubstitution: {
489 NamedDecl *Parm = cast<NamedDecl>((Decl *)Active->Entity);
490 std::string Name;
491 if (!Parm->getName().empty())
492 Name = std::string(" '") + Parm->getName().str() + "'";
493
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000494 Diags.Report(Active->PointOfInstantiation,
Douglas Gregore62e6a02009-11-11 19:13:48 +0000495 diag::note_prior_template_arg_substitution)
496 << isa<TemplateTemplateParmDecl>(Parm)
497 << Name
498 << getTemplateArgumentBindingsText(
499 Active->Template->getTemplateParameters(),
500 Active->TemplateArgs,
501 Active->NumTemplateArgs)
502 << Active->InstantiationRange;
503 break;
504 }
Douglas Gregor84d49a22009-11-11 21:54:23 +0000505
506 case ActiveTemplateInstantiation::DefaultTemplateArgumentChecking: {
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000507 Diags.Report(Active->PointOfInstantiation,
Douglas Gregor84d49a22009-11-11 21:54:23 +0000508 diag::note_template_default_arg_checking)
509 << getTemplateArgumentBindingsText(
510 Active->Template->getTemplateParameters(),
511 Active->TemplateArgs,
512 Active->NumTemplateArgs)
513 << Active->InstantiationRange;
514 break;
515 }
Douglas Gregor79cf6032009-03-10 20:44:00 +0000516 }
Douglas Gregor4ea568f2009-03-10 18:03:33 +0000517 }
518}
519
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +0000520TemplateDeductionInfo *Sema::isSFINAEContext() const {
Douglas Gregor33834512009-06-14 07:33:30 +0000521 using llvm::SmallVector;
522 for (SmallVector<ActiveTemplateInstantiation, 16>::const_reverse_iterator
523 Active = ActiveTemplateInstantiations.rbegin(),
524 ActiveEnd = ActiveTemplateInstantiations.rend();
525 Active != ActiveEnd;
Douglas Gregor84d49a22009-11-11 21:54:23 +0000526 ++Active)
527 {
Douglas Gregor33834512009-06-14 07:33:30 +0000528 switch(Active->Kind) {
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000529 case ActiveTemplateInstantiation::TemplateInstantiation:
Anders Carlsson657bad42009-09-05 05:14:19 +0000530 case ActiveTemplateInstantiation::DefaultFunctionArgumentInstantiation:
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000531 // This is a template instantiation, so there is no SFINAE.
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +0000532 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000533
Douglas Gregor33834512009-06-14 07:33:30 +0000534 case ActiveTemplateInstantiation::DefaultTemplateArgumentInstantiation:
Douglas Gregore62e6a02009-11-11 19:13:48 +0000535 case ActiveTemplateInstantiation::PriorTemplateArgumentSubstitution:
Douglas Gregor84d49a22009-11-11 21:54:23 +0000536 case ActiveTemplateInstantiation::DefaultTemplateArgumentChecking:
Douglas Gregore62e6a02009-11-11 19:13:48 +0000537 // A default template argument instantiation and substitution into
538 // template parameters with arguments for prior parameters may or may
539 // not be a SFINAE context; look further up the stack.
Douglas Gregor33834512009-06-14 07:33:30 +0000540 break;
Mike Stump11289f42009-09-09 15:08:12 +0000541
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000542 case ActiveTemplateInstantiation::ExplicitTemplateArgumentSubstitution:
543 case ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution:
544 // We're either substitution explicitly-specified template arguments
545 // or deduced template arguments, so SFINAE applies.
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +0000546 assert(Active->DeductionInfo && "Missing deduction info pointer");
547 return Active->DeductionInfo;
Douglas Gregor33834512009-06-14 07:33:30 +0000548 }
549 }
550
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +0000551 return 0;
Douglas Gregor33834512009-06-14 07:33:30 +0000552}
553
Douglas Gregorfe1e1102009-02-27 19:31:52 +0000554//===----------------------------------------------------------------------===/
555// Template Instantiation for Types
556//===----------------------------------------------------------------------===/
Douglas Gregor17c0d7b2009-02-28 00:25:32 +0000557namespace {
Douglas Gregor14cf7522010-04-30 18:55:50 +0000558 class TemplateInstantiator : public TreeTransform<TemplateInstantiator> {
Douglas Gregor01afeef2009-08-28 20:31:08 +0000559 const MultiLevelTemplateArgumentList &TemplateArgs;
Douglas Gregor17c0d7b2009-02-28 00:25:32 +0000560 SourceLocation Loc;
561 DeclarationName Entity;
Douglas Gregorfe1e1102009-02-27 19:31:52 +0000562
Douglas Gregor17c0d7b2009-02-28 00:25:32 +0000563 public:
Douglas Gregorebe10102009-08-20 07:17:43 +0000564 typedef TreeTransform<TemplateInstantiator> inherited;
Mike Stump11289f42009-09-09 15:08:12 +0000565
566 TemplateInstantiator(Sema &SemaRef,
Douglas Gregor01afeef2009-08-28 20:31:08 +0000567 const MultiLevelTemplateArgumentList &TemplateArgs,
Douglas Gregord6ff3322009-08-04 16:50:30 +0000568 SourceLocation Loc,
Mike Stump11289f42009-09-09 15:08:12 +0000569 DeclarationName Entity)
570 : inherited(SemaRef), TemplateArgs(TemplateArgs), Loc(Loc),
Douglas Gregorebe10102009-08-20 07:17:43 +0000571 Entity(Entity) { }
Douglas Gregor17c0d7b2009-02-28 00:25:32 +0000572
Mike Stump11289f42009-09-09 15:08:12 +0000573 /// \brief Determine whether the given type \p T has already been
Douglas Gregord6ff3322009-08-04 16:50:30 +0000574 /// transformed.
575 ///
576 /// For the purposes of template instantiation, a type has already been
577 /// transformed if it is NULL or if it is not dependent.
Douglas Gregor5597ab42010-05-07 23:12:07 +0000578 bool AlreadyTransformed(QualType T);
Mike Stump11289f42009-09-09 15:08:12 +0000579
Douglas Gregord6ff3322009-08-04 16:50:30 +0000580 /// \brief Returns the location of the entity being instantiated, if known.
581 SourceLocation getBaseLocation() { return Loc; }
Mike Stump11289f42009-09-09 15:08:12 +0000582
Douglas Gregord6ff3322009-08-04 16:50:30 +0000583 /// \brief Returns the name of the entity being instantiated, if any.
584 DeclarationName getBaseEntity() { return Entity; }
Mike Stump11289f42009-09-09 15:08:12 +0000585
Douglas Gregoref6ab412009-10-27 06:26:26 +0000586 /// \brief Sets the "base" location and entity when that
587 /// information is known based on another transformation.
588 void setBase(SourceLocation Loc, DeclarationName Entity) {
589 this->Loc = Loc;
590 this->Entity = Entity;
591 }
Douglas Gregor840bd6c2010-12-20 22:05:00 +0000592
593 bool TryExpandParameterPacks(SourceLocation EllipsisLoc,
594 SourceRange PatternRange,
595 const UnexpandedParameterPack *Unexpanded,
596 unsigned NumUnexpanded,
597 bool &ShouldExpand,
Douglas Gregor76aca7b2010-12-21 00:52:54 +0000598 unsigned &NumExpansions) {
599 return getSema().CheckParameterPacksForExpansion(EllipsisLoc,
600 PatternRange, Unexpanded,
601 NumUnexpanded,
602 TemplateArgs,
603 ShouldExpand,
604 NumExpansions);
605 }
Douglas Gregor840bd6c2010-12-20 22:05:00 +0000606
Douglas Gregord6ff3322009-08-04 16:50:30 +0000607 /// \brief Transform the given declaration by instantiating a reference to
608 /// this declaration.
Douglas Gregora04f2ca2010-03-01 15:56:25 +0000609 Decl *TransformDecl(SourceLocation Loc, Decl *D);
Douglas Gregora16548e2009-08-11 05:31:07 +0000610
Mike Stump11289f42009-09-09 15:08:12 +0000611 /// \brief Transform the definition of the given declaration by
Douglas Gregorebe10102009-08-20 07:17:43 +0000612 /// instantiating it.
Douglas Gregor25289362010-03-01 17:25:41 +0000613 Decl *TransformDefinition(SourceLocation Loc, Decl *D);
Mike Stump11289f42009-09-09 15:08:12 +0000614
Douglas Gregora5cb6da2009-10-20 05:58:46 +0000615 /// \bried Transform the first qualifier within a scope by instantiating the
616 /// declaration.
617 NamedDecl *TransformFirstQualifierInScope(NamedDecl *D, SourceLocation Loc);
618
Douglas Gregorebe10102009-08-20 07:17:43 +0000619 /// \brief Rebuild the exception declaration and register the declaration
620 /// as an instantiated local.
Douglas Gregor9f0e1aa2010-09-09 17:09:21 +0000621 VarDecl *RebuildExceptionDecl(VarDecl *ExceptionDecl,
John McCallbcd03502009-12-07 02:54:59 +0000622 TypeSourceInfo *Declarator,
Douglas Gregorebe10102009-08-20 07:17:43 +0000623 IdentifierInfo *Name,
Douglas Gregor9f0e1aa2010-09-09 17:09:21 +0000624 SourceLocation Loc);
Mike Stump11289f42009-09-09 15:08:12 +0000625
Douglas Gregorf4e837f2010-04-26 17:57:08 +0000626 /// \brief Rebuild the Objective-C exception declaration and register the
627 /// declaration as an instantiated local.
628 VarDecl *RebuildObjCExceptionDecl(VarDecl *ExceptionDecl,
629 TypeSourceInfo *TSInfo, QualType T);
630
John McCall7f41d982009-09-11 04:59:25 +0000631 /// \brief Check for tag mismatches when instantiating an
632 /// elaborated type.
John McCall954b5de2010-11-04 19:04:38 +0000633 QualType RebuildElaboratedType(SourceLocation KeywordLoc,
634 ElaboratedTypeKeyword Keyword,
Abramo Bagnara6150c882010-05-11 21:36:43 +0000635 NestedNameSpecifier *NNS, QualType T);
John McCall7f41d982009-09-11 04:59:25 +0000636
John McCalldadc5752010-08-24 06:29:42 +0000637 ExprResult TransformPredefinedExpr(PredefinedExpr *E);
638 ExprResult TransformDeclRefExpr(DeclRefExpr *E);
639 ExprResult TransformCXXDefaultArgExpr(CXXDefaultArgExpr *E);
640 ExprResult TransformTemplateParmRefExpr(DeclRefExpr *E,
Douglas Gregoreb5a39d2010-12-24 00:15:10 +0000641 NonTypeTemplateParmDecl *D);
Sebastian Redl14236c82009-11-08 13:56:19 +0000642
Douglas Gregor14cf7522010-04-30 18:55:50 +0000643 QualType TransformFunctionProtoType(TypeLocBuilder &TLB,
John McCall31f82722010-11-12 08:19:04 +0000644 FunctionProtoTypeLoc TL);
John McCall58f10c32010-03-11 09:03:00 +0000645 ParmVarDecl *TransformFunctionTypeParam(ParmVarDecl *OldParm);
646
Mike Stump11289f42009-09-09 15:08:12 +0000647 /// \brief Transforms a template type parameter type by performing
Douglas Gregord6ff3322009-08-04 16:50:30 +0000648 /// substitution of the corresponding template type argument.
John McCall550e0c22009-10-21 00:40:46 +0000649 QualType TransformTemplateTypeParmType(TypeLocBuilder &TLB,
John McCall31f82722010-11-12 08:19:04 +0000650 TemplateTypeParmTypeLoc TL);
Nick Lewyckyc96c37f2010-07-06 19:51:49 +0000651
John McCalldadc5752010-08-24 06:29:42 +0000652 ExprResult TransformCallExpr(CallExpr *CE) {
Nick Lewyckyc96c37f2010-07-06 19:51:49 +0000653 getSema().CallsUndergoingInstantiation.push_back(CE);
John McCalldadc5752010-08-24 06:29:42 +0000654 ExprResult Result =
Nick Lewyckyc96c37f2010-07-06 19:51:49 +0000655 TreeTransform<TemplateInstantiator>::TransformCallExpr(CE);
656 getSema().CallsUndergoingInstantiation.pop_back();
657 return move(Result);
658 }
Douglas Gregord6ff3322009-08-04 16:50:30 +0000659 };
Douglas Gregor04318252009-07-06 15:59:29 +0000660}
661
Douglas Gregor5597ab42010-05-07 23:12:07 +0000662bool TemplateInstantiator::AlreadyTransformed(QualType T) {
663 if (T.isNull())
664 return true;
665
Douglas Gregor5a5073e2010-05-24 17:22:01 +0000666 if (T->isDependentType() || T->isVariablyModifiedType())
Douglas Gregor5597ab42010-05-07 23:12:07 +0000667 return false;
668
669 getSema().MarkDeclarationsReferencedInType(Loc, T);
670 return true;
671}
672
Douglas Gregora04f2ca2010-03-01 15:56:25 +0000673Decl *TemplateInstantiator::TransformDecl(SourceLocation Loc, Decl *D) {
Douglas Gregor2b6ca462009-09-03 21:38:09 +0000674 if (!D)
675 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000676
Douglas Gregor2b6ca462009-09-03 21:38:09 +0000677 if (TemplateTemplateParmDecl *TTP = dyn_cast<TemplateTemplateParmDecl>(D)) {
Douglas Gregor01afeef2009-08-28 20:31:08 +0000678 if (TTP->getDepth() < TemplateArgs.getNumLevels()) {
Douglas Gregorb93971082010-02-05 19:54:12 +0000679 // If the corresponding template argument is NULL or non-existent, it's
680 // because we are performing instantiation from explicitly-specified
681 // template arguments in a function template, but there were some
682 // arguments left unspecified.
683 if (!TemplateArgs.hasTemplateArgument(TTP->getDepth(),
684 TTP->getPosition()))
685 return D;
686
Douglas Gregor840bd6c2010-12-20 22:05:00 +0000687 // FIXME: Variadic templates index substitution.
Douglas Gregor9167f8b2009-11-11 01:00:40 +0000688 TemplateName Template
689 = TemplateArgs(TTP->getDepth(), TTP->getPosition()).getAsTemplate();
690 assert(!Template.isNull() && Template.getAsTemplateDecl() &&
Douglas Gregor01afeef2009-08-28 20:31:08 +0000691 "Wrong kind of template template argument");
Douglas Gregor9167f8b2009-11-11 01:00:40 +0000692 return Template.getAsTemplateDecl();
Douglas Gregor01afeef2009-08-28 20:31:08 +0000693 }
Mike Stump11289f42009-09-09 15:08:12 +0000694
Douglas Gregor9167f8b2009-11-11 01:00:40 +0000695 // Fall through to find the instantiated declaration for this template
696 // template parameter.
Douglas Gregor71dc5092009-08-06 06:41:21 +0000697 }
Mike Stump11289f42009-09-09 15:08:12 +0000698
Douglas Gregora04f2ca2010-03-01 15:56:25 +0000699 return SemaRef.FindInstantiatedDecl(Loc, cast<NamedDecl>(D), TemplateArgs);
Douglas Gregord6ff3322009-08-04 16:50:30 +0000700}
701
Douglas Gregor25289362010-03-01 17:25:41 +0000702Decl *TemplateInstantiator::TransformDefinition(SourceLocation Loc, Decl *D) {
John McCall76d824f2009-08-25 22:02:44 +0000703 Decl *Inst = getSema().SubstDecl(D, getSema().CurContext, TemplateArgs);
Douglas Gregorebe10102009-08-20 07:17:43 +0000704 if (!Inst)
705 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000706
Douglas Gregorebe10102009-08-20 07:17:43 +0000707 getSema().CurrentInstantiationScope->InstantiatedLocal(D, Inst);
708 return Inst;
709}
710
Douglas Gregora5cb6da2009-10-20 05:58:46 +0000711NamedDecl *
712TemplateInstantiator::TransformFirstQualifierInScope(NamedDecl *D,
713 SourceLocation Loc) {
714 // If the first part of the nested-name-specifier was a template type
715 // parameter, instantiate that type parameter down to a tag type.
716 if (TemplateTypeParmDecl *TTPD = dyn_cast_or_null<TemplateTypeParmDecl>(D)) {
717 const TemplateTypeParmType *TTP
718 = cast<TemplateTypeParmType>(getSema().Context.getTypeDeclType(TTPD));
Douglas Gregor53c3f4e2010-12-20 22:48:17 +0000719
Douglas Gregora5cb6da2009-10-20 05:58:46 +0000720 if (TTP->getDepth() < TemplateArgs.getNumLevels()) {
Douglas Gregor53c3f4e2010-12-20 22:48:17 +0000721 // FIXME: This needs testing w/ member access expressions.
722 TemplateArgument Arg = TemplateArgs(TTP->getDepth(), TTP->getIndex());
723
724 if (TTP->isParameterPack()) {
725 assert(Arg.getKind() == TemplateArgument::Pack &&
726 "Missing argument pack");
727
728 if (getSema().ArgumentPackSubstitutionIndex == -1) {
729 // FIXME: Variadic templates fun case.
730 getSema().Diag(Loc, diag::err_pack_expansion_mismatch_unsupported);
731 return 0;
732 }
733
734 Arg = Arg.pack_begin()[getSema().ArgumentPackSubstitutionIndex];
735 }
736
737 QualType T = Arg.getAsType();
Douglas Gregora5cb6da2009-10-20 05:58:46 +0000738 if (T.isNull())
Douglas Gregora04f2ca2010-03-01 15:56:25 +0000739 return cast_or_null<NamedDecl>(TransformDecl(Loc, D));
Douglas Gregora5cb6da2009-10-20 05:58:46 +0000740
741 if (const TagType *Tag = T->getAs<TagType>())
742 return Tag->getDecl();
743
744 // The resulting type is not a tag; complain.
745 getSema().Diag(Loc, diag::err_nested_name_spec_non_tag) << T;
746 return 0;
747 }
748 }
749
Douglas Gregora04f2ca2010-03-01 15:56:25 +0000750 return cast_or_null<NamedDecl>(TransformDecl(Loc, D));
Douglas Gregora5cb6da2009-10-20 05:58:46 +0000751}
752
Douglas Gregorebe10102009-08-20 07:17:43 +0000753VarDecl *
754TemplateInstantiator::RebuildExceptionDecl(VarDecl *ExceptionDecl,
John McCallbcd03502009-12-07 02:54:59 +0000755 TypeSourceInfo *Declarator,
Douglas Gregorebe10102009-08-20 07:17:43 +0000756 IdentifierInfo *Name,
Douglas Gregor9f0e1aa2010-09-09 17:09:21 +0000757 SourceLocation Loc) {
758 VarDecl *Var = inherited::RebuildExceptionDecl(ExceptionDecl, Declarator,
759 Name, Loc);
Douglas Gregorf4e837f2010-04-26 17:57:08 +0000760 if (Var)
761 getSema().CurrentInstantiationScope->InstantiatedLocal(ExceptionDecl, Var);
762 return Var;
763}
764
765VarDecl *TemplateInstantiator::RebuildObjCExceptionDecl(VarDecl *ExceptionDecl,
766 TypeSourceInfo *TSInfo,
767 QualType T) {
768 VarDecl *Var = inherited::RebuildObjCExceptionDecl(ExceptionDecl, TSInfo, T);
769 if (Var)
Douglas Gregorebe10102009-08-20 07:17:43 +0000770 getSema().CurrentInstantiationScope->InstantiatedLocal(ExceptionDecl, Var);
771 return Var;
772}
773
John McCall7f41d982009-09-11 04:59:25 +0000774QualType
John McCall954b5de2010-11-04 19:04:38 +0000775TemplateInstantiator::RebuildElaboratedType(SourceLocation KeywordLoc,
776 ElaboratedTypeKeyword Keyword,
Abramo Bagnara6150c882010-05-11 21:36:43 +0000777 NestedNameSpecifier *NNS,
778 QualType T) {
John McCall7f41d982009-09-11 04:59:25 +0000779 if (const TagType *TT = T->getAs<TagType>()) {
780 TagDecl* TD = TT->getDecl();
781
John McCall954b5de2010-11-04 19:04:38 +0000782 SourceLocation TagLocation = KeywordLoc;
John McCall7f41d982009-09-11 04:59:25 +0000783
784 // FIXME: type might be anonymous.
785 IdentifierInfo *Id = TD->getIdentifier();
786
787 // TODO: should we even warn on struct/class mismatches for this? Seems
788 // like it's likely to produce a lot of spurious errors.
Abramo Bagnara6150c882010-05-11 21:36:43 +0000789 if (Keyword != ETK_None && Keyword != ETK_Typename) {
790 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForKeyword(Keyword);
791 if (!SemaRef.isAcceptableTagRedeclaration(TD, Kind, TagLocation, *Id)) {
792 SemaRef.Diag(TagLocation, diag::err_use_with_wrong_tag)
793 << Id
794 << FixItHint::CreateReplacement(SourceRange(TagLocation),
795 TD->getKindName());
796 SemaRef.Diag(TD->getLocation(), diag::note_previous_use);
797 }
John McCall7f41d982009-09-11 04:59:25 +0000798 }
799 }
800
John McCall954b5de2010-11-04 19:04:38 +0000801 return TreeTransform<TemplateInstantiator>::RebuildElaboratedType(KeywordLoc,
802 Keyword,
Abramo Bagnara6150c882010-05-11 21:36:43 +0000803 NNS, T);
John McCall7f41d982009-09-11 04:59:25 +0000804}
805
John McCalldadc5752010-08-24 06:29:42 +0000806ExprResult
John McCall47f29ea2009-12-08 09:21:05 +0000807TemplateInstantiator::TransformPredefinedExpr(PredefinedExpr *E) {
Anders Carlsson0b209a82009-09-11 01:22:35 +0000808 if (!E->isTypeDependent())
John McCallc3007a22010-10-26 07:05:15 +0000809 return SemaRef.Owned(E);
Anders Carlsson0b209a82009-09-11 01:22:35 +0000810
811 FunctionDecl *currentDecl = getSema().getCurFunctionDecl();
812 assert(currentDecl && "Must have current function declaration when "
813 "instantiating.");
814
815 PredefinedExpr::IdentType IT = E->getIdentType();
816
Anders Carlsson5bd8d192010-02-11 18:20:28 +0000817 unsigned Length = PredefinedExpr::ComputeName(IT, currentDecl).length();
Anders Carlsson0b209a82009-09-11 01:22:35 +0000818
819 llvm::APInt LengthI(32, Length + 1);
John McCall8ccfcb52009-09-24 19:53:00 +0000820 QualType ResTy = getSema().Context.CharTy.withConst();
Anders Carlsson0b209a82009-09-11 01:22:35 +0000821 ResTy = getSema().Context.getConstantArrayType(ResTy, LengthI,
822 ArrayType::Normal, 0);
823 PredefinedExpr *PE =
824 new (getSema().Context) PredefinedExpr(E->getLocation(), ResTy, IT);
825 return getSema().Owned(PE);
826}
827
John McCalldadc5752010-08-24 06:29:42 +0000828ExprResult
John McCall13481c52010-02-06 08:42:39 +0000829TemplateInstantiator::TransformTemplateParmRefExpr(DeclRefExpr *E,
Douglas Gregor6c379e22010-02-08 23:41:45 +0000830 NonTypeTemplateParmDecl *NTTP) {
John McCall13481c52010-02-06 08:42:39 +0000831 // If the corresponding template argument is NULL or non-existent, it's
832 // because we are performing instantiation from explicitly-specified
833 // template arguments in a function template, but there were some
834 // arguments left unspecified.
835 if (!TemplateArgs.hasTemplateArgument(NTTP->getDepth(),
836 NTTP->getPosition()))
John McCallc3007a22010-10-26 07:05:15 +0000837 return SemaRef.Owned(E);
Mike Stump11289f42009-09-09 15:08:12 +0000838
Douglas Gregoreb5a39d2010-12-24 00:15:10 +0000839 TemplateArgument Arg = TemplateArgs(NTTP->getDepth(), NTTP->getPosition());
840 if (NTTP->isParameterPack()) {
841 assert(Arg.getKind() == TemplateArgument::Pack &&
842 "Missing argument pack");
843
844 if (getSema().ArgumentPackSubstitutionIndex == -1) {
845 // FIXME: Variadic templates fun case.
846 getSema().Diag(Loc, diag::err_pack_expansion_mismatch_unsupported);
847 return ExprError();
848 }
849
850 Arg = Arg.pack_begin()[getSema().ArgumentPackSubstitutionIndex];
851 }
Mike Stump11289f42009-09-09 15:08:12 +0000852
John McCall13481c52010-02-06 08:42:39 +0000853 // The template argument itself might be an expression, in which
854 // case we just return that expression.
855 if (Arg.getKind() == TemplateArgument::Expression)
John McCallc3007a22010-10-26 07:05:15 +0000856 return SemaRef.Owned(Arg.getAsExpr());
Mike Stump11289f42009-09-09 15:08:12 +0000857
John McCall13481c52010-02-06 08:42:39 +0000858 if (Arg.getKind() == TemplateArgument::Declaration) {
859 ValueDecl *VD = cast<ValueDecl>(Arg.getAsDecl());
Mike Stump11289f42009-09-09 15:08:12 +0000860
John McCall15dda372010-02-06 10:23:53 +0000861 // Find the instantiation of the template argument. This is
862 // required for nested templates.
John McCall13481c52010-02-06 08:42:39 +0000863 VD = cast_or_null<ValueDecl>(
Douglas Gregora04f2ca2010-03-01 15:56:25 +0000864 getSema().FindInstantiatedDecl(E->getLocation(),
865 VD, TemplateArgs));
John McCall13481c52010-02-06 08:42:39 +0000866 if (!VD)
John McCallfaf5fb42010-08-26 23:41:50 +0000867 return ExprError();
Mike Stump11289f42009-09-09 15:08:12 +0000868
John McCall15dda372010-02-06 10:23:53 +0000869 // Derive the type we want the substituted decl to have. This had
870 // better be non-dependent, or these checks will have serious problems.
871 QualType TargetType = SemaRef.SubstType(NTTP->getType(), TemplateArgs,
Douglas Gregor6c379e22010-02-08 23:41:45 +0000872 E->getLocation(),
873 DeclarationName());
John McCall15dda372010-02-06 10:23:53 +0000874 assert(!TargetType.isNull() && "type substitution failed for param type");
875 assert(!TargetType->isDependentType() && "param type still dependent");
Douglas Gregord5cb1dd2010-03-28 02:42:43 +0000876 return SemaRef.BuildExpressionFromDeclTemplateArgument(Arg,
877 TargetType,
878 E->getLocation());
John McCall13481c52010-02-06 08:42:39 +0000879 }
880
Douglas Gregord5cb1dd2010-03-28 02:42:43 +0000881 return SemaRef.BuildExpressionFromIntegralTemplateArgument(Arg,
882 E->getSourceRange().getBegin());
John McCall13481c52010-02-06 08:42:39 +0000883}
884
885
John McCalldadc5752010-08-24 06:29:42 +0000886ExprResult
John McCall13481c52010-02-06 08:42:39 +0000887TemplateInstantiator::TransformDeclRefExpr(DeclRefExpr *E) {
888 NamedDecl *D = E->getDecl();
889 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(D)) {
890 if (NTTP->getDepth() < TemplateArgs.getNumLevels())
891 return TransformTemplateParmRefExpr(E, NTTP);
Douglas Gregor954de172009-10-31 17:21:17 +0000892
893 // We have a non-type template parameter that isn't fully substituted;
894 // FindInstantiatedDecl will find it in the local instantiation scope.
Douglas Gregora16548e2009-08-11 05:31:07 +0000895 }
Mike Stump11289f42009-09-09 15:08:12 +0000896
John McCall47f29ea2009-12-08 09:21:05 +0000897 return TreeTransform<TemplateInstantiator>::TransformDeclRefExpr(E);
Douglas Gregora16548e2009-08-11 05:31:07 +0000898}
899
John McCalldadc5752010-08-24 06:29:42 +0000900ExprResult TemplateInstantiator::TransformCXXDefaultArgExpr(
John McCall47f29ea2009-12-08 09:21:05 +0000901 CXXDefaultArgExpr *E) {
Sebastian Redl14236c82009-11-08 13:56:19 +0000902 assert(!cast<FunctionDecl>(E->getParam()->getDeclContext())->
903 getDescribedFunctionTemplate() &&
904 "Default arg expressions are never formed in dependent cases.");
Douglas Gregor033f6752009-12-23 23:03:06 +0000905 return SemaRef.BuildCXXDefaultArgExpr(E->getUsedLocation(),
906 cast<FunctionDecl>(E->getParam()->getDeclContext()),
907 E->getParam());
Sebastian Redl14236c82009-11-08 13:56:19 +0000908}
909
Douglas Gregor14cf7522010-04-30 18:55:50 +0000910QualType TemplateInstantiator::TransformFunctionProtoType(TypeLocBuilder &TLB,
John McCall31f82722010-11-12 08:19:04 +0000911 FunctionProtoTypeLoc TL) {
Douglas Gregor14cf7522010-04-30 18:55:50 +0000912 // We need a local instantiation scope for this function prototype.
John McCall19c1bfd2010-08-25 05:32:35 +0000913 LocalInstantiationScope Scope(SemaRef, /*CombineWithOuterScope=*/true);
John McCall31f82722010-11-12 08:19:04 +0000914 return inherited::TransformFunctionProtoType(TLB, TL);
John McCall58f10c32010-03-11 09:03:00 +0000915}
916
917ParmVarDecl *
918TemplateInstantiator::TransformFunctionTypeParam(ParmVarDecl *OldParm) {
Douglas Gregor940bca72010-04-12 07:48:19 +0000919 return SemaRef.SubstParmVarDecl(OldParm, TemplateArgs);
John McCall58f10c32010-03-11 09:03:00 +0000920}
921
Mike Stump11289f42009-09-09 15:08:12 +0000922QualType
John McCall550e0c22009-10-21 00:40:46 +0000923TemplateInstantiator::TransformTemplateTypeParmType(TypeLocBuilder &TLB,
John McCall31f82722010-11-12 08:19:04 +0000924 TemplateTypeParmTypeLoc TL) {
John McCall550e0c22009-10-21 00:40:46 +0000925 TemplateTypeParmType *T = TL.getTypePtr();
Douglas Gregor01afeef2009-08-28 20:31:08 +0000926 if (T->getDepth() < TemplateArgs.getNumLevels()) {
Douglas Gregorfe1e1102009-02-27 19:31:52 +0000927 // Replace the template type parameter with its corresponding
928 // template argument.
Mike Stump11289f42009-09-09 15:08:12 +0000929
930 // If the corresponding template argument is NULL or doesn't exist, it's
931 // because we are performing instantiation from explicitly-specified
932 // template arguments in a function template class, but there were some
Douglas Gregore3f1f352009-07-01 00:28:38 +0000933 // arguments left unspecified.
John McCall550e0c22009-10-21 00:40:46 +0000934 if (!TemplateArgs.hasTemplateArgument(T->getDepth(), T->getIndex())) {
935 TemplateTypeParmTypeLoc NewTL
936 = TLB.push<TemplateTypeParmTypeLoc>(TL.getType());
937 NewTL.setNameLoc(TL.getNameLoc());
938 return TL.getType();
939 }
Mike Stump11289f42009-09-09 15:08:12 +0000940
Douglas Gregor840bd6c2010-12-20 22:05:00 +0000941 TemplateArgument Arg = TemplateArgs(T->getDepth(), T->getIndex());
942
943 if (T->isParameterPack()) {
944 assert(Arg.getKind() == TemplateArgument::Pack &&
945 "Missing argument pack");
946
947 if (getSema().ArgumentPackSubstitutionIndex == -1) {
948 // FIXME: Variadic templates fun case.
949 getSema().Diag(TL.getSourceRange().getBegin(),
950 diag::err_pack_expansion_mismatch_unsupported);
951 return QualType();
952 }
953
954 Arg = Arg.pack_begin()[getSema().ArgumentPackSubstitutionIndex];
955 }
956
957 assert(Arg.getKind() == TemplateArgument::Type &&
Douglas Gregorfe1e1102009-02-27 19:31:52 +0000958 "Template argument kind mismatch");
Douglas Gregor01afeef2009-08-28 20:31:08 +0000959
Douglas Gregor840bd6c2010-12-20 22:05:00 +0000960 QualType Replacement = Arg.getAsType();
John McCallcebee162009-10-18 09:09:24 +0000961
962 // TODO: only do this uniquing once, at the start of instantiation.
John McCall550e0c22009-10-21 00:40:46 +0000963 QualType Result
964 = getSema().Context.getSubstTemplateTypeParmType(T, Replacement);
965 SubstTemplateTypeParmTypeLoc NewTL
966 = TLB.push<SubstTemplateTypeParmTypeLoc>(Result);
967 NewTL.setNameLoc(TL.getNameLoc());
968 return Result;
Mike Stump11289f42009-09-09 15:08:12 +0000969 }
Douglas Gregorfe1e1102009-02-27 19:31:52 +0000970
971 // The template type parameter comes from an inner template (e.g.,
972 // the template parameter list of a member template inside the
973 // template we are instantiating). Create a new template type
974 // parameter with the template "level" reduced by one.
John McCall550e0c22009-10-21 00:40:46 +0000975 QualType Result
976 = getSema().Context.getTemplateTypeParmType(T->getDepth()
977 - TemplateArgs.getNumLevels(),
978 T->getIndex(),
979 T->isParameterPack(),
Douglas Gregor2ebcae12010-06-16 15:23:05 +0000980 T->getName());
John McCall550e0c22009-10-21 00:40:46 +0000981 TemplateTypeParmTypeLoc NewTL = TLB.push<TemplateTypeParmTypeLoc>(Result);
982 NewTL.setNameLoc(TL.getNameLoc());
983 return Result;
Douglas Gregor17c0d7b2009-02-28 00:25:32 +0000984}
Douglas Gregorfe1e1102009-02-27 19:31:52 +0000985
John McCall76d824f2009-08-25 22:02:44 +0000986/// \brief Perform substitution on the type T with a given set of template
987/// arguments.
Douglas Gregorfe1e1102009-02-27 19:31:52 +0000988///
989/// This routine substitutes the given template arguments into the
990/// type T and produces the instantiated type.
991///
992/// \param T the type into which the template arguments will be
993/// substituted. If this type is not dependent, it will be returned
994/// immediately.
995///
996/// \param TemplateArgs the template arguments that will be
997/// substituted for the top-level template parameters within T.
998///
Douglas Gregorfe1e1102009-02-27 19:31:52 +0000999/// \param Loc the location in the source code where this substitution
1000/// is being performed. It will typically be the location of the
1001/// declarator (if we're instantiating the type of some declaration)
1002/// or the location of the type in the source code (if, e.g., we're
1003/// instantiating the type of a cast expression).
1004///
1005/// \param Entity the name of the entity associated with a declaration
1006/// being instantiated (if any). May be empty to indicate that there
1007/// is no such entity (if, e.g., this is a type that occurs as part of
1008/// a cast expression) or that the entity has no name (e.g., an
1009/// unnamed function parameter).
1010///
1011/// \returns If the instantiation succeeds, the instantiated
1012/// type. Otherwise, produces diagnostics and returns a NULL type.
John McCallbcd03502009-12-07 02:54:59 +00001013TypeSourceInfo *Sema::SubstType(TypeSourceInfo *T,
John McCall609459e2009-10-21 00:58:09 +00001014 const MultiLevelTemplateArgumentList &Args,
1015 SourceLocation Loc,
1016 DeclarationName Entity) {
1017 assert(!ActiveTemplateInstantiations.empty() &&
1018 "Cannot perform an instantiation without some context on the "
1019 "instantiation stack");
1020
Douglas Gregor5a5073e2010-05-24 17:22:01 +00001021 if (!T->getType()->isDependentType() &&
1022 !T->getType()->isVariablyModifiedType())
John McCall609459e2009-10-21 00:58:09 +00001023 return T;
1024
1025 TemplateInstantiator Instantiator(*this, Args, Loc, Entity);
1026 return Instantiator.TransformType(T);
1027}
1028
1029/// Deprecated form of the above.
Mike Stump11289f42009-09-09 15:08:12 +00001030QualType Sema::SubstType(QualType T,
Douglas Gregor01afeef2009-08-28 20:31:08 +00001031 const MultiLevelTemplateArgumentList &TemplateArgs,
John McCall76d824f2009-08-25 22:02:44 +00001032 SourceLocation Loc, DeclarationName Entity) {
Douglas Gregor79cf6032009-03-10 20:44:00 +00001033 assert(!ActiveTemplateInstantiations.empty() &&
1034 "Cannot perform an instantiation without some context on the "
1035 "instantiation stack");
1036
Douglas Gregor5a5073e2010-05-24 17:22:01 +00001037 // If T is not a dependent type or a variably-modified type, there
1038 // is nothing to do.
1039 if (!T->isDependentType() && !T->isVariablyModifiedType())
Douglas Gregorfe1e1102009-02-27 19:31:52 +00001040 return T;
1041
Douglas Gregord6ff3322009-08-04 16:50:30 +00001042 TemplateInstantiator Instantiator(*this, TemplateArgs, Loc, Entity);
1043 return Instantiator.TransformType(T);
Douglas Gregorfe1e1102009-02-27 19:31:52 +00001044}
Douglas Gregor463421d2009-03-03 04:44:36 +00001045
John McCallb29f78f2010-04-09 17:38:44 +00001046static bool NeedsInstantiationAsFunctionType(TypeSourceInfo *T) {
Douglas Gregor5a5073e2010-05-24 17:22:01 +00001047 if (T->getType()->isDependentType() || T->getType()->isVariablyModifiedType())
John McCallb29f78f2010-04-09 17:38:44 +00001048 return true;
1049
Abramo Bagnara6d810632010-12-14 22:11:44 +00001050 TypeLoc TL = T->getTypeLoc().IgnoreParens();
John McCallb29f78f2010-04-09 17:38:44 +00001051 if (!isa<FunctionProtoTypeLoc>(TL))
1052 return false;
1053
1054 FunctionProtoTypeLoc FP = cast<FunctionProtoTypeLoc>(TL);
1055 for (unsigned I = 0, E = FP.getNumArgs(); I != E; ++I) {
1056 ParmVarDecl *P = FP.getArg(I);
1057
1058 // TODO: currently we always rebuild expressions. When we
1059 // properly get lazier about this, we should use the same
1060 // logic to avoid rebuilding prototypes here.
1061 if (P->hasInit())
1062 return true;
1063 }
1064
1065 return false;
1066}
1067
1068/// A form of SubstType intended specifically for instantiating the
1069/// type of a FunctionDecl. Its purpose is solely to force the
1070/// instantiation of default-argument expressions.
1071TypeSourceInfo *Sema::SubstFunctionDeclType(TypeSourceInfo *T,
1072 const MultiLevelTemplateArgumentList &Args,
1073 SourceLocation Loc,
1074 DeclarationName Entity) {
1075 assert(!ActiveTemplateInstantiations.empty() &&
1076 "Cannot perform an instantiation without some context on the "
1077 "instantiation stack");
1078
1079 if (!NeedsInstantiationAsFunctionType(T))
1080 return T;
1081
1082 TemplateInstantiator Instantiator(*this, Args, Loc, Entity);
1083
1084 TypeLocBuilder TLB;
1085
1086 TypeLoc TL = T->getTypeLoc();
1087 TLB.reserve(TL.getFullDataSize());
1088
John McCall31f82722010-11-12 08:19:04 +00001089 QualType Result = Instantiator.TransformType(TLB, TL);
John McCallb29f78f2010-04-09 17:38:44 +00001090 if (Result.isNull())
1091 return 0;
1092
1093 return TLB.getTypeSourceInfo(Context, Result);
1094}
1095
Douglas Gregor940bca72010-04-12 07:48:19 +00001096ParmVarDecl *Sema::SubstParmVarDecl(ParmVarDecl *OldParm,
1097 const MultiLevelTemplateArgumentList &TemplateArgs) {
1098 TypeSourceInfo *OldDI = OldParm->getTypeSourceInfo();
1099 TypeSourceInfo *NewDI = SubstType(OldDI, TemplateArgs, OldParm->getLocation(),
1100 OldParm->getDeclName());
1101 if (!NewDI)
1102 return 0;
1103
1104 if (NewDI->getType()->isVoidType()) {
1105 Diag(OldParm->getLocation(), diag::err_param_with_void_type);
1106 return 0;
1107 }
1108
1109 ParmVarDecl *NewParm = CheckParameter(Context.getTranslationUnitDecl(),
1110 NewDI, NewDI->getType(),
1111 OldParm->getIdentifier(),
1112 OldParm->getLocation(),
Douglas Gregorc4df4072010-04-19 22:54:31 +00001113 OldParm->getStorageClass(),
1114 OldParm->getStorageClassAsWritten());
Douglas Gregor940bca72010-04-12 07:48:19 +00001115 if (!NewParm)
1116 return 0;
Douglas Gregor6044d692010-05-19 17:02:24 +00001117
Douglas Gregor940bca72010-04-12 07:48:19 +00001118 // Mark the (new) default argument as uninstantiated (if any).
1119 if (OldParm->hasUninstantiatedDefaultArg()) {
1120 Expr *Arg = OldParm->getUninstantiatedDefaultArg();
1121 NewParm->setUninstantiatedDefaultArg(Arg);
Douglas Gregor758cb672010-10-12 18:23:32 +00001122 } else if (OldParm->hasUnparsedDefaultArg()) {
1123 NewParm->setUnparsedDefaultArg();
1124 UnparsedDefaultArgInstantiations[OldParm].push_back(NewParm);
Douglas Gregor940bca72010-04-12 07:48:19 +00001125 } else if (Expr *Arg = OldParm->getDefaultArg())
1126 NewParm->setUninstantiatedDefaultArg(Arg);
1127
1128 NewParm->setHasInheritedDefaultArg(OldParm->hasInheritedDefaultArg());
1129
1130 CurrentInstantiationScope->InstantiatedLocal(OldParm, NewParm);
Argyrios Kyrtzidis3816ed42010-07-19 10:14:41 +00001131 // FIXME: OldParm may come from a FunctionProtoType, in which case CurContext
1132 // can be anything, is this right ?
Fariborz Jahanian714447b2010-07-13 21:05:02 +00001133 NewParm->setDeclContext(CurContext);
Fariborz Jahaniana6c7efe2010-07-13 20:05:58 +00001134
Douglas Gregor940bca72010-04-12 07:48:19 +00001135 return NewParm;
1136}
1137
John McCall76d824f2009-08-25 22:02:44 +00001138/// \brief Perform substitution on the base class specifiers of the
1139/// given class template specialization.
Douglas Gregor463421d2009-03-03 04:44:36 +00001140///
1141/// Produces a diagnostic and returns true on error, returns false and
1142/// attaches the instantiated base classes to the class template
1143/// specialization if successful.
Mike Stump11289f42009-09-09 15:08:12 +00001144bool
John McCall76d824f2009-08-25 22:02:44 +00001145Sema::SubstBaseSpecifiers(CXXRecordDecl *Instantiation,
1146 CXXRecordDecl *Pattern,
Douglas Gregor01afeef2009-08-28 20:31:08 +00001147 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor463421d2009-03-03 04:44:36 +00001148 bool Invalid = false;
Douglas Gregor6181ded2009-05-29 18:27:38 +00001149 llvm::SmallVector<CXXBaseSpecifier*, 4> InstantiatedBases;
Mike Stump11289f42009-09-09 15:08:12 +00001150 for (ClassTemplateSpecializationDecl::base_class_iterator
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001151 Base = Pattern->bases_begin(), BaseEnd = Pattern->bases_end();
Douglas Gregor2a72edd2009-03-10 18:52:44 +00001152 Base != BaseEnd; ++Base) {
Douglas Gregor463421d2009-03-03 04:44:36 +00001153 if (!Base->getType()->isDependentType()) {
Fariborz Jahanian5c14ec32009-07-22 17:41:53 +00001154 InstantiatedBases.push_back(new (Context) CXXBaseSpecifier(*Base));
Douglas Gregor463421d2009-03-03 04:44:36 +00001155 continue;
1156 }
1157
Douglas Gregor752a5952011-01-03 22:36:02 +00001158 SourceLocation EllipsisLoc;
1159 if (Base->isPackExpansion()) {
1160 // This is a pack expansion. See whether we should expand it now, or
1161 // wait until later.
1162 llvm::SmallVector<UnexpandedParameterPack, 2> Unexpanded;
1163 collectUnexpandedParameterPacks(Base->getTypeSourceInfo()->getTypeLoc(),
1164 Unexpanded);
1165 bool ShouldExpand = false;
1166 unsigned NumExpansions = 0;
1167 if (CheckParameterPacksForExpansion(Base->getEllipsisLoc(),
1168 Base->getSourceRange(),
1169 Unexpanded.data(), Unexpanded.size(),
1170 TemplateArgs, ShouldExpand,
1171 NumExpansions)) {
Douglas Gregor752a5952011-01-03 22:36:02 +00001172 Invalid = true;
Douglas Gregor44e7df62011-01-04 00:32:56 +00001173 continue;
Douglas Gregor752a5952011-01-03 22:36:02 +00001174 }
1175
1176 // If we should expand this pack expansion now, do so.
1177 if (ShouldExpand) {
1178 for (unsigned I = 0; I != NumExpansions; ++I) {
1179 Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(*this, I);
1180
1181 TypeSourceInfo *BaseTypeLoc = SubstType(Base->getTypeSourceInfo(),
1182 TemplateArgs,
1183 Base->getSourceRange().getBegin(),
1184 DeclarationName());
1185 if (!BaseTypeLoc) {
1186 Invalid = true;
1187 continue;
1188 }
1189
1190 if (CXXBaseSpecifier *InstantiatedBase
1191 = CheckBaseSpecifier(Instantiation,
1192 Base->getSourceRange(),
1193 Base->isVirtual(),
1194 Base->getAccessSpecifierAsWritten(),
1195 BaseTypeLoc,
1196 SourceLocation()))
1197 InstantiatedBases.push_back(InstantiatedBase);
1198 else
1199 Invalid = true;
1200 }
1201
1202 continue;
1203 }
1204
1205 // The resulting base specifier will (still) be a pack expansion.
1206 EllipsisLoc = Base->getEllipsisLoc();
1207 }
1208
1209 Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(*this, -1);
Nick Lewycky19b9f952010-07-26 16:56:01 +00001210 TypeSourceInfo *BaseTypeLoc = SubstType(Base->getTypeSourceInfo(),
1211 TemplateArgs,
1212 Base->getSourceRange().getBegin(),
1213 DeclarationName());
1214 if (!BaseTypeLoc) {
Douglas Gregor463421d2009-03-03 04:44:36 +00001215 Invalid = true;
1216 continue;
1217 }
1218
1219 if (CXXBaseSpecifier *InstantiatedBase
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001220 = CheckBaseSpecifier(Instantiation,
Douglas Gregor463421d2009-03-03 04:44:36 +00001221 Base->getSourceRange(),
1222 Base->isVirtual(),
1223 Base->getAccessSpecifierAsWritten(),
Douglas Gregor752a5952011-01-03 22:36:02 +00001224 BaseTypeLoc,
1225 EllipsisLoc))
Douglas Gregor463421d2009-03-03 04:44:36 +00001226 InstantiatedBases.push_back(InstantiatedBase);
1227 else
1228 Invalid = true;
1229 }
1230
Douglas Gregor2a72edd2009-03-10 18:52:44 +00001231 if (!Invalid &&
Jay Foad7d0479f2009-05-21 09:52:38 +00001232 AttachBaseSpecifiers(Instantiation, InstantiatedBases.data(),
Douglas Gregor463421d2009-03-03 04:44:36 +00001233 InstantiatedBases.size()))
1234 Invalid = true;
1235
1236 return Invalid;
1237}
1238
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001239/// \brief Instantiate the definition of a class from a given pattern.
1240///
1241/// \param PointOfInstantiation The point of instantiation within the
1242/// source code.
1243///
1244/// \param Instantiation is the declaration whose definition is being
1245/// instantiated. This will be either a class template specialization
1246/// or a member class of a class template specialization.
1247///
1248/// \param Pattern is the pattern from which the instantiation
1249/// occurs. This will be either the declaration of a class template or
1250/// the declaration of a member class of a class template.
1251///
1252/// \param TemplateArgs The template arguments to be substituted into
1253/// the pattern.
1254///
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001255/// \param TSK the kind of implicit or explicit instantiation to perform.
Douglas Gregor8a2e6012009-08-24 15:23:48 +00001256///
1257/// \param Complain whether to complain if the class cannot be instantiated due
1258/// to the lack of a definition.
1259///
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001260/// \returns true if an error occurred, false otherwise.
1261bool
1262Sema::InstantiateClass(SourceLocation PointOfInstantiation,
1263 CXXRecordDecl *Instantiation, CXXRecordDecl *Pattern,
Douglas Gregor01afeef2009-08-28 20:31:08 +00001264 const MultiLevelTemplateArgumentList &TemplateArgs,
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001265 TemplateSpecializationKind TSK,
Douglas Gregor8a2e6012009-08-24 15:23:48 +00001266 bool Complain) {
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001267 bool Invalid = false;
John McCall87a44eb2009-08-20 01:44:21 +00001268
Mike Stump11289f42009-09-09 15:08:12 +00001269 CXXRecordDecl *PatternDef
Douglas Gregor0a5a2212010-02-11 01:04:33 +00001270 = cast_or_null<CXXRecordDecl>(Pattern->getDefinition());
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001271 if (!PatternDef) {
Douglas Gregor8a2e6012009-08-24 15:23:48 +00001272 if (!Complain) {
1273 // Say nothing
1274 } else if (Pattern == Instantiation->getInstantiatedFromMemberClass()) {
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001275 Diag(PointOfInstantiation,
1276 diag::err_implicit_instantiate_member_undefined)
1277 << Context.getTypeDeclType(Instantiation);
1278 Diag(Pattern->getLocation(), diag::note_member_of_template_here);
1279 } else {
Douglas Gregora1f49972009-05-13 00:25:59 +00001280 Diag(PointOfInstantiation, diag::err_template_instantiate_undefined)
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001281 << (TSK != TSK_ImplicitInstantiation)
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001282 << Context.getTypeDeclType(Instantiation);
1283 Diag(Pattern->getLocation(), diag::note_template_decl_here);
1284 }
1285 return true;
1286 }
1287 Pattern = PatternDef;
1288
Douglas Gregord6ba93d2009-10-15 15:54:05 +00001289 // \brief Record the point of instantiation.
1290 if (MemberSpecializationInfo *MSInfo
1291 = Instantiation->getMemberSpecializationInfo()) {
1292 MSInfo->setTemplateSpecializationKind(TSK);
1293 MSInfo->setPointOfInstantiation(PointOfInstantiation);
Douglas Gregoref6ab412009-10-27 06:26:26 +00001294 } else if (ClassTemplateSpecializationDecl *Spec
1295 = dyn_cast<ClassTemplateSpecializationDecl>(Instantiation)) {
1296 Spec->setTemplateSpecializationKind(TSK);
1297 Spec->setPointOfInstantiation(PointOfInstantiation);
Douglas Gregord6ba93d2009-10-15 15:54:05 +00001298 }
1299
Douglas Gregorf3430ae2009-03-25 21:23:52 +00001300 InstantiatingTemplate Inst(*this, PointOfInstantiation, Instantiation);
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001301 if (Inst)
1302 return true;
1303
1304 // Enter the scope of this instantiation. We don't use
1305 // PushDeclContext because we don't have a scope.
John McCall80e58cd2010-04-29 00:35:03 +00001306 ContextRAII SavedContext(*this, Instantiation);
Douglas Gregor17158422010-05-12 17:27:19 +00001307 EnterExpressionEvaluationContext EvalContext(*this,
John McCallfaf5fb42010-08-26 23:41:50 +00001308 Sema::PotentiallyEvaluated);
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001309
Douglas Gregor51121572010-03-24 01:33:17 +00001310 // If this is an instantiation of a local class, merge this local
1311 // instantiation scope with the enclosing scope. Otherwise, every
1312 // instantiation of a class has its own local instantiation scope.
1313 bool MergeWithParentScope = !Instantiation->isDefinedOutsideFunctionOrMethod();
John McCall19c1bfd2010-08-25 05:32:35 +00001314 LocalInstantiationScope Scope(*this, MergeWithParentScope);
Douglas Gregor51121572010-03-24 01:33:17 +00001315
John McCall6602bb12010-08-01 02:01:53 +00001316 // Pull attributes from the pattern onto the instantiation.
1317 InstantiateAttrs(TemplateArgs, Pattern, Instantiation);
1318
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001319 // Start the definition of this instantiation.
1320 Instantiation->startDefinition();
Douglas Gregore9029562010-05-06 00:28:52 +00001321
1322 Instantiation->setTagKind(Pattern->getTagKind());
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001323
John McCall76d824f2009-08-25 22:02:44 +00001324 // Do substitution on the base class specifiers.
1325 if (SubstBaseSpecifiers(Instantiation, Pattern, TemplateArgs))
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001326 Invalid = true;
1327
Douglas Gregor869853e2010-11-10 19:44:59 +00001328 TemplateDeclInstantiator Instantiator(*this, Instantiation, TemplateArgs);
John McCall48871652010-08-21 09:40:31 +00001329 llvm::SmallVector<Decl*, 4> Fields;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001330 for (RecordDecl::decl_iterator Member = Pattern->decls_begin(),
Mike Stump11289f42009-09-09 15:08:12 +00001331 MemberEnd = Pattern->decls_end();
Douglas Gregorbcced4e2009-04-09 21:40:53 +00001332 Member != MemberEnd; ++Member) {
Argyrios Kyrtzidis9a94d9b2010-11-04 03:18:57 +00001333 // Don't instantiate members not belonging in this semantic context.
1334 // e.g. for:
1335 // @code
1336 // template <int i> class A {
1337 // class B *g;
1338 // };
1339 // @endcode
1340 // 'class B' has the template as lexical context but semantically it is
1341 // introduced in namespace scope.
1342 if ((*Member)->getDeclContext() != Pattern)
1343 continue;
1344
Douglas Gregor869853e2010-11-10 19:44:59 +00001345 if ((*Member)->isInvalidDecl()) {
1346 Invalid = true;
1347 continue;
1348 }
1349
1350 Decl *NewMember = Instantiator.Visit(*Member);
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001351 if (NewMember) {
Eli Friedmand0e8de22009-12-07 00:22:08 +00001352 if (FieldDecl *Field = dyn_cast<FieldDecl>(NewMember))
John McCall48871652010-08-21 09:40:31 +00001353 Fields.push_back(Field);
Eli Friedmand0e8de22009-12-07 00:22:08 +00001354 else if (NewMember->isInvalidDecl())
1355 Invalid = true;
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001356 } else {
1357 // FIXME: Eventually, a NULL return will mean that one of the
Mike Stump87c57ac2009-05-16 07:39:55 +00001358 // instantiations was a semantic disaster, and we'll want to set Invalid =
1359 // true. For now, we expect to skip some members that we can't yet handle.
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001360 }
1361 }
1362
1363 // Finish checking fields.
John McCall48871652010-08-21 09:40:31 +00001364 ActOnFields(0, Instantiation->getLocation(), Instantiation,
Jay Foad7d0479f2009-05-21 09:52:38 +00001365 Fields.data(), Fields.size(), SourceLocation(), SourceLocation(),
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001366 0);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001367 CheckCompletedCXXClass(Instantiation);
Douglas Gregor3c74d412009-10-14 20:14:33 +00001368 if (Instantiation->isInvalidDecl())
1369 Invalid = true;
Douglas Gregor869853e2010-11-10 19:44:59 +00001370 else {
1371 // Instantiate any out-of-line class template partial
1372 // specializations now.
1373 for (TemplateDeclInstantiator::delayed_partial_spec_iterator
1374 P = Instantiator.delayed_partial_spec_begin(),
1375 PEnd = Instantiator.delayed_partial_spec_end();
1376 P != PEnd; ++P) {
1377 if (!Instantiator.InstantiateClassTemplatePartialSpecialization(
1378 P->first,
1379 P->second)) {
1380 Invalid = true;
1381 break;
1382 }
1383 }
1384 }
1385
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001386 // Exit the scope of this instantiation.
John McCall80e58cd2010-04-29 00:35:03 +00001387 SavedContext.pop();
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001388
Douglas Gregor88d292c2010-05-13 16:44:06 +00001389 if (!Invalid) {
Douglas Gregor28ad4b52009-05-26 20:50:29 +00001390 Consumer.HandleTagDeclDefinition(Instantiation);
1391
Douglas Gregor88d292c2010-05-13 16:44:06 +00001392 // Always emit the vtable for an explicit instantiation definition
1393 // of a polymorphic class template specialization.
1394 if (TSK == TSK_ExplicitInstantiationDefinition)
1395 MarkVTableUsed(PointOfInstantiation, Instantiation, true);
1396 }
1397
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001398 return Invalid;
1399}
1400
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +00001401namespace {
1402 /// \brief A partial specialization whose template arguments have matched
1403 /// a given template-id.
1404 struct PartialSpecMatchResult {
1405 ClassTemplatePartialSpecializationDecl *Partial;
1406 TemplateArgumentList *Args;
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +00001407 };
1408}
1409
Mike Stump11289f42009-09-09 15:08:12 +00001410bool
Douglas Gregor463421d2009-03-03 04:44:36 +00001411Sema::InstantiateClassTemplateSpecialization(
Douglas Gregoref6ab412009-10-27 06:26:26 +00001412 SourceLocation PointOfInstantiation,
Douglas Gregor463421d2009-03-03 04:44:36 +00001413 ClassTemplateSpecializationDecl *ClassTemplateSpec,
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001414 TemplateSpecializationKind TSK,
Douglas Gregor8a2e6012009-08-24 15:23:48 +00001415 bool Complain) {
Douglas Gregor463421d2009-03-03 04:44:36 +00001416 // Perform the actual instantiation on the canonical declaration.
1417 ClassTemplateSpec = cast<ClassTemplateSpecializationDecl>(
Argyrios Kyrtzidis6b7e3762009-07-18 00:34:25 +00001418 ClassTemplateSpec->getCanonicalDecl());
Douglas Gregor463421d2009-03-03 04:44:36 +00001419
Douglas Gregor4aa04b12009-09-11 21:19:12 +00001420 // Check whether we have already instantiated or specialized this class
1421 // template specialization.
1422 if (ClassTemplateSpec->getSpecializationKind() != TSK_Undeclared) {
1423 if (ClassTemplateSpec->getSpecializationKind() ==
1424 TSK_ExplicitInstantiationDeclaration &&
1425 TSK == TSK_ExplicitInstantiationDefinition) {
1426 // An explicit instantiation definition follows an explicit instantiation
1427 // declaration (C++0x [temp.explicit]p10); go ahead and perform the
1428 // explicit instantiation.
1429 ClassTemplateSpec->setSpecializationKind(TSK);
Douglas Gregor88d292c2010-05-13 16:44:06 +00001430
1431 // If this is an explicit instantiation definition, mark the
1432 // vtable as used.
1433 if (TSK == TSK_ExplicitInstantiationDefinition)
1434 MarkVTableUsed(PointOfInstantiation, ClassTemplateSpec, true);
1435
Douglas Gregor4aa04b12009-09-11 21:19:12 +00001436 return false;
1437 }
1438
1439 // We can only instantiate something that hasn't already been
1440 // instantiated or specialized. Fail without any diagnostics: our
1441 // caller will provide an error message.
Douglas Gregor463421d2009-03-03 04:44:36 +00001442 return true;
Douglas Gregor4aa04b12009-09-11 21:19:12 +00001443 }
Douglas Gregor463421d2009-03-03 04:44:36 +00001444
Douglas Gregor00a511f2009-09-15 16:51:42 +00001445 if (ClassTemplateSpec->isInvalidDecl())
1446 return true;
1447
Douglas Gregor463421d2009-03-03 04:44:36 +00001448 ClassTemplateDecl *Template = ClassTemplateSpec->getSpecializedTemplate();
Douglas Gregor01afeef2009-08-28 20:31:08 +00001449 CXXRecordDecl *Pattern = 0;
Douglas Gregor2373c592009-05-31 09:31:02 +00001450
Douglas Gregor170bc422009-06-12 22:31:52 +00001451 // C++ [temp.class.spec.match]p1:
1452 // When a class template is used in a context that requires an
1453 // instantiation of the class, it is necessary to determine
1454 // whether the instantiation is to be generated using the primary
1455 // template or one of the partial specializations. This is done by
1456 // matching the template arguments of the class template
1457 // specialization with the template argument lists of the partial
1458 // specializations.
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +00001459 typedef PartialSpecMatchResult MatchResult;
Douglas Gregorb7ae10f2009-06-05 00:53:49 +00001460 llvm::SmallVector<MatchResult, 4> Matched;
Douglas Gregor407e9612010-04-30 05:56:50 +00001461 llvm::SmallVector<ClassTemplatePartialSpecializationDecl *, 4> PartialSpecs;
1462 Template->getPartialSpecializations(PartialSpecs);
1463 for (unsigned I = 0, N = PartialSpecs.size(); I != N; ++I) {
1464 ClassTemplatePartialSpecializationDecl *Partial = PartialSpecs[I];
John McCallbc077cf2010-02-08 23:07:23 +00001465 TemplateDeductionInfo Info(Context, PointOfInstantiation);
Douglas Gregor181aa4a2009-06-12 18:26:56 +00001466 if (TemplateDeductionResult Result
Douglas Gregor407e9612010-04-30 05:56:50 +00001467 = DeduceTemplateArguments(Partial,
Douglas Gregor181aa4a2009-06-12 18:26:56 +00001468 ClassTemplateSpec->getTemplateArgs(),
1469 Info)) {
1470 // FIXME: Store the failed-deduction information for use in
1471 // diagnostics, later.
1472 (void)Result;
1473 } else {
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +00001474 Matched.push_back(PartialSpecMatchResult());
1475 Matched.back().Partial = Partial;
1476 Matched.back().Args = Info.take();
Douglas Gregor181aa4a2009-06-12 18:26:56 +00001477 }
Douglas Gregor2373c592009-05-31 09:31:02 +00001478 }
1479
Douglas Gregor21610382009-10-29 00:04:11 +00001480 if (Matched.size() >= 1) {
Douglas Gregorbe999392009-09-15 16:23:51 +00001481 llvm::SmallVector<MatchResult, 4>::iterator Best = Matched.begin();
Douglas Gregor21610382009-10-29 00:04:11 +00001482 if (Matched.size() == 1) {
1483 // -- If exactly one matching specialization is found, the
1484 // instantiation is generated from that specialization.
1485 // We don't need to do anything for this.
1486 } else {
1487 // -- If more than one matching specialization is found, the
1488 // partial order rules (14.5.4.2) are used to determine
1489 // whether one of the specializations is more specialized
1490 // than the others. If none of the specializations is more
1491 // specialized than all of the other matching
1492 // specializations, then the use of the class template is
1493 // ambiguous and the program is ill-formed.
1494 for (llvm::SmallVector<MatchResult, 4>::iterator P = Best + 1,
1495 PEnd = Matched.end();
1496 P != PEnd; ++P) {
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +00001497 if (getMoreSpecializedPartialSpecialization(P->Partial, Best->Partial,
John McCallbc077cf2010-02-08 23:07:23 +00001498 PointOfInstantiation)
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +00001499 == P->Partial)
Douglas Gregor21610382009-10-29 00:04:11 +00001500 Best = P;
Douglas Gregorbe999392009-09-15 16:23:51 +00001501 }
Douglas Gregorbe999392009-09-15 16:23:51 +00001502
Douglas Gregor21610382009-10-29 00:04:11 +00001503 // Determine if the best partial specialization is more specialized than
1504 // the others.
1505 bool Ambiguous = false;
Douglas Gregorbe999392009-09-15 16:23:51 +00001506 for (llvm::SmallVector<MatchResult, 4>::iterator P = Matched.begin(),
1507 PEnd = Matched.end();
Douglas Gregor21610382009-10-29 00:04:11 +00001508 P != PEnd; ++P) {
1509 if (P != Best &&
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +00001510 getMoreSpecializedPartialSpecialization(P->Partial, Best->Partial,
John McCallbc077cf2010-02-08 23:07:23 +00001511 PointOfInstantiation)
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +00001512 != Best->Partial) {
Douglas Gregor21610382009-10-29 00:04:11 +00001513 Ambiguous = true;
1514 break;
1515 }
1516 }
1517
1518 if (Ambiguous) {
1519 // Partial ordering did not produce a clear winner. Complain.
1520 ClassTemplateSpec->setInvalidDecl();
1521 Diag(PointOfInstantiation, diag::err_partial_spec_ordering_ambiguous)
1522 << ClassTemplateSpec;
1523
1524 // Print the matching partial specializations.
1525 for (llvm::SmallVector<MatchResult, 4>::iterator P = Matched.begin(),
1526 PEnd = Matched.end();
1527 P != PEnd; ++P)
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +00001528 Diag(P->Partial->getLocation(), diag::note_partial_spec_match)
1529 << getTemplateArgumentBindingsText(
1530 P->Partial->getTemplateParameters(),
1531 *P->Args);
Douglas Gregor01afeef2009-08-28 20:31:08 +00001532
Douglas Gregor21610382009-10-29 00:04:11 +00001533 return true;
1534 }
Douglas Gregorbe999392009-09-15 16:23:51 +00001535 }
1536
1537 // Instantiate using the best class template partial specialization.
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +00001538 ClassTemplatePartialSpecializationDecl *OrigPartialSpec = Best->Partial;
Douglas Gregor21610382009-10-29 00:04:11 +00001539 while (OrigPartialSpec->getInstantiatedFromMember()) {
1540 // If we've found an explicit specialization of this class template,
1541 // stop here and use that as the pattern.
1542 if (OrigPartialSpec->isMemberSpecialization())
1543 break;
1544
1545 OrigPartialSpec = OrigPartialSpec->getInstantiatedFromMember();
1546 }
1547
1548 Pattern = OrigPartialSpec;
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +00001549 ClassTemplateSpec->setInstantiationOf(Best->Partial, Best->Args);
Douglas Gregor170bc422009-06-12 22:31:52 +00001550 } else {
1551 // -- If no matches are found, the instantiation is generated
1552 // from the primary template.
Douglas Gregor01afeef2009-08-28 20:31:08 +00001553 ClassTemplateDecl *OrigTemplate = Template;
Douglas Gregorcf915552009-10-13 16:30:37 +00001554 while (OrigTemplate->getInstantiatedFromMemberTemplate()) {
1555 // If we've found an explicit specialization of this class template,
1556 // stop here and use that as the pattern.
1557 if (OrigTemplate->isMemberSpecialization())
1558 break;
1559
Douglas Gregor01afeef2009-08-28 20:31:08 +00001560 OrigTemplate = OrigTemplate->getInstantiatedFromMemberTemplate();
Douglas Gregorcf915552009-10-13 16:30:37 +00001561 }
1562
Douglas Gregor01afeef2009-08-28 20:31:08 +00001563 Pattern = OrigTemplate->getTemplatedDecl();
Douglas Gregor2373c592009-05-31 09:31:02 +00001564 }
Douglas Gregor463421d2009-03-03 04:44:36 +00001565
Douglas Gregoref6ab412009-10-27 06:26:26 +00001566 bool Result = InstantiateClass(PointOfInstantiation, ClassTemplateSpec,
1567 Pattern,
1568 getTemplateInstantiationArgs(ClassTemplateSpec),
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001569 TSK,
Douglas Gregor8a2e6012009-08-24 15:23:48 +00001570 Complain);
Mike Stump11289f42009-09-09 15:08:12 +00001571
Douglas Gregorb7ae10f2009-06-05 00:53:49 +00001572 return Result;
Douglas Gregor463421d2009-03-03 04:44:36 +00001573}
Douglas Gregor90a1a652009-03-19 17:26:29 +00001574
John McCall76d824f2009-08-25 22:02:44 +00001575/// \brief Instantiates the definitions of all of the member
1576/// of the given class, which is an instantiation of a class template
1577/// or a member class of a template.
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00001578void
1579Sema::InstantiateClassMembers(SourceLocation PointOfInstantiation,
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001580 CXXRecordDecl *Instantiation,
1581 const MultiLevelTemplateArgumentList &TemplateArgs,
1582 TemplateSpecializationKind TSK) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001583 for (DeclContext::decl_iterator D = Instantiation->decls_begin(),
1584 DEnd = Instantiation->decls_end();
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00001585 D != DEnd; ++D) {
Douglas Gregor1d957a32009-10-27 18:42:08 +00001586 bool SuppressNew = false;
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00001587 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(*D)) {
Douglas Gregor1d957a32009-10-27 18:42:08 +00001588 if (FunctionDecl *Pattern
1589 = Function->getInstantiatedFromMemberFunction()) {
1590 MemberSpecializationInfo *MSInfo
1591 = Function->getMemberSpecializationInfo();
1592 assert(MSInfo && "No member specialization information?");
Douglas Gregor06aa50412010-04-09 21:02:29 +00001593 if (MSInfo->getTemplateSpecializationKind()
1594 == TSK_ExplicitSpecialization)
1595 continue;
1596
Douglas Gregor1d957a32009-10-27 18:42:08 +00001597 if (CheckSpecializationInstantiationRedecl(PointOfInstantiation, TSK,
1598 Function,
1599 MSInfo->getTemplateSpecializationKind(),
Nico Weberd75488d2010-09-27 21:02:09 +00001600 MSInfo->getPointOfInstantiation(),
Douglas Gregor1d957a32009-10-27 18:42:08 +00001601 SuppressNew) ||
1602 SuppressNew)
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001603 continue;
1604
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001605 if (Function->hasBody())
Douglas Gregor1d957a32009-10-27 18:42:08 +00001606 continue;
1607
1608 if (TSK == TSK_ExplicitInstantiationDefinition) {
1609 // C++0x [temp.explicit]p8:
1610 // An explicit instantiation definition that names a class template
1611 // specialization explicitly instantiates the class template
1612 // specialization and is only an explicit instantiation definition
1613 // of members whose definition is visible at the point of
1614 // instantiation.
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001615 if (!Pattern->hasBody())
Douglas Gregor1d957a32009-10-27 18:42:08 +00001616 continue;
1617
1618 Function->setTemplateSpecializationKind(TSK, PointOfInstantiation);
1619
1620 InstantiateFunctionDefinition(PointOfInstantiation, Function);
1621 } else {
1622 Function->setTemplateSpecializationKind(TSK, PointOfInstantiation);
1623 }
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001624 }
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00001625 } else if (VarDecl *Var = dyn_cast<VarDecl>(*D)) {
Douglas Gregor86d142a2009-10-08 07:24:58 +00001626 if (Var->isStaticDataMember()) {
Douglas Gregor1d957a32009-10-27 18:42:08 +00001627 MemberSpecializationInfo *MSInfo = Var->getMemberSpecializationInfo();
1628 assert(MSInfo && "No member specialization information?");
Douglas Gregor06aa50412010-04-09 21:02:29 +00001629 if (MSInfo->getTemplateSpecializationKind()
1630 == TSK_ExplicitSpecialization)
1631 continue;
1632
Douglas Gregor1d957a32009-10-27 18:42:08 +00001633 if (CheckSpecializationInstantiationRedecl(PointOfInstantiation, TSK,
1634 Var,
1635 MSInfo->getTemplateSpecializationKind(),
Nico Weberd75488d2010-09-27 21:02:09 +00001636 MSInfo->getPointOfInstantiation(),
Douglas Gregor1d957a32009-10-27 18:42:08 +00001637 SuppressNew) ||
1638 SuppressNew)
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001639 continue;
1640
Douglas Gregor1d957a32009-10-27 18:42:08 +00001641 if (TSK == TSK_ExplicitInstantiationDefinition) {
1642 // C++0x [temp.explicit]p8:
1643 // An explicit instantiation definition that names a class template
1644 // specialization explicitly instantiates the class template
1645 // specialization and is only an explicit instantiation definition
1646 // of members whose definition is visible at the point of
1647 // instantiation.
1648 if (!Var->getInstantiatedFromStaticDataMember()
1649 ->getOutOfLineDefinition())
1650 continue;
1651
1652 Var->setTemplateSpecializationKind(TSK, PointOfInstantiation);
Douglas Gregor86d142a2009-10-08 07:24:58 +00001653 InstantiateStaticDataMemberDefinition(PointOfInstantiation, Var);
Douglas Gregor1d957a32009-10-27 18:42:08 +00001654 } else {
1655 Var->setTemplateSpecializationKind(TSK, PointOfInstantiation);
1656 }
1657 }
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00001658 } else if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(*D)) {
Douglas Gregor1da22252010-04-18 18:11:38 +00001659 // Always skip the injected-class-name, along with any
1660 // redeclarations of nested classes, since both would cause us
1661 // to try to instantiate the members of a class twice.
1662 if (Record->isInjectedClassName() || Record->getPreviousDeclaration())
Douglas Gregord801b062009-10-07 23:56:10 +00001663 continue;
1664
Douglas Gregor1d957a32009-10-27 18:42:08 +00001665 MemberSpecializationInfo *MSInfo = Record->getMemberSpecializationInfo();
1666 assert(MSInfo && "No member specialization information?");
Douglas Gregor06aa50412010-04-09 21:02:29 +00001667
1668 if (MSInfo->getTemplateSpecializationKind()
1669 == TSK_ExplicitSpecialization)
1670 continue;
Nico Weberd75488d2010-09-27 21:02:09 +00001671
Douglas Gregor1d957a32009-10-27 18:42:08 +00001672 if (CheckSpecializationInstantiationRedecl(PointOfInstantiation, TSK,
1673 Record,
1674 MSInfo->getTemplateSpecializationKind(),
Nico Weberd75488d2010-09-27 21:02:09 +00001675 MSInfo->getPointOfInstantiation(),
Douglas Gregor1d957a32009-10-27 18:42:08 +00001676 SuppressNew) ||
1677 SuppressNew)
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001678 continue;
1679
Douglas Gregor1d957a32009-10-27 18:42:08 +00001680 CXXRecordDecl *Pattern = Record->getInstantiatedFromMemberClass();
1681 assert(Pattern && "Missing instantiated-from-template information");
1682
Douglas Gregor0a5a2212010-02-11 01:04:33 +00001683 if (!Record->getDefinition()) {
1684 if (!Pattern->getDefinition()) {
Douglas Gregor1d957a32009-10-27 18:42:08 +00001685 // C++0x [temp.explicit]p8:
1686 // An explicit instantiation definition that names a class template
1687 // specialization explicitly instantiates the class template
1688 // specialization and is only an explicit instantiation definition
1689 // of members whose definition is visible at the point of
1690 // instantiation.
1691 if (TSK == TSK_ExplicitInstantiationDeclaration) {
1692 MSInfo->setTemplateSpecializationKind(TSK);
1693 MSInfo->setPointOfInstantiation(PointOfInstantiation);
1694 }
1695
1696 continue;
1697 }
1698
1699 InstantiateClass(PointOfInstantiation, Record, Pattern,
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001700 TemplateArgs,
1701 TSK);
Nico Weberd75488d2010-09-27 21:02:09 +00001702 } else {
1703 if (TSK == TSK_ExplicitInstantiationDefinition &&
1704 Record->getTemplateSpecializationKind() ==
1705 TSK_ExplicitInstantiationDeclaration) {
1706 Record->setTemplateSpecializationKind(TSK);
1707 MarkVTableUsed(PointOfInstantiation, Record, true);
1708 }
Douglas Gregor1d957a32009-10-27 18:42:08 +00001709 }
Douglas Gregorc093c1d2009-10-08 01:19:17 +00001710
Douglas Gregor0a5a2212010-02-11 01:04:33 +00001711 Pattern = cast_or_null<CXXRecordDecl>(Record->getDefinition());
Douglas Gregor1d957a32009-10-27 18:42:08 +00001712 if (Pattern)
1713 InstantiateClassMembers(PointOfInstantiation, Pattern, TemplateArgs,
1714 TSK);
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00001715 }
1716 }
1717}
1718
1719/// \brief Instantiate the definitions of all of the members of the
1720/// given class template specialization, which was named as part of an
1721/// explicit instantiation.
Mike Stump11289f42009-09-09 15:08:12 +00001722void
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001723Sema::InstantiateClassTemplateSpecializationMembers(
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00001724 SourceLocation PointOfInstantiation,
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001725 ClassTemplateSpecializationDecl *ClassTemplateSpec,
1726 TemplateSpecializationKind TSK) {
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00001727 // C++0x [temp.explicit]p7:
1728 // An explicit instantiation that names a class template
1729 // specialization is an explicit instantion of the same kind
1730 // (declaration or definition) of each of its members (not
1731 // including members inherited from base classes) that has not
1732 // been previously explicitly specialized in the translation unit
1733 // containing the explicit instantiation, except as described
1734 // below.
1735 InstantiateClassMembers(PointOfInstantiation, ClassTemplateSpec,
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001736 getTemplateInstantiationArgs(ClassTemplateSpec),
1737 TSK);
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00001738}
1739
John McCalldadc5752010-08-24 06:29:42 +00001740StmtResult
Douglas Gregor01afeef2009-08-28 20:31:08 +00001741Sema::SubstStmt(Stmt *S, const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregorebe10102009-08-20 07:17:43 +00001742 if (!S)
1743 return Owned(S);
1744
1745 TemplateInstantiator Instantiator(*this, TemplateArgs,
1746 SourceLocation(),
1747 DeclarationName());
1748 return Instantiator.TransformStmt(S);
1749}
1750
John McCalldadc5752010-08-24 06:29:42 +00001751ExprResult
Douglas Gregor01afeef2009-08-28 20:31:08 +00001752Sema::SubstExpr(Expr *E, const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregora16548e2009-08-11 05:31:07 +00001753 if (!E)
1754 return Owned(E);
Mike Stump11289f42009-09-09 15:08:12 +00001755
Douglas Gregora16548e2009-08-11 05:31:07 +00001756 TemplateInstantiator Instantiator(*this, TemplateArgs,
1757 SourceLocation(),
1758 DeclarationName());
1759 return Instantiator.TransformExpr(E);
1760}
1761
John McCall76d824f2009-08-25 22:02:44 +00001762/// \brief Do template substitution on a nested-name-specifier.
Douglas Gregorf21eb492009-03-26 23:50:42 +00001763NestedNameSpecifier *
John McCall76d824f2009-08-25 22:02:44 +00001764Sema::SubstNestedNameSpecifier(NestedNameSpecifier *NNS,
Douglas Gregor01afeef2009-08-28 20:31:08 +00001765 SourceRange Range,
1766 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor1135c352009-08-06 05:28:30 +00001767 TemplateInstantiator Instantiator(*this, TemplateArgs, Range.getBegin(),
1768 DeclarationName());
Douglas Gregorcd3f49f2010-02-25 04:46:04 +00001769 return Instantiator.TransformNestedNameSpecifier(NNS, Range);
Douglas Gregor90a1a652009-03-19 17:26:29 +00001770}
Douglas Gregoraa594892009-03-31 18:38:02 +00001771
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001772/// \brief Do template substitution on declaration name info.
1773DeclarationNameInfo
1774Sema::SubstDeclarationNameInfo(const DeclarationNameInfo &NameInfo,
1775 const MultiLevelTemplateArgumentList &TemplateArgs) {
1776 TemplateInstantiator Instantiator(*this, TemplateArgs, NameInfo.getLoc(),
1777 NameInfo.getName());
1778 return Instantiator.TransformDeclarationNameInfo(NameInfo);
1779}
1780
Douglas Gregoraa594892009-03-31 18:38:02 +00001781TemplateName
John McCall76d824f2009-08-25 22:02:44 +00001782Sema::SubstTemplateName(TemplateName Name, SourceLocation Loc,
Douglas Gregor01afeef2009-08-28 20:31:08 +00001783 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor71dc5092009-08-06 06:41:21 +00001784 TemplateInstantiator Instantiator(*this, TemplateArgs, Loc,
1785 DeclarationName());
1786 return Instantiator.TransformTemplateName(Name);
Douglas Gregoraa594892009-03-31 18:38:02 +00001787}
Douglas Gregorc43620d2009-06-11 00:06:24 +00001788
Douglas Gregor0f3feb42010-12-22 21:19:48 +00001789bool Sema::Subst(const TemplateArgumentLoc *Args, unsigned NumArgs,
1790 TemplateArgumentListInfo &Result,
John McCall0ad16662009-10-29 08:12:44 +00001791 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregore922c772009-08-04 22:27:00 +00001792 TemplateInstantiator Instantiator(*this, TemplateArgs, SourceLocation(),
1793 DeclarationName());
Douglas Gregor0f3feb42010-12-22 21:19:48 +00001794
1795 return Instantiator.TransformTemplateArguments(Args, NumArgs, Result);
Douglas Gregorc43620d2009-06-11 00:06:24 +00001796}
Douglas Gregor14cf7522010-04-30 18:55:50 +00001797
John McCall19c1bfd2010-08-25 05:32:35 +00001798Decl *LocalInstantiationScope::getInstantiationOf(const Decl *D) {
Douglas Gregor14cf7522010-04-30 18:55:50 +00001799 for (LocalInstantiationScope *Current = this; Current;
1800 Current = Current->Outer) {
1801 // Check if we found something within this scope.
Douglas Gregore9fc8dc2010-12-21 21:22:51 +00001802 const Decl *CheckD = D;
1803 do {
1804 llvm::DenseMap<const Decl *, Decl *>::iterator Found
1805 = Current->LocalDecls.find(CheckD);
1806 if (Found != Current->LocalDecls.end())
1807 return Found->second;
1808
1809 // If this is a tag declaration, it's possible that we need to look for
1810 // a previous declaration.
1811 if (const TagDecl *Tag = dyn_cast<TagDecl>(CheckD))
1812 CheckD = Tag->getPreviousDeclaration();
1813 else
1814 CheckD = 0;
1815 } while (CheckD);
1816
Douglas Gregor14cf7522010-04-30 18:55:50 +00001817 // If we aren't combined with our outer scope, we're done.
1818 if (!Current->CombineWithOuterScope)
1819 break;
1820 }
1821
1822 assert(D->isInvalidDecl() &&
1823 "declaration was not instantiated in this scope!");
1824 return 0;
1825}
1826
John McCall19c1bfd2010-08-25 05:32:35 +00001827void LocalInstantiationScope::InstantiatedLocal(const Decl *D, Decl *Inst) {
Douglas Gregor14cf7522010-04-30 18:55:50 +00001828 Decl *&Stored = LocalDecls[D];
1829 assert((!Stored || Stored == Inst)&& "Already instantiated this local");
1830 Stored = Inst;
1831}