blob: 7dab4bd8bd55b67f358d9bfef8a0a9bf5d17af28 [file] [log] [blame]
Douglas Gregorfe1e1102009-02-27 19:31:52 +00001//===------- SemaTemplateInstantiate.cpp - C++ Template Instantiation ------===/
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//===----------------------------------------------------------------------===/
8//
9// This file implements C++ template instantiation.
10//
11//===----------------------------------------------------------------------===/
12
13#include "Sema.h"
Douglas Gregord6ff3322009-08-04 16:50:30 +000014#include "TreeTransform.h"
John McCallb53bbd42009-11-22 01:44:31 +000015#include "Lookup.h"
Douglas Gregor28ad4b52009-05-26 20:50:29 +000016#include "clang/AST/ASTConsumer.h"
Douglas Gregorfe1e1102009-02-27 19:31:52 +000017#include "clang/AST/ASTContext.h"
18#include "clang/AST/Expr.h"
Douglas Gregorfe1e1102009-02-27 19:31:52 +000019#include "clang/AST/DeclTemplate.h"
20#include "clang/Parse/DeclSpec.h"
21#include "clang/Basic/LangOptions.h"
22
23using namespace clang;
24
Douglas Gregor4ea568f2009-03-10 18:03:33 +000025//===----------------------------------------------------------------------===/
26// Template Instantiation Support
27//===----------------------------------------------------------------------===/
28
Douglas Gregor01afeef2009-08-28 20:31:08 +000029/// \brief Retrieve the template argument list(s) that should be used to
30/// instantiate the definition of the given declaration.
Douglas Gregor36d7c5f2009-11-09 19:17:50 +000031///
32/// \param D the declaration for which we are computing template instantiation
33/// arguments.
34///
35/// \param Innermost if non-NULL, the innermost template argument list.
Douglas Gregor8c702532010-02-05 07:33:43 +000036///
37/// \param RelativeToPrimary true if we should get the template
38/// arguments relative to the primary template, even when we're
39/// dealing with a specialization. This is only relevant for function
40/// template specializations.
Douglas Gregor1bd7a942010-05-03 23:29:10 +000041///
42/// \param Pattern If non-NULL, indicates the pattern from which we will be
43/// instantiating the definition of the given declaration, \p D. This is
44/// used to determine the proper set of template instantiation arguments for
45/// friend function template specializations.
Douglas Gregora654dd82009-08-28 17:37:35 +000046MultiLevelTemplateArgumentList
Douglas Gregor36d7c5f2009-11-09 19:17:50 +000047Sema::getTemplateInstantiationArgs(NamedDecl *D,
Douglas Gregor8c702532010-02-05 07:33:43 +000048 const TemplateArgumentList *Innermost,
Douglas Gregor1bd7a942010-05-03 23:29:10 +000049 bool RelativeToPrimary,
50 const FunctionDecl *Pattern) {
Douglas Gregora654dd82009-08-28 17:37:35 +000051 // Accumulate the set of template argument lists in this structure.
52 MultiLevelTemplateArgumentList Result;
Mike Stump11289f42009-09-09 15:08:12 +000053
Douglas Gregor36d7c5f2009-11-09 19:17:50 +000054 if (Innermost)
55 Result.addOuterTemplateArguments(Innermost);
56
Douglas Gregora654dd82009-08-28 17:37:35 +000057 DeclContext *Ctx = dyn_cast<DeclContext>(D);
58 if (!Ctx)
59 Ctx = D->getDeclContext();
Mike Stump11289f42009-09-09 15:08:12 +000060
John McCall970d5302009-08-29 03:16:09 +000061 while (!Ctx->isFileContext()) {
Douglas Gregora654dd82009-08-28 17:37:35 +000062 // Add template arguments from a class template instantiation.
Mike Stump11289f42009-09-09 15:08:12 +000063 if (ClassTemplateSpecializationDecl *Spec
Douglas Gregora654dd82009-08-28 17:37:35 +000064 = dyn_cast<ClassTemplateSpecializationDecl>(Ctx)) {
65 // We're done when we hit an explicit specialization.
66 if (Spec->getSpecializationKind() == TSK_ExplicitSpecialization)
67 break;
Mike Stump11289f42009-09-09 15:08:12 +000068
Douglas Gregora654dd82009-08-28 17:37:35 +000069 Result.addOuterTemplateArguments(&Spec->getTemplateInstantiationArgs());
Douglas Gregorcf915552009-10-13 16:30:37 +000070
71 // If this class template specialization was instantiated from a
72 // specialized member that is a class template, we're done.
73 assert(Spec->getSpecializedTemplate() && "No class template?");
74 if (Spec->getSpecializedTemplate()->isMemberSpecialization())
75 break;
Mike Stump11289f42009-09-09 15:08:12 +000076 }
Douglas Gregora654dd82009-08-28 17:37:35 +000077 // Add template arguments from a function template specialization.
John McCall970d5302009-08-29 03:16:09 +000078 else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Ctx)) {
Douglas Gregor8c702532010-02-05 07:33:43 +000079 if (!RelativeToPrimary &&
80 Function->getTemplateSpecializationKind()
81 == TSK_ExplicitSpecialization)
Douglas Gregorcf915552009-10-13 16:30:37 +000082 break;
83
Douglas Gregora654dd82009-08-28 17:37:35 +000084 if (const TemplateArgumentList *TemplateArgs
Douglas Gregorcf915552009-10-13 16:30:37 +000085 = Function->getTemplateSpecializationArgs()) {
86 // Add the template arguments for this specialization.
Douglas Gregora654dd82009-08-28 17:37:35 +000087 Result.addOuterTemplateArguments(TemplateArgs);
John McCall970d5302009-08-29 03:16:09 +000088
Douglas Gregorcf915552009-10-13 16:30:37 +000089 // If this function was instantiated from a specialized member that is
90 // a function template, we're done.
91 assert(Function->getPrimaryTemplate() && "No function template?");
92 if (Function->getPrimaryTemplate()->isMemberSpecialization())
93 break;
94 }
95
John McCall970d5302009-08-29 03:16:09 +000096 // If this is a friend declaration and it declares an entity at
97 // namespace scope, take arguments from its lexical parent
Douglas Gregor1bd7a942010-05-03 23:29:10 +000098 // instead of its semantic parent, unless of course the pattern we're
99 // instantiating actually comes from the file's context!
John McCall970d5302009-08-29 03:16:09 +0000100 if (Function->getFriendObjectKind() &&
Douglas Gregor1bd7a942010-05-03 23:29:10 +0000101 Function->getDeclContext()->isFileContext() &&
102 (!Pattern || !Pattern->getLexicalDeclContext()->isFileContext())) {
John McCall970d5302009-08-29 03:16:09 +0000103 Ctx = Function->getLexicalDeclContext();
Douglas Gregor8c702532010-02-05 07:33:43 +0000104 RelativeToPrimary = false;
John McCall970d5302009-08-29 03:16:09 +0000105 continue;
106 }
Douglas Gregora654dd82009-08-28 17:37:35 +0000107 }
John McCall970d5302009-08-29 03:16:09 +0000108
109 Ctx = Ctx->getParent();
Douglas Gregor8c702532010-02-05 07:33:43 +0000110 RelativeToPrimary = false;
Douglas Gregorb4850462009-05-14 23:26:13 +0000111 }
Mike Stump11289f42009-09-09 15:08:12 +0000112
Douglas Gregora654dd82009-08-28 17:37:35 +0000113 return Result;
Douglas Gregorb4850462009-05-14 23:26:13 +0000114}
115
Douglas Gregor84d49a22009-11-11 21:54:23 +0000116bool Sema::ActiveTemplateInstantiation::isInstantiationRecord() const {
117 switch (Kind) {
118 case TemplateInstantiation:
119 case DefaultTemplateArgumentInstantiation:
120 case DefaultFunctionArgumentInstantiation:
121 return true;
122
123 case ExplicitTemplateArgumentSubstitution:
124 case DeducedTemplateArgumentSubstitution:
125 case PriorTemplateArgumentSubstitution:
126 case DefaultTemplateArgumentChecking:
127 return false;
128 }
129
130 return true;
131}
132
Douglas Gregorfcd5db32009-03-10 00:06:19 +0000133Sema::InstantiatingTemplate::
134InstantiatingTemplate(Sema &SemaRef, SourceLocation PointOfInstantiation,
Douglas Gregor85673582009-05-18 17:01:57 +0000135 Decl *Entity,
Douglas Gregorfcd5db32009-03-10 00:06:19 +0000136 SourceRange InstantiationRange)
137 : SemaRef(SemaRef) {
Douglas Gregor79cf6032009-03-10 20:44:00 +0000138
139 Invalid = CheckInstantiationDepth(PointOfInstantiation,
140 InstantiationRange);
141 if (!Invalid) {
Douglas Gregorfcd5db32009-03-10 00:06:19 +0000142 ActiveTemplateInstantiation Inst;
Douglas Gregor79cf6032009-03-10 20:44:00 +0000143 Inst.Kind = ActiveTemplateInstantiation::TemplateInstantiation;
Douglas Gregorfcd5db32009-03-10 00:06:19 +0000144 Inst.PointOfInstantiation = PointOfInstantiation;
Douglas Gregor79cf6032009-03-10 20:44:00 +0000145 Inst.Entity = reinterpret_cast<uintptr_t>(Entity);
Douglas Gregorc9220832009-03-12 18:36:18 +0000146 Inst.TemplateArgs = 0;
147 Inst.NumTemplateArgs = 0;
Douglas Gregor79cf6032009-03-10 20:44:00 +0000148 Inst.InstantiationRange = InstantiationRange;
149 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
Douglas Gregor79cf6032009-03-10 20:44:00 +0000150 }
151}
152
Mike Stump11289f42009-09-09 15:08:12 +0000153Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef,
Douglas Gregor79cf6032009-03-10 20:44:00 +0000154 SourceLocation PointOfInstantiation,
155 TemplateDecl *Template,
156 const TemplateArgument *TemplateArgs,
157 unsigned NumTemplateArgs,
158 SourceRange InstantiationRange)
159 : SemaRef(SemaRef) {
160
161 Invalid = CheckInstantiationDepth(PointOfInstantiation,
162 InstantiationRange);
163 if (!Invalid) {
164 ActiveTemplateInstantiation Inst;
Mike Stump11289f42009-09-09 15:08:12 +0000165 Inst.Kind
Douglas Gregor79cf6032009-03-10 20:44:00 +0000166 = ActiveTemplateInstantiation::DefaultTemplateArgumentInstantiation;
167 Inst.PointOfInstantiation = PointOfInstantiation;
168 Inst.Entity = reinterpret_cast<uintptr_t>(Template);
169 Inst.TemplateArgs = TemplateArgs;
170 Inst.NumTemplateArgs = NumTemplateArgs;
Douglas Gregorfcd5db32009-03-10 00:06:19 +0000171 Inst.InstantiationRange = InstantiationRange;
172 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
Douglas Gregorfcd5db32009-03-10 00:06:19 +0000173 }
174}
175
Mike Stump11289f42009-09-09 15:08:12 +0000176Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef,
Douglas Gregor637d9982009-06-10 23:47:09 +0000177 SourceLocation PointOfInstantiation,
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000178 FunctionTemplateDecl *FunctionTemplate,
179 const TemplateArgument *TemplateArgs,
180 unsigned NumTemplateArgs,
181 ActiveTemplateInstantiation::InstantiationKind Kind,
182 SourceRange InstantiationRange)
183: SemaRef(SemaRef) {
Mike Stump11289f42009-09-09 15:08:12 +0000184
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000185 Invalid = CheckInstantiationDepth(PointOfInstantiation,
186 InstantiationRange);
187 if (!Invalid) {
188 ActiveTemplateInstantiation Inst;
189 Inst.Kind = Kind;
190 Inst.PointOfInstantiation = PointOfInstantiation;
191 Inst.Entity = reinterpret_cast<uintptr_t>(FunctionTemplate);
192 Inst.TemplateArgs = TemplateArgs;
193 Inst.NumTemplateArgs = NumTemplateArgs;
194 Inst.InstantiationRange = InstantiationRange;
195 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
Douglas Gregor84d49a22009-11-11 21:54:23 +0000196
197 if (!Inst.isInstantiationRecord())
198 ++SemaRef.NonInstantiationEntries;
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000199 }
200}
201
Mike Stump11289f42009-09-09 15:08:12 +0000202Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef,
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000203 SourceLocation PointOfInstantiation,
Douglas Gregor637d9982009-06-10 23:47:09 +0000204 ClassTemplatePartialSpecializationDecl *PartialSpec,
205 const TemplateArgument *TemplateArgs,
206 unsigned NumTemplateArgs,
207 SourceRange InstantiationRange)
208 : SemaRef(SemaRef) {
209
Douglas Gregor84d49a22009-11-11 21:54:23 +0000210 Invalid = false;
211
212 ActiveTemplateInstantiation Inst;
213 Inst.Kind = ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution;
214 Inst.PointOfInstantiation = PointOfInstantiation;
215 Inst.Entity = reinterpret_cast<uintptr_t>(PartialSpec);
216 Inst.TemplateArgs = TemplateArgs;
217 Inst.NumTemplateArgs = NumTemplateArgs;
218 Inst.InstantiationRange = InstantiationRange;
219 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
220
221 assert(!Inst.isInstantiationRecord());
222 ++SemaRef.NonInstantiationEntries;
Douglas Gregor637d9982009-06-10 23:47:09 +0000223}
224
Mike Stump11289f42009-09-09 15:08:12 +0000225Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef,
Douglas Gregore62e6a02009-11-11 19:13:48 +0000226 SourceLocation PointOfInstantiation,
Anders Carlsson657bad42009-09-05 05:14:19 +0000227 ParmVarDecl *Param,
228 const TemplateArgument *TemplateArgs,
229 unsigned NumTemplateArgs,
230 SourceRange InstantiationRange)
231 : SemaRef(SemaRef) {
Mike Stump11289f42009-09-09 15:08:12 +0000232
Douglas Gregore62e6a02009-11-11 19:13:48 +0000233 Invalid = CheckInstantiationDepth(PointOfInstantiation, InstantiationRange);
Anders Carlsson657bad42009-09-05 05:14:19 +0000234
235 if (!Invalid) {
236 ActiveTemplateInstantiation Inst;
237 Inst.Kind
238 = ActiveTemplateInstantiation::DefaultFunctionArgumentInstantiation;
Douglas Gregore62e6a02009-11-11 19:13:48 +0000239 Inst.PointOfInstantiation = PointOfInstantiation;
Anders Carlsson657bad42009-09-05 05:14:19 +0000240 Inst.Entity = reinterpret_cast<uintptr_t>(Param);
241 Inst.TemplateArgs = TemplateArgs;
242 Inst.NumTemplateArgs = NumTemplateArgs;
243 Inst.InstantiationRange = InstantiationRange;
244 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
Douglas Gregore62e6a02009-11-11 19:13:48 +0000245 }
246}
247
248Sema::InstantiatingTemplate::
249InstantiatingTemplate(Sema &SemaRef, SourceLocation PointOfInstantiation,
250 TemplateDecl *Template,
251 NonTypeTemplateParmDecl *Param,
252 const TemplateArgument *TemplateArgs,
253 unsigned NumTemplateArgs,
254 SourceRange InstantiationRange) : SemaRef(SemaRef) {
Douglas Gregor84d49a22009-11-11 21:54:23 +0000255 Invalid = false;
Douglas Gregore62e6a02009-11-11 19:13:48 +0000256
Douglas Gregor84d49a22009-11-11 21:54:23 +0000257 ActiveTemplateInstantiation Inst;
258 Inst.Kind = ActiveTemplateInstantiation::PriorTemplateArgumentSubstitution;
259 Inst.PointOfInstantiation = PointOfInstantiation;
260 Inst.Template = Template;
261 Inst.Entity = reinterpret_cast<uintptr_t>(Param);
262 Inst.TemplateArgs = TemplateArgs;
263 Inst.NumTemplateArgs = NumTemplateArgs;
264 Inst.InstantiationRange = InstantiationRange;
265 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
266
267 assert(!Inst.isInstantiationRecord());
268 ++SemaRef.NonInstantiationEntries;
Douglas Gregore62e6a02009-11-11 19:13:48 +0000269}
270
271Sema::InstantiatingTemplate::
272InstantiatingTemplate(Sema &SemaRef, SourceLocation PointOfInstantiation,
273 TemplateDecl *Template,
274 TemplateTemplateParmDecl *Param,
275 const TemplateArgument *TemplateArgs,
276 unsigned NumTemplateArgs,
277 SourceRange InstantiationRange) : SemaRef(SemaRef) {
Douglas Gregor84d49a22009-11-11 21:54:23 +0000278 Invalid = false;
279 ActiveTemplateInstantiation Inst;
280 Inst.Kind = ActiveTemplateInstantiation::PriorTemplateArgumentSubstitution;
281 Inst.PointOfInstantiation = PointOfInstantiation;
282 Inst.Template = Template;
283 Inst.Entity = reinterpret_cast<uintptr_t>(Param);
284 Inst.TemplateArgs = TemplateArgs;
285 Inst.NumTemplateArgs = NumTemplateArgs;
286 Inst.InstantiationRange = InstantiationRange;
287 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
Douglas Gregore62e6a02009-11-11 19:13:48 +0000288
Douglas Gregor84d49a22009-11-11 21:54:23 +0000289 assert(!Inst.isInstantiationRecord());
290 ++SemaRef.NonInstantiationEntries;
291}
292
293Sema::InstantiatingTemplate::
294InstantiatingTemplate(Sema &SemaRef, SourceLocation PointOfInstantiation,
295 TemplateDecl *Template,
296 NamedDecl *Param,
297 const TemplateArgument *TemplateArgs,
298 unsigned NumTemplateArgs,
299 SourceRange InstantiationRange) : SemaRef(SemaRef) {
300 Invalid = false;
301
302 ActiveTemplateInstantiation Inst;
303 Inst.Kind = ActiveTemplateInstantiation::DefaultTemplateArgumentChecking;
304 Inst.PointOfInstantiation = PointOfInstantiation;
305 Inst.Template = Template;
306 Inst.Entity = reinterpret_cast<uintptr_t>(Param);
307 Inst.TemplateArgs = TemplateArgs;
308 Inst.NumTemplateArgs = NumTemplateArgs;
309 Inst.InstantiationRange = InstantiationRange;
310 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
311
312 assert(!Inst.isInstantiationRecord());
313 ++SemaRef.NonInstantiationEntries;
Anders Carlsson657bad42009-09-05 05:14:19 +0000314}
315
Douglas Gregor85673582009-05-18 17:01:57 +0000316void Sema::InstantiatingTemplate::Clear() {
317 if (!Invalid) {
Douglas Gregor84d49a22009-11-11 21:54:23 +0000318 if (!SemaRef.ActiveTemplateInstantiations.back().isInstantiationRecord()) {
319 assert(SemaRef.NonInstantiationEntries > 0);
320 --SemaRef.NonInstantiationEntries;
321 }
322
Douglas Gregorfcd5db32009-03-10 00:06:19 +0000323 SemaRef.ActiveTemplateInstantiations.pop_back();
Douglas Gregor85673582009-05-18 17:01:57 +0000324 Invalid = true;
325 }
Douglas Gregorfcd5db32009-03-10 00:06:19 +0000326}
327
Douglas Gregor79cf6032009-03-10 20:44:00 +0000328bool Sema::InstantiatingTemplate::CheckInstantiationDepth(
329 SourceLocation PointOfInstantiation,
330 SourceRange InstantiationRange) {
Douglas Gregor84d49a22009-11-11 21:54:23 +0000331 assert(SemaRef.NonInstantiationEntries <=
332 SemaRef.ActiveTemplateInstantiations.size());
333 if ((SemaRef.ActiveTemplateInstantiations.size() -
334 SemaRef.NonInstantiationEntries)
335 <= SemaRef.getLangOptions().InstantiationDepth)
Douglas Gregor79cf6032009-03-10 20:44:00 +0000336 return false;
337
Mike Stump11289f42009-09-09 15:08:12 +0000338 SemaRef.Diag(PointOfInstantiation,
Douglas Gregor79cf6032009-03-10 20:44:00 +0000339 diag::err_template_recursion_depth_exceeded)
340 << SemaRef.getLangOptions().InstantiationDepth
341 << InstantiationRange;
342 SemaRef.Diag(PointOfInstantiation, diag::note_template_recursion_depth)
343 << SemaRef.getLangOptions().InstantiationDepth;
344 return true;
345}
346
Douglas Gregor4ea568f2009-03-10 18:03:33 +0000347/// \brief Prints the current instantiation stack through a series of
348/// notes.
349void Sema::PrintInstantiationStack() {
Douglas Gregorffed1cb2010-04-20 07:18:24 +0000350 // Determine which template instantiations to skip, if any.
351 unsigned SkipStart = ActiveTemplateInstantiations.size(), SkipEnd = SkipStart;
352 unsigned Limit = Diags.getTemplateBacktraceLimit();
353 if (Limit && Limit < ActiveTemplateInstantiations.size()) {
354 SkipStart = Limit / 2 + Limit % 2;
355 SkipEnd = ActiveTemplateInstantiations.size() - Limit / 2;
356 }
357
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000358 // FIXME: In all of these cases, we need to show the template arguments
Douglas Gregorffed1cb2010-04-20 07:18:24 +0000359 unsigned InstantiationIdx = 0;
Douglas Gregor4ea568f2009-03-10 18:03:33 +0000360 for (llvm::SmallVector<ActiveTemplateInstantiation, 16>::reverse_iterator
361 Active = ActiveTemplateInstantiations.rbegin(),
362 ActiveEnd = ActiveTemplateInstantiations.rend();
363 Active != ActiveEnd;
Douglas Gregorffed1cb2010-04-20 07:18:24 +0000364 ++Active, ++InstantiationIdx) {
365 // Skip this instantiation?
366 if (InstantiationIdx >= SkipStart && InstantiationIdx < SkipEnd) {
367 if (InstantiationIdx == SkipStart) {
368 // Note that we're skipping instantiations.
369 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
370 diag::note_instantiation_contexts_suppressed)
371 << unsigned(ActiveTemplateInstantiations.size() - Limit);
372 }
373 continue;
374 }
375
Douglas Gregor79cf6032009-03-10 20:44:00 +0000376 switch (Active->Kind) {
377 case ActiveTemplateInstantiation::TemplateInstantiation: {
Douglas Gregor85673582009-05-18 17:01:57 +0000378 Decl *D = reinterpret_cast<Decl *>(Active->Entity);
379 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) {
380 unsigned DiagID = diag::note_template_member_class_here;
381 if (isa<ClassTemplateSpecializationDecl>(Record))
382 DiagID = diag::note_template_class_instantiation_here;
Mike Stump11289f42009-09-09 15:08:12 +0000383 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
Douglas Gregor85673582009-05-18 17:01:57 +0000384 DiagID)
385 << Context.getTypeDeclType(Record)
386 << Active->InstantiationRange;
Douglas Gregora6ef8f02009-07-24 20:34:43 +0000387 } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
Douglas Gregor4adbc6d2009-06-26 00:10:03 +0000388 unsigned DiagID;
389 if (Function->getPrimaryTemplate())
390 DiagID = diag::note_function_template_spec_here;
391 else
392 DiagID = diag::note_template_member_function_here;
Mike Stump11289f42009-09-09 15:08:12 +0000393 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
Douglas Gregor85673582009-05-18 17:01:57 +0000394 DiagID)
395 << Function
396 << Active->InstantiationRange;
Douglas Gregora6ef8f02009-07-24 20:34:43 +0000397 } else {
398 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
399 diag::note_template_static_data_member_def_here)
400 << cast<VarDecl>(D)
401 << Active->InstantiationRange;
Douglas Gregor85673582009-05-18 17:01:57 +0000402 }
Douglas Gregor79cf6032009-03-10 20:44:00 +0000403 break;
404 }
405
406 case ActiveTemplateInstantiation::DefaultTemplateArgumentInstantiation: {
407 TemplateDecl *Template = cast<TemplateDecl>((Decl *)Active->Entity);
408 std::string TemplateArgsStr
Douglas Gregordc572a32009-03-30 22:58:21 +0000409 = TemplateSpecializationType::PrintTemplateArgumentList(
Mike Stump11289f42009-09-09 15:08:12 +0000410 Active->TemplateArgs,
Douglas Gregor7de59662009-05-29 20:38:28 +0000411 Active->NumTemplateArgs,
412 Context.PrintingPolicy);
Douglas Gregor79cf6032009-03-10 20:44:00 +0000413 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
414 diag::note_default_arg_instantiation_here)
415 << (Template->getNameAsString() + TemplateArgsStr)
416 << Active->InstantiationRange;
417 break;
418 }
Douglas Gregor637d9982009-06-10 23:47:09 +0000419
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000420 case ActiveTemplateInstantiation::ExplicitTemplateArgumentSubstitution: {
Mike Stump11289f42009-09-09 15:08:12 +0000421 FunctionTemplateDecl *FnTmpl
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000422 = cast<FunctionTemplateDecl>((Decl *)Active->Entity);
Douglas Gregor637d9982009-06-10 23:47:09 +0000423 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000424 diag::note_explicit_template_arg_substitution_here)
Douglas Gregor607f1412010-03-30 20:35:20 +0000425 << FnTmpl
426 << getTemplateArgumentBindingsText(FnTmpl->getTemplateParameters(),
427 Active->TemplateArgs,
428 Active->NumTemplateArgs)
429 << Active->InstantiationRange;
Douglas Gregor637d9982009-06-10 23:47:09 +0000430 break;
431 }
Mike Stump11289f42009-09-09 15:08:12 +0000432
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000433 case ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution:
434 if (ClassTemplatePartialSpecializationDecl *PartialSpec
435 = dyn_cast<ClassTemplatePartialSpecializationDecl>(
436 (Decl *)Active->Entity)) {
437 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
438 diag::note_partial_spec_deduct_instantiation_here)
439 << Context.getTypeDeclType(PartialSpec)
Douglas Gregor607f1412010-03-30 20:35:20 +0000440 << getTemplateArgumentBindingsText(
441 PartialSpec->getTemplateParameters(),
442 Active->TemplateArgs,
443 Active->NumTemplateArgs)
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000444 << Active->InstantiationRange;
445 } else {
446 FunctionTemplateDecl *FnTmpl
447 = cast<FunctionTemplateDecl>((Decl *)Active->Entity);
448 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
449 diag::note_function_template_deduction_instantiation_here)
Douglas Gregor607f1412010-03-30 20:35:20 +0000450 << FnTmpl
451 << getTemplateArgumentBindingsText(FnTmpl->getTemplateParameters(),
452 Active->TemplateArgs,
453 Active->NumTemplateArgs)
454 << Active->InstantiationRange;
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000455 }
456 break;
Douglas Gregor637d9982009-06-10 23:47:09 +0000457
Anders Carlsson657bad42009-09-05 05:14:19 +0000458 case ActiveTemplateInstantiation::DefaultFunctionArgumentInstantiation: {
459 ParmVarDecl *Param = cast<ParmVarDecl>((Decl *)Active->Entity);
460 FunctionDecl *FD = cast<FunctionDecl>(Param->getDeclContext());
Mike Stump11289f42009-09-09 15:08:12 +0000461
Anders Carlsson657bad42009-09-05 05:14:19 +0000462 std::string TemplateArgsStr
463 = TemplateSpecializationType::PrintTemplateArgumentList(
Mike Stump11289f42009-09-09 15:08:12 +0000464 Active->TemplateArgs,
Anders Carlsson657bad42009-09-05 05:14:19 +0000465 Active->NumTemplateArgs,
466 Context.PrintingPolicy);
467 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
468 diag::note_default_function_arg_instantiation_here)
Anders Carlssondc6d2c32009-09-05 05:38:54 +0000469 << (FD->getNameAsString() + TemplateArgsStr)
Anders Carlsson657bad42009-09-05 05:14:19 +0000470 << Active->InstantiationRange;
471 break;
472 }
Mike Stump11289f42009-09-09 15:08:12 +0000473
Douglas Gregore62e6a02009-11-11 19:13:48 +0000474 case ActiveTemplateInstantiation::PriorTemplateArgumentSubstitution: {
475 NamedDecl *Parm = cast<NamedDecl>((Decl *)Active->Entity);
476 std::string Name;
477 if (!Parm->getName().empty())
478 Name = std::string(" '") + Parm->getName().str() + "'";
479
480 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
481 diag::note_prior_template_arg_substitution)
482 << isa<TemplateTemplateParmDecl>(Parm)
483 << Name
484 << getTemplateArgumentBindingsText(
485 Active->Template->getTemplateParameters(),
486 Active->TemplateArgs,
487 Active->NumTemplateArgs)
488 << Active->InstantiationRange;
489 break;
490 }
Douglas Gregor84d49a22009-11-11 21:54:23 +0000491
492 case ActiveTemplateInstantiation::DefaultTemplateArgumentChecking: {
493 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
494 diag::note_template_default_arg_checking)
495 << getTemplateArgumentBindingsText(
496 Active->Template->getTemplateParameters(),
497 Active->TemplateArgs,
498 Active->NumTemplateArgs)
499 << Active->InstantiationRange;
500 break;
501 }
Douglas Gregor79cf6032009-03-10 20:44:00 +0000502 }
Douglas Gregor4ea568f2009-03-10 18:03:33 +0000503 }
504}
505
Douglas Gregor33834512009-06-14 07:33:30 +0000506bool Sema::isSFINAEContext() const {
507 using llvm::SmallVector;
508 for (SmallVector<ActiveTemplateInstantiation, 16>::const_reverse_iterator
509 Active = ActiveTemplateInstantiations.rbegin(),
510 ActiveEnd = ActiveTemplateInstantiations.rend();
511 Active != ActiveEnd;
Douglas Gregor84d49a22009-11-11 21:54:23 +0000512 ++Active)
513 {
Douglas Gregor33834512009-06-14 07:33:30 +0000514 switch(Active->Kind) {
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000515 case ActiveTemplateInstantiation::TemplateInstantiation:
Anders Carlsson657bad42009-09-05 05:14:19 +0000516 case ActiveTemplateInstantiation::DefaultFunctionArgumentInstantiation:
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000517 // This is a template instantiation, so there is no SFINAE.
518 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000519
Douglas Gregor33834512009-06-14 07:33:30 +0000520 case ActiveTemplateInstantiation::DefaultTemplateArgumentInstantiation:
Douglas Gregore62e6a02009-11-11 19:13:48 +0000521 case ActiveTemplateInstantiation::PriorTemplateArgumentSubstitution:
Douglas Gregor84d49a22009-11-11 21:54:23 +0000522 case ActiveTemplateInstantiation::DefaultTemplateArgumentChecking:
Douglas Gregore62e6a02009-11-11 19:13:48 +0000523 // A default template argument instantiation and substitution into
524 // template parameters with arguments for prior parameters may or may
525 // not be a SFINAE context; look further up the stack.
Douglas Gregor33834512009-06-14 07:33:30 +0000526 break;
Mike Stump11289f42009-09-09 15:08:12 +0000527
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000528 case ActiveTemplateInstantiation::ExplicitTemplateArgumentSubstitution:
529 case ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution:
530 // We're either substitution explicitly-specified template arguments
531 // or deduced template arguments, so SFINAE applies.
532 return true;
Douglas Gregor33834512009-06-14 07:33:30 +0000533 }
534 }
535
536 return false;
537}
538
Douglas Gregorfe1e1102009-02-27 19:31:52 +0000539//===----------------------------------------------------------------------===/
540// Template Instantiation for Types
541//===----------------------------------------------------------------------===/
Douglas Gregor17c0d7b2009-02-28 00:25:32 +0000542namespace {
Douglas Gregor14cf7522010-04-30 18:55:50 +0000543 class TemplateInstantiator : public TreeTransform<TemplateInstantiator> {
Douglas Gregor01afeef2009-08-28 20:31:08 +0000544 const MultiLevelTemplateArgumentList &TemplateArgs;
Douglas Gregor17c0d7b2009-02-28 00:25:32 +0000545 SourceLocation Loc;
546 DeclarationName Entity;
Douglas Gregorfe1e1102009-02-27 19:31:52 +0000547
Douglas Gregor17c0d7b2009-02-28 00:25:32 +0000548 public:
Douglas Gregorebe10102009-08-20 07:17:43 +0000549 typedef TreeTransform<TemplateInstantiator> inherited;
Mike Stump11289f42009-09-09 15:08:12 +0000550
551 TemplateInstantiator(Sema &SemaRef,
Douglas Gregor01afeef2009-08-28 20:31:08 +0000552 const MultiLevelTemplateArgumentList &TemplateArgs,
Douglas Gregord6ff3322009-08-04 16:50:30 +0000553 SourceLocation Loc,
Mike Stump11289f42009-09-09 15:08:12 +0000554 DeclarationName Entity)
555 : inherited(SemaRef), TemplateArgs(TemplateArgs), Loc(Loc),
Douglas Gregorebe10102009-08-20 07:17:43 +0000556 Entity(Entity) { }
Douglas Gregor17c0d7b2009-02-28 00:25:32 +0000557
Mike Stump11289f42009-09-09 15:08:12 +0000558 /// \brief Determine whether the given type \p T has already been
Douglas Gregord6ff3322009-08-04 16:50:30 +0000559 /// transformed.
560 ///
561 /// For the purposes of template instantiation, a type has already been
562 /// transformed if it is NULL or if it is not dependent.
Douglas Gregor5597ab42010-05-07 23:12:07 +0000563 bool AlreadyTransformed(QualType T);
Mike Stump11289f42009-09-09 15:08:12 +0000564
Douglas Gregord6ff3322009-08-04 16:50:30 +0000565 /// \brief Returns the location of the entity being instantiated, if known.
566 SourceLocation getBaseLocation() { return Loc; }
Mike Stump11289f42009-09-09 15:08:12 +0000567
Douglas Gregord6ff3322009-08-04 16:50:30 +0000568 /// \brief Returns the name of the entity being instantiated, if any.
569 DeclarationName getBaseEntity() { return Entity; }
Mike Stump11289f42009-09-09 15:08:12 +0000570
Douglas Gregoref6ab412009-10-27 06:26:26 +0000571 /// \brief Sets the "base" location and entity when that
572 /// information is known based on another transformation.
573 void setBase(SourceLocation Loc, DeclarationName Entity) {
574 this->Loc = Loc;
575 this->Entity = Entity;
576 }
577
Douglas Gregord6ff3322009-08-04 16:50:30 +0000578 /// \brief Transform the given declaration by instantiating a reference to
579 /// this declaration.
Douglas Gregora04f2ca2010-03-01 15:56:25 +0000580 Decl *TransformDecl(SourceLocation Loc, Decl *D);
Douglas Gregora16548e2009-08-11 05:31:07 +0000581
Mike Stump11289f42009-09-09 15:08:12 +0000582 /// \brief Transform the definition of the given declaration by
Douglas Gregorebe10102009-08-20 07:17:43 +0000583 /// instantiating it.
Douglas Gregor25289362010-03-01 17:25:41 +0000584 Decl *TransformDefinition(SourceLocation Loc, Decl *D);
Mike Stump11289f42009-09-09 15:08:12 +0000585
Douglas Gregora5cb6da2009-10-20 05:58:46 +0000586 /// \bried Transform the first qualifier within a scope by instantiating the
587 /// declaration.
588 NamedDecl *TransformFirstQualifierInScope(NamedDecl *D, SourceLocation Loc);
589
Douglas Gregorebe10102009-08-20 07:17:43 +0000590 /// \brief Rebuild the exception declaration and register the declaration
591 /// as an instantiated local.
Mike Stump11289f42009-09-09 15:08:12 +0000592 VarDecl *RebuildExceptionDecl(VarDecl *ExceptionDecl, QualType T,
John McCallbcd03502009-12-07 02:54:59 +0000593 TypeSourceInfo *Declarator,
Douglas Gregorebe10102009-08-20 07:17:43 +0000594 IdentifierInfo *Name,
595 SourceLocation Loc, SourceRange TypeRange);
Mike Stump11289f42009-09-09 15:08:12 +0000596
Douglas Gregorf4e837f2010-04-26 17:57:08 +0000597 /// \brief Rebuild the Objective-C exception declaration and register the
598 /// declaration as an instantiated local.
599 VarDecl *RebuildObjCExceptionDecl(VarDecl *ExceptionDecl,
600 TypeSourceInfo *TSInfo, QualType T);
601
John McCall7f41d982009-09-11 04:59:25 +0000602 /// \brief Check for tag mismatches when instantiating an
603 /// elaborated type.
604 QualType RebuildElaboratedType(QualType T, ElaboratedType::TagKind Tag);
605
John McCall47f29ea2009-12-08 09:21:05 +0000606 Sema::OwningExprResult TransformPredefinedExpr(PredefinedExpr *E);
607 Sema::OwningExprResult TransformDeclRefExpr(DeclRefExpr *E);
John McCall47f29ea2009-12-08 09:21:05 +0000608 Sema::OwningExprResult TransformCXXDefaultArgExpr(CXXDefaultArgExpr *E);
John McCall13481c52010-02-06 08:42:39 +0000609 Sema::OwningExprResult TransformTemplateParmRefExpr(DeclRefExpr *E,
610 NonTypeTemplateParmDecl *D);
Sebastian Redl14236c82009-11-08 13:56:19 +0000611
Douglas Gregor14cf7522010-04-30 18:55:50 +0000612 QualType TransformFunctionProtoType(TypeLocBuilder &TLB,
613 FunctionProtoTypeLoc TL,
614 QualType ObjectType);
John McCall58f10c32010-03-11 09:03:00 +0000615 ParmVarDecl *TransformFunctionTypeParam(ParmVarDecl *OldParm);
616
Mike Stump11289f42009-09-09 15:08:12 +0000617 /// \brief Transforms a template type parameter type by performing
Douglas Gregord6ff3322009-08-04 16:50:30 +0000618 /// substitution of the corresponding template type argument.
John McCall550e0c22009-10-21 00:40:46 +0000619 QualType TransformTemplateTypeParmType(TypeLocBuilder &TLB,
Douglas Gregorfe17d252010-02-16 19:09:40 +0000620 TemplateTypeParmTypeLoc TL,
621 QualType ObjectType);
Douglas Gregord6ff3322009-08-04 16:50:30 +0000622 };
Douglas Gregor04318252009-07-06 15:59:29 +0000623}
624
Douglas Gregor5597ab42010-05-07 23:12:07 +0000625bool TemplateInstantiator::AlreadyTransformed(QualType T) {
626 if (T.isNull())
627 return true;
628
629 if (T->isDependentType())
630 return false;
631
632 getSema().MarkDeclarationsReferencedInType(Loc, T);
633 return true;
634}
635
Douglas Gregora04f2ca2010-03-01 15:56:25 +0000636Decl *TemplateInstantiator::TransformDecl(SourceLocation Loc, Decl *D) {
Douglas Gregor2b6ca462009-09-03 21:38:09 +0000637 if (!D)
638 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000639
Douglas Gregor2b6ca462009-09-03 21:38:09 +0000640 if (TemplateTemplateParmDecl *TTP = dyn_cast<TemplateTemplateParmDecl>(D)) {
Douglas Gregor01afeef2009-08-28 20:31:08 +0000641 if (TTP->getDepth() < TemplateArgs.getNumLevels()) {
Douglas Gregorb93971082010-02-05 19:54:12 +0000642 // If the corresponding template argument is NULL or non-existent, it's
643 // because we are performing instantiation from explicitly-specified
644 // template arguments in a function template, but there were some
645 // arguments left unspecified.
646 if (!TemplateArgs.hasTemplateArgument(TTP->getDepth(),
647 TTP->getPosition()))
648 return D;
649
Douglas Gregor9167f8b2009-11-11 01:00:40 +0000650 TemplateName Template
651 = TemplateArgs(TTP->getDepth(), TTP->getPosition()).getAsTemplate();
652 assert(!Template.isNull() && Template.getAsTemplateDecl() &&
Douglas Gregor01afeef2009-08-28 20:31:08 +0000653 "Wrong kind of template template argument");
Douglas Gregor9167f8b2009-11-11 01:00:40 +0000654 return Template.getAsTemplateDecl();
Douglas Gregor01afeef2009-08-28 20:31:08 +0000655 }
Mike Stump11289f42009-09-09 15:08:12 +0000656
Douglas Gregor9167f8b2009-11-11 01:00:40 +0000657 // Fall through to find the instantiated declaration for this template
658 // template parameter.
Douglas Gregor71dc5092009-08-06 06:41:21 +0000659 }
Mike Stump11289f42009-09-09 15:08:12 +0000660
Douglas Gregora04f2ca2010-03-01 15:56:25 +0000661 return SemaRef.FindInstantiatedDecl(Loc, cast<NamedDecl>(D), TemplateArgs);
Douglas Gregord6ff3322009-08-04 16:50:30 +0000662}
663
Douglas Gregor25289362010-03-01 17:25:41 +0000664Decl *TemplateInstantiator::TransformDefinition(SourceLocation Loc, Decl *D) {
John McCall76d824f2009-08-25 22:02:44 +0000665 Decl *Inst = getSema().SubstDecl(D, getSema().CurContext, TemplateArgs);
Douglas Gregorebe10102009-08-20 07:17:43 +0000666 if (!Inst)
667 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000668
Douglas Gregorebe10102009-08-20 07:17:43 +0000669 getSema().CurrentInstantiationScope->InstantiatedLocal(D, Inst);
670 return Inst;
671}
672
Douglas Gregora5cb6da2009-10-20 05:58:46 +0000673NamedDecl *
674TemplateInstantiator::TransformFirstQualifierInScope(NamedDecl *D,
675 SourceLocation Loc) {
676 // If the first part of the nested-name-specifier was a template type
677 // parameter, instantiate that type parameter down to a tag type.
678 if (TemplateTypeParmDecl *TTPD = dyn_cast_or_null<TemplateTypeParmDecl>(D)) {
679 const TemplateTypeParmType *TTP
680 = cast<TemplateTypeParmType>(getSema().Context.getTypeDeclType(TTPD));
681 if (TTP->getDepth() < TemplateArgs.getNumLevels()) {
682 QualType T = TemplateArgs(TTP->getDepth(), TTP->getIndex()).getAsType();
683 if (T.isNull())
Douglas Gregora04f2ca2010-03-01 15:56:25 +0000684 return cast_or_null<NamedDecl>(TransformDecl(Loc, D));
Douglas Gregora5cb6da2009-10-20 05:58:46 +0000685
686 if (const TagType *Tag = T->getAs<TagType>())
687 return Tag->getDecl();
688
689 // The resulting type is not a tag; complain.
690 getSema().Diag(Loc, diag::err_nested_name_spec_non_tag) << T;
691 return 0;
692 }
693 }
694
Douglas Gregora04f2ca2010-03-01 15:56:25 +0000695 return cast_or_null<NamedDecl>(TransformDecl(Loc, D));
Douglas Gregora5cb6da2009-10-20 05:58:46 +0000696}
697
Douglas Gregorebe10102009-08-20 07:17:43 +0000698VarDecl *
699TemplateInstantiator::RebuildExceptionDecl(VarDecl *ExceptionDecl,
Mike Stump11289f42009-09-09 15:08:12 +0000700 QualType T,
John McCallbcd03502009-12-07 02:54:59 +0000701 TypeSourceInfo *Declarator,
Douglas Gregorebe10102009-08-20 07:17:43 +0000702 IdentifierInfo *Name,
Mike Stump11289f42009-09-09 15:08:12 +0000703 SourceLocation Loc,
Douglas Gregorebe10102009-08-20 07:17:43 +0000704 SourceRange TypeRange) {
705 VarDecl *Var = inherited::RebuildExceptionDecl(ExceptionDecl, T, Declarator,
706 Name, Loc, TypeRange);
Douglas Gregorf4e837f2010-04-26 17:57:08 +0000707 if (Var)
708 getSema().CurrentInstantiationScope->InstantiatedLocal(ExceptionDecl, Var);
709 return Var;
710}
711
712VarDecl *TemplateInstantiator::RebuildObjCExceptionDecl(VarDecl *ExceptionDecl,
713 TypeSourceInfo *TSInfo,
714 QualType T) {
715 VarDecl *Var = inherited::RebuildObjCExceptionDecl(ExceptionDecl, TSInfo, T);
716 if (Var)
Douglas Gregorebe10102009-08-20 07:17:43 +0000717 getSema().CurrentInstantiationScope->InstantiatedLocal(ExceptionDecl, Var);
718 return Var;
719}
720
John McCall7f41d982009-09-11 04:59:25 +0000721QualType
722TemplateInstantiator::RebuildElaboratedType(QualType T,
723 ElaboratedType::TagKind Tag) {
724 if (const TagType *TT = T->getAs<TagType>()) {
725 TagDecl* TD = TT->getDecl();
726
727 // FIXME: this location is very wrong; we really need typelocs.
728 SourceLocation TagLocation = TD->getTagKeywordLoc();
729
730 // FIXME: type might be anonymous.
731 IdentifierInfo *Id = TD->getIdentifier();
732
733 // TODO: should we even warn on struct/class mismatches for this? Seems
734 // like it's likely to produce a lot of spurious errors.
735 if (!SemaRef.isAcceptableTagRedeclaration(TD, Tag, TagLocation, *Id)) {
736 SemaRef.Diag(TagLocation, diag::err_use_with_wrong_tag)
737 << Id
Douglas Gregora771f462010-03-31 17:46:05 +0000738 << FixItHint::CreateReplacement(SourceRange(TagLocation),
739 TD->getKindName());
John McCall7f41d982009-09-11 04:59:25 +0000740 SemaRef.Diag(TD->getLocation(), diag::note_previous_use);
741 }
742 }
743
744 return TreeTransform<TemplateInstantiator>::RebuildElaboratedType(T, Tag);
745}
746
747Sema::OwningExprResult
John McCall47f29ea2009-12-08 09:21:05 +0000748TemplateInstantiator::TransformPredefinedExpr(PredefinedExpr *E) {
Anders Carlsson0b209a82009-09-11 01:22:35 +0000749 if (!E->isTypeDependent())
750 return SemaRef.Owned(E->Retain());
751
752 FunctionDecl *currentDecl = getSema().getCurFunctionDecl();
753 assert(currentDecl && "Must have current function declaration when "
754 "instantiating.");
755
756 PredefinedExpr::IdentType IT = E->getIdentType();
757
Anders Carlsson5bd8d192010-02-11 18:20:28 +0000758 unsigned Length = PredefinedExpr::ComputeName(IT, currentDecl).length();
Anders Carlsson0b209a82009-09-11 01:22:35 +0000759
760 llvm::APInt LengthI(32, Length + 1);
John McCall8ccfcb52009-09-24 19:53:00 +0000761 QualType ResTy = getSema().Context.CharTy.withConst();
Anders Carlsson0b209a82009-09-11 01:22:35 +0000762 ResTy = getSema().Context.getConstantArrayType(ResTy, LengthI,
763 ArrayType::Normal, 0);
764 PredefinedExpr *PE =
765 new (getSema().Context) PredefinedExpr(E->getLocation(), ResTy, IT);
766 return getSema().Owned(PE);
767}
768
769Sema::OwningExprResult
John McCall13481c52010-02-06 08:42:39 +0000770TemplateInstantiator::TransformTemplateParmRefExpr(DeclRefExpr *E,
Douglas Gregor6c379e22010-02-08 23:41:45 +0000771 NonTypeTemplateParmDecl *NTTP) {
John McCall13481c52010-02-06 08:42:39 +0000772 // If the corresponding template argument is NULL or non-existent, it's
773 // because we are performing instantiation from explicitly-specified
774 // template arguments in a function template, but there were some
775 // arguments left unspecified.
776 if (!TemplateArgs.hasTemplateArgument(NTTP->getDepth(),
777 NTTP->getPosition()))
778 return SemaRef.Owned(E->Retain());
Mike Stump11289f42009-09-09 15:08:12 +0000779
John McCall13481c52010-02-06 08:42:39 +0000780 const TemplateArgument &Arg = TemplateArgs(NTTP->getDepth(),
781 NTTP->getPosition());
Mike Stump11289f42009-09-09 15:08:12 +0000782
John McCall13481c52010-02-06 08:42:39 +0000783 // The template argument itself might be an expression, in which
784 // case we just return that expression.
785 if (Arg.getKind() == TemplateArgument::Expression)
786 return SemaRef.Owned(Arg.getAsExpr()->Retain());
Mike Stump11289f42009-09-09 15:08:12 +0000787
John McCall13481c52010-02-06 08:42:39 +0000788 if (Arg.getKind() == TemplateArgument::Declaration) {
789 ValueDecl *VD = cast<ValueDecl>(Arg.getAsDecl());
Mike Stump11289f42009-09-09 15:08:12 +0000790
John McCall15dda372010-02-06 10:23:53 +0000791 // Find the instantiation of the template argument. This is
792 // required for nested templates.
John McCall13481c52010-02-06 08:42:39 +0000793 VD = cast_or_null<ValueDecl>(
Douglas Gregora04f2ca2010-03-01 15:56:25 +0000794 getSema().FindInstantiatedDecl(E->getLocation(),
795 VD, TemplateArgs));
John McCall13481c52010-02-06 08:42:39 +0000796 if (!VD)
797 return SemaRef.ExprError();
Mike Stump11289f42009-09-09 15:08:12 +0000798
John McCall15dda372010-02-06 10:23:53 +0000799 // Derive the type we want the substituted decl to have. This had
800 // better be non-dependent, or these checks will have serious problems.
801 QualType TargetType = SemaRef.SubstType(NTTP->getType(), TemplateArgs,
Douglas Gregor6c379e22010-02-08 23:41:45 +0000802 E->getLocation(),
803 DeclarationName());
John McCall15dda372010-02-06 10:23:53 +0000804 assert(!TargetType.isNull() && "type substitution failed for param type");
805 assert(!TargetType->isDependentType() && "param type still dependent");
Douglas Gregord5cb1dd2010-03-28 02:42:43 +0000806 return SemaRef.BuildExpressionFromDeclTemplateArgument(Arg,
807 TargetType,
808 E->getLocation());
John McCall13481c52010-02-06 08:42:39 +0000809 }
810
Douglas Gregord5cb1dd2010-03-28 02:42:43 +0000811 return SemaRef.BuildExpressionFromIntegralTemplateArgument(Arg,
812 E->getSourceRange().getBegin());
John McCall13481c52010-02-06 08:42:39 +0000813}
814
815
816Sema::OwningExprResult
817TemplateInstantiator::TransformDeclRefExpr(DeclRefExpr *E) {
818 NamedDecl *D = E->getDecl();
819 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(D)) {
820 if (NTTP->getDepth() < TemplateArgs.getNumLevels())
821 return TransformTemplateParmRefExpr(E, NTTP);
Douglas Gregor954de172009-10-31 17:21:17 +0000822
823 // We have a non-type template parameter that isn't fully substituted;
824 // FindInstantiatedDecl will find it in the local instantiation scope.
Douglas Gregora16548e2009-08-11 05:31:07 +0000825 }
Mike Stump11289f42009-09-09 15:08:12 +0000826
John McCall47f29ea2009-12-08 09:21:05 +0000827 return TreeTransform<TemplateInstantiator>::TransformDeclRefExpr(E);
Douglas Gregora16548e2009-08-11 05:31:07 +0000828}
829
Sebastian Redl14236c82009-11-08 13:56:19 +0000830Sema::OwningExprResult TemplateInstantiator::TransformCXXDefaultArgExpr(
John McCall47f29ea2009-12-08 09:21:05 +0000831 CXXDefaultArgExpr *E) {
Sebastian Redl14236c82009-11-08 13:56:19 +0000832 assert(!cast<FunctionDecl>(E->getParam()->getDeclContext())->
833 getDescribedFunctionTemplate() &&
834 "Default arg expressions are never formed in dependent cases.");
Douglas Gregor033f6752009-12-23 23:03:06 +0000835 return SemaRef.BuildCXXDefaultArgExpr(E->getUsedLocation(),
836 cast<FunctionDecl>(E->getParam()->getDeclContext()),
837 E->getParam());
Sebastian Redl14236c82009-11-08 13:56:19 +0000838}
839
Douglas Gregor14cf7522010-04-30 18:55:50 +0000840QualType TemplateInstantiator::TransformFunctionProtoType(TypeLocBuilder &TLB,
841 FunctionProtoTypeLoc TL,
842 QualType ObjectType) {
843 // We need a local instantiation scope for this function prototype.
844 Sema::LocalInstantiationScope Scope(SemaRef, /*CombineWithOuterScope=*/true);
845 return inherited::TransformFunctionProtoType(TLB, TL, ObjectType);
John McCall58f10c32010-03-11 09:03:00 +0000846}
847
848ParmVarDecl *
849TemplateInstantiator::TransformFunctionTypeParam(ParmVarDecl *OldParm) {
Douglas Gregor940bca72010-04-12 07:48:19 +0000850 return SemaRef.SubstParmVarDecl(OldParm, TemplateArgs);
John McCall58f10c32010-03-11 09:03:00 +0000851}
852
Mike Stump11289f42009-09-09 15:08:12 +0000853QualType
John McCall550e0c22009-10-21 00:40:46 +0000854TemplateInstantiator::TransformTemplateTypeParmType(TypeLocBuilder &TLB,
Douglas Gregorfe17d252010-02-16 19:09:40 +0000855 TemplateTypeParmTypeLoc TL,
856 QualType ObjectType) {
John McCall550e0c22009-10-21 00:40:46 +0000857 TemplateTypeParmType *T = TL.getTypePtr();
Douglas Gregor01afeef2009-08-28 20:31:08 +0000858 if (T->getDepth() < TemplateArgs.getNumLevels()) {
Douglas Gregorfe1e1102009-02-27 19:31:52 +0000859 // Replace the template type parameter with its corresponding
860 // template argument.
Mike Stump11289f42009-09-09 15:08:12 +0000861
862 // If the corresponding template argument is NULL or doesn't exist, it's
863 // because we are performing instantiation from explicitly-specified
864 // template arguments in a function template class, but there were some
Douglas Gregore3f1f352009-07-01 00:28:38 +0000865 // arguments left unspecified.
John McCall550e0c22009-10-21 00:40:46 +0000866 if (!TemplateArgs.hasTemplateArgument(T->getDepth(), T->getIndex())) {
867 TemplateTypeParmTypeLoc NewTL
868 = TLB.push<TemplateTypeParmTypeLoc>(TL.getType());
869 NewTL.setNameLoc(TL.getNameLoc());
870 return TL.getType();
871 }
Mike Stump11289f42009-09-09 15:08:12 +0000872
873 assert(TemplateArgs(T->getDepth(), T->getIndex()).getKind()
Douglas Gregor01afeef2009-08-28 20:31:08 +0000874 == TemplateArgument::Type &&
Douglas Gregorfe1e1102009-02-27 19:31:52 +0000875 "Template argument kind mismatch");
Douglas Gregor01afeef2009-08-28 20:31:08 +0000876
John McCallcebee162009-10-18 09:09:24 +0000877 QualType Replacement
878 = TemplateArgs(T->getDepth(), T->getIndex()).getAsType();
879
880 // TODO: only do this uniquing once, at the start of instantiation.
John McCall550e0c22009-10-21 00:40:46 +0000881 QualType Result
882 = getSema().Context.getSubstTemplateTypeParmType(T, Replacement);
883 SubstTemplateTypeParmTypeLoc NewTL
884 = TLB.push<SubstTemplateTypeParmTypeLoc>(Result);
885 NewTL.setNameLoc(TL.getNameLoc());
886 return Result;
Mike Stump11289f42009-09-09 15:08:12 +0000887 }
Douglas Gregorfe1e1102009-02-27 19:31:52 +0000888
889 // The template type parameter comes from an inner template (e.g.,
890 // the template parameter list of a member template inside the
891 // template we are instantiating). Create a new template type
892 // parameter with the template "level" reduced by one.
John McCall550e0c22009-10-21 00:40:46 +0000893 QualType Result
894 = getSema().Context.getTemplateTypeParmType(T->getDepth()
895 - TemplateArgs.getNumLevels(),
896 T->getIndex(),
897 T->isParameterPack(),
898 T->getName());
899 TemplateTypeParmTypeLoc NewTL = TLB.push<TemplateTypeParmTypeLoc>(Result);
900 NewTL.setNameLoc(TL.getNameLoc());
901 return Result;
Douglas Gregor17c0d7b2009-02-28 00:25:32 +0000902}
Douglas Gregorfe1e1102009-02-27 19:31:52 +0000903
John McCall76d824f2009-08-25 22:02:44 +0000904/// \brief Perform substitution on the type T with a given set of template
905/// arguments.
Douglas Gregorfe1e1102009-02-27 19:31:52 +0000906///
907/// This routine substitutes the given template arguments into the
908/// type T and produces the instantiated type.
909///
910/// \param T the type into which the template arguments will be
911/// substituted. If this type is not dependent, it will be returned
912/// immediately.
913///
914/// \param TemplateArgs the template arguments that will be
915/// substituted for the top-level template parameters within T.
916///
Douglas Gregorfe1e1102009-02-27 19:31:52 +0000917/// \param Loc the location in the source code where this substitution
918/// is being performed. It will typically be the location of the
919/// declarator (if we're instantiating the type of some declaration)
920/// or the location of the type in the source code (if, e.g., we're
921/// instantiating the type of a cast expression).
922///
923/// \param Entity the name of the entity associated with a declaration
924/// being instantiated (if any). May be empty to indicate that there
925/// is no such entity (if, e.g., this is a type that occurs as part of
926/// a cast expression) or that the entity has no name (e.g., an
927/// unnamed function parameter).
928///
929/// \returns If the instantiation succeeds, the instantiated
930/// type. Otherwise, produces diagnostics and returns a NULL type.
John McCallbcd03502009-12-07 02:54:59 +0000931TypeSourceInfo *Sema::SubstType(TypeSourceInfo *T,
John McCall609459e2009-10-21 00:58:09 +0000932 const MultiLevelTemplateArgumentList &Args,
933 SourceLocation Loc,
934 DeclarationName Entity) {
935 assert(!ActiveTemplateInstantiations.empty() &&
936 "Cannot perform an instantiation without some context on the "
937 "instantiation stack");
938
939 if (!T->getType()->isDependentType())
940 return T;
941
942 TemplateInstantiator Instantiator(*this, Args, Loc, Entity);
943 return Instantiator.TransformType(T);
944}
945
946/// Deprecated form of the above.
Mike Stump11289f42009-09-09 15:08:12 +0000947QualType Sema::SubstType(QualType T,
Douglas Gregor01afeef2009-08-28 20:31:08 +0000948 const MultiLevelTemplateArgumentList &TemplateArgs,
John McCall76d824f2009-08-25 22:02:44 +0000949 SourceLocation Loc, DeclarationName Entity) {
Douglas Gregor79cf6032009-03-10 20:44:00 +0000950 assert(!ActiveTemplateInstantiations.empty() &&
951 "Cannot perform an instantiation without some context on the "
952 "instantiation stack");
953
Douglas Gregorfe1e1102009-02-27 19:31:52 +0000954 // If T is not a dependent type, there is nothing to do.
955 if (!T->isDependentType())
956 return T;
957
Douglas Gregord6ff3322009-08-04 16:50:30 +0000958 TemplateInstantiator Instantiator(*this, TemplateArgs, Loc, Entity);
959 return Instantiator.TransformType(T);
Douglas Gregorfe1e1102009-02-27 19:31:52 +0000960}
Douglas Gregor463421d2009-03-03 04:44:36 +0000961
John McCallb29f78f2010-04-09 17:38:44 +0000962static bool NeedsInstantiationAsFunctionType(TypeSourceInfo *T) {
963 if (T->getType()->isDependentType())
964 return true;
965
966 TypeLoc TL = T->getTypeLoc();
967 if (!isa<FunctionProtoTypeLoc>(TL))
968 return false;
969
970 FunctionProtoTypeLoc FP = cast<FunctionProtoTypeLoc>(TL);
971 for (unsigned I = 0, E = FP.getNumArgs(); I != E; ++I) {
972 ParmVarDecl *P = FP.getArg(I);
973
974 // TODO: currently we always rebuild expressions. When we
975 // properly get lazier about this, we should use the same
976 // logic to avoid rebuilding prototypes here.
977 if (P->hasInit())
978 return true;
979 }
980
981 return false;
982}
983
984/// A form of SubstType intended specifically for instantiating the
985/// type of a FunctionDecl. Its purpose is solely to force the
986/// instantiation of default-argument expressions.
987TypeSourceInfo *Sema::SubstFunctionDeclType(TypeSourceInfo *T,
988 const MultiLevelTemplateArgumentList &Args,
989 SourceLocation Loc,
990 DeclarationName Entity) {
991 assert(!ActiveTemplateInstantiations.empty() &&
992 "Cannot perform an instantiation without some context on the "
993 "instantiation stack");
994
995 if (!NeedsInstantiationAsFunctionType(T))
996 return T;
997
998 TemplateInstantiator Instantiator(*this, Args, Loc, Entity);
999
1000 TypeLocBuilder TLB;
1001
1002 TypeLoc TL = T->getTypeLoc();
1003 TLB.reserve(TL.getFullDataSize());
1004
1005 QualType Result = Instantiator.TransformType(TLB, TL, QualType());
1006 if (Result.isNull())
1007 return 0;
1008
1009 return TLB.getTypeSourceInfo(Context, Result);
1010}
1011
Douglas Gregor940bca72010-04-12 07:48:19 +00001012ParmVarDecl *Sema::SubstParmVarDecl(ParmVarDecl *OldParm,
1013 const MultiLevelTemplateArgumentList &TemplateArgs) {
1014 TypeSourceInfo *OldDI = OldParm->getTypeSourceInfo();
1015 TypeSourceInfo *NewDI = SubstType(OldDI, TemplateArgs, OldParm->getLocation(),
1016 OldParm->getDeclName());
1017 if (!NewDI)
1018 return 0;
1019
1020 if (NewDI->getType()->isVoidType()) {
1021 Diag(OldParm->getLocation(), diag::err_param_with_void_type);
1022 return 0;
1023 }
1024
1025 ParmVarDecl *NewParm = CheckParameter(Context.getTranslationUnitDecl(),
1026 NewDI, NewDI->getType(),
1027 OldParm->getIdentifier(),
1028 OldParm->getLocation(),
Douglas Gregorc4df4072010-04-19 22:54:31 +00001029 OldParm->getStorageClass(),
1030 OldParm->getStorageClassAsWritten());
Douglas Gregor940bca72010-04-12 07:48:19 +00001031 if (!NewParm)
1032 return 0;
1033
1034 // Mark the (new) default argument as uninstantiated (if any).
1035 if (OldParm->hasUninstantiatedDefaultArg()) {
1036 Expr *Arg = OldParm->getUninstantiatedDefaultArg();
1037 NewParm->setUninstantiatedDefaultArg(Arg);
1038 } else if (Expr *Arg = OldParm->getDefaultArg())
1039 NewParm->setUninstantiatedDefaultArg(Arg);
1040
1041 NewParm->setHasInheritedDefaultArg(OldParm->hasInheritedDefaultArg());
1042
1043 CurrentInstantiationScope->InstantiatedLocal(OldParm, NewParm);
1044 return NewParm;
1045}
1046
John McCall76d824f2009-08-25 22:02:44 +00001047/// \brief Perform substitution on the base class specifiers of the
1048/// given class template specialization.
Douglas Gregor463421d2009-03-03 04:44:36 +00001049///
1050/// Produces a diagnostic and returns true on error, returns false and
1051/// attaches the instantiated base classes to the class template
1052/// specialization if successful.
Mike Stump11289f42009-09-09 15:08:12 +00001053bool
John McCall76d824f2009-08-25 22:02:44 +00001054Sema::SubstBaseSpecifiers(CXXRecordDecl *Instantiation,
1055 CXXRecordDecl *Pattern,
Douglas Gregor01afeef2009-08-28 20:31:08 +00001056 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor463421d2009-03-03 04:44:36 +00001057 bool Invalid = false;
Douglas Gregor6181ded2009-05-29 18:27:38 +00001058 llvm::SmallVector<CXXBaseSpecifier*, 4> InstantiatedBases;
Mike Stump11289f42009-09-09 15:08:12 +00001059 for (ClassTemplateSpecializationDecl::base_class_iterator
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001060 Base = Pattern->bases_begin(), BaseEnd = Pattern->bases_end();
Douglas Gregor2a72edd2009-03-10 18:52:44 +00001061 Base != BaseEnd; ++Base) {
Douglas Gregor463421d2009-03-03 04:44:36 +00001062 if (!Base->getType()->isDependentType()) {
Anders Carlssonae3c5cf2009-12-03 17:49:57 +00001063 const CXXRecordDecl *BaseDecl =
1064 cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
1065
1066 // Make sure to set the attributes from the base.
1067 SetClassDeclAttributesFromBase(Instantiation, BaseDecl,
1068 Base->isVirtual());
1069
Fariborz Jahanian5c14ec32009-07-22 17:41:53 +00001070 InstantiatedBases.push_back(new (Context) CXXBaseSpecifier(*Base));
Douglas Gregor463421d2009-03-03 04:44:36 +00001071 continue;
1072 }
1073
Mike Stump11289f42009-09-09 15:08:12 +00001074 QualType BaseType = SubstType(Base->getType(),
1075 TemplateArgs,
John McCall76d824f2009-08-25 22:02:44 +00001076 Base->getSourceRange().getBegin(),
1077 DeclarationName());
Douglas Gregor463421d2009-03-03 04:44:36 +00001078 if (BaseType.isNull()) {
1079 Invalid = true;
1080 continue;
1081 }
1082
1083 if (CXXBaseSpecifier *InstantiatedBase
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001084 = CheckBaseSpecifier(Instantiation,
Douglas Gregor463421d2009-03-03 04:44:36 +00001085 Base->getSourceRange(),
1086 Base->isVirtual(),
1087 Base->getAccessSpecifierAsWritten(),
1088 BaseType,
1089 /*FIXME: Not totally accurate */
1090 Base->getSourceRange().getBegin()))
1091 InstantiatedBases.push_back(InstantiatedBase);
1092 else
1093 Invalid = true;
1094 }
1095
Douglas Gregor2a72edd2009-03-10 18:52:44 +00001096 if (!Invalid &&
Jay Foad7d0479f2009-05-21 09:52:38 +00001097 AttachBaseSpecifiers(Instantiation, InstantiatedBases.data(),
Douglas Gregor463421d2009-03-03 04:44:36 +00001098 InstantiatedBases.size()))
1099 Invalid = true;
1100
1101 return Invalid;
1102}
1103
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001104/// \brief Instantiate the definition of a class from a given pattern.
1105///
1106/// \param PointOfInstantiation The point of instantiation within the
1107/// source code.
1108///
1109/// \param Instantiation is the declaration whose definition is being
1110/// instantiated. This will be either a class template specialization
1111/// or a member class of a class template specialization.
1112///
1113/// \param Pattern is the pattern from which the instantiation
1114/// occurs. This will be either the declaration of a class template or
1115/// the declaration of a member class of a class template.
1116///
1117/// \param TemplateArgs The template arguments to be substituted into
1118/// the pattern.
1119///
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001120/// \param TSK the kind of implicit or explicit instantiation to perform.
Douglas Gregor8a2e6012009-08-24 15:23:48 +00001121///
1122/// \param Complain whether to complain if the class cannot be instantiated due
1123/// to the lack of a definition.
1124///
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001125/// \returns true if an error occurred, false otherwise.
1126bool
1127Sema::InstantiateClass(SourceLocation PointOfInstantiation,
1128 CXXRecordDecl *Instantiation, CXXRecordDecl *Pattern,
Douglas Gregor01afeef2009-08-28 20:31:08 +00001129 const MultiLevelTemplateArgumentList &TemplateArgs,
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001130 TemplateSpecializationKind TSK,
Douglas Gregor8a2e6012009-08-24 15:23:48 +00001131 bool Complain) {
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001132 bool Invalid = false;
John McCall87a44eb2009-08-20 01:44:21 +00001133
Mike Stump11289f42009-09-09 15:08:12 +00001134 CXXRecordDecl *PatternDef
Douglas Gregor0a5a2212010-02-11 01:04:33 +00001135 = cast_or_null<CXXRecordDecl>(Pattern->getDefinition());
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001136 if (!PatternDef) {
Douglas Gregor8a2e6012009-08-24 15:23:48 +00001137 if (!Complain) {
1138 // Say nothing
1139 } else if (Pattern == Instantiation->getInstantiatedFromMemberClass()) {
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001140 Diag(PointOfInstantiation,
1141 diag::err_implicit_instantiate_member_undefined)
1142 << Context.getTypeDeclType(Instantiation);
1143 Diag(Pattern->getLocation(), diag::note_member_of_template_here);
1144 } else {
Douglas Gregora1f49972009-05-13 00:25:59 +00001145 Diag(PointOfInstantiation, diag::err_template_instantiate_undefined)
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001146 << (TSK != TSK_ImplicitInstantiation)
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001147 << Context.getTypeDeclType(Instantiation);
1148 Diag(Pattern->getLocation(), diag::note_template_decl_here);
1149 }
1150 return true;
1151 }
1152 Pattern = PatternDef;
1153
Douglas Gregord6ba93d2009-10-15 15:54:05 +00001154 // \brief Record the point of instantiation.
1155 if (MemberSpecializationInfo *MSInfo
1156 = Instantiation->getMemberSpecializationInfo()) {
1157 MSInfo->setTemplateSpecializationKind(TSK);
1158 MSInfo->setPointOfInstantiation(PointOfInstantiation);
Douglas Gregoref6ab412009-10-27 06:26:26 +00001159 } else if (ClassTemplateSpecializationDecl *Spec
1160 = dyn_cast<ClassTemplateSpecializationDecl>(Instantiation)) {
1161 Spec->setTemplateSpecializationKind(TSK);
1162 Spec->setPointOfInstantiation(PointOfInstantiation);
Douglas Gregord6ba93d2009-10-15 15:54:05 +00001163 }
1164
Douglas Gregorf3430ae2009-03-25 21:23:52 +00001165 InstantiatingTemplate Inst(*this, PointOfInstantiation, Instantiation);
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001166 if (Inst)
1167 return true;
1168
1169 // Enter the scope of this instantiation. We don't use
1170 // PushDeclContext because we don't have a scope.
John McCall80e58cd2010-04-29 00:35:03 +00001171 ContextRAII SavedContext(*this, Instantiation);
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001172
Douglas Gregor51121572010-03-24 01:33:17 +00001173 // If this is an instantiation of a local class, merge this local
1174 // instantiation scope with the enclosing scope. Otherwise, every
1175 // instantiation of a class has its own local instantiation scope.
1176 bool MergeWithParentScope = !Instantiation->isDefinedOutsideFunctionOrMethod();
1177 Sema::LocalInstantiationScope Scope(*this, MergeWithParentScope);
1178
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001179 // Start the definition of this instantiation.
1180 Instantiation->startDefinition();
Douglas Gregore9029562010-05-06 00:28:52 +00001181
1182 Instantiation->setTagKind(Pattern->getTagKind());
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001183
John McCall76d824f2009-08-25 22:02:44 +00001184 // Do substitution on the base class specifiers.
1185 if (SubstBaseSpecifiers(Instantiation, Pattern, TemplateArgs))
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001186 Invalid = true;
1187
Douglas Gregor6181ded2009-05-29 18:27:38 +00001188 llvm::SmallVector<DeclPtrTy, 4> Fields;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001189 for (RecordDecl::decl_iterator Member = Pattern->decls_begin(),
Mike Stump11289f42009-09-09 15:08:12 +00001190 MemberEnd = Pattern->decls_end();
Douglas Gregorbcced4e2009-04-09 21:40:53 +00001191 Member != MemberEnd; ++Member) {
John McCall76d824f2009-08-25 22:02:44 +00001192 Decl *NewMember = SubstDecl(*Member, Instantiation, TemplateArgs);
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001193 if (NewMember) {
Eli Friedmand0e8de22009-12-07 00:22:08 +00001194 if (FieldDecl *Field = dyn_cast<FieldDecl>(NewMember))
Chris Lattner83f095c2009-03-28 19:18:32 +00001195 Fields.push_back(DeclPtrTy::make(Field));
Eli Friedmand0e8de22009-12-07 00:22:08 +00001196 else if (NewMember->isInvalidDecl())
1197 Invalid = true;
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001198 } else {
1199 // FIXME: Eventually, a NULL return will mean that one of the
Mike Stump87c57ac2009-05-16 07:39:55 +00001200 // instantiations was a semantic disaster, and we'll want to set Invalid =
1201 // true. For now, we expect to skip some members that we can't yet handle.
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001202 }
1203 }
1204
1205 // Finish checking fields.
Chris Lattner83f095c2009-03-28 19:18:32 +00001206 ActOnFields(0, Instantiation->getLocation(), DeclPtrTy::make(Instantiation),
Jay Foad7d0479f2009-05-21 09:52:38 +00001207 Fields.data(), Fields.size(), SourceLocation(), SourceLocation(),
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001208 0);
Douglas Gregorb93b6062010-04-12 17:09:20 +00001209 CheckCompletedCXXClass(/*Scope=*/0, Instantiation);
Douglas Gregor3c74d412009-10-14 20:14:33 +00001210 if (Instantiation->isInvalidDecl())
1211 Invalid = true;
1212
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001213 // Exit the scope of this instantiation.
John McCall80e58cd2010-04-29 00:35:03 +00001214 SavedContext.pop();
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001215
Douglas Gregor0a0f04d2010-01-06 04:44:19 +00001216 // If this is a polymorphic C++ class without a key function, we'll
1217 // have to mark all of the virtual members to allow emission of a vtable
1218 // in this translation unit.
Chandler Carruth3e0c1402010-02-15 22:12:26 +00001219 if (Instantiation->isDynamicClass() &&
1220 !Context.getKeyFunction(Instantiation)) {
1221 // Local classes need to have their methods instantiated immediately in
1222 // order to have the correct instantiation scope.
1223 if (Instantiation->isLocalClass()) {
1224 MarkVirtualMembersReferenced(PointOfInstantiation,
1225 Instantiation);
1226 } else {
Douglas Gregor0a0f04d2010-01-06 04:44:19 +00001227 ClassesWithUnmarkedVirtualMembers.push_back(std::make_pair(Instantiation,
1228 PointOfInstantiation));
Chandler Carruth3e0c1402010-02-15 22:12:26 +00001229 }
1230 }
Douglas Gregor0a0f04d2010-01-06 04:44:19 +00001231
Douglas Gregor28ad4b52009-05-26 20:50:29 +00001232 if (!Invalid)
1233 Consumer.HandleTagDeclDefinition(Instantiation);
1234
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001235 return Invalid;
1236}
1237
Mike Stump11289f42009-09-09 15:08:12 +00001238bool
Douglas Gregor463421d2009-03-03 04:44:36 +00001239Sema::InstantiateClassTemplateSpecialization(
Douglas Gregoref6ab412009-10-27 06:26:26 +00001240 SourceLocation PointOfInstantiation,
Douglas Gregor463421d2009-03-03 04:44:36 +00001241 ClassTemplateSpecializationDecl *ClassTemplateSpec,
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001242 TemplateSpecializationKind TSK,
Douglas Gregor8a2e6012009-08-24 15:23:48 +00001243 bool Complain) {
Douglas Gregor463421d2009-03-03 04:44:36 +00001244 // Perform the actual instantiation on the canonical declaration.
1245 ClassTemplateSpec = cast<ClassTemplateSpecializationDecl>(
Argyrios Kyrtzidis6b7e3762009-07-18 00:34:25 +00001246 ClassTemplateSpec->getCanonicalDecl());
Douglas Gregor463421d2009-03-03 04:44:36 +00001247
Douglas Gregor4aa04b12009-09-11 21:19:12 +00001248 // Check whether we have already instantiated or specialized this class
1249 // template specialization.
1250 if (ClassTemplateSpec->getSpecializationKind() != TSK_Undeclared) {
1251 if (ClassTemplateSpec->getSpecializationKind() ==
1252 TSK_ExplicitInstantiationDeclaration &&
1253 TSK == TSK_ExplicitInstantiationDefinition) {
1254 // An explicit instantiation definition follows an explicit instantiation
1255 // declaration (C++0x [temp.explicit]p10); go ahead and perform the
1256 // explicit instantiation.
1257 ClassTemplateSpec->setSpecializationKind(TSK);
Douglas Gregor4aa04b12009-09-11 21:19:12 +00001258 return false;
1259 }
1260
1261 // We can only instantiate something that hasn't already been
1262 // instantiated or specialized. Fail without any diagnostics: our
1263 // caller will provide an error message.
Douglas Gregor463421d2009-03-03 04:44:36 +00001264 return true;
Douglas Gregor4aa04b12009-09-11 21:19:12 +00001265 }
Douglas Gregor463421d2009-03-03 04:44:36 +00001266
Douglas Gregor00a511f2009-09-15 16:51:42 +00001267 if (ClassTemplateSpec->isInvalidDecl())
1268 return true;
1269
Douglas Gregor463421d2009-03-03 04:44:36 +00001270 ClassTemplateDecl *Template = ClassTemplateSpec->getSpecializedTemplate();
Douglas Gregor01afeef2009-08-28 20:31:08 +00001271 CXXRecordDecl *Pattern = 0;
Douglas Gregor2373c592009-05-31 09:31:02 +00001272
Douglas Gregor170bc422009-06-12 22:31:52 +00001273 // C++ [temp.class.spec.match]p1:
1274 // When a class template is used in a context that requires an
1275 // instantiation of the class, it is necessary to determine
1276 // whether the instantiation is to be generated using the primary
1277 // template or one of the partial specializations. This is done by
1278 // matching the template arguments of the class template
1279 // specialization with the template argument lists of the partial
1280 // specializations.
Douglas Gregorb7ae10f2009-06-05 00:53:49 +00001281 typedef std::pair<ClassTemplatePartialSpecializationDecl *,
1282 TemplateArgumentList *> MatchResult;
1283 llvm::SmallVector<MatchResult, 4> Matched;
Douglas Gregor407e9612010-04-30 05:56:50 +00001284 llvm::SmallVector<ClassTemplatePartialSpecializationDecl *, 4> PartialSpecs;
1285 Template->getPartialSpecializations(PartialSpecs);
1286 for (unsigned I = 0, N = PartialSpecs.size(); I != N; ++I) {
1287 ClassTemplatePartialSpecializationDecl *Partial = PartialSpecs[I];
John McCallbc077cf2010-02-08 23:07:23 +00001288 TemplateDeductionInfo Info(Context, PointOfInstantiation);
Douglas Gregor181aa4a2009-06-12 18:26:56 +00001289 if (TemplateDeductionResult Result
Douglas Gregor407e9612010-04-30 05:56:50 +00001290 = DeduceTemplateArguments(Partial,
Douglas Gregor181aa4a2009-06-12 18:26:56 +00001291 ClassTemplateSpec->getTemplateArgs(),
1292 Info)) {
1293 // FIXME: Store the failed-deduction information for use in
1294 // diagnostics, later.
1295 (void)Result;
1296 } else {
Douglas Gregor407e9612010-04-30 05:56:50 +00001297 Matched.push_back(std::make_pair(Partial, Info.take()));
Douglas Gregor181aa4a2009-06-12 18:26:56 +00001298 }
Douglas Gregor2373c592009-05-31 09:31:02 +00001299 }
1300
Douglas Gregor21610382009-10-29 00:04:11 +00001301 if (Matched.size() >= 1) {
Douglas Gregorbe999392009-09-15 16:23:51 +00001302 llvm::SmallVector<MatchResult, 4>::iterator Best = Matched.begin();
Douglas Gregor21610382009-10-29 00:04:11 +00001303 if (Matched.size() == 1) {
1304 // -- If exactly one matching specialization is found, the
1305 // instantiation is generated from that specialization.
1306 // We don't need to do anything for this.
1307 } else {
1308 // -- If more than one matching specialization is found, the
1309 // partial order rules (14.5.4.2) are used to determine
1310 // whether one of the specializations is more specialized
1311 // than the others. If none of the specializations is more
1312 // specialized than all of the other matching
1313 // specializations, then the use of the class template is
1314 // ambiguous and the program is ill-formed.
1315 for (llvm::SmallVector<MatchResult, 4>::iterator P = Best + 1,
1316 PEnd = Matched.end();
1317 P != PEnd; ++P) {
John McCallbc077cf2010-02-08 23:07:23 +00001318 if (getMoreSpecializedPartialSpecialization(P->first, Best->first,
1319 PointOfInstantiation)
Douglas Gregor21610382009-10-29 00:04:11 +00001320 == P->first)
1321 Best = P;
Douglas Gregorbe999392009-09-15 16:23:51 +00001322 }
Douglas Gregorbe999392009-09-15 16:23:51 +00001323
Douglas Gregor21610382009-10-29 00:04:11 +00001324 // Determine if the best partial specialization is more specialized than
1325 // the others.
1326 bool Ambiguous = false;
Douglas Gregorbe999392009-09-15 16:23:51 +00001327 for (llvm::SmallVector<MatchResult, 4>::iterator P = Matched.begin(),
1328 PEnd = Matched.end();
Douglas Gregor21610382009-10-29 00:04:11 +00001329 P != PEnd; ++P) {
1330 if (P != Best &&
John McCallbc077cf2010-02-08 23:07:23 +00001331 getMoreSpecializedPartialSpecialization(P->first, Best->first,
1332 PointOfInstantiation)
Douglas Gregor21610382009-10-29 00:04:11 +00001333 != Best->first) {
1334 Ambiguous = true;
1335 break;
1336 }
1337 }
1338
1339 if (Ambiguous) {
1340 // Partial ordering did not produce a clear winner. Complain.
1341 ClassTemplateSpec->setInvalidDecl();
1342 Diag(PointOfInstantiation, diag::err_partial_spec_ordering_ambiguous)
1343 << ClassTemplateSpec;
1344
1345 // Print the matching partial specializations.
1346 for (llvm::SmallVector<MatchResult, 4>::iterator P = Matched.begin(),
1347 PEnd = Matched.end();
1348 P != PEnd; ++P)
1349 Diag(P->first->getLocation(), diag::note_partial_spec_match)
1350 << getTemplateArgumentBindingsText(P->first->getTemplateParameters(),
1351 *P->second);
Douglas Gregor01afeef2009-08-28 20:31:08 +00001352
Douglas Gregor21610382009-10-29 00:04:11 +00001353 return true;
1354 }
Douglas Gregorbe999392009-09-15 16:23:51 +00001355 }
1356
1357 // Instantiate using the best class template partial specialization.
Douglas Gregor21610382009-10-29 00:04:11 +00001358 ClassTemplatePartialSpecializationDecl *OrigPartialSpec = Best->first;
1359 while (OrigPartialSpec->getInstantiatedFromMember()) {
1360 // If we've found an explicit specialization of this class template,
1361 // stop here and use that as the pattern.
1362 if (OrigPartialSpec->isMemberSpecialization())
1363 break;
1364
1365 OrigPartialSpec = OrigPartialSpec->getInstantiatedFromMember();
1366 }
1367
1368 Pattern = OrigPartialSpec;
Douglas Gregorbe999392009-09-15 16:23:51 +00001369 ClassTemplateSpec->setInstantiationOf(Best->first, Best->second);
Douglas Gregor170bc422009-06-12 22:31:52 +00001370 } else {
1371 // -- If no matches are found, the instantiation is generated
1372 // from the primary template.
Douglas Gregor01afeef2009-08-28 20:31:08 +00001373 ClassTemplateDecl *OrigTemplate = Template;
Douglas Gregorcf915552009-10-13 16:30:37 +00001374 while (OrigTemplate->getInstantiatedFromMemberTemplate()) {
1375 // If we've found an explicit specialization of this class template,
1376 // stop here and use that as the pattern.
1377 if (OrigTemplate->isMemberSpecialization())
1378 break;
1379
Douglas Gregor01afeef2009-08-28 20:31:08 +00001380 OrigTemplate = OrigTemplate->getInstantiatedFromMemberTemplate();
Douglas Gregorcf915552009-10-13 16:30:37 +00001381 }
1382
Douglas Gregor01afeef2009-08-28 20:31:08 +00001383 Pattern = OrigTemplate->getTemplatedDecl();
Douglas Gregor2373c592009-05-31 09:31:02 +00001384 }
Douglas Gregor463421d2009-03-03 04:44:36 +00001385
Douglas Gregoref6ab412009-10-27 06:26:26 +00001386 bool Result = InstantiateClass(PointOfInstantiation, ClassTemplateSpec,
1387 Pattern,
1388 getTemplateInstantiationArgs(ClassTemplateSpec),
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001389 TSK,
Douglas Gregor8a2e6012009-08-24 15:23:48 +00001390 Complain);
Mike Stump11289f42009-09-09 15:08:12 +00001391
Douglas Gregorb7ae10f2009-06-05 00:53:49 +00001392 for (unsigned I = 0, N = Matched.size(); I != N; ++I) {
1393 // FIXME: Implement TemplateArgumentList::Destroy!
1394 // if (Matched[I].first != Pattern)
1395 // Matched[I].second->Destroy(Context);
1396 }
Mike Stump11289f42009-09-09 15:08:12 +00001397
Douglas Gregorb7ae10f2009-06-05 00:53:49 +00001398 return Result;
Douglas Gregor463421d2009-03-03 04:44:36 +00001399}
Douglas Gregor90a1a652009-03-19 17:26:29 +00001400
John McCall76d824f2009-08-25 22:02:44 +00001401/// \brief Instantiates the definitions of all of the member
1402/// of the given class, which is an instantiation of a class template
1403/// or a member class of a template.
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00001404void
1405Sema::InstantiateClassMembers(SourceLocation PointOfInstantiation,
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001406 CXXRecordDecl *Instantiation,
1407 const MultiLevelTemplateArgumentList &TemplateArgs,
1408 TemplateSpecializationKind TSK) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001409 for (DeclContext::decl_iterator D = Instantiation->decls_begin(),
1410 DEnd = Instantiation->decls_end();
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00001411 D != DEnd; ++D) {
Douglas Gregor1d957a32009-10-27 18:42:08 +00001412 bool SuppressNew = false;
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00001413 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(*D)) {
Douglas Gregor1d957a32009-10-27 18:42:08 +00001414 if (FunctionDecl *Pattern
1415 = Function->getInstantiatedFromMemberFunction()) {
1416 MemberSpecializationInfo *MSInfo
1417 = Function->getMemberSpecializationInfo();
1418 assert(MSInfo && "No member specialization information?");
Douglas Gregor06aa50412010-04-09 21:02:29 +00001419 if (MSInfo->getTemplateSpecializationKind()
1420 == TSK_ExplicitSpecialization)
1421 continue;
1422
Douglas Gregor1d957a32009-10-27 18:42:08 +00001423 if (CheckSpecializationInstantiationRedecl(PointOfInstantiation, TSK,
1424 Function,
1425 MSInfo->getTemplateSpecializationKind(),
1426 MSInfo->getPointOfInstantiation(),
1427 SuppressNew) ||
1428 SuppressNew)
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001429 continue;
1430
Douglas Gregor1d957a32009-10-27 18:42:08 +00001431 if (Function->getBody())
1432 continue;
1433
1434 if (TSK == TSK_ExplicitInstantiationDefinition) {
1435 // C++0x [temp.explicit]p8:
1436 // An explicit instantiation definition that names a class template
1437 // specialization explicitly instantiates the class template
1438 // specialization and is only an explicit instantiation definition
1439 // of members whose definition is visible at the point of
1440 // instantiation.
1441 if (!Pattern->getBody())
1442 continue;
1443
1444 Function->setTemplateSpecializationKind(TSK, PointOfInstantiation);
1445
1446 InstantiateFunctionDefinition(PointOfInstantiation, Function);
1447 } else {
1448 Function->setTemplateSpecializationKind(TSK, PointOfInstantiation);
1449 }
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001450 }
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00001451 } else if (VarDecl *Var = dyn_cast<VarDecl>(*D)) {
Douglas Gregor86d142a2009-10-08 07:24:58 +00001452 if (Var->isStaticDataMember()) {
Douglas Gregor1d957a32009-10-27 18:42:08 +00001453 MemberSpecializationInfo *MSInfo = Var->getMemberSpecializationInfo();
1454 assert(MSInfo && "No member specialization information?");
Douglas Gregor06aa50412010-04-09 21:02:29 +00001455 if (MSInfo->getTemplateSpecializationKind()
1456 == TSK_ExplicitSpecialization)
1457 continue;
1458
Douglas Gregor1d957a32009-10-27 18:42:08 +00001459 if (CheckSpecializationInstantiationRedecl(PointOfInstantiation, TSK,
1460 Var,
1461 MSInfo->getTemplateSpecializationKind(),
1462 MSInfo->getPointOfInstantiation(),
1463 SuppressNew) ||
1464 SuppressNew)
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001465 continue;
1466
Douglas Gregor1d957a32009-10-27 18:42:08 +00001467 if (TSK == TSK_ExplicitInstantiationDefinition) {
1468 // C++0x [temp.explicit]p8:
1469 // An explicit instantiation definition that names a class template
1470 // specialization explicitly instantiates the class template
1471 // specialization and is only an explicit instantiation definition
1472 // of members whose definition is visible at the point of
1473 // instantiation.
1474 if (!Var->getInstantiatedFromStaticDataMember()
1475 ->getOutOfLineDefinition())
1476 continue;
1477
1478 Var->setTemplateSpecializationKind(TSK, PointOfInstantiation);
Douglas Gregor86d142a2009-10-08 07:24:58 +00001479 InstantiateStaticDataMemberDefinition(PointOfInstantiation, Var);
Douglas Gregor1d957a32009-10-27 18:42:08 +00001480 } else {
1481 Var->setTemplateSpecializationKind(TSK, PointOfInstantiation);
1482 }
1483 }
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00001484 } else if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(*D)) {
Douglas Gregor1da22252010-04-18 18:11:38 +00001485 // Always skip the injected-class-name, along with any
1486 // redeclarations of nested classes, since both would cause us
1487 // to try to instantiate the members of a class twice.
1488 if (Record->isInjectedClassName() || Record->getPreviousDeclaration())
Douglas Gregord801b062009-10-07 23:56:10 +00001489 continue;
1490
Douglas Gregor1d957a32009-10-27 18:42:08 +00001491 MemberSpecializationInfo *MSInfo = Record->getMemberSpecializationInfo();
1492 assert(MSInfo && "No member specialization information?");
Douglas Gregor06aa50412010-04-09 21:02:29 +00001493
1494 if (MSInfo->getTemplateSpecializationKind()
1495 == TSK_ExplicitSpecialization)
1496 continue;
1497
Douglas Gregor1d957a32009-10-27 18:42:08 +00001498 if (CheckSpecializationInstantiationRedecl(PointOfInstantiation, TSK,
1499 Record,
1500 MSInfo->getTemplateSpecializationKind(),
1501 MSInfo->getPointOfInstantiation(),
1502 SuppressNew) ||
1503 SuppressNew)
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001504 continue;
1505
Douglas Gregor1d957a32009-10-27 18:42:08 +00001506 CXXRecordDecl *Pattern = Record->getInstantiatedFromMemberClass();
1507 assert(Pattern && "Missing instantiated-from-template information");
1508
Douglas Gregor0a5a2212010-02-11 01:04:33 +00001509 if (!Record->getDefinition()) {
1510 if (!Pattern->getDefinition()) {
Douglas Gregor1d957a32009-10-27 18:42:08 +00001511 // C++0x [temp.explicit]p8:
1512 // An explicit instantiation definition that names a class template
1513 // specialization explicitly instantiates the class template
1514 // specialization and is only an explicit instantiation definition
1515 // of members whose definition is visible at the point of
1516 // instantiation.
1517 if (TSK == TSK_ExplicitInstantiationDeclaration) {
1518 MSInfo->setTemplateSpecializationKind(TSK);
1519 MSInfo->setPointOfInstantiation(PointOfInstantiation);
1520 }
1521
1522 continue;
1523 }
1524
1525 InstantiateClass(PointOfInstantiation, Record, Pattern,
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001526 TemplateArgs,
1527 TSK);
Douglas Gregor1d957a32009-10-27 18:42:08 +00001528 }
Douglas Gregorc093c1d2009-10-08 01:19:17 +00001529
Douglas Gregor0a5a2212010-02-11 01:04:33 +00001530 Pattern = cast_or_null<CXXRecordDecl>(Record->getDefinition());
Douglas Gregor1d957a32009-10-27 18:42:08 +00001531 if (Pattern)
1532 InstantiateClassMembers(PointOfInstantiation, Pattern, TemplateArgs,
1533 TSK);
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00001534 }
1535 }
1536}
1537
1538/// \brief Instantiate the definitions of all of the members of the
1539/// given class template specialization, which was named as part of an
1540/// explicit instantiation.
Mike Stump11289f42009-09-09 15:08:12 +00001541void
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001542Sema::InstantiateClassTemplateSpecializationMembers(
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00001543 SourceLocation PointOfInstantiation,
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001544 ClassTemplateSpecializationDecl *ClassTemplateSpec,
1545 TemplateSpecializationKind TSK) {
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00001546 // C++0x [temp.explicit]p7:
1547 // An explicit instantiation that names a class template
1548 // specialization is an explicit instantion of the same kind
1549 // (declaration or definition) of each of its members (not
1550 // including members inherited from base classes) that has not
1551 // been previously explicitly specialized in the translation unit
1552 // containing the explicit instantiation, except as described
1553 // below.
1554 InstantiateClassMembers(PointOfInstantiation, ClassTemplateSpec,
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001555 getTemplateInstantiationArgs(ClassTemplateSpec),
1556 TSK);
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00001557}
1558
Mike Stump11289f42009-09-09 15:08:12 +00001559Sema::OwningStmtResult
Douglas Gregor01afeef2009-08-28 20:31:08 +00001560Sema::SubstStmt(Stmt *S, const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregorebe10102009-08-20 07:17:43 +00001561 if (!S)
1562 return Owned(S);
1563
1564 TemplateInstantiator Instantiator(*this, TemplateArgs,
1565 SourceLocation(),
1566 DeclarationName());
1567 return Instantiator.TransformStmt(S);
1568}
1569
Mike Stump11289f42009-09-09 15:08:12 +00001570Sema::OwningExprResult
Douglas Gregor01afeef2009-08-28 20:31:08 +00001571Sema::SubstExpr(Expr *E, const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregora16548e2009-08-11 05:31:07 +00001572 if (!E)
1573 return Owned(E);
Mike Stump11289f42009-09-09 15:08:12 +00001574
Douglas Gregora16548e2009-08-11 05:31:07 +00001575 TemplateInstantiator Instantiator(*this, TemplateArgs,
1576 SourceLocation(),
1577 DeclarationName());
1578 return Instantiator.TransformExpr(E);
1579}
1580
John McCall76d824f2009-08-25 22:02:44 +00001581/// \brief Do template substitution on a nested-name-specifier.
Douglas Gregorf21eb492009-03-26 23:50:42 +00001582NestedNameSpecifier *
John McCall76d824f2009-08-25 22:02:44 +00001583Sema::SubstNestedNameSpecifier(NestedNameSpecifier *NNS,
Douglas Gregor01afeef2009-08-28 20:31:08 +00001584 SourceRange Range,
1585 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor1135c352009-08-06 05:28:30 +00001586 TemplateInstantiator Instantiator(*this, TemplateArgs, Range.getBegin(),
1587 DeclarationName());
Douglas Gregorcd3f49f2010-02-25 04:46:04 +00001588 return Instantiator.TransformNestedNameSpecifier(NNS, Range);
Douglas Gregor90a1a652009-03-19 17:26:29 +00001589}
Douglas Gregoraa594892009-03-31 18:38:02 +00001590
1591TemplateName
John McCall76d824f2009-08-25 22:02:44 +00001592Sema::SubstTemplateName(TemplateName Name, SourceLocation Loc,
Douglas Gregor01afeef2009-08-28 20:31:08 +00001593 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor71dc5092009-08-06 06:41:21 +00001594 TemplateInstantiator Instantiator(*this, TemplateArgs, Loc,
1595 DeclarationName());
1596 return Instantiator.TransformTemplateName(Name);
Douglas Gregoraa594892009-03-31 18:38:02 +00001597}
Douglas Gregorc43620d2009-06-11 00:06:24 +00001598
John McCall0ad16662009-10-29 08:12:44 +00001599bool Sema::Subst(const TemplateArgumentLoc &Input, TemplateArgumentLoc &Output,
1600 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregore922c772009-08-04 22:27:00 +00001601 TemplateInstantiator Instantiator(*this, TemplateArgs, SourceLocation(),
1602 DeclarationName());
John McCall0ad16662009-10-29 08:12:44 +00001603
1604 return Instantiator.TransformTemplateArgument(Input, Output);
Douglas Gregorc43620d2009-06-11 00:06:24 +00001605}
Douglas Gregor14cf7522010-04-30 18:55:50 +00001606
1607Decl *Sema::LocalInstantiationScope::getInstantiationOf(const Decl *D) {
1608 for (LocalInstantiationScope *Current = this; Current;
1609 Current = Current->Outer) {
1610 // Check if we found something within this scope.
1611 llvm::DenseMap<const Decl *, Decl *>::iterator Found
1612 = Current->LocalDecls.find(D);
1613 if (Found != Current->LocalDecls.end())
1614 return Found->second;
1615
1616 // If we aren't combined with our outer scope, we're done.
1617 if (!Current->CombineWithOuterScope)
1618 break;
1619 }
1620
1621 assert(D->isInvalidDecl() &&
1622 "declaration was not instantiated in this scope!");
1623 return 0;
1624}
1625
1626void Sema::LocalInstantiationScope::InstantiatedLocal(const Decl *D,
1627 Decl *Inst) {
1628 Decl *&Stored = LocalDecls[D];
1629 assert((!Stored || Stored == Inst)&& "Already instantiated this local");
1630 Stored = Inst;
1631}