blob: 712f5c0e2b546f20d61c7f85151087cadf2a5db0 [file] [log] [blame]
Douglas Gregor99ebf652009-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 Gregor577f75a2009-08-04 16:50:30 +000014#include "TreeTransform.h"
John McCall5b3f9132009-11-22 01:44:31 +000015#include "Lookup.h"
Douglas Gregoraba43bb2009-05-26 20:50:29 +000016#include "clang/AST/ASTConsumer.h"
Douglas Gregor99ebf652009-02-27 19:31:52 +000017#include "clang/AST/ASTContext.h"
18#include "clang/AST/Expr.h"
Douglas Gregor99ebf652009-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 Gregoree1828a2009-03-10 18:03:33 +000025//===----------------------------------------------------------------------===/
26// Template Instantiation Support
27//===----------------------------------------------------------------------===/
28
Douglas Gregord6350ae2009-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 Gregor0f8716b2009-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 Gregor525f96c2010-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 Gregore7089b02010-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 Gregord1102432009-08-28 17:37:35 +000046MultiLevelTemplateArgumentList
Douglas Gregor0f8716b2009-11-09 19:17:50 +000047Sema::getTemplateInstantiationArgs(NamedDecl *D,
Douglas Gregor525f96c2010-02-05 07:33:43 +000048 const TemplateArgumentList *Innermost,
Douglas Gregore7089b02010-05-03 23:29:10 +000049 bool RelativeToPrimary,
50 const FunctionDecl *Pattern) {
Douglas Gregord1102432009-08-28 17:37:35 +000051 // Accumulate the set of template argument lists in this structure.
52 MultiLevelTemplateArgumentList Result;
Mike Stump1eb44332009-09-09 15:08:12 +000053
Douglas Gregor0f8716b2009-11-09 19:17:50 +000054 if (Innermost)
55 Result.addOuterTemplateArguments(Innermost);
56
Douglas Gregord1102432009-08-28 17:37:35 +000057 DeclContext *Ctx = dyn_cast<DeclContext>(D);
58 if (!Ctx)
59 Ctx = D->getDeclContext();
Mike Stump1eb44332009-09-09 15:08:12 +000060
John McCallf181d8a2009-08-29 03:16:09 +000061 while (!Ctx->isFileContext()) {
Douglas Gregord1102432009-08-28 17:37:35 +000062 // Add template arguments from a class template instantiation.
Mike Stump1eb44332009-09-09 15:08:12 +000063 if (ClassTemplateSpecializationDecl *Spec
Douglas Gregord1102432009-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 Stump1eb44332009-09-09 15:08:12 +000068
Douglas Gregord1102432009-08-28 17:37:35 +000069 Result.addOuterTemplateArguments(&Spec->getTemplateInstantiationArgs());
Douglas Gregorfd056bc2009-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 Stump1eb44332009-09-09 15:08:12 +000076 }
Douglas Gregord1102432009-08-28 17:37:35 +000077 // Add template arguments from a function template specialization.
John McCallf181d8a2009-08-29 03:16:09 +000078 else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Ctx)) {
Douglas Gregor525f96c2010-02-05 07:33:43 +000079 if (!RelativeToPrimary &&
80 Function->getTemplateSpecializationKind()
81 == TSK_ExplicitSpecialization)
Douglas Gregorfd056bc2009-10-13 16:30:37 +000082 break;
83
Douglas Gregord1102432009-08-28 17:37:35 +000084 if (const TemplateArgumentList *TemplateArgs
Douglas Gregorfd056bc2009-10-13 16:30:37 +000085 = Function->getTemplateSpecializationArgs()) {
86 // Add the template arguments for this specialization.
Douglas Gregord1102432009-08-28 17:37:35 +000087 Result.addOuterTemplateArguments(TemplateArgs);
John McCallf181d8a2009-08-29 03:16:09 +000088
Douglas Gregorfd056bc2009-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 McCallf181d8a2009-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 Gregore7089b02010-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 McCallf181d8a2009-08-29 03:16:09 +0000100 if (Function->getFriendObjectKind() &&
Douglas Gregore7089b02010-05-03 23:29:10 +0000101 Function->getDeclContext()->isFileContext() &&
102 (!Pattern || !Pattern->getLexicalDeclContext()->isFileContext())) {
John McCallf181d8a2009-08-29 03:16:09 +0000103 Ctx = Function->getLexicalDeclContext();
Douglas Gregor525f96c2010-02-05 07:33:43 +0000104 RelativeToPrimary = false;
John McCallf181d8a2009-08-29 03:16:09 +0000105 continue;
106 }
Douglas Gregord1102432009-08-28 17:37:35 +0000107 }
John McCallf181d8a2009-08-29 03:16:09 +0000108
109 Ctx = Ctx->getParent();
Douglas Gregor525f96c2010-02-05 07:33:43 +0000110 RelativeToPrimary = false;
Douglas Gregor54dabfc2009-05-14 23:26:13 +0000111 }
Mike Stump1eb44332009-09-09 15:08:12 +0000112
Douglas Gregord1102432009-08-28 17:37:35 +0000113 return Result;
Douglas Gregor54dabfc2009-05-14 23:26:13 +0000114}
115
Douglas Gregorf35f8282009-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 Gregor26dce442009-03-10 00:06:19 +0000133Sema::InstantiatingTemplate::
134InstantiatingTemplate(Sema &SemaRef, SourceLocation PointOfInstantiation,
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000135 Decl *Entity,
Douglas Gregor26dce442009-03-10 00:06:19 +0000136 SourceRange InstantiationRange)
137 : SemaRef(SemaRef) {
Douglas Gregordf667e72009-03-10 20:44:00 +0000138
139 Invalid = CheckInstantiationDepth(PointOfInstantiation,
140 InstantiationRange);
141 if (!Invalid) {
Douglas Gregor26dce442009-03-10 00:06:19 +0000142 ActiveTemplateInstantiation Inst;
Douglas Gregordf667e72009-03-10 20:44:00 +0000143 Inst.Kind = ActiveTemplateInstantiation::TemplateInstantiation;
Douglas Gregor26dce442009-03-10 00:06:19 +0000144 Inst.PointOfInstantiation = PointOfInstantiation;
Douglas Gregordf667e72009-03-10 20:44:00 +0000145 Inst.Entity = reinterpret_cast<uintptr_t>(Entity);
Douglas Gregor313a81d2009-03-12 18:36:18 +0000146 Inst.TemplateArgs = 0;
147 Inst.NumTemplateArgs = 0;
Douglas Gregordf667e72009-03-10 20:44:00 +0000148 Inst.InstantiationRange = InstantiationRange;
149 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
Douglas Gregordf667e72009-03-10 20:44:00 +0000150 }
151}
152
Mike Stump1eb44332009-09-09 15:08:12 +0000153Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef,
Douglas Gregordf667e72009-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 Stump1eb44332009-09-09 15:08:12 +0000165 Inst.Kind
Douglas Gregordf667e72009-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 Gregor26dce442009-03-10 00:06:19 +0000171 Inst.InstantiationRange = InstantiationRange;
172 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
Douglas Gregor26dce442009-03-10 00:06:19 +0000173 }
174}
175
Mike Stump1eb44332009-09-09 15:08:12 +0000176Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef,
Douglas Gregor637a4092009-06-10 23:47:09 +0000177 SourceLocation PointOfInstantiation,
Douglas Gregorcca9e962009-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 Stump1eb44332009-09-09 15:08:12 +0000184
Douglas Gregorcca9e962009-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 Gregorf35f8282009-11-11 21:54:23 +0000196
197 if (!Inst.isInstantiationRecord())
198 ++SemaRef.NonInstantiationEntries;
Douglas Gregorcca9e962009-07-01 22:01:06 +0000199 }
200}
201
Mike Stump1eb44332009-09-09 15:08:12 +0000202Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef,
Douglas Gregorcca9e962009-07-01 22:01:06 +0000203 SourceLocation PointOfInstantiation,
Douglas Gregor637a4092009-06-10 23:47:09 +0000204 ClassTemplatePartialSpecializationDecl *PartialSpec,
205 const TemplateArgument *TemplateArgs,
206 unsigned NumTemplateArgs,
207 SourceRange InstantiationRange)
208 : SemaRef(SemaRef) {
209
Douglas Gregorf35f8282009-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 Gregor637a4092009-06-10 23:47:09 +0000223}
224
Mike Stump1eb44332009-09-09 15:08:12 +0000225Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef,
Douglas Gregor9148c3f2009-11-11 19:13:48 +0000226 SourceLocation PointOfInstantiation,
Anders Carlsson25cae7f2009-09-05 05:14:19 +0000227 ParmVarDecl *Param,
228 const TemplateArgument *TemplateArgs,
229 unsigned NumTemplateArgs,
230 SourceRange InstantiationRange)
231 : SemaRef(SemaRef) {
Mike Stump1eb44332009-09-09 15:08:12 +0000232
Douglas Gregor9148c3f2009-11-11 19:13:48 +0000233 Invalid = CheckInstantiationDepth(PointOfInstantiation, InstantiationRange);
Anders Carlsson25cae7f2009-09-05 05:14:19 +0000234
235 if (!Invalid) {
236 ActiveTemplateInstantiation Inst;
237 Inst.Kind
238 = ActiveTemplateInstantiation::DefaultFunctionArgumentInstantiation;
Douglas Gregor9148c3f2009-11-11 19:13:48 +0000239 Inst.PointOfInstantiation = PointOfInstantiation;
Anders Carlsson25cae7f2009-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 Gregor9148c3f2009-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 Gregorf35f8282009-11-11 21:54:23 +0000255 Invalid = false;
Douglas Gregor9148c3f2009-11-11 19:13:48 +0000256
Douglas Gregorf35f8282009-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 Gregor9148c3f2009-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 Gregorf35f8282009-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 Gregor9148c3f2009-11-11 19:13:48 +0000288
Douglas Gregorf35f8282009-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 Carlsson25cae7f2009-09-05 05:14:19 +0000314}
315
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000316void Sema::InstantiatingTemplate::Clear() {
317 if (!Invalid) {
Douglas Gregorf35f8282009-11-11 21:54:23 +0000318 if (!SemaRef.ActiveTemplateInstantiations.back().isInstantiationRecord()) {
319 assert(SemaRef.NonInstantiationEntries > 0);
320 --SemaRef.NonInstantiationEntries;
321 }
322
Douglas Gregor26dce442009-03-10 00:06:19 +0000323 SemaRef.ActiveTemplateInstantiations.pop_back();
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000324 Invalid = true;
325 }
Douglas Gregor26dce442009-03-10 00:06:19 +0000326}
327
Douglas Gregordf667e72009-03-10 20:44:00 +0000328bool Sema::InstantiatingTemplate::CheckInstantiationDepth(
329 SourceLocation PointOfInstantiation,
330 SourceRange InstantiationRange) {
Douglas Gregorf35f8282009-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 Gregordf667e72009-03-10 20:44:00 +0000336 return false;
337
Mike Stump1eb44332009-09-09 15:08:12 +0000338 SemaRef.Diag(PointOfInstantiation,
Douglas Gregordf667e72009-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 Gregoree1828a2009-03-10 18:03:33 +0000347/// \brief Prints the current instantiation stack through a series of
348/// notes.
349void Sema::PrintInstantiationStack() {
Douglas Gregor575cf372010-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 Gregorcca9e962009-07-01 22:01:06 +0000358 // FIXME: In all of these cases, we need to show the template arguments
Douglas Gregor575cf372010-04-20 07:18:24 +0000359 unsigned InstantiationIdx = 0;
Douglas Gregoree1828a2009-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 Gregor575cf372010-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 Gregordf667e72009-03-10 20:44:00 +0000376 switch (Active->Kind) {
377 case ActiveTemplateInstantiation::TemplateInstantiation: {
Douglas Gregorf3e7ce42009-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 Stump1eb44332009-09-09 15:08:12 +0000383 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000384 DiagID)
385 << Context.getTypeDeclType(Record)
386 << Active->InstantiationRange;
Douglas Gregor7caa6822009-07-24 20:34:43 +0000387 } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
Douglas Gregor1637be72009-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 Stump1eb44332009-09-09 15:08:12 +0000393 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000394 DiagID)
395 << Function
396 << Active->InstantiationRange;
Douglas Gregor7caa6822009-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 Gregorf3e7ce42009-05-18 17:01:57 +0000402 }
Douglas Gregordf667e72009-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 Gregor7532dc62009-03-30 22:58:21 +0000409 = TemplateSpecializationType::PrintTemplateArgumentList(
Mike Stump1eb44332009-09-09 15:08:12 +0000410 Active->TemplateArgs,
Douglas Gregord249e1d1f2009-05-29 20:38:28 +0000411 Active->NumTemplateArgs,
412 Context.PrintingPolicy);
Douglas Gregordf667e72009-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 Gregor637a4092009-06-10 23:47:09 +0000419
Douglas Gregorcca9e962009-07-01 22:01:06 +0000420 case ActiveTemplateInstantiation::ExplicitTemplateArgumentSubstitution: {
Mike Stump1eb44332009-09-09 15:08:12 +0000421 FunctionTemplateDecl *FnTmpl
Douglas Gregorcca9e962009-07-01 22:01:06 +0000422 = cast<FunctionTemplateDecl>((Decl *)Active->Entity);
Douglas Gregor637a4092009-06-10 23:47:09 +0000423 Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
Douglas Gregorcca9e962009-07-01 22:01:06 +0000424 diag::note_explicit_template_arg_substitution_here)
Douglas Gregor5e402912010-03-30 20:35:20 +0000425 << FnTmpl
426 << getTemplateArgumentBindingsText(FnTmpl->getTemplateParameters(),
427 Active->TemplateArgs,
428 Active->NumTemplateArgs)
429 << Active->InstantiationRange;
Douglas Gregor637a4092009-06-10 23:47:09 +0000430 break;
431 }
Mike Stump1eb44332009-09-09 15:08:12 +0000432
Douglas Gregorcca9e962009-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 Gregor5e402912010-03-30 20:35:20 +0000440 << getTemplateArgumentBindingsText(
441 PartialSpec->getTemplateParameters(),
442 Active->TemplateArgs,
443 Active->NumTemplateArgs)
Douglas Gregorcca9e962009-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 Gregor5e402912010-03-30 20:35:20 +0000450 << FnTmpl
451 << getTemplateArgumentBindingsText(FnTmpl->getTemplateParameters(),
452 Active->TemplateArgs,
453 Active->NumTemplateArgs)
454 << Active->InstantiationRange;
Douglas Gregorcca9e962009-07-01 22:01:06 +0000455 }
456 break;
Douglas Gregor637a4092009-06-10 23:47:09 +0000457
Anders Carlsson25cae7f2009-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 Stump1eb44332009-09-09 15:08:12 +0000461
Anders Carlsson25cae7f2009-09-05 05:14:19 +0000462 std::string TemplateArgsStr
463 = TemplateSpecializationType::PrintTemplateArgumentList(
Mike Stump1eb44332009-09-09 15:08:12 +0000464 Active->TemplateArgs,
Anders Carlsson25cae7f2009-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 Carlsson6bc107b2009-09-05 05:38:54 +0000469 << (FD->getNameAsString() + TemplateArgsStr)
Anders Carlsson25cae7f2009-09-05 05:14:19 +0000470 << Active->InstantiationRange;
471 break;
472 }
Mike Stump1eb44332009-09-09 15:08:12 +0000473
Douglas Gregor9148c3f2009-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 Gregorf35f8282009-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 Gregordf667e72009-03-10 20:44:00 +0000502 }
Douglas Gregoree1828a2009-03-10 18:03:33 +0000503 }
504}
505
Douglas Gregor5e9f35c2009-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 Gregorf35f8282009-11-11 21:54:23 +0000512 ++Active)
513 {
Douglas Gregor5e9f35c2009-06-14 07:33:30 +0000514 switch(Active->Kind) {
Douglas Gregorcca9e962009-07-01 22:01:06 +0000515 case ActiveTemplateInstantiation::TemplateInstantiation:
Anders Carlsson25cae7f2009-09-05 05:14:19 +0000516 case ActiveTemplateInstantiation::DefaultFunctionArgumentInstantiation:
Douglas Gregorcca9e962009-07-01 22:01:06 +0000517 // This is a template instantiation, so there is no SFINAE.
518 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000519
Douglas Gregor5e9f35c2009-06-14 07:33:30 +0000520 case ActiveTemplateInstantiation::DefaultTemplateArgumentInstantiation:
Douglas Gregor9148c3f2009-11-11 19:13:48 +0000521 case ActiveTemplateInstantiation::PriorTemplateArgumentSubstitution:
Douglas Gregorf35f8282009-11-11 21:54:23 +0000522 case ActiveTemplateInstantiation::DefaultTemplateArgumentChecking:
Douglas Gregor9148c3f2009-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 Gregor5e9f35c2009-06-14 07:33:30 +0000526 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000527
Douglas Gregorcca9e962009-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 Gregor5e9f35c2009-06-14 07:33:30 +0000533 }
534 }
535
536 return false;
537}
538
Douglas Gregor99ebf652009-02-27 19:31:52 +0000539//===----------------------------------------------------------------------===/
540// Template Instantiation for Types
541//===----------------------------------------------------------------------===/
Douglas Gregorcd281c32009-02-28 00:25:32 +0000542namespace {
Douglas Gregor895162d2010-04-30 18:55:50 +0000543 class TemplateInstantiator : public TreeTransform<TemplateInstantiator> {
Douglas Gregord6350ae2009-08-28 20:31:08 +0000544 const MultiLevelTemplateArgumentList &TemplateArgs;
Douglas Gregorcd281c32009-02-28 00:25:32 +0000545 SourceLocation Loc;
546 DeclarationName Entity;
Douglas Gregor99ebf652009-02-27 19:31:52 +0000547
Douglas Gregorcd281c32009-02-28 00:25:32 +0000548 public:
Douglas Gregor43959a92009-08-20 07:17:43 +0000549 typedef TreeTransform<TemplateInstantiator> inherited;
Mike Stump1eb44332009-09-09 15:08:12 +0000550
551 TemplateInstantiator(Sema &SemaRef,
Douglas Gregord6350ae2009-08-28 20:31:08 +0000552 const MultiLevelTemplateArgumentList &TemplateArgs,
Douglas Gregor577f75a2009-08-04 16:50:30 +0000553 SourceLocation Loc,
Mike Stump1eb44332009-09-09 15:08:12 +0000554 DeclarationName Entity)
555 : inherited(SemaRef), TemplateArgs(TemplateArgs), Loc(Loc),
Douglas Gregor43959a92009-08-20 07:17:43 +0000556 Entity(Entity) { }
Douglas Gregorcd281c32009-02-28 00:25:32 +0000557
Mike Stump1eb44332009-09-09 15:08:12 +0000558 /// \brief Determine whether the given type \p T has already been
Douglas Gregor577f75a2009-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 Gregorb4eeaff2010-05-07 23:12:07 +0000563 bool AlreadyTransformed(QualType T);
Mike Stump1eb44332009-09-09 15:08:12 +0000564
Douglas Gregor577f75a2009-08-04 16:50:30 +0000565 /// \brief Returns the location of the entity being instantiated, if known.
566 SourceLocation getBaseLocation() { return Loc; }
Mike Stump1eb44332009-09-09 15:08:12 +0000567
Douglas Gregor577f75a2009-08-04 16:50:30 +0000568 /// \brief Returns the name of the entity being instantiated, if any.
569 DeclarationName getBaseEntity() { return Entity; }
Mike Stump1eb44332009-09-09 15:08:12 +0000570
Douglas Gregor972e6ce2009-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 Gregor577f75a2009-08-04 16:50:30 +0000578 /// \brief Transform the given declaration by instantiating a reference to
579 /// this declaration.
Douglas Gregor7c1e98f2010-03-01 15:56:25 +0000580 Decl *TransformDecl(SourceLocation Loc, Decl *D);
Douglas Gregorb98b1992009-08-11 05:31:07 +0000581
Mike Stump1eb44332009-09-09 15:08:12 +0000582 /// \brief Transform the definition of the given declaration by
Douglas Gregor43959a92009-08-20 07:17:43 +0000583 /// instantiating it.
Douglas Gregoraac571c2010-03-01 17:25:41 +0000584 Decl *TransformDefinition(SourceLocation Loc, Decl *D);
Mike Stump1eb44332009-09-09 15:08:12 +0000585
Douglas Gregor6cd21982009-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 Gregor43959a92009-08-20 07:17:43 +0000590 /// \brief Rebuild the exception declaration and register the declaration
591 /// as an instantiated local.
Mike Stump1eb44332009-09-09 15:08:12 +0000592 VarDecl *RebuildExceptionDecl(VarDecl *ExceptionDecl, QualType T,
John McCalla93c9342009-12-07 02:54:59 +0000593 TypeSourceInfo *Declarator,
Douglas Gregor43959a92009-08-20 07:17:43 +0000594 IdentifierInfo *Name,
595 SourceLocation Loc, SourceRange TypeRange);
Mike Stump1eb44332009-09-09 15:08:12 +0000596
Douglas Gregorbe270a02010-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 McCallc4e70192009-09-11 04:59:25 +0000602 /// \brief Check for tag mismatches when instantiating an
603 /// elaborated type.
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000604 QualType RebuildElaboratedType(ElaboratedTypeKeyword Keyword,
605 NestedNameSpecifier *NNS, QualType T);
John McCallc4e70192009-09-11 04:59:25 +0000606
John McCall454feb92009-12-08 09:21:05 +0000607 Sema::OwningExprResult TransformPredefinedExpr(PredefinedExpr *E);
608 Sema::OwningExprResult TransformDeclRefExpr(DeclRefExpr *E);
John McCall454feb92009-12-08 09:21:05 +0000609 Sema::OwningExprResult TransformCXXDefaultArgExpr(CXXDefaultArgExpr *E);
John McCallb8fc0532010-02-06 08:42:39 +0000610 Sema::OwningExprResult TransformTemplateParmRefExpr(DeclRefExpr *E,
611 NonTypeTemplateParmDecl *D);
Sebastian Redla29e51b2009-11-08 13:56:19 +0000612
Douglas Gregor895162d2010-04-30 18:55:50 +0000613 QualType TransformFunctionProtoType(TypeLocBuilder &TLB,
614 FunctionProtoTypeLoc TL,
615 QualType ObjectType);
John McCall21ef0fa2010-03-11 09:03:00 +0000616 ParmVarDecl *TransformFunctionTypeParam(ParmVarDecl *OldParm);
617
Mike Stump1eb44332009-09-09 15:08:12 +0000618 /// \brief Transforms a template type parameter type by performing
Douglas Gregor577f75a2009-08-04 16:50:30 +0000619 /// substitution of the corresponding template type argument.
John McCalla2becad2009-10-21 00:40:46 +0000620 QualType TransformTemplateTypeParmType(TypeLocBuilder &TLB,
Douglas Gregor124b8782010-02-16 19:09:40 +0000621 TemplateTypeParmTypeLoc TL,
622 QualType ObjectType);
Douglas Gregor577f75a2009-08-04 16:50:30 +0000623 };
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000624}
625
Douglas Gregorb4eeaff2010-05-07 23:12:07 +0000626bool TemplateInstantiator::AlreadyTransformed(QualType T) {
627 if (T.isNull())
628 return true;
629
Douglas Gregor836adf62010-05-24 17:22:01 +0000630 if (T->isDependentType() || T->isVariablyModifiedType())
Douglas Gregorb4eeaff2010-05-07 23:12:07 +0000631 return false;
632
633 getSema().MarkDeclarationsReferencedInType(Loc, T);
634 return true;
635}
636
Douglas Gregor7c1e98f2010-03-01 15:56:25 +0000637Decl *TemplateInstantiator::TransformDecl(SourceLocation Loc, Decl *D) {
Douglas Gregorc68afe22009-09-03 21:38:09 +0000638 if (!D)
639 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000640
Douglas Gregorc68afe22009-09-03 21:38:09 +0000641 if (TemplateTemplateParmDecl *TTP = dyn_cast<TemplateTemplateParmDecl>(D)) {
Douglas Gregord6350ae2009-08-28 20:31:08 +0000642 if (TTP->getDepth() < TemplateArgs.getNumLevels()) {
Douglas Gregor6d3e6272010-02-05 19:54:12 +0000643 // If the corresponding template argument is NULL or non-existent, it's
644 // because we are performing instantiation from explicitly-specified
645 // template arguments in a function template, but there were some
646 // arguments left unspecified.
647 if (!TemplateArgs.hasTemplateArgument(TTP->getDepth(),
648 TTP->getPosition()))
649 return D;
650
Douglas Gregor788cd062009-11-11 01:00:40 +0000651 TemplateName Template
652 = TemplateArgs(TTP->getDepth(), TTP->getPosition()).getAsTemplate();
653 assert(!Template.isNull() && Template.getAsTemplateDecl() &&
Douglas Gregord6350ae2009-08-28 20:31:08 +0000654 "Wrong kind of template template argument");
Douglas Gregor788cd062009-11-11 01:00:40 +0000655 return Template.getAsTemplateDecl();
Douglas Gregord6350ae2009-08-28 20:31:08 +0000656 }
Mike Stump1eb44332009-09-09 15:08:12 +0000657
Douglas Gregor788cd062009-11-11 01:00:40 +0000658 // Fall through to find the instantiated declaration for this template
659 // template parameter.
Douglas Gregord1067e52009-08-06 06:41:21 +0000660 }
Mike Stump1eb44332009-09-09 15:08:12 +0000661
Douglas Gregor7c1e98f2010-03-01 15:56:25 +0000662 return SemaRef.FindInstantiatedDecl(Loc, cast<NamedDecl>(D), TemplateArgs);
Douglas Gregor577f75a2009-08-04 16:50:30 +0000663}
664
Douglas Gregoraac571c2010-03-01 17:25:41 +0000665Decl *TemplateInstantiator::TransformDefinition(SourceLocation Loc, Decl *D) {
John McCallce3ff2b2009-08-25 22:02:44 +0000666 Decl *Inst = getSema().SubstDecl(D, getSema().CurContext, TemplateArgs);
Douglas Gregor43959a92009-08-20 07:17:43 +0000667 if (!Inst)
668 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000669
Douglas Gregor43959a92009-08-20 07:17:43 +0000670 getSema().CurrentInstantiationScope->InstantiatedLocal(D, Inst);
671 return Inst;
672}
673
Douglas Gregor6cd21982009-10-20 05:58:46 +0000674NamedDecl *
675TemplateInstantiator::TransformFirstQualifierInScope(NamedDecl *D,
676 SourceLocation Loc) {
677 // If the first part of the nested-name-specifier was a template type
678 // parameter, instantiate that type parameter down to a tag type.
679 if (TemplateTypeParmDecl *TTPD = dyn_cast_or_null<TemplateTypeParmDecl>(D)) {
680 const TemplateTypeParmType *TTP
681 = cast<TemplateTypeParmType>(getSema().Context.getTypeDeclType(TTPD));
682 if (TTP->getDepth() < TemplateArgs.getNumLevels()) {
683 QualType T = TemplateArgs(TTP->getDepth(), TTP->getIndex()).getAsType();
684 if (T.isNull())
Douglas Gregor7c1e98f2010-03-01 15:56:25 +0000685 return cast_or_null<NamedDecl>(TransformDecl(Loc, D));
Douglas Gregor6cd21982009-10-20 05:58:46 +0000686
687 if (const TagType *Tag = T->getAs<TagType>())
688 return Tag->getDecl();
689
690 // The resulting type is not a tag; complain.
691 getSema().Diag(Loc, diag::err_nested_name_spec_non_tag) << T;
692 return 0;
693 }
694 }
695
Douglas Gregor7c1e98f2010-03-01 15:56:25 +0000696 return cast_or_null<NamedDecl>(TransformDecl(Loc, D));
Douglas Gregor6cd21982009-10-20 05:58:46 +0000697}
698
Douglas Gregor43959a92009-08-20 07:17:43 +0000699VarDecl *
700TemplateInstantiator::RebuildExceptionDecl(VarDecl *ExceptionDecl,
Mike Stump1eb44332009-09-09 15:08:12 +0000701 QualType T,
John McCalla93c9342009-12-07 02:54:59 +0000702 TypeSourceInfo *Declarator,
Douglas Gregor43959a92009-08-20 07:17:43 +0000703 IdentifierInfo *Name,
Mike Stump1eb44332009-09-09 15:08:12 +0000704 SourceLocation Loc,
Douglas Gregor43959a92009-08-20 07:17:43 +0000705 SourceRange TypeRange) {
706 VarDecl *Var = inherited::RebuildExceptionDecl(ExceptionDecl, T, Declarator,
707 Name, Loc, TypeRange);
Douglas Gregorbe270a02010-04-26 17:57:08 +0000708 if (Var)
709 getSema().CurrentInstantiationScope->InstantiatedLocal(ExceptionDecl, Var);
710 return Var;
711}
712
713VarDecl *TemplateInstantiator::RebuildObjCExceptionDecl(VarDecl *ExceptionDecl,
714 TypeSourceInfo *TSInfo,
715 QualType T) {
716 VarDecl *Var = inherited::RebuildObjCExceptionDecl(ExceptionDecl, TSInfo, T);
717 if (Var)
Douglas Gregor43959a92009-08-20 07:17:43 +0000718 getSema().CurrentInstantiationScope->InstantiatedLocal(ExceptionDecl, Var);
719 return Var;
720}
721
John McCallc4e70192009-09-11 04:59:25 +0000722QualType
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000723TemplateInstantiator::RebuildElaboratedType(ElaboratedTypeKeyword Keyword,
724 NestedNameSpecifier *NNS,
725 QualType T) {
John McCallc4e70192009-09-11 04:59:25 +0000726 if (const TagType *TT = T->getAs<TagType>()) {
727 TagDecl* TD = TT->getDecl();
728
729 // FIXME: this location is very wrong; we really need typelocs.
730 SourceLocation TagLocation = TD->getTagKeywordLoc();
731
732 // FIXME: type might be anonymous.
733 IdentifierInfo *Id = TD->getIdentifier();
734
735 // TODO: should we even warn on struct/class mismatches for this? Seems
736 // like it's likely to produce a lot of spurious errors.
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000737 if (Keyword != ETK_None && Keyword != ETK_Typename) {
738 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForKeyword(Keyword);
739 if (!SemaRef.isAcceptableTagRedeclaration(TD, Kind, TagLocation, *Id)) {
740 SemaRef.Diag(TagLocation, diag::err_use_with_wrong_tag)
741 << Id
742 << FixItHint::CreateReplacement(SourceRange(TagLocation),
743 TD->getKindName());
744 SemaRef.Diag(TD->getLocation(), diag::note_previous_use);
745 }
John McCallc4e70192009-09-11 04:59:25 +0000746 }
747 }
748
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000749 return TreeTransform<TemplateInstantiator>::RebuildElaboratedType(Keyword,
750 NNS, T);
John McCallc4e70192009-09-11 04:59:25 +0000751}
752
753Sema::OwningExprResult
John McCall454feb92009-12-08 09:21:05 +0000754TemplateInstantiator::TransformPredefinedExpr(PredefinedExpr *E) {
Anders Carlsson773f3972009-09-11 01:22:35 +0000755 if (!E->isTypeDependent())
756 return SemaRef.Owned(E->Retain());
757
758 FunctionDecl *currentDecl = getSema().getCurFunctionDecl();
759 assert(currentDecl && "Must have current function declaration when "
760 "instantiating.");
761
762 PredefinedExpr::IdentType IT = E->getIdentType();
763
Anders Carlsson848fa642010-02-11 18:20:28 +0000764 unsigned Length = PredefinedExpr::ComputeName(IT, currentDecl).length();
Anders Carlsson773f3972009-09-11 01:22:35 +0000765
766 llvm::APInt LengthI(32, Length + 1);
John McCall0953e762009-09-24 19:53:00 +0000767 QualType ResTy = getSema().Context.CharTy.withConst();
Anders Carlsson773f3972009-09-11 01:22:35 +0000768 ResTy = getSema().Context.getConstantArrayType(ResTy, LengthI,
769 ArrayType::Normal, 0);
770 PredefinedExpr *PE =
771 new (getSema().Context) PredefinedExpr(E->getLocation(), ResTy, IT);
772 return getSema().Owned(PE);
773}
774
775Sema::OwningExprResult
John McCallb8fc0532010-02-06 08:42:39 +0000776TemplateInstantiator::TransformTemplateParmRefExpr(DeclRefExpr *E,
Douglas Gregordcee9802010-02-08 23:41:45 +0000777 NonTypeTemplateParmDecl *NTTP) {
John McCallb8fc0532010-02-06 08:42:39 +0000778 // If the corresponding template argument is NULL or non-existent, it's
779 // because we are performing instantiation from explicitly-specified
780 // template arguments in a function template, but there were some
781 // arguments left unspecified.
782 if (!TemplateArgs.hasTemplateArgument(NTTP->getDepth(),
783 NTTP->getPosition()))
784 return SemaRef.Owned(E->Retain());
Mike Stump1eb44332009-09-09 15:08:12 +0000785
John McCallb8fc0532010-02-06 08:42:39 +0000786 const TemplateArgument &Arg = TemplateArgs(NTTP->getDepth(),
787 NTTP->getPosition());
Mike Stump1eb44332009-09-09 15:08:12 +0000788
John McCallb8fc0532010-02-06 08:42:39 +0000789 // The template argument itself might be an expression, in which
790 // case we just return that expression.
791 if (Arg.getKind() == TemplateArgument::Expression)
792 return SemaRef.Owned(Arg.getAsExpr()->Retain());
Mike Stump1eb44332009-09-09 15:08:12 +0000793
John McCallb8fc0532010-02-06 08:42:39 +0000794 if (Arg.getKind() == TemplateArgument::Declaration) {
795 ValueDecl *VD = cast<ValueDecl>(Arg.getAsDecl());
Mike Stump1eb44332009-09-09 15:08:12 +0000796
John McCall645cf442010-02-06 10:23:53 +0000797 // Find the instantiation of the template argument. This is
798 // required for nested templates.
John McCallb8fc0532010-02-06 08:42:39 +0000799 VD = cast_or_null<ValueDecl>(
Douglas Gregor7c1e98f2010-03-01 15:56:25 +0000800 getSema().FindInstantiatedDecl(E->getLocation(),
801 VD, TemplateArgs));
John McCallb8fc0532010-02-06 08:42:39 +0000802 if (!VD)
803 return SemaRef.ExprError();
Mike Stump1eb44332009-09-09 15:08:12 +0000804
John McCall645cf442010-02-06 10:23:53 +0000805 // Derive the type we want the substituted decl to have. This had
806 // better be non-dependent, or these checks will have serious problems.
807 QualType TargetType = SemaRef.SubstType(NTTP->getType(), TemplateArgs,
Douglas Gregordcee9802010-02-08 23:41:45 +0000808 E->getLocation(),
809 DeclarationName());
John McCall645cf442010-02-06 10:23:53 +0000810 assert(!TargetType.isNull() && "type substitution failed for param type");
811 assert(!TargetType->isDependentType() && "param type still dependent");
Douglas Gregor02024a92010-03-28 02:42:43 +0000812 return SemaRef.BuildExpressionFromDeclTemplateArgument(Arg,
813 TargetType,
814 E->getLocation());
John McCallb8fc0532010-02-06 08:42:39 +0000815 }
816
Douglas Gregor02024a92010-03-28 02:42:43 +0000817 return SemaRef.BuildExpressionFromIntegralTemplateArgument(Arg,
818 E->getSourceRange().getBegin());
John McCallb8fc0532010-02-06 08:42:39 +0000819}
820
821
822Sema::OwningExprResult
823TemplateInstantiator::TransformDeclRefExpr(DeclRefExpr *E) {
824 NamedDecl *D = E->getDecl();
825 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(D)) {
826 if (NTTP->getDepth() < TemplateArgs.getNumLevels())
827 return TransformTemplateParmRefExpr(E, NTTP);
Douglas Gregor550d9b22009-10-31 17:21:17 +0000828
829 // We have a non-type template parameter that isn't fully substituted;
830 // FindInstantiatedDecl will find it in the local instantiation scope.
Douglas Gregorb98b1992009-08-11 05:31:07 +0000831 }
Mike Stump1eb44332009-09-09 15:08:12 +0000832
John McCall454feb92009-12-08 09:21:05 +0000833 return TreeTransform<TemplateInstantiator>::TransformDeclRefExpr(E);
Douglas Gregorb98b1992009-08-11 05:31:07 +0000834}
835
Sebastian Redla29e51b2009-11-08 13:56:19 +0000836Sema::OwningExprResult TemplateInstantiator::TransformCXXDefaultArgExpr(
John McCall454feb92009-12-08 09:21:05 +0000837 CXXDefaultArgExpr *E) {
Sebastian Redla29e51b2009-11-08 13:56:19 +0000838 assert(!cast<FunctionDecl>(E->getParam()->getDeclContext())->
839 getDescribedFunctionTemplate() &&
840 "Default arg expressions are never formed in dependent cases.");
Douglas Gregor036aed12009-12-23 23:03:06 +0000841 return SemaRef.BuildCXXDefaultArgExpr(E->getUsedLocation(),
842 cast<FunctionDecl>(E->getParam()->getDeclContext()),
843 E->getParam());
Sebastian Redla29e51b2009-11-08 13:56:19 +0000844}
845
Douglas Gregor895162d2010-04-30 18:55:50 +0000846QualType TemplateInstantiator::TransformFunctionProtoType(TypeLocBuilder &TLB,
847 FunctionProtoTypeLoc TL,
848 QualType ObjectType) {
849 // We need a local instantiation scope for this function prototype.
850 Sema::LocalInstantiationScope Scope(SemaRef, /*CombineWithOuterScope=*/true);
851 return inherited::TransformFunctionProtoType(TLB, TL, ObjectType);
John McCall21ef0fa2010-03-11 09:03:00 +0000852}
853
854ParmVarDecl *
855TemplateInstantiator::TransformFunctionTypeParam(ParmVarDecl *OldParm) {
Douglas Gregorcb27b0f2010-04-12 07:48:19 +0000856 return SemaRef.SubstParmVarDecl(OldParm, TemplateArgs);
John McCall21ef0fa2010-03-11 09:03:00 +0000857}
858
Mike Stump1eb44332009-09-09 15:08:12 +0000859QualType
John McCalla2becad2009-10-21 00:40:46 +0000860TemplateInstantiator::TransformTemplateTypeParmType(TypeLocBuilder &TLB,
Douglas Gregor124b8782010-02-16 19:09:40 +0000861 TemplateTypeParmTypeLoc TL,
862 QualType ObjectType) {
John McCalla2becad2009-10-21 00:40:46 +0000863 TemplateTypeParmType *T = TL.getTypePtr();
Douglas Gregord6350ae2009-08-28 20:31:08 +0000864 if (T->getDepth() < TemplateArgs.getNumLevels()) {
Douglas Gregor99ebf652009-02-27 19:31:52 +0000865 // Replace the template type parameter with its corresponding
866 // template argument.
Mike Stump1eb44332009-09-09 15:08:12 +0000867
868 // If the corresponding template argument is NULL or doesn't exist, it's
869 // because we are performing instantiation from explicitly-specified
870 // template arguments in a function template class, but there were some
Douglas Gregor16134c62009-07-01 00:28:38 +0000871 // arguments left unspecified.
John McCalla2becad2009-10-21 00:40:46 +0000872 if (!TemplateArgs.hasTemplateArgument(T->getDepth(), T->getIndex())) {
873 TemplateTypeParmTypeLoc NewTL
874 = TLB.push<TemplateTypeParmTypeLoc>(TL.getType());
875 NewTL.setNameLoc(TL.getNameLoc());
876 return TL.getType();
877 }
Mike Stump1eb44332009-09-09 15:08:12 +0000878
879 assert(TemplateArgs(T->getDepth(), T->getIndex()).getKind()
Douglas Gregord6350ae2009-08-28 20:31:08 +0000880 == TemplateArgument::Type &&
Douglas Gregor99ebf652009-02-27 19:31:52 +0000881 "Template argument kind mismatch");
Douglas Gregord6350ae2009-08-28 20:31:08 +0000882
John McCall49a832b2009-10-18 09:09:24 +0000883 QualType Replacement
884 = TemplateArgs(T->getDepth(), T->getIndex()).getAsType();
885
886 // TODO: only do this uniquing once, at the start of instantiation.
John McCalla2becad2009-10-21 00:40:46 +0000887 QualType Result
888 = getSema().Context.getSubstTemplateTypeParmType(T, Replacement);
889 SubstTemplateTypeParmTypeLoc NewTL
890 = TLB.push<SubstTemplateTypeParmTypeLoc>(Result);
891 NewTL.setNameLoc(TL.getNameLoc());
892 return Result;
Mike Stump1eb44332009-09-09 15:08:12 +0000893 }
Douglas Gregor99ebf652009-02-27 19:31:52 +0000894
895 // The template type parameter comes from an inner template (e.g.,
896 // the template parameter list of a member template inside the
897 // template we are instantiating). Create a new template type
898 // parameter with the template "level" reduced by one.
Abramo Bagnara6cd80872010-06-16 14:59:30 +0000899 TemplateTypeParmDecl *NewTTPDecl = 0;
900 if (TemplateTypeParmDecl *OldTTPDecl = T->getDecl())
901 NewTTPDecl = cast_or_null<TemplateTypeParmDecl>(
902 TransformDecl(TL.getNameLoc(), OldTTPDecl));
903
John McCalla2becad2009-10-21 00:40:46 +0000904 QualType Result
905 = getSema().Context.getTemplateTypeParmType(T->getDepth()
906 - TemplateArgs.getNumLevels(),
907 T->getIndex(),
908 T->isParameterPack(),
Abramo Bagnara6cd80872010-06-16 14:59:30 +0000909 NewTTPDecl);
John McCalla2becad2009-10-21 00:40:46 +0000910 TemplateTypeParmTypeLoc NewTL = TLB.push<TemplateTypeParmTypeLoc>(Result);
911 NewTL.setNameLoc(TL.getNameLoc());
912 return Result;
Douglas Gregorcd281c32009-02-28 00:25:32 +0000913}
Douglas Gregor99ebf652009-02-27 19:31:52 +0000914
John McCallce3ff2b2009-08-25 22:02:44 +0000915/// \brief Perform substitution on the type T with a given set of template
916/// arguments.
Douglas Gregor99ebf652009-02-27 19:31:52 +0000917///
918/// This routine substitutes the given template arguments into the
919/// type T and produces the instantiated type.
920///
921/// \param T the type into which the template arguments will be
922/// substituted. If this type is not dependent, it will be returned
923/// immediately.
924///
925/// \param TemplateArgs the template arguments that will be
926/// substituted for the top-level template parameters within T.
927///
Douglas Gregor99ebf652009-02-27 19:31:52 +0000928/// \param Loc the location in the source code where this substitution
929/// is being performed. It will typically be the location of the
930/// declarator (if we're instantiating the type of some declaration)
931/// or the location of the type in the source code (if, e.g., we're
932/// instantiating the type of a cast expression).
933///
934/// \param Entity the name of the entity associated with a declaration
935/// being instantiated (if any). May be empty to indicate that there
936/// is no such entity (if, e.g., this is a type that occurs as part of
937/// a cast expression) or that the entity has no name (e.g., an
938/// unnamed function parameter).
939///
940/// \returns If the instantiation succeeds, the instantiated
941/// type. Otherwise, produces diagnostics and returns a NULL type.
John McCalla93c9342009-12-07 02:54:59 +0000942TypeSourceInfo *Sema::SubstType(TypeSourceInfo *T,
John McCallcd7ba1c2009-10-21 00:58:09 +0000943 const MultiLevelTemplateArgumentList &Args,
944 SourceLocation Loc,
945 DeclarationName Entity) {
946 assert(!ActiveTemplateInstantiations.empty() &&
947 "Cannot perform an instantiation without some context on the "
948 "instantiation stack");
949
Douglas Gregor836adf62010-05-24 17:22:01 +0000950 if (!T->getType()->isDependentType() &&
951 !T->getType()->isVariablyModifiedType())
John McCallcd7ba1c2009-10-21 00:58:09 +0000952 return T;
953
954 TemplateInstantiator Instantiator(*this, Args, Loc, Entity);
955 return Instantiator.TransformType(T);
956}
957
958/// Deprecated form of the above.
Mike Stump1eb44332009-09-09 15:08:12 +0000959QualType Sema::SubstType(QualType T,
Douglas Gregord6350ae2009-08-28 20:31:08 +0000960 const MultiLevelTemplateArgumentList &TemplateArgs,
John McCallce3ff2b2009-08-25 22:02:44 +0000961 SourceLocation Loc, DeclarationName Entity) {
Douglas Gregordf667e72009-03-10 20:44:00 +0000962 assert(!ActiveTemplateInstantiations.empty() &&
963 "Cannot perform an instantiation without some context on the "
964 "instantiation stack");
965
Douglas Gregor836adf62010-05-24 17:22:01 +0000966 // If T is not a dependent type or a variably-modified type, there
967 // is nothing to do.
968 if (!T->isDependentType() && !T->isVariablyModifiedType())
Douglas Gregor99ebf652009-02-27 19:31:52 +0000969 return T;
970
Douglas Gregor577f75a2009-08-04 16:50:30 +0000971 TemplateInstantiator Instantiator(*this, TemplateArgs, Loc, Entity);
972 return Instantiator.TransformType(T);
Douglas Gregor99ebf652009-02-27 19:31:52 +0000973}
Douglas Gregor2943aed2009-03-03 04:44:36 +0000974
John McCall6cd3b9f2010-04-09 17:38:44 +0000975static bool NeedsInstantiationAsFunctionType(TypeSourceInfo *T) {
Douglas Gregor836adf62010-05-24 17:22:01 +0000976 if (T->getType()->isDependentType() || T->getType()->isVariablyModifiedType())
John McCall6cd3b9f2010-04-09 17:38:44 +0000977 return true;
978
979 TypeLoc TL = T->getTypeLoc();
980 if (!isa<FunctionProtoTypeLoc>(TL))
981 return false;
982
983 FunctionProtoTypeLoc FP = cast<FunctionProtoTypeLoc>(TL);
984 for (unsigned I = 0, E = FP.getNumArgs(); I != E; ++I) {
985 ParmVarDecl *P = FP.getArg(I);
986
987 // TODO: currently we always rebuild expressions. When we
988 // properly get lazier about this, we should use the same
989 // logic to avoid rebuilding prototypes here.
990 if (P->hasInit())
991 return true;
992 }
993
994 return false;
995}
996
997/// A form of SubstType intended specifically for instantiating the
998/// type of a FunctionDecl. Its purpose is solely to force the
999/// instantiation of default-argument expressions.
1000TypeSourceInfo *Sema::SubstFunctionDeclType(TypeSourceInfo *T,
1001 const MultiLevelTemplateArgumentList &Args,
1002 SourceLocation Loc,
1003 DeclarationName Entity) {
1004 assert(!ActiveTemplateInstantiations.empty() &&
1005 "Cannot perform an instantiation without some context on the "
1006 "instantiation stack");
1007
1008 if (!NeedsInstantiationAsFunctionType(T))
1009 return T;
1010
1011 TemplateInstantiator Instantiator(*this, Args, Loc, Entity);
1012
1013 TypeLocBuilder TLB;
1014
1015 TypeLoc TL = T->getTypeLoc();
1016 TLB.reserve(TL.getFullDataSize());
1017
1018 QualType Result = Instantiator.TransformType(TLB, TL, QualType());
1019 if (Result.isNull())
1020 return 0;
1021
1022 return TLB.getTypeSourceInfo(Context, Result);
1023}
1024
Douglas Gregorcb27b0f2010-04-12 07:48:19 +00001025ParmVarDecl *Sema::SubstParmVarDecl(ParmVarDecl *OldParm,
1026 const MultiLevelTemplateArgumentList &TemplateArgs) {
1027 TypeSourceInfo *OldDI = OldParm->getTypeSourceInfo();
1028 TypeSourceInfo *NewDI = SubstType(OldDI, TemplateArgs, OldParm->getLocation(),
1029 OldParm->getDeclName());
1030 if (!NewDI)
1031 return 0;
1032
1033 if (NewDI->getType()->isVoidType()) {
1034 Diag(OldParm->getLocation(), diag::err_param_with_void_type);
1035 return 0;
1036 }
1037
1038 ParmVarDecl *NewParm = CheckParameter(Context.getTranslationUnitDecl(),
1039 NewDI, NewDI->getType(),
1040 OldParm->getIdentifier(),
1041 OldParm->getLocation(),
Douglas Gregor16573fa2010-04-19 22:54:31 +00001042 OldParm->getStorageClass(),
1043 OldParm->getStorageClassAsWritten());
Douglas Gregorcb27b0f2010-04-12 07:48:19 +00001044 if (!NewParm)
1045 return 0;
Douglas Gregor4469e8a2010-05-19 17:02:24 +00001046
Douglas Gregorcb27b0f2010-04-12 07:48:19 +00001047 // Mark the (new) default argument as uninstantiated (if any).
1048 if (OldParm->hasUninstantiatedDefaultArg()) {
1049 Expr *Arg = OldParm->getUninstantiatedDefaultArg();
1050 NewParm->setUninstantiatedDefaultArg(Arg);
1051 } else if (Expr *Arg = OldParm->getDefaultArg())
1052 NewParm->setUninstantiatedDefaultArg(Arg);
1053
1054 NewParm->setHasInheritedDefaultArg(OldParm->hasInheritedDefaultArg());
1055
1056 CurrentInstantiationScope->InstantiatedLocal(OldParm, NewParm);
1057 return NewParm;
1058}
1059
John McCallce3ff2b2009-08-25 22:02:44 +00001060/// \brief Perform substitution on the base class specifiers of the
1061/// given class template specialization.
Douglas Gregor2943aed2009-03-03 04:44:36 +00001062///
1063/// Produces a diagnostic and returns true on error, returns false and
1064/// attaches the instantiated base classes to the class template
1065/// specialization if successful.
Mike Stump1eb44332009-09-09 15:08:12 +00001066bool
John McCallce3ff2b2009-08-25 22:02:44 +00001067Sema::SubstBaseSpecifiers(CXXRecordDecl *Instantiation,
1068 CXXRecordDecl *Pattern,
Douglas Gregord6350ae2009-08-28 20:31:08 +00001069 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor2943aed2009-03-03 04:44:36 +00001070 bool Invalid = false;
Douglas Gregor0ca20ac2009-05-29 18:27:38 +00001071 llvm::SmallVector<CXXBaseSpecifier*, 4> InstantiatedBases;
Mike Stump1eb44332009-09-09 15:08:12 +00001072 for (ClassTemplateSpecializationDecl::base_class_iterator
Douglas Gregord475b8d2009-03-25 21:17:03 +00001073 Base = Pattern->bases_begin(), BaseEnd = Pattern->bases_end();
Douglas Gregor27b152f2009-03-10 18:52:44 +00001074 Base != BaseEnd; ++Base) {
Douglas Gregor2943aed2009-03-03 04:44:36 +00001075 if (!Base->getType()->isDependentType()) {
Anders Carlsson51f94042009-12-03 17:49:57 +00001076 const CXXRecordDecl *BaseDecl =
1077 cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
1078
1079 // Make sure to set the attributes from the base.
1080 SetClassDeclAttributesFromBase(Instantiation, BaseDecl,
1081 Base->isVirtual());
1082
Fariborz Jahanian71c6e712009-07-22 17:41:53 +00001083 InstantiatedBases.push_back(new (Context) CXXBaseSpecifier(*Base));
Douglas Gregor2943aed2009-03-03 04:44:36 +00001084 continue;
1085 }
1086
Mike Stump1eb44332009-09-09 15:08:12 +00001087 QualType BaseType = SubstType(Base->getType(),
1088 TemplateArgs,
John McCallce3ff2b2009-08-25 22:02:44 +00001089 Base->getSourceRange().getBegin(),
1090 DeclarationName());
Douglas Gregor2943aed2009-03-03 04:44:36 +00001091 if (BaseType.isNull()) {
1092 Invalid = true;
1093 continue;
1094 }
1095
1096 if (CXXBaseSpecifier *InstantiatedBase
Douglas Gregord475b8d2009-03-25 21:17:03 +00001097 = CheckBaseSpecifier(Instantiation,
Douglas Gregor2943aed2009-03-03 04:44:36 +00001098 Base->getSourceRange(),
1099 Base->isVirtual(),
1100 Base->getAccessSpecifierAsWritten(),
1101 BaseType,
1102 /*FIXME: Not totally accurate */
1103 Base->getSourceRange().getBegin()))
1104 InstantiatedBases.push_back(InstantiatedBase);
1105 else
1106 Invalid = true;
1107 }
1108
Douglas Gregor27b152f2009-03-10 18:52:44 +00001109 if (!Invalid &&
Jay Foadbeaaccd2009-05-21 09:52:38 +00001110 AttachBaseSpecifiers(Instantiation, InstantiatedBases.data(),
Douglas Gregor2943aed2009-03-03 04:44:36 +00001111 InstantiatedBases.size()))
1112 Invalid = true;
1113
1114 return Invalid;
1115}
1116
Douglas Gregord475b8d2009-03-25 21:17:03 +00001117/// \brief Instantiate the definition of a class from a given pattern.
1118///
1119/// \param PointOfInstantiation The point of instantiation within the
1120/// source code.
1121///
1122/// \param Instantiation is the declaration whose definition is being
1123/// instantiated. This will be either a class template specialization
1124/// or a member class of a class template specialization.
1125///
1126/// \param Pattern is the pattern from which the instantiation
1127/// occurs. This will be either the declaration of a class template or
1128/// the declaration of a member class of a class template.
1129///
1130/// \param TemplateArgs The template arguments to be substituted into
1131/// the pattern.
1132///
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001133/// \param TSK the kind of implicit or explicit instantiation to perform.
Douglas Gregor5842ba92009-08-24 15:23:48 +00001134///
1135/// \param Complain whether to complain if the class cannot be instantiated due
1136/// to the lack of a definition.
1137///
Douglas Gregord475b8d2009-03-25 21:17:03 +00001138/// \returns true if an error occurred, false otherwise.
1139bool
1140Sema::InstantiateClass(SourceLocation PointOfInstantiation,
1141 CXXRecordDecl *Instantiation, CXXRecordDecl *Pattern,
Douglas Gregord6350ae2009-08-28 20:31:08 +00001142 const MultiLevelTemplateArgumentList &TemplateArgs,
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001143 TemplateSpecializationKind TSK,
Douglas Gregor5842ba92009-08-24 15:23:48 +00001144 bool Complain) {
Douglas Gregord475b8d2009-03-25 21:17:03 +00001145 bool Invalid = false;
John McCalle29ba202009-08-20 01:44:21 +00001146
Mike Stump1eb44332009-09-09 15:08:12 +00001147 CXXRecordDecl *PatternDef
Douglas Gregor952b0172010-02-11 01:04:33 +00001148 = cast_or_null<CXXRecordDecl>(Pattern->getDefinition());
Douglas Gregord475b8d2009-03-25 21:17:03 +00001149 if (!PatternDef) {
Douglas Gregor5842ba92009-08-24 15:23:48 +00001150 if (!Complain) {
1151 // Say nothing
1152 } else if (Pattern == Instantiation->getInstantiatedFromMemberClass()) {
Douglas Gregord475b8d2009-03-25 21:17:03 +00001153 Diag(PointOfInstantiation,
1154 diag::err_implicit_instantiate_member_undefined)
1155 << Context.getTypeDeclType(Instantiation);
1156 Diag(Pattern->getLocation(), diag::note_member_of_template_here);
1157 } else {
Douglas Gregor93dfdb12009-05-13 00:25:59 +00001158 Diag(PointOfInstantiation, diag::err_template_instantiate_undefined)
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001159 << (TSK != TSK_ImplicitInstantiation)
Douglas Gregord475b8d2009-03-25 21:17:03 +00001160 << Context.getTypeDeclType(Instantiation);
1161 Diag(Pattern->getLocation(), diag::note_template_decl_here);
1162 }
1163 return true;
1164 }
1165 Pattern = PatternDef;
1166
Douglas Gregor454885e2009-10-15 15:54:05 +00001167 // \brief Record the point of instantiation.
1168 if (MemberSpecializationInfo *MSInfo
1169 = Instantiation->getMemberSpecializationInfo()) {
1170 MSInfo->setTemplateSpecializationKind(TSK);
1171 MSInfo->setPointOfInstantiation(PointOfInstantiation);
Douglas Gregor972e6ce2009-10-27 06:26:26 +00001172 } else if (ClassTemplateSpecializationDecl *Spec
1173 = dyn_cast<ClassTemplateSpecializationDecl>(Instantiation)) {
1174 Spec->setTemplateSpecializationKind(TSK);
1175 Spec->setPointOfInstantiation(PointOfInstantiation);
Douglas Gregor454885e2009-10-15 15:54:05 +00001176 }
1177
Douglas Gregord048bb72009-03-25 21:23:52 +00001178 InstantiatingTemplate Inst(*this, PointOfInstantiation, Instantiation);
Douglas Gregord475b8d2009-03-25 21:17:03 +00001179 if (Inst)
1180 return true;
1181
1182 // Enter the scope of this instantiation. We don't use
1183 // PushDeclContext because we don't have a scope.
John McCallf5813822010-04-29 00:35:03 +00001184 ContextRAII SavedContext(*this, Instantiation);
Douglas Gregor9679caf2010-05-12 17:27:19 +00001185 EnterExpressionEvaluationContext EvalContext(*this,
1186 Action::PotentiallyEvaluated);
Douglas Gregord475b8d2009-03-25 21:17:03 +00001187
Douglas Gregor05030bb2010-03-24 01:33:17 +00001188 // If this is an instantiation of a local class, merge this local
1189 // instantiation scope with the enclosing scope. Otherwise, every
1190 // instantiation of a class has its own local instantiation scope.
1191 bool MergeWithParentScope = !Instantiation->isDefinedOutsideFunctionOrMethod();
1192 Sema::LocalInstantiationScope Scope(*this, MergeWithParentScope);
1193
Douglas Gregord475b8d2009-03-25 21:17:03 +00001194 // Start the definition of this instantiation.
1195 Instantiation->startDefinition();
Douglas Gregor13c85772010-05-06 00:28:52 +00001196
1197 Instantiation->setTagKind(Pattern->getTagKind());
Douglas Gregord475b8d2009-03-25 21:17:03 +00001198
John McCallce3ff2b2009-08-25 22:02:44 +00001199 // Do substitution on the base class specifiers.
1200 if (SubstBaseSpecifiers(Instantiation, Pattern, TemplateArgs))
Douglas Gregord475b8d2009-03-25 21:17:03 +00001201 Invalid = true;
1202
Douglas Gregor0ca20ac2009-05-29 18:27:38 +00001203 llvm::SmallVector<DeclPtrTy, 4> Fields;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001204 for (RecordDecl::decl_iterator Member = Pattern->decls_begin(),
Mike Stump1eb44332009-09-09 15:08:12 +00001205 MemberEnd = Pattern->decls_end();
Douglas Gregor6ab35242009-04-09 21:40:53 +00001206 Member != MemberEnd; ++Member) {
John McCallce3ff2b2009-08-25 22:02:44 +00001207 Decl *NewMember = SubstDecl(*Member, Instantiation, TemplateArgs);
Douglas Gregord475b8d2009-03-25 21:17:03 +00001208 if (NewMember) {
Eli Friedman721e77d2009-12-07 00:22:08 +00001209 if (FieldDecl *Field = dyn_cast<FieldDecl>(NewMember))
Chris Lattnerb28317a2009-03-28 19:18:32 +00001210 Fields.push_back(DeclPtrTy::make(Field));
Eli Friedman721e77d2009-12-07 00:22:08 +00001211 else if (NewMember->isInvalidDecl())
1212 Invalid = true;
Douglas Gregord475b8d2009-03-25 21:17:03 +00001213 } else {
1214 // FIXME: Eventually, a NULL return will mean that one of the
Mike Stump390b4cc2009-05-16 07:39:55 +00001215 // instantiations was a semantic disaster, and we'll want to set Invalid =
1216 // true. For now, we expect to skip some members that we can't yet handle.
Douglas Gregord475b8d2009-03-25 21:17:03 +00001217 }
1218 }
1219
1220 // Finish checking fields.
Chris Lattnerb28317a2009-03-28 19:18:32 +00001221 ActOnFields(0, Instantiation->getLocation(), DeclPtrTy::make(Instantiation),
Jay Foadbeaaccd2009-05-21 09:52:38 +00001222 Fields.data(), Fields.size(), SourceLocation(), SourceLocation(),
Douglas Gregord475b8d2009-03-25 21:17:03 +00001223 0);
Douglas Gregor6275e0c2010-04-12 17:09:20 +00001224 CheckCompletedCXXClass(/*Scope=*/0, Instantiation);
Douglas Gregor663b5a02009-10-14 20:14:33 +00001225 if (Instantiation->isInvalidDecl())
1226 Invalid = true;
1227
Douglas Gregord475b8d2009-03-25 21:17:03 +00001228 // Exit the scope of this instantiation.
John McCallf5813822010-04-29 00:35:03 +00001229 SavedContext.pop();
Douglas Gregord475b8d2009-03-25 21:17:03 +00001230
Douglas Gregor6fb745b2010-05-13 16:44:06 +00001231 if (!Invalid) {
Douglas Gregoraba43bb2009-05-26 20:50:29 +00001232 Consumer.HandleTagDeclDefinition(Instantiation);
1233
Douglas Gregor6fb745b2010-05-13 16:44:06 +00001234 // Always emit the vtable for an explicit instantiation definition
1235 // of a polymorphic class template specialization.
1236 if (TSK == TSK_ExplicitInstantiationDefinition)
1237 MarkVTableUsed(PointOfInstantiation, Instantiation, true);
1238 }
1239
Douglas Gregord475b8d2009-03-25 21:17:03 +00001240 return Invalid;
1241}
1242
Mike Stump1eb44332009-09-09 15:08:12 +00001243bool
Douglas Gregor2943aed2009-03-03 04:44:36 +00001244Sema::InstantiateClassTemplateSpecialization(
Douglas Gregor972e6ce2009-10-27 06:26:26 +00001245 SourceLocation PointOfInstantiation,
Douglas Gregor2943aed2009-03-03 04:44:36 +00001246 ClassTemplateSpecializationDecl *ClassTemplateSpec,
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001247 TemplateSpecializationKind TSK,
Douglas Gregor5842ba92009-08-24 15:23:48 +00001248 bool Complain) {
Douglas Gregor2943aed2009-03-03 04:44:36 +00001249 // Perform the actual instantiation on the canonical declaration.
1250 ClassTemplateSpec = cast<ClassTemplateSpecializationDecl>(
Argyrios Kyrtzidis97fbaa22009-07-18 00:34:25 +00001251 ClassTemplateSpec->getCanonicalDecl());
Douglas Gregor2943aed2009-03-03 04:44:36 +00001252
Douglas Gregor52604ab2009-09-11 21:19:12 +00001253 // Check whether we have already instantiated or specialized this class
1254 // template specialization.
1255 if (ClassTemplateSpec->getSpecializationKind() != TSK_Undeclared) {
1256 if (ClassTemplateSpec->getSpecializationKind() ==
1257 TSK_ExplicitInstantiationDeclaration &&
1258 TSK == TSK_ExplicitInstantiationDefinition) {
1259 // An explicit instantiation definition follows an explicit instantiation
1260 // declaration (C++0x [temp.explicit]p10); go ahead and perform the
1261 // explicit instantiation.
1262 ClassTemplateSpec->setSpecializationKind(TSK);
Douglas Gregor6fb745b2010-05-13 16:44:06 +00001263
1264 // If this is an explicit instantiation definition, mark the
1265 // vtable as used.
1266 if (TSK == TSK_ExplicitInstantiationDefinition)
1267 MarkVTableUsed(PointOfInstantiation, ClassTemplateSpec, true);
1268
Douglas Gregor52604ab2009-09-11 21:19:12 +00001269 return false;
1270 }
1271
1272 // We can only instantiate something that hasn't already been
1273 // instantiated or specialized. Fail without any diagnostics: our
1274 // caller will provide an error message.
Douglas Gregor2943aed2009-03-03 04:44:36 +00001275 return true;
Douglas Gregor52604ab2009-09-11 21:19:12 +00001276 }
Douglas Gregor2943aed2009-03-03 04:44:36 +00001277
Douglas Gregor9eea08b2009-09-15 16:51:42 +00001278 if (ClassTemplateSpec->isInvalidDecl())
1279 return true;
1280
Douglas Gregor2943aed2009-03-03 04:44:36 +00001281 ClassTemplateDecl *Template = ClassTemplateSpec->getSpecializedTemplate();
Douglas Gregord6350ae2009-08-28 20:31:08 +00001282 CXXRecordDecl *Pattern = 0;
Douglas Gregorc8ab2562009-05-31 09:31:02 +00001283
Douglas Gregorc1efb3f2009-06-12 22:31:52 +00001284 // C++ [temp.class.spec.match]p1:
1285 // When a class template is used in a context that requires an
1286 // instantiation of the class, it is necessary to determine
1287 // whether the instantiation is to be generated using the primary
1288 // template or one of the partial specializations. This is done by
1289 // matching the template arguments of the class template
1290 // specialization with the template argument lists of the partial
1291 // specializations.
Douglas Gregor199d9912009-06-05 00:53:49 +00001292 typedef std::pair<ClassTemplatePartialSpecializationDecl *,
1293 TemplateArgumentList *> MatchResult;
1294 llvm::SmallVector<MatchResult, 4> Matched;
Douglas Gregordc60c1e2010-04-30 05:56:50 +00001295 llvm::SmallVector<ClassTemplatePartialSpecializationDecl *, 4> PartialSpecs;
1296 Template->getPartialSpecializations(PartialSpecs);
1297 for (unsigned I = 0, N = PartialSpecs.size(); I != N; ++I) {
1298 ClassTemplatePartialSpecializationDecl *Partial = PartialSpecs[I];
John McCall5769d612010-02-08 23:07:23 +00001299 TemplateDeductionInfo Info(Context, PointOfInstantiation);
Douglas Gregorf67875d2009-06-12 18:26:56 +00001300 if (TemplateDeductionResult Result
Douglas Gregordc60c1e2010-04-30 05:56:50 +00001301 = DeduceTemplateArguments(Partial,
Douglas Gregorf67875d2009-06-12 18:26:56 +00001302 ClassTemplateSpec->getTemplateArgs(),
1303 Info)) {
1304 // FIXME: Store the failed-deduction information for use in
1305 // diagnostics, later.
1306 (void)Result;
1307 } else {
Douglas Gregordc60c1e2010-04-30 05:56:50 +00001308 Matched.push_back(std::make_pair(Partial, Info.take()));
Douglas Gregorf67875d2009-06-12 18:26:56 +00001309 }
Douglas Gregorc8ab2562009-05-31 09:31:02 +00001310 }
1311
Douglas Gregored9c0f92009-10-29 00:04:11 +00001312 if (Matched.size() >= 1) {
Douglas Gregorbf4ea562009-09-15 16:23:51 +00001313 llvm::SmallVector<MatchResult, 4>::iterator Best = Matched.begin();
Douglas Gregored9c0f92009-10-29 00:04:11 +00001314 if (Matched.size() == 1) {
1315 // -- If exactly one matching specialization is found, the
1316 // instantiation is generated from that specialization.
1317 // We don't need to do anything for this.
1318 } else {
1319 // -- If more than one matching specialization is found, the
1320 // partial order rules (14.5.4.2) are used to determine
1321 // whether one of the specializations is more specialized
1322 // than the others. If none of the specializations is more
1323 // specialized than all of the other matching
1324 // specializations, then the use of the class template is
1325 // ambiguous and the program is ill-formed.
1326 for (llvm::SmallVector<MatchResult, 4>::iterator P = Best + 1,
1327 PEnd = Matched.end();
1328 P != PEnd; ++P) {
John McCall5769d612010-02-08 23:07:23 +00001329 if (getMoreSpecializedPartialSpecialization(P->first, Best->first,
1330 PointOfInstantiation)
Douglas Gregored9c0f92009-10-29 00:04:11 +00001331 == P->first)
1332 Best = P;
Douglas Gregorbf4ea562009-09-15 16:23:51 +00001333 }
Douglas Gregorbf4ea562009-09-15 16:23:51 +00001334
Douglas Gregored9c0f92009-10-29 00:04:11 +00001335 // Determine if the best partial specialization is more specialized than
1336 // the others.
1337 bool Ambiguous = false;
Douglas Gregorbf4ea562009-09-15 16:23:51 +00001338 for (llvm::SmallVector<MatchResult, 4>::iterator P = Matched.begin(),
1339 PEnd = Matched.end();
Douglas Gregored9c0f92009-10-29 00:04:11 +00001340 P != PEnd; ++P) {
1341 if (P != Best &&
John McCall5769d612010-02-08 23:07:23 +00001342 getMoreSpecializedPartialSpecialization(P->first, Best->first,
1343 PointOfInstantiation)
Douglas Gregored9c0f92009-10-29 00:04:11 +00001344 != Best->first) {
1345 Ambiguous = true;
1346 break;
1347 }
1348 }
1349
1350 if (Ambiguous) {
1351 // Partial ordering did not produce a clear winner. Complain.
1352 ClassTemplateSpec->setInvalidDecl();
1353 Diag(PointOfInstantiation, diag::err_partial_spec_ordering_ambiguous)
1354 << ClassTemplateSpec;
1355
1356 // Print the matching partial specializations.
1357 for (llvm::SmallVector<MatchResult, 4>::iterator P = Matched.begin(),
1358 PEnd = Matched.end();
1359 P != PEnd; ++P)
1360 Diag(P->first->getLocation(), diag::note_partial_spec_match)
1361 << getTemplateArgumentBindingsText(P->first->getTemplateParameters(),
1362 *P->second);
Douglas Gregord6350ae2009-08-28 20:31:08 +00001363
Douglas Gregored9c0f92009-10-29 00:04:11 +00001364 return true;
1365 }
Douglas Gregorbf4ea562009-09-15 16:23:51 +00001366 }
1367
1368 // Instantiate using the best class template partial specialization.
Douglas Gregored9c0f92009-10-29 00:04:11 +00001369 ClassTemplatePartialSpecializationDecl *OrigPartialSpec = Best->first;
1370 while (OrigPartialSpec->getInstantiatedFromMember()) {
1371 // If we've found an explicit specialization of this class template,
1372 // stop here and use that as the pattern.
1373 if (OrigPartialSpec->isMemberSpecialization())
1374 break;
1375
1376 OrigPartialSpec = OrigPartialSpec->getInstantiatedFromMember();
1377 }
1378
1379 Pattern = OrigPartialSpec;
Douglas Gregorbf4ea562009-09-15 16:23:51 +00001380 ClassTemplateSpec->setInstantiationOf(Best->first, Best->second);
Douglas Gregorc1efb3f2009-06-12 22:31:52 +00001381 } else {
1382 // -- If no matches are found, the instantiation is generated
1383 // from the primary template.
Douglas Gregord6350ae2009-08-28 20:31:08 +00001384 ClassTemplateDecl *OrigTemplate = Template;
Douglas Gregorfd056bc2009-10-13 16:30:37 +00001385 while (OrigTemplate->getInstantiatedFromMemberTemplate()) {
1386 // If we've found an explicit specialization of this class template,
1387 // stop here and use that as the pattern.
1388 if (OrigTemplate->isMemberSpecialization())
1389 break;
1390
Douglas Gregord6350ae2009-08-28 20:31:08 +00001391 OrigTemplate = OrigTemplate->getInstantiatedFromMemberTemplate();
Douglas Gregorfd056bc2009-10-13 16:30:37 +00001392 }
1393
Douglas Gregord6350ae2009-08-28 20:31:08 +00001394 Pattern = OrigTemplate->getTemplatedDecl();
Douglas Gregorc8ab2562009-05-31 09:31:02 +00001395 }
Douglas Gregor2943aed2009-03-03 04:44:36 +00001396
Douglas Gregor972e6ce2009-10-27 06:26:26 +00001397 bool Result = InstantiateClass(PointOfInstantiation, ClassTemplateSpec,
1398 Pattern,
1399 getTemplateInstantiationArgs(ClassTemplateSpec),
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001400 TSK,
Douglas Gregor5842ba92009-08-24 15:23:48 +00001401 Complain);
Mike Stump1eb44332009-09-09 15:08:12 +00001402
Douglas Gregor199d9912009-06-05 00:53:49 +00001403 for (unsigned I = 0, N = Matched.size(); I != N; ++I) {
1404 // FIXME: Implement TemplateArgumentList::Destroy!
1405 // if (Matched[I].first != Pattern)
1406 // Matched[I].second->Destroy(Context);
1407 }
Mike Stump1eb44332009-09-09 15:08:12 +00001408
Douglas Gregor199d9912009-06-05 00:53:49 +00001409 return Result;
Douglas Gregor2943aed2009-03-03 04:44:36 +00001410}
Douglas Gregor5953d8b2009-03-19 17:26:29 +00001411
John McCallce3ff2b2009-08-25 22:02:44 +00001412/// \brief Instantiates the definitions of all of the member
1413/// of the given class, which is an instantiation of a class template
1414/// or a member class of a template.
Douglas Gregora58861f2009-05-13 20:28:22 +00001415void
1416Sema::InstantiateClassMembers(SourceLocation PointOfInstantiation,
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001417 CXXRecordDecl *Instantiation,
1418 const MultiLevelTemplateArgumentList &TemplateArgs,
1419 TemplateSpecializationKind TSK) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001420 for (DeclContext::decl_iterator D = Instantiation->decls_begin(),
1421 DEnd = Instantiation->decls_end();
Douglas Gregora58861f2009-05-13 20:28:22 +00001422 D != DEnd; ++D) {
Douglas Gregor0d035142009-10-27 18:42:08 +00001423 bool SuppressNew = false;
Douglas Gregora58861f2009-05-13 20:28:22 +00001424 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(*D)) {
Douglas Gregor0d035142009-10-27 18:42:08 +00001425 if (FunctionDecl *Pattern
1426 = Function->getInstantiatedFromMemberFunction()) {
1427 MemberSpecializationInfo *MSInfo
1428 = Function->getMemberSpecializationInfo();
1429 assert(MSInfo && "No member specialization information?");
Douglas Gregorc42b6522010-04-09 21:02:29 +00001430 if (MSInfo->getTemplateSpecializationKind()
1431 == TSK_ExplicitSpecialization)
1432 continue;
1433
Douglas Gregor0d035142009-10-27 18:42:08 +00001434 if (CheckSpecializationInstantiationRedecl(PointOfInstantiation, TSK,
1435 Function,
1436 MSInfo->getTemplateSpecializationKind(),
1437 MSInfo->getPointOfInstantiation(),
1438 SuppressNew) ||
1439 SuppressNew)
Douglas Gregorf6b11852009-10-08 15:14:33 +00001440 continue;
1441
Douglas Gregor0d035142009-10-27 18:42:08 +00001442 if (Function->getBody())
1443 continue;
1444
1445 if (TSK == TSK_ExplicitInstantiationDefinition) {
1446 // C++0x [temp.explicit]p8:
1447 // An explicit instantiation definition that names a class template
1448 // specialization explicitly instantiates the class template
1449 // specialization and is only an explicit instantiation definition
1450 // of members whose definition is visible at the point of
1451 // instantiation.
1452 if (!Pattern->getBody())
1453 continue;
1454
1455 Function->setTemplateSpecializationKind(TSK, PointOfInstantiation);
1456
1457 InstantiateFunctionDefinition(PointOfInstantiation, Function);
1458 } else {
1459 Function->setTemplateSpecializationKind(TSK, PointOfInstantiation);
1460 }
Douglas Gregorf6b11852009-10-08 15:14:33 +00001461 }
Douglas Gregora58861f2009-05-13 20:28:22 +00001462 } else if (VarDecl *Var = dyn_cast<VarDecl>(*D)) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001463 if (Var->isStaticDataMember()) {
Douglas Gregor0d035142009-10-27 18:42:08 +00001464 MemberSpecializationInfo *MSInfo = Var->getMemberSpecializationInfo();
1465 assert(MSInfo && "No member specialization information?");
Douglas Gregorc42b6522010-04-09 21:02:29 +00001466 if (MSInfo->getTemplateSpecializationKind()
1467 == TSK_ExplicitSpecialization)
1468 continue;
1469
Douglas Gregor0d035142009-10-27 18:42:08 +00001470 if (CheckSpecializationInstantiationRedecl(PointOfInstantiation, TSK,
1471 Var,
1472 MSInfo->getTemplateSpecializationKind(),
1473 MSInfo->getPointOfInstantiation(),
1474 SuppressNew) ||
1475 SuppressNew)
Douglas Gregorf6b11852009-10-08 15:14:33 +00001476 continue;
1477
Douglas Gregor0d035142009-10-27 18:42:08 +00001478 if (TSK == TSK_ExplicitInstantiationDefinition) {
1479 // C++0x [temp.explicit]p8:
1480 // An explicit instantiation definition that names a class template
1481 // specialization explicitly instantiates the class template
1482 // specialization and is only an explicit instantiation definition
1483 // of members whose definition is visible at the point of
1484 // instantiation.
1485 if (!Var->getInstantiatedFromStaticDataMember()
1486 ->getOutOfLineDefinition())
1487 continue;
1488
1489 Var->setTemplateSpecializationKind(TSK, PointOfInstantiation);
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001490 InstantiateStaticDataMemberDefinition(PointOfInstantiation, Var);
Douglas Gregor0d035142009-10-27 18:42:08 +00001491 } else {
1492 Var->setTemplateSpecializationKind(TSK, PointOfInstantiation);
1493 }
1494 }
Douglas Gregora58861f2009-05-13 20:28:22 +00001495 } else if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(*D)) {
Douglas Gregora77eaa92010-04-18 18:11:38 +00001496 // Always skip the injected-class-name, along with any
1497 // redeclarations of nested classes, since both would cause us
1498 // to try to instantiate the members of a class twice.
1499 if (Record->isInjectedClassName() || Record->getPreviousDeclaration())
Douglas Gregor2db32322009-10-07 23:56:10 +00001500 continue;
1501
Douglas Gregor0d035142009-10-27 18:42:08 +00001502 MemberSpecializationInfo *MSInfo = Record->getMemberSpecializationInfo();
1503 assert(MSInfo && "No member specialization information?");
Douglas Gregorc42b6522010-04-09 21:02:29 +00001504
1505 if (MSInfo->getTemplateSpecializationKind()
1506 == TSK_ExplicitSpecialization)
1507 continue;
1508
Douglas Gregor0d035142009-10-27 18:42:08 +00001509 if (CheckSpecializationInstantiationRedecl(PointOfInstantiation, TSK,
1510 Record,
1511 MSInfo->getTemplateSpecializationKind(),
1512 MSInfo->getPointOfInstantiation(),
1513 SuppressNew) ||
1514 SuppressNew)
Douglas Gregorf6b11852009-10-08 15:14:33 +00001515 continue;
1516
Douglas Gregor0d035142009-10-27 18:42:08 +00001517 CXXRecordDecl *Pattern = Record->getInstantiatedFromMemberClass();
1518 assert(Pattern && "Missing instantiated-from-template information");
1519
Douglas Gregor952b0172010-02-11 01:04:33 +00001520 if (!Record->getDefinition()) {
1521 if (!Pattern->getDefinition()) {
Douglas Gregor0d035142009-10-27 18:42:08 +00001522 // C++0x [temp.explicit]p8:
1523 // An explicit instantiation definition that names a class template
1524 // specialization explicitly instantiates the class template
1525 // specialization and is only an explicit instantiation definition
1526 // of members whose definition is visible at the point of
1527 // instantiation.
1528 if (TSK == TSK_ExplicitInstantiationDeclaration) {
1529 MSInfo->setTemplateSpecializationKind(TSK);
1530 MSInfo->setPointOfInstantiation(PointOfInstantiation);
1531 }
1532
1533 continue;
1534 }
1535
1536 InstantiateClass(PointOfInstantiation, Record, Pattern,
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001537 TemplateArgs,
1538 TSK);
Douglas Gregor0d035142009-10-27 18:42:08 +00001539 }
Douglas Gregore9374d52009-10-08 01:19:17 +00001540
Douglas Gregor952b0172010-02-11 01:04:33 +00001541 Pattern = cast_or_null<CXXRecordDecl>(Record->getDefinition());
Douglas Gregor0d035142009-10-27 18:42:08 +00001542 if (Pattern)
1543 InstantiateClassMembers(PointOfInstantiation, Pattern, TemplateArgs,
1544 TSK);
Douglas Gregora58861f2009-05-13 20:28:22 +00001545 }
1546 }
1547}
1548
1549/// \brief Instantiate the definitions of all of the members of the
1550/// given class template specialization, which was named as part of an
1551/// explicit instantiation.
Mike Stump1eb44332009-09-09 15:08:12 +00001552void
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001553Sema::InstantiateClassTemplateSpecializationMembers(
Douglas Gregora58861f2009-05-13 20:28:22 +00001554 SourceLocation PointOfInstantiation,
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001555 ClassTemplateSpecializationDecl *ClassTemplateSpec,
1556 TemplateSpecializationKind TSK) {
Douglas Gregora58861f2009-05-13 20:28:22 +00001557 // C++0x [temp.explicit]p7:
1558 // An explicit instantiation that names a class template
1559 // specialization is an explicit instantion of the same kind
1560 // (declaration or definition) of each of its members (not
1561 // including members inherited from base classes) that has not
1562 // been previously explicitly specialized in the translation unit
1563 // containing the explicit instantiation, except as described
1564 // below.
1565 InstantiateClassMembers(PointOfInstantiation, ClassTemplateSpec,
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001566 getTemplateInstantiationArgs(ClassTemplateSpec),
1567 TSK);
Douglas Gregora58861f2009-05-13 20:28:22 +00001568}
1569
Mike Stump1eb44332009-09-09 15:08:12 +00001570Sema::OwningStmtResult
Douglas Gregord6350ae2009-08-28 20:31:08 +00001571Sema::SubstStmt(Stmt *S, const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor43959a92009-08-20 07:17:43 +00001572 if (!S)
1573 return Owned(S);
1574
1575 TemplateInstantiator Instantiator(*this, TemplateArgs,
1576 SourceLocation(),
1577 DeclarationName());
1578 return Instantiator.TransformStmt(S);
1579}
1580
Mike Stump1eb44332009-09-09 15:08:12 +00001581Sema::OwningExprResult
Douglas Gregord6350ae2009-08-28 20:31:08 +00001582Sema::SubstExpr(Expr *E, const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregorb98b1992009-08-11 05:31:07 +00001583 if (!E)
1584 return Owned(E);
Mike Stump1eb44332009-09-09 15:08:12 +00001585
Douglas Gregorb98b1992009-08-11 05:31:07 +00001586 TemplateInstantiator Instantiator(*this, TemplateArgs,
1587 SourceLocation(),
1588 DeclarationName());
1589 return Instantiator.TransformExpr(E);
1590}
1591
John McCallce3ff2b2009-08-25 22:02:44 +00001592/// \brief Do template substitution on a nested-name-specifier.
Douglas Gregorab452ba2009-03-26 23:50:42 +00001593NestedNameSpecifier *
John McCallce3ff2b2009-08-25 22:02:44 +00001594Sema::SubstNestedNameSpecifier(NestedNameSpecifier *NNS,
Douglas Gregord6350ae2009-08-28 20:31:08 +00001595 SourceRange Range,
1596 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregordcee1a12009-08-06 05:28:30 +00001597 TemplateInstantiator Instantiator(*this, TemplateArgs, Range.getBegin(),
1598 DeclarationName());
Douglas Gregoredc90502010-02-25 04:46:04 +00001599 return Instantiator.TransformNestedNameSpecifier(NNS, Range);
Douglas Gregor5953d8b2009-03-19 17:26:29 +00001600}
Douglas Gregorde650ae2009-03-31 18:38:02 +00001601
1602TemplateName
John McCallce3ff2b2009-08-25 22:02:44 +00001603Sema::SubstTemplateName(TemplateName Name, SourceLocation Loc,
Douglas Gregord6350ae2009-08-28 20:31:08 +00001604 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregord1067e52009-08-06 06:41:21 +00001605 TemplateInstantiator Instantiator(*this, TemplateArgs, Loc,
1606 DeclarationName());
1607 return Instantiator.TransformTemplateName(Name);
Douglas Gregorde650ae2009-03-31 18:38:02 +00001608}
Douglas Gregor91333002009-06-11 00:06:24 +00001609
John McCall833ca992009-10-29 08:12:44 +00001610bool Sema::Subst(const TemplateArgumentLoc &Input, TemplateArgumentLoc &Output,
1611 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor670444e2009-08-04 22:27:00 +00001612 TemplateInstantiator Instantiator(*this, TemplateArgs, SourceLocation(),
1613 DeclarationName());
John McCall833ca992009-10-29 08:12:44 +00001614
1615 return Instantiator.TransformTemplateArgument(Input, Output);
Douglas Gregor91333002009-06-11 00:06:24 +00001616}
Douglas Gregor895162d2010-04-30 18:55:50 +00001617
1618Decl *Sema::LocalInstantiationScope::getInstantiationOf(const Decl *D) {
1619 for (LocalInstantiationScope *Current = this; Current;
1620 Current = Current->Outer) {
1621 // Check if we found something within this scope.
1622 llvm::DenseMap<const Decl *, Decl *>::iterator Found
1623 = Current->LocalDecls.find(D);
1624 if (Found != Current->LocalDecls.end())
1625 return Found->second;
1626
1627 // If we aren't combined with our outer scope, we're done.
1628 if (!Current->CombineWithOuterScope)
1629 break;
1630 }
1631
1632 assert(D->isInvalidDecl() &&
1633 "declaration was not instantiated in this scope!");
1634 return 0;
1635}
1636
1637void Sema::LocalInstantiationScope::InstantiatedLocal(const Decl *D,
1638 Decl *Inst) {
1639 Decl *&Stored = LocalDecls[D];
1640 assert((!Stored || Stored == Inst)&& "Already instantiated this local");
1641 Stored = Inst;
1642}