blob: aff19ec597e2c0653292a7b8251828bed43da6dc [file] [log] [blame]
Douglas Gregorfe1e1102009-02-27 19:31:52 +00001//===------- SemaTemplateInstantiate.cpp - C++ Template Instantiation ------===/
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//===----------------------------------------------------------------------===/
8//
9// This file implements C++ template instantiation.
10//
11//===----------------------------------------------------------------------===/
12
John McCall83024632010-08-25 22:03:47 +000013#include "clang/Sema/SemaInternal.h"
Douglas Gregord6ff3322009-08-04 16:50:30 +000014#include "TreeTransform.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000015#include "clang/AST/ASTConsumer.h"
16#include "clang/AST/ASTContext.h"
Faisal Vali2cba1332013-10-23 06:44:28 +000017#include "clang/AST/ASTLambda.h"
Richard Smith4b054b22016-08-24 21:25:37 +000018#include "clang/AST/ASTMutationListener.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000019#include "clang/AST/DeclTemplate.h"
20#include "clang/AST/Expr.h"
21#include "clang/Basic/LangOptions.h"
John McCall8b0666c2010-08-20 18:27:03 +000022#include "clang/Sema/DeclSpec.h"
Richard Smith938f40b2011-06-11 17:19:42 +000023#include "clang/Sema/Initialization.h"
Douglas Gregorc3a6ade2010-08-12 20:07:10 +000024#include "clang/Sema/Lookup.h"
Richard Smithe19b95d2016-05-26 20:23:13 +000025#include "clang/Sema/PrettyDeclStackTrace.h"
John McCallde6836a2010-08-24 07:21:54 +000026#include "clang/Sema/Template.h"
John McCall19c1bfd2010-08-25 05:32:35 +000027#include "clang/Sema/TemplateDeduction.h"
Douglas Gregorfe1e1102009-02-27 19:31:52 +000028
29using namespace clang;
John McCall19c1bfd2010-08-25 05:32:35 +000030using namespace sema;
Douglas Gregorfe1e1102009-02-27 19:31:52 +000031
Douglas Gregor4ea568f2009-03-10 18:03:33 +000032//===----------------------------------------------------------------------===/
33// Template Instantiation Support
34//===----------------------------------------------------------------------===/
35
Douglas Gregor01afeef2009-08-28 20:31:08 +000036/// \brief Retrieve the template argument list(s) that should be used to
37/// instantiate the definition of the given declaration.
Douglas Gregor36d7c5f2009-11-09 19:17:50 +000038///
39/// \param D the declaration for which we are computing template instantiation
40/// arguments.
41///
42/// \param Innermost if non-NULL, the innermost template argument list.
Douglas Gregor8c702532010-02-05 07:33:43 +000043///
44/// \param RelativeToPrimary true if we should get the template
45/// arguments relative to the primary template, even when we're
46/// dealing with a specialization. This is only relevant for function
47/// template specializations.
Douglas Gregor1bd7a942010-05-03 23:29:10 +000048///
49/// \param Pattern If non-NULL, indicates the pattern from which we will be
50/// instantiating the definition of the given declaration, \p D. This is
51/// used to determine the proper set of template instantiation arguments for
52/// friend function template specializations.
Douglas Gregora654dd82009-08-28 17:37:35 +000053MultiLevelTemplateArgumentList
Douglas Gregor36d7c5f2009-11-09 19:17:50 +000054Sema::getTemplateInstantiationArgs(NamedDecl *D,
Douglas Gregor8c702532010-02-05 07:33:43 +000055 const TemplateArgumentList *Innermost,
Douglas Gregor1bd7a942010-05-03 23:29:10 +000056 bool RelativeToPrimary,
57 const FunctionDecl *Pattern) {
Douglas Gregora654dd82009-08-28 17:37:35 +000058 // Accumulate the set of template argument lists in this structure.
59 MultiLevelTemplateArgumentList Result;
Mike Stump11289f42009-09-09 15:08:12 +000060
Douglas Gregor36d7c5f2009-11-09 19:17:50 +000061 if (Innermost)
62 Result.addOuterTemplateArguments(Innermost);
63
Douglas Gregora654dd82009-08-28 17:37:35 +000064 DeclContext *Ctx = dyn_cast<DeclContext>(D);
Douglas Gregora51c9cc2011-05-22 00:21:10 +000065 if (!Ctx) {
Douglas Gregora654dd82009-08-28 17:37:35 +000066 Ctx = D->getDeclContext();
Larisse Voufo39a1e502013-08-06 01:03:05 +000067
68 // Add template arguments from a variable template instantiation.
69 if (VarTemplateSpecializationDecl *Spec =
70 dyn_cast<VarTemplateSpecializationDecl>(D)) {
71 // We're done when we hit an explicit specialization.
72 if (Spec->getSpecializationKind() == TSK_ExplicitSpecialization &&
73 !isa<VarTemplatePartialSpecializationDecl>(Spec))
74 return Result;
75
76 Result.addOuterTemplateArguments(&Spec->getTemplateInstantiationArgs());
77
78 // If this variable template specialization was instantiated from a
79 // specialized member that is a variable template, we're done.
80 assert(Spec->getSpecializedTemplate() && "No variable template?");
Richard Smithbeef3452014-01-16 23:39:20 +000081 llvm::PointerUnion<VarTemplateDecl*,
82 VarTemplatePartialSpecializationDecl*> Specialized
83 = Spec->getSpecializedTemplateOrPartial();
84 if (VarTemplatePartialSpecializationDecl *Partial =
85 Specialized.dyn_cast<VarTemplatePartialSpecializationDecl *>()) {
86 if (Partial->isMemberSpecialization())
87 return Result;
88 } else {
89 VarTemplateDecl *Tmpl = Specialized.get<VarTemplateDecl *>();
90 if (Tmpl->isMemberSpecialization())
91 return Result;
92 }
Larisse Voufo39a1e502013-08-06 01:03:05 +000093 }
94
Douglas Gregor55462622011-06-15 14:20:42 +000095 // If we have a template template parameter with translation unit context,
96 // then we're performing substitution into a default template argument of
97 // this template template parameter before we've constructed the template
98 // that will own this template template parameter. In this case, we
99 // use empty template parameter lists for all of the outer templates
100 // to avoid performing any substitutions.
101 if (Ctx->isTranslationUnit()) {
102 if (TemplateTemplateParmDecl *TTP
103 = dyn_cast<TemplateTemplateParmDecl>(D)) {
104 for (unsigned I = 0, N = TTP->getDepth() + 1; I != N; ++I)
Richard Smith841d8b22013-05-17 03:04:50 +0000105 Result.addOuterTemplateArguments(None);
Douglas Gregor55462622011-06-15 14:20:42 +0000106 return Result;
107 }
108 }
Douglas Gregora51c9cc2011-05-22 00:21:10 +0000109 }
110
John McCall970d5302009-08-29 03:16:09 +0000111 while (!Ctx->isFileContext()) {
Douglas Gregora654dd82009-08-28 17:37:35 +0000112 // Add template arguments from a class template instantiation.
Mike Stump11289f42009-09-09 15:08:12 +0000113 if (ClassTemplateSpecializationDecl *Spec
Douglas Gregora654dd82009-08-28 17:37:35 +0000114 = dyn_cast<ClassTemplateSpecializationDecl>(Ctx)) {
115 // We're done when we hit an explicit specialization.
Douglas Gregor9961ce92010-07-08 18:37:38 +0000116 if (Spec->getSpecializationKind() == TSK_ExplicitSpecialization &&
117 !isa<ClassTemplatePartialSpecializationDecl>(Spec))
Douglas Gregora654dd82009-08-28 17:37:35 +0000118 break;
Mike Stump11289f42009-09-09 15:08:12 +0000119
Douglas Gregora654dd82009-08-28 17:37:35 +0000120 Result.addOuterTemplateArguments(&Spec->getTemplateInstantiationArgs());
Douglas Gregorcf915552009-10-13 16:30:37 +0000121
122 // If this class template specialization was instantiated from a
123 // specialized member that is a class template, we're done.
124 assert(Spec->getSpecializedTemplate() && "No class template?");
125 if (Spec->getSpecializedTemplate()->isMemberSpecialization())
126 break;
Mike Stump11289f42009-09-09 15:08:12 +0000127 }
Douglas Gregora654dd82009-08-28 17:37:35 +0000128 // Add template arguments from a function template specialization.
John McCall970d5302009-08-29 03:16:09 +0000129 else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Ctx)) {
Douglas Gregor8c702532010-02-05 07:33:43 +0000130 if (!RelativeToPrimary &&
Francois Pichet00c7e6c2011-08-14 03:52:19 +0000131 (Function->getTemplateSpecializationKind() ==
132 TSK_ExplicitSpecialization &&
133 !Function->getClassScopeSpecializationPattern()))
Douglas Gregorcf915552009-10-13 16:30:37 +0000134 break;
135
Douglas Gregora654dd82009-08-28 17:37:35 +0000136 if (const TemplateArgumentList *TemplateArgs
Douglas Gregorcf915552009-10-13 16:30:37 +0000137 = Function->getTemplateSpecializationArgs()) {
138 // Add the template arguments for this specialization.
Douglas Gregora654dd82009-08-28 17:37:35 +0000139 Result.addOuterTemplateArguments(TemplateArgs);
John McCall970d5302009-08-29 03:16:09 +0000140
Douglas Gregorcf915552009-10-13 16:30:37 +0000141 // If this function was instantiated from a specialized member that is
142 // a function template, we're done.
143 assert(Function->getPrimaryTemplate() && "No function template?");
144 if (Function->getPrimaryTemplate()->isMemberSpecialization())
145 break;
Faisal Vali2cba1332013-10-23 06:44:28 +0000146
147 // If this function is a generic lambda specialization, we are done.
148 if (isGenericLambdaCallOperatorSpecialization(Function))
149 break;
150
Douglas Gregor43669f82011-03-05 17:54:25 +0000151 } else if (FunctionTemplateDecl *FunTmpl
152 = Function->getDescribedFunctionTemplate()) {
153 // Add the "injected" template arguments.
Richard Smith841d8b22013-05-17 03:04:50 +0000154 Result.addOuterTemplateArguments(FunTmpl->getInjectedTemplateArgs());
Douglas Gregorcf915552009-10-13 16:30:37 +0000155 }
156
John McCall970d5302009-08-29 03:16:09 +0000157 // If this is a friend declaration and it declares an entity at
158 // namespace scope, take arguments from its lexical parent
Douglas Gregor1bd7a942010-05-03 23:29:10 +0000159 // instead of its semantic parent, unless of course the pattern we're
160 // instantiating actually comes from the file's context!
John McCall970d5302009-08-29 03:16:09 +0000161 if (Function->getFriendObjectKind() &&
Douglas Gregor1bd7a942010-05-03 23:29:10 +0000162 Function->getDeclContext()->isFileContext() &&
163 (!Pattern || !Pattern->getLexicalDeclContext()->isFileContext())) {
John McCall970d5302009-08-29 03:16:09 +0000164 Ctx = Function->getLexicalDeclContext();
Douglas Gregor8c702532010-02-05 07:33:43 +0000165 RelativeToPrimary = false;
John McCall970d5302009-08-29 03:16:09 +0000166 continue;
167 }
Douglas Gregor9961ce92010-07-08 18:37:38 +0000168 } else if (CXXRecordDecl *Rec = dyn_cast<CXXRecordDecl>(Ctx)) {
169 if (ClassTemplateDecl *ClassTemplate = Rec->getDescribedClassTemplate()) {
170 QualType T = ClassTemplate->getInjectedClassNameSpecialization();
Richard Smith841d8b22013-05-17 03:04:50 +0000171 const TemplateSpecializationType *TST =
172 cast<TemplateSpecializationType>(Context.getCanonicalType(T));
173 Result.addOuterTemplateArguments(
174 llvm::makeArrayRef(TST->getArgs(), TST->getNumArgs()));
Douglas Gregor9961ce92010-07-08 18:37:38 +0000175 if (ClassTemplate->isMemberSpecialization())
176 break;
177 }
Douglas Gregora654dd82009-08-28 17:37:35 +0000178 }
John McCall970d5302009-08-29 03:16:09 +0000179
180 Ctx = Ctx->getParent();
Douglas Gregor8c702532010-02-05 07:33:43 +0000181 RelativeToPrimary = false;
Douglas Gregorb4850462009-05-14 23:26:13 +0000182 }
Mike Stump11289f42009-09-09 15:08:12 +0000183
Douglas Gregora654dd82009-08-28 17:37:35 +0000184 return Result;
Douglas Gregorb4850462009-05-14 23:26:13 +0000185}
186
Douglas Gregor84d49a22009-11-11 21:54:23 +0000187bool Sema::ActiveTemplateInstantiation::isInstantiationRecord() const {
188 switch (Kind) {
189 case TemplateInstantiation:
Richard Smithf623c962012-04-17 00:58:00 +0000190 case ExceptionSpecInstantiation:
Douglas Gregor84d49a22009-11-11 21:54:23 +0000191 case DefaultTemplateArgumentInstantiation:
192 case DefaultFunctionArgumentInstantiation:
Douglas Gregor84d49a22009-11-11 21:54:23 +0000193 case ExplicitTemplateArgumentSubstitution:
194 case DeducedTemplateArgumentSubstitution:
195 case PriorTemplateArgumentSubstitution:
Richard Smith8a874c92012-07-08 02:38:24 +0000196 return true;
197
Douglas Gregor84d49a22009-11-11 21:54:23 +0000198 case DefaultTemplateArgumentChecking:
199 return false;
200 }
David Blaikie8a40f702012-01-17 06:56:22 +0000201
202 llvm_unreachable("Invalid InstantiationKind!");
Douglas Gregor84d49a22009-11-11 21:54:23 +0000203}
204
Benjamin Kramer7761a042015-03-06 16:36:50 +0000205Sema::InstantiatingTemplate::InstantiatingTemplate(
206 Sema &SemaRef, ActiveTemplateInstantiation::InstantiationKind Kind,
Stephan Tolksdorf232670f2014-03-13 20:34:22 +0000207 SourceLocation PointOfInstantiation, SourceRange InstantiationRange,
208 Decl *Entity, NamedDecl *Template, ArrayRef<TemplateArgument> TemplateArgs,
Benjamin Kramer7761a042015-03-06 16:36:50 +0000209 sema::TemplateDeductionInfo *DeductionInfo)
210 : SemaRef(SemaRef), SavedInNonInstantiationSFINAEContext(
211 SemaRef.InNonInstantiationSFINAEContext) {
Akira Hatanaka43556c12016-11-03 15:04:58 +0000212 // Don't allow further instantiation if a fatal error and an uncompilable
Akira Hatanaka40c15ab2016-11-03 17:11:28 +0000213 // error have occurred. Any diagnostics we might have raised will not be
Akira Hatanaka43556c12016-11-03 15:04:58 +0000214 // visible, and we do not need to construct a correct AST.
215 if (SemaRef.Diags.hasFatalErrorOccurred() &&
216 SemaRef.Diags.hasUncompilableErrorOccurred()) {
David Majnemer8c969ea2015-01-30 05:01:23 +0000217 Invalid = true;
218 return;
219 }
Stephan Tolksdorf232670f2014-03-13 20:34:22 +0000220 Invalid = CheckInstantiationDepth(PointOfInstantiation, InstantiationRange);
221 if (!Invalid) {
222 ActiveTemplateInstantiation Inst;
223 Inst.Kind = Kind;
224 Inst.PointOfInstantiation = PointOfInstantiation;
225 Inst.Entity = Entity;
226 Inst.Template = Template;
227 Inst.TemplateArgs = TemplateArgs.data();
228 Inst.NumTemplateArgs = TemplateArgs.size();
229 Inst.DeductionInfo = DeductionInfo;
230 Inst.InstantiationRange = InstantiationRange;
Richard Smith54f18e82016-08-31 02:15:21 +0000231 AlreadyInstantiating =
232 !SemaRef.InstantiatingSpecializations
233 .insert(std::make_pair(Inst.Entity->getCanonicalDecl(), Inst.Kind))
234 .second;
Stephan Tolksdorf232670f2014-03-13 20:34:22 +0000235 SemaRef.InNonInstantiationSFINAEContext = false;
236 SemaRef.ActiveTemplateInstantiations.push_back(Inst);
237 if (!Inst.isInstantiationRecord())
238 ++SemaRef.NonInstantiationEntries;
239 }
240}
241
Benjamin Kramer7761a042015-03-06 16:36:50 +0000242Sema::InstantiatingTemplate::InstantiatingTemplate(
243 Sema &SemaRef, SourceLocation PointOfInstantiation, Decl *Entity,
244 SourceRange InstantiationRange)
245 : InstantiatingTemplate(SemaRef,
246 ActiveTemplateInstantiation::TemplateInstantiation,
247 PointOfInstantiation, InstantiationRange, Entity) {}
Douglas Gregor79cf6032009-03-10 20:44:00 +0000248
Benjamin Kramer7761a042015-03-06 16:36:50 +0000249Sema::InstantiatingTemplate::InstantiatingTemplate(
250 Sema &SemaRef, SourceLocation PointOfInstantiation, FunctionDecl *Entity,
251 ExceptionSpecification, SourceRange InstantiationRange)
252 : InstantiatingTemplate(
253 SemaRef, ActiveTemplateInstantiation::ExceptionSpecInstantiation,
254 PointOfInstantiation, InstantiationRange, Entity) {}
Richard Smithf623c962012-04-17 00:58:00 +0000255
Benjamin Kramer7761a042015-03-06 16:36:50 +0000256Sema::InstantiatingTemplate::InstantiatingTemplate(
Richard Smith54f18e82016-08-31 02:15:21 +0000257 Sema &SemaRef, SourceLocation PointOfInstantiation, TemplateParameter Param,
258 TemplateDecl *Template, ArrayRef<TemplateArgument> TemplateArgs,
259 SourceRange InstantiationRange)
Benjamin Kramer7761a042015-03-06 16:36:50 +0000260 : InstantiatingTemplate(
261 SemaRef,
262 ActiveTemplateInstantiation::DefaultTemplateArgumentInstantiation,
Richard Smith54f18e82016-08-31 02:15:21 +0000263 PointOfInstantiation, InstantiationRange, getAsNamedDecl(Param),
264 Template, TemplateArgs) {}
Douglas Gregorfcd5db32009-03-10 00:06:19 +0000265
Benjamin Kramer7761a042015-03-06 16:36:50 +0000266Sema::InstantiatingTemplate::InstantiatingTemplate(
267 Sema &SemaRef, SourceLocation PointOfInstantiation,
268 FunctionTemplateDecl *FunctionTemplate,
269 ArrayRef<TemplateArgument> TemplateArgs,
270 ActiveTemplateInstantiation::InstantiationKind Kind,
271 sema::TemplateDeductionInfo &DeductionInfo, SourceRange InstantiationRange)
272 : InstantiatingTemplate(SemaRef, Kind, PointOfInstantiation,
273 InstantiationRange, FunctionTemplate, nullptr,
Richard Smith54f18e82016-08-31 02:15:21 +0000274 TemplateArgs, &DeductionInfo) {
275 assert(
276 Kind == ActiveTemplateInstantiation::ExplicitTemplateArgumentSubstitution ||
277 Kind == ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution);
278}
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000279
Benjamin Kramer7761a042015-03-06 16:36:50 +0000280Sema::InstantiatingTemplate::InstantiatingTemplate(
281 Sema &SemaRef, SourceLocation PointOfInstantiation,
Richard Smith0e617ec2016-12-27 07:56:27 +0000282 TemplateDecl *Template,
283 ArrayRef<TemplateArgument> TemplateArgs,
284 sema::TemplateDeductionInfo &DeductionInfo, SourceRange InstantiationRange)
285 : InstantiatingTemplate(
286 SemaRef,
287 ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution,
288 PointOfInstantiation, InstantiationRange, Template, nullptr,
289 TemplateArgs, &DeductionInfo) {}
290
291Sema::InstantiatingTemplate::InstantiatingTemplate(
292 Sema &SemaRef, SourceLocation PointOfInstantiation,
Benjamin Kramer7761a042015-03-06 16:36:50 +0000293 ClassTemplatePartialSpecializationDecl *PartialSpec,
294 ArrayRef<TemplateArgument> TemplateArgs,
295 sema::TemplateDeductionInfo &DeductionInfo, SourceRange InstantiationRange)
296 : InstantiatingTemplate(
297 SemaRef,
298 ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution,
299 PointOfInstantiation, InstantiationRange, PartialSpec, nullptr,
300 TemplateArgs, &DeductionInfo) {}
Douglas Gregor637d9982009-06-10 23:47:09 +0000301
Larisse Voufo39a1e502013-08-06 01:03:05 +0000302Sema::InstantiatingTemplate::InstantiatingTemplate(
303 Sema &SemaRef, SourceLocation PointOfInstantiation,
304 VarTemplatePartialSpecializationDecl *PartialSpec,
305 ArrayRef<TemplateArgument> TemplateArgs,
306 sema::TemplateDeductionInfo &DeductionInfo, SourceRange InstantiationRange)
Benjamin Kramer7761a042015-03-06 16:36:50 +0000307 : InstantiatingTemplate(
308 SemaRef,
309 ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution,
310 PointOfInstantiation, InstantiationRange, PartialSpec, nullptr,
311 TemplateArgs, &DeductionInfo) {}
Larisse Voufo39a1e502013-08-06 01:03:05 +0000312
Benjamin Kramer7761a042015-03-06 16:36:50 +0000313Sema::InstantiatingTemplate::InstantiatingTemplate(
314 Sema &SemaRef, SourceLocation PointOfInstantiation, ParmVarDecl *Param,
315 ArrayRef<TemplateArgument> TemplateArgs, SourceRange InstantiationRange)
316 : InstantiatingTemplate(
317 SemaRef,
318 ActiveTemplateInstantiation::DefaultFunctionArgumentInstantiation,
319 PointOfInstantiation, InstantiationRange, Param, nullptr,
320 TemplateArgs) {}
Douglas Gregore62e6a02009-11-11 19:13:48 +0000321
Benjamin Kramer7761a042015-03-06 16:36:50 +0000322Sema::InstantiatingTemplate::InstantiatingTemplate(
323 Sema &SemaRef, SourceLocation PointOfInstantiation, NamedDecl *Template,
324 NonTypeTemplateParmDecl *Param, ArrayRef<TemplateArgument> TemplateArgs,
325 SourceRange InstantiationRange)
326 : InstantiatingTemplate(
327 SemaRef,
328 ActiveTemplateInstantiation::PriorTemplateArgumentSubstitution,
329 PointOfInstantiation, InstantiationRange, Param, Template,
330 TemplateArgs) {}
Stephan Tolksdorf232670f2014-03-13 20:34:22 +0000331
Benjamin Kramer7761a042015-03-06 16:36:50 +0000332Sema::InstantiatingTemplate::InstantiatingTemplate(
333 Sema &SemaRef, SourceLocation PointOfInstantiation, NamedDecl *Template,
334 TemplateTemplateParmDecl *Param, ArrayRef<TemplateArgument> TemplateArgs,
335 SourceRange InstantiationRange)
336 : InstantiatingTemplate(
337 SemaRef,
338 ActiveTemplateInstantiation::PriorTemplateArgumentSubstitution,
339 PointOfInstantiation, InstantiationRange, Param, Template,
340 TemplateArgs) {}
Douglas Gregore62e6a02009-11-11 19:13:48 +0000341
Benjamin Kramer7761a042015-03-06 16:36:50 +0000342Sema::InstantiatingTemplate::InstantiatingTemplate(
343 Sema &SemaRef, SourceLocation PointOfInstantiation, TemplateDecl *Template,
344 NamedDecl *Param, ArrayRef<TemplateArgument> TemplateArgs,
345 SourceRange InstantiationRange)
346 : InstantiatingTemplate(
347 SemaRef, ActiveTemplateInstantiation::DefaultTemplateArgumentChecking,
348 PointOfInstantiation, InstantiationRange, Param, Template,
349 TemplateArgs) {}
Anders Carlsson657bad42009-09-05 05:14:19 +0000350
Douglas Gregor85673582009-05-18 17:01:57 +0000351void Sema::InstantiatingTemplate::Clear() {
352 if (!Invalid) {
Richard Smith54f18e82016-08-31 02:15:21 +0000353 auto &Active = SemaRef.ActiveTemplateInstantiations.back();
354 if (!Active.isInstantiationRecord()) {
Douglas Gregor84d49a22009-11-11 21:54:23 +0000355 assert(SemaRef.NonInstantiationEntries > 0);
356 --SemaRef.NonInstantiationEntries;
357 }
Douglas Gregoredb76852011-01-27 22:31:44 +0000358 SemaRef.InNonInstantiationSFINAEContext
359 = SavedInNonInstantiationSFINAEContext;
Richard Smith0e5d7b82013-07-25 23:08:39 +0000360
361 // Name lookup no longer looks in this template's defining module.
362 assert(SemaRef.ActiveTemplateInstantiations.size() >=
363 SemaRef.ActiveTemplateInstantiationLookupModules.size() &&
364 "forgot to remove a lookup module for a template instantiation");
365 if (SemaRef.ActiveTemplateInstantiations.size() ==
366 SemaRef.ActiveTemplateInstantiationLookupModules.size()) {
367 if (Module *M = SemaRef.ActiveTemplateInstantiationLookupModules.back())
368 SemaRef.LookupModulesCache.erase(M);
369 SemaRef.ActiveTemplateInstantiationLookupModules.pop_back();
370 }
371
Richard Smith54f18e82016-08-31 02:15:21 +0000372 if (!AlreadyInstantiating)
373 SemaRef.InstantiatingSpecializations.erase(
374 std::make_pair(Active.Entity, Active.Kind));
375
Douglas Gregorfcd5db32009-03-10 00:06:19 +0000376 SemaRef.ActiveTemplateInstantiations.pop_back();
Douglas Gregor85673582009-05-18 17:01:57 +0000377 Invalid = true;
378 }
Douglas Gregorfcd5db32009-03-10 00:06:19 +0000379}
380
Douglas Gregor79cf6032009-03-10 20:44:00 +0000381bool Sema::InstantiatingTemplate::CheckInstantiationDepth(
382 SourceLocation PointOfInstantiation,
383 SourceRange InstantiationRange) {
Douglas Gregor84d49a22009-11-11 21:54:23 +0000384 assert(SemaRef.NonInstantiationEntries <=
385 SemaRef.ActiveTemplateInstantiations.size());
386 if ((SemaRef.ActiveTemplateInstantiations.size() -
387 SemaRef.NonInstantiationEntries)
David Blaikiebbafb8a2012-03-11 07:00:24 +0000388 <= SemaRef.getLangOpts().InstantiationDepth)
Douglas Gregor79cf6032009-03-10 20:44:00 +0000389 return false;
390
Mike Stump11289f42009-09-09 15:08:12 +0000391 SemaRef.Diag(PointOfInstantiation,
Douglas Gregor79cf6032009-03-10 20:44:00 +0000392 diag::err_template_recursion_depth_exceeded)
David Blaikiebbafb8a2012-03-11 07:00:24 +0000393 << SemaRef.getLangOpts().InstantiationDepth
Douglas Gregor79cf6032009-03-10 20:44:00 +0000394 << InstantiationRange;
395 SemaRef.Diag(PointOfInstantiation, diag::note_template_recursion_depth)
David Blaikiebbafb8a2012-03-11 07:00:24 +0000396 << SemaRef.getLangOpts().InstantiationDepth;
Douglas Gregor79cf6032009-03-10 20:44:00 +0000397 return true;
398}
399
Douglas Gregor4ea568f2009-03-10 18:03:33 +0000400/// \brief Prints the current instantiation stack through a series of
401/// notes.
402void Sema::PrintInstantiationStack() {
Douglas Gregorffed1cb2010-04-20 07:18:24 +0000403 // Determine which template instantiations to skip, if any.
404 unsigned SkipStart = ActiveTemplateInstantiations.size(), SkipEnd = SkipStart;
405 unsigned Limit = Diags.getTemplateBacktraceLimit();
406 if (Limit && Limit < ActiveTemplateInstantiations.size()) {
407 SkipStart = Limit / 2 + Limit % 2;
408 SkipEnd = ActiveTemplateInstantiations.size() - Limit / 2;
409 }
410
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000411 // FIXME: In all of these cases, we need to show the template arguments
Douglas Gregorffed1cb2010-04-20 07:18:24 +0000412 unsigned InstantiationIdx = 0;
Craig Topper2341c0d2013-07-04 03:08:24 +0000413 for (SmallVectorImpl<ActiveTemplateInstantiation>::reverse_iterator
Douglas Gregor4ea568f2009-03-10 18:03:33 +0000414 Active = ActiveTemplateInstantiations.rbegin(),
415 ActiveEnd = ActiveTemplateInstantiations.rend();
416 Active != ActiveEnd;
Douglas Gregorffed1cb2010-04-20 07:18:24 +0000417 ++Active, ++InstantiationIdx) {
418 // Skip this instantiation?
419 if (InstantiationIdx >= SkipStart && InstantiationIdx < SkipEnd) {
420 if (InstantiationIdx == SkipStart) {
421 // Note that we're skipping instantiations.
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000422 Diags.Report(Active->PointOfInstantiation,
Douglas Gregorffed1cb2010-04-20 07:18:24 +0000423 diag::note_instantiation_contexts_suppressed)
424 << unsigned(ActiveTemplateInstantiations.size() - Limit);
425 }
426 continue;
427 }
428
Douglas Gregor79cf6032009-03-10 20:44:00 +0000429 switch (Active->Kind) {
430 case ActiveTemplateInstantiation::TemplateInstantiation: {
Nick Lewyckycc8990f2012-11-16 08:40:59 +0000431 Decl *D = Active->Entity;
Douglas Gregor85673582009-05-18 17:01:57 +0000432 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) {
433 unsigned DiagID = diag::note_template_member_class_here;
434 if (isa<ClassTemplateSpecializationDecl>(Record))
435 DiagID = diag::note_template_class_instantiation_here;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000436 Diags.Report(Active->PointOfInstantiation, DiagID)
Richard Smith792c22d2016-12-24 04:09:05 +0000437 << Record << Active->InstantiationRange;
Douglas Gregora6ef8f02009-07-24 20:34:43 +0000438 } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
Douglas Gregor4adbc6d2009-06-26 00:10:03 +0000439 unsigned DiagID;
440 if (Function->getPrimaryTemplate())
441 DiagID = diag::note_function_template_spec_here;
442 else
443 DiagID = diag::note_template_member_function_here;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000444 Diags.Report(Active->PointOfInstantiation, DiagID)
Douglas Gregor85673582009-05-18 17:01:57 +0000445 << Function
446 << Active->InstantiationRange;
Richard Smith3f1b5d02011-05-05 21:57:07 +0000447 } else if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000448 Diags.Report(Active->PointOfInstantiation,
Larisse Voufodbd65772013-08-14 20:15:02 +0000449 VD->isStaticDataMember()?
450 diag::note_template_static_data_member_def_here
451 : diag::note_template_variable_def_here)
Richard Smith3f1b5d02011-05-05 21:57:07 +0000452 << VD
453 << Active->InstantiationRange;
Richard Smith4b38ded2012-03-14 23:13:10 +0000454 } else if (EnumDecl *ED = dyn_cast<EnumDecl>(D)) {
455 Diags.Report(Active->PointOfInstantiation,
456 diag::note_template_enum_def_here)
457 << ED
458 << Active->InstantiationRange;
Reid Klecknerd60b82f2014-11-17 23:36:45 +0000459 } else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
460 Diags.Report(Active->PointOfInstantiation,
461 diag::note_template_nsdmi_here)
462 << FD << Active->InstantiationRange;
Richard Smith3f1b5d02011-05-05 21:57:07 +0000463 } else {
464 Diags.Report(Active->PointOfInstantiation,
465 diag::note_template_type_alias_instantiation_here)
466 << cast<TypeAliasTemplateDecl>(D)
Douglas Gregora6ef8f02009-07-24 20:34:43 +0000467 << Active->InstantiationRange;
Douglas Gregor85673582009-05-18 17:01:57 +0000468 }
Douglas Gregor79cf6032009-03-10 20:44:00 +0000469 break;
470 }
471
472 case ActiveTemplateInstantiation::DefaultTemplateArgumentInstantiation: {
Richard Smith54f18e82016-08-31 02:15:21 +0000473 TemplateDecl *Template = cast<TemplateDecl>(Active->Template);
Benjamin Kramer9170e912013-02-22 15:46:01 +0000474 SmallVector<char, 128> TemplateArgsStr;
475 llvm::raw_svector_ostream OS(TemplateArgsStr);
476 Template->printName(OS);
David Majnemer6fbeee32016-07-07 04:43:07 +0000477 TemplateSpecializationType::PrintTemplateArgumentList(
478 OS, Active->template_arguments(), getPrintingPolicy());
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000479 Diags.Report(Active->PointOfInstantiation,
Douglas Gregor79cf6032009-03-10 20:44:00 +0000480 diag::note_default_arg_instantiation_here)
Benjamin Kramer9170e912013-02-22 15:46:01 +0000481 << OS.str()
Douglas Gregor79cf6032009-03-10 20:44:00 +0000482 << Active->InstantiationRange;
483 break;
484 }
Douglas Gregor637d9982009-06-10 23:47:09 +0000485
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000486 case ActiveTemplateInstantiation::ExplicitTemplateArgumentSubstitution: {
Nick Lewyckycc8990f2012-11-16 08:40:59 +0000487 FunctionTemplateDecl *FnTmpl = cast<FunctionTemplateDecl>(Active->Entity);
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000488 Diags.Report(Active->PointOfInstantiation,
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000489 diag::note_explicit_template_arg_substitution_here)
Douglas Gregor607f1412010-03-30 20:35:20 +0000490 << FnTmpl
491 << getTemplateArgumentBindingsText(FnTmpl->getTemplateParameters(),
492 Active->TemplateArgs,
493 Active->NumTemplateArgs)
494 << Active->InstantiationRange;
Douglas Gregor637d9982009-06-10 23:47:09 +0000495 break;
496 }
Mike Stump11289f42009-09-09 15:08:12 +0000497
Richard Smithe68a38f2016-12-24 04:20:31 +0000498 case ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution: {
499 if (FunctionTemplateDecl *FnTmpl =
500 dyn_cast<FunctionTemplateDecl>(Active->Entity)) {
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000501 Diags.Report(Active->PointOfInstantiation,
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000502 diag::note_function_template_deduction_instantiation_here)
Douglas Gregor607f1412010-03-30 20:35:20 +0000503 << FnTmpl
504 << getTemplateArgumentBindingsText(FnTmpl->getTemplateParameters(),
505 Active->TemplateArgs,
506 Active->NumTemplateArgs)
507 << Active->InstantiationRange;
Richard Smithe68a38f2016-12-24 04:20:31 +0000508 } else {
509 bool IsVar = isa<VarTemplateDecl>(Active->Entity) ||
510 isa<VarTemplateSpecializationDecl>(Active->Entity);
Richard Smith0e617ec2016-12-27 07:56:27 +0000511 bool IsTemplate = false;
Richard Smithe68a38f2016-12-24 04:20:31 +0000512 TemplateParameterList *Params;
Richard Smith0e617ec2016-12-27 07:56:27 +0000513 if (auto *D = dyn_cast<TemplateDecl>(Active->Entity)) {
514 IsTemplate = true;
515 Params = D->getTemplateParameters();
516 } else if (auto *D = dyn_cast<ClassTemplatePartialSpecializationDecl>(
Richard Smithe68a38f2016-12-24 04:20:31 +0000517 Active->Entity)) {
518 Params = D->getTemplateParameters();
519 } else if (auto *D = dyn_cast<VarTemplatePartialSpecializationDecl>(
520 Active->Entity)) {
521 Params = D->getTemplateParameters();
522 } else {
523 llvm_unreachable("unexpected template kind");
524 }
525
Richard Smithe68a38f2016-12-24 04:20:31 +0000526 Diags.Report(Active->PointOfInstantiation,
527 diag::note_deduced_template_arg_substitution_here)
Richard Smith0e617ec2016-12-27 07:56:27 +0000528 << IsVar << IsTemplate << cast<NamedDecl>(Active->Entity)
Richard Smithe68a38f2016-12-24 04:20:31 +0000529 << getTemplateArgumentBindingsText(Params, Active->TemplateArgs,
530 Active->NumTemplateArgs)
531 << Active->InstantiationRange;
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000532 }
533 break;
Richard Smithe68a38f2016-12-24 04:20:31 +0000534 }
Douglas Gregor637d9982009-06-10 23:47:09 +0000535
Anders Carlsson657bad42009-09-05 05:14:19 +0000536 case ActiveTemplateInstantiation::DefaultFunctionArgumentInstantiation: {
Nick Lewyckycc8990f2012-11-16 08:40:59 +0000537 ParmVarDecl *Param = cast<ParmVarDecl>(Active->Entity);
Anders Carlsson657bad42009-09-05 05:14:19 +0000538 FunctionDecl *FD = cast<FunctionDecl>(Param->getDeclContext());
Mike Stump11289f42009-09-09 15:08:12 +0000539
Benjamin Kramer9170e912013-02-22 15:46:01 +0000540 SmallVector<char, 128> TemplateArgsStr;
541 llvm::raw_svector_ostream OS(TemplateArgsStr);
542 FD->printName(OS);
David Majnemer6fbeee32016-07-07 04:43:07 +0000543 TemplateSpecializationType::PrintTemplateArgumentList(
544 OS, Active->template_arguments(), getPrintingPolicy());
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000545 Diags.Report(Active->PointOfInstantiation,
Anders Carlsson657bad42009-09-05 05:14:19 +0000546 diag::note_default_function_arg_instantiation_here)
Benjamin Kramer9170e912013-02-22 15:46:01 +0000547 << OS.str()
Anders Carlsson657bad42009-09-05 05:14:19 +0000548 << Active->InstantiationRange;
549 break;
550 }
Mike Stump11289f42009-09-09 15:08:12 +0000551
Douglas Gregore62e6a02009-11-11 19:13:48 +0000552 case ActiveTemplateInstantiation::PriorTemplateArgumentSubstitution: {
Nick Lewyckycc8990f2012-11-16 08:40:59 +0000553 NamedDecl *Parm = cast<NamedDecl>(Active->Entity);
Douglas Gregore62e6a02009-11-11 19:13:48 +0000554 std::string Name;
555 if (!Parm->getName().empty())
556 Name = std::string(" '") + Parm->getName().str() + "'";
Craig Topperc3ec1492014-05-26 06:22:03 +0000557
558 TemplateParameterList *TemplateParams = nullptr;
Douglas Gregorca4686d2011-01-04 23:35:54 +0000559 if (TemplateDecl *Template = dyn_cast<TemplateDecl>(Active->Template))
560 TemplateParams = Template->getTemplateParameters();
561 else
562 TemplateParams =
563 cast<ClassTemplatePartialSpecializationDecl>(Active->Template)
564 ->getTemplateParameters();
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000565 Diags.Report(Active->PointOfInstantiation,
Douglas Gregore62e6a02009-11-11 19:13:48 +0000566 diag::note_prior_template_arg_substitution)
567 << isa<TemplateTemplateParmDecl>(Parm)
568 << Name
Douglas Gregorca4686d2011-01-04 23:35:54 +0000569 << getTemplateArgumentBindingsText(TemplateParams,
Douglas Gregore62e6a02009-11-11 19:13:48 +0000570 Active->TemplateArgs,
571 Active->NumTemplateArgs)
572 << Active->InstantiationRange;
573 break;
574 }
Douglas Gregor84d49a22009-11-11 21:54:23 +0000575
576 case ActiveTemplateInstantiation::DefaultTemplateArgumentChecking: {
Craig Topperc3ec1492014-05-26 06:22:03 +0000577 TemplateParameterList *TemplateParams = nullptr;
Douglas Gregorca4686d2011-01-04 23:35:54 +0000578 if (TemplateDecl *Template = dyn_cast<TemplateDecl>(Active->Template))
579 TemplateParams = Template->getTemplateParameters();
580 else
581 TemplateParams =
582 cast<ClassTemplatePartialSpecializationDecl>(Active->Template)
583 ->getTemplateParameters();
584
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000585 Diags.Report(Active->PointOfInstantiation,
Douglas Gregor84d49a22009-11-11 21:54:23 +0000586 diag::note_template_default_arg_checking)
Douglas Gregorca4686d2011-01-04 23:35:54 +0000587 << getTemplateArgumentBindingsText(TemplateParams,
Douglas Gregor84d49a22009-11-11 21:54:23 +0000588 Active->TemplateArgs,
589 Active->NumTemplateArgs)
590 << Active->InstantiationRange;
591 break;
592 }
Richard Smithf623c962012-04-17 00:58:00 +0000593
594 case ActiveTemplateInstantiation::ExceptionSpecInstantiation:
595 Diags.Report(Active->PointOfInstantiation,
596 diag::note_template_exception_spec_instantiation_here)
Nick Lewyckycc8990f2012-11-16 08:40:59 +0000597 << cast<FunctionDecl>(Active->Entity)
Richard Smithf623c962012-04-17 00:58:00 +0000598 << Active->InstantiationRange;
599 break;
Douglas Gregor79cf6032009-03-10 20:44:00 +0000600 }
Douglas Gregor4ea568f2009-03-10 18:03:33 +0000601 }
602}
603
David Blaikie05785d12013-02-20 22:23:23 +0000604Optional<TemplateDeductionInfo *> Sema::isSFINAEContext() const {
Douglas Gregoredb76852011-01-27 22:31:44 +0000605 if (InNonInstantiationSFINAEContext)
Craig Topperc3ec1492014-05-26 06:22:03 +0000606 return Optional<TemplateDeductionInfo *>(nullptr);
Douglas Gregoredb76852011-01-27 22:31:44 +0000607
Craig Topper2341c0d2013-07-04 03:08:24 +0000608 for (SmallVectorImpl<ActiveTemplateInstantiation>::const_reverse_iterator
Douglas Gregor33834512009-06-14 07:33:30 +0000609 Active = ActiveTemplateInstantiations.rbegin(),
610 ActiveEnd = ActiveTemplateInstantiations.rend();
611 Active != ActiveEnd;
Douglas Gregor84d49a22009-11-11 21:54:23 +0000612 ++Active)
613 {
Douglas Gregor33834512009-06-14 07:33:30 +0000614 switch(Active->Kind) {
Douglas Gregoredb76852011-01-27 22:31:44 +0000615 case ActiveTemplateInstantiation::TemplateInstantiation:
Richard Smith72249ba2012-04-26 07:24:08 +0000616 // An instantiation of an alias template may or may not be a SFINAE
617 // context, depending on what else is on the stack.
Nick Lewyckycc8990f2012-11-16 08:40:59 +0000618 if (isa<TypeAliasTemplateDecl>(Active->Entity))
Richard Smith72249ba2012-04-26 07:24:08 +0000619 break;
620 // Fall through.
621 case ActiveTemplateInstantiation::DefaultFunctionArgumentInstantiation:
Richard Smithf623c962012-04-17 00:58:00 +0000622 case ActiveTemplateInstantiation::ExceptionSpecInstantiation:
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000623 // This is a template instantiation, so there is no SFINAE.
David Blaikie7a30dc52013-02-21 01:47:18 +0000624 return None;
Mike Stump11289f42009-09-09 15:08:12 +0000625
Douglas Gregor33834512009-06-14 07:33:30 +0000626 case ActiveTemplateInstantiation::DefaultTemplateArgumentInstantiation:
Douglas Gregore62e6a02009-11-11 19:13:48 +0000627 case ActiveTemplateInstantiation::PriorTemplateArgumentSubstitution:
Douglas Gregor84d49a22009-11-11 21:54:23 +0000628 case ActiveTemplateInstantiation::DefaultTemplateArgumentChecking:
Douglas Gregore62e6a02009-11-11 19:13:48 +0000629 // A default template argument instantiation and substitution into
630 // template parameters with arguments for prior parameters may or may
631 // not be a SFINAE context; look further up the stack.
Douglas Gregor33834512009-06-14 07:33:30 +0000632 break;
Mike Stump11289f42009-09-09 15:08:12 +0000633
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000634 case ActiveTemplateInstantiation::ExplicitTemplateArgumentSubstitution:
635 case ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution:
636 // We're either substitution explicitly-specified template arguments
637 // or deduced template arguments, so SFINAE applies.
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +0000638 assert(Active->DeductionInfo && "Missing deduction info pointer");
639 return Active->DeductionInfo;
Douglas Gregor33834512009-06-14 07:33:30 +0000640 }
641 }
642
David Blaikie7a30dc52013-02-21 01:47:18 +0000643 return None;
Douglas Gregor33834512009-06-14 07:33:30 +0000644}
645
Douglas Gregora8bac7f2011-01-10 07:32:04 +0000646/// \brief Retrieve the depth and index of a parameter pack.
647static std::pair<unsigned, unsigned>
648getDepthAndIndex(NamedDecl *ND) {
649 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(ND))
650 return std::make_pair(TTP->getDepth(), TTP->getIndex());
651
652 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(ND))
653 return std::make_pair(NTTP->getDepth(), NTTP->getIndex());
654
655 TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(ND);
656 return std::make_pair(TTP->getDepth(), TTP->getIndex());
657}
658
Douglas Gregorfe1e1102009-02-27 19:31:52 +0000659//===----------------------------------------------------------------------===/
660// Template Instantiation for Types
661//===----------------------------------------------------------------------===/
Douglas Gregor17c0d7b2009-02-28 00:25:32 +0000662namespace {
Douglas Gregor14cf7522010-04-30 18:55:50 +0000663 class TemplateInstantiator : public TreeTransform<TemplateInstantiator> {
Douglas Gregor01afeef2009-08-28 20:31:08 +0000664 const MultiLevelTemplateArgumentList &TemplateArgs;
Douglas Gregor17c0d7b2009-02-28 00:25:32 +0000665 SourceLocation Loc;
666 DeclarationName Entity;
Douglas Gregorfe1e1102009-02-27 19:31:52 +0000667
Douglas Gregor17c0d7b2009-02-28 00:25:32 +0000668 public:
Douglas Gregorebe10102009-08-20 07:17:43 +0000669 typedef TreeTransform<TemplateInstantiator> inherited;
Mike Stump11289f42009-09-09 15:08:12 +0000670
671 TemplateInstantiator(Sema &SemaRef,
Douglas Gregor01afeef2009-08-28 20:31:08 +0000672 const MultiLevelTemplateArgumentList &TemplateArgs,
Douglas Gregord6ff3322009-08-04 16:50:30 +0000673 SourceLocation Loc,
Mike Stump11289f42009-09-09 15:08:12 +0000674 DeclarationName Entity)
675 : inherited(SemaRef), TemplateArgs(TemplateArgs), Loc(Loc),
Douglas Gregorebe10102009-08-20 07:17:43 +0000676 Entity(Entity) { }
Douglas Gregor17c0d7b2009-02-28 00:25:32 +0000677
Mike Stump11289f42009-09-09 15:08:12 +0000678 /// \brief Determine whether the given type \p T has already been
Douglas Gregord6ff3322009-08-04 16:50:30 +0000679 /// transformed.
680 ///
681 /// For the purposes of template instantiation, a type has already been
682 /// transformed if it is NULL or if it is not dependent.
Douglas Gregor5597ab42010-05-07 23:12:07 +0000683 bool AlreadyTransformed(QualType T);
Mike Stump11289f42009-09-09 15:08:12 +0000684
Douglas Gregord6ff3322009-08-04 16:50:30 +0000685 /// \brief Returns the location of the entity being instantiated, if known.
686 SourceLocation getBaseLocation() { return Loc; }
Mike Stump11289f42009-09-09 15:08:12 +0000687
Douglas Gregord6ff3322009-08-04 16:50:30 +0000688 /// \brief Returns the name of the entity being instantiated, if any.
689 DeclarationName getBaseEntity() { return Entity; }
Mike Stump11289f42009-09-09 15:08:12 +0000690
Douglas Gregoref6ab412009-10-27 06:26:26 +0000691 /// \brief Sets the "base" location and entity when that
692 /// information is known based on another transformation.
693 void setBase(SourceLocation Loc, DeclarationName Entity) {
694 this->Loc = Loc;
695 this->Entity = Entity;
696 }
Douglas Gregor840bd6c2010-12-20 22:05:00 +0000697
698 bool TryExpandParameterPacks(SourceLocation EllipsisLoc,
699 SourceRange PatternRange,
Robert Wilhelm16e94b92013-08-09 18:02:13 +0000700 ArrayRef<UnexpandedParameterPack> Unexpanded,
701 bool &ShouldExpand, bool &RetainExpansion,
David Blaikie05785d12013-02-20 22:23:23 +0000702 Optional<unsigned> &NumExpansions) {
Douglas Gregor76aca7b2010-12-21 00:52:54 +0000703 return getSema().CheckParameterPacksForExpansion(EllipsisLoc,
704 PatternRange, Unexpanded,
Douglas Gregor76aca7b2010-12-21 00:52:54 +0000705 TemplateArgs,
706 ShouldExpand,
Douglas Gregora8bac7f2011-01-10 07:32:04 +0000707 RetainExpansion,
Douglas Gregor76aca7b2010-12-21 00:52:54 +0000708 NumExpansions);
709 }
Douglas Gregor840bd6c2010-12-20 22:05:00 +0000710
Douglas Gregorf3010112011-01-07 16:43:16 +0000711 void ExpandingFunctionParameterPack(ParmVarDecl *Pack) {
712 SemaRef.CurrentInstantiationScope->MakeInstantiatedLocalArgPack(Pack);
713 }
714
Douglas Gregora8bac7f2011-01-10 07:32:04 +0000715 TemplateArgument ForgetPartiallySubstitutedPack() {
716 TemplateArgument Result;
717 if (NamedDecl *PartialPack
718 = SemaRef.CurrentInstantiationScope->getPartiallySubstitutedPack()){
719 MultiLevelTemplateArgumentList &TemplateArgs
720 = const_cast<MultiLevelTemplateArgumentList &>(this->TemplateArgs);
721 unsigned Depth, Index;
Benjamin Kramer867ea1d2014-03-02 13:01:17 +0000722 std::tie(Depth, Index) = getDepthAndIndex(PartialPack);
Douglas Gregora8bac7f2011-01-10 07:32:04 +0000723 if (TemplateArgs.hasTemplateArgument(Depth, Index)) {
724 Result = TemplateArgs(Depth, Index);
725 TemplateArgs.setArgument(Depth, Index, TemplateArgument());
726 }
727 }
728
729 return Result;
730 }
731
732 void RememberPartiallySubstitutedPack(TemplateArgument Arg) {
733 if (Arg.isNull())
734 return;
735
736 if (NamedDecl *PartialPack
737 = SemaRef.CurrentInstantiationScope->getPartiallySubstitutedPack()){
738 MultiLevelTemplateArgumentList &TemplateArgs
739 = const_cast<MultiLevelTemplateArgumentList &>(this->TemplateArgs);
740 unsigned Depth, Index;
Benjamin Kramer867ea1d2014-03-02 13:01:17 +0000741 std::tie(Depth, Index) = getDepthAndIndex(PartialPack);
Douglas Gregora8bac7f2011-01-10 07:32:04 +0000742 TemplateArgs.setArgument(Depth, Index, Arg);
743 }
744 }
745
Douglas Gregord6ff3322009-08-04 16:50:30 +0000746 /// \brief Transform the given declaration by instantiating a reference to
747 /// this declaration.
Douglas Gregora04f2ca2010-03-01 15:56:25 +0000748 Decl *TransformDecl(SourceLocation Loc, Decl *D);
Douglas Gregora16548e2009-08-11 05:31:07 +0000749
Douglas Gregor0c46b2b2012-02-13 22:00:16 +0000750 void transformAttrs(Decl *Old, Decl *New) {
751 SemaRef.InstantiateAttrs(TemplateArgs, Old, New);
752 }
753
754 void transformedLocalDecl(Decl *Old, Decl *New) {
Richard Smithc38498f2015-04-27 21:27:54 +0000755 // If we've instantiated the call operator of a lambda or the call
756 // operator template of a generic lambda, update the "instantiation of"
757 // information.
758 auto *NewMD = dyn_cast<CXXMethodDecl>(New);
759 if (NewMD && isLambdaCallOperator(NewMD)) {
760 auto *OldMD = dyn_cast<CXXMethodDecl>(Old);
761 if (auto *NewTD = NewMD->getDescribedFunctionTemplate())
762 NewTD->setInstantiatedFromMemberTemplate(
763 OldMD->getDescribedFunctionTemplate());
764 else
765 NewMD->setInstantiationOfMemberFunction(OldMD,
766 TSK_ImplicitInstantiation);
767 }
768
Douglas Gregor0c46b2b2012-02-13 22:00:16 +0000769 SemaRef.CurrentInstantiationScope->InstantiatedLocal(Old, New);
Richard Smithc7649dc2016-03-23 20:07:07 +0000770
771 // We recreated a local declaration, but not by instantiating it. There
772 // may be pending dependent diagnostics to produce.
773 if (auto *DC = dyn_cast<DeclContext>(Old))
774 SemaRef.PerformDependentDiagnostics(DC, TemplateArgs);
Douglas Gregor0c46b2b2012-02-13 22:00:16 +0000775 }
776
Mike Stump11289f42009-09-09 15:08:12 +0000777 /// \brief Transform the definition of the given declaration by
Douglas Gregorebe10102009-08-20 07:17:43 +0000778 /// instantiating it.
Douglas Gregor25289362010-03-01 17:25:41 +0000779 Decl *TransformDefinition(SourceLocation Loc, Decl *D);
Mike Stump11289f42009-09-09 15:08:12 +0000780
Dmitri Gribenko00bcdd32012-09-12 17:01:48 +0000781 /// \brief Transform the first qualifier within a scope by instantiating the
Douglas Gregora5cb6da2009-10-20 05:58:46 +0000782 /// declaration.
783 NamedDecl *TransformFirstQualifierInScope(NamedDecl *D, SourceLocation Loc);
784
Douglas Gregorebe10102009-08-20 07:17:43 +0000785 /// \brief Rebuild the exception declaration and register the declaration
786 /// as an instantiated local.
Douglas Gregor9f0e1aa2010-09-09 17:09:21 +0000787 VarDecl *RebuildExceptionDecl(VarDecl *ExceptionDecl,
John McCallbcd03502009-12-07 02:54:59 +0000788 TypeSourceInfo *Declarator,
Abramo Bagnaradff19302011-03-08 08:55:46 +0000789 SourceLocation StartLoc,
790 SourceLocation NameLoc,
791 IdentifierInfo *Name);
Mike Stump11289f42009-09-09 15:08:12 +0000792
Douglas Gregorf4e837f2010-04-26 17:57:08 +0000793 /// \brief Rebuild the Objective-C exception declaration and register the
794 /// declaration as an instantiated local.
795 VarDecl *RebuildObjCExceptionDecl(VarDecl *ExceptionDecl,
796 TypeSourceInfo *TSInfo, QualType T);
797
John McCall7f41d982009-09-11 04:59:25 +0000798 /// \brief Check for tag mismatches when instantiating an
799 /// elaborated type.
John McCall954b5de2010-11-04 19:04:38 +0000800 QualType RebuildElaboratedType(SourceLocation KeywordLoc,
801 ElaboratedTypeKeyword Keyword,
Douglas Gregor844cb502011-03-01 18:12:44 +0000802 NestedNameSpecifierLoc QualifierLoc,
803 QualType T);
John McCall7f41d982009-09-11 04:59:25 +0000804
Craig Topperc3ec1492014-05-26 06:22:03 +0000805 TemplateName
806 TransformTemplateName(CXXScopeSpec &SS, TemplateName Name,
807 SourceLocation NameLoc,
808 QualType ObjectType = QualType(),
Richard Smithfd3dae02017-01-20 00:20:39 +0000809 NamedDecl *FirstQualifierInScope = nullptr,
810 bool AllowInjectedClassName = false);
Douglas Gregor9db53502011-03-02 18:07:45 +0000811
Tyler Nowickic724a83e2014-10-12 20:46:07 +0000812 const LoopHintAttr *TransformLoopHintAttr(const LoopHintAttr *LH);
813
John McCalldadc5752010-08-24 06:29:42 +0000814 ExprResult TransformPredefinedExpr(PredefinedExpr *E);
815 ExprResult TransformDeclRefExpr(DeclRefExpr *E);
816 ExprResult TransformCXXDefaultArgExpr(CXXDefaultArgExpr *E);
Richard Smithb15fe3a2012-09-12 00:56:43 +0000817
John McCalldadc5752010-08-24 06:29:42 +0000818 ExprResult TransformTemplateParmRefExpr(DeclRefExpr *E,
Douglas Gregoreb5a39d2010-12-24 00:15:10 +0000819 NonTypeTemplateParmDecl *D);
Douglas Gregorcdbc5392011-01-15 01:15:58 +0000820 ExprResult TransformSubstNonTypeTemplateParmPackExpr(
821 SubstNonTypeTemplateParmPackExpr *E);
Richard Smithb15fe3a2012-09-12 00:56:43 +0000822
823 /// \brief Rebuild a DeclRefExpr for a ParmVarDecl reference.
824 ExprResult RebuildParmVarDeclRefExpr(ParmVarDecl *PD, SourceLocation Loc);
825
826 /// \brief Transform a reference to a function parameter pack.
827 ExprResult TransformFunctionParmPackRefExpr(DeclRefExpr *E,
828 ParmVarDecl *PD);
829
830 /// \brief Transform a FunctionParmPackExpr which was built when we couldn't
831 /// expand a function parameter pack reference which refers to an expanded
832 /// pack.
833 ExprResult TransformFunctionParmPackExpr(FunctionParmPackExpr *E);
834
Hans Wennborge113c202014-09-18 16:01:32 +0000835 QualType TransformFunctionProtoType(TypeLocBuilder &TLB,
Richard Smith2e321552014-11-12 02:00:47 +0000836 FunctionProtoTypeLoc TL) {
837 // Call the base version; it will forward to our overridden version below.
838 return inherited::TransformFunctionProtoType(TLB, TL);
839 }
840
841 template<typename Fn>
Douglas Gregor3024f072012-04-16 07:05:22 +0000842 QualType TransformFunctionProtoType(TypeLocBuilder &TLB,
843 FunctionProtoTypeLoc TL,
844 CXXRecordDecl *ThisContext,
Richard Smith2e321552014-11-12 02:00:47 +0000845 unsigned ThisTypeQuals,
846 Fn TransformExceptionSpec);
Douglas Gregor3024f072012-04-16 07:05:22 +0000847
Douglas Gregor715e4612011-01-14 22:40:04 +0000848 ParmVarDecl *TransformFunctionTypeParam(ParmVarDecl *OldParm,
John McCall8fb0d9d2011-05-01 22:35:37 +0000849 int indexAdjustment,
David Blaikie05785d12013-02-20 22:23:23 +0000850 Optional<unsigned> NumExpansions,
Douglas Gregor0dd22bc2012-01-25 16:15:54 +0000851 bool ExpectParameterPack);
John McCall58f10c32010-03-11 09:03:00 +0000852
Mike Stump11289f42009-09-09 15:08:12 +0000853 /// \brief Transforms a template type parameter type by performing
Douglas Gregord6ff3322009-08-04 16:50:30 +0000854 /// substitution of the corresponding template type argument.
John McCall550e0c22009-10-21 00:40:46 +0000855 QualType TransformTemplateTypeParmType(TypeLocBuilder &TLB,
John McCall31f82722010-11-12 08:19:04 +0000856 TemplateTypeParmTypeLoc TL);
Nick Lewyckyc96c37f2010-07-06 19:51:49 +0000857
Douglas Gregorada4b792011-01-14 02:55:32 +0000858 /// \brief Transforms an already-substituted template type parameter pack
859 /// into either itself (if we aren't substituting into its pack expansion)
860 /// or the appropriate substituted argument.
861 QualType TransformSubstTemplateTypeParmPackType(TypeLocBuilder &TLB,
862 SubstTemplateTypeParmPackTypeLoc TL);
863
Richard Smith2589b9802012-07-25 03:56:55 +0000864 ExprResult TransformLambdaExpr(LambdaExpr *E) {
865 LocalInstantiationScope Scope(SemaRef, /*CombineWithOuterScope=*/true);
866 return TreeTransform<TemplateInstantiator>::TransformLambdaExpr(E);
867 }
868
David Majnemerb1004102014-03-02 18:46:05 +0000869 TemplateParameterList *TransformTemplateParameterList(
Faisal Vali2cba1332013-10-23 06:44:28 +0000870 TemplateParameterList *OrigTPL) {
871 if (!OrigTPL || !OrigTPL->size()) return OrigTPL;
872
873 DeclContext *Owner = OrigTPL->getParam(0)->getDeclContext();
874 TemplateDeclInstantiator DeclInstantiator(getSema(),
875 /* DeclContext *Owner */ Owner, TemplateArgs);
876 return DeclInstantiator.SubstTemplateParams(OrigTPL);
877 }
John McCall7c454bb2011-07-15 05:09:51 +0000878 private:
879 ExprResult transformNonTypeTemplateParmRef(NonTypeTemplateParmDecl *parm,
880 SourceLocation loc,
Richard Smith34349002012-07-09 03:07:20 +0000881 TemplateArgument arg);
Douglas Gregord6ff3322009-08-04 16:50:30 +0000882 };
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000883}
Douglas Gregor04318252009-07-06 15:59:29 +0000884
Douglas Gregor5597ab42010-05-07 23:12:07 +0000885bool TemplateInstantiator::AlreadyTransformed(QualType T) {
886 if (T.isNull())
887 return true;
888
Douglas Gregor678d76c2011-07-01 01:22:09 +0000889 if (T->isInstantiationDependentType() || T->isVariablyModifiedType())
Douglas Gregor5597ab42010-05-07 23:12:07 +0000890 return false;
891
892 getSema().MarkDeclarationsReferencedInType(Loc, T);
893 return true;
894}
895
Eli Friedman8917ad52013-07-19 19:40:38 +0000896static TemplateArgument
897getPackSubstitutedTemplateArgument(Sema &S, TemplateArgument Arg) {
898 assert(S.ArgumentPackSubstitutionIndex >= 0);
899 assert(S.ArgumentPackSubstitutionIndex < (int)Arg.pack_size());
900 Arg = Arg.pack_begin()[S.ArgumentPackSubstitutionIndex];
901 if (Arg.isPackExpansion())
902 Arg = Arg.getPackExpansionPattern();
903 return Arg;
904}
905
Douglas Gregora04f2ca2010-03-01 15:56:25 +0000906Decl *TemplateInstantiator::TransformDecl(SourceLocation Loc, Decl *D) {
Douglas Gregor2b6ca462009-09-03 21:38:09 +0000907 if (!D)
Craig Topperc3ec1492014-05-26 06:22:03 +0000908 return nullptr;
Mike Stump11289f42009-09-09 15:08:12 +0000909
Douglas Gregor2b6ca462009-09-03 21:38:09 +0000910 if (TemplateTemplateParmDecl *TTP = dyn_cast<TemplateTemplateParmDecl>(D)) {
Douglas Gregor01afeef2009-08-28 20:31:08 +0000911 if (TTP->getDepth() < TemplateArgs.getNumLevels()) {
Douglas Gregorb93971082010-02-05 19:54:12 +0000912 // If the corresponding template argument is NULL or non-existent, it's
913 // because we are performing instantiation from explicitly-specified
914 // template arguments in a function template, but there were some
915 // arguments left unspecified.
916 if (!TemplateArgs.hasTemplateArgument(TTP->getDepth(),
917 TTP->getPosition()))
918 return D;
919
Douglas Gregorf5500772011-01-05 15:48:55 +0000920 TemplateArgument Arg = TemplateArgs(TTP->getDepth(), TTP->getPosition());
921
922 if (TTP->isParameterPack()) {
923 assert(Arg.getKind() == TemplateArgument::Pack &&
924 "Missing argument pack");
Eli Friedman8917ad52013-07-19 19:40:38 +0000925 Arg = getPackSubstitutedTemplateArgument(getSema(), Arg);
Douglas Gregorf5500772011-01-05 15:48:55 +0000926 }
927
928 TemplateName Template = Arg.getAsTemplate();
Douglas Gregor9167f8b2009-11-11 01:00:40 +0000929 assert(!Template.isNull() && Template.getAsTemplateDecl() &&
Douglas Gregor01afeef2009-08-28 20:31:08 +0000930 "Wrong kind of template template argument");
Douglas Gregor9167f8b2009-11-11 01:00:40 +0000931 return Template.getAsTemplateDecl();
Douglas Gregor01afeef2009-08-28 20:31:08 +0000932 }
Mike Stump11289f42009-09-09 15:08:12 +0000933
Douglas Gregor9167f8b2009-11-11 01:00:40 +0000934 // Fall through to find the instantiated declaration for this template
935 // template parameter.
Douglas Gregor71dc5092009-08-06 06:41:21 +0000936 }
Mike Stump11289f42009-09-09 15:08:12 +0000937
Douglas Gregora04f2ca2010-03-01 15:56:25 +0000938 return SemaRef.FindInstantiatedDecl(Loc, cast<NamedDecl>(D), TemplateArgs);
Douglas Gregord6ff3322009-08-04 16:50:30 +0000939}
940
Douglas Gregor25289362010-03-01 17:25:41 +0000941Decl *TemplateInstantiator::TransformDefinition(SourceLocation Loc, Decl *D) {
John McCall76d824f2009-08-25 22:02:44 +0000942 Decl *Inst = getSema().SubstDecl(D, getSema().CurContext, TemplateArgs);
Douglas Gregorebe10102009-08-20 07:17:43 +0000943 if (!Inst)
Craig Topperc3ec1492014-05-26 06:22:03 +0000944 return nullptr;
Mike Stump11289f42009-09-09 15:08:12 +0000945
Douglas Gregorebe10102009-08-20 07:17:43 +0000946 getSema().CurrentInstantiationScope->InstantiatedLocal(D, Inst);
947 return Inst;
948}
949
Douglas Gregora5cb6da2009-10-20 05:58:46 +0000950NamedDecl *
951TemplateInstantiator::TransformFirstQualifierInScope(NamedDecl *D,
952 SourceLocation Loc) {
953 // If the first part of the nested-name-specifier was a template type
954 // parameter, instantiate that type parameter down to a tag type.
955 if (TemplateTypeParmDecl *TTPD = dyn_cast_or_null<TemplateTypeParmDecl>(D)) {
956 const TemplateTypeParmType *TTP
957 = cast<TemplateTypeParmType>(getSema().Context.getTypeDeclType(TTPD));
Douglas Gregor53c3f4e2010-12-20 22:48:17 +0000958
Douglas Gregora5cb6da2009-10-20 05:58:46 +0000959 if (TTP->getDepth() < TemplateArgs.getNumLevels()) {
Douglas Gregor53c3f4e2010-12-20 22:48:17 +0000960 // FIXME: This needs testing w/ member access expressions.
961 TemplateArgument Arg = TemplateArgs(TTP->getDepth(), TTP->getIndex());
962
963 if (TTP->isParameterPack()) {
964 assert(Arg.getKind() == TemplateArgument::Pack &&
965 "Missing argument pack");
966
Douglas Gregore1d60df2011-01-14 23:41:42 +0000967 if (getSema().ArgumentPackSubstitutionIndex == -1)
Craig Topperc3ec1492014-05-26 06:22:03 +0000968 return nullptr;
969
Eli Friedman8917ad52013-07-19 19:40:38 +0000970 Arg = getPackSubstitutedTemplateArgument(getSema(), Arg);
Douglas Gregor53c3f4e2010-12-20 22:48:17 +0000971 }
972
973 QualType T = Arg.getAsType();
Douglas Gregora5cb6da2009-10-20 05:58:46 +0000974 if (T.isNull())
Douglas Gregora04f2ca2010-03-01 15:56:25 +0000975 return cast_or_null<NamedDecl>(TransformDecl(Loc, D));
Douglas Gregora5cb6da2009-10-20 05:58:46 +0000976
977 if (const TagType *Tag = T->getAs<TagType>())
978 return Tag->getDecl();
979
980 // The resulting type is not a tag; complain.
981 getSema().Diag(Loc, diag::err_nested_name_spec_non_tag) << T;
Craig Topperc3ec1492014-05-26 06:22:03 +0000982 return nullptr;
Douglas Gregora5cb6da2009-10-20 05:58:46 +0000983 }
984 }
985
Douglas Gregora04f2ca2010-03-01 15:56:25 +0000986 return cast_or_null<NamedDecl>(TransformDecl(Loc, D));
Douglas Gregora5cb6da2009-10-20 05:58:46 +0000987}
988
Douglas Gregorebe10102009-08-20 07:17:43 +0000989VarDecl *
990TemplateInstantiator::RebuildExceptionDecl(VarDecl *ExceptionDecl,
John McCallbcd03502009-12-07 02:54:59 +0000991 TypeSourceInfo *Declarator,
Abramo Bagnaradff19302011-03-08 08:55:46 +0000992 SourceLocation StartLoc,
993 SourceLocation NameLoc,
994 IdentifierInfo *Name) {
Douglas Gregor9f0e1aa2010-09-09 17:09:21 +0000995 VarDecl *Var = inherited::RebuildExceptionDecl(ExceptionDecl, Declarator,
Abramo Bagnaradff19302011-03-08 08:55:46 +0000996 StartLoc, NameLoc, Name);
Douglas Gregorf4e837f2010-04-26 17:57:08 +0000997 if (Var)
998 getSema().CurrentInstantiationScope->InstantiatedLocal(ExceptionDecl, Var);
999 return Var;
1000}
1001
1002VarDecl *TemplateInstantiator::RebuildObjCExceptionDecl(VarDecl *ExceptionDecl,
1003 TypeSourceInfo *TSInfo,
1004 QualType T) {
1005 VarDecl *Var = inherited::RebuildObjCExceptionDecl(ExceptionDecl, TSInfo, T);
1006 if (Var)
Douglas Gregorebe10102009-08-20 07:17:43 +00001007 getSema().CurrentInstantiationScope->InstantiatedLocal(ExceptionDecl, Var);
1008 return Var;
1009}
1010
John McCall7f41d982009-09-11 04:59:25 +00001011QualType
John McCall954b5de2010-11-04 19:04:38 +00001012TemplateInstantiator::RebuildElaboratedType(SourceLocation KeywordLoc,
1013 ElaboratedTypeKeyword Keyword,
Douglas Gregor844cb502011-03-01 18:12:44 +00001014 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara6150c882010-05-11 21:36:43 +00001015 QualType T) {
John McCall7f41d982009-09-11 04:59:25 +00001016 if (const TagType *TT = T->getAs<TagType>()) {
1017 TagDecl* TD = TT->getDecl();
1018
John McCall954b5de2010-11-04 19:04:38 +00001019 SourceLocation TagLocation = KeywordLoc;
John McCall7f41d982009-09-11 04:59:25 +00001020
John McCall7f41d982009-09-11 04:59:25 +00001021 IdentifierInfo *Id = TD->getIdentifier();
1022
1023 // TODO: should we even warn on struct/class mismatches for this? Seems
1024 // like it's likely to produce a lot of spurious errors.
Richard Smith80b3c5a2012-08-17 00:12:27 +00001025 if (Id && Keyword != ETK_None && Keyword != ETK_Typename) {
Abramo Bagnara6150c882010-05-11 21:36:43 +00001026 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForKeyword(Keyword);
Richard Trieucaa33d32011-06-10 03:11:26 +00001027 if (!SemaRef.isAcceptableTagRedeclaration(TD, Kind, /*isDefinition*/false,
Justin Bognerc6ecb7c2015-07-10 23:05:47 +00001028 TagLocation, Id)) {
Abramo Bagnara6150c882010-05-11 21:36:43 +00001029 SemaRef.Diag(TagLocation, diag::err_use_with_wrong_tag)
1030 << Id
1031 << FixItHint::CreateReplacement(SourceRange(TagLocation),
1032 TD->getKindName());
1033 SemaRef.Diag(TD->getLocation(), diag::note_previous_use);
1034 }
John McCall7f41d982009-09-11 04:59:25 +00001035 }
1036 }
1037
John McCall954b5de2010-11-04 19:04:38 +00001038 return TreeTransform<TemplateInstantiator>::RebuildElaboratedType(KeywordLoc,
1039 Keyword,
Douglas Gregor844cb502011-03-01 18:12:44 +00001040 QualifierLoc,
1041 T);
John McCall7f41d982009-09-11 04:59:25 +00001042}
1043
Richard Smithfd3dae02017-01-20 00:20:39 +00001044TemplateName TemplateInstantiator::TransformTemplateName(
1045 CXXScopeSpec &SS, TemplateName Name, SourceLocation NameLoc,
1046 QualType ObjectType, NamedDecl *FirstQualifierInScope,
1047 bool AllowInjectedClassName) {
Douglas Gregor9db53502011-03-02 18:07:45 +00001048 if (TemplateTemplateParmDecl *TTP
1049 = dyn_cast_or_null<TemplateTemplateParmDecl>(Name.getAsTemplateDecl())) {
1050 if (TTP->getDepth() < TemplateArgs.getNumLevels()) {
1051 // If the corresponding template argument is NULL or non-existent, it's
1052 // because we are performing instantiation from explicitly-specified
1053 // template arguments in a function template, but there were some
1054 // arguments left unspecified.
1055 if (!TemplateArgs.hasTemplateArgument(TTP->getDepth(),
1056 TTP->getPosition()))
1057 return Name;
1058
1059 TemplateArgument Arg = TemplateArgs(TTP->getDepth(), TTP->getPosition());
1060
1061 if (TTP->isParameterPack()) {
1062 assert(Arg.getKind() == TemplateArgument::Pack &&
1063 "Missing argument pack");
1064
1065 if (getSema().ArgumentPackSubstitutionIndex == -1) {
1066 // We have the template argument pack to substitute, but we're not
1067 // actually expanding the enclosing pack expansion yet. So, just
1068 // keep the entire argument pack.
1069 return getSema().Context.getSubstTemplateTemplateParmPack(TTP, Arg);
1070 }
Eli Friedman8917ad52013-07-19 19:40:38 +00001071
1072 Arg = getPackSubstitutedTemplateArgument(getSema(), Arg);
Douglas Gregor9db53502011-03-02 18:07:45 +00001073 }
1074
1075 TemplateName Template = Arg.getAsTemplate();
Richard Smith3f1b5d02011-05-05 21:57:07 +00001076 assert(!Template.isNull() && "Null template template argument");
John McCalld9dfe3a2011-06-30 08:33:18 +00001077
Douglas Gregor9d9f8db2011-03-05 20:06:51 +00001078 // We don't ever want to substitute for a qualified template name, since
1079 // the qualifier is handled separately. So, look through the qualified
1080 // template name to its underlying declaration.
1081 if (QualifiedTemplateName *QTN = Template.getAsQualifiedTemplateName())
1082 Template = TemplateName(QTN->getTemplateDecl());
John McCalld9dfe3a2011-06-30 08:33:18 +00001083
1084 Template = getSema().Context.getSubstTemplateTemplateParm(TTP, Template);
Douglas Gregor9db53502011-03-02 18:07:45 +00001085 return Template;
1086 }
1087 }
1088
1089 if (SubstTemplateTemplateParmPackStorage *SubstPack
1090 = Name.getAsSubstTemplateTemplateParmPack()) {
1091 if (getSema().ArgumentPackSubstitutionIndex == -1)
1092 return Name;
1093
Eli Friedman8917ad52013-07-19 19:40:38 +00001094 TemplateArgument Arg = SubstPack->getArgumentPack();
1095 Arg = getPackSubstitutedTemplateArgument(getSema(), Arg);
1096 return Arg.getAsTemplate();
Douglas Gregor9db53502011-03-02 18:07:45 +00001097 }
Richard Smithfd3dae02017-01-20 00:20:39 +00001098
1099 return inherited::TransformTemplateName(SS, Name, NameLoc, ObjectType,
1100 FirstQualifierInScope,
1101 AllowInjectedClassName);
Douglas Gregor9db53502011-03-02 18:07:45 +00001102}
1103
John McCalldadc5752010-08-24 06:29:42 +00001104ExprResult
John McCall47f29ea2009-12-08 09:21:05 +00001105TemplateInstantiator::TransformPredefinedExpr(PredefinedExpr *E) {
Anders Carlsson0b209a82009-09-11 01:22:35 +00001106 if (!E->isTypeDependent())
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00001107 return E;
Anders Carlsson0b209a82009-09-11 01:22:35 +00001108
Wei Panc354d212013-09-16 13:57:27 +00001109 return getSema().BuildPredefinedExpr(E->getLocation(), E->getIdentType());
Anders Carlsson0b209a82009-09-11 01:22:35 +00001110}
1111
John McCalldadc5752010-08-24 06:29:42 +00001112ExprResult
John McCall13481c52010-02-06 08:42:39 +00001113TemplateInstantiator::TransformTemplateParmRefExpr(DeclRefExpr *E,
Douglas Gregor6c379e22010-02-08 23:41:45 +00001114 NonTypeTemplateParmDecl *NTTP) {
John McCall13481c52010-02-06 08:42:39 +00001115 // If the corresponding template argument is NULL or non-existent, it's
1116 // because we are performing instantiation from explicitly-specified
1117 // template arguments in a function template, but there were some
1118 // arguments left unspecified.
1119 if (!TemplateArgs.hasTemplateArgument(NTTP->getDepth(),
1120 NTTP->getPosition()))
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00001121 return E;
Mike Stump11289f42009-09-09 15:08:12 +00001122
Douglas Gregoreb5a39d2010-12-24 00:15:10 +00001123 TemplateArgument Arg = TemplateArgs(NTTP->getDepth(), NTTP->getPosition());
1124 if (NTTP->isParameterPack()) {
1125 assert(Arg.getKind() == TemplateArgument::Pack &&
1126 "Missing argument pack");
1127
1128 if (getSema().ArgumentPackSubstitutionIndex == -1) {
Douglas Gregorcdbc5392011-01-15 01:15:58 +00001129 // We have an argument pack, but we can't select a particular argument
1130 // out of it yet. Therefore, we'll build an expression to hold on to that
1131 // argument pack.
1132 QualType TargetType = SemaRef.SubstType(NTTP->getType(), TemplateArgs,
1133 E->getLocation(),
1134 NTTP->getDeclName());
1135 if (TargetType.isNull())
1136 return ExprError();
1137
1138 return new (SemaRef.Context) SubstNonTypeTemplateParmPackExpr(TargetType,
1139 NTTP,
1140 E->getLocation(),
1141 Arg);
Douglas Gregoreb5a39d2010-12-24 00:15:10 +00001142 }
1143
Eli Friedman8917ad52013-07-19 19:40:38 +00001144 Arg = getPackSubstitutedTemplateArgument(getSema(), Arg);
Douglas Gregoreb5a39d2010-12-24 00:15:10 +00001145 }
Mike Stump11289f42009-09-09 15:08:12 +00001146
John McCall7c454bb2011-07-15 05:09:51 +00001147 return transformNonTypeTemplateParmRef(NTTP, E->getLocation(), Arg);
1148}
1149
Tyler Nowickic724a83e2014-10-12 20:46:07 +00001150const LoopHintAttr *
1151TemplateInstantiator::TransformLoopHintAttr(const LoopHintAttr *LH) {
1152 Expr *TransformedExpr = getDerived().TransformExpr(LH->getValue()).get();
1153
1154 if (TransformedExpr == LH->getValue())
1155 return LH;
1156
1157 // Generate error if there is a problem with the value.
1158 if (getSema().CheckLoopHintExpr(TransformedExpr, LH->getLocation()))
1159 return LH;
1160
1161 // Create new LoopHintValueAttr with integral expression in place of the
1162 // non-type template parameter.
1163 return LoopHintAttr::CreateImplicit(
1164 getSema().Context, LH->getSemanticSpelling(), LH->getOption(),
1165 LH->getState(), TransformedExpr, LH->getRange());
1166}
1167
John McCall7c454bb2011-07-15 05:09:51 +00001168ExprResult TemplateInstantiator::transformNonTypeTemplateParmRef(
1169 NonTypeTemplateParmDecl *parm,
1170 SourceLocation loc,
Richard Smith34349002012-07-09 03:07:20 +00001171 TemplateArgument arg) {
John McCall7c454bb2011-07-15 05:09:51 +00001172 ExprResult result;
1173 QualType type;
1174
John McCall13481c52010-02-06 08:42:39 +00001175 // The template argument itself might be an expression, in which
1176 // case we just return that expression.
John McCall7c454bb2011-07-15 05:09:51 +00001177 if (arg.getKind() == TemplateArgument::Expression) {
1178 Expr *argExpr = arg.getAsExpr();
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00001179 result = argExpr;
John McCall7c454bb2011-07-15 05:09:51 +00001180 type = argExpr->getType();
Mike Stump11289f42009-09-09 15:08:12 +00001181
Eli Friedmanb826a002012-09-26 02:36:12 +00001182 } else if (arg.getKind() == TemplateArgument::Declaration ||
1183 arg.getKind() == TemplateArgument::NullPtr) {
Douglas Gregor31f55dc2012-04-06 22:40:38 +00001184 ValueDecl *VD;
Eli Friedmanb826a002012-09-26 02:36:12 +00001185 if (arg.getKind() == TemplateArgument::Declaration) {
1186 VD = cast<ValueDecl>(arg.getAsDecl());
Mike Stump11289f42009-09-09 15:08:12 +00001187
Douglas Gregor31f55dc2012-04-06 22:40:38 +00001188 // Find the instantiation of the template argument. This is
1189 // required for nested templates.
1190 VD = cast_or_null<ValueDecl>(
1191 getSema().FindInstantiatedDecl(loc, VD, TemplateArgs));
1192 if (!VD)
1193 return ExprError();
1194 } else {
1195 // Propagate NULL template argument.
Craig Topperc3ec1492014-05-26 06:22:03 +00001196 VD = nullptr;
Douglas Gregor31f55dc2012-04-06 22:40:38 +00001197 }
1198
John McCall15dda372010-02-06 10:23:53 +00001199 // Derive the type we want the substituted decl to have. This had
1200 // better be non-dependent, or these checks will have serious problems.
John McCall7c454bb2011-07-15 05:09:51 +00001201 if (parm->isExpandedParameterPack()) {
1202 type = parm->getExpansionType(SemaRef.ArgumentPackSubstitutionIndex);
1203 } else if (parm->isParameterPack() &&
1204 isa<PackExpansionType>(parm->getType())) {
1205 type = SemaRef.SubstType(
1206 cast<PackExpansionType>(parm->getType())->getPattern(),
1207 TemplateArgs, loc, parm->getDeclName());
1208 } else {
Richard Smith5f274382016-09-28 23:55:27 +00001209 type = SemaRef.SubstType(VD ? arg.getParamTypeForDecl() : arg.getNullPtrType(),
1210 TemplateArgs, loc, parm->getDeclName());
John McCall7c454bb2011-07-15 05:09:51 +00001211 }
1212 assert(!type.isNull() && "type substitution failed for param type");
1213 assert(!type->isDependentType() && "param type still dependent");
1214 result = SemaRef.BuildExpressionFromDeclTemplateArgument(arg, type, loc);
John McCall13481c52010-02-06 08:42:39 +00001215
John McCall7c454bb2011-07-15 05:09:51 +00001216 if (!result.isInvalid()) type = result.get()->getType();
1217 } else {
1218 result = SemaRef.BuildExpressionFromIntegralTemplateArgument(arg, loc);
1219
1220 // Note that this type can be different from the type of 'result',
1221 // e.g. if it's an enum type.
1222 type = arg.getIntegralType();
1223 }
1224 if (result.isInvalid()) return ExprError();
1225
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001226 Expr *resultExpr = result.get();
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00001227 return new (SemaRef.Context) SubstNonTypeTemplateParmExpr(
1228 type, resultExpr->getValueKind(), loc, parm, resultExpr);
John McCall13481c52010-02-06 08:42:39 +00001229}
1230
Douglas Gregorcdbc5392011-01-15 01:15:58 +00001231ExprResult
1232TemplateInstantiator::TransformSubstNonTypeTemplateParmPackExpr(
1233 SubstNonTypeTemplateParmPackExpr *E) {
1234 if (getSema().ArgumentPackSubstitutionIndex == -1) {
1235 // We aren't expanding the parameter pack, so just return ourselves.
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00001236 return E;
Douglas Gregorcdbc5392011-01-15 01:15:58 +00001237 }
Eli Friedman8917ad52013-07-19 19:40:38 +00001238
1239 TemplateArgument Arg = E->getArgumentPack();
1240 Arg = getPackSubstitutedTemplateArgument(getSema(), Arg);
John McCall7c454bb2011-07-15 05:09:51 +00001241 return transformNonTypeTemplateParmRef(E->getParameterPack(),
1242 E->getParameterPackLocation(),
1243 Arg);
Douglas Gregorcdbc5392011-01-15 01:15:58 +00001244}
John McCall13481c52010-02-06 08:42:39 +00001245
John McCalldadc5752010-08-24 06:29:42 +00001246ExprResult
Richard Smithb15fe3a2012-09-12 00:56:43 +00001247TemplateInstantiator::RebuildParmVarDeclRefExpr(ParmVarDecl *PD,
1248 SourceLocation Loc) {
1249 DeclarationNameInfo NameInfo(PD->getDeclName(), Loc);
1250 return getSema().BuildDeclarationNameExpr(CXXScopeSpec(), NameInfo, PD);
1251}
1252
1253ExprResult
1254TemplateInstantiator::TransformFunctionParmPackExpr(FunctionParmPackExpr *E) {
1255 if (getSema().ArgumentPackSubstitutionIndex != -1) {
1256 // We can expand this parameter pack now.
1257 ParmVarDecl *D = E->getExpansion(getSema().ArgumentPackSubstitutionIndex);
1258 ValueDecl *VD = cast_or_null<ValueDecl>(TransformDecl(E->getExprLoc(), D));
1259 if (!VD)
1260 return ExprError();
1261 return RebuildParmVarDeclRefExpr(cast<ParmVarDecl>(VD), E->getExprLoc());
1262 }
1263
1264 QualType T = TransformType(E->getType());
1265 if (T.isNull())
1266 return ExprError();
1267
1268 // Transform each of the parameter expansions into the corresponding
1269 // parameters in the instantiation of the function decl.
James Y Knight48fefa32015-09-30 14:04:23 +00001270 SmallVector<ParmVarDecl *, 8> Parms;
Richard Smithb15fe3a2012-09-12 00:56:43 +00001271 Parms.reserve(E->getNumExpansions());
1272 for (FunctionParmPackExpr::iterator I = E->begin(), End = E->end();
1273 I != End; ++I) {
1274 ParmVarDecl *D =
1275 cast_or_null<ParmVarDecl>(TransformDecl(E->getExprLoc(), *I));
1276 if (!D)
1277 return ExprError();
1278 Parms.push_back(D);
1279 }
1280
1281 return FunctionParmPackExpr::Create(getSema().Context, T,
1282 E->getParameterPack(),
1283 E->getParameterPackLocation(), Parms);
1284}
1285
1286ExprResult
1287TemplateInstantiator::TransformFunctionParmPackRefExpr(DeclRefExpr *E,
1288 ParmVarDecl *PD) {
1289 typedef LocalInstantiationScope::DeclArgumentPack DeclArgumentPack;
1290 llvm::PointerUnion<Decl *, DeclArgumentPack *> *Found
1291 = getSema().CurrentInstantiationScope->findInstantiationOf(PD);
1292 assert(Found && "no instantiation for parameter pack");
1293
1294 Decl *TransformedDecl;
1295 if (DeclArgumentPack *Pack = Found->dyn_cast<DeclArgumentPack *>()) {
Nico Weberc153d242014-07-28 00:02:09 +00001296 // If this is a reference to a function parameter pack which we can
1297 // substitute but can't yet expand, build a FunctionParmPackExpr for it.
Richard Smithb15fe3a2012-09-12 00:56:43 +00001298 if (getSema().ArgumentPackSubstitutionIndex == -1) {
1299 QualType T = TransformType(E->getType());
1300 if (T.isNull())
1301 return ExprError();
1302 return FunctionParmPackExpr::Create(getSema().Context, T, PD,
1303 E->getExprLoc(), *Pack);
1304 }
1305
1306 TransformedDecl = (*Pack)[getSema().ArgumentPackSubstitutionIndex];
1307 } else {
1308 TransformedDecl = Found->get<Decl*>();
1309 }
1310
1311 // We have either an unexpanded pack or a specific expansion.
1312 return RebuildParmVarDeclRefExpr(cast<ParmVarDecl>(TransformedDecl),
1313 E->getExprLoc());
1314}
1315
1316ExprResult
John McCall13481c52010-02-06 08:42:39 +00001317TemplateInstantiator::TransformDeclRefExpr(DeclRefExpr *E) {
1318 NamedDecl *D = E->getDecl();
Richard Smithb15fe3a2012-09-12 00:56:43 +00001319
1320 // Handle references to non-type template parameters and non-type template
1321 // parameter packs.
John McCall13481c52010-02-06 08:42:39 +00001322 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(D)) {
1323 if (NTTP->getDepth() < TemplateArgs.getNumLevels())
1324 return TransformTemplateParmRefExpr(E, NTTP);
Douglas Gregor954de172009-10-31 17:21:17 +00001325
1326 // We have a non-type template parameter that isn't fully substituted;
1327 // FindInstantiatedDecl will find it in the local instantiation scope.
Douglas Gregora16548e2009-08-11 05:31:07 +00001328 }
Mike Stump11289f42009-09-09 15:08:12 +00001329
Richard Smithb15fe3a2012-09-12 00:56:43 +00001330 // Handle references to function parameter packs.
1331 if (ParmVarDecl *PD = dyn_cast<ParmVarDecl>(D))
1332 if (PD->isParameterPack())
1333 return TransformFunctionParmPackRefExpr(E, PD);
1334
John McCall47f29ea2009-12-08 09:21:05 +00001335 return TreeTransform<TemplateInstantiator>::TransformDeclRefExpr(E);
Douglas Gregora16548e2009-08-11 05:31:07 +00001336}
1337
John McCalldadc5752010-08-24 06:29:42 +00001338ExprResult TemplateInstantiator::TransformCXXDefaultArgExpr(
John McCall47f29ea2009-12-08 09:21:05 +00001339 CXXDefaultArgExpr *E) {
Sebastian Redl14236c82009-11-08 13:56:19 +00001340 assert(!cast<FunctionDecl>(E->getParam()->getDeclContext())->
1341 getDescribedFunctionTemplate() &&
1342 "Default arg expressions are never formed in dependent cases.");
Douglas Gregor033f6752009-12-23 23:03:06 +00001343 return SemaRef.BuildCXXDefaultArgExpr(E->getUsedLocation(),
1344 cast<FunctionDecl>(E->getParam()->getDeclContext()),
1345 E->getParam());
Sebastian Redl14236c82009-11-08 13:56:19 +00001346}
1347
Richard Smith2e321552014-11-12 02:00:47 +00001348template<typename Fn>
Douglas Gregor3024f072012-04-16 07:05:22 +00001349QualType TemplateInstantiator::TransformFunctionProtoType(TypeLocBuilder &TLB,
1350 FunctionProtoTypeLoc TL,
1351 CXXRecordDecl *ThisContext,
Richard Smith2e321552014-11-12 02:00:47 +00001352 unsigned ThisTypeQuals,
1353 Fn TransformExceptionSpec) {
Douglas Gregor3024f072012-04-16 07:05:22 +00001354 // We need a local instantiation scope for this function prototype.
1355 LocalInstantiationScope Scope(SemaRef, /*CombineWithOuterScope=*/true);
Richard Smith2e321552014-11-12 02:00:47 +00001356 return inherited::TransformFunctionProtoType(
1357 TLB, TL, ThisContext, ThisTypeQuals, TransformExceptionSpec);
Douglas Gregor3024f072012-04-16 07:05:22 +00001358}
1359
John McCall58f10c32010-03-11 09:03:00 +00001360ParmVarDecl *
Douglas Gregor715e4612011-01-14 22:40:04 +00001361TemplateInstantiator::TransformFunctionTypeParam(ParmVarDecl *OldParm,
John McCall8fb0d9d2011-05-01 22:35:37 +00001362 int indexAdjustment,
David Blaikie05785d12013-02-20 22:23:23 +00001363 Optional<unsigned> NumExpansions,
Douglas Gregor0dd22bc2012-01-25 16:15:54 +00001364 bool ExpectParameterPack) {
John McCall8fb0d9d2011-05-01 22:35:37 +00001365 return SemaRef.SubstParmVarDecl(OldParm, TemplateArgs, indexAdjustment,
Douglas Gregor0dd22bc2012-01-25 16:15:54 +00001366 NumExpansions, ExpectParameterPack);
John McCall58f10c32010-03-11 09:03:00 +00001367}
1368
Mike Stump11289f42009-09-09 15:08:12 +00001369QualType
John McCall550e0c22009-10-21 00:40:46 +00001370TemplateInstantiator::TransformTemplateTypeParmType(TypeLocBuilder &TLB,
John McCall31f82722010-11-12 08:19:04 +00001371 TemplateTypeParmTypeLoc TL) {
John McCall424cec92011-01-19 06:33:43 +00001372 const TemplateTypeParmType *T = TL.getTypePtr();
Douglas Gregor01afeef2009-08-28 20:31:08 +00001373 if (T->getDepth() < TemplateArgs.getNumLevels()) {
Douglas Gregorfe1e1102009-02-27 19:31:52 +00001374 // Replace the template type parameter with its corresponding
1375 // template argument.
Mike Stump11289f42009-09-09 15:08:12 +00001376
1377 // If the corresponding template argument is NULL or doesn't exist, it's
1378 // because we are performing instantiation from explicitly-specified
1379 // template arguments in a function template class, but there were some
Douglas Gregore3f1f352009-07-01 00:28:38 +00001380 // arguments left unspecified.
John McCall550e0c22009-10-21 00:40:46 +00001381 if (!TemplateArgs.hasTemplateArgument(T->getDepth(), T->getIndex())) {
1382 TemplateTypeParmTypeLoc NewTL
1383 = TLB.push<TemplateTypeParmTypeLoc>(TL.getType());
1384 NewTL.setNameLoc(TL.getNameLoc());
1385 return TL.getType();
1386 }
Mike Stump11289f42009-09-09 15:08:12 +00001387
Douglas Gregor840bd6c2010-12-20 22:05:00 +00001388 TemplateArgument Arg = TemplateArgs(T->getDepth(), T->getIndex());
1389
1390 if (T->isParameterPack()) {
1391 assert(Arg.getKind() == TemplateArgument::Pack &&
1392 "Missing argument pack");
1393
1394 if (getSema().ArgumentPackSubstitutionIndex == -1) {
Douglas Gregorada4b792011-01-14 02:55:32 +00001395 // We have the template argument pack, but we're not expanding the
1396 // enclosing pack expansion yet. Just save the template argument
1397 // pack for later substitution.
1398 QualType Result
1399 = getSema().Context.getSubstTemplateTypeParmPackType(T, Arg);
1400 SubstTemplateTypeParmPackTypeLoc NewTL
1401 = TLB.push<SubstTemplateTypeParmPackTypeLoc>(Result);
1402 NewTL.setNameLoc(TL.getNameLoc());
1403 return Result;
Douglas Gregor840bd6c2010-12-20 22:05:00 +00001404 }
1405
Eli Friedman8917ad52013-07-19 19:40:38 +00001406 Arg = getPackSubstitutedTemplateArgument(getSema(), Arg);
Douglas Gregor840bd6c2010-12-20 22:05:00 +00001407 }
1408
1409 assert(Arg.getKind() == TemplateArgument::Type &&
Douglas Gregorfe1e1102009-02-27 19:31:52 +00001410 "Template argument kind mismatch");
Douglas Gregor01afeef2009-08-28 20:31:08 +00001411
Douglas Gregor840bd6c2010-12-20 22:05:00 +00001412 QualType Replacement = Arg.getAsType();
John McCallcebee162009-10-18 09:09:24 +00001413
1414 // TODO: only do this uniquing once, at the start of instantiation.
John McCall550e0c22009-10-21 00:40:46 +00001415 QualType Result
1416 = getSema().Context.getSubstTemplateTypeParmType(T, Replacement);
1417 SubstTemplateTypeParmTypeLoc NewTL
1418 = TLB.push<SubstTemplateTypeParmTypeLoc>(Result);
1419 NewTL.setNameLoc(TL.getNameLoc());
1420 return Result;
Mike Stump11289f42009-09-09 15:08:12 +00001421 }
Douglas Gregorfe1e1102009-02-27 19:31:52 +00001422
1423 // The template type parameter comes from an inner template (e.g.,
1424 // the template parameter list of a member template inside the
1425 // template we are instantiating). Create a new template type
1426 // parameter with the template "level" reduced by one.
Craig Topperc3ec1492014-05-26 06:22:03 +00001427 TemplateTypeParmDecl *NewTTPDecl = nullptr;
Chandler Carruth08836322011-05-01 00:51:33 +00001428 if (TemplateTypeParmDecl *OldTTPDecl = T->getDecl())
1429 NewTTPDecl = cast_or_null<TemplateTypeParmDecl>(
1430 TransformDecl(TL.getNameLoc(), OldTTPDecl));
1431
John McCall550e0c22009-10-21 00:40:46 +00001432 QualType Result
1433 = getSema().Context.getTemplateTypeParmType(T->getDepth()
1434 - TemplateArgs.getNumLevels(),
1435 T->getIndex(),
1436 T->isParameterPack(),
Chandler Carruth08836322011-05-01 00:51:33 +00001437 NewTTPDecl);
John McCall550e0c22009-10-21 00:40:46 +00001438 TemplateTypeParmTypeLoc NewTL = TLB.push<TemplateTypeParmTypeLoc>(Result);
1439 NewTL.setNameLoc(TL.getNameLoc());
1440 return Result;
Douglas Gregor17c0d7b2009-02-28 00:25:32 +00001441}
Douglas Gregorfe1e1102009-02-27 19:31:52 +00001442
Douglas Gregorada4b792011-01-14 02:55:32 +00001443QualType
1444TemplateInstantiator::TransformSubstTemplateTypeParmPackType(
1445 TypeLocBuilder &TLB,
1446 SubstTemplateTypeParmPackTypeLoc TL) {
1447 if (getSema().ArgumentPackSubstitutionIndex == -1) {
1448 // We aren't expanding the parameter pack, so just return ourselves.
1449 SubstTemplateTypeParmPackTypeLoc NewTL
1450 = TLB.push<SubstTemplateTypeParmPackTypeLoc>(TL.getType());
1451 NewTL.setNameLoc(TL.getNameLoc());
1452 return TL.getType();
1453 }
Eli Friedman8917ad52013-07-19 19:40:38 +00001454
1455 TemplateArgument Arg = TL.getTypePtr()->getArgumentPack();
1456 Arg = getPackSubstitutedTemplateArgument(getSema(), Arg);
1457 QualType Result = Arg.getAsType();
1458
Douglas Gregorada4b792011-01-14 02:55:32 +00001459 Result = getSema().Context.getSubstTemplateTypeParmType(
1460 TL.getTypePtr()->getReplacedParameter(),
1461 Result);
1462 SubstTemplateTypeParmTypeLoc NewTL
1463 = TLB.push<SubstTemplateTypeParmTypeLoc>(Result);
1464 NewTL.setNameLoc(TL.getNameLoc());
1465 return Result;
1466}
1467
John McCall76d824f2009-08-25 22:02:44 +00001468/// \brief Perform substitution on the type T with a given set of template
1469/// arguments.
Douglas Gregorfe1e1102009-02-27 19:31:52 +00001470///
1471/// This routine substitutes the given template arguments into the
1472/// type T and produces the instantiated type.
1473///
1474/// \param T the type into which the template arguments will be
1475/// substituted. If this type is not dependent, it will be returned
1476/// immediately.
1477///
James Dennett634962f2012-06-14 21:40:34 +00001478/// \param Args the template arguments that will be
Douglas Gregorfe1e1102009-02-27 19:31:52 +00001479/// substituted for the top-level template parameters within T.
1480///
Douglas Gregorfe1e1102009-02-27 19:31:52 +00001481/// \param Loc the location in the source code where this substitution
1482/// is being performed. It will typically be the location of the
1483/// declarator (if we're instantiating the type of some declaration)
1484/// or the location of the type in the source code (if, e.g., we're
1485/// instantiating the type of a cast expression).
1486///
1487/// \param Entity the name of the entity associated with a declaration
1488/// being instantiated (if any). May be empty to indicate that there
1489/// is no such entity (if, e.g., this is a type that occurs as part of
1490/// a cast expression) or that the entity has no name (e.g., an
1491/// unnamed function parameter).
1492///
1493/// \returns If the instantiation succeeds, the instantiated
1494/// type. Otherwise, produces diagnostics and returns a NULL type.
John McCallbcd03502009-12-07 02:54:59 +00001495TypeSourceInfo *Sema::SubstType(TypeSourceInfo *T,
John McCall609459e2009-10-21 00:58:09 +00001496 const MultiLevelTemplateArgumentList &Args,
1497 SourceLocation Loc,
1498 DeclarationName Entity) {
1499 assert(!ActiveTemplateInstantiations.empty() &&
1500 "Cannot perform an instantiation without some context on the "
1501 "instantiation stack");
1502
Douglas Gregor678d76c2011-07-01 01:22:09 +00001503 if (!T->getType()->isInstantiationDependentType() &&
Douglas Gregor5a5073e2010-05-24 17:22:01 +00001504 !T->getType()->isVariablyModifiedType())
John McCall609459e2009-10-21 00:58:09 +00001505 return T;
1506
1507 TemplateInstantiator Instantiator(*this, Args, Loc, Entity);
1508 return Instantiator.TransformType(T);
1509}
1510
Douglas Gregor5499af42011-01-05 23:12:31 +00001511TypeSourceInfo *Sema::SubstType(TypeLoc TL,
1512 const MultiLevelTemplateArgumentList &Args,
1513 SourceLocation Loc,
1514 DeclarationName Entity) {
1515 assert(!ActiveTemplateInstantiations.empty() &&
1516 "Cannot perform an instantiation without some context on the "
1517 "instantiation stack");
1518
1519 if (TL.getType().isNull())
Craig Topperc3ec1492014-05-26 06:22:03 +00001520 return nullptr;
Douglas Gregor5499af42011-01-05 23:12:31 +00001521
Douglas Gregor678d76c2011-07-01 01:22:09 +00001522 if (!TL.getType()->isInstantiationDependentType() &&
Douglas Gregor5499af42011-01-05 23:12:31 +00001523 !TL.getType()->isVariablyModifiedType()) {
1524 // FIXME: Make a copy of the TypeLoc data here, so that we can
1525 // return a new TypeSourceInfo. Inefficient!
1526 TypeLocBuilder TLB;
1527 TLB.pushFullCopy(TL);
1528 return TLB.getTypeSourceInfo(Context, TL.getType());
1529 }
1530
1531 TemplateInstantiator Instantiator(*this, Args, Loc, Entity);
1532 TypeLocBuilder TLB;
1533 TLB.reserve(TL.getFullDataSize());
1534 QualType Result = Instantiator.TransformType(TLB, TL);
1535 if (Result.isNull())
Craig Topperc3ec1492014-05-26 06:22:03 +00001536 return nullptr;
Douglas Gregor5499af42011-01-05 23:12:31 +00001537
1538 return TLB.getTypeSourceInfo(Context, Result);
1539}
1540
John McCall609459e2009-10-21 00:58:09 +00001541/// Deprecated form of the above.
Mike Stump11289f42009-09-09 15:08:12 +00001542QualType Sema::SubstType(QualType T,
Douglas Gregor01afeef2009-08-28 20:31:08 +00001543 const MultiLevelTemplateArgumentList &TemplateArgs,
John McCall76d824f2009-08-25 22:02:44 +00001544 SourceLocation Loc, DeclarationName Entity) {
Douglas Gregor79cf6032009-03-10 20:44:00 +00001545 assert(!ActiveTemplateInstantiations.empty() &&
1546 "Cannot perform an instantiation without some context on the "
1547 "instantiation stack");
1548
Douglas Gregor5a5073e2010-05-24 17:22:01 +00001549 // If T is not a dependent type or a variably-modified type, there
1550 // is nothing to do.
Douglas Gregor678d76c2011-07-01 01:22:09 +00001551 if (!T->isInstantiationDependentType() && !T->isVariablyModifiedType())
Douglas Gregorfe1e1102009-02-27 19:31:52 +00001552 return T;
1553
Douglas Gregord6ff3322009-08-04 16:50:30 +00001554 TemplateInstantiator Instantiator(*this, TemplateArgs, Loc, Entity);
1555 return Instantiator.TransformType(T);
Douglas Gregorfe1e1102009-02-27 19:31:52 +00001556}
Douglas Gregor463421d2009-03-03 04:44:36 +00001557
John McCallb29f78f2010-04-09 17:38:44 +00001558static bool NeedsInstantiationAsFunctionType(TypeSourceInfo *T) {
Nico Weberc0973372016-02-01 22:31:51 +00001559 if (T->getType()->isInstantiationDependentType() ||
Douglas Gregor678d76c2011-07-01 01:22:09 +00001560 T->getType()->isVariablyModifiedType())
John McCallb29f78f2010-04-09 17:38:44 +00001561 return true;
1562
Abramo Bagnara6d810632010-12-14 22:11:44 +00001563 TypeLoc TL = T->getTypeLoc().IgnoreParens();
David Blaikie6adc78e2013-02-18 22:06:02 +00001564 if (!TL.getAs<FunctionProtoTypeLoc>())
John McCallb29f78f2010-04-09 17:38:44 +00001565 return false;
1566
David Blaikie6adc78e2013-02-18 22:06:02 +00001567 FunctionProtoTypeLoc FP = TL.castAs<FunctionProtoTypeLoc>();
Nico Weberc0973372016-02-01 22:31:51 +00001568 for (ParmVarDecl *P : FP.getParams()) {
Reid Klecknera09e44c2013-07-31 21:00:18 +00001569 // This must be synthesized from a typedef.
1570 if (!P) continue;
1571
Nico Weberc0973372016-02-01 22:31:51 +00001572 // If there are any parameters, a new TypeSourceInfo that refers to the
1573 // instantiated parameters must be built.
1574 return true;
John McCallb29f78f2010-04-09 17:38:44 +00001575 }
1576
1577 return false;
1578}
1579
1580/// A form of SubstType intended specifically for instantiating the
1581/// type of a FunctionDecl. Its purpose is solely to force the
Richard Smith2e321552014-11-12 02:00:47 +00001582/// instantiation of default-argument expressions and to avoid
1583/// instantiating an exception-specification.
John McCallb29f78f2010-04-09 17:38:44 +00001584TypeSourceInfo *Sema::SubstFunctionDeclType(TypeSourceInfo *T,
1585 const MultiLevelTemplateArgumentList &Args,
1586 SourceLocation Loc,
Douglas Gregor3024f072012-04-16 07:05:22 +00001587 DeclarationName Entity,
1588 CXXRecordDecl *ThisContext,
1589 unsigned ThisTypeQuals) {
John McCallb29f78f2010-04-09 17:38:44 +00001590 assert(!ActiveTemplateInstantiations.empty() &&
1591 "Cannot perform an instantiation without some context on the "
1592 "instantiation stack");
Nico Weberc0973372016-02-01 22:31:51 +00001593
John McCallb29f78f2010-04-09 17:38:44 +00001594 if (!NeedsInstantiationAsFunctionType(T))
1595 return T;
1596
1597 TemplateInstantiator Instantiator(*this, Args, Loc, Entity);
1598
1599 TypeLocBuilder TLB;
1600
1601 TypeLoc TL = T->getTypeLoc();
1602 TLB.reserve(TL.getFullDataSize());
1603
Douglas Gregor3024f072012-04-16 07:05:22 +00001604 QualType Result;
David Blaikie6adc78e2013-02-18 22:06:02 +00001605
Richard Smith2e321552014-11-12 02:00:47 +00001606 if (FunctionProtoTypeLoc Proto =
1607 TL.IgnoreParens().getAs<FunctionProtoTypeLoc>()) {
1608 // Instantiate the type, other than its exception specification. The
1609 // exception specification is instantiated in InitFunctionInstantiation
1610 // once we've built the FunctionDecl.
1611 // FIXME: Set the exception specification to EST_Uninstantiated here,
1612 // instead of rebuilding the function type again later.
1613 Result = Instantiator.TransformFunctionProtoType(
1614 TLB, Proto, ThisContext, ThisTypeQuals,
1615 [](FunctionProtoType::ExceptionSpecInfo &ESI,
1616 bool &Changed) { return false; });
Douglas Gregor3024f072012-04-16 07:05:22 +00001617 } else {
1618 Result = Instantiator.TransformType(TLB, TL);
1619 }
John McCallb29f78f2010-04-09 17:38:44 +00001620 if (Result.isNull())
Craig Topperc3ec1492014-05-26 06:22:03 +00001621 return nullptr;
John McCallb29f78f2010-04-09 17:38:44 +00001622
1623 return TLB.getTypeSourceInfo(Context, Result);
1624}
1625
Richard Smith2e321552014-11-12 02:00:47 +00001626void Sema::SubstExceptionSpec(FunctionDecl *New, const FunctionProtoType *Proto,
1627 const MultiLevelTemplateArgumentList &Args) {
1628 FunctionProtoType::ExceptionSpecInfo ESI =
1629 Proto->getExtProtoInfo().ExceptionSpec;
1630 assert(ESI.Type != EST_Uninstantiated);
1631
1632 TemplateInstantiator Instantiator(*this, Args, New->getLocation(),
1633 New->getDeclName());
1634
1635 SmallVector<QualType, 4> ExceptionStorage;
1636 bool Changed = false;
1637 if (Instantiator.TransformExceptionSpec(
1638 New->getTypeSourceInfo()->getTypeLoc().getLocEnd(), ESI,
1639 ExceptionStorage, Changed))
1640 // On error, recover by dropping the exception specification.
1641 ESI.Type = EST_None;
1642
1643 UpdateExceptionSpec(New, ESI);
1644}
1645
Douglas Gregor940bca72010-04-12 07:48:19 +00001646ParmVarDecl *Sema::SubstParmVarDecl(ParmVarDecl *OldParm,
Douglas Gregor715e4612011-01-14 22:40:04 +00001647 const MultiLevelTemplateArgumentList &TemplateArgs,
John McCall8fb0d9d2011-05-01 22:35:37 +00001648 int indexAdjustment,
David Blaikie05785d12013-02-20 22:23:23 +00001649 Optional<unsigned> NumExpansions,
Douglas Gregor0dd22bc2012-01-25 16:15:54 +00001650 bool ExpectParameterPack) {
Douglas Gregor940bca72010-04-12 07:48:19 +00001651 TypeSourceInfo *OldDI = OldParm->getTypeSourceInfo();
Craig Topperc3ec1492014-05-26 06:22:03 +00001652 TypeSourceInfo *NewDI = nullptr;
1653
Douglas Gregor5499af42011-01-05 23:12:31 +00001654 TypeLoc OldTL = OldDI->getTypeLoc();
David Blaikie6adc78e2013-02-18 22:06:02 +00001655 if (PackExpansionTypeLoc ExpansionTL = OldTL.getAs<PackExpansionTypeLoc>()) {
1656
Douglas Gregor5499af42011-01-05 23:12:31 +00001657 // We have a function parameter pack. Substitute into the pattern of the
1658 // expansion.
1659 NewDI = SubstType(ExpansionTL.getPatternLoc(), TemplateArgs,
1660 OldParm->getLocation(), OldParm->getDeclName());
1661 if (!NewDI)
Craig Topperc3ec1492014-05-26 06:22:03 +00001662 return nullptr;
1663
Douglas Gregor5499af42011-01-05 23:12:31 +00001664 if (NewDI->getType()->containsUnexpandedParameterPack()) {
1665 // We still have unexpanded parameter packs, which means that
1666 // our function parameter is still a function parameter pack.
1667 // Therefore, make its type a pack expansion type.
Douglas Gregor0dca5fd2011-01-14 17:04:44 +00001668 NewDI = CheckPackExpansion(NewDI, ExpansionTL.getEllipsisLoc(),
Douglas Gregor715e4612011-01-14 22:40:04 +00001669 NumExpansions);
Douglas Gregor0dd22bc2012-01-25 16:15:54 +00001670 } else if (ExpectParameterPack) {
1671 // We expected to get a parameter pack but didn't (because the type
1672 // itself is not a pack expansion type), so complain. This can occur when
1673 // the substitution goes through an alias template that "loses" the
1674 // pack expansion.
1675 Diag(OldParm->getLocation(),
1676 diag::err_function_parameter_pack_without_parameter_packs)
1677 << NewDI->getType();
Craig Topperc3ec1492014-05-26 06:22:03 +00001678 return nullptr;
Douglas Gregor0dd22bc2012-01-25 16:15:54 +00001679 }
Douglas Gregor5499af42011-01-05 23:12:31 +00001680 } else {
1681 NewDI = SubstType(OldDI, TemplateArgs, OldParm->getLocation(),
1682 OldParm->getDeclName());
1683 }
1684
Douglas Gregor940bca72010-04-12 07:48:19 +00001685 if (!NewDI)
Craig Topperc3ec1492014-05-26 06:22:03 +00001686 return nullptr;
Douglas Gregor940bca72010-04-12 07:48:19 +00001687
1688 if (NewDI->getType()->isVoidType()) {
1689 Diag(OldParm->getLocation(), diag::err_param_with_void_type);
Craig Topperc3ec1492014-05-26 06:22:03 +00001690 return nullptr;
Douglas Gregor940bca72010-04-12 07:48:19 +00001691 }
1692
1693 ParmVarDecl *NewParm = CheckParameter(Context.getTranslationUnitDecl(),
Abramo Bagnaradff19302011-03-08 08:55:46 +00001694 OldParm->getInnerLocStart(),
Douglas Gregor940bca72010-04-12 07:48:19 +00001695 OldParm->getLocation(),
Abramo Bagnaradff19302011-03-08 08:55:46 +00001696 OldParm->getIdentifier(),
1697 NewDI->getType(), NewDI,
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001698 OldParm->getStorageClass());
Douglas Gregor940bca72010-04-12 07:48:19 +00001699 if (!NewParm)
Craig Topperc3ec1492014-05-26 06:22:03 +00001700 return nullptr;
1701
Douglas Gregor940bca72010-04-12 07:48:19 +00001702 // Mark the (new) default argument as uninstantiated (if any).
1703 if (OldParm->hasUninstantiatedDefaultArg()) {
1704 Expr *Arg = OldParm->getUninstantiatedDefaultArg();
1705 NewParm->setUninstantiatedDefaultArg(Arg);
Douglas Gregor758cb672010-10-12 18:23:32 +00001706 } else if (OldParm->hasUnparsedDefaultArg()) {
1707 NewParm->setUnparsedDefaultArg();
1708 UnparsedDefaultArgInstantiations[OldParm].push_back(NewParm);
Serge Pavlov3739f5e72015-06-29 17:50:19 +00001709 } else if (Expr *Arg = OldParm->getDefaultArg()) {
1710 FunctionDecl *OwningFunc = cast<FunctionDecl>(OldParm->getDeclContext());
Serge Pavlov73c6a242015-08-23 10:22:28 +00001711 if (OwningFunc->isLexicallyWithinFunctionOrMethod()) {
1712 // Instantiate default arguments for methods of local classes (DR1484)
1713 // and non-defining declarations.
1714 Sema::ContextRAII SavedContext(*this, OwningFunc);
Akira Hatanakad644e022016-12-16 03:19:41 +00001715 LocalInstantiationScope Local(*this, true);
Serge Pavlov3739f5e72015-06-29 17:50:19 +00001716 ExprResult NewArg = SubstExpr(Arg, TemplateArgs);
John McCalldc40b612015-12-11 01:56:36 +00001717 if (NewArg.isUsable()) {
1718 // It would be nice if we still had this.
1719 SourceLocation EqualLoc = NewArg.get()->getLocStart();
1720 SetParamDefaultArgument(NewParm, NewArg.get(), EqualLoc);
1721 }
Serge Pavlov3739f5e72015-06-29 17:50:19 +00001722 } else {
1723 // FIXME: if we non-lazily instantiated non-dependent default args for
1724 // non-dependent parameter types we could remove a bunch of duplicate
1725 // conversion warnings for such arguments.
1726 NewParm->setUninstantiatedDefaultArg(Arg);
1727 }
1728 }
Douglas Gregor940bca72010-04-12 07:48:19 +00001729
1730 NewParm->setHasInheritedDefaultArg(OldParm->hasInheritedDefaultArg());
Douglas Gregor0dd22bc2012-01-25 16:15:54 +00001731
Douglas Gregorf3010112011-01-07 16:43:16 +00001732 if (OldParm->isParameterPack() && !NewParm->isParameterPack()) {
Richard Smith928be492012-01-25 02:14:59 +00001733 // Add the new parameter to the instantiated parameter pack.
Douglas Gregorf3010112011-01-07 16:43:16 +00001734 CurrentInstantiationScope->InstantiatedLocalPackArg(OldParm, NewParm);
1735 } else {
1736 // Introduce an Old -> New mapping
Douglas Gregor5499af42011-01-05 23:12:31 +00001737 CurrentInstantiationScope->InstantiatedLocal(OldParm, NewParm);
Douglas Gregorf3010112011-01-07 16:43:16 +00001738 }
Douglas Gregor5499af42011-01-05 23:12:31 +00001739
Argyrios Kyrtzidis3816ed42010-07-19 10:14:41 +00001740 // FIXME: OldParm may come from a FunctionProtoType, in which case CurContext
1741 // can be anything, is this right ?
Fariborz Jahanian714447b2010-07-13 21:05:02 +00001742 NewParm->setDeclContext(CurContext);
John McCall8fb0d9d2011-05-01 22:35:37 +00001743
1744 NewParm->setScopeInfo(OldParm->getFunctionScopeDepth(),
1745 OldParm->getFunctionScopeIndex() + indexAdjustment);
Jordan Rosea0a86be2013-03-08 22:25:36 +00001746
1747 InstantiateAttrs(TemplateArgs, OldParm, NewParm);
1748
Douglas Gregor940bca72010-04-12 07:48:19 +00001749 return NewParm;
1750}
1751
Douglas Gregordd472162011-01-07 00:20:55 +00001752/// \brief Substitute the given template arguments into the given set of
1753/// parameters, producing the set of parameter types that would be generated
1754/// from such a substitution.
David Majnemer59f77922016-06-24 04:05:48 +00001755bool Sema::SubstParmTypes(
1756 SourceLocation Loc, ArrayRef<ParmVarDecl *> Params,
1757 const FunctionProtoType::ExtParameterInfo *ExtParamInfos,
1758 const MultiLevelTemplateArgumentList &TemplateArgs,
1759 SmallVectorImpl<QualType> &ParamTypes,
1760 SmallVectorImpl<ParmVarDecl *> *OutParams,
1761 ExtParameterInfoBuilder &ParamInfos) {
Douglas Gregordd472162011-01-07 00:20:55 +00001762 assert(!ActiveTemplateInstantiations.empty() &&
1763 "Cannot perform an instantiation without some context on the "
1764 "instantiation stack");
1765
1766 TemplateInstantiator Instantiator(*this, TemplateArgs, Loc,
1767 DeclarationName());
David Majnemer59f77922016-06-24 04:05:48 +00001768 return Instantiator.TransformFunctionTypeParams(
1769 Loc, Params, nullptr, ExtParamInfos, ParamTypes, OutParams, ParamInfos);
Douglas Gregordd472162011-01-07 00:20:55 +00001770}
1771
John McCall76d824f2009-08-25 22:02:44 +00001772/// \brief Perform substitution on the base class specifiers of the
1773/// given class template specialization.
Douglas Gregor463421d2009-03-03 04:44:36 +00001774///
1775/// Produces a diagnostic and returns true on error, returns false and
1776/// attaches the instantiated base classes to the class template
1777/// specialization if successful.
Mike Stump11289f42009-09-09 15:08:12 +00001778bool
John McCall76d824f2009-08-25 22:02:44 +00001779Sema::SubstBaseSpecifiers(CXXRecordDecl *Instantiation,
1780 CXXRecordDecl *Pattern,
Douglas Gregor01afeef2009-08-28 20:31:08 +00001781 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor463421d2009-03-03 04:44:36 +00001782 bool Invalid = false;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001783 SmallVector<CXXBaseSpecifier*, 4> InstantiatedBases;
Richard Trieub5841332015-04-15 01:21:42 +00001784 for (const auto &Base : Pattern->bases()) {
Aaron Ballman574705e2014-03-13 15:41:46 +00001785 if (!Base.getType()->isDependentType()) {
1786 if (const CXXRecordDecl *RD = Base.getType()->getAsCXXRecordDecl()) {
Matt Beaumont-Gay76d0b462013-06-21 18:58:32 +00001787 if (RD->isInvalidDecl())
1788 Instantiation->setInvalidDecl();
1789 }
Aaron Ballman574705e2014-03-13 15:41:46 +00001790 InstantiatedBases.push_back(new (Context) CXXBaseSpecifier(Base));
Douglas Gregor463421d2009-03-03 04:44:36 +00001791 continue;
1792 }
1793
Douglas Gregor752a5952011-01-03 22:36:02 +00001794 SourceLocation EllipsisLoc;
Douglas Gregorc52264e2011-03-02 02:04:06 +00001795 TypeSourceInfo *BaseTypeLoc;
Aaron Ballman574705e2014-03-13 15:41:46 +00001796 if (Base.isPackExpansion()) {
Douglas Gregor752a5952011-01-03 22:36:02 +00001797 // This is a pack expansion. See whether we should expand it now, or
1798 // wait until later.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001799 SmallVector<UnexpandedParameterPack, 2> Unexpanded;
Aaron Ballman574705e2014-03-13 15:41:46 +00001800 collectUnexpandedParameterPacks(Base.getTypeSourceInfo()->getTypeLoc(),
Douglas Gregor752a5952011-01-03 22:36:02 +00001801 Unexpanded);
1802 bool ShouldExpand = false;
Douglas Gregora8bac7f2011-01-10 07:32:04 +00001803 bool RetainExpansion = false;
David Blaikie05785d12013-02-20 22:23:23 +00001804 Optional<unsigned> NumExpansions;
Aaron Ballman574705e2014-03-13 15:41:46 +00001805 if (CheckParameterPacksForExpansion(Base.getEllipsisLoc(),
1806 Base.getSourceRange(),
David Blaikieb9c168a2011-09-22 02:34:54 +00001807 Unexpanded,
Douglas Gregor752a5952011-01-03 22:36:02 +00001808 TemplateArgs, ShouldExpand,
Douglas Gregora8bac7f2011-01-10 07:32:04 +00001809 RetainExpansion,
Douglas Gregor752a5952011-01-03 22:36:02 +00001810 NumExpansions)) {
Douglas Gregor752a5952011-01-03 22:36:02 +00001811 Invalid = true;
Douglas Gregor44e7df62011-01-04 00:32:56 +00001812 continue;
Douglas Gregor752a5952011-01-03 22:36:02 +00001813 }
1814
1815 // If we should expand this pack expansion now, do so.
1816 if (ShouldExpand) {
Douglas Gregor0dca5fd2011-01-14 17:04:44 +00001817 for (unsigned I = 0; I != *NumExpansions; ++I) {
Douglas Gregor752a5952011-01-03 22:36:02 +00001818 Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(*this, I);
1819
Aaron Ballman574705e2014-03-13 15:41:46 +00001820 TypeSourceInfo *BaseTypeLoc = SubstType(Base.getTypeSourceInfo(),
Douglas Gregor752a5952011-01-03 22:36:02 +00001821 TemplateArgs,
Aaron Ballman574705e2014-03-13 15:41:46 +00001822 Base.getSourceRange().getBegin(),
Douglas Gregor752a5952011-01-03 22:36:02 +00001823 DeclarationName());
1824 if (!BaseTypeLoc) {
1825 Invalid = true;
1826 continue;
1827 }
1828
1829 if (CXXBaseSpecifier *InstantiatedBase
1830 = CheckBaseSpecifier(Instantiation,
Aaron Ballman574705e2014-03-13 15:41:46 +00001831 Base.getSourceRange(),
1832 Base.isVirtual(),
1833 Base.getAccessSpecifierAsWritten(),
Douglas Gregor752a5952011-01-03 22:36:02 +00001834 BaseTypeLoc,
1835 SourceLocation()))
1836 InstantiatedBases.push_back(InstantiatedBase);
1837 else
1838 Invalid = true;
1839 }
1840
1841 continue;
1842 }
1843
1844 // The resulting base specifier will (still) be a pack expansion.
Aaron Ballman574705e2014-03-13 15:41:46 +00001845 EllipsisLoc = Base.getEllipsisLoc();
Douglas Gregorc52264e2011-03-02 02:04:06 +00001846 Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(*this, -1);
Aaron Ballman574705e2014-03-13 15:41:46 +00001847 BaseTypeLoc = SubstType(Base.getTypeSourceInfo(),
Douglas Gregorc52264e2011-03-02 02:04:06 +00001848 TemplateArgs,
Aaron Ballman574705e2014-03-13 15:41:46 +00001849 Base.getSourceRange().getBegin(),
Douglas Gregorc52264e2011-03-02 02:04:06 +00001850 DeclarationName());
1851 } else {
Aaron Ballman574705e2014-03-13 15:41:46 +00001852 BaseTypeLoc = SubstType(Base.getTypeSourceInfo(),
Douglas Gregorc52264e2011-03-02 02:04:06 +00001853 TemplateArgs,
Aaron Ballman574705e2014-03-13 15:41:46 +00001854 Base.getSourceRange().getBegin(),
Douglas Gregorc52264e2011-03-02 02:04:06 +00001855 DeclarationName());
Douglas Gregor752a5952011-01-03 22:36:02 +00001856 }
1857
Nick Lewycky19b9f952010-07-26 16:56:01 +00001858 if (!BaseTypeLoc) {
Douglas Gregor463421d2009-03-03 04:44:36 +00001859 Invalid = true;
1860 continue;
1861 }
1862
1863 if (CXXBaseSpecifier *InstantiatedBase
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001864 = CheckBaseSpecifier(Instantiation,
Aaron Ballman574705e2014-03-13 15:41:46 +00001865 Base.getSourceRange(),
1866 Base.isVirtual(),
1867 Base.getAccessSpecifierAsWritten(),
Douglas Gregor752a5952011-01-03 22:36:02 +00001868 BaseTypeLoc,
1869 EllipsisLoc))
Douglas Gregor463421d2009-03-03 04:44:36 +00001870 InstantiatedBases.push_back(InstantiatedBase);
1871 else
1872 Invalid = true;
1873 }
1874
Craig Topperaa700cb2015-12-27 21:55:19 +00001875 if (!Invalid && AttachBaseSpecifiers(Instantiation, InstantiatedBases))
Douglas Gregor463421d2009-03-03 04:44:36 +00001876 Invalid = true;
1877
1878 return Invalid;
1879}
1880
DeLesley Hutchins30398dd2012-01-20 22:50:54 +00001881// Defined via #include from SemaTemplateInstantiateDecl.cpp
Benjamin Kramerbf8da9d2012-02-06 11:13:08 +00001882namespace clang {
1883 namespace sema {
1884 Attr *instantiateTemplateAttribute(const Attr *At, ASTContext &C, Sema &S,
1885 const MultiLevelTemplateArgumentList &TemplateArgs);
1886 }
1887}
DeLesley Hutchins30398dd2012-01-20 22:50:54 +00001888
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001889/// \brief Instantiate the definition of a class from a given pattern.
1890///
1891/// \param PointOfInstantiation The point of instantiation within the
1892/// source code.
1893///
1894/// \param Instantiation is the declaration whose definition is being
1895/// instantiated. This will be either a class template specialization
1896/// or a member class of a class template specialization.
1897///
1898/// \param Pattern is the pattern from which the instantiation
1899/// occurs. This will be either the declaration of a class template or
1900/// the declaration of a member class of a class template.
1901///
1902/// \param TemplateArgs The template arguments to be substituted into
1903/// the pattern.
1904///
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001905/// \param TSK the kind of implicit or explicit instantiation to perform.
Douglas Gregor8a2e6012009-08-24 15:23:48 +00001906///
1907/// \param Complain whether to complain if the class cannot be instantiated due
1908/// to the lack of a definition.
1909///
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001910/// \returns true if an error occurred, false otherwise.
1911bool
1912Sema::InstantiateClass(SourceLocation PointOfInstantiation,
1913 CXXRecordDecl *Instantiation, CXXRecordDecl *Pattern,
Douglas Gregor01afeef2009-08-28 20:31:08 +00001914 const MultiLevelTemplateArgumentList &TemplateArgs,
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001915 TemplateSpecializationKind TSK,
Douglas Gregor8a2e6012009-08-24 15:23:48 +00001916 bool Complain) {
Mike Stump11289f42009-09-09 15:08:12 +00001917 CXXRecordDecl *PatternDef
Douglas Gregor0a5a2212010-02-11 01:04:33 +00001918 = cast_or_null<CXXRecordDecl>(Pattern->getDefinition());
Vassil Vassilevb21ee082016-08-18 22:01:25 +00001919 if (DiagnoseUninstantiableTemplate(PointOfInstantiation, Instantiation,
Richard Smith4b38ded2012-03-14 23:13:10 +00001920 Instantiation->getInstantiatedFromMemberClass(),
1921 Pattern, PatternDef, TSK, Complain))
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001922 return true;
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001923 Pattern = PatternDef;
1924
Douglas Gregord6ba93d2009-10-15 15:54:05 +00001925 // \brief Record the point of instantiation.
1926 if (MemberSpecializationInfo *MSInfo
1927 = Instantiation->getMemberSpecializationInfo()) {
1928 MSInfo->setTemplateSpecializationKind(TSK);
1929 MSInfo->setPointOfInstantiation(PointOfInstantiation);
Douglas Gregoref6ab412009-10-27 06:26:26 +00001930 } else if (ClassTemplateSpecializationDecl *Spec
Nico Weber3ffc4c92011-12-20 20:32:49 +00001931 = dyn_cast<ClassTemplateSpecializationDecl>(Instantiation)) {
Douglas Gregoref6ab412009-10-27 06:26:26 +00001932 Spec->setTemplateSpecializationKind(TSK);
1933 Spec->setPointOfInstantiation(PointOfInstantiation);
Douglas Gregord6ba93d2009-10-15 15:54:05 +00001934 }
Richard Smitha1087602014-03-10 00:04:29 +00001935
Douglas Gregorf3430ae2009-03-25 21:23:52 +00001936 InstantiatingTemplate Inst(*this, PointOfInstantiation, Instantiation);
Alp Tokerd4a72d52013-10-08 08:09:04 +00001937 if (Inst.isInvalid())
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001938 return true;
Richard Smith54f18e82016-08-31 02:15:21 +00001939 assert(!Inst.isAlreadyInstantiating() && "should have been caught by caller");
Richard Smithe19b95d2016-05-26 20:23:13 +00001940 PrettyDeclStackTraceEntry CrashInfo(*this, Instantiation, SourceLocation(),
1941 "instantiating class definition");
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001942
1943 // Enter the scope of this instantiation. We don't use
1944 // PushDeclContext because we don't have a scope.
John McCall80e58cd2010-04-29 00:35:03 +00001945 ContextRAII SavedContext(*this, Instantiation);
Douglas Gregor17158422010-05-12 17:27:19 +00001946 EnterExpressionEvaluationContext EvalContext(*this,
John McCallfaf5fb42010-08-26 23:41:50 +00001947 Sema::PotentiallyEvaluated);
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001948
Douglas Gregor51121572010-03-24 01:33:17 +00001949 // If this is an instantiation of a local class, merge this local
1950 // instantiation scope with the enclosing scope. Otherwise, every
1951 // instantiation of a class has its own local instantiation scope.
1952 bool MergeWithParentScope = !Instantiation->isDefinedOutsideFunctionOrMethod();
John McCall19c1bfd2010-08-25 05:32:35 +00001953 LocalInstantiationScope Scope(*this, MergeWithParentScope);
Douglas Gregor51121572010-03-24 01:33:17 +00001954
Reid Kleckner5b640342016-02-26 19:51:02 +00001955 // All dllexported classes created during instantiation should be fully
1956 // emitted after instantiation completes. We may not be ready to emit any
1957 // delayed classes already on the stack, so save them away and put them back
1958 // later.
1959 decltype(DelayedDllExportClasses) ExportedClasses;
1960 std::swap(ExportedClasses, DelayedDllExportClasses);
1961
John McCall6602bb12010-08-01 02:01:53 +00001962 // Pull attributes from the pattern onto the instantiation.
1963 InstantiateAttrs(TemplateArgs, Pattern, Instantiation);
1964
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001965 // Start the definition of this instantiation.
1966 Instantiation->startDefinition();
Richard Smitha1087602014-03-10 00:04:29 +00001967
1968 // The instantiation is visible here, even if it was first declared in an
1969 // unimported module.
1970 Instantiation->setHidden(false);
1971
1972 // FIXME: This loses the as-written tag kind for an explicit instantiation.
Douglas Gregore9029562010-05-06 00:28:52 +00001973 Instantiation->setTagKind(Pattern->getTagKind());
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001974
John McCall76d824f2009-08-25 22:02:44 +00001975 // Do substitution on the base class specifiers.
1976 if (SubstBaseSpecifiers(Instantiation, Pattern, TemplateArgs))
Douglas Gregorb9b8b812012-07-02 21:00:41 +00001977 Instantiation->setInvalidDecl();
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001978
Douglas Gregor869853e2010-11-10 19:44:59 +00001979 TemplateDeclInstantiator Instantiator(*this, Instantiation, TemplateArgs);
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001980 SmallVector<Decl*, 4> Fields;
DeLesley Hutchins30398dd2012-01-20 22:50:54 +00001981 // Delay instantiation of late parsed attributes.
1982 LateInstantiatedAttrVec LateAttrs;
1983 Instantiator.enableLateAttributeInstantiation(&LateAttrs);
1984
Aaron Ballman629afae2014-03-07 19:56:05 +00001985 for (auto *Member : Pattern->decls()) {
Argyrios Kyrtzidis9a94d9b2010-11-04 03:18:57 +00001986 // Don't instantiate members not belonging in this semantic context.
1987 // e.g. for:
1988 // @code
1989 // template <int i> class A {
1990 // class B *g;
1991 // };
1992 // @endcode
1993 // 'class B' has the template as lexical context but semantically it is
1994 // introduced in namespace scope.
Aaron Ballman629afae2014-03-07 19:56:05 +00001995 if (Member->getDeclContext() != Pattern)
Argyrios Kyrtzidis9a94d9b2010-11-04 03:18:57 +00001996 continue;
1997
Aaron Ballman629afae2014-03-07 19:56:05 +00001998 if (Member->isInvalidDecl()) {
Richard Smithded9c2e2012-07-11 22:37:56 +00001999 Instantiation->setInvalidDecl();
Douglas Gregor869853e2010-11-10 19:44:59 +00002000 continue;
2001 }
2002
Aaron Ballman629afae2014-03-07 19:56:05 +00002003 Decl *NewMember = Instantiator.Visit(Member);
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00002004 if (NewMember) {
Richard Smith938f40b2011-06-11 17:19:42 +00002005 if (FieldDecl *Field = dyn_cast<FieldDecl>(NewMember)) {
John McCall48871652010-08-21 09:40:31 +00002006 Fields.push_back(Field);
Richard Smith7d137e32012-03-23 03:33:32 +00002007 } else if (EnumDecl *Enum = dyn_cast<EnumDecl>(NewMember)) {
2008 // C++11 [temp.inst]p1: The implicit instantiation of a class template
2009 // specialization causes the implicit instantiation of the definitions
2010 // of unscoped member enumerations.
2011 // Record a point of instantiation for this implicit instantiation.
Richard Smithb66d7772012-03-23 23:09:08 +00002012 if (TSK == TSK_ImplicitInstantiation && !Enum->isScoped() &&
2013 Enum->isCompleteDefinition()) {
Richard Smith7d137e32012-03-23 03:33:32 +00002014 MemberSpecializationInfo *MSInfo =Enum->getMemberSpecializationInfo();
2015 assert(MSInfo && "no spec info for member enum specialization");
2016 MSInfo->setTemplateSpecializationKind(TSK_ImplicitInstantiation);
2017 MSInfo->setPointOfInstantiation(PointOfInstantiation);
2018 }
Richard Smithded9c2e2012-07-11 22:37:56 +00002019 } else if (StaticAssertDecl *SA = dyn_cast<StaticAssertDecl>(NewMember)) {
2020 if (SA->isFailed()) {
2021 // A static_assert failed. Bail out; instantiating this
2022 // class is probably not meaningful.
2023 Instantiation->setInvalidDecl();
2024 break;
2025 }
Richard Smith7d137e32012-03-23 03:33:32 +00002026 }
2027
2028 if (NewMember->isInvalidDecl())
Douglas Gregorb9b8b812012-07-02 21:00:41 +00002029 Instantiation->setInvalidDecl();
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00002030 } else {
2031 // FIXME: Eventually, a NULL return will mean that one of the
Douglas Gregorb9b8b812012-07-02 21:00:41 +00002032 // instantiations was a semantic disaster, and we'll want to mark the
2033 // declaration invalid.
2034 // For now, we expect to skip some members that we can't yet handle.
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00002035 }
2036 }
2037
2038 // Finish checking fields.
Craig Topperc3ec1492014-05-26 06:22:03 +00002039 ActOnFields(nullptr, Instantiation->getLocation(), Instantiation, Fields,
2040 SourceLocation(), SourceLocation(), nullptr);
Douglas Gregor0be31a22010-07-02 17:43:08 +00002041 CheckCompletedCXXClass(Instantiation);
Richard Smith938f40b2011-06-11 17:19:42 +00002042
Reid Kleckner93f661a2015-03-17 21:51:43 +00002043 // Default arguments are parsed, if not instantiated. We can go instantiate
2044 // default arg exprs for default constructors if necessary now.
Hans Wennborg99000c22015-08-15 01:18:16 +00002045 ActOnFinishCXXNonNestedClass(Instantiation);
Reid Kleckner93f661a2015-03-17 21:51:43 +00002046
Reid Kleckner5b640342016-02-26 19:51:02 +00002047 // Put back the delayed exported classes that we moved out of the way.
2048 std::swap(ExportedClasses, DelayedDllExportClasses);
2049
DeLesley Hutchins30398dd2012-01-20 22:50:54 +00002050 // Instantiate late parsed attributes, and attach them to their decls.
2051 // See Sema::InstantiateAttrs
2052 for (LateInstantiatedAttrVec::iterator I = LateAttrs.begin(),
2053 E = LateAttrs.end(); I != E; ++I) {
2054 assert(CurrentInstantiationScope == Instantiator.getStartingScope());
2055 CurrentInstantiationScope = I->Scope;
Richard Smithc3d2ebb2013-06-07 02:33:37 +00002056
2057 // Allow 'this' within late-parsed attributes.
2058 NamedDecl *ND = dyn_cast<NamedDecl>(I->NewDecl);
2059 CXXRecordDecl *ThisContext =
2060 dyn_cast_or_null<CXXRecordDecl>(ND->getDeclContext());
2061 CXXThisScopeRAII ThisScope(*this, ThisContext, /*TypeQuals*/0,
2062 ND && ND->isCXXInstanceMember());
2063
DeLesley Hutchins30398dd2012-01-20 22:50:54 +00002064 Attr *NewAttr =
2065 instantiateTemplateAttribute(I->TmplAttr, Context, *this, TemplateArgs);
2066 I->NewDecl->addAttr(NewAttr);
2067 LocalInstantiationScope::deleteScopes(I->Scope,
2068 Instantiator.getStartingScope());
2069 }
2070 Instantiator.disableLateAttributeInstantiation();
2071 LateAttrs.clear();
2072
Richard Smithd3b5c9082012-07-27 04:22:15 +00002073 ActOnFinishDelayedMemberInitializers(Instantiation);
Richard Smith938f40b2011-06-11 17:19:42 +00002074
Richard Smitha1087602014-03-10 00:04:29 +00002075 // FIXME: We should do something similar for explicit instantiations so they
2076 // end up in the right module.
Abramo Bagnara12dcbf32011-11-18 08:08:52 +00002077 if (TSK == TSK_ImplicitInstantiation) {
Argyrios Kyrtzidise3789482012-02-11 01:59:57 +00002078 Instantiation->setLocation(Pattern->getLocation());
Abramo Bagnara12dcbf32011-11-18 08:08:52 +00002079 Instantiation->setLocStart(Pattern->getInnerLocStart());
Argyrios Kyrtzidisd798c052016-07-15 18:11:33 +00002080 Instantiation->setBraceRange(Pattern->getBraceRange());
Abramo Bagnara12dcbf32011-11-18 08:08:52 +00002081 }
Abramo Bagnarafd3a4552011-10-03 20:34:03 +00002082
Douglas Gregorb9b8b812012-07-02 21:00:41 +00002083 if (!Instantiation->isInvalidDecl()) {
John McCalla0a96892012-08-10 03:15:35 +00002084 // Perform any dependent diagnostics from the pattern.
2085 PerformDependentDiagnostics(Pattern, TemplateArgs);
2086
Douglas Gregor869853e2010-11-10 19:44:59 +00002087 // Instantiate any out-of-line class template partial
2088 // specializations now.
Richard Smith10b55fc2013-09-26 03:49:48 +00002089 for (TemplateDeclInstantiator::delayed_partial_spec_iterator
Douglas Gregor869853e2010-11-10 19:44:59 +00002090 P = Instantiator.delayed_partial_spec_begin(),
2091 PEnd = Instantiator.delayed_partial_spec_end();
2092 P != PEnd; ++P) {
2093 if (!Instantiator.InstantiateClassTemplatePartialSpecialization(
Richard Smith10b55fc2013-09-26 03:49:48 +00002094 P->first, P->second)) {
2095 Instantiation->setInvalidDecl();
2096 break;
2097 }
2098 }
2099
2100 // Instantiate any out-of-line variable template partial
2101 // specializations now.
2102 for (TemplateDeclInstantiator::delayed_var_partial_spec_iterator
2103 P = Instantiator.delayed_var_partial_spec_begin(),
2104 PEnd = Instantiator.delayed_var_partial_spec_end();
2105 P != PEnd; ++P) {
2106 if (!Instantiator.InstantiateVarTemplatePartialSpecialization(
2107 P->first, P->second)) {
Douglas Gregorb9b8b812012-07-02 21:00:41 +00002108 Instantiation->setInvalidDecl();
Douglas Gregor869853e2010-11-10 19:44:59 +00002109 break;
2110 }
2111 }
2112 }
2113
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00002114 // Exit the scope of this instantiation.
John McCall80e58cd2010-04-29 00:35:03 +00002115 SavedContext.pop();
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00002116
Douglas Gregorb9b8b812012-07-02 21:00:41 +00002117 if (!Instantiation->isInvalidDecl()) {
Douglas Gregor28ad4b52009-05-26 20:50:29 +00002118 Consumer.HandleTagDeclDefinition(Instantiation);
2119
Douglas Gregor88d292c2010-05-13 16:44:06 +00002120 // Always emit the vtable for an explicit instantiation definition
2121 // of a polymorphic class template specialization.
2122 if (TSK == TSK_ExplicitInstantiationDefinition)
2123 MarkVTableUsed(PointOfInstantiation, Instantiation, true);
2124 }
2125
Douglas Gregorb9b8b812012-07-02 21:00:41 +00002126 return Instantiation->isInvalidDecl();
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00002127}
2128
Richard Smith4b38ded2012-03-14 23:13:10 +00002129/// \brief Instantiate the definition of an enum from a given pattern.
2130///
2131/// \param PointOfInstantiation The point of instantiation within the
2132/// source code.
2133/// \param Instantiation is the declaration whose definition is being
2134/// instantiated. This will be a member enumeration of a class
2135/// temploid specialization, or a local enumeration within a
2136/// function temploid specialization.
2137/// \param Pattern The templated declaration from which the instantiation
2138/// occurs.
2139/// \param TemplateArgs The template arguments to be substituted into
2140/// the pattern.
2141/// \param TSK The kind of implicit or explicit instantiation to perform.
2142///
2143/// \return \c true if an error occurred, \c false otherwise.
2144bool Sema::InstantiateEnum(SourceLocation PointOfInstantiation,
2145 EnumDecl *Instantiation, EnumDecl *Pattern,
2146 const MultiLevelTemplateArgumentList &TemplateArgs,
2147 TemplateSpecializationKind TSK) {
2148 EnumDecl *PatternDef = Pattern->getDefinition();
Vassil Vassilevb21ee082016-08-18 22:01:25 +00002149 if (DiagnoseUninstantiableTemplate(PointOfInstantiation, Instantiation,
Richard Smith4b38ded2012-03-14 23:13:10 +00002150 Instantiation->getInstantiatedFromMemberEnum(),
2151 Pattern, PatternDef, TSK,/*Complain*/true))
2152 return true;
2153 Pattern = PatternDef;
2154
2155 // Record the point of instantiation.
2156 if (MemberSpecializationInfo *MSInfo
2157 = Instantiation->getMemberSpecializationInfo()) {
2158 MSInfo->setTemplateSpecializationKind(TSK);
2159 MSInfo->setPointOfInstantiation(PointOfInstantiation);
2160 }
2161
2162 InstantiatingTemplate Inst(*this, PointOfInstantiation, Instantiation);
Alp Tokerd4a72d52013-10-08 08:09:04 +00002163 if (Inst.isInvalid())
Richard Smith4b38ded2012-03-14 23:13:10 +00002164 return true;
Richard Smith54f18e82016-08-31 02:15:21 +00002165 if (Inst.isAlreadyInstantiating())
2166 return false;
Richard Smithe19b95d2016-05-26 20:23:13 +00002167 PrettyDeclStackTraceEntry CrashInfo(*this, Instantiation, SourceLocation(),
2168 "instantiating enum definition");
Richard Smith4b38ded2012-03-14 23:13:10 +00002169
Richard Smitha1087602014-03-10 00:04:29 +00002170 // The instantiation is visible here, even if it was first declared in an
2171 // unimported module.
2172 Instantiation->setHidden(false);
2173
Richard Smith4b38ded2012-03-14 23:13:10 +00002174 // Enter the scope of this instantiation. We don't use
2175 // PushDeclContext because we don't have a scope.
2176 ContextRAII SavedContext(*this, Instantiation);
2177 EnterExpressionEvaluationContext EvalContext(*this,
2178 Sema::PotentiallyEvaluated);
2179
2180 LocalInstantiationScope Scope(*this, /*MergeWithParentScope*/true);
2181
2182 // Pull attributes from the pattern onto the instantiation.
2183 InstantiateAttrs(TemplateArgs, Pattern, Instantiation);
2184
2185 TemplateDeclInstantiator Instantiator(*this, Instantiation, TemplateArgs);
2186 Instantiator.InstantiateEnumDefinition(Instantiation, Pattern);
2187
2188 // Exit the scope of this instantiation.
2189 SavedContext.pop();
2190
2191 return Instantiation->isInvalidDecl();
2192}
2193
Reid Klecknerd60b82f2014-11-17 23:36:45 +00002194
2195/// \brief Instantiate the definition of a field from the given pattern.
2196///
2197/// \param PointOfInstantiation The point of instantiation within the
2198/// source code.
2199/// \param Instantiation is the declaration whose definition is being
2200/// instantiated. This will be a class of a class temploid
2201/// specialization, or a local enumeration within a function temploid
2202/// specialization.
2203/// \param Pattern The templated declaration from which the instantiation
2204/// occurs.
2205/// \param TemplateArgs The template arguments to be substituted into
2206/// the pattern.
2207///
2208/// \return \c true if an error occurred, \c false otherwise.
2209bool Sema::InstantiateInClassInitializer(
2210 SourceLocation PointOfInstantiation, FieldDecl *Instantiation,
2211 FieldDecl *Pattern, const MultiLevelTemplateArgumentList &TemplateArgs) {
2212 // If there is no initializer, we don't need to do anything.
2213 if (!Pattern->hasInClassInitializer())
2214 return false;
2215
2216 assert(Instantiation->getInClassInitStyle() ==
2217 Pattern->getInClassInitStyle() &&
2218 "pattern and instantiation disagree about init style");
2219
2220 // Error out if we haven't parsed the initializer of the pattern yet because
2221 // we are waiting for the closing brace of the outer class.
2222 Expr *OldInit = Pattern->getInClassInitializer();
2223 if (!OldInit) {
2224 RecordDecl *PatternRD = Pattern->getParent();
2225 RecordDecl *OutermostClass = PatternRD->getOuterLexicalRecordContext();
Richard Smith8dbc6b22016-11-22 22:55:12 +00002226 Diag(PointOfInstantiation,
2227 diag::err_in_class_initializer_not_yet_parsed)
2228 << OutermostClass << Pattern;
2229 Diag(Pattern->getLocEnd(), diag::note_in_class_initializer_not_yet_parsed);
Reid Klecknerd60b82f2014-11-17 23:36:45 +00002230 Instantiation->setInvalidDecl();
2231 return true;
2232 }
2233
2234 InstantiatingTemplate Inst(*this, PointOfInstantiation, Instantiation);
2235 if (Inst.isInvalid())
2236 return true;
Richard Smith54f18e82016-08-31 02:15:21 +00002237 if (Inst.isAlreadyInstantiating()) {
2238 // Error out if we hit an instantiation cycle for this initializer.
2239 Diag(PointOfInstantiation, diag::err_in_class_initializer_cycle)
2240 << Instantiation;
2241 return true;
2242 }
Richard Smithe19b95d2016-05-26 20:23:13 +00002243 PrettyDeclStackTraceEntry CrashInfo(*this, Instantiation, SourceLocation(),
2244 "instantiating default member init");
Reid Klecknerd60b82f2014-11-17 23:36:45 +00002245
2246 // Enter the scope of this instantiation. We don't use PushDeclContext because
2247 // we don't have a scope.
2248 ContextRAII SavedContext(*this, Instantiation->getParent());
2249 EnterExpressionEvaluationContext EvalContext(*this,
2250 Sema::PotentiallyEvaluated);
2251
Serge Pavlov907233f2015-04-28 17:58:47 +00002252 LocalInstantiationScope Scope(*this, true);
Reid Klecknerd60b82f2014-11-17 23:36:45 +00002253
2254 // Instantiate the initializer.
2255 ActOnStartCXXInClassMemberInitializer();
2256 CXXThisScopeRAII ThisScope(*this, Instantiation->getParent(), /*TypeQuals=*/0);
2257
2258 ExprResult NewInit = SubstInitializer(OldInit, TemplateArgs,
2259 /*CXXDirectInit=*/false);
2260 Expr *Init = NewInit.get();
2261 assert((!Init || !isa<ParenListExpr>(Init)) && "call-style init in class");
2262 ActOnFinishCXXInClassMemberInitializer(
2263 Instantiation, Init ? Init->getLocStart() : SourceLocation(), Init);
2264
Richard Smith4b054b22016-08-24 21:25:37 +00002265 if (auto *L = getASTMutationListener())
2266 L->DefaultMemberInitializerInstantiated(Instantiation);
2267
Reid Klecknerd60b82f2014-11-17 23:36:45 +00002268 // Return true if the in-class initializer is still missing.
2269 return !Instantiation->getInClassInitializer();
2270}
2271
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +00002272namespace {
2273 /// \brief A partial specialization whose template arguments have matched
2274 /// a given template-id.
2275 struct PartialSpecMatchResult {
2276 ClassTemplatePartialSpecializationDecl *Partial;
2277 TemplateArgumentList *Args;
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +00002278 };
2279}
2280
Richard Smith32b43762017-01-08 22:45:21 +00002281/// Get the instantiation pattern to use to instantiate the definition of a
2282/// given ClassTemplateSpecializationDecl (either the pattern of the primary
2283/// template or of a partial specialization).
2284static CXXRecordDecl *
2285getPatternForClassTemplateSpecialization(
2286 Sema &S, SourceLocation PointOfInstantiation,
Larisse Voufo72caf2b2013-08-22 00:59:14 +00002287 ClassTemplateSpecializationDecl *ClassTemplateSpec,
2288 TemplateSpecializationKind TSK, bool Complain) {
Richard Smith32b43762017-01-08 22:45:21 +00002289 Sema::InstantiatingTemplate Inst(S, PointOfInstantiation, ClassTemplateSpec);
2290 if (Inst.isInvalid() || Inst.isAlreadyInstantiating())
2291 return nullptr;
2292
Douglas Gregor463421d2009-03-03 04:44:36 +00002293 ClassTemplateDecl *Template = ClassTemplateSpec->getSpecializedTemplate();
Craig Topperc3ec1492014-05-26 06:22:03 +00002294 CXXRecordDecl *Pattern = nullptr;
Douglas Gregor2373c592009-05-31 09:31:02 +00002295
Douglas Gregor170bc422009-06-12 22:31:52 +00002296 // C++ [temp.class.spec.match]p1:
2297 // When a class template is used in a context that requires an
2298 // instantiation of the class, it is necessary to determine
2299 // whether the instantiation is to be generated using the primary
2300 // template or one of the partial specializations. This is done by
2301 // matching the template arguments of the class template
2302 // specialization with the template argument lists of the partial
2303 // specializations.
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +00002304 typedef PartialSpecMatchResult MatchResult;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002305 SmallVector<MatchResult, 4> Matched;
2306 SmallVector<ClassTemplatePartialSpecializationDecl *, 4> PartialSpecs;
Douglas Gregor407e9612010-04-30 05:56:50 +00002307 Template->getPartialSpecializations(PartialSpecs);
Larisse Voufo98b20f12013-07-19 23:00:19 +00002308 TemplateSpecCandidateSet FailedCandidates(PointOfInstantiation);
Douglas Gregor407e9612010-04-30 05:56:50 +00002309 for (unsigned I = 0, N = PartialSpecs.size(); I != N; ++I) {
2310 ClassTemplatePartialSpecializationDecl *Partial = PartialSpecs[I];
Larisse Voufo98b20f12013-07-19 23:00:19 +00002311 TemplateDeductionInfo Info(FailedCandidates.getLocation());
Richard Smith32b43762017-01-08 22:45:21 +00002312 if (Sema::TemplateDeductionResult Result = S.DeduceTemplateArguments(
2313 Partial, ClassTemplateSpec->getTemplateArgs(), Info)) {
Larisse Voufo98b20f12013-07-19 23:00:19 +00002314 // Store the failed-deduction information for use in diagnostics, later.
2315 // TODO: Actually use the failed-deduction info?
Richard Smithc2bebe92016-05-11 20:37:46 +00002316 FailedCandidates.addCandidate().set(
2317 DeclAccessPair::make(Template, AS_public), Partial,
Richard Smith32b43762017-01-08 22:45:21 +00002318 MakeDeductionFailureInfo(S.Context, Result, Info));
Douglas Gregor181aa4a2009-06-12 18:26:56 +00002319 (void)Result;
2320 } else {
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +00002321 Matched.push_back(PartialSpecMatchResult());
2322 Matched.back().Partial = Partial;
2323 Matched.back().Args = Info.take();
Douglas Gregor181aa4a2009-06-12 18:26:56 +00002324 }
Douglas Gregor2373c592009-05-31 09:31:02 +00002325 }
2326
Douglas Gregor0231d8d2011-01-19 20:10:05 +00002327 // If we're dealing with a member template where the template parameters
2328 // have been instantiated, this provides the original template parameters
2329 // from which the member template's parameters were instantiated.
Alp Toker965f8822013-11-27 05:22:15 +00002330
Douglas Gregor21610382009-10-29 00:04:11 +00002331 if (Matched.size() >= 1) {
Craig Topper2341c0d2013-07-04 03:08:24 +00002332 SmallVectorImpl<MatchResult>::iterator Best = Matched.begin();
Douglas Gregor21610382009-10-29 00:04:11 +00002333 if (Matched.size() == 1) {
2334 // -- If exactly one matching specialization is found, the
2335 // instantiation is generated from that specialization.
2336 // We don't need to do anything for this.
2337 } else {
2338 // -- If more than one matching specialization is found, the
2339 // partial order rules (14.5.4.2) are used to determine
2340 // whether one of the specializations is more specialized
2341 // than the others. If none of the specializations is more
2342 // specialized than all of the other matching
2343 // specializations, then the use of the class template is
2344 // ambiguous and the program is ill-formed.
Craig Topper2341c0d2013-07-04 03:08:24 +00002345 for (SmallVectorImpl<MatchResult>::iterator P = Best + 1,
2346 PEnd = Matched.end();
Douglas Gregor21610382009-10-29 00:04:11 +00002347 P != PEnd; ++P) {
Richard Smith32b43762017-01-08 22:45:21 +00002348 if (S.getMoreSpecializedPartialSpecialization(
2349 P->Partial, Best->Partial, PointOfInstantiation) == P->Partial)
Douglas Gregor21610382009-10-29 00:04:11 +00002350 Best = P;
Douglas Gregorbe999392009-09-15 16:23:51 +00002351 }
Douglas Gregorbe999392009-09-15 16:23:51 +00002352
Douglas Gregor21610382009-10-29 00:04:11 +00002353 // Determine if the best partial specialization is more specialized than
2354 // the others.
2355 bool Ambiguous = false;
Craig Topper2341c0d2013-07-04 03:08:24 +00002356 for (SmallVectorImpl<MatchResult>::iterator P = Matched.begin(),
2357 PEnd = Matched.end();
Douglas Gregor21610382009-10-29 00:04:11 +00002358 P != PEnd; ++P) {
2359 if (P != Best &&
Richard Smith32b43762017-01-08 22:45:21 +00002360 S.getMoreSpecializedPartialSpecialization(P->Partial, Best->Partial,
2361 PointOfInstantiation) !=
2362 Best->Partial) {
Douglas Gregor21610382009-10-29 00:04:11 +00002363 Ambiguous = true;
2364 break;
2365 }
2366 }
2367
2368 if (Ambiguous) {
2369 // Partial ordering did not produce a clear winner. Complain.
Richard Smith32b43762017-01-08 22:45:21 +00002370 Inst.Clear();
Douglas Gregor21610382009-10-29 00:04:11 +00002371 ClassTemplateSpec->setInvalidDecl();
Richard Smith32b43762017-01-08 22:45:21 +00002372 S.Diag(PointOfInstantiation, diag::err_partial_spec_ordering_ambiguous)
Douglas Gregor21610382009-10-29 00:04:11 +00002373 << ClassTemplateSpec;
2374
2375 // Print the matching partial specializations.
Craig Topper2341c0d2013-07-04 03:08:24 +00002376 for (SmallVectorImpl<MatchResult>::iterator P = Matched.begin(),
2377 PEnd = Matched.end();
Douglas Gregor21610382009-10-29 00:04:11 +00002378 P != PEnd; ++P)
Richard Smith32b43762017-01-08 22:45:21 +00002379 S.Diag(P->Partial->getLocation(), diag::note_partial_spec_match)
2380 << S.getTemplateArgumentBindingsText(
2381 P->Partial->getTemplateParameters(), *P->Args);
Douglas Gregor01afeef2009-08-28 20:31:08 +00002382
Richard Smith32b43762017-01-08 22:45:21 +00002383 return nullptr;
Douglas Gregor21610382009-10-29 00:04:11 +00002384 }
Douglas Gregorbe999392009-09-15 16:23:51 +00002385 }
2386
2387 // Instantiate using the best class template partial specialization.
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +00002388 ClassTemplatePartialSpecializationDecl *OrigPartialSpec = Best->Partial;
Douglas Gregor21610382009-10-29 00:04:11 +00002389 while (OrigPartialSpec->getInstantiatedFromMember()) {
2390 // If we've found an explicit specialization of this class template,
2391 // stop here and use that as the pattern.
2392 if (OrigPartialSpec->isMemberSpecialization())
2393 break;
2394
2395 OrigPartialSpec = OrigPartialSpec->getInstantiatedFromMember();
2396 }
2397
2398 Pattern = OrigPartialSpec;
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +00002399 ClassTemplateSpec->setInstantiationOf(Best->Partial, Best->Args);
Douglas Gregor170bc422009-06-12 22:31:52 +00002400 } else {
2401 // -- If no matches are found, the instantiation is generated
2402 // from the primary template.
Douglas Gregor01afeef2009-08-28 20:31:08 +00002403 ClassTemplateDecl *OrigTemplate = Template;
Douglas Gregorcf915552009-10-13 16:30:37 +00002404 while (OrigTemplate->getInstantiatedFromMemberTemplate()) {
2405 // If we've found an explicit specialization of this class template,
2406 // stop here and use that as the pattern.
2407 if (OrigTemplate->isMemberSpecialization())
2408 break;
2409
Douglas Gregor01afeef2009-08-28 20:31:08 +00002410 OrigTemplate = OrigTemplate->getInstantiatedFromMemberTemplate();
Douglas Gregorcf915552009-10-13 16:30:37 +00002411 }
2412
Douglas Gregor01afeef2009-08-28 20:31:08 +00002413 Pattern = OrigTemplate->getTemplatedDecl();
Douglas Gregor2373c592009-05-31 09:31:02 +00002414 }
Douglas Gregor463421d2009-03-03 04:44:36 +00002415
Richard Smith32b43762017-01-08 22:45:21 +00002416 return Pattern;
2417}
Mike Stump11289f42009-09-09 15:08:12 +00002418
Richard Smith32b43762017-01-08 22:45:21 +00002419bool Sema::InstantiateClassTemplateSpecialization(
2420 SourceLocation PointOfInstantiation,
2421 ClassTemplateSpecializationDecl *ClassTemplateSpec,
2422 TemplateSpecializationKind TSK, bool Complain) {
2423 // Perform the actual instantiation on the canonical declaration.
2424 ClassTemplateSpec = cast<ClassTemplateSpecializationDecl>(
2425 ClassTemplateSpec->getCanonicalDecl());
2426 if (ClassTemplateSpec->isInvalidDecl())
2427 return true;
2428
2429 CXXRecordDecl *Pattern = getPatternForClassTemplateSpecialization(
2430 *this, PointOfInstantiation, ClassTemplateSpec, TSK, Complain);
2431 if (!Pattern)
2432 return true;
2433
2434 return InstantiateClass(PointOfInstantiation, ClassTemplateSpec, Pattern,
2435 getTemplateInstantiationArgs(ClassTemplateSpec), TSK,
2436 Complain);
Douglas Gregor463421d2009-03-03 04:44:36 +00002437}
Douglas Gregor90a1a652009-03-19 17:26:29 +00002438
John McCall76d824f2009-08-25 22:02:44 +00002439/// \brief Instantiates the definitions of all of the member
2440/// of the given class, which is an instantiation of a class template
2441/// or a member class of a template.
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00002442void
2443Sema::InstantiateClassMembers(SourceLocation PointOfInstantiation,
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00002444 CXXRecordDecl *Instantiation,
2445 const MultiLevelTemplateArgumentList &TemplateArgs,
2446 TemplateSpecializationKind TSK) {
Richard Smitheb36ddf2014-04-24 22:45:46 +00002447 // FIXME: We need to notify the ASTMutationListener that we did all of these
2448 // things, in case we have an explicit instantiation definition in a PCM, a
2449 // module, or preamble, and the declaration is in an imported AST.
David Majnemer192d1792013-11-27 08:20:38 +00002450 assert(
2451 (TSK == TSK_ExplicitInstantiationDefinition ||
2452 TSK == TSK_ExplicitInstantiationDeclaration ||
2453 (TSK == TSK_ImplicitInstantiation && Instantiation->isLocalClass())) &&
2454 "Unexpected template specialization kind!");
Aaron Ballman629afae2014-03-07 19:56:05 +00002455 for (auto *D : Instantiation->decls()) {
Douglas Gregor1d957a32009-10-27 18:42:08 +00002456 bool SuppressNew = false;
Aaron Ballman629afae2014-03-07 19:56:05 +00002457 if (auto *Function = dyn_cast<FunctionDecl>(D)) {
Douglas Gregor1d957a32009-10-27 18:42:08 +00002458 if (FunctionDecl *Pattern
2459 = Function->getInstantiatedFromMemberFunction()) {
2460 MemberSpecializationInfo *MSInfo
2461 = Function->getMemberSpecializationInfo();
2462 assert(MSInfo && "No member specialization information?");
Douglas Gregor06aa50412010-04-09 21:02:29 +00002463 if (MSInfo->getTemplateSpecializationKind()
2464 == TSK_ExplicitSpecialization)
2465 continue;
2466
Douglas Gregor1d957a32009-10-27 18:42:08 +00002467 if (CheckSpecializationInstantiationRedecl(PointOfInstantiation, TSK,
2468 Function,
2469 MSInfo->getTemplateSpecializationKind(),
Nico Weberd75488d2010-09-27 21:02:09 +00002470 MSInfo->getPointOfInstantiation(),
Douglas Gregor1d957a32009-10-27 18:42:08 +00002471 SuppressNew) ||
2472 SuppressNew)
Douglas Gregorbbe8f462009-10-08 15:14:33 +00002473 continue;
Richard Smitheb36ddf2014-04-24 22:45:46 +00002474
2475 // C++11 [temp.explicit]p8:
2476 // An explicit instantiation definition that names a class template
2477 // specialization explicitly instantiates the class template
2478 // specialization and is only an explicit instantiation definition
2479 // of members whose definition is visible at the point of
2480 // instantiation.
2481 if (TSK == TSK_ExplicitInstantiationDefinition && !Pattern->isDefined())
Douglas Gregor1d957a32009-10-27 18:42:08 +00002482 continue;
2483
Richard Smitheb36ddf2014-04-24 22:45:46 +00002484 Function->setTemplateSpecializationKind(TSK, PointOfInstantiation);
2485
2486 if (Function->isDefined()) {
2487 // Let the ASTConsumer know that this function has been explicitly
2488 // instantiated now, and its linkage might have changed.
2489 Consumer.HandleTopLevelDecl(DeclGroupRef(Function));
2490 } else if (TSK == TSK_ExplicitInstantiationDefinition) {
Douglas Gregor1d957a32009-10-27 18:42:08 +00002491 InstantiateFunctionDefinition(PointOfInstantiation, Function);
Richard Smitheb36ddf2014-04-24 22:45:46 +00002492 } else if (TSK == TSK_ImplicitInstantiation) {
2493 PendingLocalImplicitInstantiations.push_back(
2494 std::make_pair(Function, PointOfInstantiation));
Douglas Gregor1d957a32009-10-27 18:42:08 +00002495 }
Douglas Gregorbbe8f462009-10-08 15:14:33 +00002496 }
Aaron Ballman629afae2014-03-07 19:56:05 +00002497 } else if (auto *Var = dyn_cast<VarDecl>(D)) {
Richard Smith8809a0c2013-09-27 20:14:12 +00002498 if (isa<VarTemplateSpecializationDecl>(Var))
2499 continue;
2500
Douglas Gregor86d142a2009-10-08 07:24:58 +00002501 if (Var->isStaticDataMember()) {
Douglas Gregor1d957a32009-10-27 18:42:08 +00002502 MemberSpecializationInfo *MSInfo = Var->getMemberSpecializationInfo();
2503 assert(MSInfo && "No member specialization information?");
Douglas Gregor06aa50412010-04-09 21:02:29 +00002504 if (MSInfo->getTemplateSpecializationKind()
2505 == TSK_ExplicitSpecialization)
2506 continue;
2507
Douglas Gregor1d957a32009-10-27 18:42:08 +00002508 if (CheckSpecializationInstantiationRedecl(PointOfInstantiation, TSK,
2509 Var,
2510 MSInfo->getTemplateSpecializationKind(),
Nico Weberd75488d2010-09-27 21:02:09 +00002511 MSInfo->getPointOfInstantiation(),
Douglas Gregor1d957a32009-10-27 18:42:08 +00002512 SuppressNew) ||
2513 SuppressNew)
Douglas Gregorbbe8f462009-10-08 15:14:33 +00002514 continue;
2515
Douglas Gregor1d957a32009-10-27 18:42:08 +00002516 if (TSK == TSK_ExplicitInstantiationDefinition) {
2517 // C++0x [temp.explicit]p8:
2518 // An explicit instantiation definition that names a class template
2519 // specialization explicitly instantiates the class template
2520 // specialization and is only an explicit instantiation definition
2521 // of members whose definition is visible at the point of
2522 // instantiation.
Richard Smith62f19e72016-06-25 00:15:56 +00002523 if (!Var->getInstantiatedFromStaticDataMember()->getDefinition())
Douglas Gregor1d957a32009-10-27 18:42:08 +00002524 continue;
2525
2526 Var->setTemplateSpecializationKind(TSK, PointOfInstantiation);
Douglas Gregor86d142a2009-10-08 07:24:58 +00002527 InstantiateStaticDataMemberDefinition(PointOfInstantiation, Var);
Douglas Gregor1d957a32009-10-27 18:42:08 +00002528 } else {
2529 Var->setTemplateSpecializationKind(TSK, PointOfInstantiation);
2530 }
2531 }
Aaron Ballman629afae2014-03-07 19:56:05 +00002532 } else if (auto *Record = dyn_cast<CXXRecordDecl>(D)) {
Douglas Gregor1da22252010-04-18 18:11:38 +00002533 // Always skip the injected-class-name, along with any
2534 // redeclarations of nested classes, since both would cause us
2535 // to try to instantiate the members of a class twice.
Richard Smith069ecf62014-11-20 22:56:34 +00002536 // Skip closure types; they'll get instantiated when we instantiate
2537 // the corresponding lambda-expression.
2538 if (Record->isInjectedClassName() || Record->getPreviousDecl() ||
2539 Record->isLambda())
Douglas Gregord801b062009-10-07 23:56:10 +00002540 continue;
2541
Douglas Gregor1d957a32009-10-27 18:42:08 +00002542 MemberSpecializationInfo *MSInfo = Record->getMemberSpecializationInfo();
2543 assert(MSInfo && "No member specialization information?");
Douglas Gregor06aa50412010-04-09 21:02:29 +00002544
2545 if (MSInfo->getTemplateSpecializationKind()
2546 == TSK_ExplicitSpecialization)
2547 continue;
Nico Weberd75488d2010-09-27 21:02:09 +00002548
Hans Wennborga86a83b2016-05-26 19:42:56 +00002549 if (Context.getTargetInfo().getCXXABI().isMicrosoft() &&
2550 TSK == TSK_ExplicitInstantiationDeclaration) {
2551 // In MSVC mode, explicit instantiation decl of the outer class doesn't
2552 // affect the inner class.
2553 continue;
2554 }
2555
Douglas Gregor1d957a32009-10-27 18:42:08 +00002556 if (CheckSpecializationInstantiationRedecl(PointOfInstantiation, TSK,
2557 Record,
2558 MSInfo->getTemplateSpecializationKind(),
Nico Weberd75488d2010-09-27 21:02:09 +00002559 MSInfo->getPointOfInstantiation(),
Douglas Gregor1d957a32009-10-27 18:42:08 +00002560 SuppressNew) ||
2561 SuppressNew)
Douglas Gregorbbe8f462009-10-08 15:14:33 +00002562 continue;
2563
Douglas Gregor1d957a32009-10-27 18:42:08 +00002564 CXXRecordDecl *Pattern = Record->getInstantiatedFromMemberClass();
2565 assert(Pattern && "Missing instantiated-from-template information");
2566
Douglas Gregor0a5a2212010-02-11 01:04:33 +00002567 if (!Record->getDefinition()) {
2568 if (!Pattern->getDefinition()) {
Douglas Gregor1d957a32009-10-27 18:42:08 +00002569 // C++0x [temp.explicit]p8:
2570 // An explicit instantiation definition that names a class template
2571 // specialization explicitly instantiates the class template
2572 // specialization and is only an explicit instantiation definition
2573 // of members whose definition is visible at the point of
2574 // instantiation.
2575 if (TSK == TSK_ExplicitInstantiationDeclaration) {
2576 MSInfo->setTemplateSpecializationKind(TSK);
2577 MSInfo->setPointOfInstantiation(PointOfInstantiation);
2578 }
2579
2580 continue;
2581 }
2582
2583 InstantiateClass(PointOfInstantiation, Record, Pattern,
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00002584 TemplateArgs,
2585 TSK);
Nico Weberd75488d2010-09-27 21:02:09 +00002586 } else {
2587 if (TSK == TSK_ExplicitInstantiationDefinition &&
2588 Record->getTemplateSpecializationKind() ==
2589 TSK_ExplicitInstantiationDeclaration) {
2590 Record->setTemplateSpecializationKind(TSK);
2591 MarkVTableUsed(PointOfInstantiation, Record, true);
2592 }
Douglas Gregor1d957a32009-10-27 18:42:08 +00002593 }
Douglas Gregorc093c1d2009-10-08 01:19:17 +00002594
Douglas Gregor0a5a2212010-02-11 01:04:33 +00002595 Pattern = cast_or_null<CXXRecordDecl>(Record->getDefinition());
Douglas Gregor1d957a32009-10-27 18:42:08 +00002596 if (Pattern)
2597 InstantiateClassMembers(PointOfInstantiation, Pattern, TemplateArgs,
2598 TSK);
Aaron Ballman629afae2014-03-07 19:56:05 +00002599 } else if (auto *Enum = dyn_cast<EnumDecl>(D)) {
Richard Smith4b38ded2012-03-14 23:13:10 +00002600 MemberSpecializationInfo *MSInfo = Enum->getMemberSpecializationInfo();
2601 assert(MSInfo && "No member specialization information?");
2602
2603 if (MSInfo->getTemplateSpecializationKind()
2604 == TSK_ExplicitSpecialization)
2605 continue;
2606
2607 if (CheckSpecializationInstantiationRedecl(
2608 PointOfInstantiation, TSK, Enum,
2609 MSInfo->getTemplateSpecializationKind(),
2610 MSInfo->getPointOfInstantiation(), SuppressNew) ||
2611 SuppressNew)
2612 continue;
2613
2614 if (Enum->getDefinition())
2615 continue;
2616
Richard Smith6739a102016-05-05 00:56:12 +00002617 EnumDecl *Pattern = Enum->getTemplateInstantiationPattern();
Richard Smith4b38ded2012-03-14 23:13:10 +00002618 assert(Pattern && "Missing instantiated-from-template information");
2619
2620 if (TSK == TSK_ExplicitInstantiationDefinition) {
2621 if (!Pattern->getDefinition())
2622 continue;
2623
2624 InstantiateEnum(PointOfInstantiation, Enum, Pattern, TemplateArgs, TSK);
2625 } else {
2626 MSInfo->setTemplateSpecializationKind(TSK);
2627 MSInfo->setPointOfInstantiation(PointOfInstantiation);
2628 }
Reid Klecknerd60b82f2014-11-17 23:36:45 +00002629 } else if (auto *Field = dyn_cast<FieldDecl>(D)) {
2630 // No need to instantiate in-class initializers during explicit
2631 // instantiation.
2632 if (Field->hasInClassInitializer() && TSK == TSK_ImplicitInstantiation) {
2633 CXXRecordDecl *ClassPattern =
2634 Instantiation->getTemplateInstantiationPattern();
2635 DeclContext::lookup_result Lookup =
2636 ClassPattern->lookup(Field->getDeclName());
David Majnemer76a25622016-06-09 05:26:56 +00002637 FieldDecl *Pattern = cast<FieldDecl>(Lookup.front());
Reid Klecknerd60b82f2014-11-17 23:36:45 +00002638 InstantiateInClassInitializer(PointOfInstantiation, Field, Pattern,
2639 TemplateArgs);
2640 }
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00002641 }
2642 }
2643}
2644
2645/// \brief Instantiate the definitions of all of the members of the
2646/// given class template specialization, which was named as part of an
2647/// explicit instantiation.
Mike Stump11289f42009-09-09 15:08:12 +00002648void
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00002649Sema::InstantiateClassTemplateSpecializationMembers(
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00002650 SourceLocation PointOfInstantiation,
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00002651 ClassTemplateSpecializationDecl *ClassTemplateSpec,
2652 TemplateSpecializationKind TSK) {
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00002653 // C++0x [temp.explicit]p7:
2654 // An explicit instantiation that names a class template
2655 // specialization is an explicit instantion of the same kind
2656 // (declaration or definition) of each of its members (not
2657 // including members inherited from base classes) that has not
2658 // been previously explicitly specialized in the translation unit
2659 // containing the explicit instantiation, except as described
2660 // below.
2661 InstantiateClassMembers(PointOfInstantiation, ClassTemplateSpec,
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00002662 getTemplateInstantiationArgs(ClassTemplateSpec),
2663 TSK);
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00002664}
2665
John McCalldadc5752010-08-24 06:29:42 +00002666StmtResult
Douglas Gregor01afeef2009-08-28 20:31:08 +00002667Sema::SubstStmt(Stmt *S, const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregorebe10102009-08-20 07:17:43 +00002668 if (!S)
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00002669 return S;
Douglas Gregorebe10102009-08-20 07:17:43 +00002670
2671 TemplateInstantiator Instantiator(*this, TemplateArgs,
2672 SourceLocation(),
2673 DeclarationName());
2674 return Instantiator.TransformStmt(S);
2675}
2676
John McCalldadc5752010-08-24 06:29:42 +00002677ExprResult
Douglas Gregor01afeef2009-08-28 20:31:08 +00002678Sema::SubstExpr(Expr *E, const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregora16548e2009-08-11 05:31:07 +00002679 if (!E)
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00002680 return E;
Mike Stump11289f42009-09-09 15:08:12 +00002681
Douglas Gregora16548e2009-08-11 05:31:07 +00002682 TemplateInstantiator Instantiator(*this, TemplateArgs,
2683 SourceLocation(),
2684 DeclarationName());
2685 return Instantiator.TransformExpr(E);
2686}
2687
Richard Smithd59b8322012-12-19 01:39:02 +00002688ExprResult Sema::SubstInitializer(Expr *Init,
2689 const MultiLevelTemplateArgumentList &TemplateArgs,
2690 bool CXXDirectInit) {
2691 TemplateInstantiator Instantiator(*this, TemplateArgs,
2692 SourceLocation(),
2693 DeclarationName());
2694 return Instantiator.TransformInitializer(Init, CXXDirectInit);
2695}
2696
Craig Topper99d23532015-12-24 23:58:29 +00002697bool Sema::SubstExprs(ArrayRef<Expr *> Exprs, bool IsCall,
Douglas Gregor2cd32a02011-01-07 19:35:17 +00002698 const MultiLevelTemplateArgumentList &TemplateArgs,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002699 SmallVectorImpl<Expr *> &Outputs) {
Craig Topper99d23532015-12-24 23:58:29 +00002700 if (Exprs.empty())
Douglas Gregor2cd32a02011-01-07 19:35:17 +00002701 return false;
Richard Smithd59b8322012-12-19 01:39:02 +00002702
Douglas Gregor2cd32a02011-01-07 19:35:17 +00002703 TemplateInstantiator Instantiator(*this, TemplateArgs,
2704 SourceLocation(),
2705 DeclarationName());
Craig Topper99d23532015-12-24 23:58:29 +00002706 return Instantiator.TransformExprs(Exprs.data(), Exprs.size(),
2707 IsCall, Outputs);
Douglas Gregor2cd32a02011-01-07 19:35:17 +00002708}
2709
Douglas Gregor14454802011-02-25 02:25:35 +00002710NestedNameSpecifierLoc
2711Sema::SubstNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS,
2712 const MultiLevelTemplateArgumentList &TemplateArgs) {
2713 if (!NNS)
2714 return NestedNameSpecifierLoc();
2715
2716 TemplateInstantiator Instantiator(*this, TemplateArgs, NNS.getBeginLoc(),
2717 DeclarationName());
2718 return Instantiator.TransformNestedNameSpecifierLoc(NNS);
2719}
2720
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002721/// \brief Do template substitution on declaration name info.
2722DeclarationNameInfo
2723Sema::SubstDeclarationNameInfo(const DeclarationNameInfo &NameInfo,
2724 const MultiLevelTemplateArgumentList &TemplateArgs) {
2725 TemplateInstantiator Instantiator(*this, TemplateArgs, NameInfo.getLoc(),
2726 NameInfo.getName());
2727 return Instantiator.TransformDeclarationNameInfo(NameInfo);
2728}
2729
Douglas Gregoraa594892009-03-31 18:38:02 +00002730TemplateName
Douglas Gregordf846d12011-03-02 18:46:51 +00002731Sema::SubstTemplateName(NestedNameSpecifierLoc QualifierLoc,
2732 TemplateName Name, SourceLocation Loc,
Douglas Gregor01afeef2009-08-28 20:31:08 +00002733 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor71dc5092009-08-06 06:41:21 +00002734 TemplateInstantiator Instantiator(*this, TemplateArgs, Loc,
2735 DeclarationName());
Douglas Gregordf846d12011-03-02 18:46:51 +00002736 CXXScopeSpec SS;
2737 SS.Adopt(QualifierLoc);
2738 return Instantiator.TransformTemplateName(SS, Name, Loc);
Douglas Gregoraa594892009-03-31 18:38:02 +00002739}
Douglas Gregorc43620d2009-06-11 00:06:24 +00002740
Douglas Gregor0f3feb42010-12-22 21:19:48 +00002741bool Sema::Subst(const TemplateArgumentLoc *Args, unsigned NumArgs,
2742 TemplateArgumentListInfo &Result,
John McCall0ad16662009-10-29 08:12:44 +00002743 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregore922c772009-08-04 22:27:00 +00002744 TemplateInstantiator Instantiator(*this, TemplateArgs, SourceLocation(),
2745 DeclarationName());
Douglas Gregor0f3feb42010-12-22 21:19:48 +00002746
2747 return Instantiator.TransformTemplateArguments(Args, NumArgs, Result);
Douglas Gregorc43620d2009-06-11 00:06:24 +00002748}
Douglas Gregor14cf7522010-04-30 18:55:50 +00002749
Richard Smith70b13042015-01-09 01:19:56 +00002750static const Decl *getCanonicalParmVarDecl(const Decl *D) {
DeLesley Hutchinsf39c0c22012-09-26 17:57:31 +00002751 // When storing ParmVarDecls in the local instantiation scope, we always
2752 // want to use the ParmVarDecl from the canonical function declaration,
2753 // since the map is then valid for any redeclaration or definition of that
2754 // function.
2755 if (const ParmVarDecl *PV = dyn_cast<ParmVarDecl>(D)) {
2756 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(PV->getDeclContext())) {
2757 unsigned i = PV->getFunctionScopeIndex();
Richard Smith70b13042015-01-09 01:19:56 +00002758 // This parameter might be from a freestanding function type within the
2759 // function and isn't necessarily referring to one of FD's parameters.
2760 if (FD->getParamDecl(i) == PV)
2761 return FD->getCanonicalDecl()->getParamDecl(i);
DeLesley Hutchinsf39c0c22012-09-26 17:57:31 +00002762 }
2763 }
2764 return D;
2765}
2766
2767
Douglas Gregorf3010112011-01-07 16:43:16 +00002768llvm::PointerUnion<Decl *, LocalInstantiationScope::DeclArgumentPack *> *
2769LocalInstantiationScope::findInstantiationOf(const Decl *D) {
DeLesley Hutchinsf39c0c22012-09-26 17:57:31 +00002770 D = getCanonicalParmVarDecl(D);
Chris Lattnercab02a62011-02-17 20:34:02 +00002771 for (LocalInstantiationScope *Current = this; Current;
Douglas Gregor14cf7522010-04-30 18:55:50 +00002772 Current = Current->Outer) {
Chris Lattnercab02a62011-02-17 20:34:02 +00002773
Douglas Gregor14cf7522010-04-30 18:55:50 +00002774 // Check if we found something within this scope.
Douglas Gregore9fc8dc2010-12-21 21:22:51 +00002775 const Decl *CheckD = D;
2776 do {
Douglas Gregorf3010112011-01-07 16:43:16 +00002777 LocalDeclsMap::iterator Found = Current->LocalDecls.find(CheckD);
Douglas Gregore9fc8dc2010-12-21 21:22:51 +00002778 if (Found != Current->LocalDecls.end())
Douglas Gregorf3010112011-01-07 16:43:16 +00002779 return &Found->second;
Douglas Gregore9fc8dc2010-12-21 21:22:51 +00002780
2781 // If this is a tag declaration, it's possible that we need to look for
2782 // a previous declaration.
2783 if (const TagDecl *Tag = dyn_cast<TagDecl>(CheckD))
Douglas Gregorec9fd132012-01-14 16:38:05 +00002784 CheckD = Tag->getPreviousDecl();
Douglas Gregore9fc8dc2010-12-21 21:22:51 +00002785 else
Craig Topperc3ec1492014-05-26 06:22:03 +00002786 CheckD = nullptr;
Douglas Gregore9fc8dc2010-12-21 21:22:51 +00002787 } while (CheckD);
2788
Douglas Gregor14cf7522010-04-30 18:55:50 +00002789 // If we aren't combined with our outer scope, we're done.
2790 if (!Current->CombineWithOuterScope)
2791 break;
2792 }
Chris Lattnercab02a62011-02-17 20:34:02 +00002793
Serge Pavlov7cd8f602013-07-15 06:14:07 +00002794 // If we're performing a partial substitution during template argument
2795 // deduction, we may not have values for template parameters yet.
2796 if (isa<NonTypeTemplateParmDecl>(D) || isa<TemplateTypeParmDecl>(D) ||
2797 isa<TemplateTemplateParmDecl>(D))
Craig Topperc3ec1492014-05-26 06:22:03 +00002798 return nullptr;
Serge Pavlov7cd8f602013-07-15 06:14:07 +00002799
Serge Pavlove7ad8312015-05-15 10:10:28 +00002800 // Local types referenced prior to definition may require instantiation.
2801 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D))
2802 if (RD->isLocalClass())
2803 return nullptr;
2804
2805 // Enumeration types referenced prior to definition may appear as a result of
2806 // error recovery.
2807 if (isa<EnumDecl>(D))
Serge Pavlov4c511742015-05-04 16:44:39 +00002808 return nullptr;
2809
Chris Lattnercab02a62011-02-17 20:34:02 +00002810 // If we didn't find the decl, then we either have a sema bug, or we have a
2811 // forward reference to a label declaration. Return null to indicate that
2812 // we have an uninstantiated label.
2813 assert(isa<LabelDecl>(D) && "declaration not instantiated in this scope");
Craig Topperc3ec1492014-05-26 06:22:03 +00002814 return nullptr;
Douglas Gregor14cf7522010-04-30 18:55:50 +00002815}
2816
John McCall19c1bfd2010-08-25 05:32:35 +00002817void LocalInstantiationScope::InstantiatedLocal(const Decl *D, Decl *Inst) {
DeLesley Hutchinsf39c0c22012-09-26 17:57:31 +00002818 D = getCanonicalParmVarDecl(D);
Douglas Gregorf3010112011-01-07 16:43:16 +00002819 llvm::PointerUnion<Decl *, DeclArgumentPack *> &Stored = LocalDecls[D];
Richard Smith70b13042015-01-09 01:19:56 +00002820 if (Stored.isNull()) {
2821#ifndef NDEBUG
2822 // It should not be present in any surrounding scope either.
2823 LocalInstantiationScope *Current = this;
2824 while (Current->CombineWithOuterScope && Current->Outer) {
2825 Current = Current->Outer;
2826 assert(Current->LocalDecls.find(D) == Current->LocalDecls.end() &&
2827 "Instantiated local in inner and outer scopes");
2828 }
2829#endif
Douglas Gregora8bac7f2011-01-10 07:32:04 +00002830 Stored = Inst;
Richard Smith70b13042015-01-09 01:19:56 +00002831 } else if (DeclArgumentPack *Pack = Stored.dyn_cast<DeclArgumentPack *>()) {
James Y Knight48fefa32015-09-30 14:04:23 +00002832 Pack->push_back(cast<ParmVarDecl>(Inst));
Richard Smith70b13042015-01-09 01:19:56 +00002833 } else {
Douglas Gregora8bac7f2011-01-10 07:32:04 +00002834 assert(Stored.get<Decl *>() == Inst && "Already instantiated this local");
Richard Smith70b13042015-01-09 01:19:56 +00002835 }
Douglas Gregor14cf7522010-04-30 18:55:50 +00002836}
Douglas Gregorf3010112011-01-07 16:43:16 +00002837
James Y Knight48fefa32015-09-30 14:04:23 +00002838void LocalInstantiationScope::InstantiatedLocalPackArg(const Decl *D,
2839 ParmVarDecl *Inst) {
DeLesley Hutchinsf39c0c22012-09-26 17:57:31 +00002840 D = getCanonicalParmVarDecl(D);
Douglas Gregorf3010112011-01-07 16:43:16 +00002841 DeclArgumentPack *Pack = LocalDecls[D].get<DeclArgumentPack *>();
2842 Pack->push_back(Inst);
2843}
2844
2845void LocalInstantiationScope::MakeInstantiatedLocalArgPack(const Decl *D) {
Richard Smith70b13042015-01-09 01:19:56 +00002846#ifndef NDEBUG
2847 // This should be the first time we've been told about this decl.
2848 for (LocalInstantiationScope *Current = this;
2849 Current && Current->CombineWithOuterScope; Current = Current->Outer)
2850 assert(Current->LocalDecls.find(D) == Current->LocalDecls.end() &&
2851 "Creating local pack after instantiation of local");
2852#endif
2853
DeLesley Hutchinsf39c0c22012-09-26 17:57:31 +00002854 D = getCanonicalParmVarDecl(D);
Douglas Gregorf3010112011-01-07 16:43:16 +00002855 llvm::PointerUnion<Decl *, DeclArgumentPack *> &Stored = LocalDecls[D];
Douglas Gregorf3010112011-01-07 16:43:16 +00002856 DeclArgumentPack *Pack = new DeclArgumentPack;
2857 Stored = Pack;
2858 ArgumentPacks.push_back(Pack);
2859}
2860
Douglas Gregora8bac7f2011-01-10 07:32:04 +00002861void LocalInstantiationScope::SetPartiallySubstitutedPack(NamedDecl *Pack,
2862 const TemplateArgument *ExplicitArgs,
2863 unsigned NumExplicitArgs) {
2864 assert((!PartiallySubstitutedPack || PartiallySubstitutedPack == Pack) &&
2865 "Already have a partially-substituted pack");
2866 assert((!PartiallySubstitutedPack
2867 || NumArgsInPartiallySubstitutedPack == NumExplicitArgs) &&
2868 "Wrong number of arguments in partially-substituted pack");
2869 PartiallySubstitutedPack = Pack;
2870 ArgsInPartiallySubstitutedPack = ExplicitArgs;
2871 NumArgsInPartiallySubstitutedPack = NumExplicitArgs;
2872}
2873
2874NamedDecl *LocalInstantiationScope::getPartiallySubstitutedPack(
2875 const TemplateArgument **ExplicitArgs,
2876 unsigned *NumExplicitArgs) const {
2877 if (ExplicitArgs)
Craig Topperc3ec1492014-05-26 06:22:03 +00002878 *ExplicitArgs = nullptr;
Douglas Gregora8bac7f2011-01-10 07:32:04 +00002879 if (NumExplicitArgs)
2880 *NumExplicitArgs = 0;
2881
2882 for (const LocalInstantiationScope *Current = this; Current;
2883 Current = Current->Outer) {
2884 if (Current->PartiallySubstitutedPack) {
2885 if (ExplicitArgs)
2886 *ExplicitArgs = Current->ArgsInPartiallySubstitutedPack;
2887 if (NumExplicitArgs)
2888 *NumExplicitArgs = Current->NumArgsInPartiallySubstitutedPack;
2889
2890 return Current->PartiallySubstitutedPack;
2891 }
2892
2893 if (!Current->CombineWithOuterScope)
2894 break;
2895 }
Craig Topperc3ec1492014-05-26 06:22:03 +00002896
2897 return nullptr;
Douglas Gregora8bac7f2011-01-10 07:32:04 +00002898}