31#include "llvm/Support/TimeProfiler.h"
42 return cast<CXXRecordDecl>(DC)->isLocalClass();
47template<
typename DeclT>
50 if (!OldDecl->getQualifierLoc())
53 assert((NewDecl->getFriendObjectKind() ||
54 !OldDecl->getLexicalDeclContext()->isDependentContext()) &&
55 "non-friend with qualified name defined in dependent context");
58 const_cast<DeclContext *
>(NewDecl->getFriendObjectKind()
59 ? NewDecl->getLexicalDeclContext()
60 : OldDecl->getLexicalDeclContext()));
69 NewDecl->setQualifierInfo(NewQualifierLoc);
75 return ::SubstQualifier(SemaRef, OldDecl, NewDecl, TemplateArgs);
80 return ::SubstQualifier(SemaRef, OldDecl, NewDecl, TemplateArgs);
84#include "clang/Sema/AttrTemplateInstantiate.inc"
88 const AlignedAttr *Aligned,
Decl *New,
bool IsPackExpansion) {
89 if (Aligned->isAlignmentExpr()) {
98 TemplateArgs, Aligned->getLocation(),
107 const AlignedAttr *Aligned,
Decl *New) {
108 if (!Aligned->isPackExpansion()) {
114 if (Aligned->isAlignmentExpr())
120 assert(!Unexpanded.empty() &&
"Pack expansion without parameter packs?");
123 bool Expand =
true, RetainExpansion =
false;
124 std::optional<unsigned> NumExpansions;
128 Unexpanded, TemplateArgs, Expand,
129 RetainExpansion, NumExpansions))
136 for (
unsigned I = 0; I != *NumExpansions; ++I) {
145 const AssumeAlignedAttr *Aligned,
Decl *New) {
150 Expr *E, *OE =
nullptr;
156 if (Aligned->getOffset()) {
168 const AlignValueAttr *Aligned,
Decl *New) {
179 const AllocAlignAttr *Align,
Decl *New) {
182 llvm::APInt(64, Align->getParamIndex().getSourceIndex()),
189 const AnnotateAttr *
Attr,
Decl *New) {
195 bool HasDelayedArgs =
Attr->delayedArgs_size();
204 false, TemplateArgs, Args))
207 StringRef Str =
Attr->getAnnotation();
208 if (HasDelayedArgs) {
209 if (Args.size() < 1) {
219 ActualArgs.insert(ActualArgs.begin(), Args.begin() + 1, Args.end());
220 std::swap(Args, ActualArgs);
228 Expr *Cond =
nullptr;
242 Cond = Converted.
get();
248 S.
Diag(A->
getLocation(), diag::err_attr_cond_never_constant_expr) << A;
249 for (
const auto &
P : Diags)
250 S.
Diag(
P.first,
P.second);
260 S, TemplateArgs, EIA, EIA->getCond(), Tmpl, New);
264 Cond, EIA->getMessage()));
271 S, TemplateArgs, DIA, DIA->getCond(), Tmpl, New);
276 DIA->getDiagnosticType(), DIA->getArgDependent(), New));
283 const CUDALaunchBoundsAttr &
Attr,
Decl *New) {
293 Expr *MinBlocks =
nullptr;
294 if (
Attr.getMinBlocks()) {
315 const OMPDeclareSimdDeclAttr &
Attr,
Decl *New) {
317 if (
auto *FTD = dyn_cast<FunctionTemplateDecl>(New))
318 New = FTD->getTemplatedDecl();
319 auto *FD = cast<FunctionDecl>(New);
320 auto *ThisContext = dyn_cast_or_null<CXXRecordDecl>(FD->getDeclContext());
325 if (
auto *DRE = dyn_cast<DeclRefExpr>(E->IgnoreParenImpCasts()))
326 if (
auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
329 if (FD->getNumParams() > PVD->getFunctionScopeIndex())
330 Local.InstantiatedLocal(
331 PVD, FD->getParamDecl(PVD->getFunctionScopeIndex()));
335 FD->isCXXInstanceMember());
351 if (
auto *E =
Attr.getSimdlen())
354 if (
Attr.uniforms_size() > 0) {
355 for(
auto *E :
Attr.uniforms()) {
359 Uniforms.push_back(Inst.
get());
363 auto AI =
Attr.alignments_begin();
364 for (
auto *E :
Attr.aligneds()) {
368 Aligneds.push_back(Inst.
get());
372 Alignments.push_back(Inst.
get());
376 auto SI =
Attr.steps_begin();
377 for (
auto *E :
Attr.linears()) {
381 Linears.push_back(Inst.
get());
385 Steps.push_back(Inst.
get());
388 LinModifiers.append(
Attr.modifiers_begin(),
Attr.modifiers_end());
391 Uniforms, Aligneds, Alignments, Linears, LinModifiers, Steps,
398 const OMPDeclareVariantAttr &
Attr,
Decl *New) {
400 if (
auto *FTD = dyn_cast<FunctionTemplateDecl>(New))
401 New = FTD->getTemplatedDecl();
402 auto *FD = cast<FunctionDecl>(New);
403 auto *ThisContext = dyn_cast_or_null<CXXRecordDecl>(FD->getDeclContext());
405 auto &&SubstExpr = [FD, ThisContext, &S, &TemplateArgs](
Expr *E) {
406 if (
auto *DRE = dyn_cast<DeclRefExpr>(E->IgnoreParenImpCasts()))
407 if (
auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
410 if (FD->getNumParams() > PVD->getFunctionScopeIndex())
411 Local.InstantiatedLocal(
412 PVD, FD->getParamDecl(PVD->getFunctionScopeIndex()));
416 FD->isCXXInstanceMember());
422 auto &&Subst = [&SubstExpr, &S](
Expr *E) {
432 if (
Expr *E =
Attr.getVariantFuncRef()) {
437 VariantFuncRef = Subst(E);
443 TI = *
Attr.getTraitInfos();
446 auto SubstScoreOrConditionExpr = [&S, Subst](
Expr *&E,
bool) {
461 Expr *E = VariantFuncRef.
get();
465 std::optional<std::pair<FunctionDecl *, Expr *>> DeclVarData =
467 Attr.appendArgs_size(),
473 E = DeclVarData->second;
474 FD = DeclVarData->first;
476 if (
auto *VariantDRE = dyn_cast<DeclRefExpr>(E->IgnoreParenImpCasts())) {
477 if (
auto *VariantFD = dyn_cast<FunctionDecl>(VariantDRE->getDecl())) {
478 if (
auto *VariantFTD = VariantFD->getDescribedFunctionTemplate()) {
479 if (!VariantFTD->isThisDeclarationADefinition())
490 SubstFD->getType(), FD->getType(),
498 SubstFD->setInstantiationIsPending(!SubstFD->isDefined());
502 SubstFD->getLocation(),
512 for (
Expr *E :
Attr.adjustArgsNothing()) {
516 NothingExprs.push_back(ER.
get());
518 for (
Expr *E :
Attr.adjustArgsNeedDevicePtr()) {
522 NeedDevicePtrExprs.push_back(ER.
get());
526 AppendArgs.emplace_back(II.IsTarget, II.IsTargetSync);
530 FD, E, TI, NothingExprs, NeedDevicePtrExprs, AppendArgs,
SourceLocation(),
536 const AMDGPUFlatWorkGroupSizeAttr &
Attr,
Decl *New) {
561 Expr *Cond =
nullptr;
569 Cond = SubstResult.
get();
579 const AMDGPUWavesPerEUAttr &
Attr,
Decl *New) {
589 Expr *MaxExpr =
nullptr;
590 if (
auto Max =
Attr.getMax()) {
605 const SYCLKernelAttr &
Attr,
Decl *New) {
615 if (
const auto *PNA = dyn_cast<PreferredNameAttr>(A)) {
617 const auto *RD = cast<CXXRecordDecl>(D);
621 for (
const auto *ExistingPNA : D->
specific_attrs<PreferredNameAttr>())
623 PNA->getTypedefType()))
628 if (
const auto *BA = dyn_cast<BuiltinAttr>(A)) {
630 switch (BA->getID()) {
631 case Builtin::BIforward:
641 case Builtin::BImove:
642 case Builtin::BImove_if_noexcept:
660 if (
NamedDecl *ND = dyn_cast<NamedDecl>(New)) {
664 for (
const auto *TmplAttr : Tmpl->
attrs()) {
671 *
this, dyn_cast_or_null<CXXRecordDecl>(ND->getDeclContext()),
675 TmplAttr,
Context, *
this, TemplateArgs);
685 case clang::attr::CFConsumed:
687 case clang::attr::OSConsumed:
689 case clang::attr::NSConsumed:
692 llvm_unreachable(
"Wrong argument supplied");
700 for (
const auto *TmplAttr : Tmpl->
attrs()) {
705 const AlignedAttr *Aligned = dyn_cast<AlignedAttr>(TmplAttr);
706 if (Aligned && Aligned->isAlignmentDependent()) {
711 if (
const auto *AssumeAligned = dyn_cast<AssumeAlignedAttr>(TmplAttr)) {
716 if (
const auto *AlignValue = dyn_cast<AlignValueAttr>(TmplAttr)) {
721 if (
const auto *AllocAlign = dyn_cast<AllocAlignAttr>(TmplAttr)) {
726 if (
const auto *Annotate = dyn_cast<AnnotateAttr>(TmplAttr)) {
731 if (
const auto *EnableIf = dyn_cast<EnableIfAttr>(TmplAttr)) {
733 cast<FunctionDecl>(New));
737 if (
const auto *DiagnoseIf = dyn_cast<DiagnoseIfAttr>(TmplAttr)) {
739 cast<FunctionDecl>(New));
743 if (
const auto *CUDALaunchBounds =
744 dyn_cast<CUDALaunchBoundsAttr>(TmplAttr)) {
746 *CUDALaunchBounds, New);
750 if (
const auto *Mode = dyn_cast<ModeAttr>(TmplAttr)) {
755 if (
const auto *OMPAttr = dyn_cast<OMPDeclareSimdDeclAttr>(TmplAttr)) {
760 if (
const auto *OMPAttr = dyn_cast<OMPDeclareVariantAttr>(TmplAttr)) {
765 if (
const auto *AMDGPUFlatWorkGroupSize =
766 dyn_cast<AMDGPUFlatWorkGroupSizeAttr>(TmplAttr)) {
768 *
this, TemplateArgs, *AMDGPUFlatWorkGroupSize, New);
771 if (
const auto *AMDGPUFlatWorkGroupSize =
772 dyn_cast<AMDGPUWavesPerEUAttr>(TmplAttr)) {
774 *AMDGPUFlatWorkGroupSize, New);
778 if (TmplAttr->getKind() == attr::DLLExport ||
779 TmplAttr->getKind() == attr::DLLImport) {
780 if (New->
hasAttr<DLLExportAttr>() || New->
hasAttr<DLLImportAttr>()) {
785 if (
const auto *ABIAttr = dyn_cast<ParameterABIAttr>(TmplAttr)) {
790 if (isa<NSConsumedAttr>(TmplAttr) || isa<OSConsumedAttr>(TmplAttr) ||
791 isa<CFConsumedAttr>(TmplAttr)) {
797 if (
auto *A = dyn_cast<PointerAttr>(TmplAttr)) {
798 if (!New->
hasAttr<PointerAttr>())
803 if (
auto *A = dyn_cast<OwnerAttr>(TmplAttr)) {
804 if (!New->
hasAttr<OwnerAttr>())
809 if (
auto *A = dyn_cast<SYCLKernelAttr>(TmplAttr)) {
814 assert(!TmplAttr->isPackExpansion());
815 if (TmplAttr->isLateParsed() && LateAttrs) {
824 auto *ND = cast<NamedDecl>(New);
825 auto *ThisContext = dyn_cast_or_null<CXXRecordDecl>(ND->getDeclContext());
827 ND->isCXXInstanceMember());
830 *
this, TemplateArgs);
844 for (
const auto *
Attr : Pattern->
attrs()) {
845 if (
auto *A = dyn_cast<StrictFPAttr>(
Attr)) {
846 if (!Inst->
hasAttr<StrictFPAttr>())
863 DLLExportAttr *
Attr = Ctor->
getAttr<DLLExportAttr>();
866 for (
unsigned I = 0; I != NumParams; ++I) {
877template<
typename DeclT>
879 DeclT *
Result = D->getPreviousDecl();
884 if (
Result && isa<CXXRecordDecl>(D->getDeclContext()) &&
885 D->getLexicalDeclContext() !=
Result->getLexicalDeclContext())
893 llvm_unreachable(
"Translation units cannot be instantiated");
897 llvm_unreachable(
"HLSL buffer declarations cannot be instantiated");
902 llvm_unreachable(
"pragma comment cannot be instantiated");
905Decl *TemplateDeclInstantiator::VisitPragmaDetectMismatchDecl(
907 llvm_unreachable(
"pragma comment cannot be instantiated");
912 llvm_unreachable(
"extern \"C\" context cannot be instantiated");
916 llvm_unreachable(
"GUID declaration cannot be instantiated");
919Decl *TemplateDeclInstantiator::VisitUnnamedGlobalConstantDecl(
921 llvm_unreachable(
"UnnamedGlobalConstantDecl cannot be instantiated");
924Decl *TemplateDeclInstantiator::VisitTemplateParamObjectDecl(
926 llvm_unreachable(
"template parameter objects cannot be instantiated");
930TemplateDeclInstantiator::VisitLabelDecl(
LabelDecl *D) {
938TemplateDeclInstantiator::VisitNamespaceDecl(
NamespaceDecl *D) {
939 llvm_unreachable(
"Namespaces cannot be instantiated");
1003 TagDecl *oldTag = oldTagType->getDecl();
1064 if (getPreviousDeclForInstantiation<TypedefNameDecl>(Pattern)) {
1066 if (!Found.
empty()) {
1067 PrevAliasTemplate = dyn_cast<TypeAliasTemplateDecl>(Found.
front());
1080 if (PrevAliasTemplate)
1085 if (!PrevAliasTemplate)
1105 NewBindings.push_back(cast<BindingDecl>(VisitBindingDecl(OldBD)));
1108 auto *NewDD = cast_or_null<DecompositionDecl>(
1111 if (!NewDD || NewDD->isInvalidDecl())
1112 for (
auto *NewBD : NewBindings)
1113 NewBD->setInvalidDecl();
1123 bool InstantiatingVarTemplate,
1134 SemaRef.
Diag(D->
getLocation(), diag::err_variable_instantiates_to_function)
1167 StartingScope, InstantiatingVarTemplate);
1170 if (
auto *F = dyn_cast<FunctionDecl>(DC))
1171 RT = F->getReturnType();
1172 else if (isa<BlockDecl>(DC))
1176 llvm_unreachable(
"Unknown context type");
1213Decl *TemplateDeclInstantiator::VisitFieldDecl(
FieldDecl *D) {
1218 DI = SemaRef.
SubstType(DI, TemplateArgs,
1230 SemaRef.
Diag(D->
getLocation(), diag::err_field_instantiates_to_function)
1241 else if (BitWidth) {
1247 = SemaRef.
SubstExpr(BitWidth, TemplateArgs);
1252 BitWidth = InstantiatedBitWidth.
getAs<
Expr>();
1257 cast<RecordDecl>(Owner),
1270 SemaRef.
InstantiateAttrs(TemplateArgs, D, Field, LateAttrs, StartingScope);
1272 if (
Field->hasAttrs())
1276 Field->setInvalidDecl();
1278 if (!
Field->getDeclName()) {
1283 if (
Parent->isAnonymousStructOrUnion() &&
1284 Parent->getRedeclContext()->isFunctionOrMethod())
1300 SemaRef.
Diag(D->
getLocation(), diag::err_property_is_variably_modified)
1304 DI = SemaRef.
SubstType(DI, TemplateArgs,
1316 SemaRef.
Diag(D->
getLocation(), diag::err_field_instantiates_to_function)
1345 for (
auto *PI : D->
chain()) {
1351 NamedChain[i++] = Next;
1354 QualType T = cast<FieldDecl>(NamedChain[i-1])->getType();
1357 {NamedChain, D->getChainingSize()});
1364 Owner->
addDecl(IndirectField);
1365 return IndirectField;
1380 InstTy = SemaRef.
SubstType(Ty, TemplateArgs,
1398 assert(ND &&
"friend decl must be a decl or a type!");
1405 if (!NewND)
return nullptr;
1424 = SemaRef.
SubstExpr(AssertExpr, TemplateArgs);
1429 InstantiatedAssertExpr.
get(),
1435Decl *TemplateDeclInstantiator::VisitEnumDecl(
EnumDecl *D) {
1441 if (!Prev)
return nullptr;
1442 PrevDecl = cast<EnumDecl>(Prev);
1460 Enum->setIntegerTypeSourceInfo(NewTI);
1463 &&
"Dependent type without type source info");
1486 if (Def && Def != D) {
1493 SemaRef.
SubstType(TI->getType(), TemplateArgs,
1496 DefnUnderlying,
true,
Enum);
1517 Enum->startDefinition();
1528 if (
Expr *UninstValue = EC->getInitExpr()) {
1538 if (
Value.isInvalid()) {
1551 Enum->setInvalidDecl();
1558 Enum->addDecl(EnumConst);
1559 Enumerators.push_back(EnumConst);
1560 LastEnumConst = EnumConst;
1563 !
Enum->isScoped()) {
1576 llvm_unreachable(
"EnumConstantDecls can only occur within EnumDecls.");
1581 llvm_unreachable(
"BuiltinTemplateDecls cannot be instantiated.");
1613 if (!Found.
empty()) {
1614 PrevClassTemplate = dyn_cast<ClassTemplateDecl>(Found.
front());
1615 if (PrevClassTemplate)
1628 SS.
Adopt(QualifierLoc);
1630 if (!DC)
return nullptr;
1644 if (R.isSingleResult()) {
1646 if (PrevClassTemplate)
1650 if (!PrevClassTemplate && QualifierLoc) {
1657 if (PrevClassTemplate) {
1698 if (PrevClassTemplate)
1708 if (!PrevClassTemplate)
1731 if (!PrevClassTemplate) {
1737 for (
unsigned I = 0, N = PartialSpecs.size(); I != N; ++I)
1738 if (PartialSpecs[I]->getFirstDecl()->isOutOfLine())
1739 OutOfLinePartialSpecs.push_back(std::make_pair(Inst, PartialSpecs[I]));
1746TemplateDeclInstantiator::VisitClassTemplatePartialSpecializationDecl(
1758 = dyn_cast<ClassTemplateDecl>(Found.
front());
1759 if (!InstClassTemplate)
1771 "Only static data member templates are allowed.");
1787 PrevVarTemplate = dyn_cast<VarTemplateDecl>(Found.
front());
1793 if (!VarInst)
return nullptr;
1804 if (!PrevVarTemplate)
1814 if (!PrevVarTemplate) {
1820 for (
unsigned I = 0, N = PartialSpecs.size(); I != N; ++I)
1821 if (PartialSpecs[I]->getFirstDecl()->isOutOfLine())
1822 OutOfLineVarPartialSpecs.push_back(
1823 std::make_pair(Inst, PartialSpecs[I]));
1829Decl *TemplateDeclInstantiator::VisitVarTemplatePartialSpecializationDecl(
1832 "Only static data member templates are allowed.");
1838 assert(!Found.
empty() &&
"Instantiation found nothing?");
1841 assert(InstVarTemplate &&
"Instantiation did not find a variable template?");
1881 assert(InstTemplate &&
1882 "VisitFunctionDecl/CXXMethodDecl didn't create a template!");
1900 return InstTemplate;
1909 if (!Prev)
return nullptr;
1910 PrevDecl = cast<CXXRecordDecl>(Prev);
1924 IsInjectedClassName);
1926 if (IsInjectedClassName)
1942 if (!IsInjectedClassName)
1948 Record->setObjectOfFriendDecl();
1952 Record->setAnonymousStructOrUnion(
true);
1990 LocalInstantiations.perform();
1995 if (IsInjectedClassName)
1996 assert(
Record->isInjectedClassName() &&
"Broken injected-class-name");
2014 if (OrigFunc->
getExtInfo() == NewFunc->getExtInfo())
2020 NewFunc->getParamTypes(), NewEPI);
2034 if (FunctionTemplate && !TemplateParams) {
2037 void *InsertPos =
nullptr;
2047 if (FunctionTemplate)
2052 bool MergeWithParentScope = (TemplateParams !=
nullptr) ||
2054 !(isa<Decl>(Owner) &&
2055 cast<Decl>(Owner)->isDefinedOutsideFunctionOrMethod());
2059 if (
auto *DGuide = dyn_cast<CXXDeductionGuideDecl>(D)) {
2061 SemaRef, TemplateArgs, DGuide->getExplicitSpecifier(), DGuide);
2062 if (InstantiatedExplicitSpecifier.
isInvalid())
2072 if (TemplateParams && TemplateParams->
size()) {
2074 dyn_cast<TemplateTypeParmDecl>(TemplateParams->
asArray().back());
2075 if (LastParam && LastParam->isImplicit() &&
2076 LastParam->hasTypeConstraint()) {
2106 }
else if (isFriend && QualifierLoc) {
2108 SS.
Adopt(QualifierLoc);
2110 if (!DC)
return nullptr;
2123 if (
auto *DGuide = dyn_cast<CXXDeductionGuideDecl>(D)) {
2126 InstantiatedExplicitSpecifier, NameInfo, T, TInfo,
2128 if (DGuide->isCopyDeductionCandidate())
2129 cast<CXXDeductionGuideDecl>(Function)->setIsCopyDeductionCandidate();
2136 TrailingRequiresClause);
2137 Function->setFriendConstraintRefersToEnclosingTemplate(
2143 Function->setImplicitlyInline();
2146 Function->setQualifierInfo(QualifierLoc);
2149 Function->setLocalExternDecl();
2160 Function->setLexicalDeclContext(LexicalDC);
2163 for (
unsigned P = 0;
P < Params.size(); ++
P)
2165 Params[
P]->setOwningFunction(Function);
2166 Function->setParams(Params);
2168 if (TrailingRequiresClause)
2169 Function->setTrailingRequiresClause(TrailingRequiresClause);
2171 if (TemplateParams) {
2187 Function->getLocation(),
2188 Function->getDeclName(),
2189 TemplateParams, Function);
2190 Function->setDescribedFunctionTemplate(FunctionTemplate);
2198 }
else if (FunctionTemplate) {
2201 Function->setFunctionTemplateSpecialization(FunctionTemplate,
2210 }
else if (!isFriend) {
2214 Function->setInstantiatedFromDecl(D);
2218 Function->setObjectOfFriendDecl();
2220 FT->setObjectOfFriendDecl();
2224 Function->setInvalidDecl();
2226 bool IsExplicitSpecialization =
false;
2237 assert(isFriend &&
"non-friend has dependent specialization info?");
2241 Info->getRAngleLoc());
2247 for (
unsigned I = 0, E = Info->getNumTemplates(); I != E; ++I) {
2249 Info->getTemplate(I),
2251 if (!Temp)
return nullptr;
2253 Previous.addDecl(cast<FunctionTemplateDecl>(Temp));
2259 Function->setInvalidDecl();
2261 IsExplicitSpecialization =
true;
2269 Info->getRAngleLoc());
2277 Function->setInvalidDecl();
2279 IsExplicitSpecialization =
true;
2280 }
else if (TemplateParams || !FunctionTemplate) {
2296 if (isFriend && !QualifierLoc && !FunctionTemplate) {
2316 if (Function->isLocalExternDecl()) {
2318 if (!PVD->hasDefaultArg())
2324 Expr *UninstExpr = PVD->getUninstantiatedDefaultArg();
2327 { UninstExpr }, UninstExpr->
getType());
2329 PVD->setDefaultArg(ErrorResult.
get());
2335 IsExplicitSpecialization,
2336 Function->isThisDeclarationADefinition());
2342 if (isFriend && TemplateParams && FunctionTemplate->
getPreviousDecl()) {
2346 Function->isThisDeclarationADefinition()
2357 Function->getMemberSpecializationInfo()) {
2358 if (MSInfo->getPointOfInstantiation().isInvalid()) {
2360 MSInfo->setPointOfInstantiation(Loc);
2362 std::make_pair(Function, Loc));
2375 (TemplateParams ? cast<NamedDecl>(FunctionTemplate) : Function);
2380 (Function->isLocalExternDecl() && !Function->getPreviousDecl()))
2383 if (Function->isOverloadedOperator() && !DC->
isRecord() &&
2392 std::optional<const ASTTemplateArgumentListInfo *>
2393 ClassScopeSpecializationArgs,
2396 if (FunctionTemplate && !TemplateParams) {
2402 void *InsertPos =
nullptr;
2412 if (FunctionTemplate)
2417 bool MergeWithParentScope = (TemplateParams !=
nullptr) ||
2418 !(isa<Decl>(Owner) &&
2419 cast<Decl>(Owner)->isDefinedOutsideFunctionOrMethod());
2424 unsigned NumTempParamLists = 0;
2426 TempParamLists.resize(NumTempParamLists);
2427 for (
unsigned I = 0; I != NumTempParamLists; ++I) {
2432 TempParamLists[I] = InstParams;
2439 if (InstantiatedExplicitSpecifier.
isInvalid())
2448 if (cast<CXXRecordDecl>(D->
getParent())->isLambda() &&
2450 isa<CXXConstructorDecl, CXXDestructorDecl>(D)) {
2462 if (TemplateParams && TemplateParams->
size()) {
2464 dyn_cast<TemplateTypeParmDecl>(TemplateParams->
asArray().back());
2465 if (LastParam && LastParam->isImplicit() &&
2466 LastParam->hasTypeConstraint()) {
2491 SS.
Adopt(QualifierLoc);
2501 if (!DC)
return nullptr;
2519 SemaRef.
Context, Record, StartLoc, NameInfo, T, TInfo,
2520 InstantiatedExplicitSpecifier, Constructor->UsesFPIntrin(),
2521 Constructor->isInlineSpecified(),
false,
2523 TrailingRequiresClause);
2525 if (Constructor->isDefaultConstructor() ||
2526 Constructor->isCopyOrMoveConstructor())
2530 SemaRef.
Context, Record, StartLoc, NameInfo, T, TInfo,
2531 Destructor->UsesFPIntrin(), Destructor->isInlineSpecified(),
false,
2532 Destructor->getConstexprKind(), TrailingRequiresClause);
2540 SemaRef.
Context, Record, StartLoc, NameInfo, T, TInfo,
2541 Conversion->UsesFPIntrin(), Conversion->isInlineSpecified(),
2542 InstantiatedExplicitSpecifier, Conversion->getConstexprKind(),
2543 Conversion->getEndLoc(), TrailingRequiresClause);
2547 SemaRef.
Context, Record, StartLoc, NameInfo, T, TInfo, SC,
2549 D->
getEndLoc(), TrailingRequiresClause);
2560 if (TemplateParams) {
2577 TemplateParams, Method);
2584 }
else if (FunctionTemplate) {
2591 }
else if (!isFriend) {
2600 if (NumTempParamLists)
2611 for (
unsigned P = 0;
P < Params.size(); ++
P)
2612 Params[
P]->setOwningFunction(Method);
2621 bool IsExplicitSpecialization =
false;
2627 assert(isFriend &&
"non-friend has dependent specialization info?");
2631 Info->getRAngleLoc());
2637 for (
unsigned I = 0, E = Info->getNumTemplates(); I != E; ++I) {
2639 Info->getTemplate(I),
2641 if (!Temp)
return nullptr;
2643 Previous.addDecl(cast<FunctionTemplateDecl>(Temp));
2651 IsExplicitSpecialization =
true;
2653 ClassScopeSpecializationArgs.value_or(
2658 Info->getRAngleLoc());
2668 IsExplicitSpecialization =
true;
2669 }
else if (ClassScopeSpecializationArgs) {
2676 IsExplicitSpecialization =
true;
2677 }
else if (!FunctionTemplate || TemplateParams || isFriend) {
2704 for (
unsigned P = 0;
P < Params.size(); ++
P) {
2705 if (!Params[
P]->hasDefaultArg())
2711 Expr *UninstExpr = Params[
P]->getUninstantiatedDefaultArg();
2714 { UninstExpr }, UninstExpr->
getType());
2716 Params[
P]->setDefaultArg(ErrorResult.
get());
2722 IsExplicitSpecialization,
2735 if (FunctionTemplate)
2751 if (IsExplicitSpecialization && !isFriend)
2755 if (FunctionTemplate) {
2763 }
else if (isFriend) {
2769 Record->makeDeclVisibleInContext(Method);
2780 if (Method->
hasAttr<UsedAttr>()) {
2781 if (
const auto *A = dyn_cast<CXXRecordDecl>(Owner)) {
2784 A->getMemberSpecializationInfo())
2785 Loc = MSInfo->getPointOfInstantiation();
2786 else if (
const auto *Spec = dyn_cast<ClassTemplateSpecializationDecl>(A))
2787 Loc = Spec->getPointOfInstantiation();
2813Decl *TemplateDeclInstantiator::VisitTemplateTypeParmDecl(
2817 std::optional<unsigned> NumExpanded;
2821 assert(TC->getTemplateArgsAsWritten() &&
2822 "type parameter can only be an expansion when explicit arguments "
2828 for (
auto &ArgLoc : TC->getTemplateArgsAsWritten()->arguments())
2834 bool RetainExpansion =
false;
2836 cast<CXXFoldExpr>(TC->getImmediatelyDeclaredConstraint())
2839 TC->hasExplicitTemplateArgs() ?
2840 TC->getTemplateArgsAsWritten()->getRAngleLoc() :
2841 TC->getConceptNameInfo().getEndLoc()),
2842 Unexpanded, TemplateArgs, Expand, RetainExpansion, NumExpanded))
2861 EvaluateConstraints))
2869 if (InstantiatedDefaultArg)
2880Decl *TemplateDeclInstantiator::VisitNonTypeTemplateParmDecl(
2886 bool IsExpandedParameterPack =
false;
2908 ExpandedParameterPackTypesAsWritten.push_back(NewDI);
2909 ExpandedParameterPackTypes.push_back(NewT);
2912 IsExpandedParameterPack =
true;
2927 bool RetainExpansion =
false;
2928 std::optional<unsigned> OrigNumExpansions =
2930 std::optional<unsigned> NumExpansions = OrigNumExpansions;
2932 Pattern.getSourceRange(),
2935 Expand, RetainExpansion,
2940 for (
unsigned I = 0; I != *NumExpansions; ++I) {
2953 ExpandedParameterPackTypesAsWritten.push_back(NewDI);
2954 ExpandedParameterPackTypes.push_back(NewT);
2960 IsExpandedParameterPack =
true;
2997 if (IsExpandedParameterPack)
3002 ExpandedParameterPackTypesAsWritten);
3010 if (AutoLoc.isConstrained())
3015 IsExpandedParameterPack
3030 if (!
Value.isInvalid())
3044 for (
const auto &
P : *Params) {
3045 if (
P->isTemplateParameterPack())
3057TemplateDeclInstantiator::VisitTemplateTemplateParmDecl(
3064 bool IsExpandedParameterPack =
false;
3078 ExpandedParams.push_back(Expansion);
3081 IsExpandedParameterPack =
true;
3082 InstParams = TempParams;
3094 bool RetainExpansion =
false;
3095 std::optional<unsigned> NumExpansions;
3100 Expand, RetainExpansion,
3105 for (
unsigned I = 0; I != *NumExpansions; ++I) {
3111 ExpandedParams.push_back(Expansion);
3117 IsExpandedParameterPack =
true;
3118 InstParams = TempParams;
3140 if (IsExpandedParameterPack)
3201 for (
auto *Shadow : D->
shadows()) {
3205 NamedDecl *OldTarget = Shadow->getTargetDecl();
3206 if (
auto *CUSD = dyn_cast<ConstructorUsingShadowDecl>(Shadow))
3207 if (
auto *BaseShadow = CUSD->getNominatedBaseClassShadowDecl())
3208 OldTarget = BaseShadow;
3212 dyn_cast<UnresolvedUsingIfExistsDecl>(Shadow->getTargetDecl())) {
3214 SemaRef.
Context, Owner, EmptyD->getLocation(), EmptyD->getDeclName());
3217 Shadow->getLocation(), OldTarget, TemplateArgs));
3229 Shadow->getLocation(), OldPrev, TemplateArgs));
3232 nullptr, Inst, InstTarget, PrevDecl);
3235 if (isFunctionScope)
3242Decl *TemplateDeclInstantiator::VisitUsingDecl(
UsingDecl *D) {
3263 if (
auto *RD = dyn_cast<CXXRecordDecl>(SemaRef.
CurContext))
3269 bool CheckRedeclaration = Owner->
isRecord();
3280 SS.
Adopt(QualifierLoc);
3281 if (CheckRedeclaration) {
3282 Prev.setHideTags(
false);
3347Decl *TemplateDeclInstantiator::VisitConstructorUsingShadowDecl(
3353template <
typename T>
3354Decl *TemplateDeclInstantiator::instantiateUnresolvedUsingDecl(
3355 T *D,
bool InstantiatingPackElement) {
3357 if (D->isPackExpansion() && !InstantiatingPackElement) {
3365 bool RetainExpansion =
false;
3366 std::optional<unsigned> NumExpansions;
3368 D->getEllipsisLoc(), D->getSourceRange(), Unexpanded, TemplateArgs,
3369 Expand, RetainExpansion, NumExpansions))
3374 assert(!RetainExpansion &&
3375 "should never need to retain an expansion for UsingPackDecl");
3381 return instantiateUnresolvedUsingDecl(D,
true);
3391 if (D->getDeclContext()->isFunctionOrMethod() && *NumExpansions > 1) {
3392 SemaRef.
Diag(D->getEllipsisLoc(),
3393 diag::err_using_decl_redeclaration_expansion);
3399 for (
unsigned I = 0; I != *NumExpansions; ++I) {
3401 Decl *Slice = instantiateUnresolvedUsingDecl(D,
true);
3408 Expansions.push_back(cast<NamedDecl>(Slice));
3427 SS.
Adopt(QualifierLoc);
3434 bool InstantiatingSlice = D->getEllipsisLoc().isValid() &&
3439 bool IsUsingIfExists = D->template hasAttr<UsingIfExistsAttr>();
3441 nullptr, D->getAccess(), D->getUsingLoc(),
3442 TD, TypenameLoc, SS, NameInfo, EllipsisLoc,
3444 true, IsUsingIfExists);
3453Decl *TemplateDeclInstantiator::VisitUnresolvedUsingTypenameDecl(
3455 return instantiateUnresolvedUsingDecl(D);
3458Decl *TemplateDeclInstantiator::VisitUnresolvedUsingValueDecl(
3460 return instantiateUnresolvedUsingDecl(D);
3463Decl *TemplateDeclInstantiator::VisitUnresolvedUsingIfExistsDecl(
3465 llvm_unreachable(
"referring to unresolved decl out of UsingShadowDecl");
3473 Expansions.push_back(NewUD);
3484Decl *TemplateDeclInstantiator::VisitClassScopeFunctionSpecializationDecl(
3487 return cast_or_null<CXXMethodDecl>(
3491Decl *TemplateDeclInstantiator::VisitOMPThreadPrivateDecl(
3496 assert(isa<DeclRefExpr>(Var) &&
"threadprivate arg is not a DeclRefExpr");
3497 Vars.push_back(Var);
3513 assert(isa<DeclRefExpr>(Var) &&
"allocate arg is not a DeclRefExpr");
3514 Vars.push_back(Var);
3520 if (
auto *AC = dyn_cast<OMPAllocatorClause>(
C)) {
3525 NewE.
get(), AC->
getBeginLoc(), AC->getLParenLoc(), AC->getEndLoc());
3526 }
else if (
auto *AC = dyn_cast<OMPAlignClause>(
C)) {
3531 AC->getLParenLoc(), AC->getEndLoc());
3536 Clauses.push_back(IC);
3541 if (Res.
get().isNull())
3543 return Res.
get().getSingleDecl();
3548 "Requires directive cannot be instantiated within a dependent context");
3551Decl *TemplateDeclInstantiator::VisitOMPDeclareReductionDecl(
3554 const bool RequiresInstantiation =
3559 if (RequiresInstantiation) {
3565 SubstReductionType = D->
getType();
3567 if (SubstReductionType.
isNull())
3571 bool IsCorrect =
true;
3573 std::pair<QualType, SourceLocation> ReductionTypes[] = {
3574 std::make_pair(SubstReductionType, D->
getLocation())};
3576 if (PrevDeclInScope && !PrevDeclInScope->isInvalidDecl()) {
3577 PrevDeclInScope = cast<OMPDeclareReductionDecl>(
3584 auto *NewDRD = cast<OMPDeclareReductionDecl>(DRD.get().getSingleDecl());
3586 Expr *SubstCombiner =
nullptr;
3587 Expr *SubstInitializer =
nullptr;
3594 cast<DeclRefExpr>(NewDRD->getCombinerIn())->getDecl());
3597 cast<DeclRefExpr>(NewDRD->getCombinerOut())->getDecl());
3598 auto *ThisContext = dyn_cast_or_null<CXXRecordDecl>(Owner);
3601 SubstCombiner = SemaRef.
SubstExpr(Combiner, TemplateArgs).
get();
3610 cast<DeclRefExpr>(NewDRD->getInitOrig())->getDecl());
3613 cast<DeclRefExpr>(NewDRD->getInitPriv())->getDecl());
3615 SubstInitializer = SemaRef.
SubstExpr(Init, TemplateArgs).
get();
3618 cast<VarDecl>(cast<DeclRefExpr>(D->
getInitPriv())->getDecl());
3619 IsCorrect = IsCorrect && OldPrivParm->hasInit();
3627 IsCorrect = IsCorrect && SubstCombiner &&
3630 SubstInitializer) ||
3632 !SubstInitializer));
3643 const bool RequiresInstantiation =
3649 if (RequiresInstantiation) {
3657 if (SubstMapperTy.
isNull())
3661 if (PrevDeclInScope && !PrevDeclInScope->isInvalidDecl()) {
3662 PrevDeclInScope = cast<OMPDeclareMapperDecl>(
3666 bool IsCorrect =
true;
3677 cast<DeclRefExpr>(MapperVarRef.
get())->getDecl());
3678 auto *ThisContext = dyn_cast_or_null<CXXRecordDecl>(Owner);
3683 auto *OldC = cast<OMPMapClause>(
C);
3685 for (
Expr *OE : OldC->varlists()) {
3691 NewVars.push_back(NE);
3699 SS.
Adopt(NewQualifierLoc);
3705 OldC->getIteratorModifier(), OldC->getMapTypeModifiers(),
3706 OldC->getMapTypeModifiersLoc(), SS, NewNameInfo, OldC->getMapType(),
3707 OldC->isImplicitMapType(), OldC->getMapLoc(), OldC->getColonLoc(),
3709 Clauses.push_back(NewC);
3716 VN, D->
getAccess(), MapperVarRef.
get(), Clauses, PrevDeclInScope);
3717 Decl *NewDMD = DG.
get().getSingleDecl();
3722Decl *TemplateDeclInstantiator::VisitOMPCapturedExprDecl(
3724 llvm_unreachable(
"Should not be met in templates");
3744 llvm_unreachable(
"There are only CXXRecordDecls in C++");
3748TemplateDeclInstantiator::VisitClassTemplateSpecializationDecl(
3755 "can only instantiate an explicit specialization "
3756 "for a member class template");
3763 if (!InstClassTemplate)
3769 castAs<TemplateSpecializationTypeLoc>();
3773 for (
unsigned I = 0; I != Loc.
getNumArgs(); ++I)
3782 InstTemplateArgs,
false,
3783 SugaredConverted, CanonicalConverted,
3789 void *InsertPos =
nullptr;
3821 diag::note_previous_definition);
3829 D->
getLocation(), InstClassTemplate, CanonicalConverted, PrevDecl);
3884 assert(VarTemplate &&
3885 "A template specialization without specialized template?");
3890 if (!InstVarTemplate)
3896 VarTemplateArgsInfo.
setLAngleLoc(TemplateArgsInfo->getLAngleLoc());
3897 VarTemplateArgsInfo.
setRAngleLoc(TemplateArgsInfo->getRAngleLoc());
3900 TemplateArgs, VarTemplateArgsInfo))
3907 VarTemplateArgsInfo,
false,
3908 SugaredConverted, CanonicalConverted,
3913 void *InsertPos =
nullptr;
3927 InstVarTemplate, D, VarTemplateArgsInfo, CanonicalConverted, PrevDecl);
3944 SemaRef.
Diag(D->
getLocation(), diag::err_variable_instantiates_to_function)
3955 void *InsertPos =
nullptr;
3968 StartingScope,
false, PrevDecl);
3974 llvm_unreachable(
"@defs is not supported in Objective-C++");
3981 "cannot instantiate %0 yet");
3989 llvm_unreachable(
"Concept definitions cannot reside inside a template");
3992Decl *TemplateDeclInstantiator::VisitImplicitConceptSpecializationDecl(
3994 llvm_unreachable(
"Concept specializations cannot reside inside a template");
4004 llvm_unreachable(
"Unexpected decl");
4015 SubstD = Instantiator.Visit(D);
4030 SemaRef.
Context.
BoolTy, FPT->getParamTypes(), FPT->getExtProtoInfo());
4041 assert(OldLoc &&
"type of function is not a function type?");
4043 for (
unsigned I = 0, N = OldLoc.getNumParams(); I != N; ++I)
4044 NewLoc.
setParam(I, OldLoc.getParam(I));
4054 if (Spaceship->isInvalidDecl())
4066 if (
auto *MD = dyn_cast<CXXMethodDecl>(Spaceship)) {
4068 MD,
nullptr, std::nullopt,
4071 assert(Spaceship->getFriendObjectKind() &&
4072 "defaulted spaceship is neither a member nor a friend");
4082 cast<NamedDecl>(R), Spaceship->getBeginLoc());
4086 return cast_or_null<FunctionDecl>(R);
4100 unsigned N = L->
size();
4104 for (
auto &
P : *L) {
4106 Params.push_back(D);
4126 bool EvaluateConstraints) {
4173 ClassTemplate, PartialSpec->
getLocation(), InstTemplateArgs,
4174 false, SugaredConverted, CanonicalConverted))
4180 CanonicalConverted))
4185 void *InsertPos =
nullptr;
4225 SemaRef.
Diag(PartialSpec->
getLocation(), diag::err_partial_spec_redeclared)
4227 SemaRef.
Diag(PrevDecl->
getLocation(), diag::note_prev_partial_spec_here)
4238 ClassTemplate, CanonicalConverted, InstTemplateArgs, CanonType,
4254 return InstPartialSpec;
4298 VarTemplate, PartialSpec->
getLocation(), InstTemplateArgs,
4299 false, SugaredConverted, CanonicalConverted))
4305 CanonicalConverted))
4310 void *InsertPos =
nullptr;
4348 diag::err_var_partial_spec_redeclared)
4351 diag::note_var_prev_partial_spec_here);
4364 diag::err_variable_instantiates_to_function)
4392 LateAttrs, Owner, StartingScope);
4394 return InstPartialSpec;
4401 assert(OldTInfo &&
"substituting function without type source info");
4402 assert(Params.empty() &&
"parameter vector is non-empty at start");
4407 ThisContext = cast<CXXRecordDecl>(Owner);
4408 ThisTypeQuals = Method->getMethodQualifiers();
4413 ThisContext, ThisTypeQuals, EvaluateConstraints);
4419 if (NewTInfo != OldTInfo) {
4423 unsigned NewIdx = 0;
4424 for (
unsigned OldIdx = 0, NumOldParams = OldProtoLoc.getNumParams();
4425 OldIdx != NumOldParams; ++OldIdx) {
4426 ParmVarDecl *OldParam = OldProtoLoc.getParam(OldIdx);
4432 std::optional<unsigned> NumArgumentsInExpansion;
4434 NumArgumentsInExpansion =
4437 if (!NumArgumentsInExpansion) {
4441 Params.push_back(NewParam);
4442 Scope->InstantiatedLocal(OldParam, NewParam);
4445 Scope->MakeInstantiatedLocalArgPack(OldParam);
4446 for (
unsigned I = 0; I != *NumArgumentsInExpansion; ++I) {
4448 Params.push_back(NewParam);
4449 Scope->InstantiatedLocalPackArg(OldParam, NewParam);
4458 cast<FunctionProtoType>(OldProtoLoc.getType());
4459 for (
unsigned i = 0, i_end = OldProtoLoc.getNumParams(); i != i_end;
4469 cast_or_null<ParmVarDecl>(VisitParmVarDecl(OldParam));
4472 Params.push_back(Parm);
4489 TemplateArgs, ParamTypes, &Params,
4500bool Sema::addInstantiatedParametersToScope(
4504 unsigned FParamIdx = 0;
4505 for (
unsigned I = 0, N = PatternDecl->
getNumParams(); I != N; ++I) {
4509 assert(FParamIdx < Function->getNumParams());
4510 ParmVarDecl *FunctionParam = Function->getParamDecl(FParamIdx);
4527 Scope.InstantiatedLocal(PatternParam, FunctionParam);
4533 Scope.MakeInstantiatedLocalArgPack(PatternParam);
4534 std::optional<unsigned> NumArgumentsInExpansion =
4536 if (NumArgumentsInExpansion) {
4539 for (
unsigned Arg = 0; Arg < *NumArgumentsInExpansion; ++Arg) {
4552 Scope.InstantiatedLocalPackArg(PatternParam, FunctionParam);
4583 FD,
false,
nullptr,
true);
4589 L->DefaultArgumentInstantiated(Param);
4611 Diag(PointOfInstantiation, diag::err_exception_spec_cycle) <<
Decl;
4622 Decl,
false,
nullptr,
true);
4629 if (addInstantiatedParametersToScope(
Decl, Template,
Scope, TemplateArgs)) {
4663 if (ActiveInst.Kind == ActiveInstType::ExplicitTemplateArgumentSubstitution ||
4664 ActiveInst.Kind == ActiveInstType::DeducedTemplateArgumentSubstitution) {
4666 = dyn_cast<FunctionTemplateDecl>(ActiveInst.Entity)) {
4667 assert(FunTmpl->getTemplatedDecl() == Tmpl &&
4668 "Deduction from the wrong function template?");
4671 {ActiveInst.Entity->getCanonicalDecl(), ActiveInst.Kind});
4673 ActiveInst.Kind = ActiveInstType::TemplateInstantiation;
4674 ActiveInst.Entity = New;
4680 assert(Proto &&
"Function template without prototype?");
4704 assert(NewProto &&
"Template instantiation without function prototype?");
4722 LateAttrs, StartingScope);
4738 if (isa<CXXDestructorDecl>(New) && SemaRef.
getLangOpts().CPlusPlus11)
4754 Lookups.reserve(DFI->getUnqualifiedLookups().size());
4755 bool AnyChanged =
false;
4758 DA.getDecl(), TemplateArgs);
4761 AnyChanged |= (D != DA.getDecl());
4790 *
this, Loc, FTD, Args->
asArray(),
4822 bool DefinitionRequired,
4824 if (Function->isInvalidDecl() || isa<CXXDeductionGuideDecl>(Function))
4830 Function->getTemplateSpecializationKindForInstantiation();
4837 !DefinitionRequired)
4842 if (Function->isDefined(ExistingDefn,
4855 const FunctionDecl *PatternDecl = Function->getTemplateInstantiationPattern();
4856 assert(PatternDecl &&
"instantiating a non-template");
4859 Stmt *Pattern =
nullptr;
4861 Pattern = PatternDef->
getBody(PatternDef);
4862 PatternDecl = PatternDef;
4864 PatternDef =
nullptr;
4870 Function->getInstantiatedFromMemberFunction(),
4871 PatternDecl, PatternDef, TSK,
4872 DefinitionRequired)) {
4873 if (DefinitionRequired)
4874 Function->setInvalidDecl();
4876 (Function->isConstexpr() && !Recursive)) {
4880 Function->setInstantiationIsPending(
true);
4882 std::make_pair(Function, PointOfInstantiation));
4886 Diag(PointOfInstantiation, diag::warn_func_template_missing)
4890 Diag(PointOfInstantiation, diag::note_inst_declaration_hint)
4901 Function->setInstantiationIsPending(
true);
4903 std::make_pair(Function, PointOfInstantiation));
4907 llvm::TimeTraceScope TimeScope(
"InstantiateFunction", [&]() {
4909 llvm::raw_string_ostream
OS(Name);
4934 "missing LateParsedTemplate");
4936 Pattern = PatternDecl->
getBody(PatternDecl);
4943 "unexpected kind of function template definition");
4958 for (
auto *D = Function->getMostRecentDecl(); ;
4960 D->setImplicitlyInline();
4970 "instantiating function definition");
4974 Function->setVisibleDespiteOwningModule();
4988 bool MergeWithParentScope =
false;
4989 if (
CXXRecordDecl *Rec = dyn_cast<CXXRecordDecl>(Function->getDeclContext()))
4990 MergeWithParentScope =
4991 Rec->isLocalClass() && !Function->isFunctionTemplateSpecialization();
4994 auto RebuildTypeSourceInfoForDefaultSpecialMembers = [&]() {
5000 "Special member needs to be defaulted");
5008 auto *NewRec = dyn_cast<CXXRecordDecl>(Function->getDeclContext());
5009 const auto *PatternRec =