Lines Matching +full:x +full:- +full:tal
1 //===--- SemaTemplateInstantiateDecl.cpp - C++ Template Decl Instantiation ===/
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //===----------------------------------------------------------------------===/
10 //===----------------------------------------------------------------------===/
43 const DeclContext *DC = D->getDeclContext();
44 if (DC->isFunctionOrMethod())
47 if (DC->isRecord())
48 return cast<CXXRecordDecl>(DC)->isLocalClass();
56 if (!OldDecl->getQualifierLoc())
59 assert((NewDecl->getFriendObjectKind() ||
60 !OldDecl->getLexicalDeclContext()->isDependentContext()) &&
61 "non-friend with qualified name defined in dependent context");
64 const_cast<DeclContext *>(NewDecl->getFriendObjectKind()
65 ? NewDecl->getLexicalDeclContext()
66 : OldDecl->getLexicalDeclContext()));
69 = SemaRef.SubstNestedNameSpecifierLoc(OldDecl->getQualifierLoc(),
75 NewDecl->setQualifierInfo(NewQualifierLoc);
95 if (Aligned->isAlignmentExpr()) {
99 ExprResult Result = S.SubstExpr(Aligned->getAlignmentExpr(), TemplateArgs);
104 S.SubstType(Aligned->getAlignmentType(), TemplateArgs,
105 Aligned->getLocation(), DeclarationName())) {
106 if (!S.CheckAlignasTypeArgument(Aligned->getSpelling(), Result,
107 Aligned->getLocation(),
108 Result->getTypeLoc().getSourceRange()))
117 if (!Aligned->isPackExpansion()) {
123 if (Aligned->isAlignmentExpr())
124 S.collectUnexpandedParameterPacks(Aligned->getAlignmentExpr(),
127 S.collectUnexpandedParameterPacks(Aligned->getAlignmentType()->getTypeLoc(),
135 SourceLocation EllipsisLoc = Aligned->getLocation();
136 if (S.CheckParameterPacksForExpansion(EllipsisLoc, Aligned->getRange(),
142 Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(S, -1);
160 ExprResult Result = S.SubstExpr(Aligned->getAlignment(), TemplateArgs);
165 if (Aligned->getOffset()) {
166 Result = S.SubstExpr(Aligned->getOffset(), TemplateArgs);
181 ExprResult Result = S.SubstExpr(Aligned->getAlignment(), TemplateArgs);
191 llvm::APInt(64, Align->getParamIndex().getSourceIndex()),
192 S.getASTContext().UnsignedLongLongTy, Align->getLocation());
204 bool HasDelayedArgs = Attr->delayedArgs_size();
208 ? ArrayRef<Expr *>{Attr->delayedArgs_begin(), Attr->delayedArgs_end()}
209 : ArrayRef<Expr *>{Attr->args_begin(), Attr->args_end()};
216 StringRef Str = Attr->getAnnotation();
219 S.Diag(Attr->getLoc(), diag::err_attribute_too_few_arguments)
247 if (!Cond->isTypeDependent()) {
255 if (OldCond->isValueDependent() && !Cond->isValueDependent() &&
257 S.Diag(A->getLocation(), diag::err_attr_cond_never_constant_expr) << A;
269 S, TemplateArgs, EIA, EIA->getCond(), Tmpl, New);
272 New->addAttr(new (S.getASTContext()) EnableIfAttr(S.getASTContext(), *EIA,
273 Cond, EIA->getMessage()));
280 S, TemplateArgs, DIA, DIA->getCond(), Tmpl, New);
283 New->addAttr(new (S.getASTContext()) DiagnoseIfAttr(
284 S.getASTContext(), *DIA, Cond, DIA->getMessage(),
285 DIA->getDiagnosticType(), DIA->getArgDependent(), New));
335 New = FTD->getTemplatedDecl();
337 auto *ThisContext = dyn_cast_or_null<CXXRecordDecl>(FD->getDeclContext());
341 auto SubstExpr = [&](Expr *E) -> ExprResult {
342 if (auto *DRE = dyn_cast<DeclRefExpr>(E->IgnoreParenImpCasts()))
343 if (auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
346 if (FD->getNumParams() > PVD->getFunctionScopeIndex())
348 PVD, FD->getParamDecl(PVD->getFunctionScopeIndex()));
352 FD->isCXXInstanceMember());
356 // Substitute a single OpenMP clause, which is a potentially-evaluated
357 // full-expression.
358 auto Subst = [&](Expr *E) -> ExprResult {
418 New = FTD->getTemplatedDecl();
420 auto *ThisContext = dyn_cast_or_null<CXXRecordDecl>(FD->getDeclContext());
423 if (auto *DRE = dyn_cast<DeclRefExpr>(E->IgnoreParenImpCasts()))
424 if (auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
427 if (FD->getNumParams() > PVD->getFunctionScopeIndex())
429 PVD, FD->getParamDecl(PVD->getFunctionScopeIndex()));
433 FD->isCXXInstanceMember());
437 // Substitute a single OpenMP clause, which is a potentially-evaluated
438 // full-expression.
490 E = DeclVarData->second;
491 FD = DeclVarData->first;
493 if (auto *VariantDRE = dyn_cast<DeclRefExpr>(E->IgnoreParenImpCasts())) {
494 if (auto *VariantFD = dyn_cast<FunctionDecl>(VariantDRE->getDecl())) {
495 if (auto *VariantFTD = VariantFD->getDescribedFunctionTemplate()) {
496 if (!VariantFTD->isThisDeclarationADefinition())
499 const TemplateArgumentList *TAL = TemplateArgumentList::CreateCopy(
502 auto *SubstFD = S.InstantiateFunctionDeclaration(VariantFTD, TAL,
503 New->getLocation());
507 SubstFD->getType(), FD->getType(),
513 New->getLocation(), SubstFD, /* Recursive */ true,
515 SubstFD->setInstantiationIsPending(!SubstFD->isDefined());
519 /* NameLoc */ SubstFD->getLocation(),
520 SubstFD->getType(), ExprValueKind::VK_PRValue);
587 if (!Cond->isTypeDependent())
644 New->addAttr(Attr.clone(S.getASTContext()));
654 QualType T = PNA->getTypedefType();
656 if (!T->isDependentType() && !RD->isDependentContext() &&
657 !declaresSameEntity(T->getAsCXXRecordDecl(), RD))
659 for (const auto *ExistingPNA : D->specific_attrs<PreferredNameAttr>())
660 if (S.Context.hasSameType(ExistingPNA->getTypedefType(),
661 PNA->getTypedefType()))
668 switch (BA->getID()) {
673 if (FD && FD->getNumParams() >= 1 &&
674 FD->getParamDecl(0)->getType()->isRValueReferenceType() &&
675 FD->getReturnType()->isLValueReferenceType()) {
681 // HACK: Super-old versions of libc++ (3.1 and earlier) provide
685 if (FD && !FD->getReturnType()->isReferenceType())
698 P->addAttr(Attr->clone(S.getASTContext()));
699 P->setType(S.getASTContext().getLValueReferenceType(P->getType()));
710 for (const auto *TmplAttr : Tmpl->attrs()) {
717 *this, dyn_cast_or_null<CXXRecordDecl>(ND->getDeclContext()),
718 Qualifiers(), ND->isCXXInstanceMember());
723 New->addAttr(NewAttr);
730 switch (A->getKind()) {
746 for (const auto *TmplAttr : Tmpl->attrs()) {
752 if (Aligned && Aligned->isAlignmentDependent()) {
836 if (TmplAttr->getKind() == attr::DLLExport ||
837 TmplAttr->getKind() == attr::DLLImport) {
838 if (New->hasAttr<DLLExportAttr>() || New->hasAttr<DLLImportAttr>()) {
844 Swift().AddParameterABIAttr(New, *ABIAttr, ABIAttr->getABI());
857 if (!New->hasAttr<PointerAttr>())
858 New->addAttr(A->clone(Context));
863 if (!New->hasAttr<OwnerAttr>())
864 New->addAttr(A->clone(Context));
873 assert(!TmplAttr->isPackExpansion());
874 if (TmplAttr->isLateParsed() && LateAttrs) {
879 Saved = CurrentInstantiationScope->cloneScopes(OuterMostScope);
880 LateAttrs->push_back(LateInstantiatedAttribute(TmplAttr, Saved, New));
882 // Allow 'this' within late-parsed attributes.
884 auto *ThisContext = dyn_cast_or_null<CXXRecordDecl>(ND->getDeclContext());
886 ND->isCXXInstanceMember());
891 New->addAttr(NewAttr);
897 for (const auto *Attr : Pattern->attrs()) {
899 if (!Inst->hasAttr<StrictFPAttr>())
900 Inst->addAttr(A->clone(getASTContext()));
908 Ctor->isDefaultConstructor());
909 unsigned NumParams = Ctor->getNumParams();
912 DLLExportAttr *Attr = Ctor->getAttr<DLLExportAttr>();
916 (void)CheckCXXDefaultArgExpr(Attr->getLocation(), Ctor,
917 Ctor->getParamDecl(I));
928 DeclT *Result = D->getPreviousDecl();
933 if (Result && isa<CXXRecordDecl>(D->getDeclContext()) &&
934 D->getLexicalDeclContext() != Result->getLexicalDeclContext())
980 LabelDecl *Inst = LabelDecl::Create(SemaRef.Context, Owner, D->getLocation(),
981 D->getIdentifier());
982 Owner->addDecl(Inst);
995 D->getNamespaceLoc(),
996 D->getAliasLoc(),
997 D->getIdentifier(),
998 D->getQualifierLoc(),
999 D->getTargetNameLoc(),
1000 D->getNamespace());
1001 Owner->addDecl(Inst);
1008 TypeSourceInfo *DI = D->getTypeSourceInfo();
1009 if (DI->getType()->isInstantiationDependentType() ||
1010 DI->getType()->isVariablyModifiedType()) {
1012 D->getLocation(), D->getDeclName());
1018 SemaRef.MarkDeclarationsReferencedInType(D->getLocation(), DI->getType());
1021 // HACK: 2012-10-23 g++ has a bug where it gets the value kind of ?: wrong.
1025 // are fixed in g++ and libstdc++ 4.9.0 (2014-04-22).
1026 const DecltypeType *DT = DI->getType()->getAs<DecltypeType>();
1027 CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D->getDeclContext());
1028 if (DT && RD && isa<ConditionalOperator>(DT->getUnderlyingExpr()) &&
1029 DT->isReferenceType() &&
1030 RD->getEnclosingNamespaceContext() == SemaRef.getStdNamespace() &&
1031 RD->getIdentifier() && RD->getIdentifier()->isStr("common_type") &&
1032 D->getIdentifier() && D->getIdentifier()->isStr("type") &&
1033 SemaRef.getSourceManager().isInSystemHeader(D->getBeginLoc()))
1034 // Fold it to the (non-reference) type which g++ would have produced.
1036 DI->getType().getNonReferenceType());
1041 Typedef = TypeAliasDecl::Create(SemaRef.Context, Owner, D->getBeginLoc(),
1042 D->getLocation(), D->getIdentifier(), DI);
1044 Typedef = TypedefDecl::Create(SemaRef.Context, Owner, D->getBeginLoc(),
1045 D->getLocation(), D->getIdentifier(), DI);
1047 Typedef->setInvalidDecl();
1050 // tag decl, re-establish that relationship for the new typedef.
1051 if (const TagType *oldTagType = D->getUnderlyingType()->getAs<TagType>()) {
1052 TagDecl *oldTag = oldTagType->getDecl();
1053 if (oldTag->getTypedefNameForAnonDecl() == D && !Invalid) {
1054 TagDecl *newTag = DI->getType()->castAs<TagType>()->getDecl();
1055 assert(!newTag->hasNameForLinkage());
1056 newTag->setTypedefNameForAnonDecl(Typedef);
1061 NamedDecl *InstPrev = SemaRef.FindInstantiatedDecl(D->getLocation(), Prev,
1071 Typedef->setPreviousDecl(InstPrevTypedef);
1076 if (D->getUnderlyingType()->getAs<DependentNameType>())
1079 Typedef->setAccess(D->getAccess());
1080 Typedef->setReferenced(D->isReferenced());
1088 Owner->addDecl(Typedef);
1095 Owner->addDecl(Typedef);
1105 TemplateParameterList *TempParams = D->getTemplateParameters();
1110 TypeAliasDecl *Pattern = D->getTemplatedDecl();
1112 SemaRef, D->getBeginLoc(), D,
1113 D->getTemplateDepth() >= TemplateArgs.getNumLevels()
1115 : (TemplateArgs.begin() + TemplateArgs.getNumLevels() - 1 -
1116 D->getTemplateDepth())
1117 ->Args);
1123 DeclContext::lookup_result Found = Owner->lookup(Pattern->getDeclName());
1135 = TypeAliasTemplateDecl::Create(SemaRef.Context, Owner, D->getLocation(),
1136 D->getDeclName(), InstParams, AliasInst);
1137 AliasInst->setDescribedAliasTemplate(Inst);
1139 Inst->setPreviousDecl(PrevAliasTemplate);
1141 Inst->setAccess(D->getAccess());
1144 Inst->setInstantiatedFromMemberTemplate(D);
1153 Owner->addDecl(Inst);
1159 auto *NewBD = BindingDecl::Create(SemaRef.Context, Owner, D->getLocation(),
1160 D->getIdentifier());
1161 NewBD->setReferenced(D->isReferenced());
1162 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, NewBD);
1169 for (auto *OldBD : D->bindings())
1176 if (!NewDD || NewDD->isInvalidDecl())
1178 NewBD->setInvalidDecl();
1193 D->getTypeSourceInfo(), TemplateArgs, D->getTypeSpecStartLoc(),
1194 D->getDeclName(), /*AllowDeducedTST*/true);
1198 if (DI->getType()->isFunctionType()) {
1199 SemaRef.Diag(D->getLocation(), diag::err_variable_instantiates_to_function)
1200 << D->isStaticDataMember() << DI->getType();
1205 if (D->isLocalExternDecl())
1211 Var = DecompositionDecl::Create(SemaRef.Context, DC, D->getInnerLocStart(),
1212 D->getLocation(), DI->getType(), DI,
1213 D->getStorageClass(), *Bindings);
1215 Var = VarDecl::Create(SemaRef.Context, DC, D->getInnerLocStart(),
1216 D->getLocation(), D->getIdentifier(), DI->getType(),
1217 DI, D->getStorageClass());
1222 Var->setInvalidDecl();
1233 if (D->isNRVOVariable() && !Var->isInvalidDecl()) {
1236 RT = F->getReturnType();
1238 RT = cast<FunctionType>(SemaRef.getCurBlock()->FunctionType)
1239 ->getReturnType();
1248 // This might run through some VarDecls which were returned from non-taken
1256 Var->setNRVOVariable(NRVO);
1259 Var->setImplicit(D->isImplicit());
1261 if (Var->isStaticLocal())
1264 if (Var->getTLSKind())
1272 = AccessSpecDecl::Create(SemaRef.Context, D->getAccess(), Owner,
1273 D->getAccessSpecifierLoc(), D->getColonLoc());
1274 Owner->addHiddenDecl(AD);
1280 TypeSourceInfo *DI = D->getTypeSourceInfo();
1281 if (DI->getType()->isInstantiationDependentType() ||
1282 DI->getType()->isVariablyModifiedType()) {
1284 D->getLocation(), D->getDeclName());
1286 DI = D->getTypeSourceInfo();
1288 } else if (DI->getType()->isFunctionType()) {
1291 // dependent on a template-parameter and this causes a
1294 // ill-formed.
1295 SemaRef.Diag(D->getLocation(), diag::err_field_instantiates_to_function)
1296 << DI->getType();
1300 SemaRef.MarkDeclarationsReferencedInType(D->getLocation(), DI->getType());
1303 Expr *BitWidth = D->getBitWidth();
1307 // The bit-width expression is a constant expression.
1320 FieldDecl *Field = SemaRef.CheckFieldDecl(D->getDeclName(),
1321 DI->getType(), DI,
1323 D->getLocation(),
1324 D->isMutable(),
1326 D->getInClassInitStyle(),
1327 D->getInnerLocStart(),
1328 D->getAccess(),
1331 cast<Decl>(Owner)->setInvalidDecl();
1337 if (Field->hasAttrs())
1341 Field->setInvalidDecl();
1343 if (!Field->getDeclName()) {
1347 if (CXXRecordDecl *Parent= dyn_cast<CXXRecordDecl>(Field->getDeclContext())) {
1348 if (Parent->isAnonymousStructOrUnion() &&
1349 Parent->getRedeclContext()->isFunctionOrMethod())
1350 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Field);
1353 Field->setImplicit(D->isImplicit());
1354 Field->setAccess(D->getAccess());
1355 Owner->addDecl(Field);
1362 TypeSourceInfo *DI = D->getTypeSourceInfo();
1364 if (DI->getType()->isVariablyModifiedType()) {
1365 SemaRef.Diag(D->getLocation(), diag::err_property_is_variably_modified)
1368 } else if (DI->getType()->isInstantiationDependentType()) {
1370 D->getLocation(), D->getDeclName());
1372 DI = D->getTypeSourceInfo();
1374 } else if (DI->getType()->isFunctionType()) {
1377 // dependent on a template-parameter and this causes a
1380 // ill-formed.
1381 SemaRef.Diag(D->getLocation(), diag::err_field_instantiates_to_function)
1382 << DI->getType();
1386 SemaRef.MarkDeclarationsReferencedInType(D->getLocation(), DI->getType());
1390 SemaRef.Context, Owner, D->getLocation(), D->getDeclName(), DI->getType(),
1391 DI, D->getBeginLoc(), D->getGetterId(), D->getSetterId());
1397 Property->setInvalidDecl();
1399 Property->setAccess(D->getAccess());
1400 Owner->addDecl(Property);
1407 new (SemaRef.Context)NamedDecl*[D->getChainingSize()];
1410 for (auto *PI : D->chain()) {
1411 NamedDecl *Next = SemaRef.FindInstantiatedDecl(D->getLocation(), PI,
1419 QualType T = cast<FieldDecl>(NamedChain[i-1])->getType();
1421 SemaRef.Context, Owner, D->getLocation(), D->getIdentifier(), T,
1422 {NamedChain, D->getChainingSize()});
1424 for (const auto *Attr : D->attrs())
1425 IndirectField->addAttr(Attr->clone(SemaRef.Context));
1427 IndirectField->setImplicit(D->isImplicit());
1428 IndirectField->setAccess(D->getAccess());
1429 Owner->addDecl(IndirectField);
1436 if (TypeSourceInfo *Ty = D->getFriendType()) {
1442 if (D->isUnsupportedFriend()) {
1446 D->getLocation(), DeclarationName());
1452 SemaRef.Context, Owner, D->getLocation(), InstTy, D->getFriendLoc());
1453 FD->setAccess(AS_public);
1454 FD->setUnsupportedFriend(D->isUnsupportedFriend());
1455 Owner->addDecl(FD);
1459 NamedDecl *ND = D->getFriendDecl();
1470 FriendDecl::Create(SemaRef.Context, Owner, D->getLocation(),
1471 cast<NamedDecl>(NewND), D->getFriendLoc());
1472 FD->setAccess(AS_public);
1473 FD->setUnsupportedFriend(D->isUnsupportedFriend());
1474 Owner->addDecl(FD);
1479 Expr *AssertExpr = D->getAssertExpr();
1491 SemaRef.SubstExpr(D->getMessage(), TemplateArgs);
1496 D->getLocation(), InstantiatedAssertExpr.get(),
1497 InstantiatedMessageExpr.get(), D->getRParenLoc(), D->isFailed());
1503 NamedDecl *Prev = SemaRef.FindInstantiatedDecl(D->getLocation(),
1511 EnumDecl::Create(SemaRef.Context, Owner, D->getBeginLoc(),
1512 D->getLocation(), D->getIdentifier(), PrevDecl,
1513 D->isScoped(), D->isScopedUsingClassTag(), D->isFixed());
1514 if (D->isFixed()) {
1515 if (TypeSourceInfo *TI = D->getIntegerTypeSourceInfo()) {
1519 SourceLocation UnderlyingLoc = TI->getTypeLoc().getBeginLoc();
1523 Enum->setIntegerType(SemaRef.Context.IntTy);
1525 Enum->setIntegerTypeSourceInfo(NewTI);
1527 assert(!D->getIntegerType()->isDependentType()
1529 Enum->setIntegerType(D->getIntegerType());
1535 Enum->setInstantiationOfMemberEnum(D, TSK_ImplicitInstantiation);
1536 Enum->setAccess(D->getAccess());
1548 Owner->addDecl(Enum);
1550 EnumDecl *Def = D->getDefinition();
1552 // If this is an out-of-line definition of an enum member template, check
1555 if (TypeSourceInfo *TI = Def->getIntegerTypeSourceInfo()) {
1556 SourceLocation UnderlyingLoc = TI->getTypeLoc().getBeginLoc();
1558 SemaRef.SubstType(TI->getType(), TemplateArgs,
1560 SemaRef.CheckEnumRedeclaration(Def->getLocation(), Def->isScoped(),
1572 if (isDeclWithinFunction(D) ? D == Def : Def && !Enum->isScoped()) {
1573 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Enum);
1582 Enum->startDefinition();
1585 Enum->setLocation(Pattern->getLocation());
1590 for (auto *EC : Pattern->enumerators()) {
1593 if (Expr *UninstValue = EC->getInitExpr()) {
1610 EC->getLocation(), EC->getIdentifier(),
1615 EnumConst->setInvalidDecl();
1616 Enum->setInvalidDecl();
1622 EnumConst->setAccess(Enum->getAccess());
1623 Enum->addDecl(EnumConst);
1627 if (Pattern->getDeclContext()->isFunctionOrMethod() &&
1628 !Enum->isScoped()) {
1631 SemaRef.CurrentInstantiationScope->InstantiatedLocal(EC, EnumConst);
1636 SemaRef.ActOnEnumBody(Enum->getLocation(), Enum->getBraceRange(), Enum,
1650 bool isFriend = (D->getFriendObjectKind() != Decl::FOK_None);
1655 TemplateParameterList *TempParams = D->getTemplateParameters();
1660 CXXRecordDecl *Pattern = D->getTemplatedDecl();
1665 NestedNameSpecifierLoc QualifierLoc = Pattern->getQualifierLoc();
1677 DeclContext::lookup_result Found = Owner->lookup(Pattern->getDeclName());
1681 PrevDecl = PrevClassTemplate->getTemplatedDecl();
1697 DC = SemaRef.FindInstantiatedContext(Pattern->getLocation(),
1698 Pattern->getDeclContext(),
1704 LookupResult R(SemaRef, Pattern->getDeclName(), Pattern->getLocation(),
1712 PrevDecl = PrevClassTemplate->getTemplatedDecl();
1716 SemaRef.Diag(Pattern->getLocation(), diag::err_not_tag_in_scope)
1717 << llvm::to_underlying(D->getTemplatedDecl()->getTagKind())
1718 << Pattern->getDeclName() << DC << QualifierLoc.getSourceRange();
1724 SemaRef.Context, Pattern->getTagKind(), DC, Pattern->getBeginLoc(),
1725 Pattern->getLocation(), Pattern->getIdentifier(), PrevDecl,
1728 RecordInst->setQualifierInfo(QualifierLoc);
1734 = ClassTemplateDecl::Create(SemaRef.Context, DC, D->getLocation(),
1735 D->getIdentifier(), InstParams, RecordInst);
1736 RecordInst->setDescribedClassTemplate(Inst);
1739 assert(!Owner->isDependentContext());
1740 Inst->setLexicalDeclContext(Owner);
1741 RecordInst->setLexicalDeclContext(Owner);
1742 Inst->setObjectOfFriendDecl();
1745 Inst->setCommonPtr(PrevClassTemplate->getCommonPtr());
1746 RecordInst->setTypeForDecl(
1747 PrevClassTemplate->getTemplatedDecl()->getTypeForDecl());
1749 PrevClassTemplate->getMostRecentDecl();
1751 MostRecentPrevCT->getTemplateParameters();
1755 RecordInst, InstParams, MostRecentPrevCT->getTemplatedDecl(),
1765 Inst->setAccess(PrevClassTemplate->getAccess());
1767 Inst->setAccess(D->getAccess());
1770 Inst->setObjectOfFriendDecl();
1774 Inst->setAccess(D->getAccess());
1776 Inst->setInstantiatedFromMemberTemplate(D);
1779 Inst->setPreviousDecl(PrevClassTemplate);
1783 RecordInst, Inst->getInjectedClassNameSpecialization());
1787 DC->makeDeclVisibleInContext(Inst);
1791 if (D->isOutOfLine()) {
1792 Inst->setLexicalDeclContext(D->getLexicalDeclContext());
1793 RecordInst->setLexicalDeclContext(D->getLexicalDeclContext());
1796 Owner->addDecl(Inst);
1799 // Queue up any out-of-line partial specializations of this member
1803 D->getPartialSpecializations(PartialSpecs);
1805 if (PartialSpecs[I]->getFirstDecl()->isOutOfLine())
1815 ClassTemplateDecl *ClassTemplate = D->getSpecializedTemplate();
1817 // Lookup the already-instantiated declaration in the instantiation
1820 = Owner->lookup(ClassTemplate->getDeclName());
1830 = InstClassTemplate->findPartialSpecInstantiatedFromMember(D))
1837 assert(D->getTemplatedDecl()->isStaticDataMember() &&
1843 TemplateParameterList *TempParams = D->getTemplateParameters();
1848 VarDecl *Pattern = D->getTemplatedDecl();
1852 DeclContext::lookup_result Found = Owner->lookup(Pattern->getDeclName());
1865 SemaRef.Context, DC, D->getLocation(), D->getIdentifier(), InstParams,
1867 VarInst->setDescribedVarTemplate(Inst);
1868 Inst->setPreviousDecl(PrevVarTemplate);
1870 Inst->setAccess(D->getAccess());
1872 Inst->setInstantiatedFromMemberTemplate(D);
1874 if (D->isOutOfLine()) {
1875 Inst->setLexicalDeclContext(D->getLexicalDeclContext());
1876 VarInst->setLexicalDeclContext(D->getLexicalDeclContext());
1879 Owner->addDecl(Inst);
1882 // Queue up any out-of-line partial specializations of this member
1886 D->getPartialSpecializations(PartialSpecs);
1888 if (PartialSpecs[I]->getFirstDecl()->isOutOfLine())
1898 assert(D->isStaticDataMember() &&
1901 VarTemplateDecl *VarTemplate = D->getSpecializedTemplate();
1903 // Lookup the already-instantiated declaration and return that.
1904 DeclContext::lookup_result Found = Owner->lookup(VarTemplate->getDeclName());
1911 InstVarTemplate->findPartialSpecInstantiatedFromMember(D))
1926 TemplateParameterList *TempParams = D->getTemplateParameters();
1932 if (CXXMethodDecl *DMethod = dyn_cast<CXXMethodDecl>(D->getTemplatedDecl()))
1937 D->getTemplatedDecl(),
1946 = Instantiated->getDescribedFunctionTemplate();
1947 InstTemplate->setAccess(D->getAccess());
1951 bool isFriend = (InstTemplate->getFriendObjectKind() != Decl::FOK_None);
1954 // non-definition friend declaration.
1955 if (!InstTemplate->getInstantiatedFromMemberTemplate() &&
1956 !(isFriend && !D->getTemplatedDecl()->isThisDeclarationADefinition()))
1957 InstTemplate->setInstantiatedFromMemberTemplate(D);
1961 Owner->addDecl(InstTemplate);
1962 } else if (InstTemplate->getDeclContext()->isRecord() &&
1973 NamedDecl *Prev = SemaRef.FindInstantiatedDecl(D->getLocation(),
1981 bool IsInjectedClassName = D->isInjectedClassName();
1982 if (D->isLambda())
1984 SemaRef.Context, Owner, D->getLambdaTypeInfo(), D->getLocation(),
1985 D->getLambdaDependencyKind(), D->isGenericLambda(),
1986 D->getLambdaCaptureDefault());
1988 Record = CXXRecordDecl::Create(SemaRef.Context, D->getTagKind(), Owner,
1989 D->getBeginLoc(), D->getLocation(),
1990 D->getIdentifier(), PrevDecl,
1992 // Link the type of the injected-class-name to that of the outer class.
2003 Record->setImplicit(D->isImplicit());
2007 if (D->getAccess() != AS_none)
2008 Record->setAccess(D->getAccess());
2010 Record->setInstantiationOfMemberClass(D, TSK_ImplicitInstantiation);
2014 if (D->getFriendObjectKind())
2015 Record->setObjectOfFriendDecl();
2018 if (D->isAnonymousStructOrUnion())
2019 Record->setAnonymousStructOrUnion(true);
2021 if (D->isLocalClass())
2022 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Record);
2038 Owner->addDecl(Record);
2042 if (D->isCompleteDefinition() && D->isLocalClass()) {
2045 SemaRef.InstantiateClass(D->getLocation(), Record, D, TemplateArgs,
2050 // reach the end of the outermost (non-nested) local class.
2051 if (!D->isCXXClassMember())
2052 SemaRef.InstantiateClassMembers(D->getLocation(), Record, TemplateArgs,
2063 assert(Record->isInjectedClassName() && "Broken injected-class-name");
2070 /// aren't reflected in the type-source information.
2073 /// \param TInfo The already-instantiated type.
2078 = D->getType()->castAs<FunctionProtoType>();
2080 = TInfo->getType()->castAs<FunctionProtoType>();
2081 if (OrigFunc->getExtInfo() == NewFunc->getExtInfo())
2082 return TInfo->getType();
2084 FunctionProtoType::ExtProtoInfo NewEPI = NewFunc->getExtProtoInfo();
2085 NewEPI.ExtInfo = OrigFunc->getExtInfo();
2086 return Context.getFunctionType(NewFunc->getReturnType(),
2087 NewFunc->getParamTypes(), NewEPI);
2100 FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
2106 = FunctionTemplate->findSpecialization(Innermost, InsertPos);
2115 isFriend = (FunctionTemplate->getFriendObjectKind() != Decl::FOK_None);
2117 isFriend = (D->getFriendObjectKind() != Decl::FOK_None);
2120 Owner->isFunctionOrMethod() ||
2122 cast<Decl>(Owner)->isDefinedOutsideFunctionOrMethod());
2128 TemplateArgs, DGuide->getExplicitSpecifier());
2139 if (TemplateParams && TemplateParams->size()) {
2141 dyn_cast<TemplateTypeParmDecl>(TemplateParams->asArray().back());
2142 if (LastParam && LastParam->isImplicit() &&
2143 LastParam->hasTypeConstraint()) {
2144 // In abbreviated templates, the type-constraints of invented template
2150 SemaRef.Context, TemplateParams->getTemplateLoc(),
2151 TemplateParams->getLAngleLoc(), TemplateParams->asArray(),
2152 TemplateParams->getRAngleLoc(), TemplateParams->getRequiresClause());
2156 NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc();
2164 Expr *TrailingRequiresClause = D->getTrailingRequiresClause();
2170 if (D->isLocalExternDecl()) {
2179 DC = SemaRef.FindInstantiatedContext(D->getLocation(), D->getDeclContext(),
2184 = SemaRef.SubstDeclarationNameInfo(D->getNameInfo(), TemplateArgs);
2192 SemaRef.Context, DC, D->getInnerLocStart(),
2194 D->getSourceRange().getEnd(), DGuide->getCorrespondingConstructor(),
2195 DGuide->getDeductionCandidateKind());
2196 Function->setAccess(D->getAccess());
2199 SemaRef.Context, DC, D->getInnerLocStart(), NameInfo, T, TInfo,
2200 D->getCanonicalDecl()->getStorageClass(), D->UsesFPIntrin(),
2201 D->isInlineSpecified(), D->hasWrittenPrototype(), D->getConstexprKind(),
2203 Function->setFriendConstraintRefersToEnclosingTemplate(
2204 D->FriendConstraintRefersToEnclosingTemplate());
2205 Function->setRangeEnd(D->getSourceRange().getEnd());
2208 if (D->isInlined())
2209 Function->setImplicitlyInline();
2212 Function->setQualifierInfo(QualifierLoc);
2214 if (D->isLocalExternDecl())
2215 Function->setLocalExternDecl();
2218 if (!isFriend && D->isOutOfLine() && !D->isLocalExternDecl()) {
2219 assert(D->getDeclContext()->isFileContext());
2220 LexicalDC = D->getDeclContext();
2222 else if (D->isLocalExternDecl()) {
2226 Function->setLexicalDeclContext(LexicalDC);
2231 Params[P]->setOwningFunction(Function);
2232 Function->setParams(Params);
2235 Function->setTrailingRequiresClause(TrailingRequiresClause);
2242 // struct X {
2246 // X<int> x;
2248 // We are instantiating the friend function template "f" within X<int>,
2253 Function->getLocation(),
2254 Function->getDeclName(),
2256 Function->setDescribedFunctionTemplate(FunctionTemplate);
2258 FunctionTemplate->setLexicalDeclContext(LexicalDC);
2260 if (isFriend && D->isThisDeclarationADefinition()) {
2261 FunctionTemplate->setInstantiatedFromMemberTemplate(
2262 D->getDescribedFunctionTemplate());
2269 Function->setFunctionTemplateSpecialization(FunctionTemplate,
2274 if (isFriend && D->isThisDeclarationADefinition()) {
2276 // definition. We don't want non-template functions to be marked as being
2278 Function->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation);
2283 Function->setInstantiatedFromDecl(D);
2288 Function->setObjectOfFriendDecl();
2289 if (FunctionTemplateDecl *FT = Function->getDescribedFunctionTemplate())
2290 FT->setObjectOfFriendDecl();
2294 Function->setInvalidDecl();
2299 SemaRef, Function->getDeclName(), SourceLocation(),
2300 D->isLocalExternDecl() ? Sema::LookupRedeclarationWithLinkage
2302 D->isLocalExternDecl() ? RedeclarationKind::ForExternalRedeclaration
2306 D->getDependentSpecializationInfo()) {
2308 "non-member non-friend function?");
2312 if (const auto *ArgsWritten = DFTSI->TemplateArgumentsAsWritten) {
2313 ExplicitArgs.setLAngleLoc(ArgsWritten->getLAngleLoc());
2314 ExplicitArgs.setRAngleLoc(ArgsWritten->getRAngleLoc());
2315 if (SemaRef.SubstTemplateArguments(ArgsWritten->arguments(), TemplateArgs,
2321 for (FunctionTemplateDecl *FTD : DFTSI->getCandidates()) {
2323 SemaRef.FindInstantiatedDecl(D->getLocation(), FTD, TemplateArgs))
2331 DFTSI->TemplateArgumentsAsWritten ? &ExplicitArgs : nullptr,
2333 Function->setInvalidDecl();
2337 D->getTemplateSpecializationArgsAsWritten()) {
2338 // The name of this function was written as a template-id.
2342 TemplateArgumentListInfo ExplicitArgs(ArgsWritten->getLAngleLoc(),
2343 ArgsWritten->getRAngleLoc());
2344 if (SemaRef.SubstTemplateArguments(ArgsWritten->arguments(), TemplateArgs,
2351 Function->setInvalidDecl();
2358 SemaRef.LookupQualifiedName(Previous, DC->getRedeclContext());
2390 if (Function->isLocalExternDecl()) {
2391 for (ParmVarDecl *PVD : Function->parameters()) {
2392 if (!PVD->hasDefaultArg())
2394 if (SemaRef.SubstDefaultArgument(D->getInnerLocStart(), PVD, TemplateArgs)) {
2398 Expr *UninstExpr = PVD->getUninstantiatedDefaultArg();
2400 UninstExpr->getBeginLoc(), UninstExpr->getEndLoc(),
2401 { UninstExpr }, UninstExpr->getType());
2403 PVD->setDefaultArg(ErrorResult.get());
2410 Function->isThisDeclarationADefinition());
2416 if (isFriend && TemplateParams && FunctionTemplate->getPreviousDecl()) {
2419 FunctionTemplate->getPreviousDecl()->getTemplateParameters(),
2420 Function->isThisDeclarationADefinition()
2429 if (isFriend && D->isThisDeclarationADefinition() && Function->isUsed(false)) {
2431 Function->getMemberSpecializationInfo()) {
2432 if (MSInfo->getPointOfInstantiation().isInvalid()) {
2433 SourceLocation Loc = D->getLocation(); // FIXME
2434 MSInfo->setPointOfInstantiation(Loc);
2441 if (D->isExplicitlyDefaulted()) {
2445 if (D->isDeleted())
2446 SemaRef.SetDeclDeleted(Function, D->getLocation(), D->getDeletedMessage());
2454 (Function->isLocalExternDecl() && !Function->getPreviousDecl()))
2455 DC->makeDeclVisibleInContext(PrincipalDecl);
2457 if (Function->isOverloadedOperator() && !DC->isRecord() &&
2458 PrincipalDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary))
2459 PrincipalDecl->setNonMemberOperator();
2467 FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
2476 = FunctionTemplate->findSpecialization(Innermost, InsertPos);
2485 isFriend = (FunctionTemplate->getFriendObjectKind() != Decl::FOK_None);
2487 isFriend = (D->getFriendObjectKind() != Decl::FOK_None);
2491 cast<Decl>(Owner)->isDefinedOutsideFunctionOrMethod());
2500 if (isFriend && (NumTempParamLists = D->getNumTemplateParameterLists())) {
2503 TemplateParameterList *TempParams = D->getTemplateParameterList(I);
2515 !InstantiatedExplicitSpecifier.getExpr()->isValueDependent();
2517 // Delay the instantiation of the explicit-specifier until after the
2534 // conditionally populate the TSI without breaking non-template related use
2537 if (cast<CXXRecordDecl>(D->getParent())->isLambda() &&
2538 !D->getTypeSourceInfo() &&
2541 SemaRef.Context.getTrivialTypeSourceInfo(D->getType());
2542 D->setTypeSourceInfo(TSI);
2551 if (TemplateParams && TemplateParams->size()) {
2553 dyn_cast<TemplateTypeParmDecl>(TemplateParams->asArray().back());
2554 if (LastParam && LastParam->isImplicit() &&
2555 LastParam->hasTypeConstraint()) {
2556 // In abbreviated templates, the type-constraints of invented template
2562 SemaRef.Context, TemplateParams->getTemplateLoc(),
2563 TemplateParams->getLAngleLoc(), TemplateParams->asArray(),
2564 TemplateParams->getRAngleLoc(), TemplateParams->getRequiresClause());
2568 NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc();
2586 DC = SemaRef.FindInstantiatedContext(D->getLocation(),
2587 D->getDeclContext(),
2594 Expr *TrailingRequiresClause = D->getTrailingRequiresClause();
2597 = SemaRef.SubstDeclarationNameInfo(D->getNameInfo(), TemplateArgs);
2605 SourceLocation StartLoc = D->getInnerLocStart();
2609 InstantiatedExplicitSpecifier, Constructor->UsesFPIntrin(),
2610 Constructor->isInlineSpecified(), false,
2611 Constructor->getConstexprKind(), InheritedConstructor(),
2613 Method->setRangeEnd(Constructor->getEndLoc());
2617 Destructor->UsesFPIntrin(), Destructor->isInlineSpecified(), false,
2618 Destructor->getConstexprKind(), TrailingRequiresClause);
2619 Method->setIneligibleOrNotSelected(true);
2620 Method->setRangeEnd(Destructor->getEndLoc());
2621 Method->setDeclName(SemaRef.Context.DeclarationNames.getCXXDestructorName(
2627 Conversion->UsesFPIntrin(), Conversion->isInlineSpecified(),
2628 InstantiatedExplicitSpecifier, Conversion->getConstexprKind(),
2629 Conversion->getEndLoc(), TrailingRequiresClause);
2631 StorageClass SC = D->isStatic() ? SC_Static : SC_None;
2634 D->UsesFPIntrin(), D->isInlineSpecified(), D->getConstexprKind(),
2635 D->getEndLoc(), TrailingRequiresClause);
2638 if (D->isInlined())
2639 Method->setImplicitlyInline();
2642 Method->setQualifierInfo(QualifierLoc);
2649 // struct X {
2653 // X<int> x;
2655 // We are instantiating the member template "f" within X<int>, which means
2659 Method->getLocation(),
2660 Method->getDeclName(),
2663 FunctionTemplate->setLexicalDeclContext(Owner);
2664 FunctionTemplate->setObjectOfFriendDecl();
2665 } else if (D->isOutOfLine())
2666 FunctionTemplate->setLexicalDeclContext(D->getLexicalDeclContext());
2667 Method->setDescribedFunctionTemplate(FunctionTemplate);
2671 Method->setFunctionTemplateSpecialization(FunctionTemplate,
2677 Method->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation);
2681 // out-of-line, the instantiation will have the same lexical
2685 Method->setTemplateParameterListsInfo(
2689 Method->setLexicalDeclContext(Owner);
2690 Method->setObjectOfFriendDecl();
2691 } else if (D->isOutOfLine())
2692 Method->setLexicalDeclContext(D->getLexicalDeclContext());
2696 Params[P]->setOwningFunction(Method);
2697 Method->setParams(Params);
2700 Method->setInvalidDecl();
2707 // If the name of this function was written as a template-id, instantiate
2710 D->getDependentSpecializationInfo()) {
2713 if (const auto *ArgsWritten = DFTSI->TemplateArgumentsAsWritten) {
2714 ExplicitArgs.setLAngleLoc(ArgsWritten->getLAngleLoc());
2715 ExplicitArgs.setRAngleLoc(ArgsWritten->getRAngleLoc());
2716 if (SemaRef.SubstTemplateArguments(ArgsWritten->arguments(), TemplateArgs,
2722 for (FunctionTemplateDecl *FTD : DFTSI->getCandidates()) {
2724 SemaRef.FindInstantiatedDecl(D->getLocation(), FTD, TemplateArgs))
2731 Method, DFTSI->TemplateArgumentsAsWritten ? &ExplicitArgs : nullptr,
2733 Method->setInvalidDecl();
2737 D->getTemplateSpecializationArgsAsWritten()) {
2740 TemplateArgumentListInfo ExplicitArgs(ArgsWritten->getLAngleLoc(),
2741 ArgsWritten->getRAngleLoc());
2743 if (SemaRef.SubstTemplateArguments(ArgsWritten->arguments(), TemplateArgs,
2750 Method->setInvalidDecl();
2779 if (D->isInLocalScopeForInstantiation()) {
2781 if (!Params[P]->hasDefaultArg())
2787 Expr *UninstExpr = Params[P]->getUninstantiatedDefaultArg();
2789 UninstExpr->getBeginLoc(), UninstExpr->getEndLoc(),
2790 { UninstExpr }, UninstExpr->getType());
2792 Params[P]->setDefaultArg(ErrorResult.get());
2799 Method->isThisDeclarationADefinition());
2801 if (D->isPureVirtual())
2804 // Propagate access. For a non-friend declaration, the access is
2807 if (isFriend && Method->getPreviousDecl())
2808 Method->setAccess(Method->getPreviousDecl()->getAccess());
2810 Method->setAccess(D->getAccess());
2812 FunctionTemplate->setAccess(Method->getAccess());
2817 if (D->isExplicitlyDefaulted()) {
2821 if (D->isDeletedAsWritten())
2822 SemaRef.SetDeclDeleted(Method, Method->getLocation(),
2823 D->getDeletedMessage());
2825 // If this is an explicit specialization, mark the implicitly-instantiated
2832 // ineligible so that Owner->addDecl() won't mark the class as non trivial.
2839 if (Constructor->isDefaultConstructor() ||
2840 Constructor->isCopyOrMoveConstructor())
2841 Method->setIneligibleOrNotSelected(true);
2842 } else if (Method->isCopyAssignmentOperator() ||
2843 Method->isMoveAssignmentOperator()) {
2844 Method->setIneligibleOrNotSelected(true);
2852 } else if (Method->isInvalidDecl() && !Previous.empty()) {
2857 // We only need to re-check access for methods which we didn't
2859 if (!D->getPreviousDecl())
2862 Record->makeDeclVisibleInContext(Method);
2865 // class-scope specializations because we'll have matched them with
2868 Owner->addDecl(Method);
2873 if (Method->hasAttr<UsedAttr>()) {
2877 A->getMemberSpecializationInfo())
2878 Loc = MSInfo->getPointOfInstantiation();
2880 Loc = Spec->getPointOfInstantiation();
2908 assert(D->getTypeForDecl()->isTemplateTypeParmType());
2912 if (const TypeConstraint *TC = D->getTypeConstraint()) {
2913 if (D->isPackExpansion() && !D->isExpandedParameterPack()) {
2914 assert(TC->getTemplateArgsAsWritten() &&
2921 for (auto &ArgLoc : TC->getTemplateArgsAsWritten()->arguments())
2929 cast<CXXFoldExpr>(TC->getImmediatelyDeclaredConstraint())
2930 ->getEllipsisLoc(),
2931 SourceRange(TC->getConceptNameLoc(),
2932 TC->hasExplicitTemplateArgs() ?
2933 TC->getTemplateArgsAsWritten()->getRAngleLoc() :
2934 TC->getConceptNameInfo().getEndLoc()),
2941 SemaRef.Context, Owner, D->getBeginLoc(), D->getLocation(),
2942 D->getDepth() - TemplateArgs.getNumSubstitutedLevels(), D->getIndex(),
2943 D->getIdentifier(), D->wasDeclaredWithTypename(), D->isParameterPack(),
2944 D->hasTypeConstraint(), NumExpanded);
2946 Inst->setAccess(AS_public);
2947 Inst->setImplicit(D->isImplicit());
2948 if (auto *TC = D->getTypeConstraint()) {
2949 if (!D->isImplicit()) {
2951 // with the corresponding auto-typed parameter as it might reference
2958 if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited()) {
2960 if (!SemaRef.SubstTemplateArgument(D->getDefaultArgument(), TemplateArgs,
2962 Inst->setDefaultArgument(SemaRef.getASTContext(), Output);
2967 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Inst);
2974 // Substitute into the type of the non-type template parameter.
2975 TypeLoc TL = D->getTypeSourceInfo()->getTypeLoc();
2983 if (D->isExpandedParameterPack()) {
2984 // The non-type template parameter pack is an already-expanded pack
2986 ExpandedParameterPackTypes.reserve(D->getNumExpansionTypes());
2987 ExpandedParameterPackTypesAsWritten.reserve(D->getNumExpansionTypes());
2988 for (unsigned I = 0, N = D->getNumExpansionTypes(); I != N; ++I) {
2990 SemaRef.SubstType(D->getExpansionTypeSourceInfo(I), TemplateArgs,
2991 D->getLocation(), D->getDeclName());
2996 SemaRef.CheckNonTypeTemplateParameterType(NewDI, D->getLocation());
3005 DI = D->getTypeSourceInfo();
3006 T = DI->getType();
3007 } else if (D->isPackExpansion()) {
3008 // The non-type template parameter pack's type is a pack expansion of types.
3021 Expansion.getTypePtr()->getNumExpansions();
3035 D->getLocation(),
3036 D->getDeclName());
3041 SemaRef.CheckNonTypeTemplateParameterType(NewDI, D->getLocation());
3051 // will end up using the expanded parameter pack types for type-checking.
3053 DI = D->getTypeSourceInfo();
3054 T = DI->getType();
3058 Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, -1);
3060 D->getLocation(),
3061 D->getDeclName());
3065 SemaRef.CheckNonTypeTemplateParameterType(NewPattern, D->getLocation());
3071 T = DI->getType();
3075 DI = SemaRef.SubstType(D->getTypeSourceInfo(), TemplateArgs,
3076 D->getLocation(), D->getDeclName());
3080 // Check that this type is acceptable for a non-type template parameter.
3081 T = SemaRef.CheckNonTypeTemplateParameterType(DI, D->getLocation());
3091 SemaRef.Context, Owner, D->getInnerLocStart(), D->getLocation(),
3092 D->getDepth() - TemplateArgs.getNumSubstitutedLevels(),
3093 D->getPosition(), D->getIdentifier(), T, DI, ExpandedParameterPackTypes,
3097 SemaRef.Context, Owner, D->getInnerLocStart(), D->getLocation(),
3098 D->getDepth() - TemplateArgs.getNumSubstitutedLevels(),
3099 D->getPosition(), D->getIdentifier(), T, D->isParameterPack(), DI);
3101 if (AutoTypeLoc AutoLoc = DI->getTypeLoc().getContainedAutoTypeLoc())
3106 DI->getTypeLoc().getAs<PackExpansionTypeLoc>().getEllipsisLoc();
3108 D->getPlaceholderTypeConstraint()))
3109 EllipsisLoc = Constraint->getEllipsisLoc();
3118 Param->setAccess(AS_public);
3119 Param->setImplicit(D->isImplicit());
3121 Param->setInvalidDecl();
3123 if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited()) {
3127 if (!SemaRef.SubstTemplateArgument(D->getDefaultArgument(), TemplateArgs,
3129 Param->setDefaultArgument(SemaRef.Context, Result);
3134 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
3143 if (P->isTemplateParameterPack())
3146 S.collectUnexpandedParameterPacks(NTTP->getTypeSourceInfo()->getTypeLoc(),
3149 collectUnexpandedParameterPacks(S, TTP->getTemplateParameters(),
3158 TemplateParameterList *TempParams = D->getTemplateParameters();
3164 if (D->isExpandedParameterPack()) {
3165 // The template template parameter pack is an already-expanded pack
3168 ExpandedParams.reserve(D->getNumExpansionTemplateParameters());
3169 for (unsigned I = 0, N = D->getNumExpansionTemplateParameters();
3173 SubstTemplateParams(D->getExpansionTemplateParameters(I));
3181 } else if (D->isPackExpansion()) {
3186 collectUnexpandedParameterPacks(SemaRef, D->getTemplateParameters(),
3194 if (SemaRef.CheckParameterPacksForExpansion(D->getLocation(),
3195 TempParams->getSourceRange(),
3214 // will end up using the expanded parameter pack types for type-checking.
3220 Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, -1);
3240 SemaRef.Context, Owner, D->getLocation(),
3241 D->getDepth() - TemplateArgs.getNumSubstitutedLevels(),
3242 D->getPosition(), D->getIdentifier(), D->wasDeclaredWithTypename(),
3246 SemaRef.Context, Owner, D->getLocation(),
3247 D->getDepth() - TemplateArgs.getNumSubstitutedLevels(),
3248 D->getPosition(), D->isParameterPack(), D->getIdentifier(),
3249 D->wasDeclaredWithTypename(), InstParams);
3250 if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited()) {
3252 D->getDefaultArgument().getTemplateQualifierLoc();
3256 QualifierLoc, D->getDefaultArgument().getArgument().getAsTemplate(),
3257 D->getDefaultArgument().getTemplateNameLoc(), TemplateArgs);
3259 Param->setDefaultArgument(
3262 D->getDefaultArgument().getTemplateQualifierLoc(),
3263 D->getDefaultArgument().getTemplateNameLoc()));
3265 Param->setAccess(AS_public);
3266 Param->setImplicit(D->isImplicit());
3270 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param);
3280 = UsingDirectiveDecl::Create(SemaRef.Context, Owner, D->getLocation(),
3281 D->getNamespaceKeyLocation(),
3282 D->getQualifierLoc(),
3283 D->getIdentLocation(),
3284 D->getNominatedNamespace(),
3285 D->getCommonAncestor());
3289 if (!Owner->isFunctionOrMethod())
3290 Owner->addDecl(Inst);
3299 bool isFunctionScope = Owner->isFunctionOrMethod();
3301 for (auto *Shadow : D->shadows()) {
3305 NamedDecl *OldTarget = Shadow->getTargetDecl();
3307 if (auto *BaseShadow = CUSD->getNominatedBaseClassShadowDecl())
3312 dyn_cast<UnresolvedUsingIfExistsDecl>(Shadow->getTargetDecl())) {
3314 SemaRef.Context, Owner, EmptyD->getLocation(), EmptyD->getDeclName());
3317 Shadow->getLocation(), OldTarget, TemplateArgs));
3329 Shadow->getLocation(), OldPrev, TemplateArgs));
3336 SemaRef.CurrentInstantiationScope->InstantiatedLocal(Shadow, InstShadow);
3353 = SemaRef.SubstNestedNameSpecifierLoc(D->getQualifierLoc(),
3361 DeclarationNameInfo NameInfo = D->getNameInfo();
3368 // fact, it's not really even possible in non-class scopes).
3369 bool CheckRedeclaration = Owner->isRecord();
3374 D->getUsingLoc(),
3377 D->hasTypename());
3386 if (SemaRef.CheckUsingDeclRedeclaration(D->getUsingLoc(),
3387 D->hasTypename(), SS,
3388 D->getLocation(), Prev))
3389 NewUD->setInvalidDecl();
3392 if (!NewUD->isInvalidDecl() &&
3393 SemaRef.CheckUsingDeclQualifier(D->getUsingLoc(), D->hasTypename(), SS,
3394 NameInfo, D->getLocation(), nullptr, D))
3395 NewUD->setInvalidDecl();
3398 NewUD->setAccess(D->getAccess());
3399 Owner->addDecl(NewUD);
3402 if (NewUD->isInvalidDecl())
3416 D->getLocation(), D->getEnumDecl(), TemplateArgs));
3418 if (SemaRef.RequireCompleteEnumDecl(EnumD, EnumD->getLocation()))
3421 TypeSourceInfo *TSI = SemaRef.SubstType(D->getEnumType(), TemplateArgs,
3422 D->getLocation(), D->getDeclName());
3428 UsingEnumDecl::Create(SemaRef.Context, Owner, D->getUsingLoc(),
3429 D->getEnumLoc(), D->getLocation(), TSI);
3432 NewUD->setAccess(D->getAccess());
3433 Owner->addDecl(NewUD);
3436 if (NewUD->isInvalidDecl())
3461 if (D->isPackExpansion() && !InstantiatingPackElement) {
3463 SemaRef.collectUnexpandedParameterPacks(D->getQualifierLoc(), Unexpanded);
3464 SemaRef.collectUnexpandedParameterPacks(D->getNameInfo(), Unexpanded);
3472 D->getEllipsisLoc(), D->getSourceRange(), Unexpanded, TemplateArgs,
3484 Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, -1);
3490 // same pack expansion, but this is always ill-formed because all expansions
3495 if (D->getDeclContext()->isFunctionOrMethod() && *NumExpansions > 1) {
3496 SemaRef.Diag(D->getEllipsisLoc(),
3517 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, NewD);
3522 SourceLocation TypenameLoc = TD ? TD->getTypenameLoc() : SourceLocation();
3525 = SemaRef.SubstNestedNameSpecifierLoc(D->getQualifierLoc(),
3534 = SemaRef.SubstDeclarationNameInfo(D->getNameInfo(), TemplateArgs);
3538 bool InstantiatingSlice = D->getEllipsisLoc().isValid() &&
3539 SemaRef.ArgumentPackSubstitutionIndex != -1;
3541 InstantiatingSlice ? SourceLocation() : D->getEllipsisLoc();
3543 bool IsUsingIfExists = D->template hasAttr<UsingIfExistsAttr>();
3545 /*Scope*/ nullptr, D->getAccess(), D->getUsingLoc(),
3574 for (auto *UD : D->expansions()) {
3576 SemaRef.FindInstantiatedDecl(D->getLocation(), UD, TemplateArgs))
3584 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, NewD);
3591 for (auto *I : D->varlists()) {
3598 SemaRef.OpenMP().CheckOMPThreadPrivateDecl(D->getLocation(), Vars);
3600 TD->setAccess(AS_public);
3601 Owner->addDecl(TD);
3608 for (auto *I : D->varlists()) {
3615 for (OMPClause *C : D->clauselists()) {
3618 ExprResult NewE = SemaRef.SubstExpr(AC->getAllocator(), TemplateArgs);
3622 NewE.get(), AC->getBeginLoc(), AC->getLParenLoc(), AC->getEndLoc());
3624 ExprResult NewE = SemaRef.SubstExpr(AC->getAlignment(), TemplateArgs);
3628 NewE.get(), AC->getBeginLoc(), AC->getLParenLoc(), AC->getEndLoc());
3637 D->getLocation(), Vars, Clauses, Owner);
3652 D->getType()->isDependentType() ||
3653 D->getType()->isInstantiationDependentType() ||
3654 D->getType()->containsUnexpandedParameterPack();
3658 D->getLocation(),
3660 D->getType(), TemplateArgs, D->getLocation(), DeclarationName())));
3662 SubstReductionType = D->getType();
3666 Expr *Combiner = D->getCombiner();
3667 Expr *Init = D->getInitializer();
3671 std::make_pair(SubstReductionType, D->getLocation())};
3672 auto *PrevDeclInScope = D->getPrevDeclInScope();
3673 if (PrevDeclInScope && !PrevDeclInScope->isInvalidDecl()) {
3675 SemaRef.CurrentInstantiationScope->findInstantiationOf(PrevDeclInScope)
3676 ->get<Decl *>());
3679 /*S=*/nullptr, Owner, D->getDeclName(), ReductionTypes, D->getAccess(),
3682 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, NewDRD);
3689 SemaRef.CurrentInstantiationScope->InstantiatedLocal(
3690 cast<DeclRefExpr>(D->getCombinerIn())->getDecl(),
3691 cast<DeclRefExpr>(NewDRD->getCombinerIn())->getDecl());
3692 SemaRef.CurrentInstantiationScope->InstantiatedLocal(
3693 cast<DeclRefExpr>(D->getCombinerOut())->getDecl(),
3694 cast<DeclRefExpr>(NewDRD->getCombinerOut())->getDecl());
3707 SemaRef.CurrentInstantiationScope->InstantiatedLocal(
3708 cast<DeclRefExpr>(D->getInitOrig())->getDecl(),
3709 cast<DeclRefExpr>(NewDRD->getInitOrig())->getDecl());
3710 SemaRef.CurrentInstantiationScope->InstantiatedLocal(
3711 cast<DeclRefExpr>(D->getInitPriv())->getDecl(),
3712 cast<DeclRefExpr>(NewDRD->getInitPriv())->getDecl());
3713 if (D->getInitializerKind() == OMPDeclareReductionInitKind::Call) {
3717 cast<VarDecl>(cast<DeclRefExpr>(D->getInitPriv())->getDecl());
3718 IsCorrect = IsCorrect && OldPrivParm->hasInit();
3728 (D->getInitializerKind() == OMPDeclareReductionInitKind::Call &&
3730 (D->getInitializerKind() != OMPDeclareReductionInitKind::Call &&
3734 /*S=*/nullptr, DRD, IsCorrect && !D->isInvalidDecl());
3743 D->getType()->isDependentType() ||
3744 D->getType()->isInstantiationDependentType() ||
3745 D->getType()->containsUnexpandedParameterPack();
3747 DeclarationName VN = D->getVarName();
3750 D->getLocation(),
3751 ParsedType::make(SemaRef.SubstType(D->getType(), TemplateArgs,
3752 D->getLocation(), VN)));
3754 SubstMapperTy = D->getType();
3759 auto *PrevDeclInScope = D->getPrevDeclInScope();
3760 if (PrevDeclInScope && !PrevDeclInScope->isInvalidDecl()) {
3762 SemaRef.CurrentInstantiationScope->findInstantiationOf(PrevDeclInScope)
3763 ->get<Decl *>());
3771 (*D->clauselist_begin())->getBeginLoc());
3774 /*S=*/nullptr, SubstMapperTy, D->getLocation(), VN);
3775 SemaRef.CurrentInstantiationScope->InstantiatedLocal(
3776 cast<DeclRefExpr>(D->getMapperVarRef())->getDecl(),
3777 cast<DeclRefExpr>(MapperVarRef.get())->getDecl());
3782 for (OMPClause *C : D->clauselists()) {
3785 for (Expr *OE : OldC->varlists()) {
3796 SemaRef.SubstNestedNameSpecifierLoc(OldC->getMapperQualifierLoc(),
3801 SemaRef.SubstDeclarationNameInfo(OldC->getMapperIdInfo(), TemplateArgs);
3802 OMPVarListLocTy Locs(OldC->getBeginLoc(), OldC->getLParenLoc(),
3803 OldC->getEndLoc());
3805 OldC->getIteratorModifier(), OldC->getMapTypeModifiers(),
3806 OldC->getMapTypeModifiersLoc(), SS, NewNameInfo, OldC->getMapType(),
3807 OldC->isImplicitMapType(), OldC->getMapLoc(), OldC->getColonLoc(),
3815 /*S=*/nullptr, Owner, D->getDeclName(), SubstMapperTy, D->getLocation(),
3816 VN, D->getAccess(), MapperVarRef.get(), Clauses, PrevDeclInScope);
3818 SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, NewDMD);
3834 if (Inst && !D->getDescribedFunctionTemplate())
3835 Owner->addDecl(Inst);
3850 // As a MS extension, we permit class-scope explicit specialization
3852 ClassTemplateDecl *ClassTemplate = D->getSpecializedTemplate();
3853 assert(ClassTemplate->getDeclContext()->isRecord() &&
3854 D->getTemplateSpecializationKind() == TSK_ExplicitSpecialization &&
3858 // Lookup the already-instantiated declaration in the instantiation
3862 D->getLocation(), ClassTemplate, TemplateArgs));
3870 D->getTemplateArgsAsWritten()) {
3871 InstTemplateArgs.setLAngleLoc(TemplateArgsInfo->getLAngleLoc());
3872 InstTemplateArgs.setRAngleLoc(TemplateArgsInfo->getRAngleLoc());
3874 if (SemaRef.SubstTemplateArguments(TemplateArgsInfo->arguments(),
3879 // Check that the template argument list is well-formed for this
3882 if (SemaRef.CheckTemplateArgumentList(InstClassTemplate, D->getLocation(),
3892 InstClassTemplate->findSpecialization(CanonicalConverted, InsertPos);
3898 SemaRef.CheckSpecializationInstantiationRedecl(D->getLocation(),
3899 D->getSpecializationKind(),
3901 PrevDecl->getSpecializationKind(),
3902 PrevDecl->getPointOfInstantiation(),
3911 // template<typename X> struct Inner;
3918 if (PrevDecl && PrevDecl->getDefinition() &&
3919 D->isThisDeclarationADefinition()) {
3920 SemaRef.Diag(D->getLocation(), diag::err_redefinition) << PrevDecl;
3921 SemaRef.Diag(PrevDecl->getDefinition()->getLocation(),
3929 SemaRef.Context, D->getTagKind(), Owner, D->getBeginLoc(),
3930 D->getLocation(), InstClassTemplate, CanonicalConverted, PrevDecl);
3931 InstD->setTemplateArgsAsWritten(InstTemplateArgs);
3936 InstClassTemplate->AddSpecialization(InstD, InsertPos);
3942 InstD->setAccess(D->getAccess());
3943 InstD->setInstantiationOfMemberClass(D, TSK_ImplicitInstantiation);
3944 InstD->setSpecializationKind(D->getSpecializationKind());
3945 InstD->setExternKeywordLoc(D->getExternKeywordLoc());
3946 InstD->setTemplateKeywordLoc(D->getTemplateKeywordLoc());
3948 Owner->addDecl(InstD);
3950 // Instantiate the members of the class-scope explicit specialization eagerly.
3954 if (D->isThisDeclarationADefinition() &&
3955 SemaRef.InstantiateClass(D->getLocation(), InstD, D, TemplateArgs,
3967 VarTemplateDecl *VarTemplate = D->getSpecializedTemplate();
3973 D->getLocation(), VarTemplate, TemplateArgs));
3979 D->getTemplateArgsAsWritten()) {
3980 VarTemplateArgsInfo.setLAngleLoc(TemplateArgsInfo->getLAngleLoc());
3981 VarTemplateArgsInfo.setRAngleLoc(TemplateArgsInfo->getRAngleLoc());
3983 if (SemaRef.SubstTemplateArguments(TemplateArgsInfo->arguments(),
3988 // Check that the template argument list is well-formed for this template.
3990 if (SemaRef.CheckTemplateArgumentList(InstVarTemplate, D->getLocation(),
3999 InstVarTemplate->findSpecialization(CanonicalConverted, InsertPos);
4005 D->getLocation(), D->getSpecializationKind(), PrevDecl,
4006 PrevDecl->getSpecializationKind(),
4007 PrevDecl->getPointOfInstantiation(), Ignored))
4022 SemaRef.SubstType(D->getTypeSourceInfo(), TemplateArgs,
4023 D->getTypeSpecStartLoc(), D->getDeclName());
4027 if (DI->getType()->isFunctionType()) {
4028 SemaRef.Diag(D->getLocation(), diag::err_variable_instantiates_to_function)
4029 << D->isStaticDataMember() << DI->getType();
4035 SemaRef.Context, Owner, D->getInnerLocStart(), D->getLocation(),
4036 VarTemplate, DI->getType(), DI, D->getStorageClass(), Converted);
4037 Var->setTemplateArgsAsWritten(TemplateArgsInfo);
4040 VarTemplate->findSpecialization(Converted, InsertPos);
4041 VarTemplate->AddSpecialization(Var, InsertPos);
4058 llvm_unreachable("@defs is not supported in Objective-C++");
4066 SemaRef.Diag(D->getLocation(), DiagID)
4067 << D->getDeclKindName();
4083 return RequiresExprBodyDecl::Create(SemaRef.Context, D->getDeclContext(),
4084 D->getBeginLoc());
4094 if (D->isInvalidDecl())
4098 runWithSufficientStackSpace(D->getLocation(), [&] {
4112 auto *FPT = T->castAs<FunctionProtoType>();
4114 SemaRef.Context.BoolTy, FPT->getParamTypes(), FPT->getExtProtoInfo());
4123 SemaRef.Context.getTrivialTypeSourceInfo(T, Orig->getEndLoc());
4124 auto OldLoc = TInfo->getTypeLoc().getAsAdjusted<FunctionProtoTypeLoc>();
4126 auto NewLoc = NewTInfo->getTypeLoc().castAs<FunctionProtoTypeLoc>();
4131 // and the declarator-id is replaced with operator==
4138 if (Spaceship->isInvalidDecl())
4143 // access and function-definition and in the same class scope as the
4144 // three-way comparison operator function
4147 NoTemplateArgs.addOuterRetainedLevels(RD->getTemplateDepth());
4155 assert(Spaceship->getFriendObjectKind() &&
4165 FriendDecl::Create(Context, RD, Spaceship->getLocation(),
4166 cast<NamedDecl>(R), Spaceship->getBeginLoc());
4167 FD->setAccess(AS_public);
4168 RD->addDecl(FD);
4184 unsigned N = L->size();
4191 Invalid = Invalid || !D || D->isInvalidDecl();
4198 Expr *InstRequiresClause = L->getRequiresClause();
4201 = TemplateParameterList::Create(SemaRef.Context, L->getTemplateLoc(),
4202 L->getLAngleLoc(), Params,
4203 L->getRAngleLoc(), InstRequiresClause);
4238 TemplateParameterList *TempParams = PartialSpec->getTemplateParameters();
4246 = PartialSpec->getTemplateArgsAsWritten();
4247 TemplateArgumentListInfo InstTemplateArgs(TemplArgInfo->LAngleLoc,
4248 TemplArgInfo->RAngleLoc);
4249 if (SemaRef.SubstTemplateArguments(TemplArgInfo->arguments(), TemplateArgs,
4253 // Check that the template argument list is well-formed for this
4257 ClassTemplate, PartialSpec->getLocation(), InstTemplateArgs,
4263 PartialSpec->getLocation(), ClassTemplate, InstTemplateArgs.size(),
4271 ClassTemplate->findPartialSpecialization(CanonicalConverted, InstParams,
4282 SemaRef.Context, PartialSpec->getTagKind(), Owner,
4283 PartialSpec->getBeginLoc(), PartialSpec->getLocation(), InstParams,
4287 InstPartialSpec->setTemplateArgsAsWritten(InstTemplateArgs);
4293 InstPartialSpec->setInstantiatedFromMember(PartialSpec);
4304 // template<typename X, typename Y> struct Inner;
4311 SemaRef.Diag(InstPartialSpec->getLocation(),
4314 SemaRef.Diag(PrevDecl->getLocation(), diag::note_prev_partial_spec_here)
4324 ClassTemplate->AddPartialSpecialization(InstPartialSpec,
4351 TemplateParameterList *TempParams = PartialSpec->getTemplateParameters();
4359 = PartialSpec->getTemplateArgsAsWritten();
4360 TemplateArgumentListInfo InstTemplateArgs(TemplArgInfo->LAngleLoc,
4361 TemplArgInfo->RAngleLoc);
4362 if (SemaRef.SubstTemplateArguments(TemplArgInfo->arguments(), TemplateArgs,
4366 // Check that the template argument list is well-formed for this
4370 VarTemplate, PartialSpec->getLocation(), InstTemplateArgs,
4376 PartialSpec->getLocation(), VarTemplate, InstTemplateArgs.size(),
4384 VarTemplate->findPartialSpecialization(CanonicalConverted, InstParams,
4389 PartialSpec->getTypeSourceInfo(), TemplateArgs,
4390 PartialSpec->getTypeSpecStartLoc(), PartialSpec->getDeclName());
4394 if (DI->getType()->isFunctionType()) {
4395 SemaRef.Diag(PartialSpec->getLocation(),
4397 << PartialSpec->isStaticDataMember() << DI->getType();
4404 SemaRef.Context, Owner, PartialSpec->getInnerLocStart(),
4405 PartialSpec->getLocation(), InstParams, VarTemplate, DI->getType(),
4406 DI, PartialSpec->getStorageClass(), CanonicalConverted);
4408 InstPartialSpec->setTemplateArgsAsWritten(InstTemplateArgs);
4414 InstPartialSpec->setInstantiatedFromMember(PartialSpec);
4425 // template<typename X, typename Y> pair<X,Y> p;
4432 SemaRef.Diag(PartialSpec->getLocation(),
4435 SemaRef.Diag(PrevDecl->getLocation(),
4444 VarTemplate->AddPartialSpecialization(InstPartialSpec, /*InsertPos=*/nullptr);
4455 TypeSourceInfo *OldTInfo = D->getTypeSourceInfo();
4457 assert(Params.empty() && "parameter vector is non-empty at start");
4463 ThisTypeQuals = Method->getFunctionObjectParameterType().getQualifiers();
4467 OldTInfo, TemplateArgs, D->getTypeSpecStartLoc(), D->getDeclName(),
4472 TypeLoc OldTL = OldTInfo->getTypeLoc().IgnoreParens();
4476 TypeLoc NewTL = NewTInfo->getTypeLoc().IgnoreParens();
4488 if (OldParam->isParameterPack())
4490 SemaRef.getNumArgumentsInExpansion(OldParam->getType(),
4494 // instantiated to a (still-dependent) parameter pack.
4497 Scope->InstantiatedLocal(OldParam, NewParam);
4500 Scope->MakeInstantiatedLocalArgPack(OldParam);
4504 Scope->InstantiatedLocalPackArg(OldParam, NewParam);
4519 D, D->getLocation(), OldProto->getParamType(i)));
4543 if (SemaRef.SubstParmTypes(D->getLocation(), D->parameters(), nullptr,
4557 for (auto *decl : PatternDecl->decls()) {
4562 IdentifierInfo *II = VD->getIdentifier();
4564 auto it = llvm::find_if(Function->decls(), [&](Decl *inst) {
4566 return InstVD && InstVD->isLocalVarDecl() &&
4567 InstVD->getIdentifier() == II;
4570 if (it == Function->decls().end())
4574 LSI->addCapture(cast<VarDecl>(*it), /*isBlock=*/false, /*isByref=*/false,
4575 /*isNested=*/false, VD->getLocation(), SourceLocation(),
4576 VD->getType(), /*Invalid=*/false);
4585 for (unsigned I = 0, N = PatternDecl->getNumParams(); I != N; ++I) {
4586 const ParmVarDecl *PatternParam = PatternDecl->getParamDecl(I);
4587 if (!PatternParam->isParameterPack()) {
4589 assert(FParamIdx < Function->getNumParams());
4590 ParmVarDecl *FunctionParam = Function->getParamDecl(FParamIdx);
4591 FunctionParam->setDeclName(PatternParam->getDeclName());
4593 // in the pattern. They can differ in top-level cv-qualifiers, and we want
4596 // it's instantiation-dependent.
4598 if (!PatternDecl->getType()->isDependentType()) {
4599 QualType T = SubstType(PatternParam->getType(), TemplateArgs,
4600 FunctionParam->getLocation(),
4601 FunctionParam->getDeclName());
4604 FunctionParam->setType(T);
4615 getNumArgumentsInExpansion(PatternParam->getType(), TemplateArgs);
4618 PatternParam->getType()->castAs<PackExpansionType>()->getPattern();
4620 ParmVarDecl *FunctionParam = Function->getParamDecl(FParamIdx);
4621 FunctionParam->setDeclName(PatternParam->getDeclName());
4622 if (!PatternDecl->getType()->isDependentType()) {
4625 SubstType(PatternType, TemplateArgs, FunctionParam->getLocation(),
4626 FunctionParam->getDeclName());
4629 FunctionParam->setType(T);
4643 assert(Param->hasUninstantiatedDefaultArg());
4655 // // bug: default argument A<T>::FooImpl() is evaluated with 2-level
4663 getTemplateInstantiationArgs(FD, FD->getLexicalDeclContext(),
4671 L->DefaultArgumentInstantiated(Param);
4678 const FunctionProtoType *Proto = Decl->getType()->castAs<FunctionProtoType>();
4679 if (Proto->getExceptionSpecType() != EST_Uninstantiated)
4704 getTemplateInstantiationArgs(Decl, Decl->getLexicalDeclContext(),
4709 // here, because for a non-defining friend declaration in a class template,
4712 FunctionDecl *Template = Proto->getExceptionSpecTemplate();
4724 SubstExceptionSpec(Decl, Template->getType()->castAs<FunctionProtoType>(),
4735 New->setImplicit(Tmpl->isImplicit());
4741 // If we are performing substituting explicitly-specified template arguments
4757 {ActiveInst.Entity->getCanonicalDecl(), ActiveInst.Kind});
4765 const FunctionProtoType *Proto = Tmpl->getType()->getAs<FunctionProtoType>();
4768 if (Proto->hasExceptionSpec() || Proto->getNoReturnAttr()) {
4769 FunctionProtoType::ExtProtoInfo EPI = Proto->getExtProtoInfo();
4771 // DR1330: In C++11, defer instantiation of a non-trivial
4779 !Tmpl->isInLocalScopeForInstantiation()) {
4789 = New->getType()->getAs<FunctionProtoType>();
4791 EPI = NewProto->getExtProtoInfo();
4795 New->setType(SemaRef.Context.getFunctionType(
4796 NewProto->getReturnType(), NewProto->getParamTypes(), EPI));
4805 Tmpl->isDefined(Definition);
4827 New->setAccess(Tmpl->getAccess());
4828 if (Tmpl->isVirtualAsWritten())
4829 New->setVirtualAsWritten(true);
4838 if (auto *DFI = Tmpl->getDefalutedOrDeletedInfo()) {
4840 Lookups.reserve(DFI->getUnqualifiedLookups().size());
4842 for (DeclAccessPair DA : DFI->getUnqualifiedLookups()) {
4843 NamedDecl *D = SemaRef.FindInstantiatedDecl(New->getLocation(),
4853 New->setDefaultedOrDeletedInfo(
4859 SemaRef.SetDeclDefaulted(New, Tmpl->getLocation());
4866 FunctionDecl *FD = FTD->getTemplatedDecl();
4869 InstantiatingTemplate Inst(*this, Loc, FTD, Args->asArray(), CSC, Info);
4874 MultiLevelTemplateArgumentList MArgs(FTD, Args->asArray(),
4877 return cast_or_null<FunctionDecl>(SubstDecl(FD, FD->getParent(), MArgs));
4885 if (Function->isInvalidDecl() || isa<CXXDeductionGuideDecl>(Function))
4891 Function->getTemplateSpecializationKindForInstantiation();
4897 if (Function->getBuiltinID() && TSK == TSK_ImplicitInstantiation &&
4903 if (Function->isDefined(ExistingDefn,
4905 if (ExistingDefn->isThisDeclarationADefinition())
4911 assert(ExistingDefn->isThisDeclarationInstantiatedFromAFriendDefinition());
4916 const FunctionDecl *PatternDecl = Function->getTemplateInstantiationPattern();
4917 assert(PatternDecl && "instantiating a non-template");
4919 const FunctionDecl *PatternDef = PatternDecl->getDefinition();
4922 Pattern = PatternDef->getBody(PatternDef);
4924 if (PatternDef->willHaveBody())
4931 Function->getInstantiatedFromMemberFunction(),
4935 Function->setInvalidDecl();
4937 (Function->isConstexpr() && !Recursive)) {
4941 Function->setInstantiationIsPending(true);
4946 !getSourceManager().isInSystemHeader(PatternDecl->getBeginLoc())) {
4949 Diag(PatternDecl->getLocation(), diag::note_forward_template_decl);
4960 if (PatternDecl->isLateTemplateParsed() &&
4962 Function->setInstantiationIsPending(true);
4971 Function->getNameForDiagnostic(OS, getPrintingPolicy(),
4974 auto Loc = SourceMgr.getExpansionLoc(Function->getLocation());
4992 if (!Pattern && PatternDecl->isLateTemplateParsed() &&
4995 if (PatternDecl->isFromASTFile())
4996 ExternalSource->ReadLateParsedTemplates(LateParsedTemplateMap);
5001 LateTemplateParser(OpaqueParser, *LPTIter->second);
5002 Pattern = PatternDecl->getBody(PatternDecl);
5007 assert((Pattern || PatternDecl->isDefaulted() ||
5008 PatternDecl->hasSkippedBody()) &&
5017 !PatternDecl->isInlined() &&
5018 !PatternDecl->getReturnType()->getContainedAutoType())
5021 if (PatternDecl->isInlined()) {
5024 for (auto *D = Function->getMostRecentDecl(); /**/;
5025 D = D->getPreviousDecl()) {
5026 D->setImplicitlyInline();
5040 Function->setVisibleDespiteOwningModule();
5043 Function->setLocation(PatternDecl->getLocation());
5044 Function->setInnerLocStart(PatternDecl->getInnerLocStart());
5045 Function->setRangeEnd(PatternDecl->getEndLoc());
5046 Function->setDeclarationNameLoc(PatternDecl->getNameInfo().getInfo());
5054 ThisContext = Method->getParent();
5055 ThisTypeQuals = Method->getMethodQualifiers();
5066 if (CXXRecordDecl *Rec = dyn_cast<CXXRecordDecl>(Function->getDeclContext()))
5068 Rec->isLocalClass() && !Function->isFunctionTemplateSpecialization();
5076 assert(PatternDecl->isDefaulted() &&
5085 auto *NewRec = dyn_cast<CXXRecordDecl>(Function->getDeclContext());
5087 dyn_cast<CXXRecordDecl>(PatternDecl->getDeclContext());
5090 if (!PatternRec->isLambda())
5113 getDerived().TransformDecl(TL.getNameLoc(), T->getDecl()));
5127 TypeSourceInfo *NewSI = IR.TransformType(Function->getTypeSourceInfo());
5129 Function->setType(NewSI->getType());
5130 Function->setTypeSourceInfo(NewSI);
5132 ParmVarDecl *Parm = Function->getParamDecl(0);
5133 TypeSourceInfo *NewParmSI = IR.TransformType(Parm->getTypeSourceInfo());
5135 Parm->setType(NewParmSI->getType());
5136 Parm->setTypeSourceInfo(NewParmSI);
5139 if (PatternDecl->isDefaulted()) {
5141 SetDeclDefaulted(Function, PatternDecl->getLocation());
5144 Function, Function->getLexicalDeclContext(), /*Final=*/false,
5168 if (PatternDecl->hasSkippedBody()) {
5180 Ctor->isDefaultConstructor()) {
5189 Function->setInvalidDecl();
5198 Listener->FunctionDefinitionInstantiated(Function);
5220 if (FromVar->isInvalidDecl())
5233 // If we're instantiating an explicitly-specialized member template or member
5241 IsMemberSpec = PartialSpec->isMemberSpecialization();
5243 PartialSpec, PartialSpecArgs->asArray(), /*Final=*/false);
5245 assert(VarTemplate == FromVar->getDescribedVarTemplate());
5246 IsMemberSpec = VarTemplate->isMemberSpecialization();
5251 FromVar = FromVar->getFirstDecl();
5253 TemplateDeclInstantiator Instantiator(*this, FromVar->getDeclContext(),
5266 assert(PatternDecl->isThisDeclarationADefinition() &&
5271 SubstType(PatternDecl->getTypeSourceInfo(), TemplateArgs,
5272 PatternDecl->getTypeSpecStartLoc(), PatternDecl->getDeclName());
5277 VarSpec->setType(DI->getType());
5280 VarSpec->setCompleteDefinition();
5307 (OldVar->getDescribedVarTemplate() ||
5313 // out-of-line, the instantiation will have the same lexical
5315 if (OldVar->isLocalExternDecl()) {
5316 NewVar->setLocalExternDecl();
5317 NewVar->setLexicalDeclContext(Owner);
5318 } else if (OldVar->isOutOfLine())
5319 NewVar->setLexicalDeclContext(OldVar->getLexicalDeclContext());
5320 NewVar->setTSCSpec(OldVar->getTSCSpec());
5321 NewVar->setInitStyle(OldVar->getInitStyle());
5322 NewVar->setCXXForRangeDecl(OldVar->isCXXForRangeDecl());
5323 NewVar->setObjCForDecl(OldVar->isObjCForDecl());
5324 NewVar->setConstexpr(OldVar->isConstexpr());
5325 NewVar->setInitCapture(OldVar->isInitCapture());
5326 NewVar->setPreviousDeclInSameBlockScope(
5327 OldVar->isPreviousDeclInSameBlockScope());
5328 NewVar->setAccess(OldVar->getAccess());
5330 if (!OldVar->isStaticDataMember()) {
5331 if (OldVar->isUsed(false))
5332 NewVar->setIsUsed();
5333 NewVar->setReferenced(OldVar->isReferenced());
5339 *this, NewVar->getDeclName(), NewVar->getLocation(),
5340 NewVar->isLocalExternDecl() ? Sema::LookupRedeclarationWithLinkage
5342 NewVar->isLocalExternDecl() ? RedeclarationKind::ForExternalRedeclaration
5345 if (NewVar->isLocalExternDecl() && OldVar->getPreviousDecl() &&
5346 (!OldVar->getPreviousDecl()->getDeclContext()->isDependentContext() ||
5347 OldVar->getPreviousDecl()->getDeclContext()==OldVar->getDeclContext())) {
5351 NewVar->getLocation(), OldVar->getPreviousDecl(), TemplateArgs))
5354 OldVar->hasLinkage()) {
5355 LookupQualifiedName(Previous, NewVar->getDeclContext(), false);
5362 NewVar->getLexicalDeclContext()->addHiddenDecl(NewVar);
5363 if (!NewVar->isLocalExternDecl() || !NewVar->getPreviousDecl())
5364 NewVar->getDeclContext()->makeDeclVisibleInContext(NewVar);
5367 if (!OldVar->isOutOfLine()) {
5368 if (NewVar->getDeclContext()->isFunctionOrMethod())
5369 CurrentInstantiationScope->InstantiatedLocal(OldVar, NewVar);
5378 if (NewVar->isStaticDataMember() && !InstantiatingVarTemplate &&
5380 NewVar->setInstantiationOfStaticDataMember(OldVar,
5383 // If the pattern is an (in-class) explicit specialization, then the result
5387 if (OldVTSD->getSpecializationKind() == TSK_ExplicitSpecialization &&
5389 cast<VarTemplateSpecializationDecl>(NewVar)->setSpecializationKind(
5400 } else if (NewVar->getType()->isUndeducedType()) {
5404 (OldVar->isInline() && OldVar->isThisDeclarationADefinition() &&
5405 !NewVar->isThisDeclarationADefinition())) {
5415 if (!NewVar->isInvalidDecl() &&
5416 NewVar->getDeclContext()->isFunctionOrMethod() &&
5417 OldVar->getType()->isDependentType())
5425 L->VariableDefinitionInstantiated(Var);
5429 // non-static data member.
5430 if (OldVar->isInlineSpecified())
5431 Var->setInlineSpecified();
5432 else if (OldVar->isInline())
5433 Var->setImplicitlyInline();
5435 if (OldVar->getInit()) {
5444 ContextRAII SwitchContext(*this, Var->getDeclContext());
5445 Init = SubstInitializer(OldVar->getInit(), TemplateArgs,
5446 OldVar->getInitStyle() == VarDecl::CallInit);
5452 if (Var->hasAttr<DLLImportAttr>() &&
5454 !InitExpr->isConstantInitializer(getASTContext(), false))) {
5457 bool DirectInit = OldVar->isDirectInit();
5464 Var->setInvalidDecl();
5469 if (Var->isStaticDataMember() && !Var->isInline()) {
5470 if (!Var->isOutOfLine())
5474 // another one to the out-of-line definition.
5475 if (OldVar->getFirstDecl()->hasInit())
5479 // We'll add an initializer to a for-range declaration later.
5480 if (Var->isCXXForRangeDecl() || Var->isObjCForDecl())
5493 if (Var->isInvalidDecl())
5496 // Never instantiate an explicitly-specialized entity.
5498 Var->getTemplateSpecializationKindForInstantiation();
5503 VarDecl *PatternDecl = Var->getTemplateInstantiationPattern();
5517 // in-class declaration of the template and instantiate the definition
5518 // from a separate out-of-class definition.
5519 if (PatternDecl->isStaticDataMember() &&
5520 (PatternDecl = PatternDecl->getFirstDecl())->hasInit() &&
5521 !Var->hasInit()) {
5532 Var->setVisibleDespiteOwningModule();
5544 ContextRAII PreviousContext(*this, Var->getDeclContext());
5555 assert(Var->isStaticDataMember() && PatternDecl->isStaticDataMember() &&
5559 VarDecl *Def = PatternDecl->getDefinition(getASTContext());
5572 !getSourceManager().isInSystemHeader(PatternDecl->getBeginLoc())) {
5575 Diag(PatternDecl->getLocation(), diag::note_forward_template_decl);
5585 // FIXME: Produce diagnostics when Var->getInstantiatedFromStaticDataMember().
5602 !Var->mightBeUsableInConstantExpressions(getASTContext()))
5619 if (VarDecl *Def = Var->getDefinition()) {
5622 Def->setTemplateSpecializationKind(Var->getTemplateSpecializationKind(),
5641 ContextRAII PreviousContext(*this, Var->getDeclContext());
5647 if (Def->isStaticDataMember() && !Def->isOutOfLine()) {
5652 Var = cast_or_null<VarDecl>(SubstDecl(Def, Var->getDeclContext(),
5654 } else if (Var->isStaticDataMember() &&
5655 Var->getLexicalDeclContext()->isRecord()) {
5657 // and all we have is the in-class declaration of it. Instantiate a separate
5659 TemplateDeclInstantiator Instantiator(*this, Var->getDeclContext(),
5664 VarSpec->getTemplateArgsAsWritten()) {
5665 TemplateArgInfo.setLAngleLoc(ArgInfo->getLAngleLoc());
5666 TemplateArgInfo.setRAngleLoc(ArgInfo->getRAngleLoc());
5667 for (const TemplateArgumentLoc &Arg : ArgInfo->arguments())
5672 VarSpec->getSpecializedTemplate(), Def, TemplateArgInfo,
5673 VarSpec->getTemplateArgs().asArray(), VarSpec));
5677 VarSpec->getSpecializedTemplateOrPartial();
5680 cast<VarTemplateSpecializationDecl>(Var)->setInstantiationOf(
5681 Partial, &VarSpec->getTemplateInstantiationArgs());
5695 Var->setTemplateSpecializationKind(OldVar->getTemplateSpecializationKind(),
5696 OldVar->getPointOfInstantiation());
5712 bool AnyErrors = Tmpl->isInvalidDecl();
5715 for (const auto *Init : Tmpl->inits()) {
5716 // Only instantiate written initializers, let Sema re-construct implicit
5718 if (!Init->isWritten())
5723 if (Init->isPackExpansion()) {
5725 TypeLoc BaseTL = Init->getTypeSourceInfo()->getTypeLoc();
5728 collectUnexpandedParameterPacks(Init->getInit(), Unexpanded);
5732 if (CheckParameterPacksForExpansion(Init->getEllipsisLoc(),
5739 New->setInvalidDecl();
5749 ExprResult TempInit = SubstInitializer(Init->getInit(), TemplateArgs,
5757 TypeSourceInfo *BaseTInfo = SubstType(Init->getTypeSourceInfo(),
5759 Init->getSourceLocation(),
5760 New->getDeclName());
5767 MemInitResult NewInit = BuildBaseInitializer(BaseTInfo->getType(),
5769 New->getParent(),
5783 ExprResult TempInit = SubstInitializer(Init->getInit(), TemplateArgs,
5791 if (Init->isDelegatingInitializer() || Init->isBaseInitializer()) {
5792 TypeSourceInfo *TInfo = SubstType(Init->getTypeSourceInfo(),
5794 Init->getSourceLocation(),
5795 New->getDeclName());
5798 New->setInvalidDecl();
5802 if (Init->isBaseInitializer())
5803 NewInit = BuildBaseInitializer(TInfo->getType(), TInfo, TempInit.get(),
5804 New->getParent(), EllipsisLoc);
5807 cast<CXXRecordDecl>(CurContext->getParent()));
5808 } else if (Init->isMemberInitializer()) {
5810 Init->getMemberLocation(),
5811 Init->getMember(),
5815 New->setInvalidDecl();
5820 Init->getSourceLocation());
5821 } else if (Init->isIndirectMemberInitializer()) {
5824 Init->getMemberLocation(),
5825 Init->getIndirectMember(), TemplateArgs));
5829 New->setInvalidDecl();
5834 Init->getSourceLocation());
5839 New->setInvalidDecl();
5857 Pattern = Pattern->getCanonicalDecl();
5860 Instance = Instance->getCanonicalDecl();
5862 Instance = Instance->getInstantiatedFromMemberTemplate();
5870 Pattern = Pattern->getCanonicalDecl();
5873 Instance = Instance->getCanonicalDecl();
5875 Instance = Instance->getInstantiatedFromMemberTemplate();
5885 = cast<ClassTemplatePartialSpecializationDecl>(Pattern->getCanonicalDecl());
5888 Instance->getCanonicalDecl());
5891 Instance = Instance->getInstantiatedFromMember();
5899 Pattern = Pattern->getCanonicalDecl();
5902 Instance = Instance->getCanonicalDecl();
5904 Instance = Instance->getInstantiatedFromMemberClass();
5912 Pattern = Pattern->getCanonicalDecl();
5915 Instance = Instance->getCanonicalDecl();
5917 Instance = Instance->getInstantiatedFromMemberFunction();
5925 Pattern = Pattern->getCanonicalDecl();
5928 Instance = Instance->getCanonicalDecl();
5930 Instance = Instance->getInstantiatedFromMemberEnum();
5960 OtherIsPackExpansion = OtherUUD->isPackExpansion();
5964 OtherFrom = OtherUPD->getInstantiatedFromUsingDecl();
5971 return Pattern->isPackExpansion() == OtherIsPackExpansion &&
5977 assert(Instance->isStaticDataMember());
5979 Pattern = Pattern->getCanonicalDecl();
5982 Instance = Instance->getCanonicalDecl();
5984 Instance = Instance->getInstantiatedFromStaticDataMember();
5999 if (D->getKind() != Other->getKind())
6012 if (Var->isStaticDataMember())
6027 if (!Field->getDeclName()) {
6040 return D->getDeclName() &&
6041 D->getDeclName() == cast<NamedDecl>(Other)->getDeclName();
6071 if (!DC->isDependentContext())
6075 return cast<Decl>(DC)->getTemplateDepth() > Level;
6081 DeclContext *ParentDC = D->getDeclContext();
6087 // parameters (p below) can have their ParentDC set to the translation-unit
6088 // - thus we can not consistently check if the ParentDC of such a parameter
6093 // e.g. template <class T> void Foo(auto (*p)(T y) -> decltype(y())) {}
6099 // - as long as we have a ParmVarDecl whose parent is non-dependent and
6101 // - otherwise find its instantiated decl.
6103 !cast<ParmVarDecl>(D)->getType()->isInstantiationDependentType())
6107 (ParentDependsOnArgs && (ParentDC->isFunctionOrMethod() ||
6110 (isa<CXXRecordDecl>(D) && cast<CXXRecordDecl>(D)->isLambda() &&
6111 cast<CXXRecordDecl>(D)->getTemplateDepth() >
6116 if (auto Found = CurrentInstantiationScope->findInstantiationOf(D)) {
6117 if (Decl *FD = Found->dyn_cast<Decl *>())
6121 assert(PackIdx != -1 &&
6124 return cast<NamedDecl>((*Found->get<DeclArgumentPack *>())[PackIdx]);
6135 if (D->isInvalidDecl())
6153 NeedInstantiate = RD->isLocalClass();
6155 isa<CXXDeductionGuideDecl>(D->getDeclContext()))
6161 CurrentInstantiationScope->InstantiatedLocal(D, Inst);
6172 CurrentInstantiationScope->InstantiatedLocal(D, Inst);
6177 if (!Record->isDependentContext())
6182 ClassTemplateDecl *ClassTemplate = Record->getDescribedClassTemplate();
6184 ClassTemplate = ClassTemplate->getCanonicalDecl();
6187 ClassTemplate = Spec->getSpecializedTemplate()->getCanonicalDecl();
6192 while (!DC->isFileContext()) {
6195 if (DC->Equals(Record))
6203 ClassTemplateDecl *SpecTemplate = InstSpec->getSpecializedTemplate();
6215 if (FD->getFriendObjectKind() &&
6216 FD->getNonTransparentDeclContext()->isFileContext()) {
6217 DC = FD->getLexicalDeclContext();
6223 if (Guide && Guide->isImplicit()) {
6224 TemplateDecl *TD = Guide->getDeducedTemplate();
6225 // Convert the arguments to an "as-written" list.
6228 TD->getTemplateParameters()->size())) {
6237 // We may get a non-null type with errors, in which case
6242 if (T.isNull() || T->containsErrors())
6244 CXXRecordDecl *SubstRecord = T->getAsCXXRecordDecl();
6256 // Check that this template-id names the primary template and not a
6265 << T << (SubstRecord->getTemplateSpecializationKind() ==
6274 DC = DC->getParent();
6288 if (ParentDC != D->getDeclContext()) {
6297 if (!Spec->isDependentContext()) {
6299 const RecordType *Tag = T->getAs<RecordType>();
6300 assert(Tag && "type of non-dependent record is not a RecordType");
6301 if (Tag->isBeingDefined())
6303 if (!Tag->isBeingDefined() &&
6307 ParentDC = Tag->getDecl();
6314 if (auto Name = D->getDeclName()) {
6315 DeclarationNameInfo NameInfo(Name, D->getLocation());
6321 DeclContext::lookup_result Found = ParentDC->lookup(Name);
6329 // - anonymous struct/union within a template
6330 // - unnamed class/struct/union/enum within a template
6334 ParentDC->decls_begin(),
6335 ParentDC->decls_end());
6342 // We've already complained about some ill-formed code, so most likely
6345 // FIXME: Use more fine-grained 'invalid' tracking for this.
6353 << D->getDeclName()
6355 Diag(D->getLocation(), diag::note_non_instantiated_member_here);
6360 EnumDecl *Enum = cast<EnumDecl>(ED->getLexicalDeclContext());
6363 assert(Spec->getTemplateSpecializationKind() ==
6366 << D->getDeclName()
6367 << Context.getTypeDeclType(cast<TypeDecl>(Spec->getDeclContext()));
6368 Diag(Spec->getLocation(), diag::note_enum_specialized_here)
6398 bool DefinitionRequired = Function->getTemplateSpecializationKind() ==
6400 if (Function->isMultiVersion()) {
6405 if (CurFD->isDefined())
6406 CurFD->setInstantiationIsPending(false);
6411 if (Function->isDefined())
6412 Function->setInstantiationIsPending(false);
6414 // Definition of a PCH-ed template declaration may be available only in the TU.
6416 TUKind == TU_Prefix && Function->instantiationIsPending())
6424 assert((Var->isStaticDataMember() ||
6431 if (Var->getMostRecentDecl()->isInvalidDecl())
6436 switch (Var->getMostRecentDecl()
6437 ->getTemplateSpecializationKindForInstantiation()) {
6446 if (Var != Var->getMostRecentDecl())
6455 bool DefinitionRequired = Var->getTemplateSpecializationKind() ==
6470 for (auto *DD : Pattern->ddiags()) {
6471 switch (DD->getKind()) {