blob: af56ff06ac49dc5a07b10283462ffd45e3c43d79 [file] [log] [blame]
Douglas Gregorfe1e1102009-02-27 19:31:52 +00001//===------- SemaTemplateInstantiate.cpp - C++ Template Instantiation ------===/
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Douglas Gregorfe1e1102009-02-27 19:31:52 +00006//===----------------------------------------------------------------------===/
7//
8// This file implements C++ template instantiation.
9//
10//===----------------------------------------------------------------------===/
11
John McCall83024632010-08-25 22:03:47 +000012#include "clang/Sema/SemaInternal.h"
Douglas Gregord6ff3322009-08-04 16:50:30 +000013#include "TreeTransform.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000014#include "clang/AST/ASTConsumer.h"
15#include "clang/AST/ASTContext.h"
Faisal Vali2cba1332013-10-23 06:44:28 +000016#include "clang/AST/ASTLambda.h"
Richard Smith4b054b22016-08-24 21:25:37 +000017#include "clang/AST/ASTMutationListener.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000018#include "clang/AST/DeclTemplate.h"
19#include "clang/AST/Expr.h"
Jordan Rose1e879d82018-03-23 00:07:18 +000020#include "clang/AST/PrettyDeclStackTrace.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000021#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"
John McCallde6836a2010-08-24 07:21:54 +000025#include "clang/Sema/Template.h"
John McCall19c1bfd2010-08-25 05:32:35 +000026#include "clang/Sema/TemplateDeduction.h"
Gabor Horvath207e7b12018-02-10 14:04:45 +000027#include "clang/Sema/TemplateInstCallback.h"
Anton Afanasyevd880de22019-03-30 08:42:48 +000028#include "llvm/Support/TimeProfiler.h"
Douglas Gregorfe1e1102009-02-27 19:31:52 +000029
30using namespace clang;
John McCall19c1bfd2010-08-25 05:32:35 +000031using namespace sema;
Douglas Gregorfe1e1102009-02-27 19:31:52 +000032
Douglas Gregor4ea568f2009-03-10 18:03:33 +000033//===----------------------------------------------------------------------===/
34// Template Instantiation Support
35//===----------------------------------------------------------------------===/
36
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000037/// Retrieve the template argument list(s) that should be used to
Douglas Gregor01afeef2009-08-28 20:31:08 +000038/// instantiate the definition of the given declaration.
Douglas Gregor36d7c5f2009-11-09 19:17:50 +000039///
40/// \param D the declaration for which we are computing template instantiation
41/// arguments.
42///
43/// \param Innermost if non-NULL, the innermost template argument list.
Douglas Gregor8c702532010-02-05 07:33:43 +000044///
45/// \param RelativeToPrimary true if we should get the template
46/// arguments relative to the primary template, even when we're
47/// dealing with a specialization. This is only relevant for function
48/// template specializations.
Douglas Gregor1bd7a942010-05-03 23:29:10 +000049///
50/// \param Pattern If non-NULL, indicates the pattern from which we will be
51/// instantiating the definition of the given declaration, \p D. This is
52/// used to determine the proper set of template instantiation arguments for
53/// friend function template specializations.
Douglas Gregora654dd82009-08-28 17:37:35 +000054MultiLevelTemplateArgumentList
Fangrui Song6907ce22018-07-30 19:24:48 +000055Sema::getTemplateInstantiationArgs(NamedDecl *D,
Douglas Gregor8c702532010-02-05 07:33:43 +000056 const TemplateArgumentList *Innermost,
Douglas Gregor1bd7a942010-05-03 23:29:10 +000057 bool RelativeToPrimary,
58 const FunctionDecl *Pattern) {
Douglas Gregora654dd82009-08-28 17:37:35 +000059 // Accumulate the set of template argument lists in this structure.
60 MultiLevelTemplateArgumentList Result;
Mike Stump11289f42009-09-09 15:08:12 +000061
Douglas Gregor36d7c5f2009-11-09 19:17:50 +000062 if (Innermost)
63 Result.addOuterTemplateArguments(Innermost);
Fangrui Song6907ce22018-07-30 19:24:48 +000064
Douglas Gregora654dd82009-08-28 17:37:35 +000065 DeclContext *Ctx = dyn_cast<DeclContext>(D);
Douglas Gregora51c9cc2011-05-22 00:21:10 +000066 if (!Ctx) {
Douglas Gregora654dd82009-08-28 17:37:35 +000067 Ctx = D->getDeclContext();
Larisse Voufo39a1e502013-08-06 01:03:05 +000068
Richard Smith59d05002019-04-26 02:11:23 +000069 // Add template arguments from a variable template instantiation. For a
70 // class-scope explicit specialization, there are no template arguments
71 // at this level, but there may be enclosing template arguments.
72 VarTemplateSpecializationDecl *Spec =
73 dyn_cast<VarTemplateSpecializationDecl>(D);
74 if (Spec && !Spec->isClassScopeExplicitSpecialization()) {
Larisse Voufo39a1e502013-08-06 01:03:05 +000075 // We're done when we hit an explicit specialization.
76 if (Spec->getSpecializationKind() == TSK_ExplicitSpecialization &&
77 !isa<VarTemplatePartialSpecializationDecl>(Spec))
78 return Result;
79
80 Result.addOuterTemplateArguments(&Spec->getTemplateInstantiationArgs());
81
82 // If this variable template specialization was instantiated from a
83 // specialized member that is a variable template, we're done.
84 assert(Spec->getSpecializedTemplate() && "No variable template?");
Richard Smithbeef3452014-01-16 23:39:20 +000085 llvm::PointerUnion<VarTemplateDecl*,
86 VarTemplatePartialSpecializationDecl*> Specialized
87 = Spec->getSpecializedTemplateOrPartial();
88 if (VarTemplatePartialSpecializationDecl *Partial =
89 Specialized.dyn_cast<VarTemplatePartialSpecializationDecl *>()) {
90 if (Partial->isMemberSpecialization())
91 return Result;
92 } else {
93 VarTemplateDecl *Tmpl = Specialized.get<VarTemplateDecl *>();
94 if (Tmpl->isMemberSpecialization())
95 return Result;
96 }
Larisse Voufo39a1e502013-08-06 01:03:05 +000097 }
98
Douglas Gregor55462622011-06-15 14:20:42 +000099 // If we have a template template parameter with translation unit context,
100 // then we're performing substitution into a default template argument of
101 // this template template parameter before we've constructed the template
102 // that will own this template template parameter. In this case, we
103 // use empty template parameter lists for all of the outer templates
104 // to avoid performing any substitutions.
105 if (Ctx->isTranslationUnit()) {
Fangrui Song6907ce22018-07-30 19:24:48 +0000106 if (TemplateTemplateParmDecl *TTP
Douglas Gregor55462622011-06-15 14:20:42 +0000107 = dyn_cast<TemplateTemplateParmDecl>(D)) {
108 for (unsigned I = 0, N = TTP->getDepth() + 1; I != N; ++I)
Richard Smith841d8b22013-05-17 03:04:50 +0000109 Result.addOuterTemplateArguments(None);
Douglas Gregor55462622011-06-15 14:20:42 +0000110 return Result;
111 }
112 }
Douglas Gregora51c9cc2011-05-22 00:21:10 +0000113 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000114
John McCall970d5302009-08-29 03:16:09 +0000115 while (!Ctx->isFileContext()) {
Douglas Gregora654dd82009-08-28 17:37:35 +0000116 // Add template arguments from a class template instantiation.
Richard Smith59d05002019-04-26 02:11:23 +0000117 ClassTemplateSpecializationDecl *Spec
118 = dyn_cast<ClassTemplateSpecializationDecl>(Ctx);
119 if (Spec && !Spec->isClassScopeExplicitSpecialization()) {
Douglas Gregora654dd82009-08-28 17:37:35 +0000120 // We're done when we hit an explicit specialization.
Douglas Gregor9961ce92010-07-08 18:37:38 +0000121 if (Spec->getSpecializationKind() == TSK_ExplicitSpecialization &&
122 !isa<ClassTemplatePartialSpecializationDecl>(Spec))
Douglas Gregora654dd82009-08-28 17:37:35 +0000123 break;
Mike Stump11289f42009-09-09 15:08:12 +0000124
Douglas Gregora654dd82009-08-28 17:37:35 +0000125 Result.addOuterTemplateArguments(&Spec->getTemplateInstantiationArgs());
Fangrui Song6907ce22018-07-30 19:24:48 +0000126
127 // If this class template specialization was instantiated from a
Douglas Gregorcf915552009-10-13 16:30:37 +0000128 // specialized member that is a class template, we're done.
129 assert(Spec->getSpecializedTemplate() && "No class template?");
130 if (Spec->getSpecializedTemplate()->isMemberSpecialization())
131 break;
Mike Stump11289f42009-09-09 15:08:12 +0000132 }
Douglas Gregora654dd82009-08-28 17:37:35 +0000133 // Add template arguments from a function template specialization.
John McCall970d5302009-08-29 03:16:09 +0000134 else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Ctx)) {
Douglas Gregor8c702532010-02-05 07:33:43 +0000135 if (!RelativeToPrimary &&
Richard Smithf19a8b02019-05-02 00:49:14 +0000136 Function->getTemplateSpecializationKindForInstantiation() ==
137 TSK_ExplicitSpecialization)
Douglas Gregorcf915552009-10-13 16:30:37 +0000138 break;
Fangrui Song6907ce22018-07-30 19:24:48 +0000139
Douglas Gregora654dd82009-08-28 17:37:35 +0000140 if (const TemplateArgumentList *TemplateArgs
Douglas Gregorcf915552009-10-13 16:30:37 +0000141 = Function->getTemplateSpecializationArgs()) {
142 // Add the template arguments for this specialization.
Douglas Gregora654dd82009-08-28 17:37:35 +0000143 Result.addOuterTemplateArguments(TemplateArgs);
John McCall970d5302009-08-29 03:16:09 +0000144
Douglas Gregorcf915552009-10-13 16:30:37 +0000145 // If this function was instantiated from a specialized member that is
146 // a function template, we're done.
147 assert(Function->getPrimaryTemplate() && "No function template?");
148 if (Function->getPrimaryTemplate()->isMemberSpecialization())
149 break;
Faisal Vali2cba1332013-10-23 06:44:28 +0000150
151 // If this function is a generic lambda specialization, we are done.
152 if (isGenericLambdaCallOperatorSpecialization(Function))
153 break;
154
Douglas Gregor43669f82011-03-05 17:54:25 +0000155 } else if (FunctionTemplateDecl *FunTmpl
156 = Function->getDescribedFunctionTemplate()) {
157 // Add the "injected" template arguments.
Richard Smith841d8b22013-05-17 03:04:50 +0000158 Result.addOuterTemplateArguments(FunTmpl->getInjectedTemplateArgs());
Douglas Gregorcf915552009-10-13 16:30:37 +0000159 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000160
John McCall970d5302009-08-29 03:16:09 +0000161 // If this is a friend declaration and it declares an entity at
162 // namespace scope, take arguments from its lexical parent
Douglas Gregor1bd7a942010-05-03 23:29:10 +0000163 // instead of its semantic parent, unless of course the pattern we're
164 // instantiating actually comes from the file's context!
John McCall970d5302009-08-29 03:16:09 +0000165 if (Function->getFriendObjectKind() &&
Douglas Gregor1bd7a942010-05-03 23:29:10 +0000166 Function->getDeclContext()->isFileContext() &&
167 (!Pattern || !Pattern->getLexicalDeclContext()->isFileContext())) {
John McCall970d5302009-08-29 03:16:09 +0000168 Ctx = Function->getLexicalDeclContext();
Douglas Gregor8c702532010-02-05 07:33:43 +0000169 RelativeToPrimary = false;
John McCall970d5302009-08-29 03:16:09 +0000170 continue;
171 }
Douglas Gregor9961ce92010-07-08 18:37:38 +0000172 } else if (CXXRecordDecl *Rec = dyn_cast<CXXRecordDecl>(Ctx)) {
173 if (ClassTemplateDecl *ClassTemplate = Rec->getDescribedClassTemplate()) {
174 QualType T = ClassTemplate->getInjectedClassNameSpecialization();
Richard Smith841d8b22013-05-17 03:04:50 +0000175 const TemplateSpecializationType *TST =
176 cast<TemplateSpecializationType>(Context.getCanonicalType(T));
177 Result.addOuterTemplateArguments(
178 llvm::makeArrayRef(TST->getArgs(), TST->getNumArgs()));
Douglas Gregor9961ce92010-07-08 18:37:38 +0000179 if (ClassTemplate->isMemberSpecialization())
180 break;
181 }
Douglas Gregora654dd82009-08-28 17:37:35 +0000182 }
John McCall970d5302009-08-29 03:16:09 +0000183
184 Ctx = Ctx->getParent();
Douglas Gregor8c702532010-02-05 07:33:43 +0000185 RelativeToPrimary = false;
Douglas Gregorb4850462009-05-14 23:26:13 +0000186 }
Mike Stump11289f42009-09-09 15:08:12 +0000187
Douglas Gregora654dd82009-08-28 17:37:35 +0000188 return Result;
Douglas Gregorb4850462009-05-14 23:26:13 +0000189}
190
Richard Smith696e3122017-02-23 01:43:54 +0000191bool Sema::CodeSynthesisContext::isInstantiationRecord() const {
Douglas Gregor84d49a22009-11-11 21:54:23 +0000192 switch (Kind) {
193 case TemplateInstantiation:
Richard Smithf623c962012-04-17 00:58:00 +0000194 case ExceptionSpecInstantiation:
Douglas Gregor84d49a22009-11-11 21:54:23 +0000195 case DefaultTemplateArgumentInstantiation:
196 case DefaultFunctionArgumentInstantiation:
Douglas Gregor84d49a22009-11-11 21:54:23 +0000197 case ExplicitTemplateArgumentSubstitution:
198 case DeducedTemplateArgumentSubstitution:
199 case PriorTemplateArgumentSubstitution:
Richard Smith8a874c92012-07-08 02:38:24 +0000200 return true;
201
Douglas Gregor84d49a22009-11-11 21:54:23 +0000202 case DefaultTemplateArgumentChecking:
Richard Smith13381222017-02-23 21:43:43 +0000203 case DeclaringSpecialMember:
Richard Smith883dbc42017-05-25 22:47:05 +0000204 case DefiningSynthesizedFunction:
Richard Smith5159bbad2018-09-05 22:30:37 +0000205 case ExceptionSpecEvaluation:
Douglas Gregor84d49a22009-11-11 21:54:23 +0000206 return false;
Fangrui Song6907ce22018-07-30 19:24:48 +0000207
Gabor Horvath207e7b12018-02-10 14:04:45 +0000208 // This function should never be called when Kind's value is Memoization.
209 case Memoization:
210 break;
Douglas Gregor84d49a22009-11-11 21:54:23 +0000211 }
David Blaikie8a40f702012-01-17 06:56:22 +0000212
Richard Smith696e3122017-02-23 01:43:54 +0000213 llvm_unreachable("Invalid SynthesisKind!");
Douglas Gregor84d49a22009-11-11 21:54:23 +0000214}
215
Benjamin Kramer7761a042015-03-06 16:36:50 +0000216Sema::InstantiatingTemplate::InstantiatingTemplate(
Richard Smith696e3122017-02-23 01:43:54 +0000217 Sema &SemaRef, CodeSynthesisContext::SynthesisKind Kind,
Stephan Tolksdorf232670f2014-03-13 20:34:22 +0000218 SourceLocation PointOfInstantiation, SourceRange InstantiationRange,
219 Decl *Entity, NamedDecl *Template, ArrayRef<TemplateArgument> TemplateArgs,
Benjamin Kramer7761a042015-03-06 16:36:50 +0000220 sema::TemplateDeductionInfo *DeductionInfo)
Richard Smith13381222017-02-23 21:43:43 +0000221 : SemaRef(SemaRef) {
Akira Hatanaka43556c12016-11-03 15:04:58 +0000222 // Don't allow further instantiation if a fatal error and an uncompilable
Akira Hatanaka40c15ab2016-11-03 17:11:28 +0000223 // error have occurred. Any diagnostics we might have raised will not be
Akira Hatanaka43556c12016-11-03 15:04:58 +0000224 // visible, and we do not need to construct a correct AST.
225 if (SemaRef.Diags.hasFatalErrorOccurred() &&
226 SemaRef.Diags.hasUncompilableErrorOccurred()) {
David Majnemer8c969ea2015-01-30 05:01:23 +0000227 Invalid = true;
228 return;
229 }
Stephan Tolksdorf232670f2014-03-13 20:34:22 +0000230 Invalid = CheckInstantiationDepth(PointOfInstantiation, InstantiationRange);
231 if (!Invalid) {
Richard Smith696e3122017-02-23 01:43:54 +0000232 CodeSynthesisContext Inst;
Stephan Tolksdorf232670f2014-03-13 20:34:22 +0000233 Inst.Kind = Kind;
234 Inst.PointOfInstantiation = PointOfInstantiation;
235 Inst.Entity = Entity;
236 Inst.Template = Template;
237 Inst.TemplateArgs = TemplateArgs.data();
238 Inst.NumTemplateArgs = TemplateArgs.size();
239 Inst.DeductionInfo = DeductionInfo;
240 Inst.InstantiationRange = InstantiationRange;
Richard Smith13381222017-02-23 21:43:43 +0000241 SemaRef.pushCodeSynthesisContext(Inst);
242
Richard Smith54f18e82016-08-31 02:15:21 +0000243 AlreadyInstantiating =
244 !SemaRef.InstantiatingSpecializations
245 .insert(std::make_pair(Inst.Entity->getCanonicalDecl(), Inst.Kind))
246 .second;
Gabor Horvath207e7b12018-02-10 14:04:45 +0000247 atTemplateBegin(SemaRef.TemplateInstCallbacks, SemaRef, Inst);
Stephan Tolksdorf232670f2014-03-13 20:34:22 +0000248 }
249}
250
Benjamin Kramer7761a042015-03-06 16:36:50 +0000251Sema::InstantiatingTemplate::InstantiatingTemplate(
252 Sema &SemaRef, SourceLocation PointOfInstantiation, Decl *Entity,
253 SourceRange InstantiationRange)
254 : InstantiatingTemplate(SemaRef,
Richard Smith696e3122017-02-23 01:43:54 +0000255 CodeSynthesisContext::TemplateInstantiation,
Benjamin Kramer7761a042015-03-06 16:36:50 +0000256 PointOfInstantiation, InstantiationRange, Entity) {}
Douglas Gregor79cf6032009-03-10 20:44:00 +0000257
Benjamin Kramer7761a042015-03-06 16:36:50 +0000258Sema::InstantiatingTemplate::InstantiatingTemplate(
259 Sema &SemaRef, SourceLocation PointOfInstantiation, FunctionDecl *Entity,
260 ExceptionSpecification, SourceRange InstantiationRange)
261 : InstantiatingTemplate(
Richard Smith696e3122017-02-23 01:43:54 +0000262 SemaRef, CodeSynthesisContext::ExceptionSpecInstantiation,
Benjamin Kramer7761a042015-03-06 16:36:50 +0000263 PointOfInstantiation, InstantiationRange, Entity) {}
Richard Smithf623c962012-04-17 00:58:00 +0000264
Benjamin Kramer7761a042015-03-06 16:36:50 +0000265Sema::InstantiatingTemplate::InstantiatingTemplate(
Richard Smith54f18e82016-08-31 02:15:21 +0000266 Sema &SemaRef, SourceLocation PointOfInstantiation, TemplateParameter Param,
267 TemplateDecl *Template, ArrayRef<TemplateArgument> TemplateArgs,
268 SourceRange InstantiationRange)
Benjamin Kramer7761a042015-03-06 16:36:50 +0000269 : InstantiatingTemplate(
270 SemaRef,
Richard Smith696e3122017-02-23 01:43:54 +0000271 CodeSynthesisContext::DefaultTemplateArgumentInstantiation,
Richard Smith54f18e82016-08-31 02:15:21 +0000272 PointOfInstantiation, InstantiationRange, getAsNamedDecl(Param),
273 Template, TemplateArgs) {}
Douglas Gregorfcd5db32009-03-10 00:06:19 +0000274
Benjamin Kramer7761a042015-03-06 16:36:50 +0000275Sema::InstantiatingTemplate::InstantiatingTemplate(
276 Sema &SemaRef, SourceLocation PointOfInstantiation,
277 FunctionTemplateDecl *FunctionTemplate,
278 ArrayRef<TemplateArgument> TemplateArgs,
Richard Smith696e3122017-02-23 01:43:54 +0000279 CodeSynthesisContext::SynthesisKind Kind,
Benjamin Kramer7761a042015-03-06 16:36:50 +0000280 sema::TemplateDeductionInfo &DeductionInfo, SourceRange InstantiationRange)
281 : InstantiatingTemplate(SemaRef, Kind, PointOfInstantiation,
282 InstantiationRange, FunctionTemplate, nullptr,
Richard Smith54f18e82016-08-31 02:15:21 +0000283 TemplateArgs, &DeductionInfo) {
284 assert(
Richard Smith696e3122017-02-23 01:43:54 +0000285 Kind == CodeSynthesisContext::ExplicitTemplateArgumentSubstitution ||
286 Kind == CodeSynthesisContext::DeducedTemplateArgumentSubstitution);
Richard Smith54f18e82016-08-31 02:15:21 +0000287}
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000288
Benjamin Kramer7761a042015-03-06 16:36:50 +0000289Sema::InstantiatingTemplate::InstantiatingTemplate(
290 Sema &SemaRef, SourceLocation PointOfInstantiation,
Richard Smith0e617ec2016-12-27 07:56:27 +0000291 TemplateDecl *Template,
292 ArrayRef<TemplateArgument> TemplateArgs,
293 sema::TemplateDeductionInfo &DeductionInfo, SourceRange InstantiationRange)
294 : InstantiatingTemplate(
295 SemaRef,
Richard Smith696e3122017-02-23 01:43:54 +0000296 CodeSynthesisContext::DeducedTemplateArgumentSubstitution,
Richard Smith0e617ec2016-12-27 07:56:27 +0000297 PointOfInstantiation, InstantiationRange, Template, nullptr,
298 TemplateArgs, &DeductionInfo) {}
299
300Sema::InstantiatingTemplate::InstantiatingTemplate(
301 Sema &SemaRef, SourceLocation PointOfInstantiation,
Benjamin Kramer7761a042015-03-06 16:36:50 +0000302 ClassTemplatePartialSpecializationDecl *PartialSpec,
303 ArrayRef<TemplateArgument> TemplateArgs,
304 sema::TemplateDeductionInfo &DeductionInfo, SourceRange InstantiationRange)
305 : InstantiatingTemplate(
306 SemaRef,
Richard Smith696e3122017-02-23 01:43:54 +0000307 CodeSynthesisContext::DeducedTemplateArgumentSubstitution,
Benjamin Kramer7761a042015-03-06 16:36:50 +0000308 PointOfInstantiation, InstantiationRange, PartialSpec, nullptr,
309 TemplateArgs, &DeductionInfo) {}
Douglas Gregor637d9982009-06-10 23:47:09 +0000310
Larisse Voufo39a1e502013-08-06 01:03:05 +0000311Sema::InstantiatingTemplate::InstantiatingTemplate(
312 Sema &SemaRef, SourceLocation PointOfInstantiation,
313 VarTemplatePartialSpecializationDecl *PartialSpec,
314 ArrayRef<TemplateArgument> TemplateArgs,
315 sema::TemplateDeductionInfo &DeductionInfo, SourceRange InstantiationRange)
Benjamin Kramer7761a042015-03-06 16:36:50 +0000316 : InstantiatingTemplate(
317 SemaRef,
Richard Smith696e3122017-02-23 01:43:54 +0000318 CodeSynthesisContext::DeducedTemplateArgumentSubstitution,
Benjamin Kramer7761a042015-03-06 16:36:50 +0000319 PointOfInstantiation, InstantiationRange, PartialSpec, nullptr,
320 TemplateArgs, &DeductionInfo) {}
Larisse Voufo39a1e502013-08-06 01:03:05 +0000321
Benjamin Kramer7761a042015-03-06 16:36:50 +0000322Sema::InstantiatingTemplate::InstantiatingTemplate(
323 Sema &SemaRef, SourceLocation PointOfInstantiation, ParmVarDecl *Param,
324 ArrayRef<TemplateArgument> TemplateArgs, SourceRange InstantiationRange)
325 : InstantiatingTemplate(
326 SemaRef,
Richard Smith696e3122017-02-23 01:43:54 +0000327 CodeSynthesisContext::DefaultFunctionArgumentInstantiation,
Benjamin Kramer7761a042015-03-06 16:36:50 +0000328 PointOfInstantiation, InstantiationRange, Param, nullptr,
329 TemplateArgs) {}
Douglas Gregore62e6a02009-11-11 19:13:48 +0000330
Benjamin Kramer7761a042015-03-06 16:36:50 +0000331Sema::InstantiatingTemplate::InstantiatingTemplate(
332 Sema &SemaRef, SourceLocation PointOfInstantiation, NamedDecl *Template,
333 NonTypeTemplateParmDecl *Param, ArrayRef<TemplateArgument> TemplateArgs,
334 SourceRange InstantiationRange)
335 : InstantiatingTemplate(
336 SemaRef,
Richard Smith696e3122017-02-23 01:43:54 +0000337 CodeSynthesisContext::PriorTemplateArgumentSubstitution,
Benjamin Kramer7761a042015-03-06 16:36:50 +0000338 PointOfInstantiation, InstantiationRange, Param, Template,
339 TemplateArgs) {}
Stephan Tolksdorf232670f2014-03-13 20:34:22 +0000340
Benjamin Kramer7761a042015-03-06 16:36:50 +0000341Sema::InstantiatingTemplate::InstantiatingTemplate(
342 Sema &SemaRef, SourceLocation PointOfInstantiation, NamedDecl *Template,
343 TemplateTemplateParmDecl *Param, ArrayRef<TemplateArgument> TemplateArgs,
344 SourceRange InstantiationRange)
345 : InstantiatingTemplate(
346 SemaRef,
Richard Smith696e3122017-02-23 01:43:54 +0000347 CodeSynthesisContext::PriorTemplateArgumentSubstitution,
Benjamin Kramer7761a042015-03-06 16:36:50 +0000348 PointOfInstantiation, InstantiationRange, Param, Template,
349 TemplateArgs) {}
Douglas Gregore62e6a02009-11-11 19:13:48 +0000350
Benjamin Kramer7761a042015-03-06 16:36:50 +0000351Sema::InstantiatingTemplate::InstantiatingTemplate(
352 Sema &SemaRef, SourceLocation PointOfInstantiation, TemplateDecl *Template,
353 NamedDecl *Param, ArrayRef<TemplateArgument> TemplateArgs,
354 SourceRange InstantiationRange)
355 : InstantiatingTemplate(
Richard Smith696e3122017-02-23 01:43:54 +0000356 SemaRef, CodeSynthesisContext::DefaultTemplateArgumentChecking,
Benjamin Kramer7761a042015-03-06 16:36:50 +0000357 PointOfInstantiation, InstantiationRange, Param, Template,
358 TemplateArgs) {}
Anders Carlsson657bad42009-09-05 05:14:19 +0000359
Richard Smith13381222017-02-23 21:43:43 +0000360void Sema::pushCodeSynthesisContext(CodeSynthesisContext Ctx) {
361 Ctx.SavedInNonInstantiationSFINAEContext = InNonInstantiationSFINAEContext;
362 InNonInstantiationSFINAEContext = false;
363
364 CodeSynthesisContexts.push_back(Ctx);
365
366 if (!Ctx.isInstantiationRecord())
367 ++NonInstantiationEntries;
368}
369
370void Sema::popCodeSynthesisContext() {
371 auto &Active = CodeSynthesisContexts.back();
372 if (!Active.isInstantiationRecord()) {
373 assert(NonInstantiationEntries > 0);
374 --NonInstantiationEntries;
375 }
376
377 InNonInstantiationSFINAEContext = Active.SavedInNonInstantiationSFINAEContext;
378
379 // Name lookup no longer looks in this template's defining module.
380 assert(CodeSynthesisContexts.size() >=
381 CodeSynthesisContextLookupModules.size() &&
382 "forgot to remove a lookup module for a template instantiation");
383 if (CodeSynthesisContexts.size() ==
384 CodeSynthesisContextLookupModules.size()) {
385 if (Module *M = CodeSynthesisContextLookupModules.back())
386 LookupModulesCache.erase(M);
387 CodeSynthesisContextLookupModules.pop_back();
388 }
389
390 // If we've left the code synthesis context for the current context stack,
391 // stop remembering that we've emitted that stack.
392 if (CodeSynthesisContexts.size() ==
393 LastEmittedCodeSynthesisContextDepth)
394 LastEmittedCodeSynthesisContextDepth = 0;
395
396 CodeSynthesisContexts.pop_back();
397}
398
Douglas Gregor85673582009-05-18 17:01:57 +0000399void Sema::InstantiatingTemplate::Clear() {
400 if (!Invalid) {
Richard Smith13381222017-02-23 21:43:43 +0000401 if (!AlreadyInstantiating) {
402 auto &Active = SemaRef.CodeSynthesisContexts.back();
Richard Smith54f18e82016-08-31 02:15:21 +0000403 SemaRef.InstantiatingSpecializations.erase(
404 std::make_pair(Active.Entity, Active.Kind));
Richard Smith13381222017-02-23 21:43:43 +0000405 }
Richard Smith54f18e82016-08-31 02:15:21 +0000406
Gabor Horvath207e7b12018-02-10 14:04:45 +0000407 atTemplateEnd(SemaRef.TemplateInstCallbacks, SemaRef,
408 SemaRef.CodeSynthesisContexts.back());
Richard Smith13381222017-02-23 21:43:43 +0000409
Gabor Horvath207e7b12018-02-10 14:04:45 +0000410 SemaRef.popCodeSynthesisContext();
Douglas Gregor85673582009-05-18 17:01:57 +0000411 Invalid = true;
412 }
Douglas Gregorfcd5db32009-03-10 00:06:19 +0000413}
414
Douglas Gregor79cf6032009-03-10 20:44:00 +0000415bool Sema::InstantiatingTemplate::CheckInstantiationDepth(
416 SourceLocation PointOfInstantiation,
417 SourceRange InstantiationRange) {
Douglas Gregor84d49a22009-11-11 21:54:23 +0000418 assert(SemaRef.NonInstantiationEntries <=
Richard Smith696e3122017-02-23 01:43:54 +0000419 SemaRef.CodeSynthesisContexts.size());
Fangrui Song6907ce22018-07-30 19:24:48 +0000420 if ((SemaRef.CodeSynthesisContexts.size() -
Douglas Gregor84d49a22009-11-11 21:54:23 +0000421 SemaRef.NonInstantiationEntries)
David Blaikiebbafb8a2012-03-11 07:00:24 +0000422 <= SemaRef.getLangOpts().InstantiationDepth)
Douglas Gregor79cf6032009-03-10 20:44:00 +0000423 return false;
424
Mike Stump11289f42009-09-09 15:08:12 +0000425 SemaRef.Diag(PointOfInstantiation,
Douglas Gregor79cf6032009-03-10 20:44:00 +0000426 diag::err_template_recursion_depth_exceeded)
David Blaikiebbafb8a2012-03-11 07:00:24 +0000427 << SemaRef.getLangOpts().InstantiationDepth
Douglas Gregor79cf6032009-03-10 20:44:00 +0000428 << InstantiationRange;
429 SemaRef.Diag(PointOfInstantiation, diag::note_template_recursion_depth)
David Blaikiebbafb8a2012-03-11 07:00:24 +0000430 << SemaRef.getLangOpts().InstantiationDepth;
Douglas Gregor79cf6032009-03-10 20:44:00 +0000431 return true;
432}
433
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000434/// Prints the current instantiation stack through a series of
Douglas Gregor4ea568f2009-03-10 18:03:33 +0000435/// notes.
436void Sema::PrintInstantiationStack() {
Douglas Gregorffed1cb2010-04-20 07:18:24 +0000437 // Determine which template instantiations to skip, if any.
Richard Smith696e3122017-02-23 01:43:54 +0000438 unsigned SkipStart = CodeSynthesisContexts.size(), SkipEnd = SkipStart;
Douglas Gregorffed1cb2010-04-20 07:18:24 +0000439 unsigned Limit = Diags.getTemplateBacktraceLimit();
Richard Smith696e3122017-02-23 01:43:54 +0000440 if (Limit && Limit < CodeSynthesisContexts.size()) {
Douglas Gregorffed1cb2010-04-20 07:18:24 +0000441 SkipStart = Limit / 2 + Limit % 2;
Richard Smith696e3122017-02-23 01:43:54 +0000442 SkipEnd = CodeSynthesisContexts.size() - Limit / 2;
Douglas Gregorffed1cb2010-04-20 07:18:24 +0000443 }
444
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000445 // FIXME: In all of these cases, we need to show the template arguments
Douglas Gregorffed1cb2010-04-20 07:18:24 +0000446 unsigned InstantiationIdx = 0;
Richard Smith696e3122017-02-23 01:43:54 +0000447 for (SmallVectorImpl<CodeSynthesisContext>::reverse_iterator
448 Active = CodeSynthesisContexts.rbegin(),
449 ActiveEnd = CodeSynthesisContexts.rend();
Douglas Gregor4ea568f2009-03-10 18:03:33 +0000450 Active != ActiveEnd;
Douglas Gregorffed1cb2010-04-20 07:18:24 +0000451 ++Active, ++InstantiationIdx) {
452 // Skip this instantiation?
453 if (InstantiationIdx >= SkipStart && InstantiationIdx < SkipEnd) {
454 if (InstantiationIdx == SkipStart) {
455 // Note that we're skipping instantiations.
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000456 Diags.Report(Active->PointOfInstantiation,
Douglas Gregorffed1cb2010-04-20 07:18:24 +0000457 diag::note_instantiation_contexts_suppressed)
Richard Smith696e3122017-02-23 01:43:54 +0000458 << unsigned(CodeSynthesisContexts.size() - Limit);
Douglas Gregorffed1cb2010-04-20 07:18:24 +0000459 }
460 continue;
461 }
462
Douglas Gregor79cf6032009-03-10 20:44:00 +0000463 switch (Active->Kind) {
Richard Smith696e3122017-02-23 01:43:54 +0000464 case CodeSynthesisContext::TemplateInstantiation: {
Nick Lewyckycc8990f2012-11-16 08:40:59 +0000465 Decl *D = Active->Entity;
Douglas Gregor85673582009-05-18 17:01:57 +0000466 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) {
467 unsigned DiagID = diag::note_template_member_class_here;
468 if (isa<ClassTemplateSpecializationDecl>(Record))
469 DiagID = diag::note_template_class_instantiation_here;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000470 Diags.Report(Active->PointOfInstantiation, DiagID)
Richard Smith792c22d2016-12-24 04:09:05 +0000471 << Record << Active->InstantiationRange;
Douglas Gregora6ef8f02009-07-24 20:34:43 +0000472 } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
Douglas Gregor4adbc6d2009-06-26 00:10:03 +0000473 unsigned DiagID;
474 if (Function->getPrimaryTemplate())
475 DiagID = diag::note_function_template_spec_here;
476 else
477 DiagID = diag::note_template_member_function_here;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000478 Diags.Report(Active->PointOfInstantiation, DiagID)
Douglas Gregor85673582009-05-18 17:01:57 +0000479 << Function
480 << Active->InstantiationRange;
Richard Smith3f1b5d02011-05-05 21:57:07 +0000481 } else if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000482 Diags.Report(Active->PointOfInstantiation,
Larisse Voufodbd65772013-08-14 20:15:02 +0000483 VD->isStaticDataMember()?
484 diag::note_template_static_data_member_def_here
485 : diag::note_template_variable_def_here)
Richard Smith3f1b5d02011-05-05 21:57:07 +0000486 << VD
487 << Active->InstantiationRange;
Richard Smith4b38ded2012-03-14 23:13:10 +0000488 } else if (EnumDecl *ED = dyn_cast<EnumDecl>(D)) {
489 Diags.Report(Active->PointOfInstantiation,
490 diag::note_template_enum_def_here)
491 << ED
492 << Active->InstantiationRange;
Reid Klecknerd60b82f2014-11-17 23:36:45 +0000493 } else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
494 Diags.Report(Active->PointOfInstantiation,
495 diag::note_template_nsdmi_here)
496 << FD << Active->InstantiationRange;
Richard Smith3f1b5d02011-05-05 21:57:07 +0000497 } else {
498 Diags.Report(Active->PointOfInstantiation,
499 diag::note_template_type_alias_instantiation_here)
500 << cast<TypeAliasTemplateDecl>(D)
Douglas Gregora6ef8f02009-07-24 20:34:43 +0000501 << Active->InstantiationRange;
Douglas Gregor85673582009-05-18 17:01:57 +0000502 }
Douglas Gregor79cf6032009-03-10 20:44:00 +0000503 break;
504 }
505
Richard Smith696e3122017-02-23 01:43:54 +0000506 case CodeSynthesisContext::DefaultTemplateArgumentInstantiation: {
Richard Smith54f18e82016-08-31 02:15:21 +0000507 TemplateDecl *Template = cast<TemplateDecl>(Active->Template);
Benjamin Kramer9170e912013-02-22 15:46:01 +0000508 SmallVector<char, 128> TemplateArgsStr;
509 llvm::raw_svector_ostream OS(TemplateArgsStr);
510 Template->printName(OS);
Serge Pavlov03e672c2017-11-28 16:14:14 +0000511 printTemplateArgumentList(OS, Active->template_arguments(),
512 getPrintingPolicy());
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000513 Diags.Report(Active->PointOfInstantiation,
Douglas Gregor79cf6032009-03-10 20:44:00 +0000514 diag::note_default_arg_instantiation_here)
Benjamin Kramer9170e912013-02-22 15:46:01 +0000515 << OS.str()
Douglas Gregor79cf6032009-03-10 20:44:00 +0000516 << Active->InstantiationRange;
517 break;
518 }
Douglas Gregor637d9982009-06-10 23:47:09 +0000519
Richard Smith696e3122017-02-23 01:43:54 +0000520 case CodeSynthesisContext::ExplicitTemplateArgumentSubstitution: {
Nick Lewyckycc8990f2012-11-16 08:40:59 +0000521 FunctionTemplateDecl *FnTmpl = cast<FunctionTemplateDecl>(Active->Entity);
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000522 Diags.Report(Active->PointOfInstantiation,
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000523 diag::note_explicit_template_arg_substitution_here)
Fangrui Song6907ce22018-07-30 19:24:48 +0000524 << FnTmpl
525 << getTemplateArgumentBindingsText(FnTmpl->getTemplateParameters(),
526 Active->TemplateArgs,
Douglas Gregor607f1412010-03-30 20:35:20 +0000527 Active->NumTemplateArgs)
528 << Active->InstantiationRange;
Douglas Gregor637d9982009-06-10 23:47:09 +0000529 break;
530 }
Mike Stump11289f42009-09-09 15:08:12 +0000531
Richard Smith696e3122017-02-23 01:43:54 +0000532 case CodeSynthesisContext::DeducedTemplateArgumentSubstitution: {
Richard Smithe68a38f2016-12-24 04:20:31 +0000533 if (FunctionTemplateDecl *FnTmpl =
534 dyn_cast<FunctionTemplateDecl>(Active->Entity)) {
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000535 Diags.Report(Active->PointOfInstantiation,
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000536 diag::note_function_template_deduction_instantiation_here)
Douglas Gregor607f1412010-03-30 20:35:20 +0000537 << FnTmpl
Fangrui Song6907ce22018-07-30 19:24:48 +0000538 << getTemplateArgumentBindingsText(FnTmpl->getTemplateParameters(),
539 Active->TemplateArgs,
Douglas Gregor607f1412010-03-30 20:35:20 +0000540 Active->NumTemplateArgs)
541 << Active->InstantiationRange;
Richard Smithe68a38f2016-12-24 04:20:31 +0000542 } else {
543 bool IsVar = isa<VarTemplateDecl>(Active->Entity) ||
544 isa<VarTemplateSpecializationDecl>(Active->Entity);
Richard Smith0e617ec2016-12-27 07:56:27 +0000545 bool IsTemplate = false;
Richard Smithe68a38f2016-12-24 04:20:31 +0000546 TemplateParameterList *Params;
Richard Smith0e617ec2016-12-27 07:56:27 +0000547 if (auto *D = dyn_cast<TemplateDecl>(Active->Entity)) {
548 IsTemplate = true;
549 Params = D->getTemplateParameters();
550 } else if (auto *D = dyn_cast<ClassTemplatePartialSpecializationDecl>(
Richard Smithe68a38f2016-12-24 04:20:31 +0000551 Active->Entity)) {
552 Params = D->getTemplateParameters();
553 } else if (auto *D = dyn_cast<VarTemplatePartialSpecializationDecl>(
554 Active->Entity)) {
555 Params = D->getTemplateParameters();
556 } else {
557 llvm_unreachable("unexpected template kind");
558 }
559
Richard Smithe68a38f2016-12-24 04:20:31 +0000560 Diags.Report(Active->PointOfInstantiation,
561 diag::note_deduced_template_arg_substitution_here)
Richard Smith0e617ec2016-12-27 07:56:27 +0000562 << IsVar << IsTemplate << cast<NamedDecl>(Active->Entity)
Fangrui Song6907ce22018-07-30 19:24:48 +0000563 << getTemplateArgumentBindingsText(Params, Active->TemplateArgs,
Richard Smithe68a38f2016-12-24 04:20:31 +0000564 Active->NumTemplateArgs)
565 << Active->InstantiationRange;
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000566 }
567 break;
Richard Smithe68a38f2016-12-24 04:20:31 +0000568 }
Douglas Gregor637d9982009-06-10 23:47:09 +0000569
Richard Smith696e3122017-02-23 01:43:54 +0000570 case CodeSynthesisContext::DefaultFunctionArgumentInstantiation: {
Nick Lewyckycc8990f2012-11-16 08:40:59 +0000571 ParmVarDecl *Param = cast<ParmVarDecl>(Active->Entity);
Anders Carlsson657bad42009-09-05 05:14:19 +0000572 FunctionDecl *FD = cast<FunctionDecl>(Param->getDeclContext());
Mike Stump11289f42009-09-09 15:08:12 +0000573
Benjamin Kramer9170e912013-02-22 15:46:01 +0000574 SmallVector<char, 128> TemplateArgsStr;
575 llvm::raw_svector_ostream OS(TemplateArgsStr);
576 FD->printName(OS);
Serge Pavlov03e672c2017-11-28 16:14:14 +0000577 printTemplateArgumentList(OS, Active->template_arguments(),
578 getPrintingPolicy());
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000579 Diags.Report(Active->PointOfInstantiation,
Anders Carlsson657bad42009-09-05 05:14:19 +0000580 diag::note_default_function_arg_instantiation_here)
Benjamin Kramer9170e912013-02-22 15:46:01 +0000581 << OS.str()
Anders Carlsson657bad42009-09-05 05:14:19 +0000582 << Active->InstantiationRange;
583 break;
584 }
Mike Stump11289f42009-09-09 15:08:12 +0000585
Richard Smith696e3122017-02-23 01:43:54 +0000586 case CodeSynthesisContext::PriorTemplateArgumentSubstitution: {
Nick Lewyckycc8990f2012-11-16 08:40:59 +0000587 NamedDecl *Parm = cast<NamedDecl>(Active->Entity);
Douglas Gregore62e6a02009-11-11 19:13:48 +0000588 std::string Name;
589 if (!Parm->getName().empty())
590 Name = std::string(" '") + Parm->getName().str() + "'";
Craig Topperc3ec1492014-05-26 06:22:03 +0000591
592 TemplateParameterList *TemplateParams = nullptr;
Douglas Gregorca4686d2011-01-04 23:35:54 +0000593 if (TemplateDecl *Template = dyn_cast<TemplateDecl>(Active->Template))
594 TemplateParams = Template->getTemplateParameters();
595 else
596 TemplateParams =
597 cast<ClassTemplatePartialSpecializationDecl>(Active->Template)
598 ->getTemplateParameters();
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000599 Diags.Report(Active->PointOfInstantiation,
Douglas Gregore62e6a02009-11-11 19:13:48 +0000600 diag::note_prior_template_arg_substitution)
601 << isa<TemplateTemplateParmDecl>(Parm)
602 << Name
Fangrui Song6907ce22018-07-30 19:24:48 +0000603 << getTemplateArgumentBindingsText(TemplateParams,
604 Active->TemplateArgs,
Douglas Gregore62e6a02009-11-11 19:13:48 +0000605 Active->NumTemplateArgs)
606 << Active->InstantiationRange;
607 break;
608 }
Douglas Gregor84d49a22009-11-11 21:54:23 +0000609
Richard Smith696e3122017-02-23 01:43:54 +0000610 case CodeSynthesisContext::DefaultTemplateArgumentChecking: {
Craig Topperc3ec1492014-05-26 06:22:03 +0000611 TemplateParameterList *TemplateParams = nullptr;
Douglas Gregorca4686d2011-01-04 23:35:54 +0000612 if (TemplateDecl *Template = dyn_cast<TemplateDecl>(Active->Template))
613 TemplateParams = Template->getTemplateParameters();
614 else
615 TemplateParams =
616 cast<ClassTemplatePartialSpecializationDecl>(Active->Template)
617 ->getTemplateParameters();
618
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000619 Diags.Report(Active->PointOfInstantiation,
Douglas Gregor84d49a22009-11-11 21:54:23 +0000620 diag::note_template_default_arg_checking)
Fangrui Song6907ce22018-07-30 19:24:48 +0000621 << getTemplateArgumentBindingsText(TemplateParams,
622 Active->TemplateArgs,
Douglas Gregor84d49a22009-11-11 21:54:23 +0000623 Active->NumTemplateArgs)
624 << Active->InstantiationRange;
625 break;
626 }
Richard Smithf623c962012-04-17 00:58:00 +0000627
Richard Smith5159bbad2018-09-05 22:30:37 +0000628 case CodeSynthesisContext::ExceptionSpecEvaluation:
629 Diags.Report(Active->PointOfInstantiation,
630 diag::note_evaluating_exception_spec_here)
631 << cast<FunctionDecl>(Active->Entity);
632 break;
633
Richard Smith696e3122017-02-23 01:43:54 +0000634 case CodeSynthesisContext::ExceptionSpecInstantiation:
Richard Smithf623c962012-04-17 00:58:00 +0000635 Diags.Report(Active->PointOfInstantiation,
636 diag::note_template_exception_spec_instantiation_here)
Nick Lewyckycc8990f2012-11-16 08:40:59 +0000637 << cast<FunctionDecl>(Active->Entity)
Richard Smithf623c962012-04-17 00:58:00 +0000638 << Active->InstantiationRange;
639 break;
Richard Smith13381222017-02-23 21:43:43 +0000640
641 case CodeSynthesisContext::DeclaringSpecialMember:
642 Diags.Report(Active->PointOfInstantiation,
643 diag::note_in_declaration_of_implicit_special_member)
644 << cast<CXXRecordDecl>(Active->Entity) << Active->SpecialMember;
645 break;
Richard Smith883dbc42017-05-25 22:47:05 +0000646
Gabor Horvath207e7b12018-02-10 14:04:45 +0000647 case CodeSynthesisContext::DefiningSynthesizedFunction: {
Richard Smith883dbc42017-05-25 22:47:05 +0000648 // FIXME: For synthesized members other than special members, produce a note.
649 auto *MD = dyn_cast<CXXMethodDecl>(Active->Entity);
650 auto CSM = MD ? getSpecialMember(MD) : CXXInvalid;
651 if (CSM != CXXInvalid) {
652 Diags.Report(Active->PointOfInstantiation,
653 diag::note_member_synthesized_at)
654 << CSM << Context.getTagDeclType(MD->getParent());
655 }
656 break;
Douglas Gregor79cf6032009-03-10 20:44:00 +0000657 }
Gabor Horvath207e7b12018-02-10 14:04:45 +0000658
659 case CodeSynthesisContext::Memoization:
660 break;
661 }
Douglas Gregor4ea568f2009-03-10 18:03:33 +0000662 }
663}
664
David Blaikie05785d12013-02-20 22:23:23 +0000665Optional<TemplateDeductionInfo *> Sema::isSFINAEContext() const {
Douglas Gregoredb76852011-01-27 22:31:44 +0000666 if (InNonInstantiationSFINAEContext)
Craig Topperc3ec1492014-05-26 06:22:03 +0000667 return Optional<TemplateDeductionInfo *>(nullptr);
Douglas Gregoredb76852011-01-27 22:31:44 +0000668
Richard Smith696e3122017-02-23 01:43:54 +0000669 for (SmallVectorImpl<CodeSynthesisContext>::const_reverse_iterator
670 Active = CodeSynthesisContexts.rbegin(),
671 ActiveEnd = CodeSynthesisContexts.rend();
Douglas Gregor33834512009-06-14 07:33:30 +0000672 Active != ActiveEnd;
Fangrui Song6907ce22018-07-30 19:24:48 +0000673 ++Active)
Douglas Gregor84d49a22009-11-11 21:54:23 +0000674 {
Richard Smith13381222017-02-23 21:43:43 +0000675 switch (Active->Kind) {
Richard Smith696e3122017-02-23 01:43:54 +0000676 case CodeSynthesisContext::TemplateInstantiation:
Richard Smith72249ba2012-04-26 07:24:08 +0000677 // An instantiation of an alias template may or may not be a SFINAE
678 // context, depending on what else is on the stack.
Nick Lewyckycc8990f2012-11-16 08:40:59 +0000679 if (isa<TypeAliasTemplateDecl>(Active->Entity))
Richard Smith72249ba2012-04-26 07:24:08 +0000680 break;
Reid Kleckner4dc0b1a2018-11-01 19:54:45 +0000681 LLVM_FALLTHROUGH;
Richard Smith696e3122017-02-23 01:43:54 +0000682 case CodeSynthesisContext::DefaultFunctionArgumentInstantiation:
683 case CodeSynthesisContext::ExceptionSpecInstantiation:
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000684 // This is a template instantiation, so there is no SFINAE.
David Blaikie7a30dc52013-02-21 01:47:18 +0000685 return None;
Mike Stump11289f42009-09-09 15:08:12 +0000686
Richard Smith696e3122017-02-23 01:43:54 +0000687 case CodeSynthesisContext::DefaultTemplateArgumentInstantiation:
688 case CodeSynthesisContext::PriorTemplateArgumentSubstitution:
689 case CodeSynthesisContext::DefaultTemplateArgumentChecking:
Douglas Gregore62e6a02009-11-11 19:13:48 +0000690 // A default template argument instantiation and substitution into
Fangrui Song6907ce22018-07-30 19:24:48 +0000691 // template parameters with arguments for prior parameters may or may
Douglas Gregore62e6a02009-11-11 19:13:48 +0000692 // not be a SFINAE context; look further up the stack.
Douglas Gregor33834512009-06-14 07:33:30 +0000693 break;
Mike Stump11289f42009-09-09 15:08:12 +0000694
Richard Smith696e3122017-02-23 01:43:54 +0000695 case CodeSynthesisContext::ExplicitTemplateArgumentSubstitution:
696 case CodeSynthesisContext::DeducedTemplateArgumentSubstitution:
Douglas Gregorff6cbdf2009-07-01 22:01:06 +0000697 // We're either substitution explicitly-specified template arguments
698 // or deduced template arguments, so SFINAE applies.
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +0000699 assert(Active->DeductionInfo && "Missing deduction info pointer");
700 return Active->DeductionInfo;
Richard Smith13381222017-02-23 21:43:43 +0000701
702 case CodeSynthesisContext::DeclaringSpecialMember:
Richard Smith883dbc42017-05-25 22:47:05 +0000703 case CodeSynthesisContext::DefiningSynthesizedFunction:
Richard Smith13381222017-02-23 21:43:43 +0000704 // This happens in a context unrelated to template instantiation, so
705 // there is no SFINAE.
706 return None;
Gabor Horvath207e7b12018-02-10 14:04:45 +0000707
Richard Smith5159bbad2018-09-05 22:30:37 +0000708 case CodeSynthesisContext::ExceptionSpecEvaluation:
709 // FIXME: This should not be treated as a SFINAE context, because
710 // we will cache an incorrect exception specification. However, clang
711 // bootstrap relies this! See PR31692.
712 break;
713
Gabor Horvath207e7b12018-02-10 14:04:45 +0000714 case CodeSynthesisContext::Memoization:
715 break;
Douglas Gregor33834512009-06-14 07:33:30 +0000716 }
Richard Smith13381222017-02-23 21:43:43 +0000717
718 // The inner context was transparent for SFINAE. If it occurred within a
719 // non-instantiation SFINAE context, then SFINAE applies.
720 if (Active->SavedInNonInstantiationSFINAEContext)
721 return Optional<TemplateDeductionInfo *>(nullptr);
Douglas Gregor33834512009-06-14 07:33:30 +0000722 }
723
David Blaikie7a30dc52013-02-21 01:47:18 +0000724 return None;
Douglas Gregor33834512009-06-14 07:33:30 +0000725}
726
Douglas Gregorfe1e1102009-02-27 19:31:52 +0000727//===----------------------------------------------------------------------===/
728// Template Instantiation for Types
729//===----------------------------------------------------------------------===/
Douglas Gregor17c0d7b2009-02-28 00:25:32 +0000730namespace {
Douglas Gregor14cf7522010-04-30 18:55:50 +0000731 class TemplateInstantiator : public TreeTransform<TemplateInstantiator> {
Douglas Gregor01afeef2009-08-28 20:31:08 +0000732 const MultiLevelTemplateArgumentList &TemplateArgs;
Douglas Gregor17c0d7b2009-02-28 00:25:32 +0000733 SourceLocation Loc;
734 DeclarationName Entity;
Douglas Gregorfe1e1102009-02-27 19:31:52 +0000735
Douglas Gregor17c0d7b2009-02-28 00:25:32 +0000736 public:
Douglas Gregorebe10102009-08-20 07:17:43 +0000737 typedef TreeTransform<TemplateInstantiator> inherited;
Mike Stump11289f42009-09-09 15:08:12 +0000738
739 TemplateInstantiator(Sema &SemaRef,
Douglas Gregor01afeef2009-08-28 20:31:08 +0000740 const MultiLevelTemplateArgumentList &TemplateArgs,
Douglas Gregord6ff3322009-08-04 16:50:30 +0000741 SourceLocation Loc,
Mike Stump11289f42009-09-09 15:08:12 +0000742 DeclarationName Entity)
743 : inherited(SemaRef), TemplateArgs(TemplateArgs), Loc(Loc),
Douglas Gregorebe10102009-08-20 07:17:43 +0000744 Entity(Entity) { }
Douglas Gregor17c0d7b2009-02-28 00:25:32 +0000745
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000746 /// Determine whether the given type \p T has already been
Douglas Gregord6ff3322009-08-04 16:50:30 +0000747 /// transformed.
748 ///
749 /// For the purposes of template instantiation, a type has already been
750 /// transformed if it is NULL or if it is not dependent.
Douglas Gregor5597ab42010-05-07 23:12:07 +0000751 bool AlreadyTransformed(QualType T);
Mike Stump11289f42009-09-09 15:08:12 +0000752
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000753 /// Returns the location of the entity being instantiated, if known.
Douglas Gregord6ff3322009-08-04 16:50:30 +0000754 SourceLocation getBaseLocation() { return Loc; }
Mike Stump11289f42009-09-09 15:08:12 +0000755
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000756 /// Returns the name of the entity being instantiated, if any.
Douglas Gregord6ff3322009-08-04 16:50:30 +0000757 DeclarationName getBaseEntity() { return Entity; }
Mike Stump11289f42009-09-09 15:08:12 +0000758
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000759 /// Sets the "base" location and entity when that
Douglas Gregoref6ab412009-10-27 06:26:26 +0000760 /// information is known based on another transformation.
761 void setBase(SourceLocation Loc, DeclarationName Entity) {
762 this->Loc = Loc;
763 this->Entity = Entity;
764 }
Douglas Gregor840bd6c2010-12-20 22:05:00 +0000765
766 bool TryExpandParameterPacks(SourceLocation EllipsisLoc,
767 SourceRange PatternRange,
Robert Wilhelm16e94b92013-08-09 18:02:13 +0000768 ArrayRef<UnexpandedParameterPack> Unexpanded,
769 bool &ShouldExpand, bool &RetainExpansion,
David Blaikie05785d12013-02-20 22:23:23 +0000770 Optional<unsigned> &NumExpansions) {
Fangrui Song6907ce22018-07-30 19:24:48 +0000771 return getSema().CheckParameterPacksForExpansion(EllipsisLoc,
Douglas Gregor76aca7b2010-12-21 00:52:54 +0000772 PatternRange, Unexpanded,
Fangrui Song6907ce22018-07-30 19:24:48 +0000773 TemplateArgs,
Douglas Gregor76aca7b2010-12-21 00:52:54 +0000774 ShouldExpand,
Douglas Gregora8bac7f2011-01-10 07:32:04 +0000775 RetainExpansion,
Douglas Gregor76aca7b2010-12-21 00:52:54 +0000776 NumExpansions);
777 }
Douglas Gregor840bd6c2010-12-20 22:05:00 +0000778
Fangrui Song6907ce22018-07-30 19:24:48 +0000779 void ExpandingFunctionParameterPack(ParmVarDecl *Pack) {
Douglas Gregorf3010112011-01-07 16:43:16 +0000780 SemaRef.CurrentInstantiationScope->MakeInstantiatedLocalArgPack(Pack);
781 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000782
Douglas Gregora8bac7f2011-01-10 07:32:04 +0000783 TemplateArgument ForgetPartiallySubstitutedPack() {
784 TemplateArgument Result;
785 if (NamedDecl *PartialPack
786 = SemaRef.CurrentInstantiationScope->getPartiallySubstitutedPack()){
787 MultiLevelTemplateArgumentList &TemplateArgs
788 = const_cast<MultiLevelTemplateArgumentList &>(this->TemplateArgs);
789 unsigned Depth, Index;
Benjamin Kramer867ea1d2014-03-02 13:01:17 +0000790 std::tie(Depth, Index) = getDepthAndIndex(PartialPack);
Douglas Gregora8bac7f2011-01-10 07:32:04 +0000791 if (TemplateArgs.hasTemplateArgument(Depth, Index)) {
792 Result = TemplateArgs(Depth, Index);
793 TemplateArgs.setArgument(Depth, Index, TemplateArgument());
794 }
795 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000796
Douglas Gregora8bac7f2011-01-10 07:32:04 +0000797 return Result;
798 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000799
Douglas Gregora8bac7f2011-01-10 07:32:04 +0000800 void RememberPartiallySubstitutedPack(TemplateArgument Arg) {
801 if (Arg.isNull())
802 return;
Fangrui Song6907ce22018-07-30 19:24:48 +0000803
Douglas Gregora8bac7f2011-01-10 07:32:04 +0000804 if (NamedDecl *PartialPack
805 = SemaRef.CurrentInstantiationScope->getPartiallySubstitutedPack()){
806 MultiLevelTemplateArgumentList &TemplateArgs
807 = const_cast<MultiLevelTemplateArgumentList &>(this->TemplateArgs);
808 unsigned Depth, Index;
Benjamin Kramer867ea1d2014-03-02 13:01:17 +0000809 std::tie(Depth, Index) = getDepthAndIndex(PartialPack);
Douglas Gregora8bac7f2011-01-10 07:32:04 +0000810 TemplateArgs.setArgument(Depth, Index, Arg);
811 }
812 }
813
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000814 /// Transform the given declaration by instantiating a reference to
Douglas Gregord6ff3322009-08-04 16:50:30 +0000815 /// this declaration.
Douglas Gregora04f2ca2010-03-01 15:56:25 +0000816 Decl *TransformDecl(SourceLocation Loc, Decl *D);
Douglas Gregora16548e2009-08-11 05:31:07 +0000817
Fangrui Song6907ce22018-07-30 19:24:48 +0000818 void transformAttrs(Decl *Old, Decl *New) {
Douglas Gregor0c46b2b2012-02-13 22:00:16 +0000819 SemaRef.InstantiateAttrs(TemplateArgs, Old, New);
820 }
821
822 void transformedLocalDecl(Decl *Old, Decl *New) {
Richard Smithc38498f2015-04-27 21:27:54 +0000823 // If we've instantiated the call operator of a lambda or the call
824 // operator template of a generic lambda, update the "instantiation of"
825 // information.
826 auto *NewMD = dyn_cast<CXXMethodDecl>(New);
827 if (NewMD && isLambdaCallOperator(NewMD)) {
828 auto *OldMD = dyn_cast<CXXMethodDecl>(Old);
829 if (auto *NewTD = NewMD->getDescribedFunctionTemplate())
830 NewTD->setInstantiatedFromMemberTemplate(
831 OldMD->getDescribedFunctionTemplate());
832 else
833 NewMD->setInstantiationOfMemberFunction(OldMD,
834 TSK_ImplicitInstantiation);
835 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000836
Douglas Gregor0c46b2b2012-02-13 22:00:16 +0000837 SemaRef.CurrentInstantiationScope->InstantiatedLocal(Old, New);
Richard Smithc7649dc2016-03-23 20:07:07 +0000838
839 // We recreated a local declaration, but not by instantiating it. There
840 // may be pending dependent diagnostics to produce.
841 if (auto *DC = dyn_cast<DeclContext>(Old))
842 SemaRef.PerformDependentDiagnostics(DC, TemplateArgs);
Douglas Gregor0c46b2b2012-02-13 22:00:16 +0000843 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000844
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000845 /// Transform the definition of the given declaration by
Douglas Gregorebe10102009-08-20 07:17:43 +0000846 /// instantiating it.
Douglas Gregor25289362010-03-01 17:25:41 +0000847 Decl *TransformDefinition(SourceLocation Loc, Decl *D);
Mike Stump11289f42009-09-09 15:08:12 +0000848
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000849 /// Transform the first qualifier within a scope by instantiating the
Douglas Gregora5cb6da2009-10-20 05:58:46 +0000850 /// declaration.
851 NamedDecl *TransformFirstQualifierInScope(NamedDecl *D, SourceLocation Loc);
Fangrui Song6907ce22018-07-30 19:24:48 +0000852
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000853 /// Rebuild the exception declaration and register the declaration
Douglas Gregorebe10102009-08-20 07:17:43 +0000854 /// as an instantiated local.
Fangrui Song6907ce22018-07-30 19:24:48 +0000855 VarDecl *RebuildExceptionDecl(VarDecl *ExceptionDecl,
John McCallbcd03502009-12-07 02:54:59 +0000856 TypeSourceInfo *Declarator,
Abramo Bagnaradff19302011-03-08 08:55:46 +0000857 SourceLocation StartLoc,
858 SourceLocation NameLoc,
859 IdentifierInfo *Name);
Mike Stump11289f42009-09-09 15:08:12 +0000860
Fangrui Song6907ce22018-07-30 19:24:48 +0000861 /// Rebuild the Objective-C exception declaration and register the
Douglas Gregorf4e837f2010-04-26 17:57:08 +0000862 /// declaration as an instantiated local.
Fangrui Song6907ce22018-07-30 19:24:48 +0000863 VarDecl *RebuildObjCExceptionDecl(VarDecl *ExceptionDecl,
Douglas Gregorf4e837f2010-04-26 17:57:08 +0000864 TypeSourceInfo *TSInfo, QualType T);
Fangrui Song6907ce22018-07-30 19:24:48 +0000865
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000866 /// Check for tag mismatches when instantiating an
John McCall7f41d982009-09-11 04:59:25 +0000867 /// elaborated type.
John McCall954b5de2010-11-04 19:04:38 +0000868 QualType RebuildElaboratedType(SourceLocation KeywordLoc,
869 ElaboratedTypeKeyword Keyword,
Douglas Gregor844cb502011-03-01 18:12:44 +0000870 NestedNameSpecifierLoc QualifierLoc,
871 QualType T);
John McCall7f41d982009-09-11 04:59:25 +0000872
Craig Topperc3ec1492014-05-26 06:22:03 +0000873 TemplateName
874 TransformTemplateName(CXXScopeSpec &SS, TemplateName Name,
875 SourceLocation NameLoc,
876 QualType ObjectType = QualType(),
Richard Smithfd3dae02017-01-20 00:20:39 +0000877 NamedDecl *FirstQualifierInScope = nullptr,
878 bool AllowInjectedClassName = false);
Douglas Gregor9db53502011-03-02 18:07:45 +0000879
Tyler Nowickic724a83e2014-10-12 20:46:07 +0000880 const LoopHintAttr *TransformLoopHintAttr(const LoopHintAttr *LH);
881
John McCalldadc5752010-08-24 06:29:42 +0000882 ExprResult TransformPredefinedExpr(PredefinedExpr *E);
883 ExprResult TransformDeclRefExpr(DeclRefExpr *E);
884 ExprResult TransformCXXDefaultArgExpr(CXXDefaultArgExpr *E);
Richard Smithb15fe3a2012-09-12 00:56:43 +0000885
John McCalldadc5752010-08-24 06:29:42 +0000886 ExprResult TransformTemplateParmRefExpr(DeclRefExpr *E,
Douglas Gregoreb5a39d2010-12-24 00:15:10 +0000887 NonTypeTemplateParmDecl *D);
Douglas Gregorcdbc5392011-01-15 01:15:58 +0000888 ExprResult TransformSubstNonTypeTemplateParmPackExpr(
889 SubstNonTypeTemplateParmPackExpr *E);
Richard Smithb15fe3a2012-09-12 00:56:43 +0000890
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000891 /// Rebuild a DeclRefExpr for a ParmVarDecl reference.
Richard Smithb15fe3a2012-09-12 00:56:43 +0000892 ExprResult RebuildParmVarDeclRefExpr(ParmVarDecl *PD, SourceLocation Loc);
893
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000894 /// Transform a reference to a function parameter pack.
Richard Smithb15fe3a2012-09-12 00:56:43 +0000895 ExprResult TransformFunctionParmPackRefExpr(DeclRefExpr *E,
896 ParmVarDecl *PD);
897
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000898 /// Transform a FunctionParmPackExpr which was built when we couldn't
Richard Smithb15fe3a2012-09-12 00:56:43 +0000899 /// expand a function parameter pack reference which refers to an expanded
900 /// pack.
901 ExprResult TransformFunctionParmPackExpr(FunctionParmPackExpr *E);
902
Hans Wennborge113c202014-09-18 16:01:32 +0000903 QualType TransformFunctionProtoType(TypeLocBuilder &TLB,
Richard Smith2e321552014-11-12 02:00:47 +0000904 FunctionProtoTypeLoc TL) {
905 // Call the base version; it will forward to our overridden version below.
906 return inherited::TransformFunctionProtoType(TLB, TL);
907 }
908
909 template<typename Fn>
Douglas Gregor3024f072012-04-16 07:05:22 +0000910 QualType TransformFunctionProtoType(TypeLocBuilder &TLB,
911 FunctionProtoTypeLoc TL,
912 CXXRecordDecl *ThisContext,
Mikael Nilsson9d2872d2018-12-13 10:15:27 +0000913 Qualifiers ThisTypeQuals,
Richard Smith2e321552014-11-12 02:00:47 +0000914 Fn TransformExceptionSpec);
Douglas Gregor3024f072012-04-16 07:05:22 +0000915
Douglas Gregor715e4612011-01-14 22:40:04 +0000916 ParmVarDecl *TransformFunctionTypeParam(ParmVarDecl *OldParm,
John McCall8fb0d9d2011-05-01 22:35:37 +0000917 int indexAdjustment,
David Blaikie05785d12013-02-20 22:23:23 +0000918 Optional<unsigned> NumExpansions,
Douglas Gregor0dd22bc2012-01-25 16:15:54 +0000919 bool ExpectParameterPack);
John McCall58f10c32010-03-11 09:03:00 +0000920
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000921 /// Transforms a template type parameter type by performing
Douglas Gregord6ff3322009-08-04 16:50:30 +0000922 /// substitution of the corresponding template type argument.
John McCall550e0c22009-10-21 00:40:46 +0000923 QualType TransformTemplateTypeParmType(TypeLocBuilder &TLB,
John McCall31f82722010-11-12 08:19:04 +0000924 TemplateTypeParmTypeLoc TL);
Nick Lewyckyc96c37f2010-07-06 19:51:49 +0000925
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000926 /// Transforms an already-substituted template type parameter pack
Douglas Gregorada4b792011-01-14 02:55:32 +0000927 /// into either itself (if we aren't substituting into its pack expansion)
928 /// or the appropriate substituted argument.
929 QualType TransformSubstTemplateTypeParmPackType(TypeLocBuilder &TLB,
930 SubstTemplateTypeParmPackTypeLoc TL);
931
Richard Smith2589b9802012-07-25 03:56:55 +0000932 ExprResult TransformLambdaExpr(LambdaExpr *E) {
933 LocalInstantiationScope Scope(SemaRef, /*CombineWithOuterScope=*/true);
934 return TreeTransform<TemplateInstantiator>::TransformLambdaExpr(E);
935 }
936
David Majnemerb1004102014-03-02 18:46:05 +0000937 TemplateParameterList *TransformTemplateParameterList(
Faisal Vali2cba1332013-10-23 06:44:28 +0000938 TemplateParameterList *OrigTPL) {
939 if (!OrigTPL || !OrigTPL->size()) return OrigTPL;
Fangrui Song6907ce22018-07-30 19:24:48 +0000940
Faisal Vali2cba1332013-10-23 06:44:28 +0000941 DeclContext *Owner = OrigTPL->getParam(0)->getDeclContext();
Fangrui Song6907ce22018-07-30 19:24:48 +0000942 TemplateDeclInstantiator DeclInstantiator(getSema(),
Faisal Vali2cba1332013-10-23 06:44:28 +0000943 /* DeclContext *Owner */ Owner, TemplateArgs);
Fangrui Song6907ce22018-07-30 19:24:48 +0000944 return DeclInstantiator.SubstTemplateParams(OrigTPL);
Faisal Vali2cba1332013-10-23 06:44:28 +0000945 }
John McCall7c454bb2011-07-15 05:09:51 +0000946 private:
947 ExprResult transformNonTypeTemplateParmRef(NonTypeTemplateParmDecl *parm,
948 SourceLocation loc,
Richard Smith34349002012-07-09 03:07:20 +0000949 TemplateArgument arg);
Douglas Gregord6ff3322009-08-04 16:50:30 +0000950 };
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000951}
Douglas Gregor04318252009-07-06 15:59:29 +0000952
Douglas Gregor5597ab42010-05-07 23:12:07 +0000953bool TemplateInstantiator::AlreadyTransformed(QualType T) {
954 if (T.isNull())
955 return true;
Fangrui Song6907ce22018-07-30 19:24:48 +0000956
Douglas Gregor678d76c2011-07-01 01:22:09 +0000957 if (T->isInstantiationDependentType() || T->isVariablyModifiedType())
Douglas Gregor5597ab42010-05-07 23:12:07 +0000958 return false;
Fangrui Song6907ce22018-07-30 19:24:48 +0000959
Douglas Gregor5597ab42010-05-07 23:12:07 +0000960 getSema().MarkDeclarationsReferencedInType(Loc, T);
961 return true;
962}
963
Eli Friedman8917ad52013-07-19 19:40:38 +0000964static TemplateArgument
965getPackSubstitutedTemplateArgument(Sema &S, TemplateArgument Arg) {
Fangrui Song6907ce22018-07-30 19:24:48 +0000966 assert(S.ArgumentPackSubstitutionIndex >= 0);
Eli Friedman8917ad52013-07-19 19:40:38 +0000967 assert(S.ArgumentPackSubstitutionIndex < (int)Arg.pack_size());
968 Arg = Arg.pack_begin()[S.ArgumentPackSubstitutionIndex];
969 if (Arg.isPackExpansion())
970 Arg = Arg.getPackExpansionPattern();
971 return Arg;
972}
973
Douglas Gregora04f2ca2010-03-01 15:56:25 +0000974Decl *TemplateInstantiator::TransformDecl(SourceLocation Loc, Decl *D) {
Douglas Gregor2b6ca462009-09-03 21:38:09 +0000975 if (!D)
Craig Topperc3ec1492014-05-26 06:22:03 +0000976 return nullptr;
Mike Stump11289f42009-09-09 15:08:12 +0000977
Douglas Gregor2b6ca462009-09-03 21:38:09 +0000978 if (TemplateTemplateParmDecl *TTP = dyn_cast<TemplateTemplateParmDecl>(D)) {
Douglas Gregor01afeef2009-08-28 20:31:08 +0000979 if (TTP->getDepth() < TemplateArgs.getNumLevels()) {
Douglas Gregorb93971082010-02-05 19:54:12 +0000980 // If the corresponding template argument is NULL or non-existent, it's
981 // because we are performing instantiation from explicitly-specified
982 // template arguments in a function template, but there were some
983 // arguments left unspecified.
984 if (!TemplateArgs.hasTemplateArgument(TTP->getDepth(),
985 TTP->getPosition()))
986 return D;
987
Douglas Gregorf5500772011-01-05 15:48:55 +0000988 TemplateArgument Arg = TemplateArgs(TTP->getDepth(), TTP->getPosition());
Fangrui Song6907ce22018-07-30 19:24:48 +0000989
Douglas Gregorf5500772011-01-05 15:48:55 +0000990 if (TTP->isParameterPack()) {
Fangrui Song6907ce22018-07-30 19:24:48 +0000991 assert(Arg.getKind() == TemplateArgument::Pack &&
Douglas Gregorf5500772011-01-05 15:48:55 +0000992 "Missing argument pack");
Eli Friedman8917ad52013-07-19 19:40:38 +0000993 Arg = getPackSubstitutedTemplateArgument(getSema(), Arg);
Douglas Gregorf5500772011-01-05 15:48:55 +0000994 }
995
Richard Smith1abacfc2017-08-29 22:14:43 +0000996 TemplateName Template = Arg.getAsTemplate().getNameToSubstitute();
Douglas Gregor9167f8b2009-11-11 01:00:40 +0000997 assert(!Template.isNull() && Template.getAsTemplateDecl() &&
Douglas Gregor01afeef2009-08-28 20:31:08 +0000998 "Wrong kind of template template argument");
Douglas Gregor9167f8b2009-11-11 01:00:40 +0000999 return Template.getAsTemplateDecl();
Douglas Gregor01afeef2009-08-28 20:31:08 +00001000 }
Mike Stump11289f42009-09-09 15:08:12 +00001001
Douglas Gregor9167f8b2009-11-11 01:00:40 +00001002 // Fall through to find the instantiated declaration for this template
1003 // template parameter.
Douglas Gregor71dc5092009-08-06 06:41:21 +00001004 }
Mike Stump11289f42009-09-09 15:08:12 +00001005
Douglas Gregora04f2ca2010-03-01 15:56:25 +00001006 return SemaRef.FindInstantiatedDecl(Loc, cast<NamedDecl>(D), TemplateArgs);
Douglas Gregord6ff3322009-08-04 16:50:30 +00001007}
1008
Douglas Gregor25289362010-03-01 17:25:41 +00001009Decl *TemplateInstantiator::TransformDefinition(SourceLocation Loc, Decl *D) {
John McCall76d824f2009-08-25 22:02:44 +00001010 Decl *Inst = getSema().SubstDecl(D, getSema().CurContext, TemplateArgs);
Douglas Gregorebe10102009-08-20 07:17:43 +00001011 if (!Inst)
Craig Topperc3ec1492014-05-26 06:22:03 +00001012 return nullptr;
Mike Stump11289f42009-09-09 15:08:12 +00001013
Douglas Gregorebe10102009-08-20 07:17:43 +00001014 getSema().CurrentInstantiationScope->InstantiatedLocal(D, Inst);
1015 return Inst;
1016}
1017
Douglas Gregora5cb6da2009-10-20 05:58:46 +00001018NamedDecl *
Fangrui Song6907ce22018-07-30 19:24:48 +00001019TemplateInstantiator::TransformFirstQualifierInScope(NamedDecl *D,
Douglas Gregora5cb6da2009-10-20 05:58:46 +00001020 SourceLocation Loc) {
Fangrui Song6907ce22018-07-30 19:24:48 +00001021 // If the first part of the nested-name-specifier was a template type
Douglas Gregora5cb6da2009-10-20 05:58:46 +00001022 // parameter, instantiate that type parameter down to a tag type.
1023 if (TemplateTypeParmDecl *TTPD = dyn_cast_or_null<TemplateTypeParmDecl>(D)) {
Fangrui Song6907ce22018-07-30 19:24:48 +00001024 const TemplateTypeParmType *TTP
Douglas Gregora5cb6da2009-10-20 05:58:46 +00001025 = cast<TemplateTypeParmType>(getSema().Context.getTypeDeclType(TTPD));
Fangrui Song6907ce22018-07-30 19:24:48 +00001026
Douglas Gregora5cb6da2009-10-20 05:58:46 +00001027 if (TTP->getDepth() < TemplateArgs.getNumLevels()) {
Douglas Gregor53c3f4e2010-12-20 22:48:17 +00001028 // FIXME: This needs testing w/ member access expressions.
1029 TemplateArgument Arg = TemplateArgs(TTP->getDepth(), TTP->getIndex());
Fangrui Song6907ce22018-07-30 19:24:48 +00001030
Douglas Gregor53c3f4e2010-12-20 22:48:17 +00001031 if (TTP->isParameterPack()) {
Fangrui Song6907ce22018-07-30 19:24:48 +00001032 assert(Arg.getKind() == TemplateArgument::Pack &&
Douglas Gregor53c3f4e2010-12-20 22:48:17 +00001033 "Missing argument pack");
Fangrui Song6907ce22018-07-30 19:24:48 +00001034
Douglas Gregore1d60df2011-01-14 23:41:42 +00001035 if (getSema().ArgumentPackSubstitutionIndex == -1)
Craig Topperc3ec1492014-05-26 06:22:03 +00001036 return nullptr;
1037
Eli Friedman8917ad52013-07-19 19:40:38 +00001038 Arg = getPackSubstitutedTemplateArgument(getSema(), Arg);
Douglas Gregor53c3f4e2010-12-20 22:48:17 +00001039 }
1040
1041 QualType T = Arg.getAsType();
Douglas Gregora5cb6da2009-10-20 05:58:46 +00001042 if (T.isNull())
Douglas Gregora04f2ca2010-03-01 15:56:25 +00001043 return cast_or_null<NamedDecl>(TransformDecl(Loc, D));
Fangrui Song6907ce22018-07-30 19:24:48 +00001044
Douglas Gregora5cb6da2009-10-20 05:58:46 +00001045 if (const TagType *Tag = T->getAs<TagType>())
1046 return Tag->getDecl();
Fangrui Song6907ce22018-07-30 19:24:48 +00001047
Douglas Gregora5cb6da2009-10-20 05:58:46 +00001048 // The resulting type is not a tag; complain.
1049 getSema().Diag(Loc, diag::err_nested_name_spec_non_tag) << T;
Craig Topperc3ec1492014-05-26 06:22:03 +00001050 return nullptr;
Douglas Gregora5cb6da2009-10-20 05:58:46 +00001051 }
1052 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001053
Douglas Gregora04f2ca2010-03-01 15:56:25 +00001054 return cast_or_null<NamedDecl>(TransformDecl(Loc, D));
Douglas Gregora5cb6da2009-10-20 05:58:46 +00001055}
1056
Douglas Gregorebe10102009-08-20 07:17:43 +00001057VarDecl *
1058TemplateInstantiator::RebuildExceptionDecl(VarDecl *ExceptionDecl,
John McCallbcd03502009-12-07 02:54:59 +00001059 TypeSourceInfo *Declarator,
Abramo Bagnaradff19302011-03-08 08:55:46 +00001060 SourceLocation StartLoc,
1061 SourceLocation NameLoc,
1062 IdentifierInfo *Name) {
Douglas Gregor9f0e1aa2010-09-09 17:09:21 +00001063 VarDecl *Var = inherited::RebuildExceptionDecl(ExceptionDecl, Declarator,
Abramo Bagnaradff19302011-03-08 08:55:46 +00001064 StartLoc, NameLoc, Name);
Douglas Gregorf4e837f2010-04-26 17:57:08 +00001065 if (Var)
1066 getSema().CurrentInstantiationScope->InstantiatedLocal(ExceptionDecl, Var);
1067 return Var;
1068}
1069
Fangrui Song6907ce22018-07-30 19:24:48 +00001070VarDecl *TemplateInstantiator::RebuildObjCExceptionDecl(VarDecl *ExceptionDecl,
1071 TypeSourceInfo *TSInfo,
Douglas Gregorf4e837f2010-04-26 17:57:08 +00001072 QualType T) {
1073 VarDecl *Var = inherited::RebuildObjCExceptionDecl(ExceptionDecl, TSInfo, T);
1074 if (Var)
Douglas Gregorebe10102009-08-20 07:17:43 +00001075 getSema().CurrentInstantiationScope->InstantiatedLocal(ExceptionDecl, Var);
1076 return Var;
1077}
1078
John McCall7f41d982009-09-11 04:59:25 +00001079QualType
John McCall954b5de2010-11-04 19:04:38 +00001080TemplateInstantiator::RebuildElaboratedType(SourceLocation KeywordLoc,
1081 ElaboratedTypeKeyword Keyword,
Douglas Gregor844cb502011-03-01 18:12:44 +00001082 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara6150c882010-05-11 21:36:43 +00001083 QualType T) {
John McCall7f41d982009-09-11 04:59:25 +00001084 if (const TagType *TT = T->getAs<TagType>()) {
1085 TagDecl* TD = TT->getDecl();
1086
John McCall954b5de2010-11-04 19:04:38 +00001087 SourceLocation TagLocation = KeywordLoc;
John McCall7f41d982009-09-11 04:59:25 +00001088
John McCall7f41d982009-09-11 04:59:25 +00001089 IdentifierInfo *Id = TD->getIdentifier();
1090
1091 // TODO: should we even warn on struct/class mismatches for this? Seems
1092 // like it's likely to produce a lot of spurious errors.
Richard Smith80b3c5a2012-08-17 00:12:27 +00001093 if (Id && Keyword != ETK_None && Keyword != ETK_Typename) {
Abramo Bagnara6150c882010-05-11 21:36:43 +00001094 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForKeyword(Keyword);
Richard Trieucaa33d32011-06-10 03:11:26 +00001095 if (!SemaRef.isAcceptableTagRedeclaration(TD, Kind, /*isDefinition*/false,
Justin Bognerc6ecb7c2015-07-10 23:05:47 +00001096 TagLocation, Id)) {
Abramo Bagnara6150c882010-05-11 21:36:43 +00001097 SemaRef.Diag(TagLocation, diag::err_use_with_wrong_tag)
1098 << Id
1099 << FixItHint::CreateReplacement(SourceRange(TagLocation),
1100 TD->getKindName());
1101 SemaRef.Diag(TD->getLocation(), diag::note_previous_use);
1102 }
John McCall7f41d982009-09-11 04:59:25 +00001103 }
1104 }
1105
John McCall954b5de2010-11-04 19:04:38 +00001106 return TreeTransform<TemplateInstantiator>::RebuildElaboratedType(KeywordLoc,
1107 Keyword,
Douglas Gregor844cb502011-03-01 18:12:44 +00001108 QualifierLoc,
1109 T);
John McCall7f41d982009-09-11 04:59:25 +00001110}
1111
Richard Smithfd3dae02017-01-20 00:20:39 +00001112TemplateName TemplateInstantiator::TransformTemplateName(
1113 CXXScopeSpec &SS, TemplateName Name, SourceLocation NameLoc,
1114 QualType ObjectType, NamedDecl *FirstQualifierInScope,
1115 bool AllowInjectedClassName) {
Douglas Gregor9db53502011-03-02 18:07:45 +00001116 if (TemplateTemplateParmDecl *TTP
1117 = dyn_cast_or_null<TemplateTemplateParmDecl>(Name.getAsTemplateDecl())) {
1118 if (TTP->getDepth() < TemplateArgs.getNumLevels()) {
1119 // If the corresponding template argument is NULL or non-existent, it's
1120 // because we are performing instantiation from explicitly-specified
1121 // template arguments in a function template, but there were some
1122 // arguments left unspecified.
1123 if (!TemplateArgs.hasTemplateArgument(TTP->getDepth(),
1124 TTP->getPosition()))
1125 return Name;
Fangrui Song6907ce22018-07-30 19:24:48 +00001126
Douglas Gregor9db53502011-03-02 18:07:45 +00001127 TemplateArgument Arg = TemplateArgs(TTP->getDepth(), TTP->getPosition());
Fangrui Song6907ce22018-07-30 19:24:48 +00001128
Douglas Gregor9db53502011-03-02 18:07:45 +00001129 if (TTP->isParameterPack()) {
Fangrui Song6907ce22018-07-30 19:24:48 +00001130 assert(Arg.getKind() == TemplateArgument::Pack &&
Douglas Gregor9db53502011-03-02 18:07:45 +00001131 "Missing argument pack");
Fangrui Song6907ce22018-07-30 19:24:48 +00001132
Douglas Gregor9db53502011-03-02 18:07:45 +00001133 if (getSema().ArgumentPackSubstitutionIndex == -1) {
1134 // We have the template argument pack to substitute, but we're not
1135 // actually expanding the enclosing pack expansion yet. So, just
1136 // keep the entire argument pack.
1137 return getSema().Context.getSubstTemplateTemplateParmPack(TTP, Arg);
1138 }
Eli Friedman8917ad52013-07-19 19:40:38 +00001139
1140 Arg = getPackSubstitutedTemplateArgument(getSema(), Arg);
Douglas Gregor9db53502011-03-02 18:07:45 +00001141 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001142
Richard Smith1abacfc2017-08-29 22:14:43 +00001143 TemplateName Template = Arg.getAsTemplate().getNameToSubstitute();
Richard Smith3f1b5d02011-05-05 21:57:07 +00001144 assert(!Template.isNull() && "Null template template argument");
Richard Smith1abacfc2017-08-29 22:14:43 +00001145 assert(!Template.getAsQualifiedTemplateName() &&
1146 "template decl to substitute is qualified?");
John McCalld9dfe3a2011-06-30 08:33:18 +00001147
1148 Template = getSema().Context.getSubstTemplateTemplateParm(TTP, Template);
Douglas Gregor9db53502011-03-02 18:07:45 +00001149 return Template;
1150 }
1151 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001152
Douglas Gregor9db53502011-03-02 18:07:45 +00001153 if (SubstTemplateTemplateParmPackStorage *SubstPack
1154 = Name.getAsSubstTemplateTemplateParmPack()) {
1155 if (getSema().ArgumentPackSubstitutionIndex == -1)
1156 return Name;
Fangrui Song6907ce22018-07-30 19:24:48 +00001157
Eli Friedman8917ad52013-07-19 19:40:38 +00001158 TemplateArgument Arg = SubstPack->getArgumentPack();
1159 Arg = getPackSubstitutedTemplateArgument(getSema(), Arg);
Richard Smith1abacfc2017-08-29 22:14:43 +00001160 return Arg.getAsTemplate().getNameToSubstitute();
Douglas Gregor9db53502011-03-02 18:07:45 +00001161 }
Richard Smithfd3dae02017-01-20 00:20:39 +00001162
1163 return inherited::TransformTemplateName(SS, Name, NameLoc, ObjectType,
1164 FirstQualifierInScope,
1165 AllowInjectedClassName);
Douglas Gregor9db53502011-03-02 18:07:45 +00001166}
1167
Fangrui Song6907ce22018-07-30 19:24:48 +00001168ExprResult
John McCall47f29ea2009-12-08 09:21:05 +00001169TemplateInstantiator::TransformPredefinedExpr(PredefinedExpr *E) {
Anders Carlsson0b209a82009-09-11 01:22:35 +00001170 if (!E->isTypeDependent())
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00001171 return E;
Anders Carlsson0b209a82009-09-11 01:22:35 +00001172
Bruno Ricci17ff0262018-10-27 19:21:19 +00001173 return getSema().BuildPredefinedExpr(E->getLocation(), E->getIdentKind());
Anders Carlsson0b209a82009-09-11 01:22:35 +00001174}
1175
John McCalldadc5752010-08-24 06:29:42 +00001176ExprResult
John McCall13481c52010-02-06 08:42:39 +00001177TemplateInstantiator::TransformTemplateParmRefExpr(DeclRefExpr *E,
Douglas Gregor6c379e22010-02-08 23:41:45 +00001178 NonTypeTemplateParmDecl *NTTP) {
John McCall13481c52010-02-06 08:42:39 +00001179 // If the corresponding template argument is NULL or non-existent, it's
1180 // because we are performing instantiation from explicitly-specified
1181 // template arguments in a function template, but there were some
1182 // arguments left unspecified.
1183 if (!TemplateArgs.hasTemplateArgument(NTTP->getDepth(),
1184 NTTP->getPosition()))
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00001185 return E;
Mike Stump11289f42009-09-09 15:08:12 +00001186
Douglas Gregoreb5a39d2010-12-24 00:15:10 +00001187 TemplateArgument Arg = TemplateArgs(NTTP->getDepth(), NTTP->getPosition());
Richard Smithb4f96252017-02-21 06:30:38 +00001188
1189 if (TemplateArgs.getNumLevels() != TemplateArgs.getNumSubstitutedLevels()) {
1190 // We're performing a partial substitution, so the substituted argument
1191 // could be dependent. As a result we can't create a SubstNonType*Expr
1192 // node now, since that represents a fully-substituted argument.
1193 // FIXME: We should have some AST representation for this.
1194 if (Arg.getKind() == TemplateArgument::Pack) {
1195 // FIXME: This won't work for alias templates.
1196 assert(Arg.pack_size() == 1 && Arg.pack_begin()->isPackExpansion() &&
1197 "unexpected pack arguments in partial substitution");
1198 Arg = Arg.pack_begin()->getPackExpansionPattern();
1199 }
1200 assert(Arg.getKind() == TemplateArgument::Expression &&
1201 "unexpected nontype template argument kind in partial substitution");
1202 return Arg.getAsExpr();
1203 }
1204
Douglas Gregoreb5a39d2010-12-24 00:15:10 +00001205 if (NTTP->isParameterPack()) {
Fangrui Song6907ce22018-07-30 19:24:48 +00001206 assert(Arg.getKind() == TemplateArgument::Pack &&
Douglas Gregoreb5a39d2010-12-24 00:15:10 +00001207 "Missing argument pack");
Fangrui Song6907ce22018-07-30 19:24:48 +00001208
Douglas Gregoreb5a39d2010-12-24 00:15:10 +00001209 if (getSema().ArgumentPackSubstitutionIndex == -1) {
Douglas Gregorcdbc5392011-01-15 01:15:58 +00001210 // We have an argument pack, but we can't select a particular argument
1211 // out of it yet. Therefore, we'll build an expression to hold on to that
1212 // argument pack.
1213 QualType TargetType = SemaRef.SubstType(NTTP->getType(), TemplateArgs,
Fangrui Song6907ce22018-07-30 19:24:48 +00001214 E->getLocation(),
Douglas Gregorcdbc5392011-01-15 01:15:58 +00001215 NTTP->getDeclName());
1216 if (TargetType.isNull())
1217 return ExprError();
Richard Smithf1f20e62018-02-14 02:07:53 +00001218
1219 return new (SemaRef.Context) SubstNonTypeTemplateParmPackExpr(
1220 TargetType.getNonLValueExprType(SemaRef.Context),
1221 TargetType->isReferenceType() ? VK_LValue : VK_RValue, NTTP,
1222 E->getLocation(), Arg);
Douglas Gregoreb5a39d2010-12-24 00:15:10 +00001223 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001224
Eli Friedman8917ad52013-07-19 19:40:38 +00001225 Arg = getPackSubstitutedTemplateArgument(getSema(), Arg);
Douglas Gregoreb5a39d2010-12-24 00:15:10 +00001226 }
Mike Stump11289f42009-09-09 15:08:12 +00001227
John McCall7c454bb2011-07-15 05:09:51 +00001228 return transformNonTypeTemplateParmRef(NTTP, E->getLocation(), Arg);
1229}
1230
Tyler Nowickic724a83e2014-10-12 20:46:07 +00001231const LoopHintAttr *
1232TemplateInstantiator::TransformLoopHintAttr(const LoopHintAttr *LH) {
1233 Expr *TransformedExpr = getDerived().TransformExpr(LH->getValue()).get();
1234
1235 if (TransformedExpr == LH->getValue())
1236 return LH;
1237
1238 // Generate error if there is a problem with the value.
1239 if (getSema().CheckLoopHintExpr(TransformedExpr, LH->getLocation()))
1240 return LH;
1241
1242 // Create new LoopHintValueAttr with integral expression in place of the
1243 // non-type template parameter.
1244 return LoopHintAttr::CreateImplicit(
1245 getSema().Context, LH->getSemanticSpelling(), LH->getOption(),
1246 LH->getState(), TransformedExpr, LH->getRange());
1247}
1248
John McCall7c454bb2011-07-15 05:09:51 +00001249ExprResult TemplateInstantiator::transformNonTypeTemplateParmRef(
1250 NonTypeTemplateParmDecl *parm,
1251 SourceLocation loc,
Richard Smith34349002012-07-09 03:07:20 +00001252 TemplateArgument arg) {
John McCall7c454bb2011-07-15 05:09:51 +00001253 ExprResult result;
1254 QualType type;
1255
John McCall13481c52010-02-06 08:42:39 +00001256 // The template argument itself might be an expression, in which
1257 // case we just return that expression.
John McCall7c454bb2011-07-15 05:09:51 +00001258 if (arg.getKind() == TemplateArgument::Expression) {
1259 Expr *argExpr = arg.getAsExpr();
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00001260 result = argExpr;
John McCall7c454bb2011-07-15 05:09:51 +00001261 type = argExpr->getType();
Mike Stump11289f42009-09-09 15:08:12 +00001262
Eli Friedmanb826a002012-09-26 02:36:12 +00001263 } else if (arg.getKind() == TemplateArgument::Declaration ||
1264 arg.getKind() == TemplateArgument::NullPtr) {
Douglas Gregor31f55dc2012-04-06 22:40:38 +00001265 ValueDecl *VD;
Eli Friedmanb826a002012-09-26 02:36:12 +00001266 if (arg.getKind() == TemplateArgument::Declaration) {
George Burgess IV00f70bd2018-03-01 05:43:23 +00001267 VD = arg.getAsDecl();
Mike Stump11289f42009-09-09 15:08:12 +00001268
Douglas Gregor31f55dc2012-04-06 22:40:38 +00001269 // Find the instantiation of the template argument. This is
1270 // required for nested templates.
1271 VD = cast_or_null<ValueDecl>(
1272 getSema().FindInstantiatedDecl(loc, VD, TemplateArgs));
1273 if (!VD)
1274 return ExprError();
1275 } else {
1276 // Propagate NULL template argument.
Craig Topperc3ec1492014-05-26 06:22:03 +00001277 VD = nullptr;
Douglas Gregor31f55dc2012-04-06 22:40:38 +00001278 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001279
John McCall15dda372010-02-06 10:23:53 +00001280 // Derive the type we want the substituted decl to have. This had
1281 // better be non-dependent, or these checks will have serious problems.
John McCall7c454bb2011-07-15 05:09:51 +00001282 if (parm->isExpandedParameterPack()) {
1283 type = parm->getExpansionType(SemaRef.ArgumentPackSubstitutionIndex);
Fangrui Song6907ce22018-07-30 19:24:48 +00001284 } else if (parm->isParameterPack() &&
John McCall7c454bb2011-07-15 05:09:51 +00001285 isa<PackExpansionType>(parm->getType())) {
1286 type = SemaRef.SubstType(
1287 cast<PackExpansionType>(parm->getType())->getPattern(),
1288 TemplateArgs, loc, parm->getDeclName());
1289 } else {
Richard Smith5f274382016-09-28 23:55:27 +00001290 type = SemaRef.SubstType(VD ? arg.getParamTypeForDecl() : arg.getNullPtrType(),
1291 TemplateArgs, loc, parm->getDeclName());
John McCall7c454bb2011-07-15 05:09:51 +00001292 }
1293 assert(!type.isNull() && "type substitution failed for param type");
1294 assert(!type->isDependentType() && "param type still dependent");
1295 result = SemaRef.BuildExpressionFromDeclTemplateArgument(arg, type, loc);
John McCall13481c52010-02-06 08:42:39 +00001296
John McCall7c454bb2011-07-15 05:09:51 +00001297 if (!result.isInvalid()) type = result.get()->getType();
1298 } else {
1299 result = SemaRef.BuildExpressionFromIntegralTemplateArgument(arg, loc);
1300
1301 // Note that this type can be different from the type of 'result',
1302 // e.g. if it's an enum type.
1303 type = arg.getIntegralType();
1304 }
1305 if (result.isInvalid()) return ExprError();
1306
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001307 Expr *resultExpr = result.get();
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00001308 return new (SemaRef.Context) SubstNonTypeTemplateParmExpr(
1309 type, resultExpr->getValueKind(), loc, parm, resultExpr);
John McCall13481c52010-02-06 08:42:39 +00001310}
Fangrui Song6907ce22018-07-30 19:24:48 +00001311
1312ExprResult
Douglas Gregorcdbc5392011-01-15 01:15:58 +00001313TemplateInstantiator::TransformSubstNonTypeTemplateParmPackExpr(
1314 SubstNonTypeTemplateParmPackExpr *E) {
1315 if (getSema().ArgumentPackSubstitutionIndex == -1) {
1316 // We aren't expanding the parameter pack, so just return ourselves.
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00001317 return E;
Douglas Gregorcdbc5392011-01-15 01:15:58 +00001318 }
Eli Friedman8917ad52013-07-19 19:40:38 +00001319
1320 TemplateArgument Arg = E->getArgumentPack();
1321 Arg = getPackSubstitutedTemplateArgument(getSema(), Arg);
John McCall7c454bb2011-07-15 05:09:51 +00001322 return transformNonTypeTemplateParmRef(E->getParameterPack(),
1323 E->getParameterPackLocation(),
1324 Arg);
Douglas Gregorcdbc5392011-01-15 01:15:58 +00001325}
John McCall13481c52010-02-06 08:42:39 +00001326
John McCalldadc5752010-08-24 06:29:42 +00001327ExprResult
Richard Smithb15fe3a2012-09-12 00:56:43 +00001328TemplateInstantiator::RebuildParmVarDeclRefExpr(ParmVarDecl *PD,
1329 SourceLocation Loc) {
1330 DeclarationNameInfo NameInfo(PD->getDeclName(), Loc);
1331 return getSema().BuildDeclarationNameExpr(CXXScopeSpec(), NameInfo, PD);
1332}
1333
1334ExprResult
1335TemplateInstantiator::TransformFunctionParmPackExpr(FunctionParmPackExpr *E) {
1336 if (getSema().ArgumentPackSubstitutionIndex != -1) {
1337 // We can expand this parameter pack now.
1338 ParmVarDecl *D = E->getExpansion(getSema().ArgumentPackSubstitutionIndex);
1339 ValueDecl *VD = cast_or_null<ValueDecl>(TransformDecl(E->getExprLoc(), D));
1340 if (!VD)
1341 return ExprError();
1342 return RebuildParmVarDeclRefExpr(cast<ParmVarDecl>(VD), E->getExprLoc());
1343 }
1344
1345 QualType T = TransformType(E->getType());
1346 if (T.isNull())
1347 return ExprError();
1348
1349 // Transform each of the parameter expansions into the corresponding
1350 // parameters in the instantiation of the function decl.
James Y Knight48fefa32015-09-30 14:04:23 +00001351 SmallVector<ParmVarDecl *, 8> Parms;
Richard Smithb15fe3a2012-09-12 00:56:43 +00001352 Parms.reserve(E->getNumExpansions());
1353 for (FunctionParmPackExpr::iterator I = E->begin(), End = E->end();
1354 I != End; ++I) {
1355 ParmVarDecl *D =
1356 cast_or_null<ParmVarDecl>(TransformDecl(E->getExprLoc(), *I));
1357 if (!D)
1358 return ExprError();
1359 Parms.push_back(D);
1360 }
1361
1362 return FunctionParmPackExpr::Create(getSema().Context, T,
1363 E->getParameterPack(),
1364 E->getParameterPackLocation(), Parms);
1365}
1366
1367ExprResult
1368TemplateInstantiator::TransformFunctionParmPackRefExpr(DeclRefExpr *E,
1369 ParmVarDecl *PD) {
1370 typedef LocalInstantiationScope::DeclArgumentPack DeclArgumentPack;
1371 llvm::PointerUnion<Decl *, DeclArgumentPack *> *Found
1372 = getSema().CurrentInstantiationScope->findInstantiationOf(PD);
1373 assert(Found && "no instantiation for parameter pack");
1374
1375 Decl *TransformedDecl;
1376 if (DeclArgumentPack *Pack = Found->dyn_cast<DeclArgumentPack *>()) {
Nico Weberc153d242014-07-28 00:02:09 +00001377 // If this is a reference to a function parameter pack which we can
1378 // substitute but can't yet expand, build a FunctionParmPackExpr for it.
Richard Smithb15fe3a2012-09-12 00:56:43 +00001379 if (getSema().ArgumentPackSubstitutionIndex == -1) {
1380 QualType T = TransformType(E->getType());
1381 if (T.isNull())
1382 return ExprError();
1383 return FunctionParmPackExpr::Create(getSema().Context, T, PD,
1384 E->getExprLoc(), *Pack);
1385 }
1386
1387 TransformedDecl = (*Pack)[getSema().ArgumentPackSubstitutionIndex];
1388 } else {
1389 TransformedDecl = Found->get<Decl*>();
1390 }
1391
1392 // We have either an unexpanded pack or a specific expansion.
1393 return RebuildParmVarDeclRefExpr(cast<ParmVarDecl>(TransformedDecl),
1394 E->getExprLoc());
1395}
1396
1397ExprResult
John McCall13481c52010-02-06 08:42:39 +00001398TemplateInstantiator::TransformDeclRefExpr(DeclRefExpr *E) {
1399 NamedDecl *D = E->getDecl();
Richard Smithb15fe3a2012-09-12 00:56:43 +00001400
1401 // Handle references to non-type template parameters and non-type template
1402 // parameter packs.
John McCall13481c52010-02-06 08:42:39 +00001403 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(D)) {
1404 if (NTTP->getDepth() < TemplateArgs.getNumLevels())
1405 return TransformTemplateParmRefExpr(E, NTTP);
Fangrui Song6907ce22018-07-30 19:24:48 +00001406
Douglas Gregor954de172009-10-31 17:21:17 +00001407 // We have a non-type template parameter that isn't fully substituted;
1408 // FindInstantiatedDecl will find it in the local instantiation scope.
Douglas Gregora16548e2009-08-11 05:31:07 +00001409 }
Mike Stump11289f42009-09-09 15:08:12 +00001410
Richard Smithb15fe3a2012-09-12 00:56:43 +00001411 // Handle references to function parameter packs.
1412 if (ParmVarDecl *PD = dyn_cast<ParmVarDecl>(D))
1413 if (PD->isParameterPack())
1414 return TransformFunctionParmPackRefExpr(E, PD);
1415
John McCall47f29ea2009-12-08 09:21:05 +00001416 return TreeTransform<TemplateInstantiator>::TransformDeclRefExpr(E);
Douglas Gregora16548e2009-08-11 05:31:07 +00001417}
1418
John McCalldadc5752010-08-24 06:29:42 +00001419ExprResult TemplateInstantiator::TransformCXXDefaultArgExpr(
John McCall47f29ea2009-12-08 09:21:05 +00001420 CXXDefaultArgExpr *E) {
Sebastian Redl14236c82009-11-08 13:56:19 +00001421 assert(!cast<FunctionDecl>(E->getParam()->getDeclContext())->
1422 getDescribedFunctionTemplate() &&
1423 "Default arg expressions are never formed in dependent cases.");
Douglas Gregor033f6752009-12-23 23:03:06 +00001424 return SemaRef.BuildCXXDefaultArgExpr(E->getUsedLocation(),
Fangrui Song6907ce22018-07-30 19:24:48 +00001425 cast<FunctionDecl>(E->getParam()->getDeclContext()),
Douglas Gregor033f6752009-12-23 23:03:06 +00001426 E->getParam());
Sebastian Redl14236c82009-11-08 13:56:19 +00001427}
1428
Richard Smith2e321552014-11-12 02:00:47 +00001429template<typename Fn>
Douglas Gregor3024f072012-04-16 07:05:22 +00001430QualType TemplateInstantiator::TransformFunctionProtoType(TypeLocBuilder &TLB,
1431 FunctionProtoTypeLoc TL,
1432 CXXRecordDecl *ThisContext,
Mikael Nilsson9d2872d2018-12-13 10:15:27 +00001433 Qualifiers ThisTypeQuals,
Richard Smith2e321552014-11-12 02:00:47 +00001434 Fn TransformExceptionSpec) {
Douglas Gregor3024f072012-04-16 07:05:22 +00001435 // We need a local instantiation scope for this function prototype.
1436 LocalInstantiationScope Scope(SemaRef, /*CombineWithOuterScope=*/true);
Richard Smith2e321552014-11-12 02:00:47 +00001437 return inherited::TransformFunctionProtoType(
1438 TLB, TL, ThisContext, ThisTypeQuals, TransformExceptionSpec);
Douglas Gregor3024f072012-04-16 07:05:22 +00001439}
1440
John McCall58f10c32010-03-11 09:03:00 +00001441ParmVarDecl *
Douglas Gregor715e4612011-01-14 22:40:04 +00001442TemplateInstantiator::TransformFunctionTypeParam(ParmVarDecl *OldParm,
John McCall8fb0d9d2011-05-01 22:35:37 +00001443 int indexAdjustment,
David Blaikie05785d12013-02-20 22:23:23 +00001444 Optional<unsigned> NumExpansions,
Douglas Gregor0dd22bc2012-01-25 16:15:54 +00001445 bool ExpectParameterPack) {
John McCall8fb0d9d2011-05-01 22:35:37 +00001446 return SemaRef.SubstParmVarDecl(OldParm, TemplateArgs, indexAdjustment,
Douglas Gregor0dd22bc2012-01-25 16:15:54 +00001447 NumExpansions, ExpectParameterPack);
John McCall58f10c32010-03-11 09:03:00 +00001448}
1449
Mike Stump11289f42009-09-09 15:08:12 +00001450QualType
John McCall550e0c22009-10-21 00:40:46 +00001451TemplateInstantiator::TransformTemplateTypeParmType(TypeLocBuilder &TLB,
John McCall31f82722010-11-12 08:19:04 +00001452 TemplateTypeParmTypeLoc TL) {
John McCall424cec92011-01-19 06:33:43 +00001453 const TemplateTypeParmType *T = TL.getTypePtr();
Douglas Gregor01afeef2009-08-28 20:31:08 +00001454 if (T->getDepth() < TemplateArgs.getNumLevels()) {
Douglas Gregorfe1e1102009-02-27 19:31:52 +00001455 // Replace the template type parameter with its corresponding
1456 // template argument.
Mike Stump11289f42009-09-09 15:08:12 +00001457
1458 // If the corresponding template argument is NULL or doesn't exist, it's
1459 // because we are performing instantiation from explicitly-specified
1460 // template arguments in a function template class, but there were some
Douglas Gregore3f1f352009-07-01 00:28:38 +00001461 // arguments left unspecified.
John McCall550e0c22009-10-21 00:40:46 +00001462 if (!TemplateArgs.hasTemplateArgument(T->getDepth(), T->getIndex())) {
1463 TemplateTypeParmTypeLoc NewTL
1464 = TLB.push<TemplateTypeParmTypeLoc>(TL.getType());
1465 NewTL.setNameLoc(TL.getNameLoc());
1466 return TL.getType();
1467 }
Mike Stump11289f42009-09-09 15:08:12 +00001468
Douglas Gregor840bd6c2010-12-20 22:05:00 +00001469 TemplateArgument Arg = TemplateArgs(T->getDepth(), T->getIndex());
Fangrui Song6907ce22018-07-30 19:24:48 +00001470
Douglas Gregor840bd6c2010-12-20 22:05:00 +00001471 if (T->isParameterPack()) {
Fangrui Song6907ce22018-07-30 19:24:48 +00001472 assert(Arg.getKind() == TemplateArgument::Pack &&
Douglas Gregor840bd6c2010-12-20 22:05:00 +00001473 "Missing argument pack");
Fangrui Song6907ce22018-07-30 19:24:48 +00001474
Douglas Gregor840bd6c2010-12-20 22:05:00 +00001475 if (getSema().ArgumentPackSubstitutionIndex == -1) {
Douglas Gregorada4b792011-01-14 02:55:32 +00001476 // We have the template argument pack, but we're not expanding the
1477 // enclosing pack expansion yet. Just save the template argument
1478 // pack for later substitution.
1479 QualType Result
1480 = getSema().Context.getSubstTemplateTypeParmPackType(T, Arg);
1481 SubstTemplateTypeParmPackTypeLoc NewTL
1482 = TLB.push<SubstTemplateTypeParmPackTypeLoc>(Result);
1483 NewTL.setNameLoc(TL.getNameLoc());
1484 return Result;
Douglas Gregor840bd6c2010-12-20 22:05:00 +00001485 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001486
Eli Friedman8917ad52013-07-19 19:40:38 +00001487 Arg = getPackSubstitutedTemplateArgument(getSema(), Arg);
Douglas Gregor840bd6c2010-12-20 22:05:00 +00001488 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001489
Douglas Gregor840bd6c2010-12-20 22:05:00 +00001490 assert(Arg.getKind() == TemplateArgument::Type &&
Douglas Gregorfe1e1102009-02-27 19:31:52 +00001491 "Template argument kind mismatch");
Douglas Gregor01afeef2009-08-28 20:31:08 +00001492
Douglas Gregor840bd6c2010-12-20 22:05:00 +00001493 QualType Replacement = Arg.getAsType();
John McCallcebee162009-10-18 09:09:24 +00001494
1495 // TODO: only do this uniquing once, at the start of instantiation.
John McCall550e0c22009-10-21 00:40:46 +00001496 QualType Result
1497 = getSema().Context.getSubstTemplateTypeParmType(T, Replacement);
1498 SubstTemplateTypeParmTypeLoc NewTL
1499 = TLB.push<SubstTemplateTypeParmTypeLoc>(Result);
1500 NewTL.setNameLoc(TL.getNameLoc());
1501 return Result;
Mike Stump11289f42009-09-09 15:08:12 +00001502 }
Douglas Gregorfe1e1102009-02-27 19:31:52 +00001503
1504 // The template type parameter comes from an inner template (e.g.,
1505 // the template parameter list of a member template inside the
1506 // template we are instantiating). Create a new template type
1507 // parameter with the template "level" reduced by one.
Craig Topperc3ec1492014-05-26 06:22:03 +00001508 TemplateTypeParmDecl *NewTTPDecl = nullptr;
Chandler Carruth08836322011-05-01 00:51:33 +00001509 if (TemplateTypeParmDecl *OldTTPDecl = T->getDecl())
1510 NewTTPDecl = cast_or_null<TemplateTypeParmDecl>(
1511 TransformDecl(TL.getNameLoc(), OldTTPDecl));
1512
Richard Smithb4f96252017-02-21 06:30:38 +00001513 QualType Result = getSema().Context.getTemplateTypeParmType(
1514 T->getDepth() - TemplateArgs.getNumSubstitutedLevels(), T->getIndex(),
1515 T->isParameterPack(), NewTTPDecl);
John McCall550e0c22009-10-21 00:40:46 +00001516 TemplateTypeParmTypeLoc NewTL = TLB.push<TemplateTypeParmTypeLoc>(Result);
1517 NewTL.setNameLoc(TL.getNameLoc());
1518 return Result;
Douglas Gregor17c0d7b2009-02-28 00:25:32 +00001519}
Douglas Gregorfe1e1102009-02-27 19:31:52 +00001520
Fangrui Song6907ce22018-07-30 19:24:48 +00001521QualType
Douglas Gregorada4b792011-01-14 02:55:32 +00001522TemplateInstantiator::TransformSubstTemplateTypeParmPackType(
1523 TypeLocBuilder &TLB,
1524 SubstTemplateTypeParmPackTypeLoc TL) {
1525 if (getSema().ArgumentPackSubstitutionIndex == -1) {
1526 // We aren't expanding the parameter pack, so just return ourselves.
1527 SubstTemplateTypeParmPackTypeLoc NewTL
1528 = TLB.push<SubstTemplateTypeParmPackTypeLoc>(TL.getType());
1529 NewTL.setNameLoc(TL.getNameLoc());
1530 return TL.getType();
1531 }
Eli Friedman8917ad52013-07-19 19:40:38 +00001532
1533 TemplateArgument Arg = TL.getTypePtr()->getArgumentPack();
1534 Arg = getPackSubstitutedTemplateArgument(getSema(), Arg);
1535 QualType Result = Arg.getAsType();
1536
Douglas Gregorada4b792011-01-14 02:55:32 +00001537 Result = getSema().Context.getSubstTemplateTypeParmType(
1538 TL.getTypePtr()->getReplacedParameter(),
1539 Result);
1540 SubstTemplateTypeParmTypeLoc NewTL
1541 = TLB.push<SubstTemplateTypeParmTypeLoc>(Result);
1542 NewTL.setNameLoc(TL.getNameLoc());
1543 return Result;
1544}
1545
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001546/// Perform substitution on the type T with a given set of template
John McCall76d824f2009-08-25 22:02:44 +00001547/// arguments.
Douglas Gregorfe1e1102009-02-27 19:31:52 +00001548///
1549/// This routine substitutes the given template arguments into the
1550/// type T and produces the instantiated type.
1551///
1552/// \param T the type into which the template arguments will be
1553/// substituted. If this type is not dependent, it will be returned
1554/// immediately.
1555///
James Dennett634962f2012-06-14 21:40:34 +00001556/// \param Args the template arguments that will be
Douglas Gregorfe1e1102009-02-27 19:31:52 +00001557/// substituted for the top-level template parameters within T.
1558///
Douglas Gregorfe1e1102009-02-27 19:31:52 +00001559/// \param Loc the location in the source code where this substitution
1560/// is being performed. It will typically be the location of the
1561/// declarator (if we're instantiating the type of some declaration)
1562/// or the location of the type in the source code (if, e.g., we're
1563/// instantiating the type of a cast expression).
1564///
1565/// \param Entity the name of the entity associated with a declaration
1566/// being instantiated (if any). May be empty to indicate that there
1567/// is no such entity (if, e.g., this is a type that occurs as part of
1568/// a cast expression) or that the entity has no name (e.g., an
1569/// unnamed function parameter).
1570///
Richard Smithee579842017-01-30 20:39:26 +00001571/// \param AllowDeducedTST Whether a DeducedTemplateSpecializationType is
1572/// acceptable as the top level type of the result.
1573///
Douglas Gregorfe1e1102009-02-27 19:31:52 +00001574/// \returns If the instantiation succeeds, the instantiated
1575/// type. Otherwise, produces diagnostics and returns a NULL type.
John McCallbcd03502009-12-07 02:54:59 +00001576TypeSourceInfo *Sema::SubstType(TypeSourceInfo *T,
John McCall609459e2009-10-21 00:58:09 +00001577 const MultiLevelTemplateArgumentList &Args,
1578 SourceLocation Loc,
Richard Smithee579842017-01-30 20:39:26 +00001579 DeclarationName Entity,
1580 bool AllowDeducedTST) {
Richard Smith696e3122017-02-23 01:43:54 +00001581 assert(!CodeSynthesisContexts.empty() &&
John McCall609459e2009-10-21 00:58:09 +00001582 "Cannot perform an instantiation without some context on the "
1583 "instantiation stack");
Fangrui Song6907ce22018-07-30 19:24:48 +00001584
1585 if (!T->getType()->isInstantiationDependentType() &&
Douglas Gregor5a5073e2010-05-24 17:22:01 +00001586 !T->getType()->isVariablyModifiedType())
John McCall609459e2009-10-21 00:58:09 +00001587 return T;
1588
1589 TemplateInstantiator Instantiator(*this, Args, Loc, Entity);
Richard Smithee579842017-01-30 20:39:26 +00001590 return AllowDeducedTST ? Instantiator.TransformTypeWithDeducedTST(T)
1591 : Instantiator.TransformType(T);
John McCall609459e2009-10-21 00:58:09 +00001592}
1593
Douglas Gregor5499af42011-01-05 23:12:31 +00001594TypeSourceInfo *Sema::SubstType(TypeLoc TL,
1595 const MultiLevelTemplateArgumentList &Args,
1596 SourceLocation Loc,
1597 DeclarationName Entity) {
Richard Smith696e3122017-02-23 01:43:54 +00001598 assert(!CodeSynthesisContexts.empty() &&
Douglas Gregor5499af42011-01-05 23:12:31 +00001599 "Cannot perform an instantiation without some context on the "
1600 "instantiation stack");
Fangrui Song6907ce22018-07-30 19:24:48 +00001601
Douglas Gregor5499af42011-01-05 23:12:31 +00001602 if (TL.getType().isNull())
Craig Topperc3ec1492014-05-26 06:22:03 +00001603 return nullptr;
Douglas Gregor5499af42011-01-05 23:12:31 +00001604
Fangrui Song6907ce22018-07-30 19:24:48 +00001605 if (!TL.getType()->isInstantiationDependentType() &&
Douglas Gregor5499af42011-01-05 23:12:31 +00001606 !TL.getType()->isVariablyModifiedType()) {
1607 // FIXME: Make a copy of the TypeLoc data here, so that we can
1608 // return a new TypeSourceInfo. Inefficient!
1609 TypeLocBuilder TLB;
1610 TLB.pushFullCopy(TL);
1611 return TLB.getTypeSourceInfo(Context, TL.getType());
1612 }
1613
1614 TemplateInstantiator Instantiator(*this, Args, Loc, Entity);
1615 TypeLocBuilder TLB;
1616 TLB.reserve(TL.getFullDataSize());
1617 QualType Result = Instantiator.TransformType(TLB, TL);
1618 if (Result.isNull())
Craig Topperc3ec1492014-05-26 06:22:03 +00001619 return nullptr;
Douglas Gregor5499af42011-01-05 23:12:31 +00001620
1621 return TLB.getTypeSourceInfo(Context, Result);
1622}
1623
John McCall609459e2009-10-21 00:58:09 +00001624/// Deprecated form of the above.
Mike Stump11289f42009-09-09 15:08:12 +00001625QualType Sema::SubstType(QualType T,
Douglas Gregor01afeef2009-08-28 20:31:08 +00001626 const MultiLevelTemplateArgumentList &TemplateArgs,
John McCall76d824f2009-08-25 22:02:44 +00001627 SourceLocation Loc, DeclarationName Entity) {
Richard Smith696e3122017-02-23 01:43:54 +00001628 assert(!CodeSynthesisContexts.empty() &&
Douglas Gregor79cf6032009-03-10 20:44:00 +00001629 "Cannot perform an instantiation without some context on the "
1630 "instantiation stack");
1631
Douglas Gregor5a5073e2010-05-24 17:22:01 +00001632 // If T is not a dependent type or a variably-modified type, there
1633 // is nothing to do.
Douglas Gregor678d76c2011-07-01 01:22:09 +00001634 if (!T->isInstantiationDependentType() && !T->isVariablyModifiedType())
Douglas Gregorfe1e1102009-02-27 19:31:52 +00001635 return T;
1636
Douglas Gregord6ff3322009-08-04 16:50:30 +00001637 TemplateInstantiator Instantiator(*this, TemplateArgs, Loc, Entity);
1638 return Instantiator.TransformType(T);
Douglas Gregorfe1e1102009-02-27 19:31:52 +00001639}
Douglas Gregor463421d2009-03-03 04:44:36 +00001640
John McCallb29f78f2010-04-09 17:38:44 +00001641static bool NeedsInstantiationAsFunctionType(TypeSourceInfo *T) {
Nico Weberc0973372016-02-01 22:31:51 +00001642 if (T->getType()->isInstantiationDependentType() ||
Douglas Gregor678d76c2011-07-01 01:22:09 +00001643 T->getType()->isVariablyModifiedType())
John McCallb29f78f2010-04-09 17:38:44 +00001644 return true;
1645
Abramo Bagnara6d810632010-12-14 22:11:44 +00001646 TypeLoc TL = T->getTypeLoc().IgnoreParens();
David Blaikie6adc78e2013-02-18 22:06:02 +00001647 if (!TL.getAs<FunctionProtoTypeLoc>())
John McCallb29f78f2010-04-09 17:38:44 +00001648 return false;
1649
David Blaikie6adc78e2013-02-18 22:06:02 +00001650 FunctionProtoTypeLoc FP = TL.castAs<FunctionProtoTypeLoc>();
Nico Weberc0973372016-02-01 22:31:51 +00001651 for (ParmVarDecl *P : FP.getParams()) {
Reid Klecknera09e44c2013-07-31 21:00:18 +00001652 // This must be synthesized from a typedef.
1653 if (!P) continue;
1654
Nico Weberc0973372016-02-01 22:31:51 +00001655 // If there are any parameters, a new TypeSourceInfo that refers to the
1656 // instantiated parameters must be built.
1657 return true;
John McCallb29f78f2010-04-09 17:38:44 +00001658 }
1659
1660 return false;
1661}
1662
1663/// A form of SubstType intended specifically for instantiating the
1664/// type of a FunctionDecl. Its purpose is solely to force the
Richard Smith2e321552014-11-12 02:00:47 +00001665/// instantiation of default-argument expressions and to avoid
1666/// instantiating an exception-specification.
John McCallb29f78f2010-04-09 17:38:44 +00001667TypeSourceInfo *Sema::SubstFunctionDeclType(TypeSourceInfo *T,
1668 const MultiLevelTemplateArgumentList &Args,
1669 SourceLocation Loc,
Douglas Gregor3024f072012-04-16 07:05:22 +00001670 DeclarationName Entity,
1671 CXXRecordDecl *ThisContext,
Mikael Nilsson9d2872d2018-12-13 10:15:27 +00001672 Qualifiers ThisTypeQuals) {
Richard Smith696e3122017-02-23 01:43:54 +00001673 assert(!CodeSynthesisContexts.empty() &&
John McCallb29f78f2010-04-09 17:38:44 +00001674 "Cannot perform an instantiation without some context on the "
1675 "instantiation stack");
Nico Weberc0973372016-02-01 22:31:51 +00001676
John McCallb29f78f2010-04-09 17:38:44 +00001677 if (!NeedsInstantiationAsFunctionType(T))
1678 return T;
1679
1680 TemplateInstantiator Instantiator(*this, Args, Loc, Entity);
1681
1682 TypeLocBuilder TLB;
1683
1684 TypeLoc TL = T->getTypeLoc();
1685 TLB.reserve(TL.getFullDataSize());
1686
Douglas Gregor3024f072012-04-16 07:05:22 +00001687 QualType Result;
David Blaikie6adc78e2013-02-18 22:06:02 +00001688
Richard Smith2e321552014-11-12 02:00:47 +00001689 if (FunctionProtoTypeLoc Proto =
1690 TL.IgnoreParens().getAs<FunctionProtoTypeLoc>()) {
1691 // Instantiate the type, other than its exception specification. The
1692 // exception specification is instantiated in InitFunctionInstantiation
1693 // once we've built the FunctionDecl.
1694 // FIXME: Set the exception specification to EST_Uninstantiated here,
1695 // instead of rebuilding the function type again later.
1696 Result = Instantiator.TransformFunctionProtoType(
1697 TLB, Proto, ThisContext, ThisTypeQuals,
1698 [](FunctionProtoType::ExceptionSpecInfo &ESI,
1699 bool &Changed) { return false; });
Douglas Gregor3024f072012-04-16 07:05:22 +00001700 } else {
1701 Result = Instantiator.TransformType(TLB, TL);
1702 }
John McCallb29f78f2010-04-09 17:38:44 +00001703 if (Result.isNull())
Craig Topperc3ec1492014-05-26 06:22:03 +00001704 return nullptr;
John McCallb29f78f2010-04-09 17:38:44 +00001705
1706 return TLB.getTypeSourceInfo(Context, Result);
1707}
1708
Richard Smithcd198152017-06-07 21:46:22 +00001709bool Sema::SubstExceptionSpec(SourceLocation Loc,
1710 FunctionProtoType::ExceptionSpecInfo &ESI,
1711 SmallVectorImpl<QualType> &ExceptionStorage,
1712 const MultiLevelTemplateArgumentList &Args) {
1713 assert(ESI.Type != EST_Uninstantiated);
1714
1715 bool Changed = false;
1716 TemplateInstantiator Instantiator(*this, Args, Loc, DeclarationName());
1717 return Instantiator.TransformExceptionSpec(Loc, ESI, ExceptionStorage,
1718 Changed);
1719}
1720
Richard Smith2e321552014-11-12 02:00:47 +00001721void Sema::SubstExceptionSpec(FunctionDecl *New, const FunctionProtoType *Proto,
1722 const MultiLevelTemplateArgumentList &Args) {
1723 FunctionProtoType::ExceptionSpecInfo ESI =
1724 Proto->getExtProtoInfo().ExceptionSpec;
Richard Smith2e321552014-11-12 02:00:47 +00001725
1726 SmallVector<QualType, 4> ExceptionStorage;
Stephen Kelly1c301dc2018-08-09 21:09:38 +00001727 if (SubstExceptionSpec(New->getTypeSourceInfo()->getTypeLoc().getEndLoc(),
Richard Smithcd198152017-06-07 21:46:22 +00001728 ESI, ExceptionStorage, Args))
Richard Smith2e321552014-11-12 02:00:47 +00001729 // On error, recover by dropping the exception specification.
1730 ESI.Type = EST_None;
1731
1732 UpdateExceptionSpec(New, ESI);
1733}
1734
Fangrui Song6907ce22018-07-30 19:24:48 +00001735ParmVarDecl *Sema::SubstParmVarDecl(ParmVarDecl *OldParm,
Douglas Gregor715e4612011-01-14 22:40:04 +00001736 const MultiLevelTemplateArgumentList &TemplateArgs,
John McCall8fb0d9d2011-05-01 22:35:37 +00001737 int indexAdjustment,
David Blaikie05785d12013-02-20 22:23:23 +00001738 Optional<unsigned> NumExpansions,
Douglas Gregor0dd22bc2012-01-25 16:15:54 +00001739 bool ExpectParameterPack) {
Douglas Gregor940bca72010-04-12 07:48:19 +00001740 TypeSourceInfo *OldDI = OldParm->getTypeSourceInfo();
Craig Topperc3ec1492014-05-26 06:22:03 +00001741 TypeSourceInfo *NewDI = nullptr;
1742
Douglas Gregor5499af42011-01-05 23:12:31 +00001743 TypeLoc OldTL = OldDI->getTypeLoc();
David Blaikie6adc78e2013-02-18 22:06:02 +00001744 if (PackExpansionTypeLoc ExpansionTL = OldTL.getAs<PackExpansionTypeLoc>()) {
1745
Fangrui Song6907ce22018-07-30 19:24:48 +00001746 // We have a function parameter pack. Substitute into the pattern of the
Douglas Gregor5499af42011-01-05 23:12:31 +00001747 // expansion.
Fangrui Song6907ce22018-07-30 19:24:48 +00001748 NewDI = SubstType(ExpansionTL.getPatternLoc(), TemplateArgs,
Douglas Gregor5499af42011-01-05 23:12:31 +00001749 OldParm->getLocation(), OldParm->getDeclName());
1750 if (!NewDI)
Craig Topperc3ec1492014-05-26 06:22:03 +00001751 return nullptr;
1752
Douglas Gregor5499af42011-01-05 23:12:31 +00001753 if (NewDI->getType()->containsUnexpandedParameterPack()) {
1754 // We still have unexpanded parameter packs, which means that
1755 // our function parameter is still a function parameter pack.
1756 // Therefore, make its type a pack expansion type.
Douglas Gregor0dca5fd2011-01-14 17:04:44 +00001757 NewDI = CheckPackExpansion(NewDI, ExpansionTL.getEllipsisLoc(),
Douglas Gregor715e4612011-01-14 22:40:04 +00001758 NumExpansions);
Douglas Gregor0dd22bc2012-01-25 16:15:54 +00001759 } else if (ExpectParameterPack) {
1760 // We expected to get a parameter pack but didn't (because the type
1761 // itself is not a pack expansion type), so complain. This can occur when
1762 // the substitution goes through an alias template that "loses" the
1763 // pack expansion.
Fangrui Song6907ce22018-07-30 19:24:48 +00001764 Diag(OldParm->getLocation(),
Douglas Gregor0dd22bc2012-01-25 16:15:54 +00001765 diag::err_function_parameter_pack_without_parameter_packs)
1766 << NewDI->getType();
Craig Topperc3ec1492014-05-26 06:22:03 +00001767 return nullptr;
Fangrui Song6907ce22018-07-30 19:24:48 +00001768 }
Douglas Gregor5499af42011-01-05 23:12:31 +00001769 } else {
Fangrui Song6907ce22018-07-30 19:24:48 +00001770 NewDI = SubstType(OldDI, TemplateArgs, OldParm->getLocation(),
Douglas Gregor5499af42011-01-05 23:12:31 +00001771 OldParm->getDeclName());
1772 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001773
Douglas Gregor940bca72010-04-12 07:48:19 +00001774 if (!NewDI)
Craig Topperc3ec1492014-05-26 06:22:03 +00001775 return nullptr;
Douglas Gregor940bca72010-04-12 07:48:19 +00001776
1777 if (NewDI->getType()->isVoidType()) {
1778 Diag(OldParm->getLocation(), diag::err_param_with_void_type);
Craig Topperc3ec1492014-05-26 06:22:03 +00001779 return nullptr;
Douglas Gregor940bca72010-04-12 07:48:19 +00001780 }
1781
1782 ParmVarDecl *NewParm = CheckParameter(Context.getTranslationUnitDecl(),
Abramo Bagnaradff19302011-03-08 08:55:46 +00001783 OldParm->getInnerLocStart(),
Douglas Gregor940bca72010-04-12 07:48:19 +00001784 OldParm->getLocation(),
Abramo Bagnaradff19302011-03-08 08:55:46 +00001785 OldParm->getIdentifier(),
1786 NewDI->getType(), NewDI,
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001787 OldParm->getStorageClass());
Douglas Gregor940bca72010-04-12 07:48:19 +00001788 if (!NewParm)
Craig Topperc3ec1492014-05-26 06:22:03 +00001789 return nullptr;
1790
Douglas Gregor940bca72010-04-12 07:48:19 +00001791 // Mark the (new) default argument as uninstantiated (if any).
1792 if (OldParm->hasUninstantiatedDefaultArg()) {
1793 Expr *Arg = OldParm->getUninstantiatedDefaultArg();
1794 NewParm->setUninstantiatedDefaultArg(Arg);
Douglas Gregor758cb672010-10-12 18:23:32 +00001795 } else if (OldParm->hasUnparsedDefaultArg()) {
1796 NewParm->setUnparsedDefaultArg();
1797 UnparsedDefaultArgInstantiations[OldParm].push_back(NewParm);
Serge Pavlov3739f5e72015-06-29 17:50:19 +00001798 } else if (Expr *Arg = OldParm->getDefaultArg()) {
1799 FunctionDecl *OwningFunc = cast<FunctionDecl>(OldParm->getDeclContext());
Serge Pavlov73c6a242015-08-23 10:22:28 +00001800 if (OwningFunc->isLexicallyWithinFunctionOrMethod()) {
1801 // Instantiate default arguments for methods of local classes (DR1484)
1802 // and non-defining declarations.
1803 Sema::ContextRAII SavedContext(*this, OwningFunc);
Akira Hatanakad644e022016-12-16 03:19:41 +00001804 LocalInstantiationScope Local(*this, true);
Serge Pavlov3739f5e72015-06-29 17:50:19 +00001805 ExprResult NewArg = SubstExpr(Arg, TemplateArgs);
John McCalldc40b612015-12-11 01:56:36 +00001806 if (NewArg.isUsable()) {
1807 // It would be nice if we still had this.
Stephen Kellyf2ceec42018-08-09 21:08:08 +00001808 SourceLocation EqualLoc = NewArg.get()->getBeginLoc();
John McCalldc40b612015-12-11 01:56:36 +00001809 SetParamDefaultArgument(NewParm, NewArg.get(), EqualLoc);
1810 }
Serge Pavlov3739f5e72015-06-29 17:50:19 +00001811 } else {
1812 // FIXME: if we non-lazily instantiated non-dependent default args for
1813 // non-dependent parameter types we could remove a bunch of duplicate
1814 // conversion warnings for such arguments.
1815 NewParm->setUninstantiatedDefaultArg(Arg);
1816 }
1817 }
Douglas Gregor940bca72010-04-12 07:48:19 +00001818
1819 NewParm->setHasInheritedDefaultArg(OldParm->hasInheritedDefaultArg());
Fangrui Song6907ce22018-07-30 19:24:48 +00001820
Douglas Gregorf3010112011-01-07 16:43:16 +00001821 if (OldParm->isParameterPack() && !NewParm->isParameterPack()) {
Richard Smith928be492012-01-25 02:14:59 +00001822 // Add the new parameter to the instantiated parameter pack.
Douglas Gregorf3010112011-01-07 16:43:16 +00001823 CurrentInstantiationScope->InstantiatedLocalPackArg(OldParm, NewParm);
1824 } else {
1825 // Introduce an Old -> New mapping
Fangrui Song6907ce22018-07-30 19:24:48 +00001826 CurrentInstantiationScope->InstantiatedLocal(OldParm, NewParm);
Douglas Gregorf3010112011-01-07 16:43:16 +00001827 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001828
Argyrios Kyrtzidis3816ed42010-07-19 10:14:41 +00001829 // FIXME: OldParm may come from a FunctionProtoType, in which case CurContext
1830 // can be anything, is this right ?
Fariborz Jahanian714447b2010-07-13 21:05:02 +00001831 NewParm->setDeclContext(CurContext);
John McCall8fb0d9d2011-05-01 22:35:37 +00001832
1833 NewParm->setScopeInfo(OldParm->getFunctionScopeDepth(),
1834 OldParm->getFunctionScopeIndex() + indexAdjustment);
Jordan Rosea0a86be2013-03-08 22:25:36 +00001835
1836 InstantiateAttrs(TemplateArgs, OldParm, NewParm);
1837
Fangrui Song6907ce22018-07-30 19:24:48 +00001838 return NewParm;
Douglas Gregor940bca72010-04-12 07:48:19 +00001839}
1840
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001841/// Substitute the given template arguments into the given set of
Douglas Gregordd472162011-01-07 00:20:55 +00001842/// parameters, producing the set of parameter types that would be generated
1843/// from such a substitution.
David Majnemer59f77922016-06-24 04:05:48 +00001844bool Sema::SubstParmTypes(
1845 SourceLocation Loc, ArrayRef<ParmVarDecl *> Params,
1846 const FunctionProtoType::ExtParameterInfo *ExtParamInfos,
1847 const MultiLevelTemplateArgumentList &TemplateArgs,
1848 SmallVectorImpl<QualType> &ParamTypes,
1849 SmallVectorImpl<ParmVarDecl *> *OutParams,
1850 ExtParameterInfoBuilder &ParamInfos) {
Richard Smith696e3122017-02-23 01:43:54 +00001851 assert(!CodeSynthesisContexts.empty() &&
Douglas Gregordd472162011-01-07 00:20:55 +00001852 "Cannot perform an instantiation without some context on the "
1853 "instantiation stack");
Fangrui Song6907ce22018-07-30 19:24:48 +00001854
1855 TemplateInstantiator Instantiator(*this, TemplateArgs, Loc,
Douglas Gregordd472162011-01-07 00:20:55 +00001856 DeclarationName());
David Majnemer59f77922016-06-24 04:05:48 +00001857 return Instantiator.TransformFunctionTypeParams(
1858 Loc, Params, nullptr, ExtParamInfos, ParamTypes, OutParams, ParamInfos);
Douglas Gregordd472162011-01-07 00:20:55 +00001859}
1860
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001861/// Perform substitution on the base class specifiers of the
John McCall76d824f2009-08-25 22:02:44 +00001862/// given class template specialization.
Douglas Gregor463421d2009-03-03 04:44:36 +00001863///
1864/// Produces a diagnostic and returns true on error, returns false and
1865/// attaches the instantiated base classes to the class template
1866/// specialization if successful.
Mike Stump11289f42009-09-09 15:08:12 +00001867bool
John McCall76d824f2009-08-25 22:02:44 +00001868Sema::SubstBaseSpecifiers(CXXRecordDecl *Instantiation,
1869 CXXRecordDecl *Pattern,
Douglas Gregor01afeef2009-08-28 20:31:08 +00001870 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor463421d2009-03-03 04:44:36 +00001871 bool Invalid = false;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001872 SmallVector<CXXBaseSpecifier*, 4> InstantiatedBases;
Richard Trieub5841332015-04-15 01:21:42 +00001873 for (const auto &Base : Pattern->bases()) {
Aaron Ballman574705e2014-03-13 15:41:46 +00001874 if (!Base.getType()->isDependentType()) {
1875 if (const CXXRecordDecl *RD = Base.getType()->getAsCXXRecordDecl()) {
Matt Beaumont-Gay76d0b462013-06-21 18:58:32 +00001876 if (RD->isInvalidDecl())
1877 Instantiation->setInvalidDecl();
1878 }
Aaron Ballman574705e2014-03-13 15:41:46 +00001879 InstantiatedBases.push_back(new (Context) CXXBaseSpecifier(Base));
Douglas Gregor463421d2009-03-03 04:44:36 +00001880 continue;
1881 }
1882
Douglas Gregor752a5952011-01-03 22:36:02 +00001883 SourceLocation EllipsisLoc;
Douglas Gregorc52264e2011-03-02 02:04:06 +00001884 TypeSourceInfo *BaseTypeLoc;
Aaron Ballman574705e2014-03-13 15:41:46 +00001885 if (Base.isPackExpansion()) {
Douglas Gregor752a5952011-01-03 22:36:02 +00001886 // This is a pack expansion. See whether we should expand it now, or
1887 // wait until later.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001888 SmallVector<UnexpandedParameterPack, 2> Unexpanded;
Aaron Ballman574705e2014-03-13 15:41:46 +00001889 collectUnexpandedParameterPacks(Base.getTypeSourceInfo()->getTypeLoc(),
Douglas Gregor752a5952011-01-03 22:36:02 +00001890 Unexpanded);
1891 bool ShouldExpand = false;
Douglas Gregora8bac7f2011-01-10 07:32:04 +00001892 bool RetainExpansion = false;
David Blaikie05785d12013-02-20 22:23:23 +00001893 Optional<unsigned> NumExpansions;
Fangrui Song6907ce22018-07-30 19:24:48 +00001894 if (CheckParameterPacksForExpansion(Base.getEllipsisLoc(),
Aaron Ballman574705e2014-03-13 15:41:46 +00001895 Base.getSourceRange(),
David Blaikieb9c168a2011-09-22 02:34:54 +00001896 Unexpanded,
Fangrui Song6907ce22018-07-30 19:24:48 +00001897 TemplateArgs, ShouldExpand,
Douglas Gregora8bac7f2011-01-10 07:32:04 +00001898 RetainExpansion,
Douglas Gregor752a5952011-01-03 22:36:02 +00001899 NumExpansions)) {
Douglas Gregor752a5952011-01-03 22:36:02 +00001900 Invalid = true;
Douglas Gregor44e7df62011-01-04 00:32:56 +00001901 continue;
Douglas Gregor752a5952011-01-03 22:36:02 +00001902 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001903
Douglas Gregor752a5952011-01-03 22:36:02 +00001904 // If we should expand this pack expansion now, do so.
1905 if (ShouldExpand) {
Douglas Gregor0dca5fd2011-01-14 17:04:44 +00001906 for (unsigned I = 0; I != *NumExpansions; ++I) {
Douglas Gregor752a5952011-01-03 22:36:02 +00001907 Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(*this, I);
Fangrui Song6907ce22018-07-30 19:24:48 +00001908
Aaron Ballman574705e2014-03-13 15:41:46 +00001909 TypeSourceInfo *BaseTypeLoc = SubstType(Base.getTypeSourceInfo(),
Douglas Gregor752a5952011-01-03 22:36:02 +00001910 TemplateArgs,
Aaron Ballman574705e2014-03-13 15:41:46 +00001911 Base.getSourceRange().getBegin(),
Douglas Gregor752a5952011-01-03 22:36:02 +00001912 DeclarationName());
1913 if (!BaseTypeLoc) {
1914 Invalid = true;
1915 continue;
1916 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001917
Douglas Gregor752a5952011-01-03 22:36:02 +00001918 if (CXXBaseSpecifier *InstantiatedBase
1919 = CheckBaseSpecifier(Instantiation,
Aaron Ballman574705e2014-03-13 15:41:46 +00001920 Base.getSourceRange(),
1921 Base.isVirtual(),
1922 Base.getAccessSpecifierAsWritten(),
Douglas Gregor752a5952011-01-03 22:36:02 +00001923 BaseTypeLoc,
1924 SourceLocation()))
1925 InstantiatedBases.push_back(InstantiatedBase);
1926 else
1927 Invalid = true;
1928 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001929
Douglas Gregor752a5952011-01-03 22:36:02 +00001930 continue;
1931 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001932
Douglas Gregor752a5952011-01-03 22:36:02 +00001933 // The resulting base specifier will (still) be a pack expansion.
Aaron Ballman574705e2014-03-13 15:41:46 +00001934 EllipsisLoc = Base.getEllipsisLoc();
Douglas Gregorc52264e2011-03-02 02:04:06 +00001935 Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(*this, -1);
Aaron Ballman574705e2014-03-13 15:41:46 +00001936 BaseTypeLoc = SubstType(Base.getTypeSourceInfo(),
Douglas Gregorc52264e2011-03-02 02:04:06 +00001937 TemplateArgs,
Aaron Ballman574705e2014-03-13 15:41:46 +00001938 Base.getSourceRange().getBegin(),
Douglas Gregorc52264e2011-03-02 02:04:06 +00001939 DeclarationName());
1940 } else {
Aaron Ballman574705e2014-03-13 15:41:46 +00001941 BaseTypeLoc = SubstType(Base.getTypeSourceInfo(),
Douglas Gregorc52264e2011-03-02 02:04:06 +00001942 TemplateArgs,
Aaron Ballman574705e2014-03-13 15:41:46 +00001943 Base.getSourceRange().getBegin(),
Douglas Gregorc52264e2011-03-02 02:04:06 +00001944 DeclarationName());
Douglas Gregor752a5952011-01-03 22:36:02 +00001945 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001946
Nick Lewycky19b9f952010-07-26 16:56:01 +00001947 if (!BaseTypeLoc) {
Douglas Gregor463421d2009-03-03 04:44:36 +00001948 Invalid = true;
1949 continue;
1950 }
1951
1952 if (CXXBaseSpecifier *InstantiatedBase
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001953 = CheckBaseSpecifier(Instantiation,
Aaron Ballman574705e2014-03-13 15:41:46 +00001954 Base.getSourceRange(),
1955 Base.isVirtual(),
1956 Base.getAccessSpecifierAsWritten(),
Douglas Gregor752a5952011-01-03 22:36:02 +00001957 BaseTypeLoc,
1958 EllipsisLoc))
Douglas Gregor463421d2009-03-03 04:44:36 +00001959 InstantiatedBases.push_back(InstantiatedBase);
1960 else
1961 Invalid = true;
1962 }
1963
Craig Topperaa700cb2015-12-27 21:55:19 +00001964 if (!Invalid && AttachBaseSpecifiers(Instantiation, InstantiatedBases))
Douglas Gregor463421d2009-03-03 04:44:36 +00001965 Invalid = true;
1966
1967 return Invalid;
1968}
1969
DeLesley Hutchins30398dd2012-01-20 22:50:54 +00001970// Defined via #include from SemaTemplateInstantiateDecl.cpp
Benjamin Kramerbf8da9d2012-02-06 11:13:08 +00001971namespace clang {
1972 namespace sema {
1973 Attr *instantiateTemplateAttribute(const Attr *At, ASTContext &C, Sema &S,
1974 const MultiLevelTemplateArgumentList &TemplateArgs);
Erich Keanea32910d2017-03-23 18:51:54 +00001975 Attr *instantiateTemplateAttributeForDecl(
1976 const Attr *At, ASTContext &C, Sema &S,
1977 const MultiLevelTemplateArgumentList &TemplateArgs);
Benjamin Kramerbf8da9d2012-02-06 11:13:08 +00001978 }
1979}
DeLesley Hutchins30398dd2012-01-20 22:50:54 +00001980
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001981/// Instantiate the definition of a class from a given pattern.
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00001982///
1983/// \param PointOfInstantiation The point of instantiation within the
1984/// source code.
1985///
1986/// \param Instantiation is the declaration whose definition is being
1987/// instantiated. This will be either a class template specialization
1988/// or a member class of a class template specialization.
1989///
1990/// \param Pattern is the pattern from which the instantiation
1991/// occurs. This will be either the declaration of a class template or
1992/// the declaration of a member class of a class template.
1993///
1994/// \param TemplateArgs The template arguments to be substituted into
1995/// the pattern.
1996///
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001997/// \param TSK the kind of implicit or explicit instantiation to perform.
Douglas Gregor8a2e6012009-08-24 15:23:48 +00001998///
1999/// \param Complain whether to complain if the class cannot be instantiated due
2000/// to the lack of a definition.
2001///
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00002002/// \returns true if an error occurred, false otherwise.
2003bool
2004Sema::InstantiateClass(SourceLocation PointOfInstantiation,
2005 CXXRecordDecl *Instantiation, CXXRecordDecl *Pattern,
Douglas Gregor01afeef2009-08-28 20:31:08 +00002006 const MultiLevelTemplateArgumentList &TemplateArgs,
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00002007 TemplateSpecializationKind TSK,
Douglas Gregor8a2e6012009-08-24 15:23:48 +00002008 bool Complain) {
Mike Stump11289f42009-09-09 15:08:12 +00002009 CXXRecordDecl *PatternDef
Douglas Gregor0a5a2212010-02-11 01:04:33 +00002010 = cast_or_null<CXXRecordDecl>(Pattern->getDefinition());
Vassil Vassilevb21ee082016-08-18 22:01:25 +00002011 if (DiagnoseUninstantiableTemplate(PointOfInstantiation, Instantiation,
Richard Smith4b38ded2012-03-14 23:13:10 +00002012 Instantiation->getInstantiatedFromMemberClass(),
2013 Pattern, PatternDef, TSK, Complain))
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00002014 return true;
Anton Afanasyevd880de22019-03-30 08:42:48 +00002015
2016 llvm::TimeTraceScope TimeScope("InstantiateClass", [&]() {
2017 return Instantiation->getQualifiedNameAsString();
2018 });
2019
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00002020 Pattern = PatternDef;
2021
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002022 // Record the point of instantiation.
Fangrui Song6907ce22018-07-30 19:24:48 +00002023 if (MemberSpecializationInfo *MSInfo
Douglas Gregord6ba93d2009-10-15 15:54:05 +00002024 = Instantiation->getMemberSpecializationInfo()) {
2025 MSInfo->setTemplateSpecializationKind(TSK);
2026 MSInfo->setPointOfInstantiation(PointOfInstantiation);
Fangrui Song6907ce22018-07-30 19:24:48 +00002027 } else if (ClassTemplateSpecializationDecl *Spec
Nico Weber3ffc4c92011-12-20 20:32:49 +00002028 = dyn_cast<ClassTemplateSpecializationDecl>(Instantiation)) {
Douglas Gregoref6ab412009-10-27 06:26:26 +00002029 Spec->setTemplateSpecializationKind(TSK);
2030 Spec->setPointOfInstantiation(PointOfInstantiation);
Douglas Gregord6ba93d2009-10-15 15:54:05 +00002031 }
Richard Smitha1087602014-03-10 00:04:29 +00002032
Douglas Gregorf3430ae2009-03-25 21:23:52 +00002033 InstantiatingTemplate Inst(*this, PointOfInstantiation, Instantiation);
Alp Tokerd4a72d52013-10-08 08:09:04 +00002034 if (Inst.isInvalid())
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00002035 return true;
Richard Smith54f18e82016-08-31 02:15:21 +00002036 assert(!Inst.isAlreadyInstantiating() && "should have been caught by caller");
Jordan Rose1e879d82018-03-23 00:07:18 +00002037 PrettyDeclStackTraceEntry CrashInfo(Context, Instantiation, SourceLocation(),
Richard Smithe19b95d2016-05-26 20:23:13 +00002038 "instantiating class definition");
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00002039
2040 // Enter the scope of this instantiation. We don't use
2041 // PushDeclContext because we don't have a scope.
John McCall80e58cd2010-04-29 00:35:03 +00002042 ContextRAII SavedContext(*this, Instantiation);
Faisal Valid143a0c2017-04-01 21:30:49 +00002043 EnterExpressionEvaluationContext EvalContext(
2044 *this, Sema::ExpressionEvaluationContext::PotentiallyEvaluated);
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00002045
Douglas Gregor51121572010-03-24 01:33:17 +00002046 // If this is an instantiation of a local class, merge this local
2047 // instantiation scope with the enclosing scope. Otherwise, every
2048 // instantiation of a class has its own local instantiation scope.
2049 bool MergeWithParentScope = !Instantiation->isDefinedOutsideFunctionOrMethod();
John McCall19c1bfd2010-08-25 05:32:35 +00002050 LocalInstantiationScope Scope(*this, MergeWithParentScope);
Douglas Gregor51121572010-03-24 01:33:17 +00002051
Volodymyr Sapsai4fbaa622017-09-21 19:54:12 +00002052 // Some class state isn't processed immediately but delayed till class
2053 // instantiation completes. We may not be ready to handle any delayed state
2054 // already on the stack as it might correspond to a different class, so save
2055 // it now and put it back later.
2056 SavePendingParsedClassStateRAII SavedPendingParsedClassState(*this);
Reid Kleckner5b640342016-02-26 19:51:02 +00002057
John McCall6602bb12010-08-01 02:01:53 +00002058 // Pull attributes from the pattern onto the instantiation.
2059 InstantiateAttrs(TemplateArgs, Pattern, Instantiation);
2060
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00002061 // Start the definition of this instantiation.
2062 Instantiation->startDefinition();
Richard Smitha1087602014-03-10 00:04:29 +00002063
2064 // The instantiation is visible here, even if it was first declared in an
2065 // unimported module.
Richard Smith90dc5252017-06-23 01:04:34 +00002066 Instantiation->setVisibleDespiteOwningModule();
Richard Smitha1087602014-03-10 00:04:29 +00002067
2068 // FIXME: This loses the as-written tag kind for an explicit instantiation.
Douglas Gregore9029562010-05-06 00:28:52 +00002069 Instantiation->setTagKind(Pattern->getTagKind());
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00002070
John McCall76d824f2009-08-25 22:02:44 +00002071 // Do substitution on the base class specifiers.
2072 if (SubstBaseSpecifiers(Instantiation, Pattern, TemplateArgs))
Douglas Gregorb9b8b812012-07-02 21:00:41 +00002073 Instantiation->setInvalidDecl();
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00002074
Douglas Gregor869853e2010-11-10 19:44:59 +00002075 TemplateDeclInstantiator Instantiator(*this, Instantiation, TemplateArgs);
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002076 SmallVector<Decl*, 4> Fields;
DeLesley Hutchins30398dd2012-01-20 22:50:54 +00002077 // Delay instantiation of late parsed attributes.
2078 LateInstantiatedAttrVec LateAttrs;
2079 Instantiator.enableLateAttributeInstantiation(&LateAttrs);
2080
Aaron Ballman629afae2014-03-07 19:56:05 +00002081 for (auto *Member : Pattern->decls()) {
Argyrios Kyrtzidis9a94d9b2010-11-04 03:18:57 +00002082 // Don't instantiate members not belonging in this semantic context.
2083 // e.g. for:
2084 // @code
2085 // template <int i> class A {
2086 // class B *g;
2087 // };
2088 // @endcode
2089 // 'class B' has the template as lexical context but semantically it is
2090 // introduced in namespace scope.
Aaron Ballman629afae2014-03-07 19:56:05 +00002091 if (Member->getDeclContext() != Pattern)
Argyrios Kyrtzidis9a94d9b2010-11-04 03:18:57 +00002092 continue;
2093
Erik Pilkingtona3a462e2018-07-23 22:47:37 +00002094 // BlockDecls can appear in a default-member-initializer. They must be the
2095 // child of a BlockExpr, so we only know how to instantiate them from there.
2096 if (isa<BlockDecl>(Member))
2097 continue;
2098
Aaron Ballman629afae2014-03-07 19:56:05 +00002099 if (Member->isInvalidDecl()) {
Richard Smithded9c2e2012-07-11 22:37:56 +00002100 Instantiation->setInvalidDecl();
Douglas Gregor869853e2010-11-10 19:44:59 +00002101 continue;
2102 }
2103
Aaron Ballman629afae2014-03-07 19:56:05 +00002104 Decl *NewMember = Instantiator.Visit(Member);
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00002105 if (NewMember) {
Richard Smith938f40b2011-06-11 17:19:42 +00002106 if (FieldDecl *Field = dyn_cast<FieldDecl>(NewMember)) {
John McCall48871652010-08-21 09:40:31 +00002107 Fields.push_back(Field);
Richard Smith7d137e32012-03-23 03:33:32 +00002108 } else if (EnumDecl *Enum = dyn_cast<EnumDecl>(NewMember)) {
2109 // C++11 [temp.inst]p1: The implicit instantiation of a class template
2110 // specialization causes the implicit instantiation of the definitions
2111 // of unscoped member enumerations.
2112 // Record a point of instantiation for this implicit instantiation.
Richard Smithb66d7772012-03-23 23:09:08 +00002113 if (TSK == TSK_ImplicitInstantiation && !Enum->isScoped() &&
2114 Enum->isCompleteDefinition()) {
Richard Smith7d137e32012-03-23 03:33:32 +00002115 MemberSpecializationInfo *MSInfo =Enum->getMemberSpecializationInfo();
2116 assert(MSInfo && "no spec info for member enum specialization");
2117 MSInfo->setTemplateSpecializationKind(TSK_ImplicitInstantiation);
2118 MSInfo->setPointOfInstantiation(PointOfInstantiation);
2119 }
Richard Smithded9c2e2012-07-11 22:37:56 +00002120 } else if (StaticAssertDecl *SA = dyn_cast<StaticAssertDecl>(NewMember)) {
2121 if (SA->isFailed()) {
2122 // A static_assert failed. Bail out; instantiating this
2123 // class is probably not meaningful.
2124 Instantiation->setInvalidDecl();
2125 break;
2126 }
Richard Smith7d137e32012-03-23 03:33:32 +00002127 }
2128
2129 if (NewMember->isInvalidDecl())
Douglas Gregorb9b8b812012-07-02 21:00:41 +00002130 Instantiation->setInvalidDecl();
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00002131 } else {
2132 // FIXME: Eventually, a NULL return will mean that one of the
Douglas Gregorb9b8b812012-07-02 21:00:41 +00002133 // instantiations was a semantic disaster, and we'll want to mark the
2134 // declaration invalid.
2135 // For now, we expect to skip some members that we can't yet handle.
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00002136 }
2137 }
2138
2139 // Finish checking fields.
Craig Topperc3ec1492014-05-26 06:22:03 +00002140 ActOnFields(nullptr, Instantiation->getLocation(), Instantiation, Fields,
Erich Keanec480f302018-07-12 21:09:05 +00002141 SourceLocation(), SourceLocation(), ParsedAttributesView());
Douglas Gregor0be31a22010-07-02 17:43:08 +00002142 CheckCompletedCXXClass(Instantiation);
Richard Smith938f40b2011-06-11 17:19:42 +00002143
Reid Kleckner93f661a2015-03-17 21:51:43 +00002144 // Default arguments are parsed, if not instantiated. We can go instantiate
2145 // default arg exprs for default constructors if necessary now.
Hans Wennborg99000c22015-08-15 01:18:16 +00002146 ActOnFinishCXXNonNestedClass(Instantiation);
Reid Kleckner93f661a2015-03-17 21:51:43 +00002147
DeLesley Hutchins30398dd2012-01-20 22:50:54 +00002148 // Instantiate late parsed attributes, and attach them to their decls.
2149 // See Sema::InstantiateAttrs
2150 for (LateInstantiatedAttrVec::iterator I = LateAttrs.begin(),
2151 E = LateAttrs.end(); I != E; ++I) {
2152 assert(CurrentInstantiationScope == Instantiator.getStartingScope());
2153 CurrentInstantiationScope = I->Scope;
Richard Smithc3d2ebb2013-06-07 02:33:37 +00002154
2155 // Allow 'this' within late-parsed attributes.
2156 NamedDecl *ND = dyn_cast<NamedDecl>(I->NewDecl);
2157 CXXRecordDecl *ThisContext =
2158 dyn_cast_or_null<CXXRecordDecl>(ND->getDeclContext());
Mikael Nilsson9d2872d2018-12-13 10:15:27 +00002159 CXXThisScopeRAII ThisScope(*this, ThisContext, Qualifiers(),
Richard Smithc3d2ebb2013-06-07 02:33:37 +00002160 ND && ND->isCXXInstanceMember());
2161
DeLesley Hutchins30398dd2012-01-20 22:50:54 +00002162 Attr *NewAttr =
2163 instantiateTemplateAttribute(I->TmplAttr, Context, *this, TemplateArgs);
2164 I->NewDecl->addAttr(NewAttr);
2165 LocalInstantiationScope::deleteScopes(I->Scope,
2166 Instantiator.getStartingScope());
2167 }
2168 Instantiator.disableLateAttributeInstantiation();
2169 LateAttrs.clear();
2170
Richard Smithd3b5c9082012-07-27 04:22:15 +00002171 ActOnFinishDelayedMemberInitializers(Instantiation);
Richard Smith938f40b2011-06-11 17:19:42 +00002172
Richard Smitha1087602014-03-10 00:04:29 +00002173 // FIXME: We should do something similar for explicit instantiations so they
2174 // end up in the right module.
Abramo Bagnara12dcbf32011-11-18 08:08:52 +00002175 if (TSK == TSK_ImplicitInstantiation) {
Argyrios Kyrtzidise3789482012-02-11 01:59:57 +00002176 Instantiation->setLocation(Pattern->getLocation());
Abramo Bagnara12dcbf32011-11-18 08:08:52 +00002177 Instantiation->setLocStart(Pattern->getInnerLocStart());
Argyrios Kyrtzidisd798c052016-07-15 18:11:33 +00002178 Instantiation->setBraceRange(Pattern->getBraceRange());
Abramo Bagnara12dcbf32011-11-18 08:08:52 +00002179 }
Abramo Bagnarafd3a4552011-10-03 20:34:03 +00002180
Douglas Gregorb9b8b812012-07-02 21:00:41 +00002181 if (!Instantiation->isInvalidDecl()) {
John McCalla0a96892012-08-10 03:15:35 +00002182 // Perform any dependent diagnostics from the pattern.
2183 PerformDependentDiagnostics(Pattern, TemplateArgs);
2184
Douglas Gregor869853e2010-11-10 19:44:59 +00002185 // Instantiate any out-of-line class template partial
2186 // specializations now.
Richard Smith10b55fc2013-09-26 03:49:48 +00002187 for (TemplateDeclInstantiator::delayed_partial_spec_iterator
Douglas Gregor869853e2010-11-10 19:44:59 +00002188 P = Instantiator.delayed_partial_spec_begin(),
2189 PEnd = Instantiator.delayed_partial_spec_end();
2190 P != PEnd; ++P) {
2191 if (!Instantiator.InstantiateClassTemplatePartialSpecialization(
Richard Smith10b55fc2013-09-26 03:49:48 +00002192 P->first, P->second)) {
2193 Instantiation->setInvalidDecl();
2194 break;
2195 }
2196 }
2197
2198 // Instantiate any out-of-line variable template partial
2199 // specializations now.
2200 for (TemplateDeclInstantiator::delayed_var_partial_spec_iterator
2201 P = Instantiator.delayed_var_partial_spec_begin(),
2202 PEnd = Instantiator.delayed_var_partial_spec_end();
2203 P != PEnd; ++P) {
2204 if (!Instantiator.InstantiateVarTemplatePartialSpecialization(
2205 P->first, P->second)) {
Douglas Gregorb9b8b812012-07-02 21:00:41 +00002206 Instantiation->setInvalidDecl();
Douglas Gregor869853e2010-11-10 19:44:59 +00002207 break;
2208 }
2209 }
2210 }
2211
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00002212 // Exit the scope of this instantiation.
John McCall80e58cd2010-04-29 00:35:03 +00002213 SavedContext.pop();
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00002214
Douglas Gregorb9b8b812012-07-02 21:00:41 +00002215 if (!Instantiation->isInvalidDecl()) {
Douglas Gregor28ad4b52009-05-26 20:50:29 +00002216 Consumer.HandleTagDeclDefinition(Instantiation);
2217
Douglas Gregor88d292c2010-05-13 16:44:06 +00002218 // Always emit the vtable for an explicit instantiation definition
2219 // of a polymorphic class template specialization.
2220 if (TSK == TSK_ExplicitInstantiationDefinition)
2221 MarkVTableUsed(PointOfInstantiation, Instantiation, true);
2222 }
2223
Douglas Gregorb9b8b812012-07-02 21:00:41 +00002224 return Instantiation->isInvalidDecl();
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00002225}
2226
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002227/// Instantiate the definition of an enum from a given pattern.
Richard Smith4b38ded2012-03-14 23:13:10 +00002228///
2229/// \param PointOfInstantiation The point of instantiation within the
2230/// source code.
2231/// \param Instantiation is the declaration whose definition is being
2232/// instantiated. This will be a member enumeration of a class
2233/// temploid specialization, or a local enumeration within a
2234/// function temploid specialization.
2235/// \param Pattern The templated declaration from which the instantiation
2236/// occurs.
2237/// \param TemplateArgs The template arguments to be substituted into
2238/// the pattern.
2239/// \param TSK The kind of implicit or explicit instantiation to perform.
2240///
2241/// \return \c true if an error occurred, \c false otherwise.
2242bool Sema::InstantiateEnum(SourceLocation PointOfInstantiation,
2243 EnumDecl *Instantiation, EnumDecl *Pattern,
2244 const MultiLevelTemplateArgumentList &TemplateArgs,
2245 TemplateSpecializationKind TSK) {
2246 EnumDecl *PatternDef = Pattern->getDefinition();
Vassil Vassilevb21ee082016-08-18 22:01:25 +00002247 if (DiagnoseUninstantiableTemplate(PointOfInstantiation, Instantiation,
Richard Smith4b38ded2012-03-14 23:13:10 +00002248 Instantiation->getInstantiatedFromMemberEnum(),
2249 Pattern, PatternDef, TSK,/*Complain*/true))
2250 return true;
2251 Pattern = PatternDef;
2252
2253 // Record the point of instantiation.
2254 if (MemberSpecializationInfo *MSInfo
2255 = Instantiation->getMemberSpecializationInfo()) {
2256 MSInfo->setTemplateSpecializationKind(TSK);
2257 MSInfo->setPointOfInstantiation(PointOfInstantiation);
2258 }
2259
2260 InstantiatingTemplate Inst(*this, PointOfInstantiation, Instantiation);
Alp Tokerd4a72d52013-10-08 08:09:04 +00002261 if (Inst.isInvalid())
Richard Smith4b38ded2012-03-14 23:13:10 +00002262 return true;
Richard Smith54f18e82016-08-31 02:15:21 +00002263 if (Inst.isAlreadyInstantiating())
2264 return false;
Jordan Rose1e879d82018-03-23 00:07:18 +00002265 PrettyDeclStackTraceEntry CrashInfo(Context, Instantiation, SourceLocation(),
Richard Smithe19b95d2016-05-26 20:23:13 +00002266 "instantiating enum definition");
Richard Smith4b38ded2012-03-14 23:13:10 +00002267
Richard Smitha1087602014-03-10 00:04:29 +00002268 // The instantiation is visible here, even if it was first declared in an
2269 // unimported module.
Richard Smith90dc5252017-06-23 01:04:34 +00002270 Instantiation->setVisibleDespiteOwningModule();
Richard Smitha1087602014-03-10 00:04:29 +00002271
Richard Smith4b38ded2012-03-14 23:13:10 +00002272 // Enter the scope of this instantiation. We don't use
2273 // PushDeclContext because we don't have a scope.
2274 ContextRAII SavedContext(*this, Instantiation);
Faisal Valid143a0c2017-04-01 21:30:49 +00002275 EnterExpressionEvaluationContext EvalContext(
2276 *this, Sema::ExpressionEvaluationContext::PotentiallyEvaluated);
Richard Smith4b38ded2012-03-14 23:13:10 +00002277
2278 LocalInstantiationScope Scope(*this, /*MergeWithParentScope*/true);
2279
2280 // Pull attributes from the pattern onto the instantiation.
2281 InstantiateAttrs(TemplateArgs, Pattern, Instantiation);
2282
2283 TemplateDeclInstantiator Instantiator(*this, Instantiation, TemplateArgs);
2284 Instantiator.InstantiateEnumDefinition(Instantiation, Pattern);
2285
2286 // Exit the scope of this instantiation.
2287 SavedContext.pop();
2288
2289 return Instantiation->isInvalidDecl();
2290}
2291
Reid Klecknerd60b82f2014-11-17 23:36:45 +00002292
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002293/// Instantiate the definition of a field from the given pattern.
Reid Klecknerd60b82f2014-11-17 23:36:45 +00002294///
2295/// \param PointOfInstantiation The point of instantiation within the
2296/// source code.
2297/// \param Instantiation is the declaration whose definition is being
2298/// instantiated. This will be a class of a class temploid
2299/// specialization, or a local enumeration within a function temploid
2300/// specialization.
2301/// \param Pattern The templated declaration from which the instantiation
2302/// occurs.
2303/// \param TemplateArgs The template arguments to be substituted into
2304/// the pattern.
2305///
2306/// \return \c true if an error occurred, \c false otherwise.
2307bool Sema::InstantiateInClassInitializer(
2308 SourceLocation PointOfInstantiation, FieldDecl *Instantiation,
2309 FieldDecl *Pattern, const MultiLevelTemplateArgumentList &TemplateArgs) {
2310 // If there is no initializer, we don't need to do anything.
2311 if (!Pattern->hasInClassInitializer())
2312 return false;
2313
2314 assert(Instantiation->getInClassInitStyle() ==
2315 Pattern->getInClassInitStyle() &&
2316 "pattern and instantiation disagree about init style");
2317
2318 // Error out if we haven't parsed the initializer of the pattern yet because
2319 // we are waiting for the closing brace of the outer class.
2320 Expr *OldInit = Pattern->getInClassInitializer();
2321 if (!OldInit) {
2322 RecordDecl *PatternRD = Pattern->getParent();
2323 RecordDecl *OutermostClass = PatternRD->getOuterLexicalRecordContext();
Richard Smith8dbc6b22016-11-22 22:55:12 +00002324 Diag(PointOfInstantiation,
2325 diag::err_in_class_initializer_not_yet_parsed)
2326 << OutermostClass << Pattern;
Stephen Kelly1c301dc2018-08-09 21:09:38 +00002327 Diag(Pattern->getEndLoc(), diag::note_in_class_initializer_not_yet_parsed);
Reid Klecknerd60b82f2014-11-17 23:36:45 +00002328 Instantiation->setInvalidDecl();
2329 return true;
2330 }
2331
2332 InstantiatingTemplate Inst(*this, PointOfInstantiation, Instantiation);
2333 if (Inst.isInvalid())
2334 return true;
Richard Smith54f18e82016-08-31 02:15:21 +00002335 if (Inst.isAlreadyInstantiating()) {
2336 // Error out if we hit an instantiation cycle for this initializer.
2337 Diag(PointOfInstantiation, diag::err_in_class_initializer_cycle)
2338 << Instantiation;
2339 return true;
2340 }
Jordan Rose1e879d82018-03-23 00:07:18 +00002341 PrettyDeclStackTraceEntry CrashInfo(Context, Instantiation, SourceLocation(),
Richard Smithe19b95d2016-05-26 20:23:13 +00002342 "instantiating default member init");
Reid Klecknerd60b82f2014-11-17 23:36:45 +00002343
2344 // Enter the scope of this instantiation. We don't use PushDeclContext because
2345 // we don't have a scope.
2346 ContextRAII SavedContext(*this, Instantiation->getParent());
Faisal Valid143a0c2017-04-01 21:30:49 +00002347 EnterExpressionEvaluationContext EvalContext(
2348 *this, Sema::ExpressionEvaluationContext::PotentiallyEvaluated);
Reid Klecknerd60b82f2014-11-17 23:36:45 +00002349
Serge Pavlov907233f2015-04-28 17:58:47 +00002350 LocalInstantiationScope Scope(*this, true);
Reid Klecknerd60b82f2014-11-17 23:36:45 +00002351
2352 // Instantiate the initializer.
2353 ActOnStartCXXInClassMemberInitializer();
Mikael Nilsson9d2872d2018-12-13 10:15:27 +00002354 CXXThisScopeRAII ThisScope(*this, Instantiation->getParent(), Qualifiers());
Reid Klecknerd60b82f2014-11-17 23:36:45 +00002355
2356 ExprResult NewInit = SubstInitializer(OldInit, TemplateArgs,
2357 /*CXXDirectInit=*/false);
2358 Expr *Init = NewInit.get();
2359 assert((!Init || !isa<ParenListExpr>(Init)) && "call-style init in class");
2360 ActOnFinishCXXInClassMemberInitializer(
Stephen Kellyf2ceec42018-08-09 21:08:08 +00002361 Instantiation, Init ? Init->getBeginLoc() : SourceLocation(), Init);
Reid Klecknerd60b82f2014-11-17 23:36:45 +00002362
Richard Smith4b054b22016-08-24 21:25:37 +00002363 if (auto *L = getASTMutationListener())
2364 L->DefaultMemberInitializerInstantiated(Instantiation);
2365
Reid Klecknerd60b82f2014-11-17 23:36:45 +00002366 // Return true if the in-class initializer is still missing.
2367 return !Instantiation->getInClassInitializer();
2368}
2369
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +00002370namespace {
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002371 /// A partial specialization whose template arguments have matched
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +00002372 /// a given template-id.
2373 struct PartialSpecMatchResult {
2374 ClassTemplatePartialSpecializationDecl *Partial;
2375 TemplateArgumentList *Args;
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +00002376 };
2377}
2378
Richard Smithe6d4b772017-06-07 02:42:27 +00002379bool Sema::usesPartialOrExplicitSpecialization(
2380 SourceLocation Loc, ClassTemplateSpecializationDecl *ClassTemplateSpec) {
2381 if (ClassTemplateSpec->getTemplateSpecializationKind() ==
2382 TSK_ExplicitSpecialization)
2383 return true;
2384
2385 SmallVector<ClassTemplatePartialSpecializationDecl *, 4> PartialSpecs;
2386 ClassTemplateSpec->getSpecializedTemplate()
2387 ->getPartialSpecializations(PartialSpecs);
2388 for (unsigned I = 0, N = PartialSpecs.size(); I != N; ++I) {
2389 TemplateDeductionInfo Info(Loc);
2390 if (!DeduceTemplateArguments(PartialSpecs[I],
2391 ClassTemplateSpec->getTemplateArgs(), Info))
2392 return true;
2393 }
2394
2395 return false;
2396}
2397
Richard Smith32b43762017-01-08 22:45:21 +00002398/// Get the instantiation pattern to use to instantiate the definition of a
2399/// given ClassTemplateSpecializationDecl (either the pattern of the primary
2400/// template or of a partial specialization).
2401static CXXRecordDecl *
2402getPatternForClassTemplateSpecialization(
2403 Sema &S, SourceLocation PointOfInstantiation,
Larisse Voufo72caf2b2013-08-22 00:59:14 +00002404 ClassTemplateSpecializationDecl *ClassTemplateSpec,
2405 TemplateSpecializationKind TSK, bool Complain) {
Richard Smith32b43762017-01-08 22:45:21 +00002406 Sema::InstantiatingTemplate Inst(S, PointOfInstantiation, ClassTemplateSpec);
2407 if (Inst.isInvalid() || Inst.isAlreadyInstantiating())
2408 return nullptr;
2409
Volodymyr Sapsai9b973482018-05-16 18:28:58 +00002410 llvm::PointerUnion<ClassTemplateDecl *,
2411 ClassTemplatePartialSpecializationDecl *>
2412 Specialized = ClassTemplateSpec->getSpecializedTemplateOrPartial();
2413 if (!Specialized.is<ClassTemplatePartialSpecializationDecl *>()) {
2414 // Find best matching specialization.
2415 ClassTemplateDecl *Template = ClassTemplateSpec->getSpecializedTemplate();
Douglas Gregor2373c592009-05-31 09:31:02 +00002416
Volodymyr Sapsai9b973482018-05-16 18:28:58 +00002417 // C++ [temp.class.spec.match]p1:
2418 // When a class template is used in a context that requires an
2419 // instantiation of the class, it is necessary to determine
2420 // whether the instantiation is to be generated using the primary
2421 // template or one of the partial specializations. This is done by
2422 // matching the template arguments of the class template
2423 // specialization with the template argument lists of the partial
2424 // specializations.
2425 typedef PartialSpecMatchResult MatchResult;
2426 SmallVector<MatchResult, 4> Matched;
2427 SmallVector<ClassTemplatePartialSpecializationDecl *, 4> PartialSpecs;
2428 Template->getPartialSpecializations(PartialSpecs);
2429 TemplateSpecCandidateSet FailedCandidates(PointOfInstantiation);
2430 for (unsigned I = 0, N = PartialSpecs.size(); I != N; ++I) {
2431 ClassTemplatePartialSpecializationDecl *Partial = PartialSpecs[I];
2432 TemplateDeductionInfo Info(FailedCandidates.getLocation());
2433 if (Sema::TemplateDeductionResult Result = S.DeduceTemplateArguments(
2434 Partial, ClassTemplateSpec->getTemplateArgs(), Info)) {
2435 // Store the failed-deduction information for use in diagnostics, later.
2436 // TODO: Actually use the failed-deduction info?
2437 FailedCandidates.addCandidate().set(
2438 DeclAccessPair::make(Template, AS_public), Partial,
2439 MakeDeductionFailureInfo(S.Context, Result, Info));
2440 (void)Result;
2441 } else {
2442 Matched.push_back(PartialSpecMatchResult());
2443 Matched.back().Partial = Partial;
2444 Matched.back().Args = Info.take();
2445 }
2446 }
2447
2448 // If we're dealing with a member template where the template parameters
2449 // have been instantiated, this provides the original template parameters
2450 // from which the member template's parameters were instantiated.
2451
2452 if (Matched.size() >= 1) {
2453 SmallVectorImpl<MatchResult>::iterator Best = Matched.begin();
2454 if (Matched.size() == 1) {
2455 // -- If exactly one matching specialization is found, the
2456 // instantiation is generated from that specialization.
2457 // We don't need to do anything for this.
2458 } else {
2459 // -- If more than one matching specialization is found, the
2460 // partial order rules (14.5.4.2) are used to determine
2461 // whether one of the specializations is more specialized
2462 // than the others. If none of the specializations is more
2463 // specialized than all of the other matching
2464 // specializations, then the use of the class template is
2465 // ambiguous and the program is ill-formed.
2466 for (SmallVectorImpl<MatchResult>::iterator P = Best + 1,
2467 PEnd = Matched.end();
2468 P != PEnd; ++P) {
2469 if (S.getMoreSpecializedPartialSpecialization(
2470 P->Partial, Best->Partial, PointOfInstantiation) ==
2471 P->Partial)
2472 Best = P;
2473 }
2474
2475 // Determine if the best partial specialization is more specialized than
2476 // the others.
2477 bool Ambiguous = false;
2478 for (SmallVectorImpl<MatchResult>::iterator P = Matched.begin(),
2479 PEnd = Matched.end();
2480 P != PEnd; ++P) {
2481 if (P != Best && S.getMoreSpecializedPartialSpecialization(
2482 P->Partial, Best->Partial,
2483 PointOfInstantiation) != Best->Partial) {
2484 Ambiguous = true;
2485 break;
2486 }
2487 }
2488
2489 if (Ambiguous) {
2490 // Partial ordering did not produce a clear winner. Complain.
2491 Inst.Clear();
2492 ClassTemplateSpec->setInvalidDecl();
2493 S.Diag(PointOfInstantiation,
2494 diag::err_partial_spec_ordering_ambiguous)
2495 << ClassTemplateSpec;
2496
2497 // Print the matching partial specializations.
2498 for (SmallVectorImpl<MatchResult>::iterator P = Matched.begin(),
2499 PEnd = Matched.end();
2500 P != PEnd; ++P)
2501 S.Diag(P->Partial->getLocation(), diag::note_partial_spec_match)
2502 << S.getTemplateArgumentBindingsText(
2503 P->Partial->getTemplateParameters(), *P->Args);
2504
2505 return nullptr;
2506 }
2507 }
2508
2509 ClassTemplateSpec->setInstantiationOf(Best->Partial, Best->Args);
Douglas Gregor181aa4a2009-06-12 18:26:56 +00002510 } else {
Volodymyr Sapsai9b973482018-05-16 18:28:58 +00002511 // -- If no matches are found, the instantiation is generated
2512 // from the primary template.
Douglas Gregor181aa4a2009-06-12 18:26:56 +00002513 }
Douglas Gregor2373c592009-05-31 09:31:02 +00002514 }
2515
Volodymyr Sapsai9b973482018-05-16 18:28:58 +00002516 CXXRecordDecl *Pattern = nullptr;
2517 Specialized = ClassTemplateSpec->getSpecializedTemplateOrPartial();
2518 if (auto *PartialSpec =
2519 Specialized.dyn_cast<ClassTemplatePartialSpecializationDecl *>()) {
Douglas Gregorbe999392009-09-15 16:23:51 +00002520 // Instantiate using the best class template partial specialization.
Volodymyr Sapsai9b973482018-05-16 18:28:58 +00002521 while (PartialSpec->getInstantiatedFromMember()) {
Douglas Gregor21610382009-10-29 00:04:11 +00002522 // If we've found an explicit specialization of this class template,
2523 // stop here and use that as the pattern.
Volodymyr Sapsai9b973482018-05-16 18:28:58 +00002524 if (PartialSpec->isMemberSpecialization())
Douglas Gregor21610382009-10-29 00:04:11 +00002525 break;
Volodymyr Sapsai9b973482018-05-16 18:28:58 +00002526
2527 PartialSpec = PartialSpec->getInstantiatedFromMember();
Douglas Gregor21610382009-10-29 00:04:11 +00002528 }
Volodymyr Sapsai9b973482018-05-16 18:28:58 +00002529 Pattern = PartialSpec;
Douglas Gregor170bc422009-06-12 22:31:52 +00002530 } else {
Volodymyr Sapsai9b973482018-05-16 18:28:58 +00002531 ClassTemplateDecl *Template = ClassTemplateSpec->getSpecializedTemplate();
2532 while (Template->getInstantiatedFromMemberTemplate()) {
Douglas Gregorcf915552009-10-13 16:30:37 +00002533 // If we've found an explicit specialization of this class template,
2534 // stop here and use that as the pattern.
Volodymyr Sapsai9b973482018-05-16 18:28:58 +00002535 if (Template->isMemberSpecialization())
Douglas Gregorcf915552009-10-13 16:30:37 +00002536 break;
Volodymyr Sapsai9b973482018-05-16 18:28:58 +00002537
2538 Template = Template->getInstantiatedFromMemberTemplate();
Douglas Gregorcf915552009-10-13 16:30:37 +00002539 }
Volodymyr Sapsai9b973482018-05-16 18:28:58 +00002540 Pattern = Template->getTemplatedDecl();
Douglas Gregor2373c592009-05-31 09:31:02 +00002541 }
Douglas Gregor463421d2009-03-03 04:44:36 +00002542
Richard Smith32b43762017-01-08 22:45:21 +00002543 return Pattern;
2544}
Mike Stump11289f42009-09-09 15:08:12 +00002545
Richard Smith32b43762017-01-08 22:45:21 +00002546bool Sema::InstantiateClassTemplateSpecialization(
2547 SourceLocation PointOfInstantiation,
2548 ClassTemplateSpecializationDecl *ClassTemplateSpec,
2549 TemplateSpecializationKind TSK, bool Complain) {
2550 // Perform the actual instantiation on the canonical declaration.
2551 ClassTemplateSpec = cast<ClassTemplateSpecializationDecl>(
2552 ClassTemplateSpec->getCanonicalDecl());
2553 if (ClassTemplateSpec->isInvalidDecl())
2554 return true;
2555
2556 CXXRecordDecl *Pattern = getPatternForClassTemplateSpecialization(
2557 *this, PointOfInstantiation, ClassTemplateSpec, TSK, Complain);
2558 if (!Pattern)
2559 return true;
2560
2561 return InstantiateClass(PointOfInstantiation, ClassTemplateSpec, Pattern,
2562 getTemplateInstantiationArgs(ClassTemplateSpec), TSK,
2563 Complain);
Douglas Gregor463421d2009-03-03 04:44:36 +00002564}
Douglas Gregor90a1a652009-03-19 17:26:29 +00002565
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002566/// Instantiates the definitions of all of the member
John McCall76d824f2009-08-25 22:02:44 +00002567/// of the given class, which is an instantiation of a class template
2568/// or a member class of a template.
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00002569void
2570Sema::InstantiateClassMembers(SourceLocation PointOfInstantiation,
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00002571 CXXRecordDecl *Instantiation,
2572 const MultiLevelTemplateArgumentList &TemplateArgs,
2573 TemplateSpecializationKind TSK) {
Richard Smitheb36ddf2014-04-24 22:45:46 +00002574 // FIXME: We need to notify the ASTMutationListener that we did all of these
2575 // things, in case we have an explicit instantiation definition in a PCM, a
2576 // module, or preamble, and the declaration is in an imported AST.
David Majnemer192d1792013-11-27 08:20:38 +00002577 assert(
2578 (TSK == TSK_ExplicitInstantiationDefinition ||
2579 TSK == TSK_ExplicitInstantiationDeclaration ||
2580 (TSK == TSK_ImplicitInstantiation && Instantiation->isLocalClass())) &&
2581 "Unexpected template specialization kind!");
Aaron Ballman629afae2014-03-07 19:56:05 +00002582 for (auto *D : Instantiation->decls()) {
Douglas Gregor1d957a32009-10-27 18:42:08 +00002583 bool SuppressNew = false;
Aaron Ballman629afae2014-03-07 19:56:05 +00002584 if (auto *Function = dyn_cast<FunctionDecl>(D)) {
Louis Dionned2695792018-10-04 15:49:42 +00002585 if (FunctionDecl *Pattern =
2586 Function->getInstantiatedFromMemberFunction()) {
2587
2588 if (Function->hasAttr<ExcludeFromExplicitInstantiationAttr>())
2589 continue;
2590
2591 MemberSpecializationInfo *MSInfo =
2592 Function->getMemberSpecializationInfo();
Douglas Gregor1d957a32009-10-27 18:42:08 +00002593 assert(MSInfo && "No member specialization information?");
Douglas Gregor06aa50412010-04-09 21:02:29 +00002594 if (MSInfo->getTemplateSpecializationKind()
2595 == TSK_ExplicitSpecialization)
2596 continue;
Fangrui Song6907ce22018-07-30 19:24:48 +00002597
2598 if (CheckSpecializationInstantiationRedecl(PointOfInstantiation, TSK,
2599 Function,
Douglas Gregor1d957a32009-10-27 18:42:08 +00002600 MSInfo->getTemplateSpecializationKind(),
Nico Weberd75488d2010-09-27 21:02:09 +00002601 MSInfo->getPointOfInstantiation(),
Douglas Gregor1d957a32009-10-27 18:42:08 +00002602 SuppressNew) ||
2603 SuppressNew)
Douglas Gregorbbe8f462009-10-08 15:14:33 +00002604 continue;
Richard Smitheb36ddf2014-04-24 22:45:46 +00002605
2606 // C++11 [temp.explicit]p8:
2607 // An explicit instantiation definition that names a class template
2608 // specialization explicitly instantiates the class template
2609 // specialization and is only an explicit instantiation definition
2610 // of members whose definition is visible at the point of
2611 // instantiation.
2612 if (TSK == TSK_ExplicitInstantiationDefinition && !Pattern->isDefined())
Douglas Gregor1d957a32009-10-27 18:42:08 +00002613 continue;
2614
Richard Smitheb36ddf2014-04-24 22:45:46 +00002615 Function->setTemplateSpecializationKind(TSK, PointOfInstantiation);
2616
2617 if (Function->isDefined()) {
2618 // Let the ASTConsumer know that this function has been explicitly
2619 // instantiated now, and its linkage might have changed.
2620 Consumer.HandleTopLevelDecl(DeclGroupRef(Function));
2621 } else if (TSK == TSK_ExplicitInstantiationDefinition) {
Douglas Gregor1d957a32009-10-27 18:42:08 +00002622 InstantiateFunctionDefinition(PointOfInstantiation, Function);
Richard Smitheb36ddf2014-04-24 22:45:46 +00002623 } else if (TSK == TSK_ImplicitInstantiation) {
2624 PendingLocalImplicitInstantiations.push_back(
2625 std::make_pair(Function, PointOfInstantiation));
Douglas Gregor1d957a32009-10-27 18:42:08 +00002626 }
Douglas Gregorbbe8f462009-10-08 15:14:33 +00002627 }
Aaron Ballman629afae2014-03-07 19:56:05 +00002628 } else if (auto *Var = dyn_cast<VarDecl>(D)) {
Richard Smith8809a0c2013-09-27 20:14:12 +00002629 if (isa<VarTemplateSpecializationDecl>(Var))
2630 continue;
2631
Douglas Gregor86d142a2009-10-08 07:24:58 +00002632 if (Var->isStaticDataMember()) {
Louis Dionned2695792018-10-04 15:49:42 +00002633 if (Var->hasAttr<ExcludeFromExplicitInstantiationAttr>())
2634 continue;
2635
Douglas Gregor1d957a32009-10-27 18:42:08 +00002636 MemberSpecializationInfo *MSInfo = Var->getMemberSpecializationInfo();
2637 assert(MSInfo && "No member specialization information?");
Douglas Gregor06aa50412010-04-09 21:02:29 +00002638 if (MSInfo->getTemplateSpecializationKind()
2639 == TSK_ExplicitSpecialization)
2640 continue;
Fangrui Song6907ce22018-07-30 19:24:48 +00002641
2642 if (CheckSpecializationInstantiationRedecl(PointOfInstantiation, TSK,
2643 Var,
Douglas Gregor1d957a32009-10-27 18:42:08 +00002644 MSInfo->getTemplateSpecializationKind(),
Nico Weberd75488d2010-09-27 21:02:09 +00002645 MSInfo->getPointOfInstantiation(),
Douglas Gregor1d957a32009-10-27 18:42:08 +00002646 SuppressNew) ||
2647 SuppressNew)
Douglas Gregorbbe8f462009-10-08 15:14:33 +00002648 continue;
Fangrui Song6907ce22018-07-30 19:24:48 +00002649
Douglas Gregor1d957a32009-10-27 18:42:08 +00002650 if (TSK == TSK_ExplicitInstantiationDefinition) {
2651 // C++0x [temp.explicit]p8:
2652 // An explicit instantiation definition that names a class template
Fangrui Song6907ce22018-07-30 19:24:48 +00002653 // specialization explicitly instantiates the class template
2654 // specialization and is only an explicit instantiation definition
2655 // of members whose definition is visible at the point of
Douglas Gregor1d957a32009-10-27 18:42:08 +00002656 // instantiation.
Richard Smith62f19e72016-06-25 00:15:56 +00002657 if (!Var->getInstantiatedFromStaticDataMember()->getDefinition())
Douglas Gregor1d957a32009-10-27 18:42:08 +00002658 continue;
Fangrui Song6907ce22018-07-30 19:24:48 +00002659
Douglas Gregor1d957a32009-10-27 18:42:08 +00002660 Var->setTemplateSpecializationKind(TSK, PointOfInstantiation);
Richard Smith891fc7f2017-12-05 01:31:47 +00002661 InstantiateVariableDefinition(PointOfInstantiation, Var);
Douglas Gregor1d957a32009-10-27 18:42:08 +00002662 } else {
2663 Var->setTemplateSpecializationKind(TSK, PointOfInstantiation);
2664 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002665 }
Aaron Ballman629afae2014-03-07 19:56:05 +00002666 } else if (auto *Record = dyn_cast<CXXRecordDecl>(D)) {
Louis Dionned2695792018-10-04 15:49:42 +00002667 if (Record->hasAttr<ExcludeFromExplicitInstantiationAttr>())
2668 continue;
2669
Douglas Gregor1da22252010-04-18 18:11:38 +00002670 // Always skip the injected-class-name, along with any
2671 // redeclarations of nested classes, since both would cause us
2672 // to try to instantiate the members of a class twice.
Richard Smith069ecf62014-11-20 22:56:34 +00002673 // Skip closure types; they'll get instantiated when we instantiate
2674 // the corresponding lambda-expression.
2675 if (Record->isInjectedClassName() || Record->getPreviousDecl() ||
2676 Record->isLambda())
Douglas Gregord801b062009-10-07 23:56:10 +00002677 continue;
Fangrui Song6907ce22018-07-30 19:24:48 +00002678
Douglas Gregor1d957a32009-10-27 18:42:08 +00002679 MemberSpecializationInfo *MSInfo = Record->getMemberSpecializationInfo();
2680 assert(MSInfo && "No member specialization information?");
Fangrui Song6907ce22018-07-30 19:24:48 +00002681
Douglas Gregor06aa50412010-04-09 21:02:29 +00002682 if (MSInfo->getTemplateSpecializationKind()
2683 == TSK_ExplicitSpecialization)
2684 continue;
Nico Weberd75488d2010-09-27 21:02:09 +00002685
Reid Kleckner5f789ba2019-04-29 21:32:05 +00002686 if (Context.getTargetInfo().getTriple().isOSWindows() &&
Hans Wennborga86a83b2016-05-26 19:42:56 +00002687 TSK == TSK_ExplicitInstantiationDeclaration) {
Reid Kleckner5f789ba2019-04-29 21:32:05 +00002688 // On Windows, explicit instantiation decl of the outer class doesn't
2689 // affect the inner class. Typically extern template declarations are
2690 // used in combination with dll import/export annotations, but those
2691 // are not propagated from the outer class templates to inner classes.
2692 // Therefore, do not instantiate inner classes on this platform, so
2693 // that users don't end up with undefined symbols during linking.
Hans Wennborga86a83b2016-05-26 19:42:56 +00002694 continue;
2695 }
2696
Fangrui Song6907ce22018-07-30 19:24:48 +00002697 if (CheckSpecializationInstantiationRedecl(PointOfInstantiation, TSK,
2698 Record,
Douglas Gregor1d957a32009-10-27 18:42:08 +00002699 MSInfo->getTemplateSpecializationKind(),
Nico Weberd75488d2010-09-27 21:02:09 +00002700 MSInfo->getPointOfInstantiation(),
Douglas Gregor1d957a32009-10-27 18:42:08 +00002701 SuppressNew) ||
2702 SuppressNew)
Douglas Gregorbbe8f462009-10-08 15:14:33 +00002703 continue;
Fangrui Song6907ce22018-07-30 19:24:48 +00002704
Douglas Gregor1d957a32009-10-27 18:42:08 +00002705 CXXRecordDecl *Pattern = Record->getInstantiatedFromMemberClass();
2706 assert(Pattern && "Missing instantiated-from-template information");
Fangrui Song6907ce22018-07-30 19:24:48 +00002707
Douglas Gregor0a5a2212010-02-11 01:04:33 +00002708 if (!Record->getDefinition()) {
2709 if (!Pattern->getDefinition()) {
Douglas Gregor1d957a32009-10-27 18:42:08 +00002710 // C++0x [temp.explicit]p8:
2711 // An explicit instantiation definition that names a class template
Fangrui Song6907ce22018-07-30 19:24:48 +00002712 // specialization explicitly instantiates the class template
2713 // specialization and is only an explicit instantiation definition
2714 // of members whose definition is visible at the point of
Douglas Gregor1d957a32009-10-27 18:42:08 +00002715 // instantiation.
2716 if (TSK == TSK_ExplicitInstantiationDeclaration) {
2717 MSInfo->setTemplateSpecializationKind(TSK);
2718 MSInfo->setPointOfInstantiation(PointOfInstantiation);
2719 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002720
Douglas Gregor1d957a32009-10-27 18:42:08 +00002721 continue;
2722 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002723
Douglas Gregor1d957a32009-10-27 18:42:08 +00002724 InstantiateClass(PointOfInstantiation, Record, Pattern,
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00002725 TemplateArgs,
2726 TSK);
Nico Weberd75488d2010-09-27 21:02:09 +00002727 } else {
2728 if (TSK == TSK_ExplicitInstantiationDefinition &&
2729 Record->getTemplateSpecializationKind() ==
2730 TSK_ExplicitInstantiationDeclaration) {
2731 Record->setTemplateSpecializationKind(TSK);
2732 MarkVTableUsed(PointOfInstantiation, Record, true);
2733 }
Douglas Gregor1d957a32009-10-27 18:42:08 +00002734 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002735
Douglas Gregor0a5a2212010-02-11 01:04:33 +00002736 Pattern = cast_or_null<CXXRecordDecl>(Record->getDefinition());
Douglas Gregor1d957a32009-10-27 18:42:08 +00002737 if (Pattern)
Fangrui Song6907ce22018-07-30 19:24:48 +00002738 InstantiateClassMembers(PointOfInstantiation, Pattern, TemplateArgs,
Douglas Gregor1d957a32009-10-27 18:42:08 +00002739 TSK);
Aaron Ballman629afae2014-03-07 19:56:05 +00002740 } else if (auto *Enum = dyn_cast<EnumDecl>(D)) {
Richard Smith4b38ded2012-03-14 23:13:10 +00002741 MemberSpecializationInfo *MSInfo = Enum->getMemberSpecializationInfo();
2742 assert(MSInfo && "No member specialization information?");
2743
2744 if (MSInfo->getTemplateSpecializationKind()
2745 == TSK_ExplicitSpecialization)
2746 continue;
2747
2748 if (CheckSpecializationInstantiationRedecl(
2749 PointOfInstantiation, TSK, Enum,
2750 MSInfo->getTemplateSpecializationKind(),
2751 MSInfo->getPointOfInstantiation(), SuppressNew) ||
2752 SuppressNew)
2753 continue;
2754
2755 if (Enum->getDefinition())
2756 continue;
2757
Richard Smith6739a102016-05-05 00:56:12 +00002758 EnumDecl *Pattern = Enum->getTemplateInstantiationPattern();
Richard Smith4b38ded2012-03-14 23:13:10 +00002759 assert(Pattern && "Missing instantiated-from-template information");
2760
2761 if (TSK == TSK_ExplicitInstantiationDefinition) {
2762 if (!Pattern->getDefinition())
2763 continue;
2764
2765 InstantiateEnum(PointOfInstantiation, Enum, Pattern, TemplateArgs, TSK);
2766 } else {
2767 MSInfo->setTemplateSpecializationKind(TSK);
2768 MSInfo->setPointOfInstantiation(PointOfInstantiation);
2769 }
Reid Klecknerd60b82f2014-11-17 23:36:45 +00002770 } else if (auto *Field = dyn_cast<FieldDecl>(D)) {
2771 // No need to instantiate in-class initializers during explicit
2772 // instantiation.
2773 if (Field->hasInClassInitializer() && TSK == TSK_ImplicitInstantiation) {
2774 CXXRecordDecl *ClassPattern =
2775 Instantiation->getTemplateInstantiationPattern();
2776 DeclContext::lookup_result Lookup =
2777 ClassPattern->lookup(Field->getDeclName());
David Majnemer76a25622016-06-09 05:26:56 +00002778 FieldDecl *Pattern = cast<FieldDecl>(Lookup.front());
Reid Klecknerd60b82f2014-11-17 23:36:45 +00002779 InstantiateInClassInitializer(PointOfInstantiation, Field, Pattern,
2780 TemplateArgs);
2781 }
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00002782 }
2783 }
2784}
2785
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002786/// Instantiate the definitions of all of the members of the
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00002787/// given class template specialization, which was named as part of an
2788/// explicit instantiation.
Mike Stump11289f42009-09-09 15:08:12 +00002789void
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00002790Sema::InstantiateClassTemplateSpecializationMembers(
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00002791 SourceLocation PointOfInstantiation,
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00002792 ClassTemplateSpecializationDecl *ClassTemplateSpec,
2793 TemplateSpecializationKind TSK) {
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00002794 // C++0x [temp.explicit]p7:
2795 // An explicit instantiation that names a class template
2796 // specialization is an explicit instantion of the same kind
2797 // (declaration or definition) of each of its members (not
2798 // including members inherited from base classes) that has not
2799 // been previously explicitly specialized in the translation unit
2800 // containing the explicit instantiation, except as described
2801 // below.
2802 InstantiateClassMembers(PointOfInstantiation, ClassTemplateSpec,
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00002803 getTemplateInstantiationArgs(ClassTemplateSpec),
2804 TSK);
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00002805}
2806
John McCalldadc5752010-08-24 06:29:42 +00002807StmtResult
Douglas Gregor01afeef2009-08-28 20:31:08 +00002808Sema::SubstStmt(Stmt *S, const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregorebe10102009-08-20 07:17:43 +00002809 if (!S)
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00002810 return S;
Douglas Gregorebe10102009-08-20 07:17:43 +00002811
2812 TemplateInstantiator Instantiator(*this, TemplateArgs,
2813 SourceLocation(),
2814 DeclarationName());
2815 return Instantiator.TransformStmt(S);
2816}
2817
John McCalldadc5752010-08-24 06:29:42 +00002818ExprResult
Douglas Gregor01afeef2009-08-28 20:31:08 +00002819Sema::SubstExpr(Expr *E, const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregora16548e2009-08-11 05:31:07 +00002820 if (!E)
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00002821 return E;
Mike Stump11289f42009-09-09 15:08:12 +00002822
Douglas Gregora16548e2009-08-11 05:31:07 +00002823 TemplateInstantiator Instantiator(*this, TemplateArgs,
2824 SourceLocation(),
2825 DeclarationName());
2826 return Instantiator.TransformExpr(E);
2827}
2828
Richard Smithd59b8322012-12-19 01:39:02 +00002829ExprResult Sema::SubstInitializer(Expr *Init,
2830 const MultiLevelTemplateArgumentList &TemplateArgs,
2831 bool CXXDirectInit) {
2832 TemplateInstantiator Instantiator(*this, TemplateArgs,
2833 SourceLocation(),
2834 DeclarationName());
2835 return Instantiator.TransformInitializer(Init, CXXDirectInit);
2836}
2837
Craig Topper99d23532015-12-24 23:58:29 +00002838bool Sema::SubstExprs(ArrayRef<Expr *> Exprs, bool IsCall,
Douglas Gregor2cd32a02011-01-07 19:35:17 +00002839 const MultiLevelTemplateArgumentList &TemplateArgs,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002840 SmallVectorImpl<Expr *> &Outputs) {
Craig Topper99d23532015-12-24 23:58:29 +00002841 if (Exprs.empty())
Douglas Gregor2cd32a02011-01-07 19:35:17 +00002842 return false;
Richard Smithd59b8322012-12-19 01:39:02 +00002843
Douglas Gregor2cd32a02011-01-07 19:35:17 +00002844 TemplateInstantiator Instantiator(*this, TemplateArgs,
2845 SourceLocation(),
2846 DeclarationName());
Craig Topper99d23532015-12-24 23:58:29 +00002847 return Instantiator.TransformExprs(Exprs.data(), Exprs.size(),
2848 IsCall, Outputs);
Douglas Gregor2cd32a02011-01-07 19:35:17 +00002849}
2850
Douglas Gregor14454802011-02-25 02:25:35 +00002851NestedNameSpecifierLoc
2852Sema::SubstNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS,
Fangrui Song6907ce22018-07-30 19:24:48 +00002853 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor14454802011-02-25 02:25:35 +00002854 if (!NNS)
2855 return NestedNameSpecifierLoc();
Fangrui Song6907ce22018-07-30 19:24:48 +00002856
Douglas Gregor14454802011-02-25 02:25:35 +00002857 TemplateInstantiator Instantiator(*this, TemplateArgs, NNS.getBeginLoc(),
2858 DeclarationName());
2859 return Instantiator.TransformNestedNameSpecifierLoc(NNS);
2860}
2861
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002862/// Do template substitution on declaration name info.
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002863DeclarationNameInfo
2864Sema::SubstDeclarationNameInfo(const DeclarationNameInfo &NameInfo,
2865 const MultiLevelTemplateArgumentList &TemplateArgs) {
2866 TemplateInstantiator Instantiator(*this, TemplateArgs, NameInfo.getLoc(),
2867 NameInfo.getName());
2868 return Instantiator.TransformDeclarationNameInfo(NameInfo);
2869}
2870
Douglas Gregoraa594892009-03-31 18:38:02 +00002871TemplateName
Douglas Gregordf846d12011-03-02 18:46:51 +00002872Sema::SubstTemplateName(NestedNameSpecifierLoc QualifierLoc,
2873 TemplateName Name, SourceLocation Loc,
Douglas Gregor01afeef2009-08-28 20:31:08 +00002874 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor71dc5092009-08-06 06:41:21 +00002875 TemplateInstantiator Instantiator(*this, TemplateArgs, Loc,
2876 DeclarationName());
Douglas Gregordf846d12011-03-02 18:46:51 +00002877 CXXScopeSpec SS;
2878 SS.Adopt(QualifierLoc);
2879 return Instantiator.TransformTemplateName(SS, Name, Loc);
Douglas Gregoraa594892009-03-31 18:38:02 +00002880}
Douglas Gregorc43620d2009-06-11 00:06:24 +00002881
Douglas Gregor0f3feb42010-12-22 21:19:48 +00002882bool Sema::Subst(const TemplateArgumentLoc *Args, unsigned NumArgs,
2883 TemplateArgumentListInfo &Result,
John McCall0ad16662009-10-29 08:12:44 +00002884 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregore922c772009-08-04 22:27:00 +00002885 TemplateInstantiator Instantiator(*this, TemplateArgs, SourceLocation(),
2886 DeclarationName());
Fangrui Song6907ce22018-07-30 19:24:48 +00002887
Douglas Gregor0f3feb42010-12-22 21:19:48 +00002888 return Instantiator.TransformTemplateArguments(Args, NumArgs, Result);
Douglas Gregorc43620d2009-06-11 00:06:24 +00002889}
Douglas Gregor14cf7522010-04-30 18:55:50 +00002890
Richard Smith70b13042015-01-09 01:19:56 +00002891static const Decl *getCanonicalParmVarDecl(const Decl *D) {
DeLesley Hutchinsf39c0c22012-09-26 17:57:31 +00002892 // When storing ParmVarDecls in the local instantiation scope, we always
2893 // want to use the ParmVarDecl from the canonical function declaration,
2894 // since the map is then valid for any redeclaration or definition of that
2895 // function.
2896 if (const ParmVarDecl *PV = dyn_cast<ParmVarDecl>(D)) {
2897 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(PV->getDeclContext())) {
2898 unsigned i = PV->getFunctionScopeIndex();
Richard Smith70b13042015-01-09 01:19:56 +00002899 // This parameter might be from a freestanding function type within the
2900 // function and isn't necessarily referring to one of FD's parameters.
Dmitri Gribenko66b6bb12019-04-10 20:25:07 +00002901 if (i < FD->getNumParams() && FD->getParamDecl(i) == PV)
Richard Smith70b13042015-01-09 01:19:56 +00002902 return FD->getCanonicalDecl()->getParamDecl(i);
DeLesley Hutchinsf39c0c22012-09-26 17:57:31 +00002903 }
2904 }
2905 return D;
2906}
2907
2908
Douglas Gregorf3010112011-01-07 16:43:16 +00002909llvm::PointerUnion<Decl *, LocalInstantiationScope::DeclArgumentPack *> *
2910LocalInstantiationScope::findInstantiationOf(const Decl *D) {
DeLesley Hutchinsf39c0c22012-09-26 17:57:31 +00002911 D = getCanonicalParmVarDecl(D);
Chris Lattnercab02a62011-02-17 20:34:02 +00002912 for (LocalInstantiationScope *Current = this; Current;
Douglas Gregor14cf7522010-04-30 18:55:50 +00002913 Current = Current->Outer) {
Chris Lattnercab02a62011-02-17 20:34:02 +00002914
Douglas Gregor14cf7522010-04-30 18:55:50 +00002915 // Check if we found something within this scope.
Douglas Gregore9fc8dc2010-12-21 21:22:51 +00002916 const Decl *CheckD = D;
2917 do {
Douglas Gregorf3010112011-01-07 16:43:16 +00002918 LocalDeclsMap::iterator Found = Current->LocalDecls.find(CheckD);
Douglas Gregore9fc8dc2010-12-21 21:22:51 +00002919 if (Found != Current->LocalDecls.end())
Douglas Gregorf3010112011-01-07 16:43:16 +00002920 return &Found->second;
Fangrui Song6907ce22018-07-30 19:24:48 +00002921
Douglas Gregore9fc8dc2010-12-21 21:22:51 +00002922 // If this is a tag declaration, it's possible that we need to look for
2923 // a previous declaration.
2924 if (const TagDecl *Tag = dyn_cast<TagDecl>(CheckD))
Douglas Gregorec9fd132012-01-14 16:38:05 +00002925 CheckD = Tag->getPreviousDecl();
Douglas Gregore9fc8dc2010-12-21 21:22:51 +00002926 else
Craig Topperc3ec1492014-05-26 06:22:03 +00002927 CheckD = nullptr;
Douglas Gregore9fc8dc2010-12-21 21:22:51 +00002928 } while (CheckD);
Fangrui Song6907ce22018-07-30 19:24:48 +00002929
2930 // If we aren't combined with our outer scope, we're done.
Douglas Gregor14cf7522010-04-30 18:55:50 +00002931 if (!Current->CombineWithOuterScope)
2932 break;
2933 }
Chris Lattnercab02a62011-02-17 20:34:02 +00002934
Serge Pavlov7cd8f602013-07-15 06:14:07 +00002935 // If we're performing a partial substitution during template argument
2936 // deduction, we may not have values for template parameters yet.
2937 if (isa<NonTypeTemplateParmDecl>(D) || isa<TemplateTypeParmDecl>(D) ||
2938 isa<TemplateTemplateParmDecl>(D))
Craig Topperc3ec1492014-05-26 06:22:03 +00002939 return nullptr;
Serge Pavlov7cd8f602013-07-15 06:14:07 +00002940
Serge Pavlove7ad8312015-05-15 10:10:28 +00002941 // Local types referenced prior to definition may require instantiation.
2942 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D))
2943 if (RD->isLocalClass())
2944 return nullptr;
2945
2946 // Enumeration types referenced prior to definition may appear as a result of
2947 // error recovery.
2948 if (isa<EnumDecl>(D))
Serge Pavlov4c511742015-05-04 16:44:39 +00002949 return nullptr;
2950
Chris Lattnercab02a62011-02-17 20:34:02 +00002951 // If we didn't find the decl, then we either have a sema bug, or we have a
2952 // forward reference to a label declaration. Return null to indicate that
2953 // we have an uninstantiated label.
2954 assert(isa<LabelDecl>(D) && "declaration not instantiated in this scope");
Craig Topperc3ec1492014-05-26 06:22:03 +00002955 return nullptr;
Douglas Gregor14cf7522010-04-30 18:55:50 +00002956}
2957
John McCall19c1bfd2010-08-25 05:32:35 +00002958void LocalInstantiationScope::InstantiatedLocal(const Decl *D, Decl *Inst) {
DeLesley Hutchinsf39c0c22012-09-26 17:57:31 +00002959 D = getCanonicalParmVarDecl(D);
Douglas Gregorf3010112011-01-07 16:43:16 +00002960 llvm::PointerUnion<Decl *, DeclArgumentPack *> &Stored = LocalDecls[D];
Richard Smith70b13042015-01-09 01:19:56 +00002961 if (Stored.isNull()) {
2962#ifndef NDEBUG
2963 // It should not be present in any surrounding scope either.
2964 LocalInstantiationScope *Current = this;
2965 while (Current->CombineWithOuterScope && Current->Outer) {
2966 Current = Current->Outer;
2967 assert(Current->LocalDecls.find(D) == Current->LocalDecls.end() &&
2968 "Instantiated local in inner and outer scopes");
2969 }
2970#endif
Douglas Gregora8bac7f2011-01-10 07:32:04 +00002971 Stored = Inst;
Richard Smith70b13042015-01-09 01:19:56 +00002972 } else if (DeclArgumentPack *Pack = Stored.dyn_cast<DeclArgumentPack *>()) {
James Y Knight48fefa32015-09-30 14:04:23 +00002973 Pack->push_back(cast<ParmVarDecl>(Inst));
Richard Smith70b13042015-01-09 01:19:56 +00002974 } else {
Douglas Gregora8bac7f2011-01-10 07:32:04 +00002975 assert(Stored.get<Decl *>() == Inst && "Already instantiated this local");
Richard Smith70b13042015-01-09 01:19:56 +00002976 }
Douglas Gregor14cf7522010-04-30 18:55:50 +00002977}
Douglas Gregorf3010112011-01-07 16:43:16 +00002978
James Y Knight48fefa32015-09-30 14:04:23 +00002979void LocalInstantiationScope::InstantiatedLocalPackArg(const Decl *D,
2980 ParmVarDecl *Inst) {
DeLesley Hutchinsf39c0c22012-09-26 17:57:31 +00002981 D = getCanonicalParmVarDecl(D);
Douglas Gregorf3010112011-01-07 16:43:16 +00002982 DeclArgumentPack *Pack = LocalDecls[D].get<DeclArgumentPack *>();
2983 Pack->push_back(Inst);
2984}
2985
2986void LocalInstantiationScope::MakeInstantiatedLocalArgPack(const Decl *D) {
Richard Smith70b13042015-01-09 01:19:56 +00002987#ifndef NDEBUG
2988 // This should be the first time we've been told about this decl.
2989 for (LocalInstantiationScope *Current = this;
2990 Current && Current->CombineWithOuterScope; Current = Current->Outer)
2991 assert(Current->LocalDecls.find(D) == Current->LocalDecls.end() &&
2992 "Creating local pack after instantiation of local");
2993#endif
2994
DeLesley Hutchinsf39c0c22012-09-26 17:57:31 +00002995 D = getCanonicalParmVarDecl(D);
Douglas Gregorf3010112011-01-07 16:43:16 +00002996 llvm::PointerUnion<Decl *, DeclArgumentPack *> &Stored = LocalDecls[D];
Douglas Gregorf3010112011-01-07 16:43:16 +00002997 DeclArgumentPack *Pack = new DeclArgumentPack;
2998 Stored = Pack;
2999 ArgumentPacks.push_back(Pack);
3000}
3001
Fangrui Song6907ce22018-07-30 19:24:48 +00003002void LocalInstantiationScope::SetPartiallySubstitutedPack(NamedDecl *Pack,
Douglas Gregora8bac7f2011-01-10 07:32:04 +00003003 const TemplateArgument *ExplicitArgs,
3004 unsigned NumExplicitArgs) {
3005 assert((!PartiallySubstitutedPack || PartiallySubstitutedPack == Pack) &&
3006 "Already have a partially-substituted pack");
Fangrui Song6907ce22018-07-30 19:24:48 +00003007 assert((!PartiallySubstitutedPack
Douglas Gregora8bac7f2011-01-10 07:32:04 +00003008 || NumArgsInPartiallySubstitutedPack == NumExplicitArgs) &&
3009 "Wrong number of arguments in partially-substituted pack");
3010 PartiallySubstitutedPack = Pack;
3011 ArgsInPartiallySubstitutedPack = ExplicitArgs;
3012 NumArgsInPartiallySubstitutedPack = NumExplicitArgs;
3013}
3014
3015NamedDecl *LocalInstantiationScope::getPartiallySubstitutedPack(
3016 const TemplateArgument **ExplicitArgs,
3017 unsigned *NumExplicitArgs) const {
3018 if (ExplicitArgs)
Craig Topperc3ec1492014-05-26 06:22:03 +00003019 *ExplicitArgs = nullptr;
Douglas Gregora8bac7f2011-01-10 07:32:04 +00003020 if (NumExplicitArgs)
3021 *NumExplicitArgs = 0;
Fangrui Song6907ce22018-07-30 19:24:48 +00003022
3023 for (const LocalInstantiationScope *Current = this; Current;
Douglas Gregora8bac7f2011-01-10 07:32:04 +00003024 Current = Current->Outer) {
3025 if (Current->PartiallySubstitutedPack) {
3026 if (ExplicitArgs)
3027 *ExplicitArgs = Current->ArgsInPartiallySubstitutedPack;
3028 if (NumExplicitArgs)
3029 *NumExplicitArgs = Current->NumArgsInPartiallySubstitutedPack;
Fangrui Song6907ce22018-07-30 19:24:48 +00003030
Douglas Gregora8bac7f2011-01-10 07:32:04 +00003031 return Current->PartiallySubstitutedPack;
3032 }
3033
3034 if (!Current->CombineWithOuterScope)
3035 break;
3036 }
Craig Topperc3ec1492014-05-26 06:22:03 +00003037
3038 return nullptr;
Douglas Gregora8bac7f2011-01-10 07:32:04 +00003039}