blob: 55d1d94bd825ce7d81469c0d06cdf18152dd8793 [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", [&]() {
Lubos Lunakab8cde42019-05-12 10:39:21 +00002017 std::string Name;
2018 llvm::raw_string_ostream OS(Name);
2019 Instantiation->getNameForDiagnostic(OS, getPrintingPolicy(),
2020 /*Qualified=*/true);
2021 return Name;
Anton Afanasyevd880de22019-03-30 08:42:48 +00002022 });
2023
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00002024 Pattern = PatternDef;
2025
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002026 // Record the point of instantiation.
Fangrui Song6907ce22018-07-30 19:24:48 +00002027 if (MemberSpecializationInfo *MSInfo
Douglas Gregord6ba93d2009-10-15 15:54:05 +00002028 = Instantiation->getMemberSpecializationInfo()) {
2029 MSInfo->setTemplateSpecializationKind(TSK);
2030 MSInfo->setPointOfInstantiation(PointOfInstantiation);
Fangrui Song6907ce22018-07-30 19:24:48 +00002031 } else if (ClassTemplateSpecializationDecl *Spec
Nico Weber3ffc4c92011-12-20 20:32:49 +00002032 = dyn_cast<ClassTemplateSpecializationDecl>(Instantiation)) {
Douglas Gregoref6ab412009-10-27 06:26:26 +00002033 Spec->setTemplateSpecializationKind(TSK);
2034 Spec->setPointOfInstantiation(PointOfInstantiation);
Douglas Gregord6ba93d2009-10-15 15:54:05 +00002035 }
Richard Smitha1087602014-03-10 00:04:29 +00002036
Douglas Gregorf3430ae2009-03-25 21:23:52 +00002037 InstantiatingTemplate Inst(*this, PointOfInstantiation, Instantiation);
Alp Tokerd4a72d52013-10-08 08:09:04 +00002038 if (Inst.isInvalid())
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00002039 return true;
Richard Smith54f18e82016-08-31 02:15:21 +00002040 assert(!Inst.isAlreadyInstantiating() && "should have been caught by caller");
Jordan Rose1e879d82018-03-23 00:07:18 +00002041 PrettyDeclStackTraceEntry CrashInfo(Context, Instantiation, SourceLocation(),
Richard Smithe19b95d2016-05-26 20:23:13 +00002042 "instantiating class definition");
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00002043
2044 // Enter the scope of this instantiation. We don't use
2045 // PushDeclContext because we don't have a scope.
John McCall80e58cd2010-04-29 00:35:03 +00002046 ContextRAII SavedContext(*this, Instantiation);
Faisal Valid143a0c2017-04-01 21:30:49 +00002047 EnterExpressionEvaluationContext EvalContext(
2048 *this, Sema::ExpressionEvaluationContext::PotentiallyEvaluated);
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00002049
Douglas Gregor51121572010-03-24 01:33:17 +00002050 // If this is an instantiation of a local class, merge this local
2051 // instantiation scope with the enclosing scope. Otherwise, every
2052 // instantiation of a class has its own local instantiation scope.
2053 bool MergeWithParentScope = !Instantiation->isDefinedOutsideFunctionOrMethod();
John McCall19c1bfd2010-08-25 05:32:35 +00002054 LocalInstantiationScope Scope(*this, MergeWithParentScope);
Douglas Gregor51121572010-03-24 01:33:17 +00002055
Volodymyr Sapsai4fbaa622017-09-21 19:54:12 +00002056 // Some class state isn't processed immediately but delayed till class
2057 // instantiation completes. We may not be ready to handle any delayed state
2058 // already on the stack as it might correspond to a different class, so save
2059 // it now and put it back later.
2060 SavePendingParsedClassStateRAII SavedPendingParsedClassState(*this);
Reid Kleckner5b640342016-02-26 19:51:02 +00002061
John McCall6602bb12010-08-01 02:01:53 +00002062 // Pull attributes from the pattern onto the instantiation.
2063 InstantiateAttrs(TemplateArgs, Pattern, Instantiation);
2064
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00002065 // Start the definition of this instantiation.
2066 Instantiation->startDefinition();
Richard Smitha1087602014-03-10 00:04:29 +00002067
2068 // The instantiation is visible here, even if it was first declared in an
2069 // unimported module.
Richard Smith90dc5252017-06-23 01:04:34 +00002070 Instantiation->setVisibleDespiteOwningModule();
Richard Smitha1087602014-03-10 00:04:29 +00002071
2072 // FIXME: This loses the as-written tag kind for an explicit instantiation.
Douglas Gregore9029562010-05-06 00:28:52 +00002073 Instantiation->setTagKind(Pattern->getTagKind());
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00002074
John McCall76d824f2009-08-25 22:02:44 +00002075 // Do substitution on the base class specifiers.
2076 if (SubstBaseSpecifiers(Instantiation, Pattern, TemplateArgs))
Douglas Gregorb9b8b812012-07-02 21:00:41 +00002077 Instantiation->setInvalidDecl();
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00002078
Douglas Gregor869853e2010-11-10 19:44:59 +00002079 TemplateDeclInstantiator Instantiator(*this, Instantiation, TemplateArgs);
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002080 SmallVector<Decl*, 4> Fields;
DeLesley Hutchins30398dd2012-01-20 22:50:54 +00002081 // Delay instantiation of late parsed attributes.
2082 LateInstantiatedAttrVec LateAttrs;
2083 Instantiator.enableLateAttributeInstantiation(&LateAttrs);
2084
Richard Smith921f1322019-05-13 23:35:21 +00002085 bool MightHaveConstexprVirtualFunctions = false;
Aaron Ballman629afae2014-03-07 19:56:05 +00002086 for (auto *Member : Pattern->decls()) {
Argyrios Kyrtzidis9a94d9b2010-11-04 03:18:57 +00002087 // Don't instantiate members not belonging in this semantic context.
2088 // e.g. for:
2089 // @code
2090 // template <int i> class A {
2091 // class B *g;
2092 // };
2093 // @endcode
2094 // 'class B' has the template as lexical context but semantically it is
2095 // introduced in namespace scope.
Aaron Ballman629afae2014-03-07 19:56:05 +00002096 if (Member->getDeclContext() != Pattern)
Argyrios Kyrtzidis9a94d9b2010-11-04 03:18:57 +00002097 continue;
2098
Erik Pilkingtona3a462e2018-07-23 22:47:37 +00002099 // BlockDecls can appear in a default-member-initializer. They must be the
2100 // child of a BlockExpr, so we only know how to instantiate them from there.
2101 if (isa<BlockDecl>(Member))
2102 continue;
2103
Aaron Ballman629afae2014-03-07 19:56:05 +00002104 if (Member->isInvalidDecl()) {
Richard Smithded9c2e2012-07-11 22:37:56 +00002105 Instantiation->setInvalidDecl();
Douglas Gregor869853e2010-11-10 19:44:59 +00002106 continue;
2107 }
2108
Aaron Ballman629afae2014-03-07 19:56:05 +00002109 Decl *NewMember = Instantiator.Visit(Member);
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00002110 if (NewMember) {
Richard Smith938f40b2011-06-11 17:19:42 +00002111 if (FieldDecl *Field = dyn_cast<FieldDecl>(NewMember)) {
John McCall48871652010-08-21 09:40:31 +00002112 Fields.push_back(Field);
Richard Smith7d137e32012-03-23 03:33:32 +00002113 } else if (EnumDecl *Enum = dyn_cast<EnumDecl>(NewMember)) {
2114 // C++11 [temp.inst]p1: The implicit instantiation of a class template
2115 // specialization causes the implicit instantiation of the definitions
2116 // of unscoped member enumerations.
2117 // Record a point of instantiation for this implicit instantiation.
Richard Smithb66d7772012-03-23 23:09:08 +00002118 if (TSK == TSK_ImplicitInstantiation && !Enum->isScoped() &&
2119 Enum->isCompleteDefinition()) {
Richard Smith7d137e32012-03-23 03:33:32 +00002120 MemberSpecializationInfo *MSInfo =Enum->getMemberSpecializationInfo();
2121 assert(MSInfo && "no spec info for member enum specialization");
2122 MSInfo->setTemplateSpecializationKind(TSK_ImplicitInstantiation);
2123 MSInfo->setPointOfInstantiation(PointOfInstantiation);
2124 }
Richard Smithded9c2e2012-07-11 22:37:56 +00002125 } else if (StaticAssertDecl *SA = dyn_cast<StaticAssertDecl>(NewMember)) {
2126 if (SA->isFailed()) {
2127 // A static_assert failed. Bail out; instantiating this
2128 // class is probably not meaningful.
2129 Instantiation->setInvalidDecl();
2130 break;
2131 }
Richard Smith921f1322019-05-13 23:35:21 +00002132 } else if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(NewMember)) {
2133 if (MD->isConstexpr() && !MD->getFriendObjectKind() &&
2134 (MD->isVirtualAsWritten() || Instantiation->getNumBases()))
2135 MightHaveConstexprVirtualFunctions = true;
Richard Smith7d137e32012-03-23 03:33:32 +00002136 }
2137
2138 if (NewMember->isInvalidDecl())
Douglas Gregorb9b8b812012-07-02 21:00:41 +00002139 Instantiation->setInvalidDecl();
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00002140 } else {
2141 // FIXME: Eventually, a NULL return will mean that one of the
Douglas Gregorb9b8b812012-07-02 21:00:41 +00002142 // instantiations was a semantic disaster, and we'll want to mark the
2143 // declaration invalid.
2144 // For now, we expect to skip some members that we can't yet handle.
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00002145 }
2146 }
2147
2148 // Finish checking fields.
Craig Topperc3ec1492014-05-26 06:22:03 +00002149 ActOnFields(nullptr, Instantiation->getLocation(), Instantiation, Fields,
Erich Keanec480f302018-07-12 21:09:05 +00002150 SourceLocation(), SourceLocation(), ParsedAttributesView());
Douglas Gregor0be31a22010-07-02 17:43:08 +00002151 CheckCompletedCXXClass(Instantiation);
Richard Smith938f40b2011-06-11 17:19:42 +00002152
Reid Kleckner93f661a2015-03-17 21:51:43 +00002153 // Default arguments are parsed, if not instantiated. We can go instantiate
2154 // default arg exprs for default constructors if necessary now.
Hans Wennborg99000c22015-08-15 01:18:16 +00002155 ActOnFinishCXXNonNestedClass(Instantiation);
Reid Kleckner93f661a2015-03-17 21:51:43 +00002156
DeLesley Hutchins30398dd2012-01-20 22:50:54 +00002157 // Instantiate late parsed attributes, and attach them to their decls.
2158 // See Sema::InstantiateAttrs
2159 for (LateInstantiatedAttrVec::iterator I = LateAttrs.begin(),
2160 E = LateAttrs.end(); I != E; ++I) {
2161 assert(CurrentInstantiationScope == Instantiator.getStartingScope());
2162 CurrentInstantiationScope = I->Scope;
Richard Smithc3d2ebb2013-06-07 02:33:37 +00002163
2164 // Allow 'this' within late-parsed attributes.
2165 NamedDecl *ND = dyn_cast<NamedDecl>(I->NewDecl);
2166 CXXRecordDecl *ThisContext =
2167 dyn_cast_or_null<CXXRecordDecl>(ND->getDeclContext());
Mikael Nilsson9d2872d2018-12-13 10:15:27 +00002168 CXXThisScopeRAII ThisScope(*this, ThisContext, Qualifiers(),
Richard Smithc3d2ebb2013-06-07 02:33:37 +00002169 ND && ND->isCXXInstanceMember());
2170
DeLesley Hutchins30398dd2012-01-20 22:50:54 +00002171 Attr *NewAttr =
2172 instantiateTemplateAttribute(I->TmplAttr, Context, *this, TemplateArgs);
2173 I->NewDecl->addAttr(NewAttr);
2174 LocalInstantiationScope::deleteScopes(I->Scope,
2175 Instantiator.getStartingScope());
2176 }
2177 Instantiator.disableLateAttributeInstantiation();
2178 LateAttrs.clear();
2179
Richard Smithd3b5c9082012-07-27 04:22:15 +00002180 ActOnFinishDelayedMemberInitializers(Instantiation);
Richard Smith938f40b2011-06-11 17:19:42 +00002181
Richard Smitha1087602014-03-10 00:04:29 +00002182 // FIXME: We should do something similar for explicit instantiations so they
2183 // end up in the right module.
Abramo Bagnara12dcbf32011-11-18 08:08:52 +00002184 if (TSK == TSK_ImplicitInstantiation) {
Argyrios Kyrtzidise3789482012-02-11 01:59:57 +00002185 Instantiation->setLocation(Pattern->getLocation());
Abramo Bagnara12dcbf32011-11-18 08:08:52 +00002186 Instantiation->setLocStart(Pattern->getInnerLocStart());
Argyrios Kyrtzidisd798c052016-07-15 18:11:33 +00002187 Instantiation->setBraceRange(Pattern->getBraceRange());
Abramo Bagnara12dcbf32011-11-18 08:08:52 +00002188 }
Abramo Bagnarafd3a4552011-10-03 20:34:03 +00002189
Douglas Gregorb9b8b812012-07-02 21:00:41 +00002190 if (!Instantiation->isInvalidDecl()) {
John McCalla0a96892012-08-10 03:15:35 +00002191 // Perform any dependent diagnostics from the pattern.
2192 PerformDependentDiagnostics(Pattern, TemplateArgs);
2193
Douglas Gregor869853e2010-11-10 19:44:59 +00002194 // Instantiate any out-of-line class template partial
2195 // specializations now.
Richard Smith10b55fc2013-09-26 03:49:48 +00002196 for (TemplateDeclInstantiator::delayed_partial_spec_iterator
Douglas Gregor869853e2010-11-10 19:44:59 +00002197 P = Instantiator.delayed_partial_spec_begin(),
2198 PEnd = Instantiator.delayed_partial_spec_end();
2199 P != PEnd; ++P) {
2200 if (!Instantiator.InstantiateClassTemplatePartialSpecialization(
Richard Smith10b55fc2013-09-26 03:49:48 +00002201 P->first, P->second)) {
2202 Instantiation->setInvalidDecl();
2203 break;
2204 }
2205 }
2206
2207 // Instantiate any out-of-line variable template partial
2208 // specializations now.
2209 for (TemplateDeclInstantiator::delayed_var_partial_spec_iterator
2210 P = Instantiator.delayed_var_partial_spec_begin(),
2211 PEnd = Instantiator.delayed_var_partial_spec_end();
2212 P != PEnd; ++P) {
2213 if (!Instantiator.InstantiateVarTemplatePartialSpecialization(
2214 P->first, P->second)) {
Douglas Gregorb9b8b812012-07-02 21:00:41 +00002215 Instantiation->setInvalidDecl();
Douglas Gregor869853e2010-11-10 19:44:59 +00002216 break;
2217 }
2218 }
2219 }
2220
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00002221 // Exit the scope of this instantiation.
John McCall80e58cd2010-04-29 00:35:03 +00002222 SavedContext.pop();
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00002223
Douglas Gregorb9b8b812012-07-02 21:00:41 +00002224 if (!Instantiation->isInvalidDecl()) {
Douglas Gregor28ad4b52009-05-26 20:50:29 +00002225 Consumer.HandleTagDeclDefinition(Instantiation);
2226
Douglas Gregor88d292c2010-05-13 16:44:06 +00002227 // Always emit the vtable for an explicit instantiation definition
Richard Smith921f1322019-05-13 23:35:21 +00002228 // of a polymorphic class template specialization. Otherwise, eagerly
2229 // instantiate only constexpr virtual functions in preparation for their use
2230 // in constant evaluation.
Douglas Gregor88d292c2010-05-13 16:44:06 +00002231 if (TSK == TSK_ExplicitInstantiationDefinition)
2232 MarkVTableUsed(PointOfInstantiation, Instantiation, true);
Richard Smith921f1322019-05-13 23:35:21 +00002233 else if (MightHaveConstexprVirtualFunctions)
2234 MarkVirtualMembersReferenced(PointOfInstantiation, Instantiation,
2235 /*ConstexprOnly*/ true);
Douglas Gregor88d292c2010-05-13 16:44:06 +00002236 }
2237
Douglas Gregorb9b8b812012-07-02 21:00:41 +00002238 return Instantiation->isInvalidDecl();
Douglas Gregor8ea8fd42009-03-25 21:17:03 +00002239}
2240
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002241/// Instantiate the definition of an enum from a given pattern.
Richard Smith4b38ded2012-03-14 23:13:10 +00002242///
2243/// \param PointOfInstantiation The point of instantiation within the
2244/// source code.
2245/// \param Instantiation is the declaration whose definition is being
2246/// instantiated. This will be a member enumeration of a class
2247/// temploid specialization, or a local enumeration within a
2248/// function temploid specialization.
2249/// \param Pattern The templated declaration from which the instantiation
2250/// occurs.
2251/// \param TemplateArgs The template arguments to be substituted into
2252/// the pattern.
2253/// \param TSK The kind of implicit or explicit instantiation to perform.
2254///
2255/// \return \c true if an error occurred, \c false otherwise.
2256bool Sema::InstantiateEnum(SourceLocation PointOfInstantiation,
2257 EnumDecl *Instantiation, EnumDecl *Pattern,
2258 const MultiLevelTemplateArgumentList &TemplateArgs,
2259 TemplateSpecializationKind TSK) {
2260 EnumDecl *PatternDef = Pattern->getDefinition();
Vassil Vassilevb21ee082016-08-18 22:01:25 +00002261 if (DiagnoseUninstantiableTemplate(PointOfInstantiation, Instantiation,
Richard Smith4b38ded2012-03-14 23:13:10 +00002262 Instantiation->getInstantiatedFromMemberEnum(),
2263 Pattern, PatternDef, TSK,/*Complain*/true))
2264 return true;
2265 Pattern = PatternDef;
2266
2267 // Record the point of instantiation.
2268 if (MemberSpecializationInfo *MSInfo
2269 = Instantiation->getMemberSpecializationInfo()) {
2270 MSInfo->setTemplateSpecializationKind(TSK);
2271 MSInfo->setPointOfInstantiation(PointOfInstantiation);
2272 }
2273
2274 InstantiatingTemplate Inst(*this, PointOfInstantiation, Instantiation);
Alp Tokerd4a72d52013-10-08 08:09:04 +00002275 if (Inst.isInvalid())
Richard Smith4b38ded2012-03-14 23:13:10 +00002276 return true;
Richard Smith54f18e82016-08-31 02:15:21 +00002277 if (Inst.isAlreadyInstantiating())
2278 return false;
Jordan Rose1e879d82018-03-23 00:07:18 +00002279 PrettyDeclStackTraceEntry CrashInfo(Context, Instantiation, SourceLocation(),
Richard Smithe19b95d2016-05-26 20:23:13 +00002280 "instantiating enum definition");
Richard Smith4b38ded2012-03-14 23:13:10 +00002281
Richard Smitha1087602014-03-10 00:04:29 +00002282 // The instantiation is visible here, even if it was first declared in an
2283 // unimported module.
Richard Smith90dc5252017-06-23 01:04:34 +00002284 Instantiation->setVisibleDespiteOwningModule();
Richard Smitha1087602014-03-10 00:04:29 +00002285
Richard Smith4b38ded2012-03-14 23:13:10 +00002286 // Enter the scope of this instantiation. We don't use
2287 // PushDeclContext because we don't have a scope.
2288 ContextRAII SavedContext(*this, Instantiation);
Faisal Valid143a0c2017-04-01 21:30:49 +00002289 EnterExpressionEvaluationContext EvalContext(
2290 *this, Sema::ExpressionEvaluationContext::PotentiallyEvaluated);
Richard Smith4b38ded2012-03-14 23:13:10 +00002291
2292 LocalInstantiationScope Scope(*this, /*MergeWithParentScope*/true);
2293
2294 // Pull attributes from the pattern onto the instantiation.
2295 InstantiateAttrs(TemplateArgs, Pattern, Instantiation);
2296
2297 TemplateDeclInstantiator Instantiator(*this, Instantiation, TemplateArgs);
2298 Instantiator.InstantiateEnumDefinition(Instantiation, Pattern);
2299
2300 // Exit the scope of this instantiation.
2301 SavedContext.pop();
2302
2303 return Instantiation->isInvalidDecl();
2304}
2305
Reid Klecknerd60b82f2014-11-17 23:36:45 +00002306
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002307/// Instantiate the definition of a field from the given pattern.
Reid Klecknerd60b82f2014-11-17 23:36:45 +00002308///
2309/// \param PointOfInstantiation The point of instantiation within the
2310/// source code.
2311/// \param Instantiation is the declaration whose definition is being
2312/// instantiated. This will be a class of a class temploid
2313/// specialization, or a local enumeration within a function temploid
2314/// specialization.
2315/// \param Pattern The templated declaration from which the instantiation
2316/// occurs.
2317/// \param TemplateArgs The template arguments to be substituted into
2318/// the pattern.
2319///
2320/// \return \c true if an error occurred, \c false otherwise.
2321bool Sema::InstantiateInClassInitializer(
2322 SourceLocation PointOfInstantiation, FieldDecl *Instantiation,
2323 FieldDecl *Pattern, const MultiLevelTemplateArgumentList &TemplateArgs) {
2324 // If there is no initializer, we don't need to do anything.
2325 if (!Pattern->hasInClassInitializer())
2326 return false;
2327
2328 assert(Instantiation->getInClassInitStyle() ==
2329 Pattern->getInClassInitStyle() &&
2330 "pattern and instantiation disagree about init style");
2331
2332 // Error out if we haven't parsed the initializer of the pattern yet because
2333 // we are waiting for the closing brace of the outer class.
2334 Expr *OldInit = Pattern->getInClassInitializer();
2335 if (!OldInit) {
2336 RecordDecl *PatternRD = Pattern->getParent();
2337 RecordDecl *OutermostClass = PatternRD->getOuterLexicalRecordContext();
Richard Smith8dbc6b22016-11-22 22:55:12 +00002338 Diag(PointOfInstantiation,
2339 diag::err_in_class_initializer_not_yet_parsed)
2340 << OutermostClass << Pattern;
Stephen Kelly1c301dc2018-08-09 21:09:38 +00002341 Diag(Pattern->getEndLoc(), diag::note_in_class_initializer_not_yet_parsed);
Reid Klecknerd60b82f2014-11-17 23:36:45 +00002342 Instantiation->setInvalidDecl();
2343 return true;
2344 }
2345
2346 InstantiatingTemplate Inst(*this, PointOfInstantiation, Instantiation);
2347 if (Inst.isInvalid())
2348 return true;
Richard Smith54f18e82016-08-31 02:15:21 +00002349 if (Inst.isAlreadyInstantiating()) {
2350 // Error out if we hit an instantiation cycle for this initializer.
2351 Diag(PointOfInstantiation, diag::err_in_class_initializer_cycle)
2352 << Instantiation;
2353 return true;
2354 }
Jordan Rose1e879d82018-03-23 00:07:18 +00002355 PrettyDeclStackTraceEntry CrashInfo(Context, Instantiation, SourceLocation(),
Richard Smithe19b95d2016-05-26 20:23:13 +00002356 "instantiating default member init");
Reid Klecknerd60b82f2014-11-17 23:36:45 +00002357
2358 // Enter the scope of this instantiation. We don't use PushDeclContext because
2359 // we don't have a scope.
2360 ContextRAII SavedContext(*this, Instantiation->getParent());
Faisal Valid143a0c2017-04-01 21:30:49 +00002361 EnterExpressionEvaluationContext EvalContext(
2362 *this, Sema::ExpressionEvaluationContext::PotentiallyEvaluated);
Reid Klecknerd60b82f2014-11-17 23:36:45 +00002363
Serge Pavlov907233f2015-04-28 17:58:47 +00002364 LocalInstantiationScope Scope(*this, true);
Reid Klecknerd60b82f2014-11-17 23:36:45 +00002365
2366 // Instantiate the initializer.
2367 ActOnStartCXXInClassMemberInitializer();
Mikael Nilsson9d2872d2018-12-13 10:15:27 +00002368 CXXThisScopeRAII ThisScope(*this, Instantiation->getParent(), Qualifiers());
Reid Klecknerd60b82f2014-11-17 23:36:45 +00002369
2370 ExprResult NewInit = SubstInitializer(OldInit, TemplateArgs,
2371 /*CXXDirectInit=*/false);
2372 Expr *Init = NewInit.get();
2373 assert((!Init || !isa<ParenListExpr>(Init)) && "call-style init in class");
2374 ActOnFinishCXXInClassMemberInitializer(
Stephen Kellyf2ceec42018-08-09 21:08:08 +00002375 Instantiation, Init ? Init->getBeginLoc() : SourceLocation(), Init);
Reid Klecknerd60b82f2014-11-17 23:36:45 +00002376
Richard Smith4b054b22016-08-24 21:25:37 +00002377 if (auto *L = getASTMutationListener())
2378 L->DefaultMemberInitializerInstantiated(Instantiation);
2379
Reid Klecknerd60b82f2014-11-17 23:36:45 +00002380 // Return true if the in-class initializer is still missing.
2381 return !Instantiation->getInClassInitializer();
2382}
2383
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +00002384namespace {
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002385 /// A partial specialization whose template arguments have matched
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +00002386 /// a given template-id.
2387 struct PartialSpecMatchResult {
2388 ClassTemplatePartialSpecializationDecl *Partial;
2389 TemplateArgumentList *Args;
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +00002390 };
2391}
2392
Richard Smithe6d4b772017-06-07 02:42:27 +00002393bool Sema::usesPartialOrExplicitSpecialization(
2394 SourceLocation Loc, ClassTemplateSpecializationDecl *ClassTemplateSpec) {
2395 if (ClassTemplateSpec->getTemplateSpecializationKind() ==
2396 TSK_ExplicitSpecialization)
2397 return true;
2398
2399 SmallVector<ClassTemplatePartialSpecializationDecl *, 4> PartialSpecs;
2400 ClassTemplateSpec->getSpecializedTemplate()
2401 ->getPartialSpecializations(PartialSpecs);
2402 for (unsigned I = 0, N = PartialSpecs.size(); I != N; ++I) {
2403 TemplateDeductionInfo Info(Loc);
2404 if (!DeduceTemplateArguments(PartialSpecs[I],
2405 ClassTemplateSpec->getTemplateArgs(), Info))
2406 return true;
2407 }
2408
2409 return false;
2410}
2411
Richard Smith32b43762017-01-08 22:45:21 +00002412/// Get the instantiation pattern to use to instantiate the definition of a
2413/// given ClassTemplateSpecializationDecl (either the pattern of the primary
2414/// template or of a partial specialization).
2415static CXXRecordDecl *
2416getPatternForClassTemplateSpecialization(
2417 Sema &S, SourceLocation PointOfInstantiation,
Larisse Voufo72caf2b2013-08-22 00:59:14 +00002418 ClassTemplateSpecializationDecl *ClassTemplateSpec,
2419 TemplateSpecializationKind TSK, bool Complain) {
Richard Smith32b43762017-01-08 22:45:21 +00002420 Sema::InstantiatingTemplate Inst(S, PointOfInstantiation, ClassTemplateSpec);
2421 if (Inst.isInvalid() || Inst.isAlreadyInstantiating())
2422 return nullptr;
2423
Volodymyr Sapsai9b973482018-05-16 18:28:58 +00002424 llvm::PointerUnion<ClassTemplateDecl *,
2425 ClassTemplatePartialSpecializationDecl *>
2426 Specialized = ClassTemplateSpec->getSpecializedTemplateOrPartial();
2427 if (!Specialized.is<ClassTemplatePartialSpecializationDecl *>()) {
2428 // Find best matching specialization.
2429 ClassTemplateDecl *Template = ClassTemplateSpec->getSpecializedTemplate();
Douglas Gregor2373c592009-05-31 09:31:02 +00002430
Volodymyr Sapsai9b973482018-05-16 18:28:58 +00002431 // C++ [temp.class.spec.match]p1:
2432 // When a class template is used in a context that requires an
2433 // instantiation of the class, it is necessary to determine
2434 // whether the instantiation is to be generated using the primary
2435 // template or one of the partial specializations. This is done by
2436 // matching the template arguments of the class template
2437 // specialization with the template argument lists of the partial
2438 // specializations.
2439 typedef PartialSpecMatchResult MatchResult;
2440 SmallVector<MatchResult, 4> Matched;
2441 SmallVector<ClassTemplatePartialSpecializationDecl *, 4> PartialSpecs;
2442 Template->getPartialSpecializations(PartialSpecs);
2443 TemplateSpecCandidateSet FailedCandidates(PointOfInstantiation);
2444 for (unsigned I = 0, N = PartialSpecs.size(); I != N; ++I) {
2445 ClassTemplatePartialSpecializationDecl *Partial = PartialSpecs[I];
2446 TemplateDeductionInfo Info(FailedCandidates.getLocation());
2447 if (Sema::TemplateDeductionResult Result = S.DeduceTemplateArguments(
2448 Partial, ClassTemplateSpec->getTemplateArgs(), Info)) {
2449 // Store the failed-deduction information for use in diagnostics, later.
2450 // TODO: Actually use the failed-deduction info?
2451 FailedCandidates.addCandidate().set(
2452 DeclAccessPair::make(Template, AS_public), Partial,
2453 MakeDeductionFailureInfo(S.Context, Result, Info));
2454 (void)Result;
2455 } else {
2456 Matched.push_back(PartialSpecMatchResult());
2457 Matched.back().Partial = Partial;
2458 Matched.back().Args = Info.take();
2459 }
2460 }
2461
2462 // If we're dealing with a member template where the template parameters
2463 // have been instantiated, this provides the original template parameters
2464 // from which the member template's parameters were instantiated.
2465
2466 if (Matched.size() >= 1) {
2467 SmallVectorImpl<MatchResult>::iterator Best = Matched.begin();
2468 if (Matched.size() == 1) {
2469 // -- If exactly one matching specialization is found, the
2470 // instantiation is generated from that specialization.
2471 // We don't need to do anything for this.
2472 } else {
2473 // -- If more than one matching specialization is found, the
2474 // partial order rules (14.5.4.2) are used to determine
2475 // whether one of the specializations is more specialized
2476 // than the others. If none of the specializations is more
2477 // specialized than all of the other matching
2478 // specializations, then the use of the class template is
2479 // ambiguous and the program is ill-formed.
2480 for (SmallVectorImpl<MatchResult>::iterator P = Best + 1,
2481 PEnd = Matched.end();
2482 P != PEnd; ++P) {
2483 if (S.getMoreSpecializedPartialSpecialization(
2484 P->Partial, Best->Partial, PointOfInstantiation) ==
2485 P->Partial)
2486 Best = P;
2487 }
2488
2489 // Determine if the best partial specialization is more specialized than
2490 // the others.
2491 bool Ambiguous = false;
2492 for (SmallVectorImpl<MatchResult>::iterator P = Matched.begin(),
2493 PEnd = Matched.end();
2494 P != PEnd; ++P) {
2495 if (P != Best && S.getMoreSpecializedPartialSpecialization(
2496 P->Partial, Best->Partial,
2497 PointOfInstantiation) != Best->Partial) {
2498 Ambiguous = true;
2499 break;
2500 }
2501 }
2502
2503 if (Ambiguous) {
2504 // Partial ordering did not produce a clear winner. Complain.
2505 Inst.Clear();
2506 ClassTemplateSpec->setInvalidDecl();
2507 S.Diag(PointOfInstantiation,
2508 diag::err_partial_spec_ordering_ambiguous)
2509 << ClassTemplateSpec;
2510
2511 // Print the matching partial specializations.
2512 for (SmallVectorImpl<MatchResult>::iterator P = Matched.begin(),
2513 PEnd = Matched.end();
2514 P != PEnd; ++P)
2515 S.Diag(P->Partial->getLocation(), diag::note_partial_spec_match)
2516 << S.getTemplateArgumentBindingsText(
2517 P->Partial->getTemplateParameters(), *P->Args);
2518
2519 return nullptr;
2520 }
2521 }
2522
2523 ClassTemplateSpec->setInstantiationOf(Best->Partial, Best->Args);
Douglas Gregor181aa4a2009-06-12 18:26:56 +00002524 } else {
Volodymyr Sapsai9b973482018-05-16 18:28:58 +00002525 // -- If no matches are found, the instantiation is generated
2526 // from the primary template.
Douglas Gregor181aa4a2009-06-12 18:26:56 +00002527 }
Douglas Gregor2373c592009-05-31 09:31:02 +00002528 }
2529
Volodymyr Sapsai9b973482018-05-16 18:28:58 +00002530 CXXRecordDecl *Pattern = nullptr;
2531 Specialized = ClassTemplateSpec->getSpecializedTemplateOrPartial();
2532 if (auto *PartialSpec =
2533 Specialized.dyn_cast<ClassTemplatePartialSpecializationDecl *>()) {
Douglas Gregorbe999392009-09-15 16:23:51 +00002534 // Instantiate using the best class template partial specialization.
Volodymyr Sapsai9b973482018-05-16 18:28:58 +00002535 while (PartialSpec->getInstantiatedFromMember()) {
Douglas Gregor21610382009-10-29 00:04:11 +00002536 // If we've found an explicit specialization of this class template,
2537 // stop here and use that as the pattern.
Volodymyr Sapsai9b973482018-05-16 18:28:58 +00002538 if (PartialSpec->isMemberSpecialization())
Douglas Gregor21610382009-10-29 00:04:11 +00002539 break;
Volodymyr Sapsai9b973482018-05-16 18:28:58 +00002540
2541 PartialSpec = PartialSpec->getInstantiatedFromMember();
Douglas Gregor21610382009-10-29 00:04:11 +00002542 }
Volodymyr Sapsai9b973482018-05-16 18:28:58 +00002543 Pattern = PartialSpec;
Douglas Gregor170bc422009-06-12 22:31:52 +00002544 } else {
Volodymyr Sapsai9b973482018-05-16 18:28:58 +00002545 ClassTemplateDecl *Template = ClassTemplateSpec->getSpecializedTemplate();
2546 while (Template->getInstantiatedFromMemberTemplate()) {
Douglas Gregorcf915552009-10-13 16:30:37 +00002547 // If we've found an explicit specialization of this class template,
2548 // stop here and use that as the pattern.
Volodymyr Sapsai9b973482018-05-16 18:28:58 +00002549 if (Template->isMemberSpecialization())
Douglas Gregorcf915552009-10-13 16:30:37 +00002550 break;
Volodymyr Sapsai9b973482018-05-16 18:28:58 +00002551
2552 Template = Template->getInstantiatedFromMemberTemplate();
Douglas Gregorcf915552009-10-13 16:30:37 +00002553 }
Volodymyr Sapsai9b973482018-05-16 18:28:58 +00002554 Pattern = Template->getTemplatedDecl();
Douglas Gregor2373c592009-05-31 09:31:02 +00002555 }
Douglas Gregor463421d2009-03-03 04:44:36 +00002556
Richard Smith32b43762017-01-08 22:45:21 +00002557 return Pattern;
2558}
Mike Stump11289f42009-09-09 15:08:12 +00002559
Richard Smith32b43762017-01-08 22:45:21 +00002560bool Sema::InstantiateClassTemplateSpecialization(
2561 SourceLocation PointOfInstantiation,
2562 ClassTemplateSpecializationDecl *ClassTemplateSpec,
2563 TemplateSpecializationKind TSK, bool Complain) {
2564 // Perform the actual instantiation on the canonical declaration.
2565 ClassTemplateSpec = cast<ClassTemplateSpecializationDecl>(
2566 ClassTemplateSpec->getCanonicalDecl());
2567 if (ClassTemplateSpec->isInvalidDecl())
2568 return true;
2569
2570 CXXRecordDecl *Pattern = getPatternForClassTemplateSpecialization(
2571 *this, PointOfInstantiation, ClassTemplateSpec, TSK, Complain);
2572 if (!Pattern)
2573 return true;
2574
2575 return InstantiateClass(PointOfInstantiation, ClassTemplateSpec, Pattern,
2576 getTemplateInstantiationArgs(ClassTemplateSpec), TSK,
2577 Complain);
Douglas Gregor463421d2009-03-03 04:44:36 +00002578}
Douglas Gregor90a1a652009-03-19 17:26:29 +00002579
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002580/// Instantiates the definitions of all of the member
John McCall76d824f2009-08-25 22:02:44 +00002581/// of the given class, which is an instantiation of a class template
2582/// or a member class of a template.
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00002583void
2584Sema::InstantiateClassMembers(SourceLocation PointOfInstantiation,
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00002585 CXXRecordDecl *Instantiation,
2586 const MultiLevelTemplateArgumentList &TemplateArgs,
2587 TemplateSpecializationKind TSK) {
Richard Smitheb36ddf2014-04-24 22:45:46 +00002588 // FIXME: We need to notify the ASTMutationListener that we did all of these
2589 // things, in case we have an explicit instantiation definition in a PCM, a
2590 // module, or preamble, and the declaration is in an imported AST.
David Majnemer192d1792013-11-27 08:20:38 +00002591 assert(
2592 (TSK == TSK_ExplicitInstantiationDefinition ||
2593 TSK == TSK_ExplicitInstantiationDeclaration ||
2594 (TSK == TSK_ImplicitInstantiation && Instantiation->isLocalClass())) &&
2595 "Unexpected template specialization kind!");
Aaron Ballman629afae2014-03-07 19:56:05 +00002596 for (auto *D : Instantiation->decls()) {
Douglas Gregor1d957a32009-10-27 18:42:08 +00002597 bool SuppressNew = false;
Aaron Ballman629afae2014-03-07 19:56:05 +00002598 if (auto *Function = dyn_cast<FunctionDecl>(D)) {
Louis Dionned2695792018-10-04 15:49:42 +00002599 if (FunctionDecl *Pattern =
2600 Function->getInstantiatedFromMemberFunction()) {
2601
2602 if (Function->hasAttr<ExcludeFromExplicitInstantiationAttr>())
2603 continue;
2604
2605 MemberSpecializationInfo *MSInfo =
2606 Function->getMemberSpecializationInfo();
Douglas Gregor1d957a32009-10-27 18:42:08 +00002607 assert(MSInfo && "No member specialization information?");
Douglas Gregor06aa50412010-04-09 21:02:29 +00002608 if (MSInfo->getTemplateSpecializationKind()
2609 == TSK_ExplicitSpecialization)
2610 continue;
Fangrui Song6907ce22018-07-30 19:24:48 +00002611
2612 if (CheckSpecializationInstantiationRedecl(PointOfInstantiation, TSK,
2613 Function,
Douglas Gregor1d957a32009-10-27 18:42:08 +00002614 MSInfo->getTemplateSpecializationKind(),
Nico Weberd75488d2010-09-27 21:02:09 +00002615 MSInfo->getPointOfInstantiation(),
Douglas Gregor1d957a32009-10-27 18:42:08 +00002616 SuppressNew) ||
2617 SuppressNew)
Douglas Gregorbbe8f462009-10-08 15:14:33 +00002618 continue;
Richard Smitheb36ddf2014-04-24 22:45:46 +00002619
2620 // C++11 [temp.explicit]p8:
2621 // An explicit instantiation definition that names a class template
2622 // specialization explicitly instantiates the class template
2623 // specialization and is only an explicit instantiation definition
2624 // of members whose definition is visible at the point of
2625 // instantiation.
2626 if (TSK == TSK_ExplicitInstantiationDefinition && !Pattern->isDefined())
Douglas Gregor1d957a32009-10-27 18:42:08 +00002627 continue;
2628
Richard Smitheb36ddf2014-04-24 22:45:46 +00002629 Function->setTemplateSpecializationKind(TSK, PointOfInstantiation);
2630
2631 if (Function->isDefined()) {
2632 // Let the ASTConsumer know that this function has been explicitly
2633 // instantiated now, and its linkage might have changed.
2634 Consumer.HandleTopLevelDecl(DeclGroupRef(Function));
2635 } else if (TSK == TSK_ExplicitInstantiationDefinition) {
Douglas Gregor1d957a32009-10-27 18:42:08 +00002636 InstantiateFunctionDefinition(PointOfInstantiation, Function);
Richard Smitheb36ddf2014-04-24 22:45:46 +00002637 } else if (TSK == TSK_ImplicitInstantiation) {
2638 PendingLocalImplicitInstantiations.push_back(
2639 std::make_pair(Function, PointOfInstantiation));
Douglas Gregor1d957a32009-10-27 18:42:08 +00002640 }
Douglas Gregorbbe8f462009-10-08 15:14:33 +00002641 }
Aaron Ballman629afae2014-03-07 19:56:05 +00002642 } else if (auto *Var = dyn_cast<VarDecl>(D)) {
Richard Smith8809a0c2013-09-27 20:14:12 +00002643 if (isa<VarTemplateSpecializationDecl>(Var))
2644 continue;
2645
Douglas Gregor86d142a2009-10-08 07:24:58 +00002646 if (Var->isStaticDataMember()) {
Louis Dionned2695792018-10-04 15:49:42 +00002647 if (Var->hasAttr<ExcludeFromExplicitInstantiationAttr>())
2648 continue;
2649
Douglas Gregor1d957a32009-10-27 18:42:08 +00002650 MemberSpecializationInfo *MSInfo = Var->getMemberSpecializationInfo();
2651 assert(MSInfo && "No member specialization information?");
Douglas Gregor06aa50412010-04-09 21:02:29 +00002652 if (MSInfo->getTemplateSpecializationKind()
2653 == TSK_ExplicitSpecialization)
2654 continue;
Fangrui Song6907ce22018-07-30 19:24:48 +00002655
2656 if (CheckSpecializationInstantiationRedecl(PointOfInstantiation, TSK,
2657 Var,
Douglas Gregor1d957a32009-10-27 18:42:08 +00002658 MSInfo->getTemplateSpecializationKind(),
Nico Weberd75488d2010-09-27 21:02:09 +00002659 MSInfo->getPointOfInstantiation(),
Douglas Gregor1d957a32009-10-27 18:42:08 +00002660 SuppressNew) ||
2661 SuppressNew)
Douglas Gregorbbe8f462009-10-08 15:14:33 +00002662 continue;
Fangrui Song6907ce22018-07-30 19:24:48 +00002663
Douglas Gregor1d957a32009-10-27 18:42:08 +00002664 if (TSK == TSK_ExplicitInstantiationDefinition) {
2665 // C++0x [temp.explicit]p8:
2666 // An explicit instantiation definition that names a class template
Fangrui Song6907ce22018-07-30 19:24:48 +00002667 // specialization explicitly instantiates the class template
2668 // specialization and is only an explicit instantiation definition
2669 // of members whose definition is visible at the point of
Douglas Gregor1d957a32009-10-27 18:42:08 +00002670 // instantiation.
Richard Smith62f19e72016-06-25 00:15:56 +00002671 if (!Var->getInstantiatedFromStaticDataMember()->getDefinition())
Douglas Gregor1d957a32009-10-27 18:42:08 +00002672 continue;
Fangrui Song6907ce22018-07-30 19:24:48 +00002673
Douglas Gregor1d957a32009-10-27 18:42:08 +00002674 Var->setTemplateSpecializationKind(TSK, PointOfInstantiation);
Richard Smith891fc7f2017-12-05 01:31:47 +00002675 InstantiateVariableDefinition(PointOfInstantiation, Var);
Douglas Gregor1d957a32009-10-27 18:42:08 +00002676 } else {
2677 Var->setTemplateSpecializationKind(TSK, PointOfInstantiation);
2678 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002679 }
Aaron Ballman629afae2014-03-07 19:56:05 +00002680 } else if (auto *Record = dyn_cast<CXXRecordDecl>(D)) {
Louis Dionned2695792018-10-04 15:49:42 +00002681 if (Record->hasAttr<ExcludeFromExplicitInstantiationAttr>())
2682 continue;
2683
Douglas Gregor1da22252010-04-18 18:11:38 +00002684 // Always skip the injected-class-name, along with any
2685 // redeclarations of nested classes, since both would cause us
2686 // to try to instantiate the members of a class twice.
Richard Smith069ecf62014-11-20 22:56:34 +00002687 // Skip closure types; they'll get instantiated when we instantiate
2688 // the corresponding lambda-expression.
2689 if (Record->isInjectedClassName() || Record->getPreviousDecl() ||
2690 Record->isLambda())
Douglas Gregord801b062009-10-07 23:56:10 +00002691 continue;
Fangrui Song6907ce22018-07-30 19:24:48 +00002692
Douglas Gregor1d957a32009-10-27 18:42:08 +00002693 MemberSpecializationInfo *MSInfo = Record->getMemberSpecializationInfo();
2694 assert(MSInfo && "No member specialization information?");
Fangrui Song6907ce22018-07-30 19:24:48 +00002695
Douglas Gregor06aa50412010-04-09 21:02:29 +00002696 if (MSInfo->getTemplateSpecializationKind()
2697 == TSK_ExplicitSpecialization)
2698 continue;
Nico Weberd75488d2010-09-27 21:02:09 +00002699
Reid Kleckner5f789ba2019-04-29 21:32:05 +00002700 if (Context.getTargetInfo().getTriple().isOSWindows() &&
Hans Wennborga86a83b2016-05-26 19:42:56 +00002701 TSK == TSK_ExplicitInstantiationDeclaration) {
Reid Kleckner5f789ba2019-04-29 21:32:05 +00002702 // On Windows, explicit instantiation decl of the outer class doesn't
2703 // affect the inner class. Typically extern template declarations are
2704 // used in combination with dll import/export annotations, but those
2705 // are not propagated from the outer class templates to inner classes.
2706 // Therefore, do not instantiate inner classes on this platform, so
2707 // that users don't end up with undefined symbols during linking.
Hans Wennborga86a83b2016-05-26 19:42:56 +00002708 continue;
2709 }
2710
Fangrui Song6907ce22018-07-30 19:24:48 +00002711 if (CheckSpecializationInstantiationRedecl(PointOfInstantiation, TSK,
2712 Record,
Douglas Gregor1d957a32009-10-27 18:42:08 +00002713 MSInfo->getTemplateSpecializationKind(),
Nico Weberd75488d2010-09-27 21:02:09 +00002714 MSInfo->getPointOfInstantiation(),
Douglas Gregor1d957a32009-10-27 18:42:08 +00002715 SuppressNew) ||
2716 SuppressNew)
Douglas Gregorbbe8f462009-10-08 15:14:33 +00002717 continue;
Fangrui Song6907ce22018-07-30 19:24:48 +00002718
Douglas Gregor1d957a32009-10-27 18:42:08 +00002719 CXXRecordDecl *Pattern = Record->getInstantiatedFromMemberClass();
2720 assert(Pattern && "Missing instantiated-from-template information");
Fangrui Song6907ce22018-07-30 19:24:48 +00002721
Douglas Gregor0a5a2212010-02-11 01:04:33 +00002722 if (!Record->getDefinition()) {
2723 if (!Pattern->getDefinition()) {
Douglas Gregor1d957a32009-10-27 18:42:08 +00002724 // C++0x [temp.explicit]p8:
2725 // An explicit instantiation definition that names a class template
Fangrui Song6907ce22018-07-30 19:24:48 +00002726 // specialization explicitly instantiates the class template
2727 // specialization and is only an explicit instantiation definition
2728 // of members whose definition is visible at the point of
Douglas Gregor1d957a32009-10-27 18:42:08 +00002729 // instantiation.
2730 if (TSK == TSK_ExplicitInstantiationDeclaration) {
2731 MSInfo->setTemplateSpecializationKind(TSK);
2732 MSInfo->setPointOfInstantiation(PointOfInstantiation);
2733 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002734
Douglas Gregor1d957a32009-10-27 18:42:08 +00002735 continue;
2736 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002737
Douglas Gregor1d957a32009-10-27 18:42:08 +00002738 InstantiateClass(PointOfInstantiation, Record, Pattern,
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00002739 TemplateArgs,
2740 TSK);
Nico Weberd75488d2010-09-27 21:02:09 +00002741 } else {
2742 if (TSK == TSK_ExplicitInstantiationDefinition &&
2743 Record->getTemplateSpecializationKind() ==
2744 TSK_ExplicitInstantiationDeclaration) {
2745 Record->setTemplateSpecializationKind(TSK);
2746 MarkVTableUsed(PointOfInstantiation, Record, true);
2747 }
Douglas Gregor1d957a32009-10-27 18:42:08 +00002748 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002749
Douglas Gregor0a5a2212010-02-11 01:04:33 +00002750 Pattern = cast_or_null<CXXRecordDecl>(Record->getDefinition());
Douglas Gregor1d957a32009-10-27 18:42:08 +00002751 if (Pattern)
Fangrui Song6907ce22018-07-30 19:24:48 +00002752 InstantiateClassMembers(PointOfInstantiation, Pattern, TemplateArgs,
Douglas Gregor1d957a32009-10-27 18:42:08 +00002753 TSK);
Aaron Ballman629afae2014-03-07 19:56:05 +00002754 } else if (auto *Enum = dyn_cast<EnumDecl>(D)) {
Richard Smith4b38ded2012-03-14 23:13:10 +00002755 MemberSpecializationInfo *MSInfo = Enum->getMemberSpecializationInfo();
2756 assert(MSInfo && "No member specialization information?");
2757
2758 if (MSInfo->getTemplateSpecializationKind()
2759 == TSK_ExplicitSpecialization)
2760 continue;
2761
2762 if (CheckSpecializationInstantiationRedecl(
2763 PointOfInstantiation, TSK, Enum,
2764 MSInfo->getTemplateSpecializationKind(),
2765 MSInfo->getPointOfInstantiation(), SuppressNew) ||
2766 SuppressNew)
2767 continue;
2768
2769 if (Enum->getDefinition())
2770 continue;
2771
Richard Smith6739a102016-05-05 00:56:12 +00002772 EnumDecl *Pattern = Enum->getTemplateInstantiationPattern();
Richard Smith4b38ded2012-03-14 23:13:10 +00002773 assert(Pattern && "Missing instantiated-from-template information");
2774
2775 if (TSK == TSK_ExplicitInstantiationDefinition) {
2776 if (!Pattern->getDefinition())
2777 continue;
2778
2779 InstantiateEnum(PointOfInstantiation, Enum, Pattern, TemplateArgs, TSK);
2780 } else {
2781 MSInfo->setTemplateSpecializationKind(TSK);
2782 MSInfo->setPointOfInstantiation(PointOfInstantiation);
2783 }
Reid Klecknerd60b82f2014-11-17 23:36:45 +00002784 } else if (auto *Field = dyn_cast<FieldDecl>(D)) {
2785 // No need to instantiate in-class initializers during explicit
2786 // instantiation.
2787 if (Field->hasInClassInitializer() && TSK == TSK_ImplicitInstantiation) {
2788 CXXRecordDecl *ClassPattern =
2789 Instantiation->getTemplateInstantiationPattern();
2790 DeclContext::lookup_result Lookup =
2791 ClassPattern->lookup(Field->getDeclName());
David Majnemer76a25622016-06-09 05:26:56 +00002792 FieldDecl *Pattern = cast<FieldDecl>(Lookup.front());
Reid Klecknerd60b82f2014-11-17 23:36:45 +00002793 InstantiateInClassInitializer(PointOfInstantiation, Field, Pattern,
2794 TemplateArgs);
2795 }
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00002796 }
2797 }
2798}
2799
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002800/// Instantiate the definitions of all of the members of the
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00002801/// given class template specialization, which was named as part of an
2802/// explicit instantiation.
Mike Stump11289f42009-09-09 15:08:12 +00002803void
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00002804Sema::InstantiateClassTemplateSpecializationMembers(
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00002805 SourceLocation PointOfInstantiation,
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00002806 ClassTemplateSpecializationDecl *ClassTemplateSpec,
2807 TemplateSpecializationKind TSK) {
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00002808 // C++0x [temp.explicit]p7:
2809 // An explicit instantiation that names a class template
2810 // specialization is an explicit instantion of the same kind
2811 // (declaration or definition) of each of its members (not
2812 // including members inherited from base classes) that has not
2813 // been previously explicitly specialized in the translation unit
2814 // containing the explicit instantiation, except as described
2815 // below.
2816 InstantiateClassMembers(PointOfInstantiation, ClassTemplateSpec,
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00002817 getTemplateInstantiationArgs(ClassTemplateSpec),
2818 TSK);
Douglas Gregorbbbb02d2009-05-13 20:28:22 +00002819}
2820
John McCalldadc5752010-08-24 06:29:42 +00002821StmtResult
Douglas Gregor01afeef2009-08-28 20:31:08 +00002822Sema::SubstStmt(Stmt *S, const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregorebe10102009-08-20 07:17:43 +00002823 if (!S)
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00002824 return S;
Douglas Gregorebe10102009-08-20 07:17:43 +00002825
2826 TemplateInstantiator Instantiator(*this, TemplateArgs,
2827 SourceLocation(),
2828 DeclarationName());
2829 return Instantiator.TransformStmt(S);
2830}
2831
John McCalldadc5752010-08-24 06:29:42 +00002832ExprResult
Douglas Gregor01afeef2009-08-28 20:31:08 +00002833Sema::SubstExpr(Expr *E, const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregora16548e2009-08-11 05:31:07 +00002834 if (!E)
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00002835 return E;
Mike Stump11289f42009-09-09 15:08:12 +00002836
Douglas Gregora16548e2009-08-11 05:31:07 +00002837 TemplateInstantiator Instantiator(*this, TemplateArgs,
2838 SourceLocation(),
2839 DeclarationName());
2840 return Instantiator.TransformExpr(E);
2841}
2842
Richard Smithd59b8322012-12-19 01:39:02 +00002843ExprResult Sema::SubstInitializer(Expr *Init,
2844 const MultiLevelTemplateArgumentList &TemplateArgs,
2845 bool CXXDirectInit) {
2846 TemplateInstantiator Instantiator(*this, TemplateArgs,
2847 SourceLocation(),
2848 DeclarationName());
2849 return Instantiator.TransformInitializer(Init, CXXDirectInit);
2850}
2851
Craig Topper99d23532015-12-24 23:58:29 +00002852bool Sema::SubstExprs(ArrayRef<Expr *> Exprs, bool IsCall,
Douglas Gregor2cd32a02011-01-07 19:35:17 +00002853 const MultiLevelTemplateArgumentList &TemplateArgs,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002854 SmallVectorImpl<Expr *> &Outputs) {
Craig Topper99d23532015-12-24 23:58:29 +00002855 if (Exprs.empty())
Douglas Gregor2cd32a02011-01-07 19:35:17 +00002856 return false;
Richard Smithd59b8322012-12-19 01:39:02 +00002857
Douglas Gregor2cd32a02011-01-07 19:35:17 +00002858 TemplateInstantiator Instantiator(*this, TemplateArgs,
2859 SourceLocation(),
2860 DeclarationName());
Craig Topper99d23532015-12-24 23:58:29 +00002861 return Instantiator.TransformExprs(Exprs.data(), Exprs.size(),
2862 IsCall, Outputs);
Douglas Gregor2cd32a02011-01-07 19:35:17 +00002863}
2864
Douglas Gregor14454802011-02-25 02:25:35 +00002865NestedNameSpecifierLoc
2866Sema::SubstNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS,
Fangrui Song6907ce22018-07-30 19:24:48 +00002867 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor14454802011-02-25 02:25:35 +00002868 if (!NNS)
2869 return NestedNameSpecifierLoc();
Fangrui Song6907ce22018-07-30 19:24:48 +00002870
Douglas Gregor14454802011-02-25 02:25:35 +00002871 TemplateInstantiator Instantiator(*this, TemplateArgs, NNS.getBeginLoc(),
2872 DeclarationName());
2873 return Instantiator.TransformNestedNameSpecifierLoc(NNS);
2874}
2875
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002876/// Do template substitution on declaration name info.
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002877DeclarationNameInfo
2878Sema::SubstDeclarationNameInfo(const DeclarationNameInfo &NameInfo,
2879 const MultiLevelTemplateArgumentList &TemplateArgs) {
2880 TemplateInstantiator Instantiator(*this, TemplateArgs, NameInfo.getLoc(),
2881 NameInfo.getName());
2882 return Instantiator.TransformDeclarationNameInfo(NameInfo);
2883}
2884
Douglas Gregoraa594892009-03-31 18:38:02 +00002885TemplateName
Douglas Gregordf846d12011-03-02 18:46:51 +00002886Sema::SubstTemplateName(NestedNameSpecifierLoc QualifierLoc,
2887 TemplateName Name, SourceLocation Loc,
Douglas Gregor01afeef2009-08-28 20:31:08 +00002888 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregor71dc5092009-08-06 06:41:21 +00002889 TemplateInstantiator Instantiator(*this, TemplateArgs, Loc,
2890 DeclarationName());
Douglas Gregordf846d12011-03-02 18:46:51 +00002891 CXXScopeSpec SS;
2892 SS.Adopt(QualifierLoc);
2893 return Instantiator.TransformTemplateName(SS, Name, Loc);
Douglas Gregoraa594892009-03-31 18:38:02 +00002894}
Douglas Gregorc43620d2009-06-11 00:06:24 +00002895
Douglas Gregor0f3feb42010-12-22 21:19:48 +00002896bool Sema::Subst(const TemplateArgumentLoc *Args, unsigned NumArgs,
2897 TemplateArgumentListInfo &Result,
John McCall0ad16662009-10-29 08:12:44 +00002898 const MultiLevelTemplateArgumentList &TemplateArgs) {
Douglas Gregore922c772009-08-04 22:27:00 +00002899 TemplateInstantiator Instantiator(*this, TemplateArgs, SourceLocation(),
2900 DeclarationName());
Fangrui Song6907ce22018-07-30 19:24:48 +00002901
Douglas Gregor0f3feb42010-12-22 21:19:48 +00002902 return Instantiator.TransformTemplateArguments(Args, NumArgs, Result);
Douglas Gregorc43620d2009-06-11 00:06:24 +00002903}
Douglas Gregor14cf7522010-04-30 18:55:50 +00002904
Richard Smith70b13042015-01-09 01:19:56 +00002905static const Decl *getCanonicalParmVarDecl(const Decl *D) {
DeLesley Hutchinsf39c0c22012-09-26 17:57:31 +00002906 // When storing ParmVarDecls in the local instantiation scope, we always
2907 // want to use the ParmVarDecl from the canonical function declaration,
2908 // since the map is then valid for any redeclaration or definition of that
2909 // function.
2910 if (const ParmVarDecl *PV = dyn_cast<ParmVarDecl>(D)) {
2911 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(PV->getDeclContext())) {
2912 unsigned i = PV->getFunctionScopeIndex();
Richard Smith70b13042015-01-09 01:19:56 +00002913 // This parameter might be from a freestanding function type within the
2914 // function and isn't necessarily referring to one of FD's parameters.
Dmitri Gribenko66b6bb12019-04-10 20:25:07 +00002915 if (i < FD->getNumParams() && FD->getParamDecl(i) == PV)
Richard Smith70b13042015-01-09 01:19:56 +00002916 return FD->getCanonicalDecl()->getParamDecl(i);
DeLesley Hutchinsf39c0c22012-09-26 17:57:31 +00002917 }
2918 }
2919 return D;
2920}
2921
2922
Douglas Gregorf3010112011-01-07 16:43:16 +00002923llvm::PointerUnion<Decl *, LocalInstantiationScope::DeclArgumentPack *> *
2924LocalInstantiationScope::findInstantiationOf(const Decl *D) {
DeLesley Hutchinsf39c0c22012-09-26 17:57:31 +00002925 D = getCanonicalParmVarDecl(D);
Chris Lattnercab02a62011-02-17 20:34:02 +00002926 for (LocalInstantiationScope *Current = this; Current;
Douglas Gregor14cf7522010-04-30 18:55:50 +00002927 Current = Current->Outer) {
Chris Lattnercab02a62011-02-17 20:34:02 +00002928
Douglas Gregor14cf7522010-04-30 18:55:50 +00002929 // Check if we found something within this scope.
Douglas Gregore9fc8dc2010-12-21 21:22:51 +00002930 const Decl *CheckD = D;
2931 do {
Douglas Gregorf3010112011-01-07 16:43:16 +00002932 LocalDeclsMap::iterator Found = Current->LocalDecls.find(CheckD);
Douglas Gregore9fc8dc2010-12-21 21:22:51 +00002933 if (Found != Current->LocalDecls.end())
Douglas Gregorf3010112011-01-07 16:43:16 +00002934 return &Found->second;
Fangrui Song6907ce22018-07-30 19:24:48 +00002935
Douglas Gregore9fc8dc2010-12-21 21:22:51 +00002936 // If this is a tag declaration, it's possible that we need to look for
2937 // a previous declaration.
2938 if (const TagDecl *Tag = dyn_cast<TagDecl>(CheckD))
Douglas Gregorec9fd132012-01-14 16:38:05 +00002939 CheckD = Tag->getPreviousDecl();
Douglas Gregore9fc8dc2010-12-21 21:22:51 +00002940 else
Craig Topperc3ec1492014-05-26 06:22:03 +00002941 CheckD = nullptr;
Douglas Gregore9fc8dc2010-12-21 21:22:51 +00002942 } while (CheckD);
Fangrui Song6907ce22018-07-30 19:24:48 +00002943
2944 // If we aren't combined with our outer scope, we're done.
Douglas Gregor14cf7522010-04-30 18:55:50 +00002945 if (!Current->CombineWithOuterScope)
2946 break;
2947 }
Chris Lattnercab02a62011-02-17 20:34:02 +00002948
Serge Pavlov7cd8f602013-07-15 06:14:07 +00002949 // If we're performing a partial substitution during template argument
2950 // deduction, we may not have values for template parameters yet.
2951 if (isa<NonTypeTemplateParmDecl>(D) || isa<TemplateTypeParmDecl>(D) ||
2952 isa<TemplateTemplateParmDecl>(D))
Craig Topperc3ec1492014-05-26 06:22:03 +00002953 return nullptr;
Serge Pavlov7cd8f602013-07-15 06:14:07 +00002954
Serge Pavlove7ad8312015-05-15 10:10:28 +00002955 // Local types referenced prior to definition may require instantiation.
2956 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D))
2957 if (RD->isLocalClass())
2958 return nullptr;
2959
2960 // Enumeration types referenced prior to definition may appear as a result of
2961 // error recovery.
2962 if (isa<EnumDecl>(D))
Serge Pavlov4c511742015-05-04 16:44:39 +00002963 return nullptr;
2964
Chris Lattnercab02a62011-02-17 20:34:02 +00002965 // If we didn't find the decl, then we either have a sema bug, or we have a
2966 // forward reference to a label declaration. Return null to indicate that
2967 // we have an uninstantiated label.
2968 assert(isa<LabelDecl>(D) && "declaration not instantiated in this scope");
Craig Topperc3ec1492014-05-26 06:22:03 +00002969 return nullptr;
Douglas Gregor14cf7522010-04-30 18:55:50 +00002970}
2971
John McCall19c1bfd2010-08-25 05:32:35 +00002972void LocalInstantiationScope::InstantiatedLocal(const Decl *D, Decl *Inst) {
DeLesley Hutchinsf39c0c22012-09-26 17:57:31 +00002973 D = getCanonicalParmVarDecl(D);
Douglas Gregorf3010112011-01-07 16:43:16 +00002974 llvm::PointerUnion<Decl *, DeclArgumentPack *> &Stored = LocalDecls[D];
Richard Smith70b13042015-01-09 01:19:56 +00002975 if (Stored.isNull()) {
2976#ifndef NDEBUG
2977 // It should not be present in any surrounding scope either.
2978 LocalInstantiationScope *Current = this;
2979 while (Current->CombineWithOuterScope && Current->Outer) {
2980 Current = Current->Outer;
2981 assert(Current->LocalDecls.find(D) == Current->LocalDecls.end() &&
2982 "Instantiated local in inner and outer scopes");
2983 }
2984#endif
Douglas Gregora8bac7f2011-01-10 07:32:04 +00002985 Stored = Inst;
Richard Smith70b13042015-01-09 01:19:56 +00002986 } else if (DeclArgumentPack *Pack = Stored.dyn_cast<DeclArgumentPack *>()) {
James Y Knight48fefa32015-09-30 14:04:23 +00002987 Pack->push_back(cast<ParmVarDecl>(Inst));
Richard Smith70b13042015-01-09 01:19:56 +00002988 } else {
Douglas Gregora8bac7f2011-01-10 07:32:04 +00002989 assert(Stored.get<Decl *>() == Inst && "Already instantiated this local");
Richard Smith70b13042015-01-09 01:19:56 +00002990 }
Douglas Gregor14cf7522010-04-30 18:55:50 +00002991}
Douglas Gregorf3010112011-01-07 16:43:16 +00002992
James Y Knight48fefa32015-09-30 14:04:23 +00002993void LocalInstantiationScope::InstantiatedLocalPackArg(const Decl *D,
2994 ParmVarDecl *Inst) {
DeLesley Hutchinsf39c0c22012-09-26 17:57:31 +00002995 D = getCanonicalParmVarDecl(D);
Douglas Gregorf3010112011-01-07 16:43:16 +00002996 DeclArgumentPack *Pack = LocalDecls[D].get<DeclArgumentPack *>();
2997 Pack->push_back(Inst);
2998}
2999
3000void LocalInstantiationScope::MakeInstantiatedLocalArgPack(const Decl *D) {
Richard Smith70b13042015-01-09 01:19:56 +00003001#ifndef NDEBUG
3002 // This should be the first time we've been told about this decl.
3003 for (LocalInstantiationScope *Current = this;
3004 Current && Current->CombineWithOuterScope; Current = Current->Outer)
3005 assert(Current->LocalDecls.find(D) == Current->LocalDecls.end() &&
3006 "Creating local pack after instantiation of local");
3007#endif
3008
DeLesley Hutchinsf39c0c22012-09-26 17:57:31 +00003009 D = getCanonicalParmVarDecl(D);
Douglas Gregorf3010112011-01-07 16:43:16 +00003010 llvm::PointerUnion<Decl *, DeclArgumentPack *> &Stored = LocalDecls[D];
Douglas Gregorf3010112011-01-07 16:43:16 +00003011 DeclArgumentPack *Pack = new DeclArgumentPack;
3012 Stored = Pack;
3013 ArgumentPacks.push_back(Pack);
3014}
3015
Fangrui Song6907ce22018-07-30 19:24:48 +00003016void LocalInstantiationScope::SetPartiallySubstitutedPack(NamedDecl *Pack,
Douglas Gregora8bac7f2011-01-10 07:32:04 +00003017 const TemplateArgument *ExplicitArgs,
3018 unsigned NumExplicitArgs) {
3019 assert((!PartiallySubstitutedPack || PartiallySubstitutedPack == Pack) &&
3020 "Already have a partially-substituted pack");
Fangrui Song6907ce22018-07-30 19:24:48 +00003021 assert((!PartiallySubstitutedPack
Douglas Gregora8bac7f2011-01-10 07:32:04 +00003022 || NumArgsInPartiallySubstitutedPack == NumExplicitArgs) &&
3023 "Wrong number of arguments in partially-substituted pack");
3024 PartiallySubstitutedPack = Pack;
3025 ArgsInPartiallySubstitutedPack = ExplicitArgs;
3026 NumArgsInPartiallySubstitutedPack = NumExplicitArgs;
3027}
3028
3029NamedDecl *LocalInstantiationScope::getPartiallySubstitutedPack(
3030 const TemplateArgument **ExplicitArgs,
3031 unsigned *NumExplicitArgs) const {
3032 if (ExplicitArgs)
Craig Topperc3ec1492014-05-26 06:22:03 +00003033 *ExplicitArgs = nullptr;
Douglas Gregora8bac7f2011-01-10 07:32:04 +00003034 if (NumExplicitArgs)
3035 *NumExplicitArgs = 0;
Fangrui Song6907ce22018-07-30 19:24:48 +00003036
3037 for (const LocalInstantiationScope *Current = this; Current;
Douglas Gregora8bac7f2011-01-10 07:32:04 +00003038 Current = Current->Outer) {
3039 if (Current->PartiallySubstitutedPack) {
3040 if (ExplicitArgs)
3041 *ExplicitArgs = Current->ArgsInPartiallySubstitutedPack;
3042 if (NumExplicitArgs)
3043 *NumExplicitArgs = Current->NumArgsInPartiallySubstitutedPack;
Fangrui Song6907ce22018-07-30 19:24:48 +00003044
Douglas Gregora8bac7f2011-01-10 07:32:04 +00003045 return Current->PartiallySubstitutedPack;
3046 }
3047
3048 if (!Current->CombineWithOuterScope)
3049 break;
3050 }
Craig Topperc3ec1492014-05-26 06:22:03 +00003051
3052 return nullptr;
Douglas Gregora8bac7f2011-01-10 07:32:04 +00003053}