1f4a2713aSLionel Sambuc //===--- SemaTemplateInstantiateDecl.cpp - C++ Template Decl 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 for declarations.
10f4a2713aSLionel Sambuc //
11f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===/
12f4a2713aSLionel Sambuc #include "clang/Sema/SemaInternal.h"
13f4a2713aSLionel Sambuc #include "clang/AST/ASTConsumer.h"
14f4a2713aSLionel Sambuc #include "clang/AST/ASTContext.h"
15*0a6a1f1dSLionel Sambuc #include "clang/AST/ASTMutationListener.h"
16f4a2713aSLionel Sambuc #include "clang/AST/DeclTemplate.h"
17f4a2713aSLionel Sambuc #include "clang/AST/DeclVisitor.h"
18f4a2713aSLionel Sambuc #include "clang/AST/DependentDiagnostic.h"
19f4a2713aSLionel Sambuc #include "clang/AST/Expr.h"
20f4a2713aSLionel Sambuc #include "clang/AST/ExprCXX.h"
21f4a2713aSLionel Sambuc #include "clang/AST/TypeLoc.h"
22f4a2713aSLionel Sambuc #include "clang/Sema/Lookup.h"
23f4a2713aSLionel Sambuc #include "clang/Sema/PrettyDeclStackTrace.h"
24f4a2713aSLionel Sambuc #include "clang/Sema/Template.h"
25f4a2713aSLionel Sambuc
26f4a2713aSLionel Sambuc using namespace clang;
27f4a2713aSLionel Sambuc
isDeclWithinFunction(const Decl * D)28*0a6a1f1dSLionel Sambuc static bool isDeclWithinFunction(const Decl *D) {
29*0a6a1f1dSLionel Sambuc const DeclContext *DC = D->getDeclContext();
30*0a6a1f1dSLionel Sambuc if (DC->isFunctionOrMethod())
31*0a6a1f1dSLionel Sambuc return true;
32*0a6a1f1dSLionel Sambuc
33*0a6a1f1dSLionel Sambuc if (DC->isRecord())
34*0a6a1f1dSLionel Sambuc return cast<CXXRecordDecl>(DC)->isLocalClass();
35*0a6a1f1dSLionel Sambuc
36*0a6a1f1dSLionel Sambuc return false;
37*0a6a1f1dSLionel Sambuc }
38*0a6a1f1dSLionel Sambuc
39*0a6a1f1dSLionel Sambuc template<typename DeclT>
SubstQualifier(Sema & SemaRef,const DeclT * OldDecl,DeclT * NewDecl,const MultiLevelTemplateArgumentList & TemplateArgs)40*0a6a1f1dSLionel Sambuc static bool SubstQualifier(Sema &SemaRef, const DeclT *OldDecl, DeclT *NewDecl,
41*0a6a1f1dSLionel Sambuc const MultiLevelTemplateArgumentList &TemplateArgs) {
42f4a2713aSLionel Sambuc if (!OldDecl->getQualifierLoc())
43f4a2713aSLionel Sambuc return false;
44f4a2713aSLionel Sambuc
45*0a6a1f1dSLionel Sambuc assert((NewDecl->getFriendObjectKind() ||
46*0a6a1f1dSLionel Sambuc !OldDecl->getLexicalDeclContext()->isDependentContext()) &&
47*0a6a1f1dSLionel Sambuc "non-friend with qualified name defined in dependent context");
48*0a6a1f1dSLionel Sambuc Sema::ContextRAII SavedContext(
49*0a6a1f1dSLionel Sambuc SemaRef,
50*0a6a1f1dSLionel Sambuc const_cast<DeclContext *>(NewDecl->getFriendObjectKind()
51*0a6a1f1dSLionel Sambuc ? NewDecl->getLexicalDeclContext()
52*0a6a1f1dSLionel Sambuc : OldDecl->getLexicalDeclContext()));
53*0a6a1f1dSLionel Sambuc
54f4a2713aSLionel Sambuc NestedNameSpecifierLoc NewQualifierLoc
55f4a2713aSLionel Sambuc = SemaRef.SubstNestedNameSpecifierLoc(OldDecl->getQualifierLoc(),
56f4a2713aSLionel Sambuc TemplateArgs);
57f4a2713aSLionel Sambuc
58f4a2713aSLionel Sambuc if (!NewQualifierLoc)
59f4a2713aSLionel Sambuc return true;
60f4a2713aSLionel Sambuc
61f4a2713aSLionel Sambuc NewDecl->setQualifierInfo(NewQualifierLoc);
62f4a2713aSLionel Sambuc return false;
63f4a2713aSLionel Sambuc }
64f4a2713aSLionel Sambuc
SubstQualifier(const DeclaratorDecl * OldDecl,DeclaratorDecl * NewDecl)65*0a6a1f1dSLionel Sambuc bool TemplateDeclInstantiator::SubstQualifier(const DeclaratorDecl *OldDecl,
66*0a6a1f1dSLionel Sambuc DeclaratorDecl *NewDecl) {
67*0a6a1f1dSLionel Sambuc return ::SubstQualifier(SemaRef, OldDecl, NewDecl, TemplateArgs);
68*0a6a1f1dSLionel Sambuc }
69*0a6a1f1dSLionel Sambuc
SubstQualifier(const TagDecl * OldDecl,TagDecl * NewDecl)70f4a2713aSLionel Sambuc bool TemplateDeclInstantiator::SubstQualifier(const TagDecl *OldDecl,
71f4a2713aSLionel Sambuc TagDecl *NewDecl) {
72*0a6a1f1dSLionel Sambuc return ::SubstQualifier(SemaRef, OldDecl, NewDecl, TemplateArgs);
73f4a2713aSLionel Sambuc }
74f4a2713aSLionel Sambuc
75f4a2713aSLionel Sambuc // Include attribute instantiation code.
76f4a2713aSLionel Sambuc #include "clang/Sema/AttrTemplateInstantiate.inc"
77f4a2713aSLionel Sambuc
instantiateDependentAlignedAttr(Sema & S,const MultiLevelTemplateArgumentList & TemplateArgs,const AlignedAttr * Aligned,Decl * New,bool IsPackExpansion)78f4a2713aSLionel Sambuc static void instantiateDependentAlignedAttr(
79f4a2713aSLionel Sambuc Sema &S, const MultiLevelTemplateArgumentList &TemplateArgs,
80f4a2713aSLionel Sambuc const AlignedAttr *Aligned, Decl *New, bool IsPackExpansion) {
81f4a2713aSLionel Sambuc if (Aligned->isAlignmentExpr()) {
82f4a2713aSLionel Sambuc // The alignment expression is a constant expression.
83f4a2713aSLionel Sambuc EnterExpressionEvaluationContext Unevaluated(S, Sema::ConstantEvaluated);
84f4a2713aSLionel Sambuc ExprResult Result = S.SubstExpr(Aligned->getAlignmentExpr(), TemplateArgs);
85f4a2713aSLionel Sambuc if (!Result.isInvalid())
86*0a6a1f1dSLionel Sambuc S.AddAlignedAttr(Aligned->getLocation(), New, Result.getAs<Expr>(),
87f4a2713aSLionel Sambuc Aligned->getSpellingListIndex(), IsPackExpansion);
88f4a2713aSLionel Sambuc } else {
89f4a2713aSLionel Sambuc TypeSourceInfo *Result = S.SubstType(Aligned->getAlignmentType(),
90f4a2713aSLionel Sambuc TemplateArgs, Aligned->getLocation(),
91f4a2713aSLionel Sambuc DeclarationName());
92f4a2713aSLionel Sambuc if (Result)
93f4a2713aSLionel Sambuc S.AddAlignedAttr(Aligned->getLocation(), New, Result,
94f4a2713aSLionel Sambuc Aligned->getSpellingListIndex(), IsPackExpansion);
95f4a2713aSLionel Sambuc }
96f4a2713aSLionel Sambuc }
97f4a2713aSLionel Sambuc
instantiateDependentAlignedAttr(Sema & S,const MultiLevelTemplateArgumentList & TemplateArgs,const AlignedAttr * Aligned,Decl * New)98f4a2713aSLionel Sambuc static void instantiateDependentAlignedAttr(
99f4a2713aSLionel Sambuc Sema &S, const MultiLevelTemplateArgumentList &TemplateArgs,
100f4a2713aSLionel Sambuc const AlignedAttr *Aligned, Decl *New) {
101f4a2713aSLionel Sambuc if (!Aligned->isPackExpansion()) {
102f4a2713aSLionel Sambuc instantiateDependentAlignedAttr(S, TemplateArgs, Aligned, New, false);
103f4a2713aSLionel Sambuc return;
104f4a2713aSLionel Sambuc }
105f4a2713aSLionel Sambuc
106f4a2713aSLionel Sambuc SmallVector<UnexpandedParameterPack, 2> Unexpanded;
107f4a2713aSLionel Sambuc if (Aligned->isAlignmentExpr())
108f4a2713aSLionel Sambuc S.collectUnexpandedParameterPacks(Aligned->getAlignmentExpr(),
109f4a2713aSLionel Sambuc Unexpanded);
110f4a2713aSLionel Sambuc else
111f4a2713aSLionel Sambuc S.collectUnexpandedParameterPacks(Aligned->getAlignmentType()->getTypeLoc(),
112f4a2713aSLionel Sambuc Unexpanded);
113f4a2713aSLionel Sambuc assert(!Unexpanded.empty() && "Pack expansion without parameter packs?");
114f4a2713aSLionel Sambuc
115f4a2713aSLionel Sambuc // Determine whether we can expand this attribute pack yet.
116f4a2713aSLionel Sambuc bool Expand = true, RetainExpansion = false;
117f4a2713aSLionel Sambuc Optional<unsigned> NumExpansions;
118f4a2713aSLionel Sambuc // FIXME: Use the actual location of the ellipsis.
119f4a2713aSLionel Sambuc SourceLocation EllipsisLoc = Aligned->getLocation();
120f4a2713aSLionel Sambuc if (S.CheckParameterPacksForExpansion(EllipsisLoc, Aligned->getRange(),
121f4a2713aSLionel Sambuc Unexpanded, TemplateArgs, Expand,
122f4a2713aSLionel Sambuc RetainExpansion, NumExpansions))
123f4a2713aSLionel Sambuc return;
124f4a2713aSLionel Sambuc
125f4a2713aSLionel Sambuc if (!Expand) {
126f4a2713aSLionel Sambuc Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(S, -1);
127f4a2713aSLionel Sambuc instantiateDependentAlignedAttr(S, TemplateArgs, Aligned, New, true);
128f4a2713aSLionel Sambuc } else {
129f4a2713aSLionel Sambuc for (unsigned I = 0; I != *NumExpansions; ++I) {
130f4a2713aSLionel Sambuc Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(S, I);
131f4a2713aSLionel Sambuc instantiateDependentAlignedAttr(S, TemplateArgs, Aligned, New, false);
132f4a2713aSLionel Sambuc }
133f4a2713aSLionel Sambuc }
134f4a2713aSLionel Sambuc }
135f4a2713aSLionel Sambuc
instantiateDependentAssumeAlignedAttr(Sema & S,const MultiLevelTemplateArgumentList & TemplateArgs,const AssumeAlignedAttr * Aligned,Decl * New)136*0a6a1f1dSLionel Sambuc static void instantiateDependentAssumeAlignedAttr(
137*0a6a1f1dSLionel Sambuc Sema &S, const MultiLevelTemplateArgumentList &TemplateArgs,
138*0a6a1f1dSLionel Sambuc const AssumeAlignedAttr *Aligned, Decl *New) {
139*0a6a1f1dSLionel Sambuc // The alignment expression is a constant expression.
140*0a6a1f1dSLionel Sambuc EnterExpressionEvaluationContext Unevaluated(S, Sema::ConstantEvaluated);
141*0a6a1f1dSLionel Sambuc
142*0a6a1f1dSLionel Sambuc Expr *E, *OE = nullptr;
143*0a6a1f1dSLionel Sambuc ExprResult Result = S.SubstExpr(Aligned->getAlignment(), TemplateArgs);
144*0a6a1f1dSLionel Sambuc if (Result.isInvalid())
145*0a6a1f1dSLionel Sambuc return;
146*0a6a1f1dSLionel Sambuc E = Result.getAs<Expr>();
147*0a6a1f1dSLionel Sambuc
148*0a6a1f1dSLionel Sambuc if (Aligned->getOffset()) {
149*0a6a1f1dSLionel Sambuc Result = S.SubstExpr(Aligned->getOffset(), TemplateArgs);
150*0a6a1f1dSLionel Sambuc if (Result.isInvalid())
151*0a6a1f1dSLionel Sambuc return;
152*0a6a1f1dSLionel Sambuc OE = Result.getAs<Expr>();
153*0a6a1f1dSLionel Sambuc }
154*0a6a1f1dSLionel Sambuc
155*0a6a1f1dSLionel Sambuc S.AddAssumeAlignedAttr(Aligned->getLocation(), New, E, OE,
156*0a6a1f1dSLionel Sambuc Aligned->getSpellingListIndex());
157*0a6a1f1dSLionel Sambuc }
158*0a6a1f1dSLionel Sambuc
instantiateDependentAlignValueAttr(Sema & S,const MultiLevelTemplateArgumentList & TemplateArgs,const AlignValueAttr * Aligned,Decl * New)159*0a6a1f1dSLionel Sambuc static void instantiateDependentAlignValueAttr(
160*0a6a1f1dSLionel Sambuc Sema &S, const MultiLevelTemplateArgumentList &TemplateArgs,
161*0a6a1f1dSLionel Sambuc const AlignValueAttr *Aligned, Decl *New) {
162*0a6a1f1dSLionel Sambuc // The alignment expression is a constant expression.
163*0a6a1f1dSLionel Sambuc EnterExpressionEvaluationContext Unevaluated(S, Sema::ConstantEvaluated);
164*0a6a1f1dSLionel Sambuc ExprResult Result = S.SubstExpr(Aligned->getAlignment(), TemplateArgs);
165*0a6a1f1dSLionel Sambuc if (!Result.isInvalid())
166*0a6a1f1dSLionel Sambuc S.AddAlignValueAttr(Aligned->getLocation(), New, Result.getAs<Expr>(),
167*0a6a1f1dSLionel Sambuc Aligned->getSpellingListIndex());
168*0a6a1f1dSLionel Sambuc }
169*0a6a1f1dSLionel Sambuc
instantiateDependentEnableIfAttr(Sema & S,const MultiLevelTemplateArgumentList & TemplateArgs,const EnableIfAttr * A,const Decl * Tmpl,Decl * New)170*0a6a1f1dSLionel Sambuc static void instantiateDependentEnableIfAttr(
171*0a6a1f1dSLionel Sambuc Sema &S, const MultiLevelTemplateArgumentList &TemplateArgs,
172*0a6a1f1dSLionel Sambuc const EnableIfAttr *A, const Decl *Tmpl, Decl *New) {
173*0a6a1f1dSLionel Sambuc Expr *Cond = nullptr;
174*0a6a1f1dSLionel Sambuc {
175*0a6a1f1dSLionel Sambuc EnterExpressionEvaluationContext Unevaluated(S, Sema::Unevaluated);
176*0a6a1f1dSLionel Sambuc ExprResult Result = S.SubstExpr(A->getCond(), TemplateArgs);
177*0a6a1f1dSLionel Sambuc if (Result.isInvalid())
178*0a6a1f1dSLionel Sambuc return;
179*0a6a1f1dSLionel Sambuc Cond = Result.getAs<Expr>();
180*0a6a1f1dSLionel Sambuc }
181*0a6a1f1dSLionel Sambuc if (A->getCond()->isTypeDependent() && !Cond->isTypeDependent()) {
182*0a6a1f1dSLionel Sambuc ExprResult Converted = S.PerformContextuallyConvertToBool(Cond);
183*0a6a1f1dSLionel Sambuc if (Converted.isInvalid())
184*0a6a1f1dSLionel Sambuc return;
185*0a6a1f1dSLionel Sambuc Cond = Converted.get();
186*0a6a1f1dSLionel Sambuc }
187*0a6a1f1dSLionel Sambuc
188*0a6a1f1dSLionel Sambuc SmallVector<PartialDiagnosticAt, 8> Diags;
189*0a6a1f1dSLionel Sambuc if (A->getCond()->isValueDependent() && !Cond->isValueDependent() &&
190*0a6a1f1dSLionel Sambuc !Expr::isPotentialConstantExprUnevaluated(Cond, cast<FunctionDecl>(Tmpl),
191*0a6a1f1dSLionel Sambuc Diags)) {
192*0a6a1f1dSLionel Sambuc S.Diag(A->getLocation(), diag::err_enable_if_never_constant_expr);
193*0a6a1f1dSLionel Sambuc for (int I = 0, N = Diags.size(); I != N; ++I)
194*0a6a1f1dSLionel Sambuc S.Diag(Diags[I].first, Diags[I].second);
195*0a6a1f1dSLionel Sambuc return;
196*0a6a1f1dSLionel Sambuc }
197*0a6a1f1dSLionel Sambuc
198*0a6a1f1dSLionel Sambuc EnableIfAttr *EIA = new (S.getASTContext())
199*0a6a1f1dSLionel Sambuc EnableIfAttr(A->getLocation(), S.getASTContext(), Cond,
200*0a6a1f1dSLionel Sambuc A->getMessage(),
201*0a6a1f1dSLionel Sambuc A->getSpellingListIndex());
202*0a6a1f1dSLionel Sambuc New->addAttr(EIA);
203*0a6a1f1dSLionel Sambuc }
204*0a6a1f1dSLionel Sambuc
InstantiateAttrs(const MultiLevelTemplateArgumentList & TemplateArgs,const Decl * Tmpl,Decl * New,LateInstantiatedAttrVec * LateAttrs,LocalInstantiationScope * OuterMostScope)205f4a2713aSLionel Sambuc void Sema::InstantiateAttrs(const MultiLevelTemplateArgumentList &TemplateArgs,
206f4a2713aSLionel Sambuc const Decl *Tmpl, Decl *New,
207f4a2713aSLionel Sambuc LateInstantiatedAttrVec *LateAttrs,
208f4a2713aSLionel Sambuc LocalInstantiationScope *OuterMostScope) {
209*0a6a1f1dSLionel Sambuc for (const auto *TmplAttr : Tmpl->attrs()) {
210f4a2713aSLionel Sambuc // FIXME: This should be generalized to more than just the AlignedAttr.
211f4a2713aSLionel Sambuc const AlignedAttr *Aligned = dyn_cast<AlignedAttr>(TmplAttr);
212f4a2713aSLionel Sambuc if (Aligned && Aligned->isAlignmentDependent()) {
213f4a2713aSLionel Sambuc instantiateDependentAlignedAttr(*this, TemplateArgs, Aligned, New);
214f4a2713aSLionel Sambuc continue;
215f4a2713aSLionel Sambuc }
216f4a2713aSLionel Sambuc
217*0a6a1f1dSLionel Sambuc const AssumeAlignedAttr *AssumeAligned = dyn_cast<AssumeAlignedAttr>(TmplAttr);
218*0a6a1f1dSLionel Sambuc if (AssumeAligned) {
219*0a6a1f1dSLionel Sambuc instantiateDependentAssumeAlignedAttr(*this, TemplateArgs, AssumeAligned, New);
220*0a6a1f1dSLionel Sambuc continue;
221*0a6a1f1dSLionel Sambuc }
222*0a6a1f1dSLionel Sambuc
223*0a6a1f1dSLionel Sambuc const AlignValueAttr *AlignValue = dyn_cast<AlignValueAttr>(TmplAttr);
224*0a6a1f1dSLionel Sambuc if (AlignValue) {
225*0a6a1f1dSLionel Sambuc instantiateDependentAlignValueAttr(*this, TemplateArgs, AlignValue, New);
226*0a6a1f1dSLionel Sambuc continue;
227*0a6a1f1dSLionel Sambuc }
228*0a6a1f1dSLionel Sambuc
229*0a6a1f1dSLionel Sambuc const EnableIfAttr *EnableIf = dyn_cast<EnableIfAttr>(TmplAttr);
230*0a6a1f1dSLionel Sambuc if (EnableIf && EnableIf->getCond()->isValueDependent()) {
231*0a6a1f1dSLionel Sambuc instantiateDependentEnableIfAttr(*this, TemplateArgs, EnableIf, Tmpl,
232*0a6a1f1dSLionel Sambuc New);
233*0a6a1f1dSLionel Sambuc continue;
234*0a6a1f1dSLionel Sambuc }
235*0a6a1f1dSLionel Sambuc
236*0a6a1f1dSLionel Sambuc // Existing DLL attribute on the instantiation takes precedence.
237*0a6a1f1dSLionel Sambuc if (TmplAttr->getKind() == attr::DLLExport ||
238*0a6a1f1dSLionel Sambuc TmplAttr->getKind() == attr::DLLImport) {
239*0a6a1f1dSLionel Sambuc if (New->hasAttr<DLLExportAttr>() || New->hasAttr<DLLImportAttr>()) {
240*0a6a1f1dSLionel Sambuc continue;
241*0a6a1f1dSLionel Sambuc }
242*0a6a1f1dSLionel Sambuc }
243*0a6a1f1dSLionel Sambuc
244f4a2713aSLionel Sambuc assert(!TmplAttr->isPackExpansion());
245f4a2713aSLionel Sambuc if (TmplAttr->isLateParsed() && LateAttrs) {
246f4a2713aSLionel Sambuc // Late parsed attributes must be instantiated and attached after the
247f4a2713aSLionel Sambuc // enclosing class has been instantiated. See Sema::InstantiateClass.
248*0a6a1f1dSLionel Sambuc LocalInstantiationScope *Saved = nullptr;
249f4a2713aSLionel Sambuc if (CurrentInstantiationScope)
250f4a2713aSLionel Sambuc Saved = CurrentInstantiationScope->cloneScopes(OuterMostScope);
251f4a2713aSLionel Sambuc LateAttrs->push_back(LateInstantiatedAttribute(TmplAttr, Saved, New));
252f4a2713aSLionel Sambuc } else {
253f4a2713aSLionel Sambuc // Allow 'this' within late-parsed attributes.
254f4a2713aSLionel Sambuc NamedDecl *ND = dyn_cast<NamedDecl>(New);
255f4a2713aSLionel Sambuc CXXRecordDecl *ThisContext =
256f4a2713aSLionel Sambuc dyn_cast_or_null<CXXRecordDecl>(ND->getDeclContext());
257f4a2713aSLionel Sambuc CXXThisScopeRAII ThisScope(*this, ThisContext, /*TypeQuals*/0,
258f4a2713aSLionel Sambuc ND && ND->isCXXInstanceMember());
259f4a2713aSLionel Sambuc
260f4a2713aSLionel Sambuc Attr *NewAttr = sema::instantiateTemplateAttribute(TmplAttr, Context,
261f4a2713aSLionel Sambuc *this, TemplateArgs);
262f4a2713aSLionel Sambuc if (NewAttr)
263f4a2713aSLionel Sambuc New->addAttr(NewAttr);
264f4a2713aSLionel Sambuc }
265f4a2713aSLionel Sambuc }
266f4a2713aSLionel Sambuc }
267f4a2713aSLionel Sambuc
268*0a6a1f1dSLionel Sambuc /// Get the previous declaration of a declaration for the purposes of template
269*0a6a1f1dSLionel Sambuc /// instantiation. If this finds a previous declaration, then the previous
270*0a6a1f1dSLionel Sambuc /// declaration of the instantiation of D should be an instantiation of the
271*0a6a1f1dSLionel Sambuc /// result of this function.
272*0a6a1f1dSLionel Sambuc template<typename DeclT>
getPreviousDeclForInstantiation(DeclT * D)273*0a6a1f1dSLionel Sambuc static DeclT *getPreviousDeclForInstantiation(DeclT *D) {
274*0a6a1f1dSLionel Sambuc DeclT *Result = D->getPreviousDecl();
275*0a6a1f1dSLionel Sambuc
276*0a6a1f1dSLionel Sambuc // If the declaration is within a class, and the previous declaration was
277*0a6a1f1dSLionel Sambuc // merged from a different definition of that class, then we don't have a
278*0a6a1f1dSLionel Sambuc // previous declaration for the purpose of template instantiation.
279*0a6a1f1dSLionel Sambuc if (Result && isa<CXXRecordDecl>(D->getDeclContext()) &&
280*0a6a1f1dSLionel Sambuc D->getLexicalDeclContext() != Result->getLexicalDeclContext())
281*0a6a1f1dSLionel Sambuc return nullptr;
282*0a6a1f1dSLionel Sambuc
283*0a6a1f1dSLionel Sambuc return Result;
284*0a6a1f1dSLionel Sambuc }
285*0a6a1f1dSLionel Sambuc
286f4a2713aSLionel Sambuc Decl *
VisitTranslationUnitDecl(TranslationUnitDecl * D)287f4a2713aSLionel Sambuc TemplateDeclInstantiator::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
288f4a2713aSLionel Sambuc llvm_unreachable("Translation units cannot be instantiated");
289f4a2713aSLionel Sambuc }
290f4a2713aSLionel Sambuc
291f4a2713aSLionel Sambuc Decl *
VisitLabelDecl(LabelDecl * D)292f4a2713aSLionel Sambuc TemplateDeclInstantiator::VisitLabelDecl(LabelDecl *D) {
293f4a2713aSLionel Sambuc LabelDecl *Inst = LabelDecl::Create(SemaRef.Context, Owner, D->getLocation(),
294f4a2713aSLionel Sambuc D->getIdentifier());
295f4a2713aSLionel Sambuc Owner->addDecl(Inst);
296f4a2713aSLionel Sambuc return Inst;
297f4a2713aSLionel Sambuc }
298f4a2713aSLionel Sambuc
299f4a2713aSLionel Sambuc Decl *
VisitNamespaceDecl(NamespaceDecl * D)300f4a2713aSLionel Sambuc TemplateDeclInstantiator::VisitNamespaceDecl(NamespaceDecl *D) {
301f4a2713aSLionel Sambuc llvm_unreachable("Namespaces cannot be instantiated");
302f4a2713aSLionel Sambuc }
303f4a2713aSLionel Sambuc
304f4a2713aSLionel Sambuc Decl *
VisitNamespaceAliasDecl(NamespaceAliasDecl * D)305f4a2713aSLionel Sambuc TemplateDeclInstantiator::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
306f4a2713aSLionel Sambuc NamespaceAliasDecl *Inst
307f4a2713aSLionel Sambuc = NamespaceAliasDecl::Create(SemaRef.Context, Owner,
308f4a2713aSLionel Sambuc D->getNamespaceLoc(),
309f4a2713aSLionel Sambuc D->getAliasLoc(),
310f4a2713aSLionel Sambuc D->getIdentifier(),
311f4a2713aSLionel Sambuc D->getQualifierLoc(),
312f4a2713aSLionel Sambuc D->getTargetNameLoc(),
313f4a2713aSLionel Sambuc D->getNamespace());
314f4a2713aSLionel Sambuc Owner->addDecl(Inst);
315f4a2713aSLionel Sambuc return Inst;
316f4a2713aSLionel Sambuc }
317f4a2713aSLionel Sambuc
InstantiateTypedefNameDecl(TypedefNameDecl * D,bool IsTypeAlias)318f4a2713aSLionel Sambuc Decl *TemplateDeclInstantiator::InstantiateTypedefNameDecl(TypedefNameDecl *D,
319f4a2713aSLionel Sambuc bool IsTypeAlias) {
320f4a2713aSLionel Sambuc bool Invalid = false;
321f4a2713aSLionel Sambuc TypeSourceInfo *DI = D->getTypeSourceInfo();
322f4a2713aSLionel Sambuc if (DI->getType()->isInstantiationDependentType() ||
323f4a2713aSLionel Sambuc DI->getType()->isVariablyModifiedType()) {
324f4a2713aSLionel Sambuc DI = SemaRef.SubstType(DI, TemplateArgs,
325f4a2713aSLionel Sambuc D->getLocation(), D->getDeclName());
326f4a2713aSLionel Sambuc if (!DI) {
327f4a2713aSLionel Sambuc Invalid = true;
328f4a2713aSLionel Sambuc DI = SemaRef.Context.getTrivialTypeSourceInfo(SemaRef.Context.IntTy);
329f4a2713aSLionel Sambuc }
330f4a2713aSLionel Sambuc } else {
331f4a2713aSLionel Sambuc SemaRef.MarkDeclarationsReferencedInType(D->getLocation(), DI->getType());
332f4a2713aSLionel Sambuc }
333f4a2713aSLionel Sambuc
334f4a2713aSLionel Sambuc // HACK: g++ has a bug where it gets the value kind of ?: wrong.
335f4a2713aSLionel Sambuc // libstdc++ relies upon this bug in its implementation of common_type.
336f4a2713aSLionel Sambuc // If we happen to be processing that implementation, fake up the g++ ?:
337f4a2713aSLionel Sambuc // semantics. See LWG issue 2141 for more information on the bug.
338f4a2713aSLionel Sambuc const DecltypeType *DT = DI->getType()->getAs<DecltypeType>();
339f4a2713aSLionel Sambuc CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D->getDeclContext());
340f4a2713aSLionel Sambuc if (DT && RD && isa<ConditionalOperator>(DT->getUnderlyingExpr()) &&
341f4a2713aSLionel Sambuc DT->isReferenceType() &&
342f4a2713aSLionel Sambuc RD->getEnclosingNamespaceContext() == SemaRef.getStdNamespace() &&
343f4a2713aSLionel Sambuc RD->getIdentifier() && RD->getIdentifier()->isStr("common_type") &&
344f4a2713aSLionel Sambuc D->getIdentifier() && D->getIdentifier()->isStr("type") &&
345f4a2713aSLionel Sambuc SemaRef.getSourceManager().isInSystemHeader(D->getLocStart()))
346f4a2713aSLionel Sambuc // Fold it to the (non-reference) type which g++ would have produced.
347f4a2713aSLionel Sambuc DI = SemaRef.Context.getTrivialTypeSourceInfo(
348f4a2713aSLionel Sambuc DI->getType().getNonReferenceType());
349f4a2713aSLionel Sambuc
350f4a2713aSLionel Sambuc // Create the new typedef
351f4a2713aSLionel Sambuc TypedefNameDecl *Typedef;
352f4a2713aSLionel Sambuc if (IsTypeAlias)
353f4a2713aSLionel Sambuc Typedef = TypeAliasDecl::Create(SemaRef.Context, Owner, D->getLocStart(),
354f4a2713aSLionel Sambuc D->getLocation(), D->getIdentifier(), DI);
355f4a2713aSLionel Sambuc else
356f4a2713aSLionel Sambuc Typedef = TypedefDecl::Create(SemaRef.Context, Owner, D->getLocStart(),
357f4a2713aSLionel Sambuc D->getLocation(), D->getIdentifier(), DI);
358f4a2713aSLionel Sambuc if (Invalid)
359f4a2713aSLionel Sambuc Typedef->setInvalidDecl();
360f4a2713aSLionel Sambuc
361f4a2713aSLionel Sambuc // If the old typedef was the name for linkage purposes of an anonymous
362f4a2713aSLionel Sambuc // tag decl, re-establish that relationship for the new typedef.
363f4a2713aSLionel Sambuc if (const TagType *oldTagType = D->getUnderlyingType()->getAs<TagType>()) {
364f4a2713aSLionel Sambuc TagDecl *oldTag = oldTagType->getDecl();
365f4a2713aSLionel Sambuc if (oldTag->getTypedefNameForAnonDecl() == D && !Invalid) {
366f4a2713aSLionel Sambuc TagDecl *newTag = DI->getType()->castAs<TagType>()->getDecl();
367f4a2713aSLionel Sambuc assert(!newTag->hasNameForLinkage());
368f4a2713aSLionel Sambuc newTag->setTypedefNameForAnonDecl(Typedef);
369f4a2713aSLionel Sambuc }
370f4a2713aSLionel Sambuc }
371f4a2713aSLionel Sambuc
372*0a6a1f1dSLionel Sambuc if (TypedefNameDecl *Prev = getPreviousDeclForInstantiation(D)) {
373f4a2713aSLionel Sambuc NamedDecl *InstPrev = SemaRef.FindInstantiatedDecl(D->getLocation(), Prev,
374f4a2713aSLionel Sambuc TemplateArgs);
375f4a2713aSLionel Sambuc if (!InstPrev)
376*0a6a1f1dSLionel Sambuc return nullptr;
377f4a2713aSLionel Sambuc
378f4a2713aSLionel Sambuc TypedefNameDecl *InstPrevTypedef = cast<TypedefNameDecl>(InstPrev);
379f4a2713aSLionel Sambuc
380f4a2713aSLionel Sambuc // If the typedef types are not identical, reject them.
381f4a2713aSLionel Sambuc SemaRef.isIncompatibleTypedef(InstPrevTypedef, Typedef);
382f4a2713aSLionel Sambuc
383f4a2713aSLionel Sambuc Typedef->setPreviousDecl(InstPrevTypedef);
384f4a2713aSLionel Sambuc }
385f4a2713aSLionel Sambuc
386f4a2713aSLionel Sambuc SemaRef.InstantiateAttrs(TemplateArgs, D, Typedef);
387f4a2713aSLionel Sambuc
388f4a2713aSLionel Sambuc Typedef->setAccess(D->getAccess());
389f4a2713aSLionel Sambuc
390f4a2713aSLionel Sambuc return Typedef;
391f4a2713aSLionel Sambuc }
392f4a2713aSLionel Sambuc
VisitTypedefDecl(TypedefDecl * D)393f4a2713aSLionel Sambuc Decl *TemplateDeclInstantiator::VisitTypedefDecl(TypedefDecl *D) {
394f4a2713aSLionel Sambuc Decl *Typedef = InstantiateTypedefNameDecl(D, /*IsTypeAlias=*/false);
395*0a6a1f1dSLionel Sambuc if (Typedef)
396f4a2713aSLionel Sambuc Owner->addDecl(Typedef);
397f4a2713aSLionel Sambuc return Typedef;
398f4a2713aSLionel Sambuc }
399f4a2713aSLionel Sambuc
VisitTypeAliasDecl(TypeAliasDecl * D)400f4a2713aSLionel Sambuc Decl *TemplateDeclInstantiator::VisitTypeAliasDecl(TypeAliasDecl *D) {
401f4a2713aSLionel Sambuc Decl *Typedef = InstantiateTypedefNameDecl(D, /*IsTypeAlias=*/true);
402*0a6a1f1dSLionel Sambuc if (Typedef)
403f4a2713aSLionel Sambuc Owner->addDecl(Typedef);
404f4a2713aSLionel Sambuc return Typedef;
405f4a2713aSLionel Sambuc }
406f4a2713aSLionel Sambuc
407f4a2713aSLionel Sambuc Decl *
VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl * D)408f4a2713aSLionel Sambuc TemplateDeclInstantiator::VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D) {
409f4a2713aSLionel Sambuc // Create a local instantiation scope for this type alias template, which
410f4a2713aSLionel Sambuc // will contain the instantiations of the template parameters.
411f4a2713aSLionel Sambuc LocalInstantiationScope Scope(SemaRef);
412f4a2713aSLionel Sambuc
413f4a2713aSLionel Sambuc TemplateParameterList *TempParams = D->getTemplateParameters();
414f4a2713aSLionel Sambuc TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
415f4a2713aSLionel Sambuc if (!InstParams)
416*0a6a1f1dSLionel Sambuc return nullptr;
417f4a2713aSLionel Sambuc
418f4a2713aSLionel Sambuc TypeAliasDecl *Pattern = D->getTemplatedDecl();
419f4a2713aSLionel Sambuc
420*0a6a1f1dSLionel Sambuc TypeAliasTemplateDecl *PrevAliasTemplate = nullptr;
421*0a6a1f1dSLionel Sambuc if (getPreviousDeclForInstantiation<TypedefNameDecl>(Pattern)) {
422f4a2713aSLionel Sambuc DeclContext::lookup_result Found = Owner->lookup(Pattern->getDeclName());
423f4a2713aSLionel Sambuc if (!Found.empty()) {
424f4a2713aSLionel Sambuc PrevAliasTemplate = dyn_cast<TypeAliasTemplateDecl>(Found.front());
425f4a2713aSLionel Sambuc }
426f4a2713aSLionel Sambuc }
427f4a2713aSLionel Sambuc
428f4a2713aSLionel Sambuc TypeAliasDecl *AliasInst = cast_or_null<TypeAliasDecl>(
429f4a2713aSLionel Sambuc InstantiateTypedefNameDecl(Pattern, /*IsTypeAlias=*/true));
430f4a2713aSLionel Sambuc if (!AliasInst)
431*0a6a1f1dSLionel Sambuc return nullptr;
432f4a2713aSLionel Sambuc
433f4a2713aSLionel Sambuc TypeAliasTemplateDecl *Inst
434f4a2713aSLionel Sambuc = TypeAliasTemplateDecl::Create(SemaRef.Context, Owner, D->getLocation(),
435f4a2713aSLionel Sambuc D->getDeclName(), InstParams, AliasInst);
436*0a6a1f1dSLionel Sambuc AliasInst->setDescribedAliasTemplate(Inst);
437f4a2713aSLionel Sambuc if (PrevAliasTemplate)
438f4a2713aSLionel Sambuc Inst->setPreviousDecl(PrevAliasTemplate);
439f4a2713aSLionel Sambuc
440f4a2713aSLionel Sambuc Inst->setAccess(D->getAccess());
441f4a2713aSLionel Sambuc
442f4a2713aSLionel Sambuc if (!PrevAliasTemplate)
443f4a2713aSLionel Sambuc Inst->setInstantiatedFromMemberTemplate(D);
444f4a2713aSLionel Sambuc
445f4a2713aSLionel Sambuc Owner->addDecl(Inst);
446f4a2713aSLionel Sambuc
447f4a2713aSLionel Sambuc return Inst;
448f4a2713aSLionel Sambuc }
449f4a2713aSLionel Sambuc
VisitVarDecl(VarDecl * D)450f4a2713aSLionel Sambuc Decl *TemplateDeclInstantiator::VisitVarDecl(VarDecl *D) {
451f4a2713aSLionel Sambuc return VisitVarDecl(D, /*InstantiatingVarTemplate=*/false);
452f4a2713aSLionel Sambuc }
453f4a2713aSLionel Sambuc
VisitVarDecl(VarDecl * D,bool InstantiatingVarTemplate)454f4a2713aSLionel Sambuc Decl *TemplateDeclInstantiator::VisitVarDecl(VarDecl *D,
455f4a2713aSLionel Sambuc bool InstantiatingVarTemplate) {
456f4a2713aSLionel Sambuc
457f4a2713aSLionel Sambuc // If this is the variable for an anonymous struct or union,
458f4a2713aSLionel Sambuc // instantiate the anonymous struct/union type first.
459f4a2713aSLionel Sambuc if (const RecordType *RecordTy = D->getType()->getAs<RecordType>())
460f4a2713aSLionel Sambuc if (RecordTy->getDecl()->isAnonymousStructOrUnion())
461f4a2713aSLionel Sambuc if (!VisitCXXRecordDecl(cast<CXXRecordDecl>(RecordTy->getDecl())))
462*0a6a1f1dSLionel Sambuc return nullptr;
463f4a2713aSLionel Sambuc
464f4a2713aSLionel Sambuc // Do substitution on the type of the declaration
465f4a2713aSLionel Sambuc TypeSourceInfo *DI = SemaRef.SubstType(D->getTypeSourceInfo(),
466f4a2713aSLionel Sambuc TemplateArgs,
467f4a2713aSLionel Sambuc D->getTypeSpecStartLoc(),
468f4a2713aSLionel Sambuc D->getDeclName());
469f4a2713aSLionel Sambuc if (!DI)
470*0a6a1f1dSLionel Sambuc return nullptr;
471f4a2713aSLionel Sambuc
472f4a2713aSLionel Sambuc if (DI->getType()->isFunctionType()) {
473f4a2713aSLionel Sambuc SemaRef.Diag(D->getLocation(), diag::err_variable_instantiates_to_function)
474f4a2713aSLionel Sambuc << D->isStaticDataMember() << DI->getType();
475*0a6a1f1dSLionel Sambuc return nullptr;
476f4a2713aSLionel Sambuc }
477f4a2713aSLionel Sambuc
478f4a2713aSLionel Sambuc DeclContext *DC = Owner;
479f4a2713aSLionel Sambuc if (D->isLocalExternDecl())
480f4a2713aSLionel Sambuc SemaRef.adjustContextForLocalExternDecl(DC);
481f4a2713aSLionel Sambuc
482f4a2713aSLionel Sambuc // Build the instantiated declaration.
483f4a2713aSLionel Sambuc VarDecl *Var = VarDecl::Create(SemaRef.Context, DC, D->getInnerLocStart(),
484f4a2713aSLionel Sambuc D->getLocation(), D->getIdentifier(),
485f4a2713aSLionel Sambuc DI->getType(), DI, D->getStorageClass());
486f4a2713aSLionel Sambuc
487f4a2713aSLionel Sambuc // In ARC, infer 'retaining' for variables of retainable type.
488f4a2713aSLionel Sambuc if (SemaRef.getLangOpts().ObjCAutoRefCount &&
489f4a2713aSLionel Sambuc SemaRef.inferObjCARCLifetime(Var))
490f4a2713aSLionel Sambuc Var->setInvalidDecl();
491f4a2713aSLionel Sambuc
492f4a2713aSLionel Sambuc // Substitute the nested name specifier, if any.
493f4a2713aSLionel Sambuc if (SubstQualifier(D, Var))
494*0a6a1f1dSLionel Sambuc return nullptr;
495f4a2713aSLionel Sambuc
496f4a2713aSLionel Sambuc SemaRef.BuildVariableInstantiation(Var, D, TemplateArgs, LateAttrs, Owner,
497f4a2713aSLionel Sambuc StartingScope, InstantiatingVarTemplate);
498*0a6a1f1dSLionel Sambuc
499*0a6a1f1dSLionel Sambuc if (D->isNRVOVariable()) {
500*0a6a1f1dSLionel Sambuc QualType ReturnType = cast<FunctionDecl>(DC)->getReturnType();
501*0a6a1f1dSLionel Sambuc if (SemaRef.isCopyElisionCandidate(ReturnType, Var, false))
502*0a6a1f1dSLionel Sambuc Var->setNRVOVariable(true);
503*0a6a1f1dSLionel Sambuc }
504*0a6a1f1dSLionel Sambuc
505*0a6a1f1dSLionel Sambuc Var->setImplicit(D->isImplicit());
506*0a6a1f1dSLionel Sambuc
507f4a2713aSLionel Sambuc return Var;
508f4a2713aSLionel Sambuc }
509f4a2713aSLionel Sambuc
VisitAccessSpecDecl(AccessSpecDecl * D)510f4a2713aSLionel Sambuc Decl *TemplateDeclInstantiator::VisitAccessSpecDecl(AccessSpecDecl *D) {
511f4a2713aSLionel Sambuc AccessSpecDecl* AD
512f4a2713aSLionel Sambuc = AccessSpecDecl::Create(SemaRef.Context, D->getAccess(), Owner,
513f4a2713aSLionel Sambuc D->getAccessSpecifierLoc(), D->getColonLoc());
514f4a2713aSLionel Sambuc Owner->addHiddenDecl(AD);
515f4a2713aSLionel Sambuc return AD;
516f4a2713aSLionel Sambuc }
517f4a2713aSLionel Sambuc
VisitFieldDecl(FieldDecl * D)518f4a2713aSLionel Sambuc Decl *TemplateDeclInstantiator::VisitFieldDecl(FieldDecl *D) {
519f4a2713aSLionel Sambuc bool Invalid = false;
520f4a2713aSLionel Sambuc TypeSourceInfo *DI = D->getTypeSourceInfo();
521f4a2713aSLionel Sambuc if (DI->getType()->isInstantiationDependentType() ||
522f4a2713aSLionel Sambuc DI->getType()->isVariablyModifiedType()) {
523f4a2713aSLionel Sambuc DI = SemaRef.SubstType(DI, TemplateArgs,
524f4a2713aSLionel Sambuc D->getLocation(), D->getDeclName());
525f4a2713aSLionel Sambuc if (!DI) {
526f4a2713aSLionel Sambuc DI = D->getTypeSourceInfo();
527f4a2713aSLionel Sambuc Invalid = true;
528f4a2713aSLionel Sambuc } else if (DI->getType()->isFunctionType()) {
529f4a2713aSLionel Sambuc // C++ [temp.arg.type]p3:
530f4a2713aSLionel Sambuc // If a declaration acquires a function type through a type
531f4a2713aSLionel Sambuc // dependent on a template-parameter and this causes a
532f4a2713aSLionel Sambuc // declaration that does not use the syntactic form of a
533f4a2713aSLionel Sambuc // function declarator to have function type, the program is
534f4a2713aSLionel Sambuc // ill-formed.
535f4a2713aSLionel Sambuc SemaRef.Diag(D->getLocation(), diag::err_field_instantiates_to_function)
536f4a2713aSLionel Sambuc << DI->getType();
537f4a2713aSLionel Sambuc Invalid = true;
538f4a2713aSLionel Sambuc }
539f4a2713aSLionel Sambuc } else {
540f4a2713aSLionel Sambuc SemaRef.MarkDeclarationsReferencedInType(D->getLocation(), DI->getType());
541f4a2713aSLionel Sambuc }
542f4a2713aSLionel Sambuc
543f4a2713aSLionel Sambuc Expr *BitWidth = D->getBitWidth();
544f4a2713aSLionel Sambuc if (Invalid)
545*0a6a1f1dSLionel Sambuc BitWidth = nullptr;
546f4a2713aSLionel Sambuc else if (BitWidth) {
547f4a2713aSLionel Sambuc // The bit-width expression is a constant expression.
548f4a2713aSLionel Sambuc EnterExpressionEvaluationContext Unevaluated(SemaRef,
549f4a2713aSLionel Sambuc Sema::ConstantEvaluated);
550f4a2713aSLionel Sambuc
551f4a2713aSLionel Sambuc ExprResult InstantiatedBitWidth
552f4a2713aSLionel Sambuc = SemaRef.SubstExpr(BitWidth, TemplateArgs);
553f4a2713aSLionel Sambuc if (InstantiatedBitWidth.isInvalid()) {
554f4a2713aSLionel Sambuc Invalid = true;
555*0a6a1f1dSLionel Sambuc BitWidth = nullptr;
556f4a2713aSLionel Sambuc } else
557*0a6a1f1dSLionel Sambuc BitWidth = InstantiatedBitWidth.getAs<Expr>();
558f4a2713aSLionel Sambuc }
559f4a2713aSLionel Sambuc
560f4a2713aSLionel Sambuc FieldDecl *Field = SemaRef.CheckFieldDecl(D->getDeclName(),
561f4a2713aSLionel Sambuc DI->getType(), DI,
562f4a2713aSLionel Sambuc cast<RecordDecl>(Owner),
563f4a2713aSLionel Sambuc D->getLocation(),
564f4a2713aSLionel Sambuc D->isMutable(),
565f4a2713aSLionel Sambuc BitWidth,
566f4a2713aSLionel Sambuc D->getInClassInitStyle(),
567f4a2713aSLionel Sambuc D->getInnerLocStart(),
568f4a2713aSLionel Sambuc D->getAccess(),
569*0a6a1f1dSLionel Sambuc nullptr);
570f4a2713aSLionel Sambuc if (!Field) {
571f4a2713aSLionel Sambuc cast<Decl>(Owner)->setInvalidDecl();
572*0a6a1f1dSLionel Sambuc return nullptr;
573f4a2713aSLionel Sambuc }
574f4a2713aSLionel Sambuc
575f4a2713aSLionel Sambuc SemaRef.InstantiateAttrs(TemplateArgs, D, Field, LateAttrs, StartingScope);
576f4a2713aSLionel Sambuc
577f4a2713aSLionel Sambuc if (Field->hasAttrs())
578f4a2713aSLionel Sambuc SemaRef.CheckAlignasUnderalignment(Field);
579f4a2713aSLionel Sambuc
580f4a2713aSLionel Sambuc if (Invalid)
581f4a2713aSLionel Sambuc Field->setInvalidDecl();
582f4a2713aSLionel Sambuc
583f4a2713aSLionel Sambuc if (!Field->getDeclName()) {
584f4a2713aSLionel Sambuc // Keep track of where this decl came from.
585f4a2713aSLionel Sambuc SemaRef.Context.setInstantiatedFromUnnamedFieldDecl(Field, D);
586f4a2713aSLionel Sambuc }
587f4a2713aSLionel Sambuc if (CXXRecordDecl *Parent= dyn_cast<CXXRecordDecl>(Field->getDeclContext())) {
588f4a2713aSLionel Sambuc if (Parent->isAnonymousStructOrUnion() &&
589f4a2713aSLionel Sambuc Parent->getRedeclContext()->isFunctionOrMethod())
590f4a2713aSLionel Sambuc SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Field);
591f4a2713aSLionel Sambuc }
592f4a2713aSLionel Sambuc
593f4a2713aSLionel Sambuc Field->setImplicit(D->isImplicit());
594f4a2713aSLionel Sambuc Field->setAccess(D->getAccess());
595f4a2713aSLionel Sambuc Owner->addDecl(Field);
596f4a2713aSLionel Sambuc
597f4a2713aSLionel Sambuc return Field;
598f4a2713aSLionel Sambuc }
599f4a2713aSLionel Sambuc
VisitMSPropertyDecl(MSPropertyDecl * D)600f4a2713aSLionel Sambuc Decl *TemplateDeclInstantiator::VisitMSPropertyDecl(MSPropertyDecl *D) {
601f4a2713aSLionel Sambuc bool Invalid = false;
602f4a2713aSLionel Sambuc TypeSourceInfo *DI = D->getTypeSourceInfo();
603f4a2713aSLionel Sambuc
604f4a2713aSLionel Sambuc if (DI->getType()->isVariablyModifiedType()) {
605f4a2713aSLionel Sambuc SemaRef.Diag(D->getLocation(), diag::err_property_is_variably_modified)
606*0a6a1f1dSLionel Sambuc << D;
607f4a2713aSLionel Sambuc Invalid = true;
608f4a2713aSLionel Sambuc } else if (DI->getType()->isInstantiationDependentType()) {
609f4a2713aSLionel Sambuc DI = SemaRef.SubstType(DI, TemplateArgs,
610f4a2713aSLionel Sambuc D->getLocation(), D->getDeclName());
611f4a2713aSLionel Sambuc if (!DI) {
612f4a2713aSLionel Sambuc DI = D->getTypeSourceInfo();
613f4a2713aSLionel Sambuc Invalid = true;
614f4a2713aSLionel Sambuc } else if (DI->getType()->isFunctionType()) {
615f4a2713aSLionel Sambuc // C++ [temp.arg.type]p3:
616f4a2713aSLionel Sambuc // If a declaration acquires a function type through a type
617f4a2713aSLionel Sambuc // dependent on a template-parameter and this causes a
618f4a2713aSLionel Sambuc // declaration that does not use the syntactic form of a
619f4a2713aSLionel Sambuc // function declarator to have function type, the program is
620f4a2713aSLionel Sambuc // ill-formed.
621f4a2713aSLionel Sambuc SemaRef.Diag(D->getLocation(), diag::err_field_instantiates_to_function)
622f4a2713aSLionel Sambuc << DI->getType();
623f4a2713aSLionel Sambuc Invalid = true;
624f4a2713aSLionel Sambuc }
625f4a2713aSLionel Sambuc } else {
626f4a2713aSLionel Sambuc SemaRef.MarkDeclarationsReferencedInType(D->getLocation(), DI->getType());
627f4a2713aSLionel Sambuc }
628f4a2713aSLionel Sambuc
629*0a6a1f1dSLionel Sambuc MSPropertyDecl *Property = MSPropertyDecl::Create(
630*0a6a1f1dSLionel Sambuc SemaRef.Context, Owner, D->getLocation(), D->getDeclName(), DI->getType(),
631*0a6a1f1dSLionel Sambuc DI, D->getLocStart(), D->getGetterId(), D->getSetterId());
632f4a2713aSLionel Sambuc
633f4a2713aSLionel Sambuc SemaRef.InstantiateAttrs(TemplateArgs, D, Property, LateAttrs,
634f4a2713aSLionel Sambuc StartingScope);
635f4a2713aSLionel Sambuc
636f4a2713aSLionel Sambuc if (Invalid)
637f4a2713aSLionel Sambuc Property->setInvalidDecl();
638f4a2713aSLionel Sambuc
639f4a2713aSLionel Sambuc Property->setAccess(D->getAccess());
640f4a2713aSLionel Sambuc Owner->addDecl(Property);
641f4a2713aSLionel Sambuc
642f4a2713aSLionel Sambuc return Property;
643f4a2713aSLionel Sambuc }
644f4a2713aSLionel Sambuc
VisitIndirectFieldDecl(IndirectFieldDecl * D)645f4a2713aSLionel Sambuc Decl *TemplateDeclInstantiator::VisitIndirectFieldDecl(IndirectFieldDecl *D) {
646f4a2713aSLionel Sambuc NamedDecl **NamedChain =
647f4a2713aSLionel Sambuc new (SemaRef.Context)NamedDecl*[D->getChainingSize()];
648f4a2713aSLionel Sambuc
649f4a2713aSLionel Sambuc int i = 0;
650*0a6a1f1dSLionel Sambuc for (auto *PI : D->chain()) {
651*0a6a1f1dSLionel Sambuc NamedDecl *Next = SemaRef.FindInstantiatedDecl(D->getLocation(), PI,
652f4a2713aSLionel Sambuc TemplateArgs);
653f4a2713aSLionel Sambuc if (!Next)
654*0a6a1f1dSLionel Sambuc return nullptr;
655f4a2713aSLionel Sambuc
656f4a2713aSLionel Sambuc NamedChain[i++] = Next;
657f4a2713aSLionel Sambuc }
658f4a2713aSLionel Sambuc
659f4a2713aSLionel Sambuc QualType T = cast<FieldDecl>(NamedChain[i-1])->getType();
660*0a6a1f1dSLionel Sambuc IndirectFieldDecl *IndirectField = IndirectFieldDecl::Create(
661*0a6a1f1dSLionel Sambuc SemaRef.Context, Owner, D->getLocation(), D->getIdentifier(), T,
662f4a2713aSLionel Sambuc NamedChain, D->getChainingSize());
663f4a2713aSLionel Sambuc
664*0a6a1f1dSLionel Sambuc for (const auto *Attr : D->attrs())
665*0a6a1f1dSLionel Sambuc IndirectField->addAttr(Attr->clone(SemaRef.Context));
666f4a2713aSLionel Sambuc
667f4a2713aSLionel Sambuc IndirectField->setImplicit(D->isImplicit());
668f4a2713aSLionel Sambuc IndirectField->setAccess(D->getAccess());
669f4a2713aSLionel Sambuc Owner->addDecl(IndirectField);
670f4a2713aSLionel Sambuc return IndirectField;
671f4a2713aSLionel Sambuc }
672f4a2713aSLionel Sambuc
VisitFriendDecl(FriendDecl * D)673f4a2713aSLionel Sambuc Decl *TemplateDeclInstantiator::VisitFriendDecl(FriendDecl *D) {
674f4a2713aSLionel Sambuc // Handle friend type expressions by simply substituting template
675f4a2713aSLionel Sambuc // parameters into the pattern type and checking the result.
676f4a2713aSLionel Sambuc if (TypeSourceInfo *Ty = D->getFriendType()) {
677f4a2713aSLionel Sambuc TypeSourceInfo *InstTy;
678f4a2713aSLionel Sambuc // If this is an unsupported friend, don't bother substituting template
679f4a2713aSLionel Sambuc // arguments into it. The actual type referred to won't be used by any
680f4a2713aSLionel Sambuc // parts of Clang, and may not be valid for instantiating. Just use the
681f4a2713aSLionel Sambuc // same info for the instantiated friend.
682f4a2713aSLionel Sambuc if (D->isUnsupportedFriend()) {
683f4a2713aSLionel Sambuc InstTy = Ty;
684f4a2713aSLionel Sambuc } else {
685f4a2713aSLionel Sambuc InstTy = SemaRef.SubstType(Ty, TemplateArgs,
686f4a2713aSLionel Sambuc D->getLocation(), DeclarationName());
687f4a2713aSLionel Sambuc }
688f4a2713aSLionel Sambuc if (!InstTy)
689*0a6a1f1dSLionel Sambuc return nullptr;
690f4a2713aSLionel Sambuc
691f4a2713aSLionel Sambuc FriendDecl *FD = SemaRef.CheckFriendTypeDecl(D->getLocStart(),
692f4a2713aSLionel Sambuc D->getFriendLoc(), InstTy);
693f4a2713aSLionel Sambuc if (!FD)
694*0a6a1f1dSLionel Sambuc return nullptr;
695f4a2713aSLionel Sambuc
696f4a2713aSLionel Sambuc FD->setAccess(AS_public);
697f4a2713aSLionel Sambuc FD->setUnsupportedFriend(D->isUnsupportedFriend());
698f4a2713aSLionel Sambuc Owner->addDecl(FD);
699f4a2713aSLionel Sambuc return FD;
700f4a2713aSLionel Sambuc }
701f4a2713aSLionel Sambuc
702f4a2713aSLionel Sambuc NamedDecl *ND = D->getFriendDecl();
703f4a2713aSLionel Sambuc assert(ND && "friend decl must be a decl or a type!");
704f4a2713aSLionel Sambuc
705f4a2713aSLionel Sambuc // All of the Visit implementations for the various potential friend
706f4a2713aSLionel Sambuc // declarations have to be carefully written to work for friend
707f4a2713aSLionel Sambuc // objects, with the most important detail being that the target
708f4a2713aSLionel Sambuc // decl should almost certainly not be placed in Owner.
709f4a2713aSLionel Sambuc Decl *NewND = Visit(ND);
710*0a6a1f1dSLionel Sambuc if (!NewND) return nullptr;
711f4a2713aSLionel Sambuc
712f4a2713aSLionel Sambuc FriendDecl *FD =
713f4a2713aSLionel Sambuc FriendDecl::Create(SemaRef.Context, Owner, D->getLocation(),
714f4a2713aSLionel Sambuc cast<NamedDecl>(NewND), D->getFriendLoc());
715f4a2713aSLionel Sambuc FD->setAccess(AS_public);
716f4a2713aSLionel Sambuc FD->setUnsupportedFriend(D->isUnsupportedFriend());
717f4a2713aSLionel Sambuc Owner->addDecl(FD);
718f4a2713aSLionel Sambuc return FD;
719f4a2713aSLionel Sambuc }
720f4a2713aSLionel Sambuc
VisitStaticAssertDecl(StaticAssertDecl * D)721f4a2713aSLionel Sambuc Decl *TemplateDeclInstantiator::VisitStaticAssertDecl(StaticAssertDecl *D) {
722f4a2713aSLionel Sambuc Expr *AssertExpr = D->getAssertExpr();
723f4a2713aSLionel Sambuc
724f4a2713aSLionel Sambuc // The expression in a static assertion is a constant expression.
725f4a2713aSLionel Sambuc EnterExpressionEvaluationContext Unevaluated(SemaRef,
726f4a2713aSLionel Sambuc Sema::ConstantEvaluated);
727f4a2713aSLionel Sambuc
728f4a2713aSLionel Sambuc ExprResult InstantiatedAssertExpr
729f4a2713aSLionel Sambuc = SemaRef.SubstExpr(AssertExpr, TemplateArgs);
730f4a2713aSLionel Sambuc if (InstantiatedAssertExpr.isInvalid())
731*0a6a1f1dSLionel Sambuc return nullptr;
732f4a2713aSLionel Sambuc
733f4a2713aSLionel Sambuc return SemaRef.BuildStaticAssertDeclaration(D->getLocation(),
734f4a2713aSLionel Sambuc InstantiatedAssertExpr.get(),
735f4a2713aSLionel Sambuc D->getMessage(),
736f4a2713aSLionel Sambuc D->getRParenLoc(),
737f4a2713aSLionel Sambuc D->isFailed());
738f4a2713aSLionel Sambuc }
739f4a2713aSLionel Sambuc
VisitEnumDecl(EnumDecl * D)740f4a2713aSLionel Sambuc Decl *TemplateDeclInstantiator::VisitEnumDecl(EnumDecl *D) {
741*0a6a1f1dSLionel Sambuc EnumDecl *PrevDecl = nullptr;
742*0a6a1f1dSLionel Sambuc if (EnumDecl *PatternPrev = getPreviousDeclForInstantiation(D)) {
743f4a2713aSLionel Sambuc NamedDecl *Prev = SemaRef.FindInstantiatedDecl(D->getLocation(),
744*0a6a1f1dSLionel Sambuc PatternPrev,
745f4a2713aSLionel Sambuc TemplateArgs);
746*0a6a1f1dSLionel Sambuc if (!Prev) return nullptr;
747f4a2713aSLionel Sambuc PrevDecl = cast<EnumDecl>(Prev);
748f4a2713aSLionel Sambuc }
749f4a2713aSLionel Sambuc
750f4a2713aSLionel Sambuc EnumDecl *Enum = EnumDecl::Create(SemaRef.Context, Owner, D->getLocStart(),
751f4a2713aSLionel Sambuc D->getLocation(), D->getIdentifier(),
752f4a2713aSLionel Sambuc PrevDecl, D->isScoped(),
753f4a2713aSLionel Sambuc D->isScopedUsingClassTag(), D->isFixed());
754f4a2713aSLionel Sambuc if (D->isFixed()) {
755f4a2713aSLionel Sambuc if (TypeSourceInfo *TI = D->getIntegerTypeSourceInfo()) {
756f4a2713aSLionel Sambuc // If we have type source information for the underlying type, it means it
757f4a2713aSLionel Sambuc // has been explicitly set by the user. Perform substitution on it before
758f4a2713aSLionel Sambuc // moving on.
759f4a2713aSLionel Sambuc SourceLocation UnderlyingLoc = TI->getTypeLoc().getBeginLoc();
760f4a2713aSLionel Sambuc TypeSourceInfo *NewTI = SemaRef.SubstType(TI, TemplateArgs, UnderlyingLoc,
761f4a2713aSLionel Sambuc DeclarationName());
762f4a2713aSLionel Sambuc if (!NewTI || SemaRef.CheckEnumUnderlyingType(NewTI))
763f4a2713aSLionel Sambuc Enum->setIntegerType(SemaRef.Context.IntTy);
764f4a2713aSLionel Sambuc else
765f4a2713aSLionel Sambuc Enum->setIntegerTypeSourceInfo(NewTI);
766f4a2713aSLionel Sambuc } else {
767f4a2713aSLionel Sambuc assert(!D->getIntegerType()->isDependentType()
768f4a2713aSLionel Sambuc && "Dependent type without type source info");
769f4a2713aSLionel Sambuc Enum->setIntegerType(D->getIntegerType());
770f4a2713aSLionel Sambuc }
771f4a2713aSLionel Sambuc }
772f4a2713aSLionel Sambuc
773f4a2713aSLionel Sambuc SemaRef.InstantiateAttrs(TemplateArgs, D, Enum);
774f4a2713aSLionel Sambuc
775f4a2713aSLionel Sambuc Enum->setInstantiationOfMemberEnum(D, TSK_ImplicitInstantiation);
776f4a2713aSLionel Sambuc Enum->setAccess(D->getAccess());
777*0a6a1f1dSLionel Sambuc // Forward the mangling number from the template to the instantiated decl.
778*0a6a1f1dSLionel Sambuc SemaRef.Context.setManglingNumber(Enum, SemaRef.Context.getManglingNumber(D));
779*0a6a1f1dSLionel Sambuc if (SubstQualifier(D, Enum)) return nullptr;
780f4a2713aSLionel Sambuc Owner->addDecl(Enum);
781f4a2713aSLionel Sambuc
782f4a2713aSLionel Sambuc EnumDecl *Def = D->getDefinition();
783f4a2713aSLionel Sambuc if (Def && Def != D) {
784f4a2713aSLionel Sambuc // If this is an out-of-line definition of an enum member template, check
785f4a2713aSLionel Sambuc // that the underlying types match in the instantiation of both
786f4a2713aSLionel Sambuc // declarations.
787f4a2713aSLionel Sambuc if (TypeSourceInfo *TI = Def->getIntegerTypeSourceInfo()) {
788f4a2713aSLionel Sambuc SourceLocation UnderlyingLoc = TI->getTypeLoc().getBeginLoc();
789f4a2713aSLionel Sambuc QualType DefnUnderlying =
790f4a2713aSLionel Sambuc SemaRef.SubstType(TI->getType(), TemplateArgs,
791f4a2713aSLionel Sambuc UnderlyingLoc, DeclarationName());
792f4a2713aSLionel Sambuc SemaRef.CheckEnumRedeclaration(Def->getLocation(), Def->isScoped(),
793f4a2713aSLionel Sambuc DefnUnderlying, Enum);
794f4a2713aSLionel Sambuc }
795f4a2713aSLionel Sambuc }
796f4a2713aSLionel Sambuc
797f4a2713aSLionel Sambuc // C++11 [temp.inst]p1: The implicit instantiation of a class template
798f4a2713aSLionel Sambuc // specialization causes the implicit instantiation of the declarations, but
799f4a2713aSLionel Sambuc // not the definitions of scoped member enumerations.
800*0a6a1f1dSLionel Sambuc //
801*0a6a1f1dSLionel Sambuc // DR1484 clarifies that enumeration definitions inside of a template
802*0a6a1f1dSLionel Sambuc // declaration aren't considered entities that can be separately instantiated
803*0a6a1f1dSLionel Sambuc // from the rest of the entity they are declared inside of.
804*0a6a1f1dSLionel Sambuc if (isDeclWithinFunction(D) ? D == Def : Def && !Enum->isScoped()) {
805*0a6a1f1dSLionel Sambuc SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Enum);
806f4a2713aSLionel Sambuc InstantiateEnumDefinition(Enum, Def);
807*0a6a1f1dSLionel Sambuc }
808f4a2713aSLionel Sambuc
809f4a2713aSLionel Sambuc return Enum;
810f4a2713aSLionel Sambuc }
811f4a2713aSLionel Sambuc
InstantiateEnumDefinition(EnumDecl * Enum,EnumDecl * Pattern)812f4a2713aSLionel Sambuc void TemplateDeclInstantiator::InstantiateEnumDefinition(
813f4a2713aSLionel Sambuc EnumDecl *Enum, EnumDecl *Pattern) {
814f4a2713aSLionel Sambuc Enum->startDefinition();
815f4a2713aSLionel Sambuc
816f4a2713aSLionel Sambuc // Update the location to refer to the definition.
817f4a2713aSLionel Sambuc Enum->setLocation(Pattern->getLocation());
818f4a2713aSLionel Sambuc
819f4a2713aSLionel Sambuc SmallVector<Decl*, 4> Enumerators;
820f4a2713aSLionel Sambuc
821*0a6a1f1dSLionel Sambuc EnumConstantDecl *LastEnumConst = nullptr;
822*0a6a1f1dSLionel Sambuc for (auto *EC : Pattern->enumerators()) {
823f4a2713aSLionel Sambuc // The specified value for the enumerator.
824*0a6a1f1dSLionel Sambuc ExprResult Value((Expr *)nullptr);
825f4a2713aSLionel Sambuc if (Expr *UninstValue = EC->getInitExpr()) {
826f4a2713aSLionel Sambuc // The enumerator's value expression is a constant expression.
827f4a2713aSLionel Sambuc EnterExpressionEvaluationContext Unevaluated(SemaRef,
828f4a2713aSLionel Sambuc Sema::ConstantEvaluated);
829f4a2713aSLionel Sambuc
830f4a2713aSLionel Sambuc Value = SemaRef.SubstExpr(UninstValue, TemplateArgs);
831f4a2713aSLionel Sambuc }
832f4a2713aSLionel Sambuc
833f4a2713aSLionel Sambuc // Drop the initial value and continue.
834f4a2713aSLionel Sambuc bool isInvalid = false;
835f4a2713aSLionel Sambuc if (Value.isInvalid()) {
836*0a6a1f1dSLionel Sambuc Value = nullptr;
837f4a2713aSLionel Sambuc isInvalid = true;
838f4a2713aSLionel Sambuc }
839f4a2713aSLionel Sambuc
840f4a2713aSLionel Sambuc EnumConstantDecl *EnumConst
841f4a2713aSLionel Sambuc = SemaRef.CheckEnumConstant(Enum, LastEnumConst,
842f4a2713aSLionel Sambuc EC->getLocation(), EC->getIdentifier(),
843f4a2713aSLionel Sambuc Value.get());
844f4a2713aSLionel Sambuc
845f4a2713aSLionel Sambuc if (isInvalid) {
846f4a2713aSLionel Sambuc if (EnumConst)
847f4a2713aSLionel Sambuc EnumConst->setInvalidDecl();
848f4a2713aSLionel Sambuc Enum->setInvalidDecl();
849f4a2713aSLionel Sambuc }
850f4a2713aSLionel Sambuc
851f4a2713aSLionel Sambuc if (EnumConst) {
852*0a6a1f1dSLionel Sambuc SemaRef.InstantiateAttrs(TemplateArgs, EC, EnumConst);
853f4a2713aSLionel Sambuc
854f4a2713aSLionel Sambuc EnumConst->setAccess(Enum->getAccess());
855f4a2713aSLionel Sambuc Enum->addDecl(EnumConst);
856f4a2713aSLionel Sambuc Enumerators.push_back(EnumConst);
857f4a2713aSLionel Sambuc LastEnumConst = EnumConst;
858f4a2713aSLionel Sambuc
859f4a2713aSLionel Sambuc if (Pattern->getDeclContext()->isFunctionOrMethod() &&
860f4a2713aSLionel Sambuc !Enum->isScoped()) {
861f4a2713aSLionel Sambuc // If the enumeration is within a function or method, record the enum
862f4a2713aSLionel Sambuc // constant as a local.
863*0a6a1f1dSLionel Sambuc SemaRef.CurrentInstantiationScope->InstantiatedLocal(EC, EnumConst);
864f4a2713aSLionel Sambuc }
865f4a2713aSLionel Sambuc }
866f4a2713aSLionel Sambuc }
867f4a2713aSLionel Sambuc
868f4a2713aSLionel Sambuc // FIXME: Fixup LBraceLoc
869f4a2713aSLionel Sambuc SemaRef.ActOnEnumBody(Enum->getLocation(), SourceLocation(),
870f4a2713aSLionel Sambuc Enum->getRBraceLoc(), Enum,
871f4a2713aSLionel Sambuc Enumerators,
872*0a6a1f1dSLionel Sambuc nullptr, nullptr);
873f4a2713aSLionel Sambuc }
874f4a2713aSLionel Sambuc
VisitEnumConstantDecl(EnumConstantDecl * D)875f4a2713aSLionel Sambuc Decl *TemplateDeclInstantiator::VisitEnumConstantDecl(EnumConstantDecl *D) {
876f4a2713aSLionel Sambuc llvm_unreachable("EnumConstantDecls can only occur within EnumDecls.");
877f4a2713aSLionel Sambuc }
878f4a2713aSLionel Sambuc
VisitClassTemplateDecl(ClassTemplateDecl * D)879f4a2713aSLionel Sambuc Decl *TemplateDeclInstantiator::VisitClassTemplateDecl(ClassTemplateDecl *D) {
880f4a2713aSLionel Sambuc bool isFriend = (D->getFriendObjectKind() != Decl::FOK_None);
881f4a2713aSLionel Sambuc
882f4a2713aSLionel Sambuc // Create a local instantiation scope for this class template, which
883f4a2713aSLionel Sambuc // will contain the instantiations of the template parameters.
884f4a2713aSLionel Sambuc LocalInstantiationScope Scope(SemaRef);
885f4a2713aSLionel Sambuc TemplateParameterList *TempParams = D->getTemplateParameters();
886f4a2713aSLionel Sambuc TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
887f4a2713aSLionel Sambuc if (!InstParams)
888*0a6a1f1dSLionel Sambuc return nullptr;
889f4a2713aSLionel Sambuc
890f4a2713aSLionel Sambuc CXXRecordDecl *Pattern = D->getTemplatedDecl();
891f4a2713aSLionel Sambuc
892f4a2713aSLionel Sambuc // Instantiate the qualifier. We have to do this first in case
893f4a2713aSLionel Sambuc // we're a friend declaration, because if we are then we need to put
894f4a2713aSLionel Sambuc // the new declaration in the appropriate context.
895f4a2713aSLionel Sambuc NestedNameSpecifierLoc QualifierLoc = Pattern->getQualifierLoc();
896f4a2713aSLionel Sambuc if (QualifierLoc) {
897f4a2713aSLionel Sambuc QualifierLoc = SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc,
898f4a2713aSLionel Sambuc TemplateArgs);
899f4a2713aSLionel Sambuc if (!QualifierLoc)
900*0a6a1f1dSLionel Sambuc return nullptr;
901f4a2713aSLionel Sambuc }
902f4a2713aSLionel Sambuc
903*0a6a1f1dSLionel Sambuc CXXRecordDecl *PrevDecl = nullptr;
904*0a6a1f1dSLionel Sambuc ClassTemplateDecl *PrevClassTemplate = nullptr;
905f4a2713aSLionel Sambuc
906*0a6a1f1dSLionel Sambuc if (!isFriend && getPreviousDeclForInstantiation(Pattern)) {
907f4a2713aSLionel Sambuc DeclContext::lookup_result Found = Owner->lookup(Pattern->getDeclName());
908f4a2713aSLionel Sambuc if (!Found.empty()) {
909f4a2713aSLionel Sambuc PrevClassTemplate = dyn_cast<ClassTemplateDecl>(Found.front());
910f4a2713aSLionel Sambuc if (PrevClassTemplate)
911f4a2713aSLionel Sambuc PrevDecl = PrevClassTemplate->getTemplatedDecl();
912f4a2713aSLionel Sambuc }
913f4a2713aSLionel Sambuc }
914f4a2713aSLionel Sambuc
915f4a2713aSLionel Sambuc // If this isn't a friend, then it's a member template, in which
916f4a2713aSLionel Sambuc // case we just want to build the instantiation in the
917f4a2713aSLionel Sambuc // specialization. If it is a friend, we want to build it in
918f4a2713aSLionel Sambuc // the appropriate context.
919f4a2713aSLionel Sambuc DeclContext *DC = Owner;
920f4a2713aSLionel Sambuc if (isFriend) {
921f4a2713aSLionel Sambuc if (QualifierLoc) {
922f4a2713aSLionel Sambuc CXXScopeSpec SS;
923f4a2713aSLionel Sambuc SS.Adopt(QualifierLoc);
924f4a2713aSLionel Sambuc DC = SemaRef.computeDeclContext(SS);
925*0a6a1f1dSLionel Sambuc if (!DC) return nullptr;
926f4a2713aSLionel Sambuc } else {
927f4a2713aSLionel Sambuc DC = SemaRef.FindInstantiatedContext(Pattern->getLocation(),
928f4a2713aSLionel Sambuc Pattern->getDeclContext(),
929f4a2713aSLionel Sambuc TemplateArgs);
930f4a2713aSLionel Sambuc }
931f4a2713aSLionel Sambuc
932f4a2713aSLionel Sambuc // Look for a previous declaration of the template in the owning
933f4a2713aSLionel Sambuc // context.
934f4a2713aSLionel Sambuc LookupResult R(SemaRef, Pattern->getDeclName(), Pattern->getLocation(),
935f4a2713aSLionel Sambuc Sema::LookupOrdinaryName, Sema::ForRedeclaration);
936f4a2713aSLionel Sambuc SemaRef.LookupQualifiedName(R, DC);
937f4a2713aSLionel Sambuc
938f4a2713aSLionel Sambuc if (R.isSingleResult()) {
939f4a2713aSLionel Sambuc PrevClassTemplate = R.getAsSingle<ClassTemplateDecl>();
940f4a2713aSLionel Sambuc if (PrevClassTemplate)
941f4a2713aSLionel Sambuc PrevDecl = PrevClassTemplate->getTemplatedDecl();
942f4a2713aSLionel Sambuc }
943f4a2713aSLionel Sambuc
944f4a2713aSLionel Sambuc if (!PrevClassTemplate && QualifierLoc) {
945f4a2713aSLionel Sambuc SemaRef.Diag(Pattern->getLocation(), diag::err_not_tag_in_scope)
946f4a2713aSLionel Sambuc << D->getTemplatedDecl()->getTagKind() << Pattern->getDeclName() << DC
947f4a2713aSLionel Sambuc << QualifierLoc.getSourceRange();
948*0a6a1f1dSLionel Sambuc return nullptr;
949f4a2713aSLionel Sambuc }
950f4a2713aSLionel Sambuc
951f4a2713aSLionel Sambuc bool AdoptedPreviousTemplateParams = false;
952f4a2713aSLionel Sambuc if (PrevClassTemplate) {
953f4a2713aSLionel Sambuc bool Complain = true;
954f4a2713aSLionel Sambuc
955f4a2713aSLionel Sambuc // HACK: libstdc++ 4.2.1 contains an ill-formed friend class
956f4a2713aSLionel Sambuc // template for struct std::tr1::__detail::_Map_base, where the
957f4a2713aSLionel Sambuc // template parameters of the friend declaration don't match the
958f4a2713aSLionel Sambuc // template parameters of the original declaration. In this one
959f4a2713aSLionel Sambuc // case, we don't complain about the ill-formed friend
960f4a2713aSLionel Sambuc // declaration.
961f4a2713aSLionel Sambuc if (isFriend && Pattern->getIdentifier() &&
962f4a2713aSLionel Sambuc Pattern->getIdentifier()->isStr("_Map_base") &&
963f4a2713aSLionel Sambuc DC->isNamespace() &&
964f4a2713aSLionel Sambuc cast<NamespaceDecl>(DC)->getIdentifier() &&
965f4a2713aSLionel Sambuc cast<NamespaceDecl>(DC)->getIdentifier()->isStr("__detail")) {
966f4a2713aSLionel Sambuc DeclContext *DCParent = DC->getParent();
967f4a2713aSLionel Sambuc if (DCParent->isNamespace() &&
968f4a2713aSLionel Sambuc cast<NamespaceDecl>(DCParent)->getIdentifier() &&
969f4a2713aSLionel Sambuc cast<NamespaceDecl>(DCParent)->getIdentifier()->isStr("tr1")) {
970*0a6a1f1dSLionel Sambuc if (cast<Decl>(DCParent)->isInStdNamespace())
971f4a2713aSLionel Sambuc Complain = false;
972f4a2713aSLionel Sambuc }
973f4a2713aSLionel Sambuc }
974f4a2713aSLionel Sambuc
975f4a2713aSLionel Sambuc TemplateParameterList *PrevParams
976f4a2713aSLionel Sambuc = PrevClassTemplate->getTemplateParameters();
977f4a2713aSLionel Sambuc
978f4a2713aSLionel Sambuc // Make sure the parameter lists match.
979f4a2713aSLionel Sambuc if (!SemaRef.TemplateParameterListsAreEqual(InstParams, PrevParams,
980f4a2713aSLionel Sambuc Complain,
981f4a2713aSLionel Sambuc Sema::TPL_TemplateMatch)) {
982f4a2713aSLionel Sambuc if (Complain)
983*0a6a1f1dSLionel Sambuc return nullptr;
984f4a2713aSLionel Sambuc
985f4a2713aSLionel Sambuc AdoptedPreviousTemplateParams = true;
986f4a2713aSLionel Sambuc InstParams = PrevParams;
987f4a2713aSLionel Sambuc }
988f4a2713aSLionel Sambuc
989f4a2713aSLionel Sambuc // Do some additional validation, then merge default arguments
990f4a2713aSLionel Sambuc // from the existing declarations.
991f4a2713aSLionel Sambuc if (!AdoptedPreviousTemplateParams &&
992f4a2713aSLionel Sambuc SemaRef.CheckTemplateParameterList(InstParams, PrevParams,
993f4a2713aSLionel Sambuc Sema::TPC_ClassTemplate))
994*0a6a1f1dSLionel Sambuc return nullptr;
995f4a2713aSLionel Sambuc }
996f4a2713aSLionel Sambuc }
997f4a2713aSLionel Sambuc
998f4a2713aSLionel Sambuc CXXRecordDecl *RecordInst
999f4a2713aSLionel Sambuc = CXXRecordDecl::Create(SemaRef.Context, Pattern->getTagKind(), DC,
1000f4a2713aSLionel Sambuc Pattern->getLocStart(), Pattern->getLocation(),
1001f4a2713aSLionel Sambuc Pattern->getIdentifier(), PrevDecl,
1002f4a2713aSLionel Sambuc /*DelayTypeCreation=*/true);
1003f4a2713aSLionel Sambuc
1004f4a2713aSLionel Sambuc if (QualifierLoc)
1005f4a2713aSLionel Sambuc RecordInst->setQualifierInfo(QualifierLoc);
1006f4a2713aSLionel Sambuc
1007f4a2713aSLionel Sambuc ClassTemplateDecl *Inst
1008f4a2713aSLionel Sambuc = ClassTemplateDecl::Create(SemaRef.Context, DC, D->getLocation(),
1009f4a2713aSLionel Sambuc D->getIdentifier(), InstParams, RecordInst,
1010f4a2713aSLionel Sambuc PrevClassTemplate);
1011f4a2713aSLionel Sambuc RecordInst->setDescribedClassTemplate(Inst);
1012f4a2713aSLionel Sambuc
1013f4a2713aSLionel Sambuc if (isFriend) {
1014f4a2713aSLionel Sambuc if (PrevClassTemplate)
1015f4a2713aSLionel Sambuc Inst->setAccess(PrevClassTemplate->getAccess());
1016f4a2713aSLionel Sambuc else
1017f4a2713aSLionel Sambuc Inst->setAccess(D->getAccess());
1018f4a2713aSLionel Sambuc
1019f4a2713aSLionel Sambuc Inst->setObjectOfFriendDecl();
1020f4a2713aSLionel Sambuc // TODO: do we want to track the instantiation progeny of this
1021f4a2713aSLionel Sambuc // friend target decl?
1022f4a2713aSLionel Sambuc } else {
1023f4a2713aSLionel Sambuc Inst->setAccess(D->getAccess());
1024f4a2713aSLionel Sambuc if (!PrevClassTemplate)
1025f4a2713aSLionel Sambuc Inst->setInstantiatedFromMemberTemplate(D);
1026f4a2713aSLionel Sambuc }
1027f4a2713aSLionel Sambuc
1028f4a2713aSLionel Sambuc // Trigger creation of the type for the instantiation.
1029f4a2713aSLionel Sambuc SemaRef.Context.getInjectedClassNameType(RecordInst,
1030f4a2713aSLionel Sambuc Inst->getInjectedClassNameSpecialization());
1031f4a2713aSLionel Sambuc
1032f4a2713aSLionel Sambuc // Finish handling of friends.
1033f4a2713aSLionel Sambuc if (isFriend) {
1034f4a2713aSLionel Sambuc DC->makeDeclVisibleInContext(Inst);
1035f4a2713aSLionel Sambuc Inst->setLexicalDeclContext(Owner);
1036f4a2713aSLionel Sambuc RecordInst->setLexicalDeclContext(Owner);
1037f4a2713aSLionel Sambuc return Inst;
1038f4a2713aSLionel Sambuc }
1039f4a2713aSLionel Sambuc
1040f4a2713aSLionel Sambuc if (D->isOutOfLine()) {
1041f4a2713aSLionel Sambuc Inst->setLexicalDeclContext(D->getLexicalDeclContext());
1042f4a2713aSLionel Sambuc RecordInst->setLexicalDeclContext(D->getLexicalDeclContext());
1043f4a2713aSLionel Sambuc }
1044f4a2713aSLionel Sambuc
1045f4a2713aSLionel Sambuc Owner->addDecl(Inst);
1046f4a2713aSLionel Sambuc
1047f4a2713aSLionel Sambuc if (!PrevClassTemplate) {
1048f4a2713aSLionel Sambuc // Queue up any out-of-line partial specializations of this member
1049f4a2713aSLionel Sambuc // class template; the client will force their instantiation once
1050f4a2713aSLionel Sambuc // the enclosing class has been instantiated.
1051f4a2713aSLionel Sambuc SmallVector<ClassTemplatePartialSpecializationDecl *, 4> PartialSpecs;
1052f4a2713aSLionel Sambuc D->getPartialSpecializations(PartialSpecs);
1053f4a2713aSLionel Sambuc for (unsigned I = 0, N = PartialSpecs.size(); I != N; ++I)
1054f4a2713aSLionel Sambuc if (PartialSpecs[I]->getFirstDecl()->isOutOfLine())
1055f4a2713aSLionel Sambuc OutOfLinePartialSpecs.push_back(std::make_pair(Inst, PartialSpecs[I]));
1056f4a2713aSLionel Sambuc }
1057f4a2713aSLionel Sambuc
1058f4a2713aSLionel Sambuc return Inst;
1059f4a2713aSLionel Sambuc }
1060f4a2713aSLionel Sambuc
1061f4a2713aSLionel Sambuc Decl *
VisitClassTemplatePartialSpecializationDecl(ClassTemplatePartialSpecializationDecl * D)1062f4a2713aSLionel Sambuc TemplateDeclInstantiator::VisitClassTemplatePartialSpecializationDecl(
1063f4a2713aSLionel Sambuc ClassTemplatePartialSpecializationDecl *D) {
1064f4a2713aSLionel Sambuc ClassTemplateDecl *ClassTemplate = D->getSpecializedTemplate();
1065f4a2713aSLionel Sambuc
1066f4a2713aSLionel Sambuc // Lookup the already-instantiated declaration in the instantiation
1067f4a2713aSLionel Sambuc // of the class template and return that.
1068f4a2713aSLionel Sambuc DeclContext::lookup_result Found
1069f4a2713aSLionel Sambuc = Owner->lookup(ClassTemplate->getDeclName());
1070f4a2713aSLionel Sambuc if (Found.empty())
1071*0a6a1f1dSLionel Sambuc return nullptr;
1072f4a2713aSLionel Sambuc
1073f4a2713aSLionel Sambuc ClassTemplateDecl *InstClassTemplate
1074f4a2713aSLionel Sambuc = dyn_cast<ClassTemplateDecl>(Found.front());
1075f4a2713aSLionel Sambuc if (!InstClassTemplate)
1076*0a6a1f1dSLionel Sambuc return nullptr;
1077f4a2713aSLionel Sambuc
1078f4a2713aSLionel Sambuc if (ClassTemplatePartialSpecializationDecl *Result
1079f4a2713aSLionel Sambuc = InstClassTemplate->findPartialSpecInstantiatedFromMember(D))
1080f4a2713aSLionel Sambuc return Result;
1081f4a2713aSLionel Sambuc
1082f4a2713aSLionel Sambuc return InstantiateClassTemplatePartialSpecialization(InstClassTemplate, D);
1083f4a2713aSLionel Sambuc }
1084f4a2713aSLionel Sambuc
VisitVarTemplateDecl(VarTemplateDecl * D)1085f4a2713aSLionel Sambuc Decl *TemplateDeclInstantiator::VisitVarTemplateDecl(VarTemplateDecl *D) {
1086f4a2713aSLionel Sambuc assert(D->getTemplatedDecl()->isStaticDataMember() &&
1087f4a2713aSLionel Sambuc "Only static data member templates are allowed.");
1088f4a2713aSLionel Sambuc
1089f4a2713aSLionel Sambuc // Create a local instantiation scope for this variable template, which
1090f4a2713aSLionel Sambuc // will contain the instantiations of the template parameters.
1091f4a2713aSLionel Sambuc LocalInstantiationScope Scope(SemaRef);
1092f4a2713aSLionel Sambuc TemplateParameterList *TempParams = D->getTemplateParameters();
1093f4a2713aSLionel Sambuc TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
1094f4a2713aSLionel Sambuc if (!InstParams)
1095*0a6a1f1dSLionel Sambuc return nullptr;
1096f4a2713aSLionel Sambuc
1097f4a2713aSLionel Sambuc VarDecl *Pattern = D->getTemplatedDecl();
1098*0a6a1f1dSLionel Sambuc VarTemplateDecl *PrevVarTemplate = nullptr;
1099f4a2713aSLionel Sambuc
1100*0a6a1f1dSLionel Sambuc if (getPreviousDeclForInstantiation(Pattern)) {
1101f4a2713aSLionel Sambuc DeclContext::lookup_result Found = Owner->lookup(Pattern->getDeclName());
1102f4a2713aSLionel Sambuc if (!Found.empty())
1103f4a2713aSLionel Sambuc PrevVarTemplate = dyn_cast<VarTemplateDecl>(Found.front());
1104f4a2713aSLionel Sambuc }
1105f4a2713aSLionel Sambuc
1106f4a2713aSLionel Sambuc VarDecl *VarInst =
1107f4a2713aSLionel Sambuc cast_or_null<VarDecl>(VisitVarDecl(Pattern,
1108f4a2713aSLionel Sambuc /*InstantiatingVarTemplate=*/true));
1109f4a2713aSLionel Sambuc
1110f4a2713aSLionel Sambuc DeclContext *DC = Owner;
1111f4a2713aSLionel Sambuc
1112f4a2713aSLionel Sambuc VarTemplateDecl *Inst = VarTemplateDecl::Create(
1113f4a2713aSLionel Sambuc SemaRef.Context, DC, D->getLocation(), D->getIdentifier(), InstParams,
1114*0a6a1f1dSLionel Sambuc VarInst);
1115f4a2713aSLionel Sambuc VarInst->setDescribedVarTemplate(Inst);
1116*0a6a1f1dSLionel Sambuc Inst->setPreviousDecl(PrevVarTemplate);
1117f4a2713aSLionel Sambuc
1118f4a2713aSLionel Sambuc Inst->setAccess(D->getAccess());
1119f4a2713aSLionel Sambuc if (!PrevVarTemplate)
1120f4a2713aSLionel Sambuc Inst->setInstantiatedFromMemberTemplate(D);
1121f4a2713aSLionel Sambuc
1122f4a2713aSLionel Sambuc if (D->isOutOfLine()) {
1123f4a2713aSLionel Sambuc Inst->setLexicalDeclContext(D->getLexicalDeclContext());
1124f4a2713aSLionel Sambuc VarInst->setLexicalDeclContext(D->getLexicalDeclContext());
1125f4a2713aSLionel Sambuc }
1126f4a2713aSLionel Sambuc
1127f4a2713aSLionel Sambuc Owner->addDecl(Inst);
1128f4a2713aSLionel Sambuc
1129f4a2713aSLionel Sambuc if (!PrevVarTemplate) {
1130f4a2713aSLionel Sambuc // Queue up any out-of-line partial specializations of this member
1131f4a2713aSLionel Sambuc // variable template; the client will force their instantiation once
1132f4a2713aSLionel Sambuc // the enclosing class has been instantiated.
1133f4a2713aSLionel Sambuc SmallVector<VarTemplatePartialSpecializationDecl *, 4> PartialSpecs;
1134f4a2713aSLionel Sambuc D->getPartialSpecializations(PartialSpecs);
1135f4a2713aSLionel Sambuc for (unsigned I = 0, N = PartialSpecs.size(); I != N; ++I)
1136f4a2713aSLionel Sambuc if (PartialSpecs[I]->getFirstDecl()->isOutOfLine())
1137f4a2713aSLionel Sambuc OutOfLineVarPartialSpecs.push_back(
1138f4a2713aSLionel Sambuc std::make_pair(Inst, PartialSpecs[I]));
1139f4a2713aSLionel Sambuc }
1140f4a2713aSLionel Sambuc
1141f4a2713aSLionel Sambuc return Inst;
1142f4a2713aSLionel Sambuc }
1143f4a2713aSLionel Sambuc
VisitVarTemplatePartialSpecializationDecl(VarTemplatePartialSpecializationDecl * D)1144f4a2713aSLionel Sambuc Decl *TemplateDeclInstantiator::VisitVarTemplatePartialSpecializationDecl(
1145f4a2713aSLionel Sambuc VarTemplatePartialSpecializationDecl *D) {
1146f4a2713aSLionel Sambuc assert(D->isStaticDataMember() &&
1147f4a2713aSLionel Sambuc "Only static data member templates are allowed.");
1148f4a2713aSLionel Sambuc
1149f4a2713aSLionel Sambuc VarTemplateDecl *VarTemplate = D->getSpecializedTemplate();
1150f4a2713aSLionel Sambuc
1151f4a2713aSLionel Sambuc // Lookup the already-instantiated declaration and return that.
1152f4a2713aSLionel Sambuc DeclContext::lookup_result Found = Owner->lookup(VarTemplate->getDeclName());
1153f4a2713aSLionel Sambuc assert(!Found.empty() && "Instantiation found nothing?");
1154f4a2713aSLionel Sambuc
1155f4a2713aSLionel Sambuc VarTemplateDecl *InstVarTemplate = dyn_cast<VarTemplateDecl>(Found.front());
1156f4a2713aSLionel Sambuc assert(InstVarTemplate && "Instantiation did not find a variable template?");
1157f4a2713aSLionel Sambuc
1158f4a2713aSLionel Sambuc if (VarTemplatePartialSpecializationDecl *Result =
1159f4a2713aSLionel Sambuc InstVarTemplate->findPartialSpecInstantiatedFromMember(D))
1160f4a2713aSLionel Sambuc return Result;
1161f4a2713aSLionel Sambuc
1162f4a2713aSLionel Sambuc return InstantiateVarTemplatePartialSpecialization(InstVarTemplate, D);
1163f4a2713aSLionel Sambuc }
1164f4a2713aSLionel Sambuc
1165f4a2713aSLionel Sambuc Decl *
VisitFunctionTemplateDecl(FunctionTemplateDecl * D)1166f4a2713aSLionel Sambuc TemplateDeclInstantiator::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
1167f4a2713aSLionel Sambuc // Create a local instantiation scope for this function template, which
1168f4a2713aSLionel Sambuc // will contain the instantiations of the template parameters and then get
1169f4a2713aSLionel Sambuc // merged with the local instantiation scope for the function template
1170f4a2713aSLionel Sambuc // itself.
1171f4a2713aSLionel Sambuc LocalInstantiationScope Scope(SemaRef);
1172f4a2713aSLionel Sambuc
1173f4a2713aSLionel Sambuc TemplateParameterList *TempParams = D->getTemplateParameters();
1174f4a2713aSLionel Sambuc TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
1175f4a2713aSLionel Sambuc if (!InstParams)
1176*0a6a1f1dSLionel Sambuc return nullptr;
1177f4a2713aSLionel Sambuc
1178*0a6a1f1dSLionel Sambuc FunctionDecl *Instantiated = nullptr;
1179f4a2713aSLionel Sambuc if (CXXMethodDecl *DMethod = dyn_cast<CXXMethodDecl>(D->getTemplatedDecl()))
1180f4a2713aSLionel Sambuc Instantiated = cast_or_null<FunctionDecl>(VisitCXXMethodDecl(DMethod,
1181f4a2713aSLionel Sambuc InstParams));
1182f4a2713aSLionel Sambuc else
1183f4a2713aSLionel Sambuc Instantiated = cast_or_null<FunctionDecl>(VisitFunctionDecl(
1184f4a2713aSLionel Sambuc D->getTemplatedDecl(),
1185f4a2713aSLionel Sambuc InstParams));
1186f4a2713aSLionel Sambuc
1187f4a2713aSLionel Sambuc if (!Instantiated)
1188*0a6a1f1dSLionel Sambuc return nullptr;
1189f4a2713aSLionel Sambuc
1190f4a2713aSLionel Sambuc // Link the instantiated function template declaration to the function
1191f4a2713aSLionel Sambuc // template from which it was instantiated.
1192f4a2713aSLionel Sambuc FunctionTemplateDecl *InstTemplate
1193f4a2713aSLionel Sambuc = Instantiated->getDescribedFunctionTemplate();
1194f4a2713aSLionel Sambuc InstTemplate->setAccess(D->getAccess());
1195f4a2713aSLionel Sambuc assert(InstTemplate &&
1196f4a2713aSLionel Sambuc "VisitFunctionDecl/CXXMethodDecl didn't create a template!");
1197f4a2713aSLionel Sambuc
1198f4a2713aSLionel Sambuc bool isFriend = (InstTemplate->getFriendObjectKind() != Decl::FOK_None);
1199f4a2713aSLionel Sambuc
1200f4a2713aSLionel Sambuc // Link the instantiation back to the pattern *unless* this is a
1201f4a2713aSLionel Sambuc // non-definition friend declaration.
1202f4a2713aSLionel Sambuc if (!InstTemplate->getInstantiatedFromMemberTemplate() &&
1203f4a2713aSLionel Sambuc !(isFriend && !D->getTemplatedDecl()->isThisDeclarationADefinition()))
1204f4a2713aSLionel Sambuc InstTemplate->setInstantiatedFromMemberTemplate(D);
1205f4a2713aSLionel Sambuc
1206f4a2713aSLionel Sambuc // Make declarations visible in the appropriate context.
1207f4a2713aSLionel Sambuc if (!isFriend) {
1208f4a2713aSLionel Sambuc Owner->addDecl(InstTemplate);
1209f4a2713aSLionel Sambuc } else if (InstTemplate->getDeclContext()->isRecord() &&
1210*0a6a1f1dSLionel Sambuc !getPreviousDeclForInstantiation(D)) {
1211f4a2713aSLionel Sambuc SemaRef.CheckFriendAccess(InstTemplate);
1212f4a2713aSLionel Sambuc }
1213f4a2713aSLionel Sambuc
1214f4a2713aSLionel Sambuc return InstTemplate;
1215f4a2713aSLionel Sambuc }
1216f4a2713aSLionel Sambuc
VisitCXXRecordDecl(CXXRecordDecl * D)1217f4a2713aSLionel Sambuc Decl *TemplateDeclInstantiator::VisitCXXRecordDecl(CXXRecordDecl *D) {
1218*0a6a1f1dSLionel Sambuc CXXRecordDecl *PrevDecl = nullptr;
1219f4a2713aSLionel Sambuc if (D->isInjectedClassName())
1220f4a2713aSLionel Sambuc PrevDecl = cast<CXXRecordDecl>(Owner);
1221*0a6a1f1dSLionel Sambuc else if (CXXRecordDecl *PatternPrev = getPreviousDeclForInstantiation(D)) {
1222f4a2713aSLionel Sambuc NamedDecl *Prev = SemaRef.FindInstantiatedDecl(D->getLocation(),
1223*0a6a1f1dSLionel Sambuc PatternPrev,
1224f4a2713aSLionel Sambuc TemplateArgs);
1225*0a6a1f1dSLionel Sambuc if (!Prev) return nullptr;
1226f4a2713aSLionel Sambuc PrevDecl = cast<CXXRecordDecl>(Prev);
1227f4a2713aSLionel Sambuc }
1228f4a2713aSLionel Sambuc
1229f4a2713aSLionel Sambuc CXXRecordDecl *Record
1230f4a2713aSLionel Sambuc = CXXRecordDecl::Create(SemaRef.Context, D->getTagKind(), Owner,
1231f4a2713aSLionel Sambuc D->getLocStart(), D->getLocation(),
1232f4a2713aSLionel Sambuc D->getIdentifier(), PrevDecl);
1233f4a2713aSLionel Sambuc
1234f4a2713aSLionel Sambuc // Substitute the nested name specifier, if any.
1235f4a2713aSLionel Sambuc if (SubstQualifier(D, Record))
1236*0a6a1f1dSLionel Sambuc return nullptr;
1237f4a2713aSLionel Sambuc
1238f4a2713aSLionel Sambuc Record->setImplicit(D->isImplicit());
1239f4a2713aSLionel Sambuc // FIXME: Check against AS_none is an ugly hack to work around the issue that
1240f4a2713aSLionel Sambuc // the tag decls introduced by friend class declarations don't have an access
1241f4a2713aSLionel Sambuc // specifier. Remove once this area of the code gets sorted out.
1242f4a2713aSLionel Sambuc if (D->getAccess() != AS_none)
1243f4a2713aSLionel Sambuc Record->setAccess(D->getAccess());
1244f4a2713aSLionel Sambuc if (!D->isInjectedClassName())
1245f4a2713aSLionel Sambuc Record->setInstantiationOfMemberClass(D, TSK_ImplicitInstantiation);
1246f4a2713aSLionel Sambuc
1247f4a2713aSLionel Sambuc // If the original function was part of a friend declaration,
1248f4a2713aSLionel Sambuc // inherit its namespace state.
1249f4a2713aSLionel Sambuc if (D->getFriendObjectKind())
1250f4a2713aSLionel Sambuc Record->setObjectOfFriendDecl();
1251f4a2713aSLionel Sambuc
1252f4a2713aSLionel Sambuc // Make sure that anonymous structs and unions are recorded.
1253*0a6a1f1dSLionel Sambuc if (D->isAnonymousStructOrUnion())
1254f4a2713aSLionel Sambuc Record->setAnonymousStructOrUnion(true);
1255*0a6a1f1dSLionel Sambuc
1256*0a6a1f1dSLionel Sambuc if (D->isLocalClass())
1257f4a2713aSLionel Sambuc SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Record);
1258*0a6a1f1dSLionel Sambuc
1259*0a6a1f1dSLionel Sambuc // Forward the mangling number from the template to the instantiated decl.
1260*0a6a1f1dSLionel Sambuc SemaRef.Context.setManglingNumber(Record,
1261*0a6a1f1dSLionel Sambuc SemaRef.Context.getManglingNumber(D));
1262f4a2713aSLionel Sambuc
1263f4a2713aSLionel Sambuc Owner->addDecl(Record);
1264*0a6a1f1dSLionel Sambuc
1265*0a6a1f1dSLionel Sambuc // DR1484 clarifies that the members of a local class are instantiated as part
1266*0a6a1f1dSLionel Sambuc // of the instantiation of their enclosing entity.
1267*0a6a1f1dSLionel Sambuc if (D->isCompleteDefinition() && D->isLocalClass()) {
1268*0a6a1f1dSLionel Sambuc SemaRef.InstantiateClass(D->getLocation(), Record, D, TemplateArgs,
1269*0a6a1f1dSLionel Sambuc TSK_ImplicitInstantiation,
1270*0a6a1f1dSLionel Sambuc /*Complain=*/true);
1271*0a6a1f1dSLionel Sambuc SemaRef.InstantiateClassMembers(D->getLocation(), Record, TemplateArgs,
1272*0a6a1f1dSLionel Sambuc TSK_ImplicitInstantiation);
1273*0a6a1f1dSLionel Sambuc }
1274*0a6a1f1dSLionel Sambuc
1275*0a6a1f1dSLionel Sambuc SemaRef.DiagnoseUnusedNestedTypedefs(Record);
1276*0a6a1f1dSLionel Sambuc
1277f4a2713aSLionel Sambuc return Record;
1278f4a2713aSLionel Sambuc }
1279f4a2713aSLionel Sambuc
1280f4a2713aSLionel Sambuc /// \brief Adjust the given function type for an instantiation of the
1281f4a2713aSLionel Sambuc /// given declaration, to cope with modifications to the function's type that
1282f4a2713aSLionel Sambuc /// aren't reflected in the type-source information.
1283f4a2713aSLionel Sambuc ///
1284f4a2713aSLionel Sambuc /// \param D The declaration we're instantiating.
1285f4a2713aSLionel Sambuc /// \param TInfo The already-instantiated type.
adjustFunctionTypeForInstantiation(ASTContext & Context,FunctionDecl * D,TypeSourceInfo * TInfo)1286f4a2713aSLionel Sambuc static QualType adjustFunctionTypeForInstantiation(ASTContext &Context,
1287f4a2713aSLionel Sambuc FunctionDecl *D,
1288f4a2713aSLionel Sambuc TypeSourceInfo *TInfo) {
1289f4a2713aSLionel Sambuc const FunctionProtoType *OrigFunc
1290f4a2713aSLionel Sambuc = D->getType()->castAs<FunctionProtoType>();
1291f4a2713aSLionel Sambuc const FunctionProtoType *NewFunc
1292f4a2713aSLionel Sambuc = TInfo->getType()->castAs<FunctionProtoType>();
1293f4a2713aSLionel Sambuc if (OrigFunc->getExtInfo() == NewFunc->getExtInfo())
1294f4a2713aSLionel Sambuc return TInfo->getType();
1295f4a2713aSLionel Sambuc
1296f4a2713aSLionel Sambuc FunctionProtoType::ExtProtoInfo NewEPI = NewFunc->getExtProtoInfo();
1297f4a2713aSLionel Sambuc NewEPI.ExtInfo = OrigFunc->getExtInfo();
1298*0a6a1f1dSLionel Sambuc return Context.getFunctionType(NewFunc->getReturnType(),
1299*0a6a1f1dSLionel Sambuc NewFunc->getParamTypes(), NewEPI);
1300f4a2713aSLionel Sambuc }
1301f4a2713aSLionel Sambuc
1302f4a2713aSLionel Sambuc /// Normal class members are of more specific types and therefore
1303f4a2713aSLionel Sambuc /// don't make it here. This function serves two purposes:
1304f4a2713aSLionel Sambuc /// 1) instantiating function templates
1305f4a2713aSLionel Sambuc /// 2) substituting friend declarations
VisitFunctionDecl(FunctionDecl * D,TemplateParameterList * TemplateParams)1306f4a2713aSLionel Sambuc Decl *TemplateDeclInstantiator::VisitFunctionDecl(FunctionDecl *D,
1307f4a2713aSLionel Sambuc TemplateParameterList *TemplateParams) {
1308f4a2713aSLionel Sambuc // Check whether there is already a function template specialization for
1309f4a2713aSLionel Sambuc // this declaration.
1310f4a2713aSLionel Sambuc FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
1311f4a2713aSLionel Sambuc if (FunctionTemplate && !TemplateParams) {
1312f4a2713aSLionel Sambuc ArrayRef<TemplateArgument> Innermost = TemplateArgs.getInnermost();
1313f4a2713aSLionel Sambuc
1314*0a6a1f1dSLionel Sambuc void *InsertPos = nullptr;
1315f4a2713aSLionel Sambuc FunctionDecl *SpecFunc
1316*0a6a1f1dSLionel Sambuc = FunctionTemplate->findSpecialization(Innermost, InsertPos);
1317f4a2713aSLionel Sambuc
1318f4a2713aSLionel Sambuc // If we already have a function template specialization, return it.
1319f4a2713aSLionel Sambuc if (SpecFunc)
1320f4a2713aSLionel Sambuc return SpecFunc;
1321f4a2713aSLionel Sambuc }
1322f4a2713aSLionel Sambuc
1323f4a2713aSLionel Sambuc bool isFriend;
1324f4a2713aSLionel Sambuc if (FunctionTemplate)
1325f4a2713aSLionel Sambuc isFriend = (FunctionTemplate->getFriendObjectKind() != Decl::FOK_None);
1326f4a2713aSLionel Sambuc else
1327f4a2713aSLionel Sambuc isFriend = (D->getFriendObjectKind() != Decl::FOK_None);
1328f4a2713aSLionel Sambuc
1329*0a6a1f1dSLionel Sambuc bool MergeWithParentScope = (TemplateParams != nullptr) ||
1330f4a2713aSLionel Sambuc Owner->isFunctionOrMethod() ||
1331f4a2713aSLionel Sambuc !(isa<Decl>(Owner) &&
1332f4a2713aSLionel Sambuc cast<Decl>(Owner)->isDefinedOutsideFunctionOrMethod());
1333f4a2713aSLionel Sambuc LocalInstantiationScope Scope(SemaRef, MergeWithParentScope);
1334f4a2713aSLionel Sambuc
1335f4a2713aSLionel Sambuc SmallVector<ParmVarDecl *, 4> Params;
1336f4a2713aSLionel Sambuc TypeSourceInfo *TInfo = SubstFunctionType(D, Params);
1337f4a2713aSLionel Sambuc if (!TInfo)
1338*0a6a1f1dSLionel Sambuc return nullptr;
1339f4a2713aSLionel Sambuc QualType T = adjustFunctionTypeForInstantiation(SemaRef.Context, D, TInfo);
1340f4a2713aSLionel Sambuc
1341f4a2713aSLionel Sambuc NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc();
1342f4a2713aSLionel Sambuc if (QualifierLoc) {
1343f4a2713aSLionel Sambuc QualifierLoc = SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc,
1344f4a2713aSLionel Sambuc TemplateArgs);
1345f4a2713aSLionel Sambuc if (!QualifierLoc)
1346*0a6a1f1dSLionel Sambuc return nullptr;
1347f4a2713aSLionel Sambuc }
1348f4a2713aSLionel Sambuc
1349f4a2713aSLionel Sambuc // If we're instantiating a local function declaration, put the result
1350f4a2713aSLionel Sambuc // in the enclosing namespace; otherwise we need to find the instantiated
1351f4a2713aSLionel Sambuc // context.
1352f4a2713aSLionel Sambuc DeclContext *DC;
1353f4a2713aSLionel Sambuc if (D->isLocalExternDecl()) {
1354f4a2713aSLionel Sambuc DC = Owner;
1355f4a2713aSLionel Sambuc SemaRef.adjustContextForLocalExternDecl(DC);
1356f4a2713aSLionel Sambuc } else if (isFriend && QualifierLoc) {
1357f4a2713aSLionel Sambuc CXXScopeSpec SS;
1358f4a2713aSLionel Sambuc SS.Adopt(QualifierLoc);
1359f4a2713aSLionel Sambuc DC = SemaRef.computeDeclContext(SS);
1360*0a6a1f1dSLionel Sambuc if (!DC) return nullptr;
1361f4a2713aSLionel Sambuc } else {
1362f4a2713aSLionel Sambuc DC = SemaRef.FindInstantiatedContext(D->getLocation(), D->getDeclContext(),
1363f4a2713aSLionel Sambuc TemplateArgs);
1364f4a2713aSLionel Sambuc }
1365f4a2713aSLionel Sambuc
1366f4a2713aSLionel Sambuc FunctionDecl *Function =
1367f4a2713aSLionel Sambuc FunctionDecl::Create(SemaRef.Context, DC, D->getInnerLocStart(),
1368f4a2713aSLionel Sambuc D->getNameInfo(), T, TInfo,
1369f4a2713aSLionel Sambuc D->getCanonicalDecl()->getStorageClass(),
1370f4a2713aSLionel Sambuc D->isInlineSpecified(), D->hasWrittenPrototype(),
1371f4a2713aSLionel Sambuc D->isConstexpr());
1372f4a2713aSLionel Sambuc Function->setRangeEnd(D->getSourceRange().getEnd());
1373f4a2713aSLionel Sambuc
1374f4a2713aSLionel Sambuc if (D->isInlined())
1375f4a2713aSLionel Sambuc Function->setImplicitlyInline();
1376f4a2713aSLionel Sambuc
1377f4a2713aSLionel Sambuc if (QualifierLoc)
1378f4a2713aSLionel Sambuc Function->setQualifierInfo(QualifierLoc);
1379f4a2713aSLionel Sambuc
1380f4a2713aSLionel Sambuc if (D->isLocalExternDecl())
1381f4a2713aSLionel Sambuc Function->setLocalExternDecl();
1382f4a2713aSLionel Sambuc
1383f4a2713aSLionel Sambuc DeclContext *LexicalDC = Owner;
1384f4a2713aSLionel Sambuc if (!isFriend && D->isOutOfLine() && !D->isLocalExternDecl()) {
1385f4a2713aSLionel Sambuc assert(D->getDeclContext()->isFileContext());
1386f4a2713aSLionel Sambuc LexicalDC = D->getDeclContext();
1387f4a2713aSLionel Sambuc }
1388f4a2713aSLionel Sambuc
1389f4a2713aSLionel Sambuc Function->setLexicalDeclContext(LexicalDC);
1390f4a2713aSLionel Sambuc
1391f4a2713aSLionel Sambuc // Attach the parameters
1392f4a2713aSLionel Sambuc for (unsigned P = 0; P < Params.size(); ++P)
1393f4a2713aSLionel Sambuc if (Params[P])
1394f4a2713aSLionel Sambuc Params[P]->setOwningFunction(Function);
1395f4a2713aSLionel Sambuc Function->setParams(Params);
1396f4a2713aSLionel Sambuc
1397f4a2713aSLionel Sambuc SourceLocation InstantiateAtPOI;
1398f4a2713aSLionel Sambuc if (TemplateParams) {
1399f4a2713aSLionel Sambuc // Our resulting instantiation is actually a function template, since we
1400f4a2713aSLionel Sambuc // are substituting only the outer template parameters. For example, given
1401f4a2713aSLionel Sambuc //
1402f4a2713aSLionel Sambuc // template<typename T>
1403f4a2713aSLionel Sambuc // struct X {
1404f4a2713aSLionel Sambuc // template<typename U> friend void f(T, U);
1405f4a2713aSLionel Sambuc // };
1406f4a2713aSLionel Sambuc //
1407f4a2713aSLionel Sambuc // X<int> x;
1408f4a2713aSLionel Sambuc //
1409f4a2713aSLionel Sambuc // We are instantiating the friend function template "f" within X<int>,
1410f4a2713aSLionel Sambuc // which means substituting int for T, but leaving "f" as a friend function
1411f4a2713aSLionel Sambuc // template.
1412f4a2713aSLionel Sambuc // Build the function template itself.
1413f4a2713aSLionel Sambuc FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, DC,
1414f4a2713aSLionel Sambuc Function->getLocation(),
1415f4a2713aSLionel Sambuc Function->getDeclName(),
1416f4a2713aSLionel Sambuc TemplateParams, Function);
1417f4a2713aSLionel Sambuc Function->setDescribedFunctionTemplate(FunctionTemplate);
1418f4a2713aSLionel Sambuc
1419f4a2713aSLionel Sambuc FunctionTemplate->setLexicalDeclContext(LexicalDC);
1420f4a2713aSLionel Sambuc
1421f4a2713aSLionel Sambuc if (isFriend && D->isThisDeclarationADefinition()) {
1422f4a2713aSLionel Sambuc // TODO: should we remember this connection regardless of whether
1423f4a2713aSLionel Sambuc // the friend declaration provided a body?
1424f4a2713aSLionel Sambuc FunctionTemplate->setInstantiatedFromMemberTemplate(
1425f4a2713aSLionel Sambuc D->getDescribedFunctionTemplate());
1426f4a2713aSLionel Sambuc }
1427f4a2713aSLionel Sambuc } else if (FunctionTemplate) {
1428f4a2713aSLionel Sambuc // Record this function template specialization.
1429f4a2713aSLionel Sambuc ArrayRef<TemplateArgument> Innermost = TemplateArgs.getInnermost();
1430f4a2713aSLionel Sambuc Function->setFunctionTemplateSpecialization(FunctionTemplate,
1431f4a2713aSLionel Sambuc TemplateArgumentList::CreateCopy(SemaRef.Context,
1432f4a2713aSLionel Sambuc Innermost.begin(),
1433f4a2713aSLionel Sambuc Innermost.size()),
1434*0a6a1f1dSLionel Sambuc /*InsertPos=*/nullptr);
1435f4a2713aSLionel Sambuc } else if (isFriend) {
1436f4a2713aSLionel Sambuc // Note, we need this connection even if the friend doesn't have a body.
1437f4a2713aSLionel Sambuc // Its body may exist but not have been attached yet due to deferred
1438f4a2713aSLionel Sambuc // parsing.
1439f4a2713aSLionel Sambuc // FIXME: It might be cleaner to set this when attaching the body to the
1440f4a2713aSLionel Sambuc // friend function declaration, however that would require finding all the
1441f4a2713aSLionel Sambuc // instantiations and modifying them.
1442f4a2713aSLionel Sambuc Function->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation);
1443f4a2713aSLionel Sambuc }
1444f4a2713aSLionel Sambuc
1445f4a2713aSLionel Sambuc if (InitFunctionInstantiation(Function, D))
1446f4a2713aSLionel Sambuc Function->setInvalidDecl();
1447f4a2713aSLionel Sambuc
1448f4a2713aSLionel Sambuc bool isExplicitSpecialization = false;
1449f4a2713aSLionel Sambuc
1450f4a2713aSLionel Sambuc LookupResult Previous(
1451f4a2713aSLionel Sambuc SemaRef, Function->getDeclName(), SourceLocation(),
1452f4a2713aSLionel Sambuc D->isLocalExternDecl() ? Sema::LookupRedeclarationWithLinkage
1453f4a2713aSLionel Sambuc : Sema::LookupOrdinaryName,
1454f4a2713aSLionel Sambuc Sema::ForRedeclaration);
1455f4a2713aSLionel Sambuc
1456f4a2713aSLionel Sambuc if (DependentFunctionTemplateSpecializationInfo *Info
1457f4a2713aSLionel Sambuc = D->getDependentSpecializationInfo()) {
1458f4a2713aSLionel Sambuc assert(isFriend && "non-friend has dependent specialization info?");
1459f4a2713aSLionel Sambuc
1460f4a2713aSLionel Sambuc // This needs to be set now for future sanity.
1461f4a2713aSLionel Sambuc Function->setObjectOfFriendDecl();
1462f4a2713aSLionel Sambuc
1463f4a2713aSLionel Sambuc // Instantiate the explicit template arguments.
1464f4a2713aSLionel Sambuc TemplateArgumentListInfo ExplicitArgs(Info->getLAngleLoc(),
1465f4a2713aSLionel Sambuc Info->getRAngleLoc());
1466f4a2713aSLionel Sambuc if (SemaRef.Subst(Info->getTemplateArgs(), Info->getNumTemplateArgs(),
1467f4a2713aSLionel Sambuc ExplicitArgs, TemplateArgs))
1468*0a6a1f1dSLionel Sambuc return nullptr;
1469f4a2713aSLionel Sambuc
1470f4a2713aSLionel Sambuc // Map the candidate templates to their instantiations.
1471f4a2713aSLionel Sambuc for (unsigned I = 0, E = Info->getNumTemplates(); I != E; ++I) {
1472f4a2713aSLionel Sambuc Decl *Temp = SemaRef.FindInstantiatedDecl(D->getLocation(),
1473f4a2713aSLionel Sambuc Info->getTemplate(I),
1474f4a2713aSLionel Sambuc TemplateArgs);
1475*0a6a1f1dSLionel Sambuc if (!Temp) return nullptr;
1476f4a2713aSLionel Sambuc
1477f4a2713aSLionel Sambuc Previous.addDecl(cast<FunctionTemplateDecl>(Temp));
1478f4a2713aSLionel Sambuc }
1479f4a2713aSLionel Sambuc
1480f4a2713aSLionel Sambuc if (SemaRef.CheckFunctionTemplateSpecialization(Function,
1481f4a2713aSLionel Sambuc &ExplicitArgs,
1482f4a2713aSLionel Sambuc Previous))
1483f4a2713aSLionel Sambuc Function->setInvalidDecl();
1484f4a2713aSLionel Sambuc
1485f4a2713aSLionel Sambuc isExplicitSpecialization = true;
1486f4a2713aSLionel Sambuc
1487f4a2713aSLionel Sambuc } else if (TemplateParams || !FunctionTemplate) {
1488f4a2713aSLionel Sambuc // Look only into the namespace where the friend would be declared to
1489f4a2713aSLionel Sambuc // find a previous declaration. This is the innermost enclosing namespace,
1490f4a2713aSLionel Sambuc // as described in ActOnFriendFunctionDecl.
1491f4a2713aSLionel Sambuc SemaRef.LookupQualifiedName(Previous, DC);
1492f4a2713aSLionel Sambuc
1493f4a2713aSLionel Sambuc // In C++, the previous declaration we find might be a tag type
1494f4a2713aSLionel Sambuc // (class or enum). In this case, the new declaration will hide the
1495f4a2713aSLionel Sambuc // tag type. Note that this does does not apply if we're declaring a
1496f4a2713aSLionel Sambuc // typedef (C++ [dcl.typedef]p4).
1497f4a2713aSLionel Sambuc if (Previous.isSingleTagDecl())
1498f4a2713aSLionel Sambuc Previous.clear();
1499f4a2713aSLionel Sambuc }
1500f4a2713aSLionel Sambuc
1501*0a6a1f1dSLionel Sambuc SemaRef.CheckFunctionDeclaration(/*Scope*/ nullptr, Function, Previous,
1502f4a2713aSLionel Sambuc isExplicitSpecialization);
1503f4a2713aSLionel Sambuc
1504f4a2713aSLionel Sambuc NamedDecl *PrincipalDecl = (TemplateParams
1505f4a2713aSLionel Sambuc ? cast<NamedDecl>(FunctionTemplate)
1506f4a2713aSLionel Sambuc : Function);
1507f4a2713aSLionel Sambuc
1508f4a2713aSLionel Sambuc // If the original function was part of a friend declaration,
1509f4a2713aSLionel Sambuc // inherit its namespace state and add it to the owner.
1510f4a2713aSLionel Sambuc if (isFriend) {
1511f4a2713aSLionel Sambuc PrincipalDecl->setObjectOfFriendDecl();
1512f4a2713aSLionel Sambuc DC->makeDeclVisibleInContext(PrincipalDecl);
1513f4a2713aSLionel Sambuc
1514*0a6a1f1dSLionel Sambuc bool QueuedInstantiation = false;
1515f4a2713aSLionel Sambuc
1516*0a6a1f1dSLionel Sambuc // C++11 [temp.friend]p4 (DR329):
1517*0a6a1f1dSLionel Sambuc // When a function is defined in a friend function declaration in a class
1518*0a6a1f1dSLionel Sambuc // template, the function is instantiated when the function is odr-used.
1519*0a6a1f1dSLionel Sambuc // The same restrictions on multiple declarations and definitions that
1520*0a6a1f1dSLionel Sambuc // apply to non-template function declarations and definitions also apply
1521*0a6a1f1dSLionel Sambuc // to these implicit definitions.
1522*0a6a1f1dSLionel Sambuc if (D->isThisDeclarationADefinition()) {
1523f4a2713aSLionel Sambuc // Check for a function body.
1524*0a6a1f1dSLionel Sambuc const FunctionDecl *Definition = nullptr;
1525f4a2713aSLionel Sambuc if (Function->isDefined(Definition) &&
1526f4a2713aSLionel Sambuc Definition->getTemplateSpecializationKind() == TSK_Undeclared) {
1527*0a6a1f1dSLionel Sambuc SemaRef.Diag(Function->getLocation(), diag::err_redefinition)
1528*0a6a1f1dSLionel Sambuc << Function->getDeclName();
1529f4a2713aSLionel Sambuc SemaRef.Diag(Definition->getLocation(), diag::note_previous_definition);
1530f4a2713aSLionel Sambuc }
1531f4a2713aSLionel Sambuc // Check for redefinitions due to other instantiations of this or
1532f4a2713aSLionel Sambuc // a similar friend function.
1533*0a6a1f1dSLionel Sambuc else for (auto R : Function->redecls()) {
1534*0a6a1f1dSLionel Sambuc if (R == Function)
1535f4a2713aSLionel Sambuc continue;
1536*0a6a1f1dSLionel Sambuc
1537*0a6a1f1dSLionel Sambuc // If some prior declaration of this function has been used, we need
1538*0a6a1f1dSLionel Sambuc // to instantiate its definition.
1539*0a6a1f1dSLionel Sambuc if (!QueuedInstantiation && R->isUsed(false)) {
1540*0a6a1f1dSLionel Sambuc if (MemberSpecializationInfo *MSInfo =
1541*0a6a1f1dSLionel Sambuc Function->getMemberSpecializationInfo()) {
1542f4a2713aSLionel Sambuc if (MSInfo->getPointOfInstantiation().isInvalid()) {
1543f4a2713aSLionel Sambuc SourceLocation Loc = R->getLocation(); // FIXME
1544f4a2713aSLionel Sambuc MSInfo->setPointOfInstantiation(Loc);
1545f4a2713aSLionel Sambuc SemaRef.PendingLocalImplicitInstantiations.push_back(
1546f4a2713aSLionel Sambuc std::make_pair(Function, Loc));
1547*0a6a1f1dSLionel Sambuc QueuedInstantiation = true;
1548f4a2713aSLionel Sambuc }
1549f4a2713aSLionel Sambuc }
1550f4a2713aSLionel Sambuc }
1551*0a6a1f1dSLionel Sambuc
1552*0a6a1f1dSLionel Sambuc // If some prior declaration of this function was a friend with an
1553*0a6a1f1dSLionel Sambuc // uninstantiated definition, reject it.
1554*0a6a1f1dSLionel Sambuc if (R->getFriendObjectKind()) {
1555*0a6a1f1dSLionel Sambuc if (const FunctionDecl *RPattern =
1556*0a6a1f1dSLionel Sambuc R->getTemplateInstantiationPattern()) {
1557f4a2713aSLionel Sambuc if (RPattern->isDefined(RPattern)) {
1558*0a6a1f1dSLionel Sambuc SemaRef.Diag(Function->getLocation(), diag::err_redefinition)
1559f4a2713aSLionel Sambuc << Function->getDeclName();
1560f4a2713aSLionel Sambuc SemaRef.Diag(R->getLocation(), diag::note_previous_definition);
1561f4a2713aSLionel Sambuc break;
1562f4a2713aSLionel Sambuc }
1563f4a2713aSLionel Sambuc }
1564f4a2713aSLionel Sambuc }
1565f4a2713aSLionel Sambuc }
1566f4a2713aSLionel Sambuc }
1567*0a6a1f1dSLionel Sambuc }
1568f4a2713aSLionel Sambuc
1569f4a2713aSLionel Sambuc if (Function->isLocalExternDecl() && !Function->getPreviousDecl())
1570f4a2713aSLionel Sambuc DC->makeDeclVisibleInContext(PrincipalDecl);
1571f4a2713aSLionel Sambuc
1572f4a2713aSLionel Sambuc if (Function->isOverloadedOperator() && !DC->isRecord() &&
1573f4a2713aSLionel Sambuc PrincipalDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary))
1574f4a2713aSLionel Sambuc PrincipalDecl->setNonMemberOperator();
1575f4a2713aSLionel Sambuc
1576f4a2713aSLionel Sambuc assert(!D->isDefaulted() && "only methods should be defaulted");
1577f4a2713aSLionel Sambuc return Function;
1578f4a2713aSLionel Sambuc }
1579f4a2713aSLionel Sambuc
1580f4a2713aSLionel Sambuc Decl *
VisitCXXMethodDecl(CXXMethodDecl * D,TemplateParameterList * TemplateParams,bool IsClassScopeSpecialization)1581f4a2713aSLionel Sambuc TemplateDeclInstantiator::VisitCXXMethodDecl(CXXMethodDecl *D,
1582f4a2713aSLionel Sambuc TemplateParameterList *TemplateParams,
1583f4a2713aSLionel Sambuc bool IsClassScopeSpecialization) {
1584f4a2713aSLionel Sambuc FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
1585f4a2713aSLionel Sambuc if (FunctionTemplate && !TemplateParams) {
1586f4a2713aSLionel Sambuc // We are creating a function template specialization from a function
1587f4a2713aSLionel Sambuc // template. Check whether there is already a function template
1588f4a2713aSLionel Sambuc // specialization for this particular set of template arguments.
1589f4a2713aSLionel Sambuc ArrayRef<TemplateArgument> Innermost = TemplateArgs.getInnermost();
1590f4a2713aSLionel Sambuc
1591*0a6a1f1dSLionel Sambuc void *InsertPos = nullptr;
1592f4a2713aSLionel Sambuc FunctionDecl *SpecFunc
1593*0a6a1f1dSLionel Sambuc = FunctionTemplate->findSpecialization(Innermost, InsertPos);
1594f4a2713aSLionel Sambuc
1595f4a2713aSLionel Sambuc // If we already have a function template specialization, return it.
1596f4a2713aSLionel Sambuc if (SpecFunc)
1597f4a2713aSLionel Sambuc return SpecFunc;
1598f4a2713aSLionel Sambuc }
1599f4a2713aSLionel Sambuc
1600f4a2713aSLionel Sambuc bool isFriend;
1601f4a2713aSLionel Sambuc if (FunctionTemplate)
1602f4a2713aSLionel Sambuc isFriend = (FunctionTemplate->getFriendObjectKind() != Decl::FOK_None);
1603f4a2713aSLionel Sambuc else
1604f4a2713aSLionel Sambuc isFriend = (D->getFriendObjectKind() != Decl::FOK_None);
1605f4a2713aSLionel Sambuc
1606*0a6a1f1dSLionel Sambuc bool MergeWithParentScope = (TemplateParams != nullptr) ||
1607f4a2713aSLionel Sambuc !(isa<Decl>(Owner) &&
1608f4a2713aSLionel Sambuc cast<Decl>(Owner)->isDefinedOutsideFunctionOrMethod());
1609f4a2713aSLionel Sambuc LocalInstantiationScope Scope(SemaRef, MergeWithParentScope);
1610f4a2713aSLionel Sambuc
1611f4a2713aSLionel Sambuc // Instantiate enclosing template arguments for friends.
1612f4a2713aSLionel Sambuc SmallVector<TemplateParameterList *, 4> TempParamLists;
1613f4a2713aSLionel Sambuc unsigned NumTempParamLists = 0;
1614f4a2713aSLionel Sambuc if (isFriend && (NumTempParamLists = D->getNumTemplateParameterLists())) {
1615f4a2713aSLionel Sambuc TempParamLists.set_size(NumTempParamLists);
1616f4a2713aSLionel Sambuc for (unsigned I = 0; I != NumTempParamLists; ++I) {
1617f4a2713aSLionel Sambuc TemplateParameterList *TempParams = D->getTemplateParameterList(I);
1618f4a2713aSLionel Sambuc TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
1619f4a2713aSLionel Sambuc if (!InstParams)
1620*0a6a1f1dSLionel Sambuc return nullptr;
1621f4a2713aSLionel Sambuc TempParamLists[I] = InstParams;
1622f4a2713aSLionel Sambuc }
1623f4a2713aSLionel Sambuc }
1624f4a2713aSLionel Sambuc
1625f4a2713aSLionel Sambuc SmallVector<ParmVarDecl *, 4> Params;
1626f4a2713aSLionel Sambuc TypeSourceInfo *TInfo = SubstFunctionType(D, Params);
1627f4a2713aSLionel Sambuc if (!TInfo)
1628*0a6a1f1dSLionel Sambuc return nullptr;
1629f4a2713aSLionel Sambuc QualType T = adjustFunctionTypeForInstantiation(SemaRef.Context, D, TInfo);
1630f4a2713aSLionel Sambuc
1631f4a2713aSLionel Sambuc NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc();
1632f4a2713aSLionel Sambuc if (QualifierLoc) {
1633f4a2713aSLionel Sambuc QualifierLoc = SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc,
1634f4a2713aSLionel Sambuc TemplateArgs);
1635f4a2713aSLionel Sambuc if (!QualifierLoc)
1636*0a6a1f1dSLionel Sambuc return nullptr;
1637f4a2713aSLionel Sambuc }
1638f4a2713aSLionel Sambuc
1639f4a2713aSLionel Sambuc DeclContext *DC = Owner;
1640f4a2713aSLionel Sambuc if (isFriend) {
1641f4a2713aSLionel Sambuc if (QualifierLoc) {
1642f4a2713aSLionel Sambuc CXXScopeSpec SS;
1643f4a2713aSLionel Sambuc SS.Adopt(QualifierLoc);
1644f4a2713aSLionel Sambuc DC = SemaRef.computeDeclContext(SS);
1645f4a2713aSLionel Sambuc
1646f4a2713aSLionel Sambuc if (DC && SemaRef.RequireCompleteDeclContext(SS, DC))
1647*0a6a1f1dSLionel Sambuc return nullptr;
1648f4a2713aSLionel Sambuc } else {
1649f4a2713aSLionel Sambuc DC = SemaRef.FindInstantiatedContext(D->getLocation(),
1650f4a2713aSLionel Sambuc D->getDeclContext(),
1651f4a2713aSLionel Sambuc TemplateArgs);
1652f4a2713aSLionel Sambuc }
1653*0a6a1f1dSLionel Sambuc if (!DC) return nullptr;
1654f4a2713aSLionel Sambuc }
1655f4a2713aSLionel Sambuc
1656f4a2713aSLionel Sambuc // Build the instantiated method declaration.
1657f4a2713aSLionel Sambuc CXXRecordDecl *Record = cast<CXXRecordDecl>(DC);
1658*0a6a1f1dSLionel Sambuc CXXMethodDecl *Method = nullptr;
1659f4a2713aSLionel Sambuc
1660f4a2713aSLionel Sambuc SourceLocation StartLoc = D->getInnerLocStart();
1661f4a2713aSLionel Sambuc DeclarationNameInfo NameInfo
1662f4a2713aSLionel Sambuc = SemaRef.SubstDeclarationNameInfo(D->getNameInfo(), TemplateArgs);
1663f4a2713aSLionel Sambuc if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
1664f4a2713aSLionel Sambuc Method = CXXConstructorDecl::Create(SemaRef.Context, Record,
1665f4a2713aSLionel Sambuc StartLoc, NameInfo, T, TInfo,
1666f4a2713aSLionel Sambuc Constructor->isExplicit(),
1667f4a2713aSLionel Sambuc Constructor->isInlineSpecified(),
1668f4a2713aSLionel Sambuc false, Constructor->isConstexpr());
1669f4a2713aSLionel Sambuc
1670f4a2713aSLionel Sambuc // Claim that the instantiation of a constructor or constructor template
1671f4a2713aSLionel Sambuc // inherits the same constructor that the template does.
1672f4a2713aSLionel Sambuc if (CXXConstructorDecl *Inh = const_cast<CXXConstructorDecl *>(
1673f4a2713aSLionel Sambuc Constructor->getInheritedConstructor())) {
1674f4a2713aSLionel Sambuc // If we're instantiating a specialization of a function template, our
1675f4a2713aSLionel Sambuc // "inherited constructor" will actually itself be a function template.
1676f4a2713aSLionel Sambuc // Instantiate a declaration of it, too.
1677f4a2713aSLionel Sambuc if (FunctionTemplate) {
1678f4a2713aSLionel Sambuc assert(!TemplateParams && Inh->getDescribedFunctionTemplate() &&
1679f4a2713aSLionel Sambuc !Inh->getParent()->isDependentContext() &&
1680f4a2713aSLionel Sambuc "inheriting constructor template in dependent context?");
1681f4a2713aSLionel Sambuc Sema::InstantiatingTemplate Inst(SemaRef, Constructor->getLocation(),
1682f4a2713aSLionel Sambuc Inh);
1683f4a2713aSLionel Sambuc if (Inst.isInvalid())
1684*0a6a1f1dSLionel Sambuc return nullptr;
1685f4a2713aSLionel Sambuc Sema::ContextRAII SavedContext(SemaRef, Inh->getDeclContext());
1686f4a2713aSLionel Sambuc LocalInstantiationScope LocalScope(SemaRef);
1687f4a2713aSLionel Sambuc
1688f4a2713aSLionel Sambuc // Use the same template arguments that we deduced for the inheriting
1689f4a2713aSLionel Sambuc // constructor. There's no way they could be deduced differently.
1690f4a2713aSLionel Sambuc MultiLevelTemplateArgumentList InheritedArgs;
1691f4a2713aSLionel Sambuc InheritedArgs.addOuterTemplateArguments(TemplateArgs.getInnermost());
1692f4a2713aSLionel Sambuc Inh = cast_or_null<CXXConstructorDecl>(
1693f4a2713aSLionel Sambuc SemaRef.SubstDecl(Inh, Inh->getDeclContext(), InheritedArgs));
1694f4a2713aSLionel Sambuc if (!Inh)
1695*0a6a1f1dSLionel Sambuc return nullptr;
1696f4a2713aSLionel Sambuc }
1697f4a2713aSLionel Sambuc cast<CXXConstructorDecl>(Method)->setInheritedConstructor(Inh);
1698f4a2713aSLionel Sambuc }
1699f4a2713aSLionel Sambuc } else if (CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(D)) {
1700f4a2713aSLionel Sambuc Method = CXXDestructorDecl::Create(SemaRef.Context, Record,
1701f4a2713aSLionel Sambuc StartLoc, NameInfo, T, TInfo,
1702f4a2713aSLionel Sambuc Destructor->isInlineSpecified(),
1703f4a2713aSLionel Sambuc false);
1704f4a2713aSLionel Sambuc } else if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) {
1705f4a2713aSLionel Sambuc Method = CXXConversionDecl::Create(SemaRef.Context, Record,
1706f4a2713aSLionel Sambuc StartLoc, NameInfo, T, TInfo,
1707f4a2713aSLionel Sambuc Conversion->isInlineSpecified(),
1708f4a2713aSLionel Sambuc Conversion->isExplicit(),
1709f4a2713aSLionel Sambuc Conversion->isConstexpr(),
1710f4a2713aSLionel Sambuc Conversion->getLocEnd());
1711f4a2713aSLionel Sambuc } else {
1712f4a2713aSLionel Sambuc StorageClass SC = D->isStatic() ? SC_Static : SC_None;
1713f4a2713aSLionel Sambuc Method = CXXMethodDecl::Create(SemaRef.Context, Record,
1714f4a2713aSLionel Sambuc StartLoc, NameInfo, T, TInfo,
1715f4a2713aSLionel Sambuc SC, D->isInlineSpecified(),
1716f4a2713aSLionel Sambuc D->isConstexpr(), D->getLocEnd());
1717f4a2713aSLionel Sambuc }
1718f4a2713aSLionel Sambuc
1719f4a2713aSLionel Sambuc if (D->isInlined())
1720f4a2713aSLionel Sambuc Method->setImplicitlyInline();
1721f4a2713aSLionel Sambuc
1722f4a2713aSLionel Sambuc if (QualifierLoc)
1723f4a2713aSLionel Sambuc Method->setQualifierInfo(QualifierLoc);
1724f4a2713aSLionel Sambuc
1725f4a2713aSLionel Sambuc if (TemplateParams) {
1726f4a2713aSLionel Sambuc // Our resulting instantiation is actually a function template, since we
1727f4a2713aSLionel Sambuc // are substituting only the outer template parameters. For example, given
1728f4a2713aSLionel Sambuc //
1729f4a2713aSLionel Sambuc // template<typename T>
1730f4a2713aSLionel Sambuc // struct X {
1731f4a2713aSLionel Sambuc // template<typename U> void f(T, U);
1732f4a2713aSLionel Sambuc // };
1733f4a2713aSLionel Sambuc //
1734f4a2713aSLionel Sambuc // X<int> x;
1735f4a2713aSLionel Sambuc //
1736f4a2713aSLionel Sambuc // We are instantiating the member template "f" within X<int>, which means
1737f4a2713aSLionel Sambuc // substituting int for T, but leaving "f" as a member function template.
1738f4a2713aSLionel Sambuc // Build the function template itself.
1739f4a2713aSLionel Sambuc FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, Record,
1740f4a2713aSLionel Sambuc Method->getLocation(),
1741f4a2713aSLionel Sambuc Method->getDeclName(),
1742f4a2713aSLionel Sambuc TemplateParams, Method);
1743f4a2713aSLionel Sambuc if (isFriend) {
1744f4a2713aSLionel Sambuc FunctionTemplate->setLexicalDeclContext(Owner);
1745f4a2713aSLionel Sambuc FunctionTemplate->setObjectOfFriendDecl();
1746f4a2713aSLionel Sambuc } else if (D->isOutOfLine())
1747f4a2713aSLionel Sambuc FunctionTemplate->setLexicalDeclContext(D->getLexicalDeclContext());
1748f4a2713aSLionel Sambuc Method->setDescribedFunctionTemplate(FunctionTemplate);
1749f4a2713aSLionel Sambuc } else if (FunctionTemplate) {
1750f4a2713aSLionel Sambuc // Record this function template specialization.
1751f4a2713aSLionel Sambuc ArrayRef<TemplateArgument> Innermost = TemplateArgs.getInnermost();
1752f4a2713aSLionel Sambuc Method->setFunctionTemplateSpecialization(FunctionTemplate,
1753f4a2713aSLionel Sambuc TemplateArgumentList::CreateCopy(SemaRef.Context,
1754f4a2713aSLionel Sambuc Innermost.begin(),
1755f4a2713aSLionel Sambuc Innermost.size()),
1756*0a6a1f1dSLionel Sambuc /*InsertPos=*/nullptr);
1757f4a2713aSLionel Sambuc } else if (!isFriend) {
1758f4a2713aSLionel Sambuc // Record that this is an instantiation of a member function.
1759f4a2713aSLionel Sambuc Method->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation);
1760f4a2713aSLionel Sambuc }
1761f4a2713aSLionel Sambuc
1762f4a2713aSLionel Sambuc // If we are instantiating a member function defined
1763f4a2713aSLionel Sambuc // out-of-line, the instantiation will have the same lexical
1764f4a2713aSLionel Sambuc // context (which will be a namespace scope) as the template.
1765f4a2713aSLionel Sambuc if (isFriend) {
1766f4a2713aSLionel Sambuc if (NumTempParamLists)
1767f4a2713aSLionel Sambuc Method->setTemplateParameterListsInfo(SemaRef.Context,
1768f4a2713aSLionel Sambuc NumTempParamLists,
1769f4a2713aSLionel Sambuc TempParamLists.data());
1770f4a2713aSLionel Sambuc
1771f4a2713aSLionel Sambuc Method->setLexicalDeclContext(Owner);
1772f4a2713aSLionel Sambuc Method->setObjectOfFriendDecl();
1773f4a2713aSLionel Sambuc } else if (D->isOutOfLine())
1774f4a2713aSLionel Sambuc Method->setLexicalDeclContext(D->getLexicalDeclContext());
1775f4a2713aSLionel Sambuc
1776f4a2713aSLionel Sambuc // Attach the parameters
1777f4a2713aSLionel Sambuc for (unsigned P = 0; P < Params.size(); ++P)
1778f4a2713aSLionel Sambuc Params[P]->setOwningFunction(Method);
1779f4a2713aSLionel Sambuc Method->setParams(Params);
1780f4a2713aSLionel Sambuc
1781f4a2713aSLionel Sambuc if (InitMethodInstantiation(Method, D))
1782f4a2713aSLionel Sambuc Method->setInvalidDecl();
1783f4a2713aSLionel Sambuc
1784f4a2713aSLionel Sambuc LookupResult Previous(SemaRef, NameInfo, Sema::LookupOrdinaryName,
1785f4a2713aSLionel Sambuc Sema::ForRedeclaration);
1786f4a2713aSLionel Sambuc
1787f4a2713aSLionel Sambuc if (!FunctionTemplate || TemplateParams || isFriend) {
1788f4a2713aSLionel Sambuc SemaRef.LookupQualifiedName(Previous, Record);
1789f4a2713aSLionel Sambuc
1790f4a2713aSLionel Sambuc // In C++, the previous declaration we find might be a tag type
1791f4a2713aSLionel Sambuc // (class or enum). In this case, the new declaration will hide the
1792f4a2713aSLionel Sambuc // tag type. Note that this does does not apply if we're declaring a
1793f4a2713aSLionel Sambuc // typedef (C++ [dcl.typedef]p4).
1794f4a2713aSLionel Sambuc if (Previous.isSingleTagDecl())
1795f4a2713aSLionel Sambuc Previous.clear();
1796f4a2713aSLionel Sambuc }
1797f4a2713aSLionel Sambuc
1798f4a2713aSLionel Sambuc if (!IsClassScopeSpecialization)
1799*0a6a1f1dSLionel Sambuc SemaRef.CheckFunctionDeclaration(nullptr, Method, Previous, false);
1800f4a2713aSLionel Sambuc
1801f4a2713aSLionel Sambuc if (D->isPure())
1802f4a2713aSLionel Sambuc SemaRef.CheckPureMethod(Method, SourceRange());
1803f4a2713aSLionel Sambuc
1804f4a2713aSLionel Sambuc // Propagate access. For a non-friend declaration, the access is
1805f4a2713aSLionel Sambuc // whatever we're propagating from. For a friend, it should be the
1806f4a2713aSLionel Sambuc // previous declaration we just found.
1807f4a2713aSLionel Sambuc if (isFriend && Method->getPreviousDecl())
1808f4a2713aSLionel Sambuc Method->setAccess(Method->getPreviousDecl()->getAccess());
1809f4a2713aSLionel Sambuc else
1810f4a2713aSLionel Sambuc Method->setAccess(D->getAccess());
1811f4a2713aSLionel Sambuc if (FunctionTemplate)
1812f4a2713aSLionel Sambuc FunctionTemplate->setAccess(Method->getAccess());
1813f4a2713aSLionel Sambuc
1814f4a2713aSLionel Sambuc SemaRef.CheckOverrideControl(Method);
1815f4a2713aSLionel Sambuc
1816f4a2713aSLionel Sambuc // If a function is defined as defaulted or deleted, mark it as such now.
1817f4a2713aSLionel Sambuc if (D->isExplicitlyDefaulted())
1818f4a2713aSLionel Sambuc SemaRef.SetDeclDefaulted(Method, Method->getLocation());
1819f4a2713aSLionel Sambuc if (D->isDeletedAsWritten())
1820f4a2713aSLionel Sambuc SemaRef.SetDeclDeleted(Method, Method->getLocation());
1821f4a2713aSLionel Sambuc
1822f4a2713aSLionel Sambuc // If there's a function template, let our caller handle it.
1823f4a2713aSLionel Sambuc if (FunctionTemplate) {
1824f4a2713aSLionel Sambuc // do nothing
1825f4a2713aSLionel Sambuc
1826f4a2713aSLionel Sambuc // Don't hide a (potentially) valid declaration with an invalid one.
1827f4a2713aSLionel Sambuc } else if (Method->isInvalidDecl() && !Previous.empty()) {
1828f4a2713aSLionel Sambuc // do nothing
1829f4a2713aSLionel Sambuc
1830f4a2713aSLionel Sambuc // Otherwise, check access to friends and make them visible.
1831f4a2713aSLionel Sambuc } else if (isFriend) {
1832f4a2713aSLionel Sambuc // We only need to re-check access for methods which we didn't
1833f4a2713aSLionel Sambuc // manage to match during parsing.
1834f4a2713aSLionel Sambuc if (!D->getPreviousDecl())
1835f4a2713aSLionel Sambuc SemaRef.CheckFriendAccess(Method);
1836f4a2713aSLionel Sambuc
1837f4a2713aSLionel Sambuc Record->makeDeclVisibleInContext(Method);
1838f4a2713aSLionel Sambuc
1839f4a2713aSLionel Sambuc // Otherwise, add the declaration. We don't need to do this for
1840f4a2713aSLionel Sambuc // class-scope specializations because we'll have matched them with
1841f4a2713aSLionel Sambuc // the appropriate template.
1842f4a2713aSLionel Sambuc } else if (!IsClassScopeSpecialization) {
1843f4a2713aSLionel Sambuc Owner->addDecl(Method);
1844f4a2713aSLionel Sambuc }
1845f4a2713aSLionel Sambuc
1846f4a2713aSLionel Sambuc return Method;
1847f4a2713aSLionel Sambuc }
1848f4a2713aSLionel Sambuc
VisitCXXConstructorDecl(CXXConstructorDecl * D)1849f4a2713aSLionel Sambuc Decl *TemplateDeclInstantiator::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
1850f4a2713aSLionel Sambuc return VisitCXXMethodDecl(D);
1851f4a2713aSLionel Sambuc }
1852f4a2713aSLionel Sambuc
VisitCXXDestructorDecl(CXXDestructorDecl * D)1853f4a2713aSLionel Sambuc Decl *TemplateDeclInstantiator::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
1854f4a2713aSLionel Sambuc return VisitCXXMethodDecl(D);
1855f4a2713aSLionel Sambuc }
1856f4a2713aSLionel Sambuc
VisitCXXConversionDecl(CXXConversionDecl * D)1857f4a2713aSLionel Sambuc Decl *TemplateDeclInstantiator::VisitCXXConversionDecl(CXXConversionDecl *D) {
1858f4a2713aSLionel Sambuc return VisitCXXMethodDecl(D);
1859f4a2713aSLionel Sambuc }
1860f4a2713aSLionel Sambuc
VisitParmVarDecl(ParmVarDecl * D)1861f4a2713aSLionel Sambuc Decl *TemplateDeclInstantiator::VisitParmVarDecl(ParmVarDecl *D) {
1862f4a2713aSLionel Sambuc return SemaRef.SubstParmVarDecl(D, TemplateArgs, /*indexAdjustment*/ 0, None,
1863f4a2713aSLionel Sambuc /*ExpectParameterPack=*/ false);
1864f4a2713aSLionel Sambuc }
1865f4a2713aSLionel Sambuc
VisitTemplateTypeParmDecl(TemplateTypeParmDecl * D)1866f4a2713aSLionel Sambuc Decl *TemplateDeclInstantiator::VisitTemplateTypeParmDecl(
1867f4a2713aSLionel Sambuc TemplateTypeParmDecl *D) {
1868f4a2713aSLionel Sambuc // TODO: don't always clone when decls are refcounted.
1869f4a2713aSLionel Sambuc assert(D->getTypeForDecl()->isTemplateTypeParmType());
1870f4a2713aSLionel Sambuc
1871f4a2713aSLionel Sambuc TemplateTypeParmDecl *Inst =
1872f4a2713aSLionel Sambuc TemplateTypeParmDecl::Create(SemaRef.Context, Owner,
1873f4a2713aSLionel Sambuc D->getLocStart(), D->getLocation(),
1874f4a2713aSLionel Sambuc D->getDepth() - TemplateArgs.getNumLevels(),
1875f4a2713aSLionel Sambuc D->getIndex(), D->getIdentifier(),
1876f4a2713aSLionel Sambuc D->wasDeclaredWithTypename(),
1877f4a2713aSLionel Sambuc D->isParameterPack());
1878f4a2713aSLionel Sambuc Inst->setAccess(AS_public);
1879f4a2713aSLionel Sambuc
1880f4a2713aSLionel Sambuc if (D->hasDefaultArgument()) {
1881f4a2713aSLionel Sambuc TypeSourceInfo *InstantiatedDefaultArg =
1882f4a2713aSLionel Sambuc SemaRef.SubstType(D->getDefaultArgumentInfo(), TemplateArgs,
1883f4a2713aSLionel Sambuc D->getDefaultArgumentLoc(), D->getDeclName());
1884f4a2713aSLionel Sambuc if (InstantiatedDefaultArg)
1885f4a2713aSLionel Sambuc Inst->setDefaultArgument(InstantiatedDefaultArg, false);
1886f4a2713aSLionel Sambuc }
1887f4a2713aSLionel Sambuc
1888f4a2713aSLionel Sambuc // Introduce this template parameter's instantiation into the instantiation
1889f4a2713aSLionel Sambuc // scope.
1890f4a2713aSLionel Sambuc SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Inst);
1891f4a2713aSLionel Sambuc
1892f4a2713aSLionel Sambuc return Inst;
1893f4a2713aSLionel Sambuc }
1894f4a2713aSLionel Sambuc
VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl * D)1895f4a2713aSLionel Sambuc Decl *TemplateDeclInstantiator::VisitNonTypeTemplateParmDecl(
1896f4a2713aSLionel Sambuc NonTypeTemplateParmDecl *D) {
1897f4a2713aSLionel Sambuc // Substitute into the type of the non-type template parameter.
1898f4a2713aSLionel Sambuc TypeLoc TL = D->getTypeSourceInfo()->getTypeLoc();
1899f4a2713aSLionel Sambuc SmallVector<TypeSourceInfo *, 4> ExpandedParameterPackTypesAsWritten;
1900f4a2713aSLionel Sambuc SmallVector<QualType, 4> ExpandedParameterPackTypes;
1901f4a2713aSLionel Sambuc bool IsExpandedParameterPack = false;
1902f4a2713aSLionel Sambuc TypeSourceInfo *DI;
1903f4a2713aSLionel Sambuc QualType T;
1904f4a2713aSLionel Sambuc bool Invalid = false;
1905f4a2713aSLionel Sambuc
1906f4a2713aSLionel Sambuc if (D->isExpandedParameterPack()) {
1907f4a2713aSLionel Sambuc // The non-type template parameter pack is an already-expanded pack
1908f4a2713aSLionel Sambuc // expansion of types. Substitute into each of the expanded types.
1909f4a2713aSLionel Sambuc ExpandedParameterPackTypes.reserve(D->getNumExpansionTypes());
1910f4a2713aSLionel Sambuc ExpandedParameterPackTypesAsWritten.reserve(D->getNumExpansionTypes());
1911f4a2713aSLionel Sambuc for (unsigned I = 0, N = D->getNumExpansionTypes(); I != N; ++I) {
1912f4a2713aSLionel Sambuc TypeSourceInfo *NewDI =SemaRef.SubstType(D->getExpansionTypeSourceInfo(I),
1913f4a2713aSLionel Sambuc TemplateArgs,
1914f4a2713aSLionel Sambuc D->getLocation(),
1915f4a2713aSLionel Sambuc D->getDeclName());
1916f4a2713aSLionel Sambuc if (!NewDI)
1917*0a6a1f1dSLionel Sambuc return nullptr;
1918f4a2713aSLionel Sambuc
1919f4a2713aSLionel Sambuc ExpandedParameterPackTypesAsWritten.push_back(NewDI);
1920f4a2713aSLionel Sambuc QualType NewT =SemaRef.CheckNonTypeTemplateParameterType(NewDI->getType(),
1921f4a2713aSLionel Sambuc D->getLocation());
1922f4a2713aSLionel Sambuc if (NewT.isNull())
1923*0a6a1f1dSLionel Sambuc return nullptr;
1924f4a2713aSLionel Sambuc ExpandedParameterPackTypes.push_back(NewT);
1925f4a2713aSLionel Sambuc }
1926f4a2713aSLionel Sambuc
1927f4a2713aSLionel Sambuc IsExpandedParameterPack = true;
1928f4a2713aSLionel Sambuc DI = D->getTypeSourceInfo();
1929f4a2713aSLionel Sambuc T = DI->getType();
1930f4a2713aSLionel Sambuc } else if (D->isPackExpansion()) {
1931f4a2713aSLionel Sambuc // The non-type template parameter pack's type is a pack expansion of types.
1932f4a2713aSLionel Sambuc // Determine whether we need to expand this parameter pack into separate
1933f4a2713aSLionel Sambuc // types.
1934f4a2713aSLionel Sambuc PackExpansionTypeLoc Expansion = TL.castAs<PackExpansionTypeLoc>();
1935f4a2713aSLionel Sambuc TypeLoc Pattern = Expansion.getPatternLoc();
1936f4a2713aSLionel Sambuc SmallVector<UnexpandedParameterPack, 2> Unexpanded;
1937f4a2713aSLionel Sambuc SemaRef.collectUnexpandedParameterPacks(Pattern, Unexpanded);
1938f4a2713aSLionel Sambuc
1939f4a2713aSLionel Sambuc // Determine whether the set of unexpanded parameter packs can and should
1940f4a2713aSLionel Sambuc // be expanded.
1941f4a2713aSLionel Sambuc bool Expand = true;
1942f4a2713aSLionel Sambuc bool RetainExpansion = false;
1943f4a2713aSLionel Sambuc Optional<unsigned> OrigNumExpansions
1944f4a2713aSLionel Sambuc = Expansion.getTypePtr()->getNumExpansions();
1945f4a2713aSLionel Sambuc Optional<unsigned> NumExpansions = OrigNumExpansions;
1946f4a2713aSLionel Sambuc if (SemaRef.CheckParameterPacksForExpansion(Expansion.getEllipsisLoc(),
1947f4a2713aSLionel Sambuc Pattern.getSourceRange(),
1948f4a2713aSLionel Sambuc Unexpanded,
1949f4a2713aSLionel Sambuc TemplateArgs,
1950f4a2713aSLionel Sambuc Expand, RetainExpansion,
1951f4a2713aSLionel Sambuc NumExpansions))
1952*0a6a1f1dSLionel Sambuc return nullptr;
1953f4a2713aSLionel Sambuc
1954f4a2713aSLionel Sambuc if (Expand) {
1955f4a2713aSLionel Sambuc for (unsigned I = 0; I != *NumExpansions; ++I) {
1956f4a2713aSLionel Sambuc Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, I);
1957f4a2713aSLionel Sambuc TypeSourceInfo *NewDI = SemaRef.SubstType(Pattern, TemplateArgs,
1958f4a2713aSLionel Sambuc D->getLocation(),
1959f4a2713aSLionel Sambuc D->getDeclName());
1960f4a2713aSLionel Sambuc if (!NewDI)
1961*0a6a1f1dSLionel Sambuc return nullptr;
1962f4a2713aSLionel Sambuc
1963f4a2713aSLionel Sambuc ExpandedParameterPackTypesAsWritten.push_back(NewDI);
1964f4a2713aSLionel Sambuc QualType NewT = SemaRef.CheckNonTypeTemplateParameterType(
1965f4a2713aSLionel Sambuc NewDI->getType(),
1966f4a2713aSLionel Sambuc D->getLocation());
1967f4a2713aSLionel Sambuc if (NewT.isNull())
1968*0a6a1f1dSLionel Sambuc return nullptr;
1969f4a2713aSLionel Sambuc ExpandedParameterPackTypes.push_back(NewT);
1970f4a2713aSLionel Sambuc }
1971f4a2713aSLionel Sambuc
1972f4a2713aSLionel Sambuc // Note that we have an expanded parameter pack. The "type" of this
1973f4a2713aSLionel Sambuc // expanded parameter pack is the original expansion type, but callers
1974f4a2713aSLionel Sambuc // will end up using the expanded parameter pack types for type-checking.
1975f4a2713aSLionel Sambuc IsExpandedParameterPack = true;
1976f4a2713aSLionel Sambuc DI = D->getTypeSourceInfo();
1977f4a2713aSLionel Sambuc T = DI->getType();
1978f4a2713aSLionel Sambuc } else {
1979f4a2713aSLionel Sambuc // We cannot fully expand the pack expansion now, so substitute into the
1980f4a2713aSLionel Sambuc // pattern and create a new pack expansion type.
1981f4a2713aSLionel Sambuc Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, -1);
1982f4a2713aSLionel Sambuc TypeSourceInfo *NewPattern = SemaRef.SubstType(Pattern, TemplateArgs,
1983f4a2713aSLionel Sambuc D->getLocation(),
1984f4a2713aSLionel Sambuc D->getDeclName());
1985f4a2713aSLionel Sambuc if (!NewPattern)
1986*0a6a1f1dSLionel Sambuc return nullptr;
1987f4a2713aSLionel Sambuc
1988f4a2713aSLionel Sambuc DI = SemaRef.CheckPackExpansion(NewPattern, Expansion.getEllipsisLoc(),
1989f4a2713aSLionel Sambuc NumExpansions);
1990f4a2713aSLionel Sambuc if (!DI)
1991*0a6a1f1dSLionel Sambuc return nullptr;
1992f4a2713aSLionel Sambuc
1993f4a2713aSLionel Sambuc T = DI->getType();
1994f4a2713aSLionel Sambuc }
1995f4a2713aSLionel Sambuc } else {
1996f4a2713aSLionel Sambuc // Simple case: substitution into a parameter that is not a parameter pack.
1997f4a2713aSLionel Sambuc DI = SemaRef.SubstType(D->getTypeSourceInfo(), TemplateArgs,
1998f4a2713aSLionel Sambuc D->getLocation(), D->getDeclName());
1999f4a2713aSLionel Sambuc if (!DI)
2000*0a6a1f1dSLionel Sambuc return nullptr;
2001f4a2713aSLionel Sambuc
2002f4a2713aSLionel Sambuc // Check that this type is acceptable for a non-type template parameter.
2003f4a2713aSLionel Sambuc T = SemaRef.CheckNonTypeTemplateParameterType(DI->getType(),
2004f4a2713aSLionel Sambuc D->getLocation());
2005f4a2713aSLionel Sambuc if (T.isNull()) {
2006f4a2713aSLionel Sambuc T = SemaRef.Context.IntTy;
2007f4a2713aSLionel Sambuc Invalid = true;
2008f4a2713aSLionel Sambuc }
2009f4a2713aSLionel Sambuc }
2010f4a2713aSLionel Sambuc
2011f4a2713aSLionel Sambuc NonTypeTemplateParmDecl *Param;
2012f4a2713aSLionel Sambuc if (IsExpandedParameterPack)
2013f4a2713aSLionel Sambuc Param = NonTypeTemplateParmDecl::Create(SemaRef.Context, Owner,
2014f4a2713aSLionel Sambuc D->getInnerLocStart(),
2015f4a2713aSLionel Sambuc D->getLocation(),
2016f4a2713aSLionel Sambuc D->getDepth() - TemplateArgs.getNumLevels(),
2017f4a2713aSLionel Sambuc D->getPosition(),
2018f4a2713aSLionel Sambuc D->getIdentifier(), T,
2019f4a2713aSLionel Sambuc DI,
2020f4a2713aSLionel Sambuc ExpandedParameterPackTypes.data(),
2021f4a2713aSLionel Sambuc ExpandedParameterPackTypes.size(),
2022f4a2713aSLionel Sambuc ExpandedParameterPackTypesAsWritten.data());
2023f4a2713aSLionel Sambuc else
2024f4a2713aSLionel Sambuc Param = NonTypeTemplateParmDecl::Create(SemaRef.Context, Owner,
2025f4a2713aSLionel Sambuc D->getInnerLocStart(),
2026f4a2713aSLionel Sambuc D->getLocation(),
2027f4a2713aSLionel Sambuc D->getDepth() - TemplateArgs.getNumLevels(),
2028f4a2713aSLionel Sambuc D->getPosition(),
2029f4a2713aSLionel Sambuc D->getIdentifier(), T,
2030f4a2713aSLionel Sambuc D->isParameterPack(), DI);
2031f4a2713aSLionel Sambuc
2032f4a2713aSLionel Sambuc Param->setAccess(AS_public);
2033f4a2713aSLionel Sambuc if (Invalid)
2034f4a2713aSLionel Sambuc Param->setInvalidDecl();
2035f4a2713aSLionel Sambuc
2036f4a2713aSLionel Sambuc if (D->hasDefaultArgument()) {
2037f4a2713aSLionel Sambuc ExprResult Value = SemaRef.SubstExpr(D->getDefaultArgument(), TemplateArgs);
2038f4a2713aSLionel Sambuc if (!Value.isInvalid())
2039f4a2713aSLionel Sambuc Param->setDefaultArgument(Value.get(), false);
2040f4a2713aSLionel Sambuc }
2041f4a2713aSLionel Sambuc
2042f4a2713aSLionel Sambuc // Introduce this template parameter's instantiation into the instantiation
2043f4a2713aSLionel Sambuc // scope.
2044f4a2713aSLionel Sambuc SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
2045f4a2713aSLionel Sambuc return Param;
2046f4a2713aSLionel Sambuc }
2047f4a2713aSLionel Sambuc
collectUnexpandedParameterPacks(Sema & S,TemplateParameterList * Params,SmallVectorImpl<UnexpandedParameterPack> & Unexpanded)2048f4a2713aSLionel Sambuc static void collectUnexpandedParameterPacks(
2049f4a2713aSLionel Sambuc Sema &S,
2050f4a2713aSLionel Sambuc TemplateParameterList *Params,
2051f4a2713aSLionel Sambuc SmallVectorImpl<UnexpandedParameterPack> &Unexpanded) {
2052f4a2713aSLionel Sambuc for (TemplateParameterList::const_iterator I = Params->begin(),
2053f4a2713aSLionel Sambuc E = Params->end(); I != E; ++I) {
2054f4a2713aSLionel Sambuc if ((*I)->isTemplateParameterPack())
2055f4a2713aSLionel Sambuc continue;
2056f4a2713aSLionel Sambuc if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(*I))
2057f4a2713aSLionel Sambuc S.collectUnexpandedParameterPacks(NTTP->getTypeSourceInfo()->getTypeLoc(),
2058f4a2713aSLionel Sambuc Unexpanded);
2059f4a2713aSLionel Sambuc if (TemplateTemplateParmDecl *TTP = dyn_cast<TemplateTemplateParmDecl>(*I))
2060f4a2713aSLionel Sambuc collectUnexpandedParameterPacks(S, TTP->getTemplateParameters(),
2061f4a2713aSLionel Sambuc Unexpanded);
2062f4a2713aSLionel Sambuc }
2063f4a2713aSLionel Sambuc }
2064f4a2713aSLionel Sambuc
2065f4a2713aSLionel Sambuc Decl *
VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl * D)2066f4a2713aSLionel Sambuc TemplateDeclInstantiator::VisitTemplateTemplateParmDecl(
2067f4a2713aSLionel Sambuc TemplateTemplateParmDecl *D) {
2068f4a2713aSLionel Sambuc // Instantiate the template parameter list of the template template parameter.
2069f4a2713aSLionel Sambuc TemplateParameterList *TempParams = D->getTemplateParameters();
2070f4a2713aSLionel Sambuc TemplateParameterList *InstParams;
2071f4a2713aSLionel Sambuc SmallVector<TemplateParameterList*, 8> ExpandedParams;
2072f4a2713aSLionel Sambuc
2073f4a2713aSLionel Sambuc bool IsExpandedParameterPack = false;
2074f4a2713aSLionel Sambuc
2075f4a2713aSLionel Sambuc if (D->isExpandedParameterPack()) {
2076f4a2713aSLionel Sambuc // The template template parameter pack is an already-expanded pack
2077f4a2713aSLionel Sambuc // expansion of template parameters. Substitute into each of the expanded
2078f4a2713aSLionel Sambuc // parameters.
2079f4a2713aSLionel Sambuc ExpandedParams.reserve(D->getNumExpansionTemplateParameters());
2080f4a2713aSLionel Sambuc for (unsigned I = 0, N = D->getNumExpansionTemplateParameters();
2081f4a2713aSLionel Sambuc I != N; ++I) {
2082f4a2713aSLionel Sambuc LocalInstantiationScope Scope(SemaRef);
2083f4a2713aSLionel Sambuc TemplateParameterList *Expansion =
2084f4a2713aSLionel Sambuc SubstTemplateParams(D->getExpansionTemplateParameters(I));
2085f4a2713aSLionel Sambuc if (!Expansion)
2086*0a6a1f1dSLionel Sambuc return nullptr;
2087f4a2713aSLionel Sambuc ExpandedParams.push_back(Expansion);
2088f4a2713aSLionel Sambuc }
2089f4a2713aSLionel Sambuc
2090f4a2713aSLionel Sambuc IsExpandedParameterPack = true;
2091f4a2713aSLionel Sambuc InstParams = TempParams;
2092f4a2713aSLionel Sambuc } else if (D->isPackExpansion()) {
2093f4a2713aSLionel Sambuc // The template template parameter pack expands to a pack of template
2094f4a2713aSLionel Sambuc // template parameters. Determine whether we need to expand this parameter
2095f4a2713aSLionel Sambuc // pack into separate parameters.
2096f4a2713aSLionel Sambuc SmallVector<UnexpandedParameterPack, 2> Unexpanded;
2097f4a2713aSLionel Sambuc collectUnexpandedParameterPacks(SemaRef, D->getTemplateParameters(),
2098f4a2713aSLionel Sambuc Unexpanded);
2099f4a2713aSLionel Sambuc
2100f4a2713aSLionel Sambuc // Determine whether the set of unexpanded parameter packs can and should
2101f4a2713aSLionel Sambuc // be expanded.
2102f4a2713aSLionel Sambuc bool Expand = true;
2103f4a2713aSLionel Sambuc bool RetainExpansion = false;
2104f4a2713aSLionel Sambuc Optional<unsigned> NumExpansions;
2105f4a2713aSLionel Sambuc if (SemaRef.CheckParameterPacksForExpansion(D->getLocation(),
2106f4a2713aSLionel Sambuc TempParams->getSourceRange(),
2107f4a2713aSLionel Sambuc Unexpanded,
2108f4a2713aSLionel Sambuc TemplateArgs,
2109f4a2713aSLionel Sambuc Expand, RetainExpansion,
2110f4a2713aSLionel Sambuc NumExpansions))
2111*0a6a1f1dSLionel Sambuc return nullptr;
2112f4a2713aSLionel Sambuc
2113f4a2713aSLionel Sambuc if (Expand) {
2114f4a2713aSLionel Sambuc for (unsigned I = 0; I != *NumExpansions; ++I) {
2115f4a2713aSLionel Sambuc Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, I);
2116f4a2713aSLionel Sambuc LocalInstantiationScope Scope(SemaRef);
2117f4a2713aSLionel Sambuc TemplateParameterList *Expansion = SubstTemplateParams(TempParams);
2118f4a2713aSLionel Sambuc if (!Expansion)
2119*0a6a1f1dSLionel Sambuc return nullptr;
2120f4a2713aSLionel Sambuc ExpandedParams.push_back(Expansion);
2121f4a2713aSLionel Sambuc }
2122f4a2713aSLionel Sambuc
2123f4a2713aSLionel Sambuc // Note that we have an expanded parameter pack. The "type" of this
2124f4a2713aSLionel Sambuc // expanded parameter pack is the original expansion type, but callers
2125f4a2713aSLionel Sambuc // will end up using the expanded parameter pack types for type-checking.
2126f4a2713aSLionel Sambuc IsExpandedParameterPack = true;
2127f4a2713aSLionel Sambuc InstParams = TempParams;
2128f4a2713aSLionel Sambuc } else {
2129f4a2713aSLionel Sambuc // We cannot fully expand the pack expansion now, so just substitute
2130f4a2713aSLionel Sambuc // into the pattern.
2131f4a2713aSLionel Sambuc Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, -1);
2132f4a2713aSLionel Sambuc
2133f4a2713aSLionel Sambuc LocalInstantiationScope Scope(SemaRef);
2134f4a2713aSLionel Sambuc InstParams = SubstTemplateParams(TempParams);
2135f4a2713aSLionel Sambuc if (!InstParams)
2136*0a6a1f1dSLionel Sambuc return nullptr;
2137f4a2713aSLionel Sambuc }
2138f4a2713aSLionel Sambuc } else {
2139f4a2713aSLionel Sambuc // Perform the actual substitution of template parameters within a new,
2140f4a2713aSLionel Sambuc // local instantiation scope.
2141f4a2713aSLionel Sambuc LocalInstantiationScope Scope(SemaRef);
2142f4a2713aSLionel Sambuc InstParams = SubstTemplateParams(TempParams);
2143f4a2713aSLionel Sambuc if (!InstParams)
2144*0a6a1f1dSLionel Sambuc return nullptr;
2145f4a2713aSLionel Sambuc }
2146f4a2713aSLionel Sambuc
2147f4a2713aSLionel Sambuc // Build the template template parameter.
2148f4a2713aSLionel Sambuc TemplateTemplateParmDecl *Param;
2149f4a2713aSLionel Sambuc if (IsExpandedParameterPack)
2150f4a2713aSLionel Sambuc Param = TemplateTemplateParmDecl::Create(SemaRef.Context, Owner,
2151f4a2713aSLionel Sambuc D->getLocation(),
2152f4a2713aSLionel Sambuc D->getDepth() - TemplateArgs.getNumLevels(),
2153f4a2713aSLionel Sambuc D->getPosition(),
2154f4a2713aSLionel Sambuc D->getIdentifier(), InstParams,
2155f4a2713aSLionel Sambuc ExpandedParams);
2156f4a2713aSLionel Sambuc else
2157f4a2713aSLionel Sambuc Param = TemplateTemplateParmDecl::Create(SemaRef.Context, Owner,
2158f4a2713aSLionel Sambuc D->getLocation(),
2159f4a2713aSLionel Sambuc D->getDepth() - TemplateArgs.getNumLevels(),
2160f4a2713aSLionel Sambuc D->getPosition(),
2161f4a2713aSLionel Sambuc D->isParameterPack(),
2162f4a2713aSLionel Sambuc D->getIdentifier(), InstParams);
2163f4a2713aSLionel Sambuc if (D->hasDefaultArgument()) {
2164f4a2713aSLionel Sambuc NestedNameSpecifierLoc QualifierLoc =
2165f4a2713aSLionel Sambuc D->getDefaultArgument().getTemplateQualifierLoc();
2166f4a2713aSLionel Sambuc QualifierLoc =
2167f4a2713aSLionel Sambuc SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc, TemplateArgs);
2168f4a2713aSLionel Sambuc TemplateName TName = SemaRef.SubstTemplateName(
2169f4a2713aSLionel Sambuc QualifierLoc, D->getDefaultArgument().getArgument().getAsTemplate(),
2170f4a2713aSLionel Sambuc D->getDefaultArgument().getTemplateNameLoc(), TemplateArgs);
2171f4a2713aSLionel Sambuc if (!TName.isNull())
2172f4a2713aSLionel Sambuc Param->setDefaultArgument(
2173f4a2713aSLionel Sambuc TemplateArgumentLoc(TemplateArgument(TName),
2174f4a2713aSLionel Sambuc D->getDefaultArgument().getTemplateQualifierLoc(),
2175f4a2713aSLionel Sambuc D->getDefaultArgument().getTemplateNameLoc()),
2176f4a2713aSLionel Sambuc false);
2177f4a2713aSLionel Sambuc }
2178f4a2713aSLionel Sambuc Param->setAccess(AS_public);
2179f4a2713aSLionel Sambuc
2180f4a2713aSLionel Sambuc // Introduce this template parameter's instantiation into the instantiation
2181f4a2713aSLionel Sambuc // scope.
2182f4a2713aSLionel Sambuc SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
2183f4a2713aSLionel Sambuc
2184f4a2713aSLionel Sambuc return Param;
2185f4a2713aSLionel Sambuc }
2186f4a2713aSLionel Sambuc
VisitUsingDirectiveDecl(UsingDirectiveDecl * D)2187f4a2713aSLionel Sambuc Decl *TemplateDeclInstantiator::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
2188f4a2713aSLionel Sambuc // Using directives are never dependent (and never contain any types or
2189f4a2713aSLionel Sambuc // expressions), so they require no explicit instantiation work.
2190f4a2713aSLionel Sambuc
2191f4a2713aSLionel Sambuc UsingDirectiveDecl *Inst
2192f4a2713aSLionel Sambuc = UsingDirectiveDecl::Create(SemaRef.Context, Owner, D->getLocation(),
2193f4a2713aSLionel Sambuc D->getNamespaceKeyLocation(),
2194f4a2713aSLionel Sambuc D->getQualifierLoc(),
2195f4a2713aSLionel Sambuc D->getIdentLocation(),
2196f4a2713aSLionel Sambuc D->getNominatedNamespace(),
2197f4a2713aSLionel Sambuc D->getCommonAncestor());
2198f4a2713aSLionel Sambuc
2199f4a2713aSLionel Sambuc // Add the using directive to its declaration context
2200f4a2713aSLionel Sambuc // only if this is not a function or method.
2201f4a2713aSLionel Sambuc if (!Owner->isFunctionOrMethod())
2202f4a2713aSLionel Sambuc Owner->addDecl(Inst);
2203f4a2713aSLionel Sambuc
2204f4a2713aSLionel Sambuc return Inst;
2205f4a2713aSLionel Sambuc }
2206f4a2713aSLionel Sambuc
VisitUsingDecl(UsingDecl * D)2207f4a2713aSLionel Sambuc Decl *TemplateDeclInstantiator::VisitUsingDecl(UsingDecl *D) {
2208f4a2713aSLionel Sambuc
2209f4a2713aSLionel Sambuc // The nested name specifier may be dependent, for example
2210f4a2713aSLionel Sambuc // template <typename T> struct t {
2211f4a2713aSLionel Sambuc // struct s1 { T f1(); };
2212f4a2713aSLionel Sambuc // struct s2 : s1 { using s1::f1; };
2213f4a2713aSLionel Sambuc // };
2214f4a2713aSLionel Sambuc // template struct t<int>;
2215f4a2713aSLionel Sambuc // Here, in using s1::f1, s1 refers to t<T>::s1;
2216f4a2713aSLionel Sambuc // we need to substitute for t<int>::s1.
2217f4a2713aSLionel Sambuc NestedNameSpecifierLoc QualifierLoc
2218f4a2713aSLionel Sambuc = SemaRef.SubstNestedNameSpecifierLoc(D->getQualifierLoc(),
2219f4a2713aSLionel Sambuc TemplateArgs);
2220f4a2713aSLionel Sambuc if (!QualifierLoc)
2221*0a6a1f1dSLionel Sambuc return nullptr;
2222f4a2713aSLionel Sambuc
2223f4a2713aSLionel Sambuc // The name info is non-dependent, so no transformation
2224f4a2713aSLionel Sambuc // is required.
2225f4a2713aSLionel Sambuc DeclarationNameInfo NameInfo = D->getNameInfo();
2226f4a2713aSLionel Sambuc
2227f4a2713aSLionel Sambuc // We only need to do redeclaration lookups if we're in a class
2228f4a2713aSLionel Sambuc // scope (in fact, it's not really even possible in non-class
2229f4a2713aSLionel Sambuc // scopes).
2230f4a2713aSLionel Sambuc bool CheckRedeclaration = Owner->isRecord();
2231f4a2713aSLionel Sambuc
2232f4a2713aSLionel Sambuc LookupResult Prev(SemaRef, NameInfo, Sema::LookupUsingDeclName,
2233f4a2713aSLionel Sambuc Sema::ForRedeclaration);
2234f4a2713aSLionel Sambuc
2235f4a2713aSLionel Sambuc UsingDecl *NewUD = UsingDecl::Create(SemaRef.Context, Owner,
2236f4a2713aSLionel Sambuc D->getUsingLoc(),
2237f4a2713aSLionel Sambuc QualifierLoc,
2238f4a2713aSLionel Sambuc NameInfo,
2239f4a2713aSLionel Sambuc D->hasTypename());
2240f4a2713aSLionel Sambuc
2241f4a2713aSLionel Sambuc CXXScopeSpec SS;
2242f4a2713aSLionel Sambuc SS.Adopt(QualifierLoc);
2243f4a2713aSLionel Sambuc if (CheckRedeclaration) {
2244f4a2713aSLionel Sambuc Prev.setHideTags(false);
2245f4a2713aSLionel Sambuc SemaRef.LookupQualifiedName(Prev, Owner);
2246f4a2713aSLionel Sambuc
2247f4a2713aSLionel Sambuc // Check for invalid redeclarations.
2248f4a2713aSLionel Sambuc if (SemaRef.CheckUsingDeclRedeclaration(D->getUsingLoc(),
2249f4a2713aSLionel Sambuc D->hasTypename(), SS,
2250f4a2713aSLionel Sambuc D->getLocation(), Prev))
2251f4a2713aSLionel Sambuc NewUD->setInvalidDecl();
2252f4a2713aSLionel Sambuc
2253f4a2713aSLionel Sambuc }
2254f4a2713aSLionel Sambuc
2255f4a2713aSLionel Sambuc if (!NewUD->isInvalidDecl() &&
2256*0a6a1f1dSLionel Sambuc SemaRef.CheckUsingDeclQualifier(D->getUsingLoc(), SS, NameInfo,
2257f4a2713aSLionel Sambuc D->getLocation()))
2258f4a2713aSLionel Sambuc NewUD->setInvalidDecl();
2259f4a2713aSLionel Sambuc
2260f4a2713aSLionel Sambuc SemaRef.Context.setInstantiatedFromUsingDecl(NewUD, D);
2261f4a2713aSLionel Sambuc NewUD->setAccess(D->getAccess());
2262f4a2713aSLionel Sambuc Owner->addDecl(NewUD);
2263f4a2713aSLionel Sambuc
2264f4a2713aSLionel Sambuc // Don't process the shadow decls for an invalid decl.
2265f4a2713aSLionel Sambuc if (NewUD->isInvalidDecl())
2266f4a2713aSLionel Sambuc return NewUD;
2267f4a2713aSLionel Sambuc
2268f4a2713aSLionel Sambuc if (NameInfo.getName().getNameKind() == DeclarationName::CXXConstructorName) {
2269*0a6a1f1dSLionel Sambuc SemaRef.CheckInheritingConstructorUsingDecl(NewUD);
2270f4a2713aSLionel Sambuc return NewUD;
2271f4a2713aSLionel Sambuc }
2272f4a2713aSLionel Sambuc
2273f4a2713aSLionel Sambuc bool isFunctionScope = Owner->isFunctionOrMethod();
2274f4a2713aSLionel Sambuc
2275f4a2713aSLionel Sambuc // Process the shadow decls.
2276*0a6a1f1dSLionel Sambuc for (auto *Shadow : D->shadows()) {
2277f4a2713aSLionel Sambuc NamedDecl *InstTarget =
2278f4a2713aSLionel Sambuc cast_or_null<NamedDecl>(SemaRef.FindInstantiatedDecl(
2279f4a2713aSLionel Sambuc Shadow->getLocation(), Shadow->getTargetDecl(), TemplateArgs));
2280f4a2713aSLionel Sambuc if (!InstTarget)
2281*0a6a1f1dSLionel Sambuc return nullptr;
2282f4a2713aSLionel Sambuc
2283*0a6a1f1dSLionel Sambuc UsingShadowDecl *PrevDecl = nullptr;
2284f4a2713aSLionel Sambuc if (CheckRedeclaration) {
2285f4a2713aSLionel Sambuc if (SemaRef.CheckUsingShadowDecl(NewUD, InstTarget, Prev, PrevDecl))
2286f4a2713aSLionel Sambuc continue;
2287*0a6a1f1dSLionel Sambuc } else if (UsingShadowDecl *OldPrev =
2288*0a6a1f1dSLionel Sambuc getPreviousDeclForInstantiation(Shadow)) {
2289f4a2713aSLionel Sambuc PrevDecl = cast_or_null<UsingShadowDecl>(SemaRef.FindInstantiatedDecl(
2290f4a2713aSLionel Sambuc Shadow->getLocation(), OldPrev, TemplateArgs));
2291f4a2713aSLionel Sambuc }
2292f4a2713aSLionel Sambuc
2293f4a2713aSLionel Sambuc UsingShadowDecl *InstShadow =
2294*0a6a1f1dSLionel Sambuc SemaRef.BuildUsingShadowDecl(/*Scope*/nullptr, NewUD, InstTarget,
2295*0a6a1f1dSLionel Sambuc PrevDecl);
2296f4a2713aSLionel Sambuc SemaRef.Context.setInstantiatedFromUsingShadowDecl(InstShadow, Shadow);
2297f4a2713aSLionel Sambuc
2298f4a2713aSLionel Sambuc if (isFunctionScope)
2299f4a2713aSLionel Sambuc SemaRef.CurrentInstantiationScope->InstantiatedLocal(Shadow, InstShadow);
2300f4a2713aSLionel Sambuc }
2301f4a2713aSLionel Sambuc
2302f4a2713aSLionel Sambuc return NewUD;
2303f4a2713aSLionel Sambuc }
2304f4a2713aSLionel Sambuc
VisitUsingShadowDecl(UsingShadowDecl * D)2305f4a2713aSLionel Sambuc Decl *TemplateDeclInstantiator::VisitUsingShadowDecl(UsingShadowDecl *D) {
2306f4a2713aSLionel Sambuc // Ignore these; we handle them in bulk when processing the UsingDecl.
2307*0a6a1f1dSLionel Sambuc return nullptr;
2308f4a2713aSLionel Sambuc }
2309f4a2713aSLionel Sambuc
2310f4a2713aSLionel Sambuc Decl * TemplateDeclInstantiator
VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl * D)2311f4a2713aSLionel Sambuc ::VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D) {
2312f4a2713aSLionel Sambuc NestedNameSpecifierLoc QualifierLoc
2313f4a2713aSLionel Sambuc = SemaRef.SubstNestedNameSpecifierLoc(D->getQualifierLoc(),
2314f4a2713aSLionel Sambuc TemplateArgs);
2315f4a2713aSLionel Sambuc if (!QualifierLoc)
2316*0a6a1f1dSLionel Sambuc return nullptr;
2317f4a2713aSLionel Sambuc
2318f4a2713aSLionel Sambuc CXXScopeSpec SS;
2319f4a2713aSLionel Sambuc SS.Adopt(QualifierLoc);
2320f4a2713aSLionel Sambuc
2321f4a2713aSLionel Sambuc // Since NameInfo refers to a typename, it cannot be a C++ special name.
2322f4a2713aSLionel Sambuc // Hence, no transformation is required for it.
2323f4a2713aSLionel Sambuc DeclarationNameInfo NameInfo(D->getDeclName(), D->getLocation());
2324f4a2713aSLionel Sambuc NamedDecl *UD =
2325*0a6a1f1dSLionel Sambuc SemaRef.BuildUsingDeclaration(/*Scope*/ nullptr, D->getAccess(),
2326*0a6a1f1dSLionel Sambuc D->getUsingLoc(), SS, NameInfo, nullptr,
2327f4a2713aSLionel Sambuc /*instantiation*/ true,
2328f4a2713aSLionel Sambuc /*typename*/ true, D->getTypenameLoc());
2329f4a2713aSLionel Sambuc if (UD)
2330f4a2713aSLionel Sambuc SemaRef.Context.setInstantiatedFromUsingDecl(cast<UsingDecl>(UD), D);
2331f4a2713aSLionel Sambuc
2332f4a2713aSLionel Sambuc return UD;
2333f4a2713aSLionel Sambuc }
2334f4a2713aSLionel Sambuc
2335f4a2713aSLionel Sambuc Decl * TemplateDeclInstantiator
VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl * D)2336f4a2713aSLionel Sambuc ::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
2337f4a2713aSLionel Sambuc NestedNameSpecifierLoc QualifierLoc
2338f4a2713aSLionel Sambuc = SemaRef.SubstNestedNameSpecifierLoc(D->getQualifierLoc(), TemplateArgs);
2339f4a2713aSLionel Sambuc if (!QualifierLoc)
2340*0a6a1f1dSLionel Sambuc return nullptr;
2341f4a2713aSLionel Sambuc
2342f4a2713aSLionel Sambuc CXXScopeSpec SS;
2343f4a2713aSLionel Sambuc SS.Adopt(QualifierLoc);
2344f4a2713aSLionel Sambuc
2345f4a2713aSLionel Sambuc DeclarationNameInfo NameInfo
2346f4a2713aSLionel Sambuc = SemaRef.SubstDeclarationNameInfo(D->getNameInfo(), TemplateArgs);
2347f4a2713aSLionel Sambuc
2348f4a2713aSLionel Sambuc NamedDecl *UD =
2349*0a6a1f1dSLionel Sambuc SemaRef.BuildUsingDeclaration(/*Scope*/ nullptr, D->getAccess(),
2350*0a6a1f1dSLionel Sambuc D->getUsingLoc(), SS, NameInfo, nullptr,
2351f4a2713aSLionel Sambuc /*instantiation*/ true,
2352f4a2713aSLionel Sambuc /*typename*/ false, SourceLocation());
2353f4a2713aSLionel Sambuc if (UD)
2354f4a2713aSLionel Sambuc SemaRef.Context.setInstantiatedFromUsingDecl(cast<UsingDecl>(UD), D);
2355f4a2713aSLionel Sambuc
2356f4a2713aSLionel Sambuc return UD;
2357f4a2713aSLionel Sambuc }
2358f4a2713aSLionel Sambuc
2359f4a2713aSLionel Sambuc
VisitClassScopeFunctionSpecializationDecl(ClassScopeFunctionSpecializationDecl * Decl)2360f4a2713aSLionel Sambuc Decl *TemplateDeclInstantiator::VisitClassScopeFunctionSpecializationDecl(
2361f4a2713aSLionel Sambuc ClassScopeFunctionSpecializationDecl *Decl) {
2362f4a2713aSLionel Sambuc CXXMethodDecl *OldFD = Decl->getSpecialization();
2363*0a6a1f1dSLionel Sambuc CXXMethodDecl *NewFD =
2364*0a6a1f1dSLionel Sambuc cast_or_null<CXXMethodDecl>(VisitCXXMethodDecl(OldFD, nullptr, true));
2365*0a6a1f1dSLionel Sambuc if (!NewFD)
2366*0a6a1f1dSLionel Sambuc return nullptr;
2367f4a2713aSLionel Sambuc
2368f4a2713aSLionel Sambuc LookupResult Previous(SemaRef, NewFD->getNameInfo(), Sema::LookupOrdinaryName,
2369f4a2713aSLionel Sambuc Sema::ForRedeclaration);
2370f4a2713aSLionel Sambuc
2371f4a2713aSLionel Sambuc TemplateArgumentListInfo TemplateArgs;
2372*0a6a1f1dSLionel Sambuc TemplateArgumentListInfo *TemplateArgsPtr = nullptr;
2373f4a2713aSLionel Sambuc if (Decl->hasExplicitTemplateArgs()) {
2374f4a2713aSLionel Sambuc TemplateArgs = Decl->templateArgs();
2375f4a2713aSLionel Sambuc TemplateArgsPtr = &TemplateArgs;
2376f4a2713aSLionel Sambuc }
2377f4a2713aSLionel Sambuc
2378f4a2713aSLionel Sambuc SemaRef.LookupQualifiedName(Previous, SemaRef.CurContext);
2379f4a2713aSLionel Sambuc if (SemaRef.CheckFunctionTemplateSpecialization(NewFD, TemplateArgsPtr,
2380f4a2713aSLionel Sambuc Previous)) {
2381f4a2713aSLionel Sambuc NewFD->setInvalidDecl();
2382f4a2713aSLionel Sambuc return NewFD;
2383f4a2713aSLionel Sambuc }
2384f4a2713aSLionel Sambuc
2385f4a2713aSLionel Sambuc // Associate the specialization with the pattern.
2386f4a2713aSLionel Sambuc FunctionDecl *Specialization = cast<FunctionDecl>(Previous.getFoundDecl());
2387f4a2713aSLionel Sambuc assert(Specialization && "Class scope Specialization is null");
2388f4a2713aSLionel Sambuc SemaRef.Context.setClassScopeSpecializationPattern(Specialization, OldFD);
2389f4a2713aSLionel Sambuc
2390f4a2713aSLionel Sambuc return NewFD;
2391f4a2713aSLionel Sambuc }
2392f4a2713aSLionel Sambuc
VisitOMPThreadPrivateDecl(OMPThreadPrivateDecl * D)2393f4a2713aSLionel Sambuc Decl *TemplateDeclInstantiator::VisitOMPThreadPrivateDecl(
2394f4a2713aSLionel Sambuc OMPThreadPrivateDecl *D) {
2395f4a2713aSLionel Sambuc SmallVector<Expr *, 5> Vars;
2396*0a6a1f1dSLionel Sambuc for (auto *I : D->varlists()) {
2397*0a6a1f1dSLionel Sambuc Expr *Var = SemaRef.SubstExpr(I, TemplateArgs).get();
2398f4a2713aSLionel Sambuc assert(isa<DeclRefExpr>(Var) && "threadprivate arg is not a DeclRefExpr");
2399f4a2713aSLionel Sambuc Vars.push_back(Var);
2400f4a2713aSLionel Sambuc }
2401f4a2713aSLionel Sambuc
2402f4a2713aSLionel Sambuc OMPThreadPrivateDecl *TD =
2403f4a2713aSLionel Sambuc SemaRef.CheckOMPThreadPrivateDecl(D->getLocation(), Vars);
2404f4a2713aSLionel Sambuc
2405*0a6a1f1dSLionel Sambuc TD->setAccess(AS_public);
2406*0a6a1f1dSLionel Sambuc Owner->addDecl(TD);
2407*0a6a1f1dSLionel Sambuc
2408f4a2713aSLionel Sambuc return TD;
2409f4a2713aSLionel Sambuc }
2410f4a2713aSLionel Sambuc
VisitFunctionDecl(FunctionDecl * D)2411f4a2713aSLionel Sambuc Decl *TemplateDeclInstantiator::VisitFunctionDecl(FunctionDecl *D) {
2412*0a6a1f1dSLionel Sambuc return VisitFunctionDecl(D, nullptr);
2413f4a2713aSLionel Sambuc }
2414f4a2713aSLionel Sambuc
VisitCXXMethodDecl(CXXMethodDecl * D)2415f4a2713aSLionel Sambuc Decl *TemplateDeclInstantiator::VisitCXXMethodDecl(CXXMethodDecl *D) {
2416*0a6a1f1dSLionel Sambuc return VisitCXXMethodDecl(D, nullptr);
2417f4a2713aSLionel Sambuc }
2418f4a2713aSLionel Sambuc
VisitRecordDecl(RecordDecl * D)2419f4a2713aSLionel Sambuc Decl *TemplateDeclInstantiator::VisitRecordDecl(RecordDecl *D) {
2420f4a2713aSLionel Sambuc llvm_unreachable("There are only CXXRecordDecls in C++");
2421f4a2713aSLionel Sambuc }
2422f4a2713aSLionel Sambuc
2423f4a2713aSLionel Sambuc Decl *
VisitClassTemplateSpecializationDecl(ClassTemplateSpecializationDecl * D)2424f4a2713aSLionel Sambuc TemplateDeclInstantiator::VisitClassTemplateSpecializationDecl(
2425f4a2713aSLionel Sambuc ClassTemplateSpecializationDecl *D) {
2426*0a6a1f1dSLionel Sambuc // As a MS extension, we permit class-scope explicit specialization
2427*0a6a1f1dSLionel Sambuc // of member class templates.
2428*0a6a1f1dSLionel Sambuc ClassTemplateDecl *ClassTemplate = D->getSpecializedTemplate();
2429*0a6a1f1dSLionel Sambuc assert(ClassTemplate->getDeclContext()->isRecord() &&
2430*0a6a1f1dSLionel Sambuc D->getTemplateSpecializationKind() == TSK_ExplicitSpecialization &&
2431*0a6a1f1dSLionel Sambuc "can only instantiate an explicit specialization "
2432*0a6a1f1dSLionel Sambuc "for a member class template");
2433*0a6a1f1dSLionel Sambuc
2434*0a6a1f1dSLionel Sambuc // Lookup the already-instantiated declaration in the instantiation
2435*0a6a1f1dSLionel Sambuc // of the class template. FIXME: Diagnose or assert if this fails?
2436*0a6a1f1dSLionel Sambuc DeclContext::lookup_result Found
2437*0a6a1f1dSLionel Sambuc = Owner->lookup(ClassTemplate->getDeclName());
2438*0a6a1f1dSLionel Sambuc if (Found.empty())
2439*0a6a1f1dSLionel Sambuc return nullptr;
2440*0a6a1f1dSLionel Sambuc ClassTemplateDecl *InstClassTemplate
2441*0a6a1f1dSLionel Sambuc = dyn_cast<ClassTemplateDecl>(Found.front());
2442*0a6a1f1dSLionel Sambuc if (!InstClassTemplate)
2443*0a6a1f1dSLionel Sambuc return nullptr;
2444*0a6a1f1dSLionel Sambuc
2445*0a6a1f1dSLionel Sambuc // Substitute into the template arguments of the class template explicit
2446*0a6a1f1dSLionel Sambuc // specialization.
2447*0a6a1f1dSLionel Sambuc TemplateSpecializationTypeLoc Loc = D->getTypeAsWritten()->getTypeLoc().
2448*0a6a1f1dSLionel Sambuc castAs<TemplateSpecializationTypeLoc>();
2449*0a6a1f1dSLionel Sambuc TemplateArgumentListInfo InstTemplateArgs(Loc.getLAngleLoc(),
2450*0a6a1f1dSLionel Sambuc Loc.getRAngleLoc());
2451*0a6a1f1dSLionel Sambuc SmallVector<TemplateArgumentLoc, 4> ArgLocs;
2452*0a6a1f1dSLionel Sambuc for (unsigned I = 0; I != Loc.getNumArgs(); ++I)
2453*0a6a1f1dSLionel Sambuc ArgLocs.push_back(Loc.getArgLoc(I));
2454*0a6a1f1dSLionel Sambuc if (SemaRef.Subst(ArgLocs.data(), ArgLocs.size(),
2455*0a6a1f1dSLionel Sambuc InstTemplateArgs, TemplateArgs))
2456*0a6a1f1dSLionel Sambuc return nullptr;
2457*0a6a1f1dSLionel Sambuc
2458*0a6a1f1dSLionel Sambuc // Check that the template argument list is well-formed for this
2459*0a6a1f1dSLionel Sambuc // class template.
2460*0a6a1f1dSLionel Sambuc SmallVector<TemplateArgument, 4> Converted;
2461*0a6a1f1dSLionel Sambuc if (SemaRef.CheckTemplateArgumentList(InstClassTemplate,
2462*0a6a1f1dSLionel Sambuc D->getLocation(),
2463*0a6a1f1dSLionel Sambuc InstTemplateArgs,
2464*0a6a1f1dSLionel Sambuc false,
2465*0a6a1f1dSLionel Sambuc Converted))
2466*0a6a1f1dSLionel Sambuc return nullptr;
2467*0a6a1f1dSLionel Sambuc
2468*0a6a1f1dSLionel Sambuc // Figure out where to insert this class template explicit specialization
2469*0a6a1f1dSLionel Sambuc // in the member template's set of class template explicit specializations.
2470*0a6a1f1dSLionel Sambuc void *InsertPos = nullptr;
2471*0a6a1f1dSLionel Sambuc ClassTemplateSpecializationDecl *PrevDecl =
2472*0a6a1f1dSLionel Sambuc InstClassTemplate->findSpecialization(Converted, InsertPos);
2473*0a6a1f1dSLionel Sambuc
2474*0a6a1f1dSLionel Sambuc // Check whether we've already seen a conflicting instantiation of this
2475*0a6a1f1dSLionel Sambuc // declaration (for instance, if there was a prior implicit instantiation).
2476*0a6a1f1dSLionel Sambuc bool Ignored;
2477*0a6a1f1dSLionel Sambuc if (PrevDecl &&
2478*0a6a1f1dSLionel Sambuc SemaRef.CheckSpecializationInstantiationRedecl(D->getLocation(),
2479*0a6a1f1dSLionel Sambuc D->getSpecializationKind(),
2480*0a6a1f1dSLionel Sambuc PrevDecl,
2481*0a6a1f1dSLionel Sambuc PrevDecl->getSpecializationKind(),
2482*0a6a1f1dSLionel Sambuc PrevDecl->getPointOfInstantiation(),
2483*0a6a1f1dSLionel Sambuc Ignored))
2484*0a6a1f1dSLionel Sambuc return nullptr;
2485*0a6a1f1dSLionel Sambuc
2486*0a6a1f1dSLionel Sambuc // If PrevDecl was a definition and D is also a definition, diagnose.
2487*0a6a1f1dSLionel Sambuc // This happens in cases like:
2488*0a6a1f1dSLionel Sambuc //
2489*0a6a1f1dSLionel Sambuc // template<typename T, typename U>
2490*0a6a1f1dSLionel Sambuc // struct Outer {
2491*0a6a1f1dSLionel Sambuc // template<typename X> struct Inner;
2492*0a6a1f1dSLionel Sambuc // template<> struct Inner<T> {};
2493*0a6a1f1dSLionel Sambuc // template<> struct Inner<U> {};
2494*0a6a1f1dSLionel Sambuc // };
2495*0a6a1f1dSLionel Sambuc //
2496*0a6a1f1dSLionel Sambuc // Outer<int, int> outer; // error: the explicit specializations of Inner
2497*0a6a1f1dSLionel Sambuc // // have the same signature.
2498*0a6a1f1dSLionel Sambuc if (PrevDecl && PrevDecl->getDefinition() &&
2499*0a6a1f1dSLionel Sambuc D->isThisDeclarationADefinition()) {
2500*0a6a1f1dSLionel Sambuc SemaRef.Diag(D->getLocation(), diag::err_redefinition) << PrevDecl;
2501*0a6a1f1dSLionel Sambuc SemaRef.Diag(PrevDecl->getDefinition()->getLocation(),
2502*0a6a1f1dSLionel Sambuc diag::note_previous_definition);
2503*0a6a1f1dSLionel Sambuc return nullptr;
2504*0a6a1f1dSLionel Sambuc }
2505*0a6a1f1dSLionel Sambuc
2506*0a6a1f1dSLionel Sambuc // Create the class template partial specialization declaration.
2507*0a6a1f1dSLionel Sambuc ClassTemplateSpecializationDecl *InstD
2508*0a6a1f1dSLionel Sambuc = ClassTemplateSpecializationDecl::Create(SemaRef.Context,
2509*0a6a1f1dSLionel Sambuc D->getTagKind(),
2510*0a6a1f1dSLionel Sambuc Owner,
2511*0a6a1f1dSLionel Sambuc D->getLocStart(),
2512*0a6a1f1dSLionel Sambuc D->getLocation(),
2513*0a6a1f1dSLionel Sambuc InstClassTemplate,
2514*0a6a1f1dSLionel Sambuc Converted.data(),
2515*0a6a1f1dSLionel Sambuc Converted.size(),
2516*0a6a1f1dSLionel Sambuc PrevDecl);
2517*0a6a1f1dSLionel Sambuc
2518*0a6a1f1dSLionel Sambuc // Add this partial specialization to the set of class template partial
2519*0a6a1f1dSLionel Sambuc // specializations.
2520*0a6a1f1dSLionel Sambuc if (!PrevDecl)
2521*0a6a1f1dSLionel Sambuc InstClassTemplate->AddSpecialization(InstD, InsertPos);
2522*0a6a1f1dSLionel Sambuc
2523*0a6a1f1dSLionel Sambuc // Substitute the nested name specifier, if any.
2524*0a6a1f1dSLionel Sambuc if (SubstQualifier(D, InstD))
2525*0a6a1f1dSLionel Sambuc return nullptr;
2526*0a6a1f1dSLionel Sambuc
2527*0a6a1f1dSLionel Sambuc // Build the canonical type that describes the converted template
2528*0a6a1f1dSLionel Sambuc // arguments of the class template explicit specialization.
2529*0a6a1f1dSLionel Sambuc QualType CanonType = SemaRef.Context.getTemplateSpecializationType(
2530*0a6a1f1dSLionel Sambuc TemplateName(InstClassTemplate), Converted.data(), Converted.size(),
2531*0a6a1f1dSLionel Sambuc SemaRef.Context.getRecordType(InstD));
2532*0a6a1f1dSLionel Sambuc
2533*0a6a1f1dSLionel Sambuc // Build the fully-sugared type for this class template
2534*0a6a1f1dSLionel Sambuc // specialization as the user wrote in the specialization
2535*0a6a1f1dSLionel Sambuc // itself. This means that we'll pretty-print the type retrieved
2536*0a6a1f1dSLionel Sambuc // from the specialization's declaration the way that the user
2537*0a6a1f1dSLionel Sambuc // actually wrote the specialization, rather than formatting the
2538*0a6a1f1dSLionel Sambuc // name based on the "canonical" representation used to store the
2539*0a6a1f1dSLionel Sambuc // template arguments in the specialization.
2540*0a6a1f1dSLionel Sambuc TypeSourceInfo *WrittenTy = SemaRef.Context.getTemplateSpecializationTypeInfo(
2541*0a6a1f1dSLionel Sambuc TemplateName(InstClassTemplate), D->getLocation(), InstTemplateArgs,
2542*0a6a1f1dSLionel Sambuc CanonType);
2543*0a6a1f1dSLionel Sambuc
2544*0a6a1f1dSLionel Sambuc InstD->setAccess(D->getAccess());
2545*0a6a1f1dSLionel Sambuc InstD->setInstantiationOfMemberClass(D, TSK_ImplicitInstantiation);
2546*0a6a1f1dSLionel Sambuc InstD->setSpecializationKind(D->getSpecializationKind());
2547*0a6a1f1dSLionel Sambuc InstD->setTypeAsWritten(WrittenTy);
2548*0a6a1f1dSLionel Sambuc InstD->setExternLoc(D->getExternLoc());
2549*0a6a1f1dSLionel Sambuc InstD->setTemplateKeywordLoc(D->getTemplateKeywordLoc());
2550*0a6a1f1dSLionel Sambuc
2551*0a6a1f1dSLionel Sambuc Owner->addDecl(InstD);
2552*0a6a1f1dSLionel Sambuc
2553*0a6a1f1dSLionel Sambuc // Instantiate the members of the class-scope explicit specialization eagerly.
2554*0a6a1f1dSLionel Sambuc // We don't have support for lazy instantiation of an explicit specialization
2555*0a6a1f1dSLionel Sambuc // yet, and MSVC eagerly instantiates in this case.
2556*0a6a1f1dSLionel Sambuc if (D->isThisDeclarationADefinition() &&
2557*0a6a1f1dSLionel Sambuc SemaRef.InstantiateClass(D->getLocation(), InstD, D, TemplateArgs,
2558*0a6a1f1dSLionel Sambuc TSK_ImplicitInstantiation,
2559*0a6a1f1dSLionel Sambuc /*Complain=*/true))
2560*0a6a1f1dSLionel Sambuc return nullptr;
2561*0a6a1f1dSLionel Sambuc
2562*0a6a1f1dSLionel Sambuc return InstD;
2563f4a2713aSLionel Sambuc }
2564f4a2713aSLionel Sambuc
VisitVarTemplateSpecializationDecl(VarTemplateSpecializationDecl * D)2565f4a2713aSLionel Sambuc Decl *TemplateDeclInstantiator::VisitVarTemplateSpecializationDecl(
2566f4a2713aSLionel Sambuc VarTemplateSpecializationDecl *D) {
2567f4a2713aSLionel Sambuc
2568f4a2713aSLionel Sambuc TemplateArgumentListInfo VarTemplateArgsInfo;
2569f4a2713aSLionel Sambuc VarTemplateDecl *VarTemplate = D->getSpecializedTemplate();
2570f4a2713aSLionel Sambuc assert(VarTemplate &&
2571f4a2713aSLionel Sambuc "A template specialization without specialized template?");
2572f4a2713aSLionel Sambuc
2573f4a2713aSLionel Sambuc // Substitute the current template arguments.
2574f4a2713aSLionel Sambuc const TemplateArgumentListInfo &TemplateArgsInfo = D->getTemplateArgsInfo();
2575f4a2713aSLionel Sambuc VarTemplateArgsInfo.setLAngleLoc(TemplateArgsInfo.getLAngleLoc());
2576f4a2713aSLionel Sambuc VarTemplateArgsInfo.setRAngleLoc(TemplateArgsInfo.getRAngleLoc());
2577f4a2713aSLionel Sambuc
2578f4a2713aSLionel Sambuc if (SemaRef.Subst(TemplateArgsInfo.getArgumentArray(),
2579f4a2713aSLionel Sambuc TemplateArgsInfo.size(), VarTemplateArgsInfo, TemplateArgs))
2580*0a6a1f1dSLionel Sambuc return nullptr;
2581f4a2713aSLionel Sambuc
2582f4a2713aSLionel Sambuc // Check that the template argument list is well-formed for this template.
2583f4a2713aSLionel Sambuc SmallVector<TemplateArgument, 4> Converted;
2584f4a2713aSLionel Sambuc if (SemaRef.CheckTemplateArgumentList(
2585f4a2713aSLionel Sambuc VarTemplate, VarTemplate->getLocStart(),
2586f4a2713aSLionel Sambuc const_cast<TemplateArgumentListInfo &>(VarTemplateArgsInfo), false,
2587*0a6a1f1dSLionel Sambuc Converted))
2588*0a6a1f1dSLionel Sambuc return nullptr;
2589f4a2713aSLionel Sambuc
2590f4a2713aSLionel Sambuc // Find the variable template specialization declaration that
2591f4a2713aSLionel Sambuc // corresponds to these arguments.
2592*0a6a1f1dSLionel Sambuc void *InsertPos = nullptr;
2593f4a2713aSLionel Sambuc if (VarTemplateSpecializationDecl *VarSpec = VarTemplate->findSpecialization(
2594*0a6a1f1dSLionel Sambuc Converted, InsertPos))
2595f4a2713aSLionel Sambuc // If we already have a variable template specialization, return it.
2596f4a2713aSLionel Sambuc return VarSpec;
2597f4a2713aSLionel Sambuc
2598f4a2713aSLionel Sambuc return VisitVarTemplateSpecializationDecl(VarTemplate, D, InsertPos,
2599f4a2713aSLionel Sambuc VarTemplateArgsInfo, Converted);
2600f4a2713aSLionel Sambuc }
2601f4a2713aSLionel Sambuc
VisitVarTemplateSpecializationDecl(VarTemplateDecl * VarTemplate,VarDecl * D,void * InsertPos,const TemplateArgumentListInfo & TemplateArgsInfo,ArrayRef<TemplateArgument> Converted)2602f4a2713aSLionel Sambuc Decl *TemplateDeclInstantiator::VisitVarTemplateSpecializationDecl(
2603f4a2713aSLionel Sambuc VarTemplateDecl *VarTemplate, VarDecl *D, void *InsertPos,
2604f4a2713aSLionel Sambuc const TemplateArgumentListInfo &TemplateArgsInfo,
2605*0a6a1f1dSLionel Sambuc ArrayRef<TemplateArgument> Converted) {
2606f4a2713aSLionel Sambuc
2607f4a2713aSLionel Sambuc // If this is the variable for an anonymous struct or union,
2608f4a2713aSLionel Sambuc // instantiate the anonymous struct/union type first.
2609f4a2713aSLionel Sambuc if (const RecordType *RecordTy = D->getType()->getAs<RecordType>())
2610f4a2713aSLionel Sambuc if (RecordTy->getDecl()->isAnonymousStructOrUnion())
2611f4a2713aSLionel Sambuc if (!VisitCXXRecordDecl(cast<CXXRecordDecl>(RecordTy->getDecl())))
2612*0a6a1f1dSLionel Sambuc return nullptr;
2613f4a2713aSLionel Sambuc
2614f4a2713aSLionel Sambuc // Do substitution on the type of the declaration
2615f4a2713aSLionel Sambuc TypeSourceInfo *DI =
2616f4a2713aSLionel Sambuc SemaRef.SubstType(D->getTypeSourceInfo(), TemplateArgs,
2617f4a2713aSLionel Sambuc D->getTypeSpecStartLoc(), D->getDeclName());
2618f4a2713aSLionel Sambuc if (!DI)
2619*0a6a1f1dSLionel Sambuc return nullptr;
2620f4a2713aSLionel Sambuc
2621f4a2713aSLionel Sambuc if (DI->getType()->isFunctionType()) {
2622f4a2713aSLionel Sambuc SemaRef.Diag(D->getLocation(), diag::err_variable_instantiates_to_function)
2623f4a2713aSLionel Sambuc << D->isStaticDataMember() << DI->getType();
2624*0a6a1f1dSLionel Sambuc return nullptr;
2625f4a2713aSLionel Sambuc }
2626f4a2713aSLionel Sambuc
2627f4a2713aSLionel Sambuc // Build the instantiated declaration
2628f4a2713aSLionel Sambuc VarTemplateSpecializationDecl *Var = VarTemplateSpecializationDecl::Create(
2629f4a2713aSLionel Sambuc SemaRef.Context, Owner, D->getInnerLocStart(), D->getLocation(),
2630f4a2713aSLionel Sambuc VarTemplate, DI->getType(), DI, D->getStorageClass(), Converted.data(),
2631f4a2713aSLionel Sambuc Converted.size());
2632f4a2713aSLionel Sambuc Var->setTemplateArgsInfo(TemplateArgsInfo);
2633f4a2713aSLionel Sambuc if (InsertPos)
2634f4a2713aSLionel Sambuc VarTemplate->AddSpecialization(Var, InsertPos);
2635f4a2713aSLionel Sambuc
2636f4a2713aSLionel Sambuc // Substitute the nested name specifier, if any.
2637f4a2713aSLionel Sambuc if (SubstQualifier(D, Var))
2638*0a6a1f1dSLionel Sambuc return nullptr;
2639f4a2713aSLionel Sambuc
2640f4a2713aSLionel Sambuc SemaRef.BuildVariableInstantiation(Var, D, TemplateArgs, LateAttrs,
2641f4a2713aSLionel Sambuc Owner, StartingScope);
2642f4a2713aSLionel Sambuc
2643f4a2713aSLionel Sambuc return Var;
2644f4a2713aSLionel Sambuc }
2645f4a2713aSLionel Sambuc
VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl * D)2646f4a2713aSLionel Sambuc Decl *TemplateDeclInstantiator::VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *D) {
2647f4a2713aSLionel Sambuc llvm_unreachable("@defs is not supported in Objective-C++");
2648f4a2713aSLionel Sambuc }
2649f4a2713aSLionel Sambuc
VisitFriendTemplateDecl(FriendTemplateDecl * D)2650f4a2713aSLionel Sambuc Decl *TemplateDeclInstantiator::VisitFriendTemplateDecl(FriendTemplateDecl *D) {
2651f4a2713aSLionel Sambuc // FIXME: We need to be able to instantiate FriendTemplateDecls.
2652f4a2713aSLionel Sambuc unsigned DiagID = SemaRef.getDiagnostics().getCustomDiagID(
2653f4a2713aSLionel Sambuc DiagnosticsEngine::Error,
2654f4a2713aSLionel Sambuc "cannot instantiate %0 yet");
2655f4a2713aSLionel Sambuc SemaRef.Diag(D->getLocation(), DiagID)
2656f4a2713aSLionel Sambuc << D->getDeclKindName();
2657f4a2713aSLionel Sambuc
2658*0a6a1f1dSLionel Sambuc return nullptr;
2659f4a2713aSLionel Sambuc }
2660f4a2713aSLionel Sambuc
VisitDecl(Decl * D)2661f4a2713aSLionel Sambuc Decl *TemplateDeclInstantiator::VisitDecl(Decl *D) {
2662f4a2713aSLionel Sambuc llvm_unreachable("Unexpected decl");
2663f4a2713aSLionel Sambuc }
2664f4a2713aSLionel Sambuc
SubstDecl(Decl * D,DeclContext * Owner,const MultiLevelTemplateArgumentList & TemplateArgs)2665f4a2713aSLionel Sambuc Decl *Sema::SubstDecl(Decl *D, DeclContext *Owner,
2666f4a2713aSLionel Sambuc const MultiLevelTemplateArgumentList &TemplateArgs) {
2667f4a2713aSLionel Sambuc TemplateDeclInstantiator Instantiator(*this, Owner, TemplateArgs);
2668f4a2713aSLionel Sambuc if (D->isInvalidDecl())
2669*0a6a1f1dSLionel Sambuc return nullptr;
2670f4a2713aSLionel Sambuc
2671f4a2713aSLionel Sambuc return Instantiator.Visit(D);
2672f4a2713aSLionel Sambuc }
2673f4a2713aSLionel Sambuc
2674f4a2713aSLionel Sambuc /// \brief Instantiates a nested template parameter list in the current
2675f4a2713aSLionel Sambuc /// instantiation context.
2676f4a2713aSLionel Sambuc ///
2677f4a2713aSLionel Sambuc /// \param L The parameter list to instantiate
2678f4a2713aSLionel Sambuc ///
2679f4a2713aSLionel Sambuc /// \returns NULL if there was an error
2680f4a2713aSLionel Sambuc TemplateParameterList *
SubstTemplateParams(TemplateParameterList * L)2681f4a2713aSLionel Sambuc TemplateDeclInstantiator::SubstTemplateParams(TemplateParameterList *L) {
2682f4a2713aSLionel Sambuc // Get errors for all the parameters before bailing out.
2683f4a2713aSLionel Sambuc bool Invalid = false;
2684f4a2713aSLionel Sambuc
2685f4a2713aSLionel Sambuc unsigned N = L->size();
2686f4a2713aSLionel Sambuc typedef SmallVector<NamedDecl *, 8> ParamVector;
2687f4a2713aSLionel Sambuc ParamVector Params;
2688f4a2713aSLionel Sambuc Params.reserve(N);
2689f4a2713aSLionel Sambuc for (TemplateParameterList::iterator PI = L->begin(), PE = L->end();
2690f4a2713aSLionel Sambuc PI != PE; ++PI) {
2691f4a2713aSLionel Sambuc NamedDecl *D = cast_or_null<NamedDecl>(Visit(*PI));
2692f4a2713aSLionel Sambuc Params.push_back(D);
2693f4a2713aSLionel Sambuc Invalid = Invalid || !D || D->isInvalidDecl();
2694f4a2713aSLionel Sambuc }
2695f4a2713aSLionel Sambuc
2696f4a2713aSLionel Sambuc // Clean up if we had an error.
2697f4a2713aSLionel Sambuc if (Invalid)
2698*0a6a1f1dSLionel Sambuc return nullptr;
2699f4a2713aSLionel Sambuc
2700f4a2713aSLionel Sambuc TemplateParameterList *InstL
2701f4a2713aSLionel Sambuc = TemplateParameterList::Create(SemaRef.Context, L->getTemplateLoc(),
2702f4a2713aSLionel Sambuc L->getLAngleLoc(), &Params.front(), N,
2703f4a2713aSLionel Sambuc L->getRAngleLoc());
2704f4a2713aSLionel Sambuc return InstL;
2705f4a2713aSLionel Sambuc }
2706f4a2713aSLionel Sambuc
2707f4a2713aSLionel Sambuc /// \brief Instantiate the declaration of a class template partial
2708f4a2713aSLionel Sambuc /// specialization.
2709f4a2713aSLionel Sambuc ///
2710f4a2713aSLionel Sambuc /// \param ClassTemplate the (instantiated) class template that is partially
2711f4a2713aSLionel Sambuc // specialized by the instantiation of \p PartialSpec.
2712f4a2713aSLionel Sambuc ///
2713f4a2713aSLionel Sambuc /// \param PartialSpec the (uninstantiated) class template partial
2714f4a2713aSLionel Sambuc /// specialization that we are instantiating.
2715f4a2713aSLionel Sambuc ///
2716f4a2713aSLionel Sambuc /// \returns The instantiated partial specialization, if successful; otherwise,
2717f4a2713aSLionel Sambuc /// NULL to indicate an error.
2718f4a2713aSLionel Sambuc ClassTemplatePartialSpecializationDecl *
InstantiateClassTemplatePartialSpecialization(ClassTemplateDecl * ClassTemplate,ClassTemplatePartialSpecializationDecl * PartialSpec)2719f4a2713aSLionel Sambuc TemplateDeclInstantiator::InstantiateClassTemplatePartialSpecialization(
2720f4a2713aSLionel Sambuc ClassTemplateDecl *ClassTemplate,
2721f4a2713aSLionel Sambuc ClassTemplatePartialSpecializationDecl *PartialSpec) {
2722f4a2713aSLionel Sambuc // Create a local instantiation scope for this class template partial
2723f4a2713aSLionel Sambuc // specialization, which will contain the instantiations of the template
2724f4a2713aSLionel Sambuc // parameters.
2725f4a2713aSLionel Sambuc LocalInstantiationScope Scope(SemaRef);
2726f4a2713aSLionel Sambuc
2727f4a2713aSLionel Sambuc // Substitute into the template parameters of the class template partial
2728f4a2713aSLionel Sambuc // specialization.
2729f4a2713aSLionel Sambuc TemplateParameterList *TempParams = PartialSpec->getTemplateParameters();
2730f4a2713aSLionel Sambuc TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
2731f4a2713aSLionel Sambuc if (!InstParams)
2732*0a6a1f1dSLionel Sambuc return nullptr;
2733f4a2713aSLionel Sambuc
2734f4a2713aSLionel Sambuc // Substitute into the template arguments of the class template partial
2735f4a2713aSLionel Sambuc // specialization.
2736f4a2713aSLionel Sambuc const ASTTemplateArgumentListInfo *TemplArgInfo
2737f4a2713aSLionel Sambuc = PartialSpec->getTemplateArgsAsWritten();
2738f4a2713aSLionel Sambuc TemplateArgumentListInfo InstTemplateArgs(TemplArgInfo->LAngleLoc,
2739f4a2713aSLionel Sambuc TemplArgInfo->RAngleLoc);
2740f4a2713aSLionel Sambuc if (SemaRef.Subst(TemplArgInfo->getTemplateArgs(),
2741f4a2713aSLionel Sambuc TemplArgInfo->NumTemplateArgs,
2742f4a2713aSLionel Sambuc InstTemplateArgs, TemplateArgs))
2743*0a6a1f1dSLionel Sambuc return nullptr;
2744f4a2713aSLionel Sambuc
2745f4a2713aSLionel Sambuc // Check that the template argument list is well-formed for this
2746f4a2713aSLionel Sambuc // class template.
2747f4a2713aSLionel Sambuc SmallVector<TemplateArgument, 4> Converted;
2748f4a2713aSLionel Sambuc if (SemaRef.CheckTemplateArgumentList(ClassTemplate,
2749f4a2713aSLionel Sambuc PartialSpec->getLocation(),
2750f4a2713aSLionel Sambuc InstTemplateArgs,
2751f4a2713aSLionel Sambuc false,
2752f4a2713aSLionel Sambuc Converted))
2753*0a6a1f1dSLionel Sambuc return nullptr;
2754f4a2713aSLionel Sambuc
2755f4a2713aSLionel Sambuc // Figure out where to insert this class template partial specialization
2756f4a2713aSLionel Sambuc // in the member template's set of class template partial specializations.
2757*0a6a1f1dSLionel Sambuc void *InsertPos = nullptr;
2758f4a2713aSLionel Sambuc ClassTemplateSpecializationDecl *PrevDecl
2759*0a6a1f1dSLionel Sambuc = ClassTemplate->findPartialSpecialization(Converted, InsertPos);
2760f4a2713aSLionel Sambuc
2761f4a2713aSLionel Sambuc // Build the canonical type that describes the converted template
2762f4a2713aSLionel Sambuc // arguments of the class template partial specialization.
2763f4a2713aSLionel Sambuc QualType CanonType
2764f4a2713aSLionel Sambuc = SemaRef.Context.getTemplateSpecializationType(TemplateName(ClassTemplate),
2765f4a2713aSLionel Sambuc Converted.data(),
2766f4a2713aSLionel Sambuc Converted.size());
2767f4a2713aSLionel Sambuc
2768f4a2713aSLionel Sambuc // Build the fully-sugared type for this class template
2769f4a2713aSLionel Sambuc // specialization as the user wrote in the specialization
2770f4a2713aSLionel Sambuc // itself. This means that we'll pretty-print the type retrieved
2771f4a2713aSLionel Sambuc // from the specialization's declaration the way that the user
2772f4a2713aSLionel Sambuc // actually wrote the specialization, rather than formatting the
2773f4a2713aSLionel Sambuc // name based on the "canonical" representation used to store the
2774f4a2713aSLionel Sambuc // template arguments in the specialization.
2775f4a2713aSLionel Sambuc TypeSourceInfo *WrittenTy
2776f4a2713aSLionel Sambuc = SemaRef.Context.getTemplateSpecializationTypeInfo(
2777f4a2713aSLionel Sambuc TemplateName(ClassTemplate),
2778f4a2713aSLionel Sambuc PartialSpec->getLocation(),
2779f4a2713aSLionel Sambuc InstTemplateArgs,
2780f4a2713aSLionel Sambuc CanonType);
2781f4a2713aSLionel Sambuc
2782f4a2713aSLionel Sambuc if (PrevDecl) {
2783f4a2713aSLionel Sambuc // We've already seen a partial specialization with the same template
2784f4a2713aSLionel Sambuc // parameters and template arguments. This can happen, for example, when
2785f4a2713aSLionel Sambuc // substituting the outer template arguments ends up causing two
2786f4a2713aSLionel Sambuc // class template partial specializations of a member class template
2787f4a2713aSLionel Sambuc // to have identical forms, e.g.,
2788f4a2713aSLionel Sambuc //
2789f4a2713aSLionel Sambuc // template<typename T, typename U>
2790f4a2713aSLionel Sambuc // struct Outer {
2791f4a2713aSLionel Sambuc // template<typename X, typename Y> struct Inner;
2792f4a2713aSLionel Sambuc // template<typename Y> struct Inner<T, Y>;
2793f4a2713aSLionel Sambuc // template<typename Y> struct Inner<U, Y>;
2794f4a2713aSLionel Sambuc // };
2795f4a2713aSLionel Sambuc //
2796f4a2713aSLionel Sambuc // Outer<int, int> outer; // error: the partial specializations of Inner
2797f4a2713aSLionel Sambuc // // have the same signature.
2798f4a2713aSLionel Sambuc SemaRef.Diag(PartialSpec->getLocation(), diag::err_partial_spec_redeclared)
2799f4a2713aSLionel Sambuc << WrittenTy->getType();
2800f4a2713aSLionel Sambuc SemaRef.Diag(PrevDecl->getLocation(), diag::note_prev_partial_spec_here)
2801f4a2713aSLionel Sambuc << SemaRef.Context.getTypeDeclType(PrevDecl);
2802*0a6a1f1dSLionel Sambuc return nullptr;
2803f4a2713aSLionel Sambuc }
2804f4a2713aSLionel Sambuc
2805f4a2713aSLionel Sambuc
2806f4a2713aSLionel Sambuc // Create the class template partial specialization declaration.
2807f4a2713aSLionel Sambuc ClassTemplatePartialSpecializationDecl *InstPartialSpec
2808f4a2713aSLionel Sambuc = ClassTemplatePartialSpecializationDecl::Create(SemaRef.Context,
2809f4a2713aSLionel Sambuc PartialSpec->getTagKind(),
2810f4a2713aSLionel Sambuc Owner,
2811f4a2713aSLionel Sambuc PartialSpec->getLocStart(),
2812f4a2713aSLionel Sambuc PartialSpec->getLocation(),
2813f4a2713aSLionel Sambuc InstParams,
2814f4a2713aSLionel Sambuc ClassTemplate,
2815f4a2713aSLionel Sambuc Converted.data(),
2816f4a2713aSLionel Sambuc Converted.size(),
2817f4a2713aSLionel Sambuc InstTemplateArgs,
2818f4a2713aSLionel Sambuc CanonType,
2819*0a6a1f1dSLionel Sambuc nullptr);
2820f4a2713aSLionel Sambuc // Substitute the nested name specifier, if any.
2821f4a2713aSLionel Sambuc if (SubstQualifier(PartialSpec, InstPartialSpec))
2822*0a6a1f1dSLionel Sambuc return nullptr;
2823f4a2713aSLionel Sambuc
2824f4a2713aSLionel Sambuc InstPartialSpec->setInstantiatedFromMember(PartialSpec);
2825f4a2713aSLionel Sambuc InstPartialSpec->setTypeAsWritten(WrittenTy);
2826f4a2713aSLionel Sambuc
2827f4a2713aSLionel Sambuc // Add this partial specialization to the set of class template partial
2828f4a2713aSLionel Sambuc // specializations.
2829*0a6a1f1dSLionel Sambuc ClassTemplate->AddPartialSpecialization(InstPartialSpec,
2830*0a6a1f1dSLionel Sambuc /*InsertPos=*/nullptr);
2831f4a2713aSLionel Sambuc return InstPartialSpec;
2832f4a2713aSLionel Sambuc }
2833f4a2713aSLionel Sambuc
2834f4a2713aSLionel Sambuc /// \brief Instantiate the declaration of a variable template partial
2835f4a2713aSLionel Sambuc /// specialization.
2836f4a2713aSLionel Sambuc ///
2837f4a2713aSLionel Sambuc /// \param VarTemplate the (instantiated) variable template that is partially
2838f4a2713aSLionel Sambuc /// specialized by the instantiation of \p PartialSpec.
2839f4a2713aSLionel Sambuc ///
2840f4a2713aSLionel Sambuc /// \param PartialSpec the (uninstantiated) variable template partial
2841f4a2713aSLionel Sambuc /// specialization that we are instantiating.
2842f4a2713aSLionel Sambuc ///
2843f4a2713aSLionel Sambuc /// \returns The instantiated partial specialization, if successful; otherwise,
2844f4a2713aSLionel Sambuc /// NULL to indicate an error.
2845f4a2713aSLionel Sambuc VarTemplatePartialSpecializationDecl *
InstantiateVarTemplatePartialSpecialization(VarTemplateDecl * VarTemplate,VarTemplatePartialSpecializationDecl * PartialSpec)2846f4a2713aSLionel Sambuc TemplateDeclInstantiator::InstantiateVarTemplatePartialSpecialization(
2847f4a2713aSLionel Sambuc VarTemplateDecl *VarTemplate,
2848f4a2713aSLionel Sambuc VarTemplatePartialSpecializationDecl *PartialSpec) {
2849f4a2713aSLionel Sambuc // Create a local instantiation scope for this variable template partial
2850f4a2713aSLionel Sambuc // specialization, which will contain the instantiations of the template
2851f4a2713aSLionel Sambuc // parameters.
2852f4a2713aSLionel Sambuc LocalInstantiationScope Scope(SemaRef);
2853f4a2713aSLionel Sambuc
2854f4a2713aSLionel Sambuc // Substitute into the template parameters of the variable template partial
2855f4a2713aSLionel Sambuc // specialization.
2856f4a2713aSLionel Sambuc TemplateParameterList *TempParams = PartialSpec->getTemplateParameters();
2857f4a2713aSLionel Sambuc TemplateParameterList *InstParams = SubstTemplateParams(TempParams);
2858f4a2713aSLionel Sambuc if (!InstParams)
2859*0a6a1f1dSLionel Sambuc return nullptr;
2860f4a2713aSLionel Sambuc
2861f4a2713aSLionel Sambuc // Substitute into the template arguments of the variable template partial
2862f4a2713aSLionel Sambuc // specialization.
2863f4a2713aSLionel Sambuc const ASTTemplateArgumentListInfo *TemplArgInfo
2864f4a2713aSLionel Sambuc = PartialSpec->getTemplateArgsAsWritten();
2865f4a2713aSLionel Sambuc TemplateArgumentListInfo InstTemplateArgs(TemplArgInfo->LAngleLoc,
2866f4a2713aSLionel Sambuc TemplArgInfo->RAngleLoc);
2867f4a2713aSLionel Sambuc if (SemaRef.Subst(TemplArgInfo->getTemplateArgs(),
2868f4a2713aSLionel Sambuc TemplArgInfo->NumTemplateArgs,
2869f4a2713aSLionel Sambuc InstTemplateArgs, TemplateArgs))
2870*0a6a1f1dSLionel Sambuc return nullptr;
2871f4a2713aSLionel Sambuc
2872f4a2713aSLionel Sambuc // Check that the template argument list is well-formed for this
2873f4a2713aSLionel Sambuc // class template.
2874f4a2713aSLionel Sambuc SmallVector<TemplateArgument, 4> Converted;
2875f4a2713aSLionel Sambuc if (SemaRef.CheckTemplateArgumentList(VarTemplate, PartialSpec->getLocation(),
2876f4a2713aSLionel Sambuc InstTemplateArgs, false, Converted))
2877*0a6a1f1dSLionel Sambuc return nullptr;
2878f4a2713aSLionel Sambuc
2879f4a2713aSLionel Sambuc // Figure out where to insert this variable template partial specialization
2880f4a2713aSLionel Sambuc // in the member template's set of variable template partial specializations.
2881*0a6a1f1dSLionel Sambuc void *InsertPos = nullptr;
2882f4a2713aSLionel Sambuc VarTemplateSpecializationDecl *PrevDecl =
2883*0a6a1f1dSLionel Sambuc VarTemplate->findPartialSpecialization(Converted, InsertPos);
2884f4a2713aSLionel Sambuc
2885f4a2713aSLionel Sambuc // Build the canonical type that describes the converted template
2886f4a2713aSLionel Sambuc // arguments of the variable template partial specialization.
2887f4a2713aSLionel Sambuc QualType CanonType = SemaRef.Context.getTemplateSpecializationType(
2888f4a2713aSLionel Sambuc TemplateName(VarTemplate), Converted.data(), Converted.size());
2889f4a2713aSLionel Sambuc
2890f4a2713aSLionel Sambuc // Build the fully-sugared type for this variable template
2891f4a2713aSLionel Sambuc // specialization as the user wrote in the specialization
2892f4a2713aSLionel Sambuc // itself. This means that we'll pretty-print the type retrieved
2893f4a2713aSLionel Sambuc // from the specialization's declaration the way that the user
2894f4a2713aSLionel Sambuc // actually wrote the specialization, rather than formatting the
2895f4a2713aSLionel Sambuc // name based on the "canonical" representation used to store the
2896f4a2713aSLionel Sambuc // template arguments in the specialization.
2897f4a2713aSLionel Sambuc TypeSourceInfo *WrittenTy = SemaRef.Context.getTemplateSpecializationTypeInfo(
2898f4a2713aSLionel Sambuc TemplateName(VarTemplate), PartialSpec->getLocation(), InstTemplateArgs,
2899f4a2713aSLionel Sambuc CanonType);
2900f4a2713aSLionel Sambuc
2901f4a2713aSLionel Sambuc if (PrevDecl) {
2902f4a2713aSLionel Sambuc // We've already seen a partial specialization with the same template
2903f4a2713aSLionel Sambuc // parameters and template arguments. This can happen, for example, when
2904f4a2713aSLionel Sambuc // substituting the outer template arguments ends up causing two
2905f4a2713aSLionel Sambuc // variable template partial specializations of a member variable template
2906f4a2713aSLionel Sambuc // to have identical forms, e.g.,
2907f4a2713aSLionel Sambuc //
2908f4a2713aSLionel Sambuc // template<typename T, typename U>
2909f4a2713aSLionel Sambuc // struct Outer {
2910f4a2713aSLionel Sambuc // template<typename X, typename Y> pair<X,Y> p;
2911f4a2713aSLionel Sambuc // template<typename Y> pair<T, Y> p;
2912f4a2713aSLionel Sambuc // template<typename Y> pair<U, Y> p;
2913f4a2713aSLionel Sambuc // };
2914f4a2713aSLionel Sambuc //
2915f4a2713aSLionel Sambuc // Outer<int, int> outer; // error: the partial specializations of Inner
2916f4a2713aSLionel Sambuc // // have the same signature.
2917f4a2713aSLionel Sambuc SemaRef.Diag(PartialSpec->getLocation(),
2918f4a2713aSLionel Sambuc diag::err_var_partial_spec_redeclared)
2919f4a2713aSLionel Sambuc << WrittenTy->getType();
2920f4a2713aSLionel Sambuc SemaRef.Diag(PrevDecl->getLocation(),
2921f4a2713aSLionel Sambuc diag::note_var_prev_partial_spec_here);
2922*0a6a1f1dSLionel Sambuc return nullptr;
2923f4a2713aSLionel Sambuc }
2924f4a2713aSLionel Sambuc
2925f4a2713aSLionel Sambuc // Do substitution on the type of the declaration
2926f4a2713aSLionel Sambuc TypeSourceInfo *DI = SemaRef.SubstType(
2927f4a2713aSLionel Sambuc PartialSpec->getTypeSourceInfo(), TemplateArgs,
2928f4a2713aSLionel Sambuc PartialSpec->getTypeSpecStartLoc(), PartialSpec->getDeclName());
2929f4a2713aSLionel Sambuc if (!DI)
2930*0a6a1f1dSLionel Sambuc return nullptr;
2931f4a2713aSLionel Sambuc
2932f4a2713aSLionel Sambuc if (DI->getType()->isFunctionType()) {
2933f4a2713aSLionel Sambuc SemaRef.Diag(PartialSpec->getLocation(),
2934f4a2713aSLionel Sambuc diag::err_variable_instantiates_to_function)
2935f4a2713aSLionel Sambuc << PartialSpec->isStaticDataMember() << DI->getType();
2936*0a6a1f1dSLionel Sambuc return nullptr;
2937f4a2713aSLionel Sambuc }
2938f4a2713aSLionel Sambuc
2939f4a2713aSLionel Sambuc // Create the variable template partial specialization declaration.
2940f4a2713aSLionel Sambuc VarTemplatePartialSpecializationDecl *InstPartialSpec =
2941f4a2713aSLionel Sambuc VarTemplatePartialSpecializationDecl::Create(
2942f4a2713aSLionel Sambuc SemaRef.Context, Owner, PartialSpec->getInnerLocStart(),
2943f4a2713aSLionel Sambuc PartialSpec->getLocation(), InstParams, VarTemplate, DI->getType(),
2944f4a2713aSLionel Sambuc DI, PartialSpec->getStorageClass(), Converted.data(),
2945f4a2713aSLionel Sambuc Converted.size(), InstTemplateArgs);
2946f4a2713aSLionel Sambuc
2947f4a2713aSLionel Sambuc // Substitute the nested name specifier, if any.
2948f4a2713aSLionel Sambuc if (SubstQualifier(PartialSpec, InstPartialSpec))
2949*0a6a1f1dSLionel Sambuc return nullptr;
2950f4a2713aSLionel Sambuc
2951f4a2713aSLionel Sambuc InstPartialSpec->setInstantiatedFromMember(PartialSpec);
2952f4a2713aSLionel Sambuc InstPartialSpec->setTypeAsWritten(WrittenTy);
2953f4a2713aSLionel Sambuc
2954f4a2713aSLionel Sambuc // Add this partial specialization to the set of variable template partial
2955f4a2713aSLionel Sambuc // specializations. The instantiation of the initializer is not necessary.
2956*0a6a1f1dSLionel Sambuc VarTemplate->AddPartialSpecialization(InstPartialSpec, /*InsertPos=*/nullptr);
2957f4a2713aSLionel Sambuc
2958f4a2713aSLionel Sambuc SemaRef.BuildVariableInstantiation(InstPartialSpec, PartialSpec, TemplateArgs,
2959f4a2713aSLionel Sambuc LateAttrs, Owner, StartingScope);
2960f4a2713aSLionel Sambuc
2961f4a2713aSLionel Sambuc return InstPartialSpec;
2962f4a2713aSLionel Sambuc }
2963f4a2713aSLionel Sambuc
2964f4a2713aSLionel Sambuc TypeSourceInfo*
SubstFunctionType(FunctionDecl * D,SmallVectorImpl<ParmVarDecl * > & Params)2965f4a2713aSLionel Sambuc TemplateDeclInstantiator::SubstFunctionType(FunctionDecl *D,
2966f4a2713aSLionel Sambuc SmallVectorImpl<ParmVarDecl *> &Params) {
2967f4a2713aSLionel Sambuc TypeSourceInfo *OldTInfo = D->getTypeSourceInfo();
2968f4a2713aSLionel Sambuc assert(OldTInfo && "substituting function without type source info");
2969f4a2713aSLionel Sambuc assert(Params.empty() && "parameter vector is non-empty at start");
2970f4a2713aSLionel Sambuc
2971*0a6a1f1dSLionel Sambuc CXXRecordDecl *ThisContext = nullptr;
2972f4a2713aSLionel Sambuc unsigned ThisTypeQuals = 0;
2973f4a2713aSLionel Sambuc if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
2974f4a2713aSLionel Sambuc ThisContext = cast<CXXRecordDecl>(Owner);
2975f4a2713aSLionel Sambuc ThisTypeQuals = Method->getTypeQualifiers();
2976f4a2713aSLionel Sambuc }
2977f4a2713aSLionel Sambuc
2978f4a2713aSLionel Sambuc TypeSourceInfo *NewTInfo
2979f4a2713aSLionel Sambuc = SemaRef.SubstFunctionDeclType(OldTInfo, TemplateArgs,
2980f4a2713aSLionel Sambuc D->getTypeSpecStartLoc(),
2981f4a2713aSLionel Sambuc D->getDeclName(),
2982f4a2713aSLionel Sambuc ThisContext, ThisTypeQuals);
2983f4a2713aSLionel Sambuc if (!NewTInfo)
2984*0a6a1f1dSLionel Sambuc return nullptr;
2985f4a2713aSLionel Sambuc
2986f4a2713aSLionel Sambuc TypeLoc OldTL = OldTInfo->getTypeLoc().IgnoreParens();
2987f4a2713aSLionel Sambuc if (FunctionProtoTypeLoc OldProtoLoc = OldTL.getAs<FunctionProtoTypeLoc>()) {
2988f4a2713aSLionel Sambuc if (NewTInfo != OldTInfo) {
2989f4a2713aSLionel Sambuc // Get parameters from the new type info.
2990f4a2713aSLionel Sambuc TypeLoc NewTL = NewTInfo->getTypeLoc().IgnoreParens();
2991f4a2713aSLionel Sambuc FunctionProtoTypeLoc NewProtoLoc = NewTL.castAs<FunctionProtoTypeLoc>();
2992f4a2713aSLionel Sambuc unsigned NewIdx = 0;
2993*0a6a1f1dSLionel Sambuc for (unsigned OldIdx = 0, NumOldParams = OldProtoLoc.getNumParams();
2994f4a2713aSLionel Sambuc OldIdx != NumOldParams; ++OldIdx) {
2995*0a6a1f1dSLionel Sambuc ParmVarDecl *OldParam = OldProtoLoc.getParam(OldIdx);
2996f4a2713aSLionel Sambuc LocalInstantiationScope *Scope = SemaRef.CurrentInstantiationScope;
2997f4a2713aSLionel Sambuc
2998f4a2713aSLionel Sambuc Optional<unsigned> NumArgumentsInExpansion;
2999f4a2713aSLionel Sambuc if (OldParam->isParameterPack())
3000f4a2713aSLionel Sambuc NumArgumentsInExpansion =
3001f4a2713aSLionel Sambuc SemaRef.getNumArgumentsInExpansion(OldParam->getType(),
3002f4a2713aSLionel Sambuc TemplateArgs);
3003f4a2713aSLionel Sambuc if (!NumArgumentsInExpansion) {
3004f4a2713aSLionel Sambuc // Simple case: normal parameter, or a parameter pack that's
3005f4a2713aSLionel Sambuc // instantiated to a (still-dependent) parameter pack.
3006*0a6a1f1dSLionel Sambuc ParmVarDecl *NewParam = NewProtoLoc.getParam(NewIdx++);
3007f4a2713aSLionel Sambuc Params.push_back(NewParam);
3008f4a2713aSLionel Sambuc Scope->InstantiatedLocal(OldParam, NewParam);
3009f4a2713aSLionel Sambuc } else {
3010f4a2713aSLionel Sambuc // Parameter pack expansion: make the instantiation an argument pack.
3011f4a2713aSLionel Sambuc Scope->MakeInstantiatedLocalArgPack(OldParam);
3012f4a2713aSLionel Sambuc for (unsigned I = 0; I != *NumArgumentsInExpansion; ++I) {
3013*0a6a1f1dSLionel Sambuc ParmVarDecl *NewParam = NewProtoLoc.getParam(NewIdx++);
3014f4a2713aSLionel Sambuc Params.push_back(NewParam);
3015f4a2713aSLionel Sambuc Scope->InstantiatedLocalPackArg(OldParam, NewParam);
3016f4a2713aSLionel Sambuc }
3017f4a2713aSLionel Sambuc }
3018f4a2713aSLionel Sambuc }
3019f4a2713aSLionel Sambuc } else {
3020f4a2713aSLionel Sambuc // The function type itself was not dependent and therefore no
3021f4a2713aSLionel Sambuc // substitution occurred. However, we still need to instantiate
3022f4a2713aSLionel Sambuc // the function parameters themselves.
3023f4a2713aSLionel Sambuc const FunctionProtoType *OldProto =
3024f4a2713aSLionel Sambuc cast<FunctionProtoType>(OldProtoLoc.getType());
3025*0a6a1f1dSLionel Sambuc for (unsigned i = 0, i_end = OldProtoLoc.getNumParams(); i != i_end;
3026*0a6a1f1dSLionel Sambuc ++i) {
3027*0a6a1f1dSLionel Sambuc ParmVarDecl *OldParam = OldProtoLoc.getParam(i);
3028f4a2713aSLionel Sambuc if (!OldParam) {
3029f4a2713aSLionel Sambuc Params.push_back(SemaRef.BuildParmVarDeclForTypedef(
3030*0a6a1f1dSLionel Sambuc D, D->getLocation(), OldProto->getParamType(i)));
3031f4a2713aSLionel Sambuc continue;
3032f4a2713aSLionel Sambuc }
3033f4a2713aSLionel Sambuc
3034f4a2713aSLionel Sambuc ParmVarDecl *Parm =
3035f4a2713aSLionel Sambuc cast_or_null<ParmVarDecl>(VisitParmVarDecl(OldParam));
3036f4a2713aSLionel Sambuc if (!Parm)
3037*0a6a1f1dSLionel Sambuc return nullptr;
3038f4a2713aSLionel Sambuc Params.push_back(Parm);
3039f4a2713aSLionel Sambuc }
3040f4a2713aSLionel Sambuc }
3041f4a2713aSLionel Sambuc } else {
3042f4a2713aSLionel Sambuc // If the type of this function, after ignoring parentheses, is not
3043f4a2713aSLionel Sambuc // *directly* a function type, then we're instantiating a function that
3044f4a2713aSLionel Sambuc // was declared via a typedef or with attributes, e.g.,
3045f4a2713aSLionel Sambuc //
3046f4a2713aSLionel Sambuc // typedef int functype(int, int);
3047f4a2713aSLionel Sambuc // functype func;
3048f4a2713aSLionel Sambuc // int __cdecl meth(int, int);
3049f4a2713aSLionel Sambuc //
3050f4a2713aSLionel Sambuc // In this case, we'll just go instantiate the ParmVarDecls that we
3051f4a2713aSLionel Sambuc // synthesized in the method declaration.
3052f4a2713aSLionel Sambuc SmallVector<QualType, 4> ParamTypes;
3053f4a2713aSLionel Sambuc if (SemaRef.SubstParmTypes(D->getLocation(), D->param_begin(),
3054f4a2713aSLionel Sambuc D->getNumParams(), TemplateArgs, ParamTypes,
3055f4a2713aSLionel Sambuc &Params))
3056*0a6a1f1dSLionel Sambuc return nullptr;
3057f4a2713aSLionel Sambuc }
3058f4a2713aSLionel Sambuc
3059f4a2713aSLionel Sambuc return NewTInfo;
3060f4a2713aSLionel Sambuc }
3061f4a2713aSLionel Sambuc
3062f4a2713aSLionel Sambuc /// Introduce the instantiated function parameters into the local
3063f4a2713aSLionel Sambuc /// instantiation scope, and set the parameter names to those used
3064f4a2713aSLionel Sambuc /// in the template.
addInstantiatedParametersToScope(Sema & S,FunctionDecl * Function,const FunctionDecl * PatternDecl,LocalInstantiationScope & Scope,const MultiLevelTemplateArgumentList & TemplateArgs)3065*0a6a1f1dSLionel Sambuc static bool addInstantiatedParametersToScope(Sema &S, FunctionDecl *Function,
3066f4a2713aSLionel Sambuc const FunctionDecl *PatternDecl,
3067f4a2713aSLionel Sambuc LocalInstantiationScope &Scope,
3068f4a2713aSLionel Sambuc const MultiLevelTemplateArgumentList &TemplateArgs) {
3069f4a2713aSLionel Sambuc unsigned FParamIdx = 0;
3070f4a2713aSLionel Sambuc for (unsigned I = 0, N = PatternDecl->getNumParams(); I != N; ++I) {
3071f4a2713aSLionel Sambuc const ParmVarDecl *PatternParam = PatternDecl->getParamDecl(I);
3072f4a2713aSLionel Sambuc if (!PatternParam->isParameterPack()) {
3073f4a2713aSLionel Sambuc // Simple case: not a parameter pack.
3074f4a2713aSLionel Sambuc assert(FParamIdx < Function->getNumParams());
3075f4a2713aSLionel Sambuc ParmVarDecl *FunctionParam = Function->getParamDecl(FParamIdx);
3076f4a2713aSLionel Sambuc FunctionParam->setDeclName(PatternParam->getDeclName());
3077*0a6a1f1dSLionel Sambuc // If the parameter's type is not dependent, update it to match the type
3078*0a6a1f1dSLionel Sambuc // in the pattern. They can differ in top-level cv-qualifiers, and we want
3079*0a6a1f1dSLionel Sambuc // the pattern's type here. If the type is dependent, they can't differ,
3080*0a6a1f1dSLionel Sambuc // per core issue 1668. Substitute into the type from the pattern, in case
3081*0a6a1f1dSLionel Sambuc // it's instantiation-dependent.
3082*0a6a1f1dSLionel Sambuc // FIXME: Updating the type to work around this is at best fragile.
3083*0a6a1f1dSLionel Sambuc if (!PatternDecl->getType()->isDependentType()) {
3084*0a6a1f1dSLionel Sambuc QualType T = S.SubstType(PatternParam->getType(), TemplateArgs,
3085*0a6a1f1dSLionel Sambuc FunctionParam->getLocation(),
3086*0a6a1f1dSLionel Sambuc FunctionParam->getDeclName());
3087*0a6a1f1dSLionel Sambuc if (T.isNull())
3088*0a6a1f1dSLionel Sambuc return true;
3089*0a6a1f1dSLionel Sambuc FunctionParam->setType(T);
3090*0a6a1f1dSLionel Sambuc }
3091*0a6a1f1dSLionel Sambuc
3092f4a2713aSLionel Sambuc Scope.InstantiatedLocal(PatternParam, FunctionParam);
3093f4a2713aSLionel Sambuc ++FParamIdx;
3094f4a2713aSLionel Sambuc continue;
3095f4a2713aSLionel Sambuc }
3096f4a2713aSLionel Sambuc
3097f4a2713aSLionel Sambuc // Expand the parameter pack.
3098f4a2713aSLionel Sambuc Scope.MakeInstantiatedLocalArgPack(PatternParam);
3099f4a2713aSLionel Sambuc Optional<unsigned> NumArgumentsInExpansion
3100f4a2713aSLionel Sambuc = S.getNumArgumentsInExpansion(PatternParam->getType(), TemplateArgs);
3101f4a2713aSLionel Sambuc assert(NumArgumentsInExpansion &&
3102f4a2713aSLionel Sambuc "should only be called when all template arguments are known");
3103*0a6a1f1dSLionel Sambuc QualType PatternType =
3104*0a6a1f1dSLionel Sambuc PatternParam->getType()->castAs<PackExpansionType>()->getPattern();
3105f4a2713aSLionel Sambuc for (unsigned Arg = 0; Arg < *NumArgumentsInExpansion; ++Arg) {
3106f4a2713aSLionel Sambuc ParmVarDecl *FunctionParam = Function->getParamDecl(FParamIdx);
3107f4a2713aSLionel Sambuc FunctionParam->setDeclName(PatternParam->getDeclName());
3108*0a6a1f1dSLionel Sambuc if (!PatternDecl->getType()->isDependentType()) {
3109*0a6a1f1dSLionel Sambuc Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(S, Arg);
3110*0a6a1f1dSLionel Sambuc QualType T = S.SubstType(PatternType, TemplateArgs,
3111*0a6a1f1dSLionel Sambuc FunctionParam->getLocation(),
3112*0a6a1f1dSLionel Sambuc FunctionParam->getDeclName());
3113*0a6a1f1dSLionel Sambuc if (T.isNull())
3114*0a6a1f1dSLionel Sambuc return true;
3115*0a6a1f1dSLionel Sambuc FunctionParam->setType(T);
3116*0a6a1f1dSLionel Sambuc }
3117*0a6a1f1dSLionel Sambuc
3118f4a2713aSLionel Sambuc Scope.InstantiatedLocalPackArg(PatternParam, FunctionParam);
3119f4a2713aSLionel Sambuc ++FParamIdx;
3120f4a2713aSLionel Sambuc }
3121f4a2713aSLionel Sambuc }
3122f4a2713aSLionel Sambuc
3123*0a6a1f1dSLionel Sambuc return false;
3124f4a2713aSLionel Sambuc }
3125f4a2713aSLionel Sambuc
InstantiateExceptionSpec(SourceLocation PointOfInstantiation,FunctionDecl * Decl)3126f4a2713aSLionel Sambuc void Sema::InstantiateExceptionSpec(SourceLocation PointOfInstantiation,
3127f4a2713aSLionel Sambuc FunctionDecl *Decl) {
3128f4a2713aSLionel Sambuc const FunctionProtoType *Proto = Decl->getType()->castAs<FunctionProtoType>();
3129f4a2713aSLionel Sambuc if (Proto->getExceptionSpecType() != EST_Uninstantiated)
3130f4a2713aSLionel Sambuc return;
3131f4a2713aSLionel Sambuc
3132f4a2713aSLionel Sambuc InstantiatingTemplate Inst(*this, PointOfInstantiation, Decl,
3133f4a2713aSLionel Sambuc InstantiatingTemplate::ExceptionSpecification());
3134f4a2713aSLionel Sambuc if (Inst.isInvalid()) {
3135f4a2713aSLionel Sambuc // We hit the instantiation depth limit. Clear the exception specification
3136f4a2713aSLionel Sambuc // so that our callers don't have to cope with EST_Uninstantiated.
3137*0a6a1f1dSLionel Sambuc UpdateExceptionSpec(Decl, EST_None);
3138f4a2713aSLionel Sambuc return;
3139f4a2713aSLionel Sambuc }
3140f4a2713aSLionel Sambuc
3141f4a2713aSLionel Sambuc // Enter the scope of this instantiation. We don't use
3142f4a2713aSLionel Sambuc // PushDeclContext because we don't have a scope.
3143f4a2713aSLionel Sambuc Sema::ContextRAII savedContext(*this, Decl);
3144f4a2713aSLionel Sambuc LocalInstantiationScope Scope(*this);
3145f4a2713aSLionel Sambuc
3146f4a2713aSLionel Sambuc MultiLevelTemplateArgumentList TemplateArgs =
3147*0a6a1f1dSLionel Sambuc getTemplateInstantiationArgs(Decl, nullptr, /*RelativeToPrimary*/true);
3148f4a2713aSLionel Sambuc
3149f4a2713aSLionel Sambuc FunctionDecl *Template = Proto->getExceptionSpecTemplate();
3150*0a6a1f1dSLionel Sambuc if (addInstantiatedParametersToScope(*this, Decl, Template, Scope,
3151*0a6a1f1dSLionel Sambuc TemplateArgs)) {
3152*0a6a1f1dSLionel Sambuc UpdateExceptionSpec(Decl, EST_None);
3153*0a6a1f1dSLionel Sambuc return;
3154*0a6a1f1dSLionel Sambuc }
3155f4a2713aSLionel Sambuc
3156*0a6a1f1dSLionel Sambuc SubstExceptionSpec(Decl, Template->getType()->castAs<FunctionProtoType>(),
3157f4a2713aSLionel Sambuc TemplateArgs);
3158f4a2713aSLionel Sambuc }
3159f4a2713aSLionel Sambuc
3160f4a2713aSLionel Sambuc /// \brief Initializes the common fields of an instantiation function
3161f4a2713aSLionel Sambuc /// declaration (New) from the corresponding fields of its template (Tmpl).
3162f4a2713aSLionel Sambuc ///
3163f4a2713aSLionel Sambuc /// \returns true if there was an error
3164f4a2713aSLionel Sambuc bool
InitFunctionInstantiation(FunctionDecl * New,FunctionDecl * Tmpl)3165f4a2713aSLionel Sambuc TemplateDeclInstantiator::InitFunctionInstantiation(FunctionDecl *New,
3166f4a2713aSLionel Sambuc FunctionDecl *Tmpl) {
3167f4a2713aSLionel Sambuc if (Tmpl->isDeleted())
3168f4a2713aSLionel Sambuc New->setDeletedAsWritten();
3169f4a2713aSLionel Sambuc
3170*0a6a1f1dSLionel Sambuc // Forward the mangling number from the template to the instantiated decl.
3171*0a6a1f1dSLionel Sambuc SemaRef.Context.setManglingNumber(New,
3172*0a6a1f1dSLionel Sambuc SemaRef.Context.getManglingNumber(Tmpl));
3173*0a6a1f1dSLionel Sambuc
3174f4a2713aSLionel Sambuc // If we are performing substituting explicitly-specified template arguments
3175f4a2713aSLionel Sambuc // or deduced template arguments into a function template and we reach this
3176f4a2713aSLionel Sambuc // point, we are now past the point where SFINAE applies and have committed
3177f4a2713aSLionel Sambuc // to keeping the new function template specialization. We therefore
3178f4a2713aSLionel Sambuc // convert the active template instantiation for the function template
3179f4a2713aSLionel Sambuc // into a template instantiation for this specific function template
3180f4a2713aSLionel Sambuc // specialization, which is not a SFINAE context, so that we diagnose any
3181f4a2713aSLionel Sambuc // further errors in the declaration itself.
3182f4a2713aSLionel Sambuc typedef Sema::ActiveTemplateInstantiation ActiveInstType;
3183f4a2713aSLionel Sambuc ActiveInstType &ActiveInst = SemaRef.ActiveTemplateInstantiations.back();
3184f4a2713aSLionel Sambuc if (ActiveInst.Kind == ActiveInstType::ExplicitTemplateArgumentSubstitution ||
3185f4a2713aSLionel Sambuc ActiveInst.Kind == ActiveInstType::DeducedTemplateArgumentSubstitution) {
3186f4a2713aSLionel Sambuc if (FunctionTemplateDecl *FunTmpl
3187f4a2713aSLionel Sambuc = dyn_cast<FunctionTemplateDecl>(ActiveInst.Entity)) {
3188f4a2713aSLionel Sambuc assert(FunTmpl->getTemplatedDecl() == Tmpl &&
3189f4a2713aSLionel Sambuc "Deduction from the wrong function template?");
3190f4a2713aSLionel Sambuc (void) FunTmpl;
3191f4a2713aSLionel Sambuc ActiveInst.Kind = ActiveInstType::TemplateInstantiation;
3192f4a2713aSLionel Sambuc ActiveInst.Entity = New;
3193f4a2713aSLionel Sambuc }
3194f4a2713aSLionel Sambuc }
3195f4a2713aSLionel Sambuc
3196f4a2713aSLionel Sambuc const FunctionProtoType *Proto = Tmpl->getType()->getAs<FunctionProtoType>();
3197f4a2713aSLionel Sambuc assert(Proto && "Function template without prototype?");
3198f4a2713aSLionel Sambuc
3199f4a2713aSLionel Sambuc if (Proto->hasExceptionSpec() || Proto->getNoReturnAttr()) {
3200f4a2713aSLionel Sambuc FunctionProtoType::ExtProtoInfo EPI = Proto->getExtProtoInfo();
3201f4a2713aSLionel Sambuc
3202f4a2713aSLionel Sambuc // DR1330: In C++11, defer instantiation of a non-trivial
3203f4a2713aSLionel Sambuc // exception specification.
3204f4a2713aSLionel Sambuc if (SemaRef.getLangOpts().CPlusPlus11 &&
3205*0a6a1f1dSLionel Sambuc EPI.ExceptionSpec.Type != EST_None &&
3206*0a6a1f1dSLionel Sambuc EPI.ExceptionSpec.Type != EST_DynamicNone &&
3207*0a6a1f1dSLionel Sambuc EPI.ExceptionSpec.Type != EST_BasicNoexcept) {
3208f4a2713aSLionel Sambuc FunctionDecl *ExceptionSpecTemplate = Tmpl;
3209*0a6a1f1dSLionel Sambuc if (EPI.ExceptionSpec.Type == EST_Uninstantiated)
3210*0a6a1f1dSLionel Sambuc ExceptionSpecTemplate = EPI.ExceptionSpec.SourceTemplate;
3211f4a2713aSLionel Sambuc ExceptionSpecificationType NewEST = EST_Uninstantiated;
3212*0a6a1f1dSLionel Sambuc if (EPI.ExceptionSpec.Type == EST_Unevaluated)
3213f4a2713aSLionel Sambuc NewEST = EST_Unevaluated;
3214f4a2713aSLionel Sambuc
3215f4a2713aSLionel Sambuc // Mark the function has having an uninstantiated exception specification.
3216f4a2713aSLionel Sambuc const FunctionProtoType *NewProto
3217f4a2713aSLionel Sambuc = New->getType()->getAs<FunctionProtoType>();
3218f4a2713aSLionel Sambuc assert(NewProto && "Template instantiation without function prototype?");
3219f4a2713aSLionel Sambuc EPI = NewProto->getExtProtoInfo();
3220*0a6a1f1dSLionel Sambuc EPI.ExceptionSpec.Type = NewEST;
3221*0a6a1f1dSLionel Sambuc EPI.ExceptionSpec.SourceDecl = New;
3222*0a6a1f1dSLionel Sambuc EPI.ExceptionSpec.SourceTemplate = ExceptionSpecTemplate;
3223f4a2713aSLionel Sambuc New->setType(SemaRef.Context.getFunctionType(
3224*0a6a1f1dSLionel Sambuc NewProto->getReturnType(), NewProto->getParamTypes(), EPI));
3225f4a2713aSLionel Sambuc } else {
3226*0a6a1f1dSLionel Sambuc SemaRef.SubstExceptionSpec(New, Proto, TemplateArgs);
3227f4a2713aSLionel Sambuc }
3228f4a2713aSLionel Sambuc }
3229f4a2713aSLionel Sambuc
3230f4a2713aSLionel Sambuc // Get the definition. Leaves the variable unchanged if undefined.
3231f4a2713aSLionel Sambuc const FunctionDecl *Definition = Tmpl;
3232f4a2713aSLionel Sambuc Tmpl->isDefined(Definition);
3233f4a2713aSLionel Sambuc
3234f4a2713aSLionel Sambuc SemaRef.InstantiateAttrs(TemplateArgs, Definition, New,
3235f4a2713aSLionel Sambuc LateAttrs, StartingScope);
3236f4a2713aSLionel Sambuc
3237f4a2713aSLionel Sambuc return false;
3238f4a2713aSLionel Sambuc }
3239f4a2713aSLionel Sambuc
3240f4a2713aSLionel Sambuc /// \brief Initializes common fields of an instantiated method
3241f4a2713aSLionel Sambuc /// declaration (New) from the corresponding fields of its template
3242f4a2713aSLionel Sambuc /// (Tmpl).
3243f4a2713aSLionel Sambuc ///
3244f4a2713aSLionel Sambuc /// \returns true if there was an error
3245f4a2713aSLionel Sambuc bool
InitMethodInstantiation(CXXMethodDecl * New,CXXMethodDecl * Tmpl)3246f4a2713aSLionel Sambuc TemplateDeclInstantiator::InitMethodInstantiation(CXXMethodDecl *New,
3247f4a2713aSLionel Sambuc CXXMethodDecl *Tmpl) {
3248f4a2713aSLionel Sambuc if (InitFunctionInstantiation(New, Tmpl))
3249f4a2713aSLionel Sambuc return true;
3250f4a2713aSLionel Sambuc
3251f4a2713aSLionel Sambuc New->setAccess(Tmpl->getAccess());
3252f4a2713aSLionel Sambuc if (Tmpl->isVirtualAsWritten())
3253f4a2713aSLionel Sambuc New->setVirtualAsWritten(true);
3254f4a2713aSLionel Sambuc
3255f4a2713aSLionel Sambuc // FIXME: New needs a pointer to Tmpl
3256f4a2713aSLionel Sambuc return false;
3257f4a2713aSLionel Sambuc }
3258f4a2713aSLionel Sambuc
3259f4a2713aSLionel Sambuc /// \brief Instantiate the definition of the given function from its
3260f4a2713aSLionel Sambuc /// template.
3261f4a2713aSLionel Sambuc ///
3262f4a2713aSLionel Sambuc /// \param PointOfInstantiation the point at which the instantiation was
3263f4a2713aSLionel Sambuc /// required. Note that this is not precisely a "point of instantiation"
3264f4a2713aSLionel Sambuc /// for the function, but it's close.
3265f4a2713aSLionel Sambuc ///
3266f4a2713aSLionel Sambuc /// \param Function the already-instantiated declaration of a
3267f4a2713aSLionel Sambuc /// function template specialization or member function of a class template
3268f4a2713aSLionel Sambuc /// specialization.
3269f4a2713aSLionel Sambuc ///
3270f4a2713aSLionel Sambuc /// \param Recursive if true, recursively instantiates any functions that
3271f4a2713aSLionel Sambuc /// are required by this instantiation.
3272f4a2713aSLionel Sambuc ///
3273f4a2713aSLionel Sambuc /// \param DefinitionRequired if true, then we are performing an explicit
3274f4a2713aSLionel Sambuc /// instantiation where the body of the function is required. Complain if
3275f4a2713aSLionel Sambuc /// there is no such body.
InstantiateFunctionDefinition(SourceLocation PointOfInstantiation,FunctionDecl * Function,bool Recursive,bool DefinitionRequired)3276f4a2713aSLionel Sambuc void Sema::InstantiateFunctionDefinition(SourceLocation PointOfInstantiation,
3277f4a2713aSLionel Sambuc FunctionDecl *Function,
3278f4a2713aSLionel Sambuc bool Recursive,
3279f4a2713aSLionel Sambuc bool DefinitionRequired) {
3280f4a2713aSLionel Sambuc if (Function->isInvalidDecl() || Function->isDefined())
3281f4a2713aSLionel Sambuc return;
3282f4a2713aSLionel Sambuc
3283f4a2713aSLionel Sambuc // Never instantiate an explicit specialization except if it is a class scope
3284f4a2713aSLionel Sambuc // explicit specialization.
3285f4a2713aSLionel Sambuc if (Function->getTemplateSpecializationKind() == TSK_ExplicitSpecialization &&
3286f4a2713aSLionel Sambuc !Function->getClassScopeSpecializationPattern())
3287f4a2713aSLionel Sambuc return;
3288f4a2713aSLionel Sambuc
3289f4a2713aSLionel Sambuc // Find the function body that we'll be substituting.
3290f4a2713aSLionel Sambuc const FunctionDecl *PatternDecl = Function->getTemplateInstantiationPattern();
3291f4a2713aSLionel Sambuc assert(PatternDecl && "instantiating a non-template");
3292f4a2713aSLionel Sambuc
3293f4a2713aSLionel Sambuc Stmt *Pattern = PatternDecl->getBody(PatternDecl);
3294f4a2713aSLionel Sambuc assert(PatternDecl && "template definition is not a template");
3295f4a2713aSLionel Sambuc if (!Pattern) {
3296f4a2713aSLionel Sambuc // Try to find a defaulted definition
3297f4a2713aSLionel Sambuc PatternDecl->isDefined(PatternDecl);
3298f4a2713aSLionel Sambuc }
3299f4a2713aSLionel Sambuc assert(PatternDecl && "template definition is not a template");
3300f4a2713aSLionel Sambuc
3301f4a2713aSLionel Sambuc // Postpone late parsed template instantiations.
3302f4a2713aSLionel Sambuc if (PatternDecl->isLateTemplateParsed() &&
3303f4a2713aSLionel Sambuc !LateTemplateParser) {
3304f4a2713aSLionel Sambuc PendingInstantiations.push_back(
3305f4a2713aSLionel Sambuc std::make_pair(Function, PointOfInstantiation));
3306f4a2713aSLionel Sambuc return;
3307f4a2713aSLionel Sambuc }
3308f4a2713aSLionel Sambuc
3309*0a6a1f1dSLionel Sambuc // If we're performing recursive template instantiation, create our own
3310*0a6a1f1dSLionel Sambuc // queue of pending implicit instantiations that we will instantiate later,
3311*0a6a1f1dSLionel Sambuc // while we're still within our own instantiation context.
3312*0a6a1f1dSLionel Sambuc // This has to happen before LateTemplateParser below is called, so that
3313*0a6a1f1dSLionel Sambuc // it marks vtables used in late parsed templates as used.
3314*0a6a1f1dSLionel Sambuc SavePendingLocalImplicitInstantiationsRAII
3315*0a6a1f1dSLionel Sambuc SavedPendingLocalImplicitInstantiations(*this);
3316*0a6a1f1dSLionel Sambuc std::unique_ptr<SavePendingInstantiationsAndVTableUsesRAII>
3317*0a6a1f1dSLionel Sambuc SavePendingInstantiationsAndVTableUses;
3318*0a6a1f1dSLionel Sambuc if (Recursive) {
3319*0a6a1f1dSLionel Sambuc SavePendingInstantiationsAndVTableUses.reset(
3320*0a6a1f1dSLionel Sambuc new SavePendingInstantiationsAndVTableUsesRAII(*this));
3321*0a6a1f1dSLionel Sambuc }
3322*0a6a1f1dSLionel Sambuc
3323f4a2713aSLionel Sambuc // Call the LateTemplateParser callback if there is a need to late parse
3324f4a2713aSLionel Sambuc // a templated function definition.
3325f4a2713aSLionel Sambuc if (!Pattern && PatternDecl->isLateTemplateParsed() &&
3326f4a2713aSLionel Sambuc LateTemplateParser) {
3327f4a2713aSLionel Sambuc // FIXME: Optimize to allow individual templates to be deserialized.
3328f4a2713aSLionel Sambuc if (PatternDecl->isFromASTFile())
3329f4a2713aSLionel Sambuc ExternalSource->ReadLateParsedTemplates(LateParsedTemplateMap);
3330f4a2713aSLionel Sambuc
3331f4a2713aSLionel Sambuc LateParsedTemplate *LPT = LateParsedTemplateMap.lookup(PatternDecl);
3332f4a2713aSLionel Sambuc assert(LPT && "missing LateParsedTemplate");
3333f4a2713aSLionel Sambuc LateTemplateParser(OpaqueParser, *LPT);
3334f4a2713aSLionel Sambuc Pattern = PatternDecl->getBody(PatternDecl);
3335f4a2713aSLionel Sambuc }
3336f4a2713aSLionel Sambuc
3337f4a2713aSLionel Sambuc if (!Pattern && !PatternDecl->isDefaulted()) {
3338f4a2713aSLionel Sambuc if (DefinitionRequired) {
3339f4a2713aSLionel Sambuc if (Function->getPrimaryTemplate())
3340f4a2713aSLionel Sambuc Diag(PointOfInstantiation,
3341f4a2713aSLionel Sambuc diag::err_explicit_instantiation_undefined_func_template)
3342f4a2713aSLionel Sambuc << Function->getPrimaryTemplate();
3343f4a2713aSLionel Sambuc else
3344f4a2713aSLionel Sambuc Diag(PointOfInstantiation,
3345f4a2713aSLionel Sambuc diag::err_explicit_instantiation_undefined_member)
3346f4a2713aSLionel Sambuc << 1 << Function->getDeclName() << Function->getDeclContext();
3347f4a2713aSLionel Sambuc
3348f4a2713aSLionel Sambuc if (PatternDecl)
3349f4a2713aSLionel Sambuc Diag(PatternDecl->getLocation(),
3350f4a2713aSLionel Sambuc diag::note_explicit_instantiation_here);
3351f4a2713aSLionel Sambuc Function->setInvalidDecl();
3352f4a2713aSLionel Sambuc } else if (Function->getTemplateSpecializationKind()
3353f4a2713aSLionel Sambuc == TSK_ExplicitInstantiationDefinition) {
3354*0a6a1f1dSLionel Sambuc assert(!Recursive);
3355f4a2713aSLionel Sambuc PendingInstantiations.push_back(
3356f4a2713aSLionel Sambuc std::make_pair(Function, PointOfInstantiation));
3357f4a2713aSLionel Sambuc }
3358f4a2713aSLionel Sambuc
3359f4a2713aSLionel Sambuc return;
3360f4a2713aSLionel Sambuc }
3361f4a2713aSLionel Sambuc
3362f4a2713aSLionel Sambuc // C++1y [temp.explicit]p10:
3363f4a2713aSLionel Sambuc // Except for inline functions, declarations with types deduced from their
3364f4a2713aSLionel Sambuc // initializer or return value, and class template specializations, other
3365f4a2713aSLionel Sambuc // explicit instantiation declarations have the effect of suppressing the
3366f4a2713aSLionel Sambuc // implicit instantiation of the entity to which they refer.
3367*0a6a1f1dSLionel Sambuc if (Function->getTemplateSpecializationKind() ==
3368*0a6a1f1dSLionel Sambuc TSK_ExplicitInstantiationDeclaration &&
3369f4a2713aSLionel Sambuc !PatternDecl->isInlined() &&
3370*0a6a1f1dSLionel Sambuc !PatternDecl->getReturnType()->getContainedAutoType())
3371f4a2713aSLionel Sambuc return;
3372f4a2713aSLionel Sambuc
3373*0a6a1f1dSLionel Sambuc if (PatternDecl->isInlined()) {
3374*0a6a1f1dSLionel Sambuc // Function, and all later redeclarations of it (from imported modules,
3375*0a6a1f1dSLionel Sambuc // for instance), are now implicitly inline.
3376*0a6a1f1dSLionel Sambuc for (auto *D = Function->getMostRecentDecl(); /**/;
3377*0a6a1f1dSLionel Sambuc D = D->getPreviousDecl()) {
3378*0a6a1f1dSLionel Sambuc D->setImplicitlyInline();
3379*0a6a1f1dSLionel Sambuc if (D == Function)
3380*0a6a1f1dSLionel Sambuc break;
3381*0a6a1f1dSLionel Sambuc }
3382*0a6a1f1dSLionel Sambuc }
3383f4a2713aSLionel Sambuc
3384f4a2713aSLionel Sambuc InstantiatingTemplate Inst(*this, PointOfInstantiation, Function);
3385f4a2713aSLionel Sambuc if (Inst.isInvalid())
3386f4a2713aSLionel Sambuc return;
3387f4a2713aSLionel Sambuc
3388f4a2713aSLionel Sambuc // Copy the inner loc start from the pattern.
3389f4a2713aSLionel Sambuc Function->setInnerLocStart(PatternDecl->getInnerLocStart());
3390f4a2713aSLionel Sambuc
3391f4a2713aSLionel Sambuc EnterExpressionEvaluationContext EvalContext(*this,
3392f4a2713aSLionel Sambuc Sema::PotentiallyEvaluated);
3393f4a2713aSLionel Sambuc
3394f4a2713aSLionel Sambuc // Introduce a new scope where local variable instantiations will be
3395f4a2713aSLionel Sambuc // recorded, unless we're actually a member function within a local
3396f4a2713aSLionel Sambuc // class, in which case we need to merge our results with the parent
3397f4a2713aSLionel Sambuc // scope (of the enclosing function).
3398f4a2713aSLionel Sambuc bool MergeWithParentScope = false;
3399f4a2713aSLionel Sambuc if (CXXRecordDecl *Rec = dyn_cast<CXXRecordDecl>(Function->getDeclContext()))
3400f4a2713aSLionel Sambuc MergeWithParentScope = Rec->isLocalClass();
3401f4a2713aSLionel Sambuc
3402f4a2713aSLionel Sambuc LocalInstantiationScope Scope(*this, MergeWithParentScope);
3403f4a2713aSLionel Sambuc
3404f4a2713aSLionel Sambuc if (PatternDecl->isDefaulted())
3405f4a2713aSLionel Sambuc SetDeclDefaulted(Function, PatternDecl->getLocation());
3406f4a2713aSLionel Sambuc else {
3407*0a6a1f1dSLionel Sambuc MultiLevelTemplateArgumentList TemplateArgs =
3408*0a6a1f1dSLionel Sambuc getTemplateInstantiationArgs(Function, nullptr, false, PatternDecl);
3409*0a6a1f1dSLionel Sambuc
3410*0a6a1f1dSLionel Sambuc // Substitute into the qualifier; we can get a substitution failure here
3411*0a6a1f1dSLionel Sambuc // through evil use of alias templates.
3412*0a6a1f1dSLionel Sambuc // FIXME: Is CurContext correct for this? Should we go to the (instantiation
3413*0a6a1f1dSLionel Sambuc // of the) lexical context of the pattern?
3414*0a6a1f1dSLionel Sambuc SubstQualifier(*this, PatternDecl, Function, TemplateArgs);
3415*0a6a1f1dSLionel Sambuc
3416*0a6a1f1dSLionel Sambuc ActOnStartOfFunctionDef(nullptr, Function);
3417f4a2713aSLionel Sambuc
3418f4a2713aSLionel Sambuc // Enter the scope of this instantiation. We don't use
3419f4a2713aSLionel Sambuc // PushDeclContext because we don't have a scope.
3420f4a2713aSLionel Sambuc Sema::ContextRAII savedContext(*this, Function);
3421f4a2713aSLionel Sambuc
3422*0a6a1f1dSLionel Sambuc if (addInstantiatedParametersToScope(*this, Function, PatternDecl, Scope,
3423*0a6a1f1dSLionel Sambuc TemplateArgs))
3424*0a6a1f1dSLionel Sambuc return;
3425f4a2713aSLionel Sambuc
3426f4a2713aSLionel Sambuc // If this is a constructor, instantiate the member initializers.
3427f4a2713aSLionel Sambuc if (const CXXConstructorDecl *Ctor =
3428f4a2713aSLionel Sambuc dyn_cast<CXXConstructorDecl>(PatternDecl)) {
3429f4a2713aSLionel Sambuc InstantiateMemInitializers(cast<CXXConstructorDecl>(Function), Ctor,
3430f4a2713aSLionel Sambuc TemplateArgs);
3431f4a2713aSLionel Sambuc }
3432f4a2713aSLionel Sambuc
3433f4a2713aSLionel Sambuc // Instantiate the function body.
3434f4a2713aSLionel Sambuc StmtResult Body = SubstStmt(Pattern, TemplateArgs);
3435f4a2713aSLionel Sambuc
3436f4a2713aSLionel Sambuc if (Body.isInvalid())
3437f4a2713aSLionel Sambuc Function->setInvalidDecl();
3438f4a2713aSLionel Sambuc
3439f4a2713aSLionel Sambuc ActOnFinishFunctionBody(Function, Body.get(),
3440f4a2713aSLionel Sambuc /*IsInstantiation=*/true);
3441f4a2713aSLionel Sambuc
3442f4a2713aSLionel Sambuc PerformDependentDiagnostics(PatternDecl, TemplateArgs);
3443f4a2713aSLionel Sambuc
3444*0a6a1f1dSLionel Sambuc if (auto *Listener = getASTMutationListener())
3445*0a6a1f1dSLionel Sambuc Listener->FunctionDefinitionInstantiated(Function);
3446*0a6a1f1dSLionel Sambuc
3447f4a2713aSLionel Sambuc savedContext.pop();
3448f4a2713aSLionel Sambuc }
3449f4a2713aSLionel Sambuc
3450f4a2713aSLionel Sambuc DeclGroupRef DG(Function);
3451f4a2713aSLionel Sambuc Consumer.HandleTopLevelDecl(DG);
3452f4a2713aSLionel Sambuc
3453f4a2713aSLionel Sambuc // This class may have local implicit instantiations that need to be
3454f4a2713aSLionel Sambuc // instantiation within this scope.
3455f4a2713aSLionel Sambuc PerformPendingInstantiations(/*LocalOnly=*/true);
3456f4a2713aSLionel Sambuc Scope.Exit();
3457f4a2713aSLionel Sambuc
3458f4a2713aSLionel Sambuc if (Recursive) {
3459f4a2713aSLionel Sambuc // Define any pending vtables.
3460f4a2713aSLionel Sambuc DefineUsedVTables();
3461f4a2713aSLionel Sambuc
3462f4a2713aSLionel Sambuc // Instantiate any pending implicit instantiations found during the
3463f4a2713aSLionel Sambuc // instantiation of this template.
3464f4a2713aSLionel Sambuc PerformPendingInstantiations();
3465f4a2713aSLionel Sambuc
3466*0a6a1f1dSLionel Sambuc // Restore PendingInstantiations and VTableUses.
3467*0a6a1f1dSLionel Sambuc SavePendingInstantiationsAndVTableUses.reset();
3468f4a2713aSLionel Sambuc }
3469f4a2713aSLionel Sambuc }
3470f4a2713aSLionel Sambuc
BuildVarTemplateInstantiation(VarTemplateDecl * VarTemplate,VarDecl * FromVar,const TemplateArgumentList & TemplateArgList,const TemplateArgumentListInfo & TemplateArgsInfo,SmallVectorImpl<TemplateArgument> & Converted,SourceLocation PointOfInstantiation,void * InsertPos,LateInstantiatedAttrVec * LateAttrs,LocalInstantiationScope * StartingScope)3471f4a2713aSLionel Sambuc VarTemplateSpecializationDecl *Sema::BuildVarTemplateInstantiation(
3472f4a2713aSLionel Sambuc VarTemplateDecl *VarTemplate, VarDecl *FromVar,
3473f4a2713aSLionel Sambuc const TemplateArgumentList &TemplateArgList,
3474f4a2713aSLionel Sambuc const TemplateArgumentListInfo &TemplateArgsInfo,
3475f4a2713aSLionel Sambuc SmallVectorImpl<TemplateArgument> &Converted,
3476f4a2713aSLionel Sambuc SourceLocation PointOfInstantiation, void *InsertPos,
3477f4a2713aSLionel Sambuc LateInstantiatedAttrVec *LateAttrs,
3478f4a2713aSLionel Sambuc LocalInstantiationScope *StartingScope) {
3479f4a2713aSLionel Sambuc if (FromVar->isInvalidDecl())
3480*0a6a1f1dSLionel Sambuc return nullptr;
3481f4a2713aSLionel Sambuc
3482f4a2713aSLionel Sambuc InstantiatingTemplate Inst(*this, PointOfInstantiation, FromVar);
3483f4a2713aSLionel Sambuc if (Inst.isInvalid())
3484*0a6a1f1dSLionel Sambuc return nullptr;
3485f4a2713aSLionel Sambuc
3486f4a2713aSLionel Sambuc MultiLevelTemplateArgumentList TemplateArgLists;
3487f4a2713aSLionel Sambuc TemplateArgLists.addOuterTemplateArguments(&TemplateArgList);
3488f4a2713aSLionel Sambuc
3489f4a2713aSLionel Sambuc // Instantiate the first declaration of the variable template: for a partial
3490f4a2713aSLionel Sambuc // specialization of a static data member template, the first declaration may
3491f4a2713aSLionel Sambuc // or may not be the declaration in the class; if it's in the class, we want
3492f4a2713aSLionel Sambuc // to instantiate a member in the class (a declaration), and if it's outside,
3493f4a2713aSLionel Sambuc // we want to instantiate a definition.
3494*0a6a1f1dSLionel Sambuc //
3495*0a6a1f1dSLionel Sambuc // If we're instantiating an explicitly-specialized member template or member
3496*0a6a1f1dSLionel Sambuc // partial specialization, don't do this. The member specialization completely
3497*0a6a1f1dSLionel Sambuc // replaces the original declaration in this case.
3498*0a6a1f1dSLionel Sambuc bool IsMemberSpec = false;
3499*0a6a1f1dSLionel Sambuc if (VarTemplatePartialSpecializationDecl *PartialSpec =
3500*0a6a1f1dSLionel Sambuc dyn_cast<VarTemplatePartialSpecializationDecl>(FromVar))
3501*0a6a1f1dSLionel Sambuc IsMemberSpec = PartialSpec->isMemberSpecialization();
3502*0a6a1f1dSLionel Sambuc else if (VarTemplateDecl *FromTemplate = FromVar->getDescribedVarTemplate())
3503*0a6a1f1dSLionel Sambuc IsMemberSpec = FromTemplate->isMemberSpecialization();
3504*0a6a1f1dSLionel Sambuc if (!IsMemberSpec)
3505f4a2713aSLionel Sambuc FromVar = FromVar->getFirstDecl();
3506f4a2713aSLionel Sambuc
3507f4a2713aSLionel Sambuc MultiLevelTemplateArgumentList MultiLevelList(TemplateArgList);
3508f4a2713aSLionel Sambuc TemplateDeclInstantiator Instantiator(*this, FromVar->getDeclContext(),
3509f4a2713aSLionel Sambuc MultiLevelList);
3510f4a2713aSLionel Sambuc
3511f4a2713aSLionel Sambuc // TODO: Set LateAttrs and StartingScope ...
3512f4a2713aSLionel Sambuc
3513f4a2713aSLionel Sambuc return cast_or_null<VarTemplateSpecializationDecl>(
3514f4a2713aSLionel Sambuc Instantiator.VisitVarTemplateSpecializationDecl(
3515f4a2713aSLionel Sambuc VarTemplate, FromVar, InsertPos, TemplateArgsInfo, Converted));
3516f4a2713aSLionel Sambuc }
3517f4a2713aSLionel Sambuc
3518f4a2713aSLionel Sambuc /// \brief Instantiates a variable template specialization by completing it
3519f4a2713aSLionel Sambuc /// with appropriate type information and initializer.
CompleteVarTemplateSpecializationDecl(VarTemplateSpecializationDecl * VarSpec,VarDecl * PatternDecl,const MultiLevelTemplateArgumentList & TemplateArgs)3520f4a2713aSLionel Sambuc VarTemplateSpecializationDecl *Sema::CompleteVarTemplateSpecializationDecl(
3521f4a2713aSLionel Sambuc VarTemplateSpecializationDecl *VarSpec, VarDecl *PatternDecl,
3522f4a2713aSLionel Sambuc const MultiLevelTemplateArgumentList &TemplateArgs) {
3523f4a2713aSLionel Sambuc
3524f4a2713aSLionel Sambuc // Do substitution on the type of the declaration
3525f4a2713aSLionel Sambuc TypeSourceInfo *DI =
3526f4a2713aSLionel Sambuc SubstType(PatternDecl->getTypeSourceInfo(), TemplateArgs,
3527f4a2713aSLionel Sambuc PatternDecl->getTypeSpecStartLoc(), PatternDecl->getDeclName());
3528f4a2713aSLionel Sambuc if (!DI)
3529*0a6a1f1dSLionel Sambuc return nullptr;
3530f4a2713aSLionel Sambuc
3531f4a2713aSLionel Sambuc // Update the type of this variable template specialization.
3532f4a2713aSLionel Sambuc VarSpec->setType(DI->getType());
3533f4a2713aSLionel Sambuc
3534f4a2713aSLionel Sambuc // Instantiate the initializer.
3535f4a2713aSLionel Sambuc InstantiateVariableInitializer(VarSpec, PatternDecl, TemplateArgs);
3536f4a2713aSLionel Sambuc
3537f4a2713aSLionel Sambuc return VarSpec;
3538f4a2713aSLionel Sambuc }
3539f4a2713aSLionel Sambuc
3540f4a2713aSLionel Sambuc /// BuildVariableInstantiation - Used after a new variable has been created.
3541f4a2713aSLionel Sambuc /// Sets basic variable data and decides whether to postpone the
3542f4a2713aSLionel Sambuc /// variable instantiation.
BuildVariableInstantiation(VarDecl * NewVar,VarDecl * OldVar,const MultiLevelTemplateArgumentList & TemplateArgs,LateInstantiatedAttrVec * LateAttrs,DeclContext * Owner,LocalInstantiationScope * StartingScope,bool InstantiatingVarTemplate)3543f4a2713aSLionel Sambuc void Sema::BuildVariableInstantiation(
3544f4a2713aSLionel Sambuc VarDecl *NewVar, VarDecl *OldVar,
3545f4a2713aSLionel Sambuc const MultiLevelTemplateArgumentList &TemplateArgs,
3546f4a2713aSLionel Sambuc LateInstantiatedAttrVec *LateAttrs, DeclContext *Owner,
3547f4a2713aSLionel Sambuc LocalInstantiationScope *StartingScope,
3548f4a2713aSLionel Sambuc bool InstantiatingVarTemplate) {
3549f4a2713aSLionel Sambuc
3550f4a2713aSLionel Sambuc // If we are instantiating a local extern declaration, the
3551f4a2713aSLionel Sambuc // instantiation belongs lexically to the containing function.
3552f4a2713aSLionel Sambuc // If we are instantiating a static data member defined
3553f4a2713aSLionel Sambuc // out-of-line, the instantiation will have the same lexical
3554f4a2713aSLionel Sambuc // context (which will be a namespace scope) as the template.
3555f4a2713aSLionel Sambuc if (OldVar->isLocalExternDecl()) {
3556f4a2713aSLionel Sambuc NewVar->setLocalExternDecl();
3557f4a2713aSLionel Sambuc NewVar->setLexicalDeclContext(Owner);
3558f4a2713aSLionel Sambuc } else if (OldVar->isOutOfLine())
3559f4a2713aSLionel Sambuc NewVar->setLexicalDeclContext(OldVar->getLexicalDeclContext());
3560f4a2713aSLionel Sambuc NewVar->setTSCSpec(OldVar->getTSCSpec());
3561f4a2713aSLionel Sambuc NewVar->setInitStyle(OldVar->getInitStyle());
3562f4a2713aSLionel Sambuc NewVar->setCXXForRangeDecl(OldVar->isCXXForRangeDecl());
3563f4a2713aSLionel Sambuc NewVar->setConstexpr(OldVar->isConstexpr());
3564f4a2713aSLionel Sambuc NewVar->setInitCapture(OldVar->isInitCapture());
3565f4a2713aSLionel Sambuc NewVar->setPreviousDeclInSameBlockScope(
3566f4a2713aSLionel Sambuc OldVar->isPreviousDeclInSameBlockScope());
3567f4a2713aSLionel Sambuc NewVar->setAccess(OldVar->getAccess());
3568f4a2713aSLionel Sambuc
3569f4a2713aSLionel Sambuc if (!OldVar->isStaticDataMember()) {
3570f4a2713aSLionel Sambuc if (OldVar->isUsed(false))
3571f4a2713aSLionel Sambuc NewVar->setIsUsed();
3572f4a2713aSLionel Sambuc NewVar->setReferenced(OldVar->isReferenced());
3573f4a2713aSLionel Sambuc }
3574f4a2713aSLionel Sambuc
3575f4a2713aSLionel Sambuc // See if the old variable had a type-specifier that defined an anonymous tag.
3576f4a2713aSLionel Sambuc // If it did, mark the new variable as being the declarator for the new
3577f4a2713aSLionel Sambuc // anonymous tag.
3578f4a2713aSLionel Sambuc if (const TagType *OldTagType = OldVar->getType()->getAs<TagType>()) {
3579f4a2713aSLionel Sambuc TagDecl *OldTag = OldTagType->getDecl();
3580f4a2713aSLionel Sambuc if (OldTag->getDeclaratorForAnonDecl() == OldVar) {
3581f4a2713aSLionel Sambuc TagDecl *NewTag = NewVar->getType()->castAs<TagType>()->getDecl();
3582f4a2713aSLionel Sambuc assert(!NewTag->hasNameForLinkage() &&
3583f4a2713aSLionel Sambuc !NewTag->hasDeclaratorForAnonDecl());
3584f4a2713aSLionel Sambuc NewTag->setDeclaratorForAnonDecl(NewVar);
3585f4a2713aSLionel Sambuc }
3586f4a2713aSLionel Sambuc }
3587f4a2713aSLionel Sambuc
3588f4a2713aSLionel Sambuc InstantiateAttrs(TemplateArgs, OldVar, NewVar, LateAttrs, StartingScope);
3589f4a2713aSLionel Sambuc
3590f4a2713aSLionel Sambuc LookupResult Previous(
3591f4a2713aSLionel Sambuc *this, NewVar->getDeclName(), NewVar->getLocation(),
3592f4a2713aSLionel Sambuc NewVar->isLocalExternDecl() ? Sema::LookupRedeclarationWithLinkage
3593f4a2713aSLionel Sambuc : Sema::LookupOrdinaryName,
3594f4a2713aSLionel Sambuc Sema::ForRedeclaration);
3595f4a2713aSLionel Sambuc
3596*0a6a1f1dSLionel Sambuc if (NewVar->isLocalExternDecl() && OldVar->getPreviousDecl() &&
3597*0a6a1f1dSLionel Sambuc (!OldVar->getPreviousDecl()->getDeclContext()->isDependentContext() ||
3598*0a6a1f1dSLionel Sambuc OldVar->getPreviousDecl()->getDeclContext()==OldVar->getDeclContext())) {
3599f4a2713aSLionel Sambuc // We have a previous declaration. Use that one, so we merge with the
3600f4a2713aSLionel Sambuc // right type.
3601f4a2713aSLionel Sambuc if (NamedDecl *NewPrev = FindInstantiatedDecl(
3602f4a2713aSLionel Sambuc NewVar->getLocation(), OldVar->getPreviousDecl(), TemplateArgs))
3603f4a2713aSLionel Sambuc Previous.addDecl(NewPrev);
3604f4a2713aSLionel Sambuc } else if (!isa<VarTemplateSpecializationDecl>(NewVar) &&
3605f4a2713aSLionel Sambuc OldVar->hasLinkage())
3606f4a2713aSLionel Sambuc LookupQualifiedName(Previous, NewVar->getDeclContext(), false);
3607f4a2713aSLionel Sambuc CheckVariableDeclaration(NewVar, Previous);
3608f4a2713aSLionel Sambuc
3609f4a2713aSLionel Sambuc if (!InstantiatingVarTemplate) {
3610f4a2713aSLionel Sambuc NewVar->getLexicalDeclContext()->addHiddenDecl(NewVar);
3611f4a2713aSLionel Sambuc if (!NewVar->isLocalExternDecl() || !NewVar->getPreviousDecl())
3612f4a2713aSLionel Sambuc NewVar->getDeclContext()->makeDeclVisibleInContext(NewVar);
3613f4a2713aSLionel Sambuc }
3614f4a2713aSLionel Sambuc
3615f4a2713aSLionel Sambuc if (!OldVar->isOutOfLine()) {
3616f4a2713aSLionel Sambuc if (NewVar->getDeclContext()->isFunctionOrMethod())
3617f4a2713aSLionel Sambuc CurrentInstantiationScope->InstantiatedLocal(OldVar, NewVar);
3618f4a2713aSLionel Sambuc }
3619f4a2713aSLionel Sambuc
3620f4a2713aSLionel Sambuc // Link instantiations of static data members back to the template from
3621f4a2713aSLionel Sambuc // which they were instantiated.
3622f4a2713aSLionel Sambuc if (NewVar->isStaticDataMember() && !InstantiatingVarTemplate)
3623f4a2713aSLionel Sambuc NewVar->setInstantiationOfStaticDataMember(OldVar,
3624f4a2713aSLionel Sambuc TSK_ImplicitInstantiation);
3625f4a2713aSLionel Sambuc
3626*0a6a1f1dSLionel Sambuc // Forward the mangling number from the template to the instantiated decl.
3627*0a6a1f1dSLionel Sambuc Context.setManglingNumber(NewVar, Context.getManglingNumber(OldVar));
3628*0a6a1f1dSLionel Sambuc Context.setStaticLocalNumber(NewVar, Context.getStaticLocalNumber(OldVar));
3629*0a6a1f1dSLionel Sambuc
3630f4a2713aSLionel Sambuc // Delay instantiation of the initializer for variable templates until a
3631*0a6a1f1dSLionel Sambuc // definition of the variable is needed. We need it right away if the type
3632*0a6a1f1dSLionel Sambuc // contains 'auto'.
3633*0a6a1f1dSLionel Sambuc if ((!isa<VarTemplateSpecializationDecl>(NewVar) &&
3634*0a6a1f1dSLionel Sambuc !InstantiatingVarTemplate) ||
3635*0a6a1f1dSLionel Sambuc NewVar->getType()->isUndeducedType())
3636f4a2713aSLionel Sambuc InstantiateVariableInitializer(NewVar, OldVar, TemplateArgs);
3637f4a2713aSLionel Sambuc
3638f4a2713aSLionel Sambuc // Diagnose unused local variables with dependent types, where the diagnostic
3639f4a2713aSLionel Sambuc // will have been deferred.
3640f4a2713aSLionel Sambuc if (!NewVar->isInvalidDecl() &&
3641*0a6a1f1dSLionel Sambuc NewVar->getDeclContext()->isFunctionOrMethod() &&
3642f4a2713aSLionel Sambuc OldVar->getType()->isDependentType())
3643f4a2713aSLionel Sambuc DiagnoseUnusedDecl(NewVar);
3644f4a2713aSLionel Sambuc }
3645f4a2713aSLionel Sambuc
3646f4a2713aSLionel Sambuc /// \brief Instantiate the initializer of a variable.
InstantiateVariableInitializer(VarDecl * Var,VarDecl * OldVar,const MultiLevelTemplateArgumentList & TemplateArgs)3647f4a2713aSLionel Sambuc void Sema::InstantiateVariableInitializer(
3648f4a2713aSLionel Sambuc VarDecl *Var, VarDecl *OldVar,
3649f4a2713aSLionel Sambuc const MultiLevelTemplateArgumentList &TemplateArgs) {
3650f4a2713aSLionel Sambuc
3651f4a2713aSLionel Sambuc if (Var->getAnyInitializer())
3652f4a2713aSLionel Sambuc // We already have an initializer in the class.
3653f4a2713aSLionel Sambuc return;
3654f4a2713aSLionel Sambuc
3655f4a2713aSLionel Sambuc if (OldVar->getInit()) {
3656f4a2713aSLionel Sambuc if (Var->isStaticDataMember() && !OldVar->isOutOfLine())
3657f4a2713aSLionel Sambuc PushExpressionEvaluationContext(Sema::ConstantEvaluated, OldVar);
3658f4a2713aSLionel Sambuc else
3659f4a2713aSLionel Sambuc PushExpressionEvaluationContext(Sema::PotentiallyEvaluated, OldVar);
3660f4a2713aSLionel Sambuc
3661f4a2713aSLionel Sambuc // Instantiate the initializer.
3662f4a2713aSLionel Sambuc ExprResult Init =
3663f4a2713aSLionel Sambuc SubstInitializer(OldVar->getInit(), TemplateArgs,
3664f4a2713aSLionel Sambuc OldVar->getInitStyle() == VarDecl::CallInit);
3665f4a2713aSLionel Sambuc if (!Init.isInvalid()) {
3666f4a2713aSLionel Sambuc bool TypeMayContainAuto = true;
3667*0a6a1f1dSLionel Sambuc Expr *InitExpr = Init.get();
3668*0a6a1f1dSLionel Sambuc
3669*0a6a1f1dSLionel Sambuc if (Var->hasAttr<DLLImportAttr>() &&
3670*0a6a1f1dSLionel Sambuc (!InitExpr ||
3671*0a6a1f1dSLionel Sambuc !InitExpr->isConstantInitializer(getASTContext(), false))) {
3672*0a6a1f1dSLionel Sambuc // Do not dynamically initialize dllimport variables.
3673*0a6a1f1dSLionel Sambuc } else if (InitExpr) {
3674f4a2713aSLionel Sambuc bool DirectInit = OldVar->isDirectInit();
3675*0a6a1f1dSLionel Sambuc AddInitializerToDecl(Var, InitExpr, DirectInit, TypeMayContainAuto);
3676f4a2713aSLionel Sambuc } else
3677f4a2713aSLionel Sambuc ActOnUninitializedDecl(Var, TypeMayContainAuto);
3678f4a2713aSLionel Sambuc } else {
3679f4a2713aSLionel Sambuc // FIXME: Not too happy about invalidating the declaration
3680f4a2713aSLionel Sambuc // because of a bogus initializer.
3681f4a2713aSLionel Sambuc Var->setInvalidDecl();
3682f4a2713aSLionel Sambuc }
3683f4a2713aSLionel Sambuc
3684f4a2713aSLionel Sambuc PopExpressionEvaluationContext();
3685f4a2713aSLionel Sambuc } else if ((!Var->isStaticDataMember() || Var->isOutOfLine()) &&
3686f4a2713aSLionel Sambuc !Var->isCXXForRangeDecl())
3687f4a2713aSLionel Sambuc ActOnUninitializedDecl(Var, false);
3688f4a2713aSLionel Sambuc }
3689f4a2713aSLionel Sambuc
3690f4a2713aSLionel Sambuc /// \brief Instantiate the definition of the given variable from its
3691f4a2713aSLionel Sambuc /// template.
3692f4a2713aSLionel Sambuc ///
3693f4a2713aSLionel Sambuc /// \param PointOfInstantiation the point at which the instantiation was
3694f4a2713aSLionel Sambuc /// required. Note that this is not precisely a "point of instantiation"
3695f4a2713aSLionel Sambuc /// for the function, but it's close.
3696f4a2713aSLionel Sambuc ///
3697f4a2713aSLionel Sambuc /// \param Var the already-instantiated declaration of a static member
3698f4a2713aSLionel Sambuc /// variable of a class template specialization.
3699f4a2713aSLionel Sambuc ///
3700f4a2713aSLionel Sambuc /// \param Recursive if true, recursively instantiates any functions that
3701f4a2713aSLionel Sambuc /// are required by this instantiation.
3702f4a2713aSLionel Sambuc ///
3703f4a2713aSLionel Sambuc /// \param DefinitionRequired if true, then we are performing an explicit
3704f4a2713aSLionel Sambuc /// instantiation where an out-of-line definition of the member variable
3705f4a2713aSLionel Sambuc /// is required. Complain if there is no such definition.
InstantiateStaticDataMemberDefinition(SourceLocation PointOfInstantiation,VarDecl * Var,bool Recursive,bool DefinitionRequired)3706f4a2713aSLionel Sambuc void Sema::InstantiateStaticDataMemberDefinition(
3707f4a2713aSLionel Sambuc SourceLocation PointOfInstantiation,
3708f4a2713aSLionel Sambuc VarDecl *Var,
3709f4a2713aSLionel Sambuc bool Recursive,
3710f4a2713aSLionel Sambuc bool DefinitionRequired) {
3711f4a2713aSLionel Sambuc InstantiateVariableDefinition(PointOfInstantiation, Var, Recursive,
3712f4a2713aSLionel Sambuc DefinitionRequired);
3713f4a2713aSLionel Sambuc }
3714f4a2713aSLionel Sambuc
InstantiateVariableDefinition(SourceLocation PointOfInstantiation,VarDecl * Var,bool Recursive,bool DefinitionRequired)3715f4a2713aSLionel Sambuc void Sema::InstantiateVariableDefinition(SourceLocation PointOfInstantiation,
3716f4a2713aSLionel Sambuc VarDecl *Var, bool Recursive,
3717f4a2713aSLionel Sambuc bool DefinitionRequired) {
3718f4a2713aSLionel Sambuc if (Var->isInvalidDecl())
3719f4a2713aSLionel Sambuc return;
3720f4a2713aSLionel Sambuc
3721f4a2713aSLionel Sambuc VarTemplateSpecializationDecl *VarSpec =
3722f4a2713aSLionel Sambuc dyn_cast<VarTemplateSpecializationDecl>(Var);
3723*0a6a1f1dSLionel Sambuc VarDecl *PatternDecl = nullptr, *Def = nullptr;
3724f4a2713aSLionel Sambuc MultiLevelTemplateArgumentList TemplateArgs =
3725f4a2713aSLionel Sambuc getTemplateInstantiationArgs(Var);
3726f4a2713aSLionel Sambuc
3727f4a2713aSLionel Sambuc if (VarSpec) {
3728f4a2713aSLionel Sambuc // If this is a variable template specialization, make sure that it is
3729f4a2713aSLionel Sambuc // non-dependent, then find its instantiation pattern.
3730f4a2713aSLionel Sambuc bool InstantiationDependent = false;
3731f4a2713aSLionel Sambuc assert(!TemplateSpecializationType::anyDependentTemplateArguments(
3732f4a2713aSLionel Sambuc VarSpec->getTemplateArgsInfo(), InstantiationDependent) &&
3733f4a2713aSLionel Sambuc "Only instantiate variable template specializations that are "
3734f4a2713aSLionel Sambuc "not type-dependent");
3735f4a2713aSLionel Sambuc (void)InstantiationDependent;
3736f4a2713aSLionel Sambuc
3737f4a2713aSLionel Sambuc // Find the variable initialization that we'll be substituting. If the
3738f4a2713aSLionel Sambuc // pattern was instantiated from a member template, look back further to
3739f4a2713aSLionel Sambuc // find the real pattern.
3740f4a2713aSLionel Sambuc assert(VarSpec->getSpecializedTemplate() &&
3741f4a2713aSLionel Sambuc "Specialization without specialized template?");
3742f4a2713aSLionel Sambuc llvm::PointerUnion<VarTemplateDecl *,
3743f4a2713aSLionel Sambuc VarTemplatePartialSpecializationDecl *> PatternPtr =
3744f4a2713aSLionel Sambuc VarSpec->getSpecializedTemplateOrPartial();
3745f4a2713aSLionel Sambuc if (PatternPtr.is<VarTemplatePartialSpecializationDecl *>()) {
3746f4a2713aSLionel Sambuc VarTemplatePartialSpecializationDecl *Tmpl =
3747f4a2713aSLionel Sambuc PatternPtr.get<VarTemplatePartialSpecializationDecl *>();
3748f4a2713aSLionel Sambuc while (VarTemplatePartialSpecializationDecl *From =
3749f4a2713aSLionel Sambuc Tmpl->getInstantiatedFromMember()) {
3750f4a2713aSLionel Sambuc if (Tmpl->isMemberSpecialization())
3751f4a2713aSLionel Sambuc break;
3752f4a2713aSLionel Sambuc
3753f4a2713aSLionel Sambuc Tmpl = From;
3754f4a2713aSLionel Sambuc }
3755f4a2713aSLionel Sambuc PatternDecl = Tmpl;
3756f4a2713aSLionel Sambuc } else {
3757f4a2713aSLionel Sambuc VarTemplateDecl *Tmpl = PatternPtr.get<VarTemplateDecl *>();
3758f4a2713aSLionel Sambuc while (VarTemplateDecl *From =
3759f4a2713aSLionel Sambuc Tmpl->getInstantiatedFromMemberTemplate()) {
3760f4a2713aSLionel Sambuc if (Tmpl->isMemberSpecialization())
3761f4a2713aSLionel Sambuc break;
3762f4a2713aSLionel Sambuc
3763f4a2713aSLionel Sambuc Tmpl = From;
3764f4a2713aSLionel Sambuc }
3765f4a2713aSLionel Sambuc PatternDecl = Tmpl->getTemplatedDecl();
3766f4a2713aSLionel Sambuc }
3767f4a2713aSLionel Sambuc
3768f4a2713aSLionel Sambuc // If this is a static data member template, there might be an
3769f4a2713aSLionel Sambuc // uninstantiated initializer on the declaration. If so, instantiate
3770f4a2713aSLionel Sambuc // it now.
3771f4a2713aSLionel Sambuc if (PatternDecl->isStaticDataMember() &&
3772f4a2713aSLionel Sambuc (PatternDecl = PatternDecl->getFirstDecl())->hasInit() &&
3773f4a2713aSLionel Sambuc !Var->hasInit()) {
3774f4a2713aSLionel Sambuc // FIXME: Factor out the duplicated instantiation context setup/tear down
3775f4a2713aSLionel Sambuc // code here.
3776f4a2713aSLionel Sambuc InstantiatingTemplate Inst(*this, PointOfInstantiation, Var);
3777f4a2713aSLionel Sambuc if (Inst.isInvalid())
3778f4a2713aSLionel Sambuc return;
3779f4a2713aSLionel Sambuc
3780f4a2713aSLionel Sambuc // If we're performing recursive template instantiation, create our own
3781f4a2713aSLionel Sambuc // queue of pending implicit instantiations that we will instantiate
3782f4a2713aSLionel Sambuc // later, while we're still within our own instantiation context.
3783*0a6a1f1dSLionel Sambuc std::unique_ptr<SavePendingInstantiationsAndVTableUsesRAII>
3784*0a6a1f1dSLionel Sambuc SavePendingInstantiationsAndVTableUses;
3785f4a2713aSLionel Sambuc if (Recursive) {
3786*0a6a1f1dSLionel Sambuc SavePendingInstantiationsAndVTableUses.reset(
3787*0a6a1f1dSLionel Sambuc new SavePendingInstantiationsAndVTableUsesRAII(*this));
3788f4a2713aSLionel Sambuc }
3789f4a2713aSLionel Sambuc
3790f4a2713aSLionel Sambuc LocalInstantiationScope Local(*this);
3791f4a2713aSLionel Sambuc
3792f4a2713aSLionel Sambuc // Enter the scope of this instantiation. We don't use
3793f4a2713aSLionel Sambuc // PushDeclContext because we don't have a scope.
3794f4a2713aSLionel Sambuc ContextRAII PreviousContext(*this, Var->getDeclContext());
3795f4a2713aSLionel Sambuc InstantiateVariableInitializer(Var, PatternDecl, TemplateArgs);
3796f4a2713aSLionel Sambuc PreviousContext.pop();
3797f4a2713aSLionel Sambuc
3798f4a2713aSLionel Sambuc // FIXME: Need to inform the ASTConsumer that we instantiated the
3799f4a2713aSLionel Sambuc // initializer?
3800f4a2713aSLionel Sambuc
3801f4a2713aSLionel Sambuc // This variable may have local implicit instantiations that need to be
3802f4a2713aSLionel Sambuc // instantiated within this scope.
3803f4a2713aSLionel Sambuc PerformPendingInstantiations(/*LocalOnly=*/true);
3804f4a2713aSLionel Sambuc
3805f4a2713aSLionel Sambuc Local.Exit();
3806f4a2713aSLionel Sambuc
3807f4a2713aSLionel Sambuc if (Recursive) {
3808f4a2713aSLionel Sambuc // Define any newly required vtables.
3809f4a2713aSLionel Sambuc DefineUsedVTables();
3810f4a2713aSLionel Sambuc
3811f4a2713aSLionel Sambuc // Instantiate any pending implicit instantiations found during the
3812f4a2713aSLionel Sambuc // instantiation of this template.
3813f4a2713aSLionel Sambuc PerformPendingInstantiations();
3814f4a2713aSLionel Sambuc
3815*0a6a1f1dSLionel Sambuc // Restore PendingInstantiations and VTableUses.
3816*0a6a1f1dSLionel Sambuc SavePendingInstantiationsAndVTableUses.reset();
3817f4a2713aSLionel Sambuc }
3818f4a2713aSLionel Sambuc }
3819f4a2713aSLionel Sambuc
3820f4a2713aSLionel Sambuc // Find actual definition
3821f4a2713aSLionel Sambuc Def = PatternDecl->getDefinition(getASTContext());
3822f4a2713aSLionel Sambuc } else {
3823f4a2713aSLionel Sambuc // If this is a static data member, find its out-of-line definition.
3824f4a2713aSLionel Sambuc assert(Var->isStaticDataMember() && "not a static data member?");
3825f4a2713aSLionel Sambuc PatternDecl = Var->getInstantiatedFromStaticDataMember();
3826f4a2713aSLionel Sambuc
3827f4a2713aSLionel Sambuc assert(PatternDecl && "data member was not instantiated from a template?");
3828f4a2713aSLionel Sambuc assert(PatternDecl->isStaticDataMember() && "not a static data member?");
3829f4a2713aSLionel Sambuc Def = PatternDecl->getOutOfLineDefinition();
3830f4a2713aSLionel Sambuc }
3831f4a2713aSLionel Sambuc
3832f4a2713aSLionel Sambuc // If we don't have a definition of the variable template, we won't perform
3833f4a2713aSLionel Sambuc // any instantiation. Rather, we rely on the user to instantiate this
3834f4a2713aSLionel Sambuc // definition (or provide a specialization for it) in another translation
3835f4a2713aSLionel Sambuc // unit.
3836f4a2713aSLionel Sambuc if (!Def) {
3837f4a2713aSLionel Sambuc if (DefinitionRequired) {
3838f4a2713aSLionel Sambuc if (VarSpec)
3839f4a2713aSLionel Sambuc Diag(PointOfInstantiation,
3840f4a2713aSLionel Sambuc diag::err_explicit_instantiation_undefined_var_template) << Var;
3841f4a2713aSLionel Sambuc else
3842f4a2713aSLionel Sambuc Diag(PointOfInstantiation,
3843f4a2713aSLionel Sambuc diag::err_explicit_instantiation_undefined_member)
3844f4a2713aSLionel Sambuc << 2 << Var->getDeclName() << Var->getDeclContext();
3845f4a2713aSLionel Sambuc Diag(PatternDecl->getLocation(),
3846f4a2713aSLionel Sambuc diag::note_explicit_instantiation_here);
3847f4a2713aSLionel Sambuc if (VarSpec)
3848f4a2713aSLionel Sambuc Var->setInvalidDecl();
3849f4a2713aSLionel Sambuc } else if (Var->getTemplateSpecializationKind()
3850f4a2713aSLionel Sambuc == TSK_ExplicitInstantiationDefinition) {
3851f4a2713aSLionel Sambuc PendingInstantiations.push_back(
3852f4a2713aSLionel Sambuc std::make_pair(Var, PointOfInstantiation));
3853f4a2713aSLionel Sambuc }
3854f4a2713aSLionel Sambuc
3855f4a2713aSLionel Sambuc return;
3856f4a2713aSLionel Sambuc }
3857f4a2713aSLionel Sambuc
3858f4a2713aSLionel Sambuc TemplateSpecializationKind TSK = Var->getTemplateSpecializationKind();
3859f4a2713aSLionel Sambuc
3860f4a2713aSLionel Sambuc // Never instantiate an explicit specialization.
3861f4a2713aSLionel Sambuc if (TSK == TSK_ExplicitSpecialization)
3862f4a2713aSLionel Sambuc return;
3863f4a2713aSLionel Sambuc
3864f4a2713aSLionel Sambuc // C++11 [temp.explicit]p10:
3865f4a2713aSLionel Sambuc // Except for inline functions, [...] explicit instantiation declarations
3866f4a2713aSLionel Sambuc // have the effect of suppressing the implicit instantiation of the entity
3867f4a2713aSLionel Sambuc // to which they refer.
3868f4a2713aSLionel Sambuc if (TSK == TSK_ExplicitInstantiationDeclaration)
3869f4a2713aSLionel Sambuc return;
3870f4a2713aSLionel Sambuc
3871f4a2713aSLionel Sambuc // Make sure to pass the instantiated variable to the consumer at the end.
3872f4a2713aSLionel Sambuc struct PassToConsumerRAII {
3873f4a2713aSLionel Sambuc ASTConsumer &Consumer;
3874f4a2713aSLionel Sambuc VarDecl *Var;
3875f4a2713aSLionel Sambuc
3876f4a2713aSLionel Sambuc PassToConsumerRAII(ASTConsumer &Consumer, VarDecl *Var)
3877f4a2713aSLionel Sambuc : Consumer(Consumer), Var(Var) { }
3878f4a2713aSLionel Sambuc
3879f4a2713aSLionel Sambuc ~PassToConsumerRAII() {
3880f4a2713aSLionel Sambuc Consumer.HandleCXXStaticMemberVarInstantiation(Var);
3881f4a2713aSLionel Sambuc }
3882f4a2713aSLionel Sambuc } PassToConsumerRAII(Consumer, Var);
3883f4a2713aSLionel Sambuc
3884f4a2713aSLionel Sambuc // If we already have a definition, we're done.
3885f4a2713aSLionel Sambuc if (VarDecl *Def = Var->getDefinition()) {
3886f4a2713aSLionel Sambuc // We may be explicitly instantiating something we've already implicitly
3887f4a2713aSLionel Sambuc // instantiated.
3888f4a2713aSLionel Sambuc Def->setTemplateSpecializationKind(Var->getTemplateSpecializationKind(),
3889f4a2713aSLionel Sambuc PointOfInstantiation);
3890f4a2713aSLionel Sambuc return;
3891f4a2713aSLionel Sambuc }
3892f4a2713aSLionel Sambuc
3893f4a2713aSLionel Sambuc InstantiatingTemplate Inst(*this, PointOfInstantiation, Var);
3894f4a2713aSLionel Sambuc if (Inst.isInvalid())
3895f4a2713aSLionel Sambuc return;
3896f4a2713aSLionel Sambuc
3897f4a2713aSLionel Sambuc // If we're performing recursive template instantiation, create our own
3898f4a2713aSLionel Sambuc // queue of pending implicit instantiations that we will instantiate later,
3899f4a2713aSLionel Sambuc // while we're still within our own instantiation context.
3900*0a6a1f1dSLionel Sambuc SavePendingLocalImplicitInstantiationsRAII
3901*0a6a1f1dSLionel Sambuc SavedPendingLocalImplicitInstantiations(*this);
3902*0a6a1f1dSLionel Sambuc std::unique_ptr<SavePendingInstantiationsAndVTableUsesRAII>
3903*0a6a1f1dSLionel Sambuc SavePendingInstantiationsAndVTableUses;
3904f4a2713aSLionel Sambuc if (Recursive) {
3905*0a6a1f1dSLionel Sambuc SavePendingInstantiationsAndVTableUses.reset(
3906*0a6a1f1dSLionel Sambuc new SavePendingInstantiationsAndVTableUsesRAII(*this));
3907f4a2713aSLionel Sambuc }
3908f4a2713aSLionel Sambuc
3909f4a2713aSLionel Sambuc // Enter the scope of this instantiation. We don't use
3910f4a2713aSLionel Sambuc // PushDeclContext because we don't have a scope.
3911f4a2713aSLionel Sambuc ContextRAII PreviousContext(*this, Var->getDeclContext());
3912f4a2713aSLionel Sambuc LocalInstantiationScope Local(*this);
3913f4a2713aSLionel Sambuc
3914f4a2713aSLionel Sambuc VarDecl *OldVar = Var;
3915f4a2713aSLionel Sambuc if (!VarSpec)
3916f4a2713aSLionel Sambuc Var = cast_or_null<VarDecl>(SubstDecl(Def, Var->getDeclContext(),
3917f4a2713aSLionel Sambuc TemplateArgs));
3918f4a2713aSLionel Sambuc else if (Var->isStaticDataMember() &&
3919f4a2713aSLionel Sambuc Var->getLexicalDeclContext()->isRecord()) {
3920f4a2713aSLionel Sambuc // We need to instantiate the definition of a static data member template,
3921f4a2713aSLionel Sambuc // and all we have is the in-class declaration of it. Instantiate a separate
3922f4a2713aSLionel Sambuc // declaration of the definition.
3923f4a2713aSLionel Sambuc TemplateDeclInstantiator Instantiator(*this, Var->getDeclContext(),
3924f4a2713aSLionel Sambuc TemplateArgs);
3925f4a2713aSLionel Sambuc Var = cast_or_null<VarDecl>(Instantiator.VisitVarTemplateSpecializationDecl(
3926*0a6a1f1dSLionel Sambuc VarSpec->getSpecializedTemplate(), Def, nullptr,
3927f4a2713aSLionel Sambuc VarSpec->getTemplateArgsInfo(), VarSpec->getTemplateArgs().asArray()));
3928f4a2713aSLionel Sambuc if (Var) {
3929f4a2713aSLionel Sambuc llvm::PointerUnion<VarTemplateDecl *,
3930f4a2713aSLionel Sambuc VarTemplatePartialSpecializationDecl *> PatternPtr =
3931f4a2713aSLionel Sambuc VarSpec->getSpecializedTemplateOrPartial();
3932f4a2713aSLionel Sambuc if (VarTemplatePartialSpecializationDecl *Partial =
3933f4a2713aSLionel Sambuc PatternPtr.dyn_cast<VarTemplatePartialSpecializationDecl *>())
3934f4a2713aSLionel Sambuc cast<VarTemplateSpecializationDecl>(Var)->setInstantiationOf(
3935f4a2713aSLionel Sambuc Partial, &VarSpec->getTemplateInstantiationArgs());
3936f4a2713aSLionel Sambuc
3937f4a2713aSLionel Sambuc // Merge the definition with the declaration.
3938f4a2713aSLionel Sambuc LookupResult R(*this, Var->getDeclName(), Var->getLocation(),
3939f4a2713aSLionel Sambuc LookupOrdinaryName, ForRedeclaration);
3940f4a2713aSLionel Sambuc R.addDecl(OldVar);
3941f4a2713aSLionel Sambuc MergeVarDecl(Var, R);
3942f4a2713aSLionel Sambuc
3943f4a2713aSLionel Sambuc // Attach the initializer.
3944f4a2713aSLionel Sambuc InstantiateVariableInitializer(Var, Def, TemplateArgs);
3945f4a2713aSLionel Sambuc }
3946f4a2713aSLionel Sambuc } else
3947f4a2713aSLionel Sambuc // Complete the existing variable's definition with an appropriately
3948f4a2713aSLionel Sambuc // substituted type and initializer.
3949f4a2713aSLionel Sambuc Var = CompleteVarTemplateSpecializationDecl(VarSpec, Def, TemplateArgs);
3950f4a2713aSLionel Sambuc
3951f4a2713aSLionel Sambuc PreviousContext.pop();
3952f4a2713aSLionel Sambuc
3953f4a2713aSLionel Sambuc if (Var) {
3954f4a2713aSLionel Sambuc PassToConsumerRAII.Var = Var;
3955f4a2713aSLionel Sambuc Var->setTemplateSpecializationKind(OldVar->getTemplateSpecializationKind(),
3956f4a2713aSLionel Sambuc OldVar->getPointOfInstantiation());
3957f4a2713aSLionel Sambuc }
3958f4a2713aSLionel Sambuc
3959f4a2713aSLionel Sambuc // This variable may have local implicit instantiations that need to be
3960f4a2713aSLionel Sambuc // instantiated within this scope.
3961f4a2713aSLionel Sambuc PerformPendingInstantiations(/*LocalOnly=*/true);
3962f4a2713aSLionel Sambuc
3963f4a2713aSLionel Sambuc Local.Exit();
3964f4a2713aSLionel Sambuc
3965f4a2713aSLionel Sambuc if (Recursive) {
3966f4a2713aSLionel Sambuc // Define any newly required vtables.
3967f4a2713aSLionel Sambuc DefineUsedVTables();
3968f4a2713aSLionel Sambuc
3969f4a2713aSLionel Sambuc // Instantiate any pending implicit instantiations found during the
3970f4a2713aSLionel Sambuc // instantiation of this template.
3971f4a2713aSLionel Sambuc PerformPendingInstantiations();
3972f4a2713aSLionel Sambuc
3973*0a6a1f1dSLionel Sambuc // Restore PendingInstantiations and VTableUses.
3974*0a6a1f1dSLionel Sambuc SavePendingInstantiationsAndVTableUses.reset();
3975f4a2713aSLionel Sambuc }
3976f4a2713aSLionel Sambuc }
3977f4a2713aSLionel Sambuc
3978f4a2713aSLionel Sambuc void
InstantiateMemInitializers(CXXConstructorDecl * New,const CXXConstructorDecl * Tmpl,const MultiLevelTemplateArgumentList & TemplateArgs)3979f4a2713aSLionel Sambuc Sema::InstantiateMemInitializers(CXXConstructorDecl *New,
3980f4a2713aSLionel Sambuc const CXXConstructorDecl *Tmpl,
3981f4a2713aSLionel Sambuc const MultiLevelTemplateArgumentList &TemplateArgs) {
3982f4a2713aSLionel Sambuc
3983f4a2713aSLionel Sambuc SmallVector<CXXCtorInitializer*, 4> NewInits;
3984f4a2713aSLionel Sambuc bool AnyErrors = Tmpl->isInvalidDecl();
3985f4a2713aSLionel Sambuc
3986f4a2713aSLionel Sambuc // Instantiate all the initializers.
3987*0a6a1f1dSLionel Sambuc for (const auto *Init : Tmpl->inits()) {
3988f4a2713aSLionel Sambuc // Only instantiate written initializers, let Sema re-construct implicit
3989f4a2713aSLionel Sambuc // ones.
3990f4a2713aSLionel Sambuc if (!Init->isWritten())
3991f4a2713aSLionel Sambuc continue;
3992f4a2713aSLionel Sambuc
3993f4a2713aSLionel Sambuc SourceLocation EllipsisLoc;
3994f4a2713aSLionel Sambuc
3995f4a2713aSLionel Sambuc if (Init->isPackExpansion()) {
3996f4a2713aSLionel Sambuc // This is a pack expansion. We should expand it now.
3997f4a2713aSLionel Sambuc TypeLoc BaseTL = Init->getTypeSourceInfo()->getTypeLoc();
3998f4a2713aSLionel Sambuc SmallVector<UnexpandedParameterPack, 4> Unexpanded;
3999f4a2713aSLionel Sambuc collectUnexpandedParameterPacks(BaseTL, Unexpanded);
4000f4a2713aSLionel Sambuc collectUnexpandedParameterPacks(Init->getInit(), Unexpanded);
4001f4a2713aSLionel Sambuc bool ShouldExpand = false;
4002f4a2713aSLionel Sambuc bool RetainExpansion = false;
4003f4a2713aSLionel Sambuc Optional<unsigned> NumExpansions;
4004f4a2713aSLionel Sambuc if (CheckParameterPacksForExpansion(Init->getEllipsisLoc(),
4005f4a2713aSLionel Sambuc BaseTL.getSourceRange(),
4006f4a2713aSLionel Sambuc Unexpanded,
4007f4a2713aSLionel Sambuc TemplateArgs, ShouldExpand,
4008f4a2713aSLionel Sambuc RetainExpansion,
4009f4a2713aSLionel Sambuc NumExpansions)) {
4010f4a2713aSLionel Sambuc AnyErrors = true;
4011f4a2713aSLionel Sambuc New->setInvalidDecl();
4012f4a2713aSLionel Sambuc continue;
4013f4a2713aSLionel Sambuc }
4014f4a2713aSLionel Sambuc assert(ShouldExpand && "Partial instantiation of base initializer?");
4015f4a2713aSLionel Sambuc
4016f4a2713aSLionel Sambuc // Loop over all of the arguments in the argument pack(s),
4017f4a2713aSLionel Sambuc for (unsigned I = 0; I != *NumExpansions; ++I) {
4018f4a2713aSLionel Sambuc Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(*this, I);
4019f4a2713aSLionel Sambuc
4020f4a2713aSLionel Sambuc // Instantiate the initializer.
4021f4a2713aSLionel Sambuc ExprResult TempInit = SubstInitializer(Init->getInit(), TemplateArgs,
4022f4a2713aSLionel Sambuc /*CXXDirectInit=*/true);
4023f4a2713aSLionel Sambuc if (TempInit.isInvalid()) {
4024f4a2713aSLionel Sambuc AnyErrors = true;
4025f4a2713aSLionel Sambuc break;
4026f4a2713aSLionel Sambuc }
4027f4a2713aSLionel Sambuc
4028f4a2713aSLionel Sambuc // Instantiate the base type.
4029f4a2713aSLionel Sambuc TypeSourceInfo *BaseTInfo = SubstType(Init->getTypeSourceInfo(),
4030f4a2713aSLionel Sambuc TemplateArgs,
4031f4a2713aSLionel Sambuc Init->getSourceLocation(),
4032f4a2713aSLionel Sambuc New->getDeclName());
4033f4a2713aSLionel Sambuc if (!BaseTInfo) {
4034f4a2713aSLionel Sambuc AnyErrors = true;
4035f4a2713aSLionel Sambuc break;
4036f4a2713aSLionel Sambuc }
4037f4a2713aSLionel Sambuc
4038f4a2713aSLionel Sambuc // Build the initializer.
4039f4a2713aSLionel Sambuc MemInitResult NewInit = BuildBaseInitializer(BaseTInfo->getType(),
4040*0a6a1f1dSLionel Sambuc BaseTInfo, TempInit.get(),
4041f4a2713aSLionel Sambuc New->getParent(),
4042f4a2713aSLionel Sambuc SourceLocation());
4043f4a2713aSLionel Sambuc if (NewInit.isInvalid()) {
4044f4a2713aSLionel Sambuc AnyErrors = true;
4045f4a2713aSLionel Sambuc break;
4046f4a2713aSLionel Sambuc }
4047f4a2713aSLionel Sambuc
4048f4a2713aSLionel Sambuc NewInits.push_back(NewInit.get());
4049f4a2713aSLionel Sambuc }
4050f4a2713aSLionel Sambuc
4051f4a2713aSLionel Sambuc continue;
4052f4a2713aSLionel Sambuc }
4053f4a2713aSLionel Sambuc
4054f4a2713aSLionel Sambuc // Instantiate the initializer.
4055f4a2713aSLionel Sambuc ExprResult TempInit = SubstInitializer(Init->getInit(), TemplateArgs,
4056f4a2713aSLionel Sambuc /*CXXDirectInit=*/true);
4057f4a2713aSLionel Sambuc if (TempInit.isInvalid()) {
4058f4a2713aSLionel Sambuc AnyErrors = true;
4059f4a2713aSLionel Sambuc continue;
4060f4a2713aSLionel Sambuc }
4061f4a2713aSLionel Sambuc
4062f4a2713aSLionel Sambuc MemInitResult NewInit;
4063f4a2713aSLionel Sambuc if (Init->isDelegatingInitializer() || Init->isBaseInitializer()) {
4064f4a2713aSLionel Sambuc TypeSourceInfo *TInfo = SubstType(Init->getTypeSourceInfo(),
4065f4a2713aSLionel Sambuc TemplateArgs,
4066f4a2713aSLionel Sambuc Init->getSourceLocation(),
4067f4a2713aSLionel Sambuc New->getDeclName());
4068f4a2713aSLionel Sambuc if (!TInfo) {
4069f4a2713aSLionel Sambuc AnyErrors = true;
4070f4a2713aSLionel Sambuc New->setInvalidDecl();
4071f4a2713aSLionel Sambuc continue;
4072f4a2713aSLionel Sambuc }
4073f4a2713aSLionel Sambuc
4074f4a2713aSLionel Sambuc if (Init->isBaseInitializer())
4075*0a6a1f1dSLionel Sambuc NewInit = BuildBaseInitializer(TInfo->getType(), TInfo, TempInit.get(),
4076f4a2713aSLionel Sambuc New->getParent(), EllipsisLoc);
4077f4a2713aSLionel Sambuc else
4078*0a6a1f1dSLionel Sambuc NewInit = BuildDelegatingInitializer(TInfo, TempInit.get(),
4079f4a2713aSLionel Sambuc cast<CXXRecordDecl>(CurContext->getParent()));
4080f4a2713aSLionel Sambuc } else if (Init->isMemberInitializer()) {
4081f4a2713aSLionel Sambuc FieldDecl *Member = cast_or_null<FieldDecl>(FindInstantiatedDecl(
4082f4a2713aSLionel Sambuc Init->getMemberLocation(),
4083f4a2713aSLionel Sambuc Init->getMember(),
4084f4a2713aSLionel Sambuc TemplateArgs));
4085f4a2713aSLionel Sambuc if (!Member) {
4086f4a2713aSLionel Sambuc AnyErrors = true;
4087f4a2713aSLionel Sambuc New->setInvalidDecl();
4088f4a2713aSLionel Sambuc continue;
4089f4a2713aSLionel Sambuc }
4090f4a2713aSLionel Sambuc
4091*0a6a1f1dSLionel Sambuc NewInit = BuildMemberInitializer(Member, TempInit.get(),
4092f4a2713aSLionel Sambuc Init->getSourceLocation());
4093f4a2713aSLionel Sambuc } else if (Init->isIndirectMemberInitializer()) {
4094f4a2713aSLionel Sambuc IndirectFieldDecl *IndirectMember =
4095f4a2713aSLionel Sambuc cast_or_null<IndirectFieldDecl>(FindInstantiatedDecl(
4096f4a2713aSLionel Sambuc Init->getMemberLocation(),
4097f4a2713aSLionel Sambuc Init->getIndirectMember(), TemplateArgs));
4098f4a2713aSLionel Sambuc
4099f4a2713aSLionel Sambuc if (!IndirectMember) {
4100f4a2713aSLionel Sambuc AnyErrors = true;
4101f4a2713aSLionel Sambuc New->setInvalidDecl();
4102f4a2713aSLionel Sambuc continue;
4103f4a2713aSLionel Sambuc }
4104f4a2713aSLionel Sambuc
4105*0a6a1f1dSLionel Sambuc NewInit = BuildMemberInitializer(IndirectMember, TempInit.get(),
4106f4a2713aSLionel Sambuc Init->getSourceLocation());
4107f4a2713aSLionel Sambuc }
4108f4a2713aSLionel Sambuc
4109f4a2713aSLionel Sambuc if (NewInit.isInvalid()) {
4110f4a2713aSLionel Sambuc AnyErrors = true;
4111f4a2713aSLionel Sambuc New->setInvalidDecl();
4112f4a2713aSLionel Sambuc } else {
4113f4a2713aSLionel Sambuc NewInits.push_back(NewInit.get());
4114f4a2713aSLionel Sambuc }
4115f4a2713aSLionel Sambuc }
4116f4a2713aSLionel Sambuc
4117f4a2713aSLionel Sambuc // Assign all the initializers to the new constructor.
4118f4a2713aSLionel Sambuc ActOnMemInitializers(New,
4119f4a2713aSLionel Sambuc /*FIXME: ColonLoc */
4120f4a2713aSLionel Sambuc SourceLocation(),
4121f4a2713aSLionel Sambuc NewInits,
4122f4a2713aSLionel Sambuc AnyErrors);
4123f4a2713aSLionel Sambuc }
4124f4a2713aSLionel Sambuc
4125f4a2713aSLionel Sambuc // TODO: this could be templated if the various decl types used the
4126f4a2713aSLionel Sambuc // same method name.
isInstantiationOf(ClassTemplateDecl * Pattern,ClassTemplateDecl * Instance)4127f4a2713aSLionel Sambuc static bool isInstantiationOf(ClassTemplateDecl *Pattern,
4128f4a2713aSLionel Sambuc ClassTemplateDecl *Instance) {
4129f4a2713aSLionel Sambuc Pattern = Pattern->getCanonicalDecl();
4130f4a2713aSLionel Sambuc
4131f4a2713aSLionel Sambuc do {
4132f4a2713aSLionel Sambuc Instance = Instance->getCanonicalDecl();
4133f4a2713aSLionel Sambuc if (Pattern == Instance) return true;
4134f4a2713aSLionel Sambuc Instance = Instance->getInstantiatedFromMemberTemplate();
4135f4a2713aSLionel Sambuc } while (Instance);
4136f4a2713aSLionel Sambuc
4137f4a2713aSLionel Sambuc return false;
4138f4a2713aSLionel Sambuc }
4139f4a2713aSLionel Sambuc
isInstantiationOf(FunctionTemplateDecl * Pattern,FunctionTemplateDecl * Instance)4140f4a2713aSLionel Sambuc static bool isInstantiationOf(FunctionTemplateDecl *Pattern,
4141f4a2713aSLionel Sambuc FunctionTemplateDecl *Instance) {
4142f4a2713aSLionel Sambuc Pattern = Pattern->getCanonicalDecl();
4143f4a2713aSLionel Sambuc
4144f4a2713aSLionel Sambuc do {
4145f4a2713aSLionel Sambuc Instance = Instance->getCanonicalDecl();
4146f4a2713aSLionel Sambuc if (Pattern == Instance) return true;
4147f4a2713aSLionel Sambuc Instance = Instance->getInstantiatedFromMemberTemplate();
4148f4a2713aSLionel Sambuc } while (Instance);
4149f4a2713aSLionel Sambuc
4150f4a2713aSLionel Sambuc return false;
4151f4a2713aSLionel Sambuc }
4152f4a2713aSLionel Sambuc
4153f4a2713aSLionel Sambuc static bool
isInstantiationOf(ClassTemplatePartialSpecializationDecl * Pattern,ClassTemplatePartialSpecializationDecl * Instance)4154f4a2713aSLionel Sambuc isInstantiationOf(ClassTemplatePartialSpecializationDecl *Pattern,
4155f4a2713aSLionel Sambuc ClassTemplatePartialSpecializationDecl *Instance) {
4156f4a2713aSLionel Sambuc Pattern
4157f4a2713aSLionel Sambuc = cast<ClassTemplatePartialSpecializationDecl>(Pattern->getCanonicalDecl());
4158f4a2713aSLionel Sambuc do {
4159f4a2713aSLionel Sambuc Instance = cast<ClassTemplatePartialSpecializationDecl>(
4160f4a2713aSLionel Sambuc Instance->getCanonicalDecl());
4161f4a2713aSLionel Sambuc if (Pattern == Instance)
4162f4a2713aSLionel Sambuc return true;
4163f4a2713aSLionel Sambuc Instance = Instance->getInstantiatedFromMember();
4164f4a2713aSLionel Sambuc } while (Instance);
4165f4a2713aSLionel Sambuc
4166f4a2713aSLionel Sambuc return false;
4167f4a2713aSLionel Sambuc }
4168f4a2713aSLionel Sambuc
isInstantiationOf(CXXRecordDecl * Pattern,CXXRecordDecl * Instance)4169f4a2713aSLionel Sambuc static bool isInstantiationOf(CXXRecordDecl *Pattern,
4170f4a2713aSLionel Sambuc CXXRecordDecl *Instance) {
4171f4a2713aSLionel Sambuc Pattern = Pattern->getCanonicalDecl();
4172f4a2713aSLionel Sambuc
4173f4a2713aSLionel Sambuc do {
4174f4a2713aSLionel Sambuc Instance = Instance->getCanonicalDecl();
4175f4a2713aSLionel Sambuc if (Pattern == Instance) return true;
4176f4a2713aSLionel Sambuc Instance = Instance->getInstantiatedFromMemberClass();
4177f4a2713aSLionel Sambuc } while (Instance);
4178f4a2713aSLionel Sambuc
4179f4a2713aSLionel Sambuc return false;
4180f4a2713aSLionel Sambuc }
4181f4a2713aSLionel Sambuc
isInstantiationOf(FunctionDecl * Pattern,FunctionDecl * Instance)4182f4a2713aSLionel Sambuc static bool isInstantiationOf(FunctionDecl *Pattern,
4183f4a2713aSLionel Sambuc FunctionDecl *Instance) {
4184f4a2713aSLionel Sambuc Pattern = Pattern->getCanonicalDecl();
4185f4a2713aSLionel Sambuc
4186f4a2713aSLionel Sambuc do {
4187f4a2713aSLionel Sambuc Instance = Instance->getCanonicalDecl();
4188f4a2713aSLionel Sambuc if (Pattern == Instance) return true;
4189f4a2713aSLionel Sambuc Instance = Instance->getInstantiatedFromMemberFunction();
4190f4a2713aSLionel Sambuc } while (Instance);
4191f4a2713aSLionel Sambuc
4192f4a2713aSLionel Sambuc return false;
4193f4a2713aSLionel Sambuc }
4194f4a2713aSLionel Sambuc
isInstantiationOf(EnumDecl * Pattern,EnumDecl * Instance)4195f4a2713aSLionel Sambuc static bool isInstantiationOf(EnumDecl *Pattern,
4196f4a2713aSLionel Sambuc EnumDecl *Instance) {
4197f4a2713aSLionel Sambuc Pattern = Pattern->getCanonicalDecl();
4198f4a2713aSLionel Sambuc
4199f4a2713aSLionel Sambuc do {
4200f4a2713aSLionel Sambuc Instance = Instance->getCanonicalDecl();
4201f4a2713aSLionel Sambuc if (Pattern == Instance) return true;
4202f4a2713aSLionel Sambuc Instance = Instance->getInstantiatedFromMemberEnum();
4203f4a2713aSLionel Sambuc } while (Instance);
4204f4a2713aSLionel Sambuc
4205f4a2713aSLionel Sambuc return false;
4206f4a2713aSLionel Sambuc }
4207f4a2713aSLionel Sambuc
isInstantiationOf(UsingShadowDecl * Pattern,UsingShadowDecl * Instance,ASTContext & C)4208f4a2713aSLionel Sambuc static bool isInstantiationOf(UsingShadowDecl *Pattern,
4209f4a2713aSLionel Sambuc UsingShadowDecl *Instance,
4210f4a2713aSLionel Sambuc ASTContext &C) {
4211*0a6a1f1dSLionel Sambuc return declaresSameEntity(C.getInstantiatedFromUsingShadowDecl(Instance),
4212*0a6a1f1dSLionel Sambuc Pattern);
4213f4a2713aSLionel Sambuc }
4214f4a2713aSLionel Sambuc
isInstantiationOf(UsingDecl * Pattern,UsingDecl * Instance,ASTContext & C)4215f4a2713aSLionel Sambuc static bool isInstantiationOf(UsingDecl *Pattern,
4216f4a2713aSLionel Sambuc UsingDecl *Instance,
4217f4a2713aSLionel Sambuc ASTContext &C) {
4218*0a6a1f1dSLionel Sambuc return declaresSameEntity(C.getInstantiatedFromUsingDecl(Instance), Pattern);
4219f4a2713aSLionel Sambuc }
4220f4a2713aSLionel Sambuc
isInstantiationOf(UnresolvedUsingValueDecl * Pattern,UsingDecl * Instance,ASTContext & C)4221f4a2713aSLionel Sambuc static bool isInstantiationOf(UnresolvedUsingValueDecl *Pattern,
4222f4a2713aSLionel Sambuc UsingDecl *Instance,
4223f4a2713aSLionel Sambuc ASTContext &C) {
4224*0a6a1f1dSLionel Sambuc return declaresSameEntity(C.getInstantiatedFromUsingDecl(Instance), Pattern);
4225f4a2713aSLionel Sambuc }
4226f4a2713aSLionel Sambuc
isInstantiationOf(UnresolvedUsingTypenameDecl * Pattern,UsingDecl * Instance,ASTContext & C)4227f4a2713aSLionel Sambuc static bool isInstantiationOf(UnresolvedUsingTypenameDecl *Pattern,
4228f4a2713aSLionel Sambuc UsingDecl *Instance,
4229f4a2713aSLionel Sambuc ASTContext &C) {
4230*0a6a1f1dSLionel Sambuc return declaresSameEntity(C.getInstantiatedFromUsingDecl(Instance), Pattern);
4231f4a2713aSLionel Sambuc }
4232f4a2713aSLionel Sambuc
isInstantiationOfStaticDataMember(VarDecl * Pattern,VarDecl * Instance)4233f4a2713aSLionel Sambuc static bool isInstantiationOfStaticDataMember(VarDecl *Pattern,
4234f4a2713aSLionel Sambuc VarDecl *Instance) {
4235f4a2713aSLionel Sambuc assert(Instance->isStaticDataMember());
4236f4a2713aSLionel Sambuc
4237f4a2713aSLionel Sambuc Pattern = Pattern->getCanonicalDecl();
4238f4a2713aSLionel Sambuc
4239f4a2713aSLionel Sambuc do {
4240f4a2713aSLionel Sambuc Instance = Instance->getCanonicalDecl();
4241f4a2713aSLionel Sambuc if (Pattern == Instance) return true;
4242f4a2713aSLionel Sambuc Instance = Instance->getInstantiatedFromStaticDataMember();
4243f4a2713aSLionel Sambuc } while (Instance);
4244f4a2713aSLionel Sambuc
4245f4a2713aSLionel Sambuc return false;
4246f4a2713aSLionel Sambuc }
4247f4a2713aSLionel Sambuc
4248f4a2713aSLionel Sambuc // Other is the prospective instantiation
4249f4a2713aSLionel Sambuc // D is the prospective pattern
isInstantiationOf(ASTContext & Ctx,NamedDecl * D,Decl * Other)4250f4a2713aSLionel Sambuc static bool isInstantiationOf(ASTContext &Ctx, NamedDecl *D, Decl *Other) {
4251f4a2713aSLionel Sambuc if (D->getKind() != Other->getKind()) {
4252f4a2713aSLionel Sambuc if (UnresolvedUsingTypenameDecl *UUD
4253f4a2713aSLionel Sambuc = dyn_cast<UnresolvedUsingTypenameDecl>(D)) {
4254f4a2713aSLionel Sambuc if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) {
4255f4a2713aSLionel Sambuc return isInstantiationOf(UUD, UD, Ctx);
4256f4a2713aSLionel Sambuc }
4257f4a2713aSLionel Sambuc }
4258f4a2713aSLionel Sambuc
4259f4a2713aSLionel Sambuc if (UnresolvedUsingValueDecl *UUD
4260f4a2713aSLionel Sambuc = dyn_cast<UnresolvedUsingValueDecl>(D)) {
4261f4a2713aSLionel Sambuc if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) {
4262f4a2713aSLionel Sambuc return isInstantiationOf(UUD, UD, Ctx);
4263f4a2713aSLionel Sambuc }
4264f4a2713aSLionel Sambuc }
4265f4a2713aSLionel Sambuc
4266f4a2713aSLionel Sambuc return false;
4267f4a2713aSLionel Sambuc }
4268f4a2713aSLionel Sambuc
4269f4a2713aSLionel Sambuc if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Other))
4270f4a2713aSLionel Sambuc return isInstantiationOf(cast<CXXRecordDecl>(D), Record);
4271f4a2713aSLionel Sambuc
4272f4a2713aSLionel Sambuc if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Other))
4273f4a2713aSLionel Sambuc return isInstantiationOf(cast<FunctionDecl>(D), Function);
4274f4a2713aSLionel Sambuc
4275f4a2713aSLionel Sambuc if (EnumDecl *Enum = dyn_cast<EnumDecl>(Other))
4276f4a2713aSLionel Sambuc return isInstantiationOf(cast<EnumDecl>(D), Enum);
4277f4a2713aSLionel Sambuc
4278f4a2713aSLionel Sambuc if (VarDecl *Var = dyn_cast<VarDecl>(Other))
4279f4a2713aSLionel Sambuc if (Var->isStaticDataMember())
4280f4a2713aSLionel Sambuc return isInstantiationOfStaticDataMember(cast<VarDecl>(D), Var);
4281f4a2713aSLionel Sambuc
4282f4a2713aSLionel Sambuc if (ClassTemplateDecl *Temp = dyn_cast<ClassTemplateDecl>(Other))
4283f4a2713aSLionel Sambuc return isInstantiationOf(cast<ClassTemplateDecl>(D), Temp);
4284f4a2713aSLionel Sambuc
4285f4a2713aSLionel Sambuc if (FunctionTemplateDecl *Temp = dyn_cast<FunctionTemplateDecl>(Other))
4286f4a2713aSLionel Sambuc return isInstantiationOf(cast<FunctionTemplateDecl>(D), Temp);
4287f4a2713aSLionel Sambuc
4288f4a2713aSLionel Sambuc if (ClassTemplatePartialSpecializationDecl *PartialSpec
4289f4a2713aSLionel Sambuc = dyn_cast<ClassTemplatePartialSpecializationDecl>(Other))
4290f4a2713aSLionel Sambuc return isInstantiationOf(cast<ClassTemplatePartialSpecializationDecl>(D),
4291f4a2713aSLionel Sambuc PartialSpec);
4292f4a2713aSLionel Sambuc
4293f4a2713aSLionel Sambuc if (FieldDecl *Field = dyn_cast<FieldDecl>(Other)) {
4294f4a2713aSLionel Sambuc if (!Field->getDeclName()) {
4295f4a2713aSLionel Sambuc // This is an unnamed field.
4296*0a6a1f1dSLionel Sambuc return declaresSameEntity(Ctx.getInstantiatedFromUnnamedFieldDecl(Field),
4297*0a6a1f1dSLionel Sambuc cast<FieldDecl>(D));
4298f4a2713aSLionel Sambuc }
4299f4a2713aSLionel Sambuc }
4300f4a2713aSLionel Sambuc
4301f4a2713aSLionel Sambuc if (UsingDecl *Using = dyn_cast<UsingDecl>(Other))
4302f4a2713aSLionel Sambuc return isInstantiationOf(cast<UsingDecl>(D), Using, Ctx);
4303f4a2713aSLionel Sambuc
4304f4a2713aSLionel Sambuc if (UsingShadowDecl *Shadow = dyn_cast<UsingShadowDecl>(Other))
4305f4a2713aSLionel Sambuc return isInstantiationOf(cast<UsingShadowDecl>(D), Shadow, Ctx);
4306f4a2713aSLionel Sambuc
4307f4a2713aSLionel Sambuc return D->getDeclName() && isa<NamedDecl>(Other) &&
4308f4a2713aSLionel Sambuc D->getDeclName() == cast<NamedDecl>(Other)->getDeclName();
4309f4a2713aSLionel Sambuc }
4310f4a2713aSLionel Sambuc
4311f4a2713aSLionel Sambuc template<typename ForwardIterator>
findInstantiationOf(ASTContext & Ctx,NamedDecl * D,ForwardIterator first,ForwardIterator last)4312f4a2713aSLionel Sambuc static NamedDecl *findInstantiationOf(ASTContext &Ctx,
4313f4a2713aSLionel Sambuc NamedDecl *D,
4314f4a2713aSLionel Sambuc ForwardIterator first,
4315f4a2713aSLionel Sambuc ForwardIterator last) {
4316f4a2713aSLionel Sambuc for (; first != last; ++first)
4317f4a2713aSLionel Sambuc if (isInstantiationOf(Ctx, D, *first))
4318f4a2713aSLionel Sambuc return cast<NamedDecl>(*first);
4319f4a2713aSLionel Sambuc
4320*0a6a1f1dSLionel Sambuc return nullptr;
4321f4a2713aSLionel Sambuc }
4322f4a2713aSLionel Sambuc
4323f4a2713aSLionel Sambuc /// \brief Finds the instantiation of the given declaration context
4324f4a2713aSLionel Sambuc /// within the current instantiation.
4325f4a2713aSLionel Sambuc ///
4326f4a2713aSLionel Sambuc /// \returns NULL if there was an error
FindInstantiatedContext(SourceLocation Loc,DeclContext * DC,const MultiLevelTemplateArgumentList & TemplateArgs)4327f4a2713aSLionel Sambuc DeclContext *Sema::FindInstantiatedContext(SourceLocation Loc, DeclContext* DC,
4328f4a2713aSLionel Sambuc const MultiLevelTemplateArgumentList &TemplateArgs) {
4329f4a2713aSLionel Sambuc if (NamedDecl *D = dyn_cast<NamedDecl>(DC)) {
4330f4a2713aSLionel Sambuc Decl* ID = FindInstantiatedDecl(Loc, D, TemplateArgs);
4331f4a2713aSLionel Sambuc return cast_or_null<DeclContext>(ID);
4332f4a2713aSLionel Sambuc } else return DC;
4333f4a2713aSLionel Sambuc }
4334f4a2713aSLionel Sambuc
4335f4a2713aSLionel Sambuc /// \brief Find the instantiation of the given declaration within the
4336f4a2713aSLionel Sambuc /// current instantiation.
4337f4a2713aSLionel Sambuc ///
4338f4a2713aSLionel Sambuc /// This routine is intended to be used when \p D is a declaration
4339f4a2713aSLionel Sambuc /// referenced from within a template, that needs to mapped into the
4340f4a2713aSLionel Sambuc /// corresponding declaration within an instantiation. For example,
4341f4a2713aSLionel Sambuc /// given:
4342f4a2713aSLionel Sambuc ///
4343f4a2713aSLionel Sambuc /// \code
4344f4a2713aSLionel Sambuc /// template<typename T>
4345f4a2713aSLionel Sambuc /// struct X {
4346f4a2713aSLionel Sambuc /// enum Kind {
4347f4a2713aSLionel Sambuc /// KnownValue = sizeof(T)
4348f4a2713aSLionel Sambuc /// };
4349f4a2713aSLionel Sambuc ///
4350f4a2713aSLionel Sambuc /// bool getKind() const { return KnownValue; }
4351f4a2713aSLionel Sambuc /// };
4352f4a2713aSLionel Sambuc ///
4353f4a2713aSLionel Sambuc /// template struct X<int>;
4354f4a2713aSLionel Sambuc /// \endcode
4355f4a2713aSLionel Sambuc ///
4356f4a2713aSLionel Sambuc /// In the instantiation of <tt>X<int>::getKind()</tt>, we need to map the
4357f4a2713aSLionel Sambuc /// \p EnumConstantDecl for \p KnownValue (which refers to
4358f4a2713aSLionel Sambuc /// <tt>X<T>::<Kind>::KnownValue</tt>) to its instantiation
4359f4a2713aSLionel Sambuc /// (<tt>X<int>::<Kind>::KnownValue</tt>). \p FindInstantiatedDecl performs
4360f4a2713aSLionel Sambuc /// this mapping from within the instantiation of <tt>X<int></tt>.
FindInstantiatedDecl(SourceLocation Loc,NamedDecl * D,const MultiLevelTemplateArgumentList & TemplateArgs)4361f4a2713aSLionel Sambuc NamedDecl *Sema::FindInstantiatedDecl(SourceLocation Loc, NamedDecl *D,
4362f4a2713aSLionel Sambuc const MultiLevelTemplateArgumentList &TemplateArgs) {
4363f4a2713aSLionel Sambuc DeclContext *ParentDC = D->getDeclContext();
4364f4a2713aSLionel Sambuc // FIXME: Parmeters of pointer to functions (y below) that are themselves
4365f4a2713aSLionel Sambuc // parameters (p below) can have their ParentDC set to the translation-unit
4366f4a2713aSLionel Sambuc // - thus we can not consistently check if the ParentDC of such a parameter
4367f4a2713aSLionel Sambuc // is Dependent or/and a FunctionOrMethod.
4368f4a2713aSLionel Sambuc // For e.g. this code, during Template argument deduction tries to
4369f4a2713aSLionel Sambuc // find an instantiated decl for (T y) when the ParentDC for y is
4370f4a2713aSLionel Sambuc // the translation unit.
4371f4a2713aSLionel Sambuc // e.g. template <class T> void Foo(auto (*p)(T y) -> decltype(y())) {}
4372f4a2713aSLionel Sambuc // float baz(float(*)()) { return 0.0; }
4373f4a2713aSLionel Sambuc // Foo(baz);
4374f4a2713aSLionel Sambuc // The better fix here is perhaps to ensure that a ParmVarDecl, by the time
4375f4a2713aSLionel Sambuc // it gets here, always has a FunctionOrMethod as its ParentDC??
4376f4a2713aSLionel Sambuc // For now:
4377f4a2713aSLionel Sambuc // - as long as we have a ParmVarDecl whose parent is non-dependent and
4378f4a2713aSLionel Sambuc // whose type is not instantiation dependent, do nothing to the decl
4379f4a2713aSLionel Sambuc // - otherwise find its instantiated decl.
4380f4a2713aSLionel Sambuc if (isa<ParmVarDecl>(D) && !ParentDC->isDependentContext() &&
4381f4a2713aSLionel Sambuc !cast<ParmVarDecl>(D)->getType()->isInstantiationDependentType())
4382f4a2713aSLionel Sambuc return D;
4383f4a2713aSLionel Sambuc if (isa<ParmVarDecl>(D) || isa<NonTypeTemplateParmDecl>(D) ||
4384f4a2713aSLionel Sambuc isa<TemplateTypeParmDecl>(D) || isa<TemplateTemplateParmDecl>(D) ||
4385f4a2713aSLionel Sambuc (ParentDC->isFunctionOrMethod() && ParentDC->isDependentContext()) ||
4386f4a2713aSLionel Sambuc (isa<CXXRecordDecl>(D) && cast<CXXRecordDecl>(D)->isLambda())) {
4387f4a2713aSLionel Sambuc // D is a local of some kind. Look into the map of local
4388f4a2713aSLionel Sambuc // declarations to their instantiations.
4389*0a6a1f1dSLionel Sambuc if (CurrentInstantiationScope) {
4390*0a6a1f1dSLionel Sambuc if (auto Found = CurrentInstantiationScope->findInstantiationOf(D)) {
4391f4a2713aSLionel Sambuc if (Decl *FD = Found->dyn_cast<Decl *>())
4392f4a2713aSLionel Sambuc return cast<NamedDecl>(FD);
4393f4a2713aSLionel Sambuc
4394f4a2713aSLionel Sambuc int PackIdx = ArgumentPackSubstitutionIndex;
4395*0a6a1f1dSLionel Sambuc assert(PackIdx != -1 &&
4396*0a6a1f1dSLionel Sambuc "found declaration pack but not pack expanding");
4397*0a6a1f1dSLionel Sambuc typedef LocalInstantiationScope::DeclArgumentPack DeclArgumentPack;
4398f4a2713aSLionel Sambuc return cast<NamedDecl>((*Found->get<DeclArgumentPack *>())[PackIdx]);
4399f4a2713aSLionel Sambuc }
4400*0a6a1f1dSLionel Sambuc }
4401f4a2713aSLionel Sambuc
4402f4a2713aSLionel Sambuc // If we're performing a partial substitution during template argument
4403f4a2713aSLionel Sambuc // deduction, we may not have values for template parameters yet. They
4404f4a2713aSLionel Sambuc // just map to themselves.
4405f4a2713aSLionel Sambuc if (isa<NonTypeTemplateParmDecl>(D) || isa<TemplateTypeParmDecl>(D) ||
4406f4a2713aSLionel Sambuc isa<TemplateTemplateParmDecl>(D))
4407f4a2713aSLionel Sambuc return D;
4408f4a2713aSLionel Sambuc
4409f4a2713aSLionel Sambuc if (D->isInvalidDecl())
4410*0a6a1f1dSLionel Sambuc return nullptr;
4411f4a2713aSLionel Sambuc
4412f4a2713aSLionel Sambuc // If we didn't find the decl, then we must have a label decl that hasn't
4413f4a2713aSLionel Sambuc // been found yet. Lazily instantiate it and return it now.
4414f4a2713aSLionel Sambuc assert(isa<LabelDecl>(D));
4415f4a2713aSLionel Sambuc
4416f4a2713aSLionel Sambuc Decl *Inst = SubstDecl(D, CurContext, TemplateArgs);
4417f4a2713aSLionel Sambuc assert(Inst && "Failed to instantiate label??");
4418f4a2713aSLionel Sambuc
4419f4a2713aSLionel Sambuc CurrentInstantiationScope->InstantiatedLocal(D, Inst);
4420f4a2713aSLionel Sambuc return cast<LabelDecl>(Inst);
4421f4a2713aSLionel Sambuc }
4422f4a2713aSLionel Sambuc
4423f4a2713aSLionel Sambuc // For variable template specializations, update those that are still
4424f4a2713aSLionel Sambuc // type-dependent.
4425f4a2713aSLionel Sambuc if (VarTemplateSpecializationDecl *VarSpec =
4426f4a2713aSLionel Sambuc dyn_cast<VarTemplateSpecializationDecl>(D)) {
4427f4a2713aSLionel Sambuc bool InstantiationDependent = false;
4428f4a2713aSLionel Sambuc const TemplateArgumentListInfo &VarTemplateArgs =
4429f4a2713aSLionel Sambuc VarSpec->getTemplateArgsInfo();
4430f4a2713aSLionel Sambuc if (TemplateSpecializationType::anyDependentTemplateArguments(
4431f4a2713aSLionel Sambuc VarTemplateArgs, InstantiationDependent))
4432f4a2713aSLionel Sambuc D = cast<NamedDecl>(
4433f4a2713aSLionel Sambuc SubstDecl(D, VarSpec->getDeclContext(), TemplateArgs));
4434f4a2713aSLionel Sambuc return D;
4435f4a2713aSLionel Sambuc }
4436f4a2713aSLionel Sambuc
4437f4a2713aSLionel Sambuc if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) {
4438f4a2713aSLionel Sambuc if (!Record->isDependentContext())
4439f4a2713aSLionel Sambuc return D;
4440f4a2713aSLionel Sambuc
4441f4a2713aSLionel Sambuc // Determine whether this record is the "templated" declaration describing
4442f4a2713aSLionel Sambuc // a class template or class template partial specialization.
4443f4a2713aSLionel Sambuc ClassTemplateDecl *ClassTemplate = Record->getDescribedClassTemplate();
4444f4a2713aSLionel Sambuc if (ClassTemplate)
4445f4a2713aSLionel Sambuc ClassTemplate = ClassTemplate->getCanonicalDecl();
4446f4a2713aSLionel Sambuc else if (ClassTemplatePartialSpecializationDecl *PartialSpec
4447f4a2713aSLionel Sambuc = dyn_cast<ClassTemplatePartialSpecializationDecl>(Record))
4448f4a2713aSLionel Sambuc ClassTemplate = PartialSpec->getSpecializedTemplate()->getCanonicalDecl();
4449f4a2713aSLionel Sambuc
4450f4a2713aSLionel Sambuc // Walk the current context to find either the record or an instantiation of
4451f4a2713aSLionel Sambuc // it.
4452f4a2713aSLionel Sambuc DeclContext *DC = CurContext;
4453f4a2713aSLionel Sambuc while (!DC->isFileContext()) {
4454f4a2713aSLionel Sambuc // If we're performing substitution while we're inside the template
4455f4a2713aSLionel Sambuc // definition, we'll find our own context. We're done.
4456f4a2713aSLionel Sambuc if (DC->Equals(Record))
4457f4a2713aSLionel Sambuc return Record;
4458f4a2713aSLionel Sambuc
4459f4a2713aSLionel Sambuc if (CXXRecordDecl *InstRecord = dyn_cast<CXXRecordDecl>(DC)) {
4460f4a2713aSLionel Sambuc // Check whether we're in the process of instantiating a class template
4461f4a2713aSLionel Sambuc // specialization of the template we're mapping.
4462f4a2713aSLionel Sambuc if (ClassTemplateSpecializationDecl *InstSpec
4463f4a2713aSLionel Sambuc = dyn_cast<ClassTemplateSpecializationDecl>(InstRecord)){
4464f4a2713aSLionel Sambuc ClassTemplateDecl *SpecTemplate = InstSpec->getSpecializedTemplate();
4465f4a2713aSLionel Sambuc if (ClassTemplate && isInstantiationOf(ClassTemplate, SpecTemplate))
4466f4a2713aSLionel Sambuc return InstRecord;
4467f4a2713aSLionel Sambuc }
4468f4a2713aSLionel Sambuc
4469f4a2713aSLionel Sambuc // Check whether we're in the process of instantiating a member class.
4470f4a2713aSLionel Sambuc if (isInstantiationOf(Record, InstRecord))
4471f4a2713aSLionel Sambuc return InstRecord;
4472f4a2713aSLionel Sambuc }
4473f4a2713aSLionel Sambuc
4474f4a2713aSLionel Sambuc // Move to the outer template scope.
4475f4a2713aSLionel Sambuc if (FunctionDecl *FD = dyn_cast<FunctionDecl>(DC)) {
4476f4a2713aSLionel Sambuc if (FD->getFriendObjectKind() && FD->getDeclContext()->isFileContext()){
4477f4a2713aSLionel Sambuc DC = FD->getLexicalDeclContext();
4478f4a2713aSLionel Sambuc continue;
4479f4a2713aSLionel Sambuc }
4480f4a2713aSLionel Sambuc }
4481f4a2713aSLionel Sambuc
4482f4a2713aSLionel Sambuc DC = DC->getParent();
4483f4a2713aSLionel Sambuc }
4484f4a2713aSLionel Sambuc
4485f4a2713aSLionel Sambuc // Fall through to deal with other dependent record types (e.g.,
4486f4a2713aSLionel Sambuc // anonymous unions in class templates).
4487f4a2713aSLionel Sambuc }
4488f4a2713aSLionel Sambuc
4489f4a2713aSLionel Sambuc if (!ParentDC->isDependentContext())
4490f4a2713aSLionel Sambuc return D;
4491f4a2713aSLionel Sambuc
4492f4a2713aSLionel Sambuc ParentDC = FindInstantiatedContext(Loc, ParentDC, TemplateArgs);
4493f4a2713aSLionel Sambuc if (!ParentDC)
4494*0a6a1f1dSLionel Sambuc return nullptr;
4495f4a2713aSLionel Sambuc
4496f4a2713aSLionel Sambuc if (ParentDC != D->getDeclContext()) {
4497f4a2713aSLionel Sambuc // We performed some kind of instantiation in the parent context,
4498f4a2713aSLionel Sambuc // so now we need to look into the instantiated parent context to
4499f4a2713aSLionel Sambuc // find the instantiation of the declaration D.
4500f4a2713aSLionel Sambuc
4501f4a2713aSLionel Sambuc // If our context used to be dependent, we may need to instantiate
4502f4a2713aSLionel Sambuc // it before performing lookup into that context.
4503f4a2713aSLionel Sambuc bool IsBeingInstantiated = false;
4504f4a2713aSLionel Sambuc if (CXXRecordDecl *Spec = dyn_cast<CXXRecordDecl>(ParentDC)) {
4505f4a2713aSLionel Sambuc if (!Spec->isDependentContext()) {
4506f4a2713aSLionel Sambuc QualType T = Context.getTypeDeclType(Spec);
4507f4a2713aSLionel Sambuc const RecordType *Tag = T->getAs<RecordType>();
4508f4a2713aSLionel Sambuc assert(Tag && "type of non-dependent record is not a RecordType");
4509f4a2713aSLionel Sambuc if (Tag->isBeingDefined())
4510f4a2713aSLionel Sambuc IsBeingInstantiated = true;
4511f4a2713aSLionel Sambuc if (!Tag->isBeingDefined() &&
4512f4a2713aSLionel Sambuc RequireCompleteType(Loc, T, diag::err_incomplete_type))
4513*0a6a1f1dSLionel Sambuc return nullptr;
4514f4a2713aSLionel Sambuc
4515f4a2713aSLionel Sambuc ParentDC = Tag->getDecl();
4516f4a2713aSLionel Sambuc }
4517f4a2713aSLionel Sambuc }
4518f4a2713aSLionel Sambuc
4519*0a6a1f1dSLionel Sambuc NamedDecl *Result = nullptr;
4520f4a2713aSLionel Sambuc if (D->getDeclName()) {
4521f4a2713aSLionel Sambuc DeclContext::lookup_result Found = ParentDC->lookup(D->getDeclName());
4522f4a2713aSLionel Sambuc Result = findInstantiationOf(Context, D, Found.begin(), Found.end());
4523f4a2713aSLionel Sambuc } else {
4524f4a2713aSLionel Sambuc // Since we don't have a name for the entity we're looking for,
4525f4a2713aSLionel Sambuc // our only option is to walk through all of the declarations to
4526f4a2713aSLionel Sambuc // find that name. This will occur in a few cases:
4527f4a2713aSLionel Sambuc //
4528f4a2713aSLionel Sambuc // - anonymous struct/union within a template
4529f4a2713aSLionel Sambuc // - unnamed class/struct/union/enum within a template
4530f4a2713aSLionel Sambuc //
4531f4a2713aSLionel Sambuc // FIXME: Find a better way to find these instantiations!
4532f4a2713aSLionel Sambuc Result = findInstantiationOf(Context, D,
4533f4a2713aSLionel Sambuc ParentDC->decls_begin(),
4534f4a2713aSLionel Sambuc ParentDC->decls_end());
4535f4a2713aSLionel Sambuc }
4536f4a2713aSLionel Sambuc
4537f4a2713aSLionel Sambuc if (!Result) {
4538f4a2713aSLionel Sambuc if (isa<UsingShadowDecl>(D)) {
4539f4a2713aSLionel Sambuc // UsingShadowDecls can instantiate to nothing because of using hiding.
4540f4a2713aSLionel Sambuc } else if (Diags.hasErrorOccurred()) {
4541f4a2713aSLionel Sambuc // We've already complained about something, so most likely this
4542f4a2713aSLionel Sambuc // declaration failed to instantiate. There's no point in complaining
4543f4a2713aSLionel Sambuc // further, since this is normal in invalid code.
4544f4a2713aSLionel Sambuc } else if (IsBeingInstantiated) {
4545f4a2713aSLionel Sambuc // The class in which this member exists is currently being
4546f4a2713aSLionel Sambuc // instantiated, and we haven't gotten around to instantiating this
4547f4a2713aSLionel Sambuc // member yet. This can happen when the code uses forward declarations
4548f4a2713aSLionel Sambuc // of member classes, and introduces ordering dependencies via
4549f4a2713aSLionel Sambuc // template instantiation.
4550f4a2713aSLionel Sambuc Diag(Loc, diag::err_member_not_yet_instantiated)
4551f4a2713aSLionel Sambuc << D->getDeclName()
4552f4a2713aSLionel Sambuc << Context.getTypeDeclType(cast<CXXRecordDecl>(ParentDC));
4553f4a2713aSLionel Sambuc Diag(D->getLocation(), diag::note_non_instantiated_member_here);
4554f4a2713aSLionel Sambuc } else if (EnumConstantDecl *ED = dyn_cast<EnumConstantDecl>(D)) {
4555f4a2713aSLionel Sambuc // This enumeration constant was found when the template was defined,
4556f4a2713aSLionel Sambuc // but can't be found in the instantiation. This can happen if an
4557f4a2713aSLionel Sambuc // unscoped enumeration member is explicitly specialized.
4558f4a2713aSLionel Sambuc EnumDecl *Enum = cast<EnumDecl>(ED->getLexicalDeclContext());
4559f4a2713aSLionel Sambuc EnumDecl *Spec = cast<EnumDecl>(FindInstantiatedDecl(Loc, Enum,
4560f4a2713aSLionel Sambuc TemplateArgs));
4561f4a2713aSLionel Sambuc assert(Spec->getTemplateSpecializationKind() ==
4562f4a2713aSLionel Sambuc TSK_ExplicitSpecialization);
4563f4a2713aSLionel Sambuc Diag(Loc, diag::err_enumerator_does_not_exist)
4564f4a2713aSLionel Sambuc << D->getDeclName()
4565f4a2713aSLionel Sambuc << Context.getTypeDeclType(cast<TypeDecl>(Spec->getDeclContext()));
4566f4a2713aSLionel Sambuc Diag(Spec->getLocation(), diag::note_enum_specialized_here)
4567f4a2713aSLionel Sambuc << Context.getTypeDeclType(Spec);
4568f4a2713aSLionel Sambuc } else {
4569f4a2713aSLionel Sambuc // We should have found something, but didn't.
4570f4a2713aSLionel Sambuc llvm_unreachable("Unable to find instantiation of declaration!");
4571f4a2713aSLionel Sambuc }
4572f4a2713aSLionel Sambuc }
4573f4a2713aSLionel Sambuc
4574f4a2713aSLionel Sambuc D = Result;
4575f4a2713aSLionel Sambuc }
4576f4a2713aSLionel Sambuc
4577f4a2713aSLionel Sambuc return D;
4578f4a2713aSLionel Sambuc }
4579f4a2713aSLionel Sambuc
4580f4a2713aSLionel Sambuc /// \brief Performs template instantiation for all implicit template
4581f4a2713aSLionel Sambuc /// instantiations we have seen until this point.
PerformPendingInstantiations(bool LocalOnly)4582f4a2713aSLionel Sambuc void Sema::PerformPendingInstantiations(bool LocalOnly) {
4583f4a2713aSLionel Sambuc while (!PendingLocalImplicitInstantiations.empty() ||
4584f4a2713aSLionel Sambuc (!LocalOnly && !PendingInstantiations.empty())) {
4585f4a2713aSLionel Sambuc PendingImplicitInstantiation Inst;
4586f4a2713aSLionel Sambuc
4587f4a2713aSLionel Sambuc if (PendingLocalImplicitInstantiations.empty()) {
4588f4a2713aSLionel Sambuc Inst = PendingInstantiations.front();
4589f4a2713aSLionel Sambuc PendingInstantiations.pop_front();
4590f4a2713aSLionel Sambuc } else {
4591f4a2713aSLionel Sambuc Inst = PendingLocalImplicitInstantiations.front();
4592f4a2713aSLionel Sambuc PendingLocalImplicitInstantiations.pop_front();
4593f4a2713aSLionel Sambuc }
4594f4a2713aSLionel Sambuc
4595f4a2713aSLionel Sambuc // Instantiate function definitions
4596f4a2713aSLionel Sambuc if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Inst.first)) {
4597f4a2713aSLionel Sambuc PrettyDeclStackTraceEntry CrashInfo(*this, Function, SourceLocation(),
4598f4a2713aSLionel Sambuc "instantiating function definition");
4599f4a2713aSLionel Sambuc bool DefinitionRequired = Function->getTemplateSpecializationKind() ==
4600f4a2713aSLionel Sambuc TSK_ExplicitInstantiationDefinition;
4601f4a2713aSLionel Sambuc InstantiateFunctionDefinition(/*FIXME:*/Inst.second, Function, true,
4602f4a2713aSLionel Sambuc DefinitionRequired);
4603f4a2713aSLionel Sambuc continue;
4604f4a2713aSLionel Sambuc }
4605f4a2713aSLionel Sambuc
4606f4a2713aSLionel Sambuc // Instantiate variable definitions
4607f4a2713aSLionel Sambuc VarDecl *Var = cast<VarDecl>(Inst.first);
4608f4a2713aSLionel Sambuc
4609f4a2713aSLionel Sambuc assert((Var->isStaticDataMember() ||
4610f4a2713aSLionel Sambuc isa<VarTemplateSpecializationDecl>(Var)) &&
4611f4a2713aSLionel Sambuc "Not a static data member, nor a variable template"
4612f4a2713aSLionel Sambuc " specialization?");
4613f4a2713aSLionel Sambuc
4614f4a2713aSLionel Sambuc // Don't try to instantiate declarations if the most recent redeclaration
4615f4a2713aSLionel Sambuc // is invalid.
4616f4a2713aSLionel Sambuc if (Var->getMostRecentDecl()->isInvalidDecl())
4617f4a2713aSLionel Sambuc continue;
4618f4a2713aSLionel Sambuc
4619f4a2713aSLionel Sambuc // Check if the most recent declaration has changed the specialization kind
4620f4a2713aSLionel Sambuc // and removed the need for implicit instantiation.
4621f4a2713aSLionel Sambuc switch (Var->getMostRecentDecl()->getTemplateSpecializationKind()) {
4622f4a2713aSLionel Sambuc case TSK_Undeclared:
4623f4a2713aSLionel Sambuc llvm_unreachable("Cannot instantitiate an undeclared specialization.");
4624f4a2713aSLionel Sambuc case TSK_ExplicitInstantiationDeclaration:
4625f4a2713aSLionel Sambuc case TSK_ExplicitSpecialization:
4626f4a2713aSLionel Sambuc continue; // No longer need to instantiate this type.
4627f4a2713aSLionel Sambuc case TSK_ExplicitInstantiationDefinition:
4628f4a2713aSLionel Sambuc // We only need an instantiation if the pending instantiation *is* the
4629f4a2713aSLionel Sambuc // explicit instantiation.
4630f4a2713aSLionel Sambuc if (Var != Var->getMostRecentDecl()) continue;
4631f4a2713aSLionel Sambuc case TSK_ImplicitInstantiation:
4632f4a2713aSLionel Sambuc break;
4633f4a2713aSLionel Sambuc }
4634f4a2713aSLionel Sambuc
4635f4a2713aSLionel Sambuc PrettyDeclStackTraceEntry CrashInfo(*this, Var, SourceLocation(),
4636f4a2713aSLionel Sambuc "instantiating variable definition");
4637f4a2713aSLionel Sambuc bool DefinitionRequired = Var->getTemplateSpecializationKind() ==
4638f4a2713aSLionel Sambuc TSK_ExplicitInstantiationDefinition;
4639f4a2713aSLionel Sambuc
4640f4a2713aSLionel Sambuc // Instantiate static data member definitions or variable template
4641f4a2713aSLionel Sambuc // specializations.
4642f4a2713aSLionel Sambuc InstantiateVariableDefinition(/*FIXME:*/ Inst.second, Var, true,
4643f4a2713aSLionel Sambuc DefinitionRequired);
4644f4a2713aSLionel Sambuc }
4645f4a2713aSLionel Sambuc }
4646f4a2713aSLionel Sambuc
PerformDependentDiagnostics(const DeclContext * Pattern,const MultiLevelTemplateArgumentList & TemplateArgs)4647f4a2713aSLionel Sambuc void Sema::PerformDependentDiagnostics(const DeclContext *Pattern,
4648f4a2713aSLionel Sambuc const MultiLevelTemplateArgumentList &TemplateArgs) {
4649*0a6a1f1dSLionel Sambuc for (auto DD : Pattern->ddiags()) {
4650f4a2713aSLionel Sambuc switch (DD->getKind()) {
4651f4a2713aSLionel Sambuc case DependentDiagnostic::Access:
4652f4a2713aSLionel Sambuc HandleDependentAccessCheck(*DD, TemplateArgs);
4653f4a2713aSLionel Sambuc break;
4654f4a2713aSLionel Sambuc }
4655f4a2713aSLionel Sambuc }
4656f4a2713aSLionel Sambuc }
4657