32#include "llvm/Support/TimeProfiler.h"
43 return cast<CXXRecordDecl>(DC)->isLocalClass();
48template<
typename DeclT>
51 if (!OldDecl->getQualifierLoc())
54 assert((NewDecl->getFriendObjectKind() ||
55 !OldDecl->getLexicalDeclContext()->isDependentContext()) &&
56 "non-friend with qualified name defined in dependent context");
59 const_cast<DeclContext *
>(NewDecl->getFriendObjectKind()
60 ? NewDecl->getLexicalDeclContext()
61 : OldDecl->getLexicalDeclContext()));
70 NewDecl->setQualifierInfo(NewQualifierLoc);
76 return ::SubstQualifier(SemaRef, OldDecl, NewDecl, TemplateArgs);
81 return ::SubstQualifier(SemaRef, OldDecl, NewDecl, TemplateArgs);
85#include "clang/Sema/AttrTemplateInstantiate.inc"
89 const AlignedAttr *Aligned,
Decl *New,
bool IsPackExpansion) {
90 if (Aligned->isAlignmentExpr()) {
99 S.
SubstType(Aligned->getAlignmentType(), TemplateArgs,
102 Aligned->getLocation(),
103 Result->getTypeLoc().getSourceRange()))
111 const AlignedAttr *Aligned,
Decl *New) {
112 if (!Aligned->isPackExpansion()) {
118 if (Aligned->isAlignmentExpr())
124 assert(!Unexpanded.empty() &&
"Pack expansion without parameter packs?");
127 bool Expand =
true, RetainExpansion =
false;
128 std::optional<unsigned> NumExpansions;
132 Unexpanded, TemplateArgs, Expand,
133 RetainExpansion, NumExpansions))
140 for (
unsigned I = 0; I != *NumExpansions; ++I) {
149 const AssumeAlignedAttr *Aligned,
Decl *New) {
154 Expr *E, *OE =
nullptr;
160 if (Aligned->getOffset()) {
172 const AlignValueAttr *Aligned,
Decl *New) {
183 const AllocAlignAttr *Align,
Decl *New) {
186 llvm::APInt(64, Align->getParamIndex().getSourceIndex()),
193 const AnnotateAttr *
Attr,
Decl *New) {
199 bool HasDelayedArgs =
Attr->delayedArgs_size();
208 false, TemplateArgs, Args))
211 StringRef Str =
Attr->getAnnotation();
212 if (HasDelayedArgs) {
213 if (Args.size() < 1) {
223 ActualArgs.insert(ActualArgs.begin(), Args.begin() + 1, Args.end());
224 std::swap(Args, ActualArgs);
232 Expr *Cond =
nullptr;
246 Cond = Converted.
get();
252 S.
Diag(A->
getLocation(), diag::err_attr_cond_never_constant_expr) << A;
253 for (
const auto &
P : Diags)
254 S.
Diag(
P.first,
P.second);
264 S, TemplateArgs, EIA, EIA->getCond(), Tmpl, New);
268 Cond, EIA->getMessage()));
275 S, TemplateArgs, DIA, DIA->getCond(), Tmpl, New);
280 DIA->getDiagnosticType(), DIA->getArgDependent(), New));
287 const CUDALaunchBoundsAttr &
Attr,
Decl *New) {
297 Expr *MinBlocks =
nullptr;
298 if (
Attr.getMinBlocks()) {
319 const OMPDeclareSimdDeclAttr &
Attr,
Decl *New) {
321 if (
auto *FTD = dyn_cast<FunctionTemplateDecl>(New))
322 New = FTD->getTemplatedDecl();
323 auto *FD = cast<FunctionDecl>(New);
324 auto *ThisContext = dyn_cast_or_null<CXXRecordDecl>(FD->getDeclContext());
329 if (
auto *DRE = dyn_cast<DeclRefExpr>(E->IgnoreParenImpCasts()))
330 if (
auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
333 if (FD->getNumParams() > PVD->getFunctionScopeIndex())
334 Local.InstantiatedLocal(
335 PVD, FD->getParamDecl(PVD->getFunctionScopeIndex()));
339 FD->isCXXInstanceMember());
355 if (
auto *E =
Attr.getSimdlen())
358 if (
Attr.uniforms_size() > 0) {
359 for(
auto *E :
Attr.uniforms()) {
363 Uniforms.push_back(Inst.
get());
367 auto AI =
Attr.alignments_begin();
368 for (
auto *E :
Attr.aligneds()) {
372 Aligneds.push_back(Inst.
get());
376 Alignments.push_back(Inst.
get());
380 auto SI =
Attr.steps_begin();
381 for (
auto *E :
Attr.linears()) {
385 Linears.push_back(Inst.
get());
389 Steps.push_back(Inst.
get());
392 LinModifiers.append(
Attr.modifiers_begin(),
Attr.modifiers_end());
395 Uniforms, Aligneds, Alignments, Linears, LinModifiers, Steps,
402 const OMPDeclareVariantAttr &
Attr,
Decl *New) {
404 if (
auto *FTD = dyn_cast<FunctionTemplateDecl>(New))
405 New = FTD->getTemplatedDecl();
406 auto *FD = cast<FunctionDecl>(New);
407 auto *ThisContext = dyn_cast_or_null<CXXRecordDecl>(FD->getDeclContext());
409 auto &&SubstExpr = [FD, ThisContext, &S, &TemplateArgs](
Expr *E) {
410 if (
auto *DRE = dyn_cast<DeclRefExpr>(E->IgnoreParenImpCasts()))
411 if (
auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
414 if (FD->getNumParams() > PVD->getFunctionScopeIndex())
415 Local.InstantiatedLocal(
416 PVD, FD->getParamDecl(PVD->getFunctionScopeIndex()));
420 FD->isCXXInstanceMember());
426 auto &&Subst = [&SubstExpr, &S](
Expr *E) {
436 if (
Expr *E =
Attr.getVariantFuncRef()) {
441 VariantFuncRef = Subst(E);
447 TI = *
Attr.getTraitInfos();
450 auto SubstScoreOrConditionExpr = [&S, Subst](
Expr *&E,
bool) {
465 Expr *E = VariantFuncRef.
get();
469 std::optional<std::pair<FunctionDecl *, Expr *>> DeclVarData =
471 Attr.appendArgs_size(),
477 E = DeclVarData->second;
478 FD = DeclVarData->first;
480 if (
auto *VariantDRE = dyn_cast<DeclRefExpr>(E->IgnoreParenImpCasts())) {
481 if (
auto *VariantFD = dyn_cast<FunctionDecl>(VariantDRE->getDecl())) {
482 if (
auto *VariantFTD = VariantFD->getDescribedFunctionTemplate()) {
483 if (!VariantFTD->isThisDeclarationADefinition())
494 SubstFD->getType(), FD->getType(),
502 SubstFD->setInstantiationIsPending(!SubstFD->isDefined());
506 SubstFD->getLocation(),
516 for (
Expr *E :
Attr.adjustArgsNothing()) {
520 NothingExprs.push_back(ER.
get());
522 for (
Expr *E :
Attr.adjustArgsNeedDevicePtr()) {
526 NeedDevicePtrExprs.push_back(ER.
get());
530 AppendArgs.emplace_back(II.IsTarget, II.IsTargetSync);
534 FD, E, TI, NothingExprs, NeedDevicePtrExprs, AppendArgs,
SourceLocation(),
540 const AMDGPUFlatWorkGroupSizeAttr &
Attr,
Decl *New) {
565 Expr *Cond =
nullptr;
573 Cond = SubstResult.
get();
583 const AMDGPUWavesPerEUAttr &
Attr,
Decl *New) {
593 Expr *MaxExpr =
nullptr;
594 if (
auto Max =
Attr.getMax()) {
609 const SYCLKernelAttr &
Attr,
Decl *New) {
619 if (
const auto *PNA = dyn_cast<PreferredNameAttr>(A)) {
621 const auto *RD = cast<CXXRecordDecl>(D);
625 for (
const auto *ExistingPNA : D->
specific_attrs<PreferredNameAttr>())
627 PNA->getTypedefType()))
632 if (
const auto *BA = dyn_cast<BuiltinAttr>(A)) {
634 switch (BA->getID()) {
635 case Builtin::BIforward:
645 case Builtin::BImove:
646 case Builtin::BImove_if_noexcept:
664 if (
NamedDecl *ND = dyn_cast<NamedDecl>(New)) {
668 for (
const auto *TmplAttr : Tmpl->
attrs()) {
675 *
this, dyn_cast_or_null<CXXRecordDecl>(ND->getDeclContext()),
679 TmplAttr,
Context, *
this, TemplateArgs);
689 case clang::attr::CFConsumed:
691 case clang::attr::OSConsumed:
693 case clang::attr::NSConsumed:
696 llvm_unreachable(
"Wrong argument supplied");
704 for (
const auto *TmplAttr : Tmpl->
attrs()) {
709 const AlignedAttr *Aligned = dyn_cast<AlignedAttr>(TmplAttr);
710 if (Aligned && Aligned->isAlignmentDependent()) {
715 if (
const auto *AssumeAligned = dyn_cast<AssumeAlignedAttr>(TmplAttr)) {
720 if (
const auto *AlignValue = dyn_cast<AlignValueAttr>(TmplAttr)) {
725 if (
const auto *AllocAlign = dyn_cast<AllocAlignAttr>(TmplAttr)) {
730 if (
const auto *Annotate = dyn_cast<AnnotateAttr>(TmplAttr)) {
735 if (
const auto *EnableIf = dyn_cast<EnableIfAttr>(TmplAttr)) {
737 cast<FunctionDecl>(New));
741 if (
const auto *DiagnoseIf = dyn_cast<DiagnoseIfAttr>(TmplAttr)) {
743 cast<FunctionDecl>(New));
747 if (
const auto *CUDALaunchBounds =
748 dyn_cast<CUDALaunchBoundsAttr>(TmplAttr)) {
750 *CUDALaunchBounds, New);
754 if (
const auto *Mode = dyn_cast<ModeAttr>(TmplAttr)) {
759 if (
const auto *OMPAttr = dyn_cast<OMPDeclareSimdDeclAttr>(TmplAttr)) {
764 if (
const auto *OMPAttr = dyn_cast<OMPDeclareVariantAttr>(TmplAttr)) {
769 if (
const auto *AMDGPUFlatWorkGroupSize =
770 dyn_cast<AMDGPUFlatWorkGroupSizeAttr>(TmplAttr)) {
772 *
this, TemplateArgs, *AMDGPUFlatWorkGroupSize, New);
775 if (
const auto *AMDGPUFlatWorkGroupSize =
776 dyn_cast<AMDGPUWavesPerEUAttr>(TmplAttr)) {
778 *AMDGPUFlatWorkGroupSize, New);
782 if (TmplAttr->getKind() == attr::DLLExport ||
783 TmplAttr->getKind() == attr::DLLImport) {
784 if (New->
hasAttr<DLLExportAttr>() || New->
hasAttr<DLLImportAttr>()) {
789 if (
const auto *ABIAttr = dyn_cast<ParameterABIAttr>(TmplAttr)) {
794 if (isa<NSConsumedAttr>(TmplAttr) || isa<OSConsumedAttr>(TmplAttr) ||
795 isa<CFConsumedAttr>(TmplAttr)) {
801 if (
auto *A = dyn_cast<PointerAttr>(TmplAttr)) {
802 if (!New->
hasAttr<PointerAttr>())
807 if (
auto *A = dyn_cast<OwnerAttr>(TmplAttr)) {
808 if (!New->
hasAttr<OwnerAttr>())
813 if (
auto *A = dyn_cast<SYCLKernelAttr>(TmplAttr)) {
818 assert(!TmplAttr->isPackExpansion());
819 if (TmplAttr->isLateParsed() && LateAttrs) {
828 auto *ND = cast<NamedDecl>(New);
829 auto *ThisContext = dyn_cast_or_null<CXXRecordDecl>(ND->getDeclContext());
831 ND->isCXXInstanceMember());
834 *
this, TemplateArgs);
848 for (
const auto *
Attr : Pattern->
attrs()) {
849 if (
auto *A = dyn_cast<StrictFPAttr>(
Attr)) {
850 if (!Inst->
hasAttr<StrictFPAttr>())
867 DLLExportAttr *
Attr = Ctor->
getAttr<DLLExportAttr>();
870 for (
unsigned I = 0; I != NumParams; ++I) {
881template<
typename DeclT>
883 DeclT *
Result = D->getPreviousDecl();
888 if (
Result && isa<CXXRecordDecl>(D->getDeclContext()) &&
889 D->getLexicalDeclContext() !=
Result->getLexicalDeclContext())
897 llvm_unreachable(
"Translation units cannot be instantiated");
901 llvm_unreachable(
"HLSL buffer declarations cannot be instantiated");
906 llvm_unreachable(
"pragma comment cannot be instantiated");
909Decl *TemplateDeclInstantiator::VisitPragmaDetectMismatchDecl(
911 llvm_unreachable(
"pragma comment cannot be instantiated");
916 llvm_unreachable(
"extern \"C\" context cannot be instantiated");
920 llvm_unreachable(
"GUID declaration cannot be instantiated");
923Decl *TemplateDeclInstantiator::VisitUnnamedGlobalConstantDecl(
925 llvm_unreachable(
"UnnamedGlobalConstantDecl cannot be instantiated");
928Decl *TemplateDeclInstantiator::VisitTemplateParamObjectDecl(
930 llvm_unreachable(
"template parameter objects cannot be instantiated");
934TemplateDeclInstantiator::VisitLabelDecl(
LabelDecl *D) {
942TemplateDeclInstantiator::VisitNamespaceDecl(
NamespaceDecl *D) {
943 llvm_unreachable(
"Namespaces cannot be instantiated");
1007 TagDecl *oldTag = oldTagType->getDecl();
1068 if (getPreviousDeclForInstantiation<TypedefNameDecl>(Pattern)) {
1070 if (!Found.
empty()) {
1071 PrevAliasTemplate = dyn_cast<TypeAliasTemplateDecl>(Found.
front());
1084 if (PrevAliasTemplate)
1089 if (!PrevAliasTemplate)
1109 NewBindings.push_back(cast<BindingDecl>(VisitBindingDecl(OldBD)));
1112 auto *NewDD = cast_or_null<DecompositionDecl>(
1115 if (!NewDD || NewDD->isInvalidDecl())
1116 for (
auto *NewBD : NewBindings)
1117 NewBD->setInvalidDecl();
1127 bool InstantiatingVarTemplate,
1138 SemaRef.
Diag(D->
getLocation(), diag::err_variable_instantiates_to_function)
1171 StartingScope, InstantiatingVarTemplate);
1174 if (
auto *F = dyn_cast<FunctionDecl>(DC))
1175 RT = F->getReturnType();
1176 else if (isa<BlockDecl>(DC))
1180 llvm_unreachable(
"Unknown context type");
1217Decl *TemplateDeclInstantiator::VisitFieldDecl(
FieldDecl *D) {
1222 DI = SemaRef.
SubstType(DI, TemplateArgs,
1234 SemaRef.
Diag(D->
getLocation(), diag::err_field_instantiates_to_function)
1245 else if (BitWidth) {
1251 = SemaRef.
SubstExpr(BitWidth, TemplateArgs);
1256 BitWidth = InstantiatedBitWidth.
getAs<
Expr>();
1261 cast<RecordDecl>(Owner),
1274 SemaRef.
InstantiateAttrs(TemplateArgs, D, Field, LateAttrs, StartingScope);
1276 if (
Field->hasAttrs())
1280 Field->setInvalidDecl();
1282 if (!
Field->getDeclName()) {
1287 if (
Parent->isAnonymousStructOrUnion() &&
1288 Parent->getRedeclContext()->isFunctionOrMethod())
1304 SemaRef.
Diag(D->
getLocation(), diag::err_property_is_variably_modified)
1308 DI = SemaRef.
SubstType(DI, TemplateArgs,
1320 SemaRef.
Diag(D->
getLocation(), diag::err_field_instantiates_to_function)
1349 for (
auto *PI : D->
chain()) {
1355 NamedChain[i++] = Next;
1358 QualType T = cast<FieldDecl>(NamedChain[i-1])->getType();
1361 {NamedChain, D->getChainingSize()});
1368 Owner->
addDecl(IndirectField);
1369 return IndirectField;
1384 InstTy = SemaRef.
SubstType(Ty, TemplateArgs,
1402 assert(ND &&
"friend decl must be a decl or a type!");
1409 if (!NewND)
return nullptr;
1428 = SemaRef.
SubstExpr(AssertExpr, TemplateArgs);
1434 if (InstantiatedMessageExpr.
isInvalid())
1442Decl *TemplateDeclInstantiator::VisitEnumDecl(
EnumDecl *D) {
1448 if (!Prev)
return nullptr;
1449 PrevDecl = cast<EnumDecl>(Prev);
1467 Enum->setIntegerTypeSourceInfo(NewTI);
1470 &&
"Dependent type without type source info");
1493 if (Def && Def != D) {
1500 SemaRef.
SubstType(TI->getType(), TemplateArgs,
1503 DefnUnderlying,
true,
Enum);
1524 Enum->startDefinition();
1535 if (
Expr *UninstValue = EC->getInitExpr()) {
1545 if (
Value.isInvalid()) {
1558 Enum->setInvalidDecl();
1565 Enum->addDecl(EnumConst);
1566 Enumerators.push_back(EnumConst);
1567 LastEnumConst = EnumConst;
1570 !
Enum->isScoped()) {
1583 llvm_unreachable(
"EnumConstantDecls can only occur within EnumDecls.");
1588 llvm_unreachable(
"BuiltinTemplateDecls cannot be instantiated.");
1620 if (!Found.
empty()) {
1621 PrevClassTemplate = dyn_cast<ClassTemplateDecl>(Found.
front());
1622 if (PrevClassTemplate)
1635 SS.
Adopt(QualifierLoc);
1637 if (!DC)
return nullptr;
1651 if (R.isSingleResult()) {
1653 if (PrevClassTemplate)
1657 if (!PrevClassTemplate && QualifierLoc) {
1685 if (PrevClassTemplate) {
1716 if (!PrevClassTemplate)
1739 if (!PrevClassTemplate) {
1745 for (
unsigned I = 0, N = PartialSpecs.size(); I != N; ++I)
1746 if (PartialSpecs[I]->getFirstDecl()->isOutOfLine())
1747 OutOfLinePartialSpecs.push_back(std::make_pair(Inst, PartialSpecs[I]));
1754TemplateDeclInstantiator::VisitClassTemplatePartialSpecializationDecl(
1766 = dyn_cast<ClassTemplateDecl>(Found.
front());
1767 if (!InstClassTemplate)
1779 "Only static data member templates are allowed.");
1795 PrevVarTemplate = dyn_cast<VarTemplateDecl>(Found.
front());
1801 if (!VarInst)
return nullptr;
1812 if (!PrevVarTemplate)
1822 if (!PrevVarTemplate) {
1828 for (
unsigned I = 0, N = PartialSpecs.size(); I != N; ++I)
1829 if (PartialSpecs[I]->getFirstDecl()->isOutOfLine())
1830 OutOfLineVarPartialSpecs.push_back(
1831 std::make_pair(Inst, PartialSpecs[I]));
1837Decl *TemplateDeclInstantiator::VisitVarTemplatePartialSpecializationDecl(
1840 "Only static data member templates are allowed.");
1846 assert(!Found.
empty() &&
"Instantiation found nothing?");
1849 assert(InstVarTemplate &&
"Instantiation did not find a variable template?");
1889 assert(InstTemplate &&
1890 "VisitFunctionDecl/CXXMethodDecl didn't create a template!");
1908 return InstTemplate;
1917 if (!Prev)
return nullptr;
1918 PrevDecl = cast<CXXRecordDecl>(Prev);
1932 IsInjectedClassName);
1934 if (IsInjectedClassName)
1950 if (!IsInjectedClassName)
1956 Record->setObjectOfFriendDecl();
1960 Record->setAnonymousStructOrUnion(
true);
1998 LocalInstantiations.perform();
2003 if (IsInjectedClassName)
2004 assert(
Record->isInjectedClassName() &&
"Broken injected-class-name");
2022 if (OrigFunc->
getExtInfo() == NewFunc->getExtInfo())
2028 NewFunc->getParamTypes(), NewEPI);
2042 if (FunctionTemplate && !TemplateParams) {
2045 void *InsertPos =
nullptr;
2055 if (FunctionTemplate)
2060 bool MergeWithParentScope = (TemplateParams !=
nullptr) ||
2062 !(isa<Decl>(Owner) &&
2063 cast<Decl>(Owner)->isDefinedOutsideFunctionOrMethod());
2067 if (
auto *DGuide = dyn_cast<CXXDeductionGuideDecl>(D)) {
2069 SemaRef, TemplateArgs, DGuide->getExplicitSpecifier(), DGuide);
2070 if (InstantiatedExplicitSpecifier.
isInvalid())
2080 if (TemplateParams && TemplateParams->
size()) {
2082 dyn_cast<TemplateTypeParmDecl>(TemplateParams->
asArray().back());
2083 if (LastParam && LastParam->isImplicit() &&
2084 LastParam->hasTypeConstraint()) {
2114 }
else if (isFriend && QualifierLoc) {
2116 SS.
Adopt(QualifierLoc);
2118 if (!DC)
return nullptr;
2131 if (
auto *DGuide = dyn_cast<CXXDeductionGuideDecl>(D)) {
2134 InstantiatedExplicitSpecifier, NameInfo, T, TInfo,
2136 DGuide->getDeductionCandidateKind());
2143 TrailingRequiresClause);
2144 Function->setFriendConstraintRefersToEnclosingTemplate(
2150 Function->setImplicitlyInline();
2153 Function->setQualifierInfo(QualifierLoc);
2156 Function->setLocalExternDecl();
2167 Function->setLexicalDeclContext(LexicalDC);
2170 for (
unsigned P = 0;
P < Params.size(); ++
P)
2172 Params[
P]->setOwningFunction(Function);
2173 Function->setParams(Params);
2175 if (TrailingRequiresClause)
2176 Function->setTrailingRequiresClause(TrailingRequiresClause);
2178 if (TemplateParams) {
2194 Function->getLocation(),
2195 Function->getDeclName(),
2196 TemplateParams, Function);
2197 Function->setDescribedFunctionTemplate(FunctionTemplate);
2205 }
else if (FunctionTemplate) {
2208 Function->setFunctionTemplateSpecialization(FunctionTemplate,
2217 }
else if (!isFriend) {
2221 Function->setInstantiatedFromDecl(D);
2225 Function->setObjectOfFriendDecl();
2227 FT->setObjectOfFriendDecl();
2231 Function->setInvalidDecl();
2233 bool IsExplicitSpecialization =
false;
2244 assert(isFriend &&
"non-friend has dependent specialization info?");
2248 Info->getRAngleLoc());
2254 for (
unsigned I = 0, E = Info->getNumTemplates(); I != E; ++I) {
2256 Info->getTemplate(I),
2258 if (!Temp)
return nullptr;
2260 Previous.addDecl(cast<FunctionTemplateDecl>(Temp));
2266 Function->setInvalidDecl();
2268 IsExplicitSpecialization =
true;
2276 Info->getRAngleLoc());
2284 Function->setInvalidDecl();
2286 IsExplicitSpecialization =
true;
2287 }
else if (TemplateParams || !FunctionTemplate) {
2303 if (isFriend && !QualifierLoc) {
2323 if (Function->isLocalExternDecl()) {
2325 if (!PVD->hasDefaultArg())
2331 Expr *UninstExpr = PVD->getUninstantiatedDefaultArg();
2334 { UninstExpr }, UninstExpr->
getType());
2336 PVD->setDefaultArg(ErrorResult.
get());
2342 IsExplicitSpecialization,
2343 Function->isThisDeclarationADefinition());
2349 if (isFriend && TemplateParams && FunctionTemplate->
getPreviousDecl()) {
2353 Function->isThisDeclarationADefinition()
2364 Function->getMemberSpecializationInfo()) {
2365 if (MSInfo->getPointOfInstantiation().isInvalid()) {
2367 MSInfo->setPointOfInstantiation(Loc);
2369 std::make_pair(Function, Loc));
2382 (TemplateParams ? cast<NamedDecl>(FunctionTemplate) : Function);
2387 (Function->isLocalExternDecl() && !Function->getPreviousDecl()))
2390 if (Function->isOverloadedOperator() && !DC->
isRecord() &&
2399 std::optional<const ASTTemplateArgumentListInfo *>
2400 ClassScopeSpecializationArgs,
2403 if (FunctionTemplate && !TemplateParams) {
2409 void *InsertPos =
nullptr;
2419 if (FunctionTemplate)
2424 bool MergeWithParentScope = (TemplateParams !=
nullptr) ||
2425 !(isa<Decl>(Owner) &&
2426 cast<Decl>(Owner)->isDefinedOutsideFunctionOrMethod());
2434 unsigned NumTempParamLists = 0;
2436 TempParamLists.resize(NumTempParamLists);
2437 for (
unsigned I = 0; I != NumTempParamLists; ++I) {
2442 TempParamLists[I] = InstParams;
2449 if (InstantiatedExplicitSpecifier.
isInvalid())
2458 if (cast<CXXRecordDecl>(D->
getParent())->isLambda() &&
2460 isa<CXXConstructorDecl, CXXDestructorDecl>(D)) {
2472 if (TemplateParams && TemplateParams->
size()) {
2474 dyn_cast<TemplateTypeParmDecl>(TemplateParams->
asArray().back());
2475 if (LastParam && LastParam->isImplicit() &&
2476 LastParam->hasTypeConstraint()) {
2501 SS.
Adopt(QualifierLoc);
2511 if (!DC)
return nullptr;
2529 SemaRef.
Context, Record, StartLoc, NameInfo, T, TInfo,
2530 InstantiatedExplicitSpecifier, Constructor->UsesFPIntrin(),
2531 Constructor->isInlineSpecified(),
false,
2533 TrailingRequiresClause);
2537 SemaRef.
Context, Record, StartLoc, NameInfo, T, TInfo,
2538 Destructor->UsesFPIntrin(), Destructor->isInlineSpecified(),
false,
2539 Destructor->getConstexprKind(), TrailingRequiresClause);
2547 SemaRef.
Context, Record, StartLoc, NameInfo, T, TInfo,
2548 Conversion->UsesFPIntrin(), Conversion->isInlineSpecified(),
2549 InstantiatedExplicitSpecifier, Conversion->getConstexprKind(),
2550 Conversion->getEndLoc(), TrailingRequiresClause);
2554 SemaRef.
Context, Record, StartLoc, NameInfo, T, TInfo, SC,
2556 D->
getEndLoc(), TrailingRequiresClause);
2565 if (TemplateParams) {
2582 TemplateParams, Method);
2589 }
else if (FunctionTemplate) {
2596 }
else if (!isFriend) {
2605 if (NumTempParamLists)
2616 for (
unsigned P = 0;
P < Params.size(); ++
P)
2617 Params[
P]->setOwningFunction(Method);
2626 bool IsExplicitSpecialization =
false;
2632 assert(isFriend &&
"non-friend has dependent specialization info?");
2636 Info->getRAngleLoc());
2642 for (
unsigned I = 0, E = Info->getNumTemplates(); I != E; ++I) {
2644 Info->getTemplate(I),
2646 if (!Temp)
return nullptr;
2648 Previous.addDecl(cast<FunctionTemplateDecl>(Temp));
2656 IsExplicitSpecialization =
true;
2658 ClassScopeSpecializationArgs.value_or(
2663 Info->getRAngleLoc());
2673 IsExplicitSpecialization =
true;
2674 }
else if (ClassScopeSpecializationArgs) {
2681 IsExplicitSpecialization =
true;
2682 }
else if (!FunctionTemplate || TemplateParams || isFriend) {
2709 for (
unsigned P = 0;
P < Params.size(); ++
P) {
2710 if (!Params[
P]->hasDefaultArg())
2716 Expr *UninstExpr = Params[
P]->getUninstantiatedDefaultArg();
2719 { UninstExpr }, UninstExpr->
getType());
2721 Params[
P]->setDefaultArg(ErrorResult.
get());
2727 IsExplicitSpecialization,
2740 if (FunctionTemplate)
2756 if (IsExplicitSpecialization && !isFriend)
2766 if (
auto *Constructor = dyn_cast<CXXConstructorDecl>(Method)) {
2767 if (Constructor->isDefaultConstructor() ||
2768 Constructor->isCopyOrMoveConstructor())
2776 if (FunctionTemplate) {
2784 }
else if (isFriend) {
2790 Record->makeDeclVisibleInContext(Method);
2801 if (Method->
hasAttr<UsedAttr>()) {
2802 if (
const auto *A = dyn_cast<CXXRecordDecl>(Owner)) {
2805 A->getMemberSpecializationInfo())
2806 Loc = MSInfo->getPointOfInstantiation();
2807 else if (
const auto *Spec = dyn_cast<ClassTemplateSpecializationDecl>(A))
2808 Loc = Spec->getPointOfInstantiation();
2834Decl *TemplateDeclInstantiator::VisitTemplateTypeParmDecl(
2838 std::optional<unsigned> NumExpanded;
2842 assert(TC->getTemplateArgsAsWritten() &&
2843 "type parameter can only be an expansion when explicit arguments "
2849 for (
auto &ArgLoc : TC->getTemplateArgsAsWritten()->arguments())
2855 bool RetainExpansion =
false;
2857 cast<CXXFoldExpr>(TC->getImmediatelyDeclaredConstraint())
2860 TC->hasExplicitTemplateArgs() ?
2861 TC->getTemplateArgsAsWritten()->getRAngleLoc() :
2862 TC->getConceptNameInfo().getEndLoc()),
2863 Unexpanded, TemplateArgs, Expand, RetainExpansion, NumExpanded))
2882 EvaluateConstraints))
2890 if (InstantiatedDefaultArg)
2901Decl *TemplateDeclInstantiator::VisitNonTypeTemplateParmDecl(
2907 bool IsExpandedParameterPack =
false;
2929 ExpandedParameterPackTypesAsWritten.push_back(NewDI);
2930 ExpandedParameterPackTypes.push_back(NewT);
2933 IsExpandedParameterPack =
true;
2948 bool RetainExpansion =
false;
2949 std::optional<unsigned> OrigNumExpansions =
2951 std::optional<unsigned> NumExpansions = OrigNumExpansions;
2953 Pattern.getSourceRange(),
2956 Expand, RetainExpansion,
2961 for (
unsigned I = 0; I != *NumExpansions; ++I) {
2974 ExpandedParameterPackTypesAsWritten.push_back(NewDI);
2975 ExpandedParameterPackTypes.push_back(NewT);
2981 IsExpandedParameterPack =
true;
3018 if (IsExpandedParameterPack)
3023 ExpandedParameterPackTypesAsWritten);
3031 if (AutoLoc.isConstrained())
3036 IsExpandedParameterPack
3051 if (!
Value.isInvalid())
3065 for (
const auto &
P : *Params) {
3066 if (
P->isTemplateParameterPack())
3078TemplateDeclInstantiator::VisitTemplateTemplateParmDecl(
3085 bool IsExpandedParameterPack =
false;
3099 ExpandedParams.push_back(Expansion);
3102 IsExpandedParameterPack =
true;
3103 InstParams = TempParams;
3115 bool RetainExpansion =
false;
3116 std::optional<unsigned> NumExpansions;
3121 Expand, RetainExpansion,
3126 for (
unsigned I = 0; I != *NumExpansions; ++I) {
3132 ExpandedParams.push_back(Expansion);
3138 IsExpandedParameterPack =
true;
3139 InstParams = TempParams;
3161 if (IsExpandedParameterPack)
3222 for (
auto *Shadow : D->
shadows()) {
3226 NamedDecl *OldTarget = Shadow->getTargetDecl();
3227 if (
auto *CUSD = dyn_cast<ConstructorUsingShadowDecl>(Shadow))
3228 if (
auto *BaseShadow = CUSD->getNominatedBaseClassShadowDecl())
3229 OldTarget = BaseShadow;
3233 dyn_cast<UnresolvedUsingIfExistsDecl>(Shadow->getTargetDecl())) {
3235 SemaRef.
Context, Owner, EmptyD->getLocation(), EmptyD->getDeclName());
3238 Shadow->getLocation(), OldTarget, TemplateArgs));
3250 Shadow->getLocation(), OldPrev, TemplateArgs));
3253 nullptr, Inst, InstTarget, PrevDecl);
3256 if (isFunctionScope)
3263Decl *TemplateDeclInstantiator::VisitUsingDecl(
UsingDecl *D) {
3284 if (
auto *RD = dyn_cast<CXXRecordDecl>(SemaRef.
CurContext))
3290 bool CheckRedeclaration = Owner->
isRecord();
3301 SS.
Adopt(QualifierLoc);
3302 if (CheckRedeclaration) {
3303 Prev.setHideTags(
false);
3368Decl *TemplateDeclInstantiator::VisitConstructorUsingShadowDecl(
3374template <
typename T>
3375Decl *TemplateDeclInstantiator::instantiateUnresolvedUsingDecl(
3376 T *D,
bool InstantiatingPackElement) {
3378 if (D->isPackExpansion() && !InstantiatingPackElement) {
3386 bool RetainExpansion =
false;
3387 std::optional<unsigned> NumExpansions;
3389 D->getEllipsisLoc(), D->getSourceRange(), Unexpanded, TemplateArgs,
3390 Expand, RetainExpansion, NumExpansions))
3395 assert(!RetainExpansion &&
3396 "should never need to retain an expansion for UsingPackDecl");
3402 return instantiateUnresolvedUsingDecl(D,
true);
3412 if (D->getDeclContext()->isFunctionOrMethod() && *NumExpansions > 1) {
3413 SemaRef.
Diag(D->getEllipsisLoc(),
3414 diag::err_using_decl_redeclaration_expansion);
3420 for (
unsigned I = 0; I != *NumExpansions; ++I) {
3422 Decl *Slice = instantiateUnresolvedUsingDecl(D,
true);
3429 Expansions.push_back(cast<NamedDecl>(Slice));
3448 SS.
Adopt(QualifierLoc);
3455 bool InstantiatingSlice = D->getEllipsisLoc().isValid() &&
3460 bool IsUsingIfExists = D->template hasAttr<UsingIfExistsAttr>();
3462 nullptr, D->getAccess(), D->getUsingLoc(),
3463 TD, TypenameLoc, SS, NameInfo, EllipsisLoc,
3465 true, IsUsingIfExists);
3474Decl *TemplateDeclInstantiator::VisitUnresolvedUsingTypenameDecl(
3476 return instantiateUnresolvedUsingDecl(D);
3479Decl *TemplateDeclInstantiator::VisitUnresolvedUsingValueDecl(
3481 return instantiateUnresolvedUsingDecl(D);
3484Decl *TemplateDeclInstantiator::VisitUnresolvedUsingIfExistsDecl(
3486 llvm_unreachable(
"referring to unresolved decl out of UsingShadowDecl");
3494 Expansions.push_back(NewUD);
3505Decl *TemplateDeclInstantiator::VisitClassScopeFunctionSpecializationDecl(
3508 return cast_or_null<CXXMethodDecl>(
3512Decl *TemplateDeclInstantiator::VisitOMPThreadPrivateDecl(
3517 assert(isa<DeclRefExpr>(Var) &&
"threadprivate arg is not a DeclRefExpr");
3518 Vars.push_back(Var);
3534 assert(isa<DeclRefExpr>(Var) &&
"allocate arg is not a DeclRefExpr");
3535 Vars.push_back(Var);
3541 if (
auto *AC = dyn_cast<OMPAllocatorClause>(
C)) {
3546 NewE.
get(), AC->getBeginLoc(), AC->getLParenLoc(), AC->getEndLoc());
3547 }
else if (
auto *AC = dyn_cast<OMPAlignClause>(
C)) {
3552 AC->getLParenLoc(), AC->getEndLoc());
3557 Clauses.push_back(IC);
3562 if (Res.
get().isNull())
3564 return Res.
get().getSingleDecl();
3569 "Requires directive cannot be instantiated within a dependent context");
3572Decl *TemplateDeclInstantiator::VisitOMPDeclareReductionDecl(
3575 const bool RequiresInstantiation =
3580 if (RequiresInstantiation) {
3586 SubstReductionType = D->
getType();
3588 if (SubstReductionType.
isNull())
3592 bool IsCorrect =
true;
3594 std::pair<QualType, SourceLocation> ReductionTypes[] = {
3595 std::make_pair(SubstReductionType, D->
getLocation())};
3597 if (PrevDeclInScope && !PrevDeclInScope->isInvalidDecl()) {
3598 PrevDeclInScope = cast<OMPDeclareReductionDecl>(
3605 auto *NewDRD = cast<OMPDeclareReductionDecl>(DRD.get().getSingleDecl());
3607 Expr *SubstCombiner =
nullptr;
3608 Expr *SubstInitializer =
nullptr;
3615 cast<DeclRefExpr>(NewDRD->getCombinerIn())->getDecl());
3618 cast<DeclRefExpr>(NewDRD->getCombinerOut())->getDecl());
3619 auto *ThisContext = dyn_cast_or_null<CXXRecordDecl>(Owner);
3622 SubstCombiner = SemaRef.
SubstExpr(Combiner, TemplateArgs).
get();
3631 cast<DeclRefExpr>(NewDRD->getInitOrig())->getDecl());
3634 cast<DeclRefExpr>(NewDRD->getInitPriv())->getDecl());
3636 SubstInitializer = SemaRef.
SubstExpr(Init, TemplateArgs).
get();
3639 cast<VarDecl>(cast<DeclRefExpr>(D->
getInitPriv())->getDecl());
3640 IsCorrect = IsCorrect && OldPrivParm->hasInit();
3648 IsCorrect = IsCorrect && SubstCombiner &&
3651 SubstInitializer) ||
3653 !SubstInitializer));
3664 const bool RequiresInstantiation =
3670 if (RequiresInstantiation) {
3678 if (SubstMapperTy.
isNull())
3682 if (PrevDeclInScope && !PrevDeclInScope->isInvalidDecl()) {
3683 PrevDeclInScope = cast<OMPDeclareMapperDecl>(
3687 bool IsCorrect =
true;
3698 cast<DeclRefExpr>(MapperVarRef.
get())->getDecl());
3699 auto *ThisContext = dyn_cast_or_null<CXXRecordDecl>(Owner);
3704 auto *OldC = cast<OMPMapClause>(
C);
3706 for (
Expr *OE : OldC->varlists()) {
3712 NewVars.push_back(NE);
3720 SS.
Adopt(NewQualifierLoc);
3726 OldC->getIteratorModifier(), OldC->getMapTypeModifiers(),
3727 OldC->getMapTypeModifiersLoc(), SS, NewNameInfo, OldC->getMapType(),
3728 OldC->isImplicitMapType(), OldC->getMapLoc(), OldC->getColonLoc(),
3730 Clauses.push_back(NewC);
3737 VN, D->
getAccess(), MapperVarRef.
get(), Clauses, PrevDeclInScope);
3738 Decl *NewDMD = DG.
get().getSingleDecl();
3743Decl *TemplateDeclInstantiator::VisitOMPCapturedExprDecl(
3745 llvm_unreachable(
"Should not be met in templates");
3765 llvm_unreachable(
"There are only CXXRecordDecls in C++");
3769TemplateDeclInstantiator::VisitClassTemplateSpecializationDecl(
3776 "can only instantiate an explicit specialization "
3777 "for a member class template");
3784 if (!InstClassTemplate)
3790 castAs<TemplateSpecializationTypeLoc>();
3794 for (
unsigned I = 0; I != Loc.
getNumArgs(); ++I)
3803 InstTemplateArgs,
false,
3804 SugaredConverted, CanonicalConverted,
3810 void *InsertPos =
nullptr;
3842 diag::note_previous_definition);
3850 D->
getLocation(), InstClassTemplate, CanonicalConverted, PrevDecl);
3905 assert(VarTemplate &&
3906 "A template specialization without specialized template?");
3911 if (!InstVarTemplate)
3917 VarTemplateArgsInfo.
setLAngleLoc(TemplateArgsInfo->getLAngleLoc());
3918 VarTemplateArgsInfo.
setRAngleLoc(TemplateArgsInfo->getRAngleLoc());
3921 TemplateArgs, VarTemplateArgsInfo))
3928 VarTemplateArgsInfo,
false,
3929 SugaredConverted, CanonicalConverted,
3934 void *InsertPos =
nullptr;
3948 InstVarTemplate, D, VarTemplateArgsInfo, CanonicalConverted, PrevDecl);
3965 SemaRef.
Diag(D->
getLocation(), diag::err_variable_instantiates_to_function)
3976 void *InsertPos =
nullptr;
3989 StartingScope,
false, PrevDecl);
3995 llvm_unreachable(
"@defs is not supported in Objective-C++");
4002 "cannot instantiate %0 yet");
4010 llvm_unreachable(
"Concept definitions cannot reside inside a template");
4013Decl *TemplateDeclInstantiator::VisitImplicitConceptSpecializationDecl(
4015 llvm_unreachable(
"Concept specializations cannot reside inside a template");
4025 llvm_unreachable(
"Unexpected decl");
4036 SubstD = Instantiator.Visit(D);
4051 SemaRef.
Context.
BoolTy, FPT->getParamTypes(), FPT->getExtProtoInfo());
4062 assert(OldLoc &&
"type of function is not a function type?");
4064 for (
unsigned I = 0, N = OldLoc.getNumParams(); I != N; ++I)
4065 NewLoc.
setParam(I, OldLoc.getParam(I));
4075 if (Spaceship->isInvalidDecl())
4087 if (
auto *MD = dyn_cast<CXXMethodDecl>(Spaceship)) {
4089 MD,
nullptr, std::nullopt,
4092 assert(Spaceship->getFriendObjectKind() &&
4093 "defaulted spaceship is neither a member nor a friend");
4103 cast<NamedDecl>(R), Spaceship->getBeginLoc());
4107 return cast_or_null<FunctionDecl>(R);
4121 unsigned N = L->
size();
4125 for (
auto &
P : *L) {
4127 Params.push_back(D);
4147 bool EvaluateConstraints) {
4194 ClassTemplate, PartialSpec->
getLocation(), InstTemplateArgs,
4195 false, SugaredConverted, CanonicalConverted))
4201 CanonicalConverted))
4206 void *InsertPos =
nullptr;
4246 SemaRef.
Diag(PartialSpec->
getLocation(), diag::err_partial_spec_redeclared)
4248 SemaRef.
Diag(PrevDecl->
getLocation(), diag::note_prev_partial_spec_here)
4259 ClassTemplate, CanonicalConverted, InstTemplateArgs, CanonType,
4275 return InstPartialSpec;
4319 VarTemplate, PartialSpec->
getLocation(), InstTemplateArgs,
4320 false, SugaredConverted, CanonicalConverted))
4326 CanonicalConverted))
4331 void *InsertPos =
nullptr;
4369 diag::err_var_partial_spec_redeclared)
4372 diag::note_var_prev_partial_spec_here);
4385 diag::err_variable_instantiates_to_function)
4413 LateAttrs, Owner, StartingScope);
4415 return InstPartialSpec;
4422 assert(OldTInfo &&
"substituting function without type source info");
4423 assert(Params.empty() &&
"parameter vector is non-empty at start");
4428 ThisContext = cast<CXXRecordDecl>(Owner);
4429 ThisTypeQuals = Method->getMethodQualifiers();
4434 ThisContext, ThisTypeQuals, EvaluateConstraints);
4440 if (NewTInfo != OldTInfo) {
4444 unsigned NewIdx = 0;
4445 for (
unsigned OldIdx = 0, NumOldParams = OldProtoLoc.getNumParams();
4446 OldIdx != NumOldParams; ++OldIdx) {
4447 ParmVarDecl *OldParam = OldProtoLoc.getParam(OldIdx);
4453 std::optional<unsigned> NumArgumentsInExpansion;
4455 NumArgumentsInExpansion =
4458 if (!NumArgumentsInExpansion) {
4462 Params.push_back(NewParam);
4463 Scope->InstantiatedLocal(OldParam, NewParam);
4466 Scope->MakeInstantiatedLocalArgPack(OldParam);
4467 for (
unsigned I = 0; I != *NumArgumentsInExpansion; ++I) {
4469 Params.push_back(NewParam);
4470 Scope->InstantiatedLocalPackArg(OldParam, NewParam);
4479 cast<FunctionProtoType>(OldProtoLoc.getType());
4480 for (
unsigned i = 0, i_end = OldProtoLoc.getNumParams(); i != i_end;
4490 cast_or_null<ParmVarDecl>(VisitParmVarDecl(OldParam));
4493 Params.push_back(Parm);
4510 TemplateArgs, ParamTypes, &Params,
4521bool Sema::addInstantiatedParametersToScope(
4525 unsigned FParamIdx = 0;
4526 for (
unsigned I = 0, N = PatternDecl->
getNumParams(); I != N; ++I) {
4530 assert(FParamIdx < Function->getNumParams());
4531 ParmVarDecl *FunctionParam = Function->getParamDecl(FParamIdx);
4548 Scope.InstantiatedLocal(PatternParam, FunctionParam);
4554 Scope.MakeInstantiatedLocalArgPack(PatternParam);
4555 std::optional<unsigned> NumArgumentsInExpansion =
4557 if (NumArgumentsInExpansion) {
4560 for (
unsigned Arg = 0; Arg < *NumArgumentsInExpansion; ++Arg) {
4573 Scope.InstantiatedLocalPackArg(PatternParam, FunctionParam);
4604 FD,
false,
nullptr,
true);
4610 L->DefaultArgumentInstantiated(Param);
4632 Diag(PointOfInstantiation, diag::err_exception_spec_cycle) <<
Decl;
4643 Decl,
false,
nullptr,
true);
4650 if (addInstantiatedParametersToScope(
Decl, Template,
Scope, TemplateArgs)) {
4684 if (ActiveInst.Kind == ActiveInstType::ExplicitTemplateArgumentSubstitution ||
4685 ActiveInst.Kind == ActiveInstType::DeducedTemplateArgumentSubstitution) {
4686 if (isa<FunctionTemplateDecl>(ActiveInst.Entity)) {
4688 {ActiveInst.Entity->getCanonicalDecl(), ActiveInst.Kind});
4690 ActiveInst.Kind = ActiveInstType::TemplateInstantiation;
4691 ActiveInst.Entity = New;
4697 assert(Proto &&
"Function template without prototype?");
4721 assert(NewProto &&
"Template instantiation without function prototype?");
4739 LateAttrs, StartingScope);
4755 if (isa<CXXDestructorDecl>(New) && SemaRef.
getLangOpts().CPlusPlus11)
4771 Lookups.reserve(DFI->getUnqualifiedLookups().size());
4772 bool AnyChanged =
false;
4775 DA.getDecl(), TemplateArgs);
4778 AnyChanged |= (D != DA.getDecl());
4807 *
this, Loc, FTD, Args->
asArray(),
4839 bool DefinitionRequired,
4841 if (Function->isInvalidDecl() || isa<CXXDeductionGuideDecl>(Function))
4847 Function->getTemplateSpecializationKindForInstantiation();
4854 !DefinitionRequired)
4859 if (Function->isDefined(ExistingDefn,
4872 const FunctionDecl *PatternDecl = Function->getTemplateInstantiationPattern();
4873 assert(PatternDecl &&
"instantiating a non-template");
4876 Stmt *Pattern =
nullptr;
4878 Pattern = PatternDef->
getBody(PatternDef);
4879 PatternDecl = PatternDef;
4881 PatternDef =
nullptr;
4887 Function->getInstantiatedFromMemberFunction(),
4888 PatternDecl, PatternDef, TSK,
4889 DefinitionRequired)) {
4890 if (DefinitionRequired)
4891 Function->setInvalidDecl();
4893 (Function->isConstexpr() && !Recursive)) {
4897 Function->setInstantiationIsPending(
true);
4899 std::make_pair(Function, PointOfInstantiation));
4903 Diag(PointOfInstantiation, diag::warn_func_template_missing)
4907 Diag(PointOfInstantiation, diag::note_inst_declaration_hint)
4918 Function->setInstantiationIsPending(
true);
4920 std::make_pair(Function, PointOfInstantiation));
4924 llvm::TimeTraceScope TimeScope(
"InstantiateFunction", [&]() {
4926 llvm::raw_string_ostream
OS(Name);
4951 "missing LateParsedTemplate");
4953 Pattern = PatternDecl->
getBody(PatternDecl);
4960 "unexpected kind of function template definition");
4975 for (
auto *D = Function->getMostRecentDecl(); ;
4977 D->setImplicitlyInline();
4987 "instantiating function definition");
4991 Function->setVisibleDespiteOwningModule();
4994 Function->setLocation(PatternDecl->
getLocation());
4996 Function->setRangeEnd(PatternDecl->
getEndLoc());