[clang] [libcxx] [Clang] Implement CWG2369 "Ordering between constraints and substitution" (PR #102857)
via cfe-commits
cfe-commits at lists.llvm.org
Sun Aug 18 00:32:51 PDT 2024
================
@@ -1122,6 +1122,154 @@ bool Sema::CheckInstantiatedFunctionTemplateConstraints(
PointOfInstantiation, Satisfaction);
}
+namespace {
+
+// We employ a TreeTransform because RAV couldn't recurse into a bunch of
+// Exprs e.g. SizeOfPackExpr, CXXFoldExpr, etc.
+// FIXME: Could we do the Decl instantiation as we substitute into
+// the constraint expressions?
+class InstantiateReferencedParameter
+ : public TreeTransform<InstantiateReferencedParameter> {
+ const MultiLevelTemplateArgumentList &TemplateArgs;
+
+ llvm::SmallPtrSet<ParmVarDecl *, 4> InstantiatedDecls;
+
+ FunctionDecl *PrimaryTemplatedFunction;
+
+ using inherited = TreeTransform<InstantiateReferencedParameter>;
+
+ bool instantiateParameterToScope(ParmVarDecl *OldParm,
+ LocalInstantiationScope &Scope) {
+ // The current context might have been changed by lambda expressions. So
+ // resume it before we substitute into parameters.
+ Sema::ContextRAII Context(SemaRef, PrimaryTemplatedFunction);
+ std::optional<unsigned> NumExpansions;
+ ParmVarDecl *NewParm = nullptr;
+ unsigned IndexAdjustment = 0;
+ if (OldParm->isParameterPack()) {
+ SmallVector<UnexpandedParameterPack, 2> Unexpanded;
+ TypeLoc TL = OldParm->getTypeSourceInfo()->getTypeLoc();
+ PackExpansionTypeLoc ExpansionTL = TL.castAs<PackExpansionTypeLoc>();
+ TypeLoc Pattern = ExpansionTL.getPatternLoc();
+ SemaRef.collectUnexpandedParameterPacks(Pattern, Unexpanded);
+
+ assert(!Unexpanded.empty() &&
+ "A pack Decl doesn't contain anything unexpanded?");
+
+ bool ShouldExpand = false;
+ bool RetainExpansion = false;
+ std::optional<unsigned> OrigNumExpansions =
+ ExpansionTL.getTypePtr()->getNumExpansions();
+ NumExpansions = OrigNumExpansions;
+ if (SemaRef.CheckParameterPacksForExpansion(
+ ExpansionTL.getEllipsisLoc(), Pattern.getSourceRange(),
+ Unexpanded, TemplateArgs, ShouldExpand, RetainExpansion,
+ NumExpansions))
+ return true;
+
+ assert(ShouldExpand && !RetainExpansion &&
+ "Shouldn't retain an expansion here!");
+ Scope.MakeInstantiatedLocalArgPack(OldParm);
+
+ for (unsigned I = 0; I != *NumExpansions; ++I) {
+ Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, I);
+ ParmVarDecl *NewParm = SemaRef.SubstParmVarDecl(
+ OldParm, TemplateArgs, /*indexAdjustment=*/IndexAdjustment++,
+ NumExpansions, /*ExpectParameterPack=*/false,
+ /*EvaluateConstraints=*/false);
+ if (!NewParm)
+ return true;
+ }
+
+ return false;
+ }
+ NewParm = SemaRef.SubstParmVarDecl(OldParm, TemplateArgs,
+ /*indexAdjustment=*/IndexAdjustment,
+ std::nullopt,
+ /*ExpectParameterPack=*/false);
+ if (!NewParm)
+ return true;
+ Scope.InstantiatedLocal(OldParm, NewParm);
+ return false;
+ }
+
+public:
+ InstantiateReferencedParameter(
+ Sema &SemaRef, const MultiLevelTemplateArgumentList &TemplateArgs,
+ FunctionDecl *PrimaryTemplatedFunction)
+ : inherited(SemaRef), TemplateArgs(TemplateArgs),
+ PrimaryTemplatedFunction(PrimaryTemplatedFunction) {}
+
+ Decl *TransformDecl(SourceLocation Loc, Decl *D) {
+ if (auto *PVD = dyn_cast_if_present<ParmVarDecl>(D);
+ PVD && PVD->getDeclContext() == PrimaryTemplatedFunction &&
+ !InstantiatedDecls.contains(PVD)) {
+ instantiateParameterToScope(PVD, *SemaRef.CurrentInstantiationScope);
----------------
cor3ntin wrote:
See also https://github.com/cplusplus/CWG/issues/364
https://cplusplus.github.io/CWG/issues/2769.html
https://gcc.godbolt.org/z/WrfG5Gxaj
I think instead of doing the substitution there, we want to do it where we check atomic constraints
https://github.com/llvm/llvm-project/pull/102857
More information about the cfe-commits
mailing list