1f4a2713aSLionel Sambuc //===------- SemaTemplateInstantiate.cpp - C++ Template Instantiation ------===/
2f4a2713aSLionel Sambuc //
3f4a2713aSLionel Sambuc // The LLVM Compiler Infrastructure
4f4a2713aSLionel Sambuc //
5f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source
6f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details.
7f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===/
8f4a2713aSLionel Sambuc //
9f4a2713aSLionel Sambuc // This file implements C++ template instantiation.
10f4a2713aSLionel Sambuc //
11f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===/
12f4a2713aSLionel Sambuc
13f4a2713aSLionel Sambuc #include "clang/Sema/SemaInternal.h"
14f4a2713aSLionel Sambuc #include "TreeTransform.h"
15f4a2713aSLionel Sambuc #include "clang/AST/ASTConsumer.h"
16f4a2713aSLionel Sambuc #include "clang/AST/ASTContext.h"
17f4a2713aSLionel Sambuc #include "clang/AST/ASTLambda.h"
18f4a2713aSLionel Sambuc #include "clang/AST/DeclTemplate.h"
19f4a2713aSLionel Sambuc #include "clang/AST/Expr.h"
20f4a2713aSLionel Sambuc #include "clang/Basic/LangOptions.h"
21f4a2713aSLionel Sambuc #include "clang/Sema/DeclSpec.h"
22f4a2713aSLionel Sambuc #include "clang/Sema/Initialization.h"
23f4a2713aSLionel Sambuc #include "clang/Sema/Lookup.h"
24f4a2713aSLionel Sambuc #include "clang/Sema/Template.h"
25f4a2713aSLionel Sambuc #include "clang/Sema/TemplateDeduction.h"
26f4a2713aSLionel Sambuc
27f4a2713aSLionel Sambuc using namespace clang;
28f4a2713aSLionel Sambuc using namespace sema;
29f4a2713aSLionel Sambuc
30f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===/
31f4a2713aSLionel Sambuc // Template Instantiation Support
32f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===/
33f4a2713aSLionel Sambuc
34f4a2713aSLionel Sambuc /// \brief Retrieve the template argument list(s) that should be used to
35f4a2713aSLionel Sambuc /// instantiate the definition of the given declaration.
36f4a2713aSLionel Sambuc ///
37f4a2713aSLionel Sambuc /// \param D the declaration for which we are computing template instantiation
38f4a2713aSLionel Sambuc /// arguments.
39f4a2713aSLionel Sambuc ///
40f4a2713aSLionel Sambuc /// \param Innermost if non-NULL, the innermost template argument list.
41f4a2713aSLionel Sambuc ///
42f4a2713aSLionel Sambuc /// \param RelativeToPrimary true if we should get the template
43f4a2713aSLionel Sambuc /// arguments relative to the primary template, even when we're
44f4a2713aSLionel Sambuc /// dealing with a specialization. This is only relevant for function
45f4a2713aSLionel Sambuc /// template specializations.
46f4a2713aSLionel Sambuc ///
47f4a2713aSLionel Sambuc /// \param Pattern If non-NULL, indicates the pattern from which we will be
48f4a2713aSLionel Sambuc /// instantiating the definition of the given declaration, \p D. This is
49f4a2713aSLionel Sambuc /// used to determine the proper set of template instantiation arguments for
50f4a2713aSLionel Sambuc /// friend function template specializations.
51f4a2713aSLionel Sambuc MultiLevelTemplateArgumentList
getTemplateInstantiationArgs(NamedDecl * D,const TemplateArgumentList * Innermost,bool RelativeToPrimary,const FunctionDecl * Pattern)52f4a2713aSLionel Sambuc Sema::getTemplateInstantiationArgs(NamedDecl *D,
53f4a2713aSLionel Sambuc const TemplateArgumentList *Innermost,
54f4a2713aSLionel Sambuc bool RelativeToPrimary,
55f4a2713aSLionel Sambuc const FunctionDecl *Pattern) {
56f4a2713aSLionel Sambuc // Accumulate the set of template argument lists in this structure.
57f4a2713aSLionel Sambuc MultiLevelTemplateArgumentList Result;
58f4a2713aSLionel Sambuc
59f4a2713aSLionel Sambuc if (Innermost)
60f4a2713aSLionel Sambuc Result.addOuterTemplateArguments(Innermost);
61f4a2713aSLionel Sambuc
62f4a2713aSLionel Sambuc DeclContext *Ctx = dyn_cast<DeclContext>(D);
63f4a2713aSLionel Sambuc if (!Ctx) {
64f4a2713aSLionel Sambuc Ctx = D->getDeclContext();
65f4a2713aSLionel Sambuc
66f4a2713aSLionel Sambuc // Add template arguments from a variable template instantiation.
67f4a2713aSLionel Sambuc if (VarTemplateSpecializationDecl *Spec =
68f4a2713aSLionel Sambuc dyn_cast<VarTemplateSpecializationDecl>(D)) {
69f4a2713aSLionel Sambuc // We're done when we hit an explicit specialization.
70f4a2713aSLionel Sambuc if (Spec->getSpecializationKind() == TSK_ExplicitSpecialization &&
71f4a2713aSLionel Sambuc !isa<VarTemplatePartialSpecializationDecl>(Spec))
72f4a2713aSLionel Sambuc return Result;
73f4a2713aSLionel Sambuc
74f4a2713aSLionel Sambuc Result.addOuterTemplateArguments(&Spec->getTemplateInstantiationArgs());
75f4a2713aSLionel Sambuc
76f4a2713aSLionel Sambuc // If this variable template specialization was instantiated from a
77f4a2713aSLionel Sambuc // specialized member that is a variable template, we're done.
78f4a2713aSLionel Sambuc assert(Spec->getSpecializedTemplate() && "No variable template?");
79*0a6a1f1dSLionel Sambuc llvm::PointerUnion<VarTemplateDecl*,
80*0a6a1f1dSLionel Sambuc VarTemplatePartialSpecializationDecl*> Specialized
81*0a6a1f1dSLionel Sambuc = Spec->getSpecializedTemplateOrPartial();
82*0a6a1f1dSLionel Sambuc if (VarTemplatePartialSpecializationDecl *Partial =
83*0a6a1f1dSLionel Sambuc Specialized.dyn_cast<VarTemplatePartialSpecializationDecl *>()) {
84*0a6a1f1dSLionel Sambuc if (Partial->isMemberSpecialization())
85f4a2713aSLionel Sambuc return Result;
86*0a6a1f1dSLionel Sambuc } else {
87*0a6a1f1dSLionel Sambuc VarTemplateDecl *Tmpl = Specialized.get<VarTemplateDecl *>();
88*0a6a1f1dSLionel Sambuc if (Tmpl->isMemberSpecialization())
89*0a6a1f1dSLionel Sambuc return Result;
90*0a6a1f1dSLionel Sambuc }
91f4a2713aSLionel Sambuc }
92f4a2713aSLionel Sambuc
93f4a2713aSLionel Sambuc // If we have a template template parameter with translation unit context,
94f4a2713aSLionel Sambuc // then we're performing substitution into a default template argument of
95f4a2713aSLionel Sambuc // this template template parameter before we've constructed the template
96f4a2713aSLionel Sambuc // that will own this template template parameter. In this case, we
97f4a2713aSLionel Sambuc // use empty template parameter lists for all of the outer templates
98f4a2713aSLionel Sambuc // to avoid performing any substitutions.
99f4a2713aSLionel Sambuc if (Ctx->isTranslationUnit()) {
100f4a2713aSLionel Sambuc if (TemplateTemplateParmDecl *TTP
101f4a2713aSLionel Sambuc = dyn_cast<TemplateTemplateParmDecl>(D)) {
102f4a2713aSLionel Sambuc for (unsigned I = 0, N = TTP->getDepth() + 1; I != N; ++I)
103f4a2713aSLionel Sambuc Result.addOuterTemplateArguments(None);
104f4a2713aSLionel Sambuc return Result;
105f4a2713aSLionel Sambuc }
106f4a2713aSLionel Sambuc }
107f4a2713aSLionel Sambuc }
108f4a2713aSLionel Sambuc
109f4a2713aSLionel Sambuc while (!Ctx->isFileContext()) {
110f4a2713aSLionel Sambuc // Add template arguments from a class template instantiation.
111f4a2713aSLionel Sambuc if (ClassTemplateSpecializationDecl *Spec
112f4a2713aSLionel Sambuc = dyn_cast<ClassTemplateSpecializationDecl>(Ctx)) {
113f4a2713aSLionel Sambuc // We're done when we hit an explicit specialization.
114f4a2713aSLionel Sambuc if (Spec->getSpecializationKind() == TSK_ExplicitSpecialization &&
115f4a2713aSLionel Sambuc !isa<ClassTemplatePartialSpecializationDecl>(Spec))
116f4a2713aSLionel Sambuc break;
117f4a2713aSLionel Sambuc
118f4a2713aSLionel Sambuc Result.addOuterTemplateArguments(&Spec->getTemplateInstantiationArgs());
119f4a2713aSLionel Sambuc
120f4a2713aSLionel Sambuc // If this class template specialization was instantiated from a
121f4a2713aSLionel Sambuc // specialized member that is a class template, we're done.
122f4a2713aSLionel Sambuc assert(Spec->getSpecializedTemplate() && "No class template?");
123f4a2713aSLionel Sambuc if (Spec->getSpecializedTemplate()->isMemberSpecialization())
124f4a2713aSLionel Sambuc break;
125f4a2713aSLionel Sambuc }
126f4a2713aSLionel Sambuc // Add template arguments from a function template specialization.
127f4a2713aSLionel Sambuc else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Ctx)) {
128f4a2713aSLionel Sambuc if (!RelativeToPrimary &&
129f4a2713aSLionel Sambuc (Function->getTemplateSpecializationKind() ==
130f4a2713aSLionel Sambuc TSK_ExplicitSpecialization &&
131f4a2713aSLionel Sambuc !Function->getClassScopeSpecializationPattern()))
132f4a2713aSLionel Sambuc break;
133f4a2713aSLionel Sambuc
134f4a2713aSLionel Sambuc if (const TemplateArgumentList *TemplateArgs
135f4a2713aSLionel Sambuc = Function->getTemplateSpecializationArgs()) {
136f4a2713aSLionel Sambuc // Add the template arguments for this specialization.
137f4a2713aSLionel Sambuc Result.addOuterTemplateArguments(TemplateArgs);
138f4a2713aSLionel Sambuc
139f4a2713aSLionel Sambuc // If this function was instantiated from a specialized member that is
140f4a2713aSLionel Sambuc // a function template, we're done.
141f4a2713aSLionel Sambuc assert(Function->getPrimaryTemplate() && "No function template?");
142f4a2713aSLionel Sambuc if (Function->getPrimaryTemplate()->isMemberSpecialization())
143f4a2713aSLionel Sambuc break;
144f4a2713aSLionel Sambuc
145f4a2713aSLionel Sambuc // If this function is a generic lambda specialization, we are done.
146f4a2713aSLionel Sambuc if (isGenericLambdaCallOperatorSpecialization(Function))
147f4a2713aSLionel Sambuc break;
148f4a2713aSLionel Sambuc
149f4a2713aSLionel Sambuc } else if (FunctionTemplateDecl *FunTmpl
150f4a2713aSLionel Sambuc = Function->getDescribedFunctionTemplate()) {
151f4a2713aSLionel Sambuc // Add the "injected" template arguments.
152f4a2713aSLionel Sambuc Result.addOuterTemplateArguments(FunTmpl->getInjectedTemplateArgs());
153f4a2713aSLionel Sambuc }
154f4a2713aSLionel Sambuc
155f4a2713aSLionel Sambuc // If this is a friend declaration and it declares an entity at
156f4a2713aSLionel Sambuc // namespace scope, take arguments from its lexical parent
157f4a2713aSLionel Sambuc // instead of its semantic parent, unless of course the pattern we're
158f4a2713aSLionel Sambuc // instantiating actually comes from the file's context!
159f4a2713aSLionel Sambuc if (Function->getFriendObjectKind() &&
160f4a2713aSLionel Sambuc Function->getDeclContext()->isFileContext() &&
161f4a2713aSLionel Sambuc (!Pattern || !Pattern->getLexicalDeclContext()->isFileContext())) {
162f4a2713aSLionel Sambuc Ctx = Function->getLexicalDeclContext();
163f4a2713aSLionel Sambuc RelativeToPrimary = false;
164f4a2713aSLionel Sambuc continue;
165f4a2713aSLionel Sambuc }
166f4a2713aSLionel Sambuc } else if (CXXRecordDecl *Rec = dyn_cast<CXXRecordDecl>(Ctx)) {
167f4a2713aSLionel Sambuc if (ClassTemplateDecl *ClassTemplate = Rec->getDescribedClassTemplate()) {
168f4a2713aSLionel Sambuc QualType T = ClassTemplate->getInjectedClassNameSpecialization();
169f4a2713aSLionel Sambuc const TemplateSpecializationType *TST =
170f4a2713aSLionel Sambuc cast<TemplateSpecializationType>(Context.getCanonicalType(T));
171f4a2713aSLionel Sambuc Result.addOuterTemplateArguments(
172f4a2713aSLionel Sambuc llvm::makeArrayRef(TST->getArgs(), TST->getNumArgs()));
173f4a2713aSLionel Sambuc if (ClassTemplate->isMemberSpecialization())
174f4a2713aSLionel Sambuc break;
175f4a2713aSLionel Sambuc }
176f4a2713aSLionel Sambuc }
177f4a2713aSLionel Sambuc
178f4a2713aSLionel Sambuc Ctx = Ctx->getParent();
179f4a2713aSLionel Sambuc RelativeToPrimary = false;
180f4a2713aSLionel Sambuc }
181f4a2713aSLionel Sambuc
182f4a2713aSLionel Sambuc return Result;
183f4a2713aSLionel Sambuc }
184f4a2713aSLionel Sambuc
isInstantiationRecord() const185f4a2713aSLionel Sambuc bool Sema::ActiveTemplateInstantiation::isInstantiationRecord() const {
186f4a2713aSLionel Sambuc switch (Kind) {
187f4a2713aSLionel Sambuc case TemplateInstantiation:
188f4a2713aSLionel Sambuc case ExceptionSpecInstantiation:
189f4a2713aSLionel Sambuc case DefaultTemplateArgumentInstantiation:
190f4a2713aSLionel Sambuc case DefaultFunctionArgumentInstantiation:
191f4a2713aSLionel Sambuc case ExplicitTemplateArgumentSubstitution:
192f4a2713aSLionel Sambuc case DeducedTemplateArgumentSubstitution:
193f4a2713aSLionel Sambuc case PriorTemplateArgumentSubstitution:
194f4a2713aSLionel Sambuc return true;
195f4a2713aSLionel Sambuc
196f4a2713aSLionel Sambuc case DefaultTemplateArgumentChecking:
197f4a2713aSLionel Sambuc return false;
198f4a2713aSLionel Sambuc }
199f4a2713aSLionel Sambuc
200f4a2713aSLionel Sambuc llvm_unreachable("Invalid InstantiationKind!");
201f4a2713aSLionel Sambuc }
202f4a2713aSLionel Sambuc
Initialize(ActiveTemplateInstantiation::InstantiationKind Kind,SourceLocation PointOfInstantiation,SourceRange InstantiationRange,Decl * Entity,NamedDecl * Template,ArrayRef<TemplateArgument> TemplateArgs,sema::TemplateDeductionInfo * DeductionInfo)203*0a6a1f1dSLionel Sambuc void Sema::InstantiatingTemplate::Initialize(
204*0a6a1f1dSLionel Sambuc ActiveTemplateInstantiation::InstantiationKind Kind,
205*0a6a1f1dSLionel Sambuc SourceLocation PointOfInstantiation, SourceRange InstantiationRange,
206*0a6a1f1dSLionel Sambuc Decl *Entity, NamedDecl *Template, ArrayRef<TemplateArgument> TemplateArgs,
207*0a6a1f1dSLionel Sambuc sema::TemplateDeductionInfo *DeductionInfo) {
208*0a6a1f1dSLionel Sambuc SavedInNonInstantiationSFINAEContext =
209*0a6a1f1dSLionel Sambuc SemaRef.InNonInstantiationSFINAEContext;
210*0a6a1f1dSLionel Sambuc Invalid = CheckInstantiationDepth(PointOfInstantiation, InstantiationRange);
211*0a6a1f1dSLionel Sambuc if (!Invalid) {
212*0a6a1f1dSLionel Sambuc ActiveTemplateInstantiation Inst;
213*0a6a1f1dSLionel Sambuc Inst.Kind = Kind;
214*0a6a1f1dSLionel Sambuc Inst.PointOfInstantiation = PointOfInstantiation;
215*0a6a1f1dSLionel Sambuc Inst.Entity = Entity;
216*0a6a1f1dSLionel Sambuc Inst.Template = Template;
217*0a6a1f1dSLionel Sambuc Inst.TemplateArgs = TemplateArgs.data();
218*0a6a1f1dSLionel Sambuc Inst.NumTemplateArgs = TemplateArgs.size();
219*0a6a1f1dSLionel Sambuc Inst.DeductionInfo = DeductionInfo;
220*0a6a1f1dSLionel Sambuc Inst.InstantiationRange = InstantiationRange;
221*0a6a1f1dSLionel Sambuc SemaRef.InNonInstantiationSFINAEContext = false;
222*0a6a1f1dSLionel Sambuc SemaRef.ActiveTemplateInstantiations.push_back(Inst);
223*0a6a1f1dSLionel Sambuc if (!Inst.isInstantiationRecord())
224*0a6a1f1dSLionel Sambuc ++SemaRef.NonInstantiationEntries;
225*0a6a1f1dSLionel Sambuc }
226*0a6a1f1dSLionel Sambuc }
227*0a6a1f1dSLionel Sambuc
228f4a2713aSLionel Sambuc Sema::InstantiatingTemplate::
InstantiatingTemplate(Sema & SemaRef,SourceLocation PointOfInstantiation,Decl * Entity,SourceRange InstantiationRange)229f4a2713aSLionel Sambuc InstantiatingTemplate(Sema &SemaRef, SourceLocation PointOfInstantiation,
230f4a2713aSLionel Sambuc Decl *Entity,
231f4a2713aSLionel Sambuc SourceRange InstantiationRange)
232*0a6a1f1dSLionel Sambuc : SemaRef(SemaRef)
233f4a2713aSLionel Sambuc {
234*0a6a1f1dSLionel Sambuc Initialize(ActiveTemplateInstantiation::TemplateInstantiation,
235*0a6a1f1dSLionel Sambuc PointOfInstantiation, InstantiationRange, Entity);
236f4a2713aSLionel Sambuc }
237f4a2713aSLionel Sambuc
238f4a2713aSLionel Sambuc Sema::InstantiatingTemplate::
InstantiatingTemplate(Sema & SemaRef,SourceLocation PointOfInstantiation,FunctionDecl * Entity,ExceptionSpecification,SourceRange InstantiationRange)239f4a2713aSLionel Sambuc InstantiatingTemplate(Sema &SemaRef, SourceLocation PointOfInstantiation,
240f4a2713aSLionel Sambuc FunctionDecl *Entity, ExceptionSpecification,
241f4a2713aSLionel Sambuc SourceRange InstantiationRange)
242*0a6a1f1dSLionel Sambuc : SemaRef(SemaRef)
243f4a2713aSLionel Sambuc {
244*0a6a1f1dSLionel Sambuc Initialize(ActiveTemplateInstantiation::ExceptionSpecInstantiation,
245*0a6a1f1dSLionel Sambuc PointOfInstantiation, InstantiationRange, Entity);
246f4a2713aSLionel Sambuc }
247f4a2713aSLionel Sambuc
248f4a2713aSLionel Sambuc Sema::InstantiatingTemplate::
InstantiatingTemplate(Sema & SemaRef,SourceLocation PointOfInstantiation,TemplateDecl * Template,ArrayRef<TemplateArgument> TemplateArgs,SourceRange InstantiationRange)249f4a2713aSLionel Sambuc InstantiatingTemplate(Sema &SemaRef, SourceLocation PointOfInstantiation,
250f4a2713aSLionel Sambuc TemplateDecl *Template,
251f4a2713aSLionel Sambuc ArrayRef<TemplateArgument> TemplateArgs,
252f4a2713aSLionel Sambuc SourceRange InstantiationRange)
253*0a6a1f1dSLionel Sambuc : SemaRef(SemaRef)
254f4a2713aSLionel Sambuc {
255*0a6a1f1dSLionel Sambuc Initialize(ActiveTemplateInstantiation::DefaultTemplateArgumentInstantiation,
256*0a6a1f1dSLionel Sambuc PointOfInstantiation, InstantiationRange,
257*0a6a1f1dSLionel Sambuc Template, nullptr, TemplateArgs);
258f4a2713aSLionel Sambuc }
259f4a2713aSLionel Sambuc
260f4a2713aSLionel Sambuc Sema::InstantiatingTemplate::
InstantiatingTemplate(Sema & SemaRef,SourceLocation PointOfInstantiation,FunctionTemplateDecl * FunctionTemplate,ArrayRef<TemplateArgument> TemplateArgs,ActiveTemplateInstantiation::InstantiationKind Kind,sema::TemplateDeductionInfo & DeductionInfo,SourceRange InstantiationRange)261f4a2713aSLionel Sambuc InstantiatingTemplate(Sema &SemaRef, SourceLocation PointOfInstantiation,
262f4a2713aSLionel Sambuc FunctionTemplateDecl *FunctionTemplate,
263f4a2713aSLionel Sambuc ArrayRef<TemplateArgument> TemplateArgs,
264f4a2713aSLionel Sambuc ActiveTemplateInstantiation::InstantiationKind Kind,
265f4a2713aSLionel Sambuc sema::TemplateDeductionInfo &DeductionInfo,
266f4a2713aSLionel Sambuc SourceRange InstantiationRange)
267*0a6a1f1dSLionel Sambuc : SemaRef(SemaRef)
268f4a2713aSLionel Sambuc {
269*0a6a1f1dSLionel Sambuc Initialize(Kind, PointOfInstantiation, InstantiationRange,
270*0a6a1f1dSLionel Sambuc FunctionTemplate, nullptr, TemplateArgs, &DeductionInfo);
271f4a2713aSLionel Sambuc }
272f4a2713aSLionel Sambuc
273f4a2713aSLionel Sambuc Sema::InstantiatingTemplate::
InstantiatingTemplate(Sema & SemaRef,SourceLocation PointOfInstantiation,ClassTemplatePartialSpecializationDecl * PartialSpec,ArrayRef<TemplateArgument> TemplateArgs,sema::TemplateDeductionInfo & DeductionInfo,SourceRange InstantiationRange)274f4a2713aSLionel Sambuc InstantiatingTemplate(Sema &SemaRef, SourceLocation PointOfInstantiation,
275f4a2713aSLionel Sambuc ClassTemplatePartialSpecializationDecl *PartialSpec,
276f4a2713aSLionel Sambuc ArrayRef<TemplateArgument> TemplateArgs,
277f4a2713aSLionel Sambuc sema::TemplateDeductionInfo &DeductionInfo,
278f4a2713aSLionel Sambuc SourceRange InstantiationRange)
279*0a6a1f1dSLionel Sambuc : SemaRef(SemaRef)
280f4a2713aSLionel Sambuc {
281*0a6a1f1dSLionel Sambuc Initialize(ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution,
282*0a6a1f1dSLionel Sambuc PointOfInstantiation, InstantiationRange,
283*0a6a1f1dSLionel Sambuc PartialSpec, nullptr, TemplateArgs, &DeductionInfo);
284f4a2713aSLionel Sambuc }
285f4a2713aSLionel Sambuc
InstantiatingTemplate(Sema & SemaRef,SourceLocation PointOfInstantiation,VarTemplatePartialSpecializationDecl * PartialSpec,ArrayRef<TemplateArgument> TemplateArgs,sema::TemplateDeductionInfo & DeductionInfo,SourceRange InstantiationRange)286f4a2713aSLionel Sambuc Sema::InstantiatingTemplate::InstantiatingTemplate(
287f4a2713aSLionel Sambuc Sema &SemaRef, SourceLocation PointOfInstantiation,
288f4a2713aSLionel Sambuc VarTemplatePartialSpecializationDecl *PartialSpec,
289f4a2713aSLionel Sambuc ArrayRef<TemplateArgument> TemplateArgs,
290f4a2713aSLionel Sambuc sema::TemplateDeductionInfo &DeductionInfo, SourceRange InstantiationRange)
291*0a6a1f1dSLionel Sambuc : SemaRef(SemaRef)
292*0a6a1f1dSLionel Sambuc {
293*0a6a1f1dSLionel Sambuc Initialize(ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution,
294*0a6a1f1dSLionel Sambuc PointOfInstantiation, InstantiationRange,
295*0a6a1f1dSLionel Sambuc PartialSpec, nullptr, TemplateArgs, &DeductionInfo);
296f4a2713aSLionel Sambuc }
297f4a2713aSLionel Sambuc
298f4a2713aSLionel Sambuc Sema::InstantiatingTemplate::
InstantiatingTemplate(Sema & SemaRef,SourceLocation PointOfInstantiation,ParmVarDecl * Param,ArrayRef<TemplateArgument> TemplateArgs,SourceRange InstantiationRange)299f4a2713aSLionel Sambuc InstantiatingTemplate(Sema &SemaRef, SourceLocation PointOfInstantiation,
300f4a2713aSLionel Sambuc ParmVarDecl *Param,
301f4a2713aSLionel Sambuc ArrayRef<TemplateArgument> TemplateArgs,
302f4a2713aSLionel Sambuc SourceRange InstantiationRange)
303*0a6a1f1dSLionel Sambuc : SemaRef(SemaRef)
304f4a2713aSLionel Sambuc {
305*0a6a1f1dSLionel Sambuc Initialize(ActiveTemplateInstantiation::DefaultFunctionArgumentInstantiation,
306*0a6a1f1dSLionel Sambuc PointOfInstantiation, InstantiationRange,
307*0a6a1f1dSLionel Sambuc Param, nullptr, TemplateArgs);
308f4a2713aSLionel Sambuc }
309*0a6a1f1dSLionel Sambuc
310f4a2713aSLionel Sambuc
311f4a2713aSLionel Sambuc Sema::InstantiatingTemplate::
InstantiatingTemplate(Sema & SemaRef,SourceLocation PointOfInstantiation,NamedDecl * Template,NonTypeTemplateParmDecl * Param,ArrayRef<TemplateArgument> TemplateArgs,SourceRange InstantiationRange)312f4a2713aSLionel Sambuc InstantiatingTemplate(Sema &SemaRef, SourceLocation PointOfInstantiation,
313f4a2713aSLionel Sambuc NamedDecl *Template, NonTypeTemplateParmDecl *Param,
314f4a2713aSLionel Sambuc ArrayRef<TemplateArgument> TemplateArgs,
315f4a2713aSLionel Sambuc SourceRange InstantiationRange)
316*0a6a1f1dSLionel Sambuc : SemaRef(SemaRef)
317f4a2713aSLionel Sambuc {
318*0a6a1f1dSLionel Sambuc Initialize(ActiveTemplateInstantiation::PriorTemplateArgumentSubstitution,
319*0a6a1f1dSLionel Sambuc PointOfInstantiation, InstantiationRange,
320*0a6a1f1dSLionel Sambuc Param, Template, TemplateArgs);
321f4a2713aSLionel Sambuc }
322f4a2713aSLionel Sambuc
323f4a2713aSLionel Sambuc Sema::InstantiatingTemplate::
InstantiatingTemplate(Sema & SemaRef,SourceLocation PointOfInstantiation,NamedDecl * Template,TemplateTemplateParmDecl * Param,ArrayRef<TemplateArgument> TemplateArgs,SourceRange InstantiationRange)324f4a2713aSLionel Sambuc InstantiatingTemplate(Sema &SemaRef, SourceLocation PointOfInstantiation,
325f4a2713aSLionel Sambuc NamedDecl *Template, TemplateTemplateParmDecl *Param,
326f4a2713aSLionel Sambuc ArrayRef<TemplateArgument> TemplateArgs,
327f4a2713aSLionel Sambuc SourceRange InstantiationRange)
328*0a6a1f1dSLionel Sambuc : SemaRef(SemaRef)
329f4a2713aSLionel Sambuc {
330*0a6a1f1dSLionel Sambuc Initialize(ActiveTemplateInstantiation::PriorTemplateArgumentSubstitution,
331*0a6a1f1dSLionel Sambuc PointOfInstantiation, InstantiationRange,
332*0a6a1f1dSLionel Sambuc Param, Template, TemplateArgs);
333f4a2713aSLionel Sambuc }
334f4a2713aSLionel Sambuc
335f4a2713aSLionel Sambuc Sema::InstantiatingTemplate::
InstantiatingTemplate(Sema & SemaRef,SourceLocation PointOfInstantiation,TemplateDecl * Template,NamedDecl * Param,ArrayRef<TemplateArgument> TemplateArgs,SourceRange InstantiationRange)336f4a2713aSLionel Sambuc InstantiatingTemplate(Sema &SemaRef, SourceLocation PointOfInstantiation,
337f4a2713aSLionel Sambuc TemplateDecl *Template, NamedDecl *Param,
338f4a2713aSLionel Sambuc ArrayRef<TemplateArgument> TemplateArgs,
339f4a2713aSLionel Sambuc SourceRange InstantiationRange)
340*0a6a1f1dSLionel Sambuc : SemaRef(SemaRef)
341f4a2713aSLionel Sambuc {
342*0a6a1f1dSLionel Sambuc Initialize(ActiveTemplateInstantiation::DefaultTemplateArgumentChecking,
343*0a6a1f1dSLionel Sambuc PointOfInstantiation, InstantiationRange,
344*0a6a1f1dSLionel Sambuc Param, Template, TemplateArgs);
345f4a2713aSLionel Sambuc }
346f4a2713aSLionel Sambuc
Clear()347f4a2713aSLionel Sambuc void Sema::InstantiatingTemplate::Clear() {
348f4a2713aSLionel Sambuc if (!Invalid) {
349f4a2713aSLionel Sambuc if (!SemaRef.ActiveTemplateInstantiations.back().isInstantiationRecord()) {
350f4a2713aSLionel Sambuc assert(SemaRef.NonInstantiationEntries > 0);
351f4a2713aSLionel Sambuc --SemaRef.NonInstantiationEntries;
352f4a2713aSLionel Sambuc }
353f4a2713aSLionel Sambuc SemaRef.InNonInstantiationSFINAEContext
354f4a2713aSLionel Sambuc = SavedInNonInstantiationSFINAEContext;
355f4a2713aSLionel Sambuc
356f4a2713aSLionel Sambuc // Name lookup no longer looks in this template's defining module.
357f4a2713aSLionel Sambuc assert(SemaRef.ActiveTemplateInstantiations.size() >=
358f4a2713aSLionel Sambuc SemaRef.ActiveTemplateInstantiationLookupModules.size() &&
359f4a2713aSLionel Sambuc "forgot to remove a lookup module for a template instantiation");
360f4a2713aSLionel Sambuc if (SemaRef.ActiveTemplateInstantiations.size() ==
361f4a2713aSLionel Sambuc SemaRef.ActiveTemplateInstantiationLookupModules.size()) {
362f4a2713aSLionel Sambuc if (Module *M = SemaRef.ActiveTemplateInstantiationLookupModules.back())
363f4a2713aSLionel Sambuc SemaRef.LookupModulesCache.erase(M);
364f4a2713aSLionel Sambuc SemaRef.ActiveTemplateInstantiationLookupModules.pop_back();
365f4a2713aSLionel Sambuc }
366f4a2713aSLionel Sambuc
367f4a2713aSLionel Sambuc SemaRef.ActiveTemplateInstantiations.pop_back();
368f4a2713aSLionel Sambuc Invalid = true;
369f4a2713aSLionel Sambuc }
370f4a2713aSLionel Sambuc }
371f4a2713aSLionel Sambuc
CheckInstantiationDepth(SourceLocation PointOfInstantiation,SourceRange InstantiationRange)372f4a2713aSLionel Sambuc bool Sema::InstantiatingTemplate::CheckInstantiationDepth(
373f4a2713aSLionel Sambuc SourceLocation PointOfInstantiation,
374f4a2713aSLionel Sambuc SourceRange InstantiationRange) {
375f4a2713aSLionel Sambuc assert(SemaRef.NonInstantiationEntries <=
376f4a2713aSLionel Sambuc SemaRef.ActiveTemplateInstantiations.size());
377f4a2713aSLionel Sambuc if ((SemaRef.ActiveTemplateInstantiations.size() -
378f4a2713aSLionel Sambuc SemaRef.NonInstantiationEntries)
379f4a2713aSLionel Sambuc <= SemaRef.getLangOpts().InstantiationDepth)
380f4a2713aSLionel Sambuc return false;
381f4a2713aSLionel Sambuc
382f4a2713aSLionel Sambuc SemaRef.Diag(PointOfInstantiation,
383f4a2713aSLionel Sambuc diag::err_template_recursion_depth_exceeded)
384f4a2713aSLionel Sambuc << SemaRef.getLangOpts().InstantiationDepth
385f4a2713aSLionel Sambuc << InstantiationRange;
386f4a2713aSLionel Sambuc SemaRef.Diag(PointOfInstantiation, diag::note_template_recursion_depth)
387f4a2713aSLionel Sambuc << SemaRef.getLangOpts().InstantiationDepth;
388f4a2713aSLionel Sambuc return true;
389f4a2713aSLionel Sambuc }
390f4a2713aSLionel Sambuc
391f4a2713aSLionel Sambuc /// \brief Prints the current instantiation stack through a series of
392f4a2713aSLionel Sambuc /// notes.
PrintInstantiationStack()393f4a2713aSLionel Sambuc void Sema::PrintInstantiationStack() {
394f4a2713aSLionel Sambuc // Determine which template instantiations to skip, if any.
395f4a2713aSLionel Sambuc unsigned SkipStart = ActiveTemplateInstantiations.size(), SkipEnd = SkipStart;
396f4a2713aSLionel Sambuc unsigned Limit = Diags.getTemplateBacktraceLimit();
397f4a2713aSLionel Sambuc if (Limit && Limit < ActiveTemplateInstantiations.size()) {
398f4a2713aSLionel Sambuc SkipStart = Limit / 2 + Limit % 2;
399f4a2713aSLionel Sambuc SkipEnd = ActiveTemplateInstantiations.size() - Limit / 2;
400f4a2713aSLionel Sambuc }
401f4a2713aSLionel Sambuc
402f4a2713aSLionel Sambuc // FIXME: In all of these cases, we need to show the template arguments
403f4a2713aSLionel Sambuc unsigned InstantiationIdx = 0;
404f4a2713aSLionel Sambuc for (SmallVectorImpl<ActiveTemplateInstantiation>::reverse_iterator
405f4a2713aSLionel Sambuc Active = ActiveTemplateInstantiations.rbegin(),
406f4a2713aSLionel Sambuc ActiveEnd = ActiveTemplateInstantiations.rend();
407f4a2713aSLionel Sambuc Active != ActiveEnd;
408f4a2713aSLionel Sambuc ++Active, ++InstantiationIdx) {
409f4a2713aSLionel Sambuc // Skip this instantiation?
410f4a2713aSLionel Sambuc if (InstantiationIdx >= SkipStart && InstantiationIdx < SkipEnd) {
411f4a2713aSLionel Sambuc if (InstantiationIdx == SkipStart) {
412f4a2713aSLionel Sambuc // Note that we're skipping instantiations.
413f4a2713aSLionel Sambuc Diags.Report(Active->PointOfInstantiation,
414f4a2713aSLionel Sambuc diag::note_instantiation_contexts_suppressed)
415f4a2713aSLionel Sambuc << unsigned(ActiveTemplateInstantiations.size() - Limit);
416f4a2713aSLionel Sambuc }
417f4a2713aSLionel Sambuc continue;
418f4a2713aSLionel Sambuc }
419f4a2713aSLionel Sambuc
420f4a2713aSLionel Sambuc switch (Active->Kind) {
421f4a2713aSLionel Sambuc case ActiveTemplateInstantiation::TemplateInstantiation: {
422f4a2713aSLionel Sambuc Decl *D = Active->Entity;
423f4a2713aSLionel Sambuc if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) {
424f4a2713aSLionel Sambuc unsigned DiagID = diag::note_template_member_class_here;
425f4a2713aSLionel Sambuc if (isa<ClassTemplateSpecializationDecl>(Record))
426f4a2713aSLionel Sambuc DiagID = diag::note_template_class_instantiation_here;
427f4a2713aSLionel Sambuc Diags.Report(Active->PointOfInstantiation, DiagID)
428f4a2713aSLionel Sambuc << Context.getTypeDeclType(Record)
429f4a2713aSLionel Sambuc << Active->InstantiationRange;
430f4a2713aSLionel Sambuc } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
431f4a2713aSLionel Sambuc unsigned DiagID;
432f4a2713aSLionel Sambuc if (Function->getPrimaryTemplate())
433f4a2713aSLionel Sambuc DiagID = diag::note_function_template_spec_here;
434f4a2713aSLionel Sambuc else
435f4a2713aSLionel Sambuc DiagID = diag::note_template_member_function_here;
436f4a2713aSLionel Sambuc Diags.Report(Active->PointOfInstantiation, DiagID)
437f4a2713aSLionel Sambuc << Function
438f4a2713aSLionel Sambuc << Active->InstantiationRange;
439f4a2713aSLionel Sambuc } else if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
440f4a2713aSLionel Sambuc Diags.Report(Active->PointOfInstantiation,
441f4a2713aSLionel Sambuc VD->isStaticDataMember()?
442f4a2713aSLionel Sambuc diag::note_template_static_data_member_def_here
443f4a2713aSLionel Sambuc : diag::note_template_variable_def_here)
444f4a2713aSLionel Sambuc << VD
445f4a2713aSLionel Sambuc << Active->InstantiationRange;
446f4a2713aSLionel Sambuc } else if (EnumDecl *ED = dyn_cast<EnumDecl>(D)) {
447f4a2713aSLionel Sambuc Diags.Report(Active->PointOfInstantiation,
448f4a2713aSLionel Sambuc diag::note_template_enum_def_here)
449f4a2713aSLionel Sambuc << ED
450f4a2713aSLionel Sambuc << Active->InstantiationRange;
451*0a6a1f1dSLionel Sambuc } else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
452*0a6a1f1dSLionel Sambuc Diags.Report(Active->PointOfInstantiation,
453*0a6a1f1dSLionel Sambuc diag::note_template_nsdmi_here)
454*0a6a1f1dSLionel Sambuc << FD << Active->InstantiationRange;
455f4a2713aSLionel Sambuc } else {
456f4a2713aSLionel Sambuc Diags.Report(Active->PointOfInstantiation,
457f4a2713aSLionel Sambuc diag::note_template_type_alias_instantiation_here)
458f4a2713aSLionel Sambuc << cast<TypeAliasTemplateDecl>(D)
459f4a2713aSLionel Sambuc << Active->InstantiationRange;
460f4a2713aSLionel Sambuc }
461f4a2713aSLionel Sambuc break;
462f4a2713aSLionel Sambuc }
463f4a2713aSLionel Sambuc
464f4a2713aSLionel Sambuc case ActiveTemplateInstantiation::DefaultTemplateArgumentInstantiation: {
465f4a2713aSLionel Sambuc TemplateDecl *Template = cast<TemplateDecl>(Active->Entity);
466f4a2713aSLionel Sambuc SmallVector<char, 128> TemplateArgsStr;
467f4a2713aSLionel Sambuc llvm::raw_svector_ostream OS(TemplateArgsStr);
468f4a2713aSLionel Sambuc Template->printName(OS);
469f4a2713aSLionel Sambuc TemplateSpecializationType::PrintTemplateArgumentList(OS,
470f4a2713aSLionel Sambuc Active->TemplateArgs,
471f4a2713aSLionel Sambuc Active->NumTemplateArgs,
472f4a2713aSLionel Sambuc getPrintingPolicy());
473f4a2713aSLionel Sambuc Diags.Report(Active->PointOfInstantiation,
474f4a2713aSLionel Sambuc diag::note_default_arg_instantiation_here)
475f4a2713aSLionel Sambuc << OS.str()
476f4a2713aSLionel Sambuc << Active->InstantiationRange;
477f4a2713aSLionel Sambuc break;
478f4a2713aSLionel Sambuc }
479f4a2713aSLionel Sambuc
480f4a2713aSLionel Sambuc case ActiveTemplateInstantiation::ExplicitTemplateArgumentSubstitution: {
481f4a2713aSLionel Sambuc FunctionTemplateDecl *FnTmpl = cast<FunctionTemplateDecl>(Active->Entity);
482f4a2713aSLionel Sambuc Diags.Report(Active->PointOfInstantiation,
483f4a2713aSLionel Sambuc diag::note_explicit_template_arg_substitution_here)
484f4a2713aSLionel Sambuc << FnTmpl
485f4a2713aSLionel Sambuc << getTemplateArgumentBindingsText(FnTmpl->getTemplateParameters(),
486f4a2713aSLionel Sambuc Active->TemplateArgs,
487f4a2713aSLionel Sambuc Active->NumTemplateArgs)
488f4a2713aSLionel Sambuc << Active->InstantiationRange;
489f4a2713aSLionel Sambuc break;
490f4a2713aSLionel Sambuc }
491f4a2713aSLionel Sambuc
492f4a2713aSLionel Sambuc case ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution:
493f4a2713aSLionel Sambuc if (ClassTemplatePartialSpecializationDecl *PartialSpec =
494f4a2713aSLionel Sambuc dyn_cast<ClassTemplatePartialSpecializationDecl>(Active->Entity)) {
495f4a2713aSLionel Sambuc Diags.Report(Active->PointOfInstantiation,
496f4a2713aSLionel Sambuc diag::note_partial_spec_deduct_instantiation_here)
497f4a2713aSLionel Sambuc << Context.getTypeDeclType(PartialSpec)
498f4a2713aSLionel Sambuc << getTemplateArgumentBindingsText(
499f4a2713aSLionel Sambuc PartialSpec->getTemplateParameters(),
500f4a2713aSLionel Sambuc Active->TemplateArgs,
501f4a2713aSLionel Sambuc Active->NumTemplateArgs)
502f4a2713aSLionel Sambuc << Active->InstantiationRange;
503f4a2713aSLionel Sambuc } else {
504f4a2713aSLionel Sambuc FunctionTemplateDecl *FnTmpl
505f4a2713aSLionel Sambuc = cast<FunctionTemplateDecl>(Active->Entity);
506f4a2713aSLionel Sambuc Diags.Report(Active->PointOfInstantiation,
507f4a2713aSLionel Sambuc diag::note_function_template_deduction_instantiation_here)
508f4a2713aSLionel Sambuc << FnTmpl
509f4a2713aSLionel Sambuc << getTemplateArgumentBindingsText(FnTmpl->getTemplateParameters(),
510f4a2713aSLionel Sambuc Active->TemplateArgs,
511f4a2713aSLionel Sambuc Active->NumTemplateArgs)
512f4a2713aSLionel Sambuc << Active->InstantiationRange;
513f4a2713aSLionel Sambuc }
514f4a2713aSLionel Sambuc break;
515f4a2713aSLionel Sambuc
516f4a2713aSLionel Sambuc case ActiveTemplateInstantiation::DefaultFunctionArgumentInstantiation: {
517f4a2713aSLionel Sambuc ParmVarDecl *Param = cast<ParmVarDecl>(Active->Entity);
518f4a2713aSLionel Sambuc FunctionDecl *FD = cast<FunctionDecl>(Param->getDeclContext());
519f4a2713aSLionel Sambuc
520f4a2713aSLionel Sambuc SmallVector<char, 128> TemplateArgsStr;
521f4a2713aSLionel Sambuc llvm::raw_svector_ostream OS(TemplateArgsStr);
522f4a2713aSLionel Sambuc FD->printName(OS);
523f4a2713aSLionel Sambuc TemplateSpecializationType::PrintTemplateArgumentList(OS,
524f4a2713aSLionel Sambuc Active->TemplateArgs,
525f4a2713aSLionel Sambuc Active->NumTemplateArgs,
526f4a2713aSLionel Sambuc getPrintingPolicy());
527f4a2713aSLionel Sambuc Diags.Report(Active->PointOfInstantiation,
528f4a2713aSLionel Sambuc diag::note_default_function_arg_instantiation_here)
529f4a2713aSLionel Sambuc << OS.str()
530f4a2713aSLionel Sambuc << Active->InstantiationRange;
531f4a2713aSLionel Sambuc break;
532f4a2713aSLionel Sambuc }
533f4a2713aSLionel Sambuc
534f4a2713aSLionel Sambuc case ActiveTemplateInstantiation::PriorTemplateArgumentSubstitution: {
535f4a2713aSLionel Sambuc NamedDecl *Parm = cast<NamedDecl>(Active->Entity);
536f4a2713aSLionel Sambuc std::string Name;
537f4a2713aSLionel Sambuc if (!Parm->getName().empty())
538f4a2713aSLionel Sambuc Name = std::string(" '") + Parm->getName().str() + "'";
539f4a2713aSLionel Sambuc
540*0a6a1f1dSLionel Sambuc TemplateParameterList *TemplateParams = nullptr;
541f4a2713aSLionel Sambuc if (TemplateDecl *Template = dyn_cast<TemplateDecl>(Active->Template))
542f4a2713aSLionel Sambuc TemplateParams = Template->getTemplateParameters();
543f4a2713aSLionel Sambuc else
544f4a2713aSLionel Sambuc TemplateParams =
545f4a2713aSLionel Sambuc cast<ClassTemplatePartialSpecializationDecl>(Active->Template)
546f4a2713aSLionel Sambuc ->getTemplateParameters();
547f4a2713aSLionel Sambuc Diags.Report(Active->PointOfInstantiation,
548f4a2713aSLionel Sambuc diag::note_prior_template_arg_substitution)
549f4a2713aSLionel Sambuc << isa<TemplateTemplateParmDecl>(Parm)
550f4a2713aSLionel Sambuc << Name
551f4a2713aSLionel Sambuc << getTemplateArgumentBindingsText(TemplateParams,
552f4a2713aSLionel Sambuc Active->TemplateArgs,
553f4a2713aSLionel Sambuc Active->NumTemplateArgs)
554f4a2713aSLionel Sambuc << Active->InstantiationRange;
555f4a2713aSLionel Sambuc break;
556f4a2713aSLionel Sambuc }
557f4a2713aSLionel Sambuc
558f4a2713aSLionel Sambuc case ActiveTemplateInstantiation::DefaultTemplateArgumentChecking: {
559*0a6a1f1dSLionel Sambuc TemplateParameterList *TemplateParams = nullptr;
560f4a2713aSLionel Sambuc if (TemplateDecl *Template = dyn_cast<TemplateDecl>(Active->Template))
561f4a2713aSLionel Sambuc TemplateParams = Template->getTemplateParameters();
562f4a2713aSLionel Sambuc else
563f4a2713aSLionel Sambuc TemplateParams =
564f4a2713aSLionel Sambuc cast<ClassTemplatePartialSpecializationDecl>(Active->Template)
565f4a2713aSLionel Sambuc ->getTemplateParameters();
566f4a2713aSLionel Sambuc
567f4a2713aSLionel Sambuc Diags.Report(Active->PointOfInstantiation,
568f4a2713aSLionel Sambuc diag::note_template_default_arg_checking)
569f4a2713aSLionel Sambuc << getTemplateArgumentBindingsText(TemplateParams,
570f4a2713aSLionel Sambuc Active->TemplateArgs,
571f4a2713aSLionel Sambuc Active->NumTemplateArgs)
572f4a2713aSLionel Sambuc << Active->InstantiationRange;
573f4a2713aSLionel Sambuc break;
574f4a2713aSLionel Sambuc }
575f4a2713aSLionel Sambuc
576f4a2713aSLionel Sambuc case ActiveTemplateInstantiation::ExceptionSpecInstantiation:
577f4a2713aSLionel Sambuc Diags.Report(Active->PointOfInstantiation,
578f4a2713aSLionel Sambuc diag::note_template_exception_spec_instantiation_here)
579f4a2713aSLionel Sambuc << cast<FunctionDecl>(Active->Entity)
580f4a2713aSLionel Sambuc << Active->InstantiationRange;
581f4a2713aSLionel Sambuc break;
582f4a2713aSLionel Sambuc }
583f4a2713aSLionel Sambuc }
584f4a2713aSLionel Sambuc }
585f4a2713aSLionel Sambuc
isSFINAEContext() const586f4a2713aSLionel Sambuc Optional<TemplateDeductionInfo *> Sema::isSFINAEContext() const {
587f4a2713aSLionel Sambuc if (InNonInstantiationSFINAEContext)
588*0a6a1f1dSLionel Sambuc return Optional<TemplateDeductionInfo *>(nullptr);
589f4a2713aSLionel Sambuc
590f4a2713aSLionel Sambuc for (SmallVectorImpl<ActiveTemplateInstantiation>::const_reverse_iterator
591f4a2713aSLionel Sambuc Active = ActiveTemplateInstantiations.rbegin(),
592f4a2713aSLionel Sambuc ActiveEnd = ActiveTemplateInstantiations.rend();
593f4a2713aSLionel Sambuc Active != ActiveEnd;
594f4a2713aSLionel Sambuc ++Active)
595f4a2713aSLionel Sambuc {
596f4a2713aSLionel Sambuc switch(Active->Kind) {
597f4a2713aSLionel Sambuc case ActiveTemplateInstantiation::TemplateInstantiation:
598f4a2713aSLionel Sambuc // An instantiation of an alias template may or may not be a SFINAE
599f4a2713aSLionel Sambuc // context, depending on what else is on the stack.
600f4a2713aSLionel Sambuc if (isa<TypeAliasTemplateDecl>(Active->Entity))
601f4a2713aSLionel Sambuc break;
602f4a2713aSLionel Sambuc // Fall through.
603f4a2713aSLionel Sambuc case ActiveTemplateInstantiation::DefaultFunctionArgumentInstantiation:
604f4a2713aSLionel Sambuc case ActiveTemplateInstantiation::ExceptionSpecInstantiation:
605f4a2713aSLionel Sambuc // This is a template instantiation, so there is no SFINAE.
606f4a2713aSLionel Sambuc return None;
607f4a2713aSLionel Sambuc
608f4a2713aSLionel Sambuc case ActiveTemplateInstantiation::DefaultTemplateArgumentInstantiation:
609f4a2713aSLionel Sambuc case ActiveTemplateInstantiation::PriorTemplateArgumentSubstitution:
610f4a2713aSLionel Sambuc case ActiveTemplateInstantiation::DefaultTemplateArgumentChecking:
611f4a2713aSLionel Sambuc // A default template argument instantiation and substitution into
612f4a2713aSLionel Sambuc // template parameters with arguments for prior parameters may or may
613f4a2713aSLionel Sambuc // not be a SFINAE context; look further up the stack.
614f4a2713aSLionel Sambuc break;
615f4a2713aSLionel Sambuc
616f4a2713aSLionel Sambuc case ActiveTemplateInstantiation::ExplicitTemplateArgumentSubstitution:
617f4a2713aSLionel Sambuc case ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution:
618f4a2713aSLionel Sambuc // We're either substitution explicitly-specified template arguments
619f4a2713aSLionel Sambuc // or deduced template arguments, so SFINAE applies.
620f4a2713aSLionel Sambuc assert(Active->DeductionInfo && "Missing deduction info pointer");
621f4a2713aSLionel Sambuc return Active->DeductionInfo;
622f4a2713aSLionel Sambuc }
623f4a2713aSLionel Sambuc }
624f4a2713aSLionel Sambuc
625f4a2713aSLionel Sambuc return None;
626f4a2713aSLionel Sambuc }
627f4a2713aSLionel Sambuc
628f4a2713aSLionel Sambuc /// \brief Retrieve the depth and index of a parameter pack.
629f4a2713aSLionel Sambuc static std::pair<unsigned, unsigned>
getDepthAndIndex(NamedDecl * ND)630f4a2713aSLionel Sambuc getDepthAndIndex(NamedDecl *ND) {
631f4a2713aSLionel Sambuc if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(ND))
632f4a2713aSLionel Sambuc return std::make_pair(TTP->getDepth(), TTP->getIndex());
633f4a2713aSLionel Sambuc
634f4a2713aSLionel Sambuc if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(ND))
635f4a2713aSLionel Sambuc return std::make_pair(NTTP->getDepth(), NTTP->getIndex());
636f4a2713aSLionel Sambuc
637f4a2713aSLionel Sambuc TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(ND);
638f4a2713aSLionel Sambuc return std::make_pair(TTP->getDepth(), TTP->getIndex());
639f4a2713aSLionel Sambuc }
640f4a2713aSLionel Sambuc
641f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===/
642f4a2713aSLionel Sambuc // Template Instantiation for Types
643f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===/
644f4a2713aSLionel Sambuc namespace {
645f4a2713aSLionel Sambuc class TemplateInstantiator : public TreeTransform<TemplateInstantiator> {
646f4a2713aSLionel Sambuc const MultiLevelTemplateArgumentList &TemplateArgs;
647f4a2713aSLionel Sambuc SourceLocation Loc;
648f4a2713aSLionel Sambuc DeclarationName Entity;
649f4a2713aSLionel Sambuc
650f4a2713aSLionel Sambuc public:
651f4a2713aSLionel Sambuc typedef TreeTransform<TemplateInstantiator> inherited;
652f4a2713aSLionel Sambuc
TemplateInstantiator(Sema & SemaRef,const MultiLevelTemplateArgumentList & TemplateArgs,SourceLocation Loc,DeclarationName Entity)653f4a2713aSLionel Sambuc TemplateInstantiator(Sema &SemaRef,
654f4a2713aSLionel Sambuc const MultiLevelTemplateArgumentList &TemplateArgs,
655f4a2713aSLionel Sambuc SourceLocation Loc,
656f4a2713aSLionel Sambuc DeclarationName Entity)
657f4a2713aSLionel Sambuc : inherited(SemaRef), TemplateArgs(TemplateArgs), Loc(Loc),
658f4a2713aSLionel Sambuc Entity(Entity) { }
659f4a2713aSLionel Sambuc
660f4a2713aSLionel Sambuc /// \brief Determine whether the given type \p T has already been
661f4a2713aSLionel Sambuc /// transformed.
662f4a2713aSLionel Sambuc ///
663f4a2713aSLionel Sambuc /// For the purposes of template instantiation, a type has already been
664f4a2713aSLionel Sambuc /// transformed if it is NULL or if it is not dependent.
665f4a2713aSLionel Sambuc bool AlreadyTransformed(QualType T);
666f4a2713aSLionel Sambuc
667f4a2713aSLionel Sambuc /// \brief Returns the location of the entity being instantiated, if known.
getBaseLocation()668f4a2713aSLionel Sambuc SourceLocation getBaseLocation() { return Loc; }
669f4a2713aSLionel Sambuc
670f4a2713aSLionel Sambuc /// \brief Returns the name of the entity being instantiated, if any.
getBaseEntity()671f4a2713aSLionel Sambuc DeclarationName getBaseEntity() { return Entity; }
672f4a2713aSLionel Sambuc
673f4a2713aSLionel Sambuc /// \brief Sets the "base" location and entity when that
674f4a2713aSLionel Sambuc /// information is known based on another transformation.
setBase(SourceLocation Loc,DeclarationName Entity)675f4a2713aSLionel Sambuc void setBase(SourceLocation Loc, DeclarationName Entity) {
676f4a2713aSLionel Sambuc this->Loc = Loc;
677f4a2713aSLionel Sambuc this->Entity = Entity;
678f4a2713aSLionel Sambuc }
679f4a2713aSLionel Sambuc
TryExpandParameterPacks(SourceLocation EllipsisLoc,SourceRange PatternRange,ArrayRef<UnexpandedParameterPack> Unexpanded,bool & ShouldExpand,bool & RetainExpansion,Optional<unsigned> & NumExpansions)680f4a2713aSLionel Sambuc bool TryExpandParameterPacks(SourceLocation EllipsisLoc,
681f4a2713aSLionel Sambuc SourceRange PatternRange,
682f4a2713aSLionel Sambuc ArrayRef<UnexpandedParameterPack> Unexpanded,
683f4a2713aSLionel Sambuc bool &ShouldExpand, bool &RetainExpansion,
684f4a2713aSLionel Sambuc Optional<unsigned> &NumExpansions) {
685f4a2713aSLionel Sambuc return getSema().CheckParameterPacksForExpansion(EllipsisLoc,
686f4a2713aSLionel Sambuc PatternRange, Unexpanded,
687f4a2713aSLionel Sambuc TemplateArgs,
688f4a2713aSLionel Sambuc ShouldExpand,
689f4a2713aSLionel Sambuc RetainExpansion,
690f4a2713aSLionel Sambuc NumExpansions);
691f4a2713aSLionel Sambuc }
692f4a2713aSLionel Sambuc
ExpandingFunctionParameterPack(ParmVarDecl * Pack)693f4a2713aSLionel Sambuc void ExpandingFunctionParameterPack(ParmVarDecl *Pack) {
694f4a2713aSLionel Sambuc SemaRef.CurrentInstantiationScope->MakeInstantiatedLocalArgPack(Pack);
695f4a2713aSLionel Sambuc }
696f4a2713aSLionel Sambuc
ForgetPartiallySubstitutedPack()697f4a2713aSLionel Sambuc TemplateArgument ForgetPartiallySubstitutedPack() {
698f4a2713aSLionel Sambuc TemplateArgument Result;
699f4a2713aSLionel Sambuc if (NamedDecl *PartialPack
700f4a2713aSLionel Sambuc = SemaRef.CurrentInstantiationScope->getPartiallySubstitutedPack()){
701f4a2713aSLionel Sambuc MultiLevelTemplateArgumentList &TemplateArgs
702f4a2713aSLionel Sambuc = const_cast<MultiLevelTemplateArgumentList &>(this->TemplateArgs);
703f4a2713aSLionel Sambuc unsigned Depth, Index;
704*0a6a1f1dSLionel Sambuc std::tie(Depth, Index) = getDepthAndIndex(PartialPack);
705f4a2713aSLionel Sambuc if (TemplateArgs.hasTemplateArgument(Depth, Index)) {
706f4a2713aSLionel Sambuc Result = TemplateArgs(Depth, Index);
707f4a2713aSLionel Sambuc TemplateArgs.setArgument(Depth, Index, TemplateArgument());
708f4a2713aSLionel Sambuc }
709f4a2713aSLionel Sambuc }
710f4a2713aSLionel Sambuc
711f4a2713aSLionel Sambuc return Result;
712f4a2713aSLionel Sambuc }
713f4a2713aSLionel Sambuc
RememberPartiallySubstitutedPack(TemplateArgument Arg)714f4a2713aSLionel Sambuc void RememberPartiallySubstitutedPack(TemplateArgument Arg) {
715f4a2713aSLionel Sambuc if (Arg.isNull())
716f4a2713aSLionel Sambuc return;
717f4a2713aSLionel Sambuc
718f4a2713aSLionel Sambuc if (NamedDecl *PartialPack
719f4a2713aSLionel Sambuc = SemaRef.CurrentInstantiationScope->getPartiallySubstitutedPack()){
720f4a2713aSLionel Sambuc MultiLevelTemplateArgumentList &TemplateArgs
721f4a2713aSLionel Sambuc = const_cast<MultiLevelTemplateArgumentList &>(this->TemplateArgs);
722f4a2713aSLionel Sambuc unsigned Depth, Index;
723*0a6a1f1dSLionel Sambuc std::tie(Depth, Index) = getDepthAndIndex(PartialPack);
724f4a2713aSLionel Sambuc TemplateArgs.setArgument(Depth, Index, Arg);
725f4a2713aSLionel Sambuc }
726f4a2713aSLionel Sambuc }
727f4a2713aSLionel Sambuc
728f4a2713aSLionel Sambuc /// \brief Transform the given declaration by instantiating a reference to
729f4a2713aSLionel Sambuc /// this declaration.
730f4a2713aSLionel Sambuc Decl *TransformDecl(SourceLocation Loc, Decl *D);
731f4a2713aSLionel Sambuc
transformAttrs(Decl * Old,Decl * New)732f4a2713aSLionel Sambuc void transformAttrs(Decl *Old, Decl *New) {
733f4a2713aSLionel Sambuc SemaRef.InstantiateAttrs(TemplateArgs, Old, New);
734f4a2713aSLionel Sambuc }
735f4a2713aSLionel Sambuc
transformedLocalDecl(Decl * Old,Decl * New)736f4a2713aSLionel Sambuc void transformedLocalDecl(Decl *Old, Decl *New) {
737f4a2713aSLionel Sambuc SemaRef.CurrentInstantiationScope->InstantiatedLocal(Old, New);
738f4a2713aSLionel Sambuc }
739f4a2713aSLionel Sambuc
740f4a2713aSLionel Sambuc /// \brief Transform the definition of the given declaration by
741f4a2713aSLionel Sambuc /// instantiating it.
742f4a2713aSLionel Sambuc Decl *TransformDefinition(SourceLocation Loc, Decl *D);
743f4a2713aSLionel Sambuc
744f4a2713aSLionel Sambuc /// \brief Transform the first qualifier within a scope by instantiating the
745f4a2713aSLionel Sambuc /// declaration.
746f4a2713aSLionel Sambuc NamedDecl *TransformFirstQualifierInScope(NamedDecl *D, SourceLocation Loc);
747f4a2713aSLionel Sambuc
748f4a2713aSLionel Sambuc /// \brief Rebuild the exception declaration and register the declaration
749f4a2713aSLionel Sambuc /// as an instantiated local.
750f4a2713aSLionel Sambuc VarDecl *RebuildExceptionDecl(VarDecl *ExceptionDecl,
751f4a2713aSLionel Sambuc TypeSourceInfo *Declarator,
752f4a2713aSLionel Sambuc SourceLocation StartLoc,
753f4a2713aSLionel Sambuc SourceLocation NameLoc,
754f4a2713aSLionel Sambuc IdentifierInfo *Name);
755f4a2713aSLionel Sambuc
756f4a2713aSLionel Sambuc /// \brief Rebuild the Objective-C exception declaration and register the
757f4a2713aSLionel Sambuc /// declaration as an instantiated local.
758f4a2713aSLionel Sambuc VarDecl *RebuildObjCExceptionDecl(VarDecl *ExceptionDecl,
759f4a2713aSLionel Sambuc TypeSourceInfo *TSInfo, QualType T);
760f4a2713aSLionel Sambuc
761f4a2713aSLionel Sambuc /// \brief Check for tag mismatches when instantiating an
762f4a2713aSLionel Sambuc /// elaborated type.
763f4a2713aSLionel Sambuc QualType RebuildElaboratedType(SourceLocation KeywordLoc,
764f4a2713aSLionel Sambuc ElaboratedTypeKeyword Keyword,
765f4a2713aSLionel Sambuc NestedNameSpecifierLoc QualifierLoc,
766f4a2713aSLionel Sambuc QualType T);
767f4a2713aSLionel Sambuc
768*0a6a1f1dSLionel Sambuc TemplateName
769*0a6a1f1dSLionel Sambuc TransformTemplateName(CXXScopeSpec &SS, TemplateName Name,
770f4a2713aSLionel Sambuc SourceLocation NameLoc,
771f4a2713aSLionel Sambuc QualType ObjectType = QualType(),
772*0a6a1f1dSLionel Sambuc NamedDecl *FirstQualifierInScope = nullptr);
773*0a6a1f1dSLionel Sambuc
774*0a6a1f1dSLionel Sambuc const LoopHintAttr *TransformLoopHintAttr(const LoopHintAttr *LH);
775f4a2713aSLionel Sambuc
776f4a2713aSLionel Sambuc ExprResult TransformPredefinedExpr(PredefinedExpr *E);
777f4a2713aSLionel Sambuc ExprResult TransformDeclRefExpr(DeclRefExpr *E);
778f4a2713aSLionel Sambuc ExprResult TransformCXXDefaultArgExpr(CXXDefaultArgExpr *E);
779f4a2713aSLionel Sambuc
780f4a2713aSLionel Sambuc ExprResult TransformTemplateParmRefExpr(DeclRefExpr *E,
781f4a2713aSLionel Sambuc NonTypeTemplateParmDecl *D);
782f4a2713aSLionel Sambuc ExprResult TransformSubstNonTypeTemplateParmPackExpr(
783f4a2713aSLionel Sambuc SubstNonTypeTemplateParmPackExpr *E);
784f4a2713aSLionel Sambuc
785f4a2713aSLionel Sambuc /// \brief Rebuild a DeclRefExpr for a ParmVarDecl reference.
786f4a2713aSLionel Sambuc ExprResult RebuildParmVarDeclRefExpr(ParmVarDecl *PD, SourceLocation Loc);
787f4a2713aSLionel Sambuc
788f4a2713aSLionel Sambuc /// \brief Transform a reference to a function parameter pack.
789f4a2713aSLionel Sambuc ExprResult TransformFunctionParmPackRefExpr(DeclRefExpr *E,
790f4a2713aSLionel Sambuc ParmVarDecl *PD);
791f4a2713aSLionel Sambuc
792f4a2713aSLionel Sambuc /// \brief Transform a FunctionParmPackExpr which was built when we couldn't
793f4a2713aSLionel Sambuc /// expand a function parameter pack reference which refers to an expanded
794f4a2713aSLionel Sambuc /// pack.
795f4a2713aSLionel Sambuc ExprResult TransformFunctionParmPackExpr(FunctionParmPackExpr *E);
796f4a2713aSLionel Sambuc
TransformFunctionProtoType(TypeLocBuilder & TLB,FunctionProtoTypeLoc TL)797f4a2713aSLionel Sambuc QualType TransformFunctionProtoType(TypeLocBuilder &TLB,
798*0a6a1f1dSLionel Sambuc FunctionProtoTypeLoc TL) {
799*0a6a1f1dSLionel Sambuc // Call the base version; it will forward to our overridden version below.
800*0a6a1f1dSLionel Sambuc return inherited::TransformFunctionProtoType(TLB, TL);
801*0a6a1f1dSLionel Sambuc }
802*0a6a1f1dSLionel Sambuc
803*0a6a1f1dSLionel Sambuc template<typename Fn>
804f4a2713aSLionel Sambuc QualType TransformFunctionProtoType(TypeLocBuilder &TLB,
805f4a2713aSLionel Sambuc FunctionProtoTypeLoc TL,
806f4a2713aSLionel Sambuc CXXRecordDecl *ThisContext,
807*0a6a1f1dSLionel Sambuc unsigned ThisTypeQuals,
808*0a6a1f1dSLionel Sambuc Fn TransformExceptionSpec);
809f4a2713aSLionel Sambuc
810f4a2713aSLionel Sambuc ParmVarDecl *TransformFunctionTypeParam(ParmVarDecl *OldParm,
811f4a2713aSLionel Sambuc int indexAdjustment,
812f4a2713aSLionel Sambuc Optional<unsigned> NumExpansions,
813f4a2713aSLionel Sambuc bool ExpectParameterPack);
814f4a2713aSLionel Sambuc
815f4a2713aSLionel Sambuc /// \brief Transforms a template type parameter type by performing
816f4a2713aSLionel Sambuc /// substitution of the corresponding template type argument.
817f4a2713aSLionel Sambuc QualType TransformTemplateTypeParmType(TypeLocBuilder &TLB,
818f4a2713aSLionel Sambuc TemplateTypeParmTypeLoc TL);
819f4a2713aSLionel Sambuc
820f4a2713aSLionel Sambuc /// \brief Transforms an already-substituted template type parameter pack
821f4a2713aSLionel Sambuc /// into either itself (if we aren't substituting into its pack expansion)
822f4a2713aSLionel Sambuc /// or the appropriate substituted argument.
823f4a2713aSLionel Sambuc QualType TransformSubstTemplateTypeParmPackType(TypeLocBuilder &TLB,
824f4a2713aSLionel Sambuc SubstTemplateTypeParmPackTypeLoc TL);
825f4a2713aSLionel Sambuc
TransformCallExpr(CallExpr * CE)826f4a2713aSLionel Sambuc ExprResult TransformCallExpr(CallExpr *CE) {
827f4a2713aSLionel Sambuc getSema().CallsUndergoingInstantiation.push_back(CE);
828f4a2713aSLionel Sambuc ExprResult Result =
829f4a2713aSLionel Sambuc TreeTransform<TemplateInstantiator>::TransformCallExpr(CE);
830f4a2713aSLionel Sambuc getSema().CallsUndergoingInstantiation.pop_back();
831f4a2713aSLionel Sambuc return Result;
832f4a2713aSLionel Sambuc }
833f4a2713aSLionel Sambuc
TransformLambdaExpr(LambdaExpr * E)834f4a2713aSLionel Sambuc ExprResult TransformLambdaExpr(LambdaExpr *E) {
835f4a2713aSLionel Sambuc LocalInstantiationScope Scope(SemaRef, /*CombineWithOuterScope=*/true);
836f4a2713aSLionel Sambuc return TreeTransform<TemplateInstantiator>::TransformLambdaExpr(E);
837f4a2713aSLionel Sambuc }
838f4a2713aSLionel Sambuc
TransformLambdaScope(LambdaExpr * E,CXXMethodDecl * NewCallOperator,ArrayRef<InitCaptureInfoTy> InitCaptureExprsAndTypes)839f4a2713aSLionel Sambuc ExprResult TransformLambdaScope(LambdaExpr *E,
840*0a6a1f1dSLionel Sambuc CXXMethodDecl *NewCallOperator,
841*0a6a1f1dSLionel Sambuc ArrayRef<InitCaptureInfoTy> InitCaptureExprsAndTypes) {
842f4a2713aSLionel Sambuc CXXMethodDecl *const OldCallOperator = E->getCallOperator();
843f4a2713aSLionel Sambuc // In the generic lambda case, we set the NewTemplate to be considered
844f4a2713aSLionel Sambuc // an "instantiation" of the OldTemplate.
845f4a2713aSLionel Sambuc if (FunctionTemplateDecl *const NewCallOperatorTemplate =
846f4a2713aSLionel Sambuc NewCallOperator->getDescribedFunctionTemplate()) {
847f4a2713aSLionel Sambuc
848f4a2713aSLionel Sambuc FunctionTemplateDecl *const OldCallOperatorTemplate =
849f4a2713aSLionel Sambuc OldCallOperator->getDescribedFunctionTemplate();
850f4a2713aSLionel Sambuc NewCallOperatorTemplate->setInstantiatedFromMemberTemplate(
851f4a2713aSLionel Sambuc OldCallOperatorTemplate);
852f4a2713aSLionel Sambuc } else
853f4a2713aSLionel Sambuc // For a non-generic lambda we set the NewCallOperator to
854f4a2713aSLionel Sambuc // be an instantiation of the OldCallOperator.
855f4a2713aSLionel Sambuc NewCallOperator->setInstantiationOfMemberFunction(OldCallOperator,
856f4a2713aSLionel Sambuc TSK_ImplicitInstantiation);
857f4a2713aSLionel Sambuc
858*0a6a1f1dSLionel Sambuc return inherited::TransformLambdaScope(E, NewCallOperator,
859*0a6a1f1dSLionel Sambuc InitCaptureExprsAndTypes);
860f4a2713aSLionel Sambuc }
TransformTemplateParameterList(TemplateParameterList * OrigTPL)861f4a2713aSLionel Sambuc TemplateParameterList *TransformTemplateParameterList(
862f4a2713aSLionel Sambuc TemplateParameterList *OrigTPL) {
863f4a2713aSLionel Sambuc if (!OrigTPL || !OrigTPL->size()) return OrigTPL;
864f4a2713aSLionel Sambuc
865f4a2713aSLionel Sambuc DeclContext *Owner = OrigTPL->getParam(0)->getDeclContext();
866f4a2713aSLionel Sambuc TemplateDeclInstantiator DeclInstantiator(getSema(),
867f4a2713aSLionel Sambuc /* DeclContext *Owner */ Owner, TemplateArgs);
868f4a2713aSLionel Sambuc return DeclInstantiator.SubstTemplateParams(OrigTPL);
869f4a2713aSLionel Sambuc }
870f4a2713aSLionel Sambuc private:
871f4a2713aSLionel Sambuc ExprResult transformNonTypeTemplateParmRef(NonTypeTemplateParmDecl *parm,
872f4a2713aSLionel Sambuc SourceLocation loc,
873f4a2713aSLionel Sambuc TemplateArgument arg);
874f4a2713aSLionel Sambuc };
875f4a2713aSLionel Sambuc }
876f4a2713aSLionel Sambuc
AlreadyTransformed(QualType T)877f4a2713aSLionel Sambuc bool TemplateInstantiator::AlreadyTransformed(QualType T) {
878f4a2713aSLionel Sambuc if (T.isNull())
879f4a2713aSLionel Sambuc return true;
880f4a2713aSLionel Sambuc
881f4a2713aSLionel Sambuc if (T->isInstantiationDependentType() || T->isVariablyModifiedType())
882f4a2713aSLionel Sambuc return false;
883f4a2713aSLionel Sambuc
884f4a2713aSLionel Sambuc getSema().MarkDeclarationsReferencedInType(Loc, T);
885f4a2713aSLionel Sambuc return true;
886f4a2713aSLionel Sambuc }
887f4a2713aSLionel Sambuc
888f4a2713aSLionel Sambuc static TemplateArgument
getPackSubstitutedTemplateArgument(Sema & S,TemplateArgument Arg)889f4a2713aSLionel Sambuc getPackSubstitutedTemplateArgument(Sema &S, TemplateArgument Arg) {
890f4a2713aSLionel Sambuc assert(S.ArgumentPackSubstitutionIndex >= 0);
891f4a2713aSLionel Sambuc assert(S.ArgumentPackSubstitutionIndex < (int)Arg.pack_size());
892f4a2713aSLionel Sambuc Arg = Arg.pack_begin()[S.ArgumentPackSubstitutionIndex];
893f4a2713aSLionel Sambuc if (Arg.isPackExpansion())
894f4a2713aSLionel Sambuc Arg = Arg.getPackExpansionPattern();
895f4a2713aSLionel Sambuc return Arg;
896f4a2713aSLionel Sambuc }
897f4a2713aSLionel Sambuc
TransformDecl(SourceLocation Loc,Decl * D)898f4a2713aSLionel Sambuc Decl *TemplateInstantiator::TransformDecl(SourceLocation Loc, Decl *D) {
899f4a2713aSLionel Sambuc if (!D)
900*0a6a1f1dSLionel Sambuc return nullptr;
901f4a2713aSLionel Sambuc
902f4a2713aSLionel Sambuc if (TemplateTemplateParmDecl *TTP = dyn_cast<TemplateTemplateParmDecl>(D)) {
903f4a2713aSLionel Sambuc if (TTP->getDepth() < TemplateArgs.getNumLevels()) {
904f4a2713aSLionel Sambuc // If the corresponding template argument is NULL or non-existent, it's
905f4a2713aSLionel Sambuc // because we are performing instantiation from explicitly-specified
906f4a2713aSLionel Sambuc // template arguments in a function template, but there were some
907f4a2713aSLionel Sambuc // arguments left unspecified.
908f4a2713aSLionel Sambuc if (!TemplateArgs.hasTemplateArgument(TTP->getDepth(),
909f4a2713aSLionel Sambuc TTP->getPosition()))
910f4a2713aSLionel Sambuc return D;
911f4a2713aSLionel Sambuc
912f4a2713aSLionel Sambuc TemplateArgument Arg = TemplateArgs(TTP->getDepth(), TTP->getPosition());
913f4a2713aSLionel Sambuc
914f4a2713aSLionel Sambuc if (TTP->isParameterPack()) {
915f4a2713aSLionel Sambuc assert(Arg.getKind() == TemplateArgument::Pack &&
916f4a2713aSLionel Sambuc "Missing argument pack");
917f4a2713aSLionel Sambuc Arg = getPackSubstitutedTemplateArgument(getSema(), Arg);
918f4a2713aSLionel Sambuc }
919f4a2713aSLionel Sambuc
920f4a2713aSLionel Sambuc TemplateName Template = Arg.getAsTemplate();
921f4a2713aSLionel Sambuc assert(!Template.isNull() && Template.getAsTemplateDecl() &&
922f4a2713aSLionel Sambuc "Wrong kind of template template argument");
923f4a2713aSLionel Sambuc return Template.getAsTemplateDecl();
924f4a2713aSLionel Sambuc }
925f4a2713aSLionel Sambuc
926f4a2713aSLionel Sambuc // Fall through to find the instantiated declaration for this template
927f4a2713aSLionel Sambuc // template parameter.
928f4a2713aSLionel Sambuc }
929f4a2713aSLionel Sambuc
930f4a2713aSLionel Sambuc return SemaRef.FindInstantiatedDecl(Loc, cast<NamedDecl>(D), TemplateArgs);
931f4a2713aSLionel Sambuc }
932f4a2713aSLionel Sambuc
TransformDefinition(SourceLocation Loc,Decl * D)933f4a2713aSLionel Sambuc Decl *TemplateInstantiator::TransformDefinition(SourceLocation Loc, Decl *D) {
934f4a2713aSLionel Sambuc Decl *Inst = getSema().SubstDecl(D, getSema().CurContext, TemplateArgs);
935f4a2713aSLionel Sambuc if (!Inst)
936*0a6a1f1dSLionel Sambuc return nullptr;
937f4a2713aSLionel Sambuc
938f4a2713aSLionel Sambuc getSema().CurrentInstantiationScope->InstantiatedLocal(D, Inst);
939f4a2713aSLionel Sambuc return Inst;
940f4a2713aSLionel Sambuc }
941f4a2713aSLionel Sambuc
942f4a2713aSLionel Sambuc NamedDecl *
TransformFirstQualifierInScope(NamedDecl * D,SourceLocation Loc)943f4a2713aSLionel Sambuc TemplateInstantiator::TransformFirstQualifierInScope(NamedDecl *D,
944f4a2713aSLionel Sambuc SourceLocation Loc) {
945f4a2713aSLionel Sambuc // If the first part of the nested-name-specifier was a template type
946f4a2713aSLionel Sambuc // parameter, instantiate that type parameter down to a tag type.
947f4a2713aSLionel Sambuc if (TemplateTypeParmDecl *TTPD = dyn_cast_or_null<TemplateTypeParmDecl>(D)) {
948f4a2713aSLionel Sambuc const TemplateTypeParmType *TTP
949f4a2713aSLionel Sambuc = cast<TemplateTypeParmType>(getSema().Context.getTypeDeclType(TTPD));
950f4a2713aSLionel Sambuc
951f4a2713aSLionel Sambuc if (TTP->getDepth() < TemplateArgs.getNumLevels()) {
952f4a2713aSLionel Sambuc // FIXME: This needs testing w/ member access expressions.
953f4a2713aSLionel Sambuc TemplateArgument Arg = TemplateArgs(TTP->getDepth(), TTP->getIndex());
954f4a2713aSLionel Sambuc
955f4a2713aSLionel Sambuc if (TTP->isParameterPack()) {
956f4a2713aSLionel Sambuc assert(Arg.getKind() == TemplateArgument::Pack &&
957f4a2713aSLionel Sambuc "Missing argument pack");
958f4a2713aSLionel Sambuc
959f4a2713aSLionel Sambuc if (getSema().ArgumentPackSubstitutionIndex == -1)
960*0a6a1f1dSLionel Sambuc return nullptr;
961f4a2713aSLionel Sambuc
962f4a2713aSLionel Sambuc Arg = getPackSubstitutedTemplateArgument(getSema(), Arg);
963f4a2713aSLionel Sambuc }
964f4a2713aSLionel Sambuc
965f4a2713aSLionel Sambuc QualType T = Arg.getAsType();
966f4a2713aSLionel Sambuc if (T.isNull())
967f4a2713aSLionel Sambuc return cast_or_null<NamedDecl>(TransformDecl(Loc, D));
968f4a2713aSLionel Sambuc
969f4a2713aSLionel Sambuc if (const TagType *Tag = T->getAs<TagType>())
970f4a2713aSLionel Sambuc return Tag->getDecl();
971f4a2713aSLionel Sambuc
972f4a2713aSLionel Sambuc // The resulting type is not a tag; complain.
973f4a2713aSLionel Sambuc getSema().Diag(Loc, diag::err_nested_name_spec_non_tag) << T;
974*0a6a1f1dSLionel Sambuc return nullptr;
975f4a2713aSLionel Sambuc }
976f4a2713aSLionel Sambuc }
977f4a2713aSLionel Sambuc
978f4a2713aSLionel Sambuc return cast_or_null<NamedDecl>(TransformDecl(Loc, D));
979f4a2713aSLionel Sambuc }
980f4a2713aSLionel Sambuc
981f4a2713aSLionel Sambuc VarDecl *
RebuildExceptionDecl(VarDecl * ExceptionDecl,TypeSourceInfo * Declarator,SourceLocation StartLoc,SourceLocation NameLoc,IdentifierInfo * Name)982f4a2713aSLionel Sambuc TemplateInstantiator::RebuildExceptionDecl(VarDecl *ExceptionDecl,
983f4a2713aSLionel Sambuc TypeSourceInfo *Declarator,
984f4a2713aSLionel Sambuc SourceLocation StartLoc,
985f4a2713aSLionel Sambuc SourceLocation NameLoc,
986f4a2713aSLionel Sambuc IdentifierInfo *Name) {
987f4a2713aSLionel Sambuc VarDecl *Var = inherited::RebuildExceptionDecl(ExceptionDecl, Declarator,
988f4a2713aSLionel Sambuc StartLoc, NameLoc, Name);
989f4a2713aSLionel Sambuc if (Var)
990f4a2713aSLionel Sambuc getSema().CurrentInstantiationScope->InstantiatedLocal(ExceptionDecl, Var);
991f4a2713aSLionel Sambuc return Var;
992f4a2713aSLionel Sambuc }
993f4a2713aSLionel Sambuc
RebuildObjCExceptionDecl(VarDecl * ExceptionDecl,TypeSourceInfo * TSInfo,QualType T)994f4a2713aSLionel Sambuc VarDecl *TemplateInstantiator::RebuildObjCExceptionDecl(VarDecl *ExceptionDecl,
995f4a2713aSLionel Sambuc TypeSourceInfo *TSInfo,
996f4a2713aSLionel Sambuc QualType T) {
997f4a2713aSLionel Sambuc VarDecl *Var = inherited::RebuildObjCExceptionDecl(ExceptionDecl, TSInfo, T);
998f4a2713aSLionel Sambuc if (Var)
999f4a2713aSLionel Sambuc getSema().CurrentInstantiationScope->InstantiatedLocal(ExceptionDecl, Var);
1000f4a2713aSLionel Sambuc return Var;
1001f4a2713aSLionel Sambuc }
1002f4a2713aSLionel Sambuc
1003f4a2713aSLionel Sambuc QualType
RebuildElaboratedType(SourceLocation KeywordLoc,ElaboratedTypeKeyword Keyword,NestedNameSpecifierLoc QualifierLoc,QualType T)1004f4a2713aSLionel Sambuc TemplateInstantiator::RebuildElaboratedType(SourceLocation KeywordLoc,
1005f4a2713aSLionel Sambuc ElaboratedTypeKeyword Keyword,
1006f4a2713aSLionel Sambuc NestedNameSpecifierLoc QualifierLoc,
1007f4a2713aSLionel Sambuc QualType T) {
1008f4a2713aSLionel Sambuc if (const TagType *TT = T->getAs<TagType>()) {
1009f4a2713aSLionel Sambuc TagDecl* TD = TT->getDecl();
1010f4a2713aSLionel Sambuc
1011f4a2713aSLionel Sambuc SourceLocation TagLocation = KeywordLoc;
1012f4a2713aSLionel Sambuc
1013f4a2713aSLionel Sambuc IdentifierInfo *Id = TD->getIdentifier();
1014f4a2713aSLionel Sambuc
1015f4a2713aSLionel Sambuc // TODO: should we even warn on struct/class mismatches for this? Seems
1016f4a2713aSLionel Sambuc // like it's likely to produce a lot of spurious errors.
1017f4a2713aSLionel Sambuc if (Id && Keyword != ETK_None && Keyword != ETK_Typename) {
1018f4a2713aSLionel Sambuc TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForKeyword(Keyword);
1019f4a2713aSLionel Sambuc if (!SemaRef.isAcceptableTagRedeclaration(TD, Kind, /*isDefinition*/false,
1020f4a2713aSLionel Sambuc TagLocation, *Id)) {
1021f4a2713aSLionel Sambuc SemaRef.Diag(TagLocation, diag::err_use_with_wrong_tag)
1022f4a2713aSLionel Sambuc << Id
1023f4a2713aSLionel Sambuc << FixItHint::CreateReplacement(SourceRange(TagLocation),
1024f4a2713aSLionel Sambuc TD->getKindName());
1025f4a2713aSLionel Sambuc SemaRef.Diag(TD->getLocation(), diag::note_previous_use);
1026f4a2713aSLionel Sambuc }
1027f4a2713aSLionel Sambuc }
1028f4a2713aSLionel Sambuc }
1029f4a2713aSLionel Sambuc
1030f4a2713aSLionel Sambuc return TreeTransform<TemplateInstantiator>::RebuildElaboratedType(KeywordLoc,
1031f4a2713aSLionel Sambuc Keyword,
1032f4a2713aSLionel Sambuc QualifierLoc,
1033f4a2713aSLionel Sambuc T);
1034f4a2713aSLionel Sambuc }
1035f4a2713aSLionel Sambuc
TransformTemplateName(CXXScopeSpec & SS,TemplateName Name,SourceLocation NameLoc,QualType ObjectType,NamedDecl * FirstQualifierInScope)1036f4a2713aSLionel Sambuc TemplateName TemplateInstantiator::TransformTemplateName(CXXScopeSpec &SS,
1037f4a2713aSLionel Sambuc TemplateName Name,
1038f4a2713aSLionel Sambuc SourceLocation NameLoc,
1039f4a2713aSLionel Sambuc QualType ObjectType,
1040f4a2713aSLionel Sambuc NamedDecl *FirstQualifierInScope) {
1041f4a2713aSLionel Sambuc if (TemplateTemplateParmDecl *TTP
1042f4a2713aSLionel Sambuc = dyn_cast_or_null<TemplateTemplateParmDecl>(Name.getAsTemplateDecl())) {
1043f4a2713aSLionel Sambuc if (TTP->getDepth() < TemplateArgs.getNumLevels()) {
1044f4a2713aSLionel Sambuc // If the corresponding template argument is NULL or non-existent, it's
1045f4a2713aSLionel Sambuc // because we are performing instantiation from explicitly-specified
1046f4a2713aSLionel Sambuc // template arguments in a function template, but there were some
1047f4a2713aSLionel Sambuc // arguments left unspecified.
1048f4a2713aSLionel Sambuc if (!TemplateArgs.hasTemplateArgument(TTP->getDepth(),
1049f4a2713aSLionel Sambuc TTP->getPosition()))
1050f4a2713aSLionel Sambuc return Name;
1051f4a2713aSLionel Sambuc
1052f4a2713aSLionel Sambuc TemplateArgument Arg = TemplateArgs(TTP->getDepth(), TTP->getPosition());
1053f4a2713aSLionel Sambuc
1054f4a2713aSLionel Sambuc if (TTP->isParameterPack()) {
1055f4a2713aSLionel Sambuc assert(Arg.getKind() == TemplateArgument::Pack &&
1056f4a2713aSLionel Sambuc "Missing argument pack");
1057f4a2713aSLionel Sambuc
1058f4a2713aSLionel Sambuc if (getSema().ArgumentPackSubstitutionIndex == -1) {
1059f4a2713aSLionel Sambuc // We have the template argument pack to substitute, but we're not
1060f4a2713aSLionel Sambuc // actually expanding the enclosing pack expansion yet. So, just
1061f4a2713aSLionel Sambuc // keep the entire argument pack.
1062f4a2713aSLionel Sambuc return getSema().Context.getSubstTemplateTemplateParmPack(TTP, Arg);
1063f4a2713aSLionel Sambuc }
1064f4a2713aSLionel Sambuc
1065f4a2713aSLionel Sambuc Arg = getPackSubstitutedTemplateArgument(getSema(), Arg);
1066f4a2713aSLionel Sambuc }
1067f4a2713aSLionel Sambuc
1068f4a2713aSLionel Sambuc TemplateName Template = Arg.getAsTemplate();
1069f4a2713aSLionel Sambuc assert(!Template.isNull() && "Null template template argument");
1070f4a2713aSLionel Sambuc
1071f4a2713aSLionel Sambuc // We don't ever want to substitute for a qualified template name, since
1072f4a2713aSLionel Sambuc // the qualifier is handled separately. So, look through the qualified
1073f4a2713aSLionel Sambuc // template name to its underlying declaration.
1074f4a2713aSLionel Sambuc if (QualifiedTemplateName *QTN = Template.getAsQualifiedTemplateName())
1075f4a2713aSLionel Sambuc Template = TemplateName(QTN->getTemplateDecl());
1076f4a2713aSLionel Sambuc
1077f4a2713aSLionel Sambuc Template = getSema().Context.getSubstTemplateTemplateParm(TTP, Template);
1078f4a2713aSLionel Sambuc return Template;
1079f4a2713aSLionel Sambuc }
1080f4a2713aSLionel Sambuc }
1081f4a2713aSLionel Sambuc
1082f4a2713aSLionel Sambuc if (SubstTemplateTemplateParmPackStorage *SubstPack
1083f4a2713aSLionel Sambuc = Name.getAsSubstTemplateTemplateParmPack()) {
1084f4a2713aSLionel Sambuc if (getSema().ArgumentPackSubstitutionIndex == -1)
1085f4a2713aSLionel Sambuc return Name;
1086f4a2713aSLionel Sambuc
1087f4a2713aSLionel Sambuc TemplateArgument Arg = SubstPack->getArgumentPack();
1088f4a2713aSLionel Sambuc Arg = getPackSubstitutedTemplateArgument(getSema(), Arg);
1089f4a2713aSLionel Sambuc return Arg.getAsTemplate();
1090f4a2713aSLionel Sambuc }
1091f4a2713aSLionel Sambuc
1092f4a2713aSLionel Sambuc return inherited::TransformTemplateName(SS, Name, NameLoc, ObjectType,
1093f4a2713aSLionel Sambuc FirstQualifierInScope);
1094f4a2713aSLionel Sambuc }
1095f4a2713aSLionel Sambuc
1096f4a2713aSLionel Sambuc ExprResult
TransformPredefinedExpr(PredefinedExpr * E)1097f4a2713aSLionel Sambuc TemplateInstantiator::TransformPredefinedExpr(PredefinedExpr *E) {
1098f4a2713aSLionel Sambuc if (!E->isTypeDependent())
1099*0a6a1f1dSLionel Sambuc return E;
1100f4a2713aSLionel Sambuc
1101f4a2713aSLionel Sambuc return getSema().BuildPredefinedExpr(E->getLocation(), E->getIdentType());
1102f4a2713aSLionel Sambuc }
1103f4a2713aSLionel Sambuc
1104f4a2713aSLionel Sambuc ExprResult
TransformTemplateParmRefExpr(DeclRefExpr * E,NonTypeTemplateParmDecl * NTTP)1105f4a2713aSLionel Sambuc TemplateInstantiator::TransformTemplateParmRefExpr(DeclRefExpr *E,
1106f4a2713aSLionel Sambuc NonTypeTemplateParmDecl *NTTP) {
1107f4a2713aSLionel Sambuc // If the corresponding template argument is NULL or non-existent, it's
1108f4a2713aSLionel Sambuc // because we are performing instantiation from explicitly-specified
1109f4a2713aSLionel Sambuc // template arguments in a function template, but there were some
1110f4a2713aSLionel Sambuc // arguments left unspecified.
1111f4a2713aSLionel Sambuc if (!TemplateArgs.hasTemplateArgument(NTTP->getDepth(),
1112f4a2713aSLionel Sambuc NTTP->getPosition()))
1113*0a6a1f1dSLionel Sambuc return E;
1114f4a2713aSLionel Sambuc
1115f4a2713aSLionel Sambuc TemplateArgument Arg = TemplateArgs(NTTP->getDepth(), NTTP->getPosition());
1116f4a2713aSLionel Sambuc if (NTTP->isParameterPack()) {
1117f4a2713aSLionel Sambuc assert(Arg.getKind() == TemplateArgument::Pack &&
1118f4a2713aSLionel Sambuc "Missing argument pack");
1119f4a2713aSLionel Sambuc
1120f4a2713aSLionel Sambuc if (getSema().ArgumentPackSubstitutionIndex == -1) {
1121f4a2713aSLionel Sambuc // We have an argument pack, but we can't select a particular argument
1122f4a2713aSLionel Sambuc // out of it yet. Therefore, we'll build an expression to hold on to that
1123f4a2713aSLionel Sambuc // argument pack.
1124f4a2713aSLionel Sambuc QualType TargetType = SemaRef.SubstType(NTTP->getType(), TemplateArgs,
1125f4a2713aSLionel Sambuc E->getLocation(),
1126f4a2713aSLionel Sambuc NTTP->getDeclName());
1127f4a2713aSLionel Sambuc if (TargetType.isNull())
1128f4a2713aSLionel Sambuc return ExprError();
1129f4a2713aSLionel Sambuc
1130f4a2713aSLionel Sambuc return new (SemaRef.Context) SubstNonTypeTemplateParmPackExpr(TargetType,
1131f4a2713aSLionel Sambuc NTTP,
1132f4a2713aSLionel Sambuc E->getLocation(),
1133f4a2713aSLionel Sambuc Arg);
1134f4a2713aSLionel Sambuc }
1135f4a2713aSLionel Sambuc
1136f4a2713aSLionel Sambuc Arg = getPackSubstitutedTemplateArgument(getSema(), Arg);
1137f4a2713aSLionel Sambuc }
1138f4a2713aSLionel Sambuc
1139f4a2713aSLionel Sambuc return transformNonTypeTemplateParmRef(NTTP, E->getLocation(), Arg);
1140f4a2713aSLionel Sambuc }
1141f4a2713aSLionel Sambuc
1142*0a6a1f1dSLionel Sambuc const LoopHintAttr *
TransformLoopHintAttr(const LoopHintAttr * LH)1143*0a6a1f1dSLionel Sambuc TemplateInstantiator::TransformLoopHintAttr(const LoopHintAttr *LH) {
1144*0a6a1f1dSLionel Sambuc Expr *TransformedExpr = getDerived().TransformExpr(LH->getValue()).get();
1145*0a6a1f1dSLionel Sambuc
1146*0a6a1f1dSLionel Sambuc if (TransformedExpr == LH->getValue())
1147*0a6a1f1dSLionel Sambuc return LH;
1148*0a6a1f1dSLionel Sambuc
1149*0a6a1f1dSLionel Sambuc // Generate error if there is a problem with the value.
1150*0a6a1f1dSLionel Sambuc if (getSema().CheckLoopHintExpr(TransformedExpr, LH->getLocation()))
1151*0a6a1f1dSLionel Sambuc return LH;
1152*0a6a1f1dSLionel Sambuc
1153*0a6a1f1dSLionel Sambuc // Create new LoopHintValueAttr with integral expression in place of the
1154*0a6a1f1dSLionel Sambuc // non-type template parameter.
1155*0a6a1f1dSLionel Sambuc return LoopHintAttr::CreateImplicit(
1156*0a6a1f1dSLionel Sambuc getSema().Context, LH->getSemanticSpelling(), LH->getOption(),
1157*0a6a1f1dSLionel Sambuc LH->getState(), TransformedExpr, LH->getRange());
1158*0a6a1f1dSLionel Sambuc }
1159*0a6a1f1dSLionel Sambuc
transformNonTypeTemplateParmRef(NonTypeTemplateParmDecl * parm,SourceLocation loc,TemplateArgument arg)1160f4a2713aSLionel Sambuc ExprResult TemplateInstantiator::transformNonTypeTemplateParmRef(
1161f4a2713aSLionel Sambuc NonTypeTemplateParmDecl *parm,
1162f4a2713aSLionel Sambuc SourceLocation loc,
1163f4a2713aSLionel Sambuc TemplateArgument arg) {
1164f4a2713aSLionel Sambuc ExprResult result;
1165f4a2713aSLionel Sambuc QualType type;
1166f4a2713aSLionel Sambuc
1167f4a2713aSLionel Sambuc // The template argument itself might be an expression, in which
1168f4a2713aSLionel Sambuc // case we just return that expression.
1169f4a2713aSLionel Sambuc if (arg.getKind() == TemplateArgument::Expression) {
1170f4a2713aSLionel Sambuc Expr *argExpr = arg.getAsExpr();
1171*0a6a1f1dSLionel Sambuc result = argExpr;
1172f4a2713aSLionel Sambuc type = argExpr->getType();
1173f4a2713aSLionel Sambuc
1174f4a2713aSLionel Sambuc } else if (arg.getKind() == TemplateArgument::Declaration ||
1175f4a2713aSLionel Sambuc arg.getKind() == TemplateArgument::NullPtr) {
1176f4a2713aSLionel Sambuc ValueDecl *VD;
1177f4a2713aSLionel Sambuc if (arg.getKind() == TemplateArgument::Declaration) {
1178f4a2713aSLionel Sambuc VD = cast<ValueDecl>(arg.getAsDecl());
1179f4a2713aSLionel Sambuc
1180f4a2713aSLionel Sambuc // Find the instantiation of the template argument. This is
1181f4a2713aSLionel Sambuc // required for nested templates.
1182f4a2713aSLionel Sambuc VD = cast_or_null<ValueDecl>(
1183f4a2713aSLionel Sambuc getSema().FindInstantiatedDecl(loc, VD, TemplateArgs));
1184f4a2713aSLionel Sambuc if (!VD)
1185f4a2713aSLionel Sambuc return ExprError();
1186f4a2713aSLionel Sambuc } else {
1187f4a2713aSLionel Sambuc // Propagate NULL template argument.
1188*0a6a1f1dSLionel Sambuc VD = nullptr;
1189f4a2713aSLionel Sambuc }
1190f4a2713aSLionel Sambuc
1191f4a2713aSLionel Sambuc // Derive the type we want the substituted decl to have. This had
1192f4a2713aSLionel Sambuc // better be non-dependent, or these checks will have serious problems.
1193f4a2713aSLionel Sambuc if (parm->isExpandedParameterPack()) {
1194f4a2713aSLionel Sambuc type = parm->getExpansionType(SemaRef.ArgumentPackSubstitutionIndex);
1195f4a2713aSLionel Sambuc } else if (parm->isParameterPack() &&
1196f4a2713aSLionel Sambuc isa<PackExpansionType>(parm->getType())) {
1197f4a2713aSLionel Sambuc type = SemaRef.SubstType(
1198f4a2713aSLionel Sambuc cast<PackExpansionType>(parm->getType())->getPattern(),
1199f4a2713aSLionel Sambuc TemplateArgs, loc, parm->getDeclName());
1200f4a2713aSLionel Sambuc } else {
1201f4a2713aSLionel Sambuc type = SemaRef.SubstType(parm->getType(), TemplateArgs,
1202f4a2713aSLionel Sambuc loc, parm->getDeclName());
1203f4a2713aSLionel Sambuc }
1204f4a2713aSLionel Sambuc assert(!type.isNull() && "type substitution failed for param type");
1205f4a2713aSLionel Sambuc assert(!type->isDependentType() && "param type still dependent");
1206f4a2713aSLionel Sambuc result = SemaRef.BuildExpressionFromDeclTemplateArgument(arg, type, loc);
1207f4a2713aSLionel Sambuc
1208f4a2713aSLionel Sambuc if (!result.isInvalid()) type = result.get()->getType();
1209f4a2713aSLionel Sambuc } else {
1210f4a2713aSLionel Sambuc result = SemaRef.BuildExpressionFromIntegralTemplateArgument(arg, loc);
1211f4a2713aSLionel Sambuc
1212f4a2713aSLionel Sambuc // Note that this type can be different from the type of 'result',
1213f4a2713aSLionel Sambuc // e.g. if it's an enum type.
1214f4a2713aSLionel Sambuc type = arg.getIntegralType();
1215f4a2713aSLionel Sambuc }
1216f4a2713aSLionel Sambuc if (result.isInvalid()) return ExprError();
1217f4a2713aSLionel Sambuc
1218*0a6a1f1dSLionel Sambuc Expr *resultExpr = result.get();
1219*0a6a1f1dSLionel Sambuc return new (SemaRef.Context) SubstNonTypeTemplateParmExpr(
1220*0a6a1f1dSLionel Sambuc type, resultExpr->getValueKind(), loc, parm, resultExpr);
1221f4a2713aSLionel Sambuc }
1222f4a2713aSLionel Sambuc
1223f4a2713aSLionel Sambuc ExprResult
TransformSubstNonTypeTemplateParmPackExpr(SubstNonTypeTemplateParmPackExpr * E)1224f4a2713aSLionel Sambuc TemplateInstantiator::TransformSubstNonTypeTemplateParmPackExpr(
1225f4a2713aSLionel Sambuc SubstNonTypeTemplateParmPackExpr *E) {
1226f4a2713aSLionel Sambuc if (getSema().ArgumentPackSubstitutionIndex == -1) {
1227f4a2713aSLionel Sambuc // We aren't expanding the parameter pack, so just return ourselves.
1228*0a6a1f1dSLionel Sambuc return E;
1229f4a2713aSLionel Sambuc }
1230f4a2713aSLionel Sambuc
1231f4a2713aSLionel Sambuc TemplateArgument Arg = E->getArgumentPack();
1232f4a2713aSLionel Sambuc Arg = getPackSubstitutedTemplateArgument(getSema(), Arg);
1233f4a2713aSLionel Sambuc return transformNonTypeTemplateParmRef(E->getParameterPack(),
1234f4a2713aSLionel Sambuc E->getParameterPackLocation(),
1235f4a2713aSLionel Sambuc Arg);
1236f4a2713aSLionel Sambuc }
1237f4a2713aSLionel Sambuc
1238f4a2713aSLionel Sambuc ExprResult
RebuildParmVarDeclRefExpr(ParmVarDecl * PD,SourceLocation Loc)1239f4a2713aSLionel Sambuc TemplateInstantiator::RebuildParmVarDeclRefExpr(ParmVarDecl *PD,
1240f4a2713aSLionel Sambuc SourceLocation Loc) {
1241f4a2713aSLionel Sambuc DeclarationNameInfo NameInfo(PD->getDeclName(), Loc);
1242f4a2713aSLionel Sambuc return getSema().BuildDeclarationNameExpr(CXXScopeSpec(), NameInfo, PD);
1243f4a2713aSLionel Sambuc }
1244f4a2713aSLionel Sambuc
1245f4a2713aSLionel Sambuc ExprResult
TransformFunctionParmPackExpr(FunctionParmPackExpr * E)1246f4a2713aSLionel Sambuc TemplateInstantiator::TransformFunctionParmPackExpr(FunctionParmPackExpr *E) {
1247f4a2713aSLionel Sambuc if (getSema().ArgumentPackSubstitutionIndex != -1) {
1248f4a2713aSLionel Sambuc // We can expand this parameter pack now.
1249f4a2713aSLionel Sambuc ParmVarDecl *D = E->getExpansion(getSema().ArgumentPackSubstitutionIndex);
1250f4a2713aSLionel Sambuc ValueDecl *VD = cast_or_null<ValueDecl>(TransformDecl(E->getExprLoc(), D));
1251f4a2713aSLionel Sambuc if (!VD)
1252f4a2713aSLionel Sambuc return ExprError();
1253f4a2713aSLionel Sambuc return RebuildParmVarDeclRefExpr(cast<ParmVarDecl>(VD), E->getExprLoc());
1254f4a2713aSLionel Sambuc }
1255f4a2713aSLionel Sambuc
1256f4a2713aSLionel Sambuc QualType T = TransformType(E->getType());
1257f4a2713aSLionel Sambuc if (T.isNull())
1258f4a2713aSLionel Sambuc return ExprError();
1259f4a2713aSLionel Sambuc
1260f4a2713aSLionel Sambuc // Transform each of the parameter expansions into the corresponding
1261f4a2713aSLionel Sambuc // parameters in the instantiation of the function decl.
1262f4a2713aSLionel Sambuc SmallVector<Decl *, 8> Parms;
1263f4a2713aSLionel Sambuc Parms.reserve(E->getNumExpansions());
1264f4a2713aSLionel Sambuc for (FunctionParmPackExpr::iterator I = E->begin(), End = E->end();
1265f4a2713aSLionel Sambuc I != End; ++I) {
1266f4a2713aSLionel Sambuc ParmVarDecl *D =
1267f4a2713aSLionel Sambuc cast_or_null<ParmVarDecl>(TransformDecl(E->getExprLoc(), *I));
1268f4a2713aSLionel Sambuc if (!D)
1269f4a2713aSLionel Sambuc return ExprError();
1270f4a2713aSLionel Sambuc Parms.push_back(D);
1271f4a2713aSLionel Sambuc }
1272f4a2713aSLionel Sambuc
1273f4a2713aSLionel Sambuc return FunctionParmPackExpr::Create(getSema().Context, T,
1274f4a2713aSLionel Sambuc E->getParameterPack(),
1275f4a2713aSLionel Sambuc E->getParameterPackLocation(), Parms);
1276f4a2713aSLionel Sambuc }
1277f4a2713aSLionel Sambuc
1278f4a2713aSLionel Sambuc ExprResult
TransformFunctionParmPackRefExpr(DeclRefExpr * E,ParmVarDecl * PD)1279f4a2713aSLionel Sambuc TemplateInstantiator::TransformFunctionParmPackRefExpr(DeclRefExpr *E,
1280f4a2713aSLionel Sambuc ParmVarDecl *PD) {
1281f4a2713aSLionel Sambuc typedef LocalInstantiationScope::DeclArgumentPack DeclArgumentPack;
1282f4a2713aSLionel Sambuc llvm::PointerUnion<Decl *, DeclArgumentPack *> *Found
1283f4a2713aSLionel Sambuc = getSema().CurrentInstantiationScope->findInstantiationOf(PD);
1284f4a2713aSLionel Sambuc assert(Found && "no instantiation for parameter pack");
1285f4a2713aSLionel Sambuc
1286f4a2713aSLionel Sambuc Decl *TransformedDecl;
1287f4a2713aSLionel Sambuc if (DeclArgumentPack *Pack = Found->dyn_cast<DeclArgumentPack *>()) {
1288*0a6a1f1dSLionel Sambuc // If this is a reference to a function parameter pack which we can
1289*0a6a1f1dSLionel Sambuc // substitute but can't yet expand, build a FunctionParmPackExpr for it.
1290f4a2713aSLionel Sambuc if (getSema().ArgumentPackSubstitutionIndex == -1) {
1291f4a2713aSLionel Sambuc QualType T = TransformType(E->getType());
1292f4a2713aSLionel Sambuc if (T.isNull())
1293f4a2713aSLionel Sambuc return ExprError();
1294f4a2713aSLionel Sambuc return FunctionParmPackExpr::Create(getSema().Context, T, PD,
1295f4a2713aSLionel Sambuc E->getExprLoc(), *Pack);
1296f4a2713aSLionel Sambuc }
1297f4a2713aSLionel Sambuc
1298f4a2713aSLionel Sambuc TransformedDecl = (*Pack)[getSema().ArgumentPackSubstitutionIndex];
1299f4a2713aSLionel Sambuc } else {
1300f4a2713aSLionel Sambuc TransformedDecl = Found->get<Decl*>();
1301f4a2713aSLionel Sambuc }
1302f4a2713aSLionel Sambuc
1303f4a2713aSLionel Sambuc // We have either an unexpanded pack or a specific expansion.
1304f4a2713aSLionel Sambuc return RebuildParmVarDeclRefExpr(cast<ParmVarDecl>(TransformedDecl),
1305f4a2713aSLionel Sambuc E->getExprLoc());
1306f4a2713aSLionel Sambuc }
1307f4a2713aSLionel Sambuc
1308f4a2713aSLionel Sambuc ExprResult
TransformDeclRefExpr(DeclRefExpr * E)1309f4a2713aSLionel Sambuc TemplateInstantiator::TransformDeclRefExpr(DeclRefExpr *E) {
1310f4a2713aSLionel Sambuc NamedDecl *D = E->getDecl();
1311f4a2713aSLionel Sambuc
1312f4a2713aSLionel Sambuc // Handle references to non-type template parameters and non-type template
1313f4a2713aSLionel Sambuc // parameter packs.
1314f4a2713aSLionel Sambuc if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(D)) {
1315f4a2713aSLionel Sambuc if (NTTP->getDepth() < TemplateArgs.getNumLevels())
1316f4a2713aSLionel Sambuc return TransformTemplateParmRefExpr(E, NTTP);
1317f4a2713aSLionel Sambuc
1318f4a2713aSLionel Sambuc // We have a non-type template parameter that isn't fully substituted;
1319f4a2713aSLionel Sambuc // FindInstantiatedDecl will find it in the local instantiation scope.
1320f4a2713aSLionel Sambuc }
1321f4a2713aSLionel Sambuc
1322f4a2713aSLionel Sambuc // Handle references to function parameter packs.
1323f4a2713aSLionel Sambuc if (ParmVarDecl *PD = dyn_cast<ParmVarDecl>(D))
1324f4a2713aSLionel Sambuc if (PD->isParameterPack())
1325f4a2713aSLionel Sambuc return TransformFunctionParmPackRefExpr(E, PD);
1326f4a2713aSLionel Sambuc
1327f4a2713aSLionel Sambuc return TreeTransform<TemplateInstantiator>::TransformDeclRefExpr(E);
1328f4a2713aSLionel Sambuc }
1329f4a2713aSLionel Sambuc
TransformCXXDefaultArgExpr(CXXDefaultArgExpr * E)1330f4a2713aSLionel Sambuc ExprResult TemplateInstantiator::TransformCXXDefaultArgExpr(
1331f4a2713aSLionel Sambuc CXXDefaultArgExpr *E) {
1332f4a2713aSLionel Sambuc assert(!cast<FunctionDecl>(E->getParam()->getDeclContext())->
1333f4a2713aSLionel Sambuc getDescribedFunctionTemplate() &&
1334f4a2713aSLionel Sambuc "Default arg expressions are never formed in dependent cases.");
1335f4a2713aSLionel Sambuc return SemaRef.BuildCXXDefaultArgExpr(E->getUsedLocation(),
1336f4a2713aSLionel Sambuc cast<FunctionDecl>(E->getParam()->getDeclContext()),
1337f4a2713aSLionel Sambuc E->getParam());
1338f4a2713aSLionel Sambuc }
1339f4a2713aSLionel Sambuc
1340*0a6a1f1dSLionel Sambuc template<typename Fn>
TransformFunctionProtoType(TypeLocBuilder & TLB,FunctionProtoTypeLoc TL,CXXRecordDecl * ThisContext,unsigned ThisTypeQuals,Fn TransformExceptionSpec)1341f4a2713aSLionel Sambuc QualType TemplateInstantiator::TransformFunctionProtoType(TypeLocBuilder &TLB,
1342f4a2713aSLionel Sambuc FunctionProtoTypeLoc TL,
1343f4a2713aSLionel Sambuc CXXRecordDecl *ThisContext,
1344*0a6a1f1dSLionel Sambuc unsigned ThisTypeQuals,
1345*0a6a1f1dSLionel Sambuc Fn TransformExceptionSpec) {
1346f4a2713aSLionel Sambuc // We need a local instantiation scope for this function prototype.
1347f4a2713aSLionel Sambuc LocalInstantiationScope Scope(SemaRef, /*CombineWithOuterScope=*/true);
1348*0a6a1f1dSLionel Sambuc return inherited::TransformFunctionProtoType(
1349*0a6a1f1dSLionel Sambuc TLB, TL, ThisContext, ThisTypeQuals, TransformExceptionSpec);
1350f4a2713aSLionel Sambuc }
1351f4a2713aSLionel Sambuc
1352f4a2713aSLionel Sambuc ParmVarDecl *
TransformFunctionTypeParam(ParmVarDecl * OldParm,int indexAdjustment,Optional<unsigned> NumExpansions,bool ExpectParameterPack)1353f4a2713aSLionel Sambuc TemplateInstantiator::TransformFunctionTypeParam(ParmVarDecl *OldParm,
1354f4a2713aSLionel Sambuc int indexAdjustment,
1355f4a2713aSLionel Sambuc Optional<unsigned> NumExpansions,
1356f4a2713aSLionel Sambuc bool ExpectParameterPack) {
1357f4a2713aSLionel Sambuc return SemaRef.SubstParmVarDecl(OldParm, TemplateArgs, indexAdjustment,
1358f4a2713aSLionel Sambuc NumExpansions, ExpectParameterPack);
1359f4a2713aSLionel Sambuc }
1360f4a2713aSLionel Sambuc
1361f4a2713aSLionel Sambuc QualType
TransformTemplateTypeParmType(TypeLocBuilder & TLB,TemplateTypeParmTypeLoc TL)1362f4a2713aSLionel Sambuc TemplateInstantiator::TransformTemplateTypeParmType(TypeLocBuilder &TLB,
1363f4a2713aSLionel Sambuc TemplateTypeParmTypeLoc TL) {
1364f4a2713aSLionel Sambuc const TemplateTypeParmType *T = TL.getTypePtr();
1365f4a2713aSLionel Sambuc if (T->getDepth() < TemplateArgs.getNumLevels()) {
1366f4a2713aSLionel Sambuc // Replace the template type parameter with its corresponding
1367f4a2713aSLionel Sambuc // template argument.
1368f4a2713aSLionel Sambuc
1369f4a2713aSLionel Sambuc // If the corresponding template argument is NULL or doesn't exist, it's
1370f4a2713aSLionel Sambuc // because we are performing instantiation from explicitly-specified
1371f4a2713aSLionel Sambuc // template arguments in a function template class, but there were some
1372f4a2713aSLionel Sambuc // arguments left unspecified.
1373f4a2713aSLionel Sambuc if (!TemplateArgs.hasTemplateArgument(T->getDepth(), T->getIndex())) {
1374f4a2713aSLionel Sambuc TemplateTypeParmTypeLoc NewTL
1375f4a2713aSLionel Sambuc = TLB.push<TemplateTypeParmTypeLoc>(TL.getType());
1376f4a2713aSLionel Sambuc NewTL.setNameLoc(TL.getNameLoc());
1377f4a2713aSLionel Sambuc return TL.getType();
1378f4a2713aSLionel Sambuc }
1379f4a2713aSLionel Sambuc
1380f4a2713aSLionel Sambuc TemplateArgument Arg = TemplateArgs(T->getDepth(), T->getIndex());
1381f4a2713aSLionel Sambuc
1382f4a2713aSLionel Sambuc if (T->isParameterPack()) {
1383f4a2713aSLionel Sambuc assert(Arg.getKind() == TemplateArgument::Pack &&
1384f4a2713aSLionel Sambuc "Missing argument pack");
1385f4a2713aSLionel Sambuc
1386f4a2713aSLionel Sambuc if (getSema().ArgumentPackSubstitutionIndex == -1) {
1387f4a2713aSLionel Sambuc // We have the template argument pack, but we're not expanding the
1388f4a2713aSLionel Sambuc // enclosing pack expansion yet. Just save the template argument
1389f4a2713aSLionel Sambuc // pack for later substitution.
1390f4a2713aSLionel Sambuc QualType Result
1391f4a2713aSLionel Sambuc = getSema().Context.getSubstTemplateTypeParmPackType(T, Arg);
1392f4a2713aSLionel Sambuc SubstTemplateTypeParmPackTypeLoc NewTL
1393f4a2713aSLionel Sambuc = TLB.push<SubstTemplateTypeParmPackTypeLoc>(Result);
1394f4a2713aSLionel Sambuc NewTL.setNameLoc(TL.getNameLoc());
1395f4a2713aSLionel Sambuc return Result;
1396f4a2713aSLionel Sambuc }
1397f4a2713aSLionel Sambuc
1398f4a2713aSLionel Sambuc Arg = getPackSubstitutedTemplateArgument(getSema(), Arg);
1399f4a2713aSLionel Sambuc }
1400f4a2713aSLionel Sambuc
1401f4a2713aSLionel Sambuc assert(Arg.getKind() == TemplateArgument::Type &&
1402f4a2713aSLionel Sambuc "Template argument kind mismatch");
1403f4a2713aSLionel Sambuc
1404f4a2713aSLionel Sambuc QualType Replacement = Arg.getAsType();
1405f4a2713aSLionel Sambuc
1406f4a2713aSLionel Sambuc // TODO: only do this uniquing once, at the start of instantiation.
1407f4a2713aSLionel Sambuc QualType Result
1408f4a2713aSLionel Sambuc = getSema().Context.getSubstTemplateTypeParmType(T, Replacement);
1409f4a2713aSLionel Sambuc SubstTemplateTypeParmTypeLoc NewTL
1410f4a2713aSLionel Sambuc = TLB.push<SubstTemplateTypeParmTypeLoc>(Result);
1411f4a2713aSLionel Sambuc NewTL.setNameLoc(TL.getNameLoc());
1412f4a2713aSLionel Sambuc return Result;
1413f4a2713aSLionel Sambuc }
1414f4a2713aSLionel Sambuc
1415f4a2713aSLionel Sambuc // The template type parameter comes from an inner template (e.g.,
1416f4a2713aSLionel Sambuc // the template parameter list of a member template inside the
1417f4a2713aSLionel Sambuc // template we are instantiating). Create a new template type
1418f4a2713aSLionel Sambuc // parameter with the template "level" reduced by one.
1419*0a6a1f1dSLionel Sambuc TemplateTypeParmDecl *NewTTPDecl = nullptr;
1420f4a2713aSLionel Sambuc if (TemplateTypeParmDecl *OldTTPDecl = T->getDecl())
1421f4a2713aSLionel Sambuc NewTTPDecl = cast_or_null<TemplateTypeParmDecl>(
1422f4a2713aSLionel Sambuc TransformDecl(TL.getNameLoc(), OldTTPDecl));
1423f4a2713aSLionel Sambuc
1424f4a2713aSLionel Sambuc QualType Result
1425f4a2713aSLionel Sambuc = getSema().Context.getTemplateTypeParmType(T->getDepth()
1426f4a2713aSLionel Sambuc - TemplateArgs.getNumLevels(),
1427f4a2713aSLionel Sambuc T->getIndex(),
1428f4a2713aSLionel Sambuc T->isParameterPack(),
1429f4a2713aSLionel Sambuc NewTTPDecl);
1430f4a2713aSLionel Sambuc TemplateTypeParmTypeLoc NewTL = TLB.push<TemplateTypeParmTypeLoc>(Result);
1431f4a2713aSLionel Sambuc NewTL.setNameLoc(TL.getNameLoc());
1432f4a2713aSLionel Sambuc return Result;
1433f4a2713aSLionel Sambuc }
1434f4a2713aSLionel Sambuc
1435f4a2713aSLionel Sambuc QualType
TransformSubstTemplateTypeParmPackType(TypeLocBuilder & TLB,SubstTemplateTypeParmPackTypeLoc TL)1436f4a2713aSLionel Sambuc TemplateInstantiator::TransformSubstTemplateTypeParmPackType(
1437f4a2713aSLionel Sambuc TypeLocBuilder &TLB,
1438f4a2713aSLionel Sambuc SubstTemplateTypeParmPackTypeLoc TL) {
1439f4a2713aSLionel Sambuc if (getSema().ArgumentPackSubstitutionIndex == -1) {
1440f4a2713aSLionel Sambuc // We aren't expanding the parameter pack, so just return ourselves.
1441f4a2713aSLionel Sambuc SubstTemplateTypeParmPackTypeLoc NewTL
1442f4a2713aSLionel Sambuc = TLB.push<SubstTemplateTypeParmPackTypeLoc>(TL.getType());
1443f4a2713aSLionel Sambuc NewTL.setNameLoc(TL.getNameLoc());
1444f4a2713aSLionel Sambuc return TL.getType();
1445f4a2713aSLionel Sambuc }
1446f4a2713aSLionel Sambuc
1447f4a2713aSLionel Sambuc TemplateArgument Arg = TL.getTypePtr()->getArgumentPack();
1448f4a2713aSLionel Sambuc Arg = getPackSubstitutedTemplateArgument(getSema(), Arg);
1449f4a2713aSLionel Sambuc QualType Result = Arg.getAsType();
1450f4a2713aSLionel Sambuc
1451f4a2713aSLionel Sambuc Result = getSema().Context.getSubstTemplateTypeParmType(
1452f4a2713aSLionel Sambuc TL.getTypePtr()->getReplacedParameter(),
1453f4a2713aSLionel Sambuc Result);
1454f4a2713aSLionel Sambuc SubstTemplateTypeParmTypeLoc NewTL
1455f4a2713aSLionel Sambuc = TLB.push<SubstTemplateTypeParmTypeLoc>(Result);
1456f4a2713aSLionel Sambuc NewTL.setNameLoc(TL.getNameLoc());
1457f4a2713aSLionel Sambuc return Result;
1458f4a2713aSLionel Sambuc }
1459f4a2713aSLionel Sambuc
1460f4a2713aSLionel Sambuc /// \brief Perform substitution on the type T with a given set of template
1461f4a2713aSLionel Sambuc /// arguments.
1462f4a2713aSLionel Sambuc ///
1463f4a2713aSLionel Sambuc /// This routine substitutes the given template arguments into the
1464f4a2713aSLionel Sambuc /// type T and produces the instantiated type.
1465f4a2713aSLionel Sambuc ///
1466f4a2713aSLionel Sambuc /// \param T the type into which the template arguments will be
1467f4a2713aSLionel Sambuc /// substituted. If this type is not dependent, it will be returned
1468f4a2713aSLionel Sambuc /// immediately.
1469f4a2713aSLionel Sambuc ///
1470f4a2713aSLionel Sambuc /// \param Args the template arguments that will be
1471f4a2713aSLionel Sambuc /// substituted for the top-level template parameters within T.
1472f4a2713aSLionel Sambuc ///
1473f4a2713aSLionel Sambuc /// \param Loc the location in the source code where this substitution
1474f4a2713aSLionel Sambuc /// is being performed. It will typically be the location of the
1475f4a2713aSLionel Sambuc /// declarator (if we're instantiating the type of some declaration)
1476f4a2713aSLionel Sambuc /// or the location of the type in the source code (if, e.g., we're
1477f4a2713aSLionel Sambuc /// instantiating the type of a cast expression).
1478f4a2713aSLionel Sambuc ///
1479f4a2713aSLionel Sambuc /// \param Entity the name of the entity associated with a declaration
1480f4a2713aSLionel Sambuc /// being instantiated (if any). May be empty to indicate that there
1481f4a2713aSLionel Sambuc /// is no such entity (if, e.g., this is a type that occurs as part of
1482f4a2713aSLionel Sambuc /// a cast expression) or that the entity has no name (e.g., an
1483f4a2713aSLionel Sambuc /// unnamed function parameter).
1484f4a2713aSLionel Sambuc ///
1485f4a2713aSLionel Sambuc /// \returns If the instantiation succeeds, the instantiated
1486f4a2713aSLionel Sambuc /// type. Otherwise, produces diagnostics and returns a NULL type.
SubstType(TypeSourceInfo * T,const MultiLevelTemplateArgumentList & Args,SourceLocation Loc,DeclarationName Entity)1487f4a2713aSLionel Sambuc TypeSourceInfo *Sema::SubstType(TypeSourceInfo *T,
1488f4a2713aSLionel Sambuc const MultiLevelTemplateArgumentList &Args,
1489f4a2713aSLionel Sambuc SourceLocation Loc,
1490f4a2713aSLionel Sambuc DeclarationName Entity) {
1491f4a2713aSLionel Sambuc assert(!ActiveTemplateInstantiations.empty() &&
1492f4a2713aSLionel Sambuc "Cannot perform an instantiation without some context on the "
1493f4a2713aSLionel Sambuc "instantiation stack");
1494f4a2713aSLionel Sambuc
1495f4a2713aSLionel Sambuc if (!T->getType()->isInstantiationDependentType() &&
1496f4a2713aSLionel Sambuc !T->getType()->isVariablyModifiedType())
1497f4a2713aSLionel Sambuc return T;
1498f4a2713aSLionel Sambuc
1499f4a2713aSLionel Sambuc TemplateInstantiator Instantiator(*this, Args, Loc, Entity);
1500f4a2713aSLionel Sambuc return Instantiator.TransformType(T);
1501f4a2713aSLionel Sambuc }
1502f4a2713aSLionel Sambuc
SubstType(TypeLoc TL,const MultiLevelTemplateArgumentList & Args,SourceLocation Loc,DeclarationName Entity)1503f4a2713aSLionel Sambuc TypeSourceInfo *Sema::SubstType(TypeLoc TL,
1504f4a2713aSLionel Sambuc const MultiLevelTemplateArgumentList &Args,
1505f4a2713aSLionel Sambuc SourceLocation Loc,
1506f4a2713aSLionel Sambuc DeclarationName Entity) {
1507f4a2713aSLionel Sambuc assert(!ActiveTemplateInstantiations.empty() &&
1508f4a2713aSLionel Sambuc "Cannot perform an instantiation without some context on the "
1509f4a2713aSLionel Sambuc "instantiation stack");
1510f4a2713aSLionel Sambuc
1511f4a2713aSLionel Sambuc if (TL.getType().isNull())
1512*0a6a1f1dSLionel Sambuc return nullptr;
1513f4a2713aSLionel Sambuc
1514f4a2713aSLionel Sambuc if (!TL.getType()->isInstantiationDependentType() &&
1515f4a2713aSLionel Sambuc !TL.getType()->isVariablyModifiedType()) {
1516f4a2713aSLionel Sambuc // FIXME: Make a copy of the TypeLoc data here, so that we can
1517f4a2713aSLionel Sambuc // return a new TypeSourceInfo. Inefficient!
1518f4a2713aSLionel Sambuc TypeLocBuilder TLB;
1519f4a2713aSLionel Sambuc TLB.pushFullCopy(TL);
1520f4a2713aSLionel Sambuc return TLB.getTypeSourceInfo(Context, TL.getType());
1521f4a2713aSLionel Sambuc }
1522f4a2713aSLionel Sambuc
1523f4a2713aSLionel Sambuc TemplateInstantiator Instantiator(*this, Args, Loc, Entity);
1524f4a2713aSLionel Sambuc TypeLocBuilder TLB;
1525f4a2713aSLionel Sambuc TLB.reserve(TL.getFullDataSize());
1526f4a2713aSLionel Sambuc QualType Result = Instantiator.TransformType(TLB, TL);
1527f4a2713aSLionel Sambuc if (Result.isNull())
1528*0a6a1f1dSLionel Sambuc return nullptr;
1529f4a2713aSLionel Sambuc
1530f4a2713aSLionel Sambuc return TLB.getTypeSourceInfo(Context, Result);
1531f4a2713aSLionel Sambuc }
1532f4a2713aSLionel Sambuc
1533f4a2713aSLionel Sambuc /// Deprecated form of the above.
SubstType(QualType T,const MultiLevelTemplateArgumentList & TemplateArgs,SourceLocation Loc,DeclarationName Entity)1534f4a2713aSLionel Sambuc QualType Sema::SubstType(QualType T,
1535f4a2713aSLionel Sambuc const MultiLevelTemplateArgumentList &TemplateArgs,
1536f4a2713aSLionel Sambuc SourceLocation Loc, DeclarationName Entity) {
1537f4a2713aSLionel Sambuc assert(!ActiveTemplateInstantiations.empty() &&
1538f4a2713aSLionel Sambuc "Cannot perform an instantiation without some context on the "
1539f4a2713aSLionel Sambuc "instantiation stack");
1540f4a2713aSLionel Sambuc
1541f4a2713aSLionel Sambuc // If T is not a dependent type or a variably-modified type, there
1542f4a2713aSLionel Sambuc // is nothing to do.
1543f4a2713aSLionel Sambuc if (!T->isInstantiationDependentType() && !T->isVariablyModifiedType())
1544f4a2713aSLionel Sambuc return T;
1545f4a2713aSLionel Sambuc
1546f4a2713aSLionel Sambuc TemplateInstantiator Instantiator(*this, TemplateArgs, Loc, Entity);
1547f4a2713aSLionel Sambuc return Instantiator.TransformType(T);
1548f4a2713aSLionel Sambuc }
1549f4a2713aSLionel Sambuc
NeedsInstantiationAsFunctionType(TypeSourceInfo * T)1550f4a2713aSLionel Sambuc static bool NeedsInstantiationAsFunctionType(TypeSourceInfo *T) {
1551f4a2713aSLionel Sambuc if (T->getType()->isInstantiationDependentType() ||
1552f4a2713aSLionel Sambuc T->getType()->isVariablyModifiedType())
1553f4a2713aSLionel Sambuc return true;
1554f4a2713aSLionel Sambuc
1555f4a2713aSLionel Sambuc TypeLoc TL = T->getTypeLoc().IgnoreParens();
1556f4a2713aSLionel Sambuc if (!TL.getAs<FunctionProtoTypeLoc>())
1557f4a2713aSLionel Sambuc return false;
1558f4a2713aSLionel Sambuc
1559f4a2713aSLionel Sambuc FunctionProtoTypeLoc FP = TL.castAs<FunctionProtoTypeLoc>();
1560*0a6a1f1dSLionel Sambuc for (unsigned I = 0, E = FP.getNumParams(); I != E; ++I) {
1561*0a6a1f1dSLionel Sambuc ParmVarDecl *P = FP.getParam(I);
1562f4a2713aSLionel Sambuc
1563f4a2713aSLionel Sambuc // This must be synthesized from a typedef.
1564f4a2713aSLionel Sambuc if (!P) continue;
1565f4a2713aSLionel Sambuc
1566f4a2713aSLionel Sambuc // The parameter's type as written might be dependent even if the
1567f4a2713aSLionel Sambuc // decayed type was not dependent.
1568f4a2713aSLionel Sambuc if (TypeSourceInfo *TSInfo = P->getTypeSourceInfo())
1569f4a2713aSLionel Sambuc if (TSInfo->getType()->isInstantiationDependentType())
1570f4a2713aSLionel Sambuc return true;
1571f4a2713aSLionel Sambuc
1572f4a2713aSLionel Sambuc // TODO: currently we always rebuild expressions. When we
1573f4a2713aSLionel Sambuc // properly get lazier about this, we should use the same
1574f4a2713aSLionel Sambuc // logic to avoid rebuilding prototypes here.
1575f4a2713aSLionel Sambuc if (P->hasDefaultArg())
1576f4a2713aSLionel Sambuc return true;
1577f4a2713aSLionel Sambuc }
1578f4a2713aSLionel Sambuc
1579f4a2713aSLionel Sambuc return false;
1580f4a2713aSLionel Sambuc }
1581f4a2713aSLionel Sambuc
1582f4a2713aSLionel Sambuc /// A form of SubstType intended specifically for instantiating the
1583f4a2713aSLionel Sambuc /// type of a FunctionDecl. Its purpose is solely to force the
1584*0a6a1f1dSLionel Sambuc /// instantiation of default-argument expressions and to avoid
1585*0a6a1f1dSLionel Sambuc /// instantiating an exception-specification.
SubstFunctionDeclType(TypeSourceInfo * T,const MultiLevelTemplateArgumentList & Args,SourceLocation Loc,DeclarationName Entity,CXXRecordDecl * ThisContext,unsigned ThisTypeQuals)1586f4a2713aSLionel Sambuc TypeSourceInfo *Sema::SubstFunctionDeclType(TypeSourceInfo *T,
1587f4a2713aSLionel Sambuc const MultiLevelTemplateArgumentList &Args,
1588f4a2713aSLionel Sambuc SourceLocation Loc,
1589f4a2713aSLionel Sambuc DeclarationName Entity,
1590f4a2713aSLionel Sambuc CXXRecordDecl *ThisContext,
1591f4a2713aSLionel Sambuc unsigned ThisTypeQuals) {
1592f4a2713aSLionel Sambuc assert(!ActiveTemplateInstantiations.empty() &&
1593f4a2713aSLionel Sambuc "Cannot perform an instantiation without some context on the "
1594f4a2713aSLionel Sambuc "instantiation stack");
1595f4a2713aSLionel Sambuc
1596f4a2713aSLionel Sambuc if (!NeedsInstantiationAsFunctionType(T))
1597f4a2713aSLionel Sambuc return T;
1598f4a2713aSLionel Sambuc
1599f4a2713aSLionel Sambuc TemplateInstantiator Instantiator(*this, Args, Loc, Entity);
1600f4a2713aSLionel Sambuc
1601f4a2713aSLionel Sambuc TypeLocBuilder TLB;
1602f4a2713aSLionel Sambuc
1603f4a2713aSLionel Sambuc TypeLoc TL = T->getTypeLoc();
1604f4a2713aSLionel Sambuc TLB.reserve(TL.getFullDataSize());
1605f4a2713aSLionel Sambuc
1606f4a2713aSLionel Sambuc QualType Result;
1607f4a2713aSLionel Sambuc
1608*0a6a1f1dSLionel Sambuc if (FunctionProtoTypeLoc Proto =
1609*0a6a1f1dSLionel Sambuc TL.IgnoreParens().getAs<FunctionProtoTypeLoc>()) {
1610*0a6a1f1dSLionel Sambuc // Instantiate the type, other than its exception specification. The
1611*0a6a1f1dSLionel Sambuc // exception specification is instantiated in InitFunctionInstantiation
1612*0a6a1f1dSLionel Sambuc // once we've built the FunctionDecl.
1613*0a6a1f1dSLionel Sambuc // FIXME: Set the exception specification to EST_Uninstantiated here,
1614*0a6a1f1dSLionel Sambuc // instead of rebuilding the function type again later.
1615*0a6a1f1dSLionel Sambuc Result = Instantiator.TransformFunctionProtoType(
1616*0a6a1f1dSLionel Sambuc TLB, Proto, ThisContext, ThisTypeQuals,
1617*0a6a1f1dSLionel Sambuc [](FunctionProtoType::ExceptionSpecInfo &ESI,
1618*0a6a1f1dSLionel Sambuc bool &Changed) { return false; });
1619f4a2713aSLionel Sambuc } else {
1620f4a2713aSLionel Sambuc Result = Instantiator.TransformType(TLB, TL);
1621f4a2713aSLionel Sambuc }
1622f4a2713aSLionel Sambuc if (Result.isNull())
1623*0a6a1f1dSLionel Sambuc return nullptr;
1624f4a2713aSLionel Sambuc
1625f4a2713aSLionel Sambuc return TLB.getTypeSourceInfo(Context, Result);
1626f4a2713aSLionel Sambuc }
1627f4a2713aSLionel Sambuc
SubstExceptionSpec(FunctionDecl * New,const FunctionProtoType * Proto,const MultiLevelTemplateArgumentList & Args)1628*0a6a1f1dSLionel Sambuc void Sema::SubstExceptionSpec(FunctionDecl *New, const FunctionProtoType *Proto,
1629*0a6a1f1dSLionel Sambuc const MultiLevelTemplateArgumentList &Args) {
1630*0a6a1f1dSLionel Sambuc FunctionProtoType::ExceptionSpecInfo ESI =
1631*0a6a1f1dSLionel Sambuc Proto->getExtProtoInfo().ExceptionSpec;
1632*0a6a1f1dSLionel Sambuc assert(ESI.Type != EST_Uninstantiated);
1633*0a6a1f1dSLionel Sambuc
1634*0a6a1f1dSLionel Sambuc TemplateInstantiator Instantiator(*this, Args, New->getLocation(),
1635*0a6a1f1dSLionel Sambuc New->getDeclName());
1636*0a6a1f1dSLionel Sambuc
1637*0a6a1f1dSLionel Sambuc SmallVector<QualType, 4> ExceptionStorage;
1638*0a6a1f1dSLionel Sambuc bool Changed = false;
1639*0a6a1f1dSLionel Sambuc if (Instantiator.TransformExceptionSpec(
1640*0a6a1f1dSLionel Sambuc New->getTypeSourceInfo()->getTypeLoc().getLocEnd(), ESI,
1641*0a6a1f1dSLionel Sambuc ExceptionStorage, Changed))
1642*0a6a1f1dSLionel Sambuc // On error, recover by dropping the exception specification.
1643*0a6a1f1dSLionel Sambuc ESI.Type = EST_None;
1644*0a6a1f1dSLionel Sambuc
1645*0a6a1f1dSLionel Sambuc UpdateExceptionSpec(New, ESI);
1646*0a6a1f1dSLionel Sambuc }
1647*0a6a1f1dSLionel Sambuc
SubstParmVarDecl(ParmVarDecl * OldParm,const MultiLevelTemplateArgumentList & TemplateArgs,int indexAdjustment,Optional<unsigned> NumExpansions,bool ExpectParameterPack)1648f4a2713aSLionel Sambuc ParmVarDecl *Sema::SubstParmVarDecl(ParmVarDecl *OldParm,
1649f4a2713aSLionel Sambuc const MultiLevelTemplateArgumentList &TemplateArgs,
1650f4a2713aSLionel Sambuc int indexAdjustment,
1651f4a2713aSLionel Sambuc Optional<unsigned> NumExpansions,
1652f4a2713aSLionel Sambuc bool ExpectParameterPack) {
1653f4a2713aSLionel Sambuc TypeSourceInfo *OldDI = OldParm->getTypeSourceInfo();
1654*0a6a1f1dSLionel Sambuc TypeSourceInfo *NewDI = nullptr;
1655f4a2713aSLionel Sambuc
1656f4a2713aSLionel Sambuc TypeLoc OldTL = OldDI->getTypeLoc();
1657f4a2713aSLionel Sambuc if (PackExpansionTypeLoc ExpansionTL = OldTL.getAs<PackExpansionTypeLoc>()) {
1658f4a2713aSLionel Sambuc
1659f4a2713aSLionel Sambuc // We have a function parameter pack. Substitute into the pattern of the
1660f4a2713aSLionel Sambuc // expansion.
1661f4a2713aSLionel Sambuc NewDI = SubstType(ExpansionTL.getPatternLoc(), TemplateArgs,
1662f4a2713aSLionel Sambuc OldParm->getLocation(), OldParm->getDeclName());
1663f4a2713aSLionel Sambuc if (!NewDI)
1664*0a6a1f1dSLionel Sambuc return nullptr;
1665f4a2713aSLionel Sambuc
1666f4a2713aSLionel Sambuc if (NewDI->getType()->containsUnexpandedParameterPack()) {
1667f4a2713aSLionel Sambuc // We still have unexpanded parameter packs, which means that
1668f4a2713aSLionel Sambuc // our function parameter is still a function parameter pack.
1669f4a2713aSLionel Sambuc // Therefore, make its type a pack expansion type.
1670f4a2713aSLionel Sambuc NewDI = CheckPackExpansion(NewDI, ExpansionTL.getEllipsisLoc(),
1671f4a2713aSLionel Sambuc NumExpansions);
1672f4a2713aSLionel Sambuc } else if (ExpectParameterPack) {
1673f4a2713aSLionel Sambuc // We expected to get a parameter pack but didn't (because the type
1674f4a2713aSLionel Sambuc // itself is not a pack expansion type), so complain. This can occur when
1675f4a2713aSLionel Sambuc // the substitution goes through an alias template that "loses" the
1676f4a2713aSLionel Sambuc // pack expansion.
1677f4a2713aSLionel Sambuc Diag(OldParm->getLocation(),
1678f4a2713aSLionel Sambuc diag::err_function_parameter_pack_without_parameter_packs)
1679f4a2713aSLionel Sambuc << NewDI->getType();
1680*0a6a1f1dSLionel Sambuc return nullptr;
1681f4a2713aSLionel Sambuc }
1682f4a2713aSLionel Sambuc } else {
1683f4a2713aSLionel Sambuc NewDI = SubstType(OldDI, TemplateArgs, OldParm->getLocation(),
1684f4a2713aSLionel Sambuc OldParm->getDeclName());
1685f4a2713aSLionel Sambuc }
1686f4a2713aSLionel Sambuc
1687f4a2713aSLionel Sambuc if (!NewDI)
1688*0a6a1f1dSLionel Sambuc return nullptr;
1689f4a2713aSLionel Sambuc
1690f4a2713aSLionel Sambuc if (NewDI->getType()->isVoidType()) {
1691f4a2713aSLionel Sambuc Diag(OldParm->getLocation(), diag::err_param_with_void_type);
1692*0a6a1f1dSLionel Sambuc return nullptr;
1693f4a2713aSLionel Sambuc }
1694f4a2713aSLionel Sambuc
1695f4a2713aSLionel Sambuc ParmVarDecl *NewParm = CheckParameter(Context.getTranslationUnitDecl(),
1696f4a2713aSLionel Sambuc OldParm->getInnerLocStart(),
1697f4a2713aSLionel Sambuc OldParm->getLocation(),
1698f4a2713aSLionel Sambuc OldParm->getIdentifier(),
1699f4a2713aSLionel Sambuc NewDI->getType(), NewDI,
1700f4a2713aSLionel Sambuc OldParm->getStorageClass());
1701f4a2713aSLionel Sambuc if (!NewParm)
1702*0a6a1f1dSLionel Sambuc return nullptr;
1703f4a2713aSLionel Sambuc
1704f4a2713aSLionel Sambuc // Mark the (new) default argument as uninstantiated (if any).
1705f4a2713aSLionel Sambuc if (OldParm->hasUninstantiatedDefaultArg()) {
1706f4a2713aSLionel Sambuc Expr *Arg = OldParm->getUninstantiatedDefaultArg();
1707f4a2713aSLionel Sambuc NewParm->setUninstantiatedDefaultArg(Arg);
1708f4a2713aSLionel Sambuc } else if (OldParm->hasUnparsedDefaultArg()) {
1709f4a2713aSLionel Sambuc NewParm->setUnparsedDefaultArg();
1710f4a2713aSLionel Sambuc UnparsedDefaultArgInstantiations[OldParm].push_back(NewParm);
1711f4a2713aSLionel Sambuc } else if (Expr *Arg = OldParm->getDefaultArg())
1712f4a2713aSLionel Sambuc // FIXME: if we non-lazily instantiated non-dependent default args for
1713f4a2713aSLionel Sambuc // non-dependent parameter types we could remove a bunch of duplicate
1714f4a2713aSLionel Sambuc // conversion warnings for such arguments.
1715f4a2713aSLionel Sambuc NewParm->setUninstantiatedDefaultArg(Arg);
1716f4a2713aSLionel Sambuc
1717f4a2713aSLionel Sambuc NewParm->setHasInheritedDefaultArg(OldParm->hasInheritedDefaultArg());
1718f4a2713aSLionel Sambuc
1719f4a2713aSLionel Sambuc if (OldParm->isParameterPack() && !NewParm->isParameterPack()) {
1720f4a2713aSLionel Sambuc // Add the new parameter to the instantiated parameter pack.
1721f4a2713aSLionel Sambuc CurrentInstantiationScope->InstantiatedLocalPackArg(OldParm, NewParm);
1722f4a2713aSLionel Sambuc } else {
1723f4a2713aSLionel Sambuc // Introduce an Old -> New mapping
1724f4a2713aSLionel Sambuc CurrentInstantiationScope->InstantiatedLocal(OldParm, NewParm);
1725f4a2713aSLionel Sambuc }
1726f4a2713aSLionel Sambuc
1727f4a2713aSLionel Sambuc // FIXME: OldParm may come from a FunctionProtoType, in which case CurContext
1728f4a2713aSLionel Sambuc // can be anything, is this right ?
1729f4a2713aSLionel Sambuc NewParm->setDeclContext(CurContext);
1730f4a2713aSLionel Sambuc
1731f4a2713aSLionel Sambuc NewParm->setScopeInfo(OldParm->getFunctionScopeDepth(),
1732f4a2713aSLionel Sambuc OldParm->getFunctionScopeIndex() + indexAdjustment);
1733f4a2713aSLionel Sambuc
1734f4a2713aSLionel Sambuc InstantiateAttrs(TemplateArgs, OldParm, NewParm);
1735f4a2713aSLionel Sambuc
1736f4a2713aSLionel Sambuc return NewParm;
1737f4a2713aSLionel Sambuc }
1738f4a2713aSLionel Sambuc
1739f4a2713aSLionel Sambuc /// \brief Substitute the given template arguments into the given set of
1740f4a2713aSLionel Sambuc /// parameters, producing the set of parameter types that would be generated
1741f4a2713aSLionel Sambuc /// from such a substitution.
SubstParmTypes(SourceLocation Loc,ParmVarDecl ** Params,unsigned NumParams,const MultiLevelTemplateArgumentList & TemplateArgs,SmallVectorImpl<QualType> & ParamTypes,SmallVectorImpl<ParmVarDecl * > * OutParams)1742f4a2713aSLionel Sambuc bool Sema::SubstParmTypes(SourceLocation Loc,
1743f4a2713aSLionel Sambuc ParmVarDecl **Params, unsigned NumParams,
1744f4a2713aSLionel Sambuc const MultiLevelTemplateArgumentList &TemplateArgs,
1745f4a2713aSLionel Sambuc SmallVectorImpl<QualType> &ParamTypes,
1746f4a2713aSLionel Sambuc SmallVectorImpl<ParmVarDecl *> *OutParams) {
1747f4a2713aSLionel Sambuc assert(!ActiveTemplateInstantiations.empty() &&
1748f4a2713aSLionel Sambuc "Cannot perform an instantiation without some context on the "
1749f4a2713aSLionel Sambuc "instantiation stack");
1750f4a2713aSLionel Sambuc
1751f4a2713aSLionel Sambuc TemplateInstantiator Instantiator(*this, TemplateArgs, Loc,
1752f4a2713aSLionel Sambuc DeclarationName());
1753*0a6a1f1dSLionel Sambuc return Instantiator.TransformFunctionTypeParams(Loc, Params, NumParams,
1754*0a6a1f1dSLionel Sambuc nullptr, ParamTypes,
1755*0a6a1f1dSLionel Sambuc OutParams);
1756f4a2713aSLionel Sambuc }
1757f4a2713aSLionel Sambuc
1758f4a2713aSLionel Sambuc /// \brief Perform substitution on the base class specifiers of the
1759f4a2713aSLionel Sambuc /// given class template specialization.
1760f4a2713aSLionel Sambuc ///
1761f4a2713aSLionel Sambuc /// Produces a diagnostic and returns true on error, returns false and
1762f4a2713aSLionel Sambuc /// attaches the instantiated base classes to the class template
1763f4a2713aSLionel Sambuc /// specialization if successful.
1764f4a2713aSLionel Sambuc bool
SubstBaseSpecifiers(CXXRecordDecl * Instantiation,CXXRecordDecl * Pattern,const MultiLevelTemplateArgumentList & TemplateArgs)1765f4a2713aSLionel Sambuc Sema::SubstBaseSpecifiers(CXXRecordDecl *Instantiation,
1766f4a2713aSLionel Sambuc CXXRecordDecl *Pattern,
1767f4a2713aSLionel Sambuc const MultiLevelTemplateArgumentList &TemplateArgs) {
1768f4a2713aSLionel Sambuc bool Invalid = false;
1769f4a2713aSLionel Sambuc SmallVector<CXXBaseSpecifier*, 4> InstantiatedBases;
1770*0a6a1f1dSLionel Sambuc for (const auto Base : Pattern->bases()) {
1771*0a6a1f1dSLionel Sambuc if (!Base.getType()->isDependentType()) {
1772*0a6a1f1dSLionel Sambuc if (const CXXRecordDecl *RD = Base.getType()->getAsCXXRecordDecl()) {
1773f4a2713aSLionel Sambuc if (RD->isInvalidDecl())
1774f4a2713aSLionel Sambuc Instantiation->setInvalidDecl();
1775f4a2713aSLionel Sambuc }
1776*0a6a1f1dSLionel Sambuc InstantiatedBases.push_back(new (Context) CXXBaseSpecifier(Base));
1777f4a2713aSLionel Sambuc continue;
1778f4a2713aSLionel Sambuc }
1779f4a2713aSLionel Sambuc
1780f4a2713aSLionel Sambuc SourceLocation EllipsisLoc;
1781f4a2713aSLionel Sambuc TypeSourceInfo *BaseTypeLoc;
1782*0a6a1f1dSLionel Sambuc if (Base.isPackExpansion()) {
1783f4a2713aSLionel Sambuc // This is a pack expansion. See whether we should expand it now, or
1784f4a2713aSLionel Sambuc // wait until later.
1785f4a2713aSLionel Sambuc SmallVector<UnexpandedParameterPack, 2> Unexpanded;
1786*0a6a1f1dSLionel Sambuc collectUnexpandedParameterPacks(Base.getTypeSourceInfo()->getTypeLoc(),
1787f4a2713aSLionel Sambuc Unexpanded);
1788f4a2713aSLionel Sambuc bool ShouldExpand = false;
1789f4a2713aSLionel Sambuc bool RetainExpansion = false;
1790f4a2713aSLionel Sambuc Optional<unsigned> NumExpansions;
1791*0a6a1f1dSLionel Sambuc if (CheckParameterPacksForExpansion(Base.getEllipsisLoc(),
1792*0a6a1f1dSLionel Sambuc Base.getSourceRange(),
1793f4a2713aSLionel Sambuc Unexpanded,
1794f4a2713aSLionel Sambuc TemplateArgs, ShouldExpand,
1795f4a2713aSLionel Sambuc RetainExpansion,
1796f4a2713aSLionel Sambuc NumExpansions)) {
1797f4a2713aSLionel Sambuc Invalid = true;
1798f4a2713aSLionel Sambuc continue;
1799f4a2713aSLionel Sambuc }
1800f4a2713aSLionel Sambuc
1801f4a2713aSLionel Sambuc // If we should expand this pack expansion now, do so.
1802f4a2713aSLionel Sambuc if (ShouldExpand) {
1803f4a2713aSLionel Sambuc for (unsigned I = 0; I != *NumExpansions; ++I) {
1804f4a2713aSLionel Sambuc Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(*this, I);
1805f4a2713aSLionel Sambuc
1806*0a6a1f1dSLionel Sambuc TypeSourceInfo *BaseTypeLoc = SubstType(Base.getTypeSourceInfo(),
1807f4a2713aSLionel Sambuc TemplateArgs,
1808*0a6a1f1dSLionel Sambuc Base.getSourceRange().getBegin(),
1809f4a2713aSLionel Sambuc DeclarationName());
1810f4a2713aSLionel Sambuc if (!BaseTypeLoc) {
1811f4a2713aSLionel Sambuc Invalid = true;
1812f4a2713aSLionel Sambuc continue;
1813f4a2713aSLionel Sambuc }
1814f4a2713aSLionel Sambuc
1815f4a2713aSLionel Sambuc if (CXXBaseSpecifier *InstantiatedBase
1816f4a2713aSLionel Sambuc = CheckBaseSpecifier(Instantiation,
1817*0a6a1f1dSLionel Sambuc Base.getSourceRange(),
1818*0a6a1f1dSLionel Sambuc Base.isVirtual(),
1819*0a6a1f1dSLionel Sambuc Base.getAccessSpecifierAsWritten(),
1820f4a2713aSLionel Sambuc BaseTypeLoc,
1821f4a2713aSLionel Sambuc SourceLocation()))
1822f4a2713aSLionel Sambuc InstantiatedBases.push_back(InstantiatedBase);
1823f4a2713aSLionel Sambuc else
1824f4a2713aSLionel Sambuc Invalid = true;
1825f4a2713aSLionel Sambuc }
1826f4a2713aSLionel Sambuc
1827f4a2713aSLionel Sambuc continue;
1828f4a2713aSLionel Sambuc }
1829f4a2713aSLionel Sambuc
1830f4a2713aSLionel Sambuc // The resulting base specifier will (still) be a pack expansion.
1831*0a6a1f1dSLionel Sambuc EllipsisLoc = Base.getEllipsisLoc();
1832f4a2713aSLionel Sambuc Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(*this, -1);
1833*0a6a1f1dSLionel Sambuc BaseTypeLoc = SubstType(Base.getTypeSourceInfo(),
1834f4a2713aSLionel Sambuc TemplateArgs,
1835*0a6a1f1dSLionel Sambuc Base.getSourceRange().getBegin(),
1836f4a2713aSLionel Sambuc DeclarationName());
1837f4a2713aSLionel Sambuc } else {
1838*0a6a1f1dSLionel Sambuc BaseTypeLoc = SubstType(Base.getTypeSourceInfo(),
1839f4a2713aSLionel Sambuc TemplateArgs,
1840*0a6a1f1dSLionel Sambuc Base.getSourceRange().getBegin(),
1841f4a2713aSLionel Sambuc DeclarationName());
1842f4a2713aSLionel Sambuc }
1843f4a2713aSLionel Sambuc
1844f4a2713aSLionel Sambuc if (!BaseTypeLoc) {
1845f4a2713aSLionel Sambuc Invalid = true;
1846f4a2713aSLionel Sambuc continue;
1847f4a2713aSLionel Sambuc }
1848f4a2713aSLionel Sambuc
1849f4a2713aSLionel Sambuc if (CXXBaseSpecifier *InstantiatedBase
1850f4a2713aSLionel Sambuc = CheckBaseSpecifier(Instantiation,
1851*0a6a1f1dSLionel Sambuc Base.getSourceRange(),
1852*0a6a1f1dSLionel Sambuc Base.isVirtual(),
1853*0a6a1f1dSLionel Sambuc Base.getAccessSpecifierAsWritten(),
1854f4a2713aSLionel Sambuc BaseTypeLoc,
1855f4a2713aSLionel Sambuc EllipsisLoc))
1856f4a2713aSLionel Sambuc InstantiatedBases.push_back(InstantiatedBase);
1857f4a2713aSLionel Sambuc else
1858f4a2713aSLionel Sambuc Invalid = true;
1859f4a2713aSLionel Sambuc }
1860f4a2713aSLionel Sambuc
1861f4a2713aSLionel Sambuc if (!Invalid &&
1862f4a2713aSLionel Sambuc AttachBaseSpecifiers(Instantiation, InstantiatedBases.data(),
1863f4a2713aSLionel Sambuc InstantiatedBases.size()))
1864f4a2713aSLionel Sambuc Invalid = true;
1865f4a2713aSLionel Sambuc
1866f4a2713aSLionel Sambuc return Invalid;
1867f4a2713aSLionel Sambuc }
1868f4a2713aSLionel Sambuc
1869f4a2713aSLionel Sambuc // Defined via #include from SemaTemplateInstantiateDecl.cpp
1870f4a2713aSLionel Sambuc namespace clang {
1871f4a2713aSLionel Sambuc namespace sema {
1872f4a2713aSLionel Sambuc Attr *instantiateTemplateAttribute(const Attr *At, ASTContext &C, Sema &S,
1873f4a2713aSLionel Sambuc const MultiLevelTemplateArgumentList &TemplateArgs);
1874f4a2713aSLionel Sambuc }
1875f4a2713aSLionel Sambuc }
1876f4a2713aSLionel Sambuc
1877f4a2713aSLionel Sambuc /// Determine whether we would be unable to instantiate this template (because
1878f4a2713aSLionel Sambuc /// it either has no definition, or is in the process of being instantiated).
DiagnoseUninstantiableTemplate(Sema & S,SourceLocation PointOfInstantiation,TagDecl * Instantiation,bool InstantiatedFromMember,TagDecl * Pattern,TagDecl * PatternDef,TemplateSpecializationKind TSK,bool Complain=true)1879f4a2713aSLionel Sambuc static bool DiagnoseUninstantiableTemplate(Sema &S,
1880f4a2713aSLionel Sambuc SourceLocation PointOfInstantiation,
1881f4a2713aSLionel Sambuc TagDecl *Instantiation,
1882f4a2713aSLionel Sambuc bool InstantiatedFromMember,
1883f4a2713aSLionel Sambuc TagDecl *Pattern,
1884f4a2713aSLionel Sambuc TagDecl *PatternDef,
1885f4a2713aSLionel Sambuc TemplateSpecializationKind TSK,
1886f4a2713aSLionel Sambuc bool Complain = true) {
1887f4a2713aSLionel Sambuc if (PatternDef && !PatternDef->isBeingDefined())
1888f4a2713aSLionel Sambuc return false;
1889f4a2713aSLionel Sambuc
1890f4a2713aSLionel Sambuc if (!Complain || (PatternDef && PatternDef->isInvalidDecl())) {
1891f4a2713aSLionel Sambuc // Say nothing
1892f4a2713aSLionel Sambuc } else if (PatternDef) {
1893f4a2713aSLionel Sambuc assert(PatternDef->isBeingDefined());
1894f4a2713aSLionel Sambuc S.Diag(PointOfInstantiation,
1895f4a2713aSLionel Sambuc diag::err_template_instantiate_within_definition)
1896f4a2713aSLionel Sambuc << (TSK != TSK_ImplicitInstantiation)
1897f4a2713aSLionel Sambuc << S.Context.getTypeDeclType(Instantiation);
1898f4a2713aSLionel Sambuc // Not much point in noting the template declaration here, since
1899f4a2713aSLionel Sambuc // we're lexically inside it.
1900f4a2713aSLionel Sambuc Instantiation->setInvalidDecl();
1901f4a2713aSLionel Sambuc } else if (InstantiatedFromMember) {
1902f4a2713aSLionel Sambuc S.Diag(PointOfInstantiation,
1903f4a2713aSLionel Sambuc diag::err_implicit_instantiate_member_undefined)
1904f4a2713aSLionel Sambuc << S.Context.getTypeDeclType(Instantiation);
1905*0a6a1f1dSLionel Sambuc S.Diag(Pattern->getLocation(), diag::note_member_declared_at);
1906f4a2713aSLionel Sambuc } else {
1907f4a2713aSLionel Sambuc S.Diag(PointOfInstantiation, diag::err_template_instantiate_undefined)
1908f4a2713aSLionel Sambuc << (TSK != TSK_ImplicitInstantiation)
1909f4a2713aSLionel Sambuc << S.Context.getTypeDeclType(Instantiation);
1910f4a2713aSLionel Sambuc S.Diag(Pattern->getLocation(), diag::note_template_decl_here);
1911f4a2713aSLionel Sambuc }
1912f4a2713aSLionel Sambuc
1913f4a2713aSLionel Sambuc // In general, Instantiation isn't marked invalid to get more than one
1914f4a2713aSLionel Sambuc // error for multiple undefined instantiations. But the code that does
1915f4a2713aSLionel Sambuc // explicit declaration -> explicit definition conversion can't handle
1916f4a2713aSLionel Sambuc // invalid declarations, so mark as invalid in that case.
1917f4a2713aSLionel Sambuc if (TSK == TSK_ExplicitInstantiationDeclaration)
1918f4a2713aSLionel Sambuc Instantiation->setInvalidDecl();
1919f4a2713aSLionel Sambuc return true;
1920f4a2713aSLionel Sambuc }
1921f4a2713aSLionel Sambuc
1922f4a2713aSLionel Sambuc /// \brief Instantiate the definition of a class from a given pattern.
1923f4a2713aSLionel Sambuc ///
1924f4a2713aSLionel Sambuc /// \param PointOfInstantiation The point of instantiation within the
1925f4a2713aSLionel Sambuc /// source code.
1926f4a2713aSLionel Sambuc ///
1927f4a2713aSLionel Sambuc /// \param Instantiation is the declaration whose definition is being
1928f4a2713aSLionel Sambuc /// instantiated. This will be either a class template specialization
1929f4a2713aSLionel Sambuc /// or a member class of a class template specialization.
1930f4a2713aSLionel Sambuc ///
1931f4a2713aSLionel Sambuc /// \param Pattern is the pattern from which the instantiation
1932f4a2713aSLionel Sambuc /// occurs. This will be either the declaration of a class template or
1933f4a2713aSLionel Sambuc /// the declaration of a member class of a class template.
1934f4a2713aSLionel Sambuc ///
1935f4a2713aSLionel Sambuc /// \param TemplateArgs The template arguments to be substituted into
1936f4a2713aSLionel Sambuc /// the pattern.
1937f4a2713aSLionel Sambuc ///
1938f4a2713aSLionel Sambuc /// \param TSK the kind of implicit or explicit instantiation to perform.
1939f4a2713aSLionel Sambuc ///
1940f4a2713aSLionel Sambuc /// \param Complain whether to complain if the class cannot be instantiated due
1941f4a2713aSLionel Sambuc /// to the lack of a definition.
1942f4a2713aSLionel Sambuc ///
1943f4a2713aSLionel Sambuc /// \returns true if an error occurred, false otherwise.
1944f4a2713aSLionel Sambuc bool
InstantiateClass(SourceLocation PointOfInstantiation,CXXRecordDecl * Instantiation,CXXRecordDecl * Pattern,const MultiLevelTemplateArgumentList & TemplateArgs,TemplateSpecializationKind TSK,bool Complain)1945f4a2713aSLionel Sambuc Sema::InstantiateClass(SourceLocation PointOfInstantiation,
1946f4a2713aSLionel Sambuc CXXRecordDecl *Instantiation, CXXRecordDecl *Pattern,
1947f4a2713aSLionel Sambuc const MultiLevelTemplateArgumentList &TemplateArgs,
1948f4a2713aSLionel Sambuc TemplateSpecializationKind TSK,
1949f4a2713aSLionel Sambuc bool Complain) {
1950f4a2713aSLionel Sambuc CXXRecordDecl *PatternDef
1951f4a2713aSLionel Sambuc = cast_or_null<CXXRecordDecl>(Pattern->getDefinition());
1952f4a2713aSLionel Sambuc if (DiagnoseUninstantiableTemplate(*this, PointOfInstantiation, Instantiation,
1953f4a2713aSLionel Sambuc Instantiation->getInstantiatedFromMemberClass(),
1954f4a2713aSLionel Sambuc Pattern, PatternDef, TSK, Complain))
1955f4a2713aSLionel Sambuc return true;
1956f4a2713aSLionel Sambuc Pattern = PatternDef;
1957f4a2713aSLionel Sambuc
1958f4a2713aSLionel Sambuc // \brief Record the point of instantiation.
1959f4a2713aSLionel Sambuc if (MemberSpecializationInfo *MSInfo
1960f4a2713aSLionel Sambuc = Instantiation->getMemberSpecializationInfo()) {
1961f4a2713aSLionel Sambuc MSInfo->setTemplateSpecializationKind(TSK);
1962f4a2713aSLionel Sambuc MSInfo->setPointOfInstantiation(PointOfInstantiation);
1963f4a2713aSLionel Sambuc } else if (ClassTemplateSpecializationDecl *Spec
1964f4a2713aSLionel Sambuc = dyn_cast<ClassTemplateSpecializationDecl>(Instantiation)) {
1965f4a2713aSLionel Sambuc Spec->setTemplateSpecializationKind(TSK);
1966f4a2713aSLionel Sambuc Spec->setPointOfInstantiation(PointOfInstantiation);
1967f4a2713aSLionel Sambuc }
1968f4a2713aSLionel Sambuc
1969f4a2713aSLionel Sambuc InstantiatingTemplate Inst(*this, PointOfInstantiation, Instantiation);
1970f4a2713aSLionel Sambuc if (Inst.isInvalid())
1971f4a2713aSLionel Sambuc return true;
1972f4a2713aSLionel Sambuc
1973f4a2713aSLionel Sambuc // Enter the scope of this instantiation. We don't use
1974f4a2713aSLionel Sambuc // PushDeclContext because we don't have a scope.
1975f4a2713aSLionel Sambuc ContextRAII SavedContext(*this, Instantiation);
1976f4a2713aSLionel Sambuc EnterExpressionEvaluationContext EvalContext(*this,
1977f4a2713aSLionel Sambuc Sema::PotentiallyEvaluated);
1978f4a2713aSLionel Sambuc
1979f4a2713aSLionel Sambuc // If this is an instantiation of a local class, merge this local
1980f4a2713aSLionel Sambuc // instantiation scope with the enclosing scope. Otherwise, every
1981f4a2713aSLionel Sambuc // instantiation of a class has its own local instantiation scope.
1982f4a2713aSLionel Sambuc bool MergeWithParentScope = !Instantiation->isDefinedOutsideFunctionOrMethod();
1983f4a2713aSLionel Sambuc LocalInstantiationScope Scope(*this, MergeWithParentScope);
1984f4a2713aSLionel Sambuc
1985f4a2713aSLionel Sambuc // Pull attributes from the pattern onto the instantiation.
1986f4a2713aSLionel Sambuc InstantiateAttrs(TemplateArgs, Pattern, Instantiation);
1987f4a2713aSLionel Sambuc
1988f4a2713aSLionel Sambuc // Start the definition of this instantiation.
1989f4a2713aSLionel Sambuc Instantiation->startDefinition();
1990f4a2713aSLionel Sambuc
1991*0a6a1f1dSLionel Sambuc // The instantiation is visible here, even if it was first declared in an
1992*0a6a1f1dSLionel Sambuc // unimported module.
1993*0a6a1f1dSLionel Sambuc Instantiation->setHidden(false);
1994*0a6a1f1dSLionel Sambuc
1995*0a6a1f1dSLionel Sambuc // FIXME: This loses the as-written tag kind for an explicit instantiation.
1996f4a2713aSLionel Sambuc Instantiation->setTagKind(Pattern->getTagKind());
1997f4a2713aSLionel Sambuc
1998f4a2713aSLionel Sambuc // Do substitution on the base class specifiers.
1999f4a2713aSLionel Sambuc if (SubstBaseSpecifiers(Instantiation, Pattern, TemplateArgs))
2000f4a2713aSLionel Sambuc Instantiation->setInvalidDecl();
2001f4a2713aSLionel Sambuc
2002f4a2713aSLionel Sambuc TemplateDeclInstantiator Instantiator(*this, Instantiation, TemplateArgs);
2003f4a2713aSLionel Sambuc SmallVector<Decl*, 4> Fields;
2004f4a2713aSLionel Sambuc // Delay instantiation of late parsed attributes.
2005f4a2713aSLionel Sambuc LateInstantiatedAttrVec LateAttrs;
2006f4a2713aSLionel Sambuc Instantiator.enableLateAttributeInstantiation(&LateAttrs);
2007f4a2713aSLionel Sambuc
2008*0a6a1f1dSLionel Sambuc for (auto *Member : Pattern->decls()) {
2009f4a2713aSLionel Sambuc // Don't instantiate members not belonging in this semantic context.
2010f4a2713aSLionel Sambuc // e.g. for:
2011f4a2713aSLionel Sambuc // @code
2012f4a2713aSLionel Sambuc // template <int i> class A {
2013f4a2713aSLionel Sambuc // class B *g;
2014f4a2713aSLionel Sambuc // };
2015f4a2713aSLionel Sambuc // @endcode
2016f4a2713aSLionel Sambuc // 'class B' has the template as lexical context but semantically it is
2017f4a2713aSLionel Sambuc // introduced in namespace scope.
2018*0a6a1f1dSLionel Sambuc if (Member->getDeclContext() != Pattern)
2019f4a2713aSLionel Sambuc continue;
2020f4a2713aSLionel Sambuc
2021*0a6a1f1dSLionel Sambuc if (Member->isInvalidDecl()) {
2022f4a2713aSLionel Sambuc Instantiation->setInvalidDecl();
2023f4a2713aSLionel Sambuc continue;
2024f4a2713aSLionel Sambuc }
2025f4a2713aSLionel Sambuc
2026*0a6a1f1dSLionel Sambuc Decl *NewMember = Instantiator.Visit(Member);
2027f4a2713aSLionel Sambuc if (NewMember) {
2028f4a2713aSLionel Sambuc if (FieldDecl *Field = dyn_cast<FieldDecl>(NewMember)) {
2029f4a2713aSLionel Sambuc Fields.push_back(Field);
2030f4a2713aSLionel Sambuc } else if (EnumDecl *Enum = dyn_cast<EnumDecl>(NewMember)) {
2031f4a2713aSLionel Sambuc // C++11 [temp.inst]p1: The implicit instantiation of a class template
2032f4a2713aSLionel Sambuc // specialization causes the implicit instantiation of the definitions
2033f4a2713aSLionel Sambuc // of unscoped member enumerations.
2034f4a2713aSLionel Sambuc // Record a point of instantiation for this implicit instantiation.
2035f4a2713aSLionel Sambuc if (TSK == TSK_ImplicitInstantiation && !Enum->isScoped() &&
2036f4a2713aSLionel Sambuc Enum->isCompleteDefinition()) {
2037f4a2713aSLionel Sambuc MemberSpecializationInfo *MSInfo =Enum->getMemberSpecializationInfo();
2038f4a2713aSLionel Sambuc assert(MSInfo && "no spec info for member enum specialization");
2039f4a2713aSLionel Sambuc MSInfo->setTemplateSpecializationKind(TSK_ImplicitInstantiation);
2040f4a2713aSLionel Sambuc MSInfo->setPointOfInstantiation(PointOfInstantiation);
2041f4a2713aSLionel Sambuc }
2042f4a2713aSLionel Sambuc } else if (StaticAssertDecl *SA = dyn_cast<StaticAssertDecl>(NewMember)) {
2043f4a2713aSLionel Sambuc if (SA->isFailed()) {
2044f4a2713aSLionel Sambuc // A static_assert failed. Bail out; instantiating this
2045f4a2713aSLionel Sambuc // class is probably not meaningful.
2046f4a2713aSLionel Sambuc Instantiation->setInvalidDecl();
2047f4a2713aSLionel Sambuc break;
2048f4a2713aSLionel Sambuc }
2049f4a2713aSLionel Sambuc }
2050f4a2713aSLionel Sambuc
2051f4a2713aSLionel Sambuc if (NewMember->isInvalidDecl())
2052f4a2713aSLionel Sambuc Instantiation->setInvalidDecl();
2053f4a2713aSLionel Sambuc } else {
2054f4a2713aSLionel Sambuc // FIXME: Eventually, a NULL return will mean that one of the
2055f4a2713aSLionel Sambuc // instantiations was a semantic disaster, and we'll want to mark the
2056f4a2713aSLionel Sambuc // declaration invalid.
2057f4a2713aSLionel Sambuc // For now, we expect to skip some members that we can't yet handle.
2058f4a2713aSLionel Sambuc }
2059f4a2713aSLionel Sambuc }
2060f4a2713aSLionel Sambuc
2061f4a2713aSLionel Sambuc // Finish checking fields.
2062*0a6a1f1dSLionel Sambuc ActOnFields(nullptr, Instantiation->getLocation(), Instantiation, Fields,
2063*0a6a1f1dSLionel Sambuc SourceLocation(), SourceLocation(), nullptr);
2064f4a2713aSLionel Sambuc CheckCompletedCXXClass(Instantiation);
2065f4a2713aSLionel Sambuc
2066f4a2713aSLionel Sambuc // Instantiate late parsed attributes, and attach them to their decls.
2067f4a2713aSLionel Sambuc // See Sema::InstantiateAttrs
2068f4a2713aSLionel Sambuc for (LateInstantiatedAttrVec::iterator I = LateAttrs.begin(),
2069f4a2713aSLionel Sambuc E = LateAttrs.end(); I != E; ++I) {
2070f4a2713aSLionel Sambuc assert(CurrentInstantiationScope == Instantiator.getStartingScope());
2071f4a2713aSLionel Sambuc CurrentInstantiationScope = I->Scope;
2072f4a2713aSLionel Sambuc
2073f4a2713aSLionel Sambuc // Allow 'this' within late-parsed attributes.
2074f4a2713aSLionel Sambuc NamedDecl *ND = dyn_cast<NamedDecl>(I->NewDecl);
2075f4a2713aSLionel Sambuc CXXRecordDecl *ThisContext =
2076f4a2713aSLionel Sambuc dyn_cast_or_null<CXXRecordDecl>(ND->getDeclContext());
2077f4a2713aSLionel Sambuc CXXThisScopeRAII ThisScope(*this, ThisContext, /*TypeQuals*/0,
2078f4a2713aSLionel Sambuc ND && ND->isCXXInstanceMember());
2079f4a2713aSLionel Sambuc
2080f4a2713aSLionel Sambuc Attr *NewAttr =
2081f4a2713aSLionel Sambuc instantiateTemplateAttribute(I->TmplAttr, Context, *this, TemplateArgs);
2082f4a2713aSLionel Sambuc I->NewDecl->addAttr(NewAttr);
2083f4a2713aSLionel Sambuc LocalInstantiationScope::deleteScopes(I->Scope,
2084f4a2713aSLionel Sambuc Instantiator.getStartingScope());
2085f4a2713aSLionel Sambuc }
2086f4a2713aSLionel Sambuc Instantiator.disableLateAttributeInstantiation();
2087f4a2713aSLionel Sambuc LateAttrs.clear();
2088f4a2713aSLionel Sambuc
2089f4a2713aSLionel Sambuc ActOnFinishDelayedMemberInitializers(Instantiation);
2090f4a2713aSLionel Sambuc
2091*0a6a1f1dSLionel Sambuc // FIXME: We should do something similar for explicit instantiations so they
2092*0a6a1f1dSLionel Sambuc // end up in the right module.
2093f4a2713aSLionel Sambuc if (TSK == TSK_ImplicitInstantiation) {
2094f4a2713aSLionel Sambuc Instantiation->setLocation(Pattern->getLocation());
2095f4a2713aSLionel Sambuc Instantiation->setLocStart(Pattern->getInnerLocStart());
2096f4a2713aSLionel Sambuc Instantiation->setRBraceLoc(Pattern->getRBraceLoc());
2097f4a2713aSLionel Sambuc }
2098f4a2713aSLionel Sambuc
2099f4a2713aSLionel Sambuc if (!Instantiation->isInvalidDecl()) {
2100f4a2713aSLionel Sambuc // Perform any dependent diagnostics from the pattern.
2101f4a2713aSLionel Sambuc PerformDependentDiagnostics(Pattern, TemplateArgs);
2102f4a2713aSLionel Sambuc
2103f4a2713aSLionel Sambuc // Instantiate any out-of-line class template partial
2104f4a2713aSLionel Sambuc // specializations now.
2105f4a2713aSLionel Sambuc for (TemplateDeclInstantiator::delayed_partial_spec_iterator
2106f4a2713aSLionel Sambuc P = Instantiator.delayed_partial_spec_begin(),
2107f4a2713aSLionel Sambuc PEnd = Instantiator.delayed_partial_spec_end();
2108f4a2713aSLionel Sambuc P != PEnd; ++P) {
2109f4a2713aSLionel Sambuc if (!Instantiator.InstantiateClassTemplatePartialSpecialization(
2110f4a2713aSLionel Sambuc P->first, P->second)) {
2111f4a2713aSLionel Sambuc Instantiation->setInvalidDecl();
2112f4a2713aSLionel Sambuc break;
2113f4a2713aSLionel Sambuc }
2114f4a2713aSLionel Sambuc }
2115f4a2713aSLionel Sambuc
2116f4a2713aSLionel Sambuc // Instantiate any out-of-line variable template partial
2117f4a2713aSLionel Sambuc // specializations now.
2118f4a2713aSLionel Sambuc for (TemplateDeclInstantiator::delayed_var_partial_spec_iterator
2119f4a2713aSLionel Sambuc P = Instantiator.delayed_var_partial_spec_begin(),
2120f4a2713aSLionel Sambuc PEnd = Instantiator.delayed_var_partial_spec_end();
2121f4a2713aSLionel Sambuc P != PEnd; ++P) {
2122f4a2713aSLionel Sambuc if (!Instantiator.InstantiateVarTemplatePartialSpecialization(
2123f4a2713aSLionel Sambuc P->first, P->second)) {
2124f4a2713aSLionel Sambuc Instantiation->setInvalidDecl();
2125f4a2713aSLionel Sambuc break;
2126f4a2713aSLionel Sambuc }
2127f4a2713aSLionel Sambuc }
2128f4a2713aSLionel Sambuc }
2129f4a2713aSLionel Sambuc
2130f4a2713aSLionel Sambuc // Exit the scope of this instantiation.
2131f4a2713aSLionel Sambuc SavedContext.pop();
2132f4a2713aSLionel Sambuc
2133f4a2713aSLionel Sambuc if (!Instantiation->isInvalidDecl()) {
2134f4a2713aSLionel Sambuc Consumer.HandleTagDeclDefinition(Instantiation);
2135f4a2713aSLionel Sambuc
2136f4a2713aSLionel Sambuc // Always emit the vtable for an explicit instantiation definition
2137f4a2713aSLionel Sambuc // of a polymorphic class template specialization.
2138f4a2713aSLionel Sambuc if (TSK == TSK_ExplicitInstantiationDefinition)
2139f4a2713aSLionel Sambuc MarkVTableUsed(PointOfInstantiation, Instantiation, true);
2140f4a2713aSLionel Sambuc }
2141f4a2713aSLionel Sambuc
2142f4a2713aSLionel Sambuc return Instantiation->isInvalidDecl();
2143f4a2713aSLionel Sambuc }
2144f4a2713aSLionel Sambuc
2145f4a2713aSLionel Sambuc /// \brief Instantiate the definition of an enum from a given pattern.
2146f4a2713aSLionel Sambuc ///
2147f4a2713aSLionel Sambuc /// \param PointOfInstantiation The point of instantiation within the
2148f4a2713aSLionel Sambuc /// source code.
2149f4a2713aSLionel Sambuc /// \param Instantiation is the declaration whose definition is being
2150f4a2713aSLionel Sambuc /// instantiated. This will be a member enumeration of a class
2151f4a2713aSLionel Sambuc /// temploid specialization, or a local enumeration within a
2152f4a2713aSLionel Sambuc /// function temploid specialization.
2153f4a2713aSLionel Sambuc /// \param Pattern The templated declaration from which the instantiation
2154f4a2713aSLionel Sambuc /// occurs.
2155f4a2713aSLionel Sambuc /// \param TemplateArgs The template arguments to be substituted into
2156f4a2713aSLionel Sambuc /// the pattern.
2157f4a2713aSLionel Sambuc /// \param TSK The kind of implicit or explicit instantiation to perform.
2158f4a2713aSLionel Sambuc ///
2159f4a2713aSLionel Sambuc /// \return \c true if an error occurred, \c false otherwise.
InstantiateEnum(SourceLocation PointOfInstantiation,EnumDecl * Instantiation,EnumDecl * Pattern,const MultiLevelTemplateArgumentList & TemplateArgs,TemplateSpecializationKind TSK)2160f4a2713aSLionel Sambuc bool Sema::InstantiateEnum(SourceLocation PointOfInstantiation,
2161f4a2713aSLionel Sambuc EnumDecl *Instantiation, EnumDecl *Pattern,
2162f4a2713aSLionel Sambuc const MultiLevelTemplateArgumentList &TemplateArgs,
2163f4a2713aSLionel Sambuc TemplateSpecializationKind TSK) {
2164f4a2713aSLionel Sambuc EnumDecl *PatternDef = Pattern->getDefinition();
2165f4a2713aSLionel Sambuc if (DiagnoseUninstantiableTemplate(*this, PointOfInstantiation, Instantiation,
2166f4a2713aSLionel Sambuc Instantiation->getInstantiatedFromMemberEnum(),
2167f4a2713aSLionel Sambuc Pattern, PatternDef, TSK,/*Complain*/true))
2168f4a2713aSLionel Sambuc return true;
2169f4a2713aSLionel Sambuc Pattern = PatternDef;
2170f4a2713aSLionel Sambuc
2171f4a2713aSLionel Sambuc // Record the point of instantiation.
2172f4a2713aSLionel Sambuc if (MemberSpecializationInfo *MSInfo
2173f4a2713aSLionel Sambuc = Instantiation->getMemberSpecializationInfo()) {
2174f4a2713aSLionel Sambuc MSInfo->setTemplateSpecializationKind(TSK);
2175f4a2713aSLionel Sambuc MSInfo->setPointOfInstantiation(PointOfInstantiation);
2176f4a2713aSLionel Sambuc }
2177f4a2713aSLionel Sambuc
2178f4a2713aSLionel Sambuc InstantiatingTemplate Inst(*this, PointOfInstantiation, Instantiation);
2179f4a2713aSLionel Sambuc if (Inst.isInvalid())
2180f4a2713aSLionel Sambuc return true;
2181f4a2713aSLionel Sambuc
2182*0a6a1f1dSLionel Sambuc // The instantiation is visible here, even if it was first declared in an
2183*0a6a1f1dSLionel Sambuc // unimported module.
2184*0a6a1f1dSLionel Sambuc Instantiation->setHidden(false);
2185*0a6a1f1dSLionel Sambuc
2186f4a2713aSLionel Sambuc // Enter the scope of this instantiation. We don't use
2187f4a2713aSLionel Sambuc // PushDeclContext because we don't have a scope.
2188f4a2713aSLionel Sambuc ContextRAII SavedContext(*this, Instantiation);
2189f4a2713aSLionel Sambuc EnterExpressionEvaluationContext EvalContext(*this,
2190f4a2713aSLionel Sambuc Sema::PotentiallyEvaluated);
2191f4a2713aSLionel Sambuc
2192f4a2713aSLionel Sambuc LocalInstantiationScope Scope(*this, /*MergeWithParentScope*/true);
2193f4a2713aSLionel Sambuc
2194f4a2713aSLionel Sambuc // Pull attributes from the pattern onto the instantiation.
2195f4a2713aSLionel Sambuc InstantiateAttrs(TemplateArgs, Pattern, Instantiation);
2196f4a2713aSLionel Sambuc
2197f4a2713aSLionel Sambuc TemplateDeclInstantiator Instantiator(*this, Instantiation, TemplateArgs);
2198f4a2713aSLionel Sambuc Instantiator.InstantiateEnumDefinition(Instantiation, Pattern);
2199f4a2713aSLionel Sambuc
2200f4a2713aSLionel Sambuc // Exit the scope of this instantiation.
2201f4a2713aSLionel Sambuc SavedContext.pop();
2202f4a2713aSLionel Sambuc
2203f4a2713aSLionel Sambuc return Instantiation->isInvalidDecl();
2204f4a2713aSLionel Sambuc }
2205f4a2713aSLionel Sambuc
2206*0a6a1f1dSLionel Sambuc
2207*0a6a1f1dSLionel Sambuc /// \brief Instantiate the definition of a field from the given pattern.
2208*0a6a1f1dSLionel Sambuc ///
2209*0a6a1f1dSLionel Sambuc /// \param PointOfInstantiation The point of instantiation within the
2210*0a6a1f1dSLionel Sambuc /// source code.
2211*0a6a1f1dSLionel Sambuc /// \param Instantiation is the declaration whose definition is being
2212*0a6a1f1dSLionel Sambuc /// instantiated. This will be a class of a class temploid
2213*0a6a1f1dSLionel Sambuc /// specialization, or a local enumeration within a function temploid
2214*0a6a1f1dSLionel Sambuc /// specialization.
2215*0a6a1f1dSLionel Sambuc /// \param Pattern The templated declaration from which the instantiation
2216*0a6a1f1dSLionel Sambuc /// occurs.
2217*0a6a1f1dSLionel Sambuc /// \param TemplateArgs The template arguments to be substituted into
2218*0a6a1f1dSLionel Sambuc /// the pattern.
2219*0a6a1f1dSLionel Sambuc ///
2220*0a6a1f1dSLionel Sambuc /// \return \c true if an error occurred, \c false otherwise.
InstantiateInClassInitializer(SourceLocation PointOfInstantiation,FieldDecl * Instantiation,FieldDecl * Pattern,const MultiLevelTemplateArgumentList & TemplateArgs)2221*0a6a1f1dSLionel Sambuc bool Sema::InstantiateInClassInitializer(
2222*0a6a1f1dSLionel Sambuc SourceLocation PointOfInstantiation, FieldDecl *Instantiation,
2223*0a6a1f1dSLionel Sambuc FieldDecl *Pattern, const MultiLevelTemplateArgumentList &TemplateArgs) {
2224*0a6a1f1dSLionel Sambuc // If there is no initializer, we don't need to do anything.
2225*0a6a1f1dSLionel Sambuc if (!Pattern->hasInClassInitializer())
2226*0a6a1f1dSLionel Sambuc return false;
2227*0a6a1f1dSLionel Sambuc
2228*0a6a1f1dSLionel Sambuc assert(Instantiation->getInClassInitStyle() ==
2229*0a6a1f1dSLionel Sambuc Pattern->getInClassInitStyle() &&
2230*0a6a1f1dSLionel Sambuc "pattern and instantiation disagree about init style");
2231*0a6a1f1dSLionel Sambuc
2232*0a6a1f1dSLionel Sambuc // Error out if we haven't parsed the initializer of the pattern yet because
2233*0a6a1f1dSLionel Sambuc // we are waiting for the closing brace of the outer class.
2234*0a6a1f1dSLionel Sambuc Expr *OldInit = Pattern->getInClassInitializer();
2235*0a6a1f1dSLionel Sambuc if (!OldInit) {
2236*0a6a1f1dSLionel Sambuc RecordDecl *PatternRD = Pattern->getParent();
2237*0a6a1f1dSLionel Sambuc RecordDecl *OutermostClass = PatternRD->getOuterLexicalRecordContext();
2238*0a6a1f1dSLionel Sambuc if (OutermostClass == PatternRD) {
2239*0a6a1f1dSLionel Sambuc Diag(Pattern->getLocEnd(), diag::err_in_class_initializer_not_yet_parsed)
2240*0a6a1f1dSLionel Sambuc << PatternRD << Pattern;
2241*0a6a1f1dSLionel Sambuc } else {
2242*0a6a1f1dSLionel Sambuc Diag(Pattern->getLocEnd(),
2243*0a6a1f1dSLionel Sambuc diag::err_in_class_initializer_not_yet_parsed_outer_class)
2244*0a6a1f1dSLionel Sambuc << PatternRD << OutermostClass << Pattern;
2245*0a6a1f1dSLionel Sambuc }
2246*0a6a1f1dSLionel Sambuc Instantiation->setInvalidDecl();
2247*0a6a1f1dSLionel Sambuc return true;
2248*0a6a1f1dSLionel Sambuc }
2249*0a6a1f1dSLionel Sambuc
2250*0a6a1f1dSLionel Sambuc InstantiatingTemplate Inst(*this, PointOfInstantiation, Instantiation);
2251*0a6a1f1dSLionel Sambuc if (Inst.isInvalid())
2252*0a6a1f1dSLionel Sambuc return true;
2253*0a6a1f1dSLionel Sambuc
2254*0a6a1f1dSLionel Sambuc // Enter the scope of this instantiation. We don't use PushDeclContext because
2255*0a6a1f1dSLionel Sambuc // we don't have a scope.
2256*0a6a1f1dSLionel Sambuc ContextRAII SavedContext(*this, Instantiation->getParent());
2257*0a6a1f1dSLionel Sambuc EnterExpressionEvaluationContext EvalContext(*this,
2258*0a6a1f1dSLionel Sambuc Sema::PotentiallyEvaluated);
2259*0a6a1f1dSLionel Sambuc
2260*0a6a1f1dSLionel Sambuc LocalInstantiationScope Scope(*this);
2261*0a6a1f1dSLionel Sambuc
2262*0a6a1f1dSLionel Sambuc // Instantiate the initializer.
2263*0a6a1f1dSLionel Sambuc ActOnStartCXXInClassMemberInitializer();
2264*0a6a1f1dSLionel Sambuc CXXThisScopeRAII ThisScope(*this, Instantiation->getParent(), /*TypeQuals=*/0);
2265*0a6a1f1dSLionel Sambuc
2266*0a6a1f1dSLionel Sambuc ExprResult NewInit = SubstInitializer(OldInit, TemplateArgs,
2267*0a6a1f1dSLionel Sambuc /*CXXDirectInit=*/false);
2268*0a6a1f1dSLionel Sambuc Expr *Init = NewInit.get();
2269*0a6a1f1dSLionel Sambuc assert((!Init || !isa<ParenListExpr>(Init)) && "call-style init in class");
2270*0a6a1f1dSLionel Sambuc ActOnFinishCXXInClassMemberInitializer(
2271*0a6a1f1dSLionel Sambuc Instantiation, Init ? Init->getLocStart() : SourceLocation(), Init);
2272*0a6a1f1dSLionel Sambuc
2273*0a6a1f1dSLionel Sambuc // Exit the scope of this instantiation.
2274*0a6a1f1dSLionel Sambuc SavedContext.pop();
2275*0a6a1f1dSLionel Sambuc
2276*0a6a1f1dSLionel Sambuc // Return true if the in-class initializer is still missing.
2277*0a6a1f1dSLionel Sambuc return !Instantiation->getInClassInitializer();
2278*0a6a1f1dSLionel Sambuc }
2279*0a6a1f1dSLionel Sambuc
2280f4a2713aSLionel Sambuc namespace {
2281f4a2713aSLionel Sambuc /// \brief A partial specialization whose template arguments have matched
2282f4a2713aSLionel Sambuc /// a given template-id.
2283f4a2713aSLionel Sambuc struct PartialSpecMatchResult {
2284f4a2713aSLionel Sambuc ClassTemplatePartialSpecializationDecl *Partial;
2285f4a2713aSLionel Sambuc TemplateArgumentList *Args;
2286f4a2713aSLionel Sambuc };
2287f4a2713aSLionel Sambuc }
2288f4a2713aSLionel Sambuc
InstantiateClassTemplateSpecialization(SourceLocation PointOfInstantiation,ClassTemplateSpecializationDecl * ClassTemplateSpec,TemplateSpecializationKind TSK,bool Complain)2289f4a2713aSLionel Sambuc bool Sema::InstantiateClassTemplateSpecialization(
2290f4a2713aSLionel Sambuc SourceLocation PointOfInstantiation,
2291f4a2713aSLionel Sambuc ClassTemplateSpecializationDecl *ClassTemplateSpec,
2292f4a2713aSLionel Sambuc TemplateSpecializationKind TSK, bool Complain) {
2293f4a2713aSLionel Sambuc // Perform the actual instantiation on the canonical declaration.
2294f4a2713aSLionel Sambuc ClassTemplateSpec = cast<ClassTemplateSpecializationDecl>(
2295f4a2713aSLionel Sambuc ClassTemplateSpec->getCanonicalDecl());
2296f4a2713aSLionel Sambuc
2297f4a2713aSLionel Sambuc // Check whether we have already instantiated or specialized this class
2298f4a2713aSLionel Sambuc // template specialization.
2299f4a2713aSLionel Sambuc if (ClassTemplateSpec->getSpecializationKind() != TSK_Undeclared) {
2300f4a2713aSLionel Sambuc if (ClassTemplateSpec->getSpecializationKind() ==
2301f4a2713aSLionel Sambuc TSK_ExplicitInstantiationDeclaration &&
2302f4a2713aSLionel Sambuc TSK == TSK_ExplicitInstantiationDefinition) {
2303f4a2713aSLionel Sambuc // An explicit instantiation definition follows an explicit instantiation
2304f4a2713aSLionel Sambuc // declaration (C++0x [temp.explicit]p10); go ahead and perform the
2305f4a2713aSLionel Sambuc // explicit instantiation.
2306f4a2713aSLionel Sambuc ClassTemplateSpec->setSpecializationKind(TSK);
2307f4a2713aSLionel Sambuc
2308f4a2713aSLionel Sambuc // If this is an explicit instantiation definition, mark the
2309f4a2713aSLionel Sambuc // vtable as used.
2310f4a2713aSLionel Sambuc if (TSK == TSK_ExplicitInstantiationDefinition &&
2311f4a2713aSLionel Sambuc !ClassTemplateSpec->isInvalidDecl())
2312f4a2713aSLionel Sambuc MarkVTableUsed(PointOfInstantiation, ClassTemplateSpec, true);
2313f4a2713aSLionel Sambuc
2314f4a2713aSLionel Sambuc return false;
2315f4a2713aSLionel Sambuc }
2316f4a2713aSLionel Sambuc
2317f4a2713aSLionel Sambuc // We can only instantiate something that hasn't already been
2318f4a2713aSLionel Sambuc // instantiated or specialized. Fail without any diagnostics: our
2319f4a2713aSLionel Sambuc // caller will provide an error message.
2320f4a2713aSLionel Sambuc return true;
2321f4a2713aSLionel Sambuc }
2322f4a2713aSLionel Sambuc
2323f4a2713aSLionel Sambuc if (ClassTemplateSpec->isInvalidDecl())
2324f4a2713aSLionel Sambuc return true;
2325f4a2713aSLionel Sambuc
2326f4a2713aSLionel Sambuc ClassTemplateDecl *Template = ClassTemplateSpec->getSpecializedTemplate();
2327*0a6a1f1dSLionel Sambuc CXXRecordDecl *Pattern = nullptr;
2328f4a2713aSLionel Sambuc
2329f4a2713aSLionel Sambuc // C++ [temp.class.spec.match]p1:
2330f4a2713aSLionel Sambuc // When a class template is used in a context that requires an
2331f4a2713aSLionel Sambuc // instantiation of the class, it is necessary to determine
2332f4a2713aSLionel Sambuc // whether the instantiation is to be generated using the primary
2333f4a2713aSLionel Sambuc // template or one of the partial specializations. This is done by
2334f4a2713aSLionel Sambuc // matching the template arguments of the class template
2335f4a2713aSLionel Sambuc // specialization with the template argument lists of the partial
2336f4a2713aSLionel Sambuc // specializations.
2337f4a2713aSLionel Sambuc typedef PartialSpecMatchResult MatchResult;
2338f4a2713aSLionel Sambuc SmallVector<MatchResult, 4> Matched;
2339f4a2713aSLionel Sambuc SmallVector<ClassTemplatePartialSpecializationDecl *, 4> PartialSpecs;
2340f4a2713aSLionel Sambuc Template->getPartialSpecializations(PartialSpecs);
2341f4a2713aSLionel Sambuc TemplateSpecCandidateSet FailedCandidates(PointOfInstantiation);
2342f4a2713aSLionel Sambuc for (unsigned I = 0, N = PartialSpecs.size(); I != N; ++I) {
2343f4a2713aSLionel Sambuc ClassTemplatePartialSpecializationDecl *Partial = PartialSpecs[I];
2344f4a2713aSLionel Sambuc TemplateDeductionInfo Info(FailedCandidates.getLocation());
2345f4a2713aSLionel Sambuc if (TemplateDeductionResult Result
2346f4a2713aSLionel Sambuc = DeduceTemplateArguments(Partial,
2347f4a2713aSLionel Sambuc ClassTemplateSpec->getTemplateArgs(),
2348f4a2713aSLionel Sambuc Info)) {
2349f4a2713aSLionel Sambuc // Store the failed-deduction information for use in diagnostics, later.
2350f4a2713aSLionel Sambuc // TODO: Actually use the failed-deduction info?
2351f4a2713aSLionel Sambuc FailedCandidates.addCandidate()
2352f4a2713aSLionel Sambuc .set(Partial, MakeDeductionFailureInfo(Context, Result, Info));
2353f4a2713aSLionel Sambuc (void)Result;
2354f4a2713aSLionel Sambuc } else {
2355f4a2713aSLionel Sambuc Matched.push_back(PartialSpecMatchResult());
2356f4a2713aSLionel Sambuc Matched.back().Partial = Partial;
2357f4a2713aSLionel Sambuc Matched.back().Args = Info.take();
2358f4a2713aSLionel Sambuc }
2359f4a2713aSLionel Sambuc }
2360f4a2713aSLionel Sambuc
2361f4a2713aSLionel Sambuc // If we're dealing with a member template where the template parameters
2362f4a2713aSLionel Sambuc // have been instantiated, this provides the original template parameters
2363f4a2713aSLionel Sambuc // from which the member template's parameters were instantiated.
2364f4a2713aSLionel Sambuc
2365f4a2713aSLionel Sambuc if (Matched.size() >= 1) {
2366f4a2713aSLionel Sambuc SmallVectorImpl<MatchResult>::iterator Best = Matched.begin();
2367f4a2713aSLionel Sambuc if (Matched.size() == 1) {
2368f4a2713aSLionel Sambuc // -- If exactly one matching specialization is found, the
2369f4a2713aSLionel Sambuc // instantiation is generated from that specialization.
2370f4a2713aSLionel Sambuc // We don't need to do anything for this.
2371f4a2713aSLionel Sambuc } else {
2372f4a2713aSLionel Sambuc // -- If more than one matching specialization is found, the
2373f4a2713aSLionel Sambuc // partial order rules (14.5.4.2) are used to determine
2374f4a2713aSLionel Sambuc // whether one of the specializations is more specialized
2375f4a2713aSLionel Sambuc // than the others. If none of the specializations is more
2376f4a2713aSLionel Sambuc // specialized than all of the other matching
2377f4a2713aSLionel Sambuc // specializations, then the use of the class template is
2378f4a2713aSLionel Sambuc // ambiguous and the program is ill-formed.
2379f4a2713aSLionel Sambuc for (SmallVectorImpl<MatchResult>::iterator P = Best + 1,
2380f4a2713aSLionel Sambuc PEnd = Matched.end();
2381f4a2713aSLionel Sambuc P != PEnd; ++P) {
2382f4a2713aSLionel Sambuc if (getMoreSpecializedPartialSpecialization(P->Partial, Best->Partial,
2383f4a2713aSLionel Sambuc PointOfInstantiation)
2384f4a2713aSLionel Sambuc == P->Partial)
2385f4a2713aSLionel Sambuc Best = P;
2386f4a2713aSLionel Sambuc }
2387f4a2713aSLionel Sambuc
2388f4a2713aSLionel Sambuc // Determine if the best partial specialization is more specialized than
2389f4a2713aSLionel Sambuc // the others.
2390f4a2713aSLionel Sambuc bool Ambiguous = false;
2391f4a2713aSLionel Sambuc for (SmallVectorImpl<MatchResult>::iterator P = Matched.begin(),
2392f4a2713aSLionel Sambuc PEnd = Matched.end();
2393f4a2713aSLionel Sambuc P != PEnd; ++P) {
2394f4a2713aSLionel Sambuc if (P != Best &&
2395f4a2713aSLionel Sambuc getMoreSpecializedPartialSpecialization(P->Partial, Best->Partial,
2396f4a2713aSLionel Sambuc PointOfInstantiation)
2397f4a2713aSLionel Sambuc != Best->Partial) {
2398f4a2713aSLionel Sambuc Ambiguous = true;
2399f4a2713aSLionel Sambuc break;
2400f4a2713aSLionel Sambuc }
2401f4a2713aSLionel Sambuc }
2402f4a2713aSLionel Sambuc
2403f4a2713aSLionel Sambuc if (Ambiguous) {
2404f4a2713aSLionel Sambuc // Partial ordering did not produce a clear winner. Complain.
2405f4a2713aSLionel Sambuc ClassTemplateSpec->setInvalidDecl();
2406f4a2713aSLionel Sambuc Diag(PointOfInstantiation, diag::err_partial_spec_ordering_ambiguous)
2407f4a2713aSLionel Sambuc << ClassTemplateSpec;
2408f4a2713aSLionel Sambuc
2409f4a2713aSLionel Sambuc // Print the matching partial specializations.
2410f4a2713aSLionel Sambuc for (SmallVectorImpl<MatchResult>::iterator P = Matched.begin(),
2411f4a2713aSLionel Sambuc PEnd = Matched.end();
2412f4a2713aSLionel Sambuc P != PEnd; ++P)
2413f4a2713aSLionel Sambuc Diag(P->Partial->getLocation(), diag::note_partial_spec_match)
2414f4a2713aSLionel Sambuc << getTemplateArgumentBindingsText(
2415f4a2713aSLionel Sambuc P->Partial->getTemplateParameters(),
2416f4a2713aSLionel Sambuc *P->Args);
2417f4a2713aSLionel Sambuc
2418f4a2713aSLionel Sambuc return true;
2419f4a2713aSLionel Sambuc }
2420f4a2713aSLionel Sambuc }
2421f4a2713aSLionel Sambuc
2422f4a2713aSLionel Sambuc // Instantiate using the best class template partial specialization.
2423f4a2713aSLionel Sambuc ClassTemplatePartialSpecializationDecl *OrigPartialSpec = Best->Partial;
2424f4a2713aSLionel Sambuc while (OrigPartialSpec->getInstantiatedFromMember()) {
2425f4a2713aSLionel Sambuc // If we've found an explicit specialization of this class template,
2426f4a2713aSLionel Sambuc // stop here and use that as the pattern.
2427f4a2713aSLionel Sambuc if (OrigPartialSpec->isMemberSpecialization())
2428f4a2713aSLionel Sambuc break;
2429f4a2713aSLionel Sambuc
2430f4a2713aSLionel Sambuc OrigPartialSpec = OrigPartialSpec->getInstantiatedFromMember();
2431f4a2713aSLionel Sambuc }
2432f4a2713aSLionel Sambuc
2433f4a2713aSLionel Sambuc Pattern = OrigPartialSpec;
2434f4a2713aSLionel Sambuc ClassTemplateSpec->setInstantiationOf(Best->Partial, Best->Args);
2435f4a2713aSLionel Sambuc } else {
2436f4a2713aSLionel Sambuc // -- If no matches are found, the instantiation is generated
2437f4a2713aSLionel Sambuc // from the primary template.
2438f4a2713aSLionel Sambuc ClassTemplateDecl *OrigTemplate = Template;
2439f4a2713aSLionel Sambuc while (OrigTemplate->getInstantiatedFromMemberTemplate()) {
2440f4a2713aSLionel Sambuc // If we've found an explicit specialization of this class template,
2441f4a2713aSLionel Sambuc // stop here and use that as the pattern.
2442f4a2713aSLionel Sambuc if (OrigTemplate->isMemberSpecialization())
2443f4a2713aSLionel Sambuc break;
2444f4a2713aSLionel Sambuc
2445f4a2713aSLionel Sambuc OrigTemplate = OrigTemplate->getInstantiatedFromMemberTemplate();
2446f4a2713aSLionel Sambuc }
2447f4a2713aSLionel Sambuc
2448f4a2713aSLionel Sambuc Pattern = OrigTemplate->getTemplatedDecl();
2449f4a2713aSLionel Sambuc }
2450f4a2713aSLionel Sambuc
2451f4a2713aSLionel Sambuc bool Result = InstantiateClass(PointOfInstantiation, ClassTemplateSpec,
2452f4a2713aSLionel Sambuc Pattern,
2453f4a2713aSLionel Sambuc getTemplateInstantiationArgs(ClassTemplateSpec),
2454f4a2713aSLionel Sambuc TSK,
2455f4a2713aSLionel Sambuc Complain);
2456f4a2713aSLionel Sambuc
2457f4a2713aSLionel Sambuc return Result;
2458f4a2713aSLionel Sambuc }
2459f4a2713aSLionel Sambuc
2460f4a2713aSLionel Sambuc /// \brief Instantiates the definitions of all of the member
2461f4a2713aSLionel Sambuc /// of the given class, which is an instantiation of a class template
2462f4a2713aSLionel Sambuc /// or a member class of a template.
2463f4a2713aSLionel Sambuc void
InstantiateClassMembers(SourceLocation PointOfInstantiation,CXXRecordDecl * Instantiation,const MultiLevelTemplateArgumentList & TemplateArgs,TemplateSpecializationKind TSK)2464f4a2713aSLionel Sambuc Sema::InstantiateClassMembers(SourceLocation PointOfInstantiation,
2465f4a2713aSLionel Sambuc CXXRecordDecl *Instantiation,
2466f4a2713aSLionel Sambuc const MultiLevelTemplateArgumentList &TemplateArgs,
2467f4a2713aSLionel Sambuc TemplateSpecializationKind TSK) {
2468*0a6a1f1dSLionel Sambuc // FIXME: We need to notify the ASTMutationListener that we did all of these
2469*0a6a1f1dSLionel Sambuc // things, in case we have an explicit instantiation definition in a PCM, a
2470*0a6a1f1dSLionel Sambuc // module, or preamble, and the declaration is in an imported AST.
2471*0a6a1f1dSLionel Sambuc assert(
2472*0a6a1f1dSLionel Sambuc (TSK == TSK_ExplicitInstantiationDefinition ||
2473*0a6a1f1dSLionel Sambuc TSK == TSK_ExplicitInstantiationDeclaration ||
2474*0a6a1f1dSLionel Sambuc (TSK == TSK_ImplicitInstantiation && Instantiation->isLocalClass())) &&
2475*0a6a1f1dSLionel Sambuc "Unexpected template specialization kind!");
2476*0a6a1f1dSLionel Sambuc for (auto *D : Instantiation->decls()) {
2477f4a2713aSLionel Sambuc bool SuppressNew = false;
2478*0a6a1f1dSLionel Sambuc if (auto *Function = dyn_cast<FunctionDecl>(D)) {
2479f4a2713aSLionel Sambuc if (FunctionDecl *Pattern
2480f4a2713aSLionel Sambuc = Function->getInstantiatedFromMemberFunction()) {
2481f4a2713aSLionel Sambuc MemberSpecializationInfo *MSInfo
2482f4a2713aSLionel Sambuc = Function->getMemberSpecializationInfo();
2483f4a2713aSLionel Sambuc assert(MSInfo && "No member specialization information?");
2484f4a2713aSLionel Sambuc if (MSInfo->getTemplateSpecializationKind()
2485f4a2713aSLionel Sambuc == TSK_ExplicitSpecialization)
2486f4a2713aSLionel Sambuc continue;
2487f4a2713aSLionel Sambuc
2488f4a2713aSLionel Sambuc if (CheckSpecializationInstantiationRedecl(PointOfInstantiation, TSK,
2489f4a2713aSLionel Sambuc Function,
2490f4a2713aSLionel Sambuc MSInfo->getTemplateSpecializationKind(),
2491f4a2713aSLionel Sambuc MSInfo->getPointOfInstantiation(),
2492f4a2713aSLionel Sambuc SuppressNew) ||
2493f4a2713aSLionel Sambuc SuppressNew)
2494f4a2713aSLionel Sambuc continue;
2495f4a2713aSLionel Sambuc
2496*0a6a1f1dSLionel Sambuc // C++11 [temp.explicit]p8:
2497f4a2713aSLionel Sambuc // An explicit instantiation definition that names a class template
2498f4a2713aSLionel Sambuc // specialization explicitly instantiates the class template
2499f4a2713aSLionel Sambuc // specialization and is only an explicit instantiation definition
2500f4a2713aSLionel Sambuc // of members whose definition is visible at the point of
2501f4a2713aSLionel Sambuc // instantiation.
2502*0a6a1f1dSLionel Sambuc if (TSK == TSK_ExplicitInstantiationDefinition && !Pattern->isDefined())
2503f4a2713aSLionel Sambuc continue;
2504f4a2713aSLionel Sambuc
2505f4a2713aSLionel Sambuc Function->setTemplateSpecializationKind(TSK, PointOfInstantiation);
2506f4a2713aSLionel Sambuc
2507*0a6a1f1dSLionel Sambuc if (Function->isDefined()) {
2508*0a6a1f1dSLionel Sambuc // Let the ASTConsumer know that this function has been explicitly
2509*0a6a1f1dSLionel Sambuc // instantiated now, and its linkage might have changed.
2510*0a6a1f1dSLionel Sambuc Consumer.HandleTopLevelDecl(DeclGroupRef(Function));
2511*0a6a1f1dSLionel Sambuc } else if (TSK == TSK_ExplicitInstantiationDefinition) {
2512f4a2713aSLionel Sambuc InstantiateFunctionDefinition(PointOfInstantiation, Function);
2513*0a6a1f1dSLionel Sambuc } else if (TSK == TSK_ImplicitInstantiation) {
2514*0a6a1f1dSLionel Sambuc PendingLocalImplicitInstantiations.push_back(
2515*0a6a1f1dSLionel Sambuc std::make_pair(Function, PointOfInstantiation));
2516f4a2713aSLionel Sambuc }
2517f4a2713aSLionel Sambuc }
2518*0a6a1f1dSLionel Sambuc } else if (auto *Var = dyn_cast<VarDecl>(D)) {
2519f4a2713aSLionel Sambuc if (isa<VarTemplateSpecializationDecl>(Var))
2520f4a2713aSLionel Sambuc continue;
2521f4a2713aSLionel Sambuc
2522f4a2713aSLionel Sambuc if (Var->isStaticDataMember()) {
2523f4a2713aSLionel Sambuc MemberSpecializationInfo *MSInfo = Var->getMemberSpecializationInfo();
2524f4a2713aSLionel Sambuc assert(MSInfo && "No member specialization information?");
2525f4a2713aSLionel Sambuc if (MSInfo->getTemplateSpecializationKind()
2526f4a2713aSLionel Sambuc == TSK_ExplicitSpecialization)
2527f4a2713aSLionel Sambuc continue;
2528f4a2713aSLionel Sambuc
2529f4a2713aSLionel Sambuc if (CheckSpecializationInstantiationRedecl(PointOfInstantiation, TSK,
2530f4a2713aSLionel Sambuc Var,
2531f4a2713aSLionel Sambuc MSInfo->getTemplateSpecializationKind(),
2532f4a2713aSLionel Sambuc MSInfo->getPointOfInstantiation(),
2533f4a2713aSLionel Sambuc SuppressNew) ||
2534f4a2713aSLionel Sambuc SuppressNew)
2535f4a2713aSLionel Sambuc continue;
2536f4a2713aSLionel Sambuc
2537f4a2713aSLionel Sambuc if (TSK == TSK_ExplicitInstantiationDefinition) {
2538f4a2713aSLionel Sambuc // C++0x [temp.explicit]p8:
2539f4a2713aSLionel Sambuc // An explicit instantiation definition that names a class template
2540f4a2713aSLionel Sambuc // specialization explicitly instantiates the class template
2541f4a2713aSLionel Sambuc // specialization and is only an explicit instantiation definition
2542f4a2713aSLionel Sambuc // of members whose definition is visible at the point of
2543f4a2713aSLionel Sambuc // instantiation.
2544f4a2713aSLionel Sambuc if (!Var->getInstantiatedFromStaticDataMember()
2545f4a2713aSLionel Sambuc ->getOutOfLineDefinition())
2546f4a2713aSLionel Sambuc continue;
2547f4a2713aSLionel Sambuc
2548f4a2713aSLionel Sambuc Var->setTemplateSpecializationKind(TSK, PointOfInstantiation);
2549f4a2713aSLionel Sambuc InstantiateStaticDataMemberDefinition(PointOfInstantiation, Var);
2550f4a2713aSLionel Sambuc } else {
2551f4a2713aSLionel Sambuc Var->setTemplateSpecializationKind(TSK, PointOfInstantiation);
2552f4a2713aSLionel Sambuc }
2553f4a2713aSLionel Sambuc }
2554*0a6a1f1dSLionel Sambuc } else if (auto *Record = dyn_cast<CXXRecordDecl>(D)) {
2555f4a2713aSLionel Sambuc // Always skip the injected-class-name, along with any
2556f4a2713aSLionel Sambuc // redeclarations of nested classes, since both would cause us
2557f4a2713aSLionel Sambuc // to try to instantiate the members of a class twice.
2558*0a6a1f1dSLionel Sambuc // Skip closure types; they'll get instantiated when we instantiate
2559*0a6a1f1dSLionel Sambuc // the corresponding lambda-expression.
2560*0a6a1f1dSLionel Sambuc if (Record->isInjectedClassName() || Record->getPreviousDecl() ||
2561*0a6a1f1dSLionel Sambuc Record->isLambda())
2562f4a2713aSLionel Sambuc continue;
2563f4a2713aSLionel Sambuc
2564f4a2713aSLionel Sambuc MemberSpecializationInfo *MSInfo = Record->getMemberSpecializationInfo();
2565f4a2713aSLionel Sambuc assert(MSInfo && "No member specialization information?");
2566f4a2713aSLionel Sambuc
2567f4a2713aSLionel Sambuc if (MSInfo->getTemplateSpecializationKind()
2568f4a2713aSLionel Sambuc == TSK_ExplicitSpecialization)
2569f4a2713aSLionel Sambuc continue;
2570f4a2713aSLionel Sambuc
2571f4a2713aSLionel Sambuc if (CheckSpecializationInstantiationRedecl(PointOfInstantiation, TSK,
2572f4a2713aSLionel Sambuc Record,
2573f4a2713aSLionel Sambuc MSInfo->getTemplateSpecializationKind(),
2574f4a2713aSLionel Sambuc MSInfo->getPointOfInstantiation(),
2575f4a2713aSLionel Sambuc SuppressNew) ||
2576f4a2713aSLionel Sambuc SuppressNew)
2577f4a2713aSLionel Sambuc continue;
2578f4a2713aSLionel Sambuc
2579f4a2713aSLionel Sambuc CXXRecordDecl *Pattern = Record->getInstantiatedFromMemberClass();
2580f4a2713aSLionel Sambuc assert(Pattern && "Missing instantiated-from-template information");
2581f4a2713aSLionel Sambuc
2582f4a2713aSLionel Sambuc if (!Record->getDefinition()) {
2583f4a2713aSLionel Sambuc if (!Pattern->getDefinition()) {
2584f4a2713aSLionel Sambuc // C++0x [temp.explicit]p8:
2585f4a2713aSLionel Sambuc // An explicit instantiation definition that names a class template
2586f4a2713aSLionel Sambuc // specialization explicitly instantiates the class template
2587f4a2713aSLionel Sambuc // specialization and is only an explicit instantiation definition
2588f4a2713aSLionel Sambuc // of members whose definition is visible at the point of
2589f4a2713aSLionel Sambuc // instantiation.
2590f4a2713aSLionel Sambuc if (TSK == TSK_ExplicitInstantiationDeclaration) {
2591f4a2713aSLionel Sambuc MSInfo->setTemplateSpecializationKind(TSK);
2592f4a2713aSLionel Sambuc MSInfo->setPointOfInstantiation(PointOfInstantiation);
2593f4a2713aSLionel Sambuc }
2594f4a2713aSLionel Sambuc
2595f4a2713aSLionel Sambuc continue;
2596f4a2713aSLionel Sambuc }
2597f4a2713aSLionel Sambuc
2598f4a2713aSLionel Sambuc InstantiateClass(PointOfInstantiation, Record, Pattern,
2599f4a2713aSLionel Sambuc TemplateArgs,
2600f4a2713aSLionel Sambuc TSK);
2601f4a2713aSLionel Sambuc } else {
2602f4a2713aSLionel Sambuc if (TSK == TSK_ExplicitInstantiationDefinition &&
2603f4a2713aSLionel Sambuc Record->getTemplateSpecializationKind() ==
2604f4a2713aSLionel Sambuc TSK_ExplicitInstantiationDeclaration) {
2605f4a2713aSLionel Sambuc Record->setTemplateSpecializationKind(TSK);
2606f4a2713aSLionel Sambuc MarkVTableUsed(PointOfInstantiation, Record, true);
2607f4a2713aSLionel Sambuc }
2608f4a2713aSLionel Sambuc }
2609f4a2713aSLionel Sambuc
2610f4a2713aSLionel Sambuc Pattern = cast_or_null<CXXRecordDecl>(Record->getDefinition());
2611f4a2713aSLionel Sambuc if (Pattern)
2612f4a2713aSLionel Sambuc InstantiateClassMembers(PointOfInstantiation, Pattern, TemplateArgs,
2613f4a2713aSLionel Sambuc TSK);
2614*0a6a1f1dSLionel Sambuc } else if (auto *Enum = dyn_cast<EnumDecl>(D)) {
2615f4a2713aSLionel Sambuc MemberSpecializationInfo *MSInfo = Enum->getMemberSpecializationInfo();
2616f4a2713aSLionel Sambuc assert(MSInfo && "No member specialization information?");
2617f4a2713aSLionel Sambuc
2618f4a2713aSLionel Sambuc if (MSInfo->getTemplateSpecializationKind()
2619f4a2713aSLionel Sambuc == TSK_ExplicitSpecialization)
2620f4a2713aSLionel Sambuc continue;
2621f4a2713aSLionel Sambuc
2622f4a2713aSLionel Sambuc if (CheckSpecializationInstantiationRedecl(
2623f4a2713aSLionel Sambuc PointOfInstantiation, TSK, Enum,
2624f4a2713aSLionel Sambuc MSInfo->getTemplateSpecializationKind(),
2625f4a2713aSLionel Sambuc MSInfo->getPointOfInstantiation(), SuppressNew) ||
2626f4a2713aSLionel Sambuc SuppressNew)
2627f4a2713aSLionel Sambuc continue;
2628f4a2713aSLionel Sambuc
2629f4a2713aSLionel Sambuc if (Enum->getDefinition())
2630f4a2713aSLionel Sambuc continue;
2631f4a2713aSLionel Sambuc
2632f4a2713aSLionel Sambuc EnumDecl *Pattern = Enum->getInstantiatedFromMemberEnum();
2633f4a2713aSLionel Sambuc assert(Pattern && "Missing instantiated-from-template information");
2634f4a2713aSLionel Sambuc
2635f4a2713aSLionel Sambuc if (TSK == TSK_ExplicitInstantiationDefinition) {
2636f4a2713aSLionel Sambuc if (!Pattern->getDefinition())
2637f4a2713aSLionel Sambuc continue;
2638f4a2713aSLionel Sambuc
2639f4a2713aSLionel Sambuc InstantiateEnum(PointOfInstantiation, Enum, Pattern, TemplateArgs, TSK);
2640f4a2713aSLionel Sambuc } else {
2641f4a2713aSLionel Sambuc MSInfo->setTemplateSpecializationKind(TSK);
2642f4a2713aSLionel Sambuc MSInfo->setPointOfInstantiation(PointOfInstantiation);
2643f4a2713aSLionel Sambuc }
2644*0a6a1f1dSLionel Sambuc } else if (auto *Field = dyn_cast<FieldDecl>(D)) {
2645*0a6a1f1dSLionel Sambuc // No need to instantiate in-class initializers during explicit
2646*0a6a1f1dSLionel Sambuc // instantiation.
2647*0a6a1f1dSLionel Sambuc if (Field->hasInClassInitializer() && TSK == TSK_ImplicitInstantiation) {
2648*0a6a1f1dSLionel Sambuc CXXRecordDecl *ClassPattern =
2649*0a6a1f1dSLionel Sambuc Instantiation->getTemplateInstantiationPattern();
2650*0a6a1f1dSLionel Sambuc DeclContext::lookup_result Lookup =
2651*0a6a1f1dSLionel Sambuc ClassPattern->lookup(Field->getDeclName());
2652*0a6a1f1dSLionel Sambuc assert(Lookup.size() == 1);
2653*0a6a1f1dSLionel Sambuc FieldDecl *Pattern = cast<FieldDecl>(Lookup[0]);
2654*0a6a1f1dSLionel Sambuc InstantiateInClassInitializer(PointOfInstantiation, Field, Pattern,
2655*0a6a1f1dSLionel Sambuc TemplateArgs);
2656*0a6a1f1dSLionel Sambuc }
2657f4a2713aSLionel Sambuc }
2658f4a2713aSLionel Sambuc }
2659f4a2713aSLionel Sambuc }
2660f4a2713aSLionel Sambuc
2661f4a2713aSLionel Sambuc /// \brief Instantiate the definitions of all of the members of the
2662f4a2713aSLionel Sambuc /// given class template specialization, which was named as part of an
2663f4a2713aSLionel Sambuc /// explicit instantiation.
2664f4a2713aSLionel Sambuc void
InstantiateClassTemplateSpecializationMembers(SourceLocation PointOfInstantiation,ClassTemplateSpecializationDecl * ClassTemplateSpec,TemplateSpecializationKind TSK)2665f4a2713aSLionel Sambuc Sema::InstantiateClassTemplateSpecializationMembers(
2666f4a2713aSLionel Sambuc SourceLocation PointOfInstantiation,
2667f4a2713aSLionel Sambuc ClassTemplateSpecializationDecl *ClassTemplateSpec,
2668f4a2713aSLionel Sambuc TemplateSpecializationKind TSK) {
2669f4a2713aSLionel Sambuc // C++0x [temp.explicit]p7:
2670f4a2713aSLionel Sambuc // An explicit instantiation that names a class template
2671f4a2713aSLionel Sambuc // specialization is an explicit instantion of the same kind
2672f4a2713aSLionel Sambuc // (declaration or definition) of each of its members (not
2673f4a2713aSLionel Sambuc // including members inherited from base classes) that has not
2674f4a2713aSLionel Sambuc // been previously explicitly specialized in the translation unit
2675f4a2713aSLionel Sambuc // containing the explicit instantiation, except as described
2676f4a2713aSLionel Sambuc // below.
2677f4a2713aSLionel Sambuc InstantiateClassMembers(PointOfInstantiation, ClassTemplateSpec,
2678f4a2713aSLionel Sambuc getTemplateInstantiationArgs(ClassTemplateSpec),
2679f4a2713aSLionel Sambuc TSK);
2680f4a2713aSLionel Sambuc }
2681f4a2713aSLionel Sambuc
2682f4a2713aSLionel Sambuc StmtResult
SubstStmt(Stmt * S,const MultiLevelTemplateArgumentList & TemplateArgs)2683f4a2713aSLionel Sambuc Sema::SubstStmt(Stmt *S, const MultiLevelTemplateArgumentList &TemplateArgs) {
2684f4a2713aSLionel Sambuc if (!S)
2685*0a6a1f1dSLionel Sambuc return S;
2686f4a2713aSLionel Sambuc
2687f4a2713aSLionel Sambuc TemplateInstantiator Instantiator(*this, TemplateArgs,
2688f4a2713aSLionel Sambuc SourceLocation(),
2689f4a2713aSLionel Sambuc DeclarationName());
2690f4a2713aSLionel Sambuc return Instantiator.TransformStmt(S);
2691f4a2713aSLionel Sambuc }
2692f4a2713aSLionel Sambuc
2693f4a2713aSLionel Sambuc ExprResult
SubstExpr(Expr * E,const MultiLevelTemplateArgumentList & TemplateArgs)2694f4a2713aSLionel Sambuc Sema::SubstExpr(Expr *E, const MultiLevelTemplateArgumentList &TemplateArgs) {
2695f4a2713aSLionel Sambuc if (!E)
2696*0a6a1f1dSLionel Sambuc return E;
2697f4a2713aSLionel Sambuc
2698f4a2713aSLionel Sambuc TemplateInstantiator Instantiator(*this, TemplateArgs,
2699f4a2713aSLionel Sambuc SourceLocation(),
2700f4a2713aSLionel Sambuc DeclarationName());
2701f4a2713aSLionel Sambuc return Instantiator.TransformExpr(E);
2702f4a2713aSLionel Sambuc }
2703f4a2713aSLionel Sambuc
SubstInitializer(Expr * Init,const MultiLevelTemplateArgumentList & TemplateArgs,bool CXXDirectInit)2704f4a2713aSLionel Sambuc ExprResult Sema::SubstInitializer(Expr *Init,
2705f4a2713aSLionel Sambuc const MultiLevelTemplateArgumentList &TemplateArgs,
2706f4a2713aSLionel Sambuc bool CXXDirectInit) {
2707f4a2713aSLionel Sambuc TemplateInstantiator Instantiator(*this, TemplateArgs,
2708f4a2713aSLionel Sambuc SourceLocation(),
2709f4a2713aSLionel Sambuc DeclarationName());
2710f4a2713aSLionel Sambuc return Instantiator.TransformInitializer(Init, CXXDirectInit);
2711f4a2713aSLionel Sambuc }
2712f4a2713aSLionel Sambuc
SubstExprs(Expr ** Exprs,unsigned NumExprs,bool IsCall,const MultiLevelTemplateArgumentList & TemplateArgs,SmallVectorImpl<Expr * > & Outputs)2713f4a2713aSLionel Sambuc bool Sema::SubstExprs(Expr **Exprs, unsigned NumExprs, bool IsCall,
2714f4a2713aSLionel Sambuc const MultiLevelTemplateArgumentList &TemplateArgs,
2715f4a2713aSLionel Sambuc SmallVectorImpl<Expr *> &Outputs) {
2716f4a2713aSLionel Sambuc if (NumExprs == 0)
2717f4a2713aSLionel Sambuc return false;
2718f4a2713aSLionel Sambuc
2719f4a2713aSLionel Sambuc TemplateInstantiator Instantiator(*this, TemplateArgs,
2720f4a2713aSLionel Sambuc SourceLocation(),
2721f4a2713aSLionel Sambuc DeclarationName());
2722f4a2713aSLionel Sambuc return Instantiator.TransformExprs(Exprs, NumExprs, IsCall, Outputs);
2723f4a2713aSLionel Sambuc }
2724f4a2713aSLionel Sambuc
2725f4a2713aSLionel Sambuc NestedNameSpecifierLoc
SubstNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS,const MultiLevelTemplateArgumentList & TemplateArgs)2726f4a2713aSLionel Sambuc Sema::SubstNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS,
2727f4a2713aSLionel Sambuc const MultiLevelTemplateArgumentList &TemplateArgs) {
2728f4a2713aSLionel Sambuc if (!NNS)
2729f4a2713aSLionel Sambuc return NestedNameSpecifierLoc();
2730f4a2713aSLionel Sambuc
2731f4a2713aSLionel Sambuc TemplateInstantiator Instantiator(*this, TemplateArgs, NNS.getBeginLoc(),
2732f4a2713aSLionel Sambuc DeclarationName());
2733f4a2713aSLionel Sambuc return Instantiator.TransformNestedNameSpecifierLoc(NNS);
2734f4a2713aSLionel Sambuc }
2735f4a2713aSLionel Sambuc
2736f4a2713aSLionel Sambuc /// \brief Do template substitution on declaration name info.
2737f4a2713aSLionel Sambuc DeclarationNameInfo
SubstDeclarationNameInfo(const DeclarationNameInfo & NameInfo,const MultiLevelTemplateArgumentList & TemplateArgs)2738f4a2713aSLionel Sambuc Sema::SubstDeclarationNameInfo(const DeclarationNameInfo &NameInfo,
2739f4a2713aSLionel Sambuc const MultiLevelTemplateArgumentList &TemplateArgs) {
2740f4a2713aSLionel Sambuc TemplateInstantiator Instantiator(*this, TemplateArgs, NameInfo.getLoc(),
2741f4a2713aSLionel Sambuc NameInfo.getName());
2742f4a2713aSLionel Sambuc return Instantiator.TransformDeclarationNameInfo(NameInfo);
2743f4a2713aSLionel Sambuc }
2744f4a2713aSLionel Sambuc
2745f4a2713aSLionel Sambuc TemplateName
SubstTemplateName(NestedNameSpecifierLoc QualifierLoc,TemplateName Name,SourceLocation Loc,const MultiLevelTemplateArgumentList & TemplateArgs)2746f4a2713aSLionel Sambuc Sema::SubstTemplateName(NestedNameSpecifierLoc QualifierLoc,
2747f4a2713aSLionel Sambuc TemplateName Name, SourceLocation Loc,
2748f4a2713aSLionel Sambuc const MultiLevelTemplateArgumentList &TemplateArgs) {
2749f4a2713aSLionel Sambuc TemplateInstantiator Instantiator(*this, TemplateArgs, Loc,
2750f4a2713aSLionel Sambuc DeclarationName());
2751f4a2713aSLionel Sambuc CXXScopeSpec SS;
2752f4a2713aSLionel Sambuc SS.Adopt(QualifierLoc);
2753f4a2713aSLionel Sambuc return Instantiator.TransformTemplateName(SS, Name, Loc);
2754f4a2713aSLionel Sambuc }
2755f4a2713aSLionel Sambuc
Subst(const TemplateArgumentLoc * Args,unsigned NumArgs,TemplateArgumentListInfo & Result,const MultiLevelTemplateArgumentList & TemplateArgs)2756f4a2713aSLionel Sambuc bool Sema::Subst(const TemplateArgumentLoc *Args, unsigned NumArgs,
2757f4a2713aSLionel Sambuc TemplateArgumentListInfo &Result,
2758f4a2713aSLionel Sambuc const MultiLevelTemplateArgumentList &TemplateArgs) {
2759f4a2713aSLionel Sambuc TemplateInstantiator Instantiator(*this, TemplateArgs, SourceLocation(),
2760f4a2713aSLionel Sambuc DeclarationName());
2761f4a2713aSLionel Sambuc
2762f4a2713aSLionel Sambuc return Instantiator.TransformTemplateArguments(Args, NumArgs, Result);
2763f4a2713aSLionel Sambuc }
2764f4a2713aSLionel Sambuc
getCanonicalParmVarDecl(const Decl * D)2765f4a2713aSLionel Sambuc static const Decl *getCanonicalParmVarDecl(const Decl *D) {
2766f4a2713aSLionel Sambuc // When storing ParmVarDecls in the local instantiation scope, we always
2767f4a2713aSLionel Sambuc // want to use the ParmVarDecl from the canonical function declaration,
2768f4a2713aSLionel Sambuc // since the map is then valid for any redeclaration or definition of that
2769f4a2713aSLionel Sambuc // function.
2770f4a2713aSLionel Sambuc if (const ParmVarDecl *PV = dyn_cast<ParmVarDecl>(D)) {
2771f4a2713aSLionel Sambuc if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(PV->getDeclContext())) {
2772f4a2713aSLionel Sambuc unsigned i = PV->getFunctionScopeIndex();
2773*0a6a1f1dSLionel Sambuc // This parameter might be from a freestanding function type within the
2774*0a6a1f1dSLionel Sambuc // function and isn't necessarily referring to one of FD's parameters.
2775*0a6a1f1dSLionel Sambuc if (FD->getParamDecl(i) == PV)
2776f4a2713aSLionel Sambuc return FD->getCanonicalDecl()->getParamDecl(i);
2777f4a2713aSLionel Sambuc }
2778f4a2713aSLionel Sambuc }
2779f4a2713aSLionel Sambuc return D;
2780f4a2713aSLionel Sambuc }
2781f4a2713aSLionel Sambuc
2782f4a2713aSLionel Sambuc
2783f4a2713aSLionel Sambuc llvm::PointerUnion<Decl *, LocalInstantiationScope::DeclArgumentPack *> *
findInstantiationOf(const Decl * D)2784f4a2713aSLionel Sambuc LocalInstantiationScope::findInstantiationOf(const Decl *D) {
2785f4a2713aSLionel Sambuc D = getCanonicalParmVarDecl(D);
2786f4a2713aSLionel Sambuc for (LocalInstantiationScope *Current = this; Current;
2787f4a2713aSLionel Sambuc Current = Current->Outer) {
2788f4a2713aSLionel Sambuc
2789f4a2713aSLionel Sambuc // Check if we found something within this scope.
2790f4a2713aSLionel Sambuc const Decl *CheckD = D;
2791f4a2713aSLionel Sambuc do {
2792f4a2713aSLionel Sambuc LocalDeclsMap::iterator Found = Current->LocalDecls.find(CheckD);
2793f4a2713aSLionel Sambuc if (Found != Current->LocalDecls.end())
2794f4a2713aSLionel Sambuc return &Found->second;
2795f4a2713aSLionel Sambuc
2796f4a2713aSLionel Sambuc // If this is a tag declaration, it's possible that we need to look for
2797f4a2713aSLionel Sambuc // a previous declaration.
2798f4a2713aSLionel Sambuc if (const TagDecl *Tag = dyn_cast<TagDecl>(CheckD))
2799f4a2713aSLionel Sambuc CheckD = Tag->getPreviousDecl();
2800f4a2713aSLionel Sambuc else
2801*0a6a1f1dSLionel Sambuc CheckD = nullptr;
2802f4a2713aSLionel Sambuc } while (CheckD);
2803f4a2713aSLionel Sambuc
2804f4a2713aSLionel Sambuc // If we aren't combined with our outer scope, we're done.
2805f4a2713aSLionel Sambuc if (!Current->CombineWithOuterScope)
2806f4a2713aSLionel Sambuc break;
2807f4a2713aSLionel Sambuc }
2808f4a2713aSLionel Sambuc
2809f4a2713aSLionel Sambuc // If we're performing a partial substitution during template argument
2810f4a2713aSLionel Sambuc // deduction, we may not have values for template parameters yet.
2811f4a2713aSLionel Sambuc if (isa<NonTypeTemplateParmDecl>(D) || isa<TemplateTypeParmDecl>(D) ||
2812f4a2713aSLionel Sambuc isa<TemplateTemplateParmDecl>(D))
2813*0a6a1f1dSLionel Sambuc return nullptr;
2814f4a2713aSLionel Sambuc
2815f4a2713aSLionel Sambuc // If we didn't find the decl, then we either have a sema bug, or we have a
2816f4a2713aSLionel Sambuc // forward reference to a label declaration. Return null to indicate that
2817f4a2713aSLionel Sambuc // we have an uninstantiated label.
2818f4a2713aSLionel Sambuc assert(isa<LabelDecl>(D) && "declaration not instantiated in this scope");
2819*0a6a1f1dSLionel Sambuc return nullptr;
2820f4a2713aSLionel Sambuc }
2821f4a2713aSLionel Sambuc
InstantiatedLocal(const Decl * D,Decl * Inst)2822f4a2713aSLionel Sambuc void LocalInstantiationScope::InstantiatedLocal(const Decl *D, Decl *Inst) {
2823f4a2713aSLionel Sambuc D = getCanonicalParmVarDecl(D);
2824f4a2713aSLionel Sambuc llvm::PointerUnion<Decl *, DeclArgumentPack *> &Stored = LocalDecls[D];
2825*0a6a1f1dSLionel Sambuc if (Stored.isNull()) {
2826*0a6a1f1dSLionel Sambuc #ifndef NDEBUG
2827*0a6a1f1dSLionel Sambuc // It should not be present in any surrounding scope either.
2828*0a6a1f1dSLionel Sambuc LocalInstantiationScope *Current = this;
2829*0a6a1f1dSLionel Sambuc while (Current->CombineWithOuterScope && Current->Outer) {
2830*0a6a1f1dSLionel Sambuc Current = Current->Outer;
2831*0a6a1f1dSLionel Sambuc assert(Current->LocalDecls.find(D) == Current->LocalDecls.end() &&
2832*0a6a1f1dSLionel Sambuc "Instantiated local in inner and outer scopes");
2833*0a6a1f1dSLionel Sambuc }
2834*0a6a1f1dSLionel Sambuc #endif
2835f4a2713aSLionel Sambuc Stored = Inst;
2836*0a6a1f1dSLionel Sambuc } else if (DeclArgumentPack *Pack = Stored.dyn_cast<DeclArgumentPack *>()) {
2837f4a2713aSLionel Sambuc Pack->push_back(Inst);
2838*0a6a1f1dSLionel Sambuc } else {
2839f4a2713aSLionel Sambuc assert(Stored.get<Decl *>() == Inst && "Already instantiated this local");
2840f4a2713aSLionel Sambuc }
2841*0a6a1f1dSLionel Sambuc }
2842f4a2713aSLionel Sambuc
InstantiatedLocalPackArg(const Decl * D,Decl * Inst)2843f4a2713aSLionel Sambuc void LocalInstantiationScope::InstantiatedLocalPackArg(const Decl *D,
2844f4a2713aSLionel Sambuc Decl *Inst) {
2845f4a2713aSLionel Sambuc D = getCanonicalParmVarDecl(D);
2846f4a2713aSLionel Sambuc DeclArgumentPack *Pack = LocalDecls[D].get<DeclArgumentPack *>();
2847f4a2713aSLionel Sambuc Pack->push_back(Inst);
2848f4a2713aSLionel Sambuc }
2849f4a2713aSLionel Sambuc
MakeInstantiatedLocalArgPack(const Decl * D)2850f4a2713aSLionel Sambuc void LocalInstantiationScope::MakeInstantiatedLocalArgPack(const Decl *D) {
2851*0a6a1f1dSLionel Sambuc #ifndef NDEBUG
2852*0a6a1f1dSLionel Sambuc // This should be the first time we've been told about this decl.
2853*0a6a1f1dSLionel Sambuc for (LocalInstantiationScope *Current = this;
2854*0a6a1f1dSLionel Sambuc Current && Current->CombineWithOuterScope; Current = Current->Outer)
2855*0a6a1f1dSLionel Sambuc assert(Current->LocalDecls.find(D) == Current->LocalDecls.end() &&
2856*0a6a1f1dSLionel Sambuc "Creating local pack after instantiation of local");
2857*0a6a1f1dSLionel Sambuc #endif
2858*0a6a1f1dSLionel Sambuc
2859f4a2713aSLionel Sambuc D = getCanonicalParmVarDecl(D);
2860f4a2713aSLionel Sambuc llvm::PointerUnion<Decl *, DeclArgumentPack *> &Stored = LocalDecls[D];
2861f4a2713aSLionel Sambuc DeclArgumentPack *Pack = new DeclArgumentPack;
2862f4a2713aSLionel Sambuc Stored = Pack;
2863f4a2713aSLionel Sambuc ArgumentPacks.push_back(Pack);
2864f4a2713aSLionel Sambuc }
2865f4a2713aSLionel Sambuc
SetPartiallySubstitutedPack(NamedDecl * Pack,const TemplateArgument * ExplicitArgs,unsigned NumExplicitArgs)2866f4a2713aSLionel Sambuc void LocalInstantiationScope::SetPartiallySubstitutedPack(NamedDecl *Pack,
2867f4a2713aSLionel Sambuc const TemplateArgument *ExplicitArgs,
2868f4a2713aSLionel Sambuc unsigned NumExplicitArgs) {
2869f4a2713aSLionel Sambuc assert((!PartiallySubstitutedPack || PartiallySubstitutedPack == Pack) &&
2870f4a2713aSLionel Sambuc "Already have a partially-substituted pack");
2871f4a2713aSLionel Sambuc assert((!PartiallySubstitutedPack
2872f4a2713aSLionel Sambuc || NumArgsInPartiallySubstitutedPack == NumExplicitArgs) &&
2873f4a2713aSLionel Sambuc "Wrong number of arguments in partially-substituted pack");
2874f4a2713aSLionel Sambuc PartiallySubstitutedPack = Pack;
2875f4a2713aSLionel Sambuc ArgsInPartiallySubstitutedPack = ExplicitArgs;
2876f4a2713aSLionel Sambuc NumArgsInPartiallySubstitutedPack = NumExplicitArgs;
2877f4a2713aSLionel Sambuc }
2878f4a2713aSLionel Sambuc
getPartiallySubstitutedPack(const TemplateArgument ** ExplicitArgs,unsigned * NumExplicitArgs) const2879f4a2713aSLionel Sambuc NamedDecl *LocalInstantiationScope::getPartiallySubstitutedPack(
2880f4a2713aSLionel Sambuc const TemplateArgument **ExplicitArgs,
2881f4a2713aSLionel Sambuc unsigned *NumExplicitArgs) const {
2882f4a2713aSLionel Sambuc if (ExplicitArgs)
2883*0a6a1f1dSLionel Sambuc *ExplicitArgs = nullptr;
2884f4a2713aSLionel Sambuc if (NumExplicitArgs)
2885f4a2713aSLionel Sambuc *NumExplicitArgs = 0;
2886f4a2713aSLionel Sambuc
2887f4a2713aSLionel Sambuc for (const LocalInstantiationScope *Current = this; Current;
2888f4a2713aSLionel Sambuc Current = Current->Outer) {
2889f4a2713aSLionel Sambuc if (Current->PartiallySubstitutedPack) {
2890f4a2713aSLionel Sambuc if (ExplicitArgs)
2891f4a2713aSLionel Sambuc *ExplicitArgs = Current->ArgsInPartiallySubstitutedPack;
2892f4a2713aSLionel Sambuc if (NumExplicitArgs)
2893f4a2713aSLionel Sambuc *NumExplicitArgs = Current->NumArgsInPartiallySubstitutedPack;
2894f4a2713aSLionel Sambuc
2895f4a2713aSLionel Sambuc return Current->PartiallySubstitutedPack;
2896f4a2713aSLionel Sambuc }
2897f4a2713aSLionel Sambuc
2898f4a2713aSLionel Sambuc if (!Current->CombineWithOuterScope)
2899f4a2713aSLionel Sambuc break;
2900f4a2713aSLionel Sambuc }
2901f4a2713aSLionel Sambuc
2902*0a6a1f1dSLionel Sambuc return nullptr;
2903f4a2713aSLionel Sambuc }
2904