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