blob: c7489ad821076f78db09a350767cf3cd88d7681a [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.
563 bool AlreadyTransformed(QualType T) {
564 return T.isNull() || !T->isDependentType();
Douglas Gregorf61eca92009-05-13 18:28:20 +0000565 }
Mike Stump11289f42009-09-09 15:08:12 +0000566
Douglas Gregord6ff3322009-08-04 16:50:30 +0000567 /// \brief Returns the location of the entity being instantiated, if known.
568 SourceLocation getBaseLocation() { return Loc; }
Mike Stump11289f42009-09-09 15:08:12 +0000569
Douglas Gregord6ff3322009-08-04 16:50:30 +0000570 /// \brief Returns the name of the entity being instantiated, if any.
571 DeclarationName getBaseEntity() { return Entity; }
Mike Stump11289f42009-09-09 15:08:12 +0000572
Douglas Gregoref6ab412009-10-27 06:26:26 +0000573 /// \brief Sets the "base" location and entity when that
574 /// information is known based on another transformation.
575 void setBase(SourceLocation Loc, DeclarationName Entity) {
576 this->Loc = Loc;
577 this->Entity = Entity;
578 }
579
Douglas Gregord6ff3322009-08-04 16:50:30 +0000580 /// \brief Transform the given declaration by instantiating a reference to
581 /// this declaration.
Douglas Gregora04f2ca2010-03-01 15:56:25 +0000582 Decl *TransformDecl(SourceLocation Loc, Decl *D);
Douglas Gregora16548e2009-08-11 05:31:07 +0000583
Mike Stump11289f42009-09-09 15:08:12 +0000584 /// \brief Transform the definition of the given declaration by
Douglas Gregorebe10102009-08-20 07:17:43 +0000585 /// instantiating it.
Douglas Gregor25289362010-03-01 17:25:41 +0000586 Decl *TransformDefinition(SourceLocation Loc, Decl *D);
Mike Stump11289f42009-09-09 15:08:12 +0000587
Douglas Gregora5cb6da2009-10-20 05:58:46 +0000588 /// \bried Transform the first qualifier within a scope by instantiating the
589 /// declaration.
590 NamedDecl *TransformFirstQualifierInScope(NamedDecl *D, SourceLocation Loc);
591
Douglas Gregorebe10102009-08-20 07:17:43 +0000592 /// \brief Rebuild the exception declaration and register the declaration
593 /// as an instantiated local.
Mike Stump11289f42009-09-09 15:08:12 +0000594 VarDecl *RebuildExceptionDecl(VarDecl *ExceptionDecl, QualType T,
John McCallbcd03502009-12-07 02:54:59 +0000595 TypeSourceInfo *Declarator,
Douglas Gregorebe10102009-08-20 07:17:43 +0000596 IdentifierInfo *Name,
597 SourceLocation Loc, SourceRange TypeRange);
Mike Stump11289f42009-09-09 15:08:12 +0000598
Douglas Gregorf4e837f2010-04-26 17:57:08 +0000599 /// \brief Rebuild the Objective-C exception declaration and register the
600 /// declaration as an instantiated local.
601 VarDecl *RebuildObjCExceptionDecl(VarDecl *ExceptionDecl,
602 TypeSourceInfo *TSInfo, QualType T);
603
John McCall7f41d982009-09-11 04:59:25 +0000604 /// \brief Check for tag mismatches when instantiating an
605 /// elaborated type.
606 QualType RebuildElaboratedType(QualType T, ElaboratedType::TagKind Tag);
607
John McCall47f29ea2009-12-08 09:21:05 +0000608 Sema::OwningExprResult TransformPredefinedExpr(PredefinedExpr *E);
609 Sema::OwningExprResult TransformDeclRefExpr(DeclRefExpr *E);
John McCall47f29ea2009-12-08 09:21:05 +0000610 Sema::OwningExprResult TransformCXXDefaultArgExpr(CXXDefaultArgExpr *E);
John McCall13481c52010-02-06 08:42:39 +0000611 Sema::OwningExprResult TransformTemplateParmRefExpr(DeclRefExpr *E,
612 NonTypeTemplateParmDecl *D);
Sebastian Redl14236c82009-11-08 13:56:19 +0000613
Douglas Gregor14cf7522010-04-30 18:55:50 +0000614 QualType TransformFunctionProtoType(TypeLocBuilder &TLB,
615 FunctionProtoTypeLoc TL,
616 QualType ObjectType);
John McCall58f10c32010-03-11 09:03:00 +0000617 ParmVarDecl *TransformFunctionTypeParam(ParmVarDecl *OldParm);
618
Mike Stump11289f42009-09-09 15:08:12 +0000619 /// \brief Transforms a template type parameter type by performing
Douglas Gregord6ff3322009-08-04 16:50:30 +0000620 /// substitution of the corresponding template type argument.
John McCall550e0c22009-10-21 00:40:46 +0000621 QualType TransformTemplateTypeParmType(TypeLocBuilder &TLB,
Douglas Gregorfe17d252010-02-16 19:09:40 +0000622 TemplateTypeParmTypeLoc TL,
623 QualType ObjectType);
Douglas Gregord6ff3322009-08-04 16:50:30 +0000624 };
Douglas Gregor04318252009-07-06 15:59:29 +0000625}
626
Douglas Gregora04f2ca2010-03-01 15:56:25 +0000627Decl *TemplateInstantiator::TransformDecl(SourceLocation Loc, Decl *D) {
Douglas Gregor2b6ca462009-09-03 21:38:09 +0000628 if (!D)
629 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000630
Douglas Gregor2b6ca462009-09-03 21:38:09 +0000631 if (TemplateTemplateParmDecl *TTP = dyn_cast<TemplateTemplateParmDecl>(D)) {
Douglas Gregor01afeef2009-08-28 20:31:08 +0000632 if (TTP->getDepth() < TemplateArgs.getNumLevels()) {
Douglas Gregorb93971082010-02-05 19:54:12 +0000633 // If the corresponding template argument is NULL or non-existent, it's
634 // because we are performing instantiation from explicitly-specified
635 // template arguments in a function template, but there were some
636 // arguments left unspecified.
637 if (!TemplateArgs.hasTemplateArgument(TTP->getDepth(),
638 TTP->getPosition()))
639 return D;
640
Douglas Gregor9167f8b2009-11-11 01:00:40 +0000641 TemplateName Template
642 = TemplateArgs(TTP->getDepth(), TTP->getPosition()).getAsTemplate();
643 assert(!Template.isNull() && Template.getAsTemplateDecl() &&
Douglas Gregor01afeef2009-08-28 20:31:08 +0000644 "Wrong kind of template template argument");
Douglas Gregor9167f8b2009-11-11 01:00:40 +0000645 return Template.getAsTemplateDecl();
Douglas Gregor01afeef2009-08-28 20:31:08 +0000646 }
Mike Stump11289f42009-09-09 15:08:12 +0000647
Douglas Gregor9167f8b2009-11-11 01:00:40 +0000648 // Fall through to find the instantiated declaration for this template
649 // template parameter.
Douglas Gregor71dc5092009-08-06 06:41:21 +0000650 }
Mike Stump11289f42009-09-09 15:08:12 +0000651
Douglas Gregora04f2ca2010-03-01 15:56:25 +0000652 return SemaRef.FindInstantiatedDecl(Loc, cast<NamedDecl>(D), TemplateArgs);
Douglas Gregord6ff3322009-08-04 16:50:30 +0000653}
654
Douglas Gregor25289362010-03-01 17:25:41 +0000655Decl *TemplateInstantiator::TransformDefinition(SourceLocation Loc, Decl *D) {
John McCall76d824f2009-08-25 22:02:44 +0000656 Decl *Inst = getSema().SubstDecl(D, getSema().CurContext, TemplateArgs);
Douglas Gregorebe10102009-08-20 07:17:43 +0000657 if (!Inst)
658 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000659
Douglas Gregorebe10102009-08-20 07:17:43 +0000660 getSema().CurrentInstantiationScope->InstantiatedLocal(D, Inst);
661 return Inst;
662}
663
Douglas Gregora5cb6da2009-10-20 05:58:46 +0000664NamedDecl *
665TemplateInstantiator::TransformFirstQualifierInScope(NamedDecl *D,
666 SourceLocation Loc) {
667 // If the first part of the nested-name-specifier was a template type
668 // parameter, instantiate that type parameter down to a tag type.
669 if (TemplateTypeParmDecl *TTPD = dyn_cast_or_null<TemplateTypeParmDecl>(D)) {
670 const TemplateTypeParmType *TTP
671 = cast<TemplateTypeParmType>(getSema().Context.getTypeDeclType(TTPD));
672 if (TTP->getDepth() < TemplateArgs.getNumLevels()) {
673 QualType T = TemplateArgs(TTP->getDepth(), TTP->getIndex()).getAsType();
674 if (T.isNull())
Douglas Gregora04f2ca2010-03-01 15:56:25 +0000675 return cast_or_null<NamedDecl>(TransformDecl(Loc, D));
Douglas Gregora5cb6da2009-10-20 05:58:46 +0000676
677 if (const TagType *Tag = T->getAs<TagType>())
678 return Tag->getDecl();
679
680 // The resulting type is not a tag; complain.
681 getSema().Diag(Loc, diag::err_nested_name_spec_non_tag) << T;
682 return 0;
683 }
684 }
685
Douglas Gregora04f2ca2010-03-01 15:56:25 +0000686 return cast_or_null<NamedDecl>(TransformDecl(Loc, D));
Douglas Gregora5cb6da2009-10-20 05:58:46 +0000687}
688
Douglas Gregorebe10102009-08-20 07:17:43 +0000689VarDecl *
690TemplateInstantiator::RebuildExceptionDecl(VarDecl *ExceptionDecl,
Mike Stump11289f42009-09-09 15:08:12 +0000691 QualType T,
John McCallbcd03502009-12-07 02:54:59 +0000692 TypeSourceInfo *Declarator,
Douglas Gregorebe10102009-08-20 07:17:43 +0000693 IdentifierInfo *Name,
Mike Stump11289f42009-09-09 15:08:12 +0000694 SourceLocation Loc,
Douglas Gregorebe10102009-08-20 07:17:43 +0000695 SourceRange TypeRange) {
696 VarDecl *Var = inherited::RebuildExceptionDecl(ExceptionDecl, T, Declarator,
697 Name, Loc, TypeRange);
Douglas Gregorf4e837f2010-04-26 17:57:08 +0000698 if (Var)
699 getSema().CurrentInstantiationScope->InstantiatedLocal(ExceptionDecl, Var);
700 return Var;
701}
702
703VarDecl *TemplateInstantiator::RebuildObjCExceptionDecl(VarDecl *ExceptionDecl,
704 TypeSourceInfo *TSInfo,
705 QualType T) {
706 VarDecl *Var = inherited::RebuildObjCExceptionDecl(ExceptionDecl, TSInfo, T);
707 if (Var)
Douglas Gregorebe10102009-08-20 07:17:43 +0000708 getSema().CurrentInstantiationScope->InstantiatedLocal(ExceptionDecl, Var);
709 return Var;
710}
711
John McCall7f41d982009-09-11 04:59:25 +0000712QualType
713TemplateInstantiator::RebuildElaboratedType(QualType T,
714 ElaboratedType::TagKind Tag) {
715 if (const TagType *TT = T->getAs<TagType>()) {
716 TagDecl* TD = TT->getDecl();
717
718 // FIXME: this location is very wrong; we really need typelocs.
719 SourceLocation TagLocation = TD->getTagKeywordLoc();
720
721 // FIXME: type might be anonymous.
722 IdentifierInfo *Id = TD->getIdentifier();
723
724 // TODO: should we even warn on struct/class mismatches for this? Seems
725 // like it's likely to produce a lot of spurious errors.
726 if (!SemaRef.isAcceptableTagRedeclaration(TD, Tag, TagLocation, *Id)) {
727 SemaRef.Diag(TagLocation, diag::err_use_with_wrong_tag)
728 << Id
Douglas Gregora771f462010-03-31 17:46:05 +0000729 << FixItHint::CreateReplacement(SourceRange(TagLocation),
730 TD->getKindName());
John McCall7f41d982009-09-11 04:59:25 +0000731 SemaRef.Diag(TD->getLocation(), diag::note_previous_use);
732 }
733 }
734
735 return TreeTransform<TemplateInstantiator>::RebuildElaboratedType(T, Tag);
736}
737
738Sema::OwningExprResult
John McCall47f29ea2009-12-08 09:21:05 +0000739TemplateInstantiator::TransformPredefinedExpr(PredefinedExpr *E) {
Anders Carlsson0b209a82009-09-11 01:22:35 +0000740 if (!E->isTypeDependent())
741 return SemaRef.Owned(E->Retain());
742
743 FunctionDecl *currentDecl = getSema().getCurFunctionDecl();
744 assert(currentDecl && "Must have current function declaration when "
745 "instantiating.");
746
747 PredefinedExpr::IdentType IT = E->getIdentType();
748
Anders Carlsson5bd8d192010-02-11 18:20:28 +0000749 unsigned Length = PredefinedExpr::ComputeName(IT, currentDecl).length();
Anders Carlsson0b209a82009-09-11 01:22:35 +0000750
751 llvm::APInt LengthI(32, Length + 1);
John McCall8ccfcb52009-09-24 19:53:00 +0000752 QualType ResTy = getSema().Context.CharTy.withConst();
Anders Carlsson0b209a82009-09-11 01:22:35 +0000753 ResTy = getSema().Context.getConstantArrayType(ResTy, LengthI,
754 ArrayType::Normal, 0);
755 PredefinedExpr *PE =
756 new (getSema().Context) PredefinedExpr(E->getLocation(), ResTy, IT);
757 return getSema().Owned(PE);
758}
759
760Sema::OwningExprResult
John McCall13481c52010-02-06 08:42:39 +0000761TemplateInstantiator::TransformTemplateParmRefExpr(DeclRefExpr *E,
Douglas Gregor6c379e22010-02-08 23:41:45 +0000762 NonTypeTemplateParmDecl *NTTP) {
John McCall13481c52010-02-06 08:42:39 +0000763 // If the corresponding template argument is NULL or non-existent, it's
764 // because we are performing instantiation from explicitly-specified
765 // template arguments in a function template, but there were some
766 // arguments left unspecified.
767 if (!TemplateArgs.hasTemplateArgument(NTTP->getDepth(),
768 NTTP->getPosition()))
769 return SemaRef.Owned(E->Retain());
Mike Stump11289f42009-09-09 15:08:12 +0000770
John McCall13481c52010-02-06 08:42:39 +0000771 const TemplateArgument &Arg = TemplateArgs(NTTP->getDepth(),
772 NTTP->getPosition());
Mike Stump11289f42009-09-09 15:08:12 +0000773
John McCall13481c52010-02-06 08:42:39 +0000774 // The template argument itself might be an expression, in which
775 // case we just return that expression.
776 if (Arg.getKind() == TemplateArgument::Expression)
777 return SemaRef.Owned(Arg.getAsExpr()->Retain());
Mike Stump11289f42009-09-09 15:08:12 +0000778
John McCall13481c52010-02-06 08:42:39 +0000779 if (Arg.getKind() == TemplateArgument::Declaration) {
780 ValueDecl *VD = cast<ValueDecl>(Arg.getAsDecl());
Mike Stump11289f42009-09-09 15:08:12 +0000781
John McCall15dda372010-02-06 10:23:53 +0000782 // Find the instantiation of the template argument. This is
783 // required for nested templates.
John McCall13481c52010-02-06 08:42:39 +0000784 VD = cast_or_null<ValueDecl>(
Douglas Gregora04f2ca2010-03-01 15:56:25 +0000785 getSema().FindInstantiatedDecl(E->getLocation(),
786 VD, TemplateArgs));
John McCall13481c52010-02-06 08:42:39 +0000787 if (!VD)
788 return SemaRef.ExprError();
Mike Stump11289f42009-09-09 15:08:12 +0000789
John McCall15dda372010-02-06 10:23:53 +0000790 // Derive the type we want the substituted decl to have. This had
791 // better be non-dependent, or these checks will have serious problems.
792 QualType TargetType = SemaRef.SubstType(NTTP->getType(), TemplateArgs,
Douglas Gregor6c379e22010-02-08 23:41:45 +0000793 E->getLocation(),
794 DeclarationName());
John McCall15dda372010-02-06 10:23:53 +0000795 assert(!TargetType.isNull() && "type substitution failed for param type");
796 assert(!TargetType->isDependentType() && "param type still dependent");
Douglas Gregord5cb1dd2010-03-28 02:42:43 +0000797 return SemaRef.BuildExpressionFromDeclTemplateArgument(Arg,
798 TargetType,
799 E->getLocation());
John McCall13481c52010-02-06 08:42:39 +0000800 }
801
Douglas Gregord5cb1dd2010-03-28 02:42:43 +0000802 return SemaRef.BuildExpressionFromIntegralTemplateArgument(Arg,
803 E->getSourceRange().getBegin());
John McCall13481c52010-02-06 08:42:39 +0000804}
805
806
807Sema::OwningExprResult
808TemplateInstantiator::TransformDeclRefExpr(DeclRefExpr *E) {
809 NamedDecl *D = E->getDecl();
810 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(D)) {
811 if (NTTP->getDepth() < TemplateArgs.getNumLevels())
812 return TransformTemplateParmRefExpr(E, NTTP);
Douglas Gregor954de172009-10-31 17:21:17 +0000813
814 // We have a non-type template parameter that isn't fully substituted;
815 // FindInstantiatedDecl will find it in the local instantiation scope.
Douglas Gregora16548e2009-08-11 05:31:07 +0000816 }
Mike Stump11289f42009-09-09 15:08:12 +0000817
John McCall47f29ea2009-12-08 09:21:05 +0000818 return TreeTransform<TemplateInstantiator>::TransformDeclRefExpr(E);
Douglas Gregora16548e2009-08-11 05:31:07 +0000819}
820
Sebastian Redl14236c82009-11-08 13:56:19 +0000821Sema::OwningExprResult TemplateInstantiator::TransformCXXDefaultArgExpr(
John McCall47f29ea2009-12-08 09:21:05 +0000822 CXXDefaultArgExpr *E) {
Sebastian Redl14236c82009-11-08 13:56:19 +0000823 assert(!cast<FunctionDecl>(E->getParam()->getDeclContext())->
824 getDescribedFunctionTemplate() &&
825 "Default arg expressions are never formed in dependent cases.");
Douglas Gregor033f6752009-12-23 23:03:06 +0000826 return SemaRef.BuildCXXDefaultArgExpr(E->getUsedLocation(),
827 cast<FunctionDecl>(E->getParam()->getDeclContext()),
828 E->getParam());
Sebastian Redl14236c82009-11-08 13:56:19 +0000829}
830
Douglas Gregor14cf7522010-04-30 18:55:50 +0000831QualType TemplateInstantiator::TransformFunctionProtoType(TypeLocBuilder &TLB,
832 FunctionProtoTypeLoc TL,
833 QualType ObjectType) {
834 // We need a local instantiation scope for this function prototype.
835 Sema::LocalInstantiationScope Scope(SemaRef, /*CombineWithOuterScope=*/true);
836 return inherited::TransformFunctionProtoType(TLB, TL, ObjectType);
John McCall58f10c32010-03-11 09:03:00 +0000837}
838
839ParmVarDecl *
840TemplateInstantiator::TransformFunctionTypeParam(ParmVarDecl *OldParm) {
Douglas Gregor940bca72010-04-12 07:48:19 +0000841 return SemaRef.SubstParmVarDecl(OldParm, TemplateArgs);
John McCall58f10c32010-03-11 09:03:00 +0000842}
843
Mike Stump11289f42009-09-09 15:08:12 +0000844QualType
John McCall550e0c22009-10-21 00:40:46 +0000845TemplateInstantiator::TransformTemplateTypeParmType(TypeLocBuilder &TLB,
Douglas Gregorfe17d252010-02-16 19:09:40 +0000846 TemplateTypeParmTypeLoc TL,
847 QualType ObjectType) {
John McCall550e0c22009-10-21 00:40:46 +0000848 TemplateTypeParmType *T = TL.getTypePtr();
Douglas Gregor01afeef2009-08-28 20:31:08 +0000849 if (T->getDepth() < TemplateArgs.getNumLevels()) {
Douglas Gregorfe1e1102009-02-27 19:31:52 +0000850 // Replace the template type parameter with its corresponding
851 // template argument.
Mike Stump11289f42009-09-09 15:08:12 +0000852
853 // If the corresponding template argument is NULL or doesn't exist, it's
854 // because we are performing instantiation from explicitly-specified
855 // template arguments in a function template class, but there were some
Douglas Gregore3f1f352009-07-01 00:28:38 +0000856 // arguments left unspecified.
John McCall550e0c22009-10-21 00:40:46 +0000857 if (!TemplateArgs.hasTemplateArgument(T->getDepth(), T->getIndex())) {
858 TemplateTypeParmTypeLoc NewTL
859 = TLB.push<TemplateTypeParmTypeLoc>(TL.getType());
860 NewTL.setNameLoc(TL.getNameLoc());
861 return TL.getType();
862 }
Mike Stump11289f42009-09-09 15:08:12 +0000863
864 assert(TemplateArgs(T->getDepth(), T->getIndex()).getKind()
Douglas Gregor01afeef2009-08-28 20:31:08 +0000865 == TemplateArgument::Type &&
Douglas Gregorfe1e1102009-02-27 19:31:52 +0000866 "Template argument kind mismatch");
Douglas Gregor01afeef2009-08-28 20:31:08 +0000867
John McCallcebee162009-10-18 09:09:24 +0000868 QualType Replacement
869 = TemplateArgs(T->getDepth(), T->getIndex()).getAsType();
870
871 // TODO: only do this uniquing once, at the start of instantiation.
John McCall550e0c22009-10-21 00:40:46 +0000872 QualType Result
873 = getSema().Context.getSubstTemplateTypeParmType(T, Replacement);
874 SubstTemplateTypeParmTypeLoc NewTL
875 = TLB.push<SubstTemplateTypeParmTypeLoc>(Result);
876 NewTL.setNameLoc(TL.getNameLoc());
877 return Result;
Mike Stump11289f42009-09-09 15:08:12 +0000878 }
Douglas Gregorfe1e1102009-02-27 19:31:52 +0000879
880 // The template type parameter comes from an inner template (e.g.,
881 // the template parameter list of a member template inside the
882 // template we are instantiating). Create a new template type
883 // parameter with the template "level" reduced by one.
John McCall550e0c22009-10-21 00:40:46 +0000884 QualType Result
885 = getSema().Context.getTemplateTypeParmType(T->getDepth()
886 - TemplateArgs.getNumLevels(),
887 T->getIndex(),
888 T->isParameterPack(),
889 T->getName());
890 TemplateTypeParmTypeLoc NewTL = TLB.push<TemplateTypeParmTypeLoc>(Result);
891 NewTL.setNameLoc(TL.getNameLoc());
892 return Result;
Douglas Gregor17c0d7b2009-02-28 00:25:32 +0000893}
Douglas Gregorfe1e1102009-02-27 19:31:52 +0000894
John McCall76d824f2009-08-25 22:02:44 +0000895/// \brief Perform substitution on the type T with a given set of template
896/// arguments.
Douglas Gregorfe1e1102009-02-27 19:31:52 +0000897///
898/// This routine substitutes the given template arguments into the
899/// type T and produces the instantiated type.
900///
901/// \param T the type into which the template arguments will be
902/// substituted. If this type is not dependent, it will be returned
903/// immediately.
904///
905/// \param TemplateArgs the template arguments that will be
906/// substituted for the top-level template parameters within T.
907///
Douglas Gregorfe1e1102009-02-27 19:31:52 +0000908/// \param Loc the location in the source code where this substitution
909/// is being performed. It will typically be the location of the
910/// declarator (if we're instantiating the type of some declaration)
911/// or the location of the type in the source code (if, e.g., we're
912/// instantiating the type of a cast expression).
913///
914/// \param Entity the name of the entity associated with a declaration
915/// being instantiated (if any). May be empty to indicate that there
916/// is no such entity (if, e.g., this is a type that occurs as part of
917/// a cast expression) or that the entity has no name (e.g., an
918/// unnamed function parameter).
919///
920/// \returns If the instantiation succeeds, the instantiated
921/// type. Otherwise, produces diagnostics and returns a NULL type.
John McCallbcd03502009-12-07 02:54:59 +0000922TypeSourceInfo *Sema::SubstType(TypeSourceInfo *T,
John McCall609459e2009-10-21 00:58:09 +0000923 const MultiLevelTemplateArgumentList &Args,
924 SourceLocation Loc,
925 DeclarationName Entity) {
926 assert(!ActiveTemplateInstantiations.empty() &&
927 "Cannot perform an instantiation without some context on the "
928 "instantiation stack");
929
930 if (!T->getType()->isDependentType())
931 return T;
932
933 TemplateInstantiator Instantiator(*this, Args, Loc, Entity);
934 return Instantiator.TransformType(T);
935}
936
937/// Deprecated form of the above.
Mike Stump11289f42009-09-09 15:08:12 +0000938QualType Sema::SubstType(QualType T,
Douglas Gregor01afeef2009-08-28 20:31:08 +0000939 const MultiLevelTemplateArgumentList &TemplateArgs,
John McCall76d824f2009-08-25 22:02:44 +0000940 SourceLocation Loc, DeclarationName Entity) {
Douglas Gregor79cf6032009-03-10 20:44:00 +0000941 assert(!ActiveTemplateInstantiations.empty() &&
942 "Cannot perform an instantiation without some context on the "
943 "instantiation stack");
944
Douglas Gregorfe1e1102009-02-27 19:31:52 +0000945 // If T is not a dependent type, there is nothing to do.
946 if (!T->isDependentType())
947 return T;
948
Douglas Gregord6ff3322009-08-04 16:50:30 +0000949 TemplateInstantiator Instantiator(*this, TemplateArgs, Loc, Entity);
950 return Instantiator.TransformType(T);
Douglas Gregorfe1e1102009-02-27 19:31:52 +0000951}
Douglas Gregor463421d2009-03-03 04:44:36 +0000952
John McCallb29f78f2010-04-09 17:38:44 +0000953static bool NeedsInstantiationAsFunctionType(TypeSourceInfo *T) {
954 if (T->getType()->isDependentType())
955 return true;
956
957 TypeLoc TL = T->getTypeLoc();
958 if (!isa<FunctionProtoTypeLoc>(TL))
959 return false;
960
961 FunctionProtoTypeLoc FP = cast<FunctionProtoTypeLoc>(TL);
962 for (unsigned I = 0, E = FP.getNumArgs(); I != E; ++I) {
963 ParmVarDecl *P = FP.getArg(I);
964
965 // TODO: currently we always rebuild expressions. When we
966 // properly get lazier about this, we should use the same
967 // logic to avoid rebuilding prototypes here.
968 if (P->hasInit())
969 return true;
970 }
971
972 return false;
973}
974
975/// A form of SubstType intended specifically for instantiating the
976/// type of a FunctionDecl. Its purpose is solely to force the
977/// instantiation of default-argument expressions.
978TypeSourceInfo *Sema::SubstFunctionDeclType(TypeSourceInfo *T,
979 const MultiLevelTemplateArgumentList &Args,
980 SourceLocation Loc,
981 DeclarationName Entity) {
982 assert(!ActiveTemplateInstantiations.empty() &&
983 "Cannot perform an instantiation without some context on the "
984 "instantiation stack");
985
986 if (!NeedsInstantiationAsFunctionType(T))
987 return T;
988
989 TemplateInstantiator Instantiator(*this, Args, Loc, Entity);
990
991 TypeLocBuilder TLB;
992
993 TypeLoc TL = T->getTypeLoc();
994 TLB.reserve(TL.getFullDataSize());
995
996 QualType Result = Instantiator.TransformType(TLB, TL, QualType());
997 if (Result.isNull())
998 return 0;
999
1000 return TLB.getTypeSourceInfo(Context, Result);
1001}
1002
Douglas Gregor940bca72010-04-12 07:48:19 +00001003ParmVarDecl *Sema::SubstParmVarDecl(ParmVarDecl *OldParm,
1004 const MultiLevelTemplateArgumentList &TemplateArgs) {
1005 TypeSourceInfo *OldDI = OldParm->getTypeSourceInfo();
1006 TypeSourceInfo *NewDI = SubstType(OldDI, TemplateArgs, OldParm->getLocation(),
1007 OldParm->getDeclName());
1008 if (!NewDI)
1009 return 0;
1010
1011 if (NewDI->getType()->isVoidType()) {
1012 Diag(OldParm->getLocation(), diag::err_param_with_void_type);
1013 return 0;
1014 }
1015
1016 ParmVarDecl *NewParm = CheckParameter(Context.getTranslationUnitDecl(),
1017 NewDI, NewDI->getType(),
1018 OldParm->getIdentifier(),
1019 OldParm->getLocation(),
Douglas Gregorc4df4072010-04-19 22:54:31 +00001020 OldParm->getStorageClass(),
1021 OldParm->getStorageClassAsWritten());
Douglas Gregor940bca72010-04-12 07:48:19 +00001022 if (!NewParm)
1023 return 0;
1024
1025 // Mark the (new) default argument as uninstantiated (if any).
1026 if (OldParm->hasUninstantiatedDefaultArg()) {
1027 Expr *Arg = OldParm->getUninstantiatedDefaultArg();
1028 NewParm->setUninstantiatedDefaultArg(Arg);
1029 } else if (Expr *Arg = OldParm->getDefaultArg())
1030 NewParm->setUninstantiatedDefaultArg(Arg);
1031
1032 NewParm->setHasInheritedDefaultArg(OldParm->hasInheritedDefaultArg());
1033
1034 CurrentInstantiationScope->InstantiatedLocal(OldParm, NewParm);
1035 return NewParm;
1036}
1037
John McCall76d824f2009-08-25 22:02:44 +00001038/// \brief Perform substitution on the base class specifiers of the
1039/// given class template specialization.
Douglas Gregor463421d2009-03-03 04:44:36 +00001040///
1041/// Produces a diagnostic and returns true on error, returns false and
1042/// attaches the instantiated base classes to the class template
1043/// specialization if successful.
Mike Stump11289f42009-09-09 15:08:12 +00001044bool
John McCall76d824f2009-08-25 22:02:44 +00001045Sema::SubstBaseSpecifiers(CXXRecordDecl *Instantiation,
1046 CXXRecordDecl *Pattern,
Douglas Gregor01afeef2009-08-28 20:31:08 +00001047 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor463421d2009-03-03 04:44:36 +00001048 bool Invalid = false;
Douglas Gregor6181ded2009-05-29 18:27:38 +00001049 llvm::SmallVector<CXXBaseSpecifier*, 4> InstantiatedBases;
Mike Stump11289f42009-09-09 15:08:12 +00001050 for (ClassTemplateSpecializationDecl::base_class_iterator
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001051 Base = Pattern->bases_begin(), BaseEnd = Pattern->bases_end();
Douglas Gregor2a72edd2009-03-10 18:52:44 +00001052 Base != BaseEnd; ++Base) {
Douglas Gregor463421d2009-03-03 04:44:36 +00001053 if (!Base->getType()->isDependentType()) {
Anders Carlssonae3c5cf2009-12-03 17:49:57 +00001054 const CXXRecordDecl *BaseDecl =
1055 cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
1056
1057 // Make sure to set the attributes from the base.
1058 SetClassDeclAttributesFromBase(Instantiation, BaseDecl,
1059 Base->isVirtual());
1060
Fariborz Jahanian5c14ec32009-07-22 17:41:53 +00001061 InstantiatedBases.push_back(new (Context) CXXBaseSpecifier(*Base));
Douglas Gregor463421d2009-03-03 04:44:36 +00001062 continue;
1063 }
1064
Mike Stump11289f42009-09-09 15:08:12 +00001065 QualType BaseType = SubstType(Base->getType(),
1066 TemplateArgs,
John McCall76d824f2009-08-25 22:02:44 +00001067 Base->getSourceRange().getBegin(),
1068 DeclarationName());
Douglas Gregor463421d2009-03-03 04:44:36 +00001069 if (BaseType.isNull()) {
1070 Invalid = true;
1071 continue;
1072 }
1073
1074 if (CXXBaseSpecifier *InstantiatedBase
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001075 = CheckBaseSpecifier(Instantiation,
Douglas Gregor463421d2009-03-03 04:44:36 +00001076 Base->getSourceRange(),
1077 Base->isVirtual(),
1078 Base->getAccessSpecifierAsWritten(),
1079 BaseType,
1080 /*FIXME: Not totally accurate */
1081 Base->getSourceRange().getBegin()))
1082 InstantiatedBases.push_back(InstantiatedBase);
1083 else
1084 Invalid = true;
1085 }
1086
Douglas Gregor2a72edd2009-03-10 18:52:44 +00001087 if (!Invalid &&
Jay Foad7d0479f2009-05-21 09:52:38 +00001088 AttachBaseSpecifiers(Instantiation, InstantiatedBases.data(),
Douglas Gregor463421d2009-03-03 04:44:36 +00001089 InstantiatedBases.size()))
1090 Invalid = true;
1091
1092 return Invalid;
1093}
1094
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001095/// \brief Instantiate the definition of a class from a given pattern.
1096///
1097/// \param PointOfInstantiation The point of instantiation within the
1098/// source code.
1099///
1100/// \param Instantiation is the declaration whose definition is being
1101/// instantiated. This will be either a class template specialization
1102/// or a member class of a class template specialization.
1103///
1104/// \param Pattern is the pattern from which the instantiation
1105/// occurs. This will be either the declaration of a class template or
1106/// the declaration of a member class of a class template.
1107///
1108/// \param TemplateArgs The template arguments to be substituted into
1109/// the pattern.
1110///
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001111/// \param TSK the kind of implicit or explicit instantiation to perform.
Douglas Gregor8a2e6012009-08-24 15:23:48 +00001112///
1113/// \param Complain whether to complain if the class cannot be instantiated due
1114/// to the lack of a definition.
1115///
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001116/// \returns true if an error occurred, false otherwise.
1117bool
1118Sema::InstantiateClass(SourceLocation PointOfInstantiation,
1119 CXXRecordDecl *Instantiation, CXXRecordDecl *Pattern,
Douglas Gregor01afeef2009-08-28 20:31:08 +00001120 const MultiLevelTemplateArgumentList &TemplateArgs,
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001121 TemplateSpecializationKind TSK,
Douglas Gregor8a2e6012009-08-24 15:23:48 +00001122 bool Complain) {
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001123 bool Invalid = false;
John McCall87a44eb2009-08-20 01:44:21 +00001124
Mike Stump11289f42009-09-09 15:08:12 +00001125 CXXRecordDecl *PatternDef
Douglas Gregor0a5a2212010-02-11 01:04:33 +00001126 = cast_or_null<CXXRecordDecl>(Pattern->getDefinition());
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001127 if (!PatternDef) {
Douglas Gregor8a2e6012009-08-24 15:23:48 +00001128 if (!Complain) {
1129 // Say nothing
1130 } else if (Pattern == Instantiation->getInstantiatedFromMemberClass()) {
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001131 Diag(PointOfInstantiation,
1132 diag::err_implicit_instantiate_member_undefined)
1133 << Context.getTypeDeclType(Instantiation);
1134 Diag(Pattern->getLocation(), diag::note_member_of_template_here);
1135 } else {
Douglas Gregora1f49972009-05-13 00:25:59 +00001136 Diag(PointOfInstantiation, diag::err_template_instantiate_undefined)
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001137 << (TSK != TSK_ImplicitInstantiation)
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001138 << Context.getTypeDeclType(Instantiation);
1139 Diag(Pattern->getLocation(), diag::note_template_decl_here);
1140 }
1141 return true;
1142 }
1143 Pattern = PatternDef;
1144
Douglas Gregord6ba93d2009-10-15 15:54:05 +00001145 // \brief Record the point of instantiation.
1146 if (MemberSpecializationInfo *MSInfo
1147 = Instantiation->getMemberSpecializationInfo()) {
1148 MSInfo->setTemplateSpecializationKind(TSK);
1149 MSInfo->setPointOfInstantiation(PointOfInstantiation);
Douglas Gregoref6ab412009-10-27 06:26:26 +00001150 } else if (ClassTemplateSpecializationDecl *Spec
1151 = dyn_cast<ClassTemplateSpecializationDecl>(Instantiation)) {
1152 Spec->setTemplateSpecializationKind(TSK);
1153 Spec->setPointOfInstantiation(PointOfInstantiation);
Douglas Gregord6ba93d2009-10-15 15:54:05 +00001154 }
1155
Douglas Gregorf3430ae2009-03-25 21:23:52 +00001156 InstantiatingTemplate Inst(*this, PointOfInstantiation, Instantiation);
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001157 if (Inst)
1158 return true;
1159
1160 // Enter the scope of this instantiation. We don't use
1161 // PushDeclContext because we don't have a scope.
John McCall80e58cd2010-04-29 00:35:03 +00001162 ContextRAII SavedContext(*this, Instantiation);
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001163
Douglas Gregor51121572010-03-24 01:33:17 +00001164 // If this is an instantiation of a local class, merge this local
1165 // instantiation scope with the enclosing scope. Otherwise, every
1166 // instantiation of a class has its own local instantiation scope.
1167 bool MergeWithParentScope = !Instantiation->isDefinedOutsideFunctionOrMethod();
1168 Sema::LocalInstantiationScope Scope(*this, MergeWithParentScope);
1169
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001170 // Start the definition of this instantiation.
1171 Instantiation->startDefinition();
Douglas Gregore9029562010-05-06 00:28:52 +00001172
1173 Instantiation->setTagKind(Pattern->getTagKind());
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001174
John McCall76d824f2009-08-25 22:02:44 +00001175 // Do substitution on the base class specifiers.
1176 if (SubstBaseSpecifiers(Instantiation, Pattern, TemplateArgs))
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001177 Invalid = true;
1178
Douglas Gregor6181ded2009-05-29 18:27:38 +00001179 llvm::SmallVector<DeclPtrTy, 4> Fields;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001180 for (RecordDecl::decl_iterator Member = Pattern->decls_begin(),
Mike Stump11289f42009-09-09 15:08:12 +00001181 MemberEnd = Pattern->decls_end();
Douglas Gregorbcced4e2009-04-09 21:40:53 +00001182 Member != MemberEnd; ++Member) {
John McCall76d824f2009-08-25 22:02:44 +00001183 Decl *NewMember = SubstDecl(*Member, Instantiation, TemplateArgs);
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001184 if (NewMember) {
Eli Friedmand0e8de22009-12-07 00:22:08 +00001185 if (FieldDecl *Field = dyn_cast<FieldDecl>(NewMember))
Chris Lattner83f095c2009-03-28 19:18:32 +00001186 Fields.push_back(DeclPtrTy::make(Field));
Eli Friedmand0e8de22009-12-07 00:22:08 +00001187 else if (NewMember->isInvalidDecl())
1188 Invalid = true;
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001189 } else {
1190 // FIXME: Eventually, a NULL return will mean that one of the
Mike Stump87c57ac2009-05-16 07:39:55 +00001191 // instantiations was a semantic disaster, and we'll want to set Invalid =
1192 // true. For now, we expect to skip some members that we can't yet handle.
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001193 }
1194 }
1195
1196 // Finish checking fields.
Chris Lattner83f095c2009-03-28 19:18:32 +00001197 ActOnFields(0, Instantiation->getLocation(), DeclPtrTy::make(Instantiation),
Jay Foad7d0479f2009-05-21 09:52:38 +00001198 Fields.data(), Fields.size(), SourceLocation(), SourceLocation(),
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001199 0);
Douglas Gregorb93b6062010-04-12 17:09:20 +00001200 CheckCompletedCXXClass(/*Scope=*/0, Instantiation);
Douglas Gregor3c74d412009-10-14 20:14:33 +00001201 if (Instantiation->isInvalidDecl())
1202 Invalid = true;
1203
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001204 // Exit the scope of this instantiation.
John McCall80e58cd2010-04-29 00:35:03 +00001205 SavedContext.pop();
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001206
Douglas Gregor0a0f04d2010-01-06 04:44:19 +00001207 // If this is a polymorphic C++ class without a key function, we'll
1208 // have to mark all of the virtual members to allow emission of a vtable
1209 // in this translation unit.
Chandler Carruth3e0c1402010-02-15 22:12:26 +00001210 if (Instantiation->isDynamicClass() &&
1211 !Context.getKeyFunction(Instantiation)) {
1212 // Local classes need to have their methods instantiated immediately in
1213 // order to have the correct instantiation scope.
1214 if (Instantiation->isLocalClass()) {
1215 MarkVirtualMembersReferenced(PointOfInstantiation,
1216 Instantiation);
1217 } else {
Douglas Gregor0a0f04d2010-01-06 04:44:19 +00001218 ClassesWithUnmarkedVirtualMembers.push_back(std::make_pair(Instantiation,
1219 PointOfInstantiation));
Chandler Carruth3e0c1402010-02-15 22:12:26 +00001220 }
1221 }
Douglas Gregor0a0f04d2010-01-06 04:44:19 +00001222
Douglas Gregor28ad4b52009-05-26 20:50:29 +00001223 if (!Invalid)
1224 Consumer.HandleTagDeclDefinition(Instantiation);
1225
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001226 return Invalid;
1227}
1228
Mike Stump11289f42009-09-09 15:08:12 +00001229bool
Douglas Gregor463421d2009-03-03 04:44:36 +00001230Sema::InstantiateClassTemplateSpecialization(
Douglas Gregoref6ab412009-10-27 06:26:26 +00001231 SourceLocation PointOfInstantiation,
Douglas Gregor463421d2009-03-03 04:44:36 +00001232 ClassTemplateSpecializationDecl *ClassTemplateSpec,
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001233 TemplateSpecializationKind TSK,
Douglas Gregor8a2e6012009-08-24 15:23:48 +00001234 bool Complain) {
Douglas Gregor463421d2009-03-03 04:44:36 +00001235 // Perform the actual instantiation on the canonical declaration.
1236 ClassTemplateSpec = cast<ClassTemplateSpecializationDecl>(
Argyrios Kyrtzidis6b7e3762009-07-18 00:34:25 +00001237 ClassTemplateSpec->getCanonicalDecl());
Douglas Gregor463421d2009-03-03 04:44:36 +00001238
Douglas Gregor4aa04b12009-09-11 21:19:12 +00001239 // Check whether we have already instantiated or specialized this class
1240 // template specialization.
1241 if (ClassTemplateSpec->getSpecializationKind() != TSK_Undeclared) {
1242 if (ClassTemplateSpec->getSpecializationKind() ==
1243 TSK_ExplicitInstantiationDeclaration &&
1244 TSK == TSK_ExplicitInstantiationDefinition) {
1245 // An explicit instantiation definition follows an explicit instantiation
1246 // declaration (C++0x [temp.explicit]p10); go ahead and perform the
1247 // explicit instantiation.
1248 ClassTemplateSpec->setSpecializationKind(TSK);
Douglas Gregor4aa04b12009-09-11 21:19:12 +00001249 return false;
1250 }
1251
1252 // We can only instantiate something that hasn't already been
1253 // instantiated or specialized. Fail without any diagnostics: our
1254 // caller will provide an error message.
Douglas Gregor463421d2009-03-03 04:44:36 +00001255 return true;
Douglas Gregor4aa04b12009-09-11 21:19:12 +00001256 }
Douglas Gregor463421d2009-03-03 04:44:36 +00001257
Douglas Gregor00a511f2009-09-15 16:51:42 +00001258 if (ClassTemplateSpec->isInvalidDecl())
1259 return true;
1260
Douglas Gregor463421d2009-03-03 04:44:36 +00001261 ClassTemplateDecl *Template = ClassTemplateSpec->getSpecializedTemplate();
Douglas Gregor01afeef2009-08-28 20:31:08 +00001262 CXXRecordDecl *Pattern = 0;
Douglas Gregor2373c592009-05-31 09:31:02 +00001263
Douglas Gregor170bc422009-06-12 22:31:52 +00001264 // C++ [temp.class.spec.match]p1:
1265 // When a class template is used in a context that requires an
1266 // instantiation of the class, it is necessary to determine
1267 // whether the instantiation is to be generated using the primary
1268 // template or one of the partial specializations. This is done by
1269 // matching the template arguments of the class template
1270 // specialization with the template argument lists of the partial
1271 // specializations.
Douglas Gregorb7ae10f2009-06-05 00:53:49 +00001272 typedef std::pair<ClassTemplatePartialSpecializationDecl *,
1273 TemplateArgumentList *> MatchResult;
1274 llvm::SmallVector<MatchResult, 4> Matched;
Douglas Gregor407e9612010-04-30 05:56:50 +00001275 llvm::SmallVector<ClassTemplatePartialSpecializationDecl *, 4> PartialSpecs;
1276 Template->getPartialSpecializations(PartialSpecs);
1277 for (unsigned I = 0, N = PartialSpecs.size(); I != N; ++I) {
1278 ClassTemplatePartialSpecializationDecl *Partial = PartialSpecs[I];
John McCallbc077cf2010-02-08 23:07:23 +00001279 TemplateDeductionInfo Info(Context, PointOfInstantiation);
Douglas Gregor181aa4a2009-06-12 18:26:56 +00001280 if (TemplateDeductionResult Result
Douglas Gregor407e9612010-04-30 05:56:50 +00001281 = DeduceTemplateArguments(Partial,
Douglas Gregor181aa4a2009-06-12 18:26:56 +00001282 ClassTemplateSpec->getTemplateArgs(),
1283 Info)) {
1284 // FIXME: Store the failed-deduction information for use in
1285 // diagnostics, later.
1286 (void)Result;
1287 } else {
Douglas Gregor407e9612010-04-30 05:56:50 +00001288 Matched.push_back(std::make_pair(Partial, Info.take()));
Douglas Gregor181aa4a2009-06-12 18:26:56 +00001289 }
Douglas Gregor2373c592009-05-31 09:31:02 +00001290 }
1291
Douglas Gregor21610382009-10-29 00:04:11 +00001292 if (Matched.size() >= 1) {
Douglas Gregorbe999392009-09-15 16:23:51 +00001293 llvm::SmallVector<MatchResult, 4>::iterator Best = Matched.begin();
Douglas Gregor21610382009-10-29 00:04:11 +00001294 if (Matched.size() == 1) {
1295 // -- If exactly one matching specialization is found, the
1296 // instantiation is generated from that specialization.
1297 // We don't need to do anything for this.
1298 } else {
1299 // -- If more than one matching specialization is found, the
1300 // partial order rules (14.5.4.2) are used to determine
1301 // whether one of the specializations is more specialized
1302 // than the others. If none of the specializations is more
1303 // specialized than all of the other matching
1304 // specializations, then the use of the class template is
1305 // ambiguous and the program is ill-formed.
1306 for (llvm::SmallVector<MatchResult, 4>::iterator P = Best + 1,
1307 PEnd = Matched.end();
1308 P != PEnd; ++P) {
John McCallbc077cf2010-02-08 23:07:23 +00001309 if (getMoreSpecializedPartialSpecialization(P->first, Best->first,
1310 PointOfInstantiation)
Douglas Gregor21610382009-10-29 00:04:11 +00001311 == P->first)
1312 Best = P;
Douglas Gregorbe999392009-09-15 16:23:51 +00001313 }
Douglas Gregorbe999392009-09-15 16:23:51 +00001314
Douglas Gregor21610382009-10-29 00:04:11 +00001315 // Determine if the best partial specialization is more specialized than
1316 // the others.
1317 bool Ambiguous = false;
Douglas Gregorbe999392009-09-15 16:23:51 +00001318 for (llvm::SmallVector<MatchResult, 4>::iterator P = Matched.begin(),
1319 PEnd = Matched.end();
Douglas Gregor21610382009-10-29 00:04:11 +00001320 P != PEnd; ++P) {
1321 if (P != Best &&
John McCallbc077cf2010-02-08 23:07:23 +00001322 getMoreSpecializedPartialSpecialization(P->first, Best->first,
1323 PointOfInstantiation)
Douglas Gregor21610382009-10-29 00:04:11 +00001324 != Best->first) {
1325 Ambiguous = true;
1326 break;
1327 }
1328 }
1329
1330 if (Ambiguous) {
1331 // Partial ordering did not produce a clear winner. Complain.
1332 ClassTemplateSpec->setInvalidDecl();
1333 Diag(PointOfInstantiation, diag::err_partial_spec_ordering_ambiguous)
1334 << ClassTemplateSpec;
1335
1336 // Print the matching partial specializations.
1337 for (llvm::SmallVector<MatchResult, 4>::iterator P = Matched.begin(),
1338 PEnd = Matched.end();
1339 P != PEnd; ++P)
1340 Diag(P->first->getLocation(), diag::note_partial_spec_match)
1341 << getTemplateArgumentBindingsText(P->first->getTemplateParameters(),
1342 *P->second);
Douglas Gregor01afeef2009-08-28 20:31:08 +00001343
Douglas Gregor21610382009-10-29 00:04:11 +00001344 return true;
1345 }
Douglas Gregorbe999392009-09-15 16:23:51 +00001346 }
1347
1348 // Instantiate using the best class template partial specialization.
Douglas Gregor21610382009-10-29 00:04:11 +00001349 ClassTemplatePartialSpecializationDecl *OrigPartialSpec = Best->first;
1350 while (OrigPartialSpec->getInstantiatedFromMember()) {
1351 // If we've found an explicit specialization of this class template,
1352 // stop here and use that as the pattern.
1353 if (OrigPartialSpec->isMemberSpecialization())
1354 break;
1355
1356 OrigPartialSpec = OrigPartialSpec->getInstantiatedFromMember();
1357 }
1358
1359 Pattern = OrigPartialSpec;
Douglas Gregorbe999392009-09-15 16:23:51 +00001360 ClassTemplateSpec->setInstantiationOf(Best->first, Best->second);
Douglas Gregor170bc422009-06-12 22:31:52 +00001361 } else {
1362 // -- If no matches are found, the instantiation is generated
1363 // from the primary template.
Douglas Gregor01afeef2009-08-28 20:31:08 +00001364 ClassTemplateDecl *OrigTemplate = Template;
Douglas Gregorcf915552009-10-13 16:30:37 +00001365 while (OrigTemplate->getInstantiatedFromMemberTemplate()) {
1366 // If we've found an explicit specialization of this class template,
1367 // stop here and use that as the pattern.
1368 if (OrigTemplate->isMemberSpecialization())
1369 break;
1370
Douglas Gregor01afeef2009-08-28 20:31:08 +00001371 OrigTemplate = OrigTemplate->getInstantiatedFromMemberTemplate();
Douglas Gregorcf915552009-10-13 16:30:37 +00001372 }
1373
Douglas Gregor01afeef2009-08-28 20:31:08 +00001374 Pattern = OrigTemplate->getTemplatedDecl();
Douglas Gregor2373c592009-05-31 09:31:02 +00001375 }
Douglas Gregor463421d2009-03-03 04:44:36 +00001376
Douglas Gregoref6ab412009-10-27 06:26:26 +00001377 bool Result = InstantiateClass(PointOfInstantiation, ClassTemplateSpec,
1378 Pattern,
1379 getTemplateInstantiationArgs(ClassTemplateSpec),
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001380 TSK,
Douglas Gregor8a2e6012009-08-24 15:23:48 +00001381 Complain);
Mike Stump11289f42009-09-09 15:08:12 +00001382
Douglas Gregorb7ae10f2009-06-05 00:53:49 +00001383 for (unsigned I = 0, N = Matched.size(); I != N; ++I) {
1384 // FIXME: Implement TemplateArgumentList::Destroy!
1385 // if (Matched[I].first != Pattern)
1386 // Matched[I].second->Destroy(Context);
1387 }
Mike Stump11289f42009-09-09 15:08:12 +00001388
Douglas Gregorb7ae10f2009-06-05 00:53:49 +00001389 return Result;
Douglas Gregor463421d2009-03-03 04:44:36 +00001390}
Douglas Gregor90a1a652009-03-19 17:26:29 +00001391
John McCall76d824f2009-08-25 22:02:44 +00001392/// \brief Instantiates the definitions of all of the member
1393/// of the given class, which is an instantiation of a class template
1394/// or a member class of a template.
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00001395void
1396Sema::InstantiateClassMembers(SourceLocation PointOfInstantiation,
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001397 CXXRecordDecl *Instantiation,
1398 const MultiLevelTemplateArgumentList &TemplateArgs,
1399 TemplateSpecializationKind TSK) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001400 for (DeclContext::decl_iterator D = Instantiation->decls_begin(),
1401 DEnd = Instantiation->decls_end();
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00001402 D != DEnd; ++D) {
Douglas Gregor1d957a32009-10-27 18:42:08 +00001403 bool SuppressNew = false;
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00001404 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(*D)) {
Douglas Gregor1d957a32009-10-27 18:42:08 +00001405 if (FunctionDecl *Pattern
1406 = Function->getInstantiatedFromMemberFunction()) {
1407 MemberSpecializationInfo *MSInfo
1408 = Function->getMemberSpecializationInfo();
1409 assert(MSInfo && "No member specialization information?");
Douglas Gregor06aa50412010-04-09 21:02:29 +00001410 if (MSInfo->getTemplateSpecializationKind()
1411 == TSK_ExplicitSpecialization)
1412 continue;
1413
Douglas Gregor1d957a32009-10-27 18:42:08 +00001414 if (CheckSpecializationInstantiationRedecl(PointOfInstantiation, TSK,
1415 Function,
1416 MSInfo->getTemplateSpecializationKind(),
1417 MSInfo->getPointOfInstantiation(),
1418 SuppressNew) ||
1419 SuppressNew)
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001420 continue;
1421
Douglas Gregor1d957a32009-10-27 18:42:08 +00001422 if (Function->getBody())
1423 continue;
1424
1425 if (TSK == TSK_ExplicitInstantiationDefinition) {
1426 // C++0x [temp.explicit]p8:
1427 // An explicit instantiation definition that names a class template
1428 // specialization explicitly instantiates the class template
1429 // specialization and is only an explicit instantiation definition
1430 // of members whose definition is visible at the point of
1431 // instantiation.
1432 if (!Pattern->getBody())
1433 continue;
1434
1435 Function->setTemplateSpecializationKind(TSK, PointOfInstantiation);
1436
1437 InstantiateFunctionDefinition(PointOfInstantiation, Function);
1438 } else {
1439 Function->setTemplateSpecializationKind(TSK, PointOfInstantiation);
1440 }
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001441 }
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00001442 } else if (VarDecl *Var = dyn_cast<VarDecl>(*D)) {
Douglas Gregor86d142a2009-10-08 07:24:58 +00001443 if (Var->isStaticDataMember()) {
Douglas Gregor1d957a32009-10-27 18:42:08 +00001444 MemberSpecializationInfo *MSInfo = Var->getMemberSpecializationInfo();
1445 assert(MSInfo && "No member specialization information?");
Douglas Gregor06aa50412010-04-09 21:02:29 +00001446 if (MSInfo->getTemplateSpecializationKind()
1447 == TSK_ExplicitSpecialization)
1448 continue;
1449
Douglas Gregor1d957a32009-10-27 18:42:08 +00001450 if (CheckSpecializationInstantiationRedecl(PointOfInstantiation, TSK,
1451 Var,
1452 MSInfo->getTemplateSpecializationKind(),
1453 MSInfo->getPointOfInstantiation(),
1454 SuppressNew) ||
1455 SuppressNew)
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001456 continue;
1457
Douglas Gregor1d957a32009-10-27 18:42:08 +00001458 if (TSK == TSK_ExplicitInstantiationDefinition) {
1459 // C++0x [temp.explicit]p8:
1460 // An explicit instantiation definition that names a class template
1461 // specialization explicitly instantiates the class template
1462 // specialization and is only an explicit instantiation definition
1463 // of members whose definition is visible at the point of
1464 // instantiation.
1465 if (!Var->getInstantiatedFromStaticDataMember()
1466 ->getOutOfLineDefinition())
1467 continue;
1468
1469 Var->setTemplateSpecializationKind(TSK, PointOfInstantiation);
Douglas Gregor86d142a2009-10-08 07:24:58 +00001470 InstantiateStaticDataMemberDefinition(PointOfInstantiation, Var);
Douglas Gregor1d957a32009-10-27 18:42:08 +00001471 } else {
1472 Var->setTemplateSpecializationKind(TSK, PointOfInstantiation);
1473 }
1474 }
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00001475 } else if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(*D)) {
Douglas Gregor1da22252010-04-18 18:11:38 +00001476 // Always skip the injected-class-name, along with any
1477 // redeclarations of nested classes, since both would cause us
1478 // to try to instantiate the members of a class twice.
1479 if (Record->isInjectedClassName() || Record->getPreviousDeclaration())
Douglas Gregord801b062009-10-07 23:56:10 +00001480 continue;
1481
Douglas Gregor1d957a32009-10-27 18:42:08 +00001482 MemberSpecializationInfo *MSInfo = Record->getMemberSpecializationInfo();
1483 assert(MSInfo && "No member specialization information?");
Douglas Gregor06aa50412010-04-09 21:02:29 +00001484
1485 if (MSInfo->getTemplateSpecializationKind()
1486 == TSK_ExplicitSpecialization)
1487 continue;
1488
Douglas Gregor1d957a32009-10-27 18:42:08 +00001489 if (CheckSpecializationInstantiationRedecl(PointOfInstantiation, TSK,
1490 Record,
1491 MSInfo->getTemplateSpecializationKind(),
1492 MSInfo->getPointOfInstantiation(),
1493 SuppressNew) ||
1494 SuppressNew)
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001495 continue;
1496
Douglas Gregor1d957a32009-10-27 18:42:08 +00001497 CXXRecordDecl *Pattern = Record->getInstantiatedFromMemberClass();
1498 assert(Pattern && "Missing instantiated-from-template information");
1499
Douglas Gregor0a5a2212010-02-11 01:04:33 +00001500 if (!Record->getDefinition()) {
1501 if (!Pattern->getDefinition()) {
Douglas Gregor1d957a32009-10-27 18:42:08 +00001502 // C++0x [temp.explicit]p8:
1503 // An explicit instantiation definition that names a class template
1504 // specialization explicitly instantiates the class template
1505 // specialization and is only an explicit instantiation definition
1506 // of members whose definition is visible at the point of
1507 // instantiation.
1508 if (TSK == TSK_ExplicitInstantiationDeclaration) {
1509 MSInfo->setTemplateSpecializationKind(TSK);
1510 MSInfo->setPointOfInstantiation(PointOfInstantiation);
1511 }
1512
1513 continue;
1514 }
1515
1516 InstantiateClass(PointOfInstantiation, Record, Pattern,
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001517 TemplateArgs,
1518 TSK);
Douglas Gregor1d957a32009-10-27 18:42:08 +00001519 }
Douglas Gregorc093c1d2009-10-08 01:19:17 +00001520
Douglas Gregor0a5a2212010-02-11 01:04:33 +00001521 Pattern = cast_or_null<CXXRecordDecl>(Record->getDefinition());
Douglas Gregor1d957a32009-10-27 18:42:08 +00001522 if (Pattern)
1523 InstantiateClassMembers(PointOfInstantiation, Pattern, TemplateArgs,
1524 TSK);
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00001525 }
1526 }
1527}
1528
1529/// \brief Instantiate the definitions of all of the members of the
1530/// given class template specialization, which was named as part of an
1531/// explicit instantiation.
Mike Stump11289f42009-09-09 15:08:12 +00001532void
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001533Sema::InstantiateClassTemplateSpecializationMembers(
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00001534 SourceLocation PointOfInstantiation,
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001535 ClassTemplateSpecializationDecl *ClassTemplateSpec,
1536 TemplateSpecializationKind TSK) {
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00001537 // C++0x [temp.explicit]p7:
1538 // An explicit instantiation that names a class template
1539 // specialization is an explicit instantion of the same kind
1540 // (declaration or definition) of each of its members (not
1541 // including members inherited from base classes) that has not
1542 // been previously explicitly specialized in the translation unit
1543 // containing the explicit instantiation, except as described
1544 // below.
1545 InstantiateClassMembers(PointOfInstantiation, ClassTemplateSpec,
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001546 getTemplateInstantiationArgs(ClassTemplateSpec),
1547 TSK);
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00001548}
1549
Mike Stump11289f42009-09-09 15:08:12 +00001550Sema::OwningStmtResult
Douglas Gregor01afeef2009-08-28 20:31:08 +00001551Sema::SubstStmt(Stmt *S, const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregorebe10102009-08-20 07:17:43 +00001552 if (!S)
1553 return Owned(S);
1554
1555 TemplateInstantiator Instantiator(*this, TemplateArgs,
1556 SourceLocation(),
1557 DeclarationName());
1558 return Instantiator.TransformStmt(S);
1559}
1560
Mike Stump11289f42009-09-09 15:08:12 +00001561Sema::OwningExprResult
Douglas Gregor01afeef2009-08-28 20:31:08 +00001562Sema::SubstExpr(Expr *E, const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregora16548e2009-08-11 05:31:07 +00001563 if (!E)
1564 return Owned(E);
Mike Stump11289f42009-09-09 15:08:12 +00001565
Douglas Gregora16548e2009-08-11 05:31:07 +00001566 TemplateInstantiator Instantiator(*this, TemplateArgs,
1567 SourceLocation(),
1568 DeclarationName());
1569 return Instantiator.TransformExpr(E);
1570}
1571
John McCall76d824f2009-08-25 22:02:44 +00001572/// \brief Do template substitution on a nested-name-specifier.
Douglas Gregorf21eb492009-03-26 23:50:42 +00001573NestedNameSpecifier *
John McCall76d824f2009-08-25 22:02:44 +00001574Sema::SubstNestedNameSpecifier(NestedNameSpecifier *NNS,
Douglas Gregor01afeef2009-08-28 20:31:08 +00001575 SourceRange Range,
1576 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor1135c352009-08-06 05:28:30 +00001577 TemplateInstantiator Instantiator(*this, TemplateArgs, Range.getBegin(),
1578 DeclarationName());
Douglas Gregorcd3f49f2010-02-25 04:46:04 +00001579 return Instantiator.TransformNestedNameSpecifier(NNS, Range);
Douglas Gregor90a1a652009-03-19 17:26:29 +00001580}
Douglas Gregoraa594892009-03-31 18:38:02 +00001581
1582TemplateName
John McCall76d824f2009-08-25 22:02:44 +00001583Sema::SubstTemplateName(TemplateName Name, SourceLocation Loc,
Douglas Gregor01afeef2009-08-28 20:31:08 +00001584 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor71dc5092009-08-06 06:41:21 +00001585 TemplateInstantiator Instantiator(*this, TemplateArgs, Loc,
1586 DeclarationName());
1587 return Instantiator.TransformTemplateName(Name);
Douglas Gregoraa594892009-03-31 18:38:02 +00001588}
Douglas Gregorc43620d2009-06-11 00:06:24 +00001589
John McCall0ad16662009-10-29 08:12:44 +00001590bool Sema::Subst(const TemplateArgumentLoc &Input, TemplateArgumentLoc &Output,
1591 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregore922c772009-08-04 22:27:00 +00001592 TemplateInstantiator Instantiator(*this, TemplateArgs, SourceLocation(),
1593 DeclarationName());
John McCall0ad16662009-10-29 08:12:44 +00001594
1595 return Instantiator.TransformTemplateArgument(Input, Output);
Douglas Gregorc43620d2009-06-11 00:06:24 +00001596}
Douglas Gregor14cf7522010-04-30 18:55:50 +00001597
1598Decl *Sema::LocalInstantiationScope::getInstantiationOf(const Decl *D) {
1599 for (LocalInstantiationScope *Current = this; Current;
1600 Current = Current->Outer) {
1601 // Check if we found something within this scope.
1602 llvm::DenseMap<const Decl *, Decl *>::iterator Found
1603 = Current->LocalDecls.find(D);
1604 if (Found != Current->LocalDecls.end())
1605 return Found->second;
1606
1607 // If we aren't combined with our outer scope, we're done.
1608 if (!Current->CombineWithOuterScope)
1609 break;
1610 }
1611
1612 assert(D->isInvalidDecl() &&
1613 "declaration was not instantiated in this scope!");
1614 return 0;
1615}
1616
1617void Sema::LocalInstantiationScope::InstantiatedLocal(const Decl *D,
1618 Decl *Inst) {
1619 Decl *&Stored = LocalDecls[D];
1620 assert((!Stored || Stored == Inst)&& "Already instantiated this local");
1621 Stored = Inst;
1622}