38#include "llvm/ADT/STLForwardCompat.h"
39#include "llvm/ADT/StringExtras.h"
40#include "llvm/Support/ErrorHandling.h"
41#include "llvm/Support/SaveAndRestore.h"
42#include "llvm/Support/TimeProfiler.h"
55 const Decl *NextDecl =
nullptr;
57 bool ClearRelativeToPrimary =
true;
58 static Response Done() {
63 static Response ChangeDecl(
const Decl *ND) {
68 static Response ChangeDecl(
const DeclContext *Ctx) {
74 static Response UseNextDecl(
const Decl *CurDecl) {
78 static Response DontClearRelativeToPrimaryNextDecl(
const Decl *CurDecl) {
79 Response R = Response::UseNextDecl(CurDecl);
80 R.ClearRelativeToPrimary =
false;
89getPrimaryTemplateOfGenericLambda(
const FunctionDecl *LambdaCallOperator) {
91 return LambdaCallOperator;
93 if (
auto *FTD = dyn_cast_if_present<FunctionTemplateDecl>(
95 FTD && FTD->getInstantiatedFromMemberTemplate()) {
97 FTD->getInstantiatedFromMemberTemplate()->getTemplatedDecl();
103 }
else if (
auto *Prev = cast<CXXMethodDecl>(LambdaCallOperator)
104 ->getInstantiatedFromMemberFunction())
105 LambdaCallOperator = Prev;
109 return LambdaCallOperator;
112struct EnclosingTypeAliasTemplateDetails {
117 explicit operator bool()
noexcept {
return Template; }
122EnclosingTypeAliasTemplateDetails
123getEnclosingTypeAliasTemplateDecl(
Sema &SemaRef) {
126 TypeAliasTemplateInstantiation)
128 EnclosingTypeAliasTemplateDetails Result;
129 auto *TATD = cast<TypeAliasTemplateDecl>(CSC.Entity),
130 *Next = TATD->getInstantiatedFromMemberTemplate();
134 CSC.template_arguments(),
137 Result.PrimaryTypeAliasDecl = Next;
138 Next = Next->getInstantiatedFromMemberTemplate();
151bool isLambdaEnclosedByTypeAliasDecl(
155 Visitor(
const FunctionDecl *CallOperator) : CallOperator(CallOperator) {}
156 bool VisitLambdaExpr(
LambdaExpr *LE)
override {
159 return getPrimaryTemplateOfGenericLambda(LE->getCallOperator()) !=
168 return !Visitor(getPrimaryTemplateOfGenericLambda(LambdaCallOperator))
169 .TraverseType(Underlying);
176 bool SkipForSpecialization) {
180 return Response::DontClearRelativeToPrimaryNextDecl(VarTemplSpec);
184 !isa<VarTemplatePartialSpecializationDecl>(VarTemplSpec))
185 return Response::Done();
190 llvm::PointerUnion<VarTemplateDecl *, VarTemplatePartialSpecializationDecl *>
194 if (!SkipForSpecialization)
195 Result.addOuterTemplateArguments(
198 if (Partial->isMemberSpecialization())
199 return Response::Done();
202 if (!SkipForSpecialization)
203 Result.addOuterTemplateArguments(
207 return Response::Done();
209 return Response::DontClearRelativeToPrimaryNextDecl(VarTemplSpec);
221 for (
unsigned I = 0, N = TTP->
getDepth() + 1; I != N; ++I)
222 Result.addOuterTemplateArguments(std::nullopt);
223 return Response::Done();
226Response HandlePartialClassTemplateSpec(
229 if (!SkipForSpecialization)
231 return Response::Done();
238 bool SkipForSpecialization) {
242 !isa<ClassTemplatePartialSpecializationDecl>(ClassTemplSpec))
243 return Response::Done();
245 if (!SkipForSpecialization)
246 Result.addOuterTemplateArguments(
255 return Response::Done();
261 if (
auto *InstFromPartialTempl =
264 return Response::ChangeDecl(
265 InstFromPartialTempl->getLexicalDeclContext());
267 return Response::UseNextDecl(ClassTemplSpec);
273 bool ForConstraintInstantiation,
274 bool ForDefaultArgumentSubstitution) {
276 if (!RelativeToPrimary &&
277 Function->getTemplateSpecializationKindForInstantiation() ==
279 return Response::Done();
281 if (!RelativeToPrimary &&
286 return Response::UseNextDecl(
Function);
288 Function->getTemplateSpecializationArgs()) {
291 TemplateArgs->asArray(),
294 if (RelativeToPrimary &&
295 (
Function->getTemplateSpecializationKind() ==
298 !
Function->getPrimaryTemplate()->getFriendObjectKind())))
299 return Response::UseNextDecl(
Function);
303 assert(
Function->getPrimaryTemplate() &&
"No function template?");
304 if (!ForDefaultArgumentSubstitution &&
305 Function->getPrimaryTemplate()->isMemberSpecialization())
306 return Response::Done();
309 if (!ForConstraintInstantiation &&
311 return Response::Done();
313 }
else if (
Function->getDescribedFunctionTemplate()) {
315 (ForConstraintInstantiation || Result.getNumSubstitutedLevels() == 0) &&
316 "Outer template not instantiated?");
323 Function->getNonTransparentDeclContext()->isFileContext() &&
325 return Response::ChangeDecl(
Function->getLexicalDeclContext());
328 if (ForConstraintInstantiation &&
Function->getFriendObjectKind())
329 return Response::ChangeDecl(
Function->getLexicalDeclContext());
330 return Response::UseNextDecl(
Function);
333Response HandleFunctionTemplateDecl(
Sema &SemaRef,
336 if (!isa<ClassTemplateSpecializationDecl>(FTD->
getDeclContext())) {
337 Result.addOuterTemplateArguments(
366 if (TSTy->isCurrentInstantiation()) {
367 auto *RD = TSTy->getCanonicalTypeInternal()->getAsCXXRecordDecl();
369 Arguments = CTD->getInjectedTemplateArgs(SemaRef.
Context);
371 dyn_cast<ClassTemplateSpecializationDecl>(RD))
375 Result.addOuterTemplateArguments(
376 TSTy->getTemplateName().getAsTemplateDecl(), Arguments,
391 bool ForConstraintInstantiation) {
394 (ForConstraintInstantiation || Result.getNumSubstitutedLevels() == 0) &&
395 "Outer template not instantiated?");
396 if (ClassTemplate->isMemberSpecialization())
397 return Response::Done();
398 if (ForConstraintInstantiation)
399 Result.addOuterTemplateArguments(
401 ClassTemplate->getInjectedTemplateArgs(SemaRef.
Context),
408 return Response::Done();
413 if (ForConstraintInstantiation && IsFriend &&
422 return Response::ChangeDecl(LCD);
426 if (
auto TypeAlias = getEnclosingTypeAliasTemplateDecl(SemaRef);
427 ForConstraintInstantiation && TypeAlias) {
429 TypeAlias.PrimaryTypeAliasDecl)) {
430 Result.addOuterTemplateArguments(TypeAlias.Template,
431 TypeAlias.AssociatedTemplateArguments,
446 return Response::ChangeDecl(TypeAlias.Template->getDeclContext());
451 return Response::UseNextDecl(Rec);
454Response HandleImplicitConceptSpecializationDecl(
457 Result.addOuterTemplateArguments(
461 return Response::UseNextDecl(CSD);
464Response HandleGenericDeclContext(
const Decl *CurDecl) {
465 return Response::UseNextDecl(CurDecl);
473 const FunctionDecl *Pattern,
bool ForConstraintInstantiation,
474 bool SkipForSpecialization,
bool ForDefaultArgumentSubstitution) {
475 assert((ND || DC) &&
"Can't find arguments for a decl if one isn't provided");
480 const Decl *CurDecl = ND;
486 Result.addOuterTemplateArguments(
const_cast<NamedDecl *
>(ND), *Innermost,
496 if (
const auto *TTP = dyn_cast<TemplateTemplateParmDecl>(CurDecl))
497 HandleDefaultTempArgIntoTempTempParam(TTP,
Result);
498 CurDecl = Response::UseNextDecl(CurDecl).NextDecl;
503 if (
const auto *VarTemplSpec =
504 dyn_cast<VarTemplateSpecializationDecl>(CurDecl)) {
505 R = HandleVarTemplateSpec(VarTemplSpec,
Result, SkipForSpecialization);
506 }
else if (
const auto *PartialClassTemplSpec =
507 dyn_cast<ClassTemplatePartialSpecializationDecl>(CurDecl)) {
508 R = HandlePartialClassTemplateSpec(PartialClassTemplSpec,
Result,
509 SkipForSpecialization);
510 }
else if (
const auto *ClassTemplSpec =
511 dyn_cast<ClassTemplateSpecializationDecl>(CurDecl)) {
512 R = HandleClassTemplateSpec(ClassTemplSpec,
Result,
513 SkipForSpecialization);
514 }
else if (
const auto *
Function = dyn_cast<FunctionDecl>(CurDecl)) {
515 R = HandleFunction(*
this,
Function,
Result, Pattern, RelativeToPrimary,
516 ForConstraintInstantiation,
517 ForDefaultArgumentSubstitution);
518 }
else if (
const auto *Rec = dyn_cast<CXXRecordDecl>(CurDecl)) {
520 ForConstraintInstantiation);
521 }
else if (
const auto *CSD =
522 dyn_cast<ImplicitConceptSpecializationDecl>(CurDecl)) {
523 R = HandleImplicitConceptSpecializationDecl(CSD,
Result);
524 }
else if (
const auto *FTD = dyn_cast<FunctionTemplateDecl>(CurDecl)) {
525 R = HandleFunctionTemplateDecl(*
this, FTD,
Result);
526 }
else if (
const auto *CTD = dyn_cast<ClassTemplateDecl>(CurDecl)) {
527 R = Response::ChangeDecl(CTD->getLexicalDeclContext());
528 }
else if (!isa<DeclContext>(CurDecl)) {
529 R = Response::DontClearRelativeToPrimaryNextDecl(CurDecl);
530 if (
const auto *TTP = dyn_cast<TemplateTemplateParmDecl>(CurDecl)) {
531 R = HandleDefaultTempArgIntoTempTempParam(TTP,
Result);
534 R = HandleGenericDeclContext(CurDecl);
539 if (R.ClearRelativeToPrimary)
540 RelativeToPrimary =
false;
542 CurDecl = R.NextDecl;
585 llvm_unreachable(
"Invalid SynthesisKind!");
615 AlreadyInstantiating = !Inst.Entity ?
false :
617 .insert({Inst.Entity->getCanonicalDecl(), Inst.Kind})
628 PointOfInstantiation, InstantiationRange, Entity) {}
635 PointOfInstantiation, InstantiationRange, Entity) {}
645 Template, TemplateArgs) {}
655 TemplateArgs, &DeductionInfo) {
669 PointOfInstantiation, InstantiationRange, Template, nullptr,
670 TemplateArgs, &DeductionInfo) {}
680 PointOfInstantiation, InstantiationRange, PartialSpec, nullptr,
681 TemplateArgs, &DeductionInfo) {}
691 PointOfInstantiation, InstantiationRange, PartialSpec, nullptr,
692 TemplateArgs, &DeductionInfo) {}
700 PointOfInstantiation, InstantiationRange, Param, nullptr,
710 PointOfInstantiation, InstantiationRange, Param, Template,
720 PointOfInstantiation, InstantiationRange, Param, Template,
729 PointOfInstantiation, InstantiationRange, Entity,
730 nullptr, TemplateArgs) {}
738 PointOfInstantiation, InstantiationRange, Param, Template,
747 PointOfInstantiation, InstantiationRange, nullptr,
748 nullptr, {}, &DeductionInfo) {}
756 PointOfInstantiation, InstantiationRange, nullptr,
764 PointOfInstantiation, InstantiationRange, nullptr,
765 nullptr, {}, &DeductionInfo) {}
773 PointOfInstantiation, InstantiationRange, Template, nullptr,
782 PointOfInstantiation, InstantiationRange, Template, nullptr,
783 {}, &DeductionInfo) {}
791 PointOfInstantiation, InstantiationRange, Template) {}
799 PointOfInstantiation, InstantiationRange, Template) {}
806 PointOfInstantiation, InstantiationRange, Entity) {}
825 if (!Active.isInstantiationRecord()) {
835 "forgot to remove a lookup module for a template instantiation");
854 if (!AlreadyInstantiating) {
858 {Active.Entity->getCanonicalDecl(), Active.Kind});
872 llvm::raw_string_ostream
OS(
Result);
873 llvm::ListSeparator Comma;
874 for (
const Expr *Arg : Args) {
876 Arg->IgnoreParens()->printPretty(
OS,
nullptr,
882bool Sema::InstantiatingTemplate::CheckInstantiationDepth(
893 diag::err_template_recursion_depth_exceeded)
895 << InstantiationRange;
896 SemaRef.
Diag(PointOfInstantiation, diag::note_template_recursion_depth)
906 SkipStart = Limit / 2 + Limit % 2;
911 unsigned InstantiationIdx = 0;
916 ++Active, ++InstantiationIdx) {
918 if (InstantiationIdx >= SkipStart && InstantiationIdx < SkipEnd) {
919 if (InstantiationIdx == SkipStart) {
922 diag::note_instantiation_contexts_suppressed)
928 switch (Active->Kind) {
930 Decl *
D = Active->Entity;
932 unsigned DiagID = diag::note_template_member_class_here;
933 if (isa<ClassTemplateSpecializationDecl>(
Record))
934 DiagID = diag::note_template_class_instantiation_here;
936 <<
Record << Active->InstantiationRange;
940 DiagID = diag::note_function_template_spec_here;
942 DiagID = diag::note_template_member_function_here;
945 << Active->InstantiationRange;
946 }
else if (
VarDecl *VD = dyn_cast<VarDecl>(
D)) {
948 VD->isStaticDataMember()?
949 diag::note_template_static_data_member_def_here
950 : diag::note_template_variable_def_here)
952 << Active->InstantiationRange;
953 }
else if (
EnumDecl *ED = dyn_cast<EnumDecl>(
D)) {
955 diag::note_template_enum_def_here)
957 << Active->InstantiationRange;
958 }
else if (
FieldDecl *FD = dyn_cast<FieldDecl>(
D)) {
960 diag::note_template_nsdmi_here)
961 << FD << Active->InstantiationRange;
964 diag::note_template_class_instantiation_here)
965 << CTD << Active->InstantiationRange;
971 TemplateDecl *Template = cast<TemplateDecl>(Active->Template);
973 llvm::raw_svector_ostream
OS(TemplateArgsStr);
978 diag::note_default_arg_instantiation_here)
980 << Active->InstantiationRange;
987 diag::note_explicit_template_arg_substitution_here)
990 Active->TemplateArgs,
991 Active->NumTemplateArgs)
992 << Active->InstantiationRange;
998 dyn_cast<FunctionTemplateDecl>(Active->Entity)) {
1000 diag::note_function_template_deduction_instantiation_here)
1003 Active->TemplateArgs,
1004 Active->NumTemplateArgs)
1005 << Active->InstantiationRange;
1007 bool IsVar = isa<VarTemplateDecl>(Active->Entity) ||
1008 isa<VarTemplateSpecializationDecl>(Active->Entity);
1009 bool IsTemplate =
false;
1011 if (
auto *
D = dyn_cast<TemplateDecl>(Active->Entity)) {
1013 Params =
D->getTemplateParameters();
1014 }
else if (
auto *
D = dyn_cast<ClassTemplatePartialSpecializationDecl>(
1016 Params =
D->getTemplateParameters();
1017 }
else if (
auto *
D = dyn_cast<VarTemplatePartialSpecializationDecl>(
1019 Params =
D->getTemplateParameters();
1021 llvm_unreachable(
"unexpected template kind");
1025 diag::note_deduced_template_arg_substitution_here)
1026 << IsVar << IsTemplate << cast<NamedDecl>(Active->Entity)
1028 Active->NumTemplateArgs)
1029 << Active->InstantiationRange;
1035 ParmVarDecl *Param = cast<ParmVarDecl>(Active->Entity);
1039 llvm::raw_svector_ostream
OS(TemplateArgsStr);
1044 diag::note_default_function_arg_instantiation_here)
1046 << Active->InstantiationRange;
1051 NamedDecl *Parm = cast<NamedDecl>(Active->Entity);
1054 Name = std::string(
" '") + Parm->
getName().str() +
"'";
1057 if (
TemplateDecl *Template = dyn_cast<TemplateDecl>(Active->Template))
1058 TemplateParams = Template->getTemplateParameters();
1061 cast<ClassTemplatePartialSpecializationDecl>(Active->Template)
1062 ->getTemplateParameters();
1064 diag::note_prior_template_arg_substitution)
1065 << isa<TemplateTemplateParmDecl>(Parm)
1068 Active->TemplateArgs,
1069 Active->NumTemplateArgs)
1070 << Active->InstantiationRange;
1076 if (
TemplateDecl *Template = dyn_cast<TemplateDecl>(Active->Template))
1077 TemplateParams = Template->getTemplateParameters();
1080 cast<ClassTemplatePartialSpecializationDecl>(Active->Template)
1081 ->getTemplateParameters();
1084 diag::note_template_default_arg_checking)
1086 Active->TemplateArgs,
1087 Active->NumTemplateArgs)
1088 << Active->InstantiationRange;
1094 diag::note_evaluating_exception_spec_here)
1095 << cast<FunctionDecl>(Active->Entity);
1100 diag::note_template_exception_spec_instantiation_here)
1101 << cast<FunctionDecl>(Active->Entity)
1102 << Active->InstantiationRange;
1107 diag::note_template_requirement_instantiation_here)
1108 << Active->InstantiationRange;
1112 diag::note_template_requirement_params_instantiation_here)
1113 << Active->InstantiationRange;
1118 diag::note_nested_requirement_here)
1119 << Active->InstantiationRange;
1124 diag::note_in_declaration_of_implicit_special_member)
1125 << cast<CXXRecordDecl>(Active->Entity)
1126 << llvm::to_underlying(Active->SpecialMember);
1131 diag::note_in_declaration_of_implicit_equality_comparison);
1137 auto *FD = dyn_cast<FunctionDecl>(Active->Entity);
1141 auto *MD = cast<CXXMethodDecl>(FD);
1143 diag::note_member_synthesized_at)
1144 << MD->isExplicitlyDefaulted()
1150 .getNonReferenceType()
1151 .getUnqualifiedType();
1153 diag::note_comparison_synthesized_at)
1161 diag::note_rewriting_operator_as_spaceship);
1166 diag::note_in_binding_decl_init)
1167 << cast<BindingDecl>(Active->Entity);
1172 diag::note_due_to_dllexported_class)
1173 << cast<CXXRecordDecl>(Active->Entity) << !
getLangOpts().CPlusPlus11;
1178 diag::note_building_builtin_dump_struct_call)
1188 diag::note_lambda_substitution_here);
1191 unsigned DiagID = 0;
1192 if (!Active->Entity) {
1194 diag::note_nested_requirement_here)
1195 << Active->InstantiationRange;
1198 if (isa<ConceptDecl>(Active->Entity))
1199 DiagID = diag::note_concept_specialization_here;
1200 else if (isa<TemplateDecl>(Active->Entity))
1201 DiagID = diag::note_checking_constraints_for_template_id_here;
1202 else if (isa<VarTemplatePartialSpecializationDecl>(Active->Entity))
1203 DiagID = diag::note_checking_constraints_for_var_spec_id_here;
1204 else if (isa<ClassTemplatePartialSpecializationDecl>(Active->Entity))
1205 DiagID = diag::note_checking_constraints_for_class_spec_id_here;
1207 assert(isa<FunctionDecl>(Active->Entity));
1208 DiagID = diag::note_checking_constraints_for_function_here;
1211 llvm::raw_svector_ostream
OS(TemplateArgsStr);
1213 if (!isa<FunctionDecl>(Active->Entity)) {
1217 Diags.
Report(Active->PointOfInstantiation, DiagID) <<
OS.str()
1218 << Active->InstantiationRange;
1223 diag::note_constraint_substitution_here)
1224 << Active->InstantiationRange;
1228 diag::note_constraint_normalization_here)
1229 << cast<NamedDecl>(Active->Entity) << Active->InstantiationRange;
1233 diag::note_parameter_mapping_substitution_here)
1234 << Active->InstantiationRange;
1238 diag::note_building_deduction_guide_here);
1242 diag::note_template_type_alias_instantiation_here)
1243 << cast<TypeAliasTemplateDecl>(Active->Entity)
1244 << Active->InstantiationRange;
1252 return std::optional<TemplateDeductionInfo *>(
nullptr);
1257 Active != ActiveEnd;
1260 switch (Active->Kind) {
1264 if (isa<TypeAliasTemplateDecl>(Active->Entity))
1275 return std::nullopt;
1282 return std::nullopt;
1303 assert(Active->DeductionInfo &&
"Missing deduction info pointer");
1304 return Active->DeductionInfo;
1315 return std::nullopt;
1329 if (Active->SavedInNonInstantiationSFINAEContext)
1330 return std::optional<TemplateDeductionInfo *>(
nullptr);
1333 return std::nullopt;
1340 class TemplateInstantiator :
public TreeTransform<TemplateInstantiator> {
1345 bool EvaluateConstraints =
true;
1348 bool IsIncomplete =
false;
1350 bool BailOutOnIncomplete;
1355 TemplateInstantiator(
Sema &SemaRef,
1358 bool BailOutOnIncomplete =
false)
1359 : inherited(SemaRef), TemplateArgs(TemplateArgs),
Loc(
Loc),
1360 Entity(Entity), BailOutOnIncomplete(BailOutOnIncomplete) {}
1362 void setEvaluateConstraints(
bool B) {
1363 EvaluateConstraints = B;
1365 bool getEvaluateConstraints() {
1366 return EvaluateConstraints;
1383 bool getIsIncomplete()
const {
return IsIncomplete; }
1389 this->Entity = Entity;
1392 unsigned TransformTemplateDepth(
unsigned Depth) {
1397 int Index = getSema().ArgumentPackSubstitutionIndex;
1399 return std::nullopt;
1406 bool &ShouldExpand,
bool &RetainExpansion,
1407 std::optional<unsigned> &NumExpansions) {
1408 return getSema().CheckParameterPacksForExpansion(EllipsisLoc,
1409 PatternRange, Unexpanded,
1416 void ExpandingFunctionParameterPack(
ParmVarDecl *Pack) {
1426 unsigned Depth, Index;
1429 Result = TemplateArgs(Depth, Index);
1432 IsIncomplete =
true;
1433 if (BailOutOnIncomplete)
1449 unsigned Depth, Index;
1459 void transformAttrs(
Decl *Old,
Decl *New) {
1465 (NewDecls.size() != 1 || !NewDecls.front()->isParameterPack())) {
1467 for (
auto *New : NewDecls)
1469 Old, cast<VarDecl>(New));
1473 assert(NewDecls.size() == 1 &&
1474 "should only have multiple expansions for a pack");
1475 Decl *New = NewDecls.front();
1480 auto *NewMD = dyn_cast<CXXMethodDecl>(New);
1482 auto *OldMD = dyn_cast<CXXMethodDecl>(Old);
1483 if (
auto *NewTD = NewMD->getDescribedFunctionTemplate())
1484 NewTD->setInstantiatedFromMemberTemplate(
1485 OldMD->getDescribedFunctionTemplate());
1487 NewMD->setInstantiationOfMemberFunction(OldMD,
1495 if (
auto *DC = dyn_cast<DeclContext>(Old);
1496 DC && DC->isDependentContext() && DC->isFunctionOrMethod())
1537 NamedDecl *FirstQualifierInScope =
nullptr,
1538 bool AllowInjectedClassName =
false);
1540 const AnnotateAttr *TransformAnnotateAttr(
const AnnotateAttr *AA);
1541 const CXXAssumeAttr *TransformCXXAssumeAttr(
const CXXAssumeAttr *AA);
1542 const LoopHintAttr *TransformLoopHintAttr(
const LoopHintAttr *LH);
1543 const NoInlineAttr *TransformStmtNoInlineAttr(
const Stmt *OrigS,
1545 const NoInlineAttr *A);
1546 const AlwaysInlineAttr *
1547 TransformStmtAlwaysInlineAttr(
const Stmt *OrigS,
const Stmt *InstS,
1548 const AlwaysInlineAttr *A);
1549 const CodeAlignAttr *TransformCodeAlignAttr(
const CodeAlignAttr *CA);
1556 ExprResult TransformSubstNonTypeTemplateParmPackExpr(
1558 ExprResult TransformSubstNonTypeTemplateParmExpr(
1575 return inherited::TransformFunctionProtoType(TLB, TL);
1580 auto Type = inherited::TransformInjectedClassNameType(TLB, TL);
1583 if (
Type.isNull() &&
1590 inherited::TransformType(ICT->getInjectedSpecializationType());
1601 bool Uneval =
false) {
1603 std::vector<TemplateArgument> TArgs;
1623 return inherited::TransformTemplateArgument(Input, Output, Uneval);
1626 template<
typename Fn>
1631 Fn TransformExceptionSpec);
1634 TransformFunctionTypeParam(
ParmVarDecl *OldParm,
int indexAdjustment,
1635 std::optional<unsigned> NumExpansions,
1636 bool ExpectParameterPack);
1638 using inherited::TransformTemplateTypeParmType;
1643 bool SuppressObjCLifetime);
1645 QualType BuildSubstTemplateTypeParmType(
1647 Decl *AssociatedDecl,
unsigned Index, std::optional<unsigned> PackIndex,
1653 using inherited::TransformSubstTemplateTypeParmPackType;
1657 bool SuppressObjCLifetime);
1663 if (
Type->getSubstitutionFlag() !=
1664 SubstTemplateTypeParmTypeFlag::ExpandPacksInPlace)
1665 return inherited::TransformSubstTemplateTypeParmType(TLB, TL);
1667 assert(
Type->getPackIndex());
1669 Type->getReplacedParameter()->getDepth(),
Type->getIndex());
1674 return inherited::TransformSubstTemplateTypeParmType(TLB, TL);
1679 if (
auto TypeAlias =
1680 TemplateInstArgsHelpers::getEnclosingTypeAliasTemplateDecl(
1682 TypeAlias && TemplateInstArgsHelpers::isLambdaEnclosedByTypeAliasDecl(
1684 unsigned TypeAliasDeclDepth =
TypeAlias.Template->getTemplateDepth();
1686 return CXXRecordDecl::LambdaDependencyKind::LDK_AlwaysDependent;
1689 return CXXRecordDecl::LambdaDependencyKind::LDK_AlwaysDependent;
1691 return inherited::ComputeLambdaDependency(LSI);
1703 return inherited::TransformLambdaExpr(
E);
1709 return inherited::TransformBlockExpr(
E);
1716 assert(PVD &&
"null in a parameter list");
1717 if (!PVD->hasDefaultArg())
1719 Expr *UninstExpr = PVD->getUninstantiatedDefaultArg();
1730 PVD->setDefaultArg(ErrorResult.
get());
1733 return inherited::RebuildLambdaExpr(StartLoc, EndLoc, LSI);
1747 return inherited::TransformLambdaBody(
E, Body);
1751 ExprResult Transformed = inherited::TransformSizeOfPackExpr(
E);
1754 auto *TransformedExpr = cast<SizeOfPackExpr>(Transformed.
get());
1757 TransformedExpr->getPack() ==
E->getPack()) {
1759 TransformDecl(
E->getPackLoc(), TransformedExpr->getPack());
1762 TransformedExpr->setPack(cast<NamedDecl>(NewPack));
1764 return TransformedExpr;
1769 ExprResult TransReq = inherited::TransformRequiresExpr(
E);
1772 assert(TransReq.
get() !=
E &&
1773 "Do not change value of isSatisfied for the existing expression. "
1774 "Create a new expression instead.");
1775 if (
E->getBody()->isDependentContext()) {
1781 if (Trap.hasErrorOccurred())
1787 bool TransformRequiresExprRequirements(
1790 bool SatisfactionDetermined =
false;
1793 if (!SatisfactionDetermined) {
1794 if (
auto *TypeReq = dyn_cast<concepts::TypeRequirement>(Req))
1795 TransReq = TransformTypeRequirement(TypeReq);
1796 else if (
auto *ExprReq = dyn_cast<concepts::ExprRequirement>(Req))
1797 TransReq = TransformExprRequirement(ExprReq);
1799 TransReq = TransformNestedRequirement(
1800 cast<concepts::NestedRequirement>(Req));
1809 SatisfactionDetermined =
true;
1812 Transformed.push_back(TransReq);
1819 if (!OrigTPL || !OrigTPL->
size())
return OrigTPL;
1823 Owner, TemplateArgs);
1824 DeclInstantiator.setEvaluateConstraints(EvaluateConstraints);
1825 return DeclInstantiator.SubstTemplateParams(OrigTPL);
1843 transformNonTypeTemplateParmRef(
Decl *AssociatedDecl,
1846 std::optional<unsigned> PackIndex);
1850bool TemplateInstantiator::AlreadyTransformed(
QualType T) {
1857 getSema().MarkDeclarationsReferencedInType(
Loc,
T);
1882 TTP->getPosition())) {
1883 IsIncomplete =
true;
1884 return BailOutOnIncomplete ? nullptr :
D;
1889 if (TTP->isParameterPack()) {
1900 "Missing argument pack");
1906 "Wrong kind of template template argument");
1918 Decl *Inst = getSema().SubstDecl(
D, getSema().
CurContext, TemplateArgs);
1922 getSema().CurrentInstantiationScope->InstantiatedLocal(
D, Inst);
1926bool TemplateInstantiator::TransformExceptionSpec(
1933 return inherited::TransformExceptionSpec(
Loc, ESI, Exceptions, Changed);
1937TemplateInstantiator::TransformFirstQualifierInScope(
NamedDecl *
D,
1951 "Missing argument pack");
1961 return cast_or_null<NamedDecl>(TransformDecl(
Loc,
D));
1964 return Tag->getDecl();
1967 getSema().Diag(
Loc, diag::err_nested_name_spec_non_tag) <<
T;
1972 return cast_or_null<NamedDecl>(TransformDecl(
Loc,
D));
1976TemplateInstantiator::RebuildExceptionDecl(
VarDecl *ExceptionDecl,
1982 StartLoc, NameLoc, Name);
1984 getSema().CurrentInstantiationScope->InstantiatedLocal(ExceptionDecl, Var);
1988VarDecl *TemplateInstantiator::RebuildObjCExceptionDecl(
VarDecl *ExceptionDecl,
1991 VarDecl *Var = inherited::RebuildObjCExceptionDecl(ExceptionDecl, TSInfo,
T);
1993 getSema().CurrentInstantiationScope->InstantiatedLocal(ExceptionDecl, Var);
1998TemplateInstantiator::RebuildElaboratedType(
SourceLocation KeywordLoc,
2016 SemaRef.
Diag(TagLocation, diag::err_use_with_wrong_tag)
2025 return inherited::RebuildElaboratedType(KeywordLoc, Keyword, QualifierLoc,
T);
2028TemplateName TemplateInstantiator::TransformTemplateName(
2031 bool AllowInjectedClassName) {
2033 = dyn_cast_or_null<TemplateTemplateParmDecl>(Name.getAsTemplateDecl())) {
2040 TTP->getPosition())) {
2041 IsIncomplete =
true;
2052 "unexpected pack arguments in template rewrite");
2056 "unexpected nontype template argument kind in template rewrite");
2060 auto [AssociatedDecl, Final] =
2062 std::optional<unsigned> PackIndex;
2065 "Missing argument pack");
2071 return getSema().Context.getSubstTemplateTemplateParmPack(
2072 Arg, AssociatedDecl, TTP->
getIndex(), Final);
2075 PackIndex = getPackIndex(Arg);
2080 assert(!Template.
isNull() &&
"Null template template argument");
2084 return getSema().Context.getSubstTemplateTemplateParm(
2085 Template, AssociatedDecl, TTP->
getIndex(), PackIndex);
2090 = Name.getAsSubstTemplateTemplateParmPack()) {
2097 if (SubstPack->getFinal())
2099 return getSema().Context.getSubstTemplateTemplateParm(
2100 Template, SubstPack->getAssociatedDecl(), SubstPack->getIndex(),
2101 getPackIndex(Pack));
2104 return inherited::TransformTemplateName(SS, Name, NameLoc, ObjectType,
2105 FirstQualifierInScope,
2106 AllowInjectedClassName);
2114 return getSema().BuildPredefinedExpr(
E->getLocation(),
E->getIdentKind());
2118TemplateInstantiator::TransformTemplateParmRefExpr(
DeclRefExpr *
E,
2126 IsIncomplete =
true;
2127 return BailOutOnIncomplete ?
ExprError() :
E;
2137 "unexpected pack arguments in template rewrite");
2141 "unexpected nontype template argument kind in template rewrite");
2148 std::optional<unsigned> PackIndex;
2151 "Missing argument pack");
2169 E->getLocation(), Arg, AssociatedDecl, NTTP->
getPosition());
2171 PackIndex = getPackIndex(Arg);
2175 return transformNonTypeTemplateParmRef(AssociatedDecl, NTTP,
E->getLocation(),
2180TemplateInstantiator::TransformAnnotateAttr(
const AnnotateAttr *AA) {
2182 for (
Expr *Arg : AA->args()) {
2183 ExprResult Res = getDerived().TransformExpr(Arg);
2185 Args.push_back(Res.
get());
2187 return AnnotateAttr::CreateImplicit(getSema().
Context, AA->getAnnotation(),
2188 Args.data(), Args.size(), AA->getRange());
2191const CXXAssumeAttr *
2192TemplateInstantiator::TransformCXXAssumeAttr(
const CXXAssumeAttr *AA) {
2193 ExprResult Res = getDerived().TransformExpr(AA->getAssumption());
2197 Res = getSema().ActOnFinishFullExpr(Res.
get(),
2202 if (!(Res.
get()->
getDependence() & ExprDependence::TypeValueInstantiation)) {
2203 Res = getSema().BuildCXXAssumeExpr(Res.
get(), AA->getAttrName(),
2209 return CXXAssumeAttr::CreateImplicit(getSema().
Context, Res.
get(),
2214TemplateInstantiator::TransformLoopHintAttr(
const LoopHintAttr *LH) {
2215 Expr *TransformedExpr = getDerived().TransformExpr(LH->getValue()).get();
2217 if (TransformedExpr == LH->getValue())
2222 LH->getSemanticSpelling() ==
2223 LoopHintAttr::Pragma_unroll))
2226 LoopHintAttr::OptionType Option = LH->getOption();
2227 LoopHintAttr::LoopHintState State = LH->getState();
2229 llvm::APSInt ValueAPS =
2232 if (ValueAPS.isZero() || ValueAPS.isOne()) {
2233 Option = LoopHintAttr::Unroll;
2234 State = LoopHintAttr::Disable;
2239 return LoopHintAttr::CreateImplicit(getSema().
Context, Option, State,
2240 TransformedExpr, *LH);
2242const NoInlineAttr *TemplateInstantiator::TransformStmtNoInlineAttr(
2243 const Stmt *OrigS,
const Stmt *InstS,
const NoInlineAttr *A) {
2249const AlwaysInlineAttr *TemplateInstantiator::TransformStmtAlwaysInlineAttr(
2250 const Stmt *OrigS,
const Stmt *InstS,
const AlwaysInlineAttr *A) {
2257const CodeAlignAttr *
2258TemplateInstantiator::TransformCodeAlignAttr(
const CodeAlignAttr *CA) {
2259 Expr *TransformedExpr = getDerived().TransformExpr(CA->getAlignment()).get();
2260 return getSema().BuildCodeAlignAttr(*CA, TransformedExpr);
2263ExprResult TemplateInstantiator::transformNonTypeTemplateParmRef(
2266 std::optional<unsigned> PackIndex) {
2271 auto SubstParamType = [&] {
2278 T = cast<PackExpansionType>(
T)->getPattern();
2282 bool refParam =
false;
2288 Expr *argExpr =
arg.getAsExpr();
2293 QualType paramType = SubstParamType();
2308 VD = cast_or_null<ValueDecl>(
2314 QualType paramType =
arg.getNonTypeTemplateArgumentType();
2315 assert(!paramType.
isNull() &&
"type substitution failed for param type");
2316 assert(!paramType->
isDependentType() &&
"param type still dependent");
2320 QualType paramType =
arg.getNonTypeTemplateArgumentType();
2331 Expr *resultExpr = result.
get();
2335 AssociatedDecl, parm->
getIndex(), PackIndex, refParam);
2339TemplateInstantiator::TransformSubstNonTypeTemplateParmPackExpr(
2349 return transformNonTypeTemplateParmRef(
2350 E->getAssociatedDecl(),
E->getParameterPack(),
2351 E->getParameterPackLocation(), Arg, getPackIndex(Pack));
2355TemplateInstantiator::TransformSubstNonTypeTemplateParmExpr(
2357 ExprResult SubstReplacement =
E->getReplacement();
2358 if (!isa<ConstantExpr>(SubstReplacement.
get()))
2359 SubstReplacement = TransformExpr(
E->getReplacement());
2388 SubstReplacement.
get(), SugaredConverted,
2392 return transformNonTypeTemplateParmRef(
E->getAssociatedDecl(),
2394 SugaredConverted,
E->getPackIndex());
2400 return getSema().BuildDeclarationNameExpr(
CXXScopeSpec(), NameInfo, PD);
2411 return RebuildVarDeclRefExpr(VD,
E->
getExprLoc());
2421 Vars.reserve(
E->getNumExpansions());
2432 E->getParameterPackLocation(), Vars);
2433 getSema().MarkFunctionParmPackReferenced(PackExpr);
2438TemplateInstantiator::TransformFunctionParmPackRefExpr(
DeclRefExpr *
E,
2441 llvm::PointerUnion<Decl *, DeclArgumentPack *> *
Found
2442 = getSema().CurrentInstantiationScope->findInstantiationOf(PD);
2443 assert(
Found &&
"no instantiation for parameter pack");
2445 Decl *TransformedDecl;
2446 if (DeclArgumentPack *Pack =
Found->dyn_cast<DeclArgumentPack *>()) {
2455 getSema().MarkFunctionParmPackReferenced(PackExpr);
2459 TransformedDecl = (*Pack)[getSema().ArgumentPackSubstitutionIndex];
2461 TransformedDecl = cast<Decl *>(*
Found);
2465 return RebuildVarDeclRefExpr(cast<VarDecl>(TransformedDecl),
E->
getExprLoc());
2469TemplateInstantiator::TransformDeclRefExpr(
DeclRefExpr *
E) {
2476 return TransformTemplateParmRefExpr(
E, NTTP);
2483 if (
VarDecl *PD = dyn_cast<VarDecl>(
D))
2485 return TransformFunctionParmPackRefExpr(
E, PD);
2487 return inherited::TransformDeclRefExpr(
E);
2490ExprResult TemplateInstantiator::TransformCXXDefaultArgExpr(
2492 assert(!cast<FunctionDecl>(
E->getParam()->getDeclContext())->
2493 getDescribedFunctionTemplate() &&
2494 "Default arg expressions are never formed in dependent cases.");
2496 E->getUsedLocation(), cast<FunctionDecl>(
E->getParam()->getDeclContext()),
2500template<
typename Fn>
2505 Fn TransformExceptionSpec) {
2514 std::optional<LocalInstantiationScope>
Scope;
2515 if (!Current || !Current->isLambdaOrBlock())
2518 return inherited::TransformFunctionProtoType(
2519 TLB, TL, ThisContext, ThisTypeQuals, TransformExceptionSpec);
2522ParmVarDecl *TemplateInstantiator::TransformFunctionTypeParam(
2524 std::optional<unsigned> NumExpansions,
bool ExpectParameterPack) {
2526 OldParm, TemplateArgs, indexAdjustment, NumExpansions,
2527 ExpectParameterPack, EvaluateConstraints);
2533QualType TemplateInstantiator::BuildSubstTemplateTypeParmType(
2535 Decl *AssociatedDecl,
unsigned Index, std::optional<unsigned> PackIndex,
2541 if (SuppressObjCLifetime) {
2543 RQs = Replacement.getQualifiers();
2554 QualType Result = getSema().Context.getSubstTemplateTypeParmType(
2555 Replacement, AssociatedDecl, Index, PackIndex);
2563TemplateInstantiator::TransformTemplateTypeParmType(
TypeLocBuilder &TLB,
2565 bool SuppressObjCLifetime) {
2576 IsIncomplete =
true;
2577 if (BailOutOnIncomplete)
2593 "unexpected pack arguments in template rewrite");
2597 "unexpected nontype template argument kind in template rewrite");
2603 auto [AssociatedDecl, Final] =
2605 std::optional<unsigned> PackIndex;
2606 if (
T->isParameterPack()) {
2608 "Missing argument pack");
2614 QualType Result = getSema().Context.getSubstTemplateTypeParmPackType(
2615 AssociatedDecl,
T->getIndex(), Final, Arg);
2623 PackIndex = getPackIndex(Arg);
2628 "Template argument kind mismatch");
2630 return BuildSubstTemplateTypeParmType(TLB, SuppressObjCLifetime, Final,
2631 AssociatedDecl,
T->getIndex(),
2641 NewTTPDecl = cast_or_null<TemplateTypeParmDecl>(
2645 T->isParameterPack(), NewTTPDecl);
2651QualType TemplateInstantiator::TransformSubstTemplateTypeParmPackType(
2653 bool SuppressObjCLifetime) {
2656 Decl *NewReplaced = TransformDecl(TL.
getNameLoc(),
T->getAssociatedDecl());
2661 if (NewReplaced !=
T->getAssociatedDecl())
2662 Result = getSema().Context.getSubstTemplateTypeParmPackType(
2663 NewReplaced,
T->getIndex(),
T->getFinal(),
T->getArgumentPack());
2672 return BuildSubstTemplateTypeParmType(
2673 TLB, SuppressObjCLifetime,
T->getFinal(), NewReplaced,
T->getIndex(),
2687 ErrorLoc = PDA.first;
2692 llvm::raw_svector_ostream
OS(Entity);
2696 C.backupStr(Entity), ErrorLoc,
C.backupStr(Message)};
2702 llvm::raw_svector_ostream
OS(Entity);
2706 C.backupStr(Entity),
2707 Location, StringRef()};
2710ExprResult TemplateInstantiator::TransformRequiresTypeParams(
2724 if (getDerived().TransformFunctionTypeParams(
2725 KWLoc, Params,
nullptr,
nullptr, PTypes,
2726 &TransParams, PInfos, &ErrorIdx) ||
2727 Trap.hasErrorOccurred()) {
2733 SemaRef, Info, [&](llvm::raw_ostream &
OS) {
OS << *FailedDecl; })));
2734 return getDerived().RebuildRequiresExpr(KWLoc, Body, RE->
getLParenLoc(),
2736 TransReqs, RBraceLoc);
2747 if (AlwaysRebuild())
2748 return RebuildTypeRequirement(
2758 if (TypeInst.isInvalid())
2761 if (!TransType || Trap.hasErrorOccurred())
2763 [&] (llvm::raw_ostream&
OS) {
2766 return RebuildTypeRequirement(TransType);
2776 llvm::PointerUnion<Expr *, concepts::Requirement::SubstitutionDiagnostic *>
2785 if (ExprInst.isInvalid())
2788 if (!TransExprRes.
isInvalid() && !Trap.hasErrorOccurred() &&
2791 if (TransExprRes.
isInvalid() || Trap.hasErrorOccurred())
2796 TransExpr = TransExprRes.
get();
2799 std::optional<concepts::ExprRequirement::ReturnTypeRequirement> TransRetReq;
2801 if (RetReq.isEmpty())
2802 TransRetReq.emplace();
2803 else if (RetReq.isSubstitutionFailure())
2804 TransRetReq.emplace(RetReq.getSubstitutionDiagnostic());
2805 else if (RetReq.isTypeConstraint()) {
2807 RetReq.getTypeConstraintTemplateParameterList();
2811 if (TPLInst.isInvalid())
2814 if (!TPL || Trap.hasErrorOccurred())
2816 [&] (llvm::raw_ostream&
OS) {
2817 RetReq.getTypeConstraint()->getImmediatelyDeclaredConstraint()
2822 TransRetReq.emplace(TPL);
2825 assert(TransRetReq &&
"All code paths leading here must set TransRetReq");
2826 if (
Expr *
E = TransExpr.dyn_cast<
Expr *>())
2828 std::move(*TransRetReq));
2829 return RebuildExprRequirement(
2830 cast<concepts::Requirement::SubstitutionDiagnostic *>(TransExpr),
2835TemplateInstantiator::TransformNestedRequirement(
2840 if (AlwaysRebuild())
2849 if (!getEvaluateConstraints()) {
2851 if (TransConstraint.
isInvalid() || !TransConstraint.
get())
2871 if (ConstrInst.isInvalid())
2875 nullptr, {Req->getConstraintExpr()},
Result, TemplateArgs,
2878 TransConstraint =
Result[0];
2879 assert(!Trap.hasErrorOccurred() &&
"Substitution failures must be handled "
2880 "by CheckConstraintSatisfaction.");
2886 if (TransConstraint.
isInvalid() || !TransConstraint.
get() ||
2889 llvm::raw_svector_ostream
OS(Entity);
2903 bool AllowDeducedTST) {
2905 "Cannot perform an instantiation without some context on the "
2906 "instantiation stack");
2912 TemplateInstantiator Instantiator(*
this, Args,
Loc, Entity);
2913 return AllowDeducedTST ? Instantiator.TransformTypeWithDeducedTST(
T)
2914 : Instantiator.TransformType(
T);
2922 "Cannot perform an instantiation without some context on the "
2923 "instantiation stack");
2937 TemplateInstantiator Instantiator(*
this, Args,
Loc, Entity);
2951 bool *IsIncompleteSubstitution) {
2953 "Cannot perform an instantiation without some context on the "
2954 "instantiation stack");
2961 TemplateInstantiator Instantiator(
2962 *
this, TemplateArgs,
Loc, Entity,
2963 IsIncompleteSubstitution !=
nullptr);
2964 QualType QT = Instantiator.TransformType(
T);
2965 if (IsIncompleteSubstitution && Instantiator.getIsIncomplete())
2966 *IsIncompleteSubstitution =
true;
2975 TypeLoc TL =
T->getTypeLoc().IgnoreParens();
2998 bool EvaluateConstraints) {
3000 "Cannot perform an instantiation without some context on the "
3001 "instantiation stack");
3006 TemplateInstantiator Instantiator(*
this, Args,
Loc, Entity);
3007 Instantiator.setEvaluateConstraints(EvaluateConstraints);
3023 Result = Instantiator.TransformFunctionProtoType(
3024 TLB, Proto, ThisContext, ThisTypeQuals,
3026 bool &Changed) {
return false; });
3028 Result = Instantiator.TransformType(TLB, TL);
3042 bool Changed =
false;
3044 return Instantiator.TransformExceptionSpec(
Loc, ESI, ExceptionStorage,
3055 ESI, ExceptionStorage, Args))
3064 struct GetContainedInventedTypeParmVisitor :
3065 public TypeVisitor<GetContainedInventedTypeParmVisitor,
3066 TemplateTypeParmDecl *> {
3067 using TypeVisitor<GetContainedInventedTypeParmVisitor,
3073 return Visit(
T.getTypePtr());
3078 if (!
T->getDecl() || !
T->getDecl()->isImplicit())
3080 return T->getDecl();
3087 return Visit(
T->getNamedType());
3099 return Visit(
T->getPointeeTypeAsWritten());
3107 return Visit(
T->getElementType());
3112 return Visit(
T->getElementType());
3116 return Visit(
T->getElementType());
3120 return VisitFunctionType(
T);
3128 return Visit(
T->getInnerType());
3132 return Visit(
T->getModifiedType());
3136 return Visit(
T->getUnderlyingType());
3140 return Visit(
T->getOriginalType());
3144 return Visit(
T->getPattern());
3152struct ExpandPackedTypeConstraints
3159 ExpandPackedTypeConstraints(
3161 : inherited(SemaRef), TemplateArgs(TemplateArgs) {}
3163 using inherited::TransformTemplateTypeParmType;
3168 if (!
T->isParameterPack()) {
3179 std::optional<unsigned> PackIndex;
3184 TL.
getType(),
T->getDecl(),
T->getIndex(), PackIndex,
3185 SubstTemplateTypeParmTypeFlag::ExpandPacksInPlace);
3195 if (
T->getPackIndex()) {
3201 return inherited::TransformSubstTemplateTypeParmType(TLB, TL);
3206 return inherited::TransformTemplateArguments(Args.begin(), Args.end(), Out);
3215 bool EvaluateConstraints) {
3219 if (!EvaluateConstraints) {
3220 bool ShouldExpandExplicitTemplateArgs =
3222 llvm::any_of(TemplArgInfo->
arguments(), [](
auto &Arg) {
3223 return Arg.getArgument().containsUnexpandedParameterPack();
3240 if (ShouldExpandExplicitTemplateArgs) {
3244 if (ExpandPackedTypeConstraints(*
this, TemplateArgs)
3252 if (
auto *FE = dyn_cast<CXXFoldExpr>(ConstraintExpr)) {
3253 assert(FE->getLHS());
3254 ConstraintExpr = FE->getLHS();
3256 auto *CSE = cast<ConceptSpecializationExpr>(ConstraintExpr);
3257 assert(!CSE->getTemplateArguments().empty() &&
3258 "Empty template arguments?");
3259 ConstrainedType = CSE->getTemplateArguments()[0].getAsType();
3260 assert(!ConstrainedType.
isNull() &&
3261 "Failed to extract the original ConstrainedType?");
3267 Inst, ConstrainedType,
3300 int indexAdjustment, std::optional<unsigned> NumExpansions,
3301 bool ExpectParameterPack,
bool EvaluateConstraint) {
3310 NewDI =
SubstType(ExpansionTL.getPatternLoc(), TemplateArgs,
3321 }
else if (ExpectParameterPack) {
3327 diag::err_function_parameter_pack_without_parameter_packs)
3351 GetContainedInventedTypeParmVisitor().Visit(OldDI->
getType())) {
3353 auto *Inst = cast_or_null<TemplateTypeParmDecl>(
3358 if (Inst && !Inst->getTypeConstraint()) {
3427 "Cannot perform an instantiation without some context on the "
3428 "instantiation stack");
3430 TemplateInstantiator Instantiator(*
this, TemplateArgs,
Loc,
3432 return Instantiator.TransformFunctionTypeParams(
3433 Loc, Params,
nullptr, ExtParamInfos, ParamTypes, OutParams, ParamInfos);
3451 Diag(Param->
getBeginLoc(), diag::err_recursive_default_argument) << FD;
3463 std::unique_ptr<LocalInstantiationScope> LIS;
3471 LIS = std::make_unique<LocalInstantiationScope>(*
this);
3474 if (addInstantiatedParametersToScope(FD, PatternFD, *LIS, TemplateArgs))
3496 Result = InitSeq.Perform(*
this, Entity, Kind, ResultE);
3523 for (
const auto &
Base : Pattern->
bases()) {
3524 if (!
Base.getType()->isDependentType()) {
3526 if (RD->isInvalidDecl())
3535 if (
Base.isPackExpansion()) {
3541 bool ShouldExpand =
false;
3542 bool RetainExpansion =
false;
3543 std::optional<unsigned> NumExpansions;
3545 Base.getSourceRange(),
3547 TemplateArgs, ShouldExpand,
3556 for (
unsigned I = 0; I != *NumExpansions; ++I) {
3561 Base.getSourceRange().getBegin(),
3570 Base.getSourceRange(),
3572 Base.getAccessSpecifierAsWritten(),
3575 InstantiatedBases.push_back(InstantiatedBase);
3584 EllipsisLoc =
Base.getEllipsisLoc();
3588 Base.getSourceRange().getBegin(),
3593 Base.getSourceRange().getBegin(),
3604 Base.getSourceRange(),
3606 Base.getAccessSpecifierAsWritten(),
3609 InstantiatedBases.push_back(InstantiatedBase);
3641 Pattern, PatternDef, TSK, Complain))
3644 llvm::TimeTraceScope TimeScope(
"InstantiateClass", [&]() {
3645 llvm::TimeTraceMetadata M;
3646 llvm::raw_string_ostream
OS(M.Detail);
3649 if (llvm::isTimeTraceVerbose()) {
3657 Pattern = PatternDef;
3663 MSInfo->setPointOfInstantiation(PointOfInstantiation);
3665 = dyn_cast<ClassTemplateSpecializationDecl>(Instantiation)) {
3666 Spec->setTemplateSpecializationKind(TSK);
3667 Spec->setPointOfInstantiation(PointOfInstantiation);
3675 "instantiating class definition");
3693 SavePendingParsedClassStateRAII SavedPendingParsedClassState(*
this);
3719 bool MightHaveConstexprVirtualFunctions =
false;
3730 if (
Member->getDeclContext() != Pattern)
3737 if (isa<BlockDecl>(
Member) ||
3738 (isa<CXXRecordDecl>(
Member) && cast<CXXRecordDecl>(
Member)->isLambda()))
3741 if (
Member->isInvalidDecl()) {
3748 if (
FieldDecl *Field = dyn_cast<FieldDecl>(NewMember)) {
3749 Fields.push_back(Field);
3750 }
else if (
EnumDecl *
Enum = dyn_cast<EnumDecl>(NewMember)) {
3756 Enum->isCompleteDefinition()) {
3758 assert(MSInfo &&
"no spec info for member enum specialization");
3763 if (SA->isFailed()) {
3769 }
else if (
CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(NewMember)) {
3772 MightHaveConstexprVirtualFunctions =
true;
3793 if (ParsingClassDepth == 0)
3798 for (LateInstantiatedAttrVec::iterator I = LateAttrs.begin(),
3799 E = LateAttrs.end(); I !=
E; ++I) {
3804 auto *ND = cast<NamedDecl>(I->NewDecl);
3805 auto *ThisContext = dyn_cast_or_null<CXXRecordDecl>(ND->getDeclContext());
3807 ND->isCXXInstanceMember());
3812 I->NewDecl->addAttr(NewAttr);
3841 P->first,
P->second)) {
3854 P->first,
P->second)) {
3871 else if (MightHaveConstexprVirtualFunctions)
3888 Pattern, PatternDef, TSK,
true))
3890 Pattern = PatternDef;
3896 MSInfo->setPointOfInstantiation(PointOfInstantiation);
3905 "instantiating enum definition");
3940 "pattern and instantiation disagree about init style");
3948 Diag(PointOfInstantiation,
3949 diag::err_default_member_initializer_not_yet_parsed)
3950 << OutermostClass << Pattern;
3951 Diag(Pattern->getEndLoc(),
3952 diag::note_default_member_initializer_not_yet_parsed);
3962 Diag(PointOfInstantiation, diag::err_default_member_initializer_cycle)
3967 "instantiating default member init");
3975 PointOfInstantiation, Instantiation,
CurContext};
3986 assert((!
Init || !isa<ParenListExpr>(
Init)) &&
"call-style init in class");
3991 L->DefaultMemberInitializerInstantiated(Instantiation);
4000 struct PartialSpecMatchResult {
4026 !CTPSD->getMostRecentDecl()->isMemberSpecialization())
4056 if (!isa<ClassTemplatePartialSpecializationDecl *>(Specialized)) {
4085 !Partial->getMostRecentDecl()->isMemberSpecialization())
4099 Matched.push_back(PartialSpecMatchResult());
4100 Matched.back().Partial = Partial;
4109 if (Matched.size() >= 1) {
4111 if (Matched.size() == 1) {
4124 PEnd = Matched.end();
4127 P->Partial, Best->Partial, PointOfInstantiation) ==
4134 bool Ambiguous =
false;
4136 PEnd = Matched.end();
4139 P->Partial, Best->Partial,
4140 PointOfInstantiation) != Best->Partial) {
4150 S.
Diag(PointOfInstantiation,
4151 diag::err_partial_spec_ordering_ambiguous)
4152 << ClassTemplateSpec;
4156 PEnd = Matched.end();
4158 S.
Diag(
P->Partial->getLocation(), diag::note_partial_spec_match)
4160 P->Partial->getTemplateParameters(), *
P->Args);
4175 if (
auto *PartialSpec =
4178 while (PartialSpec->getInstantiatedFromMember()) {
4181 if (PartialSpec->isMemberSpecialization())
4184 PartialSpec = PartialSpec->getInstantiatedFromMember();
4186 Pattern = PartialSpec;
4208 ClassTemplateSpec = cast<ClassTemplateSpecializationDecl>(
4215 ClassTemplateSpec, TSK);
4220 PointOfInstantiation, ClassTemplateSpec, Pattern.
get(),
4236 "Unexpected template specialization kind!");
4237 for (
auto *
D : Instantiation->
decls()) {
4238 bool SuppressNew =
false;
4239 if (
auto *
Function = dyn_cast<FunctionDecl>(
D)) {
4241 Function->getInstantiatedFromMemberFunction()) {
4243 if (
Function->isIneligibleOrNotSelected())
4246 if (
Function->getTrailingRequiresClause()) {
4254 if (
Function->hasAttr<ExcludeFromExplicitInstantiationAttr>())
4258 Function->getTemplateSpecializationKind();
4263 PointOfInstantiation, TSK,
Function, PrevTSK,
4264 Function->getPointOfInstantiation(), SuppressNew) ||
4277 Function->setTemplateSpecializationKind(TSK, PointOfInstantiation);
4287 std::make_pair(
Function, PointOfInstantiation));
4290 }
else if (
auto *Var = dyn_cast<VarDecl>(
D)) {
4291 if (isa<VarTemplateSpecializationDecl>(Var))
4295 if (Var->
hasAttr<ExcludeFromExplicitInstantiationAttr>())
4299 assert(MSInfo &&
"No member specialization information?");
4328 }
else if (
auto *
Record = dyn_cast<CXXRecordDecl>(
D)) {
4329 if (
Record->hasAttr<ExcludeFromExplicitInstantiationAttr>())
4337 if (
Record->isInjectedClassName() ||
Record->getPreviousDecl() ||
4342 assert(MSInfo &&
"No member specialization information?");
4368 assert(Pattern &&
"Missing instantiated-from-template information");
4370 if (!
Record->getDefinition()) {
4391 Record->getTemplateSpecializationKind() ==
4393 Record->setTemplateSpecializationKind(TSK);
4398 Pattern = cast_or_null<CXXRecordDecl>(
Record->getDefinition());
4402 }
else if (
auto *
Enum = dyn_cast<EnumDecl>(
D)) {
4404 assert(MSInfo &&
"No member specialization information?");
4411 PointOfInstantiation, TSK,
Enum,
4417 if (
Enum->getDefinition())
4420 EnumDecl *Pattern =
Enum->getTemplateInstantiationPattern();
4421 assert(Pattern &&
"Missing instantiated-from-template information");
4432 }
else if (
auto *Field = dyn_cast<FieldDecl>(
D)) {
4439 ClassPattern->
lookup(Field->getDeclName());
4472 TemplateInstantiator Instantiator(*
this, TemplateArgs,
4475 return Instantiator.TransformStmt(S);
4483 TemplateInstantiator Instantiator(*
this, TemplateArgs,
Loc, Entity);
4484 return Instantiator.TransformTemplateArgument(Input, Output);
4491 TemplateInstantiator Instantiator(*
this, TemplateArgs,
SourceLocation(),
4493 return Instantiator.TransformTemplateArguments(Args.begin(), Args.end(), Out);
4501 TemplateInstantiator Instantiator(*
this, TemplateArgs,
4504 return Instantiator.TransformExpr(
E);
4520 TemplateInstantiator Instantiator(*
this, TemplateArgs,
SourceLocation(),
4522 Instantiator.setEvaluateConstraints(
false);
4523 return Instantiator.TransformExpr(
E);
4528 bool CXXDirectInit) {
4529 TemplateInstantiator Instantiator(*
this, TemplateArgs,
SourceLocation(),
4531 return Instantiator.TransformInitializer(
Init, CXXDirectInit);
4540 TemplateInstantiator Instantiator(*
this, TemplateArgs,
4543 return Instantiator.TransformExprs(Exprs.data(), Exprs.size(),
4553 TemplateInstantiator Instantiator(*
this, TemplateArgs, NNS.
getBeginLoc(),
4555 return Instantiator.TransformNestedNameSpecifierLoc(NNS);
4561 TemplateInstantiator Instantiator(*
this, TemplateArgs, NameInfo.
getLoc(),
4563 return Instantiator.TransformDeclarationNameInfo(NameInfo);
4570 TemplateInstantiator Instantiator(*
this, TemplateArgs,
Loc,
4573 SS.
Adopt(QualifierLoc);
4574 return Instantiator.TransformTemplateName(SS, Name,
Loc);
4582 if (
const ParmVarDecl *PV = dyn_cast<ParmVarDecl>(
D)) {
4583 if (
const FunctionDecl *FD = dyn_cast<FunctionDecl>(PV->getDeclContext())) {
4584 unsigned i = PV->getFunctionScopeIndex();
4587 if (i < FD->getNumParams() && FD->getParamDecl(i) == PV)
4595llvm::PointerUnion<Decl *, LocalInstantiationScope::DeclArgumentPack *> *
4599 Current = Current->Outer) {
4602 const Decl *CheckD =
D;
4604 LocalDeclsMap::iterator
Found = Current->LocalDecls.find(CheckD);
4605 if (
Found != Current->LocalDecls.end())
4606 return &
Found->second;
4610 if (
const TagDecl *Tag = dyn_cast<TagDecl>(CheckD))
4611 CheckD = Tag->getPreviousDecl();
4617 if (!Current->CombineWithOuterScope)
4623 if (isa<NonTypeTemplateParmDecl>(
D) || isa<TemplateTypeParmDecl>(
D) ||
4624 isa<TemplateTemplateParmDecl>(
D))
4629 if (RD->isLocalClass())
4634 if (isa<EnumDecl>(
D))
4639 if (isa<TypedefNameDecl>(
D) &&
4646 assert(isa<LabelDecl>(
D) &&
"declaration not instantiated in this scope");
4652 llvm::PointerUnion<Decl *, DeclArgumentPack *> &Stored = LocalDecls[
D];
4653 if (Stored.isNull()) {
4657 while (Current->CombineWithOuterScope && Current->Outer) {
4658 Current = Current->Outer;
4659 assert(!Current->LocalDecls.contains(
D) &&
4660 "Instantiated local in inner and outer scopes");
4665 Pack->push_back(cast<VarDecl>(Inst));
4667 assert(cast<Decl *>(Stored) == Inst &&
"Already instantiated this local");
4675 Pack->push_back(Inst);
4682 Current && Current->CombineWithOuterScope; Current = Current->Outer)
4683 assert(!Current->LocalDecls.contains(
D) &&
4684 "Creating local pack after instantiation of local");
4688 llvm::PointerUnion<Decl *, DeclArgumentPack *> &Stored = LocalDecls[
D];
4691 ArgumentPacks.push_back(Pack);
4696 if (llvm::is_contained(*Pack,
D))
4703 unsigned NumExplicitArgs) {
4704 assert((!PartiallySubstitutedPack || PartiallySubstitutedPack == Pack) &&
4705 "Already have a partially-substituted pack");
4706 assert((!PartiallySubstitutedPack
4707 || NumArgsInPartiallySubstitutedPack == NumExplicitArgs) &&
4708 "Wrong number of arguments in partially-substituted pack");
4709 PartiallySubstitutedPack = Pack;
4710 ArgsInPartiallySubstitutedPack = ExplicitArgs;
4711 NumArgsInPartiallySubstitutedPack = NumExplicitArgs;
4716 unsigned *NumExplicitArgs)
const {
4718 *ExplicitArgs =
nullptr;
4719 if (NumExplicitArgs)
4720 *NumExplicitArgs = 0;
4723 Current = Current->Outer) {
4724 if (Current->PartiallySubstitutedPack) {
4726 *ExplicitArgs = Current->ArgsInPartiallySubstitutedPack;
4727 if (NumExplicitArgs)
4728 *NumExplicitArgs = Current->NumArgsInPartiallySubstitutedPack;
4730 return Current->PartiallySubstitutedPack;
4733 if (!Current->CombineWithOuterScope)
This file provides AST data structures related to concepts.
Defines the clang::ASTContext interface.
This file provides some common utility functions for processing Lambda related AST Constructs.
Defines the C++ template declaration subclasses.
Defines Expressions and AST nodes for C++2a concepts.
static void print(llvm::raw_ostream &OS, const T &V, ASTContext &ASTCtx, QualType Ty)
Defines the clang::LangOptions interface.
llvm::MachO::Record Record
MatchFinder::MatchResult MatchResult
static const Decl * getCanonicalParmVarDecl(const Decl *D)
static bool NeedsInstantiationAsFunctionType(TypeSourceInfo *T)
static concepts::Requirement::SubstitutionDiagnostic * createSubstDiag(Sema &S, TemplateDeductionInfo &Info, Sema::EntityPrinter Printer)
static ActionResult< CXXRecordDecl * > getPatternForClassTemplateSpecialization(Sema &S, SourceLocation PointOfInstantiation, ClassTemplateSpecializationDecl *ClassTemplateSpec, TemplateSpecializationKind TSK)
Get the instantiation pattern to use to instantiate the definition of a given ClassTemplateSpecializa...
static std::string convertCallArgsToString(Sema &S, llvm::ArrayRef< const Expr * > Args)
static TemplateArgument getPackSubstitutedTemplateArgument(Sema &S, TemplateArgument Arg)
Defines the clang::TypeLoc interface and its subclasses.
C Language Family Type Representation.
virtual void HandleTagDeclDefinition(TagDecl *D)
HandleTagDeclDefinition - This callback is invoked each time a TagDecl (e.g.
virtual bool HandleTopLevelDecl(DeclGroupRef D)
HandleTopLevelDecl - Handle the specified top-level declaration.
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
TranslationUnitDecl * getTranslationUnitDecl() const
QualType getTagDeclType(const TagDecl *Decl) const
Return the unique reference to the type for the specified TagDecl (struct/union/class/enum) decl.
bool hasSameType(QualType T1, QualType T2) const
Determine whether the given types T1 and T2 are equivalent.
QualType getSubstTemplateTypeParmType(QualType Replacement, Decl *AssociatedDecl, unsigned Index, std::optional< unsigned > PackIndex, SubstTemplateTypeParmTypeFlag Flag=SubstTemplateTypeParmTypeFlag::None) const
Retrieve a substitution-result type.
QualType getTypeDeclType(const TypeDecl *Decl, const TypeDecl *PrevDecl=nullptr) const
Return the unique reference to the type for the specified type declaration.
QualType getQualifiedType(SplitQualType split) const
Un-split a SplitQualType.
const clang::PrintingPolicy & getPrintingPolicy() const
const TargetInfo & getTargetInfo() const
Represents a type which was implicitly adjusted by the semantic engine for arbitrary reasons.
Represents an array type, per C99 6.7.5.2 - Array Declarators.
Attr - This represents one attribute.
An attributed type is a type to which a type attribute has been applied.
BlockExpr - Adaptor class for mixing a BlockDecl with expressions.
Represents a base class of a C++ class.
A default argument (C++ [dcl.fct.default]).
Represents a static or instance method of a struct/union/class.
Represents a C++ struct/union/class.
Decl * getLambdaContextDecl() const
Retrieve the declaration that provides additional context for a lambda, when the normal declaration c...
const FunctionDecl * isLocalClass() const
If the class is a local class [class.local], returns the enclosing function declaration.
CXXRecordDecl * getInstantiatedFromMemberClass() const
If this record is an instantiation of a member class, retrieves the member class from which it was in...
bool isLambda() const
Determine whether this class describes a lambda function object.
CXXRecordDecl * getDefinition() const
unsigned getNumBases() const
Retrieves the number of base classes of this class.
const CXXRecordDecl * getTemplateInstantiationPattern() const
Retrieve the record declaration from which this record could be instantiated.
TemplateSpecializationKind getTemplateSpecializationKind() const
Determine whether this particular class is a specialization or instantiation of a class template or m...
ClassTemplateDecl * getDescribedClassTemplate() const
Retrieves the class template that is described by this class declaration.
MemberSpecializationInfo * getMemberSpecializationInfo() const
If this class is an instantiation of a member class of a class template specialization,...
CXXMethodDecl * getLambdaCallOperator() const
Retrieve the lambda call operator of the closure type if this is a closure type.
CXXRecordDecl * getCanonicalDecl() override
Retrieves the "canonical" declaration of the given declaration.
void setTemplateSpecializationKind(TemplateSpecializationKind TSK)
Set the kind of specialization or template instantiation this is.
Represents a C++ nested-name-specifier or a global scope specifier.
<